From: Richard Palethorpe <rpalethorpe@suse.de>
To: Alessandro Carminati <alessandro.carminati@gmail.com>
Cc: acarmina@redhat.com, ltp@lists.linux.it
Subject: Re: [LTP] [PATCH 1/2] tst_find_backing_dev: Get dev name from /sys/dev/block/*/uevent
Date: Tue, 08 Nov 2022 09:39:44 +0000 [thread overview]
Message-ID: <877d05wxwe.fsf@suse.de> (raw)
In-Reply-To: <20221107163957.721315-2-alessandro.carminati@gmail.com>
Hello,
I'd like to merge, but discovered some more issues that requuire more
than a fixup before merge.
Also please use the -v flag in git format-patch to version the patches
after the first revision. I don't mind which version you start at now.
Alessandro Carminati <alessandro.carminati@gmail.com> writes:
> In some minimal Linux, the /dev/root can be missing. The consequence of
> this is that mountinfo doesn't contain the correct information. btrfs
> file systems are yet another point of trouble for this function.
>
> The unevent file in sysfs is another method to retrieve device info
> using the sysfs.
>
> btrfs file systems are special from the device name retrieval, and in
> place of use of the minor/major they are approached by using the uuid.
> In the end, btrfs strategy is a slightly modified version of the same
> unevent strategy.
>
> Non btrfs look in "/sys/dev/block/%d:%d/uevent" major, minor
> btrfs look in /sys/fs/btrfs/%s/devices/%s/uevent, uuid, devname
>
> The btrfs handling requires BTRFS specific ioctl for finding the
> file system uuid, and for this reason, btrfs/ioctl.h is needed.
>
> Signed-off-by: Richard Palethorpe <rpalethorpe@suse.com>
> Signed-off-by: Alessandro Carminati <alessandro.carminati@gmail.com>
> ---
> lib/tst_device.c | 91 ++++++++++++++++++++++++++++++++----------------
> 1 file changed, 61 insertions(+), 30 deletions(-)
>
> diff --git a/lib/tst_device.c b/lib/tst_device.c
> index 8419b80c3..054e39bcd 100644
> --- a/lib/tst_device.c
> +++ b/lib/tst_device.c
> @@ -33,6 +33,9 @@
> #include <stdint.h>
> #include <inttypes.h>
> #include <sys/sysmacros.h>
> +#include <linux/btrfs.h>
> +#include <linux/limits.h>
> +#include <dirent.h>
> #include "lapi/syscalls.h"
> #include "test.h"
> #include "safe_macros.h"
> @@ -45,6 +48,8 @@
>
> #define DEV_FILE "test_dev.img"
> #define DEV_SIZE_MB 300u
> +#define UUID_STR_SZ 37
> +#define UUID_FMT "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x"
>
> static char dev_path[1024];
> static int device_acquired;
> @@ -519,48 +524,74 @@ static int count_match_len(const char *first, const char *second)
> void tst_find_backing_dev(const char *path, char *dev)
> {
> struct stat buf;
> - FILE *file;
> - char line[PATH_MAX];
> - char *pre = NULL;
> - char *next = NULL;
> - unsigned int dev_major, dev_minor, line_mjr, line_mnr;
> - unsigned int len, best_match_len = 1;
> - char mnt_point[PATH_MAX];
> + struct btrfs_ioctl_fs_info_args args = {0};
> + struct dirent *d;
> + char uevent_path[PATH_MAX];
> + char dev_name[NAME_MAX];
> + char bdev_path[PATH_MAX];
> + char btrfs_uuid_str[UUID_STR_SZ];
> + DIR *dir;
> + unsigned int dev_major, dev_minor;
> + int fd;
>
> if (stat(path, &buf) < 0)
> tst_brkm(TWARN | TERRNO, NULL, "stat() failed");
>
> dev_major = major(buf.st_dev);
> dev_minor = minor(buf.st_dev);
> - file = SAFE_FOPEN(NULL, "/proc/self/mountinfo", "r");
> *dev = '\0';
>
> - while (fgets(line, sizeof(line), file)) {
> - if (sscanf(line, "%*d %*d %d:%d %*s %s",
> - &line_mjr, &line_mnr, mnt_point) != 3)
> - continue;
> -
> - pre = strstr(line, " - ");
> - pre = strtok_r(pre, " ", &next);
> - pre = strtok_r(NULL, " ", &next);
> - pre = strtok_r(NULL, " ", &next);
> -
> - if (line_mjr == dev_major && line_mnr == dev_minor) {
> - strcpy(dev, pre);
> - break;
> - }
> -
> - len = count_match_len(path, mnt_point);
> - if (len > best_match_len) {
> - strcpy(dev, pre);
> - best_match_len = len;
> + if (dev_major == 0) {
> + tst_resm(TINFO, "Use BTRFS specific strategy");
> +
> + fd = SAFE_OPEN(NULL, dirname(path), O_DIRECTORY);
There are two problems here. One is simple and that dirname can modify
path, but path is a const pointer (compiler should warn about dropping
const modifiers). The simple solution is just to copy path into a buffer.
Secondly ioctl_loop05 passes the path to an image, but the self test in
/lib/newlib_tests/tst_device.c passes the mount point. So unless I am
mistaken dirname will return the dir below the mount point which is
wrong.
One option is to try opening path as a dir first and if that fails, use
dirname to get the containing folder. Changeing ioctl_loop05 would also
be valid.
> + if (!ioctl(fd, BTRFS_IOC_FS_INFO, &args)) {
> + sprintf(btrfs_uuid_str,
> + UUID_FMT,
> + args.fsid[0], args.fsid[1],
> + args.fsid[2], args.fsid[3],
> + args.fsid[4], args.fsid[5],
> + args.fsid[6], args.fsid[7],
> + args.fsid[8], args.fsid[9],
> + args.fsid[10], args.fsid[11],
> + args.fsid[12], args.fsid[13],
> + args.fsid[14], args.fsid[15]);
> + sprintf(bdev_path,
> + "/sys/fs/btrfs/%s/devices", btrfs_uuid_str);
> + } else {
> + tst_brkm(TBROK, NULL, "BTRFS ioctl failed. Is %s
> on a tmpfs?", path);
Need TERRNO here and/or check that the errorno is ENOTTY otherwise the
hint makes no sense.
> + }
> + SAFE_CLOSE(NULL, fd);
> + dir = SAFE_OPENDIR(NULL, bdev_path);
> + while (d = SAFE_READDIR(NULL, dir)) {
> + if (d->d_name[0]!='.')
There are a few formatting errors like the missing spaces around !=.
Run make check-tst_device in the lib dir and see the kernel style
guidelines.
> + break;
> }
> + uevent_path[0] = '\0';
> + if (d) {
> + sprintf(uevent_path, "%s/%s/uevent",
> + bdev_path, d->d_name);
> + } else {
> + tst_brkm(TBROK, NULL, "No backining device
> found");
Still need to print some information about where we are looking (bdev_path).
> + }
> + if (SAFE_READDIR(NULL, dir))
> + tst_resm(TINFO, "Warning: used first of multiple backing device.");
> + SAFE_CLOSEDIR(NULL, dir);
> + } else {
> +
> + tst_resm(TINFO, "Use uevent strategy");
> + sprintf(uevent_path,
> + "/sys/dev/block/%d:%d/uevent", dev_major, dev_minor);
> }
>
> - SAFE_FCLOSE(NULL, file);
> + if (!access(uevent_path, R_OK)) {
> + FILE_LINES_SCANF(NULL, uevent_path, "DEVNAME=%s", dev_name);
>
> - if (!*dev)
> - tst_brkm(TBROK, NULL, "Cannot find block device for %s", path);
> + if (dev_name[0])
> + sprintf(dev, "/dev/%s", dev_name);
> + } else {
> + tst_brkm(TBROK, NULL, "uevent file (%s) access failed",
> uevent_path);
Also we can use (TBROK | TERRNO) here as access sets that.
> + }
make check somehow missing this. The } is indented too far.
>
> if (stat(dev, &buf) < 0)
> tst_brkm(TWARN | TERRNO, NULL, "stat(%s) failed", dev);
--
Thank you,
Richard.
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2022-11-08 10:20 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <[LTP] [PATCH] Fix tst_find_backing_dev when no initramfs>
2022-10-26 14:04 ` [LTP] [PATCH 0/2] tst_find_backing_dev: fix stat fails /dev/root Alessandro Carminati
2022-10-26 14:04 ` [LTP] [PATCH 1/2] tst_find_backing_dev: Get dev name from /sys/dev/block/*/uevent Alessandro Carminati
2022-10-27 8:08 ` Richard Palethorpe
2022-10-27 18:58 ` Alessandro Carminati
2022-10-31 12:08 ` Richard Palethorpe
2022-10-31 16:57 ` Alessandro Carminati
2022-11-01 9:09 ` Richard Palethorpe
2022-11-02 20:34 ` [LTP] [PATCH 0/2] tst_find_backing_dev: fix stat fails /dev/root Alessandro Carminati
2022-11-02 20:34 ` [LTP] [PATCH 1/2] tst_find_backing_dev: Get dev name from /sys/dev/block/*/uevent Alessandro Carminati
2022-11-03 8:04 ` Richard Palethorpe
2022-11-04 4:41 ` [LTP] [PATCH 0/2] Fix tst_find_backing_dev adding BTRFS support and /dev/root missing case Alessandro Carminati
2022-11-04 4:41 ` [LTP] [PATCH 1/2] tst_find_backing_dev: Get dev name from /sys/*/uevent Alessandro Carminati
2022-11-07 8:46 ` Richard Palethorpe
2022-11-07 16:39 ` [LTP] [PATCH 0/2] Fix tst_find_backing_dev adding BTRFS support and /dev/root missing case Alessandro Carminati
2022-11-07 16:39 ` [LTP] [PATCH 1/2] tst_find_backing_dev: Get dev name from /sys/dev/block/*/uevent Alessandro Carminati
2022-11-08 9:39 ` Richard Palethorpe [this message]
2022-11-09 19:38 ` [LTP] [PATCH v6 0/2] tst_find_backing_dev: fix stat fails /dev/root Alessandro Carminati
2022-11-09 19:38 ` [LTP] [PATCH v6 1/2] tst_find_backing_dev: Get dev name from /sys/dev/block/*/uevent Alessandro Carminati
2022-11-10 11:16 ` Richard Palethorpe
2022-11-09 19:38 ` [LTP] [PATCH v6 2/2] c-test-api: Documentation updated Alessandro Carminati
2022-11-10 11:29 ` Richard Palethorpe
2022-11-07 16:39 ` [LTP] [PATCH " Alessandro Carminati
2022-11-04 4:41 ` Alessandro Carminati
2022-11-07 8:12 ` [LTP] [PATCH 0/2] Fix tst_find_backing_dev adding BTRFS support and /dev/root missing case Richard Palethorpe
2022-11-02 20:34 ` [LTP] [PATCH 2/2] c-test-api: Documentation updated Alessandro Carminati
2022-10-26 14:04 ` Alessandro Carminati
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=877d05wxwe.fsf@suse.de \
--to=rpalethorpe@suse.de \
--cc=acarmina@redhat.com \
--cc=alessandro.carminati@gmail.com \
--cc=ltp@lists.linux.it \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.