diff mbox series

[v2] tst_find_backing_dev: match mount point if major/minor can't be found

Message ID 65a82dbc6ea54c5d02a1238f5fa26d19388c5d2a.1652789936.git.jstancek@redhat.com
State Accepted, archived
Headers show
Series [v2] tst_find_backing_dev: match mount point if major/minor can't be found | expand

Commit Message

Jan Stancek May 17, 2022, 12:19 p.m. UTC
ioctl_loop05 fails on btrfs, because tst_find_backing_dev doesn't find
major/minor returned by stat()

Per https://lwn.net/Articles/866582
  "btrfs allocates a separate device number (the usual major/minor pair)
  for each subvolume ... and call to on a file within a subvolume will
  return a device number that does not exist in files like mountinfo."

As fallback, if there's no major/minor match, use best match of mount path.

Signed-off-by: Jan Stancek <jstancek@redhat.com>
---
 lib/tst_device.c | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)

Comments

Cyril Hrubis May 17, 2022, 3:33 p.m. UTC | #1
Hi!
> +		len = count_match_len(path, mnt_point);
> +		if (len > best_match_len) {

I wonder if we should also check for minimal len to avoid trival prefix
matches such as "/". Maybe we should set the best_match_len to 1 to
begin with.

> +			strcpy(dev, pre);
> +			best_match_len = len;
> +		}
>  	}
>  
>  	SAFE_FCLOSE(NULL, file);
> -- 
> 2.27.0
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp
Jan Stancek May 19, 2022, 9:56 a.m. UTC | #2
On Tue, May 17, 2022 at 5:30 PM Cyril Hrubis <chrubis@suse.cz> wrote:
>
> Hi!
> > +             len = count_match_len(path, mnt_point);
> > +             if (len > best_match_len) {
>
> I wonder if we should also check for minimal len to avoid trival prefix
> matches such as "/". Maybe we should set the best_match_len to 1 to
> begin with.

Sure, we can do that. Would you ack this for release as well?
This has been failing for quite a while on fedora (which uses btrfs).

>
> > +                     strcpy(dev, pre);
> > +                     best_match_len = len;
> > +             }
> >       }
> >
> >       SAFE_FCLOSE(NULL, file);
> > --
> > 2.27.0
> >
> >
> > --
> > Mailing list info: https://lists.linux.it/listinfo/ltp
>
> --
> Cyril Hrubis
> chrubis@suse.cz
>
Cyril Hrubis May 19, 2022, 10:01 a.m. UTC | #3
Hi!
> Sure, we can do that. Would you ack this for release as well?
> This has been failing for quite a while on fedora (which uses btrfs).

Sure, either way this version looks good.

Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
Jan Stancek May 19, 2022, 10:13 a.m. UTC | #4
On Thu, May 19, 2022 at 11:59 AM Cyril Hrubis <chrubis@suse.cz> wrote:
>
> Hi!
> > Sure, we can do that. Would you ack this for release as well?
> > This has been failing for quite a while on fedora (which uses btrfs).
>
> Sure, either way this version looks good.
>
> Reviewed-by: Cyril Hrubis <chrubis@suse.cz>

pushed
diff mbox series

Patch

diff --git a/lib/tst_device.c b/lib/tst_device.c
index d296f9118cde..b1af0f0146d4 100644
--- a/lib/tst_device.c
+++ b/lib/tst_device.c
@@ -506,6 +506,16 @@  unsigned long tst_dev_bytes_written(const char *dev)
 	return dev_bytes_written;
 }
 
+static int count_match_len(const char *first, const char *second)
+{
+	int len = 0;
+
+	while (*first && *first++ == *second++)
+		len++;
+
+	return len;
+}
+
 void tst_find_backing_dev(const char *path, char *dev)
 {
 	struct stat buf;
@@ -514,6 +524,8 @@  void tst_find_backing_dev(const char *path, char *dev)
 	char *pre = NULL;
 	char *next = NULL;
 	unsigned int dev_major, dev_minor, line_mjr, line_mnr;
+	unsigned int len, best_match_len = 0;
+	char mnt_point[PATH_MAX];
 
 	if (stat(path, &buf) < 0)
 		tst_brkm(TWARN | TERRNO, NULL, "stat() failed");
@@ -524,17 +536,25 @@  void tst_find_backing_dev(const char *path, char *dev)
 	*dev = '\0';
 
 	while (fgets(line, sizeof(line), file)) {
-		if (sscanf(line, "%*d %*d %d:%d", &line_mjr, &line_mnr) != 2)
+		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) {
-			pre = strstr(line, " - ");
-			pre = strtok_r(pre, " ", &next);
-			pre = strtok_r(NULL, " ", &next);
-			pre = strtok_r(NULL, " ", &next);
 			strcpy(dev, pre);
 			break;
 		}
+
+		len = count_match_len(path, mnt_point);
+		if (len > best_match_len) {
+			strcpy(dev, pre);
+			best_match_len = len;
+		}
 	}
 
 	SAFE_FCLOSE(NULL, file);