diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-03-16 16:49:37 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-03-16 16:55:47 +0100 |
commit | ab518eea9c41235a3fcde80f3ea99669eaade621 (patch) | |
tree | 4bd1783e343316fa11c2ade135dbe0b0561689a9 /libbb | |
parent | 018804204f41e6e60cec536843275f8fdd4d3620 (diff) | |
download | busybox-w32-ab518eea9c41235a3fcde80f3ea99669eaade621.tar.gz busybox-w32-ab518eea9c41235a3fcde80f3ea99669eaade621.tar.bz2 busybox-w32-ab518eea9c41235a3fcde80f3ea99669eaade621.zip |
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>
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/loop.c | 49 |
1 files changed, 33 insertions, 16 deletions
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) | |||
78 | return rc; | 78 | return rc; |
79 | } | 79 | } |
80 | 80 | ||
81 | /* Returns 0 if mounted RW, 1 if mounted read-only, <0 for error. | 81 | /* Returns opened fd to the loop device, <0 on error. |
82 | *device is loop device to use, or if *device==NULL finds a loop device to | 82 | * *device is loop device to use, or if *device==NULL finds a loop device to |
83 | mount it on and sets *device to a strdup of that loop device name. This | 83 | * mount it on and sets *device to a strdup of that loop device name. This |
84 | search will re-use an existing loop device already bound to that | 84 | * search will re-use an existing loop device already bound to that |
85 | file/offset if it finds one. | 85 | * file/offset if it finds one. |
86 | */ | 86 | */ |
87 | int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offset, int ro) | 87 | int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offset, unsigned flags) |
88 | { | 88 | { |
89 | char dev[LOOP_NAMESIZE]; | 89 | char dev[LOOP_NAMESIZE]; |
90 | char *try; | 90 | char *try; |
91 | bb_loop_info loopinfo; | 91 | bb_loop_info loopinfo; |
92 | struct stat statbuf; | 92 | struct stat statbuf; |
93 | int i, dfd, ffd, mode, rc = -1; | 93 | int i, dfd, ffd, mode, rc; |
94 | |||
95 | rc = dfd = -1; | ||
94 | 96 | ||
95 | /* Open the file. Barf if this doesn't work. */ | 97 | /* Open the file. Barf if this doesn't work. */ |
96 | mode = ro ? O_RDONLY : O_RDWR; | 98 | mode = (flags & BB_LO_FLAGS_READ_ONLY) ? O_RDONLY : O_RDWR; |
97 | open_ffd: | 99 | open_ffd: |
98 | ffd = open(file, mode); | 100 | ffd = open(file, mode); |
99 | if (ffd < 0) { | 101 | if (ffd < 0) { |
@@ -144,20 +146,35 @@ int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offse | |||
144 | 146 | ||
145 | /* If device is free, claim it. */ | 147 | /* If device is free, claim it. */ |
146 | if (rc && errno == ENXIO) { | 148 | if (rc && errno == ENXIO) { |
147 | memset(&loopinfo, 0, sizeof(loopinfo)); | ||
148 | safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE); | ||
149 | loopinfo.lo_offset = offset; | ||
150 | /* Associate free loop device with file. */ | 149 | /* Associate free loop device with file. */ |
151 | if (ioctl(dfd, LOOP_SET_FD, ffd) == 0) { | 150 | if (ioctl(dfd, LOOP_SET_FD, ffd) == 0) { |
152 | if (ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo) == 0) | 151 | memset(&loopinfo, 0, sizeof(loopinfo)); |
153 | rc = 0; | 152 | safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE); |
154 | else | 153 | loopinfo.lo_offset = offset; |
154 | /* | ||
155 | * Used by mount to set LO_FLAGS_AUTOCLEAR. | ||
156 | * LO_FLAGS_READ_ONLY is not set because RO is controlled by open type of the file. | ||
157 | * Note that closing LO_FLAGS_AUTOCLEARed dfd before mount | ||
158 | * is wrong (would free the loop device!) | ||
159 | */ | ||
160 | loopinfo.lo_flags = (flags & ~BB_LO_FLAGS_READ_ONLY); | ||
161 | rc = ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo); | ||
162 | if (rc != 0 && (loopinfo.lo_flags & BB_LO_FLAGS_AUTOCLEAR)) { | ||
163 | /* Old kernel, does not support LO_FLAGS_AUTOCLEAR? */ | ||
164 | /* (this code path is not tested) */ | ||
165 | loopinfo.lo_flags -= BB_LO_FLAGS_AUTOCLEAR; | ||
166 | rc = ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo); | ||
167 | } | ||
168 | if (rc != 0) { | ||
155 | ioctl(dfd, LOOP_CLR_FD, 0); | 169 | ioctl(dfd, LOOP_CLR_FD, 0); |
170 | } | ||
156 | } | 171 | } |
157 | } else { | 172 | } else { |
158 | rc = -1; | 173 | rc = -1; |
159 | } | 174 | } |
160 | close(dfd); | 175 | if (rc != 0) { |
176 | close(dfd); | ||
177 | } | ||
161 | try_again: | 178 | try_again: |
162 | if (*device) break; | 179 | if (*device) break; |
163 | } | 180 | } |
@@ -165,7 +182,7 @@ int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offse | |||
165 | if (rc == 0) { | 182 | if (rc == 0) { |
166 | if (!*device) | 183 | if (!*device) |
167 | *device = xstrdup(dev); | 184 | *device = xstrdup(dev); |
168 | return (mode == O_RDONLY); /* 1:ro, 0:rw */ | 185 | return dfd; |
169 | } | 186 | } |
170 | return rc; | 187 | return rc; |
171 | } | 188 | } |