xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_dir.c (revision ddb365bfc9e868ad24ccdcb0dc91af18b10df082)
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 	dmu_tx_mark_netfree(tx);
721 	error = dmu_tx_assign(tx, TXG_WAIT);
722 	if (error) {
723 		/*
724 		 * Not enough space to delete the file.  Leave it in the
725 		 * unlinked set, leaking it until the fs is remounted (at
726 		 * which point we'll call zfs_unlinked_drain() to process it).
727 		 */
728 		dmu_tx_abort(tx);
729 		zfs_znode_dmu_fini(zp);
730 		zfs_znode_free(zp);
731 		goto out;
732 	}
733 
734 	if (xzp) {
735 		ASSERT(error == 0);
736 		mutex_enter(&xzp->z_lock);
737 		xzp->z_unlinked = B_TRUE;	/* mark xzp for deletion */
738 		xzp->z_links = 0;	/* no more links to it */
739 		VERIFY(0 == sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
740 		    &xzp->z_links, sizeof (xzp->z_links), tx));
741 		mutex_exit(&xzp->z_lock);
742 		zfs_unlinked_add(xzp, tx);
743 	}
744 
745 	/* Remove this znode from the unlinked set */
746 	VERIFY3U(0, ==,
747 	    zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
748 
749 	zfs_znode_delete(zp, tx);
750 
751 	dmu_tx_commit(tx);
752 out:
753 	if (xzp)
754 		VN_RELE(ZTOV(xzp));
755 }
756 
757 static uint64_t
758 zfs_dirent(znode_t *zp, uint64_t mode)
759 {
760 	uint64_t de = zp->z_id;
761 
762 	if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
763 		de |= IFTODT(mode) << 60;
764 	return (de);
765 }
766 
767 /*
768  * Link zp into dl.  Can only fail if zp has been unlinked.
769  */
770 int
771 zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
772 {
773 	znode_t *dzp = dl->dl_dzp;
774 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
775 	vnode_t *vp = ZTOV(zp);
776 	uint64_t value;
777 	int zp_is_dir = (vp->v_type == VDIR);
778 	sa_bulk_attr_t bulk[5];
779 	uint64_t mtime[2], ctime[2];
780 	int count = 0;
781 	int error;
782 
783 	mutex_enter(&zp->z_lock);
784 
785 	if (!(flag & ZRENAMING)) {
786 		if (zp->z_unlinked) {	/* no new links to unlinked zp */
787 			ASSERT(!(flag & (ZNEW | ZEXISTS)));
788 			mutex_exit(&zp->z_lock);
789 			return (SET_ERROR(ENOENT));
790 		}
791 		zp->z_links++;
792 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
793 		    &zp->z_links, sizeof (zp->z_links));
794 
795 	}
796 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
797 	    &dzp->z_id, sizeof (dzp->z_id));
798 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
799 	    &zp->z_pflags, sizeof (zp->z_pflags));
800 
801 	if (!(flag & ZNEW)) {
802 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
803 		    ctime, sizeof (ctime));
804 		zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
805 		    ctime, B_TRUE);
806 	}
807 	error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
808 	ASSERT(error == 0);
809 
810 	mutex_exit(&zp->z_lock);
811 
812 	mutex_enter(&dzp->z_lock);
813 	dzp->z_size++;
814 	dzp->z_links += zp_is_dir;
815 	count = 0;
816 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
817 	    &dzp->z_size, sizeof (dzp->z_size));
818 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
819 	    &dzp->z_links, sizeof (dzp->z_links));
820 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
821 	    mtime, sizeof (mtime));
822 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
823 	    ctime, sizeof (ctime));
824 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
825 	    &dzp->z_pflags, sizeof (dzp->z_pflags));
826 	zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
827 	error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
828 	ASSERT(error == 0);
829 	mutex_exit(&dzp->z_lock);
830 
831 	value = zfs_dirent(zp, zp->z_mode);
832 	error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
833 	    8, 1, &value, tx);
834 	ASSERT(error == 0);
835 
836 	dnlc_update(ZTOV(dzp), dl->dl_name, vp);
837 
838 	return (0);
839 }
840 
841 /*
842  * The match type in the code for this function should conform to:
843  *
844  * ------------------------------------------------------------------------
845  * fs type  | z_norm      | lookup type | match type
846  * ---------|-------------|-------------|----------------------------------
847  * CS !norm | 0           |           0 | 0 (exact)
848  * CS  norm | formX       |           0 | MT_NORMALIZE
849  * CI !norm | upper       |   !ZCIEXACT | MT_NORMALIZE
850  * CI !norm | upper       |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
851  * CI  norm | upper|formX |   !ZCIEXACT | MT_NORMALIZE
852  * CI  norm | upper|formX |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
853  * CM !norm | upper       |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
854  * CM !norm | upper       |     ZCILOOK | MT_NORMALIZE
855  * CM  norm | upper|formX |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
856  * CM  norm | upper|formX |     ZCILOOK | MT_NORMALIZE
857  *
858  * Abbreviations:
859  *    CS = Case Sensitive, CI = Case Insensitive, CM = Case Mixed
860  *    upper = case folding set by fs type on creation (U8_TEXTPREP_TOUPPER)
861  *    formX = unicode normalization form set on fs creation
862  */
863 static int
864 zfs_dropname(zfs_dirlock_t *dl, znode_t *zp, znode_t *dzp, dmu_tx_t *tx,
865     int flag)
866 {
867 	int error;
868 
869 	if (zp->z_zfsvfs->z_norm) {
870 		matchtype_t mt = MT_NORMALIZE;
871 
872 		if ((zp->z_zfsvfs->z_case == ZFS_CASE_INSENSITIVE &&
873 		    (flag & ZCIEXACT)) ||
874 		    (zp->z_zfsvfs->z_case == ZFS_CASE_MIXED &&
875 		    !(flag & ZCILOOK))) {
876 			mt |= MT_MATCH_CASE;
877 		}
878 
879 		error = zap_remove_norm(zp->z_zfsvfs->z_os, dzp->z_id,
880 		    dl->dl_name, mt, tx);
881 	} else {
882 		error = zap_remove(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
883 		    tx);
884 	}
885 
886 	return (error);
887 }
888 
889 /*
890  * Unlink zp from dl, and mark zp for deletion if this was the last link.
891  * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
892  * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
893  * If it's non-NULL, we use it to indicate whether the znode needs deletion,
894  * and it's the caller's job to do it.
895  */
896 int
897 zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
898     boolean_t *unlinkedp)
899 {
900 	znode_t *dzp = dl->dl_dzp;
901 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
902 	vnode_t *vp = ZTOV(zp);
903 	int zp_is_dir = (vp->v_type == VDIR);
904 	boolean_t unlinked = B_FALSE;
905 	sa_bulk_attr_t bulk[5];
906 	uint64_t mtime[2], ctime[2];
907 	int count = 0;
908 	int error;
909 
910 	dnlc_remove(ZTOV(dzp), dl->dl_name);
911 
912 	if (!(flag & ZRENAMING)) {
913 		if (vn_vfswlock(vp))		/* prevent new mounts on zp */
914 			return (SET_ERROR(EBUSY));
915 
916 		if (vn_ismntpt(vp)) {		/* don't remove mount point */
917 			vn_vfsunlock(vp);
918 			return (SET_ERROR(EBUSY));
919 		}
920 
921 		mutex_enter(&zp->z_lock);
922 
923 		if (zp_is_dir && !zfs_dirempty(zp)) {
924 			mutex_exit(&zp->z_lock);
925 			vn_vfsunlock(vp);
926 			return (SET_ERROR(EEXIST));
927 		}
928 
929 		/*
930 		 * If we get here, we are going to try to remove the object.
931 		 * First try removing the name from the directory; if that
932 		 * fails, return the error.
933 		 */
934 		error = zfs_dropname(dl, zp, dzp, tx, flag);
935 		if (error != 0) {
936 			mutex_exit(&zp->z_lock);
937 			vn_vfsunlock(vp);
938 			return (error);
939 		}
940 
941 		if (zp->z_links <= zp_is_dir) {
942 			zfs_panic_recover("zfs: link count on %s is %u, "
943 			    "should be at least %u",
944 			    zp->z_vnode->v_path != vn_vpath_empty ?
945 			    zp->z_vnode->v_path : "<unknown>",
946 			    (int)zp->z_links, zp_is_dir + 1);
947 			zp->z_links = zp_is_dir + 1;
948 		}
949 		if (--zp->z_links == zp_is_dir) {
950 			zp->z_unlinked = B_TRUE;
951 			zp->z_links = 0;
952 			unlinked = B_TRUE;
953 		} else {
954 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
955 			    NULL, &ctime, sizeof (ctime));
956 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
957 			    NULL, &zp->z_pflags, sizeof (zp->z_pflags));
958 			zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
959 			    B_TRUE);
960 		}
961 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
962 		    NULL, &zp->z_links, sizeof (zp->z_links));
963 		error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
964 		count = 0;
965 		ASSERT(error == 0);
966 		mutex_exit(&zp->z_lock);
967 		vn_vfsunlock(vp);
968 	} else {
969 		error = zfs_dropname(dl, zp, dzp, tx, flag);
970 		if (error != 0)
971 			return (error);
972 	}
973 
974 	mutex_enter(&dzp->z_lock);
975 	dzp->z_size--;		/* one dirent removed */
976 	dzp->z_links -= zp_is_dir;	/* ".." link from zp */
977 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
978 	    NULL, &dzp->z_links, sizeof (dzp->z_links));
979 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
980 	    NULL, &dzp->z_size, sizeof (dzp->z_size));
981 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
982 	    NULL, ctime, sizeof (ctime));
983 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
984 	    NULL, mtime, sizeof (mtime));
985 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
986 	    NULL, &dzp->z_pflags, sizeof (dzp->z_pflags));
987 	zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
988 	error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
989 	ASSERT(error == 0);
990 	mutex_exit(&dzp->z_lock);
991 
992 	if (unlinkedp != NULL)
993 		*unlinkedp = unlinked;
994 	else if (unlinked)
995 		zfs_unlinked_add(zp, tx);
996 
997 	return (0);
998 }
999 
1000 /*
1001  * Indicate whether the directory is empty.  Works with or without z_lock
1002  * held, but can only be consider a hint in the latter case.  Returns true
1003  * if only "." and ".." remain and there's no work in progress.
1004  */
1005 boolean_t
1006 zfs_dirempty(znode_t *dzp)
1007 {
1008 	return (dzp->z_size == 2 && dzp->z_dirlocks == 0);
1009 }
1010 
1011 int
1012 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
1013 {
1014 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1015 	znode_t *xzp;
1016 	dmu_tx_t *tx;
1017 	int error;
1018 	zfs_acl_ids_t acl_ids;
1019 	boolean_t fuid_dirtied;
1020 	uint64_t parent;
1021 
1022 	*xvpp = NULL;
1023 
1024 	if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr))
1025 		return (error);
1026 
1027 	if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL,
1028 	    &acl_ids)) != 0)
1029 		return (error);
1030 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, zp->z_projid)) {
1031 		zfs_acl_ids_free(&acl_ids);
1032 		return (SET_ERROR(EDQUOT));
1033 	}
1034 
1035 	tx = dmu_tx_create(zfsvfs->z_os);
1036 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1037 	    ZFS_SA_BASE_ATTR_SIZE);
1038 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1039 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1040 	fuid_dirtied = zfsvfs->z_fuid_dirty;
1041 	if (fuid_dirtied)
1042 		zfs_fuid_txhold(zfsvfs, tx);
1043 	error = dmu_tx_assign(tx, TXG_WAIT);
1044 	if (error) {
1045 		zfs_acl_ids_free(&acl_ids);
1046 		dmu_tx_abort(tx);
1047 		return (error);
1048 	}
1049 	zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, &acl_ids);
1050 
1051 	if (fuid_dirtied)
1052 		zfs_fuid_sync(zfsvfs, tx);
1053 
1054 #ifdef DEBUG
1055 	error = sa_lookup(xzp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
1056 	    &parent, sizeof (parent));
1057 	ASSERT(error == 0 && parent == zp->z_id);
1058 #endif
1059 
1060 	VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xzp->z_id,
1061 	    sizeof (xzp->z_id), tx));
1062 
1063 	(void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
1064 	    xzp, "", NULL, acl_ids.z_fuidp, vap);
1065 
1066 	zfs_acl_ids_free(&acl_ids);
1067 	dmu_tx_commit(tx);
1068 
1069 	*xvpp = ZTOV(xzp);
1070 
1071 	return (0);
1072 }
1073 
1074 /*
1075  * Return a znode for the extended attribute directory for zp.
1076  * ** If the directory does not already exist, it is created **
1077  *
1078  *	IN:	zp	- znode to obtain attribute directory from
1079  *		cr	- credentials of caller
1080  *		flags	- flags from the VOP_LOOKUP call
1081  *
1082  *	OUT:	xzpp	- pointer to extended attribute znode
1083  *
1084  *	RETURN:	0 on success
1085  *		error number on failure
1086  */
1087 int
1088 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
1089 {
1090 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1091 	znode_t		*xzp;
1092 	zfs_dirlock_t	*dl;
1093 	vattr_t		va;
1094 	int		error;
1095 top:
1096 	error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR, NULL, NULL);
1097 	if (error)
1098 		return (error);
1099 
1100 	if (xzp != NULL) {
1101 		*xvpp = ZTOV(xzp);
1102 		zfs_dirent_unlock(dl);
1103 		return (0);
1104 	}
1105 
1106 
1107 	if (!(flags & CREATE_XATTR_DIR)) {
1108 		zfs_dirent_unlock(dl);
1109 		return (SET_ERROR(ENOENT));
1110 	}
1111 
1112 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
1113 		zfs_dirent_unlock(dl);
1114 		return (SET_ERROR(EROFS));
1115 	}
1116 
1117 	/*
1118 	 * The ability to 'create' files in an attribute
1119 	 * directory comes from the write_xattr permission on the base file.
1120 	 *
1121 	 * The ability to 'search' an attribute directory requires
1122 	 * read_xattr permission on the base file.
1123 	 *
1124 	 * Once in a directory the ability to read/write attributes
1125 	 * is controlled by the permissions on the attribute file.
1126 	 */
1127 	va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
1128 	va.va_type = VDIR;
1129 	va.va_mode = S_IFDIR | S_ISVTX | 0777;
1130 	zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
1131 
1132 	error = zfs_make_xattrdir(zp, &va, xvpp, cr);
1133 	zfs_dirent_unlock(dl);
1134 
1135 	if (error == ERESTART) {
1136 		/* NB: we already did dmu_tx_wait() if necessary */
1137 		goto top;
1138 	}
1139 
1140 	return (error);
1141 }
1142 
1143 /*
1144  * Decide whether it is okay to remove within a sticky directory.
1145  *
1146  * In sticky directories, write access is not sufficient;
1147  * you can remove entries from a directory only if:
1148  *
1149  *	you own the directory,
1150  *	you own the entry,
1151  *	the entry is a plain file and you have write access,
1152  *	or you are privileged (checked in secpolicy...).
1153  *
1154  * The function returns 0 if remove access is granted.
1155  */
1156 int
1157 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
1158 {
1159 	uid_t		uid;
1160 	uid_t		downer;
1161 	uid_t		fowner;
1162 	zfsvfs_t	*zfsvfs = zdp->z_zfsvfs;
1163 
1164 	if (zdp->z_zfsvfs->z_replay)
1165 		return (0);
1166 
1167 	if ((zdp->z_mode & S_ISVTX) == 0)
1168 		return (0);
1169 
1170 	downer = zfs_fuid_map_id(zfsvfs, zdp->z_uid, cr, ZFS_OWNER);
1171 	fowner = zfs_fuid_map_id(zfsvfs, zp->z_uid, cr, ZFS_OWNER);
1172 
1173 	if ((uid = crgetuid(cr)) == downer || uid == fowner ||
1174 	    (ZTOV(zp)->v_type == VREG &&
1175 	    zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
1176 		return (0);
1177 	else
1178 		return (secpolicy_vnode_remove(cr));
1179 }
1180