| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/blkpg.h> |
| 4 |
#include <linux/fs.h> |
| 5 |
#include <sys/file.h> |
| 6 |
#include <sys/ioctl.h> |
| 7 |
#include <sys/stat.h> |
| 8 |
#include <unistd.h> |
| 9 |
|
| 10 |
#include "sd-device.h" |
| 11 |
|
| 12 |
#include "alloc-util.h" |
| 13 |
#include "blockdev-util.h" |
| 14 |
#include "btrfs-util.h" |
| 15 |
#include "device-private.h" |
| 16 |
#include "device-util.h" |
| 17 |
#include "devnum-util.h" |
| 18 |
#include "dirent-util.h" |
| 19 |
#include "errno-util.h" |
| 20 |
#include "fd-util.h" |
| 21 |
#include "fileio.h" |
| 22 |
#include "fs-util.h" |
| 23 |
#include "parse-util.h" |
| 24 |
#include "path-util.h" |
| 25 |
#include "string-util.h" |
| 26 |
|
| 27 |
static int fd_get_devnum(int fd, BlockDeviceLookupFlags flags, dev_t *ret) { |
| 28 |
struct stat st; |
| 29 |
dev_t devnum; |
| 30 |
int r; |
| 31 |
|
| 32 |
assert(fd >= 0); |
| 33 |
assert(ret); |
| 34 |
|
| 35 |
if (fstat(fd, &st) < 0) |
| 36 |
return -errno; |
| 37 |
|
| 38 |
if (S_ISBLK(st.st_mode)) |
| 39 |
devnum = st.st_rdev; |
| 40 |
else if (!FLAGS_SET(flags, BLOCK_DEVICE_LOOKUP_BACKING)) |
| 41 |
return -ENOTBLK; |
| 42 |
else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode)) |
| 43 |
return -ENOTBLK; |
| 44 |
else if (major(st.st_dev) != 0) |
| 45 |
devnum = st.st_dev; |
| 46 |
else { |
| 47 |
/* If major(st.st_dev) is zero, this might mean we are backed by btrfs, which needs special |
| 48 |
* handing, to get the backing device node. */ |
| 49 |
|
| 50 |
r = btrfs_get_block_device_fd(fd, &devnum); |
| 51 |
if (r == -ENOTTY) /* not btrfs */ |
| 52 |
return -ENOTBLK; |
| 53 |
if (r < 0) |
| 54 |
return r; |
| 55 |
} |
| 56 |
|
| 57 |
*ret = devnum; |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
int block_device_is_whole_disk(sd_device *dev) { |
| 62 |
int r; |
| 63 |
|
| 64 |
assert(dev); |
| 65 |
|
| 66 |
r = device_in_subsystem(dev, "block"); |
| 67 |
if (r < 0) |
| 68 |
return r; |
| 69 |
if (r == 0) |
| 70 |
return -ENOTBLK; |
| 71 |
|
| 72 |
return device_is_devtype(dev, "disk"); |
| 73 |
} |
| 74 |
|
| 75 |
int block_device_get_whole_disk(sd_device *dev, sd_device **ret) { |
| 76 |
int r; |
| 77 |
|
| 78 |
assert(dev); |
| 79 |
assert(ret); |
| 80 |
|
| 81 |
/* Do not unref returned sd_device object. */ |
| 82 |
|
| 83 |
r = block_device_is_whole_disk(dev); |
| 84 |
if (r < 0) |
| 85 |
return r; |
| 86 |
if (r == 0) { |
| 87 |
r = sd_device_get_parent(dev, &dev); |
| 88 |
if (r == -ENOENT) /* Already removed? Let's return a recognizable error. */ |
| 89 |
return -ENODEV; |
| 90 |
if (r < 0) |
| 91 |
return r; |
| 92 |
|
| 93 |
r = block_device_is_whole_disk(dev); |
| 94 |
if (r < 0) |
| 95 |
return r; |
| 96 |
if (r == 0) |
| 97 |
return -ENXIO; |
| 98 |
} |
| 99 |
|
| 100 |
*ret = dev; |
| 101 |
return 0; |
| 102 |
} |
| 103 |
|
| 104 |
static int block_device_get_originating_one(sd_device *dev, sd_device **ret) { |
| 105 |
_cleanup_(sd_device_unrefp) sd_device *first_found = NULL; |
| 106 |
const char *suffix; |
| 107 |
dev_t devnum = 0; /* avoid false maybe-uninitialized warning */ |
| 108 |
|
| 109 |
/* For the specified block device tries to chase it through the layers, in case LUKS-style DM |
| 110 |
* stacking is used, trying to find the next underlying layer. */ |
| 111 |
|
| 112 |
assert(dev); |
| 113 |
assert(ret); |
| 114 |
|
| 115 |
FOREACH_DEVICE_CHILD_WITH_SUFFIX(dev, child, suffix) { |
| 116 |
sd_device *child_whole_disk; |
| 117 |
dev_t n; |
| 118 |
|
| 119 |
if (!path_startswith(suffix, "slaves")) |
| 120 |
continue; |
| 121 |
|
| 122 |
if (block_device_get_whole_disk(child, &child_whole_disk) < 0) |
| 123 |
continue; |
| 124 |
|
| 125 |
if (sd_device_get_devnum(child_whole_disk, &n) < 0) |
| 126 |
continue; |
| 127 |
|
| 128 |
if (!first_found) { |
| 129 |
first_found = sd_device_ref(child); |
| 130 |
devnum = n; |
| 131 |
continue; |
| 132 |
} |
| 133 |
|
| 134 |
/* We found a device backed by multiple other devices. We don't really support automatic |
| 135 |
* discovery on such setups, with the exception of dm-verity partitions. In this case there |
| 136 |
* are two backing devices: the data partition and the hash partition. We are fine with such |
| 137 |
* setups, however, only if both partitions are on the same physical device. Hence, let's |
| 138 |
* verify this by iterating over every node in the 'slaves/' directory and comparing them with |
| 139 |
* the first that gets returned by readdir(), to ensure they all point to the same device. */ |
| 140 |
if (n != devnum) |
| 141 |
return -ENOTUNIQ; |
| 142 |
} |
| 143 |
|
| 144 |
if (!first_found) |
| 145 |
return -ENOENT; |
| 146 |
|
| 147 |
*ret = TAKE_PTR(first_found); |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
int block_device_get_originating(sd_device *dev, sd_device **ret, bool recursive) { |
| 152 |
_cleanup_(sd_device_unrefp) sd_device *current = NULL; |
| 153 |
int r; |
| 154 |
|
| 155 |
assert(dev); |
| 156 |
assert(ret); |
| 157 |
|
| 158 |
if (!recursive) |
| 159 |
return block_device_get_originating_one(dev, ret); |
| 160 |
|
| 161 |
current = sd_device_ref(dev); |
| 162 |
|
| 163 |
for (;;) { |
| 164 |
sd_device *origin; |
| 165 |
|
| 166 |
r = block_device_get_originating_one(current, &origin); |
| 167 |
if (r == -ENOENT) |
| 168 |
break; |
| 169 |
if (r < 0) |
| 170 |
return r; |
| 171 |
|
| 172 |
sd_device_unref(current); |
| 173 |
current = origin; |
| 174 |
} |
| 175 |
|
| 176 |
if (current == dev) |
| 177 |
return -ENOENT; |
| 178 |
|
| 179 |
*ret = TAKE_PTR(current); |
| 180 |
|
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
int block_device_new_from_fd(int fd, BlockDeviceLookupFlags flags, sd_device **ret) { |
| 185 |
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
| 186 |
dev_t devnum; |
| 187 |
int r; |
| 188 |
|
| 189 |
assert(fd >= 0); |
| 190 |
assert(ret); |
| 191 |
|
| 192 |
r = fd_get_devnum(fd, flags, &devnum); |
| 193 |
if (r < 0) |
| 194 |
return r; |
| 195 |
|
| 196 |
r = sd_device_new_from_devnum(&dev, 'b', devnum); |
| 197 |
if (r < 0) |
| 198 |
return r; |
| 199 |
|
| 200 |
if (FLAGS_SET(flags, BLOCK_DEVICE_LOOKUP_ORIGINATING)) { |
| 201 |
_cleanup_(sd_device_unrefp) sd_device *dev_origin = NULL; |
| 202 |
sd_device *dev_whole_disk; |
| 203 |
|
| 204 |
r = block_device_get_whole_disk(dev, &dev_whole_disk); |
| 205 |
if (r < 0) |
| 206 |
return r; |
| 207 |
|
| 208 |
r = block_device_get_originating(dev_whole_disk, &dev_origin, /* recursive= */ false); |
| 209 |
if (r >= 0) |
| 210 |
device_unref_and_replace_new_ref(dev, dev_origin); |
| 211 |
else if (r != -ENOENT) |
| 212 |
return r; |
| 213 |
} |
| 214 |
|
| 215 |
if (FLAGS_SET(flags, BLOCK_DEVICE_LOOKUP_WHOLE_DISK)) { |
| 216 |
sd_device *dev_whole_disk; |
| 217 |
|
| 218 |
r = block_device_get_whole_disk(dev, &dev_whole_disk); |
| 219 |
if (r < 0) |
| 220 |
return r; |
| 221 |
|
| 222 |
*ret = sd_device_ref(dev_whole_disk); |
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
*ret = sd_device_ref(dev); |
| 227 |
return 0; |
| 228 |
} |
| 229 |
|
| 230 |
int block_device_new_from_path(const char *path, BlockDeviceLookupFlags flags, sd_device **ret) { |
| 231 |
_cleanup_close_ int fd = -EBADF; |
| 232 |
|
| 233 |
assert(path); |
| 234 |
assert(ret); |
| 235 |
|
| 236 |
fd = open(path, O_CLOEXEC|O_PATH); |
| 237 |
if (fd < 0) |
| 238 |
return -errno; |
| 239 |
|
| 240 |
return block_device_new_from_fd(fd, flags, ret); |
| 241 |
} |
| 242 |
|
| 243 |
int block_get_whole_disk(dev_t d, dev_t *ret) { |
| 244 |
char p[SYS_BLOCK_PATH_MAX("/partition")]; |
| 245 |
_cleanup_free_ char *s = NULL; |
| 246 |
dev_t devt; |
| 247 |
int r; |
| 248 |
|
| 249 |
assert(ret); |
| 250 |
|
| 251 |
if (major(d) == 0) |
| 252 |
return -ENODEV; |
| 253 |
|
| 254 |
/* If it has a queue this is good enough for us */ |
| 255 |
xsprintf_sys_block_path(p, "/queue", d); |
| 256 |
if (access(p, F_OK) >= 0) { |
| 257 |
*ret = d; |
| 258 |
return 0; |
| 259 |
} |
| 260 |
if (errno != ENOENT) |
| 261 |
return -errno; |
| 262 |
|
| 263 |
/* If it is a partition find the originating device */ |
| 264 |
xsprintf_sys_block_path(p, "/partition", d); |
| 265 |
if (access(p, F_OK) < 0) |
| 266 |
return -errno; |
| 267 |
|
| 268 |
/* Get parent dev_t */ |
| 269 |
xsprintf_sys_block_path(p, "/../dev", d); |
| 270 |
r = read_one_line_file(p, &s); |
| 271 |
if (r < 0) |
| 272 |
return r; |
| 273 |
|
| 274 |
r = parse_devnum(s, &devt); |
| 275 |
if (r < 0) |
| 276 |
return r; |
| 277 |
|
| 278 |
/* Only return this if it is really good enough for us. */ |
| 279 |
xsprintf_sys_block_path(p, "/queue", devt); |
| 280 |
if (access(p, F_OK) < 0) |
| 281 |
return -errno; |
| 282 |
|
| 283 |
*ret = devt; |
| 284 |
return 1; |
| 285 |
} |
| 286 |
|
| 287 |
int get_block_device_fd(int fd, dev_t *ret) { |
| 288 |
struct stat st; |
| 289 |
int r; |
| 290 |
|
| 291 |
assert(fd >= 0); |
| 292 |
assert(ret); |
| 293 |
|
| 294 |
/* Gets the block device directly backing a file system. If the block device is encrypted, returns |
| 295 |
* the device mapper block device. */ |
| 296 |
|
| 297 |
if (fstat(fd, &st)) |
| 298 |
return -errno; |
| 299 |
|
| 300 |
if (major(st.st_dev) != 0) { |
| 301 |
*ret = st.st_dev; |
| 302 |
return 1; |
| 303 |
} |
| 304 |
|
| 305 |
r = btrfs_get_block_device_fd(fd, ret); |
| 306 |
if (r != -ENOTTY) /* ENOTTY: not btrfs */ |
| 307 |
return r; |
| 308 |
|
| 309 |
*ret = 0; |
| 310 |
return 0; |
| 311 |
} |
| 312 |
|
| 313 |
int get_block_device(const char *path, dev_t *ret) { |
| 314 |
_cleanup_close_ int fd = -EBADF; |
| 315 |
|
| 316 |
assert(path); |
| 317 |
assert(ret); |
| 318 |
|
| 319 |
fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC); |
| 320 |
if (fd < 0) |
| 321 |
return -errno; |
| 322 |
|
| 323 |
return get_block_device_fd(fd, ret); |
| 324 |
} |
| 325 |
|
| 326 |
int block_get_originating(dev_t dt, dev_t *ret, bool recursive) { |
| 327 |
_cleanup_(sd_device_unrefp) sd_device *dev = NULL, *origin = NULL; |
| 328 |
int r; |
| 329 |
|
| 330 |
assert(ret); |
| 331 |
|
| 332 |
r = sd_device_new_from_devnum(&dev, 'b', dt); |
| 333 |
if (r < 0) |
| 334 |
return r; |
| 335 |
|
| 336 |
r = block_device_get_originating(dev, &origin, recursive); |
| 337 |
if (r < 0) |
| 338 |
return r; |
| 339 |
|
| 340 |
return sd_device_get_devnum(origin, ret); |
| 341 |
} |
| 342 |
|
| 343 |
int get_block_device_harder_fd(int fd, dev_t *ret) { |
| 344 |
int r; |
| 345 |
|
| 346 |
assert(fd >= 0); |
| 347 |
assert(ret); |
| 348 |
|
| 349 |
/* Gets the backing block device for a file system, and handles LUKS encrypted file systems, looking |
| 350 |
* for its underlying physical device, if there is one. */ |
| 351 |
|
| 352 |
r = get_block_device_fd(fd, ret); |
| 353 |
if (r <= 0) |
| 354 |
return r; |
| 355 |
|
| 356 |
r = block_get_originating(*ret, ret, /* recursive= */ true); |
| 357 |
if (r < 0) |
| 358 |
log_debug_errno(r, "Failed to chase block device, ignoring: %m"); |
| 359 |
|
| 360 |
return 1; |
| 361 |
} |
| 362 |
|
| 363 |
int get_block_device_harder(const char *path, dev_t *ret) { |
| 364 |
_cleanup_close_ int fd = -EBADF; |
| 365 |
|
| 366 |
assert(path); |
| 367 |
assert(ret); |
| 368 |
|
| 369 |
fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC); |
| 370 |
if (fd < 0) |
| 371 |
return -errno; |
| 372 |
|
| 373 |
return get_block_device_harder_fd(fd, ret); |
| 374 |
} |
| 375 |
|
| 376 |
int lock_whole_block_device(dev_t devt, int open_flags, int operation) { |
| 377 |
_cleanup_close_ int lock_fd = -EBADF; |
| 378 |
dev_t whole_devt; |
| 379 |
int r; |
| 380 |
|
| 381 |
/* Let's get a BSD file lock on the whole block device, as per: https://systemd.io/BLOCK_DEVICE_LOCKING |
| 382 |
* |
| 383 |
* NB: it matters whether open_flags indicates open for write: only then will the eventual closing of |
| 384 |
* the fd trigger udev's partitioning rescanning of the device (as it watches for IN_CLOSE_WRITE), |
| 385 |
* hence make sure to pass the right value there. */ |
| 386 |
|
| 387 |
r = block_get_whole_disk(devt, &whole_devt); |
| 388 |
if (r < 0) |
| 389 |
return r; |
| 390 |
|
| 391 |
lock_fd = device_open_from_devnum(S_IFBLK, whole_devt, open_flags|O_CLOEXEC|O_NONBLOCK|O_NOCTTY, NULL); |
| 392 |
if (lock_fd < 0) |
| 393 |
return lock_fd; |
| 394 |
|
| 395 |
if (flock(lock_fd, operation) < 0) |
| 396 |
return -errno; |
| 397 |
|
| 398 |
return TAKE_FD(lock_fd); |
| 399 |
} |
| 400 |
|
| 401 |
int blockdev_partscan_enabled(sd_device *dev) { |
| 402 |
unsigned capability; |
| 403 |
int r, ext_range; |
| 404 |
|
| 405 |
/* Checks if partition scanning is correctly enabled on the block device. |
| 406 |
* |
| 407 |
* The 'GENHD_FL_NO_PART_SCAN' flag was introduced by |
| 408 |
* https://github.com/torvalds/linux/commit/d27769ec3df1a8de9ca450d2dcd72d1ab259ba32 (v3.2). |
| 409 |
* But at that time, the flag is also effectively implied when 'minors' element of 'struct gendisk' |
| 410 |
* is 1, which can be check with 'ext_range' sysfs attribute. Explicit flag ('GENHD_FL_NO_PART_SCAN') |
| 411 |
* can be obtained from 'capability' sysattr. |
| 412 |
* |
| 413 |
* With https://github.com/torvalds/linux/commit/46e7eac647b34ed4106a8262f8bedbb90801fadd (v5.17), |
| 414 |
* the flag is renamed to GENHD_FL_NO_PART. |
| 415 |
* |
| 416 |
* With https://github.com/torvalds/linux/commit/1ebe2e5f9d68e94c524aba876f27b945669a7879 (v5.17), |
| 417 |
* we can check the flag from 'ext_range' sysfs attribute directly. |
| 418 |
* |
| 419 |
* With https://github.com/torvalds/linux/commit/430cc5d3ab4d0ba0bd011cfbb0035e46ba92920c (v5.17), |
| 420 |
* the value of GENHD_FL_NO_PART is changed from 0x0200 to 0x0004. 💣💣💣 |
| 421 |
* Note, the new value was used by the GENHD_FL_MEDIA_CHANGE_NOTIFY flag, which was introduced by |
| 422 |
* 86ce18d7b7925bfd6b64c061828ca2a857ee83b8 (v2.6.22), and removed by |
| 423 |
* 9243c6f3e012a92dd900d97ef45efaf8a8edc448 (v5.7). If we believe the commit message of |
| 424 |
* e81cd5a983bb35dabd38ee472cf3fea1c63e0f23, the flag was never used. So, fortunately, we can use |
| 425 |
* both the new and old values safely. |
| 426 |
* |
| 427 |
* With https://github.com/torvalds/linux/commit/b9684a71fca793213378dd410cd11675d973eaa1 (v5.19), |
| 428 |
* another flag GD_SUPPRESS_PART_SCAN is introduced for loopback block device, and partition scanning |
| 429 |
* is done only when both GENHD_FL_NO_PART and GD_SUPPRESS_PART_SCAN are not set. Before the commit, |
| 430 |
* LO_FLAGS_PARTSCAN flag was directly tied with GENHD_FL_NO_PART. But with this change now it is |
| 431 |
* tied with GD_SUPPRESS_PART_SCAN. So, LO_FLAGS_PARTSCAN cannot be obtained from 'ext_range' |
| 432 |
* sysattr, which corresponds to GENHD_FL_NO_PART, and we need to read 'loop/partscan'. 💣💣💣 |
| 433 |
* |
| 434 |
* With https://github.com/torvalds/linux/commit/73a166d9749230d598320fdae3b687cdc0e2e205 (v6.3), |
| 435 |
* the GD_SUPPRESS_PART_SCAN flag is also introduced for userspace block device (ublk). Though, not |
| 436 |
* sure if we should support the device... |
| 437 |
* |
| 438 |
* With https://github.com/torvalds/linux/commit/e81cd5a983bb35dabd38ee472cf3fea1c63e0f23 (v6.3), |
| 439 |
* the 'capability' sysfs attribute is deprecated, hence we cannot check flags from it. 💣💣💣 |
| 440 |
* |
| 441 |
* With https://github.com/torvalds/linux/commit/a4217c6740dc64a3eb6815868a9260825e8c68c6 (v6.10, |
| 442 |
* backported to v6.6+), the partscan status is directly exposed as 'partscan' sysattr. |
| 443 |
* |
| 444 |
* To support both old and new kernels, we need to do the following: |
| 445 |
* 1) check 'partscan' sysfs attribute where the information is made directly available, |
| 446 |
* 2) check if the blockdev refers to a partition, where partscan is not supported, |
| 447 |
* 3) check 'loop/partscan' sysfs attribute for loopback block devices, and if '0' we can conclude |
| 448 |
* partition scanning is disabled, |
| 449 |
* 4) check 'ext_range' sysfs attribute, and if '1' we can conclude partition scanning is disabled, |
| 450 |
* 5) otherwise check 'capability' sysfs attribute for ancient version. */ |
| 451 |
|
| 452 |
assert(dev); |
| 453 |
|
| 454 |
r = device_in_subsystem(dev, "block"); |
| 455 |
if (r < 0) |
| 456 |
return r; |
| 457 |
if (r == 0) |
| 458 |
return -ENOTBLK; |
| 459 |
|
| 460 |
/* For v6.10 or newer. */ |
| 461 |
r = device_get_sysattr_bool(dev, "partscan"); |
| 462 |
if (r != -ENOENT) |
| 463 |
return r; |
| 464 |
|
| 465 |
/* Partition block devices never have partition scanning on, there's no concept of sub-partitions for |
| 466 |
* partitions. */ |
| 467 |
r = device_is_devtype(dev, "partition"); |
| 468 |
if (r < 0) |
| 469 |
return r; |
| 470 |
if (r > 0) |
| 471 |
return false; |
| 472 |
|
| 473 |
/* For loopback block device, especially for v5.19 or newer. Even if this is enabled, we also need to |
| 474 |
* check GENHD_FL_NO_PART flag through 'ext_range' and 'capability' sysfs attributes below. */ |
| 475 |
if (device_get_sysattr_bool(dev, "loop/partscan") == 0) |
| 476 |
return false; |
| 477 |
|
| 478 |
r = device_get_sysattr_int(dev, "ext_range", &ext_range); |
| 479 |
if (r == -ENOENT) /* If the ext_range file doesn't exist then we are most likely looking at a |
| 480 |
* partition block device, not the whole block device. And that means we have no |
| 481 |
* partition scanning on for it (we do for its parent, but not for the partition |
| 482 |
* itself). */ |
| 483 |
return false; |
| 484 |
if (r < 0) |
| 485 |
return r; |
| 486 |
|
| 487 |
if (ext_range <= 1) /* The value should be always positive, but the kernel uses '%d' for the |
| 488 |
* attribute. Let's gracefully handle zero or negative. */ |
| 489 |
return false; |
| 490 |
|
| 491 |
r = device_get_sysattr_unsigned_full(dev, "capability", 16, &capability); |
| 492 |
if (r == -ENOENT) |
| 493 |
return false; |
| 494 |
if (r < 0) |
| 495 |
return r; |
| 496 |
|
| 497 |
#define GENHD_FL_NO_PART_OLD 0x0200 |
| 498 |
#define GENHD_FL_NO_PART_NEW 0x0004 |
| 499 |
/* If one of the NO_PART flags is set, part scanning is definitely off. */ |
| 500 |
if ((capability & (GENHD_FL_NO_PART_OLD | GENHD_FL_NO_PART_NEW)) != 0) |
| 501 |
return false; |
| 502 |
|
| 503 |
/* Otherwise, assume part scanning is on, we have no further checks available. Assume the best. */ |
| 504 |
return true; |
| 505 |
} |
| 506 |
|
| 507 |
int blockdev_partscan_enabled_fd(int fd) { |
| 508 |
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
| 509 |
int r; |
| 510 |
|
| 511 |
assert(fd >= 0); |
| 512 |
|
| 513 |
r = block_device_new_from_fd(fd, 0, &dev); |
| 514 |
if (r < 0) |
| 515 |
return r; |
| 516 |
|
| 517 |
return blockdev_partscan_enabled(dev); |
| 518 |
} |
| 519 |
|
| 520 |
static int blockdev_is_encrypted(const char *sysfs_path, unsigned depth_left) { |
| 521 |
_cleanup_free_ char *p = NULL, *uuids = NULL; |
| 522 |
_cleanup_closedir_ DIR *d = NULL; |
| 523 |
int r, found_encrypted = false; |
| 524 |
|
| 525 |
assert(sysfs_path); |
| 526 |
|
| 527 |
if (depth_left == 0) |
| 528 |
return -EINVAL; |
| 529 |
|
| 530 |
p = path_join(sysfs_path, "dm/uuid"); |
| 531 |
if (!p) |
| 532 |
return -ENOMEM; |
| 533 |
|
| 534 |
r = read_one_line_file(p, &uuids); |
| 535 |
if (r != -ENOENT) { |
| 536 |
if (r < 0) |
| 537 |
return r; |
| 538 |
|
| 539 |
/* The DM device's uuid attribute is prefixed with "CRYPT-" if this is a dm-crypt device. */ |
| 540 |
if (startswith(uuids, "CRYPT-")) |
| 541 |
return true; |
| 542 |
} |
| 543 |
|
| 544 |
/* Not a dm-crypt device itself. But maybe it is on top of one? Follow the links in the "slaves/" |
| 545 |
* subdir. */ |
| 546 |
|
| 547 |
p = mfree(p); |
| 548 |
p = path_join(sysfs_path, "slaves"); |
| 549 |
if (!p) |
| 550 |
return -ENOMEM; |
| 551 |
|
| 552 |
d = opendir(p); |
| 553 |
if (!d) { |
| 554 |
if (errno == ENOENT) /* Doesn't have underlying devices */ |
| 555 |
return false; |
| 556 |
|
| 557 |
return -errno; |
| 558 |
} |
| 559 |
|
| 560 |
for (;;) { |
| 561 |
_cleanup_free_ char *q = NULL; |
| 562 |
struct dirent *de; |
| 563 |
|
| 564 |
errno = 0; |
| 565 |
de = readdir_no_dot(d); |
| 566 |
if (!de) { |
| 567 |
if (errno != 0) |
| 568 |
return -errno; |
| 569 |
|
| 570 |
break; /* No more underlying devices */ |
| 571 |
} |
| 572 |
|
| 573 |
q = path_join(p, de->d_name); |
| 574 |
if (!q) |
| 575 |
return -ENOMEM; |
| 576 |
|
| 577 |
r = blockdev_is_encrypted(q, depth_left - 1); |
| 578 |
if (r < 0) |
| 579 |
return r; |
| 580 |
if (r == 0) /* we found one that is not encrypted? then propagate that immediately */ |
| 581 |
return false; |
| 582 |
|
| 583 |
found_encrypted = true; |
| 584 |
} |
| 585 |
|
| 586 |
return found_encrypted; |
| 587 |
} |
| 588 |
|
| 589 |
int fd_is_encrypted(int fd) { |
| 590 |
char p[SYS_BLOCK_PATH_MAX("")]; |
| 591 |
dev_t devt; |
| 592 |
int r; |
| 593 |
|
| 594 |
r = get_block_device_fd(fd, &devt); |
| 595 |
if (r < 0) |
| 596 |
return r; |
| 597 |
if (r == 0) /* doesn't have a block device */ |
| 598 |
return false; |
| 599 |
|
| 600 |
xsprintf_sys_block_path(p, NULL, devt); |
| 601 |
|
| 602 |
return blockdev_is_encrypted(p, 10 /* safety net: maximum recursion depth */); |
| 603 |
} |
| 604 |
|
| 605 |
int path_is_encrypted(const char *path) { |
| 606 |
char p[SYS_BLOCK_PATH_MAX("")]; |
| 607 |
dev_t devt; |
| 608 |
int r; |
| 609 |
|
| 610 |
r = get_block_device(path, &devt); |
| 611 |
if (r < 0) |
| 612 |
return r; |
| 613 |
if (r == 0) /* doesn't have a block device */ |
| 614 |
return false; |
| 615 |
|
| 616 |
xsprintf_sys_block_path(p, NULL, devt); |
| 617 |
|
| 618 |
return blockdev_is_encrypted(p, 10 /* safety net: maximum recursion depth */); |
| 619 |
} |
| 620 |
|
| 621 |
int fd_get_whole_disk(int fd, bool backing, dev_t *ret) { |
| 622 |
dev_t devt; |
| 623 |
int r; |
| 624 |
|
| 625 |
assert(fd >= 0); |
| 626 |
assert(ret); |
| 627 |
|
| 628 |
r = fd_get_devnum(fd, backing ? BLOCK_DEVICE_LOOKUP_BACKING : 0, &devt); |
| 629 |
if (r < 0) |
| 630 |
return r; |
| 631 |
|
| 632 |
return block_get_whole_disk(devt, ret); |
| 633 |
} |
| 634 |
|
| 635 |
int path_get_whole_disk(const char *path, bool backing, dev_t *ret) { |
| 636 |
_cleanup_close_ int fd = -EBADF; |
| 637 |
|
| 638 |
fd = open(path, O_CLOEXEC|O_PATH); |
| 639 |
if (fd < 0) |
| 640 |
return -errno; |
| 641 |
|
| 642 |
return fd_get_whole_disk(fd, backing, ret); |
| 643 |
} |
| 644 |
|
| 645 |
int block_device_add_partition(int fd, int nr, uint64_t start, uint64_t size) { |
| 646 |
|
| 647 |
assert(fd >= 0); |
| 648 |
assert(nr > 0); |
| 649 |
|
| 650 |
struct blkpg_partition bp = { |
| 651 |
.pno = nr, |
| 652 |
.start = start, |
| 653 |
.length = size, |
| 654 |
}; |
| 655 |
|
| 656 |
struct blkpg_ioctl_arg ba = { |
| 657 |
.op = BLKPG_ADD_PARTITION, |
| 658 |
.data = &bp, |
| 659 |
.datalen = sizeof(bp), |
| 660 |
}; |
| 661 |
|
| 662 |
return RET_NERRNO(ioctl(fd, BLKPG, &ba)); |
| 663 |
} |
| 664 |
|
| 665 |
int block_device_remove_partition(int fd, int nr) { |
| 666 |
|
| 667 |
assert(fd >= 0); |
| 668 |
assert(nr > 0); |
| 669 |
|
| 670 |
struct blkpg_partition bp = { |
| 671 |
.pno = nr, |
| 672 |
}; |
| 673 |
|
| 674 |
struct blkpg_ioctl_arg ba = { |
| 675 |
.op = BLKPG_DEL_PARTITION, |
| 676 |
.data = &bp, |
| 677 |
.datalen = sizeof(bp), |
| 678 |
}; |
| 679 |
|
| 680 |
return RET_NERRNO(ioctl(fd, BLKPG, &ba)); |
| 681 |
} |
| 682 |
|
| 683 |
int block_device_resize_partition( |
| 684 |
int fd, |
| 685 |
int nr, |
| 686 |
uint64_t start, |
| 687 |
uint64_t size) { |
| 688 |
|
| 689 |
assert(fd >= 0); |
| 690 |
assert(nr > 0); |
| 691 |
|
| 692 |
struct blkpg_partition bp = { |
| 693 |
.pno = nr, |
| 694 |
.start = start, |
| 695 |
.length = size, |
| 696 |
}; |
| 697 |
|
| 698 |
struct blkpg_ioctl_arg ba = { |
| 699 |
.op = BLKPG_RESIZE_PARTITION, |
| 700 |
.data = &bp, |
| 701 |
.datalen = sizeof(bp), |
| 702 |
}; |
| 703 |
|
| 704 |
return RET_NERRNO(ioctl(fd, BLKPG, &ba)); |
| 705 |
} |
| 706 |
|
| 707 |
int partition_enumerator_new(sd_device *dev, sd_device_enumerator **ret) { |
| 708 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 709 |
const char *s; |
| 710 |
int r; |
| 711 |
|
| 712 |
assert(dev); |
| 713 |
assert(ret); |
| 714 |
|
| 715 |
/* Refuse invocation on partition block device, insist on "whole" device */ |
| 716 |
r = block_device_is_whole_disk(dev); |
| 717 |
if (r < 0) |
| 718 |
return r; |
| 719 |
if (r == 0) |
| 720 |
return -ENXIO; /* return a recognizable error */ |
| 721 |
|
| 722 |
r = sd_device_enumerator_new(&e); |
| 723 |
if (r < 0) |
| 724 |
return r; |
| 725 |
|
| 726 |
r = sd_device_enumerator_allow_uninitialized(e); |
| 727 |
if (r < 0) |
| 728 |
return r; |
| 729 |
|
| 730 |
r = sd_device_enumerator_add_match_parent(e, dev); |
| 731 |
if (r < 0) |
| 732 |
return r; |
| 733 |
|
| 734 |
r = sd_device_get_sysname(dev, &s); |
| 735 |
if (r < 0) |
| 736 |
return r; |
| 737 |
|
| 738 |
/* Also add sysname check for safety. Hopefully, this also improves performance. */ |
| 739 |
s = strjoina(s, "*"); |
| 740 |
r = sd_device_enumerator_add_match_sysname(e, s); |
| 741 |
if (r < 0) |
| 742 |
return r; |
| 743 |
|
| 744 |
r = sd_device_enumerator_add_match_subsystem(e, "block", /* match= */ true); |
| 745 |
if (r < 0) |
| 746 |
return r; |
| 747 |
|
| 748 |
r = sd_device_enumerator_add_match_property(e, "DEVTYPE", "partition"); |
| 749 |
if (r < 0) |
| 750 |
return r; |
| 751 |
|
| 752 |
*ret = TAKE_PTR(e); |
| 753 |
return 0; |
| 754 |
} |
| 755 |
|
| 756 |
int block_device_remove_all_partitions(sd_device *dev, int fd) { |
| 757 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 758 |
_cleanup_(sd_device_unrefp) sd_device *dev_unref = NULL; |
| 759 |
_cleanup_close_ int fd_close = -EBADF; |
| 760 |
bool has_partitions = false; |
| 761 |
int r, k = 0; |
| 762 |
|
| 763 |
assert(dev || fd >= 0); |
| 764 |
|
| 765 |
if (!dev) { |
| 766 |
r = block_device_new_from_fd(fd, 0, &dev_unref); |
| 767 |
if (r < 0) |
| 768 |
return r; |
| 769 |
|
| 770 |
dev = dev_unref; |
| 771 |
} |
| 772 |
|
| 773 |
r = partition_enumerator_new(dev, &e); |
| 774 |
if (r < 0) |
| 775 |
return r; |
| 776 |
|
| 777 |
if (fd < 0) { |
| 778 |
fd_close = sd_device_open(dev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_RDONLY); |
| 779 |
if (fd_close < 0) |
| 780 |
return fd_close; |
| 781 |
|
| 782 |
fd = fd_close; |
| 783 |
} |
| 784 |
|
| 785 |
FOREACH_DEVICE(e, part) { |
| 786 |
const char *v, *devname; |
| 787 |
int nr; |
| 788 |
|
| 789 |
has_partitions = true; |
| 790 |
|
| 791 |
r = sd_device_get_devname(part, &devname); |
| 792 |
if (r < 0) |
| 793 |
return r; |
| 794 |
|
| 795 |
r = sd_device_get_property_value(part, "PARTN", &v); |
| 796 |
if (r < 0) |
| 797 |
return r; |
| 798 |
|
| 799 |
r = safe_atoi(v, &nr); |
| 800 |
if (r < 0) |
| 801 |
return r; |
| 802 |
|
| 803 |
r = btrfs_forget_device(devname); |
| 804 |
if (r < 0 && r != -ENOENT) |
| 805 |
log_debug_errno(r, "Failed to forget btrfs device %s, ignoring: %m", devname); |
| 806 |
|
| 807 |
r = block_device_remove_partition(fd, nr); |
| 808 |
if (r == -ENODEV) { |
| 809 |
log_debug("Kernel removed partition %s before us, ignoring", devname); |
| 810 |
continue; |
| 811 |
} |
| 812 |
if (r < 0) { |
| 813 |
log_debug_errno(r, "Failed to remove partition %s: %m", devname); |
| 814 |
k = k < 0 ? k : r; |
| 815 |
continue; |
| 816 |
} |
| 817 |
|
| 818 |
log_debug("Removed partition %s", devname); |
| 819 |
} |
| 820 |
|
| 821 |
return k < 0 ? k : has_partitions; |
| 822 |
} |
| 823 |
|
| 824 |
int blockdev_get_sector_size(int fd, uint32_t *ret) { |
| 825 |
int ssz = 0; |
| 826 |
|
| 827 |
assert(fd >= 0); |
| 828 |
assert(ret); |
| 829 |
|
| 830 |
if (ioctl(fd, BLKSSZGET, &ssz) < 0) |
| 831 |
return -errno; |
| 832 |
if (ssz <= 0) /* make sure the field is initialized */ |
| 833 |
return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Block device reported invalid sector size %i.", ssz); |
| 834 |
|
| 835 |
*ret = ssz; |
| 836 |
return 0; |
| 837 |
} |
| 838 |
|
| 839 |
int blockdev_get_device_size(int fd, uint64_t *ret) { |
| 840 |
uint64_t sz = 0; |
| 841 |
|
| 842 |
assert(fd >= 0); |
| 843 |
assert(ret); |
| 844 |
|
| 845 |
/* This is just a type-safe wrapper around BLKGETSIZE64 that gets us around having to include messy linux/fs.h in various clients */ |
| 846 |
|
| 847 |
if (ioctl(fd, BLKGETSIZE64, &sz) < 0) |
| 848 |
return -errno; |
| 849 |
|
| 850 |
*ret = sz; |
| 851 |
return 0; |
| 852 |
} |
| 853 |
|
| 854 |
int blockdev_get_root(int level, dev_t *ret) { |
| 855 |
_cleanup_free_ char *p = NULL; |
| 856 |
dev_t devno; |
| 857 |
int r; |
| 858 |
|
| 859 |
/* Returns the device node backing the root file system. Traces through |
| 860 |
* dm-crypt/dm-verity/... Returns > 0 and the devno of the device on success. If there's no block |
| 861 |
* device (or multiple) returns 0 and a devno of 0. Failure otherwise. |
| 862 |
* |
| 863 |
* If the root mount has been replaced by some form of volatile file system (overlayfs), the original |
| 864 |
* root block device node is symlinked in /run/systemd/volatile-root. Let's read that here. */ |
| 865 |
r = readlink_malloc("/run/systemd/volatile-root", &p); |
| 866 |
if (r == -ENOENT) { /* volatile-root not found */ |
| 867 |
r = get_block_device_harder("/", &devno); |
| 868 |
if (r == -EUCLEAN) |
| 869 |
return btrfs_log_dev_root(level, r, "root file system"); |
| 870 |
if (r < 0) |
| 871 |
return log_full_errno(level, r, "Failed to determine block device of root file system: %m"); |
| 872 |
if (r == 0) { /* Not backed by a single block device. (Could be NFS or so, or could be multi-device RAID or so) */ |
| 873 |
r = get_block_device_harder("/usr", &devno); |
| 874 |
if (r == -EUCLEAN) |
| 875 |
return btrfs_log_dev_root(level, r, "/usr"); |
| 876 |
if (r < 0) |
| 877 |
return log_full_errno(level, r, "Failed to determine block device of /usr/ file system: %m"); |
| 878 |
if (r == 0) { /* /usr/ not backed by single block device, either. */ |
| 879 |
log_debug("Neither root nor /usr/ file system are on a (single) block device."); |
| 880 |
|
| 881 |
if (ret) |
| 882 |
*ret = 0; |
| 883 |
|
| 884 |
return 0; |
| 885 |
} |
| 886 |
} |
| 887 |
} else if (r < 0) |
| 888 |
return log_full_errno(level, r, "Failed to read symlink /run/systemd/volatile-root: %m"); |
| 889 |
else { |
| 890 |
mode_t m; |
| 891 |
r = device_path_parse_major_minor(p, &m, &devno); |
| 892 |
if (r < 0) |
| 893 |
return log_full_errno(level, r, "Failed to parse major/minor device node: %m"); |
| 894 |
if (!S_ISBLK(m)) |
| 895 |
return log_full_errno(level, SYNTHETIC_ERRNO(ENOTBLK), "Volatile root device is of wrong type."); |
| 896 |
} |
| 897 |
|
| 898 |
if (ret) |
| 899 |
*ret = devno; |
| 900 |
|
| 901 |
return 1; |
| 902 |
} |
| 903 |
|
| 904 |
int partition_node_of(const char *node, unsigned nr, char **ret) { |
| 905 |
_cleanup_free_ char *fn = NULL, *dn = NULL; |
| 906 |
int r; |
| 907 |
|
| 908 |
assert(node); |
| 909 |
assert(nr > 0); |
| 910 |
assert(ret); |
| 911 |
|
| 912 |
/* Given a device node path to a block device returns the device node path to the partition block |
| 913 |
* device of the specified partition */ |
| 914 |
|
| 915 |
r = path_split_prefix_filename(node, &dn, &fn); |
| 916 |
if (r < 0) |
| 917 |
return r; |
| 918 |
if (r == O_DIRECTORY) |
| 919 |
return -EISDIR; |
| 920 |
|
| 921 |
size_t l = strlen(fn); |
| 922 |
assert(l > 0); /* underflow check for the subtraction below */ |
| 923 |
|
| 924 |
bool need_p = ascii_isdigit(fn[l-1]); /* Last char a digit? */ |
| 925 |
|
| 926 |
_cleanup_free_ char *subnode = NULL; |
| 927 |
if (asprintf(&subnode, "%s%s%u", fn, need_p ? "p" : "", nr) < 0) |
| 928 |
return -ENOMEM; |
| 929 |
|
| 930 |
if (dn) { |
| 931 |
_cleanup_free_ char *j = path_join(dn, subnode); |
| 932 |
if (!j) |
| 933 |
return -ENOMEM; |
| 934 |
|
| 935 |
*ret = TAKE_PTR(j); |
| 936 |
} else |
| 937 |
*ret = TAKE_PTR(subnode); |
| 938 |
|
| 939 |
return 0; |
| 940 |
} |