]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/blockdev-util.c
blockdev-util: rework get_block_device()
[thirdparty/systemd.git] / src / basic / blockdev-util.c
CommitLineData
18c528e9 1/* SPDX-License-Identifier: LGPL-2.1+ */
18c528e9 2
f5947a5e 3#include <unistd.h>
18c528e9
LP
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"
f5947a5e 11#include "missing_magic.h"
3a47c40d 12#include "parse-util.h"
18c528e9
LP
13#include "stat-util.h"
14
15int block_get_whole_disk(dev_t d, dev_t *ret) {
16 char p[SYS_BLOCK_PATH_MAX("/partition")];
17 _cleanup_free_ char *s = NULL;
3a47c40d 18 dev_t devt;
18c528e9
LP
19 int r;
20
21 assert(ret);
22
51f14fa1
LP
23 if (major(d) == 0)
24 return -ENODEV;
25
18c528e9
LP
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)
d18b3009 36 return -errno;
18c528e9
LP
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
3a47c40d
LP
44 r = parse_dev(s, &devt);
45 if (r < 0)
46 return r;
18c528e9
LP
47
48 /* Only return this if it is really good enough for us. */
3a47c40d 49 xsprintf_sys_block_path(p, "/queue", devt);
18c528e9 50 if (access(p, F_OK) < 0)
d18b3009 51 return -errno;
18c528e9 52
3a47c40d 53 *ret = devt;
a0ea1dee 54 return 1;
18c528e9
LP
55}
56
db8728a6
LP
57int get_block_device(const char *path, dev_t *ret) {
58 _cleanup_close_ int fd = -1;
18c528e9 59 struct stat st;
db8728a6 60 int r;
18c528e9
LP
61
62 assert(path);
db8728a6 63 assert(ret);
18c528e9 64
db8728a6
LP
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;
18c528e9 71
db8728a6 72 if (fstat(fd, &st))
18c528e9
LP
73 return -errno;
74
75 if (major(st.st_dev) != 0) {
db8728a6 76 *ret = st.st_dev;
18c528e9
LP
77 return 1;
78 }
79
db8728a6
LP
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;
18c528e9 85
db8728a6 86 *ret = 0;
18c528e9
LP
87 return 0;
88}
89
880f09bd 90int block_get_originating(dev_t dt, dev_t *ret) {
18c528e9
LP
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;
880f09bd 95 const char *q;
3a47c40d 96 dev_t devt;
18c528e9
LP
97 int r;
98
880f09bd
LP
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. */
18c528e9
LP
101
102 xsprintf_sys_block_path(p, "/slaves", dt);
103 d = opendir(p);
880f09bd 104 if (!d)
18c528e9 105 return -errno;
18c528e9
LP
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
657ee2d8 124 u = path_join(p, de->d_name, "../dev");
18c528e9
LP
125 if (!u)
126 return -ENOMEM;
127
657ee2d8 128 v = path_join(p, found->d_name, "../dev");
18c528e9
LP
129 if (!v)
130 return -ENOMEM;
131
132 r = read_one_line_file(u, &a);
880f09bd
LP
133 if (r < 0)
134 return log_debug_errno(r, "Failed to read %s: %m", u);
18c528e9
LP
135
136 r = read_one_line_file(v, &b);
880f09bd
LP
137 if (r < 0)
138 return log_debug_errno(r, "Failed to read %s: %m", v);
18c528e9
LP
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))
880f09bd 143 return -ENOTUNIQ;
18c528e9
LP
144 }
145
146 found = de;
147 }
148
149 if (!found)
880f09bd 150 return -ENOENT;
18c528e9
LP
151
152 q = strjoina(p, "/", found->d_name, "/dev");
153
154 r = read_one_line_file(q, &t);
18c528e9
LP
155 if (r < 0)
156 return r;
157
3a47c40d
LP
158 r = parse_dev(t, &devt);
159 if (r < 0)
18c528e9
LP
160 return -EINVAL;
161
3a47c40d 162 if (major(devt) == 0)
880f09bd 163 return -ENOENT;
18c528e9 164
3a47c40d 165 *ret = devt;
18c528e9 166 return 1;
880f09bd
LP
167}
168
169int 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);
18c528e9 185
18c528e9
LP
186 return 1;
187}