linux-raid.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mateusz Kusiak <mateusz.kusiak@intel.com>
To: linux-raid@vger.kernel.org
Cc: jes@trained-monkey.org, colyli@suse.de
Subject: [PATCH 2/2] util: remove obsolete code from get_md_name
Date: Mon,  2 Jan 2023 09:46:22 +0100	[thread overview]
Message-ID: <20230102084622.29154-2-mateusz.kusiak@intel.com> (raw)
In-Reply-To: <20230102084622.29154-1-mateusz.kusiak@intel.com>

get_md_name() is used only with mdstat entries.
Remove dead code and simplyfy function.

Remove redundadnt checks from mdmon.c

Signed-off-by: Mateusz Kusiak <mateusz.kusiak@intel.com>
---
 mdmon.c |  8 +++-----
 util.c  | 51 +++++++++++++++++----------------------------------
 2 files changed, 20 insertions(+), 39 deletions(-)

diff --git a/mdmon.c b/mdmon.c
index ecf52dc8..60ba3182 100644
--- a/mdmon.c
+++ b/mdmon.c
@@ -366,7 +366,7 @@ int main(int argc, char *argv[])
 		if (argv[optind]) {
 			container_name = get_md_name(argv[optind]);
 			if (!container_name)
-				container_name = argv[optind];
+				return 1;
 		}
 	}
 
@@ -403,11 +403,9 @@ int main(int argc, char *argv[])
 
 		return status;
 	} else {
-		int mdfd = open_mddev(container_name, 1);
-
-		if (mdfd < 0)
-			return 1;
+		int mdfd = open_mddev(container_name, 0);
 		devnm = fd2devnm(mdfd);
+
 		close(mdfd);
 	}
 
diff --git a/util.c b/util.c
index 26ffdcea..9cd89fa4 100644
--- a/util.c
+++ b/util.c
@@ -968,47 +968,30 @@ dev_t devnm2devid(char *devnm)
 	return 0;
 }
 
+/**
+ * get_md_name() - Get main dev node of the md device.
+ * @devnm: Md device name or path.
+ *
+ * Function checks if the full name was passed and returns md name
+ * if it is the MD device.
+ *
+ * Return: Main dev node of the md device or NULL if not found.
+ */
 char *get_md_name(char *devnm)
 {
-	/* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
-	/* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
-
-	static char devname[50];
+	static char devname[NAME_MAX];
 	struct stat stb;
-	dev_t rdev = devnm2devid(devnm);
-	char *dn;
 
-	if (rdev == 0)
-		return 0;
-	if (strncmp(devnm, "md_", 3) == 0) {
-		snprintf(devname, sizeof(devname), "/dev/md/%s",
-			devnm + 3);
-		if (stat(devname, &stb) == 0 &&
-		    (S_IFMT&stb.st_mode) == S_IFBLK && (stb.st_rdev == rdev))
-			return devname;
-	}
-	snprintf(devname, sizeof(devname), "/dev/%s", devnm);
-	if (stat(devname, &stb) == 0 && (S_IFMT&stb.st_mode) == S_IFBLK &&
-	    (stb.st_rdev == rdev))
-		return devname;
+	if (strncmp(devnm, "/dev/", 5) == 0)
+		snprintf(devname, sizeof(devname), "%s", devnm);
+	else
+		snprintf(devname, sizeof(devname), "/dev/%s", devnm);
 
-	snprintf(devname, sizeof(devname), "/dev/md/%s", devnm+2);
-	if (stat(devname, &stb) == 0 && (S_IFMT&stb.st_mode) == S_IFBLK &&
-	    (stb.st_rdev == rdev))
+	if (!is_mddev(devname))
+		return NULL;
+	if (stat(devname, &stb) == 0 && (S_IFMT&stb.st_mode) == S_IFBLK)
 		return devname;
 
-	dn = map_dev(major(rdev), minor(rdev), 0);
-	if (dn)
-		return dn;
-	snprintf(devname, sizeof(devname), "/dev/.tmp.%s", devnm);
-	if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
-		if (errno != EEXIST)
-			return NULL;
-
-	if (stat(devname, &stb) == 0 && (S_IFMT&stb.st_mode) == S_IFBLK &&
-	    (stb.st_rdev == rdev))
-		return devname;
-	unlink(devname);
 	return NULL;
 }
 
-- 
2.26.2


  reply	other threads:[~2023-01-02  8:47 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-02  8:46 [PATCH 1/2] mdmon: fix segfault Mateusz Kusiak
2023-01-02  8:46 ` Mateusz Kusiak [this message]
2023-01-04 15:26   ` [PATCH 2/2] util: remove obsolete code from get_md_name Jes Sorensen
2023-01-04 15:26 ` [PATCH 1/2] mdmon: fix segfault Jes Sorensen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230102084622.29154-2-mateusz.kusiak@intel.com \
    --to=mateusz.kusiak@intel.com \
    --cc=colyli@suse.de \
    --cc=jes@trained-monkey.org \
    --cc=linux-raid@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).