dcavalca / rpms / mdadm

Forked from rpms/mdadm 3 years ago
Clone

Blame SOURCES/mdadm-util-unify-fstat-checking-blkdev1-into-function.patch

2c1b57
From 0a6bff09d41650f27136d56a0604c9af46b6f583 Mon Sep 17 00:00:00 2001
2c1b57
From: Zhilong Liu <zlliu@suse.com>
2c1b57
Date: Thu, 4 May 2017 20:16:21 +0800
2c1b57
Subject: [RHEL7.5 PATCH 109/169] mdadm/util: unify fstat checking blkdev
2c1b57
 into function
2c1b57
2c1b57
declare function fstat_is_blkdev() to integrate repeated fstat
2c1b57
checking block device operations, it returns true/1 when it is
2c1b57
a block device, and returns false/0 when it isn't.
2c1b57
The fd and devname are necessary parameters, *rdev is optional,
2c1b57
parse the pointer of dev_t *rdev, if valid, assigned the device
2c1b57
number to dev_t *rdev, if NULL, ignores.
2c1b57
2c1b57
Signed-off-by: Zhilong Liu <zlliu@suse.com>
2c1b57
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
2c1b57
---
2c1b57
 Assemble.c    | 19 +++++++------------
2c1b57
 Build.c       |  5 +++--
2c1b57
 Create.c      | 23 ++++++++++-------------
2c1b57
 Grow.c        | 10 ++++------
2c1b57
 Incremental.c | 33 ++++++++++++---------------------
2c1b57
 Manage.c      |  2 +-
2c1b57
 bitmap.c      | 13 ++++---------
2c1b57
 mdadm.h       |  1 +
2c1b57
 super-intel.c | 13 +++----------
2c1b57
 util.c        | 17 +++++++++++++++++
2c1b57
 10 files changed, 62 insertions(+), 74 deletions(-)
