| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#if HAVE_VALGRIND_MEMCHECK_H |
| 4 |
#include <valgrind/memcheck.h> |
| 5 |
#endif |
| 6 |
|
| 7 |
#include <fcntl.h> |
| 8 |
#include <linux/loop.h> |
| 9 |
#include <sys/file.h> |
| 10 |
#include <sys/ioctl.h> |
| 11 |
#include <unistd.h> |
| 12 |
|
| 13 |
#include "sd-device.h" |
| 14 |
|
| 15 |
#include "alloc-util.h" |
| 16 |
#include "blockdev-util.h" |
| 17 |
#include "data-fd-util.h" |
| 18 |
#include "device-private.h" |
| 19 |
#include "device-util.h" |
| 20 |
#include "devnum-util.h" |
| 21 |
#include "dissect-image.h" |
| 22 |
#include "env-util.h" |
| 23 |
#include "errno-util.h" |
| 24 |
#include "fd-util.h" |
| 25 |
#include "fileio.h" |
| 26 |
#include "fs-util.h" |
| 27 |
#include "loop-util.h" |
| 28 |
#include "parse-util.h" |
| 29 |
#include "path-util.h" |
| 30 |
#include "random-util.h" |
| 31 |
#include "stat-util.h" |
| 32 |
#include "stdio-util.h" |
| 33 |
#include "string-util.h" |
| 34 |
#include "time-util.h" |
| 35 |
|
| 36 |
static void cleanup_clear_loop_close(int *fd) { |
| 37 |
assert(fd); |
| 38 |
|
| 39 |
if (*fd < 0) |
| 40 |
return; |
| 41 |
|
| 42 |
(void) ioctl(*fd, LOOP_CLR_FD); |
| 43 |
(void) safe_close(*fd); |
| 44 |
} |
| 45 |
|
| 46 |
static int loop_is_bound(int fd) { |
| 47 |
struct loop_info64 info; |
| 48 |
|
| 49 |
if (ioctl(ASSERT_FD(fd), LOOP_GET_STATUS64, &info) < 0) { |
| 50 |
if (errno == ENXIO) |
| 51 |
return false; /* not bound! */ |
| 52 |
|
| 53 |
return -errno; |
| 54 |
} |
| 55 |
|
| 56 |
return true; /* bound! */ |
| 57 |
} |
| 58 |
|
| 59 |
static int open_lock_fd(int primary_fd, int operation) { |
| 60 |
_cleanup_close_ int lock_fd = -EBADF; |
| 61 |
|
| 62 |
assert(IN_SET(operation & ~LOCK_NB, LOCK_SH, LOCK_EX)); |
| 63 |
|
| 64 |
lock_fd = fd_reopen(ASSERT_FD(primary_fd), O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); |
| 65 |
if (lock_fd < 0) |
| 66 |
return lock_fd; |
| 67 |
|
| 68 |
if (flock(lock_fd, operation) < 0) |
| 69 |
return -errno; |
| 70 |
|
| 71 |
return TAKE_FD(lock_fd); |
| 72 |
} |
| 73 |
|
| 74 |
static int loop_configure_verify_direct_io(int fd, const struct loop_config *c) { |
| 75 |
assert(fd >= 0); |
| 76 |
assert(c); |
| 77 |
|
| 78 |
if (FLAGS_SET(c->info.lo_flags, LO_FLAGS_DIRECT_IO)) { |
| 79 |
struct loop_info64 info; |
| 80 |
|
| 81 |
if (ioctl(fd, LOOP_GET_STATUS64, &info) < 0) |
| 82 |
return log_debug_errno(errno, "Failed to issue LOOP_GET_STATUS64: %m"); |
| 83 |
|
| 84 |
#if HAVE_VALGRIND_MEMCHECK_H |
| 85 |
VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info)); |
| 86 |
#endif |
| 87 |
|
| 88 |
/* On older kernels (<= 5.3) it was necessary to set the block size of the loopback block |
| 89 |
* device to the logical block size of the underlying file system. Since there was no nice |
| 90 |
* way to query the value, we are not bothering to do this however. On newer kernels the |
| 91 |
* block size is propagated automatically and does not require intervention from us. We'll |
| 92 |
* check here if enabling direct IO worked, to make this easily debuggable however. |
| 93 |
* |
| 94 |
* (Should anyone really care and actually wants direct IO on old kernels: it might be worth |
| 95 |
* enabling direct IO with iteratively larger block sizes until it eventually works.) |
| 96 |
* |
| 97 |
* On older kernels (e.g.: 5.10) when this is attempted on a file stored on a dm-crypt |
| 98 |
* backed partition the kernel will start returning I/O errors when accessing the mounted |
| 99 |
* loop device, so return a recognizable error that causes the operation to be started |
| 100 |
* from scratch without the LO_FLAGS_DIRECT_IO flag. */ |
| 101 |
if (!FLAGS_SET(info.lo_flags, LO_FLAGS_DIRECT_IO)) |
| 102 |
return log_debug_errno( |
| 103 |
SYNTHETIC_ERRNO(ENOANO), |
| 104 |
"Could not enable direct IO mode, retrying in buffered IO mode."); |
| 105 |
} |
| 106 |
|
| 107 |
return 0; |
| 108 |
} |
| 109 |
|
| 110 |
static int loop_configure_verify(int fd, const struct loop_config *c) { |
| 111 |
bool broken = false; |
| 112 |
int r; |
| 113 |
|
| 114 |
assert(fd >= 0); |
| 115 |
assert(c); |
| 116 |
|
| 117 |
if (c->block_size != 0) { |
| 118 |
uint32_t ssz; |
| 119 |
|
| 120 |
r = blockdev_get_sector_size(fd, &ssz); |
| 121 |
if (r < 0) |
| 122 |
return r; |
| 123 |
|
| 124 |
if (ssz != c->block_size) { |
| 125 |
log_debug("LOOP_CONFIGURE didn't honour requested block size %" PRIu32 ", got %" PRIu32 " instead. Ignoring.", c->block_size, ssz); |
| 126 |
broken = true; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
if (c->info.lo_sizelimit != 0) { |
| 131 |
/* Kernel 5.8 vanilla doesn't properly propagate the size limit into the |
| 132 |
* block device. If it's used, let's immediately check if it had the desired |
| 133 |
* effect hence. And if not use classic LOOP_SET_STATUS64. */ |
| 134 |
uint64_t z; |
| 135 |
|
| 136 |
r = blockdev_get_device_size(fd, &z); |
| 137 |
if (r < 0) |
| 138 |
return r; |
| 139 |
|
| 140 |
if (z != c->info.lo_sizelimit) { |
| 141 |
log_debug("LOOP_CONFIGURE is broken, doesn't honour .info.lo_sizelimit. Falling back to LOOP_SET_STATUS64."); |
| 142 |
broken = true; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if (FLAGS_SET(c->info.lo_flags, LO_FLAGS_PARTSCAN)) { |
| 147 |
/* Kernel 5.8 vanilla doesn't properly propagate the partition scanning flag |
| 148 |
* into the block device. Let's hence verify if things work correctly here |
| 149 |
* before returning. */ |
| 150 |
|
| 151 |
r = blockdev_partscan_enabled_fd(fd); |
| 152 |
if (r < 0) |
| 153 |
return r; |
| 154 |
if (r == 0) { |
| 155 |
log_debug("LOOP_CONFIGURE is broken, doesn't honour LO_FLAGS_PARTSCAN. Falling back to LOOP_SET_STATUS64."); |
| 156 |
broken = true; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
r = loop_configure_verify_direct_io(fd, c); |
| 161 |
if (r < 0) |
| 162 |
return r; |
| 163 |
|
| 164 |
return !broken; |
| 165 |
} |
| 166 |
|
| 167 |
static int loop_configure_fallback(int fd, const struct loop_config *c) { |
| 168 |
struct loop_info64 info_copy; |
| 169 |
int r; |
| 170 |
|
| 171 |
assert(fd >= 0); |
| 172 |
assert(c); |
| 173 |
|
| 174 |
/* Only some of the flags LOOP_CONFIGURE can set are also settable via LOOP_SET_STATUS64, hence mask |
| 175 |
* them out. */ |
| 176 |
info_copy = c->info; |
| 177 |
info_copy.lo_flags &= LOOP_SET_STATUS_SETTABLE_FLAGS; |
| 178 |
|
| 179 |
/* Since kernel commit 5db470e229e22b7eda6e23b5566e532c96fb5bc3 (kernel v5.0) the LOOP_SET_STATUS64 |
| 180 |
* ioctl can return EAGAIN in case we change the info.lo_offset field, if someone else is accessing the |
| 181 |
* block device while we try to reconfigure it. This is a pretty common case, since udev might |
| 182 |
* instantly start probing the device as soon as we attach an fd to it. Hence handle it in two ways: |
| 183 |
* first, let's take the BSD lock to ensure that udev will not step in between the point in |
| 184 |
* time where we attach the fd and where we reconfigure the device. Secondly, let's wait 50ms on |
| 185 |
* EAGAIN and retry. The former should be an efficient mechanism to avoid we have to wait 50ms |
| 186 |
* needlessly if we are just racing against udev. The latter is protection against all other cases, |
| 187 |
* i.e. peers that do not take the BSD lock. */ |
| 188 |
|
| 189 |
for (unsigned n_attempts = 0;;) { |
| 190 |
if (ioctl(fd, LOOP_SET_STATUS64, &info_copy) >= 0) |
| 191 |
break; |
| 192 |
|
| 193 |
if (errno != EAGAIN || ++n_attempts >= 64) |
| 194 |
return log_debug_errno(errno, "Failed to configure loopback block device: %m"); |
| 195 |
|
| 196 |
/* Sleep some random time, but at least 10ms, at most 250ms. Increase the delay the more |
| 197 |
* failed attempts we see */ |
| 198 |
(void) usleep_safe(UINT64_C(10) * USEC_PER_MSEC + |
| 199 |
random_u64_range(UINT64_C(240) * USEC_PER_MSEC * n_attempts/64)); |
| 200 |
} |
| 201 |
|
| 202 |
/* If a block size is requested then try to configure it. If that doesn't work, ignore errors, but |
| 203 |
* afterwards, let's validate what is in effect, and if it doesn't match what we want, fail */ |
| 204 |
if (c->block_size != 0) { |
| 205 |
uint32_t ssz; |
| 206 |
|
| 207 |
if (ioctl(fd, LOOP_SET_BLOCK_SIZE, (unsigned long) c->block_size) < 0) |
| 208 |
log_debug_errno(errno, "Failed to set sector size, ignoring: %m"); |
| 209 |
|
| 210 |
r = blockdev_get_sector_size(fd, &ssz); |
| 211 |
if (r < 0) |
| 212 |
return log_debug_errno(r, "Failed to read sector size: %m"); |
| 213 |
if (ssz != c->block_size) |
| 214 |
return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Sector size of loopback device doesn't match what we requested, refusing."); |
| 215 |
} |
| 216 |
|
| 217 |
/* LO_FLAGS_DIRECT_IO is a flags we need to configure via explicit ioctls. */ |
| 218 |
if (FLAGS_SET(c->info.lo_flags, LO_FLAGS_DIRECT_IO)) |
| 219 |
if (ioctl(fd, LOOP_SET_DIRECT_IO, 1UL) < 0) |
| 220 |
log_debug_errno(errno, "Failed to enable direct IO mode, ignoring: %m"); |
| 221 |
|
| 222 |
return loop_configure_verify_direct_io(fd, c); |
| 223 |
} |
| 224 |
|
| 225 |
static int loop_configure( |
| 226 |
int nr, |
| 227 |
int open_flags, |
| 228 |
int lock_op, |
| 229 |
const struct loop_config *c, |
| 230 |
LoopDevice **ret) { |
| 231 |
|
| 232 |
static bool loop_configure_broken = false; |
| 233 |
|
| 234 |
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
| 235 |
_cleanup_(cleanup_clear_loop_close) int loop_with_fd = -EBADF; /* This must be declared before lock_fd. */ |
| 236 |
_cleanup_close_ int fd = -EBADF, lock_fd = -EBADF; |
| 237 |
_cleanup_free_ char *node = NULL; |
| 238 |
uint64_t diskseq = 0; |
| 239 |
dev_t devno; |
| 240 |
int r; |
| 241 |
|
| 242 |
assert(nr >= 0); |
| 243 |
assert(c); |
| 244 |
assert(ret); |
| 245 |
|
| 246 |
if (asprintf(&node, "/dev/loop%i", nr) < 0) |
| 247 |
return log_oom_debug(); |
| 248 |
|
| 249 |
r = sd_device_new_from_devname(&dev, node); |
| 250 |
if (r < 0) |
| 251 |
return log_debug_errno(r, "Failed to create sd_device object for \"%s\": %m", node); |
| 252 |
|
| 253 |
r = sd_device_get_devnum(dev, &devno); |
| 254 |
if (r < 0) |
| 255 |
return log_device_debug_errno(dev, r, "Failed to get devnum: %m"); |
| 256 |
|
| 257 |
fd = sd_device_open(dev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|open_flags); |
| 258 |
if (fd < 0) |
| 259 |
return log_device_debug_errno(dev, fd, "Failed to open device: %m"); |
| 260 |
|
| 261 |
/* Let's lock the device before we do anything. We take the BSD lock on a second, separately opened |
| 262 |
* fd for the device. udev after all watches for close() events (specifically IN_CLOSE_WRITE) on |
| 263 |
* block devices to reprobe them, hence by having a separate fd we will later close() we can ensure |
| 264 |
* we trigger udev after everything is done. If we'd lock our own fd instead and keep it open for a |
| 265 |
* long time udev would possibly never run on it again, even though the fd is unlocked, simply |
| 266 |
* because we never close() it. It also has the nice benefit we can use the _cleanup_close_ logic to |
| 267 |
* automatically release the lock, after we are done. */ |
| 268 |
lock_fd = open_lock_fd(fd, LOCK_EX); |
| 269 |
if (lock_fd < 0) |
| 270 |
return log_device_debug_errno(dev, lock_fd, "Failed to acquire lock: %m"); |
| 271 |
|
| 272 |
log_device_debug(dev, "Acquired exclusive lock."); |
| 273 |
|
| 274 |
/* Let's see if backing file is really unattached. Someone may already attach a backing file without |
| 275 |
* taking BSD lock. */ |
| 276 |
r = loop_is_bound(fd); |
| 277 |
if (r < 0) |
| 278 |
return log_device_debug_errno(dev, r, "Failed to check if the loopback block device is bound: %m"); |
| 279 |
if (r > 0) |
| 280 |
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EBUSY), |
| 281 |
"The loopback block device is already bound, ignoring."); |
| 282 |
|
| 283 |
/* Let's see if the device is really detached, i.e. currently has no associated partition block |
| 284 |
* devices. On various kernels (such as 5.8) it is possible to have a loopback block device that |
| 285 |
* superficially is detached but still has partition block devices associated for it. Let's then |
| 286 |
* manually remove the partitions via BLKPG, and tell the caller we did that via EUCLEAN, so they try |
| 287 |
* again. */ |
| 288 |
r = block_device_remove_all_partitions(dev, fd); |
| 289 |
if (r < 0) |
| 290 |
return log_device_debug_errno(dev, r, "Failed to remove partitions on the loopback block device: %m"); |
| 291 |
if (r > 0) |
| 292 |
/* Removed all partitions. Let's report this to the caller, to try again, and count this as |
| 293 |
* an attempt. */ |
| 294 |
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EUCLEAN), |
| 295 |
"Removed partitions on the loopback block device."); |
| 296 |
|
| 297 |
if (!loop_configure_broken) { |
| 298 |
if (ioctl(fd, LOOP_CONFIGURE, c) < 0) { |
| 299 |
/* Do fallback only if LOOP_CONFIGURE is not supported, propagate all other errors. */ |
| 300 |
if (!ERRNO_IS_IOCTL_NOT_SUPPORTED(errno)) |
| 301 |
return log_device_debug_errno(dev, errno, "ioctl(LOOP_CONFIGURE) failed: %m"); |
| 302 |
|
| 303 |
loop_configure_broken = true; |
| 304 |
} else { |
| 305 |
loop_with_fd = TAKE_FD(fd); |
| 306 |
|
| 307 |
r = loop_configure_verify(loop_with_fd, c); |
| 308 |
if (r < 0) |
| 309 |
return log_device_debug_errno(dev, r, "Failed to verify if loopback block device is correctly configured: %m"); |
| 310 |
if (r == 0) { |
| 311 |
/* LOOP_CONFIGURE doesn't work. Remember that. */ |
| 312 |
loop_configure_broken = true; |
| 313 |
|
| 314 |
/* We return EBUSY here instead of retrying immediately with LOOP_SET_FD, |
| 315 |
* because LOOP_CLR_FD is async: if the operation cannot be executed right |
| 316 |
* away it just sets the autoclear flag on the device. This means there's a |
| 317 |
* good chance we cannot actually reuse the loopback device right-away. Hence |
| 318 |
* let's assume it's busy, avoid the trouble and let the calling loop call us |
| 319 |
* again with a new, likely unused device. */ |
| 320 |
return -EBUSY; |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
if (loop_configure_broken) { |
| 326 |
if (ioctl(fd, LOOP_SET_FD, c->fd) < 0) |
| 327 |
return log_device_debug_errno(dev, errno, "ioctl(LOOP_SET_FD) failed: %m"); |
| 328 |
|
| 329 |
loop_with_fd = TAKE_FD(fd); |
| 330 |
|
| 331 |
r = loop_configure_fallback(loop_with_fd, c); |
| 332 |
if (r < 0) |
| 333 |
return r; |
| 334 |
} |
| 335 |
|
| 336 |
r = fd_get_diskseq(loop_with_fd, &diskseq); |
| 337 |
if (r < 0 && r != -EOPNOTSUPP) |
| 338 |
return log_device_debug_errno(dev, r, "Failed to get diskseq: %m"); |
| 339 |
|
| 340 |
switch (lock_op & ~LOCK_NB) { |
| 341 |
case LOCK_EX: /* Already in effect */ |
| 342 |
break; |
| 343 |
case LOCK_SH: /* Downgrade */ |
| 344 |
if (flock(lock_fd, lock_op) < 0) |
| 345 |
return log_device_debug_errno(dev, errno, "Failed to downgrade lock level: %m"); |
| 346 |
break; |
| 347 |
case LOCK_UN: /* Release */ |
| 348 |
lock_fd = safe_close(lock_fd); |
| 349 |
break; |
| 350 |
default: |
| 351 |
assert_not_reached(); |
| 352 |
} |
| 353 |
|
| 354 |
uint64_t device_size; |
| 355 |
r = blockdev_get_device_size(loop_with_fd, &device_size); |
| 356 |
if (r < 0) |
| 357 |
return log_device_debug_errno(dev, r, "Failed to get loopback device size: %m"); |
| 358 |
|
| 359 |
LoopDevice *d = new(LoopDevice, 1); |
| 360 |
if (!d) |
| 361 |
return log_oom_debug(); |
| 362 |
|
| 363 |
*d = (LoopDevice) { |
| 364 |
.n_ref = 1, |
| 365 |
.fd = TAKE_FD(loop_with_fd), |
| 366 |
.lock_fd = TAKE_FD(lock_fd), |
| 367 |
.node = TAKE_PTR(node), |
| 368 |
.nr = nr, |
| 369 |
.devno = devno, |
| 370 |
.dev = TAKE_PTR(dev), |
| 371 |
.diskseq = diskseq, |
| 372 |
.sector_size = c->block_size, |
| 373 |
.device_size = device_size, |
| 374 |
.created = true, |
| 375 |
}; |
| 376 |
|
| 377 |
*ret = TAKE_PTR(d); |
| 378 |
return 0; |
| 379 |
} |
| 380 |
|
| 381 |
static int fd_get_max_discard(int fd, uint64_t *ret) { |
| 382 |
char sysfs_path[STRLEN("/sys/dev/block/" ":" "/queue/discard_max_bytes") + DECIMAL_STR_MAX(dev_t) * 2 + 1]; |
| 383 |
_cleanup_free_ char *buffer = NULL; |
| 384 |
struct stat st; |
| 385 |
int r; |
| 386 |
|
| 387 |
assert(ret); |
| 388 |
|
| 389 |
if (fstat(ASSERT_FD(fd), &st) < 0) |
| 390 |
return -errno; |
| 391 |
|
| 392 |
r = stat_verify_block(&st); |
| 393 |
if (r < 0) |
| 394 |
return r; |
| 395 |
|
| 396 |
xsprintf(sysfs_path, "/sys/dev/block/" DEVNUM_FORMAT_STR "/queue/discard_max_bytes", DEVNUM_FORMAT_VAL(st.st_rdev)); |
| 397 |
|
| 398 |
r = read_one_line_file(sysfs_path, &buffer); |
| 399 |
if (r < 0) |
| 400 |
return r; |
| 401 |
|
| 402 |
return safe_atou64(buffer, ret); |
| 403 |
} |
| 404 |
|
| 405 |
static int fd_set_max_discard(int fd, uint64_t max_discard) { |
| 406 |
char sysfs_path[STRLEN("/sys/dev/block/" ":" "/queue/discard_max_bytes") + DECIMAL_STR_MAX(dev_t) * 2 + 1]; |
| 407 |
struct stat st; |
| 408 |
int r; |
| 409 |
|
| 410 |
if (fstat(ASSERT_FD(fd), &st) < 0) |
| 411 |
return -errno; |
| 412 |
|
| 413 |
r = stat_verify_block(&st); |
| 414 |
if (r < 0) |
| 415 |
return r; |
| 416 |
|
| 417 |
xsprintf(sysfs_path, "/sys/dev/block/" DEVNUM_FORMAT_STR "/queue/discard_max_bytes", DEVNUM_FORMAT_VAL(st.st_rdev)); |
| 418 |
|
| 419 |
return write_string_filef(sysfs_path, WRITE_STRING_FILE_DISABLE_BUFFER, "%" PRIu64, max_discard); |
| 420 |
} |
| 421 |
|
| 422 |
static int probe_sector_size_harder(int fd, uint32_t *ret) { |
| 423 |
_cleanup_close_ int non_direct_io_fd = -EBADF; |
| 424 |
int probe_fd, f_flags; |
| 425 |
|
| 426 |
assert(fd >= 0); |
| 427 |
assert(ret); |
| 428 |
|
| 429 |
/* Wraps probe_sector_size() but handles O_DIRECT: if the fd is opened with O_DIRECT there are |
| 430 |
* strict alignment requirements for reads, so we temporarily reopen it without O_DIRECT for the |
| 431 |
* probing logic. */ |
| 432 |
|
| 433 |
f_flags = fcntl(fd, F_GETFL); |
| 434 |
if (f_flags < 0) |
| 435 |
return -errno; |
| 436 |
|
| 437 |
if (FLAGS_SET(f_flags, O_DIRECT)) { |
| 438 |
non_direct_io_fd = fd_reopen(fd, O_RDONLY|O_CLOEXEC|O_NONBLOCK); |
| 439 |
if (non_direct_io_fd < 0) |
| 440 |
return non_direct_io_fd; |
| 441 |
|
| 442 |
probe_fd = non_direct_io_fd; |
| 443 |
} else |
| 444 |
probe_fd = fd; |
| 445 |
|
| 446 |
return probe_sector_size(probe_fd, ret); |
| 447 |
} |
| 448 |
|
| 449 |
static int loop_device_can_shortcut( |
| 450 |
int fd, |
| 451 |
uint64_t offset, |
| 452 |
uint64_t size, |
| 453 |
uint32_t sector_size, |
| 454 |
uint32_t device_ssz, |
| 455 |
uint32_t loop_flags) { |
| 456 |
|
| 457 |
int r; |
| 458 |
|
| 459 |
/* Returns whether we can hand back the original block device fd instead of allocating a real |
| 460 |
* loopback device for it: it must cover the whole device, the requested sector size must match the |
| 461 |
* device's sector size, and if partscan was requested it must already be enabled on the device |
| 462 |
* (otherwise e.g. partition block devices or loop devices created without LO_FLAGS_PARTSCAN would |
| 463 |
* be reused even though they cannot expose nested partitions). */ |
| 464 |
|
| 465 |
assert(fd >= 0); |
| 466 |
|
| 467 |
if (offset != 0) |
| 468 |
return false; |
| 469 |
if (!IN_SET(size, 0, UINT64_MAX)) |
| 470 |
return false; |
| 471 |
if (sector_size != device_ssz) |
| 472 |
return false; |
| 473 |
|
| 474 |
if (FLAGS_SET(loop_flags, LO_FLAGS_PARTSCAN)) { |
| 475 |
r = blockdev_partscan_enabled_fd(fd); |
| 476 |
if (r < 0) |
| 477 |
return r; |
| 478 |
if (r == 0) |
| 479 |
return false; |
| 480 |
} |
| 481 |
|
| 482 |
return true; |
| 483 |
} |
| 484 |
|
| 485 |
static int loop_device_make_internal( |
| 486 |
const char *path, |
| 487 |
int fd, |
| 488 |
int open_flags, |
| 489 |
uint64_t offset, |
| 490 |
uint64_t size, |
| 491 |
uint32_t sector_size, |
| 492 |
uint32_t loop_flags, |
| 493 |
int lock_op, |
| 494 |
LoopDevice **ret) { |
| 495 |
|
| 496 |
_cleanup_(loop_device_unrefp) LoopDevice *d = NULL; |
| 497 |
_cleanup_close_ int reopened_fd = -EBADF, control = -EBADF; |
| 498 |
_cleanup_free_ char *backing_file = NULL; |
| 499 |
struct loop_config config; |
| 500 |
int r, f_flags; |
| 501 |
struct stat st; |
| 502 |
|
| 503 |
assert(fd >= 0); |
| 504 |
assert(open_flags < 0 || IN_SET(open_flags, O_RDWR, O_RDONLY)); |
| 505 |
assert(ret); |
| 506 |
|
| 507 |
/* sector_size interpretation: |
| 508 |
* 0 → use device sector size for block devices, 512 for regular files |
| 509 |
* UINT32_MAX → probe GPT header to find the right sector size, fall back to 0 behavior |
| 510 |
* other → use the specified sector size explicitly */ |
| 511 |
|
| 512 |
f_flags = fcntl(fd, F_GETFL); |
| 513 |
if (f_flags < 0) |
| 514 |
return -errno; |
| 515 |
|
| 516 |
if (open_flags < 0) { |
| 517 |
/* If open_flags is unset, initialize it from the open fd */ |
| 518 |
if (FLAGS_SET(f_flags, O_PATH)) |
| 519 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADFD), "Access mode of image file indicates O_PATH, cannot determine read/write flags."); |
| 520 |
|
| 521 |
open_flags = f_flags & O_ACCMODE_STRICT; |
| 522 |
if (!IN_SET(open_flags, O_RDWR, O_RDONLY)) |
| 523 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADFD), "Access mode of image file is write only (?)"); |
| 524 |
} |
| 525 |
|
| 526 |
if (sector_size == UINT32_MAX) { |
| 527 |
/* If sector size is specified as UINT32_MAX, we'll try to probe the right sector size |
| 528 |
* by looking for the GPT partition header at various offsets. This of course only works |
| 529 |
* if the image already has a disk label. */ |
| 530 |
|
| 531 |
r = probe_sector_size_harder(fd, §or_size); |
| 532 |
if (r < 0) |
| 533 |
return r; |
| 534 |
if (r == 0) |
| 535 |
sector_size = 0; /* If we can't probe anything, use default sector size. */ |
| 536 |
} |
| 537 |
|
| 538 |
if (fstat(fd, &st) < 0) |
| 539 |
return -errno; |
| 540 |
|
| 541 |
if (S_ISBLK(st.st_mode)) { |
| 542 |
uint32_t device_ssz; |
| 543 |
r = blockdev_get_sector_size(fd, &device_ssz); |
| 544 |
if (r < 0) |
| 545 |
return r; |
| 546 |
|
| 547 |
if (sector_size == 0) |
| 548 |
sector_size = device_ssz; |
| 549 |
|
| 550 |
r = loop_device_can_shortcut(fd, offset, size, sector_size, device_ssz, loop_flags); |
| 551 |
if (r < 0) |
| 552 |
return r; |
| 553 |
if (r > 0) |
| 554 |
return loop_device_open_from_fd(fd, open_flags, lock_op, ret); |
| 555 |
} else { |
| 556 |
r = stat_verify_regular(&st); |
| 557 |
if (r < 0) |
| 558 |
return r; |
| 559 |
|
| 560 |
if (sector_size == 0) |
| 561 |
sector_size = 512; |
| 562 |
} |
| 563 |
|
| 564 |
if (path) { |
| 565 |
r = path_make_absolute_cwd(path, &backing_file); |
| 566 |
if (r < 0) |
| 567 |
return r; |
| 568 |
|
| 569 |
path_simplify(backing_file); |
| 570 |
} else { |
| 571 |
r = fd_get_path(fd, &backing_file); |
| 572 |
if (r < 0) |
| 573 |
return r; |
| 574 |
} |
| 575 |
|
| 576 |
if (FLAGS_SET(loop_flags, LO_FLAGS_DIRECT_IO) != FLAGS_SET(f_flags, O_DIRECT)) { |
| 577 |
/* If LO_FLAGS_DIRECT_IO is requested, then make sure we have the fd open with O_DIRECT, as |
| 578 |
* that's required. Conversely, if it's off require that O_DIRECT is off too (that's because |
| 579 |
* new kernels will implicitly enable LO_FLAGS_DIRECT_IO if O_DIRECT is set). |
| 580 |
* |
| 581 |
* Our intention here is that LO_FLAGS_DIRECT_IO is the primary knob, and O_DIRECT derived |
| 582 |
* from that automatically. */ |
| 583 |
|
| 584 |
reopened_fd = fd_reopen(fd, (FLAGS_SET(loop_flags, LO_FLAGS_DIRECT_IO) ? O_DIRECT : 0)|O_CLOEXEC|O_NONBLOCK|open_flags); |
| 585 |
if (reopened_fd < 0) { |
| 586 |
if (!FLAGS_SET(loop_flags, LO_FLAGS_DIRECT_IO)) |
| 587 |
return log_debug_errno(reopened_fd, "Failed to reopen file descriptor without O_DIRECT: %m"); |
| 588 |
|
| 589 |
/* Some file systems might not support O_DIRECT, let's gracefully continue without it then. */ |
| 590 |
log_debug_errno(reopened_fd, "Failed to enable O_DIRECT for backing file descriptor for loopback device. Continuing without."); |
| 591 |
loop_flags &= ~LO_FLAGS_DIRECT_IO; |
| 592 |
} else |
| 593 |
fd = reopened_fd; /* From now on, operate on our new O_DIRECT fd */ |
| 594 |
} |
| 595 |
|
| 596 |
control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); |
| 597 |
if (control < 0) |
| 598 |
return -errno; |
| 599 |
|
| 600 |
/* Strip LO_FLAGS_PARTSCAN from LOOP_CONFIGURE and enable it afterwards via |
| 601 |
* LOOP_SET_STATUS64 to work around a kernel race: LOOP_CONFIGURE sends a uevent with |
| 602 |
* GD_NEED_PART_SCAN set before calling loop_reread_partitions(). If udev opens the device in |
| 603 |
* response, blkdev_get_whole() triggers a first scan, then loop_reread_partitions() does a |
| 604 |
* second scan that briefly drops all partitions. By configuring without partscan, |
| 605 |
* GD_SUPPRESS_PART_SCAN stays set, making any concurrent open harmless. LOOP_SET_STATUS64 |
| 606 |
* doesn't call disk_force_media_change() so it doesn't set GD_NEED_PART_SCAN. |
| 607 |
* |
| 608 |
* See: https://lore.kernel.org/linux-block/[email protected]/T/#u |
| 609 |
* Drop this workaround once the kernel fix is widely available. */ |
| 610 |
bool deferred_partscan = FLAGS_SET(loop_flags, LO_FLAGS_PARTSCAN); |
| 611 |
|
| 612 |
config = (struct loop_config) { |
| 613 |
.fd = fd, |
| 614 |
.block_size = sector_size, |
| 615 |
.info = { |
| 616 |
/* Use the specified flags, but configure the read-only flag from the open flags, and force autoclear */ |
| 617 |
.lo_flags = ((loop_flags & ~(LO_FLAGS_READ_ONLY|LO_FLAGS_PARTSCAN)) | |
| 618 |
((open_flags & O_ACCMODE_STRICT) == O_RDONLY ? LO_FLAGS_READ_ONLY : 0) | |
| 619 |
LO_FLAGS_AUTOCLEAR), |
| 620 |
.lo_offset = offset, |
| 621 |
.lo_sizelimit = size == UINT64_MAX ? 0 : size, |
| 622 |
}, |
| 623 |
}; |
| 624 |
|
| 625 |
/* Loop around LOOP_CTL_GET_FREE, since at the moment we attempt to open the returned device it might |
| 626 |
* be gone already, taken by somebody else racing against us. */ |
| 627 |
for (unsigned n_attempts = 0;;) { |
| 628 |
usec_t usec; |
| 629 |
int nr; |
| 630 |
|
| 631 |
/* Let's take a lock on the control device first. On a busy system, where many programs |
| 632 |
* attempt to allocate a loopback device at the same time, we might otherwise keep looping |
| 633 |
* around relatively heavy operations: asking for a free loopback device, then opening it, |
| 634 |
* validating it, attaching something to it. Let's serialize this whole operation, to make |
| 635 |
* unnecessary busywork less likely. Note that this is just something we do to optimize our |
| 636 |
* own code (and whoever else decides to use LOCK_EX locks for this), taking this lock is not |
| 637 |
* necessary, it just means it's less likely we have to iterate through this loop again and |
| 638 |
* again if our own code races against our own code. |
| 639 |
* |
| 640 |
* Note: our lock protocol is to take the /dev/loop-control lock first, and the block device |
| 641 |
* lock second, if both are taken, and always in this order, to avoid ABBA locking issues. */ |
| 642 |
if (flock(control, LOCK_EX) < 0) |
| 643 |
return -errno; |
| 644 |
|
| 645 |
nr = ioctl(control, LOOP_CTL_GET_FREE); |
| 646 |
if (nr < 0) |
| 647 |
return -errno; |
| 648 |
|
| 649 |
r = loop_configure(nr, open_flags, lock_op, &config, &d); |
| 650 |
if (r >= 0) |
| 651 |
break; |
| 652 |
|
| 653 |
/* -ENODEV or friends: Somebody might've gotten the same number from the kernel, used the |
| 654 |
* device, and called LOOP_CTL_REMOVE on it. Let's retry with a new number. |
| 655 |
* -EBUSY: a file descriptor is already bound to the loopback block device. |
| 656 |
* -EUCLEAN: some left-over partition devices that were cleaned up. |
| 657 |
* -ENOANO: we tried to use LO_FLAGS_DIRECT_IO but the kernel rejected it. */ |
| 658 |
if (!ERRNO_IS_DEVICE_ABSENT(r) && !IN_SET(r, -EBUSY, -EUCLEAN, -ENOANO)) |
| 659 |
return r; |
| 660 |
|
| 661 |
/* OK, this didn't work, let's try again a bit later, but first release the lock on the |
| 662 |
* control device */ |
| 663 |
if (flock(control, LOCK_UN) < 0) |
| 664 |
return -errno; |
| 665 |
|
| 666 |
if (++n_attempts >= 64) /* Give up eventually */ |
| 667 |
return -EBUSY; |
| 668 |
|
| 669 |
/* If we failed to enable direct IO mode, let's retry without it. We restart the process as |
| 670 |
* on some combination of kernel version and storage filesystem, the kernel is very unhappy |
| 671 |
* about a failed DIRECT_IO enablement and throws I/O errors. */ |
| 672 |
if (r == -ENOANO && FLAGS_SET(config.info.lo_flags, LO_FLAGS_DIRECT_IO)) { |
| 673 |
config.info.lo_flags &= ~LO_FLAGS_DIRECT_IO; |
| 674 |
open_flags &= ~O_DIRECT; |
| 675 |
|
| 676 |
int non_direct_io_fd = fd_reopen(config.fd, O_CLOEXEC|O_NONBLOCK|open_flags); |
| 677 |
if (non_direct_io_fd < 0) |
| 678 |
return log_debug_errno( |
| 679 |
non_direct_io_fd, |
| 680 |
"Failed to reopen file descriptor without O_DIRECT: %m"); |
| 681 |
|
| 682 |
safe_close(reopened_fd); |
| 683 |
fd = config.fd = /* For cleanups */ reopened_fd = non_direct_io_fd; |
| 684 |
} |
| 685 |
|
| 686 |
/* Wait some random time, to make collision less likely. Let's pick a random time in the |
| 687 |
* range 0ms…250ms, linearly scaled by the number of failed attempts. */ |
| 688 |
usec = random_u64_range(UINT64_C(10) * USEC_PER_MSEC + |
| 689 |
UINT64_C(240) * USEC_PER_MSEC * n_attempts/64); |
| 690 |
log_debug("Trying again after %s.", FORMAT_TIMESPAN(usec, USEC_PER_MSEC)); |
| 691 |
(void) usleep_safe(usec); |
| 692 |
} |
| 693 |
|
| 694 |
if (S_ISBLK(st.st_mode)) { |
| 695 |
/* Propagate backing device's discard byte limit to our loopback block device. We do this in |
| 696 |
* order to avoid that (supposedly quick) discard requests on the loopback device get turned |
| 697 |
* into (likely slow) zero-out requests on backing devices that do not support discarding |
| 698 |
* natively, but do support zero-out. */ |
| 699 |
uint64_t discard_max_bytes; |
| 700 |
|
| 701 |
r = fd_get_max_discard(fd, &discard_max_bytes); |
| 702 |
if (r < 0) |
| 703 |
log_debug_errno(r, "Failed to read 'discard_max_bytes' of backing device, ignoring: %m"); |
| 704 |
else { |
| 705 |
r = fd_set_max_discard(d->fd, discard_max_bytes); |
| 706 |
if (r < 0) |
| 707 |
log_debug_errno(r, "Failed to write 'discard_max_bytes' of loop device, ignoring: %m"); |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
if (deferred_partscan) { |
| 712 |
/* Open+close to drain GD_NEED_PART_SCAN harmlessly (GD_SUPPRESS_PART_SCAN is still |
| 713 |
* set so no partitions appear). Then enable partscan via LOOP_SET_STATUS64. */ |
| 714 |
int tmp_fd = fd_reopen(d->fd, O_RDONLY|O_CLOEXEC|O_NONBLOCK); |
| 715 |
if (tmp_fd < 0) |
| 716 |
return log_debug_errno(tmp_fd, "Failed to reopen loop device to drain partscan flag: %m"); |
| 717 |
safe_close(tmp_fd); |
| 718 |
|
| 719 |
struct loop_info64 info; |
| 720 |
if (ioctl(d->fd, LOOP_GET_STATUS64, &info) < 0) |
| 721 |
return log_debug_errno(errno, "Failed to get loop device status: %m"); |
| 722 |
|
| 723 |
info.lo_flags |= LO_FLAGS_PARTSCAN; |
| 724 |
|
| 725 |
if (ioctl(d->fd, LOOP_SET_STATUS64, &info) < 0) |
| 726 |
return log_debug_errno(errno, "Failed to enable partscan on loop device: %m"); |
| 727 |
} |
| 728 |
|
| 729 |
d->backing_file = TAKE_PTR(backing_file); |
| 730 |
d->backing_inode = st.st_ino; |
| 731 |
d->backing_devno = st.st_dev; |
| 732 |
|
| 733 |
log_debug("Successfully acquired %s, devno=%u:%u, nr=%i, diskseq=%" PRIu64, |
| 734 |
d->node, |
| 735 |
major(d->devno), minor(d->devno), |
| 736 |
d->nr, |
| 737 |
d->diskseq); |
| 738 |
|
| 739 |
*ret = TAKE_PTR(d); |
| 740 |
return 0; |
| 741 |
} |
| 742 |
|
| 743 |
static uint32_t loop_flags_mangle(uint32_t loop_flags) { |
| 744 |
int r; |
| 745 |
|
| 746 |
r = getenv_bool("SYSTEMD_LOOP_DIRECT_IO"); |
| 747 |
if (r < 0 && r != -ENXIO) |
| 748 |
log_debug_errno(r, "Failed to parse $SYSTEMD_LOOP_DIRECT_IO, ignoring: %m"); |
| 749 |
|
| 750 |
return UPDATE_FLAG(loop_flags, LO_FLAGS_DIRECT_IO, r != 0); /* Turn on LO_FLAGS_DIRECT_IO by default, unless explicitly configured to off. */ |
| 751 |
} |
| 752 |
|
| 753 |
int loop_device_make( |
| 754 |
int fd, |
| 755 |
int open_flags, |
| 756 |
uint64_t offset, |
| 757 |
uint64_t size, |
| 758 |
uint32_t sector_size, |
| 759 |
uint32_t loop_flags, |
| 760 |
int lock_op, |
| 761 |
LoopDevice **ret) { |
| 762 |
|
| 763 |
assert(fd >= 0); |
| 764 |
assert(ret); |
| 765 |
|
| 766 |
return loop_device_make_internal( |
| 767 |
NULL, |
| 768 |
fd, |
| 769 |
open_flags, |
| 770 |
offset, |
| 771 |
size, |
| 772 |
sector_size, |
| 773 |
loop_flags_mangle(loop_flags), |
| 774 |
lock_op, |
| 775 |
ret); |
| 776 |
} |
| 777 |
|
| 778 |
int loop_device_make_by_path_at( |
| 779 |
int dir_fd, |
| 780 |
const char *path, |
| 781 |
int open_flags, |
| 782 |
uint32_t sector_size, |
| 783 |
uint32_t loop_flags, |
| 784 |
int lock_op, |
| 785 |
LoopDevice **ret) { |
| 786 |
|
| 787 |
int r, basic_flags, direct_flags, rdwr_flags; |
| 788 |
_cleanup_close_ int fd = -EBADF; |
| 789 |
bool direct = false; |
| 790 |
|
| 791 |
assert(dir_fd >= 0 || dir_fd == AT_FDCWD); |
| 792 |
assert(ret); |
| 793 |
assert(open_flags < 0 || IN_SET(open_flags, O_RDWR, O_RDONLY)); |
| 794 |
|
| 795 |
/* Passing < 0 as open_flags here means we'll try to open the device writable if we can, retrying |
| 796 |
* read-only if we cannot. */ |
| 797 |
|
| 798 |
loop_flags = loop_flags_mangle(loop_flags); |
| 799 |
|
| 800 |
/* Let's open with O_DIRECT if we can. But not all file systems support that, hence fall back to |
| 801 |
* non-O_DIRECT mode automatically, if it fails. */ |
| 802 |
|
| 803 |
basic_flags = O_CLOEXEC|O_NONBLOCK|O_NOCTTY; |
| 804 |
direct_flags = FLAGS_SET(loop_flags, LO_FLAGS_DIRECT_IO) ? O_DIRECT : 0; |
| 805 |
rdwr_flags = open_flags >= 0 ? open_flags : O_RDWR; |
| 806 |
|
| 807 |
fd = xopenat(dir_fd, path, basic_flags|direct_flags|rdwr_flags); |
| 808 |
if (fd < 0 && direct_flags != 0) /* If we had O_DIRECT on, and things failed with that, let's immediately try again without */ |
| 809 |
fd = xopenat(dir_fd, path, basic_flags|rdwr_flags); |
| 810 |
else |
| 811 |
direct = direct_flags != 0; |
| 812 |
if (fd < 0) { |
| 813 |
r = fd; |
| 814 |
|
| 815 |
/* Retry read-only? */ |
| 816 |
if (open_flags >= 0 || !ERRNO_IS_NEG_FS_WRITE_REFUSED(r)) |
| 817 |
return r; |
| 818 |
|
| 819 |
fd = xopenat(dir_fd, path, basic_flags|direct_flags|O_RDONLY); |
| 820 |
if (fd < 0 && direct_flags != 0) /* as above */ |
| 821 |
fd = xopenat(dir_fd, path, basic_flags|O_RDONLY); |
| 822 |
else |
| 823 |
direct = direct_flags != 0; |
| 824 |
if (fd < 0) |
| 825 |
return r; /* Propagate original error */ |
| 826 |
|
| 827 |
open_flags = O_RDONLY; |
| 828 |
} else if (open_flags < 0) |
| 829 |
open_flags = O_RDWR; |
| 830 |
|
| 831 |
log_debug("Opened %s in %s access mode%s, with O_DIRECT %s%s.", |
| 832 |
path ?: "loop device", |
| 833 |
open_flags == O_RDWR ? "O_RDWR" : "O_RDONLY", |
| 834 |
open_flags != rdwr_flags ? " (O_RDWR was requested but not allowed)" : "", |
| 835 |
direct ? "enabled" : "disabled", |
| 836 |
direct != (direct_flags != 0) ? " (O_DIRECT was requested but not supported)" : ""); |
| 837 |
|
| 838 |
return loop_device_make_internal( |
| 839 |
dir_fd == AT_FDCWD ? path : NULL, |
| 840 |
fd, |
| 841 |
open_flags, |
| 842 |
/* offset= */ 0, |
| 843 |
/* size= */ 0, |
| 844 |
sector_size, |
| 845 |
loop_flags, |
| 846 |
lock_op, |
| 847 |
ret); |
| 848 |
} |
| 849 |
|
| 850 |
int loop_device_make_by_path_memory( |
| 851 |
const char *path, |
| 852 |
int open_flags, |
| 853 |
uint32_t sector_size, |
| 854 |
uint32_t loop_flags, |
| 855 |
int lock_op, |
| 856 |
LoopDevice **ret) { |
| 857 |
|
| 858 |
_cleanup_close_ int fd = -EBADF, mfd = -EBADF; |
| 859 |
_cleanup_free_ char *fn = NULL; |
| 860 |
int r; |
| 861 |
|
| 862 |
assert(path); |
| 863 |
assert(open_flags < 0 || IN_SET(open_flags, O_RDWR, O_RDONLY)); |
| 864 |
assert(ret); |
| 865 |
|
| 866 |
/* memfds are always writable, so default to O_RDWR when auto-detecting. */ |
| 867 |
if (open_flags < 0) |
| 868 |
open_flags = O_RDWR; |
| 869 |
|
| 870 |
loop_flags &= ~LO_FLAGS_DIRECT_IO; /* memfds don't support O_DIRECT, hence LO_FLAGS_DIRECT_IO can't be used either */ |
| 871 |
|
| 872 |
fd = open(path, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_RDONLY); |
| 873 |
if (fd < 0) |
| 874 |
return -errno; |
| 875 |
|
| 876 |
r = fd_verify_regular_or_block(fd); |
| 877 |
if (r < 0) |
| 878 |
return r; |
| 879 |
|
| 880 |
r = path_extract_filename(path, &fn); |
| 881 |
if (r < 0) |
| 882 |
return r; |
| 883 |
|
| 884 |
mfd = memfd_clone_fd(fd, fn, open_flags|O_CLOEXEC); |
| 885 |
if (mfd < 0) |
| 886 |
return mfd; |
| 887 |
|
| 888 |
fd = safe_close(fd); /* Let's close the original early */ |
| 889 |
|
| 890 |
return loop_device_make_internal(NULL, mfd, open_flags, 0, 0, sector_size, loop_flags, lock_op, ret); |
| 891 |
} |
| 892 |
|
| 893 |
static LoopDevice* loop_device_free(LoopDevice *d) { |
| 894 |
_cleanup_close_ int control = -EBADF; |
| 895 |
int r; |
| 896 |
|
| 897 |
if (!d) |
| 898 |
return NULL; |
| 899 |
|
| 900 |
/* Release any lock we might have on the device first. We want to open+lock the /dev/loop-control |
| 901 |
* device below, but our lock protocol says that if both control and block device locks are taken, |
| 902 |
* the control lock needs to be taken first, the block device lock second — in order to avoid ABBA |
| 903 |
* locking issues. Moreover, we want to issue LOOP_CLR_FD on the block device further down, and that |
| 904 |
* would fail if we had another fd open to the device. */ |
| 905 |
d->lock_fd = safe_close(d->lock_fd); |
| 906 |
|
| 907 |
/* Let's open the control device early, and lock it, so that we can release our block device and |
| 908 |
* delete it in a synchronized fashion, and allocators won't needlessly see the block device as free |
| 909 |
* while we are about to delete it. */ |
| 910 |
if (!LOOP_DEVICE_IS_FOREIGN(d) && !d->relinquished) { |
| 911 |
control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); |
| 912 |
if (control < 0) |
| 913 |
log_debug_errno(errno, "Failed to open loop control device, cannot remove loop device '%s', ignoring: %m", strna(d->node)); |
| 914 |
else if (flock(control, LOCK_EX) < 0) |
| 915 |
log_debug_errno(errno, "Failed to lock loop control device, ignoring: %m"); |
| 916 |
} |
| 917 |
|
| 918 |
/* Then let's release the loopback block device */ |
| 919 |
if (d->fd >= 0) { |
| 920 |
/* Implicitly sync the device, since otherwise in-flight blocks might not get written */ |
| 921 |
if (fsync(d->fd) < 0) |
| 922 |
log_debug_errno(errno, "Failed to sync loop block device, ignoring: %m"); |
| 923 |
|
| 924 |
if (!LOOP_DEVICE_IS_FOREIGN(d) && !d->relinquished) { |
| 925 |
/* We are supposed to clear the loopback device. Let's do this synchronously: lock |
| 926 |
* the device, manually remove all partitions and then clear it. This should ensure |
| 927 |
* udev doesn't concurrently access the devices, and we can be reasonably sure that |
| 928 |
* once we are done here the device is cleared and all its partition children |
| 929 |
* removed. Note that we lock our primary device fd here (and not a separate locking |
| 930 |
* fd, as we do during allocation, since we want to keep the lock all the way through |
| 931 |
* the LOOP_CLR_FD, but that call would fail if we had more than one fd open.) */ |
| 932 |
|
| 933 |
if (flock(d->fd, LOCK_EX) < 0) |
| 934 |
log_debug_errno(errno, "Failed to lock loop block device, ignoring: %m"); |
| 935 |
|
| 936 |
r = block_device_remove_all_partitions(d->dev, d->fd); |
| 937 |
if (r < 0) |
| 938 |
log_debug_errno(r, "Failed to remove partitions of loopback block device, ignoring: %m"); |
| 939 |
|
| 940 |
if (ioctl(d->fd, LOOP_CLR_FD) < 0) |
| 941 |
log_debug_errno(errno, "Failed to clear loop device, ignoring: %m"); |
| 942 |
} |
| 943 |
|
| 944 |
safe_close(d->fd); |
| 945 |
} |
| 946 |
|
| 947 |
/* Now that the block device is released, let's also try to remove it */ |
| 948 |
if (control >= 0) { |
| 949 |
useconds_t delay = 5 * USEC_PER_MSEC; /* A total delay of 5090 ms between 39 attempts, |
| 950 |
* (4*5 + 5*10 + 5*20 + … + 3*640) = 5090. */ |
| 951 |
|
| 952 |
for (unsigned attempt = 1;; attempt++) { |
| 953 |
if (ioctl(control, LOOP_CTL_REMOVE, d->nr) >= 0) |
| 954 |
break; |
| 955 |
if (errno != EBUSY || attempt > 38) { |
| 956 |
log_debug_errno(errno, "Failed to remove device %s: %m", strna(d->node)); |
| 957 |
break; |
| 958 |
} |
| 959 |
if (attempt % 5 == 0) { |
| 960 |
log_debug("Device is still busy after %u attempts…", attempt); |
| 961 |
delay *= 2; |
| 962 |
} |
| 963 |
|
| 964 |
(void) usleep_safe(delay); |
| 965 |
} |
| 966 |
} |
| 967 |
|
| 968 |
free(d->node); |
| 969 |
sd_device_unref(d->dev); |
| 970 |
free(d->backing_file); |
| 971 |
return mfree(d); |
| 972 |
} |
| 973 |
|
| 974 |
DEFINE_TRIVIAL_REF_UNREF_FUNC(LoopDevice, loop_device, loop_device_free); |
| 975 |
|
| 976 |
void loop_device_relinquish(LoopDevice *d) { |
| 977 |
assert(d); |
| 978 |
|
| 979 |
/* Don't attempt to clean up the loop device anymore from this point on. Leave the clean-ing up to the kernel |
| 980 |
* itself, using the loop device "auto-clear" logic we already turned on when creating the device. */ |
| 981 |
|
| 982 |
d->relinquished = true; |
| 983 |
} |
| 984 |
|
| 985 |
void loop_device_unrelinquish(LoopDevice *d) { |
| 986 |
assert(d); |
| 987 |
d->relinquished = false; |
| 988 |
} |
| 989 |
|
| 990 |
int loop_device_open( |
| 991 |
sd_device *dev, |
| 992 |
int open_flags, |
| 993 |
int lock_op, |
| 994 |
LoopDevice **ret) { |
| 995 |
|
| 996 |
_cleanup_close_ int fd = -EBADF, lock_fd = -EBADF; |
| 997 |
_cleanup_free_ char *node = NULL, *backing_file = NULL; |
| 998 |
dev_t devnum, backing_devno = 0; |
| 999 |
struct loop_info64 info; |
| 1000 |
ino_t backing_inode = 0; |
| 1001 |
uint64_t diskseq = 0; |
| 1002 |
LoopDevice *d; |
| 1003 |
const char *s; |
| 1004 |
int r, nr = -1; |
| 1005 |
|
| 1006 |
assert(dev); |
| 1007 |
assert(IN_SET(open_flags, O_RDWR, O_RDONLY)); |
| 1008 |
assert(ret); |
| 1009 |
|
| 1010 |
/* Even if fd is provided through the argument in loop_device_open_from_fd(), we reopen the inode |
| 1011 |
* here, instead of keeping just a dup() clone of it around, since we want to ensure that the |
| 1012 |
* O_DIRECT flag of the handle we keep is off, we have our own file index, and have the right |
| 1013 |
* read/write mode in effect. */ |
| 1014 |
fd = sd_device_open(dev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|open_flags); |
| 1015 |
if (fd < 0) |
| 1016 |
return fd; |
| 1017 |
|
| 1018 |
if ((lock_op & ~LOCK_NB) != LOCK_UN) { |
| 1019 |
lock_fd = open_lock_fd(fd, lock_op); |
| 1020 |
if (lock_fd < 0) |
| 1021 |
return lock_fd; |
| 1022 |
} |
| 1023 |
|
| 1024 |
if (ioctl(fd, LOOP_GET_STATUS64, &info) >= 0) { |
| 1025 |
#if HAVE_VALGRIND_MEMCHECK_H |
| 1026 |
/* Valgrind currently doesn't know LOOP_GET_STATUS64. Remove this once it does */ |
| 1027 |
VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info)); |
| 1028 |
#endif |
| 1029 |
nr = info.lo_number; |
| 1030 |
|
| 1031 |
if (device_get_sysattr_safe_string(dev, "loop/backing_file", &s) >= 0) { |
| 1032 |
backing_file = strdup(s); |
| 1033 |
if (!backing_file) |
| 1034 |
return -ENOMEM; |
| 1035 |
} |
| 1036 |
|
| 1037 |
backing_devno = info.lo_device; |
| 1038 |
backing_inode = info.lo_inode; |
| 1039 |
} |
| 1040 |
|
| 1041 |
r = fd_get_diskseq(fd, &diskseq); |
| 1042 |
if (r < 0 && r != -EOPNOTSUPP) |
| 1043 |
return r; |
| 1044 |
|
| 1045 |
uint32_t sector_size; |
| 1046 |
r = blockdev_get_sector_size(fd, §or_size); |
| 1047 |
if (r < 0) |
| 1048 |
return r; |
| 1049 |
|
| 1050 |
uint64_t device_size; |
| 1051 |
r = blockdev_get_device_size(fd, &device_size); |
| 1052 |
if (r < 0) |
| 1053 |
return r; |
| 1054 |
|
| 1055 |
r = sd_device_get_devnum(dev, &devnum); |
| 1056 |
if (r < 0) |
| 1057 |
return r; |
| 1058 |
|
| 1059 |
r = sd_device_get_devname(dev, &s); |
| 1060 |
if (r < 0) |
| 1061 |
return r; |
| 1062 |
|
| 1063 |
node = strdup(s); |
| 1064 |
if (!node) |
| 1065 |
return -ENOMEM; |
| 1066 |
|
| 1067 |
d = new(LoopDevice, 1); |
| 1068 |
if (!d) |
| 1069 |
return -ENOMEM; |
| 1070 |
|
| 1071 |
*d = (LoopDevice) { |
| 1072 |
.n_ref = 1, |
| 1073 |
.fd = TAKE_FD(fd), |
| 1074 |
.lock_fd = TAKE_FD(lock_fd), |
| 1075 |
.nr = nr, |
| 1076 |
.node = TAKE_PTR(node), |
| 1077 |
.dev = sd_device_ref(dev), |
| 1078 |
.backing_file = TAKE_PTR(backing_file), |
| 1079 |
.backing_inode = backing_inode, |
| 1080 |
.backing_devno = backing_devno, |
| 1081 |
.relinquished = true, /* It's not ours, don't try to destroy it when this object is freed */ |
| 1082 |
.devno = devnum, |
| 1083 |
.diskseq = diskseq, |
| 1084 |
.sector_size = sector_size, |
| 1085 |
.device_size = device_size, |
| 1086 |
.created = false, |
| 1087 |
}; |
| 1088 |
|
| 1089 |
*ret = d; |
| 1090 |
return 0; |
| 1091 |
} |
| 1092 |
|
| 1093 |
int loop_device_open_from_fd( |
| 1094 |
int fd, |
| 1095 |
int open_flags, |
| 1096 |
int lock_op, |
| 1097 |
LoopDevice **ret) { |
| 1098 |
|
| 1099 |
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
| 1100 |
int r; |
| 1101 |
|
| 1102 |
r = block_device_new_from_fd(ASSERT_FD(fd), 0, &dev); |
| 1103 |
if (r < 0) |
| 1104 |
return r; |
| 1105 |
|
| 1106 |
return loop_device_open(dev, open_flags, lock_op, ret); |
| 1107 |
} |
| 1108 |
|
| 1109 |
int loop_device_open_from_path( |
| 1110 |
const char *path, |
| 1111 |
int open_flags, |
| 1112 |
int lock_op, |
| 1113 |
LoopDevice **ret) { |
| 1114 |
|
| 1115 |
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
| 1116 |
int r; |
| 1117 |
|
| 1118 |
assert(path); |
| 1119 |
|
| 1120 |
r = block_device_new_from_path(path, 0, &dev); |
| 1121 |
if (r < 0) |
| 1122 |
return r; |
| 1123 |
|
| 1124 |
return loop_device_open(dev, open_flags, lock_op, ret); |
| 1125 |
} |
| 1126 |
|
| 1127 |
static int resize_partition(int partition_fd, uint64_t offset, uint64_t size) { |
| 1128 |
char sysfs[STRLEN("/sys/dev/block/:/partition") + 2*DECIMAL_STR_MAX(dev_t) + 1]; |
| 1129 |
_cleanup_free_ char *buffer = NULL; |
| 1130 |
uint64_t current_offset, current_size, partno; |
| 1131 |
_cleanup_close_ int whole_fd = -EBADF; |
| 1132 |
struct stat st; |
| 1133 |
dev_t devno; |
| 1134 |
int r; |
| 1135 |
|
| 1136 |
/* Resizes the partition the loopback device refer to (assuming it refers to one instead of an actual |
| 1137 |
* loopback device), and changes the offset, if needed. This is a fancy wrapper around |
| 1138 |
* BLKPG_RESIZE_PARTITION. */ |
| 1139 |
|
| 1140 |
if (fstat(ASSERT_FD(partition_fd), &st) < 0) |
| 1141 |
return -errno; |
| 1142 |
|
| 1143 |
assert(S_ISBLK(st.st_mode)); |
| 1144 |
|
| 1145 |
xsprintf(sysfs, "/sys/dev/block/" DEVNUM_FORMAT_STR "/partition", DEVNUM_FORMAT_VAL(st.st_rdev)); |
| 1146 |
r = read_one_line_file(sysfs, &buffer); |
| 1147 |
if (r == -ENOENT) /* not a partition, cannot resize */ |
| 1148 |
return -ENOTTY; |
| 1149 |
if (r < 0) |
| 1150 |
return r; |
| 1151 |
r = safe_atou64(buffer, &partno); |
| 1152 |
if (r < 0) |
| 1153 |
return r; |
| 1154 |
|
| 1155 |
xsprintf(sysfs, "/sys/dev/block/" DEVNUM_FORMAT_STR "/start", DEVNUM_FORMAT_VAL(st.st_rdev)); |
| 1156 |
|
| 1157 |
buffer = mfree(buffer); |
| 1158 |
r = read_one_line_file(sysfs, &buffer); |
| 1159 |
if (r < 0) |
| 1160 |
return r; |
| 1161 |
r = safe_atou64(buffer, ¤t_offset); |
| 1162 |
if (r < 0) |
| 1163 |
return r; |
| 1164 |
if (current_offset > UINT64_MAX/512U) |
| 1165 |
return -EINVAL; |
| 1166 |
current_offset *= 512U; |
| 1167 |
|
| 1168 |
r = blockdev_get_device_size(partition_fd, ¤t_size); |
| 1169 |
if (r < 0) |
| 1170 |
return r; |
| 1171 |
|
| 1172 |
if (size == UINT64_MAX && offset == UINT64_MAX) |
| 1173 |
return 0; |
| 1174 |
if (current_size == size && current_offset == offset) |
| 1175 |
return 0; |
| 1176 |
|
| 1177 |
xsprintf(sysfs, "/sys/dev/block/" DEVNUM_FORMAT_STR "/../dev", DEVNUM_FORMAT_VAL(st.st_rdev)); |
| 1178 |
|
| 1179 |
buffer = mfree(buffer); |
| 1180 |
r = read_one_line_file(sysfs, &buffer); |
| 1181 |
if (r < 0) |
| 1182 |
return r; |
| 1183 |
r = parse_devnum(buffer, &devno); |
| 1184 |
if (r < 0) |
| 1185 |
return r; |
| 1186 |
|
| 1187 |
whole_fd = r = device_open_from_devnum(S_IFBLK, devno, O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY, NULL); |
| 1188 |
if (r < 0) |
| 1189 |
return r; |
| 1190 |
|
| 1191 |
return block_device_resize_partition( |
| 1192 |
whole_fd, |
| 1193 |
partno, |
| 1194 |
offset == UINT64_MAX ? current_offset : offset, |
| 1195 |
size == UINT64_MAX ? current_size : size); |
| 1196 |
} |
| 1197 |
|
| 1198 |
int loop_device_refresh_size(LoopDevice *d, uint64_t offset, uint64_t size) { |
| 1199 |
struct loop_info64 info; |
| 1200 |
|
| 1201 |
assert(d); |
| 1202 |
assert(d->fd >= 0); |
| 1203 |
|
| 1204 |
/* Changes the offset/start of the loop device relative to the beginning of the underlying file or |
| 1205 |
* block device. If this loop device actually refers to a partition and not a loopback device, we'll |
| 1206 |
* try to adjust the partition offsets instead. |
| 1207 |
* |
| 1208 |
* If either offset or size is UINT64_MAX we won't change that parameter. */ |
| 1209 |
|
| 1210 |
if (d->nr < 0) /* not a loopback device */ |
| 1211 |
return resize_partition(d->fd, offset, size); |
| 1212 |
|
| 1213 |
if (ioctl(d->fd, LOOP_GET_STATUS64, &info) < 0) |
| 1214 |
return -errno; |
| 1215 |
|
| 1216 |
#if HAVE_VALGRIND_MEMCHECK_H |
| 1217 |
/* Valgrind currently doesn't know LOOP_GET_STATUS64. Remove this once it does */ |
| 1218 |
VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info)); |
| 1219 |
#endif |
| 1220 |
|
| 1221 |
if ((size == UINT64_MAX || info.lo_sizelimit == size) && |
| 1222 |
(offset == UINT64_MAX || info.lo_offset == offset)) |
| 1223 |
return 0; |
| 1224 |
|
| 1225 |
if (size != UINT64_MAX) |
| 1226 |
info.lo_sizelimit = size; |
| 1227 |
if (offset != UINT64_MAX) |
| 1228 |
info.lo_offset = offset; |
| 1229 |
|
| 1230 |
return RET_NERRNO(ioctl(d->fd, LOOP_SET_STATUS64, &info)); |
| 1231 |
} |
| 1232 |
|
| 1233 |
int loop_device_flock(LoopDevice *d, int operation) { |
| 1234 |
assert(IN_SET(operation & ~LOCK_NB, LOCK_UN, LOCK_SH, LOCK_EX)); |
| 1235 |
assert(d); |
| 1236 |
|
| 1237 |
/* When unlocking just close the lock fd */ |
| 1238 |
if ((operation & ~LOCK_NB) == LOCK_UN) { |
| 1239 |
d->lock_fd = safe_close(d->lock_fd); |
| 1240 |
return 0; |
| 1241 |
} |
| 1242 |
|
| 1243 |
/* If we had no lock fd so far, create one and lock it right-away */ |
| 1244 |
if (d->lock_fd < 0) { |
| 1245 |
d->lock_fd = open_lock_fd(ASSERT_FD(d->fd), operation); |
| 1246 |
if (d->lock_fd < 0) |
| 1247 |
return d->lock_fd; |
| 1248 |
|
| 1249 |
return 0; |
| 1250 |
} |
| 1251 |
|
| 1252 |
/* Otherwise change the current lock mode on the existing fd */ |
| 1253 |
return RET_NERRNO(flock(d->lock_fd, operation)); |
| 1254 |
} |
| 1255 |
|
| 1256 |
int loop_device_sync(LoopDevice *d) { |
| 1257 |
assert(d); |
| 1258 |
|
| 1259 |
/* We also do this implicitly in loop_device_unref(). Doing this explicitly here has the benefit that |
| 1260 |
* we can check the return value though. */ |
| 1261 |
|
| 1262 |
return RET_NERRNO(fsync(ASSERT_FD(d->fd))); |
| 1263 |
} |
| 1264 |
|
| 1265 |
int loop_device_set_autoclear(LoopDevice *d, bool autoclear) { |
| 1266 |
struct loop_info64 info; |
| 1267 |
|
| 1268 |
assert(d); |
| 1269 |
|
| 1270 |
if (LOOP_DEVICE_IS_FOREIGN(d)) |
| 1271 |
return 0; |
| 1272 |
|
| 1273 |
if (ioctl(ASSERT_FD(d->fd), LOOP_GET_STATUS64, &info) < 0) |
| 1274 |
return -errno; |
| 1275 |
|
| 1276 |
if (autoclear == FLAGS_SET(info.lo_flags, LO_FLAGS_AUTOCLEAR)) |
| 1277 |
return 0; |
| 1278 |
|
| 1279 |
SET_FLAG(info.lo_flags, LO_FLAGS_AUTOCLEAR, autoclear); |
| 1280 |
|
| 1281 |
if (ioctl(d->fd, LOOP_SET_STATUS64, &info) < 0) |
| 1282 |
return -errno; |
| 1283 |
|
| 1284 |
return 1; |
| 1285 |
} |
| 1286 |
|
| 1287 |
int loop_device_set_filename(LoopDevice *d, const char *name) { |
| 1288 |
struct loop_info64 info; |
| 1289 |
|
| 1290 |
assert(d); |
| 1291 |
|
| 1292 |
/* Sets the .lo_file_name of the loopback device. This is supposed to contain the path to the file |
| 1293 |
* backing the block device, but is actually just a free-form string you can pass to the kernel. Most |
| 1294 |
* tools that actually care for the backing file path use the sysfs attribute file loop/backing_file |
| 1295 |
* which is a kernel generated string, subject to file system namespaces and such. |
| 1296 |
* |
| 1297 |
* .lo_file_name is useful since userspace can select it freely when creating a loopback block |
| 1298 |
* device, and we can use it for /dev/disk/by-loop-ref/ symlinks, and similar, so that apps can |
| 1299 |
* recognize their own loopback files. */ |
| 1300 |
|
| 1301 |
if (name && strlen(name) >= sizeof(info.lo_file_name)) |
| 1302 |
return -ENOBUFS; |
| 1303 |
|
| 1304 |
if (ioctl(ASSERT_FD(d->fd), LOOP_GET_STATUS64, &info) < 0) |
| 1305 |
return -errno; |
| 1306 |
|
| 1307 |
if (strneq((char*) info.lo_file_name, strempty(name), sizeof(info.lo_file_name))) |
| 1308 |
return 0; |
| 1309 |
|
| 1310 |
if (name) { |
| 1311 |
strncpy((char*) info.lo_file_name, name, sizeof(info.lo_file_name)-1); |
| 1312 |
info.lo_file_name[sizeof(info.lo_file_name)-1] = 0; |
| 1313 |
} else |
| 1314 |
memzero(info.lo_file_name, sizeof(info.lo_file_name)); |
| 1315 |
|
| 1316 |
if (ioctl(d->fd, LOOP_SET_STATUS64, &info) < 0) |
| 1317 |
return -errno; |
| 1318 |
|
| 1319 |
return 1; |
| 1320 |
} |