Blame SOURCES/mdadm-util-unify-stat-checking-blkdev-into-function.patch

2c1b57
From 9e04ac1c43e63eccb68eb196174069e5c23d0270 Mon Sep 17 00:00:00 2001
2c1b57
From: Zhilong Liu <zlliu@suse.com>
2c1b57
Date: Fri, 5 May 2017 11:09:41 +0800
2c1b57
Subject: [RHEL7.5 PATCH 110/169] mdadm/util: unify stat checking blkdev
2c1b57
 into function
2c1b57
2c1b57
declare function stat_is_blkdev() to integrate repeated stat
2c1b57
checking blkdev operations, it returns 'true/1' when it is a
2c1b57
block device, and returns 'false/0' when it isn't.
2c1b57
The devname is necessary parameter, *rdev is optional, parse
2c1b57
the pointer of dev_t *rdev, if valid, assigned device number
2c1b57
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    |  7 ++-----
2c1b57
 Build.c       | 25 ++++---------------------
2c1b57
 Incremental.c | 21 ++++-----------------
2c1b57
 Manage.c      | 11 +----------
2c1b57
 Monitor.c     | 16 ++++------------
2c1b57
 mdadm.h       |  1 +
2c1b57
 super-ddf.c   | 10 ++++------
2c1b57
 super-intel.c | 10 ++++------
2c1b57
 util.c        | 17 +++++++++++++++++
2c1b57
 9 files changed, 41 insertions(+), 77 deletions(-)