2c1b57
2c1b57
diff --git a/Assemble.c b/Assemble.c
2c1b57
index a9442c8..9d0a89f 100644
2c1b57
--- a/Assemble.c
2c1b57
+++ b/Assemble.c
2c1b57
@@ -149,6 +149,7 @@ static int select_devices(struct mddev_dev *devlist,
2c1b57
 	struct mdinfo *content = NULL;
2c1b57
 	int report_mismatch = ((inargv && c->verbose >= 0) || c->verbose > 0);
2c1b57
 	struct domainlist *domains = NULL;
2c1b57
+	dev_t rdev;
2c1b57
 
2c1b57
 	tmpdev = devlist; num_devs = 0;
2c1b57
 	while (tmpdev) {
2c1b57
@@ -169,7 +170,6 @@ static int select_devices(struct mddev_dev *devlist,
2c1b57
 	     tmpdev = tmpdev ? tmpdev->next : NULL) {
2c1b57
 		char *devname = tmpdev->devname;
2c1b57
 		int dfd;
2c1b57
-		struct stat stb;
2c1b57
 		struct supertype *tst;
2c1b57
 		struct dev_policy *pol = NULL;
2c1b57
 		int found_container = 0;
2c1b57
@@ -204,14 +204,7 @@ static int select_devices(struct mddev_dev *devlist,
2c1b57
 				pr_err("cannot open device %s: %s\n",
2c1b57
 				       devname, strerror(errno));
2c1b57
 			tmpdev->used = 2;
2c1b57
-		} else if (fstat(dfd, &stb)< 0) {
2c1b57
-			/* Impossible! */
2c1b57
-			pr_err("fstat failed for %s: %s\n",
2c1b57
-			       devname, strerror(errno));
2c1b57
-			tmpdev->used = 2;
2c1b57
-		} else if ((stb.st_mode & S_IFMT) != S_IFBLK) {
2c1b57
-			pr_err("%s is not a block device.\n",
2c1b57
-			       devname);
2c1b57
+		} else if (!fstat_is_blkdev(dfd, devname, &rdev)) {
2c1b57
 			tmpdev->used = 2;
2c1b57
 		} else if (must_be_container(dfd)) {
2c1b57
 			if (st) {
2c1b57
@@ -234,7 +227,8 @@ static int select_devices(struct mddev_dev *devlist,
2c1b57
 					       devname);
2c1b57
 				tmpdev->used = 2;
2c1b57
 			} else if (auto_assem &&
2c1b57
-				   !conf_test_metadata(tst->ss->name, (pol = devid_policy(stb.st_rdev)),
2c1b57
+				   !conf_test_metadata(tst->ss->name,
2c1b57
+						       (pol = devid_policy(rdev)),
2c1b57
 						       tst->ss->match_home(tst, c->homehost) == 1)) {
2c1b57
 				if (report_mismatch)
2c1b57
 					pr_err("%s has metadata type %s for which auto-assembly is disabled\n",
2c1b57
@@ -261,7 +255,8 @@ static int select_devices(struct mddev_dev *devlist,
2c1b57
 					       tst->ss->name, devname);
2c1b57
 				tmpdev->used = 2;
2c1b57
 			} else if (auto_assem && st == NULL &&
2c1b57
-				   !conf_test_metadata(tst->ss->name, (pol = devid_policy(stb.st_rdev)),
2c1b57
+				   !conf_test_metadata(tst->ss->name,
2c1b57
+						       (pol = devid_policy(rdev)),
2c1b57
 						       tst->ss->match_home(tst, c->homehost) == 1)) {
2c1b57
 				if (report_mismatch)
2c1b57
 					pr_err("%s has metadata type %s for which auto-assembly is disabled\n",
2c1b57
@@ -484,7 +479,7 @@ static int select_devices(struct mddev_dev *devlist,
2c1b57
 		/* Collect domain information from members only */
2c1b57
 		if (tmpdev && tmpdev->used == 1) {
2c1b57
 			if (!pol)
2c1b57
-				pol = devid_policy(stb.st_rdev);
2c1b57
+				pol = devid_policy(rdev);
2c1b57
 			domain_merge(&domains, pol, tst?tst->ss->name:NULL);
2c1b57
 		}
2c1b57
 		dev_policy_free(pol);
2c1b57
diff --git a/Build.c b/Build.c
2c1b57
index 665d906..2d84b96 100644
2c1b57
--- a/Build.c
2c1b57
+++ b/Build.c
2c1b57
@@ -42,6 +42,7 @@ int Build(char *mddev, struct mddev_dev *devlist,
2c1b57
 	 */
2c1b57
 	int i;
2c1b57
 	struct stat stb;
2c1b57
+	dev_t rdev;
2c1b57
 	int subdevs = 0, missing_disks = 0;
2c1b57
 	struct mddev_dev *dv;
2c1b57
 	int bitmap_fd;
2c1b57
@@ -126,8 +127,8 @@ int Build(char *mddev, struct mddev_dev *devlist,
2c1b57
 	array.nr_disks = s->raiddisks;
2c1b57
 	array.raid_disks = s->raiddisks;
2c1b57
 	array.md_minor = 0;
2c1b57
-	if (fstat(mdfd, &stb) == 0)
2c1b57
-		array.md_minor = minor(stb.st_rdev);
2c1b57
+	if (fstat_is_blkdev(mdfd, mddev, &rdev))
2c1b57
+		array.md_minor = minor(rdev);
2c1b57
 	array.not_persistent = 1;
2c1b57
 	array.state = 0; /* not clean, but no errors */
2c1b57
 	if (s->assume_clean)
2c1b57
diff --git a/Create.c b/Create.c
2c1b57
index df1bc20..239545f 100644
2c1b57
--- a/Create.c
2c1b57
+++ b/Create.c
2c1b57
@@ -89,8 +89,8 @@ int Create(struct supertype *st, char *mddev,
2c1b57
 	char *maxdisc = NULL;
2c1b57
 	int dnum, raid_disk_num;
2c1b57
 	struct mddev_dev *dv;
2c1b57
+	dev_t rdev;
2c1b57
 	int fail = 0, warn = 0;
2c1b57
-	struct stat stb;
2c1b57
 	int first_missing = subdevs * 2;
2c1b57
 	int second_missing = subdevs * 2;
2c1b57
 	int missing_disks = 0;
2c1b57
@@ -325,11 +325,8 @@ int Create(struct supertype *st, char *mddev,
2c1b57
 				dname, strerror(errno));
2c1b57
 			exit(2);
2c1b57
 		}
2c1b57
-		if (fstat(dfd, &stb) != 0 ||
2c1b57
-		    (stb.st_mode & S_IFMT) != S_IFBLK) {
2c1b57
+		if (!fstat_is_blkdev(dfd, dname, NULL)) {
2c1b57
 			close(dfd);
2c1b57
-			pr_err("%s is not a block device\n",
2c1b57
-				dname);
2c1b57
 			exit(2);
2c1b57
 		}
2c1b57
 		close(dfd);
2c1b57
@@ -641,8 +638,8 @@ int Create(struct supertype *st, char *mddev,
2c1b57
 	 * with, but it chooses to trust me instead. Sigh
2c1b57
 	 */
2c1b57
 	info.array.md_minor = 0;
2c1b57
-	if (fstat(mdfd, &stb) == 0)
2c1b57
-		info.array.md_minor = minor(stb.st_rdev);
2c1b57
+	if (fstat_is_blkdev(mdfd, mddev, &rdev))
2c1b57
+		info.array.md_minor = minor(rdev);
2c1b57
 	info.array.not_persistent = 0;
2c1b57
 
2c1b57
 	if (((s->level == 4 || s->level == 5) &&
2c1b57
@@ -841,7 +838,6 @@ int Create(struct supertype *st, char *mddev,
2c1b57
 		for (dnum = 0, raid_disk_num = 0, dv = devlist; dv;
2c1b57
 		     dv = (dv->next) ? (dv->next) : moved_disk, dnum++) {
2c1b57
 			int fd;
2c1b57
-			struct stat stb2;
2c1b57
 			struct mdinfo *inf = &infos[dnum];
2c1b57
 
2c1b57
 			if (dnum >= total_slots)
2c1b57
@@ -897,9 +893,10 @@ int Create(struct supertype *st, char *mddev,
2c1b57
 							dv->devname);
2c1b57
 						goto abort_locked;
2c1b57
 					}
2c1b57
-					fstat(fd, &stb2);
2c1b57
-					inf->disk.major = major(stb2.st_rdev);
2c1b57
-					inf->disk.minor = minor(stb2.st_rdev);
2c1b57
+					if (!fstat_is_blkdev(fd, dv->devname, &rdev))
2c1b57
+						return 1;
2c1b57
+					inf->disk.major = major(rdev);
2c1b57
+					inf->disk.minor = minor(rdev);
2c1b57
 				}
2c1b57
 				if (fd >= 0)
2c1b57
 					remove_partitions(fd);
2c1b57
@@ -920,8 +917,8 @@ int Create(struct supertype *st, char *mddev,
2c1b57
 
2c1b57
 				if (!have_container) {
2c1b57
 					/* getinfo_super might have lost these ... */
2c1b57
-					inf->disk.major = major(stb2.st_rdev);
2c1b57
-					inf->disk.minor = minor(stb2.st_rdev);
2c1b57
+					inf->disk.major = major(rdev);
2c1b57
+					inf->disk.minor = minor(rdev);
2c1b57
 				}
2c1b57
 				break;
2c1b57
 			case 2:
2c1b57
diff --git a/Grow.c b/Grow.c
2c1b57
index f4bd301..a527436 100644
2c1b57
--- a/Grow.c
2c1b57
+++ b/Grow.c
2c1b57
@@ -109,7 +109,7 @@ int Grow_Add_device(char *devname, int fd, char *newdev)
2c1b57
 	 */
2c1b57
 	struct mdinfo info;
2c1b57
 
2c1b57
-	struct stat stb;
2c1b57
+	dev_t rdev;
2c1b57
 	int nfd, fd2;
2c1b57
 	int d, nd;
2c1b57
 	struct supertype *st = NULL;
2c1b57
@@ -145,9 +145,7 @@ int Grow_Add_device(char *devname, int fd, char *newdev)
2c1b57
 		free(st);
2c1b57
 		return 1;
2c1b57
 	}
2c1b57
-	fstat(nfd, &stb;;
2c1b57
-	if ((stb.st_mode & S_IFMT) != S_IFBLK) {
2c1b57
-		pr_err("%s is not a block device!\n", newdev);
2c1b57
+	if (!fstat_is_blkdev(nfd, newdev, &rdev)) {
2c1b57
 		close(nfd);
2c1b57
 		free(st);
2c1b57
 		return 1;
2c1b57
@@ -198,8 +196,8 @@ int Grow_Add_device(char *devname, int fd, char *newdev)
2c1b57
 	 */
2c1b57
 
2c1b57
 	info.disk.number = d;
2c1b57
-	info.disk.major = major(stb.st_rdev);
2c1b57
-	info.disk.minor = minor(stb.st_rdev);
2c1b57
+	info.disk.major = major(rdev);
2c1b57
+	info.disk.minor = minor(rdev);
2c1b57
 	info.disk.raid_disk = d;
2c1b57
 	info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE);
2c1b57
 	st->ss->update_super(st, &info, "linear-grow-new", newdev,
2c1b57
diff --git a/Incremental.c b/Incremental.c
2c1b57
index 8909f2f..11a34e7 100644
2c1b57
--- a/Incremental.c
2c1b57
+++ b/Incremental.c
2c1b57
@@ -87,6 +87,7 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
2c1b57
 	 *   start the array (auto-readonly).
2c1b57
 	 */
2c1b57
 	struct stat stb;
2c1b57
+	dev_t rdev;
2c1b57
 	struct mdinfo info, dinfo;
2c1b57
 	struct mdinfo *sra = NULL, *d;
2c1b57
 	struct mddev_ident *match;
2c1b57
@@ -174,21 +175,11 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
2c1b57
 	/* 2/ Find metadata, reject if none appropriate (check
2c1b57
 	 *            version/name from args) */
2c1b57
 
2c1b57
-	if (fstat(dfd, &stb) < 0) {
2c1b57
-		if (c->verbose >= 0)
2c1b57
-			pr_err("fstat failed for %s: %s.\n",
2c1b57
-				devname, strerror(errno));
2c1b57
-		goto out;
2c1b57
-	}
2c1b57
-	if ((stb.st_mode & S_IFMT) != S_IFBLK) {
2c1b57
-		if (c->verbose >= 0)
2c1b57
-			pr_err("%s is not a block device.\n",
2c1b57
-				devname);
2c1b57
+	if (!fstat_is_blkdev(dfd, devname, &rdev))
2c1b57
 		goto out;
2c1b57
-	}
2c1b57
 
2c1b57
-	dinfo.disk.major = major(stb.st_rdev);
2c1b57
-	dinfo.disk.minor = minor(stb.st_rdev);
2c1b57
+	dinfo.disk.major = major(rdev);
2c1b57
+	dinfo.disk.minor = minor(rdev);
2c1b57
 
2c1b57
 	policy = disk_policy(&dinfo);
2c1b57
 	have_target = policy_check_path(&dinfo, &target_array);
2c1b57
@@ -339,8 +330,8 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
2c1b57
 		}
2c1b57
 
2c1b57
 		dinfo = info;
2c1b57
-		dinfo.disk.major = major(stb.st_rdev);
2c1b57
-		dinfo.disk.minor = minor(stb.st_rdev);
2c1b57
+		dinfo.disk.major = major(rdev);
2c1b57
+		dinfo.disk.minor = minor(rdev);
2c1b57
 		if (add_disk(mdfd, st, &info, &dinfo) != 0) {
2c1b57
 			pr_err("failed to add %s to new array %s: %s.\n",
2c1b57
 				devname, chosen_name, strerror(errno));
2c1b57
@@ -441,8 +432,8 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
2c1b57
 				goto out_unlock;
2c1b57
 			}
2c1b57
 		}
2c1b57
-		info.disk.major = major(stb.st_rdev);
2c1b57
-		info.disk.minor = minor(stb.st_rdev);
2c1b57
+		info.disk.major = major(rdev);
2c1b57
+		info.disk.minor = minor(rdev);
2c1b57
 		/* add disk needs to know about containers */
2c1b57
 		if (st->ss->external)
2c1b57
 			sra->array.level = LEVEL_CONTAINER;
2c1b57
@@ -863,12 +854,12 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
2c1b57
 	 * Return 0 on success, or some exit code on failure, probably 1.
2c1b57
 	 */
2c1b57
 	int rv = 1;
2c1b57
-	struct stat stb;
2c1b57
+	dev_t rdev;
2c1b57
 	struct map_ent *mp, *map = NULL;
2c1b57
 	struct mdinfo *chosen = NULL;
2c1b57
 	int dfd = *dfdp;
2c1b57
 
2c1b57
-	if (fstat(dfd, &stb) != 0)
2c1b57
+	if (!fstat_is_blkdev(dfd, devname, &rdev))
2c1b57
 		return 1;
2c1b57
 
2c1b57
 	/*
2c1b57
@@ -1038,8 +1029,8 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
2c1b57
 			devlist.writemostly = FlagDefault;
2c1b57
 			devlist.failfast = FlagDefault;
2c1b57
 			devlist.devname = chosen_devname;
2c1b57
-			sprintf(chosen_devname, "%d:%d", major(stb.st_rdev),
2c1b57
-				minor(stb.st_rdev));
2c1b57
+			sprintf(chosen_devname, "%d:%d", major(rdev),
2c1b57
+				minor(rdev));
2c1b57
 			devlist.disposition = 'a';
2c1b57
 			close(dfd);
2c1b57
 			*dfdp = -1;
2c1b57
diff --git a/Manage.c b/Manage.c
2c1b57
index 230309b..af55266 100644
2c1b57
--- a/Manage.c
2c1b57
+++ b/Manage.c
2c1b57
@@ -1513,7 +1513,7 @@ int Manage_subdevs(char *devname, int fd,
2c1b57
 			struct stat stb;
2c1b57
 			tfd = dev_open(dv->devname, O_RDONLY);
2c1b57
 			if (tfd >= 0) {
2c1b57
-				fstat(tfd, &stb;;
2c1b57
+				fstat_is_blkdev(tfd, dv->devname, &rdev);
2c1b57
 				close(tfd);
2c1b57
 			} else {
2c1b57
 				int open_err = errno;
2c1b57
diff --git a/bitmap.c b/bitmap.c
2c1b57
index 16a6b73..3653660 100644
2c1b57
--- a/bitmap.c
2c1b57
+++ b/bitmap.c
2c1b57
@@ -183,7 +183,6 @@ static int
2c1b57
 bitmap_file_open(char *filename, struct supertype **stp, int node_num)
2c1b57
 {
2c1b57
 	int fd;
2c1b57
-	struct stat stb;
2c1b57
 	struct supertype *st = *stp;
2c1b57
 
2c1b57
 	fd = open(filename, O_RDONLY|O_DIRECT);
2c1b57
@@ -193,14 +192,7 @@ bitmap_file_open(char *filename, struct supertype **stp, int node_num)
2c1b57
 		return -1;
2c1b57
 	}
2c1b57
 
2c1b57
-	if (fstat(fd, &stb) < 0) {
2c1b57
-		pr_err("failed to determine bitmap file/device type: %s\n",
2c1b57
-			strerror(errno));
2c1b57
-		close(fd);
2c1b57
-		return -1;
2c1b57
-	}
2c1b57
-
2c1b57
-	if ((stb.st_mode & S_IFMT) == S_IFBLK) {
2c1b57
+	if (fstat_is_blkdev(fd, filename, NULL)) {
2c1b57
 		/* block device, so we are probably after an internal bitmap */
2c1b57
 		if (!st)
2c1b57
 			st = guess_super(fd);
2c1b57
@@ -221,6 +213,9 @@ bitmap_file_open(char *filename, struct supertype **stp, int node_num)
2c1b57
 		}
2c1b57
 
2c1b57
 		*stp = st;
2c1b57
+	} else {
2c1b57
+		close(fd);
2c1b57
+		return -1;
2c1b57
 	}
2c1b57
 
2c1b57
 	return fd;
2c1b57
diff --git a/mdadm.h b/mdadm.h
2c1b57
index 07ee963..4adb840 100644
2c1b57
--- a/mdadm.h
2c1b57
+++ b/mdadm.h
2c1b57
@@ -1434,6 +1434,7 @@ extern int check_raid(int fd, char *name);
2c1b57
 extern int check_partitions(int fd, char *dname,
2c1b57
 			    unsigned long long freesize,
2c1b57
 			    unsigned long long size);
2c1b57
+extern int fstat_is_blkdev(int fd, char *devname, dev_t *rdev);
2c1b57
 
2c1b57
 extern int get_mdp_major(void);
2c1b57
 extern int get_maj_min(char *dev, int *major, int *minor);
2c1b57
diff --git a/super-intel.c b/super-intel.c
2c1b57
index 36f77d3..c4196ea 100644
2c1b57
--- a/super-intel.c
2c1b57
+++ b/super-intel.c
2c1b57
@@ -6562,7 +6562,7 @@ count_volumes_list(struct md_list *devlist, char *homehost,
2c1b57
 
2c1b57
 	for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
2c1b57
 		char *devname = tmpdev->devname;
2c1b57
-		struct stat stb;
2c1b57
+		dev_t rdev;
2c1b57
 		struct supertype *tst;
2c1b57
 		int dfd;
2c1b57
 		if (tmpdev->used > 1)
2c1b57
@@ -6578,14 +6578,7 @@ count_volumes_list(struct md_list *devlist, char *homehost,
2c1b57
 			dprintf("cannot open device %s: %s\n",
2c1b57
 				devname, strerror(errno));
2c1b57
 			tmpdev->used = 2;
2c1b57
-		} else if (fstat(dfd, &stb)< 0) {
2c1b57
-			/* Impossible! */
2c1b57
-			dprintf("fstat failed for %s: %s\n",
2c1b57
-				devname, strerror(errno));
2c1b57
-			tmpdev->used = 2;
2c1b57
-		} else if ((stb.st_mode & S_IFMT) != S_IFBLK) {
2c1b57
-			dprintf("%s is not a block device.\n",
2c1b57
-				devname);
2c1b57
+		} else if (!fstat_is_blkdev(dfd, devname, &rdev)) {
2c1b57
 			tmpdev->used = 2;
2c1b57
 		} else if (must_be_container(dfd)) {
2c1b57
 			struct supertype *cst;
2c1b57
@@ -6607,7 +6600,7 @@ count_volumes_list(struct md_list *devlist, char *homehost,
2c1b57
 			if (cst)
2c1b57
 				cst->ss->free_super(cst);
2c1b57
 		} else {
2c1b57
-			tmpdev->st_rdev = stb.st_rdev;
2c1b57
+			tmpdev->st_rdev = rdev;
2c1b57
 			if (tst->ss->load_super(tst,dfd, NULL)) {
2c1b57
 				dprintf("no RAID superblock on %s\n",
2c1b57
 					devname);
2c1b57
diff --git a/util.c b/util.c
2c1b57
index c7585ac..a92faf8 100644
2c1b57
--- a/util.c
2c1b57
+++ b/util.c
2c1b57
@@ -730,6 +730,23 @@ int check_raid(int fd, char *name)
2c1b57
 	return 1;
2c1b57
 }
2c1b57
 
2c1b57
+int fstat_is_blkdev(int fd, char *devname, dev_t *rdev)
2c1b57
+{
2c1b57
+	struct stat stb;
2c1b57
+
2c1b57
+	if (fstat(fd, &stb) != 0) {
2c1b57
+		pr_err("fstat failed for %s: %s\n", devname, strerror(errno));
2c1b57
+		return 0;
2c1b57
+	}
2c1b57
+	if ((S_IFMT & stb.st_mode) != S_IFBLK) {
2c1b57
+		pr_err("%s is not a block device.\n", devname);
2c1b57
+		return 0;
2c1b57
+	}
2c1b57
+	if (rdev)
2c1b57
+		*rdev = stb.st_rdev;
2c1b57
+	return 1;
2c1b57
+}
2c1b57
+
2c1b57
 int ask(char *mesg)
2c1b57
 {
2c1b57
 	char *add = "";
2c1b57
-- 
2c1b57
2.7.4
2c1b57