]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/loop-util.c
man: fix incorrectly placed full stop
[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 "errno-util.h"
18 #include "fd-util.h"
19 #include "fileio.h"
20 #include "loop-util.h"
21 #include "parse-util.h"
22 #include "stat-util.h"
23 #include "stdio-util.h"
24 #include "string-util.h"
25
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
33 int loop_device_make(
34 int fd,
35 int open_flags,
36 uint64_t offset,
37 uint64_t size,
38 uint32_t loop_flags,
39 LoopDevice **ret) {
40
41 _cleanup_free_ char *loopdev = NULL;
42 struct loop_info64 info;
43 LoopDevice *d = NULL;
44 struct stat st;
45 int nr = -1, r;
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)) {
55 if (ioctl(fd, LOOP_GET_STATUS64, &info) >= 0) {
56 /* Oh! This is a loopback device? That's interesting! */
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
62 nr = info.lo_number;
63
64 if (asprintf(&loopdev, "/dev/loop%i", nr) < 0)
65 return -ENOMEM;
66 }
67
68 if (offset == 0 && IN_SET(size, 0, UINT64_MAX)) {
69 _cleanup_close_ int copy = -1;
70
71 /* If this is already a block device, store a copy of the fd as it is */
72
73 copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
74 if (copy < 0)
75 return -errno;
76
77 d = new(LoopDevice, 1);
78 if (!d)
79 return -ENOMEM;
80 *d = (LoopDevice) {
81 .fd = TAKE_FD(copy),
82 .nr = nr,
83 .node = TAKE_PTR(loopdev),
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;
94 }
95
96 _cleanup_close_ int control = -1;
97 _cleanup_(cleanup_clear_loop_close) int loop_with_fd = -1;
98
99 control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
100 if (control < 0)
101 return -errno;
102
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. */
105 for (unsigned n_attempts = 0;;) {
106 _cleanup_close_ int loop = -1;
107
108 nr = ioctl(control, LOOP_CTL_GET_FREE);
109 if (nr < 0)
110 return -errno;
111
112 if (asprintf(&loopdev, "/dev/loop%i", nr) < 0)
113 return -ENOMEM;
114
115 loop = open(loopdev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|open_flags);
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;
128 }
129
130 if (++n_attempts >= 64) /* Give up eventually */
131 return -EBUSY;
132
133 loopdev = mfree(loopdev);
134 }
135
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
143 if (ioctl(loop_with_fd, LOOP_SET_STATUS64, &info) < 0)
144 return -errno;
145
146 d = new(LoopDevice, 1);
147 if (!d)
148 return -ENOMEM;
149 *d = (LoopDevice) {
150 .fd = TAKE_FD(loop_with_fd),
151 .node = TAKE_PTR(loopdev),
152 .nr = nr,
153 };
154
155 *ret = d;
156 return 0;
157 }
158
159 int loop_device_make_by_path(const char *path, int open_flags, uint32_t loop_flags, LoopDevice **ret) {
160 _cleanup_close_ int fd = -1;
161 int r;
162
163 assert(path);
164 assert(ret);
165 assert(open_flags < 0 || IN_SET(open_flags, O_RDWR, O_RDONLY));
166
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;
185
186 return loop_device_make(fd, open_flags, 0, 0, loop_flags, ret);
187 }
188
189 LoopDevice* loop_device_unref(LoopDevice *d) {
190 if (!d)
191 return NULL;
192
193 if (d->fd >= 0) {
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
198 if (d->nr >= 0 && !d->relinquished) {
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
207 if (d->nr >= 0 && !d->relinquished) {
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)
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 }
223 (void) usleep(50 * USEC_PER_MSEC);
224 }
225 }
226
227 free(d->node);
228 return mfree(d);
229 }
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 }
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;
243 struct loop_info64 info;
244 struct stat st;
245 LoopDevice *d;
246 int nr;
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;
257 if (!S_ISBLK(st.st_mode))
258 return -ENOTBLK;
259
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
265 nr = info.lo_number;
266 } else
267 nr = -1;
268
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),
279 .nr = nr,
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
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, &current_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, &current_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
375 int loop_device_refresh_size(LoopDevice *d, uint64_t offset, uint64_t size) {
376 struct loop_info64 info;
377 assert(d);
378
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
385 if (d->fd < 0)
386 return -EBADF;
387
388 if (d->nr < 0) /* not a loopback device */
389 return resize_partition(d->fd, offset, size);
390
391 if (ioctl(d->fd, LOOP_GET_STATUS64, &info) < 0)
392 return -errno;
393
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
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)
410 return -errno;
411
412 return 0;
413 }
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 }