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