xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_dir.c (revision 7046:361307ae060d)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51484Sek110237  * Common Development and Distribution License (the "License").
61484Sek110237  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
225771Sjp151216  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens #include <sys/types.h>
29789Sahrens #include <sys/param.h>
30789Sahrens #include <sys/time.h>
31789Sahrens #include <sys/systm.h>
32789Sahrens #include <sys/sysmacros.h>
33789Sahrens #include <sys/resource.h>
34789Sahrens #include <sys/vfs.h>
35789Sahrens #include <sys/vnode.h>
36789Sahrens #include <sys/file.h>
37789Sahrens #include <sys/mode.h>
38789Sahrens #include <sys/kmem.h>
39789Sahrens #include <sys/uio.h>
40789Sahrens #include <sys/pathname.h>
41789Sahrens #include <sys/cmn_err.h>
42789Sahrens #include <sys/errno.h>
43789Sahrens #include <sys/stat.h>
44789Sahrens #include <sys/unistd.h>
455498Stimh #include <sys/sunddi.h>
46789Sahrens #include <sys/random.h>
47789Sahrens #include <sys/policy.h>
48789Sahrens #include <sys/zfs_dir.h>
49789Sahrens #include <sys/zfs_acl.h>
50789Sahrens #include <sys/fs/zfs.h>
51789Sahrens #include "fs/fs_subr.h"
52789Sahrens #include <sys/zap.h>
53789Sahrens #include <sys/dmu.h>
54789Sahrens #include <sys/atomic.h>
55789Sahrens #include <sys/zfs_ctldir.h>
565331Samw #include <sys/zfs_fuid.h>
571484Sek110237 #include <sys/dnlc.h>
585331Samw #include <sys/extdirent.h>
595331Samw 
605331Samw /*
615331Samw  * zfs_match_find() is used by zfs_dirent_lock() to peform zap lookups
625331Samw  * of names after deciding which is the appropriate lookup interface.
635331Samw  */
645331Samw static int
655331Samw zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, char *name, boolean_t exact,
665331Samw     boolean_t update, int *deflags, pathname_t *rpnp, uint64_t *zoid)
675331Samw {
685331Samw 	int error;
695331Samw 
705331Samw 	if (zfsvfs->z_norm) {
715331Samw 		matchtype_t mt = MT_FIRST;
725331Samw 		boolean_t conflict = B_FALSE;
735331Samw 		size_t bufsz = 0;
745331Samw 		char *buf = NULL;
755331Samw 
765331Samw 		if (rpnp) {
776492Stimh 			buf = rpnp->pn_buf;
785331Samw 			bufsz = rpnp->pn_bufsize;
795331Samw 		}
805331Samw 		if (exact)
815331Samw 			mt = MT_EXACT;
825331Samw 		/*
835331Samw 		 * In the non-mixed case we only expect there would ever
845331Samw 		 * be one match, but we need to use the normalizing lookup.
855331Samw 		 */
865331Samw 		error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1,
875331Samw 		    zoid, mt, buf, bufsz, &conflict);
886492Stimh 		if (!error && deflags)
895331Samw 			*deflags = conflict ? ED_CASE_CONFLICT : 0;
905331Samw 	} else {
915331Samw 		error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid);
925331Samw 	}
935331Samw 	*zoid = ZFS_DIRENT_OBJ(*zoid);
945331Samw 
955331Samw 	if (error == ENOENT && update)
965331Samw 		dnlc_update(ZTOV(dzp), name, DNLC_NO_VNODE);
975331Samw 
985331Samw 	return (error);
995331Samw }
100789Sahrens 
101789Sahrens /*
102789Sahrens  * Lock a directory entry.  A dirlock on <dzp, name> protects that name
103789Sahrens  * in dzp's directory zap object.  As long as you hold a dirlock, you can
104789Sahrens  * assume two things: (1) dzp cannot be reaped, and (2) no other thread
105789Sahrens  * can change the zap entry for (i.e. link or unlink) this name.
106789Sahrens  *
107789Sahrens  * Input arguments:
108789Sahrens  *	dzp	- znode for directory
109789Sahrens  *	name	- name of entry to lock
110789Sahrens  *	flag	- ZNEW: if the entry already exists, fail with EEXIST.
111789Sahrens  *		  ZEXISTS: if the entry does not exist, fail with ENOENT.
112789Sahrens  *		  ZSHARED: allow concurrent access with other ZSHARED callers.
113789Sahrens  *		  ZXATTR: we want dzp's xattr directory
1145331Samw  *		  ZCILOOK: On a mixed sensitivity file system,
1155331Samw  *			   this lookup should be case-insensitive.
1165331Samw  *		  ZCIEXACT: On a purely case-insensitive file system,
1175331Samw  *			    this lookup should be case-sensitive.
1185331Samw  *		  ZRENAMING: we are locking for renaming, force narrow locks
119789Sahrens  *
120789Sahrens  * Output arguments:
121789Sahrens  *	zpp	- pointer to the znode for the entry (NULL if there isn't one)
122789Sahrens  *	dlpp	- pointer to the dirlock for this entry (NULL on error)
1235331Samw  *      direntflags - (case-insensitive lookup only)
1245331Samw  *		flags if multiple case-sensitive matches exist in directory
1255331Samw  *      realpnp     - (case-insensitive lookup only)
1265331Samw  *		actual name matched within the directory
127789Sahrens  *
128789Sahrens  * Return value: 0 on success or errno on failure.
129789Sahrens  *
130789Sahrens  * NOTE: Always checks for, and rejects, '.' and '..'.
1315331Samw  * NOTE: For case-insensitive file systems we take wide locks (see below),
1325331Samw  *	 but return znode pointers to a single match.
133789Sahrens  */
134789Sahrens int
135789Sahrens zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
1365331Samw     int flag, int *direntflags, pathname_t *realpnp)
137789Sahrens {
138789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
139789Sahrens 	zfs_dirlock_t	*dl;
1405331Samw 	boolean_t	update;
1415331Samw 	boolean_t	exact;
142789Sahrens 	uint64_t	zoid;
1435331Samw 	vnode_t		*vp = NULL;
1445331Samw 	int		error = 0;
1455331Samw 	int		cmpflags;
146789Sahrens 
147789Sahrens 	*zpp = NULL;
148789Sahrens 	*dlpp = NULL;
149789Sahrens 
150789Sahrens 	/*
151789Sahrens 	 * Verify that we are not trying to lock '.', '..', or '.zfs'
152789Sahrens 	 */
153789Sahrens 	if (name[0] == '.' &&
154789Sahrens 	    (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) ||
155789Sahrens 	    zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0)
156789Sahrens 		return (EEXIST);
157789Sahrens 
158789Sahrens 	/*
1595331Samw 	 * Case sensitivity and normalization preferences are set when
1605331Samw 	 * the file system is created.  These are stored in the
1615331Samw 	 * zfsvfs->z_case and zfsvfs->z_norm fields.  These choices
1625331Samw 	 * affect what vnodes can be cached in the DNLC, how we
1635331Samw 	 * perform zap lookups, and the "width" of our dirlocks.
1645331Samw 	 *
1655331Samw 	 * A normal dirlock locks a single name.  Note that with
1665331Samw 	 * normalization a name can be composed multiple ways, but
1675331Samw 	 * when normalized, these names all compare equal.  A wide
1685331Samw 	 * dirlock locks multiple names.  We need these when the file
1695331Samw 	 * system is supporting mixed-mode access.  It is sometimes
1705331Samw 	 * necessary to lock all case permutations of file name at
1715331Samw 	 * once so that simultaneous case-insensitive/case-sensitive
1725331Samw 	 * behaves as rationally as possible.
1735331Samw 	 */
1745331Samw 
1755331Samw 	/*
1765331Samw 	 * Decide if exact matches should be requested when performing
1775331Samw 	 * a zap lookup on file systems supporting case-insensitive
1785331Samw 	 * access.
1795331Samw 	 */
1805498Stimh 	exact =
1815498Stimh 	    ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE) && (flag & ZCIEXACT)) ||
1825498Stimh 	    ((zfsvfs->z_case == ZFS_CASE_MIXED) && !(flag & ZCILOOK));
1835331Samw 
1845331Samw 	/*
1855331Samw 	 * Only look in or update the DNLC if we are looking for the
1865331Samw 	 * name on a file system that does not require normalization
1875331Samw 	 * or case folding.  We can also look there if we happen to be
1885331Samw 	 * on a non-normalizing, mixed sensitivity file system IF we
1895331Samw 	 * are looking for the exact name.
1905331Samw 	 *
1915331Samw 	 * Maybe can add TO-UPPERed version of name to dnlc in ci-only
1925331Samw 	 * case for performance improvement?
1935331Samw 	 */
1945331Samw 	update = !zfsvfs->z_norm ||
1955498Stimh 	    ((zfsvfs->z_case == ZFS_CASE_MIXED) &&
1965331Samw 	    !(zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER) && !(flag & ZCILOOK));
1975331Samw 
1985331Samw 	/*
1995331Samw 	 * ZRENAMING indicates we are in a situation where we should
2005331Samw 	 * take narrow locks regardless of the file system's
2015331Samw 	 * preferences for normalizing and case folding.  This will
2025331Samw 	 * prevent us deadlocking trying to grab the same wide lock
2035331Samw 	 * twice if the two names happen to be case-insensitive
2045331Samw 	 * matches.
2055331Samw 	 */
2065331Samw 	if (flag & ZRENAMING)
2075331Samw 		cmpflags = 0;
2085331Samw 	else
2095331Samw 		cmpflags = zfsvfs->z_norm;
2105331Samw 
2115331Samw 	/*
212789Sahrens 	 * Wait until there are no locks on this name.
213789Sahrens 	 */
2143897Smaybee 	rw_enter(&dzp->z_name_lock, RW_READER);
215789Sahrens 	mutex_enter(&dzp->z_lock);
216789Sahrens 	for (;;) {
2173461Sahrens 		if (dzp->z_unlinked) {
218789Sahrens 			mutex_exit(&dzp->z_lock);
2193897Smaybee 			rw_exit(&dzp->z_name_lock);
220789Sahrens 			return (ENOENT);
221789Sahrens 		}
2225331Samw 		for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next) {
2235331Samw 			if ((u8_strcmp(name, dl->dl_name, 0, cmpflags,
2245331Samw 			    U8_UNICODE_LATEST, &error) == 0) || error != 0)
225789Sahrens 				break;
2265331Samw 		}
2275331Samw 		if (error != 0) {
2285331Samw 			mutex_exit(&dzp->z_lock);
2295331Samw 			rw_exit(&dzp->z_name_lock);
2305331Samw 			return (ENOENT);
2315331Samw 		}
232789Sahrens 		if (dl == NULL)	{
233789Sahrens 			/*
234789Sahrens 			 * Allocate a new dirlock and add it to the list.
235789Sahrens 			 */
236789Sahrens 			dl = kmem_alloc(sizeof (zfs_dirlock_t), KM_SLEEP);
237789Sahrens 			cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL);
238789Sahrens 			dl->dl_name = name;
239789Sahrens 			dl->dl_sharecnt = 0;
240789Sahrens 			dl->dl_namesize = 0;
241789Sahrens 			dl->dl_dzp = dzp;
242789Sahrens 			dl->dl_next = dzp->z_dirlocks;
243789Sahrens 			dzp->z_dirlocks = dl;
244789Sahrens 			break;
245789Sahrens 		}
246789Sahrens 		if ((flag & ZSHARED) && dl->dl_sharecnt != 0)
247789Sahrens 			break;
248789Sahrens 		cv_wait(&dl->dl_cv, &dzp->z_lock);
249789Sahrens 	}
250789Sahrens 
251789Sahrens 	if ((flag & ZSHARED) && ++dl->dl_sharecnt > 1 && dl->dl_namesize == 0) {
252789Sahrens 		/*
253789Sahrens 		 * We're the second shared reference to dl.  Make a copy of
254789Sahrens 		 * dl_name in case the first thread goes away before we do.
255789Sahrens 		 * Note that we initialize the new name before storing its
256789Sahrens 		 * pointer into dl_name, because the first thread may load
257789Sahrens 		 * dl->dl_name at any time.  He'll either see the old value,
258789Sahrens 		 * which is his, or the new shared copy; either is OK.
259789Sahrens 		 */
260789Sahrens 		dl->dl_namesize = strlen(dl->dl_name) + 1;
261789Sahrens 		name = kmem_alloc(dl->dl_namesize, KM_SLEEP);
262789Sahrens 		bcopy(dl->dl_name, name, dl->dl_namesize);
263789Sahrens 		dl->dl_name = name;
264789Sahrens 	}
265789Sahrens 
266789Sahrens 	mutex_exit(&dzp->z_lock);
267789Sahrens 
268789Sahrens 	/*
269789Sahrens 	 * We have a dirlock on the name.  (Note that it is the dirlock,
270789Sahrens 	 * not the dzp's z_lock, that protects the name in the zap object.)
271789Sahrens 	 * See if there's an object by this name; if so, put a hold on it.
272789Sahrens 	 */
273789Sahrens 	if (flag & ZXATTR) {
274789Sahrens 		zoid = dzp->z_phys->zp_xattr;
275789Sahrens 		error = (zoid == 0 ? ENOENT : 0);
276789Sahrens 	} else {
2775331Samw 		if (update)
2785331Samw 			vp = dnlc_lookup(ZTOV(dzp), name);
2791484Sek110237 		if (vp == DNLC_NO_VNODE) {
2801484Sek110237 			VN_RELE(vp);
2811484Sek110237 			error = ENOENT;
2821484Sek110237 		} else if (vp) {
2831484Sek110237 			if (flag & ZNEW) {
2841484Sek110237 				zfs_dirent_unlock(dl);
2851484Sek110237 				VN_RELE(vp);
2861484Sek110237 				return (EEXIST);
2871484Sek110237 			}
2881484Sek110237 			*dlpp = dl;
2891484Sek110237 			*zpp = VTOZ(vp);
2901484Sek110237 			return (0);
2911484Sek110237 		} else {
2925331Samw 			error = zfs_match_find(zfsvfs, dzp, name, exact,
2935331Samw 			    update, direntflags, realpnp, &zoid);
2941484Sek110237 		}
295789Sahrens 	}
296789Sahrens 	if (error) {
297789Sahrens 		if (error != ENOENT || (flag & ZEXISTS)) {
298789Sahrens 			zfs_dirent_unlock(dl);
299789Sahrens 			return (error);
300789Sahrens 		}
301789Sahrens 	} else {
302789Sahrens 		if (flag & ZNEW) {
303789Sahrens 			zfs_dirent_unlock(dl);
304789Sahrens 			return (EEXIST);
305789Sahrens 		}
306789Sahrens 		error = zfs_zget(zfsvfs, zoid, zpp);
307789Sahrens 		if (error) {
308789Sahrens 			zfs_dirent_unlock(dl);
309789Sahrens 			return (error);
310789Sahrens 		}
3115331Samw 		if (!(flag & ZXATTR) && update)
3121484Sek110237 			dnlc_update(ZTOV(dzp), name, ZTOV(*zpp));
313789Sahrens 	}
314789Sahrens 
315789Sahrens 	*dlpp = dl;
316789Sahrens 
317789Sahrens 	return (0);
318789Sahrens }
319789Sahrens 
320789Sahrens /*
321789Sahrens  * Unlock this directory entry and wake anyone who was waiting for it.
322789Sahrens  */
323789Sahrens void
324789Sahrens zfs_dirent_unlock(zfs_dirlock_t *dl)
325789Sahrens {
326789Sahrens 	znode_t *dzp = dl->dl_dzp;
327789Sahrens 	zfs_dirlock_t **prev_dl, *cur_dl;
328789Sahrens 
329789Sahrens 	mutex_enter(&dzp->z_lock);
3303897Smaybee 	rw_exit(&dzp->z_name_lock);
331789Sahrens 	if (dl->dl_sharecnt > 1) {
332789Sahrens 		dl->dl_sharecnt--;
333789Sahrens 		mutex_exit(&dzp->z_lock);
334789Sahrens 		return;
335789Sahrens 	}
336789Sahrens 	prev_dl = &dzp->z_dirlocks;
337789Sahrens 	while ((cur_dl = *prev_dl) != dl)
338789Sahrens 		prev_dl = &cur_dl->dl_next;
339789Sahrens 	*prev_dl = dl->dl_next;
340789Sahrens 	cv_broadcast(&dl->dl_cv);
341789Sahrens 	mutex_exit(&dzp->z_lock);
342789Sahrens 
343789Sahrens 	if (dl->dl_namesize != 0)
344789Sahrens 		kmem_free(dl->dl_name, dl->dl_namesize);
345789Sahrens 	cv_destroy(&dl->dl_cv);
346789Sahrens 	kmem_free(dl, sizeof (*dl));
347789Sahrens }
348789Sahrens 
349789Sahrens /*
350789Sahrens  * Look up an entry in a directory.
351789Sahrens  *
352789Sahrens  * NOTE: '.' and '..' are handled as special cases because
353789Sahrens  *	no directory entries are actually stored for them.  If this is
354789Sahrens  *	the root of a filesystem, then '.zfs' is also treated as a
355789Sahrens  *	special pseudo-directory.
356789Sahrens  */
357789Sahrens int
3585331Samw zfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp, int flags,
3595331Samw     int *deflg, pathname_t *rpnp)
360789Sahrens {
361789Sahrens 	zfs_dirlock_t *dl;
362789Sahrens 	znode_t *zp;
363789Sahrens 	int error = 0;
364789Sahrens 
365789Sahrens 	if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
366789Sahrens 		*vpp = ZTOV(dzp);
367789Sahrens 		VN_HOLD(*vpp);
368789Sahrens 	} else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
369789Sahrens 		zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
370789Sahrens 		/*
371789Sahrens 		 * If we are a snapshot mounted under .zfs, return
372789Sahrens 		 * the vp for the snapshot directory.
373789Sahrens 		 */
3741878Smaybee 		if (dzp->z_phys->zp_parent == dzp->z_id &&
3751878Smaybee 		    zfsvfs->z_parent != zfsvfs) {
376789Sahrens 			error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir,
3775331Samw 			    "snapshot", vpp, NULL, 0, NULL, kcred,
3785331Samw 			    NULL, NULL, NULL);
379789Sahrens 			return (error);
380789Sahrens 		}
381789Sahrens 		rw_enter(&dzp->z_parent_lock, RW_READER);
382789Sahrens 		error = zfs_zget(zfsvfs, dzp->z_phys->zp_parent, &zp);
383789Sahrens 		if (error == 0)
384789Sahrens 			*vpp = ZTOV(zp);
385789Sahrens 		rw_exit(&dzp->z_parent_lock);
386789Sahrens 	} else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) {
387789Sahrens 		*vpp = zfsctl_root(dzp);
388789Sahrens 	} else {
3895331Samw 		int zf;
3905331Samw 
3915331Samw 		zf = ZEXISTS | ZSHARED;
3925331Samw 		if (flags & FIGNORECASE)
3935331Samw 			zf |= ZCILOOK;
3945331Samw 
3955331Samw 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zf, deflg, rpnp);
396789Sahrens 		if (error == 0) {
397789Sahrens 			*vpp = ZTOV(zp);
398789Sahrens 			zfs_dirent_unlock(dl);
399869Sperrin 			dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
400789Sahrens 		}
4015331Samw 		rpnp = NULL;
402789Sahrens 	}
403789Sahrens 
4046492Stimh 	if ((flags & FIGNORECASE) && rpnp && !error)
4056492Stimh 		(void) strlcpy(rpnp->pn_buf, name, rpnp->pn_bufsize);
4065331Samw 
407789Sahrens 	return (error);
408789Sahrens }
409789Sahrens 
4101544Seschrock /*
4113461Sahrens  * unlinked Set (formerly known as the "delete queue") Error Handling
4121544Seschrock  *
4133461Sahrens  * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
4141544Seschrock  * don't specify the name of the entry that we will be manipulating.  We
4151544Seschrock  * also fib and say that we won't be adding any new entries to the
4163461Sahrens  * unlinked set, even though we might (this is to lower the minimum file
4171544Seschrock  * size that can be deleted in a full filesystem).  So on the small
4183461Sahrens  * chance that the nlink list is using a fat zap (ie. has more than
4191544Seschrock  * 2000 entries), we *may* not pre-read a block that's needed.
4201544Seschrock  * Therefore it is remotely possible for some of the assertions
4213461Sahrens  * regarding the unlinked set below to fail due to i/o error.  On a
4221544Seschrock  * nondebug system, this will result in the space being leaked.
4231544Seschrock  */
424789Sahrens void
4253461Sahrens zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
426789Sahrens {
427789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
428789Sahrens 
4293461Sahrens 	ASSERT(zp->z_unlinked);
430789Sahrens 	ASSERT3U(zp->z_phys->zp_links, ==, 0);
431789Sahrens 
432*7046Sahrens 	VERIFY3U(0, ==,
433*7046Sahrens 	    zap_add_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
4346992Smaybee }
4356992Smaybee 
436789Sahrens /*
4373461Sahrens  * Clean up any znodes that had no links when we either crashed or
4383461Sahrens  * (force) umounted the file system.
4393461Sahrens  */
4403461Sahrens void
4413461Sahrens zfs_unlinked_drain(zfsvfs_t *zfsvfs)
4423461Sahrens {
4433461Sahrens 	zap_cursor_t	zc;
4443461Sahrens 	zap_attribute_t zap;
4453461Sahrens 	dmu_object_info_t doi;
4463461Sahrens 	znode_t		*zp;
4473461Sahrens 	int		error;
4483461Sahrens 
4493461Sahrens 	/*
4503461Sahrens 	 * Interate over the contents of the unlinked set.
4513461Sahrens 	 */
4523461Sahrens 	for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
4533461Sahrens 	    zap_cursor_retrieve(&zc, &zap) == 0;
4543461Sahrens 	    zap_cursor_advance(&zc)) {
4553461Sahrens 
4563461Sahrens 		/*
4573461Sahrens 		 * See what kind of object we have in list
4583461Sahrens 		 */
4593461Sahrens 
4603461Sahrens 		error = dmu_object_info(zfsvfs->z_os,
4613461Sahrens 		    zap.za_first_integer, &doi);
4623461Sahrens 		if (error != 0)
4633461Sahrens 			continue;
4643461Sahrens 
4653461Sahrens 		ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
4663461Sahrens 		    (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
4673461Sahrens 		/*
4683461Sahrens 		 * We need to re-mark these list entries for deletion,
4693461Sahrens 		 * so we pull them back into core and set zp->z_unlinked.
4703461Sahrens 		 */
4713461Sahrens 		error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
4723461Sahrens 
4733461Sahrens 		/*
4743461Sahrens 		 * We may pick up znodes that are already marked for deletion.
4753461Sahrens 		 * This could happen during the purge of an extended attribute
4763461Sahrens 		 * directory.  All we need to do is skip over them, since they
4773461Sahrens 		 * are already in the system marked z_unlinked.
4783461Sahrens 		 */
4793461Sahrens 		if (error != 0)
4803461Sahrens 			continue;
4813461Sahrens 
4823461Sahrens 		zp->z_unlinked = B_TRUE;
4833461Sahrens 		VN_RELE(ZTOV(zp));
4843461Sahrens 	}
4853461Sahrens 	zap_cursor_fini(&zc);
4863461Sahrens }
4873461Sahrens 
4883461Sahrens /*
489789Sahrens  * Delete the entire contents of a directory.  Return a count
4905860Sck153898  * of the number of entries that could not be deleted. If we encounter
4915860Sck153898  * an error, return a count of at least one so that the directory stays
4925860Sck153898  * in the unlinked set.
493789Sahrens  *
494789Sahrens  * NOTE: this function assumes that the directory is inactive,
495789Sahrens  *	so there is no need to lock its entries before deletion.
496789Sahrens  *	Also, it assumes the directory contents is *only* regular
497789Sahrens  *	files.
498789Sahrens  */
499789Sahrens static int
500789Sahrens zfs_purgedir(znode_t *dzp)
501789Sahrens {
502789Sahrens 	zap_cursor_t	zc;
503789Sahrens 	zap_attribute_t	zap;
504789Sahrens 	znode_t		*xzp;
505789Sahrens 	dmu_tx_t	*tx;
506789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
507789Sahrens 	zfs_dirlock_t	dl;
508789Sahrens 	int skipped = 0;
509789Sahrens 	int error;
510789Sahrens 
511789Sahrens 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
512789Sahrens 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
513789Sahrens 	    zap_cursor_advance(&zc)) {
5143912Slling 		error = zfs_zget(zfsvfs,
5153912Slling 		    ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
5165860Sck153898 		if (error) {
5175860Sck153898 			skipped += 1;
5185860Sck153898 			continue;
5195860Sck153898 		}
520789Sahrens 
521789Sahrens 		ASSERT((ZTOV(xzp)->v_type == VREG) ||
522789Sahrens 		    (ZTOV(xzp)->v_type == VLNK));
523789Sahrens 
524789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
525789Sahrens 		dmu_tx_hold_bonus(tx, dzp->z_id);
5261544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
527789Sahrens 		dmu_tx_hold_bonus(tx, xzp->z_id);
5283461Sahrens 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
529789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
530789Sahrens 		if (error) {
531789Sahrens 			dmu_tx_abort(tx);
532789Sahrens 			VN_RELE(ZTOV(xzp));
533789Sahrens 			skipped += 1;
534789Sahrens 			continue;
535789Sahrens 		}
536789Sahrens 		bzero(&dl, sizeof (dl));
537789Sahrens 		dl.dl_dzp = dzp;
538789Sahrens 		dl.dl_name = zap.za_name;
539789Sahrens 
540789Sahrens 		error = zfs_link_destroy(&dl, xzp, tx, 0, NULL);
5415860Sck153898 		if (error)
5425860Sck153898 			skipped += 1;
543789Sahrens 		dmu_tx_commit(tx);
544789Sahrens 
545789Sahrens 		VN_RELE(ZTOV(xzp));
546789Sahrens 	}
547885Sahrens 	zap_cursor_fini(&zc);
5485860Sck153898 	if (error != ENOENT)
5495860Sck153898 		skipped += 1;
550789Sahrens 	return (skipped);
551789Sahrens }
552789Sahrens 
553789Sahrens void
554789Sahrens zfs_rmnode(znode_t *zp)
555789Sahrens {
556789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
557789Sahrens 	objset_t	*os = zfsvfs->z_os;
558789Sahrens 	znode_t		*xzp = NULL;
559789Sahrens 	dmu_tx_t	*tx;
560789Sahrens 	uint64_t	acl_obj;
561789Sahrens 	int		error;
562789Sahrens 
563789Sahrens 	ASSERT(ZTOV(zp)->v_count == 0);
564789Sahrens 	ASSERT(zp->z_phys->zp_links == 0);
565789Sahrens 
566789Sahrens 	/*
567789Sahrens 	 * If this is an attribute directory, purge its contents.
568789Sahrens 	 */
5693461Sahrens 	if (ZTOV(zp)->v_type == VDIR && (zp->z_phys->zp_flags & ZFS_XATTR)) {
570789Sahrens 		if (zfs_purgedir(zp) != 0) {
571789Sahrens 			/*
5723461Sahrens 			 * Not enough space to delete some xattrs.
5736992Smaybee 			 * Leave it in the unlinked set.
574789Sahrens 			 */
5755745Smaybee 			zfs_znode_dmu_fini(zp);
5765745Smaybee 			zfs_znode_free(zp);
577789Sahrens 			return;
578789Sahrens 		}
5793461Sahrens 	}
580789Sahrens 
581789Sahrens 	/*
5826992Smaybee 	 * Free up all the data in the file.
5836992Smaybee 	 */
5846992Smaybee 	error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
5856992Smaybee 	if (error) {
5866992Smaybee 		/*
5876992Smaybee 		 * Not enough space.  Leave the file in the unlinked set.
5886992Smaybee 		 */
5896992Smaybee 		zfs_znode_dmu_fini(zp);
5906992Smaybee 		zfs_znode_free(zp);
5916992Smaybee 		return;
5926992Smaybee 	}
5936992Smaybee 
5946992Smaybee 	/*
5953461Sahrens 	 * If the file has extended attributes, we're going to unlink
5963461Sahrens 	 * the xattr dir.
597789Sahrens 	 */
598789Sahrens 	if (zp->z_phys->zp_xattr) {
599789Sahrens 		error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
600789Sahrens 		ASSERT(error == 0);
601789Sahrens 	}
602789Sahrens 
603789Sahrens 	acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
604789Sahrens 
605789Sahrens 	/*
6066992Smaybee 	 * Set up the final transaction.
607789Sahrens 	 */
608789Sahrens 	tx = dmu_tx_create(os);
609789Sahrens 	dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
6103461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
611789Sahrens 	if (xzp) {
612789Sahrens 		dmu_tx_hold_bonus(tx, xzp->z_id);
6133461Sahrens 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
614789Sahrens 	}
615789Sahrens 	if (acl_obj)
616789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
617789Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
618789Sahrens 	if (error) {
6193461Sahrens 		/*
6203461Sahrens 		 * Not enough space to delete the file.  Leave it in the
6213461Sahrens 		 * unlinked set, leaking it until the fs is remounted (at
6223461Sahrens 		 * which point we'll call zfs_unlinked_drain() to process it).
6233461Sahrens 		 */
624789Sahrens 		dmu_tx_abort(tx);
6255745Smaybee 		zfs_znode_dmu_fini(zp);
6265745Smaybee 		zfs_znode_free(zp);
6275745Smaybee 		goto out;
628789Sahrens 	}
629789Sahrens 
630789Sahrens 	if (xzp) {
631789Sahrens 		dmu_buf_will_dirty(xzp->z_dbuf, tx);
632789Sahrens 		mutex_enter(&xzp->z_lock);
6333461Sahrens 		xzp->z_unlinked = B_TRUE;	/* mark xzp for deletion */
634789Sahrens 		xzp->z_phys->zp_links = 0;	/* no more links to it */
635789Sahrens 		mutex_exit(&xzp->z_lock);
6363461Sahrens 		zfs_unlinked_add(xzp, tx);
637789Sahrens 	}
638789Sahrens 
6393461Sahrens 	/* Remove this znode from the unlinked set */
640*7046Sahrens 	VERIFY3U(0, ==,
641*7046Sahrens 	    zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
642789Sahrens 
643789Sahrens 	zfs_znode_delete(zp, tx);
644789Sahrens 
645789Sahrens 	dmu_tx_commit(tx);
6465745Smaybee out:
647789Sahrens 	if (xzp)
648789Sahrens 		VN_RELE(ZTOV(xzp));
649789Sahrens }
650789Sahrens 
6514577Sahrens static uint64_t
6524577Sahrens zfs_dirent(znode_t *zp)
6534577Sahrens {
6544577Sahrens 	uint64_t de = zp->z_id;
6554577Sahrens 	if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
6564577Sahrens 		de |= IFTODT((zp)->z_phys->zp_mode) << 60;
6574577Sahrens 	return (de);
6584577Sahrens }
6594577Sahrens 
660789Sahrens /*
6613461Sahrens  * Link zp into dl.  Can only fail if zp has been unlinked.
662789Sahrens  */
663789Sahrens int
664789Sahrens zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
665789Sahrens {
666789Sahrens 	znode_t *dzp = dl->dl_dzp;
667789Sahrens 	vnode_t *vp = ZTOV(zp);
6683912Slling 	uint64_t value;
669789Sahrens 	int zp_is_dir = (vp->v_type == VDIR);
670789Sahrens 	int error;
671789Sahrens 
672789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
673789Sahrens 	mutex_enter(&zp->z_lock);
674789Sahrens 
675789Sahrens 	if (!(flag & ZRENAMING)) {
6763461Sahrens 		if (zp->z_unlinked) {	/* no new links to unlinked zp */
677789Sahrens 			ASSERT(!(flag & (ZNEW | ZEXISTS)));
678789Sahrens 			mutex_exit(&zp->z_lock);
679789Sahrens 			return (ENOENT);
680789Sahrens 		}
681789Sahrens 		zp->z_phys->zp_links++;
682789Sahrens 	}
683789Sahrens 	zp->z_phys->zp_parent = dzp->z_id;	/* dzp is now zp's parent */
684789Sahrens 
685789Sahrens 	if (!(flag & ZNEW))
686789Sahrens 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
687789Sahrens 	mutex_exit(&zp->z_lock);
688789Sahrens 
689789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
690789Sahrens 	mutex_enter(&dzp->z_lock);
691789Sahrens 	dzp->z_phys->zp_size++;			/* one dirent added */
692789Sahrens 	dzp->z_phys->zp_links += zp_is_dir;	/* ".." link from zp */
693789Sahrens 	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
694789Sahrens 	mutex_exit(&dzp->z_lock);
695789Sahrens 
6964577Sahrens 	value = zfs_dirent(zp);
697789Sahrens 	error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
6983912Slling 	    8, 1, &value, tx);
699789Sahrens 	ASSERT(error == 0);
700789Sahrens 
7011484Sek110237 	dnlc_update(ZTOV(dzp), dl->dl_name, vp);
7021484Sek110237 
703789Sahrens 	return (0);
704789Sahrens }
705789Sahrens 
706789Sahrens /*
7073461Sahrens  * Unlink zp from dl, and mark zp for deletion if this was the last link.
708789Sahrens  * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
7093461Sahrens  * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
7103461Sahrens  * If it's non-NULL, we use it to indicate whether the znode needs deletion,
711789Sahrens  * and it's the caller's job to do it.
712789Sahrens  */
713789Sahrens int
714789Sahrens zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
7153461Sahrens 	boolean_t *unlinkedp)
716789Sahrens {
717789Sahrens 	znode_t *dzp = dl->dl_dzp;
718789Sahrens 	vnode_t *vp = ZTOV(zp);
719789Sahrens 	int zp_is_dir = (vp->v_type == VDIR);
7203461Sahrens 	boolean_t unlinked = B_FALSE;
721789Sahrens 	int error;
722789Sahrens 
7231484Sek110237 	dnlc_remove(ZTOV(dzp), dl->dl_name);
7241484Sek110237 
725789Sahrens 	if (!(flag & ZRENAMING)) {
726789Sahrens 		dmu_buf_will_dirty(zp->z_dbuf, tx);
727789Sahrens 
728789Sahrens 		if (vn_vfswlock(vp))		/* prevent new mounts on zp */
729789Sahrens 			return (EBUSY);
730789Sahrens 
731789Sahrens 		if (vn_ismntpt(vp)) {		/* don't remove mount point */
732789Sahrens 			vn_vfsunlock(vp);
733789Sahrens 			return (EBUSY);
734789Sahrens 		}
735789Sahrens 
736789Sahrens 		mutex_enter(&zp->z_lock);
737789Sahrens 		if (zp_is_dir && !zfs_dirempty(zp)) {	/* dir not empty */
738789Sahrens 			mutex_exit(&zp->z_lock);
739789Sahrens 			vn_vfsunlock(vp);
740789Sahrens 			return (EEXIST);
741789Sahrens 		}
7423713Sahrens 		if (zp->z_phys->zp_links <= zp_is_dir) {
7433713Sahrens 			zfs_panic_recover("zfs: link count on %s is %u, "
7443713Sahrens 			    "should be at least %u",
7453713Sahrens 			    zp->z_vnode->v_path ? zp->z_vnode->v_path :
7463713Sahrens 			    "<unknown>", (int)zp->z_phys->zp_links,
7473713Sahrens 			    zp_is_dir + 1);
7483713Sahrens 			zp->z_phys->zp_links = zp_is_dir + 1;
7493713Sahrens 		}
750789Sahrens 		if (--zp->z_phys->zp_links == zp_is_dir) {
7513461Sahrens 			zp->z_unlinked = B_TRUE;
752789Sahrens 			zp->z_phys->zp_links = 0;
7533461Sahrens 			unlinked = B_TRUE;
754789Sahrens 		} else {
755789Sahrens 			zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
756789Sahrens 		}
757789Sahrens 		mutex_exit(&zp->z_lock);
758789Sahrens 		vn_vfsunlock(vp);
759789Sahrens 	}
760789Sahrens 
761789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
762789Sahrens 	mutex_enter(&dzp->z_lock);
763789Sahrens 	dzp->z_phys->zp_size--;			/* one dirent removed */
764789Sahrens 	dzp->z_phys->zp_links -= zp_is_dir;	/* ".." link from zp */
765789Sahrens 	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
766789Sahrens 	mutex_exit(&dzp->z_lock);
767789Sahrens 
7685331Samw 	if (zp->z_zfsvfs->z_norm) {
7695498Stimh 		if (((zp->z_zfsvfs->z_case == ZFS_CASE_INSENSITIVE) &&
7705331Samw 		    (flag & ZCIEXACT)) ||
7715498Stimh 		    ((zp->z_zfsvfs->z_case == ZFS_CASE_MIXED) &&
7725331Samw 		    !(flag & ZCILOOK)))
7735331Samw 			error = zap_remove_norm(zp->z_zfsvfs->z_os,
7745331Samw 			    dzp->z_id, dl->dl_name, MT_EXACT, tx);
7755331Samw 		else
7765331Samw 			error = zap_remove_norm(zp->z_zfsvfs->z_os,
7775331Samw 			    dzp->z_id, dl->dl_name, MT_FIRST, tx);
7785331Samw 	} else {
7795331Samw 		error = zap_remove(zp->z_zfsvfs->z_os,
7805331Samw 		    dzp->z_id, dl->dl_name, tx);
7815331Samw 	}
782789Sahrens 	ASSERT(error == 0);
783789Sahrens 
7843461Sahrens 	if (unlinkedp != NULL)
7853461Sahrens 		*unlinkedp = unlinked;
7863461Sahrens 	else if (unlinked)
7873461Sahrens 		zfs_unlinked_add(zp, tx);
788789Sahrens 
789789Sahrens 	return (0);
790789Sahrens }
791789Sahrens 
792789Sahrens /*
793789Sahrens  * Indicate whether the directory is empty.  Works with or without z_lock
794789Sahrens  * held, but can only be consider a hint in the latter case.  Returns true
795789Sahrens  * if only "." and ".." remain and there's no work in progress.
796789Sahrens  */
797789Sahrens boolean_t
798789Sahrens zfs_dirempty(znode_t *dzp)
799789Sahrens {
800789Sahrens 	return (dzp->z_phys->zp_size == 2 && dzp->z_dirlocks == 0);
801789Sahrens }
802789Sahrens 
803789Sahrens int
804789Sahrens zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
805789Sahrens {
806789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
807789Sahrens 	znode_t *xzp;
808789Sahrens 	dmu_tx_t *tx;
809789Sahrens 	int error;
8105331Samw 	zfs_fuid_info_t *fuidp = NULL;
811789Sahrens 
812789Sahrens 	*xvpp = NULL;
813789Sahrens 
8145331Samw 	if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr))
815789Sahrens 		return (error);
816789Sahrens 
817789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
818789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
8191544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
8205824Smarks 	if (IS_EPHEMERAL(crgetuid(cr)) || IS_EPHEMERAL(crgetgid(cr))) {
8215824Smarks 		if (zfsvfs->z_fuid_obj == 0) {
8225824Smarks 			dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
8235824Smarks 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
8245824Smarks 			    FUID_SIZE_ESTIMATE(zfsvfs));
8255824Smarks 			dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
8265824Smarks 		} else {
8275824Smarks 			dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
8285824Smarks 			dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
8295824Smarks 			    FUID_SIZE_ESTIMATE(zfsvfs));
8305824Smarks 		}
8315331Samw 	}
832789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
833789Sahrens 	if (error) {
8342113Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT)
8352113Sahrens 			dmu_tx_wait(tx);
836789Sahrens 		dmu_tx_abort(tx);
837789Sahrens 		return (error);
838789Sahrens 	}
8395446Sahrens 	zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, 0, NULL, &fuidp);
840789Sahrens 	ASSERT(xzp->z_phys->zp_parent == zp->z_id);
841789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
8425446Sahrens 	zp->z_phys->zp_xattr = xzp->z_id;
843789Sahrens 
8445331Samw 	(void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
8455331Samw 	    xzp, "", NULL, fuidp, vap);
8465331Samw 	if (fuidp)
8475331Samw 		zfs_fuid_info_free(fuidp);
848789Sahrens 	dmu_tx_commit(tx);
849789Sahrens 
850789Sahrens 	*xvpp = ZTOV(xzp);
851789Sahrens 
852789Sahrens 	return (0);
853789Sahrens }
854789Sahrens 
855789Sahrens /*
856789Sahrens  * Return a znode for the extended attribute directory for zp.
857789Sahrens  * ** If the directory does not already exist, it is created **
858789Sahrens  *
859789Sahrens  *	IN:	zp	- znode to obtain attribute directory from
860789Sahrens  *		cr	- credentials of caller
8613280Sck153898  *		flags	- flags from the VOP_LOOKUP call
862789Sahrens  *
863789Sahrens  *	OUT:	xzpp	- pointer to extended attribute znode
864789Sahrens  *
865789Sahrens  *	RETURN:	0 on success
866789Sahrens  *		error number on failure
867789Sahrens  */
868789Sahrens int
8693280Sck153898 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
870789Sahrens {
871789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
872789Sahrens 	znode_t		*xzp;
873789Sahrens 	zfs_dirlock_t	*dl;
874789Sahrens 	vattr_t		va;
875789Sahrens 	int		error;
876789Sahrens top:
8775331Samw 	error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR, NULL, NULL);
878789Sahrens 	if (error)
879789Sahrens 		return (error);
880789Sahrens 
881789Sahrens 	if (xzp != NULL) {
882789Sahrens 		*xvpp = ZTOV(xzp);
883789Sahrens 		zfs_dirent_unlock(dl);
884789Sahrens 		return (0);
885789Sahrens 	}
886789Sahrens 
887789Sahrens 	ASSERT(zp->z_phys->zp_xattr == 0);
888789Sahrens 
8893280Sck153898 	if (!(flags & CREATE_XATTR_DIR)) {
8903280Sck153898 		zfs_dirent_unlock(dl);
8913280Sck153898 		return (ENOENT);
8923280Sck153898 	}
8933280Sck153898 
894789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
895789Sahrens 		zfs_dirent_unlock(dl);
896789Sahrens 		return (EROFS);
897789Sahrens 	}
898789Sahrens 
899789Sahrens 	/*
900789Sahrens 	 * The ability to 'create' files in an attribute
901789Sahrens 	 * directory comes from the write_xattr permission on the base file.
902789Sahrens 	 *
903789Sahrens 	 * The ability to 'search' an attribute directory requires
904789Sahrens 	 * read_xattr permission on the base file.
905789Sahrens 	 *
906789Sahrens 	 * Once in a directory the ability to read/write attributes
907789Sahrens 	 * is controlled by the permissions on the attribute file.
908789Sahrens 	 */
909789Sahrens 	va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
910789Sahrens 	va.va_type = VDIR;
9111231Smarks 	va.va_mode = S_IFDIR | S_ISVTX | 0777;
9125771Sjp151216 	zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
913789Sahrens 
914789Sahrens 	error = zfs_make_xattrdir(zp, &va, xvpp, cr);
915789Sahrens 	zfs_dirent_unlock(dl);
916789Sahrens 
917789Sahrens 	if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
9182113Sahrens 		/* NB: we already did dmu_tx_wait() if necessary */
919789Sahrens 		goto top;
920789Sahrens 	}
921789Sahrens 
922789Sahrens 	return (error);
923789Sahrens }
924789Sahrens 
925789Sahrens /*
926789Sahrens  * Decide whether it is okay to remove within a sticky directory.
927789Sahrens  *
928789Sahrens  * In sticky directories, write access is not sufficient;
929789Sahrens  * you can remove entries from a directory only if:
930789Sahrens  *
931789Sahrens  *	you own the directory,
932789Sahrens  *	you own the entry,
933789Sahrens  *	the entry is a plain file and you have write access,
934789Sahrens  *	or you are privileged (checked in secpolicy...).
935789Sahrens  *
936789Sahrens  * The function returns 0 if remove access is granted.
937789Sahrens  */
938789Sahrens int
939789Sahrens zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
940789Sahrens {
941789Sahrens 	uid_t  		uid;
9425331Samw 	uid_t		downer;
9435331Samw 	uid_t		fowner;
9445331Samw 	zfsvfs_t	*zfsvfs = zdp->z_zfsvfs;
945789Sahrens 
946789Sahrens 	if (zdp->z_zfsvfs->z_assign >= TXG_INITIAL)	/* ZIL replay */
947789Sahrens 		return (0);
948789Sahrens 
9495331Samw 	if ((zdp->z_phys->zp_mode & S_ISVTX) == 0)
9505331Samw 		return (0);
9515331Samw 
9525959Smarks 	downer = zfs_fuid_map_id(zfsvfs, zdp->z_phys->zp_uid, cr, ZFS_OWNER);
9535959Smarks 	fowner = zfs_fuid_map_id(zfsvfs, zp->z_phys->zp_uid, cr, ZFS_OWNER);
9545331Samw 
9555331Samw 	if ((uid = crgetuid(cr)) == downer || uid == fowner ||
956789Sahrens 	    (ZTOV(zp)->v_type == VREG &&
9575331Samw 	    zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
958789Sahrens 		return (0);
959789Sahrens 	else
960789Sahrens 		return (secpolicy_vnode_remove(cr));
961789Sahrens }
962