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