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