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