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