xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_dir.c (revision 5498:334b476844ca)
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 /*
223461Sahrens  * Copyright 2007 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>
45*5498Stimh #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) {
775331Samw 			buf = rpnp->pn_path;
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);
885331Samw 		if (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 	 */
180*5498Stimh 	exact =
181*5498Stimh 	    ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE) && (flag & ZCIEXACT)) ||
182*5498Stimh 	    ((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 ||
195*5498Stimh 	    ((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 
4045331Samw 	if ((flags & FIGNORECASE) && rpnp)
4055331Samw 		(void) strlcpy(rpnp->pn_path, name, rpnp->pn_bufsize);
4065331Samw 
407789Sahrens 	return (error);
408789Sahrens }
409789Sahrens 
410789Sahrens static char *
4113461Sahrens zfs_unlinked_hexname(char namebuf[17], uint64_t x)
412789Sahrens {
413789Sahrens 	char *name = &namebuf[16];
414789Sahrens 	const char digits[16] = "0123456789abcdef";
415789Sahrens 
416789Sahrens 	*name = '\0';
417789Sahrens 	do {
418789Sahrens 		*--name = digits[x & 0xf];
419789Sahrens 		x >>= 4;
420789Sahrens 	} while (x != 0);
421789Sahrens 
422789Sahrens 	return (name);
423789Sahrens }
424789Sahrens 
4251544Seschrock /*
4263461Sahrens  * unlinked Set (formerly known as the "delete queue") Error Handling
4271544Seschrock  *
4283461Sahrens  * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
4291544Seschrock  * don't specify the name of the entry that we will be manipulating.  We
4301544Seschrock  * also fib and say that we won't be adding any new entries to the
4313461Sahrens  * unlinked set, even though we might (this is to lower the minimum file
4321544Seschrock  * size that can be deleted in a full filesystem).  So on the small
4333461Sahrens  * chance that the nlink list is using a fat zap (ie. has more than
4341544Seschrock  * 2000 entries), we *may* not pre-read a block that's needed.
4351544Seschrock  * Therefore it is remotely possible for some of the assertions
4363461Sahrens  * regarding the unlinked set below to fail due to i/o error.  On a
4371544Seschrock  * nondebug system, this will result in the space being leaked.
4381544Seschrock  */
439789Sahrens void
4403461Sahrens zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
441789Sahrens {
442789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
443789Sahrens 	char obj_name[17];
444789Sahrens 	int error;
445789Sahrens 
4463461Sahrens 	ASSERT(zp->z_unlinked);
447789Sahrens 	ASSERT3U(zp->z_phys->zp_links, ==, 0);
448789Sahrens 
4493461Sahrens 	error = zap_add(zfsvfs->z_os, zfsvfs->z_unlinkedobj,
4503461Sahrens 	    zfs_unlinked_hexname(obj_name, zp->z_id), 8, 1, &zp->z_id, tx);
451789Sahrens 	ASSERT3U(error, ==, 0);
452789Sahrens }
453789Sahrens 
454789Sahrens /*
4553461Sahrens  * Clean up any znodes that had no links when we either crashed or
4563461Sahrens  * (force) umounted the file system.
4573461Sahrens  */
4583461Sahrens void
4593461Sahrens zfs_unlinked_drain(zfsvfs_t *zfsvfs)
4603461Sahrens {
4613461Sahrens 	zap_cursor_t	zc;
4623461Sahrens 	zap_attribute_t zap;
4633461Sahrens 	dmu_object_info_t doi;
4643461Sahrens 	znode_t		*zp;
4653461Sahrens 	int		error;
4663461Sahrens 
4673461Sahrens 	/*
4683461Sahrens 	 * Interate over the contents of the unlinked set.
4693461Sahrens 	 */
4703461Sahrens 	for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
4713461Sahrens 	    zap_cursor_retrieve(&zc, &zap) == 0;
4723461Sahrens 	    zap_cursor_advance(&zc)) {
4733461Sahrens 
4743461Sahrens 		/*
4753461Sahrens 		 * See what kind of object we have in list
4763461Sahrens 		 */
4773461Sahrens 
4783461Sahrens 		error = dmu_object_info(zfsvfs->z_os,
4793461Sahrens 		    zap.za_first_integer, &doi);
4803461Sahrens 		if (error != 0)
4813461Sahrens 			continue;
4823461Sahrens 
4833461Sahrens 		ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
4843461Sahrens 		    (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
4853461Sahrens 		/*
4863461Sahrens 		 * We need to re-mark these list entries for deletion,
4873461Sahrens 		 * so we pull them back into core and set zp->z_unlinked.
4883461Sahrens 		 */
4893461Sahrens 		error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
4903461Sahrens 
4913461Sahrens 		/*
4923461Sahrens 		 * We may pick up znodes that are already marked for deletion.
4933461Sahrens 		 * This could happen during the purge of an extended attribute
4943461Sahrens 		 * directory.  All we need to do is skip over them, since they
4953461Sahrens 		 * are already in the system marked z_unlinked.
4963461Sahrens 		 */
4973461Sahrens 		if (error != 0)
4983461Sahrens 			continue;
4993461Sahrens 
5003461Sahrens 		zp->z_unlinked = B_TRUE;
5013461Sahrens 		VN_RELE(ZTOV(zp));
5023461Sahrens 	}
5033461Sahrens 	zap_cursor_fini(&zc);
5043461Sahrens }
5053461Sahrens 
5063461Sahrens /*
507789Sahrens  * Delete the entire contents of a directory.  Return a count
508789Sahrens  * of the number of entries that could not be deleted.
509789Sahrens  *
510789Sahrens  * NOTE: this function assumes that the directory is inactive,
511789Sahrens  *	so there is no need to lock its entries before deletion.
512789Sahrens  *	Also, it assumes the directory contents is *only* regular
513789Sahrens  *	files.
514789Sahrens  */
515789Sahrens static int
516789Sahrens zfs_purgedir(znode_t *dzp)
517789Sahrens {
518789Sahrens 	zap_cursor_t	zc;
519789Sahrens 	zap_attribute_t	zap;
520789Sahrens 	znode_t		*xzp;
521789Sahrens 	dmu_tx_t	*tx;
522789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
523789Sahrens 	zfs_dirlock_t	dl;
524789Sahrens 	int skipped = 0;
525789Sahrens 	int error;
526789Sahrens 
527789Sahrens 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
528789Sahrens 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
529789Sahrens 	    zap_cursor_advance(&zc)) {
5303912Slling 		error = zfs_zget(zfsvfs,
5313912Slling 		    ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
532789Sahrens 		ASSERT3U(error, ==, 0);
533789Sahrens 
534789Sahrens 		ASSERT((ZTOV(xzp)->v_type == VREG) ||
535789Sahrens 		    (ZTOV(xzp)->v_type == VLNK));
536789Sahrens 
537789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
538789Sahrens 		dmu_tx_hold_bonus(tx, dzp->z_id);
5391544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
540789Sahrens 		dmu_tx_hold_bonus(tx, xzp->z_id);
5413461Sahrens 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
542789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
543789Sahrens 		if (error) {
544789Sahrens 			dmu_tx_abort(tx);
545789Sahrens 			VN_RELE(ZTOV(xzp));
546789Sahrens 			skipped += 1;
547789Sahrens 			continue;
548789Sahrens 		}
549789Sahrens 		bzero(&dl, sizeof (dl));
550789Sahrens 		dl.dl_dzp = dzp;
551789Sahrens 		dl.dl_name = zap.za_name;
552789Sahrens 
553789Sahrens 		error = zfs_link_destroy(&dl, xzp, tx, 0, NULL);
554789Sahrens 		ASSERT3U(error, ==, 0);
555789Sahrens 		dmu_tx_commit(tx);
556789Sahrens 
557789Sahrens 		VN_RELE(ZTOV(xzp));
558789Sahrens 	}
559885Sahrens 	zap_cursor_fini(&zc);
560789Sahrens 	ASSERT(error == ENOENT);
561789Sahrens 	return (skipped);
562789Sahrens }
563789Sahrens 
564789Sahrens void
565789Sahrens zfs_rmnode(znode_t *zp)
566789Sahrens {
567789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
568789Sahrens 	objset_t	*os = zfsvfs->z_os;
569789Sahrens 	znode_t		*xzp = NULL;
570789Sahrens 	char		obj_name[17];
571789Sahrens 	dmu_tx_t	*tx;
572789Sahrens 	uint64_t	acl_obj;
573789Sahrens 	int		error;
574789Sahrens 
575789Sahrens 	ASSERT(ZTOV(zp)->v_count == 0);
576789Sahrens 	ASSERT(zp->z_phys->zp_links == 0);
577789Sahrens 
578789Sahrens 	/*
579789Sahrens 	 * If this is an attribute directory, purge its contents.
580789Sahrens 	 */
5813461Sahrens 	if (ZTOV(zp)->v_type == VDIR && (zp->z_phys->zp_flags & ZFS_XATTR)) {
582789Sahrens 		if (zfs_purgedir(zp) != 0) {
583789Sahrens 			/*
5843461Sahrens 			 * Not enough space to delete some xattrs.
5853461Sahrens 			 * Leave it on the unlinked set.
586789Sahrens 			 */
587789Sahrens 			return;
588789Sahrens 		}
5893461Sahrens 	}
590789Sahrens 
591789Sahrens 	/*
5923461Sahrens 	 * If the file has extended attributes, we're going to unlink
5933461Sahrens 	 * the xattr dir.
594789Sahrens 	 */
595789Sahrens 	if (zp->z_phys->zp_xattr) {
596789Sahrens 		error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
597789Sahrens 		ASSERT(error == 0);
598789Sahrens 	}
599789Sahrens 
600789Sahrens 	acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
601789Sahrens 
602789Sahrens 	/*
603789Sahrens 	 * Set up the transaction.
604789Sahrens 	 */
605789Sahrens 	tx = dmu_tx_create(os);
606789Sahrens 	dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
6073461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
608789Sahrens 	if (xzp) {
609789Sahrens 		dmu_tx_hold_bonus(tx, xzp->z_id);
6103461Sahrens 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
611789Sahrens 	}
612789Sahrens 	if (acl_obj)
613789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
614789Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
615789Sahrens 	if (error) {
6163461Sahrens 		/*
6173461Sahrens 		 * Not enough space to delete the file.  Leave it in the
6183461Sahrens 		 * unlinked set, leaking it until the fs is remounted (at
6193461Sahrens 		 * which point we'll call zfs_unlinked_drain() to process it).
6203461Sahrens 		 */
621789Sahrens 		dmu_tx_abort(tx);
622789Sahrens 		return;
623789Sahrens 	}
624789Sahrens 
625789Sahrens 	if (xzp) {
626789Sahrens 		dmu_buf_will_dirty(xzp->z_dbuf, tx);
627789Sahrens 		mutex_enter(&xzp->z_lock);
6283461Sahrens 		xzp->z_unlinked = B_TRUE;	/* mark xzp for deletion */
629789Sahrens 		xzp->z_phys->zp_links = 0;	/* no more links to it */
630789Sahrens 		mutex_exit(&xzp->z_lock);
6313461Sahrens 		zfs_unlinked_add(xzp, tx);
632789Sahrens 	}
633789Sahrens 
6343461Sahrens 	/* Remove this znode from the unlinked set */
6353461Sahrens 	error = zap_remove(os, zfsvfs->z_unlinkedobj,
6363461Sahrens 	    zfs_unlinked_hexname(obj_name, zp->z_id), tx);
637789Sahrens 	ASSERT3U(error, ==, 0);
638789Sahrens 
639789Sahrens 	zfs_znode_delete(zp, tx);
640789Sahrens 
641789Sahrens 	dmu_tx_commit(tx);
642789Sahrens 
643789Sahrens 	if (xzp)
644789Sahrens 		VN_RELE(ZTOV(xzp));
645789Sahrens }
646789Sahrens 
6474577Sahrens static uint64_t
6484577Sahrens zfs_dirent(znode_t *zp)
6494577Sahrens {
6504577Sahrens 	uint64_t de = zp->z_id;
6514577Sahrens 	if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
6524577Sahrens 		de |= IFTODT((zp)->z_phys->zp_mode) << 60;
6534577Sahrens 	return (de);
6544577Sahrens }
6554577Sahrens 
656789Sahrens /*
6573461Sahrens  * Link zp into dl.  Can only fail if zp has been unlinked.
658789Sahrens  */
659789Sahrens int
660789Sahrens zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
661789Sahrens {
662789Sahrens 	znode_t *dzp = dl->dl_dzp;
663789Sahrens 	vnode_t *vp = ZTOV(zp);
6643912Slling 	uint64_t value;
665789Sahrens 	int zp_is_dir = (vp->v_type == VDIR);
666789Sahrens 	int error;
667789Sahrens 
668789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
669789Sahrens 	mutex_enter(&zp->z_lock);
670789Sahrens 
671789Sahrens 	if (!(flag & ZRENAMING)) {
6723461Sahrens 		if (zp->z_unlinked) {	/* no new links to unlinked zp */
673789Sahrens 			ASSERT(!(flag & (ZNEW | ZEXISTS)));
674789Sahrens 			mutex_exit(&zp->z_lock);
675789Sahrens 			return (ENOENT);
676789Sahrens 		}
677789Sahrens 		zp->z_phys->zp_links++;
678789Sahrens 	}
679789Sahrens 	zp->z_phys->zp_parent = dzp->z_id;	/* dzp is now zp's parent */
680789Sahrens 
681789Sahrens 	if (!(flag & ZNEW))
682789Sahrens 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
683789Sahrens 	mutex_exit(&zp->z_lock);
684789Sahrens 
685789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
686789Sahrens 	mutex_enter(&dzp->z_lock);
687789Sahrens 	dzp->z_phys->zp_size++;			/* one dirent added */
688789Sahrens 	dzp->z_phys->zp_links += zp_is_dir;	/* ".." link from zp */
689789Sahrens 	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
690789Sahrens 	mutex_exit(&dzp->z_lock);
691789Sahrens 
6924577Sahrens 	value = zfs_dirent(zp);
693789Sahrens 	error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
6943912Slling 	    8, 1, &value, tx);
695789Sahrens 	ASSERT(error == 0);
696789Sahrens 
6971484Sek110237 	dnlc_update(ZTOV(dzp), dl->dl_name, vp);
6981484Sek110237 
699789Sahrens 	return (0);
700789Sahrens }
701789Sahrens 
702789Sahrens /*
7033461Sahrens  * Unlink zp from dl, and mark zp for deletion if this was the last link.
704789Sahrens  * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
7053461Sahrens  * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
7063461Sahrens  * If it's non-NULL, we use it to indicate whether the znode needs deletion,
707789Sahrens  * and it's the caller's job to do it.
708789Sahrens  */
709789Sahrens int
710789Sahrens zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
7113461Sahrens 	boolean_t *unlinkedp)
712789Sahrens {
713789Sahrens 	znode_t *dzp = dl->dl_dzp;
714789Sahrens 	vnode_t *vp = ZTOV(zp);
715789Sahrens 	int zp_is_dir = (vp->v_type == VDIR);
7163461Sahrens 	boolean_t unlinked = B_FALSE;
717789Sahrens 	int error;
718789Sahrens 
7191484Sek110237 	dnlc_remove(ZTOV(dzp), dl->dl_name);
7201484Sek110237 
721789Sahrens 	if (!(flag & ZRENAMING)) {
722789Sahrens 		dmu_buf_will_dirty(zp->z_dbuf, tx);
723789Sahrens 
724789Sahrens 		if (vn_vfswlock(vp))		/* prevent new mounts on zp */
725789Sahrens 			return (EBUSY);
726789Sahrens 
727789Sahrens 		if (vn_ismntpt(vp)) {		/* don't remove mount point */
728789Sahrens 			vn_vfsunlock(vp);
729789Sahrens 			return (EBUSY);
730789Sahrens 		}
731789Sahrens 
732789Sahrens 		mutex_enter(&zp->z_lock);
733789Sahrens 		if (zp_is_dir && !zfs_dirempty(zp)) {	/* dir not empty */
734789Sahrens 			mutex_exit(&zp->z_lock);
735789Sahrens 			vn_vfsunlock(vp);
736789Sahrens 			return (EEXIST);
737789Sahrens 		}
7383713Sahrens 		if (zp->z_phys->zp_links <= zp_is_dir) {
7393713Sahrens 			zfs_panic_recover("zfs: link count on %s is %u, "
7403713Sahrens 			    "should be at least %u",
7413713Sahrens 			    zp->z_vnode->v_path ? zp->z_vnode->v_path :
7423713Sahrens 			    "<unknown>", (int)zp->z_phys->zp_links,
7433713Sahrens 			    zp_is_dir + 1);
7443713Sahrens 			zp->z_phys->zp_links = zp_is_dir + 1;
7453713Sahrens 		}
746789Sahrens 		if (--zp->z_phys->zp_links == zp_is_dir) {
7473461Sahrens 			zp->z_unlinked = B_TRUE;
748789Sahrens 			zp->z_phys->zp_links = 0;
7493461Sahrens 			unlinked = B_TRUE;
750789Sahrens 		} else {
751789Sahrens 			zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
752789Sahrens 		}
753789Sahrens 		mutex_exit(&zp->z_lock);
754789Sahrens 		vn_vfsunlock(vp);
755789Sahrens 	}
756789Sahrens 
757789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
758789Sahrens 	mutex_enter(&dzp->z_lock);
759789Sahrens 	dzp->z_phys->zp_size--;			/* one dirent removed */
760789Sahrens 	dzp->z_phys->zp_links -= zp_is_dir;	/* ".." link from zp */
761789Sahrens 	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
762789Sahrens 	mutex_exit(&dzp->z_lock);
763789Sahrens 
7645331Samw 	if (zp->z_zfsvfs->z_norm) {
765*5498Stimh 		if (((zp->z_zfsvfs->z_case == ZFS_CASE_INSENSITIVE) &&
7665331Samw 		    (flag & ZCIEXACT)) ||
767*5498Stimh 		    ((zp->z_zfsvfs->z_case == ZFS_CASE_MIXED) &&
7685331Samw 		    !(flag & ZCILOOK)))
7695331Samw 			error = zap_remove_norm(zp->z_zfsvfs->z_os,
7705331Samw 			    dzp->z_id, dl->dl_name, MT_EXACT, tx);
7715331Samw 		else
7725331Samw 			error = zap_remove_norm(zp->z_zfsvfs->z_os,
7735331Samw 			    dzp->z_id, dl->dl_name, MT_FIRST, tx);
7745331Samw 	} else {
7755331Samw 		error = zap_remove(zp->z_zfsvfs->z_os,
7765331Samw 		    dzp->z_id, dl->dl_name, tx);
7775331Samw 	}
778789Sahrens 	ASSERT(error == 0);
779789Sahrens 
7803461Sahrens 	if (unlinkedp != NULL)
7813461Sahrens 		*unlinkedp = unlinked;
7823461Sahrens 	else if (unlinked)
7833461Sahrens 		zfs_unlinked_add(zp, tx);
784789Sahrens 
785789Sahrens 	return (0);
786789Sahrens }
787789Sahrens 
788789Sahrens /*
789789Sahrens  * Indicate whether the directory is empty.  Works with or without z_lock
790789Sahrens  * held, but can only be consider a hint in the latter case.  Returns true
791789Sahrens  * if only "." and ".." remain and there's no work in progress.
792789Sahrens  */
793789Sahrens boolean_t
794789Sahrens zfs_dirempty(znode_t *dzp)
795789Sahrens {
796789Sahrens 	return (dzp->z_phys->zp_size == 2 && dzp->z_dirlocks == 0);
797789Sahrens }
798789Sahrens 
799789Sahrens int
800789Sahrens zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
801789Sahrens {
802789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
803789Sahrens 	znode_t *xzp;
804789Sahrens 	dmu_tx_t *tx;
805789Sahrens 	int error;
8065331Samw 	zfs_fuid_info_t *fuidp = NULL;
807789Sahrens 
808789Sahrens 	*xvpp = NULL;
809789Sahrens 
8105331Samw 	if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr))
811789Sahrens 		return (error);
812789Sahrens 
813789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
814789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
8151544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
8165331Samw 	if (zfsvfs->z_fuid_obj == 0) {
8175331Samw 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
8185331Samw 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
8195331Samw 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
8205331Samw 	} else {
8215331Samw 		dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
8225331Samw 		dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, SPA_MAXBLOCKSIZE);
8235331Samw 	}
824789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
825789Sahrens 	if (error) {
8262113Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT)
8272113Sahrens 			dmu_tx_wait(tx);
828789Sahrens 		dmu_tx_abort(tx);
829789Sahrens 		return (error);
830789Sahrens 	}
8315446Sahrens 	zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, 0, NULL, &fuidp);
832789Sahrens 	ASSERT(xzp->z_phys->zp_parent == zp->z_id);
833789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
8345446Sahrens 	zp->z_phys->zp_xattr = xzp->z_id;
835789Sahrens 
8365331Samw 	(void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
8375331Samw 	    xzp, "", NULL, fuidp, vap);
8385331Samw 	if (fuidp)
8395331Samw 		zfs_fuid_info_free(fuidp);
840789Sahrens 	dmu_tx_commit(tx);
841789Sahrens 
842789Sahrens 	*xvpp = ZTOV(xzp);
843789Sahrens 
844789Sahrens 	return (0);
845789Sahrens }
846789Sahrens 
847789Sahrens /*
848789Sahrens  * Return a znode for the extended attribute directory for zp.
849789Sahrens  * ** If the directory does not already exist, it is created **
850789Sahrens  *
851789Sahrens  *	IN:	zp	- znode to obtain attribute directory from
852789Sahrens  *		cr	- credentials of caller
8533280Sck153898  *		flags	- flags from the VOP_LOOKUP call
854789Sahrens  *
855789Sahrens  *	OUT:	xzpp	- pointer to extended attribute znode
856789Sahrens  *
857789Sahrens  *	RETURN:	0 on success
858789Sahrens  *		error number on failure
859789Sahrens  */
860789Sahrens int
8613280Sck153898 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
862789Sahrens {
863789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
864789Sahrens 	znode_t		*xzp;
865789Sahrens 	zfs_dirlock_t	*dl;
866789Sahrens 	vattr_t		va;
867789Sahrens 	int		error;
868789Sahrens top:
8695331Samw 	error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR, NULL, NULL);
870789Sahrens 	if (error)
871789Sahrens 		return (error);
872789Sahrens 
873789Sahrens 	if (xzp != NULL) {
874789Sahrens 		*xvpp = ZTOV(xzp);
875789Sahrens 		zfs_dirent_unlock(dl);
876789Sahrens 		return (0);
877789Sahrens 	}
878789Sahrens 
879789Sahrens 	ASSERT(zp->z_phys->zp_xattr == 0);
880789Sahrens 
8813280Sck153898 	if (!(flags & CREATE_XATTR_DIR)) {
8823280Sck153898 		zfs_dirent_unlock(dl);
8833280Sck153898 		return (ENOENT);
8843280Sck153898 	}
8853280Sck153898 
886789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
887789Sahrens 		zfs_dirent_unlock(dl);
888789Sahrens 		return (EROFS);
889789Sahrens 	}
890789Sahrens 
891789Sahrens 	/*
892789Sahrens 	 * The ability to 'create' files in an attribute
893789Sahrens 	 * directory comes from the write_xattr permission on the base file.
894789Sahrens 	 *
895789Sahrens 	 * The ability to 'search' an attribute directory requires
896789Sahrens 	 * read_xattr permission on the base file.
897789Sahrens 	 *
898789Sahrens 	 * Once in a directory the ability to read/write attributes
899789Sahrens 	 * is controlled by the permissions on the attribute file.
900789Sahrens 	 */
901789Sahrens 	va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
902789Sahrens 	va.va_type = VDIR;
9031231Smarks 	va.va_mode = S_IFDIR | S_ISVTX | 0777;
9045331Samw 	zfs_fuid_map_ids(zp, &va.va_uid, &va.va_gid);
905789Sahrens 
906789Sahrens 	error = zfs_make_xattrdir(zp, &va, xvpp, cr);
907789Sahrens 	zfs_dirent_unlock(dl);
908789Sahrens 
909789Sahrens 	if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
9102113Sahrens 		/* NB: we already did dmu_tx_wait() if necessary */
911789Sahrens 		goto top;
912789Sahrens 	}
913789Sahrens 
914789Sahrens 	return (error);
915789Sahrens }
916789Sahrens 
917789Sahrens /*
918789Sahrens  * Decide whether it is okay to remove within a sticky directory.
919789Sahrens  *
920789Sahrens  * In sticky directories, write access is not sufficient;
921789Sahrens  * you can remove entries from a directory only if:
922789Sahrens  *
923789Sahrens  *	you own the directory,
924789Sahrens  *	you own the entry,
925789Sahrens  *	the entry is a plain file and you have write access,
926789Sahrens  *	or you are privileged (checked in secpolicy...).
927789Sahrens  *
928789Sahrens  * The function returns 0 if remove access is granted.
929789Sahrens  */
930789Sahrens int
931789Sahrens zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
932789Sahrens {
933789Sahrens 	uid_t  		uid;
9345331Samw 	uid_t		downer;
9355331Samw 	uid_t		fowner;
9365331Samw 	zfsvfs_t	*zfsvfs = zdp->z_zfsvfs;
937789Sahrens 
938789Sahrens 	if (zdp->z_zfsvfs->z_assign >= TXG_INITIAL)	/* ZIL replay */
939789Sahrens 		return (0);
940789Sahrens 
9415331Samw 	if ((zdp->z_phys->zp_mode & S_ISVTX) == 0)
9425331Samw 		return (0);
9435331Samw 
9445331Samw 	zfs_fuid_map_id(zfsvfs, zdp->z_phys->zp_uid, ZFS_OWNER, &downer);
9455331Samw 	zfs_fuid_map_id(zfsvfs, zp->z_phys->zp_uid, ZFS_OWNER, &fowner);
9465331Samw 
9475331Samw 	if ((uid = crgetuid(cr)) == downer || uid == fowner ||
948789Sahrens 	    (ZTOV(zp)->v_type == VREG &&
9495331Samw 	    zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
950789Sahrens 		return (0);
951789Sahrens 	else
952789Sahrens 		return (secpolicy_vnode_remove(cr));
953789Sahrens }
954