summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2017-03-16 16:49:37 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2017-03-16 16:55:47 +0100
commitab518eea9c41235a3fcde80f3ea99669eaade621 (patch)
tree4bd1783e343316fa11c2ade135dbe0b0561689a9
parent018804204f41e6e60cec536843275f8fdd4d3620 (diff)
mount: create loop devices with LO_FLAGS_AUTOCLEAR flag
The "autolooped" mount (mount [-oloop] IMAGE /DIR/DIR) always creates AUTOCLEARed loopdevs, so that umounting drops them (and this does not require any code in the umount userspace). This happens since circa linux-2.6.25: commit 96c5865559cee0f9cbc5173f3c949f6ce3525581 Date: Wed Feb 6 01:36:27 2008 -0800 Subject: Allow auto-destruction of loop devices IOW: in this case, umount does not have to use -d to drop the loopdev. The explicit loop mount (mount /dev/loopN /DIR/DIR) does not do this. In this case, umount without -d should not drop loopdev. Unfortunately, bbox umount currently always implies -d, this probably needs fixing. function old new delta set_loop 537 597 +60 singlemount 1101 1138 +37 losetup_main 419 432 +13 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 3/0 up/down: 110/0) Total: 110 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--include/libbb.h11
-rw-r--r--libbb/loop.c49
-rw-r--r--util-linux/losetup.c27
-rw-r--r--util-linux/mount.c21
4 files changed, 87 insertions, 21 deletions
diff --git a/include/libbb.h b/include/libbb.h
index b054e0559..e97efcb6e 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1329,10 +1329,15 @@ extern int get_linux_version_code(void) FAST_FUNC;
extern char *query_loop(const char *device) FAST_FUNC;
extern int del_loop(const char *device) FAST_FUNC;
-/* If *devname is not NULL, use that name, otherwise try to find free one,
+/*
+ * If *devname is not NULL, use that name, otherwise try to find free one,
* malloc and return it in *devname.
- * return value: 1: read-only loopdev was setup, 0: rw, < 0: error */
-extern int set_loop(char **devname, const char *file, unsigned long long offset, int ro) FAST_FUNC;
+ * return value is the opened fd to the loop device, or < on error
+ */
+extern int set_loop(char **devname, const char *file, unsigned long long offset, unsigned flags) FAST_FUNC;
+/* These constants match linux/loop.h (without BB_ prefix): */
+#define BB_LO_FLAGS_READ_ONLY 1
+#define BB_LO_FLAGS_AUTOCLEAR 4
/* Like bb_ask below, but asks on stdin with no timeout. */
char *bb_ask_stdin(const char * prompt) FAST_FUNC;
diff --git a/libbb/loop.c b/libbb/loop.c
index d30b378d7..f0d4296ae 100644
--- a/libbb/loop.c
+++ b/libbb/loop.c
@@ -78,22 +78,24 @@ int FAST_FUNC del_loop(const char *device)
return rc;
}
-/* Returns 0 if mounted RW, 1 if mounted read-only, <0 for error.
- *device is loop device to use, or if *device==NULL finds a loop device to
- mount it on and sets *device to a strdup of that loop device name. This
- search will re-use an existing loop device already bound to that
- file/offset if it finds one.
+/* Returns opened fd to the loop device, <0 on error.
+ * *device is loop device to use, or if *device==NULL finds a loop device to
+ * mount it on and sets *device to a strdup of that loop device name. This
+ * search will re-use an existing loop device already bound to that
+ * file/offset if it finds one.
*/
-int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offset, int ro)
+int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offset, unsigned flags)
{
char dev[LOOP_NAMESIZE];
char *try;
bb_loop_info loopinfo;
struct stat statbuf;
- int i, dfd, ffd, mode, rc = -1;
+ int i, dfd, ffd, mode, rc;
+
+ rc = dfd = -1;
/* Open the file. Barf if this doesn't work. */
- mode = ro ? O_RDONLY : O_RDWR;
+ mode = (flags & BB_LO_FLAGS_READ_ONLY) ? O_RDONLY : O_RDWR;
open_ffd:
ffd = open(file, mode);
if (ffd < 0) {
@@ -144,20 +146,35 @@ int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offse
/* If device is free, claim it. */
if (rc && errno == ENXIO) {
- memset(&loopinfo, 0, sizeof(loopinfo));
- safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
- loopinfo.lo_offset = offset;
/* Associate free loop device with file. */
if (ioctl(dfd, LOOP_SET_FD, ffd) == 0) {
- if (ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo) == 0)
- rc = 0;
- else
+ memset(&loopinfo, 0, sizeof(loopinfo));
+ safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
+ loopinfo.lo_offset = offset;
+ /*
+ * Used by mount to set LO_FLAGS_AUTOCLEAR.
+ * LO_FLAGS_READ_ONLY is not set because RO is controlled by open type of the file.
+ * Note that closing LO_FLAGS_AUTOCLEARed dfd before mount
+ * is wrong (would free the loop device!)
+ */
+ loopinfo.lo_flags = (flags & ~BB_LO_FLAGS_READ_ONLY);
+ rc = ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo);
+ if (rc != 0 && (loopinfo.lo_flags & BB_LO_FLAGS_AUTOCLEAR)) {
+ /* Old kernel, does not support LO_FLAGS_AUTOCLEAR? */
+ /* (this code path is not tested) */
+ loopinfo.lo_flags -= BB_LO_FLAGS_AUTOCLEAR;
+ rc = ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo);
+ }
+ if (rc != 0) {
ioctl(dfd, LOOP_CLR_FD, 0);
+ }
}
} else {
rc = -1;
}
- close(dfd);
+ if (rc != 0) {
+ close(dfd);
+ }
try_again:
if (*device) break;
}
@@ -165,7 +182,7 @@ int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offse
if (rc == 0) {
if (!*device)
*device = xstrdup(dev);
- return (mode == O_RDONLY); /* 1:ro, 0:rw */
+ return dfd;
}
return rc;
}
diff --git a/util-linux/losetup.c b/util-linux/losetup.c
index 4424d9cbb..d356f49c2 100644
--- a/util-linux/losetup.c
+++ b/util-linux/losetup.c
@@ -127,12 +127,37 @@ int losetup_main(int argc UNUSED_PARAM, char **argv)
d = *argv++;
if (argv[0]) {
- if (set_loop(&d, argv[0], offset, (opt & OPT_r)) < 0)
+ if (set_loop(&d, argv[0], offset, (opt & OPT_r) ? BB_LO_FLAGS_READ_ONLY : 0) < 0)
bb_simple_perror_msg_and_die(argv[0]);
return EXIT_SUCCESS;
}
}
+ /* TODO: util-linux 2.28 shows this when run w/o params:
+ * NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO
+ * /dev/loop0 0 0 1 0 /PATH/TO/FILE 0
+ *
+ * implemented by reading /sys:
+ *
+ * open("/sys/block", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 3
+ * newfstatat(3, "loop0/loop/backing_file", {st_mode=S_IFREG|0444, st_size=4096, ...}, 0) = 0
+ * stat("/dev/loop0", {st_mode=S_IFBLK|0660, st_rdev=makedev(7, 0), ...}) = 0
+ * open("/sys/dev/block/7:0/loop/offset", O_RDONLY|O_CLOEXEC) = 5
+ * read(5, "0\n", 4096) = 2
+ * open("/sys/dev/block/7:0/loop/sizelimit", O_RDONLY|O_CLOEXEC) = 5
+ * read(5, "0\n", 4096) = 2
+ * open("/sys/dev/block/7:0/loop/offset", O_RDONLY|O_CLOEXEC) = 5
+ * read(5, "0\n", 4096) = 2
+ * open("/sys/dev/block/7:0/loop/autoclear", O_RDONLY|O_CLOEXEC) = 5
+ * read(5, "1\n", 4096) = 2
+ * open("/sys/dev/block/7:0/ro", O_RDONLY|O_CLOEXEC) = 5
+ * read(5, "0\n", 4096) = 2
+ * open("/sys/dev/block/7:0/loop/backing_file", O_RDONLY|O_CLOEXEC) = 5
+ * read(5, "/PATH/TO/FILE", 4096) = 37
+ * open("/sys/dev/block/7:0/loop/dio", O_RDONLY|O_CLOEXEC) = 5
+ * read(5, "0\n", 4096) = 2
+ */
+
bb_show_usage(); /* does not return */
/*return EXIT_FAILURE;*/
}
diff --git a/util-linux/mount.c b/util-linux/mount.c
index f0245f714..6bb18524d 100644
--- a/util-linux/mount.c
+++ b/util-linux/mount.c
@@ -1887,6 +1887,7 @@ static int nfsmount(struct mntent *mp, unsigned long vfsflags, char *filteropts)
// NB: mp->xxx fields may be trashed on exit
static int singlemount(struct mntent *mp, int ignore_busy)
{
+ int loopfd = -1;
int rc = -1;
unsigned long vfsflags;
char *loopFile = NULL, *filteropts = NULL;
@@ -2026,7 +2027,20 @@ static int singlemount(struct mntent *mp, int ignore_busy)
if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
loopFile = bb_simplify_path(mp->mnt_fsname);
mp->mnt_fsname = NULL; // will receive malloced loop dev name
- if (set_loop(&mp->mnt_fsname, loopFile, 0, /*ro:*/ (vfsflags & MS_RDONLY)) < 0) {
+
+ // mount always creates AUTOCLEARed loopdevs, so that umounting
+ // drops them without any code in the userspace.
+ // This happens since circa linux-2.6.25:
+ // commit 96c5865559cee0f9cbc5173f3c949f6ce3525581
+ // Date: Wed Feb 6 01:36:27 2008 -0800
+ // Subject: Allow auto-destruction of loop devices
+ loopfd = set_loop(&mp->mnt_fsname,
+ loopFile,
+ 0,
+ ((vfsflags & MS_RDONLY) ? BB_LO_FLAGS_READ_ONLY : 0)
+ | BB_LO_FLAGS_AUTOCLEAR
+ );
+ if (loopfd < 0) {
if (errno == EPERM || errno == EACCES)
bb_error_msg(bb_msg_perm_denied_are_you_root);
else
@@ -2074,6 +2088,8 @@ static int singlemount(struct mntent *mp, int ignore_busy)
}
// If mount failed, clean up loop file (if any).
+ // (Newer kernels which support LO_FLAGS_AUTOCLEAR should not need this,
+ // merely "close(loopfd)" should do it?)
if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
del_loop(mp->mnt_fsname);
if (ENABLE_FEATURE_CLEAN_UP) {
@@ -2086,6 +2102,9 @@ static int singlemount(struct mntent *mp, int ignore_busy)
if (ENABLE_FEATURE_CLEAN_UP)
free(filteropts);
+ if (loopfd >= 0)
+ close(loopfd);
+
if (errno == EBUSY && ignore_busy)
return 0;
if (errno == ENOENT && (vfsflags & MOUNT_NOFAIL))