xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 12164:0eb8d6741e37)
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 /*
2212079SMark.Shellenbaum@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
254144Speteh /* Portions Copyright 2007 Jeremy Teo */
264144Speteh 
27789Sahrens #include <sys/types.h>
28789Sahrens #include <sys/param.h>
29789Sahrens #include <sys/time.h>
30789Sahrens #include <sys/systm.h>
31789Sahrens #include <sys/sysmacros.h>
32789Sahrens #include <sys/resource.h>
33789Sahrens #include <sys/vfs.h>
343898Srsb #include <sys/vfs_opreg.h>
35789Sahrens #include <sys/vnode.h>
36789Sahrens #include <sys/file.h>
37789Sahrens #include <sys/stat.h>
38789Sahrens #include <sys/kmem.h>
39789Sahrens #include <sys/taskq.h>
40789Sahrens #include <sys/uio.h>
41789Sahrens #include <sys/vmsystm.h>
42789Sahrens #include <sys/atomic.h>
432688Smaybee #include <sys/vm.h>
44789Sahrens #include <vm/seg_vn.h>
45789Sahrens #include <vm/pvn.h>
46789Sahrens #include <vm/as.h>
477315SJonathan.Adams@Sun.COM #include <vm/kpm.h>
487315SJonathan.Adams@Sun.COM #include <vm/seg_kpm.h>
49789Sahrens #include <sys/mman.h>
50789Sahrens #include <sys/pathname.h>
51789Sahrens #include <sys/cmn_err.h>
52789Sahrens #include <sys/errno.h>
53789Sahrens #include <sys/unistd.h>
54789Sahrens #include <sys/zfs_dir.h>
55789Sahrens #include <sys/zfs_acl.h>
56789Sahrens #include <sys/zfs_ioctl.h>
57789Sahrens #include <sys/fs/zfs.h>
58789Sahrens #include <sys/dmu.h>
59789Sahrens #include <sys/spa.h>
60789Sahrens #include <sys/txg.h>
61789Sahrens #include <sys/dbuf.h>
62789Sahrens #include <sys/zap.h>
6311935SMark.Shellenbaum@Sun.COM #include <sys/sa.h>
64789Sahrens #include <sys/dirent.h>
65789Sahrens #include <sys/policy.h>
66789Sahrens #include <sys/sunddi.h>
67789Sahrens #include <sys/filio.h>
687847SMark.Shellenbaum@Sun.COM #include <sys/sid.h>
69789Sahrens #include "fs/fs_subr.h"
70789Sahrens #include <sys/zfs_ctldir.h>
715331Samw #include <sys/zfs_fuid.h>
7211935SMark.Shellenbaum@Sun.COM #include <sys/zfs_sa.h>
731484Sek110237 #include <sys/dnlc.h>
741669Sperrin #include <sys/zfs_rlock.h>
755331Samw #include <sys/extdirent.h>
765331Samw #include <sys/kidmap.h>
7711134SCasper.Dik@Sun.COM #include <sys/cred.h>
785663Sck153898 #include <sys/attr.h>
79789Sahrens 
80789Sahrens /*
81789Sahrens  * Programming rules.
82789Sahrens  *
83789Sahrens  * Each vnode op performs some logical unit of work.  To do this, the ZPL must
84789Sahrens  * properly lock its in-core state, create a DMU transaction, do the work,
85789Sahrens  * record this work in the intent log (ZIL), commit the DMU transaction,
865331Samw  * and wait for the intent log to commit if it is a synchronous operation.
875331Samw  * Moreover, the vnode ops must work in both normal and log replay context.
88789Sahrens  * The ordering of events is important to avoid deadlocks and references
89789Sahrens  * to freed memory.  The example below illustrates the following Big Rules:
90789Sahrens  *
91789Sahrens  *  (1) A check must be made in each zfs thread for a mounted file system.
925367Sahrens  *	This is done avoiding races using ZFS_ENTER(zfsvfs).
935367Sahrens  *      A ZFS_EXIT(zfsvfs) is needed before all returns.  Any znodes
945367Sahrens  *      must be checked with ZFS_VERIFY_ZP(zp).  Both of these macros
955367Sahrens  *      can return EIO from the calling function.
96789Sahrens  *
97789Sahrens  *  (2)	VN_RELE() should always be the last thing except for zil_commit()
982638Sperrin  *	(if necessary) and ZFS_EXIT(). This is for 3 reasons:
99789Sahrens  *	First, if it's the last reference, the vnode/znode
100789Sahrens  *	can be freed, so the zp may point to freed memory.  Second, the last
101789Sahrens  *	reference will call zfs_zinactive(), which may induce a lot of work --
1021669Sperrin  *	pushing cached pages (which acquires range locks) and syncing out
103789Sahrens  *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
104789Sahrens  *	which could deadlock the system if you were already holding one.
1059321SNeil.Perrin@Sun.COM  *	If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
106789Sahrens  *
1071757Sperrin  *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
1081757Sperrin  *	as they can span dmu_tx_assign() calls.
1091757Sperrin  *
1108227SNeil.Perrin@Sun.COM  *  (4)	Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
111789Sahrens  *	This is critical because we don't want to block while holding locks.
112789Sahrens  *	Note, in particular, that if a lock is sometimes acquired before
113789Sahrens  *	the tx assigns, and sometimes after (e.g. z_lock), then failing to
114789Sahrens  *	use a non-blocking assign can deadlock the system.  The scenario:
115789Sahrens  *
116789Sahrens  *	Thread A has grabbed a lock before calling dmu_tx_assign().
117789Sahrens  *	Thread B is in an already-assigned tx, and blocks for this lock.
118789Sahrens  *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
119789Sahrens  *	forever, because the previous txg can't quiesce until B's tx commits.
120789Sahrens  *
121789Sahrens  *	If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
1222113Sahrens  *	then drop all locks, call dmu_tx_wait(), and try again.
123789Sahrens  *
1241757Sperrin  *  (5)	If the operation succeeded, generate the intent log entry for it
125789Sahrens  *	before dropping locks.  This ensures that the ordering of events
126789Sahrens  *	in the intent log matches the order in which they actually occurred.
1278227SNeil.Perrin@Sun.COM  *      During ZIL replay the zfs_log_* functions will update the sequence
1288227SNeil.Perrin@Sun.COM  *	number to indicate the zil transaction has replayed.
129789Sahrens  *
1301757Sperrin  *  (6)	At the end of each vnode op, the DMU tx must always commit,
131789Sahrens  *	regardless of whether there were any errors.
132789Sahrens  *
1332638Sperrin  *  (7)	After dropping all locks, invoke zil_commit(zilog, seq, foid)
134789Sahrens  *	to ensure that synchronous semantics are provided when necessary.
135789Sahrens  *
136789Sahrens  * In general, this is how things should be ordered in each vnode op:
137789Sahrens  *
138789Sahrens  *	ZFS_ENTER(zfsvfs);		// exit if unmounted
139789Sahrens  * top:
140789Sahrens  *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
141789Sahrens  *	rw_enter(...);			// grab any other locks you need
142789Sahrens  *	tx = dmu_tx_create(...);	// get DMU tx
143789Sahrens  *	dmu_tx_hold_*();		// hold each object you might modify
1448227SNeil.Perrin@Sun.COM  *	error = dmu_tx_assign(tx, TXG_NOWAIT);	// try to assign
145789Sahrens  *	if (error) {
146789Sahrens  *		rw_exit(...);		// drop locks
147789Sahrens  *		zfs_dirent_unlock(dl);	// unlock directory entry
148789Sahrens  *		VN_RELE(...);		// release held vnodes
1498227SNeil.Perrin@Sun.COM  *		if (error == ERESTART) {
1502113Sahrens  *			dmu_tx_wait(tx);
1512113Sahrens  *			dmu_tx_abort(tx);
152789Sahrens  *			goto top;
153789Sahrens  *		}
1542113Sahrens  *		dmu_tx_abort(tx);	// abort DMU tx
155789Sahrens  *		ZFS_EXIT(zfsvfs);	// finished in zfs
156789Sahrens  *		return (error);		// really out of space
157789Sahrens  *	}
158789Sahrens  *	error = do_real_work();		// do whatever this VOP does
159789Sahrens  *	if (error == 0)
1602638Sperrin  *		zfs_log_*(...);		// on success, make ZIL entry
161789Sahrens  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
162789Sahrens  *	rw_exit(...);			// drop locks
163789Sahrens  *	zfs_dirent_unlock(dl);		// unlock directory entry
164789Sahrens  *	VN_RELE(...);			// release held vnodes
1652638Sperrin  *	zil_commit(zilog, seq, foid);	// synchronous when necessary
166789Sahrens  *	ZFS_EXIT(zfsvfs);		// finished in zfs
167789Sahrens  *	return (error);			// done, report error
168789Sahrens  */
1695367Sahrens 
170789Sahrens /* ARGSUSED */
171789Sahrens static int
1725331Samw zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
173789Sahrens {
1743063Sperrin 	znode_t	*zp = VTOZ(*vpp);
1757844SMark.Shellenbaum@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1767844SMark.Shellenbaum@Sun.COM 
1777844SMark.Shellenbaum@Sun.COM 	ZFS_ENTER(zfsvfs);
1787844SMark.Shellenbaum@Sun.COM 	ZFS_VERIFY_ZP(zp);
1793063Sperrin 
18011935SMark.Shellenbaum@Sun.COM 	if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
1815331Samw 	    ((flag & FAPPEND) == 0)) {
1827844SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
1835331Samw 		return (EPERM);
1845331Samw 	}
1855331Samw 
1865331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
1875331Samw 	    ZTOV(zp)->v_type == VREG &&
18811935SMark.Shellenbaum@Sun.COM 	    !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
1897844SMark.Shellenbaum@Sun.COM 		if (fs_vscan(*vpp, cr, 0) != 0) {
1907844SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
1915331Samw 			return (EACCES);
1927844SMark.Shellenbaum@Sun.COM 		}
1937844SMark.Shellenbaum@Sun.COM 	}
1945331Samw 
1953063Sperrin 	/* Keep a count of the synchronous opens in the znode */
1963063Sperrin 	if (flag & (FSYNC | FDSYNC))
1973063Sperrin 		atomic_inc_32(&zp->z_sync_cnt);
1985331Samw 
1997844SMark.Shellenbaum@Sun.COM 	ZFS_EXIT(zfsvfs);
200789Sahrens 	return (0);
201789Sahrens }
202789Sahrens 
203789Sahrens /* ARGSUSED */
204789Sahrens static int
2055331Samw zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
2065331Samw     caller_context_t *ct)
207789Sahrens {
2083063Sperrin 	znode_t	*zp = VTOZ(vp);
2097844SMark.Shellenbaum@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2107844SMark.Shellenbaum@Sun.COM 
2119909Schris.kirby@sun.com 	/*
2129909Schris.kirby@sun.com 	 * Clean up any locks held by this process on the vp.
2139909Schris.kirby@sun.com 	 */
2149909Schris.kirby@sun.com 	cleanlocks(vp, ddi_get_pid(), 0);
2159909Schris.kirby@sun.com 	cleanshares(vp, ddi_get_pid());
2169909Schris.kirby@sun.com 
2177844SMark.Shellenbaum@Sun.COM 	ZFS_ENTER(zfsvfs);
2187844SMark.Shellenbaum@Sun.COM 	ZFS_VERIFY_ZP(zp);
2193063Sperrin 
2203063Sperrin 	/* Decrement the synchronous opens in the znode */
2214339Sperrin 	if ((flag & (FSYNC | FDSYNC)) && (count == 1))
2223063Sperrin 		atomic_dec_32(&zp->z_sync_cnt);
2233063Sperrin 
2245331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
2255331Samw 	    ZTOV(zp)->v_type == VREG &&
22611935SMark.Shellenbaum@Sun.COM 	    !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
2275331Samw 		VERIFY(fs_vscan(vp, cr, 1) == 0);
2285331Samw 
2297844SMark.Shellenbaum@Sun.COM 	ZFS_EXIT(zfsvfs);
230789Sahrens 	return (0);
231789Sahrens }
232789Sahrens 
233789Sahrens /*
234789Sahrens  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
235789Sahrens  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
236789Sahrens  */
237789Sahrens static int
238789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off)
239789Sahrens {
240789Sahrens 	znode_t	*zp = VTOZ(vp);
241789Sahrens 	uint64_t noff = (uint64_t)*off; /* new offset */
242789Sahrens 	uint64_t file_sz;
243789Sahrens 	int error;
244789Sahrens 	boolean_t hole;
245789Sahrens 
24611935SMark.Shellenbaum@Sun.COM 	file_sz = zp->z_size;
247789Sahrens 	if (noff >= file_sz)  {
248789Sahrens 		return (ENXIO);
249789Sahrens 	}
250789Sahrens 
251789Sahrens 	if (cmd == _FIO_SEEK_HOLE)
252789Sahrens 		hole = B_TRUE;
253789Sahrens 	else
254789Sahrens 		hole = B_FALSE;
255789Sahrens 
256789Sahrens 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
257789Sahrens 
258789Sahrens 	/* end of file? */
259789Sahrens 	if ((error == ESRCH) || (noff > file_sz)) {
260789Sahrens 		/*
261789Sahrens 		 * Handle the virtual hole at the end of file.
262789Sahrens 		 */
263789Sahrens 		if (hole) {
264789Sahrens 			*off = file_sz;
265789Sahrens 			return (0);
266789Sahrens 		}
267789Sahrens 		return (ENXIO);
268789Sahrens 	}
269789Sahrens 
270789Sahrens 	if (noff < *off)
271789Sahrens 		return (error);
272789Sahrens 	*off = noff;
273789Sahrens 	return (error);
274789Sahrens }
275789Sahrens 
276789Sahrens /* ARGSUSED */
277789Sahrens static int
278789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
2795331Samw     int *rvalp, caller_context_t *ct)
280789Sahrens {
281789Sahrens 	offset_t off;
282789Sahrens 	int error;
283789Sahrens 	zfsvfs_t *zfsvfs;
2845326Sek110237 	znode_t *zp;
285789Sahrens 
286789Sahrens 	switch (com) {
2874339Sperrin 	case _FIOFFS:
288789Sahrens 		return (zfs_sync(vp->v_vfsp, 0, cred));
289789Sahrens 
2901544Seschrock 		/*
2911544Seschrock 		 * The following two ioctls are used by bfu.  Faking out,
2921544Seschrock 		 * necessary to avoid bfu errors.
2931544Seschrock 		 */
2944339Sperrin 	case _FIOGDIO:
2954339Sperrin 	case _FIOSDIO:
2961544Seschrock 		return (0);
2971544Seschrock 
2984339Sperrin 	case _FIO_SEEK_DATA:
2994339Sperrin 	case _FIO_SEEK_HOLE:
300789Sahrens 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
301789Sahrens 			return (EFAULT);
302789Sahrens 
3035326Sek110237 		zp = VTOZ(vp);
3045326Sek110237 		zfsvfs = zp->z_zfsvfs;
3055367Sahrens 		ZFS_ENTER(zfsvfs);
3065367Sahrens 		ZFS_VERIFY_ZP(zp);
307789Sahrens 
308789Sahrens 		/* offset parameter is in/out */
309789Sahrens 		error = zfs_holey(vp, com, &off);
310789Sahrens 		ZFS_EXIT(zfsvfs);
311789Sahrens 		if (error)
312789Sahrens 			return (error);
313789Sahrens 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
314789Sahrens 			return (EFAULT);
315789Sahrens 		return (0);
316789Sahrens 	}
317789Sahrens 	return (ENOTTY);
318789Sahrens }
319789Sahrens 
320789Sahrens /*
3217315SJonathan.Adams@Sun.COM  * Utility functions to map and unmap a single physical page.  These
3227315SJonathan.Adams@Sun.COM  * are used to manage the mappable copies of ZFS file data, and therefore
3237315SJonathan.Adams@Sun.COM  * do not update ref/mod bits.
3247315SJonathan.Adams@Sun.COM  */
3257315SJonathan.Adams@Sun.COM caddr_t
3267315SJonathan.Adams@Sun.COM zfs_map_page(page_t *pp, enum seg_rw rw)
3277315SJonathan.Adams@Sun.COM {
3287315SJonathan.Adams@Sun.COM 	if (kpm_enable)
3297315SJonathan.Adams@Sun.COM 		return (hat_kpm_mapin(pp, 0));
3307315SJonathan.Adams@Sun.COM 	ASSERT(rw == S_READ || rw == S_WRITE);
3317315SJonathan.Adams@Sun.COM 	return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0),
3327315SJonathan.Adams@Sun.COM 	    (caddr_t)-1));
3337315SJonathan.Adams@Sun.COM }
3347315SJonathan.Adams@Sun.COM 
3357315SJonathan.Adams@Sun.COM void
3367315SJonathan.Adams@Sun.COM zfs_unmap_page(page_t *pp, caddr_t addr)
3377315SJonathan.Adams@Sun.COM {
3387315SJonathan.Adams@Sun.COM 	if (kpm_enable) {
3397315SJonathan.Adams@Sun.COM 		hat_kpm_mapout(pp, 0, addr);
3407315SJonathan.Adams@Sun.COM 	} else {
3417315SJonathan.Adams@Sun.COM 		ppmapout(addr);
3427315SJonathan.Adams@Sun.COM 	}
3437315SJonathan.Adams@Sun.COM }
3447315SJonathan.Adams@Sun.COM 
3457315SJonathan.Adams@Sun.COM /*
346789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
347789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
348789Sahrens  *
349789Sahrens  * On Write:	If we find a memory mapped page, we write to *both*
350789Sahrens  *		the page and the dmu buffer.
351789Sahrens  */
3528636SMark.Maybee@Sun.COM static void
3538636SMark.Maybee@Sun.COM update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid)
354789Sahrens {
3558636SMark.Maybee@Sun.COM 	int64_t	off;
3568636SMark.Maybee@Sun.COM 
357789Sahrens 	off = start & PAGEOFFSET;
358789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
359789Sahrens 		page_t *pp;
3608636SMark.Maybee@Sun.COM 		uint64_t nbytes = MIN(PAGESIZE - off, len);
3618636SMark.Maybee@Sun.COM 
362789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
363789Sahrens 			caddr_t va;
364789Sahrens 
3657315SJonathan.Adams@Sun.COM 			va = zfs_map_page(pp, S_WRITE);
3669512SNeil.Perrin@Sun.COM 			(void) dmu_read(os, oid, start+off, nbytes, va+off,
3679512SNeil.Perrin@Sun.COM 			    DMU_READ_PREFETCH);
3687315SJonathan.Adams@Sun.COM 			zfs_unmap_page(pp, va);
369789Sahrens 			page_unlock(pp);
370789Sahrens 		}
3718636SMark.Maybee@Sun.COM 		len -= nbytes;
372789Sahrens 		off = 0;
373789Sahrens 	}
374789Sahrens }
375789Sahrens 
376789Sahrens /*
377789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
378789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
379789Sahrens  *
380789Sahrens  * On Read:	We "read" preferentially from memory mapped pages,
381789Sahrens  *		else we default from the dmu buffer.
382789Sahrens  *
383789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
384789Sahrens  *	the file is memory mapped.
385789Sahrens  */
386789Sahrens static int
3873638Sbillm mappedread(vnode_t *vp, int nbytes, uio_t *uio)
388789Sahrens {
3893638Sbillm 	znode_t *zp = VTOZ(vp);
3903638Sbillm 	objset_t *os = zp->z_zfsvfs->z_os;
3913638Sbillm 	int64_t	start, off;
392789Sahrens 	int len = nbytes;
393789Sahrens 	int error = 0;
394789Sahrens 
395789Sahrens 	start = uio->uio_loffset;
396789Sahrens 	off = start & PAGEOFFSET;
397789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
398789Sahrens 		page_t *pp;
3993638Sbillm 		uint64_t bytes = MIN(PAGESIZE - off, len);
4003638Sbillm 
401789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
402789Sahrens 			caddr_t va;
403789Sahrens 
4047315SJonathan.Adams@Sun.COM 			va = zfs_map_page(pp, S_READ);
405789Sahrens 			error = uiomove(va + off, bytes, UIO_READ, uio);
4067315SJonathan.Adams@Sun.COM 			zfs_unmap_page(pp, va);
407789Sahrens 			page_unlock(pp);
408789Sahrens 		} else {
4093638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, bytes);
410789Sahrens 		}
411789Sahrens 		len -= bytes;
412789Sahrens 		off = 0;
413789Sahrens 		if (error)
414789Sahrens 			break;
415789Sahrens 	}
416789Sahrens 	return (error);
417789Sahrens }
418789Sahrens 
4193638Sbillm offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
420789Sahrens 
421789Sahrens /*
422789Sahrens  * Read bytes from specified file into supplied buffer.
423789Sahrens  *
424789Sahrens  *	IN:	vp	- vnode of file to be read from.
425789Sahrens  *		uio	- structure supplying read location, range info,
426789Sahrens  *			  and return buffer.
427789Sahrens  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
428789Sahrens  *		cr	- credentials of caller.
4295331Samw  *		ct	- caller context
430789Sahrens  *
431789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
432789Sahrens  *
433789Sahrens  *	RETURN:	0 if success
434789Sahrens  *		error code if failure
435789Sahrens  *
436789Sahrens  * Side Effects:
437789Sahrens  *	vp - atime updated if byte count > 0
438789Sahrens  */
439789Sahrens /* ARGSUSED */
440789Sahrens static int
441789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
442789Sahrens {
443789Sahrens 	znode_t		*zp = VTOZ(vp);
444789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4455326Sek110237 	objset_t	*os;
4463638Sbillm 	ssize_t		n, nbytes;
4473638Sbillm 	int		error;
4481669Sperrin 	rl_t		*rl;
44911539SChunli.Zhang@Sun.COM 	xuio_t		*xuio = NULL;
450789Sahrens 
4515367Sahrens 	ZFS_ENTER(zfsvfs);
4525367Sahrens 	ZFS_VERIFY_ZP(zp);
4535326Sek110237 	os = zfsvfs->z_os;
454789Sahrens 
45511935SMark.Shellenbaum@Sun.COM 	if (zp->z_pflags & ZFS_AV_QUARANTINED) {
4565929Smarks 		ZFS_EXIT(zfsvfs);
4575929Smarks 		return (EACCES);
4585929Smarks 	}
4595929Smarks 
460789Sahrens 	/*
461789Sahrens 	 * Validate file offset
462789Sahrens 	 */
463789Sahrens 	if (uio->uio_loffset < (offset_t)0) {
464789Sahrens 		ZFS_EXIT(zfsvfs);
465789Sahrens 		return (EINVAL);
466789Sahrens 	}
467789Sahrens 
468789Sahrens 	/*
469789Sahrens 	 * Fasttrack empty reads
470789Sahrens 	 */
471789Sahrens 	if (uio->uio_resid == 0) {
472789Sahrens 		ZFS_EXIT(zfsvfs);
473789Sahrens 		return (0);
474789Sahrens 	}
475789Sahrens 
476789Sahrens 	/*
4771669Sperrin 	 * Check for mandatory locks
478789Sahrens 	 */
47911935SMark.Shellenbaum@Sun.COM 	if (MANDMODE(zp->z_mode)) {
480789Sahrens 		if (error = chklock(vp, FREAD,
481789Sahrens 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
482789Sahrens 			ZFS_EXIT(zfsvfs);
483789Sahrens 			return (error);
484789Sahrens 		}
485789Sahrens 	}
486789Sahrens 
487789Sahrens 	/*
488789Sahrens 	 * If we're in FRSYNC mode, sync out this znode before reading it.
489789Sahrens 	 */
4902638Sperrin 	if (ioflag & FRSYNC)
4912638Sperrin 		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
492789Sahrens 
493789Sahrens 	/*
4941669Sperrin 	 * Lock the range against changes.
495789Sahrens 	 */
4961669Sperrin 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
4971669Sperrin 
498789Sahrens 	/*
499789Sahrens 	 * If we are reading past end-of-file we can skip
500789Sahrens 	 * to the end; but we might still need to set atime.
501789Sahrens 	 */
50211935SMark.Shellenbaum@Sun.COM 	if (uio->uio_loffset >= zp->z_size) {
503789Sahrens 		error = 0;
504789Sahrens 		goto out;
505789Sahrens 	}
506789Sahrens 
50711935SMark.Shellenbaum@Sun.COM 	ASSERT(uio->uio_loffset < zp->z_size);
50811935SMark.Shellenbaum@Sun.COM 	n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
5093638Sbillm 
51011539SChunli.Zhang@Sun.COM 	if ((uio->uio_extflg == UIO_XUIO) &&
51111539SChunli.Zhang@Sun.COM 	    (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
51211539SChunli.Zhang@Sun.COM 		int nblk;
51311539SChunli.Zhang@Sun.COM 		int blksz = zp->z_blksz;
51411539SChunli.Zhang@Sun.COM 		uint64_t offset = uio->uio_loffset;
51511539SChunli.Zhang@Sun.COM 
51611539SChunli.Zhang@Sun.COM 		xuio = (xuio_t *)uio;
51711539SChunli.Zhang@Sun.COM 		if ((ISP2(blksz))) {
51811539SChunli.Zhang@Sun.COM 			nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
51911539SChunli.Zhang@Sun.COM 			    blksz)) / blksz;
52011539SChunli.Zhang@Sun.COM 		} else {
52111539SChunli.Zhang@Sun.COM 			ASSERT(offset + n <= blksz);
52211539SChunli.Zhang@Sun.COM 			nblk = 1;
52311539SChunli.Zhang@Sun.COM 		}
52411576SSurya.Prakki@Sun.COM 		(void) dmu_xuio_init(xuio, nblk);
52511539SChunli.Zhang@Sun.COM 
52611539SChunli.Zhang@Sun.COM 		if (vn_has_cached_data(vp)) {
52711539SChunli.Zhang@Sun.COM 			/*
52811539SChunli.Zhang@Sun.COM 			 * For simplicity, we always allocate a full buffer
52911539SChunli.Zhang@Sun.COM 			 * even if we only expect to read a portion of a block.
53011539SChunli.Zhang@Sun.COM 			 */
53111539SChunli.Zhang@Sun.COM 			while (--nblk >= 0) {
53211576SSurya.Prakki@Sun.COM 				(void) dmu_xuio_add(xuio,
53311935SMark.Shellenbaum@Sun.COM 				    dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
53411935SMark.Shellenbaum@Sun.COM 				    blksz), 0, blksz);
53511539SChunli.Zhang@Sun.COM 			}
53611539SChunli.Zhang@Sun.COM 		}
53711539SChunli.Zhang@Sun.COM 	}
53811539SChunli.Zhang@Sun.COM 
5393638Sbillm 	while (n > 0) {
5403638Sbillm 		nbytes = MIN(n, zfs_read_chunk_size -
5413638Sbillm 		    P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
5423638Sbillm 
5433638Sbillm 		if (vn_has_cached_data(vp))
5443638Sbillm 			error = mappedread(vp, nbytes, uio);
5453638Sbillm 		else
5463638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, nbytes);
5477294Sperrin 		if (error) {
5487294Sperrin 			/* convert checksum errors into IO errors */
5497294Sperrin 			if (error == ECKSUM)
5507294Sperrin 				error = EIO;
5513638Sbillm 			break;
5527294Sperrin 		}
5533638Sbillm 
5543638Sbillm 		n -= nbytes;
555789Sahrens 	}
556789Sahrens out:
5572237Smaybee 	zfs_range_unlock(rl);
558789Sahrens 
559789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
560789Sahrens 	ZFS_EXIT(zfsvfs);
561789Sahrens 	return (error);
562789Sahrens }
563789Sahrens 
564789Sahrens /*
565789Sahrens  * Write the bytes to a file.
566789Sahrens  *
567789Sahrens  *	IN:	vp	- vnode of file to be written to.
568789Sahrens  *		uio	- structure supplying write location, range info,
569789Sahrens  *			  and data buffer.
570789Sahrens  *		ioflag	- FAPPEND flag set if in append mode.
571789Sahrens  *		cr	- credentials of caller.
5725331Samw  *		ct	- caller context (NFS/CIFS fem monitor only)
573789Sahrens  *
574789Sahrens  *	OUT:	uio	- updated offset and range.
575789Sahrens  *
576789Sahrens  *	RETURN:	0 if success
577789Sahrens  *		error code if failure
578789Sahrens  *
579789Sahrens  * Timestamps:
580789Sahrens  *	vp - ctime|mtime updated if byte count > 0
581789Sahrens  */
58211935SMark.Shellenbaum@Sun.COM 
583789Sahrens /* ARGSUSED */
584789Sahrens static int
585789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
586789Sahrens {
587789Sahrens 	znode_t		*zp = VTOZ(vp);
588789Sahrens 	rlim64_t	limit = uio->uio_llimit;
589789Sahrens 	ssize_t		start_resid = uio->uio_resid;
590789Sahrens 	ssize_t		tx_bytes;
591789Sahrens 	uint64_t	end_size;
592789Sahrens 	dmu_tx_t	*tx;
593789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
5945326Sek110237 	zilog_t		*zilog;
595789Sahrens 	offset_t	woff;
596789Sahrens 	ssize_t		n, nbytes;
5971669Sperrin 	rl_t		*rl;
598789Sahrens 	int		max_blksz = zfsvfs->z_max_blksz;
5991669Sperrin 	int		error;
6009412SAleksandr.Guzovskiy@Sun.COM 	arc_buf_t	*abuf;
60111539SChunli.Zhang@Sun.COM 	iovec_t		*aiov;
60211539SChunli.Zhang@Sun.COM 	xuio_t		*xuio = NULL;
60311539SChunli.Zhang@Sun.COM 	int		i_iov = 0;
60411539SChunli.Zhang@Sun.COM 	int		iovcnt = uio->uio_iovcnt;
60511539SChunli.Zhang@Sun.COM 	iovec_t		*iovp = uio->uio_iov;
60611539SChunli.Zhang@Sun.COM 	int		write_eof;
60711935SMark.Shellenbaum@Sun.COM 	int		count = 0;
60811935SMark.Shellenbaum@Sun.COM 	sa_bulk_attr_t	bulk[4];
60911935SMark.Shellenbaum@Sun.COM 	uint64_t	mtime[2], ctime[2];
610789Sahrens 
611789Sahrens 	/*
612789Sahrens 	 * Fasttrack empty write
613789Sahrens 	 */
6141669Sperrin 	n = start_resid;
615789Sahrens 	if (n == 0)
616789Sahrens 		return (0);
617789Sahrens 
6181669Sperrin 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
6191669Sperrin 		limit = MAXOFFSET_T;
6201669Sperrin 
6215367Sahrens 	ZFS_ENTER(zfsvfs);
6225367Sahrens 	ZFS_VERIFY_ZP(zp);
6236743Smarks 
62411935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
62511935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
62611935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
62711935SMark.Shellenbaum@Sun.COM 	    &zp->z_size, 8);
62811935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
62911935SMark.Shellenbaum@Sun.COM 	    &zp->z_pflags, 8);
63011935SMark.Shellenbaum@Sun.COM 
6316743Smarks 	/*
6326743Smarks 	 * If immutable or not appending then return EPERM
6336743Smarks 	 */
63411935SMark.Shellenbaum@Sun.COM 	if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
63511935SMark.Shellenbaum@Sun.COM 	    ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
63611935SMark.Shellenbaum@Sun.COM 	    (uio->uio_loffset < zp->z_size))) {
6376743Smarks 		ZFS_EXIT(zfsvfs);
6386743Smarks 		return (EPERM);
6396743Smarks 	}
6406743Smarks 
6415326Sek110237 	zilog = zfsvfs->z_log;
642789Sahrens 
643789Sahrens 	/*
64411083Swilliam.gorrell@sun.com 	 * Validate file offset
64511083Swilliam.gorrell@sun.com 	 */
64611935SMark.Shellenbaum@Sun.COM 	woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
64711083Swilliam.gorrell@sun.com 	if (woff < 0) {
64811083Swilliam.gorrell@sun.com 		ZFS_EXIT(zfsvfs);
64911083Swilliam.gorrell@sun.com 		return (EINVAL);
65011083Swilliam.gorrell@sun.com 	}
65111083Swilliam.gorrell@sun.com 
65211083Swilliam.gorrell@sun.com 	/*
65311083Swilliam.gorrell@sun.com 	 * Check for mandatory locks before calling zfs_range_lock()
65411083Swilliam.gorrell@sun.com 	 * in order to prevent a deadlock with locks set via fcntl().
65511083Swilliam.gorrell@sun.com 	 */
65611935SMark.Shellenbaum@Sun.COM 	if (MANDMODE((mode_t)zp->z_mode) &&
65711083Swilliam.gorrell@sun.com 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
65811083Swilliam.gorrell@sun.com 		ZFS_EXIT(zfsvfs);
65911083Swilliam.gorrell@sun.com 		return (error);
66011083Swilliam.gorrell@sun.com 	}
66111083Swilliam.gorrell@sun.com 
66211083Swilliam.gorrell@sun.com 	/*
6632237Smaybee 	 * Pre-fault the pages to ensure slow (eg NFS) pages
6641669Sperrin 	 * don't hold up txg.
66511539SChunli.Zhang@Sun.COM 	 * Skip this if uio contains loaned arc_buf.
666789Sahrens 	 */
66711539SChunli.Zhang@Sun.COM 	if ((uio->uio_extflg == UIO_XUIO) &&
66811539SChunli.Zhang@Sun.COM 	    (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
66911539SChunli.Zhang@Sun.COM 		xuio = (xuio_t *)uio;
67011539SChunli.Zhang@Sun.COM 	else
67111539SChunli.Zhang@Sun.COM 		uio_prefaultpages(n, uio);
672789Sahrens 
673789Sahrens 	/*
674789Sahrens 	 * If in append mode, set the io offset pointer to eof.
675789Sahrens 	 */
6761669Sperrin 	if (ioflag & FAPPEND) {
6771669Sperrin 		/*
67811083Swilliam.gorrell@sun.com 		 * Obtain an appending range lock to guarantee file append
67911083Swilliam.gorrell@sun.com 		 * semantics.  We reset the write offset once we have the lock.
6801669Sperrin 		 */
6811669Sperrin 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
68211083Swilliam.gorrell@sun.com 		woff = rl->r_off;
6831669Sperrin 		if (rl->r_len == UINT64_MAX) {
68411083Swilliam.gorrell@sun.com 			/*
68511083Swilliam.gorrell@sun.com 			 * We overlocked the file because this write will cause
68611083Swilliam.gorrell@sun.com 			 * the file block size to increase.
68711083Swilliam.gorrell@sun.com 			 * Note that zp_size cannot change with this lock held.
68811083Swilliam.gorrell@sun.com 			 */
68911935SMark.Shellenbaum@Sun.COM 			woff = zp->z_size;
6901669Sperrin 		}
69111083Swilliam.gorrell@sun.com 		uio->uio_loffset = woff;
692789Sahrens 	} else {
693789Sahrens 		/*
69411083Swilliam.gorrell@sun.com 		 * Note that if the file block size will change as a result of
69511083Swilliam.gorrell@sun.com 		 * this write, then this range lock will lock the entire file
69611083Swilliam.gorrell@sun.com 		 * so that we can re-write the block safely.
697789Sahrens 		 */
6981669Sperrin 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
699789Sahrens 	}
700789Sahrens 
701789Sahrens 	if (woff >= limit) {
7023638Sbillm 		zfs_range_unlock(rl);
7033638Sbillm 		ZFS_EXIT(zfsvfs);
7043638Sbillm 		return (EFBIG);
705789Sahrens 	}
706789Sahrens 
707789Sahrens 	if ((woff + n) > limit || woff > (limit - n))
708789Sahrens 		n = limit - woff;
709789Sahrens 
71011539SChunli.Zhang@Sun.COM 	/* Will this write extend the file length? */
71111935SMark.Shellenbaum@Sun.COM 	write_eof = (woff + n > zp->z_size);
71211935SMark.Shellenbaum@Sun.COM 
71311935SMark.Shellenbaum@Sun.COM 	end_size = MAX(zp->z_size, woff + n);
714789Sahrens 
7151669Sperrin 	/*
7163638Sbillm 	 * Write the file in reasonable size chunks.  Each chunk is written
7173638Sbillm 	 * in a separate transaction; this keeps the intent log records small
7183638Sbillm 	 * and allows us to do more fine-grained space accounting.
719789Sahrens 	 */
720789Sahrens 	while (n > 0) {
7219412SAleksandr.Guzovskiy@Sun.COM 		abuf = NULL;
7229412SAleksandr.Guzovskiy@Sun.COM 		woff = uio->uio_loffset;
7239412SAleksandr.Guzovskiy@Sun.COM again:
72411935SMark.Shellenbaum@Sun.COM 		if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
72511935SMark.Shellenbaum@Sun.COM 		    zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
7269412SAleksandr.Guzovskiy@Sun.COM 			if (abuf != NULL)
7279412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7289396SMatthew.Ahrens@Sun.COM 			error = EDQUOT;
7299396SMatthew.Ahrens@Sun.COM 			break;
7309396SMatthew.Ahrens@Sun.COM 		}
7319412SAleksandr.Guzovskiy@Sun.COM 
73211539SChunli.Zhang@Sun.COM 		if (xuio && abuf == NULL) {
73311539SChunli.Zhang@Sun.COM 			ASSERT(i_iov < iovcnt);
73411539SChunli.Zhang@Sun.COM 			aiov = &iovp[i_iov];
73511539SChunli.Zhang@Sun.COM 			abuf = dmu_xuio_arcbuf(xuio, i_iov);
73611539SChunli.Zhang@Sun.COM 			dmu_xuio_clear(xuio, i_iov);
73711539SChunli.Zhang@Sun.COM 			DTRACE_PROBE3(zfs_cp_write, int, i_iov,
73811539SChunli.Zhang@Sun.COM 			    iovec_t *, aiov, arc_buf_t *, abuf);
73911539SChunli.Zhang@Sun.COM 			ASSERT((aiov->iov_base == abuf->b_data) ||
74011539SChunli.Zhang@Sun.COM 			    ((char *)aiov->iov_base - (char *)abuf->b_data +
74111539SChunli.Zhang@Sun.COM 			    aiov->iov_len == arc_buf_size(abuf)));
74211539SChunli.Zhang@Sun.COM 			i_iov++;
74311539SChunli.Zhang@Sun.COM 		} else if (abuf == NULL && n >= max_blksz &&
74411935SMark.Shellenbaum@Sun.COM 		    woff >= zp->z_size &&
7459412SAleksandr.Guzovskiy@Sun.COM 		    P2PHASE(woff, max_blksz) == 0 &&
7469412SAleksandr.Guzovskiy@Sun.COM 		    zp->z_blksz == max_blksz) {
74711539SChunli.Zhang@Sun.COM 			/*
74811539SChunli.Zhang@Sun.COM 			 * This write covers a full block.  "Borrow" a buffer
74911539SChunli.Zhang@Sun.COM 			 * from the dmu so that we can fill it before we enter
75011539SChunli.Zhang@Sun.COM 			 * a transaction.  This avoids the possibility of
75111539SChunli.Zhang@Sun.COM 			 * holding up the transaction if the data copy hangs
75211539SChunli.Zhang@Sun.COM 			 * up on a pagefault (e.g., from an NFS server mapping).
75311539SChunli.Zhang@Sun.COM 			 */
7549412SAleksandr.Guzovskiy@Sun.COM 			size_t cbytes;
7559412SAleksandr.Guzovskiy@Sun.COM 
75611935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
75711935SMark.Shellenbaum@Sun.COM 			    max_blksz);
7589412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(abuf != NULL);
7599412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(arc_buf_size(abuf) == max_blksz);
7609412SAleksandr.Guzovskiy@Sun.COM 			if (error = uiocopy(abuf->b_data, max_blksz,
7619412SAleksandr.Guzovskiy@Sun.COM 			    UIO_WRITE, uio, &cbytes)) {
7629412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7639412SAleksandr.Guzovskiy@Sun.COM 				break;
7649412SAleksandr.Guzovskiy@Sun.COM 			}
7659412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(cbytes == max_blksz);
7669412SAleksandr.Guzovskiy@Sun.COM 		}
7679412SAleksandr.Guzovskiy@Sun.COM 
7689412SAleksandr.Guzovskiy@Sun.COM 		/*
7699412SAleksandr.Guzovskiy@Sun.COM 		 * Start a transaction.
7709412SAleksandr.Guzovskiy@Sun.COM 		 */
771789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
77211935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
773789Sahrens 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
77411935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, zp);
7758227SNeil.Perrin@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
776789Sahrens 		if (error) {
7778227SNeil.Perrin@Sun.COM 			if (error == ERESTART) {
7782113Sahrens 				dmu_tx_wait(tx);
7792113Sahrens 				dmu_tx_abort(tx);
7809412SAleksandr.Guzovskiy@Sun.COM 				goto again;
781789Sahrens 			}
7822113Sahrens 			dmu_tx_abort(tx);
7839412SAleksandr.Guzovskiy@Sun.COM 			if (abuf != NULL)
7849412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7853638Sbillm 			break;
7863638Sbillm 		}
7873638Sbillm 
7883638Sbillm 		/*
7893638Sbillm 		 * If zfs_range_lock() over-locked we grow the blocksize
7903638Sbillm 		 * and then reduce the lock range.  This will only happen
7913638Sbillm 		 * on the first iteration since zfs_range_reduce() will
7923638Sbillm 		 * shrink down r_len to the appropriate size.
7933638Sbillm 		 */
7943638Sbillm 		if (rl->r_len == UINT64_MAX) {
7953638Sbillm 			uint64_t new_blksz;
7963638Sbillm 
7973638Sbillm 			if (zp->z_blksz > max_blksz) {
7983638Sbillm 				ASSERT(!ISP2(zp->z_blksz));
7993638Sbillm 				new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
8003638Sbillm 			} else {
8013638Sbillm 				new_blksz = MIN(end_size, max_blksz);
8023638Sbillm 			}
8033638Sbillm 			zfs_grow_blocksize(zp, new_blksz, tx);
8043638Sbillm 			zfs_range_reduce(rl, woff, n);
8053638Sbillm 		}
8063638Sbillm 
8073638Sbillm 		/*
8083638Sbillm 		 * XXX - should we really limit each write to z_max_blksz?
8093638Sbillm 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
8103638Sbillm 		 */
8113638Sbillm 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
8123638Sbillm 
8139412SAleksandr.Guzovskiy@Sun.COM 		if (abuf == NULL) {
8149412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes = uio->uio_resid;
81512123STim.Haley@Sun.COM 			error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
81612123STim.Haley@Sun.COM 			    uio, nbytes, tx);
8179412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes -= uio->uio_resid;
8189412SAleksandr.Guzovskiy@Sun.COM 		} else {
8199412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes = nbytes;
82011539SChunli.Zhang@Sun.COM 			ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
82111539SChunli.Zhang@Sun.COM 			/*
82211539SChunli.Zhang@Sun.COM 			 * If this is not a full block write, but we are
82311539SChunli.Zhang@Sun.COM 			 * extending the file past EOF and this data starts
82411539SChunli.Zhang@Sun.COM 			 * block-aligned, use assign_arcbuf().  Otherwise,
82511539SChunli.Zhang@Sun.COM 			 * write via dmu_write().
82611539SChunli.Zhang@Sun.COM 			 */
82711539SChunli.Zhang@Sun.COM 			if (tx_bytes < max_blksz && (!write_eof ||
82811539SChunli.Zhang@Sun.COM 			    aiov->iov_base != abuf->b_data)) {
82911539SChunli.Zhang@Sun.COM 				ASSERT(xuio);
83011539SChunli.Zhang@Sun.COM 				dmu_write(zfsvfs->z_os, zp->z_id, woff,
83111539SChunli.Zhang@Sun.COM 				    aiov->iov_len, aiov->iov_base, tx);
83211539SChunli.Zhang@Sun.COM 				dmu_return_arcbuf(abuf);
83311539SChunli.Zhang@Sun.COM 				xuio_stat_wbuf_copied();
83411539SChunli.Zhang@Sun.COM 			} else {
83511539SChunli.Zhang@Sun.COM 				ASSERT(xuio || tx_bytes == max_blksz);
83611935SMark.Shellenbaum@Sun.COM 				dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
83711935SMark.Shellenbaum@Sun.COM 				    woff, abuf, tx);
83811539SChunli.Zhang@Sun.COM 			}
8399412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(tx_bytes <= uio->uio_resid);
8409412SAleksandr.Guzovskiy@Sun.COM 			uioskip(uio, tx_bytes);
8419412SAleksandr.Guzovskiy@Sun.COM 		}
8429412SAleksandr.Guzovskiy@Sun.COM 		if (tx_bytes && vn_has_cached_data(vp)) {
8438636SMark.Maybee@Sun.COM 			update_pages(vp, woff,
8448636SMark.Maybee@Sun.COM 			    tx_bytes, zfsvfs->z_os, zp->z_id);
8459412SAleksandr.Guzovskiy@Sun.COM 		}
8463638Sbillm 
8473638Sbillm 		/*
8483638Sbillm 		 * If we made no progress, we're done.  If we made even
8493638Sbillm 		 * partial progress, update the znode and ZIL accordingly.
8503638Sbillm 		 */
8513638Sbillm 		if (tx_bytes == 0) {
85211935SMark.Shellenbaum@Sun.COM 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
85311935SMark.Shellenbaum@Sun.COM 			    (void *)&zp->z_size, sizeof (uint64_t), tx);
8543897Smaybee 			dmu_tx_commit(tx);
8553638Sbillm 			ASSERT(error != 0);
8563638Sbillm 			break;
8573638Sbillm 		}
8583638Sbillm 
859789Sahrens 		/*
8603638Sbillm 		 * Clear Set-UID/Set-GID bits on successful write if not
8613638Sbillm 		 * privileged and at least one of the excute bits is set.
8623638Sbillm 		 *
8633638Sbillm 		 * It would be nice to to this after all writes have
8643638Sbillm 		 * been done, but that would still expose the ISUID/ISGID
8653638Sbillm 		 * to another app after the partial write is committed.
8665331Samw 		 *
867789Sahrens 		 */
8683638Sbillm 		mutex_enter(&zp->z_acl_lock);
86911935SMark.Shellenbaum@Sun.COM 		if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
8703638Sbillm 		    (S_IXUSR >> 6))) != 0 &&
87111935SMark.Shellenbaum@Sun.COM 		    (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
8723638Sbillm 		    secpolicy_vnode_setid_retain(cr,
87311935SMark.Shellenbaum@Sun.COM 		    (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
87411935SMark.Shellenbaum@Sun.COM 			uint64_t newmode;
87511935SMark.Shellenbaum@Sun.COM 			zp->z_mode &= ~(S_ISUID | S_ISGID);
87611935SMark.Shellenbaum@Sun.COM 			newmode = zp->z_mode;
87711935SMark.Shellenbaum@Sun.COM 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
87811935SMark.Shellenbaum@Sun.COM 			    (void *)&newmode, sizeof (uint64_t), tx);
8793638Sbillm 		}
8803638Sbillm 		mutex_exit(&zp->z_acl_lock);
8813638Sbillm 
88211935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
88311935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
8843638Sbillm 
8853638Sbillm 		/*
8863638Sbillm 		 * Update the file size (zp_size) if it has changed;
8873638Sbillm 		 * account for possible concurrent updates.
8883638Sbillm 		 */
88911935SMark.Shellenbaum@Sun.COM 		while ((end_size = zp->z_size) < uio->uio_loffset) {
89011935SMark.Shellenbaum@Sun.COM 			(void) atomic_cas_64(&zp->z_size, end_size,
891789Sahrens 			    uio->uio_loffset);
89211935SMark.Shellenbaum@Sun.COM 			ASSERT(error == 0);
89311935SMark.Shellenbaum@Sun.COM 		}
89411935SMark.Shellenbaum@Sun.COM 		error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
89511935SMark.Shellenbaum@Sun.COM 
8963638Sbillm 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
8973638Sbillm 		dmu_tx_commit(tx);
8983638Sbillm 
8993638Sbillm 		if (error != 0)
9003638Sbillm 			break;
9013638Sbillm 		ASSERT(tx_bytes == nbytes);
9023638Sbillm 		n -= nbytes;
903789Sahrens 	}
904789Sahrens 
9052237Smaybee 	zfs_range_unlock(rl);
906789Sahrens 
907789Sahrens 	/*
908789Sahrens 	 * If we're in replay mode, or we made no progress, return error.
909789Sahrens 	 * Otherwise, it's at least a partial write, so it's successful.
910789Sahrens 	 */
9118227SNeil.Perrin@Sun.COM 	if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
912789Sahrens 		ZFS_EXIT(zfsvfs);
913789Sahrens 		return (error);
914789Sahrens 	}
915789Sahrens 
9162638Sperrin 	if (ioflag & (FSYNC | FDSYNC))
9172638Sperrin 		zil_commit(zilog, zp->z_last_itx, zp->z_id);
918789Sahrens 
919789Sahrens 	ZFS_EXIT(zfsvfs);
920789Sahrens 	return (0);
921789Sahrens }
922789Sahrens 
9232237Smaybee void
92410922SJeff.Bonwick@Sun.COM zfs_get_done(zgd_t *zgd, int error)
9252237Smaybee {
92610922SJeff.Bonwick@Sun.COM 	znode_t *zp = zgd->zgd_private;
92710922SJeff.Bonwick@Sun.COM 	objset_t *os = zp->z_zfsvfs->z_os;
92810922SJeff.Bonwick@Sun.COM 
92910922SJeff.Bonwick@Sun.COM 	if (zgd->zgd_db)
93010922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(zgd->zgd_db, zgd);
93110922SJeff.Bonwick@Sun.COM 
93210922SJeff.Bonwick@Sun.COM 	zfs_range_unlock(zgd->zgd_rl);
93310922SJeff.Bonwick@Sun.COM 
9349321SNeil.Perrin@Sun.COM 	/*
9359321SNeil.Perrin@Sun.COM 	 * Release the vnode asynchronously as we currently have the
9369321SNeil.Perrin@Sun.COM 	 * txg stopped from syncing.
9379321SNeil.Perrin@Sun.COM 	 */
93810922SJeff.Bonwick@Sun.COM 	VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
93910922SJeff.Bonwick@Sun.COM 
94010922SJeff.Bonwick@Sun.COM 	if (error == 0 && zgd->zgd_bp)
94110922SJeff.Bonwick@Sun.COM 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
94210922SJeff.Bonwick@Sun.COM 
9433063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
9442237Smaybee }
9452237Smaybee 
94610209SMark.Musante@Sun.COM #ifdef DEBUG
94710209SMark.Musante@Sun.COM static int zil_fault_io = 0;
94810209SMark.Musante@Sun.COM #endif
94910209SMark.Musante@Sun.COM 
950789Sahrens /*
951789Sahrens  * Get data to generate a TX_WRITE intent log record.
952789Sahrens  */
953789Sahrens int
9542237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
955789Sahrens {
956789Sahrens 	zfsvfs_t *zfsvfs = arg;
957789Sahrens 	objset_t *os = zfsvfs->z_os;
958789Sahrens 	znode_t *zp;
95910922SJeff.Bonwick@Sun.COM 	uint64_t object = lr->lr_foid;
96010922SJeff.Bonwick@Sun.COM 	uint64_t offset = lr->lr_offset;
96110922SJeff.Bonwick@Sun.COM 	uint64_t size = lr->lr_length;
96210922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = &lr->lr_blkptr;
9632237Smaybee 	dmu_buf_t *db;
9643063Sperrin 	zgd_t *zgd;
965789Sahrens 	int error = 0;
966789Sahrens 
96710922SJeff.Bonwick@Sun.COM 	ASSERT(zio != NULL);
96810922SJeff.Bonwick@Sun.COM 	ASSERT(size != 0);
969789Sahrens 
970789Sahrens 	/*
9711669Sperrin 	 * Nothing to do if the file has been removed
972789Sahrens 	 */
97310922SJeff.Bonwick@Sun.COM 	if (zfs_zget(zfsvfs, object, &zp) != 0)
974789Sahrens 		return (ENOENT);
9753461Sahrens 	if (zp->z_unlinked) {
9769321SNeil.Perrin@Sun.COM 		/*
9779321SNeil.Perrin@Sun.COM 		 * Release the vnode asynchronously as we currently have the
9789321SNeil.Perrin@Sun.COM 		 * txg stopped from syncing.
9799321SNeil.Perrin@Sun.COM 		 */
9809321SNeil.Perrin@Sun.COM 		VN_RELE_ASYNC(ZTOV(zp),
9819321SNeil.Perrin@Sun.COM 		    dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
982789Sahrens 		return (ENOENT);
983789Sahrens 	}
984789Sahrens 
98510922SJeff.Bonwick@Sun.COM 	zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
98610922SJeff.Bonwick@Sun.COM 	zgd->zgd_zilog = zfsvfs->z_log;
98710922SJeff.Bonwick@Sun.COM 	zgd->zgd_private = zp;
98810922SJeff.Bonwick@Sun.COM 
989789Sahrens 	/*
990789Sahrens 	 * Write records come in two flavors: immediate and indirect.
991789Sahrens 	 * For small writes it's cheaper to store the data with the
992789Sahrens 	 * log record (immediate); for large writes it's cheaper to
993789Sahrens 	 * sync the data and get a pointer to it (indirect) so that
994789Sahrens 	 * we don't have to write the data twice.
995789Sahrens 	 */
9961669Sperrin 	if (buf != NULL) { /* immediate write */
99710922SJeff.Bonwick@Sun.COM 		zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
9981669Sperrin 		/* test for truncation needs to be done while range locked */
99911935SMark.Shellenbaum@Sun.COM 		if (offset >= zp->z_size) {
10001669Sperrin 			error = ENOENT;
100110922SJeff.Bonwick@Sun.COM 		} else {
100210922SJeff.Bonwick@Sun.COM 			error = dmu_read(os, object, offset, size, buf,
100310922SJeff.Bonwick@Sun.COM 			    DMU_READ_NO_PREFETCH);
10041669Sperrin 		}
100510922SJeff.Bonwick@Sun.COM 		ASSERT(error == 0 || error == ENOENT);
10061669Sperrin 	} else { /* indirect write */
1007789Sahrens 		/*
10081669Sperrin 		 * Have to lock the whole block to ensure when it's
10091669Sperrin 		 * written out and it's checksum is being calculated
10101669Sperrin 		 * that no one can change the data. We need to re-check
10111669Sperrin 		 * blocksize after we get the lock in case it's changed!
1012789Sahrens 		 */
10131669Sperrin 		for (;;) {
101410922SJeff.Bonwick@Sun.COM 			uint64_t blkoff;
101510922SJeff.Bonwick@Sun.COM 			size = zp->z_blksz;
101610945SJeff.Bonwick@Sun.COM 			blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
101710922SJeff.Bonwick@Sun.COM 			offset -= blkoff;
101810922SJeff.Bonwick@Sun.COM 			zgd->zgd_rl = zfs_range_lock(zp, offset, size,
101910922SJeff.Bonwick@Sun.COM 			    RL_READER);
102010922SJeff.Bonwick@Sun.COM 			if (zp->z_blksz == size)
10211669Sperrin 				break;
102210922SJeff.Bonwick@Sun.COM 			offset += blkoff;
102310922SJeff.Bonwick@Sun.COM 			zfs_range_unlock(zgd->zgd_rl);
10241669Sperrin 		}
10251669Sperrin 		/* test for truncation needs to be done while range locked */
102611935SMark.Shellenbaum@Sun.COM 		if (lr->lr_offset >= zp->z_size)
10271669Sperrin 			error = ENOENT;
102810209SMark.Musante@Sun.COM #ifdef DEBUG
102910209SMark.Musante@Sun.COM 		if (zil_fault_io) {
103010209SMark.Musante@Sun.COM 			error = EIO;
103110209SMark.Musante@Sun.COM 			zil_fault_io = 0;
103210209SMark.Musante@Sun.COM 		}
103310209SMark.Musante@Sun.COM #endif
103410922SJeff.Bonwick@Sun.COM 		if (error == 0)
103510922SJeff.Bonwick@Sun.COM 			error = dmu_buf_hold(os, object, offset, zgd, &db);
103610922SJeff.Bonwick@Sun.COM 
103710800SNeil.Perrin@Sun.COM 		if (error == 0) {
103810922SJeff.Bonwick@Sun.COM 			zgd->zgd_db = db;
103910922SJeff.Bonwick@Sun.COM 			zgd->zgd_bp = bp;
104010922SJeff.Bonwick@Sun.COM 
104110922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_offset == offset);
104210922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_size == size);
104310922SJeff.Bonwick@Sun.COM 
104410922SJeff.Bonwick@Sun.COM 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
104510922SJeff.Bonwick@Sun.COM 			    zfs_get_done, zgd);
104610922SJeff.Bonwick@Sun.COM 			ASSERT(error || lr->lr_length <= zp->z_blksz);
104710922SJeff.Bonwick@Sun.COM 
104810800SNeil.Perrin@Sun.COM 			/*
104910922SJeff.Bonwick@Sun.COM 			 * On success, we need to wait for the write I/O
105010922SJeff.Bonwick@Sun.COM 			 * initiated by dmu_sync() to complete before we can
105110922SJeff.Bonwick@Sun.COM 			 * release this dbuf.  We will finish everything up
105210922SJeff.Bonwick@Sun.COM 			 * in the zfs_get_done() callback.
105310800SNeil.Perrin@Sun.COM 			 */
105410922SJeff.Bonwick@Sun.COM 			if (error == 0)
105510922SJeff.Bonwick@Sun.COM 				return (0);
105610922SJeff.Bonwick@Sun.COM 
105710922SJeff.Bonwick@Sun.COM 			if (error == EALREADY) {
105810922SJeff.Bonwick@Sun.COM 				lr->lr_common.lrc_txtype = TX_WRITE2;
105910922SJeff.Bonwick@Sun.COM 				error = 0;
106010922SJeff.Bonwick@Sun.COM 			}
106110800SNeil.Perrin@Sun.COM 		}
1062789Sahrens 	}
106310922SJeff.Bonwick@Sun.COM 
106410922SJeff.Bonwick@Sun.COM 	zfs_get_done(zgd, error);
106510922SJeff.Bonwick@Sun.COM 
1066789Sahrens 	return (error);
1067789Sahrens }
1068789Sahrens 
1069789Sahrens /*ARGSUSED*/
1070789Sahrens static int
10715331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
10725331Samw     caller_context_t *ct)
1073789Sahrens {
1074789Sahrens 	znode_t *zp = VTOZ(vp);
1075789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1076789Sahrens 	int error;
1077789Sahrens 
10785367Sahrens 	ZFS_ENTER(zfsvfs);
10795367Sahrens 	ZFS_VERIFY_ZP(zp);
10805331Samw 
10815331Samw 	if (flag & V_ACE_MASK)
10825331Samw 		error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
10835331Samw 	else
10845331Samw 		error = zfs_zaccess_rwx(zp, mode, flag, cr);
10855331Samw 
1086789Sahrens 	ZFS_EXIT(zfsvfs);
1087789Sahrens 	return (error);
1088789Sahrens }
1089789Sahrens 
1090789Sahrens /*
10919981STim.Haley@Sun.COM  * If vnode is for a device return a specfs vnode instead.
10929981STim.Haley@Sun.COM  */
10939981STim.Haley@Sun.COM static int
10949981STim.Haley@Sun.COM specvp_check(vnode_t **vpp, cred_t *cr)
10959981STim.Haley@Sun.COM {
10969981STim.Haley@Sun.COM 	int error = 0;
10979981STim.Haley@Sun.COM 
10989981STim.Haley@Sun.COM 	if (IS_DEVVP(*vpp)) {
10999981STim.Haley@Sun.COM 		struct vnode *svp;
11009981STim.Haley@Sun.COM 
11019981STim.Haley@Sun.COM 		svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
11029981STim.Haley@Sun.COM 		VN_RELE(*vpp);
11039981STim.Haley@Sun.COM 		if (svp == NULL)
11049981STim.Haley@Sun.COM 			error = ENOSYS;
11059981STim.Haley@Sun.COM 		*vpp = svp;
11069981STim.Haley@Sun.COM 	}
11079981STim.Haley@Sun.COM 	return (error);
11089981STim.Haley@Sun.COM }
11099981STim.Haley@Sun.COM 
11109981STim.Haley@Sun.COM 
11119981STim.Haley@Sun.COM /*
1112789Sahrens  * Lookup an entry in a directory, or an extended attribute directory.
1113789Sahrens  * If it exists, return a held vnode reference for it.
1114789Sahrens  *
1115789Sahrens  *	IN:	dvp	- vnode of directory to search.
1116789Sahrens  *		nm	- name of entry to lookup.
1117789Sahrens  *		pnp	- full pathname to lookup [UNUSED].
1118789Sahrens  *		flags	- LOOKUP_XATTR set if looking for an attribute.
1119789Sahrens  *		rdir	- root directory vnode [UNUSED].
1120789Sahrens  *		cr	- credentials of caller.
11215331Samw  *		ct	- caller context
11225331Samw  *		direntflags - directory lookup flags
11235331Samw  *		realpnp - returned pathname.
1124789Sahrens  *
1125789Sahrens  *	OUT:	vpp	- vnode of located entry, NULL if not found.
1126789Sahrens  *
1127789Sahrens  *	RETURN:	0 if success
1128789Sahrens  *		error code if failure
1129789Sahrens  *
1130789Sahrens  * Timestamps:
1131789Sahrens  *	NA
1132789Sahrens  */
1133789Sahrens /* ARGSUSED */
1134789Sahrens static int
1135789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
11365331Samw     int flags, vnode_t *rdir, cred_t *cr,  caller_context_t *ct,
11375331Samw     int *direntflags, pathname_t *realpnp)
1138789Sahrens {
1139789Sahrens 	znode_t *zdp = VTOZ(dvp);
1140789Sahrens 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
11419981STim.Haley@Sun.COM 	int	error = 0;
11429981STim.Haley@Sun.COM 
11439981STim.Haley@Sun.COM 	/* fast path */
11449981STim.Haley@Sun.COM 	if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
11459981STim.Haley@Sun.COM 
11469981STim.Haley@Sun.COM 		if (dvp->v_type != VDIR) {
11479981STim.Haley@Sun.COM 			return (ENOTDIR);
114811935SMark.Shellenbaum@Sun.COM 		} else if (zdp->z_sa_hdl == NULL) {
11499981STim.Haley@Sun.COM 			return (EIO);
11509981STim.Haley@Sun.COM 		}
11519981STim.Haley@Sun.COM 
11529981STim.Haley@Sun.COM 		if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
11539981STim.Haley@Sun.COM 			error = zfs_fastaccesschk_execute(zdp, cr);
11549981STim.Haley@Sun.COM 			if (!error) {
11559981STim.Haley@Sun.COM 				*vpp = dvp;
11569981STim.Haley@Sun.COM 				VN_HOLD(*vpp);
11579981STim.Haley@Sun.COM 				return (0);
11589981STim.Haley@Sun.COM 			}
11599981STim.Haley@Sun.COM 			return (error);
11609981STim.Haley@Sun.COM 		} else {
11619981STim.Haley@Sun.COM 			vnode_t *tvp = dnlc_lookup(dvp, nm);
11629981STim.Haley@Sun.COM 
11639981STim.Haley@Sun.COM 			if (tvp) {
11649981STim.Haley@Sun.COM 				error = zfs_fastaccesschk_execute(zdp, cr);
11659981STim.Haley@Sun.COM 				if (error) {
11669981STim.Haley@Sun.COM 					VN_RELE(tvp);
11679981STim.Haley@Sun.COM 					return (error);
11689981STim.Haley@Sun.COM 				}
11699981STim.Haley@Sun.COM 				if (tvp == DNLC_NO_VNODE) {
11709981STim.Haley@Sun.COM 					VN_RELE(tvp);
11719981STim.Haley@Sun.COM 					return (ENOENT);
11729981STim.Haley@Sun.COM 				} else {
11739981STim.Haley@Sun.COM 					*vpp = tvp;
11749981STim.Haley@Sun.COM 					return (specvp_check(vpp, cr));
11759981STim.Haley@Sun.COM 				}
11769981STim.Haley@Sun.COM 			}
11779981STim.Haley@Sun.COM 		}
11789981STim.Haley@Sun.COM 	}
11799981STim.Haley@Sun.COM 
11809981STim.Haley@Sun.COM 	DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1181789Sahrens 
11825367Sahrens 	ZFS_ENTER(zfsvfs);
11835367Sahrens 	ZFS_VERIFY_ZP(zdp);
1184789Sahrens 
1185789Sahrens 	*vpp = NULL;
1186789Sahrens 
1187789Sahrens 	if (flags & LOOKUP_XATTR) {
1188789Sahrens 		/*
11893234Sck153898 		 * If the xattr property is off, refuse the lookup request.
11903234Sck153898 		 */
11913234Sck153898 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
11923234Sck153898 			ZFS_EXIT(zfsvfs);
11933234Sck153898 			return (EINVAL);
11943234Sck153898 		}
11953234Sck153898 
11963234Sck153898 		/*
1197789Sahrens 		 * We don't allow recursive attributes..
1198789Sahrens 		 * Maybe someday we will.
1199789Sahrens 		 */
120011935SMark.Shellenbaum@Sun.COM 		if (zdp->z_pflags & ZFS_XATTR) {
1201789Sahrens 			ZFS_EXIT(zfsvfs);
1202789Sahrens 			return (EINVAL);
1203789Sahrens 		}
1204789Sahrens 
12053280Sck153898 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1206789Sahrens 			ZFS_EXIT(zfsvfs);
1207789Sahrens 			return (error);
1208789Sahrens 		}
1209789Sahrens 
1210789Sahrens 		/*
1211789Sahrens 		 * Do we have permission to get into attribute directory?
1212789Sahrens 		 */
1213789Sahrens 
12145331Samw 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
12155331Samw 		    B_FALSE, cr)) {
1216789Sahrens 			VN_RELE(*vpp);
12175331Samw 			*vpp = NULL;
1218789Sahrens 		}
1219789Sahrens 
1220789Sahrens 		ZFS_EXIT(zfsvfs);
1221789Sahrens 		return (error);
1222789Sahrens 	}
1223789Sahrens 
12241512Sek110237 	if (dvp->v_type != VDIR) {
12251512Sek110237 		ZFS_EXIT(zfsvfs);
12261460Smarks 		return (ENOTDIR);
12271512Sek110237 	}
12281460Smarks 
1229789Sahrens 	/*
1230789Sahrens 	 * Check accessibility of directory.
1231789Sahrens 	 */
1232789Sahrens 
12335331Samw 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1234789Sahrens 		ZFS_EXIT(zfsvfs);
1235789Sahrens 		return (error);
1236789Sahrens 	}
1237789Sahrens 
12385498Stimh 	if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
12395331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
12405331Samw 		ZFS_EXIT(zfsvfs);
12415331Samw 		return (EILSEQ);
12425331Samw 	}
12435331Samw 
12445331Samw 	error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
12459981STim.Haley@Sun.COM 	if (error == 0)
12469981STim.Haley@Sun.COM 		error = specvp_check(vpp, cr);
1247789Sahrens 
1248789Sahrens 	ZFS_EXIT(zfsvfs);
1249789Sahrens 	return (error);
1250789Sahrens }
1251789Sahrens 
1252789Sahrens /*
1253789Sahrens  * Attempt to create a new entry in a directory.  If the entry
1254789Sahrens  * already exists, truncate the file if permissible, else return
1255789Sahrens  * an error.  Return the vp of the created or trunc'd file.
1256789Sahrens  *
1257789Sahrens  *	IN:	dvp	- vnode of directory to put new file entry in.
1258789Sahrens  *		name	- name of new file entry.
1259789Sahrens  *		vap	- attributes of new file.
1260789Sahrens  *		excl	- flag indicating exclusive or non-exclusive mode.
1261789Sahrens  *		mode	- mode to open file with.
1262789Sahrens  *		cr	- credentials of caller.
1263789Sahrens  *		flag	- large file flag [UNUSED].
12645331Samw  *		ct	- caller context
12655331Samw  *		vsecp 	- ACL to be set
1266789Sahrens  *
1267789Sahrens  *	OUT:	vpp	- vnode of created or trunc'd entry.
1268789Sahrens  *
1269789Sahrens  *	RETURN:	0 if success
1270789Sahrens  *		error code if failure
1271789Sahrens  *
1272789Sahrens  * Timestamps:
1273789Sahrens  *	dvp - ctime|mtime updated if new entry created
1274789Sahrens  *	 vp - ctime|mtime always, atime if new
1275789Sahrens  */
12765331Samw 
1277789Sahrens /* ARGSUSED */
1278789Sahrens static int
1279789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
12805331Samw     int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
12815331Samw     vsecattr_t *vsecp)
1282789Sahrens {
1283789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1284789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
12855326Sek110237 	zilog_t		*zilog;
12865326Sek110237 	objset_t	*os;
1287789Sahrens 	zfs_dirlock_t	*dl;
1288789Sahrens 	dmu_tx_t	*tx;
1289789Sahrens 	int		error;
12907847SMark.Shellenbaum@Sun.COM 	ksid_t		*ksid;
12917847SMark.Shellenbaum@Sun.COM 	uid_t		uid;
12927847SMark.Shellenbaum@Sun.COM 	gid_t		gid = crgetgid(cr);
129311935SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t   acl_ids;
12949179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
12955331Samw 
12965331Samw 	/*
12975331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
12985331Samw 	 * make sure file system is at proper version
12995331Samw 	 */
13005331Samw 
13017847SMark.Shellenbaum@Sun.COM 	ksid = crgetsid(cr, KSID_OWNER);
13027847SMark.Shellenbaum@Sun.COM 	if (ksid)
13037847SMark.Shellenbaum@Sun.COM 		uid = ksid_getid(ksid);
13047847SMark.Shellenbaum@Sun.COM 	else
13057847SMark.Shellenbaum@Sun.COM 		uid = crgetuid(cr);
13067847SMark.Shellenbaum@Sun.COM 
13075331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
13085331Samw 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
13097847SMark.Shellenbaum@Sun.COM 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
13105331Samw 		return (EINVAL);
1311789Sahrens 
13125367Sahrens 	ZFS_ENTER(zfsvfs);
13135367Sahrens 	ZFS_VERIFY_ZP(dzp);
13145326Sek110237 	os = zfsvfs->z_os;
13155326Sek110237 	zilog = zfsvfs->z_log;
1316789Sahrens 
13175498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
13185331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
13195331Samw 		ZFS_EXIT(zfsvfs);
13205331Samw 		return (EILSEQ);
13215331Samw 	}
13225331Samw 
13235331Samw 	if (vap->va_mask & AT_XVATTR) {
13245331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
13255331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
13265331Samw 			ZFS_EXIT(zfsvfs);
13275331Samw 			return (error);
13285331Samw 		}
13295331Samw 	}
1330789Sahrens top:
1331789Sahrens 	*vpp = NULL;
1332789Sahrens 
1333789Sahrens 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1334789Sahrens 		vap->va_mode &= ~VSVTX;
1335789Sahrens 
1336789Sahrens 	if (*name == '\0') {
1337789Sahrens 		/*
1338789Sahrens 		 * Null component name refers to the directory itself.
1339789Sahrens 		 */
1340789Sahrens 		VN_HOLD(dvp);
1341789Sahrens 		zp = dzp;
1342789Sahrens 		dl = NULL;
1343789Sahrens 		error = 0;
1344789Sahrens 	} else {
1345789Sahrens 		/* possible VN_HOLD(zp) */
13465331Samw 		int zflg = 0;
13475331Samw 
13485331Samw 		if (flag & FIGNORECASE)
13495331Samw 			zflg |= ZCILOOK;
13505331Samw 
13515331Samw 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
13525331Samw 		    NULL, NULL);
13535331Samw 		if (error) {
1354789Sahrens 			if (strcmp(name, "..") == 0)
1355789Sahrens 				error = EISDIR;
1356789Sahrens 			ZFS_EXIT(zfsvfs);
1357789Sahrens 			return (error);
1358789Sahrens 		}
1359789Sahrens 	}
136011935SMark.Shellenbaum@Sun.COM 
1361789Sahrens 	if (zp == NULL) {
13625331Samw 		uint64_t txtype;
13635331Samw 
1364789Sahrens 		/*
1365789Sahrens 		 * Create a new file object and update the directory
1366789Sahrens 		 * to reference it.
1367789Sahrens 		 */
13685331Samw 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1369789Sahrens 			goto out;
1370789Sahrens 		}
1371789Sahrens 
1372789Sahrens 		/*
1373789Sahrens 		 * We only support the creation of regular files in
1374789Sahrens 		 * extended attribute directories.
1375789Sahrens 		 */
137611935SMark.Shellenbaum@Sun.COM 
137711935SMark.Shellenbaum@Sun.COM 		if ((dzp->z_pflags & ZFS_XATTR) &&
1378789Sahrens 		    (vap->va_type != VREG)) {
1379789Sahrens 			error = EINVAL;
1380789Sahrens 			goto out;
1381789Sahrens 		}
1382789Sahrens 
13839179SMark.Shellenbaum@Sun.COM 		if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
13849179SMark.Shellenbaum@Sun.COM 		    &acl_ids)) != 0)
13859179SMark.Shellenbaum@Sun.COM 			goto out;
13869396SMatthew.Ahrens@Sun.COM 		if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
138710143STim.Haley@Sun.COM 			zfs_acl_ids_free(&acl_ids);
13889396SMatthew.Ahrens@Sun.COM 			error = EDQUOT;
13899396SMatthew.Ahrens@Sun.COM 			goto out;
13909396SMatthew.Ahrens@Sun.COM 		}
13919179SMark.Shellenbaum@Sun.COM 
1392789Sahrens 		tx = dmu_tx_create(os);
139311935SMark.Shellenbaum@Sun.COM 
139411935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
139511935SMark.Shellenbaum@Sun.COM 		    ZFS_SA_BASE_ATTR_SIZE);
139611935SMark.Shellenbaum@Sun.COM 
13979179SMark.Shellenbaum@Sun.COM 		fuid_dirtied = zfsvfs->z_fuid_dirty;
13989396SMatthew.Ahrens@Sun.COM 		if (fuid_dirtied)
13999396SMatthew.Ahrens@Sun.COM 			zfs_fuid_txhold(zfsvfs, tx);
14001544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
140111935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
140211935SMark.Shellenbaum@Sun.COM 		if (!zfsvfs->z_use_sa &&
140311935SMark.Shellenbaum@Sun.COM 		    acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1404789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
140511935SMark.Shellenbaum@Sun.COM 			    0, acl_ids.z_aclp->z_acl_bytes);
14065331Samw 		}
14078227SNeil.Perrin@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
1408789Sahrens 		if (error) {
14099179SMark.Shellenbaum@Sun.COM 			zfs_acl_ids_free(&acl_ids);
1410789Sahrens 			zfs_dirent_unlock(dl);
14118227SNeil.Perrin@Sun.COM 			if (error == ERESTART) {
14122113Sahrens 				dmu_tx_wait(tx);
14132113Sahrens 				dmu_tx_abort(tx);
1414789Sahrens 				goto top;
1415789Sahrens 			}
14162113Sahrens 			dmu_tx_abort(tx);
1417789Sahrens 			ZFS_EXIT(zfsvfs);
1418789Sahrens 			return (error);
1419789Sahrens 		}
142011935SMark.Shellenbaum@Sun.COM 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
14219179SMark.Shellenbaum@Sun.COM 
14229179SMark.Shellenbaum@Sun.COM 		if (fuid_dirtied)
14239179SMark.Shellenbaum@Sun.COM 			zfs_fuid_sync(zfsvfs, tx);
14249179SMark.Shellenbaum@Sun.COM 
1425789Sahrens 		(void) zfs_link_create(dl, zp, tx, ZNEW);
14265331Samw 		txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
14275331Samw 		if (flag & FIGNORECASE)
14285331Samw 			txtype |= TX_CI;
14295331Samw 		zfs_log_create(zilog, tx, txtype, dzp, zp, name,
14309179SMark.Shellenbaum@Sun.COM 		    vsecp, acl_ids.z_fuidp, vap);
14319179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
1432789Sahrens 		dmu_tx_commit(tx);
1433789Sahrens 	} else {
14345331Samw 		int aflags = (flag & FAPPEND) ? V_APPEND : 0;
14355331Samw 
1436789Sahrens 		/*
1437789Sahrens 		 * A directory entry already exists for this name.
1438789Sahrens 		 */
1439789Sahrens 		/*
1440789Sahrens 		 * Can't truncate an existing file if in exclusive mode.
1441789Sahrens 		 */
1442789Sahrens 		if (excl == EXCL) {
1443789Sahrens 			error = EEXIST;
1444789Sahrens 			goto out;
1445789Sahrens 		}
1446789Sahrens 		/*
1447789Sahrens 		 * Can't open a directory for writing.
1448789Sahrens 		 */
1449789Sahrens 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1450789Sahrens 			error = EISDIR;
1451789Sahrens 			goto out;
1452789Sahrens 		}
1453789Sahrens 		/*
1454789Sahrens 		 * Verify requested access to file.
1455789Sahrens 		 */
14565331Samw 		if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1457789Sahrens 			goto out;
1458789Sahrens 		}
1459789Sahrens 
1460789Sahrens 		mutex_enter(&dzp->z_lock);
1461789Sahrens 		dzp->z_seq++;
1462789Sahrens 		mutex_exit(&dzp->z_lock);
1463789Sahrens 
14641878Smaybee 		/*
14651878Smaybee 		 * Truncate regular files if requested.
14661878Smaybee 		 */
14671878Smaybee 		if ((ZTOV(zp)->v_type == VREG) &&
1468789Sahrens 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
14696992Smaybee 			/* we can't hold any locks when calling zfs_freesp() */
14706992Smaybee 			zfs_dirent_unlock(dl);
14716992Smaybee 			dl = NULL;
14721878Smaybee 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
14734863Spraks 			if (error == 0) {
14745331Samw 				vnevent_create(ZTOV(zp), ct);
14754863Spraks 			}
1476789Sahrens 		}
1477789Sahrens 	}
1478789Sahrens out:
1479789Sahrens 
1480789Sahrens 	if (dl)
1481789Sahrens 		zfs_dirent_unlock(dl);
1482789Sahrens 
1483789Sahrens 	if (error) {
1484789Sahrens 		if (zp)
1485789Sahrens 			VN_RELE(ZTOV(zp));
1486789Sahrens 	} else {
1487789Sahrens 		*vpp = ZTOV(zp);
14889981STim.Haley@Sun.COM 		error = specvp_check(vpp, cr);
1489789Sahrens 	}
1490789Sahrens 
1491789Sahrens 	ZFS_EXIT(zfsvfs);
1492789Sahrens 	return (error);
1493789Sahrens }
1494789Sahrens 
1495789Sahrens /*
1496789Sahrens  * Remove an entry from a directory.
1497789Sahrens  *
1498789Sahrens  *	IN:	dvp	- vnode of directory to remove entry from.
1499789Sahrens  *		name	- name of entry to remove.
1500789Sahrens  *		cr	- credentials of caller.
15015331Samw  *		ct	- caller context
15025331Samw  *		flags	- case flags
1503789Sahrens  *
1504789Sahrens  *	RETURN:	0 if success
1505789Sahrens  *		error code if failure
1506789Sahrens  *
1507789Sahrens  * Timestamps:
1508789Sahrens  *	dvp - ctime|mtime
1509789Sahrens  *	 vp - ctime (if nlink > 0)
1510789Sahrens  */
151111935SMark.Shellenbaum@Sun.COM 
151211935SMark.Shellenbaum@Sun.COM uint64_t null_xattr = 0;
151311935SMark.Shellenbaum@Sun.COM 
15145331Samw /*ARGSUSED*/
1515789Sahrens static int
15165331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
15175331Samw     int flags)
1518789Sahrens {
1519789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1520789Sahrens 	znode_t		*xzp = NULL;
1521789Sahrens 	vnode_t		*vp;
1522789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
15235326Sek110237 	zilog_t		*zilog;
152411935SMark.Shellenbaum@Sun.COM 	uint64_t	acl_obj, xattr_obj = 0;
152511935SMark.Shellenbaum@Sun.COM 	uint64_t 	xattr_obj_unlinked = 0;
1526789Sahrens 	zfs_dirlock_t	*dl;
1527789Sahrens 	dmu_tx_t	*tx;
15283461Sahrens 	boolean_t	may_delete_now, delete_now = FALSE;
15296992Smaybee 	boolean_t	unlinked, toobig = FALSE;
15305331Samw 	uint64_t	txtype;
15315331Samw 	pathname_t	*realnmp = NULL;
15325331Samw 	pathname_t	realnm;
1533789Sahrens 	int		error;
15345331Samw 	int		zflg = ZEXISTS;
1535789Sahrens 
15365367Sahrens 	ZFS_ENTER(zfsvfs);
15375367Sahrens 	ZFS_VERIFY_ZP(dzp);
15385326Sek110237 	zilog = zfsvfs->z_log;
1539789Sahrens 
15405331Samw 	if (flags & FIGNORECASE) {
15415331Samw 		zflg |= ZCILOOK;
15425331Samw 		pn_alloc(&realnm);
15435331Samw 		realnmp = &realnm;
15445331Samw 	}
15455331Samw 
1546789Sahrens top:
1547789Sahrens 	/*
1548789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1549789Sahrens 	 */
15505331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
15515331Samw 	    NULL, realnmp)) {
15525331Samw 		if (realnmp)
15535331Samw 			pn_free(realnmp);
1554789Sahrens 		ZFS_EXIT(zfsvfs);
1555789Sahrens 		return (error);
1556789Sahrens 	}
1557789Sahrens 
1558789Sahrens 	vp = ZTOV(zp);
1559789Sahrens 
1560789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1561789Sahrens 		goto out;
1562789Sahrens 	}
1563789Sahrens 
1564789Sahrens 	/*
1565789Sahrens 	 * Need to use rmdir for removing directories.
1566789Sahrens 	 */
1567789Sahrens 	if (vp->v_type == VDIR) {
1568789Sahrens 		error = EPERM;
1569789Sahrens 		goto out;
1570789Sahrens 	}
1571789Sahrens 
15725331Samw 	vnevent_remove(vp, dvp, name, ct);
15735331Samw 
15745331Samw 	if (realnmp)
15756492Stimh 		dnlc_remove(dvp, realnmp->pn_buf);
15765331Samw 	else
15775331Samw 		dnlc_remove(dvp, name);
15781484Sek110237 
1579789Sahrens 	mutex_enter(&vp->v_lock);
1580789Sahrens 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1581789Sahrens 	mutex_exit(&vp->v_lock);
1582789Sahrens 
1583789Sahrens 	/*
15843461Sahrens 	 * We may delete the znode now, or we may put it in the unlinked set;
1585789Sahrens 	 * it depends on whether we're the last link, and on whether there are
1586789Sahrens 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1587789Sahrens 	 * allow for either case.
1588789Sahrens 	 */
1589789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
15901544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
159111935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
159211935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
159311935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, dzp);
15946992Smaybee 	if (may_delete_now) {
15956992Smaybee 		toobig =
159611935SMark.Shellenbaum@Sun.COM 		    zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
15976992Smaybee 		/* if the file is too big, only hold_free a token amount */
15986992Smaybee 		dmu_tx_hold_free(tx, zp->z_id, 0,
15996992Smaybee 		    (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
16006992Smaybee 	}
1601789Sahrens 
1602789Sahrens 	/* are there any extended attributes? */
160311935SMark.Shellenbaum@Sun.COM 	error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
160411935SMark.Shellenbaum@Sun.COM 	    &xattr_obj, sizeof (xattr_obj));
160511935SMark.Shellenbaum@Sun.COM 	if (xattr_obj) {
160611935SMark.Shellenbaum@Sun.COM 		error = zfs_zget(zfsvfs, xattr_obj, &xzp);
160711935SMark.Shellenbaum@Sun.COM 		ASSERT3U(error, ==, 0);
160811935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
160911935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1610789Sahrens 	}
1611789Sahrens 
1612789Sahrens 	/* are there any additional acls */
161311935SMark.Shellenbaum@Sun.COM 	if ((acl_obj = ZFS_EXTERNAL_ACL(zp)) != 0 && may_delete_now)
1614789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1615789Sahrens 
1616789Sahrens 	/* charge as an update -- would be nice not to charge at all */
16173461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1618789Sahrens 
16198227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1620789Sahrens 	if (error) {
1621789Sahrens 		zfs_dirent_unlock(dl);
1622789Sahrens 		VN_RELE(vp);
16238227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
16242113Sahrens 			dmu_tx_wait(tx);
16252113Sahrens 			dmu_tx_abort(tx);
1626789Sahrens 			goto top;
1627789Sahrens 		}
16285331Samw 		if (realnmp)
16295331Samw 			pn_free(realnmp);
16302113Sahrens 		dmu_tx_abort(tx);
1631789Sahrens 		ZFS_EXIT(zfsvfs);
1632789Sahrens 		return (error);
1633789Sahrens 	}
1634789Sahrens 
1635789Sahrens 	/*
1636789Sahrens 	 * Remove the directory entry.
1637789Sahrens 	 */
16385331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1639789Sahrens 
1640789Sahrens 	if (error) {
1641789Sahrens 		dmu_tx_commit(tx);
1642789Sahrens 		goto out;
1643789Sahrens 	}
1644789Sahrens 
16453461Sahrens 	if (unlinked) {
164611935SMark.Shellenbaum@Sun.COM 
1647789Sahrens 		mutex_enter(&vp->v_lock);
164811935SMark.Shellenbaum@Sun.COM 
164911935SMark.Shellenbaum@Sun.COM 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
165011935SMark.Shellenbaum@Sun.COM 		    &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
16516992Smaybee 		delete_now = may_delete_now && !toobig &&
1652789Sahrens 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
165311935SMark.Shellenbaum@Sun.COM 		    xattr_obj == xattr_obj_unlinked && ZFS_EXTERNAL_ACL(zp) ==
165411935SMark.Shellenbaum@Sun.COM 		    acl_obj;
1655789Sahrens 		mutex_exit(&vp->v_lock);
1656789Sahrens 	}
1657789Sahrens 
1658789Sahrens 	if (delete_now) {
165911935SMark.Shellenbaum@Sun.COM 		if (xattr_obj_unlinked) {
166011935SMark.Shellenbaum@Sun.COM 			ASSERT3U(xzp->z_links, ==, 2);
1661789Sahrens 			mutex_enter(&xzp->z_lock);
16623461Sahrens 			xzp->z_unlinked = 1;
166311935SMark.Shellenbaum@Sun.COM 			xzp->z_links = 0;
166411935SMark.Shellenbaum@Sun.COM 			error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
166511935SMark.Shellenbaum@Sun.COM 			    &xzp->z_links, sizeof (xzp->z_links), tx);
166611935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error,  ==,  0);
1667789Sahrens 			mutex_exit(&xzp->z_lock);
16683461Sahrens 			zfs_unlinked_add(xzp, tx);
166911935SMark.Shellenbaum@Sun.COM 			if (zp->z_is_sa)
167011935SMark.Shellenbaum@Sun.COM 				error = sa_remove(zp->z_sa_hdl,
167111935SMark.Shellenbaum@Sun.COM 				    SA_ZPL_XATTR(zfsvfs), tx);
167211935SMark.Shellenbaum@Sun.COM 			else
167311935SMark.Shellenbaum@Sun.COM 				error = sa_update(zp->z_sa_hdl,
167411935SMark.Shellenbaum@Sun.COM 				    SA_ZPL_XATTR(zfsvfs), &null_xattr,
167511935SMark.Shellenbaum@Sun.COM 				    sizeof (uint64_t), tx);
167611935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error, ==, 0);
1677789Sahrens 		}
1678789Sahrens 		mutex_enter(&zp->z_lock);
1679789Sahrens 		mutex_enter(&vp->v_lock);
1680789Sahrens 		vp->v_count--;
1681789Sahrens 		ASSERT3U(vp->v_count, ==, 0);
1682789Sahrens 		mutex_exit(&vp->v_lock);
1683789Sahrens 		mutex_exit(&zp->z_lock);
1684789Sahrens 		zfs_znode_delete(zp, tx);
16853461Sahrens 	} else if (unlinked) {
16863461Sahrens 		zfs_unlinked_add(zp, tx);
1687789Sahrens 	}
1688789Sahrens 
16895331Samw 	txtype = TX_REMOVE;
16905331Samw 	if (flags & FIGNORECASE)
16915331Samw 		txtype |= TX_CI;
16925331Samw 	zfs_log_remove(zilog, tx, txtype, dzp, name);
1693789Sahrens 
1694789Sahrens 	dmu_tx_commit(tx);
1695789Sahrens out:
16965331Samw 	if (realnmp)
16975331Samw 		pn_free(realnmp);
16985331Samw 
1699789Sahrens 	zfs_dirent_unlock(dl);
1700789Sahrens 
1701789Sahrens 	if (!delete_now) {
1702789Sahrens 		VN_RELE(vp);
1703789Sahrens 	} else if (xzp) {
17046992Smaybee 		/* this rele is delayed to prevent nesting transactions */
1705789Sahrens 		VN_RELE(ZTOV(xzp));
1706789Sahrens 	}
1707789Sahrens 
1708789Sahrens 	ZFS_EXIT(zfsvfs);
1709789Sahrens 	return (error);
1710789Sahrens }
1711789Sahrens 
1712789Sahrens /*
1713789Sahrens  * Create a new directory and insert it into dvp using the name
1714789Sahrens  * provided.  Return a pointer to the inserted directory.
1715789Sahrens  *
1716789Sahrens  *	IN:	dvp	- vnode of directory to add subdir to.
1717789Sahrens  *		dirname	- name of new directory.
1718789Sahrens  *		vap	- attributes of new directory.
1719789Sahrens  *		cr	- credentials of caller.
17205331Samw  *		ct	- caller context
17215331Samw  *		vsecp	- ACL to be set
1722789Sahrens  *
1723789Sahrens  *	OUT:	vpp	- vnode of created directory.
1724789Sahrens  *
1725789Sahrens  *	RETURN:	0 if success
1726789Sahrens  *		error code if failure
1727789Sahrens  *
1728789Sahrens  * Timestamps:
1729789Sahrens  *	dvp - ctime|mtime updated
1730789Sahrens  *	 vp - ctime|mtime|atime updated
1731789Sahrens  */
17325331Samw /*ARGSUSED*/
1733789Sahrens static int
17345331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
17355331Samw     caller_context_t *ct, int flags, vsecattr_t *vsecp)
1736789Sahrens {
1737789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1738789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
17395326Sek110237 	zilog_t		*zilog;
1740789Sahrens 	zfs_dirlock_t	*dl;
17415331Samw 	uint64_t	txtype;
1742789Sahrens 	dmu_tx_t	*tx;
1743789Sahrens 	int		error;
17445331Samw 	int		zf = ZNEW;
17457847SMark.Shellenbaum@Sun.COM 	ksid_t		*ksid;
17467847SMark.Shellenbaum@Sun.COM 	uid_t		uid;
17477847SMark.Shellenbaum@Sun.COM 	gid_t		gid = crgetgid(cr);
174811935SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t   acl_ids;
17499179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
1750789Sahrens 
1751789Sahrens 	ASSERT(vap->va_type == VDIR);
1752789Sahrens 
17535331Samw 	/*
17545331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
17555331Samw 	 * make sure file system is at proper version
17565331Samw 	 */
17575331Samw 
17587847SMark.Shellenbaum@Sun.COM 	ksid = crgetsid(cr, KSID_OWNER);
17597847SMark.Shellenbaum@Sun.COM 	if (ksid)
17607847SMark.Shellenbaum@Sun.COM 		uid = ksid_getid(ksid);
17617847SMark.Shellenbaum@Sun.COM 	else
17627847SMark.Shellenbaum@Sun.COM 		uid = crgetuid(cr);
17635331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
17647847SMark.Shellenbaum@Sun.COM 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
17657876SMark.Shellenbaum@Sun.COM 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
17665331Samw 		return (EINVAL);
17675331Samw 
17685367Sahrens 	ZFS_ENTER(zfsvfs);
17695367Sahrens 	ZFS_VERIFY_ZP(dzp);
17705326Sek110237 	zilog = zfsvfs->z_log;
1771789Sahrens 
177211935SMark.Shellenbaum@Sun.COM 	if (dzp->z_pflags & ZFS_XATTR) {
1773789Sahrens 		ZFS_EXIT(zfsvfs);
1774789Sahrens 		return (EINVAL);
1775789Sahrens 	}
17765331Samw 
17775498Stimh 	if (zfsvfs->z_utf8 && u8_validate(dirname,
17785331Samw 	    strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
17795331Samw 		ZFS_EXIT(zfsvfs);
17805331Samw 		return (EILSEQ);
17815331Samw 	}
17825331Samw 	if (flags & FIGNORECASE)
17835331Samw 		zf |= ZCILOOK;
17845331Samw 
17855331Samw 	if (vap->va_mask & AT_XVATTR)
17865331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
17875331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
17885331Samw 			ZFS_EXIT(zfsvfs);
17895331Samw 			return (error);
17905331Samw 		}
1791789Sahrens 
1792789Sahrens 	/*
1793789Sahrens 	 * First make sure the new directory doesn't exist.
1794789Sahrens 	 */
17955331Samw top:
17965331Samw 	*vpp = NULL;
17975331Samw 
17985331Samw 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
17995331Samw 	    NULL, NULL)) {
1800789Sahrens 		ZFS_EXIT(zfsvfs);
1801789Sahrens 		return (error);
1802789Sahrens 	}
1803789Sahrens 
18045331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
18051231Smarks 		zfs_dirent_unlock(dl);
18061231Smarks 		ZFS_EXIT(zfsvfs);
18071231Smarks 		return (error);
18081231Smarks 	}
18091231Smarks 
18109179SMark.Shellenbaum@Sun.COM 	if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
18119179SMark.Shellenbaum@Sun.COM 	    &acl_ids)) != 0) {
18129179SMark.Shellenbaum@Sun.COM 		zfs_dirent_unlock(dl);
18139179SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
18149179SMark.Shellenbaum@Sun.COM 		return (error);
18155331Samw 	}
18169396SMatthew.Ahrens@Sun.COM 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
181710143STim.Haley@Sun.COM 		zfs_acl_ids_free(&acl_ids);
18189396SMatthew.Ahrens@Sun.COM 		zfs_dirent_unlock(dl);
18199396SMatthew.Ahrens@Sun.COM 		ZFS_EXIT(zfsvfs);
18209396SMatthew.Ahrens@Sun.COM 		return (EDQUOT);
18219396SMatthew.Ahrens@Sun.COM 	}
18229179SMark.Shellenbaum@Sun.COM 
1823789Sahrens 	/*
1824789Sahrens 	 * Add a new entry to the directory.
1825789Sahrens 	 */
1826789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
18271544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
18281544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
18299179SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
18309396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
18319396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
183211935SMark.Shellenbaum@Sun.COM 	if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
183311935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
183411935SMark.Shellenbaum@Sun.COM 		    acl_ids.z_aclp->z_acl_bytes);
183511935SMark.Shellenbaum@Sun.COM 	}
183611935SMark.Shellenbaum@Sun.COM 
183711935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
183811935SMark.Shellenbaum@Sun.COM 	    ZFS_SA_BASE_ATTR_SIZE);
183911935SMark.Shellenbaum@Sun.COM 
18408227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1841789Sahrens 	if (error) {
18429179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
1843789Sahrens 		zfs_dirent_unlock(dl);
18448227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
18452113Sahrens 			dmu_tx_wait(tx);
18462113Sahrens 			dmu_tx_abort(tx);
1847789Sahrens 			goto top;
1848789Sahrens 		}
18492113Sahrens 		dmu_tx_abort(tx);
1850789Sahrens 		ZFS_EXIT(zfsvfs);
1851789Sahrens 		return (error);
1852789Sahrens 	}
1853789Sahrens 
1854789Sahrens 	/*
1855789Sahrens 	 * Create new node.
1856789Sahrens 	 */
185711935SMark.Shellenbaum@Sun.COM 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
18589179SMark.Shellenbaum@Sun.COM 
18599179SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
18609179SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
186111935SMark.Shellenbaum@Sun.COM 
1862789Sahrens 	/*
1863789Sahrens 	 * Now put new name in parent dir.
1864789Sahrens 	 */
1865789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1866789Sahrens 
1867789Sahrens 	*vpp = ZTOV(zp);
1868789Sahrens 
18695331Samw 	txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
18705331Samw 	if (flags & FIGNORECASE)
18715331Samw 		txtype |= TX_CI;
18729179SMark.Shellenbaum@Sun.COM 	zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
18739179SMark.Shellenbaum@Sun.COM 	    acl_ids.z_fuidp, vap);
18749179SMark.Shellenbaum@Sun.COM 
18759179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_free(&acl_ids);
187611935SMark.Shellenbaum@Sun.COM 
1877789Sahrens 	dmu_tx_commit(tx);
1878789Sahrens 
1879789Sahrens 	zfs_dirent_unlock(dl);
1880789Sahrens 
1881789Sahrens 	ZFS_EXIT(zfsvfs);
1882789Sahrens 	return (0);
1883789Sahrens }
1884789Sahrens 
1885789Sahrens /*
1886789Sahrens  * Remove a directory subdir entry.  If the current working
1887789Sahrens  * directory is the same as the subdir to be removed, the
1888789Sahrens  * remove will fail.
1889789Sahrens  *
1890789Sahrens  *	IN:	dvp	- vnode of directory to remove from.
1891789Sahrens  *		name	- name of directory to be removed.
1892789Sahrens  *		cwd	- vnode of current working directory.
1893789Sahrens  *		cr	- credentials of caller.
18945331Samw  *		ct	- caller context
18955331Samw  *		flags	- case flags
1896789Sahrens  *
1897789Sahrens  *	RETURN:	0 if success
1898789Sahrens  *		error code if failure
1899789Sahrens  *
1900789Sahrens  * Timestamps:
1901789Sahrens  *	dvp - ctime|mtime updated
1902789Sahrens  */
19035331Samw /*ARGSUSED*/
1904789Sahrens static int
19055331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
19065331Samw     caller_context_t *ct, int flags)
1907789Sahrens {
1908789Sahrens 	znode_t		*dzp = VTOZ(dvp);
1909789Sahrens 	znode_t		*zp;
1910789Sahrens 	vnode_t		*vp;
1911789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
19125326Sek110237 	zilog_t		*zilog;
1913789Sahrens 	zfs_dirlock_t	*dl;
1914789Sahrens 	dmu_tx_t	*tx;
1915789Sahrens 	int		error;
19165331Samw 	int		zflg = ZEXISTS;
1917789Sahrens 
19185367Sahrens 	ZFS_ENTER(zfsvfs);
19195367Sahrens 	ZFS_VERIFY_ZP(dzp);
19205326Sek110237 	zilog = zfsvfs->z_log;
1921789Sahrens 
19225331Samw 	if (flags & FIGNORECASE)
19235331Samw 		zflg |= ZCILOOK;
1924789Sahrens top:
1925789Sahrens 	zp = NULL;
1926789Sahrens 
1927789Sahrens 	/*
1928789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1929789Sahrens 	 */
19305331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
19315331Samw 	    NULL, NULL)) {
1932789Sahrens 		ZFS_EXIT(zfsvfs);
1933789Sahrens 		return (error);
1934789Sahrens 	}
1935789Sahrens 
1936789Sahrens 	vp = ZTOV(zp);
1937789Sahrens 
1938789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1939789Sahrens 		goto out;
1940789Sahrens 	}
1941789Sahrens 
1942789Sahrens 	if (vp->v_type != VDIR) {
1943789Sahrens 		error = ENOTDIR;
1944789Sahrens 		goto out;
1945789Sahrens 	}
1946789Sahrens 
1947789Sahrens 	if (vp == cwd) {
1948789Sahrens 		error = EINVAL;
1949789Sahrens 		goto out;
1950789Sahrens 	}
1951789Sahrens 
19525331Samw 	vnevent_rmdir(vp, dvp, name, ct);
1953789Sahrens 
1954789Sahrens 	/*
19553897Smaybee 	 * Grab a lock on the directory to make sure that noone is
19563897Smaybee 	 * trying to add (or lookup) entries while we are removing it.
19573897Smaybee 	 */
19583897Smaybee 	rw_enter(&zp->z_name_lock, RW_WRITER);
19593897Smaybee 
19603897Smaybee 	/*
19613897Smaybee 	 * Grab a lock on the parent pointer to make sure we play well
1962789Sahrens 	 * with the treewalk and directory rename code.
1963789Sahrens 	 */
1964789Sahrens 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1965789Sahrens 
1966789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
19671544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
196811935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
19693461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
197011935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
197111935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, dzp);
19728227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1973789Sahrens 	if (error) {
1974789Sahrens 		rw_exit(&zp->z_parent_lock);
19753897Smaybee 		rw_exit(&zp->z_name_lock);
1976789Sahrens 		zfs_dirent_unlock(dl);
1977789Sahrens 		VN_RELE(vp);
19788227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
19792113Sahrens 			dmu_tx_wait(tx);
19802113Sahrens 			dmu_tx_abort(tx);
1981789Sahrens 			goto top;
1982789Sahrens 		}
19832113Sahrens 		dmu_tx_abort(tx);
1984789Sahrens 		ZFS_EXIT(zfsvfs);
1985789Sahrens 		return (error);
1986789Sahrens 	}
1987789Sahrens 
19885331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
19895331Samw 
19905331Samw 	if (error == 0) {
19915331Samw 		uint64_t txtype = TX_RMDIR;
19925331Samw 		if (flags & FIGNORECASE)
19935331Samw 			txtype |= TX_CI;
19945331Samw 		zfs_log_remove(zilog, tx, txtype, dzp, name);
19955331Samw 	}
1996789Sahrens 
1997789Sahrens 	dmu_tx_commit(tx);
1998789Sahrens 
1999789Sahrens 	rw_exit(&zp->z_parent_lock);
20003897Smaybee 	rw_exit(&zp->z_name_lock);
2001789Sahrens out:
2002789Sahrens 	zfs_dirent_unlock(dl);
2003789Sahrens 
2004789Sahrens 	VN_RELE(vp);
2005789Sahrens 
2006789Sahrens 	ZFS_EXIT(zfsvfs);
2007789Sahrens 	return (error);
2008789Sahrens }
2009789Sahrens 
2010789Sahrens /*
2011789Sahrens  * Read as many directory entries as will fit into the provided
2012789Sahrens  * buffer from the given directory cursor position (specified in
2013789Sahrens  * the uio structure.
2014789Sahrens  *
2015789Sahrens  *	IN:	vp	- vnode of directory to read.
2016789Sahrens  *		uio	- structure supplying read location, range info,
2017789Sahrens  *			  and return buffer.
2018789Sahrens  *		cr	- credentials of caller.
20195331Samw  *		ct	- caller context
20205331Samw  *		flags	- case flags
2021789Sahrens  *
2022789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
2023789Sahrens  *		eofp	- set to true if end-of-file detected.
2024789Sahrens  *
2025789Sahrens  *	RETURN:	0 if success
2026789Sahrens  *		error code if failure
2027789Sahrens  *
2028789Sahrens  * Timestamps:
2029789Sahrens  *	vp - atime updated
2030789Sahrens  *
2031789Sahrens  * Note that the low 4 bits of the cookie returned by zap is always zero.
2032789Sahrens  * This allows us to use the low range for "special" directory entries:
2033789Sahrens  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
2034789Sahrens  * we use the offset 2 for the '.zfs' directory.
2035789Sahrens  */
2036789Sahrens /* ARGSUSED */
2037789Sahrens static int
20385331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
20395331Samw     caller_context_t *ct, int flags)
2040789Sahrens {
2041789Sahrens 	znode_t		*zp = VTOZ(vp);
2042789Sahrens 	iovec_t		*iovp;
20435331Samw 	edirent_t	*eodp;
2044789Sahrens 	dirent64_t	*odp;
2045789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2046869Sperrin 	objset_t	*os;
2047789Sahrens 	caddr_t		outbuf;
2048789Sahrens 	size_t		bufsize;
2049789Sahrens 	zap_cursor_t	zc;
2050789Sahrens 	zap_attribute_t	zap;
2051789Sahrens 	uint_t		bytes_wanted;
2052789Sahrens 	uint64_t	offset; /* must be unsigned; checks for < 1 */
205311935SMark.Shellenbaum@Sun.COM 	uint64_t	parent;
2054789Sahrens 	int		local_eof;
2055869Sperrin 	int		outcount;
2056869Sperrin 	int		error;
2057869Sperrin 	uint8_t		prefetch;
20585663Sck153898 	boolean_t	check_sysattrs;
2059789Sahrens 
20605367Sahrens 	ZFS_ENTER(zfsvfs);
20615367Sahrens 	ZFS_VERIFY_ZP(zp);
2062789Sahrens 
206311935SMark.Shellenbaum@Sun.COM 	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
206411935SMark.Shellenbaum@Sun.COM 	    &parent, sizeof (parent))) != 0) {
206511935SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
206611935SMark.Shellenbaum@Sun.COM 		return (error);
206711935SMark.Shellenbaum@Sun.COM 	}
206811935SMark.Shellenbaum@Sun.COM 
2069789Sahrens 	/*
2070789Sahrens 	 * If we are not given an eof variable,
2071789Sahrens 	 * use a local one.
2072789Sahrens 	 */
2073789Sahrens 	if (eofp == NULL)
2074789Sahrens 		eofp = &local_eof;
2075789Sahrens 
2076789Sahrens 	/*
2077789Sahrens 	 * Check for valid iov_len.
2078789Sahrens 	 */
2079789Sahrens 	if (uio->uio_iov->iov_len <= 0) {
2080789Sahrens 		ZFS_EXIT(zfsvfs);
2081789Sahrens 		return (EINVAL);
2082789Sahrens 	}
2083789Sahrens 
2084789Sahrens 	/*
2085789Sahrens 	 * Quit if directory has been removed (posix)
2086789Sahrens 	 */
20873461Sahrens 	if ((*eofp = zp->z_unlinked) != 0) {
2088789Sahrens 		ZFS_EXIT(zfsvfs);
2089789Sahrens 		return (0);
2090789Sahrens 	}
2091789Sahrens 
2092869Sperrin 	error = 0;
2093869Sperrin 	os = zfsvfs->z_os;
2094869Sperrin 	offset = uio->uio_loffset;
2095869Sperrin 	prefetch = zp->z_zn_prefetch;
2096869Sperrin 
2097789Sahrens 	/*
2098789Sahrens 	 * Initialize the iterator cursor.
2099789Sahrens 	 */
2100789Sahrens 	if (offset <= 3) {
2101789Sahrens 		/*
2102789Sahrens 		 * Start iteration from the beginning of the directory.
2103789Sahrens 		 */
2104869Sperrin 		zap_cursor_init(&zc, os, zp->z_id);
2105789Sahrens 	} else {
2106789Sahrens 		/*
2107789Sahrens 		 * The offset is a serialized cursor.
2108789Sahrens 		 */
2109869Sperrin 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2110789Sahrens 	}
2111789Sahrens 
2112789Sahrens 	/*
2113789Sahrens 	 * Get space to change directory entries into fs independent format.
2114789Sahrens 	 */
2115789Sahrens 	iovp = uio->uio_iov;
2116789Sahrens 	bytes_wanted = iovp->iov_len;
2117789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2118789Sahrens 		bufsize = bytes_wanted;
2119789Sahrens 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
2120789Sahrens 		odp = (struct dirent64 *)outbuf;
2121789Sahrens 	} else {
2122789Sahrens 		bufsize = bytes_wanted;
2123789Sahrens 		odp = (struct dirent64 *)iovp->iov_base;
2124789Sahrens 	}
21255331Samw 	eodp = (struct edirent *)odp;
2126789Sahrens 
2127789Sahrens 	/*
21287757SJanice.Chang@Sun.COM 	 * If this VFS supports the system attribute view interface; and
21297757SJanice.Chang@Sun.COM 	 * we're looking at an extended attribute directory; and we care
21307757SJanice.Chang@Sun.COM 	 * about normalization conflicts on this vfs; then we must check
21317757SJanice.Chang@Sun.COM 	 * for normalization conflicts with the sysattr name space.
21325663Sck153898 	 */
21337757SJanice.Chang@Sun.COM 	check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
21345663Sck153898 	    (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
21355663Sck153898 	    (flags & V_RDDIR_ENTFLAGS);
21365663Sck153898 
21375663Sck153898 	/*
2138789Sahrens 	 * Transform to file-system independent format
2139789Sahrens 	 */
2140789Sahrens 	outcount = 0;
2141789Sahrens 	while (outcount < bytes_wanted) {
21423912Slling 		ino64_t objnum;
21433912Slling 		ushort_t reclen;
21443912Slling 		off64_t *next;
21453912Slling 
2146789Sahrens 		/*
2147789Sahrens 		 * Special case `.', `..', and `.zfs'.
2148789Sahrens 		 */
2149789Sahrens 		if (offset == 0) {
2150789Sahrens 			(void) strcpy(zap.za_name, ".");
21515331Samw 			zap.za_normalization_conflict = 0;
21523912Slling 			objnum = zp->z_id;
2153789Sahrens 		} else if (offset == 1) {
2154789Sahrens 			(void) strcpy(zap.za_name, "..");
21555331Samw 			zap.za_normalization_conflict = 0;
215611935SMark.Shellenbaum@Sun.COM 			objnum = parent;
2157789Sahrens 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
2158789Sahrens 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
21595331Samw 			zap.za_normalization_conflict = 0;
21603912Slling 			objnum = ZFSCTL_INO_ROOT;
2161789Sahrens 		} else {
2162789Sahrens 			/*
2163789Sahrens 			 * Grab next entry.
2164789Sahrens 			 */
2165789Sahrens 			if (error = zap_cursor_retrieve(&zc, &zap)) {
2166789Sahrens 				if ((*eofp = (error == ENOENT)) != 0)
2167789Sahrens 					break;
2168789Sahrens 				else
2169789Sahrens 					goto update;
2170789Sahrens 			}
2171789Sahrens 
2172789Sahrens 			if (zap.za_integer_length != 8 ||
2173789Sahrens 			    zap.za_num_integers != 1) {
2174789Sahrens 				cmn_err(CE_WARN, "zap_readdir: bad directory "
2175789Sahrens 				    "entry, obj = %lld, offset = %lld\n",
2176789Sahrens 				    (u_longlong_t)zp->z_id,
2177789Sahrens 				    (u_longlong_t)offset);
2178789Sahrens 				error = ENXIO;
2179789Sahrens 				goto update;
2180789Sahrens 			}
21813912Slling 
21823912Slling 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
21833912Slling 			/*
21843912Slling 			 * MacOS X can extract the object type here such as:
21853912Slling 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
21863912Slling 			 */
21875663Sck153898 
21885663Sck153898 			if (check_sysattrs && !zap.za_normalization_conflict) {
21895663Sck153898 				zap.za_normalization_conflict =
21905663Sck153898 				    xattr_sysattr_casechk(zap.za_name);
21915663Sck153898 			}
2192789Sahrens 		}
21935331Samw 
21949749STim.Haley@Sun.COM 		if (flags & V_RDDIR_ACCFILTER) {
21959749STim.Haley@Sun.COM 			/*
21969749STim.Haley@Sun.COM 			 * If we have no access at all, don't include
21979749STim.Haley@Sun.COM 			 * this entry in the returned information
21989749STim.Haley@Sun.COM 			 */
21999749STim.Haley@Sun.COM 			znode_t	*ezp;
22009749STim.Haley@Sun.COM 			if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
22019749STim.Haley@Sun.COM 				goto skip_entry;
22029749STim.Haley@Sun.COM 			if (!zfs_has_access(ezp, cr)) {
22039749STim.Haley@Sun.COM 				VN_RELE(ZTOV(ezp));
22049749STim.Haley@Sun.COM 				goto skip_entry;
22059749STim.Haley@Sun.COM 			}
22069749STim.Haley@Sun.COM 			VN_RELE(ZTOV(ezp));
22079749STim.Haley@Sun.COM 		}
22089749STim.Haley@Sun.COM 
22095331Samw 		if (flags & V_RDDIR_ENTFLAGS)
22105331Samw 			reclen = EDIRENT_RECLEN(strlen(zap.za_name));
22115331Samw 		else
22125331Samw 			reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2213789Sahrens 
2214789Sahrens 		/*
2215789Sahrens 		 * Will this entry fit in the buffer?
2216789Sahrens 		 */
22173912Slling 		if (outcount + reclen > bufsize) {
2218789Sahrens 			/*
2219789Sahrens 			 * Did we manage to fit anything in the buffer?
2220789Sahrens 			 */
2221789Sahrens 			if (!outcount) {
2222789Sahrens 				error = EINVAL;
2223789Sahrens 				goto update;
2224789Sahrens 			}
2225789Sahrens 			break;
2226789Sahrens 		}
22275331Samw 		if (flags & V_RDDIR_ENTFLAGS) {
22285331Samw 			/*
22295331Samw 			 * Add extended flag entry:
22305331Samw 			 */
22315331Samw 			eodp->ed_ino = objnum;
22325331Samw 			eodp->ed_reclen = reclen;
22335331Samw 			/* NOTE: ed_off is the offset for the *next* entry */
22345331Samw 			next = &(eodp->ed_off);
22355331Samw 			eodp->ed_eflags = zap.za_normalization_conflict ?
22365331Samw 			    ED_CASE_CONFLICT : 0;
22375331Samw 			(void) strncpy(eodp->ed_name, zap.za_name,
22385331Samw 			    EDIRENT_NAMELEN(reclen));
22395331Samw 			eodp = (edirent_t *)((intptr_t)eodp + reclen);
22405331Samw 		} else {
22415331Samw 			/*
22425331Samw 			 * Add normal entry:
22435331Samw 			 */
22445331Samw 			odp->d_ino = objnum;
22455331Samw 			odp->d_reclen = reclen;
22465331Samw 			/* NOTE: d_off is the offset for the *next* entry */
22475331Samw 			next = &(odp->d_off);
22485331Samw 			(void) strncpy(odp->d_name, zap.za_name,
22495331Samw 			    DIRENT64_NAMELEN(reclen));
22505331Samw 			odp = (dirent64_t *)((intptr_t)odp + reclen);
22515331Samw 		}
22523912Slling 		outcount += reclen;
2253789Sahrens 
2254789Sahrens 		ASSERT(outcount <= bufsize);
2255789Sahrens 
2256789Sahrens 		/* Prefetch znode */
2257869Sperrin 		if (prefetch)
22583912Slling 			dmu_prefetch(os, objnum, 0, 0);
2259789Sahrens 
22609749STim.Haley@Sun.COM 	skip_entry:
2261789Sahrens 		/*
2262789Sahrens 		 * Move to the next entry, fill in the previous offset.
2263789Sahrens 		 */
2264789Sahrens 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2265789Sahrens 			zap_cursor_advance(&zc);
2266789Sahrens 			offset = zap_cursor_serialize(&zc);
2267789Sahrens 		} else {
2268789Sahrens 			offset += 1;
2269789Sahrens 		}
2270789Sahrens 		*next = offset;
2271789Sahrens 	}
2272869Sperrin 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2273789Sahrens 
2274789Sahrens 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2275789Sahrens 		iovp->iov_base += outcount;
2276789Sahrens 		iovp->iov_len -= outcount;
2277789Sahrens 		uio->uio_resid -= outcount;
2278789Sahrens 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2279789Sahrens 		/*
2280789Sahrens 		 * Reset the pointer.
2281789Sahrens 		 */
2282789Sahrens 		offset = uio->uio_loffset;
2283789Sahrens 	}
2284789Sahrens 
2285789Sahrens update:
2286885Sahrens 	zap_cursor_fini(&zc);
2287789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2288789Sahrens 		kmem_free(outbuf, bufsize);
2289789Sahrens 
2290789Sahrens 	if (error == ENOENT)
2291789Sahrens 		error = 0;
2292789Sahrens 
2293789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2294789Sahrens 
2295789Sahrens 	uio->uio_loffset = offset;
2296789Sahrens 	ZFS_EXIT(zfsvfs);
2297789Sahrens 	return (error);
2298789Sahrens }
2299789Sahrens 
23004720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4;
23014720Sfr157268 
2302789Sahrens static int
23035331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2304789Sahrens {
2305789Sahrens 	znode_t	*zp = VTOZ(vp);
2306789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2307789Sahrens 
23081773Seschrock 	/*
23091773Seschrock 	 * Regardless of whether this is required for standards conformance,
23101773Seschrock 	 * this is the logical behavior when fsync() is called on a file with
23111773Seschrock 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
23121773Seschrock 	 * going to be pushed out as part of the zil_commit().
23131773Seschrock 	 */
23141773Seschrock 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
23151773Seschrock 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
23165331Samw 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
23171773Seschrock 
23184720Sfr157268 	(void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
23194720Sfr157268 
23205367Sahrens 	ZFS_ENTER(zfsvfs);
23215367Sahrens 	ZFS_VERIFY_ZP(zp);
23222638Sperrin 	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
2323789Sahrens 	ZFS_EXIT(zfsvfs);
2324789Sahrens 	return (0);
2325789Sahrens }
2326789Sahrens 
23275331Samw 
2328789Sahrens /*
2329789Sahrens  * Get the requested file attributes and place them in the provided
2330789Sahrens  * vattr structure.
2331789Sahrens  *
2332789Sahrens  *	IN:	vp	- vnode of file.
2333789Sahrens  *		vap	- va_mask identifies requested attributes.
23345331Samw  *			  If AT_XVATTR set, then optional attrs are requested
23355331Samw  *		flags	- ATTR_NOACLCHECK (CIFS server context)
2336789Sahrens  *		cr	- credentials of caller.
23375331Samw  *		ct	- caller context
2338789Sahrens  *
2339789Sahrens  *	OUT:	vap	- attribute values.
2340789Sahrens  *
2341789Sahrens  *	RETURN:	0 (always succeeds)
2342789Sahrens  */
2343789Sahrens /* ARGSUSED */
2344789Sahrens static int
23455331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
23465331Samw     caller_context_t *ct)
2347789Sahrens {
2348789Sahrens 	znode_t *zp = VTOZ(vp);
2349789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
23505331Samw 	int	error = 0;
23514543Smarks 	uint64_t links;
235211935SMark.Shellenbaum@Sun.COM 	uint64_t mtime[2], ctime[2];
23535331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
23545331Samw 	xoptattr_t *xoap = NULL;
23555331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
235611935SMark.Shellenbaum@Sun.COM 	sa_bulk_attr_t bulk[2];
235711935SMark.Shellenbaum@Sun.COM 	int count = 0;
2358789Sahrens 
23595367Sahrens 	ZFS_ENTER(zfsvfs);
23605367Sahrens 	ZFS_VERIFY_ZP(zp);
236111935SMark.Shellenbaum@Sun.COM 
236211935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
236311935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
236411935SMark.Shellenbaum@Sun.COM 
236511935SMark.Shellenbaum@Sun.COM 	if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
236611935SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
236711935SMark.Shellenbaum@Sun.COM 		return (error);
236811935SMark.Shellenbaum@Sun.COM 	}
2369789Sahrens 
23705331Samw 	/*
23715331Samw 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
23725331Samw 	 * Also, if we are the owner don't bother, since owner should
23735331Samw 	 * always be allowed to read basic attributes of file.
23745331Samw 	 */
237511935SMark.Shellenbaum@Sun.COM 	if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) && (zp->z_uid != crgetuid(cr))) {
23765331Samw 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
23775331Samw 		    skipaclchk, cr)) {
23785331Samw 			ZFS_EXIT(zfsvfs);
23795331Samw 			return (error);
23805331Samw 		}
23815331Samw 	}
23825331Samw 
2383789Sahrens 	/*
2384789Sahrens 	 * Return all attributes.  It's cheaper to provide the answer
2385789Sahrens 	 * than to determine whether we were asked the question.
2386789Sahrens 	 */
2387789Sahrens 
23889774SRay.Hassan@Sun.COM 	mutex_enter(&zp->z_lock);
2389789Sahrens 	vap->va_type = vp->v_type;
239011935SMark.Shellenbaum@Sun.COM 	vap->va_mode = zp->z_mode & MODEMASK;
239111935SMark.Shellenbaum@Sun.COM 	vap->va_uid = zp->z_uid;
239211935SMark.Shellenbaum@Sun.COM 	vap->va_gid = zp->z_gid;
2393789Sahrens 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2394789Sahrens 	vap->va_nodeid = zp->z_id;
23954543Smarks 	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
239611935SMark.Shellenbaum@Sun.COM 		links = zp->z_links + 1;
23974543Smarks 	else
239811935SMark.Shellenbaum@Sun.COM 		links = zp->z_links;
23994543Smarks 	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
240011935SMark.Shellenbaum@Sun.COM 	vap->va_size = zp->z_size;
24011816Smarks 	vap->va_rdev = vp->v_rdev;
2402789Sahrens 	vap->va_seq = zp->z_seq;
2403789Sahrens 
24045331Samw 	/*
24055331Samw 	 * Add in any requested optional attributes and the create time.
24065331Samw 	 * Also set the corresponding bits in the returned attribute bitmap.
24075331Samw 	 */
24085331Samw 	if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
24095331Samw 		if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
24105331Samw 			xoap->xoa_archive =
241111935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_ARCHIVE) != 0);
24125331Samw 			XVA_SET_RTN(xvap, XAT_ARCHIVE);
24135331Samw 		}
24145331Samw 
24155331Samw 		if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
24165331Samw 			xoap->xoa_readonly =
241711935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_READONLY) != 0);
24185331Samw 			XVA_SET_RTN(xvap, XAT_READONLY);
24195331Samw 		}
24205331Samw 
24215331Samw 		if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
24225331Samw 			xoap->xoa_system =
242311935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_SYSTEM) != 0);
24245331Samw 			XVA_SET_RTN(xvap, XAT_SYSTEM);
24255331Samw 		}
24265331Samw 
24275331Samw 		if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
24285331Samw 			xoap->xoa_hidden =
242911935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_HIDDEN) != 0);
24305331Samw 			XVA_SET_RTN(xvap, XAT_HIDDEN);
24315331Samw 		}
24325331Samw 
24335331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
24345331Samw 			xoap->xoa_nounlink =
243511935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NOUNLINK) != 0);
24365331Samw 			XVA_SET_RTN(xvap, XAT_NOUNLINK);
24375331Samw 		}
24385331Samw 
24395331Samw 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
24405331Samw 			xoap->xoa_immutable =
244111935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
24425331Samw 			XVA_SET_RTN(xvap, XAT_IMMUTABLE);
24435331Samw 		}
24445331Samw 
24455331Samw 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
24465331Samw 			xoap->xoa_appendonly =
244711935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_APPENDONLY) != 0);
24485331Samw 			XVA_SET_RTN(xvap, XAT_APPENDONLY);
24495331Samw 		}
24505331Samw 
24515331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
24525331Samw 			xoap->xoa_nodump =
245311935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NODUMP) != 0);
24545331Samw 			XVA_SET_RTN(xvap, XAT_NODUMP);
24555331Samw 		}
24565331Samw 
24575331Samw 		if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
24585331Samw 			xoap->xoa_opaque =
245911935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_OPAQUE) != 0);
24605331Samw 			XVA_SET_RTN(xvap, XAT_OPAQUE);
24615331Samw 		}
24625331Samw 
24635331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
24645331Samw 			xoap->xoa_av_quarantined =
246511935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
24665331Samw 			XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
24675331Samw 		}
24685331Samw 
24695331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
24705331Samw 			xoap->xoa_av_modified =
247111935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
24725331Samw 			XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
24735331Samw 		}
24745331Samw 
24755331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
247611935SMark.Shellenbaum@Sun.COM 		    vp->v_type == VREG) {
247711935SMark.Shellenbaum@Sun.COM 			zfs_sa_get_scanstamp(zp, xvap);
24785331Samw 		}
24795331Samw 
24805331Samw 		if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
248111935SMark.Shellenbaum@Sun.COM 			uint64_t times[2];
248211935SMark.Shellenbaum@Sun.COM 
248311935SMark.Shellenbaum@Sun.COM 			(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
248411935SMark.Shellenbaum@Sun.COM 			    times, sizeof (times));
248511935SMark.Shellenbaum@Sun.COM 			ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
24865331Samw 			XVA_SET_RTN(xvap, XAT_CREATETIME);
24875331Samw 		}
248810793Sdai.ngo@sun.com 
248910793Sdai.ngo@sun.com 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
249011935SMark.Shellenbaum@Sun.COM 			xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
249110793Sdai.ngo@sun.com 			XVA_SET_RTN(xvap, XAT_REPARSE);
249210793Sdai.ngo@sun.com 		}
24935331Samw 	}
24945331Samw 
249511935SMark.Shellenbaum@Sun.COM 	ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
249611935SMark.Shellenbaum@Sun.COM 	ZFS_TIME_DECODE(&vap->va_mtime, mtime);
249711935SMark.Shellenbaum@Sun.COM 	ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2498789Sahrens 
2499789Sahrens 	mutex_exit(&zp->z_lock);
2500789Sahrens 
250111935SMark.Shellenbaum@Sun.COM 	sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2502789Sahrens 
2503789Sahrens 	if (zp->z_blksz == 0) {
2504789Sahrens 		/*
2505789Sahrens 		 * Block size hasn't been set; suggest maximal I/O transfers.
2506789Sahrens 		 */
2507789Sahrens 		vap->va_blksize = zfsvfs->z_max_blksz;
2508789Sahrens 	}
2509789Sahrens 
2510789Sahrens 	ZFS_EXIT(zfsvfs);
2511789Sahrens 	return (0);
2512789Sahrens }
2513789Sahrens 
2514789Sahrens /*
2515789Sahrens  * Set the file attributes to the values contained in the
2516789Sahrens  * vattr structure.
2517789Sahrens  *
2518789Sahrens  *	IN:	vp	- vnode of file to be modified.
2519789Sahrens  *		vap	- new attribute values.
25205331Samw  *			  If AT_XVATTR set, then optional attrs are being set
2521789Sahrens  *		flags	- ATTR_UTIME set if non-default time values provided.
25225331Samw  *			- ATTR_NOACLCHECK (CIFS context only).
2523789Sahrens  *		cr	- credentials of caller.
25245331Samw  *		ct	- caller context
2525789Sahrens  *
2526789Sahrens  *	RETURN:	0 if success
2527789Sahrens  *		error code if failure
2528789Sahrens  *
2529789Sahrens  * Timestamps:
2530789Sahrens  *	vp - ctime updated, mtime updated if size changed.
2531789Sahrens  */
2532789Sahrens /* ARGSUSED */
2533789Sahrens static int
2534789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2535789Sahrens 	caller_context_t *ct)
2536789Sahrens {
25375326Sek110237 	znode_t		*zp = VTOZ(vp);
2538789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
25395326Sek110237 	zilog_t		*zilog;
2540789Sahrens 	dmu_tx_t	*tx;
25411878Smaybee 	vattr_t		oldva;
25428190SMark.Shellenbaum@Sun.COM 	xvattr_t	tmpxvattr;
2543789Sahrens 	uint_t		mask = vap->va_mask;
25441878Smaybee 	uint_t		saved_mask;
25452796Smarks 	int		trim_mask = 0;
2546789Sahrens 	uint64_t	new_mode;
25479179SMark.Shellenbaum@Sun.COM 	uint64_t	new_uid, new_gid;
254811935SMark.Shellenbaum@Sun.COM 	uint64_t	xattr_obj = 0;
254911935SMark.Shellenbaum@Sun.COM 	uint64_t	mtime[2], ctime[2];
25501231Smarks 	znode_t		*attrzp;
2551789Sahrens 	int		need_policy = FALSE;
255211935SMark.Shellenbaum@Sun.COM 	int		err, err2;
25535331Samw 	zfs_fuid_info_t *fuidp = NULL;
25545331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
25555331Samw 	xoptattr_t	*xoap;
25565824Smarks 	zfs_acl_t	*aclp = NULL;
25575331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
255811935SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied = B_FALSE;
255911935SMark.Shellenbaum@Sun.COM 	sa_bulk_attr_t	bulk[7], xattr_bulk[7];
256011935SMark.Shellenbaum@Sun.COM 	int		count = 0, xattr_count = 0;
2561789Sahrens 
2562789Sahrens 	if (mask == 0)
2563789Sahrens 		return (0);
2564789Sahrens 
2565789Sahrens 	if (mask & AT_NOSET)
2566789Sahrens 		return (EINVAL);
2567789Sahrens 
25685367Sahrens 	ZFS_ENTER(zfsvfs);
25695367Sahrens 	ZFS_VERIFY_ZP(zp);
25705331Samw 
25715331Samw 	zilog = zfsvfs->z_log;
25725331Samw 
25735331Samw 	/*
25745331Samw 	 * Make sure that if we have ephemeral uid/gid or xvattr specified
25755331Samw 	 * that file system is at proper version level
25765331Samw 	 */
25775331Samw 
25785331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
25795331Samw 	    (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
25805331Samw 	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
25815386Stimh 	    (mask & AT_XVATTR))) {
25825386Stimh 		ZFS_EXIT(zfsvfs);
25835331Samw 		return (EINVAL);
25845386Stimh 	}
25855386Stimh 
25865386Stimh 	if (mask & AT_SIZE && vp->v_type == VDIR) {
25875386Stimh 		ZFS_EXIT(zfsvfs);
2588789Sahrens 		return (EISDIR);
25895386Stimh 	}
25905386Stimh 
25915386Stimh 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
25925386Stimh 		ZFS_EXIT(zfsvfs);
25931308Smarks 		return (EINVAL);
25945386Stimh 	}
25951308Smarks 
25965331Samw 	/*
25975331Samw 	 * If this is an xvattr_t, then get a pointer to the structure of
25985331Samw 	 * optional attributes.  If this is NULL, then we have a vattr_t.
25995331Samw 	 */
26005331Samw 	xoap = xva_getxoptattr(xvap);
26015331Samw 
26028190SMark.Shellenbaum@Sun.COM 	xva_init(&tmpxvattr);
26038190SMark.Shellenbaum@Sun.COM 
26045331Samw 	/*
26055331Samw 	 * Immutable files can only alter immutable bit and atime
26065331Samw 	 */
260711935SMark.Shellenbaum@Sun.COM 	if ((zp->z_pflags & ZFS_IMMUTABLE) &&
26085331Samw 	    ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
26095386Stimh 	    ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
26105386Stimh 		ZFS_EXIT(zfsvfs);
26115331Samw 		return (EPERM);
26125386Stimh 	}
26135386Stimh 
261411935SMark.Shellenbaum@Sun.COM 	if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
26155386Stimh 		ZFS_EXIT(zfsvfs);
26165331Samw 		return (EPERM);
26175386Stimh 	}
2618789Sahrens 
26196064Smarks 	/*
26206064Smarks 	 * Verify timestamps doesn't overflow 32 bits.
26216064Smarks 	 * ZFS can handle large timestamps, but 32bit syscalls can't
26226064Smarks 	 * handle times greater than 2039.  This check should be removed
26236064Smarks 	 * once large timestamps are fully supported.
26246064Smarks 	 */
26256064Smarks 	if (mask & (AT_ATIME | AT_MTIME)) {
26266064Smarks 		if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
26276064Smarks 		    ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
26286064Smarks 			ZFS_EXIT(zfsvfs);
26296064Smarks 			return (EOVERFLOW);
26306064Smarks 		}
26316064Smarks 	}
26326064Smarks 
2633789Sahrens top:
26341231Smarks 	attrzp = NULL;
2635789Sahrens 
26369981STim.Haley@Sun.COM 	/* Can this be moved to before the top label? */
2637789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2638789Sahrens 		ZFS_EXIT(zfsvfs);
2639789Sahrens 		return (EROFS);
2640789Sahrens 	}
2641789Sahrens 
2642789Sahrens 	/*
2643789Sahrens 	 * First validate permissions
2644789Sahrens 	 */
2645789Sahrens 
2646789Sahrens 	if (mask & AT_SIZE) {
26475331Samw 		err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2648789Sahrens 		if (err) {
2649789Sahrens 			ZFS_EXIT(zfsvfs);
2650789Sahrens 			return (err);
2651789Sahrens 		}
26521878Smaybee 		/*
26531878Smaybee 		 * XXX - Note, we are not providing any open
26541878Smaybee 		 * mode flags here (like FNDELAY), so we may
26551878Smaybee 		 * block if there are locks present... this
26561878Smaybee 		 * should be addressed in openat().
26571878Smaybee 		 */
26586992Smaybee 		/* XXX - would it be OK to generate a log record here? */
26596992Smaybee 		err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
26601878Smaybee 		if (err) {
26611878Smaybee 			ZFS_EXIT(zfsvfs);
26621878Smaybee 			return (err);
26631878Smaybee 		}
2664789Sahrens 	}
2665789Sahrens 
26665331Samw 	if (mask & (AT_ATIME|AT_MTIME) ||
26675331Samw 	    ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
26685331Samw 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
26695331Samw 	    XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
26705331Samw 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
267111935SMark.Shellenbaum@Sun.COM 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
26725331Samw 		need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
26735331Samw 		    skipaclchk, cr);
267411935SMark.Shellenbaum@Sun.COM 	}
2675789Sahrens 
2676789Sahrens 	if (mask & (AT_UID|AT_GID)) {
2677789Sahrens 		int	idmask = (mask & (AT_UID|AT_GID));
2678789Sahrens 		int	take_owner;
2679789Sahrens 		int	take_group;
2680789Sahrens 
2681789Sahrens 		/*
2682913Smarks 		 * NOTE: even if a new mode is being set,
2683913Smarks 		 * we may clear S_ISUID/S_ISGID bits.
2684913Smarks 		 */
2685913Smarks 
2686913Smarks 		if (!(mask & AT_MODE))
268711935SMark.Shellenbaum@Sun.COM 			vap->va_mode = zp->z_mode;
2688913Smarks 
2689913Smarks 		/*
2690789Sahrens 		 * Take ownership or chgrp to group we are a member of
2691789Sahrens 		 */
2692789Sahrens 
2693789Sahrens 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
26945331Samw 		take_group = (mask & AT_GID) &&
26955331Samw 		    zfs_groupmember(zfsvfs, vap->va_gid, cr);
2696789Sahrens 
2697789Sahrens 		/*
2698789Sahrens 		 * If both AT_UID and AT_GID are set then take_owner and
2699789Sahrens 		 * take_group must both be set in order to allow taking
2700789Sahrens 		 * ownership.
2701789Sahrens 		 *
2702789Sahrens 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2703789Sahrens 		 *
2704789Sahrens 		 */
2705789Sahrens 
2706789Sahrens 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2707789Sahrens 		    ((idmask == AT_UID) && take_owner) ||
2708789Sahrens 		    ((idmask == AT_GID) && take_group)) {
27095331Samw 			if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
27105331Samw 			    skipaclchk, cr) == 0) {
2711789Sahrens 				/*
2712789Sahrens 				 * Remove setuid/setgid for non-privileged users
2713789Sahrens 				 */
27141115Smarks 				secpolicy_setid_clear(vap, cr);
27152796Smarks 				trim_mask = (mask & (AT_UID|AT_GID));
2716789Sahrens 			} else {
2717789Sahrens 				need_policy =  TRUE;
2718789Sahrens 			}
2719789Sahrens 		} else {
2720789Sahrens 			need_policy =  TRUE;
2721789Sahrens 		}
2722789Sahrens 	}
2723789Sahrens 
27242796Smarks 	mutex_enter(&zp->z_lock);
272511935SMark.Shellenbaum@Sun.COM 	oldva.va_mode = zp->z_mode;
272611935SMark.Shellenbaum@Sun.COM 	oldva.va_uid = zp->z_uid;
272711935SMark.Shellenbaum@Sun.COM 	oldva.va_gid = zp->z_gid;
27285331Samw 	if (mask & AT_XVATTR) {
27298190SMark.Shellenbaum@Sun.COM 		/*
27308190SMark.Shellenbaum@Sun.COM 		 * Update xvattr mask to include only those attributes
27318190SMark.Shellenbaum@Sun.COM 		 * that are actually changing.
27328190SMark.Shellenbaum@Sun.COM 		 *
27338190SMark.Shellenbaum@Sun.COM 		 * the bits will be restored prior to actually setting
27348190SMark.Shellenbaum@Sun.COM 		 * the attributes so the caller thinks they were set.
27358190SMark.Shellenbaum@Sun.COM 		 */
27368190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
27378190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_appendonly !=
273811935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
27398190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27408190SMark.Shellenbaum@Sun.COM 			} else {
27418190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_APPENDONLY);
27428190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
27438190SMark.Shellenbaum@Sun.COM 			}
27448190SMark.Shellenbaum@Sun.COM 		}
27458190SMark.Shellenbaum@Sun.COM 
27468190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
27478190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_nounlink !=
274811935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
27498190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27508190SMark.Shellenbaum@Sun.COM 			} else {
27518190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_NOUNLINK);
27528190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
27538190SMark.Shellenbaum@Sun.COM 			}
27548190SMark.Shellenbaum@Sun.COM 		}
27558190SMark.Shellenbaum@Sun.COM 
27568190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
27578190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_immutable !=
275811935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
27598190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27608190SMark.Shellenbaum@Sun.COM 			} else {
27618190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
27628190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
27638190SMark.Shellenbaum@Sun.COM 			}
27648190SMark.Shellenbaum@Sun.COM 		}
27658190SMark.Shellenbaum@Sun.COM 
27668190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
27678190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_nodump !=
276811935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NODUMP) != 0)) {
27698190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27708190SMark.Shellenbaum@Sun.COM 			} else {
27718190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_NODUMP);
27728190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
27738190SMark.Shellenbaum@Sun.COM 			}
27748190SMark.Shellenbaum@Sun.COM 		}
27758190SMark.Shellenbaum@Sun.COM 
27768190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
27778190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_av_modified !=
277811935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
27798190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27808190SMark.Shellenbaum@Sun.COM 			} else {
27818190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
27828190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
27838190SMark.Shellenbaum@Sun.COM 			}
27848190SMark.Shellenbaum@Sun.COM 		}
27858190SMark.Shellenbaum@Sun.COM 
27868190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
27878190SMark.Shellenbaum@Sun.COM 			if ((vp->v_type != VREG &&
27888190SMark.Shellenbaum@Sun.COM 			    xoap->xoa_av_quarantined) ||
27898190SMark.Shellenbaum@Sun.COM 			    xoap->xoa_av_quarantined !=
279011935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
27918190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27928190SMark.Shellenbaum@Sun.COM 			} else {
27938190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
27948190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
27958190SMark.Shellenbaum@Sun.COM 			}
27968190SMark.Shellenbaum@Sun.COM 		}
27978190SMark.Shellenbaum@Sun.COM 
279810793Sdai.ngo@sun.com 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
279910793Sdai.ngo@sun.com 			mutex_exit(&zp->z_lock);
280010793Sdai.ngo@sun.com 			ZFS_EXIT(zfsvfs);
280110793Sdai.ngo@sun.com 			return (EPERM);
280210793Sdai.ngo@sun.com 		}
280310793Sdai.ngo@sun.com 
28048190SMark.Shellenbaum@Sun.COM 		if (need_policy == FALSE &&
28058190SMark.Shellenbaum@Sun.COM 		    (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
28068190SMark.Shellenbaum@Sun.COM 		    XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
28075331Samw 			need_policy = TRUE;
28085331Samw 		}
28095331Samw 	}
28105331Samw 
28112796Smarks 	mutex_exit(&zp->z_lock);
28122796Smarks 
28132796Smarks 	if (mask & AT_MODE) {
28145331Samw 		if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
28152796Smarks 			err = secpolicy_setid_setsticky_clear(vp, vap,
28162796Smarks 			    &oldva, cr);
28172796Smarks 			if (err) {
28182796Smarks 				ZFS_EXIT(zfsvfs);
28192796Smarks 				return (err);
28202796Smarks 			}
28212796Smarks 			trim_mask |= AT_MODE;
28222796Smarks 		} else {
28232796Smarks 			need_policy = TRUE;
28242796Smarks 		}
28252796Smarks 	}
2826789Sahrens 
2827789Sahrens 	if (need_policy) {
28281115Smarks 		/*
28291115Smarks 		 * If trim_mask is set then take ownership
28302796Smarks 		 * has been granted or write_acl is present and user
28312796Smarks 		 * has the ability to modify mode.  In that case remove
28322796Smarks 		 * UID|GID and or MODE from mask so that
28331115Smarks 		 * secpolicy_vnode_setattr() doesn't revoke it.
28341115Smarks 		 */
28352796Smarks 
28362796Smarks 		if (trim_mask) {
28372796Smarks 			saved_mask = vap->va_mask;
28382796Smarks 			vap->va_mask &= ~trim_mask;
28392796Smarks 		}
2840789Sahrens 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
28415331Samw 		    (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2842789Sahrens 		if (err) {
2843789Sahrens 			ZFS_EXIT(zfsvfs);
2844789Sahrens 			return (err);
2845789Sahrens 		}
28461115Smarks 
28471115Smarks 		if (trim_mask)
28482796Smarks 			vap->va_mask |= saved_mask;
2849789Sahrens 	}
2850789Sahrens 
2851789Sahrens 	/*
2852789Sahrens 	 * secpolicy_vnode_setattr, or take ownership may have
2853789Sahrens 	 * changed va_mask
2854789Sahrens 	 */
2855789Sahrens 	mask = vap->va_mask;
2856789Sahrens 
285711935SMark.Shellenbaum@Sun.COM 	if ((mask & (AT_UID | AT_GID))) {
285811935SMark.Shellenbaum@Sun.COM 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xattr_obj,
285911935SMark.Shellenbaum@Sun.COM 		    sizeof (xattr_obj));
286011935SMark.Shellenbaum@Sun.COM 
286111935SMark.Shellenbaum@Sun.COM 		if (xattr_obj) {
286211935SMark.Shellenbaum@Sun.COM 			err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
28639396SMatthew.Ahrens@Sun.COM 			if (err)
286411935SMark.Shellenbaum@Sun.COM 				goto out2;
28659179SMark.Shellenbaum@Sun.COM 		}
28669179SMark.Shellenbaum@Sun.COM 		if (mask & AT_UID) {
28679179SMark.Shellenbaum@Sun.COM 			new_uid = zfs_fuid_create(zfsvfs,
28689179SMark.Shellenbaum@Sun.COM 			    (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
286911935SMark.Shellenbaum@Sun.COM 			if (vap->va_uid != zp->z_uid &&
287011935SMark.Shellenbaum@Sun.COM 			    zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
28719396SMatthew.Ahrens@Sun.COM 				err = EDQUOT;
287211935SMark.Shellenbaum@Sun.COM 				goto out2;
28739396SMatthew.Ahrens@Sun.COM 			}
28741231Smarks 		}
28759396SMatthew.Ahrens@Sun.COM 
28769179SMark.Shellenbaum@Sun.COM 		if (mask & AT_GID) {
28779179SMark.Shellenbaum@Sun.COM 			new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
28789179SMark.Shellenbaum@Sun.COM 			    cr, ZFS_GROUP, &fuidp);
287911935SMark.Shellenbaum@Sun.COM 			if (new_gid != zp->z_gid &&
288011935SMark.Shellenbaum@Sun.COM 			    zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
28819396SMatthew.Ahrens@Sun.COM 				err = EDQUOT;
288211935SMark.Shellenbaum@Sun.COM 				goto out2;
28839179SMark.Shellenbaum@Sun.COM 			}
28849179SMark.Shellenbaum@Sun.COM 		}
28851231Smarks 	}
288611935SMark.Shellenbaum@Sun.COM 	tx = dmu_tx_create(zfsvfs->z_os);
288711935SMark.Shellenbaum@Sun.COM 
288811935SMark.Shellenbaum@Sun.COM 	if (mask & AT_MODE) {
288911935SMark.Shellenbaum@Sun.COM 		uint64_t pmode = zp->z_mode;
289011935SMark.Shellenbaum@Sun.COM 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
289111935SMark.Shellenbaum@Sun.COM 
289211935SMark.Shellenbaum@Sun.COM 		if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
289311935SMark.Shellenbaum@Sun.COM 			goto out;
289411935SMark.Shellenbaum@Sun.COM 
289511935SMark.Shellenbaum@Sun.COM 		if (!zp->z_is_sa && ZFS_EXTERNAL_ACL(zp)) {
289611935SMark.Shellenbaum@Sun.COM 			/*
289711935SMark.Shellenbaum@Sun.COM 			 * Are we upgrading ACL from old V0 format
289811935SMark.Shellenbaum@Sun.COM 			 * to V1 format?
289911935SMark.Shellenbaum@Sun.COM 			 */
290011935SMark.Shellenbaum@Sun.COM 			if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
290111935SMark.Shellenbaum@Sun.COM 			    ZNODE_ACL_VERSION(zp) ==
290211935SMark.Shellenbaum@Sun.COM 			    ZFS_ACL_VERSION_INITIAL) {
290311935SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_free(tx,
290411935SMark.Shellenbaum@Sun.COM 				    ZFS_EXTERNAL_ACL(zp), 0,
290511935SMark.Shellenbaum@Sun.COM 				    DMU_OBJECT_END);
290611935SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
290711935SMark.Shellenbaum@Sun.COM 				    0, aclp->z_acl_bytes);
290811935SMark.Shellenbaum@Sun.COM 			} else {
290911935SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_write(tx, ZFS_EXTERNAL_ACL(zp), 0,
291011935SMark.Shellenbaum@Sun.COM 				    aclp->z_acl_bytes);
291111935SMark.Shellenbaum@Sun.COM 			}
291211935SMark.Shellenbaum@Sun.COM 		} else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
291311935SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
291411935SMark.Shellenbaum@Sun.COM 			    0, aclp->z_acl_bytes);
291511935SMark.Shellenbaum@Sun.COM 		}
291611935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
291711935SMark.Shellenbaum@Sun.COM 	} else {
291811935SMark.Shellenbaum@Sun.COM 		if ((mask & AT_XVATTR) &&
291911935SMark.Shellenbaum@Sun.COM 		    XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
292011935SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
292111935SMark.Shellenbaum@Sun.COM 		else
292211935SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
292311935SMark.Shellenbaum@Sun.COM 	}
292411935SMark.Shellenbaum@Sun.COM 
292511935SMark.Shellenbaum@Sun.COM 	if (attrzp) {
292611935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
292711935SMark.Shellenbaum@Sun.COM 	}
292811935SMark.Shellenbaum@Sun.COM 
292911935SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
293011935SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
293111935SMark.Shellenbaum@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
293211935SMark.Shellenbaum@Sun.COM 
293311935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
29341231Smarks 
29358227SNeil.Perrin@Sun.COM 	err = dmu_tx_assign(tx, TXG_NOWAIT);
2936789Sahrens 	if (err) {
29379396SMatthew.Ahrens@Sun.COM 		if (err == ERESTART)
29382113Sahrens 			dmu_tx_wait(tx);
29399396SMatthew.Ahrens@Sun.COM 		goto out;
2940789Sahrens 	}
2941789Sahrens 
294211935SMark.Shellenbaum@Sun.COM 	count = 0;
2943789Sahrens 	/*
2944789Sahrens 	 * Set each attribute requested.
2945789Sahrens 	 * We group settings according to the locks they need to acquire.
2946789Sahrens 	 *
2947789Sahrens 	 * Note: you cannot set ctime directly, although it will be
2948789Sahrens 	 * updated as a side-effect of calling this function.
2949789Sahrens 	 */
2950789Sahrens 
2951789Sahrens 	mutex_enter(&zp->z_lock);
2952789Sahrens 
295311935SMark.Shellenbaum@Sun.COM 	if (attrzp)
295411935SMark.Shellenbaum@Sun.COM 		mutex_enter(&attrzp->z_lock);
295511935SMark.Shellenbaum@Sun.COM 
2956*12164SMark.Shellenbaum@Sun.COM 	if (mask & (AT_UID|AT_GID)) {
2957*12164SMark.Shellenbaum@Sun.COM 
2958*12164SMark.Shellenbaum@Sun.COM 		if (mask & AT_UID) {
2959*12164SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
2960*12164SMark.Shellenbaum@Sun.COM 			    &new_uid, sizeof (new_uid));
2961*12164SMark.Shellenbaum@Sun.COM 			zp->z_uid = zfs_fuid_map_id(zfsvfs, new_uid,
2962*12164SMark.Shellenbaum@Sun.COM 			    cr, ZFS_OWNER);
2963*12164SMark.Shellenbaum@Sun.COM 			if (attrzp) {
2964*12164SMark.Shellenbaum@Sun.COM 				SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2965*12164SMark.Shellenbaum@Sun.COM 				    SA_ZPL_UID(zfsvfs), NULL, &new_uid,
2966*12164SMark.Shellenbaum@Sun.COM 				    sizeof (new_uid));
2967*12164SMark.Shellenbaum@Sun.COM 				attrzp->z_gid = zp->z_uid;
2968*12164SMark.Shellenbaum@Sun.COM 			}
296911935SMark.Shellenbaum@Sun.COM 		}
2970*12164SMark.Shellenbaum@Sun.COM 
2971*12164SMark.Shellenbaum@Sun.COM 		if (mask & AT_GID) {
2972*12164SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
2973*12164SMark.Shellenbaum@Sun.COM 			    NULL, &new_gid, sizeof (new_gid));
2974*12164SMark.Shellenbaum@Sun.COM 			zp->z_gid = zfs_fuid_map_id(zfsvfs, new_gid, cr,
2975*12164SMark.Shellenbaum@Sun.COM 			    ZFS_GROUP);
2976*12164SMark.Shellenbaum@Sun.COM 			if (attrzp) {
2977*12164SMark.Shellenbaum@Sun.COM 				SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2978*12164SMark.Shellenbaum@Sun.COM 				    SA_ZPL_GID(zfsvfs), NULL, &new_gid,
2979*12164SMark.Shellenbaum@Sun.COM 				    sizeof (new_gid));
2980*12164SMark.Shellenbaum@Sun.COM 				attrzp->z_gid = zp->z_gid;
2981*12164SMark.Shellenbaum@Sun.COM 			}
2982*12164SMark.Shellenbaum@Sun.COM 		}
2983*12164SMark.Shellenbaum@Sun.COM 		if (!(mask & AT_MODE)) {
2984*12164SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
2985*12164SMark.Shellenbaum@Sun.COM 			    NULL, &new_mode, sizeof (new_mode));
2986*12164SMark.Shellenbaum@Sun.COM 			new_mode = zp->z_mode;
2987*12164SMark.Shellenbaum@Sun.COM 		}
2988*12164SMark.Shellenbaum@Sun.COM 		err = zfs_acl_chown_setattr(zp);
2989*12164SMark.Shellenbaum@Sun.COM 		ASSERT(err == 0);
299011935SMark.Shellenbaum@Sun.COM 		if (attrzp) {
2991*12164SMark.Shellenbaum@Sun.COM 			err = zfs_acl_chown_setattr(attrzp);
2992*12164SMark.Shellenbaum@Sun.COM 			ASSERT(err == 0);
299311935SMark.Shellenbaum@Sun.COM 		}
299411935SMark.Shellenbaum@Sun.COM 	}
299511935SMark.Shellenbaum@Sun.COM 
2996789Sahrens 	if (mask & AT_MODE) {
29975824Smarks 		mutex_enter(&zp->z_acl_lock);
299811935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
299911935SMark.Shellenbaum@Sun.COM 		    &new_mode, sizeof (new_mode));
300011935SMark.Shellenbaum@Sun.COM 		zp->z_mode = new_mode;
3001*12164SMark.Shellenbaum@Sun.COM 		ASSERT3U((uintptr_t)aclp, !=, NULL);
30029179SMark.Shellenbaum@Sun.COM 		err = zfs_aclset_common(zp, aclp, cr, tx);
3003789Sahrens 		ASSERT3U(err, ==, 0);
300410143STim.Haley@Sun.COM 		zp->z_acl_cached = aclp;
300510143STim.Haley@Sun.COM 		aclp = NULL;
30065824Smarks 		mutex_exit(&zp->z_acl_lock);
3007789Sahrens 	}
3008789Sahrens 
30091231Smarks 	if (attrzp)
301011935SMark.Shellenbaum@Sun.COM 		mutex_exit(&attrzp->z_lock);
301111935SMark.Shellenbaum@Sun.COM 
301211935SMark.Shellenbaum@Sun.COM 	if (mask & AT_ATIME) {
301311935SMark.Shellenbaum@Sun.COM 		ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
301411935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
301511935SMark.Shellenbaum@Sun.COM 		    &zp->z_atime, sizeof (zp->z_atime));
30161231Smarks 	}
30171231Smarks 
301811935SMark.Shellenbaum@Sun.COM 	if (mask & AT_MTIME) {
301911935SMark.Shellenbaum@Sun.COM 		ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
302011935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
302111935SMark.Shellenbaum@Sun.COM 		    mtime, sizeof (mtime));
30221231Smarks 	}
30231231Smarks 
30246992Smaybee 	/* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
302511935SMark.Shellenbaum@Sun.COM 	if (mask & AT_SIZE && !(mask & AT_MTIME)) {
302611935SMark.Shellenbaum@Sun.COM 		if (!(mask & AT_MTIME))
302711935SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
302811935SMark.Shellenbaum@Sun.COM 			    NULL, mtime, sizeof (mtime));
302911935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
303011935SMark.Shellenbaum@Sun.COM 		    &ctime, sizeof (ctime));
303111935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
303211935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
303311935SMark.Shellenbaum@Sun.COM 	} else if (mask != 0) {
303411935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
303511935SMark.Shellenbaum@Sun.COM 		    &ctime, sizeof (ctime));
303611935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
303711935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
303811935SMark.Shellenbaum@Sun.COM 		if (attrzp) {
303911935SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
304011935SMark.Shellenbaum@Sun.COM 			    SA_ZPL_CTIME(zfsvfs), NULL,
304111935SMark.Shellenbaum@Sun.COM 			    &ctime, sizeof (ctime));
304211935SMark.Shellenbaum@Sun.COM 			zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
304311935SMark.Shellenbaum@Sun.COM 			    mtime, ctime, B_TRUE);
304411935SMark.Shellenbaum@Sun.COM 		}
304511935SMark.Shellenbaum@Sun.COM 	}
30465331Samw 	/*
30475331Samw 	 * Do this after setting timestamps to prevent timestamp
30485331Samw 	 * update from toggling bit
30495331Samw 	 */
30505331Samw 
30515331Samw 	if (xoap && (mask & AT_XVATTR)) {
30528190SMark.Shellenbaum@Sun.COM 
30538190SMark.Shellenbaum@Sun.COM 		/*
30548190SMark.Shellenbaum@Sun.COM 		 * restore trimmed off masks
30558190SMark.Shellenbaum@Sun.COM 		 * so that return masks can be set for caller.
30568190SMark.Shellenbaum@Sun.COM 		 */
30578190SMark.Shellenbaum@Sun.COM 
30588190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
30598190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_APPENDONLY);
30608190SMark.Shellenbaum@Sun.COM 		}
30618190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
30628190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_NOUNLINK);
30638190SMark.Shellenbaum@Sun.COM 		}
30648190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
30658190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_IMMUTABLE);
30668190SMark.Shellenbaum@Sun.COM 		}
30678190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
30688190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_NODUMP);
30698190SMark.Shellenbaum@Sun.COM 		}
30708190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
30718190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
30728190SMark.Shellenbaum@Sun.COM 		}
30738190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
30748190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
30758190SMark.Shellenbaum@Sun.COM 		}
30768190SMark.Shellenbaum@Sun.COM 
307711935SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
30785331Samw 			ASSERT(vp->v_type == VREG);
30795331Samw 
308011935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
308111935SMark.Shellenbaum@Sun.COM 		    &zp->z_pflags, sizeof (zp->z_pflags));
308211935SMark.Shellenbaum@Sun.COM 		zfs_xvattr_set(zp, xvap, tx);
30835331Samw 	}
3084789Sahrens 
30859179SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
30869179SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
30879179SMark.Shellenbaum@Sun.COM 
30881878Smaybee 	if (mask != 0)
30895331Samw 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
30905331Samw 
3091789Sahrens 	mutex_exit(&zp->z_lock);
3092789Sahrens 
30939396SMatthew.Ahrens@Sun.COM out:
309411935SMark.Shellenbaum@Sun.COM 	if (err == 0 && attrzp) {
309511935SMark.Shellenbaum@Sun.COM 		err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
309611935SMark.Shellenbaum@Sun.COM 		    xattr_count, tx);
309711935SMark.Shellenbaum@Sun.COM 		ASSERT(err2 == 0);
309811935SMark.Shellenbaum@Sun.COM 	}
309911935SMark.Shellenbaum@Sun.COM 
31001231Smarks 	if (attrzp)
31011231Smarks 		VN_RELE(ZTOV(attrzp));
310210143STim.Haley@Sun.COM 	if (aclp)
310310143STim.Haley@Sun.COM 		zfs_acl_free(aclp);
310410143STim.Haley@Sun.COM 
31059396SMatthew.Ahrens@Sun.COM 	if (fuidp) {
31069396SMatthew.Ahrens@Sun.COM 		zfs_fuid_info_free(fuidp);
31079396SMatthew.Ahrens@Sun.COM 		fuidp = NULL;
31089396SMatthew.Ahrens@Sun.COM 	}
31099396SMatthew.Ahrens@Sun.COM 
311011935SMark.Shellenbaum@Sun.COM 	if (err) {
31119396SMatthew.Ahrens@Sun.COM 		dmu_tx_abort(tx);
311211935SMark.Shellenbaum@Sun.COM 		if (err == ERESTART)
311311935SMark.Shellenbaum@Sun.COM 			goto top;
311411935SMark.Shellenbaum@Sun.COM 	} else {
311511935SMark.Shellenbaum@Sun.COM 		err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
31169396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
311711935SMark.Shellenbaum@Sun.COM 	}
311811935SMark.Shellenbaum@Sun.COM 
311911935SMark.Shellenbaum@Sun.COM 
312011935SMark.Shellenbaum@Sun.COM out2:
3121789Sahrens 	ZFS_EXIT(zfsvfs);
3122789Sahrens 	return (err);
3123789Sahrens }
3124789Sahrens 
31253271Smaybee typedef struct zfs_zlock {
31263271Smaybee 	krwlock_t	*zl_rwlock;	/* lock we acquired */
31273271Smaybee 	znode_t		*zl_znode;	/* znode we held */
31283271Smaybee 	struct zfs_zlock *zl_next;	/* next in list */
31293271Smaybee } zfs_zlock_t;
31303271Smaybee 
31313271Smaybee /*
31323271Smaybee  * Drop locks and release vnodes that were held by zfs_rename_lock().
31333271Smaybee  */
31343271Smaybee static void
31353271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp)
31363271Smaybee {
31373271Smaybee 	zfs_zlock_t *zl;
31383271Smaybee 
31393271Smaybee 	while ((zl = *zlpp) != NULL) {
31403271Smaybee 		if (zl->zl_znode != NULL)
31413271Smaybee 			VN_RELE(ZTOV(zl->zl_znode));
31423271Smaybee 		rw_exit(zl->zl_rwlock);
31433271Smaybee 		*zlpp = zl->zl_next;
31443271Smaybee 		kmem_free(zl, sizeof (*zl));
31453271Smaybee 	}
31463271Smaybee }
31473271Smaybee 
3148789Sahrens /*
3149789Sahrens  * Search back through the directory tree, using the ".." entries.
3150789Sahrens  * Lock each directory in the chain to prevent concurrent renames.
3151789Sahrens  * Fail any attempt to move a directory into one of its own descendants.
3152789Sahrens  * XXX - z_parent_lock can overlap with map or grow locks
3153789Sahrens  */
3154789Sahrens static int
3155789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3156789Sahrens {
3157789Sahrens 	zfs_zlock_t	*zl;
31583638Sbillm 	znode_t		*zp = tdzp;
3159789Sahrens 	uint64_t	rootid = zp->z_zfsvfs->z_root;
316011935SMark.Shellenbaum@Sun.COM 	uint64_t	oidp = zp->z_id;
3161789Sahrens 	krwlock_t	*rwlp = &szp->z_parent_lock;
3162789Sahrens 	krw_t		rw = RW_WRITER;
3163789Sahrens 
3164789Sahrens 	/*
3165789Sahrens 	 * First pass write-locks szp and compares to zp->z_id.
3166789Sahrens 	 * Later passes read-lock zp and compare to zp->z_parent.
3167789Sahrens 	 */
3168789Sahrens 	do {
31693271Smaybee 		if (!rw_tryenter(rwlp, rw)) {
31703271Smaybee 			/*
31713271Smaybee 			 * Another thread is renaming in this path.
31723271Smaybee 			 * Note that if we are a WRITER, we don't have any
31733271Smaybee 			 * parent_locks held yet.
31743271Smaybee 			 */
31753271Smaybee 			if (rw == RW_READER && zp->z_id > szp->z_id) {
31763271Smaybee 				/*
31773271Smaybee 				 * Drop our locks and restart
31783271Smaybee 				 */
31793271Smaybee 				zfs_rename_unlock(&zl);
31803271Smaybee 				*zlpp = NULL;
31813271Smaybee 				zp = tdzp;
318211935SMark.Shellenbaum@Sun.COM 				oidp = zp->z_id;
31833271Smaybee 				rwlp = &szp->z_parent_lock;
31843271Smaybee 				rw = RW_WRITER;
31853271Smaybee 				continue;
31863271Smaybee 			} else {
31873271Smaybee 				/*
31883271Smaybee 				 * Wait for other thread to drop its locks
31893271Smaybee 				 */
31903271Smaybee 				rw_enter(rwlp, rw);
31913271Smaybee 			}
31923271Smaybee 		}
31933271Smaybee 
3194789Sahrens 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3195789Sahrens 		zl->zl_rwlock = rwlp;
3196789Sahrens 		zl->zl_znode = NULL;
3197789Sahrens 		zl->zl_next = *zlpp;
3198789Sahrens 		*zlpp = zl;
3199789Sahrens 
320011935SMark.Shellenbaum@Sun.COM 		if (oidp == szp->z_id)		/* We're a descendant of szp */
3201789Sahrens 			return (EINVAL);
3202789Sahrens 
320311935SMark.Shellenbaum@Sun.COM 		if (oidp == rootid)		/* We've hit the top */
3204789Sahrens 			return (0);
3205789Sahrens 
3206789Sahrens 		if (rw == RW_READER) {		/* i.e. not the first pass */
320711935SMark.Shellenbaum@Sun.COM 			int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3208789Sahrens 			if (error)
3209789Sahrens 				return (error);
3210789Sahrens 			zl->zl_znode = zp;
3211789Sahrens 		}
321211935SMark.Shellenbaum@Sun.COM 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
321311935SMark.Shellenbaum@Sun.COM 		    &oidp, sizeof (oidp));
3214789Sahrens 		rwlp = &zp->z_parent_lock;
3215789Sahrens 		rw = RW_READER;
3216789Sahrens 
3217789Sahrens 	} while (zp->z_id != sdzp->z_id);
3218789Sahrens 
3219789Sahrens 	return (0);
3220789Sahrens }
3221789Sahrens 
3222789Sahrens /*
3223789Sahrens  * Move an entry from the provided source directory to the target
3224789Sahrens  * directory.  Change the entry name as indicated.
3225789Sahrens  *
3226789Sahrens  *	IN:	sdvp	- Source directory containing the "old entry".
3227789Sahrens  *		snm	- Old entry name.
3228789Sahrens  *		tdvp	- Target directory to contain the "new entry".
3229789Sahrens  *		tnm	- New entry name.
3230789Sahrens  *		cr	- credentials of caller.
32315331Samw  *		ct	- caller context
32325331Samw  *		flags	- case flags
3233789Sahrens  *
3234789Sahrens  *	RETURN:	0 if success
3235789Sahrens  *		error code if failure
3236789Sahrens  *
3237789Sahrens  * Timestamps:
3238789Sahrens  *	sdvp,tdvp - ctime|mtime updated
3239789Sahrens  */
32405331Samw /*ARGSUSED*/
3241789Sahrens static int
32425331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
32435331Samw     caller_context_t *ct, int flags)
3244789Sahrens {
3245789Sahrens 	znode_t		*tdzp, *szp, *tzp;
3246789Sahrens 	znode_t		*sdzp = VTOZ(sdvp);
3247789Sahrens 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
32485326Sek110237 	zilog_t		*zilog;
3249789Sahrens 	vnode_t		*realvp;
3250789Sahrens 	zfs_dirlock_t	*sdl, *tdl;
3251789Sahrens 	dmu_tx_t	*tx;
3252789Sahrens 	zfs_zlock_t	*zl;
32535331Samw 	int		cmp, serr, terr;
32545331Samw 	int		error = 0;
32555331Samw 	int		zflg = 0;
3256789Sahrens 
32575367Sahrens 	ZFS_ENTER(zfsvfs);
32585367Sahrens 	ZFS_VERIFY_ZP(sdzp);
32595326Sek110237 	zilog = zfsvfs->z_log;
3260789Sahrens 
3261789Sahrens 	/*
3262789Sahrens 	 * Make sure we have the real vp for the target directory.
3263789Sahrens 	 */
32645331Samw 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3265789Sahrens 		tdvp = realvp;
3266789Sahrens 
326712079SMark.Shellenbaum@Sun.COM 	if (tdvp->v_vfsp != sdvp->v_vfsp || zfsctl_is_node(tdvp)) {
3268789Sahrens 		ZFS_EXIT(zfsvfs);
3269789Sahrens 		return (EXDEV);
3270789Sahrens 	}
3271789Sahrens 
3272789Sahrens 	tdzp = VTOZ(tdvp);
32735367Sahrens 	ZFS_VERIFY_ZP(tdzp);
32745498Stimh 	if (zfsvfs->z_utf8 && u8_validate(tnm,
32755331Samw 	    strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
32765331Samw 		ZFS_EXIT(zfsvfs);
32775331Samw 		return (EILSEQ);
32785331Samw 	}
32795331Samw 
32805331Samw 	if (flags & FIGNORECASE)
32815331Samw 		zflg |= ZCILOOK;
32825331Samw 
3283789Sahrens top:
3284789Sahrens 	szp = NULL;
3285789Sahrens 	tzp = NULL;
3286789Sahrens 	zl = NULL;
3287789Sahrens 
3288789Sahrens 	/*
3289789Sahrens 	 * This is to prevent the creation of links into attribute space
3290789Sahrens 	 * by renaming a linked file into/outof an attribute directory.
3291789Sahrens 	 * See the comment in zfs_link() for why this is considered bad.
3292789Sahrens 	 */
329311935SMark.Shellenbaum@Sun.COM 	if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3294789Sahrens 		ZFS_EXIT(zfsvfs);
3295789Sahrens 		return (EINVAL);
3296789Sahrens 	}
3297789Sahrens 
3298789Sahrens 	/*
3299789Sahrens 	 * Lock source and target directory entries.  To prevent deadlock,
3300789Sahrens 	 * a lock ordering must be defined.  We lock the directory with
3301789Sahrens 	 * the smallest object id first, or if it's a tie, the one with
3302789Sahrens 	 * the lexically first name.
3303789Sahrens 	 */
3304789Sahrens 	if (sdzp->z_id < tdzp->z_id) {
3305789Sahrens 		cmp = -1;
3306789Sahrens 	} else if (sdzp->z_id > tdzp->z_id) {
3307789Sahrens 		cmp = 1;
3308789Sahrens 	} else {
33095331Samw 		/*
33105331Samw 		 * First compare the two name arguments without
33115331Samw 		 * considering any case folding.
33125331Samw 		 */
33135331Samw 		int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
33145331Samw 
33155331Samw 		cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
33165498Stimh 		ASSERT(error == 0 || !zfsvfs->z_utf8);
3317789Sahrens 		if (cmp == 0) {
3318789Sahrens 			/*
3319789Sahrens 			 * POSIX: "If the old argument and the new argument
3320789Sahrens 			 * both refer to links to the same existing file,
3321789Sahrens 			 * the rename() function shall return successfully
3322789Sahrens 			 * and perform no other action."
3323789Sahrens 			 */
3324789Sahrens 			ZFS_EXIT(zfsvfs);
3325789Sahrens 			return (0);
3326789Sahrens 		}
33275331Samw 		/*
33285331Samw 		 * If the file system is case-folding, then we may
33295331Samw 		 * have some more checking to do.  A case-folding file
33305331Samw 		 * system is either supporting mixed case sensitivity
33315331Samw 		 * access or is completely case-insensitive.  Note
33325331Samw 		 * that the file system is always case preserving.
33335331Samw 		 *
33345331Samw 		 * In mixed sensitivity mode case sensitive behavior
33355331Samw 		 * is the default.  FIGNORECASE must be used to
33365331Samw 		 * explicitly request case insensitive behavior.
33375331Samw 		 *
33385331Samw 		 * If the source and target names provided differ only
33395331Samw 		 * by case (e.g., a request to rename 'tim' to 'Tim'),
33405331Samw 		 * we will treat this as a special case in the
33415331Samw 		 * case-insensitive mode: as long as the source name
33425331Samw 		 * is an exact match, we will allow this to proceed as
33435331Samw 		 * a name-change request.
33445331Samw 		 */
33455498Stimh 		if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
33465498Stimh 		    (zfsvfs->z_case == ZFS_CASE_MIXED &&
33475498Stimh 		    flags & FIGNORECASE)) &&
33485331Samw 		    u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
33495331Samw 		    &error) == 0) {
33505331Samw 			/*
33515331Samw 			 * case preserving rename request, require exact
33525331Samw 			 * name matches
33535331Samw 			 */
33545331Samw 			zflg |= ZCIEXACT;
33555331Samw 			zflg &= ~ZCILOOK;
33565331Samw 		}
3357789Sahrens 	}
33585331Samw 
335911321SSanjeev.Bagewadi@Sun.COM 	/*
336011321SSanjeev.Bagewadi@Sun.COM 	 * If the source and destination directories are the same, we should
336111321SSanjeev.Bagewadi@Sun.COM 	 * grab the z_name_lock of that directory only once.
336211321SSanjeev.Bagewadi@Sun.COM 	 */
336311321SSanjeev.Bagewadi@Sun.COM 	if (sdzp == tdzp) {
336411321SSanjeev.Bagewadi@Sun.COM 		zflg |= ZHAVELOCK;
336511321SSanjeev.Bagewadi@Sun.COM 		rw_enter(&sdzp->z_name_lock, RW_READER);
336611321SSanjeev.Bagewadi@Sun.COM 	}
336711321SSanjeev.Bagewadi@Sun.COM 
3368789Sahrens 	if (cmp < 0) {
33695331Samw 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
33705331Samw 		    ZEXISTS | zflg, NULL, NULL);
33715331Samw 		terr = zfs_dirent_lock(&tdl,
33725331Samw 		    tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3373789Sahrens 	} else {
33745331Samw 		terr = zfs_dirent_lock(&tdl,
33755331Samw 		    tdzp, tnm, &tzp, zflg, NULL, NULL);
33765331Samw 		serr = zfs_dirent_lock(&sdl,
33775331Samw 		    sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
33785331Samw 		    NULL, NULL);
3379789Sahrens 	}
3380789Sahrens 
3381789Sahrens 	if (serr) {
3382789Sahrens 		/*
3383789Sahrens 		 * Source entry invalid or not there.
3384789Sahrens 		 */
3385789Sahrens 		if (!terr) {
3386789Sahrens 			zfs_dirent_unlock(tdl);
3387789Sahrens 			if (tzp)
3388789Sahrens 				VN_RELE(ZTOV(tzp));
3389789Sahrens 		}
339011321SSanjeev.Bagewadi@Sun.COM 
339111321SSanjeev.Bagewadi@Sun.COM 		if (sdzp == tdzp)
339211321SSanjeev.Bagewadi@Sun.COM 			rw_exit(&sdzp->z_name_lock);
339311321SSanjeev.Bagewadi@Sun.COM 
3394789Sahrens 		if (strcmp(snm, "..") == 0)
3395789Sahrens 			serr = EINVAL;
3396789Sahrens 		ZFS_EXIT(zfsvfs);
3397789Sahrens 		return (serr);
3398789Sahrens 	}
3399789Sahrens 	if (terr) {
3400789Sahrens 		zfs_dirent_unlock(sdl);
3401789Sahrens 		VN_RELE(ZTOV(szp));
340211321SSanjeev.Bagewadi@Sun.COM 
340311321SSanjeev.Bagewadi@Sun.COM 		if (sdzp == tdzp)
340411321SSanjeev.Bagewadi@Sun.COM 			rw_exit(&sdzp->z_name_lock);
340511321SSanjeev.Bagewadi@Sun.COM 
3406789Sahrens 		if (strcmp(tnm, "..") == 0)
3407789Sahrens 			terr = EINVAL;
3408789Sahrens 		ZFS_EXIT(zfsvfs);
3409789Sahrens 		return (terr);
3410789Sahrens 	}
3411789Sahrens 
3412789Sahrens 	/*
3413789Sahrens 	 * Must have write access at the source to remove the old entry
3414789Sahrens 	 * and write access at the target to create the new entry.
3415789Sahrens 	 * Note that if target and source are the same, this can be
3416789Sahrens 	 * done in a single check.
3417789Sahrens 	 */
3418789Sahrens 
3419789Sahrens 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3420789Sahrens 		goto out;
3421789Sahrens 
3422789Sahrens 	if (ZTOV(szp)->v_type == VDIR) {
3423789Sahrens 		/*
3424789Sahrens 		 * Check to make sure rename is valid.
3425789Sahrens 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3426789Sahrens 		 */
3427789Sahrens 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3428789Sahrens 			goto out;
3429789Sahrens 	}
3430789Sahrens 
3431789Sahrens 	/*
3432789Sahrens 	 * Does target exist?
3433789Sahrens 	 */
3434789Sahrens 	if (tzp) {
3435789Sahrens 		/*
3436789Sahrens 		 * Source and target must be the same type.
3437789Sahrens 		 */
3438789Sahrens 		if (ZTOV(szp)->v_type == VDIR) {
3439789Sahrens 			if (ZTOV(tzp)->v_type != VDIR) {
3440789Sahrens 				error = ENOTDIR;
3441789Sahrens 				goto out;
3442789Sahrens 			}
3443789Sahrens 		} else {
3444789Sahrens 			if (ZTOV(tzp)->v_type == VDIR) {
3445789Sahrens 				error = EISDIR;
3446789Sahrens 				goto out;
3447789Sahrens 			}
3448789Sahrens 		}
3449789Sahrens 		/*
3450789Sahrens 		 * POSIX dictates that when the source and target
3451789Sahrens 		 * entries refer to the same file object, rename
3452789Sahrens 		 * must do nothing and exit without error.
3453789Sahrens 		 */
3454789Sahrens 		if (szp->z_id == tzp->z_id) {
3455789Sahrens 			error = 0;
3456789Sahrens 			goto out;
3457789Sahrens 		}
3458789Sahrens 	}
3459789Sahrens 
34605331Samw 	vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3461789Sahrens 	if (tzp)
34625331Samw 		vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
34634863Spraks 
34644863Spraks 	/*
34654863Spraks 	 * notify the target directory if it is not the same
34664863Spraks 	 * as source directory.
34674863Spraks 	 */
34684863Spraks 	if (tdvp != sdvp) {
34695331Samw 		vnevent_rename_dest_dir(tdvp, ct);
34704863Spraks 	}
3471789Sahrens 
3472789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
347311935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
347411935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
34751544Seschrock 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
34761544Seschrock 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
347711935SMark.Shellenbaum@Sun.COM 	if (sdzp != tdzp) {
347811935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
347911935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, tdzp);
348011935SMark.Shellenbaum@Sun.COM 	}
348111935SMark.Shellenbaum@Sun.COM 	if (tzp) {
348211935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
348311935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, tzp);
348411935SMark.Shellenbaum@Sun.COM 	}
348511935SMark.Shellenbaum@Sun.COM 
348611935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, szp);
34873461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
34888227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3489789Sahrens 	if (error) {
3490789Sahrens 		if (zl != NULL)
3491789Sahrens 			zfs_rename_unlock(&zl);
3492789Sahrens 		zfs_dirent_unlock(sdl);
3493789Sahrens 		zfs_dirent_unlock(tdl);
349411321SSanjeev.Bagewadi@Sun.COM 
349511321SSanjeev.Bagewadi@Sun.COM 		if (sdzp == tdzp)
349611321SSanjeev.Bagewadi@Sun.COM 			rw_exit(&sdzp->z_name_lock);
349711321SSanjeev.Bagewadi@Sun.COM 
3498789Sahrens 		VN_RELE(ZTOV(szp));
3499789Sahrens 		if (tzp)
3500789Sahrens 			VN_RELE(ZTOV(tzp));
35018227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
35022113Sahrens 			dmu_tx_wait(tx);
35032113Sahrens 			dmu_tx_abort(tx);
3504789Sahrens 			goto top;
3505789Sahrens 		}
35062113Sahrens 		dmu_tx_abort(tx);
3507789Sahrens 		ZFS_EXIT(zfsvfs);
3508789Sahrens 		return (error);
3509789Sahrens 	}
3510789Sahrens 
3511789Sahrens 	if (tzp)	/* Attempt to remove the existing target */
35125331Samw 		error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3513789Sahrens 
3514789Sahrens 	if (error == 0) {
3515789Sahrens 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3516789Sahrens 		if (error == 0) {
351711935SMark.Shellenbaum@Sun.COM 			szp->z_pflags |= ZFS_AV_MODIFIED;
351811935SMark.Shellenbaum@Sun.COM 
351911935SMark.Shellenbaum@Sun.COM 			error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
352011935SMark.Shellenbaum@Sun.COM 			    (void *)&szp->z_pflags, sizeof (uint64_t), tx);
352111935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error, ==, 0);
35225331Samw 
3523789Sahrens 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
352411935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error, ==, 0);
35255331Samw 
35265331Samw 			zfs_log_rename(zilog, tx,
35275331Samw 			    TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0),
35285331Samw 			    sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
35296976Seschrock 
35306976Seschrock 			/* Update path information for the target vnode */
35316976Seschrock 			vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm));
3532789Sahrens 		}
3533789Sahrens 	}
3534789Sahrens 
3535789Sahrens 	dmu_tx_commit(tx);
3536789Sahrens out:
3537789Sahrens 	if (zl != NULL)
3538789Sahrens 		zfs_rename_unlock(&zl);
3539789Sahrens 
3540789Sahrens 	zfs_dirent_unlock(sdl);
3541789Sahrens 	zfs_dirent_unlock(tdl);
3542789Sahrens 
354311321SSanjeev.Bagewadi@Sun.COM 	if (sdzp == tdzp)
354411321SSanjeev.Bagewadi@Sun.COM 		rw_exit(&sdzp->z_name_lock);
354511321SSanjeev.Bagewadi@Sun.COM 
354611321SSanjeev.Bagewadi@Sun.COM 
3547789Sahrens 	VN_RELE(ZTOV(szp));
3548789Sahrens 	if (tzp)
3549789Sahrens 		VN_RELE(ZTOV(tzp));
3550789Sahrens 
3551789Sahrens 	ZFS_EXIT(zfsvfs);
3552789Sahrens 	return (error);
3553789Sahrens }
3554789Sahrens 
3555789Sahrens /*
3556789Sahrens  * Insert the indicated symbolic reference entry into the directory.
3557789Sahrens  *
3558789Sahrens  *	IN:	dvp	- Directory to contain new symbolic link.
3559789Sahrens  *		link	- Name for new symlink entry.
3560789Sahrens  *		vap	- Attributes of new entry.
3561789Sahrens  *		target	- Target path of new symlink.
3562789Sahrens  *		cr	- credentials of caller.
35635331Samw  *		ct	- caller context
35645331Samw  *		flags	- case flags
3565789Sahrens  *
3566789Sahrens  *	RETURN:	0 if success
3567789Sahrens  *		error code if failure
3568789Sahrens  *
3569789Sahrens  * Timestamps:
3570789Sahrens  *	dvp - ctime|mtime updated
3571789Sahrens  */
35725331Samw /*ARGSUSED*/
3573789Sahrens static int
35745331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
35755331Samw     caller_context_t *ct, int flags)
3576789Sahrens {
3577789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
3578789Sahrens 	zfs_dirlock_t	*dl;
3579789Sahrens 	dmu_tx_t	*tx;
3580789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
35815326Sek110237 	zilog_t		*zilog;
358211935SMark.Shellenbaum@Sun.COM 	uint64_t	len = strlen(link);
3583789Sahrens 	int		error;
35845331Samw 	int		zflg = ZNEW;
35859179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t	acl_ids;
35869179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
358711935SMark.Shellenbaum@Sun.COM 	uint64_t	txtype = TX_SYMLINK;
3588789Sahrens 
3589789Sahrens 	ASSERT(vap->va_type == VLNK);
3590789Sahrens 
35915367Sahrens 	ZFS_ENTER(zfsvfs);
35925367Sahrens 	ZFS_VERIFY_ZP(dzp);
35935326Sek110237 	zilog = zfsvfs->z_log;
35945331Samw 
35955498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
35965331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
35975331Samw 		ZFS_EXIT(zfsvfs);
35985331Samw 		return (EILSEQ);
35995331Samw 	}
36005331Samw 	if (flags & FIGNORECASE)
36015331Samw 		zflg |= ZCILOOK;
3602789Sahrens top:
36035331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3604789Sahrens 		ZFS_EXIT(zfsvfs);
3605789Sahrens 		return (error);
3606789Sahrens 	}
3607789Sahrens 
3608789Sahrens 	if (len > MAXPATHLEN) {
3609789Sahrens 		ZFS_EXIT(zfsvfs);
3610789Sahrens 		return (ENAMETOOLONG);
3611789Sahrens 	}
3612789Sahrens 
3613789Sahrens 	/*
3614789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3615789Sahrens 	 */
36165331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
36175331Samw 	if (error) {
3618789Sahrens 		ZFS_EXIT(zfsvfs);
3619789Sahrens 		return (error);
3620789Sahrens 	}
3621789Sahrens 
36229179SMark.Shellenbaum@Sun.COM 	VERIFY(0 == zfs_acl_ids_create(dzp, 0, vap, cr, NULL, &acl_ids));
36239396SMatthew.Ahrens@Sun.COM 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
36249396SMatthew.Ahrens@Sun.COM 		zfs_acl_ids_free(&acl_ids);
36259396SMatthew.Ahrens@Sun.COM 		zfs_dirent_unlock(dl);
36269396SMatthew.Ahrens@Sun.COM 		ZFS_EXIT(zfsvfs);
36279396SMatthew.Ahrens@Sun.COM 		return (EDQUOT);
36289396SMatthew.Ahrens@Sun.COM 	}
3629789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
36309179SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
3631789Sahrens 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
36321544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
363311935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
363411935SMark.Shellenbaum@Sun.COM 	    ZFS_SA_BASE_ATTR_SIZE + len);
363511935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
363611935SMark.Shellenbaum@Sun.COM 	if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
363711935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
363811935SMark.Shellenbaum@Sun.COM 		    acl_ids.z_aclp->z_acl_bytes);
363911935SMark.Shellenbaum@Sun.COM 	}
36409396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
36419396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
36428227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3643789Sahrens 	if (error) {
36449179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
3645789Sahrens 		zfs_dirent_unlock(dl);
36468227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
36472113Sahrens 			dmu_tx_wait(tx);
36482113Sahrens 			dmu_tx_abort(tx);
3649789Sahrens 			goto top;
3650789Sahrens 		}
36512113Sahrens 		dmu_tx_abort(tx);
3652789Sahrens 		ZFS_EXIT(zfsvfs);
3653789Sahrens 		return (error);
3654789Sahrens 	}
3655789Sahrens 
3656789Sahrens 	/*
3657789Sahrens 	 * Create a new object for the symlink.
365811935SMark.Shellenbaum@Sun.COM 	 * for version 4 ZPL datsets the symlink will be an SA attribute
3659789Sahrens 	 */
366011935SMark.Shellenbaum@Sun.COM 
366111935SMark.Shellenbaum@Sun.COM 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
366211935SMark.Shellenbaum@Sun.COM 
366311935SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
366411935SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
366511935SMark.Shellenbaum@Sun.COM 
366611935SMark.Shellenbaum@Sun.COM 	if (zp->z_is_sa)
366711935SMark.Shellenbaum@Sun.COM 		error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
366811935SMark.Shellenbaum@Sun.COM 		    link, len, tx);
366911935SMark.Shellenbaum@Sun.COM 	else
367011935SMark.Shellenbaum@Sun.COM 		zfs_sa_symlink(zp, link, len, tx);
367111935SMark.Shellenbaum@Sun.COM 
367211935SMark.Shellenbaum@Sun.COM 	zp->z_size = len;
367311935SMark.Shellenbaum@Sun.COM 	(void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
367411935SMark.Shellenbaum@Sun.COM 	    &zp->z_size, sizeof (zp->z_size), tx);
3675789Sahrens 	/*
3676789Sahrens 	 * Insert the new object into the directory.
3677789Sahrens 	 */
3678789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
367911935SMark.Shellenbaum@Sun.COM 
368011935SMark.Shellenbaum@Sun.COM 	if (flags & FIGNORECASE)
368111935SMark.Shellenbaum@Sun.COM 		txtype |= TX_CI;
368211935SMark.Shellenbaum@Sun.COM 	zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
36839179SMark.Shellenbaum@Sun.COM 
36849179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_free(&acl_ids);
3685789Sahrens 
3686789Sahrens 	dmu_tx_commit(tx);
3687789Sahrens 
3688789Sahrens 	zfs_dirent_unlock(dl);
3689789Sahrens 
3690789Sahrens 	VN_RELE(ZTOV(zp));
3691789Sahrens 
3692789Sahrens 	ZFS_EXIT(zfsvfs);
3693789Sahrens 	return (error);
3694789Sahrens }
3695789Sahrens 
3696789Sahrens /*
3697789Sahrens  * Return, in the buffer contained in the provided uio structure,
3698789Sahrens  * the symbolic path referred to by vp.
3699789Sahrens  *
3700789Sahrens  *	IN:	vp	- vnode of symbolic link.
3701789Sahrens  *		uoip	- structure to contain the link path.
3702789Sahrens  *		cr	- credentials of caller.
37035331Samw  *		ct	- caller context
3704789Sahrens  *
3705789Sahrens  *	OUT:	uio	- structure to contain the link path.
3706789Sahrens  *
3707789Sahrens  *	RETURN:	0 if success
3708789Sahrens  *		error code if failure
3709789Sahrens  *
3710789Sahrens  * Timestamps:
3711789Sahrens  *	vp - atime updated
3712789Sahrens  */
3713789Sahrens /* ARGSUSED */
3714789Sahrens static int
37155331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3716789Sahrens {
3717789Sahrens 	znode_t		*zp = VTOZ(vp);
3718789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3719789Sahrens 	int		error;
3720789Sahrens 
37215367Sahrens 	ZFS_ENTER(zfsvfs);
37225367Sahrens 	ZFS_VERIFY_ZP(zp);
3723789Sahrens 
372411935SMark.Shellenbaum@Sun.COM 	if (zp->z_is_sa)
372511935SMark.Shellenbaum@Sun.COM 		error = sa_lookup_uio(zp->z_sa_hdl,
372611935SMark.Shellenbaum@Sun.COM 		    SA_ZPL_SYMLINK(zfsvfs), uio);
372711935SMark.Shellenbaum@Sun.COM 	else
372811935SMark.Shellenbaum@Sun.COM 		error = zfs_sa_readlink(zp, uio);
3729789Sahrens 
3730789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
373111935SMark.Shellenbaum@Sun.COM 
3732789Sahrens 	ZFS_EXIT(zfsvfs);
3733789Sahrens 	return (error);
3734789Sahrens }
3735789Sahrens 
3736789Sahrens /*
3737789Sahrens  * Insert a new entry into directory tdvp referencing svp.
3738789Sahrens  *
3739789Sahrens  *	IN:	tdvp	- Directory to contain new entry.
3740789Sahrens  *		svp	- vnode of new entry.
3741789Sahrens  *		name	- name of new entry.
3742789Sahrens  *		cr	- credentials of caller.
37435331Samw  *		ct	- caller context
3744789Sahrens  *
3745789Sahrens  *	RETURN:	0 if success
3746789Sahrens  *		error code if failure
3747789Sahrens  *
3748789Sahrens  * Timestamps:
3749789Sahrens  *	tdvp - ctime|mtime updated
3750789Sahrens  *	 svp - ctime updated
3751789Sahrens  */
3752789Sahrens /* ARGSUSED */
3753789Sahrens static int
37545331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
37555331Samw     caller_context_t *ct, int flags)
3756789Sahrens {
3757789Sahrens 	znode_t		*dzp = VTOZ(tdvp);
3758789Sahrens 	znode_t		*tzp, *szp;
3759789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
37605326Sek110237 	zilog_t		*zilog;
3761789Sahrens 	zfs_dirlock_t	*dl;
3762789Sahrens 	dmu_tx_t	*tx;
3763789Sahrens 	vnode_t		*realvp;
3764789Sahrens 	int		error;
37655331Samw 	int		zf = ZNEW;
376612079SMark.Shellenbaum@Sun.COM 	uint64_t	parent;
3767789Sahrens 
3768789Sahrens 	ASSERT(tdvp->v_type == VDIR);
3769789Sahrens 
37705367Sahrens 	ZFS_ENTER(zfsvfs);
37715367Sahrens 	ZFS_VERIFY_ZP(dzp);
37725326Sek110237 	zilog = zfsvfs->z_log;
3773789Sahrens 
37745331Samw 	if (VOP_REALVP(svp, &realvp, ct) == 0)
3775789Sahrens 		svp = realvp;
3776789Sahrens 
377712079SMark.Shellenbaum@Sun.COM 	/*
377812079SMark.Shellenbaum@Sun.COM 	 * POSIX dictates that we return EPERM here.
377912079SMark.Shellenbaum@Sun.COM 	 * Better choices include ENOTSUP or EISDIR.
378012079SMark.Shellenbaum@Sun.COM 	 */
378112079SMark.Shellenbaum@Sun.COM 	if (svp->v_type == VDIR) {
378212079SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
378312079SMark.Shellenbaum@Sun.COM 		return (EPERM);
378412079SMark.Shellenbaum@Sun.COM 	}
378512079SMark.Shellenbaum@Sun.COM 
378612079SMark.Shellenbaum@Sun.COM 	if (svp->v_vfsp != tdvp->v_vfsp || zfsctl_is_node(svp)) {
3787789Sahrens 		ZFS_EXIT(zfsvfs);
3788789Sahrens 		return (EXDEV);
3789789Sahrens 	}
379012079SMark.Shellenbaum@Sun.COM 
37915367Sahrens 	szp = VTOZ(svp);
37925367Sahrens 	ZFS_VERIFY_ZP(szp);
3793789Sahrens 
379412079SMark.Shellenbaum@Sun.COM 	/* Prevent links to .zfs/shares files */
379512079SMark.Shellenbaum@Sun.COM 
379612079SMark.Shellenbaum@Sun.COM 	if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
379712079SMark.Shellenbaum@Sun.COM 	    &parent, sizeof (uint64_t))) != 0) {
379812079SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
379912079SMark.Shellenbaum@Sun.COM 		return (error);
380012079SMark.Shellenbaum@Sun.COM 	}
380112079SMark.Shellenbaum@Sun.COM 	if (parent == zfsvfs->z_shares_dir) {
380212079SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
380312079SMark.Shellenbaum@Sun.COM 		return (EPERM);
380412079SMark.Shellenbaum@Sun.COM 	}
380512079SMark.Shellenbaum@Sun.COM 
38065498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name,
38075331Samw 	    strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
38085331Samw 		ZFS_EXIT(zfsvfs);
38095331Samw 		return (EILSEQ);
38105331Samw 	}
38115331Samw 	if (flags & FIGNORECASE)
38125331Samw 		zf |= ZCILOOK;
38135331Samw 
3814789Sahrens 	/*
3815789Sahrens 	 * We do not support links between attributes and non-attributes
3816789Sahrens 	 * because of the potential security risk of creating links
3817789Sahrens 	 * into "normal" file space in order to circumvent restrictions
3818789Sahrens 	 * imposed in attribute space.
3819789Sahrens 	 */
382011935SMark.Shellenbaum@Sun.COM 	if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
3821789Sahrens 		ZFS_EXIT(zfsvfs);
3822789Sahrens 		return (EINVAL);
3823789Sahrens 	}
3824789Sahrens 
3825789Sahrens 
382611935SMark.Shellenbaum@Sun.COM 	if (szp->z_uid != crgetuid(cr) &&
3827789Sahrens 	    secpolicy_basic_link(cr) != 0) {
3828789Sahrens 		ZFS_EXIT(zfsvfs);
3829789Sahrens 		return (EPERM);
3830789Sahrens 	}
3831789Sahrens 
38325331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3833789Sahrens 		ZFS_EXIT(zfsvfs);
3834789Sahrens 		return (error);
3835789Sahrens 	}
3836789Sahrens 
383712079SMark.Shellenbaum@Sun.COM top:
3838789Sahrens 	/*
3839789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3840789Sahrens 	 */
38415331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
38425331Samw 	if (error) {
3843789Sahrens 		ZFS_EXIT(zfsvfs);
3844789Sahrens 		return (error);
3845789Sahrens 	}
3846789Sahrens 
3847789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
384811935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
38491544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
385011935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, szp);
385111935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, dzp);
38528227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3853789Sahrens 	if (error) {
3854789Sahrens 		zfs_dirent_unlock(dl);
38558227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
38562113Sahrens 			dmu_tx_wait(tx);
38572113Sahrens 			dmu_tx_abort(tx);
3858789Sahrens 			goto top;
3859789Sahrens 		}
38602113Sahrens 		dmu_tx_abort(tx);
3861789Sahrens 		ZFS_EXIT(zfsvfs);
3862789Sahrens 		return (error);
3863789Sahrens 	}
3864789Sahrens 
3865789Sahrens 	error = zfs_link_create(dl, szp, tx, 0);
3866789Sahrens 
38675331Samw 	if (error == 0) {
38685331Samw 		uint64_t txtype = TX_LINK;
38695331Samw 		if (flags & FIGNORECASE)
38705331Samw 			txtype |= TX_CI;
38715331Samw 		zfs_log_link(zilog, tx, txtype, dzp, szp, name);
38725331Samw 	}
3873789Sahrens 
3874789Sahrens 	dmu_tx_commit(tx);
3875789Sahrens 
3876789Sahrens 	zfs_dirent_unlock(dl);
3877789Sahrens 
38784863Spraks 	if (error == 0) {
38795331Samw 		vnevent_link(svp, ct);
38804863Spraks 	}
38814863Spraks 
3882789Sahrens 	ZFS_EXIT(zfsvfs);
3883789Sahrens 	return (error);
3884789Sahrens }
3885789Sahrens 
3886789Sahrens /*
3887789Sahrens  * zfs_null_putapage() is used when the file system has been force
3888789Sahrens  * unmounted. It just drops the pages.
3889789Sahrens  */
3890789Sahrens /* ARGSUSED */
3891789Sahrens static int
3892789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3893789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3894789Sahrens {
3895789Sahrens 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
3896789Sahrens 	return (0);
3897789Sahrens }
3898789Sahrens 
38992688Smaybee /*
39002688Smaybee  * Push a page out to disk, klustering if possible.
39012688Smaybee  *
39022688Smaybee  *	IN:	vp	- file to push page to.
39032688Smaybee  *		pp	- page to push.
39042688Smaybee  *		flags	- additional flags.
39052688Smaybee  *		cr	- credentials of caller.
39062688Smaybee  *
39072688Smaybee  *	OUT:	offp	- start of range pushed.
39082688Smaybee  *		lenp	- len of range pushed.
39092688Smaybee  *
39102688Smaybee  *	RETURN:	0 if success
39112688Smaybee  *		error code if failure
39122688Smaybee  *
39132688Smaybee  * NOTE: callers must have locked the page to be pushed.  On
39142688Smaybee  * exit, the page (and all other pages in the kluster) must be
39152688Smaybee  * unlocked.
39162688Smaybee  */
3917789Sahrens /* ARGSUSED */
3918789Sahrens static int
3919789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3920789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3921789Sahrens {
3922789Sahrens 	znode_t		*zp = VTOZ(vp);
3923789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3924789Sahrens 	dmu_tx_t	*tx;
39252688Smaybee 	u_offset_t	off, koff;
39262688Smaybee 	size_t		len, klen;
3927789Sahrens 	int		err;
3928789Sahrens 
39292688Smaybee 	off = pp->p_offset;
39302688Smaybee 	len = PAGESIZE;
39312688Smaybee 	/*
39322688Smaybee 	 * If our blocksize is bigger than the page size, try to kluster
39338227SNeil.Perrin@Sun.COM 	 * multiple pages so that we write a full block (thus avoiding
39342688Smaybee 	 * a read-modify-write).
39352688Smaybee 	 */
393611935SMark.Shellenbaum@Sun.COM 	if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
39378636SMark.Maybee@Sun.COM 		klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
39388636SMark.Maybee@Sun.COM 		koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
393911935SMark.Shellenbaum@Sun.COM 		ASSERT(koff <= zp->z_size);
394011935SMark.Shellenbaum@Sun.COM 		if (koff + klen > zp->z_size)
394111935SMark.Shellenbaum@Sun.COM 			klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
39422688Smaybee 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
39432688Smaybee 	}
39442688Smaybee 	ASSERT3U(btop(len), ==, btopr(len));
39458636SMark.Maybee@Sun.COM 
39461819Smaybee 	/*
39471819Smaybee 	 * Can't push pages past end-of-file.
39481819Smaybee 	 */
394911935SMark.Shellenbaum@Sun.COM 	if (off >= zp->z_size) {
39504709Smaybee 		/* ignore all pages */
39512688Smaybee 		err = 0;
39522688Smaybee 		goto out;
395311935SMark.Shellenbaum@Sun.COM 	} else if (off + len > zp->z_size) {
395411935SMark.Shellenbaum@Sun.COM 		int npages = btopr(zp->z_size - off);
39552688Smaybee 		page_t *trunc;
39562688Smaybee 
39572688Smaybee 		page_list_break(&pp, &trunc, npages);
39584709Smaybee 		/* ignore pages past end of file */
39592688Smaybee 		if (trunc)
39604709Smaybee 			pvn_write_done(trunc, flags);
396111935SMark.Shellenbaum@Sun.COM 		len = zp->z_size - off;
39621819Smaybee 	}
39639396SMatthew.Ahrens@Sun.COM 
396411935SMark.Shellenbaum@Sun.COM 	if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
396511935SMark.Shellenbaum@Sun.COM 	    zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
39669396SMatthew.Ahrens@Sun.COM 		err = EDQUOT;
39679396SMatthew.Ahrens@Sun.COM 		goto out;
39689396SMatthew.Ahrens@Sun.COM 	}
39698636SMark.Maybee@Sun.COM top:
3970789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3971789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, off, len);
397211935SMark.Shellenbaum@Sun.COM 
397311935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
397411935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
39758227SNeil.Perrin@Sun.COM 	err = dmu_tx_assign(tx, TXG_NOWAIT);
3976789Sahrens 	if (err != 0) {
39778227SNeil.Perrin@Sun.COM 		if (err == ERESTART) {
39782113Sahrens 			dmu_tx_wait(tx);
39792113Sahrens 			dmu_tx_abort(tx);
3980789Sahrens 			goto top;
3981789Sahrens 		}
39822113Sahrens 		dmu_tx_abort(tx);
3983789Sahrens 		goto out;
3984789Sahrens 	}
3985789Sahrens 
39862688Smaybee 	if (zp->z_blksz <= PAGESIZE) {
39877315SJonathan.Adams@Sun.COM 		caddr_t va = zfs_map_page(pp, S_READ);
39882688Smaybee 		ASSERT3U(len, <=, PAGESIZE);
39892688Smaybee 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
39907315SJonathan.Adams@Sun.COM 		zfs_unmap_page(pp, va);
39912688Smaybee 	} else {
39922688Smaybee 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
39932688Smaybee 	}
39942688Smaybee 
39952688Smaybee 	if (err == 0) {
399611935SMark.Shellenbaum@Sun.COM 		uint64_t mtime[2], ctime[2];
399711935SMark.Shellenbaum@Sun.COM 		sa_bulk_attr_t bulk[2];
399811935SMark.Shellenbaum@Sun.COM 		int count = 0;
399911935SMark.Shellenbaum@Sun.COM 
400011935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
400111935SMark.Shellenbaum@Sun.COM 		    &mtime, 16);
400211935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
400311935SMark.Shellenbaum@Sun.COM 		    &ctime, 16);
400411935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
400511935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
40068636SMark.Maybee@Sun.COM 		zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
40072688Smaybee 	}
40089951SLin.Ling@Sun.COM 	dmu_tx_commit(tx);
40092688Smaybee 
40102688Smaybee out:
40114709Smaybee 	pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4012789Sahrens 	if (offp)
4013789Sahrens 		*offp = off;
4014789Sahrens 	if (lenp)
4015789Sahrens 		*lenp = len;
4016789Sahrens 
4017789Sahrens 	return (err);
4018789Sahrens }
4019789Sahrens 
4020789Sahrens /*
4021789Sahrens  * Copy the portion of the file indicated from pages into the file.
4022789Sahrens  * The pages are stored in a page list attached to the files vnode.
4023789Sahrens  *
4024789Sahrens  *	IN:	vp	- vnode of file to push page data to.
4025789Sahrens  *		off	- position in file to put data.
4026789Sahrens  *		len	- amount of data to write.
4027789Sahrens  *		flags	- flags to control the operation.
4028789Sahrens  *		cr	- credentials of caller.
40295331Samw  *		ct	- caller context.
4030789Sahrens  *
4031789Sahrens  *	RETURN:	0 if success
4032789Sahrens  *		error code if failure
4033789Sahrens  *
4034789Sahrens  * Timestamps:
4035789Sahrens  *	vp - ctime|mtime updated
4036789Sahrens  */
40375331Samw /*ARGSUSED*/
4038789Sahrens static int
40395331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
40405331Samw     caller_context_t *ct)
4041789Sahrens {
4042789Sahrens 	znode_t		*zp = VTOZ(vp);
4043789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4044789Sahrens 	page_t		*pp;
4045789Sahrens 	size_t		io_len;
4046789Sahrens 	u_offset_t	io_off;
40478636SMark.Maybee@Sun.COM 	uint_t		blksz;
40488636SMark.Maybee@Sun.COM 	rl_t		*rl;
4049789Sahrens 	int		error = 0;
4050789Sahrens 
40515367Sahrens 	ZFS_ENTER(zfsvfs);
40525367Sahrens 	ZFS_VERIFY_ZP(zp);
4053789Sahrens 
40548636SMark.Maybee@Sun.COM 	/*
40558636SMark.Maybee@Sun.COM 	 * Align this request to the file block size in case we kluster.
40568636SMark.Maybee@Sun.COM 	 * XXX - this can result in pretty aggresive locking, which can
40578636SMark.Maybee@Sun.COM 	 * impact simultanious read/write access.  One option might be
40588636SMark.Maybee@Sun.COM 	 * to break up long requests (len == 0) into block-by-block
40598636SMark.Maybee@Sun.COM 	 * operations to get narrower locking.
40608636SMark.Maybee@Sun.COM 	 */
40618636SMark.Maybee@Sun.COM 	blksz = zp->z_blksz;
40628636SMark.Maybee@Sun.COM 	if (ISP2(blksz))
40638636SMark.Maybee@Sun.COM 		io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
40648636SMark.Maybee@Sun.COM 	else
40658636SMark.Maybee@Sun.COM 		io_off = 0;
40668636SMark.Maybee@Sun.COM 	if (len > 0 && ISP2(blksz))
40679141SMark.Maybee@Sun.COM 		io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
40688636SMark.Maybee@Sun.COM 	else
40698636SMark.Maybee@Sun.COM 		io_len = 0;
40708636SMark.Maybee@Sun.COM 
40718636SMark.Maybee@Sun.COM 	if (io_len == 0) {
4072789Sahrens 		/*
40738636SMark.Maybee@Sun.COM 		 * Search the entire vp list for pages >= io_off.
4074789Sahrens 		 */
40758636SMark.Maybee@Sun.COM 		rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
40768636SMark.Maybee@Sun.COM 		error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
40771472Sperrin 		goto out;
4078789Sahrens 	}
40798636SMark.Maybee@Sun.COM 	rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
40808636SMark.Maybee@Sun.COM 
408111935SMark.Shellenbaum@Sun.COM 	if (off > zp->z_size) {
4082789Sahrens 		/* past end of file */
40838636SMark.Maybee@Sun.COM 		zfs_range_unlock(rl);
4084789Sahrens 		ZFS_EXIT(zfsvfs);
4085789Sahrens 		return (0);
4086789Sahrens 	}
4087789Sahrens 
408811935SMark.Shellenbaum@Sun.COM 	len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
40898636SMark.Maybee@Sun.COM 
40908636SMark.Maybee@Sun.COM 	for (off = io_off; io_off < off + len; io_off += io_len) {
4091789Sahrens 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
40921669Sperrin 			pp = page_lookup(vp, io_off,
40934339Sperrin 			    (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4094789Sahrens 		} else {
4095789Sahrens 			pp = page_lookup_nowait(vp, io_off,
40964339Sperrin 			    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4097789Sahrens 		}
4098789Sahrens 
4099789Sahrens 		if (pp != NULL && pvn_getdirty(pp, flags)) {
4100789Sahrens 			int err;
4101789Sahrens 
4102789Sahrens 			/*
4103789Sahrens 			 * Found a dirty page to push
4104789Sahrens 			 */
41051669Sperrin 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
41061669Sperrin 			if (err)
4107789Sahrens 				error = err;
4108789Sahrens 		} else {
4109789Sahrens 			io_len = PAGESIZE;
4110789Sahrens 		}
4111789Sahrens 	}
41121472Sperrin out:
41138636SMark.Maybee@Sun.COM 	zfs_range_unlock(rl);
41142638Sperrin 	if ((flags & B_ASYNC) == 0)
41152638Sperrin 		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
4116789Sahrens 	ZFS_EXIT(zfsvfs);
4117789Sahrens 	return (error);
4118789Sahrens }
4119789Sahrens 
41205331Samw /*ARGSUSED*/
4121789Sahrens void
41225331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4123789Sahrens {
4124789Sahrens 	znode_t	*zp = VTOZ(vp);
4125789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4126789Sahrens 	int error;
4127789Sahrens 
41285326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
412911935SMark.Shellenbaum@Sun.COM 	if (zp->z_sa_hdl == NULL) {
41305446Sahrens 		/*
41315642Smaybee 		 * The fs has been unmounted, or we did a
41325642Smaybee 		 * suspend/resume and this file no longer exists.
41335446Sahrens 		 */
4134789Sahrens 		if (vn_has_cached_data(vp)) {
4135789Sahrens 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
4136789Sahrens 			    B_INVAL, cr);
4137789Sahrens 		}
4138789Sahrens 
41391544Seschrock 		mutex_enter(&zp->z_lock);
414010369Schris.kirby@sun.com 		mutex_enter(&vp->v_lock);
414110369Schris.kirby@sun.com 		ASSERT(vp->v_count == 1);
414210369Schris.kirby@sun.com 		vp->v_count = 0;
414310369Schris.kirby@sun.com 		mutex_exit(&vp->v_lock);
41445446Sahrens 		mutex_exit(&zp->z_lock);
41455642Smaybee 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
41465446Sahrens 		zfs_znode_free(zp);
4147789Sahrens 		return;
4148789Sahrens 	}
4149789Sahrens 
4150789Sahrens 	/*
4151789Sahrens 	 * Attempt to push any data in the page cache.  If this fails
4152789Sahrens 	 * we will get kicked out later in zfs_zinactive().
4153789Sahrens 	 */
41541298Sperrin 	if (vn_has_cached_data(vp)) {
41551298Sperrin 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
41561298Sperrin 		    cr);
41571298Sperrin 	}
4158789Sahrens 
41593461Sahrens 	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4160789Sahrens 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4161789Sahrens 
416211935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
416311935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, zp);
4164789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
4165789Sahrens 		if (error) {
4166789Sahrens 			dmu_tx_abort(tx);
4167789Sahrens 		} else {
4168789Sahrens 			mutex_enter(&zp->z_lock);
416911935SMark.Shellenbaum@Sun.COM 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
417011935SMark.Shellenbaum@Sun.COM 			    (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4171789Sahrens 			zp->z_atime_dirty = 0;
4172789Sahrens 			mutex_exit(&zp->z_lock);
4173789Sahrens 			dmu_tx_commit(tx);
4174789Sahrens 		}
4175789Sahrens 	}
4176789Sahrens 
4177789Sahrens 	zfs_zinactive(zp);
41785326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
4179789Sahrens }
4180789Sahrens 
4181789Sahrens /*
4182789Sahrens  * Bounds-check the seek operation.
4183789Sahrens  *
4184789Sahrens  *	IN:	vp	- vnode seeking within
4185789Sahrens  *		ooff	- old file offset
4186789Sahrens  *		noffp	- pointer to new file offset
41875331Samw  *		ct	- caller context
4188789Sahrens  *
4189789Sahrens  *	RETURN:	0 if success
4190789Sahrens  *		EINVAL if new offset invalid
4191789Sahrens  */
4192789Sahrens /* ARGSUSED */
4193789Sahrens static int
41945331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
41955331Samw     caller_context_t *ct)
4196789Sahrens {
4197789Sahrens 	if (vp->v_type == VDIR)
4198789Sahrens 		return (0);
4199789Sahrens 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4200789Sahrens }
4201789Sahrens 
4202789Sahrens /*
4203789Sahrens  * Pre-filter the generic locking function to trap attempts to place
4204789Sahrens  * a mandatory lock on a memory mapped file.
4205789Sahrens  */
4206789Sahrens static int
4207789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
42085331Samw     flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4209789Sahrens {
4210789Sahrens 	znode_t *zp = VTOZ(vp);
4211789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4212789Sahrens 
42135367Sahrens 	ZFS_ENTER(zfsvfs);
42145367Sahrens 	ZFS_VERIFY_ZP(zp);
4215789Sahrens 
4216789Sahrens 	/*
42171544Seschrock 	 * We are following the UFS semantics with respect to mapcnt
42181544Seschrock 	 * here: If we see that the file is mapped already, then we will
42191544Seschrock 	 * return an error, but we don't worry about races between this
42201544Seschrock 	 * function and zfs_map().
4221789Sahrens 	 */
422211935SMark.Shellenbaum@Sun.COM 	if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4223789Sahrens 		ZFS_EXIT(zfsvfs);
4224789Sahrens 		return (EAGAIN);
4225789Sahrens 	}
4226789Sahrens 	ZFS_EXIT(zfsvfs);
422710896SMark.Shellenbaum@Sun.COM 	return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4228789Sahrens }
4229789Sahrens 
4230789Sahrens /*
4231789Sahrens  * If we can't find a page in the cache, we will create a new page
4232789Sahrens  * and fill it with file data.  For efficiency, we may try to fill
42338636SMark.Maybee@Sun.COM  * multiple pages at once (klustering) to fill up the supplied page
42349265SMark.Maybee@Sun.COM  * list.  Note that the pages to be filled are held with an exclusive
42359265SMark.Maybee@Sun.COM  * lock to prevent access by other threads while they are being filled.
4236789Sahrens  */
4237789Sahrens static int
4238789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4239789Sahrens     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4240789Sahrens {
4241789Sahrens 	znode_t *zp = VTOZ(vp);
4242789Sahrens 	page_t *pp, *cur_pp;
4243789Sahrens 	objset_t *os = zp->z_zfsvfs->z_os;
4244789Sahrens 	u_offset_t io_off, total;
4245789Sahrens 	size_t io_len;
4246789Sahrens 	int err;
4247789Sahrens 
42482688Smaybee 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
42498636SMark.Maybee@Sun.COM 		/*
42508636SMark.Maybee@Sun.COM 		 * We only have a single page, don't bother klustering
42518636SMark.Maybee@Sun.COM 		 */
4252789Sahrens 		io_off = off;
4253789Sahrens 		io_len = PAGESIZE;
42549265SMark.Maybee@Sun.COM 		pp = page_create_va(vp, io_off, io_len,
42559265SMark.Maybee@Sun.COM 		    PG_EXCL | PG_WAIT, seg, addr);
4256789Sahrens 	} else {
4257789Sahrens 		/*
42588636SMark.Maybee@Sun.COM 		 * Try to find enough pages to fill the page list
4259789Sahrens 		 */
4260789Sahrens 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
42618636SMark.Maybee@Sun.COM 		    &io_len, off, plsz, 0);
4262789Sahrens 	}
4263789Sahrens 	if (pp == NULL) {
4264789Sahrens 		/*
42658636SMark.Maybee@Sun.COM 		 * The page already exists, nothing to do here.
4266789Sahrens 		 */
4267789Sahrens 		*pl = NULL;
4268789Sahrens 		return (0);
4269789Sahrens 	}
4270789Sahrens 
4271789Sahrens 	/*
4272789Sahrens 	 * Fill the pages in the kluster.
4273789Sahrens 	 */
4274789Sahrens 	cur_pp = pp;
4275789Sahrens 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
42768636SMark.Maybee@Sun.COM 		caddr_t va;
42778636SMark.Maybee@Sun.COM 
42782688Smaybee 		ASSERT3U(io_off, ==, cur_pp->p_offset);
42797315SJonathan.Adams@Sun.COM 		va = zfs_map_page(cur_pp, S_WRITE);
42809512SNeil.Perrin@Sun.COM 		err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
42819512SNeil.Perrin@Sun.COM 		    DMU_READ_PREFETCH);
42827315SJonathan.Adams@Sun.COM 		zfs_unmap_page(cur_pp, va);
4283789Sahrens 		if (err) {
4284789Sahrens 			/* On error, toss the entire kluster */
4285789Sahrens 			pvn_read_done(pp, B_ERROR);
42867294Sperrin 			/* convert checksum errors into IO errors */
42877294Sperrin 			if (err == ECKSUM)
42887294Sperrin 				err = EIO;
4289789Sahrens 			return (err);
4290789Sahrens 		}
4291789Sahrens 		cur_pp = cur_pp->p_next;
4292789Sahrens 	}
42938636SMark.Maybee@Sun.COM 
4294789Sahrens 	/*
42958636SMark.Maybee@Sun.COM 	 * Fill in the page list array from the kluster starting
42968636SMark.Maybee@Sun.COM 	 * from the desired offset `off'.
4297789Sahrens 	 * NOTE: the page list will always be null terminated.
4298789Sahrens 	 */
4299789Sahrens 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
43008636SMark.Maybee@Sun.COM 	ASSERT(pl == NULL || (*pl)->p_offset == off);
4301789Sahrens 
4302789Sahrens 	return (0);
4303789Sahrens }
4304789Sahrens 
4305789Sahrens /*
4306789Sahrens  * Return pointers to the pages for the file region [off, off + len]
4307789Sahrens  * in the pl array.  If plsz is greater than len, this function may
43088636SMark.Maybee@Sun.COM  * also return page pointers from after the specified region
43098636SMark.Maybee@Sun.COM  * (i.e. the region [off, off + plsz]).  These additional pages are
43108636SMark.Maybee@Sun.COM  * only returned if they are already in the cache, or were created as
43118636SMark.Maybee@Sun.COM  * part of a klustered read.
4312789Sahrens  *
4313789Sahrens  *	IN:	vp	- vnode of file to get data from.
4314789Sahrens  *		off	- position in file to get data from.
4315789Sahrens  *		len	- amount of data to retrieve.
4316789Sahrens  *		plsz	- length of provided page list.
4317789Sahrens  *		seg	- segment to obtain pages for.
4318789Sahrens  *		addr	- virtual address of fault.
4319789Sahrens  *		rw	- mode of created pages.
4320789Sahrens  *		cr	- credentials of caller.
43215331Samw  *		ct	- caller context.
4322789Sahrens  *
4323789Sahrens  *	OUT:	protp	- protection mode of created pages.
4324789Sahrens  *		pl	- list of pages created.
4325789Sahrens  *
4326789Sahrens  *	RETURN:	0 if success
4327789Sahrens  *		error code if failure
4328789Sahrens  *
4329789Sahrens  * Timestamps:
4330789Sahrens  *	vp - atime updated
4331789Sahrens  */
4332789Sahrens /* ARGSUSED */
4333789Sahrens static int
4334789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4335789Sahrens 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
43365331Samw 	enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4337789Sahrens {
4338789Sahrens 	znode_t		*zp = VTOZ(vp);
4339789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
43408636SMark.Maybee@Sun.COM 	page_t		**pl0 = pl;
43418636SMark.Maybee@Sun.COM 	int		err = 0;
43428636SMark.Maybee@Sun.COM 
43438636SMark.Maybee@Sun.COM 	/* we do our own caching, faultahead is unnecessary */
43448636SMark.Maybee@Sun.COM 	if (pl == NULL)
43458636SMark.Maybee@Sun.COM 		return (0);
43468636SMark.Maybee@Sun.COM 	else if (len > plsz)
43478636SMark.Maybee@Sun.COM 		len = plsz;
43488681SMark.Maybee@Sun.COM 	else
43498681SMark.Maybee@Sun.COM 		len = P2ROUNDUP(len, PAGESIZE);
43508636SMark.Maybee@Sun.COM 	ASSERT(plsz >= len);
4351789Sahrens 
43525367Sahrens 	ZFS_ENTER(zfsvfs);
43535367Sahrens 	ZFS_VERIFY_ZP(zp);
4354789Sahrens 
4355789Sahrens 	if (protp)
4356789Sahrens 		*protp = PROT_ALL;
4357789Sahrens 
4358789Sahrens 	/*
43599265SMark.Maybee@Sun.COM 	 * Loop through the requested range [off, off + len) looking
4360789Sahrens 	 * for pages.  If we don't find a page, we will need to create
4361789Sahrens 	 * a new page and fill it with data from the file.
4362789Sahrens 	 */
4363789Sahrens 	while (len > 0) {
43648636SMark.Maybee@Sun.COM 		if (*pl = page_lookup(vp, off, SE_SHARED))
43658636SMark.Maybee@Sun.COM 			*(pl+1) = NULL;
43668636SMark.Maybee@Sun.COM 		else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
43678636SMark.Maybee@Sun.COM 			goto out;
43688636SMark.Maybee@Sun.COM 		while (*pl) {
43698636SMark.Maybee@Sun.COM 			ASSERT3U((*pl)->p_offset, ==, off);
4370789Sahrens 			off += PAGESIZE;
4371789Sahrens 			addr += PAGESIZE;
43728681SMark.Maybee@Sun.COM 			if (len > 0) {
43738681SMark.Maybee@Sun.COM 				ASSERT3U(len, >=, PAGESIZE);
43748636SMark.Maybee@Sun.COM 				len -= PAGESIZE;
43758681SMark.Maybee@Sun.COM 			}
43768636SMark.Maybee@Sun.COM 			ASSERT3U(plsz, >=, PAGESIZE);
4377789Sahrens 			plsz -= PAGESIZE;
43788636SMark.Maybee@Sun.COM 			pl++;
4379789Sahrens 		}
4380789Sahrens 	}
4381789Sahrens 
4382789Sahrens 	/*
4383789Sahrens 	 * Fill out the page array with any pages already in the cache.
4384789Sahrens 	 */
43858636SMark.Maybee@Sun.COM 	while (plsz > 0 &&
43868636SMark.Maybee@Sun.COM 	    (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
43878636SMark.Maybee@Sun.COM 			off += PAGESIZE;
43888636SMark.Maybee@Sun.COM 			plsz -= PAGESIZE;
4389789Sahrens 	}
4390789Sahrens out:
43912752Sperrin 	if (err) {
43922752Sperrin 		/*
43932752Sperrin 		 * Release any pages we have previously locked.
43942752Sperrin 		 */
43952752Sperrin 		while (pl > pl0)
43962752Sperrin 			page_unlock(*--pl);
43978636SMark.Maybee@Sun.COM 	} else {
43988636SMark.Maybee@Sun.COM 		ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
43992752Sperrin 	}
44002752Sperrin 
4401789Sahrens 	*pl = NULL;
4402789Sahrens 
4403789Sahrens 	ZFS_EXIT(zfsvfs);
4404789Sahrens 	return (err);
4405789Sahrens }
4406789Sahrens 
44071544Seschrock /*
44081544Seschrock  * Request a memory map for a section of a file.  This code interacts
44091544Seschrock  * with common code and the VM system as follows:
44101544Seschrock  *
44111544Seschrock  *	common code calls mmap(), which ends up in smmap_common()
44121544Seschrock  *
44131544Seschrock  *	this calls VOP_MAP(), which takes you into (say) zfs
44141544Seschrock  *
44151544Seschrock  *	zfs_map() calls as_map(), passing segvn_create() as the callback
44161544Seschrock  *
44171544Seschrock  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
44181544Seschrock  *
44191544Seschrock  *	zfs_addmap() updates z_mapcnt
44201544Seschrock  */
44215331Samw /*ARGSUSED*/
4422789Sahrens static int
4423789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
44245331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
44255331Samw     caller_context_t *ct)
4426789Sahrens {
4427789Sahrens 	znode_t *zp = VTOZ(vp);
4428789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4429789Sahrens 	segvn_crargs_t	vn_a;
4430789Sahrens 	int		error;
4431789Sahrens 
44325929Smarks 	ZFS_ENTER(zfsvfs);
44335929Smarks 	ZFS_VERIFY_ZP(zp);
44345929Smarks 
443511935SMark.Shellenbaum@Sun.COM 	if ((prot & PROT_WRITE) && (zp->z_pflags &
443611935SMark.Shellenbaum@Sun.COM 	    (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
44375929Smarks 		ZFS_EXIT(zfsvfs);
44385331Samw 		return (EPERM);
44395929Smarks 	}
44405929Smarks 
44415929Smarks 	if ((prot & (PROT_READ | PROT_EXEC)) &&
444211935SMark.Shellenbaum@Sun.COM 	    (zp->z_pflags & ZFS_AV_QUARANTINED)) {
44435929Smarks 		ZFS_EXIT(zfsvfs);
44445929Smarks 		return (EACCES);
44455929Smarks 	}
4446789Sahrens 
4447789Sahrens 	if (vp->v_flag & VNOMAP) {
4448789Sahrens 		ZFS_EXIT(zfsvfs);
4449789Sahrens 		return (ENOSYS);
4450789Sahrens 	}
4451789Sahrens 
4452789Sahrens 	if (off < 0 || len > MAXOFFSET_T - off) {
4453789Sahrens 		ZFS_EXIT(zfsvfs);
4454789Sahrens 		return (ENXIO);
4455789Sahrens 	}
4456789Sahrens 
4457789Sahrens 	if (vp->v_type != VREG) {
4458789Sahrens 		ZFS_EXIT(zfsvfs);
4459789Sahrens 		return (ENODEV);
4460789Sahrens 	}
4461789Sahrens 
4462789Sahrens 	/*
4463789Sahrens 	 * If file is locked, disallow mapping.
4464789Sahrens 	 */
446511935SMark.Shellenbaum@Sun.COM 	if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
44661544Seschrock 		ZFS_EXIT(zfsvfs);
44671544Seschrock 		return (EAGAIN);
4468789Sahrens 	}
4469789Sahrens 
4470789Sahrens 	as_rangelock(as);
44716036Smec 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
44726036Smec 	if (error != 0) {
44736036Smec 		as_rangeunlock(as);
44746036Smec 		ZFS_EXIT(zfsvfs);
44756036Smec 		return (error);
4476789Sahrens 	}
4477789Sahrens 
4478789Sahrens 	vn_a.vp = vp;
4479789Sahrens 	vn_a.offset = (u_offset_t)off;
4480789Sahrens 	vn_a.type = flags & MAP_TYPE;
4481789Sahrens 	vn_a.prot = prot;
4482789Sahrens 	vn_a.maxprot = maxprot;
4483789Sahrens 	vn_a.cred = cr;
4484789Sahrens 	vn_a.amp = NULL;
4485789Sahrens 	vn_a.flags = flags & ~MAP_TYPE;
44861417Skchow 	vn_a.szc = 0;
44871417Skchow 	vn_a.lgrp_mem_policy_flags = 0;
4488789Sahrens 
4489789Sahrens 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
4490789Sahrens 
4491789Sahrens 	as_rangeunlock(as);
4492789Sahrens 	ZFS_EXIT(zfsvfs);
4493789Sahrens 	return (error);
4494789Sahrens }
4495789Sahrens 
4496789Sahrens /* ARGSUSED */
4497789Sahrens static int
4498789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
44995331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
45005331Samw     caller_context_t *ct)
4501789Sahrens {
45021544Seschrock 	uint64_t pages = btopr(len);
45031544Seschrock 
45041544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4505789Sahrens 	return (0);
4506789Sahrens }
4507789Sahrens 
45081773Seschrock /*
45091773Seschrock  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
45101773Seschrock  * more accurate mtime for the associated file.  Since we don't have a way of
45111773Seschrock  * detecting when the data was actually modified, we have to resort to
45121773Seschrock  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
45131773Seschrock  * last page is pushed.  The problem occurs when the msync() call is omitted,
45141773Seschrock  * which by far the most common case:
45151773Seschrock  *
45161773Seschrock  * 	open()
45171773Seschrock  * 	mmap()
45181773Seschrock  * 	<modify memory>
45191773Seschrock  * 	munmap()
45201773Seschrock  * 	close()
45211773Seschrock  * 	<time lapse>
45221773Seschrock  * 	putpage() via fsflush
45231773Seschrock  *
45241773Seschrock  * If we wait until fsflush to come along, we can have a modification time that
45251773Seschrock  * is some arbitrary point in the future.  In order to prevent this in the
45261773Seschrock  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
45271773Seschrock  * torn down.
45281773Seschrock  */
4529789Sahrens /* ARGSUSED */
4530789Sahrens static int
4531789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
45325331Samw     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
45335331Samw     caller_context_t *ct)
4534789Sahrens {
45351544Seschrock 	uint64_t pages = btopr(len);
45361544Seschrock 
45371544Seschrock 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
45381544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
45391773Seschrock 
45401773Seschrock 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
45411773Seschrock 	    vn_has_cached_data(vp))
45425331Samw 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
45431773Seschrock 
4544789Sahrens 	return (0);
4545789Sahrens }
4546789Sahrens 
4547789Sahrens /*
4548789Sahrens  * Free or allocate space in a file.  Currently, this function only
4549789Sahrens  * supports the `F_FREESP' command.  However, this command is somewhat
4550789Sahrens  * misnamed, as its functionality includes the ability to allocate as
4551789Sahrens  * well as free space.
4552789Sahrens  *
4553789Sahrens  *	IN:	vp	- vnode of file to free data in.
4554789Sahrens  *		cmd	- action to take (only F_FREESP supported).
4555789Sahrens  *		bfp	- section of file to free/alloc.
4556789Sahrens  *		flag	- current file open mode flags.
4557789Sahrens  *		offset	- current file offset.
4558789Sahrens  *		cr	- credentials of caller [UNUSED].
45595331Samw  *		ct	- caller context.
4560789Sahrens  *
4561789Sahrens  *	RETURN:	0 if success
4562789Sahrens  *		error code if failure
4563789Sahrens  *
4564789Sahrens  * Timestamps:
4565789Sahrens  *	vp - ctime|mtime updated
4566789Sahrens  */
4567789Sahrens /* ARGSUSED */
4568789Sahrens static int
4569789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4570789Sahrens     offset_t offset, cred_t *cr, caller_context_t *ct)
4571789Sahrens {
4572789Sahrens 	znode_t		*zp = VTOZ(vp);
4573789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4574789Sahrens 	uint64_t	off, len;
4575789Sahrens 	int		error;
4576789Sahrens 
45775367Sahrens 	ZFS_ENTER(zfsvfs);
45785367Sahrens 	ZFS_VERIFY_ZP(zp);
4579789Sahrens 
4580789Sahrens 	if (cmd != F_FREESP) {
4581789Sahrens 		ZFS_EXIT(zfsvfs);
4582789Sahrens 		return (EINVAL);
4583789Sahrens 	}
4584789Sahrens 
4585789Sahrens 	if (error = convoff(vp, bfp, 0, offset)) {
4586789Sahrens 		ZFS_EXIT(zfsvfs);
4587789Sahrens 		return (error);
4588789Sahrens 	}
4589789Sahrens 
4590789Sahrens 	if (bfp->l_len < 0) {
4591789Sahrens 		ZFS_EXIT(zfsvfs);
4592789Sahrens 		return (EINVAL);
4593789Sahrens 	}
4594789Sahrens 
4595789Sahrens 	off = bfp->l_start;
45961669Sperrin 	len = bfp->l_len; /* 0 means from off to end of file */
45971878Smaybee 
45986992Smaybee 	error = zfs_freesp(zp, off, len, flag, TRUE);
4599789Sahrens 
4600789Sahrens 	ZFS_EXIT(zfsvfs);
4601789Sahrens 	return (error);
4602789Sahrens }
4603789Sahrens 
46045331Samw /*ARGSUSED*/
4605789Sahrens static int
46065331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4607789Sahrens {
4608789Sahrens 	znode_t		*zp = VTOZ(vp);
4609789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
46105326Sek110237 	uint32_t	gen;
461111935SMark.Shellenbaum@Sun.COM 	uint64_t	gen64;
4612789Sahrens 	uint64_t	object = zp->z_id;
4613789Sahrens 	zfid_short_t	*zfid;
461411935SMark.Shellenbaum@Sun.COM 	int		size, i, error;
4615789Sahrens 
46165367Sahrens 	ZFS_ENTER(zfsvfs);
46175367Sahrens 	ZFS_VERIFY_ZP(zp);
461811935SMark.Shellenbaum@Sun.COM 
461911935SMark.Shellenbaum@Sun.COM 	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
462011935SMark.Shellenbaum@Sun.COM 	    &gen64, sizeof (uint64_t))) != 0)
462111935SMark.Shellenbaum@Sun.COM 		return (error);
462211935SMark.Shellenbaum@Sun.COM 
462311935SMark.Shellenbaum@Sun.COM 	gen = (uint32_t)gen64;
4624789Sahrens 
4625789Sahrens 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4626789Sahrens 	if (fidp->fid_len < size) {
4627789Sahrens 		fidp->fid_len = size;
46281512Sek110237 		ZFS_EXIT(zfsvfs);
4629789Sahrens 		return (ENOSPC);
4630789Sahrens 	}
4631789Sahrens 
4632789Sahrens 	zfid = (zfid_short_t *)fidp;
4633789Sahrens 
4634789Sahrens 	zfid->zf_len = size;
4635789Sahrens 
4636789Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
4637789Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4638789Sahrens 
4639789Sahrens 	/* Must have a non-zero generation number to distinguish from .zfs */
4640789Sahrens 	if (gen == 0)
4641789Sahrens 		gen = 1;
4642789Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
4643789Sahrens 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4644789Sahrens 
4645789Sahrens 	if (size == LONG_FID_LEN) {
4646789Sahrens 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
4647789Sahrens 		zfid_long_t	*zlfid;
4648789Sahrens 
4649789Sahrens 		zlfid = (zfid_long_t *)fidp;
4650789Sahrens 
4651789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4652789Sahrens 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4653789Sahrens 
4654789Sahrens 		/* XXX - this should be the generation number for the objset */
4655789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4656789Sahrens 			zlfid->zf_setgen[i] = 0;
4657789Sahrens 	}
4658789Sahrens 
4659789Sahrens 	ZFS_EXIT(zfsvfs);
4660789Sahrens 	return (0);
4661789Sahrens }
4662789Sahrens 
4663789Sahrens static int
46645331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
46655331Samw     caller_context_t *ct)
4666789Sahrens {
4667789Sahrens 	znode_t		*zp, *xzp;
4668789Sahrens 	zfsvfs_t	*zfsvfs;
4669789Sahrens 	zfs_dirlock_t	*dl;
4670789Sahrens 	int		error;
4671789Sahrens 
4672789Sahrens 	switch (cmd) {
4673789Sahrens 	case _PC_LINK_MAX:
4674789Sahrens 		*valp = ULONG_MAX;
4675789Sahrens 		return (0);
4676789Sahrens 
4677789Sahrens 	case _PC_FILESIZEBITS:
4678789Sahrens 		*valp = 64;
4679789Sahrens 		return (0);
4680789Sahrens 
4681789Sahrens 	case _PC_XATTR_EXISTS:
4682789Sahrens 		zp = VTOZ(vp);
4683789Sahrens 		zfsvfs = zp->z_zfsvfs;
46845367Sahrens 		ZFS_ENTER(zfsvfs);
46855367Sahrens 		ZFS_VERIFY_ZP(zp);
4686789Sahrens 		*valp = 0;
4687789Sahrens 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
46885331Samw 		    ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4689789Sahrens 		if (error == 0) {
4690789Sahrens 			zfs_dirent_unlock(dl);
4691789Sahrens 			if (!zfs_dirempty(xzp))
4692789Sahrens 				*valp = 1;
4693789Sahrens 			VN_RELE(ZTOV(xzp));
4694789Sahrens 		} else if (error == ENOENT) {
4695789Sahrens 			/*
4696789Sahrens 			 * If there aren't extended attributes, it's the
4697789Sahrens 			 * same as having zero of them.
4698789Sahrens 			 */
4699789Sahrens 			error = 0;
4700789Sahrens 		}
4701789Sahrens 		ZFS_EXIT(zfsvfs);
4702789Sahrens 		return (error);
4703789Sahrens 
47045331Samw 	case _PC_SATTR_ENABLED:
47055331Samw 	case _PC_SATTR_EXISTS:
47067757SJanice.Chang@Sun.COM 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
47075331Samw 		    (vp->v_type == VREG || vp->v_type == VDIR);
47085331Samw 		return (0);
47095331Samw 
47109749STim.Haley@Sun.COM 	case _PC_ACCESS_FILTERING:
47119749STim.Haley@Sun.COM 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
47129749STim.Haley@Sun.COM 		    vp->v_type == VDIR;
47139749STim.Haley@Sun.COM 		return (0);
47149749STim.Haley@Sun.COM 
4715789Sahrens 	case _PC_ACL_ENABLED:
4716789Sahrens 		*valp = _ACL_ACE_ENABLED;
4717789Sahrens 		return (0);
4718789Sahrens 
4719789Sahrens 	case _PC_MIN_HOLE_SIZE:
4720789Sahrens 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
4721789Sahrens 		return (0);
4722789Sahrens 
472310440SRoger.Faulkner@Sun.COM 	case _PC_TIMESTAMP_RESOLUTION:
472410440SRoger.Faulkner@Sun.COM 		/* nanosecond timestamp resolution */
472510440SRoger.Faulkner@Sun.COM 		*valp = 1L;
472610440SRoger.Faulkner@Sun.COM 		return (0);
472710440SRoger.Faulkner@Sun.COM 
4728789Sahrens 	default:
47295331Samw 		return (fs_pathconf(vp, cmd, valp, cr, ct));
4730789Sahrens 	}
4731789Sahrens }
4732789Sahrens 
4733789Sahrens /*ARGSUSED*/
4734789Sahrens static int
47355331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
47365331Samw     caller_context_t *ct)
4737789Sahrens {
4738789Sahrens 	znode_t *zp = VTOZ(vp);
4739789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4740789Sahrens 	int error;
47415331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4742789Sahrens 
47435367Sahrens 	ZFS_ENTER(zfsvfs);
47445367Sahrens 	ZFS_VERIFY_ZP(zp);
47455331Samw 	error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4746789Sahrens 	ZFS_EXIT(zfsvfs);
4747789Sahrens 
4748789Sahrens 	return (error);
4749789Sahrens }
4750789Sahrens 
4751789Sahrens /*ARGSUSED*/
4752789Sahrens static int
47535331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
47545331Samw     caller_context_t *ct)
4755789Sahrens {
4756789Sahrens 	znode_t *zp = VTOZ(vp);
4757789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4758789Sahrens 	int error;
47595331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4760789Sahrens 
47615367Sahrens 	ZFS_ENTER(zfsvfs);
47625367Sahrens 	ZFS_VERIFY_ZP(zp);
47635331Samw 	error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4764789Sahrens 	ZFS_EXIT(zfsvfs);
4765789Sahrens 	return (error);
4766789Sahrens }
4767789Sahrens 
4768789Sahrens /*
476911539SChunli.Zhang@Sun.COM  * Tunable, both must be a power of 2.
477011539SChunli.Zhang@Sun.COM  *
477111539SChunli.Zhang@Sun.COM  * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
477211539SChunli.Zhang@Sun.COM  * zcr_blksz_max: if set to less than the file block size, allow loaning out of
477311539SChunli.Zhang@Sun.COM  *                an arcbuf for a partial block read
477411539SChunli.Zhang@Sun.COM  */
477511539SChunli.Zhang@Sun.COM int zcr_blksz_min = (1 << 10);	/* 1K */
477611539SChunli.Zhang@Sun.COM int zcr_blksz_max = (1 << 17);	/* 128K */
477711539SChunli.Zhang@Sun.COM 
477811539SChunli.Zhang@Sun.COM /*ARGSUSED*/
477911539SChunli.Zhang@Sun.COM static int
478011539SChunli.Zhang@Sun.COM zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
478111539SChunli.Zhang@Sun.COM     caller_context_t *ct)
478211539SChunli.Zhang@Sun.COM {
478311539SChunli.Zhang@Sun.COM 	znode_t	*zp = VTOZ(vp);
478411539SChunli.Zhang@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
478511539SChunli.Zhang@Sun.COM 	int max_blksz = zfsvfs->z_max_blksz;
478611539SChunli.Zhang@Sun.COM 	uio_t *uio = &xuio->xu_uio;
478711539SChunli.Zhang@Sun.COM 	ssize_t size = uio->uio_resid;
478811539SChunli.Zhang@Sun.COM 	offset_t offset = uio->uio_loffset;
478911539SChunli.Zhang@Sun.COM 	int blksz;
479011539SChunli.Zhang@Sun.COM 	int fullblk, i;
479111539SChunli.Zhang@Sun.COM 	arc_buf_t *abuf;
479211539SChunli.Zhang@Sun.COM 	ssize_t maxsize;
479311539SChunli.Zhang@Sun.COM 	int preamble, postamble;
479411539SChunli.Zhang@Sun.COM 
479511539SChunli.Zhang@Sun.COM 	if (xuio->xu_type != UIOTYPE_ZEROCOPY)
479611539SChunli.Zhang@Sun.COM 		return (EINVAL);
479711539SChunli.Zhang@Sun.COM 
479811539SChunli.Zhang@Sun.COM 	ZFS_ENTER(zfsvfs);
479911539SChunli.Zhang@Sun.COM 	ZFS_VERIFY_ZP(zp);
480011539SChunli.Zhang@Sun.COM 	switch (ioflag) {
480111539SChunli.Zhang@Sun.COM 	case UIO_WRITE:
480211539SChunli.Zhang@Sun.COM 		/*
480311539SChunli.Zhang@Sun.COM 		 * Loan out an arc_buf for write if write size is bigger than
480411539SChunli.Zhang@Sun.COM 		 * max_blksz, and the file's block size is also max_blksz.
480511539SChunli.Zhang@Sun.COM 		 */
480611539SChunli.Zhang@Sun.COM 		blksz = max_blksz;
480711539SChunli.Zhang@Sun.COM 		if (size < blksz || zp->z_blksz != blksz) {
480811539SChunli.Zhang@Sun.COM 			ZFS_EXIT(zfsvfs);
480911539SChunli.Zhang@Sun.COM 			return (EINVAL);
481011539SChunli.Zhang@Sun.COM 		}
481111539SChunli.Zhang@Sun.COM 		/*
481211539SChunli.Zhang@Sun.COM 		 * Caller requests buffers for write before knowing where the
481311539SChunli.Zhang@Sun.COM 		 * write offset might be (e.g. NFS TCP write).
481411539SChunli.Zhang@Sun.COM 		 */
481511539SChunli.Zhang@Sun.COM 		if (offset == -1) {
481611539SChunli.Zhang@Sun.COM 			preamble = 0;
481711539SChunli.Zhang@Sun.COM 		} else {
481811539SChunli.Zhang@Sun.COM 			preamble = P2PHASE(offset, blksz);
481911539SChunli.Zhang@Sun.COM 			if (preamble) {
482011539SChunli.Zhang@Sun.COM 				preamble = blksz - preamble;
482111539SChunli.Zhang@Sun.COM 				size -= preamble;
482211539SChunli.Zhang@Sun.COM 			}
482311539SChunli.Zhang@Sun.COM 		}
482411539SChunli.Zhang@Sun.COM 
482511539SChunli.Zhang@Sun.COM 		postamble = P2PHASE(size, blksz);
482611539SChunli.Zhang@Sun.COM 		size -= postamble;
482711539SChunli.Zhang@Sun.COM 
482811539SChunli.Zhang@Sun.COM 		fullblk = size / blksz;
482911576SSurya.Prakki@Sun.COM 		(void) dmu_xuio_init(xuio,
483011539SChunli.Zhang@Sun.COM 		    (preamble != 0) + fullblk + (postamble != 0));
483111539SChunli.Zhang@Sun.COM 		DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
483211539SChunli.Zhang@Sun.COM 		    int, postamble, int,
483311539SChunli.Zhang@Sun.COM 		    (preamble != 0) + fullblk + (postamble != 0));
483411539SChunli.Zhang@Sun.COM 
483511539SChunli.Zhang@Sun.COM 		/*
483611539SChunli.Zhang@Sun.COM 		 * Have to fix iov base/len for partial buffers.  They
483711539SChunli.Zhang@Sun.COM 		 * currently represent full arc_buf's.
483811539SChunli.Zhang@Sun.COM 		 */
483911539SChunli.Zhang@Sun.COM 		if (preamble) {
484011539SChunli.Zhang@Sun.COM 			/* data begins in the middle of the arc_buf */
484111935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
484211935SMark.Shellenbaum@Sun.COM 			    blksz);
484311539SChunli.Zhang@Sun.COM 			ASSERT(abuf);
484411576SSurya.Prakki@Sun.COM 			(void) dmu_xuio_add(xuio, abuf,
484511576SSurya.Prakki@Sun.COM 			    blksz - preamble, preamble);
484611539SChunli.Zhang@Sun.COM 		}
484711539SChunli.Zhang@Sun.COM 
484811539SChunli.Zhang@Sun.COM 		for (i = 0; i < fullblk; i++) {
484911935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
485011935SMark.Shellenbaum@Sun.COM 			    blksz);
485111539SChunli.Zhang@Sun.COM 			ASSERT(abuf);
485211576SSurya.Prakki@Sun.COM 			(void) dmu_xuio_add(xuio, abuf, 0, blksz);
485311539SChunli.Zhang@Sun.COM 		}
485411539SChunli.Zhang@Sun.COM 
485511539SChunli.Zhang@Sun.COM 		if (postamble) {
485611539SChunli.Zhang@Sun.COM 			/* data ends in the middle of the arc_buf */
485711935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
485811935SMark.Shellenbaum@Sun.COM 			    blksz);
485911539SChunli.Zhang@Sun.COM 			ASSERT(abuf);
486011576SSurya.Prakki@Sun.COM 			(void) dmu_xuio_add(xuio, abuf, 0, postamble);
486111539SChunli.Zhang@Sun.COM 		}
486211539SChunli.Zhang@Sun.COM 		break;
486311539SChunli.Zhang@Sun.COM 	case UIO_READ:
486411539SChunli.Zhang@Sun.COM 		/*
486511539SChunli.Zhang@Sun.COM 		 * Loan out an arc_buf for read if the read size is larger than
486611539SChunli.Zhang@Sun.COM 		 * the current file block size.  Block alignment is not
486711539SChunli.Zhang@Sun.COM 		 * considered.  Partial arc_buf will be loaned out for read.
486811539SChunli.Zhang@Sun.COM 		 */
486911539SChunli.Zhang@Sun.COM 		blksz = zp->z_blksz;
487011539SChunli.Zhang@Sun.COM 		if (blksz < zcr_blksz_min)
487111539SChunli.Zhang@Sun.COM 			blksz = zcr_blksz_min;
487211539SChunli.Zhang@Sun.COM 		if (blksz > zcr_blksz_max)
487311539SChunli.Zhang@Sun.COM 			blksz = zcr_blksz_max;
487411539SChunli.Zhang@Sun.COM 		/* avoid potential complexity of dealing with it */
487511539SChunli.Zhang@Sun.COM 		if (blksz > max_blksz) {
487611539SChunli.Zhang@Sun.COM 			ZFS_EXIT(zfsvfs);
487711539SChunli.Zhang@Sun.COM 			return (EINVAL);
487811539SChunli.Zhang@Sun.COM 		}
487911539SChunli.Zhang@Sun.COM 
488011935SMark.Shellenbaum@Sun.COM 		maxsize = zp->z_size - uio->uio_loffset;
488111539SChunli.Zhang@Sun.COM 		if (size > maxsize)
488211539SChunli.Zhang@Sun.COM 			size = maxsize;
488311539SChunli.Zhang@Sun.COM 
488411539SChunli.Zhang@Sun.COM 		if (size < blksz || vn_has_cached_data(vp)) {
488511539SChunli.Zhang@Sun.COM 			ZFS_EXIT(zfsvfs);
488611539SChunli.Zhang@Sun.COM 			return (EINVAL);
488711539SChunli.Zhang@Sun.COM 		}
488811539SChunli.Zhang@Sun.COM 		break;
488911539SChunli.Zhang@Sun.COM 	default:
489011539SChunli.Zhang@Sun.COM 		ZFS_EXIT(zfsvfs);
489111539SChunli.Zhang@Sun.COM 		return (EINVAL);
489211539SChunli.Zhang@Sun.COM 	}
489311539SChunli.Zhang@Sun.COM 
489411539SChunli.Zhang@Sun.COM 	uio->uio_extflg = UIO_XUIO;
489511539SChunli.Zhang@Sun.COM 	XUIO_XUZC_RW(xuio) = ioflag;
489611539SChunli.Zhang@Sun.COM 	ZFS_EXIT(zfsvfs);
489711539SChunli.Zhang@Sun.COM 	return (0);
489811539SChunli.Zhang@Sun.COM }
489911539SChunli.Zhang@Sun.COM 
490011539SChunli.Zhang@Sun.COM /*ARGSUSED*/
490111539SChunli.Zhang@Sun.COM static int
490211539SChunli.Zhang@Sun.COM zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
490311539SChunli.Zhang@Sun.COM {
490411539SChunli.Zhang@Sun.COM 	int i;
490511539SChunli.Zhang@Sun.COM 	arc_buf_t *abuf;
490611539SChunli.Zhang@Sun.COM 	int ioflag = XUIO_XUZC_RW(xuio);
490711539SChunli.Zhang@Sun.COM 
490811539SChunli.Zhang@Sun.COM 	ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
490911539SChunli.Zhang@Sun.COM 
491011539SChunli.Zhang@Sun.COM 	i = dmu_xuio_cnt(xuio);
491111539SChunli.Zhang@Sun.COM 	while (i-- > 0) {
491211539SChunli.Zhang@Sun.COM 		abuf = dmu_xuio_arcbuf(xuio, i);
491311539SChunli.Zhang@Sun.COM 		/*
491411539SChunli.Zhang@Sun.COM 		 * if abuf == NULL, it must be a write buffer
491511539SChunli.Zhang@Sun.COM 		 * that has been returned in zfs_write().
491611539SChunli.Zhang@Sun.COM 		 */
491711539SChunli.Zhang@Sun.COM 		if (abuf)
491811539SChunli.Zhang@Sun.COM 			dmu_return_arcbuf(abuf);
491911539SChunli.Zhang@Sun.COM 		ASSERT(abuf || ioflag == UIO_WRITE);
492011539SChunli.Zhang@Sun.COM 	}
492111539SChunli.Zhang@Sun.COM 
492211539SChunli.Zhang@Sun.COM 	dmu_xuio_fini(xuio);
492311539SChunli.Zhang@Sun.COM 	return (0);
492411539SChunli.Zhang@Sun.COM }
492511539SChunli.Zhang@Sun.COM 
492611539SChunli.Zhang@Sun.COM /*
4927789Sahrens  * Predeclare these here so that the compiler assumes that
4928789Sahrens  * this is an "old style" function declaration that does
4929789Sahrens  * not include arguments => we won't get type mismatch errors
4930789Sahrens  * in the initializations that follow.
4931789Sahrens  */
4932789Sahrens static int zfs_inval();
4933789Sahrens static int zfs_isdir();
4934789Sahrens 
4935789Sahrens static int
4936789Sahrens zfs_inval()
4937789Sahrens {
4938789Sahrens 	return (EINVAL);
4939789Sahrens }
4940789Sahrens 
4941789Sahrens static int
4942789Sahrens zfs_isdir()
4943789Sahrens {
4944789Sahrens 	return (EISDIR);
4945789Sahrens }
4946789Sahrens /*
4947789Sahrens  * Directory vnode operations template
4948789Sahrens  */
4949789Sahrens vnodeops_t *zfs_dvnodeops;
4950789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = {
49513898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
49523898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
49533898Srsb 	VOPNAME_READ,		{ .error = zfs_isdir },
49543898Srsb 	VOPNAME_WRITE,		{ .error = zfs_isdir },
49553898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
49563898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
49573898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
49583898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
49593898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
49603898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
49613898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
49623898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
49633898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
49643898Srsb 	VOPNAME_MKDIR,		{ .vop_mkdir = zfs_mkdir },
49653898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
49663898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
49673898Srsb 	VOPNAME_SYMLINK,	{ .vop_symlink = zfs_symlink },
49683898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
49693898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
49703898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
49713898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
49723898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
49733898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
49743898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
49754863Spraks 	VOPNAME_VNEVENT, 	{ .vop_vnevent = fs_vnevent_support },
49763898Srsb 	NULL,			NULL
4977789Sahrens };
4978789Sahrens 
4979789Sahrens /*
4980789Sahrens  * Regular file vnode operations template
4981789Sahrens  */
4982789Sahrens vnodeops_t *zfs_fvnodeops;
4983789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = {
49843898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
49853898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
49863898Srsb 	VOPNAME_READ,		{ .vop_read = zfs_read },
49873898Srsb 	VOPNAME_WRITE,		{ .vop_write = zfs_write },
49883898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
49893898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
49903898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
49913898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
49923898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
49933898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
49943898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
49953898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
49963898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
49973898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
49983898Srsb 	VOPNAME_FRLOCK,		{ .vop_frlock = zfs_frlock },
49993898Srsb 	VOPNAME_SPACE,		{ .vop_space = zfs_space },
50003898Srsb 	VOPNAME_GETPAGE,	{ .vop_getpage = zfs_getpage },
50013898Srsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = zfs_putpage },
50023898Srsb 	VOPNAME_MAP,		{ .vop_map = zfs_map },
50033898Srsb 	VOPNAME_ADDMAP,		{ .vop_addmap = zfs_addmap },
50043898Srsb 	VOPNAME_DELMAP,		{ .vop_delmap = zfs_delmap },
50053898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50063898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
50073898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
50083898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
500911539SChunli.Zhang@Sun.COM 	VOPNAME_REQZCBUF, 	{ .vop_reqzcbuf = zfs_reqzcbuf },
501011539SChunli.Zhang@Sun.COM 	VOPNAME_RETZCBUF, 	{ .vop_retzcbuf = zfs_retzcbuf },
50113898Srsb 	NULL,			NULL
5012789Sahrens };
5013789Sahrens 
5014789Sahrens /*
5015789Sahrens  * Symbolic link vnode operations template
5016789Sahrens  */
5017789Sahrens vnodeops_t *zfs_symvnodeops;
5018789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = {
50193898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
50203898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
50213898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
50223898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
50233898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = zfs_readlink },
50243898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
50253898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
50263898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50273898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
50283898Srsb 	NULL,			NULL
5029789Sahrens };
5030789Sahrens 
5031789Sahrens /*
50328845Samw@Sun.COM  * special share hidden files vnode operations template
50338845Samw@Sun.COM  */
50348845Samw@Sun.COM vnodeops_t *zfs_sharevnodeops;
50358845Samw@Sun.COM const fs_operation_def_t zfs_sharevnodeops_template[] = {
50368845Samw@Sun.COM 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
50378845Samw@Sun.COM 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
50388845Samw@Sun.COM 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
50398845Samw@Sun.COM 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
50408845Samw@Sun.COM 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50418845Samw@Sun.COM 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
50428845Samw@Sun.COM 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
50438845Samw@Sun.COM 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
50448845Samw@Sun.COM 	NULL,			NULL
50458845Samw@Sun.COM };
50468845Samw@Sun.COM 
50478845Samw@Sun.COM /*
5048789Sahrens  * Extended attribute directory vnode operations template
5049789Sahrens  *	This template is identical to the directory vnodes
5050789Sahrens  *	operation template except for restricted operations:
5051789Sahrens  *		VOP_MKDIR()
5052789Sahrens  *		VOP_SYMLINK()
5053789Sahrens  * Note that there are other restrictions embedded in:
5054789Sahrens  *	zfs_create()	- restrict type to VREG
5055789Sahrens  *	zfs_link()	- no links into/out of attribute space
5056789Sahrens  *	zfs_rename()	- no moves into/out of attribute space
5057789Sahrens  */
5058789Sahrens vnodeops_t *zfs_xdvnodeops;
5059789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = {
50603898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
50613898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
50623898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
50633898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
50643898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
50653898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
50663898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
50673898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
50683898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
50693898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
50703898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
50713898Srsb 	VOPNAME_MKDIR,		{ .error = zfs_inval },
50723898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
50733898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
50743898Srsb 	VOPNAME_SYMLINK,	{ .error = zfs_inval },
50753898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
50763898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
50773898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
50783898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
50793898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50803898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
50813898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
50823898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
50833898Srsb 	NULL,			NULL
5084789Sahrens };
5085789Sahrens 
5086789Sahrens /*
5087789Sahrens  * Error vnode operations template
5088789Sahrens  */
5089789Sahrens vnodeops_t *zfs_evnodeops;
5090789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = {
50913898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
50923898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50933898Srsb 	NULL,			NULL
5094789Sahrens };
5095