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