All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Sandeen <sandeen@redhat.com>
To: ext4 development <linux-ext4@vger.kernel.org>
Cc: Kazuya Mio <k-mio@sx.jp.nec.com>
Subject: [PATCH] e4defrag: Handle device symlinks
Date: Thu, 19 Jul 2012 14:02:30 -0500	[thread overview]
Message-ID: <500859C6.6040401@redhat.com> (raw)

Device nodes are commonly accessed via symlinks, i.e.

# ls -l /dev/mapper/testvg-testlv 
lrwxrwxrwx. 1 root root 7 Jul 19 13:01 /dev/mapper/testvg-testlv -> ../dm-0

Today, e4defrag on such a device will fail:

# e4defrag -c /dev/mapper/testvg-testlv 
File is not regular file
 "/dev/mapper/testvg-testlv"

due to it being a link, and e4defrag on the link target does as well:

# e4defrag -c /dev/dm-0 
Filesystem is not mounted

due to the target not being found in /etc/mtab.

Fix this by checking whether the symlink target is a block device
and if so, using that device in main(), and also changing get_mount_point()
to search for a matching device number, not device name.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
---


diff --git a/misc/e4defrag.c b/misc/e4defrag.c
index 6491edd..260265c 100644
--- a/misc/e4defrag.c
+++ b/misc/e4defrag.c
@@ -266,8 +266,15 @@ static int get_mount_point(const char *devname, char *mount_point,
 {
 	/* Refer to /etc/mtab */
 	const char	*mtab = MOUNTED;
-	FILE	*fp = NULL;
+	FILE		*fp = NULL;
 	struct mntent	*mnt = NULL;
+	struct stat64	sb;
+
+	if (stat64(devname, &sb) < 0) {
+		perror(NGMSG_FILE_INFO);
+		PRINT_FILE_NAME(devname);
+		return -1;
+	}
 
 	fp = setmntent(mtab, "r");
 	if (fp == NULL) {
@@ -276,7 +283,15 @@ static int get_mount_point(const char *devname, char *mount_point,
 	}
 
 	while ((mnt = getmntent(fp)) != NULL) {
-		if (strcmp(devname, mnt->mnt_fsname) != 0)
+		struct stat64 ms;
+
+		/*
+		 * To handle device symlinks, we see if the
+ 		 * device number matches, not the name
+ 		 */
+		if (stat64(mnt->mnt_fsname, &ms) < 0)
+			continue;
+		if (sb.st_rdev != ms.st_rdev)
 			continue;
 
 		endmntent(fp);
@@ -1770,6 +1785,15 @@ int main(int argc, char *argv[])
 			continue;
 		}
 
+		/* Handle i.e. lvm device symlinks */
+		if (S_ISLNK(buf.st_mode)) {
+			struct stat64	buf2;
+
+			if (stat64(argv[i], &buf2) == 0 &&
+			    S_ISBLK(buf2.st_mode))
+				buf = buf2;
+		}
+
 		if (S_ISBLK(buf.st_mode)) {
 			/* Block device */
 			strncpy(dev_name, argv[i], strnlen(argv[i], PATH_MAX));



             reply	other threads:[~2012-07-19 19:02 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-19 19:02 Eric Sandeen [this message]
2012-07-19 19:04 ` [PATCH] e4defrag: Handle device symlinks Eric Sandeen
2012-07-28 21:54 ` Theodore Ts'o

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=500859C6.6040401@redhat.com \
    --to=sandeen@redhat.com \
    --cc=k-mio@sx.jp.nec.com \
    --cc=linux-ext4@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.