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