Debian Codegraph @

libblkid/src/superblocks/zfs.c

raw
1 /*
2 * Copyright (C) 2009-2010 by Andreas Dilger <[email protected]>
3 *
4 * This file may be redistributed under the terms of the
5 * GNU Lesser General Public License.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <ctype.h>
14 #include <inttypes.h>
15 #include <limits.h>
16 #include <stdbool.h>
17
18 #include "superblocks.h"
19
20 #define VDEV_LABEL_NVPAIR ( 16 * 1024ULL)
21 #define VDEV_LABEL_SIZE (256 * 1024ULL)
22 #define VDEV_PHYS_SIZE (112 * 1024ULL)
23 #define VDEV_LABELS 4
24 #define ZFS_MINDEVSIZE (64ULL << 20)
25 #define DATA_TYPE_UNKNOWN 0
26 #define DATA_TYPE_UINT64 8
27 #define DATA_TYPE_STRING 9
28 #define DATA_TYPE_DIRECTORY 19
29
30 typedef enum pool_state {
31 POOL_STATE_ACTIVE = 0, /* In active use */
32 POOL_STATE_EXPORTED, /* Explicitly exported */
33 POOL_STATE_DESTROYED, /* Explicitly destroyed */
34 POOL_STATE_SPARE, /* Reserved for hot spare use */
35 POOL_STATE_L2CACHE, /* Level 2 ARC device */
36 POOL_STATE_UNINITIALIZED, /* Internal spa_t state */
37 POOL_STATE_UNAVAIL, /* Internal libzfs state */
38 POOL_STATE_POTENTIALLY_ACTIVE /* Internal libzfs state */
39 } pool_state_t;
40
41 struct nvs_header_t {
42 char nvh_encoding; /* encoding method */
43 char nvh_endian; /* endianness */
44 char nvh_reserved1;
45 char nvh_reserved2;
46 uint32_t nvh_reserved3;
47 uint32_t nvh_reserved4;
48 uint32_t nvh_first_size; /* first nvpair encode size */
49 };
50
51 struct nvpair {
52 uint32_t nvp_size;
53 uint32_t nvp_unkown;
54 uint32_t nvp_namelen;
55 char nvp_name[0]; /* aligned to 4 bytes */
56 /* aligned ptr array for string arrays */
57 /* aligned array of data for value */
58 };
59
60 struct nvstring {
61 uint32_t nvs_type;
62 uint32_t nvs_elem;
63 uint32_t nvs_strlen;
64 unsigned char nvs_string[0];
65 };
66
67 struct nvuint64 {
68 uint32_t nvu_type;
69 uint32_t nvu_elem;
70 uint64_t nvu_value;
71 } __attribute__((packed));
72
73 struct nvdirectory {
74 uint32_t nvd_type;
75 uint32_t nvd_unknown[3];
76 };
77
78 struct nvlist {
79 uint32_t nvl_unknown[3];
80 struct nvpair nvl_nvpair;
81 };
82
83 /*
84 * Helper function to safely read uint32_t from potentially unaligned memory.
85 * This is needed for architectures with strict alignment requirements (e.g., SPARC).
86 */
87 static inline uint32_t copy_be32_to_cpu(const void *ptr)
88 {
89 uint32_t val;
90 memcpy(&val, ptr, sizeof(val));
91 return be32_to_cpu(val);
92 }
93
94 /*
95 * Return the offset of the given label.
96 */
97 static uint64_t
98 label_offset(uint64_t size, int l)
99 {
100 loff_t blk_align = (size % VDEV_LABEL_SIZE);
101 return (l * VDEV_LABEL_SIZE + (l < VDEV_LABELS / 2 ?
102 0 : size - VDEV_LABELS * VDEV_LABEL_SIZE - blk_align));
103 }
104
105 static bool zfs_process_value(blkid_probe pr, const char *name, size_t namelen,
106 const void *value, size_t max_value_size, unsigned directory_level, int *found)
107 {
108 uint32_t type = copy_be32_to_cpu(value);
109 if (strncmp(name, "name", namelen) == 0 &&
110 type == DATA_TYPE_STRING && !directory_level) {
111 const struct nvstring *nvs = value;
112 if (max_value_size < sizeof(struct nvstring))
113 return (false);
114 uint32_t nvs_strlen = copy_be32_to_cpu(&nvs->nvs_strlen);
115 if ((uint64_t)nvs_strlen + sizeof(*nvs) > max_value_size)
116 return (false);
117
118 DBG(LOWPROBE, ul_debug("nvstring: type %u string %.*s",
119 type, (int)nvs_strlen, nvs->nvs_string));
120
121 blkid_probe_set_label(pr, nvs->nvs_string, nvs_strlen);
122 (*found)++;
123 } else if (strncmp(name, "guid", namelen) == 0 &&
124 type == DATA_TYPE_UINT64 && !directory_level) {
125 const struct nvuint64 *nvu = value;
126 uint64_t nvu_value;
127
128 if (max_value_size < sizeof(struct nvuint64))
129 return (false);
130
131 memcpy(&nvu_value, &nvu->nvu_value, sizeof(nvu_value));
132 nvu_value = be64_to_cpu(nvu_value);
133
134 DBG(LOWPROBE, ul_debug("nvuint64: type %u value %"PRIu64,
135 type, nvu_value));
136
137 blkid_probe_sprintf_value(pr, "UUID_SUB",
138 "%"PRIu64, nvu_value);
139 (*found)++;
140 } else if (strncmp(name, "pool_guid", namelen) == 0 &&
141 type == DATA_TYPE_UINT64 && !directory_level) {
142 const struct nvuint64 *nvu = value;
143 uint64_t nvu_value;
144
145 if (max_value_size < sizeof(struct nvuint64))
146 return (false);
147
148 memcpy(&nvu_value, &nvu->nvu_value, sizeof(nvu_value));
149 nvu_value = be64_to_cpu(nvu_value);
150
151 DBG(LOWPROBE, ul_debug("nvuint64: type %u value %"PRIu64,
152 type, nvu_value));
153
154 blkid_probe_sprintf_uuid(pr, (unsigned char *) &nvu_value,
155 sizeof(nvu_value),
156 "%"PRIu64, nvu_value);
157 (*found)++;
158 } else if (strncmp(name, "ashift", namelen) == 0 &&
159 type == DATA_TYPE_UINT64) {
160 const struct nvuint64 *nvu = value;
161 uint64_t nvu_value;
162
163 if (max_value_size < sizeof(struct nvuint64))
164 return (false);
165
166 memcpy(&nvu_value, &nvu->nvu_value, sizeof(nvu_value));
167 nvu_value = be64_to_cpu(nvu_value);
168
169 if (nvu_value < 32){
170 blkid_probe_set_fsblocksize(pr, 1U << nvu_value);
171 blkid_probe_set_block_size(pr, 1U << nvu_value);
172 }
173 (*found)++;
174 } else if (strncmp(name, "version", namelen) == 0 &&
175 type == DATA_TYPE_UINT64 && !directory_level) {
176 const struct nvuint64 *nvu = value;
177 uint64_t nvu_value;
178 if (max_value_size < sizeof(struct nvuint64))
179 return (false);
180 memcpy(&nvu_value, &nvu->nvu_value, sizeof(nvu_value));
181 nvu_value = be64_to_cpu(nvu_value);
182 DBG(LOWPROBE, ul_debug("nvuint64: type %u value %"PRIu64,
183 type, nvu_value));
184 blkid_probe_sprintf_version(pr, "%" PRIu64, nvu_value);
185 (*found)++;
186 }
187 return (true);
188 }
189
190 static bool zfs_extract_guid_name(blkid_probe pr, void *buf, size_t size, bool find_label)
191 {
192 const struct nvlist *nvl;
193 const struct nvpair *nvp;
194 unsigned directory_level = 0;
195 uint64_t state = -1, guid = 0, txg = 0;
196 nvl = (const struct nvlist *)buf;
197 nvp = &nvl->nvl_nvpair;
198 int found = 0;
199
200 /* Already used up 12 bytes */
201 size -= (const unsigned char *)nvp - (const unsigned char *)buf;
202
203 while (size > sizeof(*nvp)) {
204 uint32_t nvp_size = copy_be32_to_cpu(&nvp->nvp_size);
205 uint32_t nvp_namelen = copy_be32_to_cpu(&nvp->nvp_namelen);
206 uint64_t namesize = ((uint64_t)nvp_namelen + 3) & ~3;
207 size_t max_value_size;
208 const void *value;
209 uint32_t type;
210
211 if (!nvp_size) {
212 if (!directory_level)
213 /*
214 * End of nvlist!
215 */
216 break;
217 directory_level--;
218 nvp_size = 8;
219 goto cont;
220 }
221
222 DBG(LOWPROBE, ul_debug("left %zd nvp_size %u",
223 size, nvp_size));
224
225 /* nvpair fits in buffer and name fits in nvpair? */
226 if (nvp_size > size || namesize + sizeof(*nvp) + 4 > nvp_size)
227 return (false);
228
229 DBG(LOWPROBE,
230 ul_debug("nvlist: size %u, namelen %u, name %.*s",
231 nvp_size, nvp_namelen, (int)nvp_namelen,
232 nvp->nvp_name));
233
234 max_value_size = nvp_size - (namesize + sizeof(*nvp));
235 value = nvp->nvp_name + namesize;
236 type = copy_be32_to_cpu(value);
237
238 if (type == DATA_TYPE_UNKNOWN)
239 return (false);
240
241 if (type == DATA_TYPE_DIRECTORY) {
242 if (max_value_size < sizeof(struct nvdirectory))
243 return (false);
244 const struct nvdirectory *nvu = value;
245 nvp_size = sizeof(*nvp) + namesize + sizeof(*nvu);
246 directory_level++;
247 goto cont;
248 }
249
250 if (find_label) {
251 /*
252 * We don't need to parse any tree to find a label
253 */
254 if (directory_level)
255 goto cont;
256 const struct nvuint64 *nvu = value;
257 if (!strncmp(nvp->nvp_name, "guid", nvp_namelen) &&
258 type == DATA_TYPE_UINT64) {
259 if (max_value_size < sizeof(struct nvuint64))
260 return (false);
261 memcpy(&guid, &nvu->nvu_value, sizeof(nvu->nvu_value));
262 guid = be64_to_cpu(guid);
263 } else if (!strncmp(nvp->nvp_name, "state", nvp_namelen) &&
264 type == DATA_TYPE_UINT64) {
265 if (max_value_size < sizeof(struct nvuint64))
266 return (false);
267 memcpy(&state, &nvu->nvu_value, sizeof(nvu->nvu_value));
268 state = be64_to_cpu(state);
269 } else if (!strncmp(nvp->nvp_name, "txg", nvp_namelen) &&
270 type == DATA_TYPE_UINT64) {
271 if (max_value_size < sizeof(struct nvuint64))
272 return (false);
273 memcpy(&txg, &nvu->nvu_value, sizeof(nvu->nvu_value));
274 txg = be64_to_cpu(txg);
275 }
276 } else {
277 if (zfs_process_value(pr, nvp->nvp_name, nvp_namelen, value,
278 max_value_size,directory_level, &found) == false || found >= 5)
279 return (false);
280 }
281
282 cont:
283 if (nvp_size > size)
284 return (false);
285 size -= nvp_size;
286
287 nvp = (struct nvpair *)((char *)nvp + nvp_size);
288 }
289 if (find_label && guid && state <= POOL_STATE_POTENTIALLY_ACTIVE && (state ==
290 POOL_STATE_L2CACHE || state == POOL_STATE_SPARE || txg > 0))
291 return (true);
292 return (false);
293 }
294
295 /* ZFS has 128x1kB host-endian root blocks, stored in 2 areas at the start
296 * of the disk, and 2 areas at the end of the disk. Check only some of them...
297 * #4 (@ 132kB) is the first one written on a new filesystem. */
298 static int probe_zfs(blkid_probe pr,
299 const struct blkid_idmag *mag __attribute__((__unused__)))
300 {
301 #if BYTE_ORDER == LITTLE_ENDIAN
302 int host_endian = 1;
303 #else
304 int host_endian = 0;
305 #endif
306 int swap_endian = 0;
307 loff_t offset = 0;
308 int label_no;
309 struct nvs_header_t *label = NULL;
310 bool found_label = false;
311
312 DBG(PROBE, ul_debug("probe_zfs"));
313
314 if (pr->size < ZFS_MINDEVSIZE)
315 return (1);
316
317 /* Look for at least one valid label to ensure a positive match */
318 for (label_no = 0; label_no < 4; label_no++) {
319 offset = label_offset(pr->size, label_no) + VDEV_LABEL_NVPAIR;
320
321 if ((S_ISREG(pr->mode) || blkid_probe_is_wholedisk(pr)) &&
322 blkid_probe_is_covered_by_pt(pr, offset, VDEV_PHYS_SIZE))
323 /* ignore this area, it's within any partition and
324 * we are working with whole-disk now */
325 continue;
326
327 label = (struct nvs_header_t *) blkid_probe_get_buffer(pr, offset, VDEV_PHYS_SIZE);
328
329 /*
330 * Label supports XDR encoding, reject for any other unsupported format. Also
331 * endianness can be 0 or 1, reject garbage value. Moreover, check if first
332 * nvpair encode size is non-zero.
333 */
334 if (!label || label->nvh_encoding != 0x1 || !copy_be32_to_cpu(&label->nvh_first_size) ||
335 (unsigned char) label->nvh_endian > 0x1)
336 continue;
337
338 if (host_endian != label->nvh_endian)
339 swap_endian = 1;
340
341 if (zfs_extract_guid_name(pr, label, VDEV_PHYS_SIZE, true)) {
342 found_label = true;
343 break;
344 }
345 }
346
347 if (!label || !found_label)
348 return (1);
349
350 (void) zfs_extract_guid_name(pr, label, VDEV_PHYS_SIZE, false);
351
352 /*
353 * Zero out whole nvlist header including first nvpair size
354 */
355 if (blkid_probe_set_magic(pr, offset, sizeof(struct nvs_header_t),
356 (unsigned char *) label))
357 return (1);
358
359 blkid_probe_set_fsendianness(pr, !swap_endian ?
360 BLKID_ENDIANNESS_NATIVE : BLKID_ENDIANNESS_OTHER);
361
362 return (0);
363 }
364
365 const struct blkid_idinfo zfs_idinfo =
366 {
367 .name = "zfs_member",
368 .usage = BLKID_USAGE_FILESYSTEM,
369 .probefunc = probe_zfs,
370 .minsz = 64 * 1024 * 1024,
371 .magics = BLKID_NONE_MAGIC
372 };