1 /*-
2 * Copyright (c) 2007 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 /*
28 * Stand-alone ZFS file reader.
29 */
30
31 #include <stdbool.h>
32 #include <sys/endian.h>
33 #include <sys/stat.h>
34 #include <sys/stdint.h>
35 #include <sys/list.h>
36 #include <sys/zfs_bootenv.h>
37 #include <machine/_inttypes.h>
38
39 #include "zfsimpl.h"
40 #include "zfssubr.c"
41
42 #ifdef HAS_ZSTD_ZFS
43 extern int zstd_init(void);
44 #endif
45
46 struct zfsmount {
47 char *path;
48 const spa_t *spa;
49 objset_phys_t objset;
50 uint64_t rootobj;
51 STAILQ_ENTRY(zfsmount) next;
52 };
53
54 typedef STAILQ_HEAD(zfs_mnt_list, zfsmount) zfs_mnt_list_t;
55 static zfs_mnt_list_t zfsmount = STAILQ_HEAD_INITIALIZER(zfsmount);
56
57 /*
58 * The indirect_child_t represents the vdev that we will read from, when we
59 * need to read all copies of the data (e.g. for scrub or reconstruction).
60 * For plain (non-mirror) top-level vdevs (i.e. is_vdev is not a mirror),
61 * ic_vdev is the same as is_vdev. However, for mirror top-level vdevs,
62 * ic_vdev is a child of the mirror.
63 */
64 typedef struct indirect_child {
65 void *ic_data;
66 vdev_t *ic_vdev;
67 } indirect_child_t;
68
69 /*
70 * The indirect_split_t represents one mapped segment of an i/o to the
71 * indirect vdev. For non-split (contiguously-mapped) blocks, there will be
72 * only one indirect_split_t, with is_split_offset==0 and is_size==io_size.
73 * For split blocks, there will be several of these.
74 */
75 typedef struct indirect_split {
76 list_node_t is_node; /* link on iv_splits */
77
78 /*
79 * is_split_offset is the offset into the i/o.
80 * This is the sum of the previous splits' is_size's.
81 */
82 uint64_t is_split_offset;
83
84 vdev_t *is_vdev; /* top-level vdev */
85 uint64_t is_target_offset; /* offset on is_vdev */
86 uint64_t is_size;
87 int is_children; /* number of entries in is_child[] */
88
89 /*
90 * is_good_child is the child that we are currently using to
91 * attempt reconstruction.
92 */
93 int is_good_child;
94
95 indirect_child_t is_child[1]; /* variable-length */
96 } indirect_split_t;
97
98 /*
99 * The indirect_vsd_t is associated with each i/o to the indirect vdev.
100 * It is the "Vdev-Specific Data" in the zio_t's io_vsd.
101 */
102 typedef struct indirect_vsd {
103 boolean_t iv_split_block;
104 boolean_t iv_reconstruct;
105
106 list_t iv_splits; /* list of indirect_split_t's */
107 } indirect_vsd_t;
108
109 /*
110 * List of all vdevs, chained through v_alllink.
111 */
112 static vdev_list_t zfs_vdevs;
113
114 /*
115 * List of supported read-incompatible ZFS features. Do not add here features
116 * marked as ZFEATURE_FLAG_READONLY_COMPAT, they are irrelevant for read-only!
117 */
118 static const char *features_for_read[] = {
119 "com.datto:bookmark_v2",
120 "com.datto:encryption",
121 "com.delphix:bookmark_written",
122 "com.delphix:device_removal",
123 "com.delphix:embedded_data",
124 "com.delphix:extensible_dataset",
125 "com.delphix:head_errlog",
126 "com.delphix:hole_birth",
127 "com.joyent:multi_vdev_crash_dump",
128 "com.klarasystems:vdev_zaps_v2",
129 "org.freebsd:zstd_compress",
130 "org.illumos:lz4_compress",
131 "org.illumos:sha512",
132 "org.illumos:skein",
133 "org.open-zfs:large_blocks",
134 "org.openzfs:blake3",
135 "org.zfsonlinux:large_dnode",
136 NULL
137 };
138
139 /*
140 * List of all pools, chained through spa_link.
141 */
142 static spa_list_t zfs_pools;
143
144 static const dnode_phys_t *dnode_cache_obj;
145 static uint64_t dnode_cache_bn;
146 static char *dnode_cache_buf;
147
148 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
149 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
150 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
151 static int zap_lookup(const spa_t *spa, const dnode_phys_t *dnode,
152 const char *name, uint64_t integer_size, uint64_t num_integers,
153 void *value);
154 static int objset_get_dnode(const spa_t *, const objset_phys_t *, uint64_t,
155 dnode_phys_t *);
156 static int dnode_read(const spa_t *, const dnode_phys_t *, off_t, void *,
157 size_t);
158 static int vdev_indirect_read(vdev_t *, const blkptr_t *, void *, off_t,
159 size_t);
160 static int vdev_mirror_read(vdev_t *, const blkptr_t *, void *, off_t, size_t);
161 vdev_indirect_mapping_t *vdev_indirect_mapping_open(spa_t *, objset_phys_t *,
162 uint64_t);
163 vdev_indirect_mapping_entry_phys_t *
164 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *, uint64_t,
165 uint64_t, uint64_t *);
166
167 static void
zfs_init(void)168 zfs_init(void)
169 {
170 STAILQ_INIT(&zfs_vdevs);
171 STAILQ_INIT(&zfs_pools);
172
173 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
174
175 zfs_init_crc();
176 #ifdef HAS_ZSTD_ZFS
177 zstd_init();
178 #endif
179 }
180
181 static int
nvlist_check_features_for_read(nvlist_t * nvl)182 nvlist_check_features_for_read(nvlist_t *nvl)
183 {
184 nvlist_t *features = NULL;
185 nvs_data_t *data;
186 nvp_header_t *nvp;
187 nv_string_t *nvp_name;
188 int rc;
189
190 rc = nvlist_find(nvl, ZPOOL_CONFIG_FEATURES_FOR_READ,
191 DATA_TYPE_NVLIST, NULL, &features, NULL);
192 switch (rc) {
193 case 0:
194 break; /* Continue with checks */
195
196 case ENOENT:
197 return (0); /* All features are disabled */
198
199 default:
200 return (rc); /* Error while reading nvlist */
201 }
202
203 data = (nvs_data_t *)features->nv_data;
204 nvp = &data->nvl_pair; /* first pair in nvlist */
205
206 while (nvp->encoded_size != 0 && nvp->decoded_size != 0) {
207 int i, found;
208
209 nvp_name = (nv_string_t *)((uintptr_t)nvp + sizeof(*nvp));
210 found = 0;
211
212 for (i = 0; features_for_read[i] != NULL; i++) {
213 if (memcmp(nvp_name->nv_data, features_for_read[i],
214 nvp_name->nv_size) == 0) {
215 found = 1;
216 break;
217 }
218 }
219
220 if (!found) {
221 printf("ZFS: unsupported feature: %.*s\n",
222 nvp_name->nv_size, nvp_name->nv_data);
223 rc = EIO;
224 }
225 nvp = (nvp_header_t *)((uint8_t *)nvp + nvp->encoded_size);
226 }
227 nvlist_destroy(features);
228
229 return (rc);
230 }
231
232 static int
vdev_read_phys(vdev_t * vdev,const blkptr_t * bp,void * buf,off_t offset,size_t size)233 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
234 off_t offset, size_t size)
235 {
236 size_t psize;
237 int rc;
238
239 if (vdev->v_phys_read == NULL)
240 return (ENOTSUP);
241
242 if (bp) {
243 psize = BP_GET_PSIZE(bp);
244 } else {
245 psize = size;
246 }
247
248 rc = vdev->v_phys_read(vdev, vdev->v_priv, offset, buf, psize);
249 if (rc == 0) {
250 if (bp != NULL)
251 rc = zio_checksum_verify(vdev->v_spa, bp, buf);
252 }
253
254 return (rc);
255 }
256
257 static int
vdev_write_phys(vdev_t * vdev,void * buf,off_t offset,size_t size)258 vdev_write_phys(vdev_t *vdev, void *buf, off_t offset, size_t size)
259 {
260 if (vdev->v_phys_write == NULL)
261 return (ENOTSUP);
262
263 return (vdev->v_phys_write(vdev, offset, buf, size));
264 }
265
266 typedef struct remap_segment {
267 vdev_t *rs_vd;
268 uint64_t rs_offset;
269 uint64_t rs_asize;
270 uint64_t rs_split_offset;
271 list_node_t rs_node;
272 } remap_segment_t;
273
274 static remap_segment_t *
rs_alloc(vdev_t * vd,uint64_t offset,uint64_t asize,uint64_t split_offset)275 rs_alloc(vdev_t *vd, uint64_t offset, uint64_t asize, uint64_t split_offset)
276 {
277 remap_segment_t *rs = malloc(sizeof (remap_segment_t));
278
279 if (rs != NULL) {
280 rs->rs_vd = vd;
281 rs->rs_offset = offset;
282 rs->rs_asize = asize;
283 rs->rs_split_offset = split_offset;
284 }
285
286 return (rs);
287 }
288
289 vdev_indirect_mapping_t *
vdev_indirect_mapping_open(spa_t * spa,objset_phys_t * os,uint64_t mapping_object)290 vdev_indirect_mapping_open(spa_t *spa, objset_phys_t *os,
291 uint64_t mapping_object)
292 {
293 vdev_indirect_mapping_t *vim;
294 vdev_indirect_mapping_phys_t *vim_phys;
295 int rc;
296
297 vim = calloc(1, sizeof (*vim));
298 if (vim == NULL)
299 return (NULL);
300
301 vim->vim_dn = calloc(1, sizeof (*vim->vim_dn));
302 if (vim->vim_dn == NULL) {
303 free(vim);
304 return (NULL);
305 }
306
307 rc = objset_get_dnode(spa, os, mapping_object, vim->vim_dn);
308 if (rc != 0) {
309 free(vim->vim_dn);
310 free(vim);
311 return (NULL);
312 }
313
314 vim->vim_spa = spa;
315 vim->vim_phys = malloc(sizeof (*vim->vim_phys));
316 if (vim->vim_phys == NULL) {
317 free(vim->vim_dn);
318 free(vim);
319 return (NULL);
320 }
321
322 vim_phys = (vdev_indirect_mapping_phys_t *)DN_BONUS(vim->vim_dn);
323 *vim->vim_phys = *vim_phys;
324
325 vim->vim_objset = os;
326 vim->vim_object = mapping_object;
327 vim->vim_entries = NULL;
328
329 vim->vim_havecounts =
330 (vim->vim_dn->dn_bonuslen > VDEV_INDIRECT_MAPPING_SIZE_V0);
331
332 return (vim);
333 }
334
335 /*
336 * Compare an offset with an indirect mapping entry; there are three
337 * possible scenarios:
338 *
339 * 1. The offset is "less than" the mapping entry; meaning the
340 * offset is less than the source offset of the mapping entry. In
341 * this case, there is no overlap between the offset and the
342 * mapping entry and -1 will be returned.
343 *
344 * 2. The offset is "greater than" the mapping entry; meaning the
345 * offset is greater than the mapping entry's source offset plus
346 * the entry's size. In this case, there is no overlap between
347 * the offset and the mapping entry and 1 will be returned.
348 *
349 * NOTE: If the offset is actually equal to the entry's offset
350 * plus size, this is considered to be "greater" than the entry,
351 * and this case applies (i.e. 1 will be returned). Thus, the
352 * entry's "range" can be considered to be inclusive at its
353 * start, but exclusive at its end: e.g. [src, src + size).
354 *
355 * 3. The last case to consider is if the offset actually falls
356 * within the mapping entry's range. If this is the case, the
357 * offset is considered to be "equal to" the mapping entry and
358 * 0 will be returned.
359 *
360 * NOTE: If the offset is equal to the entry's source offset,
361 * this case applies and 0 will be returned. If the offset is
362 * equal to the entry's source plus its size, this case does
363 * *not* apply (see "NOTE" above for scenario 2), and 1 will be
364 * returned.
365 */
366 static int
dva_mapping_overlap_compare(const void * v_key,const void * v_array_elem)367 dva_mapping_overlap_compare(const void *v_key, const void *v_array_elem)
368 {
369 const uint64_t *key = v_key;
370 const vdev_indirect_mapping_entry_phys_t *array_elem =
371 v_array_elem;
372 uint64_t src_offset = DVA_MAPPING_GET_SRC_OFFSET(array_elem);
373
374 if (*key < src_offset) {
375 return (-1);
376 } else if (*key < src_offset + DVA_GET_ASIZE(&array_elem->vimep_dst)) {
377 return (0);
378 } else {
379 return (1);
380 }
381 }
382
383 /*
384 * Return array entry.
385 */
386 static vdev_indirect_mapping_entry_phys_t *
vdev_indirect_mapping_entry(vdev_indirect_mapping_t * vim,uint64_t index)387 vdev_indirect_mapping_entry(vdev_indirect_mapping_t *vim, uint64_t index)
388 {
389 uint64_t size;
390 off_t offset = 0;
391 int rc;
392
393 if (vim->vim_phys->vimp_num_entries == 0)
394 return (NULL);
395
396 if (vim->vim_entries == NULL) {
397 uint64_t bsize;
398
399 bsize = vim->vim_dn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
400 size = vim->vim_phys->vimp_num_entries *
401 sizeof (*vim->vim_entries);
402 if (size > bsize) {
403 size = bsize / sizeof (*vim->vim_entries);
404 size *= sizeof (*vim->vim_entries);
405 }
406 vim->vim_entries = malloc(size);
407 if (vim->vim_entries == NULL)
408 return (NULL);
409 vim->vim_num_entries = size / sizeof (*vim->vim_entries);
410 offset = index * sizeof (*vim->vim_entries);
411 }
412
413 /* We have data in vim_entries */
414 if (offset == 0) {
415 if (index >= vim->vim_entry_offset &&
416 index <= vim->vim_entry_offset + vim->vim_num_entries) {
417 index -= vim->vim_entry_offset;
418 return (&vim->vim_entries[index]);
419 }
420 offset = index * sizeof (*vim->vim_entries);
421 }
422
423 vim->vim_entry_offset = index;
424 size = vim->vim_num_entries * sizeof (*vim->vim_entries);
425 rc = dnode_read(vim->vim_spa, vim->vim_dn, offset, vim->vim_entries,
426 size);
427 if (rc != 0) {
428 /* Read error, invalidate vim_entries. */
429 free(vim->vim_entries);
430 vim->vim_entries = NULL;
431 return (NULL);
432 }
433 index -= vim->vim_entry_offset;
434 return (&vim->vim_entries[index]);
435 }
436
437 /*
438 * Returns the mapping entry for the given offset.
439 *
440 * It's possible that the given offset will not be in the mapping table
441 * (i.e. no mapping entries contain this offset), in which case, the
442 * return value depends on the "next_if_missing" parameter.
443 *
444 * If the offset is not found in the table and "next_if_missing" is
445 * B_FALSE, then NULL will always be returned. The behavior is intended
446 * to allow consumers to get the entry corresponding to the offset
447 * parameter, iff the offset overlaps with an entry in the table.
448 *
449 * If the offset is not found in the table and "next_if_missing" is
450 * B_TRUE, then the entry nearest to the given offset will be returned,
451 * such that the entry's source offset is greater than the offset
452 * passed in (i.e. the "next" mapping entry in the table is returned, if
453 * the offset is missing from the table). If there are no entries whose
454 * source offset is greater than the passed in offset, NULL is returned.
455 */
456 static vdev_indirect_mapping_entry_phys_t *
vdev_indirect_mapping_entry_for_offset(vdev_indirect_mapping_t * vim,uint64_t offset)457 vdev_indirect_mapping_entry_for_offset(vdev_indirect_mapping_t *vim,
458 uint64_t offset)
459 {
460 ASSERT(vim->vim_phys->vimp_num_entries > 0);
461
462 vdev_indirect_mapping_entry_phys_t *entry;
463
464 uint64_t last = vim->vim_phys->vimp_num_entries - 1;
465 uint64_t base = 0;
466
467 /*
468 * We don't define these inside of the while loop because we use
469 * their value in the case that offset isn't in the mapping.
470 */
471 uint64_t mid;
472 int result;
473
474 while (last >= base) {
475 mid = base + ((last - base) >> 1);
476
477 entry = vdev_indirect_mapping_entry(vim, mid);
478 if (entry == NULL)
479 break;
480 result = dva_mapping_overlap_compare(&offset, entry);
481
482 if (result == 0) {
483 break;
484 } else if (result < 0) {
485 last = mid - 1;
486 } else {
487 base = mid + 1;
488 }
489 }
490 return (entry);
491 }
492
493 /*
494 * Given an indirect vdev and an extent on that vdev, it duplicates the
495 * physical entries of the indirect mapping that correspond to the extent
496 * to a new array and returns a pointer to it. In addition, copied_entries
497 * is populated with the number of mapping entries that were duplicated.
498 *
499 * Finally, since we are doing an allocation, it is up to the caller to
500 * free the array allocated in this function.
501 */
502 vdev_indirect_mapping_entry_phys_t *
vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t * vd,uint64_t offset,uint64_t asize,uint64_t * copied_entries)503 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *vd, uint64_t offset,
504 uint64_t asize, uint64_t *copied_entries)
505 {
506 vdev_indirect_mapping_entry_phys_t *duplicate_mappings = NULL;
507 vdev_indirect_mapping_t *vim = vd->v_mapping;
508 uint64_t entries = 0;
509
510 vdev_indirect_mapping_entry_phys_t *first_mapping =
511 vdev_indirect_mapping_entry_for_offset(vim, offset);
512 ASSERT3P(first_mapping, !=, NULL);
513
514 vdev_indirect_mapping_entry_phys_t *m = first_mapping;
515 while (asize > 0) {
516 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
517 uint64_t inner_offset = offset - DVA_MAPPING_GET_SRC_OFFSET(m);
518 uint64_t inner_size = MIN(asize, size - inner_offset);
519
520 offset += inner_size;
521 asize -= inner_size;
522 entries++;
523 m++;
524 }
525
526 size_t copy_length = entries * sizeof (*first_mapping);
527 duplicate_mappings = malloc(copy_length);
528 if (duplicate_mappings != NULL)
529 bcopy(first_mapping, duplicate_mappings, copy_length);
530 else
531 entries = 0;
532
533 *copied_entries = entries;
534
535 return (duplicate_mappings);
536 }
537
538 static vdev_t *
vdev_lookup_top(spa_t * spa,uint64_t vdev)539 vdev_lookup_top(spa_t *spa, uint64_t vdev)
540 {
541 vdev_t *rvd;
542 vdev_list_t *vlist;
543
544 vlist = &spa->spa_root_vdev->v_children;
545 STAILQ_FOREACH(rvd, vlist, v_childlink)
546 if (rvd->v_id == vdev)
547 break;
548
549 return (rvd);
550 }
551
552 /*
553 * This is a callback for vdev_indirect_remap() which allocates an
554 * indirect_split_t for each split segment and adds it to iv_splits.
555 */
556 static void
vdev_indirect_gather_splits(uint64_t split_offset,vdev_t * vd,uint64_t offset,uint64_t size,void * arg)557 vdev_indirect_gather_splits(uint64_t split_offset, vdev_t *vd, uint64_t offset,
558 uint64_t size, void *arg)
559 {
560 int n = 1;
561 zio_t *zio = arg;
562 indirect_vsd_t *iv = zio->io_vsd;
563
564 if (vd->v_read == vdev_indirect_read)
565 return;
566
567 if (vd->v_read == vdev_mirror_read)
568 n = vd->v_nchildren;
569
570 indirect_split_t *is =
571 malloc(offsetof(indirect_split_t, is_child[n]));
572 if (is == NULL) {
573 zio->io_error = ENOMEM;
574 return;
575 }
576 bzero(is, offsetof(indirect_split_t, is_child[n]));
577
578 is->is_children = n;
579 is->is_size = size;
580 is->is_split_offset = split_offset;
581 is->is_target_offset = offset;
582 is->is_vdev = vd;
583
584 /*
585 * Note that we only consider multiple copies of the data for
586 * *mirror* vdevs. We don't for "replacing" or "spare" vdevs, even
587 * though they use the same ops as mirror, because there's only one
588 * "good" copy under the replacing/spare.
589 */
590 if (vd->v_read == vdev_mirror_read) {
591 int i = 0;
592 vdev_t *kid;
593
594 STAILQ_FOREACH(kid, &vd->v_children, v_childlink) {
595 is->is_child[i++].ic_vdev = kid;
596 }
597 } else {
598 is->is_child[0].ic_vdev = vd;
599 }
600
601 list_insert_tail(&iv->iv_splits, is);
602 }
603
604 static void
vdev_indirect_remap(vdev_t * vd,uint64_t offset,uint64_t asize,void * arg)605 vdev_indirect_remap(vdev_t *vd, uint64_t offset, uint64_t asize, void *arg)
606 {
607 list_t stack;
608 spa_t *spa = vd->v_spa;
609 zio_t *zio = arg;
610 remap_segment_t *rs;
611
612 list_create(&stack, sizeof (remap_segment_t),
613 offsetof(remap_segment_t, rs_node));
614
615 rs = rs_alloc(vd, offset, asize, 0);
616 if (rs == NULL) {
617 printf("vdev_indirect_remap: out of memory.\n");
618 zio->io_error = ENOMEM;
619 }
620 for (; rs != NULL; rs = list_remove_head(&stack)) {
621 vdev_t *v = rs->rs_vd;
622 uint64_t num_entries = 0;
623 /* vdev_indirect_mapping_t *vim = v->v_mapping; */
624 vdev_indirect_mapping_entry_phys_t *mapping =
625 vdev_indirect_mapping_duplicate_adjacent_entries(v,
626 rs->rs_offset, rs->rs_asize, &num_entries);
627
628 if (num_entries == 0)
629 zio->io_error = ENOMEM;
630
631 for (uint64_t i = 0; i < num_entries; i++) {
632 vdev_indirect_mapping_entry_phys_t *m = &mapping[i];
633 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
634 uint64_t dst_offset = DVA_GET_OFFSET(&m->vimep_dst);
635 uint64_t dst_vdev = DVA_GET_VDEV(&m->vimep_dst);
636 uint64_t inner_offset = rs->rs_offset -
637 DVA_MAPPING_GET_SRC_OFFSET(m);
638 uint64_t inner_size =
639 MIN(rs->rs_asize, size - inner_offset);
640 vdev_t *dst_v = vdev_lookup_top(spa, dst_vdev);
641
642 if (dst_v->v_read == vdev_indirect_read) {
643 remap_segment_t *o;
644
645 o = rs_alloc(dst_v, dst_offset + inner_offset,
646 inner_size, rs->rs_split_offset);
647 if (o == NULL) {
648 printf("vdev_indirect_remap: "
649 "out of memory.\n");
650 zio->io_error = ENOMEM;
651 break;
652 }
653
654 list_insert_head(&stack, o);
655 }
656 vdev_indirect_gather_splits(rs->rs_split_offset, dst_v,
657 dst_offset + inner_offset,
658 inner_size, arg);
659
660 /*
661 * vdev_indirect_gather_splits can have memory
662 * allocation error, we can not recover from it.
663 */
664 if (zio->io_error != 0)
665 break;
666 rs->rs_offset += inner_size;
667 rs->rs_asize -= inner_size;
668 rs->rs_split_offset += inner_size;
669 }
670
671 free(mapping);
672 free(rs);
673 if (zio->io_error != 0)
674 break;
675 }
676
677 list_destroy(&stack);
678 }
679
680 static void
vdev_indirect_map_free(zio_t * zio)681 vdev_indirect_map_free(zio_t *zio)
682 {
683 indirect_vsd_t *iv = zio->io_vsd;
684 indirect_split_t *is;
685
686 while ((is = list_head(&iv->iv_splits)) != NULL) {
687 for (int c = 0; c < is->is_children; c++) {
688 indirect_child_t *ic = &is->is_child[c];
689 free(ic->ic_data);
690 }
691 list_remove(&iv->iv_splits, is);
692 free(is);
693 }
694 free(iv);
695 }
696
697 static int
vdev_indirect_read(vdev_t * vdev,const blkptr_t * bp,void * buf,off_t offset,size_t bytes)698 vdev_indirect_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
699 off_t offset, size_t bytes)
700 {
701 zio_t zio;
702 spa_t *spa = vdev->v_spa;
703 indirect_vsd_t *iv;
704 indirect_split_t *first;
705 int rc = EIO;
706
707 iv = calloc(1, sizeof(*iv));
708 if (iv == NULL)
709 return (ENOMEM);
710
711 list_create(&iv->iv_splits,
712 sizeof (indirect_split_t), offsetof(indirect_split_t, is_node));
713
714 bzero(&zio, sizeof(zio));
715 zio.io_spa = spa;
716 zio.io_bp = (blkptr_t *)bp;
717 zio.io_data = buf;
718 zio.io_size = bytes;
719 zio.io_offset = offset;
720 zio.io_vd = vdev;
721 zio.io_vsd = iv;
722
723 if (vdev->v_mapping == NULL) {
724 vdev_indirect_config_t *vic;
725
726 vic = &vdev->vdev_indirect_config;
727 vdev->v_mapping = vdev_indirect_mapping_open(spa,
728 spa->spa_mos, vic->vic_mapping_object);
729 }
730
731 vdev_indirect_remap(vdev, offset, bytes, &zio);
732 if (zio.io_error != 0)
733 return (zio.io_error);
734
735 first = list_head(&iv->iv_splits);
736 if (first->is_size == zio.io_size) {
737 /*
738 * This is not a split block; we are pointing to the entire
739 * data, which will checksum the same as the original data.
740 * Pass the BP down so that the child i/o can verify the
741 * checksum, and try a different location if available
742 * (e.g. on a mirror).
743 *
744 * While this special case could be handled the same as the
745 * general (split block) case, doing it this way ensures
746 * that the vast majority of blocks on indirect vdevs
747 * (which are not split) are handled identically to blocks
748 * on non-indirect vdevs. This allows us to be less strict
749 * about performance in the general (but rare) case.
750 */
751 rc = first->is_vdev->v_read(first->is_vdev, zio.io_bp,
752 zio.io_data, first->is_target_offset, bytes);
753 } else {
754 iv->iv_split_block = B_TRUE;
755 /*
756 * Read one copy of each split segment, from the
757 * top-level vdev. Since we don't know the
758 * checksum of each split individually, the child
759 * zio can't ensure that we get the right data.
760 * E.g. if it's a mirror, it will just read from a
761 * random (healthy) leaf vdev. We have to verify
762 * the checksum in vdev_indirect_io_done().
763 */
764 for (indirect_split_t *is = list_head(&iv->iv_splits);
765 is != NULL; is = list_next(&iv->iv_splits, is)) {
766 char *ptr = zio.io_data;
767
768 rc = is->is_vdev->v_read(is->is_vdev, zio.io_bp,
769 ptr + is->is_split_offset, is->is_target_offset,
770 is->is_size);
771 }
772 if (zio_checksum_verify(spa, zio.io_bp, zio.io_data))
773 rc = ECKSUM;
774 else
775 rc = 0;
776 }
777
778 vdev_indirect_map_free(&zio);
779 if (rc == 0)
780 rc = zio.io_error;
781
782 return (rc);
783 }
784
785 static int
vdev_disk_read(vdev_t * vdev,const blkptr_t * bp,void * buf,off_t offset,size_t bytes)786 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
787 off_t offset, size_t bytes)
788 {
789
790 return (vdev_read_phys(vdev, bp, buf,
791 offset + VDEV_LABEL_START_SIZE, bytes));
792 }
793
794 static int
vdev_missing_read(vdev_t * vdev __unused,const blkptr_t * bp __unused,void * buf __unused,off_t offset __unused,size_t bytes __unused)795 vdev_missing_read(vdev_t *vdev __unused, const blkptr_t *bp __unused,
796 void *buf __unused, off_t offset __unused, size_t bytes __unused)
797 {
798
799 return (ENOTSUP);
800 }
801
802 static int
vdev_mirror_read(vdev_t * vdev,const blkptr_t * bp,void * buf,off_t offset,size_t bytes)803 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
804 off_t offset, size_t bytes)
805 {
806 vdev_t *kid;
807 int rc;
808
809 rc = EIO;
810 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
811 if (kid->v_state != VDEV_STATE_HEALTHY)
812 continue;
813 rc = kid->v_read(kid, bp, buf, offset, bytes);
814 if (!rc)
815 return (0);
816 }
817
818 return (rc);
819 }
820
821 static int
vdev_replacing_read(vdev_t * vdev,const blkptr_t * bp,void * buf,off_t offset,size_t bytes)822 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
823 off_t offset, size_t bytes)
824 {
825 vdev_t *kid;
826
827 /*
828 * Here we should have two kids:
829 * First one which is the one we are replacing and we can trust
830 * only this one to have valid data, but it might not be present.
831 * Second one is that one we are replacing with. It is most likely
832 * healthy, but we can't trust it has needed data, so we won't use it.
833 */
834 kid = STAILQ_FIRST(&vdev->v_children);
835 if (kid == NULL)
836 return (EIO);
837 if (kid->v_state != VDEV_STATE_HEALTHY)
838 return (EIO);
839 return (kid->v_read(kid, bp, buf, offset, bytes));
840 }
841
842 static vdev_t *
vdev_find(uint64_t guid)843 vdev_find(uint64_t guid)
844 {
845 vdev_t *vdev;
846
847 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
848 if (vdev->v_guid == guid)
849 return (vdev);
850
851 return (0);
852 }
853
854 static vdev_t *
vdev_create(uint64_t guid,vdev_read_t * _read)855 vdev_create(uint64_t guid, vdev_read_t *_read)
856 {
857 vdev_t *vdev;
858 vdev_indirect_config_t *vic;
859
860 vdev = calloc(1, sizeof(vdev_t));
861 if (vdev != NULL) {
862 STAILQ_INIT(&vdev->v_children);
863 vdev->v_guid = guid;
864 vdev->v_read = _read;
865
866 /*
867 * root vdev has no read function, we use this fact to
868 * skip setting up data we do not need for root vdev.
869 * We only point root vdev from spa.
870 */
871 if (_read != NULL) {
872 vic = &vdev->vdev_indirect_config;
873 vic->vic_prev_indirect_vdev = UINT64_MAX;
874 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
875 }
876 }
877
878 return (vdev);
879 }
880
881 static void
vdev_set_initial_state(vdev_t * vdev,const nvlist_t * nvlist)882 vdev_set_initial_state(vdev_t *vdev, const nvlist_t *nvlist)
883 {
884 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
885 uint64_t is_log;
886
887 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
888 is_log = 0;
889 (void) nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL,
890 &is_offline, NULL);
891 (void) nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL,
892 &is_removed, NULL);
893 (void) nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, NULL,
894 &is_faulted, NULL);
895 (void) nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64,
896 NULL, &is_degraded, NULL);
897 (void) nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64,
898 NULL, &isnt_present, NULL);
899 (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, NULL,
900 &is_log, NULL);
901
902 if (is_offline != 0)
903 vdev->v_state = VDEV_STATE_OFFLINE;
904 else if (is_removed != 0)
905 vdev->v_state = VDEV_STATE_REMOVED;
906 else if (is_faulted != 0)
907 vdev->v_state = VDEV_STATE_FAULTED;
908 else if (is_degraded != 0)
909 vdev->v_state = VDEV_STATE_DEGRADED;
910 else if (isnt_present != 0)
911 vdev->v_state = VDEV_STATE_CANT_OPEN;
912
913 vdev->v_islog = is_log != 0;
914 }
915
916 static int
vdev_init(uint64_t guid,const nvlist_t * nvlist,vdev_t ** vdevp)917 vdev_init(uint64_t guid, const nvlist_t *nvlist, vdev_t **vdevp)
918 {
919 uint64_t id, ashift, asize, nparity;
920 const char *path;
921 const char *type;
922 int len, pathlen;
923 char *name;
924 vdev_t *vdev;
925
926 if (nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id,
927 NULL) ||
928 nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING, NULL,
929 &type, &len)) {
930 return (ENOENT);
931 }
932
933 if (memcmp(type, VDEV_TYPE_MIRROR, len) != 0 &&
934 memcmp(type, VDEV_TYPE_DISK, len) != 0 &&
935 #ifdef ZFS_TEST
936 memcmp(type, VDEV_TYPE_FILE, len) != 0 &&
937 #endif
938 memcmp(type, VDEV_TYPE_RAIDZ, len) != 0 &&
939 memcmp(type, VDEV_TYPE_INDIRECT, len) != 0 &&
940 memcmp(type, VDEV_TYPE_REPLACING, len) != 0 &&
941 memcmp(type, VDEV_TYPE_HOLE, len) != 0) {
942 printf("ZFS: can only boot from disk, mirror, raidz1, "
943 "raidz2 and raidz3 vdevs, got: %.*s\n", len, type);
944 return (EIO);
945 }
946
947 if (memcmp(type, VDEV_TYPE_MIRROR, len) == 0)
948 vdev = vdev_create(guid, vdev_mirror_read);
949 else if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0)
950 vdev = vdev_create(guid, vdev_raidz_read);
951 else if (memcmp(type, VDEV_TYPE_REPLACING, len) == 0)
952 vdev = vdev_create(guid, vdev_replacing_read);
953 else if (memcmp(type, VDEV_TYPE_INDIRECT, len) == 0) {
954 vdev_indirect_config_t *vic;
955
956 vdev = vdev_create(guid, vdev_indirect_read);
957 if (vdev != NULL) {
958 vdev->v_state = VDEV_STATE_HEALTHY;
959 vic = &vdev->vdev_indirect_config;
960
961 nvlist_find(nvlist,
962 ZPOOL_CONFIG_INDIRECT_OBJECT,
963 DATA_TYPE_UINT64,
964 NULL, &vic->vic_mapping_object, NULL);
965 nvlist_find(nvlist,
966 ZPOOL_CONFIG_INDIRECT_BIRTHS,
967 DATA_TYPE_UINT64,
968 NULL, &vic->vic_births_object, NULL);
969 nvlist_find(nvlist,
970 ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
971 DATA_TYPE_UINT64,
972 NULL, &vic->vic_prev_indirect_vdev, NULL);
973 }
974 } else if (memcmp(type, VDEV_TYPE_HOLE, len) == 0) {
975 vdev = vdev_create(guid, vdev_missing_read);
976 } else {
977 vdev = vdev_create(guid, vdev_disk_read);
978 }
979
980 if (vdev == NULL)
981 return (ENOMEM);
982
983 vdev_set_initial_state(vdev, nvlist);
984 vdev->v_id = id;
985 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
986 DATA_TYPE_UINT64, NULL, &ashift, NULL) == 0)
987 vdev->v_ashift = ashift;
988
989 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASIZE,
990 DATA_TYPE_UINT64, NULL, &asize, NULL) == 0) {
991 vdev->v_psize = asize +
992 VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
993 }
994
995 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
996 DATA_TYPE_UINT64, NULL, &nparity, NULL) == 0)
997 vdev->v_nparity = nparity;
998
999 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
1000 DATA_TYPE_STRING, NULL, &path, &pathlen) == 0) {
1001 char prefix[] = "/dev/";
1002
1003 len = strlen(prefix);
1004 if (len < pathlen && memcmp(path, prefix, len) == 0) {
1005 path += len;
1006 pathlen -= len;
1007 }
1008 name = malloc(pathlen + 1);
1009 bcopy(path, name, pathlen);
1010 name[pathlen] = '\0';
1011 vdev->v_name = name;
1012 } else {
1013 name = NULL;
1014 if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0) {
1015 if (vdev->v_nparity < 1 ||
1016 vdev->v_nparity > 3) {
1017 printf("ZFS: invalid raidz parity: %d\n",
1018 vdev->v_nparity);
1019 return (EIO);
1020 }
1021 (void) asprintf(&name, "%.*s%d-%" PRIu64, len, type,
1022 vdev->v_nparity, id);
1023 } else {
1024 (void) asprintf(&name, "%.*s-%" PRIu64, len, type, id);
1025 }
1026 vdev->v_name = name;
1027 }
1028 *vdevp = vdev;
1029 return (0);
1030 }
1031
1032 /*
1033 * Find slot for vdev. We return either NULL to signal to use
1034 * STAILQ_INSERT_HEAD, or we return link element to be used with
1035 * STAILQ_INSERT_AFTER.
1036 */
1037 static vdev_t *
vdev_find_previous(vdev_t * top_vdev,vdev_t * vdev)1038 vdev_find_previous(vdev_t *top_vdev, vdev_t *vdev)
1039 {
1040 vdev_t *v, *previous;
1041
1042 if (STAILQ_EMPTY(&top_vdev->v_children))
1043 return (NULL);
1044
1045 previous = NULL;
1046 STAILQ_FOREACH(v, &top_vdev->v_children, v_childlink) {
1047 if (v->v_id > vdev->v_id)
1048 return (previous);
1049
1050 if (v->v_id == vdev->v_id)
1051 return (v);
1052
1053 if (v->v_id < vdev->v_id)
1054 previous = v;
1055 }
1056 return (previous);
1057 }
1058
1059 static size_t
vdev_child_count(vdev_t * vdev)1060 vdev_child_count(vdev_t *vdev)
1061 {
1062 vdev_t *v;
1063 size_t count;
1064
1065 count = 0;
1066 STAILQ_FOREACH(v, &vdev->v_children, v_childlink) {
1067 count++;
1068 }
1069 return (count);
1070 }
1071
1072 /*
1073 * Insert vdev into top_vdev children list. List is ordered by v_id.
1074 */
1075 static void
vdev_insert(vdev_t * top_vdev,vdev_t * vdev)1076 vdev_insert(vdev_t *top_vdev, vdev_t *vdev)
1077 {
1078 vdev_t *previous;
1079 size_t count;
1080
1081 /*
1082 * The top level vdev can appear in random order, depending how
1083 * the firmware is presenting the disk devices.
1084 * However, we will insert vdev to create list ordered by v_id,
1085 * so we can use either STAILQ_INSERT_HEAD or STAILQ_INSERT_AFTER
1086 * as STAILQ does not have insert before.
1087 */
1088 previous = vdev_find_previous(top_vdev, vdev);
1089
1090 if (previous == NULL) {
1091 STAILQ_INSERT_HEAD(&top_vdev->v_children, vdev, v_childlink);
1092 } else if (previous->v_id == vdev->v_id) {
1093 /*
1094 * This vdev was configured from label config,
1095 * do not insert duplicate.
1096 */
1097 return;
1098 } else {
1099 STAILQ_INSERT_AFTER(&top_vdev->v_children, previous, vdev,
1100 v_childlink);
1101 }
1102
1103 count = vdev_child_count(top_vdev);
1104 if (top_vdev->v_nchildren < count)
1105 top_vdev->v_nchildren = count;
1106 }
1107
1108 static int
vdev_from_nvlist(spa_t * spa,uint64_t top_guid,uint64_t txg,const nvlist_t * nvlist)1109 vdev_from_nvlist(spa_t *spa, uint64_t top_guid, uint64_t txg,
1110 const nvlist_t *nvlist)
1111 {
1112 vdev_t *top_vdev, *vdev;
1113 nvlist_t **kids = NULL;
1114 int rc, nkids;
1115
1116 /* Get top vdev. */
1117 top_vdev = vdev_find(top_guid);
1118 if (top_vdev == NULL) {
1119 rc = vdev_init(top_guid, nvlist, &top_vdev);
1120 if (rc != 0)
1121 return (rc);
1122 top_vdev->v_spa = spa;
1123 top_vdev->v_top = top_vdev;
1124 top_vdev->v_txg = txg;
1125 vdev_insert(spa->spa_root_vdev, top_vdev);
1126 }
1127
1128 /* Add children if there are any. */
1129 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1130 &nkids, &kids, NULL);
1131 if (rc == 0) {
1132 for (int i = 0; i < nkids; i++) {
1133 uint64_t guid;
1134
1135 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID,
1136 DATA_TYPE_UINT64, NULL, &guid, NULL);
1137 if (rc != 0)
1138 goto done;
1139
1140 rc = vdev_init(guid, kids[i], &vdev);
1141 if (rc != 0)
1142 goto done;
1143
1144 vdev->v_spa = spa;
1145 vdev->v_top = top_vdev;
1146 vdev_insert(top_vdev, vdev);
1147 }
1148 } else {
1149 /*
1150 * When there are no children, nvlist_find() does return
1151 * error, reset it because leaf devices have no children.
1152 */
1153 rc = 0;
1154 }
1155 done:
1156 if (kids != NULL) {
1157 for (int i = 0; i < nkids; i++)
1158 nvlist_destroy(kids[i]);
1159 free(kids);
1160 }
1161
1162 return (rc);
1163 }
1164
1165 static int
vdev_init_from_label(spa_t * spa,const nvlist_t * nvlist)1166 vdev_init_from_label(spa_t *spa, const nvlist_t *nvlist)
1167 {
1168 uint64_t pool_guid, top_guid, txg;
1169 nvlist_t *vdevs;
1170 int rc;
1171
1172 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1173 NULL, &pool_guid, NULL) ||
1174 nvlist_find(nvlist, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64,
1175 NULL, &top_guid, NULL) ||
1176 nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG, DATA_TYPE_UINT64,
1177 NULL, &txg, NULL) != 0 ||
1178 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1179 NULL, &vdevs, NULL)) {
1180 printf("ZFS: can't find vdev details\n");
1181 return (ENOENT);
1182 }
1183
1184 rc = vdev_from_nvlist(spa, top_guid, txg, vdevs);
1185 nvlist_destroy(vdevs);
1186 return (rc);
1187 }
1188
1189 static void
vdev_set_state(vdev_t * vdev)1190 vdev_set_state(vdev_t *vdev)
1191 {
1192 vdev_t *kid;
1193 int good_kids;
1194 int bad_kids;
1195
1196 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1197 vdev_set_state(kid);
1198 }
1199
1200 /*
1201 * A mirror or raidz is healthy if all its kids are healthy. A
1202 * mirror is degraded if any of its kids is healthy; a raidz
1203 * is degraded if at most nparity kids are offline.
1204 */
1205 if (STAILQ_FIRST(&vdev->v_children)) {
1206 good_kids = 0;
1207 bad_kids = 0;
1208 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1209 if (kid->v_state == VDEV_STATE_HEALTHY)
1210 good_kids++;
1211 else
1212 bad_kids++;
1213 }
1214 if (bad_kids == 0) {
1215 vdev->v_state = VDEV_STATE_HEALTHY;
1216 } else {
1217 if (vdev->v_read == vdev_mirror_read) {
1218 if (good_kids) {
1219 vdev->v_state = VDEV_STATE_DEGRADED;
1220 } else {
1221 vdev->v_state = VDEV_STATE_OFFLINE;
1222 }
1223 } else if (vdev->v_read == vdev_raidz_read) {
1224 if (bad_kids > vdev->v_nparity) {
1225 vdev->v_state = VDEV_STATE_OFFLINE;
1226 } else {
1227 vdev->v_state = VDEV_STATE_DEGRADED;
1228 }
1229 }
1230 }
1231 }
1232 }
1233
1234 static int
vdev_update_from_nvlist(uint64_t top_guid,const nvlist_t * nvlist)1235 vdev_update_from_nvlist(uint64_t top_guid, const nvlist_t *nvlist)
1236 {
1237 vdev_t *vdev;
1238 nvlist_t **kids = NULL;
1239 int rc, nkids;
1240
1241 /* Update top vdev. */
1242 vdev = vdev_find(top_guid);
1243 if (vdev != NULL)
1244 vdev_set_initial_state(vdev, nvlist);
1245
1246 /* Update children if there are any. */
1247 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1248 &nkids, &kids, NULL);
1249 if (rc == 0) {
1250 for (int i = 0; i < nkids; i++) {
1251 uint64_t guid;
1252
1253 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID,
1254 DATA_TYPE_UINT64, NULL, &guid, NULL);
1255 if (rc != 0)
1256 break;
1257
1258 vdev = vdev_find(guid);
1259 if (vdev != NULL)
1260 vdev_set_initial_state(vdev, kids[i]);
1261 }
1262 } else {
1263 rc = 0;
1264 }
1265 if (kids != NULL) {
1266 for (int i = 0; i < nkids; i++)
1267 nvlist_destroy(kids[i]);
1268 free(kids);
1269 }
1270
1271 return (rc);
1272 }
1273
1274 /*
1275 * Shall not be called on root vdev, that is not linked into zfs_vdevs.
1276 * See comment in vdev_create().
1277 */
1278 static void
vdev_free(struct vdev * vdev)1279 vdev_free(struct vdev *vdev)
1280 {
1281 struct vdev *kid, *safe;
1282
1283 STAILQ_FOREACH_SAFE(kid, &vdev->v_children, v_childlink, safe)
1284 vdev_free(kid);
1285 STAILQ_REMOVE(&zfs_vdevs, vdev, vdev, v_alllink);
1286 free(vdev);
1287 }
1288
1289 static int
vdev_init_from_nvlist(spa_t * spa,const nvlist_t * nvlist)1290 vdev_init_from_nvlist(spa_t *spa, const nvlist_t *nvlist)
1291 {
1292 uint64_t pool_guid, vdev_children;
1293 nvlist_t *vdevs = NULL, **kids = NULL;
1294 int rc, nkids;
1295
1296 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1297 NULL, &pool_guid, NULL) ||
1298 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_CHILDREN, DATA_TYPE_UINT64,
1299 NULL, &vdev_children, NULL) ||
1300 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1301 NULL, &vdevs, NULL)) {
1302 printf("ZFS: can't find vdev details\n");
1303 return (ENOENT);
1304 }
1305
1306 /* Wrong guid?! */
1307 if (spa->spa_guid != pool_guid) {
1308 nvlist_destroy(vdevs);
1309 return (EINVAL);
1310 }
1311
1312 spa->spa_root_vdev->v_nchildren = vdev_children;
1313
1314 rc = nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1315 &nkids, &kids, NULL);
1316 nvlist_destroy(vdevs);
1317
1318 /*
1319 * MOS config has at least one child for root vdev.
1320 */
1321 if (rc != 0)
1322 return (rc);
1323
1324 for (int i = 0; i < nkids; i++) {
1325 uint64_t guid;
1326 vdev_t *vdev;
1327
1328 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
1329 NULL, &guid, NULL);
1330 if (rc != 0)
1331 break;
1332 vdev = vdev_find(guid);
1333 /*
1334 * Top level vdev is missing, create it.
1335 * XXXGL: how can this happen?
1336 */
1337 if (vdev == NULL)
1338 rc = vdev_from_nvlist(spa, guid, 0, kids[i]);
1339 else
1340 rc = vdev_update_from_nvlist(guid, kids[i]);
1341 if (rc != 0)
1342 break;
1343 }
1344 if (kids != NULL) {
1345 for (int i = 0; i < nkids; i++)
1346 nvlist_destroy(kids[i]);
1347 free(kids);
1348 }
1349
1350 /*
1351 * Re-evaluate top-level vdev state.
1352 */
1353 vdev_set_state(spa->spa_root_vdev);
1354
1355 return (rc);
1356 }
1357
1358 static spa_t *
spa_find_by_guid(uint64_t guid)1359 spa_find_by_guid(uint64_t guid)
1360 {
1361 spa_t *spa;
1362
1363 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1364 if (spa->spa_guid == guid)
1365 return (spa);
1366
1367 return (NULL);
1368 }
1369
1370 static spa_t *
spa_find_by_name(const char * name)1371 spa_find_by_name(const char *name)
1372 {
1373 spa_t *spa;
1374
1375 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1376 if (strcmp(spa->spa_name, name) == 0)
1377 return (spa);
1378
1379 return (NULL);
1380 }
1381
1382 static spa_t *
spa_create(uint64_t guid,const char * name)1383 spa_create(uint64_t guid, const char *name)
1384 {
1385 spa_t *spa;
1386
1387 if ((spa = calloc(1, sizeof(spa_t))) == NULL)
1388 return (NULL);
1389 if ((spa->spa_name = strdup(name)) == NULL) {
1390 free(spa);
1391 return (NULL);
1392 }
1393 spa->spa_uberblock = &spa->spa_uberblock_master;
1394 spa->spa_mos = &spa->spa_mos_master;
1395 spa->spa_guid = guid;
1396 spa->spa_root_vdev = vdev_create(guid, NULL);
1397 if (spa->spa_root_vdev == NULL) {
1398 free(spa->spa_name);
1399 free(spa);
1400 return (NULL);
1401 }
1402 spa->spa_root_vdev->v_name = spa->spa_name;
1403 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
1404
1405 return (spa);
1406 }
1407
1408 static const char *
state_name(vdev_state_t state)1409 state_name(vdev_state_t state)
1410 {
1411 static const char *names[] = {
1412 "UNKNOWN",
1413 "CLOSED",
1414 "OFFLINE",
1415 "REMOVED",
1416 "CANT_OPEN",
1417 "FAULTED",
1418 "DEGRADED",
1419 "ONLINE"
1420 };
1421 return (names[state]);
1422 }
1423
1424 #ifdef BOOT2
1425
1426 #define pager_printf printf
1427
1428 #else
1429
1430 static int
pager_printf(const char * fmt,...)1431 pager_printf(const char *fmt, ...)
1432 {
1433 char line[80];
1434 va_list args;
1435
1436 va_start(args, fmt);
1437 vsnprintf(line, sizeof(line), fmt, args);
1438 va_end(args);
1439 return (pager_output(line));
1440 }
1441
1442 #endif
1443
1444 #define STATUS_FORMAT " %s %s\n"
1445
1446 static int
print_state(int indent,const char * name,vdev_state_t state)1447 print_state(int indent, const char *name, vdev_state_t state)
1448 {
1449 int i;
1450 char buf[512];
1451
1452 buf[0] = 0;
1453 for (i = 0; i < indent; i++)
1454 strcat(buf, " ");
1455 strcat(buf, name);
1456 return (pager_printf(STATUS_FORMAT, buf, state_name(state)));
1457 }
1458
1459 static int
vdev_status(vdev_t * vdev,int indent)1460 vdev_status(vdev_t *vdev, int indent)
1461 {
1462 vdev_t *kid;
1463 int ret;
1464
1465 if (vdev->v_islog) {
1466 (void) pager_output(" logs\n");
1467 indent++;
1468 }
1469
1470 ret = print_state(indent, vdev->v_name, vdev->v_state);
1471 if (ret != 0)
1472 return (ret);
1473
1474 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1475 ret = vdev_status(kid, indent + 1);
1476 if (ret != 0)
1477 return (ret);
1478 }
1479 return (ret);
1480 }
1481
1482 static int
spa_status(spa_t * spa)1483 spa_status(spa_t *spa)
1484 {
1485 static char bootfs[ZFS_MAXNAMELEN];
1486 uint64_t rootid;
1487 vdev_list_t *vlist;
1488 vdev_t *vdev;
1489 int good_kids, bad_kids, degraded_kids, ret;
1490 vdev_state_t state;
1491
1492 ret = pager_printf(" pool: %s\n", spa->spa_name);
1493 if (ret != 0)
1494 return (ret);
1495
1496 if (zfs_get_root(spa, &rootid) == 0 &&
1497 zfs_rlookup(spa, rootid, bootfs) == 0) {
1498 if (bootfs[0] == '\0')
1499 ret = pager_printf("bootfs: %s\n", spa->spa_name);
1500 else
1501 ret = pager_printf("bootfs: %s/%s\n", spa->spa_name,
1502 bootfs);
1503 if (ret != 0)
1504 return (ret);
1505 }
1506 ret = pager_printf("config:\n\n");
1507 if (ret != 0)
1508 return (ret);
1509 ret = pager_printf(STATUS_FORMAT, "NAME", "STATE");
1510 if (ret != 0)
1511 return (ret);
1512
1513 good_kids = 0;
1514 degraded_kids = 0;
1515 bad_kids = 0;
1516 vlist = &spa->spa_root_vdev->v_children;
1517 STAILQ_FOREACH(vdev, vlist, v_childlink) {
1518 if (vdev->v_state == VDEV_STATE_HEALTHY)
1519 good_kids++;
1520 else if (vdev->v_state == VDEV_STATE_DEGRADED)
1521 degraded_kids++;
1522 else
1523 bad_kids++;
1524 }
1525
1526 state = VDEV_STATE_CLOSED;
1527 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
1528 state = VDEV_STATE_HEALTHY;
1529 else if ((good_kids + degraded_kids) > 0)
1530 state = VDEV_STATE_DEGRADED;
1531
1532 ret = print_state(0, spa->spa_name, state);
1533 if (ret != 0)
1534 return (ret);
1535
1536 STAILQ_FOREACH(vdev, vlist, v_childlink) {
1537 ret = vdev_status(vdev, 1);
1538 if (ret != 0)
1539 return (ret);
1540 }
1541 return (ret);
1542 }
1543
1544 static int
spa_all_status(void)1545 spa_all_status(void)
1546 {
1547 spa_t *spa;
1548 int first = 1, ret = 0;
1549
1550 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
1551 if (!first) {
1552 ret = pager_printf("\n");
1553 if (ret != 0)
1554 return (ret);
1555 }
1556 first = 0;
1557 ret = spa_status(spa);
1558 if (ret != 0)
1559 return (ret);
1560 }
1561 return (ret);
1562 }
1563
1564 static uint64_t
vdev_label_offset(uint64_t psize,int l,uint64_t offset)1565 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
1566 {
1567 uint64_t label_offset;
1568
1569 if (l < VDEV_LABELS / 2)
1570 label_offset = 0;
1571 else
1572 label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t);
1573
1574 return (offset + l * sizeof (vdev_label_t) + label_offset);
1575 }
1576
1577 static int
vdev_uberblock_compare(const uberblock_t * ub1,const uberblock_t * ub2)1578 vdev_uberblock_compare(const uberblock_t *ub1, const uberblock_t *ub2)
1579 {
1580 unsigned int seq1 = 0;
1581 unsigned int seq2 = 0;
1582 int cmp = AVL_CMP(ub1->ub_txg, ub2->ub_txg);
1583
1584 if (cmp != 0)
1585 return (cmp);
1586
1587 cmp = AVL_CMP(ub1->ub_timestamp, ub2->ub_timestamp);
1588 if (cmp != 0)
1589 return (cmp);
1590
1591 if (MMP_VALID(ub1) && MMP_SEQ_VALID(ub1))
1592 seq1 = MMP_SEQ(ub1);
1593
1594 if (MMP_VALID(ub2) && MMP_SEQ_VALID(ub2))
1595 seq2 = MMP_SEQ(ub2);
1596
1597 return (AVL_CMP(seq1, seq2));
1598 }
1599
1600 static int
uberblock_verify(uberblock_t * ub)1601 uberblock_verify(uberblock_t *ub)
1602 {
1603 if (ub->ub_magic == BSWAP_64((uint64_t)UBERBLOCK_MAGIC)) {
1604 byteswap_uint64_array(ub, sizeof (uberblock_t));
1605 }
1606
1607 if (ub->ub_magic != UBERBLOCK_MAGIC ||
1608 !SPA_VERSION_IS_SUPPORTED(ub->ub_version))
1609 return (EINVAL);
1610
1611 return (0);
1612 }
1613
1614 static int
vdev_label_read(vdev_t * vd,int l,void * buf,uint64_t offset,size_t size)1615 vdev_label_read(vdev_t *vd, int l, void *buf, uint64_t offset,
1616 size_t size)
1617 {
1618 blkptr_t bp;
1619 off_t off;
1620
1621 off = vdev_label_offset(vd->v_psize, l, offset);
1622
1623 BP_ZERO(&bp);
1624 BP_SET_LSIZE(&bp, size);
1625 BP_SET_PSIZE(&bp, size);
1626 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1627 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1628 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
1629 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1630
1631 return (vdev_read_phys(vd, &bp, buf, off, size));
1632 }
1633
1634 /*
1635 * We do need to be sure we write to correct location.
1636 * Our vdev label does consist of 4 fields:
1637 * pad1 (8k), reserved.
1638 * bootenv (8k), checksummed, previously reserved, may contian garbage.
1639 * vdev_phys (112k), checksummed
1640 * uberblock ring (128k), checksummed.
1641 *
1642 * Since bootenv area may contain garbage, we can not reliably read it, as
1643 * we can get checksum errors.
1644 * Next best thing is vdev_phys - it is just after bootenv. It still may
1645 * be corrupted, but in such case we will miss this one write.
1646 */
1647 static int
vdev_label_write_validate(vdev_t * vd,int l,uint64_t offset)1648 vdev_label_write_validate(vdev_t *vd, int l, uint64_t offset)
1649 {
1650 uint64_t off, o_phys;
1651 void *buf;
1652 size_t size = VDEV_PHYS_SIZE;
1653 int rc;
1654
1655 o_phys = offsetof(vdev_label_t, vl_vdev_phys);
1656 off = vdev_label_offset(vd->v_psize, l, o_phys);
1657
1658 /* off should be 8K from bootenv */
1659 if (vdev_label_offset(vd->v_psize, l, offset) + VDEV_PAD_SIZE != off)
1660 return (EINVAL);
1661
1662 buf = malloc(size);
1663 if (buf == NULL)
1664 return (ENOMEM);
1665
1666 /* Read vdev_phys */
1667 rc = vdev_label_read(vd, l, buf, o_phys, size);
1668 free(buf);
1669 return (rc);
1670 }
1671
1672 static int
vdev_label_write(vdev_t * vd,int l,vdev_boot_envblock_t * be,uint64_t offset)1673 vdev_label_write(vdev_t *vd, int l, vdev_boot_envblock_t *be, uint64_t offset)
1674 {
1675 zio_checksum_info_t *ci;
1676 zio_cksum_t cksum;
1677 off_t off;
1678 size_t size = VDEV_PAD_SIZE;
1679 int rc;
1680
1681 if (vd->v_phys_write == NULL)
1682 return (ENOTSUP);
1683
1684 off = vdev_label_offset(vd->v_psize, l, offset);
1685
1686 rc = vdev_label_write_validate(vd, l, offset);
1687 if (rc != 0) {
1688 return (rc);
1689 }
1690
1691 ci = &zio_checksum_table[ZIO_CHECKSUM_LABEL];
1692 be->vbe_zbt.zec_magic = ZEC_MAGIC;
1693 zio_checksum_label_verifier(&be->vbe_zbt.zec_cksum, off);
1694 ci->ci_func[0](be, size, NULL, &cksum);
1695 be->vbe_zbt.zec_cksum = cksum;
1696
1697 return (vdev_write_phys(vd, be, off, size));
1698 }
1699
1700 static int
vdev_write_bootenv_impl(vdev_t * vdev,vdev_boot_envblock_t * be)1701 vdev_write_bootenv_impl(vdev_t *vdev, vdev_boot_envblock_t *be)
1702 {
1703 vdev_t *kid;
1704 int rv = 0, err;
1705
1706 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1707 if (kid->v_state != VDEV_STATE_HEALTHY)
1708 continue;
1709 err = vdev_write_bootenv_impl(kid, be);
1710 if (err != 0)
1711 rv = err;
1712 }
1713
1714 /*
1715 * Non-leaf vdevs do not have v_phys_write.
1716 */
1717 if (vdev->v_phys_write == NULL)
1718 return (rv);
1719
1720 for (int l = 0; l < VDEV_LABELS; l++) {
1721 err = vdev_label_write(vdev, l, be,
1722 offsetof(vdev_label_t, vl_be));
1723 if (err != 0) {
1724 printf("failed to write bootenv to %s label %d: %d\n",
1725 vdev->v_name ? vdev->v_name : "unknown", l, err);
1726 rv = err;
1727 }
1728 }
1729 return (rv);
1730 }
1731
1732 int
vdev_write_bootenv(vdev_t * vdev,nvlist_t * nvl)1733 vdev_write_bootenv(vdev_t *vdev, nvlist_t *nvl)
1734 {
1735 vdev_boot_envblock_t *be;
1736 nvlist_t nv, *nvp;
1737 uint64_t version;
1738 int rv;
1739
1740 if (nvl->nv_size > sizeof(be->vbe_bootenv))
1741 return (E2BIG);
1742
1743 version = VB_RAW;
1744 nvp = vdev_read_bootenv(vdev);
1745 if (nvp != NULL) {
1746 nvlist_find(nvp, BOOTENV_VERSION, DATA_TYPE_UINT64, NULL,
1747 &version, NULL);
1748 nvlist_destroy(nvp);
1749 }
1750
1751 be = calloc(1, sizeof(*be));
1752 if (be == NULL)
1753 return (ENOMEM);
1754
1755 be->vbe_version = version;
1756 switch (version) {
1757 case VB_RAW:
1758 /*
1759 * If there is no envmap, we will just wipe bootenv.
1760 */
1761 nvlist_find(nvl, GRUB_ENVMAP, DATA_TYPE_STRING, NULL,
1762 be->vbe_bootenv, NULL);
1763 rv = 0;
1764 break;
1765
1766 case VB_NVLIST:
1767 nv.nv_header = nvl->nv_header;
1768 nv.nv_asize = nvl->nv_asize;
1769 nv.nv_size = nvl->nv_size;
1770
1771 bcopy(&nv.nv_header, be->vbe_bootenv, sizeof(nv.nv_header));
1772 nv.nv_data = be->vbe_bootenv + sizeof(nvs_header_t);
1773 bcopy(nvl->nv_data, nv.nv_data, nv.nv_size);
1774 rv = nvlist_export(&nv);
1775 break;
1776
1777 default:
1778 rv = EINVAL;
1779 break;
1780 }
1781
1782 if (rv == 0) {
1783 be->vbe_version = htobe64(be->vbe_version);
1784 rv = vdev_write_bootenv_impl(vdev, be);
1785 }
1786 free(be);
1787 return (rv);
1788 }
1789
1790 /*
1791 * Read the bootenv area from pool label, return the nvlist from it.
1792 * We return from first successful read.
1793 */
1794 nvlist_t *
vdev_read_bootenv(vdev_t * vdev)1795 vdev_read_bootenv(vdev_t *vdev)
1796 {
1797 vdev_t *kid;
1798 nvlist_t *benv;
1799 vdev_boot_envblock_t *be;
1800 char *command;
1801 bool ok;
1802 int rv;
1803
1804 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1805 if (kid->v_state != VDEV_STATE_HEALTHY)
1806 continue;
1807
1808 benv = vdev_read_bootenv(kid);
1809 if (benv != NULL)
1810 return (benv);
1811 }
1812
1813 be = malloc(sizeof (*be));
1814 if (be == NULL)
1815 return (NULL);
1816
1817 rv = 0;
1818 for (int l = 0; l < VDEV_LABELS; l++) {
1819 rv = vdev_label_read(vdev, l, be,
1820 offsetof(vdev_label_t, vl_be),
1821 sizeof (*be));
1822 if (rv == 0)
1823 break;
1824 }
1825 if (rv != 0) {
1826 free(be);
1827 return (NULL);
1828 }
1829
1830 be->vbe_version = be64toh(be->vbe_version);
1831 switch (be->vbe_version) {
1832 case VB_RAW:
1833 /*
1834 * we have textual data in vbe_bootenv, create nvlist
1835 * with key "envmap".
1836 */
1837 benv = nvlist_create(NV_UNIQUE_NAME);
1838 if (benv != NULL) {
1839 if (*be->vbe_bootenv == '\0') {
1840 nvlist_add_uint64(benv, BOOTENV_VERSION,
1841 VB_NVLIST);
1842 break;
1843 }
1844 nvlist_add_uint64(benv, BOOTENV_VERSION, VB_RAW);
1845 be->vbe_bootenv[sizeof (be->vbe_bootenv) - 1] = '\0';
1846 nvlist_add_string(benv, GRUB_ENVMAP, be->vbe_bootenv);
1847 }
1848 break;
1849
1850 case VB_NVLIST:
1851 benv = nvlist_import(be->vbe_bootenv, sizeof(be->vbe_bootenv));
1852 break;
1853
1854 default:
1855 command = (char *)be;
1856 ok = false;
1857
1858 /* Check for legacy zfsbootcfg command string */
1859 for (int i = 0; command[i] != '\0'; i++) {
1860 if (iscntrl(command[i])) {
1861 ok = false;
1862 break;
1863 } else {
1864 ok = true;
1865 }
1866 }
1867 benv = nvlist_create(NV_UNIQUE_NAME);
1868 if (benv != NULL) {
1869 if (ok)
1870 nvlist_add_string(benv, FREEBSD_BOOTONCE,
1871 command);
1872 else
1873 nvlist_add_uint64(benv, BOOTENV_VERSION,
1874 VB_NVLIST);
1875 }
1876 break;
1877 }
1878 free(be);
1879 return (benv);
1880 }
1881
1882 static uint64_t
vdev_get_label_asize(nvlist_t * nvl)1883 vdev_get_label_asize(nvlist_t *nvl)
1884 {
1885 nvlist_t *vdevs;
1886 uint64_t asize;
1887 const char *type;
1888 int len;
1889
1890 asize = 0;
1891 /* Get vdev tree */
1892 if (nvlist_find(nvl, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1893 NULL, &vdevs, NULL) != 0)
1894 return (asize);
1895
1896 /*
1897 * Get vdev type. We will calculate asize for raidz, mirror and disk.
1898 * For raidz, the asize is raw size of all children.
1899 */
1900 if (nvlist_find(vdevs, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
1901 NULL, &type, &len) != 0)
1902 goto done;
1903
1904 if (memcmp(type, VDEV_TYPE_MIRROR, len) != 0 &&
1905 memcmp(type, VDEV_TYPE_DISK, len) != 0 &&
1906 memcmp(type, VDEV_TYPE_RAIDZ, len) != 0)
1907 goto done;
1908
1909 if (nvlist_find(vdevs, ZPOOL_CONFIG_ASIZE, DATA_TYPE_UINT64,
1910 NULL, &asize, NULL) != 0)
1911 goto done;
1912
1913 if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0) {
1914 nvlist_t **kids;
1915 int nkids;
1916
1917 if (nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN,
1918 DATA_TYPE_NVLIST_ARRAY, &nkids, &kids, NULL) != 0) {
1919 asize = 0;
1920 goto done;
1921 }
1922
1923 asize /= nkids;
1924 for (int i = 0; i < nkids; i++)
1925 nvlist_destroy(kids[i]);
1926 free(kids);
1927 }
1928
1929 asize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
1930 done:
1931 nvlist_destroy(vdevs);
1932 return (asize);
1933 }
1934
1935 static nvlist_t *
vdev_label_read_config(vdev_t * vd,uint64_t txg)1936 vdev_label_read_config(vdev_t *vd, uint64_t txg)
1937 {
1938 vdev_phys_t *label;
1939 uint64_t best_txg = 0;
1940 uint64_t label_txg = 0;
1941 uint64_t asize;
1942 nvlist_t *nvl = NULL, *tmp;
1943 int error;
1944
1945 label = malloc(sizeof (vdev_phys_t));
1946 if (label == NULL)
1947 return (NULL);
1948
1949 for (int l = 0; l < VDEV_LABELS; l++) {
1950 if (vdev_label_read(vd, l, label,
1951 offsetof(vdev_label_t, vl_vdev_phys),
1952 sizeof (vdev_phys_t)))
1953 continue;
1954
1955 tmp = nvlist_import(label->vp_nvlist,
1956 sizeof(label->vp_nvlist));
1957 if (tmp == NULL)
1958 continue;
1959
1960 error = nvlist_find(tmp, ZPOOL_CONFIG_POOL_TXG,
1961 DATA_TYPE_UINT64, NULL, &label_txg, NULL);
1962 if (error != 0 || label_txg == 0) {
1963 nvlist_destroy(nvl);
1964 nvl = tmp;
1965 goto done;
1966 }
1967
1968 if (label_txg <= txg && label_txg > best_txg) {
1969 best_txg = label_txg;
1970 nvlist_destroy(nvl);
1971 nvl = tmp;
1972 tmp = NULL;
1973
1974 /*
1975 * Use asize from pool config. We need this
1976 * because we can get bad value from BIOS.
1977 */
1978 asize = vdev_get_label_asize(nvl);
1979 if (asize != 0) {
1980 vd->v_psize = asize;
1981 }
1982 }
1983 nvlist_destroy(tmp);
1984 }
1985
1986 if (best_txg == 0) {
1987 nvlist_destroy(nvl);
1988 nvl = NULL;
1989 }
1990 done:
1991 free(label);
1992 return (nvl);
1993 }
1994
1995 static void
vdev_uberblock_load(vdev_t * vd,uberblock_t * ub)1996 vdev_uberblock_load(vdev_t *vd, uberblock_t *ub)
1997 {
1998 uberblock_t *buf;
1999
2000 buf = malloc(VDEV_UBERBLOCK_SIZE(vd));
2001 if (buf == NULL)
2002 return;
2003
2004 for (int l = 0; l < VDEV_LABELS; l++) {
2005 for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
2006 if (vdev_label_read(vd, l, buf,
2007 VDEV_UBERBLOCK_OFFSET(vd, n),
2008 VDEV_UBERBLOCK_SIZE(vd)))
2009 continue;
2010 if (uberblock_verify(buf) != 0)
2011 continue;
2012
2013 if (vdev_uberblock_compare(buf, ub) > 0)
2014 *ub = *buf;
2015 }
2016 }
2017 free(buf);
2018 }
2019
2020 static int
vdev_probe(vdev_phys_read_t * _read,vdev_phys_write_t * _write,void * priv,spa_t ** spap)2021 vdev_probe(vdev_phys_read_t *_read, vdev_phys_write_t *_write, void *priv,
2022 spa_t **spap)
2023 {
2024 vdev_t vtmp;
2025 spa_t *spa;
2026 vdev_t *vdev;
2027 nvlist_t *nvl;
2028 uint64_t val;
2029 uint64_t guid, pool_guid, top_guid, txg;
2030 const char *pool_name;
2031 int rc, namelen;
2032
2033 /*
2034 * Load the vdev label and figure out which
2035 * uberblock is most current.
2036 */
2037 memset(&vtmp, 0, sizeof(vtmp));
2038 vtmp.v_phys_read = _read;
2039 vtmp.v_phys_write = _write;
2040 vtmp.v_priv = priv;
2041 vtmp.v_psize = P2ALIGN(ldi_get_size(priv),
2042 (uint64_t)sizeof (vdev_label_t));
2043
2044 /* Test for minimum device size. */
2045 if (vtmp.v_psize < SPA_MINDEVSIZE)
2046 return (EIO);
2047
2048 nvl = vdev_label_read_config(&vtmp, UINT64_MAX);
2049 if (nvl == NULL)
2050 return (EIO);
2051
2052 if (nvlist_find(nvl, ZPOOL_CONFIG_VERSION, DATA_TYPE_UINT64,
2053 NULL, &val, NULL) != 0) {
2054 nvlist_destroy(nvl);
2055 return (EIO);
2056 }
2057
2058 if (!SPA_VERSION_IS_SUPPORTED(val)) {
2059 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
2060 (unsigned)val, (unsigned)SPA_VERSION);
2061 nvlist_destroy(nvl);
2062 return (EIO);
2063 }
2064
2065 /* Check ZFS features for read */
2066 rc = nvlist_check_features_for_read(nvl);
2067 if (rc != 0) {
2068 nvlist_destroy(nvl);
2069 return (EIO);
2070 }
2071
2072 if (nvlist_find(nvl, ZPOOL_CONFIG_POOL_STATE, DATA_TYPE_UINT64,
2073 NULL, &val, NULL) != 0) {
2074 nvlist_destroy(nvl);
2075 return (EIO);
2076 }
2077
2078 if (val == POOL_STATE_DESTROYED) {
2079 /* We don't boot only from destroyed pools. */
2080 nvlist_destroy(nvl);
2081 return (EIO);
2082 }
2083
2084 if (nvlist_find(nvl, ZPOOL_CONFIG_POOL_TXG, DATA_TYPE_UINT64,
2085 NULL, &txg, NULL) != 0 ||
2086 nvlist_find(nvl, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64,
2087 NULL, &top_guid, NULL) != 0 ||
2088 nvlist_find(nvl, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
2089 NULL, &pool_guid, NULL) != 0 ||
2090 nvlist_find(nvl, ZPOOL_CONFIG_POOL_NAME, DATA_TYPE_STRING,
2091 NULL, &pool_name, &namelen) != 0 ||
2092 nvlist_find(nvl, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
2093 NULL, &guid, NULL) != 0) {
2094 /*
2095 * Cache and spare devices end up here - just ignore
2096 * them.
2097 */
2098 nvlist_destroy(nvl);
2099 return (EIO);
2100 }
2101
2102 /*
2103 * Create the pool if this is the first time we've seen it.
2104 */
2105 spa = spa_find_by_guid(pool_guid);
2106 if (spa == NULL) {
2107 char *name;
2108
2109 name = malloc(namelen + 1);
2110 if (name == NULL) {
2111 nvlist_destroy(nvl);
2112 return (ENOMEM);
2113 }
2114 bcopy(pool_name, name, namelen);
2115 name[namelen] = '\0';
2116 spa = spa_create(pool_guid, name);
2117 free(name);
2118 if (spa == NULL) {
2119 nvlist_destroy(nvl);
2120 return (ENOMEM);
2121 }
2122 } else {
2123 struct vdev *kid;
2124
2125 STAILQ_FOREACH(kid, &spa->spa_root_vdev->v_children,
2126 v_childlink)
2127 if (kid->v_guid == top_guid && kid->v_txg < txg) {
2128 printf("ZFS: pool %s vdev %s ignoring stale "
2129 "label from txg 0x%jx, using 0x%jx@0x%jx\n",
2130 spa->spa_name, kid->v_name,
2131 kid->v_txg, guid, txg);
2132 STAILQ_REMOVE(&spa->spa_root_vdev->v_children,
2133 kid, vdev, v_childlink);
2134 vdev_free(kid);
2135 break;
2136 }
2137 }
2138
2139 /*
2140 * Get the vdev tree and create our in-core copy of it.
2141 * If we already have a vdev with this guid, this must
2142 * be some kind of alias (overlapping slices, dangerously dedicated
2143 * disks etc).
2144 */
2145 vdev = vdev_find(guid);
2146 /* Has this vdev already been inited? */
2147 if (vdev && vdev->v_phys_read) {
2148 nvlist_destroy(nvl);
2149 return (EIO);
2150 }
2151
2152 rc = vdev_init_from_label(spa, nvl);
2153 nvlist_destroy(nvl);
2154 if (rc != 0)
2155 return (rc);
2156
2157 /*
2158 * We should already have created an incomplete vdev for this
2159 * vdev. Find it and initialise it with our read proc.
2160 */
2161 vdev = vdev_find(guid);
2162 if (vdev != NULL) {
2163 vdev->v_phys_read = _read;
2164 vdev->v_phys_write = _write;
2165 vdev->v_priv = priv;
2166 vdev->v_psize = vtmp.v_psize;
2167 /*
2168 * If no other state is set, mark vdev healthy.
2169 */
2170 if (vdev->v_state == VDEV_STATE_UNKNOWN)
2171 vdev->v_state = VDEV_STATE_HEALTHY;
2172 } else {
2173 printf("ZFS: inconsistent nvlist contents\n");
2174 return (EIO);
2175 }
2176
2177 if (vdev->v_islog)
2178 spa->spa_with_log = vdev->v_islog;
2179
2180 /*
2181 * Re-evaluate top-level vdev state.
2182 */
2183 vdev_set_state(vdev->v_top);
2184
2185 /*
2186 * Ok, we are happy with the pool so far. Lets find
2187 * the best uberblock and then we can actually access
2188 * the contents of the pool.
2189 */
2190 vdev_uberblock_load(vdev, spa->spa_uberblock);
2191
2192 if (spap != NULL)
2193 *spap = spa;
2194 return (0);
2195 }
2196
2197 static int
ilog2(int n)2198 ilog2(int n)
2199 {
2200 int v;
2201
2202 for (v = 0; v < 32; v++)
2203 if (n == (1 << v))
2204 return (v);
2205 return (-1);
2206 }
2207
2208 static int
zio_read_gang(const spa_t * spa,const blkptr_t * bp,void * buf)2209 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
2210 {
2211 blkptr_t gbh_bp;
2212 zio_gbh_phys_t zio_gb;
2213 char *pbuf;
2214 int i;
2215
2216 /* Artificial BP for gang block header. */
2217 gbh_bp = *bp;
2218 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
2219 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
2220 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
2221 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
2222 for (i = 0; i < SPA_DVAS_PER_BP; i++)
2223 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
2224
2225 /* Read gang header block using the artificial BP. */
2226 if (zio_read(spa, &gbh_bp, &zio_gb))
2227 return (EIO);
2228
2229 pbuf = buf;
2230 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
2231 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
2232
2233 if (BP_IS_HOLE(gbp))
2234 continue;
2235 if (zio_read(spa, gbp, pbuf))
2236 return (EIO);
2237 pbuf += BP_GET_PSIZE(gbp);
2238 }
2239
2240 if (zio_checksum_verify(spa, bp, buf))
2241 return (EIO);
2242 return (0);
2243 }
2244
2245 static int
zio_read(const spa_t * spa,const blkptr_t * bp,void * buf)2246 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
2247 {
2248 int cpfunc = BP_GET_COMPRESS(bp);
2249 uint64_t align, size;
2250 void *pbuf;
2251 int i, error;
2252
2253 /*
2254 * Process data embedded in block pointer
2255 */
2256 if (BP_IS_EMBEDDED(bp)) {
2257 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
2258
2259 size = BPE_GET_PSIZE(bp);
2260 ASSERT(size <= BPE_PAYLOAD_SIZE);
2261
2262 if (cpfunc != ZIO_COMPRESS_OFF)
2263 pbuf = malloc(size);
2264 else
2265 pbuf = buf;
2266
2267 if (pbuf == NULL)
2268 return (ENOMEM);
2269
2270 decode_embedded_bp_compressed(bp, pbuf);
2271 error = 0;
2272
2273 if (cpfunc != ZIO_COMPRESS_OFF) {
2274 error = zio_decompress_data(cpfunc, pbuf,
2275 size, buf, BP_GET_LSIZE(bp));
2276 free(pbuf);
2277 }
2278 if (error != 0)
2279 printf("ZFS: i/o error - unable to decompress "
2280 "block pointer data, error %d\n", error);
2281 return (error);
2282 }
2283
2284 error = EIO;
2285
2286 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
2287 const dva_t *dva = &bp->blk_dva[i];
2288 vdev_t *vdev;
2289 vdev_list_t *vlist;
2290 uint64_t vdevid;
2291 off_t offset;
2292
2293 if (!dva->dva_word[0] && !dva->dva_word[1])
2294 continue;
2295
2296 vdevid = DVA_GET_VDEV(dva);
2297 offset = DVA_GET_OFFSET(dva);
2298 vlist = &spa->spa_root_vdev->v_children;
2299 STAILQ_FOREACH(vdev, vlist, v_childlink) {
2300 if (vdev->v_id == vdevid)
2301 break;
2302 }
2303 if (!vdev || !vdev->v_read)
2304 continue;
2305
2306 size = BP_GET_PSIZE(bp);
2307 if (vdev->v_read == vdev_raidz_read) {
2308 align = 1ULL << vdev->v_ashift;
2309 if (P2PHASE(size, align) != 0)
2310 size = P2ROUNDUP(size, align);
2311 }
2312 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
2313 pbuf = malloc(size);
2314 else
2315 pbuf = buf;
2316
2317 if (pbuf == NULL) {
2318 error = ENOMEM;
2319 break;
2320 }
2321
2322 if (DVA_GET_GANG(dva))
2323 error = zio_read_gang(spa, bp, pbuf);
2324 else
2325 error = vdev->v_read(vdev, bp, pbuf, offset, size);
2326 if (error == 0) {
2327 if (cpfunc != ZIO_COMPRESS_OFF)
2328 error = zio_decompress_data(cpfunc, pbuf,
2329 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
2330 else if (size != BP_GET_PSIZE(bp))
2331 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
2332 } else {
2333 printf("zio_read error: %d\n", error);
2334 }
2335 if (buf != pbuf)
2336 free(pbuf);
2337 if (error == 0)
2338 break;
2339 }
2340 if (error != 0)
2341 printf("ZFS: i/o error - all block copies unavailable\n");
2342
2343 return (error);
2344 }
2345
2346 static int
dnode_read(const spa_t * spa,const dnode_phys_t * dnode,off_t offset,void * buf,size_t buflen)2347 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset,
2348 void *buf, size_t buflen)
2349 {
2350 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
2351 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2352 int nlevels = dnode->dn_nlevels;
2353 int i, rc;
2354
2355 if (bsize > SPA_MAXBLOCKSIZE) {
2356 printf("ZFS: I/O error - blocks larger than %llu are not "
2357 "supported\n", SPA_MAXBLOCKSIZE);
2358 return (EIO);
2359 }
2360
2361 /*
2362 * Handle odd block sizes, mirrors dmu_read_impl(). Data can't exist
2363 * past the first block, so we'll clip the read to the portion of the
2364 * buffer within bsize and zero out the remainder.
2365 */
2366 if (dnode->dn_maxblkid == 0) {
2367 size_t newbuflen;
2368
2369 newbuflen = offset > bsize ? 0 : MIN(buflen, bsize - offset);
2370 bzero((char *)buf + newbuflen, buflen - newbuflen);
2371 buflen = newbuflen;
2372 }
2373
2374 /*
2375 * Note: bsize may not be a power of two here so we need to do an
2376 * actual divide rather than a bitshift.
2377 */
2378 while (buflen > 0) {
2379 uint64_t bn = offset / bsize;
2380 int boff = offset % bsize;
2381 int ibn;
2382 const blkptr_t *indbp;
2383 blkptr_t bp;
2384
2385 if (bn > dnode->dn_maxblkid)
2386 return (EIO);
2387
2388 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
2389 goto cached;
2390
2391 indbp = dnode->dn_blkptr;
2392 for (i = 0; i < nlevels; i++) {
2393 /*
2394 * Copy the bp from the indirect array so that
2395 * we can re-use the scratch buffer for multi-level
2396 * objects.
2397 */
2398 ibn = bn >> ((nlevels - i - 1) * ibshift);
2399 ibn &= ((1 << ibshift) - 1);
2400 bp = indbp[ibn];
2401 if (BP_IS_HOLE(&bp)) {
2402 memset(dnode_cache_buf, 0, bsize);
2403 break;
2404 }
2405 rc = zio_read(spa, &bp, dnode_cache_buf);
2406 if (rc)
2407 return (rc);
2408 indbp = (const blkptr_t *) dnode_cache_buf;
2409 }
2410 dnode_cache_obj = dnode;
2411 dnode_cache_bn = bn;
2412 cached:
2413
2414 /*
2415 * The buffer contains our data block. Copy what we
2416 * need from it and loop.
2417 */
2418 i = bsize - boff;
2419 if (i > buflen) i = buflen;
2420 memcpy(buf, &dnode_cache_buf[boff], i);
2421 buf = ((char *)buf) + i;
2422 offset += i;
2423 buflen -= i;
2424 }
2425
2426 return (0);
2427 }
2428
2429 /*
2430 * Lookup a value in a microzap directory.
2431 */
2432 static int
mzap_lookup(const mzap_phys_t * mz,size_t size,const char * name,uint64_t * value)2433 mzap_lookup(const mzap_phys_t *mz, size_t size, const char *name,
2434 uint64_t *value)
2435 {
2436 const mzap_ent_phys_t *mze;
2437 int chunks, i;
2438
2439 /*
2440 * Microzap objects use exactly one block. Read the whole
2441 * thing.
2442 */
2443 chunks = size / MZAP_ENT_LEN - 1;
2444 for (i = 0; i < chunks; i++) {
2445 mze = &mz->mz_chunk[i];
2446 if (strcmp(mze->mze_name, name) == 0) {
2447 *value = mze->mze_value;
2448 return (0);
2449 }
2450 }
2451
2452 return (ENOENT);
2453 }
2454
2455 /*
2456 * Compare a name with a zap leaf entry. Return non-zero if the name
2457 * matches.
2458 */
2459 static int
fzap_name_equal(const zap_leaf_t * zl,const zap_leaf_chunk_t * zc,const char * name)2460 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2461 const char *name)
2462 {
2463 size_t namelen;
2464 const zap_leaf_chunk_t *nc;
2465 const char *p;
2466
2467 namelen = zc->l_entry.le_name_numints;
2468
2469 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2470 p = name;
2471 while (namelen > 0) {
2472 size_t len;
2473
2474 len = namelen;
2475 if (len > ZAP_LEAF_ARRAY_BYTES)
2476 len = ZAP_LEAF_ARRAY_BYTES;
2477 if (memcmp(p, nc->l_array.la_array, len))
2478 return (0);
2479 p += len;
2480 namelen -= len;
2481 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2482 }
2483
2484 return (1);
2485 }
2486
2487 /*
2488 * Extract a uint64_t value from a zap leaf entry.
2489 */
2490 static uint64_t
fzap_leaf_value(const zap_leaf_t * zl,const zap_leaf_chunk_t * zc)2491 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
2492 {
2493 const zap_leaf_chunk_t *vc;
2494 int i;
2495 uint64_t value;
2496 const uint8_t *p;
2497
2498 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
2499 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
2500 value = (value << 8) | p[i];
2501 }
2502
2503 return (value);
2504 }
2505
2506 static void
stv(int len,void * addr,uint64_t value)2507 stv(int len, void *addr, uint64_t value)
2508 {
2509 switch (len) {
2510 case 1:
2511 *(uint8_t *)addr = value;
2512 return;
2513 case 2:
2514 *(uint16_t *)addr = value;
2515 return;
2516 case 4:
2517 *(uint32_t *)addr = value;
2518 return;
2519 case 8:
2520 *(uint64_t *)addr = value;
2521 return;
2522 }
2523 }
2524
2525 /*
2526 * Extract a array from a zap leaf entry.
2527 */
2528 static void
fzap_leaf_array(const zap_leaf_t * zl,const zap_leaf_chunk_t * zc,uint64_t integer_size,uint64_t num_integers,void * buf)2529 fzap_leaf_array(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2530 uint64_t integer_size, uint64_t num_integers, void *buf)
2531 {
2532 uint64_t array_int_len = zc->l_entry.le_value_intlen;
2533 uint64_t value = 0;
2534 uint64_t *u64 = buf;
2535 char *p = buf;
2536 int len = MIN(zc->l_entry.le_value_numints, num_integers);
2537 int chunk = zc->l_entry.le_value_chunk;
2538 int byten = 0;
2539
2540 if (integer_size == 8 && len == 1) {
2541 *u64 = fzap_leaf_value(zl, zc);
2542 return;
2543 }
2544
2545 while (len > 0) {
2546 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array;
2547 int i;
2548
2549 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(zl));
2550 for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) {
2551 value = (value << 8) | la->la_array[i];
2552 byten++;
2553 if (byten == array_int_len) {
2554 stv(integer_size, p, value);
2555 byten = 0;
2556 len--;
2557 if (len == 0)
2558 return;
2559 p += integer_size;
2560 }
2561 }
2562 chunk = la->la_next;
2563 }
2564 }
2565
2566 static int
fzap_check_size(uint64_t integer_size,uint64_t num_integers)2567 fzap_check_size(uint64_t integer_size, uint64_t num_integers)
2568 {
2569
2570 switch (integer_size) {
2571 case 1:
2572 case 2:
2573 case 4:
2574 case 8:
2575 break;
2576 default:
2577 return (EINVAL);
2578 }
2579
2580 if (integer_size * num_integers > ZAP_MAXVALUELEN)
2581 return (E2BIG);
2582
2583 return (0);
2584 }
2585
2586 static void
zap_leaf_free(zap_leaf_t * leaf)2587 zap_leaf_free(zap_leaf_t *leaf)
2588 {
2589 free(leaf->l_phys);
2590 free(leaf);
2591 }
2592
2593 static int
zap_get_leaf_byblk(fat_zap_t * zap,uint64_t blk,zap_leaf_t ** lp)2594 zap_get_leaf_byblk(fat_zap_t *zap, uint64_t blk, zap_leaf_t **lp)
2595 {
2596 int bs = FZAP_BLOCK_SHIFT(zap);
2597 int err;
2598
2599 *lp = malloc(sizeof(**lp));
2600 if (*lp == NULL)
2601 return (ENOMEM);
2602
2603 (*lp)->l_bs = bs;
2604 (*lp)->l_phys = malloc(1 << bs);
2605
2606 if ((*lp)->l_phys == NULL) {
2607 free(*lp);
2608 return (ENOMEM);
2609 }
2610 err = dnode_read(zap->zap_spa, zap->zap_dnode, blk << bs, (*lp)->l_phys,
2611 1 << bs);
2612 if (err != 0) {
2613 zap_leaf_free(*lp);
2614 }
2615 return (err);
2616 }
2617
2618 static int
zap_table_load(fat_zap_t * zap,zap_table_phys_t * tbl,uint64_t idx,uint64_t * valp)2619 zap_table_load(fat_zap_t *zap, zap_table_phys_t *tbl, uint64_t idx,
2620 uint64_t *valp)
2621 {
2622 int bs = FZAP_BLOCK_SHIFT(zap);
2623 uint64_t blk = idx >> (bs - 3);
2624 uint64_t off = idx & ((1 << (bs - 3)) - 1);
2625 uint64_t *buf;
2626 int rc;
2627
2628 buf = malloc(1 << zap->zap_block_shift);
2629 if (buf == NULL)
2630 return (ENOMEM);
2631 rc = dnode_read(zap->zap_spa, zap->zap_dnode, (tbl->zt_blk + blk) << bs,
2632 buf, 1 << zap->zap_block_shift);
2633 if (rc == 0)
2634 *valp = buf[off];
2635 free(buf);
2636 return (rc);
2637 }
2638
2639 static int
zap_idx_to_blk(fat_zap_t * zap,uint64_t idx,uint64_t * valp)2640 zap_idx_to_blk(fat_zap_t *zap, uint64_t idx, uint64_t *valp)
2641 {
2642 if (zap->zap_phys->zap_ptrtbl.zt_numblks == 0) {
2643 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
2644 return (0);
2645 } else {
2646 return (zap_table_load(zap, &zap->zap_phys->zap_ptrtbl,
2647 idx, valp));
2648 }
2649 }
2650
2651 #define ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n))))
2652 static int
zap_deref_leaf(fat_zap_t * zap,uint64_t h,zap_leaf_t ** lp)2653 zap_deref_leaf(fat_zap_t *zap, uint64_t h, zap_leaf_t **lp)
2654 {
2655 uint64_t idx, blk;
2656 int err;
2657
2658 idx = ZAP_HASH_IDX(h, zap->zap_phys->zap_ptrtbl.zt_shift);
2659 err = zap_idx_to_blk(zap, idx, &blk);
2660 if (err != 0)
2661 return (err);
2662 return (zap_get_leaf_byblk(zap, blk, lp));
2663 }
2664
2665 #define CHAIN_END 0xffff /* end of the chunk chain */
2666 #define LEAF_HASH(l, h) \
2667 ((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \
2668 ((h) >> \
2669 (64 - ZAP_LEAF_HASH_SHIFT(l) - (l)->l_phys->l_hdr.lh_prefix_len)))
2670 #define LEAF_HASH_ENTPTR(l, h) (&(l)->l_phys->l_hash[LEAF_HASH(l, h)])
2671
2672 static int
zap_leaf_lookup(zap_leaf_t * zl,uint64_t hash,const char * name,uint64_t integer_size,uint64_t num_integers,void * value)2673 zap_leaf_lookup(zap_leaf_t *zl, uint64_t hash, const char *name,
2674 uint64_t integer_size, uint64_t num_integers, void *value)
2675 {
2676 int rc;
2677 uint16_t *chunkp;
2678 struct zap_leaf_entry *le;
2679
2680 /*
2681 * Make sure this chunk matches our hash.
2682 */
2683 if (zl->l_phys->l_hdr.lh_prefix_len > 0 &&
2684 zl->l_phys->l_hdr.lh_prefix !=
2685 hash >> (64 - zl->l_phys->l_hdr.lh_prefix_len))
2686 return (EIO);
2687
2688 rc = ENOENT;
2689 for (chunkp = LEAF_HASH_ENTPTR(zl, hash);
2690 *chunkp != CHAIN_END; chunkp = &le->le_next) {
2691 zap_leaf_chunk_t *zc;
2692 uint16_t chunk = *chunkp;
2693
2694 le = ZAP_LEAF_ENTRY(zl, chunk);
2695 if (le->le_hash != hash)
2696 continue;
2697 zc = &ZAP_LEAF_CHUNK(zl, chunk);
2698 if (fzap_name_equal(zl, zc, name)) {
2699 if (zc->l_entry.le_value_intlen > integer_size) {
2700 rc = EINVAL;
2701 } else {
2702 fzap_leaf_array(zl, zc, integer_size,
2703 num_integers, value);
2704 rc = 0;
2705 }
2706 break;
2707 }
2708 }
2709 return (rc);
2710 }
2711
2712 /*
2713 * Lookup a value in a fatzap directory.
2714 */
2715 static int
fzap_lookup(const spa_t * spa,const dnode_phys_t * dnode,zap_phys_t * zh,const char * name,uint64_t integer_size,uint64_t num_integers,void * value)2716 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2717 const char *name, uint64_t integer_size, uint64_t num_integers,
2718 void *value)
2719 {
2720 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2721 fat_zap_t z;
2722 zap_leaf_t *zl;
2723 uint64_t hash;
2724 int rc;
2725
2726 if (zh->zap_magic != ZAP_MAGIC)
2727 return (EIO);
2728
2729 if ((rc = fzap_check_size(integer_size, num_integers)) != 0) {
2730 return (rc);
2731 }
2732
2733 z.zap_block_shift = ilog2(bsize);
2734 z.zap_phys = zh;
2735 z.zap_spa = spa;
2736 z.zap_dnode = dnode;
2737
2738 hash = zap_hash(zh->zap_salt, name);
2739 rc = zap_deref_leaf(&z, hash, &zl);
2740 if (rc != 0)
2741 return (rc);
2742
2743 rc = zap_leaf_lookup(zl, hash, name, integer_size, num_integers, value);
2744
2745 zap_leaf_free(zl);
2746 return (rc);
2747 }
2748
2749 /*
2750 * Lookup a name in a zap object and return its value as a uint64_t.
2751 */
2752 static int
zap_lookup(const spa_t * spa,const dnode_phys_t * dnode,const char * name,uint64_t integer_size,uint64_t num_integers,void * value)2753 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
2754 uint64_t integer_size, uint64_t num_integers, void *value)
2755 {
2756 int rc;
2757 zap_phys_t *zap;
2758 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2759
2760 zap = malloc(size);
2761 if (zap == NULL)
2762 return (ENOMEM);
2763
2764 rc = dnode_read(spa, dnode, 0, zap, size);
2765 if (rc)
2766 goto done;
2767
2768 switch (zap->zap_block_type) {
2769 case ZBT_MICRO:
2770 rc = mzap_lookup((const mzap_phys_t *)zap, size, name, value);
2771 break;
2772 case ZBT_HEADER:
2773 rc = fzap_lookup(spa, dnode, zap, name, integer_size,
2774 num_integers, value);
2775 break;
2776 default:
2777 printf("ZFS: invalid zap_type=%" PRIx64 "\n",
2778 zap->zap_block_type);
2779 rc = EIO;
2780 }
2781 done:
2782 free(zap);
2783 return (rc);
2784 }
2785
2786 /*
2787 * List a microzap directory.
2788 */
2789 static int
mzap_list(const mzap_phys_t * mz,size_t size,int (* callback)(const char *,uint64_t))2790 mzap_list(const mzap_phys_t *mz, size_t size,
2791 int (*callback)(const char *, uint64_t))
2792 {
2793 const mzap_ent_phys_t *mze;
2794 int chunks, i, rc;
2795
2796 /*
2797 * Microzap objects use exactly one block. Read the whole
2798 * thing.
2799 */
2800 rc = 0;
2801 chunks = size / MZAP_ENT_LEN - 1;
2802 for (i = 0; i < chunks; i++) {
2803 mze = &mz->mz_chunk[i];
2804 if (mze->mze_name[0]) {
2805 rc = callback(mze->mze_name, mze->mze_value);
2806 if (rc != 0)
2807 break;
2808 }
2809 }
2810
2811 return (rc);
2812 }
2813
2814 /*
2815 * List a fatzap directory.
2816 */
2817 static int
fzap_list(const spa_t * spa,const dnode_phys_t * dnode,zap_phys_t * zh,int (* callback)(const char *,uint64_t))2818 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2819 int (*callback)(const char *, uint64_t))
2820 {
2821 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2822 fat_zap_t z;
2823 uint64_t i;
2824 int j, rc;
2825
2826 if (zh->zap_magic != ZAP_MAGIC)
2827 return (EIO);
2828
2829 z.zap_block_shift = ilog2(bsize);
2830 z.zap_phys = zh;
2831
2832 /*
2833 * This assumes that the leaf blocks start at block 1. The
2834 * documentation isn't exactly clear on this.
2835 */
2836 zap_leaf_t zl;
2837 zl.l_bs = z.zap_block_shift;
2838 zl.l_phys = malloc(bsize);
2839 if (zl.l_phys == NULL)
2840 return (ENOMEM);
2841
2842 for (i = 0; i < zh->zap_num_leafs; i++) {
2843 off_t off = ((off_t)(i + 1)) << zl.l_bs;
2844 char name[256], *p;
2845 uint64_t value;
2846
2847 if (dnode_read(spa, dnode, off, zl.l_phys, bsize)) {
2848 free(zl.l_phys);
2849 return (EIO);
2850 }
2851
2852 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
2853 zap_leaf_chunk_t *zc, *nc;
2854 int namelen;
2855
2856 zc = &ZAP_LEAF_CHUNK(&zl, j);
2857 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
2858 continue;
2859 namelen = zc->l_entry.le_name_numints;
2860 if (namelen > sizeof(name))
2861 namelen = sizeof(name);
2862
2863 /*
2864 * Paste the name back together.
2865 */
2866 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
2867 p = name;
2868 while (namelen > 0) {
2869 int len;
2870 len = namelen;
2871 if (len > ZAP_LEAF_ARRAY_BYTES)
2872 len = ZAP_LEAF_ARRAY_BYTES;
2873 memcpy(p, nc->l_array.la_array, len);
2874 p += len;
2875 namelen -= len;
2876 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
2877 }
2878
2879 /*
2880 * Assume the first eight bytes of the value are
2881 * a uint64_t.
2882 */
2883 value = fzap_leaf_value(&zl, zc);
2884
2885 /* printf("%s 0x%jx\n", name, (uintmax_t)value); */
2886 rc = callback((const char *)name, value);
2887 if (rc != 0) {
2888 free(zl.l_phys);
2889 return (rc);
2890 }
2891 }
2892 }
2893
2894 free(zl.l_phys);
2895 return (0);
2896 }
2897
zfs_printf(const char * name,uint64_t value __unused)2898 static int zfs_printf(const char *name, uint64_t value __unused)
2899 {
2900
2901 printf("%s\n", name);
2902
2903 return (0);
2904 }
2905
2906 /*
2907 * List a zap directory.
2908 */
2909 static int
zap_list(const spa_t * spa,const dnode_phys_t * dnode)2910 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
2911 {
2912 zap_phys_t *zap;
2913 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2914 int rc;
2915
2916 zap = malloc(size);
2917 if (zap == NULL)
2918 return (ENOMEM);
2919
2920 rc = dnode_read(spa, dnode, 0, zap, size);
2921 if (rc == 0) {
2922 if (zap->zap_block_type == ZBT_MICRO)
2923 rc = mzap_list((const mzap_phys_t *)zap, size,
2924 zfs_printf);
2925 else
2926 rc = fzap_list(spa, dnode, zap, zfs_printf);
2927 }
2928 free(zap);
2929 return (rc);
2930 }
2931
2932 static int
objset_get_dnode(const spa_t * spa,const objset_phys_t * os,uint64_t objnum,dnode_phys_t * dnode)2933 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum,
2934 dnode_phys_t *dnode)
2935 {
2936 off_t offset;
2937
2938 offset = objnum * sizeof(dnode_phys_t);
2939 return dnode_read(spa, &os->os_meta_dnode, offset,
2940 dnode, sizeof(dnode_phys_t));
2941 }
2942
2943 /*
2944 * Lookup a name in a microzap directory.
2945 */
2946 static int
mzap_rlookup(const mzap_phys_t * mz,size_t size,char * name,uint64_t value)2947 mzap_rlookup(const mzap_phys_t *mz, size_t size, char *name, uint64_t value)
2948 {
2949 const mzap_ent_phys_t *mze;
2950 int chunks, i;
2951
2952 /*
2953 * Microzap objects use exactly one block. Read the whole
2954 * thing.
2955 */
2956 chunks = size / MZAP_ENT_LEN - 1;
2957 for (i = 0; i < chunks; i++) {
2958 mze = &mz->mz_chunk[i];
2959 if (value == mze->mze_value) {
2960 strcpy(name, mze->mze_name);
2961 return (0);
2962 }
2963 }
2964
2965 return (ENOENT);
2966 }
2967
2968 static void
fzap_name_copy(const zap_leaf_t * zl,const zap_leaf_chunk_t * zc,char * name)2969 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
2970 {
2971 size_t namelen;
2972 const zap_leaf_chunk_t *nc;
2973 char *p;
2974
2975 namelen = zc->l_entry.le_name_numints;
2976
2977 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2978 p = name;
2979 while (namelen > 0) {
2980 size_t len;
2981 len = namelen;
2982 if (len > ZAP_LEAF_ARRAY_BYTES)
2983 len = ZAP_LEAF_ARRAY_BYTES;
2984 memcpy(p, nc->l_array.la_array, len);
2985 p += len;
2986 namelen -= len;
2987 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2988 }
2989
2990 *p = '\0';
2991 }
2992
2993 static int
fzap_rlookup(const spa_t * spa,const dnode_phys_t * dnode,zap_phys_t * zh,char * name,uint64_t value)2994 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2995 char *name, uint64_t value)
2996 {
2997 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2998 fat_zap_t z;
2999 uint64_t i;
3000 int j, rc;
3001
3002 if (zh->zap_magic != ZAP_MAGIC)
3003 return (EIO);
3004
3005 z.zap_block_shift = ilog2(bsize);
3006 z.zap_phys = zh;
3007
3008 /*
3009 * This assumes that the leaf blocks start at block 1. The
3010 * documentation isn't exactly clear on this.
3011 */
3012 zap_leaf_t zl;
3013 zl.l_bs = z.zap_block_shift;
3014 zl.l_phys = malloc(bsize);
3015 if (zl.l_phys == NULL)
3016 return (ENOMEM);
3017
3018 for (i = 0; i < zh->zap_num_leafs; i++) {
3019 off_t off = ((off_t)(i + 1)) << zl.l_bs;
3020
3021 rc = dnode_read(spa, dnode, off, zl.l_phys, bsize);
3022 if (rc != 0)
3023 goto done;
3024
3025 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
3026 zap_leaf_chunk_t *zc;
3027
3028 zc = &ZAP_LEAF_CHUNK(&zl, j);
3029 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
3030 continue;
3031 if (zc->l_entry.le_value_intlen != 8 ||
3032 zc->l_entry.le_value_numints != 1)
3033 continue;
3034
3035 if (fzap_leaf_value(&zl, zc) == value) {
3036 fzap_name_copy(&zl, zc, name);
3037 goto done;
3038 }
3039 }
3040 }
3041
3042 rc = ENOENT;
3043 done:
3044 free(zl.l_phys);
3045 return (rc);
3046 }
3047
3048 static int
zap_rlookup(const spa_t * spa,const dnode_phys_t * dnode,char * name,uint64_t value)3049 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name,
3050 uint64_t value)
3051 {
3052 zap_phys_t *zap;
3053 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
3054 int rc;
3055
3056 zap = malloc(size);
3057 if (zap == NULL)
3058 return (ENOMEM);
3059
3060 rc = dnode_read(spa, dnode, 0, zap, size);
3061 if (rc == 0) {
3062 if (zap->zap_block_type == ZBT_MICRO)
3063 rc = mzap_rlookup((const mzap_phys_t *)zap, size,
3064 name, value);
3065 else
3066 rc = fzap_rlookup(spa, dnode, zap, name, value);
3067 }
3068 free(zap);
3069 return (rc);
3070 }
3071
3072 static int
zfs_rlookup(const spa_t * spa,uint64_t objnum,char * result)3073 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
3074 {
3075 char name[256];
3076 char component[256];
3077 uint64_t dir_obj, parent_obj, child_dir_zapobj;
3078 dnode_phys_t child_dir_zap, snapnames_zap, dataset, dir, parent;
3079 dsl_dir_phys_t *dd;
3080 dsl_dataset_phys_t *ds;
3081 char *p;
3082 int len;
3083 boolean_t issnap = B_FALSE;
3084
3085 p = &name[sizeof(name) - 1];
3086 *p = '\0';
3087
3088 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3089 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3090 return (EIO);
3091 }
3092 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3093 dir_obj = ds->ds_dir_obj;
3094 if (ds->ds_snapnames_zapobj == 0)
3095 issnap = B_TRUE;
3096
3097 for (;;) {
3098 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir) != 0)
3099 return (EIO);
3100 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3101
3102 /* Actual loop condition. */
3103 parent_obj = dd->dd_parent_obj;
3104 if (parent_obj == 0)
3105 break;
3106
3107 if (objset_get_dnode(spa, spa->spa_mos, parent_obj,
3108 &parent) != 0)
3109 return (EIO);
3110 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
3111 if (issnap == B_TRUE) {
3112 /*
3113 * The dataset we are looking up is a snapshot
3114 * the dir_obj is the parent already, we don't want
3115 * the grandparent just yet. Reset to the parent.
3116 */
3117 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3118 /* Lookup the dataset to get the snapname ZAP */
3119 if (objset_get_dnode(spa, spa->spa_mos,
3120 dd->dd_head_dataset_obj, &dataset))
3121 return (EIO);
3122 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3123 if (objset_get_dnode(spa, spa->spa_mos,
3124 ds->ds_snapnames_zapobj, &snapnames_zap) != 0)
3125 return (EIO);
3126 /* Get the name of the snapshot */
3127 if (zap_rlookup(spa, &snapnames_zap, component,
3128 objnum) != 0)
3129 return (EIO);
3130 len = strlen(component);
3131 p -= len;
3132 memcpy(p, component, len);
3133 --p;
3134 *p = '@';
3135 issnap = B_FALSE;
3136 continue;
3137 }
3138
3139 child_dir_zapobj = dd->dd_child_dir_zapobj;
3140 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3141 &child_dir_zap) != 0)
3142 return (EIO);
3143 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
3144 return (EIO);
3145
3146 len = strlen(component);
3147 p -= len;
3148 memcpy(p, component, len);
3149 --p;
3150 *p = '/';
3151
3152 /* Actual loop iteration. */
3153 dir_obj = parent_obj;
3154 }
3155
3156 if (*p != '\0')
3157 ++p;
3158 strcpy(result, p);
3159
3160 return (0);
3161 }
3162
3163 static int
zfs_lookup_dataset(const spa_t * spa,const char * name,uint64_t * objnum)3164 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
3165 {
3166 char element[256];
3167 uint64_t dir_obj, child_dir_zapobj;
3168 dnode_phys_t child_dir_zap, snapnames_zap, dir, dataset;
3169 dsl_dir_phys_t *dd;
3170 dsl_dataset_phys_t *ds;
3171 const char *p, *q;
3172 boolean_t issnap = B_FALSE;
3173
3174 if (objset_get_dnode(spa, spa->spa_mos,
3175 DMU_POOL_DIRECTORY_OBJECT, &dir))
3176 return (EIO);
3177 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj),
3178 1, &dir_obj))
3179 return (EIO);
3180
3181 p = name;
3182 for (;;) {
3183 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir))
3184 return (EIO);
3185 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3186
3187 while (*p == '/')
3188 p++;
3189 /* Actual loop condition #1. */
3190 if (*p == '\0')
3191 break;
3192
3193 q = strchr(p, '/');
3194 if (q) {
3195 memcpy(element, p, q - p);
3196 element[q - p] = '\0';
3197 p = q + 1;
3198 } else {
3199 strcpy(element, p);
3200 p += strlen(p);
3201 }
3202
3203 if (issnap == B_TRUE) {
3204 if (objset_get_dnode(spa, spa->spa_mos,
3205 dd->dd_head_dataset_obj, &dataset))
3206 return (EIO);
3207 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3208 if (objset_get_dnode(spa, spa->spa_mos,
3209 ds->ds_snapnames_zapobj, &snapnames_zap) != 0)
3210 return (EIO);
3211 /* Actual loop condition #2. */
3212 if (zap_lookup(spa, &snapnames_zap, element,
3213 sizeof (dir_obj), 1, &dir_obj) != 0)
3214 return (ENOENT);
3215 *objnum = dir_obj;
3216 return (0);
3217 } else if ((q = strchr(element, '@')) != NULL) {
3218 issnap = B_TRUE;
3219 element[q - element] = '\0';
3220 p = q + 1;
3221 }
3222 child_dir_zapobj = dd->dd_child_dir_zapobj;
3223 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3224 &child_dir_zap) != 0)
3225 return (EIO);
3226
3227 /* Actual loop condition #2. */
3228 if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj),
3229 1, &dir_obj) != 0)
3230 return (ENOENT);
3231 }
3232
3233 *objnum = dd->dd_head_dataset_obj;
3234 return (0);
3235 }
3236
3237 #ifndef BOOT2
3238 static int
zfs_list_dataset(const spa_t * spa,uint64_t objnum)3239 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
3240 {
3241 uint64_t dir_obj, child_dir_zapobj;
3242 dnode_phys_t child_dir_zap, dir, dataset;
3243 dsl_dataset_phys_t *ds;
3244 dsl_dir_phys_t *dd;
3245
3246 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3247 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3248 return (EIO);
3249 }
3250 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3251 dir_obj = ds->ds_dir_obj;
3252
3253 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir)) {
3254 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
3255 return (EIO);
3256 }
3257 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3258
3259 child_dir_zapobj = dd->dd_child_dir_zapobj;
3260 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3261 &child_dir_zap) != 0) {
3262 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
3263 return (EIO);
3264 }
3265
3266 return (zap_list(spa, &child_dir_zap) != 0);
3267 }
3268
3269 int
zfs_callback_dataset(const spa_t * spa,uint64_t objnum,int (* callback)(const char *,uint64_t))3270 zfs_callback_dataset(const spa_t *spa, uint64_t objnum,
3271 int (*callback)(const char *, uint64_t))
3272 {
3273 uint64_t dir_obj, child_dir_zapobj;
3274 dnode_phys_t child_dir_zap, dir, dataset;
3275 dsl_dataset_phys_t *ds;
3276 dsl_dir_phys_t *dd;
3277 zap_phys_t *zap;
3278 size_t size;
3279 int err;
3280
3281 err = objset_get_dnode(spa, spa->spa_mos, objnum, &dataset);
3282 if (err != 0) {
3283 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3284 return (err);
3285 }
3286 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3287 dir_obj = ds->ds_dir_obj;
3288
3289 err = objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir);
3290 if (err != 0) {
3291 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
3292 return (err);
3293 }
3294 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3295
3296 child_dir_zapobj = dd->dd_child_dir_zapobj;
3297 err = objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3298 &child_dir_zap);
3299 if (err != 0) {
3300 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
3301 return (err);
3302 }
3303
3304 size = child_dir_zap.dn_datablkszsec << SPA_MINBLOCKSHIFT;
3305 zap = malloc(size);
3306 if (zap != NULL) {
3307 err = dnode_read(spa, &child_dir_zap, 0, zap, size);
3308 if (err != 0)
3309 goto done;
3310
3311 if (zap->zap_block_type == ZBT_MICRO)
3312 err = mzap_list((const mzap_phys_t *)zap, size,
3313 callback);
3314 else
3315 err = fzap_list(spa, &child_dir_zap, zap, callback);
3316 } else {
3317 err = ENOMEM;
3318 }
3319 done:
3320 free(zap);
3321 return (err);
3322 }
3323 #endif
3324
3325 /*
3326 * Find the object set given the object number of its dataset object
3327 * and return its details in *objset
3328 */
3329 static int
zfs_mount_dataset(const spa_t * spa,uint64_t objnum,objset_phys_t * objset)3330 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
3331 {
3332 dnode_phys_t dataset;
3333 dsl_dataset_phys_t *ds;
3334
3335 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3336 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3337 return (EIO);
3338 }
3339
3340 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3341 if (zio_read(spa, &ds->ds_bp, objset)) {
3342 printf("ZFS: can't read object set for dataset %ju\n",
3343 (uintmax_t)objnum);
3344 return (EIO);
3345 }
3346
3347 return (0);
3348 }
3349
3350 /*
3351 * Find the object set pointed to by the BOOTFS property or the root
3352 * dataset if there is none and return its details in *objset
3353 */
3354 static int
zfs_get_root(const spa_t * spa,uint64_t * objid)3355 zfs_get_root(const spa_t *spa, uint64_t *objid)
3356 {
3357 dnode_phys_t dir, propdir;
3358 uint64_t props, bootfs, root;
3359
3360 *objid = 0;
3361
3362 /*
3363 * Start with the MOS directory object.
3364 */
3365 if (objset_get_dnode(spa, spa->spa_mos,
3366 DMU_POOL_DIRECTORY_OBJECT, &dir)) {
3367 printf("ZFS: can't read MOS object directory\n");
3368 return (EIO);
3369 }
3370
3371 /*
3372 * Lookup the pool_props and see if we can find a bootfs.
3373 */
3374 if (zap_lookup(spa, &dir, DMU_POOL_PROPS,
3375 sizeof(props), 1, &props) == 0 &&
3376 objset_get_dnode(spa, spa->spa_mos, props, &propdir) == 0 &&
3377 zap_lookup(spa, &propdir, "bootfs",
3378 sizeof(bootfs), 1, &bootfs) == 0 && bootfs != 0) {
3379 *objid = bootfs;
3380 return (0);
3381 }
3382 /*
3383 * Lookup the root dataset directory
3384 */
3385 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET,
3386 sizeof(root), 1, &root) ||
3387 objset_get_dnode(spa, spa->spa_mos, root, &dir)) {
3388 printf("ZFS: can't find root dsl_dir\n");
3389 return (EIO);
3390 }
3391
3392 /*
3393 * Use the information from the dataset directory's bonus buffer
3394 * to find the dataset object and from that the object set itself.
3395 */
3396 dsl_dir_phys_t *dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3397 *objid = dd->dd_head_dataset_obj;
3398 return (0);
3399 }
3400
3401 static int
zfs_mount_impl(const spa_t * spa,uint64_t rootobj,struct zfsmount * mount)3402 zfs_mount_impl(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
3403 {
3404
3405 mount->spa = spa;
3406
3407 /*
3408 * Find the root object set if not explicitly provided
3409 */
3410 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
3411 printf("ZFS: can't find root filesystem\n");
3412 return (EIO);
3413 }
3414
3415 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
3416 printf("ZFS: can't open root filesystem\n");
3417 return (EIO);
3418 }
3419
3420 mount->rootobj = rootobj;
3421
3422 return (0);
3423 }
3424
3425 /*
3426 * callback function for feature name checks.
3427 */
3428 static int
check_feature(const char * name,uint64_t value)3429 check_feature(const char *name, uint64_t value)
3430 {
3431 int i;
3432
3433 if (value == 0)
3434 return (0);
3435 if (name[0] == '\0')
3436 return (0);
3437
3438 for (i = 0; features_for_read[i] != NULL; i++) {
3439 if (strcmp(name, features_for_read[i]) == 0)
3440 return (0);
3441 }
3442 printf("ZFS: unsupported feature: %s\n", name);
3443 return (EIO);
3444 }
3445
3446 /*
3447 * Checks whether the MOS features that are active are supported.
3448 */
3449 static int
check_mos_features(const spa_t * spa)3450 check_mos_features(const spa_t *spa)
3451 {
3452 dnode_phys_t dir;
3453 zap_phys_t *zap;
3454 uint64_t objnum;
3455 size_t size;
3456 int rc;
3457
3458 if ((rc = objset_get_dnode(spa, spa->spa_mos, DMU_OT_OBJECT_DIRECTORY,
3459 &dir)) != 0)
3460 return (rc);
3461 if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ,
3462 sizeof (objnum), 1, &objnum)) != 0) {
3463 /*
3464 * It is older pool without features. As we have already
3465 * tested the label, just return without raising the error.
3466 */
3467 return (0);
3468 }
3469
3470 if ((rc = objset_get_dnode(spa, spa->spa_mos, objnum, &dir)) != 0)
3471 return (rc);
3472
3473 if (dir.dn_type != DMU_OTN_ZAP_METADATA)
3474 return (EIO);
3475
3476 size = dir.dn_datablkszsec << SPA_MINBLOCKSHIFT;
3477 zap = malloc(size);
3478 if (zap == NULL)
3479 return (ENOMEM);
3480
3481 if (dnode_read(spa, &dir, 0, zap, size)) {
3482 free(zap);
3483 return (EIO);
3484 }
3485
3486 if (zap->zap_block_type == ZBT_MICRO)
3487 rc = mzap_list((const mzap_phys_t *)zap, size, check_feature);
3488 else
3489 rc = fzap_list(spa, &dir, zap, check_feature);
3490
3491 free(zap);
3492 return (rc);
3493 }
3494
3495 static int
load_nvlist(spa_t * spa,uint64_t obj,nvlist_t ** value)3496 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
3497 {
3498 dnode_phys_t dir;
3499 size_t size;
3500 int rc;
3501 char *nv;
3502
3503 *value = NULL;
3504 if ((rc = objset_get_dnode(spa, spa->spa_mos, obj, &dir)) != 0)
3505 return (rc);
3506 if (dir.dn_type != DMU_OT_PACKED_NVLIST &&
3507 dir.dn_bonustype != DMU_OT_PACKED_NVLIST_SIZE) {
3508 return (EIO);
3509 }
3510
3511 if (dir.dn_bonuslen != sizeof (uint64_t))
3512 return (EIO);
3513
3514 size = *(uint64_t *)DN_BONUS(&dir);
3515 nv = malloc(size);
3516 if (nv == NULL)
3517 return (ENOMEM);
3518
3519 rc = dnode_read(spa, &dir, 0, nv, size);
3520 if (rc != 0) {
3521 free(nv);
3522 nv = NULL;
3523 return (rc);
3524 }
3525 *value = nvlist_import(nv, size);
3526 free(nv);
3527 return (rc);
3528 }
3529
3530 static int
zfs_spa_init(spa_t * spa)3531 zfs_spa_init(spa_t *spa)
3532 {
3533 struct uberblock checkpoint;
3534 dnode_phys_t dir;
3535 uint64_t config_object;
3536 nvlist_t *nvlist;
3537 int rc;
3538
3539 if (zio_read(spa, &spa->spa_uberblock->ub_rootbp, spa->spa_mos)) {
3540 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
3541 return (EIO);
3542 }
3543 if (spa->spa_mos->os_type != DMU_OST_META) {
3544 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
3545 return (EIO);
3546 }
3547
3548 if (objset_get_dnode(spa, &spa->spa_mos_master,
3549 DMU_POOL_DIRECTORY_OBJECT, &dir)) {
3550 printf("ZFS: failed to read pool %s directory object\n",
3551 spa->spa_name);
3552 return (EIO);
3553 }
3554 /* this is allowed to fail, older pools do not have salt */
3555 rc = zap_lookup(spa, &dir, DMU_POOL_CHECKSUM_SALT, 1,
3556 sizeof (spa->spa_cksum_salt.zcs_bytes),
3557 spa->spa_cksum_salt.zcs_bytes);
3558
3559 rc = check_mos_features(spa);
3560 if (rc != 0) {
3561 printf("ZFS: pool %s is not supported\n", spa->spa_name);
3562 return (rc);
3563 }
3564
3565 rc = zap_lookup(spa, &dir, DMU_POOL_CONFIG,
3566 sizeof (config_object), 1, &config_object);
3567 if (rc != 0) {
3568 printf("ZFS: can not read MOS %s\n", DMU_POOL_CONFIG);
3569 return (EIO);
3570 }
3571 rc = load_nvlist(spa, config_object, &nvlist);
3572 if (rc != 0) {
3573 printf("ZFS: failed to load pool %s nvlist\n", spa->spa_name);
3574 return (rc);
3575 }
3576
3577 rc = zap_lookup(spa, &dir, DMU_POOL_ZPOOL_CHECKPOINT,
3578 sizeof(uint64_t), sizeof(checkpoint) / sizeof(uint64_t),
3579 &checkpoint);
3580 if (rc == 0 && checkpoint.ub_checkpoint_txg != 0) {
3581 memcpy(&spa->spa_uberblock_checkpoint, &checkpoint,
3582 sizeof(checkpoint));
3583 if (zio_read(spa, &spa->spa_uberblock_checkpoint.ub_rootbp,
3584 &spa->spa_mos_checkpoint)) {
3585 printf("ZFS: can not read checkpoint data.\n");
3586 return (EIO);
3587 }
3588 }
3589
3590 /*
3591 * Update vdevs from MOS config. Note, we do skip encoding bytes
3592 * here. See also vdev_label_read_config().
3593 */
3594 rc = vdev_init_from_nvlist(spa, nvlist);
3595 nvlist_destroy(nvlist);
3596 return (rc);
3597 }
3598
3599 static int
zfs_dnode_stat(const spa_t * spa,dnode_phys_t * dn,struct stat * sb)3600 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
3601 {
3602
3603 if (dn->dn_bonustype != DMU_OT_SA) {
3604 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
3605
3606 sb->st_mode = zp->zp_mode;
3607 sb->st_uid = zp->zp_uid;
3608 sb->st_gid = zp->zp_gid;
3609 sb->st_size = zp->zp_size;
3610 } else {
3611 sa_hdr_phys_t *sahdrp;
3612 int hdrsize;
3613 size_t size = 0;
3614 void *buf = NULL;
3615
3616 if (dn->dn_bonuslen != 0)
3617 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3618 else {
3619 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
3620 blkptr_t *bp = DN_SPILL_BLKPTR(dn);
3621 int error;
3622
3623 size = BP_GET_LSIZE(bp);
3624 buf = malloc(size);
3625 if (buf == NULL)
3626 error = ENOMEM;
3627 else
3628 error = zio_read(spa, bp, buf);
3629
3630 if (error != 0) {
3631 free(buf);
3632 return (error);
3633 }
3634 sahdrp = buf;
3635 } else {
3636 return (EIO);
3637 }
3638 }
3639 hdrsize = SA_HDR_SIZE(sahdrp);
3640 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
3641 SA_MODE_OFFSET);
3642 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
3643 SA_UID_OFFSET);
3644 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
3645 SA_GID_OFFSET);
3646 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
3647 SA_SIZE_OFFSET);
3648 free(buf);
3649 }
3650
3651 return (0);
3652 }
3653
3654 static int
zfs_dnode_readlink(const spa_t * spa,dnode_phys_t * dn,char * path,size_t psize)3655 zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize)
3656 {
3657 int rc = 0;
3658
3659 if (dn->dn_bonustype == DMU_OT_SA) {
3660 sa_hdr_phys_t *sahdrp = NULL;
3661 size_t size = 0;
3662 void *buf = NULL;
3663 int hdrsize;
3664 char *p;
3665
3666 if (dn->dn_bonuslen != 0) {
3667 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3668 } else {
3669 blkptr_t *bp;
3670
3671 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0)
3672 return (EIO);
3673 bp = DN_SPILL_BLKPTR(dn);
3674
3675 size = BP_GET_LSIZE(bp);
3676 buf = malloc(size);
3677 if (buf == NULL)
3678 rc = ENOMEM;
3679 else
3680 rc = zio_read(spa, bp, buf);
3681 if (rc != 0) {
3682 free(buf);
3683 return (rc);
3684 }
3685 sahdrp = buf;
3686 }
3687 hdrsize = SA_HDR_SIZE(sahdrp);
3688 p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET);
3689 memcpy(path, p, psize);
3690 free(buf);
3691 return (0);
3692 }
3693 /*
3694 * Second test is purely to silence bogus compiler
3695 * warning about accessing past the end of dn_bonus.
3696 */
3697 if (psize + sizeof(znode_phys_t) <= dn->dn_bonuslen &&
3698 sizeof(znode_phys_t) <= sizeof(dn->dn_bonus)) {
3699 memcpy(path, &dn->dn_bonus[sizeof(znode_phys_t)], psize);
3700 } else {
3701 rc = dnode_read(spa, dn, 0, path, psize);
3702 }
3703 return (rc);
3704 }
3705
3706 struct obj_list {
3707 uint64_t objnum;
3708 STAILQ_ENTRY(obj_list) entry;
3709 };
3710
3711 /*
3712 * Lookup a file and return its dnode.
3713 */
3714 static int
zfs_lookup(const struct zfsmount * mount,const char * upath,dnode_phys_t * dnode)3715 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
3716 {
3717 int rc;
3718 uint64_t objnum;
3719 const spa_t *spa;
3720 dnode_phys_t dn;
3721 const char *p, *q;
3722 char element[256];
3723 char path[1024];
3724 int symlinks_followed = 0;
3725 struct stat sb;
3726 struct obj_list *entry, *tentry;
3727 STAILQ_HEAD(, obj_list) on_cache = STAILQ_HEAD_INITIALIZER(on_cache);
3728
3729 spa = mount->spa;
3730 if (mount->objset.os_type != DMU_OST_ZFS) {
3731 printf("ZFS: unexpected object set type %ju\n",
3732 (uintmax_t)mount->objset.os_type);
3733 return (EIO);
3734 }
3735
3736 if ((entry = malloc(sizeof(struct obj_list))) == NULL)
3737 return (ENOMEM);
3738
3739 /*
3740 * Get the root directory dnode.
3741 */
3742 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
3743 if (rc) {
3744 free(entry);
3745 return (rc);
3746 }
3747
3748 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, sizeof(objnum), 1, &objnum);
3749 if (rc) {
3750 free(entry);
3751 return (rc);
3752 }
3753 entry->objnum = objnum;
3754 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3755
3756 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3757 if (rc != 0)
3758 goto done;
3759
3760 p = upath;
3761 while (p && *p) {
3762 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3763 if (rc != 0)
3764 goto done;
3765
3766 while (*p == '/')
3767 p++;
3768 if (*p == '\0')
3769 break;
3770 q = p;
3771 while (*q != '\0' && *q != '/')
3772 q++;
3773
3774 /* skip dot */
3775 if (p + 1 == q && p[0] == '.') {
3776 p++;
3777 continue;
3778 }
3779 /* double dot */
3780 if (p + 2 == q && p[0] == '.' && p[1] == '.') {
3781 p += 2;
3782 if (STAILQ_FIRST(&on_cache) ==
3783 STAILQ_LAST(&on_cache, obj_list, entry)) {
3784 rc = ENOENT;
3785 goto done;
3786 }
3787 entry = STAILQ_FIRST(&on_cache);
3788 STAILQ_REMOVE_HEAD(&on_cache, entry);
3789 free(entry);
3790 objnum = (STAILQ_FIRST(&on_cache))->objnum;
3791 continue;
3792 }
3793 if (q - p + 1 > sizeof(element)) {
3794 rc = ENAMETOOLONG;
3795 goto done;
3796 }
3797 memcpy(element, p, q - p);
3798 element[q - p] = 0;
3799 p = q;
3800
3801 if ((rc = zfs_dnode_stat(spa, &dn, &sb)) != 0)
3802 goto done;
3803 if (!S_ISDIR(sb.st_mode)) {
3804 rc = ENOTDIR;
3805 goto done;
3806 }
3807
3808 rc = zap_lookup(spa, &dn, element, sizeof (objnum), 1, &objnum);
3809 if (rc)
3810 goto done;
3811 objnum = ZFS_DIRENT_OBJ(objnum);
3812
3813 if ((entry = malloc(sizeof(struct obj_list))) == NULL) {
3814 rc = ENOMEM;
3815 goto done;
3816 }
3817 entry->objnum = objnum;
3818 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3819 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3820 if (rc)
3821 goto done;
3822
3823 /*
3824 * Check for symlink.
3825 */
3826 rc = zfs_dnode_stat(spa, &dn, &sb);
3827 if (rc)
3828 goto done;
3829 if (S_ISLNK(sb.st_mode)) {
3830 if (symlinks_followed > 10) {
3831 rc = EMLINK;
3832 goto done;
3833 }
3834 symlinks_followed++;
3835
3836 /*
3837 * Read the link value and copy the tail of our
3838 * current path onto the end.
3839 */
3840 if (sb.st_size + strlen(p) + 1 > sizeof(path)) {
3841 rc = ENAMETOOLONG;
3842 goto done;
3843 }
3844 strcpy(&path[sb.st_size], p);
3845
3846 rc = zfs_dnode_readlink(spa, &dn, path, sb.st_size);
3847 if (rc != 0)
3848 goto done;
3849
3850 /*
3851 * Restart with the new path, starting either at
3852 * the root or at the parent depending whether or
3853 * not the link is relative.
3854 */
3855 p = path;
3856 if (*p == '/') {
3857 while (STAILQ_FIRST(&on_cache) !=
3858 STAILQ_LAST(&on_cache, obj_list, entry)) {
3859 entry = STAILQ_FIRST(&on_cache);
3860 STAILQ_REMOVE_HEAD(&on_cache, entry);
3861 free(entry);
3862 }
3863 } else {
3864 entry = STAILQ_FIRST(&on_cache);
3865 STAILQ_REMOVE_HEAD(&on_cache, entry);
3866 free(entry);
3867 }
3868 objnum = (STAILQ_FIRST(&on_cache))->objnum;
3869 }
3870 }
3871
3872 *dnode = dn;
3873 done:
3874 STAILQ_FOREACH_SAFE(entry, &on_cache, entry, tentry)
3875 free(entry);
3876 return (rc);
3877 }
3878
3879 /*
3880 * Return either a cached copy of the bootenv, or read each of the vdev children
3881 * looking for the bootenv. Cache what's found and return the results. Returns 0
3882 * when benvp is filled in, and some errno when not.
3883 */
3884 static int
zfs_get_bootenv_spa(spa_t * spa,nvlist_t ** benvp)3885 zfs_get_bootenv_spa(spa_t *spa, nvlist_t **benvp)
3886 {
3887 vdev_t *vd;
3888 nvlist_t *benv = NULL;
3889
3890 if (spa->spa_bootenv == NULL) {
3891 STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children,
3892 v_childlink) {
3893 benv = vdev_read_bootenv(vd);
3894
3895 if (benv != NULL)
3896 break;
3897 }
3898 spa->spa_bootenv = benv;
3899 }
3900 benv = spa->spa_bootenv;
3901
3902 if (benv == NULL)
3903 return (ENOENT);
3904
3905 *benvp = benv;
3906 return (0);
3907 }
3908
3909 /*
3910 * Store nvlist to pool label bootenv area. Also updates cached pointer in spa.
3911 */
3912 static int
zfs_set_bootenv_spa(spa_t * spa,nvlist_t * benv)3913 zfs_set_bootenv_spa(spa_t *spa, nvlist_t *benv)
3914 {
3915 vdev_t *vd;
3916
3917 STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children, v_childlink) {
3918 vdev_write_bootenv(vd, benv);
3919 }
3920
3921 spa->spa_bootenv = benv;
3922 return (0);
3923 }
3924
3925 /*
3926 * Get bootonce value by key. The bootonce <key, value> pair is removed from the
3927 * bootenv nvlist and the remaining nvlist is committed back to disk. This process
3928 * the bootonce flag since we've reached the point in the boot that we've 'used'
3929 * the BE. For chained boot scenarios, we may reach this point multiple times (but
3930 * only remove it and return 0 the first time).
3931 */
3932 static int
zfs_get_bootonce_spa(spa_t * spa,const char * key,char * buf,size_t size)3933 zfs_get_bootonce_spa(spa_t *spa, const char *key, char *buf, size_t size)
3934 {
3935 nvlist_t *benv;
3936 char *result = NULL;
3937 int result_size, rv;
3938
3939 if ((rv = zfs_get_bootenv_spa(spa, &benv)) != 0)
3940 return (rv);
3941
3942 if ((rv = nvlist_find(benv, key, DATA_TYPE_STRING, NULL,
3943 &result, &result_size)) == 0) {
3944 if (result_size == 0) {
3945 /* ignore empty string */
3946 rv = ENOENT;
3947 } else if (buf != NULL) {
3948 size = MIN((size_t)result_size + 1, size);
3949 strlcpy(buf, result, size);
3950 }
3951 (void)nvlist_remove(benv, key, DATA_TYPE_STRING);
3952 (void)zfs_set_bootenv_spa(spa, benv);
3953 }
3954
3955 return (rv);
3956 }
3957