zfs_znode.c revision 182824
167754Smsmith/*
267754Smsmith * CDDL HEADER START
369450Smsmith *
467754Smsmith * The contents of this file are subject to the terms of the
567754Smsmith * Common Development and Distribution License (the "License").
667754Smsmith * You may not use this file except in compliance with the License.
7217365Sjkim *
8298714Sjkim * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
970243Smsmith * or http://www.opensolaris.org/os/licensing.
1067754Smsmith * See the License for the specific language governing permissions
11217365Sjkim * and limitations under the License.
12217365Sjkim *
13217365Sjkim * When distributing Covered Code, include this CDDL HEADER in each
14217365Sjkim * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15217365Sjkim * If applicable, add the following below this CDDL HEADER, with the
16217365Sjkim * fields enclosed by brackets "[]" replaced with your own identifying
17217365Sjkim * information: Portions Copyright [yyyy] [name of copyright owner]
18217365Sjkim *
19217365Sjkim * CDDL HEADER END
20217365Sjkim */
21217365Sjkim/*
22217365Sjkim * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23217365Sjkim * Use is subject to license terms.
24217365Sjkim */
2567754Smsmith
26217365Sjkim/* Portions Copyright 2007 Jeremy Teo */
27217365Sjkim
28217365Sjkim#pragma ident	"%Z%%M%	%I%	%E% SMI"
2967754Smsmith
30217365Sjkim#ifdef _KERNEL
31217365Sjkim#include <sys/types.h>
32217365Sjkim#include <sys/param.h>
33217365Sjkim#include <sys/time.h>
34217365Sjkim#include <sys/systm.h>
35217365Sjkim#include <sys/sysmacros.h>
36217365Sjkim#include <sys/resource.h>
37217365Sjkim#include <sys/mntent.h>
38217365Sjkim#include <sys/vfs.h>
39217365Sjkim#include <sys/vnode.h>
40217365Sjkim#include <sys/file.h>
41217365Sjkim#include <sys/kmem.h>
42217365Sjkim#include <sys/cmn_err.h>
4367754Smsmith#include <sys/errno.h>
44193341Sjkim#include <sys/unistd.h>
45193341Sjkim#include <sys/atomic.h>
46193341Sjkim#include <sys/zfs_dir.h>
47193341Sjkim#include <sys/zfs_acl.h>
4867754Smsmith#include <sys/zfs_ioctl.h>
4977424Smsmith#include <sys/zfs_rlock.h>
5091116Smsmith#include <sys/fs/zfs.h>
5167754Smsmith#endif /* _KERNEL */
52167802Sjkim
5367754Smsmith#include <sys/dmu.h>
54167802Sjkim#include <sys/refcount.h>
55167802Sjkim#include <sys/stat.h>
56167802Sjkim#include <sys/zap.h>
57167802Sjkim#include <sys/zfs_znode.h>
58167802Sjkim#include <sys/refcount.h>
5977424Smsmith
6067754Smsmith/* Used by fstat(1). */
6167754SmsmithSYSCTL_INT(_debug_sizeof, OID_AUTO, znode, CTLFLAG_RD, 0, sizeof(znode_t),
6267754Smsmith    "sizeof(znode_t)");
63138287Smarks
6499679Siwasaki/*
6567754Smsmith * Functions needed for userland (ie: libzpool) are not put under
6667754Smsmith * #ifdef_KERNEL; the rest of the functions have dependencies
6767754Smsmith * (such as VFS logic) that will not compile easily in userland.
6867754Smsmith */
6967754Smsmith#ifdef _KERNEL
70151937Sjkimstruct kmem_cache *znode_cache = NULL;
7167754Smsmith
7277424Smsmith/*ARGSUSED*/
7367754Smsmithstatic void
7467754Smsmithznode_pageout_func(dmu_buf_t *dbuf, void *user_ptr)
7567754Smsmith{
7667754Smsmith	znode_t *zp = user_ptr;
7767754Smsmith	vnode_t *vp;
7867754Smsmith
7967754Smsmith	mutex_enter(&zp->z_lock);
8067754Smsmith	vp = ZTOV(zp);
8192388Smsmith	if (vp == NULL) {
8292388Smsmith		mutex_exit(&zp->z_lock);
8392388Smsmith		zfs_znode_free(zp);
8499679Siwasaki	} else if (vp->v_count == 0) {
85167802Sjkim		ZTOV(zp) = NULL;
8667754Smsmith		vhold(vp);
8767754Smsmith		mutex_exit(&zp->z_lock);
8867754Smsmith		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
8967754Smsmith		vrecycle(vp, curthread);
9067754Smsmith		VOP_UNLOCK(vp, 0);
9167754Smsmith		vdrop(vp);
92167802Sjkim		zfs_znode_free(zp);
93167802Sjkim	} else {
94167802Sjkim		/* signal force unmount that this znode can be freed */
95167802Sjkim		zp->z_dbuf = NULL;
96167802Sjkim		mutex_exit(&zp->z_lock);
97167802Sjkim	}
98167802Sjkim}
99167802Sjkim
100167802Sjkimextern struct vop_vector zfs_vnodeops;
101167802Sjkimextern struct vop_vector zfs_fifoops;
10267754Smsmith
10367754Smsmith/*
10467754Smsmith * XXX: We cannot use this function as a cache constructor, because
10567754Smsmith *      there is one global cache for all file systems and we need
10667754Smsmith *      to pass vfsp here, which is not possible, because argument
10799679Siwasaki *      'cdrarg' is defined at kmem_cache_create() time.
10867754Smsmith */
109167802Sjkimstatic int
11092388Smsmithzfs_znode_cache_constructor(void *buf, void *cdrarg, int kmflags)
11167754Smsmith{
11267754Smsmith	znode_t *zp = buf;
11367754Smsmith	vnode_t *vp;
11467754Smsmith	vfs_t *vfsp = cdrarg;
11592388Smsmith	int error;
11692388Smsmith
11792388Smsmith	if (cdrarg != NULL) {
11892388Smsmith		error = getnewvnode("zfs", vfsp, &zfs_vnodeops, &vp);
11992388Smsmith		ASSERT(error == 0);
12092388Smsmith		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
12167754Smsmith		zp->z_vnode = vp;
12267754Smsmith		vp->v_data = (caddr_t)zp;
12367754Smsmith		VN_LOCK_AREC(vp);
12467754Smsmith		VN_LOCK_ASHARE(vp);
12577424Smsmith	} else {
12667754Smsmith		zp->z_vnode = NULL;
12767754Smsmith	}
12867754Smsmith	mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
129138287Smarks	rw_init(&zp->z_map_lock, NULL, RW_DEFAULT, NULL);
13099679Siwasaki	rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
13167754Smsmith	rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
13267754Smsmith	mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
13367754Smsmith
13467754Smsmith	mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
13567754Smsmith	avl_create(&zp->z_range_avl, zfs_range_compare,
136151937Sjkim	    sizeof (rl_t), offsetof(rl_t, r_node));
13767754Smsmith
13877424Smsmith	zp->z_dbuf_held = 0;
13967754Smsmith	zp->z_dirlocks = 0;
14067754Smsmith	return (0);
14167754Smsmith}
14267754Smsmith
14367754Smsmith/*ARGSUSED*/
14467754Smsmithstatic void
14567754Smsmithzfs_znode_cache_destructor(void *buf, void *cdarg)
14667754Smsmith{
147167802Sjkim	znode_t *zp = buf;
14867754Smsmith
14977424Smsmith	ASSERT(zp->z_dirlocks == 0);
15067754Smsmith	mutex_destroy(&zp->z_lock);
15167754Smsmith	rw_destroy(&zp->z_map_lock);
15267754Smsmith	rw_destroy(&zp->z_parent_lock);
15367754Smsmith	rw_destroy(&zp->z_name_lock);
15467754Smsmith	mutex_destroy(&zp->z_acl_lock);
15567754Smsmith	mutex_destroy(&zp->z_range_lock);
15667754Smsmith	avl_destroy(&zp->z_range_avl);
15767754Smsmith
15867754Smsmith	ASSERT(zp->z_dbuf_held == 0);
15967754Smsmith}
16067754Smsmith
16167754Smsmithvoid
16267754Smsmithzfs_znode_init(void)
16377424Smsmith{
16467754Smsmith	/*
16567754Smsmith	 * Initialize zcache
16667754Smsmith	 */
167151937Sjkim	ASSERT(znode_cache == NULL);
16899679Siwasaki	znode_cache = kmem_cache_create("zfs_znode_cache",
16967754Smsmith	    sizeof (znode_t), 0, /* zfs_znode_cache_constructor */ NULL,
17067754Smsmith	    zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
17167754Smsmith}
17267754Smsmith
17367754Smsmithvoid
174151937Sjkimzfs_znode_fini(void)
17567754Smsmith{
17667754Smsmith	/*
17767754Smsmith	 * Cleanup zcache
17877424Smsmith	 */
17967754Smsmith	if (znode_cache)
18067754Smsmith		kmem_cache_destroy(znode_cache);
18167754Smsmith	znode_cache = NULL;
18267754Smsmith}
18367754Smsmith
18467754Smsmith/*
18567754Smsmith * zfs_init_fs - Initialize the zfsvfs struct and the file system
18667754Smsmith *	incore "master" object.  Verify version compatibility.
18767754Smsmith */
188202771Sjkimint
18980062Smsmithzfs_init_fs(zfsvfs_t *zfsvfs, znode_t **zpp, cred_t *cr)
19067754Smsmith{
191117521Snjl	objset_t	*os = zfsvfs->z_os;
192117521Snjl	uint64_t	version = ZPL_VERSION;
193167802Sjkim	int		i, error;
19467754Smsmith	dmu_object_info_t doi;
19567754Smsmith	uint64_t fsid_guid;
19677424Smsmith
197167802Sjkim	*zpp = NULL;
19867754Smsmith
19977424Smsmith	/*
200123315Snjl	 * XXX - hack to auto-create the pool root filesystem at
20167754Smsmith	 * the first attempted mount.
20267754Smsmith	 */
20367754Smsmith	if (dmu_object_info(os, MASTER_NODE_OBJ, &doi) == ENOENT) {
20499679Siwasaki		dmu_tx_t *tx = dmu_tx_create(os);
20599679Siwasaki
20667754Smsmith		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); /* master */
20782367Smsmith		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); /* del queue */
20885756Smsmith		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); /* root node */
20977424Smsmith		error = dmu_tx_assign(tx, TXG_WAIT);
21067754Smsmith		ASSERT3U(error, ==, 0);
21167754Smsmith		zfs_create_fs(os, cr, tx);
212117521Snjl		dmu_tx_commit(tx);
21367754Smsmith	}
21467754Smsmith
21580062Smsmith	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_OBJ, 8, 1,
21667754Smsmith	    &version);
217167802Sjkim	if (error) {
21867754Smsmith		return (error);
21967754Smsmith	} else if (version != ZPL_VERSION) {
22067754Smsmith		(void) printf("Mismatched versions:  File system "
22167754Smsmith		    "is version %lld on-disk format, which is "
222209746Sjkim		    "incompatible with this software version %lld!",
22367754Smsmith		    (u_longlong_t)version, ZPL_VERSION);
22467754Smsmith		return (ENOTSUP);
22599679Siwasaki	}
22699679Siwasaki
22767754Smsmith	/*
22899679Siwasaki	 * The fsid is 64 bits, composed of an 8-bit fs type, which
22999679Siwasaki	 * separates our fsid from any other filesystem types, and a
23067754Smsmith	 * 56-bit objset unique ID.  The objset unique ID is unique to
23167754Smsmith	 * all objsets open on this system, provided by unique_create().
23267754Smsmith	 * The 8-bit fs type must be put in the low bits of fsid[1]
233117521Snjl	 * because that's where other Solaris filesystems put it.
23499679Siwasaki	 */
23599679Siwasaki	fsid_guid = dmu_objset_fsid_guid(os);
23667754Smsmith	ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
237117521Snjl	zfsvfs->z_vfs->vfs_fsid.val[0] = fsid_guid;
23867754Smsmith	zfsvfs->z_vfs->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
239117521Snjl	    zfsvfs->z_vfs->mnt_vfc->vfc_typenum & 0xFF;
240117521Snjl
241117521Snjl	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
242117521Snjl	    &zfsvfs->z_root);
24367754Smsmith	if (error)
244167802Sjkim		return (error);
245167802Sjkim	ASSERT(zfsvfs->z_root != 0);
246167802Sjkim
24767754Smsmith	/*
248167802Sjkim	 * Create the per mount vop tables.
249167802Sjkim	 */
250167802Sjkim
251298714Sjkim	/*
252298714Sjkim	 * Initialize zget mutex's
253298714Sjkim	 */
254167802Sjkim	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
25567754Smsmith		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
256167802Sjkim
25799679Siwasaki	error = zfs_zget(zfsvfs, zfsvfs->z_root, zpp);
258167802Sjkim	if (error)
259193267Sjkim		return (error);
260193267Sjkim	ASSERT3U((*zpp)->z_id, ==, zfsvfs->z_root);
261193267Sjkim
262167802Sjkim	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
263167802Sjkim	    &zfsvfs->z_unlinkedobj);
26499679Siwasaki	if (error)
265167802Sjkim		return (error);
266167802Sjkim
267167802Sjkim	return (0);
268193267Sjkim}
269193267Sjkim
270167802Sjkim/*
271167802Sjkim * define a couple of values we need available
27267754Smsmith * for both 64 and 32 bit environments.
273167802Sjkim */
27467754Smsmith#ifndef NBITSMINOR64
27567754Smsmith#define	NBITSMINOR64	32
276209746Sjkim#endif
27767754Smsmith#ifndef MAXMAJ64
278117521Snjl#define	MAXMAJ64	0xffffffffUL
279117521Snjl#endif
28067754Smsmith#ifndef	MAXMIN64
28167754Smsmith#define	MAXMIN64	0xffffffffUL
28267754Smsmith#endif
283117521Snjl
28467754Smsmith/*
28569450Smsmith * Create special expldev for ZFS private use.
28680062Smsmith * Can't use standard expldev since it doesn't do
287117521Snjl * what we want.  The standard expldev() takes a
288117521Snjl * dev32_t in LP64 and expands it to a long dev_t.
28980062Smsmith * We need an interface that takes a dev32_t in ILP32
290117521Snjl * and expands it to a long dev_t.
29167754Smsmith */
292117521Snjlstatic uint64_t
29367754Smsmithzfs_expldev(dev_t dev)
29467754Smsmith{
295117521Snjl	return (((uint64_t)umajor(dev) << NBITSMINOR64) | uminor(dev));
296117521Snjl}
297167802Sjkim/*
298117521Snjl * Special cmpldev for ZFS private use.
299117521Snjl * Can't use standard cmpldev since it takes
300117521Snjl * a long dev_t and compresses it to dev32_t in
301117521Snjl * LP64.  We need to do a compaction of a long dev_t
302117521Snjl * to a dev32_t in ILP32.
30380062Smsmith */
304193267Sjkimdev_t
305193267Sjkimzfs_cmpldev(uint64_t dev)
306167802Sjkim{
307167802Sjkim	return (makedev((dev >> NBITSMINOR64), (dev & MAXMIN64)));
308167802Sjkim}
30980062Smsmith
310167802Sjkim/*
311167802Sjkim * Construct a new znode/vnode and intialize.
312167802Sjkim *
313209746Sjkim * This does not do a call to dmu_set_user() that is
314167802Sjkim * up to the caller to do, in case you don't want to
315117521Snjl * return the znode
316167802Sjkim */
317167802Sjkimstatic znode_t *
318193267Sjkimzfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, uint64_t obj_num, int blksz)
319167802Sjkim{
320167802Sjkim	znode_t	*zp;
321167802Sjkim	vnode_t *vp;
322117521Snjl	int error;
323212761Sjkim
324212761Sjkim	zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
325117521Snjl	zfs_znode_cache_constructor(zp, zfsvfs->z_vfs, 0);
326193267Sjkim
327298714Sjkim	ASSERT(zp->z_dirlocks == NULL);
328117521Snjl
329117521Snjl	zp->z_phys = db->db_data;
330193267Sjkim	zp->z_zfsvfs = zfsvfs;
331193267Sjkim	zp->z_unlinked = 0;
332117521Snjl	zp->z_atime_dirty = 0;
33367754Smsmith	zp->z_dbuf_held = 0;
33467754Smsmith	zp->z_mapcnt = 0;
335117521Snjl	zp->z_last_itx = 0;
336117521Snjl	zp->z_dbuf = db;
33767754Smsmith	zp->z_id = obj_num;
33867754Smsmith	zp->z_blksz = blksz;
339117521Snjl	zp->z_seq = 0x7A4653;
340114237Snjl	zp->z_sync_cnt = 0;
341193267Sjkim
342298714Sjkim	mutex_enter(&zfsvfs->z_znodes_lock);
343117521Snjl	list_insert_tail(&zfsvfs->z_all_znodes, zp);
344117521Snjl	mutex_exit(&zfsvfs->z_znodes_lock);
345117521Snjl
346117521Snjl	vp = ZTOV(zp);
347117521Snjl	if (vp == NULL)
348117521Snjl		return (zp);
349117521Snjl
350193267Sjkim	vp->v_vflag |= VV_FORCEINSMQ;
351298714Sjkim	error = insmntque(vp, zfsvfs->z_vfs);
352117521Snjl	vp->v_vflag &= ~VV_FORCEINSMQ;
353117521Snjl	KASSERT(error == 0, ("insmntque() failed: error %d", error));
354117521Snjl
355117521Snjl	vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode);
356117521Snjl	switch (vp->v_type) {
357212761Sjkim	case VDIR:
358117521Snjl		zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
359212761Sjkim		break;
360212761Sjkim	case VFIFO:
361212761Sjkim		vp->v_op = &zfs_fifoops;
362212761Sjkim		break;
363212761Sjkim	}
364212761Sjkim
365117521Snjl	return (zp);
36680062Smsmith}
36767754Smsmith
36867754Smsmithstatic void
36967754Smsmithzfs_znode_dmu_init(znode_t *zp)
37067754Smsmith{
37177424Smsmith	znode_t		*nzp;
37267754Smsmith	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
373167802Sjkim	dmu_buf_t	*db = zp->z_dbuf;
374167802Sjkim
375167802Sjkim	mutex_enter(&zp->z_lock);
376167802Sjkim
377167802Sjkim	nzp = dmu_buf_set_user_ie(db, zp, &zp->z_phys, znode_pageout_func);
378167802Sjkim
379167802Sjkim	/*
380167802Sjkim	 * there should be no
381167802Sjkim	 * concurrent zgets on this object.
382167802Sjkim	 */
383167802Sjkim	ASSERT3P(nzp, ==, NULL);
384167802Sjkim
385167802Sjkim	/*
386167802Sjkim	 * Slap on VROOT if we are the root znode
387167802Sjkim	 */
388167802Sjkim	if (zp->z_id == zfsvfs->z_root) {
389241973Sjkim		ZTOV(zp)->v_flag |= VROOT;
390241973Sjkim	}
391193267Sjkim
392197104Sjkim	ASSERT(zp->z_dbuf_held == 0);
393167802Sjkim	zp->z_dbuf_held = 1;
394167802Sjkim	VFS_HOLD(zfsvfs->z_vfs);
395193267Sjkim	mutex_exit(&zp->z_lock);
396193267Sjkim}
397167802Sjkim
398167802Sjkim/*
399167802Sjkim * Create a new DMU object to hold a zfs znode.
400167802Sjkim *
401167802Sjkim *	IN:	dzp	- parent directory for new znode
402167802Sjkim *		vap	- file attributes for new znode
403197104Sjkim *		tx	- dmu transaction id for zap operations
404197104Sjkim *		cr	- credentials of caller
405197104Sjkim *		flag	- flags:
406197104Sjkim *			  IS_ROOT_NODE	- new object will be root
407167802Sjkim *			  IS_XATTR	- new object is an attribute
408167802Sjkim *			  IS_REPLAY	- intent log replay
409167802Sjkim *
410167802Sjkim *	OUT:	oid	- ID of created object
411193267Sjkim *
412193267Sjkim */
413167802Sjkimvoid
414167802Sjkimzfs_mknode(znode_t *dzp, vattr_t *vap, uint64_t *oid, dmu_tx_t *tx, cred_t *cr,
415167802Sjkim	uint_t flag, znode_t **zpp, int bonuslen)
416167802Sjkim{
417167802Sjkim	dmu_buf_t	*dbp;
418167802Sjkim	znode_phys_t	*pzp;
419167802Sjkim	znode_t		*zp;
420167802Sjkim	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
421167802Sjkim	timestruc_t	now;
422167802Sjkim	uint64_t	gen;
423197104Sjkim	int		err;
424167802Sjkim
425167802Sjkim	ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
426167802Sjkim
427167802Sjkim	if (zfsvfs->z_assign >= TXG_INITIAL) {		/* ZIL replay */
428167802Sjkim		*oid = vap->va_nodeid;
429167802Sjkim		flag |= IS_REPLAY;
430167802Sjkim		now = vap->va_ctime;		/* see zfs_replay_create() */
431167802Sjkim		gen = vap->va_nblocks;		/* ditto */
432167802Sjkim	} else {
433167802Sjkim		*oid = 0;
434167802Sjkim		gethrestime(&now);
435167802Sjkim		gen = dmu_tx_get_txg(tx);
436167802Sjkim	}
43784491Smsmith
43884491Smsmith	/*
439138287Smarks	 * Create a new DMU object.
44099679Siwasaki	 */
44184491Smsmith	/*
44284491Smsmith	 * There's currently no mechanism for pre-reading the blocks that will
44384491Smsmith	 * be to needed allocate a new object, so we accept the small chance
44484491Smsmith	 * that there will be an i/o error and we will fail one of the
44584491Smsmith	 * assertions below.
446151937Sjkim	 */
44784491Smsmith	if (vap->va_type == VDIR) {
44884491Smsmith		if (flag & IS_REPLAY) {
44984491Smsmith			err = zap_create_claim(zfsvfs->z_os, *oid,
45084491Smsmith			    DMU_OT_DIRECTORY_CONTENTS,
45184491Smsmith			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
45284491Smsmith			ASSERT3U(err, ==, 0);
45384491Smsmith		} else {
45484491Smsmith			*oid = zap_create(zfsvfs->z_os,
45584491Smsmith			    DMU_OT_DIRECTORY_CONTENTS,
45684491Smsmith			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
45784491Smsmith		}
45884491Smsmith	} else {
459167802Sjkim		if (flag & IS_REPLAY) {
46084491Smsmith			err = dmu_object_claim(zfsvfs->z_os, *oid,
46184491Smsmith			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
46284491Smsmith			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
46384491Smsmith			ASSERT3U(err, ==, 0);
46484491Smsmith		} else {
46584491Smsmith			*oid = dmu_object_alloc(zfsvfs->z_os,
46684491Smsmith			    DMU_OT_PLAIN_FILE_CONTENTS, 0,
46784491Smsmith			    DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
46884491Smsmith		}
46984491Smsmith	}
470138287Smarks	VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, *oid, NULL, &dbp));
47199679Siwasaki	dmu_buf_will_dirty(dbp, tx);
47284491Smsmith
47384491Smsmith	/*
47484491Smsmith	 * Initialize the znode physical data to zero.
47584491Smsmith	 */
47684491Smsmith	ASSERT(dbp->db_size >= sizeof (znode_phys_t));
477151937Sjkim	bzero(dbp->db_data, dbp->db_size);
47884491Smsmith	pzp = dbp->db_data;
47984491Smsmith
48084491Smsmith	/*
48184491Smsmith	 * If this is the root, fix up the half-initialized parent pointer
48284491Smsmith	 * to reference the just-allocated physical data area.
48384491Smsmith	 */
48484491Smsmith	if (flag & IS_ROOT_NODE) {
48584491Smsmith		dzp->z_phys = pzp;
48684491Smsmith		dzp->z_id = *oid;
48784491Smsmith	}
48884491Smsmith
48984491Smsmith	/*
490167802Sjkim	 * If parent is an xattr, so am I.
49184491Smsmith	 */
49284491Smsmith	if (dzp->z_phys->zp_flags & ZFS_XATTR)
49384491Smsmith		flag |= IS_XATTR;
49484491Smsmith
49584491Smsmith	if (vap->va_type == VBLK || vap->va_type == VCHR) {
49684491Smsmith		pzp->zp_rdev = zfs_expldev(vap->va_rdev);
49784491Smsmith	}
49884491Smsmith
49967754Smsmith	if (vap->va_type == VDIR) {
50067754Smsmith		pzp->zp_size = 2;		/* contents ("." and "..") */
501138287Smarks		pzp->zp_links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
50299679Siwasaki	}
50367754Smsmith
50467754Smsmith	pzp->zp_parent = dzp->z_id;
50567754Smsmith	if (flag & IS_XATTR)
50667754Smsmith		pzp->zp_flags |= ZFS_XATTR;
50767754Smsmith
508151937Sjkim	pzp->zp_gen = gen;
50967754Smsmith
51077424Smsmith	ZFS_TIME_ENCODE(&now, pzp->zp_crtime);
51167754Smsmith	ZFS_TIME_ENCODE(&now, pzp->zp_ctime);
51267754Smsmith
51367754Smsmith	if (vap->va_mask & AT_ATIME) {
51467754Smsmith		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
51567754Smsmith	} else {
51667754Smsmith		ZFS_TIME_ENCODE(&now, pzp->zp_atime);
51767754Smsmith	}
51867754Smsmith
519167802Sjkim	if (vap->va_mask & AT_MTIME) {
52067754Smsmith		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
52177424Smsmith	} else {
52267754Smsmith		ZFS_TIME_ENCODE(&now, pzp->zp_mtime);
52367754Smsmith	}
52467754Smsmith
52567754Smsmith	pzp->zp_mode = MAKEIMODE(vap->va_type, vap->va_mode);
52667754Smsmith	zp = zfs_znode_alloc(zfsvfs, dbp, *oid, 0);
52767754Smsmith
52867754Smsmith	zfs_perm_init(zp, dzp, flag, vap, tx, cr);
52967754Smsmith
53067754Smsmith	if (zpp) {
53167754Smsmith		kmutex_t *hash_mtx = ZFS_OBJ_MUTEX(zp);
53267754Smsmith
53367754Smsmith		mutex_enter(hash_mtx);
53467754Smsmith		zfs_znode_dmu_init(zp);
53577424Smsmith		mutex_exit(hash_mtx);
53667754Smsmith
53767754Smsmith		*zpp = zp;
53867754Smsmith	} else {
53999679Siwasaki		if (ZTOV(zp) != NULL) {
54099679Siwasaki			ZTOV(zp)->v_count = 0;
54167754Smsmith			VOP_UNLOCK(ZTOV(zp), 0);
54267754Smsmith		}
54367754Smsmith		dmu_buf_rele(dbp, NULL);
54467754Smsmith		zfs_znode_free(zp);
54567754Smsmith	}
54667754Smsmith}
54767754Smsmith
54867754Smsmithint
54967754Smsmithzfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
550193267Sjkim{
55167754Smsmith	dmu_object_info_t doi;
552193267Sjkim	dmu_buf_t	*db;
55367754Smsmith	znode_t		*zp;
55467754Smsmith	vnode_t		*vp;
555167802Sjkim	int err;
556167802Sjkim
557167802Sjkim	*zpp = NULL;
55867754Smsmith
55967754Smsmith	ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
56067754Smsmith
56167754Smsmith	err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
56267754Smsmith	if (err) {
56367754Smsmith		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
56467754Smsmith		return (err);
56577424Smsmith	}
56677424Smsmith
56777424Smsmith	dmu_object_info_from_db(db, &doi);
56877424Smsmith	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
56967754Smsmith	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
57067754Smsmith		dmu_buf_rele(db, NULL);
57167754Smsmith		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
572167802Sjkim		return (EINVAL);
57367754Smsmith	}
57467754Smsmith
57567754Smsmith	ASSERT(db->db_object == obj_num);
57667754Smsmith	ASSERT(db->db_offset == -1);
57767754Smsmith	ASSERT(db->db_data != NULL);
57867754Smsmith
57967754Smsmith	zp = dmu_buf_get_user(db);
58087031Smsmith
58187031Smsmith	if (zp != NULL) {
58287031Smsmith		mutex_enter(&zp->z_lock);
58387031Smsmith
58487031Smsmith		ASSERT3U(zp->z_id, ==, obj_num);
585298714Sjkim		if (zp->z_unlinked) {
58699679Siwasaki			dmu_buf_rele(db, NULL);
587209746Sjkim			mutex_exit(&zp->z_lock);
58867754Smsmith			ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
58967754Smsmith			return (ENOENT);
59067754Smsmith		} else if (zp->z_dbuf_held) {
59199679Siwasaki			dmu_buf_rele(db, NULL);
592298714Sjkim		} else {
59367754Smsmith			zp->z_dbuf_held = 1;
59467754Smsmith			VFS_HOLD(zfsvfs->z_vfs);
59567754Smsmith		}
596117521Snjl
597117521Snjl		if (ZTOV(zp) != NULL)
59867754Smsmith			VN_HOLD(ZTOV(zp));
59977424Smsmith		else {
60067754Smsmith			err = getnewvnode("zfs", zfsvfs->z_vfs, &zfs_vnodeops,
60167754Smsmith			    &zp->z_vnode);
602117521Snjl			ASSERT(err == 0);
603117521Snjl			vp = ZTOV(zp);
60467754Smsmith			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
60567754Smsmith			vp->v_data = (caddr_t)zp;
60667754Smsmith			VN_LOCK_AREC(vp);
60767754Smsmith			VN_LOCK_ASHARE(vp);
60867754Smsmith			vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode);
60967754Smsmith			if (vp->v_type == VDIR)
610298714Sjkim				zp->z_zn_prefetch = B_TRUE;	/* z_prefetch default is enabled */
61167754Smsmith			vp->v_vflag |= VV_FORCEINSMQ;
61299679Siwasaki			err = insmntque(vp, zfsvfs->z_vfs);
613200553Sjkim			vp->v_vflag &= ~VV_FORCEINSMQ;
614200553Sjkim			KASSERT(err == 0, ("insmntque() failed: error %d", err));
615200553Sjkim			VOP_UNLOCK(vp, 0);
616200553Sjkim		}
617200553Sjkim		mutex_exit(&zp->z_lock);
618200553Sjkim		ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
619200553Sjkim		*zpp = zp;
620200553Sjkim		return (0);
621217365Sjkim	}
622200553Sjkim
623217365Sjkim	/*
624200553Sjkim	 * Not found create new znode/vnode
625200553Sjkim	 */
626200553Sjkim	zp = zfs_znode_alloc(zfsvfs, db, obj_num, doi.doi_data_block_size);
62799679Siwasaki	ASSERT3U(zp->z_id, ==, obj_num);
628250838Sjkim	zfs_znode_dmu_init(zp);
62999679Siwasaki	ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
630250838Sjkim	*zpp = zp;
63199679Siwasaki	VOP_UNLOCK(vp, 0);
63267754Smsmith	return (0);
63367754Smsmith}
634298714Sjkim
635298714Sjkimvoid
63667754Smsmithzfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
637298714Sjkim{
63899679Siwasaki	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
639298714Sjkim	int error;
640298714Sjkim
641298714Sjkim	ZFS_OBJ_HOLD_ENTER(zfsvfs, zp->z_id);
64299679Siwasaki	if (zp->z_phys->zp_acl.z_acl_extern_obj) {
643298714Sjkim		error = dmu_object_free(zfsvfs->z_os,
644298714Sjkim		    zp->z_phys->zp_acl.z_acl_extern_obj, tx);
64567754Smsmith		ASSERT3U(error, ==, 0);
646298714Sjkim	}
647298714Sjkim	error = dmu_object_free(zfsvfs->z_os, zp->z_id, tx);
648298714Sjkim	ASSERT3U(error, ==, 0);
649298714Sjkim	zp->z_dbuf_held = 0;
650298714Sjkim	ZFS_OBJ_HOLD_EXIT(zfsvfs, zp->z_id);
651298714Sjkim	dmu_buf_rele(zp->z_dbuf, NULL);
652298714Sjkim}
653298714Sjkim
654123315Snjlvoid
655298714Sjkimzfs_zinactive(znode_t *zp)
656123315Snjl{
657298714Sjkim	vnode_t	*vp = ZTOV(zp);
658123315Snjl	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
659298714Sjkim	uint64_t z_id = zp->z_id;
660123315Snjl
661298714Sjkim	ASSERT(zp->z_dbuf_held && zp->z_phys);
662298714Sjkim
663298714Sjkim	/*
664298714Sjkim	 * Don't allow a zfs_zget() while were trying to release this znode
665123315Snjl	 */
666298714Sjkim	ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
667123315Snjl
66867754Smsmith	mutex_enter(&zp->z_lock);
66967754Smsmith	VI_LOCK(vp);
670298714Sjkim	if (vp->v_count > 0) {
67199679Siwasaki		/*
67267754Smsmith		 * If the hold count is greater than zero, somebody has
67367754Smsmith		 * obtained a new reference on this znode while we were
674193267Sjkim		 * processing it here, so we are done.
675193267Sjkim		 */
676209746Sjkim		VI_UNLOCK(vp);
67799679Siwasaki		mutex_exit(&zp->z_lock);
67867754Smsmith		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
679117521Snjl		return;
680117521Snjl	}
68182367Smsmith	VI_UNLOCK(vp);
68282367Smsmith
68377424Smsmith	/*
68467754Smsmith	 * If this was the last reference to a file with no links,
68567754Smsmith	 * remove the file from the file system.
68667754Smsmith	 */
687	if (zp->z_unlinked) {
688		ZTOV(zp) = NULL;
689		mutex_exit(&zp->z_lock);
690		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
691		ASSERT(vp->v_count == 0);
692		vrecycle(vp, curthread);
693		zfs_rmnode(zp);
694		VFS_RELE(zfsvfs->z_vfs);
695		return;
696	}
697	ASSERT(zp->z_phys);
698	ASSERT(zp->z_dbuf_held);
699	mutex_exit(&zp->z_lock);
700	ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
701}
702
703void
704zfs_znode_free(znode_t *zp)
705{
706	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
707
708	mutex_enter(&zfsvfs->z_znodes_lock);
709	list_remove(&zfsvfs->z_all_znodes, zp);
710	mutex_exit(&zfsvfs->z_znodes_lock);
711
712	kmem_cache_free(znode_cache, zp);
713}
714
715void
716zfs_time_stamper_locked(znode_t *zp, uint_t flag, dmu_tx_t *tx)
717{
718	timestruc_t	now;
719
720	ASSERT(MUTEX_HELD(&zp->z_lock));
721
722	gethrestime(&now);
723
724	if (tx) {
725		dmu_buf_will_dirty(zp->z_dbuf, tx);
726		zp->z_atime_dirty = 0;
727		zp->z_seq++;
728	} else {
729		zp->z_atime_dirty = 1;
730	}
731
732	if (flag & AT_ATIME)
733		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_atime);
734
735	if (flag & AT_MTIME)
736		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_mtime);
737
738	if (flag & AT_CTIME)
739		ZFS_TIME_ENCODE(&now, zp->z_phys->zp_ctime);
740}
741
742/*
743 * Update the requested znode timestamps with the current time.
744 * If we are in a transaction, then go ahead and mark the znode
745 * dirty in the transaction so the timestamps will go to disk.
746 * Otherwise, we will get pushed next time the znode is updated
747 * in a transaction, or when this znode eventually goes inactive.
748 *
749 * Why is this OK?
750 *  1 - Only the ACCESS time is ever updated outside of a transaction.
751 *  2 - Multiple consecutive updates will be collapsed into a single
752 *	znode update by the transaction grouping semantics of the DMU.
753 */
754void
755zfs_time_stamper(znode_t *zp, uint_t flag, dmu_tx_t *tx)
756{
757	mutex_enter(&zp->z_lock);
758	zfs_time_stamper_locked(zp, flag, tx);
759	mutex_exit(&zp->z_lock);
760}
761
762/*
763 * Grow the block size for a file.
764 *
765 *	IN:	zp	- znode of file to free data in.
766 *		size	- requested block size
767 *		tx	- open transaction.
768 *
769 * NOTE: this function assumes that the znode is write locked.
770 */
771void
772zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
773{
774	int		error;
775	u_longlong_t	dummy;
776
777	if (size <= zp->z_blksz)
778		return;
779	/*
780	 * If the file size is already greater than the current blocksize,
781	 * we will not grow.  If there is more than one block in a file,
782	 * the blocksize cannot change.
783	 */
784	if (zp->z_blksz && zp->z_phys->zp_size > zp->z_blksz)
785		return;
786
787	error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
788	    size, 0, tx);
789	if (error == ENOTSUP)
790		return;
791	ASSERT3U(error, ==, 0);
792
793	/* What blocksize did we actually get? */
794	dmu_object_size_from_db(zp->z_dbuf, &zp->z_blksz, &dummy);
795}
796
797/*
798 * Free space in a file.
799 *
800 *	IN:	zp	- znode of file to free data in.
801 *		off	- start of section to free.
802 *		len	- length of section to free (0 => to EOF).
803 *		flag	- current file open mode flags.
804 *
805 * 	RETURN:	0 if success
806 *		error code if failure
807 */
808int
809zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
810{
811	vnode_t *vp = ZTOV(zp);
812	dmu_tx_t *tx;
813	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
814	zilog_t *zilog = zfsvfs->z_log;
815	rl_t *rl;
816	uint64_t end = off + len;
817	uint64_t size, new_blksz;
818	int error;
819
820	if (ZTOV(zp)->v_type == VFIFO)
821		return (0);
822
823	/*
824	 * If we will change zp_size then lock the whole file,
825	 * otherwise just lock the range being freed.
826	 */
827	if (len == 0 || off + len > zp->z_phys->zp_size) {
828		rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
829	} else {
830		rl = zfs_range_lock(zp, off, len, RL_WRITER);
831		/* recheck, in case zp_size changed */
832		if (off + len > zp->z_phys->zp_size) {
833			/* lost race: file size changed, lock whole file */
834			zfs_range_unlock(rl);
835			rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
836		}
837	}
838
839	/*
840	 * Nothing to do if file already at desired length.
841	 */
842	size = zp->z_phys->zp_size;
843	if (len == 0 && size == off && off != 0) {
844		zfs_range_unlock(rl);
845		return (0);
846	}
847
848	tx = dmu_tx_create(zfsvfs->z_os);
849	dmu_tx_hold_bonus(tx, zp->z_id);
850	new_blksz = 0;
851	if (end > size &&
852	    (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
853		/*
854		 * We are growing the file past the current block size.
855		 */
856		if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
857			ASSERT(!ISP2(zp->z_blksz));
858			new_blksz = MIN(end, SPA_MAXBLOCKSIZE);
859		} else {
860			new_blksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
861		}
862		dmu_tx_hold_write(tx, zp->z_id, 0, MIN(end, new_blksz));
863	} else if (off < size) {
864		/*
865		 * If len == 0, we are truncating the file.
866		 */
867		dmu_tx_hold_free(tx, zp->z_id, off, len ? len : DMU_OBJECT_END);
868	}
869
870	error = dmu_tx_assign(tx, zfsvfs->z_assign);
871	if (error) {
872		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT)
873			dmu_tx_wait(tx);
874		dmu_tx_abort(tx);
875		zfs_range_unlock(rl);
876		return (error);
877	}
878
879	if (new_blksz)
880		zfs_grow_blocksize(zp, new_blksz, tx);
881
882	if (end > size || len == 0)
883		zp->z_phys->zp_size = end;
884
885	if (off < size) {
886		objset_t *os = zfsvfs->z_os;
887		uint64_t rlen = len;
888
889		if (len == 0)
890			rlen = -1;
891		else if (end > size)
892			rlen = size - off;
893		VERIFY(0 == dmu_free_range(os, zp->z_id, off, rlen, tx));
894	}
895
896	if (log) {
897		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
898		zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
899	}
900
901	zfs_range_unlock(rl);
902
903	dmu_tx_commit(tx);
904
905	/*
906	 * Clear any mapped pages in the truncated region.  This has to
907	 * happen outside of the transaction to avoid the possibility of
908	 * a deadlock with someone trying to push a page that we are
909	 * about to invalidate.
910	 */
911	rw_enter(&zp->z_map_lock, RW_WRITER);
912	if (end > size)
913		vnode_pager_setsize(vp, end);
914	else if (len == 0) {
915#if 0
916		error = vtruncbuf(vp, curthread->td_ucred, curthread, end, PAGE_SIZE);
917#else
918		error = vinvalbuf(vp, V_SAVE, curthread, 0, 0);
919		vnode_pager_setsize(vp, end);
920#endif
921	}
922	rw_exit(&zp->z_map_lock);
923
924	return (0);
925}
926
927void
928zfs_create_fs(objset_t *os, cred_t *cr, dmu_tx_t *tx)
929{
930	zfsvfs_t	zfsvfs;
931	uint64_t	moid, doid, roid = 0;
932	uint64_t	version = ZPL_VERSION;
933	int		error;
934	znode_t		*rootzp = NULL;
935	vattr_t		vattr;
936
937	/*
938	 * First attempt to create master node.
939	 */
940	/*
941	 * In an empty objset, there are no blocks to read and thus
942	 * there can be no i/o errors (which we assert below).
943	 */
944	moid = MASTER_NODE_OBJ;
945	error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
946	    DMU_OT_NONE, 0, tx);
947	ASSERT(error == 0);
948
949	/*
950	 * Set starting attributes.
951	 */
952
953	error = zap_update(os, moid, ZPL_VERSION_OBJ, 8, 1, &version, tx);
954	ASSERT(error == 0);
955
956	/*
957	 * Create a delete queue.
958	 */
959	doid = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
960
961	error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &doid, tx);
962	ASSERT(error == 0);
963
964	/*
965	 * Create root znode.  Create minimal znode/vnode/zfsvfs
966	 * to allow zfs_mknode to work.
967	 */
968	vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
969	vattr.va_type = VDIR;
970	vattr.va_mode = S_IFDIR|0755;
971	vattr.va_uid = UID_ROOT;
972	vattr.va_gid = GID_WHEEL;
973
974	rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
975	zfs_znode_cache_constructor(rootzp, NULL, 0);
976	rootzp->z_zfsvfs = &zfsvfs;
977	rootzp->z_unlinked = 0;
978	rootzp->z_atime_dirty = 0;
979	rootzp->z_dbuf_held = 0;
980
981	bzero(&zfsvfs, sizeof (zfsvfs_t));
982
983	zfsvfs.z_os = os;
984	zfsvfs.z_assign = TXG_NOWAIT;
985	zfsvfs.z_parent = &zfsvfs;
986
987	mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
988	list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
989	    offsetof(znode_t, z_link_node));
990
991	zfs_mknode(rootzp, &vattr, &roid, tx, cr, IS_ROOT_NODE, NULL, 0);
992	ASSERT3U(rootzp->z_id, ==, roid);
993	error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &roid, tx);
994	ASSERT(error == 0);
995
996	mutex_destroy(&zfsvfs.z_znodes_lock);
997	kmem_cache_free(znode_cache, rootzp);
998}
999#endif /* _KERNEL */
1000
1001/*
1002 * Given an object number, return its parent object number and whether
1003 * or not the object is an extended attribute directory.
1004 */
1005static int
1006zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir)
1007{
1008	dmu_buf_t *db;
1009	dmu_object_info_t doi;
1010	znode_phys_t *zp;
1011	int error;
1012
1013	if ((error = dmu_bonus_hold(osp, obj, FTAG, &db)) != 0)
1014		return (error);
1015
1016	dmu_object_info_from_db(db, &doi);
1017	if (doi.doi_bonus_type != DMU_OT_ZNODE ||
1018	    doi.doi_bonus_size < sizeof (znode_phys_t)) {
1019		dmu_buf_rele(db, FTAG);
1020		return (EINVAL);
1021	}
1022
1023	zp = db->db_data;
1024	*pobjp = zp->zp_parent;
1025	*is_xattrdir = ((zp->zp_flags & ZFS_XATTR) != 0) &&
1026	    S_ISDIR(zp->zp_mode);
1027	dmu_buf_rele(db, FTAG);
1028
1029	return (0);
1030}
1031
1032int
1033zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
1034{
1035	char *path = buf + len - 1;
1036	int error;
1037
1038	*path = '\0';
1039
1040	for (;;) {
1041		uint64_t pobj;
1042		char component[MAXNAMELEN + 2];
1043		size_t complen;
1044		int is_xattrdir;
1045
1046		if ((error = zfs_obj_to_pobj(osp, obj, &pobj,
1047		    &is_xattrdir)) != 0)
1048			break;
1049
1050		if (pobj == obj) {
1051			if (path[0] != '/')
1052				*--path = '/';
1053			break;
1054		}
1055
1056		component[0] = '/';
1057		if (is_xattrdir) {
1058			(void) sprintf(component + 1, "<xattrdir>");
1059		} else {
1060			error = zap_value_search(osp, pobj, obj, component + 1);
1061			if (error != 0)
1062				break;
1063		}
1064
1065		complen = strlen(component);
1066		path -= complen;
1067		ASSERT(path >= buf);
1068		bcopy(component, path, complen);
1069		obj = pobj;
1070	}
1071
1072	if (error == 0)
1073		(void) memmove(buf, path, buf + len - path);
1074	return (error);
1075}
1076