]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/boot/zfs/zfsimpl.c
MFC r241283: zfs boot: add code for listing child datasets of a given
[FreeBSD/stable/9.git] / sys / boot / zfs / zfsimpl.c
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 __FBSDID("$FreeBSD$");
29
30 /*
31  *      Stand-alone ZFS file reader.
32  */
33
34 #include <sys/stat.h>
35 #include <sys/stdint.h>
36
37 #include "zfsimpl.h"
38 #include "zfssubr.c"
39
40
41 struct zfsmount {
42         const spa_t     *spa;
43         objset_phys_t   objset;
44         uint64_t        rootobj;
45 };
46
47 /*
48  * List of all vdevs, chained through v_alllink.
49  */
50 static vdev_list_t zfs_vdevs;
51
52 /*
53  * List of all pools, chained through spa_link.
54  */
55 static spa_list_t zfs_pools;
56
57 static uint64_t zfs_crc64_table[256];
58 static const dnode_phys_t *dnode_cache_obj = 0;
59 static uint64_t dnode_cache_bn;
60 static char *dnode_cache_buf;
61 static char *zap_scratch;
62 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
63
64 #define TEMP_SIZE       (1024 * 1024)
65
66 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
67 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
68 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
69
70 static void
71 zfs_init(void)
72 {
73         STAILQ_INIT(&zfs_vdevs);
74         STAILQ_INIT(&zfs_pools);
75
76         zfs_temp_buf = malloc(TEMP_SIZE);
77         zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
78         zfs_temp_ptr = zfs_temp_buf;
79         dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
80         zap_scratch = malloc(SPA_MAXBLOCKSIZE);
81
82         zfs_init_crc();
83 }
84
85 static void *
86 zfs_alloc(size_t size)
87 {
88         char *ptr;
89
90         if (zfs_temp_ptr + size > zfs_temp_end) {
91                 printf("ZFS: out of temporary buffer space\n");
92                 for (;;) ;
93         }
94         ptr = zfs_temp_ptr;
95         zfs_temp_ptr += size;
96
97         return (ptr);
98 }
99
100 static void
101 zfs_free(void *ptr, size_t size)
102 {
103
104         zfs_temp_ptr -= size;
105         if (zfs_temp_ptr != ptr) {
106                 printf("ZFS: zfs_alloc()/zfs_free() mismatch\n");
107                 for (;;) ;
108         }
109 }
110
111 static int
112 xdr_int(const unsigned char **xdr, int *ip)
113 {
114         *ip = ((*xdr)[0] << 24)
115                 | ((*xdr)[1] << 16)
116                 | ((*xdr)[2] << 8)
117                 | ((*xdr)[3] << 0);
118         (*xdr) += 4;
119         return (0);
120 }
121
122 static int
123 xdr_u_int(const unsigned char **xdr, u_int *ip)
124 {
125         *ip = ((*xdr)[0] << 24)
126                 | ((*xdr)[1] << 16)
127                 | ((*xdr)[2] << 8)
128                 | ((*xdr)[3] << 0);
129         (*xdr) += 4;
130         return (0);
131 }
132
133 static int
134 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
135 {
136         u_int hi, lo;
137
138         xdr_u_int(xdr, &hi);
139         xdr_u_int(xdr, &lo);
140         *lp = (((uint64_t) hi) << 32) | lo;
141         return (0);
142 }
143
144 static int
145 nvlist_find(const unsigned char *nvlist, const char *name, int type,
146             int* elementsp, void *valuep)
147 {
148         const unsigned char *p, *pair;
149         int junk;
150         int encoded_size, decoded_size;
151
152         p = nvlist;
153         xdr_int(&p, &junk);
154         xdr_int(&p, &junk);
155
156         pair = p;
157         xdr_int(&p, &encoded_size);
158         xdr_int(&p, &decoded_size);
159         while (encoded_size && decoded_size) {
160                 int namelen, pairtype, elements;
161                 const char *pairname;
162
163                 xdr_int(&p, &namelen);
164                 pairname = (const char*) p;
165                 p += roundup(namelen, 4);
166                 xdr_int(&p, &pairtype);
167
168                 if (!memcmp(name, pairname, namelen) && type == pairtype) {
169                         xdr_int(&p, &elements);
170                         if (elementsp)
171                                 *elementsp = elements;
172                         if (type == DATA_TYPE_UINT64) {
173                                 xdr_uint64_t(&p, (uint64_t *) valuep);
174                                 return (0);
175                         } else if (type == DATA_TYPE_STRING) {
176                                 int len;
177                                 xdr_int(&p, &len);
178                                 (*(const char**) valuep) = (const char*) p;
179                                 return (0);
180                         } else if (type == DATA_TYPE_NVLIST
181                                    || type == DATA_TYPE_NVLIST_ARRAY) {
182                                 (*(const unsigned char**) valuep) =
183                                          (const unsigned char*) p;
184                                 return (0);
185                         } else {
186                                 return (EIO);
187                         }
188                 } else {
189                         /*
190                          * Not the pair we are looking for, skip to the next one.
191                          */
192                         p = pair + encoded_size;
193                 }
194
195                 pair = p;
196                 xdr_int(&p, &encoded_size);
197                 xdr_int(&p, &decoded_size);
198         }
199
200         return (EIO);
201 }
202
203 /*
204  * Return the next nvlist in an nvlist array.
205  */
206 static const unsigned char *
207 nvlist_next(const unsigned char *nvlist)
208 {
209         const unsigned char *p, *pair;
210         int junk;
211         int encoded_size, decoded_size;
212
213         p = nvlist;
214         xdr_int(&p, &junk);
215         xdr_int(&p, &junk);
216
217         pair = p;
218         xdr_int(&p, &encoded_size);
219         xdr_int(&p, &decoded_size);
220         while (encoded_size && decoded_size) {
221                 p = pair + encoded_size;
222
223                 pair = p;
224                 xdr_int(&p, &encoded_size);
225                 xdr_int(&p, &decoded_size);
226         }
227
228         return p;
229 }
230
231 #ifdef TEST
232
233 static const unsigned char *
234 nvlist_print(const unsigned char *nvlist, unsigned int indent)
235 {
236         static const char* typenames[] = {
237                 "DATA_TYPE_UNKNOWN",
238                 "DATA_TYPE_BOOLEAN",
239                 "DATA_TYPE_BYTE",
240                 "DATA_TYPE_INT16",
241                 "DATA_TYPE_UINT16",
242                 "DATA_TYPE_INT32",
243                 "DATA_TYPE_UINT32",
244                 "DATA_TYPE_INT64",
245                 "DATA_TYPE_UINT64",
246                 "DATA_TYPE_STRING",
247                 "DATA_TYPE_BYTE_ARRAY",
248                 "DATA_TYPE_INT16_ARRAY",
249                 "DATA_TYPE_UINT16_ARRAY",
250                 "DATA_TYPE_INT32_ARRAY",
251                 "DATA_TYPE_UINT32_ARRAY",
252                 "DATA_TYPE_INT64_ARRAY",
253                 "DATA_TYPE_UINT64_ARRAY",
254                 "DATA_TYPE_STRING_ARRAY",
255                 "DATA_TYPE_HRTIME",
256                 "DATA_TYPE_NVLIST",
257                 "DATA_TYPE_NVLIST_ARRAY",
258                 "DATA_TYPE_BOOLEAN_VALUE",
259                 "DATA_TYPE_INT8",
260                 "DATA_TYPE_UINT8",
261                 "DATA_TYPE_BOOLEAN_ARRAY",
262                 "DATA_TYPE_INT8_ARRAY",
263                 "DATA_TYPE_UINT8_ARRAY"
264         };
265
266         unsigned int i, j;
267         const unsigned char *p, *pair;
268         int junk;
269         int encoded_size, decoded_size;
270
271         p = nvlist;
272         xdr_int(&p, &junk);
273         xdr_int(&p, &junk);
274
275         pair = p;
276         xdr_int(&p, &encoded_size);
277         xdr_int(&p, &decoded_size);
278         while (encoded_size && decoded_size) {
279                 int namelen, pairtype, elements;
280                 const char *pairname;
281
282                 xdr_int(&p, &namelen);
283                 pairname = (const char*) p;
284                 p += roundup(namelen, 4);
285                 xdr_int(&p, &pairtype);
286
287                 for (i = 0; i < indent; i++)
288                         printf(" ");
289                 printf("%s %s", typenames[pairtype], pairname);
290
291                 xdr_int(&p, &elements);
292                 switch (pairtype) {
293                 case DATA_TYPE_UINT64: {
294                         uint64_t val;
295                         xdr_uint64_t(&p, &val);
296                         printf(" = 0x%jx\n", (uintmax_t)val);
297                         break;
298                 }
299
300                 case DATA_TYPE_STRING: {
301                         int len;
302                         xdr_int(&p, &len);
303                         printf(" = \"%s\"\n", p);
304                         break;
305                 }
306
307                 case DATA_TYPE_NVLIST:
308                         printf("\n");
309                         nvlist_print(p, indent + 1);
310                         break;
311
312                 case DATA_TYPE_NVLIST_ARRAY:
313                         for (j = 0; j < elements; j++) {
314                                 printf("[%d]\n", j);
315                                 p = nvlist_print(p, indent + 1);
316                                 if (j != elements - 1) {
317                                         for (i = 0; i < indent; i++)
318                                                 printf(" ");
319                                         printf("%s %s", typenames[pairtype], pairname);
320                                 }
321                         }
322                         break;
323
324                 default:
325                         printf("\n");
326                 }
327
328                 p = pair + encoded_size;
329
330                 pair = p;
331                 xdr_int(&p, &encoded_size);
332                 xdr_int(&p, &decoded_size);
333         }
334
335         return p;
336 }
337
338 #endif
339
340 static int
341 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
342     off_t offset, size_t size)
343 {
344         size_t psize;
345         int rc;
346
347         if (!vdev->v_phys_read)
348                 return (EIO);
349
350         if (bp) {
351                 psize = BP_GET_PSIZE(bp);
352         } else {
353                 psize = size;
354         }
355
356         /*printf("ZFS: reading %d bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/
357         rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
358         if (rc)
359                 return (rc);
360         if (bp && zio_checksum_verify(bp, buf))
361                 return (EIO);
362
363         return (0);
364 }
365
366 static int
367 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
368     off_t offset, size_t bytes)
369 {
370
371         return (vdev_read_phys(vdev, bp, buf,
372                 offset + VDEV_LABEL_START_SIZE, bytes));
373 }
374
375
376 static int
377 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
378     off_t offset, size_t bytes)
379 {
380         vdev_t *kid;
381         int rc;
382
383         rc = EIO;
384         STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
385                 if (kid->v_state != VDEV_STATE_HEALTHY)
386                         continue;
387                 rc = kid->v_read(kid, bp, buf, offset, bytes);
388                 if (!rc)
389                         return (0);
390         }
391
392         return (rc);
393 }
394
395 static int
396 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
397     off_t offset, size_t bytes)
398 {
399         vdev_t *kid;
400
401         /*
402          * Here we should have two kids:
403          * First one which is the one we are replacing and we can trust
404          * only this one to have valid data, but it might not be present.
405          * Second one is that one we are replacing with. It is most likely
406          * healthy, but we can't trust it has needed data, so we won't use it.
407          */
408         kid = STAILQ_FIRST(&vdev->v_children);
409         if (kid == NULL)
410                 return (EIO);
411         if (kid->v_state != VDEV_STATE_HEALTHY)
412                 return (EIO);
413         return (kid->v_read(kid, bp, buf, offset, bytes));
414 }
415
416 static vdev_t *
417 vdev_find(uint64_t guid)
418 {
419         vdev_t *vdev;
420
421         STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
422                 if (vdev->v_guid == guid)
423                         return (vdev);
424
425         return (0);
426 }
427
428 static vdev_t *
429 vdev_create(uint64_t guid, vdev_read_t *read)
430 {
431         vdev_t *vdev;
432
433         vdev = malloc(sizeof(vdev_t));
434         memset(vdev, 0, sizeof(vdev_t));
435         STAILQ_INIT(&vdev->v_children);
436         vdev->v_guid = guid;
437         vdev->v_state = VDEV_STATE_OFFLINE;
438         vdev->v_read = read;
439         vdev->v_phys_read = 0;
440         vdev->v_read_priv = 0;
441         STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
442
443         return (vdev);
444 }
445
446 static int
447 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
448     vdev_t **vdevp, int is_newer)
449 {
450         int rc;
451         uint64_t guid, id, ashift, nparity;
452         const char *type;
453         const char *path;
454         vdev_t *vdev, *kid;
455         const unsigned char *kids;
456         int nkids, i, is_new;
457         uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
458
459         if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID,
460                         DATA_TYPE_UINT64, 0, &guid)
461             || nvlist_find(nvlist, ZPOOL_CONFIG_ID,
462                            DATA_TYPE_UINT64, 0, &id)
463             || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE,
464                            DATA_TYPE_STRING, 0, &type)) {
465                 printf("ZFS: can't find vdev details\n");
466                 return (ENOENT);
467         }
468
469         if (strcmp(type, VDEV_TYPE_MIRROR)
470             && strcmp(type, VDEV_TYPE_DISK)
471 #ifdef ZFS_TEST
472             && strcmp(type, VDEV_TYPE_FILE)
473 #endif
474             && strcmp(type, VDEV_TYPE_RAIDZ)
475             && strcmp(type, VDEV_TYPE_REPLACING)) {
476                 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
477                 return (EIO);
478         }
479
480         is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
481
482         nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, 0,
483                         &is_offline);
484         nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, 0,
485                         &is_removed);
486         nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, 0,
487                         &is_faulted);
488         nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, 0,
489                         &is_degraded);
490         nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, 0,
491                         &isnt_present);
492
493         vdev = vdev_find(guid);
494         if (!vdev) {
495                 is_new = 1;
496
497                 if (!strcmp(type, VDEV_TYPE_MIRROR))
498                         vdev = vdev_create(guid, vdev_mirror_read);
499                 else if (!strcmp(type, VDEV_TYPE_RAIDZ))
500                         vdev = vdev_create(guid, vdev_raidz_read);
501                 else if (!strcmp(type, VDEV_TYPE_REPLACING))
502                         vdev = vdev_create(guid, vdev_replacing_read);
503                 else
504                         vdev = vdev_create(guid, vdev_disk_read);
505
506                 vdev->v_id = id;
507                 vdev->v_top = pvdev != NULL ? pvdev : vdev;
508                 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
509                         DATA_TYPE_UINT64, 0, &ashift) == 0)
510                         vdev->v_ashift = ashift;
511                 else
512                         vdev->v_ashift = 0;
513                 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
514                         DATA_TYPE_UINT64, 0, &nparity) == 0)
515                         vdev->v_nparity = nparity;
516                 else
517                         vdev->v_nparity = 0;
518                 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
519                                 DATA_TYPE_STRING, 0, &path) == 0) {
520                         if (strncmp(path, "/dev/", 5) == 0)
521                                 path += 5;
522                         vdev->v_name = strdup(path);
523                 } else {
524                         if (!strcmp(type, "raidz")) {
525                                 if (vdev->v_nparity == 1)
526                                         vdev->v_name = "raidz1";
527                                 else if (vdev->v_nparity == 2)
528                                         vdev->v_name = "raidz2";
529                                 else if (vdev->v_nparity == 3)
530                                         vdev->v_name = "raidz3";
531                                 else {
532                                         printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
533                                         return (EIO);
534                                 }
535                         } else {
536                                 vdev->v_name = strdup(type);
537                         }
538                 }
539         } else {
540                 is_new = 0;
541         }
542
543         if (is_new || is_newer) {
544                 /*
545                  * This is either new vdev or we've already seen this vdev,
546                  * but from an older vdev label, so let's refresh its state
547                  * from the newer label.
548                  */
549                 if (is_offline)
550                         vdev->v_state = VDEV_STATE_OFFLINE;
551                 else if (is_removed)
552                         vdev->v_state = VDEV_STATE_REMOVED;
553                 else if (is_faulted)
554                         vdev->v_state = VDEV_STATE_FAULTED;
555                 else if (is_degraded)
556                         vdev->v_state = VDEV_STATE_DEGRADED;
557                 else if (isnt_present)
558                         vdev->v_state = VDEV_STATE_CANT_OPEN;
559         }
560
561         rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN,
562                          DATA_TYPE_NVLIST_ARRAY, &nkids, &kids);
563         /*
564          * Its ok if we don't have any kids.
565          */
566         if (rc == 0) {
567                 vdev->v_nchildren = nkids;
568                 for (i = 0; i < nkids; i++) {
569                         rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
570                         if (rc)
571                                 return (rc);
572                         if (is_new)
573                                 STAILQ_INSERT_TAIL(&vdev->v_children, kid,
574                                                    v_childlink);
575                         kids = nvlist_next(kids);
576                 }
577         } else {
578                 vdev->v_nchildren = 0;
579         }
580
581         if (vdevp)
582                 *vdevp = vdev;
583         return (0);
584 }
585
586 static void
587 vdev_set_state(vdev_t *vdev)
588 {
589         vdev_t *kid;
590         int good_kids;
591         int bad_kids;
592
593         /*
594          * A mirror or raidz is healthy if all its kids are healthy. A
595          * mirror is degraded if any of its kids is healthy; a raidz
596          * is degraded if at most nparity kids are offline.
597          */
598         if (STAILQ_FIRST(&vdev->v_children)) {
599                 good_kids = 0;
600                 bad_kids = 0;
601                 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
602                         if (kid->v_state == VDEV_STATE_HEALTHY)
603                                 good_kids++;
604                         else
605                                 bad_kids++;
606                 }
607                 if (bad_kids == 0) {
608                         vdev->v_state = VDEV_STATE_HEALTHY;
609                 } else {
610                         if (vdev->v_read == vdev_mirror_read) {
611                                 if (good_kids) {
612                                         vdev->v_state = VDEV_STATE_DEGRADED;
613                                 } else {
614                                         vdev->v_state = VDEV_STATE_OFFLINE;
615                                 }
616                         } else if (vdev->v_read == vdev_raidz_read) {
617                                 if (bad_kids > vdev->v_nparity) {
618                                         vdev->v_state = VDEV_STATE_OFFLINE;
619                                 } else {
620                                         vdev->v_state = VDEV_STATE_DEGRADED;
621                                 }
622                         }
623                 }
624         }
625 }
626
627 static spa_t *
628 spa_find_by_guid(uint64_t guid)
629 {
630         spa_t *spa;
631
632         STAILQ_FOREACH(spa, &zfs_pools, spa_link)
633                 if (spa->spa_guid == guid)
634                         return (spa);
635
636         return (0);
637 }
638
639 static spa_t *
640 spa_find_by_name(const char *name)
641 {
642         spa_t *spa;
643
644         STAILQ_FOREACH(spa, &zfs_pools, spa_link)
645                 if (!strcmp(spa->spa_name, name))
646                         return (spa);
647
648         return (0);
649 }
650
651 static spa_t *
652 spa_create(uint64_t guid)
653 {
654         spa_t *spa;
655
656         spa = malloc(sizeof(spa_t));
657         memset(spa, 0, sizeof(spa_t));
658         STAILQ_INIT(&spa->spa_vdevs);
659         spa->spa_guid = guid;
660         STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
661
662         return (spa);
663 }
664
665 static const char *
666 state_name(vdev_state_t state)
667 {
668         static const char* names[] = {
669                 "UNKNOWN",
670                 "CLOSED",
671                 "OFFLINE",
672                 "REMOVED",
673                 "CANT_OPEN",
674                 "FAULTED",
675                 "DEGRADED",
676                 "ONLINE"
677         };
678         return names[state];
679 }
680
681 #ifdef BOOT2
682
683 #define pager_printf printf
684
685 #else
686
687 static void
688 pager_printf(const char *fmt, ...)
689 {
690         char line[80];
691         va_list args;
692
693         va_start(args, fmt);
694         vsprintf(line, fmt, args);
695         va_end(args);
696         pager_output(line);
697 }
698
699 #endif
700
701 #define STATUS_FORMAT   "        %s %s\n"
702
703 static void
704 print_state(int indent, const char *name, vdev_state_t state)
705 {
706         int i;
707         char buf[512];
708
709         buf[0] = 0;
710         for (i = 0; i < indent; i++)
711                 strcat(buf, "  ");
712         strcat(buf, name);
713         pager_printf(STATUS_FORMAT, buf, state_name(state));
714         
715 }
716
717 static void
718 vdev_status(vdev_t *vdev, int indent)
719 {
720         vdev_t *kid;
721         print_state(indent, vdev->v_name, vdev->v_state);
722
723         STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
724                 vdev_status(kid, indent + 1);
725         }
726 }
727
728 static void
729 spa_status(spa_t *spa)
730 {
731         static char bootfs[ZFS_MAXNAMELEN];
732         uint64_t rootid;
733         vdev_t *vdev;
734         int good_kids, bad_kids, degraded_kids;
735         vdev_state_t state;
736
737         pager_printf("  pool: %s\n", spa->spa_name);
738         if (zfs_get_root(spa, &rootid) == 0 &&
739             zfs_rlookup(spa, rootid, bootfs) == 0) {
740                 if (bootfs[0] == '\0')
741                         pager_printf("bootfs: %s\n", spa->spa_name);
742                 else
743                         pager_printf("bootfs: %s/%s\n", spa->spa_name, bootfs);
744         }
745         pager_printf("config:\n\n");
746         pager_printf(STATUS_FORMAT, "NAME", "STATE");
747
748         good_kids = 0;
749         degraded_kids = 0;
750         bad_kids = 0;
751         STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
752                 if (vdev->v_state == VDEV_STATE_HEALTHY)
753                         good_kids++;
754                 else if (vdev->v_state == VDEV_STATE_DEGRADED)
755                         degraded_kids++;
756                 else
757                         bad_kids++;
758         }
759
760         state = VDEV_STATE_CLOSED;
761         if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
762                 state = VDEV_STATE_HEALTHY;
763         else if ((good_kids + degraded_kids) > 0)
764                 state = VDEV_STATE_DEGRADED;
765
766         print_state(0, spa->spa_name, state);
767         STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
768                 vdev_status(vdev, 1);
769         }
770 }
771
772 static void
773 spa_all_status(void)
774 {
775         spa_t *spa;
776         int first = 1;
777
778         STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
779                 if (!first)
780                         pager_printf("\n");
781                 first = 0;
782                 spa_status(spa);
783         }
784 }
785
786 static int
787 vdev_probe(vdev_phys_read_t *read, void *read_priv, spa_t **spap)
788 {
789         vdev_t vtmp;
790         vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
791         spa_t *spa;
792         vdev_t *vdev, *top_vdev, *pool_vdev;
793         off_t off;
794         blkptr_t bp;
795         const unsigned char *nvlist;
796         uint64_t val;
797         uint64_t guid;
798         uint64_t pool_txg, pool_guid;
799         uint64_t is_log;
800         const char *pool_name;
801         const unsigned char *vdevs;
802         int i, rc, is_newer;
803         char *upbuf;
804         const struct uberblock *up;
805
806         /*
807          * Load the vdev label and figure out which
808          * uberblock is most current.
809          */
810         memset(&vtmp, 0, sizeof(vtmp));
811         vtmp.v_phys_read = read;
812         vtmp.v_read_priv = read_priv;
813         off = offsetof(vdev_label_t, vl_vdev_phys);
814         BP_ZERO(&bp);
815         BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
816         BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
817         BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
818         BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
819         DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
820         ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
821         if (vdev_read_phys(&vtmp, &bp, vdev_label, off, 0))
822                 return (EIO);
823
824         if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR) {
825                 return (EIO);
826         }
827
828         nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
829
830         if (nvlist_find(nvlist,
831                         ZPOOL_CONFIG_VERSION,
832                         DATA_TYPE_UINT64, 0, &val)) {
833                 return (EIO);
834         }
835
836         if (val > SPA_VERSION) {
837                 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
838                     (unsigned) val, (unsigned) SPA_VERSION);
839                 return (EIO);
840         }
841
842         if (nvlist_find(nvlist,
843                         ZPOOL_CONFIG_POOL_STATE,
844                         DATA_TYPE_UINT64, 0, &val)) {
845                 return (EIO);
846         }
847
848         if (val == POOL_STATE_DESTROYED) {
849                 /* We don't boot only from destroyed pools. */
850                 return (EIO);
851         }
852
853         if (nvlist_find(nvlist,
854                         ZPOOL_CONFIG_POOL_TXG,
855                         DATA_TYPE_UINT64, 0, &pool_txg)
856             || nvlist_find(nvlist,
857                            ZPOOL_CONFIG_POOL_GUID,
858                            DATA_TYPE_UINT64, 0, &pool_guid)
859             || nvlist_find(nvlist,
860                            ZPOOL_CONFIG_POOL_NAME,
861                            DATA_TYPE_STRING, 0, &pool_name)) {
862                 /*
863                  * Cache and spare devices end up here - just ignore
864                  * them.
865                  */
866                 /*printf("ZFS: can't find pool details\n");*/
867                 return (EIO);
868         }
869
870         is_log = 0;
871         (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, 0,
872             &is_log);
873         if (is_log)
874                 return (EIO);
875
876         /*
877          * Create the pool if this is the first time we've seen it.
878          */
879         spa = spa_find_by_guid(pool_guid);
880         if (!spa) {
881                 spa = spa_create(pool_guid);
882                 spa->spa_name = strdup(pool_name);
883         }
884         if (pool_txg > spa->spa_txg) {
885                 spa->spa_txg = pool_txg;
886                 is_newer = 1;
887         } else
888                 is_newer = 0;
889
890         /*
891          * Get the vdev tree and create our in-core copy of it.
892          * If we already have a vdev with this guid, this must
893          * be some kind of alias (overlapping slices, dangerously dedicated
894          * disks etc).
895          */
896         if (nvlist_find(nvlist,
897                         ZPOOL_CONFIG_GUID,
898                         DATA_TYPE_UINT64, 0, &guid)) {
899                 return (EIO);
900         }
901         vdev = vdev_find(guid);
902         if (vdev && vdev->v_phys_read)  /* Has this vdev already been inited? */
903                 return (EIO);
904
905         if (nvlist_find(nvlist,
906                         ZPOOL_CONFIG_VDEV_TREE,
907                         DATA_TYPE_NVLIST, 0, &vdevs)) {
908                 return (EIO);
909         }
910
911         rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer);
912         if (rc)
913                 return (rc);
914
915         /*
916          * Add the toplevel vdev to the pool if its not already there.
917          */
918         STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
919                 if (top_vdev == pool_vdev)
920                         break;
921         if (!pool_vdev && top_vdev)
922                 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
923
924         /*
925          * We should already have created an incomplete vdev for this
926          * vdev. Find it and initialise it with our read proc.
927          */
928         vdev = vdev_find(guid);
929         if (vdev) {
930                 vdev->v_phys_read = read;
931                 vdev->v_read_priv = read_priv;
932                 vdev->v_state = VDEV_STATE_HEALTHY;
933         } else {
934                 printf("ZFS: inconsistent nvlist contents\n");
935                 return (EIO);
936         }
937
938         /*
939          * Re-evaluate top-level vdev state.
940          */
941         vdev_set_state(top_vdev);
942
943         /*
944          * Ok, we are happy with the pool so far. Lets find
945          * the best uberblock and then we can actually access
946          * the contents of the pool.
947          */
948         upbuf = zfs_alloc(VDEV_UBERBLOCK_SIZE(vdev));
949         up = (const struct uberblock *)upbuf;
950         for (i = 0;
951              i < VDEV_UBERBLOCK_COUNT(vdev);
952              i++) {
953                 off = VDEV_UBERBLOCK_OFFSET(vdev, i);
954                 BP_ZERO(&bp);
955                 DVA_SET_OFFSET(&bp.blk_dva[0], off);
956                 BP_SET_LSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
957                 BP_SET_PSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
958                 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
959                 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
960                 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
961
962                 if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
963                         continue;
964
965                 if (up->ub_magic != UBERBLOCK_MAGIC)
966                         continue;
967                 if (up->ub_txg < spa->spa_txg)
968                         continue;
969                 if (up->ub_txg > spa->spa_uberblock.ub_txg) {
970                         spa->spa_uberblock = *up;
971                 } else if (up->ub_txg == spa->spa_uberblock.ub_txg) {
972                         if (up->ub_timestamp > spa->spa_uberblock.ub_timestamp)
973                                 spa->spa_uberblock = *up;
974                 }
975         }
976         zfs_free(upbuf, VDEV_UBERBLOCK_SIZE(vdev));
977
978         if (spap)
979                 *spap = spa;
980         return (0);
981 }
982
983 static int
984 ilog2(int n)
985 {
986         int v;
987
988         for (v = 0; v < 32; v++)
989                 if (n == (1 << v))
990                         return v;
991         return -1;
992 }
993
994 static int
995 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
996 {
997         blkptr_t gbh_bp;
998         zio_gbh_phys_t zio_gb;
999         char *pbuf;
1000         int i;
1001
1002         /* Artificial BP for gang block header. */
1003         gbh_bp = *bp;
1004         BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1005         BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1006         BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
1007         BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
1008         for (i = 0; i < SPA_DVAS_PER_BP; i++)
1009                 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
1010
1011         /* Read gang header block using the artificial BP. */
1012         if (zio_read(spa, &gbh_bp, &zio_gb))
1013                 return (EIO);
1014
1015         pbuf = buf;
1016         for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1017                 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
1018
1019                 if (BP_IS_HOLE(gbp))
1020                         continue;
1021                 if (zio_read(spa, gbp, pbuf))
1022                         return (EIO);
1023                 pbuf += BP_GET_PSIZE(gbp);
1024         }
1025
1026         if (zio_checksum_verify(bp, buf))
1027                 return (EIO);
1028         return (0);
1029 }
1030
1031 static int
1032 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
1033 {
1034         int cpfunc = BP_GET_COMPRESS(bp);
1035         uint64_t align, size;
1036         void *pbuf;
1037         int i, error;
1038
1039         error = EIO;
1040
1041         for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1042                 const dva_t *dva = &bp->blk_dva[i];
1043                 vdev_t *vdev;
1044                 int vdevid;
1045                 off_t offset;
1046
1047                 if (!dva->dva_word[0] && !dva->dva_word[1])
1048                         continue;
1049
1050                 vdevid = DVA_GET_VDEV(dva);
1051                 offset = DVA_GET_OFFSET(dva);
1052                 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1053                         if (vdev->v_id == vdevid)
1054                                 break;
1055                 }
1056                 if (!vdev || !vdev->v_read)
1057                         continue;
1058
1059                 size = BP_GET_PSIZE(bp);
1060                 if (vdev->v_read == vdev_raidz_read) {
1061                         align = 1ULL << vdev->v_top->v_ashift;
1062                         if (P2PHASE(size, align) != 0)
1063                                 size = P2ROUNDUP(size, align);
1064                 }
1065                 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
1066                         pbuf = zfs_alloc(size);
1067                 else
1068                         pbuf = buf;
1069
1070                 if (DVA_GET_GANG(dva))
1071                         error = zio_read_gang(spa, bp, pbuf);
1072                 else
1073                         error = vdev->v_read(vdev, bp, pbuf, offset, size);
1074                 if (error == 0) {
1075                         if (cpfunc != ZIO_COMPRESS_OFF)
1076                                 error = zio_decompress_data(cpfunc, pbuf,
1077                                     BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
1078                         else if (size != BP_GET_PSIZE(bp))
1079                                 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
1080                 }
1081                 if (buf != pbuf)
1082                         zfs_free(pbuf, size);
1083                 if (error == 0)
1084                         break;
1085         }
1086         if (error != 0)
1087                 printf("ZFS: i/o error - all block copies unavailable\n");
1088         return (error);
1089 }
1090
1091 static int
1092 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1093 {
1094         int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1095         int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1096         int nlevels = dnode->dn_nlevels;
1097         int i, rc;
1098
1099         /*
1100          * Note: bsize may not be a power of two here so we need to do an
1101          * actual divide rather than a bitshift.
1102          */
1103         while (buflen > 0) {
1104                 uint64_t bn = offset / bsize;
1105                 int boff = offset % bsize;
1106                 int ibn;
1107                 const blkptr_t *indbp;
1108                 blkptr_t bp;
1109
1110                 if (bn > dnode->dn_maxblkid)
1111                         return (EIO);
1112
1113                 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1114                         goto cached;
1115
1116                 indbp = dnode->dn_blkptr;
1117                 for (i = 0; i < nlevels; i++) {
1118                         /*
1119                          * Copy the bp from the indirect array so that
1120                          * we can re-use the scratch buffer for multi-level
1121                          * objects.
1122                          */
1123                         ibn = bn >> ((nlevels - i - 1) * ibshift);
1124                         ibn &= ((1 << ibshift) - 1);
1125                         bp = indbp[ibn];
1126                         rc = zio_read(spa, &bp, dnode_cache_buf);
1127                         if (rc)
1128                                 return (rc);
1129                         indbp = (const blkptr_t *) dnode_cache_buf;
1130                 }
1131                 dnode_cache_obj = dnode;
1132                 dnode_cache_bn = bn;
1133         cached:
1134
1135                 /*
1136                  * The buffer contains our data block. Copy what we
1137                  * need from it and loop.
1138                  */ 
1139                 i = bsize - boff;
1140                 if (i > buflen) i = buflen;
1141                 memcpy(buf, &dnode_cache_buf[boff], i);
1142                 buf = ((char*) buf) + i;
1143                 offset += i;
1144                 buflen -= i;
1145         }
1146
1147         return (0);
1148 }
1149
1150 /*
1151  * Lookup a value in a microzap directory. Assumes that the zap
1152  * scratch buffer contains the directory contents.
1153  */
1154 static int
1155 mzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value)
1156 {
1157         const mzap_phys_t *mz;
1158         const mzap_ent_phys_t *mze;
1159         size_t size;
1160         int chunks, i;
1161
1162         /*
1163          * Microzap objects use exactly one block. Read the whole
1164          * thing.
1165          */
1166         size = dnode->dn_datablkszsec * 512;
1167
1168         mz = (const mzap_phys_t *) zap_scratch;
1169         chunks = size / MZAP_ENT_LEN - 1;
1170
1171         for (i = 0; i < chunks; i++) {
1172                 mze = &mz->mz_chunk[i];
1173                 if (!strcmp(mze->mze_name, name)) {
1174                         *value = mze->mze_value;
1175                         return (0);
1176                 }
1177         }
1178
1179         return (ENOENT);
1180 }
1181
1182 /*
1183  * Compare a name with a zap leaf entry. Return non-zero if the name
1184  * matches.
1185  */
1186 static int
1187 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1188 {
1189         size_t namelen;
1190         const zap_leaf_chunk_t *nc;
1191         const char *p;
1192
1193         namelen = zc->l_entry.le_name_numints;
1194                         
1195         nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1196         p = name;
1197         while (namelen > 0) {
1198                 size_t len;
1199                 len = namelen;
1200                 if (len > ZAP_LEAF_ARRAY_BYTES)
1201                         len = ZAP_LEAF_ARRAY_BYTES;
1202                 if (memcmp(p, nc->l_array.la_array, len))
1203                         return (0);
1204                 p += len;
1205                 namelen -= len;
1206                 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1207         }
1208
1209         return 1;
1210 }
1211
1212 /*
1213  * Extract a uint64_t value from a zap leaf entry.
1214  */
1215 static uint64_t
1216 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1217 {
1218         const zap_leaf_chunk_t *vc;
1219         int i;
1220         uint64_t value;
1221         const uint8_t *p;
1222
1223         vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1224         for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1225                 value = (value << 8) | p[i];
1226         }
1227
1228         return value;
1229 }
1230
1231 /*
1232  * Lookup a value in a fatzap directory. Assumes that the zap scratch
1233  * buffer contains the directory header.
1234  */
1235 static int
1236 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1237 {
1238         int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1239         zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1240         fat_zap_t z;
1241         uint64_t *ptrtbl;
1242         uint64_t hash;
1243         int rc;
1244
1245         if (zh.zap_magic != ZAP_MAGIC)
1246                 return (EIO);
1247
1248         z.zap_block_shift = ilog2(bsize);
1249         z.zap_phys = (zap_phys_t *) zap_scratch;
1250
1251         /*
1252          * Figure out where the pointer table is and read it in if necessary.
1253          */
1254         if (zh.zap_ptrtbl.zt_blk) {
1255                 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1256                                zap_scratch, bsize);
1257                 if (rc)
1258                         return (rc);
1259                 ptrtbl = (uint64_t *) zap_scratch;
1260         } else {
1261                 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1262         }
1263
1264         hash = zap_hash(zh.zap_salt, name);
1265
1266         zap_leaf_t zl;
1267         zl.l_bs = z.zap_block_shift;
1268
1269         off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1270         zap_leaf_chunk_t *zc;
1271
1272         rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1273         if (rc)
1274                 return (rc);
1275
1276         zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1277
1278         /*
1279          * Make sure this chunk matches our hash.
1280          */
1281         if (zl.l_phys->l_hdr.lh_prefix_len > 0
1282             && zl.l_phys->l_hdr.lh_prefix
1283             != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1284                 return (ENOENT);
1285
1286         /*
1287          * Hash within the chunk to find our entry.
1288          */
1289         int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1290         int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1291         h = zl.l_phys->l_hash[h];
1292         if (h == 0xffff)
1293                 return (ENOENT);
1294         zc = &ZAP_LEAF_CHUNK(&zl, h);
1295         while (zc->l_entry.le_hash != hash) {
1296                 if (zc->l_entry.le_next == 0xffff) {
1297                         zc = 0;
1298                         break;
1299                 }
1300                 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1301         }
1302         if (fzap_name_equal(&zl, zc, name)) {
1303                 if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints > 8)
1304                         return (E2BIG);
1305                 *value = fzap_leaf_value(&zl, zc);
1306                 return (0);
1307         }
1308
1309         return (ENOENT);
1310 }
1311
1312 /*
1313  * Lookup a name in a zap object and return its value as a uint64_t.
1314  */
1315 static int
1316 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1317 {
1318         int rc;
1319         uint64_t zap_type;
1320         size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1321
1322         rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1323         if (rc)
1324                 return (rc);
1325
1326         zap_type = *(uint64_t *) zap_scratch;
1327         if (zap_type == ZBT_MICRO)
1328                 return mzap_lookup(dnode, name, value);
1329         else if (zap_type == ZBT_HEADER)
1330                 return fzap_lookup(spa, dnode, name, value);
1331         printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
1332         return (EIO);
1333 }
1334
1335 /*
1336  * List a microzap directory. Assumes that the zap scratch buffer contains
1337  * the directory contents.
1338  */
1339 static int
1340 mzap_list(const dnode_phys_t *dnode)
1341 {
1342         const mzap_phys_t *mz;
1343         const mzap_ent_phys_t *mze;
1344         size_t size;
1345         int chunks, i;
1346
1347         /*
1348          * Microzap objects use exactly one block. Read the whole
1349          * thing.
1350          */
1351         size = dnode->dn_datablkszsec * 512;
1352         mz = (const mzap_phys_t *) zap_scratch;
1353         chunks = size / MZAP_ENT_LEN - 1;
1354
1355         for (i = 0; i < chunks; i++) {
1356                 mze = &mz->mz_chunk[i];
1357                 if (mze->mze_name[0])
1358                         //printf("%-32s 0x%jx\n", mze->mze_name, (uintmax_t)mze->mze_value);
1359                         printf("%s\n", mze->mze_name);
1360         }
1361
1362         return (0);
1363 }
1364
1365 /*
1366  * List a fatzap directory. Assumes that the zap scratch buffer contains
1367  * the directory header.
1368  */
1369 static int
1370 fzap_list(const spa_t *spa, const dnode_phys_t *dnode)
1371 {
1372         int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1373         zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1374         fat_zap_t z;
1375         int i, j;
1376
1377         if (zh.zap_magic != ZAP_MAGIC)
1378                 return (EIO);
1379
1380         z.zap_block_shift = ilog2(bsize);
1381         z.zap_phys = (zap_phys_t *) zap_scratch;
1382
1383         /*
1384          * This assumes that the leaf blocks start at block 1. The
1385          * documentation isn't exactly clear on this.
1386          */
1387         zap_leaf_t zl;
1388         zl.l_bs = z.zap_block_shift;
1389         for (i = 0; i < zh.zap_num_leafs; i++) {
1390                 off_t off = (i + 1) << zl.l_bs;
1391                 char name[256], *p;
1392                 uint64_t value;
1393
1394                 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1395                         return (EIO);
1396
1397                 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1398
1399                 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1400                         zap_leaf_chunk_t *zc, *nc;
1401                         int namelen;
1402
1403                         zc = &ZAP_LEAF_CHUNK(&zl, j);
1404                         if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1405                                 continue;
1406                         namelen = zc->l_entry.le_name_numints;
1407                         if (namelen > sizeof(name))
1408                                 namelen = sizeof(name);
1409
1410                         /*
1411                          * Paste the name back together.
1412                          */
1413                         nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1414                         p = name;
1415                         while (namelen > 0) {
1416                                 int len;
1417                                 len = namelen;
1418                                 if (len > ZAP_LEAF_ARRAY_BYTES)
1419                                         len = ZAP_LEAF_ARRAY_BYTES;
1420                                 memcpy(p, nc->l_array.la_array, len);
1421                                 p += len;
1422                                 namelen -= len;
1423                                 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1424                         }
1425
1426                         /*
1427                          * Assume the first eight bytes of the value are
1428                          * a uint64_t.
1429                          */
1430                         value = fzap_leaf_value(&zl, zc);
1431
1432                         //printf("%s 0x%jx\n", name, (uintmax_t)value);
1433                         printf("%s\n", name);
1434                 }
1435         }
1436
1437         return (0);
1438 }
1439
1440 /*
1441  * List a zap directory.
1442  */
1443 static int
1444 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
1445 {
1446         uint64_t zap_type;
1447         size_t size = dnode->dn_datablkszsec * 512;
1448
1449         if (dnode_read(spa, dnode, 0, zap_scratch, size))
1450                 return (EIO);
1451
1452         zap_type = *(uint64_t *) zap_scratch;
1453         if (zap_type == ZBT_MICRO)
1454                 return mzap_list(dnode);
1455         else
1456                 return fzap_list(spa, dnode);
1457 }
1458
1459 static int
1460 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1461 {
1462         off_t offset;
1463
1464         offset = objnum * sizeof(dnode_phys_t);
1465         return dnode_read(spa, &os->os_meta_dnode, offset,
1466                 dnode, sizeof(dnode_phys_t));
1467 }
1468
1469 static int
1470 mzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1471 {
1472         const mzap_phys_t *mz;
1473         const mzap_ent_phys_t *mze;
1474         size_t size;
1475         int chunks, i;
1476
1477         /*
1478          * Microzap objects use exactly one block. Read the whole
1479          * thing.
1480          */
1481         size = dnode->dn_datablkszsec * 512;
1482
1483         mz = (const mzap_phys_t *) zap_scratch;
1484         chunks = size / MZAP_ENT_LEN - 1;
1485
1486         for (i = 0; i < chunks; i++) {
1487                 mze = &mz->mz_chunk[i];
1488                 if (value == mze->mze_value) {
1489                         strcpy(name, mze->mze_name);
1490                         return (0);
1491                 }
1492         }
1493
1494         return (ENOENT);
1495 }
1496
1497 static void
1498 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
1499 {
1500         size_t namelen;
1501         const zap_leaf_chunk_t *nc;
1502         char *p;
1503
1504         namelen = zc->l_entry.le_name_numints;
1505
1506         nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1507         p = name;
1508         while (namelen > 0) {
1509                 size_t len;
1510                 len = namelen;
1511                 if (len > ZAP_LEAF_ARRAY_BYTES)
1512                         len = ZAP_LEAF_ARRAY_BYTES;
1513                 memcpy(p, nc->l_array.la_array, len);
1514                 p += len;
1515                 namelen -= len;
1516                 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1517         }
1518
1519         *p = '\0';
1520 }
1521
1522 static int
1523 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1524 {
1525         int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1526         zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1527         fat_zap_t z;
1528         int i, j;
1529
1530         if (zh.zap_magic != ZAP_MAGIC)
1531                 return (EIO);
1532
1533         z.zap_block_shift = ilog2(bsize);
1534         z.zap_phys = (zap_phys_t *) zap_scratch;
1535
1536         /*
1537          * This assumes that the leaf blocks start at block 1. The
1538          * documentation isn't exactly clear on this.
1539          */
1540         zap_leaf_t zl;
1541         zl.l_bs = z.zap_block_shift;
1542         for (i = 0; i < zh.zap_num_leafs; i++) {
1543                 off_t off = (i + 1) << zl.l_bs;
1544
1545                 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1546                         return (EIO);
1547
1548                 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1549
1550                 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1551                         zap_leaf_chunk_t *zc;
1552
1553                         zc = &ZAP_LEAF_CHUNK(&zl, j);
1554                         if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1555                                 continue;
1556                         if (zc->l_entry.le_value_intlen != 8 ||
1557                             zc->l_entry.le_value_numints != 1)
1558                                 continue;
1559
1560                         if (fzap_leaf_value(&zl, zc) == value) {
1561                                 fzap_name_copy(&zl, zc, name);
1562                                 return (0);
1563                         }
1564                 }
1565         }
1566
1567         return (ENOENT);
1568 }
1569
1570 static int
1571 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1572 {
1573         int rc;
1574         uint64_t zap_type;
1575         size_t size = dnode->dn_datablkszsec * 512;
1576
1577         rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1578         if (rc)
1579                 return (rc);
1580
1581         zap_type = *(uint64_t *) zap_scratch;
1582         if (zap_type == ZBT_MICRO)
1583                 return mzap_rlookup(spa, dnode, name, value);
1584         else
1585                 return fzap_rlookup(spa, dnode, name, value);
1586 }
1587
1588 static int
1589 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
1590 {
1591         char name[256];
1592         char component[256];
1593         uint64_t dir_obj, parent_obj, child_dir_zapobj;
1594         dnode_phys_t child_dir_zap, dataset, dir, parent;
1595         dsl_dir_phys_t *dd;
1596         dsl_dataset_phys_t *ds;
1597         char *p;
1598         int len;
1599
1600         p = &name[sizeof(name) - 1];
1601         *p = '\0';
1602
1603         if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1604                 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1605                 return (EIO);
1606         }
1607         ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
1608         dir_obj = ds->ds_dir_obj;
1609
1610         for (;;) {
1611                 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
1612                         return (EIO);
1613                 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1614
1615                 /* Actual loop condition. */
1616                 parent_obj  = dd->dd_parent_obj;
1617                 if (parent_obj == 0)
1618                         break;
1619
1620                 if (objset_get_dnode(spa, &spa->spa_mos, parent_obj, &parent) != 0)
1621                         return (EIO);
1622                 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
1623                 child_dir_zapobj = dd->dd_child_dir_zapobj;
1624                 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1625                         return (EIO);
1626                 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
1627                         return (EIO);
1628
1629                 len = strlen(component);
1630                 p -= len;
1631                 memcpy(p, component, len);
1632                 --p;
1633                 *p = '/';
1634
1635                 /* Actual loop iteration. */
1636                 dir_obj = parent_obj;
1637         }
1638
1639         if (*p != '\0')
1640                 ++p;
1641         strcpy(result, p);
1642
1643         return (0);
1644 }
1645
1646 static int
1647 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
1648 {
1649         char element[256];
1650         uint64_t dir_obj, child_dir_zapobj;
1651         dnode_phys_t child_dir_zap, dir;
1652         dsl_dir_phys_t *dd;
1653         const char *p, *q;
1654
1655         if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir))
1656                 return (EIO);
1657         if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &dir_obj))
1658                 return (EIO);
1659
1660         p = name;
1661         for (;;) {
1662                 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
1663                         return (EIO);
1664                 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1665
1666                 while (*p == '/')
1667                         p++;
1668                 /* Actual loop condition #1. */
1669                 if (*p == '\0')
1670                         break;
1671
1672                 q = strchr(p, '/');
1673                 if (q) {
1674                         memcpy(element, p, q - p);
1675                         element[q - p] = '\0';
1676                         p = q + 1;
1677                 } else {
1678                         strcpy(element, p);
1679                         p += strlen(p);
1680                 }
1681
1682                 child_dir_zapobj = dd->dd_child_dir_zapobj;
1683                 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1684                         return (EIO);
1685
1686                 /* Actual loop condition #2. */
1687                 if (zap_lookup(spa, &child_dir_zap, element, &dir_obj) != 0)
1688                         return (ENOENT);
1689         }
1690
1691         *objnum = dd->dd_head_dataset_obj;
1692         return (0);
1693 }
1694
1695 #ifndef BOOT2
1696 static int
1697 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
1698 {
1699         uint64_t dir_obj, child_dir_zapobj;
1700         dnode_phys_t child_dir_zap, dir, dataset;
1701         dsl_dataset_phys_t *ds;
1702         dsl_dir_phys_t *dd;
1703
1704         if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1705                 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1706                 return (EIO);
1707         }
1708         ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1709         dir_obj = ds->ds_dir_obj;
1710
1711         if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
1712                 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
1713                 return (EIO);
1714         }
1715         dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1716
1717         child_dir_zapobj = dd->dd_child_dir_zapobj;
1718         if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) {
1719                 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
1720                 return (EIO);
1721         }
1722
1723         return (zap_list(spa, &child_dir_zap) != 0);
1724 }
1725 #endif
1726
1727 /*
1728  * Find the object set given the object number of its dataset object
1729  * and return its details in *objset
1730  */
1731 static int
1732 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
1733 {
1734         dnode_phys_t dataset;
1735         dsl_dataset_phys_t *ds;
1736
1737         if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1738                 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1739                 return (EIO);
1740         }
1741
1742         ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1743         if (zio_read(spa, &ds->ds_bp, objset)) {
1744                 printf("ZFS: can't read object set for dataset %ju\n",
1745                     (uintmax_t)objnum);
1746                 return (EIO);
1747         }
1748
1749         return (0);
1750 }
1751
1752 /*
1753  * Find the object set pointed to by the BOOTFS property or the root
1754  * dataset if there is none and return its details in *objset
1755  */
1756 static int
1757 zfs_get_root(const spa_t *spa, uint64_t *objid)
1758 {
1759         dnode_phys_t dir, propdir;
1760         uint64_t props, bootfs, root;
1761
1762         *objid = 0;
1763
1764         /*
1765          * Start with the MOS directory object.
1766          */
1767         if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
1768                 printf("ZFS: can't read MOS object directory\n");
1769                 return (EIO);
1770         }
1771
1772         /*
1773          * Lookup the pool_props and see if we can find a bootfs.
1774          */
1775         if (zap_lookup(spa, &dir, DMU_POOL_PROPS, &props) == 0
1776              && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
1777              && zap_lookup(spa, &propdir, "bootfs", &bootfs) == 0
1778              && bootfs != 0)
1779         {
1780                 *objid = bootfs;
1781                 return (0);
1782         }
1783         /*
1784          * Lookup the root dataset directory
1785          */
1786         if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &root)
1787             || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
1788                 printf("ZFS: can't find root dsl_dir\n");
1789                 return (EIO);
1790         }
1791
1792         /*
1793          * Use the information from the dataset directory's bonus buffer
1794          * to find the dataset object and from that the object set itself.
1795          */
1796         dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
1797         *objid = dd->dd_head_dataset_obj;
1798         return (0);
1799 }
1800
1801 static int
1802 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
1803 {
1804
1805         mount->spa = spa;
1806
1807         /*
1808          * Find the root object set if not explicitly provided
1809          */
1810         if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
1811                 printf("ZFS: can't find root filesystem\n");
1812                 return (EIO);
1813         }
1814
1815         if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
1816                 printf("ZFS: can't open root filesystem\n");
1817                 return (EIO);
1818         }
1819
1820         mount->rootobj = rootobj;
1821
1822         return (0);
1823 }
1824
1825 static int
1826 zfs_spa_init(spa_t *spa)
1827 {
1828
1829         if (spa->spa_inited)
1830                 return (0);
1831         if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
1832                 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
1833                 return (EIO);
1834         }
1835         spa->spa_inited = 1;
1836         return (0);
1837 }
1838
1839 static int
1840 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
1841 {
1842
1843         if (dn->dn_bonustype != DMU_OT_SA) {
1844                 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
1845
1846                 sb->st_mode = zp->zp_mode;
1847                 sb->st_uid = zp->zp_uid;
1848                 sb->st_gid = zp->zp_gid;
1849                 sb->st_size = zp->zp_size;
1850         } else {
1851                 sa_hdr_phys_t *sahdrp;
1852                 int hdrsize;
1853                 size_t size = 0;
1854                 void *buf = NULL;
1855
1856                 if (dn->dn_bonuslen != 0)
1857                         sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
1858                 else {
1859                         if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
1860                                 blkptr_t *bp = &dn->dn_spill;
1861                                 int error;
1862
1863                                 size = BP_GET_LSIZE(bp);
1864                                 buf = zfs_alloc(size);
1865                                 error = zio_read(spa, bp, buf);
1866                                 if (error != 0) {
1867                                         zfs_free(buf, size);
1868                                         return (error);
1869                                 }
1870                                 sahdrp = buf;
1871                         } else {
1872                                 return (EIO);
1873                         }
1874                 }
1875                 hdrsize = SA_HDR_SIZE(sahdrp);
1876                 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
1877                     SA_MODE_OFFSET);
1878                 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
1879                     SA_UID_OFFSET);
1880                 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
1881                     SA_GID_OFFSET);
1882                 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
1883                     SA_SIZE_OFFSET);
1884                 if (buf != NULL)
1885                         zfs_free(buf, size);
1886         }
1887
1888         return (0);
1889 }
1890
1891 /*
1892  * Lookup a file and return its dnode.
1893  */
1894 static int
1895 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
1896 {
1897         int rc;
1898         uint64_t objnum, rootnum, parentnum;
1899         const spa_t *spa;
1900         dnode_phys_t dn;
1901         const char *p, *q;
1902         char element[256];
1903         char path[1024];
1904         int symlinks_followed = 0;
1905         struct stat sb;
1906
1907         spa = mount->spa;
1908         if (mount->objset.os_type != DMU_OST_ZFS) {
1909                 printf("ZFS: unexpected object set type %ju\n",
1910                     (uintmax_t)mount->objset.os_type);
1911                 return (EIO);
1912         }
1913
1914         /*
1915          * Get the root directory dnode.
1916          */
1917         rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
1918         if (rc)
1919                 return (rc);
1920
1921         rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, &rootnum);
1922         if (rc)
1923                 return (rc);
1924
1925         rc = objset_get_dnode(spa, &mount->objset, rootnum, &dn);
1926         if (rc)
1927                 return (rc);
1928
1929         objnum = rootnum;
1930         p = upath;
1931         while (p && *p) {
1932                 while (*p == '/')
1933                         p++;
1934                 if (!*p)
1935                         break;
1936                 q = strchr(p, '/');
1937                 if (q) {
1938                         memcpy(element, p, q - p);
1939                         element[q - p] = 0;
1940                         p = q;
1941                 } else {
1942                         strcpy(element, p);
1943                         p = 0;
1944                 }
1945
1946                 rc = zfs_dnode_stat(spa, &dn, &sb);
1947                 if (rc)
1948                         return (rc);
1949                 if (!S_ISDIR(sb.st_mode))
1950                         return (ENOTDIR);
1951
1952                 parentnum = objnum;
1953                 rc = zap_lookup(spa, &dn, element, &objnum);
1954                 if (rc)
1955                         return (rc);
1956                 objnum = ZFS_DIRENT_OBJ(objnum);
1957
1958                 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
1959                 if (rc)
1960                         return (rc);
1961
1962                 /*
1963                  * Check for symlink.
1964                  */
1965                 rc = zfs_dnode_stat(spa, &dn, &sb);
1966                 if (rc)
1967                         return (rc);
1968                 if (S_ISLNK(sb.st_mode)) {
1969                         if (symlinks_followed > 10)
1970                                 return (EMLINK);
1971                         symlinks_followed++;
1972
1973                         /*
1974                          * Read the link value and copy the tail of our
1975                          * current path onto the end.
1976                          */
1977                         if (p)
1978                                 strcpy(&path[sb.st_size], p);
1979                         else
1980                                 path[sb.st_size] = 0;
1981                         if (sb.st_size + sizeof(znode_phys_t) <= dn.dn_bonuslen) {
1982                                 memcpy(path, &dn.dn_bonus[sizeof(znode_phys_t)],
1983                                         sb.st_size);
1984                         } else {
1985                                 rc = dnode_read(spa, &dn, 0, path, sb.st_size);
1986                                 if (rc)
1987                                         return (rc);
1988                         }
1989
1990                         /*
1991                          * Restart with the new path, starting either at
1992                          * the root or at the parent depending whether or
1993                          * not the link is relative.
1994                          */
1995                         p = path;
1996                         if (*p == '/')
1997                                 objnum = rootnum;
1998                         else
1999                                 objnum = parentnum;
2000                         objset_get_dnode(spa, &mount->objset, objnum, &dn);
2001                 }
2002         }
2003
2004         *dnode = dn;
2005         return (0);
2006 }