]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/blockdev-util.c
Merge pull request #8863 from evelikov/shell-completion-fixes
[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 *dev = 0;
85 return 0;
86 }
87
88 int block_get_originating(dev_t dt, dev_t *ret) {
89 _cleanup_closedir_ DIR *d = NULL;
90 _cleanup_free_ char *t = NULL;
91 char p[SYS_BLOCK_PATH_MAX("/slaves")];
92 struct dirent *de, *found = NULL;
93 unsigned maj, min;
94 const char *q;
95 int r;
96
97 /* For the specified block device tries to chase it through the layers, in case LUKS-style DM stacking is used,
98 * trying to find the next underlying layer. */
99
100 xsprintf_sys_block_path(p, "/slaves", dt);
101 d = opendir(p);
102 if (!d)
103 return -errno;
104
105 FOREACH_DIRENT_ALL(de, d, return -errno) {
106
107 if (dot_or_dot_dot(de->d_name))
108 continue;
109
110 if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN))
111 continue;
112
113 if (found) {
114 _cleanup_free_ char *u = NULL, *v = NULL, *a = NULL, *b = NULL;
115
116 /* We found a device backed by multiple other devices. We don't really support automatic
117 * discovery on such setups, with the exception of dm-verity partitions. In this case there are
118 * two backing devices: the data partition and the hash partition. We are fine with such
119 * setups, however, only if both partitions are on the same physical device. Hence, let's
120 * verify this. */
121
122 u = strjoin(p, "/", de->d_name, "/../dev");
123 if (!u)
124 return -ENOMEM;
125
126 v = strjoin(p, "/", found->d_name, "/../dev");
127 if (!v)
128 return -ENOMEM;
129
130 r = read_one_line_file(u, &a);
131 if (r < 0)
132 return log_debug_errno(r, "Failed to read %s: %m", u);
133
134 r = read_one_line_file(v, &b);
135 if (r < 0)
136 return log_debug_errno(r, "Failed to read %s: %m", v);
137
138 /* Check if the parent device is the same. If not, then the two backing devices are on
139 * different physical devices, and we don't support that. */
140 if (!streq(a, b))
141 return -ENOTUNIQ;
142 }
143
144 found = de;
145 }
146
147 if (!found)
148 return -ENOENT;
149
150 q = strjoina(p, "/", found->d_name, "/dev");
151
152 r = read_one_line_file(q, &t);
153 if (r < 0)
154 return r;
155
156 if (sscanf(t, "%u:%u", &maj, &min) != 2)
157 return -EINVAL;
158
159 if (maj == 0)
160 return -ENOENT;
161
162 *ret = makedev(maj, min);
163 return 1;
164 }
165
166 int 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);
182
183 return 1;
184 }