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