From d5688130715fcf40cd95058061cf119ba157c075 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 30 Sep 2011 11:09:38 +0200 Subject: [PATCH] umount: use new lib/loopdev.c code Signed-off-by: Karel Zak --- include/loopdev.h | 1 + lib/loopdev.c | 40 ++++++++++++++++++++++++++++++++++++++++ mount/Makefile.am | 30 ++++++++++++++++-------------- mount/umount.c | 29 +++++++++++++---------------- 4 files changed, 70 insertions(+), 30 deletions(-) diff --git a/include/loopdev.h b/include/loopdev.h index 6136535e3b..e25aa96b62 100644 --- a/include/loopdev.h +++ b/include/loopdev.h @@ -119,6 +119,7 @@ extern char *loopdev_find_by_backing_file(const char *filename, uint64_t offset, int flags); extern int loopcxt_find_unused(struct loopdev_cxt *lc); extern int loopdev_delete(const char *device); +extern int loopdev_count_by_backing_file(const char *filename, char **loopdev); /* * Low-level diff --git a/lib/loopdev.c b/lib/loopdev.c index 11555b0a84..f83a443a0c 100644 --- a/lib/loopdev.c +++ b/lib/loopdev.c @@ -1076,6 +1076,46 @@ char *loopdev_find_by_backing_file(const char *filename, uint64_t offset, int fl return res; } +/* + * Returns number of loop devices associated with @file, if only one loop + * device is associeted with the given @filename and @loopdev is not NULL then + * @loopdev returns name of the device. + */ +int loopdev_count_by_backing_file(const char *filename, char **loopdev) +{ + struct loopdev_cxt lc; + int count = 0; + + if (!filename) + return -1; + + loopcxt_init(&lc, 0); + if (loopcxt_init_iterator(&lc, LOOPITER_FL_USED)) + return -1; + + while(loopcxt_next(&lc) == 0) { + char *backing = loopcxt_get_backing_file(&lc); + + if (!backing || strcmp(backing, filename)) { + free(backing); + continue; + } + + free(backing); + if (loopdev && count == 0) + *loopdev = loopcxt_strdup_device(&lc); + count++; + } + + loopcxt_deinit(&lc); + + if (loopdev && count > 1) { + free(*loopdev); + *loopdev = NULL; + } + return count; +} + #ifdef TEST_PROGRAM_LOOPDEV #include diff --git a/mount/Makefile.am b/mount/Makefile.am index 8f4a4870f1..052d63cdfe 100644 --- a/mount/Makefile.am +++ b/mount/Makefile.am @@ -11,10 +11,20 @@ srcs_common = sundries.c $(top_srcdir)/lib/canonicalize.c sundries.h hdrs_mount = fstab.h mount_mntent.h mount_constants.h getusername.h # generic sources for mount and umount -srcs_mount = fstab.c mount_mntent.c getusername.c devname.c devname.h \ - $(srcs_common) $(hdrs_mount) $(top_srcdir)/lib/env.c \ - $(top_srcdir)/lib/linux_version.c $(top_srcdir)/lib/blkdev.c \ - $(top_srcdir)/lib/fsprobe.c $(top_srcdir)/lib/mangle.c +srcs_mount = $(srcs_common) $(hdrs_mount) \ + fstab.c \ + mount_mntent.c \ + getusername.c \ + devname.c devname.h \ + $(top_srcdir)/lib/env.c \ + $(top_srcdir)/lib/linux_version.c \ + $(top_srcdir)/lib/blkdev.c \ + $(top_srcdir)/lib/fsprobe.c \ + $(top_srcdir)/lib/mangle.c \ + $(top_srcdir)/lib/at.c \ + $(top_srcdir)/lib/sysfs.c \ + $(top_srcdir)/lib/loopdev.c \ + $(top_srcdir)/lib/strutils.c # generic flags for all programs (except losetup) # -- note that pkg-config autoconf macros (pkg.m4) does not differentiate @@ -24,20 +34,12 @@ ldadd_static = cflags_common = $(AM_CFLAGS) ldflags_static = -all-static -mount_SOURCES = mount.c \ - $(srcs_mount) \ - $(top_srcdir)/lib/setproctitle.c \ - $(top_srcdir)/lib/strutils.c \ - $(top_srcdir)/lib/at.c \ - $(top_srcdir)/lib/sysfs.c \ - $(top_srcdir)/lib/loopdev.c - +mount_SOURCES = mount.c $(srcs_mount) $(top_srcdir)/lib/setproctitle.c mount_CFLAGS = $(SUID_CFLAGS) $(cflags_common) mount_LDFLAGS = $(SUID_LDFLAGS) $(AM_LDFLAGS) mount_LDADD = $(ldadd_common) -umount_SOURCES = umount.c $(srcs_mount) $(top_srcdir)/lib/strutils.c \ - lomount.c lomount.h loop.h +umount_SOURCES = umount.c $(srcs_mount) umount_CFLAGS = $(SUID_CFLAGS) $(cflags_common) umount_LDFLAGS = $(SUID_LDFLAGS) $(AM_LDFLAGS) umount_LDADD = $(ldadd_common) diff --git a/mount/umount.c b/mount/umount.c index c7ebf7d3c8..f29d7cafb8 100644 --- a/mount/umount.c +++ b/mount/umount.c @@ -16,8 +16,7 @@ #include "sundries.h" #include "getusername.h" #include "pathnames.h" -#include "lomount.h" -#include "loop.h" +#include "loopdev.h" #include "fstab.h" #include "env.h" #include "nls.h" @@ -299,7 +298,7 @@ umount_one (const char *spec, const char *node, const char *type, * Ignore the option "-d" for non-loop devices and loop devices with * LO_FLAGS_AUTOCLEAR flag. */ - if (delloop && is_loop_device(spec)) + if (delloop && is_loopdev(spec)) myloop = 1; if (restricted) { @@ -407,8 +406,8 @@ umount_one (const char *spec, const char *node, const char *type, loopdev = spec; } gotloop: - if (loopdev && !is_loop_autoclear(loopdev)) - del_loop(loopdev); + if (loopdev && !loopdev_is_autoclear(loopdev)) + loopdev_delete(loopdev); writemtab: if (!nomtab && @@ -559,8 +558,8 @@ is_valid_loop(struct mntentchn *mc, struct mntentchn *fs) } /* check association */ - if (loopfile_used_with((char *) mc->m.mnt_fsname, - fs->m.mnt_fsname, offset) == 1) { + if (loopdev_is_used((char *) mc->m.mnt_fsname, fs->m.mnt_fsname, + offset, LOOPDEV_FL_OFFSET) == 1) { if (verbose > 1) printf(_("device %s is associated with %s\n"), mc->m.mnt_fsname, fs->m.mnt_fsname); @@ -644,20 +643,18 @@ try_loopdev: * (only if it is a regular file) */ if (!mc && !loopdev && !stat(file, &statbuf) && S_ISREG(statbuf.st_mode)) { - switch (find_loopdev_by_backing_file(file, &loopdev)) { - case 0: + int count = loopdev_count_by_backing_file(file, &loopdev); + + if (count == 1) { if (verbose) printf(_("%s is associated with %s\n"), arg, loopdev); file = loopdev; goto try_loopdev; - break; - case 2: - if (verbose) - printf(_("%s is associated with more than one loop device: not unmounting\n"), - arg); - break; - } + + } else if (count > 1 && verbose) + printf(_("%s is associated with more than one " + "loop device: not unmounting\n"), arg); } if (mc) { -- 2.47.3