The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/boot/zfs/zfsimpl.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: 5ffc7ab40e811dc814964b334b97cad2


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.