xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 11935:538c866aaac6)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51460Smarks  * Common Development and Distribution License (the "License").
61460Smarks  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
2211539SChunli.Zhang@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
264144Speteh /* Portions Copyright 2007 Jeremy Teo */
274144Speteh 
28789Sahrens #include <sys/types.h>
29789Sahrens #include <sys/param.h>
30789Sahrens #include <sys/time.h>
31789Sahrens #include <sys/systm.h>
32789Sahrens #include <sys/sysmacros.h>
33789Sahrens #include <sys/resource.h>
34789Sahrens #include <sys/vfs.h>
353898Srsb #include <sys/vfs_opreg.h>
36789Sahrens #include <sys/vnode.h>
37789Sahrens #include <sys/file.h>
38789Sahrens #include <sys/stat.h>
39789Sahrens #include <sys/kmem.h>
40789Sahrens #include <sys/taskq.h>
41789Sahrens #include <sys/uio.h>
42789Sahrens #include <sys/vmsystm.h>
43789Sahrens #include <sys/atomic.h>
442688Smaybee #include <sys/vm.h>
45789Sahrens #include <vm/seg_vn.h>
46789Sahrens #include <vm/pvn.h>
47789Sahrens #include <vm/as.h>
487315SJonathan.Adams@Sun.COM #include <vm/kpm.h>
497315SJonathan.Adams@Sun.COM #include <vm/seg_kpm.h>
50789Sahrens #include <sys/mman.h>
51789Sahrens #include <sys/pathname.h>
52789Sahrens #include <sys/cmn_err.h>
53789Sahrens #include <sys/errno.h>
54789Sahrens #include <sys/unistd.h>
55789Sahrens #include <sys/zfs_dir.h>
56789Sahrens #include <sys/zfs_acl.h>
57789Sahrens #include <sys/zfs_ioctl.h>
58789Sahrens #include <sys/fs/zfs.h>
59789Sahrens #include <sys/dmu.h>
60789Sahrens #include <sys/spa.h>
61789Sahrens #include <sys/txg.h>
62789Sahrens #include <sys/dbuf.h>
63789Sahrens #include <sys/zap.h>
64*11935SMark.Shellenbaum@Sun.COM #include <sys/sa.h>
65789Sahrens #include <sys/dirent.h>
66789Sahrens #include <sys/policy.h>
67789Sahrens #include <sys/sunddi.h>
68789Sahrens #include <sys/filio.h>
697847SMark.Shellenbaum@Sun.COM #include <sys/sid.h>
70789Sahrens #include "fs/fs_subr.h"
71789Sahrens #include <sys/zfs_ctldir.h>
725331Samw #include <sys/zfs_fuid.h>
73*11935SMark.Shellenbaum@Sun.COM #include <sys/zfs_sa.h>
741484Sek110237 #include <sys/dnlc.h>
751669Sperrin #include <sys/zfs_rlock.h>
765331Samw #include <sys/extdirent.h>
775331Samw #include <sys/kidmap.h>
7811134SCasper.Dik@Sun.COM #include <sys/cred.h>
795663Sck153898 #include <sys/attr.h>
80789Sahrens 
81789Sahrens /*
82789Sahrens  * Programming rules.
83789Sahrens  *
84789Sahrens  * Each vnode op performs some logical unit of work.  To do this, the ZPL must
85789Sahrens  * properly lock its in-core state, create a DMU transaction, do the work,
86789Sahrens  * record this work in the intent log (ZIL), commit the DMU transaction,
875331Samw  * and wait for the intent log to commit if it is a synchronous operation.
885331Samw  * Moreover, the vnode ops must work in both normal and log replay context.
89789Sahrens  * The ordering of events is important to avoid deadlocks and references
90789Sahrens  * to freed memory.  The example below illustrates the following Big Rules:
91789Sahrens  *
92789Sahrens  *  (1) A check must be made in each zfs thread for a mounted file system.
935367Sahrens  *	This is done avoiding races using ZFS_ENTER(zfsvfs).
945367Sahrens  *      A ZFS_EXIT(zfsvfs) is needed before all returns.  Any znodes
955367Sahrens  *      must be checked with ZFS_VERIFY_ZP(zp).  Both of these macros
965367Sahrens  *      can return EIO from the calling function.
97789Sahrens  *
98789Sahrens  *  (2)	VN_RELE() should always be the last thing except for zil_commit()
992638Sperrin  *	(if necessary) and ZFS_EXIT(). This is for 3 reasons:
100789Sahrens  *	First, if it's the last reference, the vnode/znode
101789Sahrens  *	can be freed, so the zp may point to freed memory.  Second, the last
102789Sahrens  *	reference will call zfs_zinactive(), which may induce a lot of work --
1031669Sperrin  *	pushing cached pages (which acquires range locks) and syncing out
104789Sahrens  *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
105789Sahrens  *	which could deadlock the system if you were already holding one.
1069321SNeil.Perrin@Sun.COM  *	If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
107789Sahrens  *
1081757Sperrin  *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
1091757Sperrin  *	as they can span dmu_tx_assign() calls.
1101757Sperrin  *
1118227SNeil.Perrin@Sun.COM  *  (4)	Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
112789Sahrens  *	This is critical because we don't want to block while holding locks.
113789Sahrens  *	Note, in particular, that if a lock is sometimes acquired before
114789Sahrens  *	the tx assigns, and sometimes after (e.g. z_lock), then failing to
115789Sahrens  *	use a non-blocking assign can deadlock the system.  The scenario:
116789Sahrens  *
117789Sahrens  *	Thread A has grabbed a lock before calling dmu_tx_assign().
118789Sahrens  *	Thread B is in an already-assigned tx, and blocks for this lock.
119789Sahrens  *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
120789Sahrens  *	forever, because the previous txg can't quiesce until B's tx commits.
121789Sahrens  *
122789Sahrens  *	If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
1232113Sahrens  *	then drop all locks, call dmu_tx_wait(), and try again.
124789Sahrens  *
1251757Sperrin  *  (5)	If the operation succeeded, generate the intent log entry for it
126789Sahrens  *	before dropping locks.  This ensures that the ordering of events
127789Sahrens  *	in the intent log matches the order in which they actually occurred.
1288227SNeil.Perrin@Sun.COM  *      During ZIL replay the zfs_log_* functions will update the sequence
1298227SNeil.Perrin@Sun.COM  *	number to indicate the zil transaction has replayed.
130789Sahrens  *
1311757Sperrin  *  (6)	At the end of each vnode op, the DMU tx must always commit,
132789Sahrens  *	regardless of whether there were any errors.
133789Sahrens  *
1342638Sperrin  *  (7)	After dropping all locks, invoke zil_commit(zilog, seq, foid)
135789Sahrens  *	to ensure that synchronous semantics are provided when necessary.
136789Sahrens  *
137789Sahrens  * In general, this is how things should be ordered in each vnode op:
138789Sahrens  *
139789Sahrens  *	ZFS_ENTER(zfsvfs);		// exit if unmounted
140789Sahrens  * top:
141789Sahrens  *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
142789Sahrens  *	rw_enter(...);			// grab any other locks you need
143789Sahrens  *	tx = dmu_tx_create(...);	// get DMU tx
144789Sahrens  *	dmu_tx_hold_*();		// hold each object you might modify
1458227SNeil.Perrin@Sun.COM  *	error = dmu_tx_assign(tx, TXG_NOWAIT);	// try to assign
146789Sahrens  *	if (error) {
147789Sahrens  *		rw_exit(...);		// drop locks
148789Sahrens  *		zfs_dirent_unlock(dl);	// unlock directory entry
149789Sahrens  *		VN_RELE(...);		// release held vnodes
1508227SNeil.Perrin@Sun.COM  *		if (error == ERESTART) {
1512113Sahrens  *			dmu_tx_wait(tx);
1522113Sahrens  *			dmu_tx_abort(tx);
153789Sahrens  *			goto top;
154789Sahrens  *		}
1552113Sahrens  *		dmu_tx_abort(tx);	// abort DMU tx
156789Sahrens  *		ZFS_EXIT(zfsvfs);	// finished in zfs
157789Sahrens  *		return (error);		// really out of space
158789Sahrens  *	}
159789Sahrens  *	error = do_real_work();		// do whatever this VOP does
160789Sahrens  *	if (error == 0)
1612638Sperrin  *		zfs_log_*(...);		// on success, make ZIL entry
162789Sahrens  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
163789Sahrens  *	rw_exit(...);			// drop locks
164789Sahrens  *	zfs_dirent_unlock(dl);		// unlock directory entry
165789Sahrens  *	VN_RELE(...);			// release held vnodes
1662638Sperrin  *	zil_commit(zilog, seq, foid);	// synchronous when necessary
167789Sahrens  *	ZFS_EXIT(zfsvfs);		// finished in zfs
168789Sahrens  *	return (error);			// done, report error
169789Sahrens  */
1705367Sahrens 
171789Sahrens /* ARGSUSED */
172789Sahrens static int
1735331Samw zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
174789Sahrens {
1753063Sperrin 	znode_t	*zp = VTOZ(*vpp);
1767844SMark.Shellenbaum@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1777844SMark.Shellenbaum@Sun.COM 
1787844SMark.Shellenbaum@Sun.COM 	ZFS_ENTER(zfsvfs);
1797844SMark.Shellenbaum@Sun.COM 	ZFS_VERIFY_ZP(zp);
1803063Sperrin 
181*11935SMark.Shellenbaum@Sun.COM 	if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
1825331Samw 	    ((flag & FAPPEND) == 0)) {
1837844SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
1845331Samw 		return (EPERM);
1855331Samw 	}
1865331Samw 
1875331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
1885331Samw 	    ZTOV(zp)->v_type == VREG &&
189*11935SMark.Shellenbaum@Sun.COM 	    !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
1907844SMark.Shellenbaum@Sun.COM 		if (fs_vscan(*vpp, cr, 0) != 0) {
1917844SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
1925331Samw 			return (EACCES);
1937844SMark.Shellenbaum@Sun.COM 		}
1947844SMark.Shellenbaum@Sun.COM 	}
1955331Samw 
1963063Sperrin 	/* Keep a count of the synchronous opens in the znode */
1973063Sperrin 	if (flag & (FSYNC | FDSYNC))
1983063Sperrin 		atomic_inc_32(&zp->z_sync_cnt);
1995331Samw 
2007844SMark.Shellenbaum@Sun.COM 	ZFS_EXIT(zfsvfs);
201789Sahrens 	return (0);
202789Sahrens }
203789Sahrens 
204789Sahrens /* ARGSUSED */
205789Sahrens static int
2065331Samw zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
2075331Samw     caller_context_t *ct)
208789Sahrens {
2093063Sperrin 	znode_t	*zp = VTOZ(vp);
2107844SMark.Shellenbaum@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2117844SMark.Shellenbaum@Sun.COM 
2129909Schris.kirby@sun.com 	/*
2139909Schris.kirby@sun.com 	 * Clean up any locks held by this process on the vp.
2149909Schris.kirby@sun.com 	 */
2159909Schris.kirby@sun.com 	cleanlocks(vp, ddi_get_pid(), 0);
2169909Schris.kirby@sun.com 	cleanshares(vp, ddi_get_pid());
2179909Schris.kirby@sun.com 
2187844SMark.Shellenbaum@Sun.COM 	ZFS_ENTER(zfsvfs);
2197844SMark.Shellenbaum@Sun.COM 	ZFS_VERIFY_ZP(zp);
2203063Sperrin 
2213063Sperrin 	/* Decrement the synchronous opens in the znode */
2224339Sperrin 	if ((flag & (FSYNC | FDSYNC)) && (count == 1))
2233063Sperrin 		atomic_dec_32(&zp->z_sync_cnt);
2243063Sperrin 
2255331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
2265331Samw 	    ZTOV(zp)->v_type == VREG &&
227*11935SMark.Shellenbaum@Sun.COM 	    !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
2285331Samw 		VERIFY(fs_vscan(vp, cr, 1) == 0);
2295331Samw 
2307844SMark.Shellenbaum@Sun.COM 	ZFS_EXIT(zfsvfs);
231789Sahrens 	return (0);
232789Sahrens }
233789Sahrens 
234789Sahrens /*
235789Sahrens  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
236789Sahrens  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
237789Sahrens  */
238789Sahrens static int
239789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off)
240789Sahrens {
241789Sahrens 	znode_t	*zp = VTOZ(vp);
242789Sahrens 	uint64_t noff = (uint64_t)*off; /* new offset */
243789Sahrens 	uint64_t file_sz;
244789Sahrens 	int error;
245789Sahrens 	boolean_t hole;
246789Sahrens 
247*11935SMark.Shellenbaum@Sun.COM 	file_sz = zp->z_size;
248789Sahrens 	if (noff >= file_sz)  {
249789Sahrens 		return (ENXIO);
250789Sahrens 	}
251789Sahrens 
252789Sahrens 	if (cmd == _FIO_SEEK_HOLE)
253789Sahrens 		hole = B_TRUE;
254789Sahrens 	else
255789Sahrens 		hole = B_FALSE;
256789Sahrens 
257789Sahrens 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
258789Sahrens 
259789Sahrens 	/* end of file? */
260789Sahrens 	if ((error == ESRCH) || (noff > file_sz)) {
261789Sahrens 		/*
262789Sahrens 		 * Handle the virtual hole at the end of file.
263789Sahrens 		 */
264789Sahrens 		if (hole) {
265789Sahrens 			*off = file_sz;
266789Sahrens 			return (0);
267789Sahrens 		}
268789Sahrens 		return (ENXIO);
269789Sahrens 	}
270789Sahrens 
271789Sahrens 	if (noff < *off)
272789Sahrens 		return (error);
273789Sahrens 	*off = noff;
274789Sahrens 	return (error);
275789Sahrens }
276789Sahrens 
277789Sahrens /* ARGSUSED */
278789Sahrens static int
279789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
2805331Samw     int *rvalp, caller_context_t *ct)
281789Sahrens {
282789Sahrens 	offset_t off;
283789Sahrens 	int error;
284789Sahrens 	zfsvfs_t *zfsvfs;
2855326Sek110237 	znode_t *zp;
286789Sahrens 
287789Sahrens 	switch (com) {
2884339Sperrin 	case _FIOFFS:
289789Sahrens 		return (zfs_sync(vp->v_vfsp, 0, cred));
290789Sahrens 
2911544Seschrock 		/*
2921544Seschrock 		 * The following two ioctls are used by bfu.  Faking out,
2931544Seschrock 		 * necessary to avoid bfu errors.
2941544Seschrock 		 */
2954339Sperrin 	case _FIOGDIO:
2964339Sperrin 	case _FIOSDIO:
2971544Seschrock 		return (0);
2981544Seschrock 
2994339Sperrin 	case _FIO_SEEK_DATA:
3004339Sperrin 	case _FIO_SEEK_HOLE:
301789Sahrens 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
302789Sahrens 			return (EFAULT);
303789Sahrens 
3045326Sek110237 		zp = VTOZ(vp);
3055326Sek110237 		zfsvfs = zp->z_zfsvfs;
3065367Sahrens 		ZFS_ENTER(zfsvfs);
3075367Sahrens 		ZFS_VERIFY_ZP(zp);
308789Sahrens 
309789Sahrens 		/* offset parameter is in/out */
310789Sahrens 		error = zfs_holey(vp, com, &off);
311789Sahrens 		ZFS_EXIT(zfsvfs);
312789Sahrens 		if (error)
313789Sahrens 			return (error);
314789Sahrens 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
315789Sahrens 			return (EFAULT);
316789Sahrens 		return (0);
317789Sahrens 	}
318789Sahrens 	return (ENOTTY);
319789Sahrens }
320789Sahrens 
321789Sahrens /*
3227315SJonathan.Adams@Sun.COM  * Utility functions to map and unmap a single physical page.  These
3237315SJonathan.Adams@Sun.COM  * are used to manage the mappable copies of ZFS file data, and therefore
3247315SJonathan.Adams@Sun.COM  * do not update ref/mod bits.
3257315SJonathan.Adams@Sun.COM  */
3267315SJonathan.Adams@Sun.COM caddr_t
3277315SJonathan.Adams@Sun.COM zfs_map_page(page_t *pp, enum seg_rw rw)
3287315SJonathan.Adams@Sun.COM {
3297315SJonathan.Adams@Sun.COM 	if (kpm_enable)
3307315SJonathan.Adams@Sun.COM 		return (hat_kpm_mapin(pp, 0));
3317315SJonathan.Adams@Sun.COM 	ASSERT(rw == S_READ || rw == S_WRITE);
3327315SJonathan.Adams@Sun.COM 	return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0),
3337315SJonathan.Adams@Sun.COM 	    (caddr_t)-1));
3347315SJonathan.Adams@Sun.COM }
3357315SJonathan.Adams@Sun.COM 
3367315SJonathan.Adams@Sun.COM void
3377315SJonathan.Adams@Sun.COM zfs_unmap_page(page_t *pp, caddr_t addr)
3387315SJonathan.Adams@Sun.COM {
3397315SJonathan.Adams@Sun.COM 	if (kpm_enable) {
3407315SJonathan.Adams@Sun.COM 		hat_kpm_mapout(pp, 0, addr);
3417315SJonathan.Adams@Sun.COM 	} else {
3427315SJonathan.Adams@Sun.COM 		ppmapout(addr);
3437315SJonathan.Adams@Sun.COM 	}
3447315SJonathan.Adams@Sun.COM }
3457315SJonathan.Adams@Sun.COM 
3467315SJonathan.Adams@Sun.COM /*
347789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
348789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
349789Sahrens  *
350789Sahrens  * On Write:	If we find a memory mapped page, we write to *both*
351789Sahrens  *		the page and the dmu buffer.
352789Sahrens  */
3538636SMark.Maybee@Sun.COM static void
3548636SMark.Maybee@Sun.COM update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid)
355789Sahrens {
3568636SMark.Maybee@Sun.COM 	int64_t	off;
3578636SMark.Maybee@Sun.COM 
358789Sahrens 	off = start & PAGEOFFSET;
359789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
360789Sahrens 		page_t *pp;
3618636SMark.Maybee@Sun.COM 		uint64_t nbytes = MIN(PAGESIZE - off, len);
3628636SMark.Maybee@Sun.COM 
363789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
364789Sahrens 			caddr_t va;
365789Sahrens 
3667315SJonathan.Adams@Sun.COM 			va = zfs_map_page(pp, S_WRITE);
3679512SNeil.Perrin@Sun.COM 			(void) dmu_read(os, oid, start+off, nbytes, va+off,
3689512SNeil.Perrin@Sun.COM 			    DMU_READ_PREFETCH);
3697315SJonathan.Adams@Sun.COM 			zfs_unmap_page(pp, va);
370789Sahrens 			page_unlock(pp);
371789Sahrens 		}
3728636SMark.Maybee@Sun.COM 		len -= nbytes;
373789Sahrens 		off = 0;
374789Sahrens 	}
375789Sahrens }
376789Sahrens 
377789Sahrens /*
378789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
379789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
380789Sahrens  *
381789Sahrens  * On Read:	We "read" preferentially from memory mapped pages,
382789Sahrens  *		else we default from the dmu buffer.
383789Sahrens  *
384789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
385789Sahrens  *	the file is memory mapped.
386789Sahrens  */
387789Sahrens static int
3883638Sbillm mappedread(vnode_t *vp, int nbytes, uio_t *uio)
389789Sahrens {
3903638Sbillm 	znode_t *zp = VTOZ(vp);
3913638Sbillm 	objset_t *os = zp->z_zfsvfs->z_os;
3923638Sbillm 	int64_t	start, off;
393789Sahrens 	int len = nbytes;
394789Sahrens 	int error = 0;
395789Sahrens 
396789Sahrens 	start = uio->uio_loffset;
397789Sahrens 	off = start & PAGEOFFSET;
398789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
399789Sahrens 		page_t *pp;
4003638Sbillm 		uint64_t bytes = MIN(PAGESIZE - off, len);
4013638Sbillm 
402789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
403789Sahrens 			caddr_t va;
404789Sahrens 
4057315SJonathan.Adams@Sun.COM 			va = zfs_map_page(pp, S_READ);
406789Sahrens 			error = uiomove(va + off, bytes, UIO_READ, uio);
4077315SJonathan.Adams@Sun.COM 			zfs_unmap_page(pp, va);
408789Sahrens 			page_unlock(pp);
409789Sahrens 		} else {
4103638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, bytes);
411789Sahrens 		}
412789Sahrens 		len -= bytes;
413789Sahrens 		off = 0;
414789Sahrens 		if (error)
415789Sahrens 			break;
416789Sahrens 	}
417789Sahrens 	return (error);
418789Sahrens }
419789Sahrens 
4203638Sbillm offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
421789Sahrens 
422789Sahrens /*
423789Sahrens  * Read bytes from specified file into supplied buffer.
424789Sahrens  *
425789Sahrens  *	IN:	vp	- vnode of file to be read from.
426789Sahrens  *		uio	- structure supplying read location, range info,
427789Sahrens  *			  and return buffer.
428789Sahrens  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
429789Sahrens  *		cr	- credentials of caller.
4305331Samw  *		ct	- caller context
431789Sahrens  *
432789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
433789Sahrens  *
434789Sahrens  *	RETURN:	0 if success
435789Sahrens  *		error code if failure
436789Sahrens  *
437789Sahrens  * Side Effects:
438789Sahrens  *	vp - atime updated if byte count > 0
439789Sahrens  */
440789Sahrens /* ARGSUSED */
441789Sahrens static int
442789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
443789Sahrens {
444789Sahrens 	znode_t		*zp = VTOZ(vp);
445789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4465326Sek110237 	objset_t	*os;
4473638Sbillm 	ssize_t		n, nbytes;
4483638Sbillm 	int		error;
4491669Sperrin 	rl_t		*rl;
45011539SChunli.Zhang@Sun.COM 	xuio_t		*xuio = NULL;
451789Sahrens 
4525367Sahrens 	ZFS_ENTER(zfsvfs);
4535367Sahrens 	ZFS_VERIFY_ZP(zp);
4545326Sek110237 	os = zfsvfs->z_os;
455789Sahrens 
456*11935SMark.Shellenbaum@Sun.COM 	if (zp->z_pflags & ZFS_AV_QUARANTINED) {
4575929Smarks 		ZFS_EXIT(zfsvfs);
4585929Smarks 		return (EACCES);
4595929Smarks 	}
4605929Smarks 
461789Sahrens 	/*
462789Sahrens 	 * Validate file offset
463789Sahrens 	 */
464789Sahrens 	if (uio->uio_loffset < (offset_t)0) {
465789Sahrens 		ZFS_EXIT(zfsvfs);
466789Sahrens 		return (EINVAL);
467789Sahrens 	}
468789Sahrens 
469789Sahrens 	/*
470789Sahrens 	 * Fasttrack empty reads
471789Sahrens 	 */
472789Sahrens 	if (uio->uio_resid == 0) {
473789Sahrens 		ZFS_EXIT(zfsvfs);
474789Sahrens 		return (0);
475789Sahrens 	}
476789Sahrens 
477789Sahrens 	/*
4781669Sperrin 	 * Check for mandatory locks
479789Sahrens 	 */
480*11935SMark.Shellenbaum@Sun.COM 	if (MANDMODE(zp->z_mode)) {
481789Sahrens 		if (error = chklock(vp, FREAD,
482789Sahrens 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
483789Sahrens 			ZFS_EXIT(zfsvfs);
484789Sahrens 			return (error);
485789Sahrens 		}
486789Sahrens 	}
487789Sahrens 
488789Sahrens 	/*
489789Sahrens 	 * If we're in FRSYNC mode, sync out this znode before reading it.
490789Sahrens 	 */
4912638Sperrin 	if (ioflag & FRSYNC)
4922638Sperrin 		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
493789Sahrens 
494789Sahrens 	/*
4951669Sperrin 	 * Lock the range against changes.
496789Sahrens 	 */
4971669Sperrin 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
4981669Sperrin 
499789Sahrens 	/*
500789Sahrens 	 * If we are reading past end-of-file we can skip
501789Sahrens 	 * to the end; but we might still need to set atime.
502789Sahrens 	 */
503*11935SMark.Shellenbaum@Sun.COM 	if (uio->uio_loffset >= zp->z_size) {
504789Sahrens 		error = 0;
505789Sahrens 		goto out;
506789Sahrens 	}
507789Sahrens 
508*11935SMark.Shellenbaum@Sun.COM 	ASSERT(uio->uio_loffset < zp->z_size);
509*11935SMark.Shellenbaum@Sun.COM 	n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
5103638Sbillm 
51111539SChunli.Zhang@Sun.COM 	if ((uio->uio_extflg == UIO_XUIO) &&
51211539SChunli.Zhang@Sun.COM 	    (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
51311539SChunli.Zhang@Sun.COM 		int nblk;
51411539SChunli.Zhang@Sun.COM 		int blksz = zp->z_blksz;
51511539SChunli.Zhang@Sun.COM 		uint64_t offset = uio->uio_loffset;
51611539SChunli.Zhang@Sun.COM 
51711539SChunli.Zhang@Sun.COM 		xuio = (xuio_t *)uio;
51811539SChunli.Zhang@Sun.COM 		if ((ISP2(blksz))) {
51911539SChunli.Zhang@Sun.COM 			nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
52011539SChunli.Zhang@Sun.COM 			    blksz)) / blksz;
52111539SChunli.Zhang@Sun.COM 		} else {
52211539SChunli.Zhang@Sun.COM 			ASSERT(offset + n <= blksz);
52311539SChunli.Zhang@Sun.COM 			nblk = 1;
52411539SChunli.Zhang@Sun.COM 		}
52511576SSurya.Prakki@Sun.COM 		(void) dmu_xuio_init(xuio, nblk);
52611539SChunli.Zhang@Sun.COM 
52711539SChunli.Zhang@Sun.COM 		if (vn_has_cached_data(vp)) {
52811539SChunli.Zhang@Sun.COM 			/*
52911539SChunli.Zhang@Sun.COM 			 * For simplicity, we always allocate a full buffer
53011539SChunli.Zhang@Sun.COM 			 * even if we only expect to read a portion of a block.
53111539SChunli.Zhang@Sun.COM 			 */
53211539SChunli.Zhang@Sun.COM 			while (--nblk >= 0) {
53311576SSurya.Prakki@Sun.COM 				(void) dmu_xuio_add(xuio,
534*11935SMark.Shellenbaum@Sun.COM 				    dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
535*11935SMark.Shellenbaum@Sun.COM 				    blksz), 0, blksz);
53611539SChunli.Zhang@Sun.COM 			}
53711539SChunli.Zhang@Sun.COM 		}
53811539SChunli.Zhang@Sun.COM 	}
53911539SChunli.Zhang@Sun.COM 
5403638Sbillm 	while (n > 0) {
5413638Sbillm 		nbytes = MIN(n, zfs_read_chunk_size -
5423638Sbillm 		    P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
5433638Sbillm 
5443638Sbillm 		if (vn_has_cached_data(vp))
5453638Sbillm 			error = mappedread(vp, nbytes, uio);
5463638Sbillm 		else
5473638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, nbytes);
5487294Sperrin 		if (error) {
5497294Sperrin 			/* convert checksum errors into IO errors */
5507294Sperrin 			if (error == ECKSUM)
5517294Sperrin 				error = EIO;
5523638Sbillm 			break;
5537294Sperrin 		}
5543638Sbillm 
5553638Sbillm 		n -= nbytes;
556789Sahrens 	}
557789Sahrens out:
5582237Smaybee 	zfs_range_unlock(rl);
559789Sahrens 
560789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
561789Sahrens 	ZFS_EXIT(zfsvfs);
562789Sahrens 	return (error);
563789Sahrens }
564789Sahrens 
565789Sahrens /*
566789Sahrens  * Write the bytes to a file.
567789Sahrens  *
568789Sahrens  *	IN:	vp	- vnode of file to be written to.
569789Sahrens  *		uio	- structure supplying write location, range info,
570789Sahrens  *			  and data buffer.
571789Sahrens  *		ioflag	- FAPPEND flag set if in append mode.
572789Sahrens  *		cr	- credentials of caller.
5735331Samw  *		ct	- caller context (NFS/CIFS fem monitor only)
574789Sahrens  *
575789Sahrens  *	OUT:	uio	- updated offset and range.
576789Sahrens  *
577789Sahrens  *	RETURN:	0 if success
578789Sahrens  *		error code if failure
579789Sahrens  *
580789Sahrens  * Timestamps:
581789Sahrens  *	vp - ctime|mtime updated if byte count > 0
582789Sahrens  */
583*11935SMark.Shellenbaum@Sun.COM 
584789Sahrens /* ARGSUSED */
585789Sahrens static int
586789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
587789Sahrens {
588789Sahrens 	znode_t		*zp = VTOZ(vp);
589789Sahrens 	rlim64_t	limit = uio->uio_llimit;
590789Sahrens 	ssize_t		start_resid = uio->uio_resid;
591789Sahrens 	ssize_t		tx_bytes;
592789Sahrens 	uint64_t	end_size;
593789Sahrens 	dmu_tx_t	*tx;
594789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
5955326Sek110237 	zilog_t		*zilog;
596789Sahrens 	offset_t	woff;
597789Sahrens 	ssize_t		n, nbytes;
5981669Sperrin 	rl_t		*rl;
599789Sahrens 	int		max_blksz = zfsvfs->z_max_blksz;
6001669Sperrin 	int		error;
6019412SAleksandr.Guzovskiy@Sun.COM 	arc_buf_t	*abuf;
60211539SChunli.Zhang@Sun.COM 	iovec_t		*aiov;
60311539SChunli.Zhang@Sun.COM 	xuio_t		*xuio = NULL;
60411539SChunli.Zhang@Sun.COM 	int		i_iov = 0;
60511539SChunli.Zhang@Sun.COM 	int		iovcnt = uio->uio_iovcnt;
60611539SChunli.Zhang@Sun.COM 	iovec_t		*iovp = uio->uio_iov;
60711539SChunli.Zhang@Sun.COM 	int		write_eof;
608*11935SMark.Shellenbaum@Sun.COM 	int		count = 0;
609*11935SMark.Shellenbaum@Sun.COM 	sa_bulk_attr_t	bulk[4];
610*11935SMark.Shellenbaum@Sun.COM 	uint64_t	mtime[2], ctime[2];
611789Sahrens 
612789Sahrens 	/*
613789Sahrens 	 * Fasttrack empty write
614789Sahrens 	 */
6151669Sperrin 	n = start_resid;
616789Sahrens 	if (n == 0)
617789Sahrens 		return (0);
618789Sahrens 
6191669Sperrin 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
6201669Sperrin 		limit = MAXOFFSET_T;
6211669Sperrin 
6225367Sahrens 	ZFS_ENTER(zfsvfs);
6235367Sahrens 	ZFS_VERIFY_ZP(zp);
6246743Smarks 
625*11935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
626*11935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
627*11935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
628*11935SMark.Shellenbaum@Sun.COM 	    &zp->z_size, 8);
629*11935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
630*11935SMark.Shellenbaum@Sun.COM 	    &zp->z_pflags, 8);
631*11935SMark.Shellenbaum@Sun.COM 
6326743Smarks 	/*
6336743Smarks 	 * If immutable or not appending then return EPERM
6346743Smarks 	 */
635*11935SMark.Shellenbaum@Sun.COM 	if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
636*11935SMark.Shellenbaum@Sun.COM 	    ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
637*11935SMark.Shellenbaum@Sun.COM 	    (uio->uio_loffset < zp->z_size))) {
6386743Smarks 		ZFS_EXIT(zfsvfs);
6396743Smarks 		return (EPERM);
6406743Smarks 	}
6416743Smarks 
6425326Sek110237 	zilog = zfsvfs->z_log;
643789Sahrens 
644789Sahrens 	/*
64511083Swilliam.gorrell@sun.com 	 * Validate file offset
64611083Swilliam.gorrell@sun.com 	 */
647*11935SMark.Shellenbaum@Sun.COM 	woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
64811083Swilliam.gorrell@sun.com 	if (woff < 0) {
64911083Swilliam.gorrell@sun.com 		ZFS_EXIT(zfsvfs);
65011083Swilliam.gorrell@sun.com 		return (EINVAL);
65111083Swilliam.gorrell@sun.com 	}
65211083Swilliam.gorrell@sun.com 
65311083Swilliam.gorrell@sun.com 	/*
65411083Swilliam.gorrell@sun.com 	 * Check for mandatory locks before calling zfs_range_lock()
65511083Swilliam.gorrell@sun.com 	 * in order to prevent a deadlock with locks set via fcntl().
65611083Swilliam.gorrell@sun.com 	 */
657*11935SMark.Shellenbaum@Sun.COM 	if (MANDMODE((mode_t)zp->z_mode) &&
65811083Swilliam.gorrell@sun.com 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
65911083Swilliam.gorrell@sun.com 		ZFS_EXIT(zfsvfs);
66011083Swilliam.gorrell@sun.com 		return (error);
66111083Swilliam.gorrell@sun.com 	}
66211083Swilliam.gorrell@sun.com 
66311083Swilliam.gorrell@sun.com 	/*
6642237Smaybee 	 * Pre-fault the pages to ensure slow (eg NFS) pages
6651669Sperrin 	 * don't hold up txg.
66611539SChunli.Zhang@Sun.COM 	 * Skip this if uio contains loaned arc_buf.
667789Sahrens 	 */
66811539SChunli.Zhang@Sun.COM 	if ((uio->uio_extflg == UIO_XUIO) &&
66911539SChunli.Zhang@Sun.COM 	    (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
67011539SChunli.Zhang@Sun.COM 		xuio = (xuio_t *)uio;
67111539SChunli.Zhang@Sun.COM 	else
67211539SChunli.Zhang@Sun.COM 		uio_prefaultpages(n, uio);
673789Sahrens 
674789Sahrens 	/*
675789Sahrens 	 * If in append mode, set the io offset pointer to eof.
676789Sahrens 	 */
6771669Sperrin 	if (ioflag & FAPPEND) {
6781669Sperrin 		/*
67911083Swilliam.gorrell@sun.com 		 * Obtain an appending range lock to guarantee file append
68011083Swilliam.gorrell@sun.com 		 * semantics.  We reset the write offset once we have the lock.
6811669Sperrin 		 */
6821669Sperrin 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
68311083Swilliam.gorrell@sun.com 		woff = rl->r_off;
6841669Sperrin 		if (rl->r_len == UINT64_MAX) {
68511083Swilliam.gorrell@sun.com 			/*
68611083Swilliam.gorrell@sun.com 			 * We overlocked the file because this write will cause
68711083Swilliam.gorrell@sun.com 			 * the file block size to increase.
68811083Swilliam.gorrell@sun.com 			 * Note that zp_size cannot change with this lock held.
68911083Swilliam.gorrell@sun.com 			 */
690*11935SMark.Shellenbaum@Sun.COM 			woff = zp->z_size;
6911669Sperrin 		}
69211083Swilliam.gorrell@sun.com 		uio->uio_loffset = woff;
693789Sahrens 	} else {
694789Sahrens 		/*
69511083Swilliam.gorrell@sun.com 		 * Note that if the file block size will change as a result of
69611083Swilliam.gorrell@sun.com 		 * this write, then this range lock will lock the entire file
69711083Swilliam.gorrell@sun.com 		 * so that we can re-write the block safely.
698789Sahrens 		 */
6991669Sperrin 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
700789Sahrens 	}
701789Sahrens 
702789Sahrens 	if (woff >= limit) {
7033638Sbillm 		zfs_range_unlock(rl);
7043638Sbillm 		ZFS_EXIT(zfsvfs);
7053638Sbillm 		return (EFBIG);
706789Sahrens 	}
707789Sahrens 
708789Sahrens 	if ((woff + n) > limit || woff > (limit - n))
709789Sahrens 		n = limit - woff;
710789Sahrens 
71111539SChunli.Zhang@Sun.COM 	/* Will this write extend the file length? */
712*11935SMark.Shellenbaum@Sun.COM 	write_eof = (woff + n > zp->z_size);
713*11935SMark.Shellenbaum@Sun.COM 
714*11935SMark.Shellenbaum@Sun.COM 	end_size = MAX(zp->z_size, woff + n);
715789Sahrens 
7161669Sperrin 	/*
7173638Sbillm 	 * Write the file in reasonable size chunks.  Each chunk is written
7183638Sbillm 	 * in a separate transaction; this keeps the intent log records small
7193638Sbillm 	 * and allows us to do more fine-grained space accounting.
720789Sahrens 	 */
721789Sahrens 	while (n > 0) {
7229412SAleksandr.Guzovskiy@Sun.COM 		abuf = NULL;
7239412SAleksandr.Guzovskiy@Sun.COM 		woff = uio->uio_loffset;
7249412SAleksandr.Guzovskiy@Sun.COM again:
725*11935SMark.Shellenbaum@Sun.COM 		if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
726*11935SMark.Shellenbaum@Sun.COM 		    zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
7279412SAleksandr.Guzovskiy@Sun.COM 			if (abuf != NULL)
7289412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7299396SMatthew.Ahrens@Sun.COM 			error = EDQUOT;
7309396SMatthew.Ahrens@Sun.COM 			break;
7319396SMatthew.Ahrens@Sun.COM 		}
7329412SAleksandr.Guzovskiy@Sun.COM 
73311539SChunli.Zhang@Sun.COM 		if (xuio && abuf == NULL) {
73411539SChunli.Zhang@Sun.COM 			ASSERT(i_iov < iovcnt);
73511539SChunli.Zhang@Sun.COM 			aiov = &iovp[i_iov];
73611539SChunli.Zhang@Sun.COM 			abuf = dmu_xuio_arcbuf(xuio, i_iov);
73711539SChunli.Zhang@Sun.COM 			dmu_xuio_clear(xuio, i_iov);
73811539SChunli.Zhang@Sun.COM 			DTRACE_PROBE3(zfs_cp_write, int, i_iov,
73911539SChunli.Zhang@Sun.COM 			    iovec_t *, aiov, arc_buf_t *, abuf);
74011539SChunli.Zhang@Sun.COM 			ASSERT((aiov->iov_base == abuf->b_data) ||
74111539SChunli.Zhang@Sun.COM 			    ((char *)aiov->iov_base - (char *)abuf->b_data +
74211539SChunli.Zhang@Sun.COM 			    aiov->iov_len == arc_buf_size(abuf)));
74311539SChunli.Zhang@Sun.COM 			i_iov++;
74411539SChunli.Zhang@Sun.COM 		} else if (abuf == NULL && n >= max_blksz &&
745*11935SMark.Shellenbaum@Sun.COM 		    woff >= zp->z_size &&
7469412SAleksandr.Guzovskiy@Sun.COM 		    P2PHASE(woff, max_blksz) == 0 &&
7479412SAleksandr.Guzovskiy@Sun.COM 		    zp->z_blksz == max_blksz) {
74811539SChunli.Zhang@Sun.COM 			/*
74911539SChunli.Zhang@Sun.COM 			 * This write covers a full block.  "Borrow" a buffer
75011539SChunli.Zhang@Sun.COM 			 * from the dmu so that we can fill it before we enter
75111539SChunli.Zhang@Sun.COM 			 * a transaction.  This avoids the possibility of
75211539SChunli.Zhang@Sun.COM 			 * holding up the transaction if the data copy hangs
75311539SChunli.Zhang@Sun.COM 			 * up on a pagefault (e.g., from an NFS server mapping).
75411539SChunli.Zhang@Sun.COM 			 */
7559412SAleksandr.Guzovskiy@Sun.COM 			size_t cbytes;
7569412SAleksandr.Guzovskiy@Sun.COM 
757*11935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
758*11935SMark.Shellenbaum@Sun.COM 			    max_blksz);
7599412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(abuf != NULL);
7609412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(arc_buf_size(abuf) == max_blksz);
7619412SAleksandr.Guzovskiy@Sun.COM 			if (error = uiocopy(abuf->b_data, max_blksz,
7629412SAleksandr.Guzovskiy@Sun.COM 			    UIO_WRITE, uio, &cbytes)) {
7639412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7649412SAleksandr.Guzovskiy@Sun.COM 				break;
7659412SAleksandr.Guzovskiy@Sun.COM 			}
7669412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(cbytes == max_blksz);
7679412SAleksandr.Guzovskiy@Sun.COM 		}
7689412SAleksandr.Guzovskiy@Sun.COM 
7699412SAleksandr.Guzovskiy@Sun.COM 		/*
7709412SAleksandr.Guzovskiy@Sun.COM 		 * Start a transaction.
7719412SAleksandr.Guzovskiy@Sun.COM 		 */
772789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
773*11935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
774789Sahrens 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
775*11935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, zp);
7768227SNeil.Perrin@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
777789Sahrens 		if (error) {
7788227SNeil.Perrin@Sun.COM 			if (error == ERESTART) {
7792113Sahrens 				dmu_tx_wait(tx);
7802113Sahrens 				dmu_tx_abort(tx);
7819412SAleksandr.Guzovskiy@Sun.COM 				goto again;
782789Sahrens 			}
7832113Sahrens 			dmu_tx_abort(tx);
7849412SAleksandr.Guzovskiy@Sun.COM 			if (abuf != NULL)
7859412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7863638Sbillm 			break;
7873638Sbillm 		}
7883638Sbillm 
7893638Sbillm 		/*
7903638Sbillm 		 * If zfs_range_lock() over-locked we grow the blocksize
7913638Sbillm 		 * and then reduce the lock range.  This will only happen
7923638Sbillm 		 * on the first iteration since zfs_range_reduce() will
7933638Sbillm 		 * shrink down r_len to the appropriate size.
7943638Sbillm 		 */
7953638Sbillm 		if (rl->r_len == UINT64_MAX) {
7963638Sbillm 			uint64_t new_blksz;
7973638Sbillm 
7983638Sbillm 			if (zp->z_blksz > max_blksz) {
7993638Sbillm 				ASSERT(!ISP2(zp->z_blksz));
8003638Sbillm 				new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
8013638Sbillm 			} else {
8023638Sbillm 				new_blksz = MIN(end_size, max_blksz);
8033638Sbillm 			}
8043638Sbillm 			zfs_grow_blocksize(zp, new_blksz, tx);
8053638Sbillm 			zfs_range_reduce(rl, woff, n);
8063638Sbillm 		}
8073638Sbillm 
8083638Sbillm 		/*
8093638Sbillm 		 * XXX - should we really limit each write to z_max_blksz?
8103638Sbillm 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
8113638Sbillm 		 */
8123638Sbillm 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
8133638Sbillm 
8149412SAleksandr.Guzovskiy@Sun.COM 		if (abuf == NULL) {
8159412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes = uio->uio_resid;
8169412SAleksandr.Guzovskiy@Sun.COM 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id, uio,
8179412SAleksandr.Guzovskiy@Sun.COM 			    nbytes, tx);
8189412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes -= uio->uio_resid;
8199412SAleksandr.Guzovskiy@Sun.COM 		} else {
8209412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes = nbytes;
82111539SChunli.Zhang@Sun.COM 			ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
82211539SChunli.Zhang@Sun.COM 			/*
82311539SChunli.Zhang@Sun.COM 			 * If this is not a full block write, but we are
82411539SChunli.Zhang@Sun.COM 			 * extending the file past EOF and this data starts
82511539SChunli.Zhang@Sun.COM 			 * block-aligned, use assign_arcbuf().  Otherwise,
82611539SChunli.Zhang@Sun.COM 			 * write via dmu_write().
82711539SChunli.Zhang@Sun.COM 			 */
82811539SChunli.Zhang@Sun.COM 			if (tx_bytes < max_blksz && (!write_eof ||
82911539SChunli.Zhang@Sun.COM 			    aiov->iov_base != abuf->b_data)) {
83011539SChunli.Zhang@Sun.COM 				ASSERT(xuio);
83111539SChunli.Zhang@Sun.COM 				dmu_write(zfsvfs->z_os, zp->z_id, woff,
83211539SChunli.Zhang@Sun.COM 				    aiov->iov_len, aiov->iov_base, tx);
83311539SChunli.Zhang@Sun.COM 				dmu_return_arcbuf(abuf);
83411539SChunli.Zhang@Sun.COM 				xuio_stat_wbuf_copied();
83511539SChunli.Zhang@Sun.COM 			} else {
83611539SChunli.Zhang@Sun.COM 				ASSERT(xuio || tx_bytes == max_blksz);
837*11935SMark.Shellenbaum@Sun.COM 				dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
838*11935SMark.Shellenbaum@Sun.COM 				    woff, abuf, tx);
83911539SChunli.Zhang@Sun.COM 			}
8409412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(tx_bytes <= uio->uio_resid);
8419412SAleksandr.Guzovskiy@Sun.COM 			uioskip(uio, tx_bytes);
8429412SAleksandr.Guzovskiy@Sun.COM 		}
8439412SAleksandr.Guzovskiy@Sun.COM 		if (tx_bytes && vn_has_cached_data(vp)) {
8448636SMark.Maybee@Sun.COM 			update_pages(vp, woff,
8458636SMark.Maybee@Sun.COM 			    tx_bytes, zfsvfs->z_os, zp->z_id);
8469412SAleksandr.Guzovskiy@Sun.COM 		}
8473638Sbillm 
8483638Sbillm 		/*
8493638Sbillm 		 * If we made no progress, we're done.  If we made even
8503638Sbillm 		 * partial progress, update the znode and ZIL accordingly.
8513638Sbillm 		 */
8523638Sbillm 		if (tx_bytes == 0) {
853*11935SMark.Shellenbaum@Sun.COM 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
854*11935SMark.Shellenbaum@Sun.COM 			    (void *)&zp->z_size, sizeof (uint64_t), tx);
8553897Smaybee 			dmu_tx_commit(tx);
8563638Sbillm 			ASSERT(error != 0);
8573638Sbillm 			break;
8583638Sbillm 		}
8593638Sbillm 
860789Sahrens 		/*
8613638Sbillm 		 * Clear Set-UID/Set-GID bits on successful write if not
8623638Sbillm 		 * privileged and at least one of the excute bits is set.
8633638Sbillm 		 *
8643638Sbillm 		 * It would be nice to to this after all writes have
8653638Sbillm 		 * been done, but that would still expose the ISUID/ISGID
8663638Sbillm 		 * to another app after the partial write is committed.
8675331Samw 		 *
868789Sahrens 		 */
8693638Sbillm 		mutex_enter(&zp->z_acl_lock);
870*11935SMark.Shellenbaum@Sun.COM 		if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
8713638Sbillm 		    (S_IXUSR >> 6))) != 0 &&
872*11935SMark.Shellenbaum@Sun.COM 		    (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
8733638Sbillm 		    secpolicy_vnode_setid_retain(cr,
874*11935SMark.Shellenbaum@Sun.COM 		    (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
875*11935SMark.Shellenbaum@Sun.COM 			uint64_t newmode;
876*11935SMark.Shellenbaum@Sun.COM 			zp->z_mode &= ~(S_ISUID | S_ISGID);
877*11935SMark.Shellenbaum@Sun.COM 			newmode = zp->z_mode;
878*11935SMark.Shellenbaum@Sun.COM 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
879*11935SMark.Shellenbaum@Sun.COM 			    (void *)&newmode, sizeof (uint64_t), tx);
8803638Sbillm 		}
8813638Sbillm 		mutex_exit(&zp->z_acl_lock);
8823638Sbillm 
883*11935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
884*11935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
8853638Sbillm 
8863638Sbillm 		/*
8873638Sbillm 		 * Update the file size (zp_size) if it has changed;
8883638Sbillm 		 * account for possible concurrent updates.
8893638Sbillm 		 */
890*11935SMark.Shellenbaum@Sun.COM 		while ((end_size = zp->z_size) < uio->uio_loffset) {
891*11935SMark.Shellenbaum@Sun.COM 			(void) atomic_cas_64(&zp->z_size, end_size,
892789Sahrens 			    uio->uio_loffset);
893*11935SMark.Shellenbaum@Sun.COM 			ASSERT(error == 0);
894*11935SMark.Shellenbaum@Sun.COM 		}
895*11935SMark.Shellenbaum@Sun.COM 		error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
896*11935SMark.Shellenbaum@Sun.COM 
8973638Sbillm 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
8983638Sbillm 		dmu_tx_commit(tx);
8993638Sbillm 
9003638Sbillm 		if (error != 0)
9013638Sbillm 			break;
9023638Sbillm 		ASSERT(tx_bytes == nbytes);
9033638Sbillm 		n -= nbytes;
904789Sahrens 	}
905789Sahrens 
9062237Smaybee 	zfs_range_unlock(rl);
907789Sahrens 
908789Sahrens 	/*
909789Sahrens 	 * If we're in replay mode, or we made no progress, return error.
910789Sahrens 	 * Otherwise, it's at least a partial write, so it's successful.
911789Sahrens 	 */
9128227SNeil.Perrin@Sun.COM 	if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
913789Sahrens 		ZFS_EXIT(zfsvfs);
914789Sahrens 		return (error);
915789Sahrens 	}
916789Sahrens 
9172638Sperrin 	if (ioflag & (FSYNC | FDSYNC))
9182638Sperrin 		zil_commit(zilog, zp->z_last_itx, zp->z_id);
919789Sahrens 
920789Sahrens 	ZFS_EXIT(zfsvfs);
921789Sahrens 	return (0);
922789Sahrens }
923789Sahrens 
9242237Smaybee void
92510922SJeff.Bonwick@Sun.COM zfs_get_done(zgd_t *zgd, int error)
9262237Smaybee {
92710922SJeff.Bonwick@Sun.COM 	znode_t *zp = zgd->zgd_private;
92810922SJeff.Bonwick@Sun.COM 	objset_t *os = zp->z_zfsvfs->z_os;
92910922SJeff.Bonwick@Sun.COM 
93010922SJeff.Bonwick@Sun.COM 	if (zgd->zgd_db)
93110922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(zgd->zgd_db, zgd);
93210922SJeff.Bonwick@Sun.COM 
93310922SJeff.Bonwick@Sun.COM 	zfs_range_unlock(zgd->zgd_rl);
93410922SJeff.Bonwick@Sun.COM 
9359321SNeil.Perrin@Sun.COM 	/*
9369321SNeil.Perrin@Sun.COM 	 * Release the vnode asynchronously as we currently have the
9379321SNeil.Perrin@Sun.COM 	 * txg stopped from syncing.
9389321SNeil.Perrin@Sun.COM 	 */
93910922SJeff.Bonwick@Sun.COM 	VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
94010922SJeff.Bonwick@Sun.COM 
94110922SJeff.Bonwick@Sun.COM 	if (error == 0 && zgd->zgd_bp)
94210922SJeff.Bonwick@Sun.COM 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
94310922SJeff.Bonwick@Sun.COM 
9443063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
9452237Smaybee }
9462237Smaybee 
94710209SMark.Musante@Sun.COM #ifdef DEBUG
94810209SMark.Musante@Sun.COM static int zil_fault_io = 0;
94910209SMark.Musante@Sun.COM #endif
95010209SMark.Musante@Sun.COM 
951789Sahrens /*
952789Sahrens  * Get data to generate a TX_WRITE intent log record.
953789Sahrens  */
954789Sahrens int
9552237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
956789Sahrens {
957789Sahrens 	zfsvfs_t *zfsvfs = arg;
958789Sahrens 	objset_t *os = zfsvfs->z_os;
959789Sahrens 	znode_t *zp;
96010922SJeff.Bonwick@Sun.COM 	uint64_t object = lr->lr_foid;
96110922SJeff.Bonwick@Sun.COM 	uint64_t offset = lr->lr_offset;
96210922SJeff.Bonwick@Sun.COM 	uint64_t size = lr->lr_length;
96310922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = &lr->lr_blkptr;
9642237Smaybee 	dmu_buf_t *db;
9653063Sperrin 	zgd_t *zgd;
966789Sahrens 	int error = 0;
967789Sahrens 
96810922SJeff.Bonwick@Sun.COM 	ASSERT(zio != NULL);
96910922SJeff.Bonwick@Sun.COM 	ASSERT(size != 0);
970789Sahrens 
971789Sahrens 	/*
9721669Sperrin 	 * Nothing to do if the file has been removed
973789Sahrens 	 */
97410922SJeff.Bonwick@Sun.COM 	if (zfs_zget(zfsvfs, object, &zp) != 0)
975789Sahrens 		return (ENOENT);
9763461Sahrens 	if (zp->z_unlinked) {
9779321SNeil.Perrin@Sun.COM 		/*
9789321SNeil.Perrin@Sun.COM 		 * Release the vnode asynchronously as we currently have the
9799321SNeil.Perrin@Sun.COM 		 * txg stopped from syncing.
9809321SNeil.Perrin@Sun.COM 		 */
9819321SNeil.Perrin@Sun.COM 		VN_RELE_ASYNC(ZTOV(zp),
9829321SNeil.Perrin@Sun.COM 		    dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
983789Sahrens 		return (ENOENT);
984789Sahrens 	}
985789Sahrens 
98610922SJeff.Bonwick@Sun.COM 	zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
98710922SJeff.Bonwick@Sun.COM 	zgd->zgd_zilog = zfsvfs->z_log;
98810922SJeff.Bonwick@Sun.COM 	zgd->zgd_private = zp;
98910922SJeff.Bonwick@Sun.COM 
990789Sahrens 	/*
991789Sahrens 	 * Write records come in two flavors: immediate and indirect.
992789Sahrens 	 * For small writes it's cheaper to store the data with the
993789Sahrens 	 * log record (immediate); for large writes it's cheaper to
994789Sahrens 	 * sync the data and get a pointer to it (indirect) so that
995789Sahrens 	 * we don't have to write the data twice.
996789Sahrens 	 */
9971669Sperrin 	if (buf != NULL) { /* immediate write */
99810922SJeff.Bonwick@Sun.COM 		zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
9991669Sperrin 		/* test for truncation needs to be done while range locked */
1000*11935SMark.Shellenbaum@Sun.COM 		if (offset >= zp->z_size) {
10011669Sperrin 			error = ENOENT;
100210922SJeff.Bonwick@Sun.COM 		} else {
100310922SJeff.Bonwick@Sun.COM 			error = dmu_read(os, object, offset, size, buf,
100410922SJeff.Bonwick@Sun.COM 			    DMU_READ_NO_PREFETCH);
10051669Sperrin 		}
100610922SJeff.Bonwick@Sun.COM 		ASSERT(error == 0 || error == ENOENT);
10071669Sperrin 	} else { /* indirect write */
1008789Sahrens 		/*
10091669Sperrin 		 * Have to lock the whole block to ensure when it's
10101669Sperrin 		 * written out and it's checksum is being calculated
10111669Sperrin 		 * that no one can change the data. We need to re-check
10121669Sperrin 		 * blocksize after we get the lock in case it's changed!
1013789Sahrens 		 */
10141669Sperrin 		for (;;) {
101510922SJeff.Bonwick@Sun.COM 			uint64_t blkoff;
101610922SJeff.Bonwick@Sun.COM 			size = zp->z_blksz;
101710945SJeff.Bonwick@Sun.COM 			blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
101810922SJeff.Bonwick@Sun.COM 			offset -= blkoff;
101910922SJeff.Bonwick@Sun.COM 			zgd->zgd_rl = zfs_range_lock(zp, offset, size,
102010922SJeff.Bonwick@Sun.COM 			    RL_READER);
102110922SJeff.Bonwick@Sun.COM 			if (zp->z_blksz == size)
10221669Sperrin 				break;
102310922SJeff.Bonwick@Sun.COM 			offset += blkoff;
102410922SJeff.Bonwick@Sun.COM 			zfs_range_unlock(zgd->zgd_rl);
10251669Sperrin 		}
10261669Sperrin 		/* test for truncation needs to be done while range locked */
1027*11935SMark.Shellenbaum@Sun.COM 		if (lr->lr_offset >= zp->z_size)
10281669Sperrin 			error = ENOENT;
102910209SMark.Musante@Sun.COM #ifdef DEBUG
103010209SMark.Musante@Sun.COM 		if (zil_fault_io) {
103110209SMark.Musante@Sun.COM 			error = EIO;
103210209SMark.Musante@Sun.COM 			zil_fault_io = 0;
103310209SMark.Musante@Sun.COM 		}
103410209SMark.Musante@Sun.COM #endif
103510922SJeff.Bonwick@Sun.COM 		if (error == 0)
103610922SJeff.Bonwick@Sun.COM 			error = dmu_buf_hold(os, object, offset, zgd, &db);
103710922SJeff.Bonwick@Sun.COM 
103810800SNeil.Perrin@Sun.COM 		if (error == 0) {
103910922SJeff.Bonwick@Sun.COM 			zgd->zgd_db = db;
104010922SJeff.Bonwick@Sun.COM 			zgd->zgd_bp = bp;
104110922SJeff.Bonwick@Sun.COM 
104210922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_offset == offset);
104310922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_size == size);
104410922SJeff.Bonwick@Sun.COM 
104510922SJeff.Bonwick@Sun.COM 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
104610922SJeff.Bonwick@Sun.COM 			    zfs_get_done, zgd);
104710922SJeff.Bonwick@Sun.COM 			ASSERT(error || lr->lr_length <= zp->z_blksz);
104810922SJeff.Bonwick@Sun.COM 
104910800SNeil.Perrin@Sun.COM 			/*
105010922SJeff.Bonwick@Sun.COM 			 * On success, we need to wait for the write I/O
105110922SJeff.Bonwick@Sun.COM 			 * initiated by dmu_sync() to complete before we can
105210922SJeff.Bonwick@Sun.COM 			 * release this dbuf.  We will finish everything up
105310922SJeff.Bonwick@Sun.COM 			 * in the zfs_get_done() callback.
105410800SNeil.Perrin@Sun.COM 			 */
105510922SJeff.Bonwick@Sun.COM 			if (error == 0)
105610922SJeff.Bonwick@Sun.COM 				return (0);
105710922SJeff.Bonwick@Sun.COM 
105810922SJeff.Bonwick@Sun.COM 			if (error == EALREADY) {
105910922SJeff.Bonwick@Sun.COM 				lr->lr_common.lrc_txtype = TX_WRITE2;
106010922SJeff.Bonwick@Sun.COM 				error = 0;
106110922SJeff.Bonwick@Sun.COM 			}
106210800SNeil.Perrin@Sun.COM 		}
1063789Sahrens 	}
106410922SJeff.Bonwick@Sun.COM 
106510922SJeff.Bonwick@Sun.COM 	zfs_get_done(zgd, error);
106610922SJeff.Bonwick@Sun.COM 
1067789Sahrens 	return (error);
1068789Sahrens }
1069789Sahrens 
1070789Sahrens /*ARGSUSED*/
1071789Sahrens static int
10725331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
10735331Samw     caller_context_t *ct)
1074789Sahrens {
1075789Sahrens 	znode_t *zp = VTOZ(vp);
1076789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1077789Sahrens 	int error;
1078789Sahrens 
10795367Sahrens 	ZFS_ENTER(zfsvfs);
10805367Sahrens 	ZFS_VERIFY_ZP(zp);
10815331Samw 
10825331Samw 	if (flag & V_ACE_MASK)
10835331Samw 		error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
10845331Samw 	else
10855331Samw 		error = zfs_zaccess_rwx(zp, mode, flag, cr);
10865331Samw 
1087789Sahrens 	ZFS_EXIT(zfsvfs);
1088789Sahrens 	return (error);
1089789Sahrens }
1090789Sahrens 
1091789Sahrens /*
10929981STim.Haley@Sun.COM  * If vnode is for a device return a specfs vnode instead.
10939981STim.Haley@Sun.COM  */
10949981STim.Haley@Sun.COM static int
10959981STim.Haley@Sun.COM specvp_check(vnode_t **vpp, cred_t *cr)
10969981STim.Haley@Sun.COM {
10979981STim.Haley@Sun.COM 	int error = 0;
10989981STim.Haley@Sun.COM 
10999981STim.Haley@Sun.COM 	if (IS_DEVVP(*vpp)) {
11009981STim.Haley@Sun.COM 		struct vnode *svp;
11019981STim.Haley@Sun.COM 
11029981STim.Haley@Sun.COM 		svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
11039981STim.Haley@Sun.COM 		VN_RELE(*vpp);
11049981STim.Haley@Sun.COM 		if (svp == NULL)
11059981STim.Haley@Sun.COM 			error = ENOSYS;
11069981STim.Haley@Sun.COM 		*vpp = svp;
11079981STim.Haley@Sun.COM 	}
11089981STim.Haley@Sun.COM 	return (error);
11099981STim.Haley@Sun.COM }
11109981STim.Haley@Sun.COM 
11119981STim.Haley@Sun.COM 
11129981STim.Haley@Sun.COM /*
1113789Sahrens  * Lookup an entry in a directory, or an extended attribute directory.
1114789Sahrens  * If it exists, return a held vnode reference for it.
1115789Sahrens  *
1116789Sahrens  *	IN:	dvp	- vnode of directory to search.
1117789Sahrens  *		nm	- name of entry to lookup.
1118789Sahrens  *		pnp	- full pathname to lookup [UNUSED].
1119789Sahrens  *		flags	- LOOKUP_XATTR set if looking for an attribute.
1120789Sahrens  *		rdir	- root directory vnode [UNUSED].
1121789Sahrens  *		cr	- credentials of caller.
11225331Samw  *		ct	- caller context
11235331Samw  *		direntflags - directory lookup flags
11245331Samw  *		realpnp - returned pathname.
1125789Sahrens  *
1126789Sahrens  *	OUT:	vpp	- vnode of located entry, NULL if not found.
1127789Sahrens  *
1128789Sahrens  *	RETURN:	0 if success
1129789Sahrens  *		error code if failure
1130789Sahrens  *
1131789Sahrens  * Timestamps:
1132789Sahrens  *	NA
1133789Sahrens  */
1134789Sahrens /* ARGSUSED */
1135789Sahrens static int
1136789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
11375331Samw     int flags, vnode_t *rdir, cred_t *cr,  caller_context_t *ct,
11385331Samw     int *direntflags, pathname_t *realpnp)
1139789Sahrens {
1140789Sahrens 	znode_t *zdp = VTOZ(dvp);
1141789Sahrens 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
11429981STim.Haley@Sun.COM 	int	error = 0;
11439981STim.Haley@Sun.COM 
11449981STim.Haley@Sun.COM 	/* fast path */
11459981STim.Haley@Sun.COM 	if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
11469981STim.Haley@Sun.COM 
11479981STim.Haley@Sun.COM 		if (dvp->v_type != VDIR) {
11489981STim.Haley@Sun.COM 			return (ENOTDIR);
1149*11935SMark.Shellenbaum@Sun.COM 		} else if (zdp->z_sa_hdl == NULL) {
11509981STim.Haley@Sun.COM 			return (EIO);
11519981STim.Haley@Sun.COM 		}
11529981STim.Haley@Sun.COM 
11539981STim.Haley@Sun.COM 		if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
11549981STim.Haley@Sun.COM 			error = zfs_fastaccesschk_execute(zdp, cr);
11559981STim.Haley@Sun.COM 			if (!error) {
11569981STim.Haley@Sun.COM 				*vpp = dvp;
11579981STim.Haley@Sun.COM 				VN_HOLD(*vpp);
11589981STim.Haley@Sun.COM 				return (0);
11599981STim.Haley@Sun.COM 			}
11609981STim.Haley@Sun.COM 			return (error);
11619981STim.Haley@Sun.COM 		} else {
11629981STim.Haley@Sun.COM 			vnode_t *tvp = dnlc_lookup(dvp, nm);
11639981STim.Haley@Sun.COM 
11649981STim.Haley@Sun.COM 			if (tvp) {
11659981STim.Haley@Sun.COM 				error = zfs_fastaccesschk_execute(zdp, cr);
11669981STim.Haley@Sun.COM 				if (error) {
11679981STim.Haley@Sun.COM 					VN_RELE(tvp);
11689981STim.Haley@Sun.COM 					return (error);
11699981STim.Haley@Sun.COM 				}
11709981STim.Haley@Sun.COM 				if (tvp == DNLC_NO_VNODE) {
11719981STim.Haley@Sun.COM 					VN_RELE(tvp);
11729981STim.Haley@Sun.COM 					return (ENOENT);
11739981STim.Haley@Sun.COM 				} else {
11749981STim.Haley@Sun.COM 					*vpp = tvp;
11759981STim.Haley@Sun.COM 					return (specvp_check(vpp, cr));
11769981STim.Haley@Sun.COM 				}
11779981STim.Haley@Sun.COM 			}
11789981STim.Haley@Sun.COM 		}
11799981STim.Haley@Sun.COM 	}
11809981STim.Haley@Sun.COM 
11819981STim.Haley@Sun.COM 	DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1182789Sahrens 
11835367Sahrens 	ZFS_ENTER(zfsvfs);
11845367Sahrens 	ZFS_VERIFY_ZP(zdp);
1185789Sahrens 
1186789Sahrens 	*vpp = NULL;
1187789Sahrens 
1188789Sahrens 	if (flags & LOOKUP_XATTR) {
1189789Sahrens 		/*
11903234Sck153898 		 * If the xattr property is off, refuse the lookup request.
11913234Sck153898 		 */
11923234Sck153898 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
11933234Sck153898 			ZFS_EXIT(zfsvfs);
11943234Sck153898 			return (EINVAL);
11953234Sck153898 		}
11963234Sck153898 
11973234Sck153898 		/*
1198789Sahrens 		 * We don't allow recursive attributes..
1199789Sahrens 		 * Maybe someday we will.
1200789Sahrens 		 */
1201*11935SMark.Shellenbaum@Sun.COM 		if (zdp->z_pflags & ZFS_XATTR) {
1202789Sahrens 			ZFS_EXIT(zfsvfs);
1203789Sahrens 			return (EINVAL);
1204789Sahrens 		}
1205789Sahrens 
12063280Sck153898 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1207789Sahrens 			ZFS_EXIT(zfsvfs);
1208789Sahrens 			return (error);
1209789Sahrens 		}
1210789Sahrens 
1211789Sahrens 		/*
1212789Sahrens 		 * Do we have permission to get into attribute directory?
1213789Sahrens 		 */
1214789Sahrens 
12155331Samw 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
12165331Samw 		    B_FALSE, cr)) {
1217789Sahrens 			VN_RELE(*vpp);
12185331Samw 			*vpp = NULL;
1219789Sahrens 		}
1220789Sahrens 
1221789Sahrens 		ZFS_EXIT(zfsvfs);
1222789Sahrens 		return (error);
1223789Sahrens 	}
1224789Sahrens 
12251512Sek110237 	if (dvp->v_type != VDIR) {
12261512Sek110237 		ZFS_EXIT(zfsvfs);
12271460Smarks 		return (ENOTDIR);
12281512Sek110237 	}
12291460Smarks 
1230789Sahrens 	/*
1231789Sahrens 	 * Check accessibility of directory.
1232789Sahrens 	 */
1233789Sahrens 
12345331Samw 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1235789Sahrens 		ZFS_EXIT(zfsvfs);
1236789Sahrens 		return (error);
1237789Sahrens 	}
1238789Sahrens 
12395498Stimh 	if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
12405331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
12415331Samw 		ZFS_EXIT(zfsvfs);
12425331Samw 		return (EILSEQ);
12435331Samw 	}
12445331Samw 
12455331Samw 	error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
12469981STim.Haley@Sun.COM 	if (error == 0)
12479981STim.Haley@Sun.COM 		error = specvp_check(vpp, cr);
1248789Sahrens 
1249789Sahrens 	ZFS_EXIT(zfsvfs);
1250789Sahrens 	return (error);
1251789Sahrens }
1252789Sahrens 
1253789Sahrens /*
1254789Sahrens  * Attempt to create a new entry in a directory.  If the entry
1255789Sahrens  * already exists, truncate the file if permissible, else return
1256789Sahrens  * an error.  Return the vp of the created or trunc'd file.
1257789Sahrens  *
1258789Sahrens  *	IN:	dvp	- vnode of directory to put new file entry in.
1259789Sahrens  *		name	- name of new file entry.
1260789Sahrens  *		vap	- attributes of new file.
1261789Sahrens  *		excl	- flag indicating exclusive or non-exclusive mode.
1262789Sahrens  *		mode	- mode to open file with.
1263789Sahrens  *		cr	- credentials of caller.
1264789Sahrens  *		flag	- large file flag [UNUSED].
12655331Samw  *		ct	- caller context
12665331Samw  *		vsecp 	- ACL to be set
1267789Sahrens  *
1268789Sahrens  *	OUT:	vpp	- vnode of created or trunc'd entry.
1269789Sahrens  *
1270789Sahrens  *	RETURN:	0 if success
1271789Sahrens  *		error code if failure
1272789Sahrens  *
1273789Sahrens  * Timestamps:
1274789Sahrens  *	dvp - ctime|mtime updated if new entry created
1275789Sahrens  *	 vp - ctime|mtime always, atime if new
1276789Sahrens  */
12775331Samw 
1278789Sahrens /* ARGSUSED */
1279789Sahrens static int
1280789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
12815331Samw     int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
12825331Samw     vsecattr_t *vsecp)
1283789Sahrens {
1284789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1285789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
12865326Sek110237 	zilog_t		*zilog;
12875326Sek110237 	objset_t	*os;
1288789Sahrens 	zfs_dirlock_t	*dl;
1289789Sahrens 	dmu_tx_t	*tx;
1290789Sahrens 	int		error;
12917847SMark.Shellenbaum@Sun.COM 	ksid_t		*ksid;
12927847SMark.Shellenbaum@Sun.COM 	uid_t		uid;
12937847SMark.Shellenbaum@Sun.COM 	gid_t		gid = crgetgid(cr);
1294*11935SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t   acl_ids;
12959179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
12965331Samw 
12975331Samw 	/*
12985331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
12995331Samw 	 * make sure file system is at proper version
13005331Samw 	 */
13015331Samw 
13027847SMark.Shellenbaum@Sun.COM 	ksid = crgetsid(cr, KSID_OWNER);
13037847SMark.Shellenbaum@Sun.COM 	if (ksid)
13047847SMark.Shellenbaum@Sun.COM 		uid = ksid_getid(ksid);
13057847SMark.Shellenbaum@Sun.COM 	else
13067847SMark.Shellenbaum@Sun.COM 		uid = crgetuid(cr);
13077847SMark.Shellenbaum@Sun.COM 
13085331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
13095331Samw 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
13107847SMark.Shellenbaum@Sun.COM 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
13115331Samw 		return (EINVAL);
1312789Sahrens 
13135367Sahrens 	ZFS_ENTER(zfsvfs);
13145367Sahrens 	ZFS_VERIFY_ZP(dzp);
13155326Sek110237 	os = zfsvfs->z_os;
13165326Sek110237 	zilog = zfsvfs->z_log;
1317789Sahrens 
13185498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
13195331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
13205331Samw 		ZFS_EXIT(zfsvfs);
13215331Samw 		return (EILSEQ);
13225331Samw 	}
13235331Samw 
13245331Samw 	if (vap->va_mask & AT_XVATTR) {
13255331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
13265331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
13275331Samw 			ZFS_EXIT(zfsvfs);
13285331Samw 			return (error);
13295331Samw 		}
13305331Samw 	}
1331789Sahrens top:
1332789Sahrens 	*vpp = NULL;
1333789Sahrens 
1334789Sahrens 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1335789Sahrens 		vap->va_mode &= ~VSVTX;
1336789Sahrens 
1337789Sahrens 	if (*name == '\0') {
1338789Sahrens 		/*
1339789Sahrens 		 * Null component name refers to the directory itself.
1340789Sahrens 		 */
1341789Sahrens 		VN_HOLD(dvp);
1342789Sahrens 		zp = dzp;
1343789Sahrens 		dl = NULL;
1344789Sahrens 		error = 0;
1345789Sahrens 	} else {
1346789Sahrens 		/* possible VN_HOLD(zp) */
13475331Samw 		int zflg = 0;
13485331Samw 
13495331Samw 		if (flag & FIGNORECASE)
13505331Samw 			zflg |= ZCILOOK;
13515331Samw 
13525331Samw 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
13535331Samw 		    NULL, NULL);
13545331Samw 		if (error) {
1355789Sahrens 			if (strcmp(name, "..") == 0)
1356789Sahrens 				error = EISDIR;
1357789Sahrens 			ZFS_EXIT(zfsvfs);
1358789Sahrens 			return (error);
1359789Sahrens 		}
1360789Sahrens 	}
1361*11935SMark.Shellenbaum@Sun.COM 
1362789Sahrens 	if (zp == NULL) {
13635331Samw 		uint64_t txtype;
13645331Samw 
1365789Sahrens 		/*
1366789Sahrens 		 * Create a new file object and update the directory
1367789Sahrens 		 * to reference it.
1368789Sahrens 		 */
13695331Samw 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1370789Sahrens 			goto out;
1371789Sahrens 		}
1372789Sahrens 
1373789Sahrens 		/*
1374789Sahrens 		 * We only support the creation of regular files in
1375789Sahrens 		 * extended attribute directories.
1376789Sahrens 		 */
1377*11935SMark.Shellenbaum@Sun.COM 
1378*11935SMark.Shellenbaum@Sun.COM 		if ((dzp->z_pflags & ZFS_XATTR) &&
1379789Sahrens 		    (vap->va_type != VREG)) {
1380789Sahrens 			error = EINVAL;
1381789Sahrens 			goto out;
1382789Sahrens 		}
1383789Sahrens 
13849179SMark.Shellenbaum@Sun.COM 		if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
13859179SMark.Shellenbaum@Sun.COM 		    &acl_ids)) != 0)
13869179SMark.Shellenbaum@Sun.COM 			goto out;
13879396SMatthew.Ahrens@Sun.COM 		if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
138810143STim.Haley@Sun.COM 			zfs_acl_ids_free(&acl_ids);
13899396SMatthew.Ahrens@Sun.COM 			error = EDQUOT;
13909396SMatthew.Ahrens@Sun.COM 			goto out;
13919396SMatthew.Ahrens@Sun.COM 		}
13929179SMark.Shellenbaum@Sun.COM 
1393789Sahrens 		tx = dmu_tx_create(os);
1394*11935SMark.Shellenbaum@Sun.COM 
1395*11935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1396*11935SMark.Shellenbaum@Sun.COM 		    ZFS_SA_BASE_ATTR_SIZE);
1397*11935SMark.Shellenbaum@Sun.COM 
13989179SMark.Shellenbaum@Sun.COM 		fuid_dirtied = zfsvfs->z_fuid_dirty;
13999396SMatthew.Ahrens@Sun.COM 		if (fuid_dirtied)
14009396SMatthew.Ahrens@Sun.COM 			zfs_fuid_txhold(zfsvfs, tx);
14011544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1402*11935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1403*11935SMark.Shellenbaum@Sun.COM 		if (!zfsvfs->z_use_sa &&
1404*11935SMark.Shellenbaum@Sun.COM 		    acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1405789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1406*11935SMark.Shellenbaum@Sun.COM 			    0, acl_ids.z_aclp->z_acl_bytes);
14075331Samw 		}
14088227SNeil.Perrin@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
1409789Sahrens 		if (error) {
14109179SMark.Shellenbaum@Sun.COM 			zfs_acl_ids_free(&acl_ids);
1411789Sahrens 			zfs_dirent_unlock(dl);
14128227SNeil.Perrin@Sun.COM 			if (error == ERESTART) {
14132113Sahrens 				dmu_tx_wait(tx);
14142113Sahrens 				dmu_tx_abort(tx);
1415789Sahrens 				goto top;
1416789Sahrens 			}
14172113Sahrens 			dmu_tx_abort(tx);
1418789Sahrens 			ZFS_EXIT(zfsvfs);
1419789Sahrens 			return (error);
1420789Sahrens 		}
1421*11935SMark.Shellenbaum@Sun.COM 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
14229179SMark.Shellenbaum@Sun.COM 
14239179SMark.Shellenbaum@Sun.COM 		if (fuid_dirtied)
14249179SMark.Shellenbaum@Sun.COM 			zfs_fuid_sync(zfsvfs, tx);
14259179SMark.Shellenbaum@Sun.COM 
1426789Sahrens 		(void) zfs_link_create(dl, zp, tx, ZNEW);
14275331Samw 		txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
14285331Samw 		if (flag & FIGNORECASE)
14295331Samw 			txtype |= TX_CI;
14305331Samw 		zfs_log_create(zilog, tx, txtype, dzp, zp, name,
14319179SMark.Shellenbaum@Sun.COM 		    vsecp, acl_ids.z_fuidp, vap);
14329179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
1433789Sahrens 		dmu_tx_commit(tx);
1434789Sahrens 	} else {
14355331Samw 		int aflags = (flag & FAPPEND) ? V_APPEND : 0;
14365331Samw 
1437789Sahrens 		/*
1438789Sahrens 		 * A directory entry already exists for this name.
1439789Sahrens 		 */
1440789Sahrens 		/*
1441789Sahrens 		 * Can't truncate an existing file if in exclusive mode.
1442789Sahrens 		 */
1443789Sahrens 		if (excl == EXCL) {
1444789Sahrens 			error = EEXIST;
1445789Sahrens 			goto out;
1446789Sahrens 		}
1447789Sahrens 		/*
1448789Sahrens 		 * Can't open a directory for writing.
1449789Sahrens 		 */
1450789Sahrens 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1451789Sahrens 			error = EISDIR;
1452789Sahrens 			goto out;
1453789Sahrens 		}
1454789Sahrens 		/*
1455789Sahrens 		 * Verify requested access to file.
1456789Sahrens 		 */
14575331Samw 		if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1458789Sahrens 			goto out;
1459789Sahrens 		}
1460789Sahrens 
1461789Sahrens 		mutex_enter(&dzp->z_lock);
1462789Sahrens 		dzp->z_seq++;
1463789Sahrens 		mutex_exit(&dzp->z_lock);
1464789Sahrens 
14651878Smaybee 		/*
14661878Smaybee 		 * Truncate regular files if requested.
14671878Smaybee 		 */
14681878Smaybee 		if ((ZTOV(zp)->v_type == VREG) &&
1469789Sahrens 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
14706992Smaybee 			/* we can't hold any locks when calling zfs_freesp() */
14716992Smaybee 			zfs_dirent_unlock(dl);
14726992Smaybee 			dl = NULL;
14731878Smaybee 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
14744863Spraks 			if (error == 0) {
14755331Samw 				vnevent_create(ZTOV(zp), ct);
14764863Spraks 			}
1477789Sahrens 		}
1478789Sahrens 	}
1479789Sahrens out:
1480789Sahrens 
1481789Sahrens 	if (dl)
1482789Sahrens 		zfs_dirent_unlock(dl);
1483789Sahrens 
1484789Sahrens 	if (error) {
1485789Sahrens 		if (zp)
1486789Sahrens 			VN_RELE(ZTOV(zp));
1487789Sahrens 	} else {
1488789Sahrens 		*vpp = ZTOV(zp);
14899981STim.Haley@Sun.COM 		error = specvp_check(vpp, cr);
1490789Sahrens 	}
1491789Sahrens 
1492789Sahrens 	ZFS_EXIT(zfsvfs);
1493789Sahrens 	return (error);
1494789Sahrens }
1495789Sahrens 
1496789Sahrens /*
1497789Sahrens  * Remove an entry from a directory.
1498789Sahrens  *
1499789Sahrens  *	IN:	dvp	- vnode of directory to remove entry from.
1500789Sahrens  *		name	- name of entry to remove.
1501789Sahrens  *		cr	- credentials of caller.
15025331Samw  *		ct	- caller context
15035331Samw  *		flags	- case flags
1504789Sahrens  *
1505789Sahrens  *	RETURN:	0 if success
1506789Sahrens  *		error code if failure
1507789Sahrens  *
1508789Sahrens  * Timestamps:
1509789Sahrens  *	dvp - ctime|mtime
1510789Sahrens  *	 vp - ctime (if nlink > 0)
1511789Sahrens  */
1512*11935SMark.Shellenbaum@Sun.COM 
1513*11935SMark.Shellenbaum@Sun.COM uint64_t null_xattr = 0;
1514*11935SMark.Shellenbaum@Sun.COM 
15155331Samw /*ARGSUSED*/
1516789Sahrens static int
15175331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
15185331Samw     int flags)
1519789Sahrens {
1520789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1521789Sahrens 	znode_t		*xzp = NULL;
1522789Sahrens 	vnode_t		*vp;
1523789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
15245326Sek110237 	zilog_t		*zilog;
1525*11935SMark.Shellenbaum@Sun.COM 	uint64_t	acl_obj, xattr_obj = 0;
1526*11935SMark.Shellenbaum@Sun.COM 	uint64_t 	xattr_obj_unlinked = 0;
1527789Sahrens 	zfs_dirlock_t	*dl;
1528789Sahrens 	dmu_tx_t	*tx;
15293461Sahrens 	boolean_t	may_delete_now, delete_now = FALSE;
15306992Smaybee 	boolean_t	unlinked, toobig = FALSE;
15315331Samw 	uint64_t	txtype;
15325331Samw 	pathname_t	*realnmp = NULL;
15335331Samw 	pathname_t	realnm;
1534789Sahrens 	int		error;
15355331Samw 	int		zflg = ZEXISTS;
1536789Sahrens 
15375367Sahrens 	ZFS_ENTER(zfsvfs);
15385367Sahrens 	ZFS_VERIFY_ZP(dzp);
15395326Sek110237 	zilog = zfsvfs->z_log;
1540789Sahrens 
15415331Samw 	if (flags & FIGNORECASE) {
15425331Samw 		zflg |= ZCILOOK;
15435331Samw 		pn_alloc(&realnm);
15445331Samw 		realnmp = &realnm;
15455331Samw 	}
15465331Samw 
1547789Sahrens top:
1548789Sahrens 	/*
1549789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1550789Sahrens 	 */
15515331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
15525331Samw 	    NULL, realnmp)) {
15535331Samw 		if (realnmp)
15545331Samw 			pn_free(realnmp);
1555789Sahrens 		ZFS_EXIT(zfsvfs);
1556789Sahrens 		return (error);
1557789Sahrens 	}
1558789Sahrens 
1559789Sahrens 	vp = ZTOV(zp);
1560789Sahrens 
1561789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1562789Sahrens 		goto out;
1563789Sahrens 	}
1564789Sahrens 
1565789Sahrens 	/*
1566789Sahrens 	 * Need to use rmdir for removing directories.
1567789Sahrens 	 */
1568789Sahrens 	if (vp->v_type == VDIR) {
1569789Sahrens 		error = EPERM;
1570789Sahrens 		goto out;
1571789Sahrens 	}
1572789Sahrens 
15735331Samw 	vnevent_remove(vp, dvp, name, ct);
15745331Samw 
15755331Samw 	if (realnmp)
15766492Stimh 		dnlc_remove(dvp, realnmp->pn_buf);
15775331Samw 	else
15785331Samw 		dnlc_remove(dvp, name);
15791484Sek110237 
1580789Sahrens 	mutex_enter(&vp->v_lock);
1581789Sahrens 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1582789Sahrens 	mutex_exit(&vp->v_lock);
1583789Sahrens 
1584789Sahrens 	/*
15853461Sahrens 	 * We may delete the znode now, or we may put it in the unlinked set;
1586789Sahrens 	 * it depends on whether we're the last link, and on whether there are
1587789Sahrens 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1588789Sahrens 	 * allow for either case.
1589789Sahrens 	 */
1590789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
15911544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1592*11935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1593*11935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
1594*11935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, dzp);
15956992Smaybee 	if (may_delete_now) {
15966992Smaybee 		toobig =
1597*11935SMark.Shellenbaum@Sun.COM 		    zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
15986992Smaybee 		/* if the file is too big, only hold_free a token amount */
15996992Smaybee 		dmu_tx_hold_free(tx, zp->z_id, 0,
16006992Smaybee 		    (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
16016992Smaybee 	}
1602789Sahrens 
1603789Sahrens 	/* are there any extended attributes? */
1604*11935SMark.Shellenbaum@Sun.COM 	error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1605*11935SMark.Shellenbaum@Sun.COM 	    &xattr_obj, sizeof (xattr_obj));
1606*11935SMark.Shellenbaum@Sun.COM 	if (xattr_obj) {
1607*11935SMark.Shellenbaum@Sun.COM 		error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1608*11935SMark.Shellenbaum@Sun.COM 		ASSERT3U(error, ==, 0);
1609*11935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1610*11935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1611789Sahrens 	}
1612789Sahrens 
1613789Sahrens 	/* are there any additional acls */
1614*11935SMark.Shellenbaum@Sun.COM 	if ((acl_obj = ZFS_EXTERNAL_ACL(zp)) != 0 && may_delete_now)
1615789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1616789Sahrens 
1617789Sahrens 	/* charge as an update -- would be nice not to charge at all */
16183461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1619789Sahrens 
16208227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1621789Sahrens 	if (error) {
1622789Sahrens 		zfs_dirent_unlock(dl);
1623789Sahrens 		VN_RELE(vp);
16248227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
16252113Sahrens 			dmu_tx_wait(tx);
16262113Sahrens 			dmu_tx_abort(tx);
1627789Sahrens 			goto top;
1628789Sahrens 		}
16295331Samw 		if (realnmp)
16305331Samw 			pn_free(realnmp);
16312113Sahrens 		dmu_tx_abort(tx);
1632789Sahrens 		ZFS_EXIT(zfsvfs);
1633789Sahrens 		return (error);
1634789Sahrens 	}
1635789Sahrens 
1636789Sahrens 	/*
1637789Sahrens 	 * Remove the directory entry.
1638789Sahrens 	 */
16395331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1640789Sahrens 
1641789Sahrens 	if (error) {
1642789Sahrens 		dmu_tx_commit(tx);
1643789Sahrens 		goto out;
1644789Sahrens 	}
1645789Sahrens 
16463461Sahrens 	if (unlinked) {
1647*11935SMark.Shellenbaum@Sun.COM 
1648789Sahrens 		mutex_enter(&vp->v_lock);
1649*11935SMark.Shellenbaum@Sun.COM 
1650*11935SMark.Shellenbaum@Sun.COM 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1651*11935SMark.Shellenbaum@Sun.COM 		    &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
16526992Smaybee 		delete_now = may_delete_now && !toobig &&
1653789Sahrens 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1654*11935SMark.Shellenbaum@Sun.COM 		    xattr_obj == xattr_obj_unlinked && ZFS_EXTERNAL_ACL(zp) ==
1655*11935SMark.Shellenbaum@Sun.COM 		    acl_obj;
1656789Sahrens 		mutex_exit(&vp->v_lock);
1657789Sahrens 	}
1658789Sahrens 
1659789Sahrens 	if (delete_now) {
1660*11935SMark.Shellenbaum@Sun.COM 		if (xattr_obj_unlinked) {
1661*11935SMark.Shellenbaum@Sun.COM 			ASSERT3U(xzp->z_links, ==, 2);
1662789Sahrens 			mutex_enter(&xzp->z_lock);
16633461Sahrens 			xzp->z_unlinked = 1;
1664*11935SMark.Shellenbaum@Sun.COM 			xzp->z_links = 0;
1665*11935SMark.Shellenbaum@Sun.COM 			error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
1666*11935SMark.Shellenbaum@Sun.COM 			    &xzp->z_links, sizeof (xzp->z_links), tx);
1667*11935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error,  ==,  0);
1668789Sahrens 			mutex_exit(&xzp->z_lock);
16693461Sahrens 			zfs_unlinked_add(xzp, tx);
1670*11935SMark.Shellenbaum@Sun.COM 			if (zp->z_is_sa)
1671*11935SMark.Shellenbaum@Sun.COM 				error = sa_remove(zp->z_sa_hdl,
1672*11935SMark.Shellenbaum@Sun.COM 				    SA_ZPL_XATTR(zfsvfs), tx);
1673*11935SMark.Shellenbaum@Sun.COM 			else
1674*11935SMark.Shellenbaum@Sun.COM 				error = sa_update(zp->z_sa_hdl,
1675*11935SMark.Shellenbaum@Sun.COM 				    SA_ZPL_XATTR(zfsvfs), &null_xattr,
1676*11935SMark.Shellenbaum@Sun.COM 				    sizeof (uint64_t), tx);
1677*11935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error, ==, 0);
1678789Sahrens 		}
1679789Sahrens 		mutex_enter(&zp->z_lock);
1680789Sahrens 		mutex_enter(&vp->v_lock);
1681789Sahrens 		vp->v_count--;
1682789Sahrens 		ASSERT3U(vp->v_count, ==, 0);
1683789Sahrens 		mutex_exit(&vp->v_lock);
1684789Sahrens 		mutex_exit(&zp->z_lock);
1685789Sahrens 		zfs_znode_delete(zp, tx);
16863461Sahrens 	} else if (unlinked) {
16873461Sahrens 		zfs_unlinked_add(zp, tx);
1688789Sahrens 	}
1689789Sahrens 
16905331Samw 	txtype = TX_REMOVE;
16915331Samw 	if (flags & FIGNORECASE)
16925331Samw 		txtype |= TX_CI;
16935331Samw 	zfs_log_remove(zilog, tx, txtype, dzp, name);
1694789Sahrens 
1695789Sahrens 	dmu_tx_commit(tx);
1696789Sahrens out:
16975331Samw 	if (realnmp)
16985331Samw 		pn_free(realnmp);
16995331Samw 
1700789Sahrens 	zfs_dirent_unlock(dl);
1701789Sahrens 
1702789Sahrens 	if (!delete_now) {
1703789Sahrens 		VN_RELE(vp);
1704789Sahrens 	} else if (xzp) {
17056992Smaybee 		/* this rele is delayed to prevent nesting transactions */
1706789Sahrens 		VN_RELE(ZTOV(xzp));
1707789Sahrens 	}
1708789Sahrens 
1709789Sahrens 	ZFS_EXIT(zfsvfs);
1710789Sahrens 	return (error);
1711789Sahrens }
1712789Sahrens 
1713789Sahrens /*
1714789Sahrens  * Create a new directory and insert it into dvp using the name
1715789Sahrens  * provided.  Return a pointer to the inserted directory.
1716789Sahrens  *
1717789Sahrens  *	IN:	dvp	- vnode of directory to add subdir to.
1718789Sahrens  *		dirname	- name of new directory.
1719789Sahrens  *		vap	- attributes of new directory.
1720789Sahrens  *		cr	- credentials of caller.
17215331Samw  *		ct	- caller context
17225331Samw  *		vsecp	- ACL to be set
1723789Sahrens  *
1724789Sahrens  *	OUT:	vpp	- vnode of created directory.
1725789Sahrens  *
1726789Sahrens  *	RETURN:	0 if success
1727789Sahrens  *		error code if failure
1728789Sahrens  *
1729789Sahrens  * Timestamps:
1730789Sahrens  *	dvp - ctime|mtime updated
1731789Sahrens  *	 vp - ctime|mtime|atime updated
1732789Sahrens  */
17335331Samw /*ARGSUSED*/
1734789Sahrens static int
17355331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
17365331Samw     caller_context_t *ct, int flags, vsecattr_t *vsecp)
1737789Sahrens {
1738789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1739789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
17405326Sek110237 	zilog_t		*zilog;
1741789Sahrens 	zfs_dirlock_t	*dl;
17425331Samw 	uint64_t	txtype;
1743789Sahrens 	dmu_tx_t	*tx;
1744789Sahrens 	int		error;
17455331Samw 	int		zf = ZNEW;
17467847SMark.Shellenbaum@Sun.COM 	ksid_t		*ksid;
17477847SMark.Shellenbaum@Sun.COM 	uid_t		uid;
17487847SMark.Shellenbaum@Sun.COM 	gid_t		gid = crgetgid(cr);
1749*11935SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t   acl_ids;
17509179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
1751789Sahrens 
1752789Sahrens 	ASSERT(vap->va_type == VDIR);
1753789Sahrens 
17545331Samw 	/*
17555331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
17565331Samw 	 * make sure file system is at proper version
17575331Samw 	 */
17585331Samw 
17597847SMark.Shellenbaum@Sun.COM 	ksid = crgetsid(cr, KSID_OWNER);
17607847SMark.Shellenbaum@Sun.COM 	if (ksid)
17617847SMark.Shellenbaum@Sun.COM 		uid = ksid_getid(ksid);
17627847SMark.Shellenbaum@Sun.COM 	else
17637847SMark.Shellenbaum@Sun.COM 		uid = crgetuid(cr);
17645331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
17657847SMark.Shellenbaum@Sun.COM 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
17667876SMark.Shellenbaum@Sun.COM 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
17675331Samw 		return (EINVAL);
17685331Samw 
17695367Sahrens 	ZFS_ENTER(zfsvfs);
17705367Sahrens 	ZFS_VERIFY_ZP(dzp);
17715326Sek110237 	zilog = zfsvfs->z_log;
1772789Sahrens 
1773*11935SMark.Shellenbaum@Sun.COM 	if (dzp->z_pflags & ZFS_XATTR) {
1774789Sahrens 		ZFS_EXIT(zfsvfs);
1775789Sahrens 		return (EINVAL);
1776789Sahrens 	}
17775331Samw 
17785498Stimh 	if (zfsvfs->z_utf8 && u8_validate(dirname,
17795331Samw 	    strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
17805331Samw 		ZFS_EXIT(zfsvfs);
17815331Samw 		return (EILSEQ);
17825331Samw 	}
17835331Samw 	if (flags & FIGNORECASE)
17845331Samw 		zf |= ZCILOOK;
17855331Samw 
17865331Samw 	if (vap->va_mask & AT_XVATTR)
17875331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
17885331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
17895331Samw 			ZFS_EXIT(zfsvfs);
17905331Samw 			return (error);
17915331Samw 		}
1792789Sahrens 
1793789Sahrens 	/*
1794789Sahrens 	 * First make sure the new directory doesn't exist.
1795789Sahrens 	 */
17965331Samw top:
17975331Samw 	*vpp = NULL;
17985331Samw 
17995331Samw 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
18005331Samw 	    NULL, NULL)) {
1801789Sahrens 		ZFS_EXIT(zfsvfs);
1802789Sahrens 		return (error);
1803789Sahrens 	}
1804789Sahrens 
18055331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
18061231Smarks 		zfs_dirent_unlock(dl);
18071231Smarks 		ZFS_EXIT(zfsvfs);
18081231Smarks 		return (error);
18091231Smarks 	}
18101231Smarks 
18119179SMark.Shellenbaum@Sun.COM 	if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
18129179SMark.Shellenbaum@Sun.COM 	    &acl_ids)) != 0) {
18139179SMark.Shellenbaum@Sun.COM 		zfs_dirent_unlock(dl);
18149179SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
18159179SMark.Shellenbaum@Sun.COM 		return (error);
18165331Samw 	}
18179396SMatthew.Ahrens@Sun.COM 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
181810143STim.Haley@Sun.COM 		zfs_acl_ids_free(&acl_ids);
18199396SMatthew.Ahrens@Sun.COM 		zfs_dirent_unlock(dl);
18209396SMatthew.Ahrens@Sun.COM 		ZFS_EXIT(zfsvfs);
18219396SMatthew.Ahrens@Sun.COM 		return (EDQUOT);
18229396SMatthew.Ahrens@Sun.COM 	}
18239179SMark.Shellenbaum@Sun.COM 
1824789Sahrens 	/*
1825789Sahrens 	 * Add a new entry to the directory.
1826789Sahrens 	 */
1827789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
18281544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
18291544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
18309179SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
18319396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
18329396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
1833*11935SMark.Shellenbaum@Sun.COM 	if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1834*11935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1835*11935SMark.Shellenbaum@Sun.COM 		    acl_ids.z_aclp->z_acl_bytes);
1836*11935SMark.Shellenbaum@Sun.COM 	}
1837*11935SMark.Shellenbaum@Sun.COM 
1838*11935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1839*11935SMark.Shellenbaum@Sun.COM 	    ZFS_SA_BASE_ATTR_SIZE);
1840*11935SMark.Shellenbaum@Sun.COM 
18418227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1842789Sahrens 	if (error) {
18439179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
1844789Sahrens 		zfs_dirent_unlock(dl);
18458227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
18462113Sahrens 			dmu_tx_wait(tx);
18472113Sahrens 			dmu_tx_abort(tx);
1848789Sahrens 			goto top;
1849789Sahrens 		}
18502113Sahrens 		dmu_tx_abort(tx);
1851789Sahrens 		ZFS_EXIT(zfsvfs);
1852789Sahrens 		return (error);
1853789Sahrens 	}
1854789Sahrens 
1855789Sahrens 	/*
1856789Sahrens 	 * Create new node.
1857789Sahrens 	 */
1858*11935SMark.Shellenbaum@Sun.COM 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
18599179SMark.Shellenbaum@Sun.COM 
18609179SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
18619179SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
1862*11935SMark.Shellenbaum@Sun.COM 
1863789Sahrens 	/*
1864789Sahrens 	 * Now put new name in parent dir.
1865789Sahrens 	 */
1866789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1867789Sahrens 
1868789Sahrens 	*vpp = ZTOV(zp);
1869789Sahrens 
18705331Samw 	txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
18715331Samw 	if (flags & FIGNORECASE)
18725331Samw 		txtype |= TX_CI;
18739179SMark.Shellenbaum@Sun.COM 	zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
18749179SMark.Shellenbaum@Sun.COM 	    acl_ids.z_fuidp, vap);
18759179SMark.Shellenbaum@Sun.COM 
18769179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_free(&acl_ids);
1877*11935SMark.Shellenbaum@Sun.COM 
1878789Sahrens 	dmu_tx_commit(tx);
1879789Sahrens 
1880789Sahrens 	zfs_dirent_unlock(dl);
1881789Sahrens 
1882789Sahrens 	ZFS_EXIT(zfsvfs);
1883789Sahrens 	return (0);
1884789Sahrens }
1885789Sahrens 
1886789Sahrens /*
1887789Sahrens  * Remove a directory subdir entry.  If the current working
1888789Sahrens  * directory is the same as the subdir to be removed, the
1889789Sahrens  * remove will fail.
1890789Sahrens  *
1891789Sahrens  *	IN:	dvp	- vnode of directory to remove from.
1892789Sahrens  *		name	- name of directory to be removed.
1893789Sahrens  *		cwd	- vnode of current working directory.
1894789Sahrens  *		cr	- credentials of caller.
18955331Samw  *		ct	- caller context
18965331Samw  *		flags	- case flags
1897789Sahrens  *
1898789Sahrens  *	RETURN:	0 if success
1899789Sahrens  *		error code if failure
1900789Sahrens  *
1901789Sahrens  * Timestamps:
1902789Sahrens  *	dvp - ctime|mtime updated
1903789Sahrens  */
19045331Samw /*ARGSUSED*/
1905789Sahrens static int
19065331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
19075331Samw     caller_context_t *ct, int flags)
1908789Sahrens {
1909789Sahrens 	znode_t		*dzp = VTOZ(dvp);
1910789Sahrens 	znode_t		*zp;
1911789Sahrens 	vnode_t		*vp;
1912789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
19135326Sek110237 	zilog_t		*zilog;
1914789Sahrens 	zfs_dirlock_t	*dl;
1915789Sahrens 	dmu_tx_t	*tx;
1916789Sahrens 	int		error;
19175331Samw 	int		zflg = ZEXISTS;
1918789Sahrens 
19195367Sahrens 	ZFS_ENTER(zfsvfs);
19205367Sahrens 	ZFS_VERIFY_ZP(dzp);
19215326Sek110237 	zilog = zfsvfs->z_log;
1922789Sahrens 
19235331Samw 	if (flags & FIGNORECASE)
19245331Samw 		zflg |= ZCILOOK;
1925789Sahrens top:
1926789Sahrens 	zp = NULL;
1927789Sahrens 
1928789Sahrens 	/*
1929789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1930789Sahrens 	 */
19315331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
19325331Samw 	    NULL, NULL)) {
1933789Sahrens 		ZFS_EXIT(zfsvfs);
1934789Sahrens 		return (error);
1935789Sahrens 	}
1936789Sahrens 
1937789Sahrens 	vp = ZTOV(zp);
1938789Sahrens 
1939789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1940789Sahrens 		goto out;
1941789Sahrens 	}
1942789Sahrens 
1943789Sahrens 	if (vp->v_type != VDIR) {
1944789Sahrens 		error = ENOTDIR;
1945789Sahrens 		goto out;
1946789Sahrens 	}
1947789Sahrens 
1948789Sahrens 	if (vp == cwd) {
1949789Sahrens 		error = EINVAL;
1950789Sahrens 		goto out;
1951789Sahrens 	}
1952789Sahrens 
19535331Samw 	vnevent_rmdir(vp, dvp, name, ct);
1954789Sahrens 
1955789Sahrens 	/*
19563897Smaybee 	 * Grab a lock on the directory to make sure that noone is
19573897Smaybee 	 * trying to add (or lookup) entries while we are removing it.
19583897Smaybee 	 */
19593897Smaybee 	rw_enter(&zp->z_name_lock, RW_WRITER);
19603897Smaybee 
19613897Smaybee 	/*
19623897Smaybee 	 * Grab a lock on the parent pointer to make sure we play well
1963789Sahrens 	 * with the treewalk and directory rename code.
1964789Sahrens 	 */
1965789Sahrens 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1966789Sahrens 
1967789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
19681544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1969*11935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
19703461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1971*11935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
1972*11935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, dzp);
19738227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1974789Sahrens 	if (error) {
1975789Sahrens 		rw_exit(&zp->z_parent_lock);
19763897Smaybee 		rw_exit(&zp->z_name_lock);
1977789Sahrens 		zfs_dirent_unlock(dl);
1978789Sahrens 		VN_RELE(vp);
19798227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
19802113Sahrens 			dmu_tx_wait(tx);
19812113Sahrens 			dmu_tx_abort(tx);
1982789Sahrens 			goto top;
1983789Sahrens 		}
19842113Sahrens 		dmu_tx_abort(tx);
1985789Sahrens 		ZFS_EXIT(zfsvfs);
1986789Sahrens 		return (error);
1987789Sahrens 	}
1988789Sahrens 
19895331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
19905331Samw 
19915331Samw 	if (error == 0) {
19925331Samw 		uint64_t txtype = TX_RMDIR;
19935331Samw 		if (flags & FIGNORECASE)
19945331Samw 			txtype |= TX_CI;
19955331Samw 		zfs_log_remove(zilog, tx, txtype, dzp, name);
19965331Samw 	}
1997789Sahrens 
1998789Sahrens 	dmu_tx_commit(tx);
1999789Sahrens 
2000789Sahrens 	rw_exit(&zp->z_parent_lock);
20013897Smaybee 	rw_exit(&zp->z_name_lock);
2002789Sahrens out:
2003789Sahrens 	zfs_dirent_unlock(dl);
2004789Sahrens 
2005789Sahrens 	VN_RELE(vp);
2006789Sahrens 
2007789Sahrens 	ZFS_EXIT(zfsvfs);
2008789Sahrens 	return (error);
2009789Sahrens }
2010789Sahrens 
2011789Sahrens /*
2012789Sahrens  * Read as many directory entries as will fit into the provided
2013789Sahrens  * buffer from the given directory cursor position (specified in
2014789Sahrens  * the uio structure.
2015789Sahrens  *
2016789Sahrens  *	IN:	vp	- vnode of directory to read.
2017789Sahrens  *		uio	- structure supplying read location, range info,
2018789Sahrens  *			  and return buffer.
2019789Sahrens  *		cr	- credentials of caller.
20205331Samw  *		ct	- caller context
20215331Samw  *		flags	- case flags
2022789Sahrens  *
2023789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
2024789Sahrens  *		eofp	- set to true if end-of-file detected.
2025789Sahrens  *
2026789Sahrens  *	RETURN:	0 if success
2027789Sahrens  *		error code if failure
2028789Sahrens  *
2029789Sahrens  * Timestamps:
2030789Sahrens  *	vp - atime updated
2031789Sahrens  *
2032789Sahrens  * Note that the low 4 bits of the cookie returned by zap is always zero.
2033789Sahrens  * This allows us to use the low range for "special" directory entries:
2034789Sahrens  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
2035789Sahrens  * we use the offset 2 for the '.zfs' directory.
2036789Sahrens  */
2037789Sahrens /* ARGSUSED */
2038789Sahrens static int
20395331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
20405331Samw     caller_context_t *ct, int flags)
2041789Sahrens {
2042789Sahrens 	znode_t		*zp = VTOZ(vp);
2043789Sahrens 	iovec_t		*iovp;
20445331Samw 	edirent_t	*eodp;
2045789Sahrens 	dirent64_t	*odp;
2046789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2047869Sperrin 	objset_t	*os;
2048789Sahrens 	caddr_t		outbuf;
2049789Sahrens 	size_t		bufsize;
2050789Sahrens 	zap_cursor_t	zc;
2051789Sahrens 	zap_attribute_t	zap;
2052789Sahrens 	uint_t		bytes_wanted;
2053789Sahrens 	uint64_t	offset; /* must be unsigned; checks for < 1 */
2054*11935SMark.Shellenbaum@Sun.COM 	uint64_t	parent;
2055789Sahrens 	int		local_eof;
2056869Sperrin 	int		outcount;
2057869Sperrin 	int		error;
2058869Sperrin 	uint8_t		prefetch;
20595663Sck153898 	boolean_t	check_sysattrs;
2060789Sahrens 
20615367Sahrens 	ZFS_ENTER(zfsvfs);
20625367Sahrens 	ZFS_VERIFY_ZP(zp);
2063789Sahrens 
2064*11935SMark.Shellenbaum@Sun.COM 	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2065*11935SMark.Shellenbaum@Sun.COM 	    &parent, sizeof (parent))) != 0) {
2066*11935SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
2067*11935SMark.Shellenbaum@Sun.COM 		return (error);
2068*11935SMark.Shellenbaum@Sun.COM 	}
2069*11935SMark.Shellenbaum@Sun.COM 
2070789Sahrens 	/*
2071789Sahrens 	 * If we are not given an eof variable,
2072789Sahrens 	 * use a local one.
2073789Sahrens 	 */
2074789Sahrens 	if (eofp == NULL)
2075789Sahrens 		eofp = &local_eof;
2076789Sahrens 
2077789Sahrens 	/*
2078789Sahrens 	 * Check for valid iov_len.
2079789Sahrens 	 */
2080789Sahrens 	if (uio->uio_iov->iov_len <= 0) {
2081789Sahrens 		ZFS_EXIT(zfsvfs);
2082789Sahrens 		return (EINVAL);
2083789Sahrens 	}
2084789Sahrens 
2085789Sahrens 	/*
2086789Sahrens 	 * Quit if directory has been removed (posix)
2087789Sahrens 	 */
20883461Sahrens 	if ((*eofp = zp->z_unlinked) != 0) {
2089789Sahrens 		ZFS_EXIT(zfsvfs);
2090789Sahrens 		return (0);
2091789Sahrens 	}
2092789Sahrens 
2093869Sperrin 	error = 0;
2094869Sperrin 	os = zfsvfs->z_os;
2095869Sperrin 	offset = uio->uio_loffset;
2096869Sperrin 	prefetch = zp->z_zn_prefetch;
2097869Sperrin 
2098789Sahrens 	/*
2099789Sahrens 	 * Initialize the iterator cursor.
2100789Sahrens 	 */
2101789Sahrens 	if (offset <= 3) {
2102789Sahrens 		/*
2103789Sahrens 		 * Start iteration from the beginning of the directory.
2104789Sahrens 		 */
2105869Sperrin 		zap_cursor_init(&zc, os, zp->z_id);
2106789Sahrens 	} else {
2107789Sahrens 		/*
2108789Sahrens 		 * The offset is a serialized cursor.
2109789Sahrens 		 */
2110869Sperrin 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2111789Sahrens 	}
2112789Sahrens 
2113789Sahrens 	/*
2114789Sahrens 	 * Get space to change directory entries into fs independent format.
2115789Sahrens 	 */
2116789Sahrens 	iovp = uio->uio_iov;
2117789Sahrens 	bytes_wanted = iovp->iov_len;
2118789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2119789Sahrens 		bufsize = bytes_wanted;
2120789Sahrens 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
2121789Sahrens 		odp = (struct dirent64 *)outbuf;
2122789Sahrens 	} else {
2123789Sahrens 		bufsize = bytes_wanted;
2124789Sahrens 		odp = (struct dirent64 *)iovp->iov_base;
2125789Sahrens 	}
21265331Samw 	eodp = (struct edirent *)odp;
2127789Sahrens 
2128789Sahrens 	/*
21297757SJanice.Chang@Sun.COM 	 * If this VFS supports the system attribute view interface; and
21307757SJanice.Chang@Sun.COM 	 * we're looking at an extended attribute directory; and we care
21317757SJanice.Chang@Sun.COM 	 * about normalization conflicts on this vfs; then we must check
21327757SJanice.Chang@Sun.COM 	 * for normalization conflicts with the sysattr name space.
21335663Sck153898 	 */
21347757SJanice.Chang@Sun.COM 	check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
21355663Sck153898 	    (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
21365663Sck153898 	    (flags & V_RDDIR_ENTFLAGS);
21375663Sck153898 
21385663Sck153898 	/*
2139789Sahrens 	 * Transform to file-system independent format
2140789Sahrens 	 */
2141789Sahrens 	outcount = 0;
2142789Sahrens 	while (outcount < bytes_wanted) {
21433912Slling 		ino64_t objnum;
21443912Slling 		ushort_t reclen;
21453912Slling 		off64_t *next;
21463912Slling 
2147789Sahrens 		/*
2148789Sahrens 		 * Special case `.', `..', and `.zfs'.
2149789Sahrens 		 */
2150789Sahrens 		if (offset == 0) {
2151789Sahrens 			(void) strcpy(zap.za_name, ".");
21525331Samw 			zap.za_normalization_conflict = 0;
21533912Slling 			objnum = zp->z_id;
2154789Sahrens 		} else if (offset == 1) {
2155789Sahrens 			(void) strcpy(zap.za_name, "..");
21565331Samw 			zap.za_normalization_conflict = 0;
2157*11935SMark.Shellenbaum@Sun.COM 			objnum = parent;
2158789Sahrens 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
2159789Sahrens 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
21605331Samw 			zap.za_normalization_conflict = 0;
21613912Slling 			objnum = ZFSCTL_INO_ROOT;
2162789Sahrens 		} else {
2163789Sahrens 			/*
2164789Sahrens 			 * Grab next entry.
2165789Sahrens 			 */
2166789Sahrens 			if (error = zap_cursor_retrieve(&zc, &zap)) {
2167789Sahrens 				if ((*eofp = (error == ENOENT)) != 0)
2168789Sahrens 					break;
2169789Sahrens 				else
2170789Sahrens 					goto update;
2171789Sahrens 			}
2172789Sahrens 
2173789Sahrens 			if (zap.za_integer_length != 8 ||
2174789Sahrens 			    zap.za_num_integers != 1) {
2175789Sahrens 				cmn_err(CE_WARN, "zap_readdir: bad directory "
2176789Sahrens 				    "entry, obj = %lld, offset = %lld\n",
2177789Sahrens 				    (u_longlong_t)zp->z_id,
2178789Sahrens 				    (u_longlong_t)offset);
2179789Sahrens 				error = ENXIO;
2180789Sahrens 				goto update;
2181789Sahrens 			}
21823912Slling 
21833912Slling 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
21843912Slling 			/*
21853912Slling 			 * MacOS X can extract the object type here such as:
21863912Slling 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
21873912Slling 			 */
21885663Sck153898 
21895663Sck153898 			if (check_sysattrs && !zap.za_normalization_conflict) {
21905663Sck153898 				zap.za_normalization_conflict =
21915663Sck153898 				    xattr_sysattr_casechk(zap.za_name);
21925663Sck153898 			}
2193789Sahrens 		}
21945331Samw 
21959749STim.Haley@Sun.COM 		if (flags & V_RDDIR_ACCFILTER) {
21969749STim.Haley@Sun.COM 			/*
21979749STim.Haley@Sun.COM 			 * If we have no access at all, don't include
21989749STim.Haley@Sun.COM 			 * this entry in the returned information
21999749STim.Haley@Sun.COM 			 */
22009749STim.Haley@Sun.COM 			znode_t	*ezp;
22019749STim.Haley@Sun.COM 			if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
22029749STim.Haley@Sun.COM 				goto skip_entry;
22039749STim.Haley@Sun.COM 			if (!zfs_has_access(ezp, cr)) {
22049749STim.Haley@Sun.COM 				VN_RELE(ZTOV(ezp));
22059749STim.Haley@Sun.COM 				goto skip_entry;
22069749STim.Haley@Sun.COM 			}
22079749STim.Haley@Sun.COM 			VN_RELE(ZTOV(ezp));
22089749STim.Haley@Sun.COM 		}
22099749STim.Haley@Sun.COM 
22105331Samw 		if (flags & V_RDDIR_ENTFLAGS)
22115331Samw 			reclen = EDIRENT_RECLEN(strlen(zap.za_name));
22125331Samw 		else
22135331Samw 			reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2214789Sahrens 
2215789Sahrens 		/*
2216789Sahrens 		 * Will this entry fit in the buffer?
2217789Sahrens 		 */
22183912Slling 		if (outcount + reclen > bufsize) {
2219789Sahrens 			/*
2220789Sahrens 			 * Did we manage to fit anything in the buffer?
2221789Sahrens 			 */
2222789Sahrens 			if (!outcount) {
2223789Sahrens 				error = EINVAL;
2224789Sahrens 				goto update;
2225789Sahrens 			}
2226789Sahrens 			break;
2227789Sahrens 		}
22285331Samw 		if (flags & V_RDDIR_ENTFLAGS) {
22295331Samw 			/*
22305331Samw 			 * Add extended flag entry:
22315331Samw 			 */
22325331Samw 			eodp->ed_ino = objnum;
22335331Samw 			eodp->ed_reclen = reclen;
22345331Samw 			/* NOTE: ed_off is the offset for the *next* entry */
22355331Samw 			next = &(eodp->ed_off);
22365331Samw 			eodp->ed_eflags = zap.za_normalization_conflict ?
22375331Samw 			    ED_CASE_CONFLICT : 0;
22385331Samw 			(void) strncpy(eodp->ed_name, zap.za_name,
22395331Samw 			    EDIRENT_NAMELEN(reclen));
22405331Samw 			eodp = (edirent_t *)((intptr_t)eodp + reclen);
22415331Samw 		} else {
22425331Samw 			/*
22435331Samw 			 * Add normal entry:
22445331Samw 			 */
22455331Samw 			odp->d_ino = objnum;
22465331Samw 			odp->d_reclen = reclen;
22475331Samw 			/* NOTE: d_off is the offset for the *next* entry */
22485331Samw 			next = &(odp->d_off);
22495331Samw 			(void) strncpy(odp->d_name, zap.za_name,
22505331Samw 			    DIRENT64_NAMELEN(reclen));
22515331Samw 			odp = (dirent64_t *)((intptr_t)odp + reclen);
22525331Samw 		}
22533912Slling 		outcount += reclen;
2254789Sahrens 
2255789Sahrens 		ASSERT(outcount <= bufsize);
2256789Sahrens 
2257789Sahrens 		/* Prefetch znode */
2258869Sperrin 		if (prefetch)
22593912Slling 			dmu_prefetch(os, objnum, 0, 0);
2260789Sahrens 
22619749STim.Haley@Sun.COM 	skip_entry:
2262789Sahrens 		/*
2263789Sahrens 		 * Move to the next entry, fill in the previous offset.
2264789Sahrens 		 */
2265789Sahrens 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2266789Sahrens 			zap_cursor_advance(&zc);
2267789Sahrens 			offset = zap_cursor_serialize(&zc);
2268789Sahrens 		} else {
2269789Sahrens 			offset += 1;
2270789Sahrens 		}
2271789Sahrens 		*next = offset;
2272789Sahrens 	}
2273869Sperrin 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2274789Sahrens 
2275789Sahrens 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2276789Sahrens 		iovp->iov_base += outcount;
2277789Sahrens 		iovp->iov_len -= outcount;
2278789Sahrens 		uio->uio_resid -= outcount;
2279789Sahrens 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2280789Sahrens 		/*
2281789Sahrens 		 * Reset the pointer.
2282789Sahrens 		 */
2283789Sahrens 		offset = uio->uio_loffset;
2284789Sahrens 	}
2285789Sahrens 
2286789Sahrens update:
2287885Sahrens 	zap_cursor_fini(&zc);
2288789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2289789Sahrens 		kmem_free(outbuf, bufsize);
2290789Sahrens 
2291789Sahrens 	if (error == ENOENT)
2292789Sahrens 		error = 0;
2293789Sahrens 
2294789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2295789Sahrens 
2296789Sahrens 	uio->uio_loffset = offset;
2297789Sahrens 	ZFS_EXIT(zfsvfs);
2298789Sahrens 	return (error);
2299789Sahrens }
2300789Sahrens 
23014720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4;
23024720Sfr157268 
2303789Sahrens static int
23045331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2305789Sahrens {
2306789Sahrens 	znode_t	*zp = VTOZ(vp);
2307789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2308789Sahrens 
23091773Seschrock 	/*
23101773Seschrock 	 * Regardless of whether this is required for standards conformance,
23111773Seschrock 	 * this is the logical behavior when fsync() is called on a file with
23121773Seschrock 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
23131773Seschrock 	 * going to be pushed out as part of the zil_commit().
23141773Seschrock 	 */
23151773Seschrock 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
23161773Seschrock 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
23175331Samw 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
23181773Seschrock 
23194720Sfr157268 	(void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
23204720Sfr157268 
23215367Sahrens 	ZFS_ENTER(zfsvfs);
23225367Sahrens 	ZFS_VERIFY_ZP(zp);
23232638Sperrin 	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
2324789Sahrens 	ZFS_EXIT(zfsvfs);
2325789Sahrens 	return (0);
2326789Sahrens }
2327789Sahrens 
23285331Samw 
2329789Sahrens /*
2330789Sahrens  * Get the requested file attributes and place them in the provided
2331789Sahrens  * vattr structure.
2332789Sahrens  *
2333789Sahrens  *	IN:	vp	- vnode of file.
2334789Sahrens  *		vap	- va_mask identifies requested attributes.
23355331Samw  *			  If AT_XVATTR set, then optional attrs are requested
23365331Samw  *		flags	- ATTR_NOACLCHECK (CIFS server context)
2337789Sahrens  *		cr	- credentials of caller.
23385331Samw  *		ct	- caller context
2339789Sahrens  *
2340789Sahrens  *	OUT:	vap	- attribute values.
2341789Sahrens  *
2342789Sahrens  *	RETURN:	0 (always succeeds)
2343789Sahrens  */
2344789Sahrens /* ARGSUSED */
2345789Sahrens static int
23465331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
23475331Samw     caller_context_t *ct)
2348789Sahrens {
2349789Sahrens 	znode_t *zp = VTOZ(vp);
2350789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
23515331Samw 	int	error = 0;
23524543Smarks 	uint64_t links;
2353*11935SMark.Shellenbaum@Sun.COM 	uint64_t mtime[2], ctime[2];
23545331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
23555331Samw 	xoptattr_t *xoap = NULL;
23565331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2357*11935SMark.Shellenbaum@Sun.COM 	sa_bulk_attr_t bulk[2];
2358*11935SMark.Shellenbaum@Sun.COM 	int count = 0;
2359789Sahrens 
23605367Sahrens 	ZFS_ENTER(zfsvfs);
23615367Sahrens 	ZFS_VERIFY_ZP(zp);
2362*11935SMark.Shellenbaum@Sun.COM 
2363*11935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2364*11935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2365*11935SMark.Shellenbaum@Sun.COM 
2366*11935SMark.Shellenbaum@Sun.COM 	if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2367*11935SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
2368*11935SMark.Shellenbaum@Sun.COM 		return (error);
2369*11935SMark.Shellenbaum@Sun.COM 	}
2370789Sahrens 
23715331Samw 	/*
23725331Samw 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
23735331Samw 	 * Also, if we are the owner don't bother, since owner should
23745331Samw 	 * always be allowed to read basic attributes of file.
23755331Samw 	 */
2376*11935SMark.Shellenbaum@Sun.COM 	if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) && (zp->z_uid != crgetuid(cr))) {
23775331Samw 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
23785331Samw 		    skipaclchk, cr)) {
23795331Samw 			ZFS_EXIT(zfsvfs);
23805331Samw 			return (error);
23815331Samw 		}
23825331Samw 	}
23835331Samw 
2384789Sahrens 	/*
2385789Sahrens 	 * Return all attributes.  It's cheaper to provide the answer
2386789Sahrens 	 * than to determine whether we were asked the question.
2387789Sahrens 	 */
2388789Sahrens 
23899774SRay.Hassan@Sun.COM 	mutex_enter(&zp->z_lock);
2390789Sahrens 	vap->va_type = vp->v_type;
2391*11935SMark.Shellenbaum@Sun.COM 	vap->va_mode = zp->z_mode & MODEMASK;
2392*11935SMark.Shellenbaum@Sun.COM 	vap->va_uid = zp->z_uid;
2393*11935SMark.Shellenbaum@Sun.COM 	vap->va_gid = zp->z_gid;
2394789Sahrens 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2395789Sahrens 	vap->va_nodeid = zp->z_id;
23964543Smarks 	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2397*11935SMark.Shellenbaum@Sun.COM 		links = zp->z_links + 1;
23984543Smarks 	else
2399*11935SMark.Shellenbaum@Sun.COM 		links = zp->z_links;
24004543Smarks 	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
2401*11935SMark.Shellenbaum@Sun.COM 	vap->va_size = zp->z_size;
24021816Smarks 	vap->va_rdev = vp->v_rdev;
2403789Sahrens 	vap->va_seq = zp->z_seq;
2404789Sahrens 
24055331Samw 	/*
24065331Samw 	 * Add in any requested optional attributes and the create time.
24075331Samw 	 * Also set the corresponding bits in the returned attribute bitmap.
24085331Samw 	 */
24095331Samw 	if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
24105331Samw 		if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
24115331Samw 			xoap->xoa_archive =
2412*11935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_ARCHIVE) != 0);
24135331Samw 			XVA_SET_RTN(xvap, XAT_ARCHIVE);
24145331Samw 		}
24155331Samw 
24165331Samw 		if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
24175331Samw 			xoap->xoa_readonly =
2418*11935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_READONLY) != 0);
24195331Samw 			XVA_SET_RTN(xvap, XAT_READONLY);
24205331Samw 		}
24215331Samw 
24225331Samw 		if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
24235331Samw 			xoap->xoa_system =
2424*11935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_SYSTEM) != 0);
24255331Samw 			XVA_SET_RTN(xvap, XAT_SYSTEM);
24265331Samw 		}
24275331Samw 
24285331Samw 		if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
24295331Samw 			xoap->xoa_hidden =
2430*11935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_HIDDEN) != 0);
24315331Samw 			XVA_SET_RTN(xvap, XAT_HIDDEN);
24325331Samw 		}
24335331Samw 
24345331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
24355331Samw 			xoap->xoa_nounlink =
2436*11935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NOUNLINK) != 0);
24375331Samw 			XVA_SET_RTN(xvap, XAT_NOUNLINK);
24385331Samw 		}
24395331Samw 
24405331Samw 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
24415331Samw 			xoap->xoa_immutable =
2442*11935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
24435331Samw 			XVA_SET_RTN(xvap, XAT_IMMUTABLE);
24445331Samw 		}
24455331Samw 
24465331Samw 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
24475331Samw 			xoap->xoa_appendonly =
2448*11935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_APPENDONLY) != 0);
24495331Samw 			XVA_SET_RTN(xvap, XAT_APPENDONLY);
24505331Samw 		}
24515331Samw 
24525331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
24535331Samw 			xoap->xoa_nodump =
2454*11935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NODUMP) != 0);
24555331Samw 			XVA_SET_RTN(xvap, XAT_NODUMP);
24565331Samw 		}
24575331Samw 
24585331Samw 		if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
24595331Samw 			xoap->xoa_opaque =
2460*11935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_OPAQUE) != 0);
24615331Samw 			XVA_SET_RTN(xvap, XAT_OPAQUE);
24625331Samw 		}
24635331Samw 
24645331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
24655331Samw 			xoap->xoa_av_quarantined =
2466*11935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
24675331Samw 			XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
24685331Samw 		}
24695331Samw 
24705331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
24715331Samw 			xoap->xoa_av_modified =
2472*11935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
24735331Samw 			XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
24745331Samw 		}
24755331Samw 
24765331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2477*11935SMark.Shellenbaum@Sun.COM 		    vp->v_type == VREG) {
2478*11935SMark.Shellenbaum@Sun.COM 			zfs_sa_get_scanstamp(zp, xvap);
24795331Samw 		}
24805331Samw 
24815331Samw 		if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2482*11935SMark.Shellenbaum@Sun.COM 			uint64_t times[2];
2483*11935SMark.Shellenbaum@Sun.COM 
2484*11935SMark.Shellenbaum@Sun.COM 			(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2485*11935SMark.Shellenbaum@Sun.COM 			    times, sizeof (times));
2486*11935SMark.Shellenbaum@Sun.COM 			ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
24875331Samw 			XVA_SET_RTN(xvap, XAT_CREATETIME);
24885331Samw 		}
248910793Sdai.ngo@sun.com 
249010793Sdai.ngo@sun.com 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2491*11935SMark.Shellenbaum@Sun.COM 			xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
249210793Sdai.ngo@sun.com 			XVA_SET_RTN(xvap, XAT_REPARSE);
249310793Sdai.ngo@sun.com 		}
24945331Samw 	}
24955331Samw 
2496*11935SMark.Shellenbaum@Sun.COM 	ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2497*11935SMark.Shellenbaum@Sun.COM 	ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2498*11935SMark.Shellenbaum@Sun.COM 	ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2499789Sahrens 
2500789Sahrens 	mutex_exit(&zp->z_lock);
2501789Sahrens 
2502*11935SMark.Shellenbaum@Sun.COM 	sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2503789Sahrens 
2504789Sahrens 	if (zp->z_blksz == 0) {
2505789Sahrens 		/*
2506789Sahrens 		 * Block size hasn't been set; suggest maximal I/O transfers.
2507789Sahrens 		 */
2508789Sahrens 		vap->va_blksize = zfsvfs->z_max_blksz;
2509789Sahrens 	}
2510789Sahrens 
2511789Sahrens 	ZFS_EXIT(zfsvfs);
2512789Sahrens 	return (0);
2513789Sahrens }
2514789Sahrens 
2515789Sahrens /*
2516789Sahrens  * Set the file attributes to the values contained in the
2517789Sahrens  * vattr structure.
2518789Sahrens  *
2519789Sahrens  *	IN:	vp	- vnode of file to be modified.
2520789Sahrens  *		vap	- new attribute values.
25215331Samw  *			  If AT_XVATTR set, then optional attrs are being set
2522789Sahrens  *		flags	- ATTR_UTIME set if non-default time values provided.
25235331Samw  *			- ATTR_NOACLCHECK (CIFS context only).
2524789Sahrens  *		cr	- credentials of caller.
25255331Samw  *		ct	- caller context
2526789Sahrens  *
2527789Sahrens  *	RETURN:	0 if success
2528789Sahrens  *		error code if failure
2529789Sahrens  *
2530789Sahrens  * Timestamps:
2531789Sahrens  *	vp - ctime updated, mtime updated if size changed.
2532789Sahrens  */
2533789Sahrens /* ARGSUSED */
2534789Sahrens static int
2535789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2536789Sahrens 	caller_context_t *ct)
2537789Sahrens {
25385326Sek110237 	znode_t		*zp = VTOZ(vp);
2539789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
25405326Sek110237 	zilog_t		*zilog;
2541789Sahrens 	dmu_tx_t	*tx;
25421878Smaybee 	vattr_t		oldva;
25438190SMark.Shellenbaum@Sun.COM 	xvattr_t	tmpxvattr;
2544789Sahrens 	uint_t		mask = vap->va_mask;
25451878Smaybee 	uint_t		saved_mask;
25462796Smarks 	int		trim_mask = 0;
2547789Sahrens 	uint64_t	new_mode;
25489179SMark.Shellenbaum@Sun.COM 	uint64_t	new_uid, new_gid;
2549*11935SMark.Shellenbaum@Sun.COM 	uint64_t	xattr_obj = 0;
2550*11935SMark.Shellenbaum@Sun.COM 	uint64_t	mtime[2], ctime[2];
25511231Smarks 	znode_t		*attrzp;
2552789Sahrens 	int		need_policy = FALSE;
2553*11935SMark.Shellenbaum@Sun.COM 	int		err, err2;
25545331Samw 	zfs_fuid_info_t *fuidp = NULL;
25555331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
25565331Samw 	xoptattr_t	*xoap;
25575824Smarks 	zfs_acl_t	*aclp = NULL;
25585331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2559*11935SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied = B_FALSE;
2560*11935SMark.Shellenbaum@Sun.COM 	sa_bulk_attr_t	bulk[7], xattr_bulk[7];
2561*11935SMark.Shellenbaum@Sun.COM 	int		count = 0, xattr_count = 0;
2562789Sahrens 
2563789Sahrens 	if (mask == 0)
2564789Sahrens 		return (0);
2565789Sahrens 
2566789Sahrens 	if (mask & AT_NOSET)
2567789Sahrens 		return (EINVAL);
2568789Sahrens 
25695367Sahrens 	ZFS_ENTER(zfsvfs);
25705367Sahrens 	ZFS_VERIFY_ZP(zp);
25715331Samw 
25725331Samw 	zilog = zfsvfs->z_log;
25735331Samw 
25745331Samw 	/*
25755331Samw 	 * Make sure that if we have ephemeral uid/gid or xvattr specified
25765331Samw 	 * that file system is at proper version level
25775331Samw 	 */
25785331Samw 
25795331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
25805331Samw 	    (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
25815331Samw 	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
25825386Stimh 	    (mask & AT_XVATTR))) {
25835386Stimh 		ZFS_EXIT(zfsvfs);
25845331Samw 		return (EINVAL);
25855386Stimh 	}
25865386Stimh 
25875386Stimh 	if (mask & AT_SIZE && vp->v_type == VDIR) {
25885386Stimh 		ZFS_EXIT(zfsvfs);
2589789Sahrens 		return (EISDIR);
25905386Stimh 	}
25915386Stimh 
25925386Stimh 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
25935386Stimh 		ZFS_EXIT(zfsvfs);
25941308Smarks 		return (EINVAL);
25955386Stimh 	}
25961308Smarks 
25975331Samw 	/*
25985331Samw 	 * If this is an xvattr_t, then get a pointer to the structure of
25995331Samw 	 * optional attributes.  If this is NULL, then we have a vattr_t.
26005331Samw 	 */
26015331Samw 	xoap = xva_getxoptattr(xvap);
26025331Samw 
26038190SMark.Shellenbaum@Sun.COM 	xva_init(&tmpxvattr);
26048190SMark.Shellenbaum@Sun.COM 
26055331Samw 	/*
26065331Samw 	 * Immutable files can only alter immutable bit and atime
26075331Samw 	 */
2608*11935SMark.Shellenbaum@Sun.COM 	if ((zp->z_pflags & ZFS_IMMUTABLE) &&
26095331Samw 	    ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
26105386Stimh 	    ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
26115386Stimh 		ZFS_EXIT(zfsvfs);
26125331Samw 		return (EPERM);
26135386Stimh 	}
26145386Stimh 
2615*11935SMark.Shellenbaum@Sun.COM 	if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
26165386Stimh 		ZFS_EXIT(zfsvfs);
26175331Samw 		return (EPERM);
26185386Stimh 	}
2619789Sahrens 
26206064Smarks 	/*
26216064Smarks 	 * Verify timestamps doesn't overflow 32 bits.
26226064Smarks 	 * ZFS can handle large timestamps, but 32bit syscalls can't
26236064Smarks 	 * handle times greater than 2039.  This check should be removed
26246064Smarks 	 * once large timestamps are fully supported.
26256064Smarks 	 */
26266064Smarks 	if (mask & (AT_ATIME | AT_MTIME)) {
26276064Smarks 		if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
26286064Smarks 		    ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
26296064Smarks 			ZFS_EXIT(zfsvfs);
26306064Smarks 			return (EOVERFLOW);
26316064Smarks 		}
26326064Smarks 	}
26336064Smarks 
2634789Sahrens top:
26351231Smarks 	attrzp = NULL;
2636789Sahrens 
26379981STim.Haley@Sun.COM 	/* Can this be moved to before the top label? */
2638789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2639789Sahrens 		ZFS_EXIT(zfsvfs);
2640789Sahrens 		return (EROFS);
2641789Sahrens 	}
2642789Sahrens 
2643789Sahrens 	/*
2644789Sahrens 	 * First validate permissions
2645789Sahrens 	 */
2646789Sahrens 
2647789Sahrens 	if (mask & AT_SIZE) {
26485331Samw 		err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2649789Sahrens 		if (err) {
2650789Sahrens 			ZFS_EXIT(zfsvfs);
2651789Sahrens 			return (err);
2652789Sahrens 		}
26531878Smaybee 		/*
26541878Smaybee 		 * XXX - Note, we are not providing any open
26551878Smaybee 		 * mode flags here (like FNDELAY), so we may
26561878Smaybee 		 * block if there are locks present... this
26571878Smaybee 		 * should be addressed in openat().
26581878Smaybee 		 */
26596992Smaybee 		/* XXX - would it be OK to generate a log record here? */
26606992Smaybee 		err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
26611878Smaybee 		if (err) {
26621878Smaybee 			ZFS_EXIT(zfsvfs);
26631878Smaybee 			return (err);
26641878Smaybee 		}
2665789Sahrens 	}
2666789Sahrens 
26675331Samw 	if (mask & (AT_ATIME|AT_MTIME) ||
26685331Samw 	    ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
26695331Samw 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
26705331Samw 	    XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
26715331Samw 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2672*11935SMark.Shellenbaum@Sun.COM 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
26735331Samw 		need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
26745331Samw 		    skipaclchk, cr);
2675*11935SMark.Shellenbaum@Sun.COM 	}
2676789Sahrens 
2677789Sahrens 	if (mask & (AT_UID|AT_GID)) {
2678789Sahrens 		int	idmask = (mask & (AT_UID|AT_GID));
2679789Sahrens 		int	take_owner;
2680789Sahrens 		int	take_group;
2681789Sahrens 
2682789Sahrens 		/*
2683913Smarks 		 * NOTE: even if a new mode is being set,
2684913Smarks 		 * we may clear S_ISUID/S_ISGID bits.
2685913Smarks 		 */
2686913Smarks 
2687913Smarks 		if (!(mask & AT_MODE))
2688*11935SMark.Shellenbaum@Sun.COM 			vap->va_mode = zp->z_mode;
2689913Smarks 
2690913Smarks 		/*
2691789Sahrens 		 * Take ownership or chgrp to group we are a member of
2692789Sahrens 		 */
2693789Sahrens 
2694789Sahrens 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
26955331Samw 		take_group = (mask & AT_GID) &&
26965331Samw 		    zfs_groupmember(zfsvfs, vap->va_gid, cr);
2697789Sahrens 
2698789Sahrens 		/*
2699789Sahrens 		 * If both AT_UID and AT_GID are set then take_owner and
2700789Sahrens 		 * take_group must both be set in order to allow taking
2701789Sahrens 		 * ownership.
2702789Sahrens 		 *
2703789Sahrens 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2704789Sahrens 		 *
2705789Sahrens 		 */
2706789Sahrens 
2707789Sahrens 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2708789Sahrens 		    ((idmask == AT_UID) && take_owner) ||
2709789Sahrens 		    ((idmask == AT_GID) && take_group)) {
27105331Samw 			if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
27115331Samw 			    skipaclchk, cr) == 0) {
2712789Sahrens 				/*
2713789Sahrens 				 * Remove setuid/setgid for non-privileged users
2714789Sahrens 				 */
27151115Smarks 				secpolicy_setid_clear(vap, cr);
27162796Smarks 				trim_mask = (mask & (AT_UID|AT_GID));
2717789Sahrens 			} else {
2718789Sahrens 				need_policy =  TRUE;
2719789Sahrens 			}
2720789Sahrens 		} else {
2721789Sahrens 			need_policy =  TRUE;
2722789Sahrens 		}
2723789Sahrens 	}
2724789Sahrens 
27252796Smarks 	mutex_enter(&zp->z_lock);
2726*11935SMark.Shellenbaum@Sun.COM 	oldva.va_mode = zp->z_mode;
2727*11935SMark.Shellenbaum@Sun.COM 	oldva.va_uid = zp->z_uid;
2728*11935SMark.Shellenbaum@Sun.COM 	oldva.va_gid = zp->z_gid;
27295331Samw 	if (mask & AT_XVATTR) {
27308190SMark.Shellenbaum@Sun.COM 		/*
27318190SMark.Shellenbaum@Sun.COM 		 * Update xvattr mask to include only those attributes
27328190SMark.Shellenbaum@Sun.COM 		 * that are actually changing.
27338190SMark.Shellenbaum@Sun.COM 		 *
27348190SMark.Shellenbaum@Sun.COM 		 * the bits will be restored prior to actually setting
27358190SMark.Shellenbaum@Sun.COM 		 * the attributes so the caller thinks they were set.
27368190SMark.Shellenbaum@Sun.COM 		 */
27378190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
27388190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_appendonly !=
2739*11935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
27408190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27418190SMark.Shellenbaum@Sun.COM 			} else {
27428190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_APPENDONLY);
27438190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
27448190SMark.Shellenbaum@Sun.COM 			}
27458190SMark.Shellenbaum@Sun.COM 		}
27468190SMark.Shellenbaum@Sun.COM 
27478190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
27488190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_nounlink !=
2749*11935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
27508190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27518190SMark.Shellenbaum@Sun.COM 			} else {
27528190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_NOUNLINK);
27538190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
27548190SMark.Shellenbaum@Sun.COM 			}
27558190SMark.Shellenbaum@Sun.COM 		}
27568190SMark.Shellenbaum@Sun.COM 
27578190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
27588190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_immutable !=
2759*11935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
27608190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27618190SMark.Shellenbaum@Sun.COM 			} else {
27628190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
27638190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
27648190SMark.Shellenbaum@Sun.COM 			}
27658190SMark.Shellenbaum@Sun.COM 		}
27668190SMark.Shellenbaum@Sun.COM 
27678190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
27688190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_nodump !=
2769*11935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NODUMP) != 0)) {
27708190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27718190SMark.Shellenbaum@Sun.COM 			} else {
27728190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_NODUMP);
27738190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
27748190SMark.Shellenbaum@Sun.COM 			}
27758190SMark.Shellenbaum@Sun.COM 		}
27768190SMark.Shellenbaum@Sun.COM 
27778190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
27788190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_av_modified !=
2779*11935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
27808190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27818190SMark.Shellenbaum@Sun.COM 			} else {
27828190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
27838190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
27848190SMark.Shellenbaum@Sun.COM 			}
27858190SMark.Shellenbaum@Sun.COM 		}
27868190SMark.Shellenbaum@Sun.COM 
27878190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
27888190SMark.Shellenbaum@Sun.COM 			if ((vp->v_type != VREG &&
27898190SMark.Shellenbaum@Sun.COM 			    xoap->xoa_av_quarantined) ||
27908190SMark.Shellenbaum@Sun.COM 			    xoap->xoa_av_quarantined !=
2791*11935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
27928190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27938190SMark.Shellenbaum@Sun.COM 			} else {
27948190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
27958190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
27968190SMark.Shellenbaum@Sun.COM 			}
27978190SMark.Shellenbaum@Sun.COM 		}
27988190SMark.Shellenbaum@Sun.COM 
279910793Sdai.ngo@sun.com 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
280010793Sdai.ngo@sun.com 			mutex_exit(&zp->z_lock);
280110793Sdai.ngo@sun.com 			ZFS_EXIT(zfsvfs);
280210793Sdai.ngo@sun.com 			return (EPERM);
280310793Sdai.ngo@sun.com 		}
280410793Sdai.ngo@sun.com 
28058190SMark.Shellenbaum@Sun.COM 		if (need_policy == FALSE &&
28068190SMark.Shellenbaum@Sun.COM 		    (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
28078190SMark.Shellenbaum@Sun.COM 		    XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
28085331Samw 			need_policy = TRUE;
28095331Samw 		}
28105331Samw 	}
28115331Samw 
28122796Smarks 	mutex_exit(&zp->z_lock);
28132796Smarks 
28142796Smarks 	if (mask & AT_MODE) {
28155331Samw 		if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
28162796Smarks 			err = secpolicy_setid_setsticky_clear(vp, vap,
28172796Smarks 			    &oldva, cr);
28182796Smarks 			if (err) {
28192796Smarks 				ZFS_EXIT(zfsvfs);
28202796Smarks 				return (err);
28212796Smarks 			}
28222796Smarks 			trim_mask |= AT_MODE;
28232796Smarks 		} else {
28242796Smarks 			need_policy = TRUE;
28252796Smarks 		}
28262796Smarks 	}
2827789Sahrens 
2828789Sahrens 	if (need_policy) {
28291115Smarks 		/*
28301115Smarks 		 * If trim_mask is set then take ownership
28312796Smarks 		 * has been granted or write_acl is present and user
28322796Smarks 		 * has the ability to modify mode.  In that case remove
28332796Smarks 		 * UID|GID and or MODE from mask so that
28341115Smarks 		 * secpolicy_vnode_setattr() doesn't revoke it.
28351115Smarks 		 */
28362796Smarks 
28372796Smarks 		if (trim_mask) {
28382796Smarks 			saved_mask = vap->va_mask;
28392796Smarks 			vap->va_mask &= ~trim_mask;
28402796Smarks 		}
2841789Sahrens 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
28425331Samw 		    (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2843789Sahrens 		if (err) {
2844789Sahrens 			ZFS_EXIT(zfsvfs);
2845789Sahrens 			return (err);
2846789Sahrens 		}
28471115Smarks 
28481115Smarks 		if (trim_mask)
28492796Smarks 			vap->va_mask |= saved_mask;
2850789Sahrens 	}
2851789Sahrens 
2852789Sahrens 	/*
2853789Sahrens 	 * secpolicy_vnode_setattr, or take ownership may have
2854789Sahrens 	 * changed va_mask
2855789Sahrens 	 */
2856789Sahrens 	mask = vap->va_mask;
2857789Sahrens 
2858*11935SMark.Shellenbaum@Sun.COM 	if ((mask & (AT_UID | AT_GID))) {
2859*11935SMark.Shellenbaum@Sun.COM 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xattr_obj,
2860*11935SMark.Shellenbaum@Sun.COM 		    sizeof (xattr_obj));
2861*11935SMark.Shellenbaum@Sun.COM 
2862*11935SMark.Shellenbaum@Sun.COM 		if (xattr_obj) {
2863*11935SMark.Shellenbaum@Sun.COM 			err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
28649396SMatthew.Ahrens@Sun.COM 			if (err)
2865*11935SMark.Shellenbaum@Sun.COM 				goto out2;
28669179SMark.Shellenbaum@Sun.COM 		}
28679179SMark.Shellenbaum@Sun.COM 		if (mask & AT_UID) {
28689179SMark.Shellenbaum@Sun.COM 			new_uid = zfs_fuid_create(zfsvfs,
28699179SMark.Shellenbaum@Sun.COM 			    (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2870*11935SMark.Shellenbaum@Sun.COM 			if (vap->va_uid != zp->z_uid &&
2871*11935SMark.Shellenbaum@Sun.COM 			    zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
28729396SMatthew.Ahrens@Sun.COM 				err = EDQUOT;
2873*11935SMark.Shellenbaum@Sun.COM 				goto out2;
28749396SMatthew.Ahrens@Sun.COM 			}
28751231Smarks 		}
28769396SMatthew.Ahrens@Sun.COM 
28779179SMark.Shellenbaum@Sun.COM 		if (mask & AT_GID) {
28789179SMark.Shellenbaum@Sun.COM 			new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
28799179SMark.Shellenbaum@Sun.COM 			    cr, ZFS_GROUP, &fuidp);
2880*11935SMark.Shellenbaum@Sun.COM 			if (new_gid != zp->z_gid &&
2881*11935SMark.Shellenbaum@Sun.COM 			    zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
28829396SMatthew.Ahrens@Sun.COM 				err = EDQUOT;
2883*11935SMark.Shellenbaum@Sun.COM 				goto out2;
28849179SMark.Shellenbaum@Sun.COM 			}
28859179SMark.Shellenbaum@Sun.COM 		}
28861231Smarks 	}
2887*11935SMark.Shellenbaum@Sun.COM 	tx = dmu_tx_create(zfsvfs->z_os);
2888*11935SMark.Shellenbaum@Sun.COM 
2889*11935SMark.Shellenbaum@Sun.COM 	if (mask & AT_MODE) {
2890*11935SMark.Shellenbaum@Sun.COM 		uint64_t pmode = zp->z_mode;
2891*11935SMark.Shellenbaum@Sun.COM 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2892*11935SMark.Shellenbaum@Sun.COM 
2893*11935SMark.Shellenbaum@Sun.COM 		if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
2894*11935SMark.Shellenbaum@Sun.COM 			goto out;
2895*11935SMark.Shellenbaum@Sun.COM 
2896*11935SMark.Shellenbaum@Sun.COM 		if (!zp->z_is_sa && ZFS_EXTERNAL_ACL(zp)) {
2897*11935SMark.Shellenbaum@Sun.COM 			/*
2898*11935SMark.Shellenbaum@Sun.COM 			 * Are we upgrading ACL from old V0 format
2899*11935SMark.Shellenbaum@Sun.COM 			 * to V1 format?
2900*11935SMark.Shellenbaum@Sun.COM 			 */
2901*11935SMark.Shellenbaum@Sun.COM 			if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
2902*11935SMark.Shellenbaum@Sun.COM 			    ZNODE_ACL_VERSION(zp) ==
2903*11935SMark.Shellenbaum@Sun.COM 			    ZFS_ACL_VERSION_INITIAL) {
2904*11935SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_free(tx,
2905*11935SMark.Shellenbaum@Sun.COM 				    ZFS_EXTERNAL_ACL(zp), 0,
2906*11935SMark.Shellenbaum@Sun.COM 				    DMU_OBJECT_END);
2907*11935SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2908*11935SMark.Shellenbaum@Sun.COM 				    0, aclp->z_acl_bytes);
2909*11935SMark.Shellenbaum@Sun.COM 			} else {
2910*11935SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_write(tx, ZFS_EXTERNAL_ACL(zp), 0,
2911*11935SMark.Shellenbaum@Sun.COM 				    aclp->z_acl_bytes);
2912*11935SMark.Shellenbaum@Sun.COM 			}
2913*11935SMark.Shellenbaum@Sun.COM 		} else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2914*11935SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2915*11935SMark.Shellenbaum@Sun.COM 			    0, aclp->z_acl_bytes);
2916*11935SMark.Shellenbaum@Sun.COM 		}
2917*11935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2918*11935SMark.Shellenbaum@Sun.COM 	} else {
2919*11935SMark.Shellenbaum@Sun.COM 		if ((mask & AT_XVATTR) &&
2920*11935SMark.Shellenbaum@Sun.COM 		    XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2921*11935SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2922*11935SMark.Shellenbaum@Sun.COM 		else
2923*11935SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2924*11935SMark.Shellenbaum@Sun.COM 	}
2925*11935SMark.Shellenbaum@Sun.COM 
2926*11935SMark.Shellenbaum@Sun.COM 	if (attrzp) {
2927*11935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
2928*11935SMark.Shellenbaum@Sun.COM 	}
2929*11935SMark.Shellenbaum@Sun.COM 
2930*11935SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
2931*11935SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
2932*11935SMark.Shellenbaum@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
2933*11935SMark.Shellenbaum@Sun.COM 
2934*11935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
29351231Smarks 
29368227SNeil.Perrin@Sun.COM 	err = dmu_tx_assign(tx, TXG_NOWAIT);
2937789Sahrens 	if (err) {
29389396SMatthew.Ahrens@Sun.COM 		if (err == ERESTART)
29392113Sahrens 			dmu_tx_wait(tx);
29409396SMatthew.Ahrens@Sun.COM 		goto out;
2941789Sahrens 	}
2942789Sahrens 
2943*11935SMark.Shellenbaum@Sun.COM 	count = 0;
2944789Sahrens 	/*
2945789Sahrens 	 * Set each attribute requested.
2946789Sahrens 	 * We group settings according to the locks they need to acquire.
2947789Sahrens 	 *
2948789Sahrens 	 * Note: you cannot set ctime directly, although it will be
2949789Sahrens 	 * updated as a side-effect of calling this function.
2950789Sahrens 	 */
2951789Sahrens 
2952789Sahrens 	mutex_enter(&zp->z_lock);
2953789Sahrens 
2954*11935SMark.Shellenbaum@Sun.COM 	if (attrzp)
2955*11935SMark.Shellenbaum@Sun.COM 		mutex_enter(&attrzp->z_lock);
2956*11935SMark.Shellenbaum@Sun.COM 
2957*11935SMark.Shellenbaum@Sun.COM 	if (mask & AT_UID) {
2958*11935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
2959*11935SMark.Shellenbaum@Sun.COM 		    &new_uid, sizeof (new_uid));
2960*11935SMark.Shellenbaum@Sun.COM 		zp->z_uid = zfs_fuid_map_id(zfsvfs, new_uid, cr, ZFS_OWNER);
2961*11935SMark.Shellenbaum@Sun.COM 		if (attrzp) {
2962*11935SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2963*11935SMark.Shellenbaum@Sun.COM 			    SA_ZPL_UID(zfsvfs), NULL, &new_uid,
2964*11935SMark.Shellenbaum@Sun.COM 			    sizeof (new_uid));
2965*11935SMark.Shellenbaum@Sun.COM 			attrzp->z_gid = zp->z_uid;
2966*11935SMark.Shellenbaum@Sun.COM 		}
2967*11935SMark.Shellenbaum@Sun.COM 	}
2968*11935SMark.Shellenbaum@Sun.COM 
2969*11935SMark.Shellenbaum@Sun.COM 	if (mask & AT_GID) {
2970*11935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
2971*11935SMark.Shellenbaum@Sun.COM 		    &new_gid, sizeof (new_gid));
2972*11935SMark.Shellenbaum@Sun.COM 		zp->z_gid = zfs_fuid_map_id(zfsvfs, new_gid, cr, ZFS_GROUP);
2973*11935SMark.Shellenbaum@Sun.COM 		if (attrzp) {
2974*11935SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2975*11935SMark.Shellenbaum@Sun.COM 			    SA_ZPL_GID(zfsvfs), NULL, &new_gid,
2976*11935SMark.Shellenbaum@Sun.COM 			    sizeof (new_gid));
2977*11935SMark.Shellenbaum@Sun.COM 			attrzp->z_gid = zp->z_gid;
2978*11935SMark.Shellenbaum@Sun.COM 		}
2979*11935SMark.Shellenbaum@Sun.COM 	}
2980*11935SMark.Shellenbaum@Sun.COM 
2981789Sahrens 	if (mask & AT_MODE) {
29825824Smarks 		mutex_enter(&zp->z_acl_lock);
2983*11935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
2984*11935SMark.Shellenbaum@Sun.COM 		    &new_mode, sizeof (new_mode));
2985*11935SMark.Shellenbaum@Sun.COM 		zp->z_mode = new_mode;
29869179SMark.Shellenbaum@Sun.COM 		err = zfs_aclset_common(zp, aclp, cr, tx);
2987789Sahrens 		ASSERT3U(err, ==, 0);
298810143STim.Haley@Sun.COM 		zp->z_acl_cached = aclp;
298910143STim.Haley@Sun.COM 		aclp = NULL;
29905824Smarks 		mutex_exit(&zp->z_acl_lock);
2991789Sahrens 	}
2992789Sahrens 
29931231Smarks 	if (attrzp)
2994*11935SMark.Shellenbaum@Sun.COM 		mutex_exit(&attrzp->z_lock);
2995*11935SMark.Shellenbaum@Sun.COM 
2996*11935SMark.Shellenbaum@Sun.COM 	if (mask & AT_ATIME) {
2997*11935SMark.Shellenbaum@Sun.COM 		ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
2998*11935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
2999*11935SMark.Shellenbaum@Sun.COM 		    &zp->z_atime, sizeof (zp->z_atime));
30001231Smarks 	}
30011231Smarks 
3002*11935SMark.Shellenbaum@Sun.COM 	if (mask & AT_MTIME) {
3003*11935SMark.Shellenbaum@Sun.COM 		ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3004*11935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3005*11935SMark.Shellenbaum@Sun.COM 		    mtime, sizeof (mtime));
30061231Smarks 	}
30071231Smarks 
30086992Smaybee 	/* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3009*11935SMark.Shellenbaum@Sun.COM 	if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3010*11935SMark.Shellenbaum@Sun.COM 		if (!(mask & AT_MTIME))
3011*11935SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3012*11935SMark.Shellenbaum@Sun.COM 			    NULL, mtime, sizeof (mtime));
3013*11935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3014*11935SMark.Shellenbaum@Sun.COM 		    &ctime, sizeof (ctime));
3015*11935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3016*11935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
3017*11935SMark.Shellenbaum@Sun.COM 	} else if (mask != 0) {
3018*11935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3019*11935SMark.Shellenbaum@Sun.COM 		    &ctime, sizeof (ctime));
3020*11935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3021*11935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
3022*11935SMark.Shellenbaum@Sun.COM 		if (attrzp) {
3023*11935SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3024*11935SMark.Shellenbaum@Sun.COM 			    SA_ZPL_CTIME(zfsvfs), NULL,
3025*11935SMark.Shellenbaum@Sun.COM 			    &ctime, sizeof (ctime));
3026*11935SMark.Shellenbaum@Sun.COM 			zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3027*11935SMark.Shellenbaum@Sun.COM 			    mtime, ctime, B_TRUE);
3028*11935SMark.Shellenbaum@Sun.COM 		}
3029*11935SMark.Shellenbaum@Sun.COM 	}
30305331Samw 	/*
30315331Samw 	 * Do this after setting timestamps to prevent timestamp
30325331Samw 	 * update from toggling bit
30335331Samw 	 */
30345331Samw 
30355331Samw 	if (xoap && (mask & AT_XVATTR)) {
30368190SMark.Shellenbaum@Sun.COM 
30378190SMark.Shellenbaum@Sun.COM 		/*
30388190SMark.Shellenbaum@Sun.COM 		 * restore trimmed off masks
30398190SMark.Shellenbaum@Sun.COM 		 * so that return masks can be set for caller.
30408190SMark.Shellenbaum@Sun.COM 		 */
30418190SMark.Shellenbaum@Sun.COM 
30428190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
30438190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_APPENDONLY);
30448190SMark.Shellenbaum@Sun.COM 		}
30458190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
30468190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_NOUNLINK);
30478190SMark.Shellenbaum@Sun.COM 		}
30488190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
30498190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_IMMUTABLE);
30508190SMark.Shellenbaum@Sun.COM 		}
30518190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
30528190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_NODUMP);
30538190SMark.Shellenbaum@Sun.COM 		}
30548190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
30558190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
30568190SMark.Shellenbaum@Sun.COM 		}
30578190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
30588190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
30598190SMark.Shellenbaum@Sun.COM 		}
30608190SMark.Shellenbaum@Sun.COM 
3061*11935SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
30625331Samw 			ASSERT(vp->v_type == VREG);
30635331Samw 
3064*11935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3065*11935SMark.Shellenbaum@Sun.COM 		    &zp->z_pflags, sizeof (zp->z_pflags));
3066*11935SMark.Shellenbaum@Sun.COM 		zfs_xvattr_set(zp, xvap, tx);
30675331Samw 	}
3068789Sahrens 
30699179SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
30709179SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
30719179SMark.Shellenbaum@Sun.COM 
30721878Smaybee 	if (mask != 0)
30735331Samw 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
30745331Samw 
3075789Sahrens 	mutex_exit(&zp->z_lock);
3076789Sahrens 
30779396SMatthew.Ahrens@Sun.COM out:
3078*11935SMark.Shellenbaum@Sun.COM 	if (err == 0 && attrzp) {
3079*11935SMark.Shellenbaum@Sun.COM 		err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3080*11935SMark.Shellenbaum@Sun.COM 		    xattr_count, tx);
3081*11935SMark.Shellenbaum@Sun.COM 		ASSERT(err2 == 0);
3082*11935SMark.Shellenbaum@Sun.COM 	}
3083*11935SMark.Shellenbaum@Sun.COM 
30841231Smarks 	if (attrzp)
30851231Smarks 		VN_RELE(ZTOV(attrzp));
308610143STim.Haley@Sun.COM 	if (aclp)
308710143STim.Haley@Sun.COM 		zfs_acl_free(aclp);
308810143STim.Haley@Sun.COM 
30899396SMatthew.Ahrens@Sun.COM 	if (fuidp) {
30909396SMatthew.Ahrens@Sun.COM 		zfs_fuid_info_free(fuidp);
30919396SMatthew.Ahrens@Sun.COM 		fuidp = NULL;
30929396SMatthew.Ahrens@Sun.COM 	}
30939396SMatthew.Ahrens@Sun.COM 
3094*11935SMark.Shellenbaum@Sun.COM 	if (err) {
30959396SMatthew.Ahrens@Sun.COM 		dmu_tx_abort(tx);
3096*11935SMark.Shellenbaum@Sun.COM 		if (err == ERESTART)
3097*11935SMark.Shellenbaum@Sun.COM 			goto top;
3098*11935SMark.Shellenbaum@Sun.COM 	} else {
3099*11935SMark.Shellenbaum@Sun.COM 		err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
31009396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
3101*11935SMark.Shellenbaum@Sun.COM 	}
3102*11935SMark.Shellenbaum@Sun.COM 
3103*11935SMark.Shellenbaum@Sun.COM 
3104*11935SMark.Shellenbaum@Sun.COM out2:
3105789Sahrens 	ZFS_EXIT(zfsvfs);
3106789Sahrens 	return (err);
3107789Sahrens }
3108789Sahrens 
31093271Smaybee typedef struct zfs_zlock {
31103271Smaybee 	krwlock_t	*zl_rwlock;	/* lock we acquired */
31113271Smaybee 	znode_t		*zl_znode;	/* znode we held */
31123271Smaybee 	struct zfs_zlock *zl_next;	/* next in list */
31133271Smaybee } zfs_zlock_t;
31143271Smaybee 
31153271Smaybee /*
31163271Smaybee  * Drop locks and release vnodes that were held by zfs_rename_lock().
31173271Smaybee  */
31183271Smaybee static void
31193271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp)
31203271Smaybee {
31213271Smaybee 	zfs_zlock_t *zl;
31223271Smaybee 
31233271Smaybee 	while ((zl = *zlpp) != NULL) {
31243271Smaybee 		if (zl->zl_znode != NULL)
31253271Smaybee 			VN_RELE(ZTOV(zl->zl_znode));
31263271Smaybee 		rw_exit(zl->zl_rwlock);
31273271Smaybee 		*zlpp = zl->zl_next;
31283271Smaybee 		kmem_free(zl, sizeof (*zl));
31293271Smaybee 	}
31303271Smaybee }
31313271Smaybee 
3132789Sahrens /*
3133789Sahrens  * Search back through the directory tree, using the ".." entries.
3134789Sahrens  * Lock each directory in the chain to prevent concurrent renames.
3135789Sahrens  * Fail any attempt to move a directory into one of its own descendants.
3136789Sahrens  * XXX - z_parent_lock can overlap with map or grow locks
3137789Sahrens  */
3138789Sahrens static int
3139789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3140789Sahrens {
3141789Sahrens 	zfs_zlock_t	*zl;
31423638Sbillm 	znode_t		*zp = tdzp;
3143789Sahrens 	uint64_t	rootid = zp->z_zfsvfs->z_root;
3144*11935SMark.Shellenbaum@Sun.COM 	uint64_t	oidp = zp->z_id;
3145789Sahrens 	krwlock_t	*rwlp = &szp->z_parent_lock;
3146789Sahrens 	krw_t		rw = RW_WRITER;
3147789Sahrens 
3148789Sahrens 	/*
3149789Sahrens 	 * First pass write-locks szp and compares to zp->z_id.
3150789Sahrens 	 * Later passes read-lock zp and compare to zp->z_parent.
3151789Sahrens 	 */
3152789Sahrens 	do {
31533271Smaybee 		if (!rw_tryenter(rwlp, rw)) {
31543271Smaybee 			/*
31553271Smaybee 			 * Another thread is renaming in this path.
31563271Smaybee 			 * Note that if we are a WRITER, we don't have any
31573271Smaybee 			 * parent_locks held yet.
31583271Smaybee 			 */
31593271Smaybee 			if (rw == RW_READER && zp->z_id > szp->z_id) {
31603271Smaybee 				/*
31613271Smaybee 				 * Drop our locks and restart
31623271Smaybee 				 */
31633271Smaybee 				zfs_rename_unlock(&zl);
31643271Smaybee 				*zlpp = NULL;
31653271Smaybee 				zp = tdzp;
3166*11935SMark.Shellenbaum@Sun.COM 				oidp = zp->z_id;
31673271Smaybee 				rwlp = &szp->z_parent_lock;
31683271Smaybee 				rw = RW_WRITER;
31693271Smaybee 				continue;
31703271Smaybee 			} else {
31713271Smaybee 				/*
31723271Smaybee 				 * Wait for other thread to drop its locks
31733271Smaybee 				 */
31743271Smaybee 				rw_enter(rwlp, rw);
31753271Smaybee 			}
31763271Smaybee 		}
31773271Smaybee 
3178789Sahrens 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3179789Sahrens 		zl->zl_rwlock = rwlp;
3180789Sahrens 		zl->zl_znode = NULL;
3181789Sahrens 		zl->zl_next = *zlpp;
3182789Sahrens 		*zlpp = zl;
3183789Sahrens 
3184*11935SMark.Shellenbaum@Sun.COM 		if (oidp == szp->z_id)		/* We're a descendant of szp */
3185789Sahrens 			return (EINVAL);
3186789Sahrens 
3187*11935SMark.Shellenbaum@Sun.COM 		if (oidp == rootid)		/* We've hit the top */
3188789Sahrens 			return (0);
3189789Sahrens 
3190789Sahrens 		if (rw == RW_READER) {		/* i.e. not the first pass */
3191*11935SMark.Shellenbaum@Sun.COM 			int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3192789Sahrens 			if (error)
3193789Sahrens 				return (error);
3194789Sahrens 			zl->zl_znode = zp;
3195789Sahrens 		}
3196*11935SMark.Shellenbaum@Sun.COM 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
3197*11935SMark.Shellenbaum@Sun.COM 		    &oidp, sizeof (oidp));
3198789Sahrens 		rwlp = &zp->z_parent_lock;
3199789Sahrens 		rw = RW_READER;
3200789Sahrens 
3201789Sahrens 	} while (zp->z_id != sdzp->z_id);
3202789Sahrens 
3203789Sahrens 	return (0);
3204789Sahrens }
3205789Sahrens 
3206789Sahrens /*
3207789Sahrens  * Move an entry from the provided source directory to the target
3208789Sahrens  * directory.  Change the entry name as indicated.
3209789Sahrens  *
3210789Sahrens  *	IN:	sdvp	- Source directory containing the "old entry".
3211789Sahrens  *		snm	- Old entry name.
3212789Sahrens  *		tdvp	- Target directory to contain the "new entry".
3213789Sahrens  *		tnm	- New entry name.
3214789Sahrens  *		cr	- credentials of caller.
32155331Samw  *		ct	- caller context
32165331Samw  *		flags	- case flags
3217789Sahrens  *
3218789Sahrens  *	RETURN:	0 if success
3219789Sahrens  *		error code if failure
3220789Sahrens  *
3221789Sahrens  * Timestamps:
3222789Sahrens  *	sdvp,tdvp - ctime|mtime updated
3223789Sahrens  */
32245331Samw /*ARGSUSED*/
3225789Sahrens static int
32265331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
32275331Samw     caller_context_t *ct, int flags)
3228789Sahrens {
3229789Sahrens 	znode_t		*tdzp, *szp, *tzp;
3230789Sahrens 	znode_t		*sdzp = VTOZ(sdvp);
3231789Sahrens 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
32325326Sek110237 	zilog_t		*zilog;
3233789Sahrens 	vnode_t		*realvp;
3234789Sahrens 	zfs_dirlock_t	*sdl, *tdl;
3235789Sahrens 	dmu_tx_t	*tx;
3236789Sahrens 	zfs_zlock_t	*zl;
32375331Samw 	int		cmp, serr, terr;
32385331Samw 	int		error = 0;
32395331Samw 	int		zflg = 0;
3240789Sahrens 
32415367Sahrens 	ZFS_ENTER(zfsvfs);
32425367Sahrens 	ZFS_VERIFY_ZP(sdzp);
32435326Sek110237 	zilog = zfsvfs->z_log;
3244789Sahrens 
3245789Sahrens 	/*
3246789Sahrens 	 * Make sure we have the real vp for the target directory.
3247789Sahrens 	 */
32485331Samw 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3249789Sahrens 		tdvp = realvp;
3250789Sahrens 
3251789Sahrens 	if (tdvp->v_vfsp != sdvp->v_vfsp) {
3252789Sahrens 		ZFS_EXIT(zfsvfs);
3253789Sahrens 		return (EXDEV);
3254789Sahrens 	}
3255789Sahrens 
3256789Sahrens 	tdzp = VTOZ(tdvp);
32575367Sahrens 	ZFS_VERIFY_ZP(tdzp);
32585498Stimh 	if (zfsvfs->z_utf8 && u8_validate(tnm,
32595331Samw 	    strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
32605331Samw 		ZFS_EXIT(zfsvfs);
32615331Samw 		return (EILSEQ);
32625331Samw 	}
32635331Samw 
32645331Samw 	if (flags & FIGNORECASE)
32655331Samw 		zflg |= ZCILOOK;
32665331Samw 
3267789Sahrens top:
3268789Sahrens 	szp = NULL;
3269789Sahrens 	tzp = NULL;
3270789Sahrens 	zl = NULL;
3271789Sahrens 
3272789Sahrens 	/*
3273789Sahrens 	 * This is to prevent the creation of links into attribute space
3274789Sahrens 	 * by renaming a linked file into/outof an attribute directory.
3275789Sahrens 	 * See the comment in zfs_link() for why this is considered bad.
3276789Sahrens 	 */
3277*11935SMark.Shellenbaum@Sun.COM 	if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3278789Sahrens 		ZFS_EXIT(zfsvfs);
3279789Sahrens 		return (EINVAL);
3280789Sahrens 	}
3281789Sahrens 
3282789Sahrens 	/*
3283789Sahrens 	 * Lock source and target directory entries.  To prevent deadlock,
3284789Sahrens 	 * a lock ordering must be defined.  We lock the directory with
3285789Sahrens 	 * the smallest object id first, or if it's a tie, the one with
3286789Sahrens 	 * the lexically first name.
3287789Sahrens 	 */
3288789Sahrens 	if (sdzp->z_id < tdzp->z_id) {
3289789Sahrens 		cmp = -1;
3290789Sahrens 	} else if (sdzp->z_id > tdzp->z_id) {
3291789Sahrens 		cmp = 1;
3292789Sahrens 	} else {
32935331Samw 		/*
32945331Samw 		 * First compare the two name arguments without
32955331Samw 		 * considering any case folding.
32965331Samw 		 */
32975331Samw 		int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
32985331Samw 
32995331Samw 		cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
33005498Stimh 		ASSERT(error == 0 || !zfsvfs->z_utf8);
3301789Sahrens 		if (cmp == 0) {
3302789Sahrens 			/*
3303789Sahrens 			 * POSIX: "If the old argument and the new argument
3304789Sahrens 			 * both refer to links to the same existing file,
3305789Sahrens 			 * the rename() function shall return successfully
3306789Sahrens 			 * and perform no other action."
3307789Sahrens 			 */
3308789Sahrens 			ZFS_EXIT(zfsvfs);
3309789Sahrens 			return (0);
3310789Sahrens 		}
33115331Samw 		/*
33125331Samw 		 * If the file system is case-folding, then we may
33135331Samw 		 * have some more checking to do.  A case-folding file
33145331Samw 		 * system is either supporting mixed case sensitivity
33155331Samw 		 * access or is completely case-insensitive.  Note
33165331Samw 		 * that the file system is always case preserving.
33175331Samw 		 *
33185331Samw 		 * In mixed sensitivity mode case sensitive behavior
33195331Samw 		 * is the default.  FIGNORECASE must be used to
33205331Samw 		 * explicitly request case insensitive behavior.
33215331Samw 		 *
33225331Samw 		 * If the source and target names provided differ only
33235331Samw 		 * by case (e.g., a request to rename 'tim' to 'Tim'),
33245331Samw 		 * we will treat this as a special case in the
33255331Samw 		 * case-insensitive mode: as long as the source name
33265331Samw 		 * is an exact match, we will allow this to proceed as
33275331Samw 		 * a name-change request.
33285331Samw 		 */
33295498Stimh 		if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
33305498Stimh 		    (zfsvfs->z_case == ZFS_CASE_MIXED &&
33315498Stimh 		    flags & FIGNORECASE)) &&
33325331Samw 		    u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
33335331Samw 		    &error) == 0) {
33345331Samw 			/*
33355331Samw 			 * case preserving rename request, require exact
33365331Samw 			 * name matches
33375331Samw 			 */
33385331Samw 			zflg |= ZCIEXACT;
33395331Samw 			zflg &= ~ZCILOOK;
33405331Samw 		}
3341789Sahrens 	}
33425331Samw 
334311321SSanjeev.Bagewadi@Sun.COM 	/*
334411321SSanjeev.Bagewadi@Sun.COM 	 * If the source and destination directories are the same, we should
334511321SSanjeev.Bagewadi@Sun.COM 	 * grab the z_name_lock of that directory only once.
334611321SSanjeev.Bagewadi@Sun.COM 	 */
334711321SSanjeev.Bagewadi@Sun.COM 	if (sdzp == tdzp) {
334811321SSanjeev.Bagewadi@Sun.COM 		zflg |= ZHAVELOCK;
334911321SSanjeev.Bagewadi@Sun.COM 		rw_enter(&sdzp->z_name_lock, RW_READER);
335011321SSanjeev.Bagewadi@Sun.COM 	}
335111321SSanjeev.Bagewadi@Sun.COM 
3352789Sahrens 	if (cmp < 0) {
33535331Samw 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
33545331Samw 		    ZEXISTS | zflg, NULL, NULL);
33555331Samw 		terr = zfs_dirent_lock(&tdl,
33565331Samw 		    tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3357789Sahrens 	} else {
33585331Samw 		terr = zfs_dirent_lock(&tdl,
33595331Samw 		    tdzp, tnm, &tzp, zflg, NULL, NULL);
33605331Samw 		serr = zfs_dirent_lock(&sdl,
33615331Samw 		    sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
33625331Samw 		    NULL, NULL);
3363789Sahrens 	}
3364789Sahrens 
3365789Sahrens 	if (serr) {
3366789Sahrens 		/*
3367789Sahrens 		 * Source entry invalid or not there.
3368789Sahrens 		 */
3369789Sahrens 		if (!terr) {
3370789Sahrens 			zfs_dirent_unlock(tdl);
3371789Sahrens 			if (tzp)
3372789Sahrens 				VN_RELE(ZTOV(tzp));
3373789Sahrens 		}
337411321SSanjeev.Bagewadi@Sun.COM 
337511321SSanjeev.Bagewadi@Sun.COM 		if (sdzp == tdzp)
337611321SSanjeev.Bagewadi@Sun.COM 			rw_exit(&sdzp->z_name_lock);
337711321SSanjeev.Bagewadi@Sun.COM 
3378789Sahrens 		if (strcmp(snm, "..") == 0)
3379789Sahrens 			serr = EINVAL;
3380789Sahrens 		ZFS_EXIT(zfsvfs);
3381789Sahrens 		return (serr);
3382789Sahrens 	}
3383789Sahrens 	if (terr) {
3384789Sahrens 		zfs_dirent_unlock(sdl);
3385789Sahrens 		VN_RELE(ZTOV(szp));
338611321SSanjeev.Bagewadi@Sun.COM 
338711321SSanjeev.Bagewadi@Sun.COM 		if (sdzp == tdzp)
338811321SSanjeev.Bagewadi@Sun.COM 			rw_exit(&sdzp->z_name_lock);
338911321SSanjeev.Bagewadi@Sun.COM 
3390789Sahrens 		if (strcmp(tnm, "..") == 0)
3391789Sahrens 			terr = EINVAL;
3392789Sahrens 		ZFS_EXIT(zfsvfs);
3393789Sahrens 		return (terr);
3394789Sahrens 	}
3395789Sahrens 
3396789Sahrens 	/*
3397789Sahrens 	 * Must have write access at the source to remove the old entry
3398789Sahrens 	 * and write access at the target to create the new entry.
3399789Sahrens 	 * Note that if target and source are the same, this can be
3400789Sahrens 	 * done in a single check.
3401789Sahrens 	 */
3402789Sahrens 
3403789Sahrens 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3404789Sahrens 		goto out;
3405789Sahrens 
3406789Sahrens 	if (ZTOV(szp)->v_type == VDIR) {
3407789Sahrens 		/*
3408789Sahrens 		 * Check to make sure rename is valid.
3409789Sahrens 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3410789Sahrens 		 */
3411789Sahrens 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3412789Sahrens 			goto out;
3413789Sahrens 	}
3414789Sahrens 
3415789Sahrens 	/*
3416789Sahrens 	 * Does target exist?
3417789Sahrens 	 */
3418789Sahrens 	if (tzp) {
3419789Sahrens 		/*
3420789Sahrens 		 * Source and target must be the same type.
3421789Sahrens 		 */
3422789Sahrens 		if (ZTOV(szp)->v_type == VDIR) {
3423789Sahrens 			if (ZTOV(tzp)->v_type != VDIR) {
3424789Sahrens 				error = ENOTDIR;
3425789Sahrens 				goto out;
3426789Sahrens 			}
3427789Sahrens 		} else {
3428789Sahrens 			if (ZTOV(tzp)->v_type == VDIR) {
3429789Sahrens 				error = EISDIR;
3430789Sahrens 				goto out;
3431789Sahrens 			}
3432789Sahrens 		}
3433789Sahrens 		/*
3434789Sahrens 		 * POSIX dictates that when the source and target
3435789Sahrens 		 * entries refer to the same file object, rename
3436789Sahrens 		 * must do nothing and exit without error.
3437789Sahrens 		 */
3438789Sahrens 		if (szp->z_id == tzp->z_id) {
3439789Sahrens 			error = 0;
3440789Sahrens 			goto out;
3441789Sahrens 		}
3442789Sahrens 	}
3443789Sahrens 
34445331Samw 	vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3445789Sahrens 	if (tzp)
34465331Samw 		vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
34474863Spraks 
34484863Spraks 	/*
34494863Spraks 	 * notify the target directory if it is not the same
34504863Spraks 	 * as source directory.
34514863Spraks 	 */
34524863Spraks 	if (tdvp != sdvp) {
34535331Samw 		vnevent_rename_dest_dir(tdvp, ct);
34544863Spraks 	}
3455789Sahrens 
3456789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3457*11935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3458*11935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
34591544Seschrock 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
34601544Seschrock 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3461*11935SMark.Shellenbaum@Sun.COM 	if (sdzp != tdzp) {
3462*11935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3463*11935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, tdzp);
3464*11935SMark.Shellenbaum@Sun.COM 	}
3465*11935SMark.Shellenbaum@Sun.COM 	if (tzp) {
3466*11935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3467*11935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, tzp);
3468*11935SMark.Shellenbaum@Sun.COM 	}
3469*11935SMark.Shellenbaum@Sun.COM 
3470*11935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, szp);
34713461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
34728227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3473789Sahrens 	if (error) {
3474789Sahrens 		if (zl != NULL)
3475789Sahrens 			zfs_rename_unlock(&zl);
3476789Sahrens 		zfs_dirent_unlock(sdl);
3477789Sahrens 		zfs_dirent_unlock(tdl);
347811321SSanjeev.Bagewadi@Sun.COM 
347911321SSanjeev.Bagewadi@Sun.COM 		if (sdzp == tdzp)
348011321SSanjeev.Bagewadi@Sun.COM 			rw_exit(&sdzp->z_name_lock);
348111321SSanjeev.Bagewadi@Sun.COM 
3482789Sahrens 		VN_RELE(ZTOV(szp));
3483789Sahrens 		if (tzp)
3484789Sahrens 			VN_RELE(ZTOV(tzp));
34858227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
34862113Sahrens 			dmu_tx_wait(tx);
34872113Sahrens 			dmu_tx_abort(tx);
3488789Sahrens 			goto top;
3489789Sahrens 		}
34902113Sahrens 		dmu_tx_abort(tx);
3491789Sahrens 		ZFS_EXIT(zfsvfs);
3492789Sahrens 		return (error);
3493789Sahrens 	}
3494789Sahrens 
3495789Sahrens 	if (tzp)	/* Attempt to remove the existing target */
34965331Samw 		error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3497789Sahrens 
3498789Sahrens 	if (error == 0) {
3499789Sahrens 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3500789Sahrens 		if (error == 0) {
3501*11935SMark.Shellenbaum@Sun.COM 			szp->z_pflags |= ZFS_AV_MODIFIED;
3502*11935SMark.Shellenbaum@Sun.COM 
3503*11935SMark.Shellenbaum@Sun.COM 			error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3504*11935SMark.Shellenbaum@Sun.COM 			    (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3505*11935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error, ==, 0);
35065331Samw 
3507789Sahrens 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3508*11935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error, ==, 0);
35095331Samw 
35105331Samw 			zfs_log_rename(zilog, tx,
35115331Samw 			    TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0),
35125331Samw 			    sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
35136976Seschrock 
35146976Seschrock 			/* Update path information for the target vnode */
35156976Seschrock 			vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm));
3516789Sahrens 		}
3517789Sahrens 	}
3518789Sahrens 
3519789Sahrens 	dmu_tx_commit(tx);
3520789Sahrens out:
3521789Sahrens 	if (zl != NULL)
3522789Sahrens 		zfs_rename_unlock(&zl);
3523789Sahrens 
3524789Sahrens 	zfs_dirent_unlock(sdl);
3525789Sahrens 	zfs_dirent_unlock(tdl);
3526789Sahrens 
352711321SSanjeev.Bagewadi@Sun.COM 	if (sdzp == tdzp)
352811321SSanjeev.Bagewadi@Sun.COM 		rw_exit(&sdzp->z_name_lock);
352911321SSanjeev.Bagewadi@Sun.COM 
353011321SSanjeev.Bagewadi@Sun.COM 
3531789Sahrens 	VN_RELE(ZTOV(szp));
3532789Sahrens 	if (tzp)
3533789Sahrens 		VN_RELE(ZTOV(tzp));
3534789Sahrens 
3535789Sahrens 	ZFS_EXIT(zfsvfs);
3536789Sahrens 	return (error);
3537789Sahrens }
3538789Sahrens 
3539789Sahrens /*
3540789Sahrens  * Insert the indicated symbolic reference entry into the directory.
3541789Sahrens  *
3542789Sahrens  *	IN:	dvp	- Directory to contain new symbolic link.
3543789Sahrens  *		link	- Name for new symlink entry.
3544789Sahrens  *		vap	- Attributes of new entry.
3545789Sahrens  *		target	- Target path of new symlink.
3546789Sahrens  *		cr	- credentials of caller.
35475331Samw  *		ct	- caller context
35485331Samw  *		flags	- case flags
3549789Sahrens  *
3550789Sahrens  *	RETURN:	0 if success
3551789Sahrens  *		error code if failure
3552789Sahrens  *
3553789Sahrens  * Timestamps:
3554789Sahrens  *	dvp - ctime|mtime updated
3555789Sahrens  */
35565331Samw /*ARGSUSED*/
3557789Sahrens static int
35585331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
35595331Samw     caller_context_t *ct, int flags)
3560789Sahrens {
3561789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
3562789Sahrens 	zfs_dirlock_t	*dl;
3563789Sahrens 	dmu_tx_t	*tx;
3564789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
35655326Sek110237 	zilog_t		*zilog;
3566*11935SMark.Shellenbaum@Sun.COM 	uint64_t	len = strlen(link);
3567789Sahrens 	int		error;
35685331Samw 	int		zflg = ZNEW;
35699179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t	acl_ids;
35709179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
3571*11935SMark.Shellenbaum@Sun.COM 	uint64_t	txtype = TX_SYMLINK;
3572789Sahrens 
3573789Sahrens 	ASSERT(vap->va_type == VLNK);
3574789Sahrens 
35755367Sahrens 	ZFS_ENTER(zfsvfs);
35765367Sahrens 	ZFS_VERIFY_ZP(dzp);
35775326Sek110237 	zilog = zfsvfs->z_log;
35785331Samw 
35795498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
35805331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
35815331Samw 		ZFS_EXIT(zfsvfs);
35825331Samw 		return (EILSEQ);
35835331Samw 	}
35845331Samw 	if (flags & FIGNORECASE)
35855331Samw 		zflg |= ZCILOOK;
3586789Sahrens top:
35875331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3588789Sahrens 		ZFS_EXIT(zfsvfs);
3589789Sahrens 		return (error);
3590789Sahrens 	}
3591789Sahrens 
3592789Sahrens 	if (len > MAXPATHLEN) {
3593789Sahrens 		ZFS_EXIT(zfsvfs);
3594789Sahrens 		return (ENAMETOOLONG);
3595789Sahrens 	}
3596789Sahrens 
3597789Sahrens 	/*
3598789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3599789Sahrens 	 */
36005331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
36015331Samw 	if (error) {
3602789Sahrens 		ZFS_EXIT(zfsvfs);
3603789Sahrens 		return (error);
3604789Sahrens 	}
3605789Sahrens 
36069179SMark.Shellenbaum@Sun.COM 	VERIFY(0 == zfs_acl_ids_create(dzp, 0, vap, cr, NULL, &acl_ids));
36079396SMatthew.Ahrens@Sun.COM 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
36089396SMatthew.Ahrens@Sun.COM 		zfs_acl_ids_free(&acl_ids);
36099396SMatthew.Ahrens@Sun.COM 		zfs_dirent_unlock(dl);
36109396SMatthew.Ahrens@Sun.COM 		ZFS_EXIT(zfsvfs);
36119396SMatthew.Ahrens@Sun.COM 		return (EDQUOT);
36129396SMatthew.Ahrens@Sun.COM 	}
3613789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
36149179SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
3615789Sahrens 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
36161544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3617*11935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3618*11935SMark.Shellenbaum@Sun.COM 	    ZFS_SA_BASE_ATTR_SIZE + len);
3619*11935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3620*11935SMark.Shellenbaum@Sun.COM 	if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3621*11935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3622*11935SMark.Shellenbaum@Sun.COM 		    acl_ids.z_aclp->z_acl_bytes);
3623*11935SMark.Shellenbaum@Sun.COM 	}
36249396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
36259396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
36268227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3627789Sahrens 	if (error) {
36289179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
3629789Sahrens 		zfs_dirent_unlock(dl);
36308227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
36312113Sahrens 			dmu_tx_wait(tx);
36322113Sahrens 			dmu_tx_abort(tx);
3633789Sahrens 			goto top;
3634789Sahrens 		}
36352113Sahrens 		dmu_tx_abort(tx);
3636789Sahrens 		ZFS_EXIT(zfsvfs);
3637789Sahrens 		return (error);
3638789Sahrens 	}
3639789Sahrens 
3640789Sahrens 	/*
3641789Sahrens 	 * Create a new object for the symlink.
3642*11935SMark.Shellenbaum@Sun.COM 	 * for version 4 ZPL datsets the symlink will be an SA attribute
3643789Sahrens 	 */
3644*11935SMark.Shellenbaum@Sun.COM 
3645*11935SMark.Shellenbaum@Sun.COM 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3646*11935SMark.Shellenbaum@Sun.COM 
3647*11935SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
3648*11935SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
3649*11935SMark.Shellenbaum@Sun.COM 
3650*11935SMark.Shellenbaum@Sun.COM 	if (zp->z_is_sa)
3651*11935SMark.Shellenbaum@Sun.COM 		error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
3652*11935SMark.Shellenbaum@Sun.COM 		    link, len, tx);
3653*11935SMark.Shellenbaum@Sun.COM 	else
3654*11935SMark.Shellenbaum@Sun.COM 		zfs_sa_symlink(zp, link, len, tx);
3655*11935SMark.Shellenbaum@Sun.COM 
3656*11935SMark.Shellenbaum@Sun.COM 	zp->z_size = len;
3657*11935SMark.Shellenbaum@Sun.COM 	(void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
3658*11935SMark.Shellenbaum@Sun.COM 	    &zp->z_size, sizeof (zp->z_size), tx);
3659789Sahrens 	/*
3660789Sahrens 	 * Insert the new object into the directory.
3661789Sahrens 	 */
3662789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
3663*11935SMark.Shellenbaum@Sun.COM 
3664*11935SMark.Shellenbaum@Sun.COM 	if (flags & FIGNORECASE)
3665*11935SMark.Shellenbaum@Sun.COM 		txtype |= TX_CI;
3666*11935SMark.Shellenbaum@Sun.COM 	zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
36679179SMark.Shellenbaum@Sun.COM 
36689179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_free(&acl_ids);
3669789Sahrens 
3670789Sahrens 	dmu_tx_commit(tx);
3671789Sahrens 
3672789Sahrens 	zfs_dirent_unlock(dl);
3673789Sahrens 
3674789Sahrens 	VN_RELE(ZTOV(zp));
3675789Sahrens 
3676789Sahrens 	ZFS_EXIT(zfsvfs);
3677789Sahrens 	return (error);
3678789Sahrens }
3679789Sahrens 
3680789Sahrens /*
3681789Sahrens  * Return, in the buffer contained in the provided uio structure,
3682789Sahrens  * the symbolic path referred to by vp.
3683789Sahrens  *
3684789Sahrens  *	IN:	vp	- vnode of symbolic link.
3685789Sahrens  *		uoip	- structure to contain the link path.
3686789Sahrens  *		cr	- credentials of caller.
36875331Samw  *		ct	- caller context
3688789Sahrens  *
3689789Sahrens  *	OUT:	uio	- structure to contain the link path.
3690789Sahrens  *
3691789Sahrens  *	RETURN:	0 if success
3692789Sahrens  *		error code if failure
3693789Sahrens  *
3694789Sahrens  * Timestamps:
3695789Sahrens  *	vp - atime updated
3696789Sahrens  */
3697789Sahrens /* ARGSUSED */
3698789Sahrens static int
36995331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3700789Sahrens {
3701789Sahrens 	znode_t		*zp = VTOZ(vp);
3702789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3703789Sahrens 	int		error;
3704789Sahrens 
37055367Sahrens 	ZFS_ENTER(zfsvfs);
37065367Sahrens 	ZFS_VERIFY_ZP(zp);
3707789Sahrens 
3708*11935SMark.Shellenbaum@Sun.COM 	if (zp->z_is_sa)
3709*11935SMark.Shellenbaum@Sun.COM 		error = sa_lookup_uio(zp->z_sa_hdl,
3710*11935SMark.Shellenbaum@Sun.COM 		    SA_ZPL_SYMLINK(zfsvfs), uio);
3711*11935SMark.Shellenbaum@Sun.COM 	else
3712*11935SMark.Shellenbaum@Sun.COM 		error = zfs_sa_readlink(zp, uio);
3713789Sahrens 
3714789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3715*11935SMark.Shellenbaum@Sun.COM 
3716789Sahrens 	ZFS_EXIT(zfsvfs);
3717789Sahrens 	return (error);
3718789Sahrens }
3719789Sahrens 
3720789Sahrens /*
3721789Sahrens  * Insert a new entry into directory tdvp referencing svp.
3722789Sahrens  *
3723789Sahrens  *	IN:	tdvp	- Directory to contain new entry.
3724789Sahrens  *		svp	- vnode of new entry.
3725789Sahrens  *		name	- name of new entry.
3726789Sahrens  *		cr	- credentials of caller.
37275331Samw  *		ct	- caller context
3728789Sahrens  *
3729789Sahrens  *	RETURN:	0 if success
3730789Sahrens  *		error code if failure
3731789Sahrens  *
3732789Sahrens  * Timestamps:
3733789Sahrens  *	tdvp - ctime|mtime updated
3734789Sahrens  *	 svp - ctime updated
3735789Sahrens  */
3736789Sahrens /* ARGSUSED */
3737789Sahrens static int
37385331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
37395331Samw     caller_context_t *ct, int flags)
3740789Sahrens {
3741789Sahrens 	znode_t		*dzp = VTOZ(tdvp);
3742789Sahrens 	znode_t		*tzp, *szp;
3743789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
37445326Sek110237 	zilog_t		*zilog;
3745789Sahrens 	zfs_dirlock_t	*dl;
3746789Sahrens 	dmu_tx_t	*tx;
3747789Sahrens 	vnode_t		*realvp;
3748789Sahrens 	int		error;
37495331Samw 	int		zf = ZNEW;
3750789Sahrens 
3751789Sahrens 	ASSERT(tdvp->v_type == VDIR);
3752789Sahrens 
37535367Sahrens 	ZFS_ENTER(zfsvfs);
37545367Sahrens 	ZFS_VERIFY_ZP(dzp);
37555326Sek110237 	zilog = zfsvfs->z_log;
3756789Sahrens 
37575331Samw 	if (VOP_REALVP(svp, &realvp, ct) == 0)
3758789Sahrens 		svp = realvp;
3759789Sahrens 
3760789Sahrens 	if (svp->v_vfsp != tdvp->v_vfsp) {
3761789Sahrens 		ZFS_EXIT(zfsvfs);
3762789Sahrens 		return (EXDEV);
3763789Sahrens 	}
37645367Sahrens 	szp = VTOZ(svp);
37655367Sahrens 	ZFS_VERIFY_ZP(szp);
3766789Sahrens 
37675498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name,
37685331Samw 	    strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
37695331Samw 		ZFS_EXIT(zfsvfs);
37705331Samw 		return (EILSEQ);
37715331Samw 	}
37725331Samw 	if (flags & FIGNORECASE)
37735331Samw 		zf |= ZCILOOK;
37745331Samw 
3775789Sahrens top:
3776789Sahrens 	/*
3777789Sahrens 	 * We do not support links between attributes and non-attributes
3778789Sahrens 	 * because of the potential security risk of creating links
3779789Sahrens 	 * into "normal" file space in order to circumvent restrictions
3780789Sahrens 	 * imposed in attribute space.
3781789Sahrens 	 */
3782*11935SMark.Shellenbaum@Sun.COM 	if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
3783789Sahrens 		ZFS_EXIT(zfsvfs);
3784789Sahrens 		return (EINVAL);
3785789Sahrens 	}
3786789Sahrens 
3787789Sahrens 	/*
3788789Sahrens 	 * POSIX dictates that we return EPERM here.
3789789Sahrens 	 * Better choices include ENOTSUP or EISDIR.
3790789Sahrens 	 */
3791789Sahrens 	if (svp->v_type == VDIR) {
3792789Sahrens 		ZFS_EXIT(zfsvfs);
3793789Sahrens 		return (EPERM);
3794789Sahrens 	}
3795789Sahrens 
3796*11935SMark.Shellenbaum@Sun.COM 	if (szp->z_uid != crgetuid(cr) &&
3797789Sahrens 	    secpolicy_basic_link(cr) != 0) {
3798789Sahrens 		ZFS_EXIT(zfsvfs);
3799789Sahrens 		return (EPERM);
3800789Sahrens 	}
3801789Sahrens 
38025331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3803789Sahrens 		ZFS_EXIT(zfsvfs);
3804789Sahrens 		return (error);
3805789Sahrens 	}
3806789Sahrens 
3807789Sahrens 	/*
3808789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3809789Sahrens 	 */
38105331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
38115331Samw 	if (error) {
3812789Sahrens 		ZFS_EXIT(zfsvfs);
3813789Sahrens 		return (error);
3814789Sahrens 	}
3815789Sahrens 
3816789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3817*11935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
38181544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3819*11935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, szp);
3820*11935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, dzp);
38218227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3822789Sahrens 	if (error) {
3823789Sahrens 		zfs_dirent_unlock(dl);
38248227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
38252113Sahrens 			dmu_tx_wait(tx);
38262113Sahrens 			dmu_tx_abort(tx);
3827789Sahrens 			goto top;
3828789Sahrens 		}
38292113Sahrens 		dmu_tx_abort(tx);
3830789Sahrens 		ZFS_EXIT(zfsvfs);
3831789Sahrens 		return (error);
3832789Sahrens 	}
3833789Sahrens 
3834789Sahrens 	error = zfs_link_create(dl, szp, tx, 0);
3835789Sahrens 
38365331Samw 	if (error == 0) {
38375331Samw 		uint64_t txtype = TX_LINK;
38385331Samw 		if (flags & FIGNORECASE)
38395331Samw 			txtype |= TX_CI;
38405331Samw 		zfs_log_link(zilog, tx, txtype, dzp, szp, name);
38415331Samw 	}
3842789Sahrens 
3843789Sahrens 	dmu_tx_commit(tx);
3844789Sahrens 
3845789Sahrens 	zfs_dirent_unlock(dl);
3846789Sahrens 
38474863Spraks 	if (error == 0) {
38485331Samw 		vnevent_link(svp, ct);
38494863Spraks 	}
38504863Spraks 
3851789Sahrens 	ZFS_EXIT(zfsvfs);
3852789Sahrens 	return (error);
3853789Sahrens }
3854789Sahrens 
3855789Sahrens /*
3856789Sahrens  * zfs_null_putapage() is used when the file system has been force
3857789Sahrens  * unmounted. It just drops the pages.
3858789Sahrens  */
3859789Sahrens /* ARGSUSED */
3860789Sahrens static int
3861789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3862789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3863789Sahrens {
3864789Sahrens 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
3865789Sahrens 	return (0);
3866789Sahrens }
3867789Sahrens 
38682688Smaybee /*
38692688Smaybee  * Push a page out to disk, klustering if possible.
38702688Smaybee  *
38712688Smaybee  *	IN:	vp	- file to push page to.
38722688Smaybee  *		pp	- page to push.
38732688Smaybee  *		flags	- additional flags.
38742688Smaybee  *		cr	- credentials of caller.
38752688Smaybee  *
38762688Smaybee  *	OUT:	offp	- start of range pushed.
38772688Smaybee  *		lenp	- len of range pushed.
38782688Smaybee  *
38792688Smaybee  *	RETURN:	0 if success
38802688Smaybee  *		error code if failure
38812688Smaybee  *
38822688Smaybee  * NOTE: callers must have locked the page to be pushed.  On
38832688Smaybee  * exit, the page (and all other pages in the kluster) must be
38842688Smaybee  * unlocked.
38852688Smaybee  */
3886789Sahrens /* ARGSUSED */
3887789Sahrens static int
3888789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3889789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3890789Sahrens {
3891789Sahrens 	znode_t		*zp = VTOZ(vp);
3892789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3893789Sahrens 	dmu_tx_t	*tx;
38942688Smaybee 	u_offset_t	off, koff;
38952688Smaybee 	size_t		len, klen;
3896789Sahrens 	int		err;
3897789Sahrens 
38982688Smaybee 	off = pp->p_offset;
38992688Smaybee 	len = PAGESIZE;
39002688Smaybee 	/*
39012688Smaybee 	 * If our blocksize is bigger than the page size, try to kluster
39028227SNeil.Perrin@Sun.COM 	 * multiple pages so that we write a full block (thus avoiding
39032688Smaybee 	 * a read-modify-write).
39042688Smaybee 	 */
3905*11935SMark.Shellenbaum@Sun.COM 	if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
39068636SMark.Maybee@Sun.COM 		klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
39078636SMark.Maybee@Sun.COM 		koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
3908*11935SMark.Shellenbaum@Sun.COM 		ASSERT(koff <= zp->z_size);
3909*11935SMark.Shellenbaum@Sun.COM 		if (koff + klen > zp->z_size)
3910*11935SMark.Shellenbaum@Sun.COM 			klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
39112688Smaybee 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
39122688Smaybee 	}
39132688Smaybee 	ASSERT3U(btop(len), ==, btopr(len));
39148636SMark.Maybee@Sun.COM 
39151819Smaybee 	/*
39161819Smaybee 	 * Can't push pages past end-of-file.
39171819Smaybee 	 */
3918*11935SMark.Shellenbaum@Sun.COM 	if (off >= zp->z_size) {
39194709Smaybee 		/* ignore all pages */
39202688Smaybee 		err = 0;
39212688Smaybee 		goto out;
3922*11935SMark.Shellenbaum@Sun.COM 	} else if (off + len > zp->z_size) {
3923*11935SMark.Shellenbaum@Sun.COM 		int npages = btopr(zp->z_size - off);
39242688Smaybee 		page_t *trunc;
39252688Smaybee 
39262688Smaybee 		page_list_break(&pp, &trunc, npages);
39274709Smaybee 		/* ignore pages past end of file */
39282688Smaybee 		if (trunc)
39294709Smaybee 			pvn_write_done(trunc, flags);
3930*11935SMark.Shellenbaum@Sun.COM 		len = zp->z_size - off;
39311819Smaybee 	}
39329396SMatthew.Ahrens@Sun.COM 
3933*11935SMark.Shellenbaum@Sun.COM 	if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
3934*11935SMark.Shellenbaum@Sun.COM 	    zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
39359396SMatthew.Ahrens@Sun.COM 		err = EDQUOT;
39369396SMatthew.Ahrens@Sun.COM 		goto out;
39379396SMatthew.Ahrens@Sun.COM 	}
39388636SMark.Maybee@Sun.COM top:
3939789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3940789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, off, len);
3941*11935SMark.Shellenbaum@Sun.COM 
3942*11935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3943*11935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
39448227SNeil.Perrin@Sun.COM 	err = dmu_tx_assign(tx, TXG_NOWAIT);
3945789Sahrens 	if (err != 0) {
39468227SNeil.Perrin@Sun.COM 		if (err == ERESTART) {
39472113Sahrens 			dmu_tx_wait(tx);
39482113Sahrens 			dmu_tx_abort(tx);
3949789Sahrens 			goto top;
3950789Sahrens 		}
39512113Sahrens 		dmu_tx_abort(tx);
3952789Sahrens 		goto out;
3953789Sahrens 	}
3954789Sahrens 
39552688Smaybee 	if (zp->z_blksz <= PAGESIZE) {
39567315SJonathan.Adams@Sun.COM 		caddr_t va = zfs_map_page(pp, S_READ);
39572688Smaybee 		ASSERT3U(len, <=, PAGESIZE);
39582688Smaybee 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
39597315SJonathan.Adams@Sun.COM 		zfs_unmap_page(pp, va);
39602688Smaybee 	} else {
39612688Smaybee 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
39622688Smaybee 	}
39632688Smaybee 
39642688Smaybee 	if (err == 0) {
3965*11935SMark.Shellenbaum@Sun.COM 		uint64_t mtime[2], ctime[2];
3966*11935SMark.Shellenbaum@Sun.COM 		sa_bulk_attr_t bulk[2];
3967*11935SMark.Shellenbaum@Sun.COM 		int count = 0;
3968*11935SMark.Shellenbaum@Sun.COM 
3969*11935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3970*11935SMark.Shellenbaum@Sun.COM 		    &mtime, 16);
3971*11935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3972*11935SMark.Shellenbaum@Sun.COM 		    &ctime, 16);
3973*11935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3974*11935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
39758636SMark.Maybee@Sun.COM 		zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
39762688Smaybee 	}
39779951SLin.Ling@Sun.COM 	dmu_tx_commit(tx);
39782688Smaybee 
39792688Smaybee out:
39804709Smaybee 	pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
3981789Sahrens 	if (offp)
3982789Sahrens 		*offp = off;
3983789Sahrens 	if (lenp)
3984789Sahrens 		*lenp = len;
3985789Sahrens 
3986789Sahrens 	return (err);
3987789Sahrens }
3988789Sahrens 
3989789Sahrens /*
3990789Sahrens  * Copy the portion of the file indicated from pages into the file.
3991789Sahrens  * The pages are stored in a page list attached to the files vnode.
3992789Sahrens  *
3993789Sahrens  *	IN:	vp	- vnode of file to push page data to.
3994789Sahrens  *		off	- position in file to put data.
3995789Sahrens  *		len	- amount of data to write.
3996789Sahrens  *		flags	- flags to control the operation.
3997789Sahrens  *		cr	- credentials of caller.
39985331Samw  *		ct	- caller context.
3999789Sahrens  *
4000789Sahrens  *	RETURN:	0 if success
4001789Sahrens  *		error code if failure
4002789Sahrens  *
4003789Sahrens  * Timestamps:
4004789Sahrens  *	vp - ctime|mtime updated
4005789Sahrens  */
40065331Samw /*ARGSUSED*/
4007789Sahrens static int
40085331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
40095331Samw     caller_context_t *ct)
4010789Sahrens {
4011789Sahrens 	znode_t		*zp = VTOZ(vp);
4012789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4013789Sahrens 	page_t		*pp;
4014789Sahrens 	size_t		io_len;
4015789Sahrens 	u_offset_t	io_off;
40168636SMark.Maybee@Sun.COM 	uint_t		blksz;
40178636SMark.Maybee@Sun.COM 	rl_t		*rl;
4018789Sahrens 	int		error = 0;
4019789Sahrens 
40205367Sahrens 	ZFS_ENTER(zfsvfs);
40215367Sahrens 	ZFS_VERIFY_ZP(zp);
4022789Sahrens 
40238636SMark.Maybee@Sun.COM 	/*
40248636SMark.Maybee@Sun.COM 	 * Align this request to the file block size in case we kluster.
40258636SMark.Maybee@Sun.COM 	 * XXX - this can result in pretty aggresive locking, which can
40268636SMark.Maybee@Sun.COM 	 * impact simultanious read/write access.  One option might be
40278636SMark.Maybee@Sun.COM 	 * to break up long requests (len == 0) into block-by-block
40288636SMark.Maybee@Sun.COM 	 * operations to get narrower locking.
40298636SMark.Maybee@Sun.COM 	 */
40308636SMark.Maybee@Sun.COM 	blksz = zp->z_blksz;
40318636SMark.Maybee@Sun.COM 	if (ISP2(blksz))
40328636SMark.Maybee@Sun.COM 		io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
40338636SMark.Maybee@Sun.COM 	else
40348636SMark.Maybee@Sun.COM 		io_off = 0;
40358636SMark.Maybee@Sun.COM 	if (len > 0 && ISP2(blksz))
40369141SMark.Maybee@Sun.COM 		io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
40378636SMark.Maybee@Sun.COM 	else
40388636SMark.Maybee@Sun.COM 		io_len = 0;
40398636SMark.Maybee@Sun.COM 
40408636SMark.Maybee@Sun.COM 	if (io_len == 0) {
4041789Sahrens 		/*
40428636SMark.Maybee@Sun.COM 		 * Search the entire vp list for pages >= io_off.
4043789Sahrens 		 */
40448636SMark.Maybee@Sun.COM 		rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
40458636SMark.Maybee@Sun.COM 		error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
40461472Sperrin 		goto out;
4047789Sahrens 	}
40488636SMark.Maybee@Sun.COM 	rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
40498636SMark.Maybee@Sun.COM 
4050*11935SMark.Shellenbaum@Sun.COM 	if (off > zp->z_size) {
4051789Sahrens 		/* past end of file */
40528636SMark.Maybee@Sun.COM 		zfs_range_unlock(rl);
4053789Sahrens 		ZFS_EXIT(zfsvfs);
4054789Sahrens 		return (0);
4055789Sahrens 	}
4056789Sahrens 
4057*11935SMark.Shellenbaum@Sun.COM 	len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
40588636SMark.Maybee@Sun.COM 
40598636SMark.Maybee@Sun.COM 	for (off = io_off; io_off < off + len; io_off += io_len) {
4060789Sahrens 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
40611669Sperrin 			pp = page_lookup(vp, io_off,
40624339Sperrin 			    (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4063789Sahrens 		} else {
4064789Sahrens 			pp = page_lookup_nowait(vp, io_off,
40654339Sperrin 			    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4066789Sahrens 		}
4067789Sahrens 
4068789Sahrens 		if (pp != NULL && pvn_getdirty(pp, flags)) {
4069789Sahrens 			int err;
4070789Sahrens 
4071789Sahrens 			/*
4072789Sahrens 			 * Found a dirty page to push
4073789Sahrens 			 */
40741669Sperrin 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
40751669Sperrin 			if (err)
4076789Sahrens 				error = err;
4077789Sahrens 		} else {
4078789Sahrens 			io_len = PAGESIZE;
4079789Sahrens 		}
4080789Sahrens 	}
40811472Sperrin out:
40828636SMark.Maybee@Sun.COM 	zfs_range_unlock(rl);
40832638Sperrin 	if ((flags & B_ASYNC) == 0)
40842638Sperrin 		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
4085789Sahrens 	ZFS_EXIT(zfsvfs);
4086789Sahrens 	return (error);
4087789Sahrens }
4088789Sahrens 
40895331Samw /*ARGSUSED*/
4090789Sahrens void
40915331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4092789Sahrens {
4093789Sahrens 	znode_t	*zp = VTOZ(vp);
4094789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4095789Sahrens 	int error;
4096789Sahrens 
40975326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4098*11935SMark.Shellenbaum@Sun.COM 	if (zp->z_sa_hdl == NULL) {
40995446Sahrens 		/*
41005642Smaybee 		 * The fs has been unmounted, or we did a
41015642Smaybee 		 * suspend/resume and this file no longer exists.
41025446Sahrens 		 */
4103789Sahrens 		if (vn_has_cached_data(vp)) {
4104789Sahrens 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
4105789Sahrens 			    B_INVAL, cr);
4106789Sahrens 		}
4107789Sahrens 
41081544Seschrock 		mutex_enter(&zp->z_lock);
410910369Schris.kirby@sun.com 		mutex_enter(&vp->v_lock);
411010369Schris.kirby@sun.com 		ASSERT(vp->v_count == 1);
411110369Schris.kirby@sun.com 		vp->v_count = 0;
411210369Schris.kirby@sun.com 		mutex_exit(&vp->v_lock);
41135446Sahrens 		mutex_exit(&zp->z_lock);
41145642Smaybee 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
41155446Sahrens 		zfs_znode_free(zp);
4116789Sahrens 		return;
4117789Sahrens 	}
4118789Sahrens 
4119789Sahrens 	/*
4120789Sahrens 	 * Attempt to push any data in the page cache.  If this fails
4121789Sahrens 	 * we will get kicked out later in zfs_zinactive().
4122789Sahrens 	 */
41231298Sperrin 	if (vn_has_cached_data(vp)) {
41241298Sperrin 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
41251298Sperrin 		    cr);
41261298Sperrin 	}
4127789Sahrens 
41283461Sahrens 	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4129789Sahrens 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4130789Sahrens 
4131*11935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4132*11935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, zp);
4133789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
4134789Sahrens 		if (error) {
4135789Sahrens 			dmu_tx_abort(tx);
4136789Sahrens 		} else {
4137789Sahrens 			mutex_enter(&zp->z_lock);
4138*11935SMark.Shellenbaum@Sun.COM 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4139*11935SMark.Shellenbaum@Sun.COM 			    (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4140789Sahrens 			zp->z_atime_dirty = 0;
4141789Sahrens 			mutex_exit(&zp->z_lock);
4142789Sahrens 			dmu_tx_commit(tx);
4143789Sahrens 		}
4144789Sahrens 	}
4145789Sahrens 
4146789Sahrens 	zfs_zinactive(zp);
41475326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
4148789Sahrens }
4149789Sahrens 
4150789Sahrens /*
4151789Sahrens  * Bounds-check the seek operation.
4152789Sahrens  *
4153789Sahrens  *	IN:	vp	- vnode seeking within
4154789Sahrens  *		ooff	- old file offset
4155789Sahrens  *		noffp	- pointer to new file offset
41565331Samw  *		ct	- caller context
4157789Sahrens  *
4158789Sahrens  *	RETURN:	0 if success
4159789Sahrens  *		EINVAL if new offset invalid
4160789Sahrens  */
4161789Sahrens /* ARGSUSED */
4162789Sahrens static int
41635331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
41645331Samw     caller_context_t *ct)
4165789Sahrens {
4166789Sahrens 	if (vp->v_type == VDIR)
4167789Sahrens 		return (0);
4168789Sahrens 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4169789Sahrens }
4170789Sahrens 
4171789Sahrens /*
4172789Sahrens  * Pre-filter the generic locking function to trap attempts to place
4173789Sahrens  * a mandatory lock on a memory mapped file.
4174789Sahrens  */
4175789Sahrens static int
4176789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
41775331Samw     flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4178789Sahrens {
4179789Sahrens 	znode_t *zp = VTOZ(vp);
4180789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4181789Sahrens 
41825367Sahrens 	ZFS_ENTER(zfsvfs);
41835367Sahrens 	ZFS_VERIFY_ZP(zp);
4184789Sahrens 
4185789Sahrens 	/*
41861544Seschrock 	 * We are following the UFS semantics with respect to mapcnt
41871544Seschrock 	 * here: If we see that the file is mapped already, then we will
41881544Seschrock 	 * return an error, but we don't worry about races between this
41891544Seschrock 	 * function and zfs_map().
4190789Sahrens 	 */
4191*11935SMark.Shellenbaum@Sun.COM 	if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4192789Sahrens 		ZFS_EXIT(zfsvfs);
4193789Sahrens 		return (EAGAIN);
4194789Sahrens 	}
4195789Sahrens 	ZFS_EXIT(zfsvfs);
419610896SMark.Shellenbaum@Sun.COM 	return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4197789Sahrens }
4198789Sahrens 
4199789Sahrens /*
4200789Sahrens  * If we can't find a page in the cache, we will create a new page
4201789Sahrens  * and fill it with file data.  For efficiency, we may try to fill
42028636SMark.Maybee@Sun.COM  * multiple pages at once (klustering) to fill up the supplied page
42039265SMark.Maybee@Sun.COM  * list.  Note that the pages to be filled are held with an exclusive
42049265SMark.Maybee@Sun.COM  * lock to prevent access by other threads while they are being filled.
4205789Sahrens  */
4206789Sahrens static int
4207789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4208789Sahrens     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4209789Sahrens {
4210789Sahrens 	znode_t *zp = VTOZ(vp);
4211789Sahrens 	page_t *pp, *cur_pp;
4212789Sahrens 	objset_t *os = zp->z_zfsvfs->z_os;
4213789Sahrens 	u_offset_t io_off, total;
4214789Sahrens 	size_t io_len;
4215789Sahrens 	int err;
4216789Sahrens 
42172688Smaybee 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
42188636SMark.Maybee@Sun.COM 		/*
42198636SMark.Maybee@Sun.COM 		 * We only have a single page, don't bother klustering
42208636SMark.Maybee@Sun.COM 		 */
4221789Sahrens 		io_off = off;
4222789Sahrens 		io_len = PAGESIZE;
42239265SMark.Maybee@Sun.COM 		pp = page_create_va(vp, io_off, io_len,
42249265SMark.Maybee@Sun.COM 		    PG_EXCL | PG_WAIT, seg, addr);
4225789Sahrens 	} else {
4226789Sahrens 		/*
42278636SMark.Maybee@Sun.COM 		 * Try to find enough pages to fill the page list
4228789Sahrens 		 */
4229789Sahrens 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
42308636SMark.Maybee@Sun.COM 		    &io_len, off, plsz, 0);
4231789Sahrens 	}
4232789Sahrens 	if (pp == NULL) {
4233789Sahrens 		/*
42348636SMark.Maybee@Sun.COM 		 * The page already exists, nothing to do here.
4235789Sahrens 		 */
4236789Sahrens 		*pl = NULL;
4237789Sahrens 		return (0);
4238789Sahrens 	}
4239789Sahrens 
4240789Sahrens 	/*
4241789Sahrens 	 * Fill the pages in the kluster.
4242789Sahrens 	 */
4243789Sahrens 	cur_pp = pp;
4244789Sahrens 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
42458636SMark.Maybee@Sun.COM 		caddr_t va;
42468636SMark.Maybee@Sun.COM 
42472688Smaybee 		ASSERT3U(io_off, ==, cur_pp->p_offset);
42487315SJonathan.Adams@Sun.COM 		va = zfs_map_page(cur_pp, S_WRITE);
42499512SNeil.Perrin@Sun.COM 		err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
42509512SNeil.Perrin@Sun.COM 		    DMU_READ_PREFETCH);
42517315SJonathan.Adams@Sun.COM 		zfs_unmap_page(cur_pp, va);
4252789Sahrens 		if (err) {
4253789Sahrens 			/* On error, toss the entire kluster */
4254789Sahrens 			pvn_read_done(pp, B_ERROR);
42557294Sperrin 			/* convert checksum errors into IO errors */
42567294Sperrin 			if (err == ECKSUM)
42577294Sperrin 				err = EIO;
4258789Sahrens 			return (err);
4259789Sahrens 		}
4260789Sahrens 		cur_pp = cur_pp->p_next;
4261789Sahrens 	}
42628636SMark.Maybee@Sun.COM 
4263789Sahrens 	/*
42648636SMark.Maybee@Sun.COM 	 * Fill in the page list array from the kluster starting
42658636SMark.Maybee@Sun.COM 	 * from the desired offset `off'.
4266789Sahrens 	 * NOTE: the page list will always be null terminated.
4267789Sahrens 	 */
4268789Sahrens 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
42698636SMark.Maybee@Sun.COM 	ASSERT(pl == NULL || (*pl)->p_offset == off);
4270789Sahrens 
4271789Sahrens 	return (0);
4272789Sahrens }
4273789Sahrens 
4274789Sahrens /*
4275789Sahrens  * Return pointers to the pages for the file region [off, off + len]
4276789Sahrens  * in the pl array.  If plsz is greater than len, this function may
42778636SMark.Maybee@Sun.COM  * also return page pointers from after the specified region
42788636SMark.Maybee@Sun.COM  * (i.e. the region [off, off + plsz]).  These additional pages are
42798636SMark.Maybee@Sun.COM  * only returned if they are already in the cache, or were created as
42808636SMark.Maybee@Sun.COM  * part of a klustered read.
4281789Sahrens  *
4282789Sahrens  *	IN:	vp	- vnode of file to get data from.
4283789Sahrens  *		off	- position in file to get data from.
4284789Sahrens  *		len	- amount of data to retrieve.
4285789Sahrens  *		plsz	- length of provided page list.
4286789Sahrens  *		seg	- segment to obtain pages for.
4287789Sahrens  *		addr	- virtual address of fault.
4288789Sahrens  *		rw	- mode of created pages.
4289789Sahrens  *		cr	- credentials of caller.
42905331Samw  *		ct	- caller context.
4291789Sahrens  *
4292789Sahrens  *	OUT:	protp	- protection mode of created pages.
4293789Sahrens  *		pl	- list of pages created.
4294789Sahrens  *
4295789Sahrens  *	RETURN:	0 if success
4296789Sahrens  *		error code if failure
4297789Sahrens  *
4298789Sahrens  * Timestamps:
4299789Sahrens  *	vp - atime updated
4300789Sahrens  */
4301789Sahrens /* ARGSUSED */
4302789Sahrens static int
4303789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4304789Sahrens 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
43055331Samw 	enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4306789Sahrens {
4307789Sahrens 	znode_t		*zp = VTOZ(vp);
4308789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
43098636SMark.Maybee@Sun.COM 	page_t		**pl0 = pl;
43108636SMark.Maybee@Sun.COM 	int		err = 0;
43118636SMark.Maybee@Sun.COM 
43128636SMark.Maybee@Sun.COM 	/* we do our own caching, faultahead is unnecessary */
43138636SMark.Maybee@Sun.COM 	if (pl == NULL)
43148636SMark.Maybee@Sun.COM 		return (0);
43158636SMark.Maybee@Sun.COM 	else if (len > plsz)
43168636SMark.Maybee@Sun.COM 		len = plsz;
43178681SMark.Maybee@Sun.COM 	else
43188681SMark.Maybee@Sun.COM 		len = P2ROUNDUP(len, PAGESIZE);
43198636SMark.Maybee@Sun.COM 	ASSERT(plsz >= len);
4320789Sahrens 
43215367Sahrens 	ZFS_ENTER(zfsvfs);
43225367Sahrens 	ZFS_VERIFY_ZP(zp);
4323789Sahrens 
4324789Sahrens 	if (protp)
4325789Sahrens 		*protp = PROT_ALL;
4326789Sahrens 
4327789Sahrens 	/*
43289265SMark.Maybee@Sun.COM 	 * Loop through the requested range [off, off + len) looking
4329789Sahrens 	 * for pages.  If we don't find a page, we will need to create
4330789Sahrens 	 * a new page and fill it with data from the file.
4331789Sahrens 	 */
4332789Sahrens 	while (len > 0) {
43338636SMark.Maybee@Sun.COM 		if (*pl = page_lookup(vp, off, SE_SHARED))
43348636SMark.Maybee@Sun.COM 			*(pl+1) = NULL;
43358636SMark.Maybee@Sun.COM 		else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
43368636SMark.Maybee@Sun.COM 			goto out;
43378636SMark.Maybee@Sun.COM 		while (*pl) {
43388636SMark.Maybee@Sun.COM 			ASSERT3U((*pl)->p_offset, ==, off);
4339789Sahrens 			off += PAGESIZE;
4340789Sahrens 			addr += PAGESIZE;
43418681SMark.Maybee@Sun.COM 			if (len > 0) {
43428681SMark.Maybee@Sun.COM 				ASSERT3U(len, >=, PAGESIZE);
43438636SMark.Maybee@Sun.COM 				len -= PAGESIZE;
43448681SMark.Maybee@Sun.COM 			}
43458636SMark.Maybee@Sun.COM 			ASSERT3U(plsz, >=, PAGESIZE);
4346789Sahrens 			plsz -= PAGESIZE;
43478636SMark.Maybee@Sun.COM 			pl++;
4348789Sahrens 		}
4349789Sahrens 	}
4350789Sahrens 
4351789Sahrens 	/*
4352789Sahrens 	 * Fill out the page array with any pages already in the cache.
4353789Sahrens 	 */
43548636SMark.Maybee@Sun.COM 	while (plsz > 0 &&
43558636SMark.Maybee@Sun.COM 	    (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
43568636SMark.Maybee@Sun.COM 			off += PAGESIZE;
43578636SMark.Maybee@Sun.COM 			plsz -= PAGESIZE;
4358789Sahrens 	}
4359789Sahrens out:
43602752Sperrin 	if (err) {
43612752Sperrin 		/*
43622752Sperrin 		 * Release any pages we have previously locked.
43632752Sperrin 		 */
43642752Sperrin 		while (pl > pl0)
43652752Sperrin 			page_unlock(*--pl);
43668636SMark.Maybee@Sun.COM 	} else {
43678636SMark.Maybee@Sun.COM 		ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
43682752Sperrin 	}
43692752Sperrin 
4370789Sahrens 	*pl = NULL;
4371789Sahrens 
4372789Sahrens 	ZFS_EXIT(zfsvfs);
4373789Sahrens 	return (err);
4374789Sahrens }
4375789Sahrens 
43761544Seschrock /*
43771544Seschrock  * Request a memory map for a section of a file.  This code interacts
43781544Seschrock  * with common code and the VM system as follows:
43791544Seschrock  *
43801544Seschrock  *	common code calls mmap(), which ends up in smmap_common()
43811544Seschrock  *
43821544Seschrock  *	this calls VOP_MAP(), which takes you into (say) zfs
43831544Seschrock  *
43841544Seschrock  *	zfs_map() calls as_map(), passing segvn_create() as the callback
43851544Seschrock  *
43861544Seschrock  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
43871544Seschrock  *
43881544Seschrock  *	zfs_addmap() updates z_mapcnt
43891544Seschrock  */
43905331Samw /*ARGSUSED*/
4391789Sahrens static int
4392789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
43935331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
43945331Samw     caller_context_t *ct)
4395789Sahrens {
4396789Sahrens 	znode_t *zp = VTOZ(vp);
4397789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4398789Sahrens 	segvn_crargs_t	vn_a;
4399789Sahrens 	int		error;
4400789Sahrens 
44015929Smarks 	ZFS_ENTER(zfsvfs);
44025929Smarks 	ZFS_VERIFY_ZP(zp);
44035929Smarks 
4404*11935SMark.Shellenbaum@Sun.COM 	if ((prot & PROT_WRITE) && (zp->z_pflags &
4405*11935SMark.Shellenbaum@Sun.COM 	    (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
44065929Smarks 		ZFS_EXIT(zfsvfs);
44075331Samw 		return (EPERM);
44085929Smarks 	}
44095929Smarks 
44105929Smarks 	if ((prot & (PROT_READ | PROT_EXEC)) &&
4411*11935SMark.Shellenbaum@Sun.COM 	    (zp->z_pflags & ZFS_AV_QUARANTINED)) {
44125929Smarks 		ZFS_EXIT(zfsvfs);
44135929Smarks 		return (EACCES);
44145929Smarks 	}
4415789Sahrens 
4416789Sahrens 	if (vp->v_flag & VNOMAP) {
4417789Sahrens 		ZFS_EXIT(zfsvfs);
4418789Sahrens 		return (ENOSYS);
4419789Sahrens 	}
4420789Sahrens 
4421789Sahrens 	if (off < 0 || len > MAXOFFSET_T - off) {
4422789Sahrens 		ZFS_EXIT(zfsvfs);
4423789Sahrens 		return (ENXIO);
4424789Sahrens 	}
4425789Sahrens 
4426789Sahrens 	if (vp->v_type != VREG) {
4427789Sahrens 		ZFS_EXIT(zfsvfs);
4428789Sahrens 		return (ENODEV);
4429789Sahrens 	}
4430789Sahrens 
4431789Sahrens 	/*
4432789Sahrens 	 * If file is locked, disallow mapping.
4433789Sahrens 	 */
4434*11935SMark.Shellenbaum@Sun.COM 	if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
44351544Seschrock 		ZFS_EXIT(zfsvfs);
44361544Seschrock 		return (EAGAIN);
4437789Sahrens 	}
4438789Sahrens 
4439789Sahrens 	as_rangelock(as);
44406036Smec 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
44416036Smec 	if (error != 0) {
44426036Smec 		as_rangeunlock(as);
44436036Smec 		ZFS_EXIT(zfsvfs);
44446036Smec 		return (error);
4445789Sahrens 	}
4446789Sahrens 
4447789Sahrens 	vn_a.vp = vp;
4448789Sahrens 	vn_a.offset = (u_offset_t)off;
4449789Sahrens 	vn_a.type = flags & MAP_TYPE;
4450789Sahrens 	vn_a.prot = prot;
4451789Sahrens 	vn_a.maxprot = maxprot;
4452789Sahrens 	vn_a.cred = cr;
4453789Sahrens 	vn_a.amp = NULL;
4454789Sahrens 	vn_a.flags = flags & ~MAP_TYPE;
44551417Skchow 	vn_a.szc = 0;
44561417Skchow 	vn_a.lgrp_mem_policy_flags = 0;
4457789Sahrens 
4458789Sahrens 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
4459789Sahrens 
4460789Sahrens 	as_rangeunlock(as);
4461789Sahrens 	ZFS_EXIT(zfsvfs);
4462789Sahrens 	return (error);
4463789Sahrens }
4464789Sahrens 
4465789Sahrens /* ARGSUSED */
4466789Sahrens static int
4467789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
44685331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
44695331Samw     caller_context_t *ct)
4470789Sahrens {
44711544Seschrock 	uint64_t pages = btopr(len);
44721544Seschrock 
44731544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4474789Sahrens 	return (0);
4475789Sahrens }
4476789Sahrens 
44771773Seschrock /*
44781773Seschrock  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
44791773Seschrock  * more accurate mtime for the associated file.  Since we don't have a way of
44801773Seschrock  * detecting when the data was actually modified, we have to resort to
44811773Seschrock  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
44821773Seschrock  * last page is pushed.  The problem occurs when the msync() call is omitted,
44831773Seschrock  * which by far the most common case:
44841773Seschrock  *
44851773Seschrock  * 	open()
44861773Seschrock  * 	mmap()
44871773Seschrock  * 	<modify memory>
44881773Seschrock  * 	munmap()
44891773Seschrock  * 	close()
44901773Seschrock  * 	<time lapse>
44911773Seschrock  * 	putpage() via fsflush
44921773Seschrock  *
44931773Seschrock  * If we wait until fsflush to come along, we can have a modification time that
44941773Seschrock  * is some arbitrary point in the future.  In order to prevent this in the
44951773Seschrock  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
44961773Seschrock  * torn down.
44971773Seschrock  */
4498789Sahrens /* ARGSUSED */
4499789Sahrens static int
4500789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
45015331Samw     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
45025331Samw     caller_context_t *ct)
4503789Sahrens {
45041544Seschrock 	uint64_t pages = btopr(len);
45051544Seschrock 
45061544Seschrock 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
45071544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
45081773Seschrock 
45091773Seschrock 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
45101773Seschrock 	    vn_has_cached_data(vp))
45115331Samw 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
45121773Seschrock 
4513789Sahrens 	return (0);
4514789Sahrens }
4515789Sahrens 
4516789Sahrens /*
4517789Sahrens  * Free or allocate space in a file.  Currently, this function only
4518789Sahrens  * supports the `F_FREESP' command.  However, this command is somewhat
4519789Sahrens  * misnamed, as its functionality includes the ability to allocate as
4520789Sahrens  * well as free space.
4521789Sahrens  *
4522789Sahrens  *	IN:	vp	- vnode of file to free data in.
4523789Sahrens  *		cmd	- action to take (only F_FREESP supported).
4524789Sahrens  *		bfp	- section of file to free/alloc.
4525789Sahrens  *		flag	- current file open mode flags.
4526789Sahrens  *		offset	- current file offset.
4527789Sahrens  *		cr	- credentials of caller [UNUSED].
45285331Samw  *		ct	- caller context.
4529789Sahrens  *
4530789Sahrens  *	RETURN:	0 if success
4531789Sahrens  *		error code if failure
4532789Sahrens  *
4533789Sahrens  * Timestamps:
4534789Sahrens  *	vp - ctime|mtime updated
4535789Sahrens  */
4536789Sahrens /* ARGSUSED */
4537789Sahrens static int
4538789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4539789Sahrens     offset_t offset, cred_t *cr, caller_context_t *ct)
4540789Sahrens {
4541789Sahrens 	znode_t		*zp = VTOZ(vp);
4542789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4543789Sahrens 	uint64_t	off, len;
4544789Sahrens 	int		error;
4545789Sahrens 
45465367Sahrens 	ZFS_ENTER(zfsvfs);
45475367Sahrens 	ZFS_VERIFY_ZP(zp);
4548789Sahrens 
4549789Sahrens 	if (cmd != F_FREESP) {
4550789Sahrens 		ZFS_EXIT(zfsvfs);
4551789Sahrens 		return (EINVAL);
4552789Sahrens 	}
4553789Sahrens 
4554789Sahrens 	if (error = convoff(vp, bfp, 0, offset)) {
4555789Sahrens 		ZFS_EXIT(zfsvfs);
4556789Sahrens 		return (error);
4557789Sahrens 	}
4558789Sahrens 
4559789Sahrens 	if (bfp->l_len < 0) {
4560789Sahrens 		ZFS_EXIT(zfsvfs);
4561789Sahrens 		return (EINVAL);
4562789Sahrens 	}
4563789Sahrens 
4564789Sahrens 	off = bfp->l_start;
45651669Sperrin 	len = bfp->l_len; /* 0 means from off to end of file */
45661878Smaybee 
45676992Smaybee 	error = zfs_freesp(zp, off, len, flag, TRUE);
4568789Sahrens 
4569789Sahrens 	ZFS_EXIT(zfsvfs);
4570789Sahrens 	return (error);
4571789Sahrens }
4572789Sahrens 
45735331Samw /*ARGSUSED*/
4574789Sahrens static int
45755331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4576789Sahrens {
4577789Sahrens 	znode_t		*zp = VTOZ(vp);
4578789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
45795326Sek110237 	uint32_t	gen;
4580*11935SMark.Shellenbaum@Sun.COM 	uint64_t	gen64;
4581789Sahrens 	uint64_t	object = zp->z_id;
4582789Sahrens 	zfid_short_t	*zfid;
4583*11935SMark.Shellenbaum@Sun.COM 	int		size, i, error;
4584789Sahrens 
45855367Sahrens 	ZFS_ENTER(zfsvfs);
45865367Sahrens 	ZFS_VERIFY_ZP(zp);
4587*11935SMark.Shellenbaum@Sun.COM 
4588*11935SMark.Shellenbaum@Sun.COM 	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
4589*11935SMark.Shellenbaum@Sun.COM 	    &gen64, sizeof (uint64_t))) != 0)
4590*11935SMark.Shellenbaum@Sun.COM 		return (error);
4591*11935SMark.Shellenbaum@Sun.COM 
4592*11935SMark.Shellenbaum@Sun.COM 	gen = (uint32_t)gen64;
4593789Sahrens 
4594789Sahrens 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4595789Sahrens 	if (fidp->fid_len < size) {
4596789Sahrens 		fidp->fid_len = size;
45971512Sek110237 		ZFS_EXIT(zfsvfs);
4598789Sahrens 		return (ENOSPC);
4599789Sahrens 	}
4600789Sahrens 
4601789Sahrens 	zfid = (zfid_short_t *)fidp;
4602789Sahrens 
4603789Sahrens 	zfid->zf_len = size;
4604789Sahrens 
4605789Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
4606789Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4607789Sahrens 
4608789Sahrens 	/* Must have a non-zero generation number to distinguish from .zfs */
4609789Sahrens 	if (gen == 0)
4610789Sahrens 		gen = 1;
4611789Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
4612789Sahrens 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4613789Sahrens 
4614789Sahrens 	if (size == LONG_FID_LEN) {
4615789Sahrens 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
4616789Sahrens 		zfid_long_t	*zlfid;
4617789Sahrens 
4618789Sahrens 		zlfid = (zfid_long_t *)fidp;
4619789Sahrens 
4620789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4621789Sahrens 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4622789Sahrens 
4623789Sahrens 		/* XXX - this should be the generation number for the objset */
4624789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4625789Sahrens 			zlfid->zf_setgen[i] = 0;
4626789Sahrens 	}
4627789Sahrens 
4628789Sahrens 	ZFS_EXIT(zfsvfs);
4629789Sahrens 	return (0);
4630789Sahrens }
4631789Sahrens 
4632789Sahrens static int
46335331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
46345331Samw     caller_context_t *ct)
4635789Sahrens {
4636789Sahrens 	znode_t		*zp, *xzp;
4637789Sahrens 	zfsvfs_t	*zfsvfs;
4638789Sahrens 	zfs_dirlock_t	*dl;
4639789Sahrens 	int		error;
4640789Sahrens 
4641789Sahrens 	switch (cmd) {
4642789Sahrens 	case _PC_LINK_MAX:
4643789Sahrens 		*valp = ULONG_MAX;
4644789Sahrens 		return (0);
4645789Sahrens 
4646789Sahrens 	case _PC_FILESIZEBITS:
4647789Sahrens 		*valp = 64;
4648789Sahrens 		return (0);
4649789Sahrens 
4650789Sahrens 	case _PC_XATTR_EXISTS:
4651789Sahrens 		zp = VTOZ(vp);
4652789Sahrens 		zfsvfs = zp->z_zfsvfs;
46535367Sahrens 		ZFS_ENTER(zfsvfs);
46545367Sahrens 		ZFS_VERIFY_ZP(zp);
4655789Sahrens 		*valp = 0;
4656789Sahrens 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
46575331Samw 		    ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4658789Sahrens 		if (error == 0) {
4659789Sahrens 			zfs_dirent_unlock(dl);
4660789Sahrens 			if (!zfs_dirempty(xzp))
4661789Sahrens 				*valp = 1;
4662789Sahrens 			VN_RELE(ZTOV(xzp));
4663789Sahrens 		} else if (error == ENOENT) {
4664789Sahrens 			/*
4665789Sahrens 			 * If there aren't extended attributes, it's the
4666789Sahrens 			 * same as having zero of them.
4667789Sahrens 			 */
4668789Sahrens 			error = 0;
4669789Sahrens 		}
4670789Sahrens 		ZFS_EXIT(zfsvfs);
4671789Sahrens 		return (error);
4672789Sahrens 
46735331Samw 	case _PC_SATTR_ENABLED:
46745331Samw 	case _PC_SATTR_EXISTS:
46757757SJanice.Chang@Sun.COM 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
46765331Samw 		    (vp->v_type == VREG || vp->v_type == VDIR);
46775331Samw 		return (0);
46785331Samw 
46799749STim.Haley@Sun.COM 	case _PC_ACCESS_FILTERING:
46809749STim.Haley@Sun.COM 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
46819749STim.Haley@Sun.COM 		    vp->v_type == VDIR;
46829749STim.Haley@Sun.COM 		return (0);
46839749STim.Haley@Sun.COM 
4684789Sahrens 	case _PC_ACL_ENABLED:
4685789Sahrens 		*valp = _ACL_ACE_ENABLED;
4686789Sahrens 		return (0);
4687789Sahrens 
4688789Sahrens 	case _PC_MIN_HOLE_SIZE:
4689789Sahrens 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
4690789Sahrens 		return (0);
4691789Sahrens 
469210440SRoger.Faulkner@Sun.COM 	case _PC_TIMESTAMP_RESOLUTION:
469310440SRoger.Faulkner@Sun.COM 		/* nanosecond timestamp resolution */
469410440SRoger.Faulkner@Sun.COM 		*valp = 1L;
469510440SRoger.Faulkner@Sun.COM 		return (0);
469610440SRoger.Faulkner@Sun.COM 
4697789Sahrens 	default:
46985331Samw 		return (fs_pathconf(vp, cmd, valp, cr, ct));
4699789Sahrens 	}
4700789Sahrens }
4701789Sahrens 
4702789Sahrens /*ARGSUSED*/
4703789Sahrens static int
47045331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
47055331Samw     caller_context_t *ct)
4706789Sahrens {
4707789Sahrens 	znode_t *zp = VTOZ(vp);
4708789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4709789Sahrens 	int error;
47105331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4711789Sahrens 
47125367Sahrens 	ZFS_ENTER(zfsvfs);
47135367Sahrens 	ZFS_VERIFY_ZP(zp);
47145331Samw 	error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4715789Sahrens 	ZFS_EXIT(zfsvfs);
4716789Sahrens 
4717789Sahrens 	return (error);
4718789Sahrens }
4719789Sahrens 
4720789Sahrens /*ARGSUSED*/
4721789Sahrens static int
47225331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
47235331Samw     caller_context_t *ct)
4724789Sahrens {
4725789Sahrens 	znode_t *zp = VTOZ(vp);
4726789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4727789Sahrens 	int error;
47285331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4729789Sahrens 
47305367Sahrens 	ZFS_ENTER(zfsvfs);
47315367Sahrens 	ZFS_VERIFY_ZP(zp);
47325331Samw 	error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4733789Sahrens 	ZFS_EXIT(zfsvfs);
4734789Sahrens 	return (error);
4735789Sahrens }
4736789Sahrens 
4737789Sahrens /*
473811539SChunli.Zhang@Sun.COM  * Tunable, both must be a power of 2.
473911539SChunli.Zhang@Sun.COM  *
474011539SChunli.Zhang@Sun.COM  * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
474111539SChunli.Zhang@Sun.COM  * zcr_blksz_max: if set to less than the file block size, allow loaning out of
474211539SChunli.Zhang@Sun.COM  *                an arcbuf for a partial block read
474311539SChunli.Zhang@Sun.COM  */
474411539SChunli.Zhang@Sun.COM int zcr_blksz_min = (1 << 10);	/* 1K */
474511539SChunli.Zhang@Sun.COM int zcr_blksz_max = (1 << 17);	/* 128K */
474611539SChunli.Zhang@Sun.COM 
474711539SChunli.Zhang@Sun.COM /*ARGSUSED*/
474811539SChunli.Zhang@Sun.COM static int
474911539SChunli.Zhang@Sun.COM zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
475011539SChunli.Zhang@Sun.COM     caller_context_t *ct)
475111539SChunli.Zhang@Sun.COM {
475211539SChunli.Zhang@Sun.COM 	znode_t	*zp = VTOZ(vp);
475311539SChunli.Zhang@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
475411539SChunli.Zhang@Sun.COM 	int max_blksz = zfsvfs->z_max_blksz;
475511539SChunli.Zhang@Sun.COM 	uio_t *uio = &xuio->xu_uio;
475611539SChunli.Zhang@Sun.COM 	ssize_t size = uio->uio_resid;
475711539SChunli.Zhang@Sun.COM 	offset_t offset = uio->uio_loffset;
475811539SChunli.Zhang@Sun.COM 	int blksz;
475911539SChunli.Zhang@Sun.COM 	int fullblk, i;
476011539SChunli.Zhang@Sun.COM 	arc_buf_t *abuf;
476111539SChunli.Zhang@Sun.COM 	ssize_t maxsize;
476211539SChunli.Zhang@Sun.COM 	int preamble, postamble;
476311539SChunli.Zhang@Sun.COM 
476411539SChunli.Zhang@Sun.COM 	if (xuio->xu_type != UIOTYPE_ZEROCOPY)
476511539SChunli.Zhang@Sun.COM 		return (EINVAL);
476611539SChunli.Zhang@Sun.COM 
476711539SChunli.Zhang@Sun.COM 	ZFS_ENTER(zfsvfs);
476811539SChunli.Zhang@Sun.COM 	ZFS_VERIFY_ZP(zp);
476911539SChunli.Zhang@Sun.COM 	switch (ioflag) {
477011539SChunli.Zhang@Sun.COM 	case UIO_WRITE:
477111539SChunli.Zhang@Sun.COM 		/*
477211539SChunli.Zhang@Sun.COM 		 * Loan out an arc_buf for write if write size is bigger than
477311539SChunli.Zhang@Sun.COM 		 * max_blksz, and the file's block size is also max_blksz.
477411539SChunli.Zhang@Sun.COM 		 */
477511539SChunli.Zhang@Sun.COM 		blksz = max_blksz;
477611539SChunli.Zhang@Sun.COM 		if (size < blksz || zp->z_blksz != blksz) {
477711539SChunli.Zhang@Sun.COM 			ZFS_EXIT(zfsvfs);
477811539SChunli.Zhang@Sun.COM 			return (EINVAL);
477911539SChunli.Zhang@Sun.COM 		}
478011539SChunli.Zhang@Sun.COM 		/*
478111539SChunli.Zhang@Sun.COM 		 * Caller requests buffers for write before knowing where the
478211539SChunli.Zhang@Sun.COM 		 * write offset might be (e.g. NFS TCP write).
478311539SChunli.Zhang@Sun.COM 		 */
478411539SChunli.Zhang@Sun.COM 		if (offset == -1) {
478511539SChunli.Zhang@Sun.COM 			preamble = 0;
478611539SChunli.Zhang@Sun.COM 		} else {
478711539SChunli.Zhang@Sun.COM 			preamble = P2PHASE(offset, blksz);
478811539SChunli.Zhang@Sun.COM 			if (preamble) {
478911539SChunli.Zhang@Sun.COM 				preamble = blksz - preamble;
479011539SChunli.Zhang@Sun.COM 				size -= preamble;
479111539SChunli.Zhang@Sun.COM 			}
479211539SChunli.Zhang@Sun.COM 		}
479311539SChunli.Zhang@Sun.COM 
479411539SChunli.Zhang@Sun.COM 		postamble = P2PHASE(size, blksz);
479511539SChunli.Zhang@Sun.COM 		size -= postamble;
479611539SChunli.Zhang@Sun.COM 
479711539SChunli.Zhang@Sun.COM 		fullblk = size / blksz;
479811576SSurya.Prakki@Sun.COM 		(void) dmu_xuio_init(xuio,
479911539SChunli.Zhang@Sun.COM 		    (preamble != 0) + fullblk + (postamble != 0));
480011539SChunli.Zhang@Sun.COM 		DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
480111539SChunli.Zhang@Sun.COM 		    int, postamble, int,
480211539SChunli.Zhang@Sun.COM 		    (preamble != 0) + fullblk + (postamble != 0));
480311539SChunli.Zhang@Sun.COM 
480411539SChunli.Zhang@Sun.COM 		/*
480511539SChunli.Zhang@Sun.COM 		 * Have to fix iov base/len for partial buffers.  They
480611539SChunli.Zhang@Sun.COM 		 * currently represent full arc_buf's.
480711539SChunli.Zhang@Sun.COM 		 */
480811539SChunli.Zhang@Sun.COM 		if (preamble) {
480911539SChunli.Zhang@Sun.COM 			/* data begins in the middle of the arc_buf */
4810*11935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4811*11935SMark.Shellenbaum@Sun.COM 			    blksz);
481211539SChunli.Zhang@Sun.COM 			ASSERT(abuf);
481311576SSurya.Prakki@Sun.COM 			(void) dmu_xuio_add(xuio, abuf,
481411576SSurya.Prakki@Sun.COM 			    blksz - preamble, preamble);
481511539SChunli.Zhang@Sun.COM 		}
481611539SChunli.Zhang@Sun.COM 
481711539SChunli.Zhang@Sun.COM 		for (i = 0; i < fullblk; i++) {
4818*11935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4819*11935SMark.Shellenbaum@Sun.COM 			    blksz);
482011539SChunli.Zhang@Sun.COM 			ASSERT(abuf);
482111576SSurya.Prakki@Sun.COM 			(void) dmu_xuio_add(xuio, abuf, 0, blksz);
482211539SChunli.Zhang@Sun.COM 		}
482311539SChunli.Zhang@Sun.COM 
482411539SChunli.Zhang@Sun.COM 		if (postamble) {
482511539SChunli.Zhang@Sun.COM 			/* data ends in the middle of the arc_buf */
4826*11935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4827*11935SMark.Shellenbaum@Sun.COM 			    blksz);
482811539SChunli.Zhang@Sun.COM 			ASSERT(abuf);
482911576SSurya.Prakki@Sun.COM 			(void) dmu_xuio_add(xuio, abuf, 0, postamble);
483011539SChunli.Zhang@Sun.COM 		}
483111539SChunli.Zhang@Sun.COM 		break;
483211539SChunli.Zhang@Sun.COM 	case UIO_READ:
483311539SChunli.Zhang@Sun.COM 		/*
483411539SChunli.Zhang@Sun.COM 		 * Loan out an arc_buf for read if the read size is larger than
483511539SChunli.Zhang@Sun.COM 		 * the current file block size.  Block alignment is not
483611539SChunli.Zhang@Sun.COM 		 * considered.  Partial arc_buf will be loaned out for read.
483711539SChunli.Zhang@Sun.COM 		 */
483811539SChunli.Zhang@Sun.COM 		blksz = zp->z_blksz;
483911539SChunli.Zhang@Sun.COM 		if (blksz < zcr_blksz_min)
484011539SChunli.Zhang@Sun.COM 			blksz = zcr_blksz_min;
484111539SChunli.Zhang@Sun.COM 		if (blksz > zcr_blksz_max)
484211539SChunli.Zhang@Sun.COM 			blksz = zcr_blksz_max;
484311539SChunli.Zhang@Sun.COM 		/* avoid potential complexity of dealing with it */
484411539SChunli.Zhang@Sun.COM 		if (blksz > max_blksz) {
484511539SChunli.Zhang@Sun.COM 			ZFS_EXIT(zfsvfs);
484611539SChunli.Zhang@Sun.COM 			return (EINVAL);
484711539SChunli.Zhang@Sun.COM 		}
484811539SChunli.Zhang@Sun.COM 
4849*11935SMark.Shellenbaum@Sun.COM 		maxsize = zp->z_size - uio->uio_loffset;
485011539SChunli.Zhang@Sun.COM 		if (size > maxsize)
485111539SChunli.Zhang@Sun.COM 			size = maxsize;
485211539SChunli.Zhang@Sun.COM 
485311539SChunli.Zhang@Sun.COM 		if (size < blksz || vn_has_cached_data(vp)) {
485411539SChunli.Zhang@Sun.COM 			ZFS_EXIT(zfsvfs);
485511539SChunli.Zhang@Sun.COM 			return (EINVAL);
485611539SChunli.Zhang@Sun.COM 		}
485711539SChunli.Zhang@Sun.COM 		break;
485811539SChunli.Zhang@Sun.COM 	default:
485911539SChunli.Zhang@Sun.COM 		ZFS_EXIT(zfsvfs);
486011539SChunli.Zhang@Sun.COM 		return (EINVAL);
486111539SChunli.Zhang@Sun.COM 	}
486211539SChunli.Zhang@Sun.COM 
486311539SChunli.Zhang@Sun.COM 	uio->uio_extflg = UIO_XUIO;
486411539SChunli.Zhang@Sun.COM 	XUIO_XUZC_RW(xuio) = ioflag;
486511539SChunli.Zhang@Sun.COM 	ZFS_EXIT(zfsvfs);
486611539SChunli.Zhang@Sun.COM 	return (0);
486711539SChunli.Zhang@Sun.COM }
486811539SChunli.Zhang@Sun.COM 
486911539SChunli.Zhang@Sun.COM /*ARGSUSED*/
487011539SChunli.Zhang@Sun.COM static int
487111539SChunli.Zhang@Sun.COM zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
487211539SChunli.Zhang@Sun.COM {
487311539SChunli.Zhang@Sun.COM 	int i;
487411539SChunli.Zhang@Sun.COM 	arc_buf_t *abuf;
487511539SChunli.Zhang@Sun.COM 	int ioflag = XUIO_XUZC_RW(xuio);
487611539SChunli.Zhang@Sun.COM 
487711539SChunli.Zhang@Sun.COM 	ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
487811539SChunli.Zhang@Sun.COM 
487911539SChunli.Zhang@Sun.COM 	i = dmu_xuio_cnt(xuio);
488011539SChunli.Zhang@Sun.COM 	while (i-- > 0) {
488111539SChunli.Zhang@Sun.COM 		abuf = dmu_xuio_arcbuf(xuio, i);
488211539SChunli.Zhang@Sun.COM 		/*
488311539SChunli.Zhang@Sun.COM 		 * if abuf == NULL, it must be a write buffer
488411539SChunli.Zhang@Sun.COM 		 * that has been returned in zfs_write().
488511539SChunli.Zhang@Sun.COM 		 */
488611539SChunli.Zhang@Sun.COM 		if (abuf)
488711539SChunli.Zhang@Sun.COM 			dmu_return_arcbuf(abuf);
488811539SChunli.Zhang@Sun.COM 		ASSERT(abuf || ioflag == UIO_WRITE);
488911539SChunli.Zhang@Sun.COM 	}
489011539SChunli.Zhang@Sun.COM 
489111539SChunli.Zhang@Sun.COM 	dmu_xuio_fini(xuio);
489211539SChunli.Zhang@Sun.COM 	return (0);
489311539SChunli.Zhang@Sun.COM }
489411539SChunli.Zhang@Sun.COM 
489511539SChunli.Zhang@Sun.COM /*
4896789Sahrens  * Predeclare these here so that the compiler assumes that
4897789Sahrens  * this is an "old style" function declaration that does
4898789Sahrens  * not include arguments => we won't get type mismatch errors
4899789Sahrens  * in the initializations that follow.
4900789Sahrens  */
4901789Sahrens static int zfs_inval();
4902789Sahrens static int zfs_isdir();
4903789Sahrens 
4904789Sahrens static int
4905789Sahrens zfs_inval()
4906789Sahrens {
4907789Sahrens 	return (EINVAL);
4908789Sahrens }
4909789Sahrens 
4910789Sahrens static int
4911789Sahrens zfs_isdir()
4912789Sahrens {
4913789Sahrens 	return (EISDIR);
4914789Sahrens }
4915789Sahrens /*
4916789Sahrens  * Directory vnode operations template
4917789Sahrens  */
4918789Sahrens vnodeops_t *zfs_dvnodeops;
4919789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = {
49203898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
49213898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
49223898Srsb 	VOPNAME_READ,		{ .error = zfs_isdir },
49233898Srsb 	VOPNAME_WRITE,		{ .error = zfs_isdir },
49243898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
49253898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
49263898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
49273898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
49283898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
49293898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
49303898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
49313898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
49323898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
49333898Srsb 	VOPNAME_MKDIR,		{ .vop_mkdir = zfs_mkdir },
49343898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
49353898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
49363898Srsb 	VOPNAME_SYMLINK,	{ .vop_symlink = zfs_symlink },
49373898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
49383898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
49393898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
49403898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
49413898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
49423898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
49433898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
49444863Spraks 	VOPNAME_VNEVENT, 	{ .vop_vnevent = fs_vnevent_support },
49453898Srsb 	NULL,			NULL
4946789Sahrens };
4947789Sahrens 
4948789Sahrens /*
4949789Sahrens  * Regular file vnode operations template
4950789Sahrens  */
4951789Sahrens vnodeops_t *zfs_fvnodeops;
4952789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = {
49533898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
49543898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
49553898Srsb 	VOPNAME_READ,		{ .vop_read = zfs_read },
49563898Srsb 	VOPNAME_WRITE,		{ .vop_write = zfs_write },
49573898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
49583898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
49593898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
49603898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
49613898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
49623898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
49633898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
49643898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
49653898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
49663898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
49673898Srsb 	VOPNAME_FRLOCK,		{ .vop_frlock = zfs_frlock },
49683898Srsb 	VOPNAME_SPACE,		{ .vop_space = zfs_space },
49693898Srsb 	VOPNAME_GETPAGE,	{ .vop_getpage = zfs_getpage },
49703898Srsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = zfs_putpage },
49713898Srsb 	VOPNAME_MAP,		{ .vop_map = zfs_map },
49723898Srsb 	VOPNAME_ADDMAP,		{ .vop_addmap = zfs_addmap },
49733898Srsb 	VOPNAME_DELMAP,		{ .vop_delmap = zfs_delmap },
49743898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
49753898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
49763898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
49773898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
497811539SChunli.Zhang@Sun.COM 	VOPNAME_REQZCBUF, 	{ .vop_reqzcbuf = zfs_reqzcbuf },
497911539SChunli.Zhang@Sun.COM 	VOPNAME_RETZCBUF, 	{ .vop_retzcbuf = zfs_retzcbuf },
49803898Srsb 	NULL,			NULL
4981789Sahrens };
4982789Sahrens 
4983789Sahrens /*
4984789Sahrens  * Symbolic link vnode operations template
4985789Sahrens  */
4986789Sahrens vnodeops_t *zfs_symvnodeops;
4987789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = {
49883898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
49893898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
49903898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
49913898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
49923898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = zfs_readlink },
49933898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
49943898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
49953898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
49963898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
49973898Srsb 	NULL,			NULL
4998789Sahrens };
4999789Sahrens 
5000789Sahrens /*
50018845Samw@Sun.COM  * special share hidden files vnode operations template
50028845Samw@Sun.COM  */
50038845Samw@Sun.COM vnodeops_t *zfs_sharevnodeops;
50048845Samw@Sun.COM const fs_operation_def_t zfs_sharevnodeops_template[] = {
50058845Samw@Sun.COM 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
50068845Samw@Sun.COM 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
50078845Samw@Sun.COM 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
50088845Samw@Sun.COM 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
50098845Samw@Sun.COM 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50108845Samw@Sun.COM 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
50118845Samw@Sun.COM 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
50128845Samw@Sun.COM 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
50138845Samw@Sun.COM 	NULL,			NULL
50148845Samw@Sun.COM };
50158845Samw@Sun.COM 
50168845Samw@Sun.COM /*
5017789Sahrens  * Extended attribute directory vnode operations template
5018789Sahrens  *	This template is identical to the directory vnodes
5019789Sahrens  *	operation template except for restricted operations:
5020789Sahrens  *		VOP_MKDIR()
5021789Sahrens  *		VOP_SYMLINK()
5022789Sahrens  * Note that there are other restrictions embedded in:
5023789Sahrens  *	zfs_create()	- restrict type to VREG
5024789Sahrens  *	zfs_link()	- no links into/out of attribute space
5025789Sahrens  *	zfs_rename()	- no moves into/out of attribute space
5026789Sahrens  */
5027789Sahrens vnodeops_t *zfs_xdvnodeops;
5028789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = {
50293898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
50303898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
50313898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
50323898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
50333898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
50343898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
50353898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
50363898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
50373898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
50383898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
50393898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
50403898Srsb 	VOPNAME_MKDIR,		{ .error = zfs_inval },
50413898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
50423898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
50433898Srsb 	VOPNAME_SYMLINK,	{ .error = zfs_inval },
50443898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
50453898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
50463898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
50473898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
50483898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50493898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
50503898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
50513898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
50523898Srsb 	NULL,			NULL
5053789Sahrens };
5054789Sahrens 
5055789Sahrens /*
5056789Sahrens  * Error vnode operations template
5057789Sahrens  */
5058789Sahrens vnodeops_t *zfs_evnodeops;
5059789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = {
50603898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
50613898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50623898Srsb 	NULL,			NULL
5063789Sahrens };
5064