]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/blockdev-util.c
blockdev-util: rework get_block_device()
[thirdparty/systemd.git] / src / basic / blockdev-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <unistd.h>
4
5 #include "alloc-util.h"
6 #include "blockdev-util.h"
7 #include "btrfs-util.h"
8 #include "dirent-util.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "missing_magic.h"
12 #include "parse-util.h"
13 #include "stat-util.h"
14
15 int block_get_whole_disk(dev_t d, dev_t *ret) {
16 char p[SYS_BLOCK_PATH_MAX("/partition")];
17 _cleanup_free_ char *s = NULL;
18 dev_t devt;
19 int r;
20
21 assert(ret);
22
23 if (major(d) == 0)
24 return -ENODEV;
25
26 /* If it has a queue this is good enough for us */
27 xsprintf_sys_block_path(p, "/queue", d);
28 if (access(p, F_OK) >= 0) {
29 *ret = d;
30 return 0;
31 }
32
33 /* If it is a partition find the originating device */
34 xsprintf_sys_block_path(p, "/partition", d);
35 if (access(p, F_OK) < 0)
36 return -errno;
37
38 /* Get parent dev_t */
39 xsprintf_sys_block_path(p, "/../dev", d);
40 r = read_one_line_file(p, &s);
41 if (r < 0)
42 return r;
43
44 r = parse_dev(s, &devt);
45 if (r < 0)
46 return r;
47
48 /* Only return this if it is really good enough for us. */
49 xsprintf_sys_block_path(p, "/queue", devt);
50 if (access(p, F_OK) < 0)
51 return -errno;
52
53 *ret = devt;
54 return 1;
55 }
56
57 int get_block_device(const char *path, dev_t *ret) {
58 _cleanup_close_ int fd = -1;
59 struct stat st;
60 int r;
61
62 assert(path);
63 assert(ret);
64
65 /* Gets the block device directly backing a file system. If the block device is encrypted, returns
66 * the device mapper block device. */
67
68 fd = open(path, O_NOFOLLOW|O_CLOEXEC);
69 if (fd < 0)
70 return -errno;
71
72 if (fstat(fd, &st))
73 return -errno;
74
75 if (major(st.st_dev) != 0) {
76 *ret = st.st_dev;
77 return 1;
78 }
79
80 r = btrfs_get_block_device_fd(fd, ret);
81 if (r > 0)
82 return 1;
83 if (r != -ENOTTY) /* not btrfs */
84 return r;
85
86 *ret = 0;
87 return 0;
88 }
89
90 int block_get_originating(dev_t dt, dev_t *ret) {
91 _cleanup_closedir_ DIR *d = NULL;
92 _cleanup_free_ char *t = NULL;
93 char p[SYS_BLOCK_PATH_MAX("/slaves")];
94 struct dirent *de, *found = NULL;
95 const char *q;
96 dev_t devt;
97 int r;
98
99 /* For the specified block device tries to chase it through the layers, in case LUKS-style DM stacking is used,
100 * trying to find the next underlying layer. */
101
102 xsprintf_sys_block_path(p, "/slaves", dt);
103 d = opendir(p);
104 if (!d)
105 return -errno;
106
107 FOREACH_DIRENT_ALL(de, d, return -errno) {
108
109 if (dot_or_dot_dot(de->d_name))
110 continue;
111
112 if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN))
113 continue;
114
115 if (found) {
116 _cleanup_free_ char *u = NULL, *v = NULL, *a = NULL, *b = NULL;
117
118 /* We found a device backed by multiple other devices. We don't really support automatic
119 * discovery on such setups, with the exception of dm-verity partitions. In this case there are
120 * two backing devices: the data partition and the hash partition. We are fine with such
121 * setups, however, only if both partitions are on the same physical device. Hence, let's
122 * verify this. */
123
124 u = path_join(p, de->d_name, "../dev");
125 if (!u)
126 return -ENOMEM;
127
128 v = path_join(p, found->d_name, "../dev");
129 if (!v)
130 return -ENOMEM;
131
132 r = read_one_line_file(u, &a);
133 if (r < 0)
134 return log_debug_errno(r, "Failed to read %s: %m", u);
135
136 r = read_one_line_file(v, &b);
137 if (r < 0)
138 return log_debug_errno(r, "Failed to read %s: %m", v);
139
140 /* Check if the parent device is the same. If not, then the two backing devices are on
141 * different physical devices, and we don't support that. */
142 if (!streq(a, b))
143 return -ENOTUNIQ;
144 }
145
146 found = de;
147 }
148
149 if (!found)
150 return -ENOENT;
151
152 q = strjoina(p, "/", found->d_name, "/dev");
153
154 r = read_one_line_file(q, &t);
155 if (r < 0)
156 return r;
157
158 r = parse_dev(t, &devt);
159 if (r < 0)
160 return -EINVAL;
161
162 if (major(devt) == 0)
163 return -ENOENT;
164
165 *ret = devt;
166 return 1;
167 }
168
169 int get_block_device_harder(const char *path, dev_t *ret) {
170 int r;
171
172 assert(path);
173 assert(ret);
174
175 /* Gets the backing block device for a file system, and handles LUKS encrypted file systems, looking for its
176 * immediate parent, if there is one. */
177
178 r = get_block_device(path, ret);
179 if (r <= 0)
180 return r;
181
182 r = block_get_originating(*ret, ret);
183 if (r < 0)
184 log_debug_errno(r, "Failed to chase block device '%s', ignoring: %m", path);
185
186 return 1;
187 }