]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/boot/zfs/zfsimpl.c
MFC r211091:
[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 (strlen(path) > 5
474                             && path[0] == '/'
475                             && path[1] == 'd'
476                             && path[2] == 'e'
477                             && path[3] == 'v'
478                             && path[4] == '/')
479                                 path += 5;
480                         vdev->v_name = strdup(path);
481                 } else {
482                         if (!strcmp(type, "raidz")) {
483                                 if (vdev->v_nparity == 1)
484                                         vdev->v_name = "raidz1";
485                                 else
486                                         vdev->v_name = "raidz2";
487                         } else {
488                                 vdev->v_name = strdup(type);
489                         }
490                 }
491
492                 if (is_offline)
493                         vdev->v_state = VDEV_STATE_OFFLINE;
494                 else if (is_removed)
495                         vdev->v_state = VDEV_STATE_REMOVED;
496                 else if (is_faulted)
497                         vdev->v_state = VDEV_STATE_FAULTED;
498                 else if (is_degraded)
499                         vdev->v_state = VDEV_STATE_DEGRADED;
500                 else
501                         vdev->v_state = VDEV_STATE_HEALTHY;
502         } else {
503                 is_new = 0;
504
505                 if (is_newer) {
506                         /*
507                          * We've already seen this vdev, but from an older
508                          * vdev label, so let's refresh its state from the
509                          * newer label.
510                          */
511                         if (is_offline)
512                                 vdev->v_state = VDEV_STATE_OFFLINE;
513                         else if (is_removed)
514                                 vdev->v_state = VDEV_STATE_REMOVED;
515                         else if (is_faulted)
516                                 vdev->v_state = VDEV_STATE_FAULTED;
517                         else if (is_degraded)
518                                 vdev->v_state = VDEV_STATE_DEGRADED;
519                         else
520                                 vdev->v_state = VDEV_STATE_HEALTHY;
521                 }
522         }
523
524         rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN,
525                          DATA_TYPE_NVLIST_ARRAY, &nkids, &kids);
526         /*
527          * Its ok if we don't have any kids.
528          */
529         if (rc == 0) {
530                 vdev->v_nchildren = nkids;
531                 for (i = 0; i < nkids; i++) {
532                         rc = vdev_init_from_nvlist(kids, &kid, is_newer);
533                         if (rc)
534                                 return (rc);
535                         if (is_new)
536                                 STAILQ_INSERT_TAIL(&vdev->v_children, kid,
537                                                    v_childlink);
538                         kids = nvlist_next(kids);
539                 }
540         } else {
541                 vdev->v_nchildren = 0;
542         }
543
544         if (vdevp)
545                 *vdevp = vdev;
546         return (0);
547 }
548
549 static void
550 vdev_set_state(vdev_t *vdev)
551 {
552         vdev_t *kid;
553         int good_kids;
554         int bad_kids;
555
556         /*
557          * A mirror or raidz is healthy if all its kids are healthy. A
558          * mirror is degraded if any of its kids is healthy; a raidz
559          * is degraded if at most nparity kids are offline.
560          */
561         if (STAILQ_FIRST(&vdev->v_children)) {
562                 good_kids = 0;
563                 bad_kids = 0;
564                 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
565                         if (kid->v_state == VDEV_STATE_HEALTHY)
566                                 good_kids++;
567                         else
568                                 bad_kids++;
569                 }
570                 if (bad_kids == 0) {
571                         vdev->v_state = VDEV_STATE_HEALTHY;
572                 } else {
573                         if (vdev->v_read == vdev_mirror_read) {
574                                 if (good_kids) {
575                                         vdev->v_state = VDEV_STATE_DEGRADED;
576                                 } else {
577                                         vdev->v_state = VDEV_STATE_OFFLINE;
578                                 }
579                         } else if (vdev->v_read == vdev_raidz_read) {
580                                 if (bad_kids > vdev->v_nparity) {
581                                         vdev->v_state = VDEV_STATE_OFFLINE;
582                                 } else {
583                                         vdev->v_state = VDEV_STATE_DEGRADED;
584                                 }
585                         }
586                 }
587         }
588 }
589
590 static spa_t *
591 spa_find_by_guid(uint64_t guid)
592 {
593         spa_t *spa;
594
595         STAILQ_FOREACH(spa, &zfs_pools, spa_link)
596                 if (spa->spa_guid == guid)
597                         return (spa);
598
599         return (0);
600 }
601
602 #ifdef BOOT2
603
604 static spa_t *
605 spa_find_by_name(const char *name)
606 {
607         spa_t *spa;
608
609         STAILQ_FOREACH(spa, &zfs_pools, spa_link)
610                 if (!strcmp(spa->spa_name, name))
611                         return (spa);
612
613         return (0);
614 }
615
616 #endif
617
618 static spa_t *
619 spa_create(uint64_t guid)
620 {
621         spa_t *spa;
622
623         spa = malloc(sizeof(spa_t));
624         memset(spa, 0, sizeof(spa_t));
625         STAILQ_INIT(&spa->spa_vdevs);
626         spa->spa_guid = guid;
627         STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
628
629         return (spa);
630 }
631
632 static const char *
633 state_name(vdev_state_t state)
634 {
635         static const char* names[] = {
636                 "UNKNOWN",
637                 "CLOSED",
638                 "OFFLINE",
639                 "REMOVED",
640                 "CANT_OPEN",
641                 "FAULTED",
642                 "DEGRADED",
643                 "ONLINE"
644         };
645         return names[state];
646 }
647
648 #ifdef BOOT2
649
650 #define pager_printf printf
651
652 #else
653
654 static void
655 pager_printf(const char *fmt, ...)
656 {
657         char line[80];
658         va_list args;
659
660         va_start(args, fmt);
661         vsprintf(line, fmt, args);
662         va_end(args);
663         pager_output(line);
664 }
665
666 #endif
667
668 #define STATUS_FORMAT   "        %-16s %-10s\n"
669
670 static void
671 print_state(int indent, const char *name, vdev_state_t state)
672 {
673         int i;
674         char buf[512];
675
676         buf[0] = 0;
677         for (i = 0; i < indent; i++)
678                 strcat(buf, "  ");
679         strcat(buf, name);
680         pager_printf(STATUS_FORMAT, buf, state_name(state));
681         
682 }
683
684 static void
685 vdev_status(vdev_t *vdev, int indent)
686 {
687         vdev_t *kid;
688         print_state(indent, vdev->v_name, vdev->v_state);
689
690         STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
691                 vdev_status(kid, indent + 1);
692         }
693 }
694
695 static void
696 spa_status(spa_t *spa)
697 {
698         vdev_t *vdev;
699         int good_kids, bad_kids, degraded_kids;
700         vdev_state_t state;
701
702         pager_printf("  pool: %s\n", spa->spa_name);
703         pager_printf("config:\n\n");
704         pager_printf(STATUS_FORMAT, "NAME", "STATE");
705
706         good_kids = 0;
707         degraded_kids = 0;
708         bad_kids = 0;
709         STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
710                 if (vdev->v_state == VDEV_STATE_HEALTHY)
711                         good_kids++;
712                 else if (vdev->v_state == VDEV_STATE_DEGRADED)
713                         degraded_kids++;
714                 else
715                         bad_kids++;
716         }
717
718         state = VDEV_STATE_CLOSED;
719         if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
720                 state = VDEV_STATE_HEALTHY;
721         else if ((good_kids + degraded_kids) > 0)
722                 state = VDEV_STATE_DEGRADED;
723
724         print_state(0, spa->spa_name, state);
725         STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
726                 vdev_status(vdev, 1);
727         }
728 }
729
730 static void
731 spa_all_status(void)
732 {
733         spa_t *spa;
734         int first = 1;
735
736         STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
737                 if (!first)
738                         pager_printf("\n");
739                 first = 0;
740                 spa_status(spa);
741         }
742 }
743
744 static int
745 vdev_probe(vdev_phys_read_t *read, void *read_priv, spa_t **spap)
746 {
747         vdev_t vtmp;
748         vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
749         spa_t *spa;
750         vdev_t *vdev, *top_vdev, *pool_vdev;
751         off_t off;
752         blkptr_t bp;
753         const unsigned char *nvlist;
754         uint64_t val;
755         uint64_t guid;
756         uint64_t pool_txg, pool_guid;
757         const char *pool_name;
758         const unsigned char *vdevs;
759         int i, rc, is_newer;
760         char upbuf[1024];
761         const struct uberblock *up;
762
763         /*
764          * Load the vdev label and figure out which
765          * uberblock is most current.
766          */
767         memset(&vtmp, 0, sizeof(vtmp));
768         vtmp.v_phys_read = read;
769         vtmp.v_read_priv = read_priv;
770         off = offsetof(vdev_label_t, vl_vdev_phys);
771         BP_ZERO(&bp);
772         BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
773         BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
774         BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
775         BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
776         ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
777         if (vdev_read_phys(&vtmp, &bp, vdev_label, off, 0))
778                 return (EIO);
779
780         if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR) {
781                 return (EIO);
782         }
783
784         nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
785
786         if (nvlist_find(nvlist,
787                         ZPOOL_CONFIG_VERSION,
788                         DATA_TYPE_UINT64, 0, &val)) {
789                 return (EIO);
790         }
791
792         if (val > SPA_VERSION) {
793                 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
794                     (unsigned) val, (unsigned) SPA_VERSION);
795                 return (EIO);
796         }
797
798         if (nvlist_find(nvlist,
799                         ZPOOL_CONFIG_POOL_STATE,
800                         DATA_TYPE_UINT64, 0, &val)) {
801                 return (EIO);
802         }
803
804 #ifndef TEST
805         if (val != POOL_STATE_ACTIVE) {
806                 /*
807                  * Don't print a message here. If we happen to reboot
808                  * while where is an exported pool around, we don't
809                  * need a cascade of confusing messages during boot.
810                  */
811                 /*printf("ZFS: pool is not active\n");*/
812                 return (EIO);
813         }
814 #endif
815
816         if (nvlist_find(nvlist,
817                         ZPOOL_CONFIG_POOL_TXG,
818                         DATA_TYPE_UINT64, 0, &pool_txg)
819             || nvlist_find(nvlist,
820                            ZPOOL_CONFIG_POOL_GUID,
821                            DATA_TYPE_UINT64, 0, &pool_guid)
822             || nvlist_find(nvlist,
823                            ZPOOL_CONFIG_POOL_NAME,
824                            DATA_TYPE_STRING, 0, &pool_name)) {
825                 /*
826                  * Cache and spare devices end up here - just ignore
827                  * them.
828                  */
829                 /*printf("ZFS: can't find pool details\n");*/
830                 return (EIO);
831         }
832
833         /*
834          * Create the pool if this is the first time we've seen it.
835          */
836         spa = spa_find_by_guid(pool_guid);
837         if (!spa) {
838                 spa = spa_create(pool_guid);
839                 spa->spa_name = strdup(pool_name);
840         }
841         if (pool_txg > spa->spa_txg) {
842                 spa->spa_txg = pool_txg;
843                 is_newer = 1;
844         } else
845                 is_newer = 0;
846
847         /*
848          * Get the vdev tree and create our in-core copy of it.
849          * If we already have a vdev with this guid, this must
850          * be some kind of alias (overlapping slices, dangerously dedicated
851          * disks etc).
852          */
853         if (nvlist_find(nvlist,
854                         ZPOOL_CONFIG_GUID,
855                         DATA_TYPE_UINT64, 0, &guid)) {
856                 return (EIO);
857         }
858         vdev = vdev_find(guid);
859         if (vdev && vdev->v_phys_read)  /* Has this vdev already been inited? */
860                 return (EIO);
861
862         if (nvlist_find(nvlist,
863                         ZPOOL_CONFIG_VDEV_TREE,
864                         DATA_TYPE_NVLIST, 0, &vdevs)) {
865                 return (EIO);
866         }
867
868         rc = vdev_init_from_nvlist(vdevs, &top_vdev, is_newer);
869         if (rc)
870                 return (rc);
871
872         /*
873          * Add the toplevel vdev to the pool if its not already there.
874          */
875         STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
876                 if (top_vdev == pool_vdev)
877                         break;
878         if (!pool_vdev && top_vdev)
879                 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
880
881         /*
882          * We should already have created an incomplete vdev for this
883          * vdev. Find it and initialise it with our read proc.
884          */
885         vdev = vdev_find(guid);
886         if (vdev) {
887                 vdev->v_phys_read = read;
888                 vdev->v_read_priv = read_priv;
889         } else {
890                 printf("ZFS: inconsistent nvlist contents\n");
891                 return (EIO);
892         }
893
894         /*
895          * Re-evaluate top-level vdev state.
896          */
897         vdev_set_state(top_vdev);
898
899         /*
900          * Ok, we are happy with the pool so far. Lets find
901          * the best uberblock and then we can actually access
902          * the contents of the pool.
903          */
904         for (i = 0;
905              i < VDEV_UBERBLOCK_RING >> UBERBLOCK_SHIFT;
906              i++) {
907                 off = offsetof(vdev_label_t, vl_uberblock);
908                 off += i << UBERBLOCK_SHIFT;
909                 BP_ZERO(&bp);
910                 DVA_SET_OFFSET(&bp.blk_dva[0], off);
911                 BP_SET_LSIZE(&bp, 1 << UBERBLOCK_SHIFT);
912                 BP_SET_PSIZE(&bp, 1 << UBERBLOCK_SHIFT);
913                 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
914                 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
915                 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
916                 if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
917                         continue;
918
919                 up = (const struct uberblock *) upbuf;
920                 if (up->ub_magic != UBERBLOCK_MAGIC)
921                         continue;
922                 if (up->ub_txg < spa->spa_txg)
923                         continue;
924                 if (up->ub_txg > spa->spa_uberblock.ub_txg) {
925                         spa->spa_uberblock = *up;
926                 } else if (up->ub_txg == spa->spa_uberblock.ub_txg) {
927                         if (up->ub_timestamp > spa->spa_uberblock.ub_timestamp)
928                                 spa->spa_uberblock = *up;
929                 }
930         }
931
932         if (spap)
933                 *spap = spa;
934         return (0);
935 }
936
937 static int
938 ilog2(int n)
939 {
940         int v;
941
942         for (v = 0; v < 32; v++)
943                 if (n == (1 << v))
944                         return v;
945         return -1;
946 }
947
948 static int
949 zio_read_gang(spa_t *spa, const blkptr_t *bp, const dva_t *dva, void *buf)
950 {
951         zio_gbh_phys_t zio_gb;
952         vdev_t *vdev;
953         int vdevid;
954         off_t offset;
955         int i;
956
957         vdevid = DVA_GET_VDEV(dva);
958         offset = DVA_GET_OFFSET(dva);
959         STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink)
960                 if (vdev->v_id == vdevid)
961                         break;
962         if (!vdev || !vdev->v_read)
963                 return (EIO);
964         if (vdev->v_read(vdev, NULL, &zio_gb, offset, SPA_GANGBLOCKSIZE))
965                 return (EIO);
966
967         for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
968                 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
969
970                 if (BP_IS_HOLE(gbp))
971                         continue;
972                 if (zio_read(spa, gbp, buf))
973                         return (EIO);
974                 buf = (char*)buf + BP_GET_PSIZE(gbp);
975         }
976  
977         return (0);
978 }
979
980 static int
981 zio_read(spa_t *spa, const blkptr_t *bp, void *buf)
982 {
983         int cpfunc = BP_GET_COMPRESS(bp);
984         size_t lsize = BP_GET_LSIZE(bp);
985         size_t psize = BP_GET_PSIZE(bp);
986         void *pbuf;
987         int i;
988
989         zfs_reset_temp();
990         if (cpfunc != ZIO_COMPRESS_OFF)
991                 pbuf = zfs_alloc_temp(psize);
992         else
993                 pbuf = buf;
994
995         for (i = 0; i < SPA_DVAS_PER_BP; i++) {
996                 const dva_t *dva = &bp->blk_dva[i];
997                 vdev_t *vdev;
998                 int vdevid;
999                 off_t offset;
1000
1001                 if (!dva->dva_word[0] && !dva->dva_word[1])
1002                         continue;
1003
1004                 if (DVA_GET_GANG(dva)) {
1005                         if (zio_read_gang(spa, bp, dva, buf))
1006                                 continue;
1007                 } else {
1008                         vdevid = DVA_GET_VDEV(dva);
1009                         offset = DVA_GET_OFFSET(dva);
1010                         STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink)
1011                                 if (vdev->v_id == vdevid)
1012                                         break;
1013                         if (!vdev || !vdev->v_read) {
1014                                 continue;
1015                         }
1016                         if (vdev->v_read(vdev, bp, pbuf, offset, psize))
1017                                 continue;
1018
1019                         if (cpfunc != ZIO_COMPRESS_OFF) {
1020                                 if (zio_decompress_data(cpfunc, pbuf, psize,
1021                                     buf, lsize))
1022                                         return (EIO);
1023                         }
1024                 }
1025
1026                 return (0);
1027         }
1028         printf("ZFS: i/o error - all block copies unavailable\n");
1029
1030         return (EIO);
1031 }
1032
1033 static int
1034 dnode_read(spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1035 {
1036         int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1037         int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1038         int nlevels = dnode->dn_nlevels;
1039         int i, rc;
1040
1041         /*
1042          * Note: bsize may not be a power of two here so we need to do an
1043          * actual divide rather than a bitshift.
1044          */
1045         while (buflen > 0) {
1046                 uint64_t bn = offset / bsize;
1047                 int boff = offset % bsize;
1048                 int ibn;
1049                 const blkptr_t *indbp;
1050                 blkptr_t bp;
1051
1052                 if (bn > dnode->dn_maxblkid)
1053                         return (EIO);
1054
1055                 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1056                         goto cached;
1057
1058                 indbp = dnode->dn_blkptr;
1059                 for (i = 0; i < nlevels; i++) {
1060                         /*
1061                          * Copy the bp from the indirect array so that
1062                          * we can re-use the scratch buffer for multi-level
1063                          * objects.
1064                          */
1065                         ibn = bn >> ((nlevels - i - 1) * ibshift);
1066                         ibn &= ((1 << ibshift) - 1);
1067                         bp = indbp[ibn];
1068                         rc = zio_read(spa, &bp, dnode_cache_buf);
1069                         if (rc)
1070                                 return (rc);
1071                         indbp = (const blkptr_t *) dnode_cache_buf;
1072                 }
1073                 dnode_cache_obj = dnode;
1074                 dnode_cache_bn = bn;
1075         cached:
1076
1077                 /*
1078                  * The buffer contains our data block. Copy what we
1079                  * need from it and loop.
1080                  */ 
1081                 i = bsize - boff;
1082                 if (i > buflen) i = buflen;
1083                 memcpy(buf, &dnode_cache_buf[boff], i);
1084                 buf = ((char*) buf) + i;
1085                 offset += i;
1086                 buflen -= i;
1087         }
1088
1089         return (0);
1090 }
1091
1092 /*
1093  * Lookup a value in a microzap directory. Assumes that the zap
1094  * scratch buffer contains the directory contents.
1095  */
1096 static int
1097 mzap_lookup(spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1098 {
1099         const mzap_phys_t *mz;
1100         const mzap_ent_phys_t *mze;
1101         size_t size;
1102         int chunks, i;
1103
1104         /*
1105          * Microzap objects use exactly one block. Read the whole
1106          * thing.
1107          */
1108         size = dnode->dn_datablkszsec * 512;
1109
1110         mz = (const mzap_phys_t *) zap_scratch;
1111         chunks = size / MZAP_ENT_LEN - 1;
1112
1113         for (i = 0; i < chunks; i++) {
1114                 mze = &mz->mz_chunk[i];
1115                 if (!strcmp(mze->mze_name, name)) {
1116                         *value = mze->mze_value;
1117                         return (0);
1118                 }
1119         }
1120
1121         return (ENOENT);
1122 }
1123
1124 /*
1125  * Compare a name with a zap leaf entry. Return non-zero if the name
1126  * matches.
1127  */
1128 static int
1129 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1130 {
1131         size_t namelen;
1132         const zap_leaf_chunk_t *nc;
1133         const char *p;
1134
1135         namelen = zc->l_entry.le_name_length;
1136                         
1137         nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1138         p = name;
1139         while (namelen > 0) {
1140                 size_t len;
1141                 len = namelen;
1142                 if (len > ZAP_LEAF_ARRAY_BYTES)
1143                         len = ZAP_LEAF_ARRAY_BYTES;
1144                 if (memcmp(p, nc->l_array.la_array, len))
1145                         return (0);
1146                 p += len;
1147                 namelen -= len;
1148                 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1149         }
1150
1151         return 1;
1152 }
1153
1154 /*
1155  * Extract a uint64_t value from a zap leaf entry.
1156  */
1157 static uint64_t
1158 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1159 {
1160         const zap_leaf_chunk_t *vc;
1161         int i;
1162         uint64_t value;
1163         const uint8_t *p;
1164
1165         vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1166         for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1167                 value = (value << 8) | p[i];
1168         }
1169
1170         return value;
1171 }
1172
1173 /*
1174  * Lookup a value in a fatzap directory. Assumes that the zap scratch
1175  * buffer contains the directory header.
1176  */
1177 static int
1178 fzap_lookup(spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1179 {
1180         int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1181         zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1182         fat_zap_t z;
1183         uint64_t *ptrtbl;
1184         uint64_t hash;
1185         int rc;
1186
1187         if (zh.zap_magic != ZAP_MAGIC)
1188                 return (EIO);
1189
1190         z.zap_block_shift = ilog2(bsize);
1191         z.zap_phys = (zap_phys_t *) zap_scratch;
1192
1193         /*
1194          * Figure out where the pointer table is and read it in if necessary.
1195          */
1196         if (zh.zap_ptrtbl.zt_blk) {
1197                 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1198                                zap_scratch, bsize);
1199                 if (rc)
1200                         return (rc);
1201                 ptrtbl = (uint64_t *) zap_scratch;
1202         } else {
1203                 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1204         }
1205
1206         hash = zap_hash(zh.zap_salt, name);
1207
1208         zap_leaf_t zl;
1209         zl.l_bs = z.zap_block_shift;
1210
1211         off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1212         zap_leaf_chunk_t *zc;
1213
1214         rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1215         if (rc)
1216                 return (rc);
1217
1218         zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1219
1220         /*
1221          * Make sure this chunk matches our hash.
1222          */
1223         if (zl.l_phys->l_hdr.lh_prefix_len > 0
1224             && zl.l_phys->l_hdr.lh_prefix
1225             != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1226                 return (ENOENT);
1227
1228         /*
1229          * Hash within the chunk to find our entry.
1230          */
1231         int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1232         int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1233         h = zl.l_phys->l_hash[h];
1234         if (h == 0xffff)
1235                 return (ENOENT);
1236         zc = &ZAP_LEAF_CHUNK(&zl, h);
1237         while (zc->l_entry.le_hash != hash) {
1238                 if (zc->l_entry.le_next == 0xffff) {
1239                         zc = 0;
1240                         break;
1241                 }
1242                 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1243         }
1244         if (fzap_name_equal(&zl, zc, name)) {
1245                 *value = fzap_leaf_value(&zl, zc);
1246                 return (0);
1247         }
1248
1249         return (ENOENT);
1250 }
1251
1252 /*
1253  * Lookup a name in a zap object and return its value as a uint64_t.
1254  */
1255 static int
1256 zap_lookup(spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1257 {
1258         int rc;
1259         uint64_t zap_type;
1260         size_t size = dnode->dn_datablkszsec * 512;
1261
1262         rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1263         if (rc)
1264                 return (rc);
1265
1266         zap_type = *(uint64_t *) zap_scratch;
1267         if (zap_type == ZBT_MICRO)
1268                 return mzap_lookup(spa, dnode, name, value);
1269         else
1270                 return fzap_lookup(spa, dnode, name, value);
1271 }
1272
1273 #ifdef BOOT2
1274
1275 /*
1276  * List a microzap directory. Assumes that the zap scratch buffer contains
1277  * the directory contents.
1278  */
1279 static int
1280 mzap_list(spa_t *spa, const dnode_phys_t *dnode)
1281 {
1282         const mzap_phys_t *mz;
1283         const mzap_ent_phys_t *mze;
1284         size_t size;
1285         int chunks, i;
1286
1287         /*
1288          * Microzap objects use exactly one block. Read the whole
1289          * thing.
1290          */
1291         size = dnode->dn_datablkszsec * 512;
1292         mz = (const mzap_phys_t *) zap_scratch;
1293         chunks = size / MZAP_ENT_LEN - 1;
1294
1295         for (i = 0; i < chunks; i++) {
1296                 mze = &mz->mz_chunk[i];
1297                 if (mze->mze_name[0])
1298                         //printf("%-32s 0x%llx\n", mze->mze_name, mze->mze_value);
1299                         printf("%s\n", mze->mze_name);
1300         }
1301
1302         return (0);
1303 }
1304
1305 /*
1306  * List a fatzap directory. Assumes that the zap scratch buffer contains
1307  * the directory header.
1308  */
1309 static int
1310 fzap_list(spa_t *spa, const dnode_phys_t *dnode)
1311 {
1312         int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1313         zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1314         fat_zap_t z;
1315         int i, j;
1316
1317         if (zh.zap_magic != ZAP_MAGIC)
1318                 return (EIO);
1319
1320         z.zap_block_shift = ilog2(bsize);
1321         z.zap_phys = (zap_phys_t *) zap_scratch;
1322
1323         /*
1324          * This assumes that the leaf blocks start at block 1. The
1325          * documentation isn't exactly clear on this.
1326          */
1327         zap_leaf_t zl;
1328         zl.l_bs = z.zap_block_shift;
1329         for (i = 0; i < zh.zap_num_leafs; i++) {
1330                 off_t off = (i + 1) << zl.l_bs;
1331                 char name[256], *p;
1332                 uint64_t value;
1333
1334                 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1335                         return (EIO);
1336
1337                 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1338
1339                 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1340                         zap_leaf_chunk_t *zc, *nc;
1341                         int namelen;
1342
1343                         zc = &ZAP_LEAF_CHUNK(&zl, j);
1344                         if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1345                                 continue;
1346                         namelen = zc->l_entry.le_name_length;
1347                         if (namelen > sizeof(name))
1348                                 namelen = sizeof(name);
1349                         
1350                         /*
1351                          * Paste the name back together.
1352                          */
1353                         nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1354                         p = name;
1355                         while (namelen > 0) {
1356                                 int len;
1357                                 len = namelen;
1358                                 if (len > ZAP_LEAF_ARRAY_BYTES)
1359                                         len = ZAP_LEAF_ARRAY_BYTES;
1360                                 memcpy(p, nc->l_array.la_array, len);
1361                                 p += len;
1362                                 namelen -= len;
1363                                 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1364                         }
1365
1366                         /*
1367                          * Assume the first eight bytes of the value are
1368                          * a uint64_t.
1369                          */
1370                         value = fzap_leaf_value(&zl, zc);
1371
1372                         printf("%-32s 0x%llx\n", name, value);
1373                 }
1374         }
1375
1376         return (0);
1377 }
1378
1379 /*
1380  * List a zap directory.
1381  */
1382 static int
1383 zap_list(spa_t *spa, const dnode_phys_t *dnode)
1384 {
1385         uint64_t zap_type;
1386         size_t size = dnode->dn_datablkszsec * 512;
1387
1388         if (dnode_read(spa, dnode, 0, zap_scratch, size))
1389                 return (EIO);
1390
1391         zap_type = *(uint64_t *) zap_scratch;
1392         if (zap_type == ZBT_MICRO)
1393                 return mzap_list(spa, dnode);
1394         else
1395                 return fzap_list(spa, dnode);
1396 }
1397
1398 #endif
1399
1400 static int
1401 objset_get_dnode(spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1402 {
1403         off_t offset;
1404
1405         offset = objnum * sizeof(dnode_phys_t);
1406         return dnode_read(spa, &os->os_meta_dnode, offset,
1407                 dnode, sizeof(dnode_phys_t));
1408 }
1409
1410 /*
1411  * Find the object set given the object number of its dataset object
1412  * and return its details in *objset
1413  */
1414 static int
1415 zfs_mount_dataset(spa_t *spa, uint64_t objnum, objset_phys_t *objset)
1416 {
1417         dnode_phys_t dataset;
1418         dsl_dataset_phys_t *ds;
1419
1420         if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1421                 printf("ZFS: can't find dataset %llu\n", objnum);
1422                 return (EIO);
1423         }
1424
1425         ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1426         if (zio_read(spa, &ds->ds_bp, objset)) {
1427                 printf("ZFS: can't read object set for dataset %llu\n", objnum);
1428                 return (EIO);
1429         }
1430
1431         return (0);
1432 }
1433
1434 /*
1435  * Find the object set pointed to by the BOOTFS property or the root
1436  * dataset if there is none and return its details in *objset
1437  */
1438 static int
1439 zfs_mount_root(spa_t *spa, objset_phys_t *objset)
1440 {
1441         dnode_phys_t dir, propdir;
1442         uint64_t props, bootfs, root;
1443
1444         /*
1445          * Start with the MOS directory object.
1446          */
1447         if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
1448                 printf("ZFS: can't read MOS object directory\n");
1449                 return (EIO);
1450         }
1451
1452         /*
1453          * Lookup the pool_props and see if we can find a bootfs.
1454          */
1455         if (zap_lookup(spa, &dir, DMU_POOL_PROPS, &props) == 0
1456              && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
1457              && zap_lookup(spa, &propdir, "bootfs", &bootfs) == 0
1458              && bootfs != 0)
1459                 return zfs_mount_dataset(spa, bootfs, objset);
1460
1461         /*
1462          * Lookup the root dataset directory
1463          */
1464         if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &root)
1465             || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
1466                 printf("ZFS: can't find root dsl_dir\n");
1467                 return (EIO);
1468         }
1469
1470         /*
1471          * Use the information from the dataset directory's bonus buffer
1472          * to find the dataset object and from that the object set itself.
1473          */
1474         dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
1475         return zfs_mount_dataset(spa, dd->dd_head_dataset_obj, objset);
1476 }
1477
1478 static int
1479 zfs_mount_pool(spa_t *spa)
1480 {
1481         /*
1482          * Find the MOS and work our way in from there.
1483          */
1484         if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
1485                 printf("ZFS: can't read MOS\n");
1486                 return (EIO);
1487         }
1488
1489         /*
1490          * Find the root object set
1491          */
1492         if (zfs_mount_root(spa, &spa->spa_root_objset)) {
1493                 printf("Can't find root filesystem - giving up\n");
1494                 return (EIO);
1495         }
1496
1497         return (0);
1498 }
1499
1500 /*
1501  * Lookup a file and return its dnode.
1502  */
1503 static int
1504 zfs_lookup(spa_t *spa, const char *upath, dnode_phys_t *dnode)
1505 {
1506         int rc;
1507         uint64_t objnum, rootnum, parentnum;
1508         dnode_phys_t dn;
1509         const znode_phys_t *zp = (const znode_phys_t *) dn.dn_bonus;
1510         const char *p, *q;
1511         char element[256];
1512         char path[1024];
1513         int symlinks_followed = 0;
1514
1515         if (spa->spa_root_objset.os_type != DMU_OST_ZFS) {
1516                 printf("ZFS: unexpected object set type %llu\n",
1517                        spa->spa_root_objset.os_type);
1518                 return (EIO);
1519         }
1520
1521         /*
1522          * Get the root directory dnode.
1523          */
1524         rc = objset_get_dnode(spa, &spa->spa_root_objset, MASTER_NODE_OBJ, &dn);
1525         if (rc)
1526                 return (rc);
1527
1528         rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, &rootnum);
1529         if (rc)
1530                 return (rc);
1531
1532         rc = objset_get_dnode(spa, &spa->spa_root_objset, rootnum, &dn);
1533         if (rc)
1534                 return (rc);
1535
1536         objnum = rootnum;
1537         p = upath;
1538         while (p && *p) {
1539                 while (*p == '/')
1540                         p++;
1541                 if (!*p)
1542                         break;
1543                 q = strchr(p, '/');
1544                 if (q) {
1545                         memcpy(element, p, q - p);
1546                         element[q - p] = 0;
1547                         p = q;
1548                 } else {
1549                         strcpy(element, p);
1550                         p = 0;
1551                 }
1552
1553                 if ((zp->zp_mode >> 12) != 0x4) {
1554                         return (ENOTDIR);
1555                 }
1556
1557                 parentnum = objnum;
1558                 rc = zap_lookup(spa, &dn, element, &objnum);
1559                 if (rc)
1560                         return (rc);
1561                 objnum = ZFS_DIRENT_OBJ(objnum);
1562
1563                 rc = objset_get_dnode(spa, &spa->spa_root_objset, objnum, &dn);
1564                 if (rc)
1565                         return (rc);
1566
1567                 /*
1568                  * Check for symlink.
1569                  */
1570                 if ((zp->zp_mode >> 12) == 0xa) {
1571                         if (symlinks_followed > 10)
1572                                 return (EMLINK);
1573                         symlinks_followed++;
1574
1575                         /*
1576                          * Read the link value and copy the tail of our
1577                          * current path onto the end.
1578                          */
1579                         if (p)
1580                                 strcpy(&path[zp->zp_size], p);
1581                         else
1582                                 path[zp->zp_size] = 0;
1583                         if (zp->zp_size + sizeof(znode_phys_t) <= dn.dn_bonuslen) {
1584                                 memcpy(path, &dn.dn_bonus[sizeof(znode_phys_t)],
1585                                         zp->zp_size);
1586                         } else {
1587                                 rc = dnode_read(spa, &dn, 0, path, zp->zp_size);
1588                                 if (rc)
1589                                         return (rc);
1590                         }
1591
1592                         /*
1593                          * Restart with the new path, starting either at
1594                          * the root or at the parent depending whether or
1595                          * not the link is relative.
1596                          */
1597                         p = path;
1598                         if (*p == '/')
1599                                 objnum = rootnum;
1600                         else
1601                                 objnum = parentnum;
1602                         objset_get_dnode(spa, &spa->spa_root_objset, objnum, &dn);
1603                 }
1604         }
1605
1606         *dnode = dn;
1607         return (0);
1608 }