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