]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/blockdev-util.c
5372c26be688988341d1b442cd03d743d60b6dff
[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 *dev) {
58 struct stat st;
59 struct statfs sfs;
60
61 assert(path);
62 assert(dev);
63
64 /* Gets the block device directly backing a file system. If
65 * the block device is encrypted, returns the device mapper
66 * block device. */
67
68 if (lstat(path, &st))
69 return -errno;
70
71 if (major(st.st_dev) != 0) {
72 *dev = st.st_dev;
73 return 1;
74 }
75
76 if (statfs(path, &sfs) < 0)
77 return -errno;
78
79 if (F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC))
80 return btrfs_get_block_device(path, dev);
81
82 *dev = 0;
83 return 0;
84 }
85
86 int block_get_originating(dev_t dt, dev_t *ret) {
87 _cleanup_closedir_ DIR *d = NULL;
88 _cleanup_free_ char *t = NULL;
89 char p[SYS_BLOCK_PATH_MAX("/slaves")];
90 struct dirent *de, *found = NULL;
91 const char *q;
92 dev_t devt;
93 int r;
94
95 /* For the specified block device tries to chase it through the layers, in case LUKS-style DM stacking is used,
96 * trying to find the next underlying layer. */
97
98 xsprintf_sys_block_path(p, "/slaves", dt);
99 d = opendir(p);
100 if (!d)
101 return -errno;
102
103 FOREACH_DIRENT_ALL(de, d, return -errno) {
104
105 if (dot_or_dot_dot(de->d_name))
106 continue;
107
108 if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN))
109 continue;
110
111 if (found) {
112 _cleanup_free_ char *u = NULL, *v = NULL, *a = NULL, *b = NULL;
113
114 /* We found a device backed by multiple other devices. We don't really support automatic
115 * discovery on such setups, with the exception of dm-verity partitions. In this case there are
116 * two backing devices: the data partition and the hash partition. We are fine with such
117 * setups, however, only if both partitions are on the same physical device. Hence, let's
118 * verify this. */
119
120 u = path_join(p, de->d_name, "../dev");
121 if (!u)
122 return -ENOMEM;
123
124 v = path_join(p, found->d_name, "../dev");
125 if (!v)
126 return -ENOMEM;
127
128 r = read_one_line_file(u, &a);
129 if (r < 0)
130 return log_debug_errno(r, "Failed to read %s: %m", u);
131
132 r = read_one_line_file(v, &b);
133 if (r < 0)
134 return log_debug_errno(r, "Failed to read %s: %m", v);
135
136 /* Check if the parent device is the same. If not, then the two backing devices are on
137 * different physical devices, and we don't support that. */
138 if (!streq(a, b))
139 return -ENOTUNIQ;
140 }
141
142 found = de;
143 }
144
145 if (!found)
146 return -ENOENT;
147
148 q = strjoina(p, "/", found->d_name, "/dev");
149
150 r = read_one_line_file(q, &t);
151 if (r < 0)
152 return r;
153
154 r = parse_dev(t, &devt);
155 if (r < 0)
156 return -EINVAL;
157
158 if (major(devt) == 0)
159 return -ENOENT;
160
161 *ret = devt;
162 return 1;
163 }
164
165 int get_block_device_harder(const char *path, dev_t *ret) {
166 int r;
167
168 assert(path);
169 assert(ret);
170
171 /* Gets the backing block device for a file system, and handles LUKS encrypted file systems, looking for its
172 * immediate parent, if there is one. */
173
174 r = get_block_device(path, ret);
175 if (r <= 0)
176 return r;
177
178 r = block_get_originating(*ret, ret);
179 if (r < 0)
180 log_debug_errno(r, "Failed to chase block device '%s', ignoring: %m", path);
181
182 return 1;
183 }