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) 2012, 2015 by Delphix. All rights reserved.
25  * Copyright (c) 2014 Integros [integros.com]
26  * Copyright 2017 Nexenta Systems, Inc.
27  */
28 
29 /* Portions Copyright 2007 Jeremy Teo */
30 /* Portions Copyright 2010 Robert Milkowski */
31 
32 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <sys/systm.h>
35 #include <sys/sysmacros.h>
36 #include <sys/resource.h>
37 #include <security/mac/mac_framework.h>
38 #include <sys/vfs.h>
39 #include <sys/endian.h>
40 #include <sys/vm.h>
41 #include <sys/vnode.h>
42 #if __FreeBSD_version >= 1300102
43 #include <sys/smr.h>
44 #endif
45 #include <sys/dirent.h>
46 #include <sys/file.h>
47 #include <sys/stat.h>
48 #include <sys/kmem.h>
49 #include <sys/taskq.h>
50 #include <sys/uio.h>
51 #include <sys/atomic.h>
52 #include <sys/namei.h>
53 #include <sys/mman.h>
54 #include <sys/cmn_err.h>
55 #include <sys/kdb.h>
56 #include <sys/sysproto.h>
57 #include <sys/errno.h>
58 #include <sys/unistd.h>
59 #include <sys/zfs_dir.h>
60 #include <sys/zfs_ioctl.h>
61 #include <sys/fs/zfs.h>
62 #include <sys/dmu.h>
63 #include <sys/dmu_objset.h>
64 #include <sys/spa.h>
65 #include <sys/txg.h>
66 #include <sys/dbuf.h>
67 #include <sys/zap.h>
68 #include <sys/sa.h>
69 #include <sys/policy.h>
70 #include <sys/sunddi.h>
71 #include <sys/filio.h>
72 #include <sys/sid.h>
73 #include <sys/zfs_ctldir.h>
74 #include <sys/zfs_fuid.h>
75 #include <sys/zfs_quota.h>
76 #include <sys/zfs_sa.h>
77 #include <sys/zfs_rlock.h>
78 #include <sys/bio.h>
79 #include <sys/buf.h>
80 #include <sys/sched.h>
81 #include <sys/acl.h>
82 #include <sys/vmmeter.h>
83 #include <vm/vm_param.h>
84 #include <sys/zil.h>
85 #include <sys/zfs_vnops.h>
86 #include <sys/module.h>
87 #include <sys/sysent.h>
88 #include <sys/dmu_impl.h>
89 #include <sys/brt.h>
90 #include <sys/zfeature.h>
91 
92 #include <vm/vm_object.h>
93 
94 #include <sys/extattr.h>
95 #include <sys/priv.h>
96 
97 #ifndef VN_OPEN_INVFS
98 #define	VN_OPEN_INVFS	0x0
99 #endif
100 
101 VFS_SMR_DECLARE;
102 
103 #if __FreeBSD_version < 1300103
104 #define	NDFREE_PNBUF(ndp)	NDFREE((ndp), NDF_ONLY_PNBUF)
105 #endif
106 
107 #if __FreeBSD_version >= 1300047
108 #define	vm_page_wire_lock(pp)
109 #define	vm_page_wire_unlock(pp)
110 #else
111 #define	vm_page_wire_lock(pp) vm_page_lock(pp)
112 #define	vm_page_wire_unlock(pp) vm_page_unlock(pp)
113 #endif
114 
115 #ifdef DEBUG_VFS_LOCKS
116 #define	VNCHECKREF(vp)				  \
117 	VNASSERT((vp)->v_holdcnt > 0 && (vp)->v_usecount > 0, vp,	\
118 	    ("%s: wrong ref counts", __func__));
119 #else
120 #define	VNCHECKREF(vp)
121 #endif
122 
123 #if __FreeBSD_version >= 1400045
124 typedef uint64_t cookie_t;
125 #else
126 typedef ulong_t cookie_t;
127 #endif
128 
129 /*
130  * Programming rules.
131  *
132  * Each vnode op performs some logical unit of work.  To do this, the ZPL must
133  * properly lock its in-core state, create a DMU transaction, do the work,
134  * record this work in the intent log (ZIL), commit the DMU transaction,
135  * and wait for the intent log to commit if it is a synchronous operation.
136  * Moreover, the vnode ops must work in both normal and log replay context.
137  * The ordering of events is important to avoid deadlocks and references
138  * to freed memory.  The example below illustrates the following Big Rules:
139  *
140  *  (1)	A check must be made in each zfs thread for a mounted file system.
141  *	This is done avoiding races using zfs_enter(zfsvfs).
142  *	A zfs_exit(zfsvfs) is needed before all returns.  Any znodes
143  *	must be checked with zfs_verify_zp(zp).  Both of these macros
144  *	can return EIO from the calling function.
145  *
146  *  (2)	VN_RELE() should always be the last thing except for zil_commit()
147  *	(if necessary) and zfs_exit(). This is for 3 reasons:
148  *	First, if it's the last reference, the vnode/znode
149  *	can be freed, so the zp may point to freed memory.  Second, the last
150  *	reference will call zfs_zinactive(), which may induce a lot of work --
151  *	pushing cached pages (which acquires range locks) and syncing out
152  *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
153  *	which could deadlock the system if you were already holding one.
154  *	If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
155  *
156  *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
157  *	as they can span dmu_tx_assign() calls.
158  *
159  *  (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
160  *      dmu_tx_assign().  This is critical because we don't want to block
161  *      while holding locks.
162  *
163  *	If no ZPL locks are held (aside from zfs_enter()), use TXG_WAIT.  This
164  *	reduces lock contention and CPU usage when we must wait (note that if
165  *	throughput is constrained by the storage, nearly every transaction
166  *	must wait).
167  *
168  *      Note, in particular, that if a lock is sometimes acquired before
169  *      the tx assigns, and sometimes after (e.g. z_lock), then failing
170  *      to use a non-blocking assign can deadlock the system.  The scenario:
171  *
172  *	Thread A has grabbed a lock before calling dmu_tx_assign().
173  *	Thread B is in an already-assigned tx, and blocks for this lock.
174  *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
175  *	forever, because the previous txg can't quiesce until B's tx commits.
176  *
177  *	If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
178  *	then drop all locks, call dmu_tx_wait(), and try again.  On subsequent
179  *	calls to dmu_tx_assign(), pass TXG_NOTHROTTLE in addition to TXG_NOWAIT,
180  *	to indicate that this operation has already called dmu_tx_wait().
181  *	This will ensure that we don't retry forever, waiting a short bit
182  *	each time.
183  *
184  *  (5)	If the operation succeeded, generate the intent log entry for it
185  *	before dropping locks.  This ensures that the ordering of events
186  *	in the intent log matches the order in which they actually occurred.
187  *	During ZIL replay the zfs_log_* functions will update the sequence
188  *	number to indicate the zil transaction has replayed.
189  *
190  *  (6)	At the end of each vnode op, the DMU tx must always commit,
191  *	regardless of whether there were any errors.
192  *
193  *  (7)	After dropping all locks, invoke zil_commit(zilog, foid)
194  *	to ensure that synchronous semantics are provided when necessary.
195  *
196  * In general, this is how things should be ordered in each vnode op:
197  *
198  *	zfs_enter(zfsvfs);		// exit if unmounted
199  * top:
200  *	zfs_dirent_lookup(&dl, ...)	// lock directory entry (may VN_HOLD())
201  *	rw_enter(...);			// grab any other locks you need
202  *	tx = dmu_tx_create(...);	// get DMU tx
203  *	dmu_tx_hold_*();		// hold each object you might modify
204  *	error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
205  *	if (error) {
206  *		rw_exit(...);		// drop locks
207  *		zfs_dirent_unlock(dl);	// unlock directory entry
208  *		VN_RELE(...);		// release held vnodes
209  *		if (error == ERESTART) {
210  *			waited = B_TRUE;
211  *			dmu_tx_wait(tx);
212  *			dmu_tx_abort(tx);
213  *			goto top;
214  *		}
215  *		dmu_tx_abort(tx);	// abort DMU tx
216  *		zfs_exit(zfsvfs);	// finished in zfs
217  *		return (error);		// really out of space
218  *	}
219  *	error = do_real_work();		// do whatever this VOP does
220  *	if (error == 0)
221  *		zfs_log_*(...);		// on success, make ZIL entry
222  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
223  *	rw_exit(...);			// drop locks
224  *	zfs_dirent_unlock(dl);		// unlock directory entry
225  *	VN_RELE(...);			// release held vnodes
226  *	zil_commit(zilog, foid);	// synchronous when necessary
227  *	zfs_exit(zfsvfs);		// finished in zfs
228  *	return (error);			// done, report error
229  */
230 static int
zfs_open(vnode_t ** vpp,int flag,cred_t * cr)231 zfs_open(vnode_t **vpp, int flag, cred_t *cr)
232 {
233 	(void) cr;
234 	znode_t	*zp = VTOZ(*vpp);
235 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
236 	int error;
237 
238 	if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
239 		return (error);
240 
241 	if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
242 	    ((flag & FAPPEND) == 0)) {
243 		zfs_exit(zfsvfs, FTAG);
244 		return (SET_ERROR(EPERM));
245 	}
246 
247 	/*
248 	 * Keep a count of the synchronous opens in the znode.  On first
249 	 * synchronous open we must convert all previous async transactions
250 	 * into sync to keep correct ordering.
251 	 */
252 	if (flag & O_SYNC) {
253 		if (atomic_inc_32_nv(&zp->z_sync_cnt) == 1)
254 			zil_async_to_sync(zfsvfs->z_log, zp->z_id);
255 	}
256 
257 	zfs_exit(zfsvfs, FTAG);
258 	return (0);
259 }
260 
261 static int
zfs_close(vnode_t * vp,int flag,int count,offset_t offset,cred_t * cr)262 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr)
263 {
264 	(void) offset, (void) cr;
265 	znode_t	*zp = VTOZ(vp);
266 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
267 	int error;
268 
269 	if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
270 		return (error);
271 
272 	/* Decrement the synchronous opens in the znode */
273 	if ((flag & O_SYNC) && (count == 1))
274 		atomic_dec_32(&zp->z_sync_cnt);
275 
276 	zfs_exit(zfsvfs, FTAG);
277 	return (0);
278 }
279 
280 static int
zfs_ioctl(vnode_t * vp,ulong_t com,intptr_t data,int flag,cred_t * cred,int * rvalp)281 zfs_ioctl(vnode_t *vp, ulong_t com, intptr_t data, int flag, cred_t *cred,
282     int *rvalp)
283 {
284 	(void) flag, (void) cred, (void) rvalp;
285 	loff_t off;
286 	int error;
287 
288 	switch (com) {
289 	case _FIOFFS:
290 	{
291 		return (0);
292 
293 		/*
294 		 * The following two ioctls are used by bfu.  Faking out,
295 		 * necessary to avoid bfu errors.
296 		 */
297 	}
298 	case _FIOGDIO:
299 	case _FIOSDIO:
300 	{
301 		return (0);
302 	}
303 
304 	case F_SEEK_DATA:
305 	case F_SEEK_HOLE:
306 	{
307 		off = *(offset_t *)data;
308 		/* offset parameter is in/out */
309 		error = zfs_holey(VTOZ(vp), com, &off);
310 		if (error)
311 			return (error);
312 		*(offset_t *)data = off;
313 		return (0);
314 	}
315 	}
316 	return (SET_ERROR(ENOTTY));
317 }
318 
319 static vm_page_t
page_busy(vnode_t * vp,int64_t start,int64_t off,int64_t nbytes)320 page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
321 {
322 	vm_object_t obj;
323 	vm_page_t pp;
324 	int64_t end;
325 
326 	/*
327 	 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE
328 	 * aligned boundaries, if the range is not aligned.  As a result a
329 	 * DEV_BSIZE subrange with partially dirty data may get marked as clean.
330 	 * It may happen that all DEV_BSIZE subranges are marked clean and thus
331 	 * the whole page would be considered clean despite have some
332 	 * dirty data.
333 	 * For this reason we should shrink the range to DEV_BSIZE aligned
334 	 * boundaries before calling vm_page_clear_dirty.
335 	 */
336 	end = rounddown2(off + nbytes, DEV_BSIZE);
337 	off = roundup2(off, DEV_BSIZE);
338 	nbytes = end - off;
339 
340 	obj = vp->v_object;
341 	zfs_vmobject_assert_wlocked_12(obj);
342 #if __FreeBSD_version < 1300050
343 	for (;;) {
344 		if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
345 		    pp->valid) {
346 			if (vm_page_xbusied(pp)) {
347 				/*
348 				 * Reference the page before unlocking and
349 				 * sleeping so that the page daemon is less
350 				 * likely to reclaim it.
351 				 */
352 				vm_page_reference(pp);
353 				vm_page_lock(pp);
354 				zfs_vmobject_wunlock(obj);
355 				vm_page_busy_sleep(pp, "zfsmwb", true);
356 				zfs_vmobject_wlock(obj);
357 				continue;
358 			}
359 			vm_page_sbusy(pp);
360 		} else if (pp != NULL) {
361 			ASSERT(!pp->valid);
362 			pp = NULL;
363 		}
364 		if (pp != NULL) {
365 			ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
366 			vm_object_pip_add(obj, 1);
367 			pmap_remove_write(pp);
368 			if (nbytes != 0)
369 				vm_page_clear_dirty(pp, off, nbytes);
370 		}
371 		break;
372 	}
373 #else
374 	vm_page_grab_valid_unlocked(&pp, obj, OFF_TO_IDX(start),
375 	    VM_ALLOC_NOCREAT | VM_ALLOC_SBUSY | VM_ALLOC_NORMAL |
376 	    VM_ALLOC_IGN_SBUSY);
377 	if (pp != NULL) {
378 		ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
379 		vm_object_pip_add(obj, 1);
380 		pmap_remove_write(pp);
381 		if (nbytes != 0)
382 			vm_page_clear_dirty(pp, off, nbytes);
383 	}
384 #endif
385 	return (pp);
386 }
387 
388 static void
page_unbusy(vm_page_t pp)389 page_unbusy(vm_page_t pp)
390 {
391 
392 	vm_page_sunbusy(pp);
393 #if __FreeBSD_version >= 1300041
394 	vm_object_pip_wakeup(pp->object);
395 #else
396 	vm_object_pip_subtract(pp->object, 1);
397 #endif
398 }
399 
400 #if __FreeBSD_version > 1300051
401 static vm_page_t
page_hold(vnode_t * vp,int64_t start)402 page_hold(vnode_t *vp, int64_t start)
403 {
404 	vm_object_t obj;
405 	vm_page_t m;
406 
407 	obj = vp->v_object;
408 	vm_page_grab_valid_unlocked(&m, obj, OFF_TO_IDX(start),
409 	    VM_ALLOC_NOCREAT | VM_ALLOC_WIRED | VM_ALLOC_IGN_SBUSY |
410 	    VM_ALLOC_NOBUSY);
411 	return (m);
412 }
413 #else
414 static vm_page_t
page_hold(vnode_t * vp,int64_t start)415 page_hold(vnode_t *vp, int64_t start)
416 {
417 	vm_object_t obj;
418 	vm_page_t pp;
419 
420 	obj = vp->v_object;
421 	zfs_vmobject_assert_wlocked(obj);
422 
423 	for (;;) {
424 		if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
425 		    pp->valid) {
426 			if (vm_page_xbusied(pp)) {
427 				/*
428 				 * Reference the page before unlocking and
429 				 * sleeping so that the page daemon is less
430 				 * likely to reclaim it.
431 				 */
432 				vm_page_reference(pp);
433 				vm_page_lock(pp);
434 				zfs_vmobject_wunlock(obj);
435 				vm_page_busy_sleep(pp, "zfsmwb", true);
436 				zfs_vmobject_wlock(obj);
437 				continue;
438 			}
439 
440 			ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
441 			vm_page_wire_lock(pp);
442 			vm_page_hold(pp);
443 			vm_page_wire_unlock(pp);
444 
445 		} else
446 			pp = NULL;
447 		break;
448 	}
449 	return (pp);
450 }
451 #endif
452 
453 static void
page_unhold(vm_page_t pp)454 page_unhold(vm_page_t pp)
455 {
456 
457 	vm_page_wire_lock(pp);
458 #if __FreeBSD_version >= 1300035
459 	vm_page_unwire(pp, PQ_ACTIVE);
460 #else
461 	vm_page_unhold(pp);
462 #endif
463 	vm_page_wire_unlock(pp);
464 }
465 
466 /*
467  * When a file is memory mapped, we must keep the IO data synchronized
468  * between the DMU cache and the memory mapped pages.  What this means:
469  *
470  * On Write:	If we find a memory mapped page, we write to *both*
471  *		the page and the dmu buffer.
472  */
473 void
update_pages(znode_t * zp,int64_t start,int len,objset_t * os)474 update_pages(znode_t *zp, int64_t start, int len, objset_t *os)
475 {
476 	vm_object_t obj;
477 	struct sf_buf *sf;
478 	vnode_t *vp = ZTOV(zp);
479 	caddr_t va;
480 	int off;
481 
482 	ASSERT3P(vp->v_mount, !=, NULL);
483 	obj = vp->v_object;
484 	ASSERT3P(obj, !=, NULL);
485 
486 	off = start & PAGEOFFSET;
487 	zfs_vmobject_wlock_12(obj);
488 #if __FreeBSD_version >= 1300041
489 	vm_object_pip_add(obj, 1);
490 #endif
491 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
492 		vm_page_t pp;
493 		int nbytes = imin(PAGESIZE - off, len);
494 
495 		if ((pp = page_busy(vp, start, off, nbytes)) != NULL) {
496 			zfs_vmobject_wunlock_12(obj);
497 
498 			va = zfs_map_page(pp, &sf);
499 			(void) dmu_read(os, zp->z_id, start + off, nbytes,
500 			    va + off, DMU_READ_PREFETCH);
501 			zfs_unmap_page(sf);
502 
503 			zfs_vmobject_wlock_12(obj);
504 			page_unbusy(pp);
505 		}
506 		len -= nbytes;
507 		off = 0;
508 	}
509 #if __FreeBSD_version >= 1300041
510 	vm_object_pip_wakeup(obj);
511 #else
512 	vm_object_pip_wakeupn(obj, 0);
513 #endif
514 	zfs_vmobject_wunlock_12(obj);
515 }
516 
517 /*
518  * Read with UIO_NOCOPY flag means that sendfile(2) requests
519  * ZFS to populate a range of page cache pages with data.
520  *
521  * NOTE: this function could be optimized to pre-allocate
522  * all pages in advance, drain exclusive busy on all of them,
523  * map them into contiguous KVA region and populate them
524  * in one single dmu_read() call.
525  */
526 int
mappedread_sf(znode_t * zp,int nbytes,zfs_uio_t * uio)527 mappedread_sf(znode_t *zp, int nbytes, zfs_uio_t *uio)
528 {
529 	vnode_t *vp = ZTOV(zp);
530 	objset_t *os = zp->z_zfsvfs->z_os;
531 	struct sf_buf *sf;
532 	vm_object_t obj;
533 	vm_page_t pp;
534 	int64_t start;
535 	caddr_t va;
536 	int len = nbytes;
537 	int error = 0;
538 
539 	ASSERT3U(zfs_uio_segflg(uio), ==, UIO_NOCOPY);
540 	ASSERT3P(vp->v_mount, !=, NULL);
541 	obj = vp->v_object;
542 	ASSERT3P(obj, !=, NULL);
543 	ASSERT0(zfs_uio_offset(uio) & PAGEOFFSET);
544 
545 	zfs_vmobject_wlock_12(obj);
546 	for (start = zfs_uio_offset(uio); len > 0; start += PAGESIZE) {
547 		int bytes = MIN(PAGESIZE, len);
548 
549 		pp = vm_page_grab_unlocked(obj, OFF_TO_IDX(start),
550 		    VM_ALLOC_SBUSY | VM_ALLOC_NORMAL | VM_ALLOC_IGN_SBUSY);
551 		if (vm_page_none_valid(pp)) {
552 			zfs_vmobject_wunlock_12(obj);
553 			va = zfs_map_page(pp, &sf);
554 			error = dmu_read(os, zp->z_id, start, bytes, va,
555 			    DMU_READ_PREFETCH);
556 			if (bytes != PAGESIZE && error == 0)
557 				memset(va + bytes, 0, PAGESIZE - bytes);
558 			zfs_unmap_page(sf);
559 			zfs_vmobject_wlock_12(obj);
560 #if  __FreeBSD_version >= 1300081
561 			if (error == 0) {
562 				vm_page_valid(pp);
563 				vm_page_activate(pp);
564 				vm_page_do_sunbusy(pp);
565 			} else {
566 				zfs_vmobject_wlock(obj);
567 				if (!vm_page_wired(pp) && pp->valid == 0 &&
568 				    vm_page_busy_tryupgrade(pp))
569 					vm_page_free(pp);
570 				else
571 					vm_page_sunbusy(pp);
572 				zfs_vmobject_wunlock(obj);
573 			}
574 #else
575 			vm_page_do_sunbusy(pp);
576 			vm_page_lock(pp);
577 			if (error) {
578 				if (pp->wire_count == 0 && pp->valid == 0 &&
579 				    !vm_page_busied(pp))
580 					vm_page_free(pp);
581 			} else {
582 				pp->valid = VM_PAGE_BITS_ALL;
583 				vm_page_activate(pp);
584 			}
585 			vm_page_unlock(pp);
586 #endif
587 		} else {
588 			ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
589 			vm_page_do_sunbusy(pp);
590 		}
591 		if (error)
592 			break;
593 		zfs_uio_advance(uio, bytes);
594 		len -= bytes;
595 	}
596 	zfs_vmobject_wunlock_12(obj);
597 	return (error);
598 }
599 
600 /*
601  * When a file is memory mapped, we must keep the IO data synchronized
602  * between the DMU cache and the memory mapped pages.  What this means:
603  *
604  * On Read:	We "read" preferentially from memory mapped pages,
605  *		else we default from the dmu buffer.
606  *
607  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
608  *	 the file is memory mapped.
609  */
610 int
mappedread(znode_t * zp,int nbytes,zfs_uio_t * uio)611 mappedread(znode_t *zp, int nbytes, zfs_uio_t *uio)
612 {
613 	vnode_t *vp = ZTOV(zp);
614 	vm_object_t obj;
615 	int64_t start;
616 	int len = nbytes;
617 	int off;
618 	int error = 0;
619 
620 	ASSERT3P(vp->v_mount, !=, NULL);
621 	obj = vp->v_object;
622 	ASSERT3P(obj, !=, NULL);
623 
624 	start = zfs_uio_offset(uio);
625 	off = start & PAGEOFFSET;
626 	zfs_vmobject_wlock_12(obj);
627 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
628 		vm_page_t pp;
629 		uint64_t bytes = MIN(PAGESIZE - off, len);
630 
631 		if ((pp = page_hold(vp, start))) {
632 			struct sf_buf *sf;
633 			caddr_t va;
634 
635 			zfs_vmobject_wunlock_12(obj);
636 			va = zfs_map_page(pp, &sf);
637 			error = vn_io_fault_uiomove(va + off, bytes,
638 			    GET_UIO_STRUCT(uio));
639 			zfs_unmap_page(sf);
640 			zfs_vmobject_wlock_12(obj);
641 			page_unhold(pp);
642 		} else {
643 			zfs_vmobject_wunlock_12(obj);
644 			error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
645 			    uio, bytes);
646 			zfs_vmobject_wlock_12(obj);
647 		}
648 		len -= bytes;
649 		off = 0;
650 		if (error)
651 			break;
652 	}
653 	zfs_vmobject_wunlock_12(obj);
654 	return (error);
655 }
656 
657 int
zfs_write_simple(znode_t * zp,const void * data,size_t len,loff_t pos,size_t * presid)658 zfs_write_simple(znode_t *zp, const void *data, size_t len,
659     loff_t pos, size_t *presid)
660 {
661 	int error = 0;
662 	ssize_t resid;
663 
664 	error = vn_rdwr(UIO_WRITE, ZTOV(zp), __DECONST(void *, data), len, pos,
665 	    UIO_SYSSPACE, IO_SYNC, kcred, NOCRED, &resid, curthread);
666 
667 	if (error) {
668 		return (SET_ERROR(error));
669 	} else if (presid == NULL) {
670 		if (resid != 0) {
671 			error = SET_ERROR(EIO);
672 		}
673 	} else {
674 		*presid = resid;
675 	}
676 	return (error);
677 }
678 
679 void
zfs_zrele_async(znode_t * zp)680 zfs_zrele_async(znode_t *zp)
681 {
682 	vnode_t *vp = ZTOV(zp);
683 	objset_t *os = ITOZSB(vp)->z_os;
684 
685 	VN_RELE_ASYNC(vp, dsl_pool_zrele_taskq(dmu_objset_pool(os)));
686 }
687 
688 static int
zfs_dd_callback(struct mount * mp,void * arg,int lkflags,struct vnode ** vpp)689 zfs_dd_callback(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
690 {
691 	int error;
692 
693 	*vpp = arg;
694 	error = vn_lock(*vpp, lkflags);
695 	if (error != 0)
696 		vrele(*vpp);
697 	return (error);
698 }
699 
700 static int
zfs_lookup_lock(vnode_t * dvp,vnode_t * vp,const char * name,int lkflags)701 zfs_lookup_lock(vnode_t *dvp, vnode_t *vp, const char *name, int lkflags)
702 {
703 	znode_t *zdp = VTOZ(dvp);
704 	zfsvfs_t *zfsvfs __unused = zdp->z_zfsvfs;
705 	int error;
706 	int ltype;
707 
708 	if (zfsvfs->z_replay == B_FALSE)
709 		ASSERT_VOP_LOCKED(dvp, __func__);
710 
711 	if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
712 		ASSERT3P(dvp, ==, vp);
713 		vref(dvp);
714 		ltype = lkflags & LK_TYPE_MASK;
715 		if (ltype != VOP_ISLOCKED(dvp)) {
716 			if (ltype == LK_EXCLUSIVE)
717 				vn_lock(dvp, LK_UPGRADE | LK_RETRY);
718 			else /* if (ltype == LK_SHARED) */
719 				vn_lock(dvp, LK_DOWNGRADE | LK_RETRY);
720 
721 			/*
722 			 * Relock for the "." case could leave us with
723 			 * reclaimed vnode.
724 			 */
725 			if (VN_IS_DOOMED(dvp)) {
726 				vrele(dvp);
727 				return (SET_ERROR(ENOENT));
728 			}
729 		}
730 		return (0);
731 	} else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
732 		/*
733 		 * Note that in this case, dvp is the child vnode, and we
734 		 * are looking up the parent vnode - exactly reverse from
735 		 * normal operation.  Unlocking dvp requires some rather
736 		 * tricky unlock/relock dance to prevent mp from being freed;
737 		 * use vn_vget_ino_gen() which takes care of all that.
738 		 *
739 		 * XXX Note that there is a time window when both vnodes are
740 		 * unlocked.  It is possible, although highly unlikely, that
741 		 * during that window the parent-child relationship between
742 		 * the vnodes may change, for example, get reversed.
743 		 * In that case we would have a wrong lock order for the vnodes.
744 		 * All other filesystems seem to ignore this problem, so we
745 		 * do the same here.
746 		 * A potential solution could be implemented as follows:
747 		 * - using LK_NOWAIT when locking the second vnode and retrying
748 		 *   if necessary
749 		 * - checking that the parent-child relationship still holds
750 		 *   after locking both vnodes and retrying if it doesn't
751 		 */
752 		error = vn_vget_ino_gen(dvp, zfs_dd_callback, vp, lkflags, &vp);
753 		return (error);
754 	} else {
755 		error = vn_lock(vp, lkflags);
756 		if (error != 0)
757 			vrele(vp);
758 		return (error);
759 	}
760 }
761 
762 /*
763  * Lookup an entry in a directory, or an extended attribute directory.
764  * If it exists, return a held vnode reference for it.
765  *
766  *	IN:	dvp	- vnode of directory to search.
767  *		nm	- name of entry to lookup.
768  *		pnp	- full pathname to lookup [UNUSED].
769  *		flags	- LOOKUP_XATTR set if looking for an attribute.
770  *		rdir	- root directory vnode [UNUSED].
771  *		cr	- credentials of caller.
772  *		ct	- caller context
773  *
774  *	OUT:	vpp	- vnode of located entry, NULL if not found.
775  *
776  *	RETURN:	0 on success, error code on failure.
777  *
778  * Timestamps:
779  *	NA
780  */
781 static int
zfs_lookup(vnode_t * dvp,const char * nm,vnode_t ** vpp,struct componentname * cnp,int nameiop,cred_t * cr,int flags,boolean_t cached)782 zfs_lookup(vnode_t *dvp, const char *nm, vnode_t **vpp,
783     struct componentname *cnp, int nameiop, cred_t *cr, int flags,
784     boolean_t cached)
785 {
786 	znode_t *zdp = VTOZ(dvp);
787 	znode_t *zp;
788 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
789 #if	__FreeBSD_version > 1300124
790 	seqc_t dvp_seqc;
791 #endif
792 	int	error = 0;
793 
794 	/*
795 	 * Fast path lookup, however we must skip DNLC lookup
796 	 * for case folding or normalizing lookups because the
797 	 * DNLC code only stores the passed in name.  This means
798 	 * creating 'a' and removing 'A' on a case insensitive
799 	 * file system would work, but DNLC still thinks 'a'
800 	 * exists and won't let you create it again on the next
801 	 * pass through fast path.
802 	 */
803 	if (!(flags & LOOKUP_XATTR)) {
804 		if (dvp->v_type != VDIR) {
805 			return (SET_ERROR(ENOTDIR));
806 		} else if (zdp->z_sa_hdl == NULL) {
807 			return (SET_ERROR(EIO));
808 		}
809 	}
810 
811 	DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp,
812 	    const char *, nm);
813 
814 	if ((error = zfs_enter_verify_zp(zfsvfs, zdp, FTAG)) != 0)
815 		return (error);
816 
817 #if	__FreeBSD_version > 1300124
818 	dvp_seqc = vn_seqc_read_notmodify(dvp);
819 #endif
820 
821 	*vpp = NULL;
822 
823 	if (flags & LOOKUP_XATTR) {
824 		/*
825 		 * If the xattr property is off, refuse the lookup request.
826 		 */
827 		if (!(zfsvfs->z_flags & ZSB_XATTR)) {
828 			zfs_exit(zfsvfs, FTAG);
829 			return (SET_ERROR(EOPNOTSUPP));
830 		}
831 
832 		/*
833 		 * We don't allow recursive attributes..
834 		 * Maybe someday we will.
835 		 */
836 		if (zdp->z_pflags & ZFS_XATTR) {
837 			zfs_exit(zfsvfs, FTAG);
838 			return (SET_ERROR(EINVAL));
839 		}
840 
841 		if ((error = zfs_get_xattrdir(VTOZ(dvp), &zp, cr, flags))) {
842 			zfs_exit(zfsvfs, FTAG);
843 			return (error);
844 		}
845 		*vpp = ZTOV(zp);
846 
847 		/*
848 		 * Do we have permission to get into attribute directory?
849 		 */
850 		error = zfs_zaccess(zp, ACE_EXECUTE, 0, B_FALSE, cr, NULL);
851 		if (error) {
852 			vrele(ZTOV(zp));
853 		}
854 
855 		zfs_exit(zfsvfs, FTAG);
856 		return (error);
857 	}
858 
859 	/*
860 	 * Check accessibility of directory if we're not coming in via
861 	 * VOP_CACHEDLOOKUP.
862 	 */
863 	if (!cached) {
864 #ifdef NOEXECCHECK
865 		if ((cnp->cn_flags & NOEXECCHECK) != 0) {
866 			cnp->cn_flags &= ~NOEXECCHECK;
867 		} else
868 #endif
869 		if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr,
870 		    NULL))) {
871 			zfs_exit(zfsvfs, FTAG);
872 			return (error);
873 		}
874 	}
875 
876 	if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
877 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
878 		zfs_exit(zfsvfs, FTAG);
879 		return (SET_ERROR(EILSEQ));
880 	}
881 
882 
883 	/*
884 	 * First handle the special cases.
885 	 */
886 	if ((cnp->cn_flags & ISDOTDOT) != 0) {
887 		/*
888 		 * If we are a snapshot mounted under .zfs, return
889 		 * the vp for the snapshot directory.
890 		 */
891 		if (zdp->z_id == zfsvfs->z_root && zfsvfs->z_parent != zfsvfs) {
892 			struct componentname cn;
893 			vnode_t *zfsctl_vp;
894 			int ltype;
895 
896 			zfs_exit(zfsvfs, FTAG);
897 			ltype = VOP_ISLOCKED(dvp);
898 			VOP_UNLOCK1(dvp);
899 			error = zfsctl_root(zfsvfs->z_parent, LK_SHARED,
900 			    &zfsctl_vp);
901 			if (error == 0) {
902 				cn.cn_nameptr = "snapshot";
903 				cn.cn_namelen = strlen(cn.cn_nameptr);
904 				cn.cn_nameiop = cnp->cn_nameiop;
905 				cn.cn_flags = cnp->cn_flags & ~ISDOTDOT;
906 				cn.cn_lkflags = cnp->cn_lkflags;
907 				error = VOP_LOOKUP(zfsctl_vp, vpp, &cn);
908 				vput(zfsctl_vp);
909 			}
910 			vn_lock(dvp, ltype | LK_RETRY);
911 			return (error);
912 		}
913 	}
914 	if (zfs_has_ctldir(zdp) && strcmp(nm, ZFS_CTLDIR_NAME) == 0) {
915 		zfs_exit(zfsvfs, FTAG);
916 		if ((cnp->cn_flags & ISLASTCN) != 0 && nameiop != LOOKUP)
917 			return (SET_ERROR(ENOTSUP));
918 		error = zfsctl_root(zfsvfs, cnp->cn_lkflags, vpp);
919 		return (error);
920 	}
921 
922 	/*
923 	 * The loop is retry the lookup if the parent-child relationship
924 	 * changes during the dot-dot locking complexities.
925 	 */
926 	for (;;) {
927 		uint64_t parent;
928 
929 		error = zfs_dirlook(zdp, nm, &zp);
930 		if (error == 0)
931 			*vpp = ZTOV(zp);
932 
933 		zfs_exit(zfsvfs, FTAG);
934 		if (error != 0)
935 			break;
936 
937 		error = zfs_lookup_lock(dvp, *vpp, nm, cnp->cn_lkflags);
938 		if (error != 0) {
939 			/*
940 			 * If we've got a locking error, then the vnode
941 			 * got reclaimed because of a force unmount.
942 			 * We never enter doomed vnodes into the name cache.
943 			 */
944 			*vpp = NULL;
945 			return (error);
946 		}
947 
948 		if ((cnp->cn_flags & ISDOTDOT) == 0)
949 			break;
950 
951 		if ((error = zfs_enter(zfsvfs, FTAG)) != 0) {
952 			vput(ZTOV(zp));
953 			*vpp = NULL;
954 			return (error);
955 		}
956 		if (zdp->z_sa_hdl == NULL) {
957 			error = SET_ERROR(EIO);
958 		} else {
959 			error = sa_lookup(zdp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
960 			    &parent, sizeof (parent));
961 		}
962 		if (error != 0) {
963 			zfs_exit(zfsvfs, FTAG);
964 			vput(ZTOV(zp));
965 			break;
966 		}
967 		if (zp->z_id == parent) {
968 			zfs_exit(zfsvfs, FTAG);
969 			break;
970 		}
971 		vput(ZTOV(zp));
972 	}
973 
974 	if (error != 0)
975 		*vpp = NULL;
976 
977 	/* Translate errors and add SAVENAME when needed. */
978 	if (cnp->cn_flags & ISLASTCN) {
979 		switch (nameiop) {
980 		case CREATE:
981 		case RENAME:
982 			if (error == ENOENT) {
983 				error = EJUSTRETURN;
984 #if __FreeBSD_version < 1400068
985 				cnp->cn_flags |= SAVENAME;
986 #endif
987 				break;
988 			}
989 			zfs_fallthrough;
990 		case DELETE:
991 #if __FreeBSD_version < 1400068
992 			if (error == 0)
993 				cnp->cn_flags |= SAVENAME;
994 #endif
995 			break;
996 		}
997 	}
998 
999 #if	__FreeBSD_version > 1300124
1000 	if ((cnp->cn_flags & ISDOTDOT) != 0) {
1001 		/*
1002 		 * FIXME: zfs_lookup_lock relocks vnodes and does nothing to
1003 		 * handle races. In particular different callers may end up
1004 		 * with different vnodes and will try to add conflicting
1005 		 * entries to the namecache.
1006 		 *
1007 		 * While finding different result may be acceptable in face
1008 		 * of concurrent modification, adding conflicting entries
1009 		 * trips over an assert in the namecache.
1010 		 *
1011 		 * Ultimately let an entry through once everything settles.
1012 		 */
1013 		if (!vn_seqc_consistent(dvp, dvp_seqc)) {
1014 			cnp->cn_flags &= ~MAKEENTRY;
1015 		}
1016 	}
1017 #endif
1018 
1019 	/* Insert name into cache (as non-existent) if appropriate. */
1020 	if (zfsvfs->z_use_namecache && !zfsvfs->z_replay &&
1021 	    error == ENOENT && (cnp->cn_flags & MAKEENTRY) != 0)
1022 		cache_enter(dvp, NULL, cnp);
1023 
1024 	/* Insert name into cache if appropriate. */
1025 	if (zfsvfs->z_use_namecache && !zfsvfs->z_replay &&
1026 	    error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1027 		if (!(cnp->cn_flags & ISLASTCN) ||
1028 		    (nameiop != DELETE && nameiop != RENAME)) {
1029 			cache_enter(dvp, *vpp, cnp);
1030 		}
1031 	}
1032 
1033 	return (error);
1034 }
1035 
1036 /*
1037  * Attempt to create a new entry in a directory.  If the entry
1038  * already exists, truncate the file if permissible, else return
1039  * an error.  Return the vp of the created or trunc'd file.
1040  *
1041  *	IN:	dvp	- vnode of directory to put new file entry in.
1042  *		name	- name of new file entry.
1043  *		vap	- attributes of new file.
1044  *		excl	- flag indicating exclusive or non-exclusive mode.
1045  *		mode	- mode to open file with.
1046  *		cr	- credentials of caller.
1047  *		flag	- large file flag [UNUSED].
1048  *		ct	- caller context
1049  *		vsecp	- ACL to be set
1050  *		mnt_ns	- Unused on FreeBSD
1051  *
1052  *	OUT:	vpp	- vnode of created or trunc'd entry.
1053  *
1054  *	RETURN:	0 on success, error code on failure.
1055  *
1056  * Timestamps:
1057  *	dvp - ctime|mtime updated if new entry created
1058  *	 vp - ctime|mtime always, atime if new
1059  */
1060 int
zfs_create(znode_t * dzp,const char * name,vattr_t * vap,int excl,int mode,znode_t ** zpp,cred_t * cr,int flag,vsecattr_t * vsecp,zidmap_t * mnt_ns)1061 zfs_create(znode_t *dzp, const char *name, vattr_t *vap, int excl, int mode,
1062     znode_t **zpp, cred_t *cr, int flag, vsecattr_t *vsecp, zidmap_t *mnt_ns)
1063 {
1064 	(void) excl, (void) mode, (void) flag;
1065 	znode_t		*zp;
1066 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1067 	zilog_t		*zilog;
1068 	objset_t	*os;
1069 	dmu_tx_t	*tx;
1070 	int		error;
1071 	uid_t		uid = crgetuid(cr);
1072 	gid_t		gid = crgetgid(cr);
1073 	uint64_t	projid = ZFS_DEFAULT_PROJID;
1074 	zfs_acl_ids_t   acl_ids;
1075 	boolean_t	fuid_dirtied;
1076 	uint64_t	txtype;
1077 #ifdef DEBUG_VFS_LOCKS
1078 	vnode_t	*dvp = ZTOV(dzp);
1079 #endif
1080 
1081 	/*
1082 	 * If we have an ephemeral id, ACL, or XVATTR then
1083 	 * make sure file system is at proper version
1084 	 */
1085 	if (zfsvfs->z_use_fuids == B_FALSE &&
1086 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
1087 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1088 		return (SET_ERROR(EINVAL));
1089 
1090 	if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
1091 		return (error);
1092 	os = zfsvfs->z_os;
1093 	zilog = zfsvfs->z_log;
1094 
1095 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1096 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1097 		zfs_exit(zfsvfs, FTAG);
1098 		return (SET_ERROR(EILSEQ));
1099 	}
1100 
1101 	if (vap->va_mask & AT_XVATTR) {
1102 		if ((error = secpolicy_xvattr(ZTOV(dzp), (xvattr_t *)vap,
1103 		    crgetuid(cr), cr, vap->va_type)) != 0) {
1104 			zfs_exit(zfsvfs, FTAG);
1105 			return (error);
1106 		}
1107 	}
1108 
1109 	*zpp = NULL;
1110 
1111 	if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1112 		vap->va_mode &= ~S_ISVTX;
1113 
1114 	error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
1115 	if (error) {
1116 		zfs_exit(zfsvfs, FTAG);
1117 		return (error);
1118 	}
1119 	ASSERT3P(zp, ==, NULL);
1120 
1121 	/*
1122 	 * Create a new file object and update the directory
1123 	 * to reference it.
1124 	 */
1125 	if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr, mnt_ns))) {
1126 		goto out;
1127 	}
1128 
1129 	/*
1130 	 * We only support the creation of regular files in
1131 	 * extended attribute directories.
1132 	 */
1133 
1134 	if ((dzp->z_pflags & ZFS_XATTR) &&
1135 	    (vap->va_type != VREG)) {
1136 		error = SET_ERROR(EINVAL);
1137 		goto out;
1138 	}
1139 
1140 	if ((error = zfs_acl_ids_create(dzp, 0, vap,
1141 	    cr, vsecp, &acl_ids, NULL)) != 0)
1142 		goto out;
1143 
1144 	if (S_ISREG(vap->va_mode) || S_ISDIR(vap->va_mode))
1145 		projid = zfs_inherit_projid(dzp);
1146 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, projid)) {
1147 		zfs_acl_ids_free(&acl_ids);
1148 		error = SET_ERROR(EDQUOT);
1149 		goto out;
1150 	}
1151 
1152 	getnewvnode_reserve_();
1153 
1154 	tx = dmu_tx_create(os);
1155 
1156 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1157 	    ZFS_SA_BASE_ATTR_SIZE);
1158 
1159 	fuid_dirtied = zfsvfs->z_fuid_dirty;
1160 	if (fuid_dirtied)
1161 		zfs_fuid_txhold(zfsvfs, tx);
1162 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1163 	dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1164 	if (!zfsvfs->z_use_sa &&
1165 	    acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1166 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1167 		    0, acl_ids.z_aclp->z_acl_bytes);
1168 	}
1169 	error = dmu_tx_assign(tx, TXG_WAIT);
1170 	if (error) {
1171 		zfs_acl_ids_free(&acl_ids);
1172 		dmu_tx_abort(tx);
1173 		getnewvnode_drop_reserve();
1174 		zfs_exit(zfsvfs, FTAG);
1175 		return (error);
1176 	}
1177 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1178 	if (fuid_dirtied)
1179 		zfs_fuid_sync(zfsvfs, tx);
1180 
1181 	(void) zfs_link_create(dzp, name, zp, tx, ZNEW);
1182 	txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1183 	zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1184 	    vsecp, acl_ids.z_fuidp, vap);
1185 	zfs_acl_ids_free(&acl_ids);
1186 	dmu_tx_commit(tx);
1187 
1188 	getnewvnode_drop_reserve();
1189 
1190 out:
1191 	VNCHECKREF(dvp);
1192 	if (error == 0) {
1193 		*zpp = zp;
1194 	}
1195 
1196 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1197 		zil_commit(zilog, 0);
1198 
1199 	zfs_exit(zfsvfs, FTAG);
1200 	return (error);
1201 }
1202 
1203 /*
1204  * Remove an entry from a directory.
1205  *
1206  *	IN:	dvp	- vnode of directory to remove entry from.
1207  *		name	- name of entry to remove.
1208  *		cr	- credentials of caller.
1209  *		ct	- caller context
1210  *		flags	- case flags
1211  *
1212  *	RETURN:	0 on success, error code on failure.
1213  *
1214  * Timestamps:
1215  *	dvp - ctime|mtime
1216  *	 vp - ctime (if nlink > 0)
1217  */
1218 static int
zfs_remove_(vnode_t * dvp,vnode_t * vp,const char * name,cred_t * cr)1219 zfs_remove_(vnode_t *dvp, vnode_t *vp, const char *name, cred_t *cr)
1220 {
1221 	znode_t		*dzp = VTOZ(dvp);
1222 	znode_t		*zp;
1223 	znode_t		*xzp;
1224 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1225 	zilog_t		*zilog;
1226 	uint64_t	xattr_obj;
1227 	uint64_t	obj = 0;
1228 	dmu_tx_t	*tx;
1229 	boolean_t	unlinked;
1230 	uint64_t	txtype;
1231 	int		error;
1232 
1233 
1234 	if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
1235 		return (error);
1236 	zp = VTOZ(vp);
1237 	if ((error = zfs_verify_zp(zp)) != 0) {
1238 		zfs_exit(zfsvfs, FTAG);
1239 		return (error);
1240 	}
1241 	zilog = zfsvfs->z_log;
1242 
1243 	xattr_obj = 0;
1244 	xzp = NULL;
1245 
1246 	if ((error = zfs_zaccess_delete(dzp, zp, cr, NULL))) {
1247 		goto out;
1248 	}
1249 
1250 	/*
1251 	 * Need to use rmdir for removing directories.
1252 	 */
1253 	if (vp->v_type == VDIR) {
1254 		error = SET_ERROR(EPERM);
1255 		goto out;
1256 	}
1257 
1258 	vnevent_remove(vp, dvp, name, ct);
1259 
1260 	obj = zp->z_id;
1261 
1262 	/* are there any extended attributes? */
1263 	error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1264 	    &xattr_obj, sizeof (xattr_obj));
1265 	if (error == 0 && xattr_obj) {
1266 		error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1267 		ASSERT0(error);
1268 	}
1269 
1270 	/*
1271 	 * We may delete the znode now, or we may put it in the unlinked set;
1272 	 * it depends on whether we're the last link, and on whether there are
1273 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1274 	 * allow for either case.
1275 	 */
1276 	tx = dmu_tx_create(zfsvfs->z_os);
1277 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1278 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1279 	zfs_sa_upgrade_txholds(tx, zp);
1280 	zfs_sa_upgrade_txholds(tx, dzp);
1281 
1282 	if (xzp) {
1283 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1284 		dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1285 	}
1286 
1287 	/* charge as an update -- would be nice not to charge at all */
1288 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1289 
1290 	/*
1291 	 * Mark this transaction as typically resulting in a net free of space
1292 	 */
1293 	dmu_tx_mark_netfree(tx);
1294 
1295 	error = dmu_tx_assign(tx, TXG_WAIT);
1296 	if (error) {
1297 		dmu_tx_abort(tx);
1298 		zfs_exit(zfsvfs, FTAG);
1299 		return (error);
1300 	}
1301 
1302 	/*
1303 	 * Remove the directory entry.
1304 	 */
1305 	error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, &unlinked);
1306 
1307 	if (error) {
1308 		dmu_tx_commit(tx);
1309 		goto out;
1310 	}
1311 
1312 	if (unlinked) {
1313 		zfs_unlinked_add(zp, tx);
1314 		vp->v_vflag |= VV_NOSYNC;
1315 	}
1316 	/* XXX check changes to linux vnops */
1317 	txtype = TX_REMOVE;
1318 	zfs_log_remove(zilog, tx, txtype, dzp, name, obj, unlinked);
1319 
1320 	dmu_tx_commit(tx);
1321 out:
1322 
1323 	if (xzp)
1324 		vrele(ZTOV(xzp));
1325 
1326 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1327 		zil_commit(zilog, 0);
1328 
1329 
1330 	zfs_exit(zfsvfs, FTAG);
1331 	return (error);
1332 }
1333 
1334 
1335 static int
zfs_lookup_internal(znode_t * dzp,const char * name,vnode_t ** vpp,struct componentname * cnp,int nameiop)1336 zfs_lookup_internal(znode_t *dzp, const char *name, vnode_t **vpp,
1337     struct componentname *cnp, int nameiop)
1338 {
1339 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1340 	int error;
1341 
1342 	cnp->cn_nameptr = __DECONST(char *, name);
1343 	cnp->cn_namelen = strlen(name);
1344 	cnp->cn_nameiop = nameiop;
1345 	cnp->cn_flags = ISLASTCN;
1346 #if __FreeBSD_version < 1400068
1347 	cnp->cn_flags |= SAVENAME;
1348 #endif
1349 	cnp->cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
1350 	cnp->cn_cred = kcred;
1351 #if __FreeBSD_version < 1400037
1352 	cnp->cn_thread = curthread;
1353 #endif
1354 
1355 	if (zfsvfs->z_use_namecache && !zfsvfs->z_replay) {
1356 		struct vop_lookup_args a;
1357 
1358 		a.a_gen.a_desc = &vop_lookup_desc;
1359 		a.a_dvp = ZTOV(dzp);
1360 		a.a_vpp = vpp;
1361 		a.a_cnp = cnp;
1362 		error = vfs_cache_lookup(&a);
1363 	} else {
1364 		error = zfs_lookup(ZTOV(dzp), name, vpp, cnp, nameiop, kcred, 0,
1365 		    B_FALSE);
1366 	}
1367 #ifdef ZFS_DEBUG
1368 	if (error) {
1369 		printf("got error %d on name %s on op %d\n", error, name,
1370 		    nameiop);
1371 		kdb_backtrace();
1372 	}
1373 #endif
1374 	return (error);
1375 }
1376 
1377 int
zfs_remove(znode_t * dzp,const char * name,cred_t * cr,int flags)1378 zfs_remove(znode_t *dzp, const char *name, cred_t *cr, int flags)
1379 {
1380 	vnode_t *vp;
1381 	int error;
1382 	struct componentname cn;
1383 
1384 	if ((error = zfs_lookup_internal(dzp, name, &vp, &cn, DELETE)))
1385 		return (error);
1386 
1387 	error = zfs_remove_(ZTOV(dzp), vp, name, cr);
1388 	vput(vp);
1389 	return (error);
1390 }
1391 /*
1392  * Create a new directory and insert it into dvp using the name
1393  * provided.  Return a pointer to the inserted directory.
1394  *
1395  *	IN:	dvp	- vnode of directory to add subdir to.
1396  *		dirname	- name of new directory.
1397  *		vap	- attributes of new directory.
1398  *		cr	- credentials of caller.
1399  *		ct	- caller context
1400  *		flags	- case flags
1401  *		vsecp	- ACL to be set
1402  *		mnt_ns	- Unused on FreeBSD
1403  *
1404  *	OUT:	vpp	- vnode of created directory.
1405  *
1406  *	RETURN:	0 on success, error code on failure.
1407  *
1408  * Timestamps:
1409  *	dvp - ctime|mtime updated
1410  *	 vp - ctime|mtime|atime updated
1411  */
1412 int
zfs_mkdir(znode_t * dzp,const char * dirname,vattr_t * vap,znode_t ** zpp,cred_t * cr,int flags,vsecattr_t * vsecp,zidmap_t * mnt_ns)1413 zfs_mkdir(znode_t *dzp, const char *dirname, vattr_t *vap, znode_t **zpp,
1414     cred_t *cr, int flags, vsecattr_t *vsecp, zidmap_t *mnt_ns)
1415 {
1416 	(void) flags, (void) vsecp;
1417 	znode_t		*zp;
1418 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1419 	zilog_t		*zilog;
1420 	uint64_t	txtype;
1421 	dmu_tx_t	*tx;
1422 	int		error;
1423 	uid_t		uid = crgetuid(cr);
1424 	gid_t		gid = crgetgid(cr);
1425 	zfs_acl_ids_t   acl_ids;
1426 	boolean_t	fuid_dirtied;
1427 
1428 	ASSERT3U(vap->va_type, ==, VDIR);
1429 
1430 	/*
1431 	 * If we have an ephemeral id, ACL, or XVATTR then
1432 	 * make sure file system is at proper version
1433 	 */
1434 	if (zfsvfs->z_use_fuids == B_FALSE &&
1435 	    ((vap->va_mask & AT_XVATTR) ||
1436 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1437 		return (SET_ERROR(EINVAL));
1438 
1439 	if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
1440 		return (error);
1441 	zilog = zfsvfs->z_log;
1442 
1443 	if (dzp->z_pflags & ZFS_XATTR) {
1444 		zfs_exit(zfsvfs, FTAG);
1445 		return (SET_ERROR(EINVAL));
1446 	}
1447 
1448 	if (zfsvfs->z_utf8 && u8_validate(dirname,
1449 	    strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1450 		zfs_exit(zfsvfs, FTAG);
1451 		return (SET_ERROR(EILSEQ));
1452 	}
1453 
1454 	if (vap->va_mask & AT_XVATTR) {
1455 		if ((error = secpolicy_xvattr(ZTOV(dzp), (xvattr_t *)vap,
1456 		    crgetuid(cr), cr, vap->va_type)) != 0) {
1457 			zfs_exit(zfsvfs, FTAG);
1458 			return (error);
1459 		}
1460 	}
1461 
1462 	if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1463 	    NULL, &acl_ids, NULL)) != 0) {
1464 		zfs_exit(zfsvfs, FTAG);
1465 		return (error);
1466 	}
1467 
1468 	/*
1469 	 * First make sure the new directory doesn't exist.
1470 	 *
1471 	 * Existence is checked first to make sure we don't return
1472 	 * EACCES instead of EEXIST which can cause some applications
1473 	 * to fail.
1474 	 */
1475 	*zpp = NULL;
1476 
1477 	if ((error = zfs_dirent_lookup(dzp, dirname, &zp, ZNEW))) {
1478 		zfs_acl_ids_free(&acl_ids);
1479 		zfs_exit(zfsvfs, FTAG);
1480 		return (error);
1481 	}
1482 	ASSERT3P(zp, ==, NULL);
1483 
1484 	if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr,
1485 	    mnt_ns))) {
1486 		zfs_acl_ids_free(&acl_ids);
1487 		zfs_exit(zfsvfs, FTAG);
1488 		return (error);
1489 	}
1490 
1491 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, zfs_inherit_projid(dzp))) {
1492 		zfs_acl_ids_free(&acl_ids);
1493 		zfs_exit(zfsvfs, FTAG);
1494 		return (SET_ERROR(EDQUOT));
1495 	}
1496 
1497 	/*
1498 	 * Add a new entry to the directory.
1499 	 */
1500 	getnewvnode_reserve_();
1501 	tx = dmu_tx_create(zfsvfs->z_os);
1502 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1503 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1504 	fuid_dirtied = zfsvfs->z_fuid_dirty;
1505 	if (fuid_dirtied)
1506 		zfs_fuid_txhold(zfsvfs, tx);
1507 	if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1508 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1509 		    acl_ids.z_aclp->z_acl_bytes);
1510 	}
1511 
1512 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1513 	    ZFS_SA_BASE_ATTR_SIZE);
1514 
1515 	error = dmu_tx_assign(tx, TXG_WAIT);
1516 	if (error) {
1517 		zfs_acl_ids_free(&acl_ids);
1518 		dmu_tx_abort(tx);
1519 		getnewvnode_drop_reserve();
1520 		zfs_exit(zfsvfs, FTAG);
1521 		return (error);
1522 	}
1523 
1524 	/*
1525 	 * Create new node.
1526 	 */
1527 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1528 
1529 	if (fuid_dirtied)
1530 		zfs_fuid_sync(zfsvfs, tx);
1531 
1532 	/*
1533 	 * Now put new name in parent dir.
1534 	 */
1535 	(void) zfs_link_create(dzp, dirname, zp, tx, ZNEW);
1536 
1537 	*zpp = zp;
1538 
1539 	txtype = zfs_log_create_txtype(Z_DIR, NULL, vap);
1540 	zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, NULL,
1541 	    acl_ids.z_fuidp, vap);
1542 
1543 	zfs_acl_ids_free(&acl_ids);
1544 
1545 	dmu_tx_commit(tx);
1546 
1547 	getnewvnode_drop_reserve();
1548 
1549 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1550 		zil_commit(zilog, 0);
1551 
1552 	zfs_exit(zfsvfs, FTAG);
1553 	return (0);
1554 }
1555 
1556 #if	__FreeBSD_version < 1300124
1557 static void
cache_vop_rmdir(struct vnode * dvp,struct vnode * vp)1558 cache_vop_rmdir(struct vnode *dvp, struct vnode *vp)
1559 {
1560 
1561 	cache_purge(dvp);
1562 	cache_purge(vp);
1563 }
1564 #endif
1565 
1566 /*
1567  * Remove a directory subdir entry.  If the current working
1568  * directory is the same as the subdir to be removed, the
1569  * remove will fail.
1570  *
1571  *	IN:	dvp	- vnode of directory to remove from.
1572  *		name	- name of directory to be removed.
1573  *		cwd	- vnode of current working directory.
1574  *		cr	- credentials of caller.
1575  *		ct	- caller context
1576  *		flags	- case flags
1577  *
1578  *	RETURN:	0 on success, error code on failure.
1579  *
1580  * Timestamps:
1581  *	dvp - ctime|mtime updated
1582  */
1583 static int
zfs_rmdir_(vnode_t * dvp,vnode_t * vp,const char * name,cred_t * cr)1584 zfs_rmdir_(vnode_t *dvp, vnode_t *vp, const char *name, cred_t *cr)
1585 {
1586 	znode_t		*dzp = VTOZ(dvp);
1587 	znode_t		*zp = VTOZ(vp);
1588 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1589 	zilog_t		*zilog;
1590 	dmu_tx_t	*tx;
1591 	int		error;
1592 
1593 	if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
1594 		return (error);
1595 	if ((error = zfs_verify_zp(zp)) != 0) {
1596 		zfs_exit(zfsvfs, FTAG);
1597 		return (error);
1598 	}
1599 	zilog = zfsvfs->z_log;
1600 
1601 
1602 	if ((error = zfs_zaccess_delete(dzp, zp, cr, NULL))) {
1603 		goto out;
1604 	}
1605 
1606 	if (vp->v_type != VDIR) {
1607 		error = SET_ERROR(ENOTDIR);
1608 		goto out;
1609 	}
1610 
1611 	vnevent_rmdir(vp, dvp, name, ct);
1612 
1613 	tx = dmu_tx_create(zfsvfs->z_os);
1614 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1615 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1616 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1617 	zfs_sa_upgrade_txholds(tx, zp);
1618 	zfs_sa_upgrade_txholds(tx, dzp);
1619 	dmu_tx_mark_netfree(tx);
1620 	error = dmu_tx_assign(tx, TXG_WAIT);
1621 	if (error) {
1622 		dmu_tx_abort(tx);
1623 		zfs_exit(zfsvfs, FTAG);
1624 		return (error);
1625 	}
1626 
1627 	error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, NULL);
1628 
1629 	if (error == 0) {
1630 		uint64_t txtype = TX_RMDIR;
1631 		zfs_log_remove(zilog, tx, txtype, dzp, name,
1632 		    ZFS_NO_OBJECT, B_FALSE);
1633 	}
1634 
1635 	dmu_tx_commit(tx);
1636 
1637 	if (zfsvfs->z_use_namecache)
1638 		cache_vop_rmdir(dvp, vp);
1639 out:
1640 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1641 		zil_commit(zilog, 0);
1642 
1643 	zfs_exit(zfsvfs, FTAG);
1644 	return (error);
1645 }
1646 
1647 int
zfs_rmdir(znode_t * dzp,const char * name,znode_t * cwd,cred_t * cr,int flags)1648 zfs_rmdir(znode_t *dzp, const char *name, znode_t *cwd, cred_t *cr, int flags)
1649 {
1650 	struct componentname cn;
1651 	vnode_t *vp;
1652 	int error;
1653 
1654 	if ((error = zfs_lookup_internal(dzp, name, &vp, &cn, DELETE)))
1655 		return (error);
1656 
1657 	error = zfs_rmdir_(ZTOV(dzp), vp, name, cr);
1658 	vput(vp);
1659 	return (error);
1660 }
1661 
1662 /*
1663  * Read as many directory entries as will fit into the provided
1664  * buffer from the given directory cursor position (specified in
1665  * the uio structure).
1666  *
1667  *	IN:	vp	- vnode of directory to read.
1668  *		uio	- structure supplying read location, range info,
1669  *			  and return buffer.
1670  *		cr	- credentials of caller.
1671  *		ct	- caller context
1672  *
1673  *	OUT:	uio	- updated offset and range, buffer filled.
1674  *		eofp	- set to true if end-of-file detected.
1675  *		ncookies- number of entries in cookies
1676  *		cookies	- offsets to directory entries
1677  *
1678  *	RETURN:	0 on success, error code on failure.
1679  *
1680  * Timestamps:
1681  *	vp - atime updated
1682  *
1683  * Note that the low 4 bits of the cookie returned by zap is always zero.
1684  * This allows us to use the low range for "special" directory entries:
1685  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
1686  * we use the offset 2 for the '.zfs' directory.
1687  */
1688 static int
zfs_readdir(vnode_t * vp,zfs_uio_t * uio,cred_t * cr,int * eofp,int * ncookies,cookie_t ** cookies)1689 zfs_readdir(vnode_t *vp, zfs_uio_t *uio, cred_t *cr, int *eofp,
1690     int *ncookies, cookie_t **cookies)
1691 {
1692 	znode_t		*zp = VTOZ(vp);
1693 	iovec_t		*iovp;
1694 	dirent64_t	*odp;
1695 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1696 	objset_t	*os;
1697 	caddr_t		outbuf;
1698 	size_t		bufsize;
1699 	zap_cursor_t	zc;
1700 	zap_attribute_t	zap;
1701 	uint_t		bytes_wanted;
1702 	uint64_t	offset; /* must be unsigned; checks for < 1 */
1703 	uint64_t	parent;
1704 	int		local_eof;
1705 	int		outcount;
1706 	int		error;
1707 	uint8_t		prefetch;
1708 	uint8_t		type;
1709 	int		ncooks;
1710 	cookie_t	*cooks = NULL;
1711 
1712 	if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
1713 		return (error);
1714 
1715 	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
1716 	    &parent, sizeof (parent))) != 0) {
1717 		zfs_exit(zfsvfs, FTAG);
1718 		return (error);
1719 	}
1720 
1721 	/*
1722 	 * If we are not given an eof variable,
1723 	 * use a local one.
1724 	 */
1725 	if (eofp == NULL)
1726 		eofp = &local_eof;
1727 
1728 	/*
1729 	 * Check for valid iov_len.
1730 	 */
1731 	if (GET_UIO_STRUCT(uio)->uio_iov->iov_len <= 0) {
1732 		zfs_exit(zfsvfs, FTAG);
1733 		return (SET_ERROR(EINVAL));
1734 	}
1735 
1736 	/*
1737 	 * Quit if directory has been removed (posix)
1738 	 */
1739 	if ((*eofp = zp->z_unlinked) != 0) {
1740 		zfs_exit(zfsvfs, FTAG);
1741 		return (0);
1742 	}
1743 
1744 	error = 0;
1745 	os = zfsvfs->z_os;
1746 	offset = zfs_uio_offset(uio);
1747 	prefetch = zp->z_zn_prefetch;
1748 
1749 	/*
1750 	 * Initialize the iterator cursor.
1751 	 */
1752 	if (offset <= 3) {
1753 		/*
1754 		 * Start iteration from the beginning of the directory.
1755 		 */
1756 		zap_cursor_init(&zc, os, zp->z_id);
1757 	} else {
1758 		/*
1759 		 * The offset is a serialized cursor.
1760 		 */
1761 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1762 	}
1763 
1764 	/*
1765 	 * Get space to change directory entries into fs independent format.
1766 	 */
1767 	iovp = GET_UIO_STRUCT(uio)->uio_iov;
1768 	bytes_wanted = iovp->iov_len;
1769 	if (zfs_uio_segflg(uio) != UIO_SYSSPACE || zfs_uio_iovcnt(uio) != 1) {
1770 		bufsize = bytes_wanted;
1771 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
1772 		odp = (struct dirent64 *)outbuf;
1773 	} else {
1774 		bufsize = bytes_wanted;
1775 		outbuf = NULL;
1776 		odp = (struct dirent64 *)iovp->iov_base;
1777 	}
1778 
1779 	if (ncookies != NULL) {
1780 		/*
1781 		 * Minimum entry size is dirent size and 1 byte for a file name.
1782 		 */
1783 		ncooks = zfs_uio_resid(uio) / (sizeof (struct dirent) -
1784 		    sizeof (((struct dirent *)NULL)->d_name) + 1);
1785 		cooks = malloc(ncooks * sizeof (*cooks), M_TEMP, M_WAITOK);
1786 		*cookies = cooks;
1787 		*ncookies = ncooks;
1788 	}
1789 
1790 	/*
1791 	 * Transform to file-system independent format
1792 	 */
1793 	outcount = 0;
1794 	while (outcount < bytes_wanted) {
1795 		ino64_t objnum;
1796 		ushort_t reclen;
1797 		off64_t *next = NULL;
1798 
1799 		/*
1800 		 * Special case `.', `..', and `.zfs'.
1801 		 */
1802 		if (offset == 0) {
1803 			(void) strcpy(zap.za_name, ".");
1804 			zap.za_normalization_conflict = 0;
1805 			objnum = zp->z_id;
1806 			type = DT_DIR;
1807 		} else if (offset == 1) {
1808 			(void) strcpy(zap.za_name, "..");
1809 			zap.za_normalization_conflict = 0;
1810 			objnum = parent;
1811 			type = DT_DIR;
1812 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
1813 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
1814 			zap.za_normalization_conflict = 0;
1815 			objnum = ZFSCTL_INO_ROOT;
1816 			type = DT_DIR;
1817 		} else {
1818 			/*
1819 			 * Grab next entry.
1820 			 */
1821 			if ((error = zap_cursor_retrieve(&zc, &zap))) {
1822 				if ((*eofp = (error == ENOENT)) != 0)
1823 					break;
1824 				else
1825 					goto update;
1826 			}
1827 
1828 			if (zap.za_integer_length != 8 ||
1829 			    zap.za_num_integers != 1) {
1830 				cmn_err(CE_WARN, "zap_readdir: bad directory "
1831 				    "entry, obj = %lld, offset = %lld\n",
1832 				    (u_longlong_t)zp->z_id,
1833 				    (u_longlong_t)offset);
1834 				error = SET_ERROR(ENXIO);
1835 				goto update;
1836 			}
1837 
1838 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
1839 			/*
1840 			 * MacOS X can extract the object type here such as:
1841 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
1842 			 */
1843 			type = ZFS_DIRENT_TYPE(zap.za_first_integer);
1844 		}
1845 
1846 		reclen = DIRENT64_RECLEN(strlen(zap.za_name));
1847 
1848 		/*
1849 		 * Will this entry fit in the buffer?
1850 		 */
1851 		if (outcount + reclen > bufsize) {
1852 			/*
1853 			 * Did we manage to fit anything in the buffer?
1854 			 */
1855 			if (!outcount) {
1856 				error = SET_ERROR(EINVAL);
1857 				goto update;
1858 			}
1859 			break;
1860 		}
1861 		/*
1862 		 * Add normal entry:
1863 		 */
1864 		odp->d_ino = objnum;
1865 		odp->d_reclen = reclen;
1866 		odp->d_namlen = strlen(zap.za_name);
1867 		/* NOTE: d_off is the offset for the *next* entry. */
1868 		next = &odp->d_off;
1869 		strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
1870 		odp->d_type = type;
1871 		dirent_terminate(odp);
1872 		odp = (dirent64_t *)((intptr_t)odp + reclen);
1873 
1874 		outcount += reclen;
1875 
1876 		ASSERT3S(outcount, <=, bufsize);
1877 
1878 		if (prefetch)
1879 			dmu_prefetch_dnode(os, objnum, ZIO_PRIORITY_SYNC_READ);
1880 
1881 		/*
1882 		 * Move to the next entry, fill in the previous offset.
1883 		 */
1884 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
1885 			zap_cursor_advance(&zc);
1886 			offset = zap_cursor_serialize(&zc);
1887 		} else {
1888 			offset += 1;
1889 		}
1890 
1891 		/* Fill the offset right after advancing the cursor. */
1892 		if (next != NULL)
1893 			*next = offset;
1894 		if (cooks != NULL) {
1895 			*cooks++ = offset;
1896 			ncooks--;
1897 			KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
1898 		}
1899 	}
1900 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
1901 
1902 	/* Subtract unused cookies */
1903 	if (ncookies != NULL)
1904 		*ncookies -= ncooks;
1905 
1906 	if (zfs_uio_segflg(uio) == UIO_SYSSPACE && zfs_uio_iovcnt(uio) == 1) {
1907 		iovp->iov_base += outcount;
1908 		iovp->iov_len -= outcount;
1909 		zfs_uio_resid(uio) -= outcount;
1910 	} else if ((error =
1911 	    zfs_uiomove(outbuf, (long)outcount, UIO_READ, uio))) {
1912 		/*
1913 		 * Reset the pointer.
1914 		 */
1915 		offset = zfs_uio_offset(uio);
1916 	}
1917 
1918 update:
1919 	zap_cursor_fini(&zc);
1920 	if (zfs_uio_segflg(uio) != UIO_SYSSPACE || zfs_uio_iovcnt(uio) != 1)
1921 		kmem_free(outbuf, bufsize);
1922 
1923 	if (error == ENOENT)
1924 		error = 0;
1925 
1926 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
1927 
1928 	zfs_uio_setoffset(uio, offset);
1929 	zfs_exit(zfsvfs, FTAG);
1930 	if (error != 0 && cookies != NULL) {
1931 		free(*cookies, M_TEMP);
1932 		*cookies = NULL;
1933 		*ncookies = 0;
1934 	}
1935 	return (error);
1936 }
1937 
1938 /*
1939  * Get the requested file attributes and place them in the provided
1940  * vattr structure.
1941  *
1942  *	IN:	vp	- vnode of file.
1943  *		vap	- va_mask identifies requested attributes.
1944  *			  If AT_XVATTR set, then optional attrs are requested
1945  *		flags	- ATTR_NOACLCHECK (CIFS server context)
1946  *		cr	- credentials of caller.
1947  *
1948  *	OUT:	vap	- attribute values.
1949  *
1950  *	RETURN:	0 (always succeeds).
1951  */
1952 static int
zfs_getattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr)1953 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
1954 {
1955 	znode_t *zp = VTOZ(vp);
1956 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1957 	int	error = 0;
1958 	uint32_t blksize;
1959 	u_longlong_t nblocks;
1960 	uint64_t mtime[2], ctime[2], crtime[2], rdev;
1961 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
1962 	xoptattr_t *xoap = NULL;
1963 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
1964 	sa_bulk_attr_t bulk[4];
1965 	int count = 0;
1966 
1967 	if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
1968 		return (error);
1969 
1970 	zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
1971 
1972 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
1973 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
1974 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
1975 	if (vp->v_type == VBLK || vp->v_type == VCHR)
1976 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
1977 		    &rdev, 8);
1978 
1979 	if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
1980 		zfs_exit(zfsvfs, FTAG);
1981 		return (error);
1982 	}
1983 
1984 	/*
1985 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
1986 	 * Also, if we are the owner don't bother, since owner should
1987 	 * always be allowed to read basic attributes of file.
1988 	 */
1989 	if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
1990 	    (vap->va_uid != crgetuid(cr))) {
1991 		if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
1992 		    skipaclchk, cr, NULL))) {
1993 			zfs_exit(zfsvfs, FTAG);
1994 			return (error);
1995 		}
1996 	}
1997 
1998 	/*
1999 	 * Return all attributes.  It's cheaper to provide the answer
2000 	 * than to determine whether we were asked the question.
2001 	 */
2002 
2003 	vap->va_type = IFTOVT(zp->z_mode);
2004 	vap->va_mode = zp->z_mode & ~S_IFMT;
2005 	vn_fsid(vp, vap);
2006 	vap->va_nodeid = zp->z_id;
2007 	vap->va_nlink = zp->z_links;
2008 	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp) &&
2009 	    zp->z_links < ZFS_LINK_MAX)
2010 		vap->va_nlink++;
2011 	vap->va_size = zp->z_size;
2012 	if (vp->v_type == VBLK || vp->v_type == VCHR)
2013 		vap->va_rdev = zfs_cmpldev(rdev);
2014 	else
2015 		vap->va_rdev = 0;
2016 	vap->va_gen = zp->z_gen;
2017 	vap->va_flags = 0;	/* FreeBSD: Reset chflags(2) flags. */
2018 	vap->va_filerev = zp->z_seq;
2019 
2020 	/*
2021 	 * Add in any requested optional attributes and the create time.
2022 	 * Also set the corresponding bits in the returned attribute bitmap.
2023 	 */
2024 	if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2025 		if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2026 			xoap->xoa_archive =
2027 			    ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2028 			XVA_SET_RTN(xvap, XAT_ARCHIVE);
2029 		}
2030 
2031 		if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2032 			xoap->xoa_readonly =
2033 			    ((zp->z_pflags & ZFS_READONLY) != 0);
2034 			XVA_SET_RTN(xvap, XAT_READONLY);
2035 		}
2036 
2037 		if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2038 			xoap->xoa_system =
2039 			    ((zp->z_pflags & ZFS_SYSTEM) != 0);
2040 			XVA_SET_RTN(xvap, XAT_SYSTEM);
2041 		}
2042 
2043 		if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2044 			xoap->xoa_hidden =
2045 			    ((zp->z_pflags & ZFS_HIDDEN) != 0);
2046 			XVA_SET_RTN(xvap, XAT_HIDDEN);
2047 		}
2048 
2049 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2050 			xoap->xoa_nounlink =
2051 			    ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2052 			XVA_SET_RTN(xvap, XAT_NOUNLINK);
2053 		}
2054 
2055 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2056 			xoap->xoa_immutable =
2057 			    ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2058 			XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2059 		}
2060 
2061 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2062 			xoap->xoa_appendonly =
2063 			    ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2064 			XVA_SET_RTN(xvap, XAT_APPENDONLY);
2065 		}
2066 
2067 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2068 			xoap->xoa_nodump =
2069 			    ((zp->z_pflags & ZFS_NODUMP) != 0);
2070 			XVA_SET_RTN(xvap, XAT_NODUMP);
2071 		}
2072 
2073 		if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2074 			xoap->xoa_opaque =
2075 			    ((zp->z_pflags & ZFS_OPAQUE) != 0);
2076 			XVA_SET_RTN(xvap, XAT_OPAQUE);
2077 		}
2078 
2079 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2080 			xoap->xoa_av_quarantined =
2081 			    ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2082 			XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2083 		}
2084 
2085 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2086 			xoap->xoa_av_modified =
2087 			    ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2088 			XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2089 		}
2090 
2091 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2092 		    vp->v_type == VREG) {
2093 			zfs_sa_get_scanstamp(zp, xvap);
2094 		}
2095 
2096 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2097 			xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2098 			XVA_SET_RTN(xvap, XAT_REPARSE);
2099 		}
2100 		if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2101 			xoap->xoa_generation = zp->z_gen;
2102 			XVA_SET_RTN(xvap, XAT_GEN);
2103 		}
2104 
2105 		if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2106 			xoap->xoa_offline =
2107 			    ((zp->z_pflags & ZFS_OFFLINE) != 0);
2108 			XVA_SET_RTN(xvap, XAT_OFFLINE);
2109 		}
2110 
2111 		if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2112 			xoap->xoa_sparse =
2113 			    ((zp->z_pflags & ZFS_SPARSE) != 0);
2114 			XVA_SET_RTN(xvap, XAT_SPARSE);
2115 		}
2116 
2117 		if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
2118 			xoap->xoa_projinherit =
2119 			    ((zp->z_pflags & ZFS_PROJINHERIT) != 0);
2120 			XVA_SET_RTN(xvap, XAT_PROJINHERIT);
2121 		}
2122 
2123 		if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
2124 			xoap->xoa_projid = zp->z_projid;
2125 			XVA_SET_RTN(xvap, XAT_PROJID);
2126 		}
2127 	}
2128 
2129 	ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2130 	ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2131 	ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2132 	ZFS_TIME_DECODE(&vap->va_birthtime, crtime);
2133 
2134 
2135 	sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2136 	vap->va_blksize = blksize;
2137 	vap->va_bytes = nblocks << 9;	/* nblocks * 512 */
2138 
2139 	if (zp->z_blksz == 0) {
2140 		/*
2141 		 * Block size hasn't been set; suggest maximal I/O transfers.
2142 		 */
2143 		vap->va_blksize = zfsvfs->z_max_blksz;
2144 	}
2145 
2146 	zfs_exit(zfsvfs, FTAG);
2147 	return (0);
2148 }
2149 
2150 /*
2151  * Set the file attributes to the values contained in the
2152  * vattr structure.
2153  *
2154  *	IN:	zp	- znode of file to be modified.
2155  *		vap	- new attribute values.
2156  *			  If AT_XVATTR set, then optional attrs are being set
2157  *		flags	- ATTR_UTIME set if non-default time values provided.
2158  *			- ATTR_NOACLCHECK (CIFS context only).
2159  *		cr	- credentials of caller.
2160  *		mnt_ns	- Unused on FreeBSD
2161  *
2162  *	RETURN:	0 on success, error code on failure.
2163  *
2164  * Timestamps:
2165  *	vp - ctime updated, mtime updated if size changed.
2166  */
2167 int
zfs_setattr(znode_t * zp,vattr_t * vap,int flags,cred_t * cr,zidmap_t * mnt_ns)2168 zfs_setattr(znode_t *zp, vattr_t *vap, int flags, cred_t *cr, zidmap_t *mnt_ns)
2169 {
2170 	vnode_t		*vp = ZTOV(zp);
2171 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2172 	objset_t	*os;
2173 	zilog_t		*zilog;
2174 	dmu_tx_t	*tx;
2175 	vattr_t		oldva;
2176 	xvattr_t	tmpxvattr;
2177 	uint_t		mask = vap->va_mask;
2178 	uint_t		saved_mask = 0;
2179 	uint64_t	saved_mode;
2180 	int		trim_mask = 0;
2181 	uint64_t	new_mode;
2182 	uint64_t	new_uid, new_gid;
2183 	uint64_t	xattr_obj;
2184 	uint64_t	mtime[2], ctime[2];
2185 	uint64_t	projid = ZFS_INVALID_PROJID;
2186 	znode_t		*attrzp;
2187 	int		need_policy = FALSE;
2188 	int		err, err2;
2189 	zfs_fuid_info_t *fuidp = NULL;
2190 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
2191 	xoptattr_t	*xoap;
2192 	zfs_acl_t	*aclp;
2193 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2194 	boolean_t	fuid_dirtied = B_FALSE;
2195 	sa_bulk_attr_t	bulk[7], xattr_bulk[7];
2196 	int		count = 0, xattr_count = 0;
2197 
2198 	if (mask == 0)
2199 		return (0);
2200 
2201 	if (mask & AT_NOSET)
2202 		return (SET_ERROR(EINVAL));
2203 
2204 	if ((err = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
2205 		return (err);
2206 
2207 	os = zfsvfs->z_os;
2208 	zilog = zfsvfs->z_log;
2209 
2210 	/*
2211 	 * Make sure that if we have ephemeral uid/gid or xvattr specified
2212 	 * that file system is at proper version level
2213 	 */
2214 
2215 	if (zfsvfs->z_use_fuids == B_FALSE &&
2216 	    (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2217 	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2218 	    (mask & AT_XVATTR))) {
2219 		zfs_exit(zfsvfs, FTAG);
2220 		return (SET_ERROR(EINVAL));
2221 	}
2222 
2223 	if (mask & AT_SIZE && vp->v_type == VDIR) {
2224 		zfs_exit(zfsvfs, FTAG);
2225 		return (SET_ERROR(EISDIR));
2226 	}
2227 
2228 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2229 		zfs_exit(zfsvfs, FTAG);
2230 		return (SET_ERROR(EINVAL));
2231 	}
2232 
2233 	/*
2234 	 * If this is an xvattr_t, then get a pointer to the structure of
2235 	 * optional attributes.  If this is NULL, then we have a vattr_t.
2236 	 */
2237 	xoap = xva_getxoptattr(xvap);
2238 
2239 	xva_init(&tmpxvattr);
2240 
2241 	/*
2242 	 * Immutable files can only alter immutable bit and atime
2243 	 */
2244 	if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2245 	    ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2246 	    ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2247 		zfs_exit(zfsvfs, FTAG);
2248 		return (SET_ERROR(EPERM));
2249 	}
2250 
2251 	/*
2252 	 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
2253 	 */
2254 
2255 	/*
2256 	 * Verify timestamps doesn't overflow 32 bits.
2257 	 * ZFS can handle large timestamps, but 32bit syscalls can't
2258 	 * handle times greater than 2039.  This check should be removed
2259 	 * once large timestamps are fully supported.
2260 	 */
2261 	if (mask & (AT_ATIME | AT_MTIME)) {
2262 		if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2263 		    ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2264 			zfs_exit(zfsvfs, FTAG);
2265 			return (SET_ERROR(EOVERFLOW));
2266 		}
2267 	}
2268 	if (xoap != NULL && (mask & AT_XVATTR)) {
2269 		if (XVA_ISSET_REQ(xvap, XAT_CREATETIME) &&
2270 		    TIMESPEC_OVERFLOW(&vap->va_birthtime)) {
2271 			zfs_exit(zfsvfs, FTAG);
2272 			return (SET_ERROR(EOVERFLOW));
2273 		}
2274 
2275 		if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
2276 			if (!dmu_objset_projectquota_enabled(os) ||
2277 			    (!S_ISREG(zp->z_mode) && !S_ISDIR(zp->z_mode))) {
2278 				zfs_exit(zfsvfs, FTAG);
2279 				return (SET_ERROR(EOPNOTSUPP));
2280 			}
2281 
2282 			projid = xoap->xoa_projid;
2283 			if (unlikely(projid == ZFS_INVALID_PROJID)) {
2284 				zfs_exit(zfsvfs, FTAG);
2285 				return (SET_ERROR(EINVAL));
2286 			}
2287 
2288 			if (projid == zp->z_projid && zp->z_pflags & ZFS_PROJID)
2289 				projid = ZFS_INVALID_PROJID;
2290 			else
2291 				need_policy = TRUE;
2292 		}
2293 
2294 		if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT) &&
2295 		    (xoap->xoa_projinherit !=
2296 		    ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) &&
2297 		    (!dmu_objset_projectquota_enabled(os) ||
2298 		    (!S_ISREG(zp->z_mode) && !S_ISDIR(zp->z_mode)))) {
2299 			zfs_exit(zfsvfs, FTAG);
2300 			return (SET_ERROR(EOPNOTSUPP));
2301 		}
2302 	}
2303 
2304 	attrzp = NULL;
2305 	aclp = NULL;
2306 
2307 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2308 		zfs_exit(zfsvfs, FTAG);
2309 		return (SET_ERROR(EROFS));
2310 	}
2311 
2312 	/*
2313 	 * First validate permissions
2314 	 */
2315 
2316 	if (mask & AT_SIZE) {
2317 		/*
2318 		 * XXX - Note, we are not providing any open
2319 		 * mode flags here (like FNDELAY), so we may
2320 		 * block if there are locks present... this
2321 		 * should be addressed in openat().
2322 		 */
2323 		/* XXX - would it be OK to generate a log record here? */
2324 		err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2325 		if (err) {
2326 			zfs_exit(zfsvfs, FTAG);
2327 			return (err);
2328 		}
2329 	}
2330 
2331 	if (mask & (AT_ATIME|AT_MTIME) ||
2332 	    ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2333 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2334 	    XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2335 	    XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2336 	    XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2337 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2338 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2339 		need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2340 		    skipaclchk, cr, mnt_ns);
2341 	}
2342 
2343 	if (mask & (AT_UID|AT_GID)) {
2344 		int	idmask = (mask & (AT_UID|AT_GID));
2345 		int	take_owner;
2346 		int	take_group;
2347 
2348 		/*
2349 		 * NOTE: even if a new mode is being set,
2350 		 * we may clear S_ISUID/S_ISGID bits.
2351 		 */
2352 
2353 		if (!(mask & AT_MODE))
2354 			vap->va_mode = zp->z_mode;
2355 
2356 		/*
2357 		 * Take ownership or chgrp to group we are a member of
2358 		 */
2359 
2360 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2361 		take_group = (mask & AT_GID) &&
2362 		    zfs_groupmember(zfsvfs, vap->va_gid, cr);
2363 
2364 		/*
2365 		 * If both AT_UID and AT_GID are set then take_owner and
2366 		 * take_group must both be set in order to allow taking
2367 		 * ownership.
2368 		 *
2369 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2370 		 *
2371 		 */
2372 
2373 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2374 		    ((idmask == AT_UID) && take_owner) ||
2375 		    ((idmask == AT_GID) && take_group)) {
2376 			if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2377 			    skipaclchk, cr, mnt_ns) == 0) {
2378 				/*
2379 				 * Remove setuid/setgid for non-privileged users
2380 				 */
2381 				secpolicy_setid_clear(vap, vp, cr);
2382 				trim_mask = (mask & (AT_UID|AT_GID));
2383 			} else {
2384 				need_policy =  TRUE;
2385 			}
2386 		} else {
2387 			need_policy =  TRUE;
2388 		}
2389 	}
2390 
2391 	oldva.va_mode = zp->z_mode;
2392 	zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2393 	if (mask & AT_XVATTR) {
2394 		/*
2395 		 * Update xvattr mask to include only those attributes
2396 		 * that are actually changing.
2397 		 *
2398 		 * the bits will be restored prior to actually setting
2399 		 * the attributes so the caller thinks they were set.
2400 		 */
2401 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2402 			if (xoap->xoa_appendonly !=
2403 			    ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2404 				need_policy = TRUE;
2405 			} else {
2406 				XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2407 				XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
2408 			}
2409 		}
2410 
2411 		if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
2412 			if (xoap->xoa_projinherit !=
2413 			    ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) {
2414 				need_policy = TRUE;
2415 			} else {
2416 				XVA_CLR_REQ(xvap, XAT_PROJINHERIT);
2417 				XVA_SET_REQ(&tmpxvattr, XAT_PROJINHERIT);
2418 			}
2419 		}
2420 
2421 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2422 			if (xoap->xoa_nounlink !=
2423 			    ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2424 				need_policy = TRUE;
2425 			} else {
2426 				XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2427 				XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
2428 			}
2429 		}
2430 
2431 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2432 			if (xoap->xoa_immutable !=
2433 			    ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2434 				need_policy = TRUE;
2435 			} else {
2436 				XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2437 				XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
2438 			}
2439 		}
2440 
2441 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2442 			if (xoap->xoa_nodump !=
2443 			    ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2444 				need_policy = TRUE;
2445 			} else {
2446 				XVA_CLR_REQ(xvap, XAT_NODUMP);
2447 				XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
2448 			}
2449 		}
2450 
2451 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2452 			if (xoap->xoa_av_modified !=
2453 			    ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2454 				need_policy = TRUE;
2455 			} else {
2456 				XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2457 				XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
2458 			}
2459 		}
2460 
2461 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2462 			if ((vp->v_type != VREG &&
2463 			    xoap->xoa_av_quarantined) ||
2464 			    xoap->xoa_av_quarantined !=
2465 			    ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2466 				need_policy = TRUE;
2467 			} else {
2468 				XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2469 				XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
2470 			}
2471 		}
2472 
2473 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2474 			zfs_exit(zfsvfs, FTAG);
2475 			return (SET_ERROR(EPERM));
2476 		}
2477 
2478 		if (need_policy == FALSE &&
2479 		    (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2480 		    XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2481 			need_policy = TRUE;
2482 		}
2483 	}
2484 
2485 	if (mask & AT_MODE) {
2486 		if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr,
2487 		    mnt_ns) == 0) {
2488 			err = secpolicy_setid_setsticky_clear(vp, vap,
2489 			    &oldva, cr);
2490 			if (err) {
2491 				zfs_exit(zfsvfs, FTAG);
2492 				return (err);
2493 			}
2494 			trim_mask |= AT_MODE;
2495 		} else {
2496 			need_policy = TRUE;
2497 		}
2498 	}
2499 
2500 	if (need_policy) {
2501 		/*
2502 		 * If trim_mask is set then take ownership
2503 		 * has been granted or write_acl is present and user
2504 		 * has the ability to modify mode.  In that case remove
2505 		 * UID|GID and or MODE from mask so that
2506 		 * secpolicy_vnode_setattr() doesn't revoke it.
2507 		 */
2508 
2509 		if (trim_mask) {
2510 			saved_mask = vap->va_mask;
2511 			vap->va_mask &= ~trim_mask;
2512 			if (trim_mask & AT_MODE) {
2513 				/*
2514 				 * Save the mode, as secpolicy_vnode_setattr()
2515 				 * will overwrite it with ova.va_mode.
2516 				 */
2517 				saved_mode = vap->va_mode;
2518 			}
2519 		}
2520 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2521 		    (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2522 		if (err) {
2523 			zfs_exit(zfsvfs, FTAG);
2524 			return (err);
2525 		}
2526 
2527 		if (trim_mask) {
2528 			vap->va_mask |= saved_mask;
2529 			if (trim_mask & AT_MODE) {
2530 				/*
2531 				 * Recover the mode after
2532 				 * secpolicy_vnode_setattr().
2533 				 */
2534 				vap->va_mode = saved_mode;
2535 			}
2536 		}
2537 	}
2538 
2539 	/*
2540 	 * secpolicy_vnode_setattr, or take ownership may have
2541 	 * changed va_mask
2542 	 */
2543 	mask = vap->va_mask;
2544 
2545 	if ((mask & (AT_UID | AT_GID)) || projid != ZFS_INVALID_PROJID) {
2546 		err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
2547 		    &xattr_obj, sizeof (xattr_obj));
2548 
2549 		if (err == 0 && xattr_obj) {
2550 			err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
2551 			if (err == 0) {
2552 				err = vn_lock(ZTOV(attrzp), LK_EXCLUSIVE);
2553 				if (err != 0)
2554 					vrele(ZTOV(attrzp));
2555 			}
2556 			if (err)
2557 				goto out2;
2558 		}
2559 		if (mask & AT_UID) {
2560 			new_uid = zfs_fuid_create(zfsvfs,
2561 			    (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2562 			if (new_uid != zp->z_uid &&
2563 			    zfs_id_overquota(zfsvfs, DMU_USERUSED_OBJECT,
2564 			    new_uid)) {
2565 				if (attrzp)
2566 					vput(ZTOV(attrzp));
2567 				err = SET_ERROR(EDQUOT);
2568 				goto out2;
2569 			}
2570 		}
2571 
2572 		if (mask & AT_GID) {
2573 			new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
2574 			    cr, ZFS_GROUP, &fuidp);
2575 			if (new_gid != zp->z_gid &&
2576 			    zfs_id_overquota(zfsvfs, DMU_GROUPUSED_OBJECT,
2577 			    new_gid)) {
2578 				if (attrzp)
2579 					vput(ZTOV(attrzp));
2580 				err = SET_ERROR(EDQUOT);
2581 				goto out2;
2582 			}
2583 		}
2584 
2585 		if (projid != ZFS_INVALID_PROJID &&
2586 		    zfs_id_overquota(zfsvfs, DMU_PROJECTUSED_OBJECT, projid)) {
2587 			if (attrzp)
2588 				vput(ZTOV(attrzp));
2589 			err = SET_ERROR(EDQUOT);
2590 			goto out2;
2591 		}
2592 	}
2593 	tx = dmu_tx_create(os);
2594 
2595 	if (mask & AT_MODE) {
2596 		uint64_t pmode = zp->z_mode;
2597 		uint64_t acl_obj;
2598 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2599 
2600 		if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
2601 		    !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
2602 			err = SET_ERROR(EPERM);
2603 			goto out;
2604 		}
2605 
2606 		if ((err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)))
2607 			goto out;
2608 
2609 		if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
2610 			/*
2611 			 * Are we upgrading ACL from old V0 format
2612 			 * to V1 format?
2613 			 */
2614 			if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
2615 			    zfs_znode_acl_version(zp) ==
2616 			    ZFS_ACL_VERSION_INITIAL) {
2617 				dmu_tx_hold_free(tx, acl_obj, 0,
2618 				    DMU_OBJECT_END);
2619 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2620 				    0, aclp->z_acl_bytes);
2621 			} else {
2622 				dmu_tx_hold_write(tx, acl_obj, 0,
2623 				    aclp->z_acl_bytes);
2624 			}
2625 		} else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2626 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2627 			    0, aclp->z_acl_bytes);
2628 		}
2629 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2630 	} else {
2631 		if (((mask & AT_XVATTR) &&
2632 		    XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) ||
2633 		    (projid != ZFS_INVALID_PROJID &&
2634 		    !(zp->z_pflags & ZFS_PROJID)))
2635 			dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2636 		else
2637 			dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2638 	}
2639 
2640 	if (attrzp) {
2641 		dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
2642 	}
2643 
2644 	fuid_dirtied = zfsvfs->z_fuid_dirty;
2645 	if (fuid_dirtied)
2646 		zfs_fuid_txhold(zfsvfs, tx);
2647 
2648 	zfs_sa_upgrade_txholds(tx, zp);
2649 
2650 	err = dmu_tx_assign(tx, TXG_WAIT);
2651 	if (err)
2652 		goto out;
2653 
2654 	count = 0;
2655 	/*
2656 	 * Set each attribute requested.
2657 	 * We group settings according to the locks they need to acquire.
2658 	 *
2659 	 * Note: you cannot set ctime directly, although it will be
2660 	 * updated as a side-effect of calling this function.
2661 	 */
2662 
2663 	if (projid != ZFS_INVALID_PROJID && !(zp->z_pflags & ZFS_PROJID)) {
2664 		/*
2665 		 * For the existed object that is upgraded from old system,
2666 		 * its on-disk layout has no slot for the project ID attribute.
2667 		 * But quota accounting logic needs to access related slots by
2668 		 * offset directly. So we need to adjust old objects' layout
2669 		 * to make the project ID to some unified and fixed offset.
2670 		 */
2671 		if (attrzp)
2672 			err = sa_add_projid(attrzp->z_sa_hdl, tx, projid);
2673 		if (err == 0)
2674 			err = sa_add_projid(zp->z_sa_hdl, tx, projid);
2675 
2676 		if (unlikely(err == EEXIST))
2677 			err = 0;
2678 		else if (err != 0)
2679 			goto out;
2680 		else
2681 			projid = ZFS_INVALID_PROJID;
2682 	}
2683 
2684 	if (mask & (AT_UID|AT_GID|AT_MODE))
2685 		mutex_enter(&zp->z_acl_lock);
2686 
2687 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
2688 	    &zp->z_pflags, sizeof (zp->z_pflags));
2689 
2690 	if (attrzp) {
2691 		if (mask & (AT_UID|AT_GID|AT_MODE))
2692 			mutex_enter(&attrzp->z_acl_lock);
2693 		SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2694 		    SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
2695 		    sizeof (attrzp->z_pflags));
2696 		if (projid != ZFS_INVALID_PROJID) {
2697 			attrzp->z_projid = projid;
2698 			SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2699 			    SA_ZPL_PROJID(zfsvfs), NULL, &attrzp->z_projid,
2700 			    sizeof (attrzp->z_projid));
2701 		}
2702 	}
2703 
2704 	if (mask & (AT_UID|AT_GID)) {
2705 
2706 		if (mask & AT_UID) {
2707 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
2708 			    &new_uid, sizeof (new_uid));
2709 			zp->z_uid = new_uid;
2710 			if (attrzp) {
2711 				SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2712 				    SA_ZPL_UID(zfsvfs), NULL, &new_uid,
2713 				    sizeof (new_uid));
2714 				attrzp->z_uid = new_uid;
2715 			}
2716 		}
2717 
2718 		if (mask & AT_GID) {
2719 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
2720 			    NULL, &new_gid, sizeof (new_gid));
2721 			zp->z_gid = new_gid;
2722 			if (attrzp) {
2723 				SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2724 				    SA_ZPL_GID(zfsvfs), NULL, &new_gid,
2725 				    sizeof (new_gid));
2726 				attrzp->z_gid = new_gid;
2727 			}
2728 		}
2729 		if (!(mask & AT_MODE)) {
2730 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
2731 			    NULL, &new_mode, sizeof (new_mode));
2732 			new_mode = zp->z_mode;
2733 		}
2734 		err = zfs_acl_chown_setattr(zp);
2735 		ASSERT0(err);
2736 		if (attrzp) {
2737 			vn_seqc_write_begin(ZTOV(attrzp));
2738 			err = zfs_acl_chown_setattr(attrzp);
2739 			vn_seqc_write_end(ZTOV(attrzp));
2740 			ASSERT0(err);
2741 		}
2742 	}
2743 
2744 	if (mask & AT_MODE) {
2745 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
2746 		    &new_mode, sizeof (new_mode));
2747 		zp->z_mode = new_mode;
2748 		ASSERT3P(aclp, !=, NULL);
2749 		err = zfs_aclset_common(zp, aclp, cr, tx);
2750 		ASSERT0(err);
2751 		if (zp->z_acl_cached)
2752 			zfs_acl_free(zp->z_acl_cached);
2753 		zp->z_acl_cached = aclp;
2754 		aclp = NULL;
2755 	}
2756 
2757 
2758 	if (mask & AT_ATIME) {
2759 		ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
2760 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
2761 		    &zp->z_atime, sizeof (zp->z_atime));
2762 	}
2763 
2764 	if (mask & AT_MTIME) {
2765 		ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
2766 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
2767 		    mtime, sizeof (mtime));
2768 	}
2769 
2770 	if (projid != ZFS_INVALID_PROJID) {
2771 		zp->z_projid = projid;
2772 		SA_ADD_BULK_ATTR(bulk, count,
2773 		    SA_ZPL_PROJID(zfsvfs), NULL, &zp->z_projid,
2774 		    sizeof (zp->z_projid));
2775 	}
2776 
2777 	/* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2778 	if (mask & AT_SIZE && !(mask & AT_MTIME)) {
2779 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
2780 		    NULL, mtime, sizeof (mtime));
2781 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
2782 		    &ctime, sizeof (ctime));
2783 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
2784 	} else if (mask != 0) {
2785 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
2786 		    &ctime, sizeof (ctime));
2787 		zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime);
2788 		if (attrzp) {
2789 			SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2790 			    SA_ZPL_CTIME(zfsvfs), NULL,
2791 			    &ctime, sizeof (ctime));
2792 			zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
2793 			    mtime, ctime);
2794 		}
2795 	}
2796 
2797 	/*
2798 	 * Do this after setting timestamps to prevent timestamp
2799 	 * update from toggling bit
2800 	 */
2801 
2802 	if (xoap && (mask & AT_XVATTR)) {
2803 
2804 		if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
2805 			xoap->xoa_createtime = vap->va_birthtime;
2806 		/*
2807 		 * restore trimmed off masks
2808 		 * so that return masks can be set for caller.
2809 		 */
2810 
2811 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
2812 			XVA_SET_REQ(xvap, XAT_APPENDONLY);
2813 		}
2814 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
2815 			XVA_SET_REQ(xvap, XAT_NOUNLINK);
2816 		}
2817 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
2818 			XVA_SET_REQ(xvap, XAT_IMMUTABLE);
2819 		}
2820 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
2821 			XVA_SET_REQ(xvap, XAT_NODUMP);
2822 		}
2823 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
2824 			XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
2825 		}
2826 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
2827 			XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
2828 		}
2829 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_PROJINHERIT)) {
2830 			XVA_SET_REQ(xvap, XAT_PROJINHERIT);
2831 		}
2832 
2833 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2834 			ASSERT3S(vp->v_type, ==, VREG);
2835 
2836 		zfs_xvattr_set(zp, xvap, tx);
2837 	}
2838 
2839 	if (fuid_dirtied)
2840 		zfs_fuid_sync(zfsvfs, tx);
2841 
2842 	if (mask != 0)
2843 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
2844 
2845 	if (mask & (AT_UID|AT_GID|AT_MODE))
2846 		mutex_exit(&zp->z_acl_lock);
2847 
2848 	if (attrzp) {
2849 		if (mask & (AT_UID|AT_GID|AT_MODE))
2850 			mutex_exit(&attrzp->z_acl_lock);
2851 	}
2852 out:
2853 	if (err == 0 && attrzp) {
2854 		err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
2855 		    xattr_count, tx);
2856 		ASSERT0(err2);
2857 	}
2858 
2859 	if (attrzp)
2860 		vput(ZTOV(attrzp));
2861 
2862 	if (aclp)
2863 		zfs_acl_free(aclp);
2864 
2865 	if (fuidp) {
2866 		zfs_fuid_info_free(fuidp);
2867 		fuidp = NULL;
2868 	}
2869 
2870 	if (err) {
2871 		dmu_tx_abort(tx);
2872 	} else {
2873 		err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
2874 		dmu_tx_commit(tx);
2875 	}
2876 
2877 out2:
2878 	if (os->os_sync == ZFS_SYNC_ALWAYS)
2879 		zil_commit(zilog, 0);
2880 
2881 	zfs_exit(zfsvfs, FTAG);
2882 	return (err);
2883 }
2884 
2885 /*
2886  * Look up the directory entries corresponding to the source and target
2887  * directory/name pairs.
2888  */
2889 static int
zfs_rename_relock_lookup(znode_t * sdzp,const struct componentname * scnp,znode_t ** szpp,znode_t * tdzp,const struct componentname * tcnp,znode_t ** tzpp)2890 zfs_rename_relock_lookup(znode_t *sdzp, const struct componentname *scnp,
2891     znode_t **szpp, znode_t *tdzp, const struct componentname *tcnp,
2892     znode_t **tzpp)
2893 {
2894 	zfsvfs_t *zfsvfs;
2895 	znode_t *szp, *tzp;
2896 	int error;
2897 
2898 	/*
2899 	 * Before using sdzp and tdzp we must ensure that they are live.
2900 	 * As a porting legacy from illumos we have two things to worry
2901 	 * about.  One is typical for FreeBSD and it is that the vnode is
2902 	 * not reclaimed (doomed).  The other is that the znode is live.
2903 	 * The current code can invalidate the znode without acquiring the
2904 	 * corresponding vnode lock if the object represented by the znode
2905 	 * and vnode is no longer valid after a rollback or receive operation.
2906 	 * z_teardown_lock hidden behind zfs_enter and zfs_exit is the lock
2907 	 * that protects the znodes from the invalidation.
2908 	 */
2909 	zfsvfs = sdzp->z_zfsvfs;
2910 	ASSERT3P(zfsvfs, ==, tdzp->z_zfsvfs);
2911 	if ((error = zfs_enter_verify_zp(zfsvfs, sdzp, FTAG)) != 0)
2912 		return (error);
2913 	if ((error = zfs_verify_zp(tdzp)) != 0) {
2914 		zfs_exit(zfsvfs, FTAG);
2915 		return (error);
2916 	}
2917 
2918 	/*
2919 	 * Re-resolve svp to be certain it still exists and fetch the
2920 	 * correct vnode.
2921 	 */
2922 	error = zfs_dirent_lookup(sdzp, scnp->cn_nameptr, &szp, ZEXISTS);
2923 	if (error != 0) {
2924 		/* Source entry invalid or not there. */
2925 		if ((scnp->cn_flags & ISDOTDOT) != 0 ||
2926 		    (scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.'))
2927 			error = SET_ERROR(EINVAL);
2928 		goto out;
2929 	}
2930 	*szpp = szp;
2931 
2932 	/*
2933 	 * Re-resolve tvp, if it disappeared we just carry on.
2934 	 */
2935 	error = zfs_dirent_lookup(tdzp, tcnp->cn_nameptr, &tzp, 0);
2936 	if (error != 0) {
2937 		vrele(ZTOV(szp));
2938 		if ((tcnp->cn_flags & ISDOTDOT) != 0)
2939 			error = SET_ERROR(EINVAL);
2940 		goto out;
2941 	}
2942 	*tzpp = tzp;
2943 out:
2944 	zfs_exit(zfsvfs, FTAG);
2945 	return (error);
2946 }
2947 
2948 /*
2949  * We acquire all but fdvp locks using non-blocking acquisitions.  If we
2950  * fail to acquire any lock in the path we will drop all held locks,
2951  * acquire the new lock in a blocking fashion, and then release it and
2952  * restart the rename.  This acquire/release step ensures that we do not
2953  * spin on a lock waiting for release.  On error release all vnode locks
2954  * and decrement references the way tmpfs_rename() would do.
2955  */
2956 static int
zfs_rename_relock(struct vnode * sdvp,struct vnode ** svpp,struct vnode * tdvp,struct vnode ** tvpp,const struct componentname * scnp,const struct componentname * tcnp)2957 zfs_rename_relock(struct vnode *sdvp, struct vnode **svpp,
2958     struct vnode *tdvp, struct vnode **tvpp,
2959     const struct componentname *scnp, const struct componentname *tcnp)
2960 {
2961 	struct vnode	*nvp, *svp, *tvp;
2962 	znode_t		*sdzp, *tdzp, *szp, *tzp;
2963 	int		error;
2964 
2965 	VOP_UNLOCK1(tdvp);
2966 	if (*tvpp != NULL && *tvpp != tdvp)
2967 		VOP_UNLOCK1(*tvpp);
2968 
2969 relock:
2970 	error = vn_lock(sdvp, LK_EXCLUSIVE);
2971 	if (error)
2972 		goto out;
2973 	error = vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT);
2974 	if (error != 0) {
2975 		VOP_UNLOCK1(sdvp);
2976 		if (error != EBUSY)
2977 			goto out;
2978 		error = vn_lock(tdvp, LK_EXCLUSIVE);
2979 		if (error)
2980 			goto out;
2981 		VOP_UNLOCK1(tdvp);
2982 		goto relock;
2983 	}
2984 	tdzp = VTOZ(tdvp);
2985 	sdzp = VTOZ(sdvp);
2986 
2987 	error = zfs_rename_relock_lookup(sdzp, scnp, &szp, tdzp, tcnp, &tzp);
2988 	if (error != 0) {
2989 		VOP_UNLOCK1(sdvp);
2990 		VOP_UNLOCK1(tdvp);
2991 		goto out;
2992 	}
2993 	svp = ZTOV(szp);
2994 	tvp = tzp != NULL ? ZTOV(tzp) : NULL;
2995 
2996 	/*
2997 	 * Now try acquire locks on svp and tvp.
2998 	 */
2999 	nvp = svp;
3000 	error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3001 	if (error != 0) {
3002 		VOP_UNLOCK1(sdvp);
3003 		VOP_UNLOCK1(tdvp);
3004 		if (tvp != NULL)
3005 			vrele(tvp);
3006 		if (error != EBUSY) {
3007 			vrele(nvp);
3008 			goto out;
3009 		}
3010 		error = vn_lock(nvp, LK_EXCLUSIVE);
3011 		if (error != 0) {
3012 			vrele(nvp);
3013 			goto out;
3014 		}
3015 		VOP_UNLOCK1(nvp);
3016 		/*
3017 		 * Concurrent rename race.
3018 		 * XXX ?
3019 		 */
3020 		if (nvp == tdvp) {
3021 			vrele(nvp);
3022 			error = SET_ERROR(EINVAL);
3023 			goto out;
3024 		}
3025 		vrele(*svpp);
3026 		*svpp = nvp;
3027 		goto relock;
3028 	}
3029 	vrele(*svpp);
3030 	*svpp = nvp;
3031 
3032 	if (*tvpp != NULL)
3033 		vrele(*tvpp);
3034 	*tvpp = NULL;
3035 	if (tvp != NULL) {
3036 		nvp = tvp;
3037 		error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3038 		if (error != 0) {
3039 			VOP_UNLOCK1(sdvp);
3040 			VOP_UNLOCK1(tdvp);
3041 			VOP_UNLOCK1(*svpp);
3042 			if (error != EBUSY) {
3043 				vrele(nvp);
3044 				goto out;
3045 			}
3046 			error = vn_lock(nvp, LK_EXCLUSIVE);
3047 			if (error != 0) {
3048 				vrele(nvp);
3049 				goto out;
3050 			}
3051 			vput(nvp);
3052 			goto relock;
3053 		}
3054 		*tvpp = nvp;
3055 	}
3056 
3057 	return (0);
3058 
3059 out:
3060 	return (error);
3061 }
3062 
3063 /*
3064  * Note that we must use VRELE_ASYNC in this function as it walks
3065  * up the directory tree and vrele may need to acquire an exclusive
3066  * lock if a last reference to a vnode is dropped.
3067  */
3068 static int
zfs_rename_check(znode_t * szp,znode_t * sdzp,znode_t * tdzp)3069 zfs_rename_check(znode_t *szp, znode_t *sdzp, znode_t *tdzp)
3070 {
3071 	zfsvfs_t	*zfsvfs;
3072 	znode_t		*zp, *zp1;
3073 	uint64_t	parent;
3074 	int		error;
3075 
3076 	zfsvfs = tdzp->z_zfsvfs;
3077 	if (tdzp == szp)
3078 		return (SET_ERROR(EINVAL));
3079 	if (tdzp == sdzp)
3080 		return (0);
3081 	if (tdzp->z_id == zfsvfs->z_root)
3082 		return (0);
3083 	zp = tdzp;
3084 	for (;;) {
3085 		ASSERT(!zp->z_unlinked);
3086 		if ((error = sa_lookup(zp->z_sa_hdl,
3087 		    SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
3088 			break;
3089 
3090 		if (parent == szp->z_id) {
3091 			error = SET_ERROR(EINVAL);
3092 			break;
3093 		}
3094 		if (parent == zfsvfs->z_root)
3095 			break;
3096 		if (parent == sdzp->z_id)
3097 			break;
3098 
3099 		error = zfs_zget(zfsvfs, parent, &zp1);
3100 		if (error != 0)
3101 			break;
3102 
3103 		if (zp != tdzp)
3104 			VN_RELE_ASYNC(ZTOV(zp),
3105 			    dsl_pool_zrele_taskq(
3106 			    dmu_objset_pool(zfsvfs->z_os)));
3107 		zp = zp1;
3108 	}
3109 
3110 	if (error == ENOTDIR)
3111 		panic("checkpath: .. not a directory\n");
3112 	if (zp != tdzp)
3113 		VN_RELE_ASYNC(ZTOV(zp),
3114 		    dsl_pool_zrele_taskq(dmu_objset_pool(zfsvfs->z_os)));
3115 	return (error);
3116 }
3117 
3118 #if	__FreeBSD_version < 1300124
3119 static void
cache_vop_rename(struct vnode * fdvp,struct vnode * fvp,struct vnode * tdvp,struct vnode * tvp,struct componentname * fcnp,struct componentname * tcnp)3120 cache_vop_rename(struct vnode *fdvp, struct vnode *fvp, struct vnode *tdvp,
3121     struct vnode *tvp, struct componentname *fcnp, struct componentname *tcnp)
3122 {
3123 
3124 	cache_purge(fvp);
3125 	if (tvp != NULL)
3126 		cache_purge(tvp);
3127 	cache_purge_negative(tdvp);
3128 }
3129 #endif
3130 
3131 static int
3132 zfs_do_rename_impl(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3133     vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3134     cred_t *cr);
3135 
3136 /*
3137  * Move an entry from the provided source directory to the target
3138  * directory.  Change the entry name as indicated.
3139  *
3140  *	IN:	sdvp	- Source directory containing the "old entry".
3141  *		scnp	- Old entry name.
3142  *		tdvp	- Target directory to contain the "new entry".
3143  *		tcnp	- New entry name.
3144  *		cr	- credentials of caller.
3145  *	INOUT:	svpp	- Source file
3146  *		tvpp	- Target file, may point to NULL initially
3147  *
3148  *	RETURN:	0 on success, error code on failure.
3149  *
3150  * Timestamps:
3151  *	sdvp,tdvp - ctime|mtime updated
3152  */
3153 static int
zfs_do_rename(vnode_t * sdvp,vnode_t ** svpp,struct componentname * scnp,vnode_t * tdvp,vnode_t ** tvpp,struct componentname * tcnp,cred_t * cr)3154 zfs_do_rename(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3155     vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3156     cred_t *cr)
3157 {
3158 	int	error;
3159 
3160 	ASSERT_VOP_ELOCKED(tdvp, __func__);
3161 	if (*tvpp != NULL)
3162 		ASSERT_VOP_ELOCKED(*tvpp, __func__);
3163 
3164 	/* Reject renames across filesystems. */
3165 	if ((*svpp)->v_mount != tdvp->v_mount ||
3166 	    ((*tvpp) != NULL && (*svpp)->v_mount != (*tvpp)->v_mount)) {
3167 		error = SET_ERROR(EXDEV);
3168 		goto out;
3169 	}
3170 
3171 	if (zfsctl_is_node(tdvp)) {
3172 		error = SET_ERROR(EXDEV);
3173 		goto out;
3174 	}
3175 
3176 	/*
3177 	 * Lock all four vnodes to ensure safety and semantics of renaming.
3178 	 */
3179 	error = zfs_rename_relock(sdvp, svpp, tdvp, tvpp, scnp, tcnp);
3180 	if (error != 0) {
3181 		/* no vnodes are locked in the case of error here */
3182 		return (error);
3183 	}
3184 
3185 	error = zfs_do_rename_impl(sdvp, svpp, scnp, tdvp, tvpp, tcnp, cr);
3186 	VOP_UNLOCK1(sdvp);
3187 	VOP_UNLOCK1(*svpp);
3188 out:
3189 	if (*tvpp != NULL)
3190 		VOP_UNLOCK1(*tvpp);
3191 	if (tdvp != *tvpp)
3192 		VOP_UNLOCK1(tdvp);
3193 
3194 	return (error);
3195 }
3196 
3197 static int
zfs_do_rename_impl(vnode_t * sdvp,vnode_t ** svpp,struct componentname * scnp,vnode_t * tdvp,vnode_t ** tvpp,struct componentname * tcnp,cred_t * cr)3198 zfs_do_rename_impl(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3199     vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3200     cred_t *cr)
3201 {
3202 	dmu_tx_t	*tx;
3203 	zfsvfs_t	*zfsvfs;
3204 	zilog_t		*zilog;
3205 	znode_t		*tdzp, *sdzp, *tzp, *szp;
3206 	const char	*snm = scnp->cn_nameptr;
3207 	const char	*tnm = tcnp->cn_nameptr;
3208 	int		error;
3209 
3210 	tdzp = VTOZ(tdvp);
3211 	sdzp = VTOZ(sdvp);
3212 	zfsvfs = tdzp->z_zfsvfs;
3213 
3214 	if ((error = zfs_enter_verify_zp(zfsvfs, tdzp, FTAG)) != 0)
3215 		return (error);
3216 	if ((error = zfs_verify_zp(sdzp)) != 0) {
3217 		zfs_exit(zfsvfs, FTAG);
3218 		return (error);
3219 	}
3220 	zilog = zfsvfs->z_log;
3221 
3222 	if (zfsvfs->z_utf8 && u8_validate(tnm,
3223 	    strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3224 		error = SET_ERROR(EILSEQ);
3225 		goto out;
3226 	}
3227 
3228 	/* If source and target are the same file, there is nothing to do. */
3229 	if ((*svpp) == (*tvpp)) {
3230 		error = 0;
3231 		goto out;
3232 	}
3233 
3234 	if (((*svpp)->v_type == VDIR && (*svpp)->v_mountedhere != NULL) ||
3235 	    ((*tvpp) != NULL && (*tvpp)->v_type == VDIR &&
3236 	    (*tvpp)->v_mountedhere != NULL)) {
3237 		error = SET_ERROR(EXDEV);
3238 		goto out;
3239 	}
3240 
3241 	szp = VTOZ(*svpp);
3242 	if ((error = zfs_verify_zp(szp)) != 0) {
3243 		zfs_exit(zfsvfs, FTAG);
3244 		return (error);
3245 	}
3246 	tzp = *tvpp == NULL ? NULL : VTOZ(*tvpp);
3247 	if (tzp != NULL) {
3248 		if ((error = zfs_verify_zp(tzp)) != 0) {
3249 			zfs_exit(zfsvfs, FTAG);
3250 			return (error);
3251 		}
3252 	}
3253 
3254 	/*
3255 	 * This is to prevent the creation of links into attribute space
3256 	 * by renaming a linked file into/outof an attribute directory.
3257 	 * See the comment in zfs_link() for why this is considered bad.
3258 	 */
3259 	if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3260 		error = SET_ERROR(EINVAL);
3261 		goto out;
3262 	}
3263 
3264 	/*
3265 	 * If we are using project inheritance, means if the directory has
3266 	 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3267 	 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3268 	 * such case, we only allow renames into our tree when the project
3269 	 * IDs are the same.
3270 	 */
3271 	if (tdzp->z_pflags & ZFS_PROJINHERIT &&
3272 	    tdzp->z_projid != szp->z_projid) {
3273 		error = SET_ERROR(EXDEV);
3274 		goto out;
3275 	}
3276 
3277 	/*
3278 	 * Must have write access at the source to remove the old entry
3279 	 * and write access at the target to create the new entry.
3280 	 * Note that if target and source are the same, this can be
3281 	 * done in a single check.
3282 	 */
3283 	if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr, NULL)))
3284 		goto out;
3285 
3286 	if ((*svpp)->v_type == VDIR) {
3287 		/*
3288 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
3289 		 */
3290 		if ((scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.') ||
3291 		    sdzp == szp ||
3292 		    (scnp->cn_flags | tcnp->cn_flags) & ISDOTDOT) {
3293 			error = EINVAL;
3294 			goto out;
3295 		}
3296 
3297 		/*
3298 		 * Check to make sure rename is valid.
3299 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3300 		 */
3301 		if ((error = zfs_rename_check(szp, sdzp, tdzp)))
3302 			goto out;
3303 	}
3304 
3305 	/*
3306 	 * Does target exist?
3307 	 */
3308 	if (tzp) {
3309 		/*
3310 		 * Source and target must be the same type.
3311 		 */
3312 		if ((*svpp)->v_type == VDIR) {
3313 			if ((*tvpp)->v_type != VDIR) {
3314 				error = SET_ERROR(ENOTDIR);
3315 				goto out;
3316 			} else {
3317 				cache_purge(tdvp);
3318 				if (sdvp != tdvp)
3319 					cache_purge(sdvp);
3320 			}
3321 		} else {
3322 			if ((*tvpp)->v_type == VDIR) {
3323 				error = SET_ERROR(EISDIR);
3324 				goto out;
3325 			}
3326 		}
3327 	}
3328 
3329 	vn_seqc_write_begin(*svpp);
3330 	vn_seqc_write_begin(sdvp);
3331 	if (*tvpp != NULL)
3332 		vn_seqc_write_begin(*tvpp);
3333 	if (tdvp != *tvpp)
3334 		vn_seqc_write_begin(tdvp);
3335 
3336 	vnevent_rename_src(*svpp, sdvp, scnp->cn_nameptr, ct);
3337 	if (tzp)
3338 		vnevent_rename_dest(*tvpp, tdvp, tnm, ct);
3339 
3340 	/*
3341 	 * notify the target directory if it is not the same
3342 	 * as source directory.
3343 	 */
3344 	if (tdvp != sdvp) {
3345 		vnevent_rename_dest_dir(tdvp, ct);
3346 	}
3347 
3348 	tx = dmu_tx_create(zfsvfs->z_os);
3349 	dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3350 	dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3351 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3352 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3353 	if (sdzp != tdzp) {
3354 		dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3355 		zfs_sa_upgrade_txholds(tx, tdzp);
3356 	}
3357 	if (tzp) {
3358 		dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3359 		zfs_sa_upgrade_txholds(tx, tzp);
3360 	}
3361 
3362 	zfs_sa_upgrade_txholds(tx, szp);
3363 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3364 	error = dmu_tx_assign(tx, TXG_WAIT);
3365 	if (error) {
3366 		dmu_tx_abort(tx);
3367 		goto out_seq;
3368 	}
3369 
3370 	if (tzp)	/* Attempt to remove the existing target */
3371 		error = zfs_link_destroy(tdzp, tnm, tzp, tx, 0, NULL);
3372 
3373 	if (error == 0) {
3374 		error = zfs_link_create(tdzp, tnm, szp, tx, ZRENAMING);
3375 		if (error == 0) {
3376 			szp->z_pflags |= ZFS_AV_MODIFIED;
3377 
3378 			error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3379 			    (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3380 			ASSERT0(error);
3381 
3382 			error = zfs_link_destroy(sdzp, snm, szp, tx, ZRENAMING,
3383 			    NULL);
3384 			if (error == 0) {
3385 				zfs_log_rename(zilog, tx, TX_RENAME, sdzp,
3386 				    snm, tdzp, tnm, szp);
3387 			} else {
3388 				/*
3389 				 * At this point, we have successfully created
3390 				 * the target name, but have failed to remove
3391 				 * the source name.  Since the create was done
3392 				 * with the ZRENAMING flag, there are
3393 				 * complications; for one, the link count is
3394 				 * wrong.  The easiest way to deal with this
3395 				 * is to remove the newly created target, and
3396 				 * return the original error.  This must
3397 				 * succeed; fortunately, it is very unlikely to
3398 				 * fail, since we just created it.
3399 				 */
3400 				VERIFY0(zfs_link_destroy(tdzp, tnm, szp, tx,
3401 				    ZRENAMING, NULL));
3402 			}
3403 		}
3404 		if (error == 0) {
3405 			cache_vop_rename(sdvp, *svpp, tdvp, *tvpp, scnp, tcnp);
3406 		}
3407 	}
3408 
3409 	dmu_tx_commit(tx);
3410 
3411 out_seq:
3412 	vn_seqc_write_end(*svpp);
3413 	vn_seqc_write_end(sdvp);
3414 	if (*tvpp != NULL)
3415 		vn_seqc_write_end(*tvpp);
3416 	if (tdvp != *tvpp)
3417 		vn_seqc_write_end(tdvp);
3418 
3419 out:
3420 	if (error == 0 && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3421 		zil_commit(zilog, 0);
3422 	zfs_exit(zfsvfs, FTAG);
3423 
3424 	return (error);
3425 }
3426 
3427 int
zfs_rename(znode_t * sdzp,const char * sname,znode_t * tdzp,const char * tname,cred_t * cr,int flags,uint64_t rflags,vattr_t * wo_vap,zidmap_t * mnt_ns)3428 zfs_rename(znode_t *sdzp, const char *sname, znode_t *tdzp, const char *tname,
3429     cred_t *cr, int flags, uint64_t rflags, vattr_t *wo_vap, zidmap_t *mnt_ns)
3430 {
3431 	struct componentname scn, tcn;
3432 	vnode_t *sdvp, *tdvp;
3433 	vnode_t *svp, *tvp;
3434 	int error;
3435 	svp = tvp = NULL;
3436 
3437 	if (rflags != 0 || wo_vap != NULL)
3438 		return (SET_ERROR(EINVAL));
3439 
3440 	sdvp = ZTOV(sdzp);
3441 	tdvp = ZTOV(tdzp);
3442 	error = zfs_lookup_internal(sdzp, sname, &svp, &scn, DELETE);
3443 	if (sdzp->z_zfsvfs->z_replay == B_FALSE)
3444 		VOP_UNLOCK1(sdvp);
3445 	if (error != 0)
3446 		goto fail;
3447 	VOP_UNLOCK1(svp);
3448 
3449 	vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
3450 	error = zfs_lookup_internal(tdzp, tname, &tvp, &tcn, RENAME);
3451 	if (error == EJUSTRETURN)
3452 		tvp = NULL;
3453 	else if (error != 0) {
3454 		VOP_UNLOCK1(tdvp);
3455 		goto fail;
3456 	}
3457 
3458 	error = zfs_do_rename(sdvp, &svp, &scn, tdvp, &tvp, &tcn, cr);
3459 fail:
3460 	if (svp != NULL)
3461 		vrele(svp);
3462 	if (tvp != NULL)
3463 		vrele(tvp);
3464 
3465 	return (error);
3466 }
3467 
3468 /*
3469  * Insert the indicated symbolic reference entry into the directory.
3470  *
3471  *	IN:	dvp	- Directory to contain new symbolic link.
3472  *		link	- Name for new symlink entry.
3473  *		vap	- Attributes of new entry.
3474  *		cr	- credentials of caller.
3475  *		ct	- caller context
3476  *		flags	- case flags
3477  *		mnt_ns	- Unused on FreeBSD
3478  *
3479  *	RETURN:	0 on success, error code on failure.
3480  *
3481  * Timestamps:
3482  *	dvp - ctime|mtime updated
3483  */
3484 int
zfs_symlink(znode_t * dzp,const char * name,vattr_t * vap,const char * link,znode_t ** zpp,cred_t * cr,int flags,zidmap_t * mnt_ns)3485 zfs_symlink(znode_t *dzp, const char *name, vattr_t *vap,
3486     const char *link, znode_t **zpp, cred_t *cr, int flags, zidmap_t *mnt_ns)
3487 {
3488 	(void) flags;
3489 	znode_t		*zp;
3490 	dmu_tx_t	*tx;
3491 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
3492 	zilog_t		*zilog;
3493 	uint64_t	len = strlen(link);
3494 	int		error;
3495 	zfs_acl_ids_t	acl_ids;
3496 	boolean_t	fuid_dirtied;
3497 	uint64_t	txtype = TX_SYMLINK;
3498 
3499 	ASSERT3S(vap->va_type, ==, VLNK);
3500 
3501 	if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
3502 		return (error);
3503 	zilog = zfsvfs->z_log;
3504 
3505 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3506 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3507 		zfs_exit(zfsvfs, FTAG);
3508 		return (SET_ERROR(EILSEQ));
3509 	}
3510 
3511 	if (len > MAXPATHLEN) {
3512 		zfs_exit(zfsvfs, FTAG);
3513 		return (SET_ERROR(ENAMETOOLONG));
3514 	}
3515 
3516 	if ((error = zfs_acl_ids_create(dzp, 0,
3517 	    vap, cr, NULL, &acl_ids, NULL)) != 0) {
3518 		zfs_exit(zfsvfs, FTAG);
3519 		return (error);
3520 	}
3521 
3522 	/*
3523 	 * Attempt to lock directory; fail if entry already exists.
3524 	 */
3525 	error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
3526 	if (error) {
3527 		zfs_acl_ids_free(&acl_ids);
3528 		zfs_exit(zfsvfs, FTAG);
3529 		return (error);
3530 	}
3531 
3532 	if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr, mnt_ns))) {
3533 		zfs_acl_ids_free(&acl_ids);
3534 		zfs_exit(zfsvfs, FTAG);
3535 		return (error);
3536 	}
3537 
3538 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids,
3539 	    0 /* projid */)) {
3540 		zfs_acl_ids_free(&acl_ids);
3541 		zfs_exit(zfsvfs, FTAG);
3542 		return (SET_ERROR(EDQUOT));
3543 	}
3544 
3545 	getnewvnode_reserve_();
3546 	tx = dmu_tx_create(zfsvfs->z_os);
3547 	fuid_dirtied = zfsvfs->z_fuid_dirty;
3548 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3549 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3550 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3551 	    ZFS_SA_BASE_ATTR_SIZE + len);
3552 	dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3553 	if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3554 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3555 		    acl_ids.z_aclp->z_acl_bytes);
3556 	}
3557 	if (fuid_dirtied)
3558 		zfs_fuid_txhold(zfsvfs, tx);
3559 	error = dmu_tx_assign(tx, TXG_WAIT);
3560 	if (error) {
3561 		zfs_acl_ids_free(&acl_ids);
3562 		dmu_tx_abort(tx);
3563 		getnewvnode_drop_reserve();
3564 		zfs_exit(zfsvfs, FTAG);
3565 		return (error);
3566 	}
3567 
3568 	/*
3569 	 * Create a new object for the symlink.
3570 	 * for version 4 ZPL datasets the symlink will be an SA attribute
3571 	 */
3572 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3573 
3574 	if (fuid_dirtied)
3575 		zfs_fuid_sync(zfsvfs, tx);
3576 
3577 	if (zp->z_is_sa)
3578 		error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
3579 		    __DECONST(void *, link), len, tx);
3580 	else
3581 		zfs_sa_symlink(zp, __DECONST(char *, link), len, tx);
3582 
3583 	zp->z_size = len;
3584 	(void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
3585 	    &zp->z_size, sizeof (zp->z_size), tx);
3586 	/*
3587 	 * Insert the new object into the directory.
3588 	 */
3589 	(void) zfs_link_create(dzp, name, zp, tx, ZNEW);
3590 
3591 	zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3592 	*zpp = zp;
3593 
3594 	zfs_acl_ids_free(&acl_ids);
3595 
3596 	dmu_tx_commit(tx);
3597 
3598 	getnewvnode_drop_reserve();
3599 
3600 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3601 		zil_commit(zilog, 0);
3602 
3603 	zfs_exit(zfsvfs, FTAG);
3604 	return (error);
3605 }
3606 
3607 /*
3608  * Return, in the buffer contained in the provided uio structure,
3609  * the symbolic path referred to by vp.
3610  *
3611  *	IN:	vp	- vnode of symbolic link.
3612  *		uio	- structure to contain the link path.
3613  *		cr	- credentials of caller.
3614  *		ct	- caller context
3615  *
3616  *	OUT:	uio	- structure containing the link path.
3617  *
3618  *	RETURN:	0 on success, error code on failure.
3619  *
3620  * Timestamps:
3621  *	vp - atime updated
3622  */
3623 static int
zfs_readlink(vnode_t * vp,zfs_uio_t * uio,cred_t * cr,caller_context_t * ct)3624 zfs_readlink(vnode_t *vp, zfs_uio_t *uio, cred_t *cr, caller_context_t *ct)
3625 {
3626 	(void) cr, (void) ct;
3627 	znode_t		*zp = VTOZ(vp);
3628 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3629 	int		error;
3630 
3631 	if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3632 		return (error);
3633 
3634 	if (zp->z_is_sa)
3635 		error = sa_lookup_uio(zp->z_sa_hdl,
3636 		    SA_ZPL_SYMLINK(zfsvfs), uio);
3637 	else
3638 		error = zfs_sa_readlink(zp, uio);
3639 
3640 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3641 
3642 	zfs_exit(zfsvfs, FTAG);
3643 	return (error);
3644 }
3645 
3646 /*
3647  * Insert a new entry into directory tdvp referencing svp.
3648  *
3649  *	IN:	tdvp	- Directory to contain new entry.
3650  *		svp	- vnode of new entry.
3651  *		name	- name of new entry.
3652  *		cr	- credentials of caller.
3653  *
3654  *	RETURN:	0 on success, error code on failure.
3655  *
3656  * Timestamps:
3657  *	tdvp - ctime|mtime updated
3658  *	 svp - ctime updated
3659  */
3660 int
zfs_link(znode_t * tdzp,znode_t * szp,const char * name,cred_t * cr,int flags)3661 zfs_link(znode_t *tdzp, znode_t *szp, const char *name, cred_t *cr,
3662     int flags)
3663 {
3664 	(void) flags;
3665 	znode_t		*tzp;
3666 	zfsvfs_t	*zfsvfs = tdzp->z_zfsvfs;
3667 	zilog_t		*zilog;
3668 	dmu_tx_t	*tx;
3669 	int		error;
3670 	uint64_t	parent;
3671 	uid_t		owner;
3672 
3673 	ASSERT3S(ZTOV(tdzp)->v_type, ==, VDIR);
3674 
3675 	if ((error = zfs_enter_verify_zp(zfsvfs, tdzp, FTAG)) != 0)
3676 		return (error);
3677 	zilog = zfsvfs->z_log;
3678 
3679 	/*
3680 	 * POSIX dictates that we return EPERM here.
3681 	 * Better choices include ENOTSUP or EISDIR.
3682 	 */
3683 	if (ZTOV(szp)->v_type == VDIR) {
3684 		zfs_exit(zfsvfs, FTAG);
3685 		return (SET_ERROR(EPERM));
3686 	}
3687 
3688 	if ((error = zfs_verify_zp(szp)) != 0) {
3689 		zfs_exit(zfsvfs, FTAG);
3690 		return (error);
3691 	}
3692 
3693 	/*
3694 	 * If we are using project inheritance, means if the directory has
3695 	 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3696 	 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3697 	 * such case, we only allow hard link creation in our tree when the
3698 	 * project IDs are the same.
3699 	 */
3700 	if (tdzp->z_pflags & ZFS_PROJINHERIT &&
3701 	    tdzp->z_projid != szp->z_projid) {
3702 		zfs_exit(zfsvfs, FTAG);
3703 		return (SET_ERROR(EXDEV));
3704 	}
3705 
3706 	if (szp->z_pflags & (ZFS_APPENDONLY |
3707 	    ZFS_IMMUTABLE | ZFS_READONLY)) {
3708 		zfs_exit(zfsvfs, FTAG);
3709 		return (SET_ERROR(EPERM));
3710 	}
3711 
3712 	/* Prevent links to .zfs/shares files */
3713 
3714 	if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
3715 	    &parent, sizeof (uint64_t))) != 0) {
3716 		zfs_exit(zfsvfs, FTAG);
3717 		return (error);
3718 	}
3719 	if (parent == zfsvfs->z_shares_dir) {
3720 		zfs_exit(zfsvfs, FTAG);
3721 		return (SET_ERROR(EPERM));
3722 	}
3723 
3724 	if (zfsvfs->z_utf8 && u8_validate(name,
3725 	    strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3726 		zfs_exit(zfsvfs, FTAG);
3727 		return (SET_ERROR(EILSEQ));
3728 	}
3729 
3730 	/*
3731 	 * We do not support links between attributes and non-attributes
3732 	 * because of the potential security risk of creating links
3733 	 * into "normal" file space in order to circumvent restrictions
3734 	 * imposed in attribute space.
3735 	 */
3736 	if ((szp->z_pflags & ZFS_XATTR) != (tdzp->z_pflags & ZFS_XATTR)) {
3737 		zfs_exit(zfsvfs, FTAG);
3738 		return (SET_ERROR(EINVAL));
3739 	}
3740 
3741 
3742 	owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
3743 	if (owner != crgetuid(cr) && secpolicy_basic_link(ZTOV(szp), cr) != 0) {
3744 		zfs_exit(zfsvfs, FTAG);
3745 		return (SET_ERROR(EPERM));
3746 	}
3747 
3748 	if ((error = zfs_zaccess(tdzp, ACE_ADD_FILE, 0, B_FALSE, cr, NULL))) {
3749 		zfs_exit(zfsvfs, FTAG);
3750 		return (error);
3751 	}
3752 
3753 	/*
3754 	 * Attempt to lock directory; fail if entry already exists.
3755 	 */
3756 	error = zfs_dirent_lookup(tdzp, name, &tzp, ZNEW);
3757 	if (error) {
3758 		zfs_exit(zfsvfs, FTAG);
3759 		return (error);
3760 	}
3761 
3762 	tx = dmu_tx_create(zfsvfs->z_os);
3763 	dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3764 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, name);
3765 	zfs_sa_upgrade_txholds(tx, szp);
3766 	zfs_sa_upgrade_txholds(tx, tdzp);
3767 	error = dmu_tx_assign(tx, TXG_WAIT);
3768 	if (error) {
3769 		dmu_tx_abort(tx);
3770 		zfs_exit(zfsvfs, FTAG);
3771 		return (error);
3772 	}
3773 
3774 	error = zfs_link_create(tdzp, name, szp, tx, 0);
3775 
3776 	if (error == 0) {
3777 		uint64_t txtype = TX_LINK;
3778 		zfs_log_link(zilog, tx, txtype, tdzp, szp, name);
3779 	}
3780 
3781 	dmu_tx_commit(tx);
3782 
3783 	if (error == 0) {
3784 		vnevent_link(ZTOV(szp), ct);
3785 	}
3786 
3787 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3788 		zil_commit(zilog, 0);
3789 
3790 	zfs_exit(zfsvfs, FTAG);
3791 	return (error);
3792 }
3793 
3794 /*
3795  * Free or allocate space in a file.  Currently, this function only
3796  * supports the `F_FREESP' command.  However, this command is somewhat
3797  * misnamed, as its functionality includes the ability to allocate as
3798  * well as free space.
3799  *
3800  *	IN:	ip	- inode of file to free data in.
3801  *		cmd	- action to take (only F_FREESP supported).
3802  *		bfp	- section of file to free/alloc.
3803  *		flag	- current file open mode flags.
3804  *		offset	- current file offset.
3805  *		cr	- credentials of caller.
3806  *
3807  *	RETURN:	0 on success, error code on failure.
3808  *
3809  * Timestamps:
3810  *	ip - ctime|mtime updated
3811  */
3812 int
zfs_space(znode_t * zp,int cmd,flock64_t * bfp,int flag,offset_t offset,cred_t * cr)3813 zfs_space(znode_t *zp, int cmd, flock64_t *bfp, int flag,
3814     offset_t offset, cred_t *cr)
3815 {
3816 	(void) offset;
3817 	zfsvfs_t	*zfsvfs = ZTOZSB(zp);
3818 	uint64_t	off, len;
3819 	int		error;
3820 
3821 	if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3822 		return (error);
3823 
3824 	if (cmd != F_FREESP) {
3825 		zfs_exit(zfsvfs, FTAG);
3826 		return (SET_ERROR(EINVAL));
3827 	}
3828 
3829 	/*
3830 	 * Callers might not be able to detect properly that we are read-only,
3831 	 * so check it explicitly here.
3832 	 */
3833 	if (zfs_is_readonly(zfsvfs)) {
3834 		zfs_exit(zfsvfs, FTAG);
3835 		return (SET_ERROR(EROFS));
3836 	}
3837 
3838 	if (bfp->l_len < 0) {
3839 		zfs_exit(zfsvfs, FTAG);
3840 		return (SET_ERROR(EINVAL));
3841 	}
3842 
3843 	/*
3844 	 * Permissions aren't checked on Solaris because on this OS
3845 	 * zfs_space() can only be called with an opened file handle.
3846 	 * On Linux we can get here through truncate_range() which
3847 	 * operates directly on inodes, so we need to check access rights.
3848 	 */
3849 	if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr, NULL))) {
3850 		zfs_exit(zfsvfs, FTAG);
3851 		return (error);
3852 	}
3853 
3854 	off = bfp->l_start;
3855 	len = bfp->l_len; /* 0 means from off to end of file */
3856 
3857 	error = zfs_freesp(zp, off, len, flag, TRUE);
3858 
3859 	zfs_exit(zfsvfs, FTAG);
3860 	return (error);
3861 }
3862 
3863 static void
zfs_inactive(vnode_t * vp,cred_t * cr,caller_context_t * ct)3864 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
3865 {
3866 	(void) cr, (void) ct;
3867 	znode_t	*zp = VTOZ(vp);
3868 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3869 	int error;
3870 
3871 	ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs);
3872 	if (zp->z_sa_hdl == NULL) {
3873 		/*
3874 		 * The fs has been unmounted, or we did a
3875 		 * suspend/resume and this file no longer exists.
3876 		 */
3877 		ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
3878 		vrecycle(vp);
3879 		return;
3880 	}
3881 
3882 	if (zp->z_unlinked) {
3883 		/*
3884 		 * Fast path to recycle a vnode of a removed file.
3885 		 */
3886 		ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
3887 		vrecycle(vp);
3888 		return;
3889 	}
3890 
3891 	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3892 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3893 
3894 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3895 		zfs_sa_upgrade_txholds(tx, zp);
3896 		error = dmu_tx_assign(tx, TXG_WAIT);
3897 		if (error) {
3898 			dmu_tx_abort(tx);
3899 		} else {
3900 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
3901 			    (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
3902 			zp->z_atime_dirty = 0;
3903 			dmu_tx_commit(tx);
3904 		}
3905 	}
3906 	ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
3907 }
3908 
3909 
3910 _Static_assert(sizeof (struct zfid_short) <= sizeof (struct fid),
3911 	"struct zfid_short bigger than struct fid");
3912 _Static_assert(sizeof (struct zfid_long) <= sizeof (struct fid),
3913 	"struct zfid_long bigger than struct fid");
3914 
3915 static int
zfs_fid(vnode_t * vp,fid_t * fidp,caller_context_t * ct)3916 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
3917 {
3918 	(void) ct;
3919 	znode_t		*zp = VTOZ(vp);
3920 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3921 	uint32_t	gen;
3922 	uint64_t	gen64;
3923 	uint64_t	object = zp->z_id;
3924 	zfid_short_t	*zfid;
3925 	int		size, i, error;
3926 
3927 	if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3928 		return (error);
3929 
3930 	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
3931 	    &gen64, sizeof (uint64_t))) != 0) {
3932 		zfs_exit(zfsvfs, FTAG);
3933 		return (error);
3934 	}
3935 
3936 	gen = (uint32_t)gen64;
3937 
3938 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
3939 	fidp->fid_len = size;
3940 
3941 	zfid = (zfid_short_t *)fidp;
3942 
3943 	zfid->zf_len = size;
3944 
3945 	for (i = 0; i < sizeof (zfid->zf_object); i++)
3946 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
3947 
3948 	/* Must have a non-zero generation number to distinguish from .zfs */
3949 	if (gen == 0)
3950 		gen = 1;
3951 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
3952 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
3953 
3954 	if (size == LONG_FID_LEN) {
3955 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
3956 		zfid_long_t	*zlfid;
3957 
3958 		zlfid = (zfid_long_t *)fidp;
3959 
3960 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
3961 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
3962 
3963 		/* XXX - this should be the generation number for the objset */
3964 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
3965 			zlfid->zf_setgen[i] = 0;
3966 	}
3967 
3968 	zfs_exit(zfsvfs, FTAG);
3969 	return (0);
3970 }
3971 
3972 static int
zfs_pathconf(vnode_t * vp,int cmd,ulong_t * valp,cred_t * cr,caller_context_t * ct)3973 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
3974     caller_context_t *ct)
3975 {
3976 	znode_t *zp;
3977 	zfsvfs_t *zfsvfs;
3978 	int error;
3979 
3980 	switch (cmd) {
3981 	case _PC_LINK_MAX:
3982 		*valp = MIN(LONG_MAX, ZFS_LINK_MAX);
3983 		return (0);
3984 
3985 	case _PC_FILESIZEBITS:
3986 		*valp = 64;
3987 		return (0);
3988 	case _PC_MIN_HOLE_SIZE:
3989 		*valp = (int)SPA_MINBLOCKSIZE;
3990 		return (0);
3991 	case _PC_ACL_EXTENDED:
3992 #if 0		/* POSIX ACLs are not implemented for ZFS on FreeBSD yet. */
3993 		zp = VTOZ(vp);
3994 		zfsvfs = zp->z_zfsvfs;
3995 		if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3996 			return (error);
3997 		*valp = zfsvfs->z_acl_type == ZFSACLTYPE_POSIX ? 1 : 0;
3998 		zfs_exit(zfsvfs, FTAG);
3999 #else
4000 		*valp = 0;
4001 #endif
4002 		return (0);
4003 
4004 	case _PC_ACL_NFS4:
4005 		zp = VTOZ(vp);
4006 		zfsvfs = zp->z_zfsvfs;
4007 		if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
4008 			return (error);
4009 		*valp = zfsvfs->z_acl_type == ZFS_ACLTYPE_NFSV4 ? 1 : 0;
4010 		zfs_exit(zfsvfs, FTAG);
4011 		return (0);
4012 
4013 	case _PC_ACL_PATH_MAX:
4014 		*valp = ACL_MAX_ENTRIES;
4015 		return (0);
4016 
4017 	default:
4018 		return (EOPNOTSUPP);
4019 	}
4020 }
4021 
4022 static int
zfs_getpages(struct vnode * vp,vm_page_t * ma,int count,int * rbehind,int * rahead)4023 zfs_getpages(struct vnode *vp, vm_page_t *ma, int count, int *rbehind,
4024     int *rahead)
4025 {
4026 	znode_t *zp = VTOZ(vp);
4027 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4028 	zfs_locked_range_t *lr;
4029 	vm_object_t object;
4030 	off_t start, end, obj_size;
4031 	uint_t blksz;
4032 	int pgsin_b, pgsin_a;
4033 	int error;
4034 
4035 	if (zfs_enter_verify_zp(zfsvfs, zp, FTAG) != 0)
4036 		return (zfs_vm_pagerret_error);
4037 
4038 	start = IDX_TO_OFF(ma[0]->pindex);
4039 	end = IDX_TO_OFF(ma[count - 1]->pindex + 1);
4040 
4041 	/*
4042 	 * Lock a range covering all required and optional pages.
4043 	 * Note that we need to handle the case of the block size growing.
4044 	 */
4045 	for (;;) {
4046 		blksz = zp->z_blksz;
4047 		lr = zfs_rangelock_tryenter(&zp->z_rangelock,
4048 		    rounddown(start, blksz),
4049 		    roundup(end, blksz) - rounddown(start, blksz), RL_READER);
4050 		if (lr == NULL) {
4051 			if (rahead != NULL) {
4052 				*rahead = 0;
4053 				rahead = NULL;
4054 			}
4055 			if (rbehind != NULL) {
4056 				*rbehind = 0;
4057 				rbehind = NULL;
4058 			}
4059 			break;
4060 		}
4061 		if (blksz == zp->z_blksz)
4062 			break;
4063 		zfs_rangelock_exit(lr);
4064 	}
4065 
4066 	object = ma[0]->object;
4067 	zfs_vmobject_wlock(object);
4068 	obj_size = object->un_pager.vnp.vnp_size;
4069 	zfs_vmobject_wunlock(object);
4070 	if (IDX_TO_OFF(ma[count - 1]->pindex) >= obj_size) {
4071 		if (lr != NULL)
4072 			zfs_rangelock_exit(lr);
4073 		zfs_exit(zfsvfs, FTAG);
4074 		return (zfs_vm_pagerret_bad);
4075 	}
4076 
4077 	pgsin_b = 0;
4078 	if (rbehind != NULL) {
4079 		pgsin_b = OFF_TO_IDX(start - rounddown(start, blksz));
4080 		pgsin_b = MIN(*rbehind, pgsin_b);
4081 	}
4082 
4083 	pgsin_a = 0;
4084 	if (rahead != NULL) {
4085 		pgsin_a = OFF_TO_IDX(roundup(end, blksz) - end);
4086 		if (end + IDX_TO_OFF(pgsin_a) >= obj_size)
4087 			pgsin_a = OFF_TO_IDX(round_page(obj_size) - end);
4088 		pgsin_a = MIN(*rahead, pgsin_a);
4089 	}
4090 
4091 	/*
4092 	 * NB: we need to pass the exact byte size of the data that we expect
4093 	 * to read after accounting for the file size.  This is required because
4094 	 * ZFS will panic if we request DMU to read beyond the end of the last
4095 	 * allocated block.
4096 	 */
4097 	error = dmu_read_pages(zfsvfs->z_os, zp->z_id, ma, count, &pgsin_b,
4098 	    &pgsin_a, MIN(end, obj_size) - (end - PAGE_SIZE));
4099 
4100 	if (lr != NULL)
4101 		zfs_rangelock_exit(lr);
4102 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4103 
4104 	dataset_kstats_update_read_kstats(&zfsvfs->z_kstat, count*PAGE_SIZE);
4105 
4106 	zfs_exit(zfsvfs, FTAG);
4107 
4108 	if (error != 0)
4109 		return (zfs_vm_pagerret_error);
4110 
4111 	VM_CNT_INC(v_vnodein);
4112 	VM_CNT_ADD(v_vnodepgsin, count + pgsin_b + pgsin_a);
4113 	if (rbehind != NULL)
4114 		*rbehind = pgsin_b;
4115 	if (rahead != NULL)
4116 		*rahead = pgsin_a;
4117 	return (zfs_vm_pagerret_ok);
4118 }
4119 
4120 #ifndef _SYS_SYSPROTO_H_
4121 struct vop_getpages_args {
4122 	struct vnode *a_vp;
4123 	vm_page_t *a_m;
4124 	int a_count;
4125 	int *a_rbehind;
4126 	int *a_rahead;
4127 };
4128 #endif
4129 
4130 static int
zfs_freebsd_getpages(struct vop_getpages_args * ap)4131 zfs_freebsd_getpages(struct vop_getpages_args *ap)
4132 {
4133 
4134 	return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind,
4135 	    ap->a_rahead));
4136 }
4137 
4138 static int
zfs_putpages(struct vnode * vp,vm_page_t * ma,size_t len,int flags,int * rtvals)4139 zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags,
4140     int *rtvals)
4141 {
4142 	znode_t		*zp = VTOZ(vp);
4143 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4144 	zfs_locked_range_t		*lr;
4145 	dmu_tx_t	*tx;
4146 	struct sf_buf	*sf;
4147 	vm_object_t	object;
4148 	vm_page_t	m;
4149 	caddr_t		va;
4150 	size_t		tocopy;
4151 	size_t		lo_len;
4152 	vm_ooffset_t	lo_off;
4153 	vm_ooffset_t	off;
4154 	uint_t		blksz;
4155 	int		ncount;
4156 	int		pcount;
4157 	int		err;
4158 	int		i;
4159 
4160 	object = vp->v_object;
4161 	KASSERT(ma[0]->object == object, ("mismatching object"));
4162 	KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length"));
4163 
4164 	pcount = btoc(len);
4165 	ncount = pcount;
4166 	for (i = 0; i < pcount; i++)
4167 		rtvals[i] = zfs_vm_pagerret_error;
4168 
4169 	if (zfs_enter_verify_zp(zfsvfs, zp, FTAG) != 0)
4170 		return (zfs_vm_pagerret_error);
4171 
4172 	off = IDX_TO_OFF(ma[0]->pindex);
4173 	blksz = zp->z_blksz;
4174 	lo_off = rounddown(off, blksz);
4175 	lo_len = roundup(len + (off - lo_off), blksz);
4176 	lr = zfs_rangelock_enter(&zp->z_rangelock, lo_off, lo_len, RL_WRITER);
4177 
4178 	zfs_vmobject_wlock(object);
4179 	if (len + off > object->un_pager.vnp.vnp_size) {
4180 		if (object->un_pager.vnp.vnp_size > off) {
4181 			int pgoff;
4182 
4183 			len = object->un_pager.vnp.vnp_size - off;
4184 			ncount = btoc(len);
4185 			if ((pgoff = (int)len & PAGE_MASK) != 0) {
4186 				/*
4187 				 * If the object is locked and the following
4188 				 * conditions hold, then the page's dirty
4189 				 * field cannot be concurrently changed by a
4190 				 * pmap operation.
4191 				 */
4192 				m = ma[ncount - 1];
4193 				vm_page_assert_sbusied(m);
4194 				KASSERT(!pmap_page_is_write_mapped(m),
4195 				    ("zfs_putpages: page %p is not read-only",
4196 				    m));
4197 				vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
4198 				    pgoff);
4199 			}
4200 		} else {
4201 			len = 0;
4202 			ncount = 0;
4203 		}
4204 		if (ncount < pcount) {
4205 			for (i = ncount; i < pcount; i++) {
4206 				rtvals[i] = zfs_vm_pagerret_bad;
4207 			}
4208 		}
4209 	}
4210 	zfs_vmobject_wunlock(object);
4211 
4212 	boolean_t commit = (flags & (zfs_vm_pagerput_sync |
4213 	    zfs_vm_pagerput_inval)) != 0 ||
4214 	    zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS;
4215 
4216 	if (ncount == 0)
4217 		goto out;
4218 
4219 	if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT, zp->z_uid) ||
4220 	    zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT, zp->z_gid) ||
4221 	    (zp->z_projid != ZFS_DEFAULT_PROJID &&
4222 	    zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT,
4223 	    zp->z_projid))) {
4224 		goto out;
4225 	}
4226 
4227 	tx = dmu_tx_create(zfsvfs->z_os);
4228 	dmu_tx_hold_write(tx, zp->z_id, off, len);
4229 
4230 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4231 	zfs_sa_upgrade_txholds(tx, zp);
4232 	err = dmu_tx_assign(tx, TXG_WAIT);
4233 	if (err != 0) {
4234 		dmu_tx_abort(tx);
4235 		goto out;
4236 	}
4237 
4238 	if (zp->z_blksz < PAGE_SIZE) {
4239 		for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) {
4240 			tocopy = len > PAGE_SIZE ? PAGE_SIZE : len;
4241 			va = zfs_map_page(ma[i], &sf);
4242 			dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx);
4243 			zfs_unmap_page(sf);
4244 		}
4245 	} else {
4246 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx);
4247 	}
4248 
4249 	if (err == 0) {
4250 		uint64_t mtime[2], ctime[2];
4251 		sa_bulk_attr_t bulk[3];
4252 		int count = 0;
4253 
4254 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4255 		    &mtime, 16);
4256 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4257 		    &ctime, 16);
4258 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4259 		    &zp->z_pflags, 8);
4260 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
4261 		err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
4262 		ASSERT0(err);
4263 		/*
4264 		 * XXX we should be passing a callback to undirty
4265 		 * but that would make the locking messier
4266 		 */
4267 		zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off,
4268 		    len, commit, NULL, NULL);
4269 
4270 		zfs_vmobject_wlock(object);
4271 		for (i = 0; i < ncount; i++) {
4272 			rtvals[i] = zfs_vm_pagerret_ok;
4273 			vm_page_undirty(ma[i]);
4274 		}
4275 		zfs_vmobject_wunlock(object);
4276 		VM_CNT_INC(v_vnodeout);
4277 		VM_CNT_ADD(v_vnodepgsout, ncount);
4278 	}
4279 	dmu_tx_commit(tx);
4280 
4281 out:
4282 	zfs_rangelock_exit(lr);
4283 	if (commit)
4284 		zil_commit(zfsvfs->z_log, zp->z_id);
4285 
4286 	dataset_kstats_update_write_kstats(&zfsvfs->z_kstat, len);
4287 
4288 	zfs_exit(zfsvfs, FTAG);
4289 	return (rtvals[0]);
4290 }
4291 
4292 #ifndef _SYS_SYSPROTO_H_
4293 struct vop_putpages_args {
4294 	struct vnode *a_vp;
4295 	vm_page_t *a_m;
4296 	int a_count;
4297 	int a_sync;
4298 	int *a_rtvals;
4299 };
4300 #endif
4301 
4302 static int
zfs_freebsd_putpages(struct vop_putpages_args * ap)4303 zfs_freebsd_putpages(struct vop_putpages_args *ap)
4304 {
4305 
4306 	return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync,
4307 	    ap->a_rtvals));
4308 }
4309 
4310 #ifndef _SYS_SYSPROTO_H_
4311 struct vop_bmap_args {
4312 	struct vnode *a_vp;
4313 	daddr_t  a_bn;
4314 	struct bufobj **a_bop;
4315 	daddr_t *a_bnp;
4316 	int *a_runp;
4317 	int *a_runb;
4318 };
4319 #endif
4320 
4321 static int
zfs_freebsd_bmap(struct vop_bmap_args * ap)4322 zfs_freebsd_bmap(struct vop_bmap_args *ap)
4323 {
4324 
4325 	if (ap->a_bop != NULL)
4326 		*ap->a_bop = &ap->a_vp->v_bufobj;
4327 	if (ap->a_bnp != NULL)
4328 		*ap->a_bnp = ap->a_bn;
4329 	if (ap->a_runp != NULL)
4330 		*ap->a_runp = 0;
4331 	if (ap->a_runb != NULL)
4332 		*ap->a_runb = 0;
4333 
4334 	return (0);
4335 }
4336 
4337 #ifndef _SYS_SYSPROTO_H_
4338 struct vop_open_args {
4339 	struct vnode *a_vp;
4340 	int a_mode;
4341 	struct ucred *a_cred;
4342 	struct thread *a_td;
4343 };
4344 #endif
4345 
4346 static int
zfs_freebsd_open(struct vop_open_args * ap)4347 zfs_freebsd_open(struct vop_open_args *ap)
4348 {
4349 	vnode_t	*vp = ap->a_vp;
4350 	znode_t *zp = VTOZ(vp);
4351 	int error;
4352 
4353 	error = zfs_open(&vp, ap->a_mode, ap->a_cred);
4354 	if (error == 0)
4355 		vnode_create_vobject(vp, zp->z_size, ap->a_td);
4356 	return (error);
4357 }
4358 
4359 #ifndef _SYS_SYSPROTO_H_
4360 struct vop_close_args {
4361 	struct vnode *a_vp;
4362 	int  a_fflag;
4363 	struct ucred *a_cred;
4364 	struct thread *a_td;
4365 };
4366 #endif
4367 
4368 static int
zfs_freebsd_close(struct vop_close_args * ap)4369 zfs_freebsd_close(struct vop_close_args *ap)
4370 {
4371 
4372 	return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred));
4373 }
4374 
4375 #ifndef _SYS_SYSPROTO_H_
4376 struct vop_ioctl_args {
4377 	struct vnode *a_vp;
4378 	ulong_t a_command;
4379 	caddr_t a_data;
4380 	int a_fflag;
4381 	struct ucred *cred;
4382 	struct thread *td;
4383 };
4384 #endif
4385 
4386 static int
zfs_freebsd_ioctl(struct vop_ioctl_args * ap)4387 zfs_freebsd_ioctl(struct vop_ioctl_args *ap)
4388 {
4389 
4390 	return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
4391 	    ap->a_fflag, ap->a_cred, NULL));
4392 }
4393 
4394 static int
ioflags(int ioflags)4395 ioflags(int ioflags)
4396 {
4397 	int flags = 0;
4398 
4399 	if (ioflags & IO_APPEND)
4400 		flags |= O_APPEND;
4401 	if (ioflags & IO_NDELAY)
4402 		flags |= O_NONBLOCK;
4403 	if (ioflags & IO_SYNC)
4404 		flags |= O_SYNC;
4405 
4406 	return (flags);
4407 }
4408 
4409 #ifndef _SYS_SYSPROTO_H_
4410 struct vop_read_args {
4411 	struct vnode *a_vp;
4412 	struct uio *a_uio;
4413 	int a_ioflag;
4414 	struct ucred *a_cred;
4415 };
4416 #endif
4417 
4418 static int
zfs_freebsd_read(struct vop_read_args * ap)4419 zfs_freebsd_read(struct vop_read_args *ap)
4420 {
4421 	zfs_uio_t uio;
4422 	zfs_uio_init(&uio, ap->a_uio);
4423 	return (zfs_read(VTOZ(ap->a_vp), &uio, ioflags(ap->a_ioflag),
4424 	    ap->a_cred));
4425 }
4426 
4427 #ifndef _SYS_SYSPROTO_H_
4428 struct vop_write_args {
4429 	struct vnode *a_vp;
4430 	struct uio *a_uio;
4431 	int a_ioflag;
4432 	struct ucred *a_cred;
4433 };
4434 #endif
4435 
4436 static int
zfs_freebsd_write(struct vop_write_args * ap)4437 zfs_freebsd_write(struct vop_write_args *ap)
4438 {
4439 	zfs_uio_t uio;
4440 	zfs_uio_init(&uio, ap->a_uio);
4441 	return (zfs_write(VTOZ(ap->a_vp), &uio, ioflags(ap->a_ioflag),
4442 	    ap->a_cred));
4443 }
4444 
4445 #if __FreeBSD_version >= 1300102
4446 /*
4447  * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
4448  * the comment above cache_fplookup for details.
4449  */
4450 static int
zfs_freebsd_fplookup_vexec(struct vop_fplookup_vexec_args * v)4451 zfs_freebsd_fplookup_vexec(struct vop_fplookup_vexec_args *v)
4452 {
4453 	vnode_t *vp;
4454 	znode_t *zp;
4455 	uint64_t pflags;
4456 
4457 	vp = v->a_vp;
4458 	zp = VTOZ_SMR(vp);
4459 	if (__predict_false(zp == NULL))
4460 		return (EAGAIN);
4461 	pflags = atomic_load_64(&zp->z_pflags);
4462 	if (pflags & ZFS_AV_QUARANTINED)
4463 		return (EAGAIN);
4464 	if (pflags & ZFS_XATTR)
4465 		return (EAGAIN);
4466 	if ((pflags & ZFS_NO_EXECS_DENIED) == 0)
4467 		return (EAGAIN);
4468 	return (0);
4469 }
4470 #endif
4471 
4472 #if __FreeBSD_version >= 1300139
4473 static int
zfs_freebsd_fplookup_symlink(struct vop_fplookup_symlink_args * v)4474 zfs_freebsd_fplookup_symlink(struct vop_fplookup_symlink_args *v)
4475 {
4476 	vnode_t *vp;
4477 	znode_t *zp;
4478 	char *target;
4479 
4480 	vp = v->a_vp;
4481 	zp = VTOZ_SMR(vp);
4482 	if (__predict_false(zp == NULL)) {
4483 		return (EAGAIN);
4484 	}
4485 
4486 	target = atomic_load_consume_ptr(&zp->z_cached_symlink);
4487 	if (target == NULL) {
4488 		return (EAGAIN);
4489 	}
4490 	return (cache_symlink_resolve(v->a_fpl, target, strlen(target)));
4491 }
4492 #endif
4493 
4494 #ifndef _SYS_SYSPROTO_H_
4495 struct vop_access_args {
4496 	struct vnode *a_vp;
4497 	accmode_t a_accmode;
4498 	struct ucred *a_cred;
4499 	struct thread *a_td;
4500 };
4501 #endif
4502 
4503 static int
zfs_freebsd_access(struct vop_access_args * ap)4504 zfs_freebsd_access(struct vop_access_args *ap)
4505 {
4506 	vnode_t *vp = ap->a_vp;
4507 	znode_t *zp = VTOZ(vp);
4508 	accmode_t accmode;
4509 	int error = 0;
4510 
4511 
4512 	if (ap->a_accmode == VEXEC) {
4513 		if (zfs_fastaccesschk_execute(zp, ap->a_cred) == 0)
4514 			return (0);
4515 	}
4516 
4517 	/*
4518 	 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
4519 	 */
4520 	accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
4521 	if (accmode != 0)
4522 		error = zfs_access(zp, accmode, 0, ap->a_cred);
4523 
4524 	/*
4525 	 * VADMIN has to be handled by vaccess().
4526 	 */
4527 	if (error == 0) {
4528 		accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
4529 		if (accmode != 0) {
4530 #if __FreeBSD_version >= 1300105
4531 			error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
4532 			    zp->z_gid, accmode, ap->a_cred);
4533 #else
4534 			error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
4535 			    zp->z_gid, accmode, ap->a_cred, NULL);
4536 #endif
4537 		}
4538 	}
4539 
4540 	/*
4541 	 * For VEXEC, ensure that at least one execute bit is set for
4542 	 * non-directories.
4543 	 */
4544 	if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR &&
4545 	    (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
4546 		error = EACCES;
4547 	}
4548 
4549 	return (error);
4550 }
4551 
4552 #ifndef _SYS_SYSPROTO_H_
4553 struct vop_lookup_args {
4554 	struct vnode *a_dvp;
4555 	struct vnode **a_vpp;
4556 	struct componentname *a_cnp;
4557 };
4558 #endif
4559 
4560 static int
zfs_freebsd_lookup(struct vop_lookup_args * ap,boolean_t cached)4561 zfs_freebsd_lookup(struct vop_lookup_args *ap, boolean_t cached)
4562 {
4563 	struct componentname *cnp = ap->a_cnp;
4564 	char nm[NAME_MAX + 1];
4565 
4566 	ASSERT3U(cnp->cn_namelen, <, sizeof (nm));
4567 	strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof (nm)));
4568 
4569 	return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
4570 	    cnp->cn_cred, 0, cached));
4571 }
4572 
4573 static int
zfs_freebsd_cachedlookup(struct vop_cachedlookup_args * ap)4574 zfs_freebsd_cachedlookup(struct vop_cachedlookup_args *ap)
4575 {
4576 
4577 	return (zfs_freebsd_lookup((struct vop_lookup_args *)ap, B_TRUE));
4578 }
4579 
4580 #ifndef _SYS_SYSPROTO_H_
4581 struct vop_lookup_args {
4582 	struct vnode *a_dvp;
4583 	struct vnode **a_vpp;
4584 	struct componentname *a_cnp;
4585 };
4586 #endif
4587 
4588 static int
zfs_cache_lookup(struct vop_lookup_args * ap)4589 zfs_cache_lookup(struct vop_lookup_args *ap)
4590 {
4591 	zfsvfs_t *zfsvfs;
4592 
4593 	zfsvfs = ap->a_dvp->v_mount->mnt_data;
4594 	if (zfsvfs->z_use_namecache)
4595 		return (vfs_cache_lookup(ap));
4596 	else
4597 		return (zfs_freebsd_lookup(ap, B_FALSE));
4598 }
4599 
4600 #ifndef _SYS_SYSPROTO_H_
4601 struct vop_create_args {
4602 	struct vnode *a_dvp;
4603 	struct vnode **a_vpp;
4604 	struct componentname *a_cnp;
4605 	struct vattr *a_vap;
4606 };
4607 #endif
4608 
4609 static int
zfs_freebsd_create(struct vop_create_args * ap)4610 zfs_freebsd_create(struct vop_create_args *ap)
4611 {
4612 	zfsvfs_t *zfsvfs;
4613 	struct componentname *cnp = ap->a_cnp;
4614 	vattr_t *vap = ap->a_vap;
4615 	znode_t *zp = NULL;
4616 	int rc, mode;
4617 
4618 #if __FreeBSD_version < 1400068
4619 	ASSERT(cnp->cn_flags & SAVENAME);
4620 #endif
4621 
4622 	vattr_init_mask(vap);
4623 	mode = vap->va_mode & ALLPERMS;
4624 	zfsvfs = ap->a_dvp->v_mount->mnt_data;
4625 	*ap->a_vpp = NULL;
4626 
4627 	rc = zfs_create(VTOZ(ap->a_dvp), cnp->cn_nameptr, vap, 0, mode,
4628 	    &zp, cnp->cn_cred, 0 /* flag */, NULL /* vsecattr */, NULL);
4629 	if (rc == 0)
4630 		*ap->a_vpp = ZTOV(zp);
4631 	if (zfsvfs->z_use_namecache &&
4632 	    rc == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
4633 		cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
4634 
4635 	return (rc);
4636 }
4637 
4638 #ifndef _SYS_SYSPROTO_H_
4639 struct vop_remove_args {
4640 	struct vnode *a_dvp;
4641 	struct vnode *a_vp;
4642 	struct componentname *a_cnp;
4643 };
4644 #endif
4645 
4646 static int
zfs_freebsd_remove(struct vop_remove_args * ap)4647 zfs_freebsd_remove(struct vop_remove_args *ap)
4648 {
4649 
4650 #if __FreeBSD_version < 1400068
4651 	ASSERT(ap->a_cnp->cn_flags & SAVENAME);
4652 #endif
4653 
4654 	return (zfs_remove_(ap->a_dvp, ap->a_vp, ap->a_cnp->cn_nameptr,
4655 	    ap->a_cnp->cn_cred));
4656 }
4657 
4658 #ifndef _SYS_SYSPROTO_H_
4659 struct vop_mkdir_args {
4660 	struct vnode *a_dvp;
4661 	struct vnode **a_vpp;
4662 	struct componentname *a_cnp;
4663 	struct vattr *a_vap;
4664 };
4665 #endif
4666 
4667 static int
zfs_freebsd_mkdir(struct vop_mkdir_args * ap)4668 zfs_freebsd_mkdir(struct vop_mkdir_args *ap)
4669 {
4670 	vattr_t *vap = ap->a_vap;
4671 	znode_t *zp = NULL;
4672 	int rc;
4673 
4674 #if __FreeBSD_version < 1400068
4675 	ASSERT(ap->a_cnp->cn_flags & SAVENAME);
4676 #endif
4677 
4678 	vattr_init_mask(vap);
4679 	*ap->a_vpp = NULL;
4680 
4681 	rc = zfs_mkdir(VTOZ(ap->a_dvp), ap->a_cnp->cn_nameptr, vap, &zp,
4682 	    ap->a_cnp->cn_cred, 0, NULL, NULL);
4683 
4684 	if (rc == 0)
4685 		*ap->a_vpp = ZTOV(zp);
4686 	return (rc);
4687 }
4688 
4689 #ifndef _SYS_SYSPROTO_H_
4690 struct vop_rmdir_args {
4691 	struct vnode *a_dvp;
4692 	struct vnode *a_vp;
4693 	struct componentname *a_cnp;
4694 };
4695 #endif
4696 
4697 static int
zfs_freebsd_rmdir(struct vop_rmdir_args * ap)4698 zfs_freebsd_rmdir(struct vop_rmdir_args *ap)
4699 {
4700 	struct componentname *cnp = ap->a_cnp;
4701 
4702 #if __FreeBSD_version < 1400068
4703 	ASSERT(cnp->cn_flags & SAVENAME);
4704 #endif
4705 
4706 	return (zfs_rmdir_(ap->a_dvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred));
4707 }
4708 
4709 #ifndef _SYS_SYSPROTO_H_
4710 struct vop_readdir_args {
4711 	struct vnode *a_vp;
4712 	struct uio *a_uio;
4713 	struct ucred *a_cred;
4714 	int *a_eofflag;
4715 	int *a_ncookies;
4716 	cookie_t **a_cookies;
4717 };
4718 #endif
4719 
4720 static int
zfs_freebsd_readdir(struct vop_readdir_args * ap)4721 zfs_freebsd_readdir(struct vop_readdir_args *ap)
4722 {
4723 	zfs_uio_t uio;
4724 	zfs_uio_init(&uio, ap->a_uio);
4725 	return (zfs_readdir(ap->a_vp, &uio, ap->a_cred, ap->a_eofflag,
4726 	    ap->a_ncookies, ap->a_cookies));
4727 }
4728 
4729 #ifndef _SYS_SYSPROTO_H_
4730 struct vop_fsync_args {
4731 	struct vnode *a_vp;
4732 	int a_waitfor;
4733 	struct thread *a_td;
4734 };
4735 #endif
4736 
4737 static int
zfs_freebsd_fsync(struct vop_fsync_args * ap)4738 zfs_freebsd_fsync(struct vop_fsync_args *ap)
4739 {
4740 
4741 	return (zfs_fsync(VTOZ(ap->a_vp), 0, ap->a_td->td_ucred));
4742 }
4743 
4744 #ifndef _SYS_SYSPROTO_H_
4745 struct vop_getattr_args {
4746 	struct vnode *a_vp;
4747 	struct vattr *a_vap;
4748 	struct ucred *a_cred;
4749 };
4750 #endif
4751 
4752 static int
zfs_freebsd_getattr(struct vop_getattr_args * ap)4753 zfs_freebsd_getattr(struct vop_getattr_args *ap)
4754 {
4755 	vattr_t *vap = ap->a_vap;
4756 	xvattr_t xvap;
4757 	ulong_t fflags = 0;
4758 	int error;
4759 
4760 	xva_init(&xvap);
4761 	xvap.xva_vattr = *vap;
4762 	xvap.xva_vattr.va_mask |= AT_XVATTR;
4763 
4764 	/* Convert chflags into ZFS-type flags. */
4765 	/* XXX: what about SF_SETTABLE?. */
4766 	XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
4767 	XVA_SET_REQ(&xvap, XAT_APPENDONLY);
4768 	XVA_SET_REQ(&xvap, XAT_NOUNLINK);
4769 	XVA_SET_REQ(&xvap, XAT_NODUMP);
4770 	XVA_SET_REQ(&xvap, XAT_READONLY);
4771 	XVA_SET_REQ(&xvap, XAT_ARCHIVE);
4772 	XVA_SET_REQ(&xvap, XAT_SYSTEM);
4773 	XVA_SET_REQ(&xvap, XAT_HIDDEN);
4774 	XVA_SET_REQ(&xvap, XAT_REPARSE);
4775 	XVA_SET_REQ(&xvap, XAT_OFFLINE);
4776 	XVA_SET_REQ(&xvap, XAT_SPARSE);
4777 
4778 	error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred);
4779 	if (error != 0)
4780 		return (error);
4781 
4782 	/* Convert ZFS xattr into chflags. */
4783 #define	FLAG_CHECK(fflag, xflag, xfield)	do {			\
4784 	if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0)		\
4785 		fflags |= (fflag);					\
4786 } while (0)
4787 	FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
4788 	    xvap.xva_xoptattrs.xoa_immutable);
4789 	FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
4790 	    xvap.xva_xoptattrs.xoa_appendonly);
4791 	FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
4792 	    xvap.xva_xoptattrs.xoa_nounlink);
4793 	FLAG_CHECK(UF_ARCHIVE, XAT_ARCHIVE,
4794 	    xvap.xva_xoptattrs.xoa_archive);
4795 	FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
4796 	    xvap.xva_xoptattrs.xoa_nodump);
4797 	FLAG_CHECK(UF_READONLY, XAT_READONLY,
4798 	    xvap.xva_xoptattrs.xoa_readonly);
4799 	FLAG_CHECK(UF_SYSTEM, XAT_SYSTEM,
4800 	    xvap.xva_xoptattrs.xoa_system);
4801 	FLAG_CHECK(UF_HIDDEN, XAT_HIDDEN,
4802 	    xvap.xva_xoptattrs.xoa_hidden);
4803 	FLAG_CHECK(UF_REPARSE, XAT_REPARSE,
4804 	    xvap.xva_xoptattrs.xoa_reparse);
4805 	FLAG_CHECK(UF_OFFLINE, XAT_OFFLINE,
4806 	    xvap.xva_xoptattrs.xoa_offline);
4807 	FLAG_CHECK(UF_SPARSE, XAT_SPARSE,
4808 	    xvap.xva_xoptattrs.xoa_sparse);
4809 
4810 #undef	FLAG_CHECK
4811 	*vap = xvap.xva_vattr;
4812 	vap->va_flags = fflags;
4813 	return (0);
4814 }
4815 
4816 #ifndef _SYS_SYSPROTO_H_
4817 struct vop_setattr_args {
4818 	struct vnode *a_vp;
4819 	struct vattr *a_vap;
4820 	struct ucred *a_cred;
4821 };
4822 #endif
4823 
4824 static int
zfs_freebsd_setattr(struct vop_setattr_args * ap)4825 zfs_freebsd_setattr(struct vop_setattr_args *ap)
4826 {
4827 	vnode_t *vp = ap->a_vp;
4828 	vattr_t *vap = ap->a_vap;
4829 	cred_t *cred = ap->a_cred;
4830 	xvattr_t xvap;
4831 	ulong_t fflags;
4832 	uint64_t zflags;
4833 
4834 	vattr_init_mask(vap);
4835 	vap->va_mask &= ~AT_NOSET;
4836 
4837 	xva_init(&xvap);
4838 	xvap.xva_vattr = *vap;
4839 
4840 	zflags = VTOZ(vp)->z_pflags;
4841 
4842 	if (vap->va_flags != VNOVAL) {
4843 		zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
4844 		int error;
4845 
4846 		if (zfsvfs->z_use_fuids == B_FALSE)
4847 			return (EOPNOTSUPP);
4848 
4849 		fflags = vap->va_flags;
4850 		/*
4851 		 * XXX KDM
4852 		 * We need to figure out whether it makes sense to allow
4853 		 * UF_REPARSE through, since we don't really have other
4854 		 * facilities to handle reparse points and zfs_setattr()
4855 		 * doesn't currently allow setting that attribute anyway.
4856 		 */
4857 		if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_ARCHIVE|
4858 		    UF_NODUMP|UF_SYSTEM|UF_HIDDEN|UF_READONLY|UF_REPARSE|
4859 		    UF_OFFLINE|UF_SPARSE)) != 0)
4860 			return (EOPNOTSUPP);
4861 		/*
4862 		 * Unprivileged processes are not permitted to unset system
4863 		 * flags, or modify flags if any system flags are set.
4864 		 * Privileged non-jail processes may not modify system flags
4865 		 * if securelevel > 0 and any existing system flags are set.
4866 		 * Privileged jail processes behave like privileged non-jail
4867 		 * processes if the PR_ALLOW_CHFLAGS permission bit is set;
4868 		 * otherwise, they behave like unprivileged processes.
4869 		 */
4870 		if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
4871 		    spl_priv_check_cred(cred, PRIV_VFS_SYSFLAGS) == 0) {
4872 			if (zflags &
4873 			    (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
4874 				error = securelevel_gt(cred, 0);
4875 				if (error != 0)
4876 					return (error);
4877 			}
4878 		} else {
4879 			/*
4880 			 * Callers may only modify the file flags on
4881 			 * objects they have VADMIN rights for.
4882 			 */
4883 			if ((error = VOP_ACCESS(vp, VADMIN, cred,
4884 			    curthread)) != 0)
4885 				return (error);
4886 			if (zflags &
4887 			    (ZFS_IMMUTABLE | ZFS_APPENDONLY |
4888 			    ZFS_NOUNLINK)) {
4889 				return (EPERM);
4890 			}
4891 			if (fflags &
4892 			    (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
4893 				return (EPERM);
4894 			}
4895 		}
4896 
4897 #define	FLAG_CHANGE(fflag, zflag, xflag, xfield)	do {		\
4898 	if (((fflags & (fflag)) && !(zflags & (zflag))) ||		\
4899 	    ((zflags & (zflag)) && !(fflags & (fflag)))) {		\
4900 		XVA_SET_REQ(&xvap, (xflag));				\
4901 		(xfield) = ((fflags & (fflag)) != 0);			\
4902 	}								\
4903 } while (0)
4904 		/* Convert chflags into ZFS-type flags. */
4905 		/* XXX: what about SF_SETTABLE?. */
4906 		FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
4907 		    xvap.xva_xoptattrs.xoa_immutable);
4908 		FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
4909 		    xvap.xva_xoptattrs.xoa_appendonly);
4910 		FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
4911 		    xvap.xva_xoptattrs.xoa_nounlink);
4912 		FLAG_CHANGE(UF_ARCHIVE, ZFS_ARCHIVE, XAT_ARCHIVE,
4913 		    xvap.xva_xoptattrs.xoa_archive);
4914 		FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
4915 		    xvap.xva_xoptattrs.xoa_nodump);
4916 		FLAG_CHANGE(UF_READONLY, ZFS_READONLY, XAT_READONLY,
4917 		    xvap.xva_xoptattrs.xoa_readonly);
4918 		FLAG_CHANGE(UF_SYSTEM, ZFS_SYSTEM, XAT_SYSTEM,
4919 		    xvap.xva_xoptattrs.xoa_system);
4920 		FLAG_CHANGE(UF_HIDDEN, ZFS_HIDDEN, XAT_HIDDEN,
4921 		    xvap.xva_xoptattrs.xoa_hidden);
4922 		FLAG_CHANGE(UF_REPARSE, ZFS_REPARSE, XAT_REPARSE,
4923 		    xvap.xva_xoptattrs.xoa_reparse);
4924 		FLAG_CHANGE(UF_OFFLINE, ZFS_OFFLINE, XAT_OFFLINE,
4925 		    xvap.xva_xoptattrs.xoa_offline);
4926 		FLAG_CHANGE(UF_SPARSE, ZFS_SPARSE, XAT_SPARSE,
4927 		    xvap.xva_xoptattrs.xoa_sparse);
4928 #undef	FLAG_CHANGE
4929 	}
4930 	if (vap->va_birthtime.tv_sec != VNOVAL) {
4931 		xvap.xva_vattr.va_mask |= AT_XVATTR;
4932 		XVA_SET_REQ(&xvap, XAT_CREATETIME);
4933 	}
4934 	return (zfs_setattr(VTOZ(vp), (vattr_t *)&xvap, 0, cred, NULL));
4935 }
4936 
4937 #ifndef _SYS_SYSPROTO_H_
4938 struct vop_rename_args {
4939 	struct vnode *a_fdvp;
4940 	struct vnode *a_fvp;
4941 	struct componentname *a_fcnp;
4942 	struct vnode *a_tdvp;
4943 	struct vnode *a_tvp;
4944 	struct componentname *a_tcnp;
4945 };
4946 #endif
4947 
4948 static int
zfs_freebsd_rename(struct vop_rename_args * ap)4949 zfs_freebsd_rename(struct vop_rename_args *ap)
4950 {
4951 	vnode_t *fdvp = ap->a_fdvp;
4952 	vnode_t *fvp = ap->a_fvp;
4953 	vnode_t *tdvp = ap->a_tdvp;
4954 	vnode_t *tvp = ap->a_tvp;
4955 	int error;
4956 
4957 #if __FreeBSD_version < 1400068
4958 	ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
4959 	ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
4960 #endif
4961 
4962 	error = zfs_do_rename(fdvp, &fvp, ap->a_fcnp, tdvp, &tvp,
4963 	    ap->a_tcnp, ap->a_fcnp->cn_cred);
4964 
4965 	vrele(fdvp);
4966 	vrele(fvp);
4967 	vrele(tdvp);
4968 	if (tvp != NULL)
4969 		vrele(tvp);
4970 
4971 	return (error);
4972 }
4973 
4974 #ifndef _SYS_SYSPROTO_H_
4975 struct vop_symlink_args {
4976 	struct vnode *a_dvp;
4977 	struct vnode **a_vpp;
4978 	struct componentname *a_cnp;
4979 	struct vattr *a_vap;
4980 	char *a_target;
4981 };
4982 #endif
4983 
4984 static int
zfs_freebsd_symlink(struct vop_symlink_args * ap)4985 zfs_freebsd_symlink(struct vop_symlink_args *ap)
4986 {
4987 	struct componentname *cnp = ap->a_cnp;
4988 	vattr_t *vap = ap->a_vap;
4989 	znode_t *zp = NULL;
4990 #if __FreeBSD_version >= 1300139
4991 	char *symlink;
4992 	size_t symlink_len;
4993 #endif
4994 	int rc;
4995 
4996 #if __FreeBSD_version < 1400068
4997 	ASSERT(cnp->cn_flags & SAVENAME);
4998 #endif
4999 
5000 	vap->va_type = VLNK;	/* FreeBSD: Syscall only sets va_mode. */
5001 	vattr_init_mask(vap);
5002 	*ap->a_vpp = NULL;
5003 
5004 	rc = zfs_symlink(VTOZ(ap->a_dvp), cnp->cn_nameptr, vap,
5005 	    ap->a_target, &zp, cnp->cn_cred, 0 /* flags */, NULL);
5006 	if (rc == 0) {
5007 		*ap->a_vpp = ZTOV(zp);
5008 		ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
5009 #if __FreeBSD_version >= 1300139
5010 		MPASS(zp->z_cached_symlink == NULL);
5011 		symlink_len = strlen(ap->a_target);
5012 		symlink = cache_symlink_alloc(symlink_len + 1, M_WAITOK);
5013 		if (symlink != NULL) {
5014 			memcpy(symlink, ap->a_target, symlink_len);
5015 			symlink[symlink_len] = '\0';
5016 			atomic_store_rel_ptr((uintptr_t *)&zp->z_cached_symlink,
5017 			    (uintptr_t)symlink);
5018 		}
5019 #endif
5020 	}
5021 	return (rc);
5022 }
5023 
5024 #ifndef _SYS_SYSPROTO_H_
5025 struct vop_readlink_args {
5026 	struct vnode *a_vp;
5027 	struct uio *a_uio;
5028 	struct ucred *a_cred;
5029 };
5030 #endif
5031 
5032 static int
zfs_freebsd_readlink(struct vop_readlink_args * ap)5033 zfs_freebsd_readlink(struct vop_readlink_args *ap)
5034 {
5035 	zfs_uio_t uio;
5036 	int error;
5037 #if __FreeBSD_version >= 1300139
5038 	znode_t	*zp = VTOZ(ap->a_vp);
5039 	char *symlink, *base;
5040 	size_t symlink_len;
5041 	bool trycache;
5042 #endif
5043 
5044 	zfs_uio_init(&uio, ap->a_uio);
5045 #if __FreeBSD_version >= 1300139
5046 	trycache = false;
5047 	if (zfs_uio_segflg(&uio) == UIO_SYSSPACE &&
5048 	    zfs_uio_iovcnt(&uio) == 1) {
5049 		base = zfs_uio_iovbase(&uio, 0);
5050 		symlink_len = zfs_uio_iovlen(&uio, 0);
5051 		trycache = true;
5052 	}
5053 #endif
5054 	error = zfs_readlink(ap->a_vp, &uio, ap->a_cred, NULL);
5055 #if __FreeBSD_version >= 1300139
5056 	if (atomic_load_ptr(&zp->z_cached_symlink) != NULL ||
5057 	    error != 0 || !trycache) {
5058 		return (error);
5059 	}
5060 	symlink_len -= zfs_uio_resid(&uio);
5061 	symlink = cache_symlink_alloc(symlink_len + 1, M_WAITOK);
5062 	if (symlink != NULL) {
5063 		memcpy(symlink, base, symlink_len);
5064 		symlink[symlink_len] = '\0';
5065 		if (!atomic_cmpset_rel_ptr((uintptr_t *)&zp->z_cached_symlink,
5066 		    (uintptr_t)NULL, (uintptr_t)symlink)) {
5067 			cache_symlink_free(symlink, symlink_len + 1);
5068 		}
5069 	}
5070 #endif
5071 	return (error);
5072 }
5073 
5074 #ifndef _SYS_SYSPROTO_H_
5075 struct vop_link_args {
5076 	struct vnode *a_tdvp;
5077 	struct vnode *a_vp;
5078 	struct componentname *a_cnp;
5079 };
5080 #endif
5081 
5082 static int
zfs_freebsd_link(struct vop_link_args * ap)5083 zfs_freebsd_link(struct vop_link_args *ap)
5084 {
5085 	struct componentname *cnp = ap->a_cnp;
5086 	vnode_t *vp = ap->a_vp;
5087 	vnode_t *tdvp = ap->a_tdvp;
5088 
5089 	if (tdvp->v_mount != vp->v_mount)
5090 		return (EXDEV);
5091 
5092 #if __FreeBSD_version < 1400068
5093 	ASSERT(cnp->cn_flags & SAVENAME);
5094 #endif
5095 
5096 	return (zfs_link(VTOZ(tdvp), VTOZ(vp),
5097 	    cnp->cn_nameptr, cnp->cn_cred, 0));
5098 }
5099 
5100 #ifndef _SYS_SYSPROTO_H_
5101 struct vop_inactive_args {
5102 	struct vnode *a_vp;
5103 	struct thread *a_td;
5104 };
5105 #endif
5106 
5107 static int
zfs_freebsd_inactive(struct vop_inactive_args * ap)5108 zfs_freebsd_inactive(struct vop_inactive_args *ap)
5109 {
5110 	vnode_t *vp = ap->a_vp;
5111 
5112 #if __FreeBSD_version >= 1300123
5113 	zfs_inactive(vp, curthread->td_ucred, NULL);
5114 #else
5115 	zfs_inactive(vp, ap->a_td->td_ucred, NULL);
5116 #endif
5117 	return (0);
5118 }
5119 
5120 #if __FreeBSD_version >= 1300042
5121 #ifndef _SYS_SYSPROTO_H_
5122 struct vop_need_inactive_args {
5123 	struct vnode *a_vp;
5124 	struct thread *a_td;
5125 };
5126 #endif
5127 
5128 static int
zfs_freebsd_need_inactive(struct vop_need_inactive_args * ap)5129 zfs_freebsd_need_inactive(struct vop_need_inactive_args *ap)
5130 {
5131 	vnode_t *vp = ap->a_vp;
5132 	znode_t	*zp = VTOZ(vp);
5133 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5134 	int need;
5135 
5136 	if (vn_need_pageq_flush(vp))
5137 		return (1);
5138 
5139 	if (!ZFS_TEARDOWN_INACTIVE_TRY_ENTER_READ(zfsvfs))
5140 		return (1);
5141 	need = (zp->z_sa_hdl == NULL || zp->z_unlinked || zp->z_atime_dirty);
5142 	ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
5143 
5144 	return (need);
5145 }
5146 #endif
5147 
5148 #ifndef _SYS_SYSPROTO_H_
5149 struct vop_reclaim_args {
5150 	struct vnode *a_vp;
5151 	struct thread *a_td;
5152 };
5153 #endif
5154 
5155 static int
zfs_freebsd_reclaim(struct vop_reclaim_args * ap)5156 zfs_freebsd_reclaim(struct vop_reclaim_args *ap)
5157 {
5158 	vnode_t	*vp = ap->a_vp;
5159 	znode_t	*zp = VTOZ(vp);
5160 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5161 
5162 	ASSERT3P(zp, !=, NULL);
5163 
5164 #if __FreeBSD_version < 1300042
5165 	/* Destroy the vm object and flush associated pages. */
5166 	vnode_destroy_vobject(vp);
5167 #endif
5168 	/*
5169 	 * z_teardown_inactive_lock protects from a race with
5170 	 * zfs_znode_dmu_fini in zfsvfs_teardown during
5171 	 * force unmount.
5172 	 */
5173 	ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs);
5174 	if (zp->z_sa_hdl == NULL)
5175 		zfs_znode_free(zp);
5176 	else
5177 		zfs_zinactive(zp);
5178 	ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
5179 
5180 	vp->v_data = NULL;
5181 	return (0);
5182 }
5183 
5184 #ifndef _SYS_SYSPROTO_H_
5185 struct vop_fid_args {
5186 	struct vnode *a_vp;
5187 	struct fid *a_fid;
5188 };
5189 #endif
5190 
5191 static int
zfs_freebsd_fid(struct vop_fid_args * ap)5192 zfs_freebsd_fid(struct vop_fid_args *ap)
5193 {
5194 
5195 	return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
5196 }
5197 
5198 
5199 #ifndef _SYS_SYSPROTO_H_
5200 struct vop_pathconf_args {
5201 	struct vnode *a_vp;
5202 	int a_name;
5203 	register_t *a_retval;
5204 } *ap;
5205 #endif
5206 
5207 static int
zfs_freebsd_pathconf(struct vop_pathconf_args * ap)5208 zfs_freebsd_pathconf(struct vop_pathconf_args *ap)
5209 {
5210 	ulong_t val;
5211 	int error;
5212 
5213 	error = zfs_pathconf(ap->a_vp, ap->a_name, &val,
5214 	    curthread->td_ucred, NULL);
5215 	if (error == 0) {
5216 		*ap->a_retval = val;
5217 		return (error);
5218 	}
5219 	if (error != EOPNOTSUPP)
5220 		return (error);
5221 
5222 	switch (ap->a_name) {
5223 	case _PC_NAME_MAX:
5224 		*ap->a_retval = NAME_MAX;
5225 		return (0);
5226 #if __FreeBSD_version >= 1400032
5227 	case _PC_DEALLOC_PRESENT:
5228 		*ap->a_retval = 1;
5229 		return (0);
5230 #endif
5231 	case _PC_PIPE_BUF:
5232 		if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO) {
5233 			*ap->a_retval = PIPE_BUF;
5234 			return (0);
5235 		}
5236 		return (EINVAL);
5237 	default:
5238 		return (vop_stdpathconf(ap));
5239 	}
5240 }
5241 
5242 static int zfs_xattr_compat = 1;
5243 
5244 static int
zfs_check_attrname(const char * name)5245 zfs_check_attrname(const char *name)
5246 {
5247 	/* We don't allow '/' character in attribute name. */
5248 	if (strchr(name, '/') != NULL)
5249 		return (SET_ERROR(EINVAL));
5250 	/* We don't allow attribute names that start with a namespace prefix. */
5251 	if (ZFS_XA_NS_PREFIX_FORBIDDEN(name))
5252 		return (SET_ERROR(EINVAL));
5253 	return (0);
5254 }
5255 
5256 /*
5257  * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
5258  * extended attribute name:
5259  *
5260  *	NAMESPACE	XATTR_COMPAT	PREFIX
5261  *	system		*		freebsd:system:
5262  *	user		1		(none, can be used to access ZFS
5263  *					fsattr(5) attributes created on Solaris)
5264  *	user		0		user.
5265  */
5266 static int
zfs_create_attrname(int attrnamespace,const char * name,char * attrname,size_t size,boolean_t compat)5267 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
5268     size_t size, boolean_t compat)
5269 {
5270 	const char *namespace, *prefix, *suffix;
5271 
5272 	memset(attrname, 0, size);
5273 
5274 	switch (attrnamespace) {
5275 	case EXTATTR_NAMESPACE_USER:
5276 		if (compat) {
5277 			/*
5278 			 * This is the default namespace by which we can access
5279 			 * all attributes created on Solaris.
5280 			 */
5281 			prefix = namespace = suffix = "";
5282 		} else {
5283 			/*
5284 			 * This is compatible with the user namespace encoding
5285 			 * on Linux prior to xattr_compat, but nothing
5286 			 * else.
5287 			 */
5288 			prefix = "";
5289 			namespace = "user";
5290 			suffix = ".";
5291 		}
5292 		break;
5293 	case EXTATTR_NAMESPACE_SYSTEM:
5294 		prefix = "freebsd:";
5295 		namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
5296 		suffix = ":";
5297 		break;
5298 	case EXTATTR_NAMESPACE_EMPTY:
5299 	default:
5300 		return (SET_ERROR(EINVAL));
5301 	}
5302 	if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
5303 	    name) >= size) {
5304 		return (SET_ERROR(ENAMETOOLONG));
5305 	}
5306 	return (0);
5307 }
5308 
5309 static int
zfs_ensure_xattr_cached(znode_t * zp)5310 zfs_ensure_xattr_cached(znode_t *zp)
5311 {
5312 	int error = 0;
5313 
5314 	ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
5315 
5316 	if (zp->z_xattr_cached != NULL)
5317 		return (0);
5318 
5319 	if (rw_write_held(&zp->z_xattr_lock))
5320 		return (zfs_sa_get_xattr(zp));
5321 
5322 	if (!rw_tryupgrade(&zp->z_xattr_lock)) {
5323 		rw_exit(&zp->z_xattr_lock);
5324 		rw_enter(&zp->z_xattr_lock, RW_WRITER);
5325 	}
5326 	if (zp->z_xattr_cached == NULL)
5327 		error = zfs_sa_get_xattr(zp);
5328 	rw_downgrade(&zp->z_xattr_lock);
5329 	return (error);
5330 }
5331 
5332 #ifndef _SYS_SYSPROTO_H_
5333 struct vop_getextattr {
5334 	IN struct vnode *a_vp;
5335 	IN int a_attrnamespace;
5336 	IN const char *a_name;
5337 	INOUT struct uio *a_uio;
5338 	OUT size_t *a_size;
5339 	IN struct ucred *a_cred;
5340 	IN struct thread *a_td;
5341 };
5342 #endif
5343 
5344 static int
zfs_getextattr_dir(struct vop_getextattr_args * ap,const char * attrname)5345 zfs_getextattr_dir(struct vop_getextattr_args *ap, const char *attrname)
5346 {
5347 	struct thread *td = ap->a_td;
5348 	struct nameidata nd;
5349 	struct vattr va;
5350 	vnode_t *xvp = NULL, *vp;
5351 	int error, flags;
5352 
5353 	error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5354 	    LOOKUP_XATTR, B_FALSE);
5355 	if (error != 0)
5356 		return (error);
5357 
5358 	flags = FREAD;
5359 #if __FreeBSD_version < 1400043
5360 	NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
5361 	    xvp, td);
5362 #else
5363 	NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp);
5364 #endif
5365 	error = vn_open_cred(&nd, &flags, 0, VN_OPEN_INVFS, ap->a_cred, NULL);
5366 	if (error != 0)
5367 		return (SET_ERROR(error));
5368 	vp = nd.ni_vp;
5369 	NDFREE_PNBUF(&nd);
5370 
5371 	if (ap->a_size != NULL) {
5372 		error = VOP_GETATTR(vp, &va, ap->a_cred);
5373 		if (error == 0)
5374 			*ap->a_size = (size_t)va.va_size;
5375 	} else if (ap->a_uio != NULL)
5376 		error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5377 
5378 	VOP_UNLOCK1(vp);
5379 	vn_close(vp, flags, ap->a_cred, td);
5380 	return (error);
5381 }
5382 
5383 static int
zfs_getextattr_sa(struct vop_getextattr_args * ap,const char * attrname)5384 zfs_getextattr_sa(struct vop_getextattr_args *ap, const char *attrname)
5385 {
5386 	znode_t *zp = VTOZ(ap->a_vp);
5387 	uchar_t *nv_value;
5388 	uint_t nv_size;
5389 	int error;
5390 
5391 	error = zfs_ensure_xattr_cached(zp);
5392 	if (error != 0)
5393 		return (error);
5394 
5395 	ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
5396 	ASSERT3P(zp->z_xattr_cached, !=, NULL);
5397 
5398 	error = nvlist_lookup_byte_array(zp->z_xattr_cached, attrname,
5399 	    &nv_value, &nv_size);
5400 	if (error != 0)
5401 		return (SET_ERROR(error));
5402 
5403 	if (ap->a_size != NULL)
5404 		*ap->a_size = nv_size;
5405 	else if (ap->a_uio != NULL)
5406 		error = uiomove(nv_value, nv_size, ap->a_uio);
5407 	if (error != 0)
5408 		return (SET_ERROR(error));
5409 
5410 	return (0);
5411 }
5412 
5413 static int
zfs_getextattr_impl(struct vop_getextattr_args * ap,boolean_t compat)5414 zfs_getextattr_impl(struct vop_getextattr_args *ap, boolean_t compat)
5415 {
5416 	znode_t *zp = VTOZ(ap->a_vp);
5417 	zfsvfs_t *zfsvfs = ZTOZSB(zp);
5418 	char attrname[EXTATTR_MAXNAMELEN+1];
5419 	int error;
5420 
5421 	error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5422 	    sizeof (attrname), compat);
5423 	if (error != 0)
5424 		return (error);
5425 
5426 	error = ENOENT;
5427 	if (zfsvfs->z_use_sa && zp->z_is_sa)
5428 		error = zfs_getextattr_sa(ap, attrname);
5429 	if (error == ENOENT)
5430 		error = zfs_getextattr_dir(ap, attrname);
5431 	return (error);
5432 }
5433 
5434 /*
5435  * Vnode operation to retrieve a named extended attribute.
5436  */
5437 static int
zfs_getextattr(struct vop_getextattr_args * ap)5438 zfs_getextattr(struct vop_getextattr_args *ap)
5439 {
5440 	znode_t *zp = VTOZ(ap->a_vp);
5441 	zfsvfs_t *zfsvfs = ZTOZSB(zp);
5442 	int error;
5443 
5444 	/*
5445 	 * If the xattr property is off, refuse the request.
5446 	 */
5447 	if (!(zfsvfs->z_flags & ZSB_XATTR))
5448 		return (SET_ERROR(EOPNOTSUPP));
5449 
5450 	error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5451 	    ap->a_cred, ap->a_td, VREAD);
5452 	if (error != 0)
5453 		return (SET_ERROR(error));
5454 
5455 	error = zfs_check_attrname(ap->a_name);
5456 	if (error != 0)
5457 		return (error);
5458 
5459 	if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5460 		return (error);
5461 	error = ENOENT;
5462 	rw_enter(&zp->z_xattr_lock, RW_READER);
5463 
5464 	error = zfs_getextattr_impl(ap, zfs_xattr_compat);
5465 	if ((error == ENOENT || error == ENOATTR) &&
5466 	    ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5467 		/*
5468 		 * Fall back to the alternate namespace format if we failed to
5469 		 * find a user xattr.
5470 		 */
5471 		error = zfs_getextattr_impl(ap, !zfs_xattr_compat);
5472 	}
5473 
5474 	rw_exit(&zp->z_xattr_lock);
5475 	zfs_exit(zfsvfs, FTAG);
5476 	if (error == ENOENT)
5477 		error = SET_ERROR(ENOATTR);
5478 	return (error);
5479 }
5480 
5481 #ifndef _SYS_SYSPROTO_H_
5482 struct vop_deleteextattr {
5483 	IN struct vnode *a_vp;
5484 	IN int a_attrnamespace;
5485 	IN const char *a_name;
5486 	IN struct ucred *a_cred;
5487 	IN struct thread *a_td;
5488 };
5489 #endif
5490 
5491 static int
zfs_deleteextattr_dir(struct vop_deleteextattr_args * ap,const char * attrname)5492 zfs_deleteextattr_dir(struct vop_deleteextattr_args *ap, const char *attrname)
5493 {
5494 	struct nameidata nd;
5495 	vnode_t *xvp = NULL, *vp;
5496 	int error;
5497 
5498 	error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5499 	    LOOKUP_XATTR, B_FALSE);
5500 	if (error != 0)
5501 		return (error);
5502 
5503 #if __FreeBSD_version < 1400043
5504 	NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
5505 	    UIO_SYSSPACE, attrname, xvp, ap->a_td);
5506 #else
5507 	NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
5508 	    UIO_SYSSPACE, attrname, xvp);
5509 #endif
5510 	error = namei(&nd);
5511 	if (error != 0)
5512 		return (SET_ERROR(error));
5513 
5514 	vp = nd.ni_vp;
5515 	error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
5516 	NDFREE_PNBUF(&nd);
5517 
5518 	vput(nd.ni_dvp);
5519 	if (vp == nd.ni_dvp)
5520 		vrele(vp);
5521 	else
5522 		vput(vp);
5523 
5524 	return (error);
5525 }
5526 
5527 static int
zfs_deleteextattr_sa(struct vop_deleteextattr_args * ap,const char * attrname)5528 zfs_deleteextattr_sa(struct vop_deleteextattr_args *ap, const char *attrname)
5529 {
5530 	znode_t *zp = VTOZ(ap->a_vp);
5531 	nvlist_t *nvl;
5532 	int error;
5533 
5534 	error = zfs_ensure_xattr_cached(zp);
5535 	if (error != 0)
5536 		return (error);
5537 
5538 	ASSERT(RW_WRITE_HELD(&zp->z_xattr_lock));
5539 	ASSERT3P(zp->z_xattr_cached, !=, NULL);
5540 
5541 	nvl = zp->z_xattr_cached;
5542 	error = nvlist_remove(nvl, attrname, DATA_TYPE_BYTE_ARRAY);
5543 	if (error != 0)
5544 		error = SET_ERROR(error);
5545 	else
5546 		error = zfs_sa_set_xattr(zp, attrname, NULL, 0);
5547 	if (error != 0) {
5548 		zp->z_xattr_cached = NULL;
5549 		nvlist_free(nvl);
5550 	}
5551 	return (error);
5552 }
5553 
5554 static int
zfs_deleteextattr_impl(struct vop_deleteextattr_args * ap,boolean_t compat)5555 zfs_deleteextattr_impl(struct vop_deleteextattr_args *ap, boolean_t compat)
5556 {
5557 	znode_t *zp = VTOZ(ap->a_vp);
5558 	zfsvfs_t *zfsvfs = ZTOZSB(zp);
5559 	char attrname[EXTATTR_MAXNAMELEN+1];
5560 	int error;
5561 
5562 	error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5563 	    sizeof (attrname), compat);
5564 	if (error != 0)
5565 		return (error);
5566 
5567 	error = ENOENT;
5568 	if (zfsvfs->z_use_sa && zp->z_is_sa)
5569 		error = zfs_deleteextattr_sa(ap, attrname);
5570 	if (error == ENOENT)
5571 		error = zfs_deleteextattr_dir(ap, attrname);
5572 	return (error);
5573 }
5574 
5575 /*
5576  * Vnode operation to remove a named attribute.
5577  */
5578 static int
zfs_deleteextattr(struct vop_deleteextattr_args * ap)5579 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
5580 {
5581 	znode_t *zp = VTOZ(ap->a_vp);
5582 	zfsvfs_t *zfsvfs = ZTOZSB(zp);
5583 	int error;
5584 
5585 	/*
5586 	 * If the xattr property is off, refuse the request.
5587 	 */
5588 	if (!(zfsvfs->z_flags & ZSB_XATTR))
5589 		return (SET_ERROR(EOPNOTSUPP));
5590 
5591 	error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5592 	    ap->a_cred, ap->a_td, VWRITE);
5593 	if (error != 0)
5594 		return (SET_ERROR(error));
5595 
5596 	error = zfs_check_attrname(ap->a_name);
5597 	if (error != 0)
5598 		return (error);
5599 
5600 	if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5601 		return (error);
5602 	rw_enter(&zp->z_xattr_lock, RW_WRITER);
5603 
5604 	error = zfs_deleteextattr_impl(ap, zfs_xattr_compat);
5605 	if ((error == ENOENT || error == ENOATTR) &&
5606 	    ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5607 		/*
5608 		 * Fall back to the alternate namespace format if we failed to
5609 		 * find a user xattr.
5610 		 */
5611 		error = zfs_deleteextattr_impl(ap, !zfs_xattr_compat);
5612 	}
5613 
5614 	rw_exit(&zp->z_xattr_lock);
5615 	zfs_exit(zfsvfs, FTAG);
5616 	if (error == ENOENT)
5617 		error = SET_ERROR(ENOATTR);
5618 	return (error);
5619 }
5620 
5621 #ifndef _SYS_SYSPROTO_H_
5622 struct vop_setextattr {
5623 	IN struct vnode *a_vp;
5624 	IN int a_attrnamespace;
5625 	IN const char *a_name;
5626 	INOUT struct uio *a_uio;
5627 	IN struct ucred *a_cred;
5628 	IN struct thread *a_td;
5629 };
5630 #endif
5631 
5632 static int
zfs_setextattr_dir(struct vop_setextattr_args * ap,const char * attrname)5633 zfs_setextattr_dir(struct vop_setextattr_args *ap, const char *attrname)
5634 {
5635 	struct thread *td = ap->a_td;
5636 	struct nameidata nd;
5637 	struct vattr va;
5638 	vnode_t *xvp = NULL, *vp;
5639 	int error, flags;
5640 
5641 	error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5642 	    LOOKUP_XATTR | CREATE_XATTR_DIR, B_FALSE);
5643 	if (error != 0)
5644 		return (error);
5645 
5646 	flags = FFLAGS(O_WRONLY | O_CREAT);
5647 #if __FreeBSD_version < 1400043
5648 	NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp, td);
5649 #else
5650 	NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp);
5651 #endif
5652 	error = vn_open_cred(&nd, &flags, 0600, VN_OPEN_INVFS, ap->a_cred,
5653 	    NULL);
5654 	if (error != 0)
5655 		return (SET_ERROR(error));
5656 	vp = nd.ni_vp;
5657 	NDFREE_PNBUF(&nd);
5658 
5659 	VATTR_NULL(&va);
5660 	va.va_size = 0;
5661 	error = VOP_SETATTR(vp, &va, ap->a_cred);
5662 	if (error == 0)
5663 		VOP_WRITE(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5664 
5665 	VOP_UNLOCK1(vp);
5666 	vn_close(vp, flags, ap->a_cred, td);
5667 	return (error);
5668 }
5669 
5670 static int
zfs_setextattr_sa(struct vop_setextattr_args * ap,const char * attrname)5671 zfs_setextattr_sa(struct vop_setextattr_args *ap, const char *attrname)
5672 {
5673 	znode_t *zp = VTOZ(ap->a_vp);
5674 	nvlist_t *nvl;
5675 	size_t sa_size;
5676 	int error;
5677 
5678 	error = zfs_ensure_xattr_cached(zp);
5679 	if (error != 0)
5680 		return (error);
5681 
5682 	ASSERT(RW_WRITE_HELD(&zp->z_xattr_lock));
5683 	ASSERT3P(zp->z_xattr_cached, !=, NULL);
5684 
5685 	nvl = zp->z_xattr_cached;
5686 	size_t entry_size = ap->a_uio->uio_resid;
5687 	if (entry_size > DXATTR_MAX_ENTRY_SIZE)
5688 		return (SET_ERROR(EFBIG));
5689 	error = nvlist_size(nvl, &sa_size, NV_ENCODE_XDR);
5690 	if (error != 0)
5691 		return (SET_ERROR(error));
5692 	if (sa_size > DXATTR_MAX_SA_SIZE)
5693 		return (SET_ERROR(EFBIG));
5694 	uchar_t *buf = kmem_alloc(entry_size, KM_SLEEP);
5695 	error = uiomove(buf, entry_size, ap->a_uio);
5696 	if (error != 0) {
5697 		error = SET_ERROR(error);
5698 	} else {
5699 		error = nvlist_add_byte_array(nvl, attrname, buf, entry_size);
5700 		if (error != 0)
5701 			error = SET_ERROR(error);
5702 	}
5703 	if (error == 0)
5704 		error = zfs_sa_set_xattr(zp, attrname, buf, entry_size);
5705 	kmem_free(buf, entry_size);
5706 	if (error != 0) {
5707 		zp->z_xattr_cached = NULL;
5708 		nvlist_free(nvl);
5709 	}
5710 	return (error);
5711 }
5712 
5713 static int
zfs_setextattr_impl(struct vop_setextattr_args * ap,boolean_t compat)5714 zfs_setextattr_impl(struct vop_setextattr_args *ap, boolean_t compat)
5715 {
5716 	znode_t *zp = VTOZ(ap->a_vp);
5717 	zfsvfs_t *zfsvfs = ZTOZSB(zp);
5718 	char attrname[EXTATTR_MAXNAMELEN+1];
5719 	int error;
5720 
5721 	error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5722 	    sizeof (attrname), compat);
5723 	if (error != 0)
5724 		return (error);
5725 
5726 	struct vop_deleteextattr_args vda = {
5727 		.a_vp = ap->a_vp,
5728 		.a_attrnamespace = ap->a_attrnamespace,
5729 		.a_name = ap->a_name,
5730 		.a_cred = ap->a_cred,
5731 		.a_td = ap->a_td,
5732 	};
5733 	error = ENOENT;
5734 	if (zfsvfs->z_use_sa && zp->z_is_sa && zfsvfs->z_xattr_sa) {
5735 		error = zfs_setextattr_sa(ap, attrname);
5736 		if (error == 0) {
5737 			/*
5738 			 * Successfully put into SA, we need to clear the one
5739 			 * in dir if present.
5740 			 */
5741 			zfs_deleteextattr_dir(&vda, attrname);
5742 		}
5743 	}
5744 	if (error != 0) {
5745 		error = zfs_setextattr_dir(ap, attrname);
5746 		if (error == 0 && zp->z_is_sa) {
5747 			/*
5748 			 * Successfully put into dir, we need to clear the one
5749 			 * in SA if present.
5750 			 */
5751 			zfs_deleteextattr_sa(&vda, attrname);
5752 		}
5753 	}
5754 	if (error == 0 && ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5755 		/*
5756 		 * Also clear all versions of the alternate compat name.
5757 		 */
5758 		zfs_deleteextattr_impl(&vda, !compat);
5759 	}
5760 	return (error);
5761 }
5762 
5763 /*
5764  * Vnode operation to set a named attribute.
5765  */
5766 static int
zfs_setextattr(struct vop_setextattr_args * ap)5767 zfs_setextattr(struct vop_setextattr_args *ap)
5768 {
5769 	znode_t *zp = VTOZ(ap->a_vp);
5770 	zfsvfs_t *zfsvfs = ZTOZSB(zp);
5771 	int error;
5772 
5773 	/*
5774 	 * If the xattr property is off, refuse the request.
5775 	 */
5776 	if (!(zfsvfs->z_flags & ZSB_XATTR))
5777 		return (SET_ERROR(EOPNOTSUPP));
5778 
5779 	error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5780 	    ap->a_cred, ap->a_td, VWRITE);
5781 	if (error != 0)
5782 		return (SET_ERROR(error));
5783 
5784 	error = zfs_check_attrname(ap->a_name);
5785 	if (error != 0)
5786 		return (error);
5787 
5788 	if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5789 		return (error);
5790 	rw_enter(&zp->z_xattr_lock, RW_WRITER);
5791 
5792 	error = zfs_setextattr_impl(ap, zfs_xattr_compat);
5793 
5794 	rw_exit(&zp->z_xattr_lock);
5795 	zfs_exit(zfsvfs, FTAG);
5796 	return (error);
5797 }
5798 
5799 #ifndef _SYS_SYSPROTO_H_
5800 struct vop_listextattr {
5801 	IN struct vnode *a_vp;
5802 	IN int a_attrnamespace;
5803 	INOUT struct uio *a_uio;
5804 	OUT size_t *a_size;
5805 	IN struct ucred *a_cred;
5806 	IN struct thread *a_td;
5807 };
5808 #endif
5809 
5810 static int
zfs_listextattr_dir(struct vop_listextattr_args * ap,const char * attrprefix)5811 zfs_listextattr_dir(struct vop_listextattr_args *ap, const char *attrprefix)
5812 {
5813 	struct thread *td = ap->a_td;
5814 	struct nameidata nd;
5815 	uint8_t dirbuf[sizeof (struct dirent)];
5816 	struct iovec aiov;
5817 	struct uio auio;
5818 	vnode_t *xvp = NULL, *vp;
5819 	int error, eof;
5820 
5821 	error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5822 	    LOOKUP_XATTR, B_FALSE);
5823 	if (error != 0) {
5824 		/*
5825 		 * ENOATTR means that the EA directory does not yet exist,
5826 		 * i.e. there are no extended attributes there.
5827 		 */
5828 		if (error == ENOATTR)
5829 			error = 0;
5830 		return (error);
5831 	}
5832 
5833 #if __FreeBSD_version < 1400043
5834 	NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
5835 	    UIO_SYSSPACE, ".", xvp, td);
5836 #else
5837 	NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
5838 	    UIO_SYSSPACE, ".", xvp);
5839 #endif
5840 	error = namei(&nd);
5841 	if (error != 0)
5842 		return (SET_ERROR(error));
5843 	vp = nd.ni_vp;
5844 	NDFREE_PNBUF(&nd);
5845 
5846 	auio.uio_iov = &aiov;
5847 	auio.uio_iovcnt = 1;
5848 	auio.uio_segflg = UIO_SYSSPACE;
5849 	auio.uio_td = td;
5850 	auio.uio_rw = UIO_READ;
5851 	auio.uio_offset = 0;
5852 
5853 	size_t plen = strlen(attrprefix);
5854 
5855 	do {
5856 		aiov.iov_base = (void *)dirbuf;
5857 		aiov.iov_len = sizeof (dirbuf);
5858 		auio.uio_resid = sizeof (dirbuf);
5859 		error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
5860 		if (error != 0)
5861 			break;
5862 		int done = sizeof (dirbuf) - auio.uio_resid;
5863 		for (int pos = 0; pos < done; ) {
5864 			struct dirent *dp = (struct dirent *)(dirbuf + pos);
5865 			pos += dp->d_reclen;
5866 			/*
5867 			 * XXX: Temporarily we also accept DT_UNKNOWN, as this
5868 			 * is what we get when attribute was created on Solaris.
5869 			 */
5870 			if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
5871 				continue;
5872 			else if (plen == 0 &&
5873 			    ZFS_XA_NS_PREFIX_FORBIDDEN(dp->d_name))
5874 				continue;
5875 			else if (strncmp(dp->d_name, attrprefix, plen) != 0)
5876 				continue;
5877 			uint8_t nlen = dp->d_namlen - plen;
5878 			if (ap->a_size != NULL) {
5879 				*ap->a_size += 1 + nlen;
5880 			} else if (ap->a_uio != NULL) {
5881 				/*
5882 				 * Format of extattr name entry is one byte for
5883 				 * length and the rest for name.
5884 				 */
5885 				error = uiomove(&nlen, 1, ap->a_uio);
5886 				if (error == 0) {
5887 					char *namep = dp->d_name + plen;
5888 					error = uiomove(namep, nlen, ap->a_uio);
5889 				}
5890 				if (error != 0) {
5891 					error = SET_ERROR(error);
5892 					break;
5893 				}
5894 			}
5895 		}
5896 	} while (!eof && error == 0);
5897 
5898 	vput(vp);
5899 	return (error);
5900 }
5901 
5902 static int
zfs_listextattr_sa(struct vop_listextattr_args * ap,const char * attrprefix)5903 zfs_listextattr_sa(struct vop_listextattr_args *ap, const char *attrprefix)
5904 {
5905 	znode_t *zp = VTOZ(ap->a_vp);
5906 	int error;
5907 
5908 	error = zfs_ensure_xattr_cached(zp);
5909 	if (error != 0)
5910 		return (error);
5911 
5912 	ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
5913 	ASSERT3P(zp->z_xattr_cached, !=, NULL);
5914 
5915 	size_t plen = strlen(attrprefix);
5916 	nvpair_t *nvp = NULL;
5917 	while ((nvp = nvlist_next_nvpair(zp->z_xattr_cached, nvp)) != NULL) {
5918 		ASSERT3U(nvpair_type(nvp), ==, DATA_TYPE_BYTE_ARRAY);
5919 
5920 		const char *name = nvpair_name(nvp);
5921 		if (plen == 0 && ZFS_XA_NS_PREFIX_FORBIDDEN(name))
5922 			continue;
5923 		else if (strncmp(name, attrprefix, plen) != 0)
5924 			continue;
5925 		uint8_t nlen = strlen(name) - plen;
5926 		if (ap->a_size != NULL) {
5927 			*ap->a_size += 1 + nlen;
5928 		} else if (ap->a_uio != NULL) {
5929 			/*
5930 			 * Format of extattr name entry is one byte for
5931 			 * length and the rest for name.
5932 			 */
5933 			error = uiomove(&nlen, 1, ap->a_uio);
5934 			if (error == 0) {
5935 				char *namep = __DECONST(char *, name) + plen;
5936 				error = uiomove(namep, nlen, ap->a_uio);
5937 			}
5938 			if (error != 0) {
5939 				error = SET_ERROR(error);
5940 				break;
5941 			}
5942 		}
5943 	}
5944 
5945 	return (error);
5946 }
5947 
5948 static int
zfs_listextattr_impl(struct vop_listextattr_args * ap,boolean_t compat)5949 zfs_listextattr_impl(struct vop_listextattr_args *ap, boolean_t compat)
5950 {
5951 	znode_t *zp = VTOZ(ap->a_vp);
5952 	zfsvfs_t *zfsvfs = ZTOZSB(zp);
5953 	char attrprefix[16];
5954 	int error;
5955 
5956 	error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
5957 	    sizeof (attrprefix), compat);
5958 	if (error != 0)
5959 		return (error);
5960 
5961 	if (zfsvfs->z_use_sa && zp->z_is_sa)
5962 		error = zfs_listextattr_sa(ap, attrprefix);
5963 	if (error == 0)
5964 		error = zfs_listextattr_dir(ap, attrprefix);
5965 	return (error);
5966 }
5967 
5968 /*
5969  * Vnode operation to retrieve extended attributes on a vnode.
5970  */
5971 static int
zfs_listextattr(struct vop_listextattr_args * ap)5972 zfs_listextattr(struct vop_listextattr_args *ap)
5973 {
5974 	znode_t *zp = VTOZ(ap->a_vp);
5975 	zfsvfs_t *zfsvfs = ZTOZSB(zp);
5976 	int error;
5977 
5978 	if (ap->a_size != NULL)
5979 		*ap->a_size = 0;
5980 
5981 	/*
5982 	 * If the xattr property is off, refuse the request.
5983 	 */
5984 	if (!(zfsvfs->z_flags & ZSB_XATTR))
5985 		return (SET_ERROR(EOPNOTSUPP));
5986 
5987 	error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5988 	    ap->a_cred, ap->a_td, VREAD);
5989 	if (error != 0)
5990 		return (SET_ERROR(error));
5991 
5992 	if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5993 		return (error);
5994 	rw_enter(&zp->z_xattr_lock, RW_READER);
5995 
5996 	error = zfs_listextattr_impl(ap, zfs_xattr_compat);
5997 	if (error == 0 && ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5998 		/* Also list user xattrs with the alternate format. */
5999 		error = zfs_listextattr_impl(ap, !zfs_xattr_compat);
6000 	}
6001 
6002 	rw_exit(&zp->z_xattr_lock);
6003 	zfs_exit(zfsvfs, FTAG);
6004 	return (error);
6005 }
6006 
6007 #ifndef _SYS_SYSPROTO_H_
6008 struct vop_getacl_args {
6009 	struct vnode *vp;
6010 	acl_type_t type;
6011 	struct acl *aclp;
6012 	struct ucred *cred;
6013 	struct thread *td;
6014 };
6015 #endif
6016 
6017 static int
zfs_freebsd_getacl(struct vop_getacl_args * ap)6018 zfs_freebsd_getacl(struct vop_getacl_args *ap)
6019 {
6020 	int		error;
6021 	vsecattr_t	vsecattr;
6022 
6023 	if (ap->a_type != ACL_TYPE_NFS4)
6024 		return (EINVAL);
6025 
6026 	vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
6027 	if ((error = zfs_getsecattr(VTOZ(ap->a_vp),
6028 	    &vsecattr, 0, ap->a_cred)))
6029 		return (error);
6030 
6031 	error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp,
6032 	    vsecattr.vsa_aclcnt);
6033 	if (vsecattr.vsa_aclentp != NULL)
6034 		kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
6035 
6036 	return (error);
6037 }
6038 
6039 #ifndef _SYS_SYSPROTO_H_
6040 struct vop_setacl_args {
6041 	struct vnode *vp;
6042 	acl_type_t type;
6043 	struct acl *aclp;
6044 	struct ucred *cred;
6045 	struct thread *td;
6046 };
6047 #endif
6048 
6049 static int
zfs_freebsd_setacl(struct vop_setacl_args * ap)6050 zfs_freebsd_setacl(struct vop_setacl_args *ap)
6051 {
6052 	int		error;
6053 	vsecattr_t vsecattr;
6054 	int		aclbsize;	/* size of acl list in bytes */
6055 	aclent_t	*aaclp;
6056 
6057 	if (ap->a_type != ACL_TYPE_NFS4)
6058 		return (EINVAL);
6059 
6060 	if (ap->a_aclp == NULL)
6061 		return (EINVAL);
6062 
6063 	if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
6064 		return (EINVAL);
6065 
6066 	/*
6067 	 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
6068 	 * splitting every entry into two and appending "canonical six"
6069 	 * entries at the end.  Don't allow for setting an ACL that would
6070 	 * cause chmod(2) to run out of ACL entries.
6071 	 */
6072 	if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
6073 		return (ENOSPC);
6074 
6075 	error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
6076 	if (error != 0)
6077 		return (error);
6078 
6079 	vsecattr.vsa_mask = VSA_ACE;
6080 	aclbsize = ap->a_aclp->acl_cnt * sizeof (ace_t);
6081 	vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
6082 	aaclp = vsecattr.vsa_aclentp;
6083 	vsecattr.vsa_aclentsz = aclbsize;
6084 
6085 	aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
6086 	error = zfs_setsecattr(VTOZ(ap->a_vp), &vsecattr, 0, ap->a_cred);
6087 	kmem_free(aaclp, aclbsize);
6088 
6089 	return (error);
6090 }
6091 
6092 #ifndef _SYS_SYSPROTO_H_
6093 struct vop_aclcheck_args {
6094 	struct vnode *vp;
6095 	acl_type_t type;
6096 	struct acl *aclp;
6097 	struct ucred *cred;
6098 	struct thread *td;
6099 };
6100 #endif
6101 
6102 static int
zfs_freebsd_aclcheck(struct vop_aclcheck_args * ap)6103 zfs_freebsd_aclcheck(struct vop_aclcheck_args *ap)
6104 {
6105 
6106 	return (EOPNOTSUPP);
6107 }
6108 
6109 static int
zfs_vptocnp(struct vop_vptocnp_args * ap)6110 zfs_vptocnp(struct vop_vptocnp_args *ap)
6111 {
6112 	vnode_t *covered_vp;
6113 	vnode_t *vp = ap->a_vp;
6114 	zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
6115 	znode_t *zp = VTOZ(vp);
6116 	int ltype;
6117 	int error;
6118 
6119 	if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
6120 		return (error);
6121 
6122 	/*
6123 	 * If we are a snapshot mounted under .zfs, run the operation
6124 	 * on the covered vnode.
6125 	 */
6126 	if (zp->z_id != zfsvfs->z_root || zfsvfs->z_parent == zfsvfs) {
6127 		char name[MAXNAMLEN + 1];
6128 		znode_t *dzp;
6129 		size_t len;
6130 
6131 		error = zfs_znode_parent_and_name(zp, &dzp, name);
6132 		if (error == 0) {
6133 			len = strlen(name);
6134 			if (*ap->a_buflen < len)
6135 				error = SET_ERROR(ENOMEM);
6136 		}
6137 		if (error == 0) {
6138 			*ap->a_buflen -= len;
6139 			memcpy(ap->a_buf + *ap->a_buflen, name, len);
6140 			*ap->a_vpp = ZTOV(dzp);
6141 		}
6142 		zfs_exit(zfsvfs, FTAG);
6143 		return (error);
6144 	}
6145 	zfs_exit(zfsvfs, FTAG);
6146 
6147 	covered_vp = vp->v_mount->mnt_vnodecovered;
6148 #if __FreeBSD_version >= 1300045
6149 	enum vgetstate vs = vget_prep(covered_vp);
6150 #else
6151 	vhold(covered_vp);
6152 #endif
6153 	ltype = VOP_ISLOCKED(vp);
6154 	VOP_UNLOCK1(vp);
6155 #if __FreeBSD_version >= 1300045
6156 	error = vget_finish(covered_vp, LK_SHARED, vs);
6157 #else
6158 	error = vget(covered_vp, LK_SHARED | LK_VNHELD, curthread);
6159 #endif
6160 	if (error == 0) {
6161 #if __FreeBSD_version >= 1300123
6162 		error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_buf,
6163 		    ap->a_buflen);
6164 #else
6165 		error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_cred,
6166 		    ap->a_buf, ap->a_buflen);
6167 #endif
6168 		vput(covered_vp);
6169 	}
6170 	vn_lock(vp, ltype | LK_RETRY);
6171 	if (VN_IS_DOOMED(vp))
6172 		error = SET_ERROR(ENOENT);
6173 	return (error);
6174 }
6175 
6176 #if __FreeBSD_version >= 1400032
6177 static int
zfs_deallocate(struct vop_deallocate_args * ap)6178 zfs_deallocate(struct vop_deallocate_args *ap)
6179 {
6180 	znode_t *zp = VTOZ(ap->a_vp);
6181 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
6182 	zilog_t *zilog;
6183 	off_t off, len, file_sz;
6184 	int error;
6185 
6186 	if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
6187 		return (error);
6188 
6189 	/*
6190 	 * Callers might not be able to detect properly that we are read-only,
6191 	 * so check it explicitly here.
6192 	 */
6193 	if (zfs_is_readonly(zfsvfs)) {
6194 		zfs_exit(zfsvfs, FTAG);
6195 		return (SET_ERROR(EROFS));
6196 	}
6197 
6198 	zilog = zfsvfs->z_log;
6199 	off = *ap->a_offset;
6200 	len = *ap->a_len;
6201 	file_sz = zp->z_size;
6202 	if (off + len > file_sz)
6203 		len = file_sz - off;
6204 	/* Fast path for out-of-range request. */
6205 	if (len <= 0) {
6206 		*ap->a_len = 0;
6207 		zfs_exit(zfsvfs, FTAG);
6208 		return (0);
6209 	}
6210 
6211 	error = zfs_freesp(zp, off, len, O_RDWR, TRUE);
6212 	if (error == 0) {
6213 		if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS ||
6214 		    (ap->a_ioflag & IO_SYNC) != 0)
6215 			zil_commit(zilog, zp->z_id);
6216 		*ap->a_offset = off + len;
6217 		*ap->a_len = 0;
6218 	}
6219 
6220 	zfs_exit(zfsvfs, FTAG);
6221 	return (error);
6222 }
6223 #endif
6224 
6225 #if __FreeBSD_version >= 1300039
6226 #ifndef _SYS_SYSPROTO_H_
6227 struct vop_copy_file_range_args {
6228 	struct vnode *a_invp;
6229 	off_t *a_inoffp;
6230 	struct vnode *a_outvp;
6231 	off_t *a_outoffp;
6232 	size_t *a_lenp;
6233 	unsigned int a_flags;
6234 	struct ucred *a_incred;
6235 	struct ucred *a_outcred;
6236 	struct thread *a_fsizetd;
6237 }
6238 #endif
6239 /*
6240  * TODO: FreeBSD will only call file system-specific copy_file_range() if both
6241  * files resides under the same mountpoint. In case of ZFS we want to be called
6242  * even is files are in different datasets (but on the same pools, but we need
6243  * to check that ourselves).
6244  */
6245 static int
zfs_freebsd_copy_file_range(struct vop_copy_file_range_args * ap)6246 zfs_freebsd_copy_file_range(struct vop_copy_file_range_args *ap)
6247 {
6248 	zfsvfs_t *outzfsvfs;
6249 	struct vnode *invp = ap->a_invp;
6250 	struct vnode *outvp = ap->a_outvp;
6251 	struct mount *mp;
6252 	struct uio io;
6253 	int error;
6254 	uint64_t len = *ap->a_lenp;
6255 
6256 	if (!zfs_bclone_enabled) {
6257 		mp = NULL;
6258 		goto bad_write_fallback;
6259 	}
6260 
6261 	/*
6262 	 * TODO: If offset/length is not aligned to recordsize, use
6263 	 * vn_generic_copy_file_range() on this fragment.
6264 	 * It would be better to do this after we lock the vnodes, but then we
6265 	 * need something else than vn_generic_copy_file_range().
6266 	 */
6267 
6268 	vn_start_write(outvp, &mp, V_WAIT);
6269 	if (__predict_true(mp == outvp->v_mount)) {
6270 		outzfsvfs = (zfsvfs_t *)mp->mnt_data;
6271 		if (!spa_feature_is_enabled(dmu_objset_spa(outzfsvfs->z_os),
6272 		    SPA_FEATURE_BLOCK_CLONING)) {
6273 			goto bad_write_fallback;
6274 		}
6275 	}
6276 	if (invp == outvp) {
6277 		if (vn_lock(outvp, LK_EXCLUSIVE) != 0) {
6278 			goto bad_write_fallback;
6279 		}
6280 	} else {
6281 #if (__FreeBSD_version >= 1302506 && __FreeBSD_version < 1400000) || \
6282 	__FreeBSD_version >= 1400086
6283 		vn_lock_pair(invp, false, LK_EXCLUSIVE, outvp, false,
6284 		    LK_EXCLUSIVE);
6285 #else
6286 		vn_lock_pair(invp, false, outvp, false);
6287 #endif
6288 		if (VN_IS_DOOMED(invp) || VN_IS_DOOMED(outvp)) {
6289 			goto bad_locked_fallback;
6290 		}
6291 	}
6292 
6293 #ifdef MAC
6294 	error = mac_vnode_check_write(curthread->td_ucred, ap->a_outcred,
6295 	    outvp);
6296 	if (error != 0)
6297 		goto out_locked;
6298 #endif
6299 
6300 	io.uio_offset = *ap->a_outoffp;
6301 	io.uio_resid = *ap->a_lenp;
6302 	error = vn_rlimit_fsize(outvp, &io, ap->a_fsizetd);
6303 	if (error != 0)
6304 		goto out_locked;
6305 
6306 	error = zfs_clone_range(VTOZ(invp), ap->a_inoffp, VTOZ(outvp),
6307 	    ap->a_outoffp, &len, ap->a_outcred);
6308 	if (error == EXDEV || error == EAGAIN || error == EINVAL ||
6309 	    error == EOPNOTSUPP)
6310 		goto bad_locked_fallback;
6311 	*ap->a_lenp = (size_t)len;
6312 out_locked:
6313 	if (invp != outvp)
6314 		VOP_UNLOCK(invp);
6315 	VOP_UNLOCK(outvp);
6316 	if (mp != NULL)
6317 		vn_finished_write(mp);
6318 	return (error);
6319 
6320 bad_locked_fallback:
6321 	if (invp != outvp)
6322 		VOP_UNLOCK(invp);
6323 	VOP_UNLOCK(outvp);
6324 bad_write_fallback:
6325 	if (mp != NULL)
6326 		vn_finished_write(mp);
6327 	error = ENOSYS;
6328 	return (error);
6329 }
6330 #endif
6331 
6332 struct vop_vector zfs_vnodeops;
6333 struct vop_vector zfs_fifoops;
6334 struct vop_vector zfs_shareops;
6335 
6336 struct vop_vector zfs_vnodeops = {
6337 	.vop_default =		&default_vnodeops,
6338 	.vop_inactive =		zfs_freebsd_inactive,
6339 #if __FreeBSD_version >= 1300042
6340 	.vop_need_inactive =	zfs_freebsd_need_inactive,
6341 #endif
6342 	.vop_reclaim =		zfs_freebsd_reclaim,
6343 #if __FreeBSD_version >= 1300102
6344 	.vop_fplookup_vexec = zfs_freebsd_fplookup_vexec,
6345 #endif
6346 #if __FreeBSD_version >= 1300139
6347 	.vop_fplookup_symlink = zfs_freebsd_fplookup_symlink,
6348 #endif
6349 	.vop_access =		zfs_freebsd_access,
6350 	.vop_allocate =		VOP_EINVAL,
6351 #if __FreeBSD_version >= 1400032
6352 	.vop_deallocate =	zfs_deallocate,
6353 #endif
6354 	.vop_lookup =		zfs_cache_lookup,
6355 	.vop_cachedlookup =	zfs_freebsd_cachedlookup,
6356 	.vop_getattr =		zfs_freebsd_getattr,
6357 	.vop_setattr =		zfs_freebsd_setattr,
6358 	.vop_create =		zfs_freebsd_create,
6359 	.vop_mknod =		(vop_mknod_t *)zfs_freebsd_create,
6360 	.vop_mkdir =		zfs_freebsd_mkdir,
6361 	.vop_readdir =		zfs_freebsd_readdir,
6362 	.vop_fsync =		zfs_freebsd_fsync,
6363 	.vop_open =		zfs_freebsd_open,
6364 	.vop_close =		zfs_freebsd_close,
6365 	.vop_rmdir =		zfs_freebsd_rmdir,
6366 	.vop_ioctl =		zfs_freebsd_ioctl,
6367 	.vop_link =		zfs_freebsd_link,
6368 	.vop_symlink =		zfs_freebsd_symlink,
6369 	.vop_readlink =		zfs_freebsd_readlink,
6370 	.vop_read =		zfs_freebsd_read,
6371 	.vop_write =		zfs_freebsd_write,
6372 	.vop_remove =		zfs_freebsd_remove,
6373 	.vop_rename =		zfs_freebsd_rename,
6374 	.vop_pathconf =		zfs_freebsd_pathconf,
6375 	.vop_bmap =		zfs_freebsd_bmap,
6376 	.vop_fid =		zfs_freebsd_fid,
6377 	.vop_getextattr =	zfs_getextattr,
6378 	.vop_deleteextattr =	zfs_deleteextattr,
6379 	.vop_setextattr =	zfs_setextattr,
6380 	.vop_listextattr =	zfs_listextattr,
6381 	.vop_getacl =		zfs_freebsd_getacl,
6382 	.vop_setacl =		zfs_freebsd_setacl,
6383 	.vop_aclcheck =		zfs_freebsd_aclcheck,
6384 	.vop_getpages =		zfs_freebsd_getpages,
6385 	.vop_putpages =		zfs_freebsd_putpages,
6386 	.vop_vptocnp =		zfs_vptocnp,
6387 #if __FreeBSD_version >= 1300064
6388 	.vop_lock1 =		vop_lock,
6389 	.vop_unlock =		vop_unlock,
6390 	.vop_islocked =		vop_islocked,
6391 #endif
6392 #if __FreeBSD_version >= 1400043
6393 	.vop_add_writecount =	vop_stdadd_writecount_nomsync,
6394 #endif
6395 #if __FreeBSD_version >= 1300039
6396 	.vop_copy_file_range =	zfs_freebsd_copy_file_range,
6397 #endif
6398 };
6399 VFS_VOP_VECTOR_REGISTER(zfs_vnodeops);
6400 
6401 struct vop_vector zfs_fifoops = {
6402 	.vop_default =		&fifo_specops,
6403 	.vop_fsync =		zfs_freebsd_fsync,
6404 #if __FreeBSD_version >= 1300102
6405 	.vop_fplookup_vexec = zfs_freebsd_fplookup_vexec,
6406 #endif
6407 #if __FreeBSD_version >= 1300139
6408 	.vop_fplookup_symlink = zfs_freebsd_fplookup_symlink,
6409 #endif
6410 	.vop_access =		zfs_freebsd_access,
6411 	.vop_getattr =		zfs_freebsd_getattr,
6412 	.vop_inactive =		zfs_freebsd_inactive,
6413 	.vop_read =		VOP_PANIC,
6414 	.vop_reclaim =		zfs_freebsd_reclaim,
6415 	.vop_setattr =		zfs_freebsd_setattr,
6416 	.vop_write =		VOP_PANIC,
6417 	.vop_pathconf = 	zfs_freebsd_pathconf,
6418 	.vop_fid =		zfs_freebsd_fid,
6419 	.vop_getacl =		zfs_freebsd_getacl,
6420 	.vop_setacl =		zfs_freebsd_setacl,
6421 	.vop_aclcheck =		zfs_freebsd_aclcheck,
6422 #if __FreeBSD_version >= 1400043
6423 	.vop_add_writecount =	vop_stdadd_writecount_nomsync,
6424 #endif
6425 };
6426 VFS_VOP_VECTOR_REGISTER(zfs_fifoops);
6427 
6428 /*
6429  * special share hidden files vnode operations template
6430  */
6431 struct vop_vector zfs_shareops = {
6432 	.vop_default =		&default_vnodeops,
6433 #if __FreeBSD_version >= 1300121
6434 	.vop_fplookup_vexec =	VOP_EAGAIN,
6435 #endif
6436 #if __FreeBSD_version >= 1300139
6437 	.vop_fplookup_symlink =	VOP_EAGAIN,
6438 #endif
6439 	.vop_access =		zfs_freebsd_access,
6440 	.vop_inactive =		zfs_freebsd_inactive,
6441 	.vop_reclaim =		zfs_freebsd_reclaim,
6442 	.vop_fid =		zfs_freebsd_fid,
6443 	.vop_pathconf =		zfs_freebsd_pathconf,
6444 #if __FreeBSD_version >= 1400043
6445 	.vop_add_writecount =	vop_stdadd_writecount_nomsync,
6446 #endif
6447 };
6448 VFS_VOP_VECTOR_REGISTER(zfs_shareops);
6449 
6450 ZFS_MODULE_PARAM(zfs, zfs_, xattr_compat, INT, ZMOD_RW,
6451 	"Use legacy ZFS xattr naming for writing new user namespace xattrs");
6452