1 /*
   2  *  GRUB  --  GRand Unified Bootloader
   3  *  Copyright (C) 1999,2000,2001,2002,2003,2004  Free Software Foundation, Inc.
   4  *
   5  *  This program is free software; you can redistribute it and/or modify
   6  *  it under the terms of the GNU General Public License as published by
   7  *  the Free Software Foundation; either version 2 of the License, or
   8  *  (at your option) any later version.
   9  *
  10  *  This program is distributed in the hope that it will be useful,
  11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  *  GNU General Public License for more details.
  14  *
  15  *  You should have received a copy of the GNU General Public License
  16  *  along with this program; if not, write to the Free Software
  17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18  */
  19 
  20 /*
  21  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  22  * Use is subject to license terms.
  23  */
  24 
  25 /*
  26  * Copyright (c) 2012 by Delphix. All rights reserved.
  27  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
  28  */
  29 
  30 /*
  31  * The zfs plug-in routines for GRUB are:
  32  *
  33  * zfs_mount() - locates a valid uberblock of the root pool and reads
  34  *              in its MOS at the memory address MOS.
  35  *
  36  * zfs_open() - locates a plain file object by following the MOS
  37  *              and places its dnode at the memory address DNODE.
  38  *
  39  * zfs_read() - read in the data blocks pointed by the DNODE.
  40  *
  41  * ZFS_SCRATCH is used as a working area.
  42  *
  43  * (memory addr)   MOS      DNODE       ZFS_SCRATCH
  44  *                  |         |          |
  45  *          +-------V---------V----------V---------------+
  46  *   memory |       | dnode   | dnode    |  scratch      |
  47  *          |       | 512B    | 512B     |  area         |
  48  *          +--------------------------------------------+
  49  */
  50 
  51 #ifdef  FSYS_ZFS
  52 
  53 #include "shared.h"
  54 #include "filesys.h"
  55 #include "fsys_zfs.h"
  56 
  57 /* cache for a file block of the currently zfs_open()-ed file */
  58 static void *file_buf = NULL;
  59 static uint64_t file_start = 0;
  60 static uint64_t file_end = 0;
  61 
  62 /* cache for a dnode block */
  63 static dnode_phys_t *dnode_buf = NULL;
  64 static dnode_phys_t *dnode_mdn = NULL;
  65 static uint64_t dnode_start = 0;
  66 static uint64_t dnode_end = 0;
  67 
  68 static uint64_t pool_guid = 0;
  69 static uberblock_t current_uberblock;
  70 static char *stackbase;
  71 
  72 decomp_entry_t decomp_table[ZIO_COMPRESS_FUNCTIONS] =
  73 {
  74         {"inherit", 0},                 /* ZIO_COMPRESS_INHERIT */
  75         {"on", lzjb_decompress},        /* ZIO_COMPRESS_ON */
  76         {"off", 0},                     /* ZIO_COMPRESS_OFF */
  77         {"lzjb", lzjb_decompress},      /* ZIO_COMPRESS_LZJB */
  78         {"empty", 0},                   /* ZIO_COMPRESS_EMPTY */
  79         {"gzip-1", 0},                  /* ZIO_COMPRESS_GZIP_1 */
  80         {"gzip-2", 0},                  /* ZIO_COMPRESS_GZIP_2 */
  81         {"gzip-3", 0},                  /* ZIO_COMPRESS_GZIP_3 */
  82         {"gzip-4", 0},                  /* ZIO_COMPRESS_GZIP_4 */
  83         {"gzip-5", 0},                  /* ZIO_COMPRESS_GZIP_5 */
  84         {"gzip-6", 0},                  /* ZIO_COMPRESS_GZIP_6 */
  85         {"gzip-7", 0},                  /* ZIO_COMPRESS_GZIP_7 */
  86         {"gzip-8", 0},                  /* ZIO_COMPRESS_GZIP_8 */
  87         {"gzip-9", 0},                  /* ZIO_COMPRESS_GZIP_9 */
  88         {"zle", 0},                     /* ZIO_COMPRESS_ZLE */
  89         {"lz4", lz4_decompress}         /* ZIO_COMPRESS_LZ4 */
  90 };
  91 
  92 static int zio_read_data(blkptr_t *bp, void *buf, char *stack);
  93 
  94 /*
  95  * Our own version of bcmp().
  96  */
  97 static int
  98 zfs_bcmp(const void *s1, const void *s2, size_t n)
  99 {
 100         const uchar_t *ps1 = s1;
 101         const uchar_t *ps2 = s2;
 102 
 103         if (s1 != s2 && n != 0) {
 104                 do {
 105                         if (*ps1++ != *ps2++)
 106                                 return (1);
 107                 } while (--n != 0);
 108         }
 109 
 110         return (0);
 111 }
 112 
 113 /*
 114  * Our own version of log2().  Same thing as highbit()-1.
 115  */
 116 static int
 117 zfs_log2(uint64_t num)
 118 {
 119         int i = 0;
 120 
 121         while (num > 1) {
 122                 i++;
 123                 num = num >> 1;
 124         }
 125 
 126         return (i);
 127 }
 128 
 129 /* Checksum Functions */
 130 static void
 131 zio_checksum_off(const void *buf, uint64_t size, zio_cksum_t *zcp)
 132 {
 133         ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
 134 }
 135 
 136 /* Checksum Table and Values */
 137 zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
 138         {{NULL,                 NULL},                  0, 0,   "inherit"},
 139         {{NULL,                 NULL},                  0, 0,   "on"},
 140         {{zio_checksum_off,     zio_checksum_off},      0, 0,   "off"},
 141         {{zio_checksum_SHA256,  zio_checksum_SHA256},   1, 1,   "label"},
 142         {{zio_checksum_SHA256,  zio_checksum_SHA256},   1, 1,   "gang_header"},
 143         {{NULL,                 NULL},                  0, 0,   "zilog"},
 144         {{fletcher_2_native,    fletcher_2_byteswap},   0, 0,   "fletcher2"},
 145         {{fletcher_4_native,    fletcher_4_byteswap},   1, 0,   "fletcher4"},
 146         {{zio_checksum_SHA256,  zio_checksum_SHA256},   1, 0,   "SHA256"},
 147         {{NULL,                 NULL},                  0, 0,   "zilog2"},
 148         {{zio_checksum_EdonR512_256,    zio_checksum_EdonR512_256_byteswap},
 149             1, 0,       "edonr512/256"}
 150 };
 151 
 152 /*
 153  * zio_checksum_verify: Provides support for checksum verification.
 154  *
 155  * Fletcher2, Fletcher4, and SHA256 are supported.
 156  *
 157  * Return:
 158  *      -1 = Failure
 159  *       0 = Success
 160  */
 161 static int
 162 zio_checksum_verify(blkptr_t *bp, char *data, int size)
 163 {
 164         zio_cksum_t zc = bp->blk_cksum;
 165         uint32_t checksum = BP_GET_CHECKSUM(bp);
 166         int byteswap = BP_SHOULD_BYTESWAP(bp);
 167         zio_eck_t *zec = (zio_eck_t *)(data + size) - 1;
 168         zio_checksum_info_t *ci = &zio_checksum_table[checksum];
 169         zio_cksum_t actual_cksum, expected_cksum;
 170 
 171         /* byteswap is not supported */
 172         if (byteswap)
 173                 return (-1);
 174 
 175         if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func[0] == NULL)
 176                 return (-1);
 177 
 178         if (ci->ci_eck) {
 179                 expected_cksum = zec->zec_cksum;
 180                 zec->zec_cksum = zc;
 181                 ci->ci_func[0](data, size, &actual_cksum);
 182                 zec->zec_cksum = expected_cksum;
 183                 zc = expected_cksum;
 184 
 185         } else {
 186                 ci->ci_func[byteswap](data, size, &actual_cksum);
 187         }
 188 
 189         if ((actual_cksum.zc_word[0] - zc.zc_word[0]) |
 190             (actual_cksum.zc_word[1] - zc.zc_word[1]) |
 191             (actual_cksum.zc_word[2] - zc.zc_word[2]) |
 192             (actual_cksum.zc_word[3] - zc.zc_word[3]))
 193                 return (-1);
 194 
 195         return (0);
 196 }
 197 
 198 /*
 199  * vdev_label_start returns the physical disk offset (in bytes) of
 200  * label "l".
 201  */
 202 static uint64_t
 203 vdev_label_start(uint64_t psize, int l)
 204 {
 205         return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
 206             0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
 207 }
 208 
 209 /*
 210  * vdev_uberblock_compare takes two uberblock structures and returns an integer
 211  * indicating the more recent of the two.
 212  *      Return Value = 1 if ub2 is more recent
 213  *      Return Value = -1 if ub1 is more recent
 214  * The most recent uberblock is determined using its transaction number and
 215  * timestamp.  The uberblock with the highest transaction number is
 216  * considered "newer".  If the transaction numbers of the two blocks match, the
 217  * timestamps are compared to determine the "newer" of the two.
 218  */
 219 static int
 220 vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
 221 {
 222         if (ub1->ub_txg < ub2->ub_txg)
 223                 return (-1);
 224         if (ub1->ub_txg > ub2->ub_txg)
 225                 return (1);
 226 
 227         if (ub1->ub_timestamp < ub2->ub_timestamp)
 228                 return (-1);
 229         if (ub1->ub_timestamp > ub2->ub_timestamp)
 230                 return (1);
 231 
 232         return (0);
 233 }
 234 
 235 /*
 236  * Three pieces of information are needed to verify an uberblock: the magic
 237  * number, the version number, and the checksum.
 238  *
 239  * Return:
 240  *     0 - Success
 241  *    -1 - Failure
 242  */
 243 static int
 244 uberblock_verify(uberblock_t *uber, uint64_t ub_size, uint64_t offset)
 245 {
 246         blkptr_t bp;
 247 
 248         BP_ZERO(&bp);
 249         BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
 250         BP_SET_BYTEORDER(&bp, ZFS_HOST_BYTEORDER);
 251         ZIO_SET_CHECKSUM(&bp.blk_cksum, offset, 0, 0, 0);
 252 
 253         if (zio_checksum_verify(&bp, (char *)uber, ub_size) != 0)
 254                 return (-1);
 255 
 256         if (uber->ub_magic == UBERBLOCK_MAGIC &&
 257             SPA_VERSION_IS_SUPPORTED(uber->ub_version))
 258                 return (0);
 259 
 260         return (-1);
 261 }
 262 
 263 /*
 264  * Find the best uberblock.
 265  * Return:
 266  *    Success - Pointer to the best uberblock.
 267  *    Failure - NULL
 268  */
 269 static uberblock_t *
 270 find_bestub(char *ub_array, uint64_t ashift, uint64_t sector)
 271 {
 272         uberblock_t *ubbest = NULL;
 273         uberblock_t *ubnext;
 274         uint64_t offset, ub_size;
 275         int i;
 276 
 277         ub_size = VDEV_UBERBLOCK_SIZE(ashift);
 278 
 279         for (i = 0; i < VDEV_UBERBLOCK_COUNT(ashift); i++) {
 280                 ubnext = (uberblock_t *)ub_array;
 281                 ub_array += ub_size;
 282                 offset = (sector << SPA_MINBLOCKSHIFT) +
 283                     VDEV_UBERBLOCK_OFFSET(ashift, i);
 284 
 285                 if (uberblock_verify(ubnext, ub_size, offset) != 0)
 286                         continue;
 287 
 288                 if (ubbest == NULL ||
 289                     vdev_uberblock_compare(ubnext, ubbest) > 0)
 290                         ubbest = ubnext;
 291         }
 292 
 293         return (ubbest);
 294 }
 295 
 296 /*
 297  * Read a block of data based on the gang block address dva,
 298  * and put its data in buf.
 299  *
 300  * Return:
 301  *      0 - success
 302  *      1 - failure
 303  */
 304 static int
 305 zio_read_gang(blkptr_t *bp, dva_t *dva, void *buf, char *stack)
 306 {
 307         zio_gbh_phys_t *zio_gb;
 308         uint64_t offset, sector;
 309         blkptr_t tmpbp;
 310         int i;
 311 
 312         zio_gb = (zio_gbh_phys_t *)stack;
 313         stack += SPA_GANGBLOCKSIZE;
 314         offset = DVA_GET_OFFSET(dva);
 315         sector = DVA_OFFSET_TO_PHYS_SECTOR(offset);
 316 
 317         /* read in the gang block header */
 318         if (devread(sector, 0, SPA_GANGBLOCKSIZE, (char *)zio_gb) == 0) {
 319                 grub_printf("failed to read in a gang block header\n");
 320                 return (1);
 321         }
 322 
 323         /* self checksuming the gang block header */
 324         BP_ZERO(&tmpbp);
 325         BP_SET_CHECKSUM(&tmpbp, ZIO_CHECKSUM_GANG_HEADER);
 326         BP_SET_BYTEORDER(&tmpbp, ZFS_HOST_BYTEORDER);
 327         ZIO_SET_CHECKSUM(&tmpbp.blk_cksum, DVA_GET_VDEV(dva),
 328             DVA_GET_OFFSET(dva), bp->blk_birth, 0);
 329         if (zio_checksum_verify(&tmpbp, (char *)zio_gb, SPA_GANGBLOCKSIZE)) {
 330                 grub_printf("failed to checksum a gang block header\n");
 331                 return (1);
 332         }
 333 
 334         for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
 335                 if (zio_gb->zg_blkptr[i].blk_birth == 0)
 336                         continue;
 337 
 338                 if (zio_read_data(&zio_gb->zg_blkptr[i], buf, stack))
 339                         return (1);
 340                 buf += BP_GET_PSIZE(&zio_gb->zg_blkptr[i]);
 341         }
 342 
 343         return (0);
 344 }
 345 
 346 /*
 347  * Read in a block of raw data to buf.
 348  *
 349  * Return:
 350  *      0 - success
 351  *      1 - failure
 352  */
 353 static int
 354 zio_read_data(blkptr_t *bp, void *buf, char *stack)
 355 {
 356         int i, psize;
 357 
 358         psize = BP_GET_PSIZE(bp);
 359 
 360         /* pick a good dva from the block pointer */
 361         for (i = 0; i < SPA_DVAS_PER_BP; i++) {
 362                 uint64_t offset, sector;
 363 
 364                 if (bp->blk_dva[i].dva_word[0] == 0 &&
 365                     bp->blk_dva[i].dva_word[1] == 0)
 366                         continue;
 367 
 368                 if (DVA_GET_GANG(&bp->blk_dva[i])) {
 369                         if (zio_read_gang(bp, &bp->blk_dva[i], buf, stack) == 0)
 370                                 return (0);
 371                 } else {
 372                         /* read in a data block */
 373                         offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
 374                         sector = DVA_OFFSET_TO_PHYS_SECTOR(offset);
 375                         if (devread(sector, 0, psize, buf) != 0)
 376                                 return (0);
 377                 }
 378         }
 379 
 380         return (1);
 381 }
 382 
 383 /*
 384  * Read in a block of data, verify its checksum, decompress if needed,
 385  * and put the uncompressed data in buf.
 386  *
 387  * Return:
 388  *      0 - success
 389  *      errnum - failure
 390  */
 391 static int
 392 zio_read(blkptr_t *bp, void *buf, char *stack)
 393 {
 394         int lsize, psize, comp;
 395         char *retbuf;
 396 
 397         comp = BP_GET_COMPRESS(bp);
 398         lsize = BP_GET_LSIZE(bp);
 399         psize = BP_GET_PSIZE(bp);
 400 
 401         if ((unsigned int)comp >= ZIO_COMPRESS_FUNCTIONS ||
 402             (comp != ZIO_COMPRESS_OFF &&
 403             decomp_table[comp].decomp_func == NULL)) {
 404                 grub_printf("compression algorithm not supported\n");
 405                 return (ERR_FSYS_CORRUPT);
 406         }
 407 
 408         if ((char *)buf < stack && ((char *)buf) + lsize > stack) {
 409                 grub_printf("not enough memory allocated\n");
 410                 return (ERR_WONT_FIT);
 411         }
 412 
 413         retbuf = buf;
 414         if (comp != ZIO_COMPRESS_OFF) {
 415                 buf = stack;
 416                 stack += psize;
 417         }
 418 
 419         if (zio_read_data(bp, buf, stack) != 0) {
 420                 grub_printf("zio_read_data failed\n");
 421                 return (ERR_FSYS_CORRUPT);
 422         }
 423 
 424         if (zio_checksum_verify(bp, buf, psize) != 0) {
 425                 grub_printf("checksum verification failed\n");
 426                 return (ERR_FSYS_CORRUPT);
 427         }
 428 
 429         if (comp != ZIO_COMPRESS_OFF) {
 430                 if (decomp_table[comp].decomp_func(buf, retbuf, psize,
 431                     lsize) != 0) {
 432                         grub_printf("zio_read decompression failed\n");
 433                         return (ERR_FSYS_CORRUPT);
 434                 }
 435         }
 436 
 437         return (0);
 438 }
 439 
 440 /*
 441  * Get the block from a block id.
 442  * push the block onto the stack.
 443  *
 444  * Return:
 445  *      0 - success
 446  *      errnum - failure
 447  */
 448 static int
 449 dmu_read(dnode_phys_t *dn, uint64_t blkid, void *buf, char *stack)
 450 {
 451         int idx, level;
 452         blkptr_t *bp_array = dn->dn_blkptr;
 453         int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
 454         blkptr_t *bp, *tmpbuf;
 455 
 456         bp = (blkptr_t *)stack;
 457         stack += sizeof (blkptr_t);
 458 
 459         tmpbuf = (blkptr_t *)stack;
 460         stack += 1<<dn->dn_indblkshift;
 461 
 462         for (level = dn->dn_nlevels - 1; level >= 0; level--) {
 463                 idx = (blkid >> (epbs * level)) & ((1<<epbs)-1);
 464                 *bp = bp_array[idx];
 465                 if (level == 0)
 466                         tmpbuf = buf;
 467                 if (BP_IS_HOLE(bp)) {
 468                         grub_memset(buf, 0,
 469                             dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
 470                         break;
 471                 } else if (errnum = zio_read(bp, tmpbuf, stack)) {
 472                         return (errnum);
 473                 }
 474 
 475                 bp_array = tmpbuf;
 476         }
 477 
 478         return (0);
 479 }
 480 
 481 /*
 482  * mzap_lookup: Looks up property described by "name" and returns the value
 483  * in "value".
 484  *
 485  * Return:
 486  *      0 - success
 487  *      errnum - failure
 488  */
 489 static int
 490 mzap_lookup(mzap_phys_t *zapobj, int objsize, const char *name,
 491         uint64_t *value)
 492 {
 493         int i, chunks;
 494         mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk;
 495 
 496         chunks = objsize / MZAP_ENT_LEN - 1;
 497         for (i = 0; i < chunks; i++) {
 498                 if (grub_strcmp(mzap_ent[i].mze_name, name) == 0) {
 499                         *value = mzap_ent[i].mze_value;
 500                         return (0);
 501                 }
 502         }
 503 
 504         return (ERR_FSYS_CORRUPT);
 505 }
 506 
 507 static uint64_t
 508 zap_hash(uint64_t salt, const char *name)
 509 {
 510         static uint64_t table[256];
 511         const uint8_t *cp;
 512         uint8_t c;
 513         uint64_t crc = salt;
 514 
 515         if (table[128] == 0) {
 516                 uint64_t *ct;
 517                 int i, j;
 518                 for (i = 0; i < 256; i++) {
 519                         for (ct = table + i, *ct = i, j = 8; j > 0; j--)
 520                                 *ct = (*ct >> 1) ^ (-(*ct & 1) &
 521                                     ZFS_CRC64_POLY);
 522                 }
 523         }
 524 
 525         if (crc == 0 || table[128] != ZFS_CRC64_POLY) {
 526                 errnum = ERR_FSYS_CORRUPT;
 527                 return (0);
 528         }
 529 
 530         for (cp = (const uint8_t *)name; (c = *cp) != '\0'; cp++)
 531                 crc = (crc >> 8) ^ table[(crc ^ c) & 0xFF];
 532 
 533         /*
 534          * Only use 28 bits, since we need 4 bits in the cookie for the
 535          * collision differentiator.  We MUST use the high bits, since
 536          * those are the ones that we first pay attention to when
 537          * choosing the bucket.
 538          */
 539         crc &= ~((1ULL << (64 - 28)) - 1);
 540 
 541         return (crc);
 542 }
 543 
 544 /*
 545  * Only to be used on 8-bit arrays.
 546  * array_len is actual len in bytes (not encoded le_value_length).
 547  * buf is null-terminated.
 548  */
 549 static int
 550 zap_leaf_array_equal(zap_leaf_phys_t *l, int blksft, int chunk,
 551     int array_len, const char *buf)
 552 {
 553         int bseen = 0;
 554 
 555         while (bseen < array_len) {
 556                 struct zap_leaf_array *la =
 557                     &ZAP_LEAF_CHUNK(l, blksft, chunk).l_array;
 558                 int toread = MIN(array_len - bseen, ZAP_LEAF_ARRAY_BYTES);
 559 
 560                 if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
 561                         return (0);
 562 
 563                 if (zfs_bcmp(la->la_array, buf + bseen, toread) != 0)
 564                         break;
 565                 chunk = la->la_next;
 566                 bseen += toread;
 567         }
 568         return (bseen == array_len);
 569 }
 570 
 571 /*
 572  * Given a zap_leaf_phys_t, walk thru the zap leaf chunks to get the
 573  * value for the property "name".
 574  *
 575  * Return:
 576  *      0 - success
 577  *      errnum - failure
 578  */
 579 static int
 580 zap_leaf_lookup(zap_leaf_phys_t *l, int blksft, uint64_t h,
 581     const char *name, uint64_t *value)
 582 {
 583         uint16_t chunk;
 584         struct zap_leaf_entry *le;
 585 
 586         /* Verify if this is a valid leaf block */
 587         if (l->l_hdr.lh_block_type != ZBT_LEAF)
 588                 return (ERR_FSYS_CORRUPT);
 589         if (l->l_hdr.lh_magic != ZAP_LEAF_MAGIC)
 590                 return (ERR_FSYS_CORRUPT);
 591 
 592         for (chunk = l->l_hash[LEAF_HASH(blksft, h)];
 593             chunk != CHAIN_END; chunk = le->le_next) {
 594 
 595                 if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
 596                         return (ERR_FSYS_CORRUPT);
 597 
 598                 le = ZAP_LEAF_ENTRY(l, blksft, chunk);
 599 
 600                 /* Verify the chunk entry */
 601                 if (le->le_type != ZAP_CHUNK_ENTRY)
 602                         return (ERR_FSYS_CORRUPT);
 603 
 604                 if (le->le_hash != h)
 605                         continue;
 606 
 607                 if (zap_leaf_array_equal(l, blksft, le->le_name_chunk,
 608                     le->le_name_length, name)) {
 609 
 610                         struct zap_leaf_array *la;
 611                         uint8_t *ip;
 612 
 613                         if (le->le_int_size != 8 || le->le_value_length != 1)
 614                                 return (ERR_FSYS_CORRUPT);
 615 
 616                         /* get the uint64_t property value */
 617                         la = &ZAP_LEAF_CHUNK(l, blksft,
 618                             le->le_value_chunk).l_array;
 619                         ip = la->la_array;
 620 
 621                         *value = (uint64_t)ip[0] << 56 | (uint64_t)ip[1] << 48 |
 622                             (uint64_t)ip[2] << 40 | (uint64_t)ip[3] << 32 |
 623                             (uint64_t)ip[4] << 24 | (uint64_t)ip[5] << 16 |
 624                             (uint64_t)ip[6] << 8 | (uint64_t)ip[7];
 625 
 626                         return (0);
 627                 }
 628         }
 629 
 630         return (ERR_FSYS_CORRUPT);
 631 }
 632 
 633 /*
 634  * Fat ZAP lookup
 635  *
 636  * Return:
 637  *      0 - success
 638  *      errnum - failure
 639  */
 640 static int
 641 fzap_lookup(dnode_phys_t *zap_dnode, zap_phys_t *zap,
 642     const char *name, uint64_t *value, char *stack)
 643 {
 644         zap_leaf_phys_t *l;
 645         uint64_t hash, idx, blkid;
 646         int blksft = zfs_log2(zap_dnode->dn_datablkszsec << DNODE_SHIFT);
 647 
 648         /* Verify if this is a fat zap header block */
 649         if (zap->zap_magic != (uint64_t)ZAP_MAGIC ||
 650             zap->zap_flags != 0)
 651                 return (ERR_FSYS_CORRUPT);
 652 
 653         hash = zap_hash(zap->zap_salt, name);
 654         if (errnum)
 655                 return (errnum);
 656 
 657         /* get block id from index */
 658         if (zap->zap_ptrtbl.zt_numblks != 0) {
 659                 /* external pointer tables not supported */
 660                 return (ERR_FSYS_CORRUPT);
 661         }
 662         idx = ZAP_HASH_IDX(hash, zap->zap_ptrtbl.zt_shift);
 663         blkid = ((uint64_t *)zap)[idx + (1<<(blksft-3-1))];
 664 
 665         /* Get the leaf block */
 666         l = (zap_leaf_phys_t *)stack;
 667         stack += 1<<blksft;
 668         if ((1<<blksft) < sizeof (zap_leaf_phys_t))
 669                 return (ERR_FSYS_CORRUPT);
 670         if (errnum = dmu_read(zap_dnode, blkid, l, stack))
 671                 return (errnum);
 672 
 673         return (zap_leaf_lookup(l, blksft, hash, name, value));
 674 }
 675 
 676 /*
 677  * Read in the data of a zap object and find the value for a matching
 678  * property name.
 679  *
 680  * Return:
 681  *      0 - success
 682  *      errnum - failure
 683  */
 684 static int
 685 zap_lookup(dnode_phys_t *zap_dnode, const char *name, uint64_t *val,
 686     char *stack)
 687 {
 688         uint64_t block_type;
 689         int size;
 690         void *zapbuf;
 691 
 692         /* Read in the first block of the zap object data. */
 693         zapbuf = stack;
 694         size = zap_dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
 695         stack += size;
 696 
 697         if ((errnum = dmu_read(zap_dnode, 0, zapbuf, stack)) != 0)
 698                 return (errnum);
 699 
 700         block_type = *((uint64_t *)zapbuf);
 701 
 702         if (block_type == ZBT_MICRO) {
 703                 return (mzap_lookup(zapbuf, size, name, val));
 704         } else if (block_type == ZBT_HEADER) {
 705                 /* this is a fat zap */
 706                 return (fzap_lookup(zap_dnode, zapbuf, name,
 707                     val, stack));
 708         }
 709 
 710         return (ERR_FSYS_CORRUPT);
 711 }
 712 
 713 typedef struct zap_attribute {
 714         int za_integer_length;
 715         uint64_t za_num_integers;
 716         uint64_t za_first_integer;
 717         char *za_name;
 718 } zap_attribute_t;
 719 
 720 typedef int (zap_cb_t)(zap_attribute_t *za, void *arg, char *stack);
 721 
 722 static int
 723 zap_iterate(dnode_phys_t *zap_dnode, zap_cb_t *cb, void *arg, char *stack)
 724 {
 725         uint32_t size = zap_dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
 726         zap_attribute_t za;
 727         int i;
 728         mzap_phys_t *mzp = (mzap_phys_t *)stack;
 729         stack += size;
 730 
 731         if ((errnum = dmu_read(zap_dnode, 0, mzp, stack)) != 0)
 732                 return (errnum);
 733 
 734         /*
 735          * Iteration over fatzap objects has not yet been implemented.
 736          * If we encounter a pool in which there are more features for
 737          * read than can fit inside a microzap (i.e., more than 2048
 738          * features for read), we can add support for fatzap iteration.
 739          * For now, fail.
 740          */
 741         if (mzp->mz_block_type != ZBT_MICRO) {
 742                 grub_printf("feature information stored in fatzap, pool "
 743                     "version not supported\n");
 744                 return (1);
 745         }
 746 
 747         za.za_integer_length = 8;
 748         za.za_num_integers = 1;
 749         for (i = 0; i < size / MZAP_ENT_LEN - 1; i++) {
 750                 mzap_ent_phys_t *mzep = &mzp->mz_chunk[i];
 751                 int err;
 752 
 753                 za.za_first_integer = mzep->mze_value;
 754                 za.za_name = mzep->mze_name;
 755                 err = cb(&za, arg, stack);
 756                 if (err != 0)
 757                         return (err);
 758         }
 759 
 760         return (0);
 761 }
 762 
 763 /*
 764  * Get the dnode of an object number from the metadnode of an object set.
 765  *
 766  * Input
 767  *      mdn - metadnode to get the object dnode
 768  *      objnum - object number for the object dnode
 769  *      buf - data buffer that holds the returning dnode
 770  *      stack - scratch area
 771  *
 772  * Return:
 773  *      0 - success
 774  *      errnum - failure
 775  */
 776 static int
 777 dnode_get(dnode_phys_t *mdn, uint64_t objnum, uint8_t type, dnode_phys_t *buf,
 778         char *stack)
 779 {
 780         uint64_t blkid, blksz; /* the block id this object dnode is in */
 781         int epbs; /* shift of number of dnodes in a block */
 782         int idx; /* index within a block */
 783         dnode_phys_t *dnbuf;
 784 
 785         blksz = mdn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
 786         epbs = zfs_log2(blksz) - DNODE_SHIFT;
 787         blkid = objnum >> epbs;
 788         idx = objnum & ((1<<epbs)-1);
 789 
 790         if (dnode_buf != NULL && dnode_mdn == mdn &&
 791             objnum >= dnode_start && objnum < dnode_end) {
 792                 grub_memmove(buf, &dnode_buf[idx], DNODE_SIZE);
 793                 VERIFY_DN_TYPE(buf, type);
 794                 return (0);
 795         }
 796 
 797         if (dnode_buf && blksz == 1<<DNODE_BLOCK_SHIFT) {
 798                 dnbuf = dnode_buf;
 799                 dnode_mdn = mdn;
 800                 dnode_start = blkid << epbs;
 801                 dnode_end = (blkid + 1) << epbs;
 802         } else {
 803                 dnbuf = (dnode_phys_t *)stack;
 804                 stack += blksz;
 805         }
 806 
 807         if (errnum = dmu_read(mdn, blkid, (char *)dnbuf, stack))
 808                 return (errnum);
 809 
 810         grub_memmove(buf, &dnbuf[idx], DNODE_SIZE);
 811         VERIFY_DN_TYPE(buf, type);
 812 
 813         return (0);
 814 }
 815 
 816 /*
 817  * Check if this is a special file that resides at the top
 818  * dataset of the pool. Currently this is the GRUB menu,
 819  * boot signature and boot signature backup.
 820  * str starts with '/'.
 821  */
 822 static int
 823 is_top_dataset_file(char *str)
 824 {
 825         char *tptr;
 826 
 827         if ((tptr = grub_strstr(str, "menu.lst")) &&
 828             (tptr[8] == '\0' || tptr[8] == ' ') &&
 829             *(tptr-1) == '/')
 830                 return (1);
 831 
 832         if (grub_strncmp(str, BOOTSIGN_DIR"/",
 833             grub_strlen(BOOTSIGN_DIR) + 1) == 0)
 834                 return (1);
 835 
 836         if (grub_strcmp(str, BOOTSIGN_BACKUP) == 0)
 837                 return (1);
 838 
 839         return (0);
 840 }
 841 
 842 static int
 843 check_feature(zap_attribute_t *za, void *arg, char *stack)
 844 {
 845         const char **names = arg;
 846         int i;
 847 
 848         if (za->za_first_integer == 0)
 849                 return (0);
 850 
 851         for (i = 0; names[i] != NULL; i++) {
 852                 if (grub_strcmp(za->za_name, names[i]) == 0) {
 853                         return (0);
 854                 }
 855         }
 856         grub_printf("missing feature for read '%s'\n", za->za_name);
 857         return (ERR_NEWER_VERSION);
 858 }
 859 
 860 /*
 861  * Get the file dnode for a given file name where mdn is the meta dnode
 862  * for this ZFS object set. When found, place the file dnode in dn.
 863  * The 'path' argument will be mangled.
 864  *
 865  * Return:
 866  *      0 - success
 867  *      errnum - failure
 868  */
 869 static int
 870 dnode_get_path(dnode_phys_t *mdn, char *path, dnode_phys_t *dn,
 871     char *stack)
 872 {
 873         uint64_t objnum, version;
 874         char *cname, ch;
 875 
 876         if (errnum = dnode_get(mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
 877             dn, stack))
 878                 return (errnum);
 879 
 880         if (errnum = zap_lookup(dn, ZPL_VERSION_STR, &version, stack))
 881                 return (errnum);
 882         if (version > ZPL_VERSION)
 883                 return (-1);
 884 
 885         if (errnum = zap_lookup(dn, ZFS_ROOT_OBJ, &objnum, stack))
 886                 return (errnum);
 887 
 888         if (errnum = dnode_get(mdn, objnum, DMU_OT_DIRECTORY_CONTENTS,
 889             dn, stack))
 890                 return (errnum);
 891 
 892         /* skip leading slashes */
 893         while (*path == '/')
 894                 path++;
 895 
 896         while (*path && !grub_isspace(*path)) {
 897 
 898                 /* get the next component name */
 899                 cname = path;
 900                 while (*path && !grub_isspace(*path) && *path != '/')
 901                         path++;
 902                 ch = *path;
 903                 *path = 0;   /* ensure null termination */
 904 
 905                 if (errnum = zap_lookup(dn, cname, &objnum, stack))
 906                         return (errnum);
 907 
 908                 objnum = ZFS_DIRENT_OBJ(objnum);
 909                 if (errnum = dnode_get(mdn, objnum, 0, dn, stack))
 910                         return (errnum);
 911 
 912                 *path = ch;
 913                 while (*path == '/')
 914                         path++;
 915         }
 916 
 917         /* We found the dnode for this file. Verify if it is a plain file. */
 918         VERIFY_DN_TYPE(dn, DMU_OT_PLAIN_FILE_CONTENTS);
 919 
 920         return (0);
 921 }
 922 
 923 /*
 924  * Get the default 'bootfs' property value from the rootpool.
 925  *
 926  * Return:
 927  *      0 - success
 928  *      errnum -failure
 929  */
 930 static int
 931 get_default_bootfsobj(dnode_phys_t *mosmdn, uint64_t *obj, char *stack)
 932 {
 933         uint64_t objnum = 0;
 934         dnode_phys_t *dn = (dnode_phys_t *)stack;
 935         stack += DNODE_SIZE;
 936 
 937         if (errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
 938             DMU_OT_OBJECT_DIRECTORY, dn, stack))
 939                 return (errnum);
 940 
 941         /*
 942          * find the object number for 'pool_props', and get the dnode
 943          * of the 'pool_props'.
 944          */
 945         if (zap_lookup(dn, DMU_POOL_PROPS, &objnum, stack))
 946                 return (ERR_FILESYSTEM_NOT_FOUND);
 947 
 948         if (errnum = dnode_get(mosmdn, objnum, DMU_OT_POOL_PROPS, dn, stack))
 949                 return (errnum);
 950 
 951         if (zap_lookup(dn, ZPOOL_PROP_BOOTFS, &objnum, stack))
 952                 return (ERR_FILESYSTEM_NOT_FOUND);
 953 
 954         if (!objnum)
 955                 return (ERR_FILESYSTEM_NOT_FOUND);
 956 
 957         *obj = objnum;
 958         return (0);
 959 }
 960 
 961 /*
 962  * List of pool features that the grub implementation of ZFS supports for
 963  * read. Note that features that are only required for write do not need
 964  * to be listed here since grub opens pools in read-only mode.
 965  */
 966 static const char *spa_feature_names[] = {
 967         "org.illumos:lz4_compress",
 968         "org.illumos:edonr_cksum",
 969         NULL
 970 };
 971 
 972 /*
 973  * Checks whether the MOS features that are active are supported by this
 974  * (GRUB's) implementation of ZFS.
 975  *
 976  * Return:
 977  *      0: Success.
 978  *      errnum: Failure.
 979  */
 980 static int
 981 check_mos_features(dnode_phys_t *mosmdn, char *stack)
 982 {
 983         uint64_t objnum;
 984         dnode_phys_t *dn;
 985         uint8_t error = 0;
 986 
 987         dn = (dnode_phys_t *)stack;
 988         stack += DNODE_SIZE;
 989 
 990         if ((errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
 991             DMU_OT_OBJECT_DIRECTORY, dn, stack)) != 0)
 992                 return (errnum);
 993 
 994         /*
 995          * Find the object number for 'features_for_read' and retrieve its
 996          * corresponding dnode. Note that we don't check features_for_write
 997          * because GRUB is not opening the pool for write.
 998          */
 999         if ((errnum = zap_lookup(dn, DMU_POOL_FEATURES_FOR_READ, &objnum,
1000             stack)) != 0)
1001                 return (errnum);
1002 
1003         if ((errnum = dnode_get(mosmdn, objnum, DMU_OTN_ZAP_METADATA,
1004             dn, stack)) != 0)
1005                 return (errnum);
1006 
1007         return (zap_iterate(dn, check_feature, spa_feature_names, stack));
1008 }
1009 
1010 /*
1011  * Given a MOS metadnode, get the metadnode of a given filesystem name (fsname),
1012  * e.g. pool/rootfs, or a given object number (obj), e.g. the object number
1013  * of pool/rootfs.
1014  *
1015  * If no fsname and no obj are given, return the DSL_DIR metadnode.
1016  * If fsname is given, return its metadnode and its matching object number.
1017  * If only obj is given, return the metadnode for this object number.
1018  *
1019  * Return:
1020  *      0 - success
1021  *      errnum - failure
1022  */
1023 static int
1024 get_objset_mdn(dnode_phys_t *mosmdn, char *fsname, uint64_t *obj,
1025     dnode_phys_t *mdn, char *stack)
1026 {
1027         uint64_t objnum, headobj;
1028         char *cname, ch;
1029         blkptr_t *bp;
1030         objset_phys_t *osp;
1031         int issnapshot = 0;
1032         char *snapname;
1033 
1034         if (fsname == NULL && obj) {
1035                 headobj = *obj;
1036                 goto skip;
1037         }
1038 
1039         if (errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
1040             DMU_OT_OBJECT_DIRECTORY, mdn, stack))
1041                 return (errnum);
1042 
1043         if (errnum = zap_lookup(mdn, DMU_POOL_ROOT_DATASET, &objnum,
1044             stack))
1045                 return (errnum);
1046 
1047         if (errnum = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR, mdn, stack))
1048                 return (errnum);
1049 
1050         if (fsname == NULL) {
1051                 headobj =
1052                     ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_head_dataset_obj;
1053                 goto skip;
1054         }
1055 
1056         /* take out the pool name */
1057         while (*fsname && !grub_isspace(*fsname) && *fsname != '/')
1058                 fsname++;
1059 
1060         while (*fsname && !grub_isspace(*fsname)) {
1061                 uint64_t childobj;
1062 
1063                 while (*fsname == '/')
1064                         fsname++;
1065 
1066                 cname = fsname;
1067                 while (*fsname && !grub_isspace(*fsname) && *fsname != '/')
1068                         fsname++;
1069                 ch = *fsname;
1070                 *fsname = 0;
1071 
1072                 snapname = cname;
1073                 while (*snapname && !grub_isspace(*snapname) && *snapname !=
1074                     '@')
1075                         snapname++;
1076                 if (*snapname == '@') {
1077                         issnapshot = 1;
1078                         *snapname = 0;
1079                 }
1080                 childobj =
1081                     ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_child_dir_zapobj;
1082                 if (errnum = dnode_get(mosmdn, childobj,
1083                     DMU_OT_DSL_DIR_CHILD_MAP, mdn, stack))
1084                         return (errnum);
1085 
1086                 if (zap_lookup(mdn, cname, &objnum, stack))
1087                         return (ERR_FILESYSTEM_NOT_FOUND);
1088 
1089                 if (errnum = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR,
1090                     mdn, stack))
1091                         return (errnum);
1092 
1093                 *fsname = ch;
1094                 if (issnapshot)
1095                         *snapname = '@';
1096         }
1097         headobj = ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_head_dataset_obj;
1098         if (obj)
1099                 *obj = headobj;
1100 
1101 skip:
1102         if (errnum = dnode_get(mosmdn, headobj, DMU_OT_DSL_DATASET, mdn, stack))
1103                 return (errnum);
1104         if (issnapshot) {
1105                 uint64_t snapobj;
1106 
1107                 snapobj = ((dsl_dataset_phys_t *)DN_BONUS(mdn))->
1108                     ds_snapnames_zapobj;
1109 
1110                 if (errnum = dnode_get(mosmdn, snapobj,
1111                     DMU_OT_DSL_DS_SNAP_MAP, mdn, stack))
1112                         return (errnum);
1113                 if (zap_lookup(mdn, snapname + 1, &headobj, stack))
1114                         return (ERR_FILESYSTEM_NOT_FOUND);
1115                 if (errnum = dnode_get(mosmdn, headobj,
1116                     DMU_OT_DSL_DATASET, mdn, stack))
1117                         return (errnum);
1118                 if (obj)
1119                         *obj = headobj;
1120         }
1121 
1122         bp = &((dsl_dataset_phys_t *)DN_BONUS(mdn))->ds_bp;
1123         osp = (objset_phys_t *)stack;
1124         stack += sizeof (objset_phys_t);
1125         if (errnum = zio_read(bp, osp, stack))
1126                 return (errnum);
1127 
1128         grub_memmove((char *)mdn, (char *)&osp->os_meta_dnode, DNODE_SIZE);
1129 
1130         return (0);
1131 }
1132 
1133 /*
1134  * For a given XDR packed nvlist, verify the first 4 bytes and move on.
1135  *
1136  * An XDR packed nvlist is encoded as (comments from nvs_xdr_create) :
1137  *
1138  *      encoding method/host endian     (4 bytes)
1139  *      nvl_version                     (4 bytes)
1140  *      nvl_nvflag                      (4 bytes)
1141  *      encoded nvpairs:
1142  *              encoded size of the nvpair      (4 bytes)
1143  *              decoded size of the nvpair      (4 bytes)
1144  *              name string size                (4 bytes)
1145  *              name string data                (sizeof(NV_ALIGN4(string))
1146  *              data type                       (4 bytes)
1147  *              # of elements in the nvpair     (4 bytes)
1148  *              data
1149  *      2 zero's for the last nvpair
1150  *              (end of the entire list)        (8 bytes)
1151  *
1152  * Return:
1153  *      0 - success
1154  *      1 - failure
1155  */
1156 static int
1157 nvlist_unpack(char *nvlist, char **out)
1158 {
1159         /* Verify if the 1st and 2nd byte in the nvlist are valid. */
1160         if (nvlist[0] != NV_ENCODE_XDR || nvlist[1] != HOST_ENDIAN)
1161                 return (1);
1162 
1163         *out = nvlist + 4;
1164         return (0);
1165 }
1166 
1167 static char *
1168 nvlist_array(char *nvlist, int index)
1169 {
1170         int i, encode_size;
1171 
1172         for (i = 0; i < index; i++) {
1173                 /* skip the header, nvl_version, and nvl_nvflag */
1174                 nvlist = nvlist + 4 * 2;
1175 
1176                 while (encode_size = BSWAP_32(*(uint32_t *)nvlist))
1177                         nvlist += encode_size; /* goto the next nvpair */
1178 
1179                 nvlist = nvlist + 4 * 2; /* skip the ending 2 zeros - 8 bytes */
1180         }
1181 
1182         return (nvlist);
1183 }
1184 
1185 /*
1186  * The nvlist_next_nvpair() function returns a handle to the next nvpair in the
1187  * list following nvpair. If nvpair is NULL, the first pair is returned. If
1188  * nvpair is the last pair in the nvlist, NULL is returned.
1189  */
1190 static char *
1191 nvlist_next_nvpair(char *nvl, char *nvpair)
1192 {
1193         char *cur, *prev;
1194         int encode_size;
1195 
1196         if (nvl == NULL)
1197                 return (NULL);
1198 
1199         if (nvpair == NULL) {
1200                 /* skip over nvl_version and nvl_nvflag */
1201                 nvpair = nvl + 4 * 2;
1202         } else {
1203                 /* skip to the next nvpair */
1204                 encode_size = BSWAP_32(*(uint32_t *)nvpair);
1205                 nvpair += encode_size;
1206         }
1207 
1208         /* 8 bytes of 0 marks the end of the list */
1209         if (*(uint64_t *)nvpair == 0)
1210                 return (NULL);
1211 
1212         return (nvpair);
1213 }
1214 
1215 /*
1216  * This function returns 0 on success and 1 on failure. On success, a string
1217  * containing the name of nvpair is saved in buf.
1218  */
1219 static int
1220 nvpair_name(char *nvp, char *buf, int buflen)
1221 {
1222         int len;
1223 
1224         /* skip over encode/decode size */
1225         nvp += 4 * 2;
1226 
1227         len = BSWAP_32(*(uint32_t *)nvp);
1228         if (buflen < len + 1)
1229                 return (1);
1230 
1231         grub_memmove(buf, nvp + 4, len);
1232         buf[len] = '\0';
1233 
1234         return (0);
1235 }
1236 
1237 /*
1238  * This function retrieves the value of the nvpair in the form of enumerated
1239  * type data_type_t. This is used to determine the appropriate type to pass to
1240  * nvpair_value().
1241  */
1242 static int
1243 nvpair_type(char *nvp)
1244 {
1245         int name_len, type;
1246 
1247         /* skip over encode/decode size */
1248         nvp += 4 * 2;
1249 
1250         /* skip over name_len */
1251         name_len = BSWAP_32(*(uint32_t *)nvp);
1252         nvp += 4;
1253 
1254         /* skip over name */
1255         nvp = nvp + ((name_len + 3) & ~3); /* align */
1256 
1257         type = BSWAP_32(*(uint32_t *)nvp);
1258 
1259         return (type);
1260 }
1261 
1262 static int
1263 nvpair_value(char *nvp, void *val, int valtype, int *nelmp)
1264 {
1265         int name_len, type, slen;
1266         char *strval = val;
1267         uint64_t *intval = val;
1268 
1269         /* skip over encode/decode size */
1270         nvp += 4 * 2;
1271 
1272         /* skip over name_len */
1273         name_len = BSWAP_32(*(uint32_t *)nvp);
1274         nvp += 4;
1275 
1276         /* skip over name */
1277         nvp = nvp + ((name_len + 3) & ~3); /* align */
1278 
1279         /* skip over type */
1280         type = BSWAP_32(*(uint32_t *)nvp);
1281         nvp += 4;
1282 
1283         if (type == valtype) {
1284                 int nelm;
1285 
1286                 nelm = BSWAP_32(*(uint32_t *)nvp);
1287                 if (valtype != DATA_TYPE_BOOLEAN && nelm < 1)
1288                         return (1);
1289                 nvp += 4;
1290 
1291                 switch (valtype) {
1292                 case DATA_TYPE_BOOLEAN:
1293                         return (0);
1294 
1295                 case DATA_TYPE_STRING:
1296                         slen = BSWAP_32(*(uint32_t *)nvp);
1297                         nvp += 4;
1298                         grub_memmove(strval, nvp, slen);
1299                         strval[slen] = '\0';
1300                         return (0);
1301 
1302                 case DATA_TYPE_UINT64:
1303                         *intval = BSWAP_64(*(uint64_t *)nvp);
1304                         return (0);
1305 
1306                 case DATA_TYPE_NVLIST:
1307                         *(void **)val = (void *)nvp;
1308                         return (0);
1309 
1310                 case DATA_TYPE_NVLIST_ARRAY:
1311                         *(void **)val = (void *)nvp;
1312                         if (nelmp)
1313                                 *nelmp = nelm;
1314                         return (0);
1315                 }
1316         }
1317 
1318         return (1);
1319 }
1320 
1321 static int
1322 nvlist_lookup_value(char *nvlist, char *name, void *val, int valtype,
1323     int *nelmp)
1324 {
1325         char *nvpair;
1326 
1327         for (nvpair = nvlist_next_nvpair(nvlist, NULL);
1328             nvpair != NULL;
1329             nvpair = nvlist_next_nvpair(nvlist, nvpair)) {
1330                 int name_len = BSWAP_32(*(uint32_t *)(nvpair + 4 * 2));
1331                 char *nvp_name = nvpair + 4 * 3;
1332 
1333                 if ((grub_strncmp(nvp_name, name, name_len) == 0) &&
1334                     nvpair_type(nvpair) == valtype) {
1335                         return (nvpair_value(nvpair, val, valtype, nelmp));
1336                 }
1337         }
1338         return (1);
1339 }
1340 
1341 /*
1342  * Check if this vdev is online and is in a good state.
1343  */
1344 static int
1345 vdev_validate(char *nv)
1346 {
1347         uint64_t ival;
1348 
1349         if (nvlist_lookup_value(nv, ZPOOL_CONFIG_OFFLINE, &ival,
1350             DATA_TYPE_UINT64, NULL) == 0 ||
1351             nvlist_lookup_value(nv, ZPOOL_CONFIG_FAULTED, &ival,
1352             DATA_TYPE_UINT64, NULL) == 0 ||
1353             nvlist_lookup_value(nv, ZPOOL_CONFIG_REMOVED, &ival,
1354             DATA_TYPE_UINT64, NULL) == 0)
1355                 return (ERR_DEV_VALUES);
1356 
1357         return (0);
1358 }
1359 
1360 /*
1361  * Get a valid vdev pathname/devid from the boot device.
1362  * The caller should already allocate MAXPATHLEN memory for bootpath and devid.
1363  */
1364 static int
1365 vdev_get_bootpath(char *nv, uint64_t inguid, char *devid, char *bootpath,
1366     int is_spare)
1367 {
1368         char type[16];
1369 
1370         if (nvlist_lookup_value(nv, ZPOOL_CONFIG_TYPE, &type, DATA_TYPE_STRING,
1371             NULL))
1372                 return (ERR_FSYS_CORRUPT);
1373 
1374         if (grub_strcmp(type, VDEV_TYPE_DISK) == 0) {
1375                 uint64_t guid;
1376 
1377                 if (vdev_validate(nv) != 0)
1378                         return (ERR_NO_BOOTPATH);
1379 
1380                 if (nvlist_lookup_value(nv, ZPOOL_CONFIG_GUID,
1381                     &guid, DATA_TYPE_UINT64, NULL) != 0)
1382                         return (ERR_NO_BOOTPATH);
1383 
1384                 if (guid != inguid)
1385                         return (ERR_NO_BOOTPATH);
1386 
1387                 /* for a spare vdev, pick the disk labeled with "is_spare" */
1388                 if (is_spare) {
1389                         uint64_t spare = 0;
1390                         (void) nvlist_lookup_value(nv, ZPOOL_CONFIG_IS_SPARE,
1391                             &spare, DATA_TYPE_UINT64, NULL);
1392                         if (!spare)
1393                                 return (ERR_NO_BOOTPATH);
1394                 }
1395 
1396                 if (nvlist_lookup_value(nv, ZPOOL_CONFIG_PHYS_PATH,
1397                     bootpath, DATA_TYPE_STRING, NULL) != 0)
1398                         bootpath[0] = '\0';
1399 
1400                 if (nvlist_lookup_value(nv, ZPOOL_CONFIG_DEVID,
1401                     devid, DATA_TYPE_STRING, NULL) != 0)
1402                         devid[0] = '\0';
1403 
1404                 if (grub_strlen(bootpath) >= MAXPATHLEN ||
1405                     grub_strlen(devid) >= MAXPATHLEN)
1406                         return (ERR_WONT_FIT);
1407 
1408                 return (0);
1409 
1410         } else if (grub_strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
1411             grub_strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
1412             (is_spare = (grub_strcmp(type, VDEV_TYPE_SPARE) == 0))) {
1413                 int nelm, i;
1414                 char *child;
1415 
1416                 if (nvlist_lookup_value(nv, ZPOOL_CONFIG_CHILDREN, &child,
1417                     DATA_TYPE_NVLIST_ARRAY, &nelm))
1418                         return (ERR_FSYS_CORRUPT);
1419 
1420                 for (i = 0; i < nelm; i++) {
1421                         char *child_i;
1422 
1423                         child_i = nvlist_array(child, i);
1424                         if (vdev_get_bootpath(child_i, inguid, devid,
1425                             bootpath, is_spare) == 0)
1426                                 return (0);
1427                 }
1428         }
1429 
1430         return (ERR_NO_BOOTPATH);
1431 }
1432 
1433 /*
1434  * Check the disk label information and retrieve needed vdev name-value pairs.
1435  *
1436  * Return:
1437  *      0 - success
1438  *      ERR_* - failure
1439  */
1440 static int
1441 check_pool_label(uint64_t sector, char *stack, char *outdevid,
1442     char *outpath, uint64_t *outguid, uint64_t *outashift, uint64_t *outversion)
1443 {
1444         vdev_phys_t *vdev;
1445         uint64_t pool_state, txg = 0;
1446         char *nvlist, *nv, *features;
1447         uint64_t diskguid;
1448 
1449         sector += (VDEV_SKIP_SIZE >> SPA_MINBLOCKSHIFT);
1450 
1451         /* Read in the vdev name-value pair list (112K). */
1452         if (devread(sector, 0, VDEV_PHYS_SIZE, stack) == 0)
1453                 return (ERR_READ);
1454 
1455         vdev = (vdev_phys_t *)stack;
1456         stack += sizeof (vdev_phys_t);
1457 
1458         if (nvlist_unpack(vdev->vp_nvlist, &nvlist))
1459                 return (ERR_FSYS_CORRUPT);
1460 
1461         if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_STATE, &pool_state,
1462             DATA_TYPE_UINT64, NULL))
1463                 return (ERR_FSYS_CORRUPT);
1464 
1465         if (pool_state == POOL_STATE_DESTROYED)
1466                 return (ERR_FILESYSTEM_NOT_FOUND);
1467 
1468         if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_NAME,
1469             current_rootpool, DATA_TYPE_STRING, NULL))
1470                 return (ERR_FSYS_CORRUPT);
1471 
1472         if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_TXG, &txg,
1473             DATA_TYPE_UINT64, NULL))
1474                 return (ERR_FSYS_CORRUPT);
1475 
1476         /* not an active device */
1477         if (txg == 0)
1478                 return (ERR_NO_BOOTPATH);
1479 
1480         if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_VERSION, outversion,
1481             DATA_TYPE_UINT64, NULL))
1482                 return (ERR_FSYS_CORRUPT);
1483         if (!SPA_VERSION_IS_SUPPORTED(*outversion))
1484                 return (ERR_NEWER_VERSION);
1485         if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_VDEV_TREE, &nv,
1486             DATA_TYPE_NVLIST, NULL))
1487                 return (ERR_FSYS_CORRUPT);
1488         if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_GUID, &diskguid,
1489             DATA_TYPE_UINT64, NULL))
1490                 return (ERR_FSYS_CORRUPT);
1491         if (nvlist_lookup_value(nv, ZPOOL_CONFIG_ASHIFT, outashift,
1492             DATA_TYPE_UINT64, NULL) != 0)
1493                 return (ERR_FSYS_CORRUPT);
1494         if (vdev_get_bootpath(nv, diskguid, outdevid, outpath, 0))
1495                 return (ERR_NO_BOOTPATH);
1496         if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_GUID, outguid,
1497             DATA_TYPE_UINT64, NULL))
1498                 return (ERR_FSYS_CORRUPT);
1499 
1500         if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_FEATURES_FOR_READ,
1501             &features, DATA_TYPE_NVLIST, NULL) == 0) {
1502                 char *nvp;
1503                 char *name = stack;
1504                 stack += MAXNAMELEN;
1505 
1506                 for (nvp = nvlist_next_nvpair(features, NULL);
1507                     nvp != NULL;
1508                     nvp = nvlist_next_nvpair(features, nvp)) {
1509                         zap_attribute_t za;
1510 
1511                         if (nvpair_name(nvp, name, MAXNAMELEN) != 0)
1512                                 return (ERR_FSYS_CORRUPT);
1513 
1514                         za.za_integer_length = 8;
1515                         za.za_num_integers = 1;
1516                         za.za_first_integer = 1;
1517                         za.za_name = name;
1518                         if (check_feature(&za, spa_feature_names, stack) != 0)
1519                                 return (ERR_NEWER_VERSION);
1520                 }
1521         }
1522 
1523         return (0);
1524 }
1525 
1526 /*
1527  * zfs_mount() locates a valid uberblock of the root pool and read in its MOS
1528  * to the memory address MOS.
1529  *
1530  * Return:
1531  *      1 - success
1532  *      0 - failure
1533  */
1534 int
1535 zfs_mount(void)
1536 {
1537         char *stack, *ub_array;
1538         int label = 0;
1539         uberblock_t *ubbest;
1540         objset_phys_t *osp;
1541         char tmp_bootpath[MAXNAMELEN];
1542         char tmp_devid[MAXNAMELEN];
1543         uint64_t tmp_guid, ashift, version;
1544         uint64_t adjpl = (uint64_t)part_length << SPA_MINBLOCKSHIFT;
1545         int err = errnum; /* preserve previous errnum state */
1546 
1547         /* if it's our first time here, zero the best uberblock out */
1548         if (best_drive == 0 && best_part == 0 && find_best_root) {
1549                 grub_memset(&current_uberblock, 0, sizeof (uberblock_t));
1550                 pool_guid = 0;
1551         }
1552 
1553         stackbase = ZFS_SCRATCH;
1554         stack = stackbase;
1555         ub_array = stack;
1556         stack += VDEV_UBERBLOCK_RING;
1557 
1558         osp = (objset_phys_t *)stack;
1559         stack += sizeof (objset_phys_t);
1560         adjpl = P2ALIGN(adjpl, (uint64_t)sizeof (vdev_label_t));
1561 
1562         for (label = 0; label < VDEV_LABELS; label++) {
1563 
1564                 /*
1565                  * some eltorito stacks don't give us a size and
1566                  * we end up setting the size to MAXUINT, further
1567                  * some of these devices stop working once a single
1568                  * read past the end has been issued. Checking
1569                  * for a maximum part_length and skipping the backup
1570                  * labels at the end of the slice/partition/device
1571                  * avoids breaking down on such devices.
1572                  */
1573                 if (part_length == MAXUINT && label == 2)
1574                         break;
1575 
1576                 uint64_t sector = vdev_label_start(adjpl,
1577                     label) >> SPA_MINBLOCKSHIFT;
1578 
1579                 /* Read in the uberblock ring (128K). */
1580                 if (devread(sector  +
1581                     ((VDEV_SKIP_SIZE + VDEV_PHYS_SIZE) >> SPA_MINBLOCKSHIFT),
1582                     0, VDEV_UBERBLOCK_RING, ub_array) == 0)
1583                         continue;
1584 
1585                 if (check_pool_label(sector, stack, tmp_devid,
1586                     tmp_bootpath, &tmp_guid, &ashift, &version))
1587                         continue;
1588 
1589                 if (pool_guid == 0)
1590                         pool_guid = tmp_guid;
1591 
1592                 if ((ubbest = find_bestub(ub_array, ashift, sector)) == NULL ||
1593                     zio_read(&ubbest->ub_rootbp, osp, stack) != 0)
1594                         continue;
1595 
1596                 VERIFY_OS_TYPE(osp, DMU_OST_META);
1597 
1598                 if (version >= SPA_VERSION_FEATURES &&
1599                     check_mos_features(&osp->os_meta_dnode, stack) != 0)
1600                         continue;
1601 
1602                 if (find_best_root && ((pool_guid != tmp_guid) ||
1603                     vdev_uberblock_compare(ubbest, &(current_uberblock)) <= 0))
1604                         continue;
1605 
1606                 /* Got the MOS. Save it at the memory addr MOS. */
1607                 grub_memmove(MOS, &osp->os_meta_dnode, DNODE_SIZE);
1608                 grub_memmove(&current_uberblock, ubbest, sizeof (uberblock_t));
1609                 grub_memmove(current_bootpath, tmp_bootpath, MAXNAMELEN);
1610                 grub_memmove(current_devid, tmp_devid, grub_strlen(tmp_devid));
1611                 is_zfs_mount = 1;
1612                 return (1);
1613         }
1614 
1615         /*
1616          * While some fs impls. (tftp) rely on setting and keeping
1617          * global errnums set, others won't reset it and will break
1618          * when issuing rawreads. The goal here is to simply not
1619          * have zfs mount attempts impact the previous state.
1620          */
1621         errnum = err;
1622         return (0);
1623 }
1624 
1625 /*
1626  * zfs_open() locates a file in the rootpool by following the
1627  * MOS and places the dnode of the file in the memory address DNODE.
1628  *
1629  * Return:
1630  *      1 - success
1631  *      0 - failure
1632  */
1633 int
1634 zfs_open(char *filename)
1635 {
1636         char *stack;
1637         dnode_phys_t *mdn;
1638 
1639         file_buf = NULL;
1640         stackbase = ZFS_SCRATCH;
1641         stack = stackbase;
1642 
1643         mdn = (dnode_phys_t *)stack;
1644         stack += sizeof (dnode_phys_t);
1645 
1646         dnode_mdn = NULL;
1647         dnode_buf = (dnode_phys_t *)stack;
1648         stack += 1<<DNODE_BLOCK_SHIFT;
1649 
1650         /*
1651          * menu.lst is placed at the root pool filesystem level,
1652          * do not goto 'current_bootfs'.
1653          */
1654         if (is_top_dataset_file(filename)) {
1655                 if (errnum = get_objset_mdn(MOS, NULL, NULL, mdn, stack))
1656                         return (0);
1657 
1658                 current_bootfs_obj = 0;
1659         } else {
1660                 if (current_bootfs[0] == '\0') {
1661                         /* Get the default root filesystem object number */
1662                         if (errnum = get_default_bootfsobj(MOS,
1663                             &current_bootfs_obj, stack))
1664                                 return (0);
1665 
1666                         if (errnum = get_objset_mdn(MOS, NULL,
1667                             &current_bootfs_obj, mdn, stack))
1668                                 return (0);
1669                 } else {
1670                         if (errnum = get_objset_mdn(MOS, current_bootfs,
1671                             &current_bootfs_obj, mdn, stack)) {
1672                                 grub_memset(current_bootfs, 0, MAXNAMELEN);
1673                                 return (0);
1674                         }
1675                 }
1676         }
1677 
1678         if (dnode_get_path(mdn, filename, DNODE, stack)) {
1679                 errnum = ERR_FILE_NOT_FOUND;
1680                 return (0);
1681         }
1682 
1683         /* get the file size and set the file position to 0 */
1684 
1685         /*
1686          * For DMU_OT_SA we will need to locate the SIZE attribute
1687          * attribute, which could be either in the bonus buffer
1688          * or the "spill" block.
1689          */
1690         if (DNODE->dn_bonustype == DMU_OT_SA) {
1691                 sa_hdr_phys_t *sahdrp;
1692                 int hdrsize;
1693 
1694                 if (DNODE->dn_bonuslen != 0) {
1695                         sahdrp = (sa_hdr_phys_t *)DN_BONUS(DNODE);
1696                 } else {
1697                         if (DNODE->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
1698                                 blkptr_t *bp = &DNODE->dn_spill;
1699                                 void *buf;
1700 
1701                                 buf = (void *)stack;
1702                                 stack += BP_GET_LSIZE(bp);
1703 
1704                                 /* reset errnum to rawread() failure */
1705                                 errnum = 0;
1706                                 if (zio_read(bp, buf, stack) != 0) {
1707                                         return (0);
1708                                 }
1709                                 sahdrp = buf;
1710                         } else {
1711                                 errnum = ERR_FSYS_CORRUPT;
1712                                 return (0);
1713                         }
1714                 }
1715                 hdrsize = SA_HDR_SIZE(sahdrp);
1716                 filemax = *(uint64_t *)((char *)sahdrp + hdrsize +
1717                     SA_SIZE_OFFSET);
1718         } else {
1719                 filemax = ((znode_phys_t *)DN_BONUS(DNODE))->zp_size;
1720         }
1721         filepos = 0;
1722 
1723         dnode_buf = NULL;
1724         return (1);
1725 }
1726 
1727 /*
1728  * zfs_read reads in the data blocks pointed by the DNODE.
1729  *
1730  * Return:
1731  *      len - the length successfully read in to the buffer
1732  *      0   - failure
1733  */
1734 int
1735 zfs_read(char *buf, int len)
1736 {
1737         char *stack;
1738         int blksz, length, movesize;
1739 
1740         if (file_buf == NULL) {
1741                 file_buf = stackbase;
1742                 stackbase += SPA_MAXBLOCKSIZE;
1743                 file_start = file_end = 0;
1744         }
1745         stack = stackbase;
1746 
1747         /*
1748          * If offset is in memory, move it into the buffer provided and return.
1749          */
1750         if (filepos >= file_start && filepos+len <= file_end) {
1751                 grub_memmove(buf, file_buf + filepos - file_start, len);
1752                 filepos += len;
1753                 return (len);
1754         }
1755 
1756         blksz = DNODE->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1757 
1758         /*
1759          * Entire Dnode is too big to fit into the space available.  We
1760          * will need to read it in chunks.  This could be optimized to
1761          * read in as large a chunk as there is space available, but for
1762          * now, this only reads in one data block at a time.
1763          */
1764         length = len;
1765         while (length) {
1766                 /*
1767                  * Find requested blkid and the offset within that block.
1768                  */
1769                 uint64_t blkid = filepos / blksz;
1770 
1771                 if (errnum = dmu_read(DNODE, blkid, file_buf, stack))
1772                         return (0);
1773 
1774                 file_start = blkid * blksz;
1775                 file_end = file_start + blksz;
1776 
1777                 movesize = MIN(length, file_end - filepos);
1778 
1779                 grub_memmove(buf, file_buf + filepos - file_start,
1780                     movesize);
1781                 buf += movesize;
1782                 length -= movesize;
1783                 filepos += movesize;
1784         }
1785 
1786         return (len);
1787 }
1788 
1789 /*
1790  * No-Op
1791  */
1792 int
1793 zfs_embed(int *start_sector, int needed_sectors)
1794 {
1795         return (1);
1796 }
1797 
1798 #endif /* FSYS_ZFS */