xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 12294:2a74b443e6b1)
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 */
26*12294SMark.Musante@Sun.COM /* Portions Copyright 2010 Robert Milkowski */
274144Speteh 
28789Sahrens #include <sys/types.h>
29789Sahrens #include <sys/param.h>
30789Sahrens #include <sys/time.h>
31789Sahrens #include <sys/systm.h>
32789Sahrens #include <sys/sysmacros.h>
33789Sahrens #include <sys/resource.h>
34789Sahrens #include <sys/vfs.h>
353898Srsb #include <sys/vfs_opreg.h>
36789Sahrens #include <sys/vnode.h>
37789Sahrens #include <sys/file.h>
38789Sahrens #include <sys/stat.h>
39789Sahrens #include <sys/kmem.h>
40789Sahrens #include <sys/taskq.h>
41789Sahrens #include <sys/uio.h>
42789Sahrens #include <sys/vmsystm.h>
43789Sahrens #include <sys/atomic.h>
442688Smaybee #include <sys/vm.h>
45789Sahrens #include <vm/seg_vn.h>
46789Sahrens #include <vm/pvn.h>
47789Sahrens #include <vm/as.h>
487315SJonathan.Adams@Sun.COM #include <vm/kpm.h>
497315SJonathan.Adams@Sun.COM #include <vm/seg_kpm.h>
50789Sahrens #include <sys/mman.h>
51789Sahrens #include <sys/pathname.h>
52789Sahrens #include <sys/cmn_err.h>
53789Sahrens #include <sys/errno.h>
54789Sahrens #include <sys/unistd.h>
55789Sahrens #include <sys/zfs_dir.h>
56789Sahrens #include <sys/zfs_acl.h>
57789Sahrens #include <sys/zfs_ioctl.h>
58789Sahrens #include <sys/fs/zfs.h>
59789Sahrens #include <sys/dmu.h>
60*12294SMark.Musante@Sun.COM #include <sys/dmu_objset.h>
61789Sahrens #include <sys/spa.h>
62789Sahrens #include <sys/txg.h>
63789Sahrens #include <sys/dbuf.h>
64789Sahrens #include <sys/zap.h>
6511935SMark.Shellenbaum@Sun.COM #include <sys/sa.h>
66789Sahrens #include <sys/dirent.h>
67789Sahrens #include <sys/policy.h>
68789Sahrens #include <sys/sunddi.h>
69789Sahrens #include <sys/filio.h>
707847SMark.Shellenbaum@Sun.COM #include <sys/sid.h>
71789Sahrens #include "fs/fs_subr.h"
72789Sahrens #include <sys/zfs_ctldir.h>
735331Samw #include <sys/zfs_fuid.h>
7411935SMark.Shellenbaum@Sun.COM #include <sys/zfs_sa.h>
751484Sek110237 #include <sys/dnlc.h>
761669Sperrin #include <sys/zfs_rlock.h>
775331Samw #include <sys/extdirent.h>
785331Samw #include <sys/kidmap.h>
7911134SCasper.Dik@Sun.COM #include <sys/cred.h>
805663Sck153898 #include <sys/attr.h>
81789Sahrens 
82789Sahrens /*
83789Sahrens  * Programming rules.
84789Sahrens  *
85789Sahrens  * Each vnode op performs some logical unit of work.  To do this, the ZPL must
86789Sahrens  * properly lock its in-core state, create a DMU transaction, do the work,
87789Sahrens  * record this work in the intent log (ZIL), commit the DMU transaction,
885331Samw  * and wait for the intent log to commit if it is a synchronous operation.
895331Samw  * Moreover, the vnode ops must work in both normal and log replay context.
90789Sahrens  * The ordering of events is important to avoid deadlocks and references
91789Sahrens  * to freed memory.  The example below illustrates the following Big Rules:
92789Sahrens  *
93789Sahrens  *  (1) A check must be made in each zfs thread for a mounted file system.
945367Sahrens  *	This is done avoiding races using ZFS_ENTER(zfsvfs).
955367Sahrens  *      A ZFS_EXIT(zfsvfs) is needed before all returns.  Any znodes
965367Sahrens  *      must be checked with ZFS_VERIFY_ZP(zp).  Both of these macros
975367Sahrens  *      can return EIO from the calling function.
98789Sahrens  *
99789Sahrens  *  (2)	VN_RELE() should always be the last thing except for zil_commit()
1002638Sperrin  *	(if necessary) and ZFS_EXIT(). This is for 3 reasons:
101789Sahrens  *	First, if it's the last reference, the vnode/znode
102789Sahrens  *	can be freed, so the zp may point to freed memory.  Second, the last
103789Sahrens  *	reference will call zfs_zinactive(), which may induce a lot of work --
1041669Sperrin  *	pushing cached pages (which acquires range locks) and syncing out
105789Sahrens  *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
106789Sahrens  *	which could deadlock the system if you were already holding one.
1079321SNeil.Perrin@Sun.COM  *	If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
108789Sahrens  *
1091757Sperrin  *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
1101757Sperrin  *	as they can span dmu_tx_assign() calls.
1111757Sperrin  *
1128227SNeil.Perrin@Sun.COM  *  (4)	Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
113789Sahrens  *	This is critical because we don't want to block while holding locks.
114789Sahrens  *	Note, in particular, that if a lock is sometimes acquired before
115789Sahrens  *	the tx assigns, and sometimes after (e.g. z_lock), then failing to
116789Sahrens  *	use a non-blocking assign can deadlock the system.  The scenario:
117789Sahrens  *
118789Sahrens  *	Thread A has grabbed a lock before calling dmu_tx_assign().
119789Sahrens  *	Thread B is in an already-assigned tx, and blocks for this lock.
120789Sahrens  *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
121789Sahrens  *	forever, because the previous txg can't quiesce until B's tx commits.
122789Sahrens  *
123789Sahrens  *	If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
1242113Sahrens  *	then drop all locks, call dmu_tx_wait(), and try again.
125789Sahrens  *
1261757Sperrin  *  (5)	If the operation succeeded, generate the intent log entry for it
127789Sahrens  *	before dropping locks.  This ensures that the ordering of events
128789Sahrens  *	in the intent log matches the order in which they actually occurred.
1298227SNeil.Perrin@Sun.COM  *      During ZIL replay the zfs_log_* functions will update the sequence
1308227SNeil.Perrin@Sun.COM  *	number to indicate the zil transaction has replayed.
131789Sahrens  *
1321757Sperrin  *  (6)	At the end of each vnode op, the DMU tx must always commit,
133789Sahrens  *	regardless of whether there were any errors.
134789Sahrens  *
1352638Sperrin  *  (7)	After dropping all locks, invoke zil_commit(zilog, seq, foid)
136789Sahrens  *	to ensure that synchronous semantics are provided when necessary.
137789Sahrens  *
138789Sahrens  * In general, this is how things should be ordered in each vnode op:
139789Sahrens  *
140789Sahrens  *	ZFS_ENTER(zfsvfs);		// exit if unmounted
141789Sahrens  * top:
142789Sahrens  *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
143789Sahrens  *	rw_enter(...);			// grab any other locks you need
144789Sahrens  *	tx = dmu_tx_create(...);	// get DMU tx
145789Sahrens  *	dmu_tx_hold_*();		// hold each object you might modify
1468227SNeil.Perrin@Sun.COM  *	error = dmu_tx_assign(tx, TXG_NOWAIT);	// try to assign
147789Sahrens  *	if (error) {
148789Sahrens  *		rw_exit(...);		// drop locks
149789Sahrens  *		zfs_dirent_unlock(dl);	// unlock directory entry
150789Sahrens  *		VN_RELE(...);		// release held vnodes
1518227SNeil.Perrin@Sun.COM  *		if (error == ERESTART) {
1522113Sahrens  *			dmu_tx_wait(tx);
1532113Sahrens  *			dmu_tx_abort(tx);
154789Sahrens  *			goto top;
155789Sahrens  *		}
1562113Sahrens  *		dmu_tx_abort(tx);	// abort DMU tx
157789Sahrens  *		ZFS_EXIT(zfsvfs);	// finished in zfs
158789Sahrens  *		return (error);		// really out of space
159789Sahrens  *	}
160789Sahrens  *	error = do_real_work();		// do whatever this VOP does
161789Sahrens  *	if (error == 0)
1622638Sperrin  *		zfs_log_*(...);		// on success, make ZIL entry
163789Sahrens  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
164789Sahrens  *	rw_exit(...);			// drop locks
165789Sahrens  *	zfs_dirent_unlock(dl);		// unlock directory entry
166789Sahrens  *	VN_RELE(...);			// release held vnodes
1672638Sperrin  *	zil_commit(zilog, seq, foid);	// synchronous when necessary
168789Sahrens  *	ZFS_EXIT(zfsvfs);		// finished in zfs
169789Sahrens  *	return (error);			// done, report error
170789Sahrens  */
1715367Sahrens 
172789Sahrens /* ARGSUSED */
173789Sahrens static int
1745331Samw zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
175789Sahrens {
1763063Sperrin 	znode_t	*zp = VTOZ(*vpp);
1777844SMark.Shellenbaum@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1787844SMark.Shellenbaum@Sun.COM 
1797844SMark.Shellenbaum@Sun.COM 	ZFS_ENTER(zfsvfs);
1807844SMark.Shellenbaum@Sun.COM 	ZFS_VERIFY_ZP(zp);
1813063Sperrin 
18211935SMark.Shellenbaum@Sun.COM 	if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
1835331Samw 	    ((flag & FAPPEND) == 0)) {
1847844SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
1855331Samw 		return (EPERM);
1865331Samw 	}
1875331Samw 
1885331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
1895331Samw 	    ZTOV(zp)->v_type == VREG &&
19011935SMark.Shellenbaum@Sun.COM 	    !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
1917844SMark.Shellenbaum@Sun.COM 		if (fs_vscan(*vpp, cr, 0) != 0) {
1927844SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
1935331Samw 			return (EACCES);
1947844SMark.Shellenbaum@Sun.COM 		}
1957844SMark.Shellenbaum@Sun.COM 	}
1965331Samw 
1973063Sperrin 	/* Keep a count of the synchronous opens in the znode */
1983063Sperrin 	if (flag & (FSYNC | FDSYNC))
1993063Sperrin 		atomic_inc_32(&zp->z_sync_cnt);
2005331Samw 
2017844SMark.Shellenbaum@Sun.COM 	ZFS_EXIT(zfsvfs);
202789Sahrens 	return (0);
203789Sahrens }
204789Sahrens 
205789Sahrens /* ARGSUSED */
206789Sahrens static int
2075331Samw zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
2085331Samw     caller_context_t *ct)
209789Sahrens {
2103063Sperrin 	znode_t	*zp = VTOZ(vp);
2117844SMark.Shellenbaum@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2127844SMark.Shellenbaum@Sun.COM 
2139909Schris.kirby@sun.com 	/*
2149909Schris.kirby@sun.com 	 * Clean up any locks held by this process on the vp.
2159909Schris.kirby@sun.com 	 */
2169909Schris.kirby@sun.com 	cleanlocks(vp, ddi_get_pid(), 0);
2179909Schris.kirby@sun.com 	cleanshares(vp, ddi_get_pid());
2189909Schris.kirby@sun.com 
2197844SMark.Shellenbaum@Sun.COM 	ZFS_ENTER(zfsvfs);
2207844SMark.Shellenbaum@Sun.COM 	ZFS_VERIFY_ZP(zp);
2213063Sperrin 
2223063Sperrin 	/* Decrement the synchronous opens in the znode */
2234339Sperrin 	if ((flag & (FSYNC | FDSYNC)) && (count == 1))
2243063Sperrin 		atomic_dec_32(&zp->z_sync_cnt);
2253063Sperrin 
2265331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
2275331Samw 	    ZTOV(zp)->v_type == VREG &&
22811935SMark.Shellenbaum@Sun.COM 	    !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
2295331Samw 		VERIFY(fs_vscan(vp, cr, 1) == 0);
2305331Samw 
2317844SMark.Shellenbaum@Sun.COM 	ZFS_EXIT(zfsvfs);
232789Sahrens 	return (0);
233789Sahrens }
234789Sahrens 
235789Sahrens /*
236789Sahrens  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
237789Sahrens  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
238789Sahrens  */
239789Sahrens static int
240789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off)
241789Sahrens {
242789Sahrens 	znode_t	*zp = VTOZ(vp);
243789Sahrens 	uint64_t noff = (uint64_t)*off; /* new offset */
244789Sahrens 	uint64_t file_sz;
245789Sahrens 	int error;
246789Sahrens 	boolean_t hole;
247789Sahrens 
24811935SMark.Shellenbaum@Sun.COM 	file_sz = zp->z_size;
249789Sahrens 	if (noff >= file_sz)  {
250789Sahrens 		return (ENXIO);
251789Sahrens 	}
252789Sahrens 
253789Sahrens 	if (cmd == _FIO_SEEK_HOLE)
254789Sahrens 		hole = B_TRUE;
255789Sahrens 	else
256789Sahrens 		hole = B_FALSE;
257789Sahrens 
258789Sahrens 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
259789Sahrens 
260789Sahrens 	/* end of file? */
261789Sahrens 	if ((error == ESRCH) || (noff > file_sz)) {
262789Sahrens 		/*
263789Sahrens 		 * Handle the virtual hole at the end of file.
264789Sahrens 		 */
265789Sahrens 		if (hole) {
266789Sahrens 			*off = file_sz;
267789Sahrens 			return (0);
268789Sahrens 		}
269789Sahrens 		return (ENXIO);
270789Sahrens 	}
271789Sahrens 
272789Sahrens 	if (noff < *off)
273789Sahrens 		return (error);
274789Sahrens 	*off = noff;
275789Sahrens 	return (error);
276789Sahrens }
277789Sahrens 
278789Sahrens /* ARGSUSED */
279789Sahrens static int
280789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
2815331Samw     int *rvalp, caller_context_t *ct)
282789Sahrens {
283789Sahrens 	offset_t off;
284789Sahrens 	int error;
285789Sahrens 	zfsvfs_t *zfsvfs;
2865326Sek110237 	znode_t *zp;
287789Sahrens 
288789Sahrens 	switch (com) {
2894339Sperrin 	case _FIOFFS:
290789Sahrens 		return (zfs_sync(vp->v_vfsp, 0, cred));
291789Sahrens 
2921544Seschrock 		/*
2931544Seschrock 		 * The following two ioctls are used by bfu.  Faking out,
2941544Seschrock 		 * necessary to avoid bfu errors.
2951544Seschrock 		 */
2964339Sperrin 	case _FIOGDIO:
2974339Sperrin 	case _FIOSDIO:
2981544Seschrock 		return (0);
2991544Seschrock 
3004339Sperrin 	case _FIO_SEEK_DATA:
3014339Sperrin 	case _FIO_SEEK_HOLE:
302789Sahrens 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
303789Sahrens 			return (EFAULT);
304789Sahrens 
3055326Sek110237 		zp = VTOZ(vp);
3065326Sek110237 		zfsvfs = zp->z_zfsvfs;
3075367Sahrens 		ZFS_ENTER(zfsvfs);
3085367Sahrens 		ZFS_VERIFY_ZP(zp);
309789Sahrens 
310789Sahrens 		/* offset parameter is in/out */
311789Sahrens 		error = zfs_holey(vp, com, &off);
312789Sahrens 		ZFS_EXIT(zfsvfs);
313789Sahrens 		if (error)
314789Sahrens 			return (error);
315789Sahrens 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
316789Sahrens 			return (EFAULT);
317789Sahrens 		return (0);
318789Sahrens 	}
319789Sahrens 	return (ENOTTY);
320789Sahrens }
321789Sahrens 
322789Sahrens /*
3237315SJonathan.Adams@Sun.COM  * Utility functions to map and unmap a single physical page.  These
3247315SJonathan.Adams@Sun.COM  * are used to manage the mappable copies of ZFS file data, and therefore
3257315SJonathan.Adams@Sun.COM  * do not update ref/mod bits.
3267315SJonathan.Adams@Sun.COM  */
3277315SJonathan.Adams@Sun.COM caddr_t
3287315SJonathan.Adams@Sun.COM zfs_map_page(page_t *pp, enum seg_rw rw)
3297315SJonathan.Adams@Sun.COM {
3307315SJonathan.Adams@Sun.COM 	if (kpm_enable)
3317315SJonathan.Adams@Sun.COM 		return (hat_kpm_mapin(pp, 0));
3327315SJonathan.Adams@Sun.COM 	ASSERT(rw == S_READ || rw == S_WRITE);
3337315SJonathan.Adams@Sun.COM 	return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0),
3347315SJonathan.Adams@Sun.COM 	    (caddr_t)-1));
3357315SJonathan.Adams@Sun.COM }
3367315SJonathan.Adams@Sun.COM 
3377315SJonathan.Adams@Sun.COM void
3387315SJonathan.Adams@Sun.COM zfs_unmap_page(page_t *pp, caddr_t addr)
3397315SJonathan.Adams@Sun.COM {
3407315SJonathan.Adams@Sun.COM 	if (kpm_enable) {
3417315SJonathan.Adams@Sun.COM 		hat_kpm_mapout(pp, 0, addr);
3427315SJonathan.Adams@Sun.COM 	} else {
3437315SJonathan.Adams@Sun.COM 		ppmapout(addr);
3447315SJonathan.Adams@Sun.COM 	}
3457315SJonathan.Adams@Sun.COM }
3467315SJonathan.Adams@Sun.COM 
3477315SJonathan.Adams@Sun.COM /*
348789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
349789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
350789Sahrens  *
351789Sahrens  * On Write:	If we find a memory mapped page, we write to *both*
352789Sahrens  *		the page and the dmu buffer.
353789Sahrens  */
3548636SMark.Maybee@Sun.COM static void
3558636SMark.Maybee@Sun.COM update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid)
356789Sahrens {
3578636SMark.Maybee@Sun.COM 	int64_t	off;
3588636SMark.Maybee@Sun.COM 
359789Sahrens 	off = start & PAGEOFFSET;
360789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
361789Sahrens 		page_t *pp;
3628636SMark.Maybee@Sun.COM 		uint64_t nbytes = MIN(PAGESIZE - off, len);
3638636SMark.Maybee@Sun.COM 
364789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
365789Sahrens 			caddr_t va;
366789Sahrens 
3677315SJonathan.Adams@Sun.COM 			va = zfs_map_page(pp, S_WRITE);
3689512SNeil.Perrin@Sun.COM 			(void) dmu_read(os, oid, start+off, nbytes, va+off,
3699512SNeil.Perrin@Sun.COM 			    DMU_READ_PREFETCH);
3707315SJonathan.Adams@Sun.COM 			zfs_unmap_page(pp, va);
371789Sahrens 			page_unlock(pp);
372789Sahrens 		}
3738636SMark.Maybee@Sun.COM 		len -= nbytes;
374789Sahrens 		off = 0;
375789Sahrens 	}
376789Sahrens }
377789Sahrens 
378789Sahrens /*
379789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
380789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
381789Sahrens  *
382789Sahrens  * On Read:	We "read" preferentially from memory mapped pages,
383789Sahrens  *		else we default from the dmu buffer.
384789Sahrens  *
385789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
386789Sahrens  *	the file is memory mapped.
387789Sahrens  */
388789Sahrens static int
3893638Sbillm mappedread(vnode_t *vp, int nbytes, uio_t *uio)
390789Sahrens {
3913638Sbillm 	znode_t *zp = VTOZ(vp);
3923638Sbillm 	objset_t *os = zp->z_zfsvfs->z_os;
3933638Sbillm 	int64_t	start, off;
394789Sahrens 	int len = nbytes;
395789Sahrens 	int error = 0;
396789Sahrens 
397789Sahrens 	start = uio->uio_loffset;
398789Sahrens 	off = start & PAGEOFFSET;
399789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
400789Sahrens 		page_t *pp;
4013638Sbillm 		uint64_t bytes = MIN(PAGESIZE - off, len);
4023638Sbillm 
403789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
404789Sahrens 			caddr_t va;
405789Sahrens 
4067315SJonathan.Adams@Sun.COM 			va = zfs_map_page(pp, S_READ);
407789Sahrens 			error = uiomove(va + off, bytes, UIO_READ, uio);
4087315SJonathan.Adams@Sun.COM 			zfs_unmap_page(pp, va);
409789Sahrens 			page_unlock(pp);
410789Sahrens 		} else {
4113638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, bytes);
412789Sahrens 		}
413789Sahrens 		len -= bytes;
414789Sahrens 		off = 0;
415789Sahrens 		if (error)
416789Sahrens 			break;
417789Sahrens 	}
418789Sahrens 	return (error);
419789Sahrens }
420789Sahrens 
4213638Sbillm offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
422789Sahrens 
423789Sahrens /*
424789Sahrens  * Read bytes from specified file into supplied buffer.
425789Sahrens  *
426789Sahrens  *	IN:	vp	- vnode of file to be read from.
427789Sahrens  *		uio	- structure supplying read location, range info,
428789Sahrens  *			  and return buffer.
429789Sahrens  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
430789Sahrens  *		cr	- credentials of caller.
4315331Samw  *		ct	- caller context
432789Sahrens  *
433789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
434789Sahrens  *
435789Sahrens  *	RETURN:	0 if success
436789Sahrens  *		error code if failure
437789Sahrens  *
438789Sahrens  * Side Effects:
439789Sahrens  *	vp - atime updated if byte count > 0
440789Sahrens  */
441789Sahrens /* ARGSUSED */
442789Sahrens static int
443789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
444789Sahrens {
445789Sahrens 	znode_t		*zp = VTOZ(vp);
446789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4475326Sek110237 	objset_t	*os;
4483638Sbillm 	ssize_t		n, nbytes;
4493638Sbillm 	int		error;
4501669Sperrin 	rl_t		*rl;
45111539SChunli.Zhang@Sun.COM 	xuio_t		*xuio = NULL;
452789Sahrens 
4535367Sahrens 	ZFS_ENTER(zfsvfs);
4545367Sahrens 	ZFS_VERIFY_ZP(zp);
4555326Sek110237 	os = zfsvfs->z_os;
456789Sahrens 
45711935SMark.Shellenbaum@Sun.COM 	if (zp->z_pflags & ZFS_AV_QUARANTINED) {
4585929Smarks 		ZFS_EXIT(zfsvfs);
4595929Smarks 		return (EACCES);
4605929Smarks 	}
4615929Smarks 
462789Sahrens 	/*
463789Sahrens 	 * Validate file offset
464789Sahrens 	 */
465789Sahrens 	if (uio->uio_loffset < (offset_t)0) {
466789Sahrens 		ZFS_EXIT(zfsvfs);
467789Sahrens 		return (EINVAL);
468789Sahrens 	}
469789Sahrens 
470789Sahrens 	/*
471789Sahrens 	 * Fasttrack empty reads
472789Sahrens 	 */
473789Sahrens 	if (uio->uio_resid == 0) {
474789Sahrens 		ZFS_EXIT(zfsvfs);
475789Sahrens 		return (0);
476789Sahrens 	}
477789Sahrens 
478789Sahrens 	/*
4791669Sperrin 	 * Check for mandatory locks
480789Sahrens 	 */
48111935SMark.Shellenbaum@Sun.COM 	if (MANDMODE(zp->z_mode)) {
482789Sahrens 		if (error = chklock(vp, FREAD,
483789Sahrens 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
484789Sahrens 			ZFS_EXIT(zfsvfs);
485789Sahrens 			return (error);
486789Sahrens 		}
487789Sahrens 	}
488789Sahrens 
489789Sahrens 	/*
490789Sahrens 	 * If we're in FRSYNC mode, sync out this znode before reading it.
491789Sahrens 	 */
492*12294SMark.Musante@Sun.COM 	if (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4932638Sperrin 		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
494789Sahrens 
495789Sahrens 	/*
4961669Sperrin 	 * Lock the range against changes.
497789Sahrens 	 */
4981669Sperrin 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
4991669Sperrin 
500789Sahrens 	/*
501789Sahrens 	 * If we are reading past end-of-file we can skip
502789Sahrens 	 * to the end; but we might still need to set atime.
503789Sahrens 	 */
50411935SMark.Shellenbaum@Sun.COM 	if (uio->uio_loffset >= zp->z_size) {
505789Sahrens 		error = 0;
506789Sahrens 		goto out;
507789Sahrens 	}
508789Sahrens 
50911935SMark.Shellenbaum@Sun.COM 	ASSERT(uio->uio_loffset < zp->z_size);
51011935SMark.Shellenbaum@Sun.COM 	n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
5113638Sbillm 
51211539SChunli.Zhang@Sun.COM 	if ((uio->uio_extflg == UIO_XUIO) &&
51311539SChunli.Zhang@Sun.COM 	    (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
51411539SChunli.Zhang@Sun.COM 		int nblk;
51511539SChunli.Zhang@Sun.COM 		int blksz = zp->z_blksz;
51611539SChunli.Zhang@Sun.COM 		uint64_t offset = uio->uio_loffset;
51711539SChunli.Zhang@Sun.COM 
51811539SChunli.Zhang@Sun.COM 		xuio = (xuio_t *)uio;
51911539SChunli.Zhang@Sun.COM 		if ((ISP2(blksz))) {
52011539SChunli.Zhang@Sun.COM 			nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
52111539SChunli.Zhang@Sun.COM 			    blksz)) / blksz;
52211539SChunli.Zhang@Sun.COM 		} else {
52311539SChunli.Zhang@Sun.COM 			ASSERT(offset + n <= blksz);
52411539SChunli.Zhang@Sun.COM 			nblk = 1;
52511539SChunli.Zhang@Sun.COM 		}
52611576SSurya.Prakki@Sun.COM 		(void) dmu_xuio_init(xuio, nblk);
52711539SChunli.Zhang@Sun.COM 
52811539SChunli.Zhang@Sun.COM 		if (vn_has_cached_data(vp)) {
52911539SChunli.Zhang@Sun.COM 			/*
53011539SChunli.Zhang@Sun.COM 			 * For simplicity, we always allocate a full buffer
53111539SChunli.Zhang@Sun.COM 			 * even if we only expect to read a portion of a block.
53211539SChunli.Zhang@Sun.COM 			 */
53311539SChunli.Zhang@Sun.COM 			while (--nblk >= 0) {
53411576SSurya.Prakki@Sun.COM 				(void) dmu_xuio_add(xuio,
53511935SMark.Shellenbaum@Sun.COM 				    dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
53611935SMark.Shellenbaum@Sun.COM 				    blksz), 0, blksz);
53711539SChunli.Zhang@Sun.COM 			}
53811539SChunli.Zhang@Sun.COM 		}
53911539SChunli.Zhang@Sun.COM 	}
54011539SChunli.Zhang@Sun.COM 
5413638Sbillm 	while (n > 0) {
5423638Sbillm 		nbytes = MIN(n, zfs_read_chunk_size -
5433638Sbillm 		    P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
5443638Sbillm 
5453638Sbillm 		if (vn_has_cached_data(vp))
5463638Sbillm 			error = mappedread(vp, nbytes, uio);
5473638Sbillm 		else
5483638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, nbytes);
5497294Sperrin 		if (error) {
5507294Sperrin 			/* convert checksum errors into IO errors */
5517294Sperrin 			if (error == ECKSUM)
5527294Sperrin 				error = EIO;
5533638Sbillm 			break;
5547294Sperrin 		}
5553638Sbillm 
5563638Sbillm 		n -= nbytes;
557789Sahrens 	}
558789Sahrens out:
5592237Smaybee 	zfs_range_unlock(rl);
560789Sahrens 
561789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
562789Sahrens 	ZFS_EXIT(zfsvfs);
563789Sahrens 	return (error);
564789Sahrens }
565789Sahrens 
566789Sahrens /*
567789Sahrens  * Write the bytes to a file.
568789Sahrens  *
569789Sahrens  *	IN:	vp	- vnode of file to be written to.
570789Sahrens  *		uio	- structure supplying write location, range info,
571789Sahrens  *			  and data buffer.
572789Sahrens  *		ioflag	- FAPPEND flag set if in append mode.
573789Sahrens  *		cr	- credentials of caller.
5745331Samw  *		ct	- caller context (NFS/CIFS fem monitor only)
575789Sahrens  *
576789Sahrens  *	OUT:	uio	- updated offset and range.
577789Sahrens  *
578789Sahrens  *	RETURN:	0 if success
579789Sahrens  *		error code if failure
580789Sahrens  *
581789Sahrens  * Timestamps:
582789Sahrens  *	vp - ctime|mtime updated if byte count > 0
583789Sahrens  */
58411935SMark.Shellenbaum@Sun.COM 
585789Sahrens /* ARGSUSED */
586789Sahrens static int
587789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
588789Sahrens {
589789Sahrens 	znode_t		*zp = VTOZ(vp);
590789Sahrens 	rlim64_t	limit = uio->uio_llimit;
591789Sahrens 	ssize_t		start_resid = uio->uio_resid;
592789Sahrens 	ssize_t		tx_bytes;
593789Sahrens 	uint64_t	end_size;
594789Sahrens 	dmu_tx_t	*tx;
595789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
5965326Sek110237 	zilog_t		*zilog;
597789Sahrens 	offset_t	woff;
598789Sahrens 	ssize_t		n, nbytes;
5991669Sperrin 	rl_t		*rl;
600789Sahrens 	int		max_blksz = zfsvfs->z_max_blksz;
6011669Sperrin 	int		error;
6029412SAleksandr.Guzovskiy@Sun.COM 	arc_buf_t	*abuf;
60311539SChunli.Zhang@Sun.COM 	iovec_t		*aiov;
60411539SChunli.Zhang@Sun.COM 	xuio_t		*xuio = NULL;
60511539SChunli.Zhang@Sun.COM 	int		i_iov = 0;
60611539SChunli.Zhang@Sun.COM 	int		iovcnt = uio->uio_iovcnt;
60711539SChunli.Zhang@Sun.COM 	iovec_t		*iovp = uio->uio_iov;
60811539SChunli.Zhang@Sun.COM 	int		write_eof;
60911935SMark.Shellenbaum@Sun.COM 	int		count = 0;
61011935SMark.Shellenbaum@Sun.COM 	sa_bulk_attr_t	bulk[4];
61111935SMark.Shellenbaum@Sun.COM 	uint64_t	mtime[2], ctime[2];
612789Sahrens 
613789Sahrens 	/*
614789Sahrens 	 * Fasttrack empty write
615789Sahrens 	 */
6161669Sperrin 	n = start_resid;
617789Sahrens 	if (n == 0)
618789Sahrens 		return (0);
619789Sahrens 
6201669Sperrin 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
6211669Sperrin 		limit = MAXOFFSET_T;
6221669Sperrin 
6235367Sahrens 	ZFS_ENTER(zfsvfs);
6245367Sahrens 	ZFS_VERIFY_ZP(zp);
6256743Smarks 
62611935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
62711935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
62811935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
62911935SMark.Shellenbaum@Sun.COM 	    &zp->z_size, 8);
63011935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
63111935SMark.Shellenbaum@Sun.COM 	    &zp->z_pflags, 8);
63211935SMark.Shellenbaum@Sun.COM 
6336743Smarks 	/*
6346743Smarks 	 * If immutable or not appending then return EPERM
6356743Smarks 	 */
63611935SMark.Shellenbaum@Sun.COM 	if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
63711935SMark.Shellenbaum@Sun.COM 	    ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
63811935SMark.Shellenbaum@Sun.COM 	    (uio->uio_loffset < zp->z_size))) {
6396743Smarks 		ZFS_EXIT(zfsvfs);
6406743Smarks 		return (EPERM);
6416743Smarks 	}
6426743Smarks 
6435326Sek110237 	zilog = zfsvfs->z_log;
644789Sahrens 
645789Sahrens 	/*
64611083Swilliam.gorrell@sun.com 	 * Validate file offset
64711083Swilliam.gorrell@sun.com 	 */
64811935SMark.Shellenbaum@Sun.COM 	woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
64911083Swilliam.gorrell@sun.com 	if (woff < 0) {
65011083Swilliam.gorrell@sun.com 		ZFS_EXIT(zfsvfs);
65111083Swilliam.gorrell@sun.com 		return (EINVAL);
65211083Swilliam.gorrell@sun.com 	}
65311083Swilliam.gorrell@sun.com 
65411083Swilliam.gorrell@sun.com 	/*
65511083Swilliam.gorrell@sun.com 	 * Check for mandatory locks before calling zfs_range_lock()
65611083Swilliam.gorrell@sun.com 	 * in order to prevent a deadlock with locks set via fcntl().
65711083Swilliam.gorrell@sun.com 	 */
65811935SMark.Shellenbaum@Sun.COM 	if (MANDMODE((mode_t)zp->z_mode) &&
65911083Swilliam.gorrell@sun.com 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
66011083Swilliam.gorrell@sun.com 		ZFS_EXIT(zfsvfs);
66111083Swilliam.gorrell@sun.com 		return (error);
66211083Swilliam.gorrell@sun.com 	}
66311083Swilliam.gorrell@sun.com 
66411083Swilliam.gorrell@sun.com 	/*
6652237Smaybee 	 * Pre-fault the pages to ensure slow (eg NFS) pages
6661669Sperrin 	 * don't hold up txg.
66711539SChunli.Zhang@Sun.COM 	 * Skip this if uio contains loaned arc_buf.
668789Sahrens 	 */
66911539SChunli.Zhang@Sun.COM 	if ((uio->uio_extflg == UIO_XUIO) &&
67011539SChunli.Zhang@Sun.COM 	    (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
67111539SChunli.Zhang@Sun.COM 		xuio = (xuio_t *)uio;
67211539SChunli.Zhang@Sun.COM 	else
67311539SChunli.Zhang@Sun.COM 		uio_prefaultpages(n, uio);
674789Sahrens 
675789Sahrens 	/*
676789Sahrens 	 * If in append mode, set the io offset pointer to eof.
677789Sahrens 	 */
6781669Sperrin 	if (ioflag & FAPPEND) {
6791669Sperrin 		/*
68011083Swilliam.gorrell@sun.com 		 * Obtain an appending range lock to guarantee file append
68111083Swilliam.gorrell@sun.com 		 * semantics.  We reset the write offset once we have the lock.
6821669Sperrin 		 */
6831669Sperrin 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
68411083Swilliam.gorrell@sun.com 		woff = rl->r_off;
6851669Sperrin 		if (rl->r_len == UINT64_MAX) {
68611083Swilliam.gorrell@sun.com 			/*
68711083Swilliam.gorrell@sun.com 			 * We overlocked the file because this write will cause
68811083Swilliam.gorrell@sun.com 			 * the file block size to increase.
68911083Swilliam.gorrell@sun.com 			 * Note that zp_size cannot change with this lock held.
69011083Swilliam.gorrell@sun.com 			 */
69111935SMark.Shellenbaum@Sun.COM 			woff = zp->z_size;
6921669Sperrin 		}
69311083Swilliam.gorrell@sun.com 		uio->uio_loffset = woff;
694789Sahrens 	} else {
695789Sahrens 		/*
69611083Swilliam.gorrell@sun.com 		 * Note that if the file block size will change as a result of
69711083Swilliam.gorrell@sun.com 		 * this write, then this range lock will lock the entire file
69811083Swilliam.gorrell@sun.com 		 * so that we can re-write the block safely.
699789Sahrens 		 */
7001669Sperrin 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
701789Sahrens 	}
702789Sahrens 
703789Sahrens 	if (woff >= limit) {
7043638Sbillm 		zfs_range_unlock(rl);
7053638Sbillm 		ZFS_EXIT(zfsvfs);
7063638Sbillm 		return (EFBIG);
707789Sahrens 	}
708789Sahrens 
709789Sahrens 	if ((woff + n) > limit || woff > (limit - n))
710789Sahrens 		n = limit - woff;
711789Sahrens 
71211539SChunli.Zhang@Sun.COM 	/* Will this write extend the file length? */
71311935SMark.Shellenbaum@Sun.COM 	write_eof = (woff + n > zp->z_size);
71411935SMark.Shellenbaum@Sun.COM 
71511935SMark.Shellenbaum@Sun.COM 	end_size = MAX(zp->z_size, woff + n);
716789Sahrens 
7171669Sperrin 	/*
7183638Sbillm 	 * Write the file in reasonable size chunks.  Each chunk is written
7193638Sbillm 	 * in a separate transaction; this keeps the intent log records small
7203638Sbillm 	 * and allows us to do more fine-grained space accounting.
721789Sahrens 	 */
722789Sahrens 	while (n > 0) {
7239412SAleksandr.Guzovskiy@Sun.COM 		abuf = NULL;
7249412SAleksandr.Guzovskiy@Sun.COM 		woff = uio->uio_loffset;
7259412SAleksandr.Guzovskiy@Sun.COM again:
72611935SMark.Shellenbaum@Sun.COM 		if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
72711935SMark.Shellenbaum@Sun.COM 		    zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
7289412SAleksandr.Guzovskiy@Sun.COM 			if (abuf != NULL)
7299412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7309396SMatthew.Ahrens@Sun.COM 			error = EDQUOT;
7319396SMatthew.Ahrens@Sun.COM 			break;
7329396SMatthew.Ahrens@Sun.COM 		}
7339412SAleksandr.Guzovskiy@Sun.COM 
73411539SChunli.Zhang@Sun.COM 		if (xuio && abuf == NULL) {
73511539SChunli.Zhang@Sun.COM 			ASSERT(i_iov < iovcnt);
73611539SChunli.Zhang@Sun.COM 			aiov = &iovp[i_iov];
73711539SChunli.Zhang@Sun.COM 			abuf = dmu_xuio_arcbuf(xuio, i_iov);
73811539SChunli.Zhang@Sun.COM 			dmu_xuio_clear(xuio, i_iov);
73911539SChunli.Zhang@Sun.COM 			DTRACE_PROBE3(zfs_cp_write, int, i_iov,
74011539SChunli.Zhang@Sun.COM 			    iovec_t *, aiov, arc_buf_t *, abuf);
74111539SChunli.Zhang@Sun.COM 			ASSERT((aiov->iov_base == abuf->b_data) ||
74211539SChunli.Zhang@Sun.COM 			    ((char *)aiov->iov_base - (char *)abuf->b_data +
74311539SChunli.Zhang@Sun.COM 			    aiov->iov_len == arc_buf_size(abuf)));
74411539SChunli.Zhang@Sun.COM 			i_iov++;
74511539SChunli.Zhang@Sun.COM 		} else if (abuf == NULL && n >= max_blksz &&
74611935SMark.Shellenbaum@Sun.COM 		    woff >= zp->z_size &&
7479412SAleksandr.Guzovskiy@Sun.COM 		    P2PHASE(woff, max_blksz) == 0 &&
7489412SAleksandr.Guzovskiy@Sun.COM 		    zp->z_blksz == max_blksz) {
74911539SChunli.Zhang@Sun.COM 			/*
75011539SChunli.Zhang@Sun.COM 			 * This write covers a full block.  "Borrow" a buffer
75111539SChunli.Zhang@Sun.COM 			 * from the dmu so that we can fill it before we enter
75211539SChunli.Zhang@Sun.COM 			 * a transaction.  This avoids the possibility of
75311539SChunli.Zhang@Sun.COM 			 * holding up the transaction if the data copy hangs
75411539SChunli.Zhang@Sun.COM 			 * up on a pagefault (e.g., from an NFS server mapping).
75511539SChunli.Zhang@Sun.COM 			 */
7569412SAleksandr.Guzovskiy@Sun.COM 			size_t cbytes;
7579412SAleksandr.Guzovskiy@Sun.COM 
75811935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
75911935SMark.Shellenbaum@Sun.COM 			    max_blksz);
7609412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(abuf != NULL);
7619412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(arc_buf_size(abuf) == max_blksz);
7629412SAleksandr.Guzovskiy@Sun.COM 			if (error = uiocopy(abuf->b_data, max_blksz,
7639412SAleksandr.Guzovskiy@Sun.COM 			    UIO_WRITE, uio, &cbytes)) {
7649412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7659412SAleksandr.Guzovskiy@Sun.COM 				break;
7669412SAleksandr.Guzovskiy@Sun.COM 			}
7679412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(cbytes == max_blksz);
7689412SAleksandr.Guzovskiy@Sun.COM 		}
7699412SAleksandr.Guzovskiy@Sun.COM 
7709412SAleksandr.Guzovskiy@Sun.COM 		/*
7719412SAleksandr.Guzovskiy@Sun.COM 		 * Start a transaction.
7729412SAleksandr.Guzovskiy@Sun.COM 		 */
773789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
77411935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
775789Sahrens 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
77611935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, zp);
7778227SNeil.Perrin@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
778789Sahrens 		if (error) {
7798227SNeil.Perrin@Sun.COM 			if (error == ERESTART) {
7802113Sahrens 				dmu_tx_wait(tx);
7812113Sahrens 				dmu_tx_abort(tx);
7829412SAleksandr.Guzovskiy@Sun.COM 				goto again;
783789Sahrens 			}
7842113Sahrens 			dmu_tx_abort(tx);
7859412SAleksandr.Guzovskiy@Sun.COM 			if (abuf != NULL)
7869412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7873638Sbillm 			break;
7883638Sbillm 		}
7893638Sbillm 
7903638Sbillm 		/*
7913638Sbillm 		 * If zfs_range_lock() over-locked we grow the blocksize
7923638Sbillm 		 * and then reduce the lock range.  This will only happen
7933638Sbillm 		 * on the first iteration since zfs_range_reduce() will
7943638Sbillm 		 * shrink down r_len to the appropriate size.
7953638Sbillm 		 */
7963638Sbillm 		if (rl->r_len == UINT64_MAX) {
7973638Sbillm 			uint64_t new_blksz;
7983638Sbillm 
7993638Sbillm 			if (zp->z_blksz > max_blksz) {
8003638Sbillm 				ASSERT(!ISP2(zp->z_blksz));
8013638Sbillm 				new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
8023638Sbillm 			} else {
8033638Sbillm 				new_blksz = MIN(end_size, max_blksz);
8043638Sbillm 			}
8053638Sbillm 			zfs_grow_blocksize(zp, new_blksz, tx);
8063638Sbillm 			zfs_range_reduce(rl, woff, n);
8073638Sbillm 		}
8083638Sbillm 
8093638Sbillm 		/*
8103638Sbillm 		 * XXX - should we really limit each write to z_max_blksz?
8113638Sbillm 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
8123638Sbillm 		 */
8133638Sbillm 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
8143638Sbillm 
8159412SAleksandr.Guzovskiy@Sun.COM 		if (abuf == NULL) {
8169412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes = uio->uio_resid;
81712123STim.Haley@Sun.COM 			error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
81812123STim.Haley@Sun.COM 			    uio, nbytes, tx);
8199412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes -= uio->uio_resid;
8209412SAleksandr.Guzovskiy@Sun.COM 		} else {
8219412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes = nbytes;
82211539SChunli.Zhang@Sun.COM 			ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
82311539SChunli.Zhang@Sun.COM 			/*
82411539SChunli.Zhang@Sun.COM 			 * If this is not a full block write, but we are
82511539SChunli.Zhang@Sun.COM 			 * extending the file past EOF and this data starts
82611539SChunli.Zhang@Sun.COM 			 * block-aligned, use assign_arcbuf().  Otherwise,
82711539SChunli.Zhang@Sun.COM 			 * write via dmu_write().
82811539SChunli.Zhang@Sun.COM 			 */
82911539SChunli.Zhang@Sun.COM 			if (tx_bytes < max_blksz && (!write_eof ||
83011539SChunli.Zhang@Sun.COM 			    aiov->iov_base != abuf->b_data)) {
83111539SChunli.Zhang@Sun.COM 				ASSERT(xuio);
83211539SChunli.Zhang@Sun.COM 				dmu_write(zfsvfs->z_os, zp->z_id, woff,
83311539SChunli.Zhang@Sun.COM 				    aiov->iov_len, aiov->iov_base, tx);
83411539SChunli.Zhang@Sun.COM 				dmu_return_arcbuf(abuf);
83511539SChunli.Zhang@Sun.COM 				xuio_stat_wbuf_copied();
83611539SChunli.Zhang@Sun.COM 			} else {
83711539SChunli.Zhang@Sun.COM 				ASSERT(xuio || tx_bytes == max_blksz);
83811935SMark.Shellenbaum@Sun.COM 				dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
83911935SMark.Shellenbaum@Sun.COM 				    woff, abuf, tx);
84011539SChunli.Zhang@Sun.COM 			}
8419412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(tx_bytes <= uio->uio_resid);
8429412SAleksandr.Guzovskiy@Sun.COM 			uioskip(uio, tx_bytes);
8439412SAleksandr.Guzovskiy@Sun.COM 		}
8449412SAleksandr.Guzovskiy@Sun.COM 		if (tx_bytes && vn_has_cached_data(vp)) {
8458636SMark.Maybee@Sun.COM 			update_pages(vp, woff,
8468636SMark.Maybee@Sun.COM 			    tx_bytes, zfsvfs->z_os, zp->z_id);
8479412SAleksandr.Guzovskiy@Sun.COM 		}
8483638Sbillm 
8493638Sbillm 		/*
8503638Sbillm 		 * If we made no progress, we're done.  If we made even
8513638Sbillm 		 * partial progress, update the znode and ZIL accordingly.
8523638Sbillm 		 */
8533638Sbillm 		if (tx_bytes == 0) {
85411935SMark.Shellenbaum@Sun.COM 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
85511935SMark.Shellenbaum@Sun.COM 			    (void *)&zp->z_size, sizeof (uint64_t), tx);
8563897Smaybee 			dmu_tx_commit(tx);
8573638Sbillm 			ASSERT(error != 0);
8583638Sbillm 			break;
8593638Sbillm 		}
8603638Sbillm 
861789Sahrens 		/*
8623638Sbillm 		 * Clear Set-UID/Set-GID bits on successful write if not
8633638Sbillm 		 * privileged and at least one of the excute bits is set.
8643638Sbillm 		 *
8653638Sbillm 		 * It would be nice to to this after all writes have
8663638Sbillm 		 * been done, but that would still expose the ISUID/ISGID
8673638Sbillm 		 * to another app after the partial write is committed.
8685331Samw 		 *
869789Sahrens 		 */
8703638Sbillm 		mutex_enter(&zp->z_acl_lock);
87111935SMark.Shellenbaum@Sun.COM 		if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
8723638Sbillm 		    (S_IXUSR >> 6))) != 0 &&
87311935SMark.Shellenbaum@Sun.COM 		    (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
8743638Sbillm 		    secpolicy_vnode_setid_retain(cr,
87511935SMark.Shellenbaum@Sun.COM 		    (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
87611935SMark.Shellenbaum@Sun.COM 			uint64_t newmode;
87711935SMark.Shellenbaum@Sun.COM 			zp->z_mode &= ~(S_ISUID | S_ISGID);
87811935SMark.Shellenbaum@Sun.COM 			newmode = zp->z_mode;
87911935SMark.Shellenbaum@Sun.COM 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
88011935SMark.Shellenbaum@Sun.COM 			    (void *)&newmode, sizeof (uint64_t), tx);
8813638Sbillm 		}
8823638Sbillm 		mutex_exit(&zp->z_acl_lock);
8833638Sbillm 
88411935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
88511935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
8863638Sbillm 
8873638Sbillm 		/*
8883638Sbillm 		 * Update the file size (zp_size) if it has changed;
8893638Sbillm 		 * account for possible concurrent updates.
8903638Sbillm 		 */
89111935SMark.Shellenbaum@Sun.COM 		while ((end_size = zp->z_size) < uio->uio_loffset) {
89211935SMark.Shellenbaum@Sun.COM 			(void) atomic_cas_64(&zp->z_size, end_size,
893789Sahrens 			    uio->uio_loffset);
89411935SMark.Shellenbaum@Sun.COM 			ASSERT(error == 0);
89511935SMark.Shellenbaum@Sun.COM 		}
89611935SMark.Shellenbaum@Sun.COM 		error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
89711935SMark.Shellenbaum@Sun.COM 
8983638Sbillm 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
8993638Sbillm 		dmu_tx_commit(tx);
9003638Sbillm 
9013638Sbillm 		if (error != 0)
9023638Sbillm 			break;
9033638Sbillm 		ASSERT(tx_bytes == nbytes);
9043638Sbillm 		n -= nbytes;
905789Sahrens 	}
906789Sahrens 
9072237Smaybee 	zfs_range_unlock(rl);
908789Sahrens 
909789Sahrens 	/*
910789Sahrens 	 * If we're in replay mode, or we made no progress, return error.
911789Sahrens 	 * Otherwise, it's at least a partial write, so it's successful.
912789Sahrens 	 */
9138227SNeil.Perrin@Sun.COM 	if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
914789Sahrens 		ZFS_EXIT(zfsvfs);
915789Sahrens 		return (error);
916789Sahrens 	}
917789Sahrens 
918*12294SMark.Musante@Sun.COM 	if (ioflag & (FSYNC | FDSYNC) ||
919*12294SMark.Musante@Sun.COM 	    zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
9202638Sperrin 		zil_commit(zilog, zp->z_last_itx, zp->z_id);
921789Sahrens 
922789Sahrens 	ZFS_EXIT(zfsvfs);
923789Sahrens 	return (0);
924789Sahrens }
925789Sahrens 
9262237Smaybee void
92710922SJeff.Bonwick@Sun.COM zfs_get_done(zgd_t *zgd, int error)
9282237Smaybee {
92910922SJeff.Bonwick@Sun.COM 	znode_t *zp = zgd->zgd_private;
93010922SJeff.Bonwick@Sun.COM 	objset_t *os = zp->z_zfsvfs->z_os;
93110922SJeff.Bonwick@Sun.COM 
93210922SJeff.Bonwick@Sun.COM 	if (zgd->zgd_db)
93310922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(zgd->zgd_db, zgd);
93410922SJeff.Bonwick@Sun.COM 
93510922SJeff.Bonwick@Sun.COM 	zfs_range_unlock(zgd->zgd_rl);
93610922SJeff.Bonwick@Sun.COM 
9379321SNeil.Perrin@Sun.COM 	/*
9389321SNeil.Perrin@Sun.COM 	 * Release the vnode asynchronously as we currently have the
9399321SNeil.Perrin@Sun.COM 	 * txg stopped from syncing.
9409321SNeil.Perrin@Sun.COM 	 */
94110922SJeff.Bonwick@Sun.COM 	VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
94210922SJeff.Bonwick@Sun.COM 
94310922SJeff.Bonwick@Sun.COM 	if (error == 0 && zgd->zgd_bp)
94410922SJeff.Bonwick@Sun.COM 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
94510922SJeff.Bonwick@Sun.COM 
9463063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
9472237Smaybee }
9482237Smaybee 
94910209SMark.Musante@Sun.COM #ifdef DEBUG
95010209SMark.Musante@Sun.COM static int zil_fault_io = 0;
95110209SMark.Musante@Sun.COM #endif
95210209SMark.Musante@Sun.COM 
953789Sahrens /*
954789Sahrens  * Get data to generate a TX_WRITE intent log record.
955789Sahrens  */
956789Sahrens int
9572237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
958789Sahrens {
959789Sahrens 	zfsvfs_t *zfsvfs = arg;
960789Sahrens 	objset_t *os = zfsvfs->z_os;
961789Sahrens 	znode_t *zp;
96210922SJeff.Bonwick@Sun.COM 	uint64_t object = lr->lr_foid;
96310922SJeff.Bonwick@Sun.COM 	uint64_t offset = lr->lr_offset;
96410922SJeff.Bonwick@Sun.COM 	uint64_t size = lr->lr_length;
96510922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = &lr->lr_blkptr;
9662237Smaybee 	dmu_buf_t *db;
9673063Sperrin 	zgd_t *zgd;
968789Sahrens 	int error = 0;
969789Sahrens 
97010922SJeff.Bonwick@Sun.COM 	ASSERT(zio != NULL);
97110922SJeff.Bonwick@Sun.COM 	ASSERT(size != 0);
972789Sahrens 
973789Sahrens 	/*
9741669Sperrin 	 * Nothing to do if the file has been removed
975789Sahrens 	 */
97610922SJeff.Bonwick@Sun.COM 	if (zfs_zget(zfsvfs, object, &zp) != 0)
977789Sahrens 		return (ENOENT);
9783461Sahrens 	if (zp->z_unlinked) {
9799321SNeil.Perrin@Sun.COM 		/*
9809321SNeil.Perrin@Sun.COM 		 * Release the vnode asynchronously as we currently have the
9819321SNeil.Perrin@Sun.COM 		 * txg stopped from syncing.
9829321SNeil.Perrin@Sun.COM 		 */
9839321SNeil.Perrin@Sun.COM 		VN_RELE_ASYNC(ZTOV(zp),
9849321SNeil.Perrin@Sun.COM 		    dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
985789Sahrens 		return (ENOENT);
986789Sahrens 	}
987789Sahrens 
98810922SJeff.Bonwick@Sun.COM 	zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
98910922SJeff.Bonwick@Sun.COM 	zgd->zgd_zilog = zfsvfs->z_log;
99010922SJeff.Bonwick@Sun.COM 	zgd->zgd_private = zp;
99110922SJeff.Bonwick@Sun.COM 
992789Sahrens 	/*
993789Sahrens 	 * Write records come in two flavors: immediate and indirect.
994789Sahrens 	 * For small writes it's cheaper to store the data with the
995789Sahrens 	 * log record (immediate); for large writes it's cheaper to
996789Sahrens 	 * sync the data and get a pointer to it (indirect) so that
997789Sahrens 	 * we don't have to write the data twice.
998789Sahrens 	 */
9991669Sperrin 	if (buf != NULL) { /* immediate write */
100010922SJeff.Bonwick@Sun.COM 		zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
10011669Sperrin 		/* test for truncation needs to be done while range locked */
100211935SMark.Shellenbaum@Sun.COM 		if (offset >= zp->z_size) {
10031669Sperrin 			error = ENOENT;
100410922SJeff.Bonwick@Sun.COM 		} else {
100510922SJeff.Bonwick@Sun.COM 			error = dmu_read(os, object, offset, size, buf,
100610922SJeff.Bonwick@Sun.COM 			    DMU_READ_NO_PREFETCH);
10071669Sperrin 		}
100810922SJeff.Bonwick@Sun.COM 		ASSERT(error == 0 || error == ENOENT);
10091669Sperrin 	} else { /* indirect write */
1010789Sahrens 		/*
10111669Sperrin 		 * Have to lock the whole block to ensure when it's
10121669Sperrin 		 * written out and it's checksum is being calculated
10131669Sperrin 		 * that no one can change the data. We need to re-check
10141669Sperrin 		 * blocksize after we get the lock in case it's changed!
1015789Sahrens 		 */
10161669Sperrin 		for (;;) {
101710922SJeff.Bonwick@Sun.COM 			uint64_t blkoff;
101810922SJeff.Bonwick@Sun.COM 			size = zp->z_blksz;
101910945SJeff.Bonwick@Sun.COM 			blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
102010922SJeff.Bonwick@Sun.COM 			offset -= blkoff;
102110922SJeff.Bonwick@Sun.COM 			zgd->zgd_rl = zfs_range_lock(zp, offset, size,
102210922SJeff.Bonwick@Sun.COM 			    RL_READER);
102310922SJeff.Bonwick@Sun.COM 			if (zp->z_blksz == size)
10241669Sperrin 				break;
102510922SJeff.Bonwick@Sun.COM 			offset += blkoff;
102610922SJeff.Bonwick@Sun.COM 			zfs_range_unlock(zgd->zgd_rl);
10271669Sperrin 		}
10281669Sperrin 		/* test for truncation needs to be done while range locked */
102911935SMark.Shellenbaum@Sun.COM 		if (lr->lr_offset >= zp->z_size)
10301669Sperrin 			error = ENOENT;
103110209SMark.Musante@Sun.COM #ifdef DEBUG
103210209SMark.Musante@Sun.COM 		if (zil_fault_io) {
103310209SMark.Musante@Sun.COM 			error = EIO;
103410209SMark.Musante@Sun.COM 			zil_fault_io = 0;
103510209SMark.Musante@Sun.COM 		}
103610209SMark.Musante@Sun.COM #endif
103710922SJeff.Bonwick@Sun.COM 		if (error == 0)
103812285SJeff.Bonwick@Sun.COM 			error = dmu_buf_hold(os, object, offset, zgd, &db,
103912285SJeff.Bonwick@Sun.COM 			    DMU_READ_NO_PREFETCH);
104010922SJeff.Bonwick@Sun.COM 
104110800SNeil.Perrin@Sun.COM 		if (error == 0) {
104210922SJeff.Bonwick@Sun.COM 			zgd->zgd_db = db;
104310922SJeff.Bonwick@Sun.COM 			zgd->zgd_bp = bp;
104410922SJeff.Bonwick@Sun.COM 
104510922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_offset == offset);
104610922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_size == size);
104710922SJeff.Bonwick@Sun.COM 
104810922SJeff.Bonwick@Sun.COM 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
104910922SJeff.Bonwick@Sun.COM 			    zfs_get_done, zgd);
105010922SJeff.Bonwick@Sun.COM 			ASSERT(error || lr->lr_length <= zp->z_blksz);
105110922SJeff.Bonwick@Sun.COM 
105210800SNeil.Perrin@Sun.COM 			/*
105310922SJeff.Bonwick@Sun.COM 			 * On success, we need to wait for the write I/O
105410922SJeff.Bonwick@Sun.COM 			 * initiated by dmu_sync() to complete before we can
105510922SJeff.Bonwick@Sun.COM 			 * release this dbuf.  We will finish everything up
105610922SJeff.Bonwick@Sun.COM 			 * in the zfs_get_done() callback.
105710800SNeil.Perrin@Sun.COM 			 */
105810922SJeff.Bonwick@Sun.COM 			if (error == 0)
105910922SJeff.Bonwick@Sun.COM 				return (0);
106010922SJeff.Bonwick@Sun.COM 
106110922SJeff.Bonwick@Sun.COM 			if (error == EALREADY) {
106210922SJeff.Bonwick@Sun.COM 				lr->lr_common.lrc_txtype = TX_WRITE2;
106310922SJeff.Bonwick@Sun.COM 				error = 0;
106410922SJeff.Bonwick@Sun.COM 			}
106510800SNeil.Perrin@Sun.COM 		}
1066789Sahrens 	}
106710922SJeff.Bonwick@Sun.COM 
106810922SJeff.Bonwick@Sun.COM 	zfs_get_done(zgd, error);
106910922SJeff.Bonwick@Sun.COM 
1070789Sahrens 	return (error);
1071789Sahrens }
1072789Sahrens 
1073789Sahrens /*ARGSUSED*/
1074789Sahrens static int
10755331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
10765331Samw     caller_context_t *ct)
1077789Sahrens {
1078789Sahrens 	znode_t *zp = VTOZ(vp);
1079789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1080789Sahrens 	int error;
1081789Sahrens 
10825367Sahrens 	ZFS_ENTER(zfsvfs);
10835367Sahrens 	ZFS_VERIFY_ZP(zp);
10845331Samw 
10855331Samw 	if (flag & V_ACE_MASK)
10865331Samw 		error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
10875331Samw 	else
10885331Samw 		error = zfs_zaccess_rwx(zp, mode, flag, cr);
10895331Samw 
1090789Sahrens 	ZFS_EXIT(zfsvfs);
1091789Sahrens 	return (error);
1092789Sahrens }
1093789Sahrens 
1094789Sahrens /*
10959981STim.Haley@Sun.COM  * If vnode is for a device return a specfs vnode instead.
10969981STim.Haley@Sun.COM  */
10979981STim.Haley@Sun.COM static int
10989981STim.Haley@Sun.COM specvp_check(vnode_t **vpp, cred_t *cr)
10999981STim.Haley@Sun.COM {
11009981STim.Haley@Sun.COM 	int error = 0;
11019981STim.Haley@Sun.COM 
11029981STim.Haley@Sun.COM 	if (IS_DEVVP(*vpp)) {
11039981STim.Haley@Sun.COM 		struct vnode *svp;
11049981STim.Haley@Sun.COM 
11059981STim.Haley@Sun.COM 		svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
11069981STim.Haley@Sun.COM 		VN_RELE(*vpp);
11079981STim.Haley@Sun.COM 		if (svp == NULL)
11089981STim.Haley@Sun.COM 			error = ENOSYS;
11099981STim.Haley@Sun.COM 		*vpp = svp;
11109981STim.Haley@Sun.COM 	}
11119981STim.Haley@Sun.COM 	return (error);
11129981STim.Haley@Sun.COM }
11139981STim.Haley@Sun.COM 
11149981STim.Haley@Sun.COM 
11159981STim.Haley@Sun.COM /*
1116789Sahrens  * Lookup an entry in a directory, or an extended attribute directory.
1117789Sahrens  * If it exists, return a held vnode reference for it.
1118789Sahrens  *
1119789Sahrens  *	IN:	dvp	- vnode of directory to search.
1120789Sahrens  *		nm	- name of entry to lookup.
1121789Sahrens  *		pnp	- full pathname to lookup [UNUSED].
1122789Sahrens  *		flags	- LOOKUP_XATTR set if looking for an attribute.
1123789Sahrens  *		rdir	- root directory vnode [UNUSED].
1124789Sahrens  *		cr	- credentials of caller.
11255331Samw  *		ct	- caller context
11265331Samw  *		direntflags - directory lookup flags
11275331Samw  *		realpnp - returned pathname.
1128789Sahrens  *
1129789Sahrens  *	OUT:	vpp	- vnode of located entry, NULL if not found.
1130789Sahrens  *
1131789Sahrens  *	RETURN:	0 if success
1132789Sahrens  *		error code if failure
1133789Sahrens  *
1134789Sahrens  * Timestamps:
1135789Sahrens  *	NA
1136789Sahrens  */
1137789Sahrens /* ARGSUSED */
1138789Sahrens static int
1139789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
11405331Samw     int flags, vnode_t *rdir, cred_t *cr,  caller_context_t *ct,
11415331Samw     int *direntflags, pathname_t *realpnp)
1142789Sahrens {
1143789Sahrens 	znode_t *zdp = VTOZ(dvp);
1144789Sahrens 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
11459981STim.Haley@Sun.COM 	int	error = 0;
11469981STim.Haley@Sun.COM 
11479981STim.Haley@Sun.COM 	/* fast path */
11489981STim.Haley@Sun.COM 	if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
11499981STim.Haley@Sun.COM 
11509981STim.Haley@Sun.COM 		if (dvp->v_type != VDIR) {
11519981STim.Haley@Sun.COM 			return (ENOTDIR);
115211935SMark.Shellenbaum@Sun.COM 		} else if (zdp->z_sa_hdl == NULL) {
11539981STim.Haley@Sun.COM 			return (EIO);
11549981STim.Haley@Sun.COM 		}
11559981STim.Haley@Sun.COM 
11569981STim.Haley@Sun.COM 		if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
11579981STim.Haley@Sun.COM 			error = zfs_fastaccesschk_execute(zdp, cr);
11589981STim.Haley@Sun.COM 			if (!error) {
11599981STim.Haley@Sun.COM 				*vpp = dvp;
11609981STim.Haley@Sun.COM 				VN_HOLD(*vpp);
11619981STim.Haley@Sun.COM 				return (0);
11629981STim.Haley@Sun.COM 			}
11639981STim.Haley@Sun.COM 			return (error);
11649981STim.Haley@Sun.COM 		} else {
11659981STim.Haley@Sun.COM 			vnode_t *tvp = dnlc_lookup(dvp, nm);
11669981STim.Haley@Sun.COM 
11679981STim.Haley@Sun.COM 			if (tvp) {
11689981STim.Haley@Sun.COM 				error = zfs_fastaccesschk_execute(zdp, cr);
11699981STim.Haley@Sun.COM 				if (error) {
11709981STim.Haley@Sun.COM 					VN_RELE(tvp);
11719981STim.Haley@Sun.COM 					return (error);
11729981STim.Haley@Sun.COM 				}
11739981STim.Haley@Sun.COM 				if (tvp == DNLC_NO_VNODE) {
11749981STim.Haley@Sun.COM 					VN_RELE(tvp);
11759981STim.Haley@Sun.COM 					return (ENOENT);
11769981STim.Haley@Sun.COM 				} else {
11779981STim.Haley@Sun.COM 					*vpp = tvp;
11789981STim.Haley@Sun.COM 					return (specvp_check(vpp, cr));
11799981STim.Haley@Sun.COM 				}
11809981STim.Haley@Sun.COM 			}
11819981STim.Haley@Sun.COM 		}
11829981STim.Haley@Sun.COM 	}
11839981STim.Haley@Sun.COM 
11849981STim.Haley@Sun.COM 	DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1185789Sahrens 
11865367Sahrens 	ZFS_ENTER(zfsvfs);
11875367Sahrens 	ZFS_VERIFY_ZP(zdp);
1188789Sahrens 
1189789Sahrens 	*vpp = NULL;
1190789Sahrens 
1191789Sahrens 	if (flags & LOOKUP_XATTR) {
1192789Sahrens 		/*
11933234Sck153898 		 * If the xattr property is off, refuse the lookup request.
11943234Sck153898 		 */
11953234Sck153898 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
11963234Sck153898 			ZFS_EXIT(zfsvfs);
11973234Sck153898 			return (EINVAL);
11983234Sck153898 		}
11993234Sck153898 
12003234Sck153898 		/*
1201789Sahrens 		 * We don't allow recursive attributes..
1202789Sahrens 		 * Maybe someday we will.
1203789Sahrens 		 */
120411935SMark.Shellenbaum@Sun.COM 		if (zdp->z_pflags & ZFS_XATTR) {
1205789Sahrens 			ZFS_EXIT(zfsvfs);
1206789Sahrens 			return (EINVAL);
1207789Sahrens 		}
1208789Sahrens 
12093280Sck153898 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1210789Sahrens 			ZFS_EXIT(zfsvfs);
1211789Sahrens 			return (error);
1212789Sahrens 		}
1213789Sahrens 
1214789Sahrens 		/*
1215789Sahrens 		 * Do we have permission to get into attribute directory?
1216789Sahrens 		 */
1217789Sahrens 
12185331Samw 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
12195331Samw 		    B_FALSE, cr)) {
1220789Sahrens 			VN_RELE(*vpp);
12215331Samw 			*vpp = NULL;
1222789Sahrens 		}
1223789Sahrens 
1224789Sahrens 		ZFS_EXIT(zfsvfs);
1225789Sahrens 		return (error);
1226789Sahrens 	}
1227789Sahrens 
12281512Sek110237 	if (dvp->v_type != VDIR) {
12291512Sek110237 		ZFS_EXIT(zfsvfs);
12301460Smarks 		return (ENOTDIR);
12311512Sek110237 	}
12321460Smarks 
1233789Sahrens 	/*
1234789Sahrens 	 * Check accessibility of directory.
1235789Sahrens 	 */
1236789Sahrens 
12375331Samw 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1238789Sahrens 		ZFS_EXIT(zfsvfs);
1239789Sahrens 		return (error);
1240789Sahrens 	}
1241789Sahrens 
12425498Stimh 	if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
12435331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
12445331Samw 		ZFS_EXIT(zfsvfs);
12455331Samw 		return (EILSEQ);
12465331Samw 	}
12475331Samw 
12485331Samw 	error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
12499981STim.Haley@Sun.COM 	if (error == 0)
12509981STim.Haley@Sun.COM 		error = specvp_check(vpp, cr);
1251789Sahrens 
1252789Sahrens 	ZFS_EXIT(zfsvfs);
1253789Sahrens 	return (error);
1254789Sahrens }
1255789Sahrens 
1256789Sahrens /*
1257789Sahrens  * Attempt to create a new entry in a directory.  If the entry
1258789Sahrens  * already exists, truncate the file if permissible, else return
1259789Sahrens  * an error.  Return the vp of the created or trunc'd file.
1260789Sahrens  *
1261789Sahrens  *	IN:	dvp	- vnode of directory to put new file entry in.
1262789Sahrens  *		name	- name of new file entry.
1263789Sahrens  *		vap	- attributes of new file.
1264789Sahrens  *		excl	- flag indicating exclusive or non-exclusive mode.
1265789Sahrens  *		mode	- mode to open file with.
1266789Sahrens  *		cr	- credentials of caller.
1267789Sahrens  *		flag	- large file flag [UNUSED].
12685331Samw  *		ct	- caller context
12695331Samw  *		vsecp 	- ACL to be set
1270789Sahrens  *
1271789Sahrens  *	OUT:	vpp	- vnode of created or trunc'd entry.
1272789Sahrens  *
1273789Sahrens  *	RETURN:	0 if success
1274789Sahrens  *		error code if failure
1275789Sahrens  *
1276789Sahrens  * Timestamps:
1277789Sahrens  *	dvp - ctime|mtime updated if new entry created
1278789Sahrens  *	 vp - ctime|mtime always, atime if new
1279789Sahrens  */
12805331Samw 
1281789Sahrens /* ARGSUSED */
1282789Sahrens static int
1283789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
12845331Samw     int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
12855331Samw     vsecattr_t *vsecp)
1286789Sahrens {
1287789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1288789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
12895326Sek110237 	zilog_t		*zilog;
12905326Sek110237 	objset_t	*os;
1291789Sahrens 	zfs_dirlock_t	*dl;
1292789Sahrens 	dmu_tx_t	*tx;
1293789Sahrens 	int		error;
12947847SMark.Shellenbaum@Sun.COM 	ksid_t		*ksid;
12957847SMark.Shellenbaum@Sun.COM 	uid_t		uid;
12967847SMark.Shellenbaum@Sun.COM 	gid_t		gid = crgetgid(cr);
129711935SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t   acl_ids;
12989179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
12995331Samw 
13005331Samw 	/*
13015331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
13025331Samw 	 * make sure file system is at proper version
13035331Samw 	 */
13045331Samw 
13057847SMark.Shellenbaum@Sun.COM 	ksid = crgetsid(cr, KSID_OWNER);
13067847SMark.Shellenbaum@Sun.COM 	if (ksid)
13077847SMark.Shellenbaum@Sun.COM 		uid = ksid_getid(ksid);
13087847SMark.Shellenbaum@Sun.COM 	else
13097847SMark.Shellenbaum@Sun.COM 		uid = crgetuid(cr);
13107847SMark.Shellenbaum@Sun.COM 
13115331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
13125331Samw 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
13137847SMark.Shellenbaum@Sun.COM 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
13145331Samw 		return (EINVAL);
1315789Sahrens 
13165367Sahrens 	ZFS_ENTER(zfsvfs);
13175367Sahrens 	ZFS_VERIFY_ZP(dzp);
13185326Sek110237 	os = zfsvfs->z_os;
13195326Sek110237 	zilog = zfsvfs->z_log;
1320789Sahrens 
13215498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
13225331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
13235331Samw 		ZFS_EXIT(zfsvfs);
13245331Samw 		return (EILSEQ);
13255331Samw 	}
13265331Samw 
13275331Samw 	if (vap->va_mask & AT_XVATTR) {
13285331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
13295331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
13305331Samw 			ZFS_EXIT(zfsvfs);
13315331Samw 			return (error);
13325331Samw 		}
13335331Samw 	}
1334789Sahrens top:
1335789Sahrens 	*vpp = NULL;
1336789Sahrens 
1337789Sahrens 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1338789Sahrens 		vap->va_mode &= ~VSVTX;
1339789Sahrens 
1340789Sahrens 	if (*name == '\0') {
1341789Sahrens 		/*
1342789Sahrens 		 * Null component name refers to the directory itself.
1343789Sahrens 		 */
1344789Sahrens 		VN_HOLD(dvp);
1345789Sahrens 		zp = dzp;
1346789Sahrens 		dl = NULL;
1347789Sahrens 		error = 0;
1348789Sahrens 	} else {
1349789Sahrens 		/* possible VN_HOLD(zp) */
13505331Samw 		int zflg = 0;
13515331Samw 
13525331Samw 		if (flag & FIGNORECASE)
13535331Samw 			zflg |= ZCILOOK;
13545331Samw 
13555331Samw 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
13565331Samw 		    NULL, NULL);
13575331Samw 		if (error) {
1358789Sahrens 			if (strcmp(name, "..") == 0)
1359789Sahrens 				error = EISDIR;
1360789Sahrens 			ZFS_EXIT(zfsvfs);
1361789Sahrens 			return (error);
1362789Sahrens 		}
1363789Sahrens 	}
136411935SMark.Shellenbaum@Sun.COM 
1365789Sahrens 	if (zp == NULL) {
13665331Samw 		uint64_t txtype;
13675331Samw 
1368789Sahrens 		/*
1369789Sahrens 		 * Create a new file object and update the directory
1370789Sahrens 		 * to reference it.
1371789Sahrens 		 */
13725331Samw 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1373789Sahrens 			goto out;
1374789Sahrens 		}
1375789Sahrens 
1376789Sahrens 		/*
1377789Sahrens 		 * We only support the creation of regular files in
1378789Sahrens 		 * extended attribute directories.
1379789Sahrens 		 */
138011935SMark.Shellenbaum@Sun.COM 
138111935SMark.Shellenbaum@Sun.COM 		if ((dzp->z_pflags & ZFS_XATTR) &&
1382789Sahrens 		    (vap->va_type != VREG)) {
1383789Sahrens 			error = EINVAL;
1384789Sahrens 			goto out;
1385789Sahrens 		}
1386789Sahrens 
13879179SMark.Shellenbaum@Sun.COM 		if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
13889179SMark.Shellenbaum@Sun.COM 		    &acl_ids)) != 0)
13899179SMark.Shellenbaum@Sun.COM 			goto out;
13909396SMatthew.Ahrens@Sun.COM 		if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
139110143STim.Haley@Sun.COM 			zfs_acl_ids_free(&acl_ids);
13929396SMatthew.Ahrens@Sun.COM 			error = EDQUOT;
13939396SMatthew.Ahrens@Sun.COM 			goto out;
13949396SMatthew.Ahrens@Sun.COM 		}
13959179SMark.Shellenbaum@Sun.COM 
1396789Sahrens 		tx = dmu_tx_create(os);
139711935SMark.Shellenbaum@Sun.COM 
139811935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
139911935SMark.Shellenbaum@Sun.COM 		    ZFS_SA_BASE_ATTR_SIZE);
140011935SMark.Shellenbaum@Sun.COM 
14019179SMark.Shellenbaum@Sun.COM 		fuid_dirtied = zfsvfs->z_fuid_dirty;
14029396SMatthew.Ahrens@Sun.COM 		if (fuid_dirtied)
14039396SMatthew.Ahrens@Sun.COM 			zfs_fuid_txhold(zfsvfs, tx);
14041544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
140511935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
140611935SMark.Shellenbaum@Sun.COM 		if (!zfsvfs->z_use_sa &&
140711935SMark.Shellenbaum@Sun.COM 		    acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1408789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
140911935SMark.Shellenbaum@Sun.COM 			    0, acl_ids.z_aclp->z_acl_bytes);
14105331Samw 		}
14118227SNeil.Perrin@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
1412789Sahrens 		if (error) {
14139179SMark.Shellenbaum@Sun.COM 			zfs_acl_ids_free(&acl_ids);
1414789Sahrens 			zfs_dirent_unlock(dl);
14158227SNeil.Perrin@Sun.COM 			if (error == ERESTART) {
14162113Sahrens 				dmu_tx_wait(tx);
14172113Sahrens 				dmu_tx_abort(tx);
1418789Sahrens 				goto top;
1419789Sahrens 			}
14202113Sahrens 			dmu_tx_abort(tx);
1421789Sahrens 			ZFS_EXIT(zfsvfs);
1422789Sahrens 			return (error);
1423789Sahrens 		}
142411935SMark.Shellenbaum@Sun.COM 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
14259179SMark.Shellenbaum@Sun.COM 
14269179SMark.Shellenbaum@Sun.COM 		if (fuid_dirtied)
14279179SMark.Shellenbaum@Sun.COM 			zfs_fuid_sync(zfsvfs, tx);
14289179SMark.Shellenbaum@Sun.COM 
1429789Sahrens 		(void) zfs_link_create(dl, zp, tx, ZNEW);
14305331Samw 		txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
14315331Samw 		if (flag & FIGNORECASE)
14325331Samw 			txtype |= TX_CI;
14335331Samw 		zfs_log_create(zilog, tx, txtype, dzp, zp, name,
14349179SMark.Shellenbaum@Sun.COM 		    vsecp, acl_ids.z_fuidp, vap);
14359179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
1436789Sahrens 		dmu_tx_commit(tx);
1437789Sahrens 	} else {
14385331Samw 		int aflags = (flag & FAPPEND) ? V_APPEND : 0;
14395331Samw 
1440789Sahrens 		/*
1441789Sahrens 		 * A directory entry already exists for this name.
1442789Sahrens 		 */
1443789Sahrens 		/*
1444789Sahrens 		 * Can't truncate an existing file if in exclusive mode.
1445789Sahrens 		 */
1446789Sahrens 		if (excl == EXCL) {
1447789Sahrens 			error = EEXIST;
1448789Sahrens 			goto out;
1449789Sahrens 		}
1450789Sahrens 		/*
1451789Sahrens 		 * Can't open a directory for writing.
1452789Sahrens 		 */
1453789Sahrens 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1454789Sahrens 			error = EISDIR;
1455789Sahrens 			goto out;
1456789Sahrens 		}
1457789Sahrens 		/*
1458789Sahrens 		 * Verify requested access to file.
1459789Sahrens 		 */
14605331Samw 		if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1461789Sahrens 			goto out;
1462789Sahrens 		}
1463789Sahrens 
1464789Sahrens 		mutex_enter(&dzp->z_lock);
1465789Sahrens 		dzp->z_seq++;
1466789Sahrens 		mutex_exit(&dzp->z_lock);
1467789Sahrens 
14681878Smaybee 		/*
14691878Smaybee 		 * Truncate regular files if requested.
14701878Smaybee 		 */
14711878Smaybee 		if ((ZTOV(zp)->v_type == VREG) &&
1472789Sahrens 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
14736992Smaybee 			/* we can't hold any locks when calling zfs_freesp() */
14746992Smaybee 			zfs_dirent_unlock(dl);
14756992Smaybee 			dl = NULL;
14761878Smaybee 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
14774863Spraks 			if (error == 0) {
14785331Samw 				vnevent_create(ZTOV(zp), ct);
14794863Spraks 			}
1480789Sahrens 		}
1481789Sahrens 	}
1482789Sahrens out:
1483789Sahrens 
1484789Sahrens 	if (dl)
1485789Sahrens 		zfs_dirent_unlock(dl);
1486789Sahrens 
1487789Sahrens 	if (error) {
1488789Sahrens 		if (zp)
1489789Sahrens 			VN_RELE(ZTOV(zp));
1490789Sahrens 	} else {
1491789Sahrens 		*vpp = ZTOV(zp);
14929981STim.Haley@Sun.COM 		error = specvp_check(vpp, cr);
1493789Sahrens 	}
1494789Sahrens 
1495*12294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1496*12294SMark.Musante@Sun.COM 		zil_commit(zilog, UINT64_MAX, 0);
1497*12294SMark.Musante@Sun.COM 
1498789Sahrens 	ZFS_EXIT(zfsvfs);
1499789Sahrens 	return (error);
1500789Sahrens }
1501789Sahrens 
1502789Sahrens /*
1503789Sahrens  * Remove an entry from a directory.
1504789Sahrens  *
1505789Sahrens  *	IN:	dvp	- vnode of directory to remove entry from.
1506789Sahrens  *		name	- name of entry to remove.
1507789Sahrens  *		cr	- credentials of caller.
15085331Samw  *		ct	- caller context
15095331Samw  *		flags	- case flags
1510789Sahrens  *
1511789Sahrens  *	RETURN:	0 if success
1512789Sahrens  *		error code if failure
1513789Sahrens  *
1514789Sahrens  * Timestamps:
1515789Sahrens  *	dvp - ctime|mtime
1516789Sahrens  *	 vp - ctime (if nlink > 0)
1517789Sahrens  */
151811935SMark.Shellenbaum@Sun.COM 
151911935SMark.Shellenbaum@Sun.COM uint64_t null_xattr = 0;
152011935SMark.Shellenbaum@Sun.COM 
15215331Samw /*ARGSUSED*/
1522789Sahrens static int
15235331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
15245331Samw     int flags)
1525789Sahrens {
1526789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1527789Sahrens 	znode_t		*xzp = NULL;
1528789Sahrens 	vnode_t		*vp;
1529789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
15305326Sek110237 	zilog_t		*zilog;
153111935SMark.Shellenbaum@Sun.COM 	uint64_t	acl_obj, xattr_obj = 0;
153211935SMark.Shellenbaum@Sun.COM 	uint64_t 	xattr_obj_unlinked = 0;
1533789Sahrens 	zfs_dirlock_t	*dl;
1534789Sahrens 	dmu_tx_t	*tx;
15353461Sahrens 	boolean_t	may_delete_now, delete_now = FALSE;
15366992Smaybee 	boolean_t	unlinked, toobig = FALSE;
15375331Samw 	uint64_t	txtype;
15385331Samw 	pathname_t	*realnmp = NULL;
15395331Samw 	pathname_t	realnm;
1540789Sahrens 	int		error;
15415331Samw 	int		zflg = ZEXISTS;
1542789Sahrens 
15435367Sahrens 	ZFS_ENTER(zfsvfs);
15445367Sahrens 	ZFS_VERIFY_ZP(dzp);
15455326Sek110237 	zilog = zfsvfs->z_log;
1546789Sahrens 
15475331Samw 	if (flags & FIGNORECASE) {
15485331Samw 		zflg |= ZCILOOK;
15495331Samw 		pn_alloc(&realnm);
15505331Samw 		realnmp = &realnm;
15515331Samw 	}
15525331Samw 
1553789Sahrens top:
1554789Sahrens 	/*
1555789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1556789Sahrens 	 */
15575331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
15585331Samw 	    NULL, realnmp)) {
15595331Samw 		if (realnmp)
15605331Samw 			pn_free(realnmp);
1561789Sahrens 		ZFS_EXIT(zfsvfs);
1562789Sahrens 		return (error);
1563789Sahrens 	}
1564789Sahrens 
1565789Sahrens 	vp = ZTOV(zp);
1566789Sahrens 
1567789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1568789Sahrens 		goto out;
1569789Sahrens 	}
1570789Sahrens 
1571789Sahrens 	/*
1572789Sahrens 	 * Need to use rmdir for removing directories.
1573789Sahrens 	 */
1574789Sahrens 	if (vp->v_type == VDIR) {
1575789Sahrens 		error = EPERM;
1576789Sahrens 		goto out;
1577789Sahrens 	}
1578789Sahrens 
15795331Samw 	vnevent_remove(vp, dvp, name, ct);
15805331Samw 
15815331Samw 	if (realnmp)
15826492Stimh 		dnlc_remove(dvp, realnmp->pn_buf);
15835331Samw 	else
15845331Samw 		dnlc_remove(dvp, name);
15851484Sek110237 
1586789Sahrens 	mutex_enter(&vp->v_lock);
1587789Sahrens 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1588789Sahrens 	mutex_exit(&vp->v_lock);
1589789Sahrens 
1590789Sahrens 	/*
15913461Sahrens 	 * We may delete the znode now, or we may put it in the unlinked set;
1592789Sahrens 	 * it depends on whether we're the last link, and on whether there are
1593789Sahrens 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1594789Sahrens 	 * allow for either case.
1595789Sahrens 	 */
1596789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
15971544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
159811935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
159911935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
160011935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, dzp);
16016992Smaybee 	if (may_delete_now) {
16026992Smaybee 		toobig =
160311935SMark.Shellenbaum@Sun.COM 		    zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
16046992Smaybee 		/* if the file is too big, only hold_free a token amount */
16056992Smaybee 		dmu_tx_hold_free(tx, zp->z_id, 0,
16066992Smaybee 		    (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
16076992Smaybee 	}
1608789Sahrens 
1609789Sahrens 	/* are there any extended attributes? */
161011935SMark.Shellenbaum@Sun.COM 	error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
161111935SMark.Shellenbaum@Sun.COM 	    &xattr_obj, sizeof (xattr_obj));
161211935SMark.Shellenbaum@Sun.COM 	if (xattr_obj) {
161311935SMark.Shellenbaum@Sun.COM 		error = zfs_zget(zfsvfs, xattr_obj, &xzp);
161411935SMark.Shellenbaum@Sun.COM 		ASSERT3U(error, ==, 0);
161511935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
161611935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1617789Sahrens 	}
1618789Sahrens 
1619789Sahrens 	/* are there any additional acls */
162011935SMark.Shellenbaum@Sun.COM 	if ((acl_obj = ZFS_EXTERNAL_ACL(zp)) != 0 && may_delete_now)
1621789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1622789Sahrens 
1623789Sahrens 	/* charge as an update -- would be nice not to charge at all */
16243461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1625789Sahrens 
16268227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1627789Sahrens 	if (error) {
1628789Sahrens 		zfs_dirent_unlock(dl);
1629789Sahrens 		VN_RELE(vp);
16308227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
16312113Sahrens 			dmu_tx_wait(tx);
16322113Sahrens 			dmu_tx_abort(tx);
1633789Sahrens 			goto top;
1634789Sahrens 		}
16355331Samw 		if (realnmp)
16365331Samw 			pn_free(realnmp);
16372113Sahrens 		dmu_tx_abort(tx);
1638789Sahrens 		ZFS_EXIT(zfsvfs);
1639789Sahrens 		return (error);
1640789Sahrens 	}
1641789Sahrens 
1642789Sahrens 	/*
1643789Sahrens 	 * Remove the directory entry.
1644789Sahrens 	 */
16455331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1646789Sahrens 
1647789Sahrens 	if (error) {
1648789Sahrens 		dmu_tx_commit(tx);
1649789Sahrens 		goto out;
1650789Sahrens 	}
1651789Sahrens 
16523461Sahrens 	if (unlinked) {
165311935SMark.Shellenbaum@Sun.COM 
1654789Sahrens 		mutex_enter(&vp->v_lock);
165511935SMark.Shellenbaum@Sun.COM 
165611935SMark.Shellenbaum@Sun.COM 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
165711935SMark.Shellenbaum@Sun.COM 		    &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
16586992Smaybee 		delete_now = may_delete_now && !toobig &&
1659789Sahrens 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
166011935SMark.Shellenbaum@Sun.COM 		    xattr_obj == xattr_obj_unlinked && ZFS_EXTERNAL_ACL(zp) ==
166111935SMark.Shellenbaum@Sun.COM 		    acl_obj;
1662789Sahrens 		mutex_exit(&vp->v_lock);
1663789Sahrens 	}
1664789Sahrens 
1665789Sahrens 	if (delete_now) {
166611935SMark.Shellenbaum@Sun.COM 		if (xattr_obj_unlinked) {
166711935SMark.Shellenbaum@Sun.COM 			ASSERT3U(xzp->z_links, ==, 2);
1668789Sahrens 			mutex_enter(&xzp->z_lock);
16693461Sahrens 			xzp->z_unlinked = 1;
167011935SMark.Shellenbaum@Sun.COM 			xzp->z_links = 0;
167111935SMark.Shellenbaum@Sun.COM 			error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
167211935SMark.Shellenbaum@Sun.COM 			    &xzp->z_links, sizeof (xzp->z_links), tx);
167311935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error,  ==,  0);
1674789Sahrens 			mutex_exit(&xzp->z_lock);
16753461Sahrens 			zfs_unlinked_add(xzp, tx);
167611935SMark.Shellenbaum@Sun.COM 			if (zp->z_is_sa)
167711935SMark.Shellenbaum@Sun.COM 				error = sa_remove(zp->z_sa_hdl,
167811935SMark.Shellenbaum@Sun.COM 				    SA_ZPL_XATTR(zfsvfs), tx);
167911935SMark.Shellenbaum@Sun.COM 			else
168011935SMark.Shellenbaum@Sun.COM 				error = sa_update(zp->z_sa_hdl,
168111935SMark.Shellenbaum@Sun.COM 				    SA_ZPL_XATTR(zfsvfs), &null_xattr,
168211935SMark.Shellenbaum@Sun.COM 				    sizeof (uint64_t), tx);
168311935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error, ==, 0);
1684789Sahrens 		}
1685789Sahrens 		mutex_enter(&zp->z_lock);
1686789Sahrens 		mutex_enter(&vp->v_lock);
1687789Sahrens 		vp->v_count--;
1688789Sahrens 		ASSERT3U(vp->v_count, ==, 0);
1689789Sahrens 		mutex_exit(&vp->v_lock);
1690789Sahrens 		mutex_exit(&zp->z_lock);
1691789Sahrens 		zfs_znode_delete(zp, tx);
16923461Sahrens 	} else if (unlinked) {
16933461Sahrens 		zfs_unlinked_add(zp, tx);
1694789Sahrens 	}
1695789Sahrens 
16965331Samw 	txtype = TX_REMOVE;
16975331Samw 	if (flags & FIGNORECASE)
16985331Samw 		txtype |= TX_CI;
16995331Samw 	zfs_log_remove(zilog, tx, txtype, dzp, name);
1700789Sahrens 
1701789Sahrens 	dmu_tx_commit(tx);
1702789Sahrens out:
17035331Samw 	if (realnmp)
17045331Samw 		pn_free(realnmp);
17055331Samw 
1706789Sahrens 	zfs_dirent_unlock(dl);
1707789Sahrens 
170812178SMark.Shellenbaum@Sun.COM 	if (!delete_now)
1709789Sahrens 		VN_RELE(vp);
171012178SMark.Shellenbaum@Sun.COM 	if (xzp)
1711789Sahrens 		VN_RELE(ZTOV(xzp));
1712789Sahrens 
1713*12294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1714*12294SMark.Musante@Sun.COM 		zil_commit(zilog, UINT64_MAX, 0);
1715*12294SMark.Musante@Sun.COM 
1716789Sahrens 	ZFS_EXIT(zfsvfs);
1717789Sahrens 	return (error);
1718789Sahrens }
1719789Sahrens 
1720789Sahrens /*
1721789Sahrens  * Create a new directory and insert it into dvp using the name
1722789Sahrens  * provided.  Return a pointer to the inserted directory.
1723789Sahrens  *
1724789Sahrens  *	IN:	dvp	- vnode of directory to add subdir to.
1725789Sahrens  *		dirname	- name of new directory.
1726789Sahrens  *		vap	- attributes of new directory.
1727789Sahrens  *		cr	- credentials of caller.
17285331Samw  *		ct	- caller context
17295331Samw  *		vsecp	- ACL to be set
1730789Sahrens  *
1731789Sahrens  *	OUT:	vpp	- vnode of created directory.
1732789Sahrens  *
1733789Sahrens  *	RETURN:	0 if success
1734789Sahrens  *		error code if failure
1735789Sahrens  *
1736789Sahrens  * Timestamps:
1737789Sahrens  *	dvp - ctime|mtime updated
1738789Sahrens  *	 vp - ctime|mtime|atime updated
1739789Sahrens  */
17405331Samw /*ARGSUSED*/
1741789Sahrens static int
17425331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
17435331Samw     caller_context_t *ct, int flags, vsecattr_t *vsecp)
1744789Sahrens {
1745789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1746789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
17475326Sek110237 	zilog_t		*zilog;
1748789Sahrens 	zfs_dirlock_t	*dl;
17495331Samw 	uint64_t	txtype;
1750789Sahrens 	dmu_tx_t	*tx;
1751789Sahrens 	int		error;
17525331Samw 	int		zf = ZNEW;
17537847SMark.Shellenbaum@Sun.COM 	ksid_t		*ksid;
17547847SMark.Shellenbaum@Sun.COM 	uid_t		uid;
17557847SMark.Shellenbaum@Sun.COM 	gid_t		gid = crgetgid(cr);
175611935SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t   acl_ids;
17579179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
1758789Sahrens 
1759789Sahrens 	ASSERT(vap->va_type == VDIR);
1760789Sahrens 
17615331Samw 	/*
17625331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
17635331Samw 	 * make sure file system is at proper version
17645331Samw 	 */
17655331Samw 
17667847SMark.Shellenbaum@Sun.COM 	ksid = crgetsid(cr, KSID_OWNER);
17677847SMark.Shellenbaum@Sun.COM 	if (ksid)
17687847SMark.Shellenbaum@Sun.COM 		uid = ksid_getid(ksid);
17697847SMark.Shellenbaum@Sun.COM 	else
17707847SMark.Shellenbaum@Sun.COM 		uid = crgetuid(cr);
17715331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
17727847SMark.Shellenbaum@Sun.COM 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
17737876SMark.Shellenbaum@Sun.COM 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
17745331Samw 		return (EINVAL);
17755331Samw 
17765367Sahrens 	ZFS_ENTER(zfsvfs);
17775367Sahrens 	ZFS_VERIFY_ZP(dzp);
17785326Sek110237 	zilog = zfsvfs->z_log;
1779789Sahrens 
178011935SMark.Shellenbaum@Sun.COM 	if (dzp->z_pflags & ZFS_XATTR) {
1781789Sahrens 		ZFS_EXIT(zfsvfs);
1782789Sahrens 		return (EINVAL);
1783789Sahrens 	}
17845331Samw 
17855498Stimh 	if (zfsvfs->z_utf8 && u8_validate(dirname,
17865331Samw 	    strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
17875331Samw 		ZFS_EXIT(zfsvfs);
17885331Samw 		return (EILSEQ);
17895331Samw 	}
17905331Samw 	if (flags & FIGNORECASE)
17915331Samw 		zf |= ZCILOOK;
17925331Samw 
17935331Samw 	if (vap->va_mask & AT_XVATTR)
17945331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
17955331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
17965331Samw 			ZFS_EXIT(zfsvfs);
17975331Samw 			return (error);
17985331Samw 		}
1799789Sahrens 
1800789Sahrens 	/*
1801789Sahrens 	 * First make sure the new directory doesn't exist.
1802789Sahrens 	 */
18035331Samw top:
18045331Samw 	*vpp = NULL;
18055331Samw 
18065331Samw 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
18075331Samw 	    NULL, NULL)) {
1808789Sahrens 		ZFS_EXIT(zfsvfs);
1809789Sahrens 		return (error);
1810789Sahrens 	}
1811789Sahrens 
18125331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
18131231Smarks 		zfs_dirent_unlock(dl);
18141231Smarks 		ZFS_EXIT(zfsvfs);
18151231Smarks 		return (error);
18161231Smarks 	}
18171231Smarks 
18189179SMark.Shellenbaum@Sun.COM 	if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
18199179SMark.Shellenbaum@Sun.COM 	    &acl_ids)) != 0) {
18209179SMark.Shellenbaum@Sun.COM 		zfs_dirent_unlock(dl);
18219179SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
18229179SMark.Shellenbaum@Sun.COM 		return (error);
18235331Samw 	}
18249396SMatthew.Ahrens@Sun.COM 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
182510143STim.Haley@Sun.COM 		zfs_acl_ids_free(&acl_ids);
18269396SMatthew.Ahrens@Sun.COM 		zfs_dirent_unlock(dl);
18279396SMatthew.Ahrens@Sun.COM 		ZFS_EXIT(zfsvfs);
18289396SMatthew.Ahrens@Sun.COM 		return (EDQUOT);
18299396SMatthew.Ahrens@Sun.COM 	}
18309179SMark.Shellenbaum@Sun.COM 
1831789Sahrens 	/*
1832789Sahrens 	 * Add a new entry to the directory.
1833789Sahrens 	 */
1834789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
18351544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
18361544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
18379179SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
18389396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
18399396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
184011935SMark.Shellenbaum@Sun.COM 	if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
184111935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
184211935SMark.Shellenbaum@Sun.COM 		    acl_ids.z_aclp->z_acl_bytes);
184311935SMark.Shellenbaum@Sun.COM 	}
184411935SMark.Shellenbaum@Sun.COM 
184511935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
184611935SMark.Shellenbaum@Sun.COM 	    ZFS_SA_BASE_ATTR_SIZE);
184711935SMark.Shellenbaum@Sun.COM 
18488227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1849789Sahrens 	if (error) {
18509179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
1851789Sahrens 		zfs_dirent_unlock(dl);
18528227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
18532113Sahrens 			dmu_tx_wait(tx);
18542113Sahrens 			dmu_tx_abort(tx);
1855789Sahrens 			goto top;
1856789Sahrens 		}
18572113Sahrens 		dmu_tx_abort(tx);
1858789Sahrens 		ZFS_EXIT(zfsvfs);
1859789Sahrens 		return (error);
1860789Sahrens 	}
1861789Sahrens 
1862789Sahrens 	/*
1863789Sahrens 	 * Create new node.
1864789Sahrens 	 */
186511935SMark.Shellenbaum@Sun.COM 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
18669179SMark.Shellenbaum@Sun.COM 
18679179SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
18689179SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
186911935SMark.Shellenbaum@Sun.COM 
1870789Sahrens 	/*
1871789Sahrens 	 * Now put new name in parent dir.
1872789Sahrens 	 */
1873789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1874789Sahrens 
1875789Sahrens 	*vpp = ZTOV(zp);
1876789Sahrens 
18775331Samw 	txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
18785331Samw 	if (flags & FIGNORECASE)
18795331Samw 		txtype |= TX_CI;
18809179SMark.Shellenbaum@Sun.COM 	zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
18819179SMark.Shellenbaum@Sun.COM 	    acl_ids.z_fuidp, vap);
18829179SMark.Shellenbaum@Sun.COM 
18839179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_free(&acl_ids);
188411935SMark.Shellenbaum@Sun.COM 
1885789Sahrens 	dmu_tx_commit(tx);
1886789Sahrens 
1887789Sahrens 	zfs_dirent_unlock(dl);
1888789Sahrens 
1889*12294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1890*12294SMark.Musante@Sun.COM 		zil_commit(zilog, UINT64_MAX, 0);
1891*12294SMark.Musante@Sun.COM 
1892789Sahrens 	ZFS_EXIT(zfsvfs);
1893789Sahrens 	return (0);
1894789Sahrens }
1895789Sahrens 
1896789Sahrens /*
1897789Sahrens  * Remove a directory subdir entry.  If the current working
1898789Sahrens  * directory is the same as the subdir to be removed, the
1899789Sahrens  * remove will fail.
1900789Sahrens  *
1901789Sahrens  *	IN:	dvp	- vnode of directory to remove from.
1902789Sahrens  *		name	- name of directory to be removed.
1903789Sahrens  *		cwd	- vnode of current working directory.
1904789Sahrens  *		cr	- credentials of caller.
19055331Samw  *		ct	- caller context
19065331Samw  *		flags	- case flags
1907789Sahrens  *
1908789Sahrens  *	RETURN:	0 if success
1909789Sahrens  *		error code if failure
1910789Sahrens  *
1911789Sahrens  * Timestamps:
1912789Sahrens  *	dvp - ctime|mtime updated
1913789Sahrens  */
19145331Samw /*ARGSUSED*/
1915789Sahrens static int
19165331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
19175331Samw     caller_context_t *ct, int flags)
1918789Sahrens {
1919789Sahrens 	znode_t		*dzp = VTOZ(dvp);
1920789Sahrens 	znode_t		*zp;
1921789Sahrens 	vnode_t		*vp;
1922789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
19235326Sek110237 	zilog_t		*zilog;
1924789Sahrens 	zfs_dirlock_t	*dl;
1925789Sahrens 	dmu_tx_t	*tx;
1926789Sahrens 	int		error;
19275331Samw 	int		zflg = ZEXISTS;
1928789Sahrens 
19295367Sahrens 	ZFS_ENTER(zfsvfs);
19305367Sahrens 	ZFS_VERIFY_ZP(dzp);
19315326Sek110237 	zilog = zfsvfs->z_log;
1932789Sahrens 
19335331Samw 	if (flags & FIGNORECASE)
19345331Samw 		zflg |= ZCILOOK;
1935789Sahrens top:
1936789Sahrens 	zp = NULL;
1937789Sahrens 
1938789Sahrens 	/*
1939789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1940789Sahrens 	 */
19415331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
19425331Samw 	    NULL, NULL)) {
1943789Sahrens 		ZFS_EXIT(zfsvfs);
1944789Sahrens 		return (error);
1945789Sahrens 	}
1946789Sahrens 
1947789Sahrens 	vp = ZTOV(zp);
1948789Sahrens 
1949789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1950789Sahrens 		goto out;
1951789Sahrens 	}
1952789Sahrens 
1953789Sahrens 	if (vp->v_type != VDIR) {
1954789Sahrens 		error = ENOTDIR;
1955789Sahrens 		goto out;
1956789Sahrens 	}
1957789Sahrens 
1958789Sahrens 	if (vp == cwd) {
1959789Sahrens 		error = EINVAL;
1960789Sahrens 		goto out;
1961789Sahrens 	}
1962789Sahrens 
19635331Samw 	vnevent_rmdir(vp, dvp, name, ct);
1964789Sahrens 
1965789Sahrens 	/*
19663897Smaybee 	 * Grab a lock on the directory to make sure that noone is
19673897Smaybee 	 * trying to add (or lookup) entries while we are removing it.
19683897Smaybee 	 */
19693897Smaybee 	rw_enter(&zp->z_name_lock, RW_WRITER);
19703897Smaybee 
19713897Smaybee 	/*
19723897Smaybee 	 * Grab a lock on the parent pointer to make sure we play well
1973789Sahrens 	 * with the treewalk and directory rename code.
1974789Sahrens 	 */
1975789Sahrens 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1976789Sahrens 
1977789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
19781544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
197911935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
19803461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
198111935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
198211935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, dzp);
19838227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1984789Sahrens 	if (error) {
1985789Sahrens 		rw_exit(&zp->z_parent_lock);
19863897Smaybee 		rw_exit(&zp->z_name_lock);
1987789Sahrens 		zfs_dirent_unlock(dl);
1988789Sahrens 		VN_RELE(vp);
19898227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
19902113Sahrens 			dmu_tx_wait(tx);
19912113Sahrens 			dmu_tx_abort(tx);
1992789Sahrens 			goto top;
1993789Sahrens 		}
19942113Sahrens 		dmu_tx_abort(tx);
1995789Sahrens 		ZFS_EXIT(zfsvfs);
1996789Sahrens 		return (error);
1997789Sahrens 	}
1998789Sahrens 
19995331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
20005331Samw 
20015331Samw 	if (error == 0) {
20025331Samw 		uint64_t txtype = TX_RMDIR;
20035331Samw 		if (flags & FIGNORECASE)
20045331Samw 			txtype |= TX_CI;
20055331Samw 		zfs_log_remove(zilog, tx, txtype, dzp, name);
20065331Samw 	}
2007789Sahrens 
2008789Sahrens 	dmu_tx_commit(tx);
2009789Sahrens 
2010789Sahrens 	rw_exit(&zp->z_parent_lock);
20113897Smaybee 	rw_exit(&zp->z_name_lock);
2012789Sahrens out:
2013789Sahrens 	zfs_dirent_unlock(dl);
2014789Sahrens 
2015789Sahrens 	VN_RELE(vp);
2016789Sahrens 
2017*12294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2018*12294SMark.Musante@Sun.COM 		zil_commit(zilog, UINT64_MAX, 0);
2019*12294SMark.Musante@Sun.COM 
2020789Sahrens 	ZFS_EXIT(zfsvfs);
2021789Sahrens 	return (error);
2022789Sahrens }
2023789Sahrens 
2024789Sahrens /*
2025789Sahrens  * Read as many directory entries as will fit into the provided
2026789Sahrens  * buffer from the given directory cursor position (specified in
2027789Sahrens  * the uio structure.
2028789Sahrens  *
2029789Sahrens  *	IN:	vp	- vnode of directory to read.
2030789Sahrens  *		uio	- structure supplying read location, range info,
2031789Sahrens  *			  and return buffer.
2032789Sahrens  *		cr	- credentials of caller.
20335331Samw  *		ct	- caller context
20345331Samw  *		flags	- case flags
2035789Sahrens  *
2036789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
2037789Sahrens  *		eofp	- set to true if end-of-file detected.
2038789Sahrens  *
2039789Sahrens  *	RETURN:	0 if success
2040789Sahrens  *		error code if failure
2041789Sahrens  *
2042789Sahrens  * Timestamps:
2043789Sahrens  *	vp - atime updated
2044789Sahrens  *
2045789Sahrens  * Note that the low 4 bits of the cookie returned by zap is always zero.
2046789Sahrens  * This allows us to use the low range for "special" directory entries:
2047789Sahrens  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
2048789Sahrens  * we use the offset 2 for the '.zfs' directory.
2049789Sahrens  */
2050789Sahrens /* ARGSUSED */
2051789Sahrens static int
20525331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
20535331Samw     caller_context_t *ct, int flags)
2054789Sahrens {
2055789Sahrens 	znode_t		*zp = VTOZ(vp);
2056789Sahrens 	iovec_t		*iovp;
20575331Samw 	edirent_t	*eodp;
2058789Sahrens 	dirent64_t	*odp;
2059789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2060869Sperrin 	objset_t	*os;
2061789Sahrens 	caddr_t		outbuf;
2062789Sahrens 	size_t		bufsize;
2063789Sahrens 	zap_cursor_t	zc;
2064789Sahrens 	zap_attribute_t	zap;
2065789Sahrens 	uint_t		bytes_wanted;
2066789Sahrens 	uint64_t	offset; /* must be unsigned; checks for < 1 */
206711935SMark.Shellenbaum@Sun.COM 	uint64_t	parent;
2068789Sahrens 	int		local_eof;
2069869Sperrin 	int		outcount;
2070869Sperrin 	int		error;
2071869Sperrin 	uint8_t		prefetch;
20725663Sck153898 	boolean_t	check_sysattrs;
2073789Sahrens 
20745367Sahrens 	ZFS_ENTER(zfsvfs);
20755367Sahrens 	ZFS_VERIFY_ZP(zp);
2076789Sahrens 
207711935SMark.Shellenbaum@Sun.COM 	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
207811935SMark.Shellenbaum@Sun.COM 	    &parent, sizeof (parent))) != 0) {
207911935SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
208011935SMark.Shellenbaum@Sun.COM 		return (error);
208111935SMark.Shellenbaum@Sun.COM 	}
208211935SMark.Shellenbaum@Sun.COM 
2083789Sahrens 	/*
2084789Sahrens 	 * If we are not given an eof variable,
2085789Sahrens 	 * use a local one.
2086789Sahrens 	 */
2087789Sahrens 	if (eofp == NULL)
2088789Sahrens 		eofp = &local_eof;
2089789Sahrens 
2090789Sahrens 	/*
2091789Sahrens 	 * Check for valid iov_len.
2092789Sahrens 	 */
2093789Sahrens 	if (uio->uio_iov->iov_len <= 0) {
2094789Sahrens 		ZFS_EXIT(zfsvfs);
2095789Sahrens 		return (EINVAL);
2096789Sahrens 	}
2097789Sahrens 
2098789Sahrens 	/*
2099789Sahrens 	 * Quit if directory has been removed (posix)
2100789Sahrens 	 */
21013461Sahrens 	if ((*eofp = zp->z_unlinked) != 0) {
2102789Sahrens 		ZFS_EXIT(zfsvfs);
2103789Sahrens 		return (0);
2104789Sahrens 	}
2105789Sahrens 
2106869Sperrin 	error = 0;
2107869Sperrin 	os = zfsvfs->z_os;
2108869Sperrin 	offset = uio->uio_loffset;
2109869Sperrin 	prefetch = zp->z_zn_prefetch;
2110869Sperrin 
2111789Sahrens 	/*
2112789Sahrens 	 * Initialize the iterator cursor.
2113789Sahrens 	 */
2114789Sahrens 	if (offset <= 3) {
2115789Sahrens 		/*
2116789Sahrens 		 * Start iteration from the beginning of the directory.
2117789Sahrens 		 */
2118869Sperrin 		zap_cursor_init(&zc, os, zp->z_id);
2119789Sahrens 	} else {
2120789Sahrens 		/*
2121789Sahrens 		 * The offset is a serialized cursor.
2122789Sahrens 		 */
2123869Sperrin 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2124789Sahrens 	}
2125789Sahrens 
2126789Sahrens 	/*
2127789Sahrens 	 * Get space to change directory entries into fs independent format.
2128789Sahrens 	 */
2129789Sahrens 	iovp = uio->uio_iov;
2130789Sahrens 	bytes_wanted = iovp->iov_len;
2131789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2132789Sahrens 		bufsize = bytes_wanted;
2133789Sahrens 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
2134789Sahrens 		odp = (struct dirent64 *)outbuf;
2135789Sahrens 	} else {
2136789Sahrens 		bufsize = bytes_wanted;
2137789Sahrens 		odp = (struct dirent64 *)iovp->iov_base;
2138789Sahrens 	}
21395331Samw 	eodp = (struct edirent *)odp;
2140789Sahrens 
2141789Sahrens 	/*
21427757SJanice.Chang@Sun.COM 	 * If this VFS supports the system attribute view interface; and
21437757SJanice.Chang@Sun.COM 	 * we're looking at an extended attribute directory; and we care
21447757SJanice.Chang@Sun.COM 	 * about normalization conflicts on this vfs; then we must check
21457757SJanice.Chang@Sun.COM 	 * for normalization conflicts with the sysattr name space.
21465663Sck153898 	 */
21477757SJanice.Chang@Sun.COM 	check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
21485663Sck153898 	    (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
21495663Sck153898 	    (flags & V_RDDIR_ENTFLAGS);
21505663Sck153898 
21515663Sck153898 	/*
2152789Sahrens 	 * Transform to file-system independent format
2153789Sahrens 	 */
2154789Sahrens 	outcount = 0;
2155789Sahrens 	while (outcount < bytes_wanted) {
21563912Slling 		ino64_t objnum;
21573912Slling 		ushort_t reclen;
21583912Slling 		off64_t *next;
21593912Slling 
2160789Sahrens 		/*
2161789Sahrens 		 * Special case `.', `..', and `.zfs'.
2162789Sahrens 		 */
2163789Sahrens 		if (offset == 0) {
2164789Sahrens 			(void) strcpy(zap.za_name, ".");
21655331Samw 			zap.za_normalization_conflict = 0;
21663912Slling 			objnum = zp->z_id;
2167789Sahrens 		} else if (offset == 1) {
2168789Sahrens 			(void) strcpy(zap.za_name, "..");
21695331Samw 			zap.za_normalization_conflict = 0;
217011935SMark.Shellenbaum@Sun.COM 			objnum = parent;
2171789Sahrens 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
2172789Sahrens 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
21735331Samw 			zap.za_normalization_conflict = 0;
21743912Slling 			objnum = ZFSCTL_INO_ROOT;
2175789Sahrens 		} else {
2176789Sahrens 			/*
2177789Sahrens 			 * Grab next entry.
2178789Sahrens 			 */
2179789Sahrens 			if (error = zap_cursor_retrieve(&zc, &zap)) {
2180789Sahrens 				if ((*eofp = (error == ENOENT)) != 0)
2181789Sahrens 					break;
2182789Sahrens 				else
2183789Sahrens 					goto update;
2184789Sahrens 			}
2185789Sahrens 
2186789Sahrens 			if (zap.za_integer_length != 8 ||
2187789Sahrens 			    zap.za_num_integers != 1) {
2188789Sahrens 				cmn_err(CE_WARN, "zap_readdir: bad directory "
2189789Sahrens 				    "entry, obj = %lld, offset = %lld\n",
2190789Sahrens 				    (u_longlong_t)zp->z_id,
2191789Sahrens 				    (u_longlong_t)offset);
2192789Sahrens 				error = ENXIO;
2193789Sahrens 				goto update;
2194789Sahrens 			}
21953912Slling 
21963912Slling 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
21973912Slling 			/*
21983912Slling 			 * MacOS X can extract the object type here such as:
21993912Slling 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
22003912Slling 			 */
22015663Sck153898 
22025663Sck153898 			if (check_sysattrs && !zap.za_normalization_conflict) {
22035663Sck153898 				zap.za_normalization_conflict =
22045663Sck153898 				    xattr_sysattr_casechk(zap.za_name);
22055663Sck153898 			}
2206789Sahrens 		}
22075331Samw 
22089749STim.Haley@Sun.COM 		if (flags & V_RDDIR_ACCFILTER) {
22099749STim.Haley@Sun.COM 			/*
22109749STim.Haley@Sun.COM 			 * If we have no access at all, don't include
22119749STim.Haley@Sun.COM 			 * this entry in the returned information
22129749STim.Haley@Sun.COM 			 */
22139749STim.Haley@Sun.COM 			znode_t	*ezp;
22149749STim.Haley@Sun.COM 			if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
22159749STim.Haley@Sun.COM 				goto skip_entry;
22169749STim.Haley@Sun.COM 			if (!zfs_has_access(ezp, cr)) {
22179749STim.Haley@Sun.COM 				VN_RELE(ZTOV(ezp));
22189749STim.Haley@Sun.COM 				goto skip_entry;
22199749STim.Haley@Sun.COM 			}
22209749STim.Haley@Sun.COM 			VN_RELE(ZTOV(ezp));
22219749STim.Haley@Sun.COM 		}
22229749STim.Haley@Sun.COM 
22235331Samw 		if (flags & V_RDDIR_ENTFLAGS)
22245331Samw 			reclen = EDIRENT_RECLEN(strlen(zap.za_name));
22255331Samw 		else
22265331Samw 			reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2227789Sahrens 
2228789Sahrens 		/*
2229789Sahrens 		 * Will this entry fit in the buffer?
2230789Sahrens 		 */
22313912Slling 		if (outcount + reclen > bufsize) {
2232789Sahrens 			/*
2233789Sahrens 			 * Did we manage to fit anything in the buffer?
2234789Sahrens 			 */
2235789Sahrens 			if (!outcount) {
2236789Sahrens 				error = EINVAL;
2237789Sahrens 				goto update;
2238789Sahrens 			}
2239789Sahrens 			break;
2240789Sahrens 		}
22415331Samw 		if (flags & V_RDDIR_ENTFLAGS) {
22425331Samw 			/*
22435331Samw 			 * Add extended flag entry:
22445331Samw 			 */
22455331Samw 			eodp->ed_ino = objnum;
22465331Samw 			eodp->ed_reclen = reclen;
22475331Samw 			/* NOTE: ed_off is the offset for the *next* entry */
22485331Samw 			next = &(eodp->ed_off);
22495331Samw 			eodp->ed_eflags = zap.za_normalization_conflict ?
22505331Samw 			    ED_CASE_CONFLICT : 0;
22515331Samw 			(void) strncpy(eodp->ed_name, zap.za_name,
22525331Samw 			    EDIRENT_NAMELEN(reclen));
22535331Samw 			eodp = (edirent_t *)((intptr_t)eodp + reclen);
22545331Samw 		} else {
22555331Samw 			/*
22565331Samw 			 * Add normal entry:
22575331Samw 			 */
22585331Samw 			odp->d_ino = objnum;
22595331Samw 			odp->d_reclen = reclen;
22605331Samw 			/* NOTE: d_off is the offset for the *next* entry */
22615331Samw 			next = &(odp->d_off);
22625331Samw 			(void) strncpy(odp->d_name, zap.za_name,
22635331Samw 			    DIRENT64_NAMELEN(reclen));
22645331Samw 			odp = (dirent64_t *)((intptr_t)odp + reclen);
22655331Samw 		}
22663912Slling 		outcount += reclen;
2267789Sahrens 
2268789Sahrens 		ASSERT(outcount <= bufsize);
2269789Sahrens 
2270789Sahrens 		/* Prefetch znode */
2271869Sperrin 		if (prefetch)
22723912Slling 			dmu_prefetch(os, objnum, 0, 0);
2273789Sahrens 
22749749STim.Haley@Sun.COM 	skip_entry:
2275789Sahrens 		/*
2276789Sahrens 		 * Move to the next entry, fill in the previous offset.
2277789Sahrens 		 */
2278789Sahrens 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2279789Sahrens 			zap_cursor_advance(&zc);
2280789Sahrens 			offset = zap_cursor_serialize(&zc);
2281789Sahrens 		} else {
2282789Sahrens 			offset += 1;
2283789Sahrens 		}
2284789Sahrens 		*next = offset;
2285789Sahrens 	}
2286869Sperrin 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2287789Sahrens 
2288789Sahrens 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2289789Sahrens 		iovp->iov_base += outcount;
2290789Sahrens 		iovp->iov_len -= outcount;
2291789Sahrens 		uio->uio_resid -= outcount;
2292789Sahrens 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2293789Sahrens 		/*
2294789Sahrens 		 * Reset the pointer.
2295789Sahrens 		 */
2296789Sahrens 		offset = uio->uio_loffset;
2297789Sahrens 	}
2298789Sahrens 
2299789Sahrens update:
2300885Sahrens 	zap_cursor_fini(&zc);
2301789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2302789Sahrens 		kmem_free(outbuf, bufsize);
2303789Sahrens 
2304789Sahrens 	if (error == ENOENT)
2305789Sahrens 		error = 0;
2306789Sahrens 
2307789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2308789Sahrens 
2309789Sahrens 	uio->uio_loffset = offset;
2310789Sahrens 	ZFS_EXIT(zfsvfs);
2311789Sahrens 	return (error);
2312789Sahrens }
2313789Sahrens 
23144720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4;
23154720Sfr157268 
2316789Sahrens static int
23175331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2318789Sahrens {
2319789Sahrens 	znode_t	*zp = VTOZ(vp);
2320789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2321789Sahrens 
23221773Seschrock 	/*
23231773Seschrock 	 * Regardless of whether this is required for standards conformance,
23241773Seschrock 	 * this is the logical behavior when fsync() is called on a file with
23251773Seschrock 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
23261773Seschrock 	 * going to be pushed out as part of the zil_commit().
23271773Seschrock 	 */
23281773Seschrock 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
23291773Seschrock 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
23305331Samw 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
23311773Seschrock 
23324720Sfr157268 	(void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
23334720Sfr157268 
2334*12294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2335*12294SMark.Musante@Sun.COM 		ZFS_ENTER(zfsvfs);
2336*12294SMark.Musante@Sun.COM 		ZFS_VERIFY_ZP(zp);
2337*12294SMark.Musante@Sun.COM 		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
2338*12294SMark.Musante@Sun.COM 		ZFS_EXIT(zfsvfs);
2339*12294SMark.Musante@Sun.COM 	}
2340789Sahrens 	return (0);
2341789Sahrens }
2342789Sahrens 
23435331Samw 
2344789Sahrens /*
2345789Sahrens  * Get the requested file attributes and place them in the provided
2346789Sahrens  * vattr structure.
2347789Sahrens  *
2348789Sahrens  *	IN:	vp	- vnode of file.
2349789Sahrens  *		vap	- va_mask identifies requested attributes.
23505331Samw  *			  If AT_XVATTR set, then optional attrs are requested
23515331Samw  *		flags	- ATTR_NOACLCHECK (CIFS server context)
2352789Sahrens  *		cr	- credentials of caller.
23535331Samw  *		ct	- caller context
2354789Sahrens  *
2355789Sahrens  *	OUT:	vap	- attribute values.
2356789Sahrens  *
2357789Sahrens  *	RETURN:	0 (always succeeds)
2358789Sahrens  */
2359789Sahrens /* ARGSUSED */
2360789Sahrens static int
23615331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
23625331Samw     caller_context_t *ct)
2363789Sahrens {
2364789Sahrens 	znode_t *zp = VTOZ(vp);
2365789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
23665331Samw 	int	error = 0;
23674543Smarks 	uint64_t links;
236811935SMark.Shellenbaum@Sun.COM 	uint64_t mtime[2], ctime[2];
23695331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
23705331Samw 	xoptattr_t *xoap = NULL;
23715331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
237211935SMark.Shellenbaum@Sun.COM 	sa_bulk_attr_t bulk[2];
237311935SMark.Shellenbaum@Sun.COM 	int count = 0;
2374789Sahrens 
23755367Sahrens 	ZFS_ENTER(zfsvfs);
23765367Sahrens 	ZFS_VERIFY_ZP(zp);
237711935SMark.Shellenbaum@Sun.COM 
237811935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
237911935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
238011935SMark.Shellenbaum@Sun.COM 
238111935SMark.Shellenbaum@Sun.COM 	if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
238211935SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
238311935SMark.Shellenbaum@Sun.COM 		return (error);
238411935SMark.Shellenbaum@Sun.COM 	}
2385789Sahrens 
23865331Samw 	/*
23875331Samw 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
23885331Samw 	 * Also, if we are the owner don't bother, since owner should
23895331Samw 	 * always be allowed to read basic attributes of file.
23905331Samw 	 */
239111935SMark.Shellenbaum@Sun.COM 	if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) && (zp->z_uid != crgetuid(cr))) {
23925331Samw 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
23935331Samw 		    skipaclchk, cr)) {
23945331Samw 			ZFS_EXIT(zfsvfs);
23955331Samw 			return (error);
23965331Samw 		}
23975331Samw 	}
23985331Samw 
2399789Sahrens 	/*
2400789Sahrens 	 * Return all attributes.  It's cheaper to provide the answer
2401789Sahrens 	 * than to determine whether we were asked the question.
2402789Sahrens 	 */
2403789Sahrens 
24049774SRay.Hassan@Sun.COM 	mutex_enter(&zp->z_lock);
2405789Sahrens 	vap->va_type = vp->v_type;
240611935SMark.Shellenbaum@Sun.COM 	vap->va_mode = zp->z_mode & MODEMASK;
240711935SMark.Shellenbaum@Sun.COM 	vap->va_uid = zp->z_uid;
240811935SMark.Shellenbaum@Sun.COM 	vap->va_gid = zp->z_gid;
2409789Sahrens 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2410789Sahrens 	vap->va_nodeid = zp->z_id;
24114543Smarks 	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
241211935SMark.Shellenbaum@Sun.COM 		links = zp->z_links + 1;
24134543Smarks 	else
241411935SMark.Shellenbaum@Sun.COM 		links = zp->z_links;
24154543Smarks 	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
241611935SMark.Shellenbaum@Sun.COM 	vap->va_size = zp->z_size;
24171816Smarks 	vap->va_rdev = vp->v_rdev;
2418789Sahrens 	vap->va_seq = zp->z_seq;
2419789Sahrens 
24205331Samw 	/*
24215331Samw 	 * Add in any requested optional attributes and the create time.
24225331Samw 	 * Also set the corresponding bits in the returned attribute bitmap.
24235331Samw 	 */
24245331Samw 	if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
24255331Samw 		if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
24265331Samw 			xoap->xoa_archive =
242711935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_ARCHIVE) != 0);
24285331Samw 			XVA_SET_RTN(xvap, XAT_ARCHIVE);
24295331Samw 		}
24305331Samw 
24315331Samw 		if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
24325331Samw 			xoap->xoa_readonly =
243311935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_READONLY) != 0);
24345331Samw 			XVA_SET_RTN(xvap, XAT_READONLY);
24355331Samw 		}
24365331Samw 
24375331Samw 		if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
24385331Samw 			xoap->xoa_system =
243911935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_SYSTEM) != 0);
24405331Samw 			XVA_SET_RTN(xvap, XAT_SYSTEM);
24415331Samw 		}
24425331Samw 
24435331Samw 		if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
24445331Samw 			xoap->xoa_hidden =
244511935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_HIDDEN) != 0);
24465331Samw 			XVA_SET_RTN(xvap, XAT_HIDDEN);
24475331Samw 		}
24485331Samw 
24495331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
24505331Samw 			xoap->xoa_nounlink =
245111935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NOUNLINK) != 0);
24525331Samw 			XVA_SET_RTN(xvap, XAT_NOUNLINK);
24535331Samw 		}
24545331Samw 
24555331Samw 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
24565331Samw 			xoap->xoa_immutable =
245711935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
24585331Samw 			XVA_SET_RTN(xvap, XAT_IMMUTABLE);
24595331Samw 		}
24605331Samw 
24615331Samw 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
24625331Samw 			xoap->xoa_appendonly =
246311935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_APPENDONLY) != 0);
24645331Samw 			XVA_SET_RTN(xvap, XAT_APPENDONLY);
24655331Samw 		}
24665331Samw 
24675331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
24685331Samw 			xoap->xoa_nodump =
246911935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NODUMP) != 0);
24705331Samw 			XVA_SET_RTN(xvap, XAT_NODUMP);
24715331Samw 		}
24725331Samw 
24735331Samw 		if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
24745331Samw 			xoap->xoa_opaque =
247511935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_OPAQUE) != 0);
24765331Samw 			XVA_SET_RTN(xvap, XAT_OPAQUE);
24775331Samw 		}
24785331Samw 
24795331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
24805331Samw 			xoap->xoa_av_quarantined =
248111935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
24825331Samw 			XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
24835331Samw 		}
24845331Samw 
24855331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
24865331Samw 			xoap->xoa_av_modified =
248711935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
24885331Samw 			XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
24895331Samw 		}
24905331Samw 
24915331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
249211935SMark.Shellenbaum@Sun.COM 		    vp->v_type == VREG) {
249311935SMark.Shellenbaum@Sun.COM 			zfs_sa_get_scanstamp(zp, xvap);
24945331Samw 		}
24955331Samw 
24965331Samw 		if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
249711935SMark.Shellenbaum@Sun.COM 			uint64_t times[2];
249811935SMark.Shellenbaum@Sun.COM 
249911935SMark.Shellenbaum@Sun.COM 			(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
250011935SMark.Shellenbaum@Sun.COM 			    times, sizeof (times));
250111935SMark.Shellenbaum@Sun.COM 			ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
25025331Samw 			XVA_SET_RTN(xvap, XAT_CREATETIME);
25035331Samw 		}
250410793Sdai.ngo@sun.com 
250510793Sdai.ngo@sun.com 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
250611935SMark.Shellenbaum@Sun.COM 			xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
250710793Sdai.ngo@sun.com 			XVA_SET_RTN(xvap, XAT_REPARSE);
250810793Sdai.ngo@sun.com 		}
25095331Samw 	}
25105331Samw 
251111935SMark.Shellenbaum@Sun.COM 	ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
251211935SMark.Shellenbaum@Sun.COM 	ZFS_TIME_DECODE(&vap->va_mtime, mtime);
251311935SMark.Shellenbaum@Sun.COM 	ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2514789Sahrens 
2515789Sahrens 	mutex_exit(&zp->z_lock);
2516789Sahrens 
251711935SMark.Shellenbaum@Sun.COM 	sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2518789Sahrens 
2519789Sahrens 	if (zp->z_blksz == 0) {
2520789Sahrens 		/*
2521789Sahrens 		 * Block size hasn't been set; suggest maximal I/O transfers.
2522789Sahrens 		 */
2523789Sahrens 		vap->va_blksize = zfsvfs->z_max_blksz;
2524789Sahrens 	}
2525789Sahrens 
2526789Sahrens 	ZFS_EXIT(zfsvfs);
2527789Sahrens 	return (0);
2528789Sahrens }
2529789Sahrens 
2530789Sahrens /*
2531789Sahrens  * Set the file attributes to the values contained in the
2532789Sahrens  * vattr structure.
2533789Sahrens  *
2534789Sahrens  *	IN:	vp	- vnode of file to be modified.
2535789Sahrens  *		vap	- new attribute values.
25365331Samw  *			  If AT_XVATTR set, then optional attrs are being set
2537789Sahrens  *		flags	- ATTR_UTIME set if non-default time values provided.
25385331Samw  *			- ATTR_NOACLCHECK (CIFS context only).
2539789Sahrens  *		cr	- credentials of caller.
25405331Samw  *		ct	- caller context
2541789Sahrens  *
2542789Sahrens  *	RETURN:	0 if success
2543789Sahrens  *		error code if failure
2544789Sahrens  *
2545789Sahrens  * Timestamps:
2546789Sahrens  *	vp - ctime updated, mtime updated if size changed.
2547789Sahrens  */
2548789Sahrens /* ARGSUSED */
2549789Sahrens static int
2550789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2551789Sahrens 	caller_context_t *ct)
2552789Sahrens {
25535326Sek110237 	znode_t		*zp = VTOZ(vp);
2554789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
25555326Sek110237 	zilog_t		*zilog;
2556789Sahrens 	dmu_tx_t	*tx;
25571878Smaybee 	vattr_t		oldva;
25588190SMark.Shellenbaum@Sun.COM 	xvattr_t	tmpxvattr;
2559789Sahrens 	uint_t		mask = vap->va_mask;
25601878Smaybee 	uint_t		saved_mask;
25612796Smarks 	int		trim_mask = 0;
2562789Sahrens 	uint64_t	new_mode;
25639179SMark.Shellenbaum@Sun.COM 	uint64_t	new_uid, new_gid;
256411935SMark.Shellenbaum@Sun.COM 	uint64_t	xattr_obj = 0;
256511935SMark.Shellenbaum@Sun.COM 	uint64_t	mtime[2], ctime[2];
25661231Smarks 	znode_t		*attrzp;
2567789Sahrens 	int		need_policy = FALSE;
256811935SMark.Shellenbaum@Sun.COM 	int		err, err2;
25695331Samw 	zfs_fuid_info_t *fuidp = NULL;
25705331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
25715331Samw 	xoptattr_t	*xoap;
25725824Smarks 	zfs_acl_t	*aclp = NULL;
25735331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
257411935SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied = B_FALSE;
257511935SMark.Shellenbaum@Sun.COM 	sa_bulk_attr_t	bulk[7], xattr_bulk[7];
257611935SMark.Shellenbaum@Sun.COM 	int		count = 0, xattr_count = 0;
2577789Sahrens 
2578789Sahrens 	if (mask == 0)
2579789Sahrens 		return (0);
2580789Sahrens 
2581789Sahrens 	if (mask & AT_NOSET)
2582789Sahrens 		return (EINVAL);
2583789Sahrens 
25845367Sahrens 	ZFS_ENTER(zfsvfs);
25855367Sahrens 	ZFS_VERIFY_ZP(zp);
25865331Samw 
25875331Samw 	zilog = zfsvfs->z_log;
25885331Samw 
25895331Samw 	/*
25905331Samw 	 * Make sure that if we have ephemeral uid/gid or xvattr specified
25915331Samw 	 * that file system is at proper version level
25925331Samw 	 */
25935331Samw 
25945331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
25955331Samw 	    (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
25965331Samw 	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
25975386Stimh 	    (mask & AT_XVATTR))) {
25985386Stimh 		ZFS_EXIT(zfsvfs);
25995331Samw 		return (EINVAL);
26005386Stimh 	}
26015386Stimh 
26025386Stimh 	if (mask & AT_SIZE && vp->v_type == VDIR) {
26035386Stimh 		ZFS_EXIT(zfsvfs);
2604789Sahrens 		return (EISDIR);
26055386Stimh 	}
26065386Stimh 
26075386Stimh 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
26085386Stimh 		ZFS_EXIT(zfsvfs);
26091308Smarks 		return (EINVAL);
26105386Stimh 	}
26111308Smarks 
26125331Samw 	/*
26135331Samw 	 * If this is an xvattr_t, then get a pointer to the structure of
26145331Samw 	 * optional attributes.  If this is NULL, then we have a vattr_t.
26155331Samw 	 */
26165331Samw 	xoap = xva_getxoptattr(xvap);
26175331Samw 
26188190SMark.Shellenbaum@Sun.COM 	xva_init(&tmpxvattr);
26198190SMark.Shellenbaum@Sun.COM 
26205331Samw 	/*
26215331Samw 	 * Immutable files can only alter immutable bit and atime
26225331Samw 	 */
262311935SMark.Shellenbaum@Sun.COM 	if ((zp->z_pflags & ZFS_IMMUTABLE) &&
26245331Samw 	    ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
26255386Stimh 	    ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
26265386Stimh 		ZFS_EXIT(zfsvfs);
26275331Samw 		return (EPERM);
26285386Stimh 	}
26295386Stimh 
263011935SMark.Shellenbaum@Sun.COM 	if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
26315386Stimh 		ZFS_EXIT(zfsvfs);
26325331Samw 		return (EPERM);
26335386Stimh 	}
2634789Sahrens 
26356064Smarks 	/*
26366064Smarks 	 * Verify timestamps doesn't overflow 32 bits.
26376064Smarks 	 * ZFS can handle large timestamps, but 32bit syscalls can't
26386064Smarks 	 * handle times greater than 2039.  This check should be removed
26396064Smarks 	 * once large timestamps are fully supported.
26406064Smarks 	 */
26416064Smarks 	if (mask & (AT_ATIME | AT_MTIME)) {
26426064Smarks 		if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
26436064Smarks 		    ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
26446064Smarks 			ZFS_EXIT(zfsvfs);
26456064Smarks 			return (EOVERFLOW);
26466064Smarks 		}
26476064Smarks 	}
26486064Smarks 
2649789Sahrens top:
26501231Smarks 	attrzp = NULL;
2651789Sahrens 
26529981STim.Haley@Sun.COM 	/* Can this be moved to before the top label? */
2653789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2654789Sahrens 		ZFS_EXIT(zfsvfs);
2655789Sahrens 		return (EROFS);
2656789Sahrens 	}
2657789Sahrens 
2658789Sahrens 	/*
2659789Sahrens 	 * First validate permissions
2660789Sahrens 	 */
2661789Sahrens 
2662789Sahrens 	if (mask & AT_SIZE) {
26635331Samw 		err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2664789Sahrens 		if (err) {
2665789Sahrens 			ZFS_EXIT(zfsvfs);
2666789Sahrens 			return (err);
2667789Sahrens 		}
26681878Smaybee 		/*
26691878Smaybee 		 * XXX - Note, we are not providing any open
26701878Smaybee 		 * mode flags here (like FNDELAY), so we may
26711878Smaybee 		 * block if there are locks present... this
26721878Smaybee 		 * should be addressed in openat().
26731878Smaybee 		 */
26746992Smaybee 		/* XXX - would it be OK to generate a log record here? */
26756992Smaybee 		err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
26761878Smaybee 		if (err) {
26771878Smaybee 			ZFS_EXIT(zfsvfs);
26781878Smaybee 			return (err);
26791878Smaybee 		}
2680789Sahrens 	}
2681789Sahrens 
26825331Samw 	if (mask & (AT_ATIME|AT_MTIME) ||
26835331Samw 	    ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
26845331Samw 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
26855331Samw 	    XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
26865331Samw 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
268711935SMark.Shellenbaum@Sun.COM 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
26885331Samw 		need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
26895331Samw 		    skipaclchk, cr);
269011935SMark.Shellenbaum@Sun.COM 	}
2691789Sahrens 
2692789Sahrens 	if (mask & (AT_UID|AT_GID)) {
2693789Sahrens 		int	idmask = (mask & (AT_UID|AT_GID));
2694789Sahrens 		int	take_owner;
2695789Sahrens 		int	take_group;
2696789Sahrens 
2697789Sahrens 		/*
2698913Smarks 		 * NOTE: even if a new mode is being set,
2699913Smarks 		 * we may clear S_ISUID/S_ISGID bits.
2700913Smarks 		 */
2701913Smarks 
2702913Smarks 		if (!(mask & AT_MODE))
270311935SMark.Shellenbaum@Sun.COM 			vap->va_mode = zp->z_mode;
2704913Smarks 
2705913Smarks 		/*
2706789Sahrens 		 * Take ownership or chgrp to group we are a member of
2707789Sahrens 		 */
2708789Sahrens 
2709789Sahrens 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
27105331Samw 		take_group = (mask & AT_GID) &&
27115331Samw 		    zfs_groupmember(zfsvfs, vap->va_gid, cr);
2712789Sahrens 
2713789Sahrens 		/*
2714789Sahrens 		 * If both AT_UID and AT_GID are set then take_owner and
2715789Sahrens 		 * take_group must both be set in order to allow taking
2716789Sahrens 		 * ownership.
2717789Sahrens 		 *
2718789Sahrens 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2719789Sahrens 		 *
2720789Sahrens 		 */
2721789Sahrens 
2722789Sahrens 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2723789Sahrens 		    ((idmask == AT_UID) && take_owner) ||
2724789Sahrens 		    ((idmask == AT_GID) && take_group)) {
27255331Samw 			if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
27265331Samw 			    skipaclchk, cr) == 0) {
2727789Sahrens 				/*
2728789Sahrens 				 * Remove setuid/setgid for non-privileged users
2729789Sahrens 				 */
27301115Smarks 				secpolicy_setid_clear(vap, cr);
27312796Smarks 				trim_mask = (mask & (AT_UID|AT_GID));
2732789Sahrens 			} else {
2733789Sahrens 				need_policy =  TRUE;
2734789Sahrens 			}
2735789Sahrens 		} else {
2736789Sahrens 			need_policy =  TRUE;
2737789Sahrens 		}
2738789Sahrens 	}
2739789Sahrens 
27402796Smarks 	mutex_enter(&zp->z_lock);
274111935SMark.Shellenbaum@Sun.COM 	oldva.va_mode = zp->z_mode;
274211935SMark.Shellenbaum@Sun.COM 	oldva.va_uid = zp->z_uid;
274311935SMark.Shellenbaum@Sun.COM 	oldva.va_gid = zp->z_gid;
27445331Samw 	if (mask & AT_XVATTR) {
27458190SMark.Shellenbaum@Sun.COM 		/*
27468190SMark.Shellenbaum@Sun.COM 		 * Update xvattr mask to include only those attributes
27478190SMark.Shellenbaum@Sun.COM 		 * that are actually changing.
27488190SMark.Shellenbaum@Sun.COM 		 *
27498190SMark.Shellenbaum@Sun.COM 		 * the bits will be restored prior to actually setting
27508190SMark.Shellenbaum@Sun.COM 		 * the attributes so the caller thinks they were set.
27518190SMark.Shellenbaum@Sun.COM 		 */
27528190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
27538190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_appendonly !=
275411935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
27558190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27568190SMark.Shellenbaum@Sun.COM 			} else {
27578190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_APPENDONLY);
27588190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
27598190SMark.Shellenbaum@Sun.COM 			}
27608190SMark.Shellenbaum@Sun.COM 		}
27618190SMark.Shellenbaum@Sun.COM 
27628190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
27638190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_nounlink !=
276411935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
27658190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27668190SMark.Shellenbaum@Sun.COM 			} else {
27678190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_NOUNLINK);
27688190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
27698190SMark.Shellenbaum@Sun.COM 			}
27708190SMark.Shellenbaum@Sun.COM 		}
27718190SMark.Shellenbaum@Sun.COM 
27728190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
27738190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_immutable !=
277411935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
27758190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27768190SMark.Shellenbaum@Sun.COM 			} else {
27778190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
27788190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
27798190SMark.Shellenbaum@Sun.COM 			}
27808190SMark.Shellenbaum@Sun.COM 		}
27818190SMark.Shellenbaum@Sun.COM 
27828190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
27838190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_nodump !=
278411935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NODUMP) != 0)) {
27858190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27868190SMark.Shellenbaum@Sun.COM 			} else {
27878190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_NODUMP);
27888190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
27898190SMark.Shellenbaum@Sun.COM 			}
27908190SMark.Shellenbaum@Sun.COM 		}
27918190SMark.Shellenbaum@Sun.COM 
27928190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
27938190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_av_modified !=
279411935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
27958190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27968190SMark.Shellenbaum@Sun.COM 			} else {
27978190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
27988190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
27998190SMark.Shellenbaum@Sun.COM 			}
28008190SMark.Shellenbaum@Sun.COM 		}
28018190SMark.Shellenbaum@Sun.COM 
28028190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
28038190SMark.Shellenbaum@Sun.COM 			if ((vp->v_type != VREG &&
28048190SMark.Shellenbaum@Sun.COM 			    xoap->xoa_av_quarantined) ||
28058190SMark.Shellenbaum@Sun.COM 			    xoap->xoa_av_quarantined !=
280611935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
28078190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
28088190SMark.Shellenbaum@Sun.COM 			} else {
28098190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
28108190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
28118190SMark.Shellenbaum@Sun.COM 			}
28128190SMark.Shellenbaum@Sun.COM 		}
28138190SMark.Shellenbaum@Sun.COM 
281410793Sdai.ngo@sun.com 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
281510793Sdai.ngo@sun.com 			mutex_exit(&zp->z_lock);
281610793Sdai.ngo@sun.com 			ZFS_EXIT(zfsvfs);
281710793Sdai.ngo@sun.com 			return (EPERM);
281810793Sdai.ngo@sun.com 		}
281910793Sdai.ngo@sun.com 
28208190SMark.Shellenbaum@Sun.COM 		if (need_policy == FALSE &&
28218190SMark.Shellenbaum@Sun.COM 		    (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
28228190SMark.Shellenbaum@Sun.COM 		    XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
28235331Samw 			need_policy = TRUE;
28245331Samw 		}
28255331Samw 	}
28265331Samw 
28272796Smarks 	mutex_exit(&zp->z_lock);
28282796Smarks 
28292796Smarks 	if (mask & AT_MODE) {
28305331Samw 		if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
28312796Smarks 			err = secpolicy_setid_setsticky_clear(vp, vap,
28322796Smarks 			    &oldva, cr);
28332796Smarks 			if (err) {
28342796Smarks 				ZFS_EXIT(zfsvfs);
28352796Smarks 				return (err);
28362796Smarks 			}
28372796Smarks 			trim_mask |= AT_MODE;
28382796Smarks 		} else {
28392796Smarks 			need_policy = TRUE;
28402796Smarks 		}
28412796Smarks 	}
2842789Sahrens 
2843789Sahrens 	if (need_policy) {
28441115Smarks 		/*
28451115Smarks 		 * If trim_mask is set then take ownership
28462796Smarks 		 * has been granted or write_acl is present and user
28472796Smarks 		 * has the ability to modify mode.  In that case remove
28482796Smarks 		 * UID|GID and or MODE from mask so that
28491115Smarks 		 * secpolicy_vnode_setattr() doesn't revoke it.
28501115Smarks 		 */
28512796Smarks 
28522796Smarks 		if (trim_mask) {
28532796Smarks 			saved_mask = vap->va_mask;
28542796Smarks 			vap->va_mask &= ~trim_mask;
28552796Smarks 		}
2856789Sahrens 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
28575331Samw 		    (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2858789Sahrens 		if (err) {
2859789Sahrens 			ZFS_EXIT(zfsvfs);
2860789Sahrens 			return (err);
2861789Sahrens 		}
28621115Smarks 
28631115Smarks 		if (trim_mask)
28642796Smarks 			vap->va_mask |= saved_mask;
2865789Sahrens 	}
2866789Sahrens 
2867789Sahrens 	/*
2868789Sahrens 	 * secpolicy_vnode_setattr, or take ownership may have
2869789Sahrens 	 * changed va_mask
2870789Sahrens 	 */
2871789Sahrens 	mask = vap->va_mask;
2872789Sahrens 
287311935SMark.Shellenbaum@Sun.COM 	if ((mask & (AT_UID | AT_GID))) {
287411935SMark.Shellenbaum@Sun.COM 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xattr_obj,
287511935SMark.Shellenbaum@Sun.COM 		    sizeof (xattr_obj));
287611935SMark.Shellenbaum@Sun.COM 
287711935SMark.Shellenbaum@Sun.COM 		if (xattr_obj) {
287811935SMark.Shellenbaum@Sun.COM 			err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
28799396SMatthew.Ahrens@Sun.COM 			if (err)
288011935SMark.Shellenbaum@Sun.COM 				goto out2;
28819179SMark.Shellenbaum@Sun.COM 		}
28829179SMark.Shellenbaum@Sun.COM 		if (mask & AT_UID) {
28839179SMark.Shellenbaum@Sun.COM 			new_uid = zfs_fuid_create(zfsvfs,
28849179SMark.Shellenbaum@Sun.COM 			    (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
288511935SMark.Shellenbaum@Sun.COM 			if (vap->va_uid != zp->z_uid &&
288611935SMark.Shellenbaum@Sun.COM 			    zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
28879396SMatthew.Ahrens@Sun.COM 				err = EDQUOT;
288811935SMark.Shellenbaum@Sun.COM 				goto out2;
28899396SMatthew.Ahrens@Sun.COM 			}
28901231Smarks 		}
28919396SMatthew.Ahrens@Sun.COM 
28929179SMark.Shellenbaum@Sun.COM 		if (mask & AT_GID) {
28939179SMark.Shellenbaum@Sun.COM 			new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
28949179SMark.Shellenbaum@Sun.COM 			    cr, ZFS_GROUP, &fuidp);
289511935SMark.Shellenbaum@Sun.COM 			if (new_gid != zp->z_gid &&
289611935SMark.Shellenbaum@Sun.COM 			    zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
28979396SMatthew.Ahrens@Sun.COM 				err = EDQUOT;
289811935SMark.Shellenbaum@Sun.COM 				goto out2;
28999179SMark.Shellenbaum@Sun.COM 			}
29009179SMark.Shellenbaum@Sun.COM 		}
29011231Smarks 	}
290211935SMark.Shellenbaum@Sun.COM 	tx = dmu_tx_create(zfsvfs->z_os);
290311935SMark.Shellenbaum@Sun.COM 
290411935SMark.Shellenbaum@Sun.COM 	if (mask & AT_MODE) {
290511935SMark.Shellenbaum@Sun.COM 		uint64_t pmode = zp->z_mode;
290611935SMark.Shellenbaum@Sun.COM 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
290711935SMark.Shellenbaum@Sun.COM 
290811935SMark.Shellenbaum@Sun.COM 		if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
290911935SMark.Shellenbaum@Sun.COM 			goto out;
291011935SMark.Shellenbaum@Sun.COM 
291111935SMark.Shellenbaum@Sun.COM 		if (!zp->z_is_sa && ZFS_EXTERNAL_ACL(zp)) {
291211935SMark.Shellenbaum@Sun.COM 			/*
291311935SMark.Shellenbaum@Sun.COM 			 * Are we upgrading ACL from old V0 format
291411935SMark.Shellenbaum@Sun.COM 			 * to V1 format?
291511935SMark.Shellenbaum@Sun.COM 			 */
291611935SMark.Shellenbaum@Sun.COM 			if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
291711935SMark.Shellenbaum@Sun.COM 			    ZNODE_ACL_VERSION(zp) ==
291811935SMark.Shellenbaum@Sun.COM 			    ZFS_ACL_VERSION_INITIAL) {
291911935SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_free(tx,
292011935SMark.Shellenbaum@Sun.COM 				    ZFS_EXTERNAL_ACL(zp), 0,
292111935SMark.Shellenbaum@Sun.COM 				    DMU_OBJECT_END);
292211935SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
292311935SMark.Shellenbaum@Sun.COM 				    0, aclp->z_acl_bytes);
292411935SMark.Shellenbaum@Sun.COM 			} else {
292511935SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_write(tx, ZFS_EXTERNAL_ACL(zp), 0,
292611935SMark.Shellenbaum@Sun.COM 				    aclp->z_acl_bytes);
292711935SMark.Shellenbaum@Sun.COM 			}
292811935SMark.Shellenbaum@Sun.COM 		} else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
292911935SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
293011935SMark.Shellenbaum@Sun.COM 			    0, aclp->z_acl_bytes);
293111935SMark.Shellenbaum@Sun.COM 		}
293211935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
293311935SMark.Shellenbaum@Sun.COM 	} else {
293411935SMark.Shellenbaum@Sun.COM 		if ((mask & AT_XVATTR) &&
293511935SMark.Shellenbaum@Sun.COM 		    XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
293611935SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
293711935SMark.Shellenbaum@Sun.COM 		else
293811935SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
293911935SMark.Shellenbaum@Sun.COM 	}
294011935SMark.Shellenbaum@Sun.COM 
294111935SMark.Shellenbaum@Sun.COM 	if (attrzp) {
294211935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
294311935SMark.Shellenbaum@Sun.COM 	}
294411935SMark.Shellenbaum@Sun.COM 
294511935SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
294611935SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
294711935SMark.Shellenbaum@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
294811935SMark.Shellenbaum@Sun.COM 
294911935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
29501231Smarks 
29518227SNeil.Perrin@Sun.COM 	err = dmu_tx_assign(tx, TXG_NOWAIT);
2952789Sahrens 	if (err) {
29539396SMatthew.Ahrens@Sun.COM 		if (err == ERESTART)
29542113Sahrens 			dmu_tx_wait(tx);
29559396SMatthew.Ahrens@Sun.COM 		goto out;
2956789Sahrens 	}
2957789Sahrens 
295811935SMark.Shellenbaum@Sun.COM 	count = 0;
2959789Sahrens 	/*
2960789Sahrens 	 * Set each attribute requested.
2961789Sahrens 	 * We group settings according to the locks they need to acquire.
2962789Sahrens 	 *
2963789Sahrens 	 * Note: you cannot set ctime directly, although it will be
2964789Sahrens 	 * updated as a side-effect of calling this function.
2965789Sahrens 	 */
2966789Sahrens 
2967789Sahrens 	mutex_enter(&zp->z_lock);
2968789Sahrens 
296911935SMark.Shellenbaum@Sun.COM 	if (attrzp)
297011935SMark.Shellenbaum@Sun.COM 		mutex_enter(&attrzp->z_lock);
297111935SMark.Shellenbaum@Sun.COM 
297212164SMark.Shellenbaum@Sun.COM 	if (mask & (AT_UID|AT_GID)) {
297312164SMark.Shellenbaum@Sun.COM 
297412164SMark.Shellenbaum@Sun.COM 		if (mask & AT_UID) {
297512164SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
297612164SMark.Shellenbaum@Sun.COM 			    &new_uid, sizeof (new_uid));
297712164SMark.Shellenbaum@Sun.COM 			zp->z_uid = zfs_fuid_map_id(zfsvfs, new_uid,
297812164SMark.Shellenbaum@Sun.COM 			    cr, ZFS_OWNER);
297912164SMark.Shellenbaum@Sun.COM 			if (attrzp) {
298012164SMark.Shellenbaum@Sun.COM 				SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
298112164SMark.Shellenbaum@Sun.COM 				    SA_ZPL_UID(zfsvfs), NULL, &new_uid,
298212164SMark.Shellenbaum@Sun.COM 				    sizeof (new_uid));
298312164SMark.Shellenbaum@Sun.COM 				attrzp->z_gid = zp->z_uid;
298412164SMark.Shellenbaum@Sun.COM 			}
298511935SMark.Shellenbaum@Sun.COM 		}
298612164SMark.Shellenbaum@Sun.COM 
298712164SMark.Shellenbaum@Sun.COM 		if (mask & AT_GID) {
298812164SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
298912164SMark.Shellenbaum@Sun.COM 			    NULL, &new_gid, sizeof (new_gid));
299012164SMark.Shellenbaum@Sun.COM 			zp->z_gid = zfs_fuid_map_id(zfsvfs, new_gid, cr,
299112164SMark.Shellenbaum@Sun.COM 			    ZFS_GROUP);
299212164SMark.Shellenbaum@Sun.COM 			if (attrzp) {
299312164SMark.Shellenbaum@Sun.COM 				SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
299412164SMark.Shellenbaum@Sun.COM 				    SA_ZPL_GID(zfsvfs), NULL, &new_gid,
299512164SMark.Shellenbaum@Sun.COM 				    sizeof (new_gid));
299612164SMark.Shellenbaum@Sun.COM 				attrzp->z_gid = zp->z_gid;
299712164SMark.Shellenbaum@Sun.COM 			}
299812164SMark.Shellenbaum@Sun.COM 		}
299912164SMark.Shellenbaum@Sun.COM 		if (!(mask & AT_MODE)) {
300012164SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
300112164SMark.Shellenbaum@Sun.COM 			    NULL, &new_mode, sizeof (new_mode));
300212164SMark.Shellenbaum@Sun.COM 			new_mode = zp->z_mode;
300312164SMark.Shellenbaum@Sun.COM 		}
300412164SMark.Shellenbaum@Sun.COM 		err = zfs_acl_chown_setattr(zp);
300512164SMark.Shellenbaum@Sun.COM 		ASSERT(err == 0);
300611935SMark.Shellenbaum@Sun.COM 		if (attrzp) {
300712164SMark.Shellenbaum@Sun.COM 			err = zfs_acl_chown_setattr(attrzp);
300812164SMark.Shellenbaum@Sun.COM 			ASSERT(err == 0);
300911935SMark.Shellenbaum@Sun.COM 		}
301011935SMark.Shellenbaum@Sun.COM 	}
301111935SMark.Shellenbaum@Sun.COM 
3012789Sahrens 	if (mask & AT_MODE) {
30135824Smarks 		mutex_enter(&zp->z_acl_lock);
301411935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
301511935SMark.Shellenbaum@Sun.COM 		    &new_mode, sizeof (new_mode));
301611935SMark.Shellenbaum@Sun.COM 		zp->z_mode = new_mode;
301712164SMark.Shellenbaum@Sun.COM 		ASSERT3U((uintptr_t)aclp, !=, NULL);
30189179SMark.Shellenbaum@Sun.COM 		err = zfs_aclset_common(zp, aclp, cr, tx);
3019789Sahrens 		ASSERT3U(err, ==, 0);
302010143STim.Haley@Sun.COM 		zp->z_acl_cached = aclp;
302110143STim.Haley@Sun.COM 		aclp = NULL;
30225824Smarks 		mutex_exit(&zp->z_acl_lock);
3023789Sahrens 	}
3024789Sahrens 
30251231Smarks 	if (attrzp)
302611935SMark.Shellenbaum@Sun.COM 		mutex_exit(&attrzp->z_lock);
302711935SMark.Shellenbaum@Sun.COM 
302811935SMark.Shellenbaum@Sun.COM 	if (mask & AT_ATIME) {
302911935SMark.Shellenbaum@Sun.COM 		ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
303011935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
303111935SMark.Shellenbaum@Sun.COM 		    &zp->z_atime, sizeof (zp->z_atime));
30321231Smarks 	}
30331231Smarks 
303411935SMark.Shellenbaum@Sun.COM 	if (mask & AT_MTIME) {
303511935SMark.Shellenbaum@Sun.COM 		ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
303611935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
303711935SMark.Shellenbaum@Sun.COM 		    mtime, sizeof (mtime));
30381231Smarks 	}
30391231Smarks 
30406992Smaybee 	/* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
304111935SMark.Shellenbaum@Sun.COM 	if (mask & AT_SIZE && !(mask & AT_MTIME)) {
304211935SMark.Shellenbaum@Sun.COM 		if (!(mask & AT_MTIME))
304311935SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
304411935SMark.Shellenbaum@Sun.COM 			    NULL, mtime, sizeof (mtime));
304511935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
304611935SMark.Shellenbaum@Sun.COM 		    &ctime, sizeof (ctime));
304711935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
304811935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
304911935SMark.Shellenbaum@Sun.COM 	} else if (mask != 0) {
305011935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
305111935SMark.Shellenbaum@Sun.COM 		    &ctime, sizeof (ctime));
305211935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
305311935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
305411935SMark.Shellenbaum@Sun.COM 		if (attrzp) {
305511935SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
305611935SMark.Shellenbaum@Sun.COM 			    SA_ZPL_CTIME(zfsvfs), NULL,
305711935SMark.Shellenbaum@Sun.COM 			    &ctime, sizeof (ctime));
305811935SMark.Shellenbaum@Sun.COM 			zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
305911935SMark.Shellenbaum@Sun.COM 			    mtime, ctime, B_TRUE);
306011935SMark.Shellenbaum@Sun.COM 		}
306111935SMark.Shellenbaum@Sun.COM 	}
30625331Samw 	/*
30635331Samw 	 * Do this after setting timestamps to prevent timestamp
30645331Samw 	 * update from toggling bit
30655331Samw 	 */
30665331Samw 
30675331Samw 	if (xoap && (mask & AT_XVATTR)) {
30688190SMark.Shellenbaum@Sun.COM 
30698190SMark.Shellenbaum@Sun.COM 		/*
30708190SMark.Shellenbaum@Sun.COM 		 * restore trimmed off masks
30718190SMark.Shellenbaum@Sun.COM 		 * so that return masks can be set for caller.
30728190SMark.Shellenbaum@Sun.COM 		 */
30738190SMark.Shellenbaum@Sun.COM 
30748190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
30758190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_APPENDONLY);
30768190SMark.Shellenbaum@Sun.COM 		}
30778190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
30788190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_NOUNLINK);
30798190SMark.Shellenbaum@Sun.COM 		}
30808190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
30818190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_IMMUTABLE);
30828190SMark.Shellenbaum@Sun.COM 		}
30838190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
30848190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_NODUMP);
30858190SMark.Shellenbaum@Sun.COM 		}
30868190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
30878190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
30888190SMark.Shellenbaum@Sun.COM 		}
30898190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
30908190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
30918190SMark.Shellenbaum@Sun.COM 		}
30928190SMark.Shellenbaum@Sun.COM 
309311935SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
30945331Samw 			ASSERT(vp->v_type == VREG);
30955331Samw 
309611935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
309711935SMark.Shellenbaum@Sun.COM 		    &zp->z_pflags, sizeof (zp->z_pflags));
309811935SMark.Shellenbaum@Sun.COM 		zfs_xvattr_set(zp, xvap, tx);
30995331Samw 	}
3100789Sahrens 
31019179SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
31029179SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
31039179SMark.Shellenbaum@Sun.COM 
31041878Smaybee 	if (mask != 0)
31055331Samw 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
31065331Samw 
3107789Sahrens 	mutex_exit(&zp->z_lock);
3108789Sahrens 
31099396SMatthew.Ahrens@Sun.COM out:
311011935SMark.Shellenbaum@Sun.COM 	if (err == 0 && attrzp) {
311111935SMark.Shellenbaum@Sun.COM 		err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
311211935SMark.Shellenbaum@Sun.COM 		    xattr_count, tx);
311311935SMark.Shellenbaum@Sun.COM 		ASSERT(err2 == 0);
311411935SMark.Shellenbaum@Sun.COM 	}
311511935SMark.Shellenbaum@Sun.COM 
31161231Smarks 	if (attrzp)
31171231Smarks 		VN_RELE(ZTOV(attrzp));
311810143STim.Haley@Sun.COM 	if (aclp)
311910143STim.Haley@Sun.COM 		zfs_acl_free(aclp);
312010143STim.Haley@Sun.COM 
31219396SMatthew.Ahrens@Sun.COM 	if (fuidp) {
31229396SMatthew.Ahrens@Sun.COM 		zfs_fuid_info_free(fuidp);
31239396SMatthew.Ahrens@Sun.COM 		fuidp = NULL;
31249396SMatthew.Ahrens@Sun.COM 	}
31259396SMatthew.Ahrens@Sun.COM 
312611935SMark.Shellenbaum@Sun.COM 	if (err) {
31279396SMatthew.Ahrens@Sun.COM 		dmu_tx_abort(tx);
312811935SMark.Shellenbaum@Sun.COM 		if (err == ERESTART)
312911935SMark.Shellenbaum@Sun.COM 			goto top;
313011935SMark.Shellenbaum@Sun.COM 	} else {
313111935SMark.Shellenbaum@Sun.COM 		err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
31329396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
313311935SMark.Shellenbaum@Sun.COM 	}
313411935SMark.Shellenbaum@Sun.COM 
313511935SMark.Shellenbaum@Sun.COM 
313611935SMark.Shellenbaum@Sun.COM out2:
3137*12294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3138*12294SMark.Musante@Sun.COM 		zil_commit(zilog, UINT64_MAX, 0);
3139*12294SMark.Musante@Sun.COM 
3140789Sahrens 	ZFS_EXIT(zfsvfs);
3141789Sahrens 	return (err);
3142789Sahrens }
3143789Sahrens 
31443271Smaybee typedef struct zfs_zlock {
31453271Smaybee 	krwlock_t	*zl_rwlock;	/* lock we acquired */
31463271Smaybee 	znode_t		*zl_znode;	/* znode we held */
31473271Smaybee 	struct zfs_zlock *zl_next;	/* next in list */
31483271Smaybee } zfs_zlock_t;
31493271Smaybee 
31503271Smaybee /*
31513271Smaybee  * Drop locks and release vnodes that were held by zfs_rename_lock().
31523271Smaybee  */
31533271Smaybee static void
31543271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp)
31553271Smaybee {
31563271Smaybee 	zfs_zlock_t *zl;
31573271Smaybee 
31583271Smaybee 	while ((zl = *zlpp) != NULL) {
31593271Smaybee 		if (zl->zl_znode != NULL)
31603271Smaybee 			VN_RELE(ZTOV(zl->zl_znode));
31613271Smaybee 		rw_exit(zl->zl_rwlock);
31623271Smaybee 		*zlpp = zl->zl_next;
31633271Smaybee 		kmem_free(zl, sizeof (*zl));
31643271Smaybee 	}
31653271Smaybee }
31663271Smaybee 
3167789Sahrens /*
3168789Sahrens  * Search back through the directory tree, using the ".." entries.
3169789Sahrens  * Lock each directory in the chain to prevent concurrent renames.
3170789Sahrens  * Fail any attempt to move a directory into one of its own descendants.
3171789Sahrens  * XXX - z_parent_lock can overlap with map or grow locks
3172789Sahrens  */
3173789Sahrens static int
3174789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3175789Sahrens {
3176789Sahrens 	zfs_zlock_t	*zl;
31773638Sbillm 	znode_t		*zp = tdzp;
3178789Sahrens 	uint64_t	rootid = zp->z_zfsvfs->z_root;
317911935SMark.Shellenbaum@Sun.COM 	uint64_t	oidp = zp->z_id;
3180789Sahrens 	krwlock_t	*rwlp = &szp->z_parent_lock;
3181789Sahrens 	krw_t		rw = RW_WRITER;
3182789Sahrens 
3183789Sahrens 	/*
3184789Sahrens 	 * First pass write-locks szp and compares to zp->z_id.
3185789Sahrens 	 * Later passes read-lock zp and compare to zp->z_parent.
3186789Sahrens 	 */
3187789Sahrens 	do {
31883271Smaybee 		if (!rw_tryenter(rwlp, rw)) {
31893271Smaybee 			/*
31903271Smaybee 			 * Another thread is renaming in this path.
31913271Smaybee 			 * Note that if we are a WRITER, we don't have any
31923271Smaybee 			 * parent_locks held yet.
31933271Smaybee 			 */
31943271Smaybee 			if (rw == RW_READER && zp->z_id > szp->z_id) {
31953271Smaybee 				/*
31963271Smaybee 				 * Drop our locks and restart
31973271Smaybee 				 */
31983271Smaybee 				zfs_rename_unlock(&zl);
31993271Smaybee 				*zlpp = NULL;
32003271Smaybee 				zp = tdzp;
320111935SMark.Shellenbaum@Sun.COM 				oidp = zp->z_id;
32023271Smaybee 				rwlp = &szp->z_parent_lock;
32033271Smaybee 				rw = RW_WRITER;
32043271Smaybee 				continue;
32053271Smaybee 			} else {
32063271Smaybee 				/*
32073271Smaybee 				 * Wait for other thread to drop its locks
32083271Smaybee 				 */
32093271Smaybee 				rw_enter(rwlp, rw);
32103271Smaybee 			}
32113271Smaybee 		}
32123271Smaybee 
3213789Sahrens 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3214789Sahrens 		zl->zl_rwlock = rwlp;
3215789Sahrens 		zl->zl_znode = NULL;
3216789Sahrens 		zl->zl_next = *zlpp;
3217789Sahrens 		*zlpp = zl;
3218789Sahrens 
321911935SMark.Shellenbaum@Sun.COM 		if (oidp == szp->z_id)		/* We're a descendant of szp */
3220789Sahrens 			return (EINVAL);
3221789Sahrens 
322211935SMark.Shellenbaum@Sun.COM 		if (oidp == rootid)		/* We've hit the top */
3223789Sahrens 			return (0);
3224789Sahrens 
3225789Sahrens 		if (rw == RW_READER) {		/* i.e. not the first pass */
322611935SMark.Shellenbaum@Sun.COM 			int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3227789Sahrens 			if (error)
3228789Sahrens 				return (error);
3229789Sahrens 			zl->zl_znode = zp;
3230789Sahrens 		}
323111935SMark.Shellenbaum@Sun.COM 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
323211935SMark.Shellenbaum@Sun.COM 		    &oidp, sizeof (oidp));
3233789Sahrens 		rwlp = &zp->z_parent_lock;
3234789Sahrens 		rw = RW_READER;
3235789Sahrens 
3236789Sahrens 	} while (zp->z_id != sdzp->z_id);
3237789Sahrens 
3238789Sahrens 	return (0);
3239789Sahrens }
3240789Sahrens 
3241789Sahrens /*
3242789Sahrens  * Move an entry from the provided source directory to the target
3243789Sahrens  * directory.  Change the entry name as indicated.
3244789Sahrens  *
3245789Sahrens  *	IN:	sdvp	- Source directory containing the "old entry".
3246789Sahrens  *		snm	- Old entry name.
3247789Sahrens  *		tdvp	- Target directory to contain the "new entry".
3248789Sahrens  *		tnm	- New entry name.
3249789Sahrens  *		cr	- credentials of caller.
32505331Samw  *		ct	- caller context
32515331Samw  *		flags	- case flags
3252789Sahrens  *
3253789Sahrens  *	RETURN:	0 if success
3254789Sahrens  *		error code if failure
3255789Sahrens  *
3256789Sahrens  * Timestamps:
3257789Sahrens  *	sdvp,tdvp - ctime|mtime updated
3258789Sahrens  */
32595331Samw /*ARGSUSED*/
3260789Sahrens static int
32615331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
32625331Samw     caller_context_t *ct, int flags)
3263789Sahrens {
3264789Sahrens 	znode_t		*tdzp, *szp, *tzp;
3265789Sahrens 	znode_t		*sdzp = VTOZ(sdvp);
3266789Sahrens 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
32675326Sek110237 	zilog_t		*zilog;
3268789Sahrens 	vnode_t		*realvp;
3269789Sahrens 	zfs_dirlock_t	*sdl, *tdl;
3270789Sahrens 	dmu_tx_t	*tx;
3271789Sahrens 	zfs_zlock_t	*zl;
32725331Samw 	int		cmp, serr, terr;
32735331Samw 	int		error = 0;
32745331Samw 	int		zflg = 0;
3275789Sahrens 
32765367Sahrens 	ZFS_ENTER(zfsvfs);
32775367Sahrens 	ZFS_VERIFY_ZP(sdzp);
32785326Sek110237 	zilog = zfsvfs->z_log;
3279789Sahrens 
3280789Sahrens 	/*
3281789Sahrens 	 * Make sure we have the real vp for the target directory.
3282789Sahrens 	 */
32835331Samw 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3284789Sahrens 		tdvp = realvp;
3285789Sahrens 
328612079SMark.Shellenbaum@Sun.COM 	if (tdvp->v_vfsp != sdvp->v_vfsp || zfsctl_is_node(tdvp)) {
3287789Sahrens 		ZFS_EXIT(zfsvfs);
3288789Sahrens 		return (EXDEV);
3289789Sahrens 	}
3290789Sahrens 
3291789Sahrens 	tdzp = VTOZ(tdvp);
32925367Sahrens 	ZFS_VERIFY_ZP(tdzp);
32935498Stimh 	if (zfsvfs->z_utf8 && u8_validate(tnm,
32945331Samw 	    strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
32955331Samw 		ZFS_EXIT(zfsvfs);
32965331Samw 		return (EILSEQ);
32975331Samw 	}
32985331Samw 
32995331Samw 	if (flags & FIGNORECASE)
33005331Samw 		zflg |= ZCILOOK;
33015331Samw 
3302789Sahrens top:
3303789Sahrens 	szp = NULL;
3304789Sahrens 	tzp = NULL;
3305789Sahrens 	zl = NULL;
3306789Sahrens 
3307789Sahrens 	/*
3308789Sahrens 	 * This is to prevent the creation of links into attribute space
3309789Sahrens 	 * by renaming a linked file into/outof an attribute directory.
3310789Sahrens 	 * See the comment in zfs_link() for why this is considered bad.
3311789Sahrens 	 */
331211935SMark.Shellenbaum@Sun.COM 	if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3313789Sahrens 		ZFS_EXIT(zfsvfs);
3314789Sahrens 		return (EINVAL);
3315789Sahrens 	}
3316789Sahrens 
3317789Sahrens 	/*
3318789Sahrens 	 * Lock source and target directory entries.  To prevent deadlock,
3319789Sahrens 	 * a lock ordering must be defined.  We lock the directory with
3320789Sahrens 	 * the smallest object id first, or if it's a tie, the one with
3321789Sahrens 	 * the lexically first name.
3322789Sahrens 	 */
3323789Sahrens 	if (sdzp->z_id < tdzp->z_id) {
3324789Sahrens 		cmp = -1;
3325789Sahrens 	} else if (sdzp->z_id > tdzp->z_id) {
3326789Sahrens 		cmp = 1;
3327789Sahrens 	} else {
33285331Samw 		/*
33295331Samw 		 * First compare the two name arguments without
33305331Samw 		 * considering any case folding.
33315331Samw 		 */
33325331Samw 		int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
33335331Samw 
33345331Samw 		cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
33355498Stimh 		ASSERT(error == 0 || !zfsvfs->z_utf8);
3336789Sahrens 		if (cmp == 0) {
3337789Sahrens 			/*
3338789Sahrens 			 * POSIX: "If the old argument and the new argument
3339789Sahrens 			 * both refer to links to the same existing file,
3340789Sahrens 			 * the rename() function shall return successfully
3341789Sahrens 			 * and perform no other action."
3342789Sahrens 			 */
3343789Sahrens 			ZFS_EXIT(zfsvfs);
3344789Sahrens 			return (0);
3345789Sahrens 		}
33465331Samw 		/*
33475331Samw 		 * If the file system is case-folding, then we may
33485331Samw 		 * have some more checking to do.  A case-folding file
33495331Samw 		 * system is either supporting mixed case sensitivity
33505331Samw 		 * access or is completely case-insensitive.  Note
33515331Samw 		 * that the file system is always case preserving.
33525331Samw 		 *
33535331Samw 		 * In mixed sensitivity mode case sensitive behavior
33545331Samw 		 * is the default.  FIGNORECASE must be used to
33555331Samw 		 * explicitly request case insensitive behavior.
33565331Samw 		 *
33575331Samw 		 * If the source and target names provided differ only
33585331Samw 		 * by case (e.g., a request to rename 'tim' to 'Tim'),
33595331Samw 		 * we will treat this as a special case in the
33605331Samw 		 * case-insensitive mode: as long as the source name
33615331Samw 		 * is an exact match, we will allow this to proceed as
33625331Samw 		 * a name-change request.
33635331Samw 		 */
33645498Stimh 		if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
33655498Stimh 		    (zfsvfs->z_case == ZFS_CASE_MIXED &&
33665498Stimh 		    flags & FIGNORECASE)) &&
33675331Samw 		    u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
33685331Samw 		    &error) == 0) {
33695331Samw 			/*
33705331Samw 			 * case preserving rename request, require exact
33715331Samw 			 * name matches
33725331Samw 			 */
33735331Samw 			zflg |= ZCIEXACT;
33745331Samw 			zflg &= ~ZCILOOK;
33755331Samw 		}
3376789Sahrens 	}
33775331Samw 
337811321SSanjeev.Bagewadi@Sun.COM 	/*
337911321SSanjeev.Bagewadi@Sun.COM 	 * If the source and destination directories are the same, we should
338011321SSanjeev.Bagewadi@Sun.COM 	 * grab the z_name_lock of that directory only once.
338111321SSanjeev.Bagewadi@Sun.COM 	 */
338211321SSanjeev.Bagewadi@Sun.COM 	if (sdzp == tdzp) {
338311321SSanjeev.Bagewadi@Sun.COM 		zflg |= ZHAVELOCK;
338411321SSanjeev.Bagewadi@Sun.COM 		rw_enter(&sdzp->z_name_lock, RW_READER);
338511321SSanjeev.Bagewadi@Sun.COM 	}
338611321SSanjeev.Bagewadi@Sun.COM 
3387789Sahrens 	if (cmp < 0) {
33885331Samw 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
33895331Samw 		    ZEXISTS | zflg, NULL, NULL);
33905331Samw 		terr = zfs_dirent_lock(&tdl,
33915331Samw 		    tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3392789Sahrens 	} else {
33935331Samw 		terr = zfs_dirent_lock(&tdl,
33945331Samw 		    tdzp, tnm, &tzp, zflg, NULL, NULL);
33955331Samw 		serr = zfs_dirent_lock(&sdl,
33965331Samw 		    sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
33975331Samw 		    NULL, NULL);
3398789Sahrens 	}
3399789Sahrens 
3400789Sahrens 	if (serr) {
3401789Sahrens 		/*
3402789Sahrens 		 * Source entry invalid or not there.
3403789Sahrens 		 */
3404789Sahrens 		if (!terr) {
3405789Sahrens 			zfs_dirent_unlock(tdl);
3406789Sahrens 			if (tzp)
3407789Sahrens 				VN_RELE(ZTOV(tzp));
3408789Sahrens 		}
340911321SSanjeev.Bagewadi@Sun.COM 
341011321SSanjeev.Bagewadi@Sun.COM 		if (sdzp == tdzp)
341111321SSanjeev.Bagewadi@Sun.COM 			rw_exit(&sdzp->z_name_lock);
341211321SSanjeev.Bagewadi@Sun.COM 
3413789Sahrens 		if (strcmp(snm, "..") == 0)
3414789Sahrens 			serr = EINVAL;
3415789Sahrens 		ZFS_EXIT(zfsvfs);
3416789Sahrens 		return (serr);
3417789Sahrens 	}
3418789Sahrens 	if (terr) {
3419789Sahrens 		zfs_dirent_unlock(sdl);
3420789Sahrens 		VN_RELE(ZTOV(szp));
342111321SSanjeev.Bagewadi@Sun.COM 
342211321SSanjeev.Bagewadi@Sun.COM 		if (sdzp == tdzp)
342311321SSanjeev.Bagewadi@Sun.COM 			rw_exit(&sdzp->z_name_lock);
342411321SSanjeev.Bagewadi@Sun.COM 
3425789Sahrens 		if (strcmp(tnm, "..") == 0)
3426789Sahrens 			terr = EINVAL;
3427789Sahrens 		ZFS_EXIT(zfsvfs);
3428789Sahrens 		return (terr);
3429789Sahrens 	}
3430789Sahrens 
3431789Sahrens 	/*
3432789Sahrens 	 * Must have write access at the source to remove the old entry
3433789Sahrens 	 * and write access at the target to create the new entry.
3434789Sahrens 	 * Note that if target and source are the same, this can be
3435789Sahrens 	 * done in a single check.
3436789Sahrens 	 */
3437789Sahrens 
3438789Sahrens 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3439789Sahrens 		goto out;
3440789Sahrens 
3441789Sahrens 	if (ZTOV(szp)->v_type == VDIR) {
3442789Sahrens 		/*
3443789Sahrens 		 * Check to make sure rename is valid.
3444789Sahrens 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3445789Sahrens 		 */
3446789Sahrens 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3447789Sahrens 			goto out;
3448789Sahrens 	}
3449789Sahrens 
3450789Sahrens 	/*
3451789Sahrens 	 * Does target exist?
3452789Sahrens 	 */
3453789Sahrens 	if (tzp) {
3454789Sahrens 		/*
3455789Sahrens 		 * Source and target must be the same type.
3456789Sahrens 		 */
3457789Sahrens 		if (ZTOV(szp)->v_type == VDIR) {
3458789Sahrens 			if (ZTOV(tzp)->v_type != VDIR) {
3459789Sahrens 				error = ENOTDIR;
3460789Sahrens 				goto out;
3461789Sahrens 			}
3462789Sahrens 		} else {
3463789Sahrens 			if (ZTOV(tzp)->v_type == VDIR) {
3464789Sahrens 				error = EISDIR;
3465789Sahrens 				goto out;
3466789Sahrens 			}
3467789Sahrens 		}
3468789Sahrens 		/*
3469789Sahrens 		 * POSIX dictates that when the source and target
3470789Sahrens 		 * entries refer to the same file object, rename
3471789Sahrens 		 * must do nothing and exit without error.
3472789Sahrens 		 */
3473789Sahrens 		if (szp->z_id == tzp->z_id) {
3474789Sahrens 			error = 0;
3475789Sahrens 			goto out;
3476789Sahrens 		}
3477789Sahrens 	}
3478789Sahrens 
34795331Samw 	vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3480789Sahrens 	if (tzp)
34815331Samw 		vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
34824863Spraks 
34834863Spraks 	/*
34844863Spraks 	 * notify the target directory if it is not the same
34854863Spraks 	 * as source directory.
34864863Spraks 	 */
34874863Spraks 	if (tdvp != sdvp) {
34885331Samw 		vnevent_rename_dest_dir(tdvp, ct);
34894863Spraks 	}
3490789Sahrens 
3491789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
349211935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
349311935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
34941544Seschrock 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
34951544Seschrock 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
349611935SMark.Shellenbaum@Sun.COM 	if (sdzp != tdzp) {
349711935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
349811935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, tdzp);
349911935SMark.Shellenbaum@Sun.COM 	}
350011935SMark.Shellenbaum@Sun.COM 	if (tzp) {
350111935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
350211935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, tzp);
350311935SMark.Shellenbaum@Sun.COM 	}
350411935SMark.Shellenbaum@Sun.COM 
350511935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, szp);
35063461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
35078227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3508789Sahrens 	if (error) {
3509789Sahrens 		if (zl != NULL)
3510789Sahrens 			zfs_rename_unlock(&zl);
3511789Sahrens 		zfs_dirent_unlock(sdl);
3512789Sahrens 		zfs_dirent_unlock(tdl);
351311321SSanjeev.Bagewadi@Sun.COM 
351411321SSanjeev.Bagewadi@Sun.COM 		if (sdzp == tdzp)
351511321SSanjeev.Bagewadi@Sun.COM 			rw_exit(&sdzp->z_name_lock);
351611321SSanjeev.Bagewadi@Sun.COM 
3517789Sahrens 		VN_RELE(ZTOV(szp));
3518789Sahrens 		if (tzp)
3519789Sahrens 			VN_RELE(ZTOV(tzp));
35208227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
35212113Sahrens 			dmu_tx_wait(tx);
35222113Sahrens 			dmu_tx_abort(tx);
3523789Sahrens 			goto top;
3524789Sahrens 		}
35252113Sahrens 		dmu_tx_abort(tx);
3526789Sahrens 		ZFS_EXIT(zfsvfs);
3527789Sahrens 		return (error);
3528789Sahrens 	}
3529789Sahrens 
3530789Sahrens 	if (tzp)	/* Attempt to remove the existing target */
35315331Samw 		error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3532789Sahrens 
3533789Sahrens 	if (error == 0) {
3534789Sahrens 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3535789Sahrens 		if (error == 0) {
353611935SMark.Shellenbaum@Sun.COM 			szp->z_pflags |= ZFS_AV_MODIFIED;
353711935SMark.Shellenbaum@Sun.COM 
353811935SMark.Shellenbaum@Sun.COM 			error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
353911935SMark.Shellenbaum@Sun.COM 			    (void *)&szp->z_pflags, sizeof (uint64_t), tx);
354011935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error, ==, 0);
35415331Samw 
3542789Sahrens 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
354311935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error, ==, 0);
35445331Samw 
35455331Samw 			zfs_log_rename(zilog, tx,
35465331Samw 			    TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0),
35475331Samw 			    sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
35486976Seschrock 
35496976Seschrock 			/* Update path information for the target vnode */
35506976Seschrock 			vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm));
3551789Sahrens 		}
3552789Sahrens 	}
3553789Sahrens 
3554789Sahrens 	dmu_tx_commit(tx);
3555789Sahrens out:
3556789Sahrens 	if (zl != NULL)
3557789Sahrens 		zfs_rename_unlock(&zl);
3558789Sahrens 
3559789Sahrens 	zfs_dirent_unlock(sdl);
3560789Sahrens 	zfs_dirent_unlock(tdl);
3561789Sahrens 
356211321SSanjeev.Bagewadi@Sun.COM 	if (sdzp == tdzp)
356311321SSanjeev.Bagewadi@Sun.COM 		rw_exit(&sdzp->z_name_lock);
356411321SSanjeev.Bagewadi@Sun.COM 
356511321SSanjeev.Bagewadi@Sun.COM 
3566789Sahrens 	VN_RELE(ZTOV(szp));
3567789Sahrens 	if (tzp)
3568789Sahrens 		VN_RELE(ZTOV(tzp));
3569789Sahrens 
3570*12294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3571*12294SMark.Musante@Sun.COM 		zil_commit(zilog, UINT64_MAX, 0);
3572*12294SMark.Musante@Sun.COM 
3573789Sahrens 	ZFS_EXIT(zfsvfs);
3574789Sahrens 	return (error);
3575789Sahrens }
3576789Sahrens 
3577789Sahrens /*
3578789Sahrens  * Insert the indicated symbolic reference entry into the directory.
3579789Sahrens  *
3580789Sahrens  *	IN:	dvp	- Directory to contain new symbolic link.
3581789Sahrens  *		link	- Name for new symlink entry.
3582789Sahrens  *		vap	- Attributes of new entry.
3583789Sahrens  *		target	- Target path of new symlink.
3584789Sahrens  *		cr	- credentials of caller.
35855331Samw  *		ct	- caller context
35865331Samw  *		flags	- case flags
3587789Sahrens  *
3588789Sahrens  *	RETURN:	0 if success
3589789Sahrens  *		error code if failure
3590789Sahrens  *
3591789Sahrens  * Timestamps:
3592789Sahrens  *	dvp - ctime|mtime updated
3593789Sahrens  */
35945331Samw /*ARGSUSED*/
3595789Sahrens static int
35965331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
35975331Samw     caller_context_t *ct, int flags)
3598789Sahrens {
3599789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
3600789Sahrens 	zfs_dirlock_t	*dl;
3601789Sahrens 	dmu_tx_t	*tx;
3602789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
36035326Sek110237 	zilog_t		*zilog;
360411935SMark.Shellenbaum@Sun.COM 	uint64_t	len = strlen(link);
3605789Sahrens 	int		error;
36065331Samw 	int		zflg = ZNEW;
36079179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t	acl_ids;
36089179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
360911935SMark.Shellenbaum@Sun.COM 	uint64_t	txtype = TX_SYMLINK;
3610789Sahrens 
3611789Sahrens 	ASSERT(vap->va_type == VLNK);
3612789Sahrens 
36135367Sahrens 	ZFS_ENTER(zfsvfs);
36145367Sahrens 	ZFS_VERIFY_ZP(dzp);
36155326Sek110237 	zilog = zfsvfs->z_log;
36165331Samw 
36175498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
36185331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
36195331Samw 		ZFS_EXIT(zfsvfs);
36205331Samw 		return (EILSEQ);
36215331Samw 	}
36225331Samw 	if (flags & FIGNORECASE)
36235331Samw 		zflg |= ZCILOOK;
3624789Sahrens top:
36255331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3626789Sahrens 		ZFS_EXIT(zfsvfs);
3627789Sahrens 		return (error);
3628789Sahrens 	}
3629789Sahrens 
3630789Sahrens 	if (len > MAXPATHLEN) {
3631789Sahrens 		ZFS_EXIT(zfsvfs);
3632789Sahrens 		return (ENAMETOOLONG);
3633789Sahrens 	}
3634789Sahrens 
3635789Sahrens 	/*
3636789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3637789Sahrens 	 */
36385331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
36395331Samw 	if (error) {
3640789Sahrens 		ZFS_EXIT(zfsvfs);
3641789Sahrens 		return (error);
3642789Sahrens 	}
3643789Sahrens 
36449179SMark.Shellenbaum@Sun.COM 	VERIFY(0 == zfs_acl_ids_create(dzp, 0, vap, cr, NULL, &acl_ids));
36459396SMatthew.Ahrens@Sun.COM 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
36469396SMatthew.Ahrens@Sun.COM 		zfs_acl_ids_free(&acl_ids);
36479396SMatthew.Ahrens@Sun.COM 		zfs_dirent_unlock(dl);
36489396SMatthew.Ahrens@Sun.COM 		ZFS_EXIT(zfsvfs);
36499396SMatthew.Ahrens@Sun.COM 		return (EDQUOT);
36509396SMatthew.Ahrens@Sun.COM 	}
3651789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
36529179SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
3653789Sahrens 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
36541544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
365511935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
365611935SMark.Shellenbaum@Sun.COM 	    ZFS_SA_BASE_ATTR_SIZE + len);
365711935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
365811935SMark.Shellenbaum@Sun.COM 	if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
365911935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
366011935SMark.Shellenbaum@Sun.COM 		    acl_ids.z_aclp->z_acl_bytes);
366111935SMark.Shellenbaum@Sun.COM 	}
36629396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
36639396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
36648227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3665789Sahrens 	if (error) {
36669179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
3667789Sahrens 		zfs_dirent_unlock(dl);
36688227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
36692113Sahrens 			dmu_tx_wait(tx);
36702113Sahrens 			dmu_tx_abort(tx);
3671789Sahrens 			goto top;
3672789Sahrens 		}
36732113Sahrens 		dmu_tx_abort(tx);
3674789Sahrens 		ZFS_EXIT(zfsvfs);
3675789Sahrens 		return (error);
3676789Sahrens 	}
3677789Sahrens 
3678789Sahrens 	/*
3679789Sahrens 	 * Create a new object for the symlink.
368011935SMark.Shellenbaum@Sun.COM 	 * for version 4 ZPL datsets the symlink will be an SA attribute
3681789Sahrens 	 */
368211935SMark.Shellenbaum@Sun.COM 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
368311935SMark.Shellenbaum@Sun.COM 
368411935SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
368511935SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
368611935SMark.Shellenbaum@Sun.COM 
368711935SMark.Shellenbaum@Sun.COM 	if (zp->z_is_sa)
368811935SMark.Shellenbaum@Sun.COM 		error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
368911935SMark.Shellenbaum@Sun.COM 		    link, len, tx);
369011935SMark.Shellenbaum@Sun.COM 	else
369111935SMark.Shellenbaum@Sun.COM 		zfs_sa_symlink(zp, link, len, tx);
369211935SMark.Shellenbaum@Sun.COM 
369311935SMark.Shellenbaum@Sun.COM 	zp->z_size = len;
369411935SMark.Shellenbaum@Sun.COM 	(void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
369511935SMark.Shellenbaum@Sun.COM 	    &zp->z_size, sizeof (zp->z_size), tx);
3696789Sahrens 	/*
3697789Sahrens 	 * Insert the new object into the directory.
3698789Sahrens 	 */
3699789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
370011935SMark.Shellenbaum@Sun.COM 
370111935SMark.Shellenbaum@Sun.COM 	if (flags & FIGNORECASE)
370211935SMark.Shellenbaum@Sun.COM 		txtype |= TX_CI;
370311935SMark.Shellenbaum@Sun.COM 	zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
37049179SMark.Shellenbaum@Sun.COM 
37059179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_free(&acl_ids);
3706789Sahrens 
3707789Sahrens 	dmu_tx_commit(tx);
3708789Sahrens 
3709789Sahrens 	zfs_dirent_unlock(dl);
3710789Sahrens 
3711789Sahrens 	VN_RELE(ZTOV(zp));
3712789Sahrens 
3713*12294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3714*12294SMark.Musante@Sun.COM 		zil_commit(zilog, UINT64_MAX, 0);
3715*12294SMark.Musante@Sun.COM 
3716789Sahrens 	ZFS_EXIT(zfsvfs);
3717789Sahrens 	return (error);
3718789Sahrens }
3719789Sahrens 
3720789Sahrens /*
3721789Sahrens  * Return, in the buffer contained in the provided uio structure,
3722789Sahrens  * the symbolic path referred to by vp.
3723789Sahrens  *
3724789Sahrens  *	IN:	vp	- vnode of symbolic link.
3725789Sahrens  *		uoip	- structure to contain the link path.
3726789Sahrens  *		cr	- credentials of caller.
37275331Samw  *		ct	- caller context
3728789Sahrens  *
3729789Sahrens  *	OUT:	uio	- structure to contain the link path.
3730789Sahrens  *
3731789Sahrens  *	RETURN:	0 if success
3732789Sahrens  *		error code if failure
3733789Sahrens  *
3734789Sahrens  * Timestamps:
3735789Sahrens  *	vp - atime updated
3736789Sahrens  */
3737789Sahrens /* ARGSUSED */
3738789Sahrens static int
37395331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3740789Sahrens {
3741789Sahrens 	znode_t		*zp = VTOZ(vp);
3742789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3743789Sahrens 	int		error;
3744789Sahrens 
37455367Sahrens 	ZFS_ENTER(zfsvfs);
37465367Sahrens 	ZFS_VERIFY_ZP(zp);
3747789Sahrens 
374811935SMark.Shellenbaum@Sun.COM 	if (zp->z_is_sa)
374911935SMark.Shellenbaum@Sun.COM 		error = sa_lookup_uio(zp->z_sa_hdl,
375011935SMark.Shellenbaum@Sun.COM 		    SA_ZPL_SYMLINK(zfsvfs), uio);
375111935SMark.Shellenbaum@Sun.COM 	else
375211935SMark.Shellenbaum@Sun.COM 		error = zfs_sa_readlink(zp, uio);
3753789Sahrens 
3754789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
375511935SMark.Shellenbaum@Sun.COM 
3756789Sahrens 	ZFS_EXIT(zfsvfs);
3757789Sahrens 	return (error);
3758789Sahrens }
3759789Sahrens 
3760789Sahrens /*
3761789Sahrens  * Insert a new entry into directory tdvp referencing svp.
3762789Sahrens  *
3763789Sahrens  *	IN:	tdvp	- Directory to contain new entry.
3764789Sahrens  *		svp	- vnode of new entry.
3765789Sahrens  *		name	- name of new entry.
3766789Sahrens  *		cr	- credentials of caller.
37675331Samw  *		ct	- caller context
3768789Sahrens  *
3769789Sahrens  *	RETURN:	0 if success
3770789Sahrens  *		error code if failure
3771789Sahrens  *
3772789Sahrens  * Timestamps:
3773789Sahrens  *	tdvp - ctime|mtime updated
3774789Sahrens  *	 svp - ctime updated
3775789Sahrens  */
3776789Sahrens /* ARGSUSED */
3777789Sahrens static int
37785331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
37795331Samw     caller_context_t *ct, int flags)
3780789Sahrens {
3781789Sahrens 	znode_t		*dzp = VTOZ(tdvp);
3782789Sahrens 	znode_t		*tzp, *szp;
3783789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
37845326Sek110237 	zilog_t		*zilog;
3785789Sahrens 	zfs_dirlock_t	*dl;
3786789Sahrens 	dmu_tx_t	*tx;
3787789Sahrens 	vnode_t		*realvp;
3788789Sahrens 	int		error;
37895331Samw 	int		zf = ZNEW;
379012079SMark.Shellenbaum@Sun.COM 	uint64_t	parent;
3791789Sahrens 
3792789Sahrens 	ASSERT(tdvp->v_type == VDIR);
3793789Sahrens 
37945367Sahrens 	ZFS_ENTER(zfsvfs);
37955367Sahrens 	ZFS_VERIFY_ZP(dzp);
37965326Sek110237 	zilog = zfsvfs->z_log;
3797789Sahrens 
37985331Samw 	if (VOP_REALVP(svp, &realvp, ct) == 0)
3799789Sahrens 		svp = realvp;
3800789Sahrens 
380112079SMark.Shellenbaum@Sun.COM 	/*
380212079SMark.Shellenbaum@Sun.COM 	 * POSIX dictates that we return EPERM here.
380312079SMark.Shellenbaum@Sun.COM 	 * Better choices include ENOTSUP or EISDIR.
380412079SMark.Shellenbaum@Sun.COM 	 */
380512079SMark.Shellenbaum@Sun.COM 	if (svp->v_type == VDIR) {
380612079SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
380712079SMark.Shellenbaum@Sun.COM 		return (EPERM);
380812079SMark.Shellenbaum@Sun.COM 	}
380912079SMark.Shellenbaum@Sun.COM 
381012079SMark.Shellenbaum@Sun.COM 	if (svp->v_vfsp != tdvp->v_vfsp || zfsctl_is_node(svp)) {
3811789Sahrens 		ZFS_EXIT(zfsvfs);
3812789Sahrens 		return (EXDEV);
3813789Sahrens 	}
381412079SMark.Shellenbaum@Sun.COM 
38155367Sahrens 	szp = VTOZ(svp);
38165367Sahrens 	ZFS_VERIFY_ZP(szp);
3817789Sahrens 
381812079SMark.Shellenbaum@Sun.COM 	/* Prevent links to .zfs/shares files */
381912079SMark.Shellenbaum@Sun.COM 
382012079SMark.Shellenbaum@Sun.COM 	if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
382112079SMark.Shellenbaum@Sun.COM 	    &parent, sizeof (uint64_t))) != 0) {
382212079SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
382312079SMark.Shellenbaum@Sun.COM 		return (error);
382412079SMark.Shellenbaum@Sun.COM 	}
382512079SMark.Shellenbaum@Sun.COM 	if (parent == zfsvfs->z_shares_dir) {
382612079SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
382712079SMark.Shellenbaum@Sun.COM 		return (EPERM);
382812079SMark.Shellenbaum@Sun.COM 	}
382912079SMark.Shellenbaum@Sun.COM 
38305498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name,
38315331Samw 	    strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
38325331Samw 		ZFS_EXIT(zfsvfs);
38335331Samw 		return (EILSEQ);
38345331Samw 	}
38355331Samw 	if (flags & FIGNORECASE)
38365331Samw 		zf |= ZCILOOK;
38375331Samw 
3838789Sahrens 	/*
3839789Sahrens 	 * We do not support links between attributes and non-attributes
3840789Sahrens 	 * because of the potential security risk of creating links
3841789Sahrens 	 * into "normal" file space in order to circumvent restrictions
3842789Sahrens 	 * imposed in attribute space.
3843789Sahrens 	 */
384411935SMark.Shellenbaum@Sun.COM 	if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
3845789Sahrens 		ZFS_EXIT(zfsvfs);
3846789Sahrens 		return (EINVAL);
3847789Sahrens 	}
3848789Sahrens 
3849789Sahrens 
385011935SMark.Shellenbaum@Sun.COM 	if (szp->z_uid != crgetuid(cr) &&
3851789Sahrens 	    secpolicy_basic_link(cr) != 0) {
3852789Sahrens 		ZFS_EXIT(zfsvfs);
3853789Sahrens 		return (EPERM);
3854789Sahrens 	}
3855789Sahrens 
38565331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3857789Sahrens 		ZFS_EXIT(zfsvfs);
3858789Sahrens 		return (error);
3859789Sahrens 	}
3860789Sahrens 
386112079SMark.Shellenbaum@Sun.COM top:
3862789Sahrens 	/*
3863789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3864789Sahrens 	 */
38655331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
38665331Samw 	if (error) {
3867789Sahrens 		ZFS_EXIT(zfsvfs);
3868789Sahrens 		return (error);
3869789Sahrens 	}
3870789Sahrens 
3871789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
387211935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
38731544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
387411935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, szp);
387511935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, dzp);
38768227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3877789Sahrens 	if (error) {
3878789Sahrens 		zfs_dirent_unlock(dl);
38798227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
38802113Sahrens 			dmu_tx_wait(tx);
38812113Sahrens 			dmu_tx_abort(tx);
3882789Sahrens 			goto top;
3883789Sahrens 		}
38842113Sahrens 		dmu_tx_abort(tx);
3885789Sahrens 		ZFS_EXIT(zfsvfs);
3886789Sahrens 		return (error);
3887789Sahrens 	}
3888789Sahrens 
3889789Sahrens 	error = zfs_link_create(dl, szp, tx, 0);
3890789Sahrens 
38915331Samw 	if (error == 0) {
38925331Samw 		uint64_t txtype = TX_LINK;
38935331Samw 		if (flags & FIGNORECASE)
38945331Samw 			txtype |= TX_CI;
38955331Samw 		zfs_log_link(zilog, tx, txtype, dzp, szp, name);
38965331Samw 	}
3897789Sahrens 
3898789Sahrens 	dmu_tx_commit(tx);
3899789Sahrens 
3900789Sahrens 	zfs_dirent_unlock(dl);
3901789Sahrens 
39024863Spraks 	if (error == 0) {
39035331Samw 		vnevent_link(svp, ct);
39044863Spraks 	}
39054863Spraks 
3906*12294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3907*12294SMark.Musante@Sun.COM 		zil_commit(zilog, UINT64_MAX, 0);
3908*12294SMark.Musante@Sun.COM 
3909789Sahrens 	ZFS_EXIT(zfsvfs);
3910789Sahrens 	return (error);
3911789Sahrens }
3912789Sahrens 
3913789Sahrens /*
3914789Sahrens  * zfs_null_putapage() is used when the file system has been force
3915789Sahrens  * unmounted. It just drops the pages.
3916789Sahrens  */
3917789Sahrens /* ARGSUSED */
3918789Sahrens static int
3919789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3920789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3921789Sahrens {
3922789Sahrens 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
3923789Sahrens 	return (0);
3924789Sahrens }
3925789Sahrens 
39262688Smaybee /*
39272688Smaybee  * Push a page out to disk, klustering if possible.
39282688Smaybee  *
39292688Smaybee  *	IN:	vp	- file to push page to.
39302688Smaybee  *		pp	- page to push.
39312688Smaybee  *		flags	- additional flags.
39322688Smaybee  *		cr	- credentials of caller.
39332688Smaybee  *
39342688Smaybee  *	OUT:	offp	- start of range pushed.
39352688Smaybee  *		lenp	- len of range pushed.
39362688Smaybee  *
39372688Smaybee  *	RETURN:	0 if success
39382688Smaybee  *		error code if failure
39392688Smaybee  *
39402688Smaybee  * NOTE: callers must have locked the page to be pushed.  On
39412688Smaybee  * exit, the page (and all other pages in the kluster) must be
39422688Smaybee  * unlocked.
39432688Smaybee  */
3944789Sahrens /* ARGSUSED */
3945789Sahrens static int
3946789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3947789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3948789Sahrens {
3949789Sahrens 	znode_t		*zp = VTOZ(vp);
3950789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3951789Sahrens 	dmu_tx_t	*tx;
39522688Smaybee 	u_offset_t	off, koff;
39532688Smaybee 	size_t		len, klen;
3954789Sahrens 	int		err;
3955789Sahrens 
39562688Smaybee 	off = pp->p_offset;
39572688Smaybee 	len = PAGESIZE;
39582688Smaybee 	/*
39592688Smaybee 	 * If our blocksize is bigger than the page size, try to kluster
39608227SNeil.Perrin@Sun.COM 	 * multiple pages so that we write a full block (thus avoiding
39612688Smaybee 	 * a read-modify-write).
39622688Smaybee 	 */
396311935SMark.Shellenbaum@Sun.COM 	if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
39648636SMark.Maybee@Sun.COM 		klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
39658636SMark.Maybee@Sun.COM 		koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
396611935SMark.Shellenbaum@Sun.COM 		ASSERT(koff <= zp->z_size);
396711935SMark.Shellenbaum@Sun.COM 		if (koff + klen > zp->z_size)
396811935SMark.Shellenbaum@Sun.COM 			klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
39692688Smaybee 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
39702688Smaybee 	}
39712688Smaybee 	ASSERT3U(btop(len), ==, btopr(len));
39728636SMark.Maybee@Sun.COM 
39731819Smaybee 	/*
39741819Smaybee 	 * Can't push pages past end-of-file.
39751819Smaybee 	 */
397611935SMark.Shellenbaum@Sun.COM 	if (off >= zp->z_size) {
39774709Smaybee 		/* ignore all pages */
39782688Smaybee 		err = 0;
39792688Smaybee 		goto out;
398011935SMark.Shellenbaum@Sun.COM 	} else if (off + len > zp->z_size) {
398111935SMark.Shellenbaum@Sun.COM 		int npages = btopr(zp->z_size - off);
39822688Smaybee 		page_t *trunc;
39832688Smaybee 
39842688Smaybee 		page_list_break(&pp, &trunc, npages);
39854709Smaybee 		/* ignore pages past end of file */
39862688Smaybee 		if (trunc)
39874709Smaybee 			pvn_write_done(trunc, flags);
398811935SMark.Shellenbaum@Sun.COM 		len = zp->z_size - off;
39891819Smaybee 	}
39909396SMatthew.Ahrens@Sun.COM 
399111935SMark.Shellenbaum@Sun.COM 	if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
399211935SMark.Shellenbaum@Sun.COM 	    zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
39939396SMatthew.Ahrens@Sun.COM 		err = EDQUOT;
39949396SMatthew.Ahrens@Sun.COM 		goto out;
39959396SMatthew.Ahrens@Sun.COM 	}
39968636SMark.Maybee@Sun.COM top:
3997789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3998789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, off, len);
399911935SMark.Shellenbaum@Sun.COM 
400011935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
400111935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
40028227SNeil.Perrin@Sun.COM 	err = dmu_tx_assign(tx, TXG_NOWAIT);
4003789Sahrens 	if (err != 0) {
40048227SNeil.Perrin@Sun.COM 		if (err == ERESTART) {
40052113Sahrens 			dmu_tx_wait(tx);
40062113Sahrens 			dmu_tx_abort(tx);
4007789Sahrens 			goto top;
4008789Sahrens 		}
40092113Sahrens 		dmu_tx_abort(tx);
4010789Sahrens 		goto out;
4011789Sahrens 	}
4012789Sahrens 
40132688Smaybee 	if (zp->z_blksz <= PAGESIZE) {
40147315SJonathan.Adams@Sun.COM 		caddr_t va = zfs_map_page(pp, S_READ);
40152688Smaybee 		ASSERT3U(len, <=, PAGESIZE);
40162688Smaybee 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
40177315SJonathan.Adams@Sun.COM 		zfs_unmap_page(pp, va);
40182688Smaybee 	} else {
40192688Smaybee 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
40202688Smaybee 	}
40212688Smaybee 
40222688Smaybee 	if (err == 0) {
402311935SMark.Shellenbaum@Sun.COM 		uint64_t mtime[2], ctime[2];
402411935SMark.Shellenbaum@Sun.COM 		sa_bulk_attr_t bulk[2];
402511935SMark.Shellenbaum@Sun.COM 		int count = 0;
402611935SMark.Shellenbaum@Sun.COM 
402711935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
402811935SMark.Shellenbaum@Sun.COM 		    &mtime, 16);
402911935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
403011935SMark.Shellenbaum@Sun.COM 		    &ctime, 16);
403111935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
403211935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
40338636SMark.Maybee@Sun.COM 		zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
40342688Smaybee 	}
40359951SLin.Ling@Sun.COM 	dmu_tx_commit(tx);
40362688Smaybee 
40372688Smaybee out:
40384709Smaybee 	pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4039789Sahrens 	if (offp)
4040789Sahrens 		*offp = off;
4041789Sahrens 	if (lenp)
4042789Sahrens 		*lenp = len;
4043789Sahrens 
4044789Sahrens 	return (err);
4045789Sahrens }
4046789Sahrens 
4047789Sahrens /*
4048789Sahrens  * Copy the portion of the file indicated from pages into the file.
4049789Sahrens  * The pages are stored in a page list attached to the files vnode.
4050789Sahrens  *
4051789Sahrens  *	IN:	vp	- vnode of file to push page data to.
4052789Sahrens  *		off	- position in file to put data.
4053789Sahrens  *		len	- amount of data to write.
4054789Sahrens  *		flags	- flags to control the operation.
4055789Sahrens  *		cr	- credentials of caller.
40565331Samw  *		ct	- caller context.
4057789Sahrens  *
4058789Sahrens  *	RETURN:	0 if success
4059789Sahrens  *		error code if failure
4060789Sahrens  *
4061789Sahrens  * Timestamps:
4062789Sahrens  *	vp - ctime|mtime updated
4063789Sahrens  */
40645331Samw /*ARGSUSED*/
4065789Sahrens static int
40665331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
40675331Samw     caller_context_t *ct)
4068789Sahrens {
4069789Sahrens 	znode_t		*zp = VTOZ(vp);
4070789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4071789Sahrens 	page_t		*pp;
4072789Sahrens 	size_t		io_len;
4073789Sahrens 	u_offset_t	io_off;
40748636SMark.Maybee@Sun.COM 	uint_t		blksz;
40758636SMark.Maybee@Sun.COM 	rl_t		*rl;
4076789Sahrens 	int		error = 0;
4077789Sahrens 
40785367Sahrens 	ZFS_ENTER(zfsvfs);
40795367Sahrens 	ZFS_VERIFY_ZP(zp);
4080789Sahrens 
40818636SMark.Maybee@Sun.COM 	/*
40828636SMark.Maybee@Sun.COM 	 * Align this request to the file block size in case we kluster.
40838636SMark.Maybee@Sun.COM 	 * XXX - this can result in pretty aggresive locking, which can
40848636SMark.Maybee@Sun.COM 	 * impact simultanious read/write access.  One option might be
40858636SMark.Maybee@Sun.COM 	 * to break up long requests (len == 0) into block-by-block
40868636SMark.Maybee@Sun.COM 	 * operations to get narrower locking.
40878636SMark.Maybee@Sun.COM 	 */
40888636SMark.Maybee@Sun.COM 	blksz = zp->z_blksz;
40898636SMark.Maybee@Sun.COM 	if (ISP2(blksz))
40908636SMark.Maybee@Sun.COM 		io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
40918636SMark.Maybee@Sun.COM 	else
40928636SMark.Maybee@Sun.COM 		io_off = 0;
40938636SMark.Maybee@Sun.COM 	if (len > 0 && ISP2(blksz))
40949141SMark.Maybee@Sun.COM 		io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
40958636SMark.Maybee@Sun.COM 	else
40968636SMark.Maybee@Sun.COM 		io_len = 0;
40978636SMark.Maybee@Sun.COM 
40988636SMark.Maybee@Sun.COM 	if (io_len == 0) {
4099789Sahrens 		/*
41008636SMark.Maybee@Sun.COM 		 * Search the entire vp list for pages >= io_off.
4101789Sahrens 		 */
41028636SMark.Maybee@Sun.COM 		rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
41038636SMark.Maybee@Sun.COM 		error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
41041472Sperrin 		goto out;
4105789Sahrens 	}
41068636SMark.Maybee@Sun.COM 	rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
41078636SMark.Maybee@Sun.COM 
410811935SMark.Shellenbaum@Sun.COM 	if (off > zp->z_size) {
4109789Sahrens 		/* past end of file */
41108636SMark.Maybee@Sun.COM 		zfs_range_unlock(rl);
4111789Sahrens 		ZFS_EXIT(zfsvfs);
4112789Sahrens 		return (0);
4113789Sahrens 	}
4114789Sahrens 
411511935SMark.Shellenbaum@Sun.COM 	len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
41168636SMark.Maybee@Sun.COM 
41178636SMark.Maybee@Sun.COM 	for (off = io_off; io_off < off + len; io_off += io_len) {
4118789Sahrens 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
41191669Sperrin 			pp = page_lookup(vp, io_off,
41204339Sperrin 			    (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4121789Sahrens 		} else {
4122789Sahrens 			pp = page_lookup_nowait(vp, io_off,
41234339Sperrin 			    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4124789Sahrens 		}
4125789Sahrens 
4126789Sahrens 		if (pp != NULL && pvn_getdirty(pp, flags)) {
4127789Sahrens 			int err;
4128789Sahrens 
4129789Sahrens 			/*
4130789Sahrens 			 * Found a dirty page to push
4131789Sahrens 			 */
41321669Sperrin 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
41331669Sperrin 			if (err)
4134789Sahrens 				error = err;
4135789Sahrens 		} else {
4136789Sahrens 			io_len = PAGESIZE;
4137789Sahrens 		}
4138789Sahrens 	}
41391472Sperrin out:
41408636SMark.Maybee@Sun.COM 	zfs_range_unlock(rl);
4141*12294SMark.Musante@Sun.COM 	if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
41422638Sperrin 		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
4143789Sahrens 	ZFS_EXIT(zfsvfs);
4144789Sahrens 	return (error);
4145789Sahrens }
4146789Sahrens 
41475331Samw /*ARGSUSED*/
4148789Sahrens void
41495331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4150789Sahrens {
4151789Sahrens 	znode_t	*zp = VTOZ(vp);
4152789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4153789Sahrens 	int error;
4154789Sahrens 
41555326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
415611935SMark.Shellenbaum@Sun.COM 	if (zp->z_sa_hdl == NULL) {
41575446Sahrens 		/*
41585642Smaybee 		 * The fs has been unmounted, or we did a
41595642Smaybee 		 * suspend/resume and this file no longer exists.
41605446Sahrens 		 */
4161789Sahrens 		if (vn_has_cached_data(vp)) {
4162789Sahrens 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
4163789Sahrens 			    B_INVAL, cr);
4164789Sahrens 		}
4165789Sahrens 
41661544Seschrock 		mutex_enter(&zp->z_lock);
416710369Schris.kirby@sun.com 		mutex_enter(&vp->v_lock);
416810369Schris.kirby@sun.com 		ASSERT(vp->v_count == 1);
416910369Schris.kirby@sun.com 		vp->v_count = 0;
417010369Schris.kirby@sun.com 		mutex_exit(&vp->v_lock);
41715446Sahrens 		mutex_exit(&zp->z_lock);
41725642Smaybee 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
41735446Sahrens 		zfs_znode_free(zp);
4174789Sahrens 		return;
4175789Sahrens 	}
4176789Sahrens 
4177789Sahrens 	/*
4178789Sahrens 	 * Attempt to push any data in the page cache.  If this fails
4179789Sahrens 	 * we will get kicked out later in zfs_zinactive().
4180789Sahrens 	 */
41811298Sperrin 	if (vn_has_cached_data(vp)) {
41821298Sperrin 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
41831298Sperrin 		    cr);
41841298Sperrin 	}
4185789Sahrens 
41863461Sahrens 	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4187789Sahrens 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4188789Sahrens 
418911935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
419011935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, zp);
4191789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
4192789Sahrens 		if (error) {
4193789Sahrens 			dmu_tx_abort(tx);
4194789Sahrens 		} else {
4195789Sahrens 			mutex_enter(&zp->z_lock);
419611935SMark.Shellenbaum@Sun.COM 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
419711935SMark.Shellenbaum@Sun.COM 			    (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4198789Sahrens 			zp->z_atime_dirty = 0;
4199789Sahrens 			mutex_exit(&zp->z_lock);
4200789Sahrens 			dmu_tx_commit(tx);
4201789Sahrens 		}
4202789Sahrens 	}
4203789Sahrens 
4204789Sahrens 	zfs_zinactive(zp);
42055326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
4206789Sahrens }
4207789Sahrens 
4208789Sahrens /*
4209789Sahrens  * Bounds-check the seek operation.
4210789Sahrens  *
4211789Sahrens  *	IN:	vp	- vnode seeking within
4212789Sahrens  *		ooff	- old file offset
4213789Sahrens  *		noffp	- pointer to new file offset
42145331Samw  *		ct	- caller context
4215789Sahrens  *
4216789Sahrens  *	RETURN:	0 if success
4217789Sahrens  *		EINVAL if new offset invalid
4218789Sahrens  */
4219789Sahrens /* ARGSUSED */
4220789Sahrens static int
42215331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
42225331Samw     caller_context_t *ct)
4223789Sahrens {
4224789Sahrens 	if (vp->v_type == VDIR)
4225789Sahrens 		return (0);
4226789Sahrens 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4227789Sahrens }
4228789Sahrens 
4229789Sahrens /*
4230789Sahrens  * Pre-filter the generic locking function to trap attempts to place
4231789Sahrens  * a mandatory lock on a memory mapped file.
4232789Sahrens  */
4233789Sahrens static int
4234789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
42355331Samw     flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4236789Sahrens {
4237789Sahrens 	znode_t *zp = VTOZ(vp);
4238789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4239789Sahrens 
42405367Sahrens 	ZFS_ENTER(zfsvfs);
42415367Sahrens 	ZFS_VERIFY_ZP(zp);
4242789Sahrens 
4243789Sahrens 	/*
42441544Seschrock 	 * We are following the UFS semantics with respect to mapcnt
42451544Seschrock 	 * here: If we see that the file is mapped already, then we will
42461544Seschrock 	 * return an error, but we don't worry about races between this
42471544Seschrock 	 * function and zfs_map().
4248789Sahrens 	 */
424911935SMark.Shellenbaum@Sun.COM 	if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4250789Sahrens 		ZFS_EXIT(zfsvfs);
4251789Sahrens 		return (EAGAIN);
4252789Sahrens 	}
4253789Sahrens 	ZFS_EXIT(zfsvfs);
425410896SMark.Shellenbaum@Sun.COM 	return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4255789Sahrens }
4256789Sahrens 
4257789Sahrens /*
4258789Sahrens  * If we can't find a page in the cache, we will create a new page
4259789Sahrens  * and fill it with file data.  For efficiency, we may try to fill
42608636SMark.Maybee@Sun.COM  * multiple pages at once (klustering) to fill up the supplied page
42619265SMark.Maybee@Sun.COM  * list.  Note that the pages to be filled are held with an exclusive
42629265SMark.Maybee@Sun.COM  * lock to prevent access by other threads while they are being filled.
4263789Sahrens  */
4264789Sahrens static int
4265789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4266789Sahrens     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4267789Sahrens {
4268789Sahrens 	znode_t *zp = VTOZ(vp);
4269789Sahrens 	page_t *pp, *cur_pp;
4270789Sahrens 	objset_t *os = zp->z_zfsvfs->z_os;
4271789Sahrens 	u_offset_t io_off, total;
4272789Sahrens 	size_t io_len;
4273789Sahrens 	int err;
4274789Sahrens 
42752688Smaybee 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
42768636SMark.Maybee@Sun.COM 		/*
42778636SMark.Maybee@Sun.COM 		 * We only have a single page, don't bother klustering
42788636SMark.Maybee@Sun.COM 		 */
4279789Sahrens 		io_off = off;
4280789Sahrens 		io_len = PAGESIZE;
42819265SMark.Maybee@Sun.COM 		pp = page_create_va(vp, io_off, io_len,
42829265SMark.Maybee@Sun.COM 		    PG_EXCL | PG_WAIT, seg, addr);
4283789Sahrens 	} else {
4284789Sahrens 		/*
42858636SMark.Maybee@Sun.COM 		 * Try to find enough pages to fill the page list
4286789Sahrens 		 */
4287789Sahrens 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
42888636SMark.Maybee@Sun.COM 		    &io_len, off, plsz, 0);
4289789Sahrens 	}
4290789Sahrens 	if (pp == NULL) {
4291789Sahrens 		/*
42928636SMark.Maybee@Sun.COM 		 * The page already exists, nothing to do here.
4293789Sahrens 		 */
4294789Sahrens 		*pl = NULL;
4295789Sahrens 		return (0);
4296789Sahrens 	}
4297789Sahrens 
4298789Sahrens 	/*
4299789Sahrens 	 * Fill the pages in the kluster.
4300789Sahrens 	 */
4301789Sahrens 	cur_pp = pp;
4302789Sahrens 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
43038636SMark.Maybee@Sun.COM 		caddr_t va;
43048636SMark.Maybee@Sun.COM 
43052688Smaybee 		ASSERT3U(io_off, ==, cur_pp->p_offset);
43067315SJonathan.Adams@Sun.COM 		va = zfs_map_page(cur_pp, S_WRITE);
43079512SNeil.Perrin@Sun.COM 		err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
43089512SNeil.Perrin@Sun.COM 		    DMU_READ_PREFETCH);
43097315SJonathan.Adams@Sun.COM 		zfs_unmap_page(cur_pp, va);
4310789Sahrens 		if (err) {
4311789Sahrens 			/* On error, toss the entire kluster */
4312789Sahrens 			pvn_read_done(pp, B_ERROR);
43137294Sperrin 			/* convert checksum errors into IO errors */
43147294Sperrin 			if (err == ECKSUM)
43157294Sperrin 				err = EIO;
4316789Sahrens 			return (err);
4317789Sahrens 		}
4318789Sahrens 		cur_pp = cur_pp->p_next;
4319789Sahrens 	}
43208636SMark.Maybee@Sun.COM 
4321789Sahrens 	/*
43228636SMark.Maybee@Sun.COM 	 * Fill in the page list array from the kluster starting
43238636SMark.Maybee@Sun.COM 	 * from the desired offset `off'.
4324789Sahrens 	 * NOTE: the page list will always be null terminated.
4325789Sahrens 	 */
4326789Sahrens 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
43278636SMark.Maybee@Sun.COM 	ASSERT(pl == NULL || (*pl)->p_offset == off);
4328789Sahrens 
4329789Sahrens 	return (0);
4330789Sahrens }
4331789Sahrens 
4332789Sahrens /*
4333789Sahrens  * Return pointers to the pages for the file region [off, off + len]
4334789Sahrens  * in the pl array.  If plsz is greater than len, this function may
43358636SMark.Maybee@Sun.COM  * also return page pointers from after the specified region
43368636SMark.Maybee@Sun.COM  * (i.e. the region [off, off + plsz]).  These additional pages are
43378636SMark.Maybee@Sun.COM  * only returned if they are already in the cache, or were created as
43388636SMark.Maybee@Sun.COM  * part of a klustered read.
4339789Sahrens  *
4340789Sahrens  *	IN:	vp	- vnode of file to get data from.
4341789Sahrens  *		off	- position in file to get data from.
4342789Sahrens  *		len	- amount of data to retrieve.
4343789Sahrens  *		plsz	- length of provided page list.
4344789Sahrens  *		seg	- segment to obtain pages for.
4345789Sahrens  *		addr	- virtual address of fault.
4346789Sahrens  *		rw	- mode of created pages.
4347789Sahrens  *		cr	- credentials of caller.
43485331Samw  *		ct	- caller context.
4349789Sahrens  *
4350789Sahrens  *	OUT:	protp	- protection mode of created pages.
4351789Sahrens  *		pl	- list of pages created.
4352789Sahrens  *
4353789Sahrens  *	RETURN:	0 if success
4354789Sahrens  *		error code if failure
4355789Sahrens  *
4356789Sahrens  * Timestamps:
4357789Sahrens  *	vp - atime updated
4358789Sahrens  */
4359789Sahrens /* ARGSUSED */
4360789Sahrens static int
4361789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4362789Sahrens 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
43635331Samw 	enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4364789Sahrens {
4365789Sahrens 	znode_t		*zp = VTOZ(vp);
4366789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
43678636SMark.Maybee@Sun.COM 	page_t		**pl0 = pl;
43688636SMark.Maybee@Sun.COM 	int		err = 0;
43698636SMark.Maybee@Sun.COM 
43708636SMark.Maybee@Sun.COM 	/* we do our own caching, faultahead is unnecessary */
43718636SMark.Maybee@Sun.COM 	if (pl == NULL)
43728636SMark.Maybee@Sun.COM 		return (0);
43738636SMark.Maybee@Sun.COM 	else if (len > plsz)
43748636SMark.Maybee@Sun.COM 		len = plsz;
43758681SMark.Maybee@Sun.COM 	else
43768681SMark.Maybee@Sun.COM 		len = P2ROUNDUP(len, PAGESIZE);
43778636SMark.Maybee@Sun.COM 	ASSERT(plsz >= len);
4378789Sahrens 
43795367Sahrens 	ZFS_ENTER(zfsvfs);
43805367Sahrens 	ZFS_VERIFY_ZP(zp);
4381789Sahrens 
4382789Sahrens 	if (protp)
4383789Sahrens 		*protp = PROT_ALL;
4384789Sahrens 
4385789Sahrens 	/*
43869265SMark.Maybee@Sun.COM 	 * Loop through the requested range [off, off + len) looking
4387789Sahrens 	 * for pages.  If we don't find a page, we will need to create
4388789Sahrens 	 * a new page and fill it with data from the file.
4389789Sahrens 	 */
4390789Sahrens 	while (len > 0) {
43918636SMark.Maybee@Sun.COM 		if (*pl = page_lookup(vp, off, SE_SHARED))
43928636SMark.Maybee@Sun.COM 			*(pl+1) = NULL;
43938636SMark.Maybee@Sun.COM 		else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
43948636SMark.Maybee@Sun.COM 			goto out;
43958636SMark.Maybee@Sun.COM 		while (*pl) {
43968636SMark.Maybee@Sun.COM 			ASSERT3U((*pl)->p_offset, ==, off);
4397789Sahrens 			off += PAGESIZE;
4398789Sahrens 			addr += PAGESIZE;
43998681SMark.Maybee@Sun.COM 			if (len > 0) {
44008681SMark.Maybee@Sun.COM 				ASSERT3U(len, >=, PAGESIZE);
44018636SMark.Maybee@Sun.COM 				len -= PAGESIZE;
44028681SMark.Maybee@Sun.COM 			}
44038636SMark.Maybee@Sun.COM 			ASSERT3U(plsz, >=, PAGESIZE);
4404789Sahrens 			plsz -= PAGESIZE;
44058636SMark.Maybee@Sun.COM 			pl++;
4406789Sahrens 		}
4407789Sahrens 	}
4408789Sahrens 
4409789Sahrens 	/*
4410789Sahrens 	 * Fill out the page array with any pages already in the cache.
4411789Sahrens 	 */
44128636SMark.Maybee@Sun.COM 	while (plsz > 0 &&
44138636SMark.Maybee@Sun.COM 	    (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
44148636SMark.Maybee@Sun.COM 			off += PAGESIZE;
44158636SMark.Maybee@Sun.COM 			plsz -= PAGESIZE;
4416789Sahrens 	}
4417789Sahrens out:
44182752Sperrin 	if (err) {
44192752Sperrin 		/*
44202752Sperrin 		 * Release any pages we have previously locked.
44212752Sperrin 		 */
44222752Sperrin 		while (pl > pl0)
44232752Sperrin 			page_unlock(*--pl);
44248636SMark.Maybee@Sun.COM 	} else {
44258636SMark.Maybee@Sun.COM 		ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
44262752Sperrin 	}
44272752Sperrin 
4428789Sahrens 	*pl = NULL;
4429789Sahrens 
4430789Sahrens 	ZFS_EXIT(zfsvfs);
4431789Sahrens 	return (err);
4432789Sahrens }
4433789Sahrens 
44341544Seschrock /*
44351544Seschrock  * Request a memory map for a section of a file.  This code interacts
44361544Seschrock  * with common code and the VM system as follows:
44371544Seschrock  *
44381544Seschrock  *	common code calls mmap(), which ends up in smmap_common()
44391544Seschrock  *
44401544Seschrock  *	this calls VOP_MAP(), which takes you into (say) zfs
44411544Seschrock  *
44421544Seschrock  *	zfs_map() calls as_map(), passing segvn_create() as the callback
44431544Seschrock  *
44441544Seschrock  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
44451544Seschrock  *
44461544Seschrock  *	zfs_addmap() updates z_mapcnt
44471544Seschrock  */
44485331Samw /*ARGSUSED*/
4449789Sahrens static int
4450789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
44515331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
44525331Samw     caller_context_t *ct)
4453789Sahrens {
4454789Sahrens 	znode_t *zp = VTOZ(vp);
4455789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4456789Sahrens 	segvn_crargs_t	vn_a;
4457789Sahrens 	int		error;
4458789Sahrens 
44595929Smarks 	ZFS_ENTER(zfsvfs);
44605929Smarks 	ZFS_VERIFY_ZP(zp);
44615929Smarks 
446211935SMark.Shellenbaum@Sun.COM 	if ((prot & PROT_WRITE) && (zp->z_pflags &
446311935SMark.Shellenbaum@Sun.COM 	    (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
44645929Smarks 		ZFS_EXIT(zfsvfs);
44655331Samw 		return (EPERM);
44665929Smarks 	}
44675929Smarks 
44685929Smarks 	if ((prot & (PROT_READ | PROT_EXEC)) &&
446911935SMark.Shellenbaum@Sun.COM 	    (zp->z_pflags & ZFS_AV_QUARANTINED)) {
44705929Smarks 		ZFS_EXIT(zfsvfs);
44715929Smarks 		return (EACCES);
44725929Smarks 	}
4473789Sahrens 
4474789Sahrens 	if (vp->v_flag & VNOMAP) {
4475789Sahrens 		ZFS_EXIT(zfsvfs);
4476789Sahrens 		return (ENOSYS);
4477789Sahrens 	}
4478789Sahrens 
4479789Sahrens 	if (off < 0 || len > MAXOFFSET_T - off) {
4480789Sahrens 		ZFS_EXIT(zfsvfs);
4481789Sahrens 		return (ENXIO);
4482789Sahrens 	}
4483789Sahrens 
4484789Sahrens 	if (vp->v_type != VREG) {
4485789Sahrens 		ZFS_EXIT(zfsvfs);
4486789Sahrens 		return (ENODEV);
4487789Sahrens 	}
4488789Sahrens 
4489789Sahrens 	/*
4490789Sahrens 	 * If file is locked, disallow mapping.
4491789Sahrens 	 */
449211935SMark.Shellenbaum@Sun.COM 	if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
44931544Seschrock 		ZFS_EXIT(zfsvfs);
44941544Seschrock 		return (EAGAIN);
4495789Sahrens 	}
4496789Sahrens 
4497789Sahrens 	as_rangelock(as);
44986036Smec 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
44996036Smec 	if (error != 0) {
45006036Smec 		as_rangeunlock(as);
45016036Smec 		ZFS_EXIT(zfsvfs);
45026036Smec 		return (error);
4503789Sahrens 	}
4504789Sahrens 
4505789Sahrens 	vn_a.vp = vp;
4506789Sahrens 	vn_a.offset = (u_offset_t)off;
4507789Sahrens 	vn_a.type = flags & MAP_TYPE;
4508789Sahrens 	vn_a.prot = prot;
4509789Sahrens 	vn_a.maxprot = maxprot;
4510789Sahrens 	vn_a.cred = cr;
4511789Sahrens 	vn_a.amp = NULL;
4512789Sahrens 	vn_a.flags = flags & ~MAP_TYPE;
45131417Skchow 	vn_a.szc = 0;
45141417Skchow 	vn_a.lgrp_mem_policy_flags = 0;
4515789Sahrens 
4516789Sahrens 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
4517789Sahrens 
4518789Sahrens 	as_rangeunlock(as);
4519789Sahrens 	ZFS_EXIT(zfsvfs);
4520789Sahrens 	return (error);
4521789Sahrens }
4522789Sahrens 
4523789Sahrens /* ARGSUSED */
4524789Sahrens static int
4525789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
45265331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
45275331Samw     caller_context_t *ct)
4528789Sahrens {
45291544Seschrock 	uint64_t pages = btopr(len);
45301544Seschrock 
45311544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4532789Sahrens 	return (0);
4533789Sahrens }
4534789Sahrens 
45351773Seschrock /*
45361773Seschrock  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
45371773Seschrock  * more accurate mtime for the associated file.  Since we don't have a way of
45381773Seschrock  * detecting when the data was actually modified, we have to resort to
45391773Seschrock  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
45401773Seschrock  * last page is pushed.  The problem occurs when the msync() call is omitted,
45411773Seschrock  * which by far the most common case:
45421773Seschrock  *
45431773Seschrock  * 	open()
45441773Seschrock  * 	mmap()
45451773Seschrock  * 	<modify memory>
45461773Seschrock  * 	munmap()
45471773Seschrock  * 	close()
45481773Seschrock  * 	<time lapse>
45491773Seschrock  * 	putpage() via fsflush
45501773Seschrock  *
45511773Seschrock  * If we wait until fsflush to come along, we can have a modification time that
45521773Seschrock  * is some arbitrary point in the future.  In order to prevent this in the
45531773Seschrock  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
45541773Seschrock  * torn down.
45551773Seschrock  */
4556789Sahrens /* ARGSUSED */
4557789Sahrens static int
4558789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
45595331Samw     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
45605331Samw     caller_context_t *ct)
4561789Sahrens {
45621544Seschrock 	uint64_t pages = btopr(len);
45631544Seschrock 
45641544Seschrock 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
45651544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
45661773Seschrock 
45671773Seschrock 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
45681773Seschrock 	    vn_has_cached_data(vp))
45695331Samw 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
45701773Seschrock 
4571789Sahrens 	return (0);
4572789Sahrens }
4573789Sahrens 
4574789Sahrens /*
4575789Sahrens  * Free or allocate space in a file.  Currently, this function only
4576789Sahrens  * supports the `F_FREESP' command.  However, this command is somewhat
4577789Sahrens  * misnamed, as its functionality includes the ability to allocate as
4578789Sahrens  * well as free space.
4579789Sahrens  *
4580789Sahrens  *	IN:	vp	- vnode of file to free data in.
4581789Sahrens  *		cmd	- action to take (only F_FREESP supported).
4582789Sahrens  *		bfp	- section of file to free/alloc.
4583789Sahrens  *		flag	- current file open mode flags.
4584789Sahrens  *		offset	- current file offset.
4585789Sahrens  *		cr	- credentials of caller [UNUSED].
45865331Samw  *		ct	- caller context.
4587789Sahrens  *
4588789Sahrens  *	RETURN:	0 if success
4589789Sahrens  *		error code if failure
4590789Sahrens  *
4591789Sahrens  * Timestamps:
4592789Sahrens  *	vp - ctime|mtime updated
4593789Sahrens  */
4594789Sahrens /* ARGSUSED */
4595789Sahrens static int
4596789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4597789Sahrens     offset_t offset, cred_t *cr, caller_context_t *ct)
4598789Sahrens {
4599789Sahrens 	znode_t		*zp = VTOZ(vp);
4600789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4601789Sahrens 	uint64_t	off, len;
4602789Sahrens 	int		error;
4603789Sahrens 
46045367Sahrens 	ZFS_ENTER(zfsvfs);
46055367Sahrens 	ZFS_VERIFY_ZP(zp);
4606789Sahrens 
4607789Sahrens 	if (cmd != F_FREESP) {
4608789Sahrens 		ZFS_EXIT(zfsvfs);
4609789Sahrens 		return (EINVAL);
4610789Sahrens 	}
4611789Sahrens 
4612789Sahrens 	if (error = convoff(vp, bfp, 0, offset)) {
4613789Sahrens 		ZFS_EXIT(zfsvfs);
4614789Sahrens 		return (error);
4615789Sahrens 	}
4616789Sahrens 
4617789Sahrens 	if (bfp->l_len < 0) {
4618789Sahrens 		ZFS_EXIT(zfsvfs);
4619789Sahrens 		return (EINVAL);
4620789Sahrens 	}
4621789Sahrens 
4622789Sahrens 	off = bfp->l_start;
46231669Sperrin 	len = bfp->l_len; /* 0 means from off to end of file */
46241878Smaybee 
46256992Smaybee 	error = zfs_freesp(zp, off, len, flag, TRUE);
4626789Sahrens 
4627789Sahrens 	ZFS_EXIT(zfsvfs);
4628789Sahrens 	return (error);
4629789Sahrens }
4630789Sahrens 
46315331Samw /*ARGSUSED*/
4632789Sahrens static int
46335331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4634789Sahrens {
4635789Sahrens 	znode_t		*zp = VTOZ(vp);
4636789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
46375326Sek110237 	uint32_t	gen;
463811935SMark.Shellenbaum@Sun.COM 	uint64_t	gen64;
4639789Sahrens 	uint64_t	object = zp->z_id;
4640789Sahrens 	zfid_short_t	*zfid;
464111935SMark.Shellenbaum@Sun.COM 	int		size, i, error;
4642789Sahrens 
46435367Sahrens 	ZFS_ENTER(zfsvfs);
46445367Sahrens 	ZFS_VERIFY_ZP(zp);
464511935SMark.Shellenbaum@Sun.COM 
464611935SMark.Shellenbaum@Sun.COM 	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
464712218SMark.Shellenbaum@Sun.COM 	    &gen64, sizeof (uint64_t))) != 0) {
464812218SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
464911935SMark.Shellenbaum@Sun.COM 		return (error);
465012218SMark.Shellenbaum@Sun.COM 	}
465111935SMark.Shellenbaum@Sun.COM 
465211935SMark.Shellenbaum@Sun.COM 	gen = (uint32_t)gen64;
4653789Sahrens 
4654789Sahrens 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4655789Sahrens 	if (fidp->fid_len < size) {
4656789Sahrens 		fidp->fid_len = size;
46571512Sek110237 		ZFS_EXIT(zfsvfs);
4658789Sahrens 		return (ENOSPC);
4659789Sahrens 	}
4660789Sahrens 
4661789Sahrens 	zfid = (zfid_short_t *)fidp;
4662789Sahrens 
4663789Sahrens 	zfid->zf_len = size;
4664789Sahrens 
4665789Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
4666789Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4667789Sahrens 
4668789Sahrens 	/* Must have a non-zero generation number to distinguish from .zfs */
4669789Sahrens 	if (gen == 0)
4670789Sahrens 		gen = 1;
4671789Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
4672789Sahrens 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4673789Sahrens 
4674789Sahrens 	if (size == LONG_FID_LEN) {
4675789Sahrens 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
4676789Sahrens 		zfid_long_t	*zlfid;
4677789Sahrens 
4678789Sahrens 		zlfid = (zfid_long_t *)fidp;
4679789Sahrens 
4680789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4681789Sahrens 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4682789Sahrens 
4683789Sahrens 		/* XXX - this should be the generation number for the objset */
4684789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4685789Sahrens 			zlfid->zf_setgen[i] = 0;
4686789Sahrens 	}
4687789Sahrens 
4688789Sahrens 	ZFS_EXIT(zfsvfs);
4689789Sahrens 	return (0);
4690789Sahrens }
4691789Sahrens 
4692789Sahrens static int
46935331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
46945331Samw     caller_context_t *ct)
4695789Sahrens {
4696789Sahrens 	znode_t		*zp, *xzp;
4697789Sahrens 	zfsvfs_t	*zfsvfs;
4698789Sahrens 	zfs_dirlock_t	*dl;
4699789Sahrens 	int		error;
4700789Sahrens 
4701789Sahrens 	switch (cmd) {
4702789Sahrens 	case _PC_LINK_MAX:
4703789Sahrens 		*valp = ULONG_MAX;
4704789Sahrens 		return (0);
4705789Sahrens 
4706789Sahrens 	case _PC_FILESIZEBITS:
4707789Sahrens 		*valp = 64;
4708789Sahrens 		return (0);
4709789Sahrens 
4710789Sahrens 	case _PC_XATTR_EXISTS:
4711789Sahrens 		zp = VTOZ(vp);
4712789Sahrens 		zfsvfs = zp->z_zfsvfs;
47135367Sahrens 		ZFS_ENTER(zfsvfs);
47145367Sahrens 		ZFS_VERIFY_ZP(zp);
4715789Sahrens 		*valp = 0;
4716789Sahrens 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
47175331Samw 		    ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4718789Sahrens 		if (error == 0) {
4719789Sahrens 			zfs_dirent_unlock(dl);
4720789Sahrens 			if (!zfs_dirempty(xzp))
4721789Sahrens 				*valp = 1;
4722789Sahrens 			VN_RELE(ZTOV(xzp));
4723789Sahrens 		} else if (error == ENOENT) {
4724789Sahrens 			/*
4725789Sahrens 			 * If there aren't extended attributes, it's the
4726789Sahrens 			 * same as having zero of them.
4727789Sahrens 			 */
4728789Sahrens 			error = 0;
4729789Sahrens 		}
4730789Sahrens 		ZFS_EXIT(zfsvfs);
4731789Sahrens 		return (error);
4732789Sahrens 
47335331Samw 	case _PC_SATTR_ENABLED:
47345331Samw 	case _PC_SATTR_EXISTS:
47357757SJanice.Chang@Sun.COM 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
47365331Samw 		    (vp->v_type == VREG || vp->v_type == VDIR);
47375331Samw 		return (0);
47385331Samw 
47399749STim.Haley@Sun.COM 	case _PC_ACCESS_FILTERING:
47409749STim.Haley@Sun.COM 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
47419749STim.Haley@Sun.COM 		    vp->v_type == VDIR;
47429749STim.Haley@Sun.COM 		return (0);
47439749STim.Haley@Sun.COM 
4744789Sahrens 	case _PC_ACL_ENABLED:
4745789Sahrens 		*valp = _ACL_ACE_ENABLED;
4746789Sahrens 		return (0);
4747789Sahrens 
4748789Sahrens 	case _PC_MIN_HOLE_SIZE:
4749789Sahrens 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
4750789Sahrens 		return (0);
4751789Sahrens 
475210440SRoger.Faulkner@Sun.COM 	case _PC_TIMESTAMP_RESOLUTION:
475310440SRoger.Faulkner@Sun.COM 		/* nanosecond timestamp resolution */
475410440SRoger.Faulkner@Sun.COM 		*valp = 1L;
475510440SRoger.Faulkner@Sun.COM 		return (0);
475610440SRoger.Faulkner@Sun.COM 
4757789Sahrens 	default:
47585331Samw 		return (fs_pathconf(vp, cmd, valp, cr, ct));
4759789Sahrens 	}
4760789Sahrens }
4761789Sahrens 
4762789Sahrens /*ARGSUSED*/
4763789Sahrens static int
47645331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
47655331Samw     caller_context_t *ct)
4766789Sahrens {
4767789Sahrens 	znode_t *zp = VTOZ(vp);
4768789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4769789Sahrens 	int error;
47705331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4771789Sahrens 
47725367Sahrens 	ZFS_ENTER(zfsvfs);
47735367Sahrens 	ZFS_VERIFY_ZP(zp);
47745331Samw 	error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4775789Sahrens 	ZFS_EXIT(zfsvfs);
4776789Sahrens 
4777789Sahrens 	return (error);
4778789Sahrens }
4779789Sahrens 
4780789Sahrens /*ARGSUSED*/
4781789Sahrens static int
47825331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
47835331Samw     caller_context_t *ct)
4784789Sahrens {
4785789Sahrens 	znode_t *zp = VTOZ(vp);
4786789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4787789Sahrens 	int error;
47885331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4789*12294SMark.Musante@Sun.COM 	zilog_t	*zilog = zfsvfs->z_log;
4790789Sahrens 
47915367Sahrens 	ZFS_ENTER(zfsvfs);
47925367Sahrens 	ZFS_VERIFY_ZP(zp);
4793*12294SMark.Musante@Sun.COM 
47945331Samw 	error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4795*12294SMark.Musante@Sun.COM 
4796*12294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4797*12294SMark.Musante@Sun.COM 		zil_commit(zilog, UINT64_MAX, 0);
4798*12294SMark.Musante@Sun.COM 
4799789Sahrens 	ZFS_EXIT(zfsvfs);
4800789Sahrens 	return (error);
4801789Sahrens }
4802789Sahrens 
4803789Sahrens /*
480411539SChunli.Zhang@Sun.COM  * Tunable, both must be a power of 2.
480511539SChunli.Zhang@Sun.COM  *
480611539SChunli.Zhang@Sun.COM  * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
480711539SChunli.Zhang@Sun.COM  * zcr_blksz_max: if set to less than the file block size, allow loaning out of
480811539SChunli.Zhang@Sun.COM  *                an arcbuf for a partial block read
480911539SChunli.Zhang@Sun.COM  */
481011539SChunli.Zhang@Sun.COM int zcr_blksz_min = (1 << 10);	/* 1K */
481111539SChunli.Zhang@Sun.COM int zcr_blksz_max = (1 << 17);	/* 128K */
481211539SChunli.Zhang@Sun.COM 
481311539SChunli.Zhang@Sun.COM /*ARGSUSED*/
481411539SChunli.Zhang@Sun.COM static int
481511539SChunli.Zhang@Sun.COM zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
481611539SChunli.Zhang@Sun.COM     caller_context_t *ct)
481711539SChunli.Zhang@Sun.COM {
481811539SChunli.Zhang@Sun.COM 	znode_t	*zp = VTOZ(vp);
481911539SChunli.Zhang@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
482011539SChunli.Zhang@Sun.COM 	int max_blksz = zfsvfs->z_max_blksz;
482111539SChunli.Zhang@Sun.COM 	uio_t *uio = &xuio->xu_uio;
482211539SChunli.Zhang@Sun.COM 	ssize_t size = uio->uio_resid;
482311539SChunli.Zhang@Sun.COM 	offset_t offset = uio->uio_loffset;
482411539SChunli.Zhang@Sun.COM 	int blksz;
482511539SChunli.Zhang@Sun.COM 	int fullblk, i;
482611539SChunli.Zhang@Sun.COM 	arc_buf_t *abuf;
482711539SChunli.Zhang@Sun.COM 	ssize_t maxsize;
482811539SChunli.Zhang@Sun.COM 	int preamble, postamble;
482911539SChunli.Zhang@Sun.COM 
483011539SChunli.Zhang@Sun.COM 	if (xuio->xu_type != UIOTYPE_ZEROCOPY)
483111539SChunli.Zhang@Sun.COM 		return (EINVAL);
483211539SChunli.Zhang@Sun.COM 
483311539SChunli.Zhang@Sun.COM 	ZFS_ENTER(zfsvfs);
483411539SChunli.Zhang@Sun.COM 	ZFS_VERIFY_ZP(zp);
483511539SChunli.Zhang@Sun.COM 	switch (ioflag) {
483611539SChunli.Zhang@Sun.COM 	case UIO_WRITE:
483711539SChunli.Zhang@Sun.COM 		/*
483811539SChunli.Zhang@Sun.COM 		 * Loan out an arc_buf for write if write size is bigger than
483911539SChunli.Zhang@Sun.COM 		 * max_blksz, and the file's block size is also max_blksz.
484011539SChunli.Zhang@Sun.COM 		 */
484111539SChunli.Zhang@Sun.COM 		blksz = max_blksz;
484211539SChunli.Zhang@Sun.COM 		if (size < blksz || zp->z_blksz != blksz) {
484311539SChunli.Zhang@Sun.COM 			ZFS_EXIT(zfsvfs);
484411539SChunli.Zhang@Sun.COM 			return (EINVAL);
484511539SChunli.Zhang@Sun.COM 		}
484611539SChunli.Zhang@Sun.COM 		/*
484711539SChunli.Zhang@Sun.COM 		 * Caller requests buffers for write before knowing where the
484811539SChunli.Zhang@Sun.COM 		 * write offset might be (e.g. NFS TCP write).
484911539SChunli.Zhang@Sun.COM 		 */
485011539SChunli.Zhang@Sun.COM 		if (offset == -1) {
485111539SChunli.Zhang@Sun.COM 			preamble = 0;
485211539SChunli.Zhang@Sun.COM 		} else {
485311539SChunli.Zhang@Sun.COM 			preamble = P2PHASE(offset, blksz);
485411539SChunli.Zhang@Sun.COM 			if (preamble) {
485511539SChunli.Zhang@Sun.COM 				preamble = blksz - preamble;
485611539SChunli.Zhang@Sun.COM 				size -= preamble;
485711539SChunli.Zhang@Sun.COM 			}
485811539SChunli.Zhang@Sun.COM 		}
485911539SChunli.Zhang@Sun.COM 
486011539SChunli.Zhang@Sun.COM 		postamble = P2PHASE(size, blksz);
486111539SChunli.Zhang@Sun.COM 		size -= postamble;
486211539SChunli.Zhang@Sun.COM 
486311539SChunli.Zhang@Sun.COM 		fullblk = size / blksz;
486411576SSurya.Prakki@Sun.COM 		(void) dmu_xuio_init(xuio,
486511539SChunli.Zhang@Sun.COM 		    (preamble != 0) + fullblk + (postamble != 0));
486611539SChunli.Zhang@Sun.COM 		DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
486711539SChunli.Zhang@Sun.COM 		    int, postamble, int,
486811539SChunli.Zhang@Sun.COM 		    (preamble != 0) + fullblk + (postamble != 0));
486911539SChunli.Zhang@Sun.COM 
487011539SChunli.Zhang@Sun.COM 		/*
487111539SChunli.Zhang@Sun.COM 		 * Have to fix iov base/len for partial buffers.  They
487211539SChunli.Zhang@Sun.COM 		 * currently represent full arc_buf's.
487311539SChunli.Zhang@Sun.COM 		 */
487411539SChunli.Zhang@Sun.COM 		if (preamble) {
487511539SChunli.Zhang@Sun.COM 			/* data begins in the middle of the arc_buf */
487611935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
487711935SMark.Shellenbaum@Sun.COM 			    blksz);
487811539SChunli.Zhang@Sun.COM 			ASSERT(abuf);
487911576SSurya.Prakki@Sun.COM 			(void) dmu_xuio_add(xuio, abuf,
488011576SSurya.Prakki@Sun.COM 			    blksz - preamble, preamble);
488111539SChunli.Zhang@Sun.COM 		}
488211539SChunli.Zhang@Sun.COM 
488311539SChunli.Zhang@Sun.COM 		for (i = 0; i < fullblk; i++) {
488411935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
488511935SMark.Shellenbaum@Sun.COM 			    blksz);
488611539SChunli.Zhang@Sun.COM 			ASSERT(abuf);
488711576SSurya.Prakki@Sun.COM 			(void) dmu_xuio_add(xuio, abuf, 0, blksz);
488811539SChunli.Zhang@Sun.COM 		}
488911539SChunli.Zhang@Sun.COM 
489011539SChunli.Zhang@Sun.COM 		if (postamble) {
489111539SChunli.Zhang@Sun.COM 			/* data ends in the middle of the arc_buf */
489211935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
489311935SMark.Shellenbaum@Sun.COM 			    blksz);
489411539SChunli.Zhang@Sun.COM 			ASSERT(abuf);
489511576SSurya.Prakki@Sun.COM 			(void) dmu_xuio_add(xuio, abuf, 0, postamble);
489611539SChunli.Zhang@Sun.COM 		}
489711539SChunli.Zhang@Sun.COM 		break;
489811539SChunli.Zhang@Sun.COM 	case UIO_READ:
489911539SChunli.Zhang@Sun.COM 		/*
490011539SChunli.Zhang@Sun.COM 		 * Loan out an arc_buf for read if the read size is larger than
490111539SChunli.Zhang@Sun.COM 		 * the current file block size.  Block alignment is not
490211539SChunli.Zhang@Sun.COM 		 * considered.  Partial arc_buf will be loaned out for read.
490311539SChunli.Zhang@Sun.COM 		 */
490411539SChunli.Zhang@Sun.COM 		blksz = zp->z_blksz;
490511539SChunli.Zhang@Sun.COM 		if (blksz < zcr_blksz_min)
490611539SChunli.Zhang@Sun.COM 			blksz = zcr_blksz_min;
490711539SChunli.Zhang@Sun.COM 		if (blksz > zcr_blksz_max)
490811539SChunli.Zhang@Sun.COM 			blksz = zcr_blksz_max;
490911539SChunli.Zhang@Sun.COM 		/* avoid potential complexity of dealing with it */
491011539SChunli.Zhang@Sun.COM 		if (blksz > max_blksz) {
491111539SChunli.Zhang@Sun.COM 			ZFS_EXIT(zfsvfs);
491211539SChunli.Zhang@Sun.COM 			return (EINVAL);
491311539SChunli.Zhang@Sun.COM 		}
491411539SChunli.Zhang@Sun.COM 
491511935SMark.Shellenbaum@Sun.COM 		maxsize = zp->z_size - uio->uio_loffset;
491611539SChunli.Zhang@Sun.COM 		if (size > maxsize)
491711539SChunli.Zhang@Sun.COM 			size = maxsize;
491811539SChunli.Zhang@Sun.COM 
491911539SChunli.Zhang@Sun.COM 		if (size < blksz || vn_has_cached_data(vp)) {
492011539SChunli.Zhang@Sun.COM 			ZFS_EXIT(zfsvfs);
492111539SChunli.Zhang@Sun.COM 			return (EINVAL);
492211539SChunli.Zhang@Sun.COM 		}
492311539SChunli.Zhang@Sun.COM 		break;
492411539SChunli.Zhang@Sun.COM 	default:
492511539SChunli.Zhang@Sun.COM 		ZFS_EXIT(zfsvfs);
492611539SChunli.Zhang@Sun.COM 		return (EINVAL);
492711539SChunli.Zhang@Sun.COM 	}
492811539SChunli.Zhang@Sun.COM 
492911539SChunli.Zhang@Sun.COM 	uio->uio_extflg = UIO_XUIO;
493011539SChunli.Zhang@Sun.COM 	XUIO_XUZC_RW(xuio) = ioflag;
493111539SChunli.Zhang@Sun.COM 	ZFS_EXIT(zfsvfs);
493211539SChunli.Zhang@Sun.COM 	return (0);
493311539SChunli.Zhang@Sun.COM }
493411539SChunli.Zhang@Sun.COM 
493511539SChunli.Zhang@Sun.COM /*ARGSUSED*/
493611539SChunli.Zhang@Sun.COM static int
493711539SChunli.Zhang@Sun.COM zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
493811539SChunli.Zhang@Sun.COM {
493911539SChunli.Zhang@Sun.COM 	int i;
494011539SChunli.Zhang@Sun.COM 	arc_buf_t *abuf;
494111539SChunli.Zhang@Sun.COM 	int ioflag = XUIO_XUZC_RW(xuio);
494211539SChunli.Zhang@Sun.COM 
494311539SChunli.Zhang@Sun.COM 	ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
494411539SChunli.Zhang@Sun.COM 
494511539SChunli.Zhang@Sun.COM 	i = dmu_xuio_cnt(xuio);
494611539SChunli.Zhang@Sun.COM 	while (i-- > 0) {
494711539SChunli.Zhang@Sun.COM 		abuf = dmu_xuio_arcbuf(xuio, i);
494811539SChunli.Zhang@Sun.COM 		/*
494911539SChunli.Zhang@Sun.COM 		 * if abuf == NULL, it must be a write buffer
495011539SChunli.Zhang@Sun.COM 		 * that has been returned in zfs_write().
495111539SChunli.Zhang@Sun.COM 		 */
495211539SChunli.Zhang@Sun.COM 		if (abuf)
495311539SChunli.Zhang@Sun.COM 			dmu_return_arcbuf(abuf);
495411539SChunli.Zhang@Sun.COM 		ASSERT(abuf || ioflag == UIO_WRITE);
495511539SChunli.Zhang@Sun.COM 	}
495611539SChunli.Zhang@Sun.COM 
495711539SChunli.Zhang@Sun.COM 	dmu_xuio_fini(xuio);
495811539SChunli.Zhang@Sun.COM 	return (0);
495911539SChunli.Zhang@Sun.COM }
496011539SChunli.Zhang@Sun.COM 
496111539SChunli.Zhang@Sun.COM /*
4962789Sahrens  * Predeclare these here so that the compiler assumes that
4963789Sahrens  * this is an "old style" function declaration that does
4964789Sahrens  * not include arguments => we won't get type mismatch errors
4965789Sahrens  * in the initializations that follow.
4966789Sahrens  */
4967789Sahrens static int zfs_inval();
4968789Sahrens static int zfs_isdir();
4969789Sahrens 
4970789Sahrens static int
4971789Sahrens zfs_inval()
4972789Sahrens {
4973789Sahrens 	return (EINVAL);
4974789Sahrens }
4975789Sahrens 
4976789Sahrens static int
4977789Sahrens zfs_isdir()
4978789Sahrens {
4979789Sahrens 	return (EISDIR);
4980789Sahrens }
4981789Sahrens /*
4982789Sahrens  * Directory vnode operations template
4983789Sahrens  */
4984789Sahrens vnodeops_t *zfs_dvnodeops;
4985789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = {
49863898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
49873898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
49883898Srsb 	VOPNAME_READ,		{ .error = zfs_isdir },
49893898Srsb 	VOPNAME_WRITE,		{ .error = zfs_isdir },
49903898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
49913898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
49923898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
49933898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
49943898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
49953898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
49963898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
49973898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
49983898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
49993898Srsb 	VOPNAME_MKDIR,		{ .vop_mkdir = zfs_mkdir },
50003898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
50013898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
50023898Srsb 	VOPNAME_SYMLINK,	{ .vop_symlink = zfs_symlink },
50033898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
50043898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
50053898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
50063898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
50073898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50083898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
50093898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
50104863Spraks 	VOPNAME_VNEVENT, 	{ .vop_vnevent = fs_vnevent_support },
50113898Srsb 	NULL,			NULL
5012789Sahrens };
5013789Sahrens 
5014789Sahrens /*
5015789Sahrens  * Regular file vnode operations template
5016789Sahrens  */
5017789Sahrens vnodeops_t *zfs_fvnodeops;
5018789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = {
50193898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
50203898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
50213898Srsb 	VOPNAME_READ,		{ .vop_read = zfs_read },
50223898Srsb 	VOPNAME_WRITE,		{ .vop_write = zfs_write },
50233898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
50243898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
50253898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
50263898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
50273898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
50283898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
50293898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
50303898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
50313898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
50323898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
50333898Srsb 	VOPNAME_FRLOCK,		{ .vop_frlock = zfs_frlock },
50343898Srsb 	VOPNAME_SPACE,		{ .vop_space = zfs_space },
50353898Srsb 	VOPNAME_GETPAGE,	{ .vop_getpage = zfs_getpage },
50363898Srsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = zfs_putpage },
50373898Srsb 	VOPNAME_MAP,		{ .vop_map = zfs_map },
50383898Srsb 	VOPNAME_ADDMAP,		{ .vop_addmap = zfs_addmap },
50393898Srsb 	VOPNAME_DELMAP,		{ .vop_delmap = zfs_delmap },
50403898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50413898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
50423898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
50433898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
504411539SChunli.Zhang@Sun.COM 	VOPNAME_REQZCBUF, 	{ .vop_reqzcbuf = zfs_reqzcbuf },
504511539SChunli.Zhang@Sun.COM 	VOPNAME_RETZCBUF, 	{ .vop_retzcbuf = zfs_retzcbuf },
50463898Srsb 	NULL,			NULL
5047789Sahrens };
5048789Sahrens 
5049789Sahrens /*
5050789Sahrens  * Symbolic link vnode operations template
5051789Sahrens  */
5052789Sahrens vnodeops_t *zfs_symvnodeops;
5053789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = {
50543898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
50553898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
50563898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
50573898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
50583898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = zfs_readlink },
50593898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
50603898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
50613898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50623898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
50633898Srsb 	NULL,			NULL
5064789Sahrens };
5065789Sahrens 
5066789Sahrens /*
50678845Samw@Sun.COM  * special share hidden files vnode operations template
50688845Samw@Sun.COM  */
50698845Samw@Sun.COM vnodeops_t *zfs_sharevnodeops;
50708845Samw@Sun.COM const fs_operation_def_t zfs_sharevnodeops_template[] = {
50718845Samw@Sun.COM 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
50728845Samw@Sun.COM 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
50738845Samw@Sun.COM 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
50748845Samw@Sun.COM 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
50758845Samw@Sun.COM 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50768845Samw@Sun.COM 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
50778845Samw@Sun.COM 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
50788845Samw@Sun.COM 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
50798845Samw@Sun.COM 	NULL,			NULL
50808845Samw@Sun.COM };
50818845Samw@Sun.COM 
50828845Samw@Sun.COM /*
5083789Sahrens  * Extended attribute directory vnode operations template
5084789Sahrens  *	This template is identical to the directory vnodes
5085789Sahrens  *	operation template except for restricted operations:
5086789Sahrens  *		VOP_MKDIR()
5087789Sahrens  *		VOP_SYMLINK()
5088789Sahrens  * Note that there are other restrictions embedded in:
5089789Sahrens  *	zfs_create()	- restrict type to VREG
5090789Sahrens  *	zfs_link()	- no links into/out of attribute space
5091789Sahrens  *	zfs_rename()	- no moves into/out of attribute space
5092789Sahrens  */
5093789Sahrens vnodeops_t *zfs_xdvnodeops;
5094789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = {
50953898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
50963898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
50973898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
50983898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
50993898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
51003898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
51013898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
51023898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
51033898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
51043898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
51053898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
51063898Srsb 	VOPNAME_MKDIR,		{ .error = zfs_inval },
51073898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
51083898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
51093898Srsb 	VOPNAME_SYMLINK,	{ .error = zfs_inval },
51103898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
51113898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
51123898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
51133898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
51143898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
51153898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
51163898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
51173898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
51183898Srsb 	NULL,			NULL
5119789Sahrens };
5120789Sahrens 
5121789Sahrens /*
5122789Sahrens  * Error vnode operations template
5123789Sahrens  */
5124789Sahrens vnodeops_t *zfs_evnodeops;
5125789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = {
51263898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
51273898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
51283898Srsb 	NULL,			NULL
5129789Sahrens };
5130