1 /*-
2 * Copyright (c) 2007 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28
29 /*
30 * Stand-alone ZFS file reader.
31 */
32
33 #include <sys/stat.h>
34 #include <sys/stdint.h>
35
36 #include "zfsimpl.h"
37 #include "zfssubr.c"
38
39
40 struct zfsmount {
41 const spa_t *spa;
42 objset_phys_t objset;
43 uint64_t rootobj;
44 };
45
46 /*
47 * List of all vdevs, chained through v_alllink.
48 */
49 static vdev_list_t zfs_vdevs;
50
51 /*
52 * List of ZFS features supported for read
53 */
54 static const char *features_for_read[] = {
55 "org.illumos:lz4_compress",
56 "com.delphix:hole_birth",
57 "com.delphix:extensible_dataset",
58 "com.delphix:embedded_data",
59 "org.open-zfs:large_blocks",
60 "org.illumos:sha512",
61 "org.zfsonlinux:large_dnode",
62 NULL
63 };
64
65 /*
66 * List of all pools, chained through spa_link.
67 */
68 static spa_list_t zfs_pools;
69
70 static const dnode_phys_t *dnode_cache_obj;
71 static uint64_t dnode_cache_bn;
72 static char *dnode_cache_buf;
73 static char *zap_scratch;
74 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
75
76 #define TEMP_SIZE (1024 * 1024)
77
78 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
79 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
80 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
81
82 static void
zfs_init(void)83 zfs_init(void)
84 {
85 STAILQ_INIT(&zfs_vdevs);
86 STAILQ_INIT(&zfs_pools);
87
88 zfs_temp_buf = malloc(TEMP_SIZE);
89 zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
90 zfs_temp_ptr = zfs_temp_buf;
91 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
92 zap_scratch = malloc(SPA_MAXBLOCKSIZE);
93
94 zfs_init_crc();
95 }
96
97 static void *
zfs_alloc(size_t size)98 zfs_alloc(size_t size)
99 {
100 char *ptr;
101
102 if (zfs_temp_ptr + size > zfs_temp_end) {
103 printf("ZFS: out of temporary buffer space\n");
104 for (;;) ;
105 }
106 ptr = zfs_temp_ptr;
107 zfs_temp_ptr += size;
108
109 return (ptr);
110 }
111
112 static void
zfs_free(void * ptr,size_t size)113 zfs_free(void *ptr, size_t size)
114 {
115
116 zfs_temp_ptr -= size;
117 if (zfs_temp_ptr != ptr) {
118 printf("ZFS: zfs_alloc()/zfs_free() mismatch\n");
119 for (;;) ;
120 }
121 }
122
123 static int
xdr_int(const unsigned char ** xdr,int * ip)124 xdr_int(const unsigned char **xdr, int *ip)
125 {
126 *ip = ((*xdr)[0] << 24)
127 | ((*xdr)[1] << 16)
128 | ((*xdr)[2] << 8)
129 | ((*xdr)[3] << 0);
130 (*xdr) += 4;
131 return (0);
132 }
133
134 static int
xdr_u_int(const unsigned char ** xdr,u_int * ip)135 xdr_u_int(const unsigned char **xdr, u_int *ip)
136 {
137 *ip = ((*xdr)[0] << 24)
138 | ((*xdr)[1] << 16)
139 | ((*xdr)[2] << 8)
140 | ((*xdr)[3] << 0);
141 (*xdr) += 4;
142 return (0);
143 }
144
145 static int
xdr_uint64_t(const unsigned char ** xdr,uint64_t * lp)146 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
147 {
148 u_int hi, lo;
149
150 xdr_u_int(xdr, &hi);
151 xdr_u_int(xdr, &lo);
152 *lp = (((uint64_t) hi) << 32) | lo;
153 return (0);
154 }
155
156 static int
nvlist_find(const unsigned char * nvlist,const char * name,int type,int * elementsp,void * valuep)157 nvlist_find(const unsigned char *nvlist, const char *name, int type,
158 int* elementsp, void *valuep)
159 {
160 const unsigned char *p, *pair;
161 int junk;
162 int encoded_size, decoded_size;
163
164 p = nvlist;
165 xdr_int(&p, &junk);
166 xdr_int(&p, &junk);
167
168 pair = p;
169 xdr_int(&p, &encoded_size);
170 xdr_int(&p, &decoded_size);
171 while (encoded_size && decoded_size) {
172 int namelen, pairtype, elements;
173 const char *pairname;
174
175 xdr_int(&p, &namelen);
176 pairname = (const char*) p;
177 p += roundup(namelen, 4);
178 xdr_int(&p, &pairtype);
179
180 if (!memcmp(name, pairname, namelen) && type == pairtype) {
181 xdr_int(&p, &elements);
182 if (elementsp)
183 *elementsp = elements;
184 if (type == DATA_TYPE_UINT64) {
185 xdr_uint64_t(&p, (uint64_t *) valuep);
186 return (0);
187 } else if (type == DATA_TYPE_STRING) {
188 int len;
189 xdr_int(&p, &len);
190 (*(const char**) valuep) = (const char*) p;
191 return (0);
192 } else if (type == DATA_TYPE_NVLIST
193 || type == DATA_TYPE_NVLIST_ARRAY) {
194 (*(const unsigned char**) valuep) =
195 (const unsigned char*) p;
196 return (0);
197 } else {
198 return (EIO);
199 }
200 } else {
201 /*
202 * Not the pair we are looking for, skip to the next one.
203 */
204 p = pair + encoded_size;
205 }
206
207 pair = p;
208 xdr_int(&p, &encoded_size);
209 xdr_int(&p, &decoded_size);
210 }
211
212 return (EIO);
213 }
214
215 static int
nvlist_check_features_for_read(const unsigned char * nvlist)216 nvlist_check_features_for_read(const unsigned char *nvlist)
217 {
218 const unsigned char *p, *pair;
219 int junk;
220 int encoded_size, decoded_size;
221 int rc;
222
223 rc = 0;
224
225 p = nvlist;
226 xdr_int(&p, &junk);
227 xdr_int(&p, &junk);
228
229 pair = p;
230 xdr_int(&p, &encoded_size);
231 xdr_int(&p, &decoded_size);
232 while (encoded_size && decoded_size) {
233 int namelen, pairtype;
234 const char *pairname;
235 int i, found;
236
237 found = 0;
238
239 xdr_int(&p, &namelen);
240 pairname = (const char*) p;
241 p += roundup(namelen, 4);
242 xdr_int(&p, &pairtype);
243
244 for (i = 0; features_for_read[i] != NULL; i++) {
245 if (!memcmp(pairname, features_for_read[i], namelen)) {
246 found = 1;
247 break;
248 }
249 }
250
251 if (!found) {
252 printf("ZFS: unsupported feature: %s\n", pairname);
253 rc = EIO;
254 }
255
256 p = pair + encoded_size;
257
258 pair = p;
259 xdr_int(&p, &encoded_size);
260 xdr_int(&p, &decoded_size);
261 }
262
263 return (rc);
264 }
265
266 /*
267 * Return the next nvlist in an nvlist array.
268 */
269 static const unsigned char *
nvlist_next(const unsigned char * nvlist)270 nvlist_next(const unsigned char *nvlist)
271 {
272 const unsigned char *p, *pair;
273 int junk;
274 int encoded_size, decoded_size;
275
276 p = nvlist;
277 xdr_int(&p, &junk);
278 xdr_int(&p, &junk);
279
280 pair = p;
281 xdr_int(&p, &encoded_size);
282 xdr_int(&p, &decoded_size);
283 while (encoded_size && decoded_size) {
284 p = pair + encoded_size;
285
286 pair = p;
287 xdr_int(&p, &encoded_size);
288 xdr_int(&p, &decoded_size);
289 }
290
291 return p;
292 }
293
294 #ifdef TEST
295
296 static const unsigned char *
nvlist_print(const unsigned char * nvlist,unsigned int indent)297 nvlist_print(const unsigned char *nvlist, unsigned int indent)
298 {
299 static const char* typenames[] = {
300 "DATA_TYPE_UNKNOWN",
301 "DATA_TYPE_BOOLEAN",
302 "DATA_TYPE_BYTE",
303 "DATA_TYPE_INT16",
304 "DATA_TYPE_UINT16",
305 "DATA_TYPE_INT32",
306 "DATA_TYPE_UINT32",
307 "DATA_TYPE_INT64",
308 "DATA_TYPE_UINT64",
309 "DATA_TYPE_STRING",
310 "DATA_TYPE_BYTE_ARRAY",
311 "DATA_TYPE_INT16_ARRAY",
312 "DATA_TYPE_UINT16_ARRAY",
313 "DATA_TYPE_INT32_ARRAY",
314 "DATA_TYPE_UINT32_ARRAY",
315 "DATA_TYPE_INT64_ARRAY",
316 "DATA_TYPE_UINT64_ARRAY",
317 "DATA_TYPE_STRING_ARRAY",
318 "DATA_TYPE_HRTIME",
319 "DATA_TYPE_NVLIST",
320 "DATA_TYPE_NVLIST_ARRAY",
321 "DATA_TYPE_BOOLEAN_VALUE",
322 "DATA_TYPE_INT8",
323 "DATA_TYPE_UINT8",
324 "DATA_TYPE_BOOLEAN_ARRAY",
325 "DATA_TYPE_INT8_ARRAY",
326 "DATA_TYPE_UINT8_ARRAY"
327 };
328
329 unsigned int i, j;
330 const unsigned char *p, *pair;
331 int junk;
332 int encoded_size, decoded_size;
333
334 p = nvlist;
335 xdr_int(&p, &junk);
336 xdr_int(&p, &junk);
337
338 pair = p;
339 xdr_int(&p, &encoded_size);
340 xdr_int(&p, &decoded_size);
341 while (encoded_size && decoded_size) {
342 int namelen, pairtype, elements;
343 const char *pairname;
344
345 xdr_int(&p, &namelen);
346 pairname = (const char*) p;
347 p += roundup(namelen, 4);
348 xdr_int(&p, &pairtype);
349
350 for (i = 0; i < indent; i++)
351 printf(" ");
352 printf("%s %s", typenames[pairtype], pairname);
353
354 xdr_int(&p, &elements);
355 switch (pairtype) {
356 case DATA_TYPE_UINT64: {
357 uint64_t val;
358 xdr_uint64_t(&p, &val);
359 printf(" = 0x%jx\n", (uintmax_t)val);
360 break;
361 }
362
363 case DATA_TYPE_STRING: {
364 int len;
365 xdr_int(&p, &len);
366 printf(" = \"%s\"\n", p);
367 break;
368 }
369
370 case DATA_TYPE_NVLIST:
371 printf("\n");
372 nvlist_print(p, indent + 1);
373 break;
374
375 case DATA_TYPE_NVLIST_ARRAY:
376 for (j = 0; j < elements; j++) {
377 printf("[%d]\n", j);
378 p = nvlist_print(p, indent + 1);
379 if (j != elements - 1) {
380 for (i = 0; i < indent; i++)
381 printf(" ");
382 printf("%s %s", typenames[pairtype], pairname);
383 }
384 }
385 break;
386
387 default:
388 printf("\n");
389 }
390
391 p = pair + encoded_size;
392
393 pair = p;
394 xdr_int(&p, &encoded_size);
395 xdr_int(&p, &decoded_size);
396 }
397
398 return p;
399 }
400
401 #endif
402
403 static int
vdev_read_phys(vdev_t * vdev,const blkptr_t * bp,void * buf,off_t offset,size_t size)404 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
405 off_t offset, size_t size)
406 {
407 size_t psize;
408 int rc;
409
410 if (!vdev->v_phys_read)
411 return (EIO);
412
413 if (bp) {
414 psize = BP_GET_PSIZE(bp);
415 } else {
416 psize = size;
417 }
418
419 /*printf("ZFS: reading %zu bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/
420 rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
421 if (rc)
422 return (rc);
423 if (bp && zio_checksum_verify(bp, buf))
424 return (EIO);
425
426 return (0);
427 }
428
429 static int
vdev_disk_read(vdev_t * vdev,const blkptr_t * bp,void * buf,off_t offset,size_t bytes)430 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
431 off_t offset, size_t bytes)
432 {
433
434 return (vdev_read_phys(vdev, bp, buf,
435 offset + VDEV_LABEL_START_SIZE, bytes));
436 }
437
438
439 static int
vdev_mirror_read(vdev_t * vdev,const blkptr_t * bp,void * buf,off_t offset,size_t bytes)440 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
441 off_t offset, size_t bytes)
442 {
443 vdev_t *kid;
444 int rc;
445
446 rc = EIO;
447 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
448 if (kid->v_state != VDEV_STATE_HEALTHY)
449 continue;
450 rc = kid->v_read(kid, bp, buf, offset, bytes);
451 if (!rc)
452 return (0);
453 }
454
455 return (rc);
456 }
457
458 static int
vdev_replacing_read(vdev_t * vdev,const blkptr_t * bp,void * buf,off_t offset,size_t bytes)459 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
460 off_t offset, size_t bytes)
461 {
462 vdev_t *kid;
463
464 /*
465 * Here we should have two kids:
466 * First one which is the one we are replacing and we can trust
467 * only this one to have valid data, but it might not be present.
468 * Second one is that one we are replacing with. It is most likely
469 * healthy, but we can't trust it has needed data, so we won't use it.
470 */
471 kid = STAILQ_FIRST(&vdev->v_children);
472 if (kid == NULL)
473 return (EIO);
474 if (kid->v_state != VDEV_STATE_HEALTHY)
475 return (EIO);
476 return (kid->v_read(kid, bp, buf, offset, bytes));
477 }
478
479 static vdev_t *
vdev_find(uint64_t guid)480 vdev_find(uint64_t guid)
481 {
482 vdev_t *vdev;
483
484 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
485 if (vdev->v_guid == guid)
486 return (vdev);
487
488 return (0);
489 }
490
491 static vdev_t *
vdev_create(uint64_t guid,vdev_read_t * vdev_read)492 vdev_create(uint64_t guid, vdev_read_t *vdev_read)
493 {
494 vdev_t *vdev;
495
496 vdev = malloc(sizeof(vdev_t));
497 memset(vdev, 0, sizeof(vdev_t));
498 STAILQ_INIT(&vdev->v_children);
499 vdev->v_guid = guid;
500 vdev->v_state = VDEV_STATE_OFFLINE;
501 vdev->v_read = vdev_read;
502 vdev->v_phys_read = 0;
503 vdev->v_read_priv = 0;
504 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
505
506 return (vdev);
507 }
508
509 static int
vdev_init_from_nvlist(const unsigned char * nvlist,vdev_t * pvdev,vdev_t ** vdevp,int is_newer)510 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
511 vdev_t **vdevp, int is_newer)
512 {
513 int rc;
514 uint64_t guid, id, ashift, nparity;
515 const char *type;
516 const char *path;
517 vdev_t *vdev, *kid;
518 const unsigned char *kids;
519 int nkids, i, is_new;
520 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
521
522 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
523 NULL, &guid) ||
524 nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id) ||
525 nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
526 NULL, &type)) {
527 printf("ZFS: can't find vdev details\n");
528 return (ENOENT);
529 }
530
531 if (strcmp(type, VDEV_TYPE_MIRROR)
532 && strcmp(type, VDEV_TYPE_DISK)
533 #ifdef ZFS_TEST
534 && strcmp(type, VDEV_TYPE_FILE)
535 #endif
536 && strcmp(type, VDEV_TYPE_RAIDZ)
537 && strcmp(type, VDEV_TYPE_REPLACING)) {
538 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
539 return (EIO);
540 }
541
542 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
543
544 nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL,
545 &is_offline);
546 nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL,
547 &is_removed);
548 nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, NULL,
549 &is_faulted);
550 nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, NULL,
551 &is_degraded);
552 nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, NULL,
553 &isnt_present);
554
555 vdev = vdev_find(guid);
556 if (!vdev) {
557 is_new = 1;
558
559 if (!strcmp(type, VDEV_TYPE_MIRROR))
560 vdev = vdev_create(guid, vdev_mirror_read);
561 else if (!strcmp(type, VDEV_TYPE_RAIDZ))
562 vdev = vdev_create(guid, vdev_raidz_read);
563 else if (!strcmp(type, VDEV_TYPE_REPLACING))
564 vdev = vdev_create(guid, vdev_replacing_read);
565 else
566 vdev = vdev_create(guid, vdev_disk_read);
567
568 vdev->v_id = id;
569 vdev->v_top = pvdev != NULL ? pvdev : vdev;
570 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
571 DATA_TYPE_UINT64, NULL, &ashift) == 0) {
572 vdev->v_ashift = ashift;
573 } else {
574 vdev->v_ashift = 0;
575 }
576 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
577 DATA_TYPE_UINT64, NULL, &nparity) == 0) {
578 vdev->v_nparity = nparity;
579 } else {
580 vdev->v_nparity = 0;
581 }
582 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
583 DATA_TYPE_STRING, NULL, &path) == 0) {
584 if (strncmp(path, "/dev/dsk/", 9) == 0)
585 path += 9;
586 vdev->v_name = strdup(path);
587 if (nvlist_find(nvlist, ZPOOL_CONFIG_PHYS_PATH,
588 DATA_TYPE_STRING, NULL, &path) == 0) {
589 vdev->v_phys_path = strdup(path);
590 } else {
591 vdev->v_phys_path = NULL;
592 }
593 if (nvlist_find(nvlist, ZPOOL_CONFIG_DEVID,
594 DATA_TYPE_STRING, NULL, &path) == 0) {
595 vdev->v_devid = strdup(path);
596 } else {
597 vdev->v_devid = NULL;
598 }
599 } else {
600 if (!strcmp(type, "raidz")) {
601 if (vdev->v_nparity == 1)
602 vdev->v_name = "raidz1";
603 else if (vdev->v_nparity == 2)
604 vdev->v_name = "raidz2";
605 else if (vdev->v_nparity == 3)
606 vdev->v_name = "raidz3";
607 else {
608 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
609 return (EIO);
610 }
611 } else {
612 vdev->v_name = strdup(type);
613 }
614 }
615 } else {
616 is_new = 0;
617 }
618
619 if (is_new || is_newer) {
620 /*
621 * This is either new vdev or we've already seen this vdev,
622 * but from an older vdev label, so let's refresh its state
623 * from the newer label.
624 */
625 if (is_offline)
626 vdev->v_state = VDEV_STATE_OFFLINE;
627 else if (is_removed)
628 vdev->v_state = VDEV_STATE_REMOVED;
629 else if (is_faulted)
630 vdev->v_state = VDEV_STATE_FAULTED;
631 else if (is_degraded)
632 vdev->v_state = VDEV_STATE_DEGRADED;
633 else if (isnt_present)
634 vdev->v_state = VDEV_STATE_CANT_OPEN;
635 }
636
637 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
638 &nkids, &kids);
639 /*
640 * Its ok if we don't have any kids.
641 */
642 if (rc == 0) {
643 vdev->v_nchildren = nkids;
644 for (i = 0; i < nkids; i++) {
645 rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
646 if (rc)
647 return (rc);
648 if (is_new)
649 STAILQ_INSERT_TAIL(&vdev->v_children, kid,
650 v_childlink);
651 kids = nvlist_next(kids);
652 }
653 } else {
654 vdev->v_nchildren = 0;
655 }
656
657 if (vdevp)
658 *vdevp = vdev;
659 return (0);
660 }
661
662 static void
vdev_set_state(vdev_t * vdev)663 vdev_set_state(vdev_t *vdev)
664 {
665 vdev_t *kid;
666 int good_kids;
667 int bad_kids;
668
669 /*
670 * A mirror or raidz is healthy if all its kids are healthy. A
671 * mirror is degraded if any of its kids is healthy; a raidz
672 * is degraded if at most nparity kids are offline.
673 */
674 if (STAILQ_FIRST(&vdev->v_children)) {
675 good_kids = 0;
676 bad_kids = 0;
677 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
678 if (kid->v_state == VDEV_STATE_HEALTHY)
679 good_kids++;
680 else
681 bad_kids++;
682 }
683 if (bad_kids == 0) {
684 vdev->v_state = VDEV_STATE_HEALTHY;
685 } else {
686 if (vdev->v_read == vdev_mirror_read) {
687 if (good_kids) {
688 vdev->v_state = VDEV_STATE_DEGRADED;
689 } else {
690 vdev->v_state = VDEV_STATE_OFFLINE;
691 }
692 } else if (vdev->v_read == vdev_raidz_read) {
693 if (bad_kids > vdev->v_nparity) {
694 vdev->v_state = VDEV_STATE_OFFLINE;
695 } else {
696 vdev->v_state = VDEV_STATE_DEGRADED;
697 }
698 }
699 }
700 }
701 }
702
703 static spa_t *
spa_find_by_guid(uint64_t guid)704 spa_find_by_guid(uint64_t guid)
705 {
706 spa_t *spa;
707
708 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
709 if (spa->spa_guid == guid)
710 return (spa);
711
712 return (0);
713 }
714
715 static spa_t *
spa_find_by_name(const char * name)716 spa_find_by_name(const char *name)
717 {
718 spa_t *spa;
719
720 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
721 if (!strcmp(spa->spa_name, name))
722 return (spa);
723
724 return (0);
725 }
726
727 spa_t *
spa_get_primary(void)728 spa_get_primary(void)
729 {
730 return (STAILQ_FIRST(&zfs_pools));
731 }
732
733 vdev_t *
spa_get_primary_vdev(const spa_t * spa)734 spa_get_primary_vdev(const spa_t *spa)
735 {
736 vdev_t *vdev;
737 vdev_t *kid;
738
739 if (spa == NULL)
740 spa = spa_get_primary();
741 if (spa == NULL)
742 return (NULL);
743 vdev = STAILQ_FIRST(&spa->spa_vdevs);
744 if (vdev == NULL)
745 return (NULL);
746 for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
747 kid = STAILQ_FIRST(&vdev->v_children))
748 vdev = kid;
749 return (vdev);
750 }
751
752 static spa_t *
spa_create(uint64_t guid,const char * name)753 spa_create(uint64_t guid, const char *name)
754 {
755 spa_t *spa;
756
757 if ((spa = malloc(sizeof(spa_t))) == NULL)
758 return (NULL);
759 memset(spa, 0, sizeof(spa_t));
760 if ((spa->spa_name = strdup(name)) == NULL) {
761 free(spa);
762 return (NULL);
763 }
764 STAILQ_INIT(&spa->spa_vdevs);
765 spa->spa_guid = guid;
766 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
767
768 return (spa);
769 }
770
771 static const char *
state_name(vdev_state_t state)772 state_name(vdev_state_t state)
773 {
774 static const char* names[] = {
775 "UNKNOWN",
776 "CLOSED",
777 "OFFLINE",
778 "REMOVED",
779 "CANT_OPEN",
780 "FAULTED",
781 "DEGRADED",
782 "ONLINE"
783 };
784 return names[state];
785 }
786
787 static int
pager_printf(const char * fmt,...)788 pager_printf(const char *fmt, ...)
789 {
790 char line[80];
791 va_list args;
792
793 va_start(args, fmt);
794 vsnprintf(line, sizeof (line), fmt, args);
795 va_end(args);
796 return (pager_output(line));
797 }
798
799 #define STATUS_FORMAT " %s %s\n"
800
801 static int
print_state(int indent,const char * name,vdev_state_t state)802 print_state(int indent, const char *name, vdev_state_t state)
803 {
804 int i;
805 char buf[512];
806
807 buf[0] = 0;
808 for (i = 0; i < indent; i++)
809 strcat(buf, " ");
810 strcat(buf, name);
811 return (pager_printf(STATUS_FORMAT, buf, state_name(state)));
812 }
813
814 static int
vdev_status(vdev_t * vdev,int indent)815 vdev_status(vdev_t *vdev, int indent)
816 {
817 vdev_t *kid;
818 int ret;
819 ret = print_state(indent, vdev->v_name, vdev->v_state);
820 if (ret != 0)
821 return (ret);
822
823 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
824 ret = vdev_status(kid, indent + 1);
825 if (ret != 0)
826 return (ret);
827 }
828 return (ret);
829 }
830
831 static int
spa_status(spa_t * spa)832 spa_status(spa_t *spa)
833 {
834 static char bootfs[ZFS_MAXNAMELEN];
835 uint64_t rootid;
836 vdev_t *vdev;
837 int good_kids, bad_kids, degraded_kids, ret;
838 vdev_state_t state;
839
840 ret = pager_printf(" pool: %s\n", spa->spa_name);
841 if (ret != 0)
842 return (ret);
843
844 if (zfs_get_root(spa, &rootid) == 0 &&
845 zfs_rlookup(spa, rootid, bootfs) == 0) {
846 if (bootfs[0] == '\0')
847 ret = pager_printf("bootfs: %s\n", spa->spa_name);
848 else
849 ret = pager_printf("bootfs: %s/%s\n", spa->spa_name,
850 bootfs);
851 if (ret != 0)
852 return (ret);
853 }
854 ret = pager_printf("config:\n\n");
855 if (ret != 0)
856 return (ret);
857 ret = pager_printf(STATUS_FORMAT, "NAME", "STATE");
858 if (ret != 0)
859 return (ret);
860
861 good_kids = 0;
862 degraded_kids = 0;
863 bad_kids = 0;
864 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
865 if (vdev->v_state == VDEV_STATE_HEALTHY)
866 good_kids++;
867 else if (vdev->v_state == VDEV_STATE_DEGRADED)
868 degraded_kids++;
869 else
870 bad_kids++;
871 }
872
873 state = VDEV_STATE_CLOSED;
874 if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
875 state = VDEV_STATE_HEALTHY;
876 else if ((good_kids + degraded_kids) > 0)
877 state = VDEV_STATE_DEGRADED;
878
879 ret = print_state(0, spa->spa_name, state);
880 if (ret != 0)
881 return (ret);
882 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
883 ret = vdev_status(vdev, 1);
884 if (ret != 0)
885 return (ret);
886 }
887 return (ret);
888 }
889
890 int
spa_all_status(void)891 spa_all_status(void)
892 {
893 spa_t *spa;
894 int first = 1, ret = 0;
895
896 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
897 if (!first) {
898 ret = pager_printf("\n");
899 if (ret != 0)
900 return (ret);
901 }
902 first = 0;
903 ret = spa_status(spa);
904 if (ret != 0)
905 return (ret);
906 }
907 return (ret);
908 }
909
910 uint64_t
vdev_label_offset(uint64_t psize,int l,uint64_t offset)911 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
912 {
913 uint64_t label_offset;
914
915 if (l < VDEV_LABELS / 2)
916 label_offset = 0;
917 else
918 label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t);
919
920 return (offset + l * sizeof (vdev_label_t) + label_offset);
921 }
922
923 static int
vdev_probe(vdev_phys_read_t * phys_read,void * read_priv,spa_t ** spap)924 vdev_probe(vdev_phys_read_t *phys_read, void *read_priv, spa_t **spap)
925 {
926 vdev_t vtmp;
927 vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
928 vdev_phys_t *tmp_label;
929 spa_t *spa;
930 vdev_t *vdev, *top_vdev, *pool_vdev;
931 off_t off;
932 blkptr_t bp;
933 const unsigned char *nvlist = NULL;
934 uint64_t val;
935 uint64_t guid;
936 uint64_t best_txg = 0;
937 uint64_t pool_txg, pool_guid;
938 uint64_t psize;
939 const char *pool_name;
940 const unsigned char *vdevs;
941 const unsigned char *features;
942 int i, l, rc, is_newer;
943 char *upbuf;
944 const struct uberblock *up;
945
946 /*
947 * Load the vdev label and figure out which
948 * uberblock is most current.
949 */
950 memset(&vtmp, 0, sizeof(vtmp));
951 vtmp.v_phys_read = phys_read;
952 vtmp.v_read_priv = read_priv;
953 psize = P2ALIGN(ldi_get_size(read_priv),
954 (uint64_t)sizeof (vdev_label_t));
955
956 /* Test for minimum device size. */
957 if (psize < SPA_MINDEVSIZE)
958 return (EIO);
959
960 tmp_label = zfs_alloc(sizeof (vdev_phys_t));
961
962 for (l = 0; l < VDEV_LABELS; l++) {
963 off = vdev_label_offset(psize, l,
964 offsetof(vdev_label_t, vl_vdev_phys));
965
966 BP_ZERO(&bp);
967 BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
968 BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
969 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
970 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
971 DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
972 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
973
974 if (vdev_read_phys(&vtmp, &bp, tmp_label, off, 0))
975 continue;
976
977 if (tmp_label->vp_nvlist[0] != NV_ENCODE_XDR)
978 continue;
979
980 nvlist = (const unsigned char *) tmp_label->vp_nvlist + 4;
981 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG,
982 DATA_TYPE_UINT64, NULL, &pool_txg) != 0)
983 continue;
984
985 if (best_txg <= pool_txg) {
986 best_txg = pool_txg;
987 memcpy(vdev_label, tmp_label, sizeof (vdev_phys_t));
988 }
989 }
990
991 zfs_free(tmp_label, sizeof (vdev_phys_t));
992
993 if (best_txg == 0)
994 return (EIO);
995
996 if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR)
997 return (EIO);
998
999 nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
1000
1001 if (nvlist_find(nvlist, ZPOOL_CONFIG_VERSION, DATA_TYPE_UINT64,
1002 NULL, &val) != 0) {
1003 return (EIO);
1004 }
1005
1006 if (!SPA_VERSION_IS_SUPPORTED(val)) {
1007 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
1008 (unsigned) val, (unsigned) SPA_VERSION);
1009 return (EIO);
1010 }
1011
1012 /* Check ZFS features for read */
1013 if (nvlist_find(nvlist, ZPOOL_CONFIG_FEATURES_FOR_READ,
1014 DATA_TYPE_NVLIST, NULL, &features) == 0 &&
1015 nvlist_check_features_for_read(features) != 0) {
1016 return (EIO);
1017 }
1018
1019 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_STATE, DATA_TYPE_UINT64,
1020 NULL, &val) != 0) {
1021 return (EIO);
1022 }
1023
1024 if (val == POOL_STATE_DESTROYED) {
1025 /* We don't boot only from destroyed pools. */
1026 return (EIO);
1027 }
1028
1029 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG, DATA_TYPE_UINT64,
1030 NULL, &pool_txg) != 0 ||
1031 nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64,
1032 NULL, &pool_guid) != 0 ||
1033 nvlist_find(nvlist, ZPOOL_CONFIG_POOL_NAME, DATA_TYPE_STRING,
1034 NULL, &pool_name) != 0) {
1035 /*
1036 * Cache and spare devices end up here - just ignore
1037 * them.
1038 */
1039 /*printf("ZFS: can't find pool details\n");*/
1040 return (EIO);
1041 }
1042
1043 if (nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64,
1044 NULL, &val) == 0 && val != 0) {
1045 return (EIO);
1046 }
1047
1048 /*
1049 * Create the pool if this is the first time we've seen it.
1050 */
1051 spa = spa_find_by_guid(pool_guid);
1052 if (spa == NULL) {
1053 spa = spa_create(pool_guid, pool_name);
1054 if (spa == NULL)
1055 return (ENOMEM);
1056 }
1057 if (pool_txg > spa->spa_txg) {
1058 spa->spa_txg = pool_txg;
1059 is_newer = 1;
1060 } else {
1061 is_newer = 0;
1062 }
1063
1064 /*
1065 * Get the vdev tree and create our in-core copy of it.
1066 * If we already have a vdev with this guid, this must
1067 * be some kind of alias (overlapping slices, dangerously dedicated
1068 * disks etc).
1069 */
1070 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
1071 NULL, &guid) != 0) {
1072 return (EIO);
1073 }
1074 vdev = vdev_find(guid);
1075 if (vdev && vdev->v_phys_read) /* Has this vdev already been inited? */
1076 return (EIO);
1077
1078 if (nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST,
1079 NULL, &vdevs)) {
1080 return (EIO);
1081 }
1082
1083 rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer);
1084 if (rc != 0)
1085 return (rc);
1086
1087 /*
1088 * Add the toplevel vdev to the pool if its not already there.
1089 */
1090 STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
1091 if (top_vdev == pool_vdev)
1092 break;
1093 if (!pool_vdev && top_vdev)
1094 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
1095
1096 /*
1097 * We should already have created an incomplete vdev for this
1098 * vdev. Find it and initialise it with our read proc.
1099 */
1100 vdev = vdev_find(guid);
1101 if (vdev) {
1102 vdev->v_phys_read = phys_read;
1103 vdev->v_read_priv = read_priv;
1104 vdev->v_state = VDEV_STATE_HEALTHY;
1105 } else {
1106 printf("ZFS: inconsistent nvlist contents\n");
1107 return (EIO);
1108 }
1109
1110 /*
1111 * Re-evaluate top-level vdev state.
1112 */
1113 vdev_set_state(top_vdev);
1114
1115 /*
1116 * Ok, we are happy with the pool so far. Lets find
1117 * the best uberblock and then we can actually access
1118 * the contents of the pool.
1119 */
1120 upbuf = zfs_alloc(VDEV_UBERBLOCK_SIZE(vdev));
1121 up = (const struct uberblock *)upbuf;
1122 for (l = 0; l < VDEV_LABELS; l++) {
1123 for (i = 0; i < VDEV_UBERBLOCK_COUNT(vdev); i++) {
1124 off = vdev_label_offset(psize, l,
1125 VDEV_UBERBLOCK_OFFSET(vdev, i));
1126 BP_ZERO(&bp);
1127 DVA_SET_OFFSET(&bp.blk_dva[0], off);
1128 BP_SET_LSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1129 BP_SET_PSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1130 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1131 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1132 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1133
1134 if (vdev_read_phys(vdev, &bp, upbuf, off, 0) != 0)
1135 continue;
1136
1137 if (up->ub_magic != UBERBLOCK_MAGIC)
1138 continue;
1139 if (up->ub_txg < spa->spa_txg)
1140 continue;
1141 if (up->ub_txg > spa->spa_uberblock.ub_txg ||
1142 (up->ub_txg == spa->spa_uberblock.ub_txg &&
1143 up->ub_timestamp >
1144 spa->spa_uberblock.ub_timestamp)) {
1145 spa->spa_uberblock = *up;
1146 }
1147 }
1148 }
1149 zfs_free(upbuf, VDEV_UBERBLOCK_SIZE(vdev));
1150
1151 if (spap != NULL)
1152 *spap = spa;
1153 return (0);
1154 }
1155
1156 static int
ilog2(int n)1157 ilog2(int n)
1158 {
1159 int v;
1160
1161 for (v = 0; v < 32; v++)
1162 if (n == (1 << v))
1163 return v;
1164 return -1;
1165 }
1166
1167 static int
zio_read_gang(const spa_t * spa,const blkptr_t * bp,void * buf)1168 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
1169 {
1170 blkptr_t gbh_bp;
1171 zio_gbh_phys_t zio_gb;
1172 char *pbuf;
1173 int i;
1174
1175 /* Artificial BP for gang block header. */
1176 gbh_bp = *bp;
1177 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1178 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1179 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
1180 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
1181 for (i = 0; i < SPA_DVAS_PER_BP; i++)
1182 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
1183
1184 /* Read gang header block using the artificial BP. */
1185 if (zio_read(spa, &gbh_bp, &zio_gb))
1186 return (EIO);
1187
1188 pbuf = buf;
1189 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1190 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
1191
1192 if (BP_IS_HOLE(gbp))
1193 continue;
1194 if (zio_read(spa, gbp, pbuf))
1195 return (EIO);
1196 pbuf += BP_GET_PSIZE(gbp);
1197 }
1198
1199 if (zio_checksum_verify(bp, buf))
1200 return (EIO);
1201 return (0);
1202 }
1203
1204 static int
zio_read(const spa_t * spa,const blkptr_t * bp,void * buf)1205 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
1206 {
1207 int cpfunc = BP_GET_COMPRESS(bp);
1208 uint64_t align, size;
1209 void *pbuf;
1210 int i, error;
1211
1212 /*
1213 * Process data embedded in block pointer
1214 */
1215 if (BP_IS_EMBEDDED(bp)) {
1216 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
1217
1218 size = BPE_GET_PSIZE(bp);
1219 ASSERT(size <= BPE_PAYLOAD_SIZE);
1220
1221 if (cpfunc != ZIO_COMPRESS_OFF)
1222 pbuf = zfs_alloc(size);
1223 else
1224 pbuf = buf;
1225
1226 decode_embedded_bp_compressed(bp, pbuf);
1227 error = 0;
1228
1229 if (cpfunc != ZIO_COMPRESS_OFF) {
1230 error = zio_decompress_data(cpfunc, pbuf,
1231 size, buf, BP_GET_LSIZE(bp));
1232 zfs_free(pbuf, size);
1233 }
1234 if (error != 0)
1235 printf("ZFS: i/o error - unable to decompress block pointer data, error %d\n",
1236 error);
1237 return (error);
1238 }
1239
1240 error = EIO;
1241
1242 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1243 const dva_t *dva = &bp->blk_dva[i];
1244 vdev_t *vdev;
1245 int vdevid;
1246 off_t offset;
1247
1248 if (!dva->dva_word[0] && !dva->dva_word[1])
1249 continue;
1250
1251 vdevid = DVA_GET_VDEV(dva);
1252 offset = DVA_GET_OFFSET(dva);
1253 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1254 if (vdev->v_id == vdevid)
1255 break;
1256 }
1257 if (!vdev || !vdev->v_read)
1258 continue;
1259
1260 size = BP_GET_PSIZE(bp);
1261 if (vdev->v_read == vdev_raidz_read) {
1262 align = 1ULL << vdev->v_top->v_ashift;
1263 if (P2PHASE(size, align) != 0)
1264 size = P2ROUNDUP(size, align);
1265 }
1266 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
1267 pbuf = zfs_alloc(size);
1268 else
1269 pbuf = buf;
1270
1271 if (DVA_GET_GANG(dva))
1272 error = zio_read_gang(spa, bp, pbuf);
1273 else
1274 error = vdev->v_read(vdev, bp, pbuf, offset, size);
1275 if (error == 0) {
1276 if (cpfunc != ZIO_COMPRESS_OFF)
1277 error = zio_decompress_data(cpfunc, pbuf,
1278 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
1279 else if (size != BP_GET_PSIZE(bp))
1280 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
1281 }
1282 if (buf != pbuf)
1283 zfs_free(pbuf, size);
1284 if (error == 0)
1285 break;
1286 }
1287 if (error != 0)
1288 printf("ZFS: i/o error - all block copies unavailable\n");
1289 return (error);
1290 }
1291
1292 static int
dnode_read(const spa_t * spa,const dnode_phys_t * dnode,off_t offset,void * buf,size_t buflen)1293 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1294 {
1295 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1296 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1297 int nlevels = dnode->dn_nlevels;
1298 int i, rc;
1299
1300 if (bsize > SPA_MAXBLOCKSIZE) {
1301 printf("ZFS: I/O error - blocks larger than %llu are not "
1302 "supported\n", SPA_MAXBLOCKSIZE);
1303 return (EIO);
1304 }
1305
1306 /*
1307 * Note: bsize may not be a power of two here so we need to do an
1308 * actual divide rather than a bitshift.
1309 */
1310 while (buflen > 0) {
1311 uint64_t bn = offset / bsize;
1312 int boff = offset % bsize;
1313 int ibn;
1314 const blkptr_t *indbp;
1315 blkptr_t bp;
1316
1317 if (bn > dnode->dn_maxblkid) {
1318 printf("warning: zfs bug: bn %llx > dn_maxblkid %llx\n",
1319 (unsigned long long)bn,
1320 (unsigned long long)dnode->dn_maxblkid);
1321 /*
1322 * zfs bug, will not return error
1323 * return (EIO);
1324 */
1325 }
1326
1327 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1328 goto cached;
1329
1330 indbp = dnode->dn_blkptr;
1331 for (i = 0; i < nlevels; i++) {
1332 /*
1333 * Copy the bp from the indirect array so that
1334 * we can re-use the scratch buffer for multi-level
1335 * objects.
1336 */
1337 ibn = bn >> ((nlevels - i - 1) * ibshift);
1338 ibn &= ((1 << ibshift) - 1);
1339 bp = indbp[ibn];
1340 if (BP_IS_HOLE(&bp)) {
1341 memset(dnode_cache_buf, 0, bsize);
1342 break;
1343 }
1344 rc = zio_read(spa, &bp, dnode_cache_buf);
1345 if (rc)
1346 return (rc);
1347 indbp = (const blkptr_t *) dnode_cache_buf;
1348 }
1349 dnode_cache_obj = dnode;
1350 dnode_cache_bn = bn;
1351 cached:
1352
1353 /*
1354 * The buffer contains our data block. Copy what we
1355 * need from it and loop.
1356 */
1357 i = bsize - boff;
1358 if (i > buflen) i = buflen;
1359 memcpy(buf, &dnode_cache_buf[boff], i);
1360 buf = ((char*) buf) + i;
1361 offset += i;
1362 buflen -= i;
1363 }
1364
1365 return (0);
1366 }
1367
1368 /*
1369 * Lookup a value in a microzap directory. Assumes that the zap
1370 * scratch buffer contains the directory contents.
1371 */
1372 static int
mzap_lookup(const dnode_phys_t * dnode,const char * name,uint64_t * value)1373 mzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value)
1374 {
1375 const mzap_phys_t *mz;
1376 const mzap_ent_phys_t *mze;
1377 size_t size;
1378 int chunks, i;
1379
1380 /*
1381 * Microzap objects use exactly one block. Read the whole
1382 * thing.
1383 */
1384 size = dnode->dn_datablkszsec * 512;
1385
1386 mz = (const mzap_phys_t *) zap_scratch;
1387 chunks = size / MZAP_ENT_LEN - 1;
1388
1389 for (i = 0; i < chunks; i++) {
1390 mze = &mz->mz_chunk[i];
1391 if (!strcmp(mze->mze_name, name)) {
1392 *value = mze->mze_value;
1393 return (0);
1394 }
1395 }
1396
1397 return (ENOENT);
1398 }
1399
1400 /*
1401 * Compare a name with a zap leaf entry. Return non-zero if the name
1402 * matches.
1403 */
1404 static int
fzap_name_equal(const zap_leaf_t * zl,const zap_leaf_chunk_t * zc,const char * name)1405 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1406 {
1407 size_t namelen;
1408 const zap_leaf_chunk_t *nc;
1409 const char *p;
1410
1411 namelen = zc->l_entry.le_name_numints;
1412
1413 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1414 p = name;
1415 while (namelen > 0) {
1416 size_t len;
1417 len = namelen;
1418 if (len > ZAP_LEAF_ARRAY_BYTES)
1419 len = ZAP_LEAF_ARRAY_BYTES;
1420 if (memcmp(p, nc->l_array.la_array, len))
1421 return (0);
1422 p += len;
1423 namelen -= len;
1424 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1425 }
1426
1427 return 1;
1428 }
1429
1430 /*
1431 * Extract a uint64_t value from a zap leaf entry.
1432 */
1433 static uint64_t
fzap_leaf_value(const zap_leaf_t * zl,const zap_leaf_chunk_t * zc)1434 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1435 {
1436 const zap_leaf_chunk_t *vc;
1437 int i;
1438 uint64_t value;
1439 const uint8_t *p;
1440
1441 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1442 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1443 value = (value << 8) | p[i];
1444 }
1445
1446 return value;
1447 }
1448
1449 /*
1450 * Lookup a value in a fatzap directory. Assumes that the zap scratch
1451 * buffer contains the directory header.
1452 */
1453 static int
fzap_lookup(const spa_t * spa,const dnode_phys_t * dnode,const char * name,uint64_t * value)1454 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1455 {
1456 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1457 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1458 fat_zap_t z;
1459 uint64_t *ptrtbl;
1460 uint64_t hash;
1461 int rc;
1462
1463 if (zh.zap_magic != ZAP_MAGIC)
1464 return (EIO);
1465
1466 z.zap_block_shift = ilog2(bsize);
1467 z.zap_phys = (zap_phys_t *) zap_scratch;
1468
1469 /*
1470 * Figure out where the pointer table is and read it in if necessary.
1471 */
1472 if (zh.zap_ptrtbl.zt_blk) {
1473 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1474 zap_scratch, bsize);
1475 if (rc)
1476 return (rc);
1477 ptrtbl = (uint64_t *) zap_scratch;
1478 } else {
1479 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1480 }
1481
1482 hash = zap_hash(zh.zap_salt, name);
1483
1484 zap_leaf_t zl;
1485 zl.l_bs = z.zap_block_shift;
1486
1487 off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1488 zap_leaf_chunk_t *zc;
1489
1490 rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1491 if (rc)
1492 return (rc);
1493
1494 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1495
1496 /*
1497 * Make sure this chunk matches our hash.
1498 */
1499 if (zl.l_phys->l_hdr.lh_prefix_len > 0
1500 && zl.l_phys->l_hdr.lh_prefix
1501 != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1502 return (ENOENT);
1503
1504 /*
1505 * Hash within the chunk to find our entry.
1506 */
1507 int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1508 int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1509 h = zl.l_phys->l_hash[h];
1510 if (h == 0xffff)
1511 return (ENOENT);
1512 zc = &ZAP_LEAF_CHUNK(&zl, h);
1513 while (zc->l_entry.le_hash != hash) {
1514 if (zc->l_entry.le_next == 0xffff) {
1515 zc = 0;
1516 break;
1517 }
1518 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1519 }
1520 if (fzap_name_equal(&zl, zc, name)) {
1521 if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints > 8)
1522 return (E2BIG);
1523 *value = fzap_leaf_value(&zl, zc);
1524 return (0);
1525 }
1526
1527 return (ENOENT);
1528 }
1529
1530 /*
1531 * Lookup a name in a zap object and return its value as a uint64_t.
1532 */
1533 static int
zap_lookup(const spa_t * spa,const dnode_phys_t * dnode,const char * name,uint64_t * value)1534 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1535 {
1536 int rc;
1537 uint64_t zap_type;
1538 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1539
1540 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1541 if (rc)
1542 return (rc);
1543
1544 zap_type = *(uint64_t *) zap_scratch;
1545 if (zap_type == ZBT_MICRO)
1546 return mzap_lookup(dnode, name, value);
1547 else if (zap_type == ZBT_HEADER)
1548 return fzap_lookup(spa, dnode, name, value);
1549 printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
1550 return (EIO);
1551 }
1552
1553 /*
1554 * List a microzap directory. Assumes that the zap scratch buffer contains
1555 * the directory contents.
1556 */
1557 static int
mzap_list(const dnode_phys_t * dnode,int (* callback)(const char *,uint64_t))1558 mzap_list(const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t))
1559 {
1560 const mzap_phys_t *mz;
1561 const mzap_ent_phys_t *mze;
1562 size_t size;
1563 int chunks, i, rc;
1564
1565 /*
1566 * Microzap objects use exactly one block. Read the whole
1567 * thing.
1568 */
1569 size = dnode->dn_datablkszsec * 512;
1570 mz = (const mzap_phys_t *) zap_scratch;
1571 chunks = size / MZAP_ENT_LEN - 1;
1572
1573 for (i = 0; i < chunks; i++) {
1574 mze = &mz->mz_chunk[i];
1575 if (mze->mze_name[0]) {
1576 rc = callback(mze->mze_name, mze->mze_value);
1577 if (rc != 0)
1578 return (rc);
1579 }
1580 }
1581
1582 return (0);
1583 }
1584
1585 /*
1586 * List a fatzap directory. Assumes that the zap scratch buffer contains
1587 * the directory header.
1588 */
1589 static int
fzap_list(const spa_t * spa,const dnode_phys_t * dnode,int (* callback)(const char *,uint64_t))1590 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t))
1591 {
1592 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1593 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1594 fat_zap_t z;
1595 int i, j, rc;
1596
1597 if (zh.zap_magic != ZAP_MAGIC)
1598 return (EIO);
1599
1600 z.zap_block_shift = ilog2(bsize);
1601 z.zap_phys = (zap_phys_t *) zap_scratch;
1602
1603 /*
1604 * This assumes that the leaf blocks start at block 1. The
1605 * documentation isn't exactly clear on this.
1606 */
1607 zap_leaf_t zl;
1608 zl.l_bs = z.zap_block_shift;
1609 for (i = 0; i < zh.zap_num_leafs; i++) {
1610 off_t off = (i + 1) << zl.l_bs;
1611 char name[256], *p;
1612 uint64_t value;
1613
1614 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1615 return (EIO);
1616
1617 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1618
1619 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1620 zap_leaf_chunk_t *zc, *nc;
1621 int namelen;
1622
1623 zc = &ZAP_LEAF_CHUNK(&zl, j);
1624 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1625 continue;
1626 namelen = zc->l_entry.le_name_numints;
1627 if (namelen > sizeof(name))
1628 namelen = sizeof(name);
1629
1630 /*
1631 * Paste the name back together.
1632 */
1633 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1634 p = name;
1635 while (namelen > 0) {
1636 int len;
1637 len = namelen;
1638 if (len > ZAP_LEAF_ARRAY_BYTES)
1639 len = ZAP_LEAF_ARRAY_BYTES;
1640 memcpy(p, nc->l_array.la_array, len);
1641 p += len;
1642 namelen -= len;
1643 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1644 }
1645
1646 /*
1647 * Assume the first eight bytes of the value are
1648 * a uint64_t.
1649 */
1650 value = fzap_leaf_value(&zl, zc);
1651
1652 //printf("%s 0x%jx\n", name, (uintmax_t)value);
1653 rc = callback((const char *)name, value);
1654 if (rc != 0)
1655 return (rc);
1656 }
1657 }
1658
1659 return (0);
1660 }
1661
zfs_printf(const char * name,uint64_t value __unused)1662 static int zfs_printf(const char *name, uint64_t value __unused)
1663 {
1664
1665 printf("%s\n", name);
1666
1667 return (0);
1668 }
1669
1670 /*
1671 * List a zap directory.
1672 */
1673 static int
zap_list(const spa_t * spa,const dnode_phys_t * dnode)1674 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
1675 {
1676 uint64_t zap_type;
1677 size_t size = dnode->dn_datablkszsec * 512;
1678
1679 if (dnode_read(spa, dnode, 0, zap_scratch, size))
1680 return (EIO);
1681
1682 zap_type = *(uint64_t *) zap_scratch;
1683 if (zap_type == ZBT_MICRO)
1684 return mzap_list(dnode, zfs_printf);
1685 else
1686 return fzap_list(spa, dnode, zfs_printf);
1687 }
1688
1689 static int
objset_get_dnode(const spa_t * spa,const objset_phys_t * os,uint64_t objnum,dnode_phys_t * dnode)1690 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1691 {
1692 off_t offset;
1693
1694 offset = objnum * sizeof(dnode_phys_t);
1695 return dnode_read(spa, &os->os_meta_dnode, offset,
1696 dnode, sizeof(dnode_phys_t));
1697 }
1698
1699 static int
mzap_rlookup(const spa_t * spa,const dnode_phys_t * dnode,char * name,uint64_t value)1700 mzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1701 {
1702 const mzap_phys_t *mz;
1703 const mzap_ent_phys_t *mze;
1704 size_t size;
1705 int chunks, i;
1706
1707 /*
1708 * Microzap objects use exactly one block. Read the whole
1709 * thing.
1710 */
1711 size = dnode->dn_datablkszsec * 512;
1712
1713 mz = (const mzap_phys_t *) zap_scratch;
1714 chunks = size / MZAP_ENT_LEN - 1;
1715
1716 for (i = 0; i < chunks; i++) {
1717 mze = &mz->mz_chunk[i];
1718 if (value == mze->mze_value) {
1719 strcpy(name, mze->mze_name);
1720 return (0);
1721 }
1722 }
1723
1724 return (ENOENT);
1725 }
1726
1727 static void
fzap_name_copy(const zap_leaf_t * zl,const zap_leaf_chunk_t * zc,char * name)1728 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
1729 {
1730 size_t namelen;
1731 const zap_leaf_chunk_t *nc;
1732 char *p;
1733
1734 namelen = zc->l_entry.le_name_numints;
1735
1736 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1737 p = name;
1738 while (namelen > 0) {
1739 size_t len;
1740 len = namelen;
1741 if (len > ZAP_LEAF_ARRAY_BYTES)
1742 len = ZAP_LEAF_ARRAY_BYTES;
1743 memcpy(p, nc->l_array.la_array, len);
1744 p += len;
1745 namelen -= len;
1746 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1747 }
1748
1749 *p = '\0';
1750 }
1751
1752 static int
fzap_rlookup(const spa_t * spa,const dnode_phys_t * dnode,char * name,uint64_t value)1753 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1754 {
1755 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1756 zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1757 fat_zap_t z;
1758 int i, j;
1759
1760 if (zh.zap_magic != ZAP_MAGIC)
1761 return (EIO);
1762
1763 z.zap_block_shift = ilog2(bsize);
1764 z.zap_phys = (zap_phys_t *) zap_scratch;
1765
1766 /*
1767 * This assumes that the leaf blocks start at block 1. The
1768 * documentation isn't exactly clear on this.
1769 */
1770 zap_leaf_t zl;
1771 zl.l_bs = z.zap_block_shift;
1772 for (i = 0; i < zh.zap_num_leafs; i++) {
1773 off_t off = (i + 1) << zl.l_bs;
1774
1775 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1776 return (EIO);
1777
1778 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1779
1780 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1781 zap_leaf_chunk_t *zc;
1782
1783 zc = &ZAP_LEAF_CHUNK(&zl, j);
1784 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1785 continue;
1786 if (zc->l_entry.le_value_intlen != 8 ||
1787 zc->l_entry.le_value_numints != 1)
1788 continue;
1789
1790 if (fzap_leaf_value(&zl, zc) == value) {
1791 fzap_name_copy(&zl, zc, name);
1792 return (0);
1793 }
1794 }
1795 }
1796
1797 return (ENOENT);
1798 }
1799
1800 static int
zap_rlookup(const spa_t * spa,const dnode_phys_t * dnode,char * name,uint64_t value)1801 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1802 {
1803 int rc;
1804 uint64_t zap_type;
1805 size_t size = dnode->dn_datablkszsec * 512;
1806
1807 rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1808 if (rc)
1809 return (rc);
1810
1811 zap_type = *(uint64_t *) zap_scratch;
1812 if (zap_type == ZBT_MICRO)
1813 return mzap_rlookup(spa, dnode, name, value);
1814 else
1815 return fzap_rlookup(spa, dnode, name, value);
1816 }
1817
1818 static int
zfs_rlookup(const spa_t * spa,uint64_t objnum,char * result)1819 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
1820 {
1821 char name[256];
1822 char component[256];
1823 uint64_t dir_obj, parent_obj, child_dir_zapobj;
1824 dnode_phys_t child_dir_zap, dataset, dir, parent;
1825 dsl_dir_phys_t *dd;
1826 dsl_dataset_phys_t *ds;
1827 char *p;
1828 int len;
1829
1830 p = &name[sizeof(name) - 1];
1831 *p = '\0';
1832
1833 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1834 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1835 return (EIO);
1836 }
1837 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
1838 dir_obj = ds->ds_dir_obj;
1839
1840 for (;;) {
1841 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
1842 return (EIO);
1843 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1844
1845 /* Actual loop condition. */
1846 parent_obj = dd->dd_parent_obj;
1847 if (parent_obj == 0)
1848 break;
1849
1850 if (objset_get_dnode(spa, &spa->spa_mos, parent_obj, &parent) != 0)
1851 return (EIO);
1852 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
1853 child_dir_zapobj = dd->dd_child_dir_zapobj;
1854 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1855 return (EIO);
1856 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
1857 return (EIO);
1858
1859 len = strlen(component);
1860 p -= len;
1861 memcpy(p, component, len);
1862 --p;
1863 *p = '/';
1864
1865 /* Actual loop iteration. */
1866 dir_obj = parent_obj;
1867 }
1868
1869 if (*p != '\0')
1870 ++p;
1871 strcpy(result, p);
1872
1873 return (0);
1874 }
1875
1876 static int
zfs_lookup_dataset(const spa_t * spa,const char * name,uint64_t * objnum)1877 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
1878 {
1879 char element[256];
1880 uint64_t dir_obj, child_dir_zapobj;
1881 dnode_phys_t child_dir_zap, dir;
1882 dsl_dir_phys_t *dd;
1883 const char *p, *q;
1884
1885 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir))
1886 return (EIO);
1887 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &dir_obj))
1888 return (EIO);
1889
1890 p = name;
1891 for (;;) {
1892 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
1893 return (EIO);
1894 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1895
1896 while (*p == '/')
1897 p++;
1898 /* Actual loop condition #1. */
1899 if (*p == '\0')
1900 break;
1901
1902 q = strchr(p, '/');
1903 if (q) {
1904 memcpy(element, p, q - p);
1905 element[q - p] = '\0';
1906 p = q + 1;
1907 } else {
1908 strcpy(element, p);
1909 p += strlen(p);
1910 }
1911
1912 child_dir_zapobj = dd->dd_child_dir_zapobj;
1913 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1914 return (EIO);
1915
1916 /* Actual loop condition #2. */
1917 if (zap_lookup(spa, &child_dir_zap, element, &dir_obj) != 0)
1918 return (ENOENT);
1919 }
1920
1921 *objnum = dd->dd_head_dataset_obj;
1922 return (0);
1923 }
1924
1925 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
1926 static int
zfs_list_dataset(const spa_t * spa,uint64_t objnum)1927 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
1928 {
1929 uint64_t dir_obj, child_dir_zapobj;
1930 dnode_phys_t child_dir_zap, dir, dataset;
1931 dsl_dataset_phys_t *ds;
1932 dsl_dir_phys_t *dd;
1933
1934 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1935 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1936 return (EIO);
1937 }
1938 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1939 dir_obj = ds->ds_dir_obj;
1940
1941 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
1942 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
1943 return (EIO);
1944 }
1945 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1946
1947 child_dir_zapobj = dd->dd_child_dir_zapobj;
1948 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) {
1949 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
1950 return (EIO);
1951 }
1952
1953 return (zap_list(spa, &child_dir_zap) != 0);
1954 }
1955
1956 int
zfs_callback_dataset(const spa_t * spa,uint64_t objnum,int (* callback)(const char *,uint64_t))1957 zfs_callback_dataset(const spa_t *spa, uint64_t objnum, int (*callback)(const char *, uint64_t))
1958 {
1959 uint64_t dir_obj, child_dir_zapobj, zap_type;
1960 dnode_phys_t child_dir_zap, dir, dataset;
1961 dsl_dataset_phys_t *ds;
1962 dsl_dir_phys_t *dd;
1963 int err;
1964
1965 err = objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset);
1966 if (err != 0) {
1967 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1968 return (err);
1969 }
1970 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1971 dir_obj = ds->ds_dir_obj;
1972
1973 err = objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir);
1974 if (err != 0) {
1975 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
1976 return (err);
1977 }
1978 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1979
1980 child_dir_zapobj = dd->dd_child_dir_zapobj;
1981 err = objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap);
1982 if (err != 0) {
1983 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
1984 return (err);
1985 }
1986
1987 err = dnode_read(spa, &child_dir_zap, 0, zap_scratch, child_dir_zap.dn_datablkszsec * 512);
1988 if (err != 0)
1989 return (err);
1990
1991 zap_type = *(uint64_t *) zap_scratch;
1992 if (zap_type == ZBT_MICRO)
1993 return mzap_list(&child_dir_zap, callback);
1994 else
1995 return fzap_list(spa, &child_dir_zap, callback);
1996 }
1997
1998 /*
1999 * Find the object set given the object number of its dataset object
2000 * and return its details in *objset
2001 */
2002 static int
zfs_mount_dataset(const spa_t * spa,uint64_t objnum,objset_phys_t * objset)2003 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
2004 {
2005 dnode_phys_t dataset;
2006 dsl_dataset_phys_t *ds;
2007
2008 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
2009 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
2010 return (EIO);
2011 }
2012
2013 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
2014 if (zio_read(spa, &ds->ds_bp, objset)) {
2015 printf("ZFS: can't read object set for dataset %ju\n",
2016 (uintmax_t)objnum);
2017 return (EIO);
2018 }
2019
2020 return (0);
2021 }
2022
2023 /*
2024 * Find the object set pointed to by the BOOTFS property or the root
2025 * dataset if there is none and return its details in *objset
2026 */
2027 static int
zfs_get_root(const spa_t * spa,uint64_t * objid)2028 zfs_get_root(const spa_t *spa, uint64_t *objid)
2029 {
2030 dnode_phys_t dir, propdir;
2031 uint64_t props, bootfs, root;
2032
2033 *objid = 0;
2034
2035 /*
2036 * Start with the MOS directory object.
2037 */
2038 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
2039 printf("ZFS: can't read MOS object directory\n");
2040 return (EIO);
2041 }
2042
2043 /*
2044 * Lookup the pool_props and see if we can find a bootfs.
2045 */
2046 if (zap_lookup(spa, &dir, DMU_POOL_PROPS, &props) == 0
2047 && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
2048 && zap_lookup(spa, &propdir, "bootfs", &bootfs) == 0
2049 && bootfs != 0)
2050 {
2051 *objid = bootfs;
2052 return (0);
2053 }
2054 /*
2055 * Lookup the root dataset directory
2056 */
2057 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &root)
2058 || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
2059 printf("ZFS: can't find root dsl_dir\n");
2060 return (EIO);
2061 }
2062
2063 /*
2064 * Use the information from the dataset directory's bonus buffer
2065 * to find the dataset object and from that the object set itself.
2066 */
2067 dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
2068 *objid = dd->dd_head_dataset_obj;
2069 return (0);
2070 }
2071
2072 static int
zfs_mount(const spa_t * spa,uint64_t rootobj,struct zfsmount * mnt)2073 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mnt)
2074 {
2075
2076 mnt->spa = spa;
2077
2078 /*
2079 * Find the root object set if not explicitly provided
2080 */
2081 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
2082 printf("ZFS: can't find root filesystem\n");
2083 return (EIO);
2084 }
2085
2086 if (zfs_mount_dataset(spa, rootobj, &mnt->objset)) {
2087 printf("ZFS: can't open root filesystem\n");
2088 return (EIO);
2089 }
2090
2091 mnt->rootobj = rootobj;
2092
2093 return (0);
2094 }
2095
2096 /*
2097 * callback function for feature name checks.
2098 */
2099 static int
check_feature(const char * name,uint64_t value)2100 check_feature(const char *name, uint64_t value)
2101 {
2102 int i;
2103
2104 if (value == 0)
2105 return (0);
2106 if (name[0] == '\0')
2107 return (0);
2108
2109 for (i = 0; features_for_read[i] != NULL; i++) {
2110 if (strcmp(name, features_for_read[i]) == 0)
2111 return (0);
2112 }
2113 printf("ZFS: unsupported feature: %s\n", name);
2114 return (EIO);
2115 }
2116
2117 /*
2118 * Checks whether the MOS features that are active are supported.
2119 */
2120 static int
check_mos_features(const spa_t * spa)2121 check_mos_features(const spa_t *spa)
2122 {
2123 dnode_phys_t dir;
2124 uint64_t objnum, zap_type;
2125 size_t size;
2126 int rc;
2127
2128 if ((rc = objset_get_dnode(spa, &spa->spa_mos, DMU_OT_OBJECT_DIRECTORY,
2129 &dir)) != 0)
2130 return (rc);
2131 if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ,
2132 &objnum)) != 0) {
2133 /*
2134 * It is older pool without features. As we have already
2135 * tested the label, just return without raising the error.
2136 */
2137 if (rc == ENOENT)
2138 rc = 0;
2139 return (rc);
2140 }
2141
2142 if ((rc = objset_get_dnode(spa, &spa->spa_mos, objnum, &dir)) != 0)
2143 return (rc);
2144
2145 if (dir.dn_type != DMU_OTN_ZAP_METADATA)
2146 return (EIO);
2147
2148 size = dir.dn_datablkszsec * 512;
2149 if (dnode_read(spa, &dir, 0, zap_scratch, size))
2150 return (EIO);
2151
2152 zap_type = *(uint64_t *) zap_scratch;
2153 if (zap_type == ZBT_MICRO)
2154 rc = mzap_list(&dir, check_feature);
2155 else
2156 rc = fzap_list(spa, &dir, check_feature);
2157
2158 return (rc);
2159 }
2160
2161 static int
zfs_spa_init(spa_t * spa)2162 zfs_spa_init(spa_t *spa)
2163 {
2164 int rc;
2165
2166 if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
2167 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
2168 return (EIO);
2169 }
2170 if (spa->spa_mos.os_type != DMU_OST_META) {
2171 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
2172 return (EIO);
2173 }
2174
2175 rc = check_mos_features(spa);
2176 if (rc != 0) {
2177 printf("ZFS: pool %s is not supported\n", spa->spa_name);
2178 }
2179
2180 return (rc);
2181 }
2182
2183 static int
zfs_dnode_stat(const spa_t * spa,dnode_phys_t * dn,struct stat * sb)2184 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
2185 {
2186
2187 if (dn->dn_bonustype != DMU_OT_SA) {
2188 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
2189
2190 sb->st_mode = zp->zp_mode;
2191 sb->st_uid = zp->zp_uid;
2192 sb->st_gid = zp->zp_gid;
2193 sb->st_size = zp->zp_size;
2194 } else {
2195 sa_hdr_phys_t *sahdrp;
2196 int hdrsize;
2197 size_t size = 0;
2198 void *buf = NULL;
2199
2200 if (dn->dn_bonuslen != 0)
2201 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
2202 else {
2203 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
2204 blkptr_t *bp = DN_SPILL_BLKPTR(dn);
2205 int error;
2206
2207 size = BP_GET_LSIZE(bp);
2208 buf = zfs_alloc(size);
2209 error = zio_read(spa, bp, buf);
2210 if (error != 0) {
2211 zfs_free(buf, size);
2212 return (error);
2213 }
2214 sahdrp = buf;
2215 } else {
2216 return (EIO);
2217 }
2218 }
2219 hdrsize = SA_HDR_SIZE(sahdrp);
2220 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
2221 SA_MODE_OFFSET);
2222 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
2223 SA_UID_OFFSET);
2224 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
2225 SA_GID_OFFSET);
2226 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
2227 SA_SIZE_OFFSET);
2228 if (buf != NULL)
2229 zfs_free(buf, size);
2230 }
2231
2232 return (0);
2233 }
2234
2235 static int
zfs_dnode_readlink(const spa_t * spa,dnode_phys_t * dn,char * path,size_t psize)2236 zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize)
2237 {
2238 int rc = 0;
2239
2240 if (dn->dn_bonustype == DMU_OT_SA) {
2241 sa_hdr_phys_t *sahdrp = NULL;
2242 size_t size = 0;
2243 void *buf = NULL;
2244 int hdrsize;
2245 char *p;
2246
2247 if (dn->dn_bonuslen != 0)
2248 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
2249 else {
2250 blkptr_t *bp;
2251
2252 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0)
2253 return (EIO);
2254 bp = DN_SPILL_BLKPTR(dn);
2255
2256 size = BP_GET_LSIZE(bp);
2257 buf = zfs_alloc(size);
2258 rc = zio_read(spa, bp, buf);
2259 if (rc != 0) {
2260 zfs_free(buf, size);
2261 return (rc);
2262 }
2263 sahdrp = buf;
2264 }
2265 hdrsize = SA_HDR_SIZE(sahdrp);
2266 p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET);
2267 memcpy(path, p, psize);
2268 if (buf != NULL)
2269 zfs_free(buf, size);
2270 return (0);
2271 }
2272 /*
2273 * Second test is purely to silence bogus compiler
2274 * warning about accessing past the end of dn_bonus.
2275 */
2276 if (psize + sizeof(znode_phys_t) <= dn->dn_bonuslen &&
2277 sizeof(znode_phys_t) <= sizeof(dn->dn_bonus)) {
2278 memcpy(path, &dn->dn_bonus[sizeof(znode_phys_t)], psize);
2279 } else {
2280 rc = dnode_read(spa, dn, 0, path, psize);
2281 }
2282 return (rc);
2283 }
2284
2285 struct obj_list {
2286 uint64_t objnum;
2287 STAILQ_ENTRY(obj_list) entry;
2288 };
2289
2290 /*
2291 * Lookup a file and return its dnode.
2292 */
2293 static int
zfs_lookup(const struct zfsmount * mnt,const char * upath,dnode_phys_t * dnode)2294 zfs_lookup(const struct zfsmount *mnt, const char *upath, dnode_phys_t *dnode)
2295 {
2296 int rc;
2297 uint64_t objnum;
2298 const spa_t *spa;
2299 dnode_phys_t dn;
2300 const char *p, *q;
2301 char element[256];
2302 char path[1024];
2303 int symlinks_followed = 0;
2304 struct stat sb;
2305 struct obj_list *entry, *tentry;
2306 STAILQ_HEAD(, obj_list) on_cache = STAILQ_HEAD_INITIALIZER(on_cache);
2307
2308 spa = mnt->spa;
2309 if (mnt->objset.os_type != DMU_OST_ZFS) {
2310 printf("ZFS: unexpected object set type %ju\n",
2311 (uintmax_t)mnt->objset.os_type);
2312 return (EIO);
2313 }
2314
2315 if ((entry = malloc(sizeof(struct obj_list))) == NULL)
2316 return (ENOMEM);
2317
2318 /*
2319 * Get the root directory dnode.
2320 */
2321 rc = objset_get_dnode(spa, &mnt->objset, MASTER_NODE_OBJ, &dn);
2322 if (rc) {
2323 free(entry);
2324 return (rc);
2325 }
2326
2327 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, &objnum);
2328 if (rc) {
2329 free(entry);
2330 return (rc);
2331 }
2332 entry->objnum = objnum;
2333 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
2334
2335 rc = objset_get_dnode(spa, &mnt->objset, objnum, &dn);
2336 if (rc != 0)
2337 goto done;
2338
2339 p = upath;
2340 while (p && *p) {
2341 rc = objset_get_dnode(spa, &mnt->objset, objnum, &dn);
2342 if (rc != 0)
2343 goto done;
2344
2345 while (*p == '/')
2346 p++;
2347 if (*p == '\0')
2348 break;
2349 q = p;
2350 while (*q != '\0' && *q != '/')
2351 q++;
2352
2353 /* skip dot */
2354 if (p + 1 == q && p[0] == '.') {
2355 p++;
2356 continue;
2357 }
2358 /* double dot */
2359 if (p + 2 == q && p[0] == '.' && p[1] == '.') {
2360 p += 2;
2361 if (STAILQ_FIRST(&on_cache) ==
2362 STAILQ_LAST(&on_cache, obj_list, entry)) {
2363 rc = ENOENT;
2364 goto done;
2365 }
2366 entry = STAILQ_FIRST(&on_cache);
2367 STAILQ_REMOVE_HEAD(&on_cache, entry);
2368 free(entry);
2369 objnum = (STAILQ_FIRST(&on_cache))->objnum;
2370 continue;
2371 }
2372 if (q - p + 1 > sizeof(element)) {
2373 rc = ENAMETOOLONG;
2374 goto done;
2375 }
2376 memcpy(element, p, q - p);
2377 element[q - p] = 0;
2378 p = q;
2379
2380 if ((rc = zfs_dnode_stat(spa, &dn, &sb)) != 0)
2381 goto done;
2382 if (!S_ISDIR(sb.st_mode)) {
2383 rc = ENOTDIR;
2384 goto done;
2385 }
2386
2387 rc = zap_lookup(spa, &dn, element, &objnum);
2388 if (rc)
2389 goto done;
2390 objnum = ZFS_DIRENT_OBJ(objnum);
2391
2392 if ((entry = malloc(sizeof(struct obj_list))) == NULL) {
2393 rc = ENOMEM;
2394 goto done;
2395 }
2396 entry->objnum = objnum;
2397 STAILQ_INSERT_HEAD(&on_cache, entry, entry);
2398 rc = objset_get_dnode(spa, &mnt->objset, objnum, &dn);
2399 if (rc)
2400 goto done;
2401
2402 /*
2403 * Check for symlink.
2404 */
2405 rc = zfs_dnode_stat(spa, &dn, &sb);
2406 if (rc)
2407 goto done;
2408 if (S_ISLNK(sb.st_mode)) {
2409 if (symlinks_followed > 10) {
2410 rc = EMLINK;
2411 goto done;
2412 }
2413 symlinks_followed++;
2414
2415 /*
2416 * Read the link value and copy the tail of our
2417 * current path onto the end.
2418 */
2419 if (sb.st_size + strlen(p) + 1 > sizeof(path)) {
2420 rc = ENAMETOOLONG;
2421 goto done;
2422 }
2423 strcpy(&path[sb.st_size], p);
2424
2425 rc = zfs_dnode_readlink(spa, &dn, path, sb.st_size);
2426 if (rc != 0)
2427 goto done;
2428
2429 /*
2430 * Restart with the new path, starting either at
2431 * the root or at the parent depending whether or
2432 * not the link is relative.
2433 */
2434 p = path;
2435 if (*p == '/') {
2436 while (STAILQ_FIRST(&on_cache) !=
2437 STAILQ_LAST(&on_cache, obj_list, entry)) {
2438 entry = STAILQ_FIRST(&on_cache);
2439 STAILQ_REMOVE_HEAD(&on_cache, entry);
2440 free(entry);
2441 }
2442 } else {
2443 entry = STAILQ_FIRST(&on_cache);
2444 STAILQ_REMOVE_HEAD(&on_cache, entry);
2445 free(entry);
2446 }
2447 objnum = (STAILQ_FIRST(&on_cache))->objnum;
2448 }
2449 }
2450
2451 *dnode = dn;
2452 done:
2453 STAILQ_FOREACH_SAFE(entry, &on_cache, entry, tentry)
2454 free(entry);
2455 return (rc);
2456 }
2457