1 /* 2 * 3 * ZFS filesystem ported to u-boot by 4 * Jorgen Lundman <lundman at lundman.net> 5 * 6 * GRUB -- GRand Unified Bootloader 7 * Copyright (C) 1999,2000,2001,2002,2003,2004 8 * Free Software Foundation, Inc. 9 * Copyright 2004 Sun Microsystems, Inc. 10 * 11 * GRUB is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation; either version 2 of the License, or 14 * (at your option) any later version. 15 * 16 * GRUB is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License 22 * along with GRUB. If not, see <http://www.gnu.org/licenses/>. 23 * 24 */ 25 26 #include <common.h> 27 #include <malloc.h> 28 #include <linux/stat.h> 29 #include <linux/time.h> 30 #include <linux/ctype.h> 31 #include <asm/byteorder.h> 32 #include "zfs_common.h" 33 34 block_dev_desc_t *zfs_dev_desc; 35 36 /* 37 * The zfs plug-in routines for GRUB are: 38 * 39 * zfs_mount() - locates a valid uberblock of the root pool and reads 40 * in its MOS at the memory address MOS. 41 * 42 * zfs_open() - locates a plain file object by following the MOS 43 * and places its dnode at the memory address DNODE. 44 * 45 * zfs_read() - read in the data blocks pointed by the DNODE. 46 * 47 */ 48 49 #include <zfs/zfs.h> 50 #include <zfs/zio.h> 51 #include <zfs/dnode.h> 52 #include <zfs/uberblock_impl.h> 53 #include <zfs/vdev_impl.h> 54 #include <zfs/zio_checksum.h> 55 #include <zfs/zap_impl.h> 56 #include <zfs/zap_leaf.h> 57 #include <zfs/zfs_znode.h> 58 #include <zfs/dmu.h> 59 #include <zfs/dmu_objset.h> 60 #include <zfs/sa_impl.h> 61 #include <zfs/dsl_dir.h> 62 #include <zfs/dsl_dataset.h> 63 64 65 #define ZPOOL_PROP_BOOTFS "bootfs" 66 67 68 /* 69 * For nvlist manipulation. (from nvpair.h) 70 */ 71 #define NV_ENCODE_NATIVE 0 72 #define NV_ENCODE_XDR 1 73 #define NV_BIG_ENDIAN 0 74 #define NV_LITTLE_ENDIAN 1 75 #define DATA_TYPE_UINT64 8 76 #define DATA_TYPE_STRING 9 77 #define DATA_TYPE_NVLIST 19 78 #define DATA_TYPE_NVLIST_ARRAY 20 79 80 81 /* 82 * Macros to get fields in a bp or DVA. 83 */ 84 #define P2PHASE(x, align) ((x) & ((align) - 1)) 85 #define DVA_OFFSET_TO_PHYS_SECTOR(offset) \ 86 ((offset + VDEV_LABEL_START_SIZE) >> SPA_MINBLOCKSHIFT) 87 88 /* 89 * return x rounded down to an align boundary 90 * eg, P2ALIGN(1200, 1024) == 1024 (1*align) 91 * eg, P2ALIGN(1024, 1024) == 1024 (1*align) 92 * eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align) 93 * eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align) 94 */ 95 #define P2ALIGN(x, align) ((x) & -(align)) 96 97 /* 98 * FAT ZAP data structures 99 */ 100 #define ZFS_CRC64_POLY 0xC96C5795D7870F42ULL /* ECMA-182, reflected form */ 101 #define ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n)))) 102 #define CHAIN_END 0xffff /* end of the chunk chain */ 103 104 /* 105 * The amount of space within the chunk available for the array is: 106 * chunk size - space for type (1) - space for next pointer (2) 107 */ 108 #define ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3) 109 110 #define ZAP_LEAF_HASH_SHIFT(bs) (bs - 5) 111 #define ZAP_LEAF_HASH_NUMENTRIES(bs) (1 << ZAP_LEAF_HASH_SHIFT(bs)) 112 #define LEAF_HASH(bs, h) \ 113 ((ZAP_LEAF_HASH_NUMENTRIES(bs)-1) & \ 114 ((h) >> (64 - ZAP_LEAF_HASH_SHIFT(bs)-l->l_hdr.lh_prefix_len))) 115 116 /* 117 * The amount of space available for chunks is: 118 * block size shift - hash entry size (2) * number of hash 119 * entries - header space (2*chunksize) 120 */ 121 #define ZAP_LEAF_NUMCHUNKS(bs) \ 122 (((1<<bs) - 2*ZAP_LEAF_HASH_NUMENTRIES(bs)) / \ 123 ZAP_LEAF_CHUNKSIZE - 2) 124 125 /* 126 * The chunks start immediately after the hash table. The end of the 127 * hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a 128 * chunk_t. 129 */ 130 #define ZAP_LEAF_CHUNK(l, bs, idx) \ 131 ((zap_leaf_chunk_t *)(l->l_hash + ZAP_LEAF_HASH_NUMENTRIES(bs)))[idx] 132 #define ZAP_LEAF_ENTRY(l, bs, idx) (&ZAP_LEAF_CHUNK(l, bs, idx).l_entry) 133 134 135 /* 136 * Decompression Entry - lzjb 137 */ 138 #ifndef NBBY 139 #define NBBY 8 140 #endif 141 142 143 144 typedef int zfs_decomp_func_t(void *s_start, void *d_start, 145 uint32_t s_len, uint32_t d_len); 146 typedef struct decomp_entry { 147 char *name; 148 zfs_decomp_func_t *decomp_func; 149 } decomp_entry_t; 150 151 typedef struct dnode_end { 152 dnode_phys_t dn; 153 zfs_endian_t endian; 154 } dnode_end_t; 155 156 struct zfs_data { 157 /* cache for a file block of the currently zfs_open()-ed file */ 158 char *file_buf; 159 uint64_t file_start; 160 uint64_t file_end; 161 162 /* XXX: ashift is per vdev, not per pool. We currently only ever touch 163 * a single vdev, but when/if raid-z or stripes are supported, this 164 * may need revision. 165 */ 166 uint64_t vdev_ashift; 167 uint64_t label_txg; 168 uint64_t pool_guid; 169 170 /* cache for a dnode block */ 171 dnode_phys_t *dnode_buf; 172 dnode_phys_t *dnode_mdn; 173 uint64_t dnode_start; 174 uint64_t dnode_end; 175 zfs_endian_t dnode_endian; 176 177 uberblock_t current_uberblock; 178 179 dnode_end_t mos; 180 dnode_end_t mdn; 181 dnode_end_t dnode; 182 183 uint64_t vdev_phys_sector; 184 185 int (*userhook)(const char *, const struct zfs_dirhook_info *); 186 struct zfs_dirhook_info *dirinfo; 187 188 }; 189 190 191 192 193 static int 194 zlib_decompress(void *s, void *d, 195 uint32_t slen, uint32_t dlen) 196 { 197 if (zlib_decompress(s, d, slen, dlen) < 0) 198 return ZFS_ERR_BAD_FS; 199 return ZFS_ERR_NONE; 200 } 201 202 static decomp_entry_t decomp_table[ZIO_COMPRESS_FUNCTIONS] = { 203 {"inherit", NULL}, /* ZIO_COMPRESS_INHERIT */ 204 {"on", lzjb_decompress}, /* ZIO_COMPRESS_ON */ 205 {"off", NULL}, /* ZIO_COMPRESS_OFF */ 206 {"lzjb", lzjb_decompress}, /* ZIO_COMPRESS_LZJB */ 207 {"empty", NULL}, /* ZIO_COMPRESS_EMPTY */ 208 {"gzip-1", zlib_decompress}, /* ZIO_COMPRESS_GZIP1 */ 209 {"gzip-2", zlib_decompress}, /* ZIO_COMPRESS_GZIP2 */ 210 {"gzip-3", zlib_decompress}, /* ZIO_COMPRESS_GZIP3 */ 211 {"gzip-4", zlib_decompress}, /* ZIO_COMPRESS_GZIP4 */ 212 {"gzip-5", zlib_decompress}, /* ZIO_COMPRESS_GZIP5 */ 213 {"gzip-6", zlib_decompress}, /* ZIO_COMPRESS_GZIP6 */ 214 {"gzip-7", zlib_decompress}, /* ZIO_COMPRESS_GZIP7 */ 215 {"gzip-8", zlib_decompress}, /* ZIO_COMPRESS_GZIP8 */ 216 {"gzip-9", zlib_decompress}, /* ZIO_COMPRESS_GZIP9 */ 217 }; 218 219 220 221 static int zio_read_data(blkptr_t *bp, zfs_endian_t endian, 222 void *buf, struct zfs_data *data); 223 224 static int 225 zio_read(blkptr_t *bp, zfs_endian_t endian, void **buf, 226 size_t *size, struct zfs_data *data); 227 228 /* 229 * Our own version of log2(). Same thing as highbit()-1. 230 */ 231 static int 232 zfs_log2(uint64_t num) 233 { 234 int i = 0; 235 236 while (num > 1) { 237 i++; 238 num = num >> 1; 239 } 240 241 return i; 242 } 243 244 245 /* Checksum Functions */ 246 static void 247 zio_checksum_off(const void *buf __attribute__ ((unused)), 248 uint64_t size __attribute__ ((unused)), 249 zfs_endian_t endian __attribute__ ((unused)), 250 zio_cksum_t *zcp) 251 { 252 ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0); 253 } 254 255 /* Checksum Table and Values */ 256 static zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = { 257 {NULL, 0, 0, "inherit"}, 258 {NULL, 0, 0, "on"}, 259 {zio_checksum_off, 0, 0, "off"}, 260 {zio_checksum_SHA256, 1, 1, "label"}, 261 {zio_checksum_SHA256, 1, 1, "gang_header"}, 262 {NULL, 0, 0, "zilog"}, 263 {fletcher_2_endian, 0, 0, "fletcher2"}, 264 {fletcher_4_endian, 1, 0, "fletcher4"}, 265 {zio_checksum_SHA256, 1, 0, "SHA256"}, 266 {NULL, 0, 0, "zilog2"}, 267 }; 268 269 /* 270 * zio_checksum_verify: Provides support for checksum verification. 271 * 272 * Fletcher2, Fletcher4, and SHA256 are supported. 273 * 274 */ 275 static int 276 zio_checksum_verify(zio_cksum_t zc, uint32_t checksum, 277 zfs_endian_t endian, char *buf, int size) 278 { 279 zio_eck_t *zec = (zio_eck_t *) (buf + size) - 1; 280 zio_checksum_info_t *ci = &zio_checksum_table[checksum]; 281 zio_cksum_t actual_cksum, expected_cksum; 282 283 if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func == NULL) { 284 printf("zfs unknown checksum function %d\n", checksum); 285 return ZFS_ERR_NOT_IMPLEMENTED_YET; 286 } 287 288 if (ci->ci_eck) { 289 expected_cksum = zec->zec_cksum; 290 zec->zec_cksum = zc; 291 ci->ci_func(buf, size, endian, &actual_cksum); 292 zec->zec_cksum = expected_cksum; 293 zc = expected_cksum; 294 } else { 295 ci->ci_func(buf, size, endian, &actual_cksum); 296 } 297 298 if ((actual_cksum.zc_word[0] != zc.zc_word[0]) 299 || (actual_cksum.zc_word[1] != zc.zc_word[1]) 300 || (actual_cksum.zc_word[2] != zc.zc_word[2]) 301 || (actual_cksum.zc_word[3] != zc.zc_word[3])) { 302 return ZFS_ERR_BAD_FS; 303 } 304 305 return ZFS_ERR_NONE; 306 } 307 308 /* 309 * vdev_uberblock_compare takes two uberblock structures and returns an integer 310 * indicating the more recent of the two. 311 * Return Value = 1 if ub2 is more recent 312 * Return Value = -1 if ub1 is more recent 313 * The most recent uberblock is determined using its transaction number and 314 * timestamp. The uberblock with the highest transaction number is 315 * considered "newer". If the transaction numbers of the two blocks match, the 316 * timestamps are compared to determine the "newer" of the two. 317 */ 318 static int 319 vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2) 320 { 321 zfs_endian_t ub1_endian, ub2_endian; 322 if (zfs_to_cpu64(ub1->ub_magic, LITTLE_ENDIAN) == UBERBLOCK_MAGIC) 323 ub1_endian = LITTLE_ENDIAN; 324 else 325 ub1_endian = BIG_ENDIAN; 326 if (zfs_to_cpu64(ub2->ub_magic, LITTLE_ENDIAN) == UBERBLOCK_MAGIC) 327 ub2_endian = LITTLE_ENDIAN; 328 else 329 ub2_endian = BIG_ENDIAN; 330 331 if (zfs_to_cpu64(ub1->ub_txg, ub1_endian) 332 < zfs_to_cpu64(ub2->ub_txg, ub2_endian)) 333 return -1; 334 if (zfs_to_cpu64(ub1->ub_txg, ub1_endian) 335 > zfs_to_cpu64(ub2->ub_txg, ub2_endian)) 336 return 1; 337 338 if (zfs_to_cpu64(ub1->ub_timestamp, ub1_endian) 339 < zfs_to_cpu64(ub2->ub_timestamp, ub2_endian)) 340 return -1; 341 if (zfs_to_cpu64(ub1->ub_timestamp, ub1_endian) 342 > zfs_to_cpu64(ub2->ub_timestamp, ub2_endian)) 343 return 1; 344 345 return 0; 346 } 347 348 /* 349 * Three pieces of information are needed to verify an uberblock: the magic 350 * number, the version number, and the checksum. 351 * 352 * Currently Implemented: version number, magic number, label txg 353 * Need to Implement: checksum 354 * 355 */ 356 static int 357 uberblock_verify(uberblock_t *uber, int offset, struct zfs_data *data) 358 { 359 int err; 360 zfs_endian_t endian = UNKNOWN_ENDIAN; 361 zio_cksum_t zc; 362 363 if (uber->ub_txg < data->label_txg) { 364 debug("ignoring partially written label: uber_txg < label_txg %llu %llu\n", 365 uber->ub_txg, data->label_txg); 366 return ZFS_ERR_BAD_FS; 367 } 368 369 if (zfs_to_cpu64(uber->ub_magic, LITTLE_ENDIAN) == UBERBLOCK_MAGIC 370 && zfs_to_cpu64(uber->ub_version, LITTLE_ENDIAN) > 0 371 && zfs_to_cpu64(uber->ub_version, LITTLE_ENDIAN) <= SPA_VERSION) 372 endian = LITTLE_ENDIAN; 373 374 if (zfs_to_cpu64(uber->ub_magic, BIG_ENDIAN) == UBERBLOCK_MAGIC 375 && zfs_to_cpu64(uber->ub_version, BIG_ENDIAN) > 0 376 && zfs_to_cpu64(uber->ub_version, BIG_ENDIAN) <= SPA_VERSION) 377 endian = BIG_ENDIAN; 378 379 if (endian == UNKNOWN_ENDIAN) { 380 printf("invalid uberblock magic\n"); 381 return ZFS_ERR_BAD_FS; 382 } 383 384 memset(&zc, 0, sizeof(zc)); 385 zc.zc_word[0] = cpu_to_zfs64(offset, endian); 386 err = zio_checksum_verify(zc, ZIO_CHECKSUM_LABEL, endian, 387 (char *) uber, UBERBLOCK_SIZE(data->vdev_ashift)); 388 389 if (!err) { 390 /* Check that the data pointed by the rootbp is usable. */ 391 void *osp = NULL; 392 size_t ospsize; 393 err = zio_read(&uber->ub_rootbp, endian, &osp, &ospsize, data); 394 free(osp); 395 396 if (!err && ospsize < OBJSET_PHYS_SIZE_V14) { 397 printf("uberblock rootbp points to invalid data\n"); 398 return ZFS_ERR_BAD_FS; 399 } 400 } 401 402 return err; 403 } 404 405 /* 406 * Find the best uberblock. 407 * Return: 408 * Success - Pointer to the best uberblock. 409 * Failure - NULL 410 */ 411 static uberblock_t *find_bestub(char *ub_array, struct zfs_data *data) 412 { 413 const uint64_t sector = data->vdev_phys_sector; 414 uberblock_t *ubbest = NULL; 415 uberblock_t *ubnext; 416 unsigned int i, offset, pickedub = 0; 417 int err = ZFS_ERR_NONE; 418 419 const unsigned int UBCOUNT = UBERBLOCK_COUNT(data->vdev_ashift); 420 const uint64_t UBBYTES = UBERBLOCK_SIZE(data->vdev_ashift); 421 422 for (i = 0; i < UBCOUNT; i++) { 423 ubnext = (uberblock_t *) (i * UBBYTES + ub_array); 424 offset = (sector << SPA_MINBLOCKSHIFT) + VDEV_PHYS_SIZE + (i * UBBYTES); 425 426 err = uberblock_verify(ubnext, offset, data); 427 if (err) 428 continue; 429 430 if (ubbest == NULL || vdev_uberblock_compare(ubnext, ubbest) > 0) { 431 ubbest = ubnext; 432 pickedub = i; 433 } 434 } 435 436 if (ubbest) 437 debug("zfs Found best uberblock at idx %d, txg %llu\n", 438 pickedub, (unsigned long long) ubbest->ub_txg); 439 440 return ubbest; 441 } 442 443 static inline size_t 444 get_psize(blkptr_t *bp, zfs_endian_t endian) 445 { 446 return (((zfs_to_cpu64((bp)->blk_prop, endian) >> 16) & 0xffff) + 1) 447 << SPA_MINBLOCKSHIFT; 448 } 449 450 static uint64_t 451 dva_get_offset(dva_t *dva, zfs_endian_t endian) 452 { 453 return zfs_to_cpu64((dva)->dva_word[1], 454 endian) << SPA_MINBLOCKSHIFT; 455 } 456 457 /* 458 * Read a block of data based on the gang block address dva, 459 * and put its data in buf. 460 * 461 */ 462 static int 463 zio_read_gang(blkptr_t *bp, zfs_endian_t endian, dva_t *dva, void *buf, 464 struct zfs_data *data) 465 { 466 zio_gbh_phys_t *zio_gb; 467 uint64_t offset, sector; 468 unsigned i; 469 int err; 470 zio_cksum_t zc; 471 472 memset(&zc, 0, sizeof(zc)); 473 474 zio_gb = malloc(SPA_GANGBLOCKSIZE); 475 if (!zio_gb) 476 return ZFS_ERR_OUT_OF_MEMORY; 477 478 offset = dva_get_offset(dva, endian); 479 sector = DVA_OFFSET_TO_PHYS_SECTOR(offset); 480 481 /* read in the gang block header */ 482 err = zfs_devread(sector, 0, SPA_GANGBLOCKSIZE, (char *) zio_gb); 483 484 if (err) { 485 free(zio_gb); 486 return err; 487 } 488 489 /* XXX */ 490 /* self checksuming the gang block header */ 491 ZIO_SET_CHECKSUM(&zc, DVA_GET_VDEV(dva), 492 dva_get_offset(dva, endian), bp->blk_birth, 0); 493 err = zio_checksum_verify(zc, ZIO_CHECKSUM_GANG_HEADER, endian, 494 (char *) zio_gb, SPA_GANGBLOCKSIZE); 495 if (err) { 496 free(zio_gb); 497 return err; 498 } 499 500 endian = (zfs_to_cpu64(bp->blk_prop, endian) >> 63) & 1; 501 502 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) { 503 if (zio_gb->zg_blkptr[i].blk_birth == 0) 504 continue; 505 506 err = zio_read_data(&zio_gb->zg_blkptr[i], endian, buf, data); 507 if (err) { 508 free(zio_gb); 509 return err; 510 } 511 buf = (char *) buf + get_psize(&zio_gb->zg_blkptr[i], endian); 512 } 513 free(zio_gb); 514 return ZFS_ERR_NONE; 515 } 516 517 /* 518 * Read in a block of raw data to buf. 519 */ 520 static int 521 zio_read_data(blkptr_t *bp, zfs_endian_t endian, void *buf, 522 struct zfs_data *data) 523 { 524 int i, psize; 525 int err = ZFS_ERR_NONE; 526 527 psize = get_psize(bp, endian); 528 529 /* pick a good dva from the block pointer */ 530 for (i = 0; i < SPA_DVAS_PER_BP; i++) { 531 uint64_t offset, sector; 532 533 if (bp->blk_dva[i].dva_word[0] == 0 && bp->blk_dva[i].dva_word[1] == 0) 534 continue; 535 536 if ((zfs_to_cpu64(bp->blk_dva[i].dva_word[1], endian)>>63) & 1) { 537 err = zio_read_gang(bp, endian, &bp->blk_dva[i], buf, data); 538 } else { 539 /* read in a data block */ 540 offset = dva_get_offset(&bp->blk_dva[i], endian); 541 sector = DVA_OFFSET_TO_PHYS_SECTOR(offset); 542 543 err = zfs_devread(sector, 0, psize, buf); 544 } 545 546 if (!err) { 547 /*Check the underlying checksum before we rule this DVA as "good"*/ 548 uint32_t checkalgo = (zfs_to_cpu64((bp)->blk_prop, endian) >> 40) & 0xff; 549 550 err = zio_checksum_verify(bp->blk_cksum, checkalgo, endian, buf, psize); 551 if (!err) 552 return ZFS_ERR_NONE; 553 } 554 555 /* If read failed or checksum bad, reset the error. Hopefully we've got some more DVA's to try.*/ 556 } 557 558 if (!err) { 559 printf("couldn't find a valid DVA\n"); 560 err = ZFS_ERR_BAD_FS; 561 } 562 563 return err; 564 } 565 566 /* 567 * Read in a block of data, verify its checksum, decompress if needed, 568 * and put the uncompressed data in buf. 569 */ 570 static int 571 zio_read(blkptr_t *bp, zfs_endian_t endian, void **buf, 572 size_t *size, struct zfs_data *data) 573 { 574 size_t lsize, psize; 575 unsigned int comp; 576 char *compbuf = NULL; 577 int err; 578 579 *buf = NULL; 580 581 comp = (zfs_to_cpu64((bp)->blk_prop, endian)>>32) & 0xff; 582 lsize = (BP_IS_HOLE(bp) ? 0 : 583 (((zfs_to_cpu64((bp)->blk_prop, endian) & 0xffff) + 1) 584 << SPA_MINBLOCKSHIFT)); 585 psize = get_psize(bp, endian); 586 587 if (size) 588 *size = lsize; 589 590 if (comp >= ZIO_COMPRESS_FUNCTIONS) { 591 printf("compression algorithm %u not supported\n", (unsigned int) comp); 592 return ZFS_ERR_NOT_IMPLEMENTED_YET; 593 } 594 595 if (comp != ZIO_COMPRESS_OFF && decomp_table[comp].decomp_func == NULL) { 596 printf("compression algorithm %s not supported\n", decomp_table[comp].name); 597 return ZFS_ERR_NOT_IMPLEMENTED_YET; 598 } 599 600 if (comp != ZIO_COMPRESS_OFF) { 601 compbuf = malloc(psize); 602 if (!compbuf) 603 return ZFS_ERR_OUT_OF_MEMORY; 604 } else { 605 compbuf = *buf = malloc(lsize); 606 } 607 608 err = zio_read_data(bp, endian, compbuf, data); 609 if (err) { 610 free(compbuf); 611 *buf = NULL; 612 return err; 613 } 614 615 if (comp != ZIO_COMPRESS_OFF) { 616 *buf = malloc(lsize); 617 if (!*buf) { 618 free(compbuf); 619 return ZFS_ERR_OUT_OF_MEMORY; 620 } 621 622 err = decomp_table[comp].decomp_func(compbuf, *buf, psize, lsize); 623 free(compbuf); 624 if (err) { 625 free(*buf); 626 *buf = NULL; 627 return err; 628 } 629 } 630 631 return ZFS_ERR_NONE; 632 } 633 634 /* 635 * Get the block from a block id. 636 * push the block onto the stack. 637 * 638 */ 639 static int 640 dmu_read(dnode_end_t *dn, uint64_t blkid, void **buf, 641 zfs_endian_t *endian_out, struct zfs_data *data) 642 { 643 int idx, level; 644 blkptr_t *bp_array = dn->dn.dn_blkptr; 645 int epbs = dn->dn.dn_indblkshift - SPA_BLKPTRSHIFT; 646 blkptr_t *bp; 647 void *tmpbuf = 0; 648 zfs_endian_t endian; 649 int err = ZFS_ERR_NONE; 650 651 bp = malloc(sizeof(blkptr_t)); 652 if (!bp) 653 return ZFS_ERR_OUT_OF_MEMORY; 654 655 endian = dn->endian; 656 for (level = dn->dn.dn_nlevels - 1; level >= 0; level--) { 657 idx = (blkid >> (epbs * level)) & ((1 << epbs) - 1); 658 *bp = bp_array[idx]; 659 if (bp_array != dn->dn.dn_blkptr) { 660 free(bp_array); 661 bp_array = 0; 662 } 663 664 if (BP_IS_HOLE(bp)) { 665 size_t size = zfs_to_cpu16(dn->dn.dn_datablkszsec, 666 dn->endian) 667 << SPA_MINBLOCKSHIFT; 668 *buf = malloc(size); 669 if (*buf) { 670 err = ZFS_ERR_OUT_OF_MEMORY; 671 break; 672 } 673 memset(*buf, 0, size); 674 endian = (zfs_to_cpu64(bp->blk_prop, endian) >> 63) & 1; 675 break; 676 } 677 if (level == 0) { 678 err = zio_read(bp, endian, buf, 0, data); 679 endian = (zfs_to_cpu64(bp->blk_prop, endian) >> 63) & 1; 680 break; 681 } 682 err = zio_read(bp, endian, &tmpbuf, 0, data); 683 endian = (zfs_to_cpu64(bp->blk_prop, endian) >> 63) & 1; 684 if (err) 685 break; 686 bp_array = tmpbuf; 687 } 688 if (bp_array != dn->dn.dn_blkptr) 689 free(bp_array); 690 if (endian_out) 691 *endian_out = endian; 692 693 free(bp); 694 return err; 695 } 696 697 /* 698 * mzap_lookup: Looks up property described by "name" and returns the value 699 * in "value". 700 */ 701 static int 702 mzap_lookup(mzap_phys_t *zapobj, zfs_endian_t endian, 703 int objsize, char *name, uint64_t * value) 704 { 705 int i, chunks; 706 mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk; 707 708 chunks = objsize / MZAP_ENT_LEN - 1; 709 for (i = 0; i < chunks; i++) { 710 if (strcmp(mzap_ent[i].mze_name, name) == 0) { 711 *value = zfs_to_cpu64(mzap_ent[i].mze_value, endian); 712 return ZFS_ERR_NONE; 713 } 714 } 715 716 printf("couldn't find '%s'\n", name); 717 return ZFS_ERR_FILE_NOT_FOUND; 718 } 719 720 static int 721 mzap_iterate(mzap_phys_t *zapobj, zfs_endian_t endian, int objsize, 722 int (*hook)(const char *name, 723 uint64_t val, 724 struct zfs_data *data), 725 struct zfs_data *data) 726 { 727 int i, chunks; 728 mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk; 729 730 chunks = objsize / MZAP_ENT_LEN - 1; 731 for (i = 0; i < chunks; i++) { 732 if (hook(mzap_ent[i].mze_name, 733 zfs_to_cpu64(mzap_ent[i].mze_value, endian), 734 data)) 735 return 1; 736 } 737 738 return 0; 739 } 740 741 static uint64_t 742 zap_hash(uint64_t salt, const char *name) 743 { 744 static uint64_t table[256]; 745 const uint8_t *cp; 746 uint8_t c; 747 uint64_t crc = salt; 748 749 if (table[128] == 0) { 750 uint64_t *ct; 751 int i, j; 752 for (i = 0; i < 256; i++) { 753 for (ct = table + i, *ct = i, j = 8; j > 0; j--) 754 *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 755 } 756 } 757 758 for (cp = (const uint8_t *) name; (c = *cp) != '\0'; cp++) 759 crc = (crc >> 8) ^ table[(crc ^ c) & 0xFF]; 760 761 /* 762 * Only use 28 bits, since we need 4 bits in the cookie for the 763 * collision differentiator. We MUST use the high bits, since 764 * those are the onces that we first pay attention to when 765 * chosing the bucket. 766 */ 767 crc &= ~((1ULL << (64 - ZAP_HASHBITS)) - 1); 768 769 return crc; 770 } 771 772 /* 773 * Only to be used on 8-bit arrays. 774 * array_len is actual len in bytes (not encoded le_value_length). 775 * buf is null-terminated. 776 */ 777 /* XXX */ 778 static int 779 zap_leaf_array_equal(zap_leaf_phys_t *l, zfs_endian_t endian, 780 int blksft, int chunk, int array_len, const char *buf) 781 { 782 int bseen = 0; 783 784 while (bseen < array_len) { 785 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, blksft, chunk).l_array; 786 int toread = MIN(array_len - bseen, ZAP_LEAF_ARRAY_BYTES); 787 788 if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft)) 789 return 0; 790 791 if (memcmp(la->la_array, buf + bseen, toread) != 0) 792 break; 793 chunk = zfs_to_cpu16(la->la_next, endian); 794 bseen += toread; 795 } 796 return (bseen == array_len); 797 } 798 799 /* XXX */ 800 static int 801 zap_leaf_array_get(zap_leaf_phys_t *l, zfs_endian_t endian, int blksft, 802 int chunk, int array_len, char *buf) 803 { 804 int bseen = 0; 805 806 while (bseen < array_len) { 807 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, blksft, chunk).l_array; 808 int toread = MIN(array_len - bseen, ZAP_LEAF_ARRAY_BYTES); 809 810 if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft)) 811 /* Don't use errno because this error is to be ignored. */ 812 return ZFS_ERR_BAD_FS; 813 814 memcpy(buf + bseen, la->la_array, toread); 815 chunk = zfs_to_cpu16(la->la_next, endian); 816 bseen += toread; 817 } 818 return ZFS_ERR_NONE; 819 } 820 821 822 /* 823 * Given a zap_leaf_phys_t, walk thru the zap leaf chunks to get the 824 * value for the property "name". 825 * 826 */ 827 /* XXX */ 828 static int 829 zap_leaf_lookup(zap_leaf_phys_t *l, zfs_endian_t endian, 830 int blksft, uint64_t h, 831 const char *name, uint64_t *value) 832 { 833 uint16_t chunk; 834 struct zap_leaf_entry *le; 835 836 /* Verify if this is a valid leaf block */ 837 if (zfs_to_cpu64(l->l_hdr.lh_block_type, endian) != ZBT_LEAF) { 838 printf("invalid leaf type\n"); 839 return ZFS_ERR_BAD_FS; 840 } 841 if (zfs_to_cpu32(l->l_hdr.lh_magic, endian) != ZAP_LEAF_MAGIC) { 842 printf("invalid leaf magic\n"); 843 return ZFS_ERR_BAD_FS; 844 } 845 846 for (chunk = zfs_to_cpu16(l->l_hash[LEAF_HASH(blksft, h)], endian); 847 chunk != CHAIN_END; chunk = le->le_next) { 848 849 if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft)) { 850 printf("invalid chunk number\n"); 851 return ZFS_ERR_BAD_FS; 852 } 853 854 le = ZAP_LEAF_ENTRY(l, blksft, chunk); 855 856 /* Verify the chunk entry */ 857 if (le->le_type != ZAP_CHUNK_ENTRY) { 858 printf("invalid chunk entry\n"); 859 return ZFS_ERR_BAD_FS; 860 } 861 862 if (zfs_to_cpu64(le->le_hash, endian) != h) 863 continue; 864 865 if (zap_leaf_array_equal(l, endian, blksft, 866 zfs_to_cpu16(le->le_name_chunk, endian), 867 zfs_to_cpu16(le->le_name_length, endian), 868 name)) { 869 struct zap_leaf_array *la; 870 871 if (le->le_int_size != 8 || le->le_value_length != 1) { 872 printf("invalid leaf chunk entry\n"); 873 return ZFS_ERR_BAD_FS; 874 } 875 /* get the uint64_t property value */ 876 la = &ZAP_LEAF_CHUNK(l, blksft, le->le_value_chunk).l_array; 877 878 *value = be64_to_cpu(la->la_array64); 879 880 return ZFS_ERR_NONE; 881 } 882 } 883 884 printf("couldn't find '%s'\n", name); 885 return ZFS_ERR_FILE_NOT_FOUND; 886 } 887 888 889 /* Verify if this is a fat zap header block */ 890 static int 891 zap_verify(zap_phys_t *zap) 892 { 893 if (zap->zap_magic != (uint64_t) ZAP_MAGIC) { 894 printf("bad ZAP magic\n"); 895 return ZFS_ERR_BAD_FS; 896 } 897 898 if (zap->zap_flags != 0) { 899 printf("bad ZAP flags\n"); 900 return ZFS_ERR_BAD_FS; 901 } 902 903 if (zap->zap_salt == 0) { 904 printf("bad ZAP salt\n"); 905 return ZFS_ERR_BAD_FS; 906 } 907 908 return ZFS_ERR_NONE; 909 } 910 911 /* 912 * Fat ZAP lookup 913 * 914 */ 915 /* XXX */ 916 static int 917 fzap_lookup(dnode_end_t *zap_dnode, zap_phys_t *zap, 918 char *name, uint64_t *value, struct zfs_data *data) 919 { 920 void *l; 921 uint64_t hash, idx, blkid; 922 int blksft = zfs_log2(zfs_to_cpu16(zap_dnode->dn.dn_datablkszsec, 923 zap_dnode->endian) << DNODE_SHIFT); 924 int err; 925 zfs_endian_t leafendian; 926 927 err = zap_verify(zap); 928 if (err) 929 return err; 930 931 hash = zap_hash(zap->zap_salt, name); 932 933 /* get block id from index */ 934 if (zap->zap_ptrtbl.zt_numblks != 0) { 935 printf("external pointer tables not supported\n"); 936 return ZFS_ERR_NOT_IMPLEMENTED_YET; 937 } 938 idx = ZAP_HASH_IDX(hash, zap->zap_ptrtbl.zt_shift); 939 blkid = ((uint64_t *) zap)[idx + (1 << (blksft - 3 - 1))]; 940 941 /* Get the leaf block */ 942 if ((1U << blksft) < sizeof(zap_leaf_phys_t)) { 943 printf("ZAP leaf is too small\n"); 944 return ZFS_ERR_BAD_FS; 945 } 946 err = dmu_read(zap_dnode, blkid, &l, &leafendian, data); 947 if (err) 948 return err; 949 950 err = zap_leaf_lookup(l, leafendian, blksft, hash, name, value); 951 free(l); 952 return err; 953 } 954 955 /* XXX */ 956 static int 957 fzap_iterate(dnode_end_t *zap_dnode, zap_phys_t *zap, 958 int (*hook)(const char *name, 959 uint64_t val, 960 struct zfs_data *data), 961 struct zfs_data *data) 962 { 963 zap_leaf_phys_t *l; 964 void *l_in; 965 uint64_t idx, blkid; 966 uint16_t chunk; 967 int blksft = zfs_log2(zfs_to_cpu16(zap_dnode->dn.dn_datablkszsec, 968 zap_dnode->endian) << DNODE_SHIFT); 969 int err; 970 zfs_endian_t endian; 971 972 if (zap_verify(zap)) 973 return 0; 974 975 /* get block id from index */ 976 if (zap->zap_ptrtbl.zt_numblks != 0) { 977 printf("external pointer tables not supported\n"); 978 return 0; 979 } 980 /* Get the leaf block */ 981 if ((1U << blksft) < sizeof(zap_leaf_phys_t)) { 982 printf("ZAP leaf is too small\n"); 983 return 0; 984 } 985 for (idx = 0; idx < zap->zap_ptrtbl.zt_numblks; idx++) { 986 blkid = ((uint64_t *) zap)[idx + (1 << (blksft - 3 - 1))]; 987 988 err = dmu_read(zap_dnode, blkid, &l_in, &endian, data); 989 l = l_in; 990 if (err) 991 continue; 992 993 /* Verify if this is a valid leaf block */ 994 if (zfs_to_cpu64(l->l_hdr.lh_block_type, endian) != ZBT_LEAF) { 995 free(l); 996 continue; 997 } 998 if (zfs_to_cpu32(l->l_hdr.lh_magic, endian) != ZAP_LEAF_MAGIC) { 999 free(l); 1000 continue; 1001 } 1002 1003 for (chunk = 0; chunk < ZAP_LEAF_NUMCHUNKS(blksft); chunk++) { 1004 char *buf; 1005 struct zap_leaf_array *la; 1006 struct zap_leaf_entry *le; 1007 uint64_t val; 1008 le = ZAP_LEAF_ENTRY(l, blksft, chunk); 1009 1010 /* Verify the chunk entry */ 1011 if (le->le_type != ZAP_CHUNK_ENTRY) 1012 continue; 1013 1014 buf = malloc(zfs_to_cpu16(le->le_name_length, endian) 1015 + 1); 1016 if (zap_leaf_array_get(l, endian, blksft, le->le_name_chunk, 1017 le->le_name_length, buf)) { 1018 free(buf); 1019 continue; 1020 } 1021 buf[le->le_name_length] = 0; 1022 1023 if (le->le_int_size != 8 1024 || zfs_to_cpu16(le->le_value_length, endian) != 1) 1025 continue; 1026 1027 /* get the uint64_t property value */ 1028 la = &ZAP_LEAF_CHUNK(l, blksft, le->le_value_chunk).l_array; 1029 val = be64_to_cpu(la->la_array64); 1030 if (hook(buf, val, data)) 1031 return 1; 1032 free(buf); 1033 } 1034 } 1035 return 0; 1036 } 1037 1038 1039 /* 1040 * Read in the data of a zap object and find the value for a matching 1041 * property name. 1042 * 1043 */ 1044 static int 1045 zap_lookup(dnode_end_t *zap_dnode, char *name, uint64_t *val, 1046 struct zfs_data *data) 1047 { 1048 uint64_t block_type; 1049 int size; 1050 void *zapbuf; 1051 int err; 1052 zfs_endian_t endian; 1053 1054 /* Read in the first block of the zap object data. */ 1055 size = zfs_to_cpu16(zap_dnode->dn.dn_datablkszsec, 1056 zap_dnode->endian) << SPA_MINBLOCKSHIFT; 1057 err = dmu_read(zap_dnode, 0, &zapbuf, &endian, data); 1058 if (err) 1059 return err; 1060 block_type = zfs_to_cpu64(*((uint64_t *) zapbuf), endian); 1061 1062 if (block_type == ZBT_MICRO) { 1063 err = (mzap_lookup(zapbuf, endian, size, name, val)); 1064 free(zapbuf); 1065 return err; 1066 } else if (block_type == ZBT_HEADER) { 1067 /* this is a fat zap */ 1068 err = (fzap_lookup(zap_dnode, zapbuf, name, val, data)); 1069 free(zapbuf); 1070 return err; 1071 } 1072 1073 printf("unknown ZAP type\n"); 1074 return ZFS_ERR_BAD_FS; 1075 } 1076 1077 static int 1078 zap_iterate(dnode_end_t *zap_dnode, 1079 int (*hook)(const char *name, uint64_t val, 1080 struct zfs_data *data), 1081 struct zfs_data *data) 1082 { 1083 uint64_t block_type; 1084 int size; 1085 void *zapbuf; 1086 int err; 1087 int ret; 1088 zfs_endian_t endian; 1089 1090 /* Read in the first block of the zap object data. */ 1091 size = zfs_to_cpu16(zap_dnode->dn.dn_datablkszsec, zap_dnode->endian) << SPA_MINBLOCKSHIFT; 1092 err = dmu_read(zap_dnode, 0, &zapbuf, &endian, data); 1093 if (err) 1094 return 0; 1095 block_type = zfs_to_cpu64(*((uint64_t *) zapbuf), endian); 1096 1097 if (block_type == ZBT_MICRO) { 1098 ret = mzap_iterate(zapbuf, endian, size, hook, data); 1099 free(zapbuf); 1100 return ret; 1101 } else if (block_type == ZBT_HEADER) { 1102 /* this is a fat zap */ 1103 ret = fzap_iterate(zap_dnode, zapbuf, hook, data); 1104 free(zapbuf); 1105 return ret; 1106 } 1107 printf("unknown ZAP type\n"); 1108 return 0; 1109 } 1110 1111 1112 /* 1113 * Get the dnode of an object number from the metadnode of an object set. 1114 * 1115 * Input 1116 * mdn - metadnode to get the object dnode 1117 * objnum - object number for the object dnode 1118 * buf - data buffer that holds the returning dnode 1119 */ 1120 static int 1121 dnode_get(dnode_end_t *mdn, uint64_t objnum, uint8_t type, 1122 dnode_end_t *buf, struct zfs_data *data) 1123 { 1124 uint64_t blkid, blksz; /* the block id this object dnode is in */ 1125 int epbs; /* shift of number of dnodes in a block */ 1126 int idx; /* index within a block */ 1127 void *dnbuf; 1128 int err; 1129 zfs_endian_t endian; 1130 1131 blksz = zfs_to_cpu16(mdn->dn.dn_datablkszsec, 1132 mdn->endian) << SPA_MINBLOCKSHIFT; 1133 1134 epbs = zfs_log2(blksz) - DNODE_SHIFT; 1135 blkid = objnum >> epbs; 1136 idx = objnum & ((1 << epbs) - 1); 1137 1138 if (data->dnode_buf != NULL && memcmp(data->dnode_mdn, mdn, 1139 sizeof(*mdn)) == 0 1140 && objnum >= data->dnode_start && objnum < data->dnode_end) { 1141 memmove(&(buf->dn), &(data->dnode_buf)[idx], DNODE_SIZE); 1142 buf->endian = data->dnode_endian; 1143 if (type && buf->dn.dn_type != type) { 1144 printf("incorrect dnode type: %02X != %02x\n", buf->dn.dn_type, type); 1145 return ZFS_ERR_BAD_FS; 1146 } 1147 return ZFS_ERR_NONE; 1148 } 1149 1150 err = dmu_read(mdn, blkid, &dnbuf, &endian, data); 1151 if (err) 1152 return err; 1153 1154 free(data->dnode_buf); 1155 free(data->dnode_mdn); 1156 data->dnode_mdn = malloc(sizeof(*mdn)); 1157 if (!data->dnode_mdn) { 1158 data->dnode_buf = 0; 1159 } else { 1160 memcpy(data->dnode_mdn, mdn, sizeof(*mdn)); 1161 data->dnode_buf = dnbuf; 1162 data->dnode_start = blkid << epbs; 1163 data->dnode_end = (blkid + 1) << epbs; 1164 data->dnode_endian = endian; 1165 } 1166 1167 memmove(&(buf->dn), (dnode_phys_t *) dnbuf + idx, DNODE_SIZE); 1168 buf->endian = endian; 1169 if (type && buf->dn.dn_type != type) { 1170 printf("incorrect dnode type\n"); 1171 return ZFS_ERR_BAD_FS; 1172 } 1173 1174 return ZFS_ERR_NONE; 1175 } 1176 1177 /* 1178 * Get the file dnode for a given file name where mdn is the meta dnode 1179 * for this ZFS object set. When found, place the file dnode in dn. 1180 * The 'path' argument will be mangled. 1181 * 1182 */ 1183 static int 1184 dnode_get_path(dnode_end_t *mdn, const char *path_in, dnode_end_t *dn, 1185 struct zfs_data *data) 1186 { 1187 uint64_t objnum, version; 1188 char *cname, ch; 1189 int err = ZFS_ERR_NONE; 1190 char *path, *path_buf; 1191 struct dnode_chain { 1192 struct dnode_chain *next; 1193 dnode_end_t dn; 1194 }; 1195 struct dnode_chain *dnode_path = 0, *dn_new, *root; 1196 1197 dn_new = malloc(sizeof(*dn_new)); 1198 if (!dn_new) 1199 return ZFS_ERR_OUT_OF_MEMORY; 1200 dn_new->next = 0; 1201 dnode_path = root = dn_new; 1202 1203 err = dnode_get(mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE, 1204 &(dnode_path->dn), data); 1205 if (err) { 1206 free(dn_new); 1207 return err; 1208 } 1209 1210 err = zap_lookup(&(dnode_path->dn), ZPL_VERSION_STR, &version, data); 1211 if (err) { 1212 free(dn_new); 1213 return err; 1214 } 1215 if (version > ZPL_VERSION) { 1216 free(dn_new); 1217 printf("too new ZPL version\n"); 1218 return ZFS_ERR_NOT_IMPLEMENTED_YET; 1219 } 1220 1221 err = zap_lookup(&(dnode_path->dn), ZFS_ROOT_OBJ, &objnum, data); 1222 if (err) { 1223 free(dn_new); 1224 return err; 1225 } 1226 1227 err = dnode_get(mdn, objnum, 0, &(dnode_path->dn), data); 1228 if (err) { 1229 free(dn_new); 1230 return err; 1231 } 1232 1233 path = path_buf = strdup(path_in); 1234 if (!path_buf) { 1235 free(dn_new); 1236 return ZFS_ERR_OUT_OF_MEMORY; 1237 } 1238 1239 while (1) { 1240 /* skip leading slashes */ 1241 while (*path == '/') 1242 path++; 1243 if (!*path) 1244 break; 1245 /* get the next component name */ 1246 cname = path; 1247 while (*path && *path != '/') 1248 path++; 1249 /* Skip dot. */ 1250 if (cname + 1 == path && cname[0] == '.') 1251 continue; 1252 /* Handle double dot. */ 1253 if (cname + 2 == path && cname[0] == '.' && cname[1] == '.') { 1254 if (dn_new->next) { 1255 dn_new = dnode_path; 1256 dnode_path = dn_new->next; 1257 free(dn_new); 1258 } else { 1259 printf("can't resolve ..\n"); 1260 err = ZFS_ERR_FILE_NOT_FOUND; 1261 break; 1262 } 1263 continue; 1264 } 1265 1266 ch = *path; 1267 *path = 0; /* ensure null termination */ 1268 1269 if (dnode_path->dn.dn.dn_type != DMU_OT_DIRECTORY_CONTENTS) { 1270 free(path_buf); 1271 printf("not a directory\n"); 1272 return ZFS_ERR_BAD_FILE_TYPE; 1273 } 1274 err = zap_lookup(&(dnode_path->dn), cname, &objnum, data); 1275 if (err) 1276 break; 1277 1278 dn_new = malloc(sizeof(*dn_new)); 1279 if (!dn_new) { 1280 err = ZFS_ERR_OUT_OF_MEMORY; 1281 break; 1282 } 1283 dn_new->next = dnode_path; 1284 dnode_path = dn_new; 1285 1286 objnum = ZFS_DIRENT_OBJ(objnum); 1287 err = dnode_get(mdn, objnum, 0, &(dnode_path->dn), data); 1288 if (err) 1289 break; 1290 1291 *path = ch; 1292 } 1293 1294 if (!err) 1295 memcpy(dn, &(dnode_path->dn), sizeof(*dn)); 1296 1297 while (dnode_path) { 1298 dn_new = dnode_path->next; 1299 free(dnode_path); 1300 dnode_path = dn_new; 1301 } 1302 free(path_buf); 1303 return err; 1304 } 1305 1306 1307 /* 1308 * Given a MOS metadnode, get the metadnode of a given filesystem name (fsname), 1309 * e.g. pool/rootfs, or a given object number (obj), e.g. the object number 1310 * of pool/rootfs. 1311 * 1312 * If no fsname and no obj are given, return the DSL_DIR metadnode. 1313 * If fsname is given, return its metadnode and its matching object number. 1314 * If only obj is given, return the metadnode for this object number. 1315 * 1316 */ 1317 static int 1318 get_filesystem_dnode(dnode_end_t *mosmdn, char *fsname, 1319 dnode_end_t *mdn, struct zfs_data *data) 1320 { 1321 uint64_t objnum; 1322 int err; 1323 1324 err = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT, 1325 DMU_OT_OBJECT_DIRECTORY, mdn, data); 1326 if (err) 1327 return err; 1328 1329 err = zap_lookup(mdn, DMU_POOL_ROOT_DATASET, &objnum, data); 1330 if (err) 1331 return err; 1332 1333 err = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR, mdn, data); 1334 if (err) 1335 return err; 1336 1337 while (*fsname) { 1338 uint64_t childobj; 1339 char *cname, ch; 1340 1341 while (*fsname == '/') 1342 fsname++; 1343 1344 if (!*fsname || *fsname == '@') 1345 break; 1346 1347 cname = fsname; 1348 while (*fsname && !isspace(*fsname) && *fsname != '/') 1349 fsname++; 1350 ch = *fsname; 1351 *fsname = 0; 1352 1353 childobj = zfs_to_cpu64((((dsl_dir_phys_t *) DN_BONUS(&mdn->dn)))->dd_child_dir_zapobj, mdn->endian); 1354 err = dnode_get(mosmdn, childobj, 1355 DMU_OT_DSL_DIR_CHILD_MAP, mdn, data); 1356 if (err) 1357 return err; 1358 1359 err = zap_lookup(mdn, cname, &objnum, data); 1360 if (err) 1361 return err; 1362 1363 err = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR, mdn, data); 1364 if (err) 1365 return err; 1366 1367 *fsname = ch; 1368 } 1369 return ZFS_ERR_NONE; 1370 } 1371 1372 static int 1373 make_mdn(dnode_end_t *mdn, struct zfs_data *data) 1374 { 1375 void *osp; 1376 blkptr_t *bp; 1377 size_t ospsize; 1378 int err; 1379 1380 bp = &(((dsl_dataset_phys_t *) DN_BONUS(&mdn->dn))->ds_bp); 1381 err = zio_read(bp, mdn->endian, &osp, &ospsize, data); 1382 if (err) 1383 return err; 1384 if (ospsize < OBJSET_PHYS_SIZE_V14) { 1385 free(osp); 1386 printf("too small osp\n"); 1387 return ZFS_ERR_BAD_FS; 1388 } 1389 1390 mdn->endian = (zfs_to_cpu64(bp->blk_prop, mdn->endian)>>63) & 1; 1391 memmove((char *) &(mdn->dn), 1392 (char *) &((objset_phys_t *) osp)->os_meta_dnode, DNODE_SIZE); 1393 free(osp); 1394 return ZFS_ERR_NONE; 1395 } 1396 1397 static int 1398 dnode_get_fullpath(const char *fullpath, dnode_end_t *mdn, 1399 uint64_t *mdnobj, dnode_end_t *dn, int *isfs, 1400 struct zfs_data *data) 1401 { 1402 char *fsname, *snapname; 1403 const char *ptr_at, *filename; 1404 uint64_t headobj; 1405 int err; 1406 1407 ptr_at = strchr(fullpath, '@'); 1408 if (!ptr_at) { 1409 *isfs = 1; 1410 filename = 0; 1411 snapname = 0; 1412 fsname = strdup(fullpath); 1413 } else { 1414 const char *ptr_slash = strchr(ptr_at, '/'); 1415 1416 *isfs = 0; 1417 fsname = malloc(ptr_at - fullpath + 1); 1418 if (!fsname) 1419 return ZFS_ERR_OUT_OF_MEMORY; 1420 memcpy(fsname, fullpath, ptr_at - fullpath); 1421 fsname[ptr_at - fullpath] = 0; 1422 if (ptr_at[1] && ptr_at[1] != '/') { 1423 snapname = malloc(ptr_slash - ptr_at); 1424 if (!snapname) { 1425 free(fsname); 1426 return ZFS_ERR_OUT_OF_MEMORY; 1427 } 1428 memcpy(snapname, ptr_at + 1, ptr_slash - ptr_at - 1); 1429 snapname[ptr_slash - ptr_at - 1] = 0; 1430 } else { 1431 snapname = 0; 1432 } 1433 if (ptr_slash) 1434 filename = ptr_slash; 1435 else 1436 filename = "/"; 1437 printf("zfs fsname = '%s' snapname='%s' filename = '%s'\n", 1438 fsname, snapname, filename); 1439 } 1440 1441 1442 err = get_filesystem_dnode(&(data->mos), fsname, dn, data); 1443 1444 if (err) { 1445 free(fsname); 1446 free(snapname); 1447 return err; 1448 } 1449 1450 headobj = zfs_to_cpu64(((dsl_dir_phys_t *) DN_BONUS(&dn->dn))->dd_head_dataset_obj, dn->endian); 1451 1452 err = dnode_get(&(data->mos), headobj, DMU_OT_DSL_DATASET, mdn, data); 1453 if (err) { 1454 free(fsname); 1455 free(snapname); 1456 return err; 1457 } 1458 1459 if (snapname) { 1460 uint64_t snapobj; 1461 1462 snapobj = zfs_to_cpu64(((dsl_dataset_phys_t *) DN_BONUS(&mdn->dn))->ds_snapnames_zapobj, mdn->endian); 1463 1464 err = dnode_get(&(data->mos), snapobj, 1465 DMU_OT_DSL_DS_SNAP_MAP, mdn, data); 1466 if (!err) 1467 err = zap_lookup(mdn, snapname, &headobj, data); 1468 if (!err) 1469 err = dnode_get(&(data->mos), headobj, DMU_OT_DSL_DATASET, mdn, data); 1470 if (err) { 1471 free(fsname); 1472 free(snapname); 1473 return err; 1474 } 1475 } 1476 1477 if (mdnobj) 1478 *mdnobj = headobj; 1479 1480 make_mdn(mdn, data); 1481 1482 if (*isfs) { 1483 free(fsname); 1484 free(snapname); 1485 return ZFS_ERR_NONE; 1486 } 1487 err = dnode_get_path(mdn, filename, dn, data); 1488 free(fsname); 1489 free(snapname); 1490 return err; 1491 } 1492 1493 /* 1494 * For a given XDR packed nvlist, verify the first 4 bytes and move on. 1495 * 1496 * An XDR packed nvlist is encoded as (comments from nvs_xdr_create) : 1497 * 1498 * encoding method/host endian (4 bytes) 1499 * nvl_version (4 bytes) 1500 * nvl_nvflag (4 bytes) 1501 * encoded nvpairs: 1502 * encoded size of the nvpair (4 bytes) 1503 * decoded size of the nvpair (4 bytes) 1504 * name string size (4 bytes) 1505 * name string data (sizeof(NV_ALIGN4(string)) 1506 * data type (4 bytes) 1507 * # of elements in the nvpair (4 bytes) 1508 * data 1509 * 2 zero's for the last nvpair 1510 * (end of the entire list) (8 bytes) 1511 * 1512 */ 1513 1514 static int 1515 nvlist_find_value(char *nvlist, char *name, int valtype, char **val, 1516 size_t *size_out, size_t *nelm_out) 1517 { 1518 int name_len, type, encode_size; 1519 char *nvpair, *nvp_name; 1520 1521 /* Verify if the 1st and 2nd byte in the nvlist are valid. */ 1522 /* NOTE: independently of what endianness header announces all 1523 subsequent values are big-endian. */ 1524 if (nvlist[0] != NV_ENCODE_XDR || (nvlist[1] != NV_LITTLE_ENDIAN 1525 && nvlist[1] != NV_BIG_ENDIAN)) { 1526 printf("zfs incorrect nvlist header\n"); 1527 return ZFS_ERR_BAD_FS; 1528 } 1529 1530 /* skip the header, nvl_version, and nvl_nvflag */ 1531 nvlist = nvlist + 4 * 3; 1532 /* 1533 * Loop thru the nvpair list 1534 * The XDR representation of an integer is in big-endian byte order. 1535 */ 1536 while ((encode_size = be32_to_cpu(*(uint32_t *) nvlist))) { 1537 int nelm; 1538 1539 nvpair = nvlist + 4 * 2; /* skip the encode/decode size */ 1540 1541 name_len = be32_to_cpu(*(uint32_t *) nvpair); 1542 nvpair += 4; 1543 1544 nvp_name = nvpair; 1545 nvpair = nvpair + ((name_len + 3) & ~3); /* align */ 1546 1547 type = be32_to_cpu(*(uint32_t *) nvpair); 1548 nvpair += 4; 1549 1550 nelm = be32_to_cpu(*(uint32_t *) nvpair); 1551 if (nelm < 1) { 1552 printf("empty nvpair\n"); 1553 return ZFS_ERR_BAD_FS; 1554 } 1555 1556 nvpair += 4; 1557 1558 if ((strncmp(nvp_name, name, name_len) == 0) && type == valtype) { 1559 *val = nvpair; 1560 *size_out = encode_size; 1561 if (nelm_out) 1562 *nelm_out = nelm; 1563 return 1; 1564 } 1565 1566 nvlist += encode_size; /* goto the next nvpair */ 1567 } 1568 return 0; 1569 } 1570 1571 int 1572 zfs_nvlist_lookup_uint64(char *nvlist, char *name, uint64_t *out) 1573 { 1574 char *nvpair; 1575 size_t size; 1576 int found; 1577 1578 found = nvlist_find_value(nvlist, name, DATA_TYPE_UINT64, &nvpair, &size, 0); 1579 if (!found) 1580 return 0; 1581 if (size < sizeof(uint64_t)) { 1582 printf("invalid uint64\n"); 1583 return ZFS_ERR_BAD_FS; 1584 } 1585 1586 *out = be64_to_cpu(*(uint64_t *) nvpair); 1587 return 1; 1588 } 1589 1590 char * 1591 zfs_nvlist_lookup_string(char *nvlist, char *name) 1592 { 1593 char *nvpair; 1594 char *ret; 1595 size_t slen; 1596 size_t size; 1597 int found; 1598 1599 found = nvlist_find_value(nvlist, name, DATA_TYPE_STRING, &nvpair, &size, 0); 1600 if (!found) 1601 return 0; 1602 if (size < 4) { 1603 printf("invalid string\n"); 1604 return 0; 1605 } 1606 slen = be32_to_cpu(*(uint32_t *) nvpair); 1607 if (slen > size - 4) 1608 slen = size - 4; 1609 ret = malloc(slen + 1); 1610 if (!ret) 1611 return 0; 1612 memcpy(ret, nvpair + 4, slen); 1613 ret[slen] = 0; 1614 return ret; 1615 } 1616 1617 char * 1618 zfs_nvlist_lookup_nvlist(char *nvlist, char *name) 1619 { 1620 char *nvpair; 1621 char *ret; 1622 size_t size; 1623 int found; 1624 1625 found = nvlist_find_value(nvlist, name, DATA_TYPE_NVLIST, &nvpair, 1626 &size, 0); 1627 if (!found) 1628 return 0; 1629 ret = calloc(1, size + 3 * sizeof(uint32_t)); 1630 if (!ret) 1631 return 0; 1632 memcpy(ret, nvlist, sizeof(uint32_t)); 1633 1634 memcpy(ret + sizeof(uint32_t), nvpair, size); 1635 return ret; 1636 } 1637 1638 int 1639 zfs_nvlist_lookup_nvlist_array_get_nelm(char *nvlist, char *name) 1640 { 1641 char *nvpair; 1642 size_t nelm, size; 1643 int found; 1644 1645 found = nvlist_find_value(nvlist, name, DATA_TYPE_NVLIST, &nvpair, 1646 &size, &nelm); 1647 if (!found) 1648 return -1; 1649 return nelm; 1650 } 1651 1652 char * 1653 zfs_nvlist_lookup_nvlist_array(char *nvlist, char *name, 1654 size_t index) 1655 { 1656 char *nvpair, *nvpairptr; 1657 int found; 1658 char *ret; 1659 size_t size; 1660 unsigned i; 1661 size_t nelm; 1662 1663 found = nvlist_find_value(nvlist, name, DATA_TYPE_NVLIST, &nvpair, 1664 &size, &nelm); 1665 if (!found) 1666 return 0; 1667 if (index >= nelm) { 1668 printf("trying to lookup past nvlist array\n"); 1669 return 0; 1670 } 1671 1672 nvpairptr = nvpair; 1673 1674 for (i = 0; i < index; i++) { 1675 uint32_t encode_size; 1676 1677 /* skip the header, nvl_version, and nvl_nvflag */ 1678 nvpairptr = nvpairptr + 4 * 2; 1679 1680 while (nvpairptr < nvpair + size 1681 && (encode_size = be32_to_cpu(*(uint32_t *) nvpairptr))) 1682 nvlist += encode_size; /* goto the next nvpair */ 1683 1684 nvlist = nvlist + 4 * 2; /* skip the ending 2 zeros - 8 bytes */ 1685 } 1686 1687 if (nvpairptr >= nvpair + size 1688 || nvpairptr + be32_to_cpu(*(uint32_t *) (nvpairptr + 4 * 2)) 1689 >= nvpair + size) { 1690 printf("incorrect nvlist array\n"); 1691 return 0; 1692 } 1693 1694 ret = calloc(1, be32_to_cpu(*(uint32_t *) (nvpairptr + 4 * 2)) 1695 + 3 * sizeof(uint32_t)); 1696 if (!ret) 1697 return 0; 1698 memcpy(ret, nvlist, sizeof(uint32_t)); 1699 1700 memcpy(ret + sizeof(uint32_t), nvpairptr, size); 1701 return ret; 1702 } 1703 1704 static int 1705 int_zfs_fetch_nvlist(struct zfs_data *data, char **nvlist) 1706 { 1707 int err; 1708 1709 *nvlist = malloc(VDEV_PHYS_SIZE); 1710 /* Read in the vdev name-value pair list (112K). */ 1711 err = zfs_devread(data->vdev_phys_sector, 0, VDEV_PHYS_SIZE, *nvlist); 1712 if (err) { 1713 free(*nvlist); 1714 *nvlist = 0; 1715 return err; 1716 } 1717 return ZFS_ERR_NONE; 1718 } 1719 1720 /* 1721 * Check the disk label information and retrieve needed vdev name-value pairs. 1722 * 1723 */ 1724 static int 1725 check_pool_label(struct zfs_data *data) 1726 { 1727 uint64_t pool_state; 1728 char *nvlist; /* for the pool */ 1729 char *vdevnvlist; /* for the vdev */ 1730 uint64_t diskguid; 1731 uint64_t version; 1732 int found; 1733 int err; 1734 1735 err = int_zfs_fetch_nvlist(data, &nvlist); 1736 if (err) 1737 return err; 1738 1739 found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_POOL_STATE, 1740 &pool_state); 1741 if (!found) { 1742 free(nvlist); 1743 printf("zfs pool state not found\n"); 1744 return ZFS_ERR_BAD_FS; 1745 } 1746 1747 if (pool_state == POOL_STATE_DESTROYED) { 1748 free(nvlist); 1749 printf("zpool is marked as destroyed\n"); 1750 return ZFS_ERR_BAD_FS; 1751 } 1752 1753 data->label_txg = 0; 1754 found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_POOL_TXG, 1755 &data->label_txg); 1756 if (!found) { 1757 free(nvlist); 1758 printf("zfs pool txg not found\n"); 1759 return ZFS_ERR_BAD_FS; 1760 } 1761 1762 /* not an active device */ 1763 if (data->label_txg == 0) { 1764 free(nvlist); 1765 printf("zpool is not active\n"); 1766 return ZFS_ERR_BAD_FS; 1767 } 1768 1769 found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_VERSION, 1770 &version); 1771 if (!found) { 1772 free(nvlist); 1773 printf("zpool config version not found\n"); 1774 return ZFS_ERR_BAD_FS; 1775 } 1776 1777 if (version > SPA_VERSION) { 1778 free(nvlist); 1779 printf("SPA version too new %llu > %llu\n", 1780 (unsigned long long) version, 1781 (unsigned long long) SPA_VERSION); 1782 return ZFS_ERR_NOT_IMPLEMENTED_YET; 1783 } 1784 1785 vdevnvlist = zfs_nvlist_lookup_nvlist(nvlist, ZPOOL_CONFIG_VDEV_TREE); 1786 if (!vdevnvlist) { 1787 free(nvlist); 1788 printf("ZFS config vdev tree not found\n"); 1789 return ZFS_ERR_BAD_FS; 1790 } 1791 1792 found = zfs_nvlist_lookup_uint64(vdevnvlist, ZPOOL_CONFIG_ASHIFT, 1793 &data->vdev_ashift); 1794 free(vdevnvlist); 1795 if (!found) { 1796 free(nvlist); 1797 printf("ZPOOL config ashift not found\n"); 1798 return ZFS_ERR_BAD_FS; 1799 } 1800 1801 found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_GUID, &diskguid); 1802 if (!found) { 1803 free(nvlist); 1804 printf("ZPOOL config guid not found\n"); 1805 return ZFS_ERR_BAD_FS; 1806 } 1807 1808 found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_POOL_GUID, &data->pool_guid); 1809 if (!found) { 1810 free(nvlist); 1811 printf("ZPOOL config pool guid not found\n"); 1812 return ZFS_ERR_BAD_FS; 1813 } 1814 1815 free(nvlist); 1816 1817 printf("ZFS Pool GUID: %llu (%016llx) Label: GUID: %llu (%016llx), txg: %llu, SPA v%llu, ashift: %llu\n", 1818 (unsigned long long) data->pool_guid, 1819 (unsigned long long) data->pool_guid, 1820 (unsigned long long) diskguid, 1821 (unsigned long long) diskguid, 1822 (unsigned long long) data->label_txg, 1823 (unsigned long long) version, 1824 (unsigned long long) data->vdev_ashift); 1825 1826 return ZFS_ERR_NONE; 1827 } 1828 1829 /* 1830 * vdev_label_start returns the physical disk offset (in bytes) of 1831 * label "l". 1832 */ 1833 static uint64_t vdev_label_start(uint64_t psize, int l) 1834 { 1835 return (l * sizeof(vdev_label_t) + (l < VDEV_LABELS / 2 ? 1836 0 : psize - 1837 VDEV_LABELS * sizeof(vdev_label_t))); 1838 } 1839 1840 void 1841 zfs_unmount(struct zfs_data *data) 1842 { 1843 free(data->dnode_buf); 1844 free(data->dnode_mdn); 1845 free(data->file_buf); 1846 free(data); 1847 } 1848 1849 /* 1850 * zfs_mount() locates a valid uberblock of the root pool and read in its MOS 1851 * to the memory address MOS. 1852 * 1853 */ 1854 struct zfs_data * 1855 zfs_mount(device_t dev) 1856 { 1857 struct zfs_data *data = 0; 1858 int label = 0, bestlabel = -1; 1859 char *ub_array; 1860 uberblock_t *ubbest; 1861 uberblock_t *ubcur = NULL; 1862 void *osp = 0; 1863 size_t ospsize; 1864 int err; 1865 1866 data = malloc(sizeof(*data)); 1867 if (!data) 1868 return 0; 1869 memset(data, 0, sizeof(*data)); 1870 1871 ub_array = malloc(VDEV_UBERBLOCK_RING); 1872 if (!ub_array) { 1873 zfs_unmount(data); 1874 return 0; 1875 } 1876 1877 ubbest = malloc(sizeof(*ubbest)); 1878 if (!ubbest) { 1879 zfs_unmount(data); 1880 return 0; 1881 } 1882 memset(ubbest, 0, sizeof(*ubbest)); 1883 1884 /* 1885 * some eltorito stacks don't give us a size and 1886 * we end up setting the size to MAXUINT, further 1887 * some of these devices stop working once a single 1888 * read past the end has been issued. Checking 1889 * for a maximum part_length and skipping the backup 1890 * labels at the end of the slice/partition/device 1891 * avoids breaking down on such devices. 1892 */ 1893 const int vdevnum = 1894 dev->part_length == 0 ? 1895 VDEV_LABELS / 2 : VDEV_LABELS; 1896 1897 /* Size in bytes of the device (disk or partition) aligned to label size*/ 1898 uint64_t device_size = 1899 dev->part_length << SECTOR_BITS; 1900 1901 const uint64_t alignedbytes = 1902 P2ALIGN(device_size, (uint64_t) sizeof(vdev_label_t)); 1903 1904 for (label = 0; label < vdevnum; label++) { 1905 uint64_t labelstartbytes = vdev_label_start(alignedbytes, label); 1906 uint64_t labelstart = labelstartbytes >> SECTOR_BITS; 1907 1908 debug("zfs reading label %d at sector %llu (byte %llu)\n", 1909 label, (unsigned long long) labelstart, 1910 (unsigned long long) labelstartbytes); 1911 1912 data->vdev_phys_sector = labelstart + 1913 ((VDEV_SKIP_SIZE + VDEV_BOOT_HEADER_SIZE) >> SECTOR_BITS); 1914 1915 err = check_pool_label(data); 1916 if (err) { 1917 printf("zfs error checking label %d\n", label); 1918 continue; 1919 } 1920 1921 /* Read in the uberblock ring (128K). */ 1922 err = zfs_devread(data->vdev_phys_sector + 1923 (VDEV_PHYS_SIZE >> SECTOR_BITS), 1924 0, VDEV_UBERBLOCK_RING, ub_array); 1925 if (err) { 1926 printf("zfs error reading uberblock ring for label %d\n", label); 1927 continue; 1928 } 1929 1930 ubcur = find_bestub(ub_array, data); 1931 if (!ubcur) { 1932 printf("zfs No good uberblocks found in label %d\n", label); 1933 continue; 1934 } 1935 1936 if (vdev_uberblock_compare(ubcur, ubbest) > 0) { 1937 /* Looks like the block is good, so use it.*/ 1938 memcpy(ubbest, ubcur, sizeof(*ubbest)); 1939 bestlabel = label; 1940 debug("zfs Current best uberblock found in label %d\n", label); 1941 } 1942 } 1943 free(ub_array); 1944 1945 /* We zero'd the structure to begin with. If we never assigned to it, 1946 magic will still be zero. */ 1947 if (!ubbest->ub_magic) { 1948 printf("couldn't find a valid ZFS label\n"); 1949 zfs_unmount(data); 1950 free(ubbest); 1951 return 0; 1952 } 1953 1954 debug("zfs ubbest %p in label %d\n", ubbest, bestlabel); 1955 1956 zfs_endian_t ub_endian = 1957 zfs_to_cpu64(ubbest->ub_magic, LITTLE_ENDIAN) == UBERBLOCK_MAGIC 1958 ? LITTLE_ENDIAN : BIG_ENDIAN; 1959 1960 debug("zfs endian set to %s\n", !ub_endian ? "big" : "little"); 1961 1962 err = zio_read(&ubbest->ub_rootbp, ub_endian, &osp, &ospsize, data); 1963 1964 if (err) { 1965 printf("couldn't zio_read object directory\n"); 1966 zfs_unmount(data); 1967 free(ubbest); 1968 return 0; 1969 } 1970 1971 if (ospsize < OBJSET_PHYS_SIZE_V14) { 1972 printf("osp too small\n"); 1973 zfs_unmount(data); 1974 free(osp); 1975 free(ubbest); 1976 return 0; 1977 } 1978 1979 /* Got the MOS. Save it at the memory addr MOS. */ 1980 memmove(&(data->mos.dn), &((objset_phys_t *) osp)->os_meta_dnode, DNODE_SIZE); 1981 data->mos.endian = 1982 (zfs_to_cpu64(ubbest->ub_rootbp.blk_prop, ub_endian) >> 63) & 1; 1983 memmove(&(data->current_uberblock), ubbest, sizeof(uberblock_t)); 1984 1985 free(osp); 1986 free(ubbest); 1987 1988 return data; 1989 } 1990 1991 int 1992 zfs_fetch_nvlist(device_t dev, char **nvlist) 1993 { 1994 struct zfs_data *zfs; 1995 int err; 1996 1997 zfs = zfs_mount(dev); 1998 if (!zfs) 1999 return ZFS_ERR_BAD_FS; 2000 err = int_zfs_fetch_nvlist(zfs, nvlist); 2001 zfs_unmount(zfs); 2002 return err; 2003 } 2004 2005 static int 2006 zfs_label(device_t device, char **label) 2007 { 2008 char *nvlist; 2009 int err; 2010 struct zfs_data *data; 2011 2012 data = zfs_mount(device); 2013 if (!data) 2014 return ZFS_ERR_BAD_FS; 2015 2016 err = int_zfs_fetch_nvlist(data, &nvlist); 2017 if (err) { 2018 zfs_unmount(data); 2019 return err; 2020 } 2021 2022 *label = zfs_nvlist_lookup_string(nvlist, ZPOOL_CONFIG_POOL_NAME); 2023 free(nvlist); 2024 zfs_unmount(data); 2025 return ZFS_ERR_NONE; 2026 } 2027 2028 static int 2029 zfs_uuid(device_t device, char **uuid) 2030 { 2031 struct zfs_data *data; 2032 2033 data = zfs_mount(device); 2034 if (!data) 2035 return ZFS_ERR_BAD_FS; 2036 2037 *uuid = malloc(17); /* %016llx + nil */ 2038 if (!*uuid) 2039 return ZFS_ERR_OUT_OF_MEMORY; 2040 2041 /* *uuid = xasprintf ("%016llx", (long long unsigned) data->pool_guid);*/ 2042 snprintf(*uuid, 17, "%016llx", (long long unsigned) data->pool_guid); 2043 zfs_unmount(data); 2044 2045 return ZFS_ERR_NONE; 2046 } 2047 2048 /* 2049 * zfs_open() locates a file in the rootpool by following the 2050 * MOS and places the dnode of the file in the memory address DNODE. 2051 */ 2052 int 2053 zfs_open(struct zfs_file *file, const char *fsfilename) 2054 { 2055 struct zfs_data *data; 2056 int err; 2057 int isfs; 2058 2059 data = zfs_mount(file->device); 2060 if (!data) 2061 return ZFS_ERR_BAD_FS; 2062 2063 err = dnode_get_fullpath(fsfilename, &(data->mdn), 0, 2064 &(data->dnode), &isfs, data); 2065 if (err) { 2066 zfs_unmount(data); 2067 return err; 2068 } 2069 2070 if (isfs) { 2071 zfs_unmount(data); 2072 printf("Missing @ or / separator\n"); 2073 return ZFS_ERR_FILE_NOT_FOUND; 2074 } 2075 2076 /* We found the dnode for this file. Verify if it is a plain file. */ 2077 if (data->dnode.dn.dn_type != DMU_OT_PLAIN_FILE_CONTENTS) { 2078 zfs_unmount(data); 2079 printf("not a file\n"); 2080 return ZFS_ERR_BAD_FILE_TYPE; 2081 } 2082 2083 /* get the file size and set the file position to 0 */ 2084 2085 /* 2086 * For DMU_OT_SA we will need to locate the SIZE attribute 2087 * attribute, which could be either in the bonus buffer 2088 * or the "spill" block. 2089 */ 2090 if (data->dnode.dn.dn_bonustype == DMU_OT_SA) { 2091 void *sahdrp; 2092 int hdrsize; 2093 2094 if (data->dnode.dn.dn_bonuslen != 0) { 2095 sahdrp = (sa_hdr_phys_t *) DN_BONUS(&data->dnode.dn); 2096 } else if (data->dnode.dn.dn_flags & DNODE_FLAG_SPILL_BLKPTR) { 2097 blkptr_t *bp = &data->dnode.dn.dn_spill; 2098 2099 err = zio_read(bp, data->dnode.endian, &sahdrp, NULL, data); 2100 if (err) 2101 return err; 2102 } else { 2103 printf("filesystem is corrupt :(\n"); 2104 return ZFS_ERR_BAD_FS; 2105 } 2106 2107 hdrsize = SA_HDR_SIZE(((sa_hdr_phys_t *) sahdrp)); 2108 file->size = *(uint64_t *) ((char *) sahdrp + hdrsize + SA_SIZE_OFFSET); 2109 } else { 2110 file->size = zfs_to_cpu64(((znode_phys_t *) DN_BONUS(&data->dnode.dn))->zp_size, data->dnode.endian); 2111 } 2112 2113 file->data = data; 2114 file->offset = 0; 2115 2116 return ZFS_ERR_NONE; 2117 } 2118 2119 uint64_t 2120 zfs_read(zfs_file_t file, char *buf, uint64_t len) 2121 { 2122 struct zfs_data *data = (struct zfs_data *) file->data; 2123 int blksz, movesize; 2124 uint64_t length; 2125 int64_t red; 2126 int err; 2127 2128 if (data->file_buf == NULL) { 2129 data->file_buf = malloc(SPA_MAXBLOCKSIZE); 2130 if (!data->file_buf) 2131 return -1; 2132 data->file_start = data->file_end = 0; 2133 } 2134 2135 /* 2136 * If offset is in memory, move it into the buffer provided and return. 2137 */ 2138 if (file->offset >= data->file_start 2139 && file->offset + len <= data->file_end) { 2140 memmove(buf, data->file_buf + file->offset - data->file_start, 2141 len); 2142 return len; 2143 } 2144 2145 blksz = zfs_to_cpu16(data->dnode.dn.dn_datablkszsec, 2146 data->dnode.endian) << SPA_MINBLOCKSHIFT; 2147 2148 /* 2149 * Entire Dnode is too big to fit into the space available. We 2150 * will need to read it in chunks. This could be optimized to 2151 * read in as large a chunk as there is space available, but for 2152 * now, this only reads in one data block at a time. 2153 */ 2154 length = len; 2155 red = 0; 2156 while (length) { 2157 void *t; 2158 /* 2159 * Find requested blkid and the offset within that block. 2160 */ 2161 uint64_t blkid = (file->offset + red) / blksz; 2162 free(data->file_buf); 2163 data->file_buf = 0; 2164 2165 err = dmu_read(&(data->dnode), blkid, &t, 2166 0, data); 2167 data->file_buf = t; 2168 if (err) 2169 return -1; 2170 2171 data->file_start = blkid * blksz; 2172 data->file_end = data->file_start + blksz; 2173 2174 movesize = MIN(length, data->file_end - (int) file->offset - red); 2175 2176 memmove(buf, data->file_buf + file->offset + red 2177 - data->file_start, movesize); 2178 buf += movesize; 2179 length -= movesize; 2180 red += movesize; 2181 } 2182 2183 return len; 2184 } 2185 2186 int 2187 zfs_close(zfs_file_t file) 2188 { 2189 zfs_unmount((struct zfs_data *) file->data); 2190 return ZFS_ERR_NONE; 2191 } 2192 2193 int 2194 zfs_getmdnobj(device_t dev, const char *fsfilename, 2195 uint64_t *mdnobj) 2196 { 2197 struct zfs_data *data; 2198 int err; 2199 int isfs; 2200 2201 data = zfs_mount(dev); 2202 if (!data) 2203 return ZFS_ERR_BAD_FS; 2204 2205 err = dnode_get_fullpath(fsfilename, &(data->mdn), mdnobj, 2206 &(data->dnode), &isfs, data); 2207 zfs_unmount(data); 2208 return err; 2209 } 2210 2211 static void 2212 fill_fs_info(struct zfs_dirhook_info *info, 2213 dnode_end_t mdn, struct zfs_data *data) 2214 { 2215 int err; 2216 dnode_end_t dn; 2217 uint64_t objnum; 2218 uint64_t headobj; 2219 2220 memset(info, 0, sizeof(*info)); 2221 2222 info->dir = 1; 2223 2224 if (mdn.dn.dn_type == DMU_OT_DSL_DIR) { 2225 headobj = zfs_to_cpu64(((dsl_dir_phys_t *) DN_BONUS(&mdn.dn))->dd_head_dataset_obj, mdn.endian); 2226 2227 err = dnode_get(&(data->mos), headobj, DMU_OT_DSL_DATASET, &mdn, data); 2228 if (err) { 2229 printf("zfs failed here 1\n"); 2230 return; 2231 } 2232 } 2233 make_mdn(&mdn, data); 2234 err = dnode_get(&mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE, 2235 &dn, data); 2236 if (err) { 2237 printf("zfs failed here 2\n"); 2238 return; 2239 } 2240 2241 err = zap_lookup(&dn, ZFS_ROOT_OBJ, &objnum, data); 2242 if (err) { 2243 printf("zfs failed here 3\n"); 2244 return; 2245 } 2246 2247 err = dnode_get(&mdn, objnum, 0, &dn, data); 2248 if (err) { 2249 printf("zfs failed here 4\n"); 2250 return; 2251 } 2252 2253 info->mtimeset = 1; 2254 info->mtime = zfs_to_cpu64(((znode_phys_t *) DN_BONUS(&dn.dn))->zp_mtime[0], dn.endian); 2255 2256 return; 2257 } 2258 2259 static int iterate_zap(const char *name, uint64_t val, struct zfs_data *data) 2260 { 2261 struct zfs_dirhook_info info; 2262 dnode_end_t dn; 2263 2264 memset(&info, 0, sizeof(info)); 2265 2266 dnode_get(&(data->mdn), val, 0, &dn, data); 2267 info.mtimeset = 1; 2268 info.mtime = zfs_to_cpu64(((znode_phys_t *) DN_BONUS(&dn.dn))->zp_mtime[0], dn.endian); 2269 info.dir = (dn.dn.dn_type == DMU_OT_DIRECTORY_CONTENTS); 2270 debug("zfs type=%d, name=%s\n", 2271 (int)dn.dn.dn_type, (char *)name); 2272 if (!data->userhook) 2273 return 0; 2274 return data->userhook(name, &info); 2275 } 2276 2277 static int iterate_zap_fs(const char *name, uint64_t val, struct zfs_data *data) 2278 { 2279 struct zfs_dirhook_info info; 2280 dnode_end_t mdn; 2281 int err; 2282 err = dnode_get(&(data->mos), val, 0, &mdn, data); 2283 if (err) 2284 return 0; 2285 if (mdn.dn.dn_type != DMU_OT_DSL_DIR) 2286 return 0; 2287 2288 fill_fs_info(&info, mdn, data); 2289 2290 if (!data->userhook) 2291 return 0; 2292 return data->userhook(name, &info); 2293 } 2294 2295 static int iterate_zap_snap(const char *name, uint64_t val, struct zfs_data *data) 2296 { 2297 struct zfs_dirhook_info info; 2298 char *name2; 2299 int ret = 0; 2300 dnode_end_t mdn; 2301 int err; 2302 2303 err = dnode_get(&(data->mos), val, 0, &mdn, data); 2304 if (err) 2305 return 0; 2306 2307 if (mdn.dn.dn_type != DMU_OT_DSL_DATASET) 2308 return 0; 2309 2310 fill_fs_info(&info, mdn, data); 2311 2312 name2 = malloc(strlen(name) + 2); 2313 name2[0] = '@'; 2314 memcpy(name2 + 1, name, strlen(name) + 1); 2315 if (data->userhook) 2316 ret = data->userhook(name2, &info); 2317 free(name2); 2318 return ret; 2319 } 2320 2321 int 2322 zfs_ls(device_t device, const char *path, 2323 int (*hook)(const char *, const struct zfs_dirhook_info *)) 2324 { 2325 struct zfs_data *data; 2326 int err; 2327 int isfs; 2328 #if 0 2329 char *label = NULL; 2330 2331 zfs_label(device, &label); 2332 if (label) 2333 printf("ZPOOL label '%s'\n", 2334 label); 2335 #endif 2336 2337 data = zfs_mount(device); 2338 if (!data) 2339 return ZFS_ERR_BAD_FS; 2340 2341 data->userhook = hook; 2342 2343 err = dnode_get_fullpath(path, &(data->mdn), 0, &(data->dnode), &isfs, data); 2344 if (err) { 2345 zfs_unmount(data); 2346 return err; 2347 } 2348 if (isfs) { 2349 uint64_t childobj, headobj; 2350 uint64_t snapobj; 2351 dnode_end_t dn; 2352 struct zfs_dirhook_info info; 2353 2354 fill_fs_info(&info, data->dnode, data); 2355 hook("@", &info); 2356 2357 childobj = zfs_to_cpu64(((dsl_dir_phys_t *) DN_BONUS(&data->dnode.dn))->dd_child_dir_zapobj, data->dnode.endian); 2358 headobj = zfs_to_cpu64(((dsl_dir_phys_t *) DN_BONUS(&data->dnode.dn))->dd_head_dataset_obj, data->dnode.endian); 2359 err = dnode_get(&(data->mos), childobj, 2360 DMU_OT_DSL_DIR_CHILD_MAP, &dn, data); 2361 if (err) { 2362 zfs_unmount(data); 2363 return err; 2364 } 2365 2366 2367 zap_iterate(&dn, iterate_zap_fs, data); 2368 2369 err = dnode_get(&(data->mos), headobj, DMU_OT_DSL_DATASET, &dn, data); 2370 if (err) { 2371 zfs_unmount(data); 2372 return err; 2373 } 2374 2375 snapobj = zfs_to_cpu64(((dsl_dataset_phys_t *) DN_BONUS(&dn.dn))->ds_snapnames_zapobj, dn.endian); 2376 2377 err = dnode_get(&(data->mos), snapobj, 2378 DMU_OT_DSL_DS_SNAP_MAP, &dn, data); 2379 if (err) { 2380 zfs_unmount(data); 2381 return err; 2382 } 2383 2384 zap_iterate(&dn, iterate_zap_snap, data); 2385 } else { 2386 if (data->dnode.dn.dn_type != DMU_OT_DIRECTORY_CONTENTS) { 2387 zfs_unmount(data); 2388 printf("not a directory\n"); 2389 return ZFS_ERR_BAD_FILE_TYPE; 2390 } 2391 zap_iterate(&(data->dnode), iterate_zap, data); 2392 } 2393 zfs_unmount(data); 2394 return ZFS_ERR_NONE; 2395 } 2396