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