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