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