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