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