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 https://opensource.org/licenses/CDDL-1.0. 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 * Copyright (c) 2012, 2014 by Delphix. All rights reserved. 24 * Copyright (c) 2014 Integros [integros.com] 25 */ 26 27 /* Portions Copyright 2007 Jeremy Teo */ 28 /* Portions Copyright 2011 Martin Matuska <mm@FreeBSD.org> */ 29 30 #ifdef _KERNEL 31 #include <sys/types.h> 32 #include <sys/param.h> 33 #include <sys/time.h> 34 #include <sys/systm.h> 35 #include <sys/sysmacros.h> 36 #include <sys/resource.h> 37 #include <sys/mntent.h> 38 #include <sys/u8_textprep.h> 39 #include <sys/dsl_dataset.h> 40 #include <sys/vfs.h> 41 #include <sys/vnode.h> 42 #include <sys/file.h> 43 #include <sys/kmem.h> 44 #include <sys/errno.h> 45 #include <sys/unistd.h> 46 #include <sys/atomic.h> 47 #include <sys/zfs_dir.h> 48 #include <sys/zfs_acl.h> 49 #include <sys/zfs_ioctl.h> 50 #include <sys/zfs_rlock.h> 51 #include <sys/zfs_fuid.h> 52 #include <sys/dnode.h> 53 #include <sys/fs/zfs.h> 54 #endif /* _KERNEL */ 55 56 #include <sys/dmu.h> 57 #include <sys/dmu_objset.h> 58 #include <sys/dmu_tx.h> 59 #include <sys/zfs_refcount.h> 60 #include <sys/stat.h> 61 #include <sys/zap.h> 62 #include <sys/zfs_znode.h> 63 #include <sys/sa.h> 64 #include <sys/zfs_sa.h> 65 #include <sys/zfs_stat.h> 66 67 #include "zfs_prop.h" 68 #include "zfs_comutil.h" 69 70 /* Used by fstat(1). */ 71 SYSCTL_INT(_debug_sizeof, OID_AUTO, znode, CTLFLAG_RD, 72 SYSCTL_NULL_INT_PTR, sizeof (znode_t), "sizeof(znode_t)"); 73 74 /* 75 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only 76 * turned on when DEBUG is also defined. 77 */ 78 #ifdef ZFS_DEBUG 79 #define ZNODE_STATS 80 #endif /* DEBUG */ 81 82 #ifdef ZNODE_STATS 83 #define ZNODE_STAT_ADD(stat) ((stat)++) 84 #else 85 #define ZNODE_STAT_ADD(stat) /* nothing */ 86 #endif /* ZNODE_STATS */ 87 88 /* 89 * Functions needed for userland (ie: libzpool) are not put under 90 * #ifdef_KERNEL; the rest of the functions have dependencies 91 * (such as VFS logic) that will not compile easily in userland. 92 */ 93 #ifdef _KERNEL 94 #if !defined(KMEM_DEBUG) && __FreeBSD_version >= 1300102 95 #define _ZFS_USE_SMR 96 static uma_zone_t znode_uma_zone; 97 #else 98 static kmem_cache_t *znode_cache = NULL; 99 #endif 100 101 extern struct vop_vector zfs_vnodeops; 102 extern struct vop_vector zfs_fifoops; 103 extern struct vop_vector zfs_shareops; 104 105 106 /* 107 * This callback is invoked when acquiring a RL_WRITER or RL_APPEND lock on 108 * z_rangelock. It will modify the offset and length of the lock to reflect 109 * znode-specific information, and convert RL_APPEND to RL_WRITER. This is 110 * called with the rangelock_t's rl_lock held, which avoids races. 111 */ 112 static void 113 zfs_rangelock_cb(zfs_locked_range_t *new, void *arg) 114 { 115 znode_t *zp = arg; 116 117 /* 118 * If in append mode, convert to writer and lock starting at the 119 * current end of file. 120 */ 121 if (new->lr_type == RL_APPEND) { 122 new->lr_offset = zp->z_size; 123 new->lr_type = RL_WRITER; 124 } 125 126 /* 127 * If we need to grow the block size then lock the whole file range. 128 */ 129 uint64_t end_size = MAX(zp->z_size, new->lr_offset + new->lr_length); 130 if (end_size > zp->z_blksz && (!ISP2(zp->z_blksz) || 131 zp->z_blksz < ZTOZSB(zp)->z_max_blksz)) { 132 new->lr_offset = 0; 133 new->lr_length = UINT64_MAX; 134 } 135 } 136 137 static int 138 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags) 139 { 140 znode_t *zp = buf; 141 142 POINTER_INVALIDATE(&zp->z_zfsvfs); 143 144 list_link_init(&zp->z_link_node); 145 146 mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL); 147 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL); 148 rw_init(&zp->z_xattr_lock, NULL, RW_DEFAULT, NULL); 149 150 zfs_rangelock_init(&zp->z_rangelock, zfs_rangelock_cb, zp); 151 152 zp->z_acl_cached = NULL; 153 zp->z_xattr_cached = NULL; 154 zp->z_xattr_parent = 0; 155 zp->z_vnode = NULL; 156 zp->z_sync_writes_cnt = 0; 157 zp->z_async_writes_cnt = 0; 158 159 return (0); 160 } 161 162 static void 163 zfs_znode_cache_destructor(void *buf, void *arg) 164 { 165 (void) arg; 166 znode_t *zp = buf; 167 168 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs)); 169 ASSERT3P(zp->z_vnode, ==, NULL); 170 ASSERT(!list_link_active(&zp->z_link_node)); 171 mutex_destroy(&zp->z_lock); 172 mutex_destroy(&zp->z_acl_lock); 173 rw_destroy(&zp->z_xattr_lock); 174 zfs_rangelock_fini(&zp->z_rangelock); 175 176 ASSERT3P(zp->z_acl_cached, ==, NULL); 177 ASSERT3P(zp->z_xattr_cached, ==, NULL); 178 179 ASSERT0(atomic_load_32(&zp->z_sync_writes_cnt)); 180 ASSERT0(atomic_load_32(&zp->z_async_writes_cnt)); 181 } 182 183 184 #ifdef _ZFS_USE_SMR 185 VFS_SMR_DECLARE; 186 187 static int 188 zfs_znode_cache_constructor_smr(void *mem, int size __unused, void *private, 189 int flags) 190 { 191 return (zfs_znode_cache_constructor(mem, private, flags)); 192 } 193 194 static void 195 zfs_znode_cache_destructor_smr(void *mem, int size __unused, void *private) 196 { 197 zfs_znode_cache_destructor(mem, private); 198 } 199 200 void 201 zfs_znode_init(void) 202 { 203 /* 204 * Initialize zcache 205 */ 206 ASSERT3P(znode_uma_zone, ==, NULL); 207 znode_uma_zone = uma_zcreate("zfs_znode_cache", 208 sizeof (znode_t), zfs_znode_cache_constructor_smr, 209 zfs_znode_cache_destructor_smr, NULL, NULL, 0, 0); 210 VFS_SMR_ZONE_SET(znode_uma_zone); 211 } 212 213 static znode_t * 214 zfs_znode_alloc_kmem(int flags) 215 { 216 return (uma_zalloc_smr(znode_uma_zone, flags)); 217 } 218 219 static void 220 zfs_znode_free_kmem(znode_t *zp) 221 { 222 if (zp->z_xattr_cached) { 223 nvlist_free(zp->z_xattr_cached); 224 zp->z_xattr_cached = NULL; 225 } 226 uma_zfree_smr(znode_uma_zone, zp); 227 } 228 #else 229 void 230 zfs_znode_init(void) 231 { 232 /* 233 * Initialize zcache 234 */ 235 ASSERT3P(znode_cache, ==, NULL); 236 znode_cache = kmem_cache_create("zfs_znode_cache", 237 sizeof (znode_t), 0, zfs_znode_cache_constructor, 238 zfs_znode_cache_destructor, NULL, NULL, NULL, 0); 239 } 240 241 static znode_t * 242 zfs_znode_alloc_kmem(int flags) 243 { 244 return (kmem_cache_alloc(znode_cache, flags)); 245 } 246 247 static void 248 zfs_znode_free_kmem(znode_t *zp) 249 { 250 if (zp->z_xattr_cached) { 251 nvlist_free(zp->z_xattr_cached); 252 zp->z_xattr_cached = NULL; 253 } 254 kmem_cache_free(znode_cache, zp); 255 } 256 #endif 257 258 void 259 zfs_znode_fini(void) 260 { 261 /* 262 * Cleanup zcache 263 */ 264 #ifdef _ZFS_USE_SMR 265 if (znode_uma_zone) { 266 uma_zdestroy(znode_uma_zone); 267 znode_uma_zone = NULL; 268 } 269 #else 270 if (znode_cache) { 271 kmem_cache_destroy(znode_cache); 272 znode_cache = NULL; 273 } 274 #endif 275 } 276 277 278 static int 279 zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx) 280 { 281 zfs_acl_ids_t acl_ids; 282 vattr_t vattr; 283 znode_t *sharezp; 284 znode_t *zp; 285 int error; 286 287 vattr.va_mask = AT_MODE|AT_UID|AT_GID; 288 vattr.va_type = VDIR; 289 vattr.va_mode = S_IFDIR|0555; 290 vattr.va_uid = crgetuid(kcred); 291 vattr.va_gid = crgetgid(kcred); 292 293 sharezp = zfs_znode_alloc_kmem(KM_SLEEP); 294 ASSERT(!POINTER_IS_VALID(sharezp->z_zfsvfs)); 295 sharezp->z_unlinked = 0; 296 sharezp->z_atime_dirty = 0; 297 sharezp->z_zfsvfs = zfsvfs; 298 sharezp->z_is_sa = zfsvfs->z_use_sa; 299 300 VERIFY0(zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr, 301 kcred, NULL, &acl_ids)); 302 zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids); 303 ASSERT3P(zp, ==, sharezp); 304 POINTER_INVALIDATE(&sharezp->z_zfsvfs); 305 error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ, 306 ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx); 307 zfsvfs->z_shares_dir = sharezp->z_id; 308 309 zfs_acl_ids_free(&acl_ids); 310 sa_handle_destroy(sharezp->z_sa_hdl); 311 zfs_znode_free_kmem(sharezp); 312 313 return (error); 314 } 315 316 /* 317 * define a couple of values we need available 318 * for both 64 and 32 bit environments. 319 */ 320 #ifndef NBITSMINOR64 321 #define NBITSMINOR64 32 322 #endif 323 #ifndef MAXMAJ64 324 #define MAXMAJ64 0xffffffffUL 325 #endif 326 #ifndef MAXMIN64 327 #define MAXMIN64 0xffffffffUL 328 #endif 329 330 /* 331 * Create special expldev for ZFS private use. 332 * Can't use standard expldev since it doesn't do 333 * what we want. The standard expldev() takes a 334 * dev32_t in LP64 and expands it to a long dev_t. 335 * We need an interface that takes a dev32_t in ILP32 336 * and expands it to a long dev_t. 337 */ 338 static uint64_t 339 zfs_expldev(dev_t dev) 340 { 341 return (((uint64_t)major(dev) << NBITSMINOR64) | minor(dev)); 342 } 343 /* 344 * Special cmpldev for ZFS private use. 345 * Can't use standard cmpldev since it takes 346 * a long dev_t and compresses it to dev32_t in 347 * LP64. We need to do a compaction of a long dev_t 348 * to a dev32_t in ILP32. 349 */ 350 dev_t 351 zfs_cmpldev(uint64_t dev) 352 { 353 return (makedev((dev >> NBITSMINOR64), (dev & MAXMIN64))); 354 } 355 356 static void 357 zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp, 358 dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl) 359 { 360 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs)); 361 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id))); 362 363 ASSERT3P(zp->z_sa_hdl, ==, NULL); 364 ASSERT3P(zp->z_acl_cached, ==, NULL); 365 if (sa_hdl == NULL) { 366 VERIFY0(sa_handle_get_from_db(zfsvfs->z_os, db, zp, 367 SA_HDL_SHARED, &zp->z_sa_hdl)); 368 } else { 369 zp->z_sa_hdl = sa_hdl; 370 sa_set_userp(sa_hdl, zp); 371 } 372 373 zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE; 374 375 /* 376 * Slap on VROOT if we are the root znode unless we are the root 377 * node of a snapshot mounted under .zfs. 378 */ 379 if (zp->z_id == zfsvfs->z_root && zfsvfs->z_parent == zfsvfs) 380 ZTOV(zp)->v_flag |= VROOT; 381 382 vn_exists(ZTOV(zp)); 383 } 384 385 void 386 zfs_znode_dmu_fini(znode_t *zp) 387 { 388 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) || 389 zp->z_unlinked || 390 ZFS_TEARDOWN_INACTIVE_WRITE_HELD(zp->z_zfsvfs)); 391 392 sa_handle_destroy(zp->z_sa_hdl); 393 zp->z_sa_hdl = NULL; 394 } 395 396 static void 397 zfs_vnode_forget(vnode_t *vp) 398 { 399 400 /* copied from insmntque_stddtr */ 401 vp->v_data = NULL; 402 vp->v_op = &dead_vnodeops; 403 vgone(vp); 404 vput(vp); 405 } 406 407 /* 408 * Construct a new znode/vnode and initialize. 409 * 410 * This does not do a call to dmu_set_user() that is 411 * up to the caller to do, in case you don't want to 412 * return the znode 413 */ 414 static znode_t * 415 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz, 416 dmu_object_type_t obj_type, sa_handle_t *hdl) 417 { 418 znode_t *zp; 419 vnode_t *vp; 420 uint64_t mode; 421 uint64_t parent; 422 #ifdef notyet 423 uint64_t mtime[2], ctime[2]; 424 #endif 425 uint64_t projid = ZFS_DEFAULT_PROJID; 426 sa_bulk_attr_t bulk[9]; 427 int count = 0; 428 int error; 429 430 zp = zfs_znode_alloc_kmem(KM_SLEEP); 431 432 #ifndef _ZFS_USE_SMR 433 KASSERT((zfsvfs->z_parent->z_vfs->mnt_kern_flag & MNTK_FPLOOKUP) == 0, 434 ("%s: fast path lookup enabled without smr", __func__)); 435 #endif 436 437 #if __FreeBSD_version >= 1300076 438 KASSERT(curthread->td_vp_reserved != NULL, 439 ("zfs_znode_alloc: getnewvnode without any vnodes reserved")); 440 #else 441 KASSERT(curthread->td_vp_reserv > 0, 442 ("zfs_znode_alloc: getnewvnode without any vnodes reserved")); 443 #endif 444 error = getnewvnode("zfs", zfsvfs->z_parent->z_vfs, &zfs_vnodeops, &vp); 445 if (error != 0) { 446 zfs_znode_free_kmem(zp); 447 return (NULL); 448 } 449 zp->z_vnode = vp; 450 vp->v_data = zp; 451 452 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs)); 453 454 zp->z_sa_hdl = NULL; 455 zp->z_unlinked = 0; 456 zp->z_atime_dirty = 0; 457 zp->z_mapcnt = 0; 458 zp->z_id = db->db_object; 459 zp->z_blksz = blksz; 460 zp->z_seq = 0x7A4653; 461 zp->z_sync_cnt = 0; 462 zp->z_sync_writes_cnt = 0; 463 zp->z_async_writes_cnt = 0; 464 #if __FreeBSD_version >= 1300139 465 atomic_store_ptr(&zp->z_cached_symlink, NULL); 466 #endif 467 468 vp = ZTOV(zp); 469 470 zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl); 471 472 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8); 473 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8); 474 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL, 475 &zp->z_size, 8); 476 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL, 477 &zp->z_links, 8); 478 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 479 &zp->z_pflags, 8); 480 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8); 481 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, 482 &zp->z_atime, 16); 483 #ifdef notyet 484 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 485 &mtime, 16); 486 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 487 &ctime, 16); 488 #endif 489 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, 490 &zp->z_uid, 8); 491 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, 492 &zp->z_gid, 8); 493 494 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0 || 495 (dmu_objset_projectquota_enabled(zfsvfs->z_os) && 496 (zp->z_pflags & ZFS_PROJID) && 497 sa_lookup(zp->z_sa_hdl, SA_ZPL_PROJID(zfsvfs), &projid, 8) != 0)) { 498 if (hdl == NULL) 499 sa_handle_destroy(zp->z_sa_hdl); 500 zfs_vnode_forget(vp); 501 zp->z_vnode = NULL; 502 zfs_znode_free_kmem(zp); 503 return (NULL); 504 } 505 506 zp->z_projid = projid; 507 zp->z_mode = mode; 508 509 /* Cache the xattr parent id */ 510 if (zp->z_pflags & ZFS_XATTR) 511 zp->z_xattr_parent = parent; 512 513 vp->v_type = IFTOVT((mode_t)mode); 514 515 switch (vp->v_type) { 516 case VDIR: 517 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */ 518 break; 519 case VFIFO: 520 vp->v_op = &zfs_fifoops; 521 break; 522 case VREG: 523 if (parent == zfsvfs->z_shares_dir) { 524 ASSERT0(zp->z_uid); 525 ASSERT0(zp->z_gid); 526 vp->v_op = &zfs_shareops; 527 } 528 break; 529 default: 530 break; 531 } 532 533 mutex_enter(&zfsvfs->z_znodes_lock); 534 list_insert_tail(&zfsvfs->z_all_znodes, zp); 535 zfsvfs->z_nr_znodes++; 536 zp->z_zfsvfs = zfsvfs; 537 mutex_exit(&zfsvfs->z_znodes_lock); 538 539 /* 540 * Acquire vnode lock before making it available to the world. 541 */ 542 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 543 VN_LOCK_AREC(vp); 544 if (vp->v_type != VFIFO) 545 VN_LOCK_ASHARE(vp); 546 547 return (zp); 548 } 549 550 static uint64_t empty_xattr; 551 static uint64_t pad[4]; 552 static zfs_acl_phys_t acl_phys; 553 /* 554 * Create a new DMU object to hold a zfs znode. 555 * 556 * IN: dzp - parent directory for new znode 557 * vap - file attributes for new znode 558 * tx - dmu transaction id for zap operations 559 * cr - credentials of caller 560 * flag - flags: 561 * IS_ROOT_NODE - new object will be root 562 * IS_XATTR - new object is an attribute 563 * bonuslen - length of bonus buffer 564 * setaclp - File/Dir initial ACL 565 * fuidp - Tracks fuid allocation. 566 * 567 * OUT: zpp - allocated znode 568 * 569 */ 570 void 571 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr, 572 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids) 573 { 574 uint64_t crtime[2], atime[2], mtime[2], ctime[2]; 575 uint64_t mode, size, links, parent, pflags; 576 uint64_t dzp_pflags = 0; 577 uint64_t rdev = 0; 578 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 579 dmu_buf_t *db; 580 timestruc_t now; 581 uint64_t gen, obj; 582 int bonuslen; 583 int dnodesize; 584 sa_handle_t *sa_hdl; 585 dmu_object_type_t obj_type; 586 sa_bulk_attr_t *sa_attrs; 587 int cnt = 0; 588 zfs_acl_locator_cb_t locate = { 0 }; 589 590 ASSERT3P(vap, !=, NULL); 591 ASSERT3U((vap->va_mask & AT_MODE), ==, AT_MODE); 592 593 if (zfsvfs->z_replay) { 594 obj = vap->va_nodeid; 595 now = vap->va_ctime; /* see zfs_replay_create() */ 596 gen = vap->va_nblocks; /* ditto */ 597 dnodesize = vap->va_fsid; /* ditto */ 598 } else { 599 obj = 0; 600 vfs_timestamp(&now); 601 gen = dmu_tx_get_txg(tx); 602 dnodesize = dmu_objset_dnodesize(zfsvfs->z_os); 603 } 604 605 if (dnodesize == 0) 606 dnodesize = DNODE_MIN_SIZE; 607 608 obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE; 609 bonuslen = (obj_type == DMU_OT_SA) ? 610 DN_BONUS_SIZE(dnodesize) : ZFS_OLD_ZNODE_PHYS_SIZE; 611 612 /* 613 * Create a new DMU object. 614 */ 615 /* 616 * There's currently no mechanism for pre-reading the blocks that will 617 * be needed to allocate a new object, so we accept the small chance 618 * that there will be an i/o error and we will fail one of the 619 * assertions below. 620 */ 621 if (vap->va_type == VDIR) { 622 if (zfsvfs->z_replay) { 623 VERIFY0(zap_create_claim_norm_dnsize(zfsvfs->z_os, obj, 624 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS, 625 obj_type, bonuslen, dnodesize, tx)); 626 } else { 627 obj = zap_create_norm_dnsize(zfsvfs->z_os, 628 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS, 629 obj_type, bonuslen, dnodesize, tx); 630 } 631 } else { 632 if (zfsvfs->z_replay) { 633 VERIFY0(dmu_object_claim_dnsize(zfsvfs->z_os, obj, 634 DMU_OT_PLAIN_FILE_CONTENTS, 0, 635 obj_type, bonuslen, dnodesize, tx)); 636 } else { 637 obj = dmu_object_alloc_dnsize(zfsvfs->z_os, 638 DMU_OT_PLAIN_FILE_CONTENTS, 0, 639 obj_type, bonuslen, dnodesize, tx); 640 } 641 } 642 643 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj); 644 VERIFY0(sa_buf_hold(zfsvfs->z_os, obj, NULL, &db)); 645 646 /* 647 * If this is the root, fix up the half-initialized parent pointer 648 * to reference the just-allocated physical data area. 649 */ 650 if (flag & IS_ROOT_NODE) { 651 dzp->z_id = obj; 652 } else { 653 dzp_pflags = dzp->z_pflags; 654 } 655 656 /* 657 * If parent is an xattr, so am I. 658 */ 659 if (dzp_pflags & ZFS_XATTR) { 660 flag |= IS_XATTR; 661 } 662 663 if (zfsvfs->z_use_fuids) 664 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED; 665 else 666 pflags = 0; 667 668 if (vap->va_type == VDIR) { 669 size = 2; /* contents ("." and "..") */ 670 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1; 671 } else { 672 size = links = 0; 673 } 674 675 if (vap->va_type == VBLK || vap->va_type == VCHR) { 676 rdev = zfs_expldev(vap->va_rdev); 677 } 678 679 parent = dzp->z_id; 680 mode = acl_ids->z_mode; 681 if (flag & IS_XATTR) 682 pflags |= ZFS_XATTR; 683 684 /* 685 * No execs denied will be determined when zfs_mode_compute() is called. 686 */ 687 pflags |= acl_ids->z_aclp->z_hints & 688 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT| 689 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED); 690 691 ZFS_TIME_ENCODE(&now, crtime); 692 ZFS_TIME_ENCODE(&now, ctime); 693 694 if (vap->va_mask & AT_ATIME) { 695 ZFS_TIME_ENCODE(&vap->va_atime, atime); 696 } else { 697 ZFS_TIME_ENCODE(&now, atime); 698 } 699 700 if (vap->va_mask & AT_MTIME) { 701 ZFS_TIME_ENCODE(&vap->va_mtime, mtime); 702 } else { 703 ZFS_TIME_ENCODE(&now, mtime); 704 } 705 706 /* Now add in all of the "SA" attributes */ 707 VERIFY0(sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED, 708 &sa_hdl)); 709 710 /* 711 * Setup the array of attributes to be replaced/set on the new file 712 * 713 * order for DMU_OT_ZNODE is critical since it needs to be constructed 714 * in the old znode_phys_t format. Don't change this ordering 715 */ 716 sa_attrs = kmem_alloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP); 717 718 if (obj_type == DMU_OT_ZNODE) { 719 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs), 720 NULL, &atime, 16); 721 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs), 722 NULL, &mtime, 16); 723 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs), 724 NULL, &ctime, 16); 725 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs), 726 NULL, &crtime, 16); 727 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs), 728 NULL, &gen, 8); 729 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs), 730 NULL, &mode, 8); 731 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs), 732 NULL, &size, 8); 733 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs), 734 NULL, &parent, 8); 735 } else { 736 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs), 737 NULL, &mode, 8); 738 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs), 739 NULL, &size, 8); 740 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs), 741 NULL, &gen, 8); 742 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), 743 NULL, &acl_ids->z_fuid, 8); 744 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), 745 NULL, &acl_ids->z_fgid, 8); 746 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs), 747 NULL, &parent, 8); 748 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs), 749 NULL, &pflags, 8); 750 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs), 751 NULL, &atime, 16); 752 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs), 753 NULL, &mtime, 16); 754 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs), 755 NULL, &ctime, 16); 756 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs), 757 NULL, &crtime, 16); 758 } 759 760 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8); 761 762 if (obj_type == DMU_OT_ZNODE) { 763 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL, 764 &empty_xattr, 8); 765 } 766 if (obj_type == DMU_OT_ZNODE || 767 (vap->va_type == VBLK || vap->va_type == VCHR)) { 768 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs), 769 NULL, &rdev, 8); 770 771 } 772 if (obj_type == DMU_OT_ZNODE) { 773 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs), 774 NULL, &pflags, 8); 775 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL, 776 &acl_ids->z_fuid, 8); 777 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL, 778 &acl_ids->z_fgid, 8); 779 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad, 780 sizeof (uint64_t) * 4); 781 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL, 782 &acl_phys, sizeof (zfs_acl_phys_t)); 783 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) { 784 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL, 785 &acl_ids->z_aclp->z_acl_count, 8); 786 locate.cb_aclp = acl_ids->z_aclp; 787 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs), 788 zfs_acl_data_locator, &locate, 789 acl_ids->z_aclp->z_acl_bytes); 790 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags, 791 acl_ids->z_fuid, acl_ids->z_fgid); 792 } 793 794 VERIFY0(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx)); 795 796 if (!(flag & IS_ROOT_NODE)) { 797 *zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl); 798 ASSERT3P(*zpp, !=, NULL); 799 } else { 800 /* 801 * If we are creating the root node, the "parent" we 802 * passed in is the znode for the root. 803 */ 804 *zpp = dzp; 805 806 (*zpp)->z_sa_hdl = sa_hdl; 807 } 808 809 (*zpp)->z_pflags = pflags; 810 (*zpp)->z_mode = mode; 811 (*zpp)->z_dnodesize = dnodesize; 812 813 if (vap->va_mask & AT_XVATTR) 814 zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx); 815 816 if (obj_type == DMU_OT_ZNODE || 817 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) { 818 VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx)); 819 } 820 if (!(flag & IS_ROOT_NODE)) { 821 vnode_t *vp = ZTOV(*zpp); 822 vp->v_vflag |= VV_FORCEINSMQ; 823 int err = insmntque(vp, zfsvfs->z_vfs); 824 vp->v_vflag &= ~VV_FORCEINSMQ; 825 (void) err; 826 KASSERT(err == 0, ("insmntque() failed: error %d", err)); 827 } 828 kmem_free(sa_attrs, sizeof (sa_bulk_attr_t) * ZPL_END); 829 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj); 830 } 831 832 /* 833 * Update in-core attributes. It is assumed the caller will be doing an 834 * sa_bulk_update to push the changes out. 835 */ 836 void 837 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx) 838 { 839 xoptattr_t *xoap; 840 841 xoap = xva_getxoptattr(xvap); 842 ASSERT3P(xoap, !=, NULL); 843 844 if (zp->z_zfsvfs->z_replay == B_FALSE) { 845 ASSERT_VOP_IN_SEQC(ZTOV(zp)); 846 } 847 848 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 849 uint64_t times[2]; 850 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times); 851 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs), 852 ×, sizeof (times), tx); 853 XVA_SET_RTN(xvap, XAT_CREATETIME); 854 } 855 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 856 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly, 857 zp->z_pflags, tx); 858 XVA_SET_RTN(xvap, XAT_READONLY); 859 } 860 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 861 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden, 862 zp->z_pflags, tx); 863 XVA_SET_RTN(xvap, XAT_HIDDEN); 864 } 865 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 866 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system, 867 zp->z_pflags, tx); 868 XVA_SET_RTN(xvap, XAT_SYSTEM); 869 } 870 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 871 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive, 872 zp->z_pflags, tx); 873 XVA_SET_RTN(xvap, XAT_ARCHIVE); 874 } 875 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 876 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable, 877 zp->z_pflags, tx); 878 XVA_SET_RTN(xvap, XAT_IMMUTABLE); 879 } 880 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 881 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink, 882 zp->z_pflags, tx); 883 XVA_SET_RTN(xvap, XAT_NOUNLINK); 884 } 885 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 886 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly, 887 zp->z_pflags, tx); 888 XVA_SET_RTN(xvap, XAT_APPENDONLY); 889 } 890 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 891 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump, 892 zp->z_pflags, tx); 893 XVA_SET_RTN(xvap, XAT_NODUMP); 894 } 895 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 896 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque, 897 zp->z_pflags, tx); 898 XVA_SET_RTN(xvap, XAT_OPAQUE); 899 } 900 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 901 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED, 902 xoap->xoa_av_quarantined, zp->z_pflags, tx); 903 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 904 } 905 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 906 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified, 907 zp->z_pflags, tx); 908 XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 909 } 910 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) { 911 zfs_sa_set_scanstamp(zp, xvap, tx); 912 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP); 913 } 914 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 915 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse, 916 zp->z_pflags, tx); 917 XVA_SET_RTN(xvap, XAT_REPARSE); 918 } 919 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) { 920 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline, 921 zp->z_pflags, tx); 922 XVA_SET_RTN(xvap, XAT_OFFLINE); 923 } 924 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) { 925 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse, 926 zp->z_pflags, tx); 927 XVA_SET_RTN(xvap, XAT_SPARSE); 928 } 929 } 930 931 int 932 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp) 933 { 934 dmu_object_info_t doi; 935 dmu_buf_t *db; 936 znode_t *zp; 937 vnode_t *vp; 938 sa_handle_t *hdl; 939 int locked; 940 int err; 941 942 getnewvnode_reserve_(); 943 again: 944 *zpp = NULL; 945 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num); 946 947 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db); 948 if (err) { 949 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 950 getnewvnode_drop_reserve(); 951 return (err); 952 } 953 954 dmu_object_info_from_db(db, &doi); 955 if (doi.doi_bonus_type != DMU_OT_SA && 956 (doi.doi_bonus_type != DMU_OT_ZNODE || 957 (doi.doi_bonus_type == DMU_OT_ZNODE && 958 doi.doi_bonus_size < sizeof (znode_phys_t)))) { 959 sa_buf_rele(db, NULL); 960 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 961 getnewvnode_drop_reserve(); 962 return (SET_ERROR(EINVAL)); 963 } 964 965 hdl = dmu_buf_get_user(db); 966 if (hdl != NULL) { 967 zp = sa_get_userdata(hdl); 968 969 /* 970 * Since "SA" does immediate eviction we 971 * should never find a sa handle that doesn't 972 * know about the znode. 973 */ 974 ASSERT3P(zp, !=, NULL); 975 ASSERT3U(zp->z_id, ==, obj_num); 976 if (zp->z_unlinked) { 977 err = SET_ERROR(ENOENT); 978 } else { 979 vp = ZTOV(zp); 980 /* 981 * Don't let the vnode disappear after 982 * ZFS_OBJ_HOLD_EXIT. 983 */ 984 VN_HOLD(vp); 985 *zpp = zp; 986 err = 0; 987 } 988 989 sa_buf_rele(db, NULL); 990 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 991 992 if (err) { 993 getnewvnode_drop_reserve(); 994 return (err); 995 } 996 997 locked = VOP_ISLOCKED(vp); 998 VI_LOCK(vp); 999 if (VN_IS_DOOMED(vp) && locked != LK_EXCLUSIVE) { 1000 /* 1001 * The vnode is doomed and this thread doesn't 1002 * hold the exclusive lock on it, so the vnode 1003 * must be being reclaimed by another thread. 1004 * Otherwise the doomed vnode is being reclaimed 1005 * by this thread and zfs_zget is called from 1006 * ZIL internals. 1007 */ 1008 VI_UNLOCK(vp); 1009 1010 /* 1011 * XXX vrele() locks the vnode when the last reference 1012 * is dropped. Although in this case the vnode is 1013 * doomed / dead and so no inactivation is required, 1014 * the vnode lock is still acquired. That could result 1015 * in a LOR with z_teardown_lock if another thread holds 1016 * the vnode's lock and tries to take z_teardown_lock. 1017 * But that is only possible if the other thread peforms 1018 * a ZFS vnode operation on the vnode. That either 1019 * should not happen if the vnode is dead or the thread 1020 * should also have a reference to the vnode and thus 1021 * our reference is not last. 1022 */ 1023 VN_RELE(vp); 1024 goto again; 1025 } 1026 VI_UNLOCK(vp); 1027 getnewvnode_drop_reserve(); 1028 return (err); 1029 } 1030 1031 /* 1032 * Not found create new znode/vnode 1033 * but only if file exists. 1034 * 1035 * There is a small window where zfs_vget() could 1036 * find this object while a file create is still in 1037 * progress. This is checked for in zfs_znode_alloc() 1038 * 1039 * if zfs_znode_alloc() fails it will drop the hold on the 1040 * bonus buffer. 1041 */ 1042 zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size, 1043 doi.doi_bonus_type, NULL); 1044 if (zp == NULL) { 1045 err = SET_ERROR(ENOENT); 1046 } else { 1047 *zpp = zp; 1048 } 1049 if (err == 0) { 1050 vnode_t *vp = ZTOV(zp); 1051 1052 err = insmntque(vp, zfsvfs->z_vfs); 1053 if (err == 0) { 1054 vp->v_hash = obj_num; 1055 VOP_UNLOCK1(vp); 1056 } else { 1057 zp->z_vnode = NULL; 1058 zfs_znode_dmu_fini(zp); 1059 zfs_znode_free(zp); 1060 *zpp = NULL; 1061 } 1062 } 1063 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1064 getnewvnode_drop_reserve(); 1065 return (err); 1066 } 1067 1068 int 1069 zfs_rezget(znode_t *zp) 1070 { 1071 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1072 dmu_object_info_t doi; 1073 dmu_buf_t *db; 1074 vnode_t *vp; 1075 uint64_t obj_num = zp->z_id; 1076 uint64_t mode, size; 1077 sa_bulk_attr_t bulk[8]; 1078 int err; 1079 int count = 0; 1080 uint64_t gen; 1081 1082 /* 1083 * Remove cached pages before reloading the znode, so that they are not 1084 * lingering after we run into any error. Ideally, we should vgone() 1085 * the vnode in case of error, but currently we cannot do that 1086 * because of the LOR between the vnode lock and z_teardown_lock. 1087 * So, instead, we have to "doom" the znode in the illumos style. 1088 * 1089 * Ignore invalid pages during the scan. This is to avoid deadlocks 1090 * between page busying and the teardown lock, as pages are busied prior 1091 * to a VOP_GETPAGES operation, which acquires the teardown read lock. 1092 * Such pages will be invalid and can safely be skipped here. 1093 */ 1094 vp = ZTOV(zp); 1095 #if __FreeBSD_version >= 1400042 1096 vn_pages_remove_valid(vp, 0, 0); 1097 #else 1098 vn_pages_remove(vp, 0, 0); 1099 #endif 1100 1101 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num); 1102 1103 mutex_enter(&zp->z_acl_lock); 1104 if (zp->z_acl_cached) { 1105 zfs_acl_free(zp->z_acl_cached); 1106 zp->z_acl_cached = NULL; 1107 } 1108 mutex_exit(&zp->z_acl_lock); 1109 1110 rw_enter(&zp->z_xattr_lock, RW_WRITER); 1111 if (zp->z_xattr_cached) { 1112 nvlist_free(zp->z_xattr_cached); 1113 zp->z_xattr_cached = NULL; 1114 } 1115 rw_exit(&zp->z_xattr_lock); 1116 1117 ASSERT3P(zp->z_sa_hdl, ==, NULL); 1118 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db); 1119 if (err) { 1120 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1121 return (err); 1122 } 1123 1124 dmu_object_info_from_db(db, &doi); 1125 if (doi.doi_bonus_type != DMU_OT_SA && 1126 (doi.doi_bonus_type != DMU_OT_ZNODE || 1127 (doi.doi_bonus_type == DMU_OT_ZNODE && 1128 doi.doi_bonus_size < sizeof (znode_phys_t)))) { 1129 sa_buf_rele(db, NULL); 1130 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1131 return (SET_ERROR(EINVAL)); 1132 } 1133 1134 zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL); 1135 size = zp->z_size; 1136 1137 /* reload cached values */ 1138 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, 1139 &gen, sizeof (gen)); 1140 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL, 1141 &zp->z_size, sizeof (zp->z_size)); 1142 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL, 1143 &zp->z_links, sizeof (zp->z_links)); 1144 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 1145 &zp->z_pflags, sizeof (zp->z_pflags)); 1146 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, 1147 &zp->z_atime, sizeof (zp->z_atime)); 1148 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, 1149 &zp->z_uid, sizeof (zp->z_uid)); 1150 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, 1151 &zp->z_gid, sizeof (zp->z_gid)); 1152 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, 1153 &mode, sizeof (mode)); 1154 1155 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) { 1156 zfs_znode_dmu_fini(zp); 1157 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1158 return (SET_ERROR(EIO)); 1159 } 1160 1161 zp->z_mode = mode; 1162 1163 if (gen != zp->z_gen) { 1164 zfs_znode_dmu_fini(zp); 1165 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1166 return (SET_ERROR(EIO)); 1167 } 1168 1169 /* 1170 * It is highly improbable but still quite possible that two 1171 * objects in different datasets are created with the same 1172 * object numbers and in transaction groups with the same 1173 * numbers. znodes corresponding to those objects would 1174 * have the same z_id and z_gen, but their other attributes 1175 * may be different. 1176 * zfs recv -F may replace one of such objects with the other. 1177 * As a result file properties recorded in the replaced 1178 * object's vnode may no longer match the received object's 1179 * properties. At present the only cached property is the 1180 * files type recorded in v_type. 1181 * So, handle this case by leaving the old vnode and znode 1182 * disassociated from the actual object. A new vnode and a 1183 * znode will be created if the object is accessed 1184 * (e.g. via a look-up). The old vnode and znode will be 1185 * recycled when the last vnode reference is dropped. 1186 */ 1187 if (vp->v_type != IFTOVT((mode_t)zp->z_mode)) { 1188 zfs_znode_dmu_fini(zp); 1189 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1190 return (SET_ERROR(EIO)); 1191 } 1192 1193 /* 1194 * If the file has zero links, then it has been unlinked on the send 1195 * side and it must be in the received unlinked set. 1196 * We call zfs_znode_dmu_fini() now to prevent any accesses to the 1197 * stale data and to prevent automatically removal of the file in 1198 * zfs_zinactive(). The file will be removed either when it is removed 1199 * on the send side and the next incremental stream is received or 1200 * when the unlinked set gets processed. 1201 */ 1202 zp->z_unlinked = (zp->z_links == 0); 1203 if (zp->z_unlinked) { 1204 zfs_znode_dmu_fini(zp); 1205 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1206 return (0); 1207 } 1208 1209 zp->z_blksz = doi.doi_data_block_size; 1210 if (zp->z_size != size) 1211 vnode_pager_setsize(vp, zp->z_size); 1212 1213 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1214 1215 return (0); 1216 } 1217 1218 void 1219 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx) 1220 { 1221 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1222 objset_t *os = zfsvfs->z_os; 1223 uint64_t obj = zp->z_id; 1224 uint64_t acl_obj = zfs_external_acl(zp); 1225 1226 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj); 1227 if (acl_obj) { 1228 VERIFY(!zp->z_is_sa); 1229 VERIFY0(dmu_object_free(os, acl_obj, tx)); 1230 } 1231 VERIFY0(dmu_object_free(os, obj, tx)); 1232 zfs_znode_dmu_fini(zp); 1233 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj); 1234 zfs_znode_free(zp); 1235 } 1236 1237 void 1238 zfs_zinactive(znode_t *zp) 1239 { 1240 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1241 uint64_t z_id = zp->z_id; 1242 1243 ASSERT3P(zp->z_sa_hdl, !=, NULL); 1244 1245 /* 1246 * Don't allow a zfs_zget() while were trying to release this znode 1247 */ 1248 ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id); 1249 1250 /* 1251 * If this was the last reference to a file with no links, remove 1252 * the file from the file system unless the file system is mounted 1253 * read-only. That can happen, for example, if the file system was 1254 * originally read-write, the file was opened, then unlinked and 1255 * the file system was made read-only before the file was finally 1256 * closed. The file will remain in the unlinked set. 1257 */ 1258 if (zp->z_unlinked) { 1259 ASSERT(!zfsvfs->z_issnap); 1260 if ((zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) == 0) { 1261 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 1262 zfs_rmnode(zp); 1263 return; 1264 } 1265 } 1266 1267 zfs_znode_dmu_fini(zp); 1268 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 1269 zfs_znode_free(zp); 1270 } 1271 1272 void 1273 zfs_znode_free(znode_t *zp) 1274 { 1275 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1276 #if __FreeBSD_version >= 1300139 1277 char *symlink; 1278 #endif 1279 1280 ASSERT3P(zp->z_sa_hdl, ==, NULL); 1281 zp->z_vnode = NULL; 1282 mutex_enter(&zfsvfs->z_znodes_lock); 1283 POINTER_INVALIDATE(&zp->z_zfsvfs); 1284 list_remove(&zfsvfs->z_all_znodes, zp); 1285 zfsvfs->z_nr_znodes--; 1286 mutex_exit(&zfsvfs->z_znodes_lock); 1287 1288 #if __FreeBSD_version >= 1300139 1289 symlink = atomic_load_ptr(&zp->z_cached_symlink); 1290 if (symlink != NULL) { 1291 atomic_store_rel_ptr((uintptr_t *)&zp->z_cached_symlink, 1292 (uintptr_t)NULL); 1293 cache_symlink_free(symlink, strlen(symlink) + 1); 1294 } 1295 #endif 1296 1297 if (zp->z_acl_cached) { 1298 zfs_acl_free(zp->z_acl_cached); 1299 zp->z_acl_cached = NULL; 1300 } 1301 1302 zfs_znode_free_kmem(zp); 1303 } 1304 1305 void 1306 zfs_tstamp_update_setup_ext(znode_t *zp, uint_t flag, uint64_t mtime[2], 1307 uint64_t ctime[2], boolean_t have_tx) 1308 { 1309 timestruc_t now; 1310 1311 vfs_timestamp(&now); 1312 1313 if (have_tx) { /* will sa_bulk_update happen really soon? */ 1314 zp->z_atime_dirty = 0; 1315 zp->z_seq++; 1316 } else { 1317 zp->z_atime_dirty = 1; 1318 } 1319 1320 if (flag & AT_ATIME) { 1321 ZFS_TIME_ENCODE(&now, zp->z_atime); 1322 } 1323 1324 if (flag & AT_MTIME) { 1325 ZFS_TIME_ENCODE(&now, mtime); 1326 if (zp->z_zfsvfs->z_use_fuids) { 1327 zp->z_pflags |= (ZFS_ARCHIVE | 1328 ZFS_AV_MODIFIED); 1329 } 1330 } 1331 1332 if (flag & AT_CTIME) { 1333 ZFS_TIME_ENCODE(&now, ctime); 1334 if (zp->z_zfsvfs->z_use_fuids) 1335 zp->z_pflags |= ZFS_ARCHIVE; 1336 } 1337 } 1338 1339 1340 void 1341 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2], 1342 uint64_t ctime[2]) 1343 { 1344 zfs_tstamp_update_setup_ext(zp, flag, mtime, ctime, B_TRUE); 1345 } 1346 /* 1347 * Grow the block size for a file. 1348 * 1349 * IN: zp - znode of file to free data in. 1350 * size - requested block size 1351 * tx - open transaction. 1352 * 1353 * NOTE: this function assumes that the znode is write locked. 1354 */ 1355 void 1356 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx) 1357 { 1358 int error; 1359 u_longlong_t dummy; 1360 1361 if (size <= zp->z_blksz) 1362 return; 1363 /* 1364 * If the file size is already greater than the current blocksize, 1365 * we will not grow. If there is more than one block in a file, 1366 * the blocksize cannot change. 1367 */ 1368 if (zp->z_blksz && zp->z_size > zp->z_blksz) 1369 return; 1370 1371 error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id, 1372 size, 0, tx); 1373 1374 if (error == ENOTSUP) 1375 return; 1376 ASSERT0(error); 1377 1378 /* What blocksize did we actually get? */ 1379 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy); 1380 } 1381 1382 /* 1383 * Increase the file length 1384 * 1385 * IN: zp - znode of file to free data in. 1386 * end - new end-of-file 1387 * 1388 * RETURN: 0 on success, error code on failure 1389 */ 1390 static int 1391 zfs_extend(znode_t *zp, uint64_t end) 1392 { 1393 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1394 dmu_tx_t *tx; 1395 zfs_locked_range_t *lr; 1396 uint64_t newblksz; 1397 int error; 1398 1399 /* 1400 * We will change zp_size, lock the whole file. 1401 */ 1402 lr = zfs_rangelock_enter(&zp->z_rangelock, 0, UINT64_MAX, RL_WRITER); 1403 1404 /* 1405 * Nothing to do if file already at desired length. 1406 */ 1407 if (end <= zp->z_size) { 1408 zfs_rangelock_exit(lr); 1409 return (0); 1410 } 1411 tx = dmu_tx_create(zfsvfs->z_os); 1412 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1413 zfs_sa_upgrade_txholds(tx, zp); 1414 if (end > zp->z_blksz && 1415 (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) { 1416 /* 1417 * We are growing the file past the current block size. 1418 */ 1419 if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) { 1420 /* 1421 * File's blocksize is already larger than the 1422 * "recordsize" property. Only let it grow to 1423 * the next power of 2. 1424 */ 1425 ASSERT(!ISP2(zp->z_blksz)); 1426 newblksz = MIN(end, 1 << highbit64(zp->z_blksz)); 1427 } else { 1428 newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz); 1429 } 1430 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz); 1431 } else { 1432 newblksz = 0; 1433 } 1434 1435 error = dmu_tx_assign(tx, TXG_WAIT); 1436 if (error) { 1437 dmu_tx_abort(tx); 1438 zfs_rangelock_exit(lr); 1439 return (error); 1440 } 1441 1442 if (newblksz) 1443 zfs_grow_blocksize(zp, newblksz, tx); 1444 1445 zp->z_size = end; 1446 1447 VERIFY0(sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs), 1448 &zp->z_size, sizeof (zp->z_size), tx)); 1449 1450 vnode_pager_setsize(ZTOV(zp), end); 1451 1452 zfs_rangelock_exit(lr); 1453 1454 dmu_tx_commit(tx); 1455 1456 return (0); 1457 } 1458 1459 /* 1460 * Free space in a file. 1461 * 1462 * IN: zp - znode of file to free data in. 1463 * off - start of section to free. 1464 * len - length of section to free. 1465 * 1466 * RETURN: 0 on success, error code on failure 1467 */ 1468 static int 1469 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len) 1470 { 1471 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1472 zfs_locked_range_t *lr; 1473 int error; 1474 1475 /* 1476 * Lock the range being freed. 1477 */ 1478 lr = zfs_rangelock_enter(&zp->z_rangelock, off, len, RL_WRITER); 1479 1480 /* 1481 * Nothing to do if file already at desired length. 1482 */ 1483 if (off >= zp->z_size) { 1484 zfs_rangelock_exit(lr); 1485 return (0); 1486 } 1487 1488 if (off + len > zp->z_size) 1489 len = zp->z_size - off; 1490 1491 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len); 1492 1493 if (error == 0) { 1494 #if __FreeBSD_version >= 1400032 1495 vnode_pager_purge_range(ZTOV(zp), off, off + len); 1496 #else 1497 /* 1498 * Before __FreeBSD_version 1400032 we cannot free block in the 1499 * middle of a file, but only at the end of a file, so this code 1500 * path should never happen. 1501 */ 1502 vnode_pager_setsize(ZTOV(zp), off); 1503 #endif 1504 } 1505 1506 zfs_rangelock_exit(lr); 1507 1508 return (error); 1509 } 1510 1511 /* 1512 * Truncate a file 1513 * 1514 * IN: zp - znode of file to free data in. 1515 * end - new end-of-file. 1516 * 1517 * RETURN: 0 on success, error code on failure 1518 */ 1519 static int 1520 zfs_trunc(znode_t *zp, uint64_t end) 1521 { 1522 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1523 vnode_t *vp = ZTOV(zp); 1524 dmu_tx_t *tx; 1525 zfs_locked_range_t *lr; 1526 int error; 1527 sa_bulk_attr_t bulk[2]; 1528 int count = 0; 1529 1530 /* 1531 * We will change zp_size, lock the whole file. 1532 */ 1533 lr = zfs_rangelock_enter(&zp->z_rangelock, 0, UINT64_MAX, RL_WRITER); 1534 1535 /* 1536 * Nothing to do if file already at desired length. 1537 */ 1538 if (end >= zp->z_size) { 1539 zfs_rangelock_exit(lr); 1540 return (0); 1541 } 1542 1543 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end, 1544 DMU_OBJECT_END); 1545 if (error) { 1546 zfs_rangelock_exit(lr); 1547 return (error); 1548 } 1549 tx = dmu_tx_create(zfsvfs->z_os); 1550 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1551 zfs_sa_upgrade_txholds(tx, zp); 1552 dmu_tx_mark_netfree(tx); 1553 error = dmu_tx_assign(tx, TXG_WAIT); 1554 if (error) { 1555 dmu_tx_abort(tx); 1556 zfs_rangelock_exit(lr); 1557 return (error); 1558 } 1559 1560 zp->z_size = end; 1561 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), 1562 NULL, &zp->z_size, sizeof (zp->z_size)); 1563 1564 if (end == 0) { 1565 zp->z_pflags &= ~ZFS_SPARSE; 1566 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), 1567 NULL, &zp->z_pflags, 8); 1568 } 1569 VERIFY0(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx)); 1570 1571 dmu_tx_commit(tx); 1572 1573 /* 1574 * Clear any mapped pages in the truncated region. This has to 1575 * happen outside of the transaction to avoid the possibility of 1576 * a deadlock with someone trying to push a page that we are 1577 * about to invalidate. 1578 */ 1579 vnode_pager_setsize(vp, end); 1580 1581 zfs_rangelock_exit(lr); 1582 1583 return (0); 1584 } 1585 1586 /* 1587 * Free space in a file 1588 * 1589 * IN: zp - znode of file to free data in. 1590 * off - start of range 1591 * len - end of range (0 => EOF) 1592 * flag - current file open mode flags. 1593 * log - TRUE if this action should be logged 1594 * 1595 * RETURN: 0 on success, error code on failure 1596 */ 1597 int 1598 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log) 1599 { 1600 dmu_tx_t *tx; 1601 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1602 zilog_t *zilog = zfsvfs->z_log; 1603 uint64_t mode; 1604 uint64_t mtime[2], ctime[2]; 1605 sa_bulk_attr_t bulk[3]; 1606 int count = 0; 1607 int error; 1608 1609 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode, 1610 sizeof (mode))) != 0) 1611 return (error); 1612 1613 if (off > zp->z_size) { 1614 error = zfs_extend(zp, off+len); 1615 if (error == 0 && log) 1616 goto log; 1617 else 1618 return (error); 1619 } 1620 1621 if (len == 0) { 1622 error = zfs_trunc(zp, off); 1623 } else { 1624 if ((error = zfs_free_range(zp, off, len)) == 0 && 1625 off + len > zp->z_size) 1626 error = zfs_extend(zp, off+len); 1627 } 1628 if (error || !log) 1629 return (error); 1630 log: 1631 tx = dmu_tx_create(zfsvfs->z_os); 1632 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1633 zfs_sa_upgrade_txholds(tx, zp); 1634 error = dmu_tx_assign(tx, TXG_WAIT); 1635 if (error) { 1636 dmu_tx_abort(tx); 1637 return (error); 1638 } 1639 1640 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16); 1641 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16); 1642 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), 1643 NULL, &zp->z_pflags, 8); 1644 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime); 1645 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 1646 ASSERT0(error); 1647 1648 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len); 1649 1650 dmu_tx_commit(tx); 1651 return (0); 1652 } 1653 1654 void 1655 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx) 1656 { 1657 uint64_t moid, obj, sa_obj, version; 1658 uint64_t sense = ZFS_CASE_SENSITIVE; 1659 uint64_t norm = 0; 1660 nvpair_t *elem; 1661 int error; 1662 int i; 1663 znode_t *rootzp = NULL; 1664 zfsvfs_t *zfsvfs; 1665 vattr_t vattr; 1666 znode_t *zp; 1667 zfs_acl_ids_t acl_ids; 1668 1669 /* 1670 * First attempt to create master node. 1671 */ 1672 /* 1673 * In an empty objset, there are no blocks to read and thus 1674 * there can be no i/o errors (which we assert below). 1675 */ 1676 moid = MASTER_NODE_OBJ; 1677 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE, 1678 DMU_OT_NONE, 0, tx); 1679 ASSERT0(error); 1680 1681 /* 1682 * Set starting attributes. 1683 */ 1684 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os))); 1685 elem = NULL; 1686 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) { 1687 /* For the moment we expect all zpl props to be uint64_ts */ 1688 uint64_t val; 1689 char *name; 1690 1691 ASSERT3S(nvpair_type(elem), ==, DATA_TYPE_UINT64); 1692 val = fnvpair_value_uint64(elem); 1693 name = nvpair_name(elem); 1694 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) { 1695 if (val < version) 1696 version = val; 1697 } else { 1698 error = zap_update(os, moid, name, 8, 1, &val, tx); 1699 } 1700 ASSERT0(error); 1701 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0) 1702 norm = val; 1703 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0) 1704 sense = val; 1705 } 1706 ASSERT3U(version, !=, 0); 1707 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx); 1708 1709 /* 1710 * Create zap object used for SA attribute registration 1711 */ 1712 1713 if (version >= ZPL_VERSION_SA) { 1714 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE, 1715 DMU_OT_NONE, 0, tx); 1716 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx); 1717 ASSERT0(error); 1718 } else { 1719 sa_obj = 0; 1720 } 1721 /* 1722 * Create a delete queue. 1723 */ 1724 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx); 1725 1726 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx); 1727 ASSERT0(error); 1728 1729 /* 1730 * Create root znode. Create minimal znode/vnode/zfsvfs 1731 * to allow zfs_mknode to work. 1732 */ 1733 VATTR_NULL(&vattr); 1734 vattr.va_mask = AT_MODE|AT_UID|AT_GID; 1735 vattr.va_type = VDIR; 1736 vattr.va_mode = S_IFDIR|0755; 1737 vattr.va_uid = crgetuid(cr); 1738 vattr.va_gid = crgetgid(cr); 1739 1740 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP); 1741 1742 rootzp = zfs_znode_alloc_kmem(KM_SLEEP); 1743 ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs)); 1744 rootzp->z_unlinked = 0; 1745 rootzp->z_atime_dirty = 0; 1746 rootzp->z_is_sa = USE_SA(version, os); 1747 1748 zfsvfs->z_os = os; 1749 zfsvfs->z_parent = zfsvfs; 1750 zfsvfs->z_version = version; 1751 zfsvfs->z_use_fuids = USE_FUIDS(version, os); 1752 zfsvfs->z_use_sa = USE_SA(version, os); 1753 zfsvfs->z_norm = norm; 1754 1755 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END, 1756 &zfsvfs->z_attr_table); 1757 1758 ASSERT0(error); 1759 1760 /* 1761 * Fold case on file systems that are always or sometimes case 1762 * insensitive. 1763 */ 1764 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED) 1765 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER; 1766 1767 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL); 1768 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t), 1769 offsetof(znode_t, z_link_node)); 1770 1771 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) 1772 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL); 1773 1774 rootzp->z_zfsvfs = zfsvfs; 1775 VERIFY0(zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr, 1776 cr, NULL, &acl_ids)); 1777 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids); 1778 ASSERT3P(zp, ==, rootzp); 1779 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx); 1780 ASSERT0(error); 1781 zfs_acl_ids_free(&acl_ids); 1782 POINTER_INVALIDATE(&rootzp->z_zfsvfs); 1783 1784 sa_handle_destroy(rootzp->z_sa_hdl); 1785 zfs_znode_free_kmem(rootzp); 1786 1787 /* 1788 * Create shares directory 1789 */ 1790 1791 error = zfs_create_share_dir(zfsvfs, tx); 1792 1793 ASSERT0(error); 1794 1795 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) 1796 mutex_destroy(&zfsvfs->z_hold_mtx[i]); 1797 kmem_free(zfsvfs, sizeof (zfsvfs_t)); 1798 } 1799 #endif /* _KERNEL */ 1800 1801 static int 1802 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table) 1803 { 1804 uint64_t sa_obj = 0; 1805 int error; 1806 1807 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj); 1808 if (error != 0 && error != ENOENT) 1809 return (error); 1810 1811 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table); 1812 return (error); 1813 } 1814 1815 static int 1816 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp, 1817 dmu_buf_t **db, const void *tag) 1818 { 1819 dmu_object_info_t doi; 1820 int error; 1821 1822 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0) 1823 return (error); 1824 1825 dmu_object_info_from_db(*db, &doi); 1826 if ((doi.doi_bonus_type != DMU_OT_SA && 1827 doi.doi_bonus_type != DMU_OT_ZNODE) || 1828 (doi.doi_bonus_type == DMU_OT_ZNODE && 1829 doi.doi_bonus_size < sizeof (znode_phys_t))) { 1830 sa_buf_rele(*db, tag); 1831 return (SET_ERROR(ENOTSUP)); 1832 } 1833 1834 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp); 1835 if (error != 0) { 1836 sa_buf_rele(*db, tag); 1837 return (error); 1838 } 1839 1840 return (0); 1841 } 1842 1843 static void 1844 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, const void *tag) 1845 { 1846 sa_handle_destroy(hdl); 1847 sa_buf_rele(db, tag); 1848 } 1849 1850 /* 1851 * Given an object number, return its parent object number and whether 1852 * or not the object is an extended attribute directory. 1853 */ 1854 static int 1855 zfs_obj_to_pobj(objset_t *osp, sa_handle_t *hdl, sa_attr_type_t *sa_table, 1856 uint64_t *pobjp, int *is_xattrdir) 1857 { 1858 uint64_t parent; 1859 uint64_t pflags; 1860 uint64_t mode; 1861 uint64_t parent_mode; 1862 sa_bulk_attr_t bulk[3]; 1863 sa_handle_t *sa_hdl; 1864 dmu_buf_t *sa_db; 1865 int count = 0; 1866 int error; 1867 1868 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL, 1869 &parent, sizeof (parent)); 1870 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL, 1871 &pflags, sizeof (pflags)); 1872 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL, 1873 &mode, sizeof (mode)); 1874 1875 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0) 1876 return (error); 1877 1878 /* 1879 * When a link is removed its parent pointer is not changed and will 1880 * be invalid. There are two cases where a link is removed but the 1881 * file stays around, when it goes to the delete queue and when there 1882 * are additional links. 1883 */ 1884 error = zfs_grab_sa_handle(osp, parent, &sa_hdl, &sa_db, FTAG); 1885 if (error != 0) 1886 return (error); 1887 1888 error = sa_lookup(sa_hdl, ZPL_MODE, &parent_mode, sizeof (parent_mode)); 1889 zfs_release_sa_handle(sa_hdl, sa_db, FTAG); 1890 if (error != 0) 1891 return (error); 1892 1893 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode); 1894 1895 /* 1896 * Extended attributes can be applied to files, directories, etc. 1897 * Otherwise the parent must be a directory. 1898 */ 1899 if (!*is_xattrdir && !S_ISDIR(parent_mode)) 1900 return (SET_ERROR(EINVAL)); 1901 1902 *pobjp = parent; 1903 1904 return (0); 1905 } 1906 1907 /* 1908 * Given an object number, return some zpl level statistics 1909 */ 1910 static int 1911 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table, 1912 zfs_stat_t *sb) 1913 { 1914 sa_bulk_attr_t bulk[4]; 1915 int count = 0; 1916 1917 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL, 1918 &sb->zs_mode, sizeof (sb->zs_mode)); 1919 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL, 1920 &sb->zs_gen, sizeof (sb->zs_gen)); 1921 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL, 1922 &sb->zs_links, sizeof (sb->zs_links)); 1923 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL, 1924 &sb->zs_ctime, sizeof (sb->zs_ctime)); 1925 1926 return (sa_bulk_lookup(hdl, bulk, count)); 1927 } 1928 1929 static int 1930 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl, 1931 sa_attr_type_t *sa_table, char *buf, int len) 1932 { 1933 sa_handle_t *sa_hdl; 1934 sa_handle_t *prevhdl = NULL; 1935 dmu_buf_t *prevdb = NULL; 1936 dmu_buf_t *sa_db = NULL; 1937 char *path = buf + len - 1; 1938 int error; 1939 1940 *path = '\0'; 1941 sa_hdl = hdl; 1942 1943 uint64_t deleteq_obj; 1944 VERIFY0(zap_lookup(osp, MASTER_NODE_OBJ, 1945 ZFS_UNLINKED_SET, sizeof (uint64_t), 1, &deleteq_obj)); 1946 error = zap_lookup_int(osp, deleteq_obj, obj); 1947 if (error == 0) { 1948 return (ESTALE); 1949 } else if (error != ENOENT) { 1950 return (error); 1951 } 1952 error = 0; 1953 1954 for (;;) { 1955 uint64_t pobj; 1956 char component[MAXNAMELEN + 2]; 1957 size_t complen; 1958 int is_xattrdir; 1959 1960 if (prevdb) { 1961 ASSERT3P(prevhdl, !=, NULL); 1962 zfs_release_sa_handle(prevhdl, prevdb, FTAG); 1963 } 1964 1965 if ((error = zfs_obj_to_pobj(osp, sa_hdl, sa_table, &pobj, 1966 &is_xattrdir)) != 0) 1967 break; 1968 1969 if (pobj == obj) { 1970 if (path[0] != '/') 1971 *--path = '/'; 1972 break; 1973 } 1974 1975 component[0] = '/'; 1976 if (is_xattrdir) { 1977 (void) sprintf(component + 1, "<xattrdir>"); 1978 } else { 1979 error = zap_value_search(osp, pobj, obj, 1980 ZFS_DIRENT_OBJ(-1ULL), component + 1); 1981 if (error != 0) 1982 break; 1983 } 1984 1985 complen = strlen(component); 1986 path -= complen; 1987 ASSERT3P(path, >=, buf); 1988 memcpy(path, component, complen); 1989 obj = pobj; 1990 1991 if (sa_hdl != hdl) { 1992 prevhdl = sa_hdl; 1993 prevdb = sa_db; 1994 } 1995 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG); 1996 if (error != 0) { 1997 sa_hdl = prevhdl; 1998 sa_db = prevdb; 1999 break; 2000 } 2001 } 2002 2003 if (sa_hdl != NULL && sa_hdl != hdl) { 2004 ASSERT3P(sa_db, !=, NULL); 2005 zfs_release_sa_handle(sa_hdl, sa_db, FTAG); 2006 } 2007 2008 if (error == 0) 2009 (void) memmove(buf, path, buf + len - path); 2010 2011 return (error); 2012 } 2013 2014 int 2015 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len) 2016 { 2017 sa_attr_type_t *sa_table; 2018 sa_handle_t *hdl; 2019 dmu_buf_t *db; 2020 int error; 2021 2022 error = zfs_sa_setup(osp, &sa_table); 2023 if (error != 0) 2024 return (error); 2025 2026 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG); 2027 if (error != 0) 2028 return (error); 2029 2030 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len); 2031 2032 zfs_release_sa_handle(hdl, db, FTAG); 2033 return (error); 2034 } 2035 2036 int 2037 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb, 2038 char *buf, int len) 2039 { 2040 char *path = buf + len - 1; 2041 sa_attr_type_t *sa_table; 2042 sa_handle_t *hdl; 2043 dmu_buf_t *db; 2044 int error; 2045 2046 *path = '\0'; 2047 2048 error = zfs_sa_setup(osp, &sa_table); 2049 if (error != 0) 2050 return (error); 2051 2052 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG); 2053 if (error != 0) 2054 return (error); 2055 2056 error = zfs_obj_to_stats_impl(hdl, sa_table, sb); 2057 if (error != 0) { 2058 zfs_release_sa_handle(hdl, db, FTAG); 2059 return (error); 2060 } 2061 2062 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len); 2063 2064 zfs_release_sa_handle(hdl, db, FTAG); 2065 return (error); 2066 } 2067 2068 2069 void 2070 zfs_znode_update_vfs(znode_t *zp) 2071 { 2072 vm_object_t object; 2073 2074 if ((object = ZTOV(zp)->v_object) == NULL || 2075 zp->z_size == object->un_pager.vnp.vnp_size) 2076 return; 2077 2078 vnode_pager_setsize(ZTOV(zp), zp->z_size); 2079 } 2080 2081 2082 #ifdef _KERNEL 2083 int 2084 zfs_znode_parent_and_name(znode_t *zp, znode_t **dzpp, char *buf) 2085 { 2086 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2087 uint64_t parent; 2088 int is_xattrdir; 2089 int err; 2090 2091 /* Extended attributes should not be visible as regular files. */ 2092 if ((zp->z_pflags & ZFS_XATTR) != 0) 2093 return (SET_ERROR(EINVAL)); 2094 2095 err = zfs_obj_to_pobj(zfsvfs->z_os, zp->z_sa_hdl, zfsvfs->z_attr_table, 2096 &parent, &is_xattrdir); 2097 if (err != 0) 2098 return (err); 2099 ASSERT0(is_xattrdir); 2100 2101 /* No name as this is a root object. */ 2102 if (parent == zp->z_id) 2103 return (SET_ERROR(EINVAL)); 2104 2105 err = zap_value_search(zfsvfs->z_os, parent, zp->z_id, 2106 ZFS_DIRENT_OBJ(-1ULL), buf); 2107 if (err != 0) 2108 return (err); 2109 err = zfs_zget(zfsvfs, parent, dzpp); 2110 return (err); 2111 } 2112 #endif /* _KERNEL */ 2113