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