xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_dir.c (revision 3665ce8aeee26b1a84fb98951ef011e0779e1ae2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
25  * Copyright 2017 Nexenta Systems, Inc.
26  * Copyright (c) 2015, Joyent, Inc.
27  */
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/time.h>
32 #include <sys/systm.h>
33 #include <sys/sysmacros.h>
34 #include <sys/resource.h>
35 #include <sys/vfs.h>
36 #include <sys/vnode.h>
37 #include <sys/file.h>
38 #include <sys/mode.h>
39 #include <sys/kmem.h>
40 #include <sys/uio.h>
41 #include <sys/pathname.h>
42 #include <sys/cmn_err.h>
43 #include <sys/errno.h>
44 #include <sys/stat.h>
45 #include <sys/unistd.h>
46 #include <sys/sunddi.h>
47 #include <sys/random.h>
48 #include <sys/policy.h>
49 #include <sys/zfs_dir.h>
50 #include <sys/zfs_acl.h>
51 #include <sys/fs/zfs.h>
52 #include "fs/fs_subr.h"
53 #include <sys/zap.h>
54 #include <sys/dmu.h>
55 #include <sys/atomic.h>
56 #include <sys/zfs_ctldir.h>
57 #include <sys/zfs_fuid.h>
58 #include <sys/sa.h>
59 #include <sys/zfs_sa.h>
60 #include <sys/dnlc.h>
61 #include <sys/extdirent.h>
62 
63 /*
64  * zfs_match_find() is used by zfs_dirent_lock() to peform zap lookups
65  * of names after deciding which is the appropriate lookup interface.
66  */
67 static int
68 zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, char *name, matchtype_t mt,
69     boolean_t update, int *deflags, pathname_t *rpnp, uint64_t *zoid)
70 {
71 	int error;
72 
73 	if (zfsvfs->z_norm) {
74 		boolean_t conflict = B_FALSE;
75 		size_t bufsz = 0;
76 		char *buf = NULL;
77 
78 		if (rpnp) {
79 			buf = rpnp->pn_buf;
80 			bufsz = rpnp->pn_bufsize;
81 		}
82 
83 		/*
84 		 * In the non-mixed case we only expect there would ever
85 		 * be one match, but we need to use the normalizing lookup.
86 		 */
87 		error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1,
88 		    zoid, mt, buf, bufsz, &conflict);
89 		if (!error && deflags)
90 			*deflags = conflict ? ED_CASE_CONFLICT : 0;
91 	} else {
92 		error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid);
93 	}
94 	*zoid = ZFS_DIRENT_OBJ(*zoid);
95 
96 	if (error == ENOENT && update)
97 		dnlc_update(ZTOV(dzp), name, DNLC_NO_VNODE);
98 
99 	return (error);
100 }
101 
102 /*
103  * Lock a directory entry.  A dirlock on <dzp, name> protects that name
104  * in dzp's directory zap object.  As long as you hold a dirlock, you can
105  * assume two things: (1) dzp cannot be reaped, and (2) no other thread
106  * can change the zap entry for (i.e. link or unlink) this name.
107  *
108  * Input arguments:
109  *	dzp	- znode for directory
110  *	name	- name of entry to lock
111  *	flag	- ZNEW: if the entry already exists, fail with EEXIST.
112  *		  ZEXISTS: if the entry does not exist, fail with ENOENT.
113  *		  ZSHARED: allow concurrent access with other ZSHARED callers.
114  *		  ZXATTR: we want dzp's xattr directory
115  *		  ZCILOOK: On a mixed sensitivity file system,
116  *			   this lookup should be case-insensitive.
117  *		  ZCIEXACT: On a purely case-insensitive file system,
118  *			    this lookup should be case-sensitive.
119  *		  ZRENAMING: we are locking for renaming, force narrow locks
120  *		  ZHAVELOCK: Don't grab the z_name_lock for this call. The
121  *			     current thread already holds it.
122  *
123  * Output arguments:
124  *	zpp	- pointer to the znode for the entry (NULL if there isn't one)
125  *	dlpp	- pointer to the dirlock for this entry (NULL on error)
126  *      direntflags - (case-insensitive lookup only)
127  *		flags if multiple case-sensitive matches exist in directory
128  *      realpnp     - (case-insensitive lookup only)
129  *		actual name matched within the directory
130  *
131  * Return value: 0 on success or errno on failure.
132  *
133  * NOTE: Always checks for, and rejects, '.' and '..'.
134  * NOTE: For case-insensitive file systems we take wide locks (see below),
135  *	 but return znode pointers to a single match.
136  */
137 int
138 zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
139     int flag, int *direntflags, pathname_t *realpnp)
140 {
141 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
142 	zfs_dirlock_t	*dl;
143 	boolean_t	update;
144 	matchtype_t	mt = 0;
145 	uint64_t	zoid;
146 	vnode_t		*vp = NULL;
147 	int		error = 0;
148 	int		cmpflags;
149 
150 	*zpp = NULL;
151 	*dlpp = NULL;
152 
153 	/*
154 	 * Verify that we are not trying to lock '.', '..', or '.zfs'
155 	 */
156 	if (name[0] == '.' &&
157 	    (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) ||
158 	    zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0)
159 		return (SET_ERROR(EEXIST));
160 
161 	/*
162 	 * Case sensitivity and normalization preferences are set when
163 	 * the file system is created.  These are stored in the
164 	 * zfsvfs->z_case and zfsvfs->z_norm fields.  These choices
165 	 * affect what vnodes can be cached in the DNLC, how we
166 	 * perform zap lookups, and the "width" of our dirlocks.
167 	 *
168 	 * A normal dirlock locks a single name.  Note that with
169 	 * normalization a name can be composed multiple ways, but
170 	 * when normalized, these names all compare equal.  A wide
171 	 * dirlock locks multiple names.  We need these when the file
172 	 * system is supporting mixed-mode access.  It is sometimes
173 	 * necessary to lock all case permutations of file name at
174 	 * once so that simultaneous case-insensitive/case-sensitive
175 	 * behaves as rationally as possible.
176 	 */
177 
178 	/*
179 	 * When matching we may need to normalize & change case according to
180 	 * FS settings.
181 	 *
182 	 * Note that a normalized match is necessary for a case insensitive
183 	 * filesystem when the lookup request is not exact because normalization
184 	 * can fold case independent of normalizing code point sequences.
185 	 *
186 	 * See the table above zfs_dropname().
187 	 */
188 	if (zfsvfs->z_norm != 0) {
189 		mt = MT_NORMALIZE;
190 
191 		/*
192 		 * Determine if the match needs to honor the case specified in
193 		 * lookup, and if so keep track of that so that during
194 		 * normalization we don't fold case.
195 		 */
196 		if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE &&
197 		    (flag & ZCIEXACT)) ||
198 		    (zfsvfs->z_case == ZFS_CASE_MIXED && !(flag & ZCILOOK))) {
199 			mt |= MT_MATCH_CASE;
200 		}
201 	}
202 
203 	/*
204 	 * Only look in or update the DNLC if we are looking for the
205 	 * name on a file system that does not require normalization
206 	 * or case folding.  We can also look there if we happen to be
207 	 * on a non-normalizing, mixed sensitivity file system IF we
208 	 * are looking for the exact name.
209 	 *
210 	 * Maybe can add TO-UPPERed version of name to dnlc in ci-only
211 	 * case for performance improvement?
212 	 */
213 	update = !zfsvfs->z_norm ||
214 	    (zfsvfs->z_case == ZFS_CASE_MIXED &&
215 	    !(zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER) && !(flag & ZCILOOK));
216 
217 	/*
218 	 * ZRENAMING indicates we are in a situation where we should
219 	 * take narrow locks regardless of the file system's
220 	 * preferences for normalizing and case folding.  This will
221 	 * prevent us deadlocking trying to grab the same wide lock
222 	 * twice if the two names happen to be case-insensitive
223 	 * matches.
224 	 */
225 	if (flag & ZRENAMING)
226 		cmpflags = 0;
227 	else
228 		cmpflags = zfsvfs->z_norm;
229 
230 	/*
231 	 * Wait until there are no locks on this name.
232 	 *
233 	 * Don't grab the the lock if it is already held. However, cannot
234 	 * have both ZSHARED and ZHAVELOCK together.
235 	 */
236 	ASSERT(!(flag & ZSHARED) || !(flag & ZHAVELOCK));
237 	if (!(flag & ZHAVELOCK))
238 		rw_enter(&dzp->z_name_lock, RW_READER);
239 
240 	mutex_enter(&dzp->z_lock);
241 	for (;;) {
242 		if (dzp->z_unlinked) {
243 			mutex_exit(&dzp->z_lock);
244 			if (!(flag & ZHAVELOCK))
245 				rw_exit(&dzp->z_name_lock);
246 			return (SET_ERROR(ENOENT));
247 		}
248 		for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next) {
249 			if ((u8_strcmp(name, dl->dl_name, 0, cmpflags,
250 			    U8_UNICODE_LATEST, &error) == 0) || error != 0)
251 				break;
252 		}
253 		if (error != 0) {
254 			mutex_exit(&dzp->z_lock);
255 			if (!(flag & ZHAVELOCK))
256 				rw_exit(&dzp->z_name_lock);
257 			return (SET_ERROR(ENOENT));
258 		}
259 		if (dl == NULL)	{
260 			/*
261 			 * Allocate a new dirlock and add it to the list.
262 			 */
263 			dl = kmem_alloc(sizeof (zfs_dirlock_t), KM_SLEEP);
264 			cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL);
265 			dl->dl_name = name;
266 			dl->dl_sharecnt = 0;
267 			dl->dl_namelock = 0;
268 			dl->dl_namesize = 0;
269 			dl->dl_dzp = dzp;
270 			dl->dl_next = dzp->z_dirlocks;
271 			dzp->z_dirlocks = dl;
272 			break;
273 		}
274 		if ((flag & ZSHARED) && dl->dl_sharecnt != 0)
275 			break;
276 		cv_wait(&dl->dl_cv, &dzp->z_lock);
277 	}
278 
279 	/*
280 	 * If the z_name_lock was NOT held for this dirlock record it.
281 	 */
282 	if (flag & ZHAVELOCK)
283 		dl->dl_namelock = 1;
284 
285 	if ((flag & ZSHARED) && ++dl->dl_sharecnt > 1 && dl->dl_namesize == 0) {
286 		/*
287 		 * We're the second shared reference to dl.  Make a copy of
288 		 * dl_name in case the first thread goes away before we do.
289 		 * Note that we initialize the new name before storing its
290 		 * pointer into dl_name, because the first thread may load
291 		 * dl->dl_name at any time.  It'll either see the old value,
292 		 * which belongs to it, or the new shared copy; either is OK.
293 		 */
294 		dl->dl_namesize = strlen(dl->dl_name) + 1;
295 		name = kmem_alloc(dl->dl_namesize, KM_SLEEP);
296 		bcopy(dl->dl_name, name, dl->dl_namesize);
297 		dl->dl_name = name;
298 	}
299 
300 	mutex_exit(&dzp->z_lock);
301 
302 	/*
303 	 * We have a dirlock on the name.  (Note that it is the dirlock,
304 	 * not the dzp's z_lock, that protects the name in the zap object.)
305 	 * See if there's an object by this name; if so, put a hold on it.
306 	 */
307 	if (flag & ZXATTR) {
308 		error = sa_lookup(dzp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &zoid,
309 		    sizeof (zoid));
310 		if (error == 0)
311 			error = (zoid == 0 ? ENOENT : 0);
312 	} else {
313 		if (update)
314 			vp = dnlc_lookup(ZTOV(dzp), name);
315 		if (vp == DNLC_NO_VNODE) {
316 			VN_RELE(vp);
317 			error = SET_ERROR(ENOENT);
318 		} else if (vp) {
319 			if (flag & ZNEW) {
320 				zfs_dirent_unlock(dl);
321 				VN_RELE(vp);
322 				return (SET_ERROR(EEXIST));
323 			}
324 			*dlpp = dl;
325 			*zpp = VTOZ(vp);
326 			return (0);
327 		} else {
328 			error = zfs_match_find(zfsvfs, dzp, name, mt,
329 			    update, direntflags, realpnp, &zoid);
330 		}
331 	}
332 	if (error) {
333 		if (error != ENOENT || (flag & ZEXISTS)) {
334 			zfs_dirent_unlock(dl);
335 			return (error);
336 		}
337 	} else {
338 		if (flag & ZNEW) {
339 			zfs_dirent_unlock(dl);
340 			return (SET_ERROR(EEXIST));
341 		}
342 		error = zfs_zget(zfsvfs, zoid, zpp);
343 		if (error) {
344 			zfs_dirent_unlock(dl);
345 			return (error);
346 		}
347 		if (!(flag & ZXATTR) && update)
348 			dnlc_update(ZTOV(dzp), name, ZTOV(*zpp));
349 	}
350 
351 	*dlpp = dl;
352 
353 	return (0);
354 }
355 
356 /*
357  * Unlock this directory entry and wake anyone who was waiting for it.
358  */
359 void
360 zfs_dirent_unlock(zfs_dirlock_t *dl)
361 {
362 	znode_t *dzp = dl->dl_dzp;
363 	zfs_dirlock_t **prev_dl, *cur_dl;
364 
365 	mutex_enter(&dzp->z_lock);
366 
367 	if (!dl->dl_namelock)
368 		rw_exit(&dzp->z_name_lock);
369 
370 	if (dl->dl_sharecnt > 1) {
371 		dl->dl_sharecnt--;
372 		mutex_exit(&dzp->z_lock);
373 		return;
374 	}
375 	prev_dl = &dzp->z_dirlocks;
376 	while ((cur_dl = *prev_dl) != dl)
377 		prev_dl = &cur_dl->dl_next;
378 	*prev_dl = dl->dl_next;
379 	cv_broadcast(&dl->dl_cv);
380 	mutex_exit(&dzp->z_lock);
381 
382 	if (dl->dl_namesize != 0)
383 		kmem_free(dl->dl_name, dl->dl_namesize);
384 	cv_destroy(&dl->dl_cv);
385 	kmem_free(dl, sizeof (*dl));
386 }
387 
388 /*
389  * Look up an entry in a directory.
390  *
391  * NOTE: '.' and '..' are handled as special cases because
392  *	no directory entries are actually stored for them.  If this is
393  *	the root of a filesystem, then '.zfs' is also treated as a
394  *	special pseudo-directory.
395  */
396 int
397 zfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp, int flags,
398     int *deflg, pathname_t *rpnp)
399 {
400 	zfs_dirlock_t *dl;
401 	znode_t *zp;
402 	int error = 0;
403 	uint64_t parent;
404 
405 	if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
406 		*vpp = ZTOV(dzp);
407 		VN_HOLD(*vpp);
408 	} else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
409 		zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
410 
411 		/*
412 		 * If we are a snapshot mounted under .zfs, return
413 		 * the vp for the snapshot directory.
414 		 */
415 		if ((error = sa_lookup(dzp->z_sa_hdl,
416 		    SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
417 			return (error);
418 		if (parent == dzp->z_id && zfsvfs->z_parent != zfsvfs) {
419 			error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir,
420 			    "snapshot", vpp, NULL, 0, NULL, kcred,
421 			    NULL, NULL, NULL);
422 			return (error);
423 		}
424 		rw_enter(&dzp->z_parent_lock, RW_READER);
425 		error = zfs_zget(zfsvfs, parent, &zp);
426 		if (error == 0)
427 			*vpp = ZTOV(zp);
428 		rw_exit(&dzp->z_parent_lock);
429 	} else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) {
430 		*vpp = zfsctl_root(dzp);
431 	} else {
432 		int zf;
433 
434 		zf = ZEXISTS | ZSHARED;
435 		if (flags & FIGNORECASE)
436 			zf |= ZCILOOK;
437 
438 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zf, deflg, rpnp);
439 		if (error == 0) {
440 			*vpp = ZTOV(zp);
441 			zfs_dirent_unlock(dl);
442 			dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
443 		}
444 		rpnp = NULL;
445 	}
446 
447 	if ((flags & FIGNORECASE) && rpnp && !error)
448 		(void) strlcpy(rpnp->pn_buf, name, rpnp->pn_bufsize);
449 
450 	return (error);
451 }
452 
453 /*
454  * unlinked Set (formerly known as the "delete queue") Error Handling
455  *
456  * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
457  * don't specify the name of the entry that we will be manipulating.  We
458  * also fib and say that we won't be adding any new entries to the
459  * unlinked set, even though we might (this is to lower the minimum file
460  * size that can be deleted in a full filesystem).  So on the small
461  * chance that the nlink list is using a fat zap (ie. has more than
462  * 2000 entries), we *may* not pre-read a block that's needed.
463  * Therefore it is remotely possible for some of the assertions
464  * regarding the unlinked set below to fail due to i/o error.  On a
465  * nondebug system, this will result in the space being leaked.
466  */
467 void
468 zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
469 {
470 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
471 
472 	ASSERT(zp->z_unlinked);
473 	ASSERT(zp->z_links == 0);
474 
475 	VERIFY3U(0, ==,
476 	    zap_add_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
477 }
478 
479 /*
480  * Clean up any znodes that had no links when we either crashed or
481  * (force) umounted the file system.
482  */
483 static void
484 zfs_unlinked_drain_task(void *arg)
485 {
486 	zfsvfs_t *zfsvfs = arg;
487 	zap_cursor_t	zc;
488 	zap_attribute_t zap;
489 	dmu_object_info_t doi;
490 	znode_t		*zp;
491 	int		error;
492 
493 	ASSERT3B(zfsvfs->z_draining, ==, B_TRUE);
494 
495 	/*
496 	 * Interate over the contents of the unlinked set.
497 	 */
498 	for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
499 	    zap_cursor_retrieve(&zc, &zap) == 0 && !zfsvfs->z_drain_cancel;
500 	    zap_cursor_advance(&zc)) {
501 
502 		/*
503 		 * See what kind of object we have in list
504 		 */
505 
506 		error = dmu_object_info(zfsvfs->z_os,
507 		    zap.za_first_integer, &doi);
508 		if (error != 0)
509 			continue;
510 
511 		ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
512 		    (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
513 		/*
514 		 * We need to re-mark these list entries for deletion,
515 		 * so we pull them back into core and set zp->z_unlinked.
516 		 */
517 		error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
518 
519 		/*
520 		 * We may pick up znodes that are already marked for deletion.
521 		 * This could happen during the purge of an extended attribute
522 		 * directory.  All we need to do is skip over them, since they
523 		 * are already in the system marked z_unlinked.
524 		 */
525 		if (error != 0)
526 			continue;
527 
528 		zp->z_unlinked = B_TRUE;
529 
530 		VN_RELE(ZTOV(zp));
531 		ASSERT3B(zfsvfs->z_unmounted, ==, B_FALSE);
532 	}
533 	zap_cursor_fini(&zc);
534 
535 	zfsvfs->z_draining = B_FALSE;
536 	zfsvfs->z_drain_task = TASKQID_INVALID;
537 }
538 
539 /*
540  * Sets z_draining then tries to dispatch async unlinked drain.
541  * If that fails executes synchronous unlinked drain.
542  */
543 void
544 zfs_unlinked_drain(zfsvfs_t *zfsvfs)
545 {
546 	ASSERT3B(zfsvfs->z_unmounted, ==, B_FALSE);
547 	ASSERT3B(zfsvfs->z_draining, ==, B_FALSE);
548 
549 	zfsvfs->z_draining = B_TRUE;
550 	zfsvfs->z_drain_cancel = B_FALSE;
551 
552 	zfsvfs->z_drain_task = taskq_dispatch(
553 	    dsl_pool_unlinked_drain_taskq(dmu_objset_pool(zfsvfs->z_os)),
554 	    zfs_unlinked_drain_task, zfsvfs, TQ_SLEEP);
555 	if (zfsvfs->z_drain_task == TASKQID_INVALID) {
556 		zfs_dbgmsg("async zfs_unlinked_drain dispatch failed");
557 		zfs_unlinked_drain_task(zfsvfs);
558 	}
559 }
560 
561 /*
562  * Wait for the unlinked drain taskq task to stop. This will interrupt the
563  * unlinked set processing if it is in progress.
564  */
565 void
566 zfs_unlinked_drain_stop_wait(zfsvfs_t *zfsvfs)
567 {
568 	ASSERT3B(zfsvfs->z_unmounted, ==, B_FALSE);
569 
570 	while (zfsvfs->z_draining) {
571 		zfsvfs->z_drain_cancel = B_TRUE;
572 		taskq_wait(dsl_pool_unlinked_drain_taskq(
573 		    dmu_objset_pool(zfsvfs->z_os)));
574 	}
575 }
576 
577 /*
578  * Delete the entire contents of a directory.  Return a count
579  * of the number of entries that could not be deleted. If we encounter
580  * an error, return a count of at least one so that the directory stays
581  * in the unlinked set.
582  *
583  * NOTE: this function assumes that the directory is inactive,
584  *	so there is no need to lock its entries before deletion.
585  *	Also, it assumes the directory contents is *only* regular
586  *	files.
587  */
588 static int
589 zfs_purgedir(znode_t *dzp)
590 {
591 	zap_cursor_t	zc;
592 	zap_attribute_t	zap;
593 	znode_t		*xzp;
594 	dmu_tx_t	*tx;
595 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
596 	zfs_dirlock_t	dl;
597 	int skipped = 0;
598 	int error;
599 
600 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
601 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
602 	    zap_cursor_advance(&zc)) {
603 		error = zfs_zget(zfsvfs,
604 		    ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
605 		if (error) {
606 			skipped += 1;
607 			continue;
608 		}
609 
610 		ASSERT((ZTOV(xzp)->v_type == VREG) ||
611 		    (ZTOV(xzp)->v_type == VLNK));
612 
613 		tx = dmu_tx_create(zfsvfs->z_os);
614 		dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
615 		dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
616 		dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
617 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
618 		/* Is this really needed ? */
619 		zfs_sa_upgrade_txholds(tx, xzp);
620 		dmu_tx_mark_netfree(tx);
621 		error = dmu_tx_assign(tx, TXG_WAIT);
622 		if (error) {
623 			dmu_tx_abort(tx);
624 			VN_RELE(ZTOV(xzp));
625 			skipped += 1;
626 			continue;
627 		}
628 		bzero(&dl, sizeof (dl));
629 		dl.dl_dzp = dzp;
630 		dl.dl_name = zap.za_name;
631 
632 		error = zfs_link_destroy(&dl, xzp, tx, 0, NULL);
633 		if (error)
634 			skipped += 1;
635 		dmu_tx_commit(tx);
636 
637 		VN_RELE(ZTOV(xzp));
638 	}
639 	zap_cursor_fini(&zc);
640 	if (error != ENOENT)
641 		skipped += 1;
642 	return (skipped);
643 }
644 
645 void
646 zfs_rmnode(znode_t *zp)
647 {
648 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
649 	objset_t	*os = zfsvfs->z_os;
650 	znode_t		*xzp = NULL;
651 	dmu_tx_t	*tx;
652 	uint64_t	acl_obj;
653 	uint64_t	xattr_obj;
654 	int		error;
655 
656 	ASSERT(zp->z_links == 0);
657 	ASSERT(ZTOV(zp)->v_count == 0);
658 
659 	/*
660 	 * If this is an attribute directory, purge its contents.
661 	 */
662 	if (ZTOV(zp)->v_type == VDIR && (zp->z_pflags & ZFS_XATTR)) {
663 		if (zfs_purgedir(zp) != 0) {
664 			/*
665 			 * Not enough space to delete some xattrs.
666 			 * Leave it in the unlinked set.
667 			 */
668 			zfs_znode_dmu_fini(zp);
669 			zfs_znode_free(zp);
670 			return;
671 		}
672 	} else {
673 		/*
674 		 * Free up all the data in the file.  We don't do this for
675 		 * XATTR directories because we need truncate and remove to be
676 		 * in the same tx, like in zfs_znode_delete(). Otherwise, if
677 		 * we crash here we'll end up with an inconsistent truncated
678 		 * zap object in the delete queue.  Note a truncated file is
679 		 * harmless since it only contains user data.
680 		 */
681 		error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
682 		if (error) {
683 			/*
684 			 * Not enough space or we were interrupted by unmount.
685 			 * Leave the file in the unlinked set.
686 			 */
687 			zfs_znode_dmu_fini(zp);
688 			zfs_znode_free(zp);
689 			return;
690 		}
691 	}
692 
693 	/*
694 	 * If the file has extended attributes, we're going to unlink
695 	 * the xattr dir.
696 	 */
697 	error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
698 	    &xattr_obj, sizeof (xattr_obj));
699 	if (error == 0 && xattr_obj) {
700 		error = zfs_zget(zfsvfs, xattr_obj, &xzp);
701 		ASSERT(error == 0);
702 	}
703 
704 	acl_obj = zfs_external_acl(zp);
705 
706 	/*
707 	 * Set up the final transaction.
708 	 */
709 	tx = dmu_tx_create(os);
710 	dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
711 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
712 	if (xzp) {
713 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
714 		dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
715 	}
716 	if (acl_obj)
717 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
718 
719 	zfs_sa_upgrade_txholds(tx, zp);
720 	error = dmu_tx_assign(tx, TXG_WAIT);
721 	if (error) {
722 		/*
723 		 * Not enough space to delete the file.  Leave it in the
724 		 * unlinked set, leaking it until the fs is remounted (at
725 		 * which point we'll call zfs_unlinked_drain() to process it).
726 		 */
727 		dmu_tx_abort(tx);
728 		zfs_znode_dmu_fini(zp);
729 		zfs_znode_free(zp);
730 		goto out;
731 	}
732 
733 	if (xzp) {
734 		ASSERT(error == 0);
735 		mutex_enter(&xzp->z_lock);
736 		xzp->z_unlinked = B_TRUE;	/* mark xzp for deletion */
737 		xzp->z_links = 0;	/* no more links to it */
738 		VERIFY(0 == sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
739 		    &xzp->z_links, sizeof (xzp->z_links), tx));
740 		mutex_exit(&xzp->z_lock);
741 		zfs_unlinked_add(xzp, tx);
742 	}
743 
744 	/* Remove this znode from the unlinked set */
745 	VERIFY3U(0, ==,
746 	    zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
747 
748 	zfs_znode_delete(zp, tx);
749 
750 	dmu_tx_commit(tx);
751 out:
752 	if (xzp)
753 		VN_RELE(ZTOV(xzp));
754 }
755 
756 static uint64_t
757 zfs_dirent(znode_t *zp, uint64_t mode)
758 {
759 	uint64_t de = zp->z_id;
760 
761 	if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
762 		de |= IFTODT(mode) << 60;
763 	return (de);
764 }
765 
766 /*
767  * Link zp into dl.  Can only fail if zp has been unlinked.
768  */
769 int
770 zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
771 {
772 	znode_t *dzp = dl->dl_dzp;
773 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
774 	vnode_t *vp = ZTOV(zp);
775 	uint64_t value;
776 	int zp_is_dir = (vp->v_type == VDIR);
777 	sa_bulk_attr_t bulk[5];
778 	uint64_t mtime[2], ctime[2];
779 	int count = 0;
780 	int error;
781 
782 	mutex_enter(&zp->z_lock);
783 
784 	if (!(flag & ZRENAMING)) {
785 		if (zp->z_unlinked) {	/* no new links to unlinked zp */
786 			ASSERT(!(flag & (ZNEW | ZEXISTS)));
787 			mutex_exit(&zp->z_lock);
788 			return (SET_ERROR(ENOENT));
789 		}
790 		zp->z_links++;
791 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
792 		    &zp->z_links, sizeof (zp->z_links));
793 
794 	}
795 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
796 	    &dzp->z_id, sizeof (dzp->z_id));
797 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
798 	    &zp->z_pflags, sizeof (zp->z_pflags));
799 
800 	if (!(flag & ZNEW)) {
801 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
802 		    ctime, sizeof (ctime));
803 		zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
804 		    ctime, B_TRUE);
805 	}
806 	error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
807 	ASSERT(error == 0);
808 
809 	mutex_exit(&zp->z_lock);
810 
811 	mutex_enter(&dzp->z_lock);
812 	dzp->z_size++;
813 	dzp->z_links += zp_is_dir;
814 	count = 0;
815 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
816 	    &dzp->z_size, sizeof (dzp->z_size));
817 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
818 	    &dzp->z_links, sizeof (dzp->z_links));
819 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
820 	    mtime, sizeof (mtime));
821 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
822 	    ctime, sizeof (ctime));
823 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
824 	    &dzp->z_pflags, sizeof (dzp->z_pflags));
825 	zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
826 	error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
827 	ASSERT(error == 0);
828 	mutex_exit(&dzp->z_lock);
829 
830 	value = zfs_dirent(zp, zp->z_mode);
831 	error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
832 	    8, 1, &value, tx);
833 	ASSERT(error == 0);
834 
835 	dnlc_update(ZTOV(dzp), dl->dl_name, vp);
836 
837 	return (0);
838 }
839 
840 /*
841  * The match type in the code for this function should conform to:
842  *
843  * ------------------------------------------------------------------------
844  * fs type  | z_norm      | lookup type | match type
845  * ---------|-------------|-------------|----------------------------------
846  * CS !norm | 0           |           0 | 0 (exact)
847  * CS  norm | formX       |           0 | MT_NORMALIZE
848  * CI !norm | upper       |   !ZCIEXACT | MT_NORMALIZE
849  * CI !norm | upper       |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
850  * CI  norm | upper|formX |   !ZCIEXACT | MT_NORMALIZE
851  * CI  norm | upper|formX |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
852  * CM !norm | upper       |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
853  * CM !norm | upper       |     ZCILOOK | MT_NORMALIZE
854  * CM  norm | upper|formX |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
855  * CM  norm | upper|formX |     ZCILOOK | MT_NORMALIZE
856  *
857  * Abbreviations:
858  *    CS = Case Sensitive, CI = Case Insensitive, CM = Case Mixed
859  *    upper = case folding set by fs type on creation (U8_TEXTPREP_TOUPPER)
860  *    formX = unicode normalization form set on fs creation
861  */
862 static int
863 zfs_dropname(zfs_dirlock_t *dl, znode_t *zp, znode_t *dzp, dmu_tx_t *tx,
864     int flag)
865 {
866 	int error;
867 
868 	if (zp->z_zfsvfs->z_norm) {
869 		matchtype_t mt = MT_NORMALIZE;
870 
871 		if ((zp->z_zfsvfs->z_case == ZFS_CASE_INSENSITIVE &&
872 		    (flag & ZCIEXACT)) ||
873 		    (zp->z_zfsvfs->z_case == ZFS_CASE_MIXED &&
874 		    !(flag & ZCILOOK))) {
875 			mt |= MT_MATCH_CASE;
876 		}
877 
878 		error = zap_remove_norm(zp->z_zfsvfs->z_os, dzp->z_id,
879 		    dl->dl_name, mt, tx);
880 	} else {
881 		error = zap_remove(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
882 		    tx);
883 	}
884 
885 	return (error);
886 }
887 
888 /*
889  * Unlink zp from dl, and mark zp for deletion if this was the last link.
890  * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
891  * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
892  * If it's non-NULL, we use it to indicate whether the znode needs deletion,
893  * and it's the caller's job to do it.
894  */
895 int
896 zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
897     boolean_t *unlinkedp)
898 {
899 	znode_t *dzp = dl->dl_dzp;
900 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
901 	vnode_t *vp = ZTOV(zp);
902 	int zp_is_dir = (vp->v_type == VDIR);
903 	boolean_t unlinked = B_FALSE;
904 	sa_bulk_attr_t bulk[5];
905 	uint64_t mtime[2], ctime[2];
906 	int count = 0;
907 	int error;
908 
909 	dnlc_remove(ZTOV(dzp), dl->dl_name);
910 
911 	if (!(flag & ZRENAMING)) {
912 		if (vn_vfswlock(vp))		/* prevent new mounts on zp */
913 			return (SET_ERROR(EBUSY));
914 
915 		if (vn_ismntpt(vp)) {		/* don't remove mount point */
916 			vn_vfsunlock(vp);
917 			return (SET_ERROR(EBUSY));
918 		}
919 
920 		mutex_enter(&zp->z_lock);
921 
922 		if (zp_is_dir && !zfs_dirempty(zp)) {
923 			mutex_exit(&zp->z_lock);
924 			vn_vfsunlock(vp);
925 			return (SET_ERROR(EEXIST));
926 		}
927 
928 		/*
929 		 * If we get here, we are going to try to remove the object.
930 		 * First try removing the name from the directory; if that
931 		 * fails, return the error.
932 		 */
933 		error = zfs_dropname(dl, zp, dzp, tx, flag);
934 		if (error != 0) {
935 			mutex_exit(&zp->z_lock);
936 			vn_vfsunlock(vp);
937 			return (error);
938 		}
939 
940 		if (zp->z_links <= zp_is_dir) {
941 			zfs_panic_recover("zfs: link count on %s is %u, "
942 			    "should be at least %u",
943 			    zp->z_vnode->v_path != vn_vpath_empty ?
944 			    zp->z_vnode->v_path : "<unknown>",
945 			    (int)zp->z_links, zp_is_dir + 1);
946 			zp->z_links = zp_is_dir + 1;
947 		}
948 		if (--zp->z_links == zp_is_dir) {
949 			zp->z_unlinked = B_TRUE;
950 			zp->z_links = 0;
951 			unlinked = B_TRUE;
952 		} else {
953 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
954 			    NULL, &ctime, sizeof (ctime));
955 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
956 			    NULL, &zp->z_pflags, sizeof (zp->z_pflags));
957 			zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
958 			    B_TRUE);
959 		}
960 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
961 		    NULL, &zp->z_links, sizeof (zp->z_links));
962 		error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
963 		count = 0;
964 		ASSERT(error == 0);
965 		mutex_exit(&zp->z_lock);
966 		vn_vfsunlock(vp);
967 	} else {
968 		error = zfs_dropname(dl, zp, dzp, tx, flag);
969 		if (error != 0)
970 			return (error);
971 	}
972 
973 	mutex_enter(&dzp->z_lock);
974 	dzp->z_size--;		/* one dirent removed */
975 	dzp->z_links -= zp_is_dir;	/* ".." link from zp */
976 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
977 	    NULL, &dzp->z_links, sizeof (dzp->z_links));
978 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
979 	    NULL, &dzp->z_size, sizeof (dzp->z_size));
980 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
981 	    NULL, ctime, sizeof (ctime));
982 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
983 	    NULL, mtime, sizeof (mtime));
984 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
985 	    NULL, &dzp->z_pflags, sizeof (dzp->z_pflags));
986 	zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
987 	error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
988 	ASSERT(error == 0);
989 	mutex_exit(&dzp->z_lock);
990 
991 	if (unlinkedp != NULL)
992 		*unlinkedp = unlinked;
993 	else if (unlinked)
994 		zfs_unlinked_add(zp, tx);
995 
996 	return (0);
997 }
998 
999 /*
1000  * Indicate whether the directory is empty.  Works with or without z_lock
1001  * held, but can only be consider a hint in the latter case.  Returns true
1002  * if only "." and ".." remain and there's no work in progress.
1003  */
1004 boolean_t
1005 zfs_dirempty(znode_t *dzp)
1006 {
1007 	return (dzp->z_size == 2 && dzp->z_dirlocks == 0);
1008 }
1009 
1010 int
1011 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
1012 {
1013 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1014 	znode_t *xzp;
1015 	dmu_tx_t *tx;
1016 	int error;
1017 	zfs_acl_ids_t acl_ids;
1018 	boolean_t fuid_dirtied;
1019 	uint64_t parent;
1020 
1021 	*xvpp = NULL;
1022 
1023 	if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr))
1024 		return (error);
1025 
1026 	if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL,
1027 	    &acl_ids)) != 0)
1028 		return (error);
1029 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, zp->z_projid)) {
1030 		zfs_acl_ids_free(&acl_ids);
1031 		return (SET_ERROR(EDQUOT));
1032 	}
1033 
1034 	tx = dmu_tx_create(zfsvfs->z_os);
1035 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1036 	    ZFS_SA_BASE_ATTR_SIZE);
1037 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1038 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1039 	fuid_dirtied = zfsvfs->z_fuid_dirty;
1040 	if (fuid_dirtied)
1041 		zfs_fuid_txhold(zfsvfs, tx);
1042 	error = dmu_tx_assign(tx, TXG_WAIT);
1043 	if (error) {
1044 		zfs_acl_ids_free(&acl_ids);
1045 		dmu_tx_abort(tx);
1046 		return (error);
1047 	}
1048 	zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, &acl_ids);
1049 
1050 	if (fuid_dirtied)
1051 		zfs_fuid_sync(zfsvfs, tx);
1052 
1053 #ifdef DEBUG
1054 	error = sa_lookup(xzp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
1055 	    &parent, sizeof (parent));
1056 	ASSERT(error == 0 && parent == zp->z_id);
1057 #endif
1058 
1059 	VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xzp->z_id,
1060 	    sizeof (xzp->z_id), tx));
1061 
1062 	(void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
1063 	    xzp, "", NULL, acl_ids.z_fuidp, vap);
1064 
1065 	zfs_acl_ids_free(&acl_ids);
1066 	dmu_tx_commit(tx);
1067 
1068 	*xvpp = ZTOV(xzp);
1069 
1070 	return (0);
1071 }
1072 
1073 /*
1074  * Return a znode for the extended attribute directory for zp.
1075  * ** If the directory does not already exist, it is created **
1076  *
1077  *	IN:	zp	- znode to obtain attribute directory from
1078  *		cr	- credentials of caller
1079  *		flags	- flags from the VOP_LOOKUP call
1080  *
1081  *	OUT:	xzpp	- pointer to extended attribute znode
1082  *
1083  *	RETURN:	0 on success
1084  *		error number on failure
1085  */
1086 int
1087 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
1088 {
1089 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1090 	znode_t		*xzp;
1091 	zfs_dirlock_t	*dl;
1092 	vattr_t		va;
1093 	int		error;
1094 top:
1095 	error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR, NULL, NULL);
1096 	if (error)
1097 		return (error);
1098 
1099 	if (xzp != NULL) {
1100 		*xvpp = ZTOV(xzp);
1101 		zfs_dirent_unlock(dl);
1102 		return (0);
1103 	}
1104 
1105 
1106 	if (!(flags & CREATE_XATTR_DIR)) {
1107 		zfs_dirent_unlock(dl);
1108 		return (SET_ERROR(ENOENT));
1109 	}
1110 
1111 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
1112 		zfs_dirent_unlock(dl);
1113 		return (SET_ERROR(EROFS));
1114 	}
1115 
1116 	/*
1117 	 * The ability to 'create' files in an attribute
1118 	 * directory comes from the write_xattr permission on the base file.
1119 	 *
1120 	 * The ability to 'search' an attribute directory requires
1121 	 * read_xattr permission on the base file.
1122 	 *
1123 	 * Once in a directory the ability to read/write attributes
1124 	 * is controlled by the permissions on the attribute file.
1125 	 */
1126 	va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
1127 	va.va_type = VDIR;
1128 	va.va_mode = S_IFDIR | S_ISVTX | 0777;
1129 	zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
1130 
1131 	error = zfs_make_xattrdir(zp, &va, xvpp, cr);
1132 	zfs_dirent_unlock(dl);
1133 
1134 	if (error == ERESTART) {
1135 		/* NB: we already did dmu_tx_wait() if necessary */
1136 		goto top;
1137 	}
1138 
1139 	return (error);
1140 }
1141 
1142 /*
1143  * Decide whether it is okay to remove within a sticky directory.
1144  *
1145  * In sticky directories, write access is not sufficient;
1146  * you can remove entries from a directory only if:
1147  *
1148  *	you own the directory,
1149  *	you own the entry,
1150  *	the entry is a plain file and you have write access,
1151  *	or you are privileged (checked in secpolicy...).
1152  *
1153  * The function returns 0 if remove access is granted.
1154  */
1155 int
1156 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
1157 {
1158 	uid_t		uid;
1159 	uid_t		downer;
1160 	uid_t		fowner;
1161 	zfsvfs_t	*zfsvfs = zdp->z_zfsvfs;
1162 
1163 	if (zdp->z_zfsvfs->z_replay)
1164 		return (0);
1165 
1166 	if ((zdp->z_mode & S_ISVTX) == 0)
1167 		return (0);
1168 
1169 	downer = zfs_fuid_map_id(zfsvfs, zdp->z_uid, cr, ZFS_OWNER);
1170 	fowner = zfs_fuid_map_id(zfsvfs, zp->z_uid, cr, ZFS_OWNER);
1171 
1172 	if ((uid = crgetuid(cr)) == downer || uid == fowner ||
1173 	    (ZTOV(zp)->v_type == VREG &&
1174 	    zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
1175 		return (0);
1176 	else
1177 		return (secpolicy_vnode_remove(cr));
1178 }
1179