1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 22ac05c741SMark Maybee * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 2675c76197Speteh /* Portions Copyright 2007 Jeremy Teo */ 2775c76197Speteh 2855434c77Sek #ifdef _KERNEL 29fa9e4066Sahrens #include <sys/types.h> 30fa9e4066Sahrens #include <sys/param.h> 31fa9e4066Sahrens #include <sys/time.h> 32fa9e4066Sahrens #include <sys/systm.h> 33fa9e4066Sahrens #include <sys/sysmacros.h> 34fa9e4066Sahrens #include <sys/resource.h> 35fa9e4066Sahrens #include <sys/mntent.h> 3672fc53bcSmarks #include <sys/mkdev.h> 37de8267e0Stimh #include <sys/u8_textprep.h> 38ab04eb8eStimh #include <sys/dsl_dataset.h> 39fa9e4066Sahrens #include <sys/vfs.h> 40aa59c4cbSrsb #include <sys/vfs_opreg.h> 41fa9e4066Sahrens #include <sys/vnode.h> 42fa9e4066Sahrens #include <sys/file.h> 43fa9e4066Sahrens #include <sys/kmem.h> 44fa9e4066Sahrens #include <sys/errno.h> 45fa9e4066Sahrens #include <sys/unistd.h> 46fa9e4066Sahrens #include <sys/mode.h> 47fa9e4066Sahrens #include <sys/atomic.h> 48fa9e4066Sahrens #include <vm/pvn.h> 49fa9e4066Sahrens #include "fs/fs_subr.h" 50fa9e4066Sahrens #include <sys/zfs_dir.h> 51fa9e4066Sahrens #include <sys/zfs_acl.h> 52fa9e4066Sahrens #include <sys/zfs_ioctl.h> 53104e2ed7Sperrin #include <sys/zfs_rlock.h> 54da6c28aaSamw #include <sys/zfs_fuid.h> 55fa9e4066Sahrens #include <sys/fs/zfs.h> 56da6c28aaSamw #include <sys/kidmap.h> 5755434c77Sek #endif /* _KERNEL */ 5855434c77Sek 5955434c77Sek #include <sys/dmu.h> 6055434c77Sek #include <sys/refcount.h> 6155434c77Sek #include <sys/stat.h> 6255434c77Sek #include <sys/zap.h> 6355434c77Sek #include <sys/zfs_znode.h> 64fa9e4066Sahrens 65de8267e0Stimh #include "zfs_prop.h" 66de8267e0Stimh 67b5fca8f8Stomee /* 68b5fca8f8Stomee * Define ZNODE_STATS to turn on statistic gathering. By default, it is only 69b5fca8f8Stomee * turned on when DEBUG is also defined. 70b5fca8f8Stomee */ 71b5fca8f8Stomee #ifdef DEBUG 72b5fca8f8Stomee #define ZNODE_STATS 73b5fca8f8Stomee #endif /* DEBUG */ 74b5fca8f8Stomee 75b5fca8f8Stomee #ifdef ZNODE_STATS 76b5fca8f8Stomee #define ZNODE_STAT_ADD(stat) ((stat)++) 77b5fca8f8Stomee #else 78b5fca8f8Stomee #define ZNODE_STAT_ADD(stat) /* nothing */ 79b5fca8f8Stomee #endif /* ZNODE_STATS */ 80b5fca8f8Stomee 81b5fca8f8Stomee #define POINTER_IS_VALID(p) (!((uintptr_t)(p) & 0x3)) 82b5fca8f8Stomee #define POINTER_INVALIDATE(pp) (*(pp) = (void *)((uintptr_t)(*(pp)) | 0x1)) 83b5fca8f8Stomee 8455434c77Sek /* 8555434c77Sek * Functions needed for userland (ie: libzpool) are not put under 8655434c77Sek * #ifdef_KERNEL; the rest of the functions have dependencies 8755434c77Sek * (such as VFS logic) that will not compile easily in userland. 8855434c77Sek */ 8955434c77Sek #ifdef _KERNEL 904e9583b2STom Erickson /* 914e9583b2STom Erickson * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to 924e9583b2STom Erickson * be freed before it can be safely accessed. 934e9583b2STom Erickson */ 944e9583b2STom Erickson krwlock_t zfsvfs_lock; 954e9583b2STom Erickson 96b5fca8f8Stomee static kmem_cache_t *znode_cache = NULL; 97fa9e4066Sahrens 98fa9e4066Sahrens /*ARGSUSED*/ 99fa9e4066Sahrens static void 100874395d5Smaybee znode_evict_error(dmu_buf_t *dbuf, void *user_ptr) 101fa9e4066Sahrens { 102874395d5Smaybee /* 103874395d5Smaybee * We should never drop all dbuf refs without first clearing 104874395d5Smaybee * the eviction callback. 105874395d5Smaybee */ 106874395d5Smaybee panic("evicting znode %p\n", user_ptr); 107fa9e4066Sahrens } 108fa9e4066Sahrens 109fa9e4066Sahrens /*ARGSUSED*/ 110fa9e4066Sahrens static int 111b5fca8f8Stomee zfs_znode_cache_constructor(void *buf, void *arg, int kmflags) 112fa9e4066Sahrens { 113fa9e4066Sahrens znode_t *zp = buf; 114fa9e4066Sahrens 115b5fca8f8Stomee ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs)); 116b5fca8f8Stomee 117b5fca8f8Stomee zp->z_vnode = vn_alloc(kmflags); 118b5fca8f8Stomee if (zp->z_vnode == NULL) { 119b5fca8f8Stomee return (-1); 120b5fca8f8Stomee } 121b5fca8f8Stomee ZTOV(zp)->v_data = zp; 122b5fca8f8Stomee 123b5fca8f8Stomee list_link_init(&zp->z_link_node); 124b5fca8f8Stomee 125fa9e4066Sahrens mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL); 126104e2ed7Sperrin rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL); 127af2c4821Smaybee rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL); 128fa9e4066Sahrens mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL); 129104e2ed7Sperrin 130104e2ed7Sperrin mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL); 131104e2ed7Sperrin avl_create(&zp->z_range_avl, zfs_range_compare, 132104e2ed7Sperrin sizeof (rl_t), offsetof(rl_t, r_node)); 133104e2ed7Sperrin 1344ccbb6e7Sahrens zp->z_dbuf = NULL; 135b5fca8f8Stomee zp->z_dirlocks = NULL; 136d47621a4STim Haley zp->z_acl_cached = NULL; 137fa9e4066Sahrens return (0); 138fa9e4066Sahrens } 139fa9e4066Sahrens 140fa9e4066Sahrens /*ARGSUSED*/ 141fa9e4066Sahrens static void 142b5fca8f8Stomee zfs_znode_cache_destructor(void *buf, void *arg) 143fa9e4066Sahrens { 144fa9e4066Sahrens znode_t *zp = buf; 145fa9e4066Sahrens 146b5fca8f8Stomee ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs)); 147b5fca8f8Stomee ASSERT(ZTOV(zp)->v_data == zp); 148b5fca8f8Stomee vn_free(ZTOV(zp)); 149b5fca8f8Stomee ASSERT(!list_link_active(&zp->z_link_node)); 150fa9e4066Sahrens mutex_destroy(&zp->z_lock); 151104e2ed7Sperrin rw_destroy(&zp->z_parent_lock); 152af2c4821Smaybee rw_destroy(&zp->z_name_lock); 153fa9e4066Sahrens mutex_destroy(&zp->z_acl_lock); 154104e2ed7Sperrin avl_destroy(&zp->z_range_avl); 155c25056deSgw mutex_destroy(&zp->z_range_lock); 156fa9e4066Sahrens 1574ccbb6e7Sahrens ASSERT(zp->z_dbuf == NULL); 158b5fca8f8Stomee ASSERT(zp->z_dirlocks == NULL); 1594929fd5eSTim Haley ASSERT(zp->z_acl_cached == NULL); 160b5fca8f8Stomee } 161b5fca8f8Stomee 162b5fca8f8Stomee #ifdef ZNODE_STATS 163b5fca8f8Stomee static struct { 164b5fca8f8Stomee uint64_t zms_zfsvfs_invalid; 1654e9583b2STom Erickson uint64_t zms_zfsvfs_recheck1; 166b5fca8f8Stomee uint64_t zms_zfsvfs_unmounted; 1674e9583b2STom Erickson uint64_t zms_zfsvfs_recheck2; 168a66b2b35STom Erickson uint64_t zms_obj_held; 169b5fca8f8Stomee uint64_t zms_vnode_locked; 170a66b2b35STom Erickson uint64_t zms_not_only_dnlc; 171b5fca8f8Stomee } znode_move_stats; 172b5fca8f8Stomee #endif /* ZNODE_STATS */ 173b5fca8f8Stomee 174b5fca8f8Stomee static void 175b5fca8f8Stomee zfs_znode_move_impl(znode_t *ozp, znode_t *nzp) 176b5fca8f8Stomee { 177b5fca8f8Stomee vnode_t *vp; 178b5fca8f8Stomee 179b5fca8f8Stomee /* Copy fields. */ 180b5fca8f8Stomee nzp->z_zfsvfs = ozp->z_zfsvfs; 181b5fca8f8Stomee 182b5fca8f8Stomee /* Swap vnodes. */ 183b5fca8f8Stomee vp = nzp->z_vnode; 184b5fca8f8Stomee nzp->z_vnode = ozp->z_vnode; 185b5fca8f8Stomee ozp->z_vnode = vp; /* let destructor free the overwritten vnode */ 186b5fca8f8Stomee ZTOV(ozp)->v_data = ozp; 187b5fca8f8Stomee ZTOV(nzp)->v_data = nzp; 188b5fca8f8Stomee 189b5fca8f8Stomee nzp->z_id = ozp->z_id; 190b5fca8f8Stomee ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */ 191b5fca8f8Stomee ASSERT(avl_numnodes(&ozp->z_range_avl) == 0); 192b5fca8f8Stomee nzp->z_unlinked = ozp->z_unlinked; 193b5fca8f8Stomee nzp->z_atime_dirty = ozp->z_atime_dirty; 194b5fca8f8Stomee nzp->z_zn_prefetch = ozp->z_zn_prefetch; 195b5fca8f8Stomee nzp->z_blksz = ozp->z_blksz; 196b5fca8f8Stomee nzp->z_seq = ozp->z_seq; 197b5fca8f8Stomee nzp->z_mapcnt = ozp->z_mapcnt; 198b5fca8f8Stomee nzp->z_last_itx = ozp->z_last_itx; 199b5fca8f8Stomee nzp->z_gen = ozp->z_gen; 200b5fca8f8Stomee nzp->z_sync_cnt = ozp->z_sync_cnt; 201b5fca8f8Stomee nzp->z_phys = ozp->z_phys; 202b5fca8f8Stomee nzp->z_dbuf = ozp->z_dbuf; 203d98a6232SMark Shellenbaum 204d98a6232SMark Shellenbaum /* 205*6638ae1dSMark Shellenbaum * Since this is just an idle znode and kmem is already dealing with 206*6638ae1dSMark Shellenbaum * memory pressure, release any cached ACL. 207d98a6232SMark Shellenbaum */ 208d98a6232SMark Shellenbaum if (ozp->z_acl_cached) { 209d98a6232SMark Shellenbaum zfs_acl_free(ozp->z_acl_cached); 210d98a6232SMark Shellenbaum ozp->z_acl_cached = NULL; 211d98a6232SMark Shellenbaum } 212b5fca8f8Stomee 213b5fca8f8Stomee /* Update back pointers. */ 214b5fca8f8Stomee (void) dmu_buf_update_user(nzp->z_dbuf, ozp, nzp, &nzp->z_phys, 215b5fca8f8Stomee znode_evict_error); 216b5fca8f8Stomee 217b5fca8f8Stomee /* 218b5fca8f8Stomee * Invalidate the original znode by clearing fields that provide a 219b5fca8f8Stomee * pointer back to the znode. Set the low bit of the vfs pointer to 220b5fca8f8Stomee * ensure that zfs_znode_move() recognizes the znode as invalid in any 221b5fca8f8Stomee * subsequent callback. 222b5fca8f8Stomee */ 223b5fca8f8Stomee ozp->z_dbuf = NULL; 224b5fca8f8Stomee POINTER_INVALIDATE(&ozp->z_zfsvfs); 225b5fca8f8Stomee } 226b5fca8f8Stomee 227b5fca8f8Stomee /*ARGSUSED*/ 228b5fca8f8Stomee static kmem_cbrc_t 229b5fca8f8Stomee zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg) 230b5fca8f8Stomee { 231b5fca8f8Stomee znode_t *ozp = buf, *nzp = newbuf; 232b5fca8f8Stomee zfsvfs_t *zfsvfs; 233b5fca8f8Stomee vnode_t *vp; 234b5fca8f8Stomee 235b5fca8f8Stomee /* 236b5fca8f8Stomee * The znode is on the file system's list of known znodes if the vfs 237b5fca8f8Stomee * pointer is valid. We set the low bit of the vfs pointer when freeing 238b5fca8f8Stomee * the znode to invalidate it, and the memory patterns written by kmem 239b5fca8f8Stomee * (baddcafe and deadbeef) set at least one of the two low bits. A newly 240b5fca8f8Stomee * created znode sets the vfs pointer last of all to indicate that the 241b5fca8f8Stomee * znode is known and in a valid state to be moved by this function. 242b5fca8f8Stomee */ 243b5fca8f8Stomee zfsvfs = ozp->z_zfsvfs; 244b5fca8f8Stomee if (!POINTER_IS_VALID(zfsvfs)) { 245b5fca8f8Stomee ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid); 246b5fca8f8Stomee return (KMEM_CBRC_DONT_KNOW); 247b5fca8f8Stomee } 248b5fca8f8Stomee 249b5fca8f8Stomee /* 2504e9583b2STom Erickson * Close a small window in which it's possible that the filesystem could 2514e9583b2STom Erickson * be unmounted and freed, and zfsvfs, though valid in the previous 2524e9583b2STom Erickson * statement, could point to unrelated memory by the time we try to 2534e9583b2STom Erickson * prevent the filesystem from being unmounted. 2544e9583b2STom Erickson */ 2554e9583b2STom Erickson rw_enter(&zfsvfs_lock, RW_WRITER); 2564e9583b2STom Erickson if (zfsvfs != ozp->z_zfsvfs) { 2574e9583b2STom Erickson rw_exit(&zfsvfs_lock); 2584e9583b2STom Erickson ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1); 2594e9583b2STom Erickson return (KMEM_CBRC_DONT_KNOW); 2604e9583b2STom Erickson } 2614e9583b2STom Erickson 2624e9583b2STom Erickson /* 2634e9583b2STom Erickson * If the znode is still valid, then so is the file system. We know that 2644e9583b2STom Erickson * no valid file system can be freed while we hold zfsvfs_lock, so we 2654e9583b2STom Erickson * can safely ensure that the filesystem is not and will not be 2664e9583b2STom Erickson * unmounted. The next statement is equivalent to ZFS_ENTER(). 267b5fca8f8Stomee */ 26814843421SMatthew Ahrens rrw_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG); 26914843421SMatthew Ahrens if (zfsvfs->z_unmounted) { 27014843421SMatthew Ahrens ZFS_EXIT(zfsvfs); 2714e9583b2STom Erickson rw_exit(&zfsvfs_lock); 272b5fca8f8Stomee ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted); 273b5fca8f8Stomee return (KMEM_CBRC_DONT_KNOW); 274b5fca8f8Stomee } 2754e9583b2STom Erickson rw_exit(&zfsvfs_lock); 276b5fca8f8Stomee 277b5fca8f8Stomee mutex_enter(&zfsvfs->z_znodes_lock); 278b5fca8f8Stomee /* 279b5fca8f8Stomee * Recheck the vfs pointer in case the znode was removed just before 280b5fca8f8Stomee * acquiring the lock. 281b5fca8f8Stomee */ 282b5fca8f8Stomee if (zfsvfs != ozp->z_zfsvfs) { 283b5fca8f8Stomee mutex_exit(&zfsvfs->z_znodes_lock); 284b5fca8f8Stomee ZFS_EXIT(zfsvfs); 2854e9583b2STom Erickson ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2); 286b5fca8f8Stomee return (KMEM_CBRC_DONT_KNOW); 287b5fca8f8Stomee } 288b5fca8f8Stomee 289b5fca8f8Stomee /* 290b5fca8f8Stomee * At this point we know that as long as we hold z_znodes_lock, the 291b5fca8f8Stomee * znode cannot be freed and fields within the znode can be safely 292a66b2b35STom Erickson * accessed. Now, prevent a race with zfs_zget(). 293b5fca8f8Stomee */ 294a66b2b35STom Erickson if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) { 295a66b2b35STom Erickson mutex_exit(&zfsvfs->z_znodes_lock); 296a66b2b35STom Erickson ZFS_EXIT(zfsvfs); 297a66b2b35STom Erickson ZNODE_STAT_ADD(znode_move_stats.zms_obj_held); 298a66b2b35STom Erickson return (KMEM_CBRC_LATER); 299a66b2b35STom Erickson } 300a66b2b35STom Erickson 301b5fca8f8Stomee vp = ZTOV(ozp); 302b5fca8f8Stomee if (mutex_tryenter(&vp->v_lock) == 0) { 303a66b2b35STom Erickson ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id); 304b5fca8f8Stomee mutex_exit(&zfsvfs->z_znodes_lock); 305b5fca8f8Stomee ZFS_EXIT(zfsvfs); 306b5fca8f8Stomee ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked); 307b5fca8f8Stomee return (KMEM_CBRC_LATER); 308b5fca8f8Stomee } 309a66b2b35STom Erickson 310b5fca8f8Stomee /* Only move znodes that are referenced _only_ by the DNLC. */ 311b5fca8f8Stomee if (vp->v_count != 1 || !vn_in_dnlc(vp)) { 312b5fca8f8Stomee mutex_exit(&vp->v_lock); 313a66b2b35STom Erickson ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id); 314b5fca8f8Stomee mutex_exit(&zfsvfs->z_znodes_lock); 315b5fca8f8Stomee ZFS_EXIT(zfsvfs); 316a66b2b35STom Erickson ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc); 317b5fca8f8Stomee return (KMEM_CBRC_LATER); 318b5fca8f8Stomee } 319b5fca8f8Stomee 320b5fca8f8Stomee /* 321b5fca8f8Stomee * The znode is known and in a valid state to move. We're holding the 322b5fca8f8Stomee * locks needed to execute the critical section. 323b5fca8f8Stomee */ 324b5fca8f8Stomee zfs_znode_move_impl(ozp, nzp); 325b5fca8f8Stomee mutex_exit(&vp->v_lock); 326a66b2b35STom Erickson ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id); 327b5fca8f8Stomee 328b5fca8f8Stomee list_link_replace(&ozp->z_link_node, &nzp->z_link_node); 329b5fca8f8Stomee mutex_exit(&zfsvfs->z_znodes_lock); 330b5fca8f8Stomee ZFS_EXIT(zfsvfs); 331b5fca8f8Stomee 332b5fca8f8Stomee return (KMEM_CBRC_YES); 333fa9e4066Sahrens } 334fa9e4066Sahrens 335fa9e4066Sahrens void 336fa9e4066Sahrens zfs_znode_init(void) 337fa9e4066Sahrens { 338fa9e4066Sahrens /* 339fa9e4066Sahrens * Initialize zcache 340fa9e4066Sahrens */ 3414e9583b2STom Erickson rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL); 342fa9e4066Sahrens ASSERT(znode_cache == NULL); 343fa9e4066Sahrens znode_cache = kmem_cache_create("zfs_znode_cache", 344fa9e4066Sahrens sizeof (znode_t), 0, zfs_znode_cache_constructor, 345fa9e4066Sahrens zfs_znode_cache_destructor, NULL, NULL, NULL, 0); 346b5fca8f8Stomee kmem_cache_set_move(znode_cache, zfs_znode_move); 347fa9e4066Sahrens } 348fa9e4066Sahrens 349fa9e4066Sahrens void 350fa9e4066Sahrens zfs_znode_fini(void) 351fa9e4066Sahrens { 352fa9e4066Sahrens /* 353fa9e4066Sahrens * Cleanup vfs & vnode ops 354fa9e4066Sahrens */ 355fa9e4066Sahrens zfs_remove_op_tables(); 356fa9e4066Sahrens 357fa9e4066Sahrens /* 358fa9e4066Sahrens * Cleanup zcache 359fa9e4066Sahrens */ 360fa9e4066Sahrens if (znode_cache) 361fa9e4066Sahrens kmem_cache_destroy(znode_cache); 362fa9e4066Sahrens znode_cache = NULL; 3634e9583b2STom Erickson rw_destroy(&zfsvfs_lock); 364fa9e4066Sahrens } 365fa9e4066Sahrens 366fa9e4066Sahrens struct vnodeops *zfs_dvnodeops; 367fa9e4066Sahrens struct vnodeops *zfs_fvnodeops; 368fa9e4066Sahrens struct vnodeops *zfs_symvnodeops; 369fa9e4066Sahrens struct vnodeops *zfs_xdvnodeops; 370fa9e4066Sahrens struct vnodeops *zfs_evnodeops; 371743a77edSAlan Wright struct vnodeops *zfs_sharevnodeops; 372fa9e4066Sahrens 373fa9e4066Sahrens void 374fa9e4066Sahrens zfs_remove_op_tables() 375fa9e4066Sahrens { 376fa9e4066Sahrens /* 377fa9e4066Sahrens * Remove vfs ops 378fa9e4066Sahrens */ 379fa9e4066Sahrens ASSERT(zfsfstype); 380fa9e4066Sahrens (void) vfs_freevfsops_by_type(zfsfstype); 381fa9e4066Sahrens zfsfstype = 0; 382fa9e4066Sahrens 383fa9e4066Sahrens /* 384fa9e4066Sahrens * Remove vnode ops 385fa9e4066Sahrens */ 386fa9e4066Sahrens if (zfs_dvnodeops) 387fa9e4066Sahrens vn_freevnodeops(zfs_dvnodeops); 388fa9e4066Sahrens if (zfs_fvnodeops) 389fa9e4066Sahrens vn_freevnodeops(zfs_fvnodeops); 390fa9e4066Sahrens if (zfs_symvnodeops) 391fa9e4066Sahrens vn_freevnodeops(zfs_symvnodeops); 392fa9e4066Sahrens if (zfs_xdvnodeops) 393fa9e4066Sahrens vn_freevnodeops(zfs_xdvnodeops); 394fa9e4066Sahrens if (zfs_evnodeops) 395fa9e4066Sahrens vn_freevnodeops(zfs_evnodeops); 396743a77edSAlan Wright if (zfs_sharevnodeops) 397743a77edSAlan Wright vn_freevnodeops(zfs_sharevnodeops); 398fa9e4066Sahrens 399fa9e4066Sahrens zfs_dvnodeops = NULL; 400fa9e4066Sahrens zfs_fvnodeops = NULL; 401fa9e4066Sahrens zfs_symvnodeops = NULL; 402fa9e4066Sahrens zfs_xdvnodeops = NULL; 403fa9e4066Sahrens zfs_evnodeops = NULL; 404743a77edSAlan Wright zfs_sharevnodeops = NULL; 405fa9e4066Sahrens } 406fa9e4066Sahrens 407fa9e4066Sahrens extern const fs_operation_def_t zfs_dvnodeops_template[]; 408fa9e4066Sahrens extern const fs_operation_def_t zfs_fvnodeops_template[]; 409fa9e4066Sahrens extern const fs_operation_def_t zfs_xdvnodeops_template[]; 410fa9e4066Sahrens extern const fs_operation_def_t zfs_symvnodeops_template[]; 411fa9e4066Sahrens extern const fs_operation_def_t zfs_evnodeops_template[]; 412743a77edSAlan Wright extern const fs_operation_def_t zfs_sharevnodeops_template[]; 413fa9e4066Sahrens 414fa9e4066Sahrens int 415fa9e4066Sahrens zfs_create_op_tables() 416fa9e4066Sahrens { 417fa9e4066Sahrens int error; 418fa9e4066Sahrens 419fa9e4066Sahrens /* 420fa9e4066Sahrens * zfs_dvnodeops can be set if mod_remove() calls mod_installfs() 421fa9e4066Sahrens * due to a failure to remove the the 2nd modlinkage (zfs_modldrv). 422fa9e4066Sahrens * In this case we just return as the ops vectors are already set up. 423fa9e4066Sahrens */ 424fa9e4066Sahrens if (zfs_dvnodeops) 425fa9e4066Sahrens return (0); 426fa9e4066Sahrens 427fa9e4066Sahrens error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template, 428fa9e4066Sahrens &zfs_dvnodeops); 429fa9e4066Sahrens if (error) 430fa9e4066Sahrens return (error); 431fa9e4066Sahrens 432fa9e4066Sahrens error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template, 433fa9e4066Sahrens &zfs_fvnodeops); 434fa9e4066Sahrens if (error) 435fa9e4066Sahrens return (error); 436fa9e4066Sahrens 437fa9e4066Sahrens error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template, 438fa9e4066Sahrens &zfs_symvnodeops); 439fa9e4066Sahrens if (error) 440fa9e4066Sahrens return (error); 441fa9e4066Sahrens 442fa9e4066Sahrens error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template, 443fa9e4066Sahrens &zfs_xdvnodeops); 444fa9e4066Sahrens if (error) 445fa9e4066Sahrens return (error); 446fa9e4066Sahrens 447fa9e4066Sahrens error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template, 448fa9e4066Sahrens &zfs_evnodeops); 449743a77edSAlan Wright if (error) 450743a77edSAlan Wright return (error); 451743a77edSAlan Wright 452743a77edSAlan Wright error = vn_make_ops(MNTTYPE_ZFS, zfs_sharevnodeops_template, 453743a77edSAlan Wright &zfs_sharevnodeops); 454743a77edSAlan Wright 455743a77edSAlan Wright return (error); 456743a77edSAlan Wright } 457743a77edSAlan Wright 4589e1320c0SMark Shellenbaum int 459743a77edSAlan Wright zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx) 460743a77edSAlan Wright { 46189459e17SMark Shellenbaum zfs_acl_ids_t acl_ids; 462743a77edSAlan Wright vattr_t vattr; 463743a77edSAlan Wright znode_t *sharezp; 464743a77edSAlan Wright vnode_t *vp; 465743a77edSAlan Wright znode_t *zp; 466743a77edSAlan Wright int error; 467743a77edSAlan Wright 468743a77edSAlan Wright vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 469743a77edSAlan Wright vattr.va_type = VDIR; 470743a77edSAlan Wright vattr.va_mode = S_IFDIR|0555; 471743a77edSAlan Wright vattr.va_uid = crgetuid(kcred); 472743a77edSAlan Wright vattr.va_gid = crgetgid(kcred); 473743a77edSAlan Wright 474743a77edSAlan Wright sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP); 475743a77edSAlan Wright sharezp->z_unlinked = 0; 476743a77edSAlan Wright sharezp->z_atime_dirty = 0; 477743a77edSAlan Wright sharezp->z_zfsvfs = zfsvfs; 478743a77edSAlan Wright 479743a77edSAlan Wright vp = ZTOV(sharezp); 480743a77edSAlan Wright vn_reinit(vp); 481743a77edSAlan Wright vp->v_type = VDIR; 482743a77edSAlan Wright 48389459e17SMark Shellenbaum VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr, 48489459e17SMark Shellenbaum kcred, NULL, &acl_ids)); 485743a77edSAlan Wright zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, 48689459e17SMark Shellenbaum &zp, 0, &acl_ids); 487743a77edSAlan Wright ASSERT3P(zp, ==, sharezp); 488743a77edSAlan Wright ASSERT(!vn_in_dnlc(ZTOV(sharezp))); /* not valid to move */ 489743a77edSAlan Wright POINTER_INVALIDATE(&sharezp->z_zfsvfs); 490743a77edSAlan Wright error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ, 491743a77edSAlan Wright ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx); 492743a77edSAlan Wright zfsvfs->z_shares_dir = sharezp->z_id; 493743a77edSAlan Wright 49489459e17SMark Shellenbaum zfs_acl_ids_free(&acl_ids); 495743a77edSAlan Wright ZTOV(sharezp)->v_count = 0; 496743a77edSAlan Wright dmu_buf_rele(sharezp->z_dbuf, NULL); 497743a77edSAlan Wright sharezp->z_dbuf = NULL; 498743a77edSAlan Wright kmem_cache_free(znode_cache, sharezp); 499fa9e4066Sahrens 500fa9e4066Sahrens return (error); 501fa9e4066Sahrens } 502fa9e4066Sahrens 50372fc53bcSmarks /* 50472fc53bcSmarks * define a couple of values we need available 50572fc53bcSmarks * for both 64 and 32 bit environments. 50672fc53bcSmarks */ 50772fc53bcSmarks #ifndef NBITSMINOR64 50872fc53bcSmarks #define NBITSMINOR64 32 50972fc53bcSmarks #endif 51072fc53bcSmarks #ifndef MAXMAJ64 51172fc53bcSmarks #define MAXMAJ64 0xffffffffUL 51272fc53bcSmarks #endif 51372fc53bcSmarks #ifndef MAXMIN64 51472fc53bcSmarks #define MAXMIN64 0xffffffffUL 51572fc53bcSmarks #endif 51672fc53bcSmarks 51772fc53bcSmarks /* 51872fc53bcSmarks * Create special expldev for ZFS private use. 51972fc53bcSmarks * Can't use standard expldev since it doesn't do 52072fc53bcSmarks * what we want. The standard expldev() takes a 52172fc53bcSmarks * dev32_t in LP64 and expands it to a long dev_t. 52272fc53bcSmarks * We need an interface that takes a dev32_t in ILP32 52372fc53bcSmarks * and expands it to a long dev_t. 52472fc53bcSmarks */ 52572fc53bcSmarks static uint64_t 52672fc53bcSmarks zfs_expldev(dev_t dev) 52772fc53bcSmarks { 52872fc53bcSmarks #ifndef _LP64 52972fc53bcSmarks major_t major = (major_t)dev >> NBITSMINOR32 & MAXMAJ32; 53072fc53bcSmarks return (((uint64_t)major << NBITSMINOR64) | 53172fc53bcSmarks ((minor_t)dev & MAXMIN32)); 53272fc53bcSmarks #else 53372fc53bcSmarks return (dev); 53472fc53bcSmarks #endif 53572fc53bcSmarks } 53672fc53bcSmarks 53772fc53bcSmarks /* 53872fc53bcSmarks * Special cmpldev for ZFS private use. 53972fc53bcSmarks * Can't use standard cmpldev since it takes 54072fc53bcSmarks * a long dev_t and compresses it to dev32_t in 54172fc53bcSmarks * LP64. We need to do a compaction of a long dev_t 54272fc53bcSmarks * to a dev32_t in ILP32. 54372fc53bcSmarks */ 54472fc53bcSmarks dev_t 54572fc53bcSmarks zfs_cmpldev(uint64_t dev) 54672fc53bcSmarks { 54772fc53bcSmarks #ifndef _LP64 54872fc53bcSmarks minor_t minor = (minor_t)dev & MAXMIN64; 54972fc53bcSmarks major_t major = (major_t)(dev >> NBITSMINOR64) & MAXMAJ64; 55072fc53bcSmarks 55172fc53bcSmarks if (major > MAXMAJ32 || minor > MAXMIN32) 55272fc53bcSmarks return (NODEV32); 55372fc53bcSmarks 55472fc53bcSmarks return (((dev32_t)major << NBITSMINOR32) | minor); 55572fc53bcSmarks #else 55672fc53bcSmarks return (dev); 55772fc53bcSmarks #endif 55872fc53bcSmarks } 55972fc53bcSmarks 5604ccbb6e7Sahrens static void 561b5fca8f8Stomee zfs_znode_dmu_init(zfsvfs_t *zfsvfs, znode_t *zp, dmu_buf_t *db) 5624ccbb6e7Sahrens { 5634ccbb6e7Sahrens znode_t *nzp; 5644ccbb6e7Sahrens 565b5fca8f8Stomee ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs)); 566b5fca8f8Stomee ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id))); 5674ccbb6e7Sahrens 5684ccbb6e7Sahrens mutex_enter(&zp->z_lock); 5694ccbb6e7Sahrens 5704ccbb6e7Sahrens ASSERT(zp->z_dbuf == NULL); 571*6638ae1dSMark Shellenbaum ASSERT(zp->z_acl_cached == NULL); 5724ccbb6e7Sahrens zp->z_dbuf = db; 573874395d5Smaybee nzp = dmu_buf_set_user_ie(db, zp, &zp->z_phys, znode_evict_error); 5744ccbb6e7Sahrens 5754ccbb6e7Sahrens /* 5764ccbb6e7Sahrens * there should be no 5774ccbb6e7Sahrens * concurrent zgets on this object. 5784ccbb6e7Sahrens */ 5794ccbb6e7Sahrens if (nzp != NULL) 580903a11ebSrh panic("existing znode %p for dbuf %p", (void *)nzp, (void *)db); 5814ccbb6e7Sahrens 5824ccbb6e7Sahrens /* 5834ccbb6e7Sahrens * Slap on VROOT if we are the root znode 5844ccbb6e7Sahrens */ 5854ccbb6e7Sahrens if (zp->z_id == zfsvfs->z_root) 5864ccbb6e7Sahrens ZTOV(zp)->v_flag |= VROOT; 5874ccbb6e7Sahrens 5884ccbb6e7Sahrens mutex_exit(&zp->z_lock); 5894ccbb6e7Sahrens vn_exists(ZTOV(zp)); 5904ccbb6e7Sahrens } 5914ccbb6e7Sahrens 592874395d5Smaybee void 5934ccbb6e7Sahrens zfs_znode_dmu_fini(znode_t *zp) 5944ccbb6e7Sahrens { 5954ccbb6e7Sahrens dmu_buf_t *db = zp->z_dbuf; 596b5fca8f8Stomee ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) || 597b5fca8f8Stomee zp->z_unlinked || 598874395d5Smaybee RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock)); 5994ccbb6e7Sahrens ASSERT(zp->z_dbuf != NULL); 6004ccbb6e7Sahrens zp->z_dbuf = NULL; 601874395d5Smaybee VERIFY(zp == dmu_buf_update_user(db, zp, NULL, NULL, NULL)); 6024ccbb6e7Sahrens dmu_buf_rele(db, NULL); 6034ccbb6e7Sahrens } 6044ccbb6e7Sahrens 605fa9e4066Sahrens /* 606fa9e4066Sahrens * Construct a new znode/vnode and intialize. 607fa9e4066Sahrens * 608fa9e4066Sahrens * This does not do a call to dmu_set_user() that is 609fa9e4066Sahrens * up to the caller to do, in case you don't want to 610fa9e4066Sahrens * return the znode 611fa9e4066Sahrens */ 612ea8dc4b6Seschrock static znode_t * 6134ccbb6e7Sahrens zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz) 614fa9e4066Sahrens { 615fa9e4066Sahrens znode_t *zp; 616fa9e4066Sahrens vnode_t *vp; 617fa9e4066Sahrens 618fa9e4066Sahrens zp = kmem_cache_alloc(znode_cache, KM_SLEEP); 619fa9e4066Sahrens 620fa9e4066Sahrens ASSERT(zp->z_dirlocks == NULL); 6214ccbb6e7Sahrens ASSERT(zp->z_dbuf == NULL); 622b5fca8f8Stomee ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs)); 623fa9e4066Sahrens 624b5fca8f8Stomee /* 625b5fca8f8Stomee * Defer setting z_zfsvfs until the znode is ready to be a candidate for 626b5fca8f8Stomee * the zfs_znode_move() callback. 627b5fca8f8Stomee */ 6284ccbb6e7Sahrens zp->z_phys = NULL; 629893a6d32Sahrens zp->z_unlinked = 0; 630fa9e4066Sahrens zp->z_atime_dirty = 0; 631fa9e4066Sahrens zp->z_mapcnt = 0; 632fa9e4066Sahrens zp->z_last_itx = 0; 6334ccbb6e7Sahrens zp->z_id = db->db_object; 634fa9e4066Sahrens zp->z_blksz = blksz; 635fa9e4066Sahrens zp->z_seq = 0x7A4653; 63667bd71c6Sperrin zp->z_sync_cnt = 0; 6374ccbb6e7Sahrens 6384ccbb6e7Sahrens vp = ZTOV(zp); 6394ccbb6e7Sahrens vn_reinit(vp); 6404ccbb6e7Sahrens 641b5fca8f8Stomee zfs_znode_dmu_init(zfsvfs, zp, db); 6424ccbb6e7Sahrens 643f18faf3fSek zp->z_gen = zp->z_phys->zp_gen; 644fa9e4066Sahrens 645fa9e4066Sahrens vp->v_vfsp = zfsvfs->z_parent->z_vfs; 646fa9e4066Sahrens vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode); 647fa9e4066Sahrens 648fa9e4066Sahrens switch (vp->v_type) { 649fa9e4066Sahrens case VDIR: 650fa9e4066Sahrens if (zp->z_phys->zp_flags & ZFS_XATTR) { 651fa9e4066Sahrens vn_setops(vp, zfs_xdvnodeops); 652fa9e4066Sahrens vp->v_flag |= V_XATTRDIR; 6534ccbb6e7Sahrens } else { 654fa9e4066Sahrens vn_setops(vp, zfs_dvnodeops); 6554ccbb6e7Sahrens } 6567f6e3e7dSperrin zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */ 657fa9e4066Sahrens break; 658fa9e4066Sahrens case VBLK: 659fa9e4066Sahrens case VCHR: 66072fc53bcSmarks vp->v_rdev = zfs_cmpldev(zp->z_phys->zp_rdev); 661fa9e4066Sahrens /*FALLTHROUGH*/ 662fa9e4066Sahrens case VFIFO: 663fa9e4066Sahrens case VSOCK: 664fa9e4066Sahrens case VDOOR: 665fa9e4066Sahrens vn_setops(vp, zfs_fvnodeops); 666fa9e4066Sahrens break; 667fa9e4066Sahrens case VREG: 668fa9e4066Sahrens vp->v_flag |= VMODSORT; 669743a77edSAlan Wright if (zp->z_phys->zp_parent == zfsvfs->z_shares_dir) 670743a77edSAlan Wright vn_setops(vp, zfs_sharevnodeops); 671743a77edSAlan Wright else 672743a77edSAlan Wright vn_setops(vp, zfs_fvnodeops); 673fa9e4066Sahrens break; 674fa9e4066Sahrens case VLNK: 675fa9e4066Sahrens vn_setops(vp, zfs_symvnodeops); 676fa9e4066Sahrens break; 677fa9e4066Sahrens default: 678fa9e4066Sahrens vn_setops(vp, zfs_evnodeops); 679fa9e4066Sahrens break; 680fa9e4066Sahrens } 681fa9e4066Sahrens 682b5fca8f8Stomee mutex_enter(&zfsvfs->z_znodes_lock); 683b5fca8f8Stomee list_insert_tail(&zfsvfs->z_all_znodes, zp); 684b5fca8f8Stomee membar_producer(); 685b5fca8f8Stomee /* 686b5fca8f8Stomee * Everything else must be valid before assigning z_zfsvfs makes the 687b5fca8f8Stomee * znode eligible for zfs_znode_move(). 688b5fca8f8Stomee */ 689b5fca8f8Stomee zp->z_zfsvfs = zfsvfs; 690b5fca8f8Stomee mutex_exit(&zfsvfs->z_znodes_lock); 691b5fca8f8Stomee 692874395d5Smaybee VFS_HOLD(zfsvfs->z_vfs); 693fa9e4066Sahrens return (zp); 694fa9e4066Sahrens } 695fa9e4066Sahrens 696fa9e4066Sahrens /* 697fa9e4066Sahrens * Create a new DMU object to hold a zfs znode. 698fa9e4066Sahrens * 699fa9e4066Sahrens * IN: dzp - parent directory for new znode 700fa9e4066Sahrens * vap - file attributes for new znode 701fa9e4066Sahrens * tx - dmu transaction id for zap operations 702fa9e4066Sahrens * cr - credentials of caller 703fa9e4066Sahrens * flag - flags: 704fa9e4066Sahrens * IS_ROOT_NODE - new object will be root 705fa9e4066Sahrens * IS_XATTR - new object is an attribute 706fa9e4066Sahrens * IS_REPLAY - intent log replay 707da6c28aaSamw * bonuslen - length of bonus buffer 708da6c28aaSamw * setaclp - File/Dir initial ACL 709da6c28aaSamw * fuidp - Tracks fuid allocation. 710fa9e4066Sahrens * 7114ccbb6e7Sahrens * OUT: zpp - allocated znode 712fa9e4066Sahrens * 713fa9e4066Sahrens */ 714fa9e4066Sahrens void 7154ccbb6e7Sahrens zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr, 71689459e17SMark Shellenbaum uint_t flag, znode_t **zpp, int bonuslen, zfs_acl_ids_t *acl_ids) 717fa9e4066Sahrens { 7184ccbb6e7Sahrens dmu_buf_t *db; 719fa9e4066Sahrens znode_phys_t *pzp; 720fa9e4066Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 721fa9e4066Sahrens timestruc_t now; 7224ccbb6e7Sahrens uint64_t gen, obj; 723fa9e4066Sahrens int err; 724fa9e4066Sahrens 725fa9e4066Sahrens ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE)); 726fa9e4066Sahrens 7271209a471SNeil Perrin if (zfsvfs->z_replay) { 7284ccbb6e7Sahrens obj = vap->va_nodeid; 729fa9e4066Sahrens flag |= IS_REPLAY; 730fa9e4066Sahrens now = vap->va_ctime; /* see zfs_replay_create() */ 731fa9e4066Sahrens gen = vap->va_nblocks; /* ditto */ 732fa9e4066Sahrens } else { 7334ccbb6e7Sahrens obj = 0; 734fa9e4066Sahrens gethrestime(&now); 735fa9e4066Sahrens gen = dmu_tx_get_txg(tx); 736fa9e4066Sahrens } 737fa9e4066Sahrens 738fa9e4066Sahrens /* 739fa9e4066Sahrens * Create a new DMU object. 740fa9e4066Sahrens */ 741ea8dc4b6Seschrock /* 742ea8dc4b6Seschrock * There's currently no mechanism for pre-reading the blocks that will 743ea8dc4b6Seschrock * be to needed allocate a new object, so we accept the small chance 744ea8dc4b6Seschrock * that there will be an i/o error and we will fail one of the 745ea8dc4b6Seschrock * assertions below. 746ea8dc4b6Seschrock */ 747fa9e4066Sahrens if (vap->va_type == VDIR) { 748fa9e4066Sahrens if (flag & IS_REPLAY) { 7494ccbb6e7Sahrens err = zap_create_claim_norm(zfsvfs->z_os, obj, 750da6c28aaSamw zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS, 751fa9e4066Sahrens DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx); 752fa9e4066Sahrens ASSERT3U(err, ==, 0); 753fa9e4066Sahrens } else { 7544ccbb6e7Sahrens obj = zap_create_norm(zfsvfs->z_os, 755da6c28aaSamw zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS, 756fa9e4066Sahrens DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx); 757fa9e4066Sahrens } 758fa9e4066Sahrens } else { 759fa9e4066Sahrens if (flag & IS_REPLAY) { 7604ccbb6e7Sahrens err = dmu_object_claim(zfsvfs->z_os, obj, 761fa9e4066Sahrens DMU_OT_PLAIN_FILE_CONTENTS, 0, 762fa9e4066Sahrens DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx); 763fa9e4066Sahrens ASSERT3U(err, ==, 0); 764fa9e4066Sahrens } else { 7654ccbb6e7Sahrens obj = dmu_object_alloc(zfsvfs->z_os, 766fa9e4066Sahrens DMU_OT_PLAIN_FILE_CONTENTS, 0, 767fa9e4066Sahrens DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx); 768fa9e4066Sahrens } 769fa9e4066Sahrens } 7704ccbb6e7Sahrens VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, obj, NULL, &db)); 7714ccbb6e7Sahrens dmu_buf_will_dirty(db, tx); 772fa9e4066Sahrens 773fa9e4066Sahrens /* 774fa9e4066Sahrens * Initialize the znode physical data to zero. 775fa9e4066Sahrens */ 7764ccbb6e7Sahrens ASSERT(db->db_size >= sizeof (znode_phys_t)); 7774ccbb6e7Sahrens bzero(db->db_data, db->db_size); 7784ccbb6e7Sahrens pzp = db->db_data; 779fa9e4066Sahrens 780fa9e4066Sahrens /* 781fa9e4066Sahrens * If this is the root, fix up the half-initialized parent pointer 782fa9e4066Sahrens * to reference the just-allocated physical data area. 783fa9e4066Sahrens */ 784fa9e4066Sahrens if (flag & IS_ROOT_NODE) { 785874395d5Smaybee dzp->z_dbuf = db; 786fa9e4066Sahrens dzp->z_phys = pzp; 7874ccbb6e7Sahrens dzp->z_id = obj; 788fa9e4066Sahrens } 789fa9e4066Sahrens 790fa9e4066Sahrens /* 791fa9e4066Sahrens * If parent is an xattr, so am I. 792fa9e4066Sahrens */ 793fa9e4066Sahrens if (dzp->z_phys->zp_flags & ZFS_XATTR) 794fa9e4066Sahrens flag |= IS_XATTR; 795fa9e4066Sahrens 796fa9e4066Sahrens if (vap->va_type == VBLK || vap->va_type == VCHR) { 79772fc53bcSmarks pzp->zp_rdev = zfs_expldev(vap->va_rdev); 798fa9e4066Sahrens } 799fa9e4066Sahrens 800da6c28aaSamw if (zfsvfs->z_use_fuids) 801da6c28aaSamw pzp->zp_flags = ZFS_ARCHIVE | ZFS_AV_MODIFIED; 802da6c28aaSamw 803fa9e4066Sahrens if (vap->va_type == VDIR) { 804fa9e4066Sahrens pzp->zp_size = 2; /* contents ("." and "..") */ 805fa9e4066Sahrens pzp->zp_links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1; 806fa9e4066Sahrens } 807fa9e4066Sahrens 808fa9e4066Sahrens pzp->zp_parent = dzp->z_id; 809fa9e4066Sahrens if (flag & IS_XATTR) 810fa9e4066Sahrens pzp->zp_flags |= ZFS_XATTR; 811fa9e4066Sahrens 812fa9e4066Sahrens pzp->zp_gen = gen; 813fa9e4066Sahrens 814fa9e4066Sahrens ZFS_TIME_ENCODE(&now, pzp->zp_crtime); 815fa9e4066Sahrens ZFS_TIME_ENCODE(&now, pzp->zp_ctime); 816fa9e4066Sahrens 817fa9e4066Sahrens if (vap->va_mask & AT_ATIME) { 818fa9e4066Sahrens ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime); 819fa9e4066Sahrens } else { 820fa9e4066Sahrens ZFS_TIME_ENCODE(&now, pzp->zp_atime); 821fa9e4066Sahrens } 822fa9e4066Sahrens 823fa9e4066Sahrens if (vap->va_mask & AT_MTIME) { 824fa9e4066Sahrens ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime); 825fa9e4066Sahrens } else { 826fa9e4066Sahrens ZFS_TIME_ENCODE(&now, pzp->zp_mtime); 827fa9e4066Sahrens } 828fa9e4066Sahrens 829fa9e4066Sahrens pzp->zp_mode = MAKEIMODE(vap->va_type, vap->va_mode); 830874395d5Smaybee if (!(flag & IS_ROOT_NODE)) { 831b5fca8f8Stomee ZFS_OBJ_HOLD_ENTER(zfsvfs, obj); 832874395d5Smaybee *zpp = zfs_znode_alloc(zfsvfs, db, 0); 833874395d5Smaybee ZFS_OBJ_HOLD_EXIT(zfsvfs, obj); 834874395d5Smaybee } else { 835874395d5Smaybee /* 836874395d5Smaybee * If we are creating the root node, the "parent" we 837874395d5Smaybee * passed in is the znode for the root. 838874395d5Smaybee */ 839874395d5Smaybee *zpp = dzp; 840874395d5Smaybee } 84189459e17SMark Shellenbaum pzp->zp_uid = acl_ids->z_fuid; 84289459e17SMark Shellenbaum pzp->zp_gid = acl_ids->z_fgid; 84389459e17SMark Shellenbaum pzp->zp_mode = acl_ids->z_mode; 84489459e17SMark Shellenbaum VERIFY(0 == zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx)); 84589459e17SMark Shellenbaum if (vap->va_mask & AT_XVATTR) 84689459e17SMark Shellenbaum zfs_xvattr_set(*zpp, (xvattr_t *)vap); 847fa9e4066Sahrens } 848fa9e4066Sahrens 849da6c28aaSamw void 850da6c28aaSamw zfs_xvattr_set(znode_t *zp, xvattr_t *xvap) 851da6c28aaSamw { 852da6c28aaSamw xoptattr_t *xoap; 853da6c28aaSamw 854da6c28aaSamw xoap = xva_getxoptattr(xvap); 855da6c28aaSamw ASSERT(xoap); 856da6c28aaSamw 857da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 858da6c28aaSamw ZFS_TIME_ENCODE(&xoap->xoa_createtime, zp->z_phys->zp_crtime); 859da6c28aaSamw XVA_SET_RTN(xvap, XAT_CREATETIME); 860da6c28aaSamw } 861da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 862da6c28aaSamw ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly); 863da6c28aaSamw XVA_SET_RTN(xvap, XAT_READONLY); 864da6c28aaSamw } 865da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 866da6c28aaSamw ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden); 867da6c28aaSamw XVA_SET_RTN(xvap, XAT_HIDDEN); 868da6c28aaSamw } 869da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 870da6c28aaSamw ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system); 871da6c28aaSamw XVA_SET_RTN(xvap, XAT_SYSTEM); 872da6c28aaSamw } 873da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 874da6c28aaSamw ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive); 875da6c28aaSamw XVA_SET_RTN(xvap, XAT_ARCHIVE); 876da6c28aaSamw } 877da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 878da6c28aaSamw ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable); 879da6c28aaSamw XVA_SET_RTN(xvap, XAT_IMMUTABLE); 880da6c28aaSamw } 881da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 882da6c28aaSamw ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink); 883da6c28aaSamw XVA_SET_RTN(xvap, XAT_NOUNLINK); 884da6c28aaSamw } 885da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 886da6c28aaSamw ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly); 887da6c28aaSamw XVA_SET_RTN(xvap, XAT_APPENDONLY); 888da6c28aaSamw } 889da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 890da6c28aaSamw ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump); 891da6c28aaSamw XVA_SET_RTN(xvap, XAT_NODUMP); 892da6c28aaSamw } 893da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 894da6c28aaSamw ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque); 895da6c28aaSamw XVA_SET_RTN(xvap, XAT_OPAQUE); 896da6c28aaSamw } 897da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 898da6c28aaSamw ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED, 899da6c28aaSamw xoap->xoa_av_quarantined); 900da6c28aaSamw XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 901da6c28aaSamw } 902da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 903da6c28aaSamw ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified); 904da6c28aaSamw XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 905da6c28aaSamw } 906da6c28aaSamw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) { 907da6c28aaSamw (void) memcpy(zp->z_phys + 1, xoap->xoa_av_scanstamp, 908da6c28aaSamw sizeof (xoap->xoa_av_scanstamp)); 909da6c28aaSamw zp->z_phys->zp_flags |= ZFS_BONUS_SCANSTAMP; 910da6c28aaSamw XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP); 911da6c28aaSamw } 912da6c28aaSamw } 913da6c28aaSamw 914fa9e4066Sahrens int 915fa9e4066Sahrens zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp) 916fa9e4066Sahrens { 917fa9e4066Sahrens dmu_object_info_t doi; 918fa9e4066Sahrens dmu_buf_t *db; 919fa9e4066Sahrens znode_t *zp; 920ea8dc4b6Seschrock int err; 921fa9e4066Sahrens 922fa9e4066Sahrens *zpp = NULL; 923fa9e4066Sahrens 924fa9e4066Sahrens ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num); 925fa9e4066Sahrens 926ea8dc4b6Seschrock err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db); 927ea8dc4b6Seschrock if (err) { 928fa9e4066Sahrens ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 929ea8dc4b6Seschrock return (err); 930fa9e4066Sahrens } 931fa9e4066Sahrens 932fa9e4066Sahrens dmu_object_info_from_db(db, &doi); 933fa9e4066Sahrens if (doi.doi_bonus_type != DMU_OT_ZNODE || 934fa9e4066Sahrens doi.doi_bonus_size < sizeof (znode_phys_t)) { 935ea8dc4b6Seschrock dmu_buf_rele(db, NULL); 936fa9e4066Sahrens ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 937fa9e4066Sahrens return (EINVAL); 938fa9e4066Sahrens } 939fa9e4066Sahrens 940fa9e4066Sahrens zp = dmu_buf_get_user(db); 941fa9e4066Sahrens if (zp != NULL) { 942fa9e4066Sahrens mutex_enter(&zp->z_lock); 943fa9e4066Sahrens 9444ccbb6e7Sahrens /* 9454ccbb6e7Sahrens * Since we do immediate eviction of the z_dbuf, we 9464ccbb6e7Sahrens * should never find a dbuf with a znode that doesn't 9474ccbb6e7Sahrens * know about the dbuf. 9484ccbb6e7Sahrens */ 9494ccbb6e7Sahrens ASSERT3P(zp->z_dbuf, ==, db); 950fa9e4066Sahrens ASSERT3U(zp->z_id, ==, obj_num); 951893a6d32Sahrens if (zp->z_unlinked) { 9524ccbb6e7Sahrens err = ENOENT; 953fa9e4066Sahrens } else { 9544ccbb6e7Sahrens VN_HOLD(ZTOV(zp)); 9554ccbb6e7Sahrens *zpp = zp; 9564ccbb6e7Sahrens err = 0; 957fa9e4066Sahrens } 9584ccbb6e7Sahrens dmu_buf_rele(db, NULL); 959fa9e4066Sahrens mutex_exit(&zp->z_lock); 960ea8dc4b6Seschrock ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 9614ccbb6e7Sahrens return (err); 962fa9e4066Sahrens } 963fa9e4066Sahrens 964fa9e4066Sahrens /* 965fa9e4066Sahrens * Not found create new znode/vnode 966fa9e4066Sahrens */ 9674ccbb6e7Sahrens zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size); 968ea8dc4b6Seschrock ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 969fa9e4066Sahrens *zpp = zp; 970fa9e4066Sahrens return (0); 971fa9e4066Sahrens } 972fa9e4066Sahrens 973f18faf3fSek int 974f18faf3fSek zfs_rezget(znode_t *zp) 975f18faf3fSek { 976f18faf3fSek zfsvfs_t *zfsvfs = zp->z_zfsvfs; 977f18faf3fSek dmu_object_info_t doi; 978f18faf3fSek dmu_buf_t *db; 979f18faf3fSek uint64_t obj_num = zp->z_id; 980f18faf3fSek int err; 981f18faf3fSek 982f18faf3fSek ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num); 983f18faf3fSek 984f18faf3fSek err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db); 985f18faf3fSek if (err) { 986f18faf3fSek ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 987f18faf3fSek return (err); 988f18faf3fSek } 989f18faf3fSek 990f18faf3fSek dmu_object_info_from_db(db, &doi); 991f18faf3fSek if (doi.doi_bonus_type != DMU_OT_ZNODE || 992f18faf3fSek doi.doi_bonus_size < sizeof (znode_phys_t)) { 993f18faf3fSek dmu_buf_rele(db, NULL); 994f18faf3fSek ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 995f18faf3fSek return (EINVAL); 996f18faf3fSek } 997f18faf3fSek 998f18faf3fSek if (((znode_phys_t *)db->db_data)->zp_gen != zp->z_gen) { 999f18faf3fSek dmu_buf_rele(db, NULL); 1000f18faf3fSek ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1001f18faf3fSek return (EIO); 1002f18faf3fSek } 1003f18faf3fSek 1004*6638ae1dSMark Shellenbaum mutex_enter(&zp->z_acl_lock); 1005*6638ae1dSMark Shellenbaum if (zp->z_acl_cached) { 1006*6638ae1dSMark Shellenbaum zfs_acl_free(zp->z_acl_cached); 1007*6638ae1dSMark Shellenbaum zp->z_acl_cached = NULL; 1008*6638ae1dSMark Shellenbaum } 1009*6638ae1dSMark Shellenbaum mutex_exit(&zp->z_acl_lock); 1010*6638ae1dSMark Shellenbaum 1011b5fca8f8Stomee zfs_znode_dmu_init(zfsvfs, zp, db); 1012f18faf3fSek zp->z_unlinked = (zp->z_phys->zp_links == 0); 10136166ad1cSek zp->z_blksz = doi.doi_data_block_size; 1014f18faf3fSek 1015f18faf3fSek ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1016f18faf3fSek 1017f18faf3fSek return (0); 1018f18faf3fSek } 1019f18faf3fSek 1020fa9e4066Sahrens void 1021fa9e4066Sahrens zfs_znode_delete(znode_t *zp, dmu_tx_t *tx) 1022fa9e4066Sahrens { 1023fa9e4066Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1024cdb0ab79Smaybee objset_t *os = zfsvfs->z_os; 10254ccbb6e7Sahrens uint64_t obj = zp->z_id; 1026cdb0ab79Smaybee uint64_t acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj; 1027fa9e4066Sahrens 10284ccbb6e7Sahrens ZFS_OBJ_HOLD_ENTER(zfsvfs, obj); 1029cdb0ab79Smaybee if (acl_obj) 1030cdb0ab79Smaybee VERIFY(0 == dmu_object_free(os, acl_obj, tx)); 1031cdb0ab79Smaybee VERIFY(0 == dmu_object_free(os, obj, tx)); 10324ccbb6e7Sahrens zfs_znode_dmu_fini(zp); 10334ccbb6e7Sahrens ZFS_OBJ_HOLD_EXIT(zfsvfs, obj); 1034874395d5Smaybee zfs_znode_free(zp); 1035fa9e4066Sahrens } 1036fa9e4066Sahrens 1037fa9e4066Sahrens void 1038fa9e4066Sahrens zfs_zinactive(znode_t *zp) 1039fa9e4066Sahrens { 1040fa9e4066Sahrens vnode_t *vp = ZTOV(zp); 1041fa9e4066Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1042fa9e4066Sahrens uint64_t z_id = zp->z_id; 1043fa9e4066Sahrens 10444ccbb6e7Sahrens ASSERT(zp->z_dbuf && zp->z_phys); 1045fa9e4066Sahrens 1046fa9e4066Sahrens /* 1047fa9e4066Sahrens * Don't allow a zfs_zget() while were trying to release this znode 1048fa9e4066Sahrens */ 1049fa9e4066Sahrens ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id); 1050fa9e4066Sahrens 1051fa9e4066Sahrens mutex_enter(&zp->z_lock); 1052fa9e4066Sahrens mutex_enter(&vp->v_lock); 1053fa9e4066Sahrens vp->v_count--; 1054fa9e4066Sahrens if (vp->v_count > 0 || vn_has_cached_data(vp)) { 1055fa9e4066Sahrens /* 1056fa9e4066Sahrens * If the hold count is greater than zero, somebody has 1057fa9e4066Sahrens * obtained a new reference on this znode while we were 1058fa9e4066Sahrens * processing it here, so we are done. If we still have 1059fa9e4066Sahrens * mapped pages then we are also done, since we don't 1060fa9e4066Sahrens * want to inactivate the znode until the pages get pushed. 1061fa9e4066Sahrens * 1062fa9e4066Sahrens * XXX - if vn_has_cached_data(vp) is true, but count == 0, 1063fa9e4066Sahrens * this seems like it would leave the znode hanging with 1064fa9e4066Sahrens * no chance to go inactive... 1065fa9e4066Sahrens */ 1066fa9e4066Sahrens mutex_exit(&vp->v_lock); 1067fa9e4066Sahrens mutex_exit(&zp->z_lock); 1068fa9e4066Sahrens ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 1069fa9e4066Sahrens return; 1070fa9e4066Sahrens } 1071fa9e4066Sahrens mutex_exit(&vp->v_lock); 1072fa9e4066Sahrens 1073fa9e4066Sahrens /* 1074fa9e4066Sahrens * If this was the last reference to a file with no links, 1075fa9e4066Sahrens * remove the file from the file system. 1076fa9e4066Sahrens */ 1077893a6d32Sahrens if (zp->z_unlinked) { 1078fa9e4066Sahrens mutex_exit(&zp->z_lock); 1079fa9e4066Sahrens ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 1080893a6d32Sahrens zfs_rmnode(zp); 1081fa9e4066Sahrens return; 1082fa9e4066Sahrens } 1083fa9e4066Sahrens mutex_exit(&zp->z_lock); 10844ccbb6e7Sahrens zfs_znode_dmu_fini(zp); 1085fa9e4066Sahrens ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 1086874395d5Smaybee zfs_znode_free(zp); 1087fa9e4066Sahrens } 1088fa9e4066Sahrens 1089fa9e4066Sahrens void 1090fa9e4066Sahrens zfs_znode_free(znode_t *zp) 1091fa9e4066Sahrens { 1092fa9e4066Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1093fa9e4066Sahrens 1094874395d5Smaybee vn_invalid(ZTOV(zp)); 1095874395d5Smaybee 1096b5fca8f8Stomee ASSERT(ZTOV(zp)->v_count == 0); 1097b5fca8f8Stomee 1098fa9e4066Sahrens mutex_enter(&zfsvfs->z_znodes_lock); 1099b5fca8f8Stomee POINTER_INVALIDATE(&zp->z_zfsvfs); 1100fa9e4066Sahrens list_remove(&zfsvfs->z_all_znodes, zp); 1101fa9e4066Sahrens mutex_exit(&zfsvfs->z_znodes_lock); 1102fa9e4066Sahrens 1103d47621a4STim Haley if (zp->z_acl_cached) { 1104d47621a4STim Haley zfs_acl_free(zp->z_acl_cached); 1105d47621a4STim Haley zp->z_acl_cached = NULL; 1106d47621a4STim Haley } 1107d47621a4STim Haley 1108fa9e4066Sahrens kmem_cache_free(znode_cache, zp); 1109874395d5Smaybee 1110874395d5Smaybee VFS_RELE(zfsvfs->z_vfs); 1111fa9e4066Sahrens } 1112fa9e4066Sahrens 1113fa9e4066Sahrens void 1114fa9e4066Sahrens zfs_time_stamper_locked(znode_t *zp, uint_t flag, dmu_tx_t *tx) 1115fa9e4066Sahrens { 1116fa9e4066Sahrens timestruc_t now; 1117fa9e4066Sahrens 1118fa9e4066Sahrens ASSERT(MUTEX_HELD(&zp->z_lock)); 1119fa9e4066Sahrens 1120fa9e4066Sahrens gethrestime(&now); 1121fa9e4066Sahrens 1122fa9e4066Sahrens if (tx) { 1123fa9e4066Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 1124fa9e4066Sahrens zp->z_atime_dirty = 0; 1125fa9e4066Sahrens zp->z_seq++; 1126fa9e4066Sahrens } else { 1127fa9e4066Sahrens zp->z_atime_dirty = 1; 1128fa9e4066Sahrens } 1129fa9e4066Sahrens 1130fa9e4066Sahrens if (flag & AT_ATIME) 1131fa9e4066Sahrens ZFS_TIME_ENCODE(&now, zp->z_phys->zp_atime); 1132fa9e4066Sahrens 1133da6c28aaSamw if (flag & AT_MTIME) { 1134fa9e4066Sahrens ZFS_TIME_ENCODE(&now, zp->z_phys->zp_mtime); 1135da6c28aaSamw if (zp->z_zfsvfs->z_use_fuids) 1136da6c28aaSamw zp->z_phys->zp_flags |= (ZFS_ARCHIVE | ZFS_AV_MODIFIED); 1137da6c28aaSamw } 1138fa9e4066Sahrens 1139da6c28aaSamw if (flag & AT_CTIME) { 1140fa9e4066Sahrens ZFS_TIME_ENCODE(&now, zp->z_phys->zp_ctime); 1141da6c28aaSamw if (zp->z_zfsvfs->z_use_fuids) 1142da6c28aaSamw zp->z_phys->zp_flags |= ZFS_ARCHIVE; 1143da6c28aaSamw } 1144fa9e4066Sahrens } 1145fa9e4066Sahrens 1146fa9e4066Sahrens /* 1147fa9e4066Sahrens * Update the requested znode timestamps with the current time. 1148fa9e4066Sahrens * If we are in a transaction, then go ahead and mark the znode 1149fa9e4066Sahrens * dirty in the transaction so the timestamps will go to disk. 1150fa9e4066Sahrens * Otherwise, we will get pushed next time the znode is updated 1151fa9e4066Sahrens * in a transaction, or when this znode eventually goes inactive. 1152fa9e4066Sahrens * 1153fa9e4066Sahrens * Why is this OK? 1154fa9e4066Sahrens * 1 - Only the ACCESS time is ever updated outside of a transaction. 1155fa9e4066Sahrens * 2 - Multiple consecutive updates will be collapsed into a single 1156fa9e4066Sahrens * znode update by the transaction grouping semantics of the DMU. 1157fa9e4066Sahrens */ 1158fa9e4066Sahrens void 1159fa9e4066Sahrens zfs_time_stamper(znode_t *zp, uint_t flag, dmu_tx_t *tx) 1160fa9e4066Sahrens { 1161fa9e4066Sahrens mutex_enter(&zp->z_lock); 1162fa9e4066Sahrens zfs_time_stamper_locked(zp, flag, tx); 1163fa9e4066Sahrens mutex_exit(&zp->z_lock); 1164fa9e4066Sahrens } 1165fa9e4066Sahrens 1166fa9e4066Sahrens /* 1167104e2ed7Sperrin * Grow the block size for a file. 1168fa9e4066Sahrens * 1169fa9e4066Sahrens * IN: zp - znode of file to free data in. 1170fa9e4066Sahrens * size - requested block size 1171fa9e4066Sahrens * tx - open transaction. 1172fa9e4066Sahrens * 1173fa9e4066Sahrens * NOTE: this function assumes that the znode is write locked. 1174fa9e4066Sahrens */ 1175104e2ed7Sperrin void 1176fa9e4066Sahrens zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx) 1177fa9e4066Sahrens { 1178fa9e4066Sahrens int error; 1179fa9e4066Sahrens u_longlong_t dummy; 1180fa9e4066Sahrens 1181fa9e4066Sahrens if (size <= zp->z_blksz) 1182104e2ed7Sperrin return; 1183fa9e4066Sahrens /* 1184fa9e4066Sahrens * If the file size is already greater than the current blocksize, 1185fa9e4066Sahrens * we will not grow. If there is more than one block in a file, 1186fa9e4066Sahrens * the blocksize cannot change. 1187fa9e4066Sahrens */ 1188fa9e4066Sahrens if (zp->z_blksz && zp->z_phys->zp_size > zp->z_blksz) 1189104e2ed7Sperrin return; 1190fa9e4066Sahrens 1191fa9e4066Sahrens error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id, 1192fa9e4066Sahrens size, 0, tx); 1193fa9e4066Sahrens if (error == ENOTSUP) 1194104e2ed7Sperrin return; 1195fa9e4066Sahrens ASSERT3U(error, ==, 0); 1196fa9e4066Sahrens 1197fa9e4066Sahrens /* What blocksize did we actually get? */ 1198fa9e4066Sahrens dmu_object_size_from_db(zp->z_dbuf, &zp->z_blksz, &dummy); 1199fa9e4066Sahrens } 1200fa9e4066Sahrens 1201fa9e4066Sahrens /* 1202fa9e4066Sahrens * This is a dummy interface used when pvn_vplist_dirty() should *not* 1203fa9e4066Sahrens * be calling back into the fs for a putpage(). E.g.: when truncating 1204fa9e4066Sahrens * a file, the pages being "thrown away* don't need to be written out. 1205fa9e4066Sahrens */ 1206fa9e4066Sahrens /* ARGSUSED */ 1207fa9e4066Sahrens static int 1208fa9e4066Sahrens zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp, 1209fa9e4066Sahrens int flags, cred_t *cr) 1210fa9e4066Sahrens { 1211fa9e4066Sahrens ASSERT(0); 1212fa9e4066Sahrens return (0); 1213fa9e4066Sahrens } 1214fa9e4066Sahrens 1215fa9e4066Sahrens /* 1216cdb0ab79Smaybee * Increase the file length 1217fa9e4066Sahrens * 1218fa9e4066Sahrens * IN: zp - znode of file to free data in. 1219cdb0ab79Smaybee * end - new end-of-file 1220fa9e4066Sahrens * 1221fa9e4066Sahrens * RETURN: 0 if success 1222fa9e4066Sahrens * error code if failure 1223fa9e4066Sahrens */ 1224cdb0ab79Smaybee static int 1225cdb0ab79Smaybee zfs_extend(znode_t *zp, uint64_t end) 1226fa9e4066Sahrens { 12275730cc9aSmaybee zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1228cdb0ab79Smaybee dmu_tx_t *tx; 12295730cc9aSmaybee rl_t *rl; 1230cdb0ab79Smaybee uint64_t newblksz; 1231104e2ed7Sperrin int error; 1232fa9e4066Sahrens 12335730cc9aSmaybee /* 1234cdb0ab79Smaybee * We will change zp_size, lock the whole file. 12355730cc9aSmaybee */ 1236cdb0ab79Smaybee rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER); 12375730cc9aSmaybee 1238fa9e4066Sahrens /* 1239fa9e4066Sahrens * Nothing to do if file already at desired length. 1240fa9e4066Sahrens */ 1241cdb0ab79Smaybee if (end <= zp->z_phys->zp_size) { 1242c5c6ffa0Smaybee zfs_range_unlock(rl); 1243fa9e4066Sahrens return (0); 1244fa9e4066Sahrens } 1245cdb0ab79Smaybee top: 12465730cc9aSmaybee tx = dmu_tx_create(zfsvfs->z_os); 12475730cc9aSmaybee dmu_tx_hold_bonus(tx, zp->z_id); 1248cdb0ab79Smaybee if (end > zp->z_blksz && 12495730cc9aSmaybee (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) { 1250fa9e4066Sahrens /* 1251fa9e4066Sahrens * We are growing the file past the current block size. 1252fa9e4066Sahrens */ 1253fa9e4066Sahrens if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) { 1254fa9e4066Sahrens ASSERT(!ISP2(zp->z_blksz)); 1255cdb0ab79Smaybee newblksz = MIN(end, SPA_MAXBLOCKSIZE); 1256fa9e4066Sahrens } else { 1257cdb0ab79Smaybee newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz); 1258fa9e4066Sahrens } 1259cdb0ab79Smaybee dmu_tx_hold_write(tx, zp->z_id, 0, newblksz); 1260cdb0ab79Smaybee } else { 1261cdb0ab79Smaybee newblksz = 0; 12625730cc9aSmaybee } 12635730cc9aSmaybee 12641209a471SNeil Perrin error = dmu_tx_assign(tx, TXG_NOWAIT); 12655730cc9aSmaybee if (error) { 12661209a471SNeil Perrin if (error == ERESTART) { 12678a2f1b91Sahrens dmu_tx_wait(tx); 1268cdb0ab79Smaybee dmu_tx_abort(tx); 1269cdb0ab79Smaybee goto top; 1270cdb0ab79Smaybee } 12715730cc9aSmaybee dmu_tx_abort(tx); 1272c5c6ffa0Smaybee zfs_range_unlock(rl); 12735730cc9aSmaybee return (error); 1274fa9e4066Sahrens } 1275cdb0ab79Smaybee dmu_buf_will_dirty(zp->z_dbuf, tx); 12765730cc9aSmaybee 1277cdb0ab79Smaybee if (newblksz) 1278cdb0ab79Smaybee zfs_grow_blocksize(zp, newblksz, tx); 12795730cc9aSmaybee 1280cdb0ab79Smaybee zp->z_phys->zp_size = end; 12815730cc9aSmaybee 1282cdb0ab79Smaybee zfs_range_unlock(rl); 12835730cc9aSmaybee 1284cdb0ab79Smaybee dmu_tx_commit(tx); 12855730cc9aSmaybee 1286cdb0ab79Smaybee return (0); 1287cdb0ab79Smaybee } 1288cdb0ab79Smaybee 1289cdb0ab79Smaybee /* 1290cdb0ab79Smaybee * Free space in a file. 1291cdb0ab79Smaybee * 1292cdb0ab79Smaybee * IN: zp - znode of file to free data in. 1293cdb0ab79Smaybee * off - start of section to free. 1294cdb0ab79Smaybee * len - length of section to free. 1295cdb0ab79Smaybee * 1296cdb0ab79Smaybee * RETURN: 0 if success 1297cdb0ab79Smaybee * error code if failure 1298cdb0ab79Smaybee */ 1299cdb0ab79Smaybee static int 1300cdb0ab79Smaybee zfs_free_range(znode_t *zp, uint64_t off, uint64_t len) 1301cdb0ab79Smaybee { 1302cdb0ab79Smaybee zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1303cdb0ab79Smaybee rl_t *rl; 1304cdb0ab79Smaybee int error; 1305cdb0ab79Smaybee 1306cdb0ab79Smaybee /* 1307cdb0ab79Smaybee * Lock the range being freed. 1308cdb0ab79Smaybee */ 1309cdb0ab79Smaybee rl = zfs_range_lock(zp, off, len, RL_WRITER); 1310cdb0ab79Smaybee 1311cdb0ab79Smaybee /* 1312cdb0ab79Smaybee * Nothing to do if file already at desired length. 1313cdb0ab79Smaybee */ 1314cdb0ab79Smaybee if (off >= zp->z_phys->zp_size) { 1315cdb0ab79Smaybee zfs_range_unlock(rl); 1316cdb0ab79Smaybee return (0); 13175730cc9aSmaybee } 13185730cc9aSmaybee 1319cdb0ab79Smaybee if (off + len > zp->z_phys->zp_size) 1320cdb0ab79Smaybee len = zp->z_phys->zp_size - off; 1321cdb0ab79Smaybee 1322cdb0ab79Smaybee error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len); 1323cdb0ab79Smaybee 1324c5c6ffa0Smaybee zfs_range_unlock(rl); 13255730cc9aSmaybee 1326cdb0ab79Smaybee return (error); 1327cdb0ab79Smaybee } 1328cdb0ab79Smaybee 1329cdb0ab79Smaybee /* 1330cdb0ab79Smaybee * Truncate a file 1331cdb0ab79Smaybee * 1332cdb0ab79Smaybee * IN: zp - znode of file to free data in. 1333cdb0ab79Smaybee * end - new end-of-file. 1334cdb0ab79Smaybee * 1335cdb0ab79Smaybee * RETURN: 0 if success 1336cdb0ab79Smaybee * error code if failure 1337cdb0ab79Smaybee */ 1338cdb0ab79Smaybee static int 1339cdb0ab79Smaybee zfs_trunc(znode_t *zp, uint64_t end) 1340cdb0ab79Smaybee { 1341cdb0ab79Smaybee zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1342cdb0ab79Smaybee vnode_t *vp = ZTOV(zp); 1343cdb0ab79Smaybee dmu_tx_t *tx; 1344cdb0ab79Smaybee rl_t *rl; 1345cdb0ab79Smaybee int error; 1346cdb0ab79Smaybee 1347cdb0ab79Smaybee /* 1348cdb0ab79Smaybee * We will change zp_size, lock the whole file. 1349cdb0ab79Smaybee */ 1350cdb0ab79Smaybee rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER); 1351cdb0ab79Smaybee 1352cdb0ab79Smaybee /* 1353cdb0ab79Smaybee * Nothing to do if file already at desired length. 1354cdb0ab79Smaybee */ 1355cdb0ab79Smaybee if (end >= zp->z_phys->zp_size) { 1356cdb0ab79Smaybee zfs_range_unlock(rl); 1357cdb0ab79Smaybee return (0); 1358cdb0ab79Smaybee } 1359cdb0ab79Smaybee 1360cdb0ab79Smaybee error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end, -1); 1361cdb0ab79Smaybee if (error) { 1362cdb0ab79Smaybee zfs_range_unlock(rl); 1363cdb0ab79Smaybee return (error); 1364cdb0ab79Smaybee } 1365cdb0ab79Smaybee top: 1366cdb0ab79Smaybee tx = dmu_tx_create(zfsvfs->z_os); 1367cdb0ab79Smaybee dmu_tx_hold_bonus(tx, zp->z_id); 13681209a471SNeil Perrin error = dmu_tx_assign(tx, TXG_NOWAIT); 1369cdb0ab79Smaybee if (error) { 13701209a471SNeil Perrin if (error == ERESTART) { 1371cdb0ab79Smaybee dmu_tx_wait(tx); 1372cdb0ab79Smaybee dmu_tx_abort(tx); 1373cdb0ab79Smaybee goto top; 1374cdb0ab79Smaybee } 1375cdb0ab79Smaybee dmu_tx_abort(tx); 1376cdb0ab79Smaybee zfs_range_unlock(rl); 1377cdb0ab79Smaybee return (error); 1378cdb0ab79Smaybee } 1379cdb0ab79Smaybee dmu_buf_will_dirty(zp->z_dbuf, tx); 1380cdb0ab79Smaybee 1381cdb0ab79Smaybee zp->z_phys->zp_size = end; 1382cdb0ab79Smaybee 13835730cc9aSmaybee dmu_tx_commit(tx); 13845730cc9aSmaybee 1385fa9e4066Sahrens /* 13865730cc9aSmaybee * Clear any mapped pages in the truncated region. This has to 13875730cc9aSmaybee * happen outside of the transaction to avoid the possibility of 13885730cc9aSmaybee * a deadlock with someone trying to push a page that we are 13895730cc9aSmaybee * about to invalidate. 1390fa9e4066Sahrens */ 1391cdb0ab79Smaybee if (vn_has_cached_data(vp)) { 1392fa9e4066Sahrens page_t *pp; 1393cdb0ab79Smaybee uint64_t start = end & PAGEMASK; 1394cdb0ab79Smaybee int poff = end & PAGEOFFSET; 1395fa9e4066Sahrens 13965730cc9aSmaybee if (poff != 0 && (pp = page_lookup(vp, start, SE_SHARED))) { 1397fa9e4066Sahrens /* 1398fa9e4066Sahrens * We need to zero a partial page. 1399fa9e4066Sahrens */ 14005730cc9aSmaybee pagezero(pp, poff, PAGESIZE - poff); 1401fa9e4066Sahrens start += PAGESIZE; 1402fa9e4066Sahrens page_unlock(pp); 1403fa9e4066Sahrens } 1404fa9e4066Sahrens error = pvn_vplist_dirty(vp, start, zfs_no_putpage, 14055730cc9aSmaybee B_INVAL | B_TRUNC, NULL); 1406fa9e4066Sahrens ASSERT(error == 0); 1407fa9e4066Sahrens } 1408ac05c741SMark Maybee 1409ac05c741SMark Maybee zfs_range_unlock(rl); 1410fa9e4066Sahrens 1411fa9e4066Sahrens return (0); 1412fa9e4066Sahrens } 1413fa9e4066Sahrens 1414cdb0ab79Smaybee /* 1415cdb0ab79Smaybee * Free space in a file 1416cdb0ab79Smaybee * 1417cdb0ab79Smaybee * IN: zp - znode of file to free data in. 1418cdb0ab79Smaybee * off - start of range 1419cdb0ab79Smaybee * len - end of range (0 => EOF) 1420cdb0ab79Smaybee * flag - current file open mode flags. 1421cdb0ab79Smaybee * log - TRUE if this action should be logged 1422cdb0ab79Smaybee * 1423cdb0ab79Smaybee * RETURN: 0 if success 1424cdb0ab79Smaybee * error code if failure 1425cdb0ab79Smaybee */ 1426cdb0ab79Smaybee int 1427cdb0ab79Smaybee zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log) 1428cdb0ab79Smaybee { 1429cdb0ab79Smaybee vnode_t *vp = ZTOV(zp); 1430cdb0ab79Smaybee dmu_tx_t *tx; 1431cdb0ab79Smaybee zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1432cdb0ab79Smaybee zilog_t *zilog = zfsvfs->z_log; 1433cdb0ab79Smaybee int error; 1434cdb0ab79Smaybee 1435cdb0ab79Smaybee if (off > zp->z_phys->zp_size) { 1436cdb0ab79Smaybee error = zfs_extend(zp, off+len); 1437cdb0ab79Smaybee if (error == 0 && log) 1438cdb0ab79Smaybee goto log; 1439cdb0ab79Smaybee else 1440cdb0ab79Smaybee return (error); 1441cdb0ab79Smaybee } 1442cdb0ab79Smaybee 1443cdb0ab79Smaybee /* 1444cdb0ab79Smaybee * Check for any locks in the region to be freed. 1445cdb0ab79Smaybee */ 1446cdb0ab79Smaybee if (MANDLOCK(vp, (mode_t)zp->z_phys->zp_mode)) { 1447cdb0ab79Smaybee uint64_t length = (len ? len : zp->z_phys->zp_size - off); 1448cdb0ab79Smaybee if (error = chklock(vp, FWRITE, off, length, flag, NULL)) 1449cdb0ab79Smaybee return (error); 1450cdb0ab79Smaybee } 1451cdb0ab79Smaybee 1452cdb0ab79Smaybee if (len == 0) { 1453cdb0ab79Smaybee error = zfs_trunc(zp, off); 1454cdb0ab79Smaybee } else { 1455cdb0ab79Smaybee if ((error = zfs_free_range(zp, off, len)) == 0 && 1456cdb0ab79Smaybee off + len > zp->z_phys->zp_size) 1457cdb0ab79Smaybee error = zfs_extend(zp, off+len); 1458cdb0ab79Smaybee } 1459cdb0ab79Smaybee if (error || !log) 1460cdb0ab79Smaybee return (error); 1461cdb0ab79Smaybee log: 1462cdb0ab79Smaybee tx = dmu_tx_create(zfsvfs->z_os); 1463cdb0ab79Smaybee dmu_tx_hold_bonus(tx, zp->z_id); 14641209a471SNeil Perrin error = dmu_tx_assign(tx, TXG_NOWAIT); 1465cdb0ab79Smaybee if (error) { 14661209a471SNeil Perrin if (error == ERESTART) { 1467cdb0ab79Smaybee dmu_tx_wait(tx); 1468cdb0ab79Smaybee dmu_tx_abort(tx); 1469cdb0ab79Smaybee goto log; 1470cdb0ab79Smaybee } 1471cdb0ab79Smaybee dmu_tx_abort(tx); 1472cdb0ab79Smaybee return (error); 1473cdb0ab79Smaybee } 1474cdb0ab79Smaybee 1475cdb0ab79Smaybee zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 1476cdb0ab79Smaybee zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len); 1477cdb0ab79Smaybee 1478cdb0ab79Smaybee dmu_tx_commit(tx); 1479cdb0ab79Smaybee return (0); 1480cdb0ab79Smaybee } 1481cdb0ab79Smaybee 1482fa9e4066Sahrens void 1483de8267e0Stimh zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx) 1484fa9e4066Sahrens { 1485fa9e4066Sahrens zfsvfs_t zfsvfs; 148614843421SMatthew Ahrens uint64_t moid, obj, version; 1487de8267e0Stimh uint64_t sense = ZFS_CASE_SENSITIVE; 1488de8267e0Stimh uint64_t norm = 0; 1489de8267e0Stimh nvpair_t *elem; 1490fa9e4066Sahrens int error; 1491fa9e4066Sahrens znode_t *rootzp = NULL; 1492fa9e4066Sahrens vnode_t *vp; 1493fa9e4066Sahrens vattr_t vattr; 14944ccbb6e7Sahrens znode_t *zp; 149589459e17SMark Shellenbaum zfs_acl_ids_t acl_ids; 1496fa9e4066Sahrens 1497fa9e4066Sahrens /* 1498fa9e4066Sahrens * First attempt to create master node. 1499fa9e4066Sahrens */ 1500ea8dc4b6Seschrock /* 1501ea8dc4b6Seschrock * In an empty objset, there are no blocks to read and thus 1502ea8dc4b6Seschrock * there can be no i/o errors (which we assert below). 1503ea8dc4b6Seschrock */ 1504fa9e4066Sahrens moid = MASTER_NODE_OBJ; 1505fa9e4066Sahrens error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE, 1506fa9e4066Sahrens DMU_OT_NONE, 0, tx); 1507fa9e4066Sahrens ASSERT(error == 0); 1508fa9e4066Sahrens 1509fa9e4066Sahrens /* 1510fa9e4066Sahrens * Set starting attributes. 1511fa9e4066Sahrens */ 151214843421SMatthew Ahrens if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_USERSPACE) 1513088f3894Sahrens version = ZPL_VERSION; 151414843421SMatthew Ahrens else if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_FUID) 151514843421SMatthew Ahrens version = ZPL_VERSION_USERSPACE - 1; 1516088f3894Sahrens else 1517088f3894Sahrens version = ZPL_VERSION_FUID - 1; 1518de8267e0Stimh elem = NULL; 1519de8267e0Stimh while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) { 1520de8267e0Stimh /* For the moment we expect all zpl props to be uint64_ts */ 1521de8267e0Stimh uint64_t val; 1522de8267e0Stimh char *name; 1523de8267e0Stimh 1524de8267e0Stimh ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64); 1525b3d911cbStimh VERIFY(nvpair_value_uint64(elem, &val) == 0); 1526de8267e0Stimh name = nvpair_name(elem); 1527de8267e0Stimh if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) { 152814843421SMatthew Ahrens if (val < version) 152914843421SMatthew Ahrens version = val; 1530de8267e0Stimh } else { 1531de8267e0Stimh error = zap_update(os, moid, name, 8, 1, &val, tx); 1532de8267e0Stimh } 1533de8267e0Stimh ASSERT(error == 0); 1534de8267e0Stimh if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0) 1535de8267e0Stimh norm = val; 1536de8267e0Stimh else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0) 1537de8267e0Stimh sense = val; 1538de8267e0Stimh } 1539de8267e0Stimh ASSERT(version != 0); 154014843421SMatthew Ahrens error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx); 1541fa9e4066Sahrens 1542fa9e4066Sahrens /* 1543fa9e4066Sahrens * Create a delete queue. 1544fa9e4066Sahrens */ 154514843421SMatthew Ahrens obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx); 1546fa9e4066Sahrens 154714843421SMatthew Ahrens error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx); 1548fa9e4066Sahrens ASSERT(error == 0); 1549fa9e4066Sahrens 1550fa9e4066Sahrens /* 1551fa9e4066Sahrens * Create root znode. Create minimal znode/vnode/zfsvfs 1552fa9e4066Sahrens * to allow zfs_mknode to work. 1553fa9e4066Sahrens */ 1554fa9e4066Sahrens vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 1555fa9e4066Sahrens vattr.va_type = VDIR; 1556fa9e4066Sahrens vattr.va_mode = S_IFDIR|0755; 1557ecd6cf80Smarks vattr.va_uid = crgetuid(cr); 1558ecd6cf80Smarks vattr.va_gid = crgetgid(cr); 1559fa9e4066Sahrens 1560fa9e4066Sahrens rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP); 1561893a6d32Sahrens rootzp->z_unlinked = 0; 1562fa9e4066Sahrens rootzp->z_atime_dirty = 0; 1563fa9e4066Sahrens 1564fa9e4066Sahrens vp = ZTOV(rootzp); 1565fa9e4066Sahrens vn_reinit(vp); 1566fa9e4066Sahrens vp->v_type = VDIR; 1567fa9e4066Sahrens 1568fa9e4066Sahrens bzero(&zfsvfs, sizeof (zfsvfs_t)); 1569fa9e4066Sahrens 1570fa9e4066Sahrens zfsvfs.z_os = os; 1571fa9e4066Sahrens zfsvfs.z_parent = &zfsvfs; 1572da6c28aaSamw zfsvfs.z_version = version; 1573da6c28aaSamw zfsvfs.z_use_fuids = USE_FUIDS(version, os); 1574da6c28aaSamw zfsvfs.z_norm = norm; 1575de8267e0Stimh /* 1576de8267e0Stimh * Fold case on file systems that are always or sometimes case 1577de8267e0Stimh * insensitive. 1578de8267e0Stimh */ 1579de8267e0Stimh if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED) 1580de8267e0Stimh zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER; 1581fa9e4066Sahrens 1582fa9e4066Sahrens mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL); 1583fa9e4066Sahrens list_create(&zfsvfs.z_all_znodes, sizeof (znode_t), 1584fa9e4066Sahrens offsetof(znode_t, z_link_node)); 1585fa9e4066Sahrens 1586b5fca8f8Stomee ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs)); 1587b5fca8f8Stomee rootzp->z_zfsvfs = &zfsvfs; 158889459e17SMark Shellenbaum VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr, 158989459e17SMark Shellenbaum cr, NULL, &acl_ids)); 159089459e17SMark Shellenbaum zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, 0, &acl_ids); 1591874395d5Smaybee ASSERT3P(zp, ==, rootzp); 1592b5fca8f8Stomee ASSERT(!vn_in_dnlc(ZTOV(rootzp))); /* not valid to move */ 15934ccbb6e7Sahrens error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx); 1594fa9e4066Sahrens ASSERT(error == 0); 159589459e17SMark Shellenbaum zfs_acl_ids_free(&acl_ids); 1596b5fca8f8Stomee POINTER_INVALIDATE(&rootzp->z_zfsvfs); 1597fa9e4066Sahrens 1598fa9e4066Sahrens ZTOV(rootzp)->v_count = 0; 1599874395d5Smaybee dmu_buf_rele(rootzp->z_dbuf, NULL); 1600874395d5Smaybee rootzp->z_dbuf = NULL; 1601fa9e4066Sahrens kmem_cache_free(znode_cache, rootzp); 1602743a77edSAlan Wright 1603743a77edSAlan Wright /* 1604743a77edSAlan Wright * Create shares directory 1605743a77edSAlan Wright */ 1606743a77edSAlan Wright 1607743a77edSAlan Wright error = zfs_create_share_dir(&zfsvfs, tx); 160889459e17SMark Shellenbaum 1609743a77edSAlan Wright ASSERT(error == 0); 1610fa9e4066Sahrens } 161155434c77Sek 1612da6c28aaSamw #endif /* _KERNEL */ 161355434c77Sek /* 161455434c77Sek * Given an object number, return its parent object number and whether 161555434c77Sek * or not the object is an extended attribute directory. 161655434c77Sek */ 161755434c77Sek static int 161855434c77Sek zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir) 161955434c77Sek { 162055434c77Sek dmu_buf_t *db; 162155434c77Sek dmu_object_info_t doi; 162255434c77Sek znode_phys_t *zp; 162355434c77Sek int error; 162455434c77Sek 162555434c77Sek if ((error = dmu_bonus_hold(osp, obj, FTAG, &db)) != 0) 162655434c77Sek return (error); 162755434c77Sek 162855434c77Sek dmu_object_info_from_db(db, &doi); 162955434c77Sek if (doi.doi_bonus_type != DMU_OT_ZNODE || 163055434c77Sek doi.doi_bonus_size < sizeof (znode_phys_t)) { 163155434c77Sek dmu_buf_rele(db, FTAG); 163255434c77Sek return (EINVAL); 163355434c77Sek } 163455434c77Sek 163555434c77Sek zp = db->db_data; 163655434c77Sek *pobjp = zp->zp_parent; 163755434c77Sek *is_xattrdir = ((zp->zp_flags & ZFS_XATTR) != 0) && 163855434c77Sek S_ISDIR(zp->zp_mode); 163955434c77Sek dmu_buf_rele(db, FTAG); 164055434c77Sek 164155434c77Sek return (0); 164255434c77Sek } 164355434c77Sek 164455434c77Sek int 164555434c77Sek zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len) 164655434c77Sek { 164755434c77Sek char *path = buf + len - 1; 164855434c77Sek int error; 164955434c77Sek 165055434c77Sek *path = '\0'; 165155434c77Sek 165255434c77Sek for (;;) { 165355434c77Sek uint64_t pobj; 165455434c77Sek char component[MAXNAMELEN + 2]; 165555434c77Sek size_t complen; 165655434c77Sek int is_xattrdir; 165755434c77Sek 165855434c77Sek if ((error = zfs_obj_to_pobj(osp, obj, &pobj, 165955434c77Sek &is_xattrdir)) != 0) 166055434c77Sek break; 166155434c77Sek 166255434c77Sek if (pobj == obj) { 166355434c77Sek if (path[0] != '/') 166455434c77Sek *--path = '/'; 166555434c77Sek break; 166655434c77Sek } 166755434c77Sek 166855434c77Sek component[0] = '/'; 166955434c77Sek if (is_xattrdir) { 167055434c77Sek (void) sprintf(component + 1, "<xattrdir>"); 167155434c77Sek } else { 1672e7437265Sahrens error = zap_value_search(osp, pobj, obj, 1673e7437265Sahrens ZFS_DIRENT_OBJ(-1ULL), component + 1); 167455434c77Sek if (error != 0) 167555434c77Sek break; 167655434c77Sek } 167755434c77Sek 167855434c77Sek complen = strlen(component); 167955434c77Sek path -= complen; 168055434c77Sek ASSERT(path >= buf); 168155434c77Sek bcopy(component, path, complen); 168255434c77Sek obj = pobj; 168355434c77Sek } 168455434c77Sek 168555434c77Sek if (error == 0) 168655434c77Sek (void) memmove(buf, path, buf + len - path); 168755434c77Sek return (error); 168855434c77Sek } 1689