xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_dir.c (revision 11321:506b7043a14c)
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 /*
229179SMark.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
117*11321SSanjeev.Bagewadi@Sun.COM  *		  ZHAVELOCK: Don't grab the z_name_lock for this call. The
118*11321SSanjeev.Bagewadi@Sun.COM  *			     current thread already holds it.
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.
213*11321SSanjeev.Bagewadi@Sun.COM 	 *
214*11321SSanjeev.Bagewadi@Sun.COM 	 * Don't grab the the lock if it is already held. However, cannot
215*11321SSanjeev.Bagewadi@Sun.COM 	 * have both ZSHARED and ZHAVELOCK together.
216789Sahrens 	 */
217*11321SSanjeev.Bagewadi@Sun.COM 	ASSERT(!(flag & ZSHARED) || !(flag & ZHAVELOCK));
218*11321SSanjeev.Bagewadi@Sun.COM 	if (!(flag & ZHAVELOCK))
219*11321SSanjeev.Bagewadi@Sun.COM 		rw_enter(&dzp->z_name_lock, RW_READER);
220*11321SSanjeev.Bagewadi@Sun.COM 
221789Sahrens 	mutex_enter(&dzp->z_lock);
222789Sahrens 	for (;;) {
2233461Sahrens 		if (dzp->z_unlinked) {
224789Sahrens 			mutex_exit(&dzp->z_lock);
225*11321SSanjeev.Bagewadi@Sun.COM 			if (!(flag & ZHAVELOCK))
226*11321SSanjeev.Bagewadi@Sun.COM 				rw_exit(&dzp->z_name_lock);
227789Sahrens 			return (ENOENT);
228789Sahrens 		}
2295331Samw 		for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next) {
2305331Samw 			if ((u8_strcmp(name, dl->dl_name, 0, cmpflags,
2315331Samw 			    U8_UNICODE_LATEST, &error) == 0) || error != 0)
232789Sahrens 				break;
2335331Samw 		}
2345331Samw 		if (error != 0) {
2355331Samw 			mutex_exit(&dzp->z_lock);
236*11321SSanjeev.Bagewadi@Sun.COM 			if (!(flag & ZHAVELOCK))
237*11321SSanjeev.Bagewadi@Sun.COM 				rw_exit(&dzp->z_name_lock);
2385331Samw 			return (ENOENT);
2395331Samw 		}
240789Sahrens 		if (dl == NULL)	{
241789Sahrens 			/*
242789Sahrens 			 * Allocate a new dirlock and add it to the list.
243789Sahrens 			 */
244789Sahrens 			dl = kmem_alloc(sizeof (zfs_dirlock_t), KM_SLEEP);
245789Sahrens 			cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL);
246789Sahrens 			dl->dl_name = name;
247789Sahrens 			dl->dl_sharecnt = 0;
248*11321SSanjeev.Bagewadi@Sun.COM 			dl->dl_namelock = 0;
249789Sahrens 			dl->dl_namesize = 0;
250789Sahrens 			dl->dl_dzp = dzp;
251789Sahrens 			dl->dl_next = dzp->z_dirlocks;
252789Sahrens 			dzp->z_dirlocks = dl;
253789Sahrens 			break;
254789Sahrens 		}
255789Sahrens 		if ((flag & ZSHARED) && dl->dl_sharecnt != 0)
256789Sahrens 			break;
257789Sahrens 		cv_wait(&dl->dl_cv, &dzp->z_lock);
258789Sahrens 	}
259789Sahrens 
260*11321SSanjeev.Bagewadi@Sun.COM 	/*
261*11321SSanjeev.Bagewadi@Sun.COM 	 * If the z_name_lock was NOT held for this dirlock record it.
262*11321SSanjeev.Bagewadi@Sun.COM 	 */
263*11321SSanjeev.Bagewadi@Sun.COM 	if (flag & ZHAVELOCK)
264*11321SSanjeev.Bagewadi@Sun.COM 		dl->dl_namelock = 1;
265*11321SSanjeev.Bagewadi@Sun.COM 
266789Sahrens 	if ((flag & ZSHARED) && ++dl->dl_sharecnt > 1 && dl->dl_namesize == 0) {
267789Sahrens 		/*
268789Sahrens 		 * We're the second shared reference to dl.  Make a copy of
269789Sahrens 		 * dl_name in case the first thread goes away before we do.
270789Sahrens 		 * Note that we initialize the new name before storing its
271789Sahrens 		 * pointer into dl_name, because the first thread may load
272789Sahrens 		 * dl->dl_name at any time.  He'll either see the old value,
273789Sahrens 		 * which is his, or the new shared copy; either is OK.
274789Sahrens 		 */
275789Sahrens 		dl->dl_namesize = strlen(dl->dl_name) + 1;
276789Sahrens 		name = kmem_alloc(dl->dl_namesize, KM_SLEEP);
277789Sahrens 		bcopy(dl->dl_name, name, dl->dl_namesize);
278789Sahrens 		dl->dl_name = name;
279789Sahrens 	}
280789Sahrens 
281789Sahrens 	mutex_exit(&dzp->z_lock);
282789Sahrens 
283789Sahrens 	/*
284789Sahrens 	 * We have a dirlock on the name.  (Note that it is the dirlock,
285789Sahrens 	 * not the dzp's z_lock, that protects the name in the zap object.)
286789Sahrens 	 * See if there's an object by this name; if so, put a hold on it.
287789Sahrens 	 */
288789Sahrens 	if (flag & ZXATTR) {
289789Sahrens 		zoid = dzp->z_phys->zp_xattr;
290789Sahrens 		error = (zoid == 0 ? ENOENT : 0);
291789Sahrens 	} else {
2925331Samw 		if (update)
2935331Samw 			vp = dnlc_lookup(ZTOV(dzp), name);
2941484Sek110237 		if (vp == DNLC_NO_VNODE) {
2951484Sek110237 			VN_RELE(vp);
2961484Sek110237 			error = ENOENT;
2971484Sek110237 		} else if (vp) {
2981484Sek110237 			if (flag & ZNEW) {
2991484Sek110237 				zfs_dirent_unlock(dl);
3001484Sek110237 				VN_RELE(vp);
3011484Sek110237 				return (EEXIST);
3021484Sek110237 			}
3031484Sek110237 			*dlpp = dl;
3041484Sek110237 			*zpp = VTOZ(vp);
3051484Sek110237 			return (0);
3061484Sek110237 		} else {
3075331Samw 			error = zfs_match_find(zfsvfs, dzp, name, exact,
3085331Samw 			    update, direntflags, realpnp, &zoid);
3091484Sek110237 		}
310789Sahrens 	}
311789Sahrens 	if (error) {
312789Sahrens 		if (error != ENOENT || (flag & ZEXISTS)) {
313789Sahrens 			zfs_dirent_unlock(dl);
314789Sahrens 			return (error);
315789Sahrens 		}
316789Sahrens 	} else {
317789Sahrens 		if (flag & ZNEW) {
318789Sahrens 			zfs_dirent_unlock(dl);
319789Sahrens 			return (EEXIST);
320789Sahrens 		}
321789Sahrens 		error = zfs_zget(zfsvfs, zoid, zpp);
322789Sahrens 		if (error) {
323789Sahrens 			zfs_dirent_unlock(dl);
324789Sahrens 			return (error);
325789Sahrens 		}
3265331Samw 		if (!(flag & ZXATTR) && update)
3271484Sek110237 			dnlc_update(ZTOV(dzp), name, ZTOV(*zpp));
328789Sahrens 	}
329789Sahrens 
330789Sahrens 	*dlpp = dl;
331789Sahrens 
332789Sahrens 	return (0);
333789Sahrens }
334789Sahrens 
335789Sahrens /*
336789Sahrens  * Unlock this directory entry and wake anyone who was waiting for it.
337789Sahrens  */
338789Sahrens void
339789Sahrens zfs_dirent_unlock(zfs_dirlock_t *dl)
340789Sahrens {
341789Sahrens 	znode_t *dzp = dl->dl_dzp;
342789Sahrens 	zfs_dirlock_t **prev_dl, *cur_dl;
343789Sahrens 
344789Sahrens 	mutex_enter(&dzp->z_lock);
345*11321SSanjeev.Bagewadi@Sun.COM 
346*11321SSanjeev.Bagewadi@Sun.COM 	if (!dl->dl_namelock)
347*11321SSanjeev.Bagewadi@Sun.COM 		rw_exit(&dzp->z_name_lock);
348*11321SSanjeev.Bagewadi@Sun.COM 
349789Sahrens 	if (dl->dl_sharecnt > 1) {
350789Sahrens 		dl->dl_sharecnt--;
351789Sahrens 		mutex_exit(&dzp->z_lock);
352789Sahrens 		return;
353789Sahrens 	}
354789Sahrens 	prev_dl = &dzp->z_dirlocks;
355789Sahrens 	while ((cur_dl = *prev_dl) != dl)
356789Sahrens 		prev_dl = &cur_dl->dl_next;
357789Sahrens 	*prev_dl = dl->dl_next;
358789Sahrens 	cv_broadcast(&dl->dl_cv);
359789Sahrens 	mutex_exit(&dzp->z_lock);
360789Sahrens 
361789Sahrens 	if (dl->dl_namesize != 0)
362789Sahrens 		kmem_free(dl->dl_name, dl->dl_namesize);
363789Sahrens 	cv_destroy(&dl->dl_cv);
364789Sahrens 	kmem_free(dl, sizeof (*dl));
365789Sahrens }
366789Sahrens 
367789Sahrens /*
368789Sahrens  * Look up an entry in a directory.
369789Sahrens  *
370789Sahrens  * NOTE: '.' and '..' are handled as special cases because
371789Sahrens  *	no directory entries are actually stored for them.  If this is
372789Sahrens  *	the root of a filesystem, then '.zfs' is also treated as a
373789Sahrens  *	special pseudo-directory.
374789Sahrens  */
375789Sahrens int
3765331Samw zfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp, int flags,
3775331Samw     int *deflg, pathname_t *rpnp)
378789Sahrens {
379789Sahrens 	zfs_dirlock_t *dl;
380789Sahrens 	znode_t *zp;
381789Sahrens 	int error = 0;
382789Sahrens 
383789Sahrens 	if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
384789Sahrens 		*vpp = ZTOV(dzp);
385789Sahrens 		VN_HOLD(*vpp);
386789Sahrens 	} else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
387789Sahrens 		zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
388789Sahrens 		/*
389789Sahrens 		 * If we are a snapshot mounted under .zfs, return
390789Sahrens 		 * the vp for the snapshot directory.
391789Sahrens 		 */
3921878Smaybee 		if (dzp->z_phys->zp_parent == dzp->z_id &&
3931878Smaybee 		    zfsvfs->z_parent != zfsvfs) {
394789Sahrens 			error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir,
3955331Samw 			    "snapshot", vpp, NULL, 0, NULL, kcred,
3965331Samw 			    NULL, NULL, NULL);
397789Sahrens 			return (error);
398789Sahrens 		}
399789Sahrens 		rw_enter(&dzp->z_parent_lock, RW_READER);
400789Sahrens 		error = zfs_zget(zfsvfs, dzp->z_phys->zp_parent, &zp);
401789Sahrens 		if (error == 0)
402789Sahrens 			*vpp = ZTOV(zp);
403789Sahrens 		rw_exit(&dzp->z_parent_lock);
404789Sahrens 	} else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) {
405789Sahrens 		*vpp = zfsctl_root(dzp);
406789Sahrens 	} else {
4075331Samw 		int zf;
4085331Samw 
4095331Samw 		zf = ZEXISTS | ZSHARED;
4105331Samw 		if (flags & FIGNORECASE)
4115331Samw 			zf |= ZCILOOK;
4125331Samw 
4135331Samw 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zf, deflg, rpnp);
414789Sahrens 		if (error == 0) {
415789Sahrens 			*vpp = ZTOV(zp);
416789Sahrens 			zfs_dirent_unlock(dl);
417869Sperrin 			dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
418789Sahrens 		}
4195331Samw 		rpnp = NULL;
420789Sahrens 	}
421789Sahrens 
4226492Stimh 	if ((flags & FIGNORECASE) && rpnp && !error)
4236492Stimh 		(void) strlcpy(rpnp->pn_buf, name, rpnp->pn_bufsize);
4245331Samw 
425789Sahrens 	return (error);
426789Sahrens }
427789Sahrens 
4281544Seschrock /*
4293461Sahrens  * unlinked Set (formerly known as the "delete queue") Error Handling
4301544Seschrock  *
4313461Sahrens  * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
4321544Seschrock  * don't specify the name of the entry that we will be manipulating.  We
4331544Seschrock  * also fib and say that we won't be adding any new entries to the
4343461Sahrens  * unlinked set, even though we might (this is to lower the minimum file
4351544Seschrock  * size that can be deleted in a full filesystem).  So on the small
4363461Sahrens  * chance that the nlink list is using a fat zap (ie. has more than
4371544Seschrock  * 2000 entries), we *may* not pre-read a block that's needed.
4381544Seschrock  * Therefore it is remotely possible for some of the assertions
4393461Sahrens  * regarding the unlinked set below to fail due to i/o error.  On a
4401544Seschrock  * nondebug system, this will result in the space being leaked.
4411544Seschrock  */
442789Sahrens void
4433461Sahrens zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
444789Sahrens {
445789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
446789Sahrens 
4473461Sahrens 	ASSERT(zp->z_unlinked);
448789Sahrens 	ASSERT3U(zp->z_phys->zp_links, ==, 0);
449789Sahrens 
4507046Sahrens 	VERIFY3U(0, ==,
4517046Sahrens 	    zap_add_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
4526992Smaybee }
4536992Smaybee 
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
5085860Sck153898  * of the number of entries that could not be deleted. If we encounter
5095860Sck153898  * an error, return a count of at least one so that the directory stays
5105860Sck153898  * in the unlinked set.
511789Sahrens  *
512789Sahrens  * NOTE: this function assumes that the directory is inactive,
513789Sahrens  *	so there is no need to lock its entries before deletion.
514789Sahrens  *	Also, it assumes the directory contents is *only* regular
515789Sahrens  *	files.
516789Sahrens  */
517789Sahrens static int
518789Sahrens zfs_purgedir(znode_t *dzp)
519789Sahrens {
520789Sahrens 	zap_cursor_t	zc;
521789Sahrens 	zap_attribute_t	zap;
522789Sahrens 	znode_t		*xzp;
523789Sahrens 	dmu_tx_t	*tx;
524789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
525789Sahrens 	zfs_dirlock_t	dl;
526789Sahrens 	int skipped = 0;
527789Sahrens 	int error;
528789Sahrens 
529789Sahrens 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
530789Sahrens 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
531789Sahrens 	    zap_cursor_advance(&zc)) {
5323912Slling 		error = zfs_zget(zfsvfs,
5333912Slling 		    ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
5345860Sck153898 		if (error) {
5355860Sck153898 			skipped += 1;
5365860Sck153898 			continue;
5375860Sck153898 		}
538789Sahrens 
539789Sahrens 		ASSERT((ZTOV(xzp)->v_type == VREG) ||
540789Sahrens 		    (ZTOV(xzp)->v_type == VLNK));
541789Sahrens 
542789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
543789Sahrens 		dmu_tx_hold_bonus(tx, dzp->z_id);
5441544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
545789Sahrens 		dmu_tx_hold_bonus(tx, xzp->z_id);
5463461Sahrens 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
547789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
548789Sahrens 		if (error) {
549789Sahrens 			dmu_tx_abort(tx);
550789Sahrens 			VN_RELE(ZTOV(xzp));
551789Sahrens 			skipped += 1;
552789Sahrens 			continue;
553789Sahrens 		}
554789Sahrens 		bzero(&dl, sizeof (dl));
555789Sahrens 		dl.dl_dzp = dzp;
556789Sahrens 		dl.dl_name = zap.za_name;
557789Sahrens 
558789Sahrens 		error = zfs_link_destroy(&dl, xzp, tx, 0, NULL);
5595860Sck153898 		if (error)
5605860Sck153898 			skipped += 1;
561789Sahrens 		dmu_tx_commit(tx);
562789Sahrens 
563789Sahrens 		VN_RELE(ZTOV(xzp));
564789Sahrens 	}
565885Sahrens 	zap_cursor_fini(&zc);
5665860Sck153898 	if (error != ENOENT)
5675860Sck153898 		skipped += 1;
568789Sahrens 	return (skipped);
569789Sahrens }
570789Sahrens 
571789Sahrens void
572789Sahrens zfs_rmnode(znode_t *zp)
573789Sahrens {
574789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
575789Sahrens 	objset_t	*os = zfsvfs->z_os;
576789Sahrens 	znode_t		*xzp = NULL;
577789Sahrens 	dmu_tx_t	*tx;
578789Sahrens 	uint64_t	acl_obj;
579789Sahrens 	int		error;
580789Sahrens 
581789Sahrens 	ASSERT(ZTOV(zp)->v_count == 0);
582789Sahrens 	ASSERT(zp->z_phys->zp_links == 0);
583789Sahrens 
584789Sahrens 	/*
585789Sahrens 	 * If this is an attribute directory, purge its contents.
586789Sahrens 	 */
5873461Sahrens 	if (ZTOV(zp)->v_type == VDIR && (zp->z_phys->zp_flags & ZFS_XATTR)) {
588789Sahrens 		if (zfs_purgedir(zp) != 0) {
589789Sahrens 			/*
5903461Sahrens 			 * Not enough space to delete some xattrs.
5916992Smaybee 			 * Leave it in the unlinked set.
592789Sahrens 			 */
5935745Smaybee 			zfs_znode_dmu_fini(zp);
5945745Smaybee 			zfs_znode_free(zp);
595789Sahrens 			return;
596789Sahrens 		}
5973461Sahrens 	}
598789Sahrens 
599789Sahrens 	/*
6006992Smaybee 	 * Free up all the data in the file.
6016992Smaybee 	 */
6026992Smaybee 	error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
6036992Smaybee 	if (error) {
6046992Smaybee 		/*
6056992Smaybee 		 * Not enough space.  Leave the file in the unlinked set.
6066992Smaybee 		 */
6076992Smaybee 		zfs_znode_dmu_fini(zp);
6086992Smaybee 		zfs_znode_free(zp);
6096992Smaybee 		return;
6106992Smaybee 	}
6116992Smaybee 
6126992Smaybee 	/*
6133461Sahrens 	 * If the file has extended attributes, we're going to unlink
6143461Sahrens 	 * the xattr dir.
615789Sahrens 	 */
616789Sahrens 	if (zp->z_phys->zp_xattr) {
617789Sahrens 		error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
618789Sahrens 		ASSERT(error == 0);
619789Sahrens 	}
620789Sahrens 
621789Sahrens 	acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
622789Sahrens 
623789Sahrens 	/*
6246992Smaybee 	 * Set up the final transaction.
625789Sahrens 	 */
626789Sahrens 	tx = dmu_tx_create(os);
627789Sahrens 	dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
6283461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
629789Sahrens 	if (xzp) {
630789Sahrens 		dmu_tx_hold_bonus(tx, xzp->z_id);
6313461Sahrens 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
632789Sahrens 	}
633789Sahrens 	if (acl_obj)
634789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
635789Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
636789Sahrens 	if (error) {
6373461Sahrens 		/*
6383461Sahrens 		 * Not enough space to delete the file.  Leave it in the
6393461Sahrens 		 * unlinked set, leaking it until the fs is remounted (at
6403461Sahrens 		 * which point we'll call zfs_unlinked_drain() to process it).
6413461Sahrens 		 */
642789Sahrens 		dmu_tx_abort(tx);
6435745Smaybee 		zfs_znode_dmu_fini(zp);
6445745Smaybee 		zfs_znode_free(zp);
6455745Smaybee 		goto out;
646789Sahrens 	}
647789Sahrens 
648789Sahrens 	if (xzp) {
649789Sahrens 		dmu_buf_will_dirty(xzp->z_dbuf, tx);
650789Sahrens 		mutex_enter(&xzp->z_lock);
6513461Sahrens 		xzp->z_unlinked = B_TRUE;	/* mark xzp for deletion */
652789Sahrens 		xzp->z_phys->zp_links = 0;	/* no more links to it */
653789Sahrens 		mutex_exit(&xzp->z_lock);
6543461Sahrens 		zfs_unlinked_add(xzp, tx);
655789Sahrens 	}
656789Sahrens 
6573461Sahrens 	/* Remove this znode from the unlinked set */
6587046Sahrens 	VERIFY3U(0, ==,
6597046Sahrens 	    zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
660789Sahrens 
661789Sahrens 	zfs_znode_delete(zp, tx);
662789Sahrens 
663789Sahrens 	dmu_tx_commit(tx);
6645745Smaybee out:
665789Sahrens 	if (xzp)
666789Sahrens 		VN_RELE(ZTOV(xzp));
667789Sahrens }
668789Sahrens 
6694577Sahrens static uint64_t
6704577Sahrens zfs_dirent(znode_t *zp)
6714577Sahrens {
6724577Sahrens 	uint64_t de = zp->z_id;
6734577Sahrens 	if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
6744577Sahrens 		de |= IFTODT((zp)->z_phys->zp_mode) << 60;
6754577Sahrens 	return (de);
6764577Sahrens }
6774577Sahrens 
678789Sahrens /*
6793461Sahrens  * Link zp into dl.  Can only fail if zp has been unlinked.
680789Sahrens  */
681789Sahrens int
682789Sahrens zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
683789Sahrens {
684789Sahrens 	znode_t *dzp = dl->dl_dzp;
685789Sahrens 	vnode_t *vp = ZTOV(zp);
6863912Slling 	uint64_t value;
687789Sahrens 	int zp_is_dir = (vp->v_type == VDIR);
688789Sahrens 	int error;
689789Sahrens 
690789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
691789Sahrens 	mutex_enter(&zp->z_lock);
692789Sahrens 
693789Sahrens 	if (!(flag & ZRENAMING)) {
6943461Sahrens 		if (zp->z_unlinked) {	/* no new links to unlinked zp */
695789Sahrens 			ASSERT(!(flag & (ZNEW | ZEXISTS)));
696789Sahrens 			mutex_exit(&zp->z_lock);
697789Sahrens 			return (ENOENT);
698789Sahrens 		}
699789Sahrens 		zp->z_phys->zp_links++;
700789Sahrens 	}
701789Sahrens 	zp->z_phys->zp_parent = dzp->z_id;	/* dzp is now zp's parent */
702789Sahrens 
703789Sahrens 	if (!(flag & ZNEW))
704789Sahrens 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
705789Sahrens 	mutex_exit(&zp->z_lock);
706789Sahrens 
707789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
708789Sahrens 	mutex_enter(&dzp->z_lock);
709789Sahrens 	dzp->z_phys->zp_size++;			/* one dirent added */
710789Sahrens 	dzp->z_phys->zp_links += zp_is_dir;	/* ".." link from zp */
711789Sahrens 	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
712789Sahrens 	mutex_exit(&dzp->z_lock);
713789Sahrens 
7144577Sahrens 	value = zfs_dirent(zp);
715789Sahrens 	error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
7163912Slling 	    8, 1, &value, tx);
717789Sahrens 	ASSERT(error == 0);
718789Sahrens 
7191484Sek110237 	dnlc_update(ZTOV(dzp), dl->dl_name, vp);
7201484Sek110237 
721789Sahrens 	return (0);
722789Sahrens }
723789Sahrens 
724789Sahrens /*
7253461Sahrens  * Unlink zp from dl, and mark zp for deletion if this was the last link.
726789Sahrens  * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
7273461Sahrens  * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
7283461Sahrens  * If it's non-NULL, we use it to indicate whether the znode needs deletion,
729789Sahrens  * and it's the caller's job to do it.
730789Sahrens  */
731789Sahrens int
732789Sahrens zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
7333461Sahrens 	boolean_t *unlinkedp)
734789Sahrens {
735789Sahrens 	znode_t *dzp = dl->dl_dzp;
736789Sahrens 	vnode_t *vp = ZTOV(zp);
737789Sahrens 	int zp_is_dir = (vp->v_type == VDIR);
7383461Sahrens 	boolean_t unlinked = B_FALSE;
739789Sahrens 	int error;
740789Sahrens 
7411484Sek110237 	dnlc_remove(ZTOV(dzp), dl->dl_name);
7421484Sek110237 
743789Sahrens 	if (!(flag & ZRENAMING)) {
744789Sahrens 		dmu_buf_will_dirty(zp->z_dbuf, tx);
745789Sahrens 
746789Sahrens 		if (vn_vfswlock(vp))		/* prevent new mounts on zp */
747789Sahrens 			return (EBUSY);
748789Sahrens 
749789Sahrens 		if (vn_ismntpt(vp)) {		/* don't remove mount point */
750789Sahrens 			vn_vfsunlock(vp);
751789Sahrens 			return (EBUSY);
752789Sahrens 		}
753789Sahrens 
754789Sahrens 		mutex_enter(&zp->z_lock);
755789Sahrens 		if (zp_is_dir && !zfs_dirempty(zp)) {	/* dir not empty */
756789Sahrens 			mutex_exit(&zp->z_lock);
757789Sahrens 			vn_vfsunlock(vp);
758789Sahrens 			return (EEXIST);
759789Sahrens 		}
7603713Sahrens 		if (zp->z_phys->zp_links <= zp_is_dir) {
7613713Sahrens 			zfs_panic_recover("zfs: link count on %s is %u, "
7623713Sahrens 			    "should be at least %u",
7633713Sahrens 			    zp->z_vnode->v_path ? zp->z_vnode->v_path :
7643713Sahrens 			    "<unknown>", (int)zp->z_phys->zp_links,
7653713Sahrens 			    zp_is_dir + 1);
7663713Sahrens 			zp->z_phys->zp_links = zp_is_dir + 1;
7673713Sahrens 		}
768789Sahrens 		if (--zp->z_phys->zp_links == zp_is_dir) {
7693461Sahrens 			zp->z_unlinked = B_TRUE;
770789Sahrens 			zp->z_phys->zp_links = 0;
7713461Sahrens 			unlinked = B_TRUE;
772789Sahrens 		} else {
773789Sahrens 			zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
774789Sahrens 		}
775789Sahrens 		mutex_exit(&zp->z_lock);
776789Sahrens 		vn_vfsunlock(vp);
777789Sahrens 	}
778789Sahrens 
779789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
780789Sahrens 	mutex_enter(&dzp->z_lock);
781789Sahrens 	dzp->z_phys->zp_size--;			/* one dirent removed */
782789Sahrens 	dzp->z_phys->zp_links -= zp_is_dir;	/* ".." link from zp */
783789Sahrens 	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
784789Sahrens 	mutex_exit(&dzp->z_lock);
785789Sahrens 
7865331Samw 	if (zp->z_zfsvfs->z_norm) {
7875498Stimh 		if (((zp->z_zfsvfs->z_case == ZFS_CASE_INSENSITIVE) &&
7885331Samw 		    (flag & ZCIEXACT)) ||
7895498Stimh 		    ((zp->z_zfsvfs->z_case == ZFS_CASE_MIXED) &&
7905331Samw 		    !(flag & ZCILOOK)))
7915331Samw 			error = zap_remove_norm(zp->z_zfsvfs->z_os,
7925331Samw 			    dzp->z_id, dl->dl_name, MT_EXACT, tx);
7935331Samw 		else
7945331Samw 			error = zap_remove_norm(zp->z_zfsvfs->z_os,
7955331Samw 			    dzp->z_id, dl->dl_name, MT_FIRST, tx);
7965331Samw 	} else {
7975331Samw 		error = zap_remove(zp->z_zfsvfs->z_os,
7985331Samw 		    dzp->z_id, dl->dl_name, tx);
7995331Samw 	}
800789Sahrens 	ASSERT(error == 0);
801789Sahrens 
8023461Sahrens 	if (unlinkedp != NULL)
8033461Sahrens 		*unlinkedp = unlinked;
8043461Sahrens 	else if (unlinked)
8053461Sahrens 		zfs_unlinked_add(zp, tx);
806789Sahrens 
807789Sahrens 	return (0);
808789Sahrens }
809789Sahrens 
810789Sahrens /*
811789Sahrens  * Indicate whether the directory is empty.  Works with or without z_lock
812789Sahrens  * held, but can only be consider a hint in the latter case.  Returns true
813789Sahrens  * if only "." and ".." remain and there's no work in progress.
814789Sahrens  */
815789Sahrens boolean_t
816789Sahrens zfs_dirempty(znode_t *dzp)
817789Sahrens {
818789Sahrens 	return (dzp->z_phys->zp_size == 2 && dzp->z_dirlocks == 0);
819789Sahrens }
820789Sahrens 
821789Sahrens int
822789Sahrens zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
823789Sahrens {
824789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
825789Sahrens 	znode_t *xzp;
826789Sahrens 	dmu_tx_t *tx;
827789Sahrens 	int error;
8289179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t acl_ids;
8299179SMark.Shellenbaum@Sun.COM 	boolean_t fuid_dirtied;
830789Sahrens 
831789Sahrens 	*xvpp = NULL;
832789Sahrens 
8335331Samw 	if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr))
834789Sahrens 		return (error);
835789Sahrens 
8369179SMark.Shellenbaum@Sun.COM 	if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL,
8379179SMark.Shellenbaum@Sun.COM 	    &acl_ids)) != 0)
8389179SMark.Shellenbaum@Sun.COM 		return (error);
8399396SMatthew.Ahrens@Sun.COM 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
8409396SMatthew.Ahrens@Sun.COM 		zfs_acl_ids_free(&acl_ids);
8419396SMatthew.Ahrens@Sun.COM 		return (EDQUOT);
8429396SMatthew.Ahrens@Sun.COM 	}
8439179SMark.Shellenbaum@Sun.COM 
844789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
845789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
8461544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
8479179SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
8489396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
8499396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
8508227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
851789Sahrens 	if (error) {
8529179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
8538227SNeil.Perrin@Sun.COM 		if (error == ERESTART)
8542113Sahrens 			dmu_tx_wait(tx);
855789Sahrens 		dmu_tx_abort(tx);
856789Sahrens 		return (error);
857789Sahrens 	}
8589179SMark.Shellenbaum@Sun.COM 	zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, 0, &acl_ids);
8599179SMark.Shellenbaum@Sun.COM 
8609179SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
8619179SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
8629179SMark.Shellenbaum@Sun.COM 
863789Sahrens 	ASSERT(xzp->z_phys->zp_parent == zp->z_id);
864789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
8655446Sahrens 	zp->z_phys->zp_xattr = xzp->z_id;
866789Sahrens 
8675331Samw 	(void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
8689179SMark.Shellenbaum@Sun.COM 	    xzp, "", NULL, acl_ids.z_fuidp, vap);
8699179SMark.Shellenbaum@Sun.COM 
8709179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_free(&acl_ids);
871789Sahrens 	dmu_tx_commit(tx);
872789Sahrens 
873789Sahrens 	*xvpp = ZTOV(xzp);
874789Sahrens 
875789Sahrens 	return (0);
876789Sahrens }
877789Sahrens 
878789Sahrens /*
879789Sahrens  * Return a znode for the extended attribute directory for zp.
880789Sahrens  * ** If the directory does not already exist, it is created **
881789Sahrens  *
882789Sahrens  *	IN:	zp	- znode to obtain attribute directory from
883789Sahrens  *		cr	- credentials of caller
8843280Sck153898  *		flags	- flags from the VOP_LOOKUP call
885789Sahrens  *
886789Sahrens  *	OUT:	xzpp	- pointer to extended attribute znode
887789Sahrens  *
888789Sahrens  *	RETURN:	0 on success
889789Sahrens  *		error number on failure
890789Sahrens  */
891789Sahrens int
8923280Sck153898 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
893789Sahrens {
894789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
895789Sahrens 	znode_t		*xzp;
896789Sahrens 	zfs_dirlock_t	*dl;
897789Sahrens 	vattr_t		va;
898789Sahrens 	int		error;
899789Sahrens top:
9005331Samw 	error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR, NULL, NULL);
901789Sahrens 	if (error)
902789Sahrens 		return (error);
903789Sahrens 
904789Sahrens 	if (xzp != NULL) {
905789Sahrens 		*xvpp = ZTOV(xzp);
906789Sahrens 		zfs_dirent_unlock(dl);
907789Sahrens 		return (0);
908789Sahrens 	}
909789Sahrens 
910789Sahrens 	ASSERT(zp->z_phys->zp_xattr == 0);
911789Sahrens 
9123280Sck153898 	if (!(flags & CREATE_XATTR_DIR)) {
9133280Sck153898 		zfs_dirent_unlock(dl);
9143280Sck153898 		return (ENOENT);
9153280Sck153898 	}
9163280Sck153898 
917789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
918789Sahrens 		zfs_dirent_unlock(dl);
919789Sahrens 		return (EROFS);
920789Sahrens 	}
921789Sahrens 
922789Sahrens 	/*
923789Sahrens 	 * The ability to 'create' files in an attribute
924789Sahrens 	 * directory comes from the write_xattr permission on the base file.
925789Sahrens 	 *
926789Sahrens 	 * The ability to 'search' an attribute directory requires
927789Sahrens 	 * read_xattr permission on the base file.
928789Sahrens 	 *
929789Sahrens 	 * Once in a directory the ability to read/write attributes
930789Sahrens 	 * is controlled by the permissions on the attribute file.
931789Sahrens 	 */
932789Sahrens 	va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
933789Sahrens 	va.va_type = VDIR;
9341231Smarks 	va.va_mode = S_IFDIR | S_ISVTX | 0777;
9355771Sjp151216 	zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
936789Sahrens 
937789Sahrens 	error = zfs_make_xattrdir(zp, &va, xvpp, cr);
938789Sahrens 	zfs_dirent_unlock(dl);
939789Sahrens 
9408227SNeil.Perrin@Sun.COM 	if (error == ERESTART) {
9412113Sahrens 		/* NB: we already did dmu_tx_wait() if necessary */
942789Sahrens 		goto top;
943789Sahrens 	}
944789Sahrens 
945789Sahrens 	return (error);
946789Sahrens }
947789Sahrens 
948789Sahrens /*
949789Sahrens  * Decide whether it is okay to remove within a sticky directory.
950789Sahrens  *
951789Sahrens  * In sticky directories, write access is not sufficient;
952789Sahrens  * you can remove entries from a directory only if:
953789Sahrens  *
954789Sahrens  *	you own the directory,
955789Sahrens  *	you own the entry,
956789Sahrens  *	the entry is a plain file and you have write access,
957789Sahrens  *	or you are privileged (checked in secpolicy...).
958789Sahrens  *
959789Sahrens  * The function returns 0 if remove access is granted.
960789Sahrens  */
961789Sahrens int
962789Sahrens zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
963789Sahrens {
964789Sahrens 	uid_t  		uid;
9655331Samw 	uid_t		downer;
9665331Samw 	uid_t		fowner;
9675331Samw 	zfsvfs_t	*zfsvfs = zdp->z_zfsvfs;
968789Sahrens 
9698227SNeil.Perrin@Sun.COM 	if (zdp->z_zfsvfs->z_replay)
970789Sahrens 		return (0);
971789Sahrens 
9725331Samw 	if ((zdp->z_phys->zp_mode & S_ISVTX) == 0)
9735331Samw 		return (0);
9745331Samw 
9755959Smarks 	downer = zfs_fuid_map_id(zfsvfs, zdp->z_phys->zp_uid, cr, ZFS_OWNER);
9765959Smarks 	fowner = zfs_fuid_map_id(zfsvfs, zp->z_phys->zp_uid, cr, ZFS_OWNER);
9775331Samw 
9785331Samw 	if ((uid = crgetuid(cr)) == downer || uid == fowner ||
979789Sahrens 	    (ZTOV(zp)->v_type == VREG &&
9805331Samw 	    zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
981789Sahrens 		return (0);
982789Sahrens 	else
983789Sahrens 		return (secpolicy_vnode_remove(cr));
984789Sahrens }
985