]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/blockdev-util.c
blockdev-util: add correct API for detecting if block device has partition scanning...
[thirdparty/systemd.git] / src / basic / blockdev-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <sys/file.h>
4 #include <unistd.h>
5
6 #include "alloc-util.h"
7 #include "blockdev-util.h"
8 #include "btrfs-util.h"
9 #include "dirent-util.h"
10 #include "fd-util.h"
11 #include "fileio.h"
12 #include "missing_magic.h"
13 #include "parse-util.h"
14 #include "stat-util.h"
15
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;
19 dev_t devt;
20 int r;
21
22 assert(ret);
23
24 if (major(d) == 0)
25 return -ENODEV;
26
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) {
30 *ret = d;
31 return 0;
32 }
33 if (errno != ENOENT)
34 return -errno;
35
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)
39 return -errno;
40
41 /* Get parent dev_t */
42 xsprintf_sys_block_path(p, "/../dev", d);
43 r = read_one_line_file(p, &s);
44 if (r < 0)
45 return r;
46
47 r = parse_dev(s, &devt);
48 if (r < 0)
49 return r;
50
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)
54 return -errno;
55
56 *ret = devt;
57 return 1;
58 }
59
60 int get_block_device(const char *path, dev_t *ret) {
61 _cleanup_close_ int fd = -1;
62 struct stat st;
63 int r;
64
65 assert(path);
66 assert(ret);
67
68 /* Gets the block device directly backing a file system. If the block device is encrypted, returns
69 * the device mapper block device. */
70
71 fd = open(path, O_NOFOLLOW|O_CLOEXEC);
72 if (fd < 0)
73 return -errno;
74
75 if (fstat(fd, &st))
76 return -errno;
77
78 if (major(st.st_dev) != 0) {
79 *ret = st.st_dev;
80 return 1;
81 }
82
83 r = btrfs_get_block_device_fd(fd, ret);
84 if (r > 0)
85 return 1;
86 if (r != -ENOTTY) /* not btrfs */
87 return r;
88
89 *ret = 0;
90 return 0;
91 }
92
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 struct dirent *de, *found = NULL;
98 const char *q;
99 dev_t devt;
100 int r;
101
102 /* For the specified block device tries to chase it through the layers, in case LUKS-style DM stacking is used,
103 * trying to find the next underlying layer. */
104
105 xsprintf_sys_block_path(p, "/slaves", dt);
106 d = opendir(p);
107 if (!d)
108 return -errno;
109
110 FOREACH_DIRENT_ALL(de, d, return -errno) {
111
112 if (dot_or_dot_dot(de->d_name))
113 continue;
114
115 if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN))
116 continue;
117
118 if (found) {
119 _cleanup_free_ char *u = NULL, *v = NULL, *a = NULL, *b = NULL;
120
121 /* We found a device backed by multiple other devices. We don't really support automatic
122 * discovery on such setups, with the exception of dm-verity partitions. In this case there are
123 * two backing devices: the data partition and the hash partition. We are fine with such
124 * setups, however, only if both partitions are on the same physical device. Hence, let's
125 * verify this. */
126
127 u = path_join(p, de->d_name, "../dev");
128 if (!u)
129 return -ENOMEM;
130
131 v = path_join(p, found->d_name, "../dev");
132 if (!v)
133 return -ENOMEM;
134
135 r = read_one_line_file(u, &a);
136 if (r < 0)
137 return log_debug_errno(r, "Failed to read %s: %m", u);
138
139 r = read_one_line_file(v, &b);
140 if (r < 0)
141 return log_debug_errno(r, "Failed to read %s: %m", v);
142
143 /* Check if the parent device is the same. If not, then the two backing devices are on
144 * different physical devices, and we don't support that. */
145 if (!streq(a, b))
146 return -ENOTUNIQ;
147 }
148
149 found = de;
150 }
151
152 if (!found)
153 return -ENOENT;
154
155 q = strjoina(p, "/", found->d_name, "/dev");
156
157 r = read_one_line_file(q, &t);
158 if (r < 0)
159 return r;
160
161 r = parse_dev(t, &devt);
162 if (r < 0)
163 return -EINVAL;
164
165 if (major(devt) == 0)
166 return -ENOENT;
167
168 *ret = devt;
169 return 1;
170 }
171
172 int get_block_device_harder(const char *path, dev_t *ret) {
173 int r;
174
175 assert(path);
176 assert(ret);
177
178 /* Gets the backing block device for a file system, and handles LUKS encrypted file systems, looking for its
179 * immediate parent, if there is one. */
180
181 r = get_block_device(path, ret);
182 if (r <= 0)
183 return r;
184
185 r = block_get_originating(*ret, ret);
186 if (r < 0)
187 log_debug_errno(r, "Failed to chase block device '%s', ignoring: %m", path);
188
189 return 1;
190 }
191
192 int lock_whole_block_device(dev_t devt, int operation) {
193 _cleanup_free_ char *whole_node = NULL;
194 _cleanup_close_ int lock_fd = -1;
195 dev_t whole_devt;
196 int r;
197
198 /* Let's get a BSD file lock on the whole block device, as per: https://systemd.io/BLOCK_DEVICE_LOCKING */
199
200 r = block_get_whole_disk(devt, &whole_devt);
201 if (r < 0)
202 return r;
203
204 r = device_path_make_major_minor(S_IFBLK, whole_devt, &whole_node);
205 if (r < 0)
206 return r;
207
208 lock_fd = open(whole_node, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
209 if (lock_fd < 0)
210 return -errno;
211
212 if (flock(lock_fd, operation) < 0)
213 return -errno;
214
215 return TAKE_FD(lock_fd);
216 }
217
218 int blockdev_partscan_enabled(int fd) {
219 _cleanup_free_ char *p = NULL, *buf = NULL;
220 unsigned long long ull;
221 struct stat st;
222 int r;
223
224 /* Checks if partition scanning is correctly enabled on the block device */
225
226 if (fstat(fd, &st) < 0)
227 return -errno;
228
229 if (!S_ISBLK(st.st_mode))
230 return -ENOTBLK;
231
232 if (asprintf(&p, "/sys/dev/block/%u:%u/capability", major(st.st_rdev), minor(st.st_rdev)) < 0)
233 return -ENOMEM;
234
235 r = read_one_line_file(p, &buf);
236 if (r == -ENOENT) /* If the capability file doesn't exist then we are most likely looking at a
237 * partition block device, not the whole block device. And that means we have no
238 * partition scanning on for it (we do for its parent, but not for the partition
239 * itself). */
240 return false;
241 if (r < 0)
242 return r;
243
244 r = safe_atollu_full(buf, 16, &ull);
245 if (r < 0)
246 return r;
247
248 #ifndef GENHD_FL_NO_PART_SCAN
249 #define GENHD_FL_NO_PART_SCAN (0x0200)
250 #endif
251
252 return !FLAGS_SET(ull, GENHD_FL_NO_PART_SCAN);
253 }