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