]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
8c1be37e | 2 | |
10c1b188 LP |
3 | #if HAVE_VALGRIND_MEMCHECK_H |
4 | #include <valgrind/memcheck.h> | |
5 | #endif | |
6 | ||
dccca82b | 7 | #include <errno.h> |
8c1be37e | 8 | #include <fcntl.h> |
f1443709 LP |
9 | #include <linux/blkpg.h> |
10 | #include <linux/fs.h> | |
8c1be37e | 11 | #include <linux/loop.h> |
441ec804 | 12 | #include <sys/file.h> |
8c1be37e | 13 | #include <sys/ioctl.h> |
f2d9213f | 14 | #include <unistd.h> |
8c1be37e LP |
15 | |
16 | #include "alloc-util.h" | |
b0a94268 | 17 | #include "errno-util.h" |
8c1be37e | 18 | #include "fd-util.h" |
f1443709 | 19 | #include "fileio.h" |
8c1be37e | 20 | #include "loop-util.h" |
f1443709 | 21 | #include "parse-util.h" |
3cc44114 | 22 | #include "stat-util.h" |
f1443709 | 23 | #include "stdio-util.h" |
f2d9213f | 24 | #include "string-util.h" |
8c1be37e | 25 | |
e8af3bfd ZJS |
26 | static void cleanup_clear_loop_close(int *fd) { |
27 | if (*fd >= 0) { | |
28 | (void) ioctl(*fd, LOOP_CLR_FD); | |
29 | (void) safe_close(*fd); | |
30 | } | |
31 | } | |
32 | ||
1b49e3e3 | 33 | int loop_device_make( |
ed9eeb7b LP |
34 | int fd, |
35 | int open_flags, | |
36 | uint64_t offset, | |
37 | uint64_t size, | |
38 | uint32_t loop_flags, | |
39 | LoopDevice **ret) { | |
8c1be37e | 40 | |
8c1be37e | 41 | _cleanup_free_ char *loopdev = NULL; |
ed9eeb7b | 42 | struct loop_info64 info; |
50d04699 | 43 | LoopDevice *d = NULL; |
8c1be37e | 44 | struct stat st; |
b26c39ad | 45 | int nr = -1, r; |
8c1be37e LP |
46 | |
47 | assert(fd >= 0); | |
48 | assert(ret); | |
49 | assert(IN_SET(open_flags, O_RDWR, O_RDONLY)); | |
50 | ||
51 | if (fstat(fd, &st) < 0) | |
52 | return -errno; | |
53 | ||
54 | if (S_ISBLK(st.st_mode)) { | |
1163a2e9 | 55 | if (ioctl(fd, LOOP_GET_STATUS64, &info) >= 0) { |
b26c39ad | 56 | /* Oh! This is a loopback device? That's interesting! */ |
10c1b188 LP |
57 | |
58 | #if HAVE_VALGRIND_MEMCHECK_H | |
59 | /* Valgrind currently doesn't know LOOP_GET_STATUS64. Remove this once it does */ | |
60 | VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info)); | |
61 | #endif | |
b26c39ad LP |
62 | nr = info.lo_number; |
63 | ||
64 | if (asprintf(&loopdev, "/dev/loop%i", nr) < 0) | |
65 | return -ENOMEM; | |
66 | } | |
67 | ||
ed9eeb7b | 68 | if (offset == 0 && IN_SET(size, 0, UINT64_MAX)) { |
ba5450f4 | 69 | _cleanup_close_ int copy = -1; |
8c1be37e | 70 | |
ed9eeb7b | 71 | /* If this is already a block device, store a copy of the fd as it is */ |
8c1be37e | 72 | |
ed9eeb7b LP |
73 | copy = fcntl(fd, F_DUPFD_CLOEXEC, 3); |
74 | if (copy < 0) | |
75 | return -errno; | |
8c1be37e | 76 | |
ed9eeb7b LP |
77 | d = new(LoopDevice, 1); |
78 | if (!d) | |
79 | return -ENOMEM; | |
ed9eeb7b | 80 | *d = (LoopDevice) { |
ba5450f4 | 81 | .fd = TAKE_FD(copy), |
b26c39ad LP |
82 | .nr = nr, |
83 | .node = TAKE_PTR(loopdev), | |
ed9eeb7b LP |
84 | .relinquished = true, /* It's not allocated by us, don't destroy it when this object is freed */ |
85 | }; | |
86 | ||
87 | *ret = d; | |
88 | return d->fd; | |
89 | } | |
90 | } else { | |
91 | r = stat_verify_regular(&st); | |
92 | if (r < 0) | |
93 | return r; | |
8c1be37e LP |
94 | } |
95 | ||
e8af3bfd ZJS |
96 | _cleanup_close_ int control = -1; |
97 | _cleanup_(cleanup_clear_loop_close) int loop_with_fd = -1; | |
98 | ||
8c1be37e LP |
99 | control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); |
100 | if (control < 0) | |
101 | return -errno; | |
102 | ||
0f6519d4 LP |
103 | /* Loop around LOOP_CTL_GET_FREE, since at the moment we attempt to open the returned device it might |
104 | * be gone already, taken by somebody else racing against us. */ | |
e8af3bfd ZJS |
105 | for (unsigned n_attempts = 0;;) { |
106 | _cleanup_close_ int loop = -1; | |
107 | ||
0f6519d4 LP |
108 | nr = ioctl(control, LOOP_CTL_GET_FREE); |
109 | if (nr < 0) | |
110 | return -errno; | |
8c1be37e | 111 | |
0f6519d4 LP |
112 | if (asprintf(&loopdev, "/dev/loop%i", nr) < 0) |
113 | return -ENOMEM; | |
8c1be37e | 114 | |
0f6519d4 | 115 | loop = open(loopdev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|open_flags); |
01813148 ZJS |
116 | if (loop < 0) { |
117 | /* Somebody might've gotten the same number from the kernel, used the device, | |
118 | * and called LOOP_CTL_REMOVE on it. Let's retry with a new number. */ | |
119 | if (errno != ENOENT) | |
120 | return -errno; | |
121 | } else { | |
122 | if (ioctl(loop, LOOP_SET_FD, fd) >= 0) { | |
123 | loop_with_fd = TAKE_FD(loop); | |
124 | break; | |
125 | } | |
126 | if (errno != EBUSY) | |
127 | return -errno; | |
e8af3bfd | 128 | } |
01813148 | 129 | |
e8af3bfd ZJS |
130 | if (++n_attempts >= 64) /* Give up eventually */ |
131 | return -EBUSY; | |
0f6519d4 LP |
132 | |
133 | loopdev = mfree(loopdev); | |
0f6519d4 | 134 | } |
8c1be37e | 135 | |
ed9eeb7b LP |
136 | info = (struct loop_info64) { |
137 | /* Use the specified flags, but configure the read-only flag from the open flags, and force autoclear */ | |
138 | .lo_flags = (loop_flags & ~LO_FLAGS_READ_ONLY) | ((loop_flags & O_ACCMODE) == O_RDONLY ? LO_FLAGS_READ_ONLY : 0) | LO_FLAGS_AUTOCLEAR, | |
139 | .lo_offset = offset, | |
140 | .lo_sizelimit = size == UINT64_MAX ? 0 : size, | |
141 | }; | |
142 | ||
e8af3bfd ZJS |
143 | if (ioctl(loop_with_fd, LOOP_SET_STATUS64, &info) < 0) |
144 | return -errno; | |
8c1be37e LP |
145 | |
146 | d = new(LoopDevice, 1); | |
e8af3bfd ZJS |
147 | if (!d) |
148 | return -ENOMEM; | |
8c1be37e | 149 | *d = (LoopDevice) { |
e8af3bfd | 150 | .fd = TAKE_FD(loop_with_fd), |
1cc6c93a | 151 | .node = TAKE_PTR(loopdev), |
8c1be37e LP |
152 | .nr = nr, |
153 | }; | |
154 | ||
8c1be37e | 155 | *ret = d; |
e8af3bfd | 156 | return 0; |
8c1be37e LP |
157 | } |
158 | ||
e08f94ac | 159 | int loop_device_make_by_path(const char *path, int open_flags, uint32_t loop_flags, LoopDevice **ret) { |
8c1be37e | 160 | _cleanup_close_ int fd = -1; |
b0a94268 | 161 | int r; |
8c1be37e LP |
162 | |
163 | assert(path); | |
164 | assert(ret); | |
b0a94268 | 165 | assert(open_flags < 0 || IN_SET(open_flags, O_RDWR, O_RDONLY)); |
8c1be37e | 166 | |
b0a94268 LP |
167 | /* Passing < 0 as open_flags here means we'll try to open the device writable if we can, retrying |
168 | * read-only if we cannot. */ | |
169 | ||
170 | fd = open(path, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|(open_flags >= 0 ? open_flags : O_RDWR)); | |
171 | if (fd < 0) { | |
172 | r = -errno; | |
173 | ||
174 | /* Retry read-only? */ | |
175 | if (open_flags >= 0 || !(ERRNO_IS_PRIVILEGE(r) || r == -EROFS)) | |
176 | return r; | |
177 | ||
178 | fd = open(path, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_RDONLY); | |
179 | if (fd < 0) | |
180 | return r; /* Propagate original error */ | |
181 | ||
182 | open_flags = O_RDONLY; | |
183 | } else if (open_flags < 0) | |
184 | open_flags = O_RDWR; | |
8c1be37e | 185 | |
1b49e3e3 | 186 | return loop_device_make(fd, open_flags, 0, 0, loop_flags, ret); |
8c1be37e LP |
187 | } |
188 | ||
189 | LoopDevice* loop_device_unref(LoopDevice *d) { | |
190 | if (!d) | |
191 | return NULL; | |
192 | ||
193 | if (d->fd >= 0) { | |
cae1e8fb LP |
194 | /* Implicitly sync the device, since otherwise in-flight blocks might not get written */ |
195 | if (fsync(d->fd) < 0) | |
196 | log_debug_errno(errno, "Failed to sync loop block device, ignoring: %m"); | |
197 | ||
a2ea3b2f | 198 | if (d->nr >= 0 && !d->relinquished) { |
8c1be37e LP |
199 | if (ioctl(d->fd, LOOP_CLR_FD) < 0) |
200 | log_debug_errno(errno, "Failed to clear loop device: %m"); | |
201 | ||
202 | } | |
203 | ||
204 | safe_close(d->fd); | |
205 | } | |
206 | ||
a2ea3b2f | 207 | if (d->nr >= 0 && !d->relinquished) { |
8c1be37e LP |
208 | _cleanup_close_ int control = -1; |
209 | ||
210 | control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); | |
211 | if (control < 0) | |
f2d9213f ZJS |
212 | log_warning_errno(errno, |
213 | "Failed to open loop control device, cannot remove loop device %s: %m", | |
214 | strna(d->node)); | |
215 | else | |
216 | for (unsigned n_attempts = 0;;) { | |
217 | if (ioctl(control, LOOP_CTL_REMOVE, d->nr) >= 0) | |
218 | break; | |
219 | if (errno != EBUSY || ++n_attempts >= 64) { | |
220 | log_warning_errno(errno, "Failed to remove device %s: %m", strna(d->node)); | |
221 | break; | |
222 | } | |
cae1e8fb | 223 | (void) usleep(50 * USEC_PER_MSEC); |
f2d9213f | 224 | } |
8c1be37e LP |
225 | } |
226 | ||
227 | free(d->node); | |
5fecf46d | 228 | return mfree(d); |
8c1be37e | 229 | } |
a2ea3b2f LP |
230 | |
231 | void loop_device_relinquish(LoopDevice *d) { | |
232 | assert(d); | |
233 | ||
234 | /* Don't attempt to clean up the loop device anymore from this point on. Leave the clean-ing up to the kernel | |
235 | * itself, using the loop device "auto-clear" logic we already turned on when creating the device. */ | |
236 | ||
237 | d->relinquished = true; | |
238 | } | |
9dabc4fd LP |
239 | |
240 | int loop_device_open(const char *loop_path, int open_flags, LoopDevice **ret) { | |
241 | _cleanup_close_ int loop_fd = -1; | |
242 | _cleanup_free_ char *p = NULL; | |
b26c39ad | 243 | struct loop_info64 info; |
9dabc4fd LP |
244 | struct stat st; |
245 | LoopDevice *d; | |
b26c39ad | 246 | int nr; |
9dabc4fd LP |
247 | |
248 | assert(loop_path); | |
249 | assert(ret); | |
250 | ||
251 | loop_fd = open(loop_path, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|open_flags); | |
252 | if (loop_fd < 0) | |
253 | return -errno; | |
254 | ||
255 | if (fstat(loop_fd, &st) < 0) | |
256 | return -errno; | |
9dabc4fd LP |
257 | if (!S_ISBLK(st.st_mode)) |
258 | return -ENOTBLK; | |
259 | ||
10c1b188 LP |
260 | if (ioctl(loop_fd, LOOP_GET_STATUS64, &info) >= 0) { |
261 | #if HAVE_VALGRIND_MEMCHECK_H | |
262 | /* Valgrind currently doesn't know LOOP_GET_STATUS64. Remove this once it does */ | |
263 | VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info)); | |
264 | #endif | |
b26c39ad | 265 | nr = info.lo_number; |
10c1b188 | 266 | } else |
b26c39ad LP |
267 | nr = -1; |
268 | ||
9dabc4fd LP |
269 | p = strdup(loop_path); |
270 | if (!p) | |
271 | return -ENOMEM; | |
272 | ||
273 | d = new(LoopDevice, 1); | |
274 | if (!d) | |
275 | return -ENOMEM; | |
276 | ||
277 | *d = (LoopDevice) { | |
278 | .fd = TAKE_FD(loop_fd), | |
b26c39ad | 279 | .nr = nr, |
9dabc4fd LP |
280 | .node = TAKE_PTR(p), |
281 | .relinquished = true, /* It's not ours, don't try to destroy it when this object is freed */ | |
282 | }; | |
283 | ||
284 | *ret = d; | |
285 | return d->fd; | |
286 | } | |
287 | ||
f1443709 LP |
288 | static int resize_partition(int partition_fd, uint64_t offset, uint64_t size) { |
289 | char sysfs[STRLEN("/sys/dev/block/:/partition") + 2*DECIMAL_STR_MAX(dev_t) + 1]; | |
290 | _cleanup_free_ char *whole = NULL, *buffer = NULL; | |
291 | uint64_t current_offset, current_size, partno; | |
292 | _cleanup_close_ int whole_fd = -1; | |
293 | struct stat st; | |
294 | dev_t devno; | |
295 | int r; | |
296 | ||
297 | assert(partition_fd >= 0); | |
298 | ||
299 | /* Resizes the partition the loopback device refer to (assuming it refers to one instead of an actual | |
300 | * loopback device), and changes the offset, if needed. This is a fancy wrapper around | |
301 | * BLKPG_RESIZE_PARTITION. */ | |
302 | ||
303 | if (fstat(partition_fd, &st) < 0) | |
304 | return -errno; | |
305 | ||
306 | assert(S_ISBLK(st.st_mode)); | |
307 | ||
308 | xsprintf(sysfs, "/sys/dev/block/%u:%u/partition", major(st.st_rdev), minor(st.st_rdev)); | |
309 | r = read_one_line_file(sysfs, &buffer); | |
310 | if (r == -ENOENT) /* not a partition, cannot resize */ | |
311 | return -ENOTTY; | |
312 | if (r < 0) | |
313 | return r; | |
314 | r = safe_atou64(buffer, &partno); | |
315 | if (r < 0) | |
316 | return r; | |
317 | ||
318 | xsprintf(sysfs, "/sys/dev/block/%u:%u/start", major(st.st_rdev), minor(st.st_rdev)); | |
319 | ||
320 | buffer = mfree(buffer); | |
321 | r = read_one_line_file(sysfs, &buffer); | |
322 | if (r < 0) | |
323 | return r; | |
324 | r = safe_atou64(buffer, ¤t_offset); | |
325 | if (r < 0) | |
326 | return r; | |
327 | if (current_offset > UINT64_MAX/512U) | |
328 | return -EINVAL; | |
329 | current_offset *= 512U; | |
330 | ||
331 | if (ioctl(partition_fd, BLKGETSIZE64, ¤t_size) < 0) | |
332 | return -EINVAL; | |
333 | ||
334 | if (size == UINT64_MAX && offset == UINT64_MAX) | |
335 | return 0; | |
336 | if (current_size == size && current_offset == offset) | |
337 | return 0; | |
338 | ||
339 | xsprintf(sysfs, "/sys/dev/block/%u:%u/../dev", major(st.st_rdev), minor(st.st_rdev)); | |
340 | ||
341 | buffer = mfree(buffer); | |
342 | r = read_one_line_file(sysfs, &buffer); | |
343 | if (r < 0) | |
344 | return r; | |
345 | r = parse_dev(buffer, &devno); | |
346 | if (r < 0) | |
347 | return r; | |
348 | ||
349 | r = device_path_make_major_minor(S_IFBLK, devno, &whole); | |
350 | if (r < 0) | |
351 | return r; | |
352 | ||
353 | whole_fd = open(whole, O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); | |
354 | if (whole_fd < 0) | |
355 | return -errno; | |
356 | ||
357 | struct blkpg_partition bp = { | |
358 | .pno = partno, | |
359 | .start = offset == UINT64_MAX ? current_offset : offset, | |
360 | .length = size == UINT64_MAX ? current_size : size, | |
361 | }; | |
362 | ||
363 | struct blkpg_ioctl_arg ba = { | |
364 | .op = BLKPG_RESIZE_PARTITION, | |
365 | .data = &bp, | |
366 | .datalen = sizeof(bp), | |
367 | }; | |
368 | ||
369 | if (ioctl(whole_fd, BLKPG, &ba) < 0) | |
370 | return -errno; | |
371 | ||
372 | return 0; | |
373 | } | |
374 | ||
c37878fc LP |
375 | int loop_device_refresh_size(LoopDevice *d, uint64_t offset, uint64_t size) { |
376 | struct loop_info64 info; | |
9dabc4fd LP |
377 | assert(d); |
378 | ||
f1443709 LP |
379 | /* Changes the offset/start of the loop device relative to the beginning of the underlying file or |
380 | * block device. If this loop device actually refers to a partition and not a loopback device, we'll | |
381 | * try to adjust the partition offsets instead. | |
382 | * | |
383 | * If either offset or size is UINT64_MAX we won't change that parameter. */ | |
384 | ||
9dabc4fd LP |
385 | if (d->fd < 0) |
386 | return -EBADF; | |
387 | ||
f1443709 LP |
388 | if (d->nr < 0) /* not a loopback device */ |
389 | return resize_partition(d->fd, offset, size); | |
390 | ||
c37878fc LP |
391 | if (ioctl(d->fd, LOOP_GET_STATUS64, &info) < 0) |
392 | return -errno; | |
393 | ||
10c1b188 LP |
394 | #if HAVE_VALGRIND_MEMCHECK_H |
395 | /* Valgrind currently doesn't know LOOP_GET_STATUS64. Remove this once it does */ | |
396 | VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info)); | |
397 | #endif | |
398 | ||
c37878fc LP |
399 | if (size == UINT64_MAX && offset == UINT64_MAX) |
400 | return 0; | |
401 | if (info.lo_sizelimit == size && info.lo_offset == offset) | |
402 | return 0; | |
403 | ||
404 | if (size != UINT64_MAX) | |
405 | info.lo_sizelimit = size; | |
406 | if (offset != UINT64_MAX) | |
407 | info.lo_offset = offset; | |
408 | ||
409 | if (ioctl(d->fd, LOOP_SET_STATUS64, &info) < 0) | |
9dabc4fd LP |
410 | return -errno; |
411 | ||
412 | return 0; | |
413 | } | |
441ec804 LP |
414 | |
415 | int loop_device_flock(LoopDevice *d, int operation) { | |
416 | assert(d); | |
417 | ||
418 | if (d->fd < 0) | |
419 | return -EBADF; | |
420 | ||
421 | if (flock(d->fd, operation) < 0) | |
422 | return -errno; | |
423 | ||
424 | return 0; | |
425 | } |