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