1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <linux/blkpg.h>
10 #include "alloc-util.h"
11 #include "blockdev-util.h"
12 #include "btrfs-util.h"
13 #include "device-util.h"
14 #include "devnum-util.h"
15 #include "dirent-util.h"
16 #include "errno-util.h"
19 #include "missing_magic.h"
20 #include "parse-util.h"
22 int block_get_whole_disk(dev_t d
, dev_t
*ret
) {
23 char p
[SYS_BLOCK_PATH_MAX("/partition")];
24 _cleanup_free_
char *s
= NULL
;
33 /* If it has a queue this is good enough for us */
34 xsprintf_sys_block_path(p
, "/queue", d
);
35 if (access(p
, F_OK
) >= 0) {
42 /* If it is a partition find the originating device */
43 xsprintf_sys_block_path(p
, "/partition", d
);
44 if (access(p
, F_OK
) < 0)
47 /* Get parent dev_t */
48 xsprintf_sys_block_path(p
, "/../dev", d
);
49 r
= read_one_line_file(p
, &s
);
53 r
= parse_devnum(s
, &devt
);
57 /* Only return this if it is really good enough for us. */
58 xsprintf_sys_block_path(p
, "/queue", devt
);
59 if (access(p
, F_OK
) < 0)
66 int get_block_device_fd(int fd
, dev_t
*ret
) {
73 /* Gets the block device directly backing a file system. If the block device is encrypted, returns
74 * the device mapper block device. */
79 if (major(st
.st_dev
) != 0) {
84 r
= btrfs_get_block_device_fd(fd
, ret
);
87 if (r
!= -ENOTTY
) /* not btrfs */
94 int get_block_device(const char *path
, dev_t
*ret
) {
95 _cleanup_close_
int fd
= -1;
100 fd
= open(path
, O_RDONLY
|O_NOFOLLOW
|O_CLOEXEC
);
104 return get_block_device_fd(fd
, ret
);
107 int block_get_originating(dev_t dt
, dev_t
*ret
) {
108 _cleanup_closedir_
DIR *d
= NULL
;
109 _cleanup_free_
char *t
= NULL
;
110 char p
[SYS_BLOCK_PATH_MAX("/slaves")];
111 _cleanup_free_
char *first_found
= NULL
;
116 /* For the specified block device tries to chase it through the layers, in case LUKS-style DM stacking is used,
117 * trying to find the next underlying layer. */
119 xsprintf_sys_block_path(p
, "/slaves", dt
);
124 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
126 if (dot_or_dot_dot(de
->d_name
))
129 if (!IN_SET(de
->d_type
, DT_LNK
, DT_UNKNOWN
))
133 _cleanup_free_
char *u
= NULL
, *v
= NULL
, *a
= NULL
, *b
= NULL
;
135 /* We found a device backed by multiple other devices. We don't really support
136 * automatic discovery on such setups, with the exception of dm-verity partitions. In
137 * this case there are two backing devices: the data partition and the hash
138 * partition. We are fine with such setups, however, only if both partitions are on
139 * the same physical device. Hence, let's verify this by iterating over every node
140 * in the 'slaves/' directory and comparing them with the first that gets returned by
141 * readdir(), to ensure they all point to the same device. */
143 u
= path_join(p
, de
->d_name
, "../dev");
147 v
= path_join(p
, first_found
, "../dev");
151 r
= read_one_line_file(u
, &a
);
153 return log_debug_errno(r
, "Failed to read %s: %m", u
);
155 r
= read_one_line_file(v
, &b
);
157 return log_debug_errno(r
, "Failed to read %s: %m", v
);
159 /* Check if the parent device is the same. If not, then the two backing devices are on
160 * different physical devices, and we don't support that. */
164 first_found
= strdup(de
->d_name
);
173 q
= strjoina(p
, "/", first_found
, "/dev");
175 r
= read_one_line_file(q
, &t
);
179 r
= parse_devnum(t
, &devt
);
183 if (major(devt
) == 0)
190 int get_block_device_harder_fd(int fd
, dev_t
*ret
) {
196 /* Gets the backing block device for a file system, and handles LUKS encrypted file systems, looking for its
197 * immediate parent, if there is one. */
199 r
= get_block_device_fd(fd
, ret
);
203 r
= block_get_originating(*ret
, ret
);
205 log_debug_errno(r
, "Failed to chase block device, ignoring: %m");
210 int get_block_device_harder(const char *path
, dev_t
*ret
) {
211 _cleanup_close_
int fd
= -1;
216 fd
= open(path
, O_RDONLY
|O_NOFOLLOW
|O_CLOEXEC
);
220 return get_block_device_harder_fd(fd
, ret
);
223 int lock_whole_block_device(dev_t devt
, int operation
) {
224 _cleanup_free_
char *whole_node
= NULL
;
225 _cleanup_close_
int lock_fd
= -1;
229 /* Let's get a BSD file lock on the whole block device, as per: https://systemd.io/BLOCK_DEVICE_LOCKING */
231 r
= block_get_whole_disk(devt
, &whole_devt
);
235 r
= device_path_make_major_minor(S_IFBLK
, whole_devt
, &whole_node
);
239 lock_fd
= open(whole_node
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
243 if (flock(lock_fd
, operation
) < 0)
246 return TAKE_FD(lock_fd
);
249 int blockdev_partscan_enabled(int fd
) {
250 _cleanup_free_
char *p
= NULL
, *buf
= NULL
;
251 unsigned long long ull
;
255 /* Checks if partition scanning is correctly enabled on the block device */
257 if (fstat(fd
, &st
) < 0)
260 if (!S_ISBLK(st
.st_mode
))
263 if (asprintf(&p
, "/sys/dev/block/%u:%u/capability", major(st
.st_rdev
), minor(st
.st_rdev
)) < 0)
266 r
= read_one_line_file(p
, &buf
);
267 if (r
== -ENOENT
) /* If the capability file doesn't exist then we are most likely looking at a
268 * partition block device, not the whole block device. And that means we have no
269 * partition scanning on for it (we do for its parent, but not for the partition
275 r
= safe_atollu_full(buf
, 16, &ull
);
279 #ifndef GENHD_FL_NO_PART_SCAN
280 #define GENHD_FL_NO_PART_SCAN (0x0200)
283 return !FLAGS_SET(ull
, GENHD_FL_NO_PART_SCAN
);
286 static int blockdev_is_encrypted(const char *sysfs_path
, unsigned depth_left
) {
287 _cleanup_free_
char *p
= NULL
, *uuids
= NULL
;
288 _cleanup_closedir_
DIR *d
= NULL
;
289 int r
, found_encrypted
= false;
296 p
= path_join(sysfs_path
, "dm/uuid");
300 r
= read_one_line_file(p
, &uuids
);
305 /* The DM device's uuid attribute is prefixed with "CRYPT-" if this is a dm-crypt device. */
306 if (startswith(uuids
, "CRYPT-"))
310 /* Not a dm-crypt device itself. But maybe it is on top of one? Follow the links in the "slaves/"
314 p
= path_join(sysfs_path
, "slaves");
320 if (errno
== ENOENT
) /* Doesn't have underlying devices */
327 _cleanup_free_
char *q
= NULL
;
331 de
= readdir_no_dot(d
);
336 break; /* No more underlying devices */
339 q
= path_join(p
, de
->d_name
);
343 r
= blockdev_is_encrypted(q
, depth_left
- 1);
346 if (r
== 0) /* we found one that is not encrypted? then propagate that immediately */
349 found_encrypted
= true;
352 return found_encrypted
;
355 int fd_is_encrypted(int fd
) {
356 char p
[SYS_BLOCK_PATH_MAX(NULL
)];
360 r
= get_block_device_fd(fd
, &devt
);
363 if (r
== 0) /* doesn't have a block device */
366 xsprintf_sys_block_path(p
, NULL
, devt
);
368 return blockdev_is_encrypted(p
, 10 /* safety net: maximum recursion depth */);
371 int path_is_encrypted(const char *path
) {
372 char p
[SYS_BLOCK_PATH_MAX(NULL
)];
376 r
= get_block_device(path
, &devt
);
379 if (r
== 0) /* doesn't have a block device */
382 xsprintf_sys_block_path(p
, NULL
, devt
);
384 return blockdev_is_encrypted(p
, 10 /* safety net: maximum recursion depth */);
387 int fd_get_whole_disk(int fd
, bool backing
, dev_t
*ret
) {
394 if (fstat(fd
, &st
) < 0)
397 if (S_ISBLK(st
.st_mode
))
401 else if (!S_ISREG(st
.st_mode
) && !S_ISDIR(st
.st_mode
))
403 else if (major(st
.st_dev
) != 0)
406 _cleanup_close_
int regfd
= -1;
408 /* If major(st.st_dev) is zero, this might mean we are backed by btrfs, which needs special
409 * handing, to get the backing device node. */
411 regfd
= fd_reopen(fd
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
415 r
= btrfs_get_block_device_fd(regfd
, &devt
);
422 return block_get_whole_disk(devt
, ret
);
425 int path_get_whole_disk(const char *path
, bool backing
, dev_t
*ret
) {
426 _cleanup_close_
int fd
= -1;
428 fd
= open(path
, O_CLOEXEC
|O_PATH
);
432 return fd_get_whole_disk(fd
, backing
, ret
);
435 int block_device_add_partition(int fd
, const char *name
, int nr
, uint64_t start
, uint64_t size
) {
440 struct blkpg_partition bp
= {
446 struct blkpg_ioctl_arg ba
= {
447 .op
= BLKPG_ADD_PARTITION
,
449 .datalen
= sizeof(bp
),
452 if (strlen(name
) >= sizeof(bp
.devname
))
455 strcpy(bp
.devname
, name
);
457 return RET_NERRNO(ioctl(fd
, BLKPG
, &ba
));
460 int block_device_remove_partition(int fd
, const char *name
, int nr
) {
465 struct blkpg_partition bp
= {
469 struct blkpg_ioctl_arg ba
= {
470 .op
= BLKPG_DEL_PARTITION
,
472 .datalen
= sizeof(bp
),
475 if (strlen(name
) >= sizeof(bp
.devname
))
478 strcpy(bp
.devname
, name
);
480 return RET_NERRNO(ioctl(fd
, BLKPG
, &ba
));
483 int block_device_remove_all_partitions(int fd
) {
485 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
486 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
490 if (fstat(fd
, &stat
) < 0)
493 r
= sd_device_new_from_devnum(&dev
, 'b', stat
.st_rdev
);
497 r
= sd_device_enumerator_new(&e
);
501 r
= sd_device_enumerator_add_match_parent(e
, dev
);
505 r
= sd_device_enumerator_add_match_subsystem(e
, "block", true);
509 r
= sd_device_enumerator_add_match_property(e
, "DEVTYPE", "partition");
513 FOREACH_DEVICE(e
, part
) {
514 const char *v
, *devname
;
517 r
= sd_device_get_devname(part
, &devname
);
521 r
= sd_device_get_property_value(part
, "PARTN", &v
);
525 r
= safe_atoi(v
, &nr
);
529 r
= block_device_remove_partition(fd
, devname
, nr
);
531 log_debug("Kernel removed partition %s before us, ignoring", devname
);
535 log_debug_errno(r
, "Failed to remove partition %s: %m", devname
);
540 log_debug("Removed partition %s", devname
);