From patchwork Thu Jul 19 19:02:30 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Sandeen X-Patchwork-Id: 172037 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 81DF32C0210 for ; Fri, 20 Jul 2012 05:02:39 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752263Ab2GSTCf (ORCPT ); Thu, 19 Jul 2012 15:02:35 -0400 Received: from mx1.redhat.com ([209.132.183.28]:27430 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752019Ab2GSTCf (ORCPT ); Thu, 19 Jul 2012 15:02:35 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q6JJ2WIf028133 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 19 Jul 2012 15:02:33 -0400 Received: from liberator.sandeen.net (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q6JJ2U1U011662 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 19 Jul 2012 15:02:31 -0400 Message-ID: <500859C6.6040401@redhat.com> Date: Thu, 19 Jul 2012 14:02:30 -0500 From: Eric Sandeen User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 MIME-Version: 1.0 To: ext4 development CC: Kazuya Mio Subject: [PATCH] e4defrag: Handle device symlinks X-Enigmail-Version: 1.4.3 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org 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 Reported-by: Peter Hjalmarsson --- -- To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html 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));