xref: /titanic_41/usr/src/uts/common/fs/zfs/zfs_dir.c (revision d10e4ef2fabf16c3237c6d6592496df3eac6a1ef)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/time.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/resource.h>
34 #include <sys/vfs.h>
35 #include <sys/vnode.h>
36 #include <sys/file.h>
37 #include <sys/mode.h>
38 #include <sys/kmem.h>
39 #include <sys/uio.h>
40 #include <sys/pathname.h>
41 #include <sys/cmn_err.h>
42 #include <sys/errno.h>
43 #include <sys/stat.h>
44 #include <sys/unistd.h>
45 #include <sys/random.h>
46 #include <sys/policy.h>
47 #include <sys/zfs_dir.h>
48 #include <sys/zfs_acl.h>
49 #include <sys/fs/zfs.h>
50 #include "fs/fs_subr.h"
51 #include <sys/zap.h>
52 #include <sys/dmu.h>
53 #include <sys/atomic.h>
54 #include <sys/zfs_ctldir.h>
55 #include <sys/dnlc.h>
56 
57 /*
58  * Lock a directory entry.  A dirlock on <dzp, name> protects that name
59  * in dzp's directory zap object.  As long as you hold a dirlock, you can
60  * assume two things: (1) dzp cannot be reaped, and (2) no other thread
61  * can change the zap entry for (i.e. link or unlink) this name.
62  *
63  * Input arguments:
64  *	dzp	- znode for directory
65  *	name	- name of entry to lock
66  *	flag	- ZNEW: if the entry already exists, fail with EEXIST.
67  *		  ZEXISTS: if the entry does not exist, fail with ENOENT.
68  *		  ZSHARED: allow concurrent access with other ZSHARED callers.
69  *		  ZXATTR: we want dzp's xattr directory
70  *
71  * Output arguments:
72  *	zpp	- pointer to the znode for the entry (NULL if there isn't one)
73  *	dlpp	- pointer to the dirlock for this entry (NULL on error)
74  *
75  * Return value: 0 on success or errno on failure.
76  *
77  * NOTE: Always checks for, and rejects, '.' and '..'.
78  */
79 int
80 zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
81 	int flag)
82 {
83 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
84 	zfs_dirlock_t	*dl;
85 	uint64_t	zoid;
86 	int		error;
87 	vnode_t		*vp;
88 
89 	*zpp = NULL;
90 	*dlpp = NULL;
91 
92 	/*
93 	 * Verify that we are not trying to lock '.', '..', or '.zfs'
94 	 */
95 	if (name[0] == '.' &&
96 	    (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) ||
97 	    zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0)
98 		return (EEXIST);
99 
100 	/*
101 	 * Wait until there are no locks on this name.
102 	 */
103 	mutex_enter(&dzp->z_lock);
104 	for (;;) {
105 		if (dzp->z_reap) {
106 			mutex_exit(&dzp->z_lock);
107 			return (ENOENT);
108 		}
109 		for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next)
110 			if (strcmp(name, dl->dl_name) == 0)
111 				break;
112 		if (dl == NULL)	{
113 			/*
114 			 * Allocate a new dirlock and add it to the list.
115 			 */
116 			dl = kmem_alloc(sizeof (zfs_dirlock_t), KM_SLEEP);
117 			cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL);
118 			dl->dl_name = name;
119 			dl->dl_sharecnt = 0;
120 			dl->dl_namesize = 0;
121 			dl->dl_dzp = dzp;
122 			dl->dl_next = dzp->z_dirlocks;
123 			dzp->z_dirlocks = dl;
124 			break;
125 		}
126 		if ((flag & ZSHARED) && dl->dl_sharecnt != 0)
127 			break;
128 		cv_wait(&dl->dl_cv, &dzp->z_lock);
129 	}
130 
131 	if ((flag & ZSHARED) && ++dl->dl_sharecnt > 1 && dl->dl_namesize == 0) {
132 		/*
133 		 * We're the second shared reference to dl.  Make a copy of
134 		 * dl_name in case the first thread goes away before we do.
135 		 * Note that we initialize the new name before storing its
136 		 * pointer into dl_name, because the first thread may load
137 		 * dl->dl_name at any time.  He'll either see the old value,
138 		 * which is his, or the new shared copy; either is OK.
139 		 */
140 		dl->dl_namesize = strlen(dl->dl_name) + 1;
141 		name = kmem_alloc(dl->dl_namesize, KM_SLEEP);
142 		bcopy(dl->dl_name, name, dl->dl_namesize);
143 		dl->dl_name = name;
144 	}
145 
146 	mutex_exit(&dzp->z_lock);
147 
148 	/*
149 	 * We have a dirlock on the name.  (Note that it is the dirlock,
150 	 * not the dzp's z_lock, that protects the name in the zap object.)
151 	 * See if there's an object by this name; if so, put a hold on it.
152 	 */
153 	if (flag & ZXATTR) {
154 		zoid = dzp->z_phys->zp_xattr;
155 		error = (zoid == 0 ? ENOENT : 0);
156 	} else {
157 		vp = dnlc_lookup(ZTOV(dzp), name);
158 		if (vp == DNLC_NO_VNODE) {
159 			VN_RELE(vp);
160 			error = ENOENT;
161 		} else if (vp) {
162 			if (flag & ZNEW) {
163 				zfs_dirent_unlock(dl);
164 				VN_RELE(vp);
165 				return (EEXIST);
166 			}
167 			*dlpp = dl;
168 			*zpp = VTOZ(vp);
169 			return (0);
170 		} else {
171 			error = zap_lookup(zfsvfs->z_os, dzp->z_id, name,
172 			    8, 1, &zoid);
173 			if (error == ENOENT)
174 				dnlc_update(ZTOV(dzp), name, DNLC_NO_VNODE);
175 		}
176 	}
177 	if (error) {
178 		if (error != ENOENT || (flag & ZEXISTS)) {
179 			zfs_dirent_unlock(dl);
180 			return (error);
181 		}
182 	} else {
183 		if (flag & ZNEW) {
184 			zfs_dirent_unlock(dl);
185 			return (EEXIST);
186 		}
187 		error = zfs_zget(zfsvfs, zoid, zpp);
188 		if (error) {
189 			zfs_dirent_unlock(dl);
190 			return (error);
191 		}
192 		if (!(flag & ZXATTR))
193 			dnlc_update(ZTOV(dzp), name, ZTOV(*zpp));
194 	}
195 
196 	*dlpp = dl;
197 
198 	return (0);
199 }
200 
201 /*
202  * Unlock this directory entry and wake anyone who was waiting for it.
203  */
204 void
205 zfs_dirent_unlock(zfs_dirlock_t *dl)
206 {
207 	znode_t *dzp = dl->dl_dzp;
208 	zfs_dirlock_t **prev_dl, *cur_dl;
209 
210 	mutex_enter(&dzp->z_lock);
211 	if (dl->dl_sharecnt > 1) {
212 		dl->dl_sharecnt--;
213 		mutex_exit(&dzp->z_lock);
214 		return;
215 	}
216 	prev_dl = &dzp->z_dirlocks;
217 	while ((cur_dl = *prev_dl) != dl)
218 		prev_dl = &cur_dl->dl_next;
219 	*prev_dl = dl->dl_next;
220 	cv_broadcast(&dl->dl_cv);
221 	mutex_exit(&dzp->z_lock);
222 
223 	if (dl->dl_namesize != 0)
224 		kmem_free(dl->dl_name, dl->dl_namesize);
225 	cv_destroy(&dl->dl_cv);
226 	kmem_free(dl, sizeof (*dl));
227 }
228 
229 /*
230  * Look up an entry in a directory.
231  *
232  * NOTE: '.' and '..' are handled as special cases because
233  *	no directory entries are actually stored for them.  If this is
234  *	the root of a filesystem, then '.zfs' is also treated as a
235  *	special pseudo-directory.
236  */
237 int
238 zfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp)
239 {
240 	zfs_dirlock_t *dl;
241 	znode_t *zp;
242 	int error = 0;
243 
244 	if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
245 		*vpp = ZTOV(dzp);
246 		VN_HOLD(*vpp);
247 	} else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
248 		zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
249 		/*
250 		 * If we are a snapshot mounted under .zfs, return
251 		 * the vp for the snapshot directory.
252 		 */
253 		if (dzp->z_phys->zp_parent == dzp->z_id &&
254 		    zfsvfs->z_parent != zfsvfs) {
255 			error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir,
256 			    "snapshot", vpp, NULL, 0, NULL, kcred);
257 			return (error);
258 		}
259 		rw_enter(&dzp->z_parent_lock, RW_READER);
260 		error = zfs_zget(zfsvfs, dzp->z_phys->zp_parent, &zp);
261 		if (error == 0)
262 			*vpp = ZTOV(zp);
263 		rw_exit(&dzp->z_parent_lock);
264 	} else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) {
265 		*vpp = zfsctl_root(dzp);
266 	} else {
267 		error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS | ZSHARED);
268 		if (error == 0) {
269 			*vpp = ZTOV(zp);
270 			zfs_dirent_unlock(dl);
271 			dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
272 		}
273 	}
274 
275 	return (error);
276 }
277 
278 static char *
279 zfs_dq_hexname(char namebuf[17], uint64_t x)
280 {
281 	char *name = &namebuf[16];
282 	const char digits[16] = "0123456789abcdef";
283 
284 	*name = '\0';
285 	do {
286 		*--name = digits[x & 0xf];
287 		x >>= 4;
288 	} while (x != 0);
289 
290 	return (name);
291 }
292 
293 /*
294  * Delete Queue Error Handling
295  *
296  * When dealing with the delete queue, we dmu_tx_hold_zap(), but we
297  * don't specify the name of the entry that we will be manipulating.  We
298  * also fib and say that we won't be adding any new entries to the
299  * delete queue, even though we might (this is to lower the minimum file
300  * size that can be deleted in a full filesystem).  So on the small
301  * chance that the delete queue is using a fat zap (ie. has more than
302  * 2000 entries), we *may* not pre-read a block that's needed.
303  * Therefore it is remotely possible for some of the assertions
304  * regarding the delete queue below to fail due to i/o error.  On a
305  * nondebug system, this will result in the space being leaked.
306  */
307 
308 void
309 zfs_dq_add(znode_t *zp, dmu_tx_t *tx)
310 {
311 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
312 	char obj_name[17];
313 	int error;
314 
315 	ASSERT(zp->z_reap);
316 	ASSERT3U(zp->z_phys->zp_links, ==, 0);
317 
318 	error = zap_add(zfsvfs->z_os, zfsvfs->z_dqueue,
319 	    zfs_dq_hexname(obj_name, zp->z_id), 8, 1, &zp->z_id, tx);
320 	ASSERT3U(error, ==, 0);
321 }
322 
323 /*
324  * Delete the entire contents of a directory.  Return a count
325  * of the number of entries that could not be deleted.
326  *
327  * NOTE: this function assumes that the directory is inactive,
328  *	so there is no need to lock its entries before deletion.
329  *	Also, it assumes the directory contents is *only* regular
330  *	files.
331  */
332 static int
333 zfs_purgedir(znode_t *dzp)
334 {
335 	zap_cursor_t	zc;
336 	zap_attribute_t	zap;
337 	znode_t		*xzp;
338 	dmu_tx_t	*tx;
339 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
340 	zfs_dirlock_t	dl;
341 	int skipped = 0;
342 	int error;
343 
344 	ASSERT(dzp->z_active == 0);
345 
346 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
347 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
348 	    zap_cursor_advance(&zc)) {
349 		error = zfs_zget(zfsvfs, zap.za_first_integer, &xzp);
350 		ASSERT3U(error, ==, 0);
351 
352 		ASSERT((ZTOV(xzp)->v_type == VREG) ||
353 		    (ZTOV(xzp)->v_type == VLNK));
354 
355 		tx = dmu_tx_create(zfsvfs->z_os);
356 		dmu_tx_hold_bonus(tx, dzp->z_id);
357 		dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
358 		dmu_tx_hold_bonus(tx, xzp->z_id);
359 		dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
360 		error = dmu_tx_assign(tx, TXG_WAIT);
361 		if (error) {
362 			dmu_tx_abort(tx);
363 			VN_RELE(ZTOV(xzp));
364 			skipped += 1;
365 			continue;
366 		}
367 		bzero(&dl, sizeof (dl));
368 		dl.dl_dzp = dzp;
369 		dl.dl_name = zap.za_name;
370 
371 		error = zfs_link_destroy(&dl, xzp, tx, 0, NULL);
372 		ASSERT3U(error, ==, 0);
373 		dmu_tx_commit(tx);
374 
375 		VN_RELE(ZTOV(xzp));
376 	}
377 	zap_cursor_fini(&zc);
378 	ASSERT(error == ENOENT);
379 	return (skipped);
380 }
381 
382 /*
383  * Special function to requeue the znodes for deletion that were
384  * in progress when we either crashed or umounted the file system.
385  *
386  * returns 1 if queue was drained.
387  */
388 static int
389 zfs_drain_dq(zfsvfs_t *zfsvfs)
390 {
391 	zap_cursor_t	zc;
392 	zap_attribute_t zap;
393 	dmu_object_info_t doi;
394 	znode_t		*zp;
395 	int		error;
396 
397 	/*
398 	 * Interate over the contents of the delete queue.
399 	 */
400 	for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_dqueue);
401 	    zap_cursor_retrieve(&zc, &zap) == 0;
402 	    zap_cursor_advance(&zc)) {
403 
404 		/*
405 		 * Create more threads if necessary to balance the load.
406 		 * quit if the delete threads have been shut down.
407 		 */
408 		if (zfs_delete_thread_target(zfsvfs, -1) != 0)
409 			return (0);
410 
411 		/*
412 		 * See what kind of object we have in queue
413 		 */
414 
415 		error = dmu_object_info(zfsvfs->z_os,
416 		    zap.za_first_integer, &doi);
417 		if (error != 0)
418 			continue;
419 
420 		ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
421 		    (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
422 		/*
423 		 * We need to re-mark these queue entries for reaping,
424 		 * so we pull them back into core and set zp->z_reap.
425 		 */
426 		error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
427 
428 		/*
429 		 * We may pick up znodes that are already marked for reaping.
430 		 * This could happen during the purge of an extended attribute
431 		 * directory.  All we need to do is skip over them, since they
432 		 * are already in the system to be processed by the delete
433 		 * thread(s).
434 		 */
435 		if (error != 0) {
436 			continue;
437 		}
438 
439 		zp->z_reap = 1;
440 		VN_RELE(ZTOV(zp));
441 	}
442 	zap_cursor_fini(&zc);
443 	return (1);
444 }
445 
446 void
447 zfs_delete_thread(void *arg)
448 {
449 	zfsvfs_t	*zfsvfs = arg;
450 	zfs_delete_t 	*zd = &zfsvfs->z_delete_head;
451 	znode_t		*zp;
452 	callb_cpr_t	cprinfo;
453 	int		drained;
454 
455 	CALLB_CPR_INIT(&cprinfo, &zd->z_mutex, callb_generic_cpr, "zfs_delete");
456 
457 	mutex_enter(&zd->z_mutex);
458 
459 	if (!zd->z_drained && !zd->z_draining) {
460 		zd->z_draining = B_TRUE;
461 		mutex_exit(&zd->z_mutex);
462 		drained = zfs_drain_dq(zfsvfs);
463 		mutex_enter(&zd->z_mutex);
464 		zd->z_draining = B_FALSE;
465 		zd->z_drained = drained;
466 		cv_broadcast(&zd->z_quiesce_cv);
467 	}
468 
469 	while (zd->z_thread_count <= zd->z_thread_target) {
470 		zp = list_head(&zd->z_znodes);
471 		if (zp == NULL) {
472 			ASSERT(zd->z_znode_count == 0);
473 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
474 			cv_wait(&zd->z_cv, &zd->z_mutex);
475 			CALLB_CPR_SAFE_END(&cprinfo, &zd->z_mutex);
476 			continue;
477 		}
478 		ASSERT(zd->z_znode_count != 0);
479 		list_remove(&zd->z_znodes, zp);
480 		if (--zd->z_znode_count == 0)
481 			cv_broadcast(&zd->z_quiesce_cv);
482 		mutex_exit(&zd->z_mutex);
483 		zfs_rmnode(zp);
484 		(void) zfs_delete_thread_target(zfsvfs, -1);
485 		mutex_enter(&zd->z_mutex);
486 	}
487 
488 	ASSERT(zd->z_thread_count != 0);
489 	if (--zd->z_thread_count == 0)
490 		cv_broadcast(&zd->z_cv);
491 
492 	CALLB_CPR_EXIT(&cprinfo);	/* NB: drops z_mutex */
493 	thread_exit();
494 }
495 
496 static int zfs_work_per_thread_shift = 11;	/* 2048 (2^11) per thread */
497 
498 /*
499  * Set the target number of delete threads to 'nthreads'.
500  * If nthreads == -1, choose a number based on current workload.
501  * If nthreads == 0, don't return until the threads have exited.
502  */
503 int
504 zfs_delete_thread_target(zfsvfs_t *zfsvfs, int nthreads)
505 {
506 	zfs_delete_t *zd = &zfsvfs->z_delete_head;
507 
508 	mutex_enter(&zd->z_mutex);
509 
510 	if (nthreads == -1) {
511 		if (zd->z_thread_target == 0) {
512 			mutex_exit(&zd->z_mutex);
513 			return (EBUSY);
514 		}
515 		nthreads = zd->z_znode_count >> zfs_work_per_thread_shift;
516 		nthreads = MIN(nthreads, ncpus << 1);
517 		nthreads = MAX(nthreads, 1);
518 		nthreads += !!zd->z_draining;
519 	}
520 
521 	zd->z_thread_target = nthreads;
522 
523 	while (zd->z_thread_count < zd->z_thread_target) {
524 		(void) thread_create(NULL, 0, zfs_delete_thread, zfsvfs,
525 		    0, &p0, TS_RUN, minclsyspri);
526 		zd->z_thread_count++;
527 	}
528 
529 	while (zd->z_thread_count > zd->z_thread_target && nthreads == 0) {
530 		cv_broadcast(&zd->z_cv);
531 		cv_wait(&zd->z_cv, &zd->z_mutex);
532 	}
533 
534 	mutex_exit(&zd->z_mutex);
535 
536 	return (0);
537 }
538 
539 /*
540  * Wait until everything that's been queued has been deleted.
541  */
542 void
543 zfs_delete_wait_empty(zfsvfs_t *zfsvfs)
544 {
545 	zfs_delete_t *zd = &zfsvfs->z_delete_head;
546 
547 	mutex_enter(&zd->z_mutex);
548 	ASSERT(zd->z_thread_target != 0);
549 	while (!zd->z_drained || zd->z_znode_count != 0) {
550 		ASSERT(zd->z_thread_target != 0);
551 		cv_wait(&zd->z_quiesce_cv, &zd->z_mutex);
552 	}
553 	mutex_exit(&zd->z_mutex);
554 }
555 
556 void
557 zfs_rmnode(znode_t *zp)
558 {
559 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
560 	objset_t	*os = zfsvfs->z_os;
561 	znode_t		*xzp = NULL;
562 	char		obj_name[17];
563 	dmu_tx_t	*tx;
564 	uint64_t	acl_obj;
565 	int		error;
566 
567 	ASSERT(zp->z_active == 0);
568 	ASSERT(ZTOV(zp)->v_count == 0);
569 	ASSERT(zp->z_phys->zp_links == 0);
570 
571 	/*
572 	 * If this is an attribute directory, purge its contents.
573 	 */
574 	if (ZTOV(zp)->v_type == VDIR && (zp->z_phys->zp_flags & ZFS_XATTR))
575 		if (zfs_purgedir(zp) != 0) {
576 			zfs_delete_t *delq = &zfsvfs->z_delete_head;
577 			/*
578 			 * Add this back to the delete list to be retried later.
579 			 *
580 			 * XXX - this could just busy loop on us...
581 			 */
582 			mutex_enter(&delq->z_mutex);
583 			list_insert_tail(&delq->z_znodes, zp);
584 			delq->z_znode_count++;
585 			mutex_exit(&delq->z_mutex);
586 			return;
587 		}
588 
589 	/*
590 	 * If the file has extended attributes, unlink the xattr dir.
591 	 */
592 	if (zp->z_phys->zp_xattr) {
593 		error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
594 		ASSERT(error == 0);
595 	}
596 
597 	acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
598 
599 	/*
600 	 * Set up the transaction.
601 	 */
602 	tx = dmu_tx_create(os);
603 	dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
604 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
605 	if (xzp) {
606 		dmu_tx_hold_bonus(tx, xzp->z_id);
607 		dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, TRUE, NULL);
608 	}
609 	if (acl_obj)
610 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
611 	error = dmu_tx_assign(tx, TXG_WAIT);
612 	if (error) {
613 		zfs_delete_t *delq = &zfsvfs->z_delete_head;
614 
615 		dmu_tx_abort(tx);
616 		/*
617 		 * Add this back to the delete list to be retried later.
618 		 *
619 		 * XXX - this could just busy loop on us...
620 		 */
621 		mutex_enter(&delq->z_mutex);
622 		list_insert_tail(&delq->z_znodes, zp);
623 		delq->z_znode_count++;
624 		mutex_exit(&delq->z_mutex);
625 		return;
626 	}
627 
628 	if (xzp) {
629 		dmu_buf_will_dirty(xzp->z_dbuf, tx);
630 		mutex_enter(&xzp->z_lock);
631 		xzp->z_reap = 1;		/* mark xzp for deletion */
632 		xzp->z_phys->zp_links = 0;	/* no more links to it */
633 		mutex_exit(&xzp->z_lock);
634 		zfs_dq_add(xzp, tx);		/* add xzp to delete queue */
635 	}
636 
637 	/*
638 	 * Remove this znode from delete queue
639 	 */
640 	error = zap_remove(os, zfsvfs->z_dqueue,
641 	    zfs_dq_hexname(obj_name, zp->z_id), tx);
642 	ASSERT3U(error, ==, 0);
643 
644 	zfs_znode_delete(zp, tx);
645 
646 	dmu_tx_commit(tx);
647 
648 	if (xzp)
649 		VN_RELE(ZTOV(xzp));
650 }
651 
652 /*
653  * Link zp into dl.  Can only fail if zp has been reaped.
654  */
655 int
656 zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
657 {
658 	znode_t *dzp = dl->dl_dzp;
659 	vnode_t *vp = ZTOV(zp);
660 	int zp_is_dir = (vp->v_type == VDIR);
661 	int error;
662 
663 	dmu_buf_will_dirty(zp->z_dbuf, tx);
664 	mutex_enter(&zp->z_lock);
665 
666 	if (!(flag & ZRENAMING)) {
667 		if (zp->z_reap) {	/* no new links to reaped zp */
668 			ASSERT(!(flag & (ZNEW | ZEXISTS)));
669 			mutex_exit(&zp->z_lock);
670 			return (ENOENT);
671 		}
672 		zp->z_phys->zp_links++;
673 	}
674 	zp->z_phys->zp_parent = dzp->z_id;	/* dzp is now zp's parent */
675 
676 	if (!(flag & ZNEW))
677 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
678 	mutex_exit(&zp->z_lock);
679 
680 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
681 	mutex_enter(&dzp->z_lock);
682 	dzp->z_phys->zp_size++;			/* one dirent added */
683 	dzp->z_phys->zp_links += zp_is_dir;	/* ".." link from zp */
684 	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
685 	mutex_exit(&dzp->z_lock);
686 
687 	error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
688 	    8, 1, &zp->z_id, tx);
689 	ASSERT(error == 0);
690 
691 	dnlc_update(ZTOV(dzp), dl->dl_name, vp);
692 
693 	return (0);
694 }
695 
696 /*
697  * Unlink zp from dl, and mark zp for reaping if this was the last link.
698  * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
699  * If 'reaped_ptr' is NULL, we put reaped znodes on the delete queue.
700  * If it's non-NULL, we use it to indicate whether the znode needs reaping,
701  * and it's the caller's job to do it.
702  */
703 int
704 zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
705 	int *reaped_ptr)
706 {
707 	znode_t *dzp = dl->dl_dzp;
708 	vnode_t *vp = ZTOV(zp);
709 	int zp_is_dir = (vp->v_type == VDIR);
710 	int reaped = 0;
711 	int error;
712 
713 	dnlc_remove(ZTOV(dzp), dl->dl_name);
714 
715 	if (!(flag & ZRENAMING)) {
716 		dmu_buf_will_dirty(zp->z_dbuf, tx);
717 
718 		if (vn_vfswlock(vp))		/* prevent new mounts on zp */
719 			return (EBUSY);
720 
721 		if (vn_ismntpt(vp)) {		/* don't remove mount point */
722 			vn_vfsunlock(vp);
723 			return (EBUSY);
724 		}
725 
726 		mutex_enter(&zp->z_lock);
727 		if (zp_is_dir && !zfs_dirempty(zp)) {	/* dir not empty */
728 			mutex_exit(&zp->z_lock);
729 			vn_vfsunlock(vp);
730 			return (EEXIST);
731 		}
732 		ASSERT(zp->z_phys->zp_links > zp_is_dir);
733 		if (--zp->z_phys->zp_links == zp_is_dir) {
734 			zp->z_reap = 1;
735 			zp->z_phys->zp_links = 0;
736 			reaped = 1;
737 		} else {
738 			zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
739 		}
740 		mutex_exit(&zp->z_lock);
741 		vn_vfsunlock(vp);
742 	}
743 
744 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
745 	mutex_enter(&dzp->z_lock);
746 	dzp->z_phys->zp_size--;			/* one dirent removed */
747 	dzp->z_phys->zp_links -= zp_is_dir;	/* ".." link from zp */
748 	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
749 	mutex_exit(&dzp->z_lock);
750 
751 	error = zap_remove(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name, tx);
752 	ASSERT(error == 0);
753 
754 	if (reaped_ptr != NULL)
755 		*reaped_ptr = reaped;
756 	else if (reaped)
757 		zfs_dq_add(zp, tx);
758 
759 	return (0);
760 }
761 
762 /*
763  * Indicate whether the directory is empty.  Works with or without z_lock
764  * held, but can only be consider a hint in the latter case.  Returns true
765  * if only "." and ".." remain and there's no work in progress.
766  */
767 boolean_t
768 zfs_dirempty(znode_t *dzp)
769 {
770 	return (dzp->z_phys->zp_size == 2 && dzp->z_dirlocks == 0);
771 }
772 
773 int
774 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
775 {
776 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
777 	znode_t *xzp;
778 	dmu_tx_t *tx;
779 	uint64_t xoid;
780 	int error;
781 
782 	*xvpp = NULL;
783 
784 	if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, cr))
785 		return (error);
786 
787 	tx = dmu_tx_create(zfsvfs->z_os);
788 	dmu_tx_hold_bonus(tx, zp->z_id);
789 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
790 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
791 	if (error) {
792 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT)
793 			dmu_tx_wait(tx);
794 		dmu_tx_abort(tx);
795 		return (error);
796 	}
797 	zfs_mknode(zp, vap, &xoid, tx, cr, IS_XATTR, &xzp, 0);
798 	ASSERT(xzp->z_id == xoid);
799 	ASSERT(xzp->z_phys->zp_parent == zp->z_id);
800 	dmu_buf_will_dirty(zp->z_dbuf, tx);
801 	zp->z_phys->zp_xattr = xoid;
802 
803 	(void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp, xzp, "");
804 	dmu_tx_commit(tx);
805 
806 	*xvpp = ZTOV(xzp);
807 
808 	return (0);
809 }
810 
811 /*
812  * Return a znode for the extended attribute directory for zp.
813  * ** If the directory does not already exist, it is created **
814  *
815  *	IN:	zp	- znode to obtain attribute directory from
816  *		cr	- credentials of caller
817  *
818  *	OUT:	xzpp	- pointer to extended attribute znode
819  *
820  *	RETURN:	0 on success
821  *		error number on failure
822  */
823 int
824 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr)
825 {
826 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
827 	znode_t		*xzp;
828 	zfs_dirlock_t	*dl;
829 	vattr_t		va;
830 	int		error;
831 top:
832 	error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR);
833 	if (error)
834 		return (error);
835 
836 	if (xzp != NULL) {
837 		*xvpp = ZTOV(xzp);
838 		zfs_dirent_unlock(dl);
839 		return (0);
840 	}
841 
842 	ASSERT(zp->z_phys->zp_xattr == 0);
843 
844 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
845 		zfs_dirent_unlock(dl);
846 		return (EROFS);
847 	}
848 
849 	/*
850 	 * The ability to 'create' files in an attribute
851 	 * directory comes from the write_xattr permission on the base file.
852 	 *
853 	 * The ability to 'search' an attribute directory requires
854 	 * read_xattr permission on the base file.
855 	 *
856 	 * Once in a directory the ability to read/write attributes
857 	 * is controlled by the permissions on the attribute file.
858 	 */
859 	va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
860 	va.va_type = VDIR;
861 	va.va_mode = S_IFDIR | S_ISVTX | 0777;
862 	va.va_uid = (uid_t)zp->z_phys->zp_uid;
863 	va.va_gid = (gid_t)zp->z_phys->zp_gid;
864 
865 	error = zfs_make_xattrdir(zp, &va, xvpp, cr);
866 	zfs_dirent_unlock(dl);
867 
868 	if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
869 		/* NB: we already did dmu_tx_wait() if necessary */
870 		goto top;
871 	}
872 
873 	return (error);
874 }
875 
876 /*
877  * Decide whether it is okay to remove within a sticky directory.
878  *
879  * In sticky directories, write access is not sufficient;
880  * you can remove entries from a directory only if:
881  *
882  *	you own the directory,
883  *	you own the entry,
884  *	the entry is a plain file and you have write access,
885  *	or you are privileged (checked in secpolicy...).
886  *
887  * The function returns 0 if remove access is granted.
888  */
889 int
890 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
891 {
892 	uid_t  		uid;
893 
894 	if (zdp->z_zfsvfs->z_assign >= TXG_INITIAL)	/* ZIL replay */
895 		return (0);
896 
897 	if ((zdp->z_phys->zp_mode & S_ISVTX) == 0 ||
898 	    (uid = crgetuid(cr)) == zdp->z_phys->zp_uid ||
899 	    uid == zp->z_phys->zp_uid ||
900 	    (ZTOV(zp)->v_type == VREG &&
901 	    zfs_zaccess(zp, ACE_WRITE_DATA, cr) == 0))
902 		return (0);
903 	else
904 		return (secpolicy_vnode_remove(cr));
905 }
906