xref: /titanic_50/usr/src/uts/common/fs/zfs/zfs_dir.c (revision c2cb63342c63c60cee771d1af82f377e34aa1217)
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 	mutex_enter(&dzp->z_lock);
104 	for (;;) {
105 		if (dzp->z_unlinked) {
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_unlinked_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  * unlinked Set (formerly known as the "delete queue") Error Handling
295  *
296  * When dealing with the unlinked set, 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  * unlinked set, 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 nlink list 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 unlinked set below to fail due to i/o error.  On a
305  * nondebug system, this will result in the space being leaked.
306  */
307 void
308 zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
309 {
310 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
311 	char obj_name[17];
312 	int error;
313 
314 	ASSERT(zp->z_unlinked);
315 	ASSERT3U(zp->z_phys->zp_links, ==, 0);
316 
317 	error = zap_add(zfsvfs->z_os, zfsvfs->z_unlinkedobj,
318 	    zfs_unlinked_hexname(obj_name, zp->z_id), 8, 1, &zp->z_id, tx);
319 	ASSERT3U(error, ==, 0);
320 }
321 
322 /*
323  * Clean up any znodes that had no links when we either crashed or
324  * (force) umounted the file system.
325  */
326 void
327 zfs_unlinked_drain(zfsvfs_t *zfsvfs)
328 {
329 	zap_cursor_t	zc;
330 	zap_attribute_t zap;
331 	dmu_object_info_t doi;
332 	znode_t		*zp;
333 	int		error;
334 
335 	/*
336 	 * Interate over the contents of the unlinked set.
337 	 */
338 	for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
339 	    zap_cursor_retrieve(&zc, &zap) == 0;
340 	    zap_cursor_advance(&zc)) {
341 
342 		/*
343 		 * See what kind of object we have in list
344 		 */
345 
346 		error = dmu_object_info(zfsvfs->z_os,
347 		    zap.za_first_integer, &doi);
348 		if (error != 0)
349 			continue;
350 
351 		ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
352 		    (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
353 		/*
354 		 * We need to re-mark these list entries for deletion,
355 		 * so we pull them back into core and set zp->z_unlinked.
356 		 */
357 		error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
358 
359 		/*
360 		 * We may pick up znodes that are already marked for deletion.
361 		 * This could happen during the purge of an extended attribute
362 		 * directory.  All we need to do is skip over them, since they
363 		 * are already in the system marked z_unlinked.
364 		 */
365 		if (error != 0)
366 			continue;
367 
368 		zp->z_unlinked = B_TRUE;
369 		VN_RELE(ZTOV(zp));
370 	}
371 	zap_cursor_fini(&zc);
372 }
373 
374 /*
375  * Delete the entire contents of a directory.  Return a count
376  * of the number of entries that could not be deleted.
377  *
378  * NOTE: this function assumes that the directory is inactive,
379  *	so there is no need to lock its entries before deletion.
380  *	Also, it assumes the directory contents is *only* regular
381  *	files.
382  */
383 static int
384 zfs_purgedir(znode_t *dzp)
385 {
386 	zap_cursor_t	zc;
387 	zap_attribute_t	zap;
388 	znode_t		*xzp;
389 	dmu_tx_t	*tx;
390 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
391 	zfs_dirlock_t	dl;
392 	int skipped = 0;
393 	int error;
394 
395 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
396 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
397 	    zap_cursor_advance(&zc)) {
398 		error = zfs_zget(zfsvfs, zap.za_first_integer, &xzp);
399 		ASSERT3U(error, ==, 0);
400 
401 		ASSERT((ZTOV(xzp)->v_type == VREG) ||
402 		    (ZTOV(xzp)->v_type == VLNK));
403 
404 		tx = dmu_tx_create(zfsvfs->z_os);
405 		dmu_tx_hold_bonus(tx, dzp->z_id);
406 		dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
407 		dmu_tx_hold_bonus(tx, xzp->z_id);
408 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
409 		error = dmu_tx_assign(tx, TXG_WAIT);
410 		if (error) {
411 			dmu_tx_abort(tx);
412 			VN_RELE(ZTOV(xzp));
413 			skipped += 1;
414 			continue;
415 		}
416 		bzero(&dl, sizeof (dl));
417 		dl.dl_dzp = dzp;
418 		dl.dl_name = zap.za_name;
419 
420 		error = zfs_link_destroy(&dl, xzp, tx, 0, NULL);
421 		ASSERT3U(error, ==, 0);
422 		dmu_tx_commit(tx);
423 
424 		VN_RELE(ZTOV(xzp));
425 	}
426 	zap_cursor_fini(&zc);
427 	ASSERT(error == ENOENT);
428 	return (skipped);
429 }
430 
431 void
432 zfs_rmnode(znode_t *zp)
433 {
434 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
435 	objset_t	*os = zfsvfs->z_os;
436 	znode_t		*xzp = NULL;
437 	char		obj_name[17];
438 	dmu_tx_t	*tx;
439 	uint64_t	acl_obj;
440 	int		error;
441 
442 	ASSERT(ZTOV(zp)->v_count == 0);
443 	ASSERT(zp->z_phys->zp_links == 0);
444 
445 	/*
446 	 * If this is an attribute directory, purge its contents.
447 	 */
448 	if (ZTOV(zp)->v_type == VDIR && (zp->z_phys->zp_flags & ZFS_XATTR)) {
449 		if (zfs_purgedir(zp) != 0) {
450 			/*
451 			 * Not enough space to delete some xattrs.
452 			 * Leave it on the unlinked set.
453 			 */
454 			return;
455 		}
456 	}
457 
458 	/*
459 	 * If the file has extended attributes, we're going to unlink
460 	 * the xattr dir.
461 	 */
462 	if (zp->z_phys->zp_xattr) {
463 		error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
464 		ASSERT(error == 0);
465 	}
466 
467 	acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
468 
469 	/*
470 	 * Set up the transaction.
471 	 */
472 	tx = dmu_tx_create(os);
473 	dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
474 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
475 	if (xzp) {
476 		dmu_tx_hold_bonus(tx, xzp->z_id);
477 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
478 	}
479 	if (acl_obj)
480 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
481 	error = dmu_tx_assign(tx, TXG_WAIT);
482 	if (error) {
483 		/*
484 		 * Not enough space to delete the file.  Leave it in the
485 		 * unlinked set, leaking it until the fs is remounted (at
486 		 * which point we'll call zfs_unlinked_drain() to process it).
487 		 */
488 		dmu_tx_abort(tx);
489 		return;
490 	}
491 
492 	if (xzp) {
493 		dmu_buf_will_dirty(xzp->z_dbuf, tx);
494 		mutex_enter(&xzp->z_lock);
495 		xzp->z_unlinked = B_TRUE;	/* mark xzp for deletion */
496 		xzp->z_phys->zp_links = 0;	/* no more links to it */
497 		mutex_exit(&xzp->z_lock);
498 		zfs_unlinked_add(xzp, tx);
499 	}
500 
501 	/* Remove this znode from the unlinked set */
502 	error = zap_remove(os, zfsvfs->z_unlinkedobj,
503 	    zfs_unlinked_hexname(obj_name, zp->z_id), tx);
504 	ASSERT3U(error, ==, 0);
505 
506 	zfs_znode_delete(zp, tx);
507 
508 	dmu_tx_commit(tx);
509 
510 	if (xzp)
511 		VN_RELE(ZTOV(xzp));
512 }
513 
514 /*
515  * Link zp into dl.  Can only fail if zp has been unlinked.
516  */
517 int
518 zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
519 {
520 	znode_t *dzp = dl->dl_dzp;
521 	vnode_t *vp = ZTOV(zp);
522 	int zp_is_dir = (vp->v_type == VDIR);
523 	int error;
524 
525 	dmu_buf_will_dirty(zp->z_dbuf, tx);
526 	mutex_enter(&zp->z_lock);
527 
528 	if (!(flag & ZRENAMING)) {
529 		if (zp->z_unlinked) {	/* no new links to unlinked zp */
530 			ASSERT(!(flag & (ZNEW | ZEXISTS)));
531 			mutex_exit(&zp->z_lock);
532 			return (ENOENT);
533 		}
534 		zp->z_phys->zp_links++;
535 	}
536 	zp->z_phys->zp_parent = dzp->z_id;	/* dzp is now zp's parent */
537 
538 	if (!(flag & ZNEW))
539 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
540 	mutex_exit(&zp->z_lock);
541 
542 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
543 	mutex_enter(&dzp->z_lock);
544 	dzp->z_phys->zp_size++;			/* one dirent added */
545 	dzp->z_phys->zp_links += zp_is_dir;	/* ".." link from zp */
546 	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
547 	mutex_exit(&dzp->z_lock);
548 
549 	error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
550 	    8, 1, &zp->z_id, tx);
551 	ASSERT(error == 0);
552 
553 	dnlc_update(ZTOV(dzp), dl->dl_name, vp);
554 
555 	return (0);
556 }
557 
558 /*
559  * Unlink zp from dl, and mark zp for deletion if this was the last link.
560  * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
561  * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
562  * If it's non-NULL, we use it to indicate whether the znode needs deletion,
563  * and it's the caller's job to do it.
564  */
565 int
566 zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
567 	boolean_t *unlinkedp)
568 {
569 	znode_t *dzp = dl->dl_dzp;
570 	vnode_t *vp = ZTOV(zp);
571 	int zp_is_dir = (vp->v_type == VDIR);
572 	boolean_t unlinked = B_FALSE;
573 	int error;
574 
575 	dnlc_remove(ZTOV(dzp), dl->dl_name);
576 
577 	if (!(flag & ZRENAMING)) {
578 		dmu_buf_will_dirty(zp->z_dbuf, tx);
579 
580 		if (vn_vfswlock(vp))		/* prevent new mounts on zp */
581 			return (EBUSY);
582 
583 		if (vn_ismntpt(vp)) {		/* don't remove mount point */
584 			vn_vfsunlock(vp);
585 			return (EBUSY);
586 		}
587 
588 		mutex_enter(&zp->z_lock);
589 		if (zp_is_dir && !zfs_dirempty(zp)) {	/* dir not empty */
590 			mutex_exit(&zp->z_lock);
591 			vn_vfsunlock(vp);
592 			return (EEXIST);
593 		}
594 		if (zp->z_phys->zp_links <= zp_is_dir) {
595 			zfs_panic_recover("zfs: link count on %s is %u, "
596 			    "should be at least %u",
597 			    zp->z_vnode->v_path ? zp->z_vnode->v_path :
598 			    "<unknown>", (int)zp->z_phys->zp_links,
599 			    zp_is_dir + 1);
600 			zp->z_phys->zp_links = zp_is_dir + 1;
601 		}
602 		if (--zp->z_phys->zp_links == zp_is_dir) {
603 			zp->z_unlinked = B_TRUE;
604 			zp->z_phys->zp_links = 0;
605 			unlinked = B_TRUE;
606 		} else {
607 			zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
608 		}
609 		mutex_exit(&zp->z_lock);
610 		vn_vfsunlock(vp);
611 	}
612 
613 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
614 	mutex_enter(&dzp->z_lock);
615 	dzp->z_phys->zp_size--;			/* one dirent removed */
616 	dzp->z_phys->zp_links -= zp_is_dir;	/* ".." link from zp */
617 	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
618 	mutex_exit(&dzp->z_lock);
619 
620 	error = zap_remove(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name, tx);
621 	ASSERT(error == 0);
622 
623 	if (unlinkedp != NULL)
624 		*unlinkedp = unlinked;
625 	else if (unlinked)
626 		zfs_unlinked_add(zp, tx);
627 
628 	return (0);
629 }
630 
631 /*
632  * Indicate whether the directory is empty.  Works with or without z_lock
633  * held, but can only be consider a hint in the latter case.  Returns true
634  * if only "." and ".." remain and there's no work in progress.
635  */
636 boolean_t
637 zfs_dirempty(znode_t *dzp)
638 {
639 	return (dzp->z_phys->zp_size == 2 && dzp->z_dirlocks == 0);
640 }
641 
642 int
643 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
644 {
645 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
646 	znode_t *xzp;
647 	dmu_tx_t *tx;
648 	uint64_t xoid;
649 	int error;
650 
651 	*xvpp = NULL;
652 
653 	if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, cr))
654 		return (error);
655 
656 	tx = dmu_tx_create(zfsvfs->z_os);
657 	dmu_tx_hold_bonus(tx, zp->z_id);
658 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
659 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
660 	if (error) {
661 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT)
662 			dmu_tx_wait(tx);
663 		dmu_tx_abort(tx);
664 		return (error);
665 	}
666 	zfs_mknode(zp, vap, &xoid, tx, cr, IS_XATTR, &xzp, 0);
667 	ASSERT(xzp->z_id == xoid);
668 	ASSERT(xzp->z_phys->zp_parent == zp->z_id);
669 	dmu_buf_will_dirty(zp->z_dbuf, tx);
670 	zp->z_phys->zp_xattr = xoid;
671 
672 	(void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp, xzp, "");
673 	dmu_tx_commit(tx);
674 
675 	*xvpp = ZTOV(xzp);
676 
677 	return (0);
678 }
679 
680 /*
681  * Return a znode for the extended attribute directory for zp.
682  * ** If the directory does not already exist, it is created **
683  *
684  *	IN:	zp	- znode to obtain attribute directory from
685  *		cr	- credentials of caller
686  *		flags	- flags from the VOP_LOOKUP call
687  *
688  *	OUT:	xzpp	- pointer to extended attribute znode
689  *
690  *	RETURN:	0 on success
691  *		error number on failure
692  */
693 int
694 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
695 {
696 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
697 	znode_t		*xzp;
698 	zfs_dirlock_t	*dl;
699 	vattr_t		va;
700 	int		error;
701 top:
702 	error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR);
703 	if (error)
704 		return (error);
705 
706 	if (xzp != NULL) {
707 		*xvpp = ZTOV(xzp);
708 		zfs_dirent_unlock(dl);
709 		return (0);
710 	}
711 
712 	ASSERT(zp->z_phys->zp_xattr == 0);
713 
714 	if (!(flags & CREATE_XATTR_DIR)) {
715 		zfs_dirent_unlock(dl);
716 		return (ENOENT);
717 	}
718 
719 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
720 		zfs_dirent_unlock(dl);
721 		return (EROFS);
722 	}
723 
724 	/*
725 	 * The ability to 'create' files in an attribute
726 	 * directory comes from the write_xattr permission on the base file.
727 	 *
728 	 * The ability to 'search' an attribute directory requires
729 	 * read_xattr permission on the base file.
730 	 *
731 	 * Once in a directory the ability to read/write attributes
732 	 * is controlled by the permissions on the attribute file.
733 	 */
734 	va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
735 	va.va_type = VDIR;
736 	va.va_mode = S_IFDIR | S_ISVTX | 0777;
737 	va.va_uid = (uid_t)zp->z_phys->zp_uid;
738 	va.va_gid = (gid_t)zp->z_phys->zp_gid;
739 
740 	error = zfs_make_xattrdir(zp, &va, xvpp, cr);
741 	zfs_dirent_unlock(dl);
742 
743 	if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
744 		/* NB: we already did dmu_tx_wait() if necessary */
745 		goto top;
746 	}
747 
748 	return (error);
749 }
750 
751 /*
752  * Decide whether it is okay to remove within a sticky directory.
753  *
754  * In sticky directories, write access is not sufficient;
755  * you can remove entries from a directory only if:
756  *
757  *	you own the directory,
758  *	you own the entry,
759  *	the entry is a plain file and you have write access,
760  *	or you are privileged (checked in secpolicy...).
761  *
762  * The function returns 0 if remove access is granted.
763  */
764 int
765 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
766 {
767 	uid_t  		uid;
768 
769 	if (zdp->z_zfsvfs->z_assign >= TXG_INITIAL)	/* ZIL replay */
770 		return (0);
771 
772 	if ((zdp->z_phys->zp_mode & S_ISVTX) == 0 ||
773 	    (uid = crgetuid(cr)) == zdp->z_phys->zp_uid ||
774 	    uid == zp->z_phys->zp_uid ||
775 	    (ZTOV(zp)->v_type == VREG &&
776 	    zfs_zaccess(zp, ACE_WRITE_DATA, cr) == 0))
777 		return (0);
778 	else
779 		return (secpolicy_vnode_remove(cr));
780 }
781