Blame SOURCES/util-Introduce2-md_get_array_info.patch

2c1b57
From 9cd39f015558dba82c293a4433b481b921ceec87 Mon Sep 17 00:00:00 2001
2c1b57
From: Jes Sorensen <Jes.Sorensen@gmail.com>
2c1b57
Date: Wed, 29 Mar 2017 14:35:41 -0400
2c1b57
Subject: [RHEL7.5 PATCH 037/169] util: Introduce md_get_array_info()
2c1b57
2c1b57
Remove most direct ioctl calls for GET_ARRAY_INFO, except for one,
2c1b57
which will be addressed in the next patch.
2c1b57
2c1b57
This is the start of the effort to clean up the use of ioctl calls and
2c1b57
introduce a more structured API, which will use sysfs and fall back to
2c1b57
ioctl for backup.
2c1b57
2c1b57
Signed-off-by: Jes Sorensen <Jes.Sorensen@gmail.com>
2c1b57
---
2c1b57
 Create.c      |  5 ++---
2c1b57
 Detail.c      |  2 +-
2c1b57
 Grow.c        | 31 ++++++++++++++++---------------
2c1b57
 Incremental.c | 11 +++++------
2c1b57
 Manage.c      | 13 ++++++-------
2c1b57
 Monitor.c     |  7 ++++---
2c1b57
 Query.c       |  7 ++++---
2c1b57
 mdadm.h       |  1 +
2c1b57
 mdassemble.c  |  2 +-
2c1b57
 util.c        | 14 +++++++++++---
2c1b57
 10 files changed, 51 insertions(+), 42 deletions(-)
