]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/boot/zfs/zfsimpl.c
MFC r213133,r213135,r213136,r213137,r213245:
[FreeBSD/stable/8.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 "zfsimpl.h"
35 #include "zfssubr.c"
36
37 /*
38  * List of all vdevs, chained through v_alllink.
39  */
40 static vdev_list_t zfs_vdevs;
41
42 /*
43  * List of all pools, chained through spa_link.
44  */
45 static spa_list_t zfs_pools;
46
47 static uint64_t zfs_crc64_table[256];
48 static const dnode_phys_t *dnode_cache_obj = 0;
49 static uint64_t dnode_cache_bn;
50 static char *dnode_cache_buf;
51 static char *zap_scratch;
52 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
53
54 #define TEMP_SIZE       (1024 * 1024)
55
56 static int zio_read(spa_t *spa, const blkptr_t *bp, void *buf);
57
58 static void
59 zfs_init(void)
60 {
61         STAILQ_INIT(&zfs_vdevs);
62         STAILQ_INIT(&zfs_pools);
63
64         zfs_temp_buf = malloc(TEMP_SIZE);
65         zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
66         zfs_temp_ptr = zfs_temp_buf;
67         dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
68         zap_scratch = malloc(SPA_MAXBLOCKSIZE);
69
70         zfs_init_crc();
71 }
72
73 static char *
74 zfs_alloc_temp(size_t sz)
75 {
76         char *p;
77
78         if (zfs_temp_ptr + sz > zfs_temp_end) {
79                 printf("ZFS: out of temporary buffer space\n");
80                 for (;;) ;
81         }
82         p = zfs_temp_ptr;
83         zfs_temp_ptr += sz;
84
85         return (p);
86 }
87
88 static void
89 zfs_reset_temp(void)
90 {
91
92         zfs_temp_ptr = zfs_temp_buf;
93 }
94
95 static int
96 xdr_int(const unsigned char **xdr, int *ip)
97 {
98         *ip = ((*xdr)[0] << 24)
99                 | ((*xdr)[1] << 16)
100                 | ((*xdr)[2] << 8)
101                 | ((*xdr)[3] << 0);
102         (*xdr) += 4;
103         return (0);
104 }
105
106 static int
107 xdr_u_int(const unsigned char **xdr, u_int *ip)
108 {
109         *ip = ((*xdr)[0] << 24)
110                 | ((*xdr)[1] << 16)
111                 | ((*xdr)[2] << 8)
112                 | ((*xdr)[3] << 0);
113         (*xdr) += 4;
114         return (0);
115 }
116
117 static int
118 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
119 {
120         u_int hi, lo;
121
122         xdr_u_int(xdr, &hi);
123         xdr_u_int(xdr, &lo);
124         *lp = (((uint64_t) hi) << 32) | lo;
125         return (0);
126 }
127
128 static int
129 nvlist_find(const unsigned char *nvlist, const char *name, int type,
130             int* elementsp, void *valuep)
131 {
132         const unsigned char *p, *pair;
133         int junk;
134         int encoded_size, decoded_size;
135
136         p = nvlist;
137         xdr_int(&p, &junk);
138         xdr_int(&p, &junk);
139
140         pair = p;
141         xdr_int(&p, &encoded_size);
142         xdr_int(&p, &decoded_size);
143         while (encoded_size && decoded_size) {
144                 int namelen, pairtype, elements;
145                 const char *pairname;
146
147                 xdr_int(&p, &namelen);
148                 pairname = (const char*) p;
149                 p += roundup(namelen, 4);
150                 xdr_int(&p, &pairtype);
151
152                 if (!memcmp(name, pairname, namelen) && type == pairtype) {
153                         xdr_int(&p, &elements);
154                         if (elementsp)
155                                 *elementsp = elements;
156                         if (type == DATA_TYPE_UINT64) {
157                                 xdr_uint64_t(&p, (uint64_t *) valuep);
158                                 return (0);
159                         } else if (type == DATA_TYPE_STRING) {
160                                 int len;
161                                 xdr_int(&p, &len);
162                                 (*(const char**) valuep) = (const char*) p;
163                                 return (0);
164                         } else if (type == DATA_TYPE_NVLIST
165                                    || type == DATA_TYPE_NVLIST_ARRAY) {
166                                 (*(const unsigned char**) valuep) =
167                                          (const unsigned char*) p;
168                                 return (0);
169                         } else {
170                                 return (EIO);
171                         }
172                 } else {
173                         /*
174                          * Not the pair we are looking for, skip to the next one.
175                          */
176                         p = pair + encoded_size;
177                 }
178
179                 pair = p;
180                 xdr_int(&p, &encoded_size);
181                 xdr_int(&p, &decoded_size);
182         }
183
184         return (EIO);
185 }
186
187 /*
188  * Return the next nvlist in an nvlist array.
189  */
190 static const unsigned char *
191 nvlist_next(const unsigned char *nvlist)
192 {
193         const unsigned char *p, *pair;
194         int junk;
195         int encoded_size, decoded_size;
196
197         p = nvlist;
198         xdr_int(&p, &junk);
199         xdr_int(&p, &junk);
200
201         pair = p;
202         xdr_int(&p, &encoded_size);
203         xdr_int(&p, &decoded_size);
204         while (encoded_size && decoded_size) {
205                 p = pair + encoded_size;
206
207                 pair = p;
208                 xdr_int(&p, &encoded_size);
209                 xdr_int(&p, &decoded_size);
210         }
211
212         return p;
213 }
214
215 #ifdef TEST
216
217 static const unsigned char *
218 nvlist_print(const unsigned char *nvlist, unsigned int indent)
219 {
220         static const char* typenames[] = {
221                 "DATA_TYPE_UNKNOWN",
222                 "DATA_TYPE_BOOLEAN",
223                 "DATA_TYPE_BYTE",
224                 "DATA_TYPE_INT16",
225                 "DATA_TYPE_UINT16",
226                 "DATA_TYPE_INT32",
227                 "DATA_TYPE_UINT32",
228                 "DATA_TYPE_INT64",
229                 "DATA_TYPE_UINT64",
230                 "DATA_TYPE_STRING",
231                 "DATA_TYPE_BYTE_ARRAY",
232                 "DATA_TYPE_INT16_ARRAY",
233                 "DATA_TYPE_UINT16_ARRAY",
234                 "DATA_TYPE_INT32_ARRAY",
235                 "DATA_TYPE_UINT32_ARRAY",
236                 "DATA_TYPE_INT64_ARRAY",
237                 "DATA_TYPE_UINT64_ARRAY",
238                 "DATA_TYPE_STRING_ARRAY",
239                 "DATA_TYPE_HRTIME",
240                 "DATA_TYPE_NVLIST",
241                 "DATA_TYPE_NVLIST_ARRAY",
242                 "DATA_TYPE_BOOLEAN_VALUE",
243                 "DATA_TYPE_INT8",
244                 "DATA_TYPE_UINT8",
245                 "DATA_TYPE_BOOLEAN_ARRAY",
246                 "DATA_TYPE_INT8_ARRAY",
247                 "DATA_TYPE_UINT8_ARRAY"
248         };
249
250         unsigned int i, j;
251         const unsigned char *p, *pair;
252         int junk;
253         int encoded_size, decoded_size;
254
255         p = nvlist;
256         xdr_int(&p, &junk);
257         xdr_int(&p, &junk);
258
259         pair = p;
260         xdr_int(&p, &encoded_size);
261         xdr_int(&p, &decoded_size);
262         while (encoded_size && decoded_size) {
263                 int namelen, pairtype, elements;
264                 const char *pairname;
265
266                 xdr_int(&p, &namelen);
267                 pairname = (const char*) p;
268                 p += roundup(namelen, 4);
269                 xdr_int(&p, &pairtype);
270
271                 for (i = 0; i < indent; i++)
272                         printf(" ");
273                 printf("%s %s", typenames[pairtype], pairname);
274
275                 xdr_int(&p, &elements);
276                 switch (pairtype) {
277                 case DATA_TYPE_UINT64: {
278                         uint64_t val;
279                         xdr_uint64_t(&p, &val);
280                         printf(" = 0x%llx\n", val);
281                         break;
282                 }
283
284                 case DATA_TYPE_STRING: {
285                         int len;
286                         xdr_int(&p, &len);
287                         printf(" = \"%s\"\n", p);
288                         break;
289                 }
290
291                 case DATA_TYPE_NVLIST:
292                         printf("\n");
293                         nvlist_print(p, indent + 1);
294                         break;
295
296                 case DATA_TYPE_NVLIST_ARRAY:
297                         for (j = 0; j < elements; j++) {
298                                 printf("[%d]\n", j);
299                                 p = nvlist_print(p, indent + 1);
300                                 if (j != elements - 1) {
301                                         for (i = 0; i < indent; i++)
302                                                 printf(" ");
303                                         printf("%s %s", typenames[pairtype], pairname);
304                                 }
305                         }
306                         break;
307
308                 default:
309                         printf("\n");
310                 }
311
312                 p = pair + encoded_size;
313
314                 pair = p;
315                 xdr_int(&p, &encoded_size);
316                 xdr_int(&p, &decoded_size);
317         }
318
319         return p;
320 }
321
322 #endif
323
324 static int
325 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
326     off_t offset, size_t size)
327 {
328         size_t psize;
329         int rc;
330
331         if (!vdev->v_phys_read)
332                 return (EIO);
333
334         if (bp) {
335                 psize = BP_GET_PSIZE(bp);
336         } else {
337                 psize = size;
338         }
339
340         /*printf("ZFS: reading %d bytes at 0x%llx to %p\n", psize, offset, buf);*/
341         rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
342         if (rc)
343                 return (rc);
344         if (bp && zio_checksum_error(bp, buf))
345                 return (EIO);
346
347         return (0);
348 }
349
350 static int
351 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
352     off_t offset, size_t bytes)
353 {
354
355         return (vdev_read_phys(vdev, bp, buf,
356                 offset + VDEV_LABEL_START_SIZE, bytes));
357 }
358
359
360 static int
361 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
362     off_t offset, size_t bytes)
363 {
364         vdev_t *kid;
365         int rc;
366
367         rc = EIO;
368         STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
369                 if (kid->v_state != VDEV_STATE_HEALTHY)
370                         continue;
371                 rc = kid->v_read(kid, bp, buf, offset, bytes);
372                 if (!rc)
373                         return (0);
374         }
375
376         return (rc);
377 }
378
379 static vdev_t *
380 vdev_find(uint64_t guid)
381 {
382         vdev_t *vdev;
383
384         STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
385                 if (vdev->v_guid == guid)
386                         return (vdev);
387
388         return (0);
389 }
390
391 static vdev_t *
392 vdev_create(uint64_t guid, vdev_read_t *read)
393 {
394         vdev_t *vdev;
395
396         vdev = malloc(sizeof(vdev_t));
397         memset(vdev, 0, sizeof(vdev_t));
398         STAILQ_INIT(&vdev->v_children);
399         vdev->v_guid = guid;
400         vdev->v_state = VDEV_STATE_OFFLINE;
401         vdev->v_read = read;
402         vdev->v_phys_read = 0;
403         vdev->v_read_priv = 0;
404         STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
405
406         return (vdev);
407 }
408
409 static int
410 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t **vdevp, int is_newer)
411 {
412         int rc;
413         uint64_t guid, id, ashift, nparity;
414         const char *type;
415         const char *path;
416         vdev_t *vdev, *kid;
417         const unsigned char *kids;
418         int nkids, i, is_new;
419         uint64_t is_offline, is_faulted, is_degraded, is_removed;
420
421         if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID,
422                         DATA_TYPE_UINT64, 0, &guid)
423             || nvlist_find(nvlist, ZPOOL_CONFIG_ID,
424                            DATA_TYPE_UINT64, 0, &id)
425             || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE,
426                            DATA_TYPE_STRING, 0, &type)) {
427                 printf("ZFS: can't find vdev details\n");
428                 return (ENOENT);
429         }
430
431         if (strcmp(type, VDEV_TYPE_MIRROR)
432             && strcmp(type, VDEV_TYPE_DISK)
433             && strcmp(type, VDEV_TYPE_RAIDZ)) {
434                 printf("ZFS: can only boot from disk, mirror or raidz vdevs\n");
435                 return (EIO);
436         }
437
438         is_offline = is_removed = is_faulted = is_degraded = 0;
439
440         nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, 0,
441                         &is_offline);
442         nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, 0,
443                         &is_removed);
444         nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, 0,
445                         &is_faulted);
446         nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, 0,
447                         &is_degraded);
448
449         vdev = vdev_find(guid);
450         if (!vdev) {
451                 is_new = 1;
452
453                 if (!strcmp(type, VDEV_TYPE_MIRROR))
454                         vdev = vdev_create(guid, vdev_mirror_read);
455                 else if (!strcmp(type, VDEV_TYPE_RAIDZ))
456                         vdev = vdev_create(guid, vdev_raidz_read);
457                 else
458                         vdev = vdev_create(guid, vdev_disk_read);
459
460                 vdev->v_id = id;
461                 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
462                         DATA_TYPE_UINT64, 0, &ashift) == 0)
463                         vdev->v_ashift = ashift;
464                 else
465                         vdev->v_ashift = 0;
466                 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
467                         DATA_TYPE_UINT64, 0, &nparity) == 0)
468                         vdev->v_nparity = nparity;
469                 else
470                         vdev->v_nparity = 0;
471                 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
472                                 DATA_TYPE_STRING, 0, &path) == 0) {
473                         if (strncmp(path, "/dev/", 5) == 0)
474                                 path += 5;
475                         vdev->v_name = strdup(path);
476                 } else {
477                         if (!strcmp(type, "raidz")) {
478                                 if (vdev->v_nparity == 1)
479                                         vdev->v_name = "raidz1";
480                                 else
481                                         vdev->v_name = "raidz2";
482                         } else {
483                                 vdev->v_name = strdup(type);
484                         }
485                 }
486
487                 if (is_offline)
488                         vdev->v_state = VDEV_STATE_OFFLINE;
489                 else if (is_removed)
490                         vdev->v_state = VDEV_STATE_REMOVED;
491                 else if (is_faulted)
492                         vdev->v_state = VDEV_STATE_FAULTED;
493                 else if (is_degraded)
494                         vdev->v_state = VDEV_STATE_DEGRADED;
495                 else
496                         vdev->v_state = VDEV_STATE_HEALTHY;
497         } else {
498                 is_new = 0;
499
500                 if (is_newer) {
501                         /*
502                          * We've already seen this vdev, but from an older
503                          * vdev label, so let's refresh its state from the
504                          * newer label.
505                          */
506                         if (is_offline)
507                                 vdev->v_state = VDEV_STATE_OFFLINE;
508                         else if (is_removed)
509                                 vdev->v_state = VDEV_STATE_REMOVED;
510                         else if (is_faulted)
511                                 vdev->v_state = VDEV_STATE_FAULTED;
512                         else if (is_degraded)
513                                 vdev->v_state = VDEV_STATE_DEGRADED;
514                         else
515                                 vdev->v_state = VDEV_STATE_HEALTHY;
516                 }
517         }
518
519         rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN,
520                          DATA_TYPE_NVLIST_ARRAY, &nkids, &kids);
521         /*
522          * Its ok if we don't have any kids.
523          */
524         if (rc == 0) {
525                 vdev->v_nchildren = nkids;
526                 for (i = 0; i < nkids; i++) {
527                         rc = vdev_init_from_nvlist(kids, &kid, is_newer);
528                         if (rc)
529                                 return (rc);
530                         if (is_new)
531                                 STAILQ_INSERT_TAIL(&vdev->v_children, kid,
532                                                    v_childlink);
533                         kids = nvlist_next(kids);
534                 }
535         } else {
536                 vdev->v_nchildren = 0;
537         }
538
539         if (vdevp)
540                 *vdevp = vdev;
541         return (0);
542 }
543
544 static void
545 vdev_set_state(vdev_t *vdev)
546 {
547         vdev_t *kid;
548         int good_kids;
549         int bad_kids;
550
551         /*
552          * A mirror or raidz is healthy if all its kids are healthy. A
553          * mirror is degraded if any of its kids is healthy; a raidz
554          * is degraded if at most nparity kids are offline.
555          */
556         if (STAILQ_FIRST(&vdev->v_children)) {
557                 good_kids = 0;
558                 bad_kids = 0;
559                 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
560                         if (kid->v_state == VDEV_STATE_HEALTHY)
561                                 good_kids++;
562                         else
563                                 bad_kids++;
564                 }
565                 if (bad_kids == 0) {
566                         vdev->v_state = VDEV_STATE_HEALTHY;
567                 } else {
568                         if (vdev->v_read == vdev_mirror_read) {
569                                 if (good_kids) {
570                                         vdev->v_state = VDEV_STATE_DEGRADED;
571                                 } else {
572                                         vdev->v_state = VDEV_STATE_OFFLINE;
573                                 }
574                         } else if (vdev->v_read == vdev_raidz_read) {
575                                 if (bad_kids > vdev->v_nparity) {
576                                         vdev->v_state = VDEV_STATE_OFFLINE;
577                                 } else {
578                                         vdev->v_state = VDEV_STATE_DEGRADED;
579                                 }
580                         }
581                 }
582         }
583 }
584
585 static spa_t *
586 spa_find_by_guid(uint64_t guid)
587 {
588         spa_t *spa;
589
590         STAILQ_FOREACH(spa, &zfs_pools, spa_link)
591                 if (spa->spa_guid == guid)
592                         return (spa);
593
594         return (0);
595 }
596
597 #ifdef BOOT2
598
599 static spa_t *
600 spa_find_by_name(const char *name)
601 {
602         spa_t *spa;
603
604         STAILQ_FOREACH(spa, &zfs_pools, spa_link)
605                 if (!strcmp(spa->spa_name, name))
606                         return (spa);
607
608         return (0);
609 }
610
611 #endif
612
613 static spa_t *
614 spa_create(uint64_t guid)
615 {
616         spa_t *spa;
617
618         spa = malloc(sizeof(spa_t));
619         memset(spa, 0, sizeof(spa_t));
620         STAILQ_INIT(&spa->spa_vdevs);
621         spa->spa_guid = guid;
622         STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
623
624         return (spa);
625 }
626
627 static const char *
628 state_name(vdev_state_t state)
629 {
630         static const char* names[] = {
631                 "UNKNOWN",
632                 "CLOSED",
633                 "OFFLINE",
634                 "REMOVED",
635                 "CANT_OPEN",
636                 "FAULTED",
637                 "DEGRADED",
638                 "ONLINE"
639         };
640         return names[state];
641 }
642
643 #ifdef BOOT2
644
645 #define pager_printf printf
646
647 #else
648
649 static void
650 pager_printf(const char *fmt, ...)
651 {
652         char line[80];
653         va_list args;
654
655         va_start(args, fmt);
656         vsprintf(line, fmt, args);
657         va_end(args);
658         pager_output(line);
659 }
660
661 #endif
662
663 #define STATUS_FORMAT   "        %s %s\n"
664
665 static void
666 print_state(int indent, const char *name, vdev_state_t state)
667 {
668         int i;
669         char buf[512];
670
671         buf[0] = 0;
672         for (i = 0; i < indent; i++)
673                 strcat(buf, "  ");
674         strcat(buf, name);
675         pager_printf(STATUS_FORMAT, buf, state_name(state));
676         
677 }
678
679 static void
680 vdev_status(vdev_t *vdev, int indent)
681 {
682         vdev_t *kid;
683         print_state(indent, vdev->v_name, vdev->v_state);
684
685         STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
686                 vdev_status(kid, indent + 1);
687         }
688 }
689
690 static void
691 spa_status(spa_t *spa)
692 {
693         vdev_t *vdev;
694         int good_kids, bad_kids, degraded_kids;
695         vdev_state_t state;
696
697         pager_printf("  pool: %s\n", spa->spa_name);
698         pager_printf("config:\n\n");
699         pager_printf(STATUS_FORMAT, "NAME", "STATE");
700
701         good_kids = 0;
702         degraded_kids = 0;
703         bad_kids = 0;
704         STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
705                 if (vdev->v_state == VDEV_STATE_HEALTHY)
706                         good_kids++;
707                 else if (vdev->v_state == VDEV_STATE_DEGRADED)
708                         degraded_kids++;
709                 else
710                         bad_kids++;
711         }
712
713         state = VDEV_STATE_CLOSED;
714         if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
715                 state = VDEV_STATE_HEALTHY;
716         else if ((good_kids + degraded_kids) > 0)
717                 state = VDEV_STATE_DEGRADED;
718
719         print_state(0, spa->spa_name, state);
720         STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
721                 vdev_status(vdev, 1);
722         }
723 }
724
725 static void
726 spa_all_status(void)
727 {
728         spa_t *spa;
729         int first = 1;
730
731         STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
732                 if (!first)
733                         pager_printf("\n");
734                 first = 0;
735                 spa_status(spa);
736         }
737 }
738
739 static int
740 vdev_probe(vdev_phys_read_t *read, void *read_priv, spa_t **spap)
741 {
742         vdev_t vtmp;
743         vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
744         spa_t *spa;
745         vdev_t *vdev, *top_vdev, *pool_vdev;
746         off_t off;
747         blkptr_t bp;
748         const unsigned char *nvlist;
749         uint64_t val;
750         uint64_t guid;
751         uint64_t pool_txg, pool_guid;
752         const char *pool_name;
753         const unsigned char *vdevs;
754         int i, rc, is_newer;
755         char upbuf[1024];
756         const struct uberblock *up;
757
758         /*
759          * Load the vdev label and figure out which
760          * uberblock is most current.
761          */
762         memset(&vtmp, 0, sizeof(vtmp));
763         vtmp.v_phys_read = read;
764         vtmp.v_read_priv = read_priv;
765         off = offsetof(vdev_label_t, vl_vdev_phys);
766         BP_ZERO(&bp);
767         BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
768         BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
769         BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
770         BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
771         ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
772         if (vdev_read_phys(&vtmp, &bp, vdev_label, off, 0))
773                 return (EIO);
774
775         if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR) {
776                 return (EIO);
777         }
778
779         nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
780
781         if (nvlist_find(nvlist,
782                         ZPOOL_CONFIG_VERSION,
783                         DATA_TYPE_UINT64, 0, &val)) {
784                 return (EIO);
785         }
786
787         if (val > SPA_VERSION) {
788                 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
789                     (unsigned) val, (unsigned) SPA_VERSION);
790                 return (EIO);
791         }
792
793         if (nvlist_find(nvlist,
794                         ZPOOL_CONFIG_POOL_STATE,
795                         DATA_TYPE_UINT64, 0, &val)) {
796                 return (EIO);
797         }
798
799 #ifndef TEST
800         if (val != POOL_STATE_ACTIVE) {
801                 /*
802                  * Don't print a message here. If we happen to reboot
803                  * while where is an exported pool around, we don't
804                  * need a cascade of confusing messages during boot.
805                  */
806                 /*printf("ZFS: pool is not active\n");*/
807                 return (EIO);
808         }
809 #endif
810
811         if (nvlist_find(nvlist,
812                         ZPOOL_CONFIG_POOL_TXG,
813                         DATA_TYPE_UINT64, 0, &pool_txg)
814             || nvlist_find(nvlist,
815                            ZPOOL_CONFIG_POOL_GUID,
816                            DATA_TYPE_UINT64, 0, &pool_guid)
817             || nvlist_find(nvlist,
818                            ZPOOL_CONFIG_POOL_NAME,
819                            DATA_TYPE_STRING, 0, &pool_name)) {
820                 /*
821                  * Cache and spare devices end up here - just ignore
822                  * them.
823                  */
824                 /*printf("ZFS: can't find pool details\n");*/
825                 return (EIO);
826         }
827
828         /*
829          * Create the pool if this is the first time we've seen it.
830          */
831         spa = spa_find_by_guid(pool_guid);
832         if (!spa) {
833                 spa = spa_create(pool_guid);
834                 spa->spa_name = strdup(pool_name);
835         }
836         if (pool_txg > spa->spa_txg) {
837                 spa->spa_txg = pool_txg;
838                 is_newer = 1;
839         } else
840                 is_newer = 0;
841
842         /*
843          * Get the vdev tree and create our in-core copy of it.
844          * If we already have a vdev with this guid, this must
845          * be some kind of alias (overlapping slices, dangerously dedicated
846          * disks etc).
847          */
848         if (nvlist_find(nvlist,
849                         ZPOOL_CONFIG_GUID,
850                         DATA_TYPE_UINT64, 0, &guid)) {
851                 return (EIO);
852         }
853         vdev = vdev_find(guid);
854         if (vdev && vdev->v_phys_read)  /* Has this vdev already been inited? */
855                 return (EIO);
856
857         if (nvlist_find(nvlist,
858                         ZPOOL_CONFIG_VDEV_TREE,
859                         DATA_TYPE_NVLIST, 0, &vdevs)) {
860                 return (EIO);
861         }
862
863         rc = vdev_init_from_nvlist(vdevs, &top_vdev, is_newer);
864         if (rc)
865                 return (rc);
866
867         /*
868          * Add the toplevel vdev to the pool if its not already there.
869          */
870         STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
871                 if (top_vdev == pool_vdev)
872                         break;
873         if (!pool_vdev && top_vdev)
874                 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
875
876         /*
877          * We should already have created an incomplete vdev for this
878          * vdev. Find it and initialise it with our read proc.
879          */
880         vdev = vdev_find(guid);
881         if (vdev) {
882                 vdev->v_phys_read = read;
883                 vdev->v_read_priv = read_priv;
884         } else {
885                 printf("ZFS: inconsistent nvlist contents\n");
886                 return (EIO);
887         }
888
889         /*
890          * Re-evaluate top-level vdev state.
891          */
892         vdev_set_state(top_vdev);
893
894         /*
895          * Ok, we are happy with the pool so far. Lets find
896          * the best uberblock and then we can actually access
897          * the contents of the pool.
898          */
899         for (i = 0;
900              i < VDEV_UBERBLOCK_RING >> UBERBLOCK_SHIFT;
901              i++) {
902                 off = offsetof(vdev_label_t, vl_uberblock);
903                 off += i << UBERBLOCK_SHIFT;
904                 BP_ZERO(&bp);
905                 DVA_SET_OFFSET(&bp.blk_dva[0], off);
906                 BP_SET_LSIZE(&bp, 1 << UBERBLOCK_SHIFT);
907                 BP_SET_PSIZE(&bp, 1 << UBERBLOCK_SHIFT);
908                 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
909                 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
910                 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
911                 if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
912                         continue;
913
914                 up = (const struct uberblock *) upbuf;
915                 if (up->ub_magic != UBERBLOCK_MAGIC)
916                         continue;
917                 if (up->ub_txg < spa->spa_txg)
918                         continue;
919                 if (up->ub_txg > spa->spa_uberblock.ub_txg) {
920                         spa->spa_uberblock = *up;
921                 } else if (up->ub_txg == spa->spa_uberblock.ub_txg) {
922                         if (up->ub_timestamp > spa->spa_uberblock.ub_timestamp)
923                                 spa->spa_uberblock = *up;
924                 }
925         }
926
927         if (spap)
928                 *spap = spa;
929         return (0);
930 }
931
932 static int
933 ilog2(int n)
934 {
935         int v;
936
937         for (v = 0; v < 32; v++)
938                 if (n == (1 << v))
939                         return v;
940         return -1;
941 }
942
943 static int
944 zio_read_gang(spa_t *spa, const blkptr_t *bp, const dva_t *dva, void *buf)
945 {
946         zio_gbh_phys_t zio_gb;
947         vdev_t *vdev;
948         int vdevid;
949         off_t offset;
950         int i;
951
952         vdevid = DVA_GET_VDEV(dva);
953         offset = DVA_GET_OFFSET(dva);
954         STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink)
955                 if (vdev->v_id == vdevid)
956                         break;
957         if (!vdev || !vdev->v_read)
958                 return (EIO);
959         if (vdev->v_read(vdev, NULL, &zio_gb, offset, SPA_GANGBLOCKSIZE))
960                 return (EIO);
961
962         for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
963                 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
964
965                 if (BP_IS_HOLE(gbp))
966                         continue;
967                 if (zio_read(spa, gbp, buf))
968                         return (EIO);
969                 buf = (char*)buf + BP_GET_PSIZE(gbp);
970         }
971  
972         return (0);
973 }
974
975 static int
976 zio_read(spa_t *spa, const blkptr_t *bp, void *buf)
977 {
978         int cpfunc = BP_GET_COMPRESS(bp);
979         size_t lsize = BP_GET_LSIZE(bp);
980         size_t psize = BP_GET_PSIZE(bp);
981         void *pbuf;
982         int i;
983
984         zfs_reset_temp();
985         if (cpfunc != ZIO_COMPRESS_OFF)
986                 pbuf = zfs_alloc_temp(psize);
987         else
988                 pbuf = buf;
989
990         for (i = 0; i < SPA_DVAS_PER_BP; i++) {
991                 const dva_t *dva = &bp->blk_dva[i];
992                 vdev_t *vdev;
993                 int vdevid;
994                 off_t offset;
995
996                 if (!dva->dva_word[0] && !dva->dva_word[1])
997                         continue;
998
999                 if (DVA_GET_GANG(dva)) {
1000                         if (zio_read_gang(spa, bp, dva, buf))
1001                                 continue;
1002                 } else {
1003                         vdevid = DVA_GET_VDEV(dva);
1004                         offset = DVA_GET_OFFSET(dva);
1005                         STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink)
1006                                 if (vdev->v_id == vdevid)
1007                                         break;
1008                         if (!vdev || !vdev->v_read) {
1009                                 continue;
1010                         }
1011                         if (vdev->v_read(vdev, bp, pbuf, offset, psize))
1012                                 continue;
1013
1014                         if (cpfunc != ZIO_COMPRESS_OFF) {
1015                                 if (zio_decompress_data(cpfunc, pbuf, psize,
1016                                     buf, lsize))
1017                                         return (EIO);
1018                         }
1019                 }
1020
1021                 return (0);
1022         }
1023         printf("ZFS: i/o error - all block copies unavailable\n");
1024
1025         return (EIO);
1026 }
1027
1028 static int
1029 dnode_read(spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1030 {
1031         int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1032         int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1033         int nlevels = dnode->dn_nlevels;
1034         int i, rc;
1035
1036         /*
1037          * Note: bsize may not be a power of two here so we need to do an
1038          * actual divide rather than a bitshift.
1039          */
1040         while (buflen > 0) {
1041                 uint64_t bn = offset / bsize;
1042                 int boff = offset % bsize;
1043                 int ibn;
1044                 const blkptr_t *indbp;
1045                 blkptr_t bp;
1046
1047                 if (bn > dnode->dn_maxblkid)
1048                         return (EIO);
1049
1050                 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1051                         goto cached;
1052
1053                 indbp = dnode->dn_blkptr;
1054                 for (i = 0; i < nlevels; i++) {
1055                         /*
1056                          * Copy the bp from the indirect array so that
1057                          * we can re-use the scratch buffer for multi-level
1058                          * objects.
1059                          */
1060                         ibn = bn >> ((nlevels - i - 1) * ibshift);
1061                         ibn &= ((1 << ibshift) - 1);
1062                         bp = indbp[ibn];
1063                         rc = zio_read(spa, &bp, dnode_cache_buf);
1064                         if (rc)
1065                                 return (rc);
1066                         indbp = (const blkptr_t *) dnode_cache_buf;
1067                 }
1068                 dnode_cache_obj = dnode;
1069                 dnode_cache_bn = bn;
1070         cached:
1071
1072                 /*
1073                  * The buffer contains our data block. Copy what we
1074                  * need from it and loop.
1075                  */ 
1076                 i = bsize - boff;
1077                 if (i > buflen) i = buflen;
1078                 memcpy(buf, &dnode_cache_buf[boff], i);
1079                 buf = ((char*) buf) + i;
1080                 offset += i;
1081                 buflen -= i;
1082         }
1083
1084         return (0);
1085 }
1086
1087 /*
1088  * Lookup a value in a microzap directory. Assumes that the zap
1089  * scratch buffer contains the directory contents.
1090  */
1091 static int
1092 mzap_lookup(spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1093 {
1094         const mzap_phys_t *mz;
1095         const mzap_ent_phys_t *mze;
1096         size_t size;
1097         int chunks, i;
1098
1099         /*
1100          * Microzap objects use exactly one block. Read the whole
1101          * thing.
1102          */
1103         size = dnode->dn_datablkszsec * 512;
1104
1105         mz = (const mzap_phys_t *) zap_scratch;
1106         chunks = size / MZAP_ENT_LEN - 1;
1107
1108         for (i = 0; i < chunks; i++) {
1109                 mze = &mz->mz_chunk[i];
1110                 if (!strcmp(mze->mze_name, name)) {
1111                         *value = mze->mze_value;
1112                         return (0);
1113                 }
1114         }
1115
1116         return (ENOENT);
1117 }
1118
1119 /*
1120  * Compare a name with a zap leaf entry. Return non-zero if the name
1121  * matches.
1122  */
1123 static int
1124 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1125 {
1126         size_t namelen;
1127         const zap_leaf_chunk_t *nc;
1128         const char *p;
1129
1130         namelen = zc->l_entry.le_name_length;
1131                         
1132         nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1133         p = name;
1134         while (namelen > 0) {
1135                 size_t len;
1136                 len = namelen;
1137                 if (len > ZAP_LEAF_ARRAY_BYTES)
1138                         len = ZAP_LEAF_ARRAY_BYTES;
1139                 if (memcmp(p, nc->l_array.la_array, len))
1140                         return (0);
1141                 p += len;
1142                 namelen -= len;
1143                 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1144         }
1145
1146         return 1;
1147 }
1148
1149 /*
1150  * Extract a uint64_t value from a zap leaf entry.
1151  */
1152 static uint64_t
1153 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1154 {
1155         const zap_leaf_chunk_t *vc;
1156         int i;
1157         uint64_t value;
1158         const uint8_t *p;
1159
1160         vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1161         for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1162                 value = (value << 8) | p[i];
1163         }
1164
1165         return value;
1166 }
1167
1168 /*
1169  * Lookup a value in a fatzap directory. Assumes that the zap scratch
1170  * buffer contains the directory header.
1171  */
1172 static int
1173 fzap_lookup(spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1174 {
1175         int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1176         zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1177         fat_zap_t z;
1178         uint64_t *ptrtbl;
1179         uint64_t hash;
1180         int rc;
1181
1182         if (zh.zap_magic != ZAP_MAGIC)
1183                 return (EIO);
1184
1185         z.zap_block_shift = ilog2(bsize);
1186         z.zap_phys = (zap_phys_t *) zap_scratch;
1187
1188         /*
1189          * Figure out where the pointer table is and read it in if necessary.
1190          */
1191         if (zh.zap_ptrtbl.zt_blk) {
1192                 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1193                                zap_scratch, bsize);
1194                 if (rc)
1195                         return (rc);
1196                 ptrtbl = (uint64_t *) zap_scratch;
1197         } else {
1198                 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1199         }
1200
1201         hash = zap_hash(zh.zap_salt, name);
1202
1203         zap_leaf_t zl;
1204         zl.l_bs = z.zap_block_shift;
1205
1206         off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1207         zap_leaf_chunk_t *zc;
1208
1209         rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1210         if (rc)
1211                 return (rc);
1212
1213         zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1214
1215         /*
1216          * Make sure this chunk matches our hash.
1217          */
1218         if (zl.l_phys->l_hdr.lh_prefix_len > 0
1219             && zl.l_phys->l_hdr.lh_prefix
1220             != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1221                 return (ENOENT);
1222
1223         /*
1224          * Hash within the chunk to find our entry.
1225          */
1226         int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1227         int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1228         h = zl.l_phys->l_hash[h];
1229         if (h == 0xffff)
1230                 return (ENOENT);
1231         zc = &ZAP_LEAF_CHUNK(&zl, h);
1232         while (zc->l_entry.le_hash != hash) {
1233                 if (zc->l_entry.le_next == 0xffff) {
1234                         zc = 0;
1235                         break;
1236                 }
1237                 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1238         }
1239         if (fzap_name_equal(&zl, zc, name)) {
1240                 *value = fzap_leaf_value(&zl, zc);
1241                 return (0);
1242         }
1243
1244         return (ENOENT);
1245 }
1246
1247 /*
1248  * Lookup a name in a zap object and return its value as a uint64_t.
1249  */
1250 static int
1251 zap_lookup(spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1252 {
1253         int rc;
1254         uint64_t zap_type;
1255         size_t size = dnode->dn_datablkszsec * 512;
1256
1257         rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1258         if (rc)
1259                 return (rc);
1260
1261         zap_type = *(uint64_t *) zap_scratch;
1262         if (zap_type == ZBT_MICRO)
1263                 return mzap_lookup(spa, dnode, name, value);
1264         else
1265                 return fzap_lookup(spa, dnode, name, value);
1266 }
1267
1268 #ifdef BOOT2
1269
1270 /*
1271  * List a microzap directory. Assumes that the zap scratch buffer contains
1272  * the directory contents.
1273  */
1274 static int
1275 mzap_list(spa_t *spa, const dnode_phys_t *dnode)
1276 {
1277         const mzap_phys_t *mz;
1278         const mzap_ent_phys_t *mze;
1279         size_t size;
1280         int chunks, i;
1281
1282         /*
1283          * Microzap objects use exactly one block. Read the whole
1284          * thing.
1285          */
1286         size = dnode->dn_datablkszsec * 512;
1287         mz = (const mzap_phys_t *) zap_scratch;
1288         chunks = size / MZAP_ENT_LEN - 1;
1289
1290         for (i = 0; i < chunks; i++) {
1291                 mze = &mz->mz_chunk[i];
1292                 if (mze->mze_name[0])
1293                         //printf("%-32s 0x%llx\n", mze->mze_name, mze->mze_value);
1294                         printf("%s\n", mze->mze_name);
1295         }
1296
1297         return (0);
1298 }
1299
1300 /*
1301  * List a fatzap directory. Assumes that the zap scratch buffer contains
1302  * the directory header.
1303  */
1304 static int
1305 fzap_list(spa_t *spa, const dnode_phys_t *dnode)
1306 {
1307         int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1308         zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1309         fat_zap_t z;
1310         int i, j;
1311
1312         if (zh.zap_magic != ZAP_MAGIC)
1313                 return (EIO);
1314
1315         z.zap_block_shift = ilog2(bsize);
1316         z.zap_phys = (zap_phys_t *) zap_scratch;
1317
1318         /*
1319          * This assumes that the leaf blocks start at block 1. The
1320          * documentation isn't exactly clear on this.
1321          */
1322         zap_leaf_t zl;
1323         zl.l_bs = z.zap_block_shift;
1324         for (i = 0; i < zh.zap_num_leafs; i++) {
1325                 off_t off = (i + 1) << zl.l_bs;
1326                 char name[256], *p;
1327                 uint64_t value;
1328
1329                 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1330                         return (EIO);
1331
1332                 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1333
1334                 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1335                         zap_leaf_chunk_t *zc, *nc;
1336                         int namelen;
1337
1338                         zc = &ZAP_LEAF_CHUNK(&zl, j);
1339                         if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1340                                 continue;
1341                         namelen = zc->l_entry.le_name_length;
1342                         if (namelen > sizeof(name))
1343                                 namelen = sizeof(name);
1344                         
1345                         /*
1346                          * Paste the name back together.
1347                          */
1348                         nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1349                         p = name;
1350                         while (namelen > 0) {
1351                                 int len;
1352                                 len = namelen;
1353                                 if (len > ZAP_LEAF_ARRAY_BYTES)
1354                                         len = ZAP_LEAF_ARRAY_BYTES;
1355                                 memcpy(p, nc->l_array.la_array, len);
1356                                 p += len;
1357                                 namelen -= len;
1358                                 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1359                         }
1360
1361                         /*
1362                          * Assume the first eight bytes of the value are
1363                          * a uint64_t.
1364                          */
1365                         value = fzap_leaf_value(&zl, zc);
1366
1367                         printf("%s 0x%llx\n", name, value);
1368                 }
1369         }
1370
1371         return (0);
1372 }
1373
1374 /*
1375  * List a zap directory.
1376  */
1377 static int
1378 zap_list(spa_t *spa, const dnode_phys_t *dnode)
1379 {
1380         uint64_t zap_type;
1381         size_t size = dnode->dn_datablkszsec * 512;
1382
1383         if (dnode_read(spa, dnode, 0, zap_scratch, size))
1384                 return (EIO);
1385
1386         zap_type = *(uint64_t *) zap_scratch;
1387         if (zap_type == ZBT_MICRO)
1388                 return mzap_list(spa, dnode);
1389         else
1390                 return fzap_list(spa, dnode);
1391 }
1392
1393 #endif
1394
1395 static int
1396 objset_get_dnode(spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1397 {
1398         off_t offset;
1399
1400         offset = objnum * sizeof(dnode_phys_t);
1401         return dnode_read(spa, &os->os_meta_dnode, offset,
1402                 dnode, sizeof(dnode_phys_t));
1403 }
1404
1405 /*
1406  * Find the object set given the object number of its dataset object
1407  * and return its details in *objset
1408  */
1409 static int
1410 zfs_mount_dataset(spa_t *spa, uint64_t objnum, objset_phys_t *objset)
1411 {
1412         dnode_phys_t dataset;
1413         dsl_dataset_phys_t *ds;
1414
1415         if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1416                 printf("ZFS: can't find dataset %llu\n", objnum);
1417                 return (EIO);
1418         }
1419
1420         ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1421         if (zio_read(spa, &ds->ds_bp, objset)) {
1422                 printf("ZFS: can't read object set for dataset %llu\n", objnum);
1423                 return (EIO);
1424         }
1425
1426         return (0);
1427 }
1428
1429 /*
1430  * Find the object set pointed to by the BOOTFS property or the root
1431  * dataset if there is none and return its details in *objset
1432  */
1433 static int
1434 zfs_mount_root(spa_t *spa, objset_phys_t *objset)
1435 {
1436         dnode_phys_t dir, propdir;
1437         uint64_t props, bootfs, root;
1438
1439         /*
1440          * Start with the MOS directory object.
1441          */
1442         if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
1443                 printf("ZFS: can't read MOS object directory\n");
1444                 return (EIO);
1445         }
1446
1447         /*
1448          * Lookup the pool_props and see if we can find a bootfs.
1449          */
1450         if (zap_lookup(spa, &dir, DMU_POOL_PROPS, &props) == 0
1451              && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
1452              && zap_lookup(spa, &propdir, "bootfs", &bootfs) == 0
1453              && bootfs != 0)
1454                 return zfs_mount_dataset(spa, bootfs, objset);
1455
1456         /*
1457          * Lookup the root dataset directory
1458          */
1459         if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &root)
1460             || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
1461                 printf("ZFS: can't find root dsl_dir\n");
1462                 return (EIO);
1463         }
1464
1465         /*
1466          * Use the information from the dataset directory's bonus buffer
1467          * to find the dataset object and from that the object set itself.
1468          */
1469         dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
1470         return zfs_mount_dataset(spa, dd->dd_head_dataset_obj, objset);
1471 }
1472
1473 static int
1474 zfs_mount_pool(spa_t *spa)
1475 {
1476         /*
1477          * Find the MOS and work our way in from there.
1478          */
1479         if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
1480                 printf("ZFS: can't read MOS\n");
1481                 return (EIO);
1482         }
1483
1484         /*
1485          * Find the root object set
1486          */
1487         if (zfs_mount_root(spa, &spa->spa_root_objset)) {
1488                 printf("Can't find root filesystem - giving up\n");
1489                 return (EIO);
1490         }
1491
1492         return (0);
1493 }
1494
1495 /*
1496  * Lookup a file and return its dnode.
1497  */
1498 static int
1499 zfs_lookup(spa_t *spa, const char *upath, dnode_phys_t *dnode)
1500 {
1501         int rc;
1502         uint64_t objnum, rootnum, parentnum;
1503         dnode_phys_t dn;
1504         const znode_phys_t *zp = (const znode_phys_t *) dn.dn_bonus;
1505         const char *p, *q;
1506         char element[256];
1507         char path[1024];
1508         int symlinks_followed = 0;
1509
1510         if (spa->spa_root_objset.os_type != DMU_OST_ZFS) {
1511                 printf("ZFS: unexpected object set type %llu\n",
1512                        spa->spa_root_objset.os_type);
1513                 return (EIO);
1514         }
1515
1516         /*
1517          * Get the root directory dnode.
1518          */
1519         rc = objset_get_dnode(spa, &spa->spa_root_objset, MASTER_NODE_OBJ, &dn);
1520         if (rc)
1521                 return (rc);
1522
1523         rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, &rootnum);
1524         if (rc)
1525                 return (rc);
1526
1527         rc = objset_get_dnode(spa, &spa->spa_root_objset, rootnum, &dn);
1528         if (rc)
1529                 return (rc);
1530
1531         objnum = rootnum;
1532         p = upath;
1533         while (p && *p) {
1534                 while (*p == '/')
1535                         p++;
1536                 if (!*p)
1537                         break;
1538                 q = strchr(p, '/');
1539                 if (q) {
1540                         memcpy(element, p, q - p);
1541                         element[q - p] = 0;
1542                         p = q;
1543                 } else {
1544                         strcpy(element, p);
1545                         p = 0;
1546                 }
1547
1548                 if ((zp->zp_mode >> 12) != 0x4) {
1549                         return (ENOTDIR);
1550                 }
1551
1552                 parentnum = objnum;
1553                 rc = zap_lookup(spa, &dn, element, &objnum);
1554                 if (rc)
1555                         return (rc);
1556                 objnum = ZFS_DIRENT_OBJ(objnum);
1557
1558                 rc = objset_get_dnode(spa, &spa->spa_root_objset, objnum, &dn);
1559                 if (rc)
1560                         return (rc);
1561
1562                 /*
1563                  * Check for symlink.
1564                  */
1565                 if ((zp->zp_mode >> 12) == 0xa) {
1566                         if (symlinks_followed > 10)
1567                                 return (EMLINK);
1568                         symlinks_followed++;
1569
1570                         /*
1571                          * Read the link value and copy the tail of our
1572                          * current path onto the end.
1573                          */
1574                         if (p)
1575                                 strcpy(&path[zp->zp_size], p);
1576                         else
1577                                 path[zp->zp_size] = 0;
1578                         if (zp->zp_size + sizeof(znode_phys_t) <= dn.dn_bonuslen) {
1579                                 memcpy(path, &dn.dn_bonus[sizeof(znode_phys_t)],
1580                                         zp->zp_size);
1581                         } else {
1582                                 rc = dnode_read(spa, &dn, 0, path, zp->zp_size);
1583                                 if (rc)
1584                                         return (rc);
1585                         }
1586
1587                         /*
1588                          * Restart with the new path, starting either at
1589                          * the root or at the parent depending whether or
1590                          * not the link is relative.
1591                          */
1592                         p = path;
1593                         if (*p == '/')
1594                                 objnum = rootnum;
1595                         else
1596                                 objnum = parentnum;
1597                         objset_get_dnode(spa, &spa->spa_root_objset, objnum, &dn);
1598                 }
1599         }
1600
1601         *dnode = dn;
1602         return (0);
1603 }