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 */
68static int
69zfs_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 */
110int
111zfs_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
200static int
201zfs_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
225int
226zfs_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 */
269void
270zfs_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 */
286void
287zfs_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 */
367static int
368zfs_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
421extern taskq_t *zfsvfs_taskq;
422
423void
424zfs_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	zfs_znode_free(zp);
547
548	dmu_tx_commit(tx);
549
550	if (xattr_obj) {
551		/*
552		 * We're using the FreeBSD taskqueue API here instead of
553		 * the Solaris taskq API since the FreeBSD API allows for a
554		 * task to be enqueued multiple times but executed once.
555		 */
556		taskqueue_enqueue(zfsvfs_taskq->tq_queue,
557		    &zfsvfs->z_unlinked_drain_task);
558	}
559}
560
561static uint64_t
562zfs_dirent(znode_t *zp, uint64_t mode)
563{
564	uint64_t de = zp->z_id;
565
566	if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
567		de |= IFTODT(mode) << 60;
568	return (de);
569}
570
571/*
572 * Link zp into dzp.  Can only fail if zp has been unlinked.
573 */
574int
575zfs_link_create(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
576    int flag)
577{
578	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
579	vnode_t *vp = ZTOV(zp);
580	uint64_t value;
581	int zp_is_dir = (vp->v_type == VDIR);
582	sa_bulk_attr_t bulk[5];
583	uint64_t mtime[2], ctime[2];
584	int count = 0;
585	int error;
586
587	if (zfsvfs->z_replay == B_FALSE) {
588		ASSERT_VOP_ELOCKED(ZTOV(dzp), __func__);
589		ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
590	}
591	if (zp_is_dir) {
592		if (dzp->z_links >= ZFS_LINK_MAX)
593			return (SET_ERROR(EMLINK));
594	}
595	if (!(flag & ZRENAMING)) {
596		if (zp->z_unlinked) {	/* no new links to unlinked zp */
597			ASSERT(!(flag & (ZNEW | ZEXISTS)));
598			return (SET_ERROR(ENOENT));
599		}
600		if (zp->z_links >= ZFS_LINK_MAX - zp_is_dir) {
601			return (SET_ERROR(EMLINK));
602		}
603		zp->z_links++;
604		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
605		    &zp->z_links, sizeof (zp->z_links));
606
607	} else {
608		ASSERT(!zp->z_unlinked);
609	}
610	value = zfs_dirent(zp, zp->z_mode);
611	error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, name,
612	    8, 1, &value, tx);
613
614	/*
615	 * zap_add could fail to add the entry if it exceeds the capacity of the
616	 * leaf-block and zap_leaf_split() failed to help.
617	 * The caller of this routine is responsible for failing the transaction
618	 * which will rollback the SA updates done above.
619	 */
620	if (error != 0) {
621		if (!(flag & ZRENAMING) && !(flag & ZNEW))
622			zp->z_links--;
623		return (error);
624	}
625
626	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
627	    &dzp->z_id, sizeof (dzp->z_id));
628	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
629	    &zp->z_pflags, sizeof (zp->z_pflags));
630
631	if (!(flag & ZNEW)) {
632		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
633		    ctime, sizeof (ctime));
634		zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
635		    ctime);
636	}
637	error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
638	ASSERT0(error);
639
640	dzp->z_size++;
641	dzp->z_links += zp_is_dir;
642	count = 0;
643	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
644	    &dzp->z_size, sizeof (dzp->z_size));
645	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
646	    &dzp->z_links, sizeof (dzp->z_links));
647	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
648	    mtime, sizeof (mtime));
649	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
650	    ctime, sizeof (ctime));
651	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
652	    &dzp->z_pflags, sizeof (dzp->z_pflags));
653	zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime);
654	error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
655	ASSERT0(error);
656	return (0);
657}
658
659/*
660 * The match type in the code for this function should conform to:
661 *
662 * ------------------------------------------------------------------------
663 * fs type  | z_norm      | lookup type | match type
664 * ---------|-------------|-------------|----------------------------------
665 * CS !norm | 0           |           0 | 0 (exact)
666 * CS  norm | formX       |           0 | MT_NORMALIZE
667 * CI !norm | upper       |   !ZCIEXACT | MT_NORMALIZE
668 * CI !norm | upper       |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
669 * CI  norm | upper|formX |   !ZCIEXACT | MT_NORMALIZE
670 * CI  norm | upper|formX |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
671 * CM !norm | upper       |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
672 * CM !norm | upper       |     ZCILOOK | MT_NORMALIZE
673 * CM  norm | upper|formX |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
674 * CM  norm | upper|formX |     ZCILOOK | MT_NORMALIZE
675 *
676 * Abbreviations:
677 *    CS = Case Sensitive, CI = Case Insensitive, CM = Case Mixed
678 *    upper = case folding set by fs type on creation (U8_TEXTPREP_TOUPPER)
679 *    formX = unicode normalization form set on fs creation
680 */
681static int
682zfs_dropname(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
683    int flag)
684{
685	int error;
686
687	if (zp->z_zfsvfs->z_norm) {
688		matchtype_t mt = MT_NORMALIZE;
689
690		if (zp->z_zfsvfs->z_case == ZFS_CASE_MIXED) {
691			mt |= MT_MATCH_CASE;
692		}
693
694		error = zap_remove_norm(zp->z_zfsvfs->z_os, dzp->z_id,
695		    name, mt, tx);
696	} else {
697		error = zap_remove(zp->z_zfsvfs->z_os, dzp->z_id, name, tx);
698	}
699
700	return (error);
701}
702
703/*
704 * Unlink zp from dzp, and mark zp for deletion if this was the last link.
705 * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
706 * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
707 * If it's non-NULL, we use it to indicate whether the znode needs deletion,
708 * and it's the caller's job to do it.
709 */
710int
711zfs_link_destroy(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
712    int flag, boolean_t *unlinkedp)
713{
714	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
715	vnode_t *vp = ZTOV(zp);
716	int zp_is_dir = (vp->v_type == VDIR);
717	boolean_t unlinked = B_FALSE;
718	sa_bulk_attr_t bulk[5];
719	uint64_t mtime[2], ctime[2];
720	int count = 0;
721	int error;
722
723	if (zfsvfs->z_replay == B_FALSE) {
724		ASSERT_VOP_ELOCKED(ZTOV(dzp), __func__);
725		ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
726	}
727	if (!(flag & ZRENAMING)) {
728
729		if (zp_is_dir && !zfs_dirempty(zp))
730			return (SET_ERROR(ENOTEMPTY));
731
732		/*
733		 * If we get here, we are going to try to remove the object.
734		 * First try removing the name from the directory; if that
735		 * fails, return the error.
736		 */
737		error = zfs_dropname(dzp, name, zp, tx, flag);
738		if (error != 0) {
739			return (error);
740		}
741
742		if (zp->z_links <= zp_is_dir) {
743			zfs_panic_recover("zfs: link count on vnode %p is %u, "
744			    "should be at least %u", zp->z_vnode,
745			    (int)zp->z_links,
746			    zp_is_dir + 1);
747			zp->z_links = zp_is_dir + 1;
748		}
749		if (--zp->z_links == zp_is_dir) {
750			zp->z_unlinked = B_TRUE;
751			zp->z_links = 0;
752			unlinked = B_TRUE;
753		} else {
754			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
755			    NULL, &ctime, sizeof (ctime));
756			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
757			    NULL, &zp->z_pflags, sizeof (zp->z_pflags));
758			zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
759			    ctime);
760		}
761		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
762		    NULL, &zp->z_links, sizeof (zp->z_links));
763		error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
764		count = 0;
765		ASSERT0(error);
766	} else {
767		ASSERT(!zp->z_unlinked);
768		error = zfs_dropname(dzp, name, zp, tx, flag);
769		if (error != 0)
770			return (error);
771	}
772
773	dzp->z_size--;		/* one dirent removed */
774	dzp->z_links -= zp_is_dir;	/* ".." link from zp */
775	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
776	    NULL, &dzp->z_links, sizeof (dzp->z_links));
777	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
778	    NULL, &dzp->z_size, sizeof (dzp->z_size));
779	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
780	    NULL, ctime, sizeof (ctime));
781	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
782	    NULL, mtime, sizeof (mtime));
783	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
784	    NULL, &dzp->z_pflags, sizeof (dzp->z_pflags));
785	zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime);
786	error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
787	ASSERT0(error);
788
789	if (unlinkedp != NULL)
790		*unlinkedp = unlinked;
791	else if (unlinked)
792		zfs_unlinked_add(zp, tx);
793
794	return (0);
795}
796
797/*
798 * Indicate whether the directory is empty.
799 */
800boolean_t
801zfs_dirempty(znode_t *dzp)
802{
803	return (dzp->z_size == 2);
804}
805
806int
807zfs_make_xattrdir(znode_t *zp, vattr_t *vap, znode_t **xvpp, cred_t *cr)
808{
809	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
810	znode_t *xzp;
811	dmu_tx_t *tx;
812	int error;
813	zfs_acl_ids_t acl_ids;
814	boolean_t fuid_dirtied;
815	uint64_t parent __maybe_unused;
816
817	*xvpp = NULL;
818
819	if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL,
820	    &acl_ids, NULL)) != 0)
821		return (error);
822	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, 0)) {
823		zfs_acl_ids_free(&acl_ids);
824		return (SET_ERROR(EDQUOT));
825	}
826
827	getnewvnode_reserve_();
828
829	tx = dmu_tx_create(zfsvfs->z_os);
830	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
831	    ZFS_SA_BASE_ATTR_SIZE);
832	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
833	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
834	fuid_dirtied = zfsvfs->z_fuid_dirty;
835	if (fuid_dirtied)
836		zfs_fuid_txhold(zfsvfs, tx);
837	error = dmu_tx_assign(tx, TXG_WAIT);
838	if (error) {
839		zfs_acl_ids_free(&acl_ids);
840		dmu_tx_abort(tx);
841		getnewvnode_drop_reserve();
842		return (error);
843	}
844	zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, &acl_ids);
845
846	if (fuid_dirtied)
847		zfs_fuid_sync(zfsvfs, tx);
848
849	ASSERT0(sa_lookup(xzp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), &parent,
850	    sizeof (parent)));
851	ASSERT3U(parent, ==, zp->z_id);
852
853	VERIFY0(sa_update(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xzp->z_id,
854	    sizeof (xzp->z_id), tx));
855
856	zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp, xzp, "", NULL,
857	    acl_ids.z_fuidp, vap);
858
859	zfs_acl_ids_free(&acl_ids);
860	dmu_tx_commit(tx);
861
862	getnewvnode_drop_reserve();
863
864	*xvpp = xzp;
865
866	return (0);
867}
868
869/*
870 * Return a znode for the extended attribute directory for zp.
871 * ** If the directory does not already exist, it is created **
872 *
873 *	IN:	zp	- znode to obtain attribute directory from
874 *		cr	- credentials of caller
875 *		flags	- flags from the VOP_LOOKUP call
876 *
877 *	OUT:	xzpp	- pointer to extended attribute znode
878 *
879 *	RETURN:	0 on success
880 *		error number on failure
881 */
882int
883zfs_get_xattrdir(znode_t *zp, znode_t **xzpp, cred_t *cr, int flags)
884{
885	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
886	znode_t		*xzp;
887	vattr_t		va;
888	int		error;
889top:
890	error = zfs_dirent_lookup(zp, "", &xzp, ZXATTR);
891	if (error)
892		return (error);
893
894	if (xzp != NULL) {
895		*xzpp = xzp;
896		return (0);
897	}
898
899
900	if (!(flags & CREATE_XATTR_DIR))
901		return (SET_ERROR(ENOATTR));
902
903	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
904		return (SET_ERROR(EROFS));
905	}
906
907	/*
908	 * The ability to 'create' files in an attribute
909	 * directory comes from the write_xattr permission on the base file.
910	 *
911	 * The ability to 'search' an attribute directory requires
912	 * read_xattr permission on the base file.
913	 *
914	 * Once in a directory the ability to read/write attributes
915	 * is controlled by the permissions on the attribute file.
916	 */
917	va.va_mask = AT_MODE | AT_UID | AT_GID;
918	va.va_type = VDIR;
919	va.va_mode = S_IFDIR | S_ISVTX | 0777;
920	zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
921
922	error = zfs_make_xattrdir(zp, &va, xzpp, cr);
923
924	if (error == ERESTART) {
925		/* NB: we already did dmu_tx_wait() if necessary */
926		goto top;
927	}
928	if (error == 0)
929		VOP_UNLOCK1(ZTOV(*xzpp));
930
931	return (error);
932}
933
934/*
935 * Decide whether it is okay to remove within a sticky directory.
936 *
937 * In sticky directories, write access is not sufficient;
938 * you can remove entries from a directory only if:
939 *
940 *	you own the directory,
941 *	you own the entry,
942 *	the entry is a plain file and you have write access,
943 *	or you are privileged (checked in secpolicy...).
944 *
945 * The function returns 0 if remove access is granted.
946 */
947int
948zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
949{
950	uid_t  		uid;
951	uid_t		downer;
952	uid_t		fowner;
953	zfsvfs_t	*zfsvfs = zdp->z_zfsvfs;
954
955	if (zdp->z_zfsvfs->z_replay)
956		return (0);
957
958	if ((zdp->z_mode & S_ISVTX) == 0)
959		return (0);
960
961	downer = zfs_fuid_map_id(zfsvfs, zdp->z_uid, cr, ZFS_OWNER);
962	fowner = zfs_fuid_map_id(zfsvfs, zp->z_uid, cr, ZFS_OWNER);
963
964	if ((uid = crgetuid(cr)) == downer || uid == fowner ||
965	    (ZTOV(zp)->v_type == VREG &&
966	    zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr, NULL) == 0))
967		return (0);
968	else
969		return (secpolicy_vnode_remove(ZTOV(zp), cr));
970}
971