xref: /titanic_41/usr/src/uts/common/fs/zfs/zfs_dir.c (revision 5a59a8b3d86e67dbe75588879c46e3629f40efec)
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 (zfsvfs->z_parent != zfsvfs) {
254 			error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir,
255 			    "snapshot", vpp, NULL, 0, NULL, kcred);
256 			return (error);
257 		}
258 		rw_enter(&dzp->z_parent_lock, RW_READER);
259 		error = zfs_zget(zfsvfs, dzp->z_phys->zp_parent, &zp);
260 		if (error == 0)
261 			*vpp = ZTOV(zp);
262 		rw_exit(&dzp->z_parent_lock);
263 	} else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) {
264 		*vpp = zfsctl_root(dzp);
265 	} else {
266 		error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS | ZSHARED);
267 		if (error == 0) {
268 			*vpp = ZTOV(zp);
269 			zfs_dirent_unlock(dl);
270 			dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
271 		}
272 	}
273 
274 	return (error);
275 }
276 
277 static char *
278 zfs_dq_hexname(char namebuf[17], uint64_t x)
279 {
280 	char *name = &namebuf[16];
281 	const char digits[16] = "0123456789abcdef";
282 
283 	*name = '\0';
284 	do {
285 		*--name = digits[x & 0xf];
286 		x >>= 4;
287 	} while (x != 0);
288 
289 	return (name);
290 }
291 
292 void
293 zfs_dq_add(znode_t *zp, dmu_tx_t *tx)
294 {
295 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
296 	char obj_name[17];
297 	int error;
298 
299 	ASSERT(zp->z_reap);
300 	ASSERT3U(zp->z_phys->zp_links, ==, 0);
301 
302 	error = zap_add(zfsvfs->z_os, zfsvfs->z_dqueue,
303 	    zfs_dq_hexname(obj_name, zp->z_id), 8, 1, &zp->z_id, tx);
304 	ASSERT3U(error, ==, 0);
305 }
306 
307 /*
308  * Delete the entire contents of a directory.  Return a count
309  * of the number of entries that could not be deleted.
310  *
311  * NOTE: this function assumes that the directory is inactive,
312  *	so there is no need to lock its entries before deletion.
313  *	Also, it assumes the directory contents is *only* regular
314  *	files.
315  */
316 static int
317 zfs_purgedir(znode_t *dzp)
318 {
319 	zap_cursor_t	zc;
320 	zap_attribute_t	zap;
321 	znode_t		*xzp;
322 	dmu_tx_t	*tx;
323 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
324 	zfs_dirlock_t	dl;
325 	int skipped = 0;
326 	int error;
327 
328 	ASSERT(dzp->z_active == 0);
329 
330 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
331 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
332 	    zap_cursor_advance(&zc)) {
333 		error = zfs_zget(zfsvfs, zap.za_first_integer, &xzp);
334 		ASSERT3U(error, ==, 0);
335 
336 		ASSERT((ZTOV(xzp)->v_type == VREG) ||
337 		    (ZTOV(xzp)->v_type == VLNK));
338 
339 		tx = dmu_tx_create(zfsvfs->z_os);
340 		dmu_tx_hold_bonus(tx, dzp->z_id);
341 		dmu_tx_hold_zap(tx, dzp->z_id, -1);
342 		dmu_tx_hold_bonus(tx, xzp->z_id);
343 		dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, 1);
344 		error = dmu_tx_assign(tx, TXG_WAIT);
345 		if (error) {
346 			dmu_tx_abort(tx);
347 			VN_RELE(ZTOV(xzp));
348 			skipped += 1;
349 			continue;
350 		}
351 		bzero(&dl, sizeof (dl));
352 		dl.dl_dzp = dzp;
353 		dl.dl_name = zap.za_name;
354 
355 		error = zfs_link_destroy(&dl, xzp, tx, 0, NULL);
356 		ASSERT3U(error, ==, 0);
357 		dmu_tx_commit(tx);
358 
359 		VN_RELE(ZTOV(xzp));
360 	}
361 	zap_cursor_fini(&zc);
362 	ASSERT(error == ENOENT);
363 	return (skipped);
364 }
365 
366 /*
367  * Special function to requeue the znodes for deletion that were
368  * in progress when we either crashed or umounted the file system.
369  */
370 static void
371 zfs_drain_dq(zfsvfs_t *zfsvfs)
372 {
373 	zap_cursor_t	zc;
374 	zap_attribute_t zap;
375 	dmu_object_info_t doi;
376 	znode_t		*zp;
377 	int		error;
378 
379 	/*
380 	 * Interate over the contents of the delete queue.
381 	 */
382 	for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_dqueue);
383 	    zap_cursor_retrieve(&zc, &zap) == 0;
384 	    zap_cursor_advance(&zc)) {
385 
386 		/*
387 		 * Need some helpers?
388 		 */
389 		if (zfs_delete_thread_target(zfsvfs, -1) != 0)
390 			return;
391 
392 		/*
393 		 * See what kind of object we have in queue
394 		 */
395 
396 		error = dmu_object_info(zfsvfs->z_os,
397 		    zap.za_first_integer, &doi);
398 		if (error != 0)
399 			continue;
400 
401 		ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
402 		    (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
403 		/*
404 		 * We need to re-mark these queue entries for reaping,
405 		 * so we pull them back into core and set zp->z_reap.
406 		 */
407 		error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
408 
409 		/*
410 		 * We may pick up znodes that are already marked for reaping.
411 		 * This could happen during the purge of an extended attribute
412 		 * directory.  All we need to do is skip over them, since they
413 		 * are already in the system to be processed by the taskq.
414 		 */
415 		if (error != 0) {
416 			continue;
417 		}
418 		zp->z_reap = 1;
419 		VN_RELE(ZTOV(zp));
420 		break;
421 	}
422 	zap_cursor_fini(&zc);
423 }
424 
425 void
426 zfs_delete_thread(void *arg)
427 {
428 	zfsvfs_t	*zfsvfs = arg;
429 	zfs_delete_t 	*zd = &zfsvfs->z_delete_head;
430 	znode_t		*zp;
431 	callb_cpr_t	cprinfo;
432 
433 	CALLB_CPR_INIT(&cprinfo, &zd->z_mutex, callb_generic_cpr, "zfs_delete");
434 
435 	mutex_enter(&zd->z_mutex);
436 
437 	if (!zd->z_drained && !zd->z_draining) {
438 		zd->z_draining = B_TRUE;
439 		mutex_exit(&zd->z_mutex);
440 		zfs_drain_dq(zfsvfs);
441 		mutex_enter(&zd->z_mutex);
442 		zd->z_draining = B_FALSE;
443 		zd->z_drained = B_TRUE;
444 		cv_broadcast(&zd->z_quiesce_cv);
445 	}
446 
447 	while (zd->z_thread_count <= zd->z_thread_target) {
448 		zp = list_head(&zd->z_znodes);
449 		if (zp == NULL) {
450 			ASSERT(zd->z_znode_count == 0);
451 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
452 			cv_wait(&zd->z_cv, &zd->z_mutex);
453 			CALLB_CPR_SAFE_END(&cprinfo, &zd->z_mutex);
454 			continue;
455 		}
456 		ASSERT(zd->z_znode_count != 0);
457 		list_remove(&zd->z_znodes, zp);
458 		if (--zd->z_znode_count == 0)
459 			cv_broadcast(&zd->z_quiesce_cv);
460 		mutex_exit(&zd->z_mutex);
461 		zfs_rmnode(zp);
462 		(void) zfs_delete_thread_target(zfsvfs, -1);
463 		mutex_enter(&zd->z_mutex);
464 	}
465 
466 	ASSERT(zd->z_thread_count != 0);
467 	if (--zd->z_thread_count == 0)
468 		cv_broadcast(&zd->z_cv);
469 
470 	CALLB_CPR_EXIT(&cprinfo);	/* NB: drops z_mutex */
471 	thread_exit();
472 }
473 
474 static int zfs_work_per_thread_shift = 11;	/* 2048 (2^11) per thread */
475 
476 /*
477  * Set the target number of delete threads to 'nthreads'.
478  * If nthreads == -1, choose a number based on current workload.
479  * If nthreads == 0, don't return until the threads have exited.
480  */
481 int
482 zfs_delete_thread_target(zfsvfs_t *zfsvfs, int nthreads)
483 {
484 	zfs_delete_t *zd = &zfsvfs->z_delete_head;
485 
486 	mutex_enter(&zd->z_mutex);
487 
488 	if (nthreads == -1) {
489 		if (zd->z_thread_target == 0) {
490 			mutex_exit(&zd->z_mutex);
491 			return (EBUSY);
492 		}
493 		nthreads = zd->z_znode_count >> zfs_work_per_thread_shift;
494 		nthreads = MIN(nthreads, ncpus << 1);
495 		nthreads = MAX(nthreads, 1);
496 		nthreads += !!zd->z_draining;
497 	}
498 
499 	zd->z_thread_target = nthreads;
500 
501 	while (zd->z_thread_count < zd->z_thread_target) {
502 		(void) thread_create(NULL, 0, zfs_delete_thread, zfsvfs,
503 		    0, &p0, TS_RUN, minclsyspri);
504 		zd->z_thread_count++;
505 	}
506 
507 	while (zd->z_thread_count > zd->z_thread_target && nthreads == 0) {
508 		cv_broadcast(&zd->z_cv);
509 		cv_wait(&zd->z_cv, &zd->z_mutex);
510 	}
511 
512 	mutex_exit(&zd->z_mutex);
513 
514 	return (0);
515 }
516 
517 /*
518  * Wait until everything that's been queued has been deleted.
519  */
520 void
521 zfs_delete_wait_empty(zfsvfs_t *zfsvfs)
522 {
523 	zfs_delete_t *zd = &zfsvfs->z_delete_head;
524 
525 	mutex_enter(&zd->z_mutex);
526 	ASSERT(zd->z_thread_target != 0);
527 	while (!zd->z_drained || zd->z_znode_count != 0) {
528 		ASSERT(zd->z_thread_target != 0);
529 		cv_wait(&zd->z_quiesce_cv, &zd->z_mutex);
530 	}
531 	mutex_exit(&zd->z_mutex);
532 }
533 
534 void
535 zfs_rmnode(znode_t *zp)
536 {
537 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
538 	objset_t	*os = zfsvfs->z_os;
539 	znode_t		*xzp = NULL;
540 	char		obj_name[17];
541 	dmu_tx_t	*tx;
542 	uint64_t	acl_obj;
543 	int		error;
544 
545 	ASSERT(zp->z_active == 0);
546 	ASSERT(ZTOV(zp)->v_count == 0);
547 	ASSERT(zp->z_phys->zp_links == 0);
548 
549 	/*
550 	 * If this is an attribute directory, purge its contents.
551 	 */
552 	if (ZTOV(zp)->v_type == VDIR && (zp->z_phys->zp_flags & ZFS_XATTR))
553 		if (zfs_purgedir(zp) != 0) {
554 			zfs_delete_t *delq = &zfsvfs->z_delete_head;
555 			/*
556 			 * Add this back to the delete list to be retried later.
557 			 *
558 			 * XXX - this could just busy loop on us...
559 			 */
560 			mutex_enter(&delq->z_mutex);
561 			list_insert_tail(&delq->z_znodes, zp);
562 			delq->z_znode_count++;
563 			mutex_exit(&delq->z_mutex);
564 			return;
565 		}
566 
567 	/*
568 	 * If the file has extended attributes, unlink the xattr dir.
569 	 */
570 	if (zp->z_phys->zp_xattr) {
571 		error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
572 		ASSERT(error == 0);
573 	}
574 
575 	acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
576 
577 	/*
578 	 * Set up the transaction.
579 	 */
580 	tx = dmu_tx_create(os);
581 	dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
582 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, -1);
583 	if (xzp) {
584 		dmu_tx_hold_bonus(tx, xzp->z_id);
585 		dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, 1);
586 	}
587 	if (acl_obj)
588 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
589 	error = dmu_tx_assign(tx, TXG_WAIT);
590 	if (error) {
591 		zfs_delete_t *delq = &zfsvfs->z_delete_head;
592 
593 		dmu_tx_abort(tx);
594 		/*
595 		 * Add this back to the delete list to be retried later.
596 		 *
597 		 * XXX - this could just busy loop on us...
598 		 */
599 		mutex_enter(&delq->z_mutex);
600 		list_insert_tail(&delq->z_znodes, zp);
601 		delq->z_znode_count++;
602 		mutex_exit(&delq->z_mutex);
603 		return;
604 	}
605 
606 	if (xzp) {
607 		dmu_buf_will_dirty(xzp->z_dbuf, tx);
608 		mutex_enter(&xzp->z_lock);
609 		xzp->z_reap = 1;		/* mark xzp for deletion */
610 		xzp->z_phys->zp_links = 0;	/* no more links to it */
611 		mutex_exit(&xzp->z_lock);
612 		zfs_dq_add(xzp, tx);		/* add xzp to delete queue */
613 	}
614 
615 	/*
616 	 * Remove this znode from delete queue
617 	 */
618 	error = zap_remove(os, zfsvfs->z_dqueue,
619 	    zfs_dq_hexname(obj_name, zp->z_id), tx);
620 	ASSERT3U(error, ==, 0);
621 
622 	zfs_znode_delete(zp, tx);
623 
624 	dmu_tx_commit(tx);
625 
626 	if (xzp)
627 		VN_RELE(ZTOV(xzp));
628 }
629 
630 /*
631  * Link zp into dl.  Can only fail if zp has been reaped.
632  */
633 int
634 zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
635 {
636 	znode_t *dzp = dl->dl_dzp;
637 	vnode_t *vp = ZTOV(zp);
638 	int zp_is_dir = (vp->v_type == VDIR);
639 	int error;
640 
641 	dmu_buf_will_dirty(zp->z_dbuf, tx);
642 	mutex_enter(&zp->z_lock);
643 
644 	if (!(flag & ZRENAMING)) {
645 		if (zp->z_reap) {	/* no new links to reaped zp */
646 			ASSERT(!(flag & (ZNEW | ZEXISTS)));
647 			mutex_exit(&zp->z_lock);
648 			return (ENOENT);
649 		}
650 		zp->z_phys->zp_links++;
651 	}
652 	zp->z_phys->zp_parent = dzp->z_id;	/* dzp is now zp's parent */
653 
654 	if (!(flag & ZNEW))
655 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
656 	mutex_exit(&zp->z_lock);
657 
658 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
659 	mutex_enter(&dzp->z_lock);
660 	dzp->z_phys->zp_size++;			/* one dirent added */
661 	dzp->z_phys->zp_links += zp_is_dir;	/* ".." link from zp */
662 	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
663 	mutex_exit(&dzp->z_lock);
664 
665 	error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
666 	    8, 1, &zp->z_id, tx);
667 	ASSERT(error == 0);
668 
669 	dnlc_update(ZTOV(dzp), dl->dl_name, vp);
670 
671 	return (0);
672 }
673 
674 /*
675  * Unlink zp from dl, and mark zp for reaping if this was the last link.
676  * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
677  * If 'reaped_ptr' is NULL, we put reaped znodes on the delete queue.
678  * If it's non-NULL, we use it to indicate whether the znode needs reaping,
679  * and it's the caller's job to do it.
680  */
681 int
682 zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
683 	int *reaped_ptr)
684 {
685 	znode_t *dzp = dl->dl_dzp;
686 	vnode_t *vp = ZTOV(zp);
687 	int zp_is_dir = (vp->v_type == VDIR);
688 	int reaped = 0;
689 	int error;
690 
691 	dnlc_remove(ZTOV(dzp), dl->dl_name);
692 
693 	if (!(flag & ZRENAMING)) {
694 		dmu_buf_will_dirty(zp->z_dbuf, tx);
695 
696 		if (vn_vfswlock(vp))		/* prevent new mounts on zp */
697 			return (EBUSY);
698 
699 		if (vn_ismntpt(vp)) {		/* don't remove mount point */
700 			vn_vfsunlock(vp);
701 			return (EBUSY);
702 		}
703 
704 		mutex_enter(&zp->z_lock);
705 		if (zp_is_dir && !zfs_dirempty(zp)) {	/* dir not empty */
706 			mutex_exit(&zp->z_lock);
707 			vn_vfsunlock(vp);
708 			return (EEXIST);
709 		}
710 		ASSERT(zp->z_phys->zp_links > zp_is_dir);
711 		if (--zp->z_phys->zp_links == zp_is_dir) {
712 			zp->z_reap = 1;
713 			zp->z_phys->zp_links = 0;
714 			reaped = 1;
715 		} else {
716 			zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
717 		}
718 		mutex_exit(&zp->z_lock);
719 		vn_vfsunlock(vp);
720 	}
721 
722 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
723 	mutex_enter(&dzp->z_lock);
724 	dzp->z_phys->zp_size--;			/* one dirent removed */
725 	dzp->z_phys->zp_links -= zp_is_dir;	/* ".." link from zp */
726 	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
727 	mutex_exit(&dzp->z_lock);
728 
729 	error = zap_remove(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name, tx);
730 	ASSERT(error == 0);
731 
732 	if (reaped_ptr != NULL)
733 		*reaped_ptr = reaped;
734 	else if (reaped)
735 		zfs_dq_add(zp, tx);
736 
737 	return (0);
738 }
739 
740 /*
741  * Indicate whether the directory is empty.  Works with or without z_lock
742  * held, but can only be consider a hint in the latter case.  Returns true
743  * if only "." and ".." remain and there's no work in progress.
744  */
745 boolean_t
746 zfs_dirempty(znode_t *dzp)
747 {
748 	return (dzp->z_phys->zp_size == 2 && dzp->z_dirlocks == 0);
749 }
750 
751 int
752 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
753 {
754 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
755 	znode_t *xzp;
756 	dmu_tx_t *tx;
757 	uint64_t xoid;
758 	int error;
759 
760 	*xvpp = NULL;
761 
762 	if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, cr))
763 		return (error);
764 
765 	tx = dmu_tx_create(zfsvfs->z_os);
766 	dmu_tx_hold_bonus(tx, zp->z_id);
767 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, 0);
768 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
769 	if (error) {
770 		dmu_tx_abort(tx);
771 		return (error);
772 	}
773 	zfs_mknode(zp, vap, &xoid, tx, cr, IS_XATTR, &xzp, 0);
774 	ASSERT(xzp->z_id == xoid);
775 	ASSERT(xzp->z_phys->zp_parent == zp->z_id);
776 	dmu_buf_will_dirty(zp->z_dbuf, tx);
777 	zp->z_phys->zp_xattr = xoid;
778 
779 	(void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp, xzp, "");
780 	dmu_tx_commit(tx);
781 
782 	*xvpp = ZTOV(xzp);
783 
784 	return (0);
785 }
786 
787 /*
788  * Return a znode for the extended attribute directory for zp.
789  * ** If the directory does not already exist, it is created **
790  *
791  *	IN:	zp	- znode to obtain attribute directory from
792  *		cr	- credentials of caller
793  *
794  *	OUT:	xzpp	- pointer to extended attribute znode
795  *
796  *	RETURN:	0 on success
797  *		error number on failure
798  */
799 int
800 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr)
801 {
802 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
803 	znode_t		*xzp;
804 	zfs_dirlock_t	*dl;
805 	vattr_t		va;
806 	int		error;
807 top:
808 	error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR);
809 	if (error)
810 		return (error);
811 
812 	if (xzp != NULL) {
813 		*xvpp = ZTOV(xzp);
814 		zfs_dirent_unlock(dl);
815 		return (0);
816 	}
817 
818 	ASSERT(zp->z_phys->zp_xattr == 0);
819 
820 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
821 		zfs_dirent_unlock(dl);
822 		return (EROFS);
823 	}
824 
825 	/*
826 	 * The ability to 'create' files in an attribute
827 	 * directory comes from the write_xattr permission on the base file.
828 	 *
829 	 * The ability to 'search' an attribute directory requires
830 	 * read_xattr permission on the base file.
831 	 *
832 	 * Once in a directory the ability to read/write attributes
833 	 * is controlled by the permissions on the attribute file.
834 	 */
835 	va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
836 	va.va_type = VDIR;
837 	va.va_mode = S_IFDIR | S_ISVTX | 0777;
838 	va.va_uid = (uid_t)zp->z_phys->zp_uid;
839 	va.va_gid = (gid_t)zp->z_phys->zp_gid;
840 
841 	error = zfs_make_xattrdir(zp, &va, xvpp, cr);
842 	zfs_dirent_unlock(dl);
843 
844 	if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
845 		txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0);
846 		goto top;
847 	}
848 
849 	return (error);
850 }
851 
852 /*
853  * Decide whether it is okay to remove within a sticky directory.
854  *
855  * In sticky directories, write access is not sufficient;
856  * you can remove entries from a directory only if:
857  *
858  *	you own the directory,
859  *	you own the entry,
860  *	the entry is a plain file and you have write access,
861  *	or you are privileged (checked in secpolicy...).
862  *
863  * The function returns 0 if remove access is granted.
864  */
865 int
866 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
867 {
868 	uid_t  		uid;
869 
870 	if (zdp->z_zfsvfs->z_assign >= TXG_INITIAL)	/* ZIL replay */
871 		return (0);
872 
873 	if ((zdp->z_phys->zp_mode & S_ISVTX) == 0 ||
874 	    (uid = crgetuid(cr)) == zdp->z_phys->zp_uid ||
875 	    uid == zp->z_phys->zp_uid ||
876 	    (ZTOV(zp)->v_type == VREG &&
877 	    zfs_zaccess(zp, ACE_WRITE_DATA, cr) == 0))
878 		return (0);
879 	else
880 		return (secpolicy_vnode_remove(cr));
881 }
882