zfs_znode.c revision 242240
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 VOP_UNLOCK(vp, 0); 636 VI_LOCK(vp); 637 vp->v_usecount--; 638 vp->v_iflag |= VI_DOOMED; 639 vp->v_data = NULL; 640 vdropl(vp); 641} 642 643/* 644 * Construct a new znode/vnode and intialize. 645 * 646 * This does not do a call to dmu_set_user() that is 647 * up to the caller to do, in case you don't want to 648 * return the znode 649 */ 650static znode_t * 651zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz, 652 dmu_object_type_t obj_type, sa_handle_t *hdl) 653{ 654 znode_t *zp; 655 vnode_t *vp; 656 uint64_t mode; 657 uint64_t parent; 658 sa_bulk_attr_t bulk[9]; 659 int count = 0; 660 661 zp = kmem_cache_alloc(znode_cache, KM_SLEEP); 662 zfs_znode_cache_constructor(zp, zfsvfs->z_parent->z_vfs, 0); 663 664 ASSERT(zp->z_dirlocks == NULL); 665 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs)); 666 zp->z_moved = 0; 667 668 /* 669 * Defer setting z_zfsvfs until the znode is ready to be a candidate for 670 * the zfs_znode_move() callback. 671 */ 672 zp->z_sa_hdl = NULL; 673 zp->z_unlinked = 0; 674 zp->z_atime_dirty = 0; 675 zp->z_mapcnt = 0; 676 zp->z_id = db->db_object; 677 zp->z_blksz = blksz; 678 zp->z_seq = 0x7A4653; 679 zp->z_sync_cnt = 0; 680 681 vp = ZTOV(zp); 682 683 zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl); 684 685 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8); 686 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8); 687 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL, 688 &zp->z_size, 8); 689 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL, 690 &zp->z_links, 8); 691 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 692 &zp->z_pflags, 8); 693 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8); 694 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, 695 &zp->z_atime, 16); 696 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, 697 &zp->z_uid, 8); 698 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, 699 &zp->z_gid, 8); 700 701 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) { 702 if (hdl == NULL) 703 sa_handle_destroy(zp->z_sa_hdl); 704 zfs_vnode_forget(vp); 705 zp->z_vnode = NULL; 706 kmem_cache_free(znode_cache, zp); 707 return (NULL); 708 } 709 710 zp->z_mode = mode; 711 712 vp->v_type = IFTOVT((mode_t)mode); 713 714 switch (vp->v_type) { 715 case VDIR: 716 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */ 717 break; 718#ifdef sun 719 case VBLK: 720 case VCHR: 721 { 722 uint64_t rdev; 723 VERIFY(sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zfsvfs), 724 &rdev, sizeof (rdev)) == 0); 725 726 vp->v_rdev = zfs_cmpldev(rdev); 727 } 728 break; 729#endif /* sun */ 730 case VFIFO: 731#ifdef sun 732 case VSOCK: 733 case VDOOR: 734#endif /* sun */ 735 vp->v_op = &zfs_fifoops; 736 break; 737 case VREG: 738 if (parent == zfsvfs->z_shares_dir) { 739 ASSERT(zp->z_uid == 0 && zp->z_gid == 0); 740 vp->v_op = &zfs_shareops; 741 } 742 break; 743#ifdef sun 744 case VLNK: 745 vn_setops(vp, zfs_symvnodeops); 746 break; 747 default: 748 vn_setops(vp, zfs_evnodeops); 749 break; 750#endif /* sun */ 751 } 752 if (vp->v_type != VFIFO) 753 VN_LOCK_ASHARE(vp); 754 755 mutex_enter(&zfsvfs->z_znodes_lock); 756 list_insert_tail(&zfsvfs->z_all_znodes, zp); 757 membar_producer(); 758 /* 759 * Everything else must be valid before assigning z_zfsvfs makes the 760 * znode eligible for zfs_znode_move(). 761 */ 762 zp->z_zfsvfs = zfsvfs; 763 mutex_exit(&zfsvfs->z_znodes_lock); 764 765 VFS_HOLD(zfsvfs->z_vfs); 766 return (zp); 767} 768 769static uint64_t empty_xattr; 770static uint64_t pad[4]; 771static zfs_acl_phys_t acl_phys; 772/* 773 * Create a new DMU object to hold a zfs znode. 774 * 775 * IN: dzp - parent directory for new znode 776 * vap - file attributes for new znode 777 * tx - dmu transaction id for zap operations 778 * cr - credentials of caller 779 * flag - flags: 780 * IS_ROOT_NODE - new object will be root 781 * IS_XATTR - new object is an attribute 782 * bonuslen - length of bonus buffer 783 * setaclp - File/Dir initial ACL 784 * fuidp - Tracks fuid allocation. 785 * 786 * OUT: zpp - allocated znode 787 * 788 */ 789void 790zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr, 791 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids) 792{ 793 uint64_t crtime[2], atime[2], mtime[2], ctime[2]; 794 uint64_t mode, size, links, parent, pflags; 795 uint64_t dzp_pflags = 0; 796 uint64_t rdev = 0; 797 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 798 dmu_buf_t *db; 799 timestruc_t now; 800 uint64_t gen, obj; 801 int err; 802 int bonuslen; 803 sa_handle_t *sa_hdl; 804 dmu_object_type_t obj_type; 805 sa_bulk_attr_t sa_attrs[ZPL_END]; 806 int cnt = 0; 807 zfs_acl_locator_cb_t locate = { 0 }; 808 809 ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE)); 810 811 if (zfsvfs->z_replay) { 812 obj = vap->va_nodeid; 813 now = vap->va_ctime; /* see zfs_replay_create() */ 814 gen = vap->va_nblocks; /* ditto */ 815 } else { 816 obj = 0; 817 gethrestime(&now); 818 gen = dmu_tx_get_txg(tx); 819 } 820 821 obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE; 822 bonuslen = (obj_type == DMU_OT_SA) ? 823 DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE; 824 825 /* 826 * Create a new DMU object. 827 */ 828 /* 829 * There's currently no mechanism for pre-reading the blocks that will 830 * be needed to allocate a new object, so we accept the small chance 831 * that there will be an i/o error and we will fail one of the 832 * assertions below. 833 */ 834 if (vap->va_type == VDIR) { 835 if (zfsvfs->z_replay) { 836 err = zap_create_claim_norm(zfsvfs->z_os, obj, 837 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS, 838 obj_type, bonuslen, tx); 839 ASSERT3U(err, ==, 0); 840 } else { 841 obj = zap_create_norm(zfsvfs->z_os, 842 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS, 843 obj_type, bonuslen, tx); 844 } 845 } else { 846 if (zfsvfs->z_replay) { 847 err = dmu_object_claim(zfsvfs->z_os, obj, 848 DMU_OT_PLAIN_FILE_CONTENTS, 0, 849 obj_type, bonuslen, tx); 850 ASSERT3U(err, ==, 0); 851 } else { 852 obj = dmu_object_alloc(zfsvfs->z_os, 853 DMU_OT_PLAIN_FILE_CONTENTS, 0, 854 obj_type, bonuslen, tx); 855 } 856 } 857 858 getnewvnode_reserve(1); 859 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj); 860 VERIFY(0 == sa_buf_hold(zfsvfs->z_os, obj, NULL, &db)); 861 862 /* 863 * If this is the root, fix up the half-initialized parent pointer 864 * to reference the just-allocated physical data area. 865 */ 866 if (flag & IS_ROOT_NODE) { 867 dzp->z_id = obj; 868 } else { 869 dzp_pflags = dzp->z_pflags; 870 } 871 872 /* 873 * If parent is an xattr, so am I. 874 */ 875 if (dzp_pflags & ZFS_XATTR) { 876 flag |= IS_XATTR; 877 } 878 879 if (zfsvfs->z_use_fuids) 880 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED; 881 else 882 pflags = 0; 883 884 if (vap->va_type == VDIR) { 885 size = 2; /* contents ("." and "..") */ 886 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1; 887 } else { 888 size = links = 0; 889 } 890 891 if (vap->va_type == VBLK || vap->va_type == VCHR) { 892 rdev = zfs_expldev(vap->va_rdev); 893 } 894 895 parent = dzp->z_id; 896 mode = acl_ids->z_mode; 897 if (flag & IS_XATTR) 898 pflags |= ZFS_XATTR; 899 900 /* 901 * No execs denied will be deterimed when zfs_mode_compute() is called. 902 */ 903 pflags |= acl_ids->z_aclp->z_hints & 904 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT| 905 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED); 906 907 ZFS_TIME_ENCODE(&now, crtime); 908 ZFS_TIME_ENCODE(&now, ctime); 909 910 if (vap->va_mask & AT_ATIME) { 911 ZFS_TIME_ENCODE(&vap->va_atime, atime); 912 } else { 913 ZFS_TIME_ENCODE(&now, atime); 914 } 915 916 if (vap->va_mask & AT_MTIME) { 917 ZFS_TIME_ENCODE(&vap->va_mtime, mtime); 918 } else { 919 ZFS_TIME_ENCODE(&now, mtime); 920 } 921 922 /* Now add in all of the "SA" attributes */ 923 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED, 924 &sa_hdl)); 925 926 /* 927 * Setup the array of attributes to be replaced/set on the new file 928 * 929 * order for DMU_OT_ZNODE is critical since it needs to be constructed 930 * in the old znode_phys_t format. Don't change this ordering 931 */ 932 933 if (obj_type == DMU_OT_ZNODE) { 934 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs), 935 NULL, &atime, 16); 936 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs), 937 NULL, &mtime, 16); 938 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs), 939 NULL, &ctime, 16); 940 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs), 941 NULL, &crtime, 16); 942 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs), 943 NULL, &gen, 8); 944 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs), 945 NULL, &mode, 8); 946 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs), 947 NULL, &size, 8); 948 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs), 949 NULL, &parent, 8); 950 } else { 951 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs), 952 NULL, &mode, 8); 953 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs), 954 NULL, &size, 8); 955 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs), 956 NULL, &gen, 8); 957 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL, 958 &acl_ids->z_fuid, 8); 959 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL, 960 &acl_ids->z_fgid, 8); 961 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs), 962 NULL, &parent, 8); 963 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs), 964 NULL, &pflags, 8); 965 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs), 966 NULL, &atime, 16); 967 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs), 968 NULL, &mtime, 16); 969 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs), 970 NULL, &ctime, 16); 971 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs), 972 NULL, &crtime, 16); 973 } 974 975 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8); 976 977 if (obj_type == DMU_OT_ZNODE) { 978 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL, 979 &empty_xattr, 8); 980 } 981 if (obj_type == DMU_OT_ZNODE || 982 (vap->va_type == VBLK || vap->va_type == VCHR)) { 983 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs), 984 NULL, &rdev, 8); 985 986 } 987 if (obj_type == DMU_OT_ZNODE) { 988 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs), 989 NULL, &pflags, 8); 990 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL, 991 &acl_ids->z_fuid, 8); 992 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL, 993 &acl_ids->z_fgid, 8); 994 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad, 995 sizeof (uint64_t) * 4); 996 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL, 997 &acl_phys, sizeof (zfs_acl_phys_t)); 998 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) { 999 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL, 1000 &acl_ids->z_aclp->z_acl_count, 8); 1001 locate.cb_aclp = acl_ids->z_aclp; 1002 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs), 1003 zfs_acl_data_locator, &locate, 1004 acl_ids->z_aclp->z_acl_bytes); 1005 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags, 1006 acl_ids->z_fuid, acl_ids->z_fgid); 1007 } 1008 1009 VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0); 1010 1011 if (!(flag & IS_ROOT_NODE)) { 1012 *zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl); 1013 ASSERT(*zpp != NULL); 1014 } else { 1015 /* 1016 * If we are creating the root node, the "parent" we 1017 * passed in is the znode for the root. 1018 */ 1019 *zpp = dzp; 1020 1021 (*zpp)->z_sa_hdl = sa_hdl; 1022 } 1023 1024 (*zpp)->z_pflags = pflags; 1025 (*zpp)->z_mode = mode; 1026 1027 if (vap->va_mask & AT_XVATTR) 1028 zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx); 1029 1030 if (obj_type == DMU_OT_ZNODE || 1031 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) { 1032 err = zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx); 1033 ASSERT3P(err, ==, 0); 1034 } 1035 if (!(flag & IS_ROOT_NODE)) { 1036 vnode_t *vp; 1037 1038 vp = ZTOV(*zpp); 1039 vp->v_vflag |= VV_FORCEINSMQ; 1040 err = insmntque(vp, zfsvfs->z_vfs); 1041 vp->v_vflag &= ~VV_FORCEINSMQ; 1042 KASSERT(err == 0, ("insmntque() failed: error %d", err)); 1043 } 1044 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj); 1045 getnewvnode_drop_reserve(); 1046} 1047 1048/* 1049 * zfs_xvattr_set only updates the in-core attributes 1050 * it is assumed the caller will be doing an sa_bulk_update 1051 * to push the changes out 1052 */ 1053void 1054zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx) 1055{ 1056 xoptattr_t *xoap; 1057 1058 xoap = xva_getxoptattr(xvap); 1059 ASSERT(xoap); 1060 1061 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 1062 uint64_t times[2]; 1063 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times); 1064 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs), 1065 ×, sizeof (times), tx); 1066 XVA_SET_RTN(xvap, XAT_CREATETIME); 1067 } 1068 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 1069 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly, 1070 zp->z_pflags, tx); 1071 XVA_SET_RTN(xvap, XAT_READONLY); 1072 } 1073 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 1074 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden, 1075 zp->z_pflags, tx); 1076 XVA_SET_RTN(xvap, XAT_HIDDEN); 1077 } 1078 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 1079 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system, 1080 zp->z_pflags, tx); 1081 XVA_SET_RTN(xvap, XAT_SYSTEM); 1082 } 1083 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 1084 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive, 1085 zp->z_pflags, tx); 1086 XVA_SET_RTN(xvap, XAT_ARCHIVE); 1087 } 1088 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 1089 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable, 1090 zp->z_pflags, tx); 1091 XVA_SET_RTN(xvap, XAT_IMMUTABLE); 1092 } 1093 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 1094 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink, 1095 zp->z_pflags, tx); 1096 XVA_SET_RTN(xvap, XAT_NOUNLINK); 1097 } 1098 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 1099 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly, 1100 zp->z_pflags, tx); 1101 XVA_SET_RTN(xvap, XAT_APPENDONLY); 1102 } 1103 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 1104 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump, 1105 zp->z_pflags, tx); 1106 XVA_SET_RTN(xvap, XAT_NODUMP); 1107 } 1108 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 1109 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque, 1110 zp->z_pflags, tx); 1111 XVA_SET_RTN(xvap, XAT_OPAQUE); 1112 } 1113 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 1114 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED, 1115 xoap->xoa_av_quarantined, zp->z_pflags, tx); 1116 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 1117 } 1118 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 1119 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified, 1120 zp->z_pflags, tx); 1121 XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 1122 } 1123 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) { 1124 zfs_sa_set_scanstamp(zp, xvap, tx); 1125 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP); 1126 } 1127 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 1128 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse, 1129 zp->z_pflags, tx); 1130 XVA_SET_RTN(xvap, XAT_REPARSE); 1131 } 1132 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) { 1133 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline, 1134 zp->z_pflags, tx); 1135 XVA_SET_RTN(xvap, XAT_OFFLINE); 1136 } 1137 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) { 1138 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse, 1139 zp->z_pflags, tx); 1140 XVA_SET_RTN(xvap, XAT_SPARSE); 1141 } 1142} 1143 1144int 1145zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp) 1146{ 1147 dmu_object_info_t doi; 1148 dmu_buf_t *db; 1149 znode_t *zp; 1150 int err; 1151 sa_handle_t *hdl; 1152 int first = 1; 1153 1154 *zpp = NULL; 1155 1156 getnewvnode_reserve(1); 1157again: 1158 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num); 1159 1160 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db); 1161 if (err) { 1162 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1163 getnewvnode_drop_reserve(); 1164 return (err); 1165 } 1166 1167 dmu_object_info_from_db(db, &doi); 1168 if (doi.doi_bonus_type != DMU_OT_SA && 1169 (doi.doi_bonus_type != DMU_OT_ZNODE || 1170 (doi.doi_bonus_type == DMU_OT_ZNODE && 1171 doi.doi_bonus_size < sizeof (znode_phys_t)))) { 1172 sa_buf_rele(db, NULL); 1173 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1174 getnewvnode_drop_reserve(); 1175 return (EINVAL); 1176 } 1177 1178 hdl = dmu_buf_get_user(db); 1179 if (hdl != NULL) { 1180 zp = sa_get_userdata(hdl); 1181 1182 1183 /* 1184 * Since "SA" does immediate eviction we 1185 * should never find a sa handle that doesn't 1186 * know about the znode. 1187 */ 1188 1189 ASSERT3P(zp, !=, NULL); 1190 1191 mutex_enter(&zp->z_lock); 1192 ASSERT3U(zp->z_id, ==, obj_num); 1193 if (zp->z_unlinked) { 1194 err = ENOENT; 1195 } else { 1196 vnode_t *vp; 1197 int dying = 0; 1198 1199 vp = ZTOV(zp); 1200 if (vp == NULL) 1201 dying = 1; 1202 else { 1203 VN_HOLD(vp); 1204 if ((vp->v_iflag & VI_DOOMED) != 0) { 1205 dying = 1; 1206 /* 1207 * Don't VN_RELE() vnode here, because 1208 * it can call vn_lock() which creates 1209 * LOR between vnode lock and znode 1210 * lock. We will VN_RELE() the vnode 1211 * after droping znode lock. 1212 */ 1213 } 1214 } 1215 if (dying) { 1216 if (first) { 1217 ZFS_LOG(1, "dying znode detected (zp=%p)", zp); 1218 first = 0; 1219 } 1220 /* 1221 * znode is dying so we can't reuse it, we must 1222 * wait until destruction is completed. 1223 */ 1224 sa_buf_rele(db, NULL); 1225 mutex_exit(&zp->z_lock); 1226 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1227 if (vp != NULL) 1228 VN_RELE(vp); 1229 tsleep(zp, 0, "zcollide", 1); 1230 goto again; 1231 } 1232 *zpp = zp; 1233 err = 0; 1234 } 1235 sa_buf_rele(db, NULL); 1236 mutex_exit(&zp->z_lock); 1237 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1238 getnewvnode_drop_reserve(); 1239 return (err); 1240 } 1241 1242 /* 1243 * Not found create new znode/vnode 1244 * but only if file exists. 1245 * 1246 * There is a small window where zfs_vget() could 1247 * find this object while a file create is still in 1248 * progress. This is checked for in zfs_znode_alloc() 1249 * 1250 * if zfs_znode_alloc() fails it will drop the hold on the 1251 * bonus buffer. 1252 */ 1253 zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size, 1254 doi.doi_bonus_type, NULL); 1255 if (zp == NULL) { 1256 err = ENOENT; 1257 } else { 1258 *zpp = zp; 1259 } 1260 if (err == 0) { 1261 vnode_t *vp = ZTOV(zp); 1262 1263 err = insmntque(vp, zfsvfs->z_vfs); 1264 if (err == 0) 1265 VOP_UNLOCK(vp, 0); 1266 else { 1267 zp->z_vnode = NULL; 1268 zfs_znode_dmu_fini(zp); 1269 zfs_znode_free(zp); 1270 *zpp = NULL; 1271 } 1272 } 1273 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1274 getnewvnode_drop_reserve(); 1275 return (err); 1276} 1277 1278int 1279zfs_rezget(znode_t *zp) 1280{ 1281 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1282 dmu_object_info_t doi; 1283 dmu_buf_t *db; 1284 vnode_t *vp; 1285 uint64_t obj_num = zp->z_id; 1286 uint64_t mode, size; 1287 sa_bulk_attr_t bulk[8]; 1288 int err; 1289 int count = 0; 1290 uint64_t gen; 1291 1292 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num); 1293 1294 mutex_enter(&zp->z_acl_lock); 1295 if (zp->z_acl_cached) { 1296 zfs_acl_free(zp->z_acl_cached); 1297 zp->z_acl_cached = NULL; 1298 } 1299 1300 mutex_exit(&zp->z_acl_lock); 1301 ASSERT(zp->z_sa_hdl == NULL); 1302 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db); 1303 if (err) { 1304 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1305 return (err); 1306 } 1307 1308 dmu_object_info_from_db(db, &doi); 1309 if (doi.doi_bonus_type != DMU_OT_SA && 1310 (doi.doi_bonus_type != DMU_OT_ZNODE || 1311 (doi.doi_bonus_type == DMU_OT_ZNODE && 1312 doi.doi_bonus_size < sizeof (znode_phys_t)))) { 1313 sa_buf_rele(db, NULL); 1314 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1315 return (EINVAL); 1316 } 1317 1318 zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL); 1319 size = zp->z_size; 1320 1321 /* reload cached values */ 1322 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, 1323 &gen, sizeof (gen)); 1324 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL, 1325 &zp->z_size, sizeof (zp->z_size)); 1326 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL, 1327 &zp->z_links, sizeof (zp->z_links)); 1328 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 1329 &zp->z_pflags, sizeof (zp->z_pflags)); 1330 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, 1331 &zp->z_atime, sizeof (zp->z_atime)); 1332 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, 1333 &zp->z_uid, sizeof (zp->z_uid)); 1334 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, 1335 &zp->z_gid, sizeof (zp->z_gid)); 1336 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, 1337 &mode, sizeof (mode)); 1338 1339 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) { 1340 zfs_znode_dmu_fini(zp); 1341 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1342 return (EIO); 1343 } 1344 1345 zp->z_mode = mode; 1346 1347 if (gen != zp->z_gen) { 1348 zfs_znode_dmu_fini(zp); 1349 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1350 return (EIO); 1351 } 1352 1353 /* 1354 * XXXPJD: Not sure how is that possible, but under heavy 1355 * zfs recv -F load it happens that z_gen is the same, but 1356 * vnode type is different than znode type. This would mean 1357 * that for example regular file was replaced with directory 1358 * which has the same object number. 1359 */ 1360 vp = ZTOV(zp); 1361 if (vp != NULL && 1362 vp->v_type != IFTOVT((mode_t)zp->z_mode)) { 1363 zfs_znode_dmu_fini(zp); 1364 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1365 return (EIO); 1366 } 1367 1368 zp->z_unlinked = (zp->z_links == 0); 1369 zp->z_blksz = doi.doi_data_block_size; 1370 if (vp != NULL) { 1371 vn_pages_remove(vp, 0, 0); 1372 if (zp->z_size != size) 1373 vnode_pager_setsize(vp, zp->z_size); 1374 } 1375 1376 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1377 1378 return (0); 1379} 1380 1381void 1382zfs_znode_delete(znode_t *zp, dmu_tx_t *tx) 1383{ 1384 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1385 objset_t *os = zfsvfs->z_os; 1386 uint64_t obj = zp->z_id; 1387 uint64_t acl_obj = zfs_external_acl(zp); 1388 1389 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj); 1390 if (acl_obj) { 1391 VERIFY(!zp->z_is_sa); 1392 VERIFY(0 == dmu_object_free(os, acl_obj, tx)); 1393 } 1394 VERIFY(0 == dmu_object_free(os, obj, tx)); 1395 zfs_znode_dmu_fini(zp); 1396 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj); 1397 zfs_znode_free(zp); 1398} 1399 1400void 1401zfs_zinactive(znode_t *zp) 1402{ 1403 vnode_t *vp = ZTOV(zp); 1404 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1405 uint64_t z_id = zp->z_id; 1406 int vfslocked; 1407 1408 ASSERT(zp->z_sa_hdl); 1409 1410 /* 1411 * Don't allow a zfs_zget() while were trying to release this znode 1412 */ 1413 ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id); 1414 1415 mutex_enter(&zp->z_lock); 1416 VI_LOCK(vp); 1417 if (vp->v_count > 0) { 1418 /* 1419 * If the hold count is greater than zero, somebody has 1420 * obtained a new reference on this znode while we were 1421 * processing it here, so we are done. 1422 */ 1423 VI_UNLOCK(vp); 1424 mutex_exit(&zp->z_lock); 1425 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 1426 return; 1427 } 1428 VI_UNLOCK(vp); 1429 1430 /* 1431 * If this was the last reference to a file with no links, 1432 * remove the file from the file system. 1433 */ 1434 if (zp->z_unlinked) { 1435 mutex_exit(&zp->z_lock); 1436 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 1437 ASSERT(vp->v_count == 0); 1438 vrecycle(vp, curthread); 1439 vfslocked = VFS_LOCK_GIANT(zfsvfs->z_vfs); 1440 zfs_rmnode(zp); 1441 VFS_UNLOCK_GIANT(vfslocked); 1442 return; 1443 } 1444 1445 mutex_exit(&zp->z_lock); 1446 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 1447} 1448 1449void 1450zfs_znode_free(znode_t *zp) 1451{ 1452 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1453 1454 ASSERT(ZTOV(zp) == NULL); 1455 ASSERT(zp->z_sa_hdl == NULL); 1456 mutex_enter(&zfsvfs->z_znodes_lock); 1457 POINTER_INVALIDATE(&zp->z_zfsvfs); 1458 list_remove(&zfsvfs->z_all_znodes, zp); 1459 mutex_exit(&zfsvfs->z_znodes_lock); 1460 1461 if (zp->z_acl_cached) { 1462 zfs_acl_free(zp->z_acl_cached); 1463 zp->z_acl_cached = NULL; 1464 } 1465 1466 kmem_cache_free(znode_cache, zp); 1467 1468 VFS_RELE(zfsvfs->z_vfs); 1469} 1470 1471void 1472zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2], 1473 uint64_t ctime[2], boolean_t have_tx) 1474{ 1475 timestruc_t now; 1476 1477 gethrestime(&now); 1478 1479 if (have_tx) { /* will sa_bulk_update happen really soon? */ 1480 zp->z_atime_dirty = 0; 1481 zp->z_seq++; 1482 } else { 1483 zp->z_atime_dirty = 1; 1484 } 1485 1486 if (flag & AT_ATIME) { 1487 ZFS_TIME_ENCODE(&now, zp->z_atime); 1488 } 1489 1490 if (flag & AT_MTIME) { 1491 ZFS_TIME_ENCODE(&now, mtime); 1492 if (zp->z_zfsvfs->z_use_fuids) { 1493 zp->z_pflags |= (ZFS_ARCHIVE | 1494 ZFS_AV_MODIFIED); 1495 } 1496 } 1497 1498 if (flag & AT_CTIME) { 1499 ZFS_TIME_ENCODE(&now, ctime); 1500 if (zp->z_zfsvfs->z_use_fuids) 1501 zp->z_pflags |= ZFS_ARCHIVE; 1502 } 1503} 1504 1505/* 1506 * Grow the block size for a file. 1507 * 1508 * IN: zp - znode of file to free data in. 1509 * size - requested block size 1510 * tx - open transaction. 1511 * 1512 * NOTE: this function assumes that the znode is write locked. 1513 */ 1514void 1515zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx) 1516{ 1517 int error; 1518 u_longlong_t dummy; 1519 1520 if (size <= zp->z_blksz) 1521 return; 1522 /* 1523 * If the file size is already greater than the current blocksize, 1524 * we will not grow. If there is more than one block in a file, 1525 * the blocksize cannot change. 1526 */ 1527 if (zp->z_blksz && zp->z_size > zp->z_blksz) 1528 return; 1529 1530 error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id, 1531 size, 0, tx); 1532 1533 if (error == ENOTSUP) 1534 return; 1535 ASSERT3U(error, ==, 0); 1536 1537 /* What blocksize did we actually get? */ 1538 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy); 1539} 1540 1541#ifdef sun 1542/* 1543 * This is a dummy interface used when pvn_vplist_dirty() should *not* 1544 * be calling back into the fs for a putpage(). E.g.: when truncating 1545 * a file, the pages being "thrown away* don't need to be written out. 1546 */ 1547/* ARGSUSED */ 1548static int 1549zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp, 1550 int flags, cred_t *cr) 1551{ 1552 ASSERT(0); 1553 return (0); 1554} 1555#endif /* sun */ 1556 1557/* 1558 * Increase the file length 1559 * 1560 * IN: zp - znode of file to free data in. 1561 * end - new end-of-file 1562 * 1563 * RETURN: 0 if success 1564 * error code if failure 1565 */ 1566static int 1567zfs_extend(znode_t *zp, uint64_t end) 1568{ 1569 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1570 dmu_tx_t *tx; 1571 rl_t *rl; 1572 uint64_t newblksz; 1573 int error; 1574 1575 /* 1576 * We will change zp_size, lock the whole file. 1577 */ 1578 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER); 1579 1580 /* 1581 * Nothing to do if file already at desired length. 1582 */ 1583 if (end <= zp->z_size) { 1584 zfs_range_unlock(rl); 1585 return (0); 1586 } 1587top: 1588 tx = dmu_tx_create(zfsvfs->z_os); 1589 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1590 zfs_sa_upgrade_txholds(tx, zp); 1591 if (end > zp->z_blksz && 1592 (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) { 1593 /* 1594 * We are growing the file past the current block size. 1595 */ 1596 if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) { 1597 ASSERT(!ISP2(zp->z_blksz)); 1598 newblksz = MIN(end, SPA_MAXBLOCKSIZE); 1599 } else { 1600 newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz); 1601 } 1602 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz); 1603 } else { 1604 newblksz = 0; 1605 } 1606 1607 error = dmu_tx_assign(tx, TXG_NOWAIT); 1608 if (error) { 1609 if (error == ERESTART) { 1610 dmu_tx_wait(tx); 1611 dmu_tx_abort(tx); 1612 goto top; 1613 } 1614 dmu_tx_abort(tx); 1615 zfs_range_unlock(rl); 1616 return (error); 1617 } 1618 1619 if (newblksz) 1620 zfs_grow_blocksize(zp, newblksz, tx); 1621 1622 zp->z_size = end; 1623 1624 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs), 1625 &zp->z_size, sizeof (zp->z_size), tx)); 1626 1627 vnode_pager_setsize(ZTOV(zp), end); 1628 1629 zfs_range_unlock(rl); 1630 1631 dmu_tx_commit(tx); 1632 1633 return (0); 1634} 1635 1636/* 1637 * Free space in a file. 1638 * 1639 * IN: zp - znode of file to free data in. 1640 * off - start of section to free. 1641 * len - length of section to free. 1642 * 1643 * RETURN: 0 if success 1644 * error code if failure 1645 */ 1646static int 1647zfs_free_range(znode_t *zp, uint64_t off, uint64_t len) 1648{ 1649 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1650 rl_t *rl; 1651 int error; 1652 1653 /* 1654 * Lock the range being freed. 1655 */ 1656 rl = zfs_range_lock(zp, off, len, RL_WRITER); 1657 1658 /* 1659 * Nothing to do if file already at desired length. 1660 */ 1661 if (off >= zp->z_size) { 1662 zfs_range_unlock(rl); 1663 return (0); 1664 } 1665 1666 if (off + len > zp->z_size) 1667 len = zp->z_size - off; 1668 1669 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len); 1670 1671 if (error == 0) { 1672 /* 1673 * In FreeBSD we cannot free block in the middle of a file, 1674 * but only at the end of a file, so this code path should 1675 * never happen. 1676 */ 1677 vnode_pager_setsize(ZTOV(zp), off); 1678 } 1679 1680 zfs_range_unlock(rl); 1681 1682 return (error); 1683} 1684 1685/* 1686 * Truncate a file 1687 * 1688 * IN: zp - znode of file to free data in. 1689 * end - new end-of-file. 1690 * 1691 * RETURN: 0 if success 1692 * error code if failure 1693 */ 1694static int 1695zfs_trunc(znode_t *zp, uint64_t end) 1696{ 1697 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1698 vnode_t *vp = ZTOV(zp); 1699 dmu_tx_t *tx; 1700 rl_t *rl; 1701 int error; 1702 sa_bulk_attr_t bulk[2]; 1703 int count = 0; 1704 1705 /* 1706 * We will change zp_size, lock the whole file. 1707 */ 1708 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER); 1709 1710 /* 1711 * Nothing to do if file already at desired length. 1712 */ 1713 if (end >= zp->z_size) { 1714 zfs_range_unlock(rl); 1715 return (0); 1716 } 1717 1718 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end, -1); 1719 if (error) { 1720 zfs_range_unlock(rl); 1721 return (error); 1722 } 1723top: 1724 tx = dmu_tx_create(zfsvfs->z_os); 1725 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1726 zfs_sa_upgrade_txholds(tx, zp); 1727 error = dmu_tx_assign(tx, TXG_NOWAIT); 1728 if (error) { 1729 if (error == ERESTART) { 1730 dmu_tx_wait(tx); 1731 dmu_tx_abort(tx); 1732 goto top; 1733 } 1734 dmu_tx_abort(tx); 1735 zfs_range_unlock(rl); 1736 return (error); 1737 } 1738 1739 zp->z_size = end; 1740 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), 1741 NULL, &zp->z_size, sizeof (zp->z_size)); 1742 1743 if (end == 0) { 1744 zp->z_pflags &= ~ZFS_SPARSE; 1745 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), 1746 NULL, &zp->z_pflags, 8); 1747 } 1748 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0); 1749 1750 dmu_tx_commit(tx); 1751 1752 /* 1753 * Clear any mapped pages in the truncated region. This has to 1754 * happen outside of the transaction to avoid the possibility of 1755 * a deadlock with someone trying to push a page that we are 1756 * about to invalidate. 1757 */ 1758 vnode_pager_setsize(vp, end); 1759 1760 zfs_range_unlock(rl); 1761 1762 return (0); 1763} 1764 1765/* 1766 * Free space in a file 1767 * 1768 * IN: zp - znode of file to free data in. 1769 * off - start of range 1770 * len - end of range (0 => EOF) 1771 * flag - current file open mode flags. 1772 * log - TRUE if this action should be logged 1773 * 1774 * RETURN: 0 if success 1775 * error code if failure 1776 */ 1777int 1778zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log) 1779{ 1780 vnode_t *vp = ZTOV(zp); 1781 dmu_tx_t *tx; 1782 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1783 zilog_t *zilog = zfsvfs->z_log; 1784 uint64_t mode; 1785 uint64_t mtime[2], ctime[2]; 1786 sa_bulk_attr_t bulk[3]; 1787 int count = 0; 1788 int error; 1789 1790 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode, 1791 sizeof (mode))) != 0) 1792 return (error); 1793 1794 if (off > zp->z_size) { 1795 error = zfs_extend(zp, off+len); 1796 if (error == 0 && log) 1797 goto log; 1798 else 1799 return (error); 1800 } 1801 1802 /* 1803 * Check for any locks in the region to be freed. 1804 */ 1805 1806 if (MANDLOCK(vp, (mode_t)mode)) { 1807 uint64_t length = (len ? len : zp->z_size - off); 1808 if (error = chklock(vp, FWRITE, off, length, flag, NULL)) 1809 return (error); 1810 } 1811 1812 if (len == 0) { 1813 error = zfs_trunc(zp, off); 1814 } else { 1815 if ((error = zfs_free_range(zp, off, len)) == 0 && 1816 off + len > zp->z_size) 1817 error = zfs_extend(zp, off+len); 1818 } 1819 if (error || !log) 1820 return (error); 1821log: 1822 tx = dmu_tx_create(zfsvfs->z_os); 1823 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1824 zfs_sa_upgrade_txholds(tx, zp); 1825 error = dmu_tx_assign(tx, TXG_NOWAIT); 1826 if (error) { 1827 if (error == ERESTART) { 1828 dmu_tx_wait(tx); 1829 dmu_tx_abort(tx); 1830 goto log; 1831 } 1832 dmu_tx_abort(tx); 1833 return (error); 1834 } 1835 1836 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16); 1837 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16); 1838 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), 1839 NULL, &zp->z_pflags, 8); 1840 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE); 1841 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 1842 ASSERT(error == 0); 1843 1844 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len); 1845 1846 dmu_tx_commit(tx); 1847 return (0); 1848} 1849 1850void 1851zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx) 1852{ 1853 zfsvfs_t zfsvfs; 1854 uint64_t moid, obj, sa_obj, version; 1855 uint64_t sense = ZFS_CASE_SENSITIVE; 1856 uint64_t norm = 0; 1857 nvpair_t *elem; 1858 int error; 1859 int i; 1860 znode_t *rootzp = NULL; 1861 vnode_t vnode; 1862 vattr_t vattr; 1863 znode_t *zp; 1864 zfs_acl_ids_t acl_ids; 1865 1866 /* 1867 * First attempt to create master node. 1868 */ 1869 /* 1870 * In an empty objset, there are no blocks to read and thus 1871 * there can be no i/o errors (which we assert below). 1872 */ 1873 moid = MASTER_NODE_OBJ; 1874 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE, 1875 DMU_OT_NONE, 0, tx); 1876 ASSERT(error == 0); 1877 1878 /* 1879 * Set starting attributes. 1880 */ 1881 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os))); 1882 elem = NULL; 1883 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) { 1884 /* For the moment we expect all zpl props to be uint64_ts */ 1885 uint64_t val; 1886 char *name; 1887 1888 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64); 1889 VERIFY(nvpair_value_uint64(elem, &val) == 0); 1890 name = nvpair_name(elem); 1891 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) { 1892 if (val < version) 1893 version = val; 1894 } else { 1895 error = zap_update(os, moid, name, 8, 1, &val, tx); 1896 } 1897 ASSERT(error == 0); 1898 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0) 1899 norm = val; 1900 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0) 1901 sense = val; 1902 } 1903 ASSERT(version != 0); 1904 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx); 1905 1906 /* 1907 * Create zap object used for SA attribute registration 1908 */ 1909 1910 if (version >= ZPL_VERSION_SA) { 1911 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE, 1912 DMU_OT_NONE, 0, tx); 1913 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx); 1914 ASSERT(error == 0); 1915 } else { 1916 sa_obj = 0; 1917 } 1918 /* 1919 * Create a delete queue. 1920 */ 1921 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx); 1922 1923 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx); 1924 ASSERT(error == 0); 1925 1926 /* 1927 * Create root znode. Create minimal znode/vnode/zfsvfs 1928 * to allow zfs_mknode to work. 1929 */ 1930 VATTR_NULL(&vattr); 1931 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 1932 vattr.va_type = VDIR; 1933 vattr.va_mode = S_IFDIR|0755; 1934 vattr.va_uid = crgetuid(cr); 1935 vattr.va_gid = crgetgid(cr); 1936 1937 bzero(&zfsvfs, sizeof (zfsvfs_t)); 1938 1939 rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP); 1940 zfs_znode_cache_constructor(rootzp, NULL, 0); 1941 ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs)); 1942 rootzp->z_moved = 0; 1943 rootzp->z_unlinked = 0; 1944 rootzp->z_atime_dirty = 0; 1945 rootzp->z_is_sa = USE_SA(version, os); 1946 1947 vnode.v_type = VDIR; 1948 vnode.v_data = rootzp; 1949 rootzp->z_vnode = &vnode; 1950 1951 zfsvfs.z_os = os; 1952 zfsvfs.z_parent = &zfsvfs; 1953 zfsvfs.z_version = version; 1954 zfsvfs.z_use_fuids = USE_FUIDS(version, os); 1955 zfsvfs.z_use_sa = USE_SA(version, os); 1956 zfsvfs.z_norm = norm; 1957 1958 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END, 1959 &zfsvfs.z_attr_table); 1960 1961 ASSERT(error == 0); 1962 1963 /* 1964 * Fold case on file systems that are always or sometimes case 1965 * insensitive. 1966 */ 1967 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED) 1968 zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER; 1969 1970 mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL); 1971 list_create(&zfsvfs.z_all_znodes, sizeof (znode_t), 1972 offsetof(znode_t, z_link_node)); 1973 1974 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) 1975 mutex_init(&zfsvfs.z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL); 1976 1977 rootzp->z_zfsvfs = &zfsvfs; 1978 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr, 1979 cr, NULL, &acl_ids)); 1980 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids); 1981 ASSERT3P(zp, ==, rootzp); 1982 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx); 1983 ASSERT(error == 0); 1984 zfs_acl_ids_free(&acl_ids); 1985 POINTER_INVALIDATE(&rootzp->z_zfsvfs); 1986 1987 sa_handle_destroy(rootzp->z_sa_hdl); 1988 rootzp->z_vnode = NULL; 1989 kmem_cache_free(znode_cache, rootzp); 1990 1991 /* 1992 * Create shares directory 1993 */ 1994 1995 error = zfs_create_share_dir(&zfsvfs, tx); 1996 1997 ASSERT(error == 0); 1998 1999 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) 2000 mutex_destroy(&zfsvfs.z_hold_mtx[i]); 2001} 2002 2003#endif /* _KERNEL */ 2004 2005static int 2006zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table) 2007{ 2008 uint64_t sa_obj = 0; 2009 int error; 2010 2011 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj); 2012 if (error != 0 && error != ENOENT) 2013 return (error); 2014 2015 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table); 2016 return (error); 2017} 2018 2019static int 2020zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp, 2021 dmu_buf_t **db, void *tag) 2022{ 2023 dmu_object_info_t doi; 2024 int error; 2025 2026 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0) 2027 return (error); 2028 2029 dmu_object_info_from_db(*db, &doi); 2030 if ((doi.doi_bonus_type != DMU_OT_SA && 2031 doi.doi_bonus_type != DMU_OT_ZNODE) || 2032 doi.doi_bonus_type == DMU_OT_ZNODE && 2033 doi.doi_bonus_size < sizeof (znode_phys_t)) { 2034 sa_buf_rele(*db, tag); 2035 return (ENOTSUP); 2036 } 2037 2038 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp); 2039 if (error != 0) { 2040 sa_buf_rele(*db, tag); 2041 return (error); 2042 } 2043 2044 return (0); 2045} 2046 2047void 2048zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag) 2049{ 2050 sa_handle_destroy(hdl); 2051 sa_buf_rele(db, tag); 2052} 2053 2054/* 2055 * Given an object number, return its parent object number and whether 2056 * or not the object is an extended attribute directory. 2057 */ 2058static int 2059zfs_obj_to_pobj(sa_handle_t *hdl, sa_attr_type_t *sa_table, uint64_t *pobjp, 2060 int *is_xattrdir) 2061{ 2062 uint64_t parent; 2063 uint64_t pflags; 2064 uint64_t mode; 2065 sa_bulk_attr_t bulk[3]; 2066 int count = 0; 2067 int error; 2068 2069 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL, 2070 &parent, sizeof (parent)); 2071 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL, 2072 &pflags, sizeof (pflags)); 2073 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL, 2074 &mode, sizeof (mode)); 2075 2076 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0) 2077 return (error); 2078 2079 *pobjp = parent; 2080 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode); 2081 2082 return (0); 2083} 2084 2085/* 2086 * Given an object number, return some zpl level statistics 2087 */ 2088static int 2089zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table, 2090 zfs_stat_t *sb) 2091{ 2092 sa_bulk_attr_t bulk[4]; 2093 int count = 0; 2094 2095 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL, 2096 &sb->zs_mode, sizeof (sb->zs_mode)); 2097 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL, 2098 &sb->zs_gen, sizeof (sb->zs_gen)); 2099 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL, 2100 &sb->zs_links, sizeof (sb->zs_links)); 2101 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL, 2102 &sb->zs_ctime, sizeof (sb->zs_ctime)); 2103 2104 return (sa_bulk_lookup(hdl, bulk, count)); 2105} 2106 2107static int 2108zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl, 2109 sa_attr_type_t *sa_table, char *buf, int len) 2110{ 2111 sa_handle_t *sa_hdl; 2112 sa_handle_t *prevhdl = NULL; 2113 dmu_buf_t *prevdb = NULL; 2114 dmu_buf_t *sa_db = NULL; 2115 char *path = buf + len - 1; 2116 int error; 2117 2118 *path = '\0'; 2119 sa_hdl = hdl; 2120 2121 for (;;) { 2122 uint64_t pobj; 2123 char component[MAXNAMELEN + 2]; 2124 size_t complen; 2125 int is_xattrdir; 2126 2127 if (prevdb) 2128 zfs_release_sa_handle(prevhdl, prevdb, FTAG); 2129 2130 if ((error = zfs_obj_to_pobj(sa_hdl, sa_table, &pobj, 2131 &is_xattrdir)) != 0) 2132 break; 2133 2134 if (pobj == obj) { 2135 if (path[0] != '/') 2136 *--path = '/'; 2137 break; 2138 } 2139 2140 component[0] = '/'; 2141 if (is_xattrdir) { 2142 (void) sprintf(component + 1, "<xattrdir>"); 2143 } else { 2144 error = zap_value_search(osp, pobj, obj, 2145 ZFS_DIRENT_OBJ(-1ULL), component + 1); 2146 if (error != 0) 2147 break; 2148 } 2149 2150 complen = strlen(component); 2151 path -= complen; 2152 ASSERT(path >= buf); 2153 bcopy(component, path, complen); 2154 obj = pobj; 2155 2156 if (sa_hdl != hdl) { 2157 prevhdl = sa_hdl; 2158 prevdb = sa_db; 2159 } 2160 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG); 2161 if (error != 0) { 2162 sa_hdl = prevhdl; 2163 sa_db = prevdb; 2164 break; 2165 } 2166 } 2167 2168 if (sa_hdl != NULL && sa_hdl != hdl) { 2169 ASSERT(sa_db != NULL); 2170 zfs_release_sa_handle(sa_hdl, sa_db, FTAG); 2171 } 2172 2173 if (error == 0) 2174 (void) memmove(buf, path, buf + len - path); 2175 2176 return (error); 2177} 2178 2179int 2180zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len) 2181{ 2182 sa_attr_type_t *sa_table; 2183 sa_handle_t *hdl; 2184 dmu_buf_t *db; 2185 int error; 2186 2187 error = zfs_sa_setup(osp, &sa_table); 2188 if (error != 0) 2189 return (error); 2190 2191 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG); 2192 if (error != 0) 2193 return (error); 2194 2195 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len); 2196 2197 zfs_release_sa_handle(hdl, db, FTAG); 2198 return (error); 2199} 2200 2201int 2202zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb, 2203 char *buf, int len) 2204{ 2205 char *path = buf + len - 1; 2206 sa_attr_type_t *sa_table; 2207 sa_handle_t *hdl; 2208 dmu_buf_t *db; 2209 int error; 2210 2211 *path = '\0'; 2212 2213 error = zfs_sa_setup(osp, &sa_table); 2214 if (error != 0) 2215 return (error); 2216 2217 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG); 2218 if (error != 0) 2219 return (error); 2220 2221 error = zfs_obj_to_stats_impl(hdl, sa_table, sb); 2222 if (error != 0) { 2223 zfs_release_sa_handle(hdl, db, FTAG); 2224 return (error); 2225 } 2226 2227 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len); 2228 2229 zfs_release_sa_handle(hdl, db, FTAG); 2230 return (error); 2231} 2232