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 2008 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/u8_textprep.h> 40 #include <sys/dsl_dataset.h> 41 #include <sys/vfs.h> 42 #include <sys/vfs_opreg.h> 43 #include <sys/vnode.h> 44 #include <sys/file.h> 45 #include <sys/kmem.h> 46 #include <sys/errno.h> 47 #include <sys/unistd.h> 48 #include <sys/mode.h> 49 #include <sys/atomic.h> 50 #include <vm/pvn.h> 51 #include "fs/fs_subr.h" 52 #include <sys/zfs_dir.h> 53 #include <sys/zfs_acl.h> 54 #include <sys/zfs_ioctl.h> 55 #include <sys/zfs_rlock.h> 56 #include <sys/zfs_fuid.h> 57 #include <sys/fs/zfs.h> 58 #include <sys/kidmap.h> 59 #endif /* _KERNEL */ 60 61 #include <sys/dmu.h> 62 #include <sys/refcount.h> 63 #include <sys/stat.h> 64 #include <sys/zap.h> 65 #include <sys/zfs_znode.h> 66 67 #include "zfs_prop.h" 68 69 /* 70 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only 71 * turned on when DEBUG is also defined. 72 */ 73 #ifdef DEBUG 74 #define ZNODE_STATS 75 #endif /* DEBUG */ 76 77 #ifdef ZNODE_STATS 78 #define ZNODE_STAT_ADD(stat) ((stat)++) 79 #else 80 #define ZNODE_STAT_ADD(stat) /* nothing */ 81 #endif /* ZNODE_STATS */ 82 83 #define POINTER_IS_VALID(p) (!((uintptr_t)(p) & 0x3)) 84 #define POINTER_INVALIDATE(pp) (*(pp) = (void *)((uintptr_t)(*(pp)) | 0x1)) 85 86 /* 87 * Functions needed for userland (ie: libzpool) are not put under 88 * #ifdef_KERNEL; the rest of the functions have dependencies 89 * (such as VFS logic) that will not compile easily in userland. 90 */ 91 #ifdef _KERNEL 92 static kmem_cache_t *znode_cache = NULL; 93 94 /*ARGSUSED*/ 95 static void 96 znode_evict_error(dmu_buf_t *dbuf, void *user_ptr) 97 { 98 /* 99 * We should never drop all dbuf refs without first clearing 100 * the eviction callback. 101 */ 102 panic("evicting znode %p\n", user_ptr); 103 } 104 105 /*ARGSUSED*/ 106 static int 107 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags) 108 { 109 znode_t *zp = buf; 110 111 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs)); 112 113 zp->z_vnode = vn_alloc(kmflags); 114 if (zp->z_vnode == NULL) { 115 return (-1); 116 } 117 ZTOV(zp)->v_data = zp; 118 119 list_link_init(&zp->z_link_node); 120 121 mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL); 122 rw_init(&zp->z_map_lock, NULL, RW_DEFAULT, NULL); 123 rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL); 124 rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL); 125 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL); 126 127 mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL); 128 avl_create(&zp->z_range_avl, zfs_range_compare, 129 sizeof (rl_t), offsetof(rl_t, r_node)); 130 131 zp->z_dbuf = NULL; 132 zp->z_dirlocks = NULL; 133 return (0); 134 } 135 136 /*ARGSUSED*/ 137 static void 138 zfs_znode_cache_destructor(void *buf, void *arg) 139 { 140 znode_t *zp = buf; 141 142 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs)); 143 ASSERT(ZTOV(zp)->v_data == zp); 144 vn_free(ZTOV(zp)); 145 ASSERT(!list_link_active(&zp->z_link_node)); 146 mutex_destroy(&zp->z_lock); 147 rw_destroy(&zp->z_map_lock); 148 rw_destroy(&zp->z_parent_lock); 149 rw_destroy(&zp->z_name_lock); 150 mutex_destroy(&zp->z_acl_lock); 151 avl_destroy(&zp->z_range_avl); 152 mutex_destroy(&zp->z_range_lock); 153 154 ASSERT(zp->z_dbuf == NULL); 155 ASSERT(zp->z_dirlocks == NULL); 156 } 157 158 #ifdef ZNODE_STATS 159 static struct { 160 uint64_t zms_zfsvfs_invalid; 161 uint64_t zms_zfsvfs_unmounted; 162 uint64_t zms_zfsvfs_recheck_invalid; 163 uint64_t zms_vnode_locked; 164 uint64_t zms_znode_in_use; 165 uint64_t zms_yes; 166 uint64_t zms_later; 167 uint64_t zms_dont_know; 168 } znode_move_stats; 169 #endif /* ZNODE_STATS */ 170 171 static void 172 zfs_znode_move_impl(znode_t *ozp, znode_t *nzp) 173 { 174 vnode_t *vp; 175 176 /* Copy fields. */ 177 nzp->z_zfsvfs = ozp->z_zfsvfs; 178 179 /* Swap vnodes. */ 180 vp = nzp->z_vnode; 181 nzp->z_vnode = ozp->z_vnode; 182 ozp->z_vnode = vp; /* let destructor free the overwritten vnode */ 183 ZTOV(ozp)->v_data = ozp; 184 ZTOV(nzp)->v_data = nzp; 185 186 nzp->z_id = ozp->z_id; 187 ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */ 188 ASSERT(avl_numnodes(&ozp->z_range_avl) == 0); 189 nzp->z_unlinked = ozp->z_unlinked; 190 nzp->z_atime_dirty = ozp->z_atime_dirty; 191 nzp->z_zn_prefetch = ozp->z_zn_prefetch; 192 nzp->z_blksz = ozp->z_blksz; 193 nzp->z_seq = ozp->z_seq; 194 nzp->z_mapcnt = ozp->z_mapcnt; 195 nzp->z_last_itx = ozp->z_last_itx; 196 nzp->z_gen = ozp->z_gen; 197 nzp->z_sync_cnt = ozp->z_sync_cnt; 198 nzp->z_phys = ozp->z_phys; 199 nzp->z_dbuf = ozp->z_dbuf; 200 201 /* Update back pointers. */ 202 (void) dmu_buf_update_user(nzp->z_dbuf, ozp, nzp, &nzp->z_phys, 203 znode_evict_error); 204 205 /* 206 * Invalidate the original znode by clearing fields that provide a 207 * pointer back to the znode. Set the low bit of the vfs pointer to 208 * ensure that zfs_znode_move() recognizes the znode as invalid in any 209 * subsequent callback. 210 */ 211 ozp->z_dbuf = NULL; 212 POINTER_INVALIDATE(&ozp->z_zfsvfs); 213 } 214 215 /* 216 * Wrapper function for ZFS_ENTER that returns 0 if successful and otherwise 217 * returns a non-zero error code. 218 */ 219 static int 220 zfs_enter(zfsvfs_t *zfsvfs) 221 { 222 ZFS_ENTER(zfsvfs); 223 return (0); 224 } 225 226 /*ARGSUSED*/ 227 static kmem_cbrc_t 228 zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg) 229 { 230 znode_t *ozp = buf, *nzp = newbuf; 231 zfsvfs_t *zfsvfs; 232 vnode_t *vp; 233 234 /* 235 * The znode is on the file system's list of known znodes if the vfs 236 * pointer is valid. We set the low bit of the vfs pointer when freeing 237 * the znode to invalidate it, and the memory patterns written by kmem 238 * (baddcafe and deadbeef) set at least one of the two low bits. A newly 239 * created znode sets the vfs pointer last of all to indicate that the 240 * znode is known and in a valid state to be moved by this function. 241 */ 242 zfsvfs = ozp->z_zfsvfs; 243 if (!POINTER_IS_VALID(zfsvfs)) { 244 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid); 245 ZNODE_STAT_ADD(znode_move_stats.zms_dont_know); 246 return (KMEM_CBRC_DONT_KNOW); 247 } 248 249 /* 250 * Ensure that the filesystem is not unmounted during the move. 251 */ 252 if (zfs_enter(zfsvfs) != 0) { /* ZFS_ENTER */ 253 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted); 254 ZNODE_STAT_ADD(znode_move_stats.zms_dont_know); 255 return (KMEM_CBRC_DONT_KNOW); 256 } 257 258 mutex_enter(&zfsvfs->z_znodes_lock); 259 /* 260 * Recheck the vfs pointer in case the znode was removed just before 261 * acquiring the lock. 262 */ 263 if (zfsvfs != ozp->z_zfsvfs) { 264 mutex_exit(&zfsvfs->z_znodes_lock); 265 ZFS_EXIT(zfsvfs); 266 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck_invalid); 267 ZNODE_STAT_ADD(znode_move_stats.zms_dont_know); 268 return (KMEM_CBRC_DONT_KNOW); 269 } 270 271 /* 272 * At this point we know that as long as we hold z_znodes_lock, the 273 * znode cannot be freed and fields within the znode can be safely 274 * accessed. 275 */ 276 vp = ZTOV(ozp); 277 if (mutex_tryenter(&vp->v_lock) == 0) { 278 mutex_exit(&zfsvfs->z_znodes_lock); 279 ZFS_EXIT(zfsvfs); 280 ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked); 281 ZNODE_STAT_ADD(znode_move_stats.zms_later); 282 return (KMEM_CBRC_LATER); 283 } 284 /* Only move znodes that are referenced _only_ by the DNLC. */ 285 if (vp->v_count != 1 || !vn_in_dnlc(vp)) { 286 mutex_exit(&vp->v_lock); 287 mutex_exit(&zfsvfs->z_znodes_lock); 288 ZFS_EXIT(zfsvfs); 289 ZNODE_STAT_ADD(znode_move_stats.zms_znode_in_use); 290 ZNODE_STAT_ADD(znode_move_stats.zms_later); 291 return (KMEM_CBRC_LATER); 292 } 293 294 /* 295 * The znode is known and in a valid state to move. We're holding the 296 * locks needed to execute the critical section. 297 */ 298 zfs_znode_move_impl(ozp, nzp); 299 mutex_exit(&vp->v_lock); 300 301 list_link_replace(&ozp->z_link_node, &nzp->z_link_node); 302 mutex_exit(&zfsvfs->z_znodes_lock); 303 ZFS_EXIT(zfsvfs); 304 305 ZNODE_STAT_ADD(znode_move_stats.zms_yes); 306 return (KMEM_CBRC_YES); 307 } 308 309 void 310 zfs_znode_init(void) 311 { 312 /* 313 * Initialize zcache 314 */ 315 ASSERT(znode_cache == NULL); 316 znode_cache = kmem_cache_create("zfs_znode_cache", 317 sizeof (znode_t), 0, zfs_znode_cache_constructor, 318 zfs_znode_cache_destructor, NULL, NULL, NULL, 0); 319 kmem_cache_set_move(znode_cache, zfs_znode_move); 320 } 321 322 void 323 zfs_znode_fini(void) 324 { 325 /* 326 * Cleanup vfs & vnode ops 327 */ 328 zfs_remove_op_tables(); 329 330 /* 331 * Cleanup zcache 332 */ 333 if (znode_cache) 334 kmem_cache_destroy(znode_cache); 335 znode_cache = NULL; 336 } 337 338 struct vnodeops *zfs_dvnodeops; 339 struct vnodeops *zfs_fvnodeops; 340 struct vnodeops *zfs_symvnodeops; 341 struct vnodeops *zfs_xdvnodeops; 342 struct vnodeops *zfs_evnodeops; 343 344 void 345 zfs_remove_op_tables() 346 { 347 /* 348 * Remove vfs ops 349 */ 350 ASSERT(zfsfstype); 351 (void) vfs_freevfsops_by_type(zfsfstype); 352 zfsfstype = 0; 353 354 /* 355 * Remove vnode ops 356 */ 357 if (zfs_dvnodeops) 358 vn_freevnodeops(zfs_dvnodeops); 359 if (zfs_fvnodeops) 360 vn_freevnodeops(zfs_fvnodeops); 361 if (zfs_symvnodeops) 362 vn_freevnodeops(zfs_symvnodeops); 363 if (zfs_xdvnodeops) 364 vn_freevnodeops(zfs_xdvnodeops); 365 if (zfs_evnodeops) 366 vn_freevnodeops(zfs_evnodeops); 367 368 zfs_dvnodeops = NULL; 369 zfs_fvnodeops = NULL; 370 zfs_symvnodeops = NULL; 371 zfs_xdvnodeops = NULL; 372 zfs_evnodeops = NULL; 373 } 374 375 extern const fs_operation_def_t zfs_dvnodeops_template[]; 376 extern const fs_operation_def_t zfs_fvnodeops_template[]; 377 extern const fs_operation_def_t zfs_xdvnodeops_template[]; 378 extern const fs_operation_def_t zfs_symvnodeops_template[]; 379 extern const fs_operation_def_t zfs_evnodeops_template[]; 380 381 int 382 zfs_create_op_tables() 383 { 384 int error; 385 386 /* 387 * zfs_dvnodeops can be set if mod_remove() calls mod_installfs() 388 * due to a failure to remove the the 2nd modlinkage (zfs_modldrv). 389 * In this case we just return as the ops vectors are already set up. 390 */ 391 if (zfs_dvnodeops) 392 return (0); 393 394 error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template, 395 &zfs_dvnodeops); 396 if (error) 397 return (error); 398 399 error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template, 400 &zfs_fvnodeops); 401 if (error) 402 return (error); 403 404 error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template, 405 &zfs_symvnodeops); 406 if (error) 407 return (error); 408 409 error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template, 410 &zfs_xdvnodeops); 411 if (error) 412 return (error); 413 414 error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template, 415 &zfs_evnodeops); 416 417 return (error); 418 } 419 420 /* 421 * zfs_init_fs - Initialize the zfsvfs struct and the file system 422 * incore "master" object. Verify version compatibility. 423 */ 424 int 425 zfs_init_fs(zfsvfs_t *zfsvfs, znode_t **zpp, cred_t *cr) 426 { 427 extern int zfsfstype; 428 429 objset_t *os = zfsvfs->z_os; 430 int i, error; 431 dmu_object_info_t doi; 432 uint64_t fsid_guid; 433 uint64_t zval; 434 435 *zpp = NULL; 436 437 /* 438 * XXX - hack to auto-create the pool root filesystem at 439 * the first attempted mount. 440 */ 441 if (dmu_object_info(os, MASTER_NODE_OBJ, &doi) == ENOENT) { 442 dmu_tx_t *tx = dmu_tx_create(os); 443 uint64_t zpl_version; 444 nvlist_t *zprops; 445 446 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); /* master */ 447 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); /* del queue */ 448 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); /* root node */ 449 error = dmu_tx_assign(tx, TXG_WAIT); 450 ASSERT3U(error, ==, 0); 451 if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_FUID) 452 zpl_version = ZPL_VERSION; 453 else 454 zpl_version = ZPL_VERSION_FUID - 1; 455 456 VERIFY(nvlist_alloc(&zprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 457 VERIFY(nvlist_add_uint64(zprops, 458 zfs_prop_to_name(ZFS_PROP_VERSION), zpl_version) == 0); 459 zfs_create_fs(os, cr, zprops, tx); 460 nvlist_free(zprops); 461 dmu_tx_commit(tx); 462 } 463 464 error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version); 465 if (error) { 466 return (error); 467 } else if (zfsvfs->z_version > ZPL_VERSION) { 468 (void) printf("Mismatched versions: File system " 469 "is version %llu on-disk format, which is " 470 "incompatible with this software version %lld!", 471 (u_longlong_t)zfsvfs->z_version, ZPL_VERSION); 472 return (ENOTSUP); 473 } 474 475 if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0) 476 return (error); 477 zfsvfs->z_norm = (int)zval; 478 if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0) 479 return (error); 480 zfsvfs->z_utf8 = (zval != 0); 481 if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0) 482 return (error); 483 zfsvfs->z_case = (uint_t)zval; 484 /* 485 * Fold case on file systems that are always or sometimes case 486 * insensitive. 487 */ 488 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE || 489 zfsvfs->z_case == ZFS_CASE_MIXED) 490 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER; 491 492 /* 493 * The fsid is 64 bits, composed of an 8-bit fs type, which 494 * separates our fsid from any other filesystem types, and a 495 * 56-bit objset unique ID. The objset unique ID is unique to 496 * all objsets open on this system, provided by unique_create(). 497 * The 8-bit fs type must be put in the low bits of fsid[1] 498 * because that's where other Solaris filesystems put it. 499 */ 500 fsid_guid = dmu_objset_fsid_guid(os); 501 ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0); 502 zfsvfs->z_vfs->vfs_fsid.val[0] = fsid_guid; 503 zfsvfs->z_vfs->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) | 504 zfsfstype & 0xFF; 505 506 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, 507 &zfsvfs->z_root); 508 if (error) 509 return (error); 510 ASSERT(zfsvfs->z_root != 0); 511 512 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1, 513 &zfsvfs->z_unlinkedobj); 514 if (error) 515 return (error); 516 517 /* 518 * Initialize zget mutex's 519 */ 520 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) 521 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL); 522 523 error = zfs_zget(zfsvfs, zfsvfs->z_root, zpp); 524 if (error) { 525 /* 526 * On error, we destroy the mutexes here since it's not 527 * possible for the caller to determine if the mutexes were 528 * initialized properly. 529 */ 530 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) 531 mutex_destroy(&zfsvfs->z_hold_mtx[i]); 532 return (error); 533 } 534 ASSERT3U((*zpp)->z_id, ==, zfsvfs->z_root); 535 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1, 536 &zfsvfs->z_fuid_obj); 537 if (error == ENOENT) 538 error = 0; 539 540 return (0); 541 } 542 543 /* 544 * define a couple of values we need available 545 * for both 64 and 32 bit environments. 546 */ 547 #ifndef NBITSMINOR64 548 #define NBITSMINOR64 32 549 #endif 550 #ifndef MAXMAJ64 551 #define MAXMAJ64 0xffffffffUL 552 #endif 553 #ifndef MAXMIN64 554 #define MAXMIN64 0xffffffffUL 555 #endif 556 557 /* 558 * Create special expldev for ZFS private use. 559 * Can't use standard expldev since it doesn't do 560 * what we want. The standard expldev() takes a 561 * dev32_t in LP64 and expands it to a long dev_t. 562 * We need an interface that takes a dev32_t in ILP32 563 * and expands it to a long dev_t. 564 */ 565 static uint64_t 566 zfs_expldev(dev_t dev) 567 { 568 #ifndef _LP64 569 major_t major = (major_t)dev >> NBITSMINOR32 & MAXMAJ32; 570 return (((uint64_t)major << NBITSMINOR64) | 571 ((minor_t)dev & MAXMIN32)); 572 #else 573 return (dev); 574 #endif 575 } 576 577 /* 578 * Special cmpldev for ZFS private use. 579 * Can't use standard cmpldev since it takes 580 * a long dev_t and compresses it to dev32_t in 581 * LP64. We need to do a compaction of a long dev_t 582 * to a dev32_t in ILP32. 583 */ 584 dev_t 585 zfs_cmpldev(uint64_t dev) 586 { 587 #ifndef _LP64 588 minor_t minor = (minor_t)dev & MAXMIN64; 589 major_t major = (major_t)(dev >> NBITSMINOR64) & MAXMAJ64; 590 591 if (major > MAXMAJ32 || minor > MAXMIN32) 592 return (NODEV32); 593 594 return (((dev32_t)major << NBITSMINOR32) | minor); 595 #else 596 return (dev); 597 #endif 598 } 599 600 static void 601 zfs_znode_dmu_init(zfsvfs_t *zfsvfs, znode_t *zp, dmu_buf_t *db) 602 { 603 znode_t *nzp; 604 605 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs)); 606 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id))); 607 608 mutex_enter(&zp->z_lock); 609 610 ASSERT(zp->z_dbuf == NULL); 611 zp->z_dbuf = db; 612 nzp = dmu_buf_set_user_ie(db, zp, &zp->z_phys, znode_evict_error); 613 614 /* 615 * there should be no 616 * concurrent zgets on this object. 617 */ 618 if (nzp != NULL) 619 panic("existing znode %p for dbuf %p", nzp, db); 620 621 /* 622 * Slap on VROOT if we are the root znode 623 */ 624 if (zp->z_id == zfsvfs->z_root) 625 ZTOV(zp)->v_flag |= VROOT; 626 627 mutex_exit(&zp->z_lock); 628 vn_exists(ZTOV(zp)); 629 } 630 631 void 632 zfs_znode_dmu_fini(znode_t *zp) 633 { 634 dmu_buf_t *db = zp->z_dbuf; 635 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) || 636 zp->z_unlinked || 637 RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock)); 638 ASSERT(zp->z_dbuf != NULL); 639 zp->z_dbuf = NULL; 640 VERIFY(zp == dmu_buf_update_user(db, zp, NULL, NULL, NULL)); 641 dmu_buf_rele(db, NULL); 642 } 643 644 /* 645 * Construct a new znode/vnode and intialize. 646 * 647 * This does not do a call to dmu_set_user() that is 648 * up to the caller to do, in case you don't want to 649 * return the znode 650 */ 651 static znode_t * 652 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz) 653 { 654 znode_t *zp; 655 vnode_t *vp; 656 657 zp = kmem_cache_alloc(znode_cache, KM_SLEEP); 658 659 ASSERT(zp->z_dirlocks == NULL); 660 ASSERT(zp->z_dbuf == NULL); 661 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs)); 662 663 /* 664 * Defer setting z_zfsvfs until the znode is ready to be a candidate for 665 * the zfs_znode_move() callback. 666 */ 667 zp->z_phys = NULL; 668 zp->z_unlinked = 0; 669 zp->z_atime_dirty = 0; 670 zp->z_mapcnt = 0; 671 zp->z_last_itx = 0; 672 zp->z_id = db->db_object; 673 zp->z_blksz = blksz; 674 zp->z_seq = 0x7A4653; 675 zp->z_sync_cnt = 0; 676 677 vp = ZTOV(zp); 678 vn_reinit(vp); 679 680 zfs_znode_dmu_init(zfsvfs, zp, db); 681 682 zp->z_gen = zp->z_phys->zp_gen; 683 684 vp->v_vfsp = zfsvfs->z_parent->z_vfs; 685 vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode); 686 687 switch (vp->v_type) { 688 case VDIR: 689 if (zp->z_phys->zp_flags & ZFS_XATTR) { 690 vn_setops(vp, zfs_xdvnodeops); 691 vp->v_flag |= V_XATTRDIR; 692 } else { 693 vn_setops(vp, zfs_dvnodeops); 694 } 695 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */ 696 break; 697 case VBLK: 698 case VCHR: 699 vp->v_rdev = zfs_cmpldev(zp->z_phys->zp_rdev); 700 /*FALLTHROUGH*/ 701 case VFIFO: 702 case VSOCK: 703 case VDOOR: 704 vn_setops(vp, zfs_fvnodeops); 705 break; 706 case VREG: 707 vp->v_flag |= VMODSORT; 708 vn_setops(vp, zfs_fvnodeops); 709 break; 710 case VLNK: 711 vn_setops(vp, zfs_symvnodeops); 712 break; 713 default: 714 vn_setops(vp, zfs_evnodeops); 715 break; 716 } 717 718 mutex_enter(&zfsvfs->z_znodes_lock); 719 list_insert_tail(&zfsvfs->z_all_znodes, zp); 720 membar_producer(); 721 /* 722 * Everything else must be valid before assigning z_zfsvfs makes the 723 * znode eligible for zfs_znode_move(). 724 */ 725 zp->z_zfsvfs = zfsvfs; 726 mutex_exit(&zfsvfs->z_znodes_lock); 727 728 VFS_HOLD(zfsvfs->z_vfs); 729 return (zp); 730 } 731 732 /* 733 * Create a new DMU object to hold a zfs znode. 734 * 735 * IN: dzp - parent directory for new znode 736 * vap - file attributes for new znode 737 * tx - dmu transaction id for zap operations 738 * cr - credentials of caller 739 * flag - flags: 740 * IS_ROOT_NODE - new object will be root 741 * IS_XATTR - new object is an attribute 742 * IS_REPLAY - intent log replay 743 * bonuslen - length of bonus buffer 744 * setaclp - File/Dir initial ACL 745 * fuidp - Tracks fuid allocation. 746 * 747 * OUT: zpp - allocated znode 748 * 749 */ 750 void 751 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr, 752 uint_t flag, znode_t **zpp, int bonuslen, zfs_acl_t *setaclp, 753 zfs_fuid_info_t **fuidp) 754 { 755 dmu_buf_t *db; 756 znode_phys_t *pzp; 757 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 758 timestruc_t now; 759 uint64_t gen, obj; 760 int err; 761 762 ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE)); 763 764 if (zfsvfs->z_assign >= TXG_INITIAL) { /* ZIL replay */ 765 obj = vap->va_nodeid; 766 flag |= IS_REPLAY; 767 now = vap->va_ctime; /* see zfs_replay_create() */ 768 gen = vap->va_nblocks; /* ditto */ 769 } else { 770 obj = 0; 771 gethrestime(&now); 772 gen = dmu_tx_get_txg(tx); 773 } 774 775 /* 776 * Create a new DMU object. 777 */ 778 /* 779 * There's currently no mechanism for pre-reading the blocks that will 780 * be to needed allocate a new object, so we accept the small chance 781 * that there will be an i/o error and we will fail one of the 782 * assertions below. 783 */ 784 if (vap->va_type == VDIR) { 785 if (flag & IS_REPLAY) { 786 err = zap_create_claim_norm(zfsvfs->z_os, obj, 787 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS, 788 DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx); 789 ASSERT3U(err, ==, 0); 790 } else { 791 obj = zap_create_norm(zfsvfs->z_os, 792 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS, 793 DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx); 794 } 795 } else { 796 if (flag & IS_REPLAY) { 797 err = dmu_object_claim(zfsvfs->z_os, obj, 798 DMU_OT_PLAIN_FILE_CONTENTS, 0, 799 DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx); 800 ASSERT3U(err, ==, 0); 801 } else { 802 obj = dmu_object_alloc(zfsvfs->z_os, 803 DMU_OT_PLAIN_FILE_CONTENTS, 0, 804 DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx); 805 } 806 } 807 VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, obj, NULL, &db)); 808 dmu_buf_will_dirty(db, tx); 809 810 /* 811 * Initialize the znode physical data to zero. 812 */ 813 ASSERT(db->db_size >= sizeof (znode_phys_t)); 814 bzero(db->db_data, db->db_size); 815 pzp = db->db_data; 816 817 /* 818 * If this is the root, fix up the half-initialized parent pointer 819 * to reference the just-allocated physical data area. 820 */ 821 if (flag & IS_ROOT_NODE) { 822 dzp->z_dbuf = db; 823 dzp->z_phys = pzp; 824 dzp->z_id = obj; 825 } 826 827 /* 828 * If parent is an xattr, so am I. 829 */ 830 if (dzp->z_phys->zp_flags & ZFS_XATTR) 831 flag |= IS_XATTR; 832 833 if (vap->va_type == VBLK || vap->va_type == VCHR) { 834 pzp->zp_rdev = zfs_expldev(vap->va_rdev); 835 } 836 837 if (zfsvfs->z_use_fuids) 838 pzp->zp_flags = ZFS_ARCHIVE | ZFS_AV_MODIFIED; 839 840 if (vap->va_type == VDIR) { 841 pzp->zp_size = 2; /* contents ("." and "..") */ 842 pzp->zp_links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1; 843 } 844 845 pzp->zp_parent = dzp->z_id; 846 if (flag & IS_XATTR) 847 pzp->zp_flags |= ZFS_XATTR; 848 849 pzp->zp_gen = gen; 850 851 ZFS_TIME_ENCODE(&now, pzp->zp_crtime); 852 ZFS_TIME_ENCODE(&now, pzp->zp_ctime); 853 854 if (vap->va_mask & AT_ATIME) { 855 ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime); 856 } else { 857 ZFS_TIME_ENCODE(&now, pzp->zp_atime); 858 } 859 860 if (vap->va_mask & AT_MTIME) { 861 ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime); 862 } else { 863 ZFS_TIME_ENCODE(&now, pzp->zp_mtime); 864 } 865 866 pzp->zp_mode = MAKEIMODE(vap->va_type, vap->va_mode); 867 if (!(flag & IS_ROOT_NODE)) { 868 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj); 869 *zpp = zfs_znode_alloc(zfsvfs, db, 0); 870 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj); 871 } else { 872 /* 873 * If we are creating the root node, the "parent" we 874 * passed in is the znode for the root. 875 */ 876 *zpp = dzp; 877 } 878 zfs_perm_init(*zpp, dzp, flag, vap, tx, cr, setaclp, fuidp); 879 } 880 881 void 882 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap) 883 { 884 xoptattr_t *xoap; 885 886 xoap = xva_getxoptattr(xvap); 887 ASSERT(xoap); 888 889 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 890 ZFS_TIME_ENCODE(&xoap->xoa_createtime, zp->z_phys->zp_crtime); 891 XVA_SET_RTN(xvap, XAT_CREATETIME); 892 } 893 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 894 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly); 895 XVA_SET_RTN(xvap, XAT_READONLY); 896 } 897 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 898 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden); 899 XVA_SET_RTN(xvap, XAT_HIDDEN); 900 } 901 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 902 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system); 903 XVA_SET_RTN(xvap, XAT_SYSTEM); 904 } 905 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 906 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive); 907 XVA_SET_RTN(xvap, XAT_ARCHIVE); 908 } 909 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 910 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable); 911 XVA_SET_RTN(xvap, XAT_IMMUTABLE); 912 } 913 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 914 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink); 915 XVA_SET_RTN(xvap, XAT_NOUNLINK); 916 } 917 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 918 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly); 919 XVA_SET_RTN(xvap, XAT_APPENDONLY); 920 } 921 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 922 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump); 923 XVA_SET_RTN(xvap, XAT_NODUMP); 924 } 925 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 926 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque); 927 XVA_SET_RTN(xvap, XAT_OPAQUE); 928 } 929 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 930 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED, 931 xoap->xoa_av_quarantined); 932 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 933 } 934 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 935 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified); 936 XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 937 } 938 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) { 939 (void) memcpy(zp->z_phys + 1, xoap->xoa_av_scanstamp, 940 sizeof (xoap->xoa_av_scanstamp)); 941 zp->z_phys->zp_flags |= ZFS_BONUS_SCANSTAMP; 942 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP); 943 } 944 } 945 946 int 947 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp) 948 { 949 dmu_object_info_t doi; 950 dmu_buf_t *db; 951 znode_t *zp; 952 int err; 953 954 *zpp = NULL; 955 956 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num); 957 958 err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db); 959 if (err) { 960 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 961 return (err); 962 } 963 964 dmu_object_info_from_db(db, &doi); 965 if (doi.doi_bonus_type != DMU_OT_ZNODE || 966 doi.doi_bonus_size < sizeof (znode_phys_t)) { 967 dmu_buf_rele(db, NULL); 968 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 969 return (EINVAL); 970 } 971 972 zp = dmu_buf_get_user(db); 973 if (zp != NULL) { 974 mutex_enter(&zp->z_lock); 975 976 /* 977 * Since we do immediate eviction of the z_dbuf, we 978 * should never find a dbuf with a znode that doesn't 979 * know about the dbuf. 980 */ 981 ASSERT3P(zp->z_dbuf, ==, db); 982 ASSERT3U(zp->z_id, ==, obj_num); 983 if (zp->z_unlinked) { 984 err = ENOENT; 985 } else { 986 VN_HOLD(ZTOV(zp)); 987 *zpp = zp; 988 err = 0; 989 } 990 dmu_buf_rele(db, NULL); 991 mutex_exit(&zp->z_lock); 992 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 993 return (err); 994 } 995 996 /* 997 * Not found create new znode/vnode 998 */ 999 zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size); 1000 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1001 *zpp = zp; 1002 return (0); 1003 } 1004 1005 int 1006 zfs_rezget(znode_t *zp) 1007 { 1008 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1009 dmu_object_info_t doi; 1010 dmu_buf_t *db; 1011 uint64_t obj_num = zp->z_id; 1012 int err; 1013 1014 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num); 1015 1016 err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db); 1017 if (err) { 1018 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1019 return (err); 1020 } 1021 1022 dmu_object_info_from_db(db, &doi); 1023 if (doi.doi_bonus_type != DMU_OT_ZNODE || 1024 doi.doi_bonus_size < sizeof (znode_phys_t)) { 1025 dmu_buf_rele(db, NULL); 1026 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1027 return (EINVAL); 1028 } 1029 1030 if (((znode_phys_t *)db->db_data)->zp_gen != zp->z_gen) { 1031 dmu_buf_rele(db, NULL); 1032 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1033 return (EIO); 1034 } 1035 1036 zfs_znode_dmu_init(zfsvfs, zp, db); 1037 zp->z_unlinked = (zp->z_phys->zp_links == 0); 1038 zp->z_blksz = doi.doi_data_block_size; 1039 1040 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1041 1042 return (0); 1043 } 1044 1045 void 1046 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx) 1047 { 1048 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1049 objset_t *os = zfsvfs->z_os; 1050 uint64_t obj = zp->z_id; 1051 uint64_t acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj; 1052 1053 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj); 1054 if (acl_obj) 1055 VERIFY(0 == dmu_object_free(os, acl_obj, tx)); 1056 VERIFY(0 == dmu_object_free(os, obj, tx)); 1057 zfs_znode_dmu_fini(zp); 1058 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj); 1059 zfs_znode_free(zp); 1060 } 1061 1062 void 1063 zfs_zinactive(znode_t *zp) 1064 { 1065 vnode_t *vp = ZTOV(zp); 1066 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1067 uint64_t z_id = zp->z_id; 1068 1069 ASSERT(zp->z_dbuf && zp->z_phys); 1070 1071 /* 1072 * Don't allow a zfs_zget() while were trying to release this znode 1073 */ 1074 ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id); 1075 1076 mutex_enter(&zp->z_lock); 1077 mutex_enter(&vp->v_lock); 1078 vp->v_count--; 1079 if (vp->v_count > 0 || vn_has_cached_data(vp)) { 1080 /* 1081 * If the hold count is greater than zero, somebody has 1082 * obtained a new reference on this znode while we were 1083 * processing it here, so we are done. If we still have 1084 * mapped pages then we are also done, since we don't 1085 * want to inactivate the znode until the pages get pushed. 1086 * 1087 * XXX - if vn_has_cached_data(vp) is true, but count == 0, 1088 * this seems like it would leave the znode hanging with 1089 * no chance to go inactive... 1090 */ 1091 mutex_exit(&vp->v_lock); 1092 mutex_exit(&zp->z_lock); 1093 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 1094 return; 1095 } 1096 mutex_exit(&vp->v_lock); 1097 1098 /* 1099 * If this was the last reference to a file with no links, 1100 * remove the file from the file system. 1101 */ 1102 if (zp->z_unlinked) { 1103 mutex_exit(&zp->z_lock); 1104 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 1105 zfs_rmnode(zp); 1106 return; 1107 } 1108 mutex_exit(&zp->z_lock); 1109 zfs_znode_dmu_fini(zp); 1110 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 1111 zfs_znode_free(zp); 1112 } 1113 1114 void 1115 zfs_znode_free(znode_t *zp) 1116 { 1117 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1118 1119 vn_invalid(ZTOV(zp)); 1120 1121 ASSERT(ZTOV(zp)->v_count == 0); 1122 1123 mutex_enter(&zfsvfs->z_znodes_lock); 1124 POINTER_INVALIDATE(&zp->z_zfsvfs); 1125 list_remove(&zfsvfs->z_all_znodes, zp); 1126 mutex_exit(&zfsvfs->z_znodes_lock); 1127 1128 kmem_cache_free(znode_cache, zp); 1129 1130 VFS_RELE(zfsvfs->z_vfs); 1131 } 1132 1133 void 1134 zfs_time_stamper_locked(znode_t *zp, uint_t flag, dmu_tx_t *tx) 1135 { 1136 timestruc_t now; 1137 1138 ASSERT(MUTEX_HELD(&zp->z_lock)); 1139 1140 gethrestime(&now); 1141 1142 if (tx) { 1143 dmu_buf_will_dirty(zp->z_dbuf, tx); 1144 zp->z_atime_dirty = 0; 1145 zp->z_seq++; 1146 } else { 1147 zp->z_atime_dirty = 1; 1148 } 1149 1150 if (flag & AT_ATIME) 1151 ZFS_TIME_ENCODE(&now, zp->z_phys->zp_atime); 1152 1153 if (flag & AT_MTIME) { 1154 ZFS_TIME_ENCODE(&now, zp->z_phys->zp_mtime); 1155 if (zp->z_zfsvfs->z_use_fuids) 1156 zp->z_phys->zp_flags |= (ZFS_ARCHIVE | ZFS_AV_MODIFIED); 1157 } 1158 1159 if (flag & AT_CTIME) { 1160 ZFS_TIME_ENCODE(&now, zp->z_phys->zp_ctime); 1161 if (zp->z_zfsvfs->z_use_fuids) 1162 zp->z_phys->zp_flags |= ZFS_ARCHIVE; 1163 } 1164 } 1165 1166 /* 1167 * Update the requested znode timestamps with the current time. 1168 * If we are in a transaction, then go ahead and mark the znode 1169 * dirty in the transaction so the timestamps will go to disk. 1170 * Otherwise, we will get pushed next time the znode is updated 1171 * in a transaction, or when this znode eventually goes inactive. 1172 * 1173 * Why is this OK? 1174 * 1 - Only the ACCESS time is ever updated outside of a transaction. 1175 * 2 - Multiple consecutive updates will be collapsed into a single 1176 * znode update by the transaction grouping semantics of the DMU. 1177 */ 1178 void 1179 zfs_time_stamper(znode_t *zp, uint_t flag, dmu_tx_t *tx) 1180 { 1181 mutex_enter(&zp->z_lock); 1182 zfs_time_stamper_locked(zp, flag, tx); 1183 mutex_exit(&zp->z_lock); 1184 } 1185 1186 /* 1187 * Grow the block size for a file. 1188 * 1189 * IN: zp - znode of file to free data in. 1190 * size - requested block size 1191 * tx - open transaction. 1192 * 1193 * NOTE: this function assumes that the znode is write locked. 1194 */ 1195 void 1196 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx) 1197 { 1198 int error; 1199 u_longlong_t dummy; 1200 1201 if (size <= zp->z_blksz) 1202 return; 1203 /* 1204 * If the file size is already greater than the current blocksize, 1205 * we will not grow. If there is more than one block in a file, 1206 * the blocksize cannot change. 1207 */ 1208 if (zp->z_blksz && zp->z_phys->zp_size > zp->z_blksz) 1209 return; 1210 1211 error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id, 1212 size, 0, tx); 1213 if (error == ENOTSUP) 1214 return; 1215 ASSERT3U(error, ==, 0); 1216 1217 /* What blocksize did we actually get? */ 1218 dmu_object_size_from_db(zp->z_dbuf, &zp->z_blksz, &dummy); 1219 } 1220 1221 /* 1222 * This is a dummy interface used when pvn_vplist_dirty() should *not* 1223 * be calling back into the fs for a putpage(). E.g.: when truncating 1224 * a file, the pages being "thrown away* don't need to be written out. 1225 */ 1226 /* ARGSUSED */ 1227 static int 1228 zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp, 1229 int flags, cred_t *cr) 1230 { 1231 ASSERT(0); 1232 return (0); 1233 } 1234 1235 /* 1236 * Increase the file length 1237 * 1238 * IN: zp - znode of file to free data in. 1239 * end - new end-of-file 1240 * 1241 * RETURN: 0 if success 1242 * error code if failure 1243 */ 1244 static int 1245 zfs_extend(znode_t *zp, uint64_t end) 1246 { 1247 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1248 dmu_tx_t *tx; 1249 rl_t *rl; 1250 uint64_t newblksz; 1251 int error; 1252 1253 /* 1254 * We will change zp_size, lock the whole file. 1255 */ 1256 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER); 1257 1258 /* 1259 * Nothing to do if file already at desired length. 1260 */ 1261 if (end <= zp->z_phys->zp_size) { 1262 zfs_range_unlock(rl); 1263 return (0); 1264 } 1265 top: 1266 tx = dmu_tx_create(zfsvfs->z_os); 1267 dmu_tx_hold_bonus(tx, zp->z_id); 1268 if (end > zp->z_blksz && 1269 (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) { 1270 /* 1271 * We are growing the file past the current block size. 1272 */ 1273 if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) { 1274 ASSERT(!ISP2(zp->z_blksz)); 1275 newblksz = MIN(end, SPA_MAXBLOCKSIZE); 1276 } else { 1277 newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz); 1278 } 1279 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz); 1280 } else { 1281 newblksz = 0; 1282 } 1283 1284 error = dmu_tx_assign(tx, zfsvfs->z_assign); 1285 if (error) { 1286 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 1287 dmu_tx_wait(tx); 1288 dmu_tx_abort(tx); 1289 goto top; 1290 } 1291 dmu_tx_abort(tx); 1292 zfs_range_unlock(rl); 1293 return (error); 1294 } 1295 dmu_buf_will_dirty(zp->z_dbuf, tx); 1296 1297 if (newblksz) 1298 zfs_grow_blocksize(zp, newblksz, tx); 1299 1300 zp->z_phys->zp_size = end; 1301 1302 zfs_range_unlock(rl); 1303 1304 dmu_tx_commit(tx); 1305 1306 return (0); 1307 } 1308 1309 /* 1310 * Free space in a file. 1311 * 1312 * IN: zp - znode of file to free data in. 1313 * off - start of section to free. 1314 * len - length of section to free. 1315 * 1316 * RETURN: 0 if success 1317 * error code if failure 1318 */ 1319 static int 1320 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len) 1321 { 1322 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1323 rl_t *rl; 1324 int error; 1325 1326 /* 1327 * Lock the range being freed. 1328 */ 1329 rl = zfs_range_lock(zp, off, len, RL_WRITER); 1330 1331 /* 1332 * Nothing to do if file already at desired length. 1333 */ 1334 if (off >= zp->z_phys->zp_size) { 1335 zfs_range_unlock(rl); 1336 return (0); 1337 } 1338 1339 if (off + len > zp->z_phys->zp_size) 1340 len = zp->z_phys->zp_size - off; 1341 1342 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len); 1343 1344 zfs_range_unlock(rl); 1345 1346 return (error); 1347 } 1348 1349 /* 1350 * Truncate a file 1351 * 1352 * IN: zp - znode of file to free data in. 1353 * end - new end-of-file. 1354 * 1355 * RETURN: 0 if success 1356 * error code if failure 1357 */ 1358 static int 1359 zfs_trunc(znode_t *zp, uint64_t end) 1360 { 1361 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1362 vnode_t *vp = ZTOV(zp); 1363 dmu_tx_t *tx; 1364 rl_t *rl; 1365 int error; 1366 1367 /* 1368 * We will change zp_size, lock the whole file. 1369 */ 1370 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER); 1371 1372 /* 1373 * Nothing to do if file already at desired length. 1374 */ 1375 if (end >= zp->z_phys->zp_size) { 1376 zfs_range_unlock(rl); 1377 return (0); 1378 } 1379 1380 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end, -1); 1381 if (error) { 1382 zfs_range_unlock(rl); 1383 return (error); 1384 } 1385 top: 1386 tx = dmu_tx_create(zfsvfs->z_os); 1387 dmu_tx_hold_bonus(tx, zp->z_id); 1388 error = dmu_tx_assign(tx, zfsvfs->z_assign); 1389 if (error) { 1390 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 1391 dmu_tx_wait(tx); 1392 dmu_tx_abort(tx); 1393 goto top; 1394 } 1395 dmu_tx_abort(tx); 1396 zfs_range_unlock(rl); 1397 return (error); 1398 } 1399 dmu_buf_will_dirty(zp->z_dbuf, tx); 1400 1401 zp->z_phys->zp_size = end; 1402 1403 dmu_tx_commit(tx); 1404 1405 zfs_range_unlock(rl); 1406 1407 /* 1408 * Clear any mapped pages in the truncated region. This has to 1409 * happen outside of the transaction to avoid the possibility of 1410 * a deadlock with someone trying to push a page that we are 1411 * about to invalidate. 1412 */ 1413 rw_enter(&zp->z_map_lock, RW_WRITER); 1414 if (vn_has_cached_data(vp)) { 1415 page_t *pp; 1416 uint64_t start = end & PAGEMASK; 1417 int poff = end & PAGEOFFSET; 1418 1419 if (poff != 0 && (pp = page_lookup(vp, start, SE_SHARED))) { 1420 /* 1421 * We need to zero a partial page. 1422 */ 1423 pagezero(pp, poff, PAGESIZE - poff); 1424 start += PAGESIZE; 1425 page_unlock(pp); 1426 } 1427 error = pvn_vplist_dirty(vp, start, zfs_no_putpage, 1428 B_INVAL | B_TRUNC, NULL); 1429 ASSERT(error == 0); 1430 } 1431 rw_exit(&zp->z_map_lock); 1432 1433 return (0); 1434 } 1435 1436 /* 1437 * Free space in a file 1438 * 1439 * IN: zp - znode of file to free data in. 1440 * off - start of range 1441 * len - end of range (0 => EOF) 1442 * flag - current file open mode flags. 1443 * log - TRUE if this action should be logged 1444 * 1445 * RETURN: 0 if success 1446 * error code if failure 1447 */ 1448 int 1449 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log) 1450 { 1451 vnode_t *vp = ZTOV(zp); 1452 dmu_tx_t *tx; 1453 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1454 zilog_t *zilog = zfsvfs->z_log; 1455 int error; 1456 1457 if (off > zp->z_phys->zp_size) { 1458 error = zfs_extend(zp, off+len); 1459 if (error == 0 && log) 1460 goto log; 1461 else 1462 return (error); 1463 } 1464 1465 /* 1466 * Check for any locks in the region to be freed. 1467 */ 1468 if (MANDLOCK(vp, (mode_t)zp->z_phys->zp_mode)) { 1469 uint64_t length = (len ? len : zp->z_phys->zp_size - off); 1470 if (error = chklock(vp, FWRITE, off, length, flag, NULL)) 1471 return (error); 1472 } 1473 1474 if (len == 0) { 1475 error = zfs_trunc(zp, off); 1476 } else { 1477 if ((error = zfs_free_range(zp, off, len)) == 0 && 1478 off + len > zp->z_phys->zp_size) 1479 error = zfs_extend(zp, off+len); 1480 } 1481 if (error || !log) 1482 return (error); 1483 log: 1484 tx = dmu_tx_create(zfsvfs->z_os); 1485 dmu_tx_hold_bonus(tx, zp->z_id); 1486 error = dmu_tx_assign(tx, zfsvfs->z_assign); 1487 if (error) { 1488 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 1489 dmu_tx_wait(tx); 1490 dmu_tx_abort(tx); 1491 goto log; 1492 } 1493 dmu_tx_abort(tx); 1494 return (error); 1495 } 1496 1497 zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 1498 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len); 1499 1500 dmu_tx_commit(tx); 1501 return (0); 1502 } 1503 1504 void 1505 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx) 1506 { 1507 zfsvfs_t zfsvfs; 1508 uint64_t moid, doid; 1509 uint64_t version = 0; 1510 uint64_t sense = ZFS_CASE_SENSITIVE; 1511 uint64_t norm = 0; 1512 nvpair_t *elem; 1513 int error; 1514 znode_t *rootzp = NULL; 1515 vnode_t *vp; 1516 vattr_t vattr; 1517 znode_t *zp; 1518 1519 /* 1520 * First attempt to create master node. 1521 */ 1522 /* 1523 * In an empty objset, there are no blocks to read and thus 1524 * there can be no i/o errors (which we assert below). 1525 */ 1526 moid = MASTER_NODE_OBJ; 1527 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE, 1528 DMU_OT_NONE, 0, tx); 1529 ASSERT(error == 0); 1530 1531 /* 1532 * Set starting attributes. 1533 */ 1534 elem = NULL; 1535 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) { 1536 /* For the moment we expect all zpl props to be uint64_ts */ 1537 uint64_t val; 1538 char *name; 1539 1540 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64); 1541 VERIFY(nvpair_value_uint64(elem, &val) == 0); 1542 name = nvpair_name(elem); 1543 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) { 1544 version = val; 1545 error = zap_update(os, moid, ZPL_VERSION_STR, 1546 8, 1, &version, tx); 1547 } else { 1548 error = zap_update(os, moid, name, 8, 1, &val, tx); 1549 } 1550 ASSERT(error == 0); 1551 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0) 1552 norm = val; 1553 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0) 1554 sense = val; 1555 } 1556 ASSERT(version != 0); 1557 1558 /* 1559 * Create a delete queue. 1560 */ 1561 doid = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx); 1562 1563 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &doid, tx); 1564 ASSERT(error == 0); 1565 1566 /* 1567 * Create root znode. Create minimal znode/vnode/zfsvfs 1568 * to allow zfs_mknode to work. 1569 */ 1570 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 1571 vattr.va_type = VDIR; 1572 vattr.va_mode = S_IFDIR|0755; 1573 vattr.va_uid = crgetuid(cr); 1574 vattr.va_gid = crgetgid(cr); 1575 1576 rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP); 1577 rootzp->z_unlinked = 0; 1578 rootzp->z_atime_dirty = 0; 1579 1580 vp = ZTOV(rootzp); 1581 vn_reinit(vp); 1582 vp->v_type = VDIR; 1583 1584 bzero(&zfsvfs, sizeof (zfsvfs_t)); 1585 1586 zfsvfs.z_os = os; 1587 zfsvfs.z_assign = TXG_NOWAIT; 1588 zfsvfs.z_parent = &zfsvfs; 1589 zfsvfs.z_version = version; 1590 zfsvfs.z_use_fuids = USE_FUIDS(version, os); 1591 zfsvfs.z_norm = norm; 1592 /* 1593 * Fold case on file systems that are always or sometimes case 1594 * insensitive. 1595 */ 1596 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED) 1597 zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER; 1598 1599 mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL); 1600 list_create(&zfsvfs.z_all_znodes, sizeof (znode_t), 1601 offsetof(znode_t, z_link_node)); 1602 1603 ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs)); 1604 rootzp->z_zfsvfs = &zfsvfs; 1605 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, 0, NULL, NULL); 1606 ASSERT3P(zp, ==, rootzp); 1607 ASSERT(!vn_in_dnlc(ZTOV(rootzp))); /* not valid to move */ 1608 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx); 1609 ASSERT(error == 0); 1610 POINTER_INVALIDATE(&rootzp->z_zfsvfs); 1611 1612 ZTOV(rootzp)->v_count = 0; 1613 dmu_buf_rele(rootzp->z_dbuf, NULL); 1614 rootzp->z_dbuf = NULL; 1615 kmem_cache_free(znode_cache, rootzp); 1616 } 1617 1618 #endif /* _KERNEL */ 1619 /* 1620 * Given an object number, return its parent object number and whether 1621 * or not the object is an extended attribute directory. 1622 */ 1623 static int 1624 zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir) 1625 { 1626 dmu_buf_t *db; 1627 dmu_object_info_t doi; 1628 znode_phys_t *zp; 1629 int error; 1630 1631 if ((error = dmu_bonus_hold(osp, obj, FTAG, &db)) != 0) 1632 return (error); 1633 1634 dmu_object_info_from_db(db, &doi); 1635 if (doi.doi_bonus_type != DMU_OT_ZNODE || 1636 doi.doi_bonus_size < sizeof (znode_phys_t)) { 1637 dmu_buf_rele(db, FTAG); 1638 return (EINVAL); 1639 } 1640 1641 zp = db->db_data; 1642 *pobjp = zp->zp_parent; 1643 *is_xattrdir = ((zp->zp_flags & ZFS_XATTR) != 0) && 1644 S_ISDIR(zp->zp_mode); 1645 dmu_buf_rele(db, FTAG); 1646 1647 return (0); 1648 } 1649 1650 int 1651 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len) 1652 { 1653 char *path = buf + len - 1; 1654 int error; 1655 1656 *path = '\0'; 1657 1658 for (;;) { 1659 uint64_t pobj; 1660 char component[MAXNAMELEN + 2]; 1661 size_t complen; 1662 int is_xattrdir; 1663 1664 if ((error = zfs_obj_to_pobj(osp, obj, &pobj, 1665 &is_xattrdir)) != 0) 1666 break; 1667 1668 if (pobj == obj) { 1669 if (path[0] != '/') 1670 *--path = '/'; 1671 break; 1672 } 1673 1674 component[0] = '/'; 1675 if (is_xattrdir) { 1676 (void) sprintf(component + 1, "<xattrdir>"); 1677 } else { 1678 error = zap_value_search(osp, pobj, obj, 1679 ZFS_DIRENT_OBJ(-1ULL), component + 1); 1680 if (error != 0) 1681 break; 1682 } 1683 1684 complen = strlen(component); 1685 path -= complen; 1686 ASSERT(path >= buf); 1687 bcopy(component, path, complen); 1688 obj = pobj; 1689 } 1690 1691 if (error == 0) 1692 (void) memmove(buf, path, buf + len - path); 1693 return (error); 1694 } 1695