zfs_znode.c revision 168958
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#pragma ident "%Z%%M% %I% %E% SMI" 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/vfs.h> 37#include <sys/vnode.h> 38#include <sys/file.h> 39#include <sys/kmem.h> 40#include <sys/cmn_err.h> 41#include <sys/errno.h> 42#include <sys/unistd.h> 43#include <sys/atomic.h> 44#include <sys/zfs_dir.h> 45#include <sys/zfs_acl.h> 46#include <sys/zfs_ioctl.h> 47#include <sys/zfs_rlock.h> 48#include <sys/fs/zfs.h> 49#endif /* _KERNEL */ 50 51#include <sys/dmu.h> 52#include <sys/refcount.h> 53#include <sys/stat.h> 54#include <sys/zap.h> 55#include <sys/zfs_znode.h> 56#include <sys/refcount.h> 57 58/* 59 * Functions needed for userland (ie: libzpool) are not put under 60 * #ifdef_KERNEL; the rest of the functions have dependencies 61 * (such as VFS logic) that will not compile easily in userland. 62 */ 63#ifdef _KERNEL 64struct kmem_cache *znode_cache = NULL; 65 66/*ARGSUSED*/ 67static void 68znode_pageout_func(dmu_buf_t *dbuf, void *user_ptr) 69{ 70 znode_t *zp = user_ptr; 71 vnode_t *vp; 72 73 mutex_enter(&zp->z_lock); 74 vp = ZTOV(zp); 75 if (vp == NULL) { 76 mutex_exit(&zp->z_lock); 77 zfs_znode_free(zp); 78 } else if (vp->v_count == 0) { 79 ZTOV(zp) = NULL; 80 vhold(vp); 81 mutex_exit(&zp->z_lock); 82 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread); 83 vrecycle(vp, curthread); 84 VOP_UNLOCK(vp, 0, curthread); 85 vdrop(vp); 86 zfs_znode_free(zp); 87 } else { 88 /* signal force unmount that this znode can be freed */ 89 zp->z_dbuf = NULL; 90 mutex_exit(&zp->z_lock); 91 } 92} 93 94extern struct vop_vector zfs_vnodeops; 95extern struct vop_vector zfs_fifoops; 96 97/* 98 * XXX: We cannot use this function as a cache constructor, because 99 * there is one global cache for all file systems and we need 100 * to pass vfsp here, which is not possible, because argument 101 * 'cdrarg' is defined at kmem_cache_create() time. 102 */ 103static int 104zfs_znode_cache_constructor(void *buf, void *cdrarg, int kmflags) 105{ 106 znode_t *zp = buf; 107 vfs_t *vfsp = cdrarg; 108 int error; 109 110 if (cdrarg != NULL) { 111 error = getnewvnode("zfs", vfsp, &zfs_vnodeops, &zp->z_vnode); 112 ASSERT(error == 0); 113 zp->z_vnode->v_data = (caddr_t)zp; 114 vhold(zp->z_vnode); 115 } else { 116 zp->z_vnode = NULL; 117 } 118 mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL); 119 rw_init(&zp->z_map_lock, NULL, RW_DEFAULT, NULL); 120 rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL); 121 rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL); 122 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL); 123 124 mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL); 125 avl_create(&zp->z_range_avl, zfs_range_compare, 126 sizeof (rl_t), offsetof(rl_t, r_node)); 127 128 zp->z_dbuf_held = 0; 129 zp->z_dirlocks = 0; 130 zp->z_lockf = NULL; 131 return (0); 132} 133 134/*ARGSUSED*/ 135static void 136zfs_znode_cache_destructor(void *buf, void *cdarg) 137{ 138 znode_t *zp = buf; 139 140 ASSERT(zp->z_dirlocks == 0); 141 mutex_destroy(&zp->z_lock); 142 rw_destroy(&zp->z_map_lock); 143 rw_destroy(&zp->z_parent_lock); 144 rw_destroy(&zp->z_name_lock); 145 mutex_destroy(&zp->z_acl_lock); 146 mutex_destroy(&zp->z_range_lock); 147 avl_destroy(&zp->z_range_avl); 148 149 ASSERT(zp->z_dbuf_held == 0); 150} 151 152void 153zfs_znode_init(void) 154{ 155 /* 156 * Initialize zcache 157 */ 158 ASSERT(znode_cache == NULL); 159 znode_cache = kmem_cache_create("zfs_znode_cache", 160 sizeof (znode_t), 0, /* zfs_znode_cache_constructor */ NULL, 161 zfs_znode_cache_destructor, NULL, NULL, NULL, 0); 162} 163 164void 165zfs_znode_fini(void) 166{ 167 /* 168 * Cleanup zcache 169 */ 170 if (znode_cache) 171 kmem_cache_destroy(znode_cache); 172 znode_cache = NULL; 173} 174 175/* 176 * zfs_init_fs - Initialize the zfsvfs struct and the file system 177 * incore "master" object. Verify version compatibility. 178 */ 179int 180zfs_init_fs(zfsvfs_t *zfsvfs, znode_t **zpp, cred_t *cr) 181{ 182 objset_t *os = zfsvfs->z_os; 183 uint64_t version = ZPL_VERSION; 184 int i, error; 185 dmu_object_info_t doi; 186 uint64_t fsid_guid; 187 188 *zpp = NULL; 189 190 /* 191 * XXX - hack to auto-create the pool root filesystem at 192 * the first attempted mount. 193 */ 194 if (dmu_object_info(os, MASTER_NODE_OBJ, &doi) == ENOENT) { 195 dmu_tx_t *tx = dmu_tx_create(os); 196 197 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); /* master */ 198 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); /* del queue */ 199 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); /* root node */ 200 error = dmu_tx_assign(tx, TXG_WAIT); 201 ASSERT3U(error, ==, 0); 202 zfs_create_fs(os, cr, tx); 203 dmu_tx_commit(tx); 204 } 205 206 error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_OBJ, 8, 1, 207 &version); 208 if (error) { 209 return (error); 210 } else if (version != ZPL_VERSION) { 211 (void) printf("Mismatched versions: File system " 212 "is version %lld on-disk format, which is " 213 "incompatible with this software version %lld!", 214 (u_longlong_t)version, ZPL_VERSION); 215 return (ENOTSUP); 216 } 217 218 /* 219 * The fsid is 64 bits, composed of an 8-bit fs type, which 220 * separates our fsid from any other filesystem types, and a 221 * 56-bit objset unique ID. The objset unique ID is unique to 222 * all objsets open on this system, provided by unique_create(). 223 * The 8-bit fs type must be put in the low bits of fsid[1] 224 * because that's where other Solaris filesystems put it. 225 */ 226 fsid_guid = dmu_objset_fsid_guid(os); 227 ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0); 228 zfsvfs->z_vfs->vfs_fsid.val[0] = fsid_guid; 229 zfsvfs->z_vfs->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) | 230 zfsvfs->z_vfs->mnt_vfc->vfc_typenum & 0xFF; 231 232 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, 233 &zfsvfs->z_root); 234 if (error) 235 return (error); 236 ASSERT(zfsvfs->z_root != 0); 237 238 /* 239 * Create the per mount vop tables. 240 */ 241 242 /* 243 * Initialize zget mutex's 244 */ 245 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) 246 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL); 247 248 error = zfs_zget(zfsvfs, zfsvfs->z_root, zpp); 249 if (error) 250 return (error); 251 ASSERT3U((*zpp)->z_id, ==, zfsvfs->z_root); 252 253 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1, 254 &zfsvfs->z_unlinkedobj); 255 if (error) 256 return (error); 257 258 return (0); 259} 260 261/* 262 * define a couple of values we need available 263 * for both 64 and 32 bit environments. 264 */ 265#ifndef NBITSMINOR64 266#define NBITSMINOR64 32 267#endif 268#ifndef MAXMAJ64 269#define MAXMAJ64 0xffffffffUL 270#endif 271#ifndef MAXMIN64 272#define MAXMIN64 0xffffffffUL 273#endif 274#ifndef major 275#define major(x) ((int)(((u_int)(x) >> 8)&0xff)) /* major number */ 276#endif 277#ifndef minor 278#define minor(x) ((int)((x)&0xffff00ff)) /* minor number */ 279#endif 280 281/* 282 * Create special expldev for ZFS private use. 283 * Can't use standard expldev since it doesn't do 284 * what we want. The standard expldev() takes a 285 * dev32_t in LP64 and expands it to a long dev_t. 286 * We need an interface that takes a dev32_t in ILP32 287 * and expands it to a long dev_t. 288 */ 289static uint64_t 290zfs_expldev(dev_t dev) 291{ 292 return (((uint64_t)major(dev) << NBITSMINOR64) | minor(dev)); 293} 294/* 295 * Special cmpldev for ZFS private use. 296 * Can't use standard cmpldev since it takes 297 * a long dev_t and compresses it to dev32_t in 298 * LP64. We need to do a compaction of a long dev_t 299 * to a dev32_t in ILP32. 300 */ 301dev_t 302zfs_cmpldev(uint64_t dev) 303{ 304 return (makedev((dev >> NBITSMINOR64), (dev & MAXMIN64))); 305} 306 307/* 308 * Construct a new znode/vnode and intialize. 309 * 310 * This does not do a call to dmu_set_user() that is 311 * up to the caller to do, in case you don't want to 312 * return the znode 313 */ 314static znode_t * 315zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, uint64_t obj_num, int blksz) 316{ 317 znode_t *zp; 318 vnode_t *vp; 319 int error; 320 321 zp = kmem_cache_alloc(znode_cache, KM_SLEEP); 322 zfs_znode_cache_constructor(zp, zfsvfs->z_vfs, 0); 323 324 ASSERT(zp->z_dirlocks == NULL); 325 326 zp->z_phys = db->db_data; 327 zp->z_zfsvfs = zfsvfs; 328 zp->z_unlinked = 0; 329 zp->z_atime_dirty = 0; 330 zp->z_dbuf_held = 0; 331 zp->z_mapcnt = 0; 332 zp->z_last_itx = 0; 333 zp->z_dbuf = db; 334 zp->z_id = obj_num; 335 zp->z_blksz = blksz; 336 zp->z_seq = 0x7A4653; 337 zp->z_sync_cnt = 0; 338 339 mutex_enter(&zfsvfs->z_znodes_lock); 340 list_insert_tail(&zfsvfs->z_all_znodes, zp); 341 mutex_exit(&zfsvfs->z_znodes_lock); 342 343 vp = ZTOV(zp); 344 if (vp == NULL) 345 return (zp); 346 347 error = insmntque(vp, zfsvfs->z_vfs); 348 KASSERT(error == 0, ("insmntque() failed: error %d", error)); 349 350 vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode); 351 switch (vp->v_type) { 352 case VDIR: 353 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */ 354 break; 355 case VFIFO: 356 vp->v_op = &zfs_fifoops; 357 break; 358 } 359 360 return (zp); 361} 362 363static void 364zfs_znode_dmu_init(znode_t *zp) 365{ 366 znode_t *nzp; 367 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 368 dmu_buf_t *db = zp->z_dbuf; 369 370 mutex_enter(&zp->z_lock); 371 372 nzp = dmu_buf_set_user(db, zp, &zp->z_phys, znode_pageout_func); 373 374 /* 375 * there should be no 376 * concurrent zgets on this object. 377 */ 378 ASSERT3P(nzp, ==, NULL); 379 380 /* 381 * Slap on VROOT if we are the root znode 382 */ 383 if (zp->z_id == zfsvfs->z_root) { 384 ZTOV(zp)->v_flag |= VROOT; 385 } 386 387 ASSERT(zp->z_dbuf_held == 0); 388 zp->z_dbuf_held = 1; 389 VFS_HOLD(zfsvfs->z_vfs); 390 mutex_exit(&zp->z_lock); 391} 392 393/* 394 * Create a new DMU object to hold a zfs znode. 395 * 396 * IN: dzp - parent directory for new znode 397 * vap - file attributes for new znode 398 * tx - dmu transaction id for zap operations 399 * cr - credentials of caller 400 * flag - flags: 401 * IS_ROOT_NODE - new object will be root 402 * IS_XATTR - new object is an attribute 403 * IS_REPLAY - intent log replay 404 * 405 * OUT: oid - ID of created object 406 * 407 */ 408void 409zfs_mknode(znode_t *dzp, vattr_t *vap, uint64_t *oid, dmu_tx_t *tx, cred_t *cr, 410 uint_t flag, znode_t **zpp, int bonuslen) 411{ 412 dmu_buf_t *dbp; 413 znode_phys_t *pzp; 414 znode_t *zp; 415 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 416 timestruc_t now; 417 uint64_t gen; 418 int err; 419 420 ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE)); 421 422 if (zfsvfs->z_assign >= TXG_INITIAL) { /* ZIL replay */ 423 *oid = vap->va_nodeid; 424 flag |= IS_REPLAY; 425 now = vap->va_ctime; /* see zfs_replay_create() */ 426 gen = vap->va_nblocks; /* ditto */ 427 } else { 428 *oid = 0; 429 gethrestime(&now); 430 gen = dmu_tx_get_txg(tx); 431 } 432 433 /* 434 * Create a new DMU object. 435 */ 436 /* 437 * There's currently no mechanism for pre-reading the blocks that will 438 * be to needed allocate a new object, so we accept the small chance 439 * that there will be an i/o error and we will fail one of the 440 * assertions below. 441 */ 442 if (vap->va_type == VDIR) { 443 if (flag & IS_REPLAY) { 444 err = zap_create_claim(zfsvfs->z_os, *oid, 445 DMU_OT_DIRECTORY_CONTENTS, 446 DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx); 447 ASSERT3U(err, ==, 0); 448 } else { 449 *oid = zap_create(zfsvfs->z_os, 450 DMU_OT_DIRECTORY_CONTENTS, 451 DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx); 452 } 453 } else { 454 if (flag & IS_REPLAY) { 455 err = dmu_object_claim(zfsvfs->z_os, *oid, 456 DMU_OT_PLAIN_FILE_CONTENTS, 0, 457 DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx); 458 ASSERT3U(err, ==, 0); 459 } else { 460 *oid = dmu_object_alloc(zfsvfs->z_os, 461 DMU_OT_PLAIN_FILE_CONTENTS, 0, 462 DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx); 463 } 464 } 465 VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, *oid, NULL, &dbp)); 466 dmu_buf_will_dirty(dbp, tx); 467 468 /* 469 * Initialize the znode physical data to zero. 470 */ 471 ASSERT(dbp->db_size >= sizeof (znode_phys_t)); 472 bzero(dbp->db_data, dbp->db_size); 473 pzp = dbp->db_data; 474 475 /* 476 * If this is the root, fix up the half-initialized parent pointer 477 * to reference the just-allocated physical data area. 478 */ 479 if (flag & IS_ROOT_NODE) { 480 dzp->z_phys = pzp; 481 dzp->z_id = *oid; 482 } 483 484 /* 485 * If parent is an xattr, so am I. 486 */ 487 if (dzp->z_phys->zp_flags & ZFS_XATTR) 488 flag |= IS_XATTR; 489 490 if (vap->va_type == VBLK || vap->va_type == VCHR) { 491 pzp->zp_rdev = zfs_expldev(vap->va_rdev); 492 } 493 494 if (vap->va_type == VDIR) { 495 pzp->zp_size = 2; /* contents ("." and "..") */ 496 pzp->zp_links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1; 497 } 498 499 pzp->zp_parent = dzp->z_id; 500 if (flag & IS_XATTR) 501 pzp->zp_flags |= ZFS_XATTR; 502 503 pzp->zp_gen = gen; 504 505 ZFS_TIME_ENCODE(&now, pzp->zp_crtime); 506 ZFS_TIME_ENCODE(&now, pzp->zp_ctime); 507 508 if (vap->va_mask & AT_ATIME) { 509 ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime); 510 } else { 511 ZFS_TIME_ENCODE(&now, pzp->zp_atime); 512 } 513 514 if (vap->va_mask & AT_MTIME) { 515 ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime); 516 } else { 517 ZFS_TIME_ENCODE(&now, pzp->zp_mtime); 518 } 519 520 pzp->zp_mode = MAKEIMODE(vap->va_type, vap->va_mode); 521 zp = zfs_znode_alloc(zfsvfs, dbp, *oid, 0); 522 523 zfs_perm_init(zp, dzp, flag, vap, tx, cr); 524 525 if (zpp) { 526 kmutex_t *hash_mtx = ZFS_OBJ_MUTEX(zp); 527 528 mutex_enter(hash_mtx); 529 zfs_znode_dmu_init(zp); 530 mutex_exit(hash_mtx); 531 532 *zpp = zp; 533 } else { 534 if (ZTOV(zp) != NULL) 535 ZTOV(zp)->v_count = 0; 536 dmu_buf_rele(dbp, NULL); 537 zfs_znode_free(zp); 538 } 539} 540 541int 542zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp) 543{ 544 dmu_object_info_t doi; 545 dmu_buf_t *db; 546 znode_t *zp; 547 vnode_t *vp; 548 int err; 549 550 *zpp = NULL; 551 552 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num); 553 554 err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db); 555 if (err) { 556 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 557 return (err); 558 } 559 560 dmu_object_info_from_db(db, &doi); 561 if (doi.doi_bonus_type != DMU_OT_ZNODE || 562 doi.doi_bonus_size < sizeof (znode_phys_t)) { 563 dmu_buf_rele(db, NULL); 564 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 565 return (EINVAL); 566 } 567 568 ASSERT(db->db_object == obj_num); 569 ASSERT(db->db_offset == -1); 570 ASSERT(db->db_data != NULL); 571 572 zp = dmu_buf_get_user(db); 573 574 if (zp != NULL) { 575 mutex_enter(&zp->z_lock); 576 577 ASSERT3U(zp->z_id, ==, obj_num); 578 if (zp->z_unlinked) { 579 dmu_buf_rele(db, NULL); 580 mutex_exit(&zp->z_lock); 581 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 582 return (ENOENT); 583 } else if (zp->z_dbuf_held) { 584 dmu_buf_rele(db, NULL); 585 } else { 586 zp->z_dbuf_held = 1; 587 VFS_HOLD(zfsvfs->z_vfs); 588 } 589 590 if (ZTOV(zp) != NULL) 591 VN_HOLD(ZTOV(zp)); 592 else { 593 err = getnewvnode("zfs", zfsvfs->z_vfs, &zfs_vnodeops, 594 &zp->z_vnode); 595 ASSERT(err == 0); 596 vp = ZTOV(zp); 597 vp->v_data = (caddr_t)zp; 598 vhold(vp); 599 vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode); 600 if (vp->v_type == VDIR) 601 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */ 602 err = insmntque(vp, zfsvfs->z_vfs); 603 KASSERT(err == 0, ("insmntque() failed: error %d", err)); 604 } 605 mutex_exit(&zp->z_lock); 606 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 607 *zpp = zp; 608 return (0); 609 } 610 611 /* 612 * Not found create new znode/vnode 613 */ 614 zp = zfs_znode_alloc(zfsvfs, db, obj_num, doi.doi_data_block_size); 615 ASSERT3U(zp->z_id, ==, obj_num); 616 zfs_znode_dmu_init(zp); 617 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 618 *zpp = zp; 619 return (0); 620} 621 622void 623zfs_znode_delete(znode_t *zp, dmu_tx_t *tx) 624{ 625 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 626 int error; 627 628 ZFS_OBJ_HOLD_ENTER(zfsvfs, zp->z_id); 629 if (zp->z_phys->zp_acl.z_acl_extern_obj) { 630 error = dmu_object_free(zfsvfs->z_os, 631 zp->z_phys->zp_acl.z_acl_extern_obj, tx); 632 ASSERT3U(error, ==, 0); 633 } 634 error = dmu_object_free(zfsvfs->z_os, zp->z_id, tx); 635 ASSERT3U(error, ==, 0); 636 zp->z_dbuf_held = 0; 637 ZFS_OBJ_HOLD_EXIT(zfsvfs, zp->z_id); 638 dmu_buf_rele(zp->z_dbuf, NULL); 639} 640 641void 642zfs_zinactive(znode_t *zp) 643{ 644 vnode_t *vp = ZTOV(zp); 645 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 646 uint64_t z_id = zp->z_id; 647 648 ASSERT(zp->z_dbuf_held && zp->z_phys); 649 650 /* 651 * Don't allow a zfs_zget() while were trying to release this znode 652 */ 653 ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id); 654 655 mutex_enter(&zp->z_lock); 656 VI_LOCK(vp); 657 if (vp->v_count > 0) { 658 /* 659 * If the hold count is greater than zero, somebody has 660 * obtained a new reference on this znode while we were 661 * processing it here, so we are done. 662 */ 663 VI_UNLOCK(vp); 664 mutex_exit(&zp->z_lock); 665 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 666 return; 667 } 668 VI_UNLOCK(vp); 669 670 /* 671 * If this was the last reference to a file with no links, 672 * remove the file from the file system. 673 */ 674 if (zp->z_unlinked) { 675 ZTOV(zp) = NULL; 676 mutex_exit(&zp->z_lock); 677 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 678 ASSERT(vp->v_count == 0); 679 vrecycle(vp, curthread); 680 zfs_rmnode(zp); 681 VFS_RELE(zfsvfs->z_vfs); 682 return; 683 } 684 ASSERT(zp->z_phys); 685 ASSERT(zp->z_dbuf_held); 686 687 zp->z_dbuf_held = 0; 688 mutex_exit(&zp->z_lock); 689 dmu_buf_rele(zp->z_dbuf, NULL); 690 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 691 VFS_RELE(zfsvfs->z_vfs); 692} 693 694/* 695 * FreeBSD: Should be called from ->vop_reclaim(). 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) { 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 kmem_cache_free(znode_cache, rootzp); 991} 992#endif /* _KERNEL */ 993 994/* 995 * Given an object number, return its parent object number and whether 996 * or not the object is an extended attribute directory. 997 */ 998static int 999zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir) 1000{ 1001 dmu_buf_t *db; 1002 dmu_object_info_t doi; 1003 znode_phys_t *zp; 1004 int error; 1005 1006 if ((error = dmu_bonus_hold(osp, obj, FTAG, &db)) != 0) 1007 return (error); 1008 1009 dmu_object_info_from_db(db, &doi); 1010 if (doi.doi_bonus_type != DMU_OT_ZNODE || 1011 doi.doi_bonus_size < sizeof (znode_phys_t)) { 1012 dmu_buf_rele(db, FTAG); 1013 return (EINVAL); 1014 } 1015 1016 zp = db->db_data; 1017 *pobjp = zp->zp_parent; 1018 *is_xattrdir = ((zp->zp_flags & ZFS_XATTR) != 0) && 1019 S_ISDIR(zp->zp_mode); 1020 dmu_buf_rele(db, FTAG); 1021 1022 return (0); 1023} 1024 1025int 1026zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len) 1027{ 1028 char *path = buf + len - 1; 1029 int error; 1030 1031 *path = '\0'; 1032 1033 for (;;) { 1034 uint64_t pobj; 1035 char component[MAXNAMELEN + 2]; 1036 size_t complen; 1037 int is_xattrdir; 1038 1039 if ((error = zfs_obj_to_pobj(osp, obj, &pobj, 1040 &is_xattrdir)) != 0) 1041 break; 1042 1043 if (pobj == obj) { 1044 if (path[0] != '/') 1045 *--path = '/'; 1046 break; 1047 } 1048 1049 component[0] = '/'; 1050 if (is_xattrdir) { 1051 (void) sprintf(component + 1, "<xattrdir>"); 1052 } else { 1053 error = zap_value_search(osp, pobj, obj, component + 1); 1054 if (error != 0) 1055 break; 1056 } 1057 1058 complen = strlen(component); 1059 path -= complen; 1060 ASSERT(path >= buf); 1061 bcopy(component, path, complen); 1062 obj = pobj; 1063 } 1064 1065 if (error == 0) 1066 (void) memmove(buf, path, buf + len - path); 1067 return (error); 1068} 1069