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