xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_dir.c (revision 9179:d8fbd96b79b3)
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 /*
22*9179SMark.Shellenbaum@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <sys/types.h>
27789Sahrens #include <sys/param.h>
28789Sahrens #include <sys/time.h>
29789Sahrens #include <sys/systm.h>
30789Sahrens #include <sys/sysmacros.h>
31789Sahrens #include <sys/resource.h>
32789Sahrens #include <sys/vfs.h>
33789Sahrens #include <sys/vnode.h>
34789Sahrens #include <sys/file.h>
35789Sahrens #include <sys/mode.h>
36789Sahrens #include <sys/kmem.h>
37789Sahrens #include <sys/uio.h>
38789Sahrens #include <sys/pathname.h>
39789Sahrens #include <sys/cmn_err.h>
40789Sahrens #include <sys/errno.h>
41789Sahrens #include <sys/stat.h>
42789Sahrens #include <sys/unistd.h>
435498Stimh #include <sys/sunddi.h>
44789Sahrens #include <sys/random.h>
45789Sahrens #include <sys/policy.h>
46789Sahrens #include <sys/zfs_dir.h>
47789Sahrens #include <sys/zfs_acl.h>
48789Sahrens #include <sys/fs/zfs.h>
49789Sahrens #include "fs/fs_subr.h"
50789Sahrens #include <sys/zap.h>
51789Sahrens #include <sys/dmu.h>
52789Sahrens #include <sys/atomic.h>
53789Sahrens #include <sys/zfs_ctldir.h>
545331Samw #include <sys/zfs_fuid.h>
551484Sek110237 #include <sys/dnlc.h>
565331Samw #include <sys/extdirent.h>
575331Samw 
585331Samw /*
595331Samw  * zfs_match_find() is used by zfs_dirent_lock() to peform zap lookups
605331Samw  * of names after deciding which is the appropriate lookup interface.
615331Samw  */
625331Samw static int
635331Samw zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, char *name, boolean_t exact,
645331Samw     boolean_t update, int *deflags, pathname_t *rpnp, uint64_t *zoid)
655331Samw {
665331Samw 	int error;
675331Samw 
685331Samw 	if (zfsvfs->z_norm) {
695331Samw 		matchtype_t mt = MT_FIRST;
705331Samw 		boolean_t conflict = B_FALSE;
715331Samw 		size_t bufsz = 0;
725331Samw 		char *buf = NULL;
735331Samw 
745331Samw 		if (rpnp) {
756492Stimh 			buf = rpnp->pn_buf;
765331Samw 			bufsz = rpnp->pn_bufsize;
775331Samw 		}
785331Samw 		if (exact)
795331Samw 			mt = MT_EXACT;
805331Samw 		/*
815331Samw 		 * In the non-mixed case we only expect there would ever
825331Samw 		 * be one match, but we need to use the normalizing lookup.
835331Samw 		 */
845331Samw 		error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1,
855331Samw 		    zoid, mt, buf, bufsz, &conflict);
866492Stimh 		if (!error && deflags)
875331Samw 			*deflags = conflict ? ED_CASE_CONFLICT : 0;
885331Samw 	} else {
895331Samw 		error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid);
905331Samw 	}
915331Samw 	*zoid = ZFS_DIRENT_OBJ(*zoid);
925331Samw 
935331Samw 	if (error == ENOENT && update)
945331Samw 		dnlc_update(ZTOV(dzp), name, DNLC_NO_VNODE);
955331Samw 
965331Samw 	return (error);
975331Samw }
98789Sahrens 
99789Sahrens /*
100789Sahrens  * Lock a directory entry.  A dirlock on <dzp, name> protects that name
101789Sahrens  * in dzp's directory zap object.  As long as you hold a dirlock, you can
102789Sahrens  * assume two things: (1) dzp cannot be reaped, and (2) no other thread
103789Sahrens  * can change the zap entry for (i.e. link or unlink) this name.
104789Sahrens  *
105789Sahrens  * Input arguments:
106789Sahrens  *	dzp	- znode for directory
107789Sahrens  *	name	- name of entry to lock
108789Sahrens  *	flag	- ZNEW: if the entry already exists, fail with EEXIST.
109789Sahrens  *		  ZEXISTS: if the entry does not exist, fail with ENOENT.
110789Sahrens  *		  ZSHARED: allow concurrent access with other ZSHARED callers.
111789Sahrens  *		  ZXATTR: we want dzp's xattr directory
1125331Samw  *		  ZCILOOK: On a mixed sensitivity file system,
1135331Samw  *			   this lookup should be case-insensitive.
1145331Samw  *		  ZCIEXACT: On a purely case-insensitive file system,
1155331Samw  *			    this lookup should be case-sensitive.
1165331Samw  *		  ZRENAMING: we are locking for renaming, force narrow locks
117789Sahrens  *
118789Sahrens  * Output arguments:
119789Sahrens  *	zpp	- pointer to the znode for the entry (NULL if there isn't one)
120789Sahrens  *	dlpp	- pointer to the dirlock for this entry (NULL on error)
1215331Samw  *      direntflags - (case-insensitive lookup only)
1225331Samw  *		flags if multiple case-sensitive matches exist in directory
1235331Samw  *      realpnp     - (case-insensitive lookup only)
1245331Samw  *		actual name matched within the directory
125789Sahrens  *
126789Sahrens  * Return value: 0 on success or errno on failure.
127789Sahrens  *
128789Sahrens  * NOTE: Always checks for, and rejects, '.' and '..'.
1295331Samw  * NOTE: For case-insensitive file systems we take wide locks (see below),
1305331Samw  *	 but return znode pointers to a single match.
131789Sahrens  */
132789Sahrens int
133789Sahrens zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
1345331Samw     int flag, int *direntflags, pathname_t *realpnp)
135789Sahrens {
136789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
137789Sahrens 	zfs_dirlock_t	*dl;
1385331Samw 	boolean_t	update;
1395331Samw 	boolean_t	exact;
140789Sahrens 	uint64_t	zoid;
1415331Samw 	vnode_t		*vp = NULL;
1425331Samw 	int		error = 0;
1435331Samw 	int		cmpflags;
144789Sahrens 
145789Sahrens 	*zpp = NULL;
146789Sahrens 	*dlpp = NULL;
147789Sahrens 
148789Sahrens 	/*
149789Sahrens 	 * Verify that we are not trying to lock '.', '..', or '.zfs'
150789Sahrens 	 */
151789Sahrens 	if (name[0] == '.' &&
152789Sahrens 	    (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) ||
153789Sahrens 	    zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0)
154789Sahrens 		return (EEXIST);
155789Sahrens 
156789Sahrens 	/*
1575331Samw 	 * Case sensitivity and normalization preferences are set when
1585331Samw 	 * the file system is created.  These are stored in the
1595331Samw 	 * zfsvfs->z_case and zfsvfs->z_norm fields.  These choices
1605331Samw 	 * affect what vnodes can be cached in the DNLC, how we
1615331Samw 	 * perform zap lookups, and the "width" of our dirlocks.
1625331Samw 	 *
1635331Samw 	 * A normal dirlock locks a single name.  Note that with
1645331Samw 	 * normalization a name can be composed multiple ways, but
1655331Samw 	 * when normalized, these names all compare equal.  A wide
1665331Samw 	 * dirlock locks multiple names.  We need these when the file
1675331Samw 	 * system is supporting mixed-mode access.  It is sometimes
1685331Samw 	 * necessary to lock all case permutations of file name at
1695331Samw 	 * once so that simultaneous case-insensitive/case-sensitive
1705331Samw 	 * behaves as rationally as possible.
1715331Samw 	 */
1725331Samw 
1735331Samw 	/*
1745331Samw 	 * Decide if exact matches should be requested when performing
1755331Samw 	 * a zap lookup on file systems supporting case-insensitive
1765331Samw 	 * access.
1775331Samw 	 */
1785498Stimh 	exact =
1795498Stimh 	    ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE) && (flag & ZCIEXACT)) ||
1805498Stimh 	    ((zfsvfs->z_case == ZFS_CASE_MIXED) && !(flag & ZCILOOK));
1815331Samw 
1825331Samw 	/*
1835331Samw 	 * Only look in or update the DNLC if we are looking for the
1845331Samw 	 * name on a file system that does not require normalization
1855331Samw 	 * or case folding.  We can also look there if we happen to be
1865331Samw 	 * on a non-normalizing, mixed sensitivity file system IF we
1875331Samw 	 * are looking for the exact name.
1885331Samw 	 *
1895331Samw 	 * Maybe can add TO-UPPERed version of name to dnlc in ci-only
1905331Samw 	 * case for performance improvement?
1915331Samw 	 */
1925331Samw 	update = !zfsvfs->z_norm ||
1935498Stimh 	    ((zfsvfs->z_case == ZFS_CASE_MIXED) &&
1945331Samw 	    !(zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER) && !(flag & ZCILOOK));
1955331Samw 
1965331Samw 	/*
1975331Samw 	 * ZRENAMING indicates we are in a situation where we should
1985331Samw 	 * take narrow locks regardless of the file system's
1995331Samw 	 * preferences for normalizing and case folding.  This will
2005331Samw 	 * prevent us deadlocking trying to grab the same wide lock
2015331Samw 	 * twice if the two names happen to be case-insensitive
2025331Samw 	 * matches.
2035331Samw 	 */
2045331Samw 	if (flag & ZRENAMING)
2055331Samw 		cmpflags = 0;
2065331Samw 	else
2075331Samw 		cmpflags = zfsvfs->z_norm;
2085331Samw 
2095331Samw 	/*
210789Sahrens 	 * Wait until there are no locks on this name.
211789Sahrens 	 */
2123897Smaybee 	rw_enter(&dzp->z_name_lock, RW_READER);
213789Sahrens 	mutex_enter(&dzp->z_lock);
214789Sahrens 	for (;;) {
2153461Sahrens 		if (dzp->z_unlinked) {
216789Sahrens 			mutex_exit(&dzp->z_lock);
2173897Smaybee 			rw_exit(&dzp->z_name_lock);
218789Sahrens 			return (ENOENT);
219789Sahrens 		}
2205331Samw 		for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next) {
2215331Samw 			if ((u8_strcmp(name, dl->dl_name, 0, cmpflags,
2225331Samw 			    U8_UNICODE_LATEST, &error) == 0) || error != 0)
223789Sahrens 				break;
2245331Samw 		}
2255331Samw 		if (error != 0) {
2265331Samw 			mutex_exit(&dzp->z_lock);
2275331Samw 			rw_exit(&dzp->z_name_lock);
2285331Samw 			return (ENOENT);
2295331Samw 		}
230789Sahrens 		if (dl == NULL)	{
231789Sahrens 			/*
232789Sahrens 			 * Allocate a new dirlock and add it to the list.
233789Sahrens 			 */
234789Sahrens 			dl = kmem_alloc(sizeof (zfs_dirlock_t), KM_SLEEP);
235789Sahrens 			cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL);
236789Sahrens 			dl->dl_name = name;
237789Sahrens 			dl->dl_sharecnt = 0;
238789Sahrens 			dl->dl_namesize = 0;
239789Sahrens 			dl->dl_dzp = dzp;
240789Sahrens 			dl->dl_next = dzp->z_dirlocks;
241789Sahrens 			dzp->z_dirlocks = dl;
242789Sahrens 			break;
243789Sahrens 		}
244789Sahrens 		if ((flag & ZSHARED) && dl->dl_sharecnt != 0)
245789Sahrens 			break;
246789Sahrens 		cv_wait(&dl->dl_cv, &dzp->z_lock);
247789Sahrens 	}
248789Sahrens 
249789Sahrens 	if ((flag & ZSHARED) && ++dl->dl_sharecnt > 1 && dl->dl_namesize == 0) {
250789Sahrens 		/*
251789Sahrens 		 * We're the second shared reference to dl.  Make a copy of
252789Sahrens 		 * dl_name in case the first thread goes away before we do.
253789Sahrens 		 * Note that we initialize the new name before storing its
254789Sahrens 		 * pointer into dl_name, because the first thread may load
255789Sahrens 		 * dl->dl_name at any time.  He'll either see the old value,
256789Sahrens 		 * which is his, or the new shared copy; either is OK.
257789Sahrens 		 */
258789Sahrens 		dl->dl_namesize = strlen(dl->dl_name) + 1;
259789Sahrens 		name = kmem_alloc(dl->dl_namesize, KM_SLEEP);
260789Sahrens 		bcopy(dl->dl_name, name, dl->dl_namesize);
261789Sahrens 		dl->dl_name = name;
262789Sahrens 	}
263789Sahrens 
264789Sahrens 	mutex_exit(&dzp->z_lock);
265789Sahrens 
266789Sahrens 	/*
267789Sahrens 	 * We have a dirlock on the name.  (Note that it is the dirlock,
268789Sahrens 	 * not the dzp's z_lock, that protects the name in the zap object.)
269789Sahrens 	 * See if there's an object by this name; if so, put a hold on it.
270789Sahrens 	 */
271789Sahrens 	if (flag & ZXATTR) {
272789Sahrens 		zoid = dzp->z_phys->zp_xattr;
273789Sahrens 		error = (zoid == 0 ? ENOENT : 0);
274789Sahrens 	} else {
2755331Samw 		if (update)
2765331Samw 			vp = dnlc_lookup(ZTOV(dzp), name);
2771484Sek110237 		if (vp == DNLC_NO_VNODE) {
2781484Sek110237 			VN_RELE(vp);
2791484Sek110237 			error = ENOENT;
2801484Sek110237 		} else if (vp) {
2811484Sek110237 			if (flag & ZNEW) {
2821484Sek110237 				zfs_dirent_unlock(dl);
2831484Sek110237 				VN_RELE(vp);
2841484Sek110237 				return (EEXIST);
2851484Sek110237 			}
2861484Sek110237 			*dlpp = dl;
2871484Sek110237 			*zpp = VTOZ(vp);
2881484Sek110237 			return (0);
2891484Sek110237 		} else {
2905331Samw 			error = zfs_match_find(zfsvfs, dzp, name, exact,
2915331Samw 			    update, direntflags, realpnp, &zoid);
2921484Sek110237 		}
293789Sahrens 	}
294789Sahrens 	if (error) {
295789Sahrens 		if (error != ENOENT || (flag & ZEXISTS)) {
296789Sahrens 			zfs_dirent_unlock(dl);
297789Sahrens 			return (error);
298789Sahrens 		}
299789Sahrens 	} else {
300789Sahrens 		if (flag & ZNEW) {
301789Sahrens 			zfs_dirent_unlock(dl);
302789Sahrens 			return (EEXIST);
303789Sahrens 		}
304789Sahrens 		error = zfs_zget(zfsvfs, zoid, zpp);
305789Sahrens 		if (error) {
306789Sahrens 			zfs_dirent_unlock(dl);
307789Sahrens 			return (error);
308789Sahrens 		}
3095331Samw 		if (!(flag & ZXATTR) && update)
3101484Sek110237 			dnlc_update(ZTOV(dzp), name, ZTOV(*zpp));
311789Sahrens 	}
312789Sahrens 
313789Sahrens 	*dlpp = dl;
314789Sahrens 
315789Sahrens 	return (0);
316789Sahrens }
317789Sahrens 
318789Sahrens /*
319789Sahrens  * Unlock this directory entry and wake anyone who was waiting for it.
320789Sahrens  */
321789Sahrens void
322789Sahrens zfs_dirent_unlock(zfs_dirlock_t *dl)
323789Sahrens {
324789Sahrens 	znode_t *dzp = dl->dl_dzp;
325789Sahrens 	zfs_dirlock_t **prev_dl, *cur_dl;
326789Sahrens 
327789Sahrens 	mutex_enter(&dzp->z_lock);
3283897Smaybee 	rw_exit(&dzp->z_name_lock);
329789Sahrens 	if (dl->dl_sharecnt > 1) {
330789Sahrens 		dl->dl_sharecnt--;
331789Sahrens 		mutex_exit(&dzp->z_lock);
332789Sahrens 		return;
333789Sahrens 	}
334789Sahrens 	prev_dl = &dzp->z_dirlocks;
335789Sahrens 	while ((cur_dl = *prev_dl) != dl)
336789Sahrens 		prev_dl = &cur_dl->dl_next;
337789Sahrens 	*prev_dl = dl->dl_next;
338789Sahrens 	cv_broadcast(&dl->dl_cv);
339789Sahrens 	mutex_exit(&dzp->z_lock);
340789Sahrens 
341789Sahrens 	if (dl->dl_namesize != 0)
342789Sahrens 		kmem_free(dl->dl_name, dl->dl_namesize);
343789Sahrens 	cv_destroy(&dl->dl_cv);
344789Sahrens 	kmem_free(dl, sizeof (*dl));
345789Sahrens }
346789Sahrens 
347789Sahrens /*
348789Sahrens  * Look up an entry in a directory.
349789Sahrens  *
350789Sahrens  * NOTE: '.' and '..' are handled as special cases because
351789Sahrens  *	no directory entries are actually stored for them.  If this is
352789Sahrens  *	the root of a filesystem, then '.zfs' is also treated as a
353789Sahrens  *	special pseudo-directory.
354789Sahrens  */
355789Sahrens int
3565331Samw zfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp, int flags,
3575331Samw     int *deflg, pathname_t *rpnp)
358789Sahrens {
359789Sahrens 	zfs_dirlock_t *dl;
360789Sahrens 	znode_t *zp;
361789Sahrens 	int error = 0;
362789Sahrens 
363789Sahrens 	if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
364789Sahrens 		*vpp = ZTOV(dzp);
365789Sahrens 		VN_HOLD(*vpp);
366789Sahrens 	} else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
367789Sahrens 		zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
368789Sahrens 		/*
369789Sahrens 		 * If we are a snapshot mounted under .zfs, return
370789Sahrens 		 * the vp for the snapshot directory.
371789Sahrens 		 */
3721878Smaybee 		if (dzp->z_phys->zp_parent == dzp->z_id &&
3731878Smaybee 		    zfsvfs->z_parent != zfsvfs) {
374789Sahrens 			error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir,
3755331Samw 			    "snapshot", vpp, NULL, 0, NULL, kcred,
3765331Samw 			    NULL, NULL, NULL);
377789Sahrens 			return (error);
378789Sahrens 		}
379789Sahrens 		rw_enter(&dzp->z_parent_lock, RW_READER);
380789Sahrens 		error = zfs_zget(zfsvfs, dzp->z_phys->zp_parent, &zp);
381789Sahrens 		if (error == 0)
382789Sahrens 			*vpp = ZTOV(zp);
383789Sahrens 		rw_exit(&dzp->z_parent_lock);
384789Sahrens 	} else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) {
385789Sahrens 		*vpp = zfsctl_root(dzp);
386789Sahrens 	} else {
3875331Samw 		int zf;
3885331Samw 
3895331Samw 		zf = ZEXISTS | ZSHARED;
3905331Samw 		if (flags & FIGNORECASE)
3915331Samw 			zf |= ZCILOOK;
3925331Samw 
3935331Samw 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zf, deflg, rpnp);
394789Sahrens 		if (error == 0) {
395789Sahrens 			*vpp = ZTOV(zp);
396789Sahrens 			zfs_dirent_unlock(dl);
397869Sperrin 			dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
398789Sahrens 		}
3995331Samw 		rpnp = NULL;
400789Sahrens 	}
401789Sahrens 
4026492Stimh 	if ((flags & FIGNORECASE) && rpnp && !error)
4036492Stimh 		(void) strlcpy(rpnp->pn_buf, name, rpnp->pn_bufsize);
4045331Samw 
405789Sahrens 	return (error);
406789Sahrens }
407789Sahrens 
4081544Seschrock /*
4093461Sahrens  * unlinked Set (formerly known as the "delete queue") Error Handling
4101544Seschrock  *
4113461Sahrens  * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
4121544Seschrock  * don't specify the name of the entry that we will be manipulating.  We
4131544Seschrock  * also fib and say that we won't be adding any new entries to the
4143461Sahrens  * unlinked set, even though we might (this is to lower the minimum file
4151544Seschrock  * size that can be deleted in a full filesystem).  So on the small
4163461Sahrens  * chance that the nlink list is using a fat zap (ie. has more than
4171544Seschrock  * 2000 entries), we *may* not pre-read a block that's needed.
4181544Seschrock  * Therefore it is remotely possible for some of the assertions
4193461Sahrens  * regarding the unlinked set below to fail due to i/o error.  On a
4201544Seschrock  * nondebug system, this will result in the space being leaked.
4211544Seschrock  */
422789Sahrens void
4233461Sahrens zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
424789Sahrens {
425789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
426789Sahrens 
4273461Sahrens 	ASSERT(zp->z_unlinked);
428789Sahrens 	ASSERT3U(zp->z_phys->zp_links, ==, 0);
429789Sahrens 
4307046Sahrens 	VERIFY3U(0, ==,
4317046Sahrens 	    zap_add_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
4326992Smaybee }
4336992Smaybee 
434789Sahrens /*
4353461Sahrens  * Clean up any znodes that had no links when we either crashed or
4363461Sahrens  * (force) umounted the file system.
4373461Sahrens  */
4383461Sahrens void
4393461Sahrens zfs_unlinked_drain(zfsvfs_t *zfsvfs)
4403461Sahrens {
4413461Sahrens 	zap_cursor_t	zc;
4423461Sahrens 	zap_attribute_t zap;
4433461Sahrens 	dmu_object_info_t doi;
4443461Sahrens 	znode_t		*zp;
4453461Sahrens 	int		error;
4463461Sahrens 
4473461Sahrens 	/*
4483461Sahrens 	 * Interate over the contents of the unlinked set.
4493461Sahrens 	 */
4503461Sahrens 	for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
4513461Sahrens 	    zap_cursor_retrieve(&zc, &zap) == 0;
4523461Sahrens 	    zap_cursor_advance(&zc)) {
4533461Sahrens 
4543461Sahrens 		/*
4553461Sahrens 		 * See what kind of object we have in list
4563461Sahrens 		 */
4573461Sahrens 
4583461Sahrens 		error = dmu_object_info(zfsvfs->z_os,
4593461Sahrens 		    zap.za_first_integer, &doi);
4603461Sahrens 		if (error != 0)
4613461Sahrens 			continue;
4623461Sahrens 
4633461Sahrens 		ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
4643461Sahrens 		    (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
4653461Sahrens 		/*
4663461Sahrens 		 * We need to re-mark these list entries for deletion,
4673461Sahrens 		 * so we pull them back into core and set zp->z_unlinked.
4683461Sahrens 		 */
4693461Sahrens 		error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
4703461Sahrens 
4713461Sahrens 		/*
4723461Sahrens 		 * We may pick up znodes that are already marked for deletion.
4733461Sahrens 		 * This could happen during the purge of an extended attribute
4743461Sahrens 		 * directory.  All we need to do is skip over them, since they
4753461Sahrens 		 * are already in the system marked z_unlinked.
4763461Sahrens 		 */
4773461Sahrens 		if (error != 0)
4783461Sahrens 			continue;
4793461Sahrens 
4803461Sahrens 		zp->z_unlinked = B_TRUE;
4813461Sahrens 		VN_RELE(ZTOV(zp));
4823461Sahrens 	}
4833461Sahrens 	zap_cursor_fini(&zc);
4843461Sahrens }
4853461Sahrens 
4863461Sahrens /*
487789Sahrens  * Delete the entire contents of a directory.  Return a count
4885860Sck153898  * of the number of entries that could not be deleted. If we encounter
4895860Sck153898  * an error, return a count of at least one so that the directory stays
4905860Sck153898  * in the unlinked set.
491789Sahrens  *
492789Sahrens  * NOTE: this function assumes that the directory is inactive,
493789Sahrens  *	so there is no need to lock its entries before deletion.
494789Sahrens  *	Also, it assumes the directory contents is *only* regular
495789Sahrens  *	files.
496789Sahrens  */
497789Sahrens static int
498789Sahrens zfs_purgedir(znode_t *dzp)
499789Sahrens {
500789Sahrens 	zap_cursor_t	zc;
501789Sahrens 	zap_attribute_t	zap;
502789Sahrens 	znode_t		*xzp;
503789Sahrens 	dmu_tx_t	*tx;
504789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
505789Sahrens 	zfs_dirlock_t	dl;
506789Sahrens 	int skipped = 0;
507789Sahrens 	int error;
508789Sahrens 
509789Sahrens 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
510789Sahrens 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
511789Sahrens 	    zap_cursor_advance(&zc)) {
5123912Slling 		error = zfs_zget(zfsvfs,
5133912Slling 		    ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
5145860Sck153898 		if (error) {
5155860Sck153898 			skipped += 1;
5165860Sck153898 			continue;
5175860Sck153898 		}
518789Sahrens 
519789Sahrens 		ASSERT((ZTOV(xzp)->v_type == VREG) ||
520789Sahrens 		    (ZTOV(xzp)->v_type == VLNK));
521789Sahrens 
522789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
523789Sahrens 		dmu_tx_hold_bonus(tx, dzp->z_id);
5241544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
525789Sahrens 		dmu_tx_hold_bonus(tx, xzp->z_id);
5263461Sahrens 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
527789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
528789Sahrens 		if (error) {
529789Sahrens 			dmu_tx_abort(tx);
530789Sahrens 			VN_RELE(ZTOV(xzp));
531789Sahrens 			skipped += 1;
532789Sahrens 			continue;
533789Sahrens 		}
534789Sahrens 		bzero(&dl, sizeof (dl));
535789Sahrens 		dl.dl_dzp = dzp;
536789Sahrens 		dl.dl_name = zap.za_name;
537789Sahrens 
538789Sahrens 		error = zfs_link_destroy(&dl, xzp, tx, 0, NULL);
5395860Sck153898 		if (error)
5405860Sck153898 			skipped += 1;
541789Sahrens 		dmu_tx_commit(tx);
542789Sahrens 
543789Sahrens 		VN_RELE(ZTOV(xzp));
544789Sahrens 	}
545885Sahrens 	zap_cursor_fini(&zc);
5465860Sck153898 	if (error != ENOENT)
5475860Sck153898 		skipped += 1;
548789Sahrens 	return (skipped);
549789Sahrens }
550789Sahrens 
551789Sahrens void
552789Sahrens zfs_rmnode(znode_t *zp)
553789Sahrens {
554789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
555789Sahrens 	objset_t	*os = zfsvfs->z_os;
556789Sahrens 	znode_t		*xzp = NULL;
557789Sahrens 	dmu_tx_t	*tx;
558789Sahrens 	uint64_t	acl_obj;
559789Sahrens 	int		error;
560789Sahrens 
561789Sahrens 	ASSERT(ZTOV(zp)->v_count == 0);
562789Sahrens 	ASSERT(zp->z_phys->zp_links == 0);
563789Sahrens 
564789Sahrens 	/*
565789Sahrens 	 * If this is an attribute directory, purge its contents.
566789Sahrens 	 */
5673461Sahrens 	if (ZTOV(zp)->v_type == VDIR && (zp->z_phys->zp_flags & ZFS_XATTR)) {
568789Sahrens 		if (zfs_purgedir(zp) != 0) {
569789Sahrens 			/*
5703461Sahrens 			 * Not enough space to delete some xattrs.
5716992Smaybee 			 * Leave it in the unlinked set.
572789Sahrens 			 */
5735745Smaybee 			zfs_znode_dmu_fini(zp);
5745745Smaybee 			zfs_znode_free(zp);
575789Sahrens 			return;
576789Sahrens 		}
5773461Sahrens 	}
578789Sahrens 
579789Sahrens 	/*
5806992Smaybee 	 * Free up all the data in the file.
5816992Smaybee 	 */
5826992Smaybee 	error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
5836992Smaybee 	if (error) {
5846992Smaybee 		/*
5856992Smaybee 		 * Not enough space.  Leave the file in the unlinked set.
5866992Smaybee 		 */
5876992Smaybee 		zfs_znode_dmu_fini(zp);
5886992Smaybee 		zfs_znode_free(zp);
5896992Smaybee 		return;
5906992Smaybee 	}
5916992Smaybee 
5926992Smaybee 	/*
5933461Sahrens 	 * If the file has extended attributes, we're going to unlink
5943461Sahrens 	 * the xattr dir.
595789Sahrens 	 */
596789Sahrens 	if (zp->z_phys->zp_xattr) {
597789Sahrens 		error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
598789Sahrens 		ASSERT(error == 0);
599789Sahrens 	}
600789Sahrens 
601789Sahrens 	acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
602789Sahrens 
603789Sahrens 	/*
6046992Smaybee 	 * Set up the final transaction.
605789Sahrens 	 */
606789Sahrens 	tx = dmu_tx_create(os);
607789Sahrens 	dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
6083461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
609789Sahrens 	if (xzp) {
610789Sahrens 		dmu_tx_hold_bonus(tx, xzp->z_id);
6113461Sahrens 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
612789Sahrens 	}
613789Sahrens 	if (acl_obj)
614789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
615789Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
616789Sahrens 	if (error) {
6173461Sahrens 		/*
6183461Sahrens 		 * Not enough space to delete the file.  Leave it in the
6193461Sahrens 		 * unlinked set, leaking it until the fs is remounted (at
6203461Sahrens 		 * which point we'll call zfs_unlinked_drain() to process it).
6213461Sahrens 		 */
622789Sahrens 		dmu_tx_abort(tx);
6235745Smaybee 		zfs_znode_dmu_fini(zp);
6245745Smaybee 		zfs_znode_free(zp);
6255745Smaybee 		goto out;
626789Sahrens 	}
627789Sahrens 
628789Sahrens 	if (xzp) {
629789Sahrens 		dmu_buf_will_dirty(xzp->z_dbuf, tx);
630789Sahrens 		mutex_enter(&xzp->z_lock);
6313461Sahrens 		xzp->z_unlinked = B_TRUE;	/* mark xzp for deletion */
632789Sahrens 		xzp->z_phys->zp_links = 0;	/* no more links to it */
633789Sahrens 		mutex_exit(&xzp->z_lock);
6343461Sahrens 		zfs_unlinked_add(xzp, tx);
635789Sahrens 	}
636789Sahrens 
6373461Sahrens 	/* Remove this znode from the unlinked set */
6387046Sahrens 	VERIFY3U(0, ==,
6397046Sahrens 	    zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
640789Sahrens 
641789Sahrens 	zfs_znode_delete(zp, tx);
642789Sahrens 
643789Sahrens 	dmu_tx_commit(tx);
6445745Smaybee out:
645789Sahrens 	if (xzp)
646789Sahrens 		VN_RELE(ZTOV(xzp));
647789Sahrens }
648789Sahrens 
6494577Sahrens static uint64_t
6504577Sahrens zfs_dirent(znode_t *zp)
6514577Sahrens {
6524577Sahrens 	uint64_t de = zp->z_id;
6534577Sahrens 	if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
6544577Sahrens 		de |= IFTODT((zp)->z_phys->zp_mode) << 60;
6554577Sahrens 	return (de);
6564577Sahrens }
6574577Sahrens 
658789Sahrens /*
6593461Sahrens  * Link zp into dl.  Can only fail if zp has been unlinked.
660789Sahrens  */
661789Sahrens int
662789Sahrens zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
663789Sahrens {
664789Sahrens 	znode_t *dzp = dl->dl_dzp;
665789Sahrens 	vnode_t *vp = ZTOV(zp);
6663912Slling 	uint64_t value;
667789Sahrens 	int zp_is_dir = (vp->v_type == VDIR);
668789Sahrens 	int error;
669789Sahrens 
670789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
671789Sahrens 	mutex_enter(&zp->z_lock);
672789Sahrens 
673789Sahrens 	if (!(flag & ZRENAMING)) {
6743461Sahrens 		if (zp->z_unlinked) {	/* no new links to unlinked zp */
675789Sahrens 			ASSERT(!(flag & (ZNEW | ZEXISTS)));
676789Sahrens 			mutex_exit(&zp->z_lock);
677789Sahrens 			return (ENOENT);
678789Sahrens 		}
679789Sahrens 		zp->z_phys->zp_links++;
680789Sahrens 	}
681789Sahrens 	zp->z_phys->zp_parent = dzp->z_id;	/* dzp is now zp's parent */
682789Sahrens 
683789Sahrens 	if (!(flag & ZNEW))
684789Sahrens 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
685789Sahrens 	mutex_exit(&zp->z_lock);
686789Sahrens 
687789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
688789Sahrens 	mutex_enter(&dzp->z_lock);
689789Sahrens 	dzp->z_phys->zp_size++;			/* one dirent added */
690789Sahrens 	dzp->z_phys->zp_links += zp_is_dir;	/* ".." link from zp */
691789Sahrens 	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
692789Sahrens 	mutex_exit(&dzp->z_lock);
693789Sahrens 
6944577Sahrens 	value = zfs_dirent(zp);
695789Sahrens 	error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
6963912Slling 	    8, 1, &value, tx);
697789Sahrens 	ASSERT(error == 0);
698789Sahrens 
6991484Sek110237 	dnlc_update(ZTOV(dzp), dl->dl_name, vp);
7001484Sek110237 
701789Sahrens 	return (0);
702789Sahrens }
703789Sahrens 
704789Sahrens /*
7053461Sahrens  * Unlink zp from dl, and mark zp for deletion if this was the last link.
706789Sahrens  * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
7073461Sahrens  * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
7083461Sahrens  * If it's non-NULL, we use it to indicate whether the znode needs deletion,
709789Sahrens  * and it's the caller's job to do it.
710789Sahrens  */
711789Sahrens int
712789Sahrens zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
7133461Sahrens 	boolean_t *unlinkedp)
714789Sahrens {
715789Sahrens 	znode_t *dzp = dl->dl_dzp;
716789Sahrens 	vnode_t *vp = ZTOV(zp);
717789Sahrens 	int zp_is_dir = (vp->v_type == VDIR);
7183461Sahrens 	boolean_t unlinked = B_FALSE;
719789Sahrens 	int error;
720789Sahrens 
7211484Sek110237 	dnlc_remove(ZTOV(dzp), dl->dl_name);
7221484Sek110237 
723789Sahrens 	if (!(flag & ZRENAMING)) {
724789Sahrens 		dmu_buf_will_dirty(zp->z_dbuf, tx);
725789Sahrens 
726789Sahrens 		if (vn_vfswlock(vp))		/* prevent new mounts on zp */
727789Sahrens 			return (EBUSY);
728789Sahrens 
729789Sahrens 		if (vn_ismntpt(vp)) {		/* don't remove mount point */
730789Sahrens 			vn_vfsunlock(vp);
731789Sahrens 			return (EBUSY);
732789Sahrens 		}
733789Sahrens 
734789Sahrens 		mutex_enter(&zp->z_lock);
735789Sahrens 		if (zp_is_dir && !zfs_dirempty(zp)) {	/* dir not empty */
736789Sahrens 			mutex_exit(&zp->z_lock);
737789Sahrens 			vn_vfsunlock(vp);
738789Sahrens 			return (EEXIST);
739789Sahrens 		}
7403713Sahrens 		if (zp->z_phys->zp_links <= zp_is_dir) {
7413713Sahrens 			zfs_panic_recover("zfs: link count on %s is %u, "
7423713Sahrens 			    "should be at least %u",
7433713Sahrens 			    zp->z_vnode->v_path ? zp->z_vnode->v_path :
7443713Sahrens 			    "<unknown>", (int)zp->z_phys->zp_links,
7453713Sahrens 			    zp_is_dir + 1);
7463713Sahrens 			zp->z_phys->zp_links = zp_is_dir + 1;
7473713Sahrens 		}
748789Sahrens 		if (--zp->z_phys->zp_links == zp_is_dir) {
7493461Sahrens 			zp->z_unlinked = B_TRUE;
750789Sahrens 			zp->z_phys->zp_links = 0;
7513461Sahrens 			unlinked = B_TRUE;
752789Sahrens 		} else {
753789Sahrens 			zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
754789Sahrens 		}
755789Sahrens 		mutex_exit(&zp->z_lock);
756789Sahrens 		vn_vfsunlock(vp);
757789Sahrens 	}
758789Sahrens 
759789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
760789Sahrens 	mutex_enter(&dzp->z_lock);
761789Sahrens 	dzp->z_phys->zp_size--;			/* one dirent removed */
762789Sahrens 	dzp->z_phys->zp_links -= zp_is_dir;	/* ".." link from zp */
763789Sahrens 	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
764789Sahrens 	mutex_exit(&dzp->z_lock);
765789Sahrens 
7665331Samw 	if (zp->z_zfsvfs->z_norm) {
7675498Stimh 		if (((zp->z_zfsvfs->z_case == ZFS_CASE_INSENSITIVE) &&
7685331Samw 		    (flag & ZCIEXACT)) ||
7695498Stimh 		    ((zp->z_zfsvfs->z_case == ZFS_CASE_MIXED) &&
7705331Samw 		    !(flag & ZCILOOK)))
7715331Samw 			error = zap_remove_norm(zp->z_zfsvfs->z_os,
7725331Samw 			    dzp->z_id, dl->dl_name, MT_EXACT, tx);
7735331Samw 		else
7745331Samw 			error = zap_remove_norm(zp->z_zfsvfs->z_os,
7755331Samw 			    dzp->z_id, dl->dl_name, MT_FIRST, tx);
7765331Samw 	} else {
7775331Samw 		error = zap_remove(zp->z_zfsvfs->z_os,
7785331Samw 		    dzp->z_id, dl->dl_name, tx);
7795331Samw 	}
780789Sahrens 	ASSERT(error == 0);
781789Sahrens 
7823461Sahrens 	if (unlinkedp != NULL)
7833461Sahrens 		*unlinkedp = unlinked;
7843461Sahrens 	else if (unlinked)
7853461Sahrens 		zfs_unlinked_add(zp, tx);
786789Sahrens 
787789Sahrens 	return (0);
788789Sahrens }
789789Sahrens 
790789Sahrens /*
791789Sahrens  * Indicate whether the directory is empty.  Works with or without z_lock
792789Sahrens  * held, but can only be consider a hint in the latter case.  Returns true
793789Sahrens  * if only "." and ".." remain and there's no work in progress.
794789Sahrens  */
795789Sahrens boolean_t
796789Sahrens zfs_dirempty(znode_t *dzp)
797789Sahrens {
798789Sahrens 	return (dzp->z_phys->zp_size == 2 && dzp->z_dirlocks == 0);
799789Sahrens }
800789Sahrens 
801789Sahrens int
802789Sahrens zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
803789Sahrens {
804789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
805789Sahrens 	znode_t *xzp;
806789Sahrens 	dmu_tx_t *tx;
807789Sahrens 	int error;
808*9179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t acl_ids;
809*9179SMark.Shellenbaum@Sun.COM 	boolean_t fuid_dirtied;
810789Sahrens 
811789Sahrens 	*xvpp = NULL;
812789Sahrens 
8135331Samw 	if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr))
814789Sahrens 		return (error);
815789Sahrens 
816*9179SMark.Shellenbaum@Sun.COM 	if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL,
817*9179SMark.Shellenbaum@Sun.COM 	    &acl_ids)) != 0)
818*9179SMark.Shellenbaum@Sun.COM 		return (error);
819*9179SMark.Shellenbaum@Sun.COM 
820789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
821789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
8221544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
823*9179SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
824*9179SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied) {
8255824Smarks 		if (zfsvfs->z_fuid_obj == 0) {
8265824Smarks 			dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
8275824Smarks 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
8285824Smarks 			    FUID_SIZE_ESTIMATE(zfsvfs));
8295824Smarks 			dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
8305824Smarks 		} else {
8315824Smarks 			dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
8325824Smarks 			dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
8335824Smarks 			    FUID_SIZE_ESTIMATE(zfsvfs));
8345824Smarks 		}
8355331Samw 	}
8368227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
837789Sahrens 	if (error) {
838*9179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
8398227SNeil.Perrin@Sun.COM 		if (error == ERESTART)
8402113Sahrens 			dmu_tx_wait(tx);
841789Sahrens 		dmu_tx_abort(tx);
842789Sahrens 		return (error);
843789Sahrens 	}
844*9179SMark.Shellenbaum@Sun.COM 	zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, 0, &acl_ids);
845*9179SMark.Shellenbaum@Sun.COM 
846*9179SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
847*9179SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
848*9179SMark.Shellenbaum@Sun.COM 
849789Sahrens 	ASSERT(xzp->z_phys->zp_parent == zp->z_id);
850789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
8515446Sahrens 	zp->z_phys->zp_xattr = xzp->z_id;
852789Sahrens 
8535331Samw 	(void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
854*9179SMark.Shellenbaum@Sun.COM 	    xzp, "", NULL, acl_ids.z_fuidp, vap);
855*9179SMark.Shellenbaum@Sun.COM 
856*9179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_free(&acl_ids);
857789Sahrens 	dmu_tx_commit(tx);
858789Sahrens 
859789Sahrens 	*xvpp = ZTOV(xzp);
860789Sahrens 
861789Sahrens 	return (0);
862789Sahrens }
863789Sahrens 
864789Sahrens /*
865789Sahrens  * Return a znode for the extended attribute directory for zp.
866789Sahrens  * ** If the directory does not already exist, it is created **
867789Sahrens  *
868789Sahrens  *	IN:	zp	- znode to obtain attribute directory from
869789Sahrens  *		cr	- credentials of caller
8703280Sck153898  *		flags	- flags from the VOP_LOOKUP call
871789Sahrens  *
872789Sahrens  *	OUT:	xzpp	- pointer to extended attribute znode
873789Sahrens  *
874789Sahrens  *	RETURN:	0 on success
875789Sahrens  *		error number on failure
876789Sahrens  */
877789Sahrens int
8783280Sck153898 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
879789Sahrens {
880789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
881789Sahrens 	znode_t		*xzp;
882789Sahrens 	zfs_dirlock_t	*dl;
883789Sahrens 	vattr_t		va;
884789Sahrens 	int		error;
885789Sahrens top:
8865331Samw 	error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR, NULL, NULL);
887789Sahrens 	if (error)
888789Sahrens 		return (error);
889789Sahrens 
890789Sahrens 	if (xzp != NULL) {
891789Sahrens 		*xvpp = ZTOV(xzp);
892789Sahrens 		zfs_dirent_unlock(dl);
893789Sahrens 		return (0);
894789Sahrens 	}
895789Sahrens 
896789Sahrens 	ASSERT(zp->z_phys->zp_xattr == 0);
897789Sahrens 
8983280Sck153898 	if (!(flags & CREATE_XATTR_DIR)) {
8993280Sck153898 		zfs_dirent_unlock(dl);
9003280Sck153898 		return (ENOENT);
9013280Sck153898 	}
9023280Sck153898 
903789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
904789Sahrens 		zfs_dirent_unlock(dl);
905789Sahrens 		return (EROFS);
906789Sahrens 	}
907789Sahrens 
908789Sahrens 	/*
909789Sahrens 	 * The ability to 'create' files in an attribute
910789Sahrens 	 * directory comes from the write_xattr permission on the base file.
911789Sahrens 	 *
912789Sahrens 	 * The ability to 'search' an attribute directory requires
913789Sahrens 	 * read_xattr permission on the base file.
914789Sahrens 	 *
915789Sahrens 	 * Once in a directory the ability to read/write attributes
916789Sahrens 	 * is controlled by the permissions on the attribute file.
917789Sahrens 	 */
918789Sahrens 	va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
919789Sahrens 	va.va_type = VDIR;
9201231Smarks 	va.va_mode = S_IFDIR | S_ISVTX | 0777;
9215771Sjp151216 	zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
922789Sahrens 
923789Sahrens 	error = zfs_make_xattrdir(zp, &va, xvpp, cr);
924789Sahrens 	zfs_dirent_unlock(dl);
925789Sahrens 
9268227SNeil.Perrin@Sun.COM 	if (error == ERESTART) {
9272113Sahrens 		/* NB: we already did dmu_tx_wait() if necessary */
928789Sahrens 		goto top;
929789Sahrens 	}
930789Sahrens 
931789Sahrens 	return (error);
932789Sahrens }
933789Sahrens 
934789Sahrens /*
935789Sahrens  * Decide whether it is okay to remove within a sticky directory.
936789Sahrens  *
937789Sahrens  * In sticky directories, write access is not sufficient;
938789Sahrens  * you can remove entries from a directory only if:
939789Sahrens  *
940789Sahrens  *	you own the directory,
941789Sahrens  *	you own the entry,
942789Sahrens  *	the entry is a plain file and you have write access,
943789Sahrens  *	or you are privileged (checked in secpolicy...).
944789Sahrens  *
945789Sahrens  * The function returns 0 if remove access is granted.
946789Sahrens  */
947789Sahrens int
948789Sahrens zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
949789Sahrens {
950789Sahrens 	uid_t  		uid;
9515331Samw 	uid_t		downer;
9525331Samw 	uid_t		fowner;
9535331Samw 	zfsvfs_t	*zfsvfs = zdp->z_zfsvfs;
954789Sahrens 
9558227SNeil.Perrin@Sun.COM 	if (zdp->z_zfsvfs->z_replay)
956789Sahrens 		return (0);
957789Sahrens 
9585331Samw 	if ((zdp->z_phys->zp_mode & S_ISVTX) == 0)
9595331Samw 		return (0);
9605331Samw 
9615959Smarks 	downer = zfs_fuid_map_id(zfsvfs, zdp->z_phys->zp_uid, cr, ZFS_OWNER);
9625959Smarks 	fowner = zfs_fuid_map_id(zfsvfs, zp->z_phys->zp_uid, cr, ZFS_OWNER);
9635331Samw 
9645331Samw 	if ((uid = crgetuid(cr)) == downer || uid == fowner ||
965789Sahrens 	    (ZTOV(zp)->v_type == VREG &&
9665331Samw 	    zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
967789Sahrens 		return (0);
968789Sahrens 	else
969789Sahrens 		return (secpolicy_vnode_remove(cr));
970789Sahrens }
971