Merge pull request #16678 from poettering/loop-configure
[thirdparty/systemd.git] / src / shared / loop-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #if HAVE_VALGRIND_MEMCHECK_H
4 #include <valgrind/memcheck.h>
5 #endif
6
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <linux/blkpg.h>
10 #include <linux/fs.h>
11 #include <linux/loop.h>
12 #include <sys/file.h>
13 #include <sys/ioctl.h>
14 #include <unistd.h>
15
16 #include "alloc-util.h"
17 #include "blockdev-util.h"
18 #include "errno-util.h"
19 #include "fd-util.h"
20 #include "fileio.h"
21 #include "loop-util.h"
22 #include "missing_loop.h"
23 #include "parse-util.h"
24 #include "stat-util.h"
25 #include "stdio-util.h"
26 #include "string-util.h"
27
28 static void cleanup_clear_loop_close(int *fd) {
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;
72 }
73
74 return 0;
75
76 fail:
77 (void) ioctl(fd, LOOP_CLR_FD);
78 return r;
79 }
80
81 int loop_device_make(
82 int fd,
83 int open_flags,
84 uint64_t offset,
85 uint64_t size,
86 uint32_t loop_flags,
87 LoopDevice **ret) {
88
89 _cleanup_free_ char *loopdev = NULL;
90 struct loop_config config;
91 LoopDevice *d = NULL;
92 struct stat st;
93 int nr = -1, r;
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)) {
103 if (ioctl(fd, LOOP_GET_STATUS64, &config.info) >= 0) {
104 /* Oh! This is a loopback device? That's interesting! */
105
106 #if HAVE_VALGRIND_MEMCHECK_H
107 /* Valgrind currently doesn't know LOOP_GET_STATUS64. Remove this once it does */
108 VALGRIND_MAKE_MEM_DEFINED(&config.info, sizeof(config.info));
109 #endif
110 nr = config.info.lo_number;
111
112 if (asprintf(&loopdev, "/dev/loop%i", nr) < 0)
113 return -ENOMEM;
114 }
115
116 if (offset == 0 && IN_SET(size, 0, UINT64_MAX)) {
117 _cleanup_close_ int copy = -1;
118
119 /* If this is already a block device, store a copy of the fd as it is */
120
121 copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
122 if (copy < 0)
123 return -errno;
124
125 d = new(LoopDevice, 1);
126 if (!d)
127 return -ENOMEM;
128 *d = (LoopDevice) {
129 .fd = TAKE_FD(copy),
130 .nr = nr,
131 .node = TAKE_PTR(loopdev),
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;
142 }
143
144 _cleanup_close_ int control = -1;
145 _cleanup_(cleanup_clear_loop_close) int loop_with_fd = -1;
146
147 control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
148 if (control < 0)
149 return -errno;
150
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
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. */
163 for (unsigned n_attempts = 0;;) {
164 _cleanup_close_ int loop = -1;
165
166 nr = ioctl(control, LOOP_CTL_GET_FREE);
167 if (nr < 0)
168 return -errno;
169
170 if (asprintf(&loopdev, "/dev/loop%i", nr) < 0)
171 return -ENOMEM;
172
173 loop = open(loopdev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|open_flags);
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 {
180 r = loop_configure(loop, &config);
181 if (r >= 0) {
182 loop_with_fd = TAKE_FD(loop);
183 break;
184 }
185 if (r != -EBUSY)
186 return r;
187 }
188
189 if (++n_attempts >= 64) /* Give up eventually */
190 return -EBUSY;
191
192 loopdev = mfree(loopdev);
193 }
194
195 d = new(LoopDevice, 1);
196 if (!d)
197 return -ENOMEM;
198 *d = (LoopDevice) {
199 .fd = TAKE_FD(loop_with_fd),
200 .node = TAKE_PTR(loopdev),
201 .nr = nr,
202 };
203
204 *ret = d;
205 return 0;
206 }
207
208 int loop_device_make_by_path(const char *path, int open_flags, uint32_t loop_flags, LoopDevice **ret) {
209 _cleanup_close_ int fd = -1;
210 int r;
211
212 assert(path);
213 assert(ret);
214 assert(open_flags < 0 || IN_SET(open_flags, O_RDWR, O_RDONLY));
215
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;
234
235 return loop_device_make(fd, open_flags, 0, 0, loop_flags, ret);
236 }
237
238 LoopDevice* loop_device_unref(LoopDevice *d) {
239 if (!d)
240 return NULL;
241
242 if (d->fd >= 0) {
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
247 if (d->nr >= 0 && !d->relinquished) {
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
256 if (d->nr >= 0 && !d->relinquished) {
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)
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 }
272 (void) usleep(50 * USEC_PER_MSEC);
273 }
274 }
275
276 free(d->node);
277 return mfree(d);
278 }
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 }
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;
292 struct loop_info64 info;
293 struct stat st;
294 LoopDevice *d;
295 int nr;
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;
306 if (!S_ISBLK(st.st_mode))
307 return -ENOTBLK;
308
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
314 nr = info.lo_number;
315 } else
316 nr = -1;
317
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),
328 .nr = nr,
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
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, &current_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, &current_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
424 int loop_device_refresh_size(LoopDevice *d, uint64_t offset, uint64_t size) {
425 struct loop_info64 info;
426 assert(d);
427
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
434 if (d->fd < 0)
435 return -EBADF;
436
437 if (d->nr < 0) /* not a loopback device */
438 return resize_partition(d->fd, offset, size);
439
440 if (ioctl(d->fd, LOOP_GET_STATUS64, &info) < 0)
441 return -errno;
442
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
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)
459 return -errno;
460
461 return 0;
462 }
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 }