2c1b57
2c1b57
diff --git a/Create.c b/Create.c
2c1b57
index 10e7d10..0e0778f 100644
2c1b57
--- a/Create.c
2c1b57
+++ b/Create.c
2c1b57
@@ -156,8 +156,7 @@ int Create(struct supertype *st, char *mddev,
2c1b57
 		memset(&inf, 0, sizeof(inf));
2c1b57
 		fd = open(devlist->devname, O_RDONLY);
2c1b57
 		if (fd >= 0 &&
2c1b57
-		    ioctl(fd, GET_ARRAY_INFO, &inf) == 0 &&
2c1b57
-		    inf.raid_disks == 0) {
2c1b57
+		    md_get_array_info(fd, &inf) == 0 && inf.raid_disks == 0) {
2c1b57
 			/* yep, looks like a container */
2c1b57
 			if (st) {
2c1b57
 				rv = st->ss->load_container(st, fd,
2c1b57
@@ -634,7 +633,7 @@ int Create(struct supertype *st, char *mddev,
2c1b57
 	} else {
2c1b57
 		mdu_array_info_t inf;
2c1b57
 		memset(&inf, 0, sizeof(inf));
2c1b57
-		ioctl(mdfd, GET_ARRAY_INFO, &inf);
2c1b57
+		md_get_array_info(mdfd, &inf);
2c1b57
 		if (inf.working_disks != 0) {
2c1b57
 			pr_err("another array by this name is already running.\n");
2c1b57
 			goto abort_locked;
2c1b57
diff --git a/Detail.c b/Detail.c
2c1b57
index 136875b..d7e886a 100644
2c1b57
--- a/Detail.c
2c1b57
+++ b/Detail.c
2c1b57
@@ -107,7 +107,7 @@ int Detail(char *dev, struct context *c)
2c1b57
 	external = (sra != NULL && sra->array.major_version == -1
2c1b57
 		    && sra->array.minor_version == -2);
2c1b57
 	st = super_by_fd(fd, &subarray);
2c1b57
-	if (ioctl(fd, GET_ARRAY_INFO, &array) == 0) {
2c1b57
+	if (md_get_array_info(fd, &array) == 0) {
2c1b57
 		inactive = 0;
2c1b57
 	} else if (errno == ENODEV && sra) {
2c1b57
 		if (sra->array.major_version == -1 &&
2c1b57
diff --git a/Grow.c b/Grow.c
2c1b57
index 6405f0e..4eab5cc 100755
2c1b57
--- a/Grow.c
2c1b57
+++ b/Grow.c
2c1b57
@@ -115,7 +115,7 @@ int Grow_Add_device(char *devname, int fd, char *newdev)
2c1b57
 	struct supertype *st = NULL;
2c1b57
 	char *subarray = NULL;
2c1b57
 
2c1b57
-	if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
2c1b57
+	if (md_get_array_info(fd, &info.array) < 0) {
2c1b57
 		pr_err("cannot get array info for %s\n", devname);
2c1b57
 		return 1;
2c1b57
 	}
2c1b57
@@ -221,7 +221,7 @@ int Grow_Add_device(char *devname, int fd, char *newdev)
2c1b57
 	 * Now go through and update all superblocks
2c1b57
 	 */
2c1b57
 
2c1b57
-	if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
2c1b57
+	if (md_get_array_info(fd, &info.array) < 0) {
2c1b57
 		pr_err("cannot get array info for %s\n", devname);
2c1b57
 		return 1;
2c1b57
 	}
2c1b57
@@ -328,7 +328,7 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
2c1b57
 			devname, bmf.pathname);
2c1b57
 		return 1;
2c1b57
 	}
2c1b57
-	if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
2c1b57
+	if (md_get_array_info(fd, &array) != 0) {
2c1b57
 		pr_err("cannot get array status for %s\n", devname);
2c1b57
 		return 1;
2c1b57
 	}
2c1b57
@@ -1784,7 +1784,7 @@ int Grow_reshape(char *devname, int fd,
2c1b57
 	struct mdinfo info;
2c1b57
 	struct mdinfo *sra;
2c1b57
 
2c1b57
-	if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
2c1b57
+	if (md_get_array_info(fd, &array) < 0) {
2c1b57
 		pr_err("%s is not an active md array - aborting\n",
2c1b57
 			devname);
2c1b57
 		return 1;
2c1b57
@@ -2030,7 +2030,7 @@ int Grow_reshape(char *devname, int fd,
2c1b57
 					/* get array parameters after takeover
2c1b57
 					 * to change one parameter at time only
2c1b57
 					 */
2c1b57
-					rv = ioctl(fd, GET_ARRAY_INFO, &array);
2c1b57
+					rv = md_get_array_info(fd, &array);
2c1b57
 				}
2c1b57
 			}
2c1b57
 			/* make sure mdmon is
2c1b57
@@ -2072,7 +2072,7 @@ int Grow_reshape(char *devname, int fd,
2c1b57
 			/* go back to raid0, drop parity disk
2c1b57
 			 */
2c1b57
 			sysfs_set_str(sra, NULL, "level", "raid0");
2c1b57
-			ioctl(fd, GET_ARRAY_INFO, &array);
2c1b57
+			md_get_array_info(fd, &array);
2c1b57
 		}
2c1b57
 
2c1b57
 size_change_error:
2c1b57
@@ -2101,7 +2101,7 @@ size_change_error:
2c1b57
 			    sysfs_set_str(sra, NULL, "resync_start", "none") < 0)
2c1b57
 				pr_err("--assume-clean not supported with --grow on this kernel\n");
2c1b57
 		}
2c1b57
-		ioctl(fd, GET_ARRAY_INFO, &array);
2c1b57
+		md_get_array_info(fd, &array);
2c1b57
 		s->size = get_component_size(fd)/2;
2c1b57
 		if (s->size == 0)
2c1b57
 			s->size = array.size;
2c1b57
@@ -2267,7 +2267,7 @@ size_change_error:
2c1b57
 			rv =1 ;
2c1b57
 		}
2c1b57
 		if (s->layout_str) {
2c1b57
-			if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
2c1b57
+			if (md_get_array_info(fd, &array) != 0) {
2c1b57
 				dprintf("Cannot get array information.\n");
2c1b57
 				goto release;
2c1b57
 			}
2c1b57
@@ -2830,7 +2830,7 @@ static int impose_reshape(struct mdinfo *sra,
2c1b57
 						 * reshape->after.data_disks);
2c1b57
 	}
2c1b57
 
2c1b57
-	ioctl(fd, GET_ARRAY_INFO, &array);
2c1b57
+	md_get_array_info(fd, &array);
2c1b57
 	if (info->array.chunk_size == info->new_chunk &&
2c1b57
 	    reshape->before.layout == reshape->after.layout &&
2c1b57
 	    st->ss->external == 0) {
2c1b57
@@ -2885,7 +2885,7 @@ static int impose_level(int fd, int level, char *devname, int verbose)
2c1b57
 	struct mdinfo info;
2c1b57
 	sysfs_init(&info, fd, NULL);
2c1b57
 
2c1b57
-	ioctl(fd, GET_ARRAY_INFO, &array);
2c1b57
+	md_get_array_info(fd, &array);
2c1b57
 	if (level == 0 &&
2c1b57
 	    (array.level >= 4 && array.level <= 6)) {
2c1b57
 		/* To convert to RAID0 we need to fail and
2c1b57
@@ -2921,7 +2921,7 @@ static int impose_level(int fd, int level, char *devname, int verbose)
2c1b57
 			      makedev(disk.major, disk.minor));
2c1b57
 		}
2c1b57
 		/* Now fail anything left */
2c1b57
-		ioctl(fd, GET_ARRAY_INFO, &array);
2c1b57
+		md_get_array_info(fd, &array);
2c1b57
 		for (d = 0, found = 0;
2c1b57
 		     d < MAX_DISKS && found < array.nr_disks;
2c1b57
 		     d++) {
2c1b57
@@ -3042,7 +3042,7 @@ static int reshape_array(char *container, int fd, char *devname,
2c1b57
 	/* when reshaping a RAID0, the component_size might be zero.
2c1b57
 	 * So try to fix that up.
2c1b57
 	 */
2c1b57
-	if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
2c1b57
+	if (md_get_array_info(fd, &array) != 0) {
2c1b57
 		dprintf("Cannot get array information.\n");
2c1b57
 		goto release;
2c1b57
 	}
2c1b57
@@ -3230,7 +3230,7 @@ static int reshape_array(char *container, int fd, char *devname,
2c1b57
 		 * some more changes: layout, raid_disks, chunk_size
2c1b57
 		 */
2c1b57
 		/* read current array info */
2c1b57
-		if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
2c1b57
+		if (md_get_array_info(fd, &array) != 0) {
2c1b57
 			dprintf("Cannot get array information.\n");
2c1b57
 			goto release;
2c1b57
 		}
2c1b57
@@ -4994,8 +4994,9 @@ int Grow_continue_command(char *devname, int fd,
2c1b57
 		int d;
2c1b57
 		int cnt = 5;
2c1b57
 		dprintf_cont("native array (%s)\n", devname);
2c1b57
-		if (ioctl(fd, GET_ARRAY_INFO, &array.array) < 0) {
2c1b57
-			pr_err("%s is not an active md array - aborting\n", devname);
2c1b57
+		if (md_get_array_info(fd, &array.array) < 0) {
2c1b57
+			pr_err("%s is not an active md array - aborting\n",
2c1b57
+			       devname);
2c1b57
 			ret_val = 1;
2c1b57
 			goto Grow_continue_command_exit;
2c1b57
 		}
2c1b57
diff --git a/Incremental.c b/Incremental.c
2c1b57
index 81afc7e..1f12c77 100644
2c1b57
--- a/Incremental.c
2c1b57
+++ b/Incremental.c
2c1b57
@@ -398,7 +398,7 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
2c1b57
 		    && ! policy_action_allows(policy, st->ss->name,
2c1b57
 					      act_re_add)
2c1b57
 		    && c->runstop < 1) {
2c1b57
-			if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
2c1b57
+			if (md_get_array_info(mdfd, &ainf) == 0) {
2c1b57
 				pr_err("not adding %s to active array (without --run) %s\n",
2c1b57
 				       devname, chosen_name);
2c1b57
 				rv = 2;
2c1b57
@@ -549,7 +549,7 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
2c1b57
 	/*   + add any bitmap file  */
2c1b57
 	/*   + start the array (auto-readonly). */
2c1b57
 
2c1b57
-	if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
2c1b57
+	if (md_get_array_info(mdfd, &ainf) == 0) {
2c1b57
 		if (c->export) {
2c1b57
 			printf("MD_STARTED=already\n");
2c1b57
 		} else if (c->verbose >= 0)
2c1b57
@@ -664,7 +664,7 @@ static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
2c1b57
 	struct mdinfo *d;
2c1b57
 	mdu_array_info_t ra;
2c1b57
 
2c1b57
-	if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0)
2c1b57
+	if (md_get_array_info(mdfd, &ra) == 0)
2c1b57
 		return; /* not safe to remove from active arrays
2c1b57
 			 * without thinking more */
2c1b57
 
2c1b57
@@ -837,7 +837,7 @@ static int container_members_max_degradation(struct map_ent *map, struct map_ent
2c1b57
 		if (afd < 0)
2c1b57
 			continue;
2c1b57
 		/* most accurate information regarding array degradation */
2c1b57
-		if (ioctl(afd, GET_ARRAY_INFO, &array) >= 0) {
2c1b57
+		if (md_get_array_info(afd, &array) >= 0) {
2c1b57
 			int degraded = array.raid_disks - array.active_disks -
2c1b57
 				       array.spare_disks;
2c1b57
 			if (degraded > max_degraded)
2c1b57
@@ -1390,8 +1390,7 @@ restart:
2c1b57
 				rv = 1;
2c1b57
 			continue;
2c1b57
 		}
2c1b57
-		if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 ||
2c1b57
-		    errno != ENODEV) {
2c1b57
+		if (md_get_array_info(mdfd, &array) == 0 || errno != ENODEV) {
2c1b57
 			close(mdfd);
2c1b57
 			continue;
2c1b57
 		}
2c1b57
diff --git a/Manage.c b/Manage.c
2c1b57
index 55218d9..24ed370 100644
2c1b57
--- a/Manage.c
2c1b57
+++ b/Manage.c
2c1b57
@@ -95,7 +95,7 @@ int Manage_ro(char *devname, int fd, int readonly)
2c1b57
 		goto out;
2c1b57
 	}
2c1b57
 #endif
2c1b57
-	if (ioctl(fd, GET_ARRAY_INFO, &array)) {
2c1b57
+	if (md_get_array_info(fd, &array)) {
2c1b57
 		pr_err("%s does not appear to be active.\n",
2c1b57
 			devname);
2c1b57
 		rv = 1;
2c1b57
@@ -539,7 +539,7 @@ static void add_faulty(struct mddev_dev *dv, int fd, char disp)
2c1b57
 	int remaining_disks;
2c1b57
 	int i;
2c1b57
 
2c1b57
-	if (ioctl(fd, GET_ARRAY_INFO, &array) != 0)
2c1b57
+	if (md_get_array_info(fd, &array) != 0)
2c1b57
 		return;
2c1b57
 
2c1b57
 	remaining_disks = array.nr_disks;
2c1b57
@@ -565,7 +565,7 @@ static void add_detached(struct mddev_dev *dv, int fd, char disp)
2c1b57
 	int remaining_disks;
2c1b57
 	int i;
2c1b57
 
2c1b57
-	if (ioctl(fd, GET_ARRAY_INFO, &array) != 0)
2c1b57
+	if (md_get_array_info(fd, &array) != 0)
2c1b57
 		return;
2c1b57
 
2c1b57
 	remaining_disks = array.nr_disks;
2c1b57
@@ -602,7 +602,7 @@ static void add_set(struct mddev_dev *dv, int fd, char set_char)
2c1b57
 	int copies, set;
2c1b57
 	int i;
2c1b57
 
2c1b57
-	if (ioctl(fd, GET_ARRAY_INFO, &array) != 0)
2c1b57
+	if (md_get_array_info(fd, &array) != 0)
2c1b57
 		return;
2c1b57
 	if (array.level != 10)
2c1b57
 		return;
2c1b57
@@ -1383,9 +1383,8 @@ int Manage_subdevs(char *devname, int fd,
2c1b57
 	int busy = 0;
2c1b57
 	int raid_slot = -1;
2c1b57
 
2c1b57
-	if (ioctl(fd, GET_ARRAY_INFO, &array)) {
2c1b57
-		pr_err("Cannot get array info for %s\n",
2c1b57
-			devname);
2c1b57
+	if (md_get_array_info(fd, &array)) {
2c1b57
+		pr_err("Cannot get array info for %s\n", devname);
2c1b57
 		goto abort;
2c1b57
 	}
2c1b57
 	sysfs_init(&info, fd, NULL);
2c1b57
diff --git a/Monitor.c b/Monitor.c
2c1b57
index bdd3e63..0a0a1e2 100644
2c1b57
--- a/Monitor.c
2c1b57
+++ b/Monitor.c
2c1b57
@@ -497,7 +497,7 @@ static int check_array(struct state *st, struct mdstat_ent *mdstat,
2c1b57
 		return 0;
2c1b57
 	}
2c1b57
 	fcntl(fd, F_SETFD, FD_CLOEXEC);
2c1b57
-	if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
2c1b57
+	if (md_get_array_info(fd, &array) < 0) {
2c1b57
 		if (!st->err)
2c1b57
 			alert("DeviceDisappeared", dev, NULL, ainfo);
2c1b57
 		st->err++;
2c1b57
@@ -709,9 +709,10 @@ static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist,
2c1b57
 
2c1b57
 			st->devname = xstrdup(name);
2c1b57
 			if ((fd = open(st->devname, O_RDONLY)) < 0 ||
2c1b57
-			    ioctl(fd, GET_ARRAY_INFO, &array)< 0) {
2c1b57
+			    md_get_array_info(fd, &array) < 0) {
2c1b57
 				/* no such array */
2c1b57
-				if (fd >=0) close(fd);
2c1b57
+				if (fd >= 0)
2c1b57
+					close(fd);
2c1b57
 				put_md_name(st->devname);
2c1b57
 				free(st->devname);
2c1b57
 				if (st->metadata) {
2c1b57
diff --git a/Query.c b/Query.c
2c1b57
index fbc1d10..cae75d1 100644
2c1b57
--- a/Query.c
2c1b57
+++ b/Query.c
2c1b57
@@ -53,9 +53,10 @@ int Query(char *dev)
2c1b57
 	}
2c1b57
 
2c1b57
 	vers = md_get_version(fd);
2c1b57
-	if (ioctl(fd, GET_ARRAY_INFO, &array)<0)
2c1b57
+	if (md_get_array_info(fd, &array) < 0)
2c1b57
 		ioctlerr = errno;
2c1b57
-	else ioctlerr = 0;
2c1b57
+	else
2c1b57
+		ioctlerr = 0;
2c1b57
 
2c1b57
 	fstat(fd, &stb;;
2c1b57
 
2c1b57
@@ -100,7 +101,7 @@ int Query(char *dev)
2c1b57
 			activity = "undetected";
2c1b57
 			if (mddev && (fd = open(mddev, O_RDONLY))>=0) {
2c1b57
 				if (md_get_version(fd) >= 9000 &&
2c1b57
-				    ioctl(fd, GET_ARRAY_INFO, &array)>= 0) {
2c1b57
+				    md_get_array_info(fd, &array) >= 0) {
2c1b57
 					if (ioctl(fd, GET_DISK_INFO, &disc) >= 0 &&
2c1b57
 					    makedev((unsigned)disc.major,(unsigned)disc.minor) == stb.st_rdev)
2c1b57
 						activity = "active";
2c1b57
diff --git a/mdadm.h b/mdadm.h
2c1b57
index dbf1f92..7770585 100644
2c1b57
--- a/mdadm.h
2c1b57
+++ b/mdadm.h
2c1b57
@@ -1405,6 +1405,7 @@ extern int Restore_metadata(char *dev, char *dir, struct context *c,
2c1b57
 			    struct supertype *st, int only);
2c1b57
 
2c1b57
 extern int md_get_version(int fd);
2c1b57
+int md_get_array_info(int fd, struct mdu_array_info_s *array);
2c1b57
 extern int get_linux_version(void);
2c1b57
 extern int mdadm_version(char *version);
2c1b57
 extern unsigned long long parse_size(char *size);
2c1b57
diff --git a/mdassemble.c b/mdassemble.c
2c1b57
index 471ffeb..a24b324 100644
2c1b57
--- a/mdassemble.c
2c1b57
+++ b/mdassemble.c
2c1b57
@@ -67,7 +67,7 @@ int main(int argc, char *argv[])
2c1b57
 			if (strcasecmp(array_list->devname, "<ignore>") == 0)
2c1b57
 				continue;
2c1b57
 			mdfd = open_mddev(array_list->devname, 0);
2c1b57
-			if (mdfd >= 0 && ioctl(mdfd, GET_ARRAY_INFO, &array) == 0) {
2c1b57
+			if (mdfd >= 0 && md_get_array_info(mdfd, &array) == 0) {
2c1b57
 				rv |= Manage_ro(array_list->devname, mdfd, -1); /* make it readwrite */
2c1b57
 				continue;
2c1b57
 			}
2c1b57
diff --git a/util.c b/util.c
2c1b57
index 374015e..725877d 100644
2c1b57
--- a/util.c
2c1b57
+++ b/util.c
2c1b57
@@ -212,6 +212,15 @@ int cluster_release_dlmlock(int lockid)
2c1b57
 #endif
2c1b57
 
2c1b57
 /*
2c1b57
+ * Get array info from the kernel. Longer term we want to deprecate the
2c1b57
+ * ioctl and get it from sysfs.
2c1b57
+ */
2c1b57
+int md_get_array_info(int fd, struct mdu_array_info_s *array)
2c1b57
+{
2c1b57
+	return ioctl(fd, GET_ARRAY_INFO, array);
2c1b57
+}
2c1b57
+
2c1b57
+/*
2c1b57
  * Parse a 128 bit uuid in 4 integers
2c1b57
  * format is 32 hexx nibbles with options :.<space> separator
2c1b57
  * If not exactly 32 hex digits are found, return 0
2c1b57
@@ -539,8 +548,7 @@ int enough_fd(int fd)
2c1b57
 	int i, rv;
2c1b57
 	char *avail;
2c1b57
 
2c1b57
-	if (ioctl(fd, GET_ARRAY_INFO, &array) != 0 ||
2c1b57
-	    array.raid_disks <= 0)
2c1b57
+	if (md_get_array_info(fd, &array) != 0 || array.raid_disks <= 0)
2c1b57
 		return 0;
2c1b57
 	avail = xcalloc(array.raid_disks, 1);
2c1b57
 	for (i = 0; i < MAX_DISKS && array.nr_disks > 0; i++) {
2c1b57
@@ -1175,7 +1183,7 @@ struct supertype *super_by_fd(int fd, char **subarrayp)
2c1b57
 		minor = sra->array.minor_version;
2c1b57
 		verstr = sra->text_version;
2c1b57
 	} else {
2c1b57
-		if (ioctl(fd, GET_ARRAY_INFO, &array))
2c1b57
+		if (md_get_array_info(fd, &array))
2c1b57
 			array.major_version = array.minor_version = 0;
2c1b57
 		vers = array.major_version;
2c1b57
 		minor = array.minor_version;
2c1b57
-- 
2c1b57
2.7.4
2c1b57