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,const nvlist_t * nvlist)1109 vdev_from_nvlist(spa_t *spa, uint64_t top_guid, const nvlist_t *nvlist)
1110 {
1111 vdev_t *top_vdev, *vdev;
1112 nvlist_t **kids = NULL;
1113 int rc, nkids;
1114
1115 /* Get top vdev. */
1116 top_vdev = vdev_find(top_guid);
1117 if (top_vdev == NULL) {
1118 rc = vdev_init(top_guid, nvlist, &top_vdev);
1119 if (rc != 0)
1120 return (rc);
1121 top_vdev->v_spa = spa;
1122 top_vdev->v_top = top_vdev;
1123 vdev_insert(spa->spa_root_vdev, top_vdev);
1124 }
1125
1126 /* Add children if there are any. */
1127 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1128 &nkids, &kids, NULL);
1129 if (rc == 0) {
1130 for (int i = 0; i < nkids; i++) {
1131 uint64_t guid;
1132
1133 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID,
1134 DATA_TYPE_UINT64, NULL, &guid, NULL);
1135 if (rc != 0)
1136 goto done;
1137
1138 rc = vdev_init(guid, kids[i], &vdev);
1139 if (rc != 0)
1140 goto done;
1141
1142 vdev->v_spa = spa;
1143 vdev->v_top = top_vdev;
1144 vdev_insert(top_vdev, vdev);
1145 }
1146 } else {
1147 /*
1148 * When there are no children, nvlist_find() does return
1149 * error, reset it because leaf devices have no children.
1150 */
1151 rc = 0;
1152 }
1153 done:
1154 if (kids != NULL) {
1155 for (int i = 0; i < nkids; i++)
1156 nvlist_destroy(kids[i]);
1157 free(kids);
1158 }
1159
1160 return (rc);
1161 }
1162
1163 static int
vdev_init_from_label(spa_t * spa,const nvlist_t * nvlist)1164 vdev_init_from_label(spa_t *spa, const nvlist_t *nvlist)
1165 {
1166 uint64_t pool_guid, top_guid;
1167 nvlist_t *vdevs;
1168 int rc;
1169
1170 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1171 NULL, &pool_guid, NULL) ||
1172 nvlist_find(nvlist, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64,
1173 NULL, &top_guid, NULL) ||
1174 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1175 NULL, &vdevs, NULL)) {
1176 printf("ZFS: can't find vdev details\n");
1177 return (ENOENT);
1178 }
1179
1180 rc = vdev_from_nvlist(spa, top_guid, vdevs);
1181 nvlist_destroy(vdevs);
1182 return (rc);
1183 }
1184
1185 static void
vdev_set_state(vdev_t * vdev)1186 vdev_set_state(vdev_t *vdev)
1187 {
1188 vdev_t *kid;
1189 int good_kids;
1190 int bad_kids;
1191
1192 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1193 vdev_set_state(kid);
1194 }
1195
1196 /*
1197 * A mirror or raidz is healthy if all its kids are healthy. A
1198 * mirror is degraded if any of its kids is healthy; a raidz
1199 * is degraded if at most nparity kids are offline.
1200 */
1201 if (STAILQ_FIRST(&vdev->v_children)) {
1202 good_kids = 0;
1203 bad_kids = 0;
1204 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1205 if (kid->v_state == VDEV_STATE_HEALTHY)
1206 good_kids++;
1207 else
1208 bad_kids++;
1209 }
1210 if (bad_kids == 0) {
1211 vdev->v_state = VDEV_STATE_HEALTHY;
1212 } else {
1213 if (vdev->v_read == vdev_mirror_read) {
1214 if (good_kids) {
1215 vdev->v_state = VDEV_STATE_DEGRADED;
1216 } else {
1217 vdev->v_state = VDEV_STATE_OFFLINE;
1218 }
1219 } else if (vdev->v_read == vdev_raidz_read) {
1220 if (bad_kids > vdev->v_nparity) {
1221 vdev->v_state = VDEV_STATE_OFFLINE;
1222 } else {
1223 vdev->v_state = VDEV_STATE_DEGRADED;
1224 }
1225 }
1226 }
1227 }
1228 }
1229
1230 static int
vdev_update_from_nvlist(uint64_t top_guid,const nvlist_t * nvlist)1231 vdev_update_from_nvlist(uint64_t top_guid, const nvlist_t *nvlist)
1232 {
1233 vdev_t *vdev;
1234 nvlist_t **kids = NULL;
1235 int rc, nkids;
1236
1237 /* Update top vdev. */
1238 vdev = vdev_find(top_guid);
1239 if (vdev != NULL)
1240 vdev_set_initial_state(vdev, nvlist);
1241
1242 /* Update children if there are any. */
1243 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1244 &nkids, &kids, NULL);
1245 if (rc == 0) {
1246 for (int i = 0; i < nkids; i++) {
1247 uint64_t guid;
1248
1249 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID,
1250 DATA_TYPE_UINT64, NULL, &guid, NULL);
1251 if (rc != 0)
1252 break;
1253
1254 vdev = vdev_find(guid);
1255 if (vdev != NULL)
1256 vdev_set_initial_state(vdev, kids[i]);
1257 }
1258 } else {
1259 rc = 0;
1260 }
1261 if (kids != NULL) {
1262 for (int i = 0; i < nkids; i++)
1263 nvlist_destroy(kids[i]);
1264 free(kids);
1265 }
1266
1267 return (rc);
1268 }
1269
1270 static int
vdev_init_from_nvlist(spa_t * spa,const nvlist_t * nvlist)1271 vdev_init_from_nvlist(spa_t *spa, const nvlist_t *nvlist)
1272 {
1273 uint64_t pool_guid, vdev_children;
1274 nvlist_t *vdevs = NULL, **kids = NULL;
1275 int rc, nkids;
1276
1277 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1278 NULL, &pool_guid, NULL) ||
1279 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_CHILDREN, DATA_TYPE_UINT64,
1280 NULL, &vdev_children, NULL) ||
1281 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1282 NULL, &vdevs, NULL)) {
1283 printf("ZFS: can't find vdev details\n");
1284 return (ENOENT);
1285 }
1286
1287 /* Wrong guid?! */
1288 if (spa->spa_guid != pool_guid) {
1289 nvlist_destroy(vdevs);
1290 return (EINVAL);
1291 }
1292
1293 spa->spa_root_vdev->v_nchildren = vdev_children;
1294
1295 rc = nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1296 &nkids, &kids, NULL);
1297 nvlist_destroy(vdevs);
1298
1299 /*
1300 * MOS config has at least one child for root vdev.
1301 */
1302 if (rc != 0)
1303 return (rc);
1304
1305 for (int i = 0; i < nkids; i++) {
1306 uint64_t guid;
1307 vdev_t *vdev;
1308
1309 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
1310 NULL, &guid, NULL);
1311 if (rc != 0)
1312 break;
1313 vdev = vdev_find(guid);
1314 /*
1315 * Top level vdev is missing, create it.
1316 */
1317 if (vdev == NULL)
1318 rc = vdev_from_nvlist(spa, guid, kids[i]);
1319 else
1320 rc = vdev_update_from_nvlist(guid, kids[i]);
1321 if (rc != 0)
1322 break;
1323 }
1324 if (kids != NULL) {
1325 for (int i = 0; i < nkids; i++)
1326 nvlist_destroy(kids[i]);
1327 free(kids);
1328 }
1329
1330 /*
1331 * Re-evaluate top-level vdev state.
1332 */
1333 vdev_set_state(spa->spa_root_vdev);
1334
1335 return (rc);
1336 }
1337
1338 static spa_t *
spa_find_by_guid(uint64_t guid)1339 spa_find_by_guid(uint64_t guid)
1340 {
1341 spa_t *spa;
1342
1343 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1344 if (spa->spa_guid == guid)
1345 return (spa);
1346
1347 return (NULL);
1348 }
1349
1350 static spa_t *
spa_find_by_name(const char * name)1351 spa_find_by_name(const char *name)
1352 {
1353 spa_t *spa;
1354
1355 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1356 if (strcmp(spa->spa_name, name) == 0)
1357 return (spa);
1358
1359 return (NULL);
1360 }
1361
1362 static spa_t *
spa_create(uint64_t guid,const char * name)1363 spa_create(uint64_t guid, const char *name)
1364 {
1365 spa_t *spa;
1366
1367 if ((spa = calloc(1, sizeof(spa_t))) == NULL)
1368 return (NULL);
1369 if ((spa->spa_name = strdup(name)) == NULL) {
1370 free(spa);
1371 return (NULL);
1372 }
1373 spa->spa_uberblock = &spa->spa_uberblock_master;
1374 spa->spa_mos = &spa->spa_mos_master;
1375 spa->spa_guid = guid;
1376 spa->spa_root_vdev = vdev_create(guid, NULL);
1377 if (spa->spa_root_vdev == NULL) {
1378 free(spa->spa_name);
1379 free(spa);
1380 return (NULL);
1381 }
1382 spa->spa_root_vdev->v_name = spa->spa_name;
1383 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
1384
1385 return (spa);
1386 }
1387
1388 static const char *
state_name(vdev_state_t state)1389 state_name(vdev_state_t state)
1390 {
1391 static const char *names[] = {
1392 "UNKNOWN",
1393 "CLOSED",
1394 "OFFLINE",
1395 "REMOVED",
1396 "CANT_OPEN",
1397 "FAULTED",
1398 "DEGRADED",
1399 "ONLINE"
1400 };
1401 return (names[state]);
1402 }
1403
1404 #ifdef BOOT2
1405
1406 #define pager_printf printf
1407
1408 #else
1409
1410 static int
pager_printf(const char * fmt,...)1411 pager_printf(const char *fmt, ...)
1412 {
1413 char line[80];
1414 va_list args;
1415
1416 va_start(args, fmt);
1417 vsnprintf(line, sizeof(line), fmt, args);
1418 va_end(args);
1419 return (pager_output(line));
1420 }
1421
1422 #endif
1423
1424 #define STATUS_FORMAT " %s %s\n"
1425
1426 static int
print_state(int indent,const char * name,vdev_state_t state)1427 print_state(int indent, const char *name, vdev_state_t state)
1428 {
1429 int i;
1430 char buf[512];
1431
1432 buf[0] = 0;
1433 for (i = 0; i < indent; i++)
1434 strcat(buf, " ");
1435 strcat(buf, name);
1436 return (pager_printf(STATUS_FORMAT, buf, state_name(state)));
1437 }
1438
1439 static int
vdev_status(vdev_t * vdev,int indent)1440 vdev_status(vdev_t *vdev, int indent)
1441 {
1442 vdev_t *kid;
1443 int ret;
1444
1445 if (vdev->v_islog) {
1446 (void) pager_output(" logs\n");
1447 indent++;
1448 }
1449
1450 ret = print_state(indent, vdev->v_name, vdev->v_state);
1451 if (ret != 0)
1452 return (ret);
1453
1454 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1455 ret = vdev_status(kid, indent + 1);
1456 if (ret != 0)
1457 return (ret);
1458 }
1459 return (ret);
1460 }
1461
1462 static int
spa_status(spa_t * spa)1463 spa_status(spa_t *spa)
1464 {
1465 static char bootfs[ZFS_MAXNAMELEN];
1466 uint64_t rootid;
1467 vdev_list_t *vlist;
1468 vdev_t *vdev;
1469 int good_kids, bad_kids, degraded_kids, ret;
1470 vdev_state_t state;
1471
1472 ret = pager_printf(" pool: %s\n", spa->spa_name);
1473 if (ret != 0)
1474 return (ret);
1475
1476 if (zfs_get_root(spa, &rootid) == 0 &&
1477 zfs_rlookup(spa, rootid, bootfs) == 0) {
1478 if (bootfs[0] == '\0')
1479 ret = pager_printf("bootfs: %s\n", spa->spa_name);
1480 else
1481 ret = pager_printf("bootfs: %s/%s\n", spa->spa_name,
1482 bootfs);
1483 if (ret != 0)
1484 return (ret);
1485 }
1486 ret = pager_printf("config:\n\n");
1487 if (ret != 0)
1488 return (ret);
1489 ret = pager_printf(STATUS_FORMAT, "NAME", "STATE");
1490 if (ret != 0)
1491 return (ret);
1492
1493 good_kids = 0;
1494 degraded_kids = 0;
1495 bad_kids = 0;
1496 vlist = &spa->spa_root_vdev->v_children;
1497 STAILQ_FOREACH(vdev, vlist, v_childlink) {
1498 if (vdev->v_state == VDEV_STATE_HEALTHY)
1499 good_kids++;
1500 else if (vdev->v_state == VDEV_STATE_DEGRADED)
1501 degraded_kids++;
1502 else
1503 bad_kids++;
1504 }
1505
1506 state = VDEV_STATE_CLOSED;
1507 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
1508 state = VDEV_STATE_HEALTHY;
1509 else if ((good_kids + degraded_kids) > 0)
1510 state = VDEV_STATE_DEGRADED;
1511
1512 ret = print_state(0, spa->spa_name, state);
1513 if (ret != 0)
1514 return (ret);
1515
1516 STAILQ_FOREACH(vdev, vlist, v_childlink) {
1517 ret = vdev_status(vdev, 1);
1518 if (ret != 0)
1519 return (ret);
1520 }
1521 return (ret);
1522 }
1523
1524 static int
spa_all_status(void)1525 spa_all_status(void)
1526 {
1527 spa_t *spa;
1528 int first = 1, ret = 0;
1529
1530 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
1531 if (!first) {
1532 ret = pager_printf("\n");
1533 if (ret != 0)
1534 return (ret);
1535 }
1536 first = 0;
1537 ret = spa_status(spa);
1538 if (ret != 0)
1539 return (ret);
1540 }
1541 return (ret);
1542 }
1543
1544 static uint64_t
vdev_label_offset(uint64_t psize,int l,uint64_t offset)1545 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
1546 {
1547 uint64_t label_offset;
1548
1549 if (l < VDEV_LABELS / 2)
1550 label_offset = 0;
1551 else
1552 label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t);
1553
1554 return (offset + l * sizeof (vdev_label_t) + label_offset);
1555 }
1556
1557 static int
vdev_uberblock_compare(const uberblock_t * ub1,const uberblock_t * ub2)1558 vdev_uberblock_compare(const uberblock_t *ub1, const uberblock_t *ub2)
1559 {
1560 unsigned int seq1 = 0;
1561 unsigned int seq2 = 0;
1562 int cmp = AVL_CMP(ub1->ub_txg, ub2->ub_txg);
1563
1564 if (cmp != 0)
1565 return (cmp);
1566
1567 cmp = AVL_CMP(ub1->ub_timestamp, ub2->ub_timestamp);
1568 if (cmp != 0)
1569 return (cmp);
1570
1571 if (MMP_VALID(ub1) && MMP_SEQ_VALID(ub1))
1572 seq1 = MMP_SEQ(ub1);
1573
1574 if (MMP_VALID(ub2) && MMP_SEQ_VALID(ub2))
1575 seq2 = MMP_SEQ(ub2);
1576
1577 return (AVL_CMP(seq1, seq2));
1578 }
1579
1580 static int
uberblock_verify(uberblock_t * ub)1581 uberblock_verify(uberblock_t *ub)
1582 {
1583 if (ub->ub_magic == BSWAP_64((uint64_t)UBERBLOCK_MAGIC)) {
1584 byteswap_uint64_array(ub, sizeof (uberblock_t));
1585 }
1586
1587 if (ub->ub_magic != UBERBLOCK_MAGIC ||
1588 !SPA_VERSION_IS_SUPPORTED(ub->ub_version))
1589 return (EINVAL);
1590
1591 return (0);
1592 }
1593
1594 static int
vdev_label_read(vdev_t * vd,int l,void * buf,uint64_t offset,size_t size)1595 vdev_label_read(vdev_t *vd, int l, void *buf, uint64_t offset,
1596 size_t size)
1597 {
1598 blkptr_t bp;
1599 off_t off;
1600
1601 off = vdev_label_offset(vd->v_psize, l, offset);
1602
1603 BP_ZERO(&bp);
1604 BP_SET_LSIZE(&bp, size);
1605 BP_SET_PSIZE(&bp, size);
1606 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1607 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1608 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
1609 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1610
1611 return (vdev_read_phys(vd, &bp, buf, off, size));
1612 }
1613
1614 /*
1615 * We do need to be sure we write to correct location.
1616 * Our vdev label does consist of 4 fields:
1617 * pad1 (8k), reserved.
1618 * bootenv (8k), checksummed, previously reserved, may contian garbage.
1619 * vdev_phys (112k), checksummed
1620 * uberblock ring (128k), checksummed.
1621 *
1622 * Since bootenv area may contain garbage, we can not reliably read it, as
1623 * we can get checksum errors.
1624 * Next best thing is vdev_phys - it is just after bootenv. It still may
1625 * be corrupted, but in such case we will miss this one write.
1626 */
1627 static int
vdev_label_write_validate(vdev_t * vd,int l,uint64_t offset)1628 vdev_label_write_validate(vdev_t *vd, int l, uint64_t offset)
1629 {
1630 uint64_t off, o_phys;
1631 void *buf;
1632 size_t size = VDEV_PHYS_SIZE;
1633 int rc;
1634
1635 o_phys = offsetof(vdev_label_t, vl_vdev_phys);
1636 off = vdev_label_offset(vd->v_psize, l, o_phys);
1637
1638 /* off should be 8K from bootenv */
1639 if (vdev_label_offset(vd->v_psize, l, offset) + VDEV_PAD_SIZE != off)
1640 return (EINVAL);
1641
1642 buf = malloc(size);
1643 if (buf == NULL)
1644 return (ENOMEM);
1645
1646 /* Read vdev_phys */
1647 rc = vdev_label_read(vd, l, buf, o_phys, size);
1648 free(buf);
1649 return (rc);
1650 }
1651
1652 static int
vdev_label_write(vdev_t * vd,int l,vdev_boot_envblock_t * be,uint64_t offset)1653 vdev_label_write(vdev_t *vd, int l, vdev_boot_envblock_t *be, uint64_t offset)
1654 {
1655 zio_checksum_info_t *ci;
1656 zio_cksum_t cksum;
1657 off_t off;
1658 size_t size = VDEV_PAD_SIZE;
1659 int rc;
1660
1661 if (vd->v_phys_write == NULL)
1662 return (ENOTSUP);
1663
1664 off = vdev_label_offset(vd->v_psize, l, offset);
1665
1666 rc = vdev_label_write_validate(vd, l, offset);
1667 if (rc != 0) {
1668 return (rc);
1669 }
1670
1671 ci = &zio_checksum_table[ZIO_CHECKSUM_LABEL];
1672 be->vbe_zbt.zec_magic = ZEC_MAGIC;
1673 zio_checksum_label_verifier(&be->vbe_zbt.zec_cksum, off);
1674 ci->ci_func[0](be, size, NULL, &cksum);
1675 be->vbe_zbt.zec_cksum = cksum;
1676
1677 return (vdev_write_phys(vd, be, off, size));
1678 }
1679
1680 static int
vdev_write_bootenv_impl(vdev_t * vdev,vdev_boot_envblock_t * be)1681 vdev_write_bootenv_impl(vdev_t *vdev, vdev_boot_envblock_t *be)
1682 {
1683 vdev_t *kid;
1684 int rv = 0, err;
1685
1686 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1687 if (kid->v_state != VDEV_STATE_HEALTHY)
1688 continue;
1689 err = vdev_write_bootenv_impl(kid, be);
1690 if (err != 0)
1691 rv = err;
1692 }
1693
1694 /*
1695 * Non-leaf vdevs do not have v_phys_write.
1696 */
1697 if (vdev->v_phys_write == NULL)
1698 return (rv);
1699
1700 for (int l = 0; l < VDEV_LABELS; l++) {
1701 err = vdev_label_write(vdev, l, be,
1702 offsetof(vdev_label_t, vl_be));
1703 if (err != 0) {
1704 printf("failed to write bootenv to %s label %d: %d\n",
1705 vdev->v_name ? vdev->v_name : "unknown", l, err);
1706 rv = err;
1707 }
1708 }
1709 return (rv);
1710 }
1711
1712 int
vdev_write_bootenv(vdev_t * vdev,nvlist_t * nvl)1713 vdev_write_bootenv(vdev_t *vdev, nvlist_t *nvl)
1714 {
1715 vdev_boot_envblock_t *be;
1716 nvlist_t nv, *nvp;
1717 uint64_t version;
1718 int rv;
1719
1720 if (nvl->nv_size > sizeof(be->vbe_bootenv))
1721 return (E2BIG);
1722
1723 version = VB_RAW;
1724 nvp = vdev_read_bootenv(vdev);
1725 if (nvp != NULL) {
1726 nvlist_find(nvp, BOOTENV_VERSION, DATA_TYPE_UINT64, NULL,
1727 &version, NULL);
1728 nvlist_destroy(nvp);
1729 }
1730
1731 be = calloc(1, sizeof(*be));
1732 if (be == NULL)
1733 return (ENOMEM);
1734
1735 be->vbe_version = version;
1736 switch (version) {
1737 case VB_RAW:
1738 /*
1739 * If there is no envmap, we will just wipe bootenv.
1740 */
1741 nvlist_find(nvl, GRUB_ENVMAP, DATA_TYPE_STRING, NULL,
1742 be->vbe_bootenv, NULL);
1743 rv = 0;
1744 break;
1745
1746 case VB_NVLIST:
1747 nv.nv_header = nvl->nv_header;
1748 nv.nv_asize = nvl->nv_asize;
1749 nv.nv_size = nvl->nv_size;
1750
1751 bcopy(&nv.nv_header, be->vbe_bootenv, sizeof(nv.nv_header));
1752 nv.nv_data = be->vbe_bootenv + sizeof(nvs_header_t);
1753 bcopy(nvl->nv_data, nv.nv_data, nv.nv_size);
1754 rv = nvlist_export(&nv);
1755 break;
1756
1757 default:
1758 rv = EINVAL;
1759 break;
1760 }
1761
1762 if (rv == 0) {
1763 be->vbe_version = htobe64(be->vbe_version);
1764 rv = vdev_write_bootenv_impl(vdev, be);
1765 }
1766 free(be);
1767 return (rv);
1768 }
1769
1770 /*
1771 * Read the bootenv area from pool label, return the nvlist from it.
1772 * We return from first successful read.
1773 */
1774 nvlist_t *
vdev_read_bootenv(vdev_t * vdev)1775 vdev_read_bootenv(vdev_t *vdev)
1776 {
1777 vdev_t *kid;
1778 nvlist_t *benv;
1779 vdev_boot_envblock_t *be;
1780 char *command;
1781 bool ok;
1782 int rv;
1783
1784 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1785 if (kid->v_state != VDEV_STATE_HEALTHY)
1786 continue;
1787
1788 benv = vdev_read_bootenv(kid);
1789 if (benv != NULL)
1790 return (benv);
1791 }
1792
1793 be = malloc(sizeof (*be));
1794 if (be == NULL)
1795 return (NULL);
1796
1797 rv = 0;
1798 for (int l = 0; l < VDEV_LABELS; l++) {
1799 rv = vdev_label_read(vdev, l, be,
1800 offsetof(vdev_label_t, vl_be),
1801 sizeof (*be));
1802 if (rv == 0)
1803 break;
1804 }
1805 if (rv != 0) {
1806 free(be);
1807 return (NULL);
1808 }
1809
1810 be->vbe_version = be64toh(be->vbe_version);
1811 switch (be->vbe_version) {
1812 case VB_RAW:
1813 /*
1814 * we have textual data in vbe_bootenv, create nvlist
1815 * with key "envmap".
1816 */
1817 benv = nvlist_create(NV_UNIQUE_NAME);
1818 if (benv != NULL) {
1819 if (*be->vbe_bootenv == '\0') {
1820 nvlist_add_uint64(benv, BOOTENV_VERSION,
1821 VB_NVLIST);
1822 break;
1823 }
1824 nvlist_add_uint64(benv, BOOTENV_VERSION, VB_RAW);
1825 be->vbe_bootenv[sizeof (be->vbe_bootenv) - 1] = '\0';
1826 nvlist_add_string(benv, GRUB_ENVMAP, be->vbe_bootenv);
1827 }
1828 break;
1829
1830 case VB_NVLIST:
1831 benv = nvlist_import(be->vbe_bootenv, sizeof(be->vbe_bootenv));
1832 break;
1833
1834 default:
1835 command = (char *)be;
1836 ok = false;
1837
1838 /* Check for legacy zfsbootcfg command string */
1839 for (int i = 0; command[i] != '\0'; i++) {
1840 if (iscntrl(command[i])) {
1841 ok = false;
1842 break;
1843 } else {
1844 ok = true;
1845 }
1846 }
1847 benv = nvlist_create(NV_UNIQUE_NAME);
1848 if (benv != NULL) {
1849 if (ok)
1850 nvlist_add_string(benv, FREEBSD_BOOTONCE,
1851 command);
1852 else
1853 nvlist_add_uint64(benv, BOOTENV_VERSION,
1854 VB_NVLIST);
1855 }
1856 break;
1857 }
1858 free(be);
1859 return (benv);
1860 }
1861
1862 static uint64_t
vdev_get_label_asize(nvlist_t * nvl)1863 vdev_get_label_asize(nvlist_t *nvl)
1864 {
1865 nvlist_t *vdevs;
1866 uint64_t asize;
1867 const char *type;
1868 int len;
1869
1870 asize = 0;
1871 /* Get vdev tree */
1872 if (nvlist_find(nvl, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1873 NULL, &vdevs, NULL) != 0)
1874 return (asize);
1875
1876 /*
1877 * Get vdev type. We will calculate asize for raidz, mirror and disk.
1878 * For raidz, the asize is raw size of all children.
1879 */
1880 if (nvlist_find(vdevs, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
1881 NULL, &type, &len) != 0)
1882 goto done;
1883
1884 if (memcmp(type, VDEV_TYPE_MIRROR, len) != 0 &&
1885 memcmp(type, VDEV_TYPE_DISK, len) != 0 &&
1886 memcmp(type, VDEV_TYPE_RAIDZ, len) != 0)
1887 goto done;
1888
1889 if (nvlist_find(vdevs, ZPOOL_CONFIG_ASIZE, DATA_TYPE_UINT64,
1890 NULL, &asize, NULL) != 0)
1891 goto done;
1892
1893 if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0) {
1894 nvlist_t **kids;
1895 int nkids;
1896
1897 if (nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN,
1898 DATA_TYPE_NVLIST_ARRAY, &nkids, &kids, NULL) != 0) {
1899 asize = 0;
1900 goto done;
1901 }
1902
1903 asize /= nkids;
1904 for (int i = 0; i < nkids; i++)
1905 nvlist_destroy(kids[i]);
1906 free(kids);
1907 }
1908
1909 asize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
1910 done:
1911 nvlist_destroy(vdevs);
1912 return (asize);
1913 }
1914
1915 static nvlist_t *
vdev_label_read_config(vdev_t * vd,uint64_t txg)1916 vdev_label_read_config(vdev_t *vd, uint64_t txg)
1917 {
1918 vdev_phys_t *label;
1919 uint64_t best_txg = 0;
1920 uint64_t label_txg = 0;
1921 uint64_t asize;
1922 nvlist_t *nvl = NULL, *tmp;
1923 int error;
1924
1925 label = malloc(sizeof (vdev_phys_t));
1926 if (label == NULL)
1927 return (NULL);
1928
1929 for (int l = 0; l < VDEV_LABELS; l++) {
1930 if (vdev_label_read(vd, l, label,
1931 offsetof(vdev_label_t, vl_vdev_phys),
1932 sizeof (vdev_phys_t)))
1933 continue;
1934
1935 tmp = nvlist_import(label->vp_nvlist,
1936 sizeof(label->vp_nvlist));
1937 if (tmp == NULL)
1938 continue;
1939
1940 error = nvlist_find(tmp, ZPOOL_CONFIG_POOL_TXG,
1941 DATA_TYPE_UINT64, NULL, &label_txg, NULL);
1942 if (error != 0 || label_txg == 0) {
1943 nvlist_destroy(nvl);
1944 nvl = tmp;
1945 goto done;
1946 }
1947
1948 if (label_txg <= txg && label_txg > best_txg) {
1949 best_txg = label_txg;
1950 nvlist_destroy(nvl);
1951 nvl = tmp;
1952 tmp = NULL;
1953
1954 /*
1955 * Use asize from pool config. We need this
1956 * because we can get bad value from BIOS.
1957 */
1958 asize = vdev_get_label_asize(nvl);
1959 if (asize != 0) {
1960 vd->v_psize = asize;
1961 }
1962 }
1963 nvlist_destroy(tmp);
1964 }
1965
1966 if (best_txg == 0) {
1967 nvlist_destroy(nvl);
1968 nvl = NULL;
1969 }
1970 done:
1971 free(label);
1972 return (nvl);
1973 }
1974
1975 static void
vdev_uberblock_load(vdev_t * vd,uberblock_t * ub)1976 vdev_uberblock_load(vdev_t *vd, uberblock_t *ub)
1977 {
1978 uberblock_t *buf;
1979
1980 buf = malloc(VDEV_UBERBLOCK_SIZE(vd));
1981 if (buf == NULL)
1982 return;
1983
1984 for (int l = 0; l < VDEV_LABELS; l++) {
1985 for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
1986 if (vdev_label_read(vd, l, buf,
1987 VDEV_UBERBLOCK_OFFSET(vd, n),
1988 VDEV_UBERBLOCK_SIZE(vd)))
1989 continue;
1990 if (uberblock_verify(buf) != 0)
1991 continue;
1992
1993 if (vdev_uberblock_compare(buf, ub) > 0)
1994 *ub = *buf;
1995 }
1996 }
1997 free(buf);
1998 }
1999
2000 static int
vdev_probe(vdev_phys_read_t * _read,vdev_phys_write_t * _write,void * priv,spa_t ** spap)2001 vdev_probe(vdev_phys_read_t *_read, vdev_phys_write_t *_write, void *priv,
2002 spa_t **spap)
2003 {
2004 vdev_t vtmp;
2005 spa_t *spa;
2006 vdev_t *vdev;
2007 nvlist_t *nvl;
2008 uint64_t val;
2009 uint64_t guid;
2010 uint64_t pool_txg, pool_guid;
2011 const char *pool_name;
2012 int rc, namelen;
2013
2014 /*
2015 * Load the vdev label and figure out which
2016 * uberblock is most current.
2017 */
2018 memset(&vtmp, 0, sizeof(vtmp));
2019 vtmp.v_phys_read = _read;
2020 vtmp.v_phys_write = _write;
2021 vtmp.v_priv = priv;
2022 vtmp.v_psize = P2ALIGN(ldi_get_size(priv),
2023 (uint64_t)sizeof (vdev_label_t));
2024
2025 /* Test for minimum device size. */
2026 if (vtmp.v_psize < SPA_MINDEVSIZE)
2027 return (EIO);
2028
2029 nvl = vdev_label_read_config(&vtmp, UINT64_MAX);
2030 if (nvl == NULL)
2031 return (EIO);
2032
2033 if (nvlist_find(nvl, ZPOOL_CONFIG_VERSION, DATA_TYPE_UINT64,
2034 NULL, &val, NULL) != 0) {
2035 nvlist_destroy(nvl);
2036 return (EIO);
2037 }
2038
2039 if (!SPA_VERSION_IS_SUPPORTED(val)) {
2040 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
2041 (unsigned)val, (unsigned)SPA_VERSION);
2042 nvlist_destroy(nvl);
2043 return (EIO);
2044 }
2045
2046 /* Check ZFS features for read */
2047 rc = nvlist_check_features_for_read(nvl);
2048 if (rc != 0) {
2049 nvlist_destroy(nvl);
2050 return (EIO);
2051 }
2052
2053 if (nvlist_find(nvl, ZPOOL_CONFIG_POOL_STATE, DATA_TYPE_UINT64,
2054 NULL, &val, NULL) != 0) {
2055 nvlist_destroy(nvl);
2056 return (EIO);
2057 }
2058
2059 if (val == POOL_STATE_DESTROYED) {
2060 /* We don't boot only from destroyed pools. */
2061 nvlist_destroy(nvl);
2062 return (EIO);
2063 }
2064
2065 if (nvlist_find(nvl, ZPOOL_CONFIG_POOL_TXG, DATA_TYPE_UINT64,
2066 NULL, &pool_txg, NULL) != 0 ||
2067 nvlist_find(nvl, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
2068 NULL, &pool_guid, NULL) != 0 ||
2069 nvlist_find(nvl, ZPOOL_CONFIG_POOL_NAME, DATA_TYPE_STRING,
2070 NULL, &pool_name, &namelen) != 0 ||
2071 nvlist_find(nvl, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
2072 NULL, &guid, NULL) != 0) {
2073 /*
2074 * Cache and spare devices end up here - just ignore
2075 * them.
2076 */
2077 nvlist_destroy(nvl);
2078 return (EIO);
2079 }
2080
2081 /*
2082 * Create the pool if this is the first time we've seen it.
2083 */
2084 spa = spa_find_by_guid(pool_guid);
2085 if (spa == NULL) {
2086 char *name;
2087
2088 name = malloc(namelen + 1);
2089 if (name == NULL) {
2090 nvlist_destroy(nvl);
2091 return (ENOMEM);
2092 }
2093 bcopy(pool_name, name, namelen);
2094 name[namelen] = '\0';
2095 spa = spa_create(pool_guid, name);
2096 free(name);
2097 if (spa == NULL) {
2098 nvlist_destroy(nvl);
2099 return (ENOMEM);
2100 }
2101 }
2102 if (pool_txg > spa->spa_txg)
2103 spa->spa_txg = pool_txg;
2104
2105 /*
2106 * Get the vdev tree and create our in-core copy of it.
2107 * If we already have a vdev with this guid, this must
2108 * be some kind of alias (overlapping slices, dangerously dedicated
2109 * disks etc).
2110 */
2111 vdev = vdev_find(guid);
2112 /* Has this vdev already been inited? */
2113 if (vdev && vdev->v_phys_read) {
2114 nvlist_destroy(nvl);
2115 return (EIO);
2116 }
2117
2118 rc = vdev_init_from_label(spa, nvl);
2119 nvlist_destroy(nvl);
2120 if (rc != 0)
2121 return (rc);
2122
2123 /*
2124 * We should already have created an incomplete vdev for this
2125 * vdev. Find it and initialise it with our read proc.
2126 */
2127 vdev = vdev_find(guid);
2128 if (vdev != NULL) {
2129 vdev->v_phys_read = _read;
2130 vdev->v_phys_write = _write;
2131 vdev->v_priv = priv;
2132 vdev->v_psize = vtmp.v_psize;
2133 /*
2134 * If no other state is set, mark vdev healthy.
2135 */
2136 if (vdev->v_state == VDEV_STATE_UNKNOWN)
2137 vdev->v_state = VDEV_STATE_HEALTHY;
2138 } else {
2139 printf("ZFS: inconsistent nvlist contents\n");
2140 return (EIO);
2141 }
2142
2143 if (vdev->v_islog)
2144 spa->spa_with_log = vdev->v_islog;
2145
2146 /*
2147 * Re-evaluate top-level vdev state.
2148 */
2149 vdev_set_state(vdev->v_top);
2150
2151 /*
2152 * Ok, we are happy with the pool so far. Lets find
2153 * the best uberblock and then we can actually access
2154 * the contents of the pool.
2155 */
2156 vdev_uberblock_load(vdev, spa->spa_uberblock);
2157
2158 if (spap != NULL)
2159 *spap = spa;
2160 return (0);
2161 }
2162
2163 static int
ilog2(int n)2164 ilog2(int n)
2165 {
2166 int v;
2167
2168 for (v = 0; v < 32; v++)
2169 if (n == (1 << v))
2170 return (v);
2171 return (-1);
2172 }
2173
2174 static int
zio_read_gang(const spa_t * spa,const blkptr_t * bp,void * buf)2175 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
2176 {
2177 blkptr_t gbh_bp;
2178 zio_gbh_phys_t zio_gb;
2179 char *pbuf;
2180 int i;
2181
2182 /* Artificial BP for gang block header. */
2183 gbh_bp = *bp;
2184 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
2185 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
2186 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
2187 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
2188 for (i = 0; i < SPA_DVAS_PER_BP; i++)
2189 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
2190
2191 /* Read gang header block using the artificial BP. */
2192 if (zio_read(spa, &gbh_bp, &zio_gb))
2193 return (EIO);
2194
2195 pbuf = buf;
2196 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
2197 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
2198
2199 if (BP_IS_HOLE(gbp))
2200 continue;
2201 if (zio_read(spa, gbp, pbuf))
2202 return (EIO);
2203 pbuf += BP_GET_PSIZE(gbp);
2204 }
2205
2206 if (zio_checksum_verify(spa, bp, buf))
2207 return (EIO);
2208 return (0);
2209 }
2210
2211 static int
zio_read(const spa_t * spa,const blkptr_t * bp,void * buf)2212 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
2213 {
2214 int cpfunc = BP_GET_COMPRESS(bp);
2215 uint64_t align, size;
2216 void *pbuf;
2217 int i, error;
2218
2219 /*
2220 * Process data embedded in block pointer
2221 */
2222 if (BP_IS_EMBEDDED(bp)) {
2223 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
2224
2225 size = BPE_GET_PSIZE(bp);
2226 ASSERT(size <= BPE_PAYLOAD_SIZE);
2227
2228 if (cpfunc != ZIO_COMPRESS_OFF)
2229 pbuf = malloc(size);
2230 else
2231 pbuf = buf;
2232
2233 if (pbuf == NULL)
2234 return (ENOMEM);
2235
2236 decode_embedded_bp_compressed(bp, pbuf);
2237 error = 0;
2238
2239 if (cpfunc != ZIO_COMPRESS_OFF) {
2240 error = zio_decompress_data(cpfunc, pbuf,
2241 size, buf, BP_GET_LSIZE(bp));
2242 free(pbuf);
2243 }
2244 if (error != 0)
2245 printf("ZFS: i/o error - unable to decompress "
2246 "block pointer data, error %d\n", error);
2247 return (error);
2248 }
2249
2250 error = EIO;
2251
2252 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
2253 const dva_t *dva = &bp->blk_dva[i];
2254 vdev_t *vdev;
2255 vdev_list_t *vlist;
2256 uint64_t vdevid;
2257 off_t offset;
2258
2259 if (!dva->dva_word[0] && !dva->dva_word[1])
2260 continue;
2261
2262 vdevid = DVA_GET_VDEV(dva);
2263 offset = DVA_GET_OFFSET(dva);
2264 vlist = &spa->spa_root_vdev->v_children;
2265 STAILQ_FOREACH(vdev, vlist, v_childlink) {
2266 if (vdev->v_id == vdevid)
2267 break;
2268 }
2269 if (!vdev || !vdev->v_read)
2270 continue;
2271
2272 size = BP_GET_PSIZE(bp);
2273 if (vdev->v_read == vdev_raidz_read) {
2274 align = 1ULL << vdev->v_ashift;
2275 if (P2PHASE(size, align) != 0)
2276 size = P2ROUNDUP(size, align);
2277 }
2278 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
2279 pbuf = malloc(size);
2280 else
2281 pbuf = buf;
2282
2283 if (pbuf == NULL) {
2284 error = ENOMEM;
2285 break;
2286 }
2287
2288 if (DVA_GET_GANG(dva))
2289 error = zio_read_gang(spa, bp, pbuf);
2290 else
2291 error = vdev->v_read(vdev, bp, pbuf, offset, size);
2292 if (error == 0) {
2293 if (cpfunc != ZIO_COMPRESS_OFF)
2294 error = zio_decompress_data(cpfunc, pbuf,
2295 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
2296 else if (size != BP_GET_PSIZE(bp))
2297 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
2298 } else {
2299 printf("zio_read error: %d\n", error);
2300 }
2301 if (buf != pbuf)
2302 free(pbuf);
2303 if (error == 0)
2304 break;
2305 }
2306 if (error != 0)
2307 printf("ZFS: i/o error - all block copies unavailable\n");
2308
2309 return (error);
2310 }
2311
2312 static int
dnode_read(const spa_t * spa,const dnode_phys_t * dnode,off_t offset,void * buf,size_t buflen)2313 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset,
2314 void *buf, size_t buflen)
2315 {
2316 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
2317 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2318 int nlevels = dnode->dn_nlevels;
2319 int i, rc;
2320
2321 if (bsize > SPA_MAXBLOCKSIZE) {
2322 printf("ZFS: I/O error - blocks larger than %llu are not "
2323 "supported\n", SPA_MAXBLOCKSIZE);
2324 return (EIO);
2325 }
2326
2327 /*
2328 * Handle odd block sizes, mirrors dmu_read_impl(). Data can't exist
2329 * past the first block, so we'll clip the read to the portion of the
2330 * buffer within bsize and zero out the remainder.
2331 */
2332 if (dnode->dn_maxblkid == 0) {
2333 size_t newbuflen;
2334
2335 newbuflen = offset > bsize ? 0 : MIN(buflen, bsize - offset);
2336 bzero((char *)buf + newbuflen, buflen - newbuflen);
2337 buflen = newbuflen;
2338 }
2339
2340 /*
2341 * Note: bsize may not be a power of two here so we need to do an
2342 * actual divide rather than a bitshift.
2343 */
2344 while (buflen > 0) {
2345 uint64_t bn = offset / bsize;
2346 int boff = offset % bsize;
2347 int ibn;
2348 const blkptr_t *indbp;
2349 blkptr_t bp;
2350
2351 if (bn > dnode->dn_maxblkid)
2352 return (EIO);
2353
2354 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
2355 goto cached;
2356
2357 indbp = dnode->dn_blkptr;
2358 for (i = 0; i < nlevels; i++) {
2359 /*
2360 * Copy the bp from the indirect array so that
2361 * we can re-use the scratch buffer for multi-level
2362 * objects.
2363 */
2364 ibn = bn >> ((nlevels - i - 1) * ibshift);
2365 ibn &= ((1 << ibshift) - 1);
2366 bp = indbp[ibn];
2367 if (BP_IS_HOLE(&bp)) {
2368 memset(dnode_cache_buf, 0, bsize);
2369 break;
2370 }
2371 rc = zio_read(spa, &bp, dnode_cache_buf);
2372 if (rc)
2373 return (rc);
2374 indbp = (const blkptr_t *) dnode_cache_buf;
2375 }
2376 dnode_cache_obj = dnode;
2377 dnode_cache_bn = bn;
2378 cached:
2379
2380 /*
2381 * The buffer contains our data block. Copy what we
2382 * need from it and loop.
2383 */
2384 i = bsize - boff;
2385 if (i > buflen) i = buflen;
2386 memcpy(buf, &dnode_cache_buf[boff], i);
2387 buf = ((char *)buf) + i;
2388 offset += i;
2389 buflen -= i;
2390 }
2391
2392 return (0);
2393 }
2394
2395 /*
2396 * Lookup a value in a microzap directory.
2397 */
2398 static int
mzap_lookup(const mzap_phys_t * mz,size_t size,const char * name,uint64_t * value)2399 mzap_lookup(const mzap_phys_t *mz, size_t size, const char *name,
2400 uint64_t *value)
2401 {
2402 const mzap_ent_phys_t *mze;
2403 int chunks, i;
2404
2405 /*
2406 * Microzap objects use exactly one block. Read the whole
2407 * thing.
2408 */
2409 chunks = size / MZAP_ENT_LEN - 1;
2410 for (i = 0; i < chunks; i++) {
2411 mze = &mz->mz_chunk[i];
2412 if (strcmp(mze->mze_name, name) == 0) {
2413 *value = mze->mze_value;
2414 return (0);
2415 }
2416 }
2417
2418 return (ENOENT);
2419 }
2420
2421 /*
2422 * Compare a name with a zap leaf entry. Return non-zero if the name
2423 * matches.
2424 */
2425 static int
fzap_name_equal(const zap_leaf_t * zl,const zap_leaf_chunk_t * zc,const char * name)2426 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2427 const char *name)
2428 {
2429 size_t namelen;
2430 const zap_leaf_chunk_t *nc;
2431 const char *p;
2432
2433 namelen = zc->l_entry.le_name_numints;
2434
2435 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2436 p = name;
2437 while (namelen > 0) {
2438 size_t len;
2439
2440 len = namelen;
2441 if (len > ZAP_LEAF_ARRAY_BYTES)
2442 len = ZAP_LEAF_ARRAY_BYTES;
2443 if (memcmp(p, nc->l_array.la_array, len))
2444 return (0);
2445 p += len;
2446 namelen -= len;
2447 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2448 }
2449
2450 return (1);
2451 }
2452
2453 /*
2454 * Extract a uint64_t value from a zap leaf entry.
2455 */
2456 static uint64_t
fzap_leaf_value(const zap_leaf_t * zl,const zap_leaf_chunk_t * zc)2457 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
2458 {
2459 const zap_leaf_chunk_t *vc;
2460 int i;
2461 uint64_t value;
2462 const uint8_t *p;
2463
2464 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
2465 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
2466 value = (value << 8) | p[i];
2467 }
2468
2469 return (value);
2470 }
2471
2472 static void
stv(int len,void * addr,uint64_t value)2473 stv(int len, void *addr, uint64_t value)
2474 {
2475 switch (len) {
2476 case 1:
2477 *(uint8_t *)addr = value;
2478 return;
2479 case 2:
2480 *(uint16_t *)addr = value;
2481 return;
2482 case 4:
2483 *(uint32_t *)addr = value;
2484 return;
2485 case 8:
2486 *(uint64_t *)addr = value;
2487 return;
2488 }
2489 }
2490
2491 /*
2492 * Extract a array from a zap leaf entry.
2493 */
2494 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)2495 fzap_leaf_array(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2496 uint64_t integer_size, uint64_t num_integers, void *buf)
2497 {
2498 uint64_t array_int_len = zc->l_entry.le_value_intlen;
2499 uint64_t value = 0;
2500 uint64_t *u64 = buf;
2501 char *p = buf;
2502 int len = MIN(zc->l_entry.le_value_numints, num_integers);
2503 int chunk = zc->l_entry.le_value_chunk;
2504 int byten = 0;
2505
2506 if (integer_size == 8 && len == 1) {
2507 *u64 = fzap_leaf_value(zl, zc);
2508 return;
2509 }
2510
2511 while (len > 0) {
2512 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array;
2513 int i;
2514
2515 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(zl));
2516 for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) {
2517 value = (value << 8) | la->la_array[i];
2518 byten++;
2519 if (byten == array_int_len) {
2520 stv(integer_size, p, value);
2521 byten = 0;
2522 len--;
2523 if (len == 0)
2524 return;
2525 p += integer_size;
2526 }
2527 }
2528 chunk = la->la_next;
2529 }
2530 }
2531
2532 static int
fzap_check_size(uint64_t integer_size,uint64_t num_integers)2533 fzap_check_size(uint64_t integer_size, uint64_t num_integers)
2534 {
2535
2536 switch (integer_size) {
2537 case 1:
2538 case 2:
2539 case 4:
2540 case 8:
2541 break;
2542 default:
2543 return (EINVAL);
2544 }
2545
2546 if (integer_size * num_integers > ZAP_MAXVALUELEN)
2547 return (E2BIG);
2548
2549 return (0);
2550 }
2551
2552 static void
zap_leaf_free(zap_leaf_t * leaf)2553 zap_leaf_free(zap_leaf_t *leaf)
2554 {
2555 free(leaf->l_phys);
2556 free(leaf);
2557 }
2558
2559 static int
zap_get_leaf_byblk(fat_zap_t * zap,uint64_t blk,zap_leaf_t ** lp)2560 zap_get_leaf_byblk(fat_zap_t *zap, uint64_t blk, zap_leaf_t **lp)
2561 {
2562 int bs = FZAP_BLOCK_SHIFT(zap);
2563 int err;
2564
2565 *lp = malloc(sizeof(**lp));
2566 if (*lp == NULL)
2567 return (ENOMEM);
2568
2569 (*lp)->l_bs = bs;
2570 (*lp)->l_phys = malloc(1 << bs);
2571
2572 if ((*lp)->l_phys == NULL) {
2573 free(*lp);
2574 return (ENOMEM);
2575 }
2576 err = dnode_read(zap->zap_spa, zap->zap_dnode, blk << bs, (*lp)->l_phys,
2577 1 << bs);
2578 if (err != 0) {
2579 zap_leaf_free(*lp);
2580 }
2581 return (err);
2582 }
2583
2584 static int
zap_table_load(fat_zap_t * zap,zap_table_phys_t * tbl,uint64_t idx,uint64_t * valp)2585 zap_table_load(fat_zap_t *zap, zap_table_phys_t *tbl, uint64_t idx,
2586 uint64_t *valp)
2587 {
2588 int bs = FZAP_BLOCK_SHIFT(zap);
2589 uint64_t blk = idx >> (bs - 3);
2590 uint64_t off = idx & ((1 << (bs - 3)) - 1);
2591 uint64_t *buf;
2592 int rc;
2593
2594 buf = malloc(1 << zap->zap_block_shift);
2595 if (buf == NULL)
2596 return (ENOMEM);
2597 rc = dnode_read(zap->zap_spa, zap->zap_dnode, (tbl->zt_blk + blk) << bs,
2598 buf, 1 << zap->zap_block_shift);
2599 if (rc == 0)
2600 *valp = buf[off];
2601 free(buf);
2602 return (rc);
2603 }
2604
2605 static int
zap_idx_to_blk(fat_zap_t * zap,uint64_t idx,uint64_t * valp)2606 zap_idx_to_blk(fat_zap_t *zap, uint64_t idx, uint64_t *valp)
2607 {
2608 if (zap->zap_phys->zap_ptrtbl.zt_numblks == 0) {
2609 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
2610 return (0);
2611 } else {
2612 return (zap_table_load(zap, &zap->zap_phys->zap_ptrtbl,
2613 idx, valp));
2614 }
2615 }
2616
2617 #define ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n))))
2618 static int
zap_deref_leaf(fat_zap_t * zap,uint64_t h,zap_leaf_t ** lp)2619 zap_deref_leaf(fat_zap_t *zap, uint64_t h, zap_leaf_t **lp)
2620 {
2621 uint64_t idx, blk;
2622 int err;
2623
2624 idx = ZAP_HASH_IDX(h, zap->zap_phys->zap_ptrtbl.zt_shift);
2625 err = zap_idx_to_blk(zap, idx, &blk);
2626 if (err != 0)
2627 return (err);
2628 return (zap_get_leaf_byblk(zap, blk, lp));
2629 }
2630
2631 #define CHAIN_END 0xffff /* end of the chunk chain */
2632 #define LEAF_HASH(l, h) \
2633 ((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \
2634 ((h) >> \
2635 (64 - ZAP_LEAF_HASH_SHIFT(l) - (l)->l_phys->l_hdr.lh_prefix_len)))
2636 #define LEAF_HASH_ENTPTR(l, h) (&(l)->l_phys->l_hash[LEAF_HASH(l, h)])
2637
2638 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)2639 zap_leaf_lookup(zap_leaf_t *zl, uint64_t hash, const char *name,
2640 uint64_t integer_size, uint64_t num_integers, void *value)
2641 {
2642 int rc;
2643 uint16_t *chunkp;
2644 struct zap_leaf_entry *le;
2645
2646 /*
2647 * Make sure this chunk matches our hash.
2648 */
2649 if (zl->l_phys->l_hdr.lh_prefix_len > 0 &&
2650 zl->l_phys->l_hdr.lh_prefix !=
2651 hash >> (64 - zl->l_phys->l_hdr.lh_prefix_len))
2652 return (EIO);
2653
2654 rc = ENOENT;
2655 for (chunkp = LEAF_HASH_ENTPTR(zl, hash);
2656 *chunkp != CHAIN_END; chunkp = &le->le_next) {
2657 zap_leaf_chunk_t *zc;
2658 uint16_t chunk = *chunkp;
2659
2660 le = ZAP_LEAF_ENTRY(zl, chunk);
2661 if (le->le_hash != hash)
2662 continue;
2663 zc = &ZAP_LEAF_CHUNK(zl, chunk);
2664 if (fzap_name_equal(zl, zc, name)) {
2665 if (zc->l_entry.le_value_intlen > integer_size) {
2666 rc = EINVAL;
2667 } else {
2668 fzap_leaf_array(zl, zc, integer_size,
2669 num_integers, value);
2670 rc = 0;
2671 }
2672 break;
2673 }
2674 }
2675 return (rc);
2676 }
2677
2678 /*
2679 * Lookup a value in a fatzap directory.
2680 */
2681 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)2682 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2683 const char *name, uint64_t integer_size, uint64_t num_integers,
2684 void *value)
2685 {
2686 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2687 fat_zap_t z;
2688 zap_leaf_t *zl;
2689 uint64_t hash;
2690 int rc;
2691
2692 if (zh->zap_magic != ZAP_MAGIC)
2693 return (EIO);
2694
2695 if ((rc = fzap_check_size(integer_size, num_integers)) != 0) {
2696 return (rc);
2697 }
2698
2699 z.zap_block_shift = ilog2(bsize);
2700 z.zap_phys = zh;
2701 z.zap_spa = spa;
2702 z.zap_dnode = dnode;
2703
2704 hash = zap_hash(zh->zap_salt, name);
2705 rc = zap_deref_leaf(&z, hash, &zl);
2706 if (rc != 0)
2707 return (rc);
2708
2709 rc = zap_leaf_lookup(zl, hash, name, integer_size, num_integers, value);
2710
2711 zap_leaf_free(zl);
2712 return (rc);
2713 }
2714
2715 /*
2716 * Lookup a name in a zap object and return its value as a uint64_t.
2717 */
2718 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)2719 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
2720 uint64_t integer_size, uint64_t num_integers, void *value)
2721 {
2722 int rc;
2723 zap_phys_t *zap;
2724 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2725
2726 zap = malloc(size);
2727 if (zap == NULL)
2728 return (ENOMEM);
2729
2730 rc = dnode_read(spa, dnode, 0, zap, size);
2731 if (rc)
2732 goto done;
2733
2734 switch (zap->zap_block_type) {
2735 case ZBT_MICRO:
2736 rc = mzap_lookup((const mzap_phys_t *)zap, size, name, value);
2737 break;
2738 case ZBT_HEADER:
2739 rc = fzap_lookup(spa, dnode, zap, name, integer_size,
2740 num_integers, value);
2741 break;
2742 default:
2743 printf("ZFS: invalid zap_type=%" PRIx64 "\n",
2744 zap->zap_block_type);
2745 rc = EIO;
2746 }
2747 done:
2748 free(zap);
2749 return (rc);
2750 }
2751
2752 /*
2753 * List a microzap directory.
2754 */
2755 static int
mzap_list(const mzap_phys_t * mz,size_t size,int (* callback)(const char *,uint64_t))2756 mzap_list(const mzap_phys_t *mz, size_t size,
2757 int (*callback)(const char *, uint64_t))
2758 {
2759 const mzap_ent_phys_t *mze;
2760 int chunks, i, rc;
2761
2762 /*
2763 * Microzap objects use exactly one block. Read the whole
2764 * thing.
2765 */
2766 rc = 0;
2767 chunks = size / MZAP_ENT_LEN - 1;
2768 for (i = 0; i < chunks; i++) {
2769 mze = &mz->mz_chunk[i];
2770 if (mze->mze_name[0]) {
2771 rc = callback(mze->mze_name, mze->mze_value);
2772 if (rc != 0)
2773 break;
2774 }
2775 }
2776
2777 return (rc);
2778 }
2779
2780 /*
2781 * List a fatzap directory.
2782 */
2783 static int
fzap_list(const spa_t * spa,const dnode_phys_t * dnode,zap_phys_t * zh,int (* callback)(const char *,uint64_t))2784 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2785 int (*callback)(const char *, uint64_t))
2786 {
2787 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2788 fat_zap_t z;
2789 uint64_t i;
2790 int j, rc;
2791
2792 if (zh->zap_magic != ZAP_MAGIC)
2793 return (EIO);
2794
2795 z.zap_block_shift = ilog2(bsize);
2796 z.zap_phys = zh;
2797
2798 /*
2799 * This assumes that the leaf blocks start at block 1. The
2800 * documentation isn't exactly clear on this.
2801 */
2802 zap_leaf_t zl;
2803 zl.l_bs = z.zap_block_shift;
2804 zl.l_phys = malloc(bsize);
2805 if (zl.l_phys == NULL)
2806 return (ENOMEM);
2807
2808 for (i = 0; i < zh->zap_num_leafs; i++) {
2809 off_t off = ((off_t)(i + 1)) << zl.l_bs;
2810 char name[256], *p;
2811 uint64_t value;
2812
2813 if (dnode_read(spa, dnode, off, zl.l_phys, bsize)) {
2814 free(zl.l_phys);
2815 return (EIO);
2816 }
2817
2818 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
2819 zap_leaf_chunk_t *zc, *nc;
2820 int namelen;
2821
2822 zc = &ZAP_LEAF_CHUNK(&zl, j);
2823 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
2824 continue;
2825 namelen = zc->l_entry.le_name_numints;
2826 if (namelen > sizeof(name))
2827 namelen = sizeof(name);
2828
2829 /*
2830 * Paste the name back together.
2831 */
2832 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
2833 p = name;
2834 while (namelen > 0) {
2835 int len;
2836 len = namelen;
2837 if (len > ZAP_LEAF_ARRAY_BYTES)
2838 len = ZAP_LEAF_ARRAY_BYTES;
2839 memcpy(p, nc->l_array.la_array, len);
2840 p += len;
2841 namelen -= len;
2842 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
2843 }
2844
2845 /*
2846 * Assume the first eight bytes of the value are
2847 * a uint64_t.
2848 */
2849 value = fzap_leaf_value(&zl, zc);
2850
2851 /* printf("%s 0x%jx\n", name, (uintmax_t)value); */
2852 rc = callback((const char *)name, value);
2853 if (rc != 0) {
2854 free(zl.l_phys);
2855 return (rc);
2856 }
2857 }
2858 }
2859
2860 free(zl.l_phys);
2861 return (0);
2862 }
2863
zfs_printf(const char * name,uint64_t value __unused)2864 static int zfs_printf(const char *name, uint64_t value __unused)
2865 {
2866
2867 printf("%s\n", name);
2868
2869 return (0);
2870 }
2871
2872 /*
2873 * List a zap directory.
2874 */
2875 static int
zap_list(const spa_t * spa,const dnode_phys_t * dnode)2876 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
2877 {
2878 zap_phys_t *zap;
2879 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2880 int rc;
2881
2882 zap = malloc(size);
2883 if (zap == NULL)
2884 return (ENOMEM);
2885
2886 rc = dnode_read(spa, dnode, 0, zap, size);
2887 if (rc == 0) {
2888 if (zap->zap_block_type == ZBT_MICRO)
2889 rc = mzap_list((const mzap_phys_t *)zap, size,
2890 zfs_printf);
2891 else
2892 rc = fzap_list(spa, dnode, zap, zfs_printf);
2893 }
2894 free(zap);
2895 return (rc);
2896 }
2897
2898 static int
objset_get_dnode(const spa_t * spa,const objset_phys_t * os,uint64_t objnum,dnode_phys_t * dnode)2899 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum,
2900 dnode_phys_t *dnode)
2901 {
2902 off_t offset;
2903
2904 offset = objnum * sizeof(dnode_phys_t);
2905 return dnode_read(spa, &os->os_meta_dnode, offset,
2906 dnode, sizeof(dnode_phys_t));
2907 }
2908
2909 /*
2910 * Lookup a name in a microzap directory.
2911 */
2912 static int
mzap_rlookup(const mzap_phys_t * mz,size_t size,char * name,uint64_t value)2913 mzap_rlookup(const mzap_phys_t *mz, size_t size, char *name, uint64_t value)
2914 {
2915 const mzap_ent_phys_t *mze;
2916 int chunks, i;
2917
2918 /*
2919 * Microzap objects use exactly one block. Read the whole
2920 * thing.
2921 */
2922 chunks = size / MZAP_ENT_LEN - 1;
2923 for (i = 0; i < chunks; i++) {
2924 mze = &mz->mz_chunk[i];
2925 if (value == mze->mze_value) {
2926 strcpy(name, mze->mze_name);
2927 return (0);
2928 }
2929 }
2930
2931 return (ENOENT);
2932 }
2933
2934 static void
fzap_name_copy(const zap_leaf_t * zl,const zap_leaf_chunk_t * zc,char * name)2935 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
2936 {
2937 size_t namelen;
2938 const zap_leaf_chunk_t *nc;
2939 char *p;
2940
2941 namelen = zc->l_entry.le_name_numints;
2942
2943 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2944 p = name;
2945 while (namelen > 0) {
2946 size_t len;
2947 len = namelen;
2948 if (len > ZAP_LEAF_ARRAY_BYTES)
2949 len = ZAP_LEAF_ARRAY_BYTES;
2950 memcpy(p, nc->l_array.la_array, len);
2951 p += len;
2952 namelen -= len;
2953 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2954 }
2955
2956 *p = '\0';
2957 }
2958
2959 static int
fzap_rlookup(const spa_t * spa,const dnode_phys_t * dnode,zap_phys_t * zh,char * name,uint64_t value)2960 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2961 char *name, uint64_t value)
2962 {
2963 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2964 fat_zap_t z;
2965 uint64_t i;
2966 int j, rc;
2967
2968 if (zh->zap_magic != ZAP_MAGIC)
2969 return (EIO);
2970
2971 z.zap_block_shift = ilog2(bsize);
2972 z.zap_phys = zh;
2973
2974 /*
2975 * This assumes that the leaf blocks start at block 1. The
2976 * documentation isn't exactly clear on this.
2977 */
2978 zap_leaf_t zl;
2979 zl.l_bs = z.zap_block_shift;
2980 zl.l_phys = malloc(bsize);
2981 if (zl.l_phys == NULL)
2982 return (ENOMEM);
2983
2984 for (i = 0; i < zh->zap_num_leafs; i++) {
2985 off_t off = ((off_t)(i + 1)) << zl.l_bs;
2986
2987 rc = dnode_read(spa, dnode, off, zl.l_phys, bsize);
2988 if (rc != 0)
2989 goto done;
2990
2991 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
2992 zap_leaf_chunk_t *zc;
2993
2994 zc = &ZAP_LEAF_CHUNK(&zl, j);
2995 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
2996 continue;
2997 if (zc->l_entry.le_value_intlen != 8 ||
2998 zc->l_entry.le_value_numints != 1)
2999 continue;
3000
3001 if (fzap_leaf_value(&zl, zc) == value) {
3002 fzap_name_copy(&zl, zc, name);
3003 goto done;
3004 }
3005 }
3006 }
3007
3008 rc = ENOENT;
3009 done:
3010 free(zl.l_phys);
3011 return (rc);
3012 }
3013
3014 static int
zap_rlookup(const spa_t * spa,const dnode_phys_t * dnode,char * name,uint64_t value)3015 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name,
3016 uint64_t value)
3017 {
3018 zap_phys_t *zap;
3019 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
3020 int rc;
3021
3022 zap = malloc(size);
3023 if (zap == NULL)
3024 return (ENOMEM);
3025
3026 rc = dnode_read(spa, dnode, 0, zap, size);
3027 if (rc == 0) {
3028 if (zap->zap_block_type == ZBT_MICRO)
3029 rc = mzap_rlookup((const mzap_phys_t *)zap, size,
3030 name, value);
3031 else
3032 rc = fzap_rlookup(spa, dnode, zap, name, value);
3033 }
3034 free(zap);
3035 return (rc);
3036 }
3037
3038 static int
zfs_rlookup(const spa_t * spa,uint64_t objnum,char * result)3039 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
3040 {
3041 char name[256];
3042 char component[256];
3043 uint64_t dir_obj, parent_obj, child_dir_zapobj;
3044 dnode_phys_t child_dir_zap, snapnames_zap, dataset, dir, parent;
3045 dsl_dir_phys_t *dd;
3046 dsl_dataset_phys_t *ds;
3047 char *p;
3048 int len;
3049 boolean_t issnap = B_FALSE;
3050
3051 p = &name[sizeof(name) - 1];
3052 *p = '\0';
3053
3054 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3055 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3056 return (EIO);
3057 }
3058 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3059 dir_obj = ds->ds_dir_obj;
3060 if (ds->ds_snapnames_zapobj == 0)
3061 issnap = B_TRUE;
3062
3063 for (;;) {
3064 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir) != 0)
3065 return (EIO);
3066 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3067
3068 /* Actual loop condition. */
3069 parent_obj = dd->dd_parent_obj;
3070 if (parent_obj == 0)
3071 break;
3072
3073 if (objset_get_dnode(spa, spa->spa_mos, parent_obj,
3074 &parent) != 0)
3075 return (EIO);
3076 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
3077 if (issnap == B_TRUE) {
3078 /*
3079 * The dataset we are looking up is a snapshot
3080 * the dir_obj is the parent already, we don't want
3081 * the grandparent just yet. Reset to the parent.
3082 */
3083 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3084 /* Lookup the dataset to get the snapname ZAP */
3085 if (objset_get_dnode(spa, spa->spa_mos,
3086 dd->dd_head_dataset_obj, &dataset))
3087 return (EIO);
3088 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3089 if (objset_get_dnode(spa, spa->spa_mos,
3090 ds->ds_snapnames_zapobj, &snapnames_zap) != 0)
3091 return (EIO);
3092 /* Get the name of the snapshot */
3093 if (zap_rlookup(spa, &snapnames_zap, component,
3094 objnum) != 0)
3095 return (EIO);
3096 len = strlen(component);
3097 p -= len;
3098 memcpy(p, component, len);
3099 --p;
3100 *p = '@';
3101 issnap = B_FALSE;
3102 continue;
3103 }
3104
3105 child_dir_zapobj = dd->dd_child_dir_zapobj;
3106 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3107 &child_dir_zap) != 0)
3108 return (EIO);
3109 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
3110 return (EIO);
3111
3112 len = strlen(component);
3113 p -= len;
3114 memcpy(p, component, len);
3115 --p;
3116 *p = '/';
3117
3118 /* Actual loop iteration. */
3119 dir_obj = parent_obj;
3120 }
3121
3122 if (*p != '\0')
3123 ++p;
3124 strcpy(result, p);
3125
3126 return (0);
3127 }
3128
3129 static int
zfs_lookup_dataset(const spa_t * spa,const char * name,uint64_t * objnum)3130 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
3131 {
3132 char element[256];
3133 uint64_t dir_obj, child_dir_zapobj;
3134 dnode_phys_t child_dir_zap, snapnames_zap, dir, dataset;
3135 dsl_dir_phys_t *dd;
3136 dsl_dataset_phys_t *ds;
3137 const char *p, *q;
3138 boolean_t issnap = B_FALSE;
3139
3140 if (objset_get_dnode(spa, spa->spa_mos,
3141 DMU_POOL_DIRECTORY_OBJECT, &dir))
3142 return (EIO);
3143 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj),
3144 1, &dir_obj))
3145 return (EIO);
3146
3147 p = name;
3148 for (;;) {
3149 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir))
3150 return (EIO);
3151 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3152
3153 while (*p == '/')
3154 p++;
3155 /* Actual loop condition #1. */
3156 if (*p == '\0')
3157 break;
3158
3159 q = strchr(p, '/');
3160 if (q) {
3161 memcpy(element, p, q - p);
3162 element[q - p] = '\0';
3163 p = q + 1;
3164 } else {
3165 strcpy(element, p);
3166 p += strlen(p);
3167 }
3168
3169 if (issnap == B_TRUE) {
3170 if (objset_get_dnode(spa, spa->spa_mos,
3171 dd->dd_head_dataset_obj, &dataset))
3172 return (EIO);
3173 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3174 if (objset_get_dnode(spa, spa->spa_mos,
3175 ds->ds_snapnames_zapobj, &snapnames_zap) != 0)
3176 return (EIO);
3177 /* Actual loop condition #2. */
3178 if (zap_lookup(spa, &snapnames_zap, element,
3179 sizeof (dir_obj), 1, &dir_obj) != 0)
3180 return (ENOENT);
3181 *objnum = dir_obj;
3182 return (0);
3183 } else if ((q = strchr(element, '@')) != NULL) {
3184 issnap = B_TRUE;
3185 element[q - element] = '\0';
3186 p = q + 1;
3187 }
3188 child_dir_zapobj = dd->dd_child_dir_zapobj;
3189 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3190 &child_dir_zap) != 0)
3191 return (EIO);
3192
3193 /* Actual loop condition #2. */
3194 if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj),
3195 1, &dir_obj) != 0)
3196 return (ENOENT);
3197 }
3198
3199 *objnum = dd->dd_head_dataset_obj;
3200 return (0);
3201 }
3202
3203 #ifndef BOOT2
3204 static int
zfs_list_dataset(const spa_t * spa,uint64_t objnum)3205 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
3206 {
3207 uint64_t dir_obj, child_dir_zapobj;
3208 dnode_phys_t child_dir_zap, dir, dataset;
3209 dsl_dataset_phys_t *ds;
3210 dsl_dir_phys_t *dd;
3211
3212 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3213 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3214 return (EIO);
3215 }
3216 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3217 dir_obj = ds->ds_dir_obj;
3218
3219 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir)) {
3220 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
3221 return (EIO);
3222 }
3223 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3224
3225 child_dir_zapobj = dd->dd_child_dir_zapobj;
3226 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3227 &child_dir_zap) != 0) {
3228 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
3229 return (EIO);
3230 }
3231
3232 return (zap_list(spa, &child_dir_zap) != 0);
3233 }
3234
3235 int
zfs_callback_dataset(const spa_t * spa,uint64_t objnum,int (* callback)(const char *,uint64_t))3236 zfs_callback_dataset(const spa_t *spa, uint64_t objnum,
3237 int (*callback)(const char *, uint64_t))
3238 {
3239 uint64_t dir_obj, child_dir_zapobj;
3240 dnode_phys_t child_dir_zap, dir, dataset;
3241 dsl_dataset_phys_t *ds;
3242 dsl_dir_phys_t *dd;
3243 zap_phys_t *zap;
3244 size_t size;
3245 int err;
3246
3247 err = objset_get_dnode(spa, spa->spa_mos, objnum, &dataset);
3248 if (err != 0) {
3249 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3250 return (err);
3251 }
3252 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3253 dir_obj = ds->ds_dir_obj;
3254
3255 err = objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir);
3256 if (err != 0) {
3257 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
3258 return (err);
3259 }
3260 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3261
3262 child_dir_zapobj = dd->dd_child_dir_zapobj;
3263 err = objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj,
3264 &child_dir_zap);
3265 if (err != 0) {
3266 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
3267 return (err);
3268 }
3269
3270 size = child_dir_zap.dn_datablkszsec << SPA_MINBLOCKSHIFT;
3271 zap = malloc(size);
3272 if (zap != NULL) {
3273 err = dnode_read(spa, &child_dir_zap, 0, zap, size);
3274 if (err != 0)
3275 goto done;
3276
3277 if (zap->zap_block_type == ZBT_MICRO)
3278 err = mzap_list((const mzap_phys_t *)zap, size,
3279 callback);
3280 else
3281 err = fzap_list(spa, &child_dir_zap, zap, callback);
3282 } else {
3283 err = ENOMEM;
3284 }
3285 done:
3286 free(zap);
3287 return (err);
3288 }
3289 #endif
3290
3291 /*
3292 * Find the object set given the object number of its dataset object
3293 * and return its details in *objset
3294 */
3295 static int
zfs_mount_dataset(const spa_t * spa,uint64_t objnum,objset_phys_t * objset)3296 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
3297 {
3298 dnode_phys_t dataset;
3299 dsl_dataset_phys_t *ds;
3300
3301 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) {
3302 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3303 return (EIO);
3304 }
3305
3306 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3307 if (zio_read(spa, &ds->ds_bp, objset)) {
3308 printf("ZFS: can't read object set for dataset %ju\n",
3309 (uintmax_t)objnum);
3310 return (EIO);
3311 }
3312
3313 return (0);
3314 }
3315
3316 /*
3317 * Find the object set pointed to by the BOOTFS property or the root
3318 * dataset if there is none and return its details in *objset
3319 */
3320 static int
zfs_get_root(const spa_t * spa,uint64_t * objid)3321 zfs_get_root(const spa_t *spa, uint64_t *objid)
3322 {
3323 dnode_phys_t dir, propdir;
3324 uint64_t props, bootfs, root;
3325
3326 *objid = 0;
3327
3328 /*
3329 * Start with the MOS directory object.
3330 */
3331 if (objset_get_dnode(spa, spa->spa_mos,
3332 DMU_POOL_DIRECTORY_OBJECT, &dir)) {
3333 printf("ZFS: can't read MOS object directory\n");
3334 return (EIO);
3335 }
3336
3337 /*
3338 * Lookup the pool_props and see if we can find a bootfs.
3339 */
3340 if (zap_lookup(spa, &dir, DMU_POOL_PROPS,
3341 sizeof(props), 1, &props) == 0 &&
3342 objset_get_dnode(spa, spa->spa_mos, props, &propdir) == 0 &&
3343 zap_lookup(spa, &propdir, "bootfs",
3344 sizeof(bootfs), 1, &bootfs) == 0 && bootfs != 0) {
3345 *objid = bootfs;
3346 return (0);
3347 }
3348 /*
3349 * Lookup the root dataset directory
3350 */
3351 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET,
3352 sizeof(root), 1, &root) ||
3353 objset_get_dnode(spa, spa->spa_mos, root, &dir)) {
3354 printf("ZFS: can't find root dsl_dir\n");
3355 return (EIO);
3356 }
3357
3358 /*
3359 * Use the information from the dataset directory's bonus buffer
3360 * to find the dataset object and from that the object set itself.
3361 */
3362 dsl_dir_phys_t *dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3363 *objid = dd->dd_head_dataset_obj;
3364 return (0);
3365 }
3366
3367 static int
zfs_mount_impl(const spa_t * spa,uint64_t rootobj,struct zfsmount * mount)3368 zfs_mount_impl(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
3369 {
3370
3371 mount->spa = spa;
3372
3373 /*
3374 * Find the root object set if not explicitly provided
3375 */
3376 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
3377 printf("ZFS: can't find root filesystem\n");
3378 return (EIO);
3379 }
3380
3381 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
3382 printf("ZFS: can't open root filesystem\n");
3383 return (EIO);
3384 }
3385
3386 mount->rootobj = rootobj;
3387
3388 return (0);
3389 }
3390
3391 /*
3392 * callback function for feature name checks.
3393 */
3394 static int
check_feature(const char * name,uint64_t value)3395 check_feature(const char *name, uint64_t value)
3396 {
3397 int i;
3398
3399 if (value == 0)
3400 return (0);
3401 if (name[0] == '\0')
3402 return (0);
3403
3404 for (i = 0; features_for_read[i] != NULL; i++) {
3405 if (strcmp(name, features_for_read[i]) == 0)
3406 return (0);
3407 }
3408 printf("ZFS: unsupported feature: %s\n", name);
3409 return (EIO);
3410 }
3411
3412 /*
3413 * Checks whether the MOS features that are active are supported.
3414 */
3415 static int
check_mos_features(const spa_t * spa)3416 check_mos_features(const spa_t *spa)
3417 {
3418 dnode_phys_t dir;
3419 zap_phys_t *zap;
3420 uint64_t objnum;
3421 size_t size;
3422 int rc;
3423
3424 if ((rc = objset_get_dnode(spa, spa->spa_mos, DMU_OT_OBJECT_DIRECTORY,
3425 &dir)) != 0)
3426 return (rc);
3427 if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ,
3428 sizeof (objnum), 1, &objnum)) != 0) {
3429 /*
3430 * It is older pool without features. As we have already
3431 * tested the label, just return without raising the error.
3432 */
3433 return (0);
3434 }
3435
3436 if ((rc = objset_get_dnode(spa, spa->spa_mos, objnum, &dir)) != 0)
3437 return (rc);
3438
3439 if (dir.dn_type != DMU_OTN_ZAP_METADATA)
3440 return (EIO);
3441
3442 size = dir.dn_datablkszsec << SPA_MINBLOCKSHIFT;
3443 zap = malloc(size);
3444 if (zap == NULL)
3445 return (ENOMEM);
3446
3447 if (dnode_read(spa, &dir, 0, zap, size)) {
3448 free(zap);
3449 return (EIO);
3450 }
3451
3452 if (zap->zap_block_type == ZBT_MICRO)
3453 rc = mzap_list((const mzap_phys_t *)zap, size, check_feature);
3454 else
3455 rc = fzap_list(spa, &dir, zap, check_feature);
3456
3457 free(zap);
3458 return (rc);
3459 }
3460
3461 static int
load_nvlist(spa_t * spa,uint64_t obj,nvlist_t ** value)3462 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
3463 {
3464 dnode_phys_t dir;
3465 size_t size;
3466 int rc;
3467 char *nv;
3468
3469 *value = NULL;
3470 if ((rc = objset_get_dnode(spa, spa->spa_mos, obj, &dir)) != 0)
3471 return (rc);
3472 if (dir.dn_type != DMU_OT_PACKED_NVLIST &&
3473 dir.dn_bonustype != DMU_OT_PACKED_NVLIST_SIZE) {
3474 return (EIO);
3475 }
3476
3477 if (dir.dn_bonuslen != sizeof (uint64_t))
3478 return (EIO);
3479
3480 size = *(uint64_t *)DN_BONUS(&dir);
3481 nv = malloc(size);
3482 if (nv == NULL)
3483 return (ENOMEM);
3484
3485 rc = dnode_read(spa, &dir, 0, nv, size);
3486 if (rc != 0) {
3487 free(nv);
3488 nv = NULL;
3489 return (rc);
3490 }
3491 *value = nvlist_import(nv, size);
3492 free(nv);
3493 return (rc);
3494 }
3495
3496 static int
zfs_spa_init(spa_t * spa)3497 zfs_spa_init(spa_t *spa)
3498 {
3499 struct uberblock checkpoint;
3500 dnode_phys_t dir;
3501 uint64_t config_object;
3502 nvlist_t *nvlist;
3503 int rc;
3504
3505 if (zio_read(spa, &spa->spa_uberblock->ub_rootbp, spa->spa_mos)) {
3506 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
3507 return (EIO);
3508 }
3509 if (spa->spa_mos->os_type != DMU_OST_META) {
3510 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
3511 return (EIO);
3512 }
3513
3514 if (objset_get_dnode(spa, &spa->spa_mos_master,
3515 DMU_POOL_DIRECTORY_OBJECT, &dir)) {
3516 printf("ZFS: failed to read pool %s directory object\n",
3517 spa->spa_name);
3518 return (EIO);
3519 }
3520 /* this is allowed to fail, older pools do not have salt */
3521 rc = zap_lookup(spa, &dir, DMU_POOL_CHECKSUM_SALT, 1,
3522 sizeof (spa->spa_cksum_salt.zcs_bytes),
3523 spa->spa_cksum_salt.zcs_bytes);
3524
3525 rc = check_mos_features(spa);
3526 if (rc != 0) {
3527 printf("ZFS: pool %s is not supported\n", spa->spa_name);
3528 return (rc);
3529 }
3530
3531 rc = zap_lookup(spa, &dir, DMU_POOL_CONFIG,
3532 sizeof (config_object), 1, &config_object);
3533 if (rc != 0) {
3534 printf("ZFS: can not read MOS %s\n", DMU_POOL_CONFIG);
3535 return (EIO);
3536 }
3537 rc = load_nvlist(spa, config_object, &nvlist);
3538 if (rc != 0) {
3539 printf("ZFS: failed to load pool %s nvlist\n", spa->spa_name);
3540 return (rc);
3541 }
3542
3543 rc = zap_lookup(spa, &dir, DMU_POOL_ZPOOL_CHECKPOINT,
3544 sizeof(uint64_t), sizeof(checkpoint) / sizeof(uint64_t),
3545 &checkpoint);
3546 if (rc == 0 && checkpoint.ub_checkpoint_txg != 0) {
3547 memcpy(&spa->spa_uberblock_checkpoint, &checkpoint,
3548 sizeof(checkpoint));
3549 if (zio_read(spa, &spa->spa_uberblock_checkpoint.ub_rootbp,
3550 &spa->spa_mos_checkpoint)) {
3551 printf("ZFS: can not read checkpoint data.\n");
3552 return (EIO);
3553 }
3554 }
3555
3556 /*
3557 * Update vdevs from MOS config. Note, we do skip encoding bytes
3558 * here. See also vdev_label_read_config().
3559 */
3560 rc = vdev_init_from_nvlist(spa, nvlist);
3561 nvlist_destroy(nvlist);
3562 return (rc);
3563 }
3564
3565 static int
zfs_dnode_stat(const spa_t * spa,dnode_phys_t * dn,struct stat * sb)3566 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
3567 {
3568
3569 if (dn->dn_bonustype != DMU_OT_SA) {
3570 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
3571
3572 sb->st_mode = zp->zp_mode;
3573 sb->st_uid = zp->zp_uid;
3574 sb->st_gid = zp->zp_gid;
3575 sb->st_size = zp->zp_size;
3576 } else {
3577 sa_hdr_phys_t *sahdrp;
3578 int hdrsize;
3579 size_t size = 0;
3580 void *buf = NULL;
3581
3582 if (dn->dn_bonuslen != 0)
3583 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3584 else {
3585 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
3586 blkptr_t *bp = DN_SPILL_BLKPTR(dn);
3587 int error;
3588
3589 size = BP_GET_LSIZE(bp);
3590 buf = malloc(size);
3591 if (buf == NULL)
3592 error = ENOMEM;
3593 else
3594 error = zio_read(spa, bp, buf);
3595
3596 if (error != 0) {
3597 free(buf);
3598 return (error);
3599 }
3600 sahdrp = buf;
3601 } else {
3602 return (EIO);
3603 }
3604 }
3605 hdrsize = SA_HDR_SIZE(sahdrp);
3606 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
3607 SA_MODE_OFFSET);
3608 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
3609 SA_UID_OFFSET);
3610 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
3611 SA_GID_OFFSET);
3612 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
3613 SA_SIZE_OFFSET);
3614 free(buf);
3615 }
3616
3617 return (0);
3618 }
3619
3620 static int
zfs_dnode_readlink(const spa_t * spa,dnode_phys_t * dn,char * path,size_t psize)3621 zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize)
3622 {
3623 int rc = 0;
3624
3625 if (dn->dn_bonustype == DMU_OT_SA) {
3626 sa_hdr_phys_t *sahdrp = NULL;
3627 size_t size = 0;
3628 void *buf = NULL;
3629 int hdrsize;
3630 char *p;
3631
3632 if (dn->dn_bonuslen != 0) {
3633 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
3634 } else {
3635 blkptr_t *bp;
3636
3637 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0)
3638 return (EIO);
3639 bp = DN_SPILL_BLKPTR(dn);
3640
3641 size = BP_GET_LSIZE(bp);
3642 buf = malloc(size);
3643 if (buf == NULL)
3644 rc = ENOMEM;
3645 else
3646 rc = zio_read(spa, bp, buf);
3647 if (rc != 0) {
3648 free(buf);
3649 return (rc);
3650 }
3651 sahdrp = buf;
3652 }
3653 hdrsize = SA_HDR_SIZE(sahdrp);
3654 p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET);
3655 memcpy(path, p, psize);
3656 free(buf);
3657 return (0);
3658 }
3659 /*
3660 * Second test is purely to silence bogus compiler
3661 * warning about accessing past the end of dn_bonus.
3662 */
3663 if (psize + sizeof(znode_phys_t) <= dn->dn_bonuslen &&
3664 sizeof(znode_phys_t) <= sizeof(dn->dn_bonus)) {
3665 memcpy(path, &dn->dn_bonus[sizeof(znode_phys_t)], psize);
3666 } else {
3667 rc = dnode_read(spa, dn, 0, path, psize);
3668 }
3669 return (rc);
3670 }
3671
3672 struct obj_list {
3673 uint64_t objnum;
3674 STAILQ_ENTRY(obj_list) entry;
3675 };
3676
3677 /*
3678 * Lookup a file and return its dnode.
3679 */
3680 static int
zfs_lookup(const struct zfsmount * mount,const char * upath,dnode_phys_t * dnode)3681 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
3682 {
3683 int rc;
3684 uint64_t objnum;
3685 const spa_t *spa;
3686 dnode_phys_t dn;
3687 const char *p, *q;
3688 char element[256];
3689 char path[1024];
3690 int symlinks_followed = 0;
3691 struct stat sb;
3692 struct obj_list *entry, *tentry;
3693 STAILQ_HEAD(, obj_list) on_cache = STAILQ_HEAD_INITIALIZER(on_cache);
3694
3695 spa = mount->spa;
3696 if (mount->objset.os_type != DMU_OST_ZFS) {
3697 printf("ZFS: unexpected object set type %ju\n",
3698 (uintmax_t)mount->objset.os_type);
3699 return (EIO);
3700 }
3701
3702 if ((entry = malloc(sizeof(struct obj_list))) == NULL)
3703 return (ENOMEM);
3704
3705 /*
3706 * Get the root directory dnode.
3707 */
3708 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
3709 if (rc) {
3710 free(entry);
3711 return (rc);
3712 }
3713
3714 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, sizeof(objnum), 1, &objnum);
3715 if (rc) {
3716 free(entry);
3717 return (rc);
3718 }
3719 entry->objnum = objnum;
3720 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3721
3722 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3723 if (rc != 0)
3724 goto done;
3725
3726 p = upath;
3727 while (p && *p) {
3728 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3729 if (rc != 0)
3730 goto done;
3731
3732 while (*p == '/')
3733 p++;
3734 if (*p == '\0')
3735 break;
3736 q = p;
3737 while (*q != '\0' && *q != '/')
3738 q++;
3739
3740 /* skip dot */
3741 if (p + 1 == q && p[0] == '.') {
3742 p++;
3743 continue;
3744 }
3745 /* double dot */
3746 if (p + 2 == q && p[0] == '.' && p[1] == '.') {
3747 p += 2;
3748 if (STAILQ_FIRST(&on_cache) ==
3749 STAILQ_LAST(&on_cache, obj_list, entry)) {
3750 rc = ENOENT;
3751 goto done;
3752 }
3753 entry = STAILQ_FIRST(&on_cache);
3754 STAILQ_REMOVE_HEAD(&on_cache, entry);
3755 free(entry);
3756 objnum = (STAILQ_FIRST(&on_cache))->objnum;
3757 continue;
3758 }
3759 if (q - p + 1 > sizeof(element)) {
3760 rc = ENAMETOOLONG;
3761 goto done;
3762 }
3763 memcpy(element, p, q - p);
3764 element[q - p] = 0;
3765 p = q;
3766
3767 if ((rc = zfs_dnode_stat(spa, &dn, &sb)) != 0)
3768 goto done;
3769 if (!S_ISDIR(sb.st_mode)) {
3770 rc = ENOTDIR;
3771 goto done;
3772 }
3773
3774 rc = zap_lookup(spa, &dn, element, sizeof (objnum), 1, &objnum);
3775 if (rc)
3776 goto done;
3777 objnum = ZFS_DIRENT_OBJ(objnum);
3778
3779 if ((entry = malloc(sizeof(struct obj_list))) == NULL) {
3780 rc = ENOMEM;
3781 goto done;
3782 }
3783 entry->objnum = objnum;
3784 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
3785 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
3786 if (rc)
3787 goto done;
3788
3789 /*
3790 * Check for symlink.
3791 */
3792 rc = zfs_dnode_stat(spa, &dn, &sb);
3793 if (rc)
3794 goto done;
3795 if (S_ISLNK(sb.st_mode)) {
3796 if (symlinks_followed > 10) {
3797 rc = EMLINK;
3798 goto done;
3799 }
3800 symlinks_followed++;
3801
3802 /*
3803 * Read the link value and copy the tail of our
3804 * current path onto the end.
3805 */
3806 if (sb.st_size + strlen(p) + 1 > sizeof(path)) {
3807 rc = ENAMETOOLONG;
3808 goto done;
3809 }
3810 strcpy(&path[sb.st_size], p);
3811
3812 rc = zfs_dnode_readlink(spa, &dn, path, sb.st_size);
3813 if (rc != 0)
3814 goto done;
3815
3816 /*
3817 * Restart with the new path, starting either at
3818 * the root or at the parent depending whether or
3819 * not the link is relative.
3820 */
3821 p = path;
3822 if (*p == '/') {
3823 while (STAILQ_FIRST(&on_cache) !=
3824 STAILQ_LAST(&on_cache, obj_list, entry)) {
3825 entry = STAILQ_FIRST(&on_cache);
3826 STAILQ_REMOVE_HEAD(&on_cache, entry);
3827 free(entry);
3828 }
3829 } else {
3830 entry = STAILQ_FIRST(&on_cache);
3831 STAILQ_REMOVE_HEAD(&on_cache, entry);
3832 free(entry);
3833 }
3834 objnum = (STAILQ_FIRST(&on_cache))->objnum;
3835 }
3836 }
3837
3838 *dnode = dn;
3839 done:
3840 STAILQ_FOREACH_SAFE(entry, &on_cache, entry, tentry)
3841 free(entry);
3842 return (rc);
3843 }
3844
3845 /*
3846 * Return either a cached copy of the bootenv, or read each of the vdev children
3847 * looking for the bootenv. Cache what's found and return the results. Returns 0
3848 * when benvp is filled in, and some errno when not.
3849 */
3850 static int
zfs_get_bootenv_spa(spa_t * spa,nvlist_t ** benvp)3851 zfs_get_bootenv_spa(spa_t *spa, nvlist_t **benvp)
3852 {
3853 vdev_t *vd;
3854 nvlist_t *benv = NULL;
3855
3856 if (spa->spa_bootenv == NULL) {
3857 STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children,
3858 v_childlink) {
3859 benv = vdev_read_bootenv(vd);
3860
3861 if (benv != NULL)
3862 break;
3863 }
3864 spa->spa_bootenv = benv;
3865 }
3866 benv = spa->spa_bootenv;
3867
3868 if (benv == NULL)
3869 return (ENOENT);
3870
3871 *benvp = benv;
3872 return (0);
3873 }
3874
3875 /*
3876 * Store nvlist to pool label bootenv area. Also updates cached pointer in spa.
3877 */
3878 static int
zfs_set_bootenv_spa(spa_t * spa,nvlist_t * benv)3879 zfs_set_bootenv_spa(spa_t *spa, nvlist_t *benv)
3880 {
3881 vdev_t *vd;
3882
3883 STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children, v_childlink) {
3884 vdev_write_bootenv(vd, benv);
3885 }
3886
3887 spa->spa_bootenv = benv;
3888 return (0);
3889 }
3890
3891 /*
3892 * Get bootonce value by key. The bootonce <key, value> pair is removed from the
3893 * bootenv nvlist and the remaining nvlist is committed back to disk. This process
3894 * the bootonce flag since we've reached the point in the boot that we've 'used'
3895 * the BE. For chained boot scenarios, we may reach this point multiple times (but
3896 * only remove it and return 0 the first time).
3897 */
3898 static int
zfs_get_bootonce_spa(spa_t * spa,const char * key,char * buf,size_t size)3899 zfs_get_bootonce_spa(spa_t *spa, const char *key, char *buf, size_t size)
3900 {
3901 nvlist_t *benv;
3902 char *result = NULL;
3903 int result_size, rv;
3904
3905 if ((rv = zfs_get_bootenv_spa(spa, &benv)) != 0)
3906 return (rv);
3907
3908 if ((rv = nvlist_find(benv, key, DATA_TYPE_STRING, NULL,
3909 &result, &result_size)) == 0) {
3910 if (result_size == 0) {
3911 /* ignore empty string */
3912 rv = ENOENT;
3913 } else if (buf != NULL) {
3914 size = MIN((size_t)result_size + 1, size);
3915 strlcpy(buf, result, size);
3916 }
3917 (void)nvlist_remove(benv, key, DATA_TYPE_STRING);
3918 (void)zfs_set_bootenv_spa(spa, benv);
3919 }
3920
3921 return (rv);
3922 }
3923