xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_dir.c (revision e07d85f87c3920e032adb855fdc500e4616c7718)
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 void
484 zfs_unlinked_drain(zfsvfs_t *zfsvfs)
485 {
486 	zap_cursor_t	zc;
487 	zap_attribute_t zap;
488 	dmu_object_info_t doi;
489 	znode_t		*zp;
490 	int		error;
491 
492 	/*
493 	 * Interate over the contents of the unlinked set.
494 	 */
495 	for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
496 	    zap_cursor_retrieve(&zc, &zap) == 0;
497 	    zap_cursor_advance(&zc)) {
498 
499 		/*
500 		 * See what kind of object we have in list
501 		 */
502 
503 		error = dmu_object_info(zfsvfs->z_os,
504 		    zap.za_first_integer, &doi);
505 		if (error != 0)
506 			continue;
507 
508 		ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
509 		    (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
510 		/*
511 		 * We need to re-mark these list entries for deletion,
512 		 * so we pull them back into core and set zp->z_unlinked.
513 		 */
514 		error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
515 
516 		/*
517 		 * We may pick up znodes that are already marked for deletion.
518 		 * This could happen during the purge of an extended attribute
519 		 * directory.  All we need to do is skip over them, since they
520 		 * are already in the system marked z_unlinked.
521 		 */
522 		if (error != 0)
523 			continue;
524 
525 		zp->z_unlinked = B_TRUE;
526 		VN_RELE(ZTOV(zp));
527 	}
528 	zap_cursor_fini(&zc);
529 }
530 
531 /*
532  * Delete the entire contents of a directory.  Return a count
533  * of the number of entries that could not be deleted. If we encounter
534  * an error, return a count of at least one so that the directory stays
535  * in the unlinked set.
536  *
537  * NOTE: this function assumes that the directory is inactive,
538  *	so there is no need to lock its entries before deletion.
539  *	Also, it assumes the directory contents is *only* regular
540  *	files.
541  */
542 static int
543 zfs_purgedir(znode_t *dzp)
544 {
545 	zap_cursor_t	zc;
546 	zap_attribute_t	zap;
547 	znode_t		*xzp;
548 	dmu_tx_t	*tx;
549 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
550 	zfs_dirlock_t	dl;
551 	int skipped = 0;
552 	int error;
553 
554 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
555 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
556 	    zap_cursor_advance(&zc)) {
557 		error = zfs_zget(zfsvfs,
558 		    ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
559 		if (error) {
560 			skipped += 1;
561 			continue;
562 		}
563 
564 		ASSERT((ZTOV(xzp)->v_type == VREG) ||
565 		    (ZTOV(xzp)->v_type == VLNK));
566 
567 		tx = dmu_tx_create(zfsvfs->z_os);
568 		dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
569 		dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
570 		dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
571 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
572 		/* Is this really needed ? */
573 		zfs_sa_upgrade_txholds(tx, xzp);
574 		dmu_tx_mark_netfree(tx);
575 		error = dmu_tx_assign(tx, TXG_WAIT);
576 		if (error) {
577 			dmu_tx_abort(tx);
578 			VN_RELE(ZTOV(xzp));
579 			skipped += 1;
580 			continue;
581 		}
582 		bzero(&dl, sizeof (dl));
583 		dl.dl_dzp = dzp;
584 		dl.dl_name = zap.za_name;
585 
586 		error = zfs_link_destroy(&dl, xzp, tx, 0, NULL);
587 		if (error)
588 			skipped += 1;
589 		dmu_tx_commit(tx);
590 
591 		VN_RELE(ZTOV(xzp));
592 	}
593 	zap_cursor_fini(&zc);
594 	if (error != ENOENT)
595 		skipped += 1;
596 	return (skipped);
597 }
598 
599 void
600 zfs_rmnode(znode_t *zp)
601 {
602 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
603 	objset_t	*os = zfsvfs->z_os;
604 	znode_t		*xzp = NULL;
605 	dmu_tx_t	*tx;
606 	uint64_t	acl_obj;
607 	uint64_t	xattr_obj;
608 	int		error;
609 
610 	ASSERT(zp->z_links == 0);
611 	ASSERT(ZTOV(zp)->v_count == 0);
612 
613 	/*
614 	 * If this is an attribute directory, purge its contents.
615 	 */
616 	if (ZTOV(zp)->v_type == VDIR && (zp->z_pflags & ZFS_XATTR)) {
617 		if (zfs_purgedir(zp) != 0) {
618 			/*
619 			 * Not enough space to delete some xattrs.
620 			 * Leave it in the unlinked set.
621 			 */
622 			zfs_znode_dmu_fini(zp);
623 			zfs_znode_free(zp);
624 			return;
625 		}
626 	} else {
627 		/*
628 		 * Free up all the data in the file.  We don't do this for
629 		 * XATTR directories because we need truncate and remove to be
630 		 * in the same tx, like in zfs_znode_delete(). Otherwise, if
631 		 * we crash here we'll end up with an inconsistent truncated
632 		 * zap object in the delete queue.  Note a truncated file is
633 		 * harmless since it only contains user data.
634 		 */
635 		error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
636 		if (error) {
637 			/*
638 			 * Not enough space or we were interrupted by unmount.
639 			 * Leave the file in the unlinked set.
640 			 */
641 			zfs_znode_dmu_fini(zp);
642 			zfs_znode_free(zp);
643 			return;
644 		}
645 	}
646 
647 	/*
648 	 * If the file has extended attributes, we're going to unlink
649 	 * the xattr dir.
650 	 */
651 	error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
652 	    &xattr_obj, sizeof (xattr_obj));
653 	if (error == 0 && xattr_obj) {
654 		error = zfs_zget(zfsvfs, xattr_obj, &xzp);
655 		ASSERT(error == 0);
656 	}
657 
658 	acl_obj = zfs_external_acl(zp);
659 
660 	/*
661 	 * Set up the final transaction.
662 	 */
663 	tx = dmu_tx_create(os);
664 	dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
665 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
666 	if (xzp) {
667 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
668 		dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
669 	}
670 	if (acl_obj)
671 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
672 
673 	zfs_sa_upgrade_txholds(tx, zp);
674 	error = dmu_tx_assign(tx, TXG_WAIT);
675 	if (error) {
676 		/*
677 		 * Not enough space to delete the file.  Leave it in the
678 		 * unlinked set, leaking it until the fs is remounted (at
679 		 * which point we'll call zfs_unlinked_drain() to process it).
680 		 */
681 		dmu_tx_abort(tx);
682 		zfs_znode_dmu_fini(zp);
683 		zfs_znode_free(zp);
684 		goto out;
685 	}
686 
687 	if (xzp) {
688 		ASSERT(error == 0);
689 		mutex_enter(&xzp->z_lock);
690 		xzp->z_unlinked = B_TRUE;	/* mark xzp for deletion */
691 		xzp->z_links = 0;	/* no more links to it */
692 		VERIFY(0 == sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
693 		    &xzp->z_links, sizeof (xzp->z_links), tx));
694 		mutex_exit(&xzp->z_lock);
695 		zfs_unlinked_add(xzp, tx);
696 	}
697 
698 	/* Remove this znode from the unlinked set */
699 	VERIFY3U(0, ==,
700 	    zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
701 
702 	zfs_znode_delete(zp, tx);
703 
704 	dmu_tx_commit(tx);
705 out:
706 	if (xzp)
707 		VN_RELE(ZTOV(xzp));
708 }
709 
710 static uint64_t
711 zfs_dirent(znode_t *zp, uint64_t mode)
712 {
713 	uint64_t de = zp->z_id;
714 
715 	if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
716 		de |= IFTODT(mode) << 60;
717 	return (de);
718 }
719 
720 /*
721  * Link zp into dl.  Can only fail if zp has been unlinked.
722  */
723 int
724 zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
725 {
726 	znode_t *dzp = dl->dl_dzp;
727 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
728 	vnode_t *vp = ZTOV(zp);
729 	uint64_t value;
730 	int zp_is_dir = (vp->v_type == VDIR);
731 	sa_bulk_attr_t bulk[5];
732 	uint64_t mtime[2], ctime[2];
733 	int count = 0;
734 	int error;
735 
736 	mutex_enter(&zp->z_lock);
737 
738 	if (!(flag & ZRENAMING)) {
739 		if (zp->z_unlinked) {	/* no new links to unlinked zp */
740 			ASSERT(!(flag & (ZNEW | ZEXISTS)));
741 			mutex_exit(&zp->z_lock);
742 			return (SET_ERROR(ENOENT));
743 		}
744 		zp->z_links++;
745 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
746 		    &zp->z_links, sizeof (zp->z_links));
747 
748 	}
749 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
750 	    &dzp->z_id, sizeof (dzp->z_id));
751 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
752 	    &zp->z_pflags, sizeof (zp->z_pflags));
753 
754 	if (!(flag & ZNEW)) {
755 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
756 		    ctime, sizeof (ctime));
757 		zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
758 		    ctime, B_TRUE);
759 	}
760 	error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
761 	ASSERT(error == 0);
762 
763 	mutex_exit(&zp->z_lock);
764 
765 	mutex_enter(&dzp->z_lock);
766 	dzp->z_size++;
767 	dzp->z_links += zp_is_dir;
768 	count = 0;
769 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
770 	    &dzp->z_size, sizeof (dzp->z_size));
771 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
772 	    &dzp->z_links, sizeof (dzp->z_links));
773 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
774 	    mtime, sizeof (mtime));
775 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
776 	    ctime, sizeof (ctime));
777 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
778 	    &dzp->z_pflags, sizeof (dzp->z_pflags));
779 	zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
780 	error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
781 	ASSERT(error == 0);
782 	mutex_exit(&dzp->z_lock);
783 
784 	value = zfs_dirent(zp, zp->z_mode);
785 	error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
786 	    8, 1, &value, tx);
787 	ASSERT(error == 0);
788 
789 	dnlc_update(ZTOV(dzp), dl->dl_name, vp);
790 
791 	return (0);
792 }
793 
794 /*
795  * The match type in the code for this function should conform to:
796  *
797  * ------------------------------------------------------------------------
798  * fs type  | z_norm      | lookup type | match type
799  * ---------|-------------|-------------|----------------------------------
800  * CS !norm | 0           |           0 | 0 (exact)
801  * CS  norm | formX       |           0 | MT_NORMALIZE
802  * CI !norm | upper       |   !ZCIEXACT | MT_NORMALIZE
803  * CI !norm | upper       |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
804  * CI  norm | upper|formX |   !ZCIEXACT | MT_NORMALIZE
805  * CI  norm | upper|formX |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
806  * CM !norm | upper       |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
807  * CM !norm | upper       |     ZCILOOK | MT_NORMALIZE
808  * CM  norm | upper|formX |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
809  * CM  norm | upper|formX |     ZCILOOK | MT_NORMALIZE
810  *
811  * Abbreviations:
812  *    CS = Case Sensitive, CI = Case Insensitive, CM = Case Mixed
813  *    upper = case folding set by fs type on creation (U8_TEXTPREP_TOUPPER)
814  *    formX = unicode normalization form set on fs creation
815  */
816 static int
817 zfs_dropname(zfs_dirlock_t *dl, znode_t *zp, znode_t *dzp, dmu_tx_t *tx,
818     int flag)
819 {
820 	int error;
821 
822 	if (zp->z_zfsvfs->z_norm) {
823 		matchtype_t mt = MT_NORMALIZE;
824 
825 		if ((zp->z_zfsvfs->z_case == ZFS_CASE_INSENSITIVE &&
826 		    (flag & ZCIEXACT)) ||
827 		    (zp->z_zfsvfs->z_case == ZFS_CASE_MIXED &&
828 		    !(flag & ZCILOOK))) {
829 			mt |= MT_MATCH_CASE;
830 		}
831 
832 		error = zap_remove_norm(zp->z_zfsvfs->z_os, dzp->z_id,
833 		    dl->dl_name, mt, tx);
834 	} else {
835 		error = zap_remove(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
836 		    tx);
837 	}
838 
839 	return (error);
840 }
841 
842 /*
843  * Unlink zp from dl, and mark zp for deletion if this was the last link.
844  * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
845  * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
846  * If it's non-NULL, we use it to indicate whether the znode needs deletion,
847  * and it's the caller's job to do it.
848  */
849 int
850 zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
851     boolean_t *unlinkedp)
852 {
853 	znode_t *dzp = dl->dl_dzp;
854 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
855 	vnode_t *vp = ZTOV(zp);
856 	int zp_is_dir = (vp->v_type == VDIR);
857 	boolean_t unlinked = B_FALSE;
858 	sa_bulk_attr_t bulk[5];
859 	uint64_t mtime[2], ctime[2];
860 	int count = 0;
861 	int error;
862 
863 	dnlc_remove(ZTOV(dzp), dl->dl_name);
864 
865 	if (!(flag & ZRENAMING)) {
866 		if (vn_vfswlock(vp))		/* prevent new mounts on zp */
867 			return (SET_ERROR(EBUSY));
868 
869 		if (vn_ismntpt(vp)) {		/* don't remove mount point */
870 			vn_vfsunlock(vp);
871 			return (SET_ERROR(EBUSY));
872 		}
873 
874 		mutex_enter(&zp->z_lock);
875 
876 		if (zp_is_dir && !zfs_dirempty(zp)) {
877 			mutex_exit(&zp->z_lock);
878 			vn_vfsunlock(vp);
879 			return (SET_ERROR(EEXIST));
880 		}
881 
882 		/*
883 		 * If we get here, we are going to try to remove the object.
884 		 * First try removing the name from the directory; if that
885 		 * fails, return the error.
886 		 */
887 		error = zfs_dropname(dl, zp, dzp, tx, flag);
888 		if (error != 0) {
889 			mutex_exit(&zp->z_lock);
890 			vn_vfsunlock(vp);
891 			return (error);
892 		}
893 
894 		if (zp->z_links <= zp_is_dir) {
895 			zfs_panic_recover("zfs: link count on %s is %u, "
896 			    "should be at least %u",
897 			    zp->z_vnode->v_path != vn_vpath_empty ?
898 			    zp->z_vnode->v_path : "<unknown>",
899 			    (int)zp->z_links, zp_is_dir + 1);
900 			zp->z_links = zp_is_dir + 1;
901 		}
902 		if (--zp->z_links == zp_is_dir) {
903 			zp->z_unlinked = B_TRUE;
904 			zp->z_links = 0;
905 			unlinked = B_TRUE;
906 		} else {
907 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
908 			    NULL, &ctime, sizeof (ctime));
909 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
910 			    NULL, &zp->z_pflags, sizeof (zp->z_pflags));
911 			zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
912 			    B_TRUE);
913 		}
914 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
915 		    NULL, &zp->z_links, sizeof (zp->z_links));
916 		error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
917 		count = 0;
918 		ASSERT(error == 0);
919 		mutex_exit(&zp->z_lock);
920 		vn_vfsunlock(vp);
921 	} else {
922 		error = zfs_dropname(dl, zp, dzp, tx, flag);
923 		if (error != 0)
924 			return (error);
925 	}
926 
927 	mutex_enter(&dzp->z_lock);
928 	dzp->z_size--;		/* one dirent removed */
929 	dzp->z_links -= zp_is_dir;	/* ".." link from zp */
930 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
931 	    NULL, &dzp->z_links, sizeof (dzp->z_links));
932 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
933 	    NULL, &dzp->z_size, sizeof (dzp->z_size));
934 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
935 	    NULL, ctime, sizeof (ctime));
936 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
937 	    NULL, mtime, sizeof (mtime));
938 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
939 	    NULL, &dzp->z_pflags, sizeof (dzp->z_pflags));
940 	zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
941 	error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
942 	ASSERT(error == 0);
943 	mutex_exit(&dzp->z_lock);
944 
945 	if (unlinkedp != NULL)
946 		*unlinkedp = unlinked;
947 	else if (unlinked)
948 		zfs_unlinked_add(zp, tx);
949 
950 	return (0);
951 }
952 
953 /*
954  * Indicate whether the directory is empty.  Works with or without z_lock
955  * held, but can only be consider a hint in the latter case.  Returns true
956  * if only "." and ".." remain and there's no work in progress.
957  */
958 boolean_t
959 zfs_dirempty(znode_t *dzp)
960 {
961 	return (dzp->z_size == 2 && dzp->z_dirlocks == 0);
962 }
963 
964 int
965 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
966 {
967 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
968 	znode_t *xzp;
969 	dmu_tx_t *tx;
970 	int error;
971 	zfs_acl_ids_t acl_ids;
972 	boolean_t fuid_dirtied;
973 	uint64_t parent;
974 
975 	*xvpp = NULL;
976 
977 	if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr))
978 		return (error);
979 
980 	if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL,
981 	    &acl_ids)) != 0)
982 		return (error);
983 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
984 		zfs_acl_ids_free(&acl_ids);
985 		return (SET_ERROR(EDQUOT));
986 	}
987 
988 	tx = dmu_tx_create(zfsvfs->z_os);
989 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
990 	    ZFS_SA_BASE_ATTR_SIZE);
991 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
992 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
993 	fuid_dirtied = zfsvfs->z_fuid_dirty;
994 	if (fuid_dirtied)
995 		zfs_fuid_txhold(zfsvfs, tx);
996 	error = dmu_tx_assign(tx, TXG_WAIT);
997 	if (error) {
998 		zfs_acl_ids_free(&acl_ids);
999 		dmu_tx_abort(tx);
1000 		return (error);
1001 	}
1002 	zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, &acl_ids);
1003 
1004 	if (fuid_dirtied)
1005 		zfs_fuid_sync(zfsvfs, tx);
1006 
1007 #ifdef DEBUG
1008 	error = sa_lookup(xzp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
1009 	    &parent, sizeof (parent));
1010 	ASSERT(error == 0 && parent == zp->z_id);
1011 #endif
1012 
1013 	VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xzp->z_id,
1014 	    sizeof (xzp->z_id), tx));
1015 
1016 	(void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
1017 	    xzp, "", NULL, acl_ids.z_fuidp, vap);
1018 
1019 	zfs_acl_ids_free(&acl_ids);
1020 	dmu_tx_commit(tx);
1021 
1022 	*xvpp = ZTOV(xzp);
1023 
1024 	return (0);
1025 }
1026 
1027 /*
1028  * Return a znode for the extended attribute directory for zp.
1029  * ** If the directory does not already exist, it is created **
1030  *
1031  *	IN:	zp	- znode to obtain attribute directory from
1032  *		cr	- credentials of caller
1033  *		flags	- flags from the VOP_LOOKUP call
1034  *
1035  *	OUT:	xzpp	- pointer to extended attribute znode
1036  *
1037  *	RETURN:	0 on success
1038  *		error number on failure
1039  */
1040 int
1041 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
1042 {
1043 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1044 	znode_t		*xzp;
1045 	zfs_dirlock_t	*dl;
1046 	vattr_t		va;
1047 	int		error;
1048 top:
1049 	error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR, NULL, NULL);
1050 	if (error)
1051 		return (error);
1052 
1053 	if (xzp != NULL) {
1054 		*xvpp = ZTOV(xzp);
1055 		zfs_dirent_unlock(dl);
1056 		return (0);
1057 	}
1058 
1059 
1060 	if (!(flags & CREATE_XATTR_DIR)) {
1061 		zfs_dirent_unlock(dl);
1062 		return (SET_ERROR(ENOENT));
1063 	}
1064 
1065 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
1066 		zfs_dirent_unlock(dl);
1067 		return (SET_ERROR(EROFS));
1068 	}
1069 
1070 	/*
1071 	 * The ability to 'create' files in an attribute
1072 	 * directory comes from the write_xattr permission on the base file.
1073 	 *
1074 	 * The ability to 'search' an attribute directory requires
1075 	 * read_xattr permission on the base file.
1076 	 *
1077 	 * Once in a directory the ability to read/write attributes
1078 	 * is controlled by the permissions on the attribute file.
1079 	 */
1080 	va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
1081 	va.va_type = VDIR;
1082 	va.va_mode = S_IFDIR | S_ISVTX | 0777;
1083 	zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
1084 
1085 	error = zfs_make_xattrdir(zp, &va, xvpp, cr);
1086 	zfs_dirent_unlock(dl);
1087 
1088 	if (error == ERESTART) {
1089 		/* NB: we already did dmu_tx_wait() if necessary */
1090 		goto top;
1091 	}
1092 
1093 	return (error);
1094 }
1095 
1096 /*
1097  * Decide whether it is okay to remove within a sticky directory.
1098  *
1099  * In sticky directories, write access is not sufficient;
1100  * you can remove entries from a directory only if:
1101  *
1102  *	you own the directory,
1103  *	you own the entry,
1104  *	the entry is a plain file and you have write access,
1105  *	or you are privileged (checked in secpolicy...).
1106  *
1107  * The function returns 0 if remove access is granted.
1108  */
1109 int
1110 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
1111 {
1112 	uid_t  		uid;
1113 	uid_t		downer;
1114 	uid_t		fowner;
1115 	zfsvfs_t	*zfsvfs = zdp->z_zfsvfs;
1116 
1117 	if (zdp->z_zfsvfs->z_replay)
1118 		return (0);
1119 
1120 	if ((zdp->z_mode & S_ISVTX) == 0)
1121 		return (0);
1122 
1123 	downer = zfs_fuid_map_id(zfsvfs, zdp->z_uid, cr, ZFS_OWNER);
1124 	fowner = zfs_fuid_map_id(zfsvfs, zp->z_uid, cr, ZFS_OWNER);
1125 
1126 	if ((uid = crgetuid(cr)) == downer || uid == fowner ||
1127 	    (ZTOV(zp)->v_type == VREG &&
1128 	    zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
1129 		return (0);
1130 	else
1131 		return (secpolicy_vnode_remove(cr));
1132 }
1133