From: Andreas Dilger Date: Fri, 13 Apr 2012 08:16:24 +0000 (-0600) Subject: ext2fs: check if Lustre filesystem is mounted X-Git-Tag: v1.42.9.wc1~7 X-Git-Url: https://git.whamcloud.com/?a=commitdiff_plain;h=a4a95e77f3a47875e58c1cbabc133cab9096d21c;p=tools%2Fe2fsprogs.git ext2fs: check if Lustre filesystem is mounted Add a check to ext2fs_check_mount_point() to loo in /proc/fs/lustre/* to see if Lustre is mounted, since st_rdev of the mountpoint does not match st_rdev of the block device itself, which confuses libext2fs. Signed-off-by: Andreas Dilger --- diff --git a/lib/ext2fs/ismounted.c b/lib/ext2fs/ismounted.c index 6c6ecff..b88d8db 100644 --- a/lib/ext2fs/ismounted.c +++ b/lib/ext2fs/ismounted.c @@ -38,6 +38,8 @@ #endif /* HAVE_GETMNTINFO */ #include #include +#include +#include #include "ext2_fs.h" #include "ext2fs.h" @@ -335,6 +337,140 @@ leave: return ret; } +static int check_lustre_proc_vals(const char *procname,const char *real_devname) +{ + struct dirent *direntp; + DIR *dirp; + int rc = 0; + char *mnt_device, *proc_val, *real_mnt_device; + + mnt_device = malloc(PATH_MAX); + if (mnt_device == NULL) { + fprintf(stderr, "Cannot allocate memory to store path\n"); + return 1; + } + proc_val = malloc(PATH_MAX); + if (proc_val == NULL) { + fprintf(stderr, "Cannot allocate memory to store path\n"); + free(mnt_device); + return 1; + } + real_mnt_device = malloc(PATH_MAX); + if (real_mnt_device == NULL) { + fprintf(stderr, "Cannot allocate memory to store path\n"); + free(proc_val); + free(mnt_device); + return 1; + } + + dirp = opendir(procname); + if (dirp) { + do { + int fd, numr; + char *ptr; + + direntp = readdir(dirp); + if (direntp == NULL) + break; + if ((strncmp(direntp->d_name, ".", 1) == 0) || + (strncmp(direntp->d_name, "..", 2) == 0)) { + continue; + } + + memset(proc_val, 0, PATH_MAX); + snprintf(proc_val, PATH_MAX, "%s/%s/mntdev", procname, + direntp->d_name); + fd = open(proc_val, O_RDONLY); + if (fd < 0) { + if (errno == ENOENT || errno == ENOTDIR) + continue; + fprintf(stderr, "Cannot open %s: %s\n", + proc_val, strerror(errno)); + rc = 1; + goto out; + } + + memset(mnt_device, 0, PATH_MAX); + numr = 0; + ptr = mnt_device; + do { + numr = read(fd, ptr, PATH_MAX); + if (numr < 0) { + fprintf(stderr, + "Failure to get mntdev info\n"); + rc = 1; + close(fd); + goto out; + } + ptr += numr; + } while (numr != 0); + close(fd); + + ptr = strchr(mnt_device, '\n'); + if (ptr) + *ptr = '\0'; + + memset(real_mnt_device, 0, PATH_MAX); + if (realpath(mnt_device, real_mnt_device) == NULL) { + fprintf(stderr, "Cannot resolve mntdev %s\n", + mnt_device); + rc = 1; + goto out; + } + + if (strcmp(real_devname, real_mnt_device) == 0) { + fprintf(stderr, + "device %s mounted by lustre per %s\n", + real_devname, proc_val); + rc = -1; + goto out; + } + } while (direntp != NULL); + + closedir(dirp); + } + +out: + free(proc_val); + free(mnt_device); + free(real_mnt_device); + + return rc; +} + +static errcode_t check_if_lustre_mounted(const char *device, int *mount_flags) +{ + char *real_device; + int rc = 0; + + real_device = malloc(PATH_MAX); + if (real_device == NULL) { + fprintf(stderr, "Cannot allocate memory to store path\n"); + return EXT2_ET_NO_MEMORY; + } + + if (realpath(device, real_device) == NULL) { + fprintf(stderr, "Cannot resolve path %s\n", device); + return EXT2_ET_BAD_DEVICE_NAME; + } + + rc = check_lustre_proc_vals("/proc/fs/lustre/obdfilter", real_device); + if (!rc) + rc = check_lustre_proc_vals("/proc/fs/lustre/mds", real_device); + if (!rc) + rc = check_lustre_proc_vals("/proc/fs/lustre/mgs", real_device); + if (!rc) + rc = check_lustre_proc_vals("/proc/fs/lustre/ost-ldiskfs", + real_device); + + if (rc) + *mount_flags |= EXT2_MF_MOUNTED; + + free(real_device); + + return 0; +} + /* * ext2fs_check_mount_point() fills determines if the device is @@ -387,6 +523,8 @@ errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags, } #endif + retval = check_if_lustre_mounted(device, mount_flags); + return 0; }