xref: /titanic_51/usr/src/uts/common/fs/zfs/zfs_dir.c (revision e8717ca234d52a19424cf76f49488e8a2f6f3e14)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/time.h>
29 #include <sys/systm.h>
30 #include <sys/sysmacros.h>
31 #include <sys/resource.h>
32 #include <sys/vfs.h>
33 #include <sys/vnode.h>
34 #include <sys/file.h>
35 #include <sys/mode.h>
36 #include <sys/kmem.h>
37 #include <sys/uio.h>
38 #include <sys/pathname.h>
39 #include <sys/cmn_err.h>
40 #include <sys/errno.h>
41 #include <sys/stat.h>
42 #include <sys/unistd.h>
43 #include <sys/sunddi.h>
44 #include <sys/random.h>
45 #include <sys/policy.h>
46 #include <sys/zfs_dir.h>
47 #include <sys/zfs_acl.h>
48 #include <sys/fs/zfs.h>
49 #include "fs/fs_subr.h"
50 #include <sys/zap.h>
51 #include <sys/dmu.h>
52 #include <sys/atomic.h>
53 #include <sys/zfs_ctldir.h>
54 #include <sys/zfs_fuid.h>
55 #include <sys/dnlc.h>
56 #include <sys/extdirent.h>
57 
58 /*
59  * zfs_match_find() is used by zfs_dirent_lock() to peform zap lookups
60  * of names after deciding which is the appropriate lookup interface.
61  */
62 static int
63 zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, char *name, boolean_t exact,
64     boolean_t update, int *deflags, pathname_t *rpnp, uint64_t *zoid)
65 {
66 	int error;
67 
68 	if (zfsvfs->z_norm) {
69 		matchtype_t mt = MT_FIRST;
70 		boolean_t conflict = B_FALSE;
71 		size_t bufsz = 0;
72 		char *buf = NULL;
73 
74 		if (rpnp) {
75 			buf = rpnp->pn_buf;
76 			bufsz = rpnp->pn_bufsize;
77 		}
78 		if (exact)
79 			mt = MT_EXACT;
80 		/*
81 		 * In the non-mixed case we only expect there would ever
82 		 * be one match, but we need to use the normalizing lookup.
83 		 */
84 		error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1,
85 		    zoid, mt, buf, bufsz, &conflict);
86 		if (!error && deflags)
87 			*deflags = conflict ? ED_CASE_CONFLICT : 0;
88 	} else {
89 		error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid);
90 	}
91 	*zoid = ZFS_DIRENT_OBJ(*zoid);
92 
93 	if (error == ENOENT && update)
94 		dnlc_update(ZTOV(dzp), name, DNLC_NO_VNODE);
95 
96 	return (error);
97 }
98 
99 /*
100  * Lock a directory entry.  A dirlock on <dzp, name> protects that name
101  * in dzp's directory zap object.  As long as you hold a dirlock, you can
102  * assume two things: (1) dzp cannot be reaped, and (2) no other thread
103  * can change the zap entry for (i.e. link or unlink) this name.
104  *
105  * Input arguments:
106  *	dzp	- znode for directory
107  *	name	- name of entry to lock
108  *	flag	- ZNEW: if the entry already exists, fail with EEXIST.
109  *		  ZEXISTS: if the entry does not exist, fail with ENOENT.
110  *		  ZSHARED: allow concurrent access with other ZSHARED callers.
111  *		  ZXATTR: we want dzp's xattr directory
112  *		  ZCILOOK: On a mixed sensitivity file system,
113  *			   this lookup should be case-insensitive.
114  *		  ZCIEXACT: On a purely case-insensitive file system,
115  *			    this lookup should be case-sensitive.
116  *		  ZRENAMING: we are locking for renaming, force narrow locks
117  *		  ZHAVELOCK: Don't grab the z_name_lock for this call. The
118  *			     current thread already holds it.
119  *
120  * Output arguments:
121  *	zpp	- pointer to the znode for the entry (NULL if there isn't one)
122  *	dlpp	- pointer to the dirlock for this entry (NULL on error)
123  *      direntflags - (case-insensitive lookup only)
124  *		flags if multiple case-sensitive matches exist in directory
125  *      realpnp     - (case-insensitive lookup only)
126  *		actual name matched within the directory
127  *
128  * Return value: 0 on success or errno on failure.
129  *
130  * NOTE: Always checks for, and rejects, '.' and '..'.
131  * NOTE: For case-insensitive file systems we take wide locks (see below),
132  *	 but return znode pointers to a single match.
133  */
134 int
135 zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
136     int flag, int *direntflags, pathname_t *realpnp)
137 {
138 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
139 	zfs_dirlock_t	*dl;
140 	boolean_t	update;
141 	boolean_t	exact;
142 	uint64_t	zoid;
143 	vnode_t		*vp = NULL;
144 	int		error = 0;
145 	int		cmpflags;
146 
147 	*zpp = NULL;
148 	*dlpp = NULL;
149 
150 	/*
151 	 * Verify that we are not trying to lock '.', '..', or '.zfs'
152 	 */
153 	if (name[0] == '.' &&
154 	    (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) ||
155 	    zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0)
156 		return (EEXIST);
157 
158 	/*
159 	 * Case sensitivity and normalization preferences are set when
160 	 * the file system is created.  These are stored in the
161 	 * zfsvfs->z_case and zfsvfs->z_norm fields.  These choices
162 	 * affect what vnodes can be cached in the DNLC, how we
163 	 * perform zap lookups, and the "width" of our dirlocks.
164 	 *
165 	 * A normal dirlock locks a single name.  Note that with
166 	 * normalization a name can be composed multiple ways, but
167 	 * when normalized, these names all compare equal.  A wide
168 	 * dirlock locks multiple names.  We need these when the file
169 	 * system is supporting mixed-mode access.  It is sometimes
170 	 * necessary to lock all case permutations of file name at
171 	 * once so that simultaneous case-insensitive/case-sensitive
172 	 * behaves as rationally as possible.
173 	 */
174 
175 	/*
176 	 * Decide if exact matches should be requested when performing
177 	 * a zap lookup on file systems supporting case-insensitive
178 	 * access.
179 	 */
180 	exact =
181 	    ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE) && (flag & ZCIEXACT)) ||
182 	    ((zfsvfs->z_case == ZFS_CASE_MIXED) && !(flag & ZCILOOK));
183 
184 	/*
185 	 * Only look in or update the DNLC if we are looking for the
186 	 * name on a file system that does not require normalization
187 	 * or case folding.  We can also look there if we happen to be
188 	 * on a non-normalizing, mixed sensitivity file system IF we
189 	 * are looking for the exact name.
190 	 *
191 	 * Maybe can add TO-UPPERed version of name to dnlc in ci-only
192 	 * case for performance improvement?
193 	 */
194 	update = !zfsvfs->z_norm ||
195 	    ((zfsvfs->z_case == ZFS_CASE_MIXED) &&
196 	    !(zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER) && !(flag & ZCILOOK));
197 
198 	/*
199 	 * ZRENAMING indicates we are in a situation where we should
200 	 * take narrow locks regardless of the file system's
201 	 * preferences for normalizing and case folding.  This will
202 	 * prevent us deadlocking trying to grab the same wide lock
203 	 * twice if the two names happen to be case-insensitive
204 	 * matches.
205 	 */
206 	if (flag & ZRENAMING)
207 		cmpflags = 0;
208 	else
209 		cmpflags = zfsvfs->z_norm;
210 
211 	/*
212 	 * Wait until there are no locks on this name.
213 	 *
214 	 * Don't grab the the lock if it is already held. However, cannot
215 	 * have both ZSHARED and ZHAVELOCK together.
216 	 */
217 	ASSERT(!(flag & ZSHARED) || !(flag & ZHAVELOCK));
218 	if (!(flag & ZHAVELOCK))
219 		rw_enter(&dzp->z_name_lock, RW_READER);
220 
221 	mutex_enter(&dzp->z_lock);
222 	for (;;) {
223 		if (dzp->z_unlinked) {
224 			mutex_exit(&dzp->z_lock);
225 			if (!(flag & ZHAVELOCK))
226 				rw_exit(&dzp->z_name_lock);
227 			return (ENOENT);
228 		}
229 		for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next) {
230 			if ((u8_strcmp(name, dl->dl_name, 0, cmpflags,
231 			    U8_UNICODE_LATEST, &error) == 0) || error != 0)
232 				break;
233 		}
234 		if (error != 0) {
235 			mutex_exit(&dzp->z_lock);
236 			if (!(flag & ZHAVELOCK))
237 				rw_exit(&dzp->z_name_lock);
238 			return (ENOENT);
239 		}
240 		if (dl == NULL)	{
241 			/*
242 			 * Allocate a new dirlock and add it to the list.
243 			 */
244 			dl = kmem_alloc(sizeof (zfs_dirlock_t), KM_SLEEP);
245 			cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL);
246 			dl->dl_name = name;
247 			dl->dl_sharecnt = 0;
248 			dl->dl_namelock = 0;
249 			dl->dl_namesize = 0;
250 			dl->dl_dzp = dzp;
251 			dl->dl_next = dzp->z_dirlocks;
252 			dzp->z_dirlocks = dl;
253 			break;
254 		}
255 		if ((flag & ZSHARED) && dl->dl_sharecnt != 0)
256 			break;
257 		cv_wait(&dl->dl_cv, &dzp->z_lock);
258 	}
259 
260 	/*
261 	 * If the z_name_lock was NOT held for this dirlock record it.
262 	 */
263 	if (flag & ZHAVELOCK)
264 		dl->dl_namelock = 1;
265 
266 	if ((flag & ZSHARED) && ++dl->dl_sharecnt > 1 && dl->dl_namesize == 0) {
267 		/*
268 		 * We're the second shared reference to dl.  Make a copy of
269 		 * dl_name in case the first thread goes away before we do.
270 		 * Note that we initialize the new name before storing its
271 		 * pointer into dl_name, because the first thread may load
272 		 * dl->dl_name at any time.  He'll either see the old value,
273 		 * which is his, or the new shared copy; either is OK.
274 		 */
275 		dl->dl_namesize = strlen(dl->dl_name) + 1;
276 		name = kmem_alloc(dl->dl_namesize, KM_SLEEP);
277 		bcopy(dl->dl_name, name, dl->dl_namesize);
278 		dl->dl_name = name;
279 	}
280 
281 	mutex_exit(&dzp->z_lock);
282 
283 	/*
284 	 * We have a dirlock on the name.  (Note that it is the dirlock,
285 	 * not the dzp's z_lock, that protects the name in the zap object.)
286 	 * See if there's an object by this name; if so, put a hold on it.
287 	 */
288 	if (flag & ZXATTR) {
289 		zoid = dzp->z_phys->zp_xattr;
290 		error = (zoid == 0 ? ENOENT : 0);
291 	} else {
292 		if (update)
293 			vp = dnlc_lookup(ZTOV(dzp), name);
294 		if (vp == DNLC_NO_VNODE) {
295 			VN_RELE(vp);
296 			error = ENOENT;
297 		} else if (vp) {
298 			if (flag & ZNEW) {
299 				zfs_dirent_unlock(dl);
300 				VN_RELE(vp);
301 				return (EEXIST);
302 			}
303 			*dlpp = dl;
304 			*zpp = VTOZ(vp);
305 			return (0);
306 		} else {
307 			error = zfs_match_find(zfsvfs, dzp, name, exact,
308 			    update, direntflags, realpnp, &zoid);
309 		}
310 	}
311 	if (error) {
312 		if (error != ENOENT || (flag & ZEXISTS)) {
313 			zfs_dirent_unlock(dl);
314 			return (error);
315 		}
316 	} else {
317 		if (flag & ZNEW) {
318 			zfs_dirent_unlock(dl);
319 			return (EEXIST);
320 		}
321 		error = zfs_zget(zfsvfs, zoid, zpp);
322 		if (error) {
323 			zfs_dirent_unlock(dl);
324 			return (error);
325 		}
326 		if (!(flag & ZXATTR) && update)
327 			dnlc_update(ZTOV(dzp), name, ZTOV(*zpp));
328 	}
329 
330 	*dlpp = dl;
331 
332 	return (0);
333 }
334 
335 /*
336  * Unlock this directory entry and wake anyone who was waiting for it.
337  */
338 void
339 zfs_dirent_unlock(zfs_dirlock_t *dl)
340 {
341 	znode_t *dzp = dl->dl_dzp;
342 	zfs_dirlock_t **prev_dl, *cur_dl;
343 
344 	mutex_enter(&dzp->z_lock);
345 
346 	if (!dl->dl_namelock)
347 		rw_exit(&dzp->z_name_lock);
348 
349 	if (dl->dl_sharecnt > 1) {
350 		dl->dl_sharecnt--;
351 		mutex_exit(&dzp->z_lock);
352 		return;
353 	}
354 	prev_dl = &dzp->z_dirlocks;
355 	while ((cur_dl = *prev_dl) != dl)
356 		prev_dl = &cur_dl->dl_next;
357 	*prev_dl = dl->dl_next;
358 	cv_broadcast(&dl->dl_cv);
359 	mutex_exit(&dzp->z_lock);
360 
361 	if (dl->dl_namesize != 0)
362 		kmem_free(dl->dl_name, dl->dl_namesize);
363 	cv_destroy(&dl->dl_cv);
364 	kmem_free(dl, sizeof (*dl));
365 }
366 
367 /*
368  * Look up an entry in a directory.
369  *
370  * NOTE: '.' and '..' are handled as special cases because
371  *	no directory entries are actually stored for them.  If this is
372  *	the root of a filesystem, then '.zfs' is also treated as a
373  *	special pseudo-directory.
374  */
375 int
376 zfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp, int flags,
377     int *deflg, pathname_t *rpnp)
378 {
379 	zfs_dirlock_t *dl;
380 	znode_t *zp;
381 	int error = 0;
382 
383 	if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
384 		*vpp = ZTOV(dzp);
385 		VN_HOLD(*vpp);
386 	} else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
387 		zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
388 		/*
389 		 * If we are a snapshot mounted under .zfs, return
390 		 * the vp for the snapshot directory.
391 		 */
392 		if (dzp->z_phys->zp_parent == dzp->z_id &&
393 		    zfsvfs->z_parent != zfsvfs) {
394 			error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir,
395 			    "snapshot", vpp, NULL, 0, NULL, kcred,
396 			    NULL, NULL, NULL);
397 			return (error);
398 		}
399 		rw_enter(&dzp->z_parent_lock, RW_READER);
400 		error = zfs_zget(zfsvfs, dzp->z_phys->zp_parent, &zp);
401 		if (error == 0)
402 			*vpp = ZTOV(zp);
403 		rw_exit(&dzp->z_parent_lock);
404 	} else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) {
405 		*vpp = zfsctl_root(dzp);
406 	} else {
407 		int zf;
408 
409 		zf = ZEXISTS | ZSHARED;
410 		if (flags & FIGNORECASE)
411 			zf |= ZCILOOK;
412 
413 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zf, deflg, rpnp);
414 		if (error == 0) {
415 			*vpp = ZTOV(zp);
416 			zfs_dirent_unlock(dl);
417 			dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
418 		}
419 		rpnp = NULL;
420 	}
421 
422 	if ((flags & FIGNORECASE) && rpnp && !error)
423 		(void) strlcpy(rpnp->pn_buf, name, rpnp->pn_bufsize);
424 
425 	return (error);
426 }
427 
428 /*
429  * unlinked Set (formerly known as the "delete queue") Error Handling
430  *
431  * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
432  * don't specify the name of the entry that we will be manipulating.  We
433  * also fib and say that we won't be adding any new entries to the
434  * unlinked set, even though we might (this is to lower the minimum file
435  * size that can be deleted in a full filesystem).  So on the small
436  * chance that the nlink list is using a fat zap (ie. has more than
437  * 2000 entries), we *may* not pre-read a block that's needed.
438  * Therefore it is remotely possible for some of the assertions
439  * regarding the unlinked set below to fail due to i/o error.  On a
440  * nondebug system, this will result in the space being leaked.
441  */
442 void
443 zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
444 {
445 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
446 
447 	ASSERT(zp->z_unlinked);
448 	ASSERT3U(zp->z_phys->zp_links, ==, 0);
449 
450 	VERIFY3U(0, ==,
451 	    zap_add_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
452 }
453 
454 /*
455  * Clean up any znodes that had no links when we either crashed or
456  * (force) umounted the file system.
457  */
458 void
459 zfs_unlinked_drain(zfsvfs_t *zfsvfs)
460 {
461 	zap_cursor_t	zc;
462 	zap_attribute_t zap;
463 	dmu_object_info_t doi;
464 	znode_t		*zp;
465 	int		error;
466 
467 	/*
468 	 * Interate over the contents of the unlinked set.
469 	 */
470 	for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
471 	    zap_cursor_retrieve(&zc, &zap) == 0;
472 	    zap_cursor_advance(&zc)) {
473 
474 		/*
475 		 * See what kind of object we have in list
476 		 */
477 
478 		error = dmu_object_info(zfsvfs->z_os,
479 		    zap.za_first_integer, &doi);
480 		if (error != 0)
481 			continue;
482 
483 		ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
484 		    (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
485 		/*
486 		 * We need to re-mark these list entries for deletion,
487 		 * so we pull them back into core and set zp->z_unlinked.
488 		 */
489 		error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
490 
491 		/*
492 		 * We may pick up znodes that are already marked for deletion.
493 		 * This could happen during the purge of an extended attribute
494 		 * directory.  All we need to do is skip over them, since they
495 		 * are already in the system marked z_unlinked.
496 		 */
497 		if (error != 0)
498 			continue;
499 
500 		zp->z_unlinked = B_TRUE;
501 		VN_RELE(ZTOV(zp));
502 	}
503 	zap_cursor_fini(&zc);
504 }
505 
506 /*
507  * Delete the entire contents of a directory.  Return a count
508  * of the number of entries that could not be deleted. If we encounter
509  * an error, return a count of at least one so that the directory stays
510  * in the unlinked set.
511  *
512  * NOTE: this function assumes that the directory is inactive,
513  *	so there is no need to lock its entries before deletion.
514  *	Also, it assumes the directory contents is *only* regular
515  *	files.
516  */
517 static int
518 zfs_purgedir(znode_t *dzp)
519 {
520 	zap_cursor_t	zc;
521 	zap_attribute_t	zap;
522 	znode_t		*xzp;
523 	dmu_tx_t	*tx;
524 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
525 	zfs_dirlock_t	dl;
526 	int skipped = 0;
527 	int error;
528 
529 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
530 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
531 	    zap_cursor_advance(&zc)) {
532 		error = zfs_zget(zfsvfs,
533 		    ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
534 		if (error) {
535 			skipped += 1;
536 			continue;
537 		}
538 
539 		ASSERT((ZTOV(xzp)->v_type == VREG) ||
540 		    (ZTOV(xzp)->v_type == VLNK));
541 
542 		tx = dmu_tx_create(zfsvfs->z_os);
543 		dmu_tx_hold_bonus(tx, dzp->z_id);
544 		dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
545 		dmu_tx_hold_bonus(tx, xzp->z_id);
546 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
547 		error = dmu_tx_assign(tx, TXG_WAIT);
548 		if (error) {
549 			dmu_tx_abort(tx);
550 			VN_RELE(ZTOV(xzp));
551 			skipped += 1;
552 			continue;
553 		}
554 		bzero(&dl, sizeof (dl));
555 		dl.dl_dzp = dzp;
556 		dl.dl_name = zap.za_name;
557 
558 		error = zfs_link_destroy(&dl, xzp, tx, 0, NULL);
559 		if (error)
560 			skipped += 1;
561 		dmu_tx_commit(tx);
562 
563 		VN_RELE(ZTOV(xzp));
564 	}
565 	zap_cursor_fini(&zc);
566 	if (error != ENOENT)
567 		skipped += 1;
568 	return (skipped);
569 }
570 
571 void
572 zfs_rmnode(znode_t *zp)
573 {
574 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
575 	objset_t	*os = zfsvfs->z_os;
576 	znode_t		*xzp = NULL;
577 	dmu_tx_t	*tx;
578 	uint64_t	acl_obj;
579 	int		error;
580 
581 	ASSERT(ZTOV(zp)->v_count == 0);
582 	ASSERT(zp->z_phys->zp_links == 0);
583 
584 	/*
585 	 * If this is an attribute directory, purge its contents.
586 	 */
587 	if (ZTOV(zp)->v_type == VDIR && (zp->z_phys->zp_flags & ZFS_XATTR)) {
588 		if (zfs_purgedir(zp) != 0) {
589 			/*
590 			 * Not enough space to delete some xattrs.
591 			 * Leave it in the unlinked set.
592 			 */
593 			zfs_znode_dmu_fini(zp);
594 			zfs_znode_free(zp);
595 			return;
596 		}
597 	}
598 
599 	/*
600 	 * Free up all the data in the file.
601 	 */
602 	error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
603 	if (error) {
604 		/*
605 		 * Not enough space.  Leave the file in the unlinked set.
606 		 */
607 		zfs_znode_dmu_fini(zp);
608 		zfs_znode_free(zp);
609 		return;
610 	}
611 
612 	/*
613 	 * If the file has extended attributes, we're going to unlink
614 	 * the xattr dir.
615 	 */
616 	if (zp->z_phys->zp_xattr) {
617 		error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
618 		ASSERT(error == 0);
619 	}
620 
621 	acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
622 
623 	/*
624 	 * Set up the final transaction.
625 	 */
626 	tx = dmu_tx_create(os);
627 	dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
628 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
629 	if (xzp) {
630 		dmu_tx_hold_bonus(tx, xzp->z_id);
631 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
632 	}
633 	if (acl_obj)
634 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
635 	error = dmu_tx_assign(tx, TXG_WAIT);
636 	if (error) {
637 		/*
638 		 * Not enough space to delete the file.  Leave it in the
639 		 * unlinked set, leaking it until the fs is remounted (at
640 		 * which point we'll call zfs_unlinked_drain() to process it).
641 		 */
642 		dmu_tx_abort(tx);
643 		zfs_znode_dmu_fini(zp);
644 		zfs_znode_free(zp);
645 		goto out;
646 	}
647 
648 	if (xzp) {
649 		dmu_buf_will_dirty(xzp->z_dbuf, tx);
650 		mutex_enter(&xzp->z_lock);
651 		xzp->z_unlinked = B_TRUE;	/* mark xzp for deletion */
652 		xzp->z_phys->zp_links = 0;	/* no more links to it */
653 		mutex_exit(&xzp->z_lock);
654 		zfs_unlinked_add(xzp, tx);
655 	}
656 
657 	/* Remove this znode from the unlinked set */
658 	VERIFY3U(0, ==,
659 	    zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
660 
661 	zfs_znode_delete(zp, tx);
662 
663 	dmu_tx_commit(tx);
664 out:
665 	if (xzp)
666 		VN_RELE(ZTOV(xzp));
667 }
668 
669 static uint64_t
670 zfs_dirent(znode_t *zp)
671 {
672 	uint64_t de = zp->z_id;
673 	if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
674 		de |= IFTODT((zp)->z_phys->zp_mode) << 60;
675 	return (de);
676 }
677 
678 /*
679  * Link zp into dl.  Can only fail if zp has been unlinked.
680  */
681 int
682 zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
683 {
684 	znode_t *dzp = dl->dl_dzp;
685 	vnode_t *vp = ZTOV(zp);
686 	uint64_t value;
687 	int zp_is_dir = (vp->v_type == VDIR);
688 	int error;
689 
690 	dmu_buf_will_dirty(zp->z_dbuf, tx);
691 	mutex_enter(&zp->z_lock);
692 
693 	if (!(flag & ZRENAMING)) {
694 		if (zp->z_unlinked) {	/* no new links to unlinked zp */
695 			ASSERT(!(flag & (ZNEW | ZEXISTS)));
696 			mutex_exit(&zp->z_lock);
697 			return (ENOENT);
698 		}
699 		zp->z_phys->zp_links++;
700 	}
701 	zp->z_phys->zp_parent = dzp->z_id;	/* dzp is now zp's parent */
702 
703 	if (!(flag & ZNEW))
704 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
705 	mutex_exit(&zp->z_lock);
706 
707 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
708 	mutex_enter(&dzp->z_lock);
709 	dzp->z_phys->zp_size++;			/* one dirent added */
710 	dzp->z_phys->zp_links += zp_is_dir;	/* ".." link from zp */
711 	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
712 	mutex_exit(&dzp->z_lock);
713 
714 	value = zfs_dirent(zp);
715 	error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
716 	    8, 1, &value, tx);
717 	ASSERT(error == 0);
718 
719 	dnlc_update(ZTOV(dzp), dl->dl_name, vp);
720 
721 	return (0);
722 }
723 
724 /*
725  * Unlink zp from dl, and mark zp for deletion if this was the last link.
726  * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
727  * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
728  * If it's non-NULL, we use it to indicate whether the znode needs deletion,
729  * and it's the caller's job to do it.
730  */
731 int
732 zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
733 	boolean_t *unlinkedp)
734 {
735 	znode_t *dzp = dl->dl_dzp;
736 	vnode_t *vp = ZTOV(zp);
737 	int zp_is_dir = (vp->v_type == VDIR);
738 	boolean_t unlinked = B_FALSE;
739 	int error;
740 
741 	dnlc_remove(ZTOV(dzp), dl->dl_name);
742 
743 	if (!(flag & ZRENAMING)) {
744 		dmu_buf_will_dirty(zp->z_dbuf, tx);
745 
746 		if (vn_vfswlock(vp))		/* prevent new mounts on zp */
747 			return (EBUSY);
748 
749 		if (vn_ismntpt(vp)) {		/* don't remove mount point */
750 			vn_vfsunlock(vp);
751 			return (EBUSY);
752 		}
753 
754 		mutex_enter(&zp->z_lock);
755 		if (zp_is_dir && !zfs_dirempty(zp)) {	/* dir not empty */
756 			mutex_exit(&zp->z_lock);
757 			vn_vfsunlock(vp);
758 			return (EEXIST);
759 		}
760 		if (zp->z_phys->zp_links <= zp_is_dir) {
761 			zfs_panic_recover("zfs: link count on %s is %u, "
762 			    "should be at least %u",
763 			    zp->z_vnode->v_path ? zp->z_vnode->v_path :
764 			    "<unknown>", (int)zp->z_phys->zp_links,
765 			    zp_is_dir + 1);
766 			zp->z_phys->zp_links = zp_is_dir + 1;
767 		}
768 		if (--zp->z_phys->zp_links == zp_is_dir) {
769 			zp->z_unlinked = B_TRUE;
770 			zp->z_phys->zp_links = 0;
771 			unlinked = B_TRUE;
772 		} else {
773 			zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
774 		}
775 		mutex_exit(&zp->z_lock);
776 		vn_vfsunlock(vp);
777 	}
778 
779 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
780 	mutex_enter(&dzp->z_lock);
781 	dzp->z_phys->zp_size--;			/* one dirent removed */
782 	dzp->z_phys->zp_links -= zp_is_dir;	/* ".." link from zp */
783 	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
784 	mutex_exit(&dzp->z_lock);
785 
786 	if (zp->z_zfsvfs->z_norm) {
787 		if (((zp->z_zfsvfs->z_case == ZFS_CASE_INSENSITIVE) &&
788 		    (flag & ZCIEXACT)) ||
789 		    ((zp->z_zfsvfs->z_case == ZFS_CASE_MIXED) &&
790 		    !(flag & ZCILOOK)))
791 			error = zap_remove_norm(zp->z_zfsvfs->z_os,
792 			    dzp->z_id, dl->dl_name, MT_EXACT, tx);
793 		else
794 			error = zap_remove_norm(zp->z_zfsvfs->z_os,
795 			    dzp->z_id, dl->dl_name, MT_FIRST, tx);
796 	} else {
797 		error = zap_remove(zp->z_zfsvfs->z_os,
798 		    dzp->z_id, dl->dl_name, tx);
799 	}
800 	ASSERT(error == 0);
801 
802 	if (unlinkedp != NULL)
803 		*unlinkedp = unlinked;
804 	else if (unlinked)
805 		zfs_unlinked_add(zp, tx);
806 
807 	return (0);
808 }
809 
810 /*
811  * Indicate whether the directory is empty.  Works with or without z_lock
812  * held, but can only be consider a hint in the latter case.  Returns true
813  * if only "." and ".." remain and there's no work in progress.
814  */
815 boolean_t
816 zfs_dirempty(znode_t *dzp)
817 {
818 	return (dzp->z_phys->zp_size == 2 && dzp->z_dirlocks == 0);
819 }
820 
821 int
822 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
823 {
824 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
825 	znode_t *xzp;
826 	dmu_tx_t *tx;
827 	int error;
828 	zfs_acl_ids_t acl_ids;
829 	boolean_t fuid_dirtied;
830 
831 	*xvpp = NULL;
832 
833 	if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr))
834 		return (error);
835 
836 	if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL,
837 	    &acl_ids)) != 0)
838 		return (error);
839 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
840 		zfs_acl_ids_free(&acl_ids);
841 		return (EDQUOT);
842 	}
843 
844 	tx = dmu_tx_create(zfsvfs->z_os);
845 	dmu_tx_hold_bonus(tx, zp->z_id);
846 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
847 	fuid_dirtied = zfsvfs->z_fuid_dirty;
848 	if (fuid_dirtied)
849 		zfs_fuid_txhold(zfsvfs, tx);
850 	error = dmu_tx_assign(tx, TXG_NOWAIT);
851 	if (error) {
852 		zfs_acl_ids_free(&acl_ids);
853 		if (error == ERESTART)
854 			dmu_tx_wait(tx);
855 		dmu_tx_abort(tx);
856 		return (error);
857 	}
858 	zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, 0, &acl_ids);
859 
860 	if (fuid_dirtied)
861 		zfs_fuid_sync(zfsvfs, tx);
862 
863 	ASSERT(xzp->z_phys->zp_parent == zp->z_id);
864 	dmu_buf_will_dirty(zp->z_dbuf, tx);
865 	zp->z_phys->zp_xattr = xzp->z_id;
866 
867 	(void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
868 	    xzp, "", NULL, acl_ids.z_fuidp, vap);
869 
870 	zfs_acl_ids_free(&acl_ids);
871 	dmu_tx_commit(tx);
872 
873 	*xvpp = ZTOV(xzp);
874 
875 	return (0);
876 }
877 
878 /*
879  * Return a znode for the extended attribute directory for zp.
880  * ** If the directory does not already exist, it is created **
881  *
882  *	IN:	zp	- znode to obtain attribute directory from
883  *		cr	- credentials of caller
884  *		flags	- flags from the VOP_LOOKUP call
885  *
886  *	OUT:	xzpp	- pointer to extended attribute znode
887  *
888  *	RETURN:	0 on success
889  *		error number on failure
890  */
891 int
892 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
893 {
894 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
895 	znode_t		*xzp;
896 	zfs_dirlock_t	*dl;
897 	vattr_t		va;
898 	int		error;
899 top:
900 	error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR, NULL, NULL);
901 	if (error)
902 		return (error);
903 
904 	if (xzp != NULL) {
905 		*xvpp = ZTOV(xzp);
906 		zfs_dirent_unlock(dl);
907 		return (0);
908 	}
909 
910 	ASSERT(zp->z_phys->zp_xattr == 0);
911 
912 	if (!(flags & CREATE_XATTR_DIR)) {
913 		zfs_dirent_unlock(dl);
914 		return (ENOENT);
915 	}
916 
917 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
918 		zfs_dirent_unlock(dl);
919 		return (EROFS);
920 	}
921 
922 	/*
923 	 * The ability to 'create' files in an attribute
924 	 * directory comes from the write_xattr permission on the base file.
925 	 *
926 	 * The ability to 'search' an attribute directory requires
927 	 * read_xattr permission on the base file.
928 	 *
929 	 * Once in a directory the ability to read/write attributes
930 	 * is controlled by the permissions on the attribute file.
931 	 */
932 	va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
933 	va.va_type = VDIR;
934 	va.va_mode = S_IFDIR | S_ISVTX | 0777;
935 	zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
936 
937 	error = zfs_make_xattrdir(zp, &va, xvpp, cr);
938 	zfs_dirent_unlock(dl);
939 
940 	if (error == ERESTART) {
941 		/* NB: we already did dmu_tx_wait() if necessary */
942 		goto top;
943 	}
944 
945 	return (error);
946 }
947 
948 /*
949  * Decide whether it is okay to remove within a sticky directory.
950  *
951  * In sticky directories, write access is not sufficient;
952  * you can remove entries from a directory only if:
953  *
954  *	you own the directory,
955  *	you own the entry,
956  *	the entry is a plain file and you have write access,
957  *	or you are privileged (checked in secpolicy...).
958  *
959  * The function returns 0 if remove access is granted.
960  */
961 int
962 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
963 {
964 	uid_t  		uid;
965 	uid_t		downer;
966 	uid_t		fowner;
967 	zfsvfs_t	*zfsvfs = zdp->z_zfsvfs;
968 
969 	if (zdp->z_zfsvfs->z_replay)
970 		return (0);
971 
972 	if ((zdp->z_phys->zp_mode & S_ISVTX) == 0)
973 		return (0);
974 
975 	downer = zfs_fuid_map_id(zfsvfs, zdp->z_phys->zp_uid, cr, ZFS_OWNER);
976 	fowner = zfs_fuid_map_id(zfsvfs, zp->z_phys->zp_uid, cr, ZFS_OWNER);
977 
978 	if ((uid = crgetuid(cr)) == downer || uid == fowner ||
979 	    (ZTOV(zp)->v_type == VREG &&
980 	    zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
981 		return (0);
982 	else
983 		return (secpolicy_vnode_remove(cr));
984 }
985