dcavalca / rpms / mdadm

Forked from rpms/mdadm 3 years ago
Clone

Blame SOURCES/sysfs-Make-sysfs_init-return-an-error-code.patch

b7f731
From dae131379f9fd82e2867aed25a3ff719f957e9a3 Mon Sep 17 00:00:00 2001
b7f731
From: Jes Sorensen <Jes.Sorensen@gmail.com>
b7f731
Date: Thu, 30 Mar 2017 16:52:37 -0400
b7f731
Subject: [RHEL7.5 PATCH 048/169] sysfs: Make sysfs_init() return an error
b7f731
 code
b7f731
b7f731
Rather than have the caller inspect the returned content, return an
b7f731
error code from sysfs_init(). In addition make all callers actually
b7f731
check it.
b7f731
b7f731
Signed-off-by: Jes Sorensen <Jes.Sorensen@gmail.com>
b7f731
---
b7f731
 Assemble.c    | 12 ++++++++++--
b7f731
 Create.c      | 10 ++++++++--
b7f731
 Grow.c        | 39 +++++++++++++++++++++++++++++++++------
b7f731
 Incremental.c | 12 ++++++++++--
b7f731
 Manage.c      |  7 +++++--
b7f731
 Monitor.c     |  4 +++-
b7f731
 mdadm.c       | 11 ++++++++---
b7f731
 mdadm.h       |  2 +-
b7f731
 sysfs.c       | 16 ++++++++++------
b7f731
 9 files changed, 88 insertions(+), 25 deletions(-)
b7f731
b7f731
diff --git a/Assemble.c b/Assemble.c
b7f731
index 6a6a56b..672cd12 100644
b7f731
--- a/Assemble.c
b7f731
+++ b/Assemble.c
b7f731
@@ -1670,7 +1670,12 @@ try_again:
b7f731
 	}
b7f731
 	st->ss->getinfo_super(st, content, NULL);
b7f731
 #ifndef MDASSEMBLE
b7f731
-	sysfs_init(content, mdfd, NULL);
b7f731
+	if (sysfs_init(content, mdfd, NULL)) {
b7f731
+		pr_err("Unable to initialize sysfs\n");
b7f731
+		close(mdfd);
b7f731
+		free(devices);
b7f731
+		return 1;
b7f731
+	}
b7f731
 #endif
b7f731
 	/* after reload context, store journal_clean in context */
b7f731
 	content->journal_clean = journal_clean;
