xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 12285:d736d62dcca2)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51460Smarks  * Common Development and Distribution License (the "License").
61460Smarks  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
2212079SMark.Shellenbaum@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
254144Speteh /* Portions Copyright 2007 Jeremy Teo */
264144Speteh 
27789Sahrens #include <sys/types.h>
28789Sahrens #include <sys/param.h>
29789Sahrens #include <sys/time.h>
30789Sahrens #include <sys/systm.h>
31789Sahrens #include <sys/sysmacros.h>
32789Sahrens #include <sys/resource.h>
33789Sahrens #include <sys/vfs.h>
343898Srsb #include <sys/vfs_opreg.h>
35789Sahrens #include <sys/vnode.h>
36789Sahrens #include <sys/file.h>
37789Sahrens #include <sys/stat.h>
38789Sahrens #include <sys/kmem.h>
39789Sahrens #include <sys/taskq.h>
40789Sahrens #include <sys/uio.h>
41789Sahrens #include <sys/vmsystm.h>
42789Sahrens #include <sys/atomic.h>
432688Smaybee #include <sys/vm.h>
44789Sahrens #include <vm/seg_vn.h>
45789Sahrens #include <vm/pvn.h>
46789Sahrens #include <vm/as.h>
477315SJonathan.Adams@Sun.COM #include <vm/kpm.h>
487315SJonathan.Adams@Sun.COM #include <vm/seg_kpm.h>
49789Sahrens #include <sys/mman.h>
50789Sahrens #include <sys/pathname.h>
51789Sahrens #include <sys/cmn_err.h>
52789Sahrens #include <sys/errno.h>
53789Sahrens #include <sys/unistd.h>
54789Sahrens #include <sys/zfs_dir.h>
55789Sahrens #include <sys/zfs_acl.h>
56789Sahrens #include <sys/zfs_ioctl.h>
57789Sahrens #include <sys/fs/zfs.h>
58789Sahrens #include <sys/dmu.h>
59789Sahrens #include <sys/spa.h>
60789Sahrens #include <sys/txg.h>
61789Sahrens #include <sys/dbuf.h>
62789Sahrens #include <sys/zap.h>
6311935SMark.Shellenbaum@Sun.COM #include <sys/sa.h>
64789Sahrens #include <sys/dirent.h>
65789Sahrens #include <sys/policy.h>
66789Sahrens #include <sys/sunddi.h>
67789Sahrens #include <sys/filio.h>
687847SMark.Shellenbaum@Sun.COM #include <sys/sid.h>
69789Sahrens #include "fs/fs_subr.h"
70789Sahrens #include <sys/zfs_ctldir.h>
715331Samw #include <sys/zfs_fuid.h>
7211935SMark.Shellenbaum@Sun.COM #include <sys/zfs_sa.h>
731484Sek110237 #include <sys/dnlc.h>
741669Sperrin #include <sys/zfs_rlock.h>
755331Samw #include <sys/extdirent.h>
765331Samw #include <sys/kidmap.h>
7711134SCasper.Dik@Sun.COM #include <sys/cred.h>
785663Sck153898 #include <sys/attr.h>
79789Sahrens 
80789Sahrens /*
81789Sahrens  * Programming rules.
82789Sahrens  *
83789Sahrens  * Each vnode op performs some logical unit of work.  To do this, the ZPL must
84789Sahrens  * properly lock its in-core state, create a DMU transaction, do the work,
85789Sahrens  * record this work in the intent log (ZIL), commit the DMU transaction,
865331Samw  * and wait for the intent log to commit if it is a synchronous operation.
875331Samw  * Moreover, the vnode ops must work in both normal and log replay context.
88789Sahrens  * The ordering of events is important to avoid deadlocks and references
89789Sahrens  * to freed memory.  The example below illustrates the following Big Rules:
90789Sahrens  *
91789Sahrens  *  (1) A check must be made in each zfs thread for a mounted file system.
925367Sahrens  *	This is done avoiding races using ZFS_ENTER(zfsvfs).
935367Sahrens  *      A ZFS_EXIT(zfsvfs) is needed before all returns.  Any znodes
945367Sahrens  *      must be checked with ZFS_VERIFY_ZP(zp).  Both of these macros
955367Sahrens  *      can return EIO from the calling function.
96789Sahrens  *
97789Sahrens  *  (2)	VN_RELE() should always be the last thing except for zil_commit()
982638Sperrin  *	(if necessary) and ZFS_EXIT(). This is for 3 reasons:
99789Sahrens  *	First, if it's the last reference, the vnode/znode
100789Sahrens  *	can be freed, so the zp may point to freed memory.  Second, the last
101789Sahrens  *	reference will call zfs_zinactive(), which may induce a lot of work --
1021669Sperrin  *	pushing cached pages (which acquires range locks) and syncing out
103789Sahrens  *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
104789Sahrens  *	which could deadlock the system if you were already holding one.
1059321SNeil.Perrin@Sun.COM  *	If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
106789Sahrens  *
1071757Sperrin  *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
1081757Sperrin  *	as they can span dmu_tx_assign() calls.
1091757Sperrin  *
1108227SNeil.Perrin@Sun.COM  *  (4)	Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
111789Sahrens  *	This is critical because we don't want to block while holding locks.
112789Sahrens  *	Note, in particular, that if a lock is sometimes acquired before
113789Sahrens  *	the tx assigns, and sometimes after (e.g. z_lock), then failing to
114789Sahrens  *	use a non-blocking assign can deadlock the system.  The scenario:
115789Sahrens  *
116789Sahrens  *	Thread A has grabbed a lock before calling dmu_tx_assign().
117789Sahrens  *	Thread B is in an already-assigned tx, and blocks for this lock.
118789Sahrens  *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
119789Sahrens  *	forever, because the previous txg can't quiesce until B's tx commits.
120789Sahrens  *
121789Sahrens  *	If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
1222113Sahrens  *	then drop all locks, call dmu_tx_wait(), and try again.
123789Sahrens  *
1241757Sperrin  *  (5)	If the operation succeeded, generate the intent log entry for it
125789Sahrens  *	before dropping locks.  This ensures that the ordering of events
126789Sahrens  *	in the intent log matches the order in which they actually occurred.
1278227SNeil.Perrin@Sun.COM  *      During ZIL replay the zfs_log_* functions will update the sequence
1288227SNeil.Perrin@Sun.COM  *	number to indicate the zil transaction has replayed.
129789Sahrens  *
1301757Sperrin  *  (6)	At the end of each vnode op, the DMU tx must always commit,
131789Sahrens  *	regardless of whether there were any errors.
132789Sahrens  *
1332638Sperrin  *  (7)	After dropping all locks, invoke zil_commit(zilog, seq, foid)
134789Sahrens  *	to ensure that synchronous semantics are provided when necessary.
135789Sahrens  *
136789Sahrens  * In general, this is how things should be ordered in each vnode op:
137789Sahrens  *
138789Sahrens  *	ZFS_ENTER(zfsvfs);		// exit if unmounted
139789Sahrens  * top:
140789Sahrens  *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
141789Sahrens  *	rw_enter(...);			// grab any other locks you need
142789Sahrens  *	tx = dmu_tx_create(...);	// get DMU tx
143789Sahrens  *	dmu_tx_hold_*();		// hold each object you might modify
1448227SNeil.Perrin@Sun.COM  *	error = dmu_tx_assign(tx, TXG_NOWAIT);	// try to assign
145789Sahrens  *	if (error) {
146789Sahrens  *		rw_exit(...);		// drop locks
147789Sahrens  *		zfs_dirent_unlock(dl);	// unlock directory entry
148789Sahrens  *		VN_RELE(...);		// release held vnodes
1498227SNeil.Perrin@Sun.COM  *		if (error == ERESTART) {
1502113Sahrens  *			dmu_tx_wait(tx);
1512113Sahrens  *			dmu_tx_abort(tx);
152789Sahrens  *			goto top;
153789Sahrens  *		}
1542113Sahrens  *		dmu_tx_abort(tx);	// abort DMU tx
155789Sahrens  *		ZFS_EXIT(zfsvfs);	// finished in zfs
156789Sahrens  *		return (error);		// really out of space
157789Sahrens  *	}
158789Sahrens  *	error = do_real_work();		// do whatever this VOP does
159789Sahrens  *	if (error == 0)
1602638Sperrin  *		zfs_log_*(...);		// on success, make ZIL entry
161789Sahrens  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
162789Sahrens  *	rw_exit(...);			// drop locks
163789Sahrens  *	zfs_dirent_unlock(dl);		// unlock directory entry
164789Sahrens  *	VN_RELE(...);			// release held vnodes
1652638Sperrin  *	zil_commit(zilog, seq, foid);	// synchronous when necessary
166789Sahrens  *	ZFS_EXIT(zfsvfs);		// finished in zfs
167789Sahrens  *	return (error);			// done, report error
168789Sahrens  */
1695367Sahrens 
170789Sahrens /* ARGSUSED */
171789Sahrens static int
1725331Samw zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
173789Sahrens {
1743063Sperrin 	znode_t	*zp = VTOZ(*vpp);
1757844SMark.Shellenbaum@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1767844SMark.Shellenbaum@Sun.COM 
1777844SMark.Shellenbaum@Sun.COM 	ZFS_ENTER(zfsvfs);
1787844SMark.Shellenbaum@Sun.COM 	ZFS_VERIFY_ZP(zp);
1793063Sperrin 
18011935SMark.Shellenbaum@Sun.COM 	if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
1815331Samw 	    ((flag & FAPPEND) == 0)) {
1827844SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
1835331Samw 		return (EPERM);
1845331Samw 	}
1855331Samw 
1865331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
1875331Samw 	    ZTOV(zp)->v_type == VREG &&
18811935SMark.Shellenbaum@Sun.COM 	    !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
1897844SMark.Shellenbaum@Sun.COM 		if (fs_vscan(*vpp, cr, 0) != 0) {
1907844SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
1915331Samw 			return (EACCES);
1927844SMark.Shellenbaum@Sun.COM 		}
1937844SMark.Shellenbaum@Sun.COM 	}
1945331Samw 
1953063Sperrin 	/* Keep a count of the synchronous opens in the znode */
1963063Sperrin 	if (flag & (FSYNC | FDSYNC))
1973063Sperrin 		atomic_inc_32(&zp->z_sync_cnt);
1985331Samw 
1997844SMark.Shellenbaum@Sun.COM 	ZFS_EXIT(zfsvfs);
200789Sahrens 	return (0);
201789Sahrens }
202789Sahrens 
203789Sahrens /* ARGSUSED */
204789Sahrens static int
2055331Samw zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
2065331Samw     caller_context_t *ct)
207789Sahrens {
2083063Sperrin 	znode_t	*zp = VTOZ(vp);
2097844SMark.Shellenbaum@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2107844SMark.Shellenbaum@Sun.COM 
2119909Schris.kirby@sun.com 	/*
2129909Schris.kirby@sun.com 	 * Clean up any locks held by this process on the vp.
2139909Schris.kirby@sun.com 	 */
2149909Schris.kirby@sun.com 	cleanlocks(vp, ddi_get_pid(), 0);
2159909Schris.kirby@sun.com 	cleanshares(vp, ddi_get_pid());
2169909Schris.kirby@sun.com 
2177844SMark.Shellenbaum@Sun.COM 	ZFS_ENTER(zfsvfs);
2187844SMark.Shellenbaum@Sun.COM 	ZFS_VERIFY_ZP(zp);
2193063Sperrin 
2203063Sperrin 	/* Decrement the synchronous opens in the znode */
2214339Sperrin 	if ((flag & (FSYNC | FDSYNC)) && (count == 1))
2223063Sperrin 		atomic_dec_32(&zp->z_sync_cnt);
2233063Sperrin 
2245331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
2255331Samw 	    ZTOV(zp)->v_type == VREG &&
22611935SMark.Shellenbaum@Sun.COM 	    !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
2275331Samw 		VERIFY(fs_vscan(vp, cr, 1) == 0);
2285331Samw 
2297844SMark.Shellenbaum@Sun.COM 	ZFS_EXIT(zfsvfs);
230789Sahrens 	return (0);
231789Sahrens }
232789Sahrens 
233789Sahrens /*
234789Sahrens  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
235789Sahrens  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
236789Sahrens  */
237789Sahrens static int
238789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off)
239789Sahrens {
240789Sahrens 	znode_t	*zp = VTOZ(vp);
241789Sahrens 	uint64_t noff = (uint64_t)*off; /* new offset */
242789Sahrens 	uint64_t file_sz;
243789Sahrens 	int error;
244789Sahrens 	boolean_t hole;
245789Sahrens 
24611935SMark.Shellenbaum@Sun.COM 	file_sz = zp->z_size;
247789Sahrens 	if (noff >= file_sz)  {
248789Sahrens 		return (ENXIO);
249789Sahrens 	}
250789Sahrens 
251789Sahrens 	if (cmd == _FIO_SEEK_HOLE)
252789Sahrens 		hole = B_TRUE;
253789Sahrens 	else
254789Sahrens 		hole = B_FALSE;
255789Sahrens 
256789Sahrens 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
257789Sahrens 
258789Sahrens 	/* end of file? */
259789Sahrens 	if ((error == ESRCH) || (noff > file_sz)) {
260789Sahrens 		/*
261789Sahrens 		 * Handle the virtual hole at the end of file.
262789Sahrens 		 */
263789Sahrens 		if (hole) {
264789Sahrens 			*off = file_sz;
265789Sahrens 			return (0);
266789Sahrens 		}
267789Sahrens 		return (ENXIO);
268789Sahrens 	}
269789Sahrens 
270789Sahrens 	if (noff < *off)
271789Sahrens 		return (error);
272789Sahrens 	*off = noff;
273789Sahrens 	return (error);
274789Sahrens }
275789Sahrens 
276789Sahrens /* ARGSUSED */
277789Sahrens static int
278789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
2795331Samw     int *rvalp, caller_context_t *ct)
280789Sahrens {
281789Sahrens 	offset_t off;
282789Sahrens 	int error;
283789Sahrens 	zfsvfs_t *zfsvfs;
2845326Sek110237 	znode_t *zp;
285789Sahrens 
286789Sahrens 	switch (com) {
2874339Sperrin 	case _FIOFFS:
288789Sahrens 		return (zfs_sync(vp->v_vfsp, 0, cred));
289789Sahrens 
2901544Seschrock 		/*
2911544Seschrock 		 * The following two ioctls are used by bfu.  Faking out,
2921544Seschrock 		 * necessary to avoid bfu errors.
2931544Seschrock 		 */
2944339Sperrin 	case _FIOGDIO:
2954339Sperrin 	case _FIOSDIO:
2961544Seschrock 		return (0);
2971544Seschrock 
2984339Sperrin 	case _FIO_SEEK_DATA:
2994339Sperrin 	case _FIO_SEEK_HOLE:
300789Sahrens 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
301789Sahrens 			return (EFAULT);
302789Sahrens 
3035326Sek110237 		zp = VTOZ(vp);
3045326Sek110237 		zfsvfs = zp->z_zfsvfs;
3055367Sahrens 		ZFS_ENTER(zfsvfs);
3065367Sahrens 		ZFS_VERIFY_ZP(zp);
307789Sahrens 
308789Sahrens 		/* offset parameter is in/out */
309789Sahrens 		error = zfs_holey(vp, com, &off);
310789Sahrens 		ZFS_EXIT(zfsvfs);
311789Sahrens 		if (error)
312789Sahrens 			return (error);
313789Sahrens 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
314789Sahrens 			return (EFAULT);
315789Sahrens 		return (0);
316789Sahrens 	}
317789Sahrens 	return (ENOTTY);
318789Sahrens }
319789Sahrens 
320789Sahrens /*
3217315SJonathan.Adams@Sun.COM  * Utility functions to map and unmap a single physical page.  These
3227315SJonathan.Adams@Sun.COM  * are used to manage the mappable copies of ZFS file data, and therefore
3237315SJonathan.Adams@Sun.COM  * do not update ref/mod bits.
3247315SJonathan.Adams@Sun.COM  */
3257315SJonathan.Adams@Sun.COM caddr_t
3267315SJonathan.Adams@Sun.COM zfs_map_page(page_t *pp, enum seg_rw rw)
3277315SJonathan.Adams@Sun.COM {
3287315SJonathan.Adams@Sun.COM 	if (kpm_enable)
3297315SJonathan.Adams@Sun.COM 		return (hat_kpm_mapin(pp, 0));
3307315SJonathan.Adams@Sun.COM 	ASSERT(rw == S_READ || rw == S_WRITE);
3317315SJonathan.Adams@Sun.COM 	return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0),
3327315SJonathan.Adams@Sun.COM 	    (caddr_t)-1));
3337315SJonathan.Adams@Sun.COM }
3347315SJonathan.Adams@Sun.COM 
3357315SJonathan.Adams@Sun.COM void
3367315SJonathan.Adams@Sun.COM zfs_unmap_page(page_t *pp, caddr_t addr)
3377315SJonathan.Adams@Sun.COM {
3387315SJonathan.Adams@Sun.COM 	if (kpm_enable) {
3397315SJonathan.Adams@Sun.COM 		hat_kpm_mapout(pp, 0, addr);
3407315SJonathan.Adams@Sun.COM 	} else {
3417315SJonathan.Adams@Sun.COM 		ppmapout(addr);
3427315SJonathan.Adams@Sun.COM 	}
3437315SJonathan.Adams@Sun.COM }
3447315SJonathan.Adams@Sun.COM 
3457315SJonathan.Adams@Sun.COM /*
346789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
347789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
348789Sahrens  *
349789Sahrens  * On Write:	If we find a memory mapped page, we write to *both*
350789Sahrens  *		the page and the dmu buffer.
351789Sahrens  */
3528636SMark.Maybee@Sun.COM static void
3538636SMark.Maybee@Sun.COM update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid)
354789Sahrens {
3558636SMark.Maybee@Sun.COM 	int64_t	off;
3568636SMark.Maybee@Sun.COM 
357789Sahrens 	off = start & PAGEOFFSET;
358789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
359789Sahrens 		page_t *pp;
3608636SMark.Maybee@Sun.COM 		uint64_t nbytes = MIN(PAGESIZE - off, len);
3618636SMark.Maybee@Sun.COM 
362789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
363789Sahrens 			caddr_t va;
364789Sahrens 
3657315SJonathan.Adams@Sun.COM 			va = zfs_map_page(pp, S_WRITE);
3669512SNeil.Perrin@Sun.COM 			(void) dmu_read(os, oid, start+off, nbytes, va+off,
3679512SNeil.Perrin@Sun.COM 			    DMU_READ_PREFETCH);
3687315SJonathan.Adams@Sun.COM 			zfs_unmap_page(pp, va);
369789Sahrens 			page_unlock(pp);
370789Sahrens 		}
3718636SMark.Maybee@Sun.COM 		len -= nbytes;
372789Sahrens 		off = 0;
373789Sahrens 	}
374789Sahrens }
375789Sahrens 
376789Sahrens /*
377789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
378789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
379789Sahrens  *
380789Sahrens  * On Read:	We "read" preferentially from memory mapped pages,
381789Sahrens  *		else we default from the dmu buffer.
382789Sahrens  *
383789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
384789Sahrens  *	the file is memory mapped.
385789Sahrens  */
386789Sahrens static int
3873638Sbillm mappedread(vnode_t *vp, int nbytes, uio_t *uio)
388789Sahrens {
3893638Sbillm 	znode_t *zp = VTOZ(vp);
3903638Sbillm 	objset_t *os = zp->z_zfsvfs->z_os;
3913638Sbillm 	int64_t	start, off;
392789Sahrens 	int len = nbytes;
393789Sahrens 	int error = 0;
394789Sahrens 
395789Sahrens 	start = uio->uio_loffset;
396789Sahrens 	off = start & PAGEOFFSET;
397789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
398789Sahrens 		page_t *pp;
3993638Sbillm 		uint64_t bytes = MIN(PAGESIZE - off, len);
4003638Sbillm 
401789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
402789Sahrens 			caddr_t va;
403789Sahrens 
4047315SJonathan.Adams@Sun.COM 			va = zfs_map_page(pp, S_READ);
405789Sahrens 			error = uiomove(va + off, bytes, UIO_READ, uio);
4067315SJonathan.Adams@Sun.COM 			zfs_unmap_page(pp, va);
407789Sahrens 			page_unlock(pp);
408789Sahrens 		} else {
4093638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, bytes);
410789Sahrens 		}
411789Sahrens 		len -= bytes;
412789Sahrens 		off = 0;
413789Sahrens 		if (error)
414789Sahrens 			break;
415789Sahrens 	}
416789Sahrens 	return (error);
417789Sahrens }
418789Sahrens 
4193638Sbillm offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
420789Sahrens 
421789Sahrens /*
422789Sahrens  * Read bytes from specified file into supplied buffer.
423789Sahrens  *
424789Sahrens  *	IN:	vp	- vnode of file to be read from.
425789Sahrens  *		uio	- structure supplying read location, range info,
426789Sahrens  *			  and return buffer.
427789Sahrens  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
428789Sahrens  *		cr	- credentials of caller.
4295331Samw  *		ct	- caller context
430789Sahrens  *
431789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
432789Sahrens  *
433789Sahrens  *	RETURN:	0 if success
434789Sahrens  *		error code if failure
435789Sahrens  *
436789Sahrens  * Side Effects:
437789Sahrens  *	vp - atime updated if byte count > 0
438789Sahrens  */
439789Sahrens /* ARGSUSED */
440789Sahrens static int
441789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
442789Sahrens {
443789Sahrens 	znode_t		*zp = VTOZ(vp);
444789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4455326Sek110237 	objset_t	*os;
4463638Sbillm 	ssize_t		n, nbytes;
4473638Sbillm 	int		error;
4481669Sperrin 	rl_t		*rl;
44911539SChunli.Zhang@Sun.COM 	xuio_t		*xuio = NULL;
450789Sahrens 
4515367Sahrens 	ZFS_ENTER(zfsvfs);
4525367Sahrens 	ZFS_VERIFY_ZP(zp);
4535326Sek110237 	os = zfsvfs->z_os;
454789Sahrens 
45511935SMark.Shellenbaum@Sun.COM 	if (zp->z_pflags & ZFS_AV_QUARANTINED) {
4565929Smarks 		ZFS_EXIT(zfsvfs);
4575929Smarks 		return (EACCES);
4585929Smarks 	}
4595929Smarks 
460789Sahrens 	/*
461789Sahrens 	 * Validate file offset
462789Sahrens 	 */
463789Sahrens 	if (uio->uio_loffset < (offset_t)0) {
464789Sahrens 		ZFS_EXIT(zfsvfs);
465789Sahrens 		return (EINVAL);
466789Sahrens 	}
467789Sahrens 
468789Sahrens 	/*
469789Sahrens 	 * Fasttrack empty reads
470789Sahrens 	 */
471789Sahrens 	if (uio->uio_resid == 0) {
472789Sahrens 		ZFS_EXIT(zfsvfs);
473789Sahrens 		return (0);
474789Sahrens 	}
475789Sahrens 
476789Sahrens 	/*
4771669Sperrin 	 * Check for mandatory locks
478789Sahrens 	 */
47911935SMark.Shellenbaum@Sun.COM 	if (MANDMODE(zp->z_mode)) {
480789Sahrens 		if (error = chklock(vp, FREAD,
481789Sahrens 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
482789Sahrens 			ZFS_EXIT(zfsvfs);
483789Sahrens 			return (error);
484789Sahrens 		}
485789Sahrens 	}
486789Sahrens 
487789Sahrens 	/*
488789Sahrens 	 * If we're in FRSYNC mode, sync out this znode before reading it.
489789Sahrens 	 */
4902638Sperrin 	if (ioflag & FRSYNC)
4912638Sperrin 		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
492789Sahrens 
493789Sahrens 	/*
4941669Sperrin 	 * Lock the range against changes.
495789Sahrens 	 */
4961669Sperrin 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
4971669Sperrin 
498789Sahrens 	/*
499789Sahrens 	 * If we are reading past end-of-file we can skip
500789Sahrens 	 * to the end; but we might still need to set atime.
501789Sahrens 	 */
50211935SMark.Shellenbaum@Sun.COM 	if (uio->uio_loffset >= zp->z_size) {
503789Sahrens 		error = 0;
504789Sahrens 		goto out;
505789Sahrens 	}
506789Sahrens 
50711935SMark.Shellenbaum@Sun.COM 	ASSERT(uio->uio_loffset < zp->z_size);
50811935SMark.Shellenbaum@Sun.COM 	n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
5093638Sbillm 
51011539SChunli.Zhang@Sun.COM 	if ((uio->uio_extflg == UIO_XUIO) &&
51111539SChunli.Zhang@Sun.COM 	    (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
51211539SChunli.Zhang@Sun.COM 		int nblk;
51311539SChunli.Zhang@Sun.COM 		int blksz = zp->z_blksz;
51411539SChunli.Zhang@Sun.COM 		uint64_t offset = uio->uio_loffset;
51511539SChunli.Zhang@Sun.COM 
51611539SChunli.Zhang@Sun.COM 		xuio = (xuio_t *)uio;
51711539SChunli.Zhang@Sun.COM 		if ((ISP2(blksz))) {
51811539SChunli.Zhang@Sun.COM 			nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
51911539SChunli.Zhang@Sun.COM 			    blksz)) / blksz;
52011539SChunli.Zhang@Sun.COM 		} else {
52111539SChunli.Zhang@Sun.COM 			ASSERT(offset + n <= blksz);
52211539SChunli.Zhang@Sun.COM 			nblk = 1;
52311539SChunli.Zhang@Sun.COM 		}
52411576SSurya.Prakki@Sun.COM 		(void) dmu_xuio_init(xuio, nblk);
52511539SChunli.Zhang@Sun.COM 
52611539SChunli.Zhang@Sun.COM 		if (vn_has_cached_data(vp)) {
52711539SChunli.Zhang@Sun.COM 			/*
52811539SChunli.Zhang@Sun.COM 			 * For simplicity, we always allocate a full buffer
52911539SChunli.Zhang@Sun.COM 			 * even if we only expect to read a portion of a block.
53011539SChunli.Zhang@Sun.COM 			 */
53111539SChunli.Zhang@Sun.COM 			while (--nblk >= 0) {
53211576SSurya.Prakki@Sun.COM 				(void) dmu_xuio_add(xuio,
53311935SMark.Shellenbaum@Sun.COM 				    dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
53411935SMark.Shellenbaum@Sun.COM 				    blksz), 0, blksz);
53511539SChunli.Zhang@Sun.COM 			}
53611539SChunli.Zhang@Sun.COM 		}
53711539SChunli.Zhang@Sun.COM 	}
53811539SChunli.Zhang@Sun.COM 
5393638Sbillm 	while (n > 0) {
5403638Sbillm 		nbytes = MIN(n, zfs_read_chunk_size -
5413638Sbillm 		    P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
5423638Sbillm 
5433638Sbillm 		if (vn_has_cached_data(vp))
5443638Sbillm 			error = mappedread(vp, nbytes, uio);
5453638Sbillm 		else
5463638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, nbytes);
5477294Sperrin 		if (error) {
5487294Sperrin 			/* convert checksum errors into IO errors */
5497294Sperrin 			if (error == ECKSUM)
5507294Sperrin 				error = EIO;
5513638Sbillm 			break;
5527294Sperrin 		}
5533638Sbillm 
5543638Sbillm 		n -= nbytes;
555789Sahrens 	}
556789Sahrens out:
5572237Smaybee 	zfs_range_unlock(rl);
558789Sahrens 
559789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
560789Sahrens 	ZFS_EXIT(zfsvfs);
561789Sahrens 	return (error);
562789Sahrens }
563789Sahrens 
564789Sahrens /*
565789Sahrens  * Write the bytes to a file.
566789Sahrens  *
567789Sahrens  *	IN:	vp	- vnode of file to be written to.
568789Sahrens  *		uio	- structure supplying write location, range info,
569789Sahrens  *			  and data buffer.
570789Sahrens  *		ioflag	- FAPPEND flag set if in append mode.
571789Sahrens  *		cr	- credentials of caller.
5725331Samw  *		ct	- caller context (NFS/CIFS fem monitor only)
573789Sahrens  *
574789Sahrens  *	OUT:	uio	- updated offset and range.
575789Sahrens  *
576789Sahrens  *	RETURN:	0 if success
577789Sahrens  *		error code if failure
578789Sahrens  *
579789Sahrens  * Timestamps:
580789Sahrens  *	vp - ctime|mtime updated if byte count > 0
581789Sahrens  */
58211935SMark.Shellenbaum@Sun.COM 
583789Sahrens /* ARGSUSED */
584789Sahrens static int
585789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
586789Sahrens {
587789Sahrens 	znode_t		*zp = VTOZ(vp);
588789Sahrens 	rlim64_t	limit = uio->uio_llimit;
589789Sahrens 	ssize_t		start_resid = uio->uio_resid;
590789Sahrens 	ssize_t		tx_bytes;
591789Sahrens 	uint64_t	end_size;
592789Sahrens 	dmu_tx_t	*tx;
593789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
5945326Sek110237 	zilog_t		*zilog;
595789Sahrens 	offset_t	woff;
596789Sahrens 	ssize_t		n, nbytes;
5971669Sperrin 	rl_t		*rl;
598789Sahrens 	int		max_blksz = zfsvfs->z_max_blksz;
5991669Sperrin 	int		error;
6009412SAleksandr.Guzovskiy@Sun.COM 	arc_buf_t	*abuf;
60111539SChunli.Zhang@Sun.COM 	iovec_t		*aiov;
60211539SChunli.Zhang@Sun.COM 	xuio_t		*xuio = NULL;
60311539SChunli.Zhang@Sun.COM 	int		i_iov = 0;
60411539SChunli.Zhang@Sun.COM 	int		iovcnt = uio->uio_iovcnt;
60511539SChunli.Zhang@Sun.COM 	iovec_t		*iovp = uio->uio_iov;
60611539SChunli.Zhang@Sun.COM 	int		write_eof;
60711935SMark.Shellenbaum@Sun.COM 	int		count = 0;
60811935SMark.Shellenbaum@Sun.COM 	sa_bulk_attr_t	bulk[4];
60911935SMark.Shellenbaum@Sun.COM 	uint64_t	mtime[2], ctime[2];
610789Sahrens 
611789Sahrens 	/*
612789Sahrens 	 * Fasttrack empty write
613789Sahrens 	 */
6141669Sperrin 	n = start_resid;
615789Sahrens 	if (n == 0)
616789Sahrens 		return (0);
617789Sahrens 
6181669Sperrin 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
6191669Sperrin 		limit = MAXOFFSET_T;
6201669Sperrin 
6215367Sahrens 	ZFS_ENTER(zfsvfs);
6225367Sahrens 	ZFS_VERIFY_ZP(zp);
6236743Smarks 
62411935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
62511935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
62611935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
62711935SMark.Shellenbaum@Sun.COM 	    &zp->z_size, 8);
62811935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
62911935SMark.Shellenbaum@Sun.COM 	    &zp->z_pflags, 8);
63011935SMark.Shellenbaum@Sun.COM 
6316743Smarks 	/*
6326743Smarks 	 * If immutable or not appending then return EPERM
6336743Smarks 	 */
63411935SMark.Shellenbaum@Sun.COM 	if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
63511935SMark.Shellenbaum@Sun.COM 	    ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
63611935SMark.Shellenbaum@Sun.COM 	    (uio->uio_loffset < zp->z_size))) {
6376743Smarks 		ZFS_EXIT(zfsvfs);
6386743Smarks 		return (EPERM);
6396743Smarks 	}
6406743Smarks 
6415326Sek110237 	zilog = zfsvfs->z_log;
642789Sahrens 
643789Sahrens 	/*
64411083Swilliam.gorrell@sun.com 	 * Validate file offset
64511083Swilliam.gorrell@sun.com 	 */
64611935SMark.Shellenbaum@Sun.COM 	woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
64711083Swilliam.gorrell@sun.com 	if (woff < 0) {
64811083Swilliam.gorrell@sun.com 		ZFS_EXIT(zfsvfs);
64911083Swilliam.gorrell@sun.com 		return (EINVAL);
65011083Swilliam.gorrell@sun.com 	}
65111083Swilliam.gorrell@sun.com 
65211083Swilliam.gorrell@sun.com 	/*
65311083Swilliam.gorrell@sun.com 	 * Check for mandatory locks before calling zfs_range_lock()
65411083Swilliam.gorrell@sun.com 	 * in order to prevent a deadlock with locks set via fcntl().
65511083Swilliam.gorrell@sun.com 	 */
65611935SMark.Shellenbaum@Sun.COM 	if (MANDMODE((mode_t)zp->z_mode) &&
65711083Swilliam.gorrell@sun.com 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
65811083Swilliam.gorrell@sun.com 		ZFS_EXIT(zfsvfs);
65911083Swilliam.gorrell@sun.com 		return (error);
66011083Swilliam.gorrell@sun.com 	}
66111083Swilliam.gorrell@sun.com 
66211083Swilliam.gorrell@sun.com 	/*
6632237Smaybee 	 * Pre-fault the pages to ensure slow (eg NFS) pages
6641669Sperrin 	 * don't hold up txg.
66511539SChunli.Zhang@Sun.COM 	 * Skip this if uio contains loaned arc_buf.
666789Sahrens 	 */
66711539SChunli.Zhang@Sun.COM 	if ((uio->uio_extflg == UIO_XUIO) &&
66811539SChunli.Zhang@Sun.COM 	    (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
66911539SChunli.Zhang@Sun.COM 		xuio = (xuio_t *)uio;
67011539SChunli.Zhang@Sun.COM 	else
67111539SChunli.Zhang@Sun.COM 		uio_prefaultpages(n, uio);
672789Sahrens 
673789Sahrens 	/*
674789Sahrens 	 * If in append mode, set the io offset pointer to eof.
675789Sahrens 	 */
6761669Sperrin 	if (ioflag & FAPPEND) {
6771669Sperrin 		/*
67811083Swilliam.gorrell@sun.com 		 * Obtain an appending range lock to guarantee file append
67911083Swilliam.gorrell@sun.com 		 * semantics.  We reset the write offset once we have the lock.
6801669Sperrin 		 */
6811669Sperrin 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
68211083Swilliam.gorrell@sun.com 		woff = rl->r_off;
6831669Sperrin 		if (rl->r_len == UINT64_MAX) {
68411083Swilliam.gorrell@sun.com 			/*
68511083Swilliam.gorrell@sun.com 			 * We overlocked the file because this write will cause
68611083Swilliam.gorrell@sun.com 			 * the file block size to increase.
68711083Swilliam.gorrell@sun.com 			 * Note that zp_size cannot change with this lock held.
68811083Swilliam.gorrell@sun.com 			 */
68911935SMark.Shellenbaum@Sun.COM 			woff = zp->z_size;
6901669Sperrin 		}
69111083Swilliam.gorrell@sun.com 		uio->uio_loffset = woff;
692789Sahrens 	} else {
693789Sahrens 		/*
69411083Swilliam.gorrell@sun.com 		 * Note that if the file block size will change as a result of
69511083Swilliam.gorrell@sun.com 		 * this write, then this range lock will lock the entire file
69611083Swilliam.gorrell@sun.com 		 * so that we can re-write the block safely.
697789Sahrens 		 */
6981669Sperrin 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
699789Sahrens 	}
700789Sahrens 
701789Sahrens 	if (woff >= limit) {
7023638Sbillm 		zfs_range_unlock(rl);
7033638Sbillm 		ZFS_EXIT(zfsvfs);
7043638Sbillm 		return (EFBIG);
705789Sahrens 	}
706789Sahrens 
707789Sahrens 	if ((woff + n) > limit || woff > (limit - n))
708789Sahrens 		n = limit - woff;
709789Sahrens 
71011539SChunli.Zhang@Sun.COM 	/* Will this write extend the file length? */
71111935SMark.Shellenbaum@Sun.COM 	write_eof = (woff + n > zp->z_size);
71211935SMark.Shellenbaum@Sun.COM 
71311935SMark.Shellenbaum@Sun.COM 	end_size = MAX(zp->z_size, woff + n);
714789Sahrens 
7151669Sperrin 	/*
7163638Sbillm 	 * Write the file in reasonable size chunks.  Each chunk is written
7173638Sbillm 	 * in a separate transaction; this keeps the intent log records small
7183638Sbillm 	 * and allows us to do more fine-grained space accounting.
719789Sahrens 	 */
720789Sahrens 	while (n > 0) {
7219412SAleksandr.Guzovskiy@Sun.COM 		abuf = NULL;
7229412SAleksandr.Guzovskiy@Sun.COM 		woff = uio->uio_loffset;
7239412SAleksandr.Guzovskiy@Sun.COM again:
72411935SMark.Shellenbaum@Sun.COM 		if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
72511935SMark.Shellenbaum@Sun.COM 		    zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
7269412SAleksandr.Guzovskiy@Sun.COM 			if (abuf != NULL)
7279412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7289396SMatthew.Ahrens@Sun.COM 			error = EDQUOT;
7299396SMatthew.Ahrens@Sun.COM 			break;
7309396SMatthew.Ahrens@Sun.COM 		}
7319412SAleksandr.Guzovskiy@Sun.COM 
73211539SChunli.Zhang@Sun.COM 		if (xuio && abuf == NULL) {
73311539SChunli.Zhang@Sun.COM 			ASSERT(i_iov < iovcnt);
73411539SChunli.Zhang@Sun.COM 			aiov = &iovp[i_iov];
73511539SChunli.Zhang@Sun.COM 			abuf = dmu_xuio_arcbuf(xuio, i_iov);
73611539SChunli.Zhang@Sun.COM 			dmu_xuio_clear(xuio, i_iov);
73711539SChunli.Zhang@Sun.COM 			DTRACE_PROBE3(zfs_cp_write, int, i_iov,
73811539SChunli.Zhang@Sun.COM 			    iovec_t *, aiov, arc_buf_t *, abuf);
73911539SChunli.Zhang@Sun.COM 			ASSERT((aiov->iov_base == abuf->b_data) ||
74011539SChunli.Zhang@Sun.COM 			    ((char *)aiov->iov_base - (char *)abuf->b_data +
74111539SChunli.Zhang@Sun.COM 			    aiov->iov_len == arc_buf_size(abuf)));
74211539SChunli.Zhang@Sun.COM 			i_iov++;
74311539SChunli.Zhang@Sun.COM 		} else if (abuf == NULL && n >= max_blksz &&
74411935SMark.Shellenbaum@Sun.COM 		    woff >= zp->z_size &&
7459412SAleksandr.Guzovskiy@Sun.COM 		    P2PHASE(woff, max_blksz) == 0 &&
7469412SAleksandr.Guzovskiy@Sun.COM 		    zp->z_blksz == max_blksz) {
74711539SChunli.Zhang@Sun.COM 			/*
74811539SChunli.Zhang@Sun.COM 			 * This write covers a full block.  "Borrow" a buffer
74911539SChunli.Zhang@Sun.COM 			 * from the dmu so that we can fill it before we enter
75011539SChunli.Zhang@Sun.COM 			 * a transaction.  This avoids the possibility of
75111539SChunli.Zhang@Sun.COM 			 * holding up the transaction if the data copy hangs
75211539SChunli.Zhang@Sun.COM 			 * up on a pagefault (e.g., from an NFS server mapping).
75311539SChunli.Zhang@Sun.COM 			 */
7549412SAleksandr.Guzovskiy@Sun.COM 			size_t cbytes;
7559412SAleksandr.Guzovskiy@Sun.COM 
75611935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
75711935SMark.Shellenbaum@Sun.COM 			    max_blksz);
7589412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(abuf != NULL);
7599412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(arc_buf_size(abuf) == max_blksz);
7609412SAleksandr.Guzovskiy@Sun.COM 			if (error = uiocopy(abuf->b_data, max_blksz,
7619412SAleksandr.Guzovskiy@Sun.COM 			    UIO_WRITE, uio, &cbytes)) {
7629412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7639412SAleksandr.Guzovskiy@Sun.COM 				break;
7649412SAleksandr.Guzovskiy@Sun.COM 			}
7659412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(cbytes == max_blksz);
7669412SAleksandr.Guzovskiy@Sun.COM 		}
7679412SAleksandr.Guzovskiy@Sun.COM 
7689412SAleksandr.Guzovskiy@Sun.COM 		/*
7699412SAleksandr.Guzovskiy@Sun.COM 		 * Start a transaction.
7709412SAleksandr.Guzovskiy@Sun.COM 		 */
771789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
77211935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
773789Sahrens 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
77411935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, zp);
7758227SNeil.Perrin@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
776789Sahrens 		if (error) {
7778227SNeil.Perrin@Sun.COM 			if (error == ERESTART) {
7782113Sahrens 				dmu_tx_wait(tx);
7792113Sahrens 				dmu_tx_abort(tx);
7809412SAleksandr.Guzovskiy@Sun.COM 				goto again;
781789Sahrens 			}
7822113Sahrens 			dmu_tx_abort(tx);
7839412SAleksandr.Guzovskiy@Sun.COM 			if (abuf != NULL)
7849412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7853638Sbillm 			break;
7863638Sbillm 		}
7873638Sbillm 
7883638Sbillm 		/*
7893638Sbillm 		 * If zfs_range_lock() over-locked we grow the blocksize
7903638Sbillm 		 * and then reduce the lock range.  This will only happen
7913638Sbillm 		 * on the first iteration since zfs_range_reduce() will
7923638Sbillm 		 * shrink down r_len to the appropriate size.
7933638Sbillm 		 */
7943638Sbillm 		if (rl->r_len == UINT64_MAX) {
7953638Sbillm 			uint64_t new_blksz;
7963638Sbillm 
7973638Sbillm 			if (zp->z_blksz > max_blksz) {
7983638Sbillm 				ASSERT(!ISP2(zp->z_blksz));
7993638Sbillm 				new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
8003638Sbillm 			} else {
8013638Sbillm 				new_blksz = MIN(end_size, max_blksz);
8023638Sbillm 			}
8033638Sbillm 			zfs_grow_blocksize(zp, new_blksz, tx);
8043638Sbillm 			zfs_range_reduce(rl, woff, n);
8053638Sbillm 		}
8063638Sbillm 
8073638Sbillm 		/*
8083638Sbillm 		 * XXX - should we really limit each write to z_max_blksz?
8093638Sbillm 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
8103638Sbillm 		 */
8113638Sbillm 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
8123638Sbillm 
8139412SAleksandr.Guzovskiy@Sun.COM 		if (abuf == NULL) {
8149412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes = uio->uio_resid;
81512123STim.Haley@Sun.COM 			error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
81612123STim.Haley@Sun.COM 			    uio, nbytes, tx);
8179412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes -= uio->uio_resid;
8189412SAleksandr.Guzovskiy@Sun.COM 		} else {
8199412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes = nbytes;
82011539SChunli.Zhang@Sun.COM 			ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
82111539SChunli.Zhang@Sun.COM 			/*
82211539SChunli.Zhang@Sun.COM 			 * If this is not a full block write, but we are
82311539SChunli.Zhang@Sun.COM 			 * extending the file past EOF and this data starts
82411539SChunli.Zhang@Sun.COM 			 * block-aligned, use assign_arcbuf().  Otherwise,
82511539SChunli.Zhang@Sun.COM 			 * write via dmu_write().
82611539SChunli.Zhang@Sun.COM 			 */
82711539SChunli.Zhang@Sun.COM 			if (tx_bytes < max_blksz && (!write_eof ||
82811539SChunli.Zhang@Sun.COM 			    aiov->iov_base != abuf->b_data)) {
82911539SChunli.Zhang@Sun.COM 				ASSERT(xuio);
83011539SChunli.Zhang@Sun.COM 				dmu_write(zfsvfs->z_os, zp->z_id, woff,
83111539SChunli.Zhang@Sun.COM 				    aiov->iov_len, aiov->iov_base, tx);
83211539SChunli.Zhang@Sun.COM 				dmu_return_arcbuf(abuf);
83311539SChunli.Zhang@Sun.COM 				xuio_stat_wbuf_copied();
83411539SChunli.Zhang@Sun.COM 			} else {
83511539SChunli.Zhang@Sun.COM 				ASSERT(xuio || tx_bytes == max_blksz);
83611935SMark.Shellenbaum@Sun.COM 				dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
83711935SMark.Shellenbaum@Sun.COM 				    woff, abuf, tx);
83811539SChunli.Zhang@Sun.COM 			}
8399412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(tx_bytes <= uio->uio_resid);
8409412SAleksandr.Guzovskiy@Sun.COM 			uioskip(uio, tx_bytes);
8419412SAleksandr.Guzovskiy@Sun.COM 		}
8429412SAleksandr.Guzovskiy@Sun.COM 		if (tx_bytes && vn_has_cached_data(vp)) {
8438636SMark.Maybee@Sun.COM 			update_pages(vp, woff,
8448636SMark.Maybee@Sun.COM 			    tx_bytes, zfsvfs->z_os, zp->z_id);
8459412SAleksandr.Guzovskiy@Sun.COM 		}
8463638Sbillm 
8473638Sbillm 		/*
8483638Sbillm 		 * If we made no progress, we're done.  If we made even
8493638Sbillm 		 * partial progress, update the znode and ZIL accordingly.
8503638Sbillm 		 */
8513638Sbillm 		if (tx_bytes == 0) {
85211935SMark.Shellenbaum@Sun.COM 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
85311935SMark.Shellenbaum@Sun.COM 			    (void *)&zp->z_size, sizeof (uint64_t), tx);
8543897Smaybee 			dmu_tx_commit(tx);
8553638Sbillm 			ASSERT(error != 0);
8563638Sbillm 			break;
8573638Sbillm 		}
8583638Sbillm 
859789Sahrens 		/*
8603638Sbillm 		 * Clear Set-UID/Set-GID bits on successful write if not
8613638Sbillm 		 * privileged and at least one of the excute bits is set.
8623638Sbillm 		 *
8633638Sbillm 		 * It would be nice to to this after all writes have
8643638Sbillm 		 * been done, but that would still expose the ISUID/ISGID
8653638Sbillm 		 * to another app after the partial write is committed.
8665331Samw 		 *
867789Sahrens 		 */
8683638Sbillm 		mutex_enter(&zp->z_acl_lock);
86911935SMark.Shellenbaum@Sun.COM 		if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
8703638Sbillm 		    (S_IXUSR >> 6))) != 0 &&
87111935SMark.Shellenbaum@Sun.COM 		    (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
8723638Sbillm 		    secpolicy_vnode_setid_retain(cr,
87311935SMark.Shellenbaum@Sun.COM 		    (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
87411935SMark.Shellenbaum@Sun.COM 			uint64_t newmode;
87511935SMark.Shellenbaum@Sun.COM 			zp->z_mode &= ~(S_ISUID | S_ISGID);
87611935SMark.Shellenbaum@Sun.COM 			newmode = zp->z_mode;
87711935SMark.Shellenbaum@Sun.COM 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
87811935SMark.Shellenbaum@Sun.COM 			    (void *)&newmode, sizeof (uint64_t), tx);
8793638Sbillm 		}
8803638Sbillm 		mutex_exit(&zp->z_acl_lock);
8813638Sbillm 
88211935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
88311935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
8843638Sbillm 
8853638Sbillm 		/*
8863638Sbillm 		 * Update the file size (zp_size) if it has changed;
8873638Sbillm 		 * account for possible concurrent updates.
8883638Sbillm 		 */
88911935SMark.Shellenbaum@Sun.COM 		while ((end_size = zp->z_size) < uio->uio_loffset) {
89011935SMark.Shellenbaum@Sun.COM 			(void) atomic_cas_64(&zp->z_size, end_size,
891789Sahrens 			    uio->uio_loffset);
89211935SMark.Shellenbaum@Sun.COM 			ASSERT(error == 0);
89311935SMark.Shellenbaum@Sun.COM 		}
89411935SMark.Shellenbaum@Sun.COM 		error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
89511935SMark.Shellenbaum@Sun.COM 
8963638Sbillm 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
8973638Sbillm 		dmu_tx_commit(tx);
8983638Sbillm 
8993638Sbillm 		if (error != 0)
9003638Sbillm 			break;
9013638Sbillm 		ASSERT(tx_bytes == nbytes);
9023638Sbillm 		n -= nbytes;
903789Sahrens 	}
904789Sahrens 
9052237Smaybee 	zfs_range_unlock(rl);
906789Sahrens 
907789Sahrens 	/*
908789Sahrens 	 * If we're in replay mode, or we made no progress, return error.
909789Sahrens 	 * Otherwise, it's at least a partial write, so it's successful.
910789Sahrens 	 */
9118227SNeil.Perrin@Sun.COM 	if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
912789Sahrens 		ZFS_EXIT(zfsvfs);
913789Sahrens 		return (error);
914789Sahrens 	}
915789Sahrens 
9162638Sperrin 	if (ioflag & (FSYNC | FDSYNC))
9172638Sperrin 		zil_commit(zilog, zp->z_last_itx, zp->z_id);
918789Sahrens 
919789Sahrens 	ZFS_EXIT(zfsvfs);
920789Sahrens 	return (0);
921789Sahrens }
922789Sahrens 
9232237Smaybee void
92410922SJeff.Bonwick@Sun.COM zfs_get_done(zgd_t *zgd, int error)
9252237Smaybee {
92610922SJeff.Bonwick@Sun.COM 	znode_t *zp = zgd->zgd_private;
92710922SJeff.Bonwick@Sun.COM 	objset_t *os = zp->z_zfsvfs->z_os;
92810922SJeff.Bonwick@Sun.COM 
92910922SJeff.Bonwick@Sun.COM 	if (zgd->zgd_db)
93010922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(zgd->zgd_db, zgd);
93110922SJeff.Bonwick@Sun.COM 
93210922SJeff.Bonwick@Sun.COM 	zfs_range_unlock(zgd->zgd_rl);
93310922SJeff.Bonwick@Sun.COM 
9349321SNeil.Perrin@Sun.COM 	/*
9359321SNeil.Perrin@Sun.COM 	 * Release the vnode asynchronously as we currently have the
9369321SNeil.Perrin@Sun.COM 	 * txg stopped from syncing.
9379321SNeil.Perrin@Sun.COM 	 */
93810922SJeff.Bonwick@Sun.COM 	VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
93910922SJeff.Bonwick@Sun.COM 
94010922SJeff.Bonwick@Sun.COM 	if (error == 0 && zgd->zgd_bp)
94110922SJeff.Bonwick@Sun.COM 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
94210922SJeff.Bonwick@Sun.COM 
9433063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
9442237Smaybee }
9452237Smaybee 
94610209SMark.Musante@Sun.COM #ifdef DEBUG
94710209SMark.Musante@Sun.COM static int zil_fault_io = 0;
94810209SMark.Musante@Sun.COM #endif
94910209SMark.Musante@Sun.COM 
950789Sahrens /*
951789Sahrens  * Get data to generate a TX_WRITE intent log record.
952789Sahrens  */
953789Sahrens int
9542237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
955789Sahrens {
956789Sahrens 	zfsvfs_t *zfsvfs = arg;
957789Sahrens 	objset_t *os = zfsvfs->z_os;
958789Sahrens 	znode_t *zp;
95910922SJeff.Bonwick@Sun.COM 	uint64_t object = lr->lr_foid;
96010922SJeff.Bonwick@Sun.COM 	uint64_t offset = lr->lr_offset;
96110922SJeff.Bonwick@Sun.COM 	uint64_t size = lr->lr_length;
96210922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = &lr->lr_blkptr;
9632237Smaybee 	dmu_buf_t *db;
9643063Sperrin 	zgd_t *zgd;
965789Sahrens 	int error = 0;
966789Sahrens 
96710922SJeff.Bonwick@Sun.COM 	ASSERT(zio != NULL);
96810922SJeff.Bonwick@Sun.COM 	ASSERT(size != 0);
969789Sahrens 
970789Sahrens 	/*
9711669Sperrin 	 * Nothing to do if the file has been removed
972789Sahrens 	 */
97310922SJeff.Bonwick@Sun.COM 	if (zfs_zget(zfsvfs, object, &zp) != 0)
974789Sahrens 		return (ENOENT);
9753461Sahrens 	if (zp->z_unlinked) {
9769321SNeil.Perrin@Sun.COM 		/*
9779321SNeil.Perrin@Sun.COM 		 * Release the vnode asynchronously as we currently have the
9789321SNeil.Perrin@Sun.COM 		 * txg stopped from syncing.
9799321SNeil.Perrin@Sun.COM 		 */
9809321SNeil.Perrin@Sun.COM 		VN_RELE_ASYNC(ZTOV(zp),
9819321SNeil.Perrin@Sun.COM 		    dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
982789Sahrens 		return (ENOENT);
983789Sahrens 	}
984789Sahrens 
98510922SJeff.Bonwick@Sun.COM 	zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
98610922SJeff.Bonwick@Sun.COM 	zgd->zgd_zilog = zfsvfs->z_log;
98710922SJeff.Bonwick@Sun.COM 	zgd->zgd_private = zp;
98810922SJeff.Bonwick@Sun.COM 
989789Sahrens 	/*
990789Sahrens 	 * Write records come in two flavors: immediate and indirect.
991789Sahrens 	 * For small writes it's cheaper to store the data with the
992789Sahrens 	 * log record (immediate); for large writes it's cheaper to
993789Sahrens 	 * sync the data and get a pointer to it (indirect) so that
994789Sahrens 	 * we don't have to write the data twice.
995789Sahrens 	 */
9961669Sperrin 	if (buf != NULL) { /* immediate write */
99710922SJeff.Bonwick@Sun.COM 		zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
9981669Sperrin 		/* test for truncation needs to be done while range locked */
99911935SMark.Shellenbaum@Sun.COM 		if (offset >= zp->z_size) {
10001669Sperrin 			error = ENOENT;
100110922SJeff.Bonwick@Sun.COM 		} else {
100210922SJeff.Bonwick@Sun.COM 			error = dmu_read(os, object, offset, size, buf,
100310922SJeff.Bonwick@Sun.COM 			    DMU_READ_NO_PREFETCH);
10041669Sperrin 		}
100510922SJeff.Bonwick@Sun.COM 		ASSERT(error == 0 || error == ENOENT);
10061669Sperrin 	} else { /* indirect write */
1007789Sahrens 		/*
10081669Sperrin 		 * Have to lock the whole block to ensure when it's
10091669Sperrin 		 * written out and it's checksum is being calculated
10101669Sperrin 		 * that no one can change the data. We need to re-check
10111669Sperrin 		 * blocksize after we get the lock in case it's changed!
1012789Sahrens 		 */
10131669Sperrin 		for (;;) {
101410922SJeff.Bonwick@Sun.COM 			uint64_t blkoff;
101510922SJeff.Bonwick@Sun.COM 			size = zp->z_blksz;
101610945SJeff.Bonwick@Sun.COM 			blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
101710922SJeff.Bonwick@Sun.COM 			offset -= blkoff;
101810922SJeff.Bonwick@Sun.COM 			zgd->zgd_rl = zfs_range_lock(zp, offset, size,
101910922SJeff.Bonwick@Sun.COM 			    RL_READER);
102010922SJeff.Bonwick@Sun.COM 			if (zp->z_blksz == size)
10211669Sperrin 				break;
102210922SJeff.Bonwick@Sun.COM 			offset += blkoff;
102310922SJeff.Bonwick@Sun.COM 			zfs_range_unlock(zgd->zgd_rl);
10241669Sperrin 		}
10251669Sperrin 		/* test for truncation needs to be done while range locked */
102611935SMark.Shellenbaum@Sun.COM 		if (lr->lr_offset >= zp->z_size)
10271669Sperrin 			error = ENOENT;
102810209SMark.Musante@Sun.COM #ifdef DEBUG
102910209SMark.Musante@Sun.COM 		if (zil_fault_io) {
103010209SMark.Musante@Sun.COM 			error = EIO;
103110209SMark.Musante@Sun.COM 			zil_fault_io = 0;
103210209SMark.Musante@Sun.COM 		}
103310209SMark.Musante@Sun.COM #endif
103410922SJeff.Bonwick@Sun.COM 		if (error == 0)
1035*12285SJeff.Bonwick@Sun.COM 			error = dmu_buf_hold(os, object, offset, zgd, &db,
1036*12285SJeff.Bonwick@Sun.COM 			    DMU_READ_NO_PREFETCH);
103710922SJeff.Bonwick@Sun.COM 
103810800SNeil.Perrin@Sun.COM 		if (error == 0) {
103910922SJeff.Bonwick@Sun.COM 			zgd->zgd_db = db;
104010922SJeff.Bonwick@Sun.COM 			zgd->zgd_bp = bp;
104110922SJeff.Bonwick@Sun.COM 
104210922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_offset == offset);
104310922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_size == size);
104410922SJeff.Bonwick@Sun.COM 
104510922SJeff.Bonwick@Sun.COM 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
104610922SJeff.Bonwick@Sun.COM 			    zfs_get_done, zgd);
104710922SJeff.Bonwick@Sun.COM 			ASSERT(error || lr->lr_length <= zp->z_blksz);
104810922SJeff.Bonwick@Sun.COM 
104910800SNeil.Perrin@Sun.COM 			/*
105010922SJeff.Bonwick@Sun.COM 			 * On success, we need to wait for the write I/O
105110922SJeff.Bonwick@Sun.COM 			 * initiated by dmu_sync() to complete before we can
105210922SJeff.Bonwick@Sun.COM 			 * release this dbuf.  We will finish everything up
105310922SJeff.Bonwick@Sun.COM 			 * in the zfs_get_done() callback.
105410800SNeil.Perrin@Sun.COM 			 */
105510922SJeff.Bonwick@Sun.COM 			if (error == 0)
105610922SJeff.Bonwick@Sun.COM 				return (0);
105710922SJeff.Bonwick@Sun.COM 
105810922SJeff.Bonwick@Sun.COM 			if (error == EALREADY) {
105910922SJeff.Bonwick@Sun.COM 				lr->lr_common.lrc_txtype = TX_WRITE2;
106010922SJeff.Bonwick@Sun.COM 				error = 0;
106110922SJeff.Bonwick@Sun.COM 			}
106210800SNeil.Perrin@Sun.COM 		}
1063789Sahrens 	}
106410922SJeff.Bonwick@Sun.COM 
106510922SJeff.Bonwick@Sun.COM 	zfs_get_done(zgd, error);
106610922SJeff.Bonwick@Sun.COM 
1067789Sahrens 	return (error);
1068789Sahrens }
1069789Sahrens 
1070789Sahrens /*ARGSUSED*/
1071789Sahrens static int
10725331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
10735331Samw     caller_context_t *ct)
1074789Sahrens {
1075789Sahrens 	znode_t *zp = VTOZ(vp);
1076789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1077789Sahrens 	int error;
1078789Sahrens 
10795367Sahrens 	ZFS_ENTER(zfsvfs);
10805367Sahrens 	ZFS_VERIFY_ZP(zp);
10815331Samw 
10825331Samw 	if (flag & V_ACE_MASK)
10835331Samw 		error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
10845331Samw 	else
10855331Samw 		error = zfs_zaccess_rwx(zp, mode, flag, cr);
10865331Samw 
1087789Sahrens 	ZFS_EXIT(zfsvfs);
1088789Sahrens 	return (error);
1089789Sahrens }
1090789Sahrens 
1091789Sahrens /*
10929981STim.Haley@Sun.COM  * If vnode is for a device return a specfs vnode instead.
10939981STim.Haley@Sun.COM  */
10949981STim.Haley@Sun.COM static int
10959981STim.Haley@Sun.COM specvp_check(vnode_t **vpp, cred_t *cr)
10969981STim.Haley@Sun.COM {
10979981STim.Haley@Sun.COM 	int error = 0;
10989981STim.Haley@Sun.COM 
10999981STim.Haley@Sun.COM 	if (IS_DEVVP(*vpp)) {
11009981STim.Haley@Sun.COM 		struct vnode *svp;
11019981STim.Haley@Sun.COM 
11029981STim.Haley@Sun.COM 		svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
11039981STim.Haley@Sun.COM 		VN_RELE(*vpp);
11049981STim.Haley@Sun.COM 		if (svp == NULL)
11059981STim.Haley@Sun.COM 			error = ENOSYS;
11069981STim.Haley@Sun.COM 		*vpp = svp;
11079981STim.Haley@Sun.COM 	}
11089981STim.Haley@Sun.COM 	return (error);
11099981STim.Haley@Sun.COM }
11109981STim.Haley@Sun.COM 
11119981STim.Haley@Sun.COM 
11129981STim.Haley@Sun.COM /*
1113789Sahrens  * Lookup an entry in a directory, or an extended attribute directory.
1114789Sahrens  * If it exists, return a held vnode reference for it.
1115789Sahrens  *
1116789Sahrens  *	IN:	dvp	- vnode of directory to search.
1117789Sahrens  *		nm	- name of entry to lookup.
1118789Sahrens  *		pnp	- full pathname to lookup [UNUSED].
1119789Sahrens  *		flags	- LOOKUP_XATTR set if looking for an attribute.
1120789Sahrens  *		rdir	- root directory vnode [UNUSED].
1121789Sahrens  *		cr	- credentials of caller.
11225331Samw  *		ct	- caller context
11235331Samw  *		direntflags - directory lookup flags
11245331Samw  *		realpnp - returned pathname.
1125789Sahrens  *
1126789Sahrens  *	OUT:	vpp	- vnode of located entry, NULL if not found.
1127789Sahrens  *
1128789Sahrens  *	RETURN:	0 if success
1129789Sahrens  *		error code if failure
1130789Sahrens  *
1131789Sahrens  * Timestamps:
1132789Sahrens  *	NA
1133789Sahrens  */
1134789Sahrens /* ARGSUSED */
1135789Sahrens static int
1136789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
11375331Samw     int flags, vnode_t *rdir, cred_t *cr,  caller_context_t *ct,
11385331Samw     int *direntflags, pathname_t *realpnp)
1139789Sahrens {
1140789Sahrens 	znode_t *zdp = VTOZ(dvp);
1141789Sahrens 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
11429981STim.Haley@Sun.COM 	int	error = 0;
11439981STim.Haley@Sun.COM 
11449981STim.Haley@Sun.COM 	/* fast path */
11459981STim.Haley@Sun.COM 	if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
11469981STim.Haley@Sun.COM 
11479981STim.Haley@Sun.COM 		if (dvp->v_type != VDIR) {
11489981STim.Haley@Sun.COM 			return (ENOTDIR);
114911935SMark.Shellenbaum@Sun.COM 		} else if (zdp->z_sa_hdl == NULL) {
11509981STim.Haley@Sun.COM 			return (EIO);
11519981STim.Haley@Sun.COM 		}
11529981STim.Haley@Sun.COM 
11539981STim.Haley@Sun.COM 		if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
11549981STim.Haley@Sun.COM 			error = zfs_fastaccesschk_execute(zdp, cr);
11559981STim.Haley@Sun.COM 			if (!error) {
11569981STim.Haley@Sun.COM 				*vpp = dvp;
11579981STim.Haley@Sun.COM 				VN_HOLD(*vpp);
11589981STim.Haley@Sun.COM 				return (0);
11599981STim.Haley@Sun.COM 			}
11609981STim.Haley@Sun.COM 			return (error);
11619981STim.Haley@Sun.COM 		} else {
11629981STim.Haley@Sun.COM 			vnode_t *tvp = dnlc_lookup(dvp, nm);
11639981STim.Haley@Sun.COM 
11649981STim.Haley@Sun.COM 			if (tvp) {
11659981STim.Haley@Sun.COM 				error = zfs_fastaccesschk_execute(zdp, cr);
11669981STim.Haley@Sun.COM 				if (error) {
11679981STim.Haley@Sun.COM 					VN_RELE(tvp);
11689981STim.Haley@Sun.COM 					return (error);
11699981STim.Haley@Sun.COM 				}
11709981STim.Haley@Sun.COM 				if (tvp == DNLC_NO_VNODE) {
11719981STim.Haley@Sun.COM 					VN_RELE(tvp);
11729981STim.Haley@Sun.COM 					return (ENOENT);
11739981STim.Haley@Sun.COM 				} else {
11749981STim.Haley@Sun.COM 					*vpp = tvp;
11759981STim.Haley@Sun.COM 					return (specvp_check(vpp, cr));
11769981STim.Haley@Sun.COM 				}
11779981STim.Haley@Sun.COM 			}
11789981STim.Haley@Sun.COM 		}
11799981STim.Haley@Sun.COM 	}
11809981STim.Haley@Sun.COM 
11819981STim.Haley@Sun.COM 	DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1182789Sahrens 
11835367Sahrens 	ZFS_ENTER(zfsvfs);
11845367Sahrens 	ZFS_VERIFY_ZP(zdp);
1185789Sahrens 
1186789Sahrens 	*vpp = NULL;
1187789Sahrens 
1188789Sahrens 	if (flags & LOOKUP_XATTR) {
1189789Sahrens 		/*
11903234Sck153898 		 * If the xattr property is off, refuse the lookup request.
11913234Sck153898 		 */
11923234Sck153898 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
11933234Sck153898 			ZFS_EXIT(zfsvfs);
11943234Sck153898 			return (EINVAL);
11953234Sck153898 		}
11963234Sck153898 
11973234Sck153898 		/*
1198789Sahrens 		 * We don't allow recursive attributes..
1199789Sahrens 		 * Maybe someday we will.
1200789Sahrens 		 */
120111935SMark.Shellenbaum@Sun.COM 		if (zdp->z_pflags & ZFS_XATTR) {
1202789Sahrens 			ZFS_EXIT(zfsvfs);
1203789Sahrens 			return (EINVAL);
1204789Sahrens 		}
1205789Sahrens 
12063280Sck153898 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1207789Sahrens 			ZFS_EXIT(zfsvfs);
1208789Sahrens 			return (error);
1209789Sahrens 		}
1210789Sahrens 
1211789Sahrens 		/*
1212789Sahrens 		 * Do we have permission to get into attribute directory?
1213789Sahrens 		 */
1214789Sahrens 
12155331Samw 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
12165331Samw 		    B_FALSE, cr)) {
1217789Sahrens 			VN_RELE(*vpp);
12185331Samw 			*vpp = NULL;
1219789Sahrens 		}
1220789Sahrens 
1221789Sahrens 		ZFS_EXIT(zfsvfs);
1222789Sahrens 		return (error);
1223789Sahrens 	}
1224789Sahrens 
12251512Sek110237 	if (dvp->v_type != VDIR) {
12261512Sek110237 		ZFS_EXIT(zfsvfs);
12271460Smarks 		return (ENOTDIR);
12281512Sek110237 	}
12291460Smarks 
1230789Sahrens 	/*
1231789Sahrens 	 * Check accessibility of directory.
1232789Sahrens 	 */
1233789Sahrens 
12345331Samw 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1235789Sahrens 		ZFS_EXIT(zfsvfs);
1236789Sahrens 		return (error);
1237789Sahrens 	}
1238789Sahrens 
12395498Stimh 	if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
12405331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
12415331Samw 		ZFS_EXIT(zfsvfs);
12425331Samw 		return (EILSEQ);
12435331Samw 	}
12445331Samw 
12455331Samw 	error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
12469981STim.Haley@Sun.COM 	if (error == 0)
12479981STim.Haley@Sun.COM 		error = specvp_check(vpp, cr);
1248789Sahrens 
1249789Sahrens 	ZFS_EXIT(zfsvfs);
1250789Sahrens 	return (error);
1251789Sahrens }
1252789Sahrens 
1253789Sahrens /*
1254789Sahrens  * Attempt to create a new entry in a directory.  If the entry
1255789Sahrens  * already exists, truncate the file if permissible, else return
1256789Sahrens  * an error.  Return the vp of the created or trunc'd file.
1257789Sahrens  *
1258789Sahrens  *	IN:	dvp	- vnode of directory to put new file entry in.
1259789Sahrens  *		name	- name of new file entry.
1260789Sahrens  *		vap	- attributes of new file.
1261789Sahrens  *		excl	- flag indicating exclusive or non-exclusive mode.
1262789Sahrens  *		mode	- mode to open file with.
1263789Sahrens  *		cr	- credentials of caller.
1264789Sahrens  *		flag	- large file flag [UNUSED].
12655331Samw  *		ct	- caller context
12665331Samw  *		vsecp 	- ACL to be set
1267789Sahrens  *
1268789Sahrens  *	OUT:	vpp	- vnode of created or trunc'd entry.
1269789Sahrens  *
1270789Sahrens  *	RETURN:	0 if success
1271789Sahrens  *		error code if failure
1272789Sahrens  *
1273789Sahrens  * Timestamps:
1274789Sahrens  *	dvp - ctime|mtime updated if new entry created
1275789Sahrens  *	 vp - ctime|mtime always, atime if new
1276789Sahrens  */
12775331Samw 
1278789Sahrens /* ARGSUSED */
1279789Sahrens static int
1280789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
12815331Samw     int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
12825331Samw     vsecattr_t *vsecp)
1283789Sahrens {
1284789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1285789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
12865326Sek110237 	zilog_t		*zilog;
12875326Sek110237 	objset_t	*os;
1288789Sahrens 	zfs_dirlock_t	*dl;
1289789Sahrens 	dmu_tx_t	*tx;
1290789Sahrens 	int		error;
12917847SMark.Shellenbaum@Sun.COM 	ksid_t		*ksid;
12927847SMark.Shellenbaum@Sun.COM 	uid_t		uid;
12937847SMark.Shellenbaum@Sun.COM 	gid_t		gid = crgetgid(cr);
129411935SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t   acl_ids;
12959179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
12965331Samw 
12975331Samw 	/*
12985331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
12995331Samw 	 * make sure file system is at proper version
13005331Samw 	 */
13015331Samw 
13027847SMark.Shellenbaum@Sun.COM 	ksid = crgetsid(cr, KSID_OWNER);
13037847SMark.Shellenbaum@Sun.COM 	if (ksid)
13047847SMark.Shellenbaum@Sun.COM 		uid = ksid_getid(ksid);
13057847SMark.Shellenbaum@Sun.COM 	else
13067847SMark.Shellenbaum@Sun.COM 		uid = crgetuid(cr);
13077847SMark.Shellenbaum@Sun.COM 
13085331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
13095331Samw 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
13107847SMark.Shellenbaum@Sun.COM 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
13115331Samw 		return (EINVAL);
1312789Sahrens 
13135367Sahrens 	ZFS_ENTER(zfsvfs);
13145367Sahrens 	ZFS_VERIFY_ZP(dzp);
13155326Sek110237 	os = zfsvfs->z_os;
13165326Sek110237 	zilog = zfsvfs->z_log;
1317789Sahrens 
13185498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
13195331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
13205331Samw 		ZFS_EXIT(zfsvfs);
13215331Samw 		return (EILSEQ);
13225331Samw 	}
13235331Samw 
13245331Samw 	if (vap->va_mask & AT_XVATTR) {
13255331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
13265331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
13275331Samw 			ZFS_EXIT(zfsvfs);
13285331Samw 			return (error);
13295331Samw 		}
13305331Samw 	}
1331789Sahrens top:
1332789Sahrens 	*vpp = NULL;
1333789Sahrens 
1334789Sahrens 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1335789Sahrens 		vap->va_mode &= ~VSVTX;
1336789Sahrens 
1337789Sahrens 	if (*name == '\0') {
1338789Sahrens 		/*
1339789Sahrens 		 * Null component name refers to the directory itself.
1340789Sahrens 		 */
1341789Sahrens 		VN_HOLD(dvp);
1342789Sahrens 		zp = dzp;
1343789Sahrens 		dl = NULL;
1344789Sahrens 		error = 0;
1345789Sahrens 	} else {
1346789Sahrens 		/* possible VN_HOLD(zp) */
13475331Samw 		int zflg = 0;
13485331Samw 
13495331Samw 		if (flag & FIGNORECASE)
13505331Samw 			zflg |= ZCILOOK;
13515331Samw 
13525331Samw 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
13535331Samw 		    NULL, NULL);
13545331Samw 		if (error) {
1355789Sahrens 			if (strcmp(name, "..") == 0)
1356789Sahrens 				error = EISDIR;
1357789Sahrens 			ZFS_EXIT(zfsvfs);
1358789Sahrens 			return (error);
1359789Sahrens 		}
1360789Sahrens 	}
136111935SMark.Shellenbaum@Sun.COM 
1362789Sahrens 	if (zp == NULL) {
13635331Samw 		uint64_t txtype;
13645331Samw 
1365789Sahrens 		/*
1366789Sahrens 		 * Create a new file object and update the directory
1367789Sahrens 		 * to reference it.
1368789Sahrens 		 */
13695331Samw 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1370789Sahrens 			goto out;
1371789Sahrens 		}
1372789Sahrens 
1373789Sahrens 		/*
1374789Sahrens 		 * We only support the creation of regular files in
1375789Sahrens 		 * extended attribute directories.
1376789Sahrens 		 */
137711935SMark.Shellenbaum@Sun.COM 
137811935SMark.Shellenbaum@Sun.COM 		if ((dzp->z_pflags & ZFS_XATTR) &&
1379789Sahrens 		    (vap->va_type != VREG)) {
1380789Sahrens 			error = EINVAL;
1381789Sahrens 			goto out;
1382789Sahrens 		}
1383789Sahrens 
13849179SMark.Shellenbaum@Sun.COM 		if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
13859179SMark.Shellenbaum@Sun.COM 		    &acl_ids)) != 0)
13869179SMark.Shellenbaum@Sun.COM 			goto out;
13879396SMatthew.Ahrens@Sun.COM 		if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
138810143STim.Haley@Sun.COM 			zfs_acl_ids_free(&acl_ids);
13899396SMatthew.Ahrens@Sun.COM 			error = EDQUOT;
13909396SMatthew.Ahrens@Sun.COM 			goto out;
13919396SMatthew.Ahrens@Sun.COM 		}
13929179SMark.Shellenbaum@Sun.COM 
1393789Sahrens 		tx = dmu_tx_create(os);
139411935SMark.Shellenbaum@Sun.COM 
139511935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
139611935SMark.Shellenbaum@Sun.COM 		    ZFS_SA_BASE_ATTR_SIZE);
139711935SMark.Shellenbaum@Sun.COM 
13989179SMark.Shellenbaum@Sun.COM 		fuid_dirtied = zfsvfs->z_fuid_dirty;
13999396SMatthew.Ahrens@Sun.COM 		if (fuid_dirtied)
14009396SMatthew.Ahrens@Sun.COM 			zfs_fuid_txhold(zfsvfs, tx);
14011544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
140211935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
140311935SMark.Shellenbaum@Sun.COM 		if (!zfsvfs->z_use_sa &&
140411935SMark.Shellenbaum@Sun.COM 		    acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1405789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
140611935SMark.Shellenbaum@Sun.COM 			    0, acl_ids.z_aclp->z_acl_bytes);
14075331Samw 		}
14088227SNeil.Perrin@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
1409789Sahrens 		if (error) {
14109179SMark.Shellenbaum@Sun.COM 			zfs_acl_ids_free(&acl_ids);
1411789Sahrens 			zfs_dirent_unlock(dl);
14128227SNeil.Perrin@Sun.COM 			if (error == ERESTART) {
14132113Sahrens 				dmu_tx_wait(tx);
14142113Sahrens 				dmu_tx_abort(tx);
1415789Sahrens 				goto top;
1416789Sahrens 			}
14172113Sahrens 			dmu_tx_abort(tx);
1418789Sahrens 			ZFS_EXIT(zfsvfs);
1419789Sahrens 			return (error);
1420789Sahrens 		}
142111935SMark.Shellenbaum@Sun.COM 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
14229179SMark.Shellenbaum@Sun.COM 
14239179SMark.Shellenbaum@Sun.COM 		if (fuid_dirtied)
14249179SMark.Shellenbaum@Sun.COM 			zfs_fuid_sync(zfsvfs, tx);
14259179SMark.Shellenbaum@Sun.COM 
1426789Sahrens 		(void) zfs_link_create(dl, zp, tx, ZNEW);
14275331Samw 		txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
14285331Samw 		if (flag & FIGNORECASE)
14295331Samw 			txtype |= TX_CI;
14305331Samw 		zfs_log_create(zilog, tx, txtype, dzp, zp, name,
14319179SMark.Shellenbaum@Sun.COM 		    vsecp, acl_ids.z_fuidp, vap);
14329179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
1433789Sahrens 		dmu_tx_commit(tx);
1434789Sahrens 	} else {
14355331Samw 		int aflags = (flag & FAPPEND) ? V_APPEND : 0;
14365331Samw 
1437789Sahrens 		/*
1438789Sahrens 		 * A directory entry already exists for this name.
1439789Sahrens 		 */
1440789Sahrens 		/*
1441789Sahrens 		 * Can't truncate an existing file if in exclusive mode.
1442789Sahrens 		 */
1443789Sahrens 		if (excl == EXCL) {
1444789Sahrens 			error = EEXIST;
1445789Sahrens 			goto out;
1446789Sahrens 		}
1447789Sahrens 		/*
1448789Sahrens 		 * Can't open a directory for writing.
1449789Sahrens 		 */
1450789Sahrens 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1451789Sahrens 			error = EISDIR;
1452789Sahrens 			goto out;
1453789Sahrens 		}
1454789Sahrens 		/*
1455789Sahrens 		 * Verify requested access to file.
1456789Sahrens 		 */
14575331Samw 		if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1458789Sahrens 			goto out;
1459789Sahrens 		}
1460789Sahrens 
1461789Sahrens 		mutex_enter(&dzp->z_lock);
1462789Sahrens 		dzp->z_seq++;
1463789Sahrens 		mutex_exit(&dzp->z_lock);
1464789Sahrens 
14651878Smaybee 		/*
14661878Smaybee 		 * Truncate regular files if requested.
14671878Smaybee 		 */
14681878Smaybee 		if ((ZTOV(zp)->v_type == VREG) &&
1469789Sahrens 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
14706992Smaybee 			/* we can't hold any locks when calling zfs_freesp() */
14716992Smaybee 			zfs_dirent_unlock(dl);
14726992Smaybee 			dl = NULL;
14731878Smaybee 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
14744863Spraks 			if (error == 0) {
14755331Samw 				vnevent_create(ZTOV(zp), ct);
14764863Spraks 			}
1477789Sahrens 		}
1478789Sahrens 	}
1479789Sahrens out:
1480789Sahrens 
1481789Sahrens 	if (dl)
1482789Sahrens 		zfs_dirent_unlock(dl);
1483789Sahrens 
1484789Sahrens 	if (error) {
1485789Sahrens 		if (zp)
1486789Sahrens 			VN_RELE(ZTOV(zp));
1487789Sahrens 	} else {
1488789Sahrens 		*vpp = ZTOV(zp);
14899981STim.Haley@Sun.COM 		error = specvp_check(vpp, cr);
1490789Sahrens 	}
1491789Sahrens 
1492789Sahrens 	ZFS_EXIT(zfsvfs);
1493789Sahrens 	return (error);
1494789Sahrens }
1495789Sahrens 
1496789Sahrens /*
1497789Sahrens  * Remove an entry from a directory.
1498789Sahrens  *
1499789Sahrens  *	IN:	dvp	- vnode of directory to remove entry from.
1500789Sahrens  *		name	- name of entry to remove.
1501789Sahrens  *		cr	- credentials of caller.
15025331Samw  *		ct	- caller context
15035331Samw  *		flags	- case flags
1504789Sahrens  *
1505789Sahrens  *	RETURN:	0 if success
1506789Sahrens  *		error code if failure
1507789Sahrens  *
1508789Sahrens  * Timestamps:
1509789Sahrens  *	dvp - ctime|mtime
1510789Sahrens  *	 vp - ctime (if nlink > 0)
1511789Sahrens  */
151211935SMark.Shellenbaum@Sun.COM 
151311935SMark.Shellenbaum@Sun.COM uint64_t null_xattr = 0;
151411935SMark.Shellenbaum@Sun.COM 
15155331Samw /*ARGSUSED*/
1516789Sahrens static int
15175331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
15185331Samw     int flags)
1519789Sahrens {
1520789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1521789Sahrens 	znode_t		*xzp = NULL;
1522789Sahrens 	vnode_t		*vp;
1523789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
15245326Sek110237 	zilog_t		*zilog;
152511935SMark.Shellenbaum@Sun.COM 	uint64_t	acl_obj, xattr_obj = 0;
152611935SMark.Shellenbaum@Sun.COM 	uint64_t 	xattr_obj_unlinked = 0;
1527789Sahrens 	zfs_dirlock_t	*dl;
1528789Sahrens 	dmu_tx_t	*tx;
15293461Sahrens 	boolean_t	may_delete_now, delete_now = FALSE;
15306992Smaybee 	boolean_t	unlinked, toobig = FALSE;
15315331Samw 	uint64_t	txtype;
15325331Samw 	pathname_t	*realnmp = NULL;
15335331Samw 	pathname_t	realnm;
1534789Sahrens 	int		error;
15355331Samw 	int		zflg = ZEXISTS;
1536789Sahrens 
15375367Sahrens 	ZFS_ENTER(zfsvfs);
15385367Sahrens 	ZFS_VERIFY_ZP(dzp);
15395326Sek110237 	zilog = zfsvfs->z_log;
1540789Sahrens 
15415331Samw 	if (flags & FIGNORECASE) {
15425331Samw 		zflg |= ZCILOOK;
15435331Samw 		pn_alloc(&realnm);
15445331Samw 		realnmp = &realnm;
15455331Samw 	}
15465331Samw 
1547789Sahrens top:
1548789Sahrens 	/*
1549789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1550789Sahrens 	 */
15515331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
15525331Samw 	    NULL, realnmp)) {
15535331Samw 		if (realnmp)
15545331Samw 			pn_free(realnmp);
1555789Sahrens 		ZFS_EXIT(zfsvfs);
1556789Sahrens 		return (error);
1557789Sahrens 	}
1558789Sahrens 
1559789Sahrens 	vp = ZTOV(zp);
1560789Sahrens 
1561789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1562789Sahrens 		goto out;
1563789Sahrens 	}
1564789Sahrens 
1565789Sahrens 	/*
1566789Sahrens 	 * Need to use rmdir for removing directories.
1567789Sahrens 	 */
1568789Sahrens 	if (vp->v_type == VDIR) {
1569789Sahrens 		error = EPERM;
1570789Sahrens 		goto out;
1571789Sahrens 	}
1572789Sahrens 
15735331Samw 	vnevent_remove(vp, dvp, name, ct);
15745331Samw 
15755331Samw 	if (realnmp)
15766492Stimh 		dnlc_remove(dvp, realnmp->pn_buf);
15775331Samw 	else
15785331Samw 		dnlc_remove(dvp, name);
15791484Sek110237 
1580789Sahrens 	mutex_enter(&vp->v_lock);
1581789Sahrens 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1582789Sahrens 	mutex_exit(&vp->v_lock);
1583789Sahrens 
1584789Sahrens 	/*
15853461Sahrens 	 * We may delete the znode now, or we may put it in the unlinked set;
1586789Sahrens 	 * it depends on whether we're the last link, and on whether there are
1587789Sahrens 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1588789Sahrens 	 * allow for either case.
1589789Sahrens 	 */
1590789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
15911544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
159211935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
159311935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
159411935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, dzp);
15956992Smaybee 	if (may_delete_now) {
15966992Smaybee 		toobig =
159711935SMark.Shellenbaum@Sun.COM 		    zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
15986992Smaybee 		/* if the file is too big, only hold_free a token amount */
15996992Smaybee 		dmu_tx_hold_free(tx, zp->z_id, 0,
16006992Smaybee 		    (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
16016992Smaybee 	}
1602789Sahrens 
1603789Sahrens 	/* are there any extended attributes? */
160411935SMark.Shellenbaum@Sun.COM 	error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
160511935SMark.Shellenbaum@Sun.COM 	    &xattr_obj, sizeof (xattr_obj));
160611935SMark.Shellenbaum@Sun.COM 	if (xattr_obj) {
160711935SMark.Shellenbaum@Sun.COM 		error = zfs_zget(zfsvfs, xattr_obj, &xzp);
160811935SMark.Shellenbaum@Sun.COM 		ASSERT3U(error, ==, 0);
160911935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
161011935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1611789Sahrens 	}
1612789Sahrens 
1613789Sahrens 	/* are there any additional acls */
161411935SMark.Shellenbaum@Sun.COM 	if ((acl_obj = ZFS_EXTERNAL_ACL(zp)) != 0 && may_delete_now)
1615789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1616789Sahrens 
1617789Sahrens 	/* charge as an update -- would be nice not to charge at all */
16183461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1619789Sahrens 
16208227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1621789Sahrens 	if (error) {
1622789Sahrens 		zfs_dirent_unlock(dl);
1623789Sahrens 		VN_RELE(vp);
16248227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
16252113Sahrens 			dmu_tx_wait(tx);
16262113Sahrens 			dmu_tx_abort(tx);
1627789Sahrens 			goto top;
1628789Sahrens 		}
16295331Samw 		if (realnmp)
16305331Samw 			pn_free(realnmp);
16312113Sahrens 		dmu_tx_abort(tx);
1632789Sahrens 		ZFS_EXIT(zfsvfs);
1633789Sahrens 		return (error);
1634789Sahrens 	}
1635789Sahrens 
1636789Sahrens 	/*
1637789Sahrens 	 * Remove the directory entry.
1638789Sahrens 	 */
16395331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1640789Sahrens 
1641789Sahrens 	if (error) {
1642789Sahrens 		dmu_tx_commit(tx);
1643789Sahrens 		goto out;
1644789Sahrens 	}
1645789Sahrens 
16463461Sahrens 	if (unlinked) {
164711935SMark.Shellenbaum@Sun.COM 
1648789Sahrens 		mutex_enter(&vp->v_lock);
164911935SMark.Shellenbaum@Sun.COM 
165011935SMark.Shellenbaum@Sun.COM 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
165111935SMark.Shellenbaum@Sun.COM 		    &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
16526992Smaybee 		delete_now = may_delete_now && !toobig &&
1653789Sahrens 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
165411935SMark.Shellenbaum@Sun.COM 		    xattr_obj == xattr_obj_unlinked && ZFS_EXTERNAL_ACL(zp) ==
165511935SMark.Shellenbaum@Sun.COM 		    acl_obj;
1656789Sahrens 		mutex_exit(&vp->v_lock);
1657789Sahrens 	}
1658789Sahrens 
1659789Sahrens 	if (delete_now) {
166011935SMark.Shellenbaum@Sun.COM 		if (xattr_obj_unlinked) {
166111935SMark.Shellenbaum@Sun.COM 			ASSERT3U(xzp->z_links, ==, 2);
1662789Sahrens 			mutex_enter(&xzp->z_lock);
16633461Sahrens 			xzp->z_unlinked = 1;
166411935SMark.Shellenbaum@Sun.COM 			xzp->z_links = 0;
166511935SMark.Shellenbaum@Sun.COM 			error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
166611935SMark.Shellenbaum@Sun.COM 			    &xzp->z_links, sizeof (xzp->z_links), tx);
166711935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error,  ==,  0);
1668789Sahrens 			mutex_exit(&xzp->z_lock);
16693461Sahrens 			zfs_unlinked_add(xzp, tx);
167011935SMark.Shellenbaum@Sun.COM 			if (zp->z_is_sa)
167111935SMark.Shellenbaum@Sun.COM 				error = sa_remove(zp->z_sa_hdl,
167211935SMark.Shellenbaum@Sun.COM 				    SA_ZPL_XATTR(zfsvfs), tx);
167311935SMark.Shellenbaum@Sun.COM 			else
167411935SMark.Shellenbaum@Sun.COM 				error = sa_update(zp->z_sa_hdl,
167511935SMark.Shellenbaum@Sun.COM 				    SA_ZPL_XATTR(zfsvfs), &null_xattr,
167611935SMark.Shellenbaum@Sun.COM 				    sizeof (uint64_t), tx);
167711935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error, ==, 0);
1678789Sahrens 		}
1679789Sahrens 		mutex_enter(&zp->z_lock);
1680789Sahrens 		mutex_enter(&vp->v_lock);
1681789Sahrens 		vp->v_count--;
1682789Sahrens 		ASSERT3U(vp->v_count, ==, 0);
1683789Sahrens 		mutex_exit(&vp->v_lock);
1684789Sahrens 		mutex_exit(&zp->z_lock);
1685789Sahrens 		zfs_znode_delete(zp, tx);
16863461Sahrens 	} else if (unlinked) {
16873461Sahrens 		zfs_unlinked_add(zp, tx);
1688789Sahrens 	}
1689789Sahrens 
16905331Samw 	txtype = TX_REMOVE;
16915331Samw 	if (flags & FIGNORECASE)
16925331Samw 		txtype |= TX_CI;
16935331Samw 	zfs_log_remove(zilog, tx, txtype, dzp, name);
1694789Sahrens 
1695789Sahrens 	dmu_tx_commit(tx);
1696789Sahrens out:
16975331Samw 	if (realnmp)
16985331Samw 		pn_free(realnmp);
16995331Samw 
1700789Sahrens 	zfs_dirent_unlock(dl);
1701789Sahrens 
170212178SMark.Shellenbaum@Sun.COM 	if (!delete_now)
1703789Sahrens 		VN_RELE(vp);
170412178SMark.Shellenbaum@Sun.COM 	if (xzp)
1705789Sahrens 		VN_RELE(ZTOV(xzp));
1706789Sahrens 
1707789Sahrens 	ZFS_EXIT(zfsvfs);
1708789Sahrens 	return (error);
1709789Sahrens }
1710789Sahrens 
1711789Sahrens /*
1712789Sahrens  * Create a new directory and insert it into dvp using the name
1713789Sahrens  * provided.  Return a pointer to the inserted directory.
1714789Sahrens  *
1715789Sahrens  *	IN:	dvp	- vnode of directory to add subdir to.
1716789Sahrens  *		dirname	- name of new directory.
1717789Sahrens  *		vap	- attributes of new directory.
1718789Sahrens  *		cr	- credentials of caller.
17195331Samw  *		ct	- caller context
17205331Samw  *		vsecp	- ACL to be set
1721789Sahrens  *
1722789Sahrens  *	OUT:	vpp	- vnode of created directory.
1723789Sahrens  *
1724789Sahrens  *	RETURN:	0 if success
1725789Sahrens  *		error code if failure
1726789Sahrens  *
1727789Sahrens  * Timestamps:
1728789Sahrens  *	dvp - ctime|mtime updated
1729789Sahrens  *	 vp - ctime|mtime|atime updated
1730789Sahrens  */
17315331Samw /*ARGSUSED*/
1732789Sahrens static int
17335331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
17345331Samw     caller_context_t *ct, int flags, vsecattr_t *vsecp)
1735789Sahrens {
1736789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1737789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
17385326Sek110237 	zilog_t		*zilog;
1739789Sahrens 	zfs_dirlock_t	*dl;
17405331Samw 	uint64_t	txtype;
1741789Sahrens 	dmu_tx_t	*tx;
1742789Sahrens 	int		error;
17435331Samw 	int		zf = ZNEW;
17447847SMark.Shellenbaum@Sun.COM 	ksid_t		*ksid;
17457847SMark.Shellenbaum@Sun.COM 	uid_t		uid;
17467847SMark.Shellenbaum@Sun.COM 	gid_t		gid = crgetgid(cr);
174711935SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t   acl_ids;
17489179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
1749789Sahrens 
1750789Sahrens 	ASSERT(vap->va_type == VDIR);
1751789Sahrens 
17525331Samw 	/*
17535331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
17545331Samw 	 * make sure file system is at proper version
17555331Samw 	 */
17565331Samw 
17577847SMark.Shellenbaum@Sun.COM 	ksid = crgetsid(cr, KSID_OWNER);
17587847SMark.Shellenbaum@Sun.COM 	if (ksid)
17597847SMark.Shellenbaum@Sun.COM 		uid = ksid_getid(ksid);
17607847SMark.Shellenbaum@Sun.COM 	else
17617847SMark.Shellenbaum@Sun.COM 		uid = crgetuid(cr);
17625331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
17637847SMark.Shellenbaum@Sun.COM 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
17647876SMark.Shellenbaum@Sun.COM 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
17655331Samw 		return (EINVAL);
17665331Samw 
17675367Sahrens 	ZFS_ENTER(zfsvfs);
17685367Sahrens 	ZFS_VERIFY_ZP(dzp);
17695326Sek110237 	zilog = zfsvfs->z_log;
1770789Sahrens 
177111935SMark.Shellenbaum@Sun.COM 	if (dzp->z_pflags & ZFS_XATTR) {
1772789Sahrens 		ZFS_EXIT(zfsvfs);
1773789Sahrens 		return (EINVAL);
1774789Sahrens 	}
17755331Samw 
17765498Stimh 	if (zfsvfs->z_utf8 && u8_validate(dirname,
17775331Samw 	    strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
17785331Samw 		ZFS_EXIT(zfsvfs);
17795331Samw 		return (EILSEQ);
17805331Samw 	}
17815331Samw 	if (flags & FIGNORECASE)
17825331Samw 		zf |= ZCILOOK;
17835331Samw 
17845331Samw 	if (vap->va_mask & AT_XVATTR)
17855331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
17865331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
17875331Samw 			ZFS_EXIT(zfsvfs);
17885331Samw 			return (error);
17895331Samw 		}
1790789Sahrens 
1791789Sahrens 	/*
1792789Sahrens 	 * First make sure the new directory doesn't exist.
1793789Sahrens 	 */
17945331Samw top:
17955331Samw 	*vpp = NULL;
17965331Samw 
17975331Samw 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
17985331Samw 	    NULL, NULL)) {
1799789Sahrens 		ZFS_EXIT(zfsvfs);
1800789Sahrens 		return (error);
1801789Sahrens 	}
1802789Sahrens 
18035331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
18041231Smarks 		zfs_dirent_unlock(dl);
18051231Smarks 		ZFS_EXIT(zfsvfs);
18061231Smarks 		return (error);
18071231Smarks 	}
18081231Smarks 
18099179SMark.Shellenbaum@Sun.COM 	if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
18109179SMark.Shellenbaum@Sun.COM 	    &acl_ids)) != 0) {
18119179SMark.Shellenbaum@Sun.COM 		zfs_dirent_unlock(dl);
18129179SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
18139179SMark.Shellenbaum@Sun.COM 		return (error);
18145331Samw 	}
18159396SMatthew.Ahrens@Sun.COM 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
181610143STim.Haley@Sun.COM 		zfs_acl_ids_free(&acl_ids);
18179396SMatthew.Ahrens@Sun.COM 		zfs_dirent_unlock(dl);
18189396SMatthew.Ahrens@Sun.COM 		ZFS_EXIT(zfsvfs);
18199396SMatthew.Ahrens@Sun.COM 		return (EDQUOT);
18209396SMatthew.Ahrens@Sun.COM 	}
18219179SMark.Shellenbaum@Sun.COM 
1822789Sahrens 	/*
1823789Sahrens 	 * Add a new entry to the directory.
1824789Sahrens 	 */
1825789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
18261544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
18271544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
18289179SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
18299396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
18309396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
183111935SMark.Shellenbaum@Sun.COM 	if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
183211935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
183311935SMark.Shellenbaum@Sun.COM 		    acl_ids.z_aclp->z_acl_bytes);
183411935SMark.Shellenbaum@Sun.COM 	}
183511935SMark.Shellenbaum@Sun.COM 
183611935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
183711935SMark.Shellenbaum@Sun.COM 	    ZFS_SA_BASE_ATTR_SIZE);
183811935SMark.Shellenbaum@Sun.COM 
18398227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1840789Sahrens 	if (error) {
18419179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
1842789Sahrens 		zfs_dirent_unlock(dl);
18438227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
18442113Sahrens 			dmu_tx_wait(tx);
18452113Sahrens 			dmu_tx_abort(tx);
1846789Sahrens 			goto top;
1847789Sahrens 		}
18482113Sahrens 		dmu_tx_abort(tx);
1849789Sahrens 		ZFS_EXIT(zfsvfs);
1850789Sahrens 		return (error);
1851789Sahrens 	}
1852789Sahrens 
1853789Sahrens 	/*
1854789Sahrens 	 * Create new node.
1855789Sahrens 	 */
185611935SMark.Shellenbaum@Sun.COM 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
18579179SMark.Shellenbaum@Sun.COM 
18589179SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
18599179SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
186011935SMark.Shellenbaum@Sun.COM 
1861789Sahrens 	/*
1862789Sahrens 	 * Now put new name in parent dir.
1863789Sahrens 	 */
1864789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1865789Sahrens 
1866789Sahrens 	*vpp = ZTOV(zp);
1867789Sahrens 
18685331Samw 	txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
18695331Samw 	if (flags & FIGNORECASE)
18705331Samw 		txtype |= TX_CI;
18719179SMark.Shellenbaum@Sun.COM 	zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
18729179SMark.Shellenbaum@Sun.COM 	    acl_ids.z_fuidp, vap);
18739179SMark.Shellenbaum@Sun.COM 
18749179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_free(&acl_ids);
187511935SMark.Shellenbaum@Sun.COM 
1876789Sahrens 	dmu_tx_commit(tx);
1877789Sahrens 
1878789Sahrens 	zfs_dirent_unlock(dl);
1879789Sahrens 
1880789Sahrens 	ZFS_EXIT(zfsvfs);
1881789Sahrens 	return (0);
1882789Sahrens }
1883789Sahrens 
1884789Sahrens /*
1885789Sahrens  * Remove a directory subdir entry.  If the current working
1886789Sahrens  * directory is the same as the subdir to be removed, the
1887789Sahrens  * remove will fail.
1888789Sahrens  *
1889789Sahrens  *	IN:	dvp	- vnode of directory to remove from.
1890789Sahrens  *		name	- name of directory to be removed.
1891789Sahrens  *		cwd	- vnode of current working directory.
1892789Sahrens  *		cr	- credentials of caller.
18935331Samw  *		ct	- caller context
18945331Samw  *		flags	- case flags
1895789Sahrens  *
1896789Sahrens  *	RETURN:	0 if success
1897789Sahrens  *		error code if failure
1898789Sahrens  *
1899789Sahrens  * Timestamps:
1900789Sahrens  *	dvp - ctime|mtime updated
1901789Sahrens  */
19025331Samw /*ARGSUSED*/
1903789Sahrens static int
19045331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
19055331Samw     caller_context_t *ct, int flags)
1906789Sahrens {
1907789Sahrens 	znode_t		*dzp = VTOZ(dvp);
1908789Sahrens 	znode_t		*zp;
1909789Sahrens 	vnode_t		*vp;
1910789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
19115326Sek110237 	zilog_t		*zilog;
1912789Sahrens 	zfs_dirlock_t	*dl;
1913789Sahrens 	dmu_tx_t	*tx;
1914789Sahrens 	int		error;
19155331Samw 	int		zflg = ZEXISTS;
1916789Sahrens 
19175367Sahrens 	ZFS_ENTER(zfsvfs);
19185367Sahrens 	ZFS_VERIFY_ZP(dzp);
19195326Sek110237 	zilog = zfsvfs->z_log;
1920789Sahrens 
19215331Samw 	if (flags & FIGNORECASE)
19225331Samw 		zflg |= ZCILOOK;
1923789Sahrens top:
1924789Sahrens 	zp = NULL;
1925789Sahrens 
1926789Sahrens 	/*
1927789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1928789Sahrens 	 */
19295331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
19305331Samw 	    NULL, NULL)) {
1931789Sahrens 		ZFS_EXIT(zfsvfs);
1932789Sahrens 		return (error);
1933789Sahrens 	}
1934789Sahrens 
1935789Sahrens 	vp = ZTOV(zp);
1936789Sahrens 
1937789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1938789Sahrens 		goto out;
1939789Sahrens 	}
1940789Sahrens 
1941789Sahrens 	if (vp->v_type != VDIR) {
1942789Sahrens 		error = ENOTDIR;
1943789Sahrens 		goto out;
1944789Sahrens 	}
1945789Sahrens 
1946789Sahrens 	if (vp == cwd) {
1947789Sahrens 		error = EINVAL;
1948789Sahrens 		goto out;
1949789Sahrens 	}
1950789Sahrens 
19515331Samw 	vnevent_rmdir(vp, dvp, name, ct);
1952789Sahrens 
1953789Sahrens 	/*
19543897Smaybee 	 * Grab a lock on the directory to make sure that noone is
19553897Smaybee 	 * trying to add (or lookup) entries while we are removing it.
19563897Smaybee 	 */
19573897Smaybee 	rw_enter(&zp->z_name_lock, RW_WRITER);
19583897Smaybee 
19593897Smaybee 	/*
19603897Smaybee 	 * Grab a lock on the parent pointer to make sure we play well
1961789Sahrens 	 * with the treewalk and directory rename code.
1962789Sahrens 	 */
1963789Sahrens 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1964789Sahrens 
1965789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
19661544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
196711935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
19683461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
196911935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
197011935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, dzp);
19718227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1972789Sahrens 	if (error) {
1973789Sahrens 		rw_exit(&zp->z_parent_lock);
19743897Smaybee 		rw_exit(&zp->z_name_lock);
1975789Sahrens 		zfs_dirent_unlock(dl);
1976789Sahrens 		VN_RELE(vp);
19778227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
19782113Sahrens 			dmu_tx_wait(tx);
19792113Sahrens 			dmu_tx_abort(tx);
1980789Sahrens 			goto top;
1981789Sahrens 		}
19822113Sahrens 		dmu_tx_abort(tx);
1983789Sahrens 		ZFS_EXIT(zfsvfs);
1984789Sahrens 		return (error);
1985789Sahrens 	}
1986789Sahrens 
19875331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
19885331Samw 
19895331Samw 	if (error == 0) {
19905331Samw 		uint64_t txtype = TX_RMDIR;
19915331Samw 		if (flags & FIGNORECASE)
19925331Samw 			txtype |= TX_CI;
19935331Samw 		zfs_log_remove(zilog, tx, txtype, dzp, name);
19945331Samw 	}
1995789Sahrens 
1996789Sahrens 	dmu_tx_commit(tx);
1997789Sahrens 
1998789Sahrens 	rw_exit(&zp->z_parent_lock);
19993897Smaybee 	rw_exit(&zp->z_name_lock);
2000789Sahrens out:
2001789Sahrens 	zfs_dirent_unlock(dl);
2002789Sahrens 
2003789Sahrens 	VN_RELE(vp);
2004789Sahrens 
2005789Sahrens 	ZFS_EXIT(zfsvfs);
2006789Sahrens 	return (error);
2007789Sahrens }
2008789Sahrens 
2009789Sahrens /*
2010789Sahrens  * Read as many directory entries as will fit into the provided
2011789Sahrens  * buffer from the given directory cursor position (specified in
2012789Sahrens  * the uio structure.
2013789Sahrens  *
2014789Sahrens  *	IN:	vp	- vnode of directory to read.
2015789Sahrens  *		uio	- structure supplying read location, range info,
2016789Sahrens  *			  and return buffer.
2017789Sahrens  *		cr	- credentials of caller.
20185331Samw  *		ct	- caller context
20195331Samw  *		flags	- case flags
2020789Sahrens  *
2021789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
2022789Sahrens  *		eofp	- set to true if end-of-file detected.
2023789Sahrens  *
2024789Sahrens  *	RETURN:	0 if success
2025789Sahrens  *		error code if failure
2026789Sahrens  *
2027789Sahrens  * Timestamps:
2028789Sahrens  *	vp - atime updated
2029789Sahrens  *
2030789Sahrens  * Note that the low 4 bits of the cookie returned by zap is always zero.
2031789Sahrens  * This allows us to use the low range for "special" directory entries:
2032789Sahrens  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
2033789Sahrens  * we use the offset 2 for the '.zfs' directory.
2034789Sahrens  */
2035789Sahrens /* ARGSUSED */
2036789Sahrens static int
20375331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
20385331Samw     caller_context_t *ct, int flags)
2039789Sahrens {
2040789Sahrens 	znode_t		*zp = VTOZ(vp);
2041789Sahrens 	iovec_t		*iovp;
20425331Samw 	edirent_t	*eodp;
2043789Sahrens 	dirent64_t	*odp;
2044789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2045869Sperrin 	objset_t	*os;
2046789Sahrens 	caddr_t		outbuf;
2047789Sahrens 	size_t		bufsize;
2048789Sahrens 	zap_cursor_t	zc;
2049789Sahrens 	zap_attribute_t	zap;
2050789Sahrens 	uint_t		bytes_wanted;
2051789Sahrens 	uint64_t	offset; /* must be unsigned; checks for < 1 */
205211935SMark.Shellenbaum@Sun.COM 	uint64_t	parent;
2053789Sahrens 	int		local_eof;
2054869Sperrin 	int		outcount;
2055869Sperrin 	int		error;
2056869Sperrin 	uint8_t		prefetch;
20575663Sck153898 	boolean_t	check_sysattrs;
2058789Sahrens 
20595367Sahrens 	ZFS_ENTER(zfsvfs);
20605367Sahrens 	ZFS_VERIFY_ZP(zp);
2061789Sahrens 
206211935SMark.Shellenbaum@Sun.COM 	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
206311935SMark.Shellenbaum@Sun.COM 	    &parent, sizeof (parent))) != 0) {
206411935SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
206511935SMark.Shellenbaum@Sun.COM 		return (error);
206611935SMark.Shellenbaum@Sun.COM 	}
206711935SMark.Shellenbaum@Sun.COM 
2068789Sahrens 	/*
2069789Sahrens 	 * If we are not given an eof variable,
2070789Sahrens 	 * use a local one.
2071789Sahrens 	 */
2072789Sahrens 	if (eofp == NULL)
2073789Sahrens 		eofp = &local_eof;
2074789Sahrens 
2075789Sahrens 	/*
2076789Sahrens 	 * Check for valid iov_len.
2077789Sahrens 	 */
2078789Sahrens 	if (uio->uio_iov->iov_len <= 0) {
2079789Sahrens 		ZFS_EXIT(zfsvfs);
2080789Sahrens 		return (EINVAL);
2081789Sahrens 	}
2082789Sahrens 
2083789Sahrens 	/*
2084789Sahrens 	 * Quit if directory has been removed (posix)
2085789Sahrens 	 */
20863461Sahrens 	if ((*eofp = zp->z_unlinked) != 0) {
2087789Sahrens 		ZFS_EXIT(zfsvfs);
2088789Sahrens 		return (0);
2089789Sahrens 	}
2090789Sahrens 
2091869Sperrin 	error = 0;
2092869Sperrin 	os = zfsvfs->z_os;
2093869Sperrin 	offset = uio->uio_loffset;
2094869Sperrin 	prefetch = zp->z_zn_prefetch;
2095869Sperrin 
2096789Sahrens 	/*
2097789Sahrens 	 * Initialize the iterator cursor.
2098789Sahrens 	 */
2099789Sahrens 	if (offset <= 3) {
2100789Sahrens 		/*
2101789Sahrens 		 * Start iteration from the beginning of the directory.
2102789Sahrens 		 */
2103869Sperrin 		zap_cursor_init(&zc, os, zp->z_id);
2104789Sahrens 	} else {
2105789Sahrens 		/*
2106789Sahrens 		 * The offset is a serialized cursor.
2107789Sahrens 		 */
2108869Sperrin 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2109789Sahrens 	}
2110789Sahrens 
2111789Sahrens 	/*
2112789Sahrens 	 * Get space to change directory entries into fs independent format.
2113789Sahrens 	 */
2114789Sahrens 	iovp = uio->uio_iov;
2115789Sahrens 	bytes_wanted = iovp->iov_len;
2116789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2117789Sahrens 		bufsize = bytes_wanted;
2118789Sahrens 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
2119789Sahrens 		odp = (struct dirent64 *)outbuf;
2120789Sahrens 	} else {
2121789Sahrens 		bufsize = bytes_wanted;
2122789Sahrens 		odp = (struct dirent64 *)iovp->iov_base;
2123789Sahrens 	}
21245331Samw 	eodp = (struct edirent *)odp;
2125789Sahrens 
2126789Sahrens 	/*
21277757SJanice.Chang@Sun.COM 	 * If this VFS supports the system attribute view interface; and
21287757SJanice.Chang@Sun.COM 	 * we're looking at an extended attribute directory; and we care
21297757SJanice.Chang@Sun.COM 	 * about normalization conflicts on this vfs; then we must check
21307757SJanice.Chang@Sun.COM 	 * for normalization conflicts with the sysattr name space.
21315663Sck153898 	 */
21327757SJanice.Chang@Sun.COM 	check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
21335663Sck153898 	    (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
21345663Sck153898 	    (flags & V_RDDIR_ENTFLAGS);
21355663Sck153898 
21365663Sck153898 	/*
2137789Sahrens 	 * Transform to file-system independent format
2138789Sahrens 	 */
2139789Sahrens 	outcount = 0;
2140789Sahrens 	while (outcount < bytes_wanted) {
21413912Slling 		ino64_t objnum;
21423912Slling 		ushort_t reclen;
21433912Slling 		off64_t *next;
21443912Slling 
2145789Sahrens 		/*
2146789Sahrens 		 * Special case `.', `..', and `.zfs'.
2147789Sahrens 		 */
2148789Sahrens 		if (offset == 0) {
2149789Sahrens 			(void) strcpy(zap.za_name, ".");
21505331Samw 			zap.za_normalization_conflict = 0;
21513912Slling 			objnum = zp->z_id;
2152789Sahrens 		} else if (offset == 1) {
2153789Sahrens 			(void) strcpy(zap.za_name, "..");
21545331Samw 			zap.za_normalization_conflict = 0;
215511935SMark.Shellenbaum@Sun.COM 			objnum = parent;
2156789Sahrens 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
2157789Sahrens 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
21585331Samw 			zap.za_normalization_conflict = 0;
21593912Slling 			objnum = ZFSCTL_INO_ROOT;
2160789Sahrens 		} else {
2161789Sahrens 			/*
2162789Sahrens 			 * Grab next entry.
2163789Sahrens 			 */
2164789Sahrens 			if (error = zap_cursor_retrieve(&zc, &zap)) {
2165789Sahrens 				if ((*eofp = (error == ENOENT)) != 0)
2166789Sahrens 					break;
2167789Sahrens 				else
2168789Sahrens 					goto update;
2169789Sahrens 			}
2170789Sahrens 
2171789Sahrens 			if (zap.za_integer_length != 8 ||
2172789Sahrens 			    zap.za_num_integers != 1) {
2173789Sahrens 				cmn_err(CE_WARN, "zap_readdir: bad directory "
2174789Sahrens 				    "entry, obj = %lld, offset = %lld\n",
2175789Sahrens 				    (u_longlong_t)zp->z_id,
2176789Sahrens 				    (u_longlong_t)offset);
2177789Sahrens 				error = ENXIO;
2178789Sahrens 				goto update;
2179789Sahrens 			}
21803912Slling 
21813912Slling 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
21823912Slling 			/*
21833912Slling 			 * MacOS X can extract the object type here such as:
21843912Slling 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
21853912Slling 			 */
21865663Sck153898 
21875663Sck153898 			if (check_sysattrs && !zap.za_normalization_conflict) {
21885663Sck153898 				zap.za_normalization_conflict =
21895663Sck153898 				    xattr_sysattr_casechk(zap.za_name);
21905663Sck153898 			}
2191789Sahrens 		}
21925331Samw 
21939749STim.Haley@Sun.COM 		if (flags & V_RDDIR_ACCFILTER) {
21949749STim.Haley@Sun.COM 			/*
21959749STim.Haley@Sun.COM 			 * If we have no access at all, don't include
21969749STim.Haley@Sun.COM 			 * this entry in the returned information
21979749STim.Haley@Sun.COM 			 */
21989749STim.Haley@Sun.COM 			znode_t	*ezp;
21999749STim.Haley@Sun.COM 			if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
22009749STim.Haley@Sun.COM 				goto skip_entry;
22019749STim.Haley@Sun.COM 			if (!zfs_has_access(ezp, cr)) {
22029749STim.Haley@Sun.COM 				VN_RELE(ZTOV(ezp));
22039749STim.Haley@Sun.COM 				goto skip_entry;
22049749STim.Haley@Sun.COM 			}
22059749STim.Haley@Sun.COM 			VN_RELE(ZTOV(ezp));
22069749STim.Haley@Sun.COM 		}
22079749STim.Haley@Sun.COM 
22085331Samw 		if (flags & V_RDDIR_ENTFLAGS)
22095331Samw 			reclen = EDIRENT_RECLEN(strlen(zap.za_name));
22105331Samw 		else
22115331Samw 			reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2212789Sahrens 
2213789Sahrens 		/*
2214789Sahrens 		 * Will this entry fit in the buffer?
2215789Sahrens 		 */
22163912Slling 		if (outcount + reclen > bufsize) {
2217789Sahrens 			/*
2218789Sahrens 			 * Did we manage to fit anything in the buffer?
2219789Sahrens 			 */
2220789Sahrens 			if (!outcount) {
2221789Sahrens 				error = EINVAL;
2222789Sahrens 				goto update;
2223789Sahrens 			}
2224789Sahrens 			break;
2225789Sahrens 		}
22265331Samw 		if (flags & V_RDDIR_ENTFLAGS) {
22275331Samw 			/*
22285331Samw 			 * Add extended flag entry:
22295331Samw 			 */
22305331Samw 			eodp->ed_ino = objnum;
22315331Samw 			eodp->ed_reclen = reclen;
22325331Samw 			/* NOTE: ed_off is the offset for the *next* entry */
22335331Samw 			next = &(eodp->ed_off);
22345331Samw 			eodp->ed_eflags = zap.za_normalization_conflict ?
22355331Samw 			    ED_CASE_CONFLICT : 0;
22365331Samw 			(void) strncpy(eodp->ed_name, zap.za_name,
22375331Samw 			    EDIRENT_NAMELEN(reclen));
22385331Samw 			eodp = (edirent_t *)((intptr_t)eodp + reclen);
22395331Samw 		} else {
22405331Samw 			/*
22415331Samw 			 * Add normal entry:
22425331Samw 			 */
22435331Samw 			odp->d_ino = objnum;
22445331Samw 			odp->d_reclen = reclen;
22455331Samw 			/* NOTE: d_off is the offset for the *next* entry */
22465331Samw 			next = &(odp->d_off);
22475331Samw 			(void) strncpy(odp->d_name, zap.za_name,
22485331Samw 			    DIRENT64_NAMELEN(reclen));
22495331Samw 			odp = (dirent64_t *)((intptr_t)odp + reclen);
22505331Samw 		}
22513912Slling 		outcount += reclen;
2252789Sahrens 
2253789Sahrens 		ASSERT(outcount <= bufsize);
2254789Sahrens 
2255789Sahrens 		/* Prefetch znode */
2256869Sperrin 		if (prefetch)
22573912Slling 			dmu_prefetch(os, objnum, 0, 0);
2258789Sahrens 
22599749STim.Haley@Sun.COM 	skip_entry:
2260789Sahrens 		/*
2261789Sahrens 		 * Move to the next entry, fill in the previous offset.
2262789Sahrens 		 */
2263789Sahrens 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2264789Sahrens 			zap_cursor_advance(&zc);
2265789Sahrens 			offset = zap_cursor_serialize(&zc);
2266789Sahrens 		} else {
2267789Sahrens 			offset += 1;
2268789Sahrens 		}
2269789Sahrens 		*next = offset;
2270789Sahrens 	}
2271869Sperrin 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2272789Sahrens 
2273789Sahrens 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2274789Sahrens 		iovp->iov_base += outcount;
2275789Sahrens 		iovp->iov_len -= outcount;
2276789Sahrens 		uio->uio_resid -= outcount;
2277789Sahrens 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2278789Sahrens 		/*
2279789Sahrens 		 * Reset the pointer.
2280789Sahrens 		 */
2281789Sahrens 		offset = uio->uio_loffset;
2282789Sahrens 	}
2283789Sahrens 
2284789Sahrens update:
2285885Sahrens 	zap_cursor_fini(&zc);
2286789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2287789Sahrens 		kmem_free(outbuf, bufsize);
2288789Sahrens 
2289789Sahrens 	if (error == ENOENT)
2290789Sahrens 		error = 0;
2291789Sahrens 
2292789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2293789Sahrens 
2294789Sahrens 	uio->uio_loffset = offset;
2295789Sahrens 	ZFS_EXIT(zfsvfs);
2296789Sahrens 	return (error);
2297789Sahrens }
2298789Sahrens 
22994720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4;
23004720Sfr157268 
2301789Sahrens static int
23025331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2303789Sahrens {
2304789Sahrens 	znode_t	*zp = VTOZ(vp);
2305789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2306789Sahrens 
23071773Seschrock 	/*
23081773Seschrock 	 * Regardless of whether this is required for standards conformance,
23091773Seschrock 	 * this is the logical behavior when fsync() is called on a file with
23101773Seschrock 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
23111773Seschrock 	 * going to be pushed out as part of the zil_commit().
23121773Seschrock 	 */
23131773Seschrock 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
23141773Seschrock 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
23155331Samw 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
23161773Seschrock 
23174720Sfr157268 	(void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
23184720Sfr157268 
23195367Sahrens 	ZFS_ENTER(zfsvfs);
23205367Sahrens 	ZFS_VERIFY_ZP(zp);
23212638Sperrin 	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
2322789Sahrens 	ZFS_EXIT(zfsvfs);
2323789Sahrens 	return (0);
2324789Sahrens }
2325789Sahrens 
23265331Samw 
2327789Sahrens /*
2328789Sahrens  * Get the requested file attributes and place them in the provided
2329789Sahrens  * vattr structure.
2330789Sahrens  *
2331789Sahrens  *	IN:	vp	- vnode of file.
2332789Sahrens  *		vap	- va_mask identifies requested attributes.
23335331Samw  *			  If AT_XVATTR set, then optional attrs are requested
23345331Samw  *		flags	- ATTR_NOACLCHECK (CIFS server context)
2335789Sahrens  *		cr	- credentials of caller.
23365331Samw  *		ct	- caller context
2337789Sahrens  *
2338789Sahrens  *	OUT:	vap	- attribute values.
2339789Sahrens  *
2340789Sahrens  *	RETURN:	0 (always succeeds)
2341789Sahrens  */
2342789Sahrens /* ARGSUSED */
2343789Sahrens static int
23445331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
23455331Samw     caller_context_t *ct)
2346789Sahrens {
2347789Sahrens 	znode_t *zp = VTOZ(vp);
2348789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
23495331Samw 	int	error = 0;
23504543Smarks 	uint64_t links;
235111935SMark.Shellenbaum@Sun.COM 	uint64_t mtime[2], ctime[2];
23525331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
23535331Samw 	xoptattr_t *xoap = NULL;
23545331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
235511935SMark.Shellenbaum@Sun.COM 	sa_bulk_attr_t bulk[2];
235611935SMark.Shellenbaum@Sun.COM 	int count = 0;
2357789Sahrens 
23585367Sahrens 	ZFS_ENTER(zfsvfs);
23595367Sahrens 	ZFS_VERIFY_ZP(zp);
236011935SMark.Shellenbaum@Sun.COM 
236111935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
236211935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
236311935SMark.Shellenbaum@Sun.COM 
236411935SMark.Shellenbaum@Sun.COM 	if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
236511935SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
236611935SMark.Shellenbaum@Sun.COM 		return (error);
236711935SMark.Shellenbaum@Sun.COM 	}
2368789Sahrens 
23695331Samw 	/*
23705331Samw 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
23715331Samw 	 * Also, if we are the owner don't bother, since owner should
23725331Samw 	 * always be allowed to read basic attributes of file.
23735331Samw 	 */
237411935SMark.Shellenbaum@Sun.COM 	if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) && (zp->z_uid != crgetuid(cr))) {
23755331Samw 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
23765331Samw 		    skipaclchk, cr)) {
23775331Samw 			ZFS_EXIT(zfsvfs);
23785331Samw 			return (error);
23795331Samw 		}
23805331Samw 	}
23815331Samw 
2382789Sahrens 	/*
2383789Sahrens 	 * Return all attributes.  It's cheaper to provide the answer
2384789Sahrens 	 * than to determine whether we were asked the question.
2385789Sahrens 	 */
2386789Sahrens 
23879774SRay.Hassan@Sun.COM 	mutex_enter(&zp->z_lock);
2388789Sahrens 	vap->va_type = vp->v_type;
238911935SMark.Shellenbaum@Sun.COM 	vap->va_mode = zp->z_mode & MODEMASK;
239011935SMark.Shellenbaum@Sun.COM 	vap->va_uid = zp->z_uid;
239111935SMark.Shellenbaum@Sun.COM 	vap->va_gid = zp->z_gid;
2392789Sahrens 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2393789Sahrens 	vap->va_nodeid = zp->z_id;
23944543Smarks 	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
239511935SMark.Shellenbaum@Sun.COM 		links = zp->z_links + 1;
23964543Smarks 	else
239711935SMark.Shellenbaum@Sun.COM 		links = zp->z_links;
23984543Smarks 	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
239911935SMark.Shellenbaum@Sun.COM 	vap->va_size = zp->z_size;
24001816Smarks 	vap->va_rdev = vp->v_rdev;
2401789Sahrens 	vap->va_seq = zp->z_seq;
2402789Sahrens 
24035331Samw 	/*
24045331Samw 	 * Add in any requested optional attributes and the create time.
24055331Samw 	 * Also set the corresponding bits in the returned attribute bitmap.
24065331Samw 	 */
24075331Samw 	if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
24085331Samw 		if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
24095331Samw 			xoap->xoa_archive =
241011935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_ARCHIVE) != 0);
24115331Samw 			XVA_SET_RTN(xvap, XAT_ARCHIVE);
24125331Samw 		}
24135331Samw 
24145331Samw 		if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
24155331Samw 			xoap->xoa_readonly =
241611935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_READONLY) != 0);
24175331Samw 			XVA_SET_RTN(xvap, XAT_READONLY);
24185331Samw 		}
24195331Samw 
24205331Samw 		if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
24215331Samw 			xoap->xoa_system =
242211935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_SYSTEM) != 0);
24235331Samw 			XVA_SET_RTN(xvap, XAT_SYSTEM);
24245331Samw 		}
24255331Samw 
24265331Samw 		if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
24275331Samw 			xoap->xoa_hidden =
242811935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_HIDDEN) != 0);
24295331Samw 			XVA_SET_RTN(xvap, XAT_HIDDEN);
24305331Samw 		}
24315331Samw 
24325331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
24335331Samw 			xoap->xoa_nounlink =
243411935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NOUNLINK) != 0);
24355331Samw 			XVA_SET_RTN(xvap, XAT_NOUNLINK);
24365331Samw 		}
24375331Samw 
24385331Samw 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
24395331Samw 			xoap->xoa_immutable =
244011935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
24415331Samw 			XVA_SET_RTN(xvap, XAT_IMMUTABLE);
24425331Samw 		}
24435331Samw 
24445331Samw 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
24455331Samw 			xoap->xoa_appendonly =
244611935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_APPENDONLY) != 0);
24475331Samw 			XVA_SET_RTN(xvap, XAT_APPENDONLY);
24485331Samw 		}
24495331Samw 
24505331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
24515331Samw 			xoap->xoa_nodump =
245211935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NODUMP) != 0);
24535331Samw 			XVA_SET_RTN(xvap, XAT_NODUMP);
24545331Samw 		}
24555331Samw 
24565331Samw 		if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
24575331Samw 			xoap->xoa_opaque =
245811935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_OPAQUE) != 0);
24595331Samw 			XVA_SET_RTN(xvap, XAT_OPAQUE);
24605331Samw 		}
24615331Samw 
24625331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
24635331Samw 			xoap->xoa_av_quarantined =
246411935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
24655331Samw 			XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
24665331Samw 		}
24675331Samw 
24685331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
24695331Samw 			xoap->xoa_av_modified =
247011935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
24715331Samw 			XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
24725331Samw 		}
24735331Samw 
24745331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
247511935SMark.Shellenbaum@Sun.COM 		    vp->v_type == VREG) {
247611935SMark.Shellenbaum@Sun.COM 			zfs_sa_get_scanstamp(zp, xvap);
24775331Samw 		}
24785331Samw 
24795331Samw 		if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
248011935SMark.Shellenbaum@Sun.COM 			uint64_t times[2];
248111935SMark.Shellenbaum@Sun.COM 
248211935SMark.Shellenbaum@Sun.COM 			(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
248311935SMark.Shellenbaum@Sun.COM 			    times, sizeof (times));
248411935SMark.Shellenbaum@Sun.COM 			ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
24855331Samw 			XVA_SET_RTN(xvap, XAT_CREATETIME);
24865331Samw 		}
248710793Sdai.ngo@sun.com 
248810793Sdai.ngo@sun.com 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
248911935SMark.Shellenbaum@Sun.COM 			xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
249010793Sdai.ngo@sun.com 			XVA_SET_RTN(xvap, XAT_REPARSE);
249110793Sdai.ngo@sun.com 		}
24925331Samw 	}
24935331Samw 
249411935SMark.Shellenbaum@Sun.COM 	ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
249511935SMark.Shellenbaum@Sun.COM 	ZFS_TIME_DECODE(&vap->va_mtime, mtime);
249611935SMark.Shellenbaum@Sun.COM 	ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2497789Sahrens 
2498789Sahrens 	mutex_exit(&zp->z_lock);
2499789Sahrens 
250011935SMark.Shellenbaum@Sun.COM 	sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2501789Sahrens 
2502789Sahrens 	if (zp->z_blksz == 0) {
2503789Sahrens 		/*
2504789Sahrens 		 * Block size hasn't been set; suggest maximal I/O transfers.
2505789Sahrens 		 */
2506789Sahrens 		vap->va_blksize = zfsvfs->z_max_blksz;
2507789Sahrens 	}
2508789Sahrens 
2509789Sahrens 	ZFS_EXIT(zfsvfs);
2510789Sahrens 	return (0);
2511789Sahrens }
2512789Sahrens 
2513789Sahrens /*
2514789Sahrens  * Set the file attributes to the values contained in the
2515789Sahrens  * vattr structure.
2516789Sahrens  *
2517789Sahrens  *	IN:	vp	- vnode of file to be modified.
2518789Sahrens  *		vap	- new attribute values.
25195331Samw  *			  If AT_XVATTR set, then optional attrs are being set
2520789Sahrens  *		flags	- ATTR_UTIME set if non-default time values provided.
25215331Samw  *			- ATTR_NOACLCHECK (CIFS context only).
2522789Sahrens  *		cr	- credentials of caller.
25235331Samw  *		ct	- caller context
2524789Sahrens  *
2525789Sahrens  *	RETURN:	0 if success
2526789Sahrens  *		error code if failure
2527789Sahrens  *
2528789Sahrens  * Timestamps:
2529789Sahrens  *	vp - ctime updated, mtime updated if size changed.
2530789Sahrens  */
2531789Sahrens /* ARGSUSED */
2532789Sahrens static int
2533789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2534789Sahrens 	caller_context_t *ct)
2535789Sahrens {
25365326Sek110237 	znode_t		*zp = VTOZ(vp);
2537789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
25385326Sek110237 	zilog_t		*zilog;
2539789Sahrens 	dmu_tx_t	*tx;
25401878Smaybee 	vattr_t		oldva;
25418190SMark.Shellenbaum@Sun.COM 	xvattr_t	tmpxvattr;
2542789Sahrens 	uint_t		mask = vap->va_mask;
25431878Smaybee 	uint_t		saved_mask;
25442796Smarks 	int		trim_mask = 0;
2545789Sahrens 	uint64_t	new_mode;
25469179SMark.Shellenbaum@Sun.COM 	uint64_t	new_uid, new_gid;
254711935SMark.Shellenbaum@Sun.COM 	uint64_t	xattr_obj = 0;
254811935SMark.Shellenbaum@Sun.COM 	uint64_t	mtime[2], ctime[2];
25491231Smarks 	znode_t		*attrzp;
2550789Sahrens 	int		need_policy = FALSE;
255111935SMark.Shellenbaum@Sun.COM 	int		err, err2;
25525331Samw 	zfs_fuid_info_t *fuidp = NULL;
25535331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
25545331Samw 	xoptattr_t	*xoap;
25555824Smarks 	zfs_acl_t	*aclp = NULL;
25565331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
255711935SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied = B_FALSE;
255811935SMark.Shellenbaum@Sun.COM 	sa_bulk_attr_t	bulk[7], xattr_bulk[7];
255911935SMark.Shellenbaum@Sun.COM 	int		count = 0, xattr_count = 0;
2560789Sahrens 
2561789Sahrens 	if (mask == 0)
2562789Sahrens 		return (0);
2563789Sahrens 
2564789Sahrens 	if (mask & AT_NOSET)
2565789Sahrens 		return (EINVAL);
2566789Sahrens 
25675367Sahrens 	ZFS_ENTER(zfsvfs);
25685367Sahrens 	ZFS_VERIFY_ZP(zp);
25695331Samw 
25705331Samw 	zilog = zfsvfs->z_log;
25715331Samw 
25725331Samw 	/*
25735331Samw 	 * Make sure that if we have ephemeral uid/gid or xvattr specified
25745331Samw 	 * that file system is at proper version level
25755331Samw 	 */
25765331Samw 
25775331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
25785331Samw 	    (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
25795331Samw 	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
25805386Stimh 	    (mask & AT_XVATTR))) {
25815386Stimh 		ZFS_EXIT(zfsvfs);
25825331Samw 		return (EINVAL);
25835386Stimh 	}
25845386Stimh 
25855386Stimh 	if (mask & AT_SIZE && vp->v_type == VDIR) {
25865386Stimh 		ZFS_EXIT(zfsvfs);
2587789Sahrens 		return (EISDIR);
25885386Stimh 	}
25895386Stimh 
25905386Stimh 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
25915386Stimh 		ZFS_EXIT(zfsvfs);
25921308Smarks 		return (EINVAL);
25935386Stimh 	}
25941308Smarks 
25955331Samw 	/*
25965331Samw 	 * If this is an xvattr_t, then get a pointer to the structure of
25975331Samw 	 * optional attributes.  If this is NULL, then we have a vattr_t.
25985331Samw 	 */
25995331Samw 	xoap = xva_getxoptattr(xvap);
26005331Samw 
26018190SMark.Shellenbaum@Sun.COM 	xva_init(&tmpxvattr);
26028190SMark.Shellenbaum@Sun.COM 
26035331Samw 	/*
26045331Samw 	 * Immutable files can only alter immutable bit and atime
26055331Samw 	 */
260611935SMark.Shellenbaum@Sun.COM 	if ((zp->z_pflags & ZFS_IMMUTABLE) &&
26075331Samw 	    ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
26085386Stimh 	    ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
26095386Stimh 		ZFS_EXIT(zfsvfs);
26105331Samw 		return (EPERM);
26115386Stimh 	}
26125386Stimh 
261311935SMark.Shellenbaum@Sun.COM 	if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
26145386Stimh 		ZFS_EXIT(zfsvfs);
26155331Samw 		return (EPERM);
26165386Stimh 	}
2617789Sahrens 
26186064Smarks 	/*
26196064Smarks 	 * Verify timestamps doesn't overflow 32 bits.
26206064Smarks 	 * ZFS can handle large timestamps, but 32bit syscalls can't
26216064Smarks 	 * handle times greater than 2039.  This check should be removed
26226064Smarks 	 * once large timestamps are fully supported.
26236064Smarks 	 */
26246064Smarks 	if (mask & (AT_ATIME | AT_MTIME)) {
26256064Smarks 		if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
26266064Smarks 		    ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
26276064Smarks 			ZFS_EXIT(zfsvfs);
26286064Smarks 			return (EOVERFLOW);
26296064Smarks 		}
26306064Smarks 	}
26316064Smarks 
2632789Sahrens top:
26331231Smarks 	attrzp = NULL;
2634789Sahrens 
26359981STim.Haley@Sun.COM 	/* Can this be moved to before the top label? */
2636789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2637789Sahrens 		ZFS_EXIT(zfsvfs);
2638789Sahrens 		return (EROFS);
2639789Sahrens 	}
2640789Sahrens 
2641789Sahrens 	/*
2642789Sahrens 	 * First validate permissions
2643789Sahrens 	 */
2644789Sahrens 
2645789Sahrens 	if (mask & AT_SIZE) {
26465331Samw 		err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2647789Sahrens 		if (err) {
2648789Sahrens 			ZFS_EXIT(zfsvfs);
2649789Sahrens 			return (err);
2650789Sahrens 		}
26511878Smaybee 		/*
26521878Smaybee 		 * XXX - Note, we are not providing any open
26531878Smaybee 		 * mode flags here (like FNDELAY), so we may
26541878Smaybee 		 * block if there are locks present... this
26551878Smaybee 		 * should be addressed in openat().
26561878Smaybee 		 */
26576992Smaybee 		/* XXX - would it be OK to generate a log record here? */
26586992Smaybee 		err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
26591878Smaybee 		if (err) {
26601878Smaybee 			ZFS_EXIT(zfsvfs);
26611878Smaybee 			return (err);
26621878Smaybee 		}
2663789Sahrens 	}
2664789Sahrens 
26655331Samw 	if (mask & (AT_ATIME|AT_MTIME) ||
26665331Samw 	    ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
26675331Samw 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
26685331Samw 	    XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
26695331Samw 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
267011935SMark.Shellenbaum@Sun.COM 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
26715331Samw 		need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
26725331Samw 		    skipaclchk, cr);
267311935SMark.Shellenbaum@Sun.COM 	}
2674789Sahrens 
2675789Sahrens 	if (mask & (AT_UID|AT_GID)) {
2676789Sahrens 		int	idmask = (mask & (AT_UID|AT_GID));
2677789Sahrens 		int	take_owner;
2678789Sahrens 		int	take_group;
2679789Sahrens 
2680789Sahrens 		/*
2681913Smarks 		 * NOTE: even if a new mode is being set,
2682913Smarks 		 * we may clear S_ISUID/S_ISGID bits.
2683913Smarks 		 */
2684913Smarks 
2685913Smarks 		if (!(mask & AT_MODE))
268611935SMark.Shellenbaum@Sun.COM 			vap->va_mode = zp->z_mode;
2687913Smarks 
2688913Smarks 		/*
2689789Sahrens 		 * Take ownership or chgrp to group we are a member of
2690789Sahrens 		 */
2691789Sahrens 
2692789Sahrens 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
26935331Samw 		take_group = (mask & AT_GID) &&
26945331Samw 		    zfs_groupmember(zfsvfs, vap->va_gid, cr);
2695789Sahrens 
2696789Sahrens 		/*
2697789Sahrens 		 * If both AT_UID and AT_GID are set then take_owner and
2698789Sahrens 		 * take_group must both be set in order to allow taking
2699789Sahrens 		 * ownership.
2700789Sahrens 		 *
2701789Sahrens 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2702789Sahrens 		 *
2703789Sahrens 		 */
2704789Sahrens 
2705789Sahrens 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2706789Sahrens 		    ((idmask == AT_UID) && take_owner) ||
2707789Sahrens 		    ((idmask == AT_GID) && take_group)) {
27085331Samw 			if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
27095331Samw 			    skipaclchk, cr) == 0) {
2710789Sahrens 				/*
2711789Sahrens 				 * Remove setuid/setgid for non-privileged users
2712789Sahrens 				 */
27131115Smarks 				secpolicy_setid_clear(vap, cr);
27142796Smarks 				trim_mask = (mask & (AT_UID|AT_GID));
2715789Sahrens 			} else {
2716789Sahrens 				need_policy =  TRUE;
2717789Sahrens 			}
2718789Sahrens 		} else {
2719789Sahrens 			need_policy =  TRUE;
2720789Sahrens 		}
2721789Sahrens 	}
2722789Sahrens 
27232796Smarks 	mutex_enter(&zp->z_lock);
272411935SMark.Shellenbaum@Sun.COM 	oldva.va_mode = zp->z_mode;
272511935SMark.Shellenbaum@Sun.COM 	oldva.va_uid = zp->z_uid;
272611935SMark.Shellenbaum@Sun.COM 	oldva.va_gid = zp->z_gid;
27275331Samw 	if (mask & AT_XVATTR) {
27288190SMark.Shellenbaum@Sun.COM 		/*
27298190SMark.Shellenbaum@Sun.COM 		 * Update xvattr mask to include only those attributes
27308190SMark.Shellenbaum@Sun.COM 		 * that are actually changing.
27318190SMark.Shellenbaum@Sun.COM 		 *
27328190SMark.Shellenbaum@Sun.COM 		 * the bits will be restored prior to actually setting
27338190SMark.Shellenbaum@Sun.COM 		 * the attributes so the caller thinks they were set.
27348190SMark.Shellenbaum@Sun.COM 		 */
27358190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
27368190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_appendonly !=
273711935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
27388190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27398190SMark.Shellenbaum@Sun.COM 			} else {
27408190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_APPENDONLY);
27418190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
27428190SMark.Shellenbaum@Sun.COM 			}
27438190SMark.Shellenbaum@Sun.COM 		}
27448190SMark.Shellenbaum@Sun.COM 
27458190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
27468190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_nounlink !=
274711935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
27488190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27498190SMark.Shellenbaum@Sun.COM 			} else {
27508190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_NOUNLINK);
27518190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
27528190SMark.Shellenbaum@Sun.COM 			}
27538190SMark.Shellenbaum@Sun.COM 		}
27548190SMark.Shellenbaum@Sun.COM 
27558190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
27568190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_immutable !=
275711935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
27588190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27598190SMark.Shellenbaum@Sun.COM 			} else {
27608190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
27618190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
27628190SMark.Shellenbaum@Sun.COM 			}
27638190SMark.Shellenbaum@Sun.COM 		}
27648190SMark.Shellenbaum@Sun.COM 
27658190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
27668190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_nodump !=
276711935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NODUMP) != 0)) {
27688190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27698190SMark.Shellenbaum@Sun.COM 			} else {
27708190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_NODUMP);
27718190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
27728190SMark.Shellenbaum@Sun.COM 			}
27738190SMark.Shellenbaum@Sun.COM 		}
27748190SMark.Shellenbaum@Sun.COM 
27758190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
27768190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_av_modified !=
277711935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
27788190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27798190SMark.Shellenbaum@Sun.COM 			} else {
27808190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
27818190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
27828190SMark.Shellenbaum@Sun.COM 			}
27838190SMark.Shellenbaum@Sun.COM 		}
27848190SMark.Shellenbaum@Sun.COM 
27858190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
27868190SMark.Shellenbaum@Sun.COM 			if ((vp->v_type != VREG &&
27878190SMark.Shellenbaum@Sun.COM 			    xoap->xoa_av_quarantined) ||
27888190SMark.Shellenbaum@Sun.COM 			    xoap->xoa_av_quarantined !=
278911935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
27908190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27918190SMark.Shellenbaum@Sun.COM 			} else {
27928190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
27938190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
27948190SMark.Shellenbaum@Sun.COM 			}
27958190SMark.Shellenbaum@Sun.COM 		}
27968190SMark.Shellenbaum@Sun.COM 
279710793Sdai.ngo@sun.com 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
279810793Sdai.ngo@sun.com 			mutex_exit(&zp->z_lock);
279910793Sdai.ngo@sun.com 			ZFS_EXIT(zfsvfs);
280010793Sdai.ngo@sun.com 			return (EPERM);
280110793Sdai.ngo@sun.com 		}
280210793Sdai.ngo@sun.com 
28038190SMark.Shellenbaum@Sun.COM 		if (need_policy == FALSE &&
28048190SMark.Shellenbaum@Sun.COM 		    (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
28058190SMark.Shellenbaum@Sun.COM 		    XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
28065331Samw 			need_policy = TRUE;
28075331Samw 		}
28085331Samw 	}
28095331Samw 
28102796Smarks 	mutex_exit(&zp->z_lock);
28112796Smarks 
28122796Smarks 	if (mask & AT_MODE) {
28135331Samw 		if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
28142796Smarks 			err = secpolicy_setid_setsticky_clear(vp, vap,
28152796Smarks 			    &oldva, cr);
28162796Smarks 			if (err) {
28172796Smarks 				ZFS_EXIT(zfsvfs);
28182796Smarks 				return (err);
28192796Smarks 			}
28202796Smarks 			trim_mask |= AT_MODE;
28212796Smarks 		} else {
28222796Smarks 			need_policy = TRUE;
28232796Smarks 		}
28242796Smarks 	}
2825789Sahrens 
2826789Sahrens 	if (need_policy) {
28271115Smarks 		/*
28281115Smarks 		 * If trim_mask is set then take ownership
28292796Smarks 		 * has been granted or write_acl is present and user
28302796Smarks 		 * has the ability to modify mode.  In that case remove
28312796Smarks 		 * UID|GID and or MODE from mask so that
28321115Smarks 		 * secpolicy_vnode_setattr() doesn't revoke it.
28331115Smarks 		 */
28342796Smarks 
28352796Smarks 		if (trim_mask) {
28362796Smarks 			saved_mask = vap->va_mask;
28372796Smarks 			vap->va_mask &= ~trim_mask;
28382796Smarks 		}
2839789Sahrens 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
28405331Samw 		    (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2841789Sahrens 		if (err) {
2842789Sahrens 			ZFS_EXIT(zfsvfs);
2843789Sahrens 			return (err);
2844789Sahrens 		}
28451115Smarks 
28461115Smarks 		if (trim_mask)
28472796Smarks 			vap->va_mask |= saved_mask;
2848789Sahrens 	}
2849789Sahrens 
2850789Sahrens 	/*
2851789Sahrens 	 * secpolicy_vnode_setattr, or take ownership may have
2852789Sahrens 	 * changed va_mask
2853789Sahrens 	 */
2854789Sahrens 	mask = vap->va_mask;
2855789Sahrens 
285611935SMark.Shellenbaum@Sun.COM 	if ((mask & (AT_UID | AT_GID))) {
285711935SMark.Shellenbaum@Sun.COM 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xattr_obj,
285811935SMark.Shellenbaum@Sun.COM 		    sizeof (xattr_obj));
285911935SMark.Shellenbaum@Sun.COM 
286011935SMark.Shellenbaum@Sun.COM 		if (xattr_obj) {
286111935SMark.Shellenbaum@Sun.COM 			err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
28629396SMatthew.Ahrens@Sun.COM 			if (err)
286311935SMark.Shellenbaum@Sun.COM 				goto out2;
28649179SMark.Shellenbaum@Sun.COM 		}
28659179SMark.Shellenbaum@Sun.COM 		if (mask & AT_UID) {
28669179SMark.Shellenbaum@Sun.COM 			new_uid = zfs_fuid_create(zfsvfs,
28679179SMark.Shellenbaum@Sun.COM 			    (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
286811935SMark.Shellenbaum@Sun.COM 			if (vap->va_uid != zp->z_uid &&
286911935SMark.Shellenbaum@Sun.COM 			    zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
28709396SMatthew.Ahrens@Sun.COM 				err = EDQUOT;
287111935SMark.Shellenbaum@Sun.COM 				goto out2;
28729396SMatthew.Ahrens@Sun.COM 			}
28731231Smarks 		}
28749396SMatthew.Ahrens@Sun.COM 
28759179SMark.Shellenbaum@Sun.COM 		if (mask & AT_GID) {
28769179SMark.Shellenbaum@Sun.COM 			new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
28779179SMark.Shellenbaum@Sun.COM 			    cr, ZFS_GROUP, &fuidp);
287811935SMark.Shellenbaum@Sun.COM 			if (new_gid != zp->z_gid &&
287911935SMark.Shellenbaum@Sun.COM 			    zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
28809396SMatthew.Ahrens@Sun.COM 				err = EDQUOT;
288111935SMark.Shellenbaum@Sun.COM 				goto out2;
28829179SMark.Shellenbaum@Sun.COM 			}
28839179SMark.Shellenbaum@Sun.COM 		}
28841231Smarks 	}
288511935SMark.Shellenbaum@Sun.COM 	tx = dmu_tx_create(zfsvfs->z_os);
288611935SMark.Shellenbaum@Sun.COM 
288711935SMark.Shellenbaum@Sun.COM 	if (mask & AT_MODE) {
288811935SMark.Shellenbaum@Sun.COM 		uint64_t pmode = zp->z_mode;
288911935SMark.Shellenbaum@Sun.COM 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
289011935SMark.Shellenbaum@Sun.COM 
289111935SMark.Shellenbaum@Sun.COM 		if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
289211935SMark.Shellenbaum@Sun.COM 			goto out;
289311935SMark.Shellenbaum@Sun.COM 
289411935SMark.Shellenbaum@Sun.COM 		if (!zp->z_is_sa && ZFS_EXTERNAL_ACL(zp)) {
289511935SMark.Shellenbaum@Sun.COM 			/*
289611935SMark.Shellenbaum@Sun.COM 			 * Are we upgrading ACL from old V0 format
289711935SMark.Shellenbaum@Sun.COM 			 * to V1 format?
289811935SMark.Shellenbaum@Sun.COM 			 */
289911935SMark.Shellenbaum@Sun.COM 			if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
290011935SMark.Shellenbaum@Sun.COM 			    ZNODE_ACL_VERSION(zp) ==
290111935SMark.Shellenbaum@Sun.COM 			    ZFS_ACL_VERSION_INITIAL) {
290211935SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_free(tx,
290311935SMark.Shellenbaum@Sun.COM 				    ZFS_EXTERNAL_ACL(zp), 0,
290411935SMark.Shellenbaum@Sun.COM 				    DMU_OBJECT_END);
290511935SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
290611935SMark.Shellenbaum@Sun.COM 				    0, aclp->z_acl_bytes);
290711935SMark.Shellenbaum@Sun.COM 			} else {
290811935SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_write(tx, ZFS_EXTERNAL_ACL(zp), 0,
290911935SMark.Shellenbaum@Sun.COM 				    aclp->z_acl_bytes);
291011935SMark.Shellenbaum@Sun.COM 			}
291111935SMark.Shellenbaum@Sun.COM 		} else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
291211935SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
291311935SMark.Shellenbaum@Sun.COM 			    0, aclp->z_acl_bytes);
291411935SMark.Shellenbaum@Sun.COM 		}
291511935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
291611935SMark.Shellenbaum@Sun.COM 	} else {
291711935SMark.Shellenbaum@Sun.COM 		if ((mask & AT_XVATTR) &&
291811935SMark.Shellenbaum@Sun.COM 		    XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
291911935SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
292011935SMark.Shellenbaum@Sun.COM 		else
292111935SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
292211935SMark.Shellenbaum@Sun.COM 	}
292311935SMark.Shellenbaum@Sun.COM 
292411935SMark.Shellenbaum@Sun.COM 	if (attrzp) {
292511935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
292611935SMark.Shellenbaum@Sun.COM 	}
292711935SMark.Shellenbaum@Sun.COM 
292811935SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
292911935SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
293011935SMark.Shellenbaum@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
293111935SMark.Shellenbaum@Sun.COM 
293211935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
29331231Smarks 
29348227SNeil.Perrin@Sun.COM 	err = dmu_tx_assign(tx, TXG_NOWAIT);
2935789Sahrens 	if (err) {
29369396SMatthew.Ahrens@Sun.COM 		if (err == ERESTART)
29372113Sahrens 			dmu_tx_wait(tx);
29389396SMatthew.Ahrens@Sun.COM 		goto out;
2939789Sahrens 	}
2940789Sahrens 
294111935SMark.Shellenbaum@Sun.COM 	count = 0;
2942789Sahrens 	/*
2943789Sahrens 	 * Set each attribute requested.
2944789Sahrens 	 * We group settings according to the locks they need to acquire.
2945789Sahrens 	 *
2946789Sahrens 	 * Note: you cannot set ctime directly, although it will be
2947789Sahrens 	 * updated as a side-effect of calling this function.
2948789Sahrens 	 */
2949789Sahrens 
2950789Sahrens 	mutex_enter(&zp->z_lock);
2951789Sahrens 
295211935SMark.Shellenbaum@Sun.COM 	if (attrzp)
295311935SMark.Shellenbaum@Sun.COM 		mutex_enter(&attrzp->z_lock);
295411935SMark.Shellenbaum@Sun.COM 
295512164SMark.Shellenbaum@Sun.COM 	if (mask & (AT_UID|AT_GID)) {
295612164SMark.Shellenbaum@Sun.COM 
295712164SMark.Shellenbaum@Sun.COM 		if (mask & AT_UID) {
295812164SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
295912164SMark.Shellenbaum@Sun.COM 			    &new_uid, sizeof (new_uid));
296012164SMark.Shellenbaum@Sun.COM 			zp->z_uid = zfs_fuid_map_id(zfsvfs, new_uid,
296112164SMark.Shellenbaum@Sun.COM 			    cr, ZFS_OWNER);
296212164SMark.Shellenbaum@Sun.COM 			if (attrzp) {
296312164SMark.Shellenbaum@Sun.COM 				SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
296412164SMark.Shellenbaum@Sun.COM 				    SA_ZPL_UID(zfsvfs), NULL, &new_uid,
296512164SMark.Shellenbaum@Sun.COM 				    sizeof (new_uid));
296612164SMark.Shellenbaum@Sun.COM 				attrzp->z_gid = zp->z_uid;
296712164SMark.Shellenbaum@Sun.COM 			}
296811935SMark.Shellenbaum@Sun.COM 		}
296912164SMark.Shellenbaum@Sun.COM 
297012164SMark.Shellenbaum@Sun.COM 		if (mask & AT_GID) {
297112164SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
297212164SMark.Shellenbaum@Sun.COM 			    NULL, &new_gid, sizeof (new_gid));
297312164SMark.Shellenbaum@Sun.COM 			zp->z_gid = zfs_fuid_map_id(zfsvfs, new_gid, cr,
297412164SMark.Shellenbaum@Sun.COM 			    ZFS_GROUP);
297512164SMark.Shellenbaum@Sun.COM 			if (attrzp) {
297612164SMark.Shellenbaum@Sun.COM 				SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
297712164SMark.Shellenbaum@Sun.COM 				    SA_ZPL_GID(zfsvfs), NULL, &new_gid,
297812164SMark.Shellenbaum@Sun.COM 				    sizeof (new_gid));
297912164SMark.Shellenbaum@Sun.COM 				attrzp->z_gid = zp->z_gid;
298012164SMark.Shellenbaum@Sun.COM 			}
298112164SMark.Shellenbaum@Sun.COM 		}
298212164SMark.Shellenbaum@Sun.COM 		if (!(mask & AT_MODE)) {
298312164SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
298412164SMark.Shellenbaum@Sun.COM 			    NULL, &new_mode, sizeof (new_mode));
298512164SMark.Shellenbaum@Sun.COM 			new_mode = zp->z_mode;
298612164SMark.Shellenbaum@Sun.COM 		}
298712164SMark.Shellenbaum@Sun.COM 		err = zfs_acl_chown_setattr(zp);
298812164SMark.Shellenbaum@Sun.COM 		ASSERT(err == 0);
298911935SMark.Shellenbaum@Sun.COM 		if (attrzp) {
299012164SMark.Shellenbaum@Sun.COM 			err = zfs_acl_chown_setattr(attrzp);
299112164SMark.Shellenbaum@Sun.COM 			ASSERT(err == 0);
299211935SMark.Shellenbaum@Sun.COM 		}
299311935SMark.Shellenbaum@Sun.COM 	}
299411935SMark.Shellenbaum@Sun.COM 
2995789Sahrens 	if (mask & AT_MODE) {
29965824Smarks 		mutex_enter(&zp->z_acl_lock);
299711935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
299811935SMark.Shellenbaum@Sun.COM 		    &new_mode, sizeof (new_mode));
299911935SMark.Shellenbaum@Sun.COM 		zp->z_mode = new_mode;
300012164SMark.Shellenbaum@Sun.COM 		ASSERT3U((uintptr_t)aclp, !=, NULL);
30019179SMark.Shellenbaum@Sun.COM 		err = zfs_aclset_common(zp, aclp, cr, tx);
3002789Sahrens 		ASSERT3U(err, ==, 0);
300310143STim.Haley@Sun.COM 		zp->z_acl_cached = aclp;
300410143STim.Haley@Sun.COM 		aclp = NULL;
30055824Smarks 		mutex_exit(&zp->z_acl_lock);
3006789Sahrens 	}
3007789Sahrens 
30081231Smarks 	if (attrzp)
300911935SMark.Shellenbaum@Sun.COM 		mutex_exit(&attrzp->z_lock);
301011935SMark.Shellenbaum@Sun.COM 
301111935SMark.Shellenbaum@Sun.COM 	if (mask & AT_ATIME) {
301211935SMark.Shellenbaum@Sun.COM 		ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
301311935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
301411935SMark.Shellenbaum@Sun.COM 		    &zp->z_atime, sizeof (zp->z_atime));
30151231Smarks 	}
30161231Smarks 
301711935SMark.Shellenbaum@Sun.COM 	if (mask & AT_MTIME) {
301811935SMark.Shellenbaum@Sun.COM 		ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
301911935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
302011935SMark.Shellenbaum@Sun.COM 		    mtime, sizeof (mtime));
30211231Smarks 	}
30221231Smarks 
30236992Smaybee 	/* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
302411935SMark.Shellenbaum@Sun.COM 	if (mask & AT_SIZE && !(mask & AT_MTIME)) {
302511935SMark.Shellenbaum@Sun.COM 		if (!(mask & AT_MTIME))
302611935SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
302711935SMark.Shellenbaum@Sun.COM 			    NULL, mtime, sizeof (mtime));
302811935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
302911935SMark.Shellenbaum@Sun.COM 		    &ctime, sizeof (ctime));
303011935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
303111935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
303211935SMark.Shellenbaum@Sun.COM 	} else if (mask != 0) {
303311935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
303411935SMark.Shellenbaum@Sun.COM 		    &ctime, sizeof (ctime));
303511935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
303611935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
303711935SMark.Shellenbaum@Sun.COM 		if (attrzp) {
303811935SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
303911935SMark.Shellenbaum@Sun.COM 			    SA_ZPL_CTIME(zfsvfs), NULL,
304011935SMark.Shellenbaum@Sun.COM 			    &ctime, sizeof (ctime));
304111935SMark.Shellenbaum@Sun.COM 			zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
304211935SMark.Shellenbaum@Sun.COM 			    mtime, ctime, B_TRUE);
304311935SMark.Shellenbaum@Sun.COM 		}
304411935SMark.Shellenbaum@Sun.COM 	}
30455331Samw 	/*
30465331Samw 	 * Do this after setting timestamps to prevent timestamp
30475331Samw 	 * update from toggling bit
30485331Samw 	 */
30495331Samw 
30505331Samw 	if (xoap && (mask & AT_XVATTR)) {
30518190SMark.Shellenbaum@Sun.COM 
30528190SMark.Shellenbaum@Sun.COM 		/*
30538190SMark.Shellenbaum@Sun.COM 		 * restore trimmed off masks
30548190SMark.Shellenbaum@Sun.COM 		 * so that return masks can be set for caller.
30558190SMark.Shellenbaum@Sun.COM 		 */
30568190SMark.Shellenbaum@Sun.COM 
30578190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
30588190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_APPENDONLY);
30598190SMark.Shellenbaum@Sun.COM 		}
30608190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
30618190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_NOUNLINK);
30628190SMark.Shellenbaum@Sun.COM 		}
30638190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
30648190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_IMMUTABLE);
30658190SMark.Shellenbaum@Sun.COM 		}
30668190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
30678190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_NODUMP);
30688190SMark.Shellenbaum@Sun.COM 		}
30698190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
30708190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
30718190SMark.Shellenbaum@Sun.COM 		}
30728190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
30738190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
30748190SMark.Shellenbaum@Sun.COM 		}
30758190SMark.Shellenbaum@Sun.COM 
307611935SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
30775331Samw 			ASSERT(vp->v_type == VREG);
30785331Samw 
307911935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
308011935SMark.Shellenbaum@Sun.COM 		    &zp->z_pflags, sizeof (zp->z_pflags));
308111935SMark.Shellenbaum@Sun.COM 		zfs_xvattr_set(zp, xvap, tx);
30825331Samw 	}
3083789Sahrens 
30849179SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
30859179SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
30869179SMark.Shellenbaum@Sun.COM 
30871878Smaybee 	if (mask != 0)
30885331Samw 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
30895331Samw 
3090789Sahrens 	mutex_exit(&zp->z_lock);
3091789Sahrens 
30929396SMatthew.Ahrens@Sun.COM out:
309311935SMark.Shellenbaum@Sun.COM 	if (err == 0 && attrzp) {
309411935SMark.Shellenbaum@Sun.COM 		err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
309511935SMark.Shellenbaum@Sun.COM 		    xattr_count, tx);
309611935SMark.Shellenbaum@Sun.COM 		ASSERT(err2 == 0);
309711935SMark.Shellenbaum@Sun.COM 	}
309811935SMark.Shellenbaum@Sun.COM 
30991231Smarks 	if (attrzp)
31001231Smarks 		VN_RELE(ZTOV(attrzp));
310110143STim.Haley@Sun.COM 	if (aclp)
310210143STim.Haley@Sun.COM 		zfs_acl_free(aclp);
310310143STim.Haley@Sun.COM 
31049396SMatthew.Ahrens@Sun.COM 	if (fuidp) {
31059396SMatthew.Ahrens@Sun.COM 		zfs_fuid_info_free(fuidp);
31069396SMatthew.Ahrens@Sun.COM 		fuidp = NULL;
31079396SMatthew.Ahrens@Sun.COM 	}
31089396SMatthew.Ahrens@Sun.COM 
310911935SMark.Shellenbaum@Sun.COM 	if (err) {
31109396SMatthew.Ahrens@Sun.COM 		dmu_tx_abort(tx);
311111935SMark.Shellenbaum@Sun.COM 		if (err == ERESTART)
311211935SMark.Shellenbaum@Sun.COM 			goto top;
311311935SMark.Shellenbaum@Sun.COM 	} else {
311411935SMark.Shellenbaum@Sun.COM 		err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
31159396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
311611935SMark.Shellenbaum@Sun.COM 	}
311711935SMark.Shellenbaum@Sun.COM 
311811935SMark.Shellenbaum@Sun.COM 
311911935SMark.Shellenbaum@Sun.COM out2:
3120789Sahrens 	ZFS_EXIT(zfsvfs);
3121789Sahrens 	return (err);
3122789Sahrens }
3123789Sahrens 
31243271Smaybee typedef struct zfs_zlock {
31253271Smaybee 	krwlock_t	*zl_rwlock;	/* lock we acquired */
31263271Smaybee 	znode_t		*zl_znode;	/* znode we held */
31273271Smaybee 	struct zfs_zlock *zl_next;	/* next in list */
31283271Smaybee } zfs_zlock_t;
31293271Smaybee 
31303271Smaybee /*
31313271Smaybee  * Drop locks and release vnodes that were held by zfs_rename_lock().
31323271Smaybee  */
31333271Smaybee static void
31343271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp)
31353271Smaybee {
31363271Smaybee 	zfs_zlock_t *zl;
31373271Smaybee 
31383271Smaybee 	while ((zl = *zlpp) != NULL) {
31393271Smaybee 		if (zl->zl_znode != NULL)
31403271Smaybee 			VN_RELE(ZTOV(zl->zl_znode));
31413271Smaybee 		rw_exit(zl->zl_rwlock);
31423271Smaybee 		*zlpp = zl->zl_next;
31433271Smaybee 		kmem_free(zl, sizeof (*zl));
31443271Smaybee 	}
31453271Smaybee }
31463271Smaybee 
3147789Sahrens /*
3148789Sahrens  * Search back through the directory tree, using the ".." entries.
3149789Sahrens  * Lock each directory in the chain to prevent concurrent renames.
3150789Sahrens  * Fail any attempt to move a directory into one of its own descendants.
3151789Sahrens  * XXX - z_parent_lock can overlap with map or grow locks
3152789Sahrens  */
3153789Sahrens static int
3154789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3155789Sahrens {
3156789Sahrens 	zfs_zlock_t	*zl;
31573638Sbillm 	znode_t		*zp = tdzp;
3158789Sahrens 	uint64_t	rootid = zp->z_zfsvfs->z_root;
315911935SMark.Shellenbaum@Sun.COM 	uint64_t	oidp = zp->z_id;
3160789Sahrens 	krwlock_t	*rwlp = &szp->z_parent_lock;
3161789Sahrens 	krw_t		rw = RW_WRITER;
3162789Sahrens 
3163789Sahrens 	/*
3164789Sahrens 	 * First pass write-locks szp and compares to zp->z_id.
3165789Sahrens 	 * Later passes read-lock zp and compare to zp->z_parent.
3166789Sahrens 	 */
3167789Sahrens 	do {
31683271Smaybee 		if (!rw_tryenter(rwlp, rw)) {
31693271Smaybee 			/*
31703271Smaybee 			 * Another thread is renaming in this path.
31713271Smaybee 			 * Note that if we are a WRITER, we don't have any
31723271Smaybee 			 * parent_locks held yet.
31733271Smaybee 			 */
31743271Smaybee 			if (rw == RW_READER && zp->z_id > szp->z_id) {
31753271Smaybee 				/*
31763271Smaybee 				 * Drop our locks and restart
31773271Smaybee 				 */
31783271Smaybee 				zfs_rename_unlock(&zl);
31793271Smaybee 				*zlpp = NULL;
31803271Smaybee 				zp = tdzp;
318111935SMark.Shellenbaum@Sun.COM 				oidp = zp->z_id;
31823271Smaybee 				rwlp = &szp->z_parent_lock;
31833271Smaybee 				rw = RW_WRITER;
31843271Smaybee 				continue;
31853271Smaybee 			} else {
31863271Smaybee 				/*
31873271Smaybee 				 * Wait for other thread to drop its locks
31883271Smaybee 				 */
31893271Smaybee 				rw_enter(rwlp, rw);
31903271Smaybee 			}
31913271Smaybee 		}
31923271Smaybee 
3193789Sahrens 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3194789Sahrens 		zl->zl_rwlock = rwlp;
3195789Sahrens 		zl->zl_znode = NULL;
3196789Sahrens 		zl->zl_next = *zlpp;
3197789Sahrens 		*zlpp = zl;
3198789Sahrens 
319911935SMark.Shellenbaum@Sun.COM 		if (oidp == szp->z_id)		/* We're a descendant of szp */
3200789Sahrens 			return (EINVAL);
3201789Sahrens 
320211935SMark.Shellenbaum@Sun.COM 		if (oidp == rootid)		/* We've hit the top */
3203789Sahrens 			return (0);
3204789Sahrens 
3205789Sahrens 		if (rw == RW_READER) {		/* i.e. not the first pass */
320611935SMark.Shellenbaum@Sun.COM 			int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3207789Sahrens 			if (error)
3208789Sahrens 				return (error);
3209789Sahrens 			zl->zl_znode = zp;
3210789Sahrens 		}
321111935SMark.Shellenbaum@Sun.COM 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
321211935SMark.Shellenbaum@Sun.COM 		    &oidp, sizeof (oidp));
3213789Sahrens 		rwlp = &zp->z_parent_lock;
3214789Sahrens 		rw = RW_READER;
3215789Sahrens 
3216789Sahrens 	} while (zp->z_id != sdzp->z_id);
3217789Sahrens 
3218789Sahrens 	return (0);
3219789Sahrens }
3220789Sahrens 
3221789Sahrens /*
3222789Sahrens  * Move an entry from the provided source directory to the target
3223789Sahrens  * directory.  Change the entry name as indicated.
3224789Sahrens  *
3225789Sahrens  *	IN:	sdvp	- Source directory containing the "old entry".
3226789Sahrens  *		snm	- Old entry name.
3227789Sahrens  *		tdvp	- Target directory to contain the "new entry".
3228789Sahrens  *		tnm	- New entry name.
3229789Sahrens  *		cr	- credentials of caller.
32305331Samw  *		ct	- caller context
32315331Samw  *		flags	- case flags
3232789Sahrens  *
3233789Sahrens  *	RETURN:	0 if success
3234789Sahrens  *		error code if failure
3235789Sahrens  *
3236789Sahrens  * Timestamps:
3237789Sahrens  *	sdvp,tdvp - ctime|mtime updated
3238789Sahrens  */
32395331Samw /*ARGSUSED*/
3240789Sahrens static int
32415331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
32425331Samw     caller_context_t *ct, int flags)
3243789Sahrens {
3244789Sahrens 	znode_t		*tdzp, *szp, *tzp;
3245789Sahrens 	znode_t		*sdzp = VTOZ(sdvp);
3246789Sahrens 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
32475326Sek110237 	zilog_t		*zilog;
3248789Sahrens 	vnode_t		*realvp;
3249789Sahrens 	zfs_dirlock_t	*sdl, *tdl;
3250789Sahrens 	dmu_tx_t	*tx;
3251789Sahrens 	zfs_zlock_t	*zl;
32525331Samw 	int		cmp, serr, terr;
32535331Samw 	int		error = 0;
32545331Samw 	int		zflg = 0;
3255789Sahrens 
32565367Sahrens 	ZFS_ENTER(zfsvfs);
32575367Sahrens 	ZFS_VERIFY_ZP(sdzp);
32585326Sek110237 	zilog = zfsvfs->z_log;
3259789Sahrens 
3260789Sahrens 	/*
3261789Sahrens 	 * Make sure we have the real vp for the target directory.
3262789Sahrens 	 */
32635331Samw 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3264789Sahrens 		tdvp = realvp;
3265789Sahrens 
326612079SMark.Shellenbaum@Sun.COM 	if (tdvp->v_vfsp != sdvp->v_vfsp || zfsctl_is_node(tdvp)) {
3267789Sahrens 		ZFS_EXIT(zfsvfs);
3268789Sahrens 		return (EXDEV);
3269789Sahrens 	}
3270789Sahrens 
3271789Sahrens 	tdzp = VTOZ(tdvp);
32725367Sahrens 	ZFS_VERIFY_ZP(tdzp);
32735498Stimh 	if (zfsvfs->z_utf8 && u8_validate(tnm,
32745331Samw 	    strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
32755331Samw 		ZFS_EXIT(zfsvfs);
32765331Samw 		return (EILSEQ);
32775331Samw 	}
32785331Samw 
32795331Samw 	if (flags & FIGNORECASE)
32805331Samw 		zflg |= ZCILOOK;
32815331Samw 
3282789Sahrens top:
3283789Sahrens 	szp = NULL;
3284789Sahrens 	tzp = NULL;
3285789Sahrens 	zl = NULL;
3286789Sahrens 
3287789Sahrens 	/*
3288789Sahrens 	 * This is to prevent the creation of links into attribute space
3289789Sahrens 	 * by renaming a linked file into/outof an attribute directory.
3290789Sahrens 	 * See the comment in zfs_link() for why this is considered bad.
3291789Sahrens 	 */
329211935SMark.Shellenbaum@Sun.COM 	if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3293789Sahrens 		ZFS_EXIT(zfsvfs);
3294789Sahrens 		return (EINVAL);
3295789Sahrens 	}
3296789Sahrens 
3297789Sahrens 	/*
3298789Sahrens 	 * Lock source and target directory entries.  To prevent deadlock,
3299789Sahrens 	 * a lock ordering must be defined.  We lock the directory with
3300789Sahrens 	 * the smallest object id first, or if it's a tie, the one with
3301789Sahrens 	 * the lexically first name.
3302789Sahrens 	 */
3303789Sahrens 	if (sdzp->z_id < tdzp->z_id) {
3304789Sahrens 		cmp = -1;
3305789Sahrens 	} else if (sdzp->z_id > tdzp->z_id) {
3306789Sahrens 		cmp = 1;
3307789Sahrens 	} else {
33085331Samw 		/*
33095331Samw 		 * First compare the two name arguments without
33105331Samw 		 * considering any case folding.
33115331Samw 		 */
33125331Samw 		int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
33135331Samw 
33145331Samw 		cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
33155498Stimh 		ASSERT(error == 0 || !zfsvfs->z_utf8);
3316789Sahrens 		if (cmp == 0) {
3317789Sahrens 			/*
3318789Sahrens 			 * POSIX: "If the old argument and the new argument
3319789Sahrens 			 * both refer to links to the same existing file,
3320789Sahrens 			 * the rename() function shall return successfully
3321789Sahrens 			 * and perform no other action."
3322789Sahrens 			 */
3323789Sahrens 			ZFS_EXIT(zfsvfs);
3324789Sahrens 			return (0);
3325789Sahrens 		}
33265331Samw 		/*
33275331Samw 		 * If the file system is case-folding, then we may
33285331Samw 		 * have some more checking to do.  A case-folding file
33295331Samw 		 * system is either supporting mixed case sensitivity
33305331Samw 		 * access or is completely case-insensitive.  Note
33315331Samw 		 * that the file system is always case preserving.
33325331Samw 		 *
33335331Samw 		 * In mixed sensitivity mode case sensitive behavior
33345331Samw 		 * is the default.  FIGNORECASE must be used to
33355331Samw 		 * explicitly request case insensitive behavior.
33365331Samw 		 *
33375331Samw 		 * If the source and target names provided differ only
33385331Samw 		 * by case (e.g., a request to rename 'tim' to 'Tim'),
33395331Samw 		 * we will treat this as a special case in the
33405331Samw 		 * case-insensitive mode: as long as the source name
33415331Samw 		 * is an exact match, we will allow this to proceed as
33425331Samw 		 * a name-change request.
33435331Samw 		 */
33445498Stimh 		if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
33455498Stimh 		    (zfsvfs->z_case == ZFS_CASE_MIXED &&
33465498Stimh 		    flags & FIGNORECASE)) &&
33475331Samw 		    u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
33485331Samw 		    &error) == 0) {
33495331Samw 			/*
33505331Samw 			 * case preserving rename request, require exact
33515331Samw 			 * name matches
33525331Samw 			 */
33535331Samw 			zflg |= ZCIEXACT;
33545331Samw 			zflg &= ~ZCILOOK;
33555331Samw 		}
3356789Sahrens 	}
33575331Samw 
335811321SSanjeev.Bagewadi@Sun.COM 	/*
335911321SSanjeev.Bagewadi@Sun.COM 	 * If the source and destination directories are the same, we should
336011321SSanjeev.Bagewadi@Sun.COM 	 * grab the z_name_lock of that directory only once.
336111321SSanjeev.Bagewadi@Sun.COM 	 */
336211321SSanjeev.Bagewadi@Sun.COM 	if (sdzp == tdzp) {
336311321SSanjeev.Bagewadi@Sun.COM 		zflg |= ZHAVELOCK;
336411321SSanjeev.Bagewadi@Sun.COM 		rw_enter(&sdzp->z_name_lock, RW_READER);
336511321SSanjeev.Bagewadi@Sun.COM 	}
336611321SSanjeev.Bagewadi@Sun.COM 
3367789Sahrens 	if (cmp < 0) {
33685331Samw 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
33695331Samw 		    ZEXISTS | zflg, NULL, NULL);
33705331Samw 		terr = zfs_dirent_lock(&tdl,
33715331Samw 		    tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3372789Sahrens 	} else {
33735331Samw 		terr = zfs_dirent_lock(&tdl,
33745331Samw 		    tdzp, tnm, &tzp, zflg, NULL, NULL);
33755331Samw 		serr = zfs_dirent_lock(&sdl,
33765331Samw 		    sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
33775331Samw 		    NULL, NULL);
3378789Sahrens 	}
3379789Sahrens 
3380789Sahrens 	if (serr) {
3381789Sahrens 		/*
3382789Sahrens 		 * Source entry invalid or not there.
3383789Sahrens 		 */
3384789Sahrens 		if (!terr) {
3385789Sahrens 			zfs_dirent_unlock(tdl);
3386789Sahrens 			if (tzp)
3387789Sahrens 				VN_RELE(ZTOV(tzp));
3388789Sahrens 		}
338911321SSanjeev.Bagewadi@Sun.COM 
339011321SSanjeev.Bagewadi@Sun.COM 		if (sdzp == tdzp)
339111321SSanjeev.Bagewadi@Sun.COM 			rw_exit(&sdzp->z_name_lock);
339211321SSanjeev.Bagewadi@Sun.COM 
3393789Sahrens 		if (strcmp(snm, "..") == 0)
3394789Sahrens 			serr = EINVAL;
3395789Sahrens 		ZFS_EXIT(zfsvfs);
3396789Sahrens 		return (serr);
3397789Sahrens 	}
3398789Sahrens 	if (terr) {
3399789Sahrens 		zfs_dirent_unlock(sdl);
3400789Sahrens 		VN_RELE(ZTOV(szp));
340111321SSanjeev.Bagewadi@Sun.COM 
340211321SSanjeev.Bagewadi@Sun.COM 		if (sdzp == tdzp)
340311321SSanjeev.Bagewadi@Sun.COM 			rw_exit(&sdzp->z_name_lock);
340411321SSanjeev.Bagewadi@Sun.COM 
3405789Sahrens 		if (strcmp(tnm, "..") == 0)
3406789Sahrens 			terr = EINVAL;
3407789Sahrens 		ZFS_EXIT(zfsvfs);
3408789Sahrens 		return (terr);
3409789Sahrens 	}
3410789Sahrens 
3411789Sahrens 	/*
3412789Sahrens 	 * Must have write access at the source to remove the old entry
3413789Sahrens 	 * and write access at the target to create the new entry.
3414789Sahrens 	 * Note that if target and source are the same, this can be
3415789Sahrens 	 * done in a single check.
3416789Sahrens 	 */
3417789Sahrens 
3418789Sahrens 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3419789Sahrens 		goto out;
3420789Sahrens 
3421789Sahrens 	if (ZTOV(szp)->v_type == VDIR) {
3422789Sahrens 		/*
3423789Sahrens 		 * Check to make sure rename is valid.
3424789Sahrens 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3425789Sahrens 		 */
3426789Sahrens 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3427789Sahrens 			goto out;
3428789Sahrens 	}
3429789Sahrens 
3430789Sahrens 	/*
3431789Sahrens 	 * Does target exist?
3432789Sahrens 	 */
3433789Sahrens 	if (tzp) {
3434789Sahrens 		/*
3435789Sahrens 		 * Source and target must be the same type.
3436789Sahrens 		 */
3437789Sahrens 		if (ZTOV(szp)->v_type == VDIR) {
3438789Sahrens 			if (ZTOV(tzp)->v_type != VDIR) {
3439789Sahrens 				error = ENOTDIR;
3440789Sahrens 				goto out;
3441789Sahrens 			}
3442789Sahrens 		} else {
3443789Sahrens 			if (ZTOV(tzp)->v_type == VDIR) {
3444789Sahrens 				error = EISDIR;
3445789Sahrens 				goto out;
3446789Sahrens 			}
3447789Sahrens 		}
3448789Sahrens 		/*
3449789Sahrens 		 * POSIX dictates that when the source and target
3450789Sahrens 		 * entries refer to the same file object, rename
3451789Sahrens 		 * must do nothing and exit without error.
3452789Sahrens 		 */
3453789Sahrens 		if (szp->z_id == tzp->z_id) {
3454789Sahrens 			error = 0;
3455789Sahrens 			goto out;
3456789Sahrens 		}
3457789Sahrens 	}
3458789Sahrens 
34595331Samw 	vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3460789Sahrens 	if (tzp)
34615331Samw 		vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
34624863Spraks 
34634863Spraks 	/*
34644863Spraks 	 * notify the target directory if it is not the same
34654863Spraks 	 * as source directory.
34664863Spraks 	 */
34674863Spraks 	if (tdvp != sdvp) {
34685331Samw 		vnevent_rename_dest_dir(tdvp, ct);
34694863Spraks 	}
3470789Sahrens 
3471789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
347211935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
347311935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
34741544Seschrock 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
34751544Seschrock 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
347611935SMark.Shellenbaum@Sun.COM 	if (sdzp != tdzp) {
347711935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
347811935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, tdzp);
347911935SMark.Shellenbaum@Sun.COM 	}
348011935SMark.Shellenbaum@Sun.COM 	if (tzp) {
348111935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
348211935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, tzp);
348311935SMark.Shellenbaum@Sun.COM 	}
348411935SMark.Shellenbaum@Sun.COM 
348511935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, szp);
34863461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
34878227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3488789Sahrens 	if (error) {
3489789Sahrens 		if (zl != NULL)
3490789Sahrens 			zfs_rename_unlock(&zl);
3491789Sahrens 		zfs_dirent_unlock(sdl);
3492789Sahrens 		zfs_dirent_unlock(tdl);
349311321SSanjeev.Bagewadi@Sun.COM 
349411321SSanjeev.Bagewadi@Sun.COM 		if (sdzp == tdzp)
349511321SSanjeev.Bagewadi@Sun.COM 			rw_exit(&sdzp->z_name_lock);
349611321SSanjeev.Bagewadi@Sun.COM 
3497789Sahrens 		VN_RELE(ZTOV(szp));
3498789Sahrens 		if (tzp)
3499789Sahrens 			VN_RELE(ZTOV(tzp));
35008227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
35012113Sahrens 			dmu_tx_wait(tx);
35022113Sahrens 			dmu_tx_abort(tx);
3503789Sahrens 			goto top;
3504789Sahrens 		}
35052113Sahrens 		dmu_tx_abort(tx);
3506789Sahrens 		ZFS_EXIT(zfsvfs);
3507789Sahrens 		return (error);
3508789Sahrens 	}
3509789Sahrens 
3510789Sahrens 	if (tzp)	/* Attempt to remove the existing target */
35115331Samw 		error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3512789Sahrens 
3513789Sahrens 	if (error == 0) {
3514789Sahrens 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3515789Sahrens 		if (error == 0) {
351611935SMark.Shellenbaum@Sun.COM 			szp->z_pflags |= ZFS_AV_MODIFIED;
351711935SMark.Shellenbaum@Sun.COM 
351811935SMark.Shellenbaum@Sun.COM 			error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
351911935SMark.Shellenbaum@Sun.COM 			    (void *)&szp->z_pflags, sizeof (uint64_t), tx);
352011935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error, ==, 0);
35215331Samw 
3522789Sahrens 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
352311935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error, ==, 0);
35245331Samw 
35255331Samw 			zfs_log_rename(zilog, tx,
35265331Samw 			    TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0),
35275331Samw 			    sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
35286976Seschrock 
35296976Seschrock 			/* Update path information for the target vnode */
35306976Seschrock 			vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm));
3531789Sahrens 		}
3532789Sahrens 	}
3533789Sahrens 
3534789Sahrens 	dmu_tx_commit(tx);
3535789Sahrens out:
3536789Sahrens 	if (zl != NULL)
3537789Sahrens 		zfs_rename_unlock(&zl);
3538789Sahrens 
3539789Sahrens 	zfs_dirent_unlock(sdl);
3540789Sahrens 	zfs_dirent_unlock(tdl);
3541789Sahrens 
354211321SSanjeev.Bagewadi@Sun.COM 	if (sdzp == tdzp)
354311321SSanjeev.Bagewadi@Sun.COM 		rw_exit(&sdzp->z_name_lock);
354411321SSanjeev.Bagewadi@Sun.COM 
354511321SSanjeev.Bagewadi@Sun.COM 
3546789Sahrens 	VN_RELE(ZTOV(szp));
3547789Sahrens 	if (tzp)
3548789Sahrens 		VN_RELE(ZTOV(tzp));
3549789Sahrens 
3550789Sahrens 	ZFS_EXIT(zfsvfs);
3551789Sahrens 	return (error);
3552789Sahrens }
3553789Sahrens 
3554789Sahrens /*
3555789Sahrens  * Insert the indicated symbolic reference entry into the directory.
3556789Sahrens  *
3557789Sahrens  *	IN:	dvp	- Directory to contain new symbolic link.
3558789Sahrens  *		link	- Name for new symlink entry.
3559789Sahrens  *		vap	- Attributes of new entry.
3560789Sahrens  *		target	- Target path of new symlink.
3561789Sahrens  *		cr	- credentials of caller.
35625331Samw  *		ct	- caller context
35635331Samw  *		flags	- case flags
3564789Sahrens  *
3565789Sahrens  *	RETURN:	0 if success
3566789Sahrens  *		error code if failure
3567789Sahrens  *
3568789Sahrens  * Timestamps:
3569789Sahrens  *	dvp - ctime|mtime updated
3570789Sahrens  */
35715331Samw /*ARGSUSED*/
3572789Sahrens static int
35735331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
35745331Samw     caller_context_t *ct, int flags)
3575789Sahrens {
3576789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
3577789Sahrens 	zfs_dirlock_t	*dl;
3578789Sahrens 	dmu_tx_t	*tx;
3579789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
35805326Sek110237 	zilog_t		*zilog;
358111935SMark.Shellenbaum@Sun.COM 	uint64_t	len = strlen(link);
3582789Sahrens 	int		error;
35835331Samw 	int		zflg = ZNEW;
35849179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t	acl_ids;
35859179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
358611935SMark.Shellenbaum@Sun.COM 	uint64_t	txtype = TX_SYMLINK;
3587789Sahrens 
3588789Sahrens 	ASSERT(vap->va_type == VLNK);
3589789Sahrens 
35905367Sahrens 	ZFS_ENTER(zfsvfs);
35915367Sahrens 	ZFS_VERIFY_ZP(dzp);
35925326Sek110237 	zilog = zfsvfs->z_log;
35935331Samw 
35945498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
35955331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
35965331Samw 		ZFS_EXIT(zfsvfs);
35975331Samw 		return (EILSEQ);
35985331Samw 	}
35995331Samw 	if (flags & FIGNORECASE)
36005331Samw 		zflg |= ZCILOOK;
3601789Sahrens top:
36025331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3603789Sahrens 		ZFS_EXIT(zfsvfs);
3604789Sahrens 		return (error);
3605789Sahrens 	}
3606789Sahrens 
3607789Sahrens 	if (len > MAXPATHLEN) {
3608789Sahrens 		ZFS_EXIT(zfsvfs);
3609789Sahrens 		return (ENAMETOOLONG);
3610789Sahrens 	}
3611789Sahrens 
3612789Sahrens 	/*
3613789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3614789Sahrens 	 */
36155331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
36165331Samw 	if (error) {
3617789Sahrens 		ZFS_EXIT(zfsvfs);
3618789Sahrens 		return (error);
3619789Sahrens 	}
3620789Sahrens 
36219179SMark.Shellenbaum@Sun.COM 	VERIFY(0 == zfs_acl_ids_create(dzp, 0, vap, cr, NULL, &acl_ids));
36229396SMatthew.Ahrens@Sun.COM 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
36239396SMatthew.Ahrens@Sun.COM 		zfs_acl_ids_free(&acl_ids);
36249396SMatthew.Ahrens@Sun.COM 		zfs_dirent_unlock(dl);
36259396SMatthew.Ahrens@Sun.COM 		ZFS_EXIT(zfsvfs);
36269396SMatthew.Ahrens@Sun.COM 		return (EDQUOT);
36279396SMatthew.Ahrens@Sun.COM 	}
3628789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
36299179SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
3630789Sahrens 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
36311544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
363211935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
363311935SMark.Shellenbaum@Sun.COM 	    ZFS_SA_BASE_ATTR_SIZE + len);
363411935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
363511935SMark.Shellenbaum@Sun.COM 	if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
363611935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
363711935SMark.Shellenbaum@Sun.COM 		    acl_ids.z_aclp->z_acl_bytes);
363811935SMark.Shellenbaum@Sun.COM 	}
36399396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
36409396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
36418227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3642789Sahrens 	if (error) {
36439179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
3644789Sahrens 		zfs_dirent_unlock(dl);
36458227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
36462113Sahrens 			dmu_tx_wait(tx);
36472113Sahrens 			dmu_tx_abort(tx);
3648789Sahrens 			goto top;
3649789Sahrens 		}
36502113Sahrens 		dmu_tx_abort(tx);
3651789Sahrens 		ZFS_EXIT(zfsvfs);
3652789Sahrens 		return (error);
3653789Sahrens 	}
3654789Sahrens 
3655789Sahrens 	/*
3656789Sahrens 	 * Create a new object for the symlink.
365711935SMark.Shellenbaum@Sun.COM 	 * for version 4 ZPL datsets the symlink will be an SA attribute
3658789Sahrens 	 */
365911935SMark.Shellenbaum@Sun.COM 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
366011935SMark.Shellenbaum@Sun.COM 
366111935SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
366211935SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
366311935SMark.Shellenbaum@Sun.COM 
366411935SMark.Shellenbaum@Sun.COM 	if (zp->z_is_sa)
366511935SMark.Shellenbaum@Sun.COM 		error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
366611935SMark.Shellenbaum@Sun.COM 		    link, len, tx);
366711935SMark.Shellenbaum@Sun.COM 	else
366811935SMark.Shellenbaum@Sun.COM 		zfs_sa_symlink(zp, link, len, tx);
366911935SMark.Shellenbaum@Sun.COM 
367011935SMark.Shellenbaum@Sun.COM 	zp->z_size = len;
367111935SMark.Shellenbaum@Sun.COM 	(void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
367211935SMark.Shellenbaum@Sun.COM 	    &zp->z_size, sizeof (zp->z_size), tx);
3673789Sahrens 	/*
3674789Sahrens 	 * Insert the new object into the directory.
3675789Sahrens 	 */
3676789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
367711935SMark.Shellenbaum@Sun.COM 
367811935SMark.Shellenbaum@Sun.COM 	if (flags & FIGNORECASE)
367911935SMark.Shellenbaum@Sun.COM 		txtype |= TX_CI;
368011935SMark.Shellenbaum@Sun.COM 	zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
36819179SMark.Shellenbaum@Sun.COM 
36829179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_free(&acl_ids);
3683789Sahrens 
3684789Sahrens 	dmu_tx_commit(tx);
3685789Sahrens 
3686789Sahrens 	zfs_dirent_unlock(dl);
3687789Sahrens 
3688789Sahrens 	VN_RELE(ZTOV(zp));
3689789Sahrens 
3690789Sahrens 	ZFS_EXIT(zfsvfs);
3691789Sahrens 	return (error);
3692789Sahrens }
3693789Sahrens 
3694789Sahrens /*
3695789Sahrens  * Return, in the buffer contained in the provided uio structure,
3696789Sahrens  * the symbolic path referred to by vp.
3697789Sahrens  *
3698789Sahrens  *	IN:	vp	- vnode of symbolic link.
3699789Sahrens  *		uoip	- structure to contain the link path.
3700789Sahrens  *		cr	- credentials of caller.
37015331Samw  *		ct	- caller context
3702789Sahrens  *
3703789Sahrens  *	OUT:	uio	- structure to contain the link path.
3704789Sahrens  *
3705789Sahrens  *	RETURN:	0 if success
3706789Sahrens  *		error code if failure
3707789Sahrens  *
3708789Sahrens  * Timestamps:
3709789Sahrens  *	vp - atime updated
3710789Sahrens  */
3711789Sahrens /* ARGSUSED */
3712789Sahrens static int
37135331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3714789Sahrens {
3715789Sahrens 	znode_t		*zp = VTOZ(vp);
3716789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3717789Sahrens 	int		error;
3718789Sahrens 
37195367Sahrens 	ZFS_ENTER(zfsvfs);
37205367Sahrens 	ZFS_VERIFY_ZP(zp);
3721789Sahrens 
372211935SMark.Shellenbaum@Sun.COM 	if (zp->z_is_sa)
372311935SMark.Shellenbaum@Sun.COM 		error = sa_lookup_uio(zp->z_sa_hdl,
372411935SMark.Shellenbaum@Sun.COM 		    SA_ZPL_SYMLINK(zfsvfs), uio);
372511935SMark.Shellenbaum@Sun.COM 	else
372611935SMark.Shellenbaum@Sun.COM 		error = zfs_sa_readlink(zp, uio);
3727789Sahrens 
3728789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
372911935SMark.Shellenbaum@Sun.COM 
3730789Sahrens 	ZFS_EXIT(zfsvfs);
3731789Sahrens 	return (error);
3732789Sahrens }
3733789Sahrens 
3734789Sahrens /*
3735789Sahrens  * Insert a new entry into directory tdvp referencing svp.
3736789Sahrens  *
3737789Sahrens  *	IN:	tdvp	- Directory to contain new entry.
3738789Sahrens  *		svp	- vnode of new entry.
3739789Sahrens  *		name	- name of new entry.
3740789Sahrens  *		cr	- credentials of caller.
37415331Samw  *		ct	- caller context
3742789Sahrens  *
3743789Sahrens  *	RETURN:	0 if success
3744789Sahrens  *		error code if failure
3745789Sahrens  *
3746789Sahrens  * Timestamps:
3747789Sahrens  *	tdvp - ctime|mtime updated
3748789Sahrens  *	 svp - ctime updated
3749789Sahrens  */
3750789Sahrens /* ARGSUSED */
3751789Sahrens static int
37525331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
37535331Samw     caller_context_t *ct, int flags)
3754789Sahrens {
3755789Sahrens 	znode_t		*dzp = VTOZ(tdvp);
3756789Sahrens 	znode_t		*tzp, *szp;
3757789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
37585326Sek110237 	zilog_t		*zilog;
3759789Sahrens 	zfs_dirlock_t	*dl;
3760789Sahrens 	dmu_tx_t	*tx;
3761789Sahrens 	vnode_t		*realvp;
3762789Sahrens 	int		error;
37635331Samw 	int		zf = ZNEW;
376412079SMark.Shellenbaum@Sun.COM 	uint64_t	parent;
3765789Sahrens 
3766789Sahrens 	ASSERT(tdvp->v_type == VDIR);
3767789Sahrens 
37685367Sahrens 	ZFS_ENTER(zfsvfs);
37695367Sahrens 	ZFS_VERIFY_ZP(dzp);
37705326Sek110237 	zilog = zfsvfs->z_log;
3771789Sahrens 
37725331Samw 	if (VOP_REALVP(svp, &realvp, ct) == 0)
3773789Sahrens 		svp = realvp;
3774789Sahrens 
377512079SMark.Shellenbaum@Sun.COM 	/*
377612079SMark.Shellenbaum@Sun.COM 	 * POSIX dictates that we return EPERM here.
377712079SMark.Shellenbaum@Sun.COM 	 * Better choices include ENOTSUP or EISDIR.
377812079SMark.Shellenbaum@Sun.COM 	 */
377912079SMark.Shellenbaum@Sun.COM 	if (svp->v_type == VDIR) {
378012079SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
378112079SMark.Shellenbaum@Sun.COM 		return (EPERM);
378212079SMark.Shellenbaum@Sun.COM 	}
378312079SMark.Shellenbaum@Sun.COM 
378412079SMark.Shellenbaum@Sun.COM 	if (svp->v_vfsp != tdvp->v_vfsp || zfsctl_is_node(svp)) {
3785789Sahrens 		ZFS_EXIT(zfsvfs);
3786789Sahrens 		return (EXDEV);
3787789Sahrens 	}
378812079SMark.Shellenbaum@Sun.COM 
37895367Sahrens 	szp = VTOZ(svp);
37905367Sahrens 	ZFS_VERIFY_ZP(szp);
3791789Sahrens 
379212079SMark.Shellenbaum@Sun.COM 	/* Prevent links to .zfs/shares files */
379312079SMark.Shellenbaum@Sun.COM 
379412079SMark.Shellenbaum@Sun.COM 	if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
379512079SMark.Shellenbaum@Sun.COM 	    &parent, sizeof (uint64_t))) != 0) {
379612079SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
379712079SMark.Shellenbaum@Sun.COM 		return (error);
379812079SMark.Shellenbaum@Sun.COM 	}
379912079SMark.Shellenbaum@Sun.COM 	if (parent == zfsvfs->z_shares_dir) {
380012079SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
380112079SMark.Shellenbaum@Sun.COM 		return (EPERM);
380212079SMark.Shellenbaum@Sun.COM 	}
380312079SMark.Shellenbaum@Sun.COM 
38045498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name,
38055331Samw 	    strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
38065331Samw 		ZFS_EXIT(zfsvfs);
38075331Samw 		return (EILSEQ);
38085331Samw 	}
38095331Samw 	if (flags & FIGNORECASE)
38105331Samw 		zf |= ZCILOOK;
38115331Samw 
3812789Sahrens 	/*
3813789Sahrens 	 * We do not support links between attributes and non-attributes
3814789Sahrens 	 * because of the potential security risk of creating links
3815789Sahrens 	 * into "normal" file space in order to circumvent restrictions
3816789Sahrens 	 * imposed in attribute space.
3817789Sahrens 	 */
381811935SMark.Shellenbaum@Sun.COM 	if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
3819789Sahrens 		ZFS_EXIT(zfsvfs);
3820789Sahrens 		return (EINVAL);
3821789Sahrens 	}
3822789Sahrens 
3823789Sahrens 
382411935SMark.Shellenbaum@Sun.COM 	if (szp->z_uid != crgetuid(cr) &&
3825789Sahrens 	    secpolicy_basic_link(cr) != 0) {
3826789Sahrens 		ZFS_EXIT(zfsvfs);
3827789Sahrens 		return (EPERM);
3828789Sahrens 	}
3829789Sahrens 
38305331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3831789Sahrens 		ZFS_EXIT(zfsvfs);
3832789Sahrens 		return (error);
3833789Sahrens 	}
3834789Sahrens 
383512079SMark.Shellenbaum@Sun.COM top:
3836789Sahrens 	/*
3837789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3838789Sahrens 	 */
38395331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
38405331Samw 	if (error) {
3841789Sahrens 		ZFS_EXIT(zfsvfs);
3842789Sahrens 		return (error);
3843789Sahrens 	}
3844789Sahrens 
3845789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
384611935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
38471544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
384811935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, szp);
384911935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, dzp);
38508227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3851789Sahrens 	if (error) {
3852789Sahrens 		zfs_dirent_unlock(dl);
38538227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
38542113Sahrens 			dmu_tx_wait(tx);
38552113Sahrens 			dmu_tx_abort(tx);
3856789Sahrens 			goto top;
3857789Sahrens 		}
38582113Sahrens 		dmu_tx_abort(tx);
3859789Sahrens 		ZFS_EXIT(zfsvfs);
3860789Sahrens 		return (error);
3861789Sahrens 	}
3862789Sahrens 
3863789Sahrens 	error = zfs_link_create(dl, szp, tx, 0);
3864789Sahrens 
38655331Samw 	if (error == 0) {
38665331Samw 		uint64_t txtype = TX_LINK;
38675331Samw 		if (flags & FIGNORECASE)
38685331Samw 			txtype |= TX_CI;
38695331Samw 		zfs_log_link(zilog, tx, txtype, dzp, szp, name);
38705331Samw 	}
3871789Sahrens 
3872789Sahrens 	dmu_tx_commit(tx);
3873789Sahrens 
3874789Sahrens 	zfs_dirent_unlock(dl);
3875789Sahrens 
38764863Spraks 	if (error == 0) {
38775331Samw 		vnevent_link(svp, ct);
38784863Spraks 	}
38794863Spraks 
3880789Sahrens 	ZFS_EXIT(zfsvfs);
3881789Sahrens 	return (error);
3882789Sahrens }
3883789Sahrens 
3884789Sahrens /*
3885789Sahrens  * zfs_null_putapage() is used when the file system has been force
3886789Sahrens  * unmounted. It just drops the pages.
3887789Sahrens  */
3888789Sahrens /* ARGSUSED */
3889789Sahrens static int
3890789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3891789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3892789Sahrens {
3893789Sahrens 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
3894789Sahrens 	return (0);
3895789Sahrens }
3896789Sahrens 
38972688Smaybee /*
38982688Smaybee  * Push a page out to disk, klustering if possible.
38992688Smaybee  *
39002688Smaybee  *	IN:	vp	- file to push page to.
39012688Smaybee  *		pp	- page to push.
39022688Smaybee  *		flags	- additional flags.
39032688Smaybee  *		cr	- credentials of caller.
39042688Smaybee  *
39052688Smaybee  *	OUT:	offp	- start of range pushed.
39062688Smaybee  *		lenp	- len of range pushed.
39072688Smaybee  *
39082688Smaybee  *	RETURN:	0 if success
39092688Smaybee  *		error code if failure
39102688Smaybee  *
39112688Smaybee  * NOTE: callers must have locked the page to be pushed.  On
39122688Smaybee  * exit, the page (and all other pages in the kluster) must be
39132688Smaybee  * unlocked.
39142688Smaybee  */
3915789Sahrens /* ARGSUSED */
3916789Sahrens static int
3917789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3918789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3919789Sahrens {
3920789Sahrens 	znode_t		*zp = VTOZ(vp);
3921789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3922789Sahrens 	dmu_tx_t	*tx;
39232688Smaybee 	u_offset_t	off, koff;
39242688Smaybee 	size_t		len, klen;
3925789Sahrens 	int		err;
3926789Sahrens 
39272688Smaybee 	off = pp->p_offset;
39282688Smaybee 	len = PAGESIZE;
39292688Smaybee 	/*
39302688Smaybee 	 * If our blocksize is bigger than the page size, try to kluster
39318227SNeil.Perrin@Sun.COM 	 * multiple pages so that we write a full block (thus avoiding
39322688Smaybee 	 * a read-modify-write).
39332688Smaybee 	 */
393411935SMark.Shellenbaum@Sun.COM 	if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
39358636SMark.Maybee@Sun.COM 		klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
39368636SMark.Maybee@Sun.COM 		koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
393711935SMark.Shellenbaum@Sun.COM 		ASSERT(koff <= zp->z_size);
393811935SMark.Shellenbaum@Sun.COM 		if (koff + klen > zp->z_size)
393911935SMark.Shellenbaum@Sun.COM 			klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
39402688Smaybee 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
39412688Smaybee 	}
39422688Smaybee 	ASSERT3U(btop(len), ==, btopr(len));
39438636SMark.Maybee@Sun.COM 
39441819Smaybee 	/*
39451819Smaybee 	 * Can't push pages past end-of-file.
39461819Smaybee 	 */
394711935SMark.Shellenbaum@Sun.COM 	if (off >= zp->z_size) {
39484709Smaybee 		/* ignore all pages */
39492688Smaybee 		err = 0;
39502688Smaybee 		goto out;
395111935SMark.Shellenbaum@Sun.COM 	} else if (off + len > zp->z_size) {
395211935SMark.Shellenbaum@Sun.COM 		int npages = btopr(zp->z_size - off);
39532688Smaybee 		page_t *trunc;
39542688Smaybee 
39552688Smaybee 		page_list_break(&pp, &trunc, npages);
39564709Smaybee 		/* ignore pages past end of file */
39572688Smaybee 		if (trunc)
39584709Smaybee 			pvn_write_done(trunc, flags);
395911935SMark.Shellenbaum@Sun.COM 		len = zp->z_size - off;
39601819Smaybee 	}
39619396SMatthew.Ahrens@Sun.COM 
396211935SMark.Shellenbaum@Sun.COM 	if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
396311935SMark.Shellenbaum@Sun.COM 	    zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
39649396SMatthew.Ahrens@Sun.COM 		err = EDQUOT;
39659396SMatthew.Ahrens@Sun.COM 		goto out;
39669396SMatthew.Ahrens@Sun.COM 	}
39678636SMark.Maybee@Sun.COM top:
3968789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3969789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, off, len);
397011935SMark.Shellenbaum@Sun.COM 
397111935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
397211935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
39738227SNeil.Perrin@Sun.COM 	err = dmu_tx_assign(tx, TXG_NOWAIT);
3974789Sahrens 	if (err != 0) {
39758227SNeil.Perrin@Sun.COM 		if (err == ERESTART) {
39762113Sahrens 			dmu_tx_wait(tx);
39772113Sahrens 			dmu_tx_abort(tx);
3978789Sahrens 			goto top;
3979789Sahrens 		}
39802113Sahrens 		dmu_tx_abort(tx);
3981789Sahrens 		goto out;
3982789Sahrens 	}
3983789Sahrens 
39842688Smaybee 	if (zp->z_blksz <= PAGESIZE) {
39857315SJonathan.Adams@Sun.COM 		caddr_t va = zfs_map_page(pp, S_READ);
39862688Smaybee 		ASSERT3U(len, <=, PAGESIZE);
39872688Smaybee 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
39887315SJonathan.Adams@Sun.COM 		zfs_unmap_page(pp, va);
39892688Smaybee 	} else {
39902688Smaybee 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
39912688Smaybee 	}
39922688Smaybee 
39932688Smaybee 	if (err == 0) {
399411935SMark.Shellenbaum@Sun.COM 		uint64_t mtime[2], ctime[2];
399511935SMark.Shellenbaum@Sun.COM 		sa_bulk_attr_t bulk[2];
399611935SMark.Shellenbaum@Sun.COM 		int count = 0;
399711935SMark.Shellenbaum@Sun.COM 
399811935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
399911935SMark.Shellenbaum@Sun.COM 		    &mtime, 16);
400011935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
400111935SMark.Shellenbaum@Sun.COM 		    &ctime, 16);
400211935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
400311935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
40048636SMark.Maybee@Sun.COM 		zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
40052688Smaybee 	}
40069951SLin.Ling@Sun.COM 	dmu_tx_commit(tx);
40072688Smaybee 
40082688Smaybee out:
40094709Smaybee 	pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4010789Sahrens 	if (offp)
4011789Sahrens 		*offp = off;
4012789Sahrens 	if (lenp)
4013789Sahrens 		*lenp = len;
4014789Sahrens 
4015789Sahrens 	return (err);
4016789Sahrens }
4017789Sahrens 
4018789Sahrens /*
4019789Sahrens  * Copy the portion of the file indicated from pages into the file.
4020789Sahrens  * The pages are stored in a page list attached to the files vnode.
4021789Sahrens  *
4022789Sahrens  *	IN:	vp	- vnode of file to push page data to.
4023789Sahrens  *		off	- position in file to put data.
4024789Sahrens  *		len	- amount of data to write.
4025789Sahrens  *		flags	- flags to control the operation.
4026789Sahrens  *		cr	- credentials of caller.
40275331Samw  *		ct	- caller context.
4028789Sahrens  *
4029789Sahrens  *	RETURN:	0 if success
4030789Sahrens  *		error code if failure
4031789Sahrens  *
4032789Sahrens  * Timestamps:
4033789Sahrens  *	vp - ctime|mtime updated
4034789Sahrens  */
40355331Samw /*ARGSUSED*/
4036789Sahrens static int
40375331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
40385331Samw     caller_context_t *ct)
4039789Sahrens {
4040789Sahrens 	znode_t		*zp = VTOZ(vp);
4041789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4042789Sahrens 	page_t		*pp;
4043789Sahrens 	size_t		io_len;
4044789Sahrens 	u_offset_t	io_off;
40458636SMark.Maybee@Sun.COM 	uint_t		blksz;
40468636SMark.Maybee@Sun.COM 	rl_t		*rl;
4047789Sahrens 	int		error = 0;
4048789Sahrens 
40495367Sahrens 	ZFS_ENTER(zfsvfs);
40505367Sahrens 	ZFS_VERIFY_ZP(zp);
4051789Sahrens 
40528636SMark.Maybee@Sun.COM 	/*
40538636SMark.Maybee@Sun.COM 	 * Align this request to the file block size in case we kluster.
40548636SMark.Maybee@Sun.COM 	 * XXX - this can result in pretty aggresive locking, which can
40558636SMark.Maybee@Sun.COM 	 * impact simultanious read/write access.  One option might be
40568636SMark.Maybee@Sun.COM 	 * to break up long requests (len == 0) into block-by-block
40578636SMark.Maybee@Sun.COM 	 * operations to get narrower locking.
40588636SMark.Maybee@Sun.COM 	 */
40598636SMark.Maybee@Sun.COM 	blksz = zp->z_blksz;
40608636SMark.Maybee@Sun.COM 	if (ISP2(blksz))
40618636SMark.Maybee@Sun.COM 		io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
40628636SMark.Maybee@Sun.COM 	else
40638636SMark.Maybee@Sun.COM 		io_off = 0;
40648636SMark.Maybee@Sun.COM 	if (len > 0 && ISP2(blksz))
40659141SMark.Maybee@Sun.COM 		io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
40668636SMark.Maybee@Sun.COM 	else
40678636SMark.Maybee@Sun.COM 		io_len = 0;
40688636SMark.Maybee@Sun.COM 
40698636SMark.Maybee@Sun.COM 	if (io_len == 0) {
4070789Sahrens 		/*
40718636SMark.Maybee@Sun.COM 		 * Search the entire vp list for pages >= io_off.
4072789Sahrens 		 */
40738636SMark.Maybee@Sun.COM 		rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
40748636SMark.Maybee@Sun.COM 		error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
40751472Sperrin 		goto out;
4076789Sahrens 	}
40778636SMark.Maybee@Sun.COM 	rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
40788636SMark.Maybee@Sun.COM 
407911935SMark.Shellenbaum@Sun.COM 	if (off > zp->z_size) {
4080789Sahrens 		/* past end of file */
40818636SMark.Maybee@Sun.COM 		zfs_range_unlock(rl);
4082789Sahrens 		ZFS_EXIT(zfsvfs);
4083789Sahrens 		return (0);
4084789Sahrens 	}
4085789Sahrens 
408611935SMark.Shellenbaum@Sun.COM 	len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
40878636SMark.Maybee@Sun.COM 
40888636SMark.Maybee@Sun.COM 	for (off = io_off; io_off < off + len; io_off += io_len) {
4089789Sahrens 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
40901669Sperrin 			pp = page_lookup(vp, io_off,
40914339Sperrin 			    (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4092789Sahrens 		} else {
4093789Sahrens 			pp = page_lookup_nowait(vp, io_off,
40944339Sperrin 			    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4095789Sahrens 		}
4096789Sahrens 
4097789Sahrens 		if (pp != NULL && pvn_getdirty(pp, flags)) {
4098789Sahrens 			int err;
4099789Sahrens 
4100789Sahrens 			/*
4101789Sahrens 			 * Found a dirty page to push
4102789Sahrens 			 */
41031669Sperrin 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
41041669Sperrin 			if (err)
4105789Sahrens 				error = err;
4106789Sahrens 		} else {
4107789Sahrens 			io_len = PAGESIZE;
4108789Sahrens 		}
4109789Sahrens 	}
41101472Sperrin out:
41118636SMark.Maybee@Sun.COM 	zfs_range_unlock(rl);
41122638Sperrin 	if ((flags & B_ASYNC) == 0)
41132638Sperrin 		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
4114789Sahrens 	ZFS_EXIT(zfsvfs);
4115789Sahrens 	return (error);
4116789Sahrens }
4117789Sahrens 
41185331Samw /*ARGSUSED*/
4119789Sahrens void
41205331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4121789Sahrens {
4122789Sahrens 	znode_t	*zp = VTOZ(vp);
4123789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4124789Sahrens 	int error;
4125789Sahrens 
41265326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
412711935SMark.Shellenbaum@Sun.COM 	if (zp->z_sa_hdl == NULL) {
41285446Sahrens 		/*
41295642Smaybee 		 * The fs has been unmounted, or we did a
41305642Smaybee 		 * suspend/resume and this file no longer exists.
41315446Sahrens 		 */
4132789Sahrens 		if (vn_has_cached_data(vp)) {
4133789Sahrens 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
4134789Sahrens 			    B_INVAL, cr);
4135789Sahrens 		}
4136789Sahrens 
41371544Seschrock 		mutex_enter(&zp->z_lock);
413810369Schris.kirby@sun.com 		mutex_enter(&vp->v_lock);
413910369Schris.kirby@sun.com 		ASSERT(vp->v_count == 1);
414010369Schris.kirby@sun.com 		vp->v_count = 0;
414110369Schris.kirby@sun.com 		mutex_exit(&vp->v_lock);
41425446Sahrens 		mutex_exit(&zp->z_lock);
41435642Smaybee 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
41445446Sahrens 		zfs_znode_free(zp);
4145789Sahrens 		return;
4146789Sahrens 	}
4147789Sahrens 
4148789Sahrens 	/*
4149789Sahrens 	 * Attempt to push any data in the page cache.  If this fails
4150789Sahrens 	 * we will get kicked out later in zfs_zinactive().
4151789Sahrens 	 */
41521298Sperrin 	if (vn_has_cached_data(vp)) {
41531298Sperrin 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
41541298Sperrin 		    cr);
41551298Sperrin 	}
4156789Sahrens 
41573461Sahrens 	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4158789Sahrens 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4159789Sahrens 
416011935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
416111935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, zp);
4162789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
4163789Sahrens 		if (error) {
4164789Sahrens 			dmu_tx_abort(tx);
4165789Sahrens 		} else {
4166789Sahrens 			mutex_enter(&zp->z_lock);
416711935SMark.Shellenbaum@Sun.COM 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
416811935SMark.Shellenbaum@Sun.COM 			    (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4169789Sahrens 			zp->z_atime_dirty = 0;
4170789Sahrens 			mutex_exit(&zp->z_lock);
4171789Sahrens 			dmu_tx_commit(tx);
4172789Sahrens 		}
4173789Sahrens 	}
4174789Sahrens 
4175789Sahrens 	zfs_zinactive(zp);
41765326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
4177789Sahrens }
4178789Sahrens 
4179789Sahrens /*
4180789Sahrens  * Bounds-check the seek operation.
4181789Sahrens  *
4182789Sahrens  *	IN:	vp	- vnode seeking within
4183789Sahrens  *		ooff	- old file offset
4184789Sahrens  *		noffp	- pointer to new file offset
41855331Samw  *		ct	- caller context
4186789Sahrens  *
4187789Sahrens  *	RETURN:	0 if success
4188789Sahrens  *		EINVAL if new offset invalid
4189789Sahrens  */
4190789Sahrens /* ARGSUSED */
4191789Sahrens static int
41925331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
41935331Samw     caller_context_t *ct)
4194789Sahrens {
4195789Sahrens 	if (vp->v_type == VDIR)
4196789Sahrens 		return (0);
4197789Sahrens 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4198789Sahrens }
4199789Sahrens 
4200789Sahrens /*
4201789Sahrens  * Pre-filter the generic locking function to trap attempts to place
4202789Sahrens  * a mandatory lock on a memory mapped file.
4203789Sahrens  */
4204789Sahrens static int
4205789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
42065331Samw     flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4207789Sahrens {
4208789Sahrens 	znode_t *zp = VTOZ(vp);
4209789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4210789Sahrens 
42115367Sahrens 	ZFS_ENTER(zfsvfs);
42125367Sahrens 	ZFS_VERIFY_ZP(zp);
4213789Sahrens 
4214789Sahrens 	/*
42151544Seschrock 	 * We are following the UFS semantics with respect to mapcnt
42161544Seschrock 	 * here: If we see that the file is mapped already, then we will
42171544Seschrock 	 * return an error, but we don't worry about races between this
42181544Seschrock 	 * function and zfs_map().
4219789Sahrens 	 */
422011935SMark.Shellenbaum@Sun.COM 	if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4221789Sahrens 		ZFS_EXIT(zfsvfs);
4222789Sahrens 		return (EAGAIN);
4223789Sahrens 	}
4224789Sahrens 	ZFS_EXIT(zfsvfs);
422510896SMark.Shellenbaum@Sun.COM 	return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4226789Sahrens }
4227789Sahrens 
4228789Sahrens /*
4229789Sahrens  * If we can't find a page in the cache, we will create a new page
4230789Sahrens  * and fill it with file data.  For efficiency, we may try to fill
42318636SMark.Maybee@Sun.COM  * multiple pages at once (klustering) to fill up the supplied page
42329265SMark.Maybee@Sun.COM  * list.  Note that the pages to be filled are held with an exclusive
42339265SMark.Maybee@Sun.COM  * lock to prevent access by other threads while they are being filled.
4234789Sahrens  */
4235789Sahrens static int
4236789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4237789Sahrens     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4238789Sahrens {
4239789Sahrens 	znode_t *zp = VTOZ(vp);
4240789Sahrens 	page_t *pp, *cur_pp;
4241789Sahrens 	objset_t *os = zp->z_zfsvfs->z_os;
4242789Sahrens 	u_offset_t io_off, total;
4243789Sahrens 	size_t io_len;
4244789Sahrens 	int err;
4245789Sahrens 
42462688Smaybee 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
42478636SMark.Maybee@Sun.COM 		/*
42488636SMark.Maybee@Sun.COM 		 * We only have a single page, don't bother klustering
42498636SMark.Maybee@Sun.COM 		 */
4250789Sahrens 		io_off = off;
4251789Sahrens 		io_len = PAGESIZE;
42529265SMark.Maybee@Sun.COM 		pp = page_create_va(vp, io_off, io_len,
42539265SMark.Maybee@Sun.COM 		    PG_EXCL | PG_WAIT, seg, addr);
4254789Sahrens 	} else {
4255789Sahrens 		/*
42568636SMark.Maybee@Sun.COM 		 * Try to find enough pages to fill the page list
4257789Sahrens 		 */
4258789Sahrens 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
42598636SMark.Maybee@Sun.COM 		    &io_len, off, plsz, 0);
4260789Sahrens 	}
4261789Sahrens 	if (pp == NULL) {
4262789Sahrens 		/*
42638636SMark.Maybee@Sun.COM 		 * The page already exists, nothing to do here.
4264789Sahrens 		 */
4265789Sahrens 		*pl = NULL;
4266789Sahrens 		return (0);
4267789Sahrens 	}
4268789Sahrens 
4269789Sahrens 	/*
4270789Sahrens 	 * Fill the pages in the kluster.
4271789Sahrens 	 */
4272789Sahrens 	cur_pp = pp;
4273789Sahrens 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
42748636SMark.Maybee@Sun.COM 		caddr_t va;
42758636SMark.Maybee@Sun.COM 
42762688Smaybee 		ASSERT3U(io_off, ==, cur_pp->p_offset);
42777315SJonathan.Adams@Sun.COM 		va = zfs_map_page(cur_pp, S_WRITE);
42789512SNeil.Perrin@Sun.COM 		err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
42799512SNeil.Perrin@Sun.COM 		    DMU_READ_PREFETCH);
42807315SJonathan.Adams@Sun.COM 		zfs_unmap_page(cur_pp, va);
4281789Sahrens 		if (err) {
4282789Sahrens 			/* On error, toss the entire kluster */
4283789Sahrens 			pvn_read_done(pp, B_ERROR);
42847294Sperrin 			/* convert checksum errors into IO errors */
42857294Sperrin 			if (err == ECKSUM)
42867294Sperrin 				err = EIO;
4287789Sahrens 			return (err);
4288789Sahrens 		}
4289789Sahrens 		cur_pp = cur_pp->p_next;
4290789Sahrens 	}
42918636SMark.Maybee@Sun.COM 
4292789Sahrens 	/*
42938636SMark.Maybee@Sun.COM 	 * Fill in the page list array from the kluster starting
42948636SMark.Maybee@Sun.COM 	 * from the desired offset `off'.
4295789Sahrens 	 * NOTE: the page list will always be null terminated.
4296789Sahrens 	 */
4297789Sahrens 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
42988636SMark.Maybee@Sun.COM 	ASSERT(pl == NULL || (*pl)->p_offset == off);
4299789Sahrens 
4300789Sahrens 	return (0);
4301789Sahrens }
4302789Sahrens 
4303789Sahrens /*
4304789Sahrens  * Return pointers to the pages for the file region [off, off + len]
4305789Sahrens  * in the pl array.  If plsz is greater than len, this function may
43068636SMark.Maybee@Sun.COM  * also return page pointers from after the specified region
43078636SMark.Maybee@Sun.COM  * (i.e. the region [off, off + plsz]).  These additional pages are
43088636SMark.Maybee@Sun.COM  * only returned if they are already in the cache, or were created as
43098636SMark.Maybee@Sun.COM  * part of a klustered read.
4310789Sahrens  *
4311789Sahrens  *	IN:	vp	- vnode of file to get data from.
4312789Sahrens  *		off	- position in file to get data from.
4313789Sahrens  *		len	- amount of data to retrieve.
4314789Sahrens  *		plsz	- length of provided page list.
4315789Sahrens  *		seg	- segment to obtain pages for.
4316789Sahrens  *		addr	- virtual address of fault.
4317789Sahrens  *		rw	- mode of created pages.
4318789Sahrens  *		cr	- credentials of caller.
43195331Samw  *		ct	- caller context.
4320789Sahrens  *
4321789Sahrens  *	OUT:	protp	- protection mode of created pages.
4322789Sahrens  *		pl	- list of pages created.
4323789Sahrens  *
4324789Sahrens  *	RETURN:	0 if success
4325789Sahrens  *		error code if failure
4326789Sahrens  *
4327789Sahrens  * Timestamps:
4328789Sahrens  *	vp - atime updated
4329789Sahrens  */
4330789Sahrens /* ARGSUSED */
4331789Sahrens static int
4332789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4333789Sahrens 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
43345331Samw 	enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4335789Sahrens {
4336789Sahrens 	znode_t		*zp = VTOZ(vp);
4337789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
43388636SMark.Maybee@Sun.COM 	page_t		**pl0 = pl;
43398636SMark.Maybee@Sun.COM 	int		err = 0;
43408636SMark.Maybee@Sun.COM 
43418636SMark.Maybee@Sun.COM 	/* we do our own caching, faultahead is unnecessary */
43428636SMark.Maybee@Sun.COM 	if (pl == NULL)
43438636SMark.Maybee@Sun.COM 		return (0);
43448636SMark.Maybee@Sun.COM 	else if (len > plsz)
43458636SMark.Maybee@Sun.COM 		len = plsz;
43468681SMark.Maybee@Sun.COM 	else
43478681SMark.Maybee@Sun.COM 		len = P2ROUNDUP(len, PAGESIZE);
43488636SMark.Maybee@Sun.COM 	ASSERT(plsz >= len);
4349789Sahrens 
43505367Sahrens 	ZFS_ENTER(zfsvfs);
43515367Sahrens 	ZFS_VERIFY_ZP(zp);
4352789Sahrens 
4353789Sahrens 	if (protp)
4354789Sahrens 		*protp = PROT_ALL;
4355789Sahrens 
4356789Sahrens 	/*
43579265SMark.Maybee@Sun.COM 	 * Loop through the requested range [off, off + len) looking
4358789Sahrens 	 * for pages.  If we don't find a page, we will need to create
4359789Sahrens 	 * a new page and fill it with data from the file.
4360789Sahrens 	 */
4361789Sahrens 	while (len > 0) {
43628636SMark.Maybee@Sun.COM 		if (*pl = page_lookup(vp, off, SE_SHARED))
43638636SMark.Maybee@Sun.COM 			*(pl+1) = NULL;
43648636SMark.Maybee@Sun.COM 		else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
43658636SMark.Maybee@Sun.COM 			goto out;
43668636SMark.Maybee@Sun.COM 		while (*pl) {
43678636SMark.Maybee@Sun.COM 			ASSERT3U((*pl)->p_offset, ==, off);
4368789Sahrens 			off += PAGESIZE;
4369789Sahrens 			addr += PAGESIZE;
43708681SMark.Maybee@Sun.COM 			if (len > 0) {
43718681SMark.Maybee@Sun.COM 				ASSERT3U(len, >=, PAGESIZE);
43728636SMark.Maybee@Sun.COM 				len -= PAGESIZE;
43738681SMark.Maybee@Sun.COM 			}
43748636SMark.Maybee@Sun.COM 			ASSERT3U(plsz, >=, PAGESIZE);
4375789Sahrens 			plsz -= PAGESIZE;
43768636SMark.Maybee@Sun.COM 			pl++;
4377789Sahrens 		}
4378789Sahrens 	}
4379789Sahrens 
4380789Sahrens 	/*
4381789Sahrens 	 * Fill out the page array with any pages already in the cache.
4382789Sahrens 	 */
43838636SMark.Maybee@Sun.COM 	while (plsz > 0 &&
43848636SMark.Maybee@Sun.COM 	    (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
43858636SMark.Maybee@Sun.COM 			off += PAGESIZE;
43868636SMark.Maybee@Sun.COM 			plsz -= PAGESIZE;
4387789Sahrens 	}
4388789Sahrens out:
43892752Sperrin 	if (err) {
43902752Sperrin 		/*
43912752Sperrin 		 * Release any pages we have previously locked.
43922752Sperrin 		 */
43932752Sperrin 		while (pl > pl0)
43942752Sperrin 			page_unlock(*--pl);
43958636SMark.Maybee@Sun.COM 	} else {
43968636SMark.Maybee@Sun.COM 		ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
43972752Sperrin 	}
43982752Sperrin 
4399789Sahrens 	*pl = NULL;
4400789Sahrens 
4401789Sahrens 	ZFS_EXIT(zfsvfs);
4402789Sahrens 	return (err);
4403789Sahrens }
4404789Sahrens 
44051544Seschrock /*
44061544Seschrock  * Request a memory map for a section of a file.  This code interacts
44071544Seschrock  * with common code and the VM system as follows:
44081544Seschrock  *
44091544Seschrock  *	common code calls mmap(), which ends up in smmap_common()
44101544Seschrock  *
44111544Seschrock  *	this calls VOP_MAP(), which takes you into (say) zfs
44121544Seschrock  *
44131544Seschrock  *	zfs_map() calls as_map(), passing segvn_create() as the callback
44141544Seschrock  *
44151544Seschrock  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
44161544Seschrock  *
44171544Seschrock  *	zfs_addmap() updates z_mapcnt
44181544Seschrock  */
44195331Samw /*ARGSUSED*/
4420789Sahrens static int
4421789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
44225331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
44235331Samw     caller_context_t *ct)
4424789Sahrens {
4425789Sahrens 	znode_t *zp = VTOZ(vp);
4426789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4427789Sahrens 	segvn_crargs_t	vn_a;
4428789Sahrens 	int		error;
4429789Sahrens 
44305929Smarks 	ZFS_ENTER(zfsvfs);
44315929Smarks 	ZFS_VERIFY_ZP(zp);
44325929Smarks 
443311935SMark.Shellenbaum@Sun.COM 	if ((prot & PROT_WRITE) && (zp->z_pflags &
443411935SMark.Shellenbaum@Sun.COM 	    (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
44355929Smarks 		ZFS_EXIT(zfsvfs);
44365331Samw 		return (EPERM);
44375929Smarks 	}
44385929Smarks 
44395929Smarks 	if ((prot & (PROT_READ | PROT_EXEC)) &&
444011935SMark.Shellenbaum@Sun.COM 	    (zp->z_pflags & ZFS_AV_QUARANTINED)) {
44415929Smarks 		ZFS_EXIT(zfsvfs);
44425929Smarks 		return (EACCES);
44435929Smarks 	}
4444789Sahrens 
4445789Sahrens 	if (vp->v_flag & VNOMAP) {
4446789Sahrens 		ZFS_EXIT(zfsvfs);
4447789Sahrens 		return (ENOSYS);
4448789Sahrens 	}
4449789Sahrens 
4450789Sahrens 	if (off < 0 || len > MAXOFFSET_T - off) {
4451789Sahrens 		ZFS_EXIT(zfsvfs);
4452789Sahrens 		return (ENXIO);
4453789Sahrens 	}
4454789Sahrens 
4455789Sahrens 	if (vp->v_type != VREG) {
4456789Sahrens 		ZFS_EXIT(zfsvfs);
4457789Sahrens 		return (ENODEV);
4458789Sahrens 	}
4459789Sahrens 
4460789Sahrens 	/*
4461789Sahrens 	 * If file is locked, disallow mapping.
4462789Sahrens 	 */
446311935SMark.Shellenbaum@Sun.COM 	if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
44641544Seschrock 		ZFS_EXIT(zfsvfs);
44651544Seschrock 		return (EAGAIN);
4466789Sahrens 	}
4467789Sahrens 
4468789Sahrens 	as_rangelock(as);
44696036Smec 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
44706036Smec 	if (error != 0) {
44716036Smec 		as_rangeunlock(as);
44726036Smec 		ZFS_EXIT(zfsvfs);
44736036Smec 		return (error);
4474789Sahrens 	}
4475789Sahrens 
4476789Sahrens 	vn_a.vp = vp;
4477789Sahrens 	vn_a.offset = (u_offset_t)off;
4478789Sahrens 	vn_a.type = flags & MAP_TYPE;
4479789Sahrens 	vn_a.prot = prot;
4480789Sahrens 	vn_a.maxprot = maxprot;
4481789Sahrens 	vn_a.cred = cr;
4482789Sahrens 	vn_a.amp = NULL;
4483789Sahrens 	vn_a.flags = flags & ~MAP_TYPE;
44841417Skchow 	vn_a.szc = 0;
44851417Skchow 	vn_a.lgrp_mem_policy_flags = 0;
4486789Sahrens 
4487789Sahrens 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
4488789Sahrens 
4489789Sahrens 	as_rangeunlock(as);
4490789Sahrens 	ZFS_EXIT(zfsvfs);
4491789Sahrens 	return (error);
4492789Sahrens }
4493789Sahrens 
4494789Sahrens /* ARGSUSED */
4495789Sahrens static int
4496789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
44975331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
44985331Samw     caller_context_t *ct)
4499789Sahrens {
45001544Seschrock 	uint64_t pages = btopr(len);
45011544Seschrock 
45021544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4503789Sahrens 	return (0);
4504789Sahrens }
4505789Sahrens 
45061773Seschrock /*
45071773Seschrock  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
45081773Seschrock  * more accurate mtime for the associated file.  Since we don't have a way of
45091773Seschrock  * detecting when the data was actually modified, we have to resort to
45101773Seschrock  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
45111773Seschrock  * last page is pushed.  The problem occurs when the msync() call is omitted,
45121773Seschrock  * which by far the most common case:
45131773Seschrock  *
45141773Seschrock  * 	open()
45151773Seschrock  * 	mmap()
45161773Seschrock  * 	<modify memory>
45171773Seschrock  * 	munmap()
45181773Seschrock  * 	close()
45191773Seschrock  * 	<time lapse>
45201773Seschrock  * 	putpage() via fsflush
45211773Seschrock  *
45221773Seschrock  * If we wait until fsflush to come along, we can have a modification time that
45231773Seschrock  * is some arbitrary point in the future.  In order to prevent this in the
45241773Seschrock  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
45251773Seschrock  * torn down.
45261773Seschrock  */
4527789Sahrens /* ARGSUSED */
4528789Sahrens static int
4529789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
45305331Samw     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
45315331Samw     caller_context_t *ct)
4532789Sahrens {
45331544Seschrock 	uint64_t pages = btopr(len);
45341544Seschrock 
45351544Seschrock 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
45361544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
45371773Seschrock 
45381773Seschrock 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
45391773Seschrock 	    vn_has_cached_data(vp))
45405331Samw 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
45411773Seschrock 
4542789Sahrens 	return (0);
4543789Sahrens }
4544789Sahrens 
4545789Sahrens /*
4546789Sahrens  * Free or allocate space in a file.  Currently, this function only
4547789Sahrens  * supports the `F_FREESP' command.  However, this command is somewhat
4548789Sahrens  * misnamed, as its functionality includes the ability to allocate as
4549789Sahrens  * well as free space.
4550789Sahrens  *
4551789Sahrens  *	IN:	vp	- vnode of file to free data in.
4552789Sahrens  *		cmd	- action to take (only F_FREESP supported).
4553789Sahrens  *		bfp	- section of file to free/alloc.
4554789Sahrens  *		flag	- current file open mode flags.
4555789Sahrens  *		offset	- current file offset.
4556789Sahrens  *		cr	- credentials of caller [UNUSED].
45575331Samw  *		ct	- caller context.
4558789Sahrens  *
4559789Sahrens  *	RETURN:	0 if success
4560789Sahrens  *		error code if failure
4561789Sahrens  *
4562789Sahrens  * Timestamps:
4563789Sahrens  *	vp - ctime|mtime updated
4564789Sahrens  */
4565789Sahrens /* ARGSUSED */
4566789Sahrens static int
4567789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4568789Sahrens     offset_t offset, cred_t *cr, caller_context_t *ct)
4569789Sahrens {
4570789Sahrens 	znode_t		*zp = VTOZ(vp);
4571789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4572789Sahrens 	uint64_t	off, len;
4573789Sahrens 	int		error;
4574789Sahrens 
45755367Sahrens 	ZFS_ENTER(zfsvfs);
45765367Sahrens 	ZFS_VERIFY_ZP(zp);
4577789Sahrens 
4578789Sahrens 	if (cmd != F_FREESP) {
4579789Sahrens 		ZFS_EXIT(zfsvfs);
4580789Sahrens 		return (EINVAL);
4581789Sahrens 	}
4582789Sahrens 
4583789Sahrens 	if (error = convoff(vp, bfp, 0, offset)) {
4584789Sahrens 		ZFS_EXIT(zfsvfs);
4585789Sahrens 		return (error);
4586789Sahrens 	}
4587789Sahrens 
4588789Sahrens 	if (bfp->l_len < 0) {
4589789Sahrens 		ZFS_EXIT(zfsvfs);
4590789Sahrens 		return (EINVAL);
4591789Sahrens 	}
4592789Sahrens 
4593789Sahrens 	off = bfp->l_start;
45941669Sperrin 	len = bfp->l_len; /* 0 means from off to end of file */
45951878Smaybee 
45966992Smaybee 	error = zfs_freesp(zp, off, len, flag, TRUE);
4597789Sahrens 
4598789Sahrens 	ZFS_EXIT(zfsvfs);
4599789Sahrens 	return (error);
4600789Sahrens }
4601789Sahrens 
46025331Samw /*ARGSUSED*/
4603789Sahrens static int
46045331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4605789Sahrens {
4606789Sahrens 	znode_t		*zp = VTOZ(vp);
4607789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
46085326Sek110237 	uint32_t	gen;
460911935SMark.Shellenbaum@Sun.COM 	uint64_t	gen64;
4610789Sahrens 	uint64_t	object = zp->z_id;
4611789Sahrens 	zfid_short_t	*zfid;
461211935SMark.Shellenbaum@Sun.COM 	int		size, i, error;
4613789Sahrens 
46145367Sahrens 	ZFS_ENTER(zfsvfs);
46155367Sahrens 	ZFS_VERIFY_ZP(zp);
461611935SMark.Shellenbaum@Sun.COM 
461711935SMark.Shellenbaum@Sun.COM 	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
461812218SMark.Shellenbaum@Sun.COM 	    &gen64, sizeof (uint64_t))) != 0) {
461912218SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
462011935SMark.Shellenbaum@Sun.COM 		return (error);
462112218SMark.Shellenbaum@Sun.COM 	}
462211935SMark.Shellenbaum@Sun.COM 
462311935SMark.Shellenbaum@Sun.COM 	gen = (uint32_t)gen64;
4624789Sahrens 
4625789Sahrens 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4626789Sahrens 	if (fidp->fid_len < size) {
4627789Sahrens 		fidp->fid_len = size;
46281512Sek110237 		ZFS_EXIT(zfsvfs);
4629789Sahrens 		return (ENOSPC);
4630789Sahrens 	}
4631789Sahrens 
4632789Sahrens 	zfid = (zfid_short_t *)fidp;
4633789Sahrens 
4634789Sahrens 	zfid->zf_len = size;
4635789Sahrens 
4636789Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
4637789Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4638789Sahrens 
4639789Sahrens 	/* Must have a non-zero generation number to distinguish from .zfs */
4640789Sahrens 	if (gen == 0)
4641789Sahrens 		gen = 1;
4642789Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
4643789Sahrens 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4644789Sahrens 
4645789Sahrens 	if (size == LONG_FID_LEN) {
4646789Sahrens 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
4647789Sahrens 		zfid_long_t	*zlfid;
4648789Sahrens 
4649789Sahrens 		zlfid = (zfid_long_t *)fidp;
4650789Sahrens 
4651789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4652789Sahrens 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4653789Sahrens 
4654789Sahrens 		/* XXX - this should be the generation number for the objset */
4655789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4656789Sahrens 			zlfid->zf_setgen[i] = 0;
4657789Sahrens 	}
4658789Sahrens 
4659789Sahrens 	ZFS_EXIT(zfsvfs);
4660789Sahrens 	return (0);
4661789Sahrens }
4662789Sahrens 
4663789Sahrens static int
46645331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
46655331Samw     caller_context_t *ct)
4666789Sahrens {
4667789Sahrens 	znode_t		*zp, *xzp;
4668789Sahrens 	zfsvfs_t	*zfsvfs;
4669789Sahrens 	zfs_dirlock_t	*dl;
4670789Sahrens 	int		error;
4671789Sahrens 
4672789Sahrens 	switch (cmd) {
4673789Sahrens 	case _PC_LINK_MAX:
4674789Sahrens 		*valp = ULONG_MAX;
4675789Sahrens 		return (0);
4676789Sahrens 
4677789Sahrens 	case _PC_FILESIZEBITS:
4678789Sahrens 		*valp = 64;
4679789Sahrens 		return (0);
4680789Sahrens 
4681789Sahrens 	case _PC_XATTR_EXISTS:
4682789Sahrens 		zp = VTOZ(vp);
4683789Sahrens 		zfsvfs = zp->z_zfsvfs;
46845367Sahrens 		ZFS_ENTER(zfsvfs);
46855367Sahrens 		ZFS_VERIFY_ZP(zp);
4686789Sahrens 		*valp = 0;
4687789Sahrens 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
46885331Samw 		    ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4689789Sahrens 		if (error == 0) {
4690789Sahrens 			zfs_dirent_unlock(dl);
4691789Sahrens 			if (!zfs_dirempty(xzp))
4692789Sahrens 				*valp = 1;
4693789Sahrens 			VN_RELE(ZTOV(xzp));
4694789Sahrens 		} else if (error == ENOENT) {
4695789Sahrens 			/*
4696789Sahrens 			 * If there aren't extended attributes, it's the
4697789Sahrens 			 * same as having zero of them.
4698789Sahrens 			 */
4699789Sahrens 			error = 0;
4700789Sahrens 		}
4701789Sahrens 		ZFS_EXIT(zfsvfs);
4702789Sahrens 		return (error);
4703789Sahrens 
47045331Samw 	case _PC_SATTR_ENABLED:
47055331Samw 	case _PC_SATTR_EXISTS:
47067757SJanice.Chang@Sun.COM 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
47075331Samw 		    (vp->v_type == VREG || vp->v_type == VDIR);
47085331Samw 		return (0);
47095331Samw 
47109749STim.Haley@Sun.COM 	case _PC_ACCESS_FILTERING:
47119749STim.Haley@Sun.COM 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
47129749STim.Haley@Sun.COM 		    vp->v_type == VDIR;
47139749STim.Haley@Sun.COM 		return (0);
47149749STim.Haley@Sun.COM 
4715789Sahrens 	case _PC_ACL_ENABLED:
4716789Sahrens 		*valp = _ACL_ACE_ENABLED;
4717789Sahrens 		return (0);
4718789Sahrens 
4719789Sahrens 	case _PC_MIN_HOLE_SIZE:
4720789Sahrens 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
4721789Sahrens 		return (0);
4722789Sahrens 
472310440SRoger.Faulkner@Sun.COM 	case _PC_TIMESTAMP_RESOLUTION:
472410440SRoger.Faulkner@Sun.COM 		/* nanosecond timestamp resolution */
472510440SRoger.Faulkner@Sun.COM 		*valp = 1L;
472610440SRoger.Faulkner@Sun.COM 		return (0);
472710440SRoger.Faulkner@Sun.COM 
4728789Sahrens 	default:
47295331Samw 		return (fs_pathconf(vp, cmd, valp, cr, ct));
4730789Sahrens 	}
4731789Sahrens }
4732789Sahrens 
4733789Sahrens /*ARGSUSED*/
4734789Sahrens static int
47355331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
47365331Samw     caller_context_t *ct)
4737789Sahrens {
4738789Sahrens 	znode_t *zp = VTOZ(vp);
4739789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4740789Sahrens 	int error;
47415331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4742789Sahrens 
47435367Sahrens 	ZFS_ENTER(zfsvfs);
47445367Sahrens 	ZFS_VERIFY_ZP(zp);
47455331Samw 	error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4746789Sahrens 	ZFS_EXIT(zfsvfs);
4747789Sahrens 
4748789Sahrens 	return (error);
4749789Sahrens }
4750789Sahrens 
4751789Sahrens /*ARGSUSED*/
4752789Sahrens static int
47535331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
47545331Samw     caller_context_t *ct)
4755789Sahrens {
4756789Sahrens 	znode_t *zp = VTOZ(vp);
4757789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4758789Sahrens 	int error;
47595331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4760789Sahrens 
47615367Sahrens 	ZFS_ENTER(zfsvfs);
47625367Sahrens 	ZFS_VERIFY_ZP(zp);
47635331Samw 	error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4764789Sahrens 	ZFS_EXIT(zfsvfs);
4765789Sahrens 	return (error);
4766789Sahrens }
4767789Sahrens 
4768789Sahrens /*
476911539SChunli.Zhang@Sun.COM  * Tunable, both must be a power of 2.
477011539SChunli.Zhang@Sun.COM  *
477111539SChunli.Zhang@Sun.COM  * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
477211539SChunli.Zhang@Sun.COM  * zcr_blksz_max: if set to less than the file block size, allow loaning out of
477311539SChunli.Zhang@Sun.COM  *                an arcbuf for a partial block read
477411539SChunli.Zhang@Sun.COM  */
477511539SChunli.Zhang@Sun.COM int zcr_blksz_min = (1 << 10);	/* 1K */
477611539SChunli.Zhang@Sun.COM int zcr_blksz_max = (1 << 17);	/* 128K */
477711539SChunli.Zhang@Sun.COM 
477811539SChunli.Zhang@Sun.COM /*ARGSUSED*/
477911539SChunli.Zhang@Sun.COM static int
478011539SChunli.Zhang@Sun.COM zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
478111539SChunli.Zhang@Sun.COM     caller_context_t *ct)
478211539SChunli.Zhang@Sun.COM {
478311539SChunli.Zhang@Sun.COM 	znode_t	*zp = VTOZ(vp);
478411539SChunli.Zhang@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
478511539SChunli.Zhang@Sun.COM 	int max_blksz = zfsvfs->z_max_blksz;
478611539SChunli.Zhang@Sun.COM 	uio_t *uio = &xuio->xu_uio;
478711539SChunli.Zhang@Sun.COM 	ssize_t size = uio->uio_resid;
478811539SChunli.Zhang@Sun.COM 	offset_t offset = uio->uio_loffset;
478911539SChunli.Zhang@Sun.COM 	int blksz;
479011539SChunli.Zhang@Sun.COM 	int fullblk, i;
479111539SChunli.Zhang@Sun.COM 	arc_buf_t *abuf;
479211539SChunli.Zhang@Sun.COM 	ssize_t maxsize;
479311539SChunli.Zhang@Sun.COM 	int preamble, postamble;
479411539SChunli.Zhang@Sun.COM 
479511539SChunli.Zhang@Sun.COM 	if (xuio->xu_type != UIOTYPE_ZEROCOPY)
479611539SChunli.Zhang@Sun.COM 		return (EINVAL);
479711539SChunli.Zhang@Sun.COM 
479811539SChunli.Zhang@Sun.COM 	ZFS_ENTER(zfsvfs);
479911539SChunli.Zhang@Sun.COM 	ZFS_VERIFY_ZP(zp);
480011539SChunli.Zhang@Sun.COM 	switch (ioflag) {
480111539SChunli.Zhang@Sun.COM 	case UIO_WRITE:
480211539SChunli.Zhang@Sun.COM 		/*
480311539SChunli.Zhang@Sun.COM 		 * Loan out an arc_buf for write if write size is bigger than
480411539SChunli.Zhang@Sun.COM 		 * max_blksz, and the file's block size is also max_blksz.
480511539SChunli.Zhang@Sun.COM 		 */
480611539SChunli.Zhang@Sun.COM 		blksz = max_blksz;
480711539SChunli.Zhang@Sun.COM 		if (size < blksz || zp->z_blksz != blksz) {
480811539SChunli.Zhang@Sun.COM 			ZFS_EXIT(zfsvfs);
480911539SChunli.Zhang@Sun.COM 			return (EINVAL);
481011539SChunli.Zhang@Sun.COM 		}
481111539SChunli.Zhang@Sun.COM 		/*
481211539SChunli.Zhang@Sun.COM 		 * Caller requests buffers for write before knowing where the
481311539SChunli.Zhang@Sun.COM 		 * write offset might be (e.g. NFS TCP write).
481411539SChunli.Zhang@Sun.COM 		 */
481511539SChunli.Zhang@Sun.COM 		if (offset == -1) {
481611539SChunli.Zhang@Sun.COM 			preamble = 0;
481711539SChunli.Zhang@Sun.COM 		} else {
481811539SChunli.Zhang@Sun.COM 			preamble = P2PHASE(offset, blksz);
481911539SChunli.Zhang@Sun.COM 			if (preamble) {
482011539SChunli.Zhang@Sun.COM 				preamble = blksz - preamble;
482111539SChunli.Zhang@Sun.COM 				size -= preamble;
482211539SChunli.Zhang@Sun.COM 			}
482311539SChunli.Zhang@Sun.COM 		}
482411539SChunli.Zhang@Sun.COM 
482511539SChunli.Zhang@Sun.COM 		postamble = P2PHASE(size, blksz);
482611539SChunli.Zhang@Sun.COM 		size -= postamble;
482711539SChunli.Zhang@Sun.COM 
482811539SChunli.Zhang@Sun.COM 		fullblk = size / blksz;
482911576SSurya.Prakki@Sun.COM 		(void) dmu_xuio_init(xuio,
483011539SChunli.Zhang@Sun.COM 		    (preamble != 0) + fullblk + (postamble != 0));
483111539SChunli.Zhang@Sun.COM 		DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
483211539SChunli.Zhang@Sun.COM 		    int, postamble, int,
483311539SChunli.Zhang@Sun.COM 		    (preamble != 0) + fullblk + (postamble != 0));
483411539SChunli.Zhang@Sun.COM 
483511539SChunli.Zhang@Sun.COM 		/*
483611539SChunli.Zhang@Sun.COM 		 * Have to fix iov base/len for partial buffers.  They
483711539SChunli.Zhang@Sun.COM 		 * currently represent full arc_buf's.
483811539SChunli.Zhang@Sun.COM 		 */
483911539SChunli.Zhang@Sun.COM 		if (preamble) {
484011539SChunli.Zhang@Sun.COM 			/* data begins in the middle of the arc_buf */
484111935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
484211935SMark.Shellenbaum@Sun.COM 			    blksz);
484311539SChunli.Zhang@Sun.COM 			ASSERT(abuf);
484411576SSurya.Prakki@Sun.COM 			(void) dmu_xuio_add(xuio, abuf,
484511576SSurya.Prakki@Sun.COM 			    blksz - preamble, preamble);
484611539SChunli.Zhang@Sun.COM 		}
484711539SChunli.Zhang@Sun.COM 
484811539SChunli.Zhang@Sun.COM 		for (i = 0; i < fullblk; i++) {
484911935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
485011935SMark.Shellenbaum@Sun.COM 			    blksz);
485111539SChunli.Zhang@Sun.COM 			ASSERT(abuf);
485211576SSurya.Prakki@Sun.COM 			(void) dmu_xuio_add(xuio, abuf, 0, blksz);
485311539SChunli.Zhang@Sun.COM 		}
485411539SChunli.Zhang@Sun.COM 
485511539SChunli.Zhang@Sun.COM 		if (postamble) {
485611539SChunli.Zhang@Sun.COM 			/* data ends in the middle of the arc_buf */
485711935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
485811935SMark.Shellenbaum@Sun.COM 			    blksz);
485911539SChunli.Zhang@Sun.COM 			ASSERT(abuf);
486011576SSurya.Prakki@Sun.COM 			(void) dmu_xuio_add(xuio, abuf, 0, postamble);
486111539SChunli.Zhang@Sun.COM 		}
486211539SChunli.Zhang@Sun.COM 		break;
486311539SChunli.Zhang@Sun.COM 	case UIO_READ:
486411539SChunli.Zhang@Sun.COM 		/*
486511539SChunli.Zhang@Sun.COM 		 * Loan out an arc_buf for read if the read size is larger than
486611539SChunli.Zhang@Sun.COM 		 * the current file block size.  Block alignment is not
486711539SChunli.Zhang@Sun.COM 		 * considered.  Partial arc_buf will be loaned out for read.
486811539SChunli.Zhang@Sun.COM 		 */
486911539SChunli.Zhang@Sun.COM 		blksz = zp->z_blksz;
487011539SChunli.Zhang@Sun.COM 		if (blksz < zcr_blksz_min)
487111539SChunli.Zhang@Sun.COM 			blksz = zcr_blksz_min;
487211539SChunli.Zhang@Sun.COM 		if (blksz > zcr_blksz_max)
487311539SChunli.Zhang@Sun.COM 			blksz = zcr_blksz_max;
487411539SChunli.Zhang@Sun.COM 		/* avoid potential complexity of dealing with it */
487511539SChunli.Zhang@Sun.COM 		if (blksz > max_blksz) {
487611539SChunli.Zhang@Sun.COM 			ZFS_EXIT(zfsvfs);
487711539SChunli.Zhang@Sun.COM 			return (EINVAL);
487811539SChunli.Zhang@Sun.COM 		}
487911539SChunli.Zhang@Sun.COM 
488011935SMark.Shellenbaum@Sun.COM 		maxsize = zp->z_size - uio->uio_loffset;
488111539SChunli.Zhang@Sun.COM 		if (size > maxsize)
488211539SChunli.Zhang@Sun.COM 			size = maxsize;
488311539SChunli.Zhang@Sun.COM 
488411539SChunli.Zhang@Sun.COM 		if (size < blksz || vn_has_cached_data(vp)) {
488511539SChunli.Zhang@Sun.COM 			ZFS_EXIT(zfsvfs);
488611539SChunli.Zhang@Sun.COM 			return (EINVAL);
488711539SChunli.Zhang@Sun.COM 		}
488811539SChunli.Zhang@Sun.COM 		break;
488911539SChunli.Zhang@Sun.COM 	default:
489011539SChunli.Zhang@Sun.COM 		ZFS_EXIT(zfsvfs);
489111539SChunli.Zhang@Sun.COM 		return (EINVAL);
489211539SChunli.Zhang@Sun.COM 	}
489311539SChunli.Zhang@Sun.COM 
489411539SChunli.Zhang@Sun.COM 	uio->uio_extflg = UIO_XUIO;
489511539SChunli.Zhang@Sun.COM 	XUIO_XUZC_RW(xuio) = ioflag;
489611539SChunli.Zhang@Sun.COM 	ZFS_EXIT(zfsvfs);
489711539SChunli.Zhang@Sun.COM 	return (0);
489811539SChunli.Zhang@Sun.COM }
489911539SChunli.Zhang@Sun.COM 
490011539SChunli.Zhang@Sun.COM /*ARGSUSED*/
490111539SChunli.Zhang@Sun.COM static int
490211539SChunli.Zhang@Sun.COM zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
490311539SChunli.Zhang@Sun.COM {
490411539SChunli.Zhang@Sun.COM 	int i;
490511539SChunli.Zhang@Sun.COM 	arc_buf_t *abuf;
490611539SChunli.Zhang@Sun.COM 	int ioflag = XUIO_XUZC_RW(xuio);
490711539SChunli.Zhang@Sun.COM 
490811539SChunli.Zhang@Sun.COM 	ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
490911539SChunli.Zhang@Sun.COM 
491011539SChunli.Zhang@Sun.COM 	i = dmu_xuio_cnt(xuio);
491111539SChunli.Zhang@Sun.COM 	while (i-- > 0) {
491211539SChunli.Zhang@Sun.COM 		abuf = dmu_xuio_arcbuf(xuio, i);
491311539SChunli.Zhang@Sun.COM 		/*
491411539SChunli.Zhang@Sun.COM 		 * if abuf == NULL, it must be a write buffer
491511539SChunli.Zhang@Sun.COM 		 * that has been returned in zfs_write().
491611539SChunli.Zhang@Sun.COM 		 */
491711539SChunli.Zhang@Sun.COM 		if (abuf)
491811539SChunli.Zhang@Sun.COM 			dmu_return_arcbuf(abuf);
491911539SChunli.Zhang@Sun.COM 		ASSERT(abuf || ioflag == UIO_WRITE);
492011539SChunli.Zhang@Sun.COM 	}
492111539SChunli.Zhang@Sun.COM 
492211539SChunli.Zhang@Sun.COM 	dmu_xuio_fini(xuio);
492311539SChunli.Zhang@Sun.COM 	return (0);
492411539SChunli.Zhang@Sun.COM }
492511539SChunli.Zhang@Sun.COM 
492611539SChunli.Zhang@Sun.COM /*
4927789Sahrens  * Predeclare these here so that the compiler assumes that
4928789Sahrens  * this is an "old style" function declaration that does
4929789Sahrens  * not include arguments => we won't get type mismatch errors
4930789Sahrens  * in the initializations that follow.
4931789Sahrens  */
4932789Sahrens static int zfs_inval();
4933789Sahrens static int zfs_isdir();
4934789Sahrens 
4935789Sahrens static int
4936789Sahrens zfs_inval()
4937789Sahrens {
4938789Sahrens 	return (EINVAL);
4939789Sahrens }
4940789Sahrens 
4941789Sahrens static int
4942789Sahrens zfs_isdir()
4943789Sahrens {
4944789Sahrens 	return (EISDIR);
4945789Sahrens }
4946789Sahrens /*
4947789Sahrens  * Directory vnode operations template
4948789Sahrens  */
4949789Sahrens vnodeops_t *zfs_dvnodeops;
4950789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = {
49513898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
49523898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
49533898Srsb 	VOPNAME_READ,		{ .error = zfs_isdir },
49543898Srsb 	VOPNAME_WRITE,		{ .error = zfs_isdir },
49553898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
49563898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
49573898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
49583898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
49593898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
49603898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
49613898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
49623898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
49633898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
49643898Srsb 	VOPNAME_MKDIR,		{ .vop_mkdir = zfs_mkdir },
49653898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
49663898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
49673898Srsb 	VOPNAME_SYMLINK,	{ .vop_symlink = zfs_symlink },
49683898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
49693898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
49703898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
49713898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
49723898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
49733898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
49743898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
49754863Spraks 	VOPNAME_VNEVENT, 	{ .vop_vnevent = fs_vnevent_support },
49763898Srsb 	NULL,			NULL
4977789Sahrens };
4978789Sahrens 
4979789Sahrens /*
4980789Sahrens  * Regular file vnode operations template
4981789Sahrens  */
4982789Sahrens vnodeops_t *zfs_fvnodeops;
4983789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = {
49843898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
49853898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
49863898Srsb 	VOPNAME_READ,		{ .vop_read = zfs_read },
49873898Srsb 	VOPNAME_WRITE,		{ .vop_write = zfs_write },
49883898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
49893898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
49903898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
49913898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
49923898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
49933898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
49943898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
49953898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
49963898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
49973898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
49983898Srsb 	VOPNAME_FRLOCK,		{ .vop_frlock = zfs_frlock },
49993898Srsb 	VOPNAME_SPACE,		{ .vop_space = zfs_space },
50003898Srsb 	VOPNAME_GETPAGE,	{ .vop_getpage = zfs_getpage },
50013898Srsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = zfs_putpage },
50023898Srsb 	VOPNAME_MAP,		{ .vop_map = zfs_map },
50033898Srsb 	VOPNAME_ADDMAP,		{ .vop_addmap = zfs_addmap },
50043898Srsb 	VOPNAME_DELMAP,		{ .vop_delmap = zfs_delmap },
50053898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50063898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
50073898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
50083898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
500911539SChunli.Zhang@Sun.COM 	VOPNAME_REQZCBUF, 	{ .vop_reqzcbuf = zfs_reqzcbuf },
501011539SChunli.Zhang@Sun.COM 	VOPNAME_RETZCBUF, 	{ .vop_retzcbuf = zfs_retzcbuf },
50113898Srsb 	NULL,			NULL
5012789Sahrens };
5013789Sahrens 
5014789Sahrens /*
5015789Sahrens  * Symbolic link vnode operations template
5016789Sahrens  */
5017789Sahrens vnodeops_t *zfs_symvnodeops;
5018789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = {
50193898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
50203898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
50213898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
50223898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
50233898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = zfs_readlink },
50243898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
50253898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
50263898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50273898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
50283898Srsb 	NULL,			NULL
5029789Sahrens };
5030789Sahrens 
5031789Sahrens /*
50328845Samw@Sun.COM  * special share hidden files vnode operations template
50338845Samw@Sun.COM  */
50348845Samw@Sun.COM vnodeops_t *zfs_sharevnodeops;
50358845Samw@Sun.COM const fs_operation_def_t zfs_sharevnodeops_template[] = {
50368845Samw@Sun.COM 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
50378845Samw@Sun.COM 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
50388845Samw@Sun.COM 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
50398845Samw@Sun.COM 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
50408845Samw@Sun.COM 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50418845Samw@Sun.COM 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
50428845Samw@Sun.COM 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
50438845Samw@Sun.COM 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
50448845Samw@Sun.COM 	NULL,			NULL
50458845Samw@Sun.COM };
50468845Samw@Sun.COM 
50478845Samw@Sun.COM /*
5048789Sahrens  * Extended attribute directory vnode operations template
5049789Sahrens  *	This template is identical to the directory vnodes
5050789Sahrens  *	operation template except for restricted operations:
5051789Sahrens  *		VOP_MKDIR()
5052789Sahrens  *		VOP_SYMLINK()
5053789Sahrens  * Note that there are other restrictions embedded in:
5054789Sahrens  *	zfs_create()	- restrict type to VREG
5055789Sahrens  *	zfs_link()	- no links into/out of attribute space
5056789Sahrens  *	zfs_rename()	- no moves into/out of attribute space
5057789Sahrens  */
5058789Sahrens vnodeops_t *zfs_xdvnodeops;
5059789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = {
50603898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
50613898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
50623898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
50633898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
50643898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
50653898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
50663898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
50673898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
50683898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
50693898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
50703898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
50713898Srsb 	VOPNAME_MKDIR,		{ .error = zfs_inval },
50723898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
50733898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
50743898Srsb 	VOPNAME_SYMLINK,	{ .error = zfs_inval },
50753898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
50763898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
50773898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
50783898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
50793898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50803898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
50813898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
50823898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
50833898Srsb 	NULL,			NULL
5084789Sahrens };
5085789Sahrens 
5086789Sahrens /*
5087789Sahrens  * Error vnode operations template
5088789Sahrens  */
5089789Sahrens vnodeops_t *zfs_evnodeops;
5090789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = {
50913898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
50923898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50933898Srsb 	NULL,			NULL
5094789Sahrens };
5095