zfs_znode.c revision 243520
11553Srgrimes/*
21553Srgrimes * CDDL HEADER START
31553Srgrimes *
41553Srgrimes * The contents of this file are subject to the terms of the
51553Srgrimes * Common Development and Distribution License (the "License").
61553Srgrimes * You may not use this file except in compliance with the License.
71553Srgrimes *
81553Srgrimes * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91553Srgrimes * or http://www.opensolaris.org/os/licensing.
101553Srgrimes * See the License for the specific language governing permissions
111553Srgrimes * and limitations under the License.
121553Srgrimes *
131553Srgrimes * When distributing Covered Code, include this CDDL HEADER in each
141553Srgrimes * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151553Srgrimes * If applicable, add the following below this CDDL HEADER, with the
161553Srgrimes * fields enclosed by brackets "[]" replaced with your own identifying
171553Srgrimes * information: Portions Copyright [yyyy] [name of copyright owner]
181553Srgrimes *
191553Srgrimes * CDDL HEADER END
201553Srgrimes */
211553Srgrimes/*
221553Srgrimes * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
231553Srgrimes * Copyright (c) 2012 by Delphix. All rights reserved.
241553Srgrimes */
251553Srgrimes
261553Srgrimes/* Portions Copyright 2007 Jeremy Teo */
271553Srgrimes/* Portions Copyright 2011 Martin Matuska <mm@FreeBSD.org> */
281553Srgrimes
2950479Speter#ifdef _KERNEL
301553Srgrimes#include <sys/types.h>
31240409Seadler#include <sys/param.h>
321553Srgrimes#include <sys/time.h>
331553Srgrimes#include <sys/systm.h>
341553Srgrimes#include <sys/sysmacros.h>
351553Srgrimes#include <sys/resource.h>
361553Srgrimes#include <sys/mntent.h>
371553Srgrimes#include <sys/u8_textprep.h>
381553Srgrimes#include <sys/dsl_dataset.h>
391553Srgrimes#include <sys/vfs.h>
401553Srgrimes#include <sys/vnode.h>
4125276Sjmg#include <sys/file.h>
421553Srgrimes#include <sys/kmem.h>
431553Srgrimes#include <sys/errno.h>
441553Srgrimes#include <sys/unistd.h>
455367Sjkh#include <sys/atomic.h>
465367Sjkh#include <sys/zfs_dir.h>
475367Sjkh#include <sys/zfs_acl.h>
4863795Sdwmalone#include <sys/zfs_ioctl.h>
4963795Sdwmalone#include <sys/zfs_rlock.h>
50155954Sjulian#include <sys/zfs_fuid.h>
515367Sjkh#include <sys/dnode.h>
521553Srgrimes#include <sys/fs/zfs.h>
531553Srgrimes#include <sys/kidmap.h>
541553Srgrimes#endif /* _KERNEL */
551553Srgrimes
561553Srgrimes#include <sys/dmu.h>
5756485Scharnier#include <sys/refcount.h>
581553Srgrimes#include <sys/stat.h>
591553Srgrimes#include <sys/zap.h>
601553Srgrimes#include <sys/zfs_znode.h>
611553Srgrimes#include <sys/sa.h>
621553Srgrimes#include <sys/zfs_sa.h>
6375169Sru#include <sys/zfs_stat.h>
641553Srgrimes#include <sys/refcount.h>
6579755Sdd
6682470Scjc#include "zfs_prop.h"
6741498Sjkh#include "zfs_comutil.h"
68108247Strhodes
69131500Sru/* Used by fstat(1). */
7082470ScjcSYSCTL_INT(_debug_sizeof, OID_AUTO, znode, CTLFLAG_RD, 0, sizeof(znode_t),
7157673Ssheldonh    "sizeof(znode_t)");
7279755Sdd
73108247Strhodes/*
7482470Scjc * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
75131500Sru * turned on when DEBUG is also defined.
7641498Sjkh */
771553Srgrimes#ifdef	DEBUG
7882470Scjc#define	ZNODE_STATS
791553Srgrimes#endif	/* DEBUG */
801553Srgrimes
8125276Sjmg#ifdef	ZNODE_STATS
8225276Sjmg#define	ZNODE_STAT_ADD(stat)			((stat)++)
8337820Sphk#else
84107312Sru#define	ZNODE_STAT_ADD(stat)			/* nothing */
8525276Sjmg#endif	/* ZNODE_STATS */
861553Srgrimes
871553Srgrimes/*
881553Srgrimes * Functions needed for userland (ie: libzpool) are not put under
891553Srgrimes * #ifdef_KERNEL; the rest of the functions have dependencies
901553Srgrimes * (such as VFS logic) that will not compile easily in userland.
911553Srgrimes */
921553Srgrimes#ifdef _KERNEL
931553Srgrimes/*
941553Srgrimes * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to
951553Srgrimes * be freed before it can be safely accessed.
961553Srgrimes */
97163255Strhodeskrwlock_t zfsvfs_lock;
98163255Strhodes
99163255Strhodesstatic kmem_cache_t *znode_cache = NULL;
100163255Strhodes
101163255Strhodes/*ARGSUSED*/
102163255Strhodesstatic void
103163255Strhodesznode_evict_error(dmu_buf_t *dbuf, void *user_ptr)
104108247Strhodes{
1051553Srgrimes	/*
1061553Srgrimes	 * We should never drop all dbuf refs without first clearing
1071553Srgrimes	 * the eviction callback.
1081553Srgrimes	 */
1091553Srgrimes	panic("evicting znode %p\n", user_ptr);
1101553Srgrimes}
1111553Srgrimes
1121553Srgrimesextern struct vop_vector zfs_vnodeops;
1131553Srgrimesextern struct vop_vector zfs_fifoops;
11437820Sphkextern struct vop_vector zfs_shareops;
11537820Sphk
116108247Strhodes/*
11737820Sphk * XXX: We cannot use this function as a cache constructor, because
11837820Sphk *      there is one global cache for all file systems and we need
11971898Sru *      to pass vfsp here, which is not possible, because argument
12037820Sphk *      'cdrarg' is defined at kmem_cache_create() time.
12137820Sphk */
122108247Strhodes/*ARGSUSED*/
123108247Strhodesstatic int
1241553Srgrimeszfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
12537820Sphk{
126102940Sdwmalone	znode_t *zp = buf;
127107312Sru	vnode_t *vp;
128102940Sdwmalone	vfs_t *vfsp = arg;
129102940Sdwmalone	int error;
130102940Sdwmalone
131102940Sdwmalone	POINTER_INVALIDATE(&zp->z_zfsvfs);
132102940Sdwmalone	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
133102940Sdwmalone
134102940Sdwmalone	if (vfsp != NULL) {
13537820Sphk		error = getnewvnode("zfs", vfsp, &zfs_vnodeops, &vp);
13637820Sphk		if (error != 0 && (kmflags & KM_NOSLEEP))
13737820Sphk			return (-1);
1381553Srgrimes		ASSERT(error == 0);
139163255Strhodes		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
140186440Strhodes		zp->z_vnode = vp;
141163255Strhodes		vp->v_data = (caddr_t)zp;
142163255Strhodes		VN_LOCK_AREC(vp);
143108247Strhodes		VN_LOCK_ASHARE(vp);
1441553Srgrimes	} else {
14525276Sjmg		zp->z_vnode = NULL;
1461553Srgrimes	}
14756485Scharnier
1481553Srgrimes	list_link_init(&zp->z_link_node);
1491553Srgrimes
15079755Sdd	mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
15163795Sdwmalone	rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
1525367Sjkh	rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
15363795Sdwmalone	mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
15463795Sdwmalone
15563795Sdwmalone	mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
15663795Sdwmalone	avl_create(&zp->z_range_avl, zfs_range_compare,
15763795Sdwmalone	    sizeof (rl_t), offsetof(rl_t, r_node));
15863795Sdwmalone
15963795Sdwmalone	zp->z_dirlocks = NULL;
160108247Strhodes	zp->z_acl_cached = NULL;
16163795Sdwmalone	zp->z_moved = 0;
16263795Sdwmalone	return (0);
16363795Sdwmalone}
16463795Sdwmalone
16563795Sdwmalone/*ARGSUSED*/
16663795Sdwmalonestatic void
167108247Strhodeszfs_znode_cache_destructor(void *buf, void *arg)
16863795Sdwmalone{
16963795Sdwmalone	znode_t *zp = buf;
17063795Sdwmalone
17163795Sdwmalone	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
17263795Sdwmalone	ASSERT(ZTOV(zp) == NULL);
17363795Sdwmalone	vn_free(ZTOV(zp));
17463795Sdwmalone	ASSERT(!list_link_active(&zp->z_link_node));
17563795Sdwmalone	mutex_destroy(&zp->z_lock);
17663795Sdwmalone	rw_destroy(&zp->z_parent_lock);
17763795Sdwmalone	rw_destroy(&zp->z_name_lock);
1785367Sjkh	mutex_destroy(&zp->z_acl_lock);
17982470Scjc	avl_destroy(&zp->z_range_avl);
18056485Scharnier	mutex_destroy(&zp->z_range_lock);
18163795Sdwmalone
18263795Sdwmalone	ASSERT(zp->z_dirlocks == NULL);
18363795Sdwmalone	ASSERT(zp->z_acl_cached == NULL);
18463795Sdwmalone}
18563795Sdwmalone
18663795Sdwmalone#ifdef	ZNODE_STATS
18763795Sdwmalonestatic struct {
18863795Sdwmalone	uint64_t zms_zfsvfs_invalid;
18963795Sdwmalone	uint64_t zms_zfsvfs_recheck1;
190103855Sdcs	uint64_t zms_zfsvfs_unmounted;
191103855Sdcs	uint64_t zms_zfsvfs_recheck2;
192103855Sdcs	uint64_t zms_obj_held;
193103855Sdcs	uint64_t zms_vnode_locked;
194103855Sdcs	uint64_t zms_not_only_dnlc;
195103855Sdcs} znode_move_stats;
196103855Sdcs#endif	/* ZNODE_STATS */
197103855Sdcs
198103855Sdcs#ifdef sun
199103855Sdcsstatic void
200103855Sdcszfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
201110776Sthomas{
202110776Sthomas	vnode_t *vp;
203110776Sthomas
204110776Sthomas	/* Copy fields. */
205110776Sthomas	nzp->z_zfsvfs = ozp->z_zfsvfs;
206110776Sthomas
207110776Sthomas	/* Swap vnodes. */
208110776Sthomas	vp = nzp->z_vnode;
209110776Sthomas	nzp->z_vnode = ozp->z_vnode;
21063795Sdwmalone	ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
21163795Sdwmalone	ZTOV(ozp)->v_data = ozp;
21263795Sdwmalone	ZTOV(nzp)->v_data = nzp;
21363795Sdwmalone
21463795Sdwmalone	nzp->z_id = ozp->z_id;
21563795Sdwmalone	ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
216108247Strhodes	ASSERT(avl_numnodes(&ozp->z_range_avl) == 0);
21763795Sdwmalone	nzp->z_unlinked = ozp->z_unlinked;
218108247Strhodes	nzp->z_atime_dirty = ozp->z_atime_dirty;
21963795Sdwmalone	nzp->z_zn_prefetch = ozp->z_zn_prefetch;
22063795Sdwmalone	nzp->z_blksz = ozp->z_blksz;
22163795Sdwmalone	nzp->z_seq = ozp->z_seq;
22263795Sdwmalone	nzp->z_mapcnt = ozp->z_mapcnt;
22363795Sdwmalone	nzp->z_gen = ozp->z_gen;
22463795Sdwmalone	nzp->z_sync_cnt = ozp->z_sync_cnt;
22563795Sdwmalone	nzp->z_is_sa = ozp->z_is_sa;
22663795Sdwmalone	nzp->z_sa_hdl = ozp->z_sa_hdl;
22763795Sdwmalone	bcopy(ozp->z_atime, nzp->z_atime, sizeof (uint64_t) * 2);
22863795Sdwmalone	nzp->z_links = ozp->z_links;
229148580Skeramida	nzp->z_size = ozp->z_size;
230110776Sthomas	nzp->z_pflags = ozp->z_pflags;
231110776Sthomas	nzp->z_uid = ozp->z_uid;
23263795Sdwmalone	nzp->z_gid = ozp->z_gid;
23363795Sdwmalone	nzp->z_mode = ozp->z_mode;
23463795Sdwmalone
23563795Sdwmalone	/*
23663795Sdwmalone	 * Since this is just an idle znode and kmem is already dealing with
23763795Sdwmalone	 * memory pressure, release any cached ACL.
2385367Sjkh	 */
2391553Srgrimes	if (ozp->z_acl_cached) {
2401553Srgrimes		zfs_acl_free(ozp->z_acl_cached);
241108247Strhodes		ozp->z_acl_cached = NULL;
2421553Srgrimes	}
2431553Srgrimes
2441553Srgrimes	sa_set_userp(nzp->z_sa_hdl, nzp);
24557673Ssheldonh
246141851Sru	/*
2475367Sjkh	 * Invalidate the original znode by clearing fields that provide a
2485367Sjkh	 * pointer back to the znode. Set the low bit of the vfs pointer to
2495367Sjkh	 * ensure that zfs_znode_move() recognizes the znode as invalid in any
25057673Ssheldonh	 * subsequent callback.
25157673Ssheldonh	 */
2525367Sjkh	ozp->z_sa_hdl = NULL;
2535367Sjkh	POINTER_INVALIDATE(&ozp->z_zfsvfs);
254141846Sru
2551553Srgrimes	/*
2561553Srgrimes	 * Mark the znode.
2571553Srgrimes	 */
2581553Srgrimes	nzp->z_moved = 1;
2591553Srgrimes	ozp->z_moved = (uint8_t)-1;
2601553Srgrimes}
2615367Sjkh
2625367Sjkh/*ARGSUSED*/
2631553Srgrimesstatic kmem_cbrc_t
2641553Srgrimeszfs_znode_move(void *buf, void *newbuf, size_t size, void *arg)
2651553Srgrimes{
2661553Srgrimes	znode_t *ozp = buf, *nzp = newbuf;
2671553Srgrimes	zfsvfs_t *zfsvfs;
2681553Srgrimes	vnode_t *vp;
2691553Srgrimes
2701553Srgrimes	/*
27125276Sjmg	 * The znode is on the file system's list of known znodes if the vfs
27225276Sjmg	 * pointer is valid. We set the low bit of the vfs pointer when freeing
27325276Sjmg	 * the znode to invalidate it, and the memory patterns written by kmem
2741553Srgrimes	 * (baddcafe and deadbeef) set at least one of the two low bits. A newly
2751553Srgrimes	 * created znode sets the vfs pointer last of all to indicate that the
2761553Srgrimes	 * znode is known and in a valid state to be moved by this function.
2771553Srgrimes	 */
2781553Srgrimes	zfsvfs = ozp->z_zfsvfs;
2791553Srgrimes	if (!POINTER_IS_VALID(zfsvfs)) {
2801553Srgrimes		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid);
2811553Srgrimes		return (KMEM_CBRC_DONT_KNOW);
28225276Sjmg	}
28325276Sjmg
28425276Sjmg	/*
2851553Srgrimes	 * Close a small window in which it's possible that the filesystem could
28625276Sjmg	 * be unmounted and freed, and zfsvfs, though valid in the previous
28725276Sjmg	 * statement, could point to unrelated memory by the time we try to
28825276Sjmg	 * prevent the filesystem from being unmounted.
289108247Strhodes	 */
2905367Sjkh	rw_enter(&zfsvfs_lock, RW_WRITER);
291108247Strhodes	if (zfsvfs != ozp->z_zfsvfs) {
2921553Srgrimes		rw_exit(&zfsvfs_lock);
2935367Sjkh		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1);
2941553Srgrimes		return (KMEM_CBRC_DONT_KNOW);
2951553Srgrimes	}
2961553Srgrimes
29725276Sjmg	/*
29825276Sjmg	 * If the znode is still valid, then so is the file system. We know that
29925276Sjmg	 * no valid file system can be freed while we hold zfsvfs_lock, so we
30025276Sjmg	 * can safely ensure that the filesystem is not and will not be
3011553Srgrimes	 * unmounted. The next statement is equivalent to ZFS_ENTER().
3021553Srgrimes	 */
3031553Srgrimes	rrw_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG);
3041553Srgrimes	if (zfsvfs->z_unmounted) {
3051553Srgrimes		ZFS_EXIT(zfsvfs);
3061553Srgrimes		rw_exit(&zfsvfs_lock);
3071553Srgrimes		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
3081553Srgrimes		return (KMEM_CBRC_DONT_KNOW);
30925276Sjmg	}
31025276Sjmg	rw_exit(&zfsvfs_lock);
3111553Srgrimes
3121553Srgrimes	mutex_enter(&zfsvfs->z_znodes_lock);
3131553Srgrimes	/*
3141553Srgrimes	 * Recheck the vfs pointer in case the znode was removed just before
3151553Srgrimes	 * acquiring the lock.
3161553Srgrimes	 */
3171553Srgrimes	if (zfsvfs != ozp->z_zfsvfs) {
31822984Sjoerg		mutex_exit(&zfsvfs->z_znodes_lock);
3191553Srgrimes		ZFS_EXIT(zfsvfs);
3201553Srgrimes		ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2);
3211553Srgrimes		return (KMEM_CBRC_DONT_KNOW);
3221553Srgrimes	}
323129865Sdwmalone
324129865Sdwmalone	/*
325129865Sdwmalone	 * At this point we know that as long as we hold z_znodes_lock, the
326129865Sdwmalone	 * znode cannot be freed and fields within the znode can be safely
327129865Sdwmalone	 * accessed. Now, prevent a race with zfs_zget().
328129865Sdwmalone	 */
329129865Sdwmalone	if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) {
330129865Sdwmalone		mutex_exit(&zfsvfs->z_znodes_lock);
331129865Sdwmalone		ZFS_EXIT(zfsvfs);
332129865Sdwmalone		ZNODE_STAT_ADD(znode_move_stats.zms_obj_held);
333129865Sdwmalone		return (KMEM_CBRC_LATER);
334129865Sdwmalone	}
335129865Sdwmalone
336129865Sdwmalone	vp = ZTOV(ozp);
337129865Sdwmalone	if (mutex_tryenter(&vp->v_lock) == 0) {
338129865Sdwmalone		ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
3391553Srgrimes		mutex_exit(&zfsvfs->z_znodes_lock);
34025276Sjmg		ZFS_EXIT(zfsvfs);
34125276Sjmg		ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
34225276Sjmg		return (KMEM_CBRC_LATER);
3431553Srgrimes	}
34456485Scharnier
3451553Srgrimes	/* Only move znodes that are referenced _only_ by the DNLC. */
346157821Sjulian	if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
347162806Sru		mutex_exit(&vp->v_lock);
348162806Sru		ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
349162806Sru		mutex_exit(&zfsvfs->z_znodes_lock);
350240409Seadler		ZFS_EXIT(zfsvfs);
351240409Seadler		ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc);
352240409Seadler		return (KMEM_CBRC_LATER);
353240409Seadler	}
354240409Seadler
355240409Seadler	/*
356240409Seadler	 * The znode is known and in a valid state to move. We're holding the
357240409Seadler	 * locks needed to execute the critical section.
3581553Srgrimes	 */
3591553Srgrimes	zfs_znode_move_impl(ozp, nzp);
3601553Srgrimes	mutex_exit(&vp->v_lock);
3611553Srgrimes	ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
3621553Srgrimes
3631553Srgrimes	list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
3641553Srgrimes	mutex_exit(&zfsvfs->z_znodes_lock);
36522984Sjoerg	ZFS_EXIT(zfsvfs);
36625276Sjmg
36725276Sjmg	return (KMEM_CBRC_YES);
36825276Sjmg}
369131500Sru#endif /* sun */
370131500Sru
37182470Scjcvoid
37222984Sjoergzfs_znode_init(void)
373131500Sru{
374131500Sru	/*
37522984Sjoerg	 * Initialize zcache
37622984Sjoerg	 */
37722984Sjoerg	rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL);
378131531Sru	ASSERT(znode_cache == NULL);
379131531Sru	znode_cache = kmem_cache_create("zfs_znode_cache",
38022984Sjoerg	    sizeof (znode_t), 0, /* zfs_znode_cache_constructor */ NULL,
38122984Sjoerg	    zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
38222984Sjoerg	kmem_cache_set_move(znode_cache, zfs_znode_move);
38322984Sjoerg}
38422984Sjoerg
38522984Sjoergvoid
38622984Sjoergzfs_znode_fini(void)
38782470Scjc{
388131500Sru#ifdef sun
389141846Sru	/*
39023004Smpp	 * Cleanup vfs & vnode ops
39122984Sjoerg	 */
39229376Sjoerg	zfs_remove_op_tables();
39322984Sjoerg#endif	/* sun */
39423004Smpp
395131500Sru	/*
396131500Sru	 * Cleanup zcache
397131500Sru	 */
39829376Sjoerg	if (znode_cache)
39929376Sjoerg		kmem_cache_destroy(znode_cache);
40029376Sjoerg	znode_cache = NULL;
401131500Sru	rw_destroy(&zfsvfs_lock);
402131500Sru}
40322984Sjoerg
404141851Sru#ifdef sun
40522984Sjoergstruct vnodeops *zfs_dvnodeops;
40622984Sjoergstruct vnodeops *zfs_fvnodeops;
40722984Sjoergstruct vnodeops *zfs_symvnodeops;
408131500Srustruct vnodeops *zfs_xdvnodeops;
409131500Srustruct vnodeops *zfs_evnodeops;
41022984Sjoergstruct vnodeops *zfs_sharevnodeops;
41123004Smpp
4121553Srgrimesvoid
4131553Srgrimeszfs_remove_op_tables()
41425276Sjmg{
41525276Sjmg	/*
4161553Srgrimes	 * Remove vfs ops
417180380Ssobomax	 */
418180380Ssobomax	ASSERT(zfsfstype);
419180380Ssobomax	(void) vfs_freevfsops_by_type(zfsfstype);
420180380Ssobomax	zfsfstype = 0;
421180380Ssobomax
422180380Ssobomax	/*
423180380Ssobomax	 * Remove vnode ops
424180380Ssobomax	 */
425180380Ssobomax	if (zfs_dvnodeops)
426180380Ssobomax		vn_freevnodeops(zfs_dvnodeops);
427180380Ssobomax	if (zfs_fvnodeops)
428180380Ssobomax		vn_freevnodeops(zfs_fvnodeops);
429180380Ssobomax	if (zfs_symvnodeops)
430180380Ssobomax		vn_freevnodeops(zfs_symvnodeops);
431140442Sru	if (zfs_xdvnodeops)
432140442Sru		vn_freevnodeops(zfs_xdvnodeops);
433140442Sru	if (zfs_evnodeops)
434140442Sru		vn_freevnodeops(zfs_evnodeops);
435140442Sru	if (zfs_sharevnodeops)
436140442Sru		vn_freevnodeops(zfs_sharevnodeops);
437140442Sru
438140442Sru	zfs_dvnodeops = NULL;
439140442Sru	zfs_fvnodeops = NULL;
440140442Sru	zfs_symvnodeops = NULL;
441140442Sru	zfs_xdvnodeops = NULL;
442140442Sru	zfs_evnodeops = NULL;
443140442Sru	zfs_sharevnodeops = NULL;
444140442Sru}
445140442Sru
446140442Sruextern const fs_operation_def_t zfs_dvnodeops_template[];
447140442Sruextern const fs_operation_def_t zfs_fvnodeops_template[];
448140442Sruextern const fs_operation_def_t zfs_xdvnodeops_template[];
449140442Sruextern const fs_operation_def_t zfs_symvnodeops_template[];
4501553Srgrimesextern const fs_operation_def_t zfs_evnodeops_template[];
4511553Srgrimesextern const fs_operation_def_t zfs_sharevnodeops_template[];
4521553Srgrimes
4531553Srgrimesint
454108247Strhodeszfs_create_op_tables()
4551553Srgrimes{
4561553Srgrimes	int error;
457186440Strhodes
4581553Srgrimes	/*
4591553Srgrimes	 * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
4601553Srgrimes	 * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
4611553Srgrimes	 * In this case we just return as the ops vectors are already set up.
4621553Srgrimes	 */
46337820Sphk	if (zfs_dvnodeops)
46437820Sphk		return (0);
46537820Sphk
4661553Srgrimes	error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template,
4671553Srgrimes	    &zfs_dvnodeops);
4681553Srgrimes	if (error)
4691553Srgrimes		return (error);
4701553Srgrimes
4711553Srgrimes	error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template,
4721553Srgrimes	    &zfs_fvnodeops);
4731553Srgrimes	if (error)
4741553Srgrimes		return (error);
4751553Srgrimes
4761553Srgrimes	error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template,
4771553Srgrimes	    &zfs_symvnodeops);
4781553Srgrimes	if (error)
4791553Srgrimes		return (error);
4801553Srgrimes
4811553Srgrimes	error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template,
4821553Srgrimes	    &zfs_xdvnodeops);
4835367Sjkh	if (error)
48422984Sjoerg		return (error);
48522984Sjoerg
48622984Sjoerg	error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template,
48750126Sgreen	    &zfs_evnodeops);
48850126Sgreen	if (error)
48974875Scwt		return (error);
49074875Scwt
49174875Scwt	error = vn_make_ops(MNTTYPE_ZFS, zfs_sharevnodeops_template,
492129865Sdwmalone	    &zfs_sharevnodeops);
493194037Savg
494194037Savg	return (error);
495194037Savg}
496194037Savg#endif	/* sun */
497129865Sdwmalone
498129865Sdwmaloneint
499129865Sdwmalonezfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
5001553Srgrimes{
501140442Sru	zfs_acl_ids_t acl_ids;
502140442Sru	vattr_t vattr;
50367249Sdwmalone	znode_t *sharezp;
5041553Srgrimes	vnode_t *vp, vnode;
50556485Scharnier	znode_t *zp;
50656485Scharnier	int error;
50756485Scharnier
50825276Sjmg	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
50925276Sjmg	vattr.va_type = VDIR;
51025276Sjmg	vattr.va_mode = S_IFDIR|0555;
51125276Sjmg	vattr.va_uid = crgetuid(kcred);
51225276Sjmg	vattr.va_gid = crgetgid(kcred);
51325276Sjmg
51425276Sjmg	sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP);
51525276Sjmg	zfs_znode_cache_constructor(sharezp, zfsvfs->z_parent->z_vfs, 0);
51625276Sjmg	ASSERT(!POINTER_IS_VALID(sharezp->z_zfsvfs));
51730184Sjoerg	sharezp->z_moved = 0;
51830184Sjoerg	sharezp->z_unlinked = 0;
519131500Sru	sharezp->z_atime_dirty = 0;
520131500Sru	sharezp->z_zfsvfs = zfsvfs;
52130184Sjoerg	sharezp->z_is_sa = zfsvfs->z_use_sa;
522131500Sru
523131500Sru	sharezp->z_vnode = &vnode;
52430184Sjoerg	vnode.v_data = sharezp;
525107788Sru
526	vp = ZTOV(sharezp);
527	vp->v_type = VDIR;
528
529	VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
530	    kcred, NULL, &acl_ids));
531	zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
532	ASSERT3P(zp, ==, sharezp);
533	POINTER_INVALIDATE(&sharezp->z_zfsvfs);
534	error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
535	    ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
536	zfsvfs->z_shares_dir = sharezp->z_id;
537
538	zfs_acl_ids_free(&acl_ids);
539	ZTOV(sharezp)->v_data = NULL;
540	ZTOV(sharezp)->v_count = 0;
541	ZTOV(sharezp)->v_holdcnt = 0;
542	zp->z_vnode = NULL;
543	sa_handle_destroy(sharezp->z_sa_hdl);
544	sharezp->z_vnode = NULL;
545	kmem_cache_free(znode_cache, sharezp);
546
547	return (error);
548}
549
550/*
551 * define a couple of values we need available
552 * for both 64 and 32 bit environments.
553 */
554#ifndef NBITSMINOR64
555#define	NBITSMINOR64	32
556#endif
557#ifndef MAXMAJ64
558#define	MAXMAJ64	0xffffffffUL
559#endif
560#ifndef	MAXMIN64
561#define	MAXMIN64	0xffffffffUL
562#endif
563
564/*
565 * Create special expldev for ZFS private use.
566 * Can't use standard expldev since it doesn't do
567 * what we want.  The standard expldev() takes a
568 * dev32_t in LP64 and expands it to a long dev_t.
569 * We need an interface that takes a dev32_t in ILP32
570 * and expands it to a long dev_t.
571 */
572static uint64_t
573zfs_expldev(dev_t dev)
574{
575	return (((uint64_t)major(dev) << NBITSMINOR64) | minor(dev));
576}
577/*
578 * Special cmpldev for ZFS private use.
579 * Can't use standard cmpldev since it takes
580 * a long dev_t and compresses it to dev32_t in
581 * LP64.  We need to do a compaction of a long dev_t
582 * to a dev32_t in ILP32.
583 */
584dev_t
585zfs_cmpldev(uint64_t dev)
586{
587	return (makedev((dev >> NBITSMINOR64), (dev & MAXMIN64)));
588}
589
590static void
591zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp,
592    dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
593{
594	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
595	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
596
597	mutex_enter(&zp->z_lock);
598
599	ASSERT(zp->z_sa_hdl == NULL);
600	ASSERT(zp->z_acl_cached == NULL);
601	if (sa_hdl == NULL) {
602		VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, zp,
603		    SA_HDL_SHARED, &zp->z_sa_hdl));
604	} else {
605		zp->z_sa_hdl = sa_hdl;
606		sa_set_userp(sa_hdl, zp);
607	}
608
609	zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
610
611	/*
612	 * Slap on VROOT if we are the root znode
613	 */
614	if (zp->z_id == zfsvfs->z_root)
615		ZTOV(zp)->v_flag |= VROOT;
616
617	mutex_exit(&zp->z_lock);
618	vn_exists(ZTOV(zp));
619}
620
621void
622zfs_znode_dmu_fini(znode_t *zp)
623{
624	ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
625	    zp->z_unlinked ||
626	    RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
627
628	sa_handle_destroy(zp->z_sa_hdl);
629	zp->z_sa_hdl = NULL;
630}
631
632static void
633zfs_vnode_forget(vnode_t *vp)
634{
635
636	/* copied from insmntque_stddtr */
637	vp->v_data = NULL;
638	vp->v_op = &dead_vnodeops;
639	vgone(vp);
640	vput(vp);
641}
642
643/*
644 * Construct a new znode/vnode and intialize.
645 *
646 * This does not do a call to dmu_set_user() that is
647 * up to the caller to do, in case you don't want to
648 * return the znode
649 */
650static znode_t *
651zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz,
652    dmu_object_type_t obj_type, sa_handle_t *hdl)
653{
654	znode_t	*zp;
655	vnode_t *vp;
656	uint64_t mode;
657	uint64_t parent;
658	sa_bulk_attr_t bulk[9];
659	int count = 0;
660
661	zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
662	zfs_znode_cache_constructor(zp, zfsvfs->z_parent->z_vfs, 0);
663
664	ASSERT(zp->z_dirlocks == NULL);
665	ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
666	zp->z_moved = 0;
667
668	/*
669	 * Defer setting z_zfsvfs until the znode is ready to be a candidate for
670	 * the zfs_znode_move() callback.
671	 */
672	zp->z_sa_hdl = NULL;
673	zp->z_unlinked = 0;
674	zp->z_atime_dirty = 0;
675	zp->z_mapcnt = 0;
676	zp->z_id = db->db_object;
677	zp->z_blksz = blksz;
678	zp->z_seq = 0x7A4653;
679	zp->z_sync_cnt = 0;
680
681	vp = ZTOV(zp);
682
683	zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl);
684
685	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
686	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8);
687	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
688	    &zp->z_size, 8);
689	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
690	    &zp->z_links, 8);
691	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
692	    &zp->z_pflags, 8);
693	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
694	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
695	    &zp->z_atime, 16);
696	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
697	    &zp->z_uid, 8);
698	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
699	    &zp->z_gid, 8);
700
701	if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
702		if (hdl == NULL)
703			sa_handle_destroy(zp->z_sa_hdl);
704		zfs_vnode_forget(vp);
705		zp->z_vnode = NULL;
706		kmem_cache_free(znode_cache, zp);
707		return (NULL);
708	}
709
710	zp->z_mode = mode;
711
712	vp->v_type = IFTOVT((mode_t)mode);
713
714	switch (vp->v_type) {
715	case VDIR:
716		zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
717		break;
718#ifdef sun
719	case VBLK:
720	case VCHR:
721		{
722			uint64_t rdev;
723			VERIFY(sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zfsvfs),
724			    &rdev, sizeof (rdev)) == 0);
725
726			vp->v_rdev = zfs_cmpldev(rdev);
727		}
728		break;
729#endif	/* sun */
730	case VFIFO:
731#ifdef sun
732	case VSOCK:
733	case VDOOR:
734#endif	/* sun */
735		vp->v_op = &zfs_fifoops;
736		break;
737	case VREG:
738		if (parent == zfsvfs->z_shares_dir) {
739			ASSERT(zp->z_uid == 0 && zp->z_gid == 0);
740			vp->v_op = &zfs_shareops;
741		}
742		break;
743#ifdef sun
744	case VLNK:
745		vn_setops(vp, zfs_symvnodeops);
746		break;
747	default:
748		vn_setops(vp, zfs_evnodeops);
749		break;
750#endif	/* sun */
751	}
752	if (vp->v_type != VFIFO)
753		VN_LOCK_ASHARE(vp);
754
755	mutex_enter(&zfsvfs->z_znodes_lock);
756	list_insert_tail(&zfsvfs->z_all_znodes, zp);
757	membar_producer();
758	/*
759	 * Everything else must be valid before assigning z_zfsvfs makes the
760	 * znode eligible for zfs_znode_move().
761	 */
762	zp->z_zfsvfs = zfsvfs;
763	mutex_exit(&zfsvfs->z_znodes_lock);
764
765	VFS_HOLD(zfsvfs->z_vfs);
766	return (zp);
767}
768
769static uint64_t empty_xattr;
770static uint64_t pad[4];
771static zfs_acl_phys_t acl_phys;
772/*
773 * Create a new DMU object to hold a zfs znode.
774 *
775 *	IN:	dzp	- parent directory for new znode
776 *		vap	- file attributes for new znode
777 *		tx	- dmu transaction id for zap operations
778 *		cr	- credentials of caller
779 *		flag	- flags:
780 *			  IS_ROOT_NODE	- new object will be root
781 *			  IS_XATTR	- new object is an attribute
782 *		bonuslen - length of bonus buffer
783 *		setaclp  - File/Dir initial ACL
784 *		fuidp	 - Tracks fuid allocation.
785 *
786 *	OUT:	zpp	- allocated znode
787 *
788 */
789void
790zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
791    uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
792{
793	uint64_t	crtime[2], atime[2], mtime[2], ctime[2];
794	uint64_t	mode, size, links, parent, pflags;
795	uint64_t	dzp_pflags = 0;
796	uint64_t	rdev = 0;
797	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
798	dmu_buf_t	*db;
799	timestruc_t	now;
800	uint64_t	gen, obj;
801	int		err;
802	int		bonuslen;
803	sa_handle_t	*sa_hdl;
804	dmu_object_type_t obj_type;
805	sa_bulk_attr_t	sa_attrs[ZPL_END];
806	int		cnt = 0;
807	zfs_acl_locator_cb_t locate = { 0 };
808
809	ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
810
811	if (zfsvfs->z_replay) {
812		obj = vap->va_nodeid;
813		now = vap->va_ctime;		/* see zfs_replay_create() */
814		gen = vap->va_nblocks;		/* ditto */
815	} else {
816		obj = 0;
817		gethrestime(&now);
818		gen = dmu_tx_get_txg(tx);
819	}
820
821	obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
822	bonuslen = (obj_type == DMU_OT_SA) ?
823	    DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
824
825	/*
826	 * Create a new DMU object.
827	 */
828	/*
829	 * There's currently no mechanism for pre-reading the blocks that will
830	 * be needed to allocate a new object, so we accept the small chance
831	 * that there will be an i/o error and we will fail one of the
832	 * assertions below.
833	 */
834	if (vap->va_type == VDIR) {
835		if (zfsvfs->z_replay) {
836			err = zap_create_claim_norm(zfsvfs->z_os, obj,
837			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
838			    obj_type, bonuslen, tx);
839			ASSERT0(err);
840		} else {
841			obj = zap_create_norm(zfsvfs->z_os,
842			    zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
843			    obj_type, bonuslen, tx);
844		}
845	} else {
846		if (zfsvfs->z_replay) {
847			err = dmu_object_claim(zfsvfs->z_os, obj,
848			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
849			    obj_type, bonuslen, tx);
850			ASSERT0(err);
851		} else {
852			obj = dmu_object_alloc(zfsvfs->z_os,
853			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
854			    obj_type, bonuslen, tx);
855		}
856	}
857
858	getnewvnode_reserve(1);
859	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
860	VERIFY(0 == sa_buf_hold(zfsvfs->z_os, obj, NULL, &db));
861
862	/*
863	 * If this is the root, fix up the half-initialized parent pointer
864	 * to reference the just-allocated physical data area.
865	 */
866	if (flag & IS_ROOT_NODE) {
867		dzp->z_id = obj;
868	} else {
869		dzp_pflags = dzp->z_pflags;
870	}
871
872	/*
873	 * If parent is an xattr, so am I.
874	 */
875	if (dzp_pflags & ZFS_XATTR) {
876		flag |= IS_XATTR;
877	}
878
879	if (zfsvfs->z_use_fuids)
880		pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
881	else
882		pflags = 0;
883
884	if (vap->va_type == VDIR) {
885		size = 2;		/* contents ("." and "..") */
886		links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
887	} else {
888		size = links = 0;
889	}
890
891	if (vap->va_type == VBLK || vap->va_type == VCHR) {
892		rdev = zfs_expldev(vap->va_rdev);
893	}
894
895	parent = dzp->z_id;
896	mode = acl_ids->z_mode;
897	if (flag & IS_XATTR)
898		pflags |= ZFS_XATTR;
899
900	/*
901	 * No execs denied will be deterimed when zfs_mode_compute() is called.
902	 */
903	pflags |= acl_ids->z_aclp->z_hints &
904	    (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
905	    ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
906
907	ZFS_TIME_ENCODE(&now, crtime);
908	ZFS_TIME_ENCODE(&now, ctime);
909
910	if (vap->va_mask & AT_ATIME) {
911		ZFS_TIME_ENCODE(&vap->va_atime, atime);
912	} else {
913		ZFS_TIME_ENCODE(&now, atime);
914	}
915
916	if (vap->va_mask & AT_MTIME) {
917		ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
918	} else {
919		ZFS_TIME_ENCODE(&now, mtime);
920	}
921
922	/* Now add in all of the "SA" attributes */
923	VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED,
924	    &sa_hdl));
925
926	/*
927	 * Setup the array of attributes to be replaced/set on the new file
928	 *
929	 * order for  DMU_OT_ZNODE is critical since it needs to be constructed
930	 * in the old znode_phys_t format.  Don't change this ordering
931	 */
932
933	if (obj_type == DMU_OT_ZNODE) {
934		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
935		    NULL, &atime, 16);
936		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
937		    NULL, &mtime, 16);
938		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
939		    NULL, &ctime, 16);
940		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
941		    NULL, &crtime, 16);
942		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
943		    NULL, &gen, 8);
944		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
945		    NULL, &mode, 8);
946		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
947		    NULL, &size, 8);
948		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
949		    NULL, &parent, 8);
950	} else {
951		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
952		    NULL, &mode, 8);
953		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
954		    NULL, &size, 8);
955		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
956		    NULL, &gen, 8);
957		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
958		    &acl_ids->z_fuid, 8);
959		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
960		    &acl_ids->z_fgid, 8);
961		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
962		    NULL, &parent, 8);
963		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
964		    NULL, &pflags, 8);
965		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
966		    NULL, &atime, 16);
967		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
968		    NULL, &mtime, 16);
969		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
970		    NULL, &ctime, 16);
971		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
972		    NULL, &crtime, 16);
973	}
974
975	SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
976
977	if (obj_type == DMU_OT_ZNODE) {
978		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL,
979		    &empty_xattr, 8);
980	}
981	if (obj_type == DMU_OT_ZNODE ||
982	    (vap->va_type == VBLK || vap->va_type == VCHR)) {
983		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs),
984		    NULL, &rdev, 8);
985
986	}
987	if (obj_type == DMU_OT_ZNODE) {
988		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
989		    NULL, &pflags, 8);
990		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
991		    &acl_ids->z_fuid, 8);
992		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
993		    &acl_ids->z_fgid, 8);
994		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad,
995		    sizeof (uint64_t) * 4);
996		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
997		    &acl_phys, sizeof (zfs_acl_phys_t));
998	} else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
999		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
1000		    &acl_ids->z_aclp->z_acl_count, 8);
1001		locate.cb_aclp = acl_ids->z_aclp;
1002		SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs),
1003		    zfs_acl_data_locator, &locate,
1004		    acl_ids->z_aclp->z_acl_bytes);
1005		mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
1006		    acl_ids->z_fuid, acl_ids->z_fgid);
1007	}
1008
1009	VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
1010
1011	if (!(flag & IS_ROOT_NODE)) {
1012		*zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl);
1013		ASSERT(*zpp != NULL);
1014	} else {
1015		/*
1016		 * If we are creating the root node, the "parent" we
1017		 * passed in is the znode for the root.
1018		 */
1019		*zpp = dzp;
1020
1021		(*zpp)->z_sa_hdl = sa_hdl;
1022	}
1023
1024	(*zpp)->z_pflags = pflags;
1025	(*zpp)->z_mode = mode;
1026
1027	if (vap->va_mask & AT_XVATTR)
1028		zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx);
1029
1030	if (obj_type == DMU_OT_ZNODE ||
1031	    acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
1032		err = zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx);
1033		ASSERT0(err);
1034	}
1035	if (!(flag & IS_ROOT_NODE)) {
1036		vnode_t *vp;
1037
1038		vp = ZTOV(*zpp);
1039		vp->v_vflag |= VV_FORCEINSMQ;
1040		err = insmntque(vp, zfsvfs->z_vfs);
1041		vp->v_vflag &= ~VV_FORCEINSMQ;
1042		KASSERT(err == 0, ("insmntque() failed: error %d", err));
1043	}
1044	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1045	getnewvnode_drop_reserve();
1046}
1047
1048/*
1049 * zfs_xvattr_set only updates the in-core attributes
1050 * it is assumed the caller will be doing an sa_bulk_update
1051 * to push the changes out
1052 */
1053void
1054zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
1055{
1056	xoptattr_t *xoap;
1057
1058	xoap = xva_getxoptattr(xvap);
1059	ASSERT(xoap);
1060
1061	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
1062		uint64_t times[2];
1063		ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
1064		(void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
1065		    &times, sizeof (times), tx);
1066		XVA_SET_RTN(xvap, XAT_CREATETIME);
1067	}
1068	if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
1069		ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
1070		    zp->z_pflags, tx);
1071		XVA_SET_RTN(xvap, XAT_READONLY);
1072	}
1073	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
1074		ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
1075		    zp->z_pflags, tx);
1076		XVA_SET_RTN(xvap, XAT_HIDDEN);
1077	}
1078	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
1079		ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
1080		    zp->z_pflags, tx);
1081		XVA_SET_RTN(xvap, XAT_SYSTEM);
1082	}
1083	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
1084		ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
1085		    zp->z_pflags, tx);
1086		XVA_SET_RTN(xvap, XAT_ARCHIVE);
1087	}
1088	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
1089		ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
1090		    zp->z_pflags, tx);
1091		XVA_SET_RTN(xvap, XAT_IMMUTABLE);
1092	}
1093	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
1094		ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
1095		    zp->z_pflags, tx);
1096		XVA_SET_RTN(xvap, XAT_NOUNLINK);
1097	}
1098	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
1099		ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
1100		    zp->z_pflags, tx);
1101		XVA_SET_RTN(xvap, XAT_APPENDONLY);
1102	}
1103	if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
1104		ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
1105		    zp->z_pflags, tx);
1106		XVA_SET_RTN(xvap, XAT_NODUMP);
1107	}
1108	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
1109		ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
1110		    zp->z_pflags, tx);
1111		XVA_SET_RTN(xvap, XAT_OPAQUE);
1112	}
1113	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
1114		ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
1115		    xoap->xoa_av_quarantined, zp->z_pflags, tx);
1116		XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
1117	}
1118	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
1119		ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
1120		    zp->z_pflags, tx);
1121		XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
1122	}
1123	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
1124		zfs_sa_set_scanstamp(zp, xvap, tx);
1125		XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
1126	}
1127	if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
1128		ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
1129		    zp->z_pflags, tx);
1130		XVA_SET_RTN(xvap, XAT_REPARSE);
1131	}
1132	if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
1133		ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
1134		    zp->z_pflags, tx);
1135		XVA_SET_RTN(xvap, XAT_OFFLINE);
1136	}
1137	if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
1138		ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
1139		    zp->z_pflags, tx);
1140		XVA_SET_RTN(xvap, XAT_SPARSE);
1141	}
1142}
1143
1144int
1145zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
1146{
1147	dmu_object_info_t doi;
1148	dmu_buf_t	*db;
1149	znode_t		*zp;
1150	vnode_t		*vp;
1151	sa_handle_t	*hdl;
1152	struct thread	*td;
1153	int locked;
1154	int err;
1155
1156	td = curthread;
1157	getnewvnode_reserve(1);
1158again:
1159	*zpp = NULL;
1160	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1161
1162	err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1163	if (err) {
1164		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1165		getnewvnode_drop_reserve();
1166		return (err);
1167	}
1168
1169	dmu_object_info_from_db(db, &doi);
1170	if (doi.doi_bonus_type != DMU_OT_SA &&
1171	    (doi.doi_bonus_type != DMU_OT_ZNODE ||
1172	    (doi.doi_bonus_type == DMU_OT_ZNODE &&
1173	    doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1174		sa_buf_rele(db, NULL);
1175		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1176		getnewvnode_drop_reserve();
1177		return (EINVAL);
1178	}
1179
1180	hdl = dmu_buf_get_user(db);
1181	if (hdl != NULL) {
1182		zp  = sa_get_userdata(hdl);
1183
1184
1185		/*
1186		 * Since "SA" does immediate eviction we
1187		 * should never find a sa handle that doesn't
1188		 * know about the znode.
1189		 */
1190
1191		ASSERT3P(zp, !=, NULL);
1192
1193		mutex_enter(&zp->z_lock);
1194		ASSERT3U(zp->z_id, ==, obj_num);
1195		if (zp->z_unlinked) {
1196			err = ENOENT;
1197		} else {
1198			vp = ZTOV(zp);
1199			*zpp = zp;
1200			err = 0;
1201		}
1202		sa_buf_rele(db, NULL);
1203
1204		/* Don't let the vnode disappear after ZFS_OBJ_HOLD_EXIT. */
1205		if (err == 0)
1206			VN_HOLD(vp);
1207
1208		mutex_exit(&zp->z_lock);
1209		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1210
1211		if (err == 0) {
1212			locked = VOP_ISLOCKED(vp);
1213			VI_LOCK(vp);
1214			if ((vp->v_iflag & VI_DOOMED) != 0 &&
1215			    locked != LK_EXCLUSIVE) {
1216				/*
1217				 * The vnode is doomed and this thread doesn't
1218				 * hold the exclusive lock on it, so the vnode
1219				 * must be being reclaimed by another thread.
1220				 * Otherwise the doomed vnode is being reclaimed
1221				 * by this thread and zfs_zget is called from
1222				 * ZIL internals.
1223				 */
1224				VI_UNLOCK(vp);
1225				VN_RELE(vp);
1226				goto again;
1227			}
1228			VI_UNLOCK(vp);
1229		}
1230		getnewvnode_drop_reserve();
1231		return (err);
1232	}
1233
1234	/*
1235	 * Not found create new znode/vnode
1236	 * but only if file exists.
1237	 *
1238	 * There is a small window where zfs_vget() could
1239	 * find this object while a file create is still in
1240	 * progress.  This is checked for in zfs_znode_alloc()
1241	 *
1242	 * if zfs_znode_alloc() fails it will drop the hold on the
1243	 * bonus buffer.
1244	 */
1245	zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size,
1246	    doi.doi_bonus_type, NULL);
1247	if (zp == NULL) {
1248		err = ENOENT;
1249	} else {
1250		*zpp = zp;
1251	}
1252	if (err == 0) {
1253		vnode_t *vp = ZTOV(zp);
1254
1255		err = insmntque(vp, zfsvfs->z_vfs);
1256		if (err == 0)
1257			VOP_UNLOCK(vp, 0);
1258		else {
1259			zp->z_vnode = NULL;
1260			zfs_znode_dmu_fini(zp);
1261			zfs_znode_free(zp);
1262			*zpp = NULL;
1263		}
1264	}
1265	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1266	getnewvnode_drop_reserve();
1267	return (err);
1268}
1269
1270int
1271zfs_rezget(znode_t *zp)
1272{
1273	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1274	dmu_object_info_t doi;
1275	dmu_buf_t *db;
1276	vnode_t *vp;
1277	uint64_t obj_num = zp->z_id;
1278	uint64_t mode, size;
1279	sa_bulk_attr_t bulk[8];
1280	int err;
1281	int count = 0;
1282	uint64_t gen;
1283
1284	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1285
1286	mutex_enter(&zp->z_acl_lock);
1287	if (zp->z_acl_cached) {
1288		zfs_acl_free(zp->z_acl_cached);
1289		zp->z_acl_cached = NULL;
1290	}
1291
1292	mutex_exit(&zp->z_acl_lock);
1293	ASSERT(zp->z_sa_hdl == NULL);
1294	err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1295	if (err) {
1296		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1297		return (err);
1298	}
1299
1300	dmu_object_info_from_db(db, &doi);
1301	if (doi.doi_bonus_type != DMU_OT_SA &&
1302	    (doi.doi_bonus_type != DMU_OT_ZNODE ||
1303	    (doi.doi_bonus_type == DMU_OT_ZNODE &&
1304	    doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1305		sa_buf_rele(db, NULL);
1306		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1307		return (EINVAL);
1308	}
1309
1310	zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL);
1311	size = zp->z_size;
1312
1313	/* reload cached values */
1314	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1315	    &gen, sizeof (gen));
1316	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
1317	    &zp->z_size, sizeof (zp->z_size));
1318	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
1319	    &zp->z_links, sizeof (zp->z_links));
1320	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1321	    &zp->z_pflags, sizeof (zp->z_pflags));
1322	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1323	    &zp->z_atime, sizeof (zp->z_atime));
1324	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1325	    &zp->z_uid, sizeof (zp->z_uid));
1326	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1327	    &zp->z_gid, sizeof (zp->z_gid));
1328	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1329	    &mode, sizeof (mode));
1330
1331	if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1332		zfs_znode_dmu_fini(zp);
1333		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1334		return (EIO);
1335	}
1336
1337	zp->z_mode = mode;
1338
1339	if (gen != zp->z_gen) {
1340		zfs_znode_dmu_fini(zp);
1341		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1342		return (EIO);
1343	}
1344
1345	/*
1346	 * XXXPJD: Not sure how is that possible, but under heavy
1347	 * zfs recv -F load it happens that z_gen is the same, but
1348	 * vnode type is different than znode type. This would mean
1349	 * that for example regular file was replaced with directory
1350	 * which has the same object number.
1351	 */
1352	vp = ZTOV(zp);
1353	if (vp != NULL &&
1354	    vp->v_type != IFTOVT((mode_t)zp->z_mode)) {
1355		zfs_znode_dmu_fini(zp);
1356		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1357		return (EIO);
1358	}
1359
1360	zp->z_unlinked = (zp->z_links == 0);
1361	zp->z_blksz = doi.doi_data_block_size;
1362	if (vp != NULL) {
1363		vn_pages_remove(vp, 0, 0);
1364		if (zp->z_size != size)
1365			vnode_pager_setsize(vp, zp->z_size);
1366	}
1367
1368	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1369
1370	return (0);
1371}
1372
1373void
1374zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1375{
1376	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1377	objset_t *os = zfsvfs->z_os;
1378	uint64_t obj = zp->z_id;
1379	uint64_t acl_obj = zfs_external_acl(zp);
1380
1381	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1382	if (acl_obj) {
1383		VERIFY(!zp->z_is_sa);
1384		VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1385	}
1386	VERIFY(0 == dmu_object_free(os, obj, tx));
1387	zfs_znode_dmu_fini(zp);
1388	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1389	zfs_znode_free(zp);
1390}
1391
1392void
1393zfs_zinactive(znode_t *zp)
1394{
1395	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1396	uint64_t z_id = zp->z_id;
1397
1398	ASSERT(zp->z_sa_hdl);
1399
1400	/*
1401	 * Don't allow a zfs_zget() while were trying to release this znode
1402	 */
1403	ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1404
1405	mutex_enter(&zp->z_lock);
1406
1407	/*
1408	 * If this was the last reference to a file with no links,
1409	 * remove the file from the file system.
1410	 */
1411	if (zp->z_unlinked) {
1412		mutex_exit(&zp->z_lock);
1413		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1414		zfs_rmnode(zp);
1415		return;
1416	}
1417
1418	mutex_exit(&zp->z_lock);
1419	zfs_znode_dmu_fini(zp);
1420	ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1421	zfs_znode_free(zp);
1422}
1423
1424void
1425zfs_znode_free(znode_t *zp)
1426{
1427	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1428
1429	ASSERT(zp->z_sa_hdl == NULL);
1430	zp->z_vnode = NULL;
1431	mutex_enter(&zfsvfs->z_znodes_lock);
1432	POINTER_INVALIDATE(&zp->z_zfsvfs);
1433	list_remove(&zfsvfs->z_all_znodes, zp);
1434	mutex_exit(&zfsvfs->z_znodes_lock);
1435
1436	if (zp->z_acl_cached) {
1437		zfs_acl_free(zp->z_acl_cached);
1438		zp->z_acl_cached = NULL;
1439	}
1440
1441	kmem_cache_free(znode_cache, zp);
1442
1443	VFS_RELE(zfsvfs->z_vfs);
1444}
1445
1446void
1447zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1448    uint64_t ctime[2], boolean_t have_tx)
1449{
1450	timestruc_t	now;
1451
1452	gethrestime(&now);
1453
1454	if (have_tx) {	/* will sa_bulk_update happen really soon? */
1455		zp->z_atime_dirty = 0;
1456		zp->z_seq++;
1457	} else {
1458		zp->z_atime_dirty = 1;
1459	}
1460
1461	if (flag & AT_ATIME) {
1462		ZFS_TIME_ENCODE(&now, zp->z_atime);
1463	}
1464
1465	if (flag & AT_MTIME) {
1466		ZFS_TIME_ENCODE(&now, mtime);
1467		if (zp->z_zfsvfs->z_use_fuids) {
1468			zp->z_pflags |= (ZFS_ARCHIVE |
1469			    ZFS_AV_MODIFIED);
1470		}
1471	}
1472
1473	if (flag & AT_CTIME) {
1474		ZFS_TIME_ENCODE(&now, ctime);
1475		if (zp->z_zfsvfs->z_use_fuids)
1476			zp->z_pflags |= ZFS_ARCHIVE;
1477	}
1478}
1479
1480/*
1481 * Grow the block size for a file.
1482 *
1483 *	IN:	zp	- znode of file to free data in.
1484 *		size	- requested block size
1485 *		tx	- open transaction.
1486 *
1487 * NOTE: this function assumes that the znode is write locked.
1488 */
1489void
1490zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1491{
1492	int		error;
1493	u_longlong_t	dummy;
1494
1495	if (size <= zp->z_blksz)
1496		return;
1497	/*
1498	 * If the file size is already greater than the current blocksize,
1499	 * we will not grow.  If there is more than one block in a file,
1500	 * the blocksize cannot change.
1501	 */
1502	if (zp->z_blksz && zp->z_size > zp->z_blksz)
1503		return;
1504
1505	error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1506	    size, 0, tx);
1507
1508	if (error == ENOTSUP)
1509		return;
1510	ASSERT0(error);
1511
1512	/* What blocksize did we actually get? */
1513	dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1514}
1515
1516#ifdef sun
1517/*
1518 * This is a dummy interface used when pvn_vplist_dirty() should *not*
1519 * be calling back into the fs for a putpage().  E.g.: when truncating
1520 * a file, the pages being "thrown away* don't need to be written out.
1521 */
1522/* ARGSUSED */
1523static int
1524zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1525    int flags, cred_t *cr)
1526{
1527	ASSERT(0);
1528	return (0);
1529}
1530#endif	/* sun */
1531
1532/*
1533 * Increase the file length
1534 *
1535 *	IN:	zp	- znode of file to free data in.
1536 *		end	- new end-of-file
1537 *
1538 * 	RETURN:	0 if success
1539 *		error code if failure
1540 */
1541static int
1542zfs_extend(znode_t *zp, uint64_t end)
1543{
1544	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1545	dmu_tx_t *tx;
1546	rl_t *rl;
1547	uint64_t newblksz;
1548	int error;
1549
1550	/*
1551	 * We will change zp_size, lock the whole file.
1552	 */
1553	rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1554
1555	/*
1556	 * Nothing to do if file already at desired length.
1557	 */
1558	if (end <= zp->z_size) {
1559		zfs_range_unlock(rl);
1560		return (0);
1561	}
1562top:
1563	tx = dmu_tx_create(zfsvfs->z_os);
1564	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1565	zfs_sa_upgrade_txholds(tx, zp);
1566	if (end > zp->z_blksz &&
1567	    (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1568		/*
1569		 * We are growing the file past the current block size.
1570		 */
1571		if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1572			ASSERT(!ISP2(zp->z_blksz));
1573			newblksz = MIN(end, SPA_MAXBLOCKSIZE);
1574		} else {
1575			newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1576		}
1577		dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1578	} else {
1579		newblksz = 0;
1580	}
1581
1582	error = dmu_tx_assign(tx, TXG_NOWAIT);
1583	if (error) {
1584		if (error == ERESTART) {
1585			dmu_tx_wait(tx);
1586			dmu_tx_abort(tx);
1587			goto top;
1588		}
1589		dmu_tx_abort(tx);
1590		zfs_range_unlock(rl);
1591		return (error);
1592	}
1593
1594	if (newblksz)
1595		zfs_grow_blocksize(zp, newblksz, tx);
1596
1597	zp->z_size = end;
1598
1599	VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs),
1600	    &zp->z_size, sizeof (zp->z_size), tx));
1601
1602	vnode_pager_setsize(ZTOV(zp), end);
1603
1604	zfs_range_unlock(rl);
1605
1606	dmu_tx_commit(tx);
1607
1608	return (0);
1609}
1610
1611/*
1612 * Free space in a file.
1613 *
1614 *	IN:	zp	- znode of file to free data in.
1615 *		off	- start of section to free.
1616 *		len	- length of section to free.
1617 *
1618 * 	RETURN:	0 if success
1619 *		error code if failure
1620 */
1621static int
1622zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1623{
1624	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1625	rl_t *rl;
1626	int error;
1627
1628	/*
1629	 * Lock the range being freed.
1630	 */
1631	rl = zfs_range_lock(zp, off, len, RL_WRITER);
1632
1633	/*
1634	 * Nothing to do if file already at desired length.
1635	 */
1636	if (off >= zp->z_size) {
1637		zfs_range_unlock(rl);
1638		return (0);
1639	}
1640
1641	if (off + len > zp->z_size)
1642		len = zp->z_size - off;
1643
1644	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1645
1646	if (error == 0) {
1647		/*
1648		 * In FreeBSD we cannot free block in the middle of a file,
1649		 * but only at the end of a file, so this code path should
1650		 * never happen.
1651		 */
1652		vnode_pager_setsize(ZTOV(zp), off);
1653	}
1654
1655	zfs_range_unlock(rl);
1656
1657	return (error);
1658}
1659
1660/*
1661 * Truncate a file
1662 *
1663 *	IN:	zp	- znode of file to free data in.
1664 *		end	- new end-of-file.
1665 *
1666 * 	RETURN:	0 if success
1667 *		error code if failure
1668 */
1669static int
1670zfs_trunc(znode_t *zp, uint64_t end)
1671{
1672	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1673	vnode_t *vp = ZTOV(zp);
1674	dmu_tx_t *tx;
1675	rl_t *rl;
1676	int error;
1677	sa_bulk_attr_t bulk[2];
1678	int count = 0;
1679
1680	/*
1681	 * We will change zp_size, lock the whole file.
1682	 */
1683	rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1684
1685	/*
1686	 * Nothing to do if file already at desired length.
1687	 */
1688	if (end >= zp->z_size) {
1689		zfs_range_unlock(rl);
1690		return (0);
1691	}
1692
1693	error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end,  -1);
1694	if (error) {
1695		zfs_range_unlock(rl);
1696		return (error);
1697	}
1698top:
1699	tx = dmu_tx_create(zfsvfs->z_os);
1700	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1701	zfs_sa_upgrade_txholds(tx, zp);
1702	error = dmu_tx_assign(tx, TXG_NOWAIT);
1703	if (error) {
1704		if (error == ERESTART) {
1705			dmu_tx_wait(tx);
1706			dmu_tx_abort(tx);
1707			goto top;
1708		}
1709		dmu_tx_abort(tx);
1710		zfs_range_unlock(rl);
1711		return (error);
1712	}
1713
1714	zp->z_size = end;
1715	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
1716	    NULL, &zp->z_size, sizeof (zp->z_size));
1717
1718	if (end == 0) {
1719		zp->z_pflags &= ~ZFS_SPARSE;
1720		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1721		    NULL, &zp->z_pflags, 8);
1722	}
1723	VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1724
1725	dmu_tx_commit(tx);
1726
1727	/*
1728	 * Clear any mapped pages in the truncated region.  This has to
1729	 * happen outside of the transaction to avoid the possibility of
1730	 * a deadlock with someone trying to push a page that we are
1731	 * about to invalidate.
1732	 */
1733	vnode_pager_setsize(vp, end);
1734
1735	zfs_range_unlock(rl);
1736
1737	return (0);
1738}
1739
1740/*
1741 * Free space in a file
1742 *
1743 *	IN:	zp	- znode of file to free data in.
1744 *		off	- start of range
1745 *		len	- end of range (0 => EOF)
1746 *		flag	- current file open mode flags.
1747 *		log	- TRUE if this action should be logged
1748 *
1749 * 	RETURN:	0 if success
1750 *		error code if failure
1751 */
1752int
1753zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1754{
1755	vnode_t *vp = ZTOV(zp);
1756	dmu_tx_t *tx;
1757	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1758	zilog_t *zilog = zfsvfs->z_log;
1759	uint64_t mode;
1760	uint64_t mtime[2], ctime[2];
1761	sa_bulk_attr_t bulk[3];
1762	int count = 0;
1763	int error;
1764
1765	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
1766	    sizeof (mode))) != 0)
1767		return (error);
1768
1769	if (off > zp->z_size) {
1770		error =  zfs_extend(zp, off+len);
1771		if (error == 0 && log)
1772			goto log;
1773		else
1774			return (error);
1775	}
1776
1777	/*
1778	 * Check for any locks in the region to be freed.
1779	 */
1780
1781	if (MANDLOCK(vp, (mode_t)mode)) {
1782		uint64_t length = (len ? len : zp->z_size - off);
1783		if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1784			return (error);
1785	}
1786
1787	if (len == 0) {
1788		error = zfs_trunc(zp, off);
1789	} else {
1790		if ((error = zfs_free_range(zp, off, len)) == 0 &&
1791		    off + len > zp->z_size)
1792			error = zfs_extend(zp, off+len);
1793	}
1794	if (error || !log)
1795		return (error);
1796log:
1797	tx = dmu_tx_create(zfsvfs->z_os);
1798	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1799	zfs_sa_upgrade_txholds(tx, zp);
1800	error = dmu_tx_assign(tx, TXG_NOWAIT);
1801	if (error) {
1802		if (error == ERESTART) {
1803			dmu_tx_wait(tx);
1804			dmu_tx_abort(tx);
1805			goto log;
1806		}
1807		dmu_tx_abort(tx);
1808		return (error);
1809	}
1810
1811	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
1812	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
1813	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1814	    NULL, &zp->z_pflags, 8);
1815	zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1816	error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1817	ASSERT(error == 0);
1818
1819	zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1820
1821	dmu_tx_commit(tx);
1822	return (0);
1823}
1824
1825void
1826zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1827{
1828	zfsvfs_t	zfsvfs;
1829	uint64_t	moid, obj, sa_obj, version;
1830	uint64_t	sense = ZFS_CASE_SENSITIVE;
1831	uint64_t	norm = 0;
1832	nvpair_t	*elem;
1833	int		error;
1834	int		i;
1835	znode_t		*rootzp = NULL;
1836	vnode_t		vnode;
1837	vattr_t		vattr;
1838	znode_t		*zp;
1839	zfs_acl_ids_t	acl_ids;
1840
1841	/*
1842	 * First attempt to create master node.
1843	 */
1844	/*
1845	 * In an empty objset, there are no blocks to read and thus
1846	 * there can be no i/o errors (which we assert below).
1847	 */
1848	moid = MASTER_NODE_OBJ;
1849	error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1850	    DMU_OT_NONE, 0, tx);
1851	ASSERT(error == 0);
1852
1853	/*
1854	 * Set starting attributes.
1855	 */
1856	version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1857	elem = NULL;
1858	while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1859		/* For the moment we expect all zpl props to be uint64_ts */
1860		uint64_t val;
1861		char *name;
1862
1863		ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1864		VERIFY(nvpair_value_uint64(elem, &val) == 0);
1865		name = nvpair_name(elem);
1866		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1867			if (val < version)
1868				version = val;
1869		} else {
1870			error = zap_update(os, moid, name, 8, 1, &val, tx);
1871		}
1872		ASSERT(error == 0);
1873		if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1874			norm = val;
1875		else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1876			sense = val;
1877	}
1878	ASSERT(version != 0);
1879	error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1880
1881	/*
1882	 * Create zap object used for SA attribute registration
1883	 */
1884
1885	if (version >= ZPL_VERSION_SA) {
1886		sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1887		    DMU_OT_NONE, 0, tx);
1888		error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1889		ASSERT(error == 0);
1890	} else {
1891		sa_obj = 0;
1892	}
1893	/*
1894	 * Create a delete queue.
1895	 */
1896	obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1897
1898	error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1899	ASSERT(error == 0);
1900
1901	/*
1902	 * Create root znode.  Create minimal znode/vnode/zfsvfs
1903	 * to allow zfs_mknode to work.
1904	 */
1905	VATTR_NULL(&vattr);
1906	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1907	vattr.va_type = VDIR;
1908	vattr.va_mode = S_IFDIR|0755;
1909	vattr.va_uid = crgetuid(cr);
1910	vattr.va_gid = crgetgid(cr);
1911
1912	bzero(&zfsvfs, sizeof (zfsvfs_t));
1913
1914	rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1915	zfs_znode_cache_constructor(rootzp, NULL, 0);
1916	ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1917	rootzp->z_moved = 0;
1918	rootzp->z_unlinked = 0;
1919	rootzp->z_atime_dirty = 0;
1920	rootzp->z_is_sa = USE_SA(version, os);
1921
1922	vnode.v_type = VDIR;
1923	vnode.v_data = rootzp;
1924	rootzp->z_vnode = &vnode;
1925
1926	zfsvfs.z_os = os;
1927	zfsvfs.z_parent = &zfsvfs;
1928	zfsvfs.z_version = version;
1929	zfsvfs.z_use_fuids = USE_FUIDS(version, os);
1930	zfsvfs.z_use_sa = USE_SA(version, os);
1931	zfsvfs.z_norm = norm;
1932
1933	error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1934	    &zfsvfs.z_attr_table);
1935
1936	ASSERT(error == 0);
1937
1938	/*
1939	 * Fold case on file systems that are always or sometimes case
1940	 * insensitive.
1941	 */
1942	if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1943		zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER;
1944
1945	mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1946	list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
1947	    offsetof(znode_t, z_link_node));
1948
1949	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1950		mutex_init(&zfsvfs.z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1951
1952	rootzp->z_zfsvfs = &zfsvfs;
1953	VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1954	    cr, NULL, &acl_ids));
1955	zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1956	ASSERT3P(zp, ==, rootzp);
1957	error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1958	ASSERT(error == 0);
1959	zfs_acl_ids_free(&acl_ids);
1960	POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1961
1962	sa_handle_destroy(rootzp->z_sa_hdl);
1963	rootzp->z_vnode = NULL;
1964	kmem_cache_free(znode_cache, rootzp);
1965
1966	/*
1967	 * Create shares directory
1968	 */
1969
1970	error = zfs_create_share_dir(&zfsvfs, tx);
1971
1972	ASSERT(error == 0);
1973
1974	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1975		mutex_destroy(&zfsvfs.z_hold_mtx[i]);
1976}
1977
1978#endif /* _KERNEL */
1979
1980static int
1981zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1982{
1983	uint64_t sa_obj = 0;
1984	int error;
1985
1986	error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1987	if (error != 0 && error != ENOENT)
1988		return (error);
1989
1990	error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1991	return (error);
1992}
1993
1994static int
1995zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1996    dmu_buf_t **db, void *tag)
1997{
1998	dmu_object_info_t doi;
1999	int error;
2000
2001	if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
2002		return (error);
2003
2004	dmu_object_info_from_db(*db, &doi);
2005	if ((doi.doi_bonus_type != DMU_OT_SA &&
2006	    doi.doi_bonus_type != DMU_OT_ZNODE) ||
2007	    doi.doi_bonus_type == DMU_OT_ZNODE &&
2008	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
2009		sa_buf_rele(*db, tag);
2010		return (ENOTSUP);
2011	}
2012
2013	error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
2014	if (error != 0) {
2015		sa_buf_rele(*db, tag);
2016		return (error);
2017	}
2018
2019	return (0);
2020}
2021
2022void
2023zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
2024{
2025	sa_handle_destroy(hdl);
2026	sa_buf_rele(db, tag);
2027}
2028
2029/*
2030 * Given an object number, return its parent object number and whether
2031 * or not the object is an extended attribute directory.
2032 */
2033static int
2034zfs_obj_to_pobj(objset_t *osp, sa_handle_t *hdl, sa_attr_type_t *sa_table,
2035    uint64_t *pobjp, int *is_xattrdir)
2036{
2037	uint64_t parent;
2038	uint64_t pflags;
2039	uint64_t mode;
2040	uint64_t parent_mode;
2041	sa_bulk_attr_t bulk[3];
2042	sa_handle_t *sa_hdl;
2043	dmu_buf_t *sa_db;
2044	int count = 0;
2045	int error;
2046
2047	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
2048	    &parent, sizeof (parent));
2049	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
2050	    &pflags, sizeof (pflags));
2051	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2052	    &mode, sizeof (mode));
2053
2054	if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
2055		return (error);
2056
2057	/*
2058	 * When a link is removed its parent pointer is not changed and will
2059	 * be invalid.  There are two cases where a link is removed but the
2060	 * file stays around, when it goes to the delete queue and when there
2061	 * are additional links.
2062	 */
2063	error = zfs_grab_sa_handle(osp, parent, &sa_hdl, &sa_db, FTAG);
2064	if (error != 0)
2065		return (error);
2066
2067	error = sa_lookup(sa_hdl, ZPL_MODE, &parent_mode, sizeof (parent_mode));
2068	zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2069	if (error != 0)
2070		return (error);
2071
2072	*is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
2073
2074	/*
2075	 * Extended attributes can be applied to files, directories, etc.
2076	 * Otherwise the parent must be a directory.
2077	 */
2078	if (!*is_xattrdir && !S_ISDIR(parent_mode))
2079		return (EINVAL);
2080
2081	*pobjp = parent;
2082
2083	return (0);
2084}
2085
2086/*
2087 * Given an object number, return some zpl level statistics
2088 */
2089static int
2090zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
2091    zfs_stat_t *sb)
2092{
2093	sa_bulk_attr_t bulk[4];
2094	int count = 0;
2095
2096	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2097	    &sb->zs_mode, sizeof (sb->zs_mode));
2098	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
2099	    &sb->zs_gen, sizeof (sb->zs_gen));
2100	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
2101	    &sb->zs_links, sizeof (sb->zs_links));
2102	SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
2103	    &sb->zs_ctime, sizeof (sb->zs_ctime));
2104
2105	return (sa_bulk_lookup(hdl, bulk, count));
2106}
2107
2108static int
2109zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
2110    sa_attr_type_t *sa_table, char *buf, int len)
2111{
2112	sa_handle_t *sa_hdl;
2113	sa_handle_t *prevhdl = NULL;
2114	dmu_buf_t *prevdb = NULL;
2115	dmu_buf_t *sa_db = NULL;
2116	char *path = buf + len - 1;
2117	int error;
2118
2119	*path = '\0';
2120	sa_hdl = hdl;
2121
2122	for (;;) {
2123		uint64_t pobj;
2124		char component[MAXNAMELEN + 2];
2125		size_t complen;
2126		int is_xattrdir;
2127
2128		if (prevdb)
2129			zfs_release_sa_handle(prevhdl, prevdb, FTAG);
2130
2131		if ((error = zfs_obj_to_pobj(osp, sa_hdl, sa_table, &pobj,
2132		    &is_xattrdir)) != 0)
2133			break;
2134
2135		if (pobj == obj) {
2136			if (path[0] != '/')
2137				*--path = '/';
2138			break;
2139		}
2140
2141		component[0] = '/';
2142		if (is_xattrdir) {
2143			(void) sprintf(component + 1, "<xattrdir>");
2144		} else {
2145			error = zap_value_search(osp, pobj, obj,
2146			    ZFS_DIRENT_OBJ(-1ULL), component + 1);
2147			if (error != 0)
2148				break;
2149		}
2150
2151		complen = strlen(component);
2152		path -= complen;
2153		ASSERT(path >= buf);
2154		bcopy(component, path, complen);
2155		obj = pobj;
2156
2157		if (sa_hdl != hdl) {
2158			prevhdl = sa_hdl;
2159			prevdb = sa_db;
2160		}
2161		error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
2162		if (error != 0) {
2163			sa_hdl = prevhdl;
2164			sa_db = prevdb;
2165			break;
2166		}
2167	}
2168
2169	if (sa_hdl != NULL && sa_hdl != hdl) {
2170		ASSERT(sa_db != NULL);
2171		zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2172	}
2173
2174	if (error == 0)
2175		(void) memmove(buf, path, buf + len - path);
2176
2177	return (error);
2178}
2179
2180int
2181zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
2182{
2183	sa_attr_type_t *sa_table;
2184	sa_handle_t *hdl;
2185	dmu_buf_t *db;
2186	int error;
2187
2188	error = zfs_sa_setup(osp, &sa_table);
2189	if (error != 0)
2190		return (error);
2191
2192	error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2193	if (error != 0)
2194		return (error);
2195
2196	error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2197
2198	zfs_release_sa_handle(hdl, db, FTAG);
2199	return (error);
2200}
2201
2202int
2203zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
2204    char *buf, int len)
2205{
2206	char *path = buf + len - 1;
2207	sa_attr_type_t *sa_table;
2208	sa_handle_t *hdl;
2209	dmu_buf_t *db;
2210	int error;
2211
2212	*path = '\0';
2213
2214	error = zfs_sa_setup(osp, &sa_table);
2215	if (error != 0)
2216		return (error);
2217
2218	error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2219	if (error != 0)
2220		return (error);
2221
2222	error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
2223	if (error != 0) {
2224		zfs_release_sa_handle(hdl, db, FTAG);
2225		return (error);
2226	}
2227
2228	error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2229
2230	zfs_release_sa_handle(hdl, db, FTAG);
2231	return (error);
2232}
2233