]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
18c528e9 | 2 | |
ac83e5ae | 3 | #include <sys/file.h> |
f5947a5e | 4 | #include <unistd.h> |
18c528e9 LP |
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" | |
f5947a5e | 12 | #include "missing_magic.h" |
3a47c40d | 13 | #include "parse-util.h" |
18c528e9 LP |
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; | |
3a47c40d | 19 | dev_t devt; |
18c528e9 LP |
20 | int r; |
21 | ||
22 | assert(ret); | |
23 | ||
51f14fa1 LP |
24 | if (major(d) == 0) |
25 | return -ENODEV; | |
26 | ||
18c528e9 LP |
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 | } | |
6cba41ab LP |
33 | if (errno != ENOENT) |
34 | return -errno; | |
18c528e9 LP |
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) | |
d18b3009 | 39 | return -errno; |
18c528e9 LP |
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 | ||
3a47c40d LP |
47 | r = parse_dev(s, &devt); |
48 | if (r < 0) | |
49 | return r; | |
18c528e9 LP |
50 | |
51 | /* Only return this if it is really good enough for us. */ | |
3a47c40d | 52 | xsprintf_sys_block_path(p, "/queue", devt); |
18c528e9 | 53 | if (access(p, F_OK) < 0) |
d18b3009 | 54 | return -errno; |
18c528e9 | 55 | |
3a47c40d | 56 | *ret = devt; |
a0ea1dee | 57 | return 1; |
18c528e9 LP |
58 | } |
59 | ||
db8728a6 LP |
60 | int get_block_device(const char *path, dev_t *ret) { |
61 | _cleanup_close_ int fd = -1; | |
18c528e9 | 62 | struct stat st; |
db8728a6 | 63 | int r; |
18c528e9 LP |
64 | |
65 | assert(path); | |
db8728a6 | 66 | assert(ret); |
18c528e9 | 67 | |
db8728a6 LP |
68 | /* Gets the block device directly backing a file system. If the block device is encrypted, returns |
69 | * the device mapper block device. */ | |
70 | ||
86b86107 | 71 | fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC); |
db8728a6 LP |
72 | if (fd < 0) |
73 | return -errno; | |
18c528e9 | 74 | |
db8728a6 | 75 | if (fstat(fd, &st)) |
18c528e9 LP |
76 | return -errno; |
77 | ||
78 | if (major(st.st_dev) != 0) { | |
db8728a6 | 79 | *ret = st.st_dev; |
18c528e9 LP |
80 | return 1; |
81 | } | |
82 | ||
db8728a6 LP |
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; | |
18c528e9 | 88 | |
db8728a6 | 89 | *ret = 0; |
18c528e9 LP |
90 | return 0; |
91 | } | |
92 | ||
880f09bd | 93 | int block_get_originating(dev_t dt, dev_t *ret) { |
18c528e9 LP |
94 | _cleanup_closedir_ DIR *d = NULL; |
95 | _cleanup_free_ char *t = NULL; | |
96 | char p[SYS_BLOCK_PATH_MAX("/slaves")]; | |
c68fc351 LP |
97 | _cleanup_free_ char *first_found = NULL; |
98 | struct dirent *de; | |
880f09bd | 99 | const char *q; |
3a47c40d | 100 | dev_t devt; |
18c528e9 LP |
101 | int r; |
102 | ||
880f09bd LP |
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. */ | |
18c528e9 LP |
105 | |
106 | xsprintf_sys_block_path(p, "/slaves", dt); | |
107 | d = opendir(p); | |
880f09bd | 108 | if (!d) |
18c528e9 | 109 | return -errno; |
18c528e9 LP |
110 | |
111 | FOREACH_DIRENT_ALL(de, d, return -errno) { | |
112 | ||
113 | if (dot_or_dot_dot(de->d_name)) | |
114 | continue; | |
115 | ||
116 | if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN)) | |
117 | continue; | |
118 | ||
c68fc351 | 119 | if (first_found) { |
18c528e9 LP |
120 | _cleanup_free_ char *u = NULL, *v = NULL, *a = NULL, *b = NULL; |
121 | ||
c68fc351 LP |
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. */ | |
18c528e9 | 129 | |
657ee2d8 | 130 | u = path_join(p, de->d_name, "../dev"); |
18c528e9 LP |
131 | if (!u) |
132 | return -ENOMEM; | |
133 | ||
c68fc351 | 134 | v = path_join(p, first_found, "../dev"); |
18c528e9 LP |
135 | if (!v) |
136 | return -ENOMEM; | |
137 | ||
138 | r = read_one_line_file(u, &a); | |
880f09bd LP |
139 | if (r < 0) |
140 | return log_debug_errno(r, "Failed to read %s: %m", u); | |
18c528e9 LP |
141 | |
142 | r = read_one_line_file(v, &b); | |
880f09bd LP |
143 | if (r < 0) |
144 | return log_debug_errno(r, "Failed to read %s: %m", v); | |
18c528e9 LP |
145 | |
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. */ | |
148 | if (!streq(a, b)) | |
880f09bd | 149 | return -ENOTUNIQ; |
c68fc351 LP |
150 | } else { |
151 | first_found = strdup(de->d_name); | |
152 | if (!first_found) | |
153 | return -ENOMEM; | |
18c528e9 | 154 | } |
18c528e9 LP |
155 | } |
156 | ||
c68fc351 | 157 | if (!first_found) |
880f09bd | 158 | return -ENOENT; |
18c528e9 | 159 | |
c68fc351 | 160 | q = strjoina(p, "/", first_found, "/dev"); |
18c528e9 LP |
161 | |
162 | r = read_one_line_file(q, &t); | |
18c528e9 LP |
163 | if (r < 0) |
164 | return r; | |
165 | ||
3a47c40d LP |
166 | r = parse_dev(t, &devt); |
167 | if (r < 0) | |
18c528e9 LP |
168 | return -EINVAL; |
169 | ||
3a47c40d | 170 | if (major(devt) == 0) |
880f09bd | 171 | return -ENOENT; |
18c528e9 | 172 | |
3a47c40d | 173 | *ret = devt; |
18c528e9 | 174 | return 1; |
880f09bd LP |
175 | } |
176 | ||
177 | int get_block_device_harder(const char *path, dev_t *ret) { | |
178 | int r; | |
179 | ||
180 | assert(path); | |
181 | assert(ret); | |
182 | ||
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. */ | |
185 | ||
186 | r = get_block_device(path, ret); | |
187 | if (r <= 0) | |
188 | return r; | |
189 | ||
190 | r = block_get_originating(*ret, ret); | |
191 | if (r < 0) | |
192 | log_debug_errno(r, "Failed to chase block device '%s', ignoring: %m", path); | |
18c528e9 | 193 | |
18c528e9 LP |
194 | return 1; |
195 | } | |
ac83e5ae LP |
196 | |
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; | |
200 | dev_t whole_devt; | |
201 | int r; | |
202 | ||
203 | /* Let's get a BSD file lock on the whole block device, as per: https://systemd.io/BLOCK_DEVICE_LOCKING */ | |
204 | ||
205 | r = block_get_whole_disk(devt, &whole_devt); | |
206 | if (r < 0) | |
207 | return r; | |
208 | ||
209 | r = device_path_make_major_minor(S_IFBLK, whole_devt, &whole_node); | |
210 | if (r < 0) | |
211 | return r; | |
212 | ||
213 | lock_fd = open(whole_node, O_RDONLY|O_CLOEXEC|O_NONBLOCK); | |
214 | if (lock_fd < 0) | |
215 | return -errno; | |
216 | ||
217 | if (flock(lock_fd, operation) < 0) | |
218 | return -errno; | |
219 | ||
220 | return TAKE_FD(lock_fd); | |
221 | } | |
e8467cd3 LP |
222 | |
223 | int blockdev_partscan_enabled(int fd) { | |
224 | _cleanup_free_ char *p = NULL, *buf = NULL; | |
225 | unsigned long long ull; | |
226 | struct stat st; | |
227 | int r; | |
228 | ||
229 | /* Checks if partition scanning is correctly enabled on the block device */ | |
230 | ||
231 | if (fstat(fd, &st) < 0) | |
232 | return -errno; | |
233 | ||
234 | if (!S_ISBLK(st.st_mode)) | |
235 | return -ENOTBLK; | |
236 | ||
237 | if (asprintf(&p, "/sys/dev/block/%u:%u/capability", major(st.st_rdev), minor(st.st_rdev)) < 0) | |
238 | return -ENOMEM; | |
239 | ||
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 | |
244 | * itself). */ | |
245 | return false; | |
246 | if (r < 0) | |
247 | return r; | |
248 | ||
249 | r = safe_atollu_full(buf, 16, &ull); | |
250 | if (r < 0) | |
251 | return r; | |
252 | ||
253 | #ifndef GENHD_FL_NO_PART_SCAN | |
254 | #define GENHD_FL_NO_PART_SCAN (0x0200) | |
255 | #endif | |
256 | ||
257 | return !FLAGS_SET(ull, GENHD_FL_NO_PART_SCAN); | |
258 | } |