]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/blockdev-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
7 #include "blockdev-util.h"
8 #include "btrfs-util.h"
9 #include "dirent-util.h"
12 #include "missing_magic.h"
13 #include "parse-util.h"
14 #include "stat-util.h"
16 int block_get_whole_disk(dev_t d
, dev_t
*ret
) {
17 char p
[SYS_BLOCK_PATH_MAX("/partition")];
18 _cleanup_free_
char *s
= NULL
;
27 /* If it has a queue this is good enough for us */
28 xsprintf_sys_block_path(p
, "/queue", d
);
29 if (access(p
, F_OK
) >= 0) {
36 /* If it is a partition find the originating device */
37 xsprintf_sys_block_path(p
, "/partition", d
);
38 if (access(p
, F_OK
) < 0)
41 /* Get parent dev_t */
42 xsprintf_sys_block_path(p
, "/../dev", d
);
43 r
= read_one_line_file(p
, &s
);
47 r
= parse_dev(s
, &devt
);
51 /* Only return this if it is really good enough for us. */
52 xsprintf_sys_block_path(p
, "/queue", devt
);
53 if (access(p
, F_OK
) < 0)
60 int get_block_device(const char *path
, dev_t
*ret
) {
61 _cleanup_close_
int fd
= -1;
68 /* Gets the block device directly backing a file system. If the block device is encrypted, returns
69 * the device mapper block device. */
71 fd
= open(path
, O_RDONLY
|O_NOFOLLOW
|O_CLOEXEC
);
78 if (major(st
.st_dev
) != 0) {
83 r
= btrfs_get_block_device_fd(fd
, ret
);
86 if (r
!= -ENOTTY
) /* not btrfs */
93 int block_get_originating(dev_t dt
, dev_t
*ret
) {
94 _cleanup_closedir_
DIR *d
= NULL
;
95 _cleanup_free_
char *t
= NULL
;
96 char p
[SYS_BLOCK_PATH_MAX("/slaves")];
97 _cleanup_free_
char *first_found
= NULL
;
103 /* For the specified block device tries to chase it through the layers, in case LUKS-style DM stacking is used,
104 * trying to find the next underlying layer. */
106 xsprintf_sys_block_path(p
, "/slaves", dt
);
111 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
113 if (dot_or_dot_dot(de
->d_name
))
116 if (!IN_SET(de
->d_type
, DT_LNK
, DT_UNKNOWN
))
120 _cleanup_free_
char *u
= NULL
, *v
= NULL
, *a
= NULL
, *b
= NULL
;
122 /* We found a device backed by multiple other devices. We don't really support
123 * automatic discovery on such setups, with the exception of dm-verity partitions. In
124 * this case there are two backing devices: the data partition and the hash
125 * partition. We are fine with such setups, however, only if both partitions are on
126 * the same physical device. Hence, let's verify this by iterating over every node
127 * in the 'slaves/' directory and comparing them with the first that gets returned by
128 * readdir(), to ensure they all point to the same device. */
130 u
= path_join(p
, de
->d_name
, "../dev");
134 v
= path_join(p
, first_found
, "../dev");
138 r
= read_one_line_file(u
, &a
);
140 return log_debug_errno(r
, "Failed to read %s: %m", u
);
142 r
= read_one_line_file(v
, &b
);
144 return log_debug_errno(r
, "Failed to read %s: %m", v
);
146 /* Check if the parent device is the same. If not, then the two backing devices are on
147 * different physical devices, and we don't support that. */
151 first_found
= strdup(de
->d_name
);
160 q
= strjoina(p
, "/", first_found
, "/dev");
162 r
= read_one_line_file(q
, &t
);
166 r
= parse_dev(t
, &devt
);
170 if (major(devt
) == 0)
177 int get_block_device_harder(const char *path
, dev_t
*ret
) {
183 /* Gets the backing block device for a file system, and handles LUKS encrypted file systems, looking for its
184 * immediate parent, if there is one. */
186 r
= get_block_device(path
, ret
);
190 r
= block_get_originating(*ret
, ret
);
192 log_debug_errno(r
, "Failed to chase block device '%s', ignoring: %m", path
);
197 int lock_whole_block_device(dev_t devt
, int operation
) {
198 _cleanup_free_
char *whole_node
= NULL
;
199 _cleanup_close_
int lock_fd
= -1;
203 /* Let's get a BSD file lock on the whole block device, as per: https://systemd.io/BLOCK_DEVICE_LOCKING */
205 r
= block_get_whole_disk(devt
, &whole_devt
);
209 r
= device_path_make_major_minor(S_IFBLK
, whole_devt
, &whole_node
);
213 lock_fd
= open(whole_node
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
217 if (flock(lock_fd
, operation
) < 0)
220 return TAKE_FD(lock_fd
);
223 int blockdev_partscan_enabled(int fd
) {
224 _cleanup_free_
char *p
= NULL
, *buf
= NULL
;
225 unsigned long long ull
;
229 /* Checks if partition scanning is correctly enabled on the block device */
231 if (fstat(fd
, &st
) < 0)
234 if (!S_ISBLK(st
.st_mode
))
237 if (asprintf(&p
, "/sys/dev/block/%u:%u/capability", major(st
.st_rdev
), minor(st
.st_rdev
)) < 0)
240 r
= read_one_line_file(p
, &buf
);
241 if (r
== -ENOENT
) /* If the capability file doesn't exist then we are most likely looking at a
242 * partition block device, not the whole block device. And that means we have no
243 * partition scanning on for it (we do for its parent, but not for the partition
249 r
= safe_atollu_full(buf
, 16, &ull
);
253 #ifndef GENHD_FL_NO_PART_SCAN
254 #define GENHD_FL_NO_PART_SCAN (0x0200)
257 return !FLAGS_SET(ull
, GENHD_FL_NO_PART_SCAN
);