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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 by Delphix. All rights reserved.
26 /* Portions Copyright 2007 Jeremy Teo */
27 /* Portions Copyright 2011 Martin Matuska <mm@FreeBSD.org> */
30 #include <sys/types.h>
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/sysmacros.h>
35 #include <sys/resource.h>
36 #include <sys/mntent.h>
37 #include <sys/u8_textprep.h>
38 #include <sys/dsl_dataset.h>
40 #include <sys/vnode.h>
43 #include <sys/errno.h>
44 #include <sys/unistd.h>
45 #include <sys/atomic.h>
46 #include <sys/zfs_dir.h>
47 #include <sys/zfs_acl.h>
48 #include <sys/zfs_ioctl.h>
49 #include <sys/zfs_rlock.h>
50 #include <sys/zfs_fuid.h>
51 #include <sys/dnode.h>
52 #include <sys/fs/zfs.h>
53 #include <sys/kidmap.h>
57 #include <sys/refcount.h>
60 #include <sys/zfs_znode.h>
62 #include <sys/zfs_sa.h>
63 #include <sys/zfs_stat.h>
64 #include <sys/refcount.h>
67 #include "zfs_comutil.h"
69 /* Used by fstat(1). */
70 SYSCTL_INT(_debug_sizeof, OID_AUTO, znode, CTLFLAG_RD, 0, sizeof(znode_t),
74 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
75 * turned on when DEBUG is also defined.
82 #define ZNODE_STAT_ADD(stat) ((stat)++)
84 #define ZNODE_STAT_ADD(stat) /* nothing */
85 #endif /* ZNODE_STATS */
88 * Functions needed for userland (ie: libzpool) are not put under
89 * #ifdef_KERNEL; the rest of the functions have dependencies
90 * (such as VFS logic) that will not compile easily in userland.
94 * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to
95 * be freed before it can be safely accessed.
97 krwlock_t zfsvfs_lock;
99 static kmem_cache_t *znode_cache = NULL;
103 znode_evict_error(dmu_buf_t *dbuf, void *user_ptr)
106 * We should never drop all dbuf refs without first clearing
107 * the eviction callback.
109 panic("evicting znode %p\n", user_ptr);
112 extern struct vop_vector zfs_vnodeops;
113 extern struct vop_vector zfs_fifoops;
114 extern struct vop_vector zfs_shareops;
117 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
121 POINTER_INVALIDATE(&zp->z_zfsvfs);
123 list_link_init(&zp->z_link_node);
125 mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
126 rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
127 rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
128 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
130 mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
131 avl_create(&zp->z_range_avl, zfs_range_compare,
132 sizeof (rl_t), offsetof(rl_t, r_node));
134 zp->z_dirlocks = NULL;
135 zp->z_acl_cached = NULL;
143 zfs_znode_cache_destructor(void *buf, void *arg)
147 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
148 ASSERT(ZTOV(zp) == NULL);
150 ASSERT(!list_link_active(&zp->z_link_node));
151 mutex_destroy(&zp->z_lock);
152 rw_destroy(&zp->z_parent_lock);
153 rw_destroy(&zp->z_name_lock);
154 mutex_destroy(&zp->z_acl_lock);
155 avl_destroy(&zp->z_range_avl);
156 mutex_destroy(&zp->z_range_lock);
158 ASSERT(zp->z_dirlocks == NULL);
159 ASSERT(zp->z_acl_cached == NULL);
164 uint64_t zms_zfsvfs_invalid;
165 uint64_t zms_zfsvfs_recheck1;
166 uint64_t zms_zfsvfs_unmounted;
167 uint64_t zms_zfsvfs_recheck2;
168 uint64_t zms_obj_held;
169 uint64_t zms_vnode_locked;
170 uint64_t zms_not_only_dnlc;
172 #endif /* ZNODE_STATS */
176 zfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
181 nzp->z_zfsvfs = ozp->z_zfsvfs;
185 nzp->z_vnode = ozp->z_vnode;
186 ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
187 ZTOV(ozp)->v_data = ozp;
188 ZTOV(nzp)->v_data = nzp;
190 nzp->z_id = ozp->z_id;
191 ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
192 ASSERT(avl_numnodes(&ozp->z_range_avl) == 0);
193 nzp->z_unlinked = ozp->z_unlinked;
194 nzp->z_atime_dirty = ozp->z_atime_dirty;
195 nzp->z_zn_prefetch = ozp->z_zn_prefetch;
196 nzp->z_blksz = ozp->z_blksz;
197 nzp->z_seq = ozp->z_seq;
198 nzp->z_mapcnt = ozp->z_mapcnt;
199 nzp->z_gen = ozp->z_gen;
200 nzp->z_sync_cnt = ozp->z_sync_cnt;
201 nzp->z_is_sa = ozp->z_is_sa;
202 nzp->z_sa_hdl = ozp->z_sa_hdl;
203 bcopy(ozp->z_atime, nzp->z_atime, sizeof (uint64_t) * 2);
204 nzp->z_links = ozp->z_links;
205 nzp->z_size = ozp->z_size;
206 nzp->z_pflags = ozp->z_pflags;
207 nzp->z_uid = ozp->z_uid;
208 nzp->z_gid = ozp->z_gid;
209 nzp->z_mode = ozp->z_mode;
212 * Since this is just an idle znode and kmem is already dealing with
213 * memory pressure, release any cached ACL.
215 if (ozp->z_acl_cached) {
216 zfs_acl_free(ozp->z_acl_cached);
217 ozp->z_acl_cached = NULL;
220 sa_set_userp(nzp->z_sa_hdl, nzp);
223 * Invalidate the original znode by clearing fields that provide a
224 * pointer back to the znode. Set the low bit of the vfs pointer to
225 * ensure that zfs_znode_move() recognizes the znode as invalid in any
226 * subsequent callback.
228 ozp->z_sa_hdl = NULL;
229 POINTER_INVALIDATE(&ozp->z_zfsvfs);
235 ozp->z_moved = (uint8_t)-1;
240 zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg)
242 znode_t *ozp = buf, *nzp = newbuf;
247 * The znode is on the file system's list of known znodes if the vfs
248 * pointer is valid. We set the low bit of the vfs pointer when freeing
249 * the znode to invalidate it, and the memory patterns written by kmem
250 * (baddcafe and deadbeef) set at least one of the two low bits. A newly
251 * created znode sets the vfs pointer last of all to indicate that the
252 * znode is known and in a valid state to be moved by this function.
254 zfsvfs = ozp->z_zfsvfs;
255 if (!POINTER_IS_VALID(zfsvfs)) {
256 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid);
257 return (KMEM_CBRC_DONT_KNOW);
261 * Close a small window in which it's possible that the filesystem could
262 * be unmounted and freed, and zfsvfs, though valid in the previous
263 * statement, could point to unrelated memory by the time we try to
264 * prevent the filesystem from being unmounted.
266 rw_enter(&zfsvfs_lock, RW_WRITER);
267 if (zfsvfs != ozp->z_zfsvfs) {
268 rw_exit(&zfsvfs_lock);
269 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1);
270 return (KMEM_CBRC_DONT_KNOW);
274 * If the znode is still valid, then so is the file system. We know that
275 * no valid file system can be freed while we hold zfsvfs_lock, so we
276 * can safely ensure that the filesystem is not and will not be
277 * unmounted. The next statement is equivalent to ZFS_ENTER().
279 rrw_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG);
280 if (zfsvfs->z_unmounted) {
282 rw_exit(&zfsvfs_lock);
283 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
284 return (KMEM_CBRC_DONT_KNOW);
286 rw_exit(&zfsvfs_lock);
288 mutex_enter(&zfsvfs->z_znodes_lock);
290 * Recheck the vfs pointer in case the znode was removed just before
291 * acquiring the lock.
293 if (zfsvfs != ozp->z_zfsvfs) {
294 mutex_exit(&zfsvfs->z_znodes_lock);
296 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2);
297 return (KMEM_CBRC_DONT_KNOW);
301 * At this point we know that as long as we hold z_znodes_lock, the
302 * znode cannot be freed and fields within the znode can be safely
303 * accessed. Now, prevent a race with zfs_zget().
305 if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) {
306 mutex_exit(&zfsvfs->z_znodes_lock);
308 ZNODE_STAT_ADD(znode_move_stats.zms_obj_held);
309 return (KMEM_CBRC_LATER);
313 if (mutex_tryenter(&vp->v_lock) == 0) {
314 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
315 mutex_exit(&zfsvfs->z_znodes_lock);
317 ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
318 return (KMEM_CBRC_LATER);
321 /* Only move znodes that are referenced _only_ by the DNLC. */
322 if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
323 mutex_exit(&vp->v_lock);
324 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
325 mutex_exit(&zfsvfs->z_znodes_lock);
327 ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc);
328 return (KMEM_CBRC_LATER);
332 * The znode is known and in a valid state to move. We're holding the
333 * locks needed to execute the critical section.
335 zfs_znode_move_impl(ozp, nzp);
336 mutex_exit(&vp->v_lock);
337 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
339 list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
340 mutex_exit(&zfsvfs->z_znodes_lock);
343 return (KMEM_CBRC_YES);
353 rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL);
354 ASSERT(znode_cache == NULL);
355 znode_cache = kmem_cache_create("zfs_znode_cache",
356 sizeof (znode_t), 0, zfs_znode_cache_constructor,
357 zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
358 kmem_cache_set_move(znode_cache, zfs_znode_move);
366 * Cleanup vfs & vnode ops
368 zfs_remove_op_tables();
375 kmem_cache_destroy(znode_cache);
377 rw_destroy(&zfsvfs_lock);
381 struct vnodeops *zfs_dvnodeops;
382 struct vnodeops *zfs_fvnodeops;
383 struct vnodeops *zfs_symvnodeops;
384 struct vnodeops *zfs_xdvnodeops;
385 struct vnodeops *zfs_evnodeops;
386 struct vnodeops *zfs_sharevnodeops;
389 zfs_remove_op_tables()
395 (void) vfs_freevfsops_by_type(zfsfstype);
402 vn_freevnodeops(zfs_dvnodeops);
404 vn_freevnodeops(zfs_fvnodeops);
406 vn_freevnodeops(zfs_symvnodeops);
408 vn_freevnodeops(zfs_xdvnodeops);
410 vn_freevnodeops(zfs_evnodeops);
411 if (zfs_sharevnodeops)
412 vn_freevnodeops(zfs_sharevnodeops);
414 zfs_dvnodeops = NULL;
415 zfs_fvnodeops = NULL;
416 zfs_symvnodeops = NULL;
417 zfs_xdvnodeops = NULL;
418 zfs_evnodeops = NULL;
419 zfs_sharevnodeops = NULL;
422 extern const fs_operation_def_t zfs_dvnodeops_template[];
423 extern const fs_operation_def_t zfs_fvnodeops_template[];
424 extern const fs_operation_def_t zfs_xdvnodeops_template[];
425 extern const fs_operation_def_t zfs_symvnodeops_template[];
426 extern const fs_operation_def_t zfs_evnodeops_template[];
427 extern const fs_operation_def_t zfs_sharevnodeops_template[];
430 zfs_create_op_tables()
435 * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
436 * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
437 * In this case we just return as the ops vectors are already set up.
442 error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template,
447 error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template,
452 error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template,
457 error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template,
462 error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template,
467 error = vn_make_ops(MNTTYPE_ZFS, zfs_sharevnodeops_template,
475 zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
477 zfs_acl_ids_t acl_ids;
483 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
484 vattr.va_type = VDIR;
485 vattr.va_mode = S_IFDIR|0555;
486 vattr.va_uid = crgetuid(kcred);
487 vattr.va_gid = crgetgid(kcred);
489 sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP);
490 ASSERT(!POINTER_IS_VALID(sharezp->z_zfsvfs));
491 sharezp->z_moved = 0;
492 sharezp->z_unlinked = 0;
493 sharezp->z_atime_dirty = 0;
494 sharezp->z_zfsvfs = zfsvfs;
495 sharezp->z_is_sa = zfsvfs->z_use_sa;
497 VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
498 kcred, NULL, &acl_ids));
499 zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
500 ASSERT3P(zp, ==, sharezp);
501 POINTER_INVALIDATE(&sharezp->z_zfsvfs);
502 error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
503 ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
504 zfsvfs->z_shares_dir = sharezp->z_id;
506 zfs_acl_ids_free(&acl_ids);
507 sa_handle_destroy(sharezp->z_sa_hdl);
508 kmem_cache_free(znode_cache, sharezp);
514 * define a couple of values we need available
515 * for both 64 and 32 bit environments.
518 #define NBITSMINOR64 32
521 #define MAXMAJ64 0xffffffffUL
524 #define MAXMIN64 0xffffffffUL
528 * Create special expldev for ZFS private use.
529 * Can't use standard expldev since it doesn't do
530 * what we want. The standard expldev() takes a
531 * dev32_t in LP64 and expands it to a long dev_t.
532 * We need an interface that takes a dev32_t in ILP32
533 * and expands it to a long dev_t.
536 zfs_expldev(dev_t dev)
538 return (((uint64_t)major(dev) << NBITSMINOR64) | minor(dev));
541 * Special cmpldev for ZFS private use.
542 * Can't use standard cmpldev since it takes
543 * a long dev_t and compresses it to dev32_t in
544 * LP64. We need to do a compaction of a long dev_t
545 * to a dev32_t in ILP32.
548 zfs_cmpldev(uint64_t dev)
550 return (makedev((dev >> NBITSMINOR64), (dev & MAXMIN64)));
554 zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp,
555 dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
557 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
558 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
560 mutex_enter(&zp->z_lock);
562 ASSERT(zp->z_sa_hdl == NULL);
563 ASSERT(zp->z_acl_cached == NULL);
564 if (sa_hdl == NULL) {
565 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, zp,
566 SA_HDL_SHARED, &zp->z_sa_hdl));
568 zp->z_sa_hdl = sa_hdl;
569 sa_set_userp(sa_hdl, zp);
572 zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
575 * Slap on VROOT if we are the root znode
577 if (zp->z_id == zfsvfs->z_root)
578 ZTOV(zp)->v_flag |= VROOT;
580 mutex_exit(&zp->z_lock);
585 zfs_znode_dmu_fini(znode_t *zp)
587 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
589 RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
591 sa_handle_destroy(zp->z_sa_hdl);
596 zfs_vnode_forget(vnode_t *vp)
599 /* copied from insmntque_stddtr */
601 vp->v_op = &dead_vnodeops;
607 * Construct a new znode/vnode and intialize.
609 * This does not do a call to dmu_set_user() that is
610 * up to the caller to do, in case you don't want to
614 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz,
615 dmu_object_type_t obj_type, sa_handle_t *hdl)
621 sa_bulk_attr_t bulk[9];
625 zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
627 KASSERT(curthread->td_vp_reserv > 0,
628 ("zfs_znode_alloc: getnewvnode without any vnodes reserved"));
629 error = getnewvnode("zfs", zfsvfs->z_parent->z_vfs, &zfs_vnodeops, &vp);
631 kmem_cache_free(znode_cache, zp);
637 ASSERT(zp->z_dirlocks == NULL);
638 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
642 * Defer setting z_zfsvfs until the znode is ready to be a candidate for
643 * the zfs_znode_move() callback.
647 zp->z_atime_dirty = 0;
649 zp->z_id = db->db_object;
651 zp->z_seq = 0x7A4653;
656 zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl);
658 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
659 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8);
660 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
662 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
664 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
666 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
667 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
669 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
671 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
674 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
676 sa_handle_destroy(zp->z_sa_hdl);
677 zfs_vnode_forget(vp);
679 kmem_cache_free(znode_cache, zp);
685 vp->v_type = IFTOVT((mode_t)mode);
687 switch (vp->v_type) {
689 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
696 VERIFY(sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zfsvfs),
697 &rdev, sizeof (rdev)) == 0);
699 vp->v_rdev = zfs_cmpldev(rdev);
708 vp->v_op = &zfs_fifoops;
711 if (parent == zfsvfs->z_shares_dir) {
712 ASSERT(zp->z_uid == 0 && zp->z_gid == 0);
713 vp->v_op = &zfs_shareops;
718 vn_setops(vp, zfs_symvnodeops);
721 vn_setops(vp, zfs_evnodeops);
726 mutex_enter(&zfsvfs->z_znodes_lock);
727 list_insert_tail(&zfsvfs->z_all_znodes, zp);
730 * Everything else must be valid before assigning z_zfsvfs makes the
731 * znode eligible for zfs_znode_move().
733 zp->z_zfsvfs = zfsvfs;
734 mutex_exit(&zfsvfs->z_znodes_lock);
737 * Acquire vnode lock before making it available to the world.
739 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
741 if (vp->v_type != VFIFO)
744 VFS_HOLD(zfsvfs->z_vfs);
748 static uint64_t empty_xattr;
749 static uint64_t pad[4];
750 static zfs_acl_phys_t acl_phys;
752 * Create a new DMU object to hold a zfs znode.
754 * IN: dzp - parent directory for new znode
755 * vap - file attributes for new znode
756 * tx - dmu transaction id for zap operations
757 * cr - credentials of caller
759 * IS_ROOT_NODE - new object will be root
760 * IS_XATTR - new object is an attribute
761 * bonuslen - length of bonus buffer
762 * setaclp - File/Dir initial ACL
763 * fuidp - Tracks fuid allocation.
765 * OUT: zpp - allocated znode
769 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
770 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
772 uint64_t crtime[2], atime[2], mtime[2], ctime[2];
773 uint64_t mode, size, links, parent, pflags;
774 uint64_t dzp_pflags = 0;
776 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
783 dmu_object_type_t obj_type;
784 sa_bulk_attr_t sa_attrs[ZPL_END];
786 zfs_acl_locator_cb_t locate = { 0 };
788 ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
790 if (zfsvfs->z_replay) {
791 obj = vap->va_nodeid;
792 now = vap->va_ctime; /* see zfs_replay_create() */
793 gen = vap->va_nblocks; /* ditto */
797 gen = dmu_tx_get_txg(tx);
800 obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
801 bonuslen = (obj_type == DMU_OT_SA) ?
802 DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
805 * Create a new DMU object.
808 * There's currently no mechanism for pre-reading the blocks that will
809 * be needed to allocate a new object, so we accept the small chance
810 * that there will be an i/o error and we will fail one of the
813 if (vap->va_type == VDIR) {
814 if (zfsvfs->z_replay) {
815 VERIFY0(zap_create_claim_norm(zfsvfs->z_os, obj,
816 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
817 obj_type, bonuslen, tx));
819 obj = zap_create_norm(zfsvfs->z_os,
820 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
821 obj_type, bonuslen, tx);
824 if (zfsvfs->z_replay) {
825 VERIFY0(dmu_object_claim(zfsvfs->z_os, obj,
826 DMU_OT_PLAIN_FILE_CONTENTS, 0,
827 obj_type, bonuslen, tx));
829 obj = dmu_object_alloc(zfsvfs->z_os,
830 DMU_OT_PLAIN_FILE_CONTENTS, 0,
831 obj_type, bonuslen, tx);
835 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
836 VERIFY(0 == sa_buf_hold(zfsvfs->z_os, obj, NULL, &db));
839 * If this is the root, fix up the half-initialized parent pointer
840 * to reference the just-allocated physical data area.
842 if (flag & IS_ROOT_NODE) {
845 dzp_pflags = dzp->z_pflags;
849 * If parent is an xattr, so am I.
851 if (dzp_pflags & ZFS_XATTR) {
855 if (zfsvfs->z_use_fuids)
856 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
860 if (vap->va_type == VDIR) {
861 size = 2; /* contents ("." and "..") */
862 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
867 if (vap->va_type == VBLK || vap->va_type == VCHR) {
868 rdev = zfs_expldev(vap->va_rdev);
872 mode = acl_ids->z_mode;
877 * No execs denied will be deterimed when zfs_mode_compute() is called.
879 pflags |= acl_ids->z_aclp->z_hints &
880 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
881 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
883 ZFS_TIME_ENCODE(&now, crtime);
884 ZFS_TIME_ENCODE(&now, ctime);
886 if (vap->va_mask & AT_ATIME) {
887 ZFS_TIME_ENCODE(&vap->va_atime, atime);
889 ZFS_TIME_ENCODE(&now, atime);
892 if (vap->va_mask & AT_MTIME) {
893 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
895 ZFS_TIME_ENCODE(&now, mtime);
898 /* Now add in all of the "SA" attributes */
899 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED,
903 * Setup the array of attributes to be replaced/set on the new file
905 * order for DMU_OT_ZNODE is critical since it needs to be constructed
906 * in the old znode_phys_t format. Don't change this ordering
909 if (obj_type == DMU_OT_ZNODE) {
910 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
912 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
914 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
916 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
918 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
920 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
922 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
924 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
927 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
929 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
931 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
933 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
934 &acl_ids->z_fuid, 8);
935 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
936 &acl_ids->z_fgid, 8);
937 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
939 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
941 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
943 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
945 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
947 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
951 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
953 if (obj_type == DMU_OT_ZNODE) {
954 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL,
957 if (obj_type == DMU_OT_ZNODE ||
958 (vap->va_type == VBLK || vap->va_type == VCHR)) {
959 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs),
963 if (obj_type == DMU_OT_ZNODE) {
964 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
966 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
967 &acl_ids->z_fuid, 8);
968 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
969 &acl_ids->z_fgid, 8);
970 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad,
971 sizeof (uint64_t) * 4);
972 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
973 &acl_phys, sizeof (zfs_acl_phys_t));
974 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
975 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
976 &acl_ids->z_aclp->z_acl_count, 8);
977 locate.cb_aclp = acl_ids->z_aclp;
978 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs),
979 zfs_acl_data_locator, &locate,
980 acl_ids->z_aclp->z_acl_bytes);
981 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
982 acl_ids->z_fuid, acl_ids->z_fgid);
985 VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
987 if (!(flag & IS_ROOT_NODE)) {
988 *zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl);
989 ASSERT(*zpp != NULL);
992 * If we are creating the root node, the "parent" we
993 * passed in is the znode for the root.
997 (*zpp)->z_sa_hdl = sa_hdl;
1000 (*zpp)->z_pflags = pflags;
1001 (*zpp)->z_mode = mode;
1003 if (vap->va_mask & AT_XVATTR)
1004 zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx);
1006 if (obj_type == DMU_OT_ZNODE ||
1007 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
1008 VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
1010 if (!(flag & IS_ROOT_NODE)) {
1014 vp->v_vflag |= VV_FORCEINSMQ;
1015 err = insmntque(vp, zfsvfs->z_vfs);
1016 vp->v_vflag &= ~VV_FORCEINSMQ;
1017 KASSERT(err == 0, ("insmntque() failed: error %d", err));
1019 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1023 * Update in-core attributes. It is assumed the caller will be doing an
1024 * sa_bulk_update to push the changes out.
1027 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
1031 xoap = xva_getxoptattr(xvap);
1034 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
1036 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
1037 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
1038 ×, sizeof (times), tx);
1039 XVA_SET_RTN(xvap, XAT_CREATETIME);
1041 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
1042 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
1044 XVA_SET_RTN(xvap, XAT_READONLY);
1046 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
1047 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
1049 XVA_SET_RTN(xvap, XAT_HIDDEN);
1051 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
1052 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
1054 XVA_SET_RTN(xvap, XAT_SYSTEM);
1056 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
1057 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
1059 XVA_SET_RTN(xvap, XAT_ARCHIVE);
1061 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
1062 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
1064 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
1066 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
1067 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
1069 XVA_SET_RTN(xvap, XAT_NOUNLINK);
1071 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
1072 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
1074 XVA_SET_RTN(xvap, XAT_APPENDONLY);
1076 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
1077 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
1079 XVA_SET_RTN(xvap, XAT_NODUMP);
1081 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
1082 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
1084 XVA_SET_RTN(xvap, XAT_OPAQUE);
1086 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
1087 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
1088 xoap->xoa_av_quarantined, zp->z_pflags, tx);
1089 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
1091 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
1092 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
1094 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
1096 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
1097 zfs_sa_set_scanstamp(zp, xvap, tx);
1098 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
1100 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
1101 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
1103 XVA_SET_RTN(xvap, XAT_REPARSE);
1105 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
1106 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
1108 XVA_SET_RTN(xvap, XAT_OFFLINE);
1110 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
1111 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
1113 XVA_SET_RTN(xvap, XAT_SPARSE);
1118 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
1120 dmu_object_info_t doi;
1130 getnewvnode_reserve(1);
1133 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1135 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1137 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1138 getnewvnode_drop_reserve();
1142 dmu_object_info_from_db(db, &doi);
1143 if (doi.doi_bonus_type != DMU_OT_SA &&
1144 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1145 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1146 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1147 sa_buf_rele(db, NULL);
1148 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1150 getnewvnode_drop_reserve();
1152 return (SET_ERROR(EINVAL));
1155 hdl = dmu_buf_get_user(db);
1157 zp = sa_get_userdata(hdl);
1161 * Since "SA" does immediate eviction we
1162 * should never find a sa handle that doesn't
1163 * know about the znode.
1166 ASSERT3P(zp, !=, NULL);
1168 mutex_enter(&zp->z_lock);
1169 ASSERT3U(zp->z_id, ==, obj_num);
1170 if (zp->z_unlinked) {
1171 err = SET_ERROR(ENOENT);
1177 sa_buf_rele(db, NULL);
1179 /* Don't let the vnode disappear after ZFS_OBJ_HOLD_EXIT. */
1183 mutex_exit(&zp->z_lock);
1184 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1187 locked = VOP_ISLOCKED(vp);
1189 if ((vp->v_iflag & VI_DOOMED) != 0 &&
1190 locked != LK_EXCLUSIVE) {
1192 * The vnode is doomed and this thread doesn't
1193 * hold the exclusive lock on it, so the vnode
1194 * must be being reclaimed by another thread.
1195 * Otherwise the doomed vnode is being reclaimed
1196 * by this thread and zfs_zget is called from
1205 getnewvnode_drop_reserve();
1210 * Not found create new znode/vnode
1211 * but only if file exists.
1213 * There is a small window where zfs_vget() could
1214 * find this object while a file create is still in
1215 * progress. This is checked for in zfs_znode_alloc()
1217 * if zfs_znode_alloc() fails it will drop the hold on the
1220 zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size,
1221 doi.doi_bonus_type, NULL);
1223 err = SET_ERROR(ENOENT);
1228 vnode_t *vp = ZTOV(zp);
1230 err = insmntque(vp, zfsvfs->z_vfs);
1235 zfs_znode_dmu_fini(zp);
1240 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1241 getnewvnode_drop_reserve();
1246 zfs_rezget(znode_t *zp)
1248 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1249 dmu_object_info_t doi;
1252 uint64_t obj_num = zp->z_id;
1253 uint64_t mode, size;
1254 sa_bulk_attr_t bulk[8];
1259 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1261 mutex_enter(&zp->z_acl_lock);
1262 if (zp->z_acl_cached) {
1263 zfs_acl_free(zp->z_acl_cached);
1264 zp->z_acl_cached = NULL;
1267 mutex_exit(&zp->z_acl_lock);
1268 ASSERT(zp->z_sa_hdl == NULL);
1269 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1271 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1275 dmu_object_info_from_db(db, &doi);
1276 if (doi.doi_bonus_type != DMU_OT_SA &&
1277 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1278 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1279 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1280 sa_buf_rele(db, NULL);
1281 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1282 return (SET_ERROR(EINVAL));
1285 zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL);
1288 /* reload cached values */
1289 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1290 &gen, sizeof (gen));
1291 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
1292 &zp->z_size, sizeof (zp->z_size));
1293 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
1294 &zp->z_links, sizeof (zp->z_links));
1295 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1296 &zp->z_pflags, sizeof (zp->z_pflags));
1297 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1298 &zp->z_atime, sizeof (zp->z_atime));
1299 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1300 &zp->z_uid, sizeof (zp->z_uid));
1301 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1302 &zp->z_gid, sizeof (zp->z_gid));
1303 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1304 &mode, sizeof (mode));
1306 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1307 zfs_znode_dmu_fini(zp);
1308 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1309 return (SET_ERROR(EIO));
1314 if (gen != zp->z_gen) {
1315 zfs_znode_dmu_fini(zp);
1316 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1317 return (SET_ERROR(EIO));
1321 * XXXPJD: Not sure how is that possible, but under heavy
1322 * zfs recv -F load it happens that z_gen is the same, but
1323 * vnode type is different than znode type. This would mean
1324 * that for example regular file was replaced with directory
1325 * which has the same object number.
1329 vp->v_type != IFTOVT((mode_t)zp->z_mode)) {
1330 zfs_znode_dmu_fini(zp);
1331 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1335 zp->z_unlinked = (zp->z_links == 0);
1336 zp->z_blksz = doi.doi_data_block_size;
1338 vn_pages_remove(vp, 0, 0);
1339 if (zp->z_size != size)
1340 vnode_pager_setsize(vp, zp->z_size);
1343 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1349 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1351 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1352 objset_t *os = zfsvfs->z_os;
1353 uint64_t obj = zp->z_id;
1354 uint64_t acl_obj = zfs_external_acl(zp);
1356 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1358 VERIFY(!zp->z_is_sa);
1359 VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1361 VERIFY(0 == dmu_object_free(os, obj, tx));
1362 zfs_znode_dmu_fini(zp);
1363 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1368 zfs_zinactive(znode_t *zp)
1370 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1371 uint64_t z_id = zp->z_id;
1373 ASSERT(zp->z_sa_hdl);
1376 * Don't allow a zfs_zget() while were trying to release this znode
1378 ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1380 mutex_enter(&zp->z_lock);
1383 * If this was the last reference to a file with no links,
1384 * remove the file from the file system.
1386 if (zp->z_unlinked) {
1387 mutex_exit(&zp->z_lock);
1388 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1393 mutex_exit(&zp->z_lock);
1394 zfs_znode_dmu_fini(zp);
1395 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1400 zfs_znode_free(znode_t *zp)
1402 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1404 ASSERT(zp->z_sa_hdl == NULL);
1406 mutex_enter(&zfsvfs->z_znodes_lock);
1407 POINTER_INVALIDATE(&zp->z_zfsvfs);
1408 list_remove(&zfsvfs->z_all_znodes, zp);
1409 mutex_exit(&zfsvfs->z_znodes_lock);
1411 if (zp->z_acl_cached) {
1412 zfs_acl_free(zp->z_acl_cached);
1413 zp->z_acl_cached = NULL;
1416 kmem_cache_free(znode_cache, zp);
1418 VFS_RELE(zfsvfs->z_vfs);
1422 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1423 uint64_t ctime[2], boolean_t have_tx)
1429 if (have_tx) { /* will sa_bulk_update happen really soon? */
1430 zp->z_atime_dirty = 0;
1433 zp->z_atime_dirty = 1;
1436 if (flag & AT_ATIME) {
1437 ZFS_TIME_ENCODE(&now, zp->z_atime);
1440 if (flag & AT_MTIME) {
1441 ZFS_TIME_ENCODE(&now, mtime);
1442 if (zp->z_zfsvfs->z_use_fuids) {
1443 zp->z_pflags |= (ZFS_ARCHIVE |
1448 if (flag & AT_CTIME) {
1449 ZFS_TIME_ENCODE(&now, ctime);
1450 if (zp->z_zfsvfs->z_use_fuids)
1451 zp->z_pflags |= ZFS_ARCHIVE;
1456 * Grow the block size for a file.
1458 * IN: zp - znode of file to free data in.
1459 * size - requested block size
1460 * tx - open transaction.
1462 * NOTE: this function assumes that the znode is write locked.
1465 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1470 if (size <= zp->z_blksz)
1473 * If the file size is already greater than the current blocksize,
1474 * we will not grow. If there is more than one block in a file,
1475 * the blocksize cannot change.
1477 if (zp->z_blksz && zp->z_size > zp->z_blksz)
1480 error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1483 if (error == ENOTSUP)
1487 /* What blocksize did we actually get? */
1488 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1493 * This is a dummy interface used when pvn_vplist_dirty() should *not*
1494 * be calling back into the fs for a putpage(). E.g.: when truncating
1495 * a file, the pages being "thrown away* don't need to be written out.
1499 zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1500 int flags, cred_t *cr)
1508 * Increase the file length
1510 * IN: zp - znode of file to free data in.
1511 * end - new end-of-file
1513 * RETURN: 0 on success, error code on failure
1516 zfs_extend(znode_t *zp, uint64_t end)
1518 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1525 * We will change zp_size, lock the whole file.
1527 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1530 * Nothing to do if file already at desired length.
1532 if (end <= zp->z_size) {
1533 zfs_range_unlock(rl);
1536 tx = dmu_tx_create(zfsvfs->z_os);
1537 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1538 zfs_sa_upgrade_txholds(tx, zp);
1539 if (end > zp->z_blksz &&
1540 (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1542 * We are growing the file past the current block size.
1544 if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1545 ASSERT(!ISP2(zp->z_blksz));
1546 newblksz = MIN(end, SPA_MAXBLOCKSIZE);
1548 newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1550 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1555 error = dmu_tx_assign(tx, TXG_WAIT);
1558 zfs_range_unlock(rl);
1563 zfs_grow_blocksize(zp, newblksz, tx);
1567 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs),
1568 &zp->z_size, sizeof (zp->z_size), tx));
1570 vnode_pager_setsize(ZTOV(zp), end);
1572 zfs_range_unlock(rl);
1580 * Free space in a file.
1582 * IN: zp - znode of file to free data in.
1583 * off - start of section to free.
1584 * len - length of section to free.
1586 * RETURN: 0 on success, error code on failure
1589 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1591 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1596 * Lock the range being freed.
1598 rl = zfs_range_lock(zp, off, len, RL_WRITER);
1601 * Nothing to do if file already at desired length.
1603 if (off >= zp->z_size) {
1604 zfs_range_unlock(rl);
1608 if (off + len > zp->z_size)
1609 len = zp->z_size - off;
1611 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1615 * In FreeBSD we cannot free block in the middle of a file,
1616 * but only at the end of a file, so this code path should
1619 vnode_pager_setsize(ZTOV(zp), off);
1622 zfs_range_unlock(rl);
1630 * IN: zp - znode of file to free data in.
1631 * end - new end-of-file.
1633 * RETURN: 0 on success, error code on failure
1636 zfs_trunc(znode_t *zp, uint64_t end)
1638 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1639 vnode_t *vp = ZTOV(zp);
1643 sa_bulk_attr_t bulk[2];
1647 * We will change zp_size, lock the whole file.
1649 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1652 * Nothing to do if file already at desired length.
1654 if (end >= zp->z_size) {
1655 zfs_range_unlock(rl);
1659 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end, -1);
1661 zfs_range_unlock(rl);
1664 tx = dmu_tx_create(zfsvfs->z_os);
1665 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1666 zfs_sa_upgrade_txholds(tx, zp);
1667 error = dmu_tx_assign(tx, TXG_WAIT);
1670 zfs_range_unlock(rl);
1675 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
1676 NULL, &zp->z_size, sizeof (zp->z_size));
1679 zp->z_pflags &= ~ZFS_SPARSE;
1680 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1681 NULL, &zp->z_pflags, 8);
1683 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1688 * Clear any mapped pages in the truncated region. This has to
1689 * happen outside of the transaction to avoid the possibility of
1690 * a deadlock with someone trying to push a page that we are
1691 * about to invalidate.
1693 vnode_pager_setsize(vp, end);
1695 zfs_range_unlock(rl);
1701 * Free space in a file
1703 * IN: zp - znode of file to free data in.
1704 * off - start of range
1705 * len - end of range (0 => EOF)
1706 * flag - current file open mode flags.
1707 * log - TRUE if this action should be logged
1709 * RETURN: 0 on success, error code on failure
1712 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1714 vnode_t *vp = ZTOV(zp);
1716 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1717 zilog_t *zilog = zfsvfs->z_log;
1719 uint64_t mtime[2], ctime[2];
1720 sa_bulk_attr_t bulk[3];
1724 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
1725 sizeof (mode))) != 0)
1728 if (off > zp->z_size) {
1729 error = zfs_extend(zp, off+len);
1730 if (error == 0 && log)
1737 * Check for any locks in the region to be freed.
1740 if (MANDLOCK(vp, (mode_t)mode)) {
1741 uint64_t length = (len ? len : zp->z_size - off);
1742 if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1747 error = zfs_trunc(zp, off);
1749 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1750 off + len > zp->z_size)
1751 error = zfs_extend(zp, off+len);
1756 tx = dmu_tx_create(zfsvfs->z_os);
1757 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1758 zfs_sa_upgrade_txholds(tx, zp);
1759 error = dmu_tx_assign(tx, TXG_WAIT);
1765 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
1766 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
1767 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1768 NULL, &zp->z_pflags, 8);
1769 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1770 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1773 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1780 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1783 uint64_t moid, obj, sa_obj, version;
1784 uint64_t sense = ZFS_CASE_SENSITIVE;
1789 znode_t *rootzp = NULL;
1792 zfs_acl_ids_t acl_ids;
1795 * First attempt to create master node.
1798 * In an empty objset, there are no blocks to read and thus
1799 * there can be no i/o errors (which we assert below).
1801 moid = MASTER_NODE_OBJ;
1802 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1803 DMU_OT_NONE, 0, tx);
1807 * Set starting attributes.
1809 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1811 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1812 /* For the moment we expect all zpl props to be uint64_ts */
1816 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1817 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1818 name = nvpair_name(elem);
1819 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1823 error = zap_update(os, moid, name, 8, 1, &val, tx);
1826 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1828 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1831 ASSERT(version != 0);
1832 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1835 * Create zap object used for SA attribute registration
1838 if (version >= ZPL_VERSION_SA) {
1839 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1840 DMU_OT_NONE, 0, tx);
1841 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1847 * Create a delete queue.
1849 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1851 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1855 * Create root znode. Create minimal znode/vnode/zfsvfs
1856 * to allow zfs_mknode to work.
1859 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1860 vattr.va_type = VDIR;
1861 vattr.va_mode = S_IFDIR|0755;
1862 vattr.va_uid = crgetuid(cr);
1863 vattr.va_gid = crgetgid(cr);
1865 bzero(&zfsvfs, sizeof (zfsvfs_t));
1867 rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1868 ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1869 rootzp->z_moved = 0;
1870 rootzp->z_unlinked = 0;
1871 rootzp->z_atime_dirty = 0;
1872 rootzp->z_is_sa = USE_SA(version, os);
1875 zfsvfs.z_parent = &zfsvfs;
1876 zfsvfs.z_version = version;
1877 zfsvfs.z_use_fuids = USE_FUIDS(version, os);
1878 zfsvfs.z_use_sa = USE_SA(version, os);
1879 zfsvfs.z_norm = norm;
1881 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1882 &zfsvfs.z_attr_table);
1887 * Fold case on file systems that are always or sometimes case
1890 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1891 zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER;
1893 mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1894 list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
1895 offsetof(znode_t, z_link_node));
1897 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1898 mutex_init(&zfsvfs.z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1900 rootzp->z_zfsvfs = &zfsvfs;
1901 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1902 cr, NULL, &acl_ids));
1903 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1904 ASSERT3P(zp, ==, rootzp);
1905 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1907 zfs_acl_ids_free(&acl_ids);
1908 POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1910 sa_handle_destroy(rootzp->z_sa_hdl);
1911 kmem_cache_free(znode_cache, rootzp);
1914 * Create shares directory
1917 error = zfs_create_share_dir(&zfsvfs, tx);
1921 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1922 mutex_destroy(&zfsvfs.z_hold_mtx[i]);
1925 #endif /* _KERNEL */
1928 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1930 uint64_t sa_obj = 0;
1933 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1934 if (error != 0 && error != ENOENT)
1937 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1942 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1943 dmu_buf_t **db, void *tag)
1945 dmu_object_info_t doi;
1948 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
1951 dmu_object_info_from_db(*db, &doi);
1952 if ((doi.doi_bonus_type != DMU_OT_SA &&
1953 doi.doi_bonus_type != DMU_OT_ZNODE) ||
1954 doi.doi_bonus_type == DMU_OT_ZNODE &&
1955 doi.doi_bonus_size < sizeof (znode_phys_t)) {
1956 sa_buf_rele(*db, tag);
1957 return (SET_ERROR(ENOTSUP));
1960 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
1962 sa_buf_rele(*db, tag);
1970 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
1972 sa_handle_destroy(hdl);
1973 sa_buf_rele(db, tag);
1977 * Given an object number, return its parent object number and whether
1978 * or not the object is an extended attribute directory.
1981 zfs_obj_to_pobj(objset_t *osp, sa_handle_t *hdl, sa_attr_type_t *sa_table,
1982 uint64_t *pobjp, int *is_xattrdir)
1987 uint64_t parent_mode;
1988 sa_bulk_attr_t bulk[3];
1989 sa_handle_t *sa_hdl;
1994 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
1995 &parent, sizeof (parent));
1996 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
1997 &pflags, sizeof (pflags));
1998 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1999 &mode, sizeof (mode));
2001 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
2005 * When a link is removed its parent pointer is not changed and will
2006 * be invalid. There are two cases where a link is removed but the
2007 * file stays around, when it goes to the delete queue and when there
2008 * are additional links.
2010 error = zfs_grab_sa_handle(osp, parent, &sa_hdl, &sa_db, FTAG);
2014 error = sa_lookup(sa_hdl, ZPL_MODE, &parent_mode, sizeof (parent_mode));
2015 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2019 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
2022 * Extended attributes can be applied to files, directories, etc.
2023 * Otherwise the parent must be a directory.
2025 if (!*is_xattrdir && !S_ISDIR(parent_mode))
2026 return (SET_ERROR(EINVAL));
2034 * Given an object number, return some zpl level statistics
2037 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
2040 sa_bulk_attr_t bulk[4];
2043 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2044 &sb->zs_mode, sizeof (sb->zs_mode));
2045 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
2046 &sb->zs_gen, sizeof (sb->zs_gen));
2047 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
2048 &sb->zs_links, sizeof (sb->zs_links));
2049 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
2050 &sb->zs_ctime, sizeof (sb->zs_ctime));
2052 return (sa_bulk_lookup(hdl, bulk, count));
2056 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
2057 sa_attr_type_t *sa_table, char *buf, int len)
2059 sa_handle_t *sa_hdl;
2060 sa_handle_t *prevhdl = NULL;
2061 dmu_buf_t *prevdb = NULL;
2062 dmu_buf_t *sa_db = NULL;
2063 char *path = buf + len - 1;
2071 char component[MAXNAMELEN + 2];
2076 zfs_release_sa_handle(prevhdl, prevdb, FTAG);
2078 if ((error = zfs_obj_to_pobj(osp, sa_hdl, sa_table, &pobj,
2079 &is_xattrdir)) != 0)
2090 (void) sprintf(component + 1, "<xattrdir>");
2092 error = zap_value_search(osp, pobj, obj,
2093 ZFS_DIRENT_OBJ(-1ULL), component + 1);
2098 complen = strlen(component);
2100 ASSERT(path >= buf);
2101 bcopy(component, path, complen);
2104 if (sa_hdl != hdl) {
2108 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
2116 if (sa_hdl != NULL && sa_hdl != hdl) {
2117 ASSERT(sa_db != NULL);
2118 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2122 (void) memmove(buf, path, buf + len - path);
2128 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
2130 sa_attr_type_t *sa_table;
2135 error = zfs_sa_setup(osp, &sa_table);
2139 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2143 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2145 zfs_release_sa_handle(hdl, db, FTAG);
2150 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
2153 char *path = buf + len - 1;
2154 sa_attr_type_t *sa_table;
2161 error = zfs_sa_setup(osp, &sa_table);
2165 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2169 error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
2171 zfs_release_sa_handle(hdl, db, FTAG);
2175 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2177 zfs_release_sa_handle(hdl, db, FTAG);