xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_dir.c (revision 3897:278bade789ba)
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 2007 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 	rw_enter(&dzp->z_name_lock, RW_READER);
104 	mutex_enter(&dzp->z_lock);
105 	for (;;) {
106 		if (dzp->z_unlinked) {
107 			mutex_exit(&dzp->z_lock);
108 			rw_exit(&dzp->z_name_lock);
109 			return (ENOENT);
110 		}
111 		for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next)
112 			if (strcmp(name, dl->dl_name) == 0)
113 				break;
114 		if (dl == NULL)	{
115 			/*
116 			 * Allocate a new dirlock and add it to the list.
117 			 */
118 			dl = kmem_alloc(sizeof (zfs_dirlock_t), KM_SLEEP);
119 			cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL);
120 			dl->dl_name = name;
121 			dl->dl_sharecnt = 0;
122 			dl->dl_namesize = 0;
123 			dl->dl_dzp = dzp;
124 			dl->dl_next = dzp->z_dirlocks;
125 			dzp->z_dirlocks = dl;
126 			break;
127 		}
128 		if ((flag & ZSHARED) && dl->dl_sharecnt != 0)
129 			break;
130 		cv_wait(&dl->dl_cv, &dzp->z_lock);
131 	}
132 
133 	if ((flag & ZSHARED) && ++dl->dl_sharecnt > 1 && dl->dl_namesize == 0) {
134 		/*
135 		 * We're the second shared reference to dl.  Make a copy of
136 		 * dl_name in case the first thread goes away before we do.
137 		 * Note that we initialize the new name before storing its
138 		 * pointer into dl_name, because the first thread may load
139 		 * dl->dl_name at any time.  He'll either see the old value,
140 		 * which is his, or the new shared copy; either is OK.
141 		 */
142 		dl->dl_namesize = strlen(dl->dl_name) + 1;
143 		name = kmem_alloc(dl->dl_namesize, KM_SLEEP);
144 		bcopy(dl->dl_name, name, dl->dl_namesize);
145 		dl->dl_name = name;
146 	}
147 
148 	mutex_exit(&dzp->z_lock);
149 
150 	/*
151 	 * We have a dirlock on the name.  (Note that it is the dirlock,
152 	 * not the dzp's z_lock, that protects the name in the zap object.)
153 	 * See if there's an object by this name; if so, put a hold on it.
154 	 */
155 	if (flag & ZXATTR) {
156 		zoid = dzp->z_phys->zp_xattr;
157 		error = (zoid == 0 ? ENOENT : 0);
158 	} else {
159 		vp = dnlc_lookup(ZTOV(dzp), name);
160 		if (vp == DNLC_NO_VNODE) {
161 			VN_RELE(vp);
162 			error = ENOENT;
163 		} else if (vp) {
164 			if (flag & ZNEW) {
165 				zfs_dirent_unlock(dl);
166 				VN_RELE(vp);
167 				return (EEXIST);
168 			}
169 			*dlpp = dl;
170 			*zpp = VTOZ(vp);
171 			return (0);
172 		} else {
173 			error = zap_lookup(zfsvfs->z_os, dzp->z_id, name,
174 			    8, 1, &zoid);
175 			if (error == ENOENT)
176 				dnlc_update(ZTOV(dzp), name, DNLC_NO_VNODE);
177 		}
178 	}
179 	if (error) {
180 		if (error != ENOENT || (flag & ZEXISTS)) {
181 			zfs_dirent_unlock(dl);
182 			return (error);
183 		}
184 	} else {
185 		if (flag & ZNEW) {
186 			zfs_dirent_unlock(dl);
187 			return (EEXIST);
188 		}
189 		error = zfs_zget(zfsvfs, zoid, zpp);
190 		if (error) {
191 			zfs_dirent_unlock(dl);
192 			return (error);
193 		}
194 		if (!(flag & ZXATTR))
195 			dnlc_update(ZTOV(dzp), name, ZTOV(*zpp));
196 	}
197 
198 	*dlpp = dl;
199 
200 	return (0);
201 }
202 
203 /*
204  * Unlock this directory entry and wake anyone who was waiting for it.
205  */
206 void
207 zfs_dirent_unlock(zfs_dirlock_t *dl)
208 {
209 	znode_t *dzp = dl->dl_dzp;
210 	zfs_dirlock_t **prev_dl, *cur_dl;
211 
212 	mutex_enter(&dzp->z_lock);
213 	rw_exit(&dzp->z_name_lock);
214 	if (dl->dl_sharecnt > 1) {
215 		dl->dl_sharecnt--;
216 		mutex_exit(&dzp->z_lock);
217 		return;
218 	}
219 	prev_dl = &dzp->z_dirlocks;
220 	while ((cur_dl = *prev_dl) != dl)
221 		prev_dl = &cur_dl->dl_next;
222 	*prev_dl = dl->dl_next;
223 	cv_broadcast(&dl->dl_cv);
224 	mutex_exit(&dzp->z_lock);
225 
226 	if (dl->dl_namesize != 0)
227 		kmem_free(dl->dl_name, dl->dl_namesize);
228 	cv_destroy(&dl->dl_cv);
229 	kmem_free(dl, sizeof (*dl));
230 }
231 
232 /*
233  * Look up an entry in a directory.
234  *
235  * NOTE: '.' and '..' are handled as special cases because
236  *	no directory entries are actually stored for them.  If this is
237  *	the root of a filesystem, then '.zfs' is also treated as a
238  *	special pseudo-directory.
239  */
240 int
241 zfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp)
242 {
243 	zfs_dirlock_t *dl;
244 	znode_t *zp;
245 	int error = 0;
246 
247 	if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
248 		*vpp = ZTOV(dzp);
249 		VN_HOLD(*vpp);
250 	} else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
251 		zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
252 		/*
253 		 * If we are a snapshot mounted under .zfs, return
254 		 * the vp for the snapshot directory.
255 		 */
256 		if (dzp->z_phys->zp_parent == dzp->z_id &&
257 		    zfsvfs->z_parent != zfsvfs) {
258 			error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir,
259 			    "snapshot", vpp, NULL, 0, NULL, kcred);
260 			return (error);
261 		}
262 		rw_enter(&dzp->z_parent_lock, RW_READER);
263 		error = zfs_zget(zfsvfs, dzp->z_phys->zp_parent, &zp);
264 		if (error == 0)
265 			*vpp = ZTOV(zp);
266 		rw_exit(&dzp->z_parent_lock);
267 	} else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) {
268 		*vpp = zfsctl_root(dzp);
269 	} else {
270 		error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS | ZSHARED);
271 		if (error == 0) {
272 			*vpp = ZTOV(zp);
273 			zfs_dirent_unlock(dl);
274 			dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
275 		}
276 	}
277 
278 	return (error);
279 }
280 
281 static char *
282 zfs_unlinked_hexname(char namebuf[17], uint64_t x)
283 {
284 	char *name = &namebuf[16];
285 	const char digits[16] = "0123456789abcdef";
286 
287 	*name = '\0';
288 	do {
289 		*--name = digits[x & 0xf];
290 		x >>= 4;
291 	} while (x != 0);
292 
293 	return (name);
294 }
295 
296 /*
297  * unlinked Set (formerly known as the "delete queue") Error Handling
298  *
299  * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
300  * don't specify the name of the entry that we will be manipulating.  We
301  * also fib and say that we won't be adding any new entries to the
302  * unlinked set, even though we might (this is to lower the minimum file
303  * size that can be deleted in a full filesystem).  So on the small
304  * chance that the nlink list is using a fat zap (ie. has more than
305  * 2000 entries), we *may* not pre-read a block that's needed.
306  * Therefore it is remotely possible for some of the assertions
307  * regarding the unlinked set below to fail due to i/o error.  On a
308  * nondebug system, this will result in the space being leaked.
309  */
310 void
311 zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
312 {
313 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
314 	char obj_name[17];
315 	int error;
316 
317 	ASSERT(zp->z_unlinked);
318 	ASSERT3U(zp->z_phys->zp_links, ==, 0);
319 
320 	error = zap_add(zfsvfs->z_os, zfsvfs->z_unlinkedobj,
321 	    zfs_unlinked_hexname(obj_name, zp->z_id), 8, 1, &zp->z_id, tx);
322 	ASSERT3U(error, ==, 0);
323 }
324 
325 /*
326  * Clean up any znodes that had no links when we either crashed or
327  * (force) umounted the file system.
328  */
329 void
330 zfs_unlinked_drain(zfsvfs_t *zfsvfs)
331 {
332 	zap_cursor_t	zc;
333 	zap_attribute_t zap;
334 	dmu_object_info_t doi;
335 	znode_t		*zp;
336 	int		error;
337 
338 	/*
339 	 * Interate over the contents of the unlinked set.
340 	 */
341 	for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
342 	    zap_cursor_retrieve(&zc, &zap) == 0;
343 	    zap_cursor_advance(&zc)) {
344 
345 		/*
346 		 * See what kind of object we have in list
347 		 */
348 
349 		error = dmu_object_info(zfsvfs->z_os,
350 		    zap.za_first_integer, &doi);
351 		if (error != 0)
352 			continue;
353 
354 		ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
355 		    (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
356 		/*
357 		 * We need to re-mark these list entries for deletion,
358 		 * so we pull them back into core and set zp->z_unlinked.
359 		 */
360 		error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
361 
362 		/*
363 		 * We may pick up znodes that are already marked for deletion.
364 		 * This could happen during the purge of an extended attribute
365 		 * directory.  All we need to do is skip over them, since they
366 		 * are already in the system marked z_unlinked.
367 		 */
368 		if (error != 0)
369 			continue;
370 
371 		zp->z_unlinked = B_TRUE;
372 		VN_RELE(ZTOV(zp));
373 	}
374 	zap_cursor_fini(&zc);
375 }
376 
377 /*
378  * Delete the entire contents of a directory.  Return a count
379  * of the number of entries that could not be deleted.
380  *
381  * NOTE: this function assumes that the directory is inactive,
382  *	so there is no need to lock its entries before deletion.
383  *	Also, it assumes the directory contents is *only* regular
384  *	files.
385  */
386 static int
387 zfs_purgedir(znode_t *dzp)
388 {
389 	zap_cursor_t	zc;
390 	zap_attribute_t	zap;
391 	znode_t		*xzp;
392 	dmu_tx_t	*tx;
393 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
394 	zfs_dirlock_t	dl;
395 	int skipped = 0;
396 	int error;
397 
398 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
399 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
400 	    zap_cursor_advance(&zc)) {
401 		error = zfs_zget(zfsvfs, zap.za_first_integer, &xzp);
402 		ASSERT3U(error, ==, 0);
403 
404 		ASSERT((ZTOV(xzp)->v_type == VREG) ||
405 		    (ZTOV(xzp)->v_type == VLNK));
406 
407 		tx = dmu_tx_create(zfsvfs->z_os);
408 		dmu_tx_hold_bonus(tx, dzp->z_id);
409 		dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
410 		dmu_tx_hold_bonus(tx, xzp->z_id);
411 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
412 		error = dmu_tx_assign(tx, TXG_WAIT);
413 		if (error) {
414 			dmu_tx_abort(tx);
415 			VN_RELE(ZTOV(xzp));
416 			skipped += 1;
417 			continue;
418 		}
419 		bzero(&dl, sizeof (dl));
420 		dl.dl_dzp = dzp;
421 		dl.dl_name = zap.za_name;
422 
423 		error = zfs_link_destroy(&dl, xzp, tx, 0, NULL);
424 		ASSERT3U(error, ==, 0);
425 		dmu_tx_commit(tx);
426 
427 		VN_RELE(ZTOV(xzp));
428 	}
429 	zap_cursor_fini(&zc);
430 	ASSERT(error == ENOENT);
431 	return (skipped);
432 }
433 
434 void
435 zfs_rmnode(znode_t *zp)
436 {
437 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
438 	objset_t	*os = zfsvfs->z_os;
439 	znode_t		*xzp = NULL;
440 	char		obj_name[17];
441 	dmu_tx_t	*tx;
442 	uint64_t	acl_obj;
443 	int		error;
444 
445 	ASSERT(ZTOV(zp)->v_count == 0);
446 	ASSERT(zp->z_phys->zp_links == 0);
447 
448 	/*
449 	 * If this is an attribute directory, purge its contents.
450 	 */
451 	if (ZTOV(zp)->v_type == VDIR && (zp->z_phys->zp_flags & ZFS_XATTR)) {
452 		if (zfs_purgedir(zp) != 0) {
453 			/*
454 			 * Not enough space to delete some xattrs.
455 			 * Leave it on the unlinked set.
456 			 */
457 			return;
458 		}
459 	}
460 
461 	/*
462 	 * If the file has extended attributes, we're going to unlink
463 	 * the xattr dir.
464 	 */
465 	if (zp->z_phys->zp_xattr) {
466 		error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
467 		ASSERT(error == 0);
468 	}
469 
470 	acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
471 
472 	/*
473 	 * Set up the transaction.
474 	 */
475 	tx = dmu_tx_create(os);
476 	dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
477 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
478 	if (xzp) {
479 		dmu_tx_hold_bonus(tx, xzp->z_id);
480 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
481 	}
482 	if (acl_obj)
483 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
484 	error = dmu_tx_assign(tx, TXG_WAIT);
485 	if (error) {
486 		/*
487 		 * Not enough space to delete the file.  Leave it in the
488 		 * unlinked set, leaking it until the fs is remounted (at
489 		 * which point we'll call zfs_unlinked_drain() to process it).
490 		 */
491 		dmu_tx_abort(tx);
492 		return;
493 	}
494 
495 	if (xzp) {
496 		dmu_buf_will_dirty(xzp->z_dbuf, tx);
497 		mutex_enter(&xzp->z_lock);
498 		xzp->z_unlinked = B_TRUE;	/* mark xzp for deletion */
499 		xzp->z_phys->zp_links = 0;	/* no more links to it */
500 		mutex_exit(&xzp->z_lock);
501 		zfs_unlinked_add(xzp, tx);
502 	}
503 
504 	/* Remove this znode from the unlinked set */
505 	error = zap_remove(os, zfsvfs->z_unlinkedobj,
506 	    zfs_unlinked_hexname(obj_name, zp->z_id), tx);
507 	ASSERT3U(error, ==, 0);
508 
509 	zfs_znode_delete(zp, tx);
510 
511 	dmu_tx_commit(tx);
512 
513 	if (xzp)
514 		VN_RELE(ZTOV(xzp));
515 }
516 
517 /*
518  * Link zp into dl.  Can only fail if zp has been unlinked.
519  */
520 int
521 zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
522 {
523 	znode_t *dzp = dl->dl_dzp;
524 	vnode_t *vp = ZTOV(zp);
525 	int zp_is_dir = (vp->v_type == VDIR);
526 	int error;
527 
528 	dmu_buf_will_dirty(zp->z_dbuf, tx);
529 	mutex_enter(&zp->z_lock);
530 
531 	if (!(flag & ZRENAMING)) {
532 		if (zp->z_unlinked) {	/* no new links to unlinked zp */
533 			ASSERT(!(flag & (ZNEW | ZEXISTS)));
534 			mutex_exit(&zp->z_lock);
535 			return (ENOENT);
536 		}
537 		zp->z_phys->zp_links++;
538 	}
539 	zp->z_phys->zp_parent = dzp->z_id;	/* dzp is now zp's parent */
540 
541 	if (!(flag & ZNEW))
542 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
543 	mutex_exit(&zp->z_lock);
544 
545 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
546 	mutex_enter(&dzp->z_lock);
547 	dzp->z_phys->zp_size++;			/* one dirent added */
548 	dzp->z_phys->zp_links += zp_is_dir;	/* ".." link from zp */
549 	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
550 	mutex_exit(&dzp->z_lock);
551 
552 	error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
553 	    8, 1, &zp->z_id, tx);
554 	ASSERT(error == 0);
555 
556 	dnlc_update(ZTOV(dzp), dl->dl_name, vp);
557 
558 	return (0);
559 }
560 
561 /*
562  * Unlink zp from dl, and mark zp for deletion if this was the last link.
563  * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
564  * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
565  * If it's non-NULL, we use it to indicate whether the znode needs deletion,
566  * and it's the caller's job to do it.
567  */
568 int
569 zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
570 	boolean_t *unlinkedp)
571 {
572 	znode_t *dzp = dl->dl_dzp;
573 	vnode_t *vp = ZTOV(zp);
574 	int zp_is_dir = (vp->v_type == VDIR);
575 	boolean_t unlinked = B_FALSE;
576 	int error;
577 
578 	dnlc_remove(ZTOV(dzp), dl->dl_name);
579 
580 	if (!(flag & ZRENAMING)) {
581 		dmu_buf_will_dirty(zp->z_dbuf, tx);
582 
583 		if (vn_vfswlock(vp))		/* prevent new mounts on zp */
584 			return (EBUSY);
585 
586 		if (vn_ismntpt(vp)) {		/* don't remove mount point */
587 			vn_vfsunlock(vp);
588 			return (EBUSY);
589 		}
590 
591 		mutex_enter(&zp->z_lock);
592 		if (zp_is_dir && !zfs_dirempty(zp)) {	/* dir not empty */
593 			mutex_exit(&zp->z_lock);
594 			vn_vfsunlock(vp);
595 			return (EEXIST);
596 		}
597 		if (zp->z_phys->zp_links <= zp_is_dir) {
598 			zfs_panic_recover("zfs: link count on %s is %u, "
599 			    "should be at least %u",
600 			    zp->z_vnode->v_path ? zp->z_vnode->v_path :
601 			    "<unknown>", (int)zp->z_phys->zp_links,
602 			    zp_is_dir + 1);
603 			zp->z_phys->zp_links = zp_is_dir + 1;
604 		}
605 		if (--zp->z_phys->zp_links == zp_is_dir) {
606 			zp->z_unlinked = B_TRUE;
607 			zp->z_phys->zp_links = 0;
608 			unlinked = B_TRUE;
609 		} else {
610 			zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
611 		}
612 		mutex_exit(&zp->z_lock);
613 		vn_vfsunlock(vp);
614 	}
615 
616 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
617 	mutex_enter(&dzp->z_lock);
618 	dzp->z_phys->zp_size--;			/* one dirent removed */
619 	dzp->z_phys->zp_links -= zp_is_dir;	/* ".." link from zp */
620 	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
621 	mutex_exit(&dzp->z_lock);
622 
623 	error = zap_remove(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name, tx);
624 	ASSERT(error == 0);
625 
626 	if (unlinkedp != NULL)
627 		*unlinkedp = unlinked;
628 	else if (unlinked)
629 		zfs_unlinked_add(zp, tx);
630 
631 	return (0);
632 }
633 
634 /*
635  * Indicate whether the directory is empty.  Works with or without z_lock
636  * held, but can only be consider a hint in the latter case.  Returns true
637  * if only "." and ".." remain and there's no work in progress.
638  */
639 boolean_t
640 zfs_dirempty(znode_t *dzp)
641 {
642 	return (dzp->z_phys->zp_size == 2 && dzp->z_dirlocks == 0);
643 }
644 
645 int
646 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
647 {
648 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
649 	znode_t *xzp;
650 	dmu_tx_t *tx;
651 	uint64_t xoid;
652 	int error;
653 
654 	*xvpp = NULL;
655 
656 	if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, cr))
657 		return (error);
658 
659 	tx = dmu_tx_create(zfsvfs->z_os);
660 	dmu_tx_hold_bonus(tx, zp->z_id);
661 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
662 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
663 	if (error) {
664 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT)
665 			dmu_tx_wait(tx);
666 		dmu_tx_abort(tx);
667 		return (error);
668 	}
669 	zfs_mknode(zp, vap, &xoid, tx, cr, IS_XATTR, &xzp, 0);
670 	ASSERT(xzp->z_id == xoid);
671 	ASSERT(xzp->z_phys->zp_parent == zp->z_id);
672 	dmu_buf_will_dirty(zp->z_dbuf, tx);
673 	zp->z_phys->zp_xattr = xoid;
674 
675 	(void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp, xzp, "");
676 	dmu_tx_commit(tx);
677 
678 	*xvpp = ZTOV(xzp);
679 
680 	return (0);
681 }
682 
683 /*
684  * Return a znode for the extended attribute directory for zp.
685  * ** If the directory does not already exist, it is created **
686  *
687  *	IN:	zp	- znode to obtain attribute directory from
688  *		cr	- credentials of caller
689  *		flags	- flags from the VOP_LOOKUP call
690  *
691  *	OUT:	xzpp	- pointer to extended attribute znode
692  *
693  *	RETURN:	0 on success
694  *		error number on failure
695  */
696 int
697 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
698 {
699 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
700 	znode_t		*xzp;
701 	zfs_dirlock_t	*dl;
702 	vattr_t		va;
703 	int		error;
704 top:
705 	error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR);
706 	if (error)
707 		return (error);
708 
709 	if (xzp != NULL) {
710 		*xvpp = ZTOV(xzp);
711 		zfs_dirent_unlock(dl);
712 		return (0);
713 	}
714 
715 	ASSERT(zp->z_phys->zp_xattr == 0);
716 
717 	if (!(flags & CREATE_XATTR_DIR)) {
718 		zfs_dirent_unlock(dl);
719 		return (ENOENT);
720 	}
721 
722 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
723 		zfs_dirent_unlock(dl);
724 		return (EROFS);
725 	}
726 
727 	/*
728 	 * The ability to 'create' files in an attribute
729 	 * directory comes from the write_xattr permission on the base file.
730 	 *
731 	 * The ability to 'search' an attribute directory requires
732 	 * read_xattr permission on the base file.
733 	 *
734 	 * Once in a directory the ability to read/write attributes
735 	 * is controlled by the permissions on the attribute file.
736 	 */
737 	va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
738 	va.va_type = VDIR;
739 	va.va_mode = S_IFDIR | S_ISVTX | 0777;
740 	va.va_uid = (uid_t)zp->z_phys->zp_uid;
741 	va.va_gid = (gid_t)zp->z_phys->zp_gid;
742 
743 	error = zfs_make_xattrdir(zp, &va, xvpp, cr);
744 	zfs_dirent_unlock(dl);
745 
746 	if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
747 		/* NB: we already did dmu_tx_wait() if necessary */
748 		goto top;
749 	}
750 
751 	return (error);
752 }
753 
754 /*
755  * Decide whether it is okay to remove within a sticky directory.
756  *
757  * In sticky directories, write access is not sufficient;
758  * you can remove entries from a directory only if:
759  *
760  *	you own the directory,
761  *	you own the entry,
762  *	the entry is a plain file and you have write access,
763  *	or you are privileged (checked in secpolicy...).
764  *
765  * The function returns 0 if remove access is granted.
766  */
767 int
768 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
769 {
770 	uid_t  		uid;
771 
772 	if (zdp->z_zfsvfs->z_assign >= TXG_INITIAL)	/* ZIL replay */
773 		return (0);
774 
775 	if ((zdp->z_phys->zp_mode & S_ISVTX) == 0 ||
776 	    (uid = crgetuid(cr)) == zdp->z_phys->zp_uid ||
777 	    uid == zp->z_phys->zp_uid ||
778 	    (ZTOV(zp)->v_type == VREG &&
779 	    zfs_zaccess(zp, ACE_WRITE_DATA, cr) == 0))
780 		return (0);
781 	else
782 		return (secpolicy_vnode_remove(cr));
783 }
784