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 #include <sys/cdefs.h>
28
29 /*
30 * Stand-alone ZFS file reader.
31 */
32
33 #include <stdbool.h>
34 #include <sys/endian.h>
35 #include <sys/stat.h>
36 #include <sys/stdint.h>
37 #include <sys/list.h>
38 #include <sys/zfs_bootenv.h>
39 #include <inttypes.h>
40
41 #include "zfsimpl.h"
42 #include "zfssubr.c"
43
44
45 struct zfsmount {
46 const spa_t *spa;
47 objset_phys_t objset;
48 uint64_t rootobj;
49 };
50
51 /*
52 * The indirect_child_t represents the vdev that we will read from, when we
53 * need to read all copies of the data (e.g. for scrub or reconstruction).
54 * For plain (non-mirror) top-level vdevs (i.e. is_vdev is not a mirror),
55 * ic_vdev is the same as is_vdev. However, for mirror top-level vdevs,
56 * ic_vdev is a child of the mirror.
57 */
58 typedef struct indirect_child {
59 void *ic_data;
60 vdev_t *ic_vdev;
61 } indirect_child_t;
62
63 /*
64 * The indirect_split_t represents one mapped segment of an i/o to the
65 * indirect vdev. For non-split (contiguously-mapped) blocks, there will be
66 * only one indirect_split_t, with is_split_offset==0 and is_size==io_size.
67 * For split blocks, there will be several of these.
68 */
69 typedef struct indirect_split {
70 list_node_t is_node; /* link on iv_splits */
71
72 /*
73 * is_split_offset is the offset into the i/o.
74 * This is the sum of the previous splits' is_size's.
75 */
76 uint64_t is_split_offset;
77
78 vdev_t *is_vdev; /* top-level vdev */
79 uint64_t is_target_offset; /* offset on is_vdev */
80 uint64_t is_size;
81 int is_children; /* number of entries in is_child[] */
82
83 /*
84 * is_good_child is the child that we are currently using to
85 * attempt reconstruction.
86 */
87 int is_good_child;
88
89 indirect_child_t is_child[1]; /* variable-length */
90 } indirect_split_t;
91
92 /*
93 * The indirect_vsd_t is associated with each i/o to the indirect vdev.
94 * It is the "Vdev-Specific Data" in the zio_t's io_vsd.
95 */
96 typedef struct indirect_vsd {
97 boolean_t iv_split_block;
98 boolean_t iv_reconstruct;
99
100 list_t iv_splits; /* list of indirect_split_t's */
101 } indirect_vsd_t;
102
103 /*
104 * List of all vdevs, chained through v_alllink.
105 */
106 static vdev_list_t zfs_vdevs;
107
108 /*
109 * List of ZFS features supported for read
110 */
111 static const char *features_for_read[] = {
112 "org.illumos:lz4_compress",
113 "com.delphix:hole_birth",
114 "com.delphix:extensible_dataset",
115 "com.delphix:embedded_data",
116 "org.open-zfs:large_blocks",
117 "org.illumos:sha512",
118 "org.illumos:skein",
119 "org.illumos:edonr",
120 "org.zfsonlinux:large_dnode",
121 "com.joyent:multi_vdev_crash_dump",
122 "com.delphix:spacemap_histogram",
123 "com.delphix:zpool_checkpoint",
124 "com.delphix:spacemap_v2",
125 "com.datto:encryption",
126 "com.datto:bookmark_v2",
127 "org.zfsonlinux:allocation_classes",
128 "com.datto:resilver_defer",
129 "com.delphix:device_removal",
130 "com.delphix:obsolete_counts",
131 NULL
132 };
133
134 /*
135 * List of all pools, chained through spa_link.
136 */
137 static spa_list_t zfs_pools;
138
139 static const dnode_phys_t *dnode_cache_obj;
140 static uint64_t dnode_cache_bn;
141 static char *dnode_cache_buf;
142
143 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
144 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
145 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
146 static int zap_lookup(const spa_t *spa, const dnode_phys_t *dnode,
147 const char *name, uint64_t integer_size, uint64_t num_integers,
148 void *value);
149 static int objset_get_dnode(const spa_t *, const objset_phys_t *, uint64_t,
150 dnode_phys_t *);
151 static int dnode_read(const spa_t *, const dnode_phys_t *, off_t, void *,
152 size_t);
153 static int vdev_indirect_read(vdev_t *, const blkptr_t *, void *, off_t,
154 size_t);
155 static int vdev_mirror_read(vdev_t *, const blkptr_t *, void *, off_t,
156 size_t);
157
158 static void
zfs_init(void)159 zfs_init(void)
160 {
161 STAILQ_INIT(&zfs_vdevs);
162 STAILQ_INIT(&zfs_pools);
163
164 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
165
166 zfs_init_crc();
167 }
168
169 static int
nvlist_check_features_for_read(nvlist_t * nvl)170 nvlist_check_features_for_read(nvlist_t *nvl)
171 {
172 nvlist_t *features = NULL;
173 nvs_data_t *data;
174 nvp_header_t *nvp;
175 nv_string_t *nvp_name;
176 int rc;
177
178 /*
179 * We may have all features disabled.
180 */
181 rc = nvlist_find(nvl, ZPOOL_CONFIG_FEATURES_FOR_READ,
182 DATA_TYPE_NVLIST, NULL, &features, NULL);
183 switch (rc) {
184 case 0:
185 break; /* Continue with checks */
186
187 case ENOENT:
188 return (0); /* All features are disabled */
189
190 default:
191 return (rc); /* Error while reading nvlist */
192 }
193
194 data = (nvs_data_t *)features->nv_data;
195 nvp = &data->nvl_pair; /* first pair in nvlist */
196
197 while (nvp->encoded_size != 0 && nvp->decoded_size != 0) {
198 int i, found;
199
200 nvp_name = (nv_string_t *)((uintptr_t)nvp + sizeof (*nvp));
201 found = 0;
202
203 for (i = 0; features_for_read[i] != NULL; i++) {
204 if (memcmp(nvp_name->nv_data, features_for_read[i],
205 nvp_name->nv_size) == 0) {
206 found = 1;
207 break;
208 }
209 }
210
211 if (!found) {
212 printf("ZFS: unsupported feature: %.*s\n",
213 nvp_name->nv_size, nvp_name->nv_data);
214 rc = EIO;
215 }
216 nvp = (nvp_header_t *)((uint8_t *)nvp + nvp->encoded_size);
217 }
218 nvlist_destroy(features);
219
220 return (rc);
221 }
222
223 static int
vdev_read_phys(vdev_t * vdev,const blkptr_t * bp,void * buf,off_t offset,size_t size)224 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
225 off_t offset, size_t size)
226 {
227 size_t psize;
228 int rc;
229
230 if (vdev->v_phys_read == NULL)
231 return (ENOTSUP);
232
233 if (bp) {
234 psize = BP_GET_PSIZE(bp);
235 } else {
236 psize = size;
237 }
238
239 rc = vdev->v_phys_read(vdev, vdev->v_priv, offset, buf, psize);
240 if (rc == 0) {
241 if (bp != NULL)
242 rc = zio_checksum_verify(vdev->v_spa, bp, buf);
243 }
244
245 return (rc);
246 }
247
248 static int
vdev_write_phys(vdev_t * vdev,void * buf,off_t offset,size_t size)249 vdev_write_phys(vdev_t *vdev, void *buf, off_t offset, size_t size)
250 {
251 if (vdev->v_phys_write == NULL)
252 return (ENOTSUP);
253
254 return (vdev->v_phys_write(vdev, offset, buf, size));
255 }
256
257 typedef struct remap_segment {
258 vdev_t *rs_vd;
259 uint64_t rs_offset;
260 uint64_t rs_asize;
261 uint64_t rs_split_offset;
262 list_node_t rs_node;
263 } remap_segment_t;
264
265 static remap_segment_t *
rs_alloc(vdev_t * vd,uint64_t offset,uint64_t asize,uint64_t split_offset)266 rs_alloc(vdev_t *vd, uint64_t offset, uint64_t asize, uint64_t split_offset)
267 {
268 remap_segment_t *rs = malloc(sizeof (remap_segment_t));
269
270 if (rs != NULL) {
271 rs->rs_vd = vd;
272 rs->rs_offset = offset;
273 rs->rs_asize = asize;
274 rs->rs_split_offset = split_offset;
275 }
276
277 return (rs);
278 }
279
280 vdev_indirect_mapping_t *
vdev_indirect_mapping_open(spa_t * spa,objset_phys_t * os,uint64_t mapping_object)281 vdev_indirect_mapping_open(spa_t *spa, objset_phys_t *os,
282 uint64_t mapping_object)
283 {
284 vdev_indirect_mapping_t *vim;
285 vdev_indirect_mapping_phys_t *vim_phys;
286 int rc;
287
288 vim = calloc(1, sizeof (*vim));
289 if (vim == NULL)
290 return (NULL);
291
292 vim->vim_dn = calloc(1, sizeof (*vim->vim_dn));
293 if (vim->vim_dn == NULL) {
294 free(vim);
295 return (NULL);
296 }
297
298 rc = objset_get_dnode(spa, os, mapping_object, vim->vim_dn);
299 if (rc != 0) {
300 free(vim->vim_dn);
301 free(vim);
302 return (NULL);
303 }
304
305 vim->vim_spa = spa;
306 vim->vim_phys = malloc(sizeof (*vim->vim_phys));
307 if (vim->vim_phys == NULL) {
308 free(vim->vim_dn);
309 free(vim);
310 return (NULL);
311 }
312
313 vim_phys = (vdev_indirect_mapping_phys_t *)DN_BONUS(vim->vim_dn);
314 *vim->vim_phys = *vim_phys;
315
316 vim->vim_objset = os;
317 vim->vim_object = mapping_object;
318 vim->vim_entries = NULL;
319
320 vim->vim_havecounts =
321 (vim->vim_dn->dn_bonuslen > VDEV_INDIRECT_MAPPING_SIZE_V0);
322
323 return (vim);
324 }
325
326 /*
327 * Compare an offset with an indirect mapping entry; there are three
328 * possible scenarios:
329 *
330 * 1. The offset is "less than" the mapping entry; meaning the
331 * offset is less than the source offset of the mapping entry. In
332 * this case, there is no overlap between the offset and the
333 * mapping entry and -1 will be returned.
334 *
335 * 2. The offset is "greater than" the mapping entry; meaning the
336 * offset is greater than the mapping entry's source offset plus
337 * the entry's size. In this case, there is no overlap between
338 * the offset and the mapping entry and 1 will be returned.
339 *
340 * NOTE: If the offset is actually equal to the entry's offset
341 * plus size, this is considered to be "greater" than the entry,
342 * and this case applies (i.e. 1 will be returned). Thus, the
343 * entry's "range" can be considered to be inclusive at its
344 * start, but exclusive at its end: e.g. [src, src + size).
345 *
346 * 3. The last case to consider is if the offset actually falls
347 * within the mapping entry's range. If this is the case, the
348 * offset is considered to be "equal to" the mapping entry and
349 * 0 will be returned.
350 *
351 * NOTE: If the offset is equal to the entry's source offset,
352 * this case applies and 0 will be returned. If the offset is
353 * equal to the entry's source plus its size, this case does
354 * *not* apply (see "NOTE" above for scenario 2), and 1 will be
355 * returned.
356 */
357 static int
dva_mapping_overlap_compare(const void * v_key,const void * v_array_elem)358 dva_mapping_overlap_compare(const void *v_key, const void *v_array_elem)
359 {
360 const uint64_t *key = v_key;
361 const vdev_indirect_mapping_entry_phys_t *array_elem =
362 v_array_elem;
363 uint64_t src_offset = DVA_MAPPING_GET_SRC_OFFSET(array_elem);
364
365 if (*key < src_offset) {
366 return (-1);
367 } else if (*key < src_offset + DVA_GET_ASIZE(&array_elem->vimep_dst)) {
368 return (0);
369 } else {
370 return (1);
371 }
372 }
373
374 /*
375 * Return array entry.
376 */
377 static vdev_indirect_mapping_entry_phys_t *
vdev_indirect_mapping_entry(vdev_indirect_mapping_t * vim,uint64_t index)378 vdev_indirect_mapping_entry(vdev_indirect_mapping_t *vim, uint64_t index)
379 {
380 uint64_t size;
381 off_t offset = 0;
382 int rc;
383
384 if (vim->vim_phys->vimp_num_entries == 0)
385 return (NULL);
386
387 if (vim->vim_entries == NULL) {
388 uint64_t bsize;
389
390 bsize = vim->vim_dn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
391 size = vim->vim_phys->vimp_num_entries *
392 sizeof (*vim->vim_entries);
393 if (size > bsize) {
394 size = bsize / sizeof (*vim->vim_entries);
395 size *= sizeof (*vim->vim_entries);
396 }
397 vim->vim_entries = malloc(size);
398 if (vim->vim_entries == NULL)
399 return (NULL);
400 vim->vim_num_entries = size / sizeof (*vim->vim_entries);
401 offset = index * sizeof (*vim->vim_entries);
402 }
403
404 /* We have data in vim_entries */
405 if (offset == 0) {
406 if (index >= vim->vim_entry_offset &&
407 index <= vim->vim_entry_offset + vim->vim_num_entries) {
408 index -= vim->vim_entry_offset;
409 return (&vim->vim_entries[index]);
410 }
411 offset = index * sizeof (*vim->vim_entries);
412 }
413
414 vim->vim_entry_offset = index;
415 size = vim->vim_num_entries * sizeof (*vim->vim_entries);
416 rc = dnode_read(vim->vim_spa, vim->vim_dn, offset, vim->vim_entries,
417 size);
418 if (rc != 0) {
419 /* Read error, invalidate vim_entries. */
420 free(vim->vim_entries);
421 vim->vim_entries = NULL;
422 return (NULL);
423 }
424 index -= vim->vim_entry_offset;
425 return (&vim->vim_entries[index]);
426 }
427
428 /*
429 * Returns the mapping entry for the given offset.
430 *
431 * It's possible that the given offset will not be in the mapping table
432 * (i.e. no mapping entries contain this offset), in which case, the
433 * return value value depends on the "next_if_missing" parameter.
434 *
435 * If the offset is not found in the table and "next_if_missing" is
436 * B_FALSE, then NULL will always be returned. The behavior is intended
437 * to allow consumers to get the entry corresponding to the offset
438 * parameter, iff the offset overlaps with an entry in the table.
439 *
440 * If the offset is not found in the table and "next_if_missing" is
441 * B_TRUE, then the entry nearest to the given offset will be returned,
442 * such that the entry's source offset is greater than the offset
443 * passed in (i.e. the "next" mapping entry in the table is returned, if
444 * the offset is missing from the table). If there are no entries whose
445 * source offset is greater than the passed in offset, NULL is returned.
446 */
447 static vdev_indirect_mapping_entry_phys_t *
vdev_indirect_mapping_entry_for_offset(vdev_indirect_mapping_t * vim,uint64_t offset)448 vdev_indirect_mapping_entry_for_offset(vdev_indirect_mapping_t *vim,
449 uint64_t offset)
450 {
451 ASSERT(vim->vim_phys->vimp_num_entries > 0);
452
453 vdev_indirect_mapping_entry_phys_t *entry;
454
455 uint64_t last = vim->vim_phys->vimp_num_entries - 1;
456 uint64_t base = 0;
457
458 /*
459 * We don't define these inside of the while loop because we use
460 * their value in the case that offset isn't in the mapping.
461 */
462 uint64_t mid;
463 int result;
464
465 while (last >= base) {
466 mid = base + ((last - base) >> 1);
467
468 entry = vdev_indirect_mapping_entry(vim, mid);
469 if (entry == NULL)
470 break;
471 result = dva_mapping_overlap_compare(&offset, entry);
472
473 if (result == 0) {
474 break;
475 } else if (result < 0) {
476 last = mid - 1;
477 } else {
478 base = mid + 1;
479 }
480 }
481 return (entry);
482 }
483
484 /*
485 * Given an indirect vdev and an extent on that vdev, it duplicates the
486 * physical entries of the indirect mapping that correspond to the extent
487 * to a new array and returns a pointer to it. In addition, copied_entries
488 * is populated with the number of mapping entries that were duplicated.
489 *
490 * Finally, since we are doing an allocation, it is up to the caller to
491 * free the array allocated in this function.
492 */
493 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)494 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *vd, uint64_t offset,
495 uint64_t asize, uint64_t *copied_entries)
496 {
497 vdev_indirect_mapping_entry_phys_t *duplicate_mappings = NULL;
498 vdev_indirect_mapping_t *vim = vd->v_mapping;
499 uint64_t entries = 0;
500
501 vdev_indirect_mapping_entry_phys_t *first_mapping =
502 vdev_indirect_mapping_entry_for_offset(vim, offset);
503 ASSERT3P(first_mapping, !=, NULL);
504
505 vdev_indirect_mapping_entry_phys_t *m = first_mapping;
506 while (asize > 0) {
507 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
508 uint64_t inner_offset = offset - DVA_MAPPING_GET_SRC_OFFSET(m);
509 uint64_t inner_size = MIN(asize, size - inner_offset);
510
511 offset += inner_size;
512 asize -= inner_size;
513 entries++;
514 m++;
515 }
516
517 size_t copy_length = entries * sizeof (*first_mapping);
518 duplicate_mappings = malloc(copy_length);
519 if (duplicate_mappings != NULL)
520 bcopy(first_mapping, duplicate_mappings, copy_length);
521 else
522 entries = 0;
523
524 *copied_entries = entries;
525
526 return (duplicate_mappings);
527 }
528
529 static vdev_t *
vdev_lookup_top(spa_t * spa,uint64_t vdev)530 vdev_lookup_top(spa_t *spa, uint64_t vdev)
531 {
532 vdev_t *rvd;
533 vdev_list_t *vlist;
534
535 vlist = &spa->spa_root_vdev->v_children;
536 STAILQ_FOREACH(rvd, vlist, v_childlink)
537 if (rvd->v_id == vdev)
538 break;
539
540 return (rvd);
541 }
542
543 /*
544 * This is a callback for vdev_indirect_remap() which allocates an
545 * indirect_split_t for each split segment and adds it to iv_splits.
546 */
547 static void
vdev_indirect_gather_splits(uint64_t split_offset,vdev_t * vd,uint64_t offset,uint64_t size,void * arg)548 vdev_indirect_gather_splits(uint64_t split_offset, vdev_t *vd, uint64_t offset,
549 uint64_t size, void *arg)
550 {
551 int n = 1;
552 zio_t *zio = arg;
553 indirect_vsd_t *iv = zio->io_vsd;
554
555 if (vd->v_read == vdev_indirect_read)
556 return;
557
558 if (vd->v_read == vdev_mirror_read)
559 n = vd->v_nchildren;
560
561 indirect_split_t *is =
562 malloc(offsetof(indirect_split_t, is_child[n]));
563 if (is == NULL) {
564 zio->io_error = ENOMEM;
565 return;
566 }
567 bzero(is, offsetof(indirect_split_t, is_child[n]));
568
569 is->is_children = n;
570 is->is_size = size;
571 is->is_split_offset = split_offset;
572 is->is_target_offset = offset;
573 is->is_vdev = vd;
574
575 /*
576 * Note that we only consider multiple copies of the data for
577 * *mirror* vdevs. We don't for "replacing" or "spare" vdevs, even
578 * though they use the same ops as mirror, because there's only one
579 * "good" copy under the replacing/spare.
580 */
581 if (vd->v_read == vdev_mirror_read) {
582 int i = 0;
583 vdev_t *kid;
584
585 STAILQ_FOREACH(kid, &vd->v_children, v_childlink) {
586 is->is_child[i++].ic_vdev = kid;
587 }
588 } else {
589 is->is_child[0].ic_vdev = vd;
590 }
591
592 list_insert_tail(&iv->iv_splits, is);
593 }
594
595 static void
vdev_indirect_remap(vdev_t * vd,uint64_t offset,uint64_t asize,void * arg)596 vdev_indirect_remap(vdev_t *vd, uint64_t offset, uint64_t asize, void *arg)
597 {
598 list_t stack;
599 spa_t *spa = vd->v_spa;
600 zio_t *zio = arg;
601 remap_segment_t *rs;
602
603 list_create(&stack, sizeof (remap_segment_t),
604 offsetof(remap_segment_t, rs_node));
605
606 rs = rs_alloc(vd, offset, asize, 0);
607 if (rs == NULL) {
608 printf("vdev_indirect_remap: out of memory.\n");
609 zio->io_error = ENOMEM;
610 }
611 for (; rs != NULL; rs = list_remove_head(&stack)) {
612 vdev_t *v = rs->rs_vd;
613 uint64_t num_entries = 0;
614 /* vdev_indirect_mapping_t *vim = v->v_mapping; */
615 vdev_indirect_mapping_entry_phys_t *mapping =
616 vdev_indirect_mapping_duplicate_adjacent_entries(v,
617 rs->rs_offset, rs->rs_asize, &num_entries);
618
619 if (num_entries == 0)
620 zio->io_error = ENOMEM;
621
622 for (uint64_t i = 0; i < num_entries; i++) {
623 vdev_indirect_mapping_entry_phys_t *m = &mapping[i];
624 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
625 uint64_t dst_offset = DVA_GET_OFFSET(&m->vimep_dst);
626 uint64_t dst_vdev = DVA_GET_VDEV(&m->vimep_dst);
627 uint64_t inner_offset = rs->rs_offset -
628 DVA_MAPPING_GET_SRC_OFFSET(m);
629 uint64_t inner_size =
630 MIN(rs->rs_asize, size - inner_offset);
631 vdev_t *dst_v = vdev_lookup_top(spa, dst_vdev);
632
633 if (dst_v->v_read == vdev_indirect_read) {
634 remap_segment_t *o;
635
636 o = rs_alloc(dst_v, dst_offset + inner_offset,
637 inner_size, rs->rs_split_offset);
638 if (o == NULL) {
639 printf("vdev_indirect_remap: "
640 "out of memory.\n");
641 zio->io_error = ENOMEM;
642 break;
643 }
644
645 list_insert_head(&stack, o);
646 }
647 vdev_indirect_gather_splits(rs->rs_split_offset, dst_v,
648 dst_offset + inner_offset,
649 inner_size, arg);
650
651 /*
652 * vdev_indirect_gather_splits can have memory
653 * allocation error, we can not recover from it.
654 */
655 if (zio->io_error != 0)
656 break;
657 rs->rs_offset += inner_size;
658 rs->rs_asize -= inner_size;
659 rs->rs_split_offset += inner_size;
660 }
661
662 free(mapping);
663 free(rs);
664 if (zio->io_error != 0)
665 break;
666 }
667
668 list_destroy(&stack);
669 }
670
671 static void
vdev_indirect_map_free(zio_t * zio)672 vdev_indirect_map_free(zio_t *zio)
673 {
674 indirect_vsd_t *iv = zio->io_vsd;
675 indirect_split_t *is;
676
677 while ((is = list_head(&iv->iv_splits)) != NULL) {
678 for (int c = 0; c < is->is_children; c++) {
679 indirect_child_t *ic = &is->is_child[c];
680 free(ic->ic_data);
681 }
682 list_remove(&iv->iv_splits, is);
683 free(is);
684 }
685 free(iv);
686 }
687
688 static int
vdev_indirect_read(vdev_t * vdev,const blkptr_t * bp,void * buf,off_t offset,size_t bytes)689 vdev_indirect_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
690 off_t offset, size_t bytes)
691 {
692 zio_t zio;
693 spa_t *spa = vdev->v_spa;
694 indirect_vsd_t *iv;
695 indirect_split_t *first;
696 int rc = EIO;
697
698 iv = calloc(1, sizeof (*iv));
699 if (iv == NULL)
700 return (ENOMEM);
701
702 list_create(&iv->iv_splits,
703 sizeof (indirect_split_t), offsetof(indirect_split_t, is_node));
704
705 bzero(&zio, sizeof (zio));
706 zio.io_spa = spa;
707 zio.io_bp = (blkptr_t *)bp;
708 zio.io_data = buf;
709 zio.io_size = bytes;
710 zio.io_offset = offset;
711 zio.io_vd = vdev;
712 zio.io_vsd = iv;
713
714 if (vdev->v_mapping == NULL) {
715 vdev_indirect_config_t *vic;
716
717 vic = &vdev->vdev_indirect_config;
718 vdev->v_mapping = vdev_indirect_mapping_open(spa,
719 &spa->spa_mos, vic->vic_mapping_object);
720 }
721
722 vdev_indirect_remap(vdev, offset, bytes, &zio);
723 if (zio.io_error != 0)
724 return (zio.io_error);
725
726 first = list_head(&iv->iv_splits);
727 if (first->is_size == zio.io_size) {
728 /*
729 * This is not a split block; we are pointing to the entire
730 * data, which will checksum the same as the original data.
731 * Pass the BP down so that the child i/o can verify the
732 * checksum, and try a different location if available
733 * (e.g. on a mirror).
734 *
735 * While this special case could be handled the same as the
736 * general (split block) case, doing it this way ensures
737 * that the vast majority of blocks on indirect vdevs
738 * (which are not split) are handled identically to blocks
739 * on non-indirect vdevs. This allows us to be less strict
740 * about performance in the general (but rare) case.
741 */
742 rc = first->is_vdev->v_read(first->is_vdev, zio.io_bp,
743 zio.io_data, first->is_target_offset, bytes);
744 } else {
745 iv->iv_split_block = B_TRUE;
746 /*
747 * Read one copy of each split segment, from the
748 * top-level vdev. Since we don't know the
749 * checksum of each split individually, the child
750 * zio can't ensure that we get the right data.
751 * E.g. if it's a mirror, it will just read from a
752 * random (healthy) leaf vdev. We have to verify
753 * the checksum in vdev_indirect_io_done().
754 */
755 for (indirect_split_t *is = list_head(&iv->iv_splits);
756 is != NULL; is = list_next(&iv->iv_splits, is)) {
757 char *ptr = zio.io_data;
758
759 rc = is->is_vdev->v_read(is->is_vdev, zio.io_bp,
760 ptr + is->is_split_offset, is->is_target_offset,
761 is->is_size);
762 }
763 if (zio_checksum_verify(spa, zio.io_bp, zio.io_data))
764 rc = ECKSUM;
765 else
766 rc = 0;
767 }
768
769 vdev_indirect_map_free(&zio);
770 if (rc == 0)
771 rc = zio.io_error;
772
773 return (rc);
774 }
775
776 static int
vdev_disk_read(vdev_t * vdev,const blkptr_t * bp,void * buf,off_t offset,size_t bytes)777 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
778 off_t offset, size_t bytes)
779 {
780
781 return (vdev_read_phys(vdev, bp, buf,
782 offset + VDEV_LABEL_START_SIZE, bytes));
783 }
784
785 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)786 vdev_missing_read(vdev_t *vdev __unused, const blkptr_t *bp __unused,
787 void *buf __unused, off_t offset __unused, size_t bytes __unused)
788 {
789
790 return (ENOTSUP);
791 }
792
793 static int
vdev_mirror_read(vdev_t * vdev,const blkptr_t * bp,void * buf,off_t offset,size_t bytes)794 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
795 off_t offset, size_t bytes)
796 {
797 vdev_t *kid;
798 int rc;
799
800 rc = EIO;
801 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
802 if (kid->v_state != VDEV_STATE_HEALTHY)
803 continue;
804 rc = kid->v_read(kid, bp, buf, offset, bytes);
805 if (!rc)
806 return (0);
807 }
808
809 return (rc);
810 }
811
812 static int
vdev_replacing_read(vdev_t * vdev,const blkptr_t * bp,void * buf,off_t offset,size_t bytes)813 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
814 off_t offset, size_t bytes)
815 {
816 vdev_t *kid;
817
818 /*
819 * Here we should have two kids:
820 * First one which is the one we are replacing and we can trust
821 * only this one to have valid data, but it might not be present.
822 * Second one is that one we are replacing with. It is most likely
823 * healthy, but we can't trust it has needed data, so we won't use it.
824 */
825 kid = STAILQ_FIRST(&vdev->v_children);
826 if (kid == NULL)
827 return (EIO);
828 if (kid->v_state != VDEV_STATE_HEALTHY)
829 return (EIO);
830 return (kid->v_read(kid, bp, buf, offset, bytes));
831 }
832
833 static vdev_t *
vdev_find(uint64_t guid)834 vdev_find(uint64_t guid)
835 {
836 vdev_t *vdev;
837
838 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
839 if (vdev->v_guid == guid)
840 return (vdev);
841
842 return (0);
843 }
844
845 static vdev_t *
vdev_create(uint64_t guid,vdev_read_t * vdev_read)846 vdev_create(uint64_t guid, vdev_read_t *vdev_read)
847 {
848 vdev_t *vdev;
849 vdev_indirect_config_t *vic;
850
851 vdev = calloc(1, sizeof (vdev_t));
852 if (vdev != NULL) {
853 STAILQ_INIT(&vdev->v_children);
854 vdev->v_guid = guid;
855 vdev->v_read = vdev_read;
856
857 /*
858 * root vdev has no read function, we use this fact to
859 * skip setting up data we do not need for root vdev.
860 * We only point root vdev from spa.
861 */
862 if (vdev_read != NULL) {
863 vic = &vdev->vdev_indirect_config;
864 vic->vic_prev_indirect_vdev = UINT64_MAX;
865 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
866 }
867 }
868
869 return (vdev);
870 }
871
872 static void
vdev_set_initial_state(vdev_t * vdev,const nvlist_t * nvlist)873 vdev_set_initial_state(vdev_t *vdev, const nvlist_t *nvlist)
874 {
875 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
876 uint64_t is_log;
877
878 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
879 is_log = 0;
880 (void) nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL,
881 &is_offline, NULL);
882 (void) nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL,
883 &is_removed, NULL);
884 (void) nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, NULL,
885 &is_faulted, NULL);
886 (void) nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64,
887 NULL, &is_degraded, NULL);
888 (void) nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64,
889 NULL, &isnt_present, NULL);
890 (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, NULL,
891 &is_log, NULL);
892
893 if (is_offline != 0)
894 vdev->v_state = VDEV_STATE_OFFLINE;
895 else if (is_removed != 0)
896 vdev->v_state = VDEV_STATE_REMOVED;
897 else if (is_faulted != 0)
898 vdev->v_state = VDEV_STATE_FAULTED;
899 else if (is_degraded != 0)
900 vdev->v_state = VDEV_STATE_DEGRADED;
901 else if (isnt_present != 0)
902 vdev->v_state = VDEV_STATE_CANT_OPEN;
903
904 vdev->v_islog = is_log != 0;
905 }
906
907 static int
vdev_init(uint64_t guid,const nvlist_t * nvlist,vdev_t ** vdevp)908 vdev_init(uint64_t guid, const nvlist_t *nvlist, vdev_t **vdevp)
909 {
910 uint64_t id, ashift, asize, nparity;
911 const char *path;
912 const char *type;
913 int len, pathlen;
914 char *name;
915 vdev_t *vdev;
916
917 if (nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id,
918 NULL) ||
919 nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
920 NULL, &type, &len)) {
921 return (ENOENT);
922 }
923
924 if (memcmp(type, VDEV_TYPE_MIRROR, len) != 0 &&
925 memcmp(type, VDEV_TYPE_DISK, len) != 0 &&
926 #ifdef ZFS_TEST
927 memcmp(type, VDEV_TYPE_FILE, len) != 0 &&
928 #endif
929 memcmp(type, VDEV_TYPE_RAIDZ, len) != 0 &&
930 memcmp(type, VDEV_TYPE_INDIRECT, len) != 0 &&
931 memcmp(type, VDEV_TYPE_REPLACING, len) != 0 &&
932 memcmp(type, VDEV_TYPE_HOLE, len) != 0) {
933 printf("ZFS: can only boot from disk, mirror, raidz1, "
934 "raidz2 and raidz3 vdevs, got: %.*s\n", len, type);
935 return (EIO);
936 }
937
938 if (memcmp(type, VDEV_TYPE_MIRROR, len) == 0)
939 vdev = vdev_create(guid, vdev_mirror_read);
940 else if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0)
941 vdev = vdev_create(guid, vdev_raidz_read);
942 else if (memcmp(type, VDEV_TYPE_REPLACING, len) == 0)
943 vdev = vdev_create(guid, vdev_replacing_read);
944 else if (memcmp(type, VDEV_TYPE_INDIRECT, len) == 0) {
945 vdev_indirect_config_t *vic;
946
947 vdev = vdev_create(guid, vdev_indirect_read);
948 if (vdev != NULL) {
949 vdev->v_state = VDEV_STATE_HEALTHY;
950 vic = &vdev->vdev_indirect_config;
951
952 nvlist_find(nvlist,
953 ZPOOL_CONFIG_INDIRECT_OBJECT,
954 DATA_TYPE_UINT64,
955 NULL, &vic->vic_mapping_object, NULL);
956 nvlist_find(nvlist,
957 ZPOOL_CONFIG_INDIRECT_BIRTHS,
958 DATA_TYPE_UINT64,
959 NULL, &vic->vic_births_object, NULL);
960 nvlist_find(nvlist,
961 ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
962 DATA_TYPE_UINT64,
963 NULL, &vic->vic_prev_indirect_vdev, NULL);
964 }
965 } else if (memcmp(type, VDEV_TYPE_HOLE, len) == 0) {
966 vdev = vdev_create(guid, vdev_missing_read);
967 } else {
968 vdev = vdev_create(guid, vdev_disk_read);
969 }
970
971 if (vdev == NULL)
972 return (ENOMEM);
973
974 vdev_set_initial_state(vdev, nvlist);
975 vdev->v_id = id;
976 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
977 DATA_TYPE_UINT64, NULL, &ashift, NULL) == 0)
978 vdev->v_ashift = ashift;
979
980 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASIZE,
981 DATA_TYPE_UINT64, NULL, &asize, NULL) == 0) {
982 vdev->v_psize = asize +
983 VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
984 }
985
986 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
987 DATA_TYPE_UINT64, NULL, &nparity, NULL) == 0)
988 vdev->v_nparity = nparity;
989
990 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
991 DATA_TYPE_STRING, NULL, &path, &pathlen) == 0) {
992 char prefix[] = "/dev/dsk/";
993
994 len = strlen(prefix);
995 if (len < pathlen && memcmp(path, prefix, len) == 0) {
996 path += len;
997 pathlen -= len;
998 }
999 name = malloc(pathlen + 1);
1000 if (name != NULL) {
1001 bcopy(path, name, pathlen);
1002 name[pathlen] = '\0';
1003 }
1004 vdev->v_name = name;
1005 vdev->v_phys_path = NULL;
1006 vdev->v_devid = NULL;
1007 if (nvlist_find(nvlist, ZPOOL_CONFIG_PHYS_PATH,
1008 DATA_TYPE_STRING, NULL, &path, &pathlen) == 0) {
1009 name = malloc(pathlen + 1);
1010 if (name != NULL) {
1011 bcopy(path, name, pathlen);
1012 name[pathlen] = '\0';
1013 vdev->v_phys_path = name;
1014 }
1015 }
1016 if (nvlist_find(nvlist, ZPOOL_CONFIG_DEVID,
1017 DATA_TYPE_STRING, NULL, &path, &pathlen) == 0) {
1018 name = malloc(pathlen + 1);
1019 if (name != NULL) {
1020 bcopy(path, name, pathlen);
1021 name[pathlen] = '\0';
1022 vdev->v_devid = name;
1023 }
1024 }
1025 } else {
1026 name = NULL;
1027 if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0) {
1028 if (vdev->v_nparity < 1 ||
1029 vdev->v_nparity > 3) {
1030 printf("ZFS: invalid raidz parity: %d\n",
1031 vdev->v_nparity);
1032 return (EIO);
1033 }
1034 (void) asprintf(&name, "%.*s%d-%" PRIu64, len, type,
1035 vdev->v_nparity, id);
1036 } else {
1037 (void) asprintf(&name, "%.*s-%" PRIu64, len, type, id);
1038 }
1039 vdev->v_name = name;
1040 }
1041 *vdevp = vdev;
1042 return (0);
1043 }
1044
1045 /*
1046 * Find slot for vdev. We return either NULL to signal to use
1047 * STAILQ_INSERT_HEAD, or we return link element to be used with
1048 * STAILQ_INSERT_AFTER.
1049 */
1050 static vdev_t *
vdev_find_previous(vdev_t * top_vdev,vdev_t * vdev)1051 vdev_find_previous(vdev_t *top_vdev, vdev_t *vdev)
1052 {
1053 vdev_t *v, *previous;
1054
1055 if (STAILQ_EMPTY(&top_vdev->v_children))
1056 return (NULL);
1057
1058 previous = NULL;
1059 STAILQ_FOREACH(v, &top_vdev->v_children, v_childlink) {
1060 if (v->v_id > vdev->v_id)
1061 return (previous);
1062
1063 if (v->v_id == vdev->v_id)
1064 return (v);
1065
1066 if (v->v_id < vdev->v_id)
1067 previous = v;
1068 }
1069 return (previous);
1070 }
1071
1072 static size_t
vdev_child_count(vdev_t * vdev)1073 vdev_child_count(vdev_t *vdev)
1074 {
1075 vdev_t *v;
1076 size_t count;
1077
1078 count = 0;
1079 STAILQ_FOREACH(v, &vdev->v_children, v_childlink) {
1080 count++;
1081 }
1082 return (count);
1083 }
1084
1085 /*
1086 * Insert vdev into top_vdev children list. List is ordered by v_id.
1087 */
1088 static void
vdev_insert(vdev_t * top_vdev,vdev_t * vdev)1089 vdev_insert(vdev_t *top_vdev, vdev_t *vdev)
1090 {
1091 vdev_t *previous;
1092 size_t count;
1093
1094 /*
1095 * The top level vdev can appear in random order, depending how
1096 * the firmware is presenting the disk devices.
1097 * However, we will insert vdev to create list ordered by v_id,
1098 * so we can use either STAILQ_INSERT_HEAD or STAILQ_INSERT_AFTER
1099 * as STAILQ does not have insert before.
1100 */
1101 previous = vdev_find_previous(top_vdev, vdev);
1102
1103 if (previous == NULL) {
1104 STAILQ_INSERT_HEAD(&top_vdev->v_children, vdev, v_childlink);
1105 } else if (previous->v_id == vdev->v_id) {
1106 /*
1107 * This vdev was configured from label config,
1108 * do not insert duplicate.
1109 */
1110 return;
1111 } else {
1112 STAILQ_INSERT_AFTER(&top_vdev->v_children, previous, vdev,
1113 v_childlink);
1114 }
1115
1116 count = vdev_child_count(top_vdev);
1117 if (top_vdev->v_nchildren < count)
1118 top_vdev->v_nchildren = count;
1119 }
1120
1121 static int
vdev_from_nvlist(spa_t * spa,uint64_t top_guid,const nvlist_t * nvlist)1122 vdev_from_nvlist(spa_t *spa, uint64_t top_guid, const nvlist_t *nvlist)
1123 {
1124 vdev_t *top_vdev, *vdev;
1125 nvlist_t **kids = NULL;
1126 int rc, nkids;
1127
1128 /* Get top vdev. */
1129 top_vdev = vdev_find(top_guid);
1130 if (top_vdev == NULL) {
1131 rc = vdev_init(top_guid, nvlist, &top_vdev);
1132 if (rc != 0)
1133 return (rc);
1134 top_vdev->v_spa = spa;
1135 top_vdev->v_top = top_vdev;
1136 vdev_insert(spa->spa_root_vdev, top_vdev);
1137 }
1138
1139 /* Add children if there are any. */
1140 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1141 &nkids, &kids, NULL);
1142 if (rc == 0) {
1143 for (int i = 0; i < nkids; i++) {
1144 uint64_t guid;
1145
1146 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID,
1147 DATA_TYPE_UINT64, NULL, &guid, NULL);
1148 if (rc != 0)
1149 goto done;
1150
1151 rc = vdev_init(guid, kids[i], &vdev);
1152 if (rc != 0)
1153 goto done;
1154
1155 vdev->v_spa = spa;
1156 vdev->v_top = top_vdev;
1157 vdev_insert(top_vdev, vdev);
1158 }
1159 } else {
1160 /*
1161 * When there are no children, nvlist_find() does return
1162 * error, reset it because leaf devices have no children.
1163 */
1164 rc = 0;
1165 }
1166 done:
1167 if (kids != NULL) {
1168 for (int i = 0; i < nkids; i++)
1169 nvlist_destroy(kids[i]);
1170 free(kids);
1171 }
1172
1173 return (rc);
1174 }
1175
1176 static int
vdev_init_from_label(spa_t * spa,const nvlist_t * nvlist)1177 vdev_init_from_label(spa_t *spa, const nvlist_t *nvlist)
1178 {
1179 uint64_t pool_guid, top_guid;
1180 nvlist_t *vdevs;
1181 int rc;
1182
1183 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1184 NULL, &pool_guid, NULL) ||
1185 nvlist_find(nvlist, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64,
1186 NULL, &top_guid, NULL) ||
1187 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1188 NULL, &vdevs, NULL)) {
1189 printf("ZFS: can't find vdev details\n");
1190 return (ENOENT);
1191 }
1192
1193 rc = vdev_from_nvlist(spa, top_guid, vdevs);
1194 nvlist_destroy(vdevs);
1195 return (rc);
1196 }
1197
1198 static void
vdev_set_state(vdev_t * vdev)1199 vdev_set_state(vdev_t *vdev)
1200 {
1201 vdev_t *kid;
1202 int good_kids;
1203 int bad_kids;
1204
1205 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1206 vdev_set_state(kid);
1207 }
1208
1209 /*
1210 * A mirror or raidz is healthy if all its kids are healthy. A
1211 * mirror is degraded if any of its kids is healthy; a raidz
1212 * is degraded if at most nparity kids are offline.
1213 */
1214 if (STAILQ_FIRST(&vdev->v_children)) {
1215 good_kids = 0;
1216 bad_kids = 0;
1217 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1218 if (kid->v_state == VDEV_STATE_HEALTHY)
1219 good_kids++;
1220 else
1221 bad_kids++;
1222 }
1223 if (bad_kids == 0) {
1224 vdev->v_state = VDEV_STATE_HEALTHY;
1225 } else {
1226 if (vdev->v_read == vdev_mirror_read) {
1227 if (good_kids) {
1228 vdev->v_state = VDEV_STATE_DEGRADED;
1229 } else {
1230 vdev->v_state = VDEV_STATE_OFFLINE;
1231 }
1232 } else if (vdev->v_read == vdev_raidz_read) {
1233 if (bad_kids > vdev->v_nparity) {
1234 vdev->v_state = VDEV_STATE_OFFLINE;
1235 } else {
1236 vdev->v_state = VDEV_STATE_DEGRADED;
1237 }
1238 }
1239 }
1240 }
1241 }
1242
1243 static int
vdev_update_from_nvlist(uint64_t top_guid,const nvlist_t * nvlist)1244 vdev_update_from_nvlist(uint64_t top_guid, const nvlist_t *nvlist)
1245 {
1246 vdev_t *vdev;
1247 nvlist_t **kids = NULL;
1248 int rc, nkids;
1249
1250 /* Update top vdev. */
1251 vdev = vdev_find(top_guid);
1252 if (vdev != NULL)
1253 vdev_set_initial_state(vdev, nvlist);
1254
1255 /* Update children if there are any. */
1256 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1257 &nkids, &kids, NULL);
1258 if (rc == 0) {
1259 for (int i = 0; i < nkids; i++) {
1260 uint64_t guid;
1261
1262 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID,
1263 DATA_TYPE_UINT64, NULL, &guid, NULL);
1264 if (rc != 0)
1265 break;
1266
1267 vdev = vdev_find(guid);
1268 if (vdev != NULL)
1269 vdev_set_initial_state(vdev, kids[i]);
1270 }
1271 } else {
1272 rc = 0;
1273 }
1274 if (kids != NULL) {
1275 for (int i = 0; i < nkids; i++)
1276 nvlist_destroy(kids[i]);
1277 free(kids);
1278 }
1279
1280 return (rc);
1281 }
1282
1283 static int
vdev_init_from_nvlist(spa_t * spa,const nvlist_t * nvlist)1284 vdev_init_from_nvlist(spa_t *spa, const nvlist_t *nvlist)
1285 {
1286 uint64_t pool_guid, vdev_children;
1287 nvlist_t *vdevs = NULL, **kids = NULL;
1288 int rc, nkids;
1289
1290 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1291 NULL, &pool_guid, NULL) ||
1292 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_CHILDREN, DATA_TYPE_UINT64,
1293 NULL, &vdev_children, NULL) ||
1294 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1295 NULL, &vdevs, NULL)) {
1296 printf("ZFS: can't find vdev details\n");
1297 return (ENOENT);
1298 }
1299
1300 /* Wrong guid?! */
1301 if (spa->spa_guid != pool_guid) {
1302 nvlist_destroy(vdevs);
1303 return (EINVAL);
1304 }
1305
1306 spa->spa_root_vdev->v_nchildren = vdev_children;
1307
1308 rc = nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1309 &nkids, &kids, NULL);
1310 nvlist_destroy(vdevs);
1311
1312 /*
1313 * MOS config has at least one child for root vdev.
1314 */
1315 if (rc != 0)
1316 return (rc);
1317
1318 for (int i = 0; i < nkids; i++) {
1319 uint64_t guid;
1320 vdev_t *vdev;
1321
1322 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
1323 NULL, &guid, NULL);
1324 if (rc != 0)
1325 break;
1326 vdev = vdev_find(guid);
1327 /*
1328 * Top level vdev is missing, create it.
1329 */
1330 if (vdev == NULL)
1331 rc = vdev_from_nvlist(spa, guid, kids[i]);
1332 else
1333 rc = vdev_update_from_nvlist(guid, kids[i]);
1334 if (rc != 0)
1335 break;
1336 }
1337 if (kids != NULL) {
1338 for (int i = 0; i < nkids; i++)
1339 nvlist_destroy(kids[i]);
1340 free(kids);
1341 }
1342
1343 /*
1344 * Re-evaluate top-level vdev state.
1345 */
1346 vdev_set_state(spa->spa_root_vdev);
1347
1348 return (rc);
1349 }
1350
1351 static spa_t *
spa_find_by_guid(uint64_t guid)1352 spa_find_by_guid(uint64_t guid)
1353 {
1354 spa_t *spa;
1355
1356 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1357 if (spa->spa_guid == guid)
1358 return (spa);
1359
1360 return (NULL);
1361 }
1362
1363 static spa_t *
spa_find_by_name(const char * name)1364 spa_find_by_name(const char *name)
1365 {
1366 spa_t *spa;
1367
1368 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1369 if (strcmp(spa->spa_name, name) == 0)
1370 return (spa);
1371
1372 return (NULL);
1373 }
1374
1375 static spa_t *
spa_find_by_dev(struct zfs_devdesc * dev)1376 spa_find_by_dev(struct zfs_devdesc *dev)
1377 {
1378
1379 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1380 return (NULL);
1381
1382 if (dev->pool_guid == 0)
1383 return (STAILQ_FIRST(&zfs_pools));
1384
1385 return (spa_find_by_guid(dev->pool_guid));
1386 }
1387
1388 static spa_t *
spa_create(uint64_t guid,const char * name)1389 spa_create(uint64_t guid, const char *name)
1390 {
1391 spa_t *spa;
1392
1393 if ((spa = calloc(1, sizeof (spa_t))) == NULL)
1394 return (NULL);
1395 if ((spa->spa_name = strdup(name)) == NULL) {
1396 free(spa);
1397 return (NULL);
1398 }
1399 spa->spa_guid = guid;
1400 spa->spa_root_vdev = vdev_create(guid, NULL);
1401 if (spa->spa_root_vdev == NULL) {
1402 free(spa->spa_name);
1403 free(spa);
1404 return (NULL);
1405 }
1406 spa->spa_root_vdev->v_name = strdup("root");
1407 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
1408
1409 return (spa);
1410 }
1411
1412 static const char *
state_name(vdev_state_t state)1413 state_name(vdev_state_t state)
1414 {
1415 static const char *names[] = {
1416 "UNKNOWN",
1417 "CLOSED",
1418 "OFFLINE",
1419 "REMOVED",
1420 "CANT_OPEN",
1421 "FAULTED",
1422 "DEGRADED",
1423 "ONLINE"
1424 };
1425 return (names[state]);
1426 }
1427
1428 static int
pager_printf(const char * fmt,...)1429 pager_printf(const char *fmt, ...)
1430 {
1431 char line[80];
1432 va_list args;
1433
1434 va_start(args, fmt);
1435 vsnprintf(line, sizeof (line), fmt, args);
1436 va_end(args);
1437 return (pager_output(line));
1438 }
1439
1440 #define STATUS_FORMAT " %s %s\n"
1441
1442 static int
print_state(int indent,const char * name,vdev_state_t state)1443 print_state(int indent, const char *name, vdev_state_t state)
1444 {
1445 int i;
1446 char buf[512];
1447
1448 buf[0] = 0;
1449 for (i = 0; i < indent; i++)
1450 strcat(buf, " ");
1451 strcat(buf, name);
1452 return (pager_printf(STATUS_FORMAT, buf, state_name(state)));
1453 }
1454
1455 static int
vdev_status(vdev_t * vdev,int indent)1456 vdev_status(vdev_t *vdev, int indent)
1457 {
1458 vdev_t *kid;
1459 int ret;
1460
1461 if (vdev->v_islog) {
1462 (void) pager_output(" logs\n");
1463 indent++;
1464 }
1465
1466 ret = print_state(indent, vdev->v_name, vdev->v_state);
1467 if (ret != 0)
1468 return (ret);
1469
1470 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1471 ret = vdev_status(kid, indent + 1);
1472 if (ret != 0)
1473 return (ret);
1474 }
1475 return (ret);
1476 }
1477
1478 static int
spa_status(spa_t * spa)1479 spa_status(spa_t *spa)
1480 {
1481 static char bootfs[ZFS_MAXNAMELEN];
1482 uint64_t rootid;
1483 vdev_list_t *vlist;
1484 vdev_t *vdev;
1485 int good_kids, bad_kids, degraded_kids, ret;
1486 vdev_state_t state;
1487
1488 ret = pager_printf(" pool: %s\n", spa->spa_name);
1489 if (ret != 0)
1490 return (ret);
1491
1492 if (zfs_get_root(spa, &rootid) == 0 &&
1493 zfs_rlookup(spa, rootid, bootfs) == 0) {
1494 if (bootfs[0] == '\0')
1495 ret = pager_printf("bootfs: %s\n", spa->spa_name);
1496 else
1497 ret = pager_printf("bootfs: %s/%s\n", spa->spa_name,
1498 bootfs);
1499 if (ret != 0)
1500 return (ret);
1501 }
1502 ret = pager_printf("config:\n\n");
1503 if (ret != 0)
1504 return (ret);
1505 ret = pager_printf(STATUS_FORMAT, "NAME", "STATE");
1506 if (ret != 0)
1507 return (ret);
1508
1509 good_kids = 0;
1510 degraded_kids = 0;
1511 bad_kids = 0;
1512 vlist = &spa->spa_root_vdev->v_children;
1513 STAILQ_FOREACH(vdev, vlist, v_childlink) {
1514 if (vdev->v_state == VDEV_STATE_HEALTHY)
1515 good_kids++;
1516 else if (vdev->v_state == VDEV_STATE_DEGRADED)
1517 degraded_kids++;
1518 else
1519 bad_kids++;
1520 }
1521
1522 state = VDEV_STATE_CLOSED;
1523 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
1524 state = VDEV_STATE_HEALTHY;
1525 else if ((good_kids + degraded_kids) > 0)
1526 state = VDEV_STATE_DEGRADED;
1527
1528 ret = print_state(0, spa->spa_name, state);
1529 if (ret != 0)
1530 return (ret);
1531
1532 STAILQ_FOREACH(vdev, vlist, v_childlink) {
1533 ret = vdev_status(vdev, 1);
1534 if (ret != 0)
1535 return (ret);
1536 }
1537 return (ret);
1538 }
1539
1540 int
spa_all_status(void)1541 spa_all_status(void)
1542 {
1543 spa_t *spa;
1544 int first = 1, ret = 0;
1545
1546 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
1547 if (!first) {
1548 ret = pager_printf("\n");
1549 if (ret != 0)
1550 return (ret);
1551 }
1552 first = 0;
1553 ret = spa_status(spa);
1554 if (ret != 0)
1555 return (ret);
1556 }
1557 return (ret);
1558 }
1559
1560 uint64_t
vdev_label_offset(uint64_t psize,int l,uint64_t offset)1561 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
1562 {
1563 uint64_t label_offset;
1564
1565 if (l < VDEV_LABELS / 2)
1566 label_offset = 0;
1567 else
1568 label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t);
1569
1570 return (offset + l * sizeof (vdev_label_t) + label_offset);
1571 }
1572
1573 static int
vdev_uberblock_compare(const uberblock_t * ub1,const uberblock_t * ub2)1574 vdev_uberblock_compare(const uberblock_t *ub1, const uberblock_t *ub2)
1575 {
1576 unsigned int seq1 = 0;
1577 unsigned int seq2 = 0;
1578 int cmp = AVL_CMP(ub1->ub_txg, ub2->ub_txg);
1579
1580 if (cmp != 0)
1581 return (cmp);
1582
1583 cmp = AVL_CMP(ub1->ub_timestamp, ub2->ub_timestamp);
1584 if (cmp != 0)
1585 return (cmp);
1586
1587 if (MMP_VALID(ub1) && MMP_SEQ_VALID(ub1))
1588 seq1 = MMP_SEQ(ub1);
1589
1590 if (MMP_VALID(ub2) && MMP_SEQ_VALID(ub2))
1591 seq2 = MMP_SEQ(ub2);
1592
1593 return (AVL_CMP(seq1, seq2));
1594 }
1595
1596 static int
uberblock_verify(uberblock_t * ub)1597 uberblock_verify(uberblock_t *ub)
1598 {
1599 if (ub->ub_magic == BSWAP_64((uint64_t)UBERBLOCK_MAGIC)) {
1600 byteswap_uint64_array(ub, sizeof (uberblock_t));
1601 }
1602
1603 if (ub->ub_magic != UBERBLOCK_MAGIC ||
1604 !SPA_VERSION_IS_SUPPORTED(ub->ub_version))
1605 return (EINVAL);
1606
1607 return (0);
1608 }
1609
1610 static int
vdev_label_read(vdev_t * vd,int l,void * buf,uint64_t offset,size_t size)1611 vdev_label_read(vdev_t *vd, int l, void *buf, uint64_t offset,
1612 size_t size)
1613 {
1614 blkptr_t bp;
1615 off_t off;
1616
1617 off = vdev_label_offset(vd->v_psize, l, offset);
1618
1619 BP_ZERO(&bp);
1620 BP_SET_LSIZE(&bp, size);
1621 BP_SET_PSIZE(&bp, size);
1622 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1623 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1624 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
1625 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1626
1627 return (vdev_read_phys(vd, &bp, buf, off, size));
1628 }
1629
1630 /*
1631 * We do need to be sure we write to correct location.
1632 * Our vdev label does consist of 4 fields:
1633 * pad1 (8k), reserved.
1634 * bootenv (8k), checksummed, previously reserved, may contain garbage.
1635 * vdev_phys (112k), checksummed
1636 * uberblock ring (128k), checksummed.
1637 *
1638 * Since bootenv area may contain garbage, we can not reliably read it, as
1639 * we can get checksum errors.
1640 * Next best thing is vdev_phys - it is just after bootenv. It still may
1641 * be corrupted, but in such case we will miss this one write.
1642 */
1643 static int
vdev_label_write_validate(vdev_t * vd,int l,uint64_t offset)1644 vdev_label_write_validate(vdev_t *vd, int l, uint64_t offset)
1645 {
1646 uint64_t off, o_phys;
1647 void *buf;
1648 size_t size = VDEV_PHYS_SIZE;
1649 int rc;
1650
1651 o_phys = offsetof(vdev_label_t, vl_vdev_phys);
1652 off = vdev_label_offset(vd->v_psize, l, o_phys);
1653
1654 /* off should be 8K from bootenv */
1655 if (vdev_label_offset(vd->v_psize, l, offset) + VDEV_PAD_SIZE != off)
1656 return (EINVAL);
1657
1658 buf = malloc(size);
1659 if (buf == NULL)
1660 return (ENOMEM);
1661
1662 /* Read vdev_phys */
1663 rc = vdev_label_read(vd, l, buf, o_phys, size);
1664 free(buf);
1665 return (rc);
1666 }
1667
1668 static int
vdev_label_write(vdev_t * vd,int l,vdev_boot_envblock_t * be,uint64_t offset)1669 vdev_label_write(vdev_t *vd, int l, vdev_boot_envblock_t *be, uint64_t offset)
1670 {
1671 zio_checksum_info_t *ci;
1672 zio_cksum_t cksum;
1673 off_t off;
1674 size_t size = VDEV_PAD_SIZE;
1675 int rc;
1676
1677 if (vd->v_phys_write == NULL)
1678 return (ENOTSUP);
1679
1680 off = vdev_label_offset(vd->v_psize, l, offset);
1681
1682 rc = vdev_label_write_validate(vd, l, offset);
1683 if (rc != 0) {
1684 return (rc);
1685 }
1686
1687 ci = &zio_checksum_table[ZIO_CHECKSUM_LABEL];
1688 be->vbe_zbt.zec_magic = ZEC_MAGIC;
1689 zio_checksum_label_verifier(&be->vbe_zbt.zec_cksum, off);
1690 ci->ci_func[0](be, size, NULL, &cksum);
1691 be->vbe_zbt.zec_cksum = cksum;
1692
1693 return (vdev_write_phys(vd, be, off, size));
1694 }
1695
1696 static int
vdev_write_bootenv_impl(vdev_t * vdev,vdev_boot_envblock_t * be)1697 vdev_write_bootenv_impl(vdev_t *vdev, vdev_boot_envblock_t *be)
1698 {
1699 vdev_t *kid;
1700 int rv = 0, rc;
1701
1702 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1703 if (kid->v_state != VDEV_STATE_HEALTHY)
1704 continue;
1705 rc = vdev_write_bootenv_impl(kid, be);
1706 if (rv == 0)
1707 rv = rc;
1708 }
1709
1710 /*
1711 * Non-leaf vdevs do not have v_phys_write.
1712 */
1713 if (vdev->v_phys_write == NULL)
1714 return (rv);
1715
1716 for (int l = 0; l < VDEV_LABELS; l++) {
1717 rc = vdev_label_write(vdev, l, be,
1718 offsetof(vdev_label_t, vl_be));
1719 if (rc != 0) {
1720 printf("failed to write bootenv to %s label %d: %d\n",
1721 vdev->v_name ? vdev->v_name : "unknown", l, rc);
1722 rv = rc;
1723 }
1724 }
1725 return (rv);
1726 }
1727
1728 int
vdev_write_bootenv(vdev_t * vdev,nvlist_t * nvl)1729 vdev_write_bootenv(vdev_t *vdev, nvlist_t *nvl)
1730 {
1731 vdev_boot_envblock_t *be;
1732 nvlist_t nv, *nvp;
1733 uint64_t version;
1734 int rv;
1735
1736 if (nvl->nv_size > sizeof (be->vbe_bootenv))
1737 return (E2BIG);
1738
1739 version = VB_RAW;
1740 nvp = vdev_read_bootenv(vdev);
1741 if (nvp != NULL) {
1742 nvlist_find(nvp, BOOTENV_VERSION, DATA_TYPE_UINT64, NULL,
1743 &version, NULL);
1744 nvlist_destroy(nvp);
1745 }
1746
1747 be = calloc(1, sizeof (*be));
1748 if (be == NULL)
1749 return (ENOMEM);
1750
1751 be->vbe_version = version;
1752 switch (version) {
1753 case VB_RAW:
1754 /*
1755 * If there is no envmap, we will just wipe bootenv.
1756 */
1757 nvlist_find(nvl, GRUB_ENVMAP, DATA_TYPE_STRING, NULL,
1758 be->vbe_bootenv, NULL);
1759 rv = 0;
1760 break;
1761
1762 case VB_NVLIST:
1763 nv.nv_header = nvl->nv_header;
1764 nv.nv_asize = nvl->nv_asize;
1765 nv.nv_size = nvl->nv_size;
1766
1767 bcopy(&nv.nv_header, be->vbe_bootenv, sizeof (nv.nv_header));
1768 nv.nv_data = (uint8_t *)be->vbe_bootenv + sizeof (nvs_header_t);
1769 bcopy(nvl->nv_data, nv.nv_data, nv.nv_size);
1770 rv = nvlist_export(&nv);
1771 break;
1772
1773 default:
1774 rv = EINVAL;
1775 break;
1776 }
1777
1778 if (rv == 0) {
1779 be->vbe_version = htobe64(be->vbe_version);
1780 rv = vdev_write_bootenv_impl(vdev, be);
1781 }
1782 free(be);
1783 return (rv);
1784 }
1785
1786 /*
1787 * Read the bootenv area from pool label, return the nvlist from it.
1788 * We return from first successful read.
1789 */
1790 nvlist_t *
vdev_read_bootenv(vdev_t * vdev)1791 vdev_read_bootenv(vdev_t *vdev)
1792 {
1793 vdev_t *kid;
1794 nvlist_t *benv;
1795 vdev_boot_envblock_t *be;
1796 char *command;
1797 bool ok;
1798 int rv;
1799
1800 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1801 if (kid->v_state != VDEV_STATE_HEALTHY)
1802 continue;
1803
1804 benv = vdev_read_bootenv(kid);
1805 if (benv != NULL)
1806 return (benv);
1807 }
1808
1809 be = malloc(sizeof (*be));
1810 if (be == NULL)
1811 return (NULL);
1812
1813 rv = 0;
1814 for (int l = 0; l < VDEV_LABELS; l++) {
1815 rv = vdev_label_read(vdev, l, be,
1816 offsetof(vdev_label_t, vl_be),
1817 sizeof (*be));
1818 if (rv == 0)
1819 break;
1820 }
1821 if (rv != 0) {
1822 free(be);
1823 return (NULL);
1824 }
1825
1826 be->vbe_version = be64toh(be->vbe_version);
1827 switch (be->vbe_version) {
1828 case VB_RAW:
1829 /*
1830 * if we have textual data in vbe_bootenv, create nvlist
1831 * with key "envmap".
1832 */
1833 benv = nvlist_create(NV_UNIQUE_NAME);
1834 if (benv != NULL) {
1835 if (*be->vbe_bootenv == '\0') {
1836 nvlist_add_uint64(benv, BOOTENV_VERSION,
1837 VB_NVLIST);
1838 break;
1839 }
1840 nvlist_add_uint64(benv, BOOTENV_VERSION, VB_RAW);
1841 be->vbe_bootenv[sizeof (be->vbe_bootenv) - 1] = '\0';
1842 nvlist_add_string(benv, GRUB_ENVMAP, be->vbe_bootenv);
1843 }
1844 break;
1845
1846 case VB_NVLIST:
1847 benv = nvlist_import(be->vbe_bootenv, sizeof (be->vbe_bootenv));
1848 break;
1849
1850 default:
1851 command = (char *)be;
1852 ok = false;
1853
1854 /* Check for legacy zfsbootcfg command string */
1855 for (int i = 0; command[i] != '\0'; i++) {
1856 if (iscntrl(command[i])) {
1857 ok = false;
1858 break;
1859 } else {
1860 ok = true;
1861 }
1862 }
1863 benv = nvlist_create(NV_UNIQUE_NAME);
1864 if (benv != NULL) {
1865 if (ok)
1866 nvlist_add_string(benv, FREEBSD_BOOTONCE,
1867 command);
1868 else
1869 nvlist_add_uint64(benv, BOOTENV_VERSION,
1870 VB_NVLIST);
1871 }
1872 break;
1873 }
1874 free(be);
1875 return (benv);
1876 }
1877
1878 static uint64_t
vdev_get_label_asize(nvlist_t * nvl)1879 vdev_get_label_asize(nvlist_t *nvl)
1880 {
1881 nvlist_t *vdevs;
1882 uint64_t asize;
1883 const char *type;
1884 int len;
1885
1886 asize = 0;
1887 /* Get vdev tree */
1888 if (nvlist_find(nvl, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1889 NULL, &vdevs, NULL) != 0)
1890 return (asize);
1891
1892 /*
1893 * Get vdev type. We will calculate asize for raidz, mirror and disk.
1894 * For raidz, the asize is raw size of all children.
1895 */
1896 if (nvlist_find(vdevs, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
1897 NULL, &type, &len) != 0)
1898 goto done;
1899
1900 if (memcmp(type, VDEV_TYPE_MIRROR, len) != 0 &&
1901 memcmp(type, VDEV_TYPE_DISK, len) != 0 &&
1902 memcmp(type, VDEV_TYPE_RAIDZ, len) != 0)
1903 goto done;
1904
1905 if (nvlist_find(vdevs, ZPOOL_CONFIG_ASIZE, DATA_TYPE_UINT64,
1906 NULL, &asize, NULL) != 0)
1907 goto done;
1908
1909 if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0) {
1910 nvlist_t **kids;
1911 int nkids;
1912
1913 if (nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN,
1914 DATA_TYPE_NVLIST_ARRAY, &nkids, &kids, NULL) != 0) {
1915 asize = 0;
1916 goto done;
1917 }
1918
1919 asize /= nkids;
1920 for (int i = 0; i < nkids; i++)
1921 nvlist_destroy(kids[i]);
1922 free(kids);
1923 }
1924
1925 asize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
1926 done:
1927 return (asize);
1928 }
1929
1930 static nvlist_t *
vdev_label_read_config(vdev_t * vd,uint64_t txg)1931 vdev_label_read_config(vdev_t *vd, uint64_t txg)
1932 {
1933 vdev_phys_t *label;
1934 uint64_t best_txg = 0;
1935 uint64_t label_txg = 0;
1936 uint64_t asize;
1937 nvlist_t *nvl = NULL, *tmp;
1938 int error;
1939
1940 label = malloc(sizeof (vdev_phys_t));
1941 if (label == NULL)
1942 return (NULL);
1943
1944 for (int l = 0; l < VDEV_LABELS; l++) {
1945 if (vdev_label_read(vd, l, label,
1946 offsetof(vdev_label_t, vl_vdev_phys),
1947 sizeof (vdev_phys_t)))
1948 continue;
1949
1950 tmp = nvlist_import(label->vp_nvlist,
1951 sizeof (label->vp_nvlist));
1952 if (tmp == NULL)
1953 continue;
1954
1955 error = nvlist_find(tmp, ZPOOL_CONFIG_POOL_TXG,
1956 DATA_TYPE_UINT64, NULL, &label_txg, NULL);
1957 if (error != 0 || label_txg == 0) {
1958 nvlist_destroy(nvl);
1959 nvl = tmp;
1960 goto done;
1961 }
1962
1963 if (label_txg <= txg && label_txg > best_txg) {
1964 best_txg = label_txg;
1965 nvlist_destroy(nvl);
1966 nvl = tmp;
1967 tmp = NULL;
1968
1969 /*
1970 * Use asize from pool config. We need this
1971 * because we can get bad value from BIOS.
1972 */
1973 asize = vdev_get_label_asize(nvl);
1974 if (asize != 0) {
1975 vd->v_psize = asize;
1976 }
1977 }
1978 nvlist_destroy(tmp);
1979 }
1980
1981 if (best_txg == 0) {
1982 nvlist_destroy(nvl);
1983 nvl = NULL;
1984 }
1985 done:
1986 free(label);
1987 return (nvl);
1988 }
1989
1990 static void
vdev_uberblock_load(vdev_t * vd,uberblock_t * ub)1991 vdev_uberblock_load(vdev_t *vd, uberblock_t *ub)
1992 {
1993 uberblock_t *buf;
1994
1995 buf = malloc(VDEV_UBERBLOCK_SIZE(vd));
1996 if (buf == NULL)
1997 return;
1998
1999 for (int l = 0; l < VDEV_LABELS; l++) {
2000 for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
2001 if (vdev_label_read(vd, l, buf,
2002 VDEV_UBERBLOCK_OFFSET(vd, n),
2003 VDEV_UBERBLOCK_SIZE(vd)))
2004 continue;
2005 if (uberblock_verify(buf) != 0)
2006 continue;
2007
2008 if (vdev_uberblock_compare(buf, ub) > 0)
2009 *ub = *buf;
2010 }
2011 }
2012 free(buf);
2013 }
2014
2015 static int
vdev_probe(vdev_phys_read_t * _read,vdev_phys_write_t * _write,void * priv,spa_t ** spap)2016 vdev_probe(vdev_phys_read_t *_read, vdev_phys_write_t *_write, void *priv,
2017 spa_t **spap)
2018 {
2019 vdev_t vtmp;
2020 spa_t *spa;
2021 vdev_t *vdev;
2022 nvlist_t *nvl;
2023 uint64_t val;
2024 uint64_t guid, vdev_children;
2025 uint64_t pool_txg, pool_guid;
2026 const char *pool_name;
2027 int rc, namelen;
2028
2029 /*
2030 * Load the vdev label and figure out which
2031 * uberblock is most current.
2032 */
2033 memset(&vtmp, 0, sizeof (vtmp));
2034 vtmp.v_phys_read = _read;
2035 vtmp.v_phys_write = _write;
2036 vtmp.v_priv = priv;
2037 vtmp.v_psize = P2ALIGN(ldi_get_size(priv),
2038 (uint64_t)sizeof (vdev_label_t));
2039
2040 /* Test for minimum device size. */
2041 if (vtmp.v_psize < SPA_MINDEVSIZE)
2042 return (EIO);
2043
2044 nvl = vdev_label_read_config(&vtmp, UINT64_MAX);
2045 if (nvl == NULL)
2046 return (EIO);
2047
2048 if (nvlist_find(nvl, ZPOOL_CONFIG_VERSION, DATA_TYPE_UINT64,
2049 NULL, &val, NULL) != 0) {
2050 nvlist_destroy(nvl);
2051 return (EIO);
2052 }
2053
2054 if (!SPA_VERSION_IS_SUPPORTED(val)) {
2055 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
2056 (unsigned)val, (unsigned)SPA_VERSION);
2057 nvlist_destroy(nvl);
2058 return (EIO);
2059 }
2060
2061 /* Check ZFS features for read */
2062 rc = nvlist_check_features_for_read(nvl);
2063 if (rc != 0) {
2064 nvlist_destroy(nvl);
2065 return (EIO);
2066 }
2067
2068 if (nvlist_find(nvl, ZPOOL_CONFIG_POOL_STATE, DATA_TYPE_UINT64,
2069 NULL, &val, NULL) != 0) {
2070 nvlist_destroy(nvl);
2071 return (EIO);
2072 }
2073
2074 if (val == POOL_STATE_DESTROYED) {
2075 /* We don't boot only from destroyed pools. */
2076 nvlist_destroy(nvl);
2077 return (EIO);
2078 }
2079
2080 if (nvlist_find(nvl, ZPOOL_CONFIG_POOL_TXG, DATA_TYPE_UINT64,
2081 NULL, &pool_txg, NULL) != 0 ||
2082 nvlist_find(nvl, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
2083 NULL, &pool_guid, NULL) != 0 ||
2084 nvlist_find(nvl, ZPOOL_CONFIG_POOL_NAME, DATA_TYPE_STRING,
2085 NULL, &pool_name, &namelen) != 0) {
2086 /*
2087 * Cache and spare devices end up here - just ignore
2088 * them.
2089 */
2090 nvlist_destroy(nvl);
2091 return (EIO);
2092 }
2093
2094 /*
2095 * Create the pool if this is the first time we've seen it.
2096 */
2097 spa = spa_find_by_guid(pool_guid);
2098 if (spa == NULL) {
2099 char *name;
2100
2101 nvlist_find(nvl, ZPOOL_CONFIG_VDEV_CHILDREN,
2102 DATA_TYPE_UINT64, NULL, &vdev_children, NULL);
2103 name = malloc(namelen + 1);
2104 if (name == NULL) {
2105 nvlist_destroy(nvl);
2106 return (ENOMEM);
2107 }
2108 bcopy(pool_name, name, namelen);
2109 name[namelen] = '\0';
2110 spa = spa_create(pool_guid, name);
2111 free(name);
2112 if (spa == NULL) {
2113 nvlist_destroy(nvl);
2114 return (ENOMEM);
2115 }
2116 spa->spa_root_vdev->v_nchildren = vdev_children;
2117 }
2118 if (pool_txg > spa->spa_txg)
2119 spa->spa_txg = pool_txg;
2120
2121 /*
2122 * Get the vdev tree and create our in-core copy of it.
2123 * If we already have a vdev with this guid, this must
2124 * be some kind of alias (overlapping slices, dangerously dedicated
2125 * disks etc).
2126 */
2127 if (nvlist_find(nvl, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
2128 NULL, &guid, NULL) != 0) {
2129 nvlist_destroy(nvl);
2130 return (EIO);
2131 }
2132 vdev = vdev_find(guid);
2133 /* Has this vdev already been inited? */
2134 if (vdev && vdev->v_phys_read) {
2135 nvlist_destroy(nvl);
2136 return (EIO);
2137 }
2138
2139 rc = vdev_init_from_label(spa, nvl);
2140 nvlist_destroy(nvl);
2141 if (rc != 0)
2142 return (rc);
2143
2144 /*
2145 * We should already have created an incomplete vdev for this
2146 * vdev. Find it and initialise it with our read proc.
2147 */
2148 vdev = vdev_find(guid);
2149 if (vdev != NULL) {
2150 vdev->v_phys_read = _read;
2151 vdev->v_phys_write = _write;
2152 vdev->v_priv = priv;
2153 vdev->v_psize = vtmp.v_psize;
2154 /*
2155 * If no other state is set, mark vdev healthy.
2156 */
2157 if (vdev->v_state == VDEV_STATE_UNKNOWN)
2158 vdev->v_state = VDEV_STATE_HEALTHY;
2159 } else {
2160 printf("ZFS: inconsistent nvlist contents\n");
2161 return (EIO);
2162 }
2163
2164 if (vdev->v_islog)
2165 spa->spa_with_log = vdev->v_islog;
2166
2167 /* Record boot vdev for spa. */
2168 if (spa->spa_boot_vdev == NULL)
2169 spa->spa_boot_vdev = vdev;
2170
2171 /*
2172 * Re-evaluate top-level vdev state.
2173 */
2174 vdev_set_state(vdev->v_top);
2175
2176 /*
2177 * Ok, we are happy with the pool so far. Lets find
2178 * the best uberblock and then we can actually access
2179 * the contents of the pool.
2180 */
2181 vdev_uberblock_load(vdev, &spa->spa_uberblock);
2182
2183 if (spap != NULL)
2184 *spap = spa;
2185 return (0);
2186 }
2187
2188 static int
ilog2(int n)2189 ilog2(int n)
2190 {
2191 int v;
2192
2193 for (v = 0; v < 32; v++)
2194 if (n == (1 << v))
2195 return (v);
2196 return (-1);
2197 }
2198
2199 static int
zio_read_gang(const spa_t * spa,const blkptr_t * bp,void * buf)2200 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
2201 {
2202 blkptr_t gbh_bp;
2203 zio_gbh_phys_t zio_gb;
2204 char *pbuf;
2205 int i;
2206
2207 /* Artificial BP for gang block header. */
2208 gbh_bp = *bp;
2209 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
2210 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
2211 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
2212 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
2213 for (i = 0; i < SPA_DVAS_PER_BP; i++)
2214 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
2215
2216 /* Read gang header block using the artificial BP. */
2217 if (zio_read(spa, &gbh_bp, &zio_gb))
2218 return (EIO);
2219
2220 pbuf = buf;
2221 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
2222 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
2223
2224 if (BP_IS_HOLE(gbp))
2225 continue;
2226 if (zio_read(spa, gbp, pbuf))
2227 return (EIO);
2228 pbuf += BP_GET_PSIZE(gbp);
2229 }
2230
2231 if (zio_checksum_verify(spa, bp, buf))
2232 return (EIO);
2233 return (0);
2234 }
2235
2236 static int
zio_read(const spa_t * spa,const blkptr_t * bp,void * buf)2237 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
2238 {
2239 int cpfunc = BP_GET_COMPRESS(bp);
2240 uint64_t align, size;
2241 void *pbuf;
2242 int i, error;
2243
2244 /*
2245 * Process data embedded in block pointer
2246 */
2247 if (BP_IS_EMBEDDED(bp)) {
2248 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
2249
2250 size = BPE_GET_PSIZE(bp);
2251 ASSERT(size <= BPE_PAYLOAD_SIZE);
2252
2253 if (cpfunc != ZIO_COMPRESS_OFF)
2254 pbuf = malloc(size);
2255 else
2256 pbuf = buf;
2257
2258 if (pbuf == NULL)
2259 return (ENOMEM);
2260
2261 decode_embedded_bp_compressed(bp, pbuf);
2262 error = 0;
2263
2264 if (cpfunc != ZIO_COMPRESS_OFF) {
2265 error = zio_decompress_data(cpfunc, pbuf,
2266 size, buf, BP_GET_LSIZE(bp));
2267 free(pbuf);
2268 }
2269 if (error != 0)
2270 printf("ZFS: i/o error - unable to decompress "
2271 "block pointer data, error %d\n", error);
2272 return (error);
2273 }
2274
2275 error = EIO;
2276
2277 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
2278 const dva_t *dva = &bp->blk_dva[i];
2279 vdev_t *vdev;
2280 vdev_list_t *vlist;
2281 uint64_t vdevid;
2282 off_t offset;
2283
2284 if (!dva->dva_word[0] && !dva->dva_word[1])
2285 continue;
2286
2287 vdevid = DVA_GET_VDEV(dva);
2288 offset = DVA_GET_OFFSET(dva);
2289 vlist = &spa->spa_root_vdev->v_children;
2290 STAILQ_FOREACH(vdev, vlist, v_childlink) {
2291 if (vdev->v_id == vdevid)
2292 break;
2293 }
2294 if (!vdev || !vdev->v_read)
2295 continue;
2296
2297 size = BP_GET_PSIZE(bp);
2298 if (vdev->v_read == vdev_raidz_read) {
2299 align = 1ULL << vdev->v_ashift;
2300 if (P2PHASE(size, align) != 0)
2301 size = P2ROUNDUP(size, align);
2302 }
2303 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
2304 pbuf = malloc(size);
2305 else
2306 pbuf = buf;
2307
2308 if (pbuf == NULL) {
2309 error = ENOMEM;
2310 break;
2311 }
2312
2313 if (DVA_GET_GANG(dva))
2314 error = zio_read_gang(spa, bp, pbuf);
2315 else
2316 error = vdev->v_read(vdev, bp, pbuf, offset, size);
2317 if (error == 0) {
2318 if (cpfunc != ZIO_COMPRESS_OFF)
2319 error = zio_decompress_data(cpfunc, pbuf,
2320 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
2321 else if (size != BP_GET_PSIZE(bp))
2322 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
2323 }
2324 if (buf != pbuf)
2325 free(pbuf);
2326 if (error == 0)
2327 break;
2328 }
2329 if (error != 0)
2330 printf("ZFS: i/o error - all block copies unavailable\n");
2331
2332 return (error);
2333 }
2334
2335 static int
dnode_read(const spa_t * spa,const dnode_phys_t * dnode,off_t offset,void * buf,size_t buflen)2336 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset,
2337 void *buf, size_t buflen)
2338 {
2339 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
2340 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2341 int nlevels = dnode->dn_nlevels;
2342 int i, rc;
2343
2344 if (bsize > SPA_MAXBLOCKSIZE) {
2345 printf("ZFS: I/O error - blocks larger than %llu are not "
2346 "supported\n", SPA_MAXBLOCKSIZE);
2347 return (EIO);
2348 }
2349
2350 /*
2351 * Note: bsize may not be a power of two here so we need to do an
2352 * actual divide rather than a bitshift.
2353 */
2354 while (buflen > 0) {
2355 uint64_t bn = offset / bsize;
2356 int boff = offset % bsize;
2357 int ibn;
2358 const blkptr_t *indbp;
2359 blkptr_t bp;
2360
2361 if (bn > dnode->dn_maxblkid) {
2362 printf("warning: zfs bug: bn %llx > dn_maxblkid %llx\n",
2363 (unsigned long long)bn,
2364 (unsigned long long)dnode->dn_maxblkid);
2365 /*
2366 * zfs bug, will not return error
2367 * return (EIO);
2368 */
2369 }
2370
2371 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
2372 goto cached;
2373
2374 indbp = dnode->dn_blkptr;
2375 for (i = 0; i < nlevels; i++) {
2376 /*
2377 * Copy the bp from the indirect array so that
2378 * we can re-use the scratch buffer for multi-level
2379 * objects.
2380 */
2381 ibn = bn >> ((nlevels - i - 1) * ibshift);
2382 ibn &= ((1 << ibshift) - 1);
2383 bp = indbp[ibn];
2384 if (BP_IS_HOLE(&bp)) {
2385 memset(dnode_cache_buf, 0, bsize);
2386 break;
2387 }
2388 rc = zio_read(spa, &bp, dnode_cache_buf);
2389 if (rc)
2390 return (rc);
2391 indbp = (const blkptr_t *) dnode_cache_buf;
2392 }
2393 dnode_cache_obj = dnode;
2394 dnode_cache_bn = bn;
2395 cached:
2396
2397 /*
2398 * The buffer contains our data block. Copy what we
2399 * need from it and loop.
2400 */
2401 i = bsize - boff;
2402 if (i > buflen) i = buflen;
2403 memcpy(buf, &dnode_cache_buf[boff], i);
2404 buf = ((char *)buf) + i;
2405 offset += i;
2406 buflen -= i;
2407 }
2408
2409 return (0);
2410 }
2411
2412 /*
2413 * Lookup a value in a microzap directory.
2414 */
2415 static int
mzap_lookup(const mzap_phys_t * mz,size_t size,const char * name,uint64_t * value)2416 mzap_lookup(const mzap_phys_t *mz, size_t size, const char *name,
2417 uint64_t *value)
2418 {
2419 const mzap_ent_phys_t *mze;
2420 int chunks, i;
2421
2422 /*
2423 * Microzap objects use exactly one block. Read the whole
2424 * thing.
2425 */
2426 chunks = size / MZAP_ENT_LEN - 1;
2427 for (i = 0; i < chunks; i++) {
2428 mze = &mz->mz_chunk[i];
2429 if (strcmp(mze->mze_name, name) == 0) {
2430 *value = mze->mze_value;
2431 return (0);
2432 }
2433 }
2434
2435 return (ENOENT);
2436 }
2437
2438 /*
2439 * Compare a name with a zap leaf entry. Return non-zero if the name
2440 * matches.
2441 */
2442 static int
fzap_name_equal(const zap_leaf_t * zl,const zap_leaf_chunk_t * zc,const char * name)2443 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2444 const char *name)
2445 {
2446 size_t namelen;
2447 const zap_leaf_chunk_t *nc;
2448 const char *p;
2449
2450 namelen = zc->l_entry.le_name_numints;
2451
2452 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2453 p = name;
2454 while (namelen > 0) {
2455 size_t len;
2456
2457 len = namelen;
2458 if (len > ZAP_LEAF_ARRAY_BYTES)
2459 len = ZAP_LEAF_ARRAY_BYTES;
2460 if (memcmp(p, nc->l_array.la_array, len))
2461 return (0);
2462 p += len;
2463 namelen -= len;
2464 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2465 }
2466
2467 return (1);
2468 }
2469
2470 /*
2471 * Extract a uint64_t value from a zap leaf entry.
2472 */
2473 static uint64_t
fzap_leaf_value(const zap_leaf_t * zl,const zap_leaf_chunk_t * zc)2474 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
2475 {
2476 const zap_leaf_chunk_t *vc;
2477 int i;
2478 uint64_t value;
2479 const uint8_t *p;
2480
2481 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
2482 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
2483 value = (value << 8) | p[i];
2484 }
2485
2486 return (value);
2487 }
2488
2489 static void
stv(int len,void * addr,uint64_t value)2490 stv(int len, void *addr, uint64_t value)
2491 {
2492 switch (len) {
2493 case 1:
2494 *(uint8_t *)addr = value;
2495 return;
2496 case 2:
2497 *(uint16_t *)addr = value;
2498 return;
2499 case 4:
2500 *(uint32_t *)addr = value;
2501 return;
2502 case 8:
2503 *(uint64_t *)addr = value;
2504 return;
2505 }
2506 }
2507
2508 /*
2509 * Extract a array from a zap leaf entry.
2510 */
2511 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)2512 fzap_leaf_array(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc,
2513 uint64_t integer_size, uint64_t num_integers, void *buf)
2514 {
2515 uint64_t array_int_len = zc->l_entry.le_value_intlen;
2516 uint64_t value = 0;
2517 uint64_t *u64 = buf;
2518 char *p = buf;
2519 int len = MIN(zc->l_entry.le_value_numints, num_integers);
2520 int chunk = zc->l_entry.le_value_chunk;
2521 int byten = 0;
2522
2523 if (integer_size == 8 && len == 1) {
2524 *u64 = fzap_leaf_value(zl, zc);
2525 return;
2526 }
2527
2528 while (len > 0) {
2529 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array;
2530 int i;
2531
2532 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(zl));
2533 for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) {
2534 value = (value << 8) | la->la_array[i];
2535 byten++;
2536 if (byten == array_int_len) {
2537 stv(integer_size, p, value);
2538 byten = 0;
2539 len--;
2540 if (len == 0)
2541 return;
2542 p += integer_size;
2543 }
2544 }
2545 chunk = la->la_next;
2546 }
2547 }
2548
2549 static int
fzap_check_size(uint64_t integer_size,uint64_t num_integers)2550 fzap_check_size(uint64_t integer_size, uint64_t num_integers)
2551 {
2552
2553 switch (integer_size) {
2554 case 1:
2555 case 2:
2556 case 4:
2557 case 8:
2558 break;
2559 default:
2560 return (EINVAL);
2561 }
2562
2563 if (integer_size * num_integers > ZAP_MAXVALUELEN)
2564 return (E2BIG);
2565
2566 return (0);
2567 }
2568
2569 static void
zap_leaf_free(zap_leaf_t * leaf)2570 zap_leaf_free(zap_leaf_t *leaf)
2571 {
2572 free(leaf->l_phys);
2573 free(leaf);
2574 }
2575
2576 static int
zap_get_leaf_byblk(fat_zap_t * zap,uint64_t blk,zap_leaf_t ** lp)2577 zap_get_leaf_byblk(fat_zap_t *zap, uint64_t blk, zap_leaf_t **lp)
2578 {
2579 int bs = FZAP_BLOCK_SHIFT(zap);
2580 int err;
2581
2582 *lp = malloc(sizeof (**lp));
2583 if (*lp == NULL)
2584 return (ENOMEM);
2585
2586 (*lp)->l_bs = bs;
2587 (*lp)->l_phys = malloc(1 << bs);
2588
2589 if ((*lp)->l_phys == NULL) {
2590 free(*lp);
2591 return (ENOMEM);
2592 }
2593 err = dnode_read(zap->zap_spa, zap->zap_dnode, blk << bs, (*lp)->l_phys,
2594 1 << bs);
2595 if (err != 0) {
2596 zap_leaf_free(*lp);
2597 }
2598 return (err);
2599 }
2600
2601 static int
zap_table_load(fat_zap_t * zap,zap_table_phys_t * tbl,uint64_t idx,uint64_t * valp)2602 zap_table_load(fat_zap_t *zap, zap_table_phys_t *tbl, uint64_t idx,
2603 uint64_t *valp)
2604 {
2605 int bs = FZAP_BLOCK_SHIFT(zap);
2606 uint64_t blk = idx >> (bs - 3);
2607 uint64_t off = idx & ((1 << (bs - 3)) - 1);
2608 uint64_t *buf;
2609 int rc;
2610
2611 buf = malloc(1 << zap->zap_block_shift);
2612 if (buf == NULL)
2613 return (ENOMEM);
2614 rc = dnode_read(zap->zap_spa, zap->zap_dnode, (tbl->zt_blk + blk) << bs,
2615 buf, 1 << zap->zap_block_shift);
2616 if (rc == 0)
2617 *valp = buf[off];
2618 free(buf);
2619 return (rc);
2620 }
2621
2622 static int
zap_idx_to_blk(fat_zap_t * zap,uint64_t idx,uint64_t * valp)2623 zap_idx_to_blk(fat_zap_t *zap, uint64_t idx, uint64_t *valp)
2624 {
2625 if (zap->zap_phys->zap_ptrtbl.zt_numblks == 0) {
2626 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
2627 return (0);
2628 } else {
2629 return (zap_table_load(zap, &zap->zap_phys->zap_ptrtbl,
2630 idx, valp));
2631 }
2632 }
2633
2634 #define ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n))))
2635 static int
zap_deref_leaf(fat_zap_t * zap,uint64_t h,zap_leaf_t ** lp)2636 zap_deref_leaf(fat_zap_t *zap, uint64_t h, zap_leaf_t **lp)
2637 {
2638 uint64_t idx, blk;
2639 int err;
2640
2641 idx = ZAP_HASH_IDX(h, zap->zap_phys->zap_ptrtbl.zt_shift);
2642 err = zap_idx_to_blk(zap, idx, &blk);
2643 if (err != 0)
2644 return (err);
2645 return (zap_get_leaf_byblk(zap, blk, lp));
2646 }
2647
2648 #define CHAIN_END 0xffff /* end of the chunk chain */
2649 #define LEAF_HASH(l, h) \
2650 ((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \
2651 ((h) >> \
2652 (64 - ZAP_LEAF_HASH_SHIFT(l) - (l)->l_phys->l_hdr.lh_prefix_len)))
2653 #define LEAF_HASH_ENTPTR(l, h) (&(l)->l_phys->l_hash[LEAF_HASH(l, h)])
2654
2655 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)2656 zap_leaf_lookup(zap_leaf_t *zl, uint64_t hash, const char *name,
2657 uint64_t integer_size, uint64_t num_integers, void *value)
2658 {
2659 int rc;
2660 uint16_t *chunkp;
2661 struct zap_leaf_entry *le;
2662
2663 /*
2664 * Make sure this chunk matches our hash.
2665 */
2666 if (zl->l_phys->l_hdr.lh_prefix_len > 0 &&
2667 zl->l_phys->l_hdr.lh_prefix !=
2668 hash >> (64 - zl->l_phys->l_hdr.lh_prefix_len))
2669 return (EIO);
2670
2671 rc = ENOENT;
2672 for (chunkp = LEAF_HASH_ENTPTR(zl, hash);
2673 *chunkp != CHAIN_END; chunkp = &le->le_next) {
2674 zap_leaf_chunk_t *zc;
2675 uint16_t chunk = *chunkp;
2676
2677 le = ZAP_LEAF_ENTRY(zl, chunk);
2678 if (le->le_hash != hash)
2679 continue;
2680 zc = &ZAP_LEAF_CHUNK(zl, chunk);
2681 if (fzap_name_equal(zl, zc, name)) {
2682 if (zc->l_entry.le_value_intlen > integer_size) {
2683 rc = EINVAL;
2684 } else {
2685 fzap_leaf_array(zl, zc, integer_size,
2686 num_integers, value);
2687 rc = 0;
2688 }
2689 break;
2690 }
2691 }
2692 return (rc);
2693 }
2694
2695 /*
2696 * Lookup a value in a fatzap directory.
2697 */
2698 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)2699 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2700 const char *name, uint64_t integer_size, uint64_t num_integers,
2701 void *value)
2702 {
2703 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2704 fat_zap_t z;
2705 zap_leaf_t *zl;
2706 uint64_t hash;
2707 int rc;
2708
2709 if (zh->zap_magic != ZAP_MAGIC)
2710 return (EIO);
2711
2712 if ((rc = fzap_check_size(integer_size, num_integers)) != 0)
2713 return (rc);
2714
2715 z.zap_block_shift = ilog2(bsize);
2716 z.zap_phys = zh;
2717 z.zap_spa = spa;
2718 z.zap_dnode = dnode;
2719
2720 hash = zap_hash(zh->zap_salt, name);
2721 rc = zap_deref_leaf(&z, hash, &zl);
2722 if (rc != 0)
2723 return (rc);
2724
2725 rc = zap_leaf_lookup(zl, hash, name, integer_size, num_integers, value);
2726
2727 zap_leaf_free(zl);
2728 return (rc);
2729 }
2730
2731 /*
2732 * Lookup a name in a zap object and return its value as a uint64_t.
2733 */
2734 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)2735 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name,
2736 uint64_t integer_size, uint64_t num_integers, void *value)
2737 {
2738 int rc;
2739 zap_phys_t *zap;
2740 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2741
2742 zap = malloc(size);
2743 if (zap == NULL)
2744 return (ENOMEM);
2745
2746 rc = dnode_read(spa, dnode, 0, zap, size);
2747 if (rc)
2748 goto done;
2749
2750 switch (zap->zap_block_type) {
2751 case ZBT_MICRO:
2752 rc = mzap_lookup((const mzap_phys_t *)zap, size, name, value);
2753 break;
2754 case ZBT_HEADER:
2755 rc = fzap_lookup(spa, dnode, zap, name, integer_size,
2756 num_integers, value);
2757 break;
2758 default:
2759 printf("ZFS: invalid zap_type=%" PRIx64 "\n",
2760 zap->zap_block_type);
2761 rc = EIO;
2762 }
2763 done:
2764 free(zap);
2765 return (rc);
2766 }
2767
2768 /*
2769 * List a microzap directory.
2770 */
2771 static int
mzap_list(const mzap_phys_t * mz,size_t size,int (* callback)(const char *,uint64_t))2772 mzap_list(const mzap_phys_t *mz, size_t size,
2773 int (*callback)(const char *, uint64_t))
2774 {
2775 const mzap_ent_phys_t *mze;
2776 int chunks, i, rc;
2777
2778 /*
2779 * Microzap objects use exactly one block. Read the whole
2780 * thing.
2781 */
2782 rc = 0;
2783 chunks = size / MZAP_ENT_LEN - 1;
2784 for (i = 0; i < chunks; i++) {
2785 mze = &mz->mz_chunk[i];
2786 if (mze->mze_name[0]) {
2787 rc = callback(mze->mze_name, mze->mze_value);
2788 if (rc != 0)
2789 break;
2790 }
2791 }
2792
2793 return (rc);
2794 }
2795
2796 /*
2797 * List a fatzap directory.
2798 */
2799 static int
fzap_list(const spa_t * spa,const dnode_phys_t * dnode,zap_phys_t * zh,int (* callback)(const char *,uint64_t))2800 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2801 int (*callback)(const char *, uint64_t))
2802 {
2803 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2804 fat_zap_t z;
2805 int i, j, rc;
2806
2807 if (zh->zap_magic != ZAP_MAGIC)
2808 return (EIO);
2809
2810 z.zap_block_shift = ilog2(bsize);
2811 z.zap_phys = zh;
2812
2813 /*
2814 * This assumes that the leaf blocks start at block 1. The
2815 * documentation isn't exactly clear on this.
2816 */
2817 zap_leaf_t zl;
2818 zl.l_bs = z.zap_block_shift;
2819 zl.l_phys = malloc(bsize);
2820 if (zl.l_phys == NULL)
2821 return (ENOMEM);
2822
2823 for (i = 0; i < zh->zap_num_leafs; i++) {
2824 off_t off = ((off_t)(i + 1)) << zl.l_bs;
2825 char name[256], *p;
2826 uint64_t value;
2827
2828 if (dnode_read(spa, dnode, off, zl.l_phys, bsize)) {
2829 free(zl.l_phys);
2830 return (EIO);
2831 }
2832
2833 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
2834 zap_leaf_chunk_t *zc, *nc;
2835 int namelen;
2836
2837 zc = &ZAP_LEAF_CHUNK(&zl, j);
2838 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
2839 continue;
2840 namelen = zc->l_entry.le_name_numints;
2841 if (namelen > sizeof (name))
2842 namelen = sizeof (name);
2843
2844 /*
2845 * Paste the name back together.
2846 */
2847 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
2848 p = name;
2849 while (namelen > 0) {
2850 int len;
2851 len = namelen;
2852 if (len > ZAP_LEAF_ARRAY_BYTES)
2853 len = ZAP_LEAF_ARRAY_BYTES;
2854 memcpy(p, nc->l_array.la_array, len);
2855 p += len;
2856 namelen -= len;
2857 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
2858 }
2859
2860 /*
2861 * Assume the first eight bytes of the value are
2862 * a uint64_t.
2863 */
2864 value = fzap_leaf_value(&zl, zc);
2865
2866 /* printf("%s 0x%jx\n", name, (uintmax_t)value); */
2867 rc = callback((const char *)name, value);
2868 if (rc != 0) {
2869 free(zl.l_phys);
2870 return (rc);
2871 }
2872 }
2873 }
2874
2875 free(zl.l_phys);
2876 return (0);
2877 }
2878
zfs_printf(const char * name,uint64_t value __unused)2879 static int zfs_printf(const char *name, uint64_t value __unused)
2880 {
2881
2882 printf("%s\n", name);
2883
2884 return (0);
2885 }
2886
2887 /*
2888 * List a zap directory.
2889 */
2890 static int
zap_list(const spa_t * spa,const dnode_phys_t * dnode)2891 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
2892 {
2893 zap_phys_t *zap;
2894 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2895 int rc;
2896
2897 zap = malloc(size);
2898 if (zap == NULL)
2899 return (ENOMEM);
2900
2901 rc = dnode_read(spa, dnode, 0, zap, size);
2902 if (rc == 0) {
2903 if (zap->zap_block_type == ZBT_MICRO)
2904 rc = mzap_list((const mzap_phys_t *)zap, size,
2905 zfs_printf);
2906 else
2907 rc = fzap_list(spa, dnode, zap, zfs_printf);
2908 }
2909 free(zap);
2910 return (rc);
2911 }
2912
2913 static int
objset_get_dnode(const spa_t * spa,const objset_phys_t * os,uint64_t objnum,dnode_phys_t * dnode)2914 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum,
2915 dnode_phys_t *dnode)
2916 {
2917 off_t offset;
2918
2919 offset = objnum * sizeof (dnode_phys_t);
2920 return (dnode_read(spa, &os->os_meta_dnode, offset,
2921 dnode, sizeof (dnode_phys_t)));
2922 }
2923
2924 /*
2925 * Lookup a name in a microzap directory.
2926 */
2927 static int
mzap_rlookup(const mzap_phys_t * mz,size_t size,char * name,uint64_t value)2928 mzap_rlookup(const mzap_phys_t *mz, size_t size, char *name, uint64_t value)
2929 {
2930 const mzap_ent_phys_t *mze;
2931 int chunks, i;
2932
2933 /*
2934 * Microzap objects use exactly one block. Read the whole
2935 * thing.
2936 */
2937 chunks = size / MZAP_ENT_LEN - 1;
2938 for (i = 0; i < chunks; i++) {
2939 mze = &mz->mz_chunk[i];
2940 if (value == mze->mze_value) {
2941 strcpy(name, mze->mze_name);
2942 return (0);
2943 }
2944 }
2945
2946 return (ENOENT);
2947 }
2948
2949 static void
fzap_name_copy(const zap_leaf_t * zl,const zap_leaf_chunk_t * zc,char * name)2950 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
2951 {
2952 size_t namelen;
2953 const zap_leaf_chunk_t *nc;
2954 char *p;
2955
2956 namelen = zc->l_entry.le_name_numints;
2957
2958 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
2959 p = name;
2960 while (namelen > 0) {
2961 size_t len;
2962 len = namelen;
2963 if (len > ZAP_LEAF_ARRAY_BYTES)
2964 len = ZAP_LEAF_ARRAY_BYTES;
2965 memcpy(p, nc->l_array.la_array, len);
2966 p += len;
2967 namelen -= len;
2968 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
2969 }
2970
2971 *p = '\0';
2972 }
2973
2974 static int
fzap_rlookup(const spa_t * spa,const dnode_phys_t * dnode,zap_phys_t * zh,char * name,uint64_t value)2975 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh,
2976 char *name, uint64_t value)
2977 {
2978 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2979 fat_zap_t z;
2980 uint64_t i;
2981 int j, rc;
2982
2983 if (zh->zap_magic != ZAP_MAGIC)
2984 return (EIO);
2985
2986 z.zap_block_shift = ilog2(bsize);
2987 z.zap_phys = zh;
2988
2989 /*
2990 * This assumes that the leaf blocks start at block 1. The
2991 * documentation isn't exactly clear on this.
2992 */
2993 zap_leaf_t zl;
2994 zl.l_bs = z.zap_block_shift;
2995 zl.l_phys = malloc(bsize);
2996 if (zl.l_phys == NULL)
2997 return (ENOMEM);
2998
2999 for (i = 0; i < zh->zap_num_leafs; i++) {
3000 off_t off = ((off_t)(i + 1)) << zl.l_bs;
3001
3002 rc = dnode_read(spa, dnode, off, zl.l_phys, bsize);
3003 if (rc != 0)
3004 goto done;
3005
3006 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
3007 zap_leaf_chunk_t *zc;
3008
3009 zc = &ZAP_LEAF_CHUNK(&zl, j);
3010 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
3011 continue;
3012 if (zc->l_entry.le_value_intlen != 8 ||
3013 zc->l_entry.le_value_numints != 1)
3014 continue;
3015
3016 if (fzap_leaf_value(&zl, zc) == value) {
3017 fzap_name_copy(&zl, zc, name);
3018 goto done;
3019 }
3020 }
3021 }
3022
3023 rc = ENOENT;
3024 done:
3025 free(zl.l_phys);
3026 return (rc);
3027 }
3028
3029 static int
zap_rlookup(const spa_t * spa,const dnode_phys_t * dnode,char * name,uint64_t value)3030 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name,
3031 uint64_t value)
3032 {
3033 zap_phys_t *zap;
3034 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
3035 int rc;
3036
3037 zap = malloc(size);
3038 if (zap == NULL)
3039 return (ENOMEM);
3040
3041 rc = dnode_read(spa, dnode, 0, zap, size);
3042 if (rc == 0) {
3043 if (zap->zap_block_type == ZBT_MICRO)
3044 rc = mzap_rlookup((const mzap_phys_t *)zap, size,
3045 name, value);
3046 else
3047 rc = fzap_rlookup(spa, dnode, zap, name, value);
3048 }
3049 free(zap);
3050 return (rc);
3051 }
3052
3053 static int
zfs_rlookup(const spa_t * spa,uint64_t objnum,char * result)3054 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
3055 {
3056 char name[256];
3057 char component[256];
3058 uint64_t dir_obj, parent_obj, child_dir_zapobj;
3059 dnode_phys_t child_dir_zap, dataset, dir, parent;
3060 dsl_dir_phys_t *dd;
3061 dsl_dataset_phys_t *ds;
3062 char *p;
3063 int len;
3064
3065 p = &name[sizeof (name) - 1];
3066 *p = '\0';
3067
3068 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
3069 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
3070 return (EIO);
3071 }
3072 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
3073 dir_obj = ds->ds_dir_obj;
3074
3075 for (;;) {
3076 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
3077 return (EIO);
3078 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3079
3080 /* Actual loop condition. */
3081 parent_obj = dd->dd_parent_obj;
3082 if (parent_obj == 0)
3083 break;
3084
3085 if (objset_get_dnode(spa, &spa->spa_mos, parent_obj,
3086 &parent) != 0)
3087 return (EIO);
3088 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
3089 child_dir_zapobj = dd->dd_child_dir_zapobj;
3090 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj,
3091 &child_dir_zap) != 0)
3092 return (EIO);
3093 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
3094 return (EIO);
3095
3096 len = strlen(component);
3097 p -= len;
3098 memcpy(p, component, len);
3099 --p;
3100 *p = '/';
3101
3102 /* Actual loop iteration. */
3103 dir_obj = parent_obj;
3104 }
3105
3106 if (*p != '\0')
3107 ++p;
3108 strcpy(result, p);
3109
3110 return (0);
3111 }
3112
3113 static int
zfs_lookup_dataset(const spa_t * spa,const char * name,uint64_t * objnum)3114 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
3115 {
3116 char element[256];
3117 uint64_t dir_obj, child_dir_zapobj;
3118 dnode_phys_t child_dir_zap, dir;
3119 dsl_dir_phys_t *dd;
3120 const char *p, *q;
3121
3122 if (objset_get_dnode(spa, &spa->spa_mos,
3123 DMU_POOL_DIRECTORY_OBJECT, &dir))
3124 return (EIO);
3125 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj),
3126 1, &dir_obj))
3127 return (EIO);
3128
3129 p = name;
3130 for (;;) {
3131 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
3132 return (EIO);
3133 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
3134
3135 while (*p == '/')
3136 p++;
3137 /* Actual loop condition #1. */
3138 if (*p == '\0')
3139 break;
3140
3141 q = strchr(p, '/');
3142 if (q) {
3143 memcpy(element, p, q - p);
3144 element[q - p] = '\0';
3145 p = q + 1;
3146 } else {
3147 strcpy(element, p);
3148 p += strlen(p);
3149 }
3150
3151 child_dir_zapobj = dd->dd_child_dir_zapobj;
3152 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj,
3153 &child_dir_zap) != 0)
3154 return (EIO);
3155
3156 /* Actual loop condition #2. */
3157 if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj),
3158 1, &dir_obj) != 0)
3159 return (ENOENT);
3160 }
3161
3162 *objnum = dd->dd_head_dataset_obj;
3163 return (0);
3164 }
3165
3166 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
3167 static int
zfs_list_dataset(const spa_t * spa,uint64_t objnum)3168 zfs_list_dataset(const spa_t *spa, uint64_t objnum)
3169 {
3170 uint64_t dir_obj, child_dir_zapobj;
3171 dnode_phys_t child_dir_zap, dir, dataset;
3172 dsl_dataset_phys_t *ds;
3173 dsl_dir_phys_t *dd;
3174
3175 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
3176