b7f731
@@ -1885,7 +1890,10 @@ int assemble_container_content(struct supertype *st, int mdfd,
b7f731
 	char *avail;
b7f731
 	int err;
b7f731
 
b7f731
-	sysfs_init(content, mdfd, NULL);
b7f731
+	if (sysfs_init(content, mdfd, NULL)) {
b7f731
+		pr_err("Unable to initialize sysfs\n");
b7f731
+		return 1;
b7f731
+	}
b7f731
 
b7f731
 	sra = sysfs_read(mdfd, NULL, GET_VERSION|GET_DEVS);
b7f731
 	if (sra == NULL || strcmp(sra->text_version, content->text_version) != 0) {
b7f731
diff --git a/Create.c b/Create.c
b7f731
index 0e0778f..32987af 100644
b7f731
--- a/Create.c
b7f731
+++ b/Create.c
b7f731
@@ -737,7 +737,10 @@ int Create(struct supertype *st, char *mddev,
b7f731
 
b7f731
 	total_slots = info.array.nr_disks;
b7f731
 	st->ss->getinfo_super(st, &info, NULL);
b7f731
-	sysfs_init(&info, mdfd, NULL);
b7f731
+	if (sysfs_init(&info, mdfd, NULL)) {
b7f731
+		pr_err("unable to initialize sysfs\n");
b7f731
+		goto abort_locked;
b7f731
+	}
b7f731
 
b7f731
 	if (did_default && c->verbose >= 0) {
b7f731
 		if (is_subarray(info.text_version)) {
b7f731
@@ -794,7 +797,10 @@ int Create(struct supertype *st, char *mddev,
b7f731
 		s->bitmap_file = NULL;
b7f731
 	}
b7f731
 
b7f731
-	sysfs_init(&info, mdfd, NULL);
b7f731
+	if (sysfs_init(&info, mdfd, NULL)) {
b7f731
+		pr_err("unable to initialize sysfs\n");
b7f731
+		goto abort_locked;
b7f731
+	}
b7f731
 
b7f731
 	if (st->ss->external && st->container_devnm[0]) {
b7f731
 		/* member */
b7f731
diff --git a/Grow.c b/Grow.c
b7f731
index 0c16d5b..78a3474 100755
b7f731
--- a/Grow.c
b7f731
+++ b/Grow.c
b7f731
@@ -455,7 +455,10 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
b7f731
 		}
b7f731
 		if (offset_setable) {
b7f731
 			st->ss->getinfo_super(st, mdi, NULL);
b7f731
-			sysfs_init(mdi, fd, NULL);
b7f731
+			if (sysfs_init(mdi, fd, NULL)) {
b7f731
+				pr_err("failed to intialize sysfs.\n");
b7f731
+				free(mdi);
b7f731
+			}
b7f731
 			rv = sysfs_set_num_signed(mdi, NULL, "bitmap/location",
b7f731
 						  mdi->bitmap_offset);
b7f731
 			free(mdi);
b7f731
@@ -2149,7 +2152,11 @@ size_change_error:
b7f731
 
b7f731
 	memset(&info, 0, sizeof(info));
b7f731
 	info.array = array;
b7f731
-	sysfs_init(&info, fd, NULL);
b7f731
+	if (sysfs_init(&info, fd, NULL)) {
b7f731
+		pr_err("failed to intialize sysfs.\n");
b7f731
+		rv = 1;
b7f731
+		goto release;
b7f731
+	}
b7f731
 	strcpy(info.text_version, sra->text_version);
b7f731
 	info.component_size = s->size*2;
b7f731
 	info.new_level = s->level;
b7f731
@@ -2870,7 +2877,11 @@ static int impose_level(int fd, int level, char *devname, int verbose)
b7f731
 	char *c;
b7f731
 	struct mdu_array_info_s array;
b7f731
 	struct mdinfo info;
b7f731
-	sysfs_init(&info, fd, NULL);
b7f731
+
b7f731
+	if (sysfs_init(&info, fd, NULL)) {
b7f731
+		pr_err("failed to intialize sysfs.\n");
b7f731
+		return  1;
b7f731
+	}
b7f731
 
b7f731
 	md_get_array_info(fd, &array);
b7f731
 	if (level == 0 &&
b7f731
@@ -3178,7 +3189,12 @@ static int reshape_array(char *container, int fd, char *devname,
b7f731
 		struct mdinfo *d;
b7f731
 
b7f731
 		if (info2) {
b7f731
-			sysfs_init(info2, fd, st->devnm);
b7f731
+			if (sysfs_init(info2, fd, st->devnm)) {
b7f731
+				pr_err("unable to initialize sysfs for %s",
b7f731
+				       st->devnm);
b7f731
+				free(info2);
b7f731
+				goto release;
b7f731
+			}
b7f731
 			/* When increasing number of devices, we need to set
b7f731
 			 * new raid_disks before adding these, or they might
b7f731
 			 * be rejected.
b7f731
@@ -3777,7 +3793,12 @@ int reshape_container(char *container, char *devname,
b7f731
 		}
b7f731
 		strcpy(last_devnm, mdstat->devnm);
b7f731
 
b7f731
-		sysfs_init(content, fd, mdstat->devnm);
b7f731
+		if (sysfs_init(content, fd, mdstat->devnm)) {
b7f731
+			pr_err("Unable to initialize sysfs for %s\n",
b7f731
+			       mdstat->devnm);
b7f731
+			rv = 1;
b7f731
+			break;
b7f731
+		}
b7f731
 
b7f731
 		if (mdmon_running(container))
b7f731
 			flush_mdmon(container);
b7f731
@@ -5110,7 +5131,13 @@ int Grow_continue_command(char *devname, int fd,
b7f731
 			goto Grow_continue_command_exit;
b7f731
 		}
b7f731
 
b7f731
-		sysfs_init(content, fd2, mdstat->devnm);
b7f731
+		if (sysfs_init(content, fd2, mdstat->devnm)) {
b7f731
+			pr_err("Unable to initialize sysfs for %s, Grow cannot continue",
b7f731
+			       mdstat->devnm);
b7f731
+			ret_val = 1;
b7f731
+			close(fd2);
b7f731
+			goto Grow_continue_command_exit;
b7f731
+		}
b7f731
 
b7f731
 		close(fd2);
b7f731
 
b7f731
diff --git a/Incremental.c b/Incremental.c
b7f731
index 802e525..28f1f77 100644
b7f731
--- a/Incremental.c
b7f731
+++ b/Incremental.c
b7f731
@@ -326,7 +326,12 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
b7f731
 		if (mdfd < 0)
b7f731
 			goto out_unlock;
b7f731
 
b7f731
-		sysfs_init(&info, mdfd, NULL);
b7f731
+		if (sysfs_init(&info, mdfd, NULL)) {
b7f731
+			pr_err("unable to initialize sysfs for %s\n",
b7f731
+			       chosen_name);
b7f731
+			rv = 2;
b7f731
+			goto out_unlock;
b7f731
+		}
b7f731
 
b7f731
 		if (set_array_info(mdfd, st, &info) != 0) {
b7f731
 			pr_err("failed to set array info for %s: %s\n",
b7f731
@@ -1734,7 +1739,10 @@ int IncrementalRemove(char *devname, char *id_path, int verbose)
b7f731
 			pr_err("%s does not appear to be a component of any array\n", devname);
b7f731
 		return 1;
b7f731
 	}
b7f731
-	sysfs_init(&mdi, -1, ent->devnm);
b7f731
+	if (sysfs_init(&mdi, -1, ent->devnm)) {
b7f731
+		pr_err("unable to initialize sysfs for: %s\n", devname);
b7f731
+		return 1;
b7f731
+	}
b7f731
 	mdfd = open_dev_excl(ent->devnm);
b7f731
 	if (mdfd > 0) {
b7f731
 		close(mdfd);
b7f731
diff --git a/Manage.c b/Manage.c
b7f731
index 0ffb6c6..618c98b 100644
b7f731
--- a/Manage.c
b7f731
+++ b/Manage.c
b7f731
@@ -1382,12 +1382,15 @@ int Manage_subdevs(char *devname, int fd,
b7f731
 	int busy = 0;
b7f731
 	int raid_slot = -1;
b7f731
 
b7f731
+	if (sysfs_init(&info, fd, NULL)) {
b7f731
+		pr_err("sysfs not availabile for %s\n", devname);
b7f731
+		goto abort;
b7f731
+	}
b7f731
+
b7f731
 	if (md_get_array_info(fd, &array)) {
b7f731
 		pr_err("Cannot get array info for %s\n", devname);
b7f731
 		goto abort;
b7f731
 	}
b7f731
-	sysfs_init(&info, fd, NULL);
b7f731
-
b7f731
 	/* array.size is only 32 bits and may be truncated.
b7f731
 	 * So read from sysfs if possible, and record number of sectors
b7f731
 	 */
b7f731
diff --git a/Monitor.c b/Monitor.c
b7f731
index 2c0f717..036a561 100644
b7f731
--- a/Monitor.c
b7f731
+++ b/Monitor.c
b7f731
@@ -1026,7 +1026,9 @@ int Wait(char *dev)
b7f731
 			 */
b7f731
 			struct mdinfo mdi;
b7f731
 			char buf[21];
b7f731
-			sysfs_init(&mdi, -1, devnm);
b7f731
+
b7f731
+			if (sysfs_init(&mdi, -1, devnm))
b7f731
+				return 2;
b7f731
 			if (sysfs_get_str(&mdi, NULL, "sync_action",
b7f731
 					  buf, 20) > 0 &&
b7f731
 			    strcmp(buf,"idle\n") != 0) {
b7f731
diff --git a/mdadm.c b/mdadm.c
b7f731
index d6b5437..3fe17fc 100644
b7f731
--- a/mdadm.c
b7f731
+++ b/mdadm.c
b7f731
@@ -1631,7 +1631,10 @@ int main(int argc, char *argv[])
b7f731
 				rv = 1;
b7f731
 				break;
b7f731
 			}
b7f731
-			sysfs_init(&sra, mdfd, NULL);
b7f731
+			if (sysfs_init(&sra, mdfd, NULL)) {
b7f731
+				rv = 1;
b7f731
+				break;
b7f731
+			}
b7f731
 			if (array_size == MAX_SIZE)
b7f731
 				err = sysfs_set_str(&sra, NULL, "array_size", "default");
b7f731
 			else
b7f731
@@ -1998,13 +2001,15 @@ int SetAction(char *dev, char *action)
b7f731
 {
b7f731
 	int fd = open(dev, O_RDONLY);
b7f731
 	struct mdinfo mdi;
b7f731
+	int retval;
b7f731
+
b7f731
 	if (fd < 0) {
b7f731
 		pr_err("Couldn't open %s: %s\n", dev, strerror(errno));
b7f731
 		return 1;
b7f731
 	}
b7f731
-	sysfs_init(&mdi, fd, NULL);
b7f731
+	retval = sysfs_init(&mdi, fd, NULL);
b7f731
 	close(fd);
b7f731
-	if (!mdi.sys_name[0]) {
b7f731
+	if (retval) {
b7f731
 		pr_err("%s is no an md array\n", dev);
b7f731
 		return 1;
b7f731
 	}
b7f731
diff --git a/mdadm.h b/mdadm.h
b7f731
index 084bc97..612bd86 100644
b7f731
--- a/mdadm.h
b7f731
+++ b/mdadm.h
b7f731
@@ -639,7 +639,7 @@ enum sysfs_read_flags {
b7f731
  * else use devnm.
b7f731
  */
b7f731
 extern int sysfs_open(char *devnm, char *devname, char *attr);
b7f731
-extern void sysfs_init(struct mdinfo *mdi, int fd, char *devnm);
b7f731
+extern int sysfs_init(struct mdinfo *mdi, int fd, char *devnm);
b7f731
 extern void sysfs_init_dev(struct mdinfo *mdi, unsigned long devid);
b7f731
 extern void sysfs_free(struct mdinfo *sra);
b7f731
 extern struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options);
b7f731
diff --git a/sysfs.c b/sysfs.c
b7f731
index 93ec3de..51deb23 100644
b7f731
--- a/sysfs.c
b7f731
+++ b/sysfs.c
b7f731
@@ -84,25 +84,30 @@ void sysfs_init_dev(struct mdinfo *mdi, unsigned long devid)
b7f731
 		 sizeof(mdi->sys_name), "dev-%s", devid2kname(devid));
b7f731
 }
b7f731
 
b7f731
-void sysfs_init(struct mdinfo *mdi, int fd, char *devnm)
b7f731
+int sysfs_init(struct mdinfo *mdi, int fd, char *devnm)
b7f731
 {
b7f731
 	struct stat stb;
b7f731
 	char fname[MAX_SYSFS_PATH_LEN];
b7f731
+	int retval = -ENODEV;
b7f731
 
b7f731
 	mdi->sys_name[0] = 0;
b7f731
 	if (fd >= 0)
b7f731
 		devnm = fd2devnm(fd);
b7f731
 
b7f731
 	if (devnm == NULL)
b7f731
-		return;
b7f731
+		goto out;
b7f731
 
b7f731
 	snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md", devnm);
b7f731
 
b7f731
 	if (stat(fname, &stb))
b7f731
-		return;
b7f731
+		goto out;
b7f731
 	if (!S_ISDIR(stb.st_mode))
b7f731
-		return;
b7f731
+		goto out;
b7f731
 	strcpy(mdi->sys_name, devnm);
b7f731
+
b7f731
+	retval = 0;
b7f731
+out:
b7f731
+	return retval;
b7f731
 }
b7f731
 
b7f731
 struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options)
b7f731
@@ -117,8 +122,7 @@ struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options)
b7f731
 	struct dirent *de;
b7f731
 
b7f731
 	sra = xcalloc(1, sizeof(*sra));
b7f731
-	sysfs_init(sra, fd, devnm);
b7f731
-	if (sra->sys_name[0] == 0) {
b7f731
+	if (sysfs_init(sra, fd, devnm)) {
b7f731
 		free(sra);
b7f731
 		return NULL;
b7f731
 	}
b7f731
-- 
b7f731
2.7.4
b7f731