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 https://opensource.org/licenses/CDDL-1.0.
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 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
25  * Copyright 2017 Nexenta Systems, Inc.
26  */
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/kmem.h>
38 #include <sys/uio.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/condvar.h>
47 #include <sys/callb.h>
48 #include <sys/smp.h>
49 #include <sys/zfs_dir.h>
50 #include <sys/zfs_acl.h>
51 #include <sys/fs/zfs.h>
52 #include <sys/zap.h>
53 #include <sys/dmu.h>
54 #include <sys/atomic.h>
55 #include <sys/zfs_ctldir.h>
56 #include <sys/zfs_fuid.h>
57 #include <sys/sa.h>
58 #include <sys/zfs_sa.h>
59 #include <sys/dmu_objset.h>
60 #include <sys/dsl_dir.h>
61 
62 #include <sys/ccompat.h>
63 
64 /*
65  * zfs_match_find() is used by zfs_dirent_lookup() to perform zap lookups
66  * of names after deciding which is the appropriate lookup interface.
67  */
68 static int
69 zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, const char *name,
70     matchtype_t mt, uint64_t *zoid)
71 {
72 	int error;
73 
74 	if (zfsvfs->z_norm) {
75 
76 		/*
77 		 * In the non-mixed case we only expect there would ever
78 		 * be one match, but we need to use the normalizing lookup.
79 		 */
80 		error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1,
81 		    zoid, mt, NULL, 0, NULL);
82 	} else {
83 		error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid);
84 	}
85 	*zoid = ZFS_DIRENT_OBJ(*zoid);
86 
87 	return (error);
88 }
89 
90 /*
91  * Look up a directory entry under a locked vnode.
92  * dvp being locked gives us a guarantee that there are no concurrent
93  * modification of the directory and, thus, if a node can be found in
94  * the directory, then it must not be unlinked.
95  *
96  * Input arguments:
97  *	dzp	- znode for directory
98  *	name	- name of entry to lock
99  *	flag	- ZNEW: if the entry already exists, fail with EEXIST.
100  *		  ZEXISTS: if the entry does not exist, fail with ENOENT.
101  *		  ZXATTR: we want dzp's xattr directory
102  *
103  * Output arguments:
104  *	zpp	- pointer to the znode for the entry (NULL if there isn't one)
105  *
106  * Return value: 0 on success or errno on failure.
107  *
108  * NOTE: Always checks for, and rejects, '.' and '..'.
109  */
110 int
111 zfs_dirent_lookup(znode_t *dzp, const char *name, znode_t **zpp, int flag)
112 {
113 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
114 	znode_t		*zp;
115 	matchtype_t	mt = 0;
116 	uint64_t	zoid;
117 	int		error = 0;
118 
119 	if (zfsvfs->z_replay == B_FALSE)
120 		ASSERT_VOP_LOCKED(ZTOV(dzp), __func__);
121 
122 	*zpp = NULL;
123 
124 	/*
125 	 * Verify that we are not trying to lock '.', '..', or '.zfs'
126 	 */
127 	if (name[0] == '.' &&
128 	    (((name[1] == '\0') || (name[1] == '.' && name[2] == '\0')) ||
129 	    (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0)))
130 		return (SET_ERROR(EEXIST));
131 
132 	/*
133 	 * Case sensitivity and normalization preferences are set when
134 	 * the file system is created.  These are stored in the
135 	 * zfsvfs->z_case and zfsvfs->z_norm fields.  These choices
136 	 * affect how we perform zap lookups.
137 	 *
138 	 * When matching we may need to normalize & change case according to
139 	 * FS settings.
140 	 *
141 	 * Note that a normalized match is necessary for a case insensitive
142 	 * filesystem when the lookup request is not exact because normalization
143 	 * can fold case independent of normalizing code point sequences.
144 	 *
145 	 * See the table above zfs_dropname().
146 	 */
147 	if (zfsvfs->z_norm != 0) {
148 		mt = MT_NORMALIZE;
149 
150 		/*
151 		 * Determine if the match needs to honor the case specified in
152 		 * lookup, and if so keep track of that so that during
153 		 * normalization we don't fold case.
154 		 */
155 		if (zfsvfs->z_case == ZFS_CASE_MIXED) {
156 			mt |= MT_MATCH_CASE;
157 		}
158 	}
159 
160 	/*
161 	 * Only look in or update the DNLC if we are looking for the
162 	 * name on a file system that does not require normalization
163 	 * or case folding.  We can also look there if we happen to be
164 	 * on a non-normalizing, mixed sensitivity file system IF we
165 	 * are looking for the exact name.
166 	 *
167 	 * NB: we do not need to worry about this flag for ZFS_CASE_SENSITIVE
168 	 * because in that case MT_EXACT and MT_FIRST should produce exactly
169 	 * the same result.
170 	 */
171 
172 	if (dzp->z_unlinked && !(flag & ZXATTR))
173 		return (ENOENT);
174 	if (flag & ZXATTR) {
175 		error = sa_lookup(dzp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &zoid,
176 		    sizeof (zoid));
177 		if (error == 0)
178 			error = (zoid == 0 ? ENOENT : 0);
179 	} else {
180 		error = zfs_match_find(zfsvfs, dzp, name, mt, &zoid);
181 	}
182 	if (error) {
183 		if (error != ENOENT || (flag & ZEXISTS)) {
184 			return (error);
185 		}
186 	} else {
187 		if (flag & ZNEW) {
188 			return (SET_ERROR(EEXIST));
189 		}
190 		error = zfs_zget(zfsvfs, zoid, &zp);
191 		if (error)
192 			return (error);
193 		ASSERT(!zp->z_unlinked);
194 		*zpp = zp;
195 	}
196 
197 	return (0);
198 }
199 
200 static int
201 zfs_dd_lookup(znode_t *dzp, znode_t **zpp)
202 {
203 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
204 	znode_t *zp;
205 	uint64_t parent;
206 	int error;
207 
208 #ifdef ZFS_DEBUG
209 	if (zfsvfs->z_replay == B_FALSE)
210 		ASSERT_VOP_LOCKED(ZTOV(dzp), __func__);
211 #endif
212 	if (dzp->z_unlinked)
213 		return (ENOENT);
214 
215 	if ((error = sa_lookup(dzp->z_sa_hdl,
216 	    SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
217 		return (error);
218 
219 	error = zfs_zget(zfsvfs, parent, &zp);
220 	if (error == 0)
221 		*zpp = zp;
222 	return (error);
223 }
224 
225 int
226 zfs_dirlook(znode_t *dzp, const char *name, znode_t **zpp)
227 {
228 	zfsvfs_t *zfsvfs __unused = dzp->z_zfsvfs;
229 	znode_t *zp = NULL;
230 	int error = 0;
231 
232 #ifdef ZFS_DEBUG
233 	if (zfsvfs->z_replay == B_FALSE)
234 		ASSERT_VOP_LOCKED(ZTOV(dzp), __func__);
235 #endif
236 	if (dzp->z_unlinked)
237 		return (SET_ERROR(ENOENT));
238 
239 	if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
240 		*zpp = dzp;
241 	} else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
242 		error = zfs_dd_lookup(dzp, &zp);
243 		if (error == 0)
244 			*zpp = zp;
245 	} else {
246 		error = zfs_dirent_lookup(dzp, name, &zp, ZEXISTS);
247 		if (error == 0) {
248 			dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
249 			*zpp = zp;
250 		}
251 	}
252 	return (error);
253 }
254 
255 /*
256  * unlinked Set (formerly known as the "delete queue") Error Handling
257  *
258  * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
259  * don't specify the name of the entry that we will be manipulating.  We
260  * also fib and say that we won't be adding any new entries to the
261  * unlinked set, even though we might (this is to lower the minimum file
262  * size that can be deleted in a full filesystem).  So on the small
263  * chance that the nlink list is using a fat zap (ie. has more than
264  * 2000 entries), we *may* not pre-read a block that's needed.
265  * Therefore it is remotely possible for some of the assertions
266  * regarding the unlinked set below to fail due to i/o error.  On a
267  * nondebug system, this will result in the space being leaked.
268  */
269 void
270 zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
271 {
272 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
273 
274 	ASSERT(zp->z_unlinked);
275 	ASSERT3U(zp->z_links, ==, 0);
276 
277 	VERIFY0(zap_add_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
278 
279 	dataset_kstats_update_nunlinks_kstat(&zfsvfs->z_kstat, 1);
280 }
281 
282 /*
283  * Clean up any znodes that had no links when we either crashed or
284  * (force) umounted the file system.
285  */
286 void
287 zfs_unlinked_drain(zfsvfs_t *zfsvfs)
288 {
289 	zap_cursor_t	zc;
290 	zap_attribute_t zap;
291 	dmu_object_info_t doi;
292 	znode_t		*zp;
293 	dmu_tx_t	*tx;
294 	int		error;
295 
296 	/*
297 	 * Iterate over the contents of the unlinked set.
298 	 */
299 	for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
300 	    zap_cursor_retrieve(&zc, &zap) == 0;
301 	    zap_cursor_advance(&zc)) {
302 
303 		/*
304 		 * See what kind of object we have in list
305 		 */
306 
307 		error = dmu_object_info(zfsvfs->z_os,
308 		    zap.za_first_integer, &doi);
309 		if (error != 0)
310 			continue;
311 
312 		ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
313 		    (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
314 		/*
315 		 * We need to re-mark these list entries for deletion,
316 		 * so we pull them back into core and set zp->z_unlinked.
317 		 */
318 		error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
319 
320 		/*
321 		 * We may pick up znodes that are already marked for deletion.
322 		 * This could happen during the purge of an extended attribute
323 		 * directory.  All we need to do is skip over them, since they
324 		 * are already in the system marked z_unlinked.
325 		 */
326 		if (error != 0)
327 			continue;
328 
329 		vn_lock(ZTOV(zp), LK_EXCLUSIVE | LK_RETRY);
330 
331 		/*
332 		 * Due to changes in zfs_rmnode we need to make sure the
333 		 * link count is set to zero here.
334 		 */
335 		if (zp->z_links != 0) {
336 			tx = dmu_tx_create(zfsvfs->z_os);
337 			dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
338 			error = dmu_tx_assign(tx, TXG_WAIT);
339 			if (error != 0) {
340 				dmu_tx_abort(tx);
341 				vput(ZTOV(zp));
342 				continue;
343 			}
344 			zp->z_links = 0;
345 			VERIFY0(sa_update(zp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
346 			    &zp->z_links, sizeof (zp->z_links), tx));
347 			dmu_tx_commit(tx);
348 		}
349 
350 		zp->z_unlinked = B_TRUE;
351 		vput(ZTOV(zp));
352 	}
353 	zap_cursor_fini(&zc);
354 }
355 
356 /*
357  * Delete the entire contents of a directory.  Return a count
358  * of the number of entries that could not be deleted. If we encounter
359  * an error, return a count of at least one so that the directory stays
360  * in the unlinked set.
361  *
362  * NOTE: this function assumes that the directory is inactive,
363  *	so there is no need to lock its entries before deletion.
364  *	Also, it assumes the directory contents is *only* regular
365  *	files.
366  */
367 static int
368 zfs_purgedir(znode_t *dzp)
369 {
370 	zap_cursor_t	zc;
371 	zap_attribute_t	zap;
372 	znode_t		*xzp;
373 	dmu_tx_t	*tx;
374 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
375 	int skipped = 0;
376 	int error;
377 
378 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
379 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
380 	    zap_cursor_advance(&zc)) {
381 		error = zfs_zget(zfsvfs,
382 		    ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
383 		if (error) {
384 			skipped += 1;
385 			continue;
386 		}
387 
388 		vn_lock(ZTOV(xzp), LK_EXCLUSIVE | LK_RETRY);
389 		ASSERT((ZTOV(xzp)->v_type == VREG) ||
390 		    (ZTOV(xzp)->v_type == VLNK));
391 
392 		tx = dmu_tx_create(zfsvfs->z_os);
393 		dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
394 		dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
395 		dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
396 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
397 		/* Is this really needed ? */
398 		zfs_sa_upgrade_txholds(tx, xzp);
399 		dmu_tx_mark_netfree(tx);
400 		error = dmu_tx_assign(tx, TXG_WAIT);
401 		if (error) {
402 			dmu_tx_abort(tx);
403 			vput(ZTOV(xzp));
404 			skipped += 1;
405 			continue;
406 		}
407 
408 		error = zfs_link_destroy(dzp, zap.za_name, xzp, tx, 0, NULL);
409 		if (error)
410 			skipped += 1;
411 		dmu_tx_commit(tx);
412 
413 		vput(ZTOV(xzp));
414 	}
415 	zap_cursor_fini(&zc);
416 	if (error != ENOENT)
417 		skipped += 1;
418 	return (skipped);
419 }
420 
421 extern taskq_t *zfsvfs_taskq;
422 
423 void
424 zfs_rmnode(znode_t *zp)
425 {
426 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
427 	objset_t	*os = zfsvfs->z_os;
428 	dmu_tx_t	*tx;
429 	uint64_t	z_id = zp->z_id;
430 	uint64_t	acl_obj;
431 	uint64_t	xattr_obj;
432 	uint64_t	count;
433 	int		error;
434 
435 	ASSERT3U(zp->z_links, ==, 0);
436 	if (zfsvfs->z_replay == B_FALSE)
437 		ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
438 
439 	/*
440 	 * If this is an attribute directory, purge its contents.
441 	 */
442 	if (ZTOV(zp) != NULL && ZTOV(zp)->v_type == VDIR &&
443 	    (zp->z_pflags & ZFS_XATTR)) {
444 		if (zfs_purgedir(zp) != 0) {
445 			/*
446 			 * Not enough space to delete some xattrs.
447 			 * Leave it in the unlinked set.
448 			 */
449 			ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
450 			zfs_znode_dmu_fini(zp);
451 			zfs_znode_free(zp);
452 			ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
453 			return;
454 		}
455 	} else {
456 		/*
457 		 * Free up all the data in the file.  We don't do this for
458 		 * XATTR directories because we need truncate and remove to be
459 		 * in the same tx, like in zfs_znode_delete(). Otherwise, if
460 		 * we crash here we'll end up with an inconsistent truncated
461 		 * zap object in the delete queue.  Note a truncated file is
462 		 * harmless since it only contains user data.
463 		 */
464 		error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
465 		if (error) {
466 			/*
467 			 * Not enough space or we were interrupted by unmount.
468 			 * Leave the file in the unlinked set.
469 			 */
470 			ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
471 			zfs_znode_dmu_fini(zp);
472 			zfs_znode_free(zp);
473 			ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
474 			return;
475 		}
476 	}
477 
478 	/*
479 	 * If the file has extended attributes, we're going to unlink
480 	 * the xattr dir.
481 	 */
482 	error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
483 	    &xattr_obj, sizeof (xattr_obj));
484 	if (error)
485 		xattr_obj = 0;
486 
487 	acl_obj = zfs_external_acl(zp);
488 
489 	/*
490 	 * Set up the final transaction.
491 	 */
492 	tx = dmu_tx_create(os);
493 	dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
494 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
495 	if (xattr_obj)
496 		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
497 	if (acl_obj)
498 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
499 
500 	zfs_sa_upgrade_txholds(tx, zp);
501 	error = dmu_tx_assign(tx, TXG_WAIT);
502 	if (error) {
503 		/*
504 		 * Not enough space to delete the file.  Leave it in the
505 		 * unlinked set, leaking it until the fs is remounted (at
506 		 * which point we'll call zfs_unlinked_drain() to process it).
507 		 */
508 		dmu_tx_abort(tx);
509 		ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
510 		zfs_znode_dmu_fini(zp);
511 		zfs_znode_free(zp);
512 		ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
513 		return;
514 	}
515 
516 	/*
517 	 * FreeBSD's implementation of zfs_zget requires a vnode to back it.
518 	 * This means that we could end up calling into getnewvnode while
519 	 * calling zfs_rmnode as a result of a prior call to getnewvnode
520 	 * trying to clear vnodes out of the cache. If this repeats we can
521 	 * recurse enough that we overflow our stack. To avoid this, we
522 	 * avoid calling zfs_zget on the xattr znode and instead simply add
523 	 * it to the unlinked set and schedule a call to zfs_unlinked_drain.
524 	 */
525 	if (xattr_obj) {
526 		/* Add extended attribute directory to the unlinked set. */
527 		VERIFY3U(0, ==,
528 		    zap_add_int(os, zfsvfs->z_unlinkedobj, xattr_obj, tx));
529 	}
530 
531 	mutex_enter(&os->os_dsl_dataset->ds_dir->dd_activity_lock);
532 
533 	/* Remove this znode from the unlinked set */
534 	VERIFY3U(0, ==,
535 	    zap_remove_int(os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
536 
537 	if (zap_count(os, zfsvfs->z_unlinkedobj, &count) == 0 && count == 0) {
538 		cv_broadcast(&os->os_dsl_dataset->ds_dir->dd_activity_cv);
539 	}
540 
541 	mutex_exit(&os->os_dsl_dataset->ds_dir->dd_activity_lock);
542 
543 	dataset_kstats_update_nunlinked_kstat(&zfsvfs->z_kstat, 1);
544 
545 	zfs_znode_delete(zp, tx);
546 
547 	dmu_tx_commit(tx);
548 
549 	if (xattr_obj) {
550 		/*
551 		 * We're using the FreeBSD taskqueue API here instead of
552 		 * the Solaris taskq API since the FreeBSD API allows for a
553 		 * task to be enqueued multiple times but executed once.
554 		 */
555 		taskqueue_enqueue(zfsvfs_taskq->tq_queue,
556 		    &zfsvfs->z_unlinked_drain_task);
557 	}
558 }
559 
560 static uint64_t
561 zfs_dirent(znode_t *zp, uint64_t mode)
562 {
563 	uint64_t de = zp->z_id;
564 
565 	if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
566 		de |= IFTODT(mode) << 60;
567 	return (de);
568 }
569 
570 /*
571  * Link zp into dzp.  Can only fail if zp has been unlinked.
572  */
573 int
574 zfs_link_create(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
575     int flag)
576 {
577 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
578 	vnode_t *vp = ZTOV(zp);
579 	uint64_t value;
580 	int zp_is_dir = (vp->v_type == VDIR);
581 	sa_bulk_attr_t bulk[5];
582 	uint64_t mtime[2], ctime[2];
583 	int count = 0;
584 	int error;
585 
586 	if (zfsvfs->z_replay == B_FALSE) {
587 		ASSERT_VOP_ELOCKED(ZTOV(dzp), __func__);
588 		ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
589 	}
590 	if (zp_is_dir) {
591 		if (dzp->z_links >= ZFS_LINK_MAX)
592 			return (SET_ERROR(EMLINK));
593 	}
594 	if (!(flag & ZRENAMING)) {
595 		if (zp->z_unlinked) {	/* no new links to unlinked zp */
596 			ASSERT(!(flag & (ZNEW | ZEXISTS)));
597 			return (SET_ERROR(ENOENT));
598 		}
599 		if (zp->z_links >= ZFS_LINK_MAX - zp_is_dir) {
600 			return (SET_ERROR(EMLINK));
601 		}
602 		zp->z_links++;
603 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
604 		    &zp->z_links, sizeof (zp->z_links));
605 
606 	} else {
607 		ASSERT(!zp->z_unlinked);
608 	}
609 	value = zfs_dirent(zp, zp->z_mode);
610 	error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, name,
611 	    8, 1, &value, tx);
612 
613 	/*
614 	 * zap_add could fail to add the entry if it exceeds the capacity of the
615 	 * leaf-block and zap_leaf_split() failed to help.
616 	 * The caller of this routine is responsible for failing the transaction
617 	 * which will rollback the SA updates done above.
618 	 */
619 	if (error != 0) {
620 		if (!(flag & ZRENAMING) && !(flag & ZNEW))
621 			zp->z_links--;
622 		return (error);
623 	}
624 
625 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
626 	    &dzp->z_id, sizeof (dzp->z_id));
627 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
628 	    &zp->z_pflags, sizeof (zp->z_pflags));
629 
630 	if (!(flag & ZNEW)) {
631 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
632 		    ctime, sizeof (ctime));
633 		zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
634 		    ctime);
635 	}
636 	error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
637 	ASSERT0(error);
638 
639 	dzp->z_size++;
640 	dzp->z_links += zp_is_dir;
641 	count = 0;
642 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
643 	    &dzp->z_size, sizeof (dzp->z_size));
644 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
645 	    &dzp->z_links, sizeof (dzp->z_links));
646 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
647 	    mtime, sizeof (mtime));
648 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
649 	    ctime, sizeof (ctime));
650 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
651 	    &dzp->z_pflags, sizeof (dzp->z_pflags));
652 	zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime);
653 	error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
654 	ASSERT0(error);
655 	return (0);
656 }
657 
658 /*
659  * The match type in the code for this function should conform to:
660  *
661  * ------------------------------------------------------------------------
662  * fs type  | z_norm      | lookup type | match type
663  * ---------|-------------|-------------|----------------------------------
664  * CS !norm | 0           |           0 | 0 (exact)
665  * CS  norm | formX       |           0 | MT_NORMALIZE
666  * CI !norm | upper       |   !ZCIEXACT | MT_NORMALIZE
667  * CI !norm | upper       |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
668  * CI  norm | upper|formX |   !ZCIEXACT | MT_NORMALIZE
669  * CI  norm | upper|formX |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
670  * CM !norm | upper       |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
671  * CM !norm | upper       |     ZCILOOK | MT_NORMALIZE
672  * CM  norm | upper|formX |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
673  * CM  norm | upper|formX |     ZCILOOK | MT_NORMALIZE
674  *
675  * Abbreviations:
676  *    CS = Case Sensitive, CI = Case Insensitive, CM = Case Mixed
677  *    upper = case folding set by fs type on creation (U8_TEXTPREP_TOUPPER)
678  *    formX = unicode normalization form set on fs creation
679  */
680 static int
681 zfs_dropname(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
682     int flag)
683 {
684 	int error;
685 
686 	if (zp->z_zfsvfs->z_norm) {
687 		matchtype_t mt = MT_NORMALIZE;
688 
689 		if (zp->z_zfsvfs->z_case == ZFS_CASE_MIXED) {
690 			mt |= MT_MATCH_CASE;
691 		}
692 
693 		error = zap_remove_norm(zp->z_zfsvfs->z_os, dzp->z_id,
694 		    name, mt, tx);
695 	} else {
696 		error = zap_remove(zp->z_zfsvfs->z_os, dzp->z_id, name, tx);
697 	}
698 
699 	return (error);
700 }
701 
702 /*
703  * Unlink zp from dzp, and mark zp for deletion if this was the last link.
704  * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
705  * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
706  * If it's non-NULL, we use it to indicate whether the znode needs deletion,
707  * and it's the caller's job to do it.
708  */
709 int
710 zfs_link_destroy(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
711     int flag, boolean_t *unlinkedp)
712 {
713 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
714 	vnode_t *vp = ZTOV(zp);
715 	int zp_is_dir = (vp->v_type == VDIR);
716 	boolean_t unlinked = B_FALSE;
717 	sa_bulk_attr_t bulk[5];
718 	uint64_t mtime[2], ctime[2];
719 	int count = 0;
720 	int error;
721 
722 	if (zfsvfs->z_replay == B_FALSE) {
723 		ASSERT_VOP_ELOCKED(ZTOV(dzp), __func__);
724 		ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
725 	}
726 	if (!(flag & ZRENAMING)) {
727 
728 		if (zp_is_dir && !zfs_dirempty(zp))
729 			return (SET_ERROR(ENOTEMPTY));
730 
731 		/*
732 		 * If we get here, we are going to try to remove the object.
733 		 * First try removing the name from the directory; if that
734 		 * fails, return the error.
735 		 */
736 		error = zfs_dropname(dzp, name, zp, tx, flag);
737 		if (error != 0) {
738 			return (error);
739 		}
740 
741 		if (zp->z_links <= zp_is_dir) {
742 			zfs_panic_recover("zfs: link count on vnode %p is %u, "
743 			    "should be at least %u", zp->z_vnode,
744 			    (int)zp->z_links,
745 			    zp_is_dir + 1);
746 			zp->z_links = zp_is_dir + 1;
747 		}
748 		if (--zp->z_links == zp_is_dir) {
749 			zp->z_unlinked = B_TRUE;
750 			zp->z_links = 0;
751 			unlinked = B_TRUE;
752 		} else {
753 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
754 			    NULL, &ctime, sizeof (ctime));
755 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
756 			    NULL, &zp->z_pflags, sizeof (zp->z_pflags));
757 			zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
758 			    ctime);
759 		}
760 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
761 		    NULL, &zp->z_links, sizeof (zp->z_links));
762 		error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
763 		count = 0;
764 		ASSERT0(error);
765 	} else {
766 		ASSERT(!zp->z_unlinked);
767 		error = zfs_dropname(dzp, name, zp, tx, flag);
768 		if (error != 0)
769 			return (error);
770 	}
771 
772 	dzp->z_size--;		/* one dirent removed */
773 	dzp->z_links -= zp_is_dir;	/* ".." link from zp */
774 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
775 	    NULL, &dzp->z_links, sizeof (dzp->z_links));
776 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
777 	    NULL, &dzp->z_size, sizeof (dzp->z_size));
778 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
779 	    NULL, ctime, sizeof (ctime));
780 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
781 	    NULL, mtime, sizeof (mtime));
782 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
783 	    NULL, &dzp->z_pflags, sizeof (dzp->z_pflags));
784 	zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime);
785 	error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
786 	ASSERT0(error);
787 
788 	if (unlinkedp != NULL)
789 		*unlinkedp = unlinked;
790 	else if (unlinked)
791 		zfs_unlinked_add(zp, tx);
792 
793 	return (0);
794 }
795 
796 /*
797  * Indicate whether the directory is empty.
798  */
799 boolean_t
800 zfs_dirempty(znode_t *dzp)
801 {
802 	return (dzp->z_size == 2);
803 }
804 
805 int
806 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, znode_t **xvpp, cred_t *cr)
807 {
808 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
809 	znode_t *xzp;
810 	dmu_tx_t *tx;
811 	int error;
812 	zfs_acl_ids_t acl_ids;
813 	boolean_t fuid_dirtied;
814 	uint64_t parent __maybe_unused;
815 
816 	*xvpp = NULL;
817 
818 	if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL,
819 	    &acl_ids, NULL)) != 0)
820 		return (error);
821 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, 0)) {
822 		zfs_acl_ids_free(&acl_ids);
823 		return (SET_ERROR(EDQUOT));
824 	}
825 
826 	getnewvnode_reserve_();
827 
828 	tx = dmu_tx_create(zfsvfs->z_os);
829 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
830 	    ZFS_SA_BASE_ATTR_SIZE);
831 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
832 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
833 	fuid_dirtied = zfsvfs->z_fuid_dirty;
834 	if (fuid_dirtied)
835 		zfs_fuid_txhold(zfsvfs, tx);
836 	error = dmu_tx_assign(tx, TXG_WAIT);
837 	if (error) {
838 		zfs_acl_ids_free(&acl_ids);
839 		dmu_tx_abort(tx);
840 		getnewvnode_drop_reserve();
841 		return (error);
842 	}
843 	zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, &acl_ids);
844 
845 	if (fuid_dirtied)
846 		zfs_fuid_sync(zfsvfs, tx);
847 
848 	ASSERT0(sa_lookup(xzp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), &parent,
849 	    sizeof (parent)));
850 	ASSERT3U(parent, ==, zp->z_id);
851 
852 	VERIFY0(sa_update(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xzp->z_id,
853 	    sizeof (xzp->z_id), tx));
854 
855 	zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp, xzp, "", NULL,
856 	    acl_ids.z_fuidp, vap);
857 
858 	zfs_acl_ids_free(&acl_ids);
859 	dmu_tx_commit(tx);
860 
861 	getnewvnode_drop_reserve();
862 
863 	*xvpp = xzp;
864 
865 	return (0);
866 }
867 
868 /*
869  * Return a znode for the extended attribute directory for zp.
870  * ** If the directory does not already exist, it is created **
871  *
872  *	IN:	zp	- znode to obtain attribute directory from
873  *		cr	- credentials of caller
874  *		flags	- flags from the VOP_LOOKUP call
875  *
876  *	OUT:	xzpp	- pointer to extended attribute znode
877  *
878  *	RETURN:	0 on success
879  *		error number on failure
880  */
881 int
882 zfs_get_xattrdir(znode_t *zp, znode_t **xzpp, cred_t *cr, int flags)
883 {
884 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
885 	znode_t		*xzp;
886 	vattr_t		va;
887 	int		error;
888 top:
889 	error = zfs_dirent_lookup(zp, "", &xzp, ZXATTR);
890 	if (error)
891 		return (error);
892 
893 	if (xzp != NULL) {
894 		*xzpp = xzp;
895 		return (0);
896 	}
897 
898 
899 	if (!(flags & CREATE_XATTR_DIR))
900 		return (SET_ERROR(ENOATTR));
901 
902 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
903 		return (SET_ERROR(EROFS));
904 	}
905 
906 	/*
907 	 * The ability to 'create' files in an attribute
908 	 * directory comes from the write_xattr permission on the base file.
909 	 *
910 	 * The ability to 'search' an attribute directory requires
911 	 * read_xattr permission on the base file.
912 	 *
913 	 * Once in a directory the ability to read/write attributes
914 	 * is controlled by the permissions on the attribute file.
915 	 */
916 	va.va_mask = AT_MODE | AT_UID | AT_GID;
917 	va.va_type = VDIR;
918 	va.va_mode = S_IFDIR | S_ISVTX | 0777;
919 	zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
920 
921 	error = zfs_make_xattrdir(zp, &va, xzpp, cr);
922 
923 	if (error == ERESTART) {
924 		/* NB: we already did dmu_tx_wait() if necessary */
925 		goto top;
926 	}
927 	if (error == 0)
928 		VOP_UNLOCK1(ZTOV(*xzpp));
929 
930 	return (error);
931 }
932 
933 /*
934  * Decide whether it is okay to remove within a sticky directory.
935  *
936  * In sticky directories, write access is not sufficient;
937  * you can remove entries from a directory only if:
938  *
939  *	you own the directory,
940  *	you own the entry,
941  *	the entry is a plain file and you have write access,
942  *	or you are privileged (checked in secpolicy...).
943  *
944  * The function returns 0 if remove access is granted.
945  */
946 int
947 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
948 {
949 	uid_t  		uid;
950 	uid_t		downer;
951 	uid_t		fowner;
952 	zfsvfs_t	*zfsvfs = zdp->z_zfsvfs;
953 
954 	if (zdp->z_zfsvfs->z_replay)
955 		return (0);
956 
957 	if ((zdp->z_mode & S_ISVTX) == 0)
958 		return (0);
959 
960 	downer = zfs_fuid_map_id(zfsvfs, zdp->z_uid, cr, ZFS_OWNER);
961 	fowner = zfs_fuid_map_id(zfsvfs, zp->z_uid, cr, ZFS_OWNER);
962 
963 	if ((uid = crgetuid(cr)) == downer || uid == fowner ||
964 	    (ZTOV(zp)->v_type == VREG &&
965 	    zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr, NULL) == 0))
966 		return (0);
967 	else
968 		return (secpolicy_vnode_remove(ZTOV(zp), cr));
969 }
970