]>
Commit | Line | Data |
---|---|---|
18c528e9 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
18c528e9 LP |
2 | |
3 | #include <sys/stat.h> | |
4 | #include <sys/statfs.h> | |
f5947a5e | 5 | #include <unistd.h> |
18c528e9 LP |
6 | |
7 | #include "alloc-util.h" | |
8 | #include "blockdev-util.h" | |
9 | #include "btrfs-util.h" | |
10 | #include "dirent-util.h" | |
11 | #include "fd-util.h" | |
12 | #include "fileio.h" | |
f5947a5e | 13 | #include "missing_magic.h" |
3a47c40d | 14 | #include "parse-util.h" |
18c528e9 LP |
15 | #include "stat-util.h" |
16 | ||
17 | int block_get_whole_disk(dev_t d, dev_t *ret) { | |
18 | char p[SYS_BLOCK_PATH_MAX("/partition")]; | |
19 | _cleanup_free_ char *s = NULL; | |
3a47c40d | 20 | dev_t devt; |
18c528e9 LP |
21 | int r; |
22 | ||
23 | assert(ret); | |
24 | ||
51f14fa1 LP |
25 | if (major(d) == 0) |
26 | return -ENODEV; | |
27 | ||
18c528e9 LP |
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) | |
d18b3009 | 38 | return -errno; |
18c528e9 LP |
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 | ||
3a47c40d LP |
46 | r = parse_dev(s, &devt); |
47 | if (r < 0) | |
48 | return r; | |
18c528e9 LP |
49 | |
50 | /* Only return this if it is really good enough for us. */ | |
3a47c40d | 51 | xsprintf_sys_block_path(p, "/queue", devt); |
18c528e9 | 52 | if (access(p, F_OK) < 0) |
d18b3009 | 53 | return -errno; |
18c528e9 | 54 | |
3a47c40d | 55 | *ret = devt; |
a0ea1dee | 56 | return 1; |
18c528e9 LP |
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 | ||
5238e957 | 66 | /* Gets the block device directly backing a file system. If |
18c528e9 LP |
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 | ||
66ae5130 | 84 | *dev = 0; |
18c528e9 LP |
85 | return 0; |
86 | } | |
87 | ||
880f09bd | 88 | int block_get_originating(dev_t dt, dev_t *ret) { |
18c528e9 LP |
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; | |
880f09bd | 93 | const char *q; |
3a47c40d | 94 | dev_t devt; |
18c528e9 LP |
95 | int r; |
96 | ||
880f09bd LP |
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. */ | |
18c528e9 LP |
99 | |
100 | xsprintf_sys_block_path(p, "/slaves", dt); | |
101 | d = opendir(p); | |
880f09bd | 102 | if (!d) |
18c528e9 | 103 | return -errno; |
18c528e9 LP |
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 | ||
657ee2d8 | 122 | u = path_join(p, de->d_name, "../dev"); |
18c528e9 LP |
123 | if (!u) |
124 | return -ENOMEM; | |
125 | ||
657ee2d8 | 126 | v = path_join(p, found->d_name, "../dev"); |
18c528e9 LP |
127 | if (!v) |
128 | return -ENOMEM; | |
129 | ||
130 | r = read_one_line_file(u, &a); | |
880f09bd LP |
131 | if (r < 0) |
132 | return log_debug_errno(r, "Failed to read %s: %m", u); | |
18c528e9 LP |
133 | |
134 | r = read_one_line_file(v, &b); | |
880f09bd LP |
135 | if (r < 0) |
136 | return log_debug_errno(r, "Failed to read %s: %m", v); | |
18c528e9 LP |
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)) | |
880f09bd | 141 | return -ENOTUNIQ; |
18c528e9 LP |
142 | } |
143 | ||
144 | found = de; | |
145 | } | |
146 | ||
147 | if (!found) | |
880f09bd | 148 | return -ENOENT; |
18c528e9 LP |
149 | |
150 | q = strjoina(p, "/", found->d_name, "/dev"); | |
151 | ||
152 | r = read_one_line_file(q, &t); | |
18c528e9 LP |
153 | if (r < 0) |
154 | return r; | |
155 | ||
3a47c40d LP |
156 | r = parse_dev(t, &devt); |
157 | if (r < 0) | |
18c528e9 LP |
158 | return -EINVAL; |
159 | ||
3a47c40d | 160 | if (major(devt) == 0) |
880f09bd | 161 | return -ENOENT; |
18c528e9 | 162 | |
3a47c40d | 163 | *ret = devt; |
18c528e9 | 164 | return 1; |
880f09bd LP |
165 | } |
166 | ||
167 | int get_block_device_harder(const char *path, dev_t *ret) { | |
168 | int r; | |
169 | ||
170 | assert(path); | |
171 | assert(ret); | |
172 | ||
173 | /* Gets the backing block device for a file system, and handles LUKS encrypted file systems, looking for its | |
174 | * immediate parent, if there is one. */ | |
175 | ||
176 | r = get_block_device(path, ret); | |
177 | if (r <= 0) | |
178 | return r; | |
179 | ||
180 | r = block_get_originating(*ret, ret); | |
181 | if (r < 0) | |
182 | log_debug_errno(r, "Failed to chase block device '%s', ignoring: %m", path); | |
18c528e9 | 183 | |
18c528e9 LP |
184 | return 1; |
185 | } |