zfs_znode.c revision 182781
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 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26/* Portions Copyright 2007 Jeremy Teo */ 27 28#pragma ident "%Z%%M% %I% %E% SMI" 29 30#ifdef _KERNEL 31#include <sys/types.h> 32#include <sys/param.h> 33#include <sys/time.h> 34#include <sys/systm.h> 35#include <sys/sysmacros.h> 36#include <sys/resource.h> 37#include <sys/mntent.h> 38#include <sys/vfs.h> 39#include <sys/vnode.h> 40#include <sys/file.h> 41#include <sys/kmem.h> 42#include <sys/cmn_err.h> 43#include <sys/errno.h> 44#include <sys/unistd.h> 45#include <sys/atomic.h> 46#include <sys/zfs_dir.h> 47#include <sys/zfs_acl.h> 48#include <sys/zfs_ioctl.h> 49#include <sys/zfs_rlock.h> 50#include <sys/fs/zfs.h> 51#endif /* _KERNEL */ 52 53#include <sys/dmu.h> 54#include <sys/refcount.h> 55#include <sys/stat.h> 56#include <sys/zap.h> 57#include <sys/zfs_znode.h> 58#include <sys/refcount.h> 59 60/* Used by fstat(1). */ 61SYSCTL_INT(_debug_sizeof, OID_AUTO, znode, CTLFLAG_RD, 0, sizeof(znode_t), 62 "sizeof(znode_t)"); 63 64/* 65 * Functions needed for userland (ie: libzpool) are not put under 66 * #ifdef_KERNEL; the rest of the functions have dependencies 67 * (such as VFS logic) that will not compile easily in userland. 68 */ 69#ifdef _KERNEL 70struct kmem_cache *znode_cache = NULL; 71 72/*ARGSUSED*/ 73static void 74znode_pageout_func(dmu_buf_t *dbuf, void *user_ptr) 75{ 76 znode_t *zp = user_ptr; 77 vnode_t *vp; 78 79 mutex_enter(&zp->z_lock); 80 vp = ZTOV(zp); 81 if (vp == NULL) { 82 mutex_exit(&zp->z_lock); 83 zfs_znode_free(zp); 84 } else if (vp->v_count == 0) { 85 ZTOV(zp) = NULL; 86 vhold(vp); 87 mutex_exit(&zp->z_lock); 88 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 89 vrecycle(vp, curthread); 90 VOP_UNLOCK(vp, 0); 91 vdrop(vp); 92 zfs_znode_free(zp); 93 } else { 94 /* signal force unmount that this znode can be freed */ 95 zp->z_dbuf = NULL; 96 mutex_exit(&zp->z_lock); 97 } 98} 99 100extern struct vop_vector zfs_vnodeops; 101extern struct vop_vector zfs_fifoops; 102 103/* 104 * XXX: We cannot use this function as a cache constructor, because 105 * there is one global cache for all file systems and we need 106 * to pass vfsp here, which is not possible, because argument 107 * 'cdrarg' is defined at kmem_cache_create() time. 108 */ 109static int 110zfs_znode_cache_constructor(void *buf, void *cdrarg, int kmflags) 111{ 112 znode_t *zp = buf; 113 vnode_t *vp; 114 vfs_t *vfsp = cdrarg; 115 int error; 116 117 if (cdrarg != NULL) { 118 error = getnewvnode("zfs", vfsp, &zfs_vnodeops, &vp); 119 ASSERT(error == 0); 120 zp->z_vnode = vp; 121 vp->v_data = (caddr_t)zp; 122 VN_LOCK_AREC(vp); 123 VN_LOCK_ASHARE(vp); 124 } else { 125 zp->z_vnode = NULL; 126 } 127 mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL); 128 rw_init(&zp->z_map_lock, NULL, RW_DEFAULT, NULL); 129 rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL); 130 rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL); 131 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL); 132 133 mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL); 134 avl_create(&zp->z_range_avl, zfs_range_compare, 135 sizeof (rl_t), offsetof(rl_t, r_node)); 136 137 zp->z_dbuf_held = 0; 138 zp->z_dirlocks = 0; 139 return (0); 140} 141 142/*ARGSUSED*/ 143static void 144zfs_znode_cache_destructor(void *buf, void *cdarg) 145{ 146 znode_t *zp = buf; 147 148 ASSERT(zp->z_dirlocks == 0); 149 mutex_destroy(&zp->z_lock); 150 rw_destroy(&zp->z_map_lock); 151 rw_destroy(&zp->z_parent_lock); 152 rw_destroy(&zp->z_name_lock); 153 mutex_destroy(&zp->z_acl_lock); 154 mutex_destroy(&zp->z_range_lock); 155 avl_destroy(&zp->z_range_avl); 156 157 ASSERT(zp->z_dbuf_held == 0); 158} 159 160void 161zfs_znode_init(void) 162{ 163 /* 164 * Initialize zcache 165 */ 166 ASSERT(znode_cache == NULL); 167 znode_cache = kmem_cache_create("zfs_znode_cache", 168 sizeof (znode_t), 0, /* zfs_znode_cache_constructor */ NULL, 169 zfs_znode_cache_destructor, NULL, NULL, NULL, 0); 170} 171 172void 173zfs_znode_fini(void) 174{ 175 /* 176 * Cleanup zcache 177 */ 178 if (znode_cache) 179 kmem_cache_destroy(znode_cache); 180 znode_cache = NULL; 181} 182 183/* 184 * zfs_init_fs - Initialize the zfsvfs struct and the file system 185 * incore "master" object. Verify version compatibility. 186 */ 187int 188zfs_init_fs(zfsvfs_t *zfsvfs, znode_t **zpp, cred_t *cr) 189{ 190 objset_t *os = zfsvfs->z_os; 191 uint64_t version = ZPL_VERSION; 192 int i, error; 193 dmu_object_info_t doi; 194 uint64_t fsid_guid; 195 196 *zpp = NULL; 197 198 /* 199 * XXX - hack to auto-create the pool root filesystem at 200 * the first attempted mount. 201 */ 202 if (dmu_object_info(os, MASTER_NODE_OBJ, &doi) == ENOENT) { 203 dmu_tx_t *tx = dmu_tx_create(os); 204 205 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); /* master */ 206 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); /* del queue */ 207 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); /* root node */ 208 error = dmu_tx_assign(tx, TXG_WAIT); 209 ASSERT3U(error, ==, 0); 210 zfs_create_fs(os, cr, tx); 211 dmu_tx_commit(tx); 212 } 213 214 error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_OBJ, 8, 1, 215 &version); 216 if (error) { 217 return (error); 218 } else if (version != ZPL_VERSION) { 219 (void) printf("Mismatched versions: File system " 220 "is version %lld on-disk format, which is " 221 "incompatible with this software version %lld!", 222 (u_longlong_t)version, ZPL_VERSION); 223 return (ENOTSUP); 224 } 225 226 /* 227 * The fsid is 64 bits, composed of an 8-bit fs type, which 228 * separates our fsid from any other filesystem types, and a 229 * 56-bit objset unique ID. The objset unique ID is unique to 230 * all objsets open on this system, provided by unique_create(). 231 * The 8-bit fs type must be put in the low bits of fsid[1] 232 * because that's where other Solaris filesystems put it. 233 */ 234 fsid_guid = dmu_objset_fsid_guid(os); 235 ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0); 236 zfsvfs->z_vfs->vfs_fsid.val[0] = fsid_guid; 237 zfsvfs->z_vfs->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) | 238 zfsvfs->z_vfs->mnt_vfc->vfc_typenum & 0xFF; 239 240 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, 241 &zfsvfs->z_root); 242 if (error) 243 return (error); 244 ASSERT(zfsvfs->z_root != 0); 245 246 /* 247 * Create the per mount vop tables. 248 */ 249 250 /* 251 * Initialize zget mutex's 252 */ 253 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) 254 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL); 255 256 error = zfs_zget(zfsvfs, zfsvfs->z_root, zpp); 257 if (error) 258 return (error); 259 ASSERT3U((*zpp)->z_id, ==, zfsvfs->z_root); 260 261 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1, 262 &zfsvfs->z_unlinkedobj); 263 if (error) 264 return (error); 265 266 return (0); 267} 268 269/* 270 * define a couple of values we need available 271 * for both 64 and 32 bit environments. 272 */ 273#ifndef NBITSMINOR64 274#define NBITSMINOR64 32 275#endif 276#ifndef MAXMAJ64 277#define MAXMAJ64 0xffffffffUL 278#endif 279#ifndef MAXMIN64 280#define MAXMIN64 0xffffffffUL 281#endif 282 283/* 284 * Create special expldev for ZFS private use. 285 * Can't use standard expldev since it doesn't do 286 * what we want. The standard expldev() takes a 287 * dev32_t in LP64 and expands it to a long dev_t. 288 * We need an interface that takes a dev32_t in ILP32 289 * and expands it to a long dev_t. 290 */ 291static uint64_t 292zfs_expldev(dev_t dev) 293{ 294 return (((uint64_t)umajor(dev) << NBITSMINOR64) | uminor(dev)); 295} 296/* 297 * Special cmpldev for ZFS private use. 298 * Can't use standard cmpldev since it takes 299 * a long dev_t and compresses it to dev32_t in 300 * LP64. We need to do a compaction of a long dev_t 301 * to a dev32_t in ILP32. 302 */ 303dev_t 304zfs_cmpldev(uint64_t dev) 305{ 306 return (makedev((dev >> NBITSMINOR64), (dev & MAXMIN64))); 307} 308 309/* 310 * Construct a new znode/vnode and intialize. 311 * 312 * This does not do a call to dmu_set_user() that is 313 * up to the caller to do, in case you don't want to 314 * return the znode 315 */ 316static znode_t * 317zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, uint64_t obj_num, int blksz) 318{ 319 znode_t *zp; 320 vnode_t *vp; 321 int error; 322 323 zp = kmem_cache_alloc(znode_cache, KM_SLEEP); 324 zfs_znode_cache_constructor(zp, zfsvfs->z_vfs, 0); 325 326 ASSERT(zp->z_dirlocks == NULL); 327 328 zp->z_phys = db->db_data; 329 zp->z_zfsvfs = zfsvfs; 330 zp->z_unlinked = 0; 331 zp->z_atime_dirty = 0; 332 zp->z_dbuf_held = 0; 333 zp->z_mapcnt = 0; 334 zp->z_last_itx = 0; 335 zp->z_dbuf = db; 336 zp->z_id = obj_num; 337 zp->z_blksz = blksz; 338 zp->z_seq = 0x7A4653; 339 zp->z_sync_cnt = 0; 340 341 mutex_enter(&zfsvfs->z_znodes_lock); 342 list_insert_tail(&zfsvfs->z_all_znodes, zp); 343 mutex_exit(&zfsvfs->z_znodes_lock); 344 345 vp = ZTOV(zp); 346 if (vp == NULL) 347 return (zp); 348 349 vp->v_vflag |= VV_FORCEINSMQ; 350 error = insmntque(vp, zfsvfs->z_vfs); 351 vp->v_vflag &= ~VV_FORCEINSMQ; 352 KASSERT(error == 0, ("insmntque() failed: error %d", error)); 353 354 vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode); 355 switch (vp->v_type) { 356 case VDIR: 357 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */ 358 break; 359 case VFIFO: 360 vp->v_op = &zfs_fifoops; 361 break; 362 } 363 364 return (zp); 365} 366 367static void 368zfs_znode_dmu_init(znode_t *zp) 369{ 370 znode_t *nzp; 371 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 372 dmu_buf_t *db = zp->z_dbuf; 373 374 mutex_enter(&zp->z_lock); 375 376 nzp = dmu_buf_set_user_ie(db, zp, &zp->z_phys, znode_pageout_func); 377 378 /* 379 * there should be no 380 * concurrent zgets on this object. 381 */ 382 ASSERT3P(nzp, ==, NULL); 383 384 /* 385 * Slap on VROOT if we are the root znode 386 */ 387 if (zp->z_id == zfsvfs->z_root) { 388 ZTOV(zp)->v_flag |= VROOT; 389 } 390 391 ASSERT(zp->z_dbuf_held == 0); 392 zp->z_dbuf_held = 1; 393 VFS_HOLD(zfsvfs->z_vfs); 394 mutex_exit(&zp->z_lock); 395} 396 397/* 398 * Create a new DMU object to hold a zfs znode. 399 * 400 * IN: dzp - parent directory for new znode 401 * vap - file attributes for new znode 402 * tx - dmu transaction id for zap operations 403 * cr - credentials of caller 404 * flag - flags: 405 * IS_ROOT_NODE - new object will be root 406 * IS_XATTR - new object is an attribute 407 * IS_REPLAY - intent log replay 408 * 409 * OUT: oid - ID of created object 410 * 411 */ 412void 413zfs_mknode(znode_t *dzp, vattr_t *vap, uint64_t *oid, dmu_tx_t *tx, cred_t *cr, 414 uint_t flag, znode_t **zpp, int bonuslen) 415{ 416 dmu_buf_t *dbp; 417 znode_phys_t *pzp; 418 znode_t *zp; 419 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 420 timestruc_t now; 421 uint64_t gen; 422 int err; 423 424 ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE)); 425 426 if (zfsvfs->z_assign >= TXG_INITIAL) { /* ZIL replay */ 427 *oid = vap->va_nodeid; 428 flag |= IS_REPLAY; 429 now = vap->va_ctime; /* see zfs_replay_create() */ 430 gen = vap->va_nblocks; /* ditto */ 431 } else { 432 *oid = 0; 433 gethrestime(&now); 434 gen = dmu_tx_get_txg(tx); 435 } 436 437 /* 438 * Create a new DMU object. 439 */ 440 /* 441 * There's currently no mechanism for pre-reading the blocks that will 442 * be to needed allocate a new object, so we accept the small chance 443 * that there will be an i/o error and we will fail one of the 444 * assertions below. 445 */ 446 if (vap->va_type == VDIR) { 447 if (flag & IS_REPLAY) { 448 err = zap_create_claim(zfsvfs->z_os, *oid, 449 DMU_OT_DIRECTORY_CONTENTS, 450 DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx); 451 ASSERT3U(err, ==, 0); 452 } else { 453 *oid = zap_create(zfsvfs->z_os, 454 DMU_OT_DIRECTORY_CONTENTS, 455 DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx); 456 } 457 } else { 458 if (flag & IS_REPLAY) { 459 err = dmu_object_claim(zfsvfs->z_os, *oid, 460 DMU_OT_PLAIN_FILE_CONTENTS, 0, 461 DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx); 462 ASSERT3U(err, ==, 0); 463 } else { 464 *oid = dmu_object_alloc(zfsvfs->z_os, 465 DMU_OT_PLAIN_FILE_CONTENTS, 0, 466 DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx); 467 } 468 } 469 VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, *oid, NULL, &dbp)); 470 dmu_buf_will_dirty(dbp, tx); 471 472 /* 473 * Initialize the znode physical data to zero. 474 */ 475 ASSERT(dbp->db_size >= sizeof (znode_phys_t)); 476 bzero(dbp->db_data, dbp->db_size); 477 pzp = dbp->db_data; 478 479 /* 480 * If this is the root, fix up the half-initialized parent pointer 481 * to reference the just-allocated physical data area. 482 */ 483 if (flag & IS_ROOT_NODE) { 484 dzp->z_phys = pzp; 485 dzp->z_id = *oid; 486 } 487 488 /* 489 * If parent is an xattr, so am I. 490 */ 491 if (dzp->z_phys->zp_flags & ZFS_XATTR) 492 flag |= IS_XATTR; 493 494 if (vap->va_type == VBLK || vap->va_type == VCHR) { 495 pzp->zp_rdev = zfs_expldev(vap->va_rdev); 496 } 497 498 if (vap->va_type == VDIR) { 499 pzp->zp_size = 2; /* contents ("." and "..") */ 500 pzp->zp_links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1; 501 } 502 503 pzp->zp_parent = dzp->z_id; 504 if (flag & IS_XATTR) 505 pzp->zp_flags |= ZFS_XATTR; 506 507 pzp->zp_gen = gen; 508 509 ZFS_TIME_ENCODE(&now, pzp->zp_crtime); 510 ZFS_TIME_ENCODE(&now, pzp->zp_ctime); 511 512 if (vap->va_mask & AT_ATIME) { 513 ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime); 514 } else { 515 ZFS_TIME_ENCODE(&now, pzp->zp_atime); 516 } 517 518 if (vap->va_mask & AT_MTIME) { 519 ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime); 520 } else { 521 ZFS_TIME_ENCODE(&now, pzp->zp_mtime); 522 } 523 524 pzp->zp_mode = MAKEIMODE(vap->va_type, vap->va_mode); 525 zp = zfs_znode_alloc(zfsvfs, dbp, *oid, 0); 526 527 zfs_perm_init(zp, dzp, flag, vap, tx, cr); 528 529 if (zpp) { 530 kmutex_t *hash_mtx = ZFS_OBJ_MUTEX(zp); 531 532 mutex_enter(hash_mtx); 533 zfs_znode_dmu_init(zp); 534 mutex_exit(hash_mtx); 535 536 *zpp = zp; 537 } else { 538 if (ZTOV(zp) != NULL) 539 ZTOV(zp)->v_count = 0; 540 dmu_buf_rele(dbp, NULL); 541 zfs_znode_free(zp); 542 } 543} 544 545int 546zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp) 547{ 548 dmu_object_info_t doi; 549 dmu_buf_t *db; 550 znode_t *zp; 551 vnode_t *vp; 552 int err; 553 554 *zpp = NULL; 555 556 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num); 557 558 err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db); 559 if (err) { 560 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 561 return (err); 562 } 563 564 dmu_object_info_from_db(db, &doi); 565 if (doi.doi_bonus_type != DMU_OT_ZNODE || 566 doi.doi_bonus_size < sizeof (znode_phys_t)) { 567 dmu_buf_rele(db, NULL); 568 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 569 return (EINVAL); 570 } 571 572 ASSERT(db->db_object == obj_num); 573 ASSERT(db->db_offset == -1); 574 ASSERT(db->db_data != NULL); 575 576 zp = dmu_buf_get_user(db); 577 578 if (zp != NULL) { 579 mutex_enter(&zp->z_lock); 580 581 ASSERT3U(zp->z_id, ==, obj_num); 582 if (zp->z_unlinked) { 583 dmu_buf_rele(db, NULL); 584 mutex_exit(&zp->z_lock); 585 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 586 return (ENOENT); 587 } else if (zp->z_dbuf_held) { 588 dmu_buf_rele(db, NULL); 589 } else { 590 zp->z_dbuf_held = 1; 591 VFS_HOLD(zfsvfs->z_vfs); 592 } 593 594 if (ZTOV(zp) != NULL) 595 VN_HOLD(ZTOV(zp)); 596 else { 597 err = getnewvnode("zfs", zfsvfs->z_vfs, &zfs_vnodeops, 598 &zp->z_vnode); 599 ASSERT(err == 0); 600 vp = ZTOV(zp); 601 vp->v_data = (caddr_t)zp; 602 VN_LOCK_AREC(vp); 603 VN_LOCK_ASHARE(vp); 604 vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode); 605 if (vp->v_type == VDIR) 606 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */ 607 vp->v_vflag |= VV_FORCEINSMQ; 608 err = insmntque(vp, zfsvfs->z_vfs); 609 vp->v_vflag &= ~VV_FORCEINSMQ; 610 KASSERT(err == 0, ("insmntque() failed: error %d", err)); 611 } 612 mutex_exit(&zp->z_lock); 613 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 614 *zpp = zp; 615 return (0); 616 } 617 618 /* 619 * Not found create new znode/vnode 620 */ 621 zp = zfs_znode_alloc(zfsvfs, db, obj_num, doi.doi_data_block_size); 622 ASSERT3U(zp->z_id, ==, obj_num); 623 zfs_znode_dmu_init(zp); 624 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 625 *zpp = zp; 626 return (0); 627} 628 629void 630zfs_znode_delete(znode_t *zp, dmu_tx_t *tx) 631{ 632 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 633 int error; 634 635 ZFS_OBJ_HOLD_ENTER(zfsvfs, zp->z_id); 636 if (zp->z_phys->zp_acl.z_acl_extern_obj) { 637 error = dmu_object_free(zfsvfs->z_os, 638 zp->z_phys->zp_acl.z_acl_extern_obj, tx); 639 ASSERT3U(error, ==, 0); 640 } 641 error = dmu_object_free(zfsvfs->z_os, zp->z_id, tx); 642 ASSERT3U(error, ==, 0); 643 zp->z_dbuf_held = 0; 644 ZFS_OBJ_HOLD_EXIT(zfsvfs, zp->z_id); 645 dmu_buf_rele(zp->z_dbuf, NULL); 646} 647 648void 649zfs_zinactive(znode_t *zp) 650{ 651 vnode_t *vp = ZTOV(zp); 652 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 653 uint64_t z_id = zp->z_id; 654 655 ASSERT(zp->z_dbuf_held && zp->z_phys); 656 657 /* 658 * Don't allow a zfs_zget() while were trying to release this znode 659 */ 660 ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id); 661 662 mutex_enter(&zp->z_lock); 663 VI_LOCK(vp); 664 if (vp->v_count > 0) { 665 /* 666 * If the hold count is greater than zero, somebody has 667 * obtained a new reference on this znode while we were 668 * processing it here, so we are done. 669 */ 670 VI_UNLOCK(vp); 671 mutex_exit(&zp->z_lock); 672 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 673 return; 674 } 675 VI_UNLOCK(vp); 676 677 /* 678 * If this was the last reference to a file with no links, 679 * remove the file from the file system. 680 */ 681 if (zp->z_unlinked) { 682 ZTOV(zp) = NULL; 683 mutex_exit(&zp->z_lock); 684 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 685 ASSERT(vp->v_count == 0); 686 vrecycle(vp, curthread); 687 zfs_rmnode(zp); 688 VFS_RELE(zfsvfs->z_vfs); 689 return; 690 } 691 ASSERT(zp->z_phys); 692 ASSERT(zp->z_dbuf_held); 693 mutex_exit(&zp->z_lock); 694 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 695} 696 697void 698zfs_znode_free(znode_t *zp) 699{ 700 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 701 702 mutex_enter(&zfsvfs->z_znodes_lock); 703 list_remove(&zfsvfs->z_all_znodes, zp); 704 mutex_exit(&zfsvfs->z_znodes_lock); 705 706 kmem_cache_free(znode_cache, zp); 707} 708 709void 710zfs_time_stamper_locked(znode_t *zp, uint_t flag, dmu_tx_t *tx) 711{ 712 timestruc_t now; 713 714 ASSERT(MUTEX_HELD(&zp->z_lock)); 715 716 gethrestime(&now); 717 718 if (tx) { 719 dmu_buf_will_dirty(zp->z_dbuf, tx); 720 zp->z_atime_dirty = 0; 721 zp->z_seq++; 722 } else { 723 zp->z_atime_dirty = 1; 724 } 725 726 if (flag & AT_ATIME) 727 ZFS_TIME_ENCODE(&now, zp->z_phys->zp_atime); 728 729 if (flag & AT_MTIME) 730 ZFS_TIME_ENCODE(&now, zp->z_phys->zp_mtime); 731 732 if (flag & AT_CTIME) 733 ZFS_TIME_ENCODE(&now, zp->z_phys->zp_ctime); 734} 735 736/* 737 * Update the requested znode timestamps with the current time. 738 * If we are in a transaction, then go ahead and mark the znode 739 * dirty in the transaction so the timestamps will go to disk. 740 * Otherwise, we will get pushed next time the znode is updated 741 * in a transaction, or when this znode eventually goes inactive. 742 * 743 * Why is this OK? 744 * 1 - Only the ACCESS time is ever updated outside of a transaction. 745 * 2 - Multiple consecutive updates will be collapsed into a single 746 * znode update by the transaction grouping semantics of the DMU. 747 */ 748void 749zfs_time_stamper(znode_t *zp, uint_t flag, dmu_tx_t *tx) 750{ 751 mutex_enter(&zp->z_lock); 752 zfs_time_stamper_locked(zp, flag, tx); 753 mutex_exit(&zp->z_lock); 754} 755 756/* 757 * Grow the block size for a file. 758 * 759 * IN: zp - znode of file to free data in. 760 * size - requested block size 761 * tx - open transaction. 762 * 763 * NOTE: this function assumes that the znode is write locked. 764 */ 765void 766zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx) 767{ 768 int error; 769 u_longlong_t dummy; 770 771 if (size <= zp->z_blksz) 772 return; 773 /* 774 * If the file size is already greater than the current blocksize, 775 * we will not grow. If there is more than one block in a file, 776 * the blocksize cannot change. 777 */ 778 if (zp->z_blksz && zp->z_phys->zp_size > zp->z_blksz) 779 return; 780 781 error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id, 782 size, 0, tx); 783 if (error == ENOTSUP) 784 return; 785 ASSERT3U(error, ==, 0); 786 787 /* What blocksize did we actually get? */ 788 dmu_object_size_from_db(zp->z_dbuf, &zp->z_blksz, &dummy); 789} 790 791/* 792 * Free space in a file. 793 * 794 * IN: zp - znode of file to free data in. 795 * off - start of section to free. 796 * len - length of section to free (0 => to EOF). 797 * flag - current file open mode flags. 798 * 799 * RETURN: 0 if success 800 * error code if failure 801 */ 802int 803zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log) 804{ 805 vnode_t *vp = ZTOV(zp); 806 dmu_tx_t *tx; 807 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 808 zilog_t *zilog = zfsvfs->z_log; 809 rl_t *rl; 810 uint64_t end = off + len; 811 uint64_t size, new_blksz; 812 int error; 813 814 if (ZTOV(zp)->v_type == VFIFO) 815 return (0); 816 817 /* 818 * If we will change zp_size then lock the whole file, 819 * otherwise just lock the range being freed. 820 */ 821 if (len == 0 || off + len > zp->z_phys->zp_size) { 822 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER); 823 } else { 824 rl = zfs_range_lock(zp, off, len, RL_WRITER); 825 /* recheck, in case zp_size changed */ 826 if (off + len > zp->z_phys->zp_size) { 827 /* lost race: file size changed, lock whole file */ 828 zfs_range_unlock(rl); 829 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER); 830 } 831 } 832 833 /* 834 * Nothing to do if file already at desired length. 835 */ 836 size = zp->z_phys->zp_size; 837 if (len == 0 && size == off && off != 0) { 838 zfs_range_unlock(rl); 839 return (0); 840 } 841 842 tx = dmu_tx_create(zfsvfs->z_os); 843 dmu_tx_hold_bonus(tx, zp->z_id); 844 new_blksz = 0; 845 if (end > size && 846 (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) { 847 /* 848 * We are growing the file past the current block size. 849 */ 850 if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) { 851 ASSERT(!ISP2(zp->z_blksz)); 852 new_blksz = MIN(end, SPA_MAXBLOCKSIZE); 853 } else { 854 new_blksz = MIN(end, zp->z_zfsvfs->z_max_blksz); 855 } 856 dmu_tx_hold_write(tx, zp->z_id, 0, MIN(end, new_blksz)); 857 } else if (off < size) { 858 /* 859 * If len == 0, we are truncating the file. 860 */ 861 dmu_tx_hold_free(tx, zp->z_id, off, len ? len : DMU_OBJECT_END); 862 } 863 864 error = dmu_tx_assign(tx, zfsvfs->z_assign); 865 if (error) { 866 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) 867 dmu_tx_wait(tx); 868 dmu_tx_abort(tx); 869 zfs_range_unlock(rl); 870 return (error); 871 } 872 873 if (new_blksz) 874 zfs_grow_blocksize(zp, new_blksz, tx); 875 876 if (end > size || len == 0) 877 zp->z_phys->zp_size = end; 878 879 if (off < size) { 880 objset_t *os = zfsvfs->z_os; 881 uint64_t rlen = len; 882 883 if (len == 0) 884 rlen = -1; 885 else if (end > size) 886 rlen = size - off; 887 VERIFY(0 == dmu_free_range(os, zp->z_id, off, rlen, tx)); 888 } 889 890 if (log) { 891 zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 892 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len); 893 } 894 895 zfs_range_unlock(rl); 896 897 dmu_tx_commit(tx); 898 899 /* 900 * Clear any mapped pages in the truncated region. This has to 901 * happen outside of the transaction to avoid the possibility of 902 * a deadlock with someone trying to push a page that we are 903 * about to invalidate. 904 */ 905 rw_enter(&zp->z_map_lock, RW_WRITER); 906 if (end > size) 907 vnode_pager_setsize(vp, end); 908 else if (len == 0) { 909#if 0 910 error = vtruncbuf(vp, curthread->td_ucred, curthread, end, PAGE_SIZE); 911#else 912 error = vinvalbuf(vp, V_SAVE, curthread, 0, 0); 913 vnode_pager_setsize(vp, end); 914#endif 915 } 916 rw_exit(&zp->z_map_lock); 917 918 return (0); 919} 920 921void 922zfs_create_fs(objset_t *os, cred_t *cr, dmu_tx_t *tx) 923{ 924 zfsvfs_t zfsvfs; 925 uint64_t moid, doid, roid = 0; 926 uint64_t version = ZPL_VERSION; 927 int error; 928 znode_t *rootzp = NULL; 929 vattr_t vattr; 930 931 /* 932 * First attempt to create master node. 933 */ 934 /* 935 * In an empty objset, there are no blocks to read and thus 936 * there can be no i/o errors (which we assert below). 937 */ 938 moid = MASTER_NODE_OBJ; 939 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE, 940 DMU_OT_NONE, 0, tx); 941 ASSERT(error == 0); 942 943 /* 944 * Set starting attributes. 945 */ 946 947 error = zap_update(os, moid, ZPL_VERSION_OBJ, 8, 1, &version, tx); 948 ASSERT(error == 0); 949 950 /* 951 * Create a delete queue. 952 */ 953 doid = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx); 954 955 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &doid, tx); 956 ASSERT(error == 0); 957 958 /* 959 * Create root znode. Create minimal znode/vnode/zfsvfs 960 * to allow zfs_mknode to work. 961 */ 962 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 963 vattr.va_type = VDIR; 964 vattr.va_mode = S_IFDIR|0755; 965 vattr.va_uid = UID_ROOT; 966 vattr.va_gid = GID_WHEEL; 967 968 rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP); 969 zfs_znode_cache_constructor(rootzp, NULL, 0); 970 rootzp->z_zfsvfs = &zfsvfs; 971 rootzp->z_unlinked = 0; 972 rootzp->z_atime_dirty = 0; 973 rootzp->z_dbuf_held = 0; 974 975 bzero(&zfsvfs, sizeof (zfsvfs_t)); 976 977 zfsvfs.z_os = os; 978 zfsvfs.z_assign = TXG_NOWAIT; 979 zfsvfs.z_parent = &zfsvfs; 980 981 mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL); 982 list_create(&zfsvfs.z_all_znodes, sizeof (znode_t), 983 offsetof(znode_t, z_link_node)); 984 985 zfs_mknode(rootzp, &vattr, &roid, tx, cr, IS_ROOT_NODE, NULL, 0); 986 ASSERT3U(rootzp->z_id, ==, roid); 987 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &roid, tx); 988 ASSERT(error == 0); 989 990 mutex_destroy(&zfsvfs.z_znodes_lock); 991 kmem_cache_free(znode_cache, rootzp); 992} 993#endif /* _KERNEL */ 994 995/* 996 * Given an object number, return its parent object number and whether 997 * or not the object is an extended attribute directory. 998 */ 999static int 1000zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir) 1001{ 1002 dmu_buf_t *db; 1003 dmu_object_info_t doi; 1004 znode_phys_t *zp; 1005 int error; 1006 1007 if ((error = dmu_bonus_hold(osp, obj, FTAG, &db)) != 0) 1008 return (error); 1009 1010 dmu_object_info_from_db(db, &doi); 1011 if (doi.doi_bonus_type != DMU_OT_ZNODE || 1012 doi.doi_bonus_size < sizeof (znode_phys_t)) { 1013 dmu_buf_rele(db, FTAG); 1014 return (EINVAL); 1015 } 1016 1017 zp = db->db_data; 1018 *pobjp = zp->zp_parent; 1019 *is_xattrdir = ((zp->zp_flags & ZFS_XATTR) != 0) && 1020 S_ISDIR(zp->zp_mode); 1021 dmu_buf_rele(db, FTAG); 1022 1023 return (0); 1024} 1025 1026int 1027zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len) 1028{ 1029 char *path = buf + len - 1; 1030 int error; 1031 1032 *path = '\0'; 1033 1034 for (;;) { 1035 uint64_t pobj; 1036 char component[MAXNAMELEN + 2]; 1037 size_t complen; 1038 int is_xattrdir; 1039 1040 if ((error = zfs_obj_to_pobj(osp, obj, &pobj, 1041 &is_xattrdir)) != 0) 1042 break; 1043 1044 if (pobj == obj) { 1045 if (path[0] != '/') 1046 *--path = '/'; 1047 break; 1048 } 1049 1050 component[0] = '/'; 1051 if (is_xattrdir) { 1052 (void) sprintf(component + 1, "<xattrdir>"); 1053 } else { 1054 error = zap_value_search(osp, pobj, obj, component + 1); 1055 if (error != 0) 1056 break; 1057 } 1058 1059 complen = strlen(component); 1060 path -= complen; 1061 ASSERT(path >= buf); 1062 bcopy(component, path, complen); 1063 obj = pobj; 1064 } 1065 1066 if (error == 0) 1067 (void) memmove(buf, path, buf + len - path); 1068 return (error); 1069} 1070