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.
25 /* Portions Copyright 2007 Jeremy Teo */
28 #include <sys/types.h>
29 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/resource.h>
34 #include <sys/mntent.h>
35 #include <sys/u8_textprep.h>
36 #include <sys/dsl_dataset.h>
38 #include <sys/vnode.h>
41 #include <sys/errno.h>
42 #include <sys/unistd.h>
43 #include <sys/atomic.h>
44 #include <sys/zfs_dir.h>
45 #include <sys/zfs_acl.h>
46 #include <sys/zfs_ioctl.h>
47 #include <sys/zfs_rlock.h>
48 #include <sys/zfs_fuid.h>
49 #include <sys/dnode.h>
50 #include <sys/fs/zfs.h>
51 #include <sys/kidmap.h>
55 #include <sys/refcount.h>
58 #include <sys/zfs_znode.h>
60 #include <sys/zfs_sa.h>
61 #include <sys/zfs_stat.h>
62 #include <sys/refcount.h>
65 #include "zfs_comutil.h"
67 /* Used by fstat(1). */
68 SYSCTL_INT(_debug_sizeof, OID_AUTO, znode, CTLFLAG_RD, 0, sizeof(znode_t),
72 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
73 * turned on when DEBUG is also defined.
80 #define ZNODE_STAT_ADD(stat) ((stat)++)
82 #define ZNODE_STAT_ADD(stat) /* nothing */
83 #endif /* ZNODE_STATS */
86 * Functions needed for userland (ie: libzpool) are not put under
87 * #ifdef_KERNEL; the rest of the functions have dependencies
88 * (such as VFS logic) that will not compile easily in userland.
92 * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to
93 * be freed before it can be safely accessed.
95 krwlock_t zfsvfs_lock;
97 static kmem_cache_t *znode_cache = NULL;
101 znode_evict_error(dmu_buf_t *dbuf, void *user_ptr)
104 * We should never drop all dbuf refs without first clearing
105 * the eviction callback.
107 panic("evicting znode %p\n", user_ptr);
110 extern struct vop_vector zfs_vnodeops;
111 extern struct vop_vector zfs_fifoops;
112 extern struct vop_vector zfs_shareops;
115 * XXX: We cannot use this function as a cache constructor, because
116 * there is one global cache for all file systems and we need
117 * to pass vfsp here, which is not possible, because argument
118 * 'cdrarg' is defined at kmem_cache_create() time.
122 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
129 POINTER_INVALIDATE(&zp->z_zfsvfs);
130 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
133 error = getnewvnode("zfs", vfsp, &zfs_vnodeops, &vp);
134 if (error != 0 && (kmflags & KM_NOSLEEP))
137 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
139 vp->v_data = (caddr_t)zp;
146 list_link_init(&zp->z_link_node);
148 mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
149 rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
150 rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
151 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
153 mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
154 avl_create(&zp->z_range_avl, zfs_range_compare,
155 sizeof (rl_t), offsetof(rl_t, r_node));
157 zp->z_dirlocks = NULL;
158 zp->z_acl_cached = NULL;
165 zfs_znode_cache_destructor(void *buf, void *arg)
169 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
170 ASSERT(ZTOV(zp) == NULL);
172 ASSERT(!list_link_active(&zp->z_link_node));
173 mutex_destroy(&zp->z_lock);
174 rw_destroy(&zp->z_parent_lock);
175 rw_destroy(&zp->z_name_lock);
176 mutex_destroy(&zp->z_acl_lock);
177 avl_destroy(&zp->z_range_avl);
178 mutex_destroy(&zp->z_range_lock);
180 ASSERT(zp->z_dirlocks == NULL);
181 ASSERT(zp->z_acl_cached == NULL);
186 uint64_t zms_zfsvfs_invalid;
187 uint64_t zms_zfsvfs_recheck1;
188 uint64_t zms_zfsvfs_unmounted;
189 uint64_t zms_zfsvfs_recheck2;
190 uint64_t zms_obj_held;
191 uint64_t zms_vnode_locked;
192 uint64_t zms_not_only_dnlc;
194 #endif /* ZNODE_STATS */
198 zfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
203 nzp->z_zfsvfs = ozp->z_zfsvfs;
207 nzp->z_vnode = ozp->z_vnode;
208 ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
209 ZTOV(ozp)->v_data = ozp;
210 ZTOV(nzp)->v_data = nzp;
212 nzp->z_id = ozp->z_id;
213 ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
214 ASSERT(avl_numnodes(&ozp->z_range_avl) == 0);
215 nzp->z_unlinked = ozp->z_unlinked;
216 nzp->z_atime_dirty = ozp->z_atime_dirty;
217 nzp->z_zn_prefetch = ozp->z_zn_prefetch;
218 nzp->z_blksz = ozp->z_blksz;
219 nzp->z_seq = ozp->z_seq;
220 nzp->z_mapcnt = ozp->z_mapcnt;
221 nzp->z_gen = ozp->z_gen;
222 nzp->z_sync_cnt = ozp->z_sync_cnt;
223 nzp->z_is_sa = ozp->z_is_sa;
224 nzp->z_sa_hdl = ozp->z_sa_hdl;
225 bcopy(ozp->z_atime, nzp->z_atime, sizeof (uint64_t) * 2);
226 nzp->z_links = ozp->z_links;
227 nzp->z_size = ozp->z_size;
228 nzp->z_pflags = ozp->z_pflags;
229 nzp->z_uid = ozp->z_uid;
230 nzp->z_gid = ozp->z_gid;
231 nzp->z_mode = ozp->z_mode;
234 * Since this is just an idle znode and kmem is already dealing with
235 * memory pressure, release any cached ACL.
237 if (ozp->z_acl_cached) {
238 zfs_acl_free(ozp->z_acl_cached);
239 ozp->z_acl_cached = NULL;
242 sa_set_userp(nzp->z_sa_hdl, nzp);
245 * Invalidate the original znode by clearing fields that provide a
246 * pointer back to the znode. Set the low bit of the vfs pointer to
247 * ensure that zfs_znode_move() recognizes the znode as invalid in any
248 * subsequent callback.
250 ozp->z_sa_hdl = NULL;
251 POINTER_INVALIDATE(&ozp->z_zfsvfs);
257 ozp->z_moved = (uint8_t)-1;
262 zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg)
264 znode_t *ozp = buf, *nzp = newbuf;
269 * The znode is on the file system's list of known znodes if the vfs
270 * pointer is valid. We set the low bit of the vfs pointer when freeing
271 * the znode to invalidate it, and the memory patterns written by kmem
272 * (baddcafe and deadbeef) set at least one of the two low bits. A newly
273 * created znode sets the vfs pointer last of all to indicate that the
274 * znode is known and in a valid state to be moved by this function.
276 zfsvfs = ozp->z_zfsvfs;
277 if (!POINTER_IS_VALID(zfsvfs)) {
278 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid);
279 return (KMEM_CBRC_DONT_KNOW);
283 * Close a small window in which it's possible that the filesystem could
284 * be unmounted and freed, and zfsvfs, though valid in the previous
285 * statement, could point to unrelated memory by the time we try to
286 * prevent the filesystem from being unmounted.
288 rw_enter(&zfsvfs_lock, RW_WRITER);
289 if (zfsvfs != ozp->z_zfsvfs) {
290 rw_exit(&zfsvfs_lock);
291 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1);
292 return (KMEM_CBRC_DONT_KNOW);
296 * If the znode is still valid, then so is the file system. We know that
297 * no valid file system can be freed while we hold zfsvfs_lock, so we
298 * can safely ensure that the filesystem is not and will not be
299 * unmounted. The next statement is equivalent to ZFS_ENTER().
301 rrw_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG);
302 if (zfsvfs->z_unmounted) {
304 rw_exit(&zfsvfs_lock);
305 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
306 return (KMEM_CBRC_DONT_KNOW);
308 rw_exit(&zfsvfs_lock);
310 mutex_enter(&zfsvfs->z_znodes_lock);
312 * Recheck the vfs pointer in case the znode was removed just before
313 * acquiring the lock.
315 if (zfsvfs != ozp->z_zfsvfs) {
316 mutex_exit(&zfsvfs->z_znodes_lock);
318 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2);
319 return (KMEM_CBRC_DONT_KNOW);
323 * At this point we know that as long as we hold z_znodes_lock, the
324 * znode cannot be freed and fields within the znode can be safely
325 * accessed. Now, prevent a race with zfs_zget().
327 if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) {
328 mutex_exit(&zfsvfs->z_znodes_lock);
330 ZNODE_STAT_ADD(znode_move_stats.zms_obj_held);
331 return (KMEM_CBRC_LATER);
335 if (mutex_tryenter(&vp->v_lock) == 0) {
336 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
337 mutex_exit(&zfsvfs->z_znodes_lock);
339 ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
340 return (KMEM_CBRC_LATER);
343 /* Only move znodes that are referenced _only_ by the DNLC. */
344 if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
345 mutex_exit(&vp->v_lock);
346 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
347 mutex_exit(&zfsvfs->z_znodes_lock);
349 ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc);
350 return (KMEM_CBRC_LATER);
354 * The znode is known and in a valid state to move. We're holding the
355 * locks needed to execute the critical section.
357 zfs_znode_move_impl(ozp, nzp);
358 mutex_exit(&vp->v_lock);
359 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
361 list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
362 mutex_exit(&zfsvfs->z_znodes_lock);
365 return (KMEM_CBRC_YES);
375 rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL);
376 ASSERT(znode_cache == NULL);
377 znode_cache = kmem_cache_create("zfs_znode_cache",
378 sizeof (znode_t), 0, /* zfs_znode_cache_constructor */ NULL,
379 zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
380 kmem_cache_set_move(znode_cache, zfs_znode_move);
388 * Cleanup vfs & vnode ops
390 zfs_remove_op_tables();
397 kmem_cache_destroy(znode_cache);
399 rw_destroy(&zfsvfs_lock);
403 struct vnodeops *zfs_dvnodeops;
404 struct vnodeops *zfs_fvnodeops;
405 struct vnodeops *zfs_symvnodeops;
406 struct vnodeops *zfs_xdvnodeops;
407 struct vnodeops *zfs_evnodeops;
408 struct vnodeops *zfs_sharevnodeops;
411 zfs_remove_op_tables()
417 (void) vfs_freevfsops_by_type(zfsfstype);
424 vn_freevnodeops(zfs_dvnodeops);
426 vn_freevnodeops(zfs_fvnodeops);
428 vn_freevnodeops(zfs_symvnodeops);
430 vn_freevnodeops(zfs_xdvnodeops);
432 vn_freevnodeops(zfs_evnodeops);
433 if (zfs_sharevnodeops)
434 vn_freevnodeops(zfs_sharevnodeops);
436 zfs_dvnodeops = NULL;
437 zfs_fvnodeops = NULL;
438 zfs_symvnodeops = NULL;
439 zfs_xdvnodeops = NULL;
440 zfs_evnodeops = NULL;
441 zfs_sharevnodeops = NULL;
444 extern const fs_operation_def_t zfs_dvnodeops_template[];
445 extern const fs_operation_def_t zfs_fvnodeops_template[];
446 extern const fs_operation_def_t zfs_xdvnodeops_template[];
447 extern const fs_operation_def_t zfs_symvnodeops_template[];
448 extern const fs_operation_def_t zfs_evnodeops_template[];
449 extern const fs_operation_def_t zfs_sharevnodeops_template[];
452 zfs_create_op_tables()
457 * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
458 * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
459 * In this case we just return as the ops vectors are already set up.
464 error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template,
469 error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template,
474 error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template,
479 error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template,
484 error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template,
489 error = vn_make_ops(MNTTYPE_ZFS, zfs_sharevnodeops_template,
497 zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
499 zfs_acl_ids_t acl_ids;
506 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
507 vattr.va_type = VDIR;
508 vattr.va_mode = S_IFDIR|0555;
509 vattr.va_uid = crgetuid(kcred);
510 vattr.va_gid = crgetgid(kcred);
512 sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP);
513 zfs_znode_cache_constructor(sharezp, zfsvfs->z_parent->z_vfs, 0);
514 ASSERT(!POINTER_IS_VALID(sharezp->z_zfsvfs));
515 sharezp->z_moved = 0;
516 sharezp->z_unlinked = 0;
517 sharezp->z_atime_dirty = 0;
518 sharezp->z_zfsvfs = zfsvfs;
519 sharezp->z_is_sa = zfsvfs->z_use_sa;
521 sharezp->z_vnode = &vnode;
522 vnode.v_data = sharezp;
527 VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
528 kcred, NULL, &acl_ids));
529 zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
530 ASSERT3P(zp, ==, sharezp);
531 POINTER_INVALIDATE(&sharezp->z_zfsvfs);
532 error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
533 ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
534 zfsvfs->z_shares_dir = sharezp->z_id;
536 zfs_acl_ids_free(&acl_ids);
537 ZTOV(sharezp)->v_data = NULL;
538 ZTOV(sharezp)->v_count = 0;
539 ZTOV(sharezp)->v_holdcnt = 0;
541 sa_handle_destroy(sharezp->z_sa_hdl);
542 sharezp->z_vnode = NULL;
543 kmem_cache_free(znode_cache, sharezp);
549 * define a couple of values we need available
550 * for both 64 and 32 bit environments.
553 #define NBITSMINOR64 32
556 #define MAXMAJ64 0xffffffffUL
559 #define MAXMIN64 0xffffffffUL
563 * Create special expldev for ZFS private use.
564 * Can't use standard expldev since it doesn't do
565 * what we want. The standard expldev() takes a
566 * dev32_t in LP64 and expands it to a long dev_t.
567 * We need an interface that takes a dev32_t in ILP32
568 * and expands it to a long dev_t.
571 zfs_expldev(dev_t dev)
573 return (((uint64_t)major(dev) << NBITSMINOR64) | minor(dev));
576 * Special cmpldev for ZFS private use.
577 * Can't use standard cmpldev since it takes
578 * a long dev_t and compresses it to dev32_t in
579 * LP64. We need to do a compaction of a long dev_t
580 * to a dev32_t in ILP32.
583 zfs_cmpldev(uint64_t dev)
585 return (makedev((dev >> NBITSMINOR64), (dev & MAXMIN64)));
589 zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp,
590 dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
592 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
593 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
595 mutex_enter(&zp->z_lock);
597 ASSERT(zp->z_sa_hdl == NULL);
598 ASSERT(zp->z_acl_cached == NULL);
599 if (sa_hdl == NULL) {
600 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, zp,
601 SA_HDL_SHARED, &zp->z_sa_hdl));
603 zp->z_sa_hdl = sa_hdl;
604 sa_set_userp(sa_hdl, zp);
607 zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
610 * Slap on VROOT if we are the root znode
612 if (zp->z_id == zfsvfs->z_root)
613 ZTOV(zp)->v_flag |= VROOT;
615 mutex_exit(&zp->z_lock);
620 zfs_znode_dmu_fini(znode_t *zp)
622 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
624 RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
626 sa_handle_destroy(zp->z_sa_hdl);
631 * Construct a new znode/vnode and intialize.
633 * This does not do a call to dmu_set_user() that is
634 * up to the caller to do, in case you don't want to
638 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz,
639 dmu_object_type_t obj_type, sa_handle_t *hdl)
645 sa_bulk_attr_t bulk[9];
648 zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
649 zfs_znode_cache_constructor(zp, zfsvfs->z_parent->z_vfs, 0);
651 ASSERT(zp->z_dirlocks == NULL);
652 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
656 * Defer setting z_zfsvfs until the znode is ready to be a candidate for
657 * the zfs_znode_move() callback.
661 zp->z_atime_dirty = 0;
663 zp->z_id = db->db_object;
665 zp->z_seq = 0x7A4653;
670 zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl);
672 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
673 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8);
674 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
676 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
678 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
680 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
681 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
683 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
685 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
688 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
690 sa_handle_destroy(zp->z_sa_hdl);
691 kmem_cache_free(znode_cache, zp);
697 vp->v_type = IFTOVT((mode_t)mode);
699 switch (vp->v_type) {
701 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
704 vp->v_op = &zfs_fifoops;
707 if (parent == zfsvfs->z_shares_dir) {
708 ASSERT(zp->z_uid == 0 && zp->z_gid == 0);
709 vp->v_op = &zfs_shareops;
713 if (vp->v_type != VFIFO)
716 mutex_enter(&zfsvfs->z_znodes_lock);
717 list_insert_tail(&zfsvfs->z_all_znodes, zp);
720 * Everything else must be valid before assigning z_zfsvfs makes the
721 * znode eligible for zfs_znode_move().
723 zp->z_zfsvfs = zfsvfs;
724 mutex_exit(&zfsvfs->z_znodes_lock);
726 VFS_HOLD(zfsvfs->z_vfs);
730 static uint64_t empty_xattr;
731 static uint64_t pad[4];
732 static zfs_acl_phys_t acl_phys;
734 * Create a new DMU object to hold a zfs znode.
736 * IN: dzp - parent directory for new znode
737 * vap - file attributes for new znode
738 * tx - dmu transaction id for zap operations
739 * cr - credentials of caller
741 * IS_ROOT_NODE - new object will be root
742 * IS_XATTR - new object is an attribute
743 * bonuslen - length of bonus buffer
744 * setaclp - File/Dir initial ACL
745 * fuidp - Tracks fuid allocation.
747 * OUT: zpp - allocated znode
751 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
752 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
754 uint64_t crtime[2], atime[2], mtime[2], ctime[2];
755 uint64_t mode, size, links, parent, pflags;
756 uint64_t dzp_pflags = 0;
758 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
765 dmu_object_type_t obj_type;
766 sa_bulk_attr_t sa_attrs[ZPL_END];
768 zfs_acl_locator_cb_t locate = { 0 };
770 ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
772 if (zfsvfs->z_replay) {
773 obj = vap->va_nodeid;
774 now = vap->va_ctime; /* see zfs_replay_create() */
775 gen = vap->va_nblocks; /* ditto */
779 gen = dmu_tx_get_txg(tx);
782 obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
783 bonuslen = (obj_type == DMU_OT_SA) ?
784 DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
787 * Create a new DMU object.
790 * There's currently no mechanism for pre-reading the blocks that will
791 * be needed to allocate a new object, so we accept the small chance
792 * that there will be an i/o error and we will fail one of the
795 if (vap->va_type == VDIR) {
796 if (zfsvfs->z_replay) {
797 err = zap_create_claim_norm(zfsvfs->z_os, obj,
798 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
799 obj_type, bonuslen, tx);
800 ASSERT3U(err, ==, 0);
802 obj = zap_create_norm(zfsvfs->z_os,
803 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
804 obj_type, bonuslen, tx);
807 if (zfsvfs->z_replay) {
808 err = dmu_object_claim(zfsvfs->z_os, obj,
809 DMU_OT_PLAIN_FILE_CONTENTS, 0,
810 obj_type, bonuslen, tx);
811 ASSERT3U(err, ==, 0);
813 obj = dmu_object_alloc(zfsvfs->z_os,
814 DMU_OT_PLAIN_FILE_CONTENTS, 0,
815 obj_type, bonuslen, tx);
819 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
820 VERIFY(0 == sa_buf_hold(zfsvfs->z_os, obj, NULL, &db));
823 * If this is the root, fix up the half-initialized parent pointer
824 * to reference the just-allocated physical data area.
826 if (flag & IS_ROOT_NODE) {
829 dzp_pflags = dzp->z_pflags;
833 * If parent is an xattr, so am I.
835 if (dzp_pflags & ZFS_XATTR) {
839 if (zfsvfs->z_use_fuids)
840 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
844 if (vap->va_type == VDIR) {
845 size = 2; /* contents ("." and "..") */
846 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
851 if (vap->va_type == VBLK || vap->va_type == VCHR) {
852 rdev = zfs_expldev(vap->va_rdev);
856 mode = acl_ids->z_mode;
861 * No execs denied will be deterimed when zfs_mode_compute() is called.
863 pflags |= acl_ids->z_aclp->z_hints &
864 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
865 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
867 ZFS_TIME_ENCODE(&now, crtime);
868 ZFS_TIME_ENCODE(&now, ctime);
870 if (vap->va_mask & AT_ATIME) {
871 ZFS_TIME_ENCODE(&vap->va_atime, atime);
873 ZFS_TIME_ENCODE(&now, atime);
876 if (vap->va_mask & AT_MTIME) {
877 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
879 ZFS_TIME_ENCODE(&now, mtime);
882 /* Now add in all of the "SA" attributes */
883 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED,
887 * Setup the array of attributes to be replaced/set on the new file
889 * order for DMU_OT_ZNODE is critical since it needs to be constructed
890 * in the old znode_phys_t format. Don't change this ordering
893 if (obj_type == DMU_OT_ZNODE) {
894 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
896 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
898 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
900 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
902 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
904 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
906 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
908 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
911 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
913 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
915 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
917 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
918 &acl_ids->z_fuid, 8);
919 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
920 &acl_ids->z_fgid, 8);
921 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
923 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
925 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
927 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
929 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
931 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
935 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
937 if (obj_type == DMU_OT_ZNODE) {
938 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL,
941 if (obj_type == DMU_OT_ZNODE ||
942 (vap->va_type == VBLK || vap->va_type == VCHR)) {
943 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs),
947 if (obj_type == DMU_OT_ZNODE) {
948 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
950 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
951 &acl_ids->z_fuid, 8);
952 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
953 &acl_ids->z_fgid, 8);
954 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad,
955 sizeof (uint64_t) * 4);
956 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
957 &acl_phys, sizeof (zfs_acl_phys_t));
958 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
959 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
960 &acl_ids->z_aclp->z_acl_count, 8);
961 locate.cb_aclp = acl_ids->z_aclp;
962 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs),
963 zfs_acl_data_locator, &locate,
964 acl_ids->z_aclp->z_acl_bytes);
965 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
966 acl_ids->z_fuid, acl_ids->z_fgid);
969 VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
971 if (!(flag & IS_ROOT_NODE)) {
972 *zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl);
973 ASSERT(*zpp != NULL);
976 * If we are creating the root node, the "parent" we
977 * passed in is the znode for the root.
981 (*zpp)->z_sa_hdl = sa_hdl;
984 (*zpp)->z_pflags = pflags;
985 (*zpp)->z_mode = mode;
987 if (vap->va_mask & AT_XVATTR)
988 zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx);
990 if (obj_type == DMU_OT_ZNODE ||
991 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
992 err = zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx);
993 ASSERT3P(err, ==, 0);
995 if (!(flag & IS_ROOT_NODE)) {
999 vp->v_vflag |= VV_FORCEINSMQ;
1000 err = insmntque(vp, zfsvfs->z_vfs);
1001 vp->v_vflag &= ~VV_FORCEINSMQ;
1002 KASSERT(err == 0, ("insmntque() failed: error %d", err));
1004 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1008 * zfs_xvattr_set only updates the in-core attributes
1009 * it is assumed the caller will be doing an sa_bulk_update
1010 * to push the changes out
1013 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
1017 xoap = xva_getxoptattr(xvap);
1020 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
1022 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
1023 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
1024 ×, sizeof (times), tx);
1025 XVA_SET_RTN(xvap, XAT_CREATETIME);
1027 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
1028 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
1030 XVA_SET_RTN(xvap, XAT_READONLY);
1032 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
1033 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
1035 XVA_SET_RTN(xvap, XAT_HIDDEN);
1037 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
1038 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
1040 XVA_SET_RTN(xvap, XAT_SYSTEM);
1042 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
1043 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
1045 XVA_SET_RTN(xvap, XAT_ARCHIVE);
1047 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
1048 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
1050 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
1052 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
1053 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
1055 XVA_SET_RTN(xvap, XAT_NOUNLINK);
1057 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
1058 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
1060 XVA_SET_RTN(xvap, XAT_APPENDONLY);
1062 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
1063 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
1065 XVA_SET_RTN(xvap, XAT_NODUMP);
1067 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
1068 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
1070 XVA_SET_RTN(xvap, XAT_OPAQUE);
1072 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
1073 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
1074 xoap->xoa_av_quarantined, zp->z_pflags, tx);
1075 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
1077 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
1078 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
1080 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
1082 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
1083 zfs_sa_set_scanstamp(zp, xvap, tx);
1084 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
1086 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
1087 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
1089 XVA_SET_RTN(xvap, XAT_REPARSE);
1091 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
1092 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
1094 XVA_SET_RTN(xvap, XAT_OFFLINE);
1096 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
1097 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
1099 XVA_SET_RTN(xvap, XAT_SPARSE);
1104 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
1106 dmu_object_info_t doi;
1116 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1118 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1120 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1124 dmu_object_info_from_db(db, &doi);
1125 if (doi.doi_bonus_type != DMU_OT_SA &&
1126 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1127 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1128 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1129 sa_buf_rele(db, NULL);
1130 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1134 hdl = dmu_buf_get_user(db);
1136 zp = sa_get_userdata(hdl);
1140 * Since "SA" does immediate eviction we
1141 * should never find a sa handle that doesn't
1142 * know about the znode.
1145 ASSERT3P(zp, !=, NULL);
1147 mutex_enter(&zp->z_lock);
1148 ASSERT3U(zp->z_id, ==, obj_num);
1149 if (zp->z_unlinked) {
1160 if ((vp->v_iflag & VI_DOOMED) != 0) {
1163 * Don't VN_RELE() vnode here, because
1164 * it can call vn_lock() which creates
1165 * LOR between vnode lock and znode
1166 * lock. We will VN_RELE() the vnode
1167 * after droping znode lock.
1173 ZFS_LOG(1, "dying znode detected (zp=%p)", zp);
1177 * znode is dying so we can't reuse it, we must
1178 * wait until destruction is completed.
1180 sa_buf_rele(db, NULL);
1181 mutex_exit(&zp->z_lock);
1182 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1185 tsleep(zp, 0, "zcollide", 1);
1191 sa_buf_rele(db, NULL);
1192 mutex_exit(&zp->z_lock);
1193 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1198 * Not found create new znode/vnode
1199 * but only if file exists.
1201 * There is a small window where zfs_vget() could
1202 * find this object while a file create is still in
1203 * progress. This is checked for in zfs_znode_alloc()
1205 * if zfs_znode_alloc() fails it will drop the hold on the
1208 zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size,
1209 doi.doi_bonus_type, NULL);
1216 vnode_t *vp = ZTOV(zp);
1218 err = insmntque(vp, zfsvfs->z_vfs);
1223 zfs_znode_dmu_fini(zp);
1228 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1233 zfs_rezget(znode_t *zp)
1235 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1236 dmu_object_info_t doi;
1238 uint64_t obj_num = zp->z_id;
1239 uint64_t mode, size;
1240 sa_bulk_attr_t bulk[8];
1245 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1247 mutex_enter(&zp->z_acl_lock);
1248 if (zp->z_acl_cached) {
1249 zfs_acl_free(zp->z_acl_cached);
1250 zp->z_acl_cached = NULL;
1253 mutex_exit(&zp->z_acl_lock);
1254 ASSERT(zp->z_sa_hdl == NULL);
1255 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1257 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1261 dmu_object_info_from_db(db, &doi);
1262 if (doi.doi_bonus_type != DMU_OT_SA &&
1263 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1264 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1265 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1266 sa_buf_rele(db, NULL);
1267 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1271 zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL);
1274 /* reload cached values */
1275 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1276 &gen, sizeof (gen));
1277 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
1278 &zp->z_size, sizeof (zp->z_size));
1279 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
1280 &zp->z_links, sizeof (zp->z_links));
1281 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1282 &zp->z_pflags, sizeof (zp->z_pflags));
1283 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1284 &zp->z_atime, sizeof (zp->z_atime));
1285 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1286 &zp->z_uid, sizeof (zp->z_uid));
1287 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1288 &zp->z_gid, sizeof (zp->z_gid));
1289 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1290 &mode, sizeof (mode));
1292 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1293 zfs_znode_dmu_fini(zp);
1294 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1300 if (gen != zp->z_gen) {
1301 zfs_znode_dmu_fini(zp);
1302 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1307 * XXXPJD: Not sure how is that possible, but under heavy
1308 * zfs recv -F load it happens that z_gen is the same, but
1309 * vnode type is different than znode type. This would mean
1310 * that for example regular file was replaced with directory
1311 * which has the same object number.
1313 if (ZTOV(zp) != NULL &&
1314 ZTOV(zp)->v_type != IFTOVT((mode_t)zp->z_mode)) {
1315 zfs_znode_dmu_fini(zp);
1316 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1320 zp->z_unlinked = (zp->z_links == 0);
1321 zp->z_blksz = doi.doi_data_block_size;
1322 if (zp->z_size != size && ZTOV(zp) != NULL)
1323 vnode_pager_setsize(ZTOV(zp), zp->z_size);
1325 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1331 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1333 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1334 objset_t *os = zfsvfs->z_os;
1335 uint64_t obj = zp->z_id;
1336 uint64_t acl_obj = zfs_external_acl(zp);
1338 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1340 VERIFY(!zp->z_is_sa);
1341 VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1343 VERIFY(0 == dmu_object_free(os, obj, tx));
1344 zfs_znode_dmu_fini(zp);
1345 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1350 zfs_zinactive(znode_t *zp)
1352 vnode_t *vp = ZTOV(zp);
1353 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1354 uint64_t z_id = zp->z_id;
1357 ASSERT(zp->z_sa_hdl);
1360 * Don't allow a zfs_zget() while were trying to release this znode
1362 ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1364 mutex_enter(&zp->z_lock);
1366 if (vp->v_count > 0) {
1368 * If the hold count is greater than zero, somebody has
1369 * obtained a new reference on this znode while we were
1370 * processing it here, so we are done.
1373 mutex_exit(&zp->z_lock);
1374 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1380 * If this was the last reference to a file with no links,
1381 * remove the file from the file system.
1383 if (zp->z_unlinked) {
1384 mutex_exit(&zp->z_lock);
1385 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1386 ASSERT(vp->v_count == 0);
1387 vrecycle(vp, curthread);
1388 vfslocked = VFS_LOCK_GIANT(zfsvfs->z_vfs);
1390 VFS_UNLOCK_GIANT(vfslocked);
1394 mutex_exit(&zp->z_lock);
1395 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1399 zfs_znode_free(znode_t *zp)
1401 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1403 ASSERT(ZTOV(zp) == NULL);
1404 ASSERT(zp->z_sa_hdl == NULL);
1405 mutex_enter(&zfsvfs->z_znodes_lock);
1406 POINTER_INVALIDATE(&zp->z_zfsvfs);
1407 list_remove(&zfsvfs->z_all_znodes, zp);
1408 mutex_exit(&zfsvfs->z_znodes_lock);
1410 if (zp->z_acl_cached) {
1411 zfs_acl_free(zp->z_acl_cached);
1412 zp->z_acl_cached = NULL;
1415 kmem_cache_free(znode_cache, zp);
1417 VFS_RELE(zfsvfs->z_vfs);
1421 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1422 uint64_t ctime[2], boolean_t have_tx)
1428 if (have_tx) { /* will sa_bulk_update happen really soon? */
1429 zp->z_atime_dirty = 0;
1432 zp->z_atime_dirty = 1;
1435 if (flag & AT_ATIME) {
1436 ZFS_TIME_ENCODE(&now, zp->z_atime);
1439 if (flag & AT_MTIME) {
1440 ZFS_TIME_ENCODE(&now, mtime);
1441 if (zp->z_zfsvfs->z_use_fuids) {
1442 zp->z_pflags |= (ZFS_ARCHIVE |
1447 if (flag & AT_CTIME) {
1448 ZFS_TIME_ENCODE(&now, ctime);
1449 if (zp->z_zfsvfs->z_use_fuids)
1450 zp->z_pflags |= ZFS_ARCHIVE;
1455 * Grow the block size for a file.
1457 * IN: zp - znode of file to free data in.
1458 * size - requested block size
1459 * tx - open transaction.
1461 * NOTE: this function assumes that the znode is write locked.
1464 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1469 if (size <= zp->z_blksz)
1472 * If the file size is already greater than the current blocksize,
1473 * we will not grow. If there is more than one block in a file,
1474 * the blocksize cannot change.
1476 if (zp->z_blksz && zp->z_size > zp->z_blksz)
1479 error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1482 if (error == ENOTSUP)
1484 ASSERT3U(error, ==, 0);
1486 /* What blocksize did we actually get? */
1487 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1492 * This is a dummy interface used when pvn_vplist_dirty() should *not*
1493 * be calling back into the fs for a putpage(). E.g.: when truncating
1494 * a file, the pages being "thrown away* don't need to be written out.
1498 zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1499 int flags, cred_t *cr)
1507 * Increase the file length
1509 * IN: zp - znode of file to free data in.
1510 * end - new end-of-file
1512 * RETURN: 0 if success
1513 * error code if 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);
1537 tx = dmu_tx_create(zfsvfs->z_os);
1538 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1539 zfs_sa_upgrade_txholds(tx, zp);
1540 if (end > zp->z_blksz &&
1541 (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1543 * We are growing the file past the current block size.
1545 if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1546 ASSERT(!ISP2(zp->z_blksz));
1547 newblksz = MIN(end, SPA_MAXBLOCKSIZE);
1549 newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1551 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1556 error = dmu_tx_assign(tx, TXG_NOWAIT);
1558 if (error == ERESTART) {
1564 zfs_range_unlock(rl);
1569 zfs_grow_blocksize(zp, newblksz, tx);
1573 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs),
1574 &zp->z_size, sizeof (zp->z_size), tx));
1576 vnode_pager_setsize(ZTOV(zp), end);
1578 zfs_range_unlock(rl);
1586 * Free space in a file.
1588 * IN: zp - znode of file to free data in.
1589 * off - start of section to free.
1590 * len - length of section to free.
1592 * RETURN: 0 if success
1593 * error code if failure
1596 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1598 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1603 * Lock the range being freed.
1605 rl = zfs_range_lock(zp, off, len, RL_WRITER);
1608 * Nothing to do if file already at desired length.
1610 if (off >= zp->z_size) {
1611 zfs_range_unlock(rl);
1615 if (off + len > zp->z_size)
1616 len = zp->z_size - off;
1618 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1622 * In FreeBSD we cannot free block in the middle of a file,
1623 * but only at the end of a file, so this code path should
1626 vnode_pager_setsize(ZTOV(zp), off);
1629 zfs_range_unlock(rl);
1637 * IN: zp - znode of file to free data in.
1638 * end - new end-of-file.
1640 * RETURN: 0 if success
1641 * error code if failure
1644 zfs_trunc(znode_t *zp, uint64_t end)
1646 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1647 vnode_t *vp = ZTOV(zp);
1651 sa_bulk_attr_t bulk[2];
1655 * We will change zp_size, lock the whole file.
1657 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1660 * Nothing to do if file already at desired length.
1662 if (end >= zp->z_size) {
1663 zfs_range_unlock(rl);
1667 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end, -1);
1669 zfs_range_unlock(rl);
1673 tx = dmu_tx_create(zfsvfs->z_os);
1674 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1675 zfs_sa_upgrade_txholds(tx, zp);
1676 error = dmu_tx_assign(tx, TXG_NOWAIT);
1678 if (error == ERESTART) {
1684 zfs_range_unlock(rl);
1689 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
1690 NULL, &zp->z_size, sizeof (zp->z_size));
1693 zp->z_pflags &= ~ZFS_SPARSE;
1694 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1695 NULL, &zp->z_pflags, 8);
1697 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1702 * Clear any mapped pages in the truncated region. This has to
1703 * happen outside of the transaction to avoid the possibility of
1704 * a deadlock with someone trying to push a page that we are
1705 * about to invalidate.
1707 vnode_pager_setsize(vp, end);
1709 zfs_range_unlock(rl);
1715 * Free space in a file
1717 * IN: zp - znode of file to free data in.
1718 * off - start of range
1719 * len - end of range (0 => EOF)
1720 * flag - current file open mode flags.
1721 * log - TRUE if this action should be logged
1723 * RETURN: 0 if success
1724 * error code if failure
1727 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1729 vnode_t *vp = ZTOV(zp);
1731 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1732 zilog_t *zilog = zfsvfs->z_log;
1734 uint64_t mtime[2], ctime[2];
1735 sa_bulk_attr_t bulk[3];
1739 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
1740 sizeof (mode))) != 0)
1743 if (off > zp->z_size) {
1744 error = zfs_extend(zp, off+len);
1745 if (error == 0 && log)
1752 * Check for any locks in the region to be freed.
1755 if (MANDLOCK(vp, (mode_t)mode)) {
1756 uint64_t length = (len ? len : zp->z_size - off);
1757 if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1762 error = zfs_trunc(zp, off);
1764 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1765 off + len > zp->z_size)
1766 error = zfs_extend(zp, off+len);
1771 tx = dmu_tx_create(zfsvfs->z_os);
1772 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1773 zfs_sa_upgrade_txholds(tx, zp);
1774 error = dmu_tx_assign(tx, TXG_NOWAIT);
1776 if (error == ERESTART) {
1785 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
1786 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
1787 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1788 NULL, &zp->z_pflags, 8);
1789 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1790 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1793 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1800 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1803 uint64_t moid, obj, sa_obj, version;
1804 uint64_t sense = ZFS_CASE_SENSITIVE;
1809 znode_t *rootzp = NULL;
1813 zfs_acl_ids_t acl_ids;
1816 * First attempt to create master node.
1819 * In an empty objset, there are no blocks to read and thus
1820 * there can be no i/o errors (which we assert below).
1822 moid = MASTER_NODE_OBJ;
1823 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1824 DMU_OT_NONE, 0, tx);
1828 * Set starting attributes.
1830 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1832 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1833 /* For the moment we expect all zpl props to be uint64_ts */
1837 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1838 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1839 name = nvpair_name(elem);
1840 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1844 error = zap_update(os, moid, name, 8, 1, &val, tx);
1847 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1849 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1852 ASSERT(version != 0);
1853 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1856 * Create zap object used for SA attribute registration
1859 if (version >= ZPL_VERSION_SA) {
1860 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1861 DMU_OT_NONE, 0, tx);
1862 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1868 * Create a delete queue.
1870 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1872 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1876 * Create root znode. Create minimal znode/vnode/zfsvfs
1877 * to allow zfs_mknode to work.
1880 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1881 vattr.va_type = VDIR;
1882 vattr.va_mode = S_IFDIR|0755;
1883 vattr.va_uid = crgetuid(cr);
1884 vattr.va_gid = crgetgid(cr);
1886 bzero(&zfsvfs, sizeof (zfsvfs_t));
1888 rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1889 zfs_znode_cache_constructor(rootzp, NULL, 0);
1890 ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1891 rootzp->z_moved = 0;
1892 rootzp->z_unlinked = 0;
1893 rootzp->z_atime_dirty = 0;
1894 rootzp->z_is_sa = USE_SA(version, os);
1896 vnode.v_type = VDIR;
1897 vnode.v_data = rootzp;
1898 rootzp->z_vnode = &vnode;
1901 zfsvfs.z_parent = &zfsvfs;
1902 zfsvfs.z_version = version;
1903 zfsvfs.z_use_fuids = USE_FUIDS(version, os);
1904 zfsvfs.z_use_sa = USE_SA(version, os);
1905 zfsvfs.z_norm = norm;
1907 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1908 &zfsvfs.z_attr_table);
1913 * Fold case on file systems that are always or sometimes case
1916 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1917 zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER;
1919 mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1920 list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
1921 offsetof(znode_t, z_link_node));
1923 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1924 mutex_init(&zfsvfs.z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1926 rootzp->z_zfsvfs = &zfsvfs;
1927 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1928 cr, NULL, &acl_ids));
1929 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1930 ASSERT3P(zp, ==, rootzp);
1931 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1933 zfs_acl_ids_free(&acl_ids);
1934 POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1936 sa_handle_destroy(rootzp->z_sa_hdl);
1937 rootzp->z_vnode = NULL;
1938 kmem_cache_free(znode_cache, rootzp);
1941 * Create shares directory
1944 error = zfs_create_share_dir(&zfsvfs, tx);
1948 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1949 mutex_destroy(&zfsvfs.z_hold_mtx[i]);
1952 #endif /* _KERNEL */
1955 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1957 uint64_t sa_obj = 0;
1960 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1961 if (error != 0 && error != ENOENT)
1964 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1969 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1970 dmu_buf_t **db, void *tag)
1972 dmu_object_info_t doi;
1975 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
1978 dmu_object_info_from_db(*db, &doi);
1979 if ((doi.doi_bonus_type != DMU_OT_SA &&
1980 doi.doi_bonus_type != DMU_OT_ZNODE) ||
1981 doi.doi_bonus_type == DMU_OT_ZNODE &&
1982 doi.doi_bonus_size < sizeof (znode_phys_t)) {
1983 sa_buf_rele(*db, tag);
1987 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
1989 sa_buf_rele(*db, tag);
1997 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
1999 sa_handle_destroy(hdl);
2000 sa_buf_rele(db, tag);
2004 * Given an object number, return its parent object number and whether
2005 * or not the object is an extended attribute directory.
2008 zfs_obj_to_pobj(sa_handle_t *hdl, sa_attr_type_t *sa_table, uint64_t *pobjp,
2014 sa_bulk_attr_t bulk[3];
2018 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
2019 &parent, sizeof (parent));
2020 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
2021 &pflags, sizeof (pflags));
2022 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2023 &mode, sizeof (mode));
2025 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
2029 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
2035 * Given an object number, return some zpl level statistics
2038 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
2041 sa_bulk_attr_t bulk[4];
2044 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2045 &sb->zs_mode, sizeof (sb->zs_mode));
2046 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
2047 &sb->zs_gen, sizeof (sb->zs_gen));
2048 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
2049 &sb->zs_links, sizeof (sb->zs_links));
2050 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
2051 &sb->zs_ctime, sizeof (sb->zs_ctime));
2053 return (sa_bulk_lookup(hdl, bulk, count));
2057 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
2058 sa_attr_type_t *sa_table, char *buf, int len)
2060 sa_handle_t *sa_hdl;
2061 sa_handle_t *prevhdl = NULL;
2062 dmu_buf_t *prevdb = NULL;
2063 dmu_buf_t *sa_db = NULL;
2064 char *path = buf + len - 1;
2072 char component[MAXNAMELEN + 2];
2077 zfs_release_sa_handle(prevhdl, prevdb, FTAG);
2079 if ((error = zfs_obj_to_pobj(sa_hdl, sa_table, &pobj,
2080 &is_xattrdir)) != 0)
2091 (void) sprintf(component + 1, "<xattrdir>");
2093 error = zap_value_search(osp, pobj, obj,
2094 ZFS_DIRENT_OBJ(-1ULL), component + 1);
2099 complen = strlen(component);
2101 ASSERT(path >= buf);
2102 bcopy(component, path, complen);
2105 if (sa_hdl != hdl) {
2109 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
2117 if (sa_hdl != NULL && sa_hdl != hdl) {
2118 ASSERT(sa_db != NULL);
2119 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2123 (void) memmove(buf, path, buf + len - path);
2129 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
2131 sa_attr_type_t *sa_table;
2136 error = zfs_sa_setup(osp, &sa_table);
2140 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2144 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2146 zfs_release_sa_handle(hdl, db, FTAG);
2151 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
2154 char *path = buf + len - 1;
2155 sa_attr_type_t *sa_table;
2162 error = zfs_sa_setup(osp, &sa_table);
2166 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2170 error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
2172 zfs_release_sa_handle(hdl, db, FTAG);
2176 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2178 zfs_release_sa_handle(hdl, db, FTAG);