1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 */ 24 25 /* Portions Copyright 2007 Jeremy Teo */ 26 27 #ifdef _KERNEL 28 #include <sys/types.h> 29 #include <sys/param.h> 30 #include <sys/time.h> 31 #include <sys/systm.h> 32 #include <sys/sysmacros.h> 33 #include <sys/resource.h> 34 #include <sys/mntent.h> 35 #include <sys/mkdev.h> 36 #include <sys/u8_textprep.h> 37 #include <sys/dsl_dataset.h> 38 #include <sys/vfs.h> 39 #include <sys/vfs_opreg.h> 40 #include <sys/vnode.h> 41 #include <sys/file.h> 42 #include <sys/kmem.h> 43 #include <sys/errno.h> 44 #include <sys/unistd.h> 45 #include <sys/mode.h> 46 #include <sys/atomic.h> 47 #include <vm/pvn.h> 48 #include "fs/fs_subr.h" 49 #include <sys/zfs_dir.h> 50 #include <sys/zfs_acl.h> 51 #include <sys/zfs_ioctl.h> 52 #include <sys/zfs_rlock.h> 53 #include <sys/zfs_fuid.h> 54 #include <sys/dnode.h> 55 #include <sys/fs/zfs.h> 56 #include <sys/kidmap.h> 57 #endif /* _KERNEL */ 58 59 #include <sys/dmu.h> 60 #include <sys/refcount.h> 61 #include <sys/stat.h> 62 #include <sys/zap.h> 63 #include <sys/zfs_znode.h> 64 #include <sys/sa.h> 65 #include <sys/zfs_sa.h> 66 67 #include "zfs_prop.h" 68 #include "zfs_comutil.h" 69 70 /* 71 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only 72 * turned on when DEBUG is also defined. 73 */ 74 #ifdef DEBUG 75 #define ZNODE_STATS 76 #endif /* DEBUG */ 77 78 #ifdef ZNODE_STATS 79 #define ZNODE_STAT_ADD(stat) ((stat)++) 80 #else 81 #define ZNODE_STAT_ADD(stat) /* nothing */ 82 #endif /* ZNODE_STATS */ 83 84 /* 85 * Functions needed for userland (ie: libzpool) are not put under 86 * #ifdef_KERNEL; the rest of the functions have dependencies 87 * (such as VFS logic) that will not compile easily in userland. 88 */ 89 #ifdef _KERNEL 90 /* 91 * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to 92 * be freed before it can be safely accessed. 93 */ 94 krwlock_t zfsvfs_lock; 95 96 static kmem_cache_t *znode_cache = NULL; 97 98 /*ARGSUSED*/ 99 static void 100 znode_evict_error(dmu_buf_t *dbuf, void *user_ptr) 101 { 102 /* 103 * We should never drop all dbuf refs without first clearing 104 * the eviction callback. 105 */ 106 panic("evicting znode %p\n", user_ptr); 107 } 108 109 /*ARGSUSED*/ 110 static int 111 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags) 112 { 113 znode_t *zp = buf; 114 115 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs)); 116 117 zp->z_vnode = vn_alloc(kmflags); 118 if (zp->z_vnode == NULL) { 119 return (-1); 120 } 121 ZTOV(zp)->v_data = zp; 122 123 list_link_init(&zp->z_link_node); 124 125 mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL); 126 rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL); 127 rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL); 128 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL); 129 130 mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL); 131 avl_create(&zp->z_range_avl, zfs_range_compare, 132 sizeof (rl_t), offsetof(rl_t, r_node)); 133 134 zp->z_dirlocks = NULL; 135 zp->z_acl_cached = NULL; 136 zp->z_moved = 0; 137 return (0); 138 } 139 140 /*ARGSUSED*/ 141 static void 142 zfs_znode_cache_destructor(void *buf, void *arg) 143 { 144 znode_t *zp = buf; 145 146 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs)); 147 ASSERT(ZTOV(zp)->v_data == zp); 148 vn_free(ZTOV(zp)); 149 ASSERT(!list_link_active(&zp->z_link_node)); 150 mutex_destroy(&zp->z_lock); 151 rw_destroy(&zp->z_parent_lock); 152 rw_destroy(&zp->z_name_lock); 153 mutex_destroy(&zp->z_acl_lock); 154 avl_destroy(&zp->z_range_avl); 155 mutex_destroy(&zp->z_range_lock); 156 157 ASSERT(zp->z_dirlocks == NULL); 158 ASSERT(zp->z_acl_cached == NULL); 159 } 160 161 #ifdef ZNODE_STATS 162 static struct { 163 uint64_t zms_zfsvfs_invalid; 164 uint64_t zms_zfsvfs_recheck1; 165 uint64_t zms_zfsvfs_unmounted; 166 uint64_t zms_zfsvfs_recheck2; 167 uint64_t zms_obj_held; 168 uint64_t zms_vnode_locked; 169 uint64_t zms_not_only_dnlc; 170 } znode_move_stats; 171 #endif /* ZNODE_STATS */ 172 173 static void 174 zfs_znode_move_impl(znode_t *ozp, znode_t *nzp) 175 { 176 vnode_t *vp; 177 178 /* Copy fields. */ 179 nzp->z_zfsvfs = ozp->z_zfsvfs; 180 181 /* Swap vnodes. */ 182 vp = nzp->z_vnode; 183 nzp->z_vnode = ozp->z_vnode; 184 ozp->z_vnode = vp; /* let destructor free the overwritten vnode */ 185 ZTOV(ozp)->v_data = ozp; 186 ZTOV(nzp)->v_data = nzp; 187 188 nzp->z_id = ozp->z_id; 189 ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */ 190 ASSERT(avl_numnodes(&ozp->z_range_avl) == 0); 191 nzp->z_unlinked = ozp->z_unlinked; 192 nzp->z_atime_dirty = ozp->z_atime_dirty; 193 nzp->z_zn_prefetch = ozp->z_zn_prefetch; 194 nzp->z_blksz = ozp->z_blksz; 195 nzp->z_seq = ozp->z_seq; 196 nzp->z_mapcnt = ozp->z_mapcnt; 197 nzp->z_gen = ozp->z_gen; 198 nzp->z_sync_cnt = ozp->z_sync_cnt; 199 nzp->z_is_sa = ozp->z_is_sa; 200 nzp->z_sa_hdl = ozp->z_sa_hdl; 201 bcopy(ozp->z_atime, nzp->z_atime, sizeof (uint64_t) * 2); 202 nzp->z_links = ozp->z_links; 203 nzp->z_size = ozp->z_size; 204 nzp->z_pflags = ozp->z_pflags; 205 nzp->z_uid = ozp->z_uid; 206 nzp->z_gid = ozp->z_gid; 207 nzp->z_mode = ozp->z_mode; 208 209 /* 210 * Since this is just an idle znode and kmem is already dealing with 211 * memory pressure, release any cached ACL. 212 */ 213 if (ozp->z_acl_cached) { 214 zfs_acl_free(ozp->z_acl_cached); 215 ozp->z_acl_cached = NULL; 216 } 217 218 sa_set_userp(nzp->z_sa_hdl, nzp); 219 220 /* 221 * Invalidate the original znode by clearing fields that provide a 222 * pointer back to the znode. Set the low bit of the vfs pointer to 223 * ensure that zfs_znode_move() recognizes the znode as invalid in any 224 * subsequent callback. 225 */ 226 ozp->z_sa_hdl = NULL; 227 POINTER_INVALIDATE(&ozp->z_zfsvfs); 228 229 /* 230 * Mark the znode. 231 */ 232 nzp->z_moved = 1; 233 ozp->z_moved = (uint8_t)-1; 234 } 235 236 /*ARGSUSED*/ 237 static kmem_cbrc_t 238 zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg) 239 { 240 znode_t *ozp = buf, *nzp = newbuf; 241 zfsvfs_t *zfsvfs; 242 vnode_t *vp; 243 244 /* 245 * The znode is on the file system's list of known znodes if the vfs 246 * pointer is valid. We set the low bit of the vfs pointer when freeing 247 * the znode to invalidate it, and the memory patterns written by kmem 248 * (baddcafe and deadbeef) set at least one of the two low bits. A newly 249 * created znode sets the vfs pointer last of all to indicate that the 250 * znode is known and in a valid state to be moved by this function. 251 */ 252 zfsvfs = ozp->z_zfsvfs; 253 if (!POINTER_IS_VALID(zfsvfs)) { 254 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid); 255 return (KMEM_CBRC_DONT_KNOW); 256 } 257 258 /* 259 * Close a small window in which it's possible that the filesystem could 260 * be unmounted and freed, and zfsvfs, though valid in the previous 261 * statement, could point to unrelated memory by the time we try to 262 * prevent the filesystem from being unmounted. 263 */ 264 rw_enter(&zfsvfs_lock, RW_WRITER); 265 if (zfsvfs != ozp->z_zfsvfs) { 266 rw_exit(&zfsvfs_lock); 267 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1); 268 return (KMEM_CBRC_DONT_KNOW); 269 } 270 271 /* 272 * If the znode is still valid, then so is the file system. We know that 273 * no valid file system can be freed while we hold zfsvfs_lock, so we 274 * can safely ensure that the filesystem is not and will not be 275 * unmounted. The next statement is equivalent to ZFS_ENTER(). 276 */ 277 rrw_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG); 278 if (zfsvfs->z_unmounted) { 279 ZFS_EXIT(zfsvfs); 280 rw_exit(&zfsvfs_lock); 281 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted); 282 return (KMEM_CBRC_DONT_KNOW); 283 } 284 rw_exit(&zfsvfs_lock); 285 286 mutex_enter(&zfsvfs->z_znodes_lock); 287 /* 288 * Recheck the vfs pointer in case the znode was removed just before 289 * acquiring the lock. 290 */ 291 if (zfsvfs != ozp->z_zfsvfs) { 292 mutex_exit(&zfsvfs->z_znodes_lock); 293 ZFS_EXIT(zfsvfs); 294 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2); 295 return (KMEM_CBRC_DONT_KNOW); 296 } 297 298 /* 299 * At this point we know that as long as we hold z_znodes_lock, the 300 * znode cannot be freed and fields within the znode can be safely 301 * accessed. Now, prevent a race with zfs_zget(). 302 */ 303 if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) { 304 mutex_exit(&zfsvfs->z_znodes_lock); 305 ZFS_EXIT(zfsvfs); 306 ZNODE_STAT_ADD(znode_move_stats.zms_obj_held); 307 return (KMEM_CBRC_LATER); 308 } 309 310 vp = ZTOV(ozp); 311 if (mutex_tryenter(&vp->v_lock) == 0) { 312 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id); 313 mutex_exit(&zfsvfs->z_znodes_lock); 314 ZFS_EXIT(zfsvfs); 315 ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked); 316 return (KMEM_CBRC_LATER); 317 } 318 319 /* Only move znodes that are referenced _only_ by the DNLC. */ 320 if (vp->v_count != 1 || !vn_in_dnlc(vp)) { 321 mutex_exit(&vp->v_lock); 322 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id); 323 mutex_exit(&zfsvfs->z_znodes_lock); 324 ZFS_EXIT(zfsvfs); 325 ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc); 326 return (KMEM_CBRC_LATER); 327 } 328 329 /* 330 * The znode is known and in a valid state to move. We're holding the 331 * locks needed to execute the critical section. 332 */ 333 zfs_znode_move_impl(ozp, nzp); 334 mutex_exit(&vp->v_lock); 335 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id); 336 337 list_link_replace(&ozp->z_link_node, &nzp->z_link_node); 338 mutex_exit(&zfsvfs->z_znodes_lock); 339 ZFS_EXIT(zfsvfs); 340 341 return (KMEM_CBRC_YES); 342 } 343 344 void 345 zfs_znode_init(void) 346 { 347 /* 348 * Initialize zcache 349 */ 350 rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL); 351 ASSERT(znode_cache == NULL); 352 znode_cache = kmem_cache_create("zfs_znode_cache", 353 sizeof (znode_t), 0, zfs_znode_cache_constructor, 354 zfs_znode_cache_destructor, NULL, NULL, NULL, 0); 355 kmem_cache_set_move(znode_cache, zfs_znode_move); 356 } 357 358 void 359 zfs_znode_fini(void) 360 { 361 /* 362 * Cleanup vfs & vnode ops 363 */ 364 zfs_remove_op_tables(); 365 366 /* 367 * Cleanup zcache 368 */ 369 if (znode_cache) 370 kmem_cache_destroy(znode_cache); 371 znode_cache = NULL; 372 rw_destroy(&zfsvfs_lock); 373 } 374 375 struct vnodeops *zfs_dvnodeops; 376 struct vnodeops *zfs_fvnodeops; 377 struct vnodeops *zfs_symvnodeops; 378 struct vnodeops *zfs_xdvnodeops; 379 struct vnodeops *zfs_evnodeops; 380 struct vnodeops *zfs_sharevnodeops; 381 382 void 383 zfs_remove_op_tables() 384 { 385 /* 386 * Remove vfs ops 387 */ 388 ASSERT(zfsfstype); 389 (void) vfs_freevfsops_by_type(zfsfstype); 390 zfsfstype = 0; 391 392 /* 393 * Remove vnode ops 394 */ 395 if (zfs_dvnodeops) 396 vn_freevnodeops(zfs_dvnodeops); 397 if (zfs_fvnodeops) 398 vn_freevnodeops(zfs_fvnodeops); 399 if (zfs_symvnodeops) 400 vn_freevnodeops(zfs_symvnodeops); 401 if (zfs_xdvnodeops) 402 vn_freevnodeops(zfs_xdvnodeops); 403 if (zfs_evnodeops) 404 vn_freevnodeops(zfs_evnodeops); 405 if (zfs_sharevnodeops) 406 vn_freevnodeops(zfs_sharevnodeops); 407 408 zfs_dvnodeops = NULL; 409 zfs_fvnodeops = NULL; 410 zfs_symvnodeops = NULL; 411 zfs_xdvnodeops = NULL; 412 zfs_evnodeops = NULL; 413 zfs_sharevnodeops = NULL; 414 } 415 416 extern const fs_operation_def_t zfs_dvnodeops_template[]; 417 extern const fs_operation_def_t zfs_fvnodeops_template[]; 418 extern const fs_operation_def_t zfs_xdvnodeops_template[]; 419 extern const fs_operation_def_t zfs_symvnodeops_template[]; 420 extern const fs_operation_def_t zfs_evnodeops_template[]; 421 extern const fs_operation_def_t zfs_sharevnodeops_template[]; 422 423 int 424 zfs_create_op_tables() 425 { 426 int error; 427 428 /* 429 * zfs_dvnodeops can be set if mod_remove() calls mod_installfs() 430 * due to a failure to remove the the 2nd modlinkage (zfs_modldrv). 431 * In this case we just return as the ops vectors are already set up. 432 */ 433 if (zfs_dvnodeops) 434 return (0); 435 436 error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template, 437 &zfs_dvnodeops); 438 if (error) 439 return (error); 440 441 error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template, 442 &zfs_fvnodeops); 443 if (error) 444 return (error); 445 446 error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template, 447 &zfs_symvnodeops); 448 if (error) 449 return (error); 450 451 error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template, 452 &zfs_xdvnodeops); 453 if (error) 454 return (error); 455 456 error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template, 457 &zfs_evnodeops); 458 if (error) 459 return (error); 460 461 error = vn_make_ops(MNTTYPE_ZFS, zfs_sharevnodeops_template, 462 &zfs_sharevnodeops); 463 464 return (error); 465 } 466 467 int 468 zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx) 469 { 470 zfs_acl_ids_t acl_ids; 471 vattr_t vattr; 472 znode_t *sharezp; 473 vnode_t *vp; 474 znode_t *zp; 475 int error; 476 477 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 478 vattr.va_type = VDIR; 479 vattr.va_mode = S_IFDIR|0555; 480 vattr.va_uid = crgetuid(kcred); 481 vattr.va_gid = crgetgid(kcred); 482 483 sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP); 484 ASSERT(!POINTER_IS_VALID(sharezp->z_zfsvfs)); 485 sharezp->z_moved = 0; 486 sharezp->z_unlinked = 0; 487 sharezp->z_atime_dirty = 0; 488 sharezp->z_zfsvfs = zfsvfs; 489 sharezp->z_is_sa = zfsvfs->z_use_sa; 490 491 vp = ZTOV(sharezp); 492 vn_reinit(vp); 493 vp->v_type = VDIR; 494 495 VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr, 496 kcred, NULL, &acl_ids)); 497 zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids); 498 ASSERT3P(zp, ==, sharezp); 499 ASSERT(!vn_in_dnlc(ZTOV(sharezp))); /* not valid to move */ 500 POINTER_INVALIDATE(&sharezp->z_zfsvfs); 501 error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ, 502 ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx); 503 zfsvfs->z_shares_dir = sharezp->z_id; 504 505 zfs_acl_ids_free(&acl_ids); 506 ZTOV(sharezp)->v_count = 0; 507 sa_handle_destroy(sharezp->z_sa_hdl); 508 kmem_cache_free(znode_cache, sharezp); 509 510 return (error); 511 } 512 513 /* 514 * define a couple of values we need available 515 * for both 64 and 32 bit environments. 516 */ 517 #ifndef NBITSMINOR64 518 #define NBITSMINOR64 32 519 #endif 520 #ifndef MAXMAJ64 521 #define MAXMAJ64 0xffffffffUL 522 #endif 523 #ifndef MAXMIN64 524 #define MAXMIN64 0xffffffffUL 525 #endif 526 527 /* 528 * Create special expldev for ZFS private use. 529 * Can't use standard expldev since it doesn't do 530 * what we want. The standard expldev() takes a 531 * dev32_t in LP64 and expands it to a long dev_t. 532 * We need an interface that takes a dev32_t in ILP32 533 * and expands it to a long dev_t. 534 */ 535 static uint64_t 536 zfs_expldev(dev_t dev) 537 { 538 #ifndef _LP64 539 major_t major = (major_t)dev >> NBITSMINOR32 & MAXMAJ32; 540 return (((uint64_t)major << NBITSMINOR64) | 541 ((minor_t)dev & MAXMIN32)); 542 #else 543 return (dev); 544 #endif 545 } 546 547 /* 548 * Special cmpldev for ZFS private use. 549 * Can't use standard cmpldev since it takes 550 * a long dev_t and compresses it to dev32_t in 551 * LP64. We need to do a compaction of a long dev_t 552 * to a dev32_t in ILP32. 553 */ 554 dev_t 555 zfs_cmpldev(uint64_t dev) 556 { 557 #ifndef _LP64 558 minor_t minor = (minor_t)dev & MAXMIN64; 559 major_t major = (major_t)(dev >> NBITSMINOR64) & MAXMAJ64; 560 561 if (major > MAXMAJ32 || minor > MAXMIN32) 562 return (NODEV32); 563 564 return (((dev32_t)major << NBITSMINOR32) | minor); 565 #else 566 return (dev); 567 #endif 568 } 569 570 static void 571 zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp, 572 dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl) 573 { 574 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs)); 575 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id))); 576 577 mutex_enter(&zp->z_lock); 578 579 ASSERT(zp->z_sa_hdl == NULL); 580 ASSERT(zp->z_acl_cached == NULL); 581 if (sa_hdl == NULL) { 582 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, zp, 583 SA_HDL_SHARED, &zp->z_sa_hdl)); 584 } else { 585 zp->z_sa_hdl = sa_hdl; 586 sa_set_userp(sa_hdl, zp); 587 } 588 589 zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE; 590 591 /* 592 * Slap on VROOT if we are the root znode 593 */ 594 if (zp->z_id == zfsvfs->z_root) 595 ZTOV(zp)->v_flag |= VROOT; 596 597 mutex_exit(&zp->z_lock); 598 vn_exists(ZTOV(zp)); 599 } 600 601 void 602 zfs_znode_dmu_fini(znode_t *zp) 603 { 604 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) || 605 zp->z_unlinked || 606 RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock)); 607 608 sa_handle_destroy(zp->z_sa_hdl); 609 zp->z_sa_hdl = NULL; 610 } 611 612 /* 613 * Construct a new znode/vnode and intialize. 614 * 615 * This does not do a call to dmu_set_user() that is 616 * up to the caller to do, in case you don't want to 617 * return the znode 618 */ 619 static znode_t * 620 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz, 621 dmu_object_type_t obj_type, sa_handle_t *hdl) 622 { 623 znode_t *zp; 624 vnode_t *vp; 625 uint64_t mode; 626 uint64_t parent; 627 uint64_t uid, gid; 628 sa_bulk_attr_t bulk[9]; 629 int count = 0; 630 631 zp = kmem_cache_alloc(znode_cache, KM_SLEEP); 632 633 ASSERT(zp->z_dirlocks == NULL); 634 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs)); 635 zp->z_moved = 0; 636 637 /* 638 * Defer setting z_zfsvfs until the znode is ready to be a candidate for 639 * the zfs_znode_move() callback. 640 */ 641 zp->z_sa_hdl = NULL; 642 zp->z_unlinked = 0; 643 zp->z_atime_dirty = 0; 644 zp->z_mapcnt = 0; 645 zp->z_id = db->db_object; 646 zp->z_blksz = blksz; 647 zp->z_seq = 0x7A4653; 648 zp->z_sync_cnt = 0; 649 650 vp = ZTOV(zp); 651 vn_reinit(vp); 652 653 zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl); 654 655 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8); 656 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8); 657 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL, 658 &zp->z_size, 8); 659 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL, 660 &zp->z_links, 8); 661 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 662 &zp->z_pflags, 8); 663 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8); 664 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, 665 &zp->z_atime, 16); 666 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, 667 &uid, 8); 668 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, 669 &gid, 8); 670 671 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) { 672 if (hdl == NULL) 673 sa_handle_destroy(zp->z_sa_hdl); 674 kmem_cache_free(znode_cache, zp); 675 return (NULL); 676 } 677 678 zp->z_uid = zfs_fuid_map_id(zfsvfs, uid, CRED(), ZFS_OWNER); 679 zp->z_gid = zfs_fuid_map_id(zfsvfs, gid, CRED(), ZFS_GROUP); 680 zp->z_mode = mode; 681 vp->v_vfsp = zfsvfs->z_parent->z_vfs; 682 683 vp->v_type = IFTOVT((mode_t)mode); 684 685 switch (vp->v_type) { 686 case VDIR: 687 if (zp->z_pflags & ZFS_XATTR) { 688 vn_setops(vp, zfs_xdvnodeops); 689 vp->v_flag |= V_XATTRDIR; 690 } else { 691 vn_setops(vp, zfs_dvnodeops); 692 } 693 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */ 694 break; 695 case VBLK: 696 case VCHR: 697 { 698 uint64_t rdev; 699 VERIFY(sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zfsvfs), 700 &rdev, sizeof (rdev)) == 0); 701 702 vp->v_rdev = zfs_cmpldev(rdev); 703 } 704 /*FALLTHROUGH*/ 705 case VFIFO: 706 case VSOCK: 707 case VDOOR: 708 vn_setops(vp, zfs_fvnodeops); 709 break; 710 case VREG: 711 vp->v_flag |= VMODSORT; 712 if (parent == zfsvfs->z_shares_dir) { 713 ASSERT(uid == 0 && gid == 0); 714 vn_setops(vp, zfs_sharevnodeops); 715 } else { 716 vn_setops(vp, zfs_fvnodeops); 717 } 718 break; 719 case VLNK: 720 vn_setops(vp, zfs_symvnodeops); 721 break; 722 default: 723 vn_setops(vp, zfs_evnodeops); 724 break; 725 } 726 727 mutex_enter(&zfsvfs->z_znodes_lock); 728 list_insert_tail(&zfsvfs->z_all_znodes, zp); 729 membar_producer(); 730 /* 731 * Everything else must be valid before assigning z_zfsvfs makes the 732 * znode eligible for zfs_znode_move(). 733 */ 734 zp->z_zfsvfs = zfsvfs; 735 mutex_exit(&zfsvfs->z_znodes_lock); 736 737 VFS_HOLD(zfsvfs->z_vfs); 738 return (zp); 739 } 740 741 static uint64_t empty_xattr; 742 static uint64_t pad[4]; 743 static zfs_acl_phys_t acl_phys; 744 /* 745 * Create a new DMU object to hold a zfs znode. 746 * 747 * IN: dzp - parent directory for new znode 748 * vap - file attributes for new znode 749 * tx - dmu transaction id for zap operations 750 * cr - credentials of caller 751 * flag - flags: 752 * IS_ROOT_NODE - new object will be root 753 * IS_XATTR - new object is an attribute 754 * bonuslen - length of bonus buffer 755 * setaclp - File/Dir initial ACL 756 * fuidp - Tracks fuid allocation. 757 * 758 * OUT: zpp - allocated znode 759 * 760 */ 761 void 762 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr, 763 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids) 764 { 765 uint64_t crtime[2], atime[2], mtime[2], ctime[2]; 766 uint64_t mode, size, links, parent, pflags; 767 uint64_t dzp_pflags = 0; 768 uint64_t rdev = 0; 769 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 770 dmu_buf_t *db; 771 timestruc_t now; 772 uint64_t gen, obj; 773 int err; 774 int bonuslen; 775 sa_handle_t *sa_hdl; 776 dmu_object_type_t obj_type; 777 sa_bulk_attr_t sa_attrs[ZPL_END]; 778 int cnt = 0; 779 zfs_acl_locator_cb_t locate = { 0 }; 780 781 ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE)); 782 783 if (zfsvfs->z_replay) { 784 obj = vap->va_nodeid; 785 now = vap->va_ctime; /* see zfs_replay_create() */ 786 gen = vap->va_nblocks; /* ditto */ 787 } else { 788 obj = 0; 789 gethrestime(&now); 790 gen = dmu_tx_get_txg(tx); 791 } 792 793 obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE; 794 bonuslen = (obj_type == DMU_OT_SA) ? 795 DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE; 796 797 /* 798 * Create a new DMU object. 799 */ 800 /* 801 * There's currently no mechanism for pre-reading the blocks that will 802 * be needed to allocate a new object, so we accept the small chance 803 * that there will be an i/o error and we will fail one of the 804 * assertions below. 805 */ 806 if (vap->va_type == VDIR) { 807 if (zfsvfs->z_replay) { 808 err = zap_create_claim_norm(zfsvfs->z_os, obj, 809 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS, 810 obj_type, bonuslen, tx); 811 ASSERT3U(err, ==, 0); 812 } else { 813 obj = zap_create_norm(zfsvfs->z_os, 814 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS, 815 obj_type, bonuslen, tx); 816 } 817 } else { 818 if (zfsvfs->z_replay) { 819 err = dmu_object_claim(zfsvfs->z_os, obj, 820 DMU_OT_PLAIN_FILE_CONTENTS, 0, 821 obj_type, bonuslen, tx); 822 ASSERT3U(err, ==, 0); 823 } else { 824 obj = dmu_object_alloc(zfsvfs->z_os, 825 DMU_OT_PLAIN_FILE_CONTENTS, 0, 826 obj_type, bonuslen, tx); 827 } 828 } 829 830 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj); 831 VERIFY(0 == sa_buf_hold(zfsvfs->z_os, obj, NULL, &db)); 832 833 /* 834 * If this is the root, fix up the half-initialized parent pointer 835 * to reference the just-allocated physical data area. 836 */ 837 if (flag & IS_ROOT_NODE) { 838 dzp->z_id = obj; 839 } else { 840 dzp_pflags = dzp->z_pflags; 841 } 842 843 /* 844 * If parent is an xattr, so am I. 845 */ 846 if (dzp_pflags & ZFS_XATTR) { 847 flag |= IS_XATTR; 848 } 849 850 if (zfsvfs->z_use_fuids) 851 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED; 852 else 853 pflags = 0; 854 855 if (vap->va_type == VDIR) { 856 size = 2; /* contents ("." and "..") */ 857 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1; 858 } else { 859 size = links = 0; 860 } 861 862 if (vap->va_type == VBLK || vap->va_type == VCHR) { 863 rdev = zfs_expldev(vap->va_rdev); 864 } 865 866 parent = dzp->z_id; 867 mode = acl_ids->z_mode; 868 if (flag & IS_XATTR) 869 pflags |= ZFS_XATTR; 870 871 /* 872 * No execs denied will be deterimed when zfs_mode_compute() is called. 873 */ 874 pflags |= acl_ids->z_aclp->z_hints & 875 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT| 876 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED); 877 878 ZFS_TIME_ENCODE(&now, crtime); 879 ZFS_TIME_ENCODE(&now, ctime); 880 881 if (vap->va_mask & AT_ATIME) { 882 ZFS_TIME_ENCODE(&vap->va_atime, atime); 883 } else { 884 ZFS_TIME_ENCODE(&now, atime); 885 } 886 887 if (vap->va_mask & AT_MTIME) { 888 ZFS_TIME_ENCODE(&vap->va_mtime, mtime); 889 } else { 890 ZFS_TIME_ENCODE(&now, mtime); 891 } 892 893 /* Now add in all of the "SA" attributes */ 894 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED, 895 &sa_hdl)); 896 897 /* 898 * Setup the array of attributes to be replaced/set on the new file 899 * 900 * order for DMU_OT_ZNODE is critical since it needs to be constructed 901 * in the old znode_phys_t format. Don't change this ordering 902 */ 903 904 if (obj_type == DMU_OT_ZNODE) { 905 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs), 906 NULL, &atime, 16); 907 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs), 908 NULL, &mtime, 16); 909 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs), 910 NULL, &ctime, 16); 911 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs), 912 NULL, &crtime, 16); 913 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs), 914 NULL, &gen, 8); 915 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs), 916 NULL, &mode, 8); 917 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs), 918 NULL, &size, 8); 919 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs), 920 NULL, &parent, 8); 921 } else { 922 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs), 923 NULL, &mode, 8); 924 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs), 925 NULL, &size, 8); 926 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs), 927 NULL, &gen, 8); 928 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL, 929 &acl_ids->z_fuid, 8); 930 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL, 931 &acl_ids->z_fgid, 8); 932 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs), 933 NULL, &parent, 8); 934 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs), 935 NULL, &pflags, 8); 936 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs), 937 NULL, &atime, 16); 938 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs), 939 NULL, &mtime, 16); 940 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs), 941 NULL, &ctime, 16); 942 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs), 943 NULL, &crtime, 16); 944 } 945 946 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8); 947 948 if (obj_type == DMU_OT_ZNODE) { 949 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL, 950 &empty_xattr, 8); 951 } 952 if (obj_type == DMU_OT_ZNODE || 953 (vap->va_type == VBLK || vap->va_type == VCHR)) { 954 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs), 955 NULL, &rdev, 8); 956 957 } 958 if (obj_type == DMU_OT_ZNODE) { 959 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs), 960 NULL, &pflags, 8); 961 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL, 962 &acl_ids->z_fuid, 8); 963 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL, 964 &acl_ids->z_fgid, 8); 965 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad, 966 sizeof (uint64_t) * 4); 967 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL, 968 &acl_phys, sizeof (zfs_acl_phys_t)); 969 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) { 970 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL, 971 &acl_ids->z_aclp->z_acl_count, 8); 972 locate.cb_aclp = acl_ids->z_aclp; 973 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs), 974 zfs_acl_data_locator, &locate, 975 acl_ids->z_aclp->z_acl_bytes); 976 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags, 977 acl_ids->z_fuid, acl_ids->z_fgid); 978 } 979 980 VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0); 981 982 if (!(flag & IS_ROOT_NODE)) { 983 *zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl); 984 ASSERT(*zpp != NULL); 985 } else { 986 /* 987 * If we are creating the root node, the "parent" we 988 * passed in is the znode for the root. 989 */ 990 *zpp = dzp; 991 992 (*zpp)->z_sa_hdl = sa_hdl; 993 } 994 995 (*zpp)->z_pflags = pflags; 996 (*zpp)->z_mode = mode; 997 998 if (vap->va_mask & AT_XVATTR) 999 zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx); 1000 1001 if (obj_type == DMU_OT_ZNODE || 1002 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) { 1003 err = zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx); 1004 ASSERT3P(err, ==, 0); 1005 } 1006 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj); 1007 } 1008 1009 /* 1010 * zfs_xvattr_set only updates the in-core attributes 1011 * it is assumed the caller will be doing an sa_bulk_update 1012 * to push the changes out 1013 */ 1014 void 1015 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx) 1016 { 1017 xoptattr_t *xoap; 1018 1019 xoap = xva_getxoptattr(xvap); 1020 ASSERT(xoap); 1021 1022 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 1023 uint64_t times[2]; 1024 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times); 1025 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs), 1026 ×, sizeof (times), tx); 1027 XVA_SET_RTN(xvap, XAT_CREATETIME); 1028 } 1029 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 1030 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly, 1031 zp->z_pflags, tx); 1032 XVA_SET_RTN(xvap, XAT_READONLY); 1033 } 1034 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 1035 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden, 1036 zp->z_pflags, tx); 1037 XVA_SET_RTN(xvap, XAT_HIDDEN); 1038 } 1039 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 1040 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system, 1041 zp->z_pflags, tx); 1042 XVA_SET_RTN(xvap, XAT_SYSTEM); 1043 } 1044 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 1045 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive, 1046 zp->z_pflags, tx); 1047 XVA_SET_RTN(xvap, XAT_ARCHIVE); 1048 } 1049 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 1050 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable, 1051 zp->z_pflags, tx); 1052 XVA_SET_RTN(xvap, XAT_IMMUTABLE); 1053 } 1054 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 1055 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink, 1056 zp->z_pflags, tx); 1057 XVA_SET_RTN(xvap, XAT_NOUNLINK); 1058 } 1059 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 1060 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly, 1061 zp->z_pflags, tx); 1062 XVA_SET_RTN(xvap, XAT_APPENDONLY); 1063 } 1064 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 1065 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump, 1066 zp->z_pflags, tx); 1067 XVA_SET_RTN(xvap, XAT_NODUMP); 1068 } 1069 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 1070 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque, 1071 zp->z_pflags, tx); 1072 XVA_SET_RTN(xvap, XAT_OPAQUE); 1073 } 1074 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 1075 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED, 1076 xoap->xoa_av_quarantined, zp->z_pflags, tx); 1077 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 1078 } 1079 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 1080 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified, 1081 zp->z_pflags, tx); 1082 XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 1083 } 1084 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) { 1085 zfs_sa_set_scanstamp(zp, xvap, tx); 1086 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP); 1087 } 1088 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 1089 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse, 1090 zp->z_pflags, tx); 1091 XVA_SET_RTN(xvap, XAT_REPARSE); 1092 } 1093 } 1094 1095 int 1096 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp) 1097 { 1098 dmu_object_info_t doi; 1099 dmu_buf_t *db; 1100 znode_t *zp; 1101 int err; 1102 sa_handle_t *hdl; 1103 1104 *zpp = NULL; 1105 1106 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num); 1107 1108 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db); 1109 if (err) { 1110 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1111 return (err); 1112 } 1113 1114 dmu_object_info_from_db(db, &doi); 1115 if (doi.doi_bonus_type != DMU_OT_SA && 1116 (doi.doi_bonus_type != DMU_OT_ZNODE || 1117 (doi.doi_bonus_type == DMU_OT_ZNODE && 1118 doi.doi_bonus_size < sizeof (znode_phys_t)))) { 1119 sa_buf_rele(db, NULL); 1120 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1121 return (EINVAL); 1122 } 1123 1124 hdl = dmu_buf_get_user(db); 1125 if (hdl != NULL) { 1126 zp = sa_get_userdata(hdl); 1127 1128 1129 /* 1130 * Since "SA" does immediate eviction we 1131 * should never find a sa handle that doesn't 1132 * know about the znode. 1133 */ 1134 1135 ASSERT3P(zp, !=, NULL); 1136 1137 mutex_enter(&zp->z_lock); 1138 ASSERT3U(zp->z_id, ==, obj_num); 1139 if (zp->z_unlinked) { 1140 err = ENOENT; 1141 } else { 1142 VN_HOLD(ZTOV(zp)); 1143 *zpp = zp; 1144 err = 0; 1145 } 1146 sa_buf_rele(db, NULL); 1147 mutex_exit(&zp->z_lock); 1148 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1149 return (err); 1150 } 1151 1152 /* 1153 * Not found create new znode/vnode 1154 * but only if file exists. 1155 * 1156 * There is a small window where zfs_vget() could 1157 * find this object while a file create is still in 1158 * progress. This is checked for in zfs_znode_alloc() 1159 * 1160 * if zfs_znode_alloc() fails it will drop the hold on the 1161 * bonus buffer. 1162 */ 1163 zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size, 1164 doi.doi_bonus_type, NULL); 1165 if (zp == NULL) { 1166 err = ENOENT; 1167 } else { 1168 *zpp = zp; 1169 } 1170 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1171 return (err); 1172 } 1173 1174 int 1175 zfs_rezget(znode_t *zp) 1176 { 1177 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1178 dmu_object_info_t doi; 1179 dmu_buf_t *db; 1180 uint64_t obj_num = zp->z_id; 1181 uint64_t mode; 1182 uint64_t uid, gid; 1183 sa_bulk_attr_t bulk[8]; 1184 int err; 1185 int count = 0; 1186 uint64_t gen; 1187 1188 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num); 1189 1190 mutex_enter(&zp->z_acl_lock); 1191 if (zp->z_acl_cached) { 1192 zfs_acl_free(zp->z_acl_cached); 1193 zp->z_acl_cached = NULL; 1194 } 1195 1196 mutex_exit(&zp->z_acl_lock); 1197 ASSERT(zp->z_sa_hdl == NULL); 1198 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db); 1199 if (err) { 1200 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1201 return (err); 1202 } 1203 1204 dmu_object_info_from_db(db, &doi); 1205 if (doi.doi_bonus_type != DMU_OT_SA && 1206 (doi.doi_bonus_type != DMU_OT_ZNODE || 1207 (doi.doi_bonus_type == DMU_OT_ZNODE && 1208 doi.doi_bonus_size < sizeof (znode_phys_t)))) { 1209 sa_buf_rele(db, NULL); 1210 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1211 return (EINVAL); 1212 } 1213 1214 zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL); 1215 1216 /* reload cached values */ 1217 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, 1218 &gen, sizeof (gen)); 1219 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL, 1220 &zp->z_size, sizeof (zp->z_size)); 1221 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL, 1222 &zp->z_links, sizeof (zp->z_links)); 1223 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 1224 &zp->z_pflags, sizeof (zp->z_pflags)); 1225 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, 1226 &zp->z_atime, sizeof (zp->z_atime)); 1227 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, 1228 &uid, sizeof (uid)); 1229 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, 1230 &gid, sizeof (gid)); 1231 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, 1232 &mode, sizeof (mode)); 1233 1234 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) { 1235 zfs_znode_dmu_fini(zp); 1236 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1237 return (EIO); 1238 } 1239 1240 zp->z_mode = mode; 1241 1242 if (gen != zp->z_gen) { 1243 zfs_znode_dmu_fini(zp); 1244 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1245 return (EIO); 1246 } 1247 1248 zp->z_uid = zfs_fuid_map_id(zfsvfs, uid, CRED(), ZFS_OWNER); 1249 zp->z_gid = zfs_fuid_map_id(zfsvfs, gid, CRED(), ZFS_GROUP); 1250 zp->z_unlinked = (zp->z_links == 0); 1251 zp->z_blksz = doi.doi_data_block_size; 1252 1253 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1254 1255 return (0); 1256 } 1257 1258 void 1259 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx) 1260 { 1261 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1262 objset_t *os = zfsvfs->z_os; 1263 uint64_t obj = zp->z_id; 1264 uint64_t acl_obj = zfs_external_acl(zp); 1265 1266 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj); 1267 if (acl_obj) { 1268 VERIFY(!zp->z_is_sa); 1269 VERIFY(0 == dmu_object_free(os, acl_obj, tx)); 1270 } 1271 VERIFY(0 == dmu_object_free(os, obj, tx)); 1272 zfs_znode_dmu_fini(zp); 1273 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj); 1274 zfs_znode_free(zp); 1275 } 1276 1277 void 1278 zfs_zinactive(znode_t *zp) 1279 { 1280 vnode_t *vp = ZTOV(zp); 1281 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1282 uint64_t z_id = zp->z_id; 1283 1284 ASSERT(zp->z_sa_hdl); 1285 1286 /* 1287 * Don't allow a zfs_zget() while were trying to release this znode 1288 */ 1289 ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id); 1290 1291 mutex_enter(&zp->z_lock); 1292 mutex_enter(&vp->v_lock); 1293 vp->v_count--; 1294 if (vp->v_count > 0 || vn_has_cached_data(vp)) { 1295 /* 1296 * If the hold count is greater than zero, somebody has 1297 * obtained a new reference on this znode while we were 1298 * processing it here, so we are done. If we still have 1299 * mapped pages then we are also done, since we don't 1300 * want to inactivate the znode until the pages get pushed. 1301 * 1302 * XXX - if vn_has_cached_data(vp) is true, but count == 0, 1303 * this seems like it would leave the znode hanging with 1304 * no chance to go inactive... 1305 */ 1306 mutex_exit(&vp->v_lock); 1307 mutex_exit(&zp->z_lock); 1308 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 1309 return; 1310 } 1311 mutex_exit(&vp->v_lock); 1312 1313 /* 1314 * If this was the last reference to a file with no links, 1315 * remove the file from the file system. 1316 */ 1317 if (zp->z_unlinked) { 1318 mutex_exit(&zp->z_lock); 1319 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 1320 zfs_rmnode(zp); 1321 return; 1322 } 1323 1324 mutex_exit(&zp->z_lock); 1325 zfs_znode_dmu_fini(zp); 1326 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 1327 zfs_znode_free(zp); 1328 } 1329 1330 void 1331 zfs_znode_free(znode_t *zp) 1332 { 1333 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1334 1335 vn_invalid(ZTOV(zp)); 1336 1337 ASSERT(ZTOV(zp)->v_count == 0); 1338 1339 mutex_enter(&zfsvfs->z_znodes_lock); 1340 POINTER_INVALIDATE(&zp->z_zfsvfs); 1341 list_remove(&zfsvfs->z_all_znodes, zp); 1342 mutex_exit(&zfsvfs->z_znodes_lock); 1343 1344 if (zp->z_acl_cached) { 1345 zfs_acl_free(zp->z_acl_cached); 1346 zp->z_acl_cached = NULL; 1347 } 1348 1349 kmem_cache_free(znode_cache, zp); 1350 1351 VFS_RELE(zfsvfs->z_vfs); 1352 } 1353 1354 void 1355 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2], 1356 uint64_t ctime[2], boolean_t have_tx) 1357 { 1358 timestruc_t now; 1359 1360 gethrestime(&now); 1361 1362 if (have_tx) { /* will sa_bulk_update happen really soon? */ 1363 zp->z_atime_dirty = 0; 1364 zp->z_seq++; 1365 } else { 1366 zp->z_atime_dirty = 1; 1367 } 1368 1369 if (flag & AT_ATIME) { 1370 ZFS_TIME_ENCODE(&now, zp->z_atime); 1371 } 1372 1373 if (flag & AT_MTIME) { 1374 ZFS_TIME_ENCODE(&now, mtime); 1375 if (zp->z_zfsvfs->z_use_fuids) { 1376 zp->z_pflags |= (ZFS_ARCHIVE | 1377 ZFS_AV_MODIFIED); 1378 } 1379 } 1380 1381 if (flag & AT_CTIME) { 1382 ZFS_TIME_ENCODE(&now, ctime); 1383 if (zp->z_zfsvfs->z_use_fuids) 1384 zp->z_pflags |= ZFS_ARCHIVE; 1385 } 1386 } 1387 1388 /* 1389 * Grow the block size for a file. 1390 * 1391 * IN: zp - znode of file to free data in. 1392 * size - requested block size 1393 * tx - open transaction. 1394 * 1395 * NOTE: this function assumes that the znode is write locked. 1396 */ 1397 void 1398 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx) 1399 { 1400 int error; 1401 u_longlong_t dummy; 1402 1403 if (size <= zp->z_blksz) 1404 return; 1405 /* 1406 * If the file size is already greater than the current blocksize, 1407 * we will not grow. If there is more than one block in a file, 1408 * the blocksize cannot change. 1409 */ 1410 if (zp->z_blksz && zp->z_size > zp->z_blksz) 1411 return; 1412 1413 error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id, 1414 size, 0, tx); 1415 1416 if (error == ENOTSUP) 1417 return; 1418 ASSERT3U(error, ==, 0); 1419 1420 /* What blocksize did we actually get? */ 1421 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy); 1422 } 1423 1424 /* 1425 * This is a dummy interface used when pvn_vplist_dirty() should *not* 1426 * be calling back into the fs for a putpage(). E.g.: when truncating 1427 * a file, the pages being "thrown away* don't need to be written out. 1428 */ 1429 /* ARGSUSED */ 1430 static int 1431 zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp, 1432 int flags, cred_t *cr) 1433 { 1434 ASSERT(0); 1435 return (0); 1436 } 1437 1438 /* 1439 * Increase the file length 1440 * 1441 * IN: zp - znode of file to free data in. 1442 * end - new end-of-file 1443 * 1444 * RETURN: 0 if success 1445 * error code if failure 1446 */ 1447 static int 1448 zfs_extend(znode_t *zp, uint64_t end) 1449 { 1450 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1451 dmu_tx_t *tx; 1452 rl_t *rl; 1453 uint64_t newblksz; 1454 int error; 1455 1456 /* 1457 * We will change zp_size, lock the whole file. 1458 */ 1459 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER); 1460 1461 /* 1462 * Nothing to do if file already at desired length. 1463 */ 1464 if (end <= zp->z_size) { 1465 zfs_range_unlock(rl); 1466 return (0); 1467 } 1468 top: 1469 tx = dmu_tx_create(zfsvfs->z_os); 1470 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1471 zfs_sa_upgrade_txholds(tx, zp); 1472 if (end > zp->z_blksz && 1473 (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) { 1474 /* 1475 * We are growing the file past the current block size. 1476 */ 1477 if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) { 1478 ASSERT(!ISP2(zp->z_blksz)); 1479 newblksz = MIN(end, SPA_MAXBLOCKSIZE); 1480 } else { 1481 newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz); 1482 } 1483 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz); 1484 } else { 1485 newblksz = 0; 1486 } 1487 1488 error = dmu_tx_assign(tx, TXG_NOWAIT); 1489 if (error) { 1490 if (error == ERESTART) { 1491 dmu_tx_wait(tx); 1492 dmu_tx_abort(tx); 1493 goto top; 1494 } 1495 dmu_tx_abort(tx); 1496 zfs_range_unlock(rl); 1497 return (error); 1498 } 1499 1500 if (newblksz) 1501 zfs_grow_blocksize(zp, newblksz, tx); 1502 1503 zp->z_size = end; 1504 1505 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs), 1506 &zp->z_size, sizeof (zp->z_size), tx)); 1507 1508 zfs_range_unlock(rl); 1509 1510 dmu_tx_commit(tx); 1511 1512 return (0); 1513 } 1514 1515 /* 1516 * Free space in a file. 1517 * 1518 * IN: zp - znode of file to free data in. 1519 * off - start of section to free. 1520 * len - length of section to free. 1521 * 1522 * RETURN: 0 if success 1523 * error code if failure 1524 */ 1525 static int 1526 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len) 1527 { 1528 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1529 rl_t *rl; 1530 int error; 1531 1532 /* 1533 * Lock the range being freed. 1534 */ 1535 rl = zfs_range_lock(zp, off, len, RL_WRITER); 1536 1537 /* 1538 * Nothing to do if file already at desired length. 1539 */ 1540 if (off >= zp->z_size) { 1541 zfs_range_unlock(rl); 1542 return (0); 1543 } 1544 1545 if (off + len > zp->z_size) 1546 len = zp->z_size - off; 1547 1548 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len); 1549 1550 zfs_range_unlock(rl); 1551 1552 return (error); 1553 } 1554 1555 /* 1556 * Truncate a file 1557 * 1558 * IN: zp - znode of file to free data in. 1559 * end - new end-of-file. 1560 * 1561 * RETURN: 0 if success 1562 * error code if failure 1563 */ 1564 static int 1565 zfs_trunc(znode_t *zp, uint64_t end) 1566 { 1567 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1568 vnode_t *vp = ZTOV(zp); 1569 dmu_tx_t *tx; 1570 rl_t *rl; 1571 int error; 1572 1573 /* 1574 * We will change zp_size, lock the whole file. 1575 */ 1576 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER); 1577 1578 /* 1579 * Nothing to do if file already at desired length. 1580 */ 1581 if (end >= zp->z_size) { 1582 zfs_range_unlock(rl); 1583 return (0); 1584 } 1585 1586 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end, -1); 1587 if (error) { 1588 zfs_range_unlock(rl); 1589 return (error); 1590 } 1591 top: 1592 tx = dmu_tx_create(zfsvfs->z_os); 1593 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1594 zfs_sa_upgrade_txholds(tx, zp); 1595 error = dmu_tx_assign(tx, TXG_NOWAIT); 1596 if (error) { 1597 if (error == ERESTART) { 1598 dmu_tx_wait(tx); 1599 dmu_tx_abort(tx); 1600 goto top; 1601 } 1602 dmu_tx_abort(tx); 1603 zfs_range_unlock(rl); 1604 return (error); 1605 } 1606 1607 zp->z_size = end; 1608 1609 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs), 1610 &zp->z_size, sizeof (zp->z_size), tx)); 1611 1612 dmu_tx_commit(tx); 1613 1614 /* 1615 * Clear any mapped pages in the truncated region. This has to 1616 * happen outside of the transaction to avoid the possibility of 1617 * a deadlock with someone trying to push a page that we are 1618 * about to invalidate. 1619 */ 1620 if (vn_has_cached_data(vp)) { 1621 page_t *pp; 1622 uint64_t start = end & PAGEMASK; 1623 int poff = end & PAGEOFFSET; 1624 1625 if (poff != 0 && (pp = page_lookup(vp, start, SE_SHARED))) { 1626 /* 1627 * We need to zero a partial page. 1628 */ 1629 pagezero(pp, poff, PAGESIZE - poff); 1630 start += PAGESIZE; 1631 page_unlock(pp); 1632 } 1633 error = pvn_vplist_dirty(vp, start, zfs_no_putpage, 1634 B_INVAL | B_TRUNC, NULL); 1635 ASSERT(error == 0); 1636 } 1637 1638 zfs_range_unlock(rl); 1639 1640 return (0); 1641 } 1642 1643 /* 1644 * Free space in a file 1645 * 1646 * IN: zp - znode of file to free data in. 1647 * off - start of range 1648 * len - end of range (0 => EOF) 1649 * flag - current file open mode flags. 1650 * log - TRUE if this action should be logged 1651 * 1652 * RETURN: 0 if success 1653 * error code if failure 1654 */ 1655 int 1656 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log) 1657 { 1658 vnode_t *vp = ZTOV(zp); 1659 dmu_tx_t *tx; 1660 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1661 zilog_t *zilog = zfsvfs->z_log; 1662 uint64_t mode; 1663 uint64_t mtime[2], ctime[2]; 1664 sa_bulk_attr_t bulk[3]; 1665 int count = 0; 1666 int error; 1667 1668 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode, 1669 sizeof (mode))) != 0) 1670 return (error); 1671 1672 if (off > zp->z_size) { 1673 error = zfs_extend(zp, off+len); 1674 if (error == 0 && log) 1675 goto log; 1676 else 1677 return (error); 1678 } 1679 1680 /* 1681 * Check for any locks in the region to be freed. 1682 */ 1683 1684 if (MANDLOCK(vp, (mode_t)mode)) { 1685 uint64_t length = (len ? len : zp->z_size - off); 1686 if (error = chklock(vp, FWRITE, off, length, flag, NULL)) 1687 return (error); 1688 } 1689 1690 if (len == 0) { 1691 error = zfs_trunc(zp, off); 1692 } else { 1693 if ((error = zfs_free_range(zp, off, len)) == 0 && 1694 off + len > zp->z_size) 1695 error = zfs_extend(zp, off+len); 1696 } 1697 if (error || !log) 1698 return (error); 1699 log: 1700 tx = dmu_tx_create(zfsvfs->z_os); 1701 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1702 zfs_sa_upgrade_txholds(tx, zp); 1703 error = dmu_tx_assign(tx, TXG_NOWAIT); 1704 if (error) { 1705 if (error == ERESTART) { 1706 dmu_tx_wait(tx); 1707 dmu_tx_abort(tx); 1708 goto log; 1709 } 1710 dmu_tx_abort(tx); 1711 return (error); 1712 } 1713 1714 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16); 1715 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16); 1716 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), 1717 NULL, &zp->z_pflags, 8); 1718 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE); 1719 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 1720 ASSERT(error == 0); 1721 1722 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len); 1723 1724 dmu_tx_commit(tx); 1725 return (0); 1726 } 1727 1728 void 1729 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx) 1730 { 1731 zfsvfs_t zfsvfs; 1732 uint64_t moid, obj, sa_obj, version; 1733 uint64_t sense = ZFS_CASE_SENSITIVE; 1734 uint64_t norm = 0; 1735 nvpair_t *elem; 1736 int error; 1737 int i; 1738 znode_t *rootzp = NULL; 1739 vnode_t *vp; 1740 vattr_t vattr; 1741 znode_t *zp; 1742 zfs_acl_ids_t acl_ids; 1743 1744 /* 1745 * First attempt to create master node. 1746 */ 1747 /* 1748 * In an empty objset, there are no blocks to read and thus 1749 * there can be no i/o errors (which we assert below). 1750 */ 1751 moid = MASTER_NODE_OBJ; 1752 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE, 1753 DMU_OT_NONE, 0, tx); 1754 ASSERT(error == 0); 1755 1756 /* 1757 * Set starting attributes. 1758 */ 1759 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os))); 1760 elem = NULL; 1761 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) { 1762 /* For the moment we expect all zpl props to be uint64_ts */ 1763 uint64_t val; 1764 char *name; 1765 1766 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64); 1767 VERIFY(nvpair_value_uint64(elem, &val) == 0); 1768 name = nvpair_name(elem); 1769 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) { 1770 if (val < version) 1771 version = val; 1772 } else { 1773 error = zap_update(os, moid, name, 8, 1, &val, tx); 1774 } 1775 ASSERT(error == 0); 1776 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0) 1777 norm = val; 1778 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0) 1779 sense = val; 1780 } 1781 ASSERT(version != 0); 1782 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx); 1783 1784 /* 1785 * Create zap object used for SA attribute registration 1786 */ 1787 1788 if (version >= ZPL_VERSION_SA) { 1789 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE, 1790 DMU_OT_NONE, 0, tx); 1791 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx); 1792 ASSERT(error == 0); 1793 } else { 1794 sa_obj = 0; 1795 } 1796 /* 1797 * Create a delete queue. 1798 */ 1799 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx); 1800 1801 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx); 1802 ASSERT(error == 0); 1803 1804 /* 1805 * Create root znode. Create minimal znode/vnode/zfsvfs 1806 * to allow zfs_mknode to work. 1807 */ 1808 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 1809 vattr.va_type = VDIR; 1810 vattr.va_mode = S_IFDIR|0755; 1811 vattr.va_uid = crgetuid(cr); 1812 vattr.va_gid = crgetgid(cr); 1813 1814 rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP); 1815 ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs)); 1816 rootzp->z_moved = 0; 1817 rootzp->z_unlinked = 0; 1818 rootzp->z_atime_dirty = 0; 1819 rootzp->z_is_sa = USE_SA(version, os); 1820 1821 vp = ZTOV(rootzp); 1822 vn_reinit(vp); 1823 vp->v_type = VDIR; 1824 1825 bzero(&zfsvfs, sizeof (zfsvfs_t)); 1826 1827 zfsvfs.z_os = os; 1828 zfsvfs.z_parent = &zfsvfs; 1829 zfsvfs.z_version = version; 1830 zfsvfs.z_use_fuids = USE_FUIDS(version, os); 1831 zfsvfs.z_use_sa = USE_SA(version, os); 1832 zfsvfs.z_norm = norm; 1833 1834 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END, 1835 &zfsvfs.z_attr_table); 1836 1837 ASSERT(error == 0); 1838 1839 /* 1840 * Fold case on file systems that are always or sometimes case 1841 * insensitive. 1842 */ 1843 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED) 1844 zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER; 1845 1846 mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL); 1847 list_create(&zfsvfs.z_all_znodes, sizeof (znode_t), 1848 offsetof(znode_t, z_link_node)); 1849 1850 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) 1851 mutex_init(&zfsvfs.z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL); 1852 1853 rootzp->z_zfsvfs = &zfsvfs; 1854 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr, 1855 cr, NULL, &acl_ids)); 1856 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids); 1857 ASSERT3P(zp, ==, rootzp); 1858 ASSERT(!vn_in_dnlc(ZTOV(rootzp))); /* not valid to move */ 1859 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx); 1860 ASSERT(error == 0); 1861 zfs_acl_ids_free(&acl_ids); 1862 POINTER_INVALIDATE(&rootzp->z_zfsvfs); 1863 1864 ZTOV(rootzp)->v_count = 0; 1865 sa_handle_destroy(rootzp->z_sa_hdl); 1866 kmem_cache_free(znode_cache, rootzp); 1867 1868 /* 1869 * Create shares directory 1870 */ 1871 1872 error = zfs_create_share_dir(&zfsvfs, tx); 1873 1874 ASSERT(error == 0); 1875 1876 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) 1877 mutex_destroy(&zfsvfs.z_hold_mtx[i]); 1878 } 1879 1880 #endif /* _KERNEL */ 1881 1882 /* 1883 * Given an object number, return its parent object number and whether 1884 * or not the object is an extended attribute directory. 1885 */ 1886 static int 1887 zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir, 1888 sa_attr_type_t *sa_table) 1889 { 1890 dmu_buf_t *db; 1891 dmu_object_info_t doi; 1892 int error; 1893 uint64_t parent; 1894 uint64_t pflags; 1895 uint64_t mode; 1896 sa_bulk_attr_t bulk[3]; 1897 sa_handle_t *hdl; 1898 int count = 0; 1899 1900 if ((error = sa_buf_hold(osp, obj, FTAG, &db)) != 0) 1901 return (error); 1902 1903 dmu_object_info_from_db(db, &doi); 1904 if ((doi.doi_bonus_type != DMU_OT_SA && 1905 doi.doi_bonus_type != DMU_OT_ZNODE) || 1906 doi.doi_bonus_type == DMU_OT_ZNODE && 1907 doi.doi_bonus_size < sizeof (znode_phys_t)) { 1908 sa_buf_rele(db, FTAG); 1909 return (EINVAL); 1910 } 1911 1912 if ((error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, 1913 &hdl)) != 0) { 1914 sa_buf_rele(db, FTAG); 1915 return (error); 1916 } 1917 1918 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], 1919 NULL, &parent, 8); 1920 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL, 1921 &pflags, 8); 1922 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL, 1923 &mode, 8); 1924 1925 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0) { 1926 sa_buf_rele(db, FTAG); 1927 sa_handle_destroy(hdl); 1928 return (error); 1929 } 1930 *pobjp = parent; 1931 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode); 1932 sa_handle_destroy(hdl); 1933 sa_buf_rele(db, FTAG); 1934 1935 return (0); 1936 } 1937 1938 int 1939 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len) 1940 { 1941 char *path = buf + len - 1; 1942 sa_attr_type_t *sa_table; 1943 int error; 1944 uint64_t sa_obj = 0; 1945 1946 *path = '\0'; 1947 1948 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj); 1949 1950 if (error != 0 && error != ENOENT) 1951 return (error); 1952 1953 if ((error = sa_setup(osp, sa_obj, zfs_attr_table, 1954 ZPL_END, &sa_table)) != 0) 1955 return (error); 1956 1957 for (;;) { 1958 uint64_t pobj; 1959 char component[MAXNAMELEN + 2]; 1960 size_t complen; 1961 int is_xattrdir; 1962 1963 if ((error = zfs_obj_to_pobj(osp, obj, &pobj, 1964 &is_xattrdir, sa_table)) != 0) 1965 break; 1966 1967 if (pobj == obj) { 1968 if (path[0] != '/') 1969 *--path = '/'; 1970 break; 1971 } 1972 1973 component[0] = '/'; 1974 if (is_xattrdir) { 1975 (void) sprintf(component + 1, "<xattrdir>"); 1976 } else { 1977 error = zap_value_search(osp, pobj, obj, 1978 ZFS_DIRENT_OBJ(-1ULL), component + 1); 1979 if (error != 0) 1980 break; 1981 } 1982 1983 complen = strlen(component); 1984 path -= complen; 1985 ASSERT(path >= buf); 1986 bcopy(component, path, complen); 1987 obj = pobj; 1988 } 1989 1990 if (error == 0) 1991 (void) memmove(buf, path, buf + len - path); 1992 1993 return (error); 1994 } 1995