2c1b57
2c1b57
diff --git a/Assemble.c b/Assemble.c
2c1b57
index 9d0a89f..30d5838 100644
2c1b57
--- a/Assemble.c
2c1b57
+++ b/Assemble.c
2c1b57
@@ -512,15 +512,12 @@ static int select_devices(struct mddev_dev *devlist,
2c1b57
 
2c1b57
 	/* Now reject spares that don't match domains of identified members */
2c1b57
 	for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
2c1b57
-		struct stat stb;
2c1b57
 		if (tmpdev->used != 3)
2c1b57
 			continue;
2c1b57
-		if (stat(tmpdev->devname, &stb)< 0) {
2c1b57
-			pr_err("fstat failed for %s: %s\n",
2c1b57
-			       tmpdev->devname, strerror(errno));
2c1b57
+		if (!stat_is_blkdev(tmpdev->devname, &rdev)) {
2c1b57
 			tmpdev->used = 2;
2c1b57
 		} else {
2c1b57
-			struct dev_policy *pol = devid_policy(stb.st_rdev);
2c1b57
+			struct dev_policy *pol = devid_policy(rdev);
2c1b57
 			int dt = domain_test(domains, pol, NULL);
2c1b57
 			if (inargv && dt != 0)
2c1b57
 				/* take this spare as domains match
2c1b57
diff --git a/Build.c b/Build.c
2c1b57
index 2d84b96..ad59867 100644
2c1b57
--- a/Build.c
2c1b57
+++ b/Build.c
2c1b57
@@ -41,7 +41,6 @@ int Build(char *mddev, struct mddev_dev *devlist,
2c1b57
 	 * cc = chunk size factor: 0==4k, 1==8k etc.
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
@@ -65,16 +64,8 @@ int Build(char *mddev, struct mddev_dev *devlist,
2c1b57
 			missing_disks++;
2c1b57
 			continue;
2c1b57
 		}
2c1b57
-		if (stat(dv->devname, &stb)) {
2c1b57
-			pr_err("Cannot find %s: %s\n",
2c1b57
-				dv->devname, strerror(errno));
2c1b57
-			return 1;
2c1b57
-		}
2c1b57
-		if ((stb.st_mode & S_IFMT) != S_IFBLK) {
2c1b57
-			pr_err("%s is not a block device.\n",
2c1b57
-				dv->devname);
2c1b57
+		if (!stat_is_blkdev(dv->devname, NULL))
2c1b57
 			return 1;
2c1b57
-		}
2c1b57
 	}
2c1b57
 
2c1b57
 	if (s->raiddisks != subdevs) {
2c1b57
@@ -162,16 +153,8 @@ int Build(char *mddev, struct mddev_dev *devlist,
2c1b57
 
2c1b57
 		if (strcmp("missing", dv->devname) == 0)
2c1b57
 			continue;
2c1b57
-		if (stat(dv->devname, &stb)) {
2c1b57
-			pr_err("Weird: %s has disappeared.\n",
2c1b57
-				dv->devname);
2c1b57
+		if (!stat_is_blkdev(dv->devname, &rdev))
2c1b57
 			goto abort;
2c1b57
-		}
2c1b57
-		if ((stb.st_mode & S_IFMT)!= S_IFBLK) {
2c1b57
-			pr_err("Weird: %s is no longer a block device.\n",
2c1b57
-				dv->devname);
2c1b57
-			goto abort;
2c1b57
-		}
2c1b57
 		fd = open(dv->devname, O_RDONLY|O_EXCL);
2c1b57
 		if (fd < 0) {
2c1b57
 			pr_err("Cannot open %s: %s\n",
2c1b57
@@ -187,8 +170,8 @@ int Build(char *mddev, struct mddev_dev *devlist,
2c1b57
 		disk.state = (1<
2c1b57
 		if (dv->writemostly == FlagSet)
2c1b57
 			disk.state |= 1<
2c1b57
-		disk.major = major(stb.st_rdev);
2c1b57
-		disk.minor = minor(stb.st_rdev);
2c1b57
+		disk.major = major(rdev);
2c1b57
+		disk.minor = minor(rdev);
2c1b57
 		if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
2c1b57
 			pr_err("ADD_NEW_DISK failed for %s: %s\n",
2c1b57
 			       dv->devname, strerror(errno));
2c1b57
diff --git a/Incremental.c b/Incremental.c
2c1b57
index 11a34e7..97b2e99 100644
2c1b57
--- a/Incremental.c
2c1b57
+++ b/Incremental.c
2c1b57
@@ -86,8 +86,7 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
2c1b57
 	 * - if number of OK devices match expected, or -R and there are enough,
2c1b57
 	 *   start the array (auto-readonly).
2c1b57
 	 */
2c1b57
-	struct stat stb;
2c1b57
-	dev_t rdev;
2c1b57
+	dev_t rdev, rdev2;
2c1b57
 	struct mdinfo info, dinfo;
2c1b57
 	struct mdinfo *sra = NULL, *d;
2c1b57
 	struct mddev_ident *match;
2c1b57
@@ -108,18 +107,8 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
2c1b57
 
2c1b57
 	struct createinfo *ci = conf_get_create_info();
2c1b57
 
2c1b57
-	if (stat(devname, &stb) < 0) {
2c1b57
-		if (c->verbose >= 0)
2c1b57
-			pr_err("stat failed for %s: %s.\n",
2c1b57
-				devname, strerror(errno));
2c1b57
-		return rv;
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 (!stat_is_blkdev(devname, &rdev))
2c1b57
 		return rv;
2c1b57
-	}
2c1b57
 	dfd = dev_open(devname, O_RDONLY);
2c1b57
 	if (dfd < 0) {
2c1b57
 		if (c->verbose >= 0)
2c1b57
@@ -158,10 +147,8 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
2c1b57
 	if (!devlist) {
2c1b57
 		devlist = conf_get_devs();
2c1b57
 		for (;devlist; devlist = devlist->next) {
2c1b57
-			struct stat st2;
2c1b57
-			if (stat(devlist->devname, &st2) == 0 &&
2c1b57
-			    (st2.st_mode & S_IFMT) == S_IFBLK &&
2c1b57
-			    st2.st_rdev == stb.st_rdev)
2c1b57
+			if (stat_is_blkdev(devlist->devname, &rdev2) &&
2c1b57
+			    rdev2 == rdev)
2c1b57
 				break;
2c1b57
 		}
2c1b57
 	}
2c1b57
diff --git a/Manage.c b/Manage.c
2c1b57
index af55266..14276b7 100644
2c1b57
--- a/Manage.c
2c1b57
+++ b/Manage.c
2c1b57
@@ -1510,24 +1510,16 @@ int Manage_subdevs(char *devname, int fd,
2c1b57
 			 */
2c1b57
 			rdev = makedev(mj, mn);
2c1b57
 		} else {
2c1b57
-			struct stat stb;
2c1b57
 			tfd = dev_open(dv->devname, O_RDONLY);
2c1b57
 			if (tfd >= 0) {
2c1b57
 				fstat_is_blkdev(tfd, dv->devname, &rdev);
2c1b57
 				close(tfd);
2c1b57
 			} else {
2c1b57
 				int open_err = errno;
2c1b57
-				if (stat(dv->devname, &stb) != 0) {
2c1b57
-					pr_err("Cannot find %s: %s\n",
2c1b57
-					       dv->devname, strerror(errno));
2c1b57
-					goto abort;
2c1b57
-				}
2c1b57
-				if ((stb.st_mode & S_IFMT) != S_IFBLK) {
2c1b57
+				if (!stat_is_blkdev(dv->devname, &rdev)) {
2c1b57
 					if (dv->disposition == 'M')
2c1b57
 						/* non-fatal. Also improbable */
2c1b57
 						continue;
2c1b57
-					pr_err("%s is not a block device.\n",
2c1b57
-					       dv->devname);
2c1b57
 					goto abort;
2c1b57
 				}
2c1b57
 				if (dv->disposition == 'r')
2c1b57
@@ -1544,7 +1536,6 @@ int Manage_subdevs(char *devname, int fd,
2c1b57
 					goto abort;
2c1b57
 				}
2c1b57
 			}
2c1b57
-			rdev = stb.st_rdev;
2c1b57
 		}
2c1b57
 		switch(dv->disposition){
2c1b57
 		default:
2c1b57
diff --git a/Monitor.c b/Monitor.c
2c1b57
index 1f15377..e2b36ff 100644
2c1b57
--- a/Monitor.c
2c1b57
+++ b/Monitor.c
2c1b57
@@ -993,23 +993,13 @@ static void link_containers_with_subarrays(struct state *list)
2c1b57
 /* Not really Monitor but ... */
2c1b57
 int Wait(char *dev)
2c1b57
 {
2c1b57
-	struct stat stb;
2c1b57
 	char devnm[32];
2c1b57
-	char *tmp;
2c1b57
 	int rv = 1;
2c1b57
 	int frozen_remaining = 3;
2c1b57
 
2c1b57
-	if (stat(dev, &stb) != 0) {
2c1b57
-		pr_err("Cannot find %s: %s\n", dev,
2c1b57
-			strerror(errno));
2c1b57
+	if (!stat_is_blkdev(dev, NULL))
2c1b57
 		return 2;
2c1b57
-	}
2c1b57
-	tmp = stat2devnm(&stb;;
2c1b57
-	if (!tmp) {
2c1b57
-		pr_err("%s is not a block device.\n", dev);
2c1b57
-		return 2;
2c1b57
-	}
2c1b57
-	strcpy(devnm, tmp);
2c1b57
+	strcpy(devnm, dev);
2c1b57
 
2c1b57
 	while(1) {
2c1b57
 		struct mdstat_ent *ms = mdstat_read(1, 0);
2c1b57
@@ -1068,6 +1058,8 @@ int WaitClean(char *dev, int sock, int verbose)
2c1b57
 	int rv = 1;
2c1b57
 	char devnm[32];
2c1b57
 
2c1b57
+	if (!stat_is_blkdev(dev, NULL))
2c1b57
+		return 2;
2c1b57
 	fd = open(dev, O_RDONLY);
2c1b57
 	if (fd < 0) {
2c1b57
 		if (verbose)
2c1b57
diff --git a/mdadm.h b/mdadm.h
2c1b57
index 4adb840..a92feb2 100644
2c1b57
--- a/mdadm.h
2c1b57
+++ b/mdadm.h
2c1b57
@@ -1435,6 +1435,7 @@ 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
+extern int stat_is_blkdev(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-ddf.c b/super-ddf.c
2c1b57
index 796eaa5..9c82f4f 100644
2c1b57
--- a/super-ddf.c
2c1b57
+++ b/super-ddf.c
2c1b57
@@ -3490,7 +3490,7 @@ static int validate_geometry_ddf_bvd(struct supertype *st,
2c1b57
 				     char *dev, unsigned long long *freesize,
2c1b57
 				     int verbose)
2c1b57
 {
2c1b57
-	struct stat stb;
2c1b57
+	dev_t rdev;
2c1b57
 	struct ddf_super *ddf = st->sb;
2c1b57
 	struct dl *dl;
2c1b57
 	unsigned long long maxsize;
2c1b57
@@ -3526,13 +3526,11 @@ static int validate_geometry_ddf_bvd(struct supertype *st,
2c1b57
 		return 1;
2c1b57
 	}
2c1b57
 	/* This device must be a member of the set */
2c1b57
-	if (stat(dev, &stb) < 0)
2c1b57
-		return 0;
2c1b57
-	if ((S_IFMT & stb.st_mode) != S_IFBLK)
2c1b57
+	if (!stat_is_blkdev(dev, NULL))
2c1b57
 		return 0;
2c1b57
 	for (dl = ddf->dlist ; dl ; dl = dl->next) {
2c1b57
-		if (dl->major == (int)major(stb.st_rdev) &&
2c1b57
-		    dl->minor == (int)minor(stb.st_rdev))
2c1b57
+		if (dl->major == (int)major(rdev) &&
2c1b57
+		    dl->minor == (int)minor(rdev))
2c1b57
 			break;
2c1b57
 	}
2c1b57
 	if (!dl) {
2c1b57
diff --git a/super-intel.c b/super-intel.c
2c1b57
index c4196ea..e13c940 100644
2c1b57
--- a/super-intel.c
2c1b57
+++ b/super-intel.c
2c1b57
@@ -6855,7 +6855,7 @@ static int validate_geometry_imsm_volume(struct supertype *st, int level,
2c1b57
 					 unsigned long long *freesize,
2c1b57
 					 int verbose)
2c1b57
 {
2c1b57
-	struct stat stb;
2c1b57
+	dev_t rdev;
2c1b57
 	struct intel_super *super = st->sb;
2c1b57
 	struct imsm_super *mpb;
2c1b57
 	struct dl *dl;
2c1b57
@@ -6920,13 +6920,11 @@ static int validate_geometry_imsm_volume(struct supertype *st, int level,
2c1b57
 	}
2c1b57
 
2c1b57
 	/* This device must be a member of the set */
2c1b57
-	if (stat(dev, &stb) < 0)
2c1b57
-		return 0;
2c1b57
-	if ((S_IFMT & stb.st_mode) != S_IFBLK)
2c1b57
+	if (!stat_is_blkdev(dev, &rdev))
2c1b57
 		return 0;
2c1b57
 	for (dl = super->disks ; dl ; dl = dl->next) {
2c1b57
-		if (dl->major == (int)major(stb.st_rdev) &&
2c1b57
-		    dl->minor == (int)minor(stb.st_rdev))
2c1b57
+		if (dl->major == (int)major(rdev) &&
2c1b57
+		    dl->minor == (int)minor(rdev))
2c1b57
 			break;
2c1b57
 	}
2c1b57
 	if (!dl) {
2c1b57
diff --git a/util.c b/util.c
2c1b57
index a92faf8..11ff2cc 100644
2c1b57
--- a/util.c
2c1b57
+++ b/util.c
2c1b57
@@ -747,6 +747,23 @@ int fstat_is_blkdev(int fd, char *devname, dev_t *rdev)
2c1b57
 	return 1;
2c1b57
 }
2c1b57
 
2c1b57
+int stat_is_blkdev(char *devname, dev_t *rdev)
2c1b57
+{
2c1b57
+	struct stat stb;
2c1b57
+
2c1b57
+	if (stat(devname, &stb) != 0) {
2c1b57
+		pr_err("stat 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