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