xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 13089:08bbd228b732)
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 */
2612294SMark.Musante@Sun.COM /* Portions Copyright 2010 Robert Milkowski */
274144Speteh 
28789Sahrens #include <sys/types.h>
29789Sahrens #include <sys/param.h>
30789Sahrens #include <sys/time.h>
31789Sahrens #include <sys/systm.h>
32789Sahrens #include <sys/sysmacros.h>
33789Sahrens #include <sys/resource.h>
34789Sahrens #include <sys/vfs.h>
353898Srsb #include <sys/vfs_opreg.h>
36789Sahrens #include <sys/vnode.h>
37789Sahrens #include <sys/file.h>
38789Sahrens #include <sys/stat.h>
39789Sahrens #include <sys/kmem.h>
40789Sahrens #include <sys/taskq.h>
41789Sahrens #include <sys/uio.h>
42789Sahrens #include <sys/vmsystm.h>
43789Sahrens #include <sys/atomic.h>
442688Smaybee #include <sys/vm.h>
45789Sahrens #include <vm/seg_vn.h>
46789Sahrens #include <vm/pvn.h>
47789Sahrens #include <vm/as.h>
487315SJonathan.Adams@Sun.COM #include <vm/kpm.h>
497315SJonathan.Adams@Sun.COM #include <vm/seg_kpm.h>
50789Sahrens #include <sys/mman.h>
51789Sahrens #include <sys/pathname.h>
52789Sahrens #include <sys/cmn_err.h>
53789Sahrens #include <sys/errno.h>
54789Sahrens #include <sys/unistd.h>
55789Sahrens #include <sys/zfs_dir.h>
56789Sahrens #include <sys/zfs_acl.h>
57789Sahrens #include <sys/zfs_ioctl.h>
58789Sahrens #include <sys/fs/zfs.h>
59789Sahrens #include <sys/dmu.h>
6012294SMark.Musante@Sun.COM #include <sys/dmu_objset.h>
61789Sahrens #include <sys/spa.h>
62789Sahrens #include <sys/txg.h>
63789Sahrens #include <sys/dbuf.h>
64789Sahrens #include <sys/zap.h>
6511935SMark.Shellenbaum@Sun.COM #include <sys/sa.h>
66789Sahrens #include <sys/dirent.h>
67789Sahrens #include <sys/policy.h>
68789Sahrens #include <sys/sunddi.h>
69789Sahrens #include <sys/filio.h>
707847SMark.Shellenbaum@Sun.COM #include <sys/sid.h>
71789Sahrens #include "fs/fs_subr.h"
72789Sahrens #include <sys/zfs_ctldir.h>
735331Samw #include <sys/zfs_fuid.h>
7411935SMark.Shellenbaum@Sun.COM #include <sys/zfs_sa.h>
751484Sek110237 #include <sys/dnlc.h>
761669Sperrin #include <sys/zfs_rlock.h>
775331Samw #include <sys/extdirent.h>
785331Samw #include <sys/kidmap.h>
7911134SCasper.Dik@Sun.COM #include <sys/cred.h>
805663Sck153898 #include <sys/attr.h>
81789Sahrens 
82789Sahrens /*
83789Sahrens  * Programming rules.
84789Sahrens  *
85789Sahrens  * Each vnode op performs some logical unit of work.  To do this, the ZPL must
86789Sahrens  * properly lock its in-core state, create a DMU transaction, do the work,
87789Sahrens  * record this work in the intent log (ZIL), commit the DMU transaction,
885331Samw  * and wait for the intent log to commit if it is a synchronous operation.
895331Samw  * Moreover, the vnode ops must work in both normal and log replay context.
90789Sahrens  * The ordering of events is important to avoid deadlocks and references
91789Sahrens  * to freed memory.  The example below illustrates the following Big Rules:
92789Sahrens  *
93789Sahrens  *  (1) A check must be made in each zfs thread for a mounted file system.
945367Sahrens  *	This is done avoiding races using ZFS_ENTER(zfsvfs).
955367Sahrens  *      A ZFS_EXIT(zfsvfs) is needed before all returns.  Any znodes
965367Sahrens  *      must be checked with ZFS_VERIFY_ZP(zp).  Both of these macros
975367Sahrens  *      can return EIO from the calling function.
98789Sahrens  *
99789Sahrens  *  (2)	VN_RELE() should always be the last thing except for zil_commit()
1002638Sperrin  *	(if necessary) and ZFS_EXIT(). This is for 3 reasons:
101789Sahrens  *	First, if it's the last reference, the vnode/znode
102789Sahrens  *	can be freed, so the zp may point to freed memory.  Second, the last
103789Sahrens  *	reference will call zfs_zinactive(), which may induce a lot of work --
1041669Sperrin  *	pushing cached pages (which acquires range locks) and syncing out
105789Sahrens  *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
106789Sahrens  *	which could deadlock the system if you were already holding one.
1079321SNeil.Perrin@Sun.COM  *	If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
108789Sahrens  *
1091757Sperrin  *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
1101757Sperrin  *	as they can span dmu_tx_assign() calls.
1111757Sperrin  *
1128227SNeil.Perrin@Sun.COM  *  (4)	Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
113789Sahrens  *	This is critical because we don't want to block while holding locks.
114789Sahrens  *	Note, in particular, that if a lock is sometimes acquired before
115789Sahrens  *	the tx assigns, and sometimes after (e.g. z_lock), then failing to
116789Sahrens  *	use a non-blocking assign can deadlock the system.  The scenario:
117789Sahrens  *
118789Sahrens  *	Thread A has grabbed a lock before calling dmu_tx_assign().
119789Sahrens  *	Thread B is in an already-assigned tx, and blocks for this lock.
120789Sahrens  *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
121789Sahrens  *	forever, because the previous txg can't quiesce until B's tx commits.
122789Sahrens  *
123789Sahrens  *	If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
1242113Sahrens  *	then drop all locks, call dmu_tx_wait(), and try again.
125789Sahrens  *
1261757Sperrin  *  (5)	If the operation succeeded, generate the intent log entry for it
127789Sahrens  *	before dropping locks.  This ensures that the ordering of events
128789Sahrens  *	in the intent log matches the order in which they actually occurred.
1298227SNeil.Perrin@Sun.COM  *      During ZIL replay the zfs_log_* functions will update the sequence
1308227SNeil.Perrin@Sun.COM  *	number to indicate the zil transaction has replayed.
131789Sahrens  *
1321757Sperrin  *  (6)	At the end of each vnode op, the DMU tx must always commit,
133789Sahrens  *	regardless of whether there were any errors.
134789Sahrens  *
13512699SNeil.Perrin@Sun.COM  *  (7)	After dropping all locks, invoke zil_commit(zilog, foid)
136789Sahrens  *	to ensure that synchronous semantics are provided when necessary.
137789Sahrens  *
138789Sahrens  * In general, this is how things should be ordered in each vnode op:
139789Sahrens  *
140789Sahrens  *	ZFS_ENTER(zfsvfs);		// exit if unmounted
141789Sahrens  * top:
142789Sahrens  *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
143789Sahrens  *	rw_enter(...);			// grab any other locks you need
144789Sahrens  *	tx = dmu_tx_create(...);	// get DMU tx
145789Sahrens  *	dmu_tx_hold_*();		// hold each object you might modify
1468227SNeil.Perrin@Sun.COM  *	error = dmu_tx_assign(tx, TXG_NOWAIT);	// try to assign
147789Sahrens  *	if (error) {
148789Sahrens  *		rw_exit(...);		// drop locks
149789Sahrens  *		zfs_dirent_unlock(dl);	// unlock directory entry
150789Sahrens  *		VN_RELE(...);		// release held vnodes
1518227SNeil.Perrin@Sun.COM  *		if (error == ERESTART) {
1522113Sahrens  *			dmu_tx_wait(tx);
1532113Sahrens  *			dmu_tx_abort(tx);
154789Sahrens  *			goto top;
155789Sahrens  *		}
1562113Sahrens  *		dmu_tx_abort(tx);	// abort DMU tx
157789Sahrens  *		ZFS_EXIT(zfsvfs);	// finished in zfs
158789Sahrens  *		return (error);		// really out of space
159789Sahrens  *	}
160789Sahrens  *	error = do_real_work();		// do whatever this VOP does
161789Sahrens  *	if (error == 0)
1622638Sperrin  *		zfs_log_*(...);		// on success, make ZIL entry
163789Sahrens  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
164789Sahrens  *	rw_exit(...);			// drop locks
165789Sahrens  *	zfs_dirent_unlock(dl);		// unlock directory entry
166789Sahrens  *	VN_RELE(...);			// release held vnodes
16712699SNeil.Perrin@Sun.COM  *	zil_commit(zilog, foid);	// synchronous when necessary
168789Sahrens  *	ZFS_EXIT(zfsvfs);		// finished in zfs
169789Sahrens  *	return (error);			// done, report error
170789Sahrens  */
1715367Sahrens 
172789Sahrens /* ARGSUSED */
173789Sahrens static int
zfs_open(vnode_t ** vpp,int flag,cred_t * cr,caller_context_t * ct)1745331Samw zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
175789Sahrens {
1763063Sperrin 	znode_t	*zp = VTOZ(*vpp);
1777844SMark.Shellenbaum@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1787844SMark.Shellenbaum@Sun.COM 
1797844SMark.Shellenbaum@Sun.COM 	ZFS_ENTER(zfsvfs);
1807844SMark.Shellenbaum@Sun.COM 	ZFS_VERIFY_ZP(zp);
1813063Sperrin 
18211935SMark.Shellenbaum@Sun.COM 	if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
1835331Samw 	    ((flag & FAPPEND) == 0)) {
1847844SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
1855331Samw 		return (EPERM);
1865331Samw 	}
1875331Samw 
1885331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
1895331Samw 	    ZTOV(zp)->v_type == VREG &&
19011935SMark.Shellenbaum@Sun.COM 	    !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
1917844SMark.Shellenbaum@Sun.COM 		if (fs_vscan(*vpp, cr, 0) != 0) {
1927844SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
1935331Samw 			return (EACCES);
1947844SMark.Shellenbaum@Sun.COM 		}
1957844SMark.Shellenbaum@Sun.COM 	}
1965331Samw 
1973063Sperrin 	/* Keep a count of the synchronous opens in the znode */
1983063Sperrin 	if (flag & (FSYNC | FDSYNC))
1993063Sperrin 		atomic_inc_32(&zp->z_sync_cnt);
2005331Samw 
2017844SMark.Shellenbaum@Sun.COM 	ZFS_EXIT(zfsvfs);
202789Sahrens 	return (0);
203789Sahrens }
204789Sahrens 
205789Sahrens /* ARGSUSED */
206789Sahrens static int
zfs_close(vnode_t * vp,int flag,int count,offset_t offset,cred_t * cr,caller_context_t * ct)2075331Samw zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
2085331Samw     caller_context_t *ct)
209789Sahrens {
2103063Sperrin 	znode_t	*zp = VTOZ(vp);
2117844SMark.Shellenbaum@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2127844SMark.Shellenbaum@Sun.COM 
2139909Schris.kirby@sun.com 	/*
2149909Schris.kirby@sun.com 	 * Clean up any locks held by this process on the vp.
2159909Schris.kirby@sun.com 	 */
2169909Schris.kirby@sun.com 	cleanlocks(vp, ddi_get_pid(), 0);
2179909Schris.kirby@sun.com 	cleanshares(vp, ddi_get_pid());
2189909Schris.kirby@sun.com 
2197844SMark.Shellenbaum@Sun.COM 	ZFS_ENTER(zfsvfs);
2207844SMark.Shellenbaum@Sun.COM 	ZFS_VERIFY_ZP(zp);
2213063Sperrin 
2223063Sperrin 	/* Decrement the synchronous opens in the znode */
2234339Sperrin 	if ((flag & (FSYNC | FDSYNC)) && (count == 1))
2243063Sperrin 		atomic_dec_32(&zp->z_sync_cnt);
2253063Sperrin 
2265331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
2275331Samw 	    ZTOV(zp)->v_type == VREG &&
22811935SMark.Shellenbaum@Sun.COM 	    !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
2295331Samw 		VERIFY(fs_vscan(vp, cr, 1) == 0);
2305331Samw 
2317844SMark.Shellenbaum@Sun.COM 	ZFS_EXIT(zfsvfs);
232789Sahrens 	return (0);
233789Sahrens }
234789Sahrens 
235789Sahrens /*
236789Sahrens  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
237789Sahrens  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
238789Sahrens  */
239789Sahrens static int
zfs_holey(vnode_t * vp,int cmd,offset_t * off)240789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off)
241789Sahrens {
242789Sahrens 	znode_t	*zp = VTOZ(vp);
243789Sahrens 	uint64_t noff = (uint64_t)*off; /* new offset */
244789Sahrens 	uint64_t file_sz;
245789Sahrens 	int error;
246789Sahrens 	boolean_t hole;
247789Sahrens 
24811935SMark.Shellenbaum@Sun.COM 	file_sz = zp->z_size;
249789Sahrens 	if (noff >= file_sz)  {
250789Sahrens 		return (ENXIO);
251789Sahrens 	}
252789Sahrens 
253789Sahrens 	if (cmd == _FIO_SEEK_HOLE)
254789Sahrens 		hole = B_TRUE;
255789Sahrens 	else
256789Sahrens 		hole = B_FALSE;
257789Sahrens 
258789Sahrens 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
259789Sahrens 
260789Sahrens 	/* end of file? */
261789Sahrens 	if ((error == ESRCH) || (noff > file_sz)) {
262789Sahrens 		/*
263789Sahrens 		 * Handle the virtual hole at the end of file.
264789Sahrens 		 */
265789Sahrens 		if (hole) {
266789Sahrens 			*off = file_sz;
267789Sahrens 			return (0);
268789Sahrens 		}
269789Sahrens 		return (ENXIO);
270789Sahrens 	}
271789Sahrens 
272789Sahrens 	if (noff < *off)
273789Sahrens 		return (error);
274789Sahrens 	*off = noff;
275789Sahrens 	return (error);
276789Sahrens }
277789Sahrens 
278789Sahrens /* ARGSUSED */
279789Sahrens static int
zfs_ioctl(vnode_t * vp,int com,intptr_t data,int flag,cred_t * cred,int * rvalp,caller_context_t * ct)280789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
2815331Samw     int *rvalp, caller_context_t *ct)
282789Sahrens {
283789Sahrens 	offset_t off;
284789Sahrens 	int error;
285789Sahrens 	zfsvfs_t *zfsvfs;
2865326Sek110237 	znode_t *zp;
287789Sahrens 
288789Sahrens 	switch (com) {
2894339Sperrin 	case _FIOFFS:
290789Sahrens 		return (zfs_sync(vp->v_vfsp, 0, cred));
291789Sahrens 
2921544Seschrock 		/*
2931544Seschrock 		 * The following two ioctls are used by bfu.  Faking out,
2941544Seschrock 		 * necessary to avoid bfu errors.
2951544Seschrock 		 */
2964339Sperrin 	case _FIOGDIO:
2974339Sperrin 	case _FIOSDIO:
2981544Seschrock 		return (0);
2991544Seschrock 
3004339Sperrin 	case _FIO_SEEK_DATA:
3014339Sperrin 	case _FIO_SEEK_HOLE:
302789Sahrens 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
303789Sahrens 			return (EFAULT);
304789Sahrens 
3055326Sek110237 		zp = VTOZ(vp);
3065326Sek110237 		zfsvfs = zp->z_zfsvfs;
3075367Sahrens 		ZFS_ENTER(zfsvfs);
3085367Sahrens 		ZFS_VERIFY_ZP(zp);
309789Sahrens 
310789Sahrens 		/* offset parameter is in/out */
311789Sahrens 		error = zfs_holey(vp, com, &off);
312789Sahrens 		ZFS_EXIT(zfsvfs);
313789Sahrens 		if (error)
314789Sahrens 			return (error);
315789Sahrens 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
316789Sahrens 			return (EFAULT);
317789Sahrens 		return (0);
318789Sahrens 	}
319789Sahrens 	return (ENOTTY);
320789Sahrens }
321789Sahrens 
322789Sahrens /*
3237315SJonathan.Adams@Sun.COM  * Utility functions to map and unmap a single physical page.  These
3247315SJonathan.Adams@Sun.COM  * are used to manage the mappable copies of ZFS file data, and therefore
3257315SJonathan.Adams@Sun.COM  * do not update ref/mod bits.
3267315SJonathan.Adams@Sun.COM  */
3277315SJonathan.Adams@Sun.COM caddr_t
zfs_map_page(page_t * pp,enum seg_rw rw)3287315SJonathan.Adams@Sun.COM zfs_map_page(page_t *pp, enum seg_rw rw)
3297315SJonathan.Adams@Sun.COM {
3307315SJonathan.Adams@Sun.COM 	if (kpm_enable)
3317315SJonathan.Adams@Sun.COM 		return (hat_kpm_mapin(pp, 0));
3327315SJonathan.Adams@Sun.COM 	ASSERT(rw == S_READ || rw == S_WRITE);
3337315SJonathan.Adams@Sun.COM 	return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0),
3347315SJonathan.Adams@Sun.COM 	    (caddr_t)-1));
3357315SJonathan.Adams@Sun.COM }
3367315SJonathan.Adams@Sun.COM 
3377315SJonathan.Adams@Sun.COM void
zfs_unmap_page(page_t * pp,caddr_t addr)3387315SJonathan.Adams@Sun.COM zfs_unmap_page(page_t *pp, caddr_t addr)
3397315SJonathan.Adams@Sun.COM {
3407315SJonathan.Adams@Sun.COM 	if (kpm_enable) {
3417315SJonathan.Adams@Sun.COM 		hat_kpm_mapout(pp, 0, addr);
3427315SJonathan.Adams@Sun.COM 	} else {
3437315SJonathan.Adams@Sun.COM 		ppmapout(addr);
3447315SJonathan.Adams@Sun.COM 	}
3457315SJonathan.Adams@Sun.COM }
3467315SJonathan.Adams@Sun.COM 
3477315SJonathan.Adams@Sun.COM /*
348789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
349789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
350789Sahrens  *
351789Sahrens  * On Write:	If we find a memory mapped page, we write to *both*
352789Sahrens  *		the page and the dmu buffer.
353789Sahrens  */
3548636SMark.Maybee@Sun.COM static void
update_pages(vnode_t * vp,int64_t start,int len,objset_t * os,uint64_t oid)3558636SMark.Maybee@Sun.COM update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid)
356789Sahrens {
3578636SMark.Maybee@Sun.COM 	int64_t	off;
3588636SMark.Maybee@Sun.COM 
359789Sahrens 	off = start & PAGEOFFSET;
360789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
361789Sahrens 		page_t *pp;
3628636SMark.Maybee@Sun.COM 		uint64_t nbytes = MIN(PAGESIZE - off, len);
3638636SMark.Maybee@Sun.COM 
364789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
365789Sahrens 			caddr_t va;
366789Sahrens 
3677315SJonathan.Adams@Sun.COM 			va = zfs_map_page(pp, S_WRITE);
3689512SNeil.Perrin@Sun.COM 			(void) dmu_read(os, oid, start+off, nbytes, va+off,
3699512SNeil.Perrin@Sun.COM 			    DMU_READ_PREFETCH);
3707315SJonathan.Adams@Sun.COM 			zfs_unmap_page(pp, va);
371789Sahrens 			page_unlock(pp);
372789Sahrens 		}
3738636SMark.Maybee@Sun.COM 		len -= nbytes;
374789Sahrens 		off = 0;
375789Sahrens 	}
376789Sahrens }
377789Sahrens 
378789Sahrens /*
379789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
380789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
381789Sahrens  *
382789Sahrens  * On Read:	We "read" preferentially from memory mapped pages,
383789Sahrens  *		else we default from the dmu buffer.
384789Sahrens  *
385789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
386789Sahrens  *	the file is memory mapped.
387789Sahrens  */
388789Sahrens static int
mappedread(vnode_t * vp,int nbytes,uio_t * uio)3893638Sbillm mappedread(vnode_t *vp, int nbytes, uio_t *uio)
390789Sahrens {
3913638Sbillm 	znode_t *zp = VTOZ(vp);
3923638Sbillm 	objset_t *os = zp->z_zfsvfs->z_os;
3933638Sbillm 	int64_t	start, off;
394789Sahrens 	int len = nbytes;
395789Sahrens 	int error = 0;
396789Sahrens 
397789Sahrens 	start = uio->uio_loffset;
398789Sahrens 	off = start & PAGEOFFSET;
399789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
400789Sahrens 		page_t *pp;
4013638Sbillm 		uint64_t bytes = MIN(PAGESIZE - off, len);
4023638Sbillm 
403789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
404789Sahrens 			caddr_t va;
405789Sahrens 
4067315SJonathan.Adams@Sun.COM 			va = zfs_map_page(pp, S_READ);
407789Sahrens 			error = uiomove(va + off, bytes, UIO_READ, uio);
4087315SJonathan.Adams@Sun.COM 			zfs_unmap_page(pp, va);
409789Sahrens 			page_unlock(pp);
410789Sahrens 		} else {
4113638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, bytes);
412789Sahrens 		}
413789Sahrens 		len -= bytes;
414789Sahrens 		off = 0;
415789Sahrens 		if (error)
416789Sahrens 			break;
417789Sahrens 	}
418789Sahrens 	return (error);
419789Sahrens }
420789Sahrens 
4213638Sbillm offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
422789Sahrens 
423789Sahrens /*
424789Sahrens  * Read bytes from specified file into supplied buffer.
425789Sahrens  *
426789Sahrens  *	IN:	vp	- vnode of file to be read from.
427789Sahrens  *		uio	- structure supplying read location, range info,
428789Sahrens  *			  and return buffer.
429789Sahrens  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
430789Sahrens  *		cr	- credentials of caller.
4315331Samw  *		ct	- caller context
432789Sahrens  *
433789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
434789Sahrens  *
435789Sahrens  *	RETURN:	0 if success
436789Sahrens  *		error code if failure
437789Sahrens  *
438789Sahrens  * Side Effects:
439789Sahrens  *	vp - atime updated if byte count > 0
440789Sahrens  */
441789Sahrens /* ARGSUSED */
442789Sahrens static int
zfs_read(vnode_t * vp,uio_t * uio,int ioflag,cred_t * cr,caller_context_t * ct)443789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
444789Sahrens {
445789Sahrens 	znode_t		*zp = VTOZ(vp);
446789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4475326Sek110237 	objset_t	*os;
4483638Sbillm 	ssize_t		n, nbytes;
4493638Sbillm 	int		error;
4501669Sperrin 	rl_t		*rl;
45111539SChunli.Zhang@Sun.COM 	xuio_t		*xuio = NULL;
452789Sahrens 
4535367Sahrens 	ZFS_ENTER(zfsvfs);
4545367Sahrens 	ZFS_VERIFY_ZP(zp);
4555326Sek110237 	os = zfsvfs->z_os;
456789Sahrens 
45711935SMark.Shellenbaum@Sun.COM 	if (zp->z_pflags & ZFS_AV_QUARANTINED) {
4585929Smarks 		ZFS_EXIT(zfsvfs);
4595929Smarks 		return (EACCES);
4605929Smarks 	}
4615929Smarks 
462789Sahrens 	/*
463789Sahrens 	 * Validate file offset
464789Sahrens 	 */
465789Sahrens 	if (uio->uio_loffset < (offset_t)0) {
466789Sahrens 		ZFS_EXIT(zfsvfs);
467789Sahrens 		return (EINVAL);
468789Sahrens 	}
469789Sahrens 
470789Sahrens 	/*
471789Sahrens 	 * Fasttrack empty reads
472789Sahrens 	 */
473789Sahrens 	if (uio->uio_resid == 0) {
474789Sahrens 		ZFS_EXIT(zfsvfs);
475789Sahrens 		return (0);
476789Sahrens 	}
477789Sahrens 
478789Sahrens 	/*
4791669Sperrin 	 * Check for mandatory locks
480789Sahrens 	 */
48111935SMark.Shellenbaum@Sun.COM 	if (MANDMODE(zp->z_mode)) {
482789Sahrens 		if (error = chklock(vp, FREAD,
483789Sahrens 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
484789Sahrens 			ZFS_EXIT(zfsvfs);
485789Sahrens 			return (error);
486789Sahrens 		}
487789Sahrens 	}
488789Sahrens 
489789Sahrens 	/*
490789Sahrens 	 * If we're in FRSYNC mode, sync out this znode before reading it.
491789Sahrens 	 */
49212294SMark.Musante@Sun.COM 	if (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
49312699SNeil.Perrin@Sun.COM 		zil_commit(zfsvfs->z_log, zp->z_id);
494789Sahrens 
495789Sahrens 	/*
4961669Sperrin 	 * Lock the range against changes.
497789Sahrens 	 */
4981669Sperrin 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
4991669Sperrin 
500789Sahrens 	/*
501789Sahrens 	 * If we are reading past end-of-file we can skip
502789Sahrens 	 * to the end; but we might still need to set atime.
503789Sahrens 	 */
50411935SMark.Shellenbaum@Sun.COM 	if (uio->uio_loffset >= zp->z_size) {
505789Sahrens 		error = 0;
506789Sahrens 		goto out;
507789Sahrens 	}
508789Sahrens 
50911935SMark.Shellenbaum@Sun.COM 	ASSERT(uio->uio_loffset < zp->z_size);
51011935SMark.Shellenbaum@Sun.COM 	n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
5113638Sbillm 
51211539SChunli.Zhang@Sun.COM 	if ((uio->uio_extflg == UIO_XUIO) &&
51311539SChunli.Zhang@Sun.COM 	    (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
51411539SChunli.Zhang@Sun.COM 		int nblk;
51511539SChunli.Zhang@Sun.COM 		int blksz = zp->z_blksz;
51611539SChunli.Zhang@Sun.COM 		uint64_t offset = uio->uio_loffset;
51711539SChunli.Zhang@Sun.COM 
51811539SChunli.Zhang@Sun.COM 		xuio = (xuio_t *)uio;
51911539SChunli.Zhang@Sun.COM 		if ((ISP2(blksz))) {
52011539SChunli.Zhang@Sun.COM 			nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
52111539SChunli.Zhang@Sun.COM 			    blksz)) / blksz;
52211539SChunli.Zhang@Sun.COM 		} else {
52311539SChunli.Zhang@Sun.COM 			ASSERT(offset + n <= blksz);
52411539SChunli.Zhang@Sun.COM 			nblk = 1;
52511539SChunli.Zhang@Sun.COM 		}
52611576SSurya.Prakki@Sun.COM 		(void) dmu_xuio_init(xuio, nblk);
52711539SChunli.Zhang@Sun.COM 
52811539SChunli.Zhang@Sun.COM 		if (vn_has_cached_data(vp)) {
52911539SChunli.Zhang@Sun.COM 			/*
53011539SChunli.Zhang@Sun.COM 			 * For simplicity, we always allocate a full buffer
53111539SChunli.Zhang@Sun.COM 			 * even if we only expect to read a portion of a block.
53211539SChunli.Zhang@Sun.COM 			 */
53311539SChunli.Zhang@Sun.COM 			while (--nblk >= 0) {
53411576SSurya.Prakki@Sun.COM 				(void) dmu_xuio_add(xuio,
53511935SMark.Shellenbaum@Sun.COM 				    dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
53611935SMark.Shellenbaum@Sun.COM 				    blksz), 0, blksz);
53711539SChunli.Zhang@Sun.COM 			}
53811539SChunli.Zhang@Sun.COM 		}
53911539SChunli.Zhang@Sun.COM 	}
54011539SChunli.Zhang@Sun.COM 
5413638Sbillm 	while (n > 0) {
5423638Sbillm 		nbytes = MIN(n, zfs_read_chunk_size -
5433638Sbillm 		    P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
5443638Sbillm 
5453638Sbillm 		if (vn_has_cached_data(vp))
5463638Sbillm 			error = mappedread(vp, nbytes, uio);
5473638Sbillm 		else
5483638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, nbytes);
5497294Sperrin 		if (error) {
5507294Sperrin 			/* convert checksum errors into IO errors */
5517294Sperrin 			if (error == ECKSUM)
5527294Sperrin 				error = EIO;
5533638Sbillm 			break;
5547294Sperrin 		}
5553638Sbillm 
5563638Sbillm 		n -= nbytes;
557789Sahrens 	}
558789Sahrens out:
5592237Smaybee 	zfs_range_unlock(rl);
560789Sahrens 
561789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
562789Sahrens 	ZFS_EXIT(zfsvfs);
563789Sahrens 	return (error);
564789Sahrens }
565789Sahrens 
566789Sahrens /*
567789Sahrens  * Write the bytes to a file.
568789Sahrens  *
569789Sahrens  *	IN:	vp	- vnode of file to be written to.
570789Sahrens  *		uio	- structure supplying write location, range info,
571789Sahrens  *			  and data buffer.
572789Sahrens  *		ioflag	- FAPPEND flag set if in append mode.
573789Sahrens  *		cr	- credentials of caller.
5745331Samw  *		ct	- caller context (NFS/CIFS fem monitor only)
575789Sahrens  *
576789Sahrens  *	OUT:	uio	- updated offset and range.
577789Sahrens  *
578789Sahrens  *	RETURN:	0 if success
579789Sahrens  *		error code if failure
580789Sahrens  *
581789Sahrens  * Timestamps:
582789Sahrens  *	vp - ctime|mtime updated if byte count > 0
583789Sahrens  */
58411935SMark.Shellenbaum@Sun.COM 
585789Sahrens /* ARGSUSED */
586789Sahrens static int
zfs_write(vnode_t * vp,uio_t * uio,int ioflag,cred_t * cr,caller_context_t * ct)587789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
588789Sahrens {
589789Sahrens 	znode_t		*zp = VTOZ(vp);
590789Sahrens 	rlim64_t	limit = uio->uio_llimit;
591789Sahrens 	ssize_t		start_resid = uio->uio_resid;
592789Sahrens 	ssize_t		tx_bytes;
593789Sahrens 	uint64_t	end_size;
594789Sahrens 	dmu_tx_t	*tx;
595789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
5965326Sek110237 	zilog_t		*zilog;
597789Sahrens 	offset_t	woff;
598789Sahrens 	ssize_t		n, nbytes;
5991669Sperrin 	rl_t		*rl;
600789Sahrens 	int		max_blksz = zfsvfs->z_max_blksz;
6011669Sperrin 	int		error;
6029412SAleksandr.Guzovskiy@Sun.COM 	arc_buf_t	*abuf;
60311539SChunli.Zhang@Sun.COM 	iovec_t		*aiov;
60411539SChunli.Zhang@Sun.COM 	xuio_t		*xuio = NULL;
60511539SChunli.Zhang@Sun.COM 	int		i_iov = 0;
60611539SChunli.Zhang@Sun.COM 	int		iovcnt = uio->uio_iovcnt;
60711539SChunli.Zhang@Sun.COM 	iovec_t		*iovp = uio->uio_iov;
60811539SChunli.Zhang@Sun.COM 	int		write_eof;
60911935SMark.Shellenbaum@Sun.COM 	int		count = 0;
61011935SMark.Shellenbaum@Sun.COM 	sa_bulk_attr_t	bulk[4];
61111935SMark.Shellenbaum@Sun.COM 	uint64_t	mtime[2], ctime[2];
612789Sahrens 
613789Sahrens 	/*
614789Sahrens 	 * Fasttrack empty write
615789Sahrens 	 */
6161669Sperrin 	n = start_resid;
617789Sahrens 	if (n == 0)
618789Sahrens 		return (0);
619789Sahrens 
6201669Sperrin 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
6211669Sperrin 		limit = MAXOFFSET_T;
6221669Sperrin 
6235367Sahrens 	ZFS_ENTER(zfsvfs);
6245367Sahrens 	ZFS_VERIFY_ZP(zp);
6256743Smarks 
62611935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
62711935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
62811935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
62911935SMark.Shellenbaum@Sun.COM 	    &zp->z_size, 8);
63011935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
63111935SMark.Shellenbaum@Sun.COM 	    &zp->z_pflags, 8);
63211935SMark.Shellenbaum@Sun.COM 
6336743Smarks 	/*
6346743Smarks 	 * If immutable or not appending then return EPERM
6356743Smarks 	 */
63611935SMark.Shellenbaum@Sun.COM 	if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
63711935SMark.Shellenbaum@Sun.COM 	    ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
63811935SMark.Shellenbaum@Sun.COM 	    (uio->uio_loffset < zp->z_size))) {
6396743Smarks 		ZFS_EXIT(zfsvfs);
6406743Smarks 		return (EPERM);
6416743Smarks 	}
6426743Smarks 
6435326Sek110237 	zilog = zfsvfs->z_log;
644789Sahrens 
645789Sahrens 	/*
64611083Swilliam.gorrell@sun.com 	 * Validate file offset
64711083Swilliam.gorrell@sun.com 	 */
64811935SMark.Shellenbaum@Sun.COM 	woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
64911083Swilliam.gorrell@sun.com 	if (woff < 0) {
65011083Swilliam.gorrell@sun.com 		ZFS_EXIT(zfsvfs);
65111083Swilliam.gorrell@sun.com 		return (EINVAL);
65211083Swilliam.gorrell@sun.com 	}
65311083Swilliam.gorrell@sun.com 
65411083Swilliam.gorrell@sun.com 	/*
65511083Swilliam.gorrell@sun.com 	 * Check for mandatory locks before calling zfs_range_lock()
65611083Swilliam.gorrell@sun.com 	 * in order to prevent a deadlock with locks set via fcntl().
65711083Swilliam.gorrell@sun.com 	 */
65811935SMark.Shellenbaum@Sun.COM 	if (MANDMODE((mode_t)zp->z_mode) &&
65911083Swilliam.gorrell@sun.com 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
66011083Swilliam.gorrell@sun.com 		ZFS_EXIT(zfsvfs);
66111083Swilliam.gorrell@sun.com 		return (error);
66211083Swilliam.gorrell@sun.com 	}
66311083Swilliam.gorrell@sun.com 
66411083Swilliam.gorrell@sun.com 	/*
6652237Smaybee 	 * Pre-fault the pages to ensure slow (eg NFS) pages
6661669Sperrin 	 * don't hold up txg.
66711539SChunli.Zhang@Sun.COM 	 * Skip this if uio contains loaned arc_buf.
668789Sahrens 	 */
66911539SChunli.Zhang@Sun.COM 	if ((uio->uio_extflg == UIO_XUIO) &&
67011539SChunli.Zhang@Sun.COM 	    (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
67111539SChunli.Zhang@Sun.COM 		xuio = (xuio_t *)uio;
67211539SChunli.Zhang@Sun.COM 	else
67312829SSanjeev.Bagewadi@Sun.COM 		uio_prefaultpages(MIN(n, max_blksz), uio);
674789Sahrens 
675789Sahrens 	/*
676789Sahrens 	 * If in append mode, set the io offset pointer to eof.
677789Sahrens 	 */
6781669Sperrin 	if (ioflag & FAPPEND) {
6791669Sperrin 		/*
68011083Swilliam.gorrell@sun.com 		 * Obtain an appending range lock to guarantee file append
68111083Swilliam.gorrell@sun.com 		 * semantics.  We reset the write offset once we have the lock.
6821669Sperrin 		 */
6831669Sperrin 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
68411083Swilliam.gorrell@sun.com 		woff = rl->r_off;
6851669Sperrin 		if (rl->r_len == UINT64_MAX) {
68611083Swilliam.gorrell@sun.com 			/*
68711083Swilliam.gorrell@sun.com 			 * We overlocked the file because this write will cause
68811083Swilliam.gorrell@sun.com 			 * the file block size to increase.
68911083Swilliam.gorrell@sun.com 			 * Note that zp_size cannot change with this lock held.
69011083Swilliam.gorrell@sun.com 			 */
69111935SMark.Shellenbaum@Sun.COM 			woff = zp->z_size;
6921669Sperrin 		}
69311083Swilliam.gorrell@sun.com 		uio->uio_loffset = woff;
694789Sahrens 	} else {
695789Sahrens 		/*
69611083Swilliam.gorrell@sun.com 		 * Note that if the file block size will change as a result of
69711083Swilliam.gorrell@sun.com 		 * this write, then this range lock will lock the entire file
69811083Swilliam.gorrell@sun.com 		 * so that we can re-write the block safely.
699789Sahrens 		 */
7001669Sperrin 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
701789Sahrens 	}
702789Sahrens 
703789Sahrens 	if (woff >= limit) {
7043638Sbillm 		zfs_range_unlock(rl);
7053638Sbillm 		ZFS_EXIT(zfsvfs);
7063638Sbillm 		return (EFBIG);
707789Sahrens 	}
708789Sahrens 
709789Sahrens 	if ((woff + n) > limit || woff > (limit - n))
710789Sahrens 		n = limit - woff;
711789Sahrens 
71211539SChunli.Zhang@Sun.COM 	/* Will this write extend the file length? */
71311935SMark.Shellenbaum@Sun.COM 	write_eof = (woff + n > zp->z_size);
71411935SMark.Shellenbaum@Sun.COM 
71511935SMark.Shellenbaum@Sun.COM 	end_size = MAX(zp->z_size, woff + n);
716789Sahrens 
7171669Sperrin 	/*
7183638Sbillm 	 * Write the file in reasonable size chunks.  Each chunk is written
7193638Sbillm 	 * in a separate transaction; this keeps the intent log records small
7203638Sbillm 	 * and allows us to do more fine-grained space accounting.
721789Sahrens 	 */
722789Sahrens 	while (n > 0) {
7239412SAleksandr.Guzovskiy@Sun.COM 		abuf = NULL;
7249412SAleksandr.Guzovskiy@Sun.COM 		woff = uio->uio_loffset;
7259412SAleksandr.Guzovskiy@Sun.COM again:
72611935SMark.Shellenbaum@Sun.COM 		if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
72711935SMark.Shellenbaum@Sun.COM 		    zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
7289412SAleksandr.Guzovskiy@Sun.COM 			if (abuf != NULL)
7299412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7309396SMatthew.Ahrens@Sun.COM 			error = EDQUOT;
7319396SMatthew.Ahrens@Sun.COM 			break;
7329396SMatthew.Ahrens@Sun.COM 		}
7339412SAleksandr.Guzovskiy@Sun.COM 
73411539SChunli.Zhang@Sun.COM 		if (xuio && abuf == NULL) {
73511539SChunli.Zhang@Sun.COM 			ASSERT(i_iov < iovcnt);
73611539SChunli.Zhang@Sun.COM 			aiov = &iovp[i_iov];
73711539SChunli.Zhang@Sun.COM 			abuf = dmu_xuio_arcbuf(xuio, i_iov);
73811539SChunli.Zhang@Sun.COM 			dmu_xuio_clear(xuio, i_iov);
73911539SChunli.Zhang@Sun.COM 			DTRACE_PROBE3(zfs_cp_write, int, i_iov,
74011539SChunli.Zhang@Sun.COM 			    iovec_t *, aiov, arc_buf_t *, abuf);
74111539SChunli.Zhang@Sun.COM 			ASSERT((aiov->iov_base == abuf->b_data) ||
74211539SChunli.Zhang@Sun.COM 			    ((char *)aiov->iov_base - (char *)abuf->b_data +
74311539SChunli.Zhang@Sun.COM 			    aiov->iov_len == arc_buf_size(abuf)));
74411539SChunli.Zhang@Sun.COM 			i_iov++;
74511539SChunli.Zhang@Sun.COM 		} else if (abuf == NULL && n >= max_blksz &&
74611935SMark.Shellenbaum@Sun.COM 		    woff >= zp->z_size &&
7479412SAleksandr.Guzovskiy@Sun.COM 		    P2PHASE(woff, max_blksz) == 0 &&
7489412SAleksandr.Guzovskiy@Sun.COM 		    zp->z_blksz == max_blksz) {
74911539SChunli.Zhang@Sun.COM 			/*
75011539SChunli.Zhang@Sun.COM 			 * This write covers a full block.  "Borrow" a buffer
75111539SChunli.Zhang@Sun.COM 			 * from the dmu so that we can fill it before we enter
75211539SChunli.Zhang@Sun.COM 			 * a transaction.  This avoids the possibility of
75311539SChunli.Zhang@Sun.COM 			 * holding up the transaction if the data copy hangs
75411539SChunli.Zhang@Sun.COM 			 * up on a pagefault (e.g., from an NFS server mapping).
75511539SChunli.Zhang@Sun.COM 			 */
7569412SAleksandr.Guzovskiy@Sun.COM 			size_t cbytes;
7579412SAleksandr.Guzovskiy@Sun.COM 
75811935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
75911935SMark.Shellenbaum@Sun.COM 			    max_blksz);
7609412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(abuf != NULL);
7619412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(arc_buf_size(abuf) == max_blksz);
7629412SAleksandr.Guzovskiy@Sun.COM 			if (error = uiocopy(abuf->b_data, max_blksz,
7639412SAleksandr.Guzovskiy@Sun.COM 			    UIO_WRITE, uio, &cbytes)) {
7649412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7659412SAleksandr.Guzovskiy@Sun.COM 				break;
7669412SAleksandr.Guzovskiy@Sun.COM 			}
7679412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(cbytes == max_blksz);
7689412SAleksandr.Guzovskiy@Sun.COM 		}
7699412SAleksandr.Guzovskiy@Sun.COM 
7709412SAleksandr.Guzovskiy@Sun.COM 		/*
7719412SAleksandr.Guzovskiy@Sun.COM 		 * Start a transaction.
7729412SAleksandr.Guzovskiy@Sun.COM 		 */
773789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
77411935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
775789Sahrens 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
77611935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, zp);
7778227SNeil.Perrin@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
778789Sahrens 		if (error) {
7798227SNeil.Perrin@Sun.COM 			if (error == ERESTART) {
7802113Sahrens 				dmu_tx_wait(tx);
7812113Sahrens 				dmu_tx_abort(tx);
7829412SAleksandr.Guzovskiy@Sun.COM 				goto again;
783789Sahrens 			}
7842113Sahrens 			dmu_tx_abort(tx);
7859412SAleksandr.Guzovskiy@Sun.COM 			if (abuf != NULL)
7869412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7873638Sbillm 			break;
7883638Sbillm 		}
7893638Sbillm 
7903638Sbillm 		/*
7913638Sbillm 		 * If zfs_range_lock() over-locked we grow the blocksize
7923638Sbillm 		 * and then reduce the lock range.  This will only happen
7933638Sbillm 		 * on the first iteration since zfs_range_reduce() will
7943638Sbillm 		 * shrink down r_len to the appropriate size.
7953638Sbillm 		 */
7963638Sbillm 		if (rl->r_len == UINT64_MAX) {
7973638Sbillm 			uint64_t new_blksz;
7983638Sbillm 
7993638Sbillm 			if (zp->z_blksz > max_blksz) {
8003638Sbillm 				ASSERT(!ISP2(zp->z_blksz));
8013638Sbillm 				new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
8023638Sbillm 			} else {
8033638Sbillm 				new_blksz = MIN(end_size, max_blksz);
8043638Sbillm 			}
8053638Sbillm 			zfs_grow_blocksize(zp, new_blksz, tx);
8063638Sbillm 			zfs_range_reduce(rl, woff, n);
8073638Sbillm 		}
8083638Sbillm 
8093638Sbillm 		/*
8103638Sbillm 		 * XXX - should we really limit each write to z_max_blksz?
8113638Sbillm 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
8123638Sbillm 		 */
8133638Sbillm 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
8143638Sbillm 
8159412SAleksandr.Guzovskiy@Sun.COM 		if (abuf == NULL) {
8169412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes = uio->uio_resid;
81712123STim.Haley@Sun.COM 			error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
81812123STim.Haley@Sun.COM 			    uio, nbytes, tx);
8199412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes -= uio->uio_resid;
8209412SAleksandr.Guzovskiy@Sun.COM 		} else {
8219412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes = nbytes;
82211539SChunli.Zhang@Sun.COM 			ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
82311539SChunli.Zhang@Sun.COM 			/*
82411539SChunli.Zhang@Sun.COM 			 * If this is not a full block write, but we are
82511539SChunli.Zhang@Sun.COM 			 * extending the file past EOF and this data starts
82611539SChunli.Zhang@Sun.COM 			 * block-aligned, use assign_arcbuf().  Otherwise,
82711539SChunli.Zhang@Sun.COM 			 * write via dmu_write().
82811539SChunli.Zhang@Sun.COM 			 */
82911539SChunli.Zhang@Sun.COM 			if (tx_bytes < max_blksz && (!write_eof ||
83011539SChunli.Zhang@Sun.COM 			    aiov->iov_base != abuf->b_data)) {
83111539SChunli.Zhang@Sun.COM 				ASSERT(xuio);
83211539SChunli.Zhang@Sun.COM 				dmu_write(zfsvfs->z_os, zp->z_id, woff,
83311539SChunli.Zhang@Sun.COM 				    aiov->iov_len, aiov->iov_base, tx);
83411539SChunli.Zhang@Sun.COM 				dmu_return_arcbuf(abuf);
83511539SChunli.Zhang@Sun.COM 				xuio_stat_wbuf_copied();
83611539SChunli.Zhang@Sun.COM 			} else {
83711539SChunli.Zhang@Sun.COM 				ASSERT(xuio || tx_bytes == max_blksz);
83811935SMark.Shellenbaum@Sun.COM 				dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
83911935SMark.Shellenbaum@Sun.COM 				    woff, abuf, tx);
84011539SChunli.Zhang@Sun.COM 			}
8419412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(tx_bytes <= uio->uio_resid);
8429412SAleksandr.Guzovskiy@Sun.COM 			uioskip(uio, tx_bytes);
8439412SAleksandr.Guzovskiy@Sun.COM 		}
8449412SAleksandr.Guzovskiy@Sun.COM 		if (tx_bytes && vn_has_cached_data(vp)) {
8458636SMark.Maybee@Sun.COM 			update_pages(vp, woff,
8468636SMark.Maybee@Sun.COM 			    tx_bytes, zfsvfs->z_os, zp->z_id);
8479412SAleksandr.Guzovskiy@Sun.COM 		}
8483638Sbillm 
8493638Sbillm 		/*
8503638Sbillm 		 * If we made no progress, we're done.  If we made even
8513638Sbillm 		 * partial progress, update the znode and ZIL accordingly.
8523638Sbillm 		 */
8533638Sbillm 		if (tx_bytes == 0) {
85411935SMark.Shellenbaum@Sun.COM 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
85511935SMark.Shellenbaum@Sun.COM 			    (void *)&zp->z_size, sizeof (uint64_t), tx);
8563897Smaybee 			dmu_tx_commit(tx);
8573638Sbillm 			ASSERT(error != 0);
8583638Sbillm 			break;
8593638Sbillm 		}
8603638Sbillm 
861789Sahrens 		/*
8623638Sbillm 		 * Clear Set-UID/Set-GID bits on successful write if not
8633638Sbillm 		 * privileged and at least one of the excute bits is set.
8643638Sbillm 		 *
8653638Sbillm 		 * It would be nice to to this after all writes have
8663638Sbillm 		 * been done, but that would still expose the ISUID/ISGID
8673638Sbillm 		 * to another app after the partial write is committed.
8685331Samw 		 *
86913069SMark.Shellenbaum@Oracle.COM 		 * Note: we don't call zfs_fuid_map_id() here because
87013069SMark.Shellenbaum@Oracle.COM 		 * user 0 is not an ephemeral uid.
871789Sahrens 		 */
8723638Sbillm 		mutex_enter(&zp->z_acl_lock);
87311935SMark.Shellenbaum@Sun.COM 		if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
8743638Sbillm 		    (S_IXUSR >> 6))) != 0 &&
87511935SMark.Shellenbaum@Sun.COM 		    (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
8763638Sbillm 		    secpolicy_vnode_setid_retain(cr,
87711935SMark.Shellenbaum@Sun.COM 		    (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
87811935SMark.Shellenbaum@Sun.COM 			uint64_t newmode;
87911935SMark.Shellenbaum@Sun.COM 			zp->z_mode &= ~(S_ISUID | S_ISGID);
88011935SMark.Shellenbaum@Sun.COM 			newmode = zp->z_mode;
88111935SMark.Shellenbaum@Sun.COM 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
88211935SMark.Shellenbaum@Sun.COM 			    (void *)&newmode, sizeof (uint64_t), tx);
8833638Sbillm 		}
8843638Sbillm 		mutex_exit(&zp->z_acl_lock);
8853638Sbillm 
88611935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
88711935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
8883638Sbillm 
8893638Sbillm 		/*
8903638Sbillm 		 * Update the file size (zp_size) if it has changed;
8913638Sbillm 		 * account for possible concurrent updates.
8923638Sbillm 		 */
89311935SMark.Shellenbaum@Sun.COM 		while ((end_size = zp->z_size) < uio->uio_loffset) {
89411935SMark.Shellenbaum@Sun.COM 			(void) atomic_cas_64(&zp->z_size, end_size,
895789Sahrens 			    uio->uio_loffset);
89611935SMark.Shellenbaum@Sun.COM 			ASSERT(error == 0);
89711935SMark.Shellenbaum@Sun.COM 		}
89812772SNeil.Perrin@Sun.COM 		/*
89912772SNeil.Perrin@Sun.COM 		 * If we are replaying and eof is non zero then force
90012772SNeil.Perrin@Sun.COM 		 * the file size to the specified eof. Note, there's no
90112772SNeil.Perrin@Sun.COM 		 * concurrency during replay.
90212772SNeil.Perrin@Sun.COM 		 */
90312772SNeil.Perrin@Sun.COM 		if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
90412772SNeil.Perrin@Sun.COM 			zp->z_size = zfsvfs->z_replay_eof;
90512772SNeil.Perrin@Sun.COM 
90611935SMark.Shellenbaum@Sun.COM 		error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
90711935SMark.Shellenbaum@Sun.COM 
9083638Sbillm 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
9093638Sbillm 		dmu_tx_commit(tx);
9103638Sbillm 
9113638Sbillm 		if (error != 0)
9123638Sbillm 			break;
9133638Sbillm 		ASSERT(tx_bytes == nbytes);
9143638Sbillm 		n -= nbytes;
91512829SSanjeev.Bagewadi@Sun.COM 
91612829SSanjeev.Bagewadi@Sun.COM 		if (!xuio && n > 0)
91712829SSanjeev.Bagewadi@Sun.COM 			uio_prefaultpages(MIN(n, max_blksz), uio);
918789Sahrens 	}
919789Sahrens 
9202237Smaybee 	zfs_range_unlock(rl);
921789Sahrens 
922789Sahrens 	/*
923789Sahrens 	 * If we're in replay mode, or we made no progress, return error.
924789Sahrens 	 * Otherwise, it's at least a partial write, so it's successful.
925789Sahrens 	 */
9268227SNeil.Perrin@Sun.COM 	if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
927789Sahrens 		ZFS_EXIT(zfsvfs);
928789Sahrens 		return (error);
929789Sahrens 	}
930789Sahrens 
93112294SMark.Musante@Sun.COM 	if (ioflag & (FSYNC | FDSYNC) ||
93212294SMark.Musante@Sun.COM 	    zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
93312699SNeil.Perrin@Sun.COM 		zil_commit(zilog, zp->z_id);
934789Sahrens 
935789Sahrens 	ZFS_EXIT(zfsvfs);
936789Sahrens 	return (0);
937789Sahrens }
938789Sahrens 
9392237Smaybee void
zfs_get_done(zgd_t * zgd,int error)94010922SJeff.Bonwick@Sun.COM zfs_get_done(zgd_t *zgd, int error)
9412237Smaybee {
94210922SJeff.Bonwick@Sun.COM 	znode_t *zp = zgd->zgd_private;
94310922SJeff.Bonwick@Sun.COM 	objset_t *os = zp->z_zfsvfs->z_os;
94410922SJeff.Bonwick@Sun.COM 
94510922SJeff.Bonwick@Sun.COM 	if (zgd->zgd_db)
94610922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(zgd->zgd_db, zgd);
94710922SJeff.Bonwick@Sun.COM 
94810922SJeff.Bonwick@Sun.COM 	zfs_range_unlock(zgd->zgd_rl);
94910922SJeff.Bonwick@Sun.COM 
9509321SNeil.Perrin@Sun.COM 	/*
9519321SNeil.Perrin@Sun.COM 	 * Release the vnode asynchronously as we currently have the
9529321SNeil.Perrin@Sun.COM 	 * txg stopped from syncing.
9539321SNeil.Perrin@Sun.COM 	 */
95410922SJeff.Bonwick@Sun.COM 	VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
95510922SJeff.Bonwick@Sun.COM 
95610922SJeff.Bonwick@Sun.COM 	if (error == 0 && zgd->zgd_bp)
95710922SJeff.Bonwick@Sun.COM 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
95810922SJeff.Bonwick@Sun.COM 
9593063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
9602237Smaybee }
9612237Smaybee 
96210209SMark.Musante@Sun.COM #ifdef DEBUG
96310209SMark.Musante@Sun.COM static int zil_fault_io = 0;
96410209SMark.Musante@Sun.COM #endif
96510209SMark.Musante@Sun.COM 
966789Sahrens /*
967789Sahrens  * Get data to generate a TX_WRITE intent log record.
968789Sahrens  */
969789Sahrens int
zfs_get_data(void * arg,lr_write_t * lr,char * buf,zio_t * zio)9702237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
971789Sahrens {
972789Sahrens 	zfsvfs_t *zfsvfs = arg;
973789Sahrens 	objset_t *os = zfsvfs->z_os;
974789Sahrens 	znode_t *zp;
97510922SJeff.Bonwick@Sun.COM 	uint64_t object = lr->lr_foid;
97610922SJeff.Bonwick@Sun.COM 	uint64_t offset = lr->lr_offset;
97710922SJeff.Bonwick@Sun.COM 	uint64_t size = lr->lr_length;
97810922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = &lr->lr_blkptr;
9792237Smaybee 	dmu_buf_t *db;
9803063Sperrin 	zgd_t *zgd;
981789Sahrens 	int error = 0;
982789Sahrens 
98310922SJeff.Bonwick@Sun.COM 	ASSERT(zio != NULL);
98410922SJeff.Bonwick@Sun.COM 	ASSERT(size != 0);
985789Sahrens 
986789Sahrens 	/*
9871669Sperrin 	 * Nothing to do if the file has been removed
988789Sahrens 	 */
98910922SJeff.Bonwick@Sun.COM 	if (zfs_zget(zfsvfs, object, &zp) != 0)
990789Sahrens 		return (ENOENT);
9913461Sahrens 	if (zp->z_unlinked) {
9929321SNeil.Perrin@Sun.COM 		/*
9939321SNeil.Perrin@Sun.COM 		 * Release the vnode asynchronously as we currently have the
9949321SNeil.Perrin@Sun.COM 		 * txg stopped from syncing.
9959321SNeil.Perrin@Sun.COM 		 */
9969321SNeil.Perrin@Sun.COM 		VN_RELE_ASYNC(ZTOV(zp),
9979321SNeil.Perrin@Sun.COM 		    dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
998789Sahrens 		return (ENOENT);
999789Sahrens 	}
1000789Sahrens 
100110922SJeff.Bonwick@Sun.COM 	zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
100210922SJeff.Bonwick@Sun.COM 	zgd->zgd_zilog = zfsvfs->z_log;
100310922SJeff.Bonwick@Sun.COM 	zgd->zgd_private = zp;
100410922SJeff.Bonwick@Sun.COM 
1005789Sahrens 	/*
1006789Sahrens 	 * Write records come in two flavors: immediate and indirect.
1007789Sahrens 	 * For small writes it's cheaper to store the data with the
1008789Sahrens 	 * log record (immediate); for large writes it's cheaper to
1009789Sahrens 	 * sync the data and get a pointer to it (indirect) so that
1010789Sahrens 	 * we don't have to write the data twice.
1011789Sahrens 	 */
10121669Sperrin 	if (buf != NULL) { /* immediate write */
101310922SJeff.Bonwick@Sun.COM 		zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
10141669Sperrin 		/* test for truncation needs to be done while range locked */
101511935SMark.Shellenbaum@Sun.COM 		if (offset >= zp->z_size) {
10161669Sperrin 			error = ENOENT;
101710922SJeff.Bonwick@Sun.COM 		} else {
101810922SJeff.Bonwick@Sun.COM 			error = dmu_read(os, object, offset, size, buf,
101910922SJeff.Bonwick@Sun.COM 			    DMU_READ_NO_PREFETCH);
10201669Sperrin 		}
102110922SJeff.Bonwick@Sun.COM 		ASSERT(error == 0 || error == ENOENT);
10221669Sperrin 	} else { /* indirect write */
1023789Sahrens 		/*
10241669Sperrin 		 * Have to lock the whole block to ensure when it's
10251669Sperrin 		 * written out and it's checksum is being calculated
10261669Sperrin 		 * that no one can change the data. We need to re-check
10271669Sperrin 		 * blocksize after we get the lock in case it's changed!
1028789Sahrens 		 */
10291669Sperrin 		for (;;) {
103010922SJeff.Bonwick@Sun.COM 			uint64_t blkoff;
103110922SJeff.Bonwick@Sun.COM 			size = zp->z_blksz;
103210945SJeff.Bonwick@Sun.COM 			blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
103310922SJeff.Bonwick@Sun.COM 			offset -= blkoff;
103410922SJeff.Bonwick@Sun.COM 			zgd->zgd_rl = zfs_range_lock(zp, offset, size,
103510922SJeff.Bonwick@Sun.COM 			    RL_READER);
103610922SJeff.Bonwick@Sun.COM 			if (zp->z_blksz == size)
10371669Sperrin 				break;
103810922SJeff.Bonwick@Sun.COM 			offset += blkoff;
103910922SJeff.Bonwick@Sun.COM 			zfs_range_unlock(zgd->zgd_rl);
10401669Sperrin 		}
10411669Sperrin 		/* test for truncation needs to be done while range locked */
104211935SMark.Shellenbaum@Sun.COM 		if (lr->lr_offset >= zp->z_size)
10431669Sperrin 			error = ENOENT;
104410209SMark.Musante@Sun.COM #ifdef DEBUG
104510209SMark.Musante@Sun.COM 		if (zil_fault_io) {
104610209SMark.Musante@Sun.COM 			error = EIO;
104710209SMark.Musante@Sun.COM 			zil_fault_io = 0;
104810209SMark.Musante@Sun.COM 		}
104910209SMark.Musante@Sun.COM #endif
105010922SJeff.Bonwick@Sun.COM 		if (error == 0)
105112285SJeff.Bonwick@Sun.COM 			error = dmu_buf_hold(os, object, offset, zgd, &db,
105212285SJeff.Bonwick@Sun.COM 			    DMU_READ_NO_PREFETCH);
105310922SJeff.Bonwick@Sun.COM 
105410800SNeil.Perrin@Sun.COM 		if (error == 0) {
105510922SJeff.Bonwick@Sun.COM 			zgd->zgd_db = db;
105610922SJeff.Bonwick@Sun.COM 			zgd->zgd_bp = bp;
105710922SJeff.Bonwick@Sun.COM 
105810922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_offset == offset);
105910922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_size == size);
106010922SJeff.Bonwick@Sun.COM 
106110922SJeff.Bonwick@Sun.COM 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
106210922SJeff.Bonwick@Sun.COM 			    zfs_get_done, zgd);
106310922SJeff.Bonwick@Sun.COM 			ASSERT(error || lr->lr_length <= zp->z_blksz);
106410922SJeff.Bonwick@Sun.COM 
106510800SNeil.Perrin@Sun.COM 			/*
106610922SJeff.Bonwick@Sun.COM 			 * On success, we need to wait for the write I/O
106710922SJeff.Bonwick@Sun.COM 			 * initiated by dmu_sync() to complete before we can
106810922SJeff.Bonwick@Sun.COM 			 * release this dbuf.  We will finish everything up
106910922SJeff.Bonwick@Sun.COM 			 * in the zfs_get_done() callback.
107010800SNeil.Perrin@Sun.COM 			 */
107110922SJeff.Bonwick@Sun.COM 			if (error == 0)
107210922SJeff.Bonwick@Sun.COM 				return (0);
107310922SJeff.Bonwick@Sun.COM 
107410922SJeff.Bonwick@Sun.COM 			if (error == EALREADY) {
107510922SJeff.Bonwick@Sun.COM 				lr->lr_common.lrc_txtype = TX_WRITE2;
107610922SJeff.Bonwick@Sun.COM 				error = 0;
107710922SJeff.Bonwick@Sun.COM 			}
107810800SNeil.Perrin@Sun.COM 		}
1079789Sahrens 	}
108010922SJeff.Bonwick@Sun.COM 
108110922SJeff.Bonwick@Sun.COM 	zfs_get_done(zgd, error);
108210922SJeff.Bonwick@Sun.COM 
1083789Sahrens 	return (error);
1084789Sahrens }
1085789Sahrens 
1086789Sahrens /*ARGSUSED*/
1087789Sahrens static int
zfs_access(vnode_t * vp,int mode,int flag,cred_t * cr,caller_context_t * ct)10885331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
10895331Samw     caller_context_t *ct)
1090789Sahrens {
1091789Sahrens 	znode_t *zp = VTOZ(vp);
1092789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1093789Sahrens 	int error;
1094789Sahrens 
10955367Sahrens 	ZFS_ENTER(zfsvfs);
10965367Sahrens 	ZFS_VERIFY_ZP(zp);
10975331Samw 
10985331Samw 	if (flag & V_ACE_MASK)
10995331Samw 		error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
11005331Samw 	else
11015331Samw 		error = zfs_zaccess_rwx(zp, mode, flag, cr);
11025331Samw 
1103789Sahrens 	ZFS_EXIT(zfsvfs);
1104789Sahrens 	return (error);
1105789Sahrens }
1106789Sahrens 
1107789Sahrens /*
11089981STim.Haley@Sun.COM  * If vnode is for a device return a specfs vnode instead.
11099981STim.Haley@Sun.COM  */
11109981STim.Haley@Sun.COM static int
specvp_check(vnode_t ** vpp,cred_t * cr)11119981STim.Haley@Sun.COM specvp_check(vnode_t **vpp, cred_t *cr)
11129981STim.Haley@Sun.COM {
11139981STim.Haley@Sun.COM 	int error = 0;
11149981STim.Haley@Sun.COM 
11159981STim.Haley@Sun.COM 	if (IS_DEVVP(*vpp)) {
11169981STim.Haley@Sun.COM 		struct vnode *svp;
11179981STim.Haley@Sun.COM 
11189981STim.Haley@Sun.COM 		svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
11199981STim.Haley@Sun.COM 		VN_RELE(*vpp);
11209981STim.Haley@Sun.COM 		if (svp == NULL)
11219981STim.Haley@Sun.COM 			error = ENOSYS;
11229981STim.Haley@Sun.COM 		*vpp = svp;
11239981STim.Haley@Sun.COM 	}
11249981STim.Haley@Sun.COM 	return (error);
11259981STim.Haley@Sun.COM }
11269981STim.Haley@Sun.COM 
11279981STim.Haley@Sun.COM 
11289981STim.Haley@Sun.COM /*
1129789Sahrens  * Lookup an entry in a directory, or an extended attribute directory.
1130789Sahrens  * If it exists, return a held vnode reference for it.
1131789Sahrens  *
1132789Sahrens  *	IN:	dvp	- vnode of directory to search.
1133789Sahrens  *		nm	- name of entry to lookup.
1134789Sahrens  *		pnp	- full pathname to lookup [UNUSED].
1135789Sahrens  *		flags	- LOOKUP_XATTR set if looking for an attribute.
1136789Sahrens  *		rdir	- root directory vnode [UNUSED].
1137789Sahrens  *		cr	- credentials of caller.
11385331Samw  *		ct	- caller context
11395331Samw  *		direntflags - directory lookup flags
11405331Samw  *		realpnp - returned pathname.
1141789Sahrens  *
1142789Sahrens  *	OUT:	vpp	- vnode of located entry, NULL if not found.
1143789Sahrens  *
1144789Sahrens  *	RETURN:	0 if success
1145789Sahrens  *		error code if failure
1146789Sahrens  *
1147789Sahrens  * Timestamps:
1148789Sahrens  *	NA
1149789Sahrens  */
1150789Sahrens /* ARGSUSED */
1151789Sahrens static int
zfs_lookup(vnode_t * dvp,char * nm,vnode_t ** vpp,struct pathname * pnp,int flags,vnode_t * rdir,cred_t * cr,caller_context_t * ct,int * direntflags,pathname_t * realpnp)1152789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
11535331Samw     int flags, vnode_t *rdir, cred_t *cr,  caller_context_t *ct,
11545331Samw     int *direntflags, pathname_t *realpnp)
1155789Sahrens {
1156789Sahrens 	znode_t *zdp = VTOZ(dvp);
1157789Sahrens 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
11589981STim.Haley@Sun.COM 	int	error = 0;
11599981STim.Haley@Sun.COM 
11609981STim.Haley@Sun.COM 	/* fast path */
11619981STim.Haley@Sun.COM 	if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
11629981STim.Haley@Sun.COM 
11639981STim.Haley@Sun.COM 		if (dvp->v_type != VDIR) {
11649981STim.Haley@Sun.COM 			return (ENOTDIR);
116511935SMark.Shellenbaum@Sun.COM 		} else if (zdp->z_sa_hdl == NULL) {
11669981STim.Haley@Sun.COM 			return (EIO);
11679981STim.Haley@Sun.COM 		}
11689981STim.Haley@Sun.COM 
11699981STim.Haley@Sun.COM 		if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
11709981STim.Haley@Sun.COM 			error = zfs_fastaccesschk_execute(zdp, cr);
11719981STim.Haley@Sun.COM 			if (!error) {
11729981STim.Haley@Sun.COM 				*vpp = dvp;
11739981STim.Haley@Sun.COM 				VN_HOLD(*vpp);
11749981STim.Haley@Sun.COM 				return (0);
11759981STim.Haley@Sun.COM 			}
11769981STim.Haley@Sun.COM 			return (error);
11779981STim.Haley@Sun.COM 		} else {
11789981STim.Haley@Sun.COM 			vnode_t *tvp = dnlc_lookup(dvp, nm);
11799981STim.Haley@Sun.COM 
11809981STim.Haley@Sun.COM 			if (tvp) {
11819981STim.Haley@Sun.COM 				error = zfs_fastaccesschk_execute(zdp, cr);
11829981STim.Haley@Sun.COM 				if (error) {
11839981STim.Haley@Sun.COM 					VN_RELE(tvp);
11849981STim.Haley@Sun.COM 					return (error);
11859981STim.Haley@Sun.COM 				}
11869981STim.Haley@Sun.COM 				if (tvp == DNLC_NO_VNODE) {
11879981STim.Haley@Sun.COM 					VN_RELE(tvp);
11889981STim.Haley@Sun.COM 					return (ENOENT);
11899981STim.Haley@Sun.COM 				} else {
11909981STim.Haley@Sun.COM 					*vpp = tvp;
11919981STim.Haley@Sun.COM 					return (specvp_check(vpp, cr));
11929981STim.Haley@Sun.COM 				}
11939981STim.Haley@Sun.COM 			}
11949981STim.Haley@Sun.COM 		}
11959981STim.Haley@Sun.COM 	}
11969981STim.Haley@Sun.COM 
11979981STim.Haley@Sun.COM 	DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1198789Sahrens 
11995367Sahrens 	ZFS_ENTER(zfsvfs);
12005367Sahrens 	ZFS_VERIFY_ZP(zdp);
1201789Sahrens 
1202789Sahrens 	*vpp = NULL;
1203789Sahrens 
1204789Sahrens 	if (flags & LOOKUP_XATTR) {
1205789Sahrens 		/*
12063234Sck153898 		 * If the xattr property is off, refuse the lookup request.
12073234Sck153898 		 */
12083234Sck153898 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
12093234Sck153898 			ZFS_EXIT(zfsvfs);
12103234Sck153898 			return (EINVAL);
12113234Sck153898 		}
12123234Sck153898 
12133234Sck153898 		/*
1214789Sahrens 		 * We don't allow recursive attributes..
1215789Sahrens 		 * Maybe someday we will.
1216789Sahrens 		 */
121711935SMark.Shellenbaum@Sun.COM 		if (zdp->z_pflags & ZFS_XATTR) {
1218789Sahrens 			ZFS_EXIT(zfsvfs);
1219789Sahrens 			return (EINVAL);
1220789Sahrens 		}
1221789Sahrens 
12223280Sck153898 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1223789Sahrens 			ZFS_EXIT(zfsvfs);
1224789Sahrens 			return (error);
1225789Sahrens 		}
1226789Sahrens 
1227789Sahrens 		/*
1228789Sahrens 		 * Do we have permission to get into attribute directory?
1229789Sahrens 		 */
1230789Sahrens 
12315331Samw 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
12325331Samw 		    B_FALSE, cr)) {
1233789Sahrens 			VN_RELE(*vpp);
12345331Samw 			*vpp = NULL;
1235789Sahrens 		}
1236789Sahrens 
1237789Sahrens 		ZFS_EXIT(zfsvfs);
1238789Sahrens 		return (error);
1239789Sahrens 	}
1240789Sahrens 
12411512Sek110237 	if (dvp->v_type != VDIR) {
12421512Sek110237 		ZFS_EXIT(zfsvfs);
12431460Smarks 		return (ENOTDIR);
12441512Sek110237 	}
12451460Smarks 
1246789Sahrens 	/*
1247789Sahrens 	 * Check accessibility of directory.
1248789Sahrens 	 */
1249789Sahrens 
12505331Samw 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1251789Sahrens 		ZFS_EXIT(zfsvfs);
1252789Sahrens 		return (error);
1253789Sahrens 	}
1254789Sahrens 
12555498Stimh 	if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
12565331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
12575331Samw 		ZFS_EXIT(zfsvfs);
12585331Samw 		return (EILSEQ);
12595331Samw 	}
12605331Samw 
12615331Samw 	error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
12629981STim.Haley@Sun.COM 	if (error == 0)
12639981STim.Haley@Sun.COM 		error = specvp_check(vpp, cr);
1264789Sahrens 
1265789Sahrens 	ZFS_EXIT(zfsvfs);
1266789Sahrens 	return (error);
1267789Sahrens }
1268789Sahrens 
1269789Sahrens /*
1270789Sahrens  * Attempt to create a new entry in a directory.  If the entry
1271789Sahrens  * already exists, truncate the file if permissible, else return
1272789Sahrens  * an error.  Return the vp of the created or trunc'd file.
1273789Sahrens  *
1274789Sahrens  *	IN:	dvp	- vnode of directory to put new file entry in.
1275789Sahrens  *		name	- name of new file entry.
1276789Sahrens  *		vap	- attributes of new file.
1277789Sahrens  *		excl	- flag indicating exclusive or non-exclusive mode.
1278789Sahrens  *		mode	- mode to open file with.
1279789Sahrens  *		cr	- credentials of caller.
1280789Sahrens  *		flag	- large file flag [UNUSED].
12815331Samw  *		ct	- caller context
12825331Samw  *		vsecp 	- ACL to be set
1283789Sahrens  *
1284789Sahrens  *	OUT:	vpp	- vnode of created or trunc'd entry.
1285789Sahrens  *
1286789Sahrens  *	RETURN:	0 if success
1287789Sahrens  *		error code if failure
1288789Sahrens  *
1289789Sahrens  * Timestamps:
1290789Sahrens  *	dvp - ctime|mtime updated if new entry created
1291789Sahrens  *	 vp - ctime|mtime always, atime if new
1292789Sahrens  */
12935331Samw 
1294789Sahrens /* ARGSUSED */
1295789Sahrens static int
zfs_create(vnode_t * dvp,char * name,vattr_t * vap,vcexcl_t excl,int mode,vnode_t ** vpp,cred_t * cr,int flag,caller_context_t * ct,vsecattr_t * vsecp)1296789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
12975331Samw     int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
12985331Samw     vsecattr_t *vsecp)
1299789Sahrens {
1300789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1301789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
13025326Sek110237 	zilog_t		*zilog;
13035326Sek110237 	objset_t	*os;
1304789Sahrens 	zfs_dirlock_t	*dl;
1305789Sahrens 	dmu_tx_t	*tx;
1306789Sahrens 	int		error;
13077847SMark.Shellenbaum@Sun.COM 	ksid_t		*ksid;
13087847SMark.Shellenbaum@Sun.COM 	uid_t		uid;
13097847SMark.Shellenbaum@Sun.COM 	gid_t		gid = crgetgid(cr);
131011935SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t   acl_ids;
13119179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
131212302SMark.Shellenbaum@Sun.COM 	boolean_t	have_acl = B_FALSE;
13135331Samw 
13145331Samw 	/*
13155331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
13165331Samw 	 * make sure file system is at proper version
13175331Samw 	 */
13185331Samw 
13197847SMark.Shellenbaum@Sun.COM 	ksid = crgetsid(cr, KSID_OWNER);
13207847SMark.Shellenbaum@Sun.COM 	if (ksid)
13217847SMark.Shellenbaum@Sun.COM 		uid = ksid_getid(ksid);
13227847SMark.Shellenbaum@Sun.COM 	else
13237847SMark.Shellenbaum@Sun.COM 		uid = crgetuid(cr);
13247847SMark.Shellenbaum@Sun.COM 
13255331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
13265331Samw 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
13277847SMark.Shellenbaum@Sun.COM 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
13285331Samw 		return (EINVAL);
1329789Sahrens 
13305367Sahrens 	ZFS_ENTER(zfsvfs);
13315367Sahrens 	ZFS_VERIFY_ZP(dzp);
13325326Sek110237 	os = zfsvfs->z_os;
13335326Sek110237 	zilog = zfsvfs->z_log;
1334789Sahrens 
13355498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
13365331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
13375331Samw 		ZFS_EXIT(zfsvfs);
13385331Samw 		return (EILSEQ);
13395331Samw 	}
13405331Samw 
13415331Samw 	if (vap->va_mask & AT_XVATTR) {
13425331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
13435331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
13445331Samw 			ZFS_EXIT(zfsvfs);
13455331Samw 			return (error);
13465331Samw 		}
13475331Samw 	}
1348789Sahrens top:
1349789Sahrens 	*vpp = NULL;
1350789Sahrens 
1351789Sahrens 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1352789Sahrens 		vap->va_mode &= ~VSVTX;
1353789Sahrens 
1354789Sahrens 	if (*name == '\0') {
1355789Sahrens 		/*
1356789Sahrens 		 * Null component name refers to the directory itself.
1357789Sahrens 		 */
1358789Sahrens 		VN_HOLD(dvp);
1359789Sahrens 		zp = dzp;
1360789Sahrens 		dl = NULL;
1361789Sahrens 		error = 0;
1362789Sahrens 	} else {
1363789Sahrens 		/* possible VN_HOLD(zp) */
13645331Samw 		int zflg = 0;
13655331Samw 
13665331Samw 		if (flag & FIGNORECASE)
13675331Samw 			zflg |= ZCILOOK;
13685331Samw 
13695331Samw 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
13705331Samw 		    NULL, NULL);
13715331Samw 		if (error) {
1372*13089SMark.Shellenbaum@Oracle.COM 			if (have_acl)
1373*13089SMark.Shellenbaum@Oracle.COM 				zfs_acl_ids_free(&acl_ids);
1374789Sahrens 			if (strcmp(name, "..") == 0)
1375789Sahrens 				error = EISDIR;
1376789Sahrens 			ZFS_EXIT(zfsvfs);
1377789Sahrens 			return (error);
1378789Sahrens 		}
1379789Sahrens 	}
138011935SMark.Shellenbaum@Sun.COM 
1381789Sahrens 	if (zp == NULL) {
13825331Samw 		uint64_t txtype;
13835331Samw 
1384789Sahrens 		/*
1385789Sahrens 		 * Create a new file object and update the directory
1386789Sahrens 		 * to reference it.
1387789Sahrens 		 */
13885331Samw 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1389*13089SMark.Shellenbaum@Oracle.COM 			if (have_acl)
1390*13089SMark.Shellenbaum@Oracle.COM 				zfs_acl_ids_free(&acl_ids);
1391789Sahrens 			goto out;
1392789Sahrens 		}
1393789Sahrens 
1394789Sahrens 		/*
1395789Sahrens 		 * We only support the creation of regular files in
1396789Sahrens 		 * extended attribute directories.
1397789Sahrens 		 */
139811935SMark.Shellenbaum@Sun.COM 
139911935SMark.Shellenbaum@Sun.COM 		if ((dzp->z_pflags & ZFS_XATTR) &&
1400789Sahrens 		    (vap->va_type != VREG)) {
1401*13089SMark.Shellenbaum@Oracle.COM 			if (have_acl)
1402*13089SMark.Shellenbaum@Oracle.COM 				zfs_acl_ids_free(&acl_ids);
1403789Sahrens 			error = EINVAL;
1404789Sahrens 			goto out;
1405789Sahrens 		}
1406789Sahrens 
140712302SMark.Shellenbaum@Sun.COM 		if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
140812302SMark.Shellenbaum@Sun.COM 		    cr, vsecp, &acl_ids)) != 0)
14099179SMark.Shellenbaum@Sun.COM 			goto out;
141012302SMark.Shellenbaum@Sun.COM 		have_acl = B_TRUE;
141112302SMark.Shellenbaum@Sun.COM 
14129396SMatthew.Ahrens@Sun.COM 		if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
141310143STim.Haley@Sun.COM 			zfs_acl_ids_free(&acl_ids);
14149396SMatthew.Ahrens@Sun.COM 			error = EDQUOT;
14159396SMatthew.Ahrens@Sun.COM 			goto out;
14169396SMatthew.Ahrens@Sun.COM 		}
14179179SMark.Shellenbaum@Sun.COM 
1418789Sahrens 		tx = dmu_tx_create(os);
141911935SMark.Shellenbaum@Sun.COM 
142011935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
142111935SMark.Shellenbaum@Sun.COM 		    ZFS_SA_BASE_ATTR_SIZE);
142211935SMark.Shellenbaum@Sun.COM 
14239179SMark.Shellenbaum@Sun.COM 		fuid_dirtied = zfsvfs->z_fuid_dirty;
14249396SMatthew.Ahrens@Sun.COM 		if (fuid_dirtied)
14259396SMatthew.Ahrens@Sun.COM 			zfs_fuid_txhold(zfsvfs, tx);
14261544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
142711935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
142811935SMark.Shellenbaum@Sun.COM 		if (!zfsvfs->z_use_sa &&
142911935SMark.Shellenbaum@Sun.COM 		    acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1430789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
143111935SMark.Shellenbaum@Sun.COM 			    0, acl_ids.z_aclp->z_acl_bytes);
14325331Samw 		}
14338227SNeil.Perrin@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
1434789Sahrens 		if (error) {
1435789Sahrens 			zfs_dirent_unlock(dl);
14368227SNeil.Perrin@Sun.COM 			if (error == ERESTART) {
14372113Sahrens 				dmu_tx_wait(tx);
14382113Sahrens 				dmu_tx_abort(tx);
1439789Sahrens 				goto top;
1440789Sahrens 			}
144112302SMark.Shellenbaum@Sun.COM 			zfs_acl_ids_free(&acl_ids);
14422113Sahrens 			dmu_tx_abort(tx);
1443789Sahrens 			ZFS_EXIT(zfsvfs);
1444789Sahrens 			return (error);
1445789Sahrens 		}
144611935SMark.Shellenbaum@Sun.COM 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
14479179SMark.Shellenbaum@Sun.COM 
14489179SMark.Shellenbaum@Sun.COM 		if (fuid_dirtied)
14499179SMark.Shellenbaum@Sun.COM 			zfs_fuid_sync(zfsvfs, tx);
14509179SMark.Shellenbaum@Sun.COM 
1451789Sahrens 		(void) zfs_link_create(dl, zp, tx, ZNEW);
14525331Samw 		txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
14535331Samw 		if (flag & FIGNORECASE)
14545331Samw 			txtype |= TX_CI;
14555331Samw 		zfs_log_create(zilog, tx, txtype, dzp, zp, name,
14569179SMark.Shellenbaum@Sun.COM 		    vsecp, acl_ids.z_fuidp, vap);
14579179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
1458789Sahrens 		dmu_tx_commit(tx);
1459789Sahrens 	} else {
14605331Samw 		int aflags = (flag & FAPPEND) ? V_APPEND : 0;
14615331Samw 
1462*13089SMark.Shellenbaum@Oracle.COM 		if (have_acl)
1463*13089SMark.Shellenbaum@Oracle.COM 			zfs_acl_ids_free(&acl_ids);
1464*13089SMark.Shellenbaum@Oracle.COM 		have_acl = B_FALSE;
1465*13089SMark.Shellenbaum@Oracle.COM 
1466789Sahrens 		/*
1467789Sahrens 		 * A directory entry already exists for this name.
1468789Sahrens 		 */
1469789Sahrens 		/*
1470789Sahrens 		 * Can't truncate an existing file if in exclusive mode.
1471789Sahrens 		 */
1472789Sahrens 		if (excl == EXCL) {
1473789Sahrens 			error = EEXIST;
1474789Sahrens 			goto out;
1475789Sahrens 		}
1476789Sahrens 		/*
1477789Sahrens 		 * Can't open a directory for writing.
1478789Sahrens 		 */
1479789Sahrens 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1480789Sahrens 			error = EISDIR;
1481789Sahrens 			goto out;
1482789Sahrens 		}
1483789Sahrens 		/*
1484789Sahrens 		 * Verify requested access to file.
1485789Sahrens 		 */
14865331Samw 		if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1487789Sahrens 			goto out;
1488789Sahrens 		}
1489789Sahrens 
1490789Sahrens 		mutex_enter(&dzp->z_lock);
1491789Sahrens 		dzp->z_seq++;
1492789Sahrens 		mutex_exit(&dzp->z_lock);
1493789Sahrens 
14941878Smaybee 		/*
14951878Smaybee 		 * Truncate regular files if requested.
14961878Smaybee 		 */
14971878Smaybee 		if ((ZTOV(zp)->v_type == VREG) &&
1498789Sahrens 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
14996992Smaybee 			/* we can't hold any locks when calling zfs_freesp() */
15006992Smaybee 			zfs_dirent_unlock(dl);
15016992Smaybee 			dl = NULL;
15021878Smaybee 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
15034863Spraks 			if (error == 0) {
15045331Samw 				vnevent_create(ZTOV(zp), ct);
15054863Spraks 			}
1506789Sahrens 		}
1507789Sahrens 	}
1508789Sahrens out:
1509789Sahrens 
1510789Sahrens 	if (dl)
1511789Sahrens 		zfs_dirent_unlock(dl);
1512789Sahrens 
1513789Sahrens 	if (error) {
1514789Sahrens 		if (zp)
1515789Sahrens 			VN_RELE(ZTOV(zp));
1516789Sahrens 	} else {
1517789Sahrens 		*vpp = ZTOV(zp);
15189981STim.Haley@Sun.COM 		error = specvp_check(vpp, cr);
1519789Sahrens 	}
1520789Sahrens 
152112294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
152212699SNeil.Perrin@Sun.COM 		zil_commit(zilog, 0);
152312294SMark.Musante@Sun.COM 
1524789Sahrens 	ZFS_EXIT(zfsvfs);
1525789Sahrens 	return (error);
1526789Sahrens }
1527789Sahrens 
1528789Sahrens /*
1529789Sahrens  * Remove an entry from a directory.
1530789Sahrens  *
1531789Sahrens  *	IN:	dvp	- vnode of directory to remove entry from.
1532789Sahrens  *		name	- name of entry to remove.
1533789Sahrens  *		cr	- credentials of caller.
15345331Samw  *		ct	- caller context
15355331Samw  *		flags	- case flags
1536789Sahrens  *
1537789Sahrens  *	RETURN:	0 if success
1538789Sahrens  *		error code if failure
1539789Sahrens  *
1540789Sahrens  * Timestamps:
1541789Sahrens  *	dvp - ctime|mtime
1542789Sahrens  *	 vp - ctime (if nlink > 0)
1543789Sahrens  */
154411935SMark.Shellenbaum@Sun.COM 
154511935SMark.Shellenbaum@Sun.COM uint64_t null_xattr = 0;
154611935SMark.Shellenbaum@Sun.COM 
15475331Samw /*ARGSUSED*/
1548789Sahrens static int
zfs_remove(vnode_t * dvp,char * name,cred_t * cr,caller_context_t * ct,int flags)15495331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
15505331Samw     int flags)
1551789Sahrens {
1552789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1553*13089SMark.Shellenbaum@Oracle.COM 	znode_t		*xzp;
1554789Sahrens 	vnode_t		*vp;
1555789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
15565326Sek110237 	zilog_t		*zilog;
1557*13089SMark.Shellenbaum@Oracle.COM 	uint64_t	acl_obj, xattr_obj;
155811935SMark.Shellenbaum@Sun.COM 	uint64_t 	xattr_obj_unlinked = 0;
155912700SNeil.Perrin@Sun.COM 	uint64_t	obj = 0;
1560789Sahrens 	zfs_dirlock_t	*dl;
1561789Sahrens 	dmu_tx_t	*tx;
15623461Sahrens 	boolean_t	may_delete_now, delete_now = FALSE;
15636992Smaybee 	boolean_t	unlinked, toobig = FALSE;
15645331Samw 	uint64_t	txtype;
15655331Samw 	pathname_t	*realnmp = NULL;
15665331Samw 	pathname_t	realnm;
1567789Sahrens 	int		error;
15685331Samw 	int		zflg = ZEXISTS;
1569789Sahrens 
15705367Sahrens 	ZFS_ENTER(zfsvfs);
15715367Sahrens 	ZFS_VERIFY_ZP(dzp);
15725326Sek110237 	zilog = zfsvfs->z_log;
1573789Sahrens 
15745331Samw 	if (flags & FIGNORECASE) {
15755331Samw 		zflg |= ZCILOOK;
15765331Samw 		pn_alloc(&realnm);
15775331Samw 		realnmp = &realnm;
15785331Samw 	}
15795331Samw 
1580789Sahrens top:
1581*13089SMark.Shellenbaum@Oracle.COM 	xattr_obj = 0;
1582*13089SMark.Shellenbaum@Oracle.COM 	xzp = NULL;
1583789Sahrens 	/*
1584789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1585789Sahrens 	 */
15865331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
15875331Samw 	    NULL, realnmp)) {
15885331Samw 		if (realnmp)
15895331Samw 			pn_free(realnmp);
1590789Sahrens 		ZFS_EXIT(zfsvfs);
1591789Sahrens 		return (error);
1592789Sahrens 	}
1593789Sahrens 
1594789Sahrens 	vp = ZTOV(zp);
1595789Sahrens 
1596789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1597789Sahrens 		goto out;
1598789Sahrens 	}
1599789Sahrens 
1600789Sahrens 	/*
1601789Sahrens 	 * Need to use rmdir for removing directories.
1602789Sahrens 	 */
1603789Sahrens 	if (vp->v_type == VDIR) {
1604789Sahrens 		error = EPERM;
1605789Sahrens 		goto out;
1606789Sahrens 	}
1607789Sahrens 
16085331Samw 	vnevent_remove(vp, dvp, name, ct);
16095331Samw 
16105331Samw 	if (realnmp)
16116492Stimh 		dnlc_remove(dvp, realnmp->pn_buf);
16125331Samw 	else
16135331Samw 		dnlc_remove(dvp, name);
16141484Sek110237 
1615789Sahrens 	mutex_enter(&vp->v_lock);
1616789Sahrens 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1617789Sahrens 	mutex_exit(&vp->v_lock);
1618789Sahrens 
1619789Sahrens 	/*
16203461Sahrens 	 * We may delete the znode now, or we may put it in the unlinked set;
1621789Sahrens 	 * it depends on whether we're the last link, and on whether there are
1622789Sahrens 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1623789Sahrens 	 * allow for either case.
1624789Sahrens 	 */
162512700SNeil.Perrin@Sun.COM 	obj = zp->z_id;
1626789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
16271544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
162811935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
162911935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
163011935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, dzp);
16316992Smaybee 	if (may_delete_now) {
16326992Smaybee 		toobig =
163311935SMark.Shellenbaum@Sun.COM 		    zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
16346992Smaybee 		/* if the file is too big, only hold_free a token amount */
16356992Smaybee 		dmu_tx_hold_free(tx, zp->z_id, 0,
16366992Smaybee 		    (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
16376992Smaybee 	}
1638789Sahrens 
1639789Sahrens 	/* are there any extended attributes? */
164011935SMark.Shellenbaum@Sun.COM 	error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
164111935SMark.Shellenbaum@Sun.COM 	    &xattr_obj, sizeof (xattr_obj));
1642*13089SMark.Shellenbaum@Oracle.COM 	if (error == 0 && xattr_obj) {
164311935SMark.Shellenbaum@Sun.COM 		error = zfs_zget(zfsvfs, xattr_obj, &xzp);
164411935SMark.Shellenbaum@Sun.COM 		ASSERT3U(error, ==, 0);
164511935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
164611935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1647789Sahrens 	}
1648789Sahrens 
164912620SMark.Shellenbaum@Oracle.COM 	mutex_enter(&zp->z_lock);
165012620SMark.Shellenbaum@Oracle.COM 	if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1651789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
165212620SMark.Shellenbaum@Oracle.COM 	mutex_exit(&zp->z_lock);
1653789Sahrens 
1654789Sahrens 	/* charge as an update -- would be nice not to charge at all */
16553461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1656789Sahrens 
16578227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1658789Sahrens 	if (error) {
1659789Sahrens 		zfs_dirent_unlock(dl);
1660789Sahrens 		VN_RELE(vp);
1661*13089SMark.Shellenbaum@Oracle.COM 		if (xzp)
1662*13089SMark.Shellenbaum@Oracle.COM 			VN_RELE(ZTOV(xzp));
16638227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
16642113Sahrens 			dmu_tx_wait(tx);
16652113Sahrens 			dmu_tx_abort(tx);
1666789Sahrens 			goto top;
1667789Sahrens 		}
16685331Samw 		if (realnmp)
16695331Samw 			pn_free(realnmp);
16702113Sahrens 		dmu_tx_abort(tx);
1671789Sahrens 		ZFS_EXIT(zfsvfs);
1672789Sahrens 		return (error);
1673789Sahrens 	}
1674789Sahrens 
1675789Sahrens 	/*
1676789Sahrens 	 * Remove the directory entry.
1677789Sahrens 	 */
16785331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1679789Sahrens 
1680789Sahrens 	if (error) {
1681789Sahrens 		dmu_tx_commit(tx);
1682789Sahrens 		goto out;
1683789Sahrens 	}
1684789Sahrens 
16853461Sahrens 	if (unlinked) {
168611935SMark.Shellenbaum@Sun.COM 
168712620SMark.Shellenbaum@Oracle.COM 		/*
168812620SMark.Shellenbaum@Oracle.COM 		 * Hold z_lock so that we can make sure that the ACL obj
168912620SMark.Shellenbaum@Oracle.COM 		 * hasn't changed.  Could have been deleted due to
169012620SMark.Shellenbaum@Oracle.COM 		 * zfs_sa_upgrade().
169112620SMark.Shellenbaum@Oracle.COM 		 */
169212620SMark.Shellenbaum@Oracle.COM 		mutex_enter(&zp->z_lock);
1693789Sahrens 		mutex_enter(&vp->v_lock);
169411935SMark.Shellenbaum@Sun.COM 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
169511935SMark.Shellenbaum@Sun.COM 		    &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
16966992Smaybee 		delete_now = may_delete_now && !toobig &&
1697789Sahrens 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
169812620SMark.Shellenbaum@Oracle.COM 		    xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
169911935SMark.Shellenbaum@Sun.COM 		    acl_obj;
1700789Sahrens 		mutex_exit(&vp->v_lock);
1701789Sahrens 	}
1702789Sahrens 
1703789Sahrens 	if (delete_now) {
170411935SMark.Shellenbaum@Sun.COM 		if (xattr_obj_unlinked) {
170511935SMark.Shellenbaum@Sun.COM 			ASSERT3U(xzp->z_links, ==, 2);
1706789Sahrens 			mutex_enter(&xzp->z_lock);
17073461Sahrens 			xzp->z_unlinked = 1;
170811935SMark.Shellenbaum@Sun.COM 			xzp->z_links = 0;
170911935SMark.Shellenbaum@Sun.COM 			error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
171011935SMark.Shellenbaum@Sun.COM 			    &xzp->z_links, sizeof (xzp->z_links), tx);
171111935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error,  ==,  0);
1712789Sahrens 			mutex_exit(&xzp->z_lock);
17133461Sahrens 			zfs_unlinked_add(xzp, tx);
171412620SMark.Shellenbaum@Oracle.COM 
171511935SMark.Shellenbaum@Sun.COM 			if (zp->z_is_sa)
171611935SMark.Shellenbaum@Sun.COM 				error = sa_remove(zp->z_sa_hdl,
171711935SMark.Shellenbaum@Sun.COM 				    SA_ZPL_XATTR(zfsvfs), tx);
171811935SMark.Shellenbaum@Sun.COM 			else
171911935SMark.Shellenbaum@Sun.COM 				error = sa_update(zp->z_sa_hdl,
172011935SMark.Shellenbaum@Sun.COM 				    SA_ZPL_XATTR(zfsvfs), &null_xattr,
172111935SMark.Shellenbaum@Sun.COM 				    sizeof (uint64_t), tx);
172211935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error, ==, 0);
1723789Sahrens 		}
1724789Sahrens 		mutex_enter(&vp->v_lock);
1725789Sahrens 		vp->v_count--;
1726789Sahrens 		ASSERT3U(vp->v_count, ==, 0);
1727789Sahrens 		mutex_exit(&vp->v_lock);
1728789Sahrens 		mutex_exit(&zp->z_lock);
1729789Sahrens 		zfs_znode_delete(zp, tx);
17303461Sahrens 	} else if (unlinked) {
173112620SMark.Shellenbaum@Oracle.COM 		mutex_exit(&zp->z_lock);
17323461Sahrens 		zfs_unlinked_add(zp, tx);
1733789Sahrens 	}
1734789Sahrens 
17355331Samw 	txtype = TX_REMOVE;
17365331Samw 	if (flags & FIGNORECASE)
17375331Samw 		txtype |= TX_CI;
173812700SNeil.Perrin@Sun.COM 	zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1739789Sahrens 
1740789Sahrens 	dmu_tx_commit(tx);
1741789Sahrens out:
17425331Samw 	if (realnmp)
17435331Samw 		pn_free(realnmp);
17445331Samw 
1745789Sahrens 	zfs_dirent_unlock(dl);
1746789Sahrens 
174712178SMark.Shellenbaum@Sun.COM 	if (!delete_now)
1748789Sahrens 		VN_RELE(vp);
174912178SMark.Shellenbaum@Sun.COM 	if (xzp)
1750789Sahrens 		VN_RELE(ZTOV(xzp));
1751789Sahrens 
175212294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
175312699SNeil.Perrin@Sun.COM 		zil_commit(zilog, 0);
175412294SMark.Musante@Sun.COM 
1755789Sahrens 	ZFS_EXIT(zfsvfs);
1756789Sahrens 	return (error);
1757789Sahrens }
1758789Sahrens 
1759789Sahrens /*
1760789Sahrens  * Create a new directory and insert it into dvp using the name
1761789Sahrens  * provided.  Return a pointer to the inserted directory.
1762789Sahrens  *
1763789Sahrens  *	IN:	dvp	- vnode of directory to add subdir to.
1764789Sahrens  *		dirname	- name of new directory.
1765789Sahrens  *		vap	- attributes of new directory.
1766789Sahrens  *		cr	- credentials of caller.
17675331Samw  *		ct	- caller context
17685331Samw  *		vsecp	- ACL to be set
1769789Sahrens  *
1770789Sahrens  *	OUT:	vpp	- vnode of created directory.
1771789Sahrens  *
1772789Sahrens  *	RETURN:	0 if success
1773789Sahrens  *		error code if failure
1774789Sahrens  *
1775789Sahrens  * Timestamps:
1776789Sahrens  *	dvp - ctime|mtime updated
1777789Sahrens  *	 vp - ctime|mtime|atime updated
1778789Sahrens  */
17795331Samw /*ARGSUSED*/
1780789Sahrens static int
zfs_mkdir(vnode_t * dvp,char * dirname,vattr_t * vap,vnode_t ** vpp,cred_t * cr,caller_context_t * ct,int flags,vsecattr_t * vsecp)17815331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
17825331Samw     caller_context_t *ct, int flags, vsecattr_t *vsecp)
1783789Sahrens {
1784789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1785789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
17865326Sek110237 	zilog_t		*zilog;
1787789Sahrens 	zfs_dirlock_t	*dl;
17885331Samw 	uint64_t	txtype;
1789789Sahrens 	dmu_tx_t	*tx;
1790789Sahrens 	int		error;
17915331Samw 	int		zf = ZNEW;
17927847SMark.Shellenbaum@Sun.COM 	ksid_t		*ksid;
17937847SMark.Shellenbaum@Sun.COM 	uid_t		uid;
17947847SMark.Shellenbaum@Sun.COM 	gid_t		gid = crgetgid(cr);
179511935SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t   acl_ids;
17969179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
1797789Sahrens 
1798789Sahrens 	ASSERT(vap->va_type == VDIR);
1799789Sahrens 
18005331Samw 	/*
18015331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
18025331Samw 	 * make sure file system is at proper version
18035331Samw 	 */
18045331Samw 
18057847SMark.Shellenbaum@Sun.COM 	ksid = crgetsid(cr, KSID_OWNER);
18067847SMark.Shellenbaum@Sun.COM 	if (ksid)
18077847SMark.Shellenbaum@Sun.COM 		uid = ksid_getid(ksid);
18087847SMark.Shellenbaum@Sun.COM 	else
18097847SMark.Shellenbaum@Sun.COM 		uid = crgetuid(cr);
18105331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
18117847SMark.Shellenbaum@Sun.COM 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
18127876SMark.Shellenbaum@Sun.COM 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
18135331Samw 		return (EINVAL);
18145331Samw 
18155367Sahrens 	ZFS_ENTER(zfsvfs);
18165367Sahrens 	ZFS_VERIFY_ZP(dzp);
18175326Sek110237 	zilog = zfsvfs->z_log;
1818789Sahrens 
181911935SMark.Shellenbaum@Sun.COM 	if (dzp->z_pflags & ZFS_XATTR) {
1820789Sahrens 		ZFS_EXIT(zfsvfs);
1821789Sahrens 		return (EINVAL);
1822789Sahrens 	}
18235331Samw 
18245498Stimh 	if (zfsvfs->z_utf8 && u8_validate(dirname,
18255331Samw 	    strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
18265331Samw 		ZFS_EXIT(zfsvfs);
18275331Samw 		return (EILSEQ);
18285331Samw 	}
18295331Samw 	if (flags & FIGNORECASE)
18305331Samw 		zf |= ZCILOOK;
18315331Samw 
183212302SMark.Shellenbaum@Sun.COM 	if (vap->va_mask & AT_XVATTR) {
18335331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
18345331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
18355331Samw 			ZFS_EXIT(zfsvfs);
18365331Samw 			return (error);
18375331Samw 		}
183812302SMark.Shellenbaum@Sun.COM 	}
183912302SMark.Shellenbaum@Sun.COM 
184012302SMark.Shellenbaum@Sun.COM 	if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
184112302SMark.Shellenbaum@Sun.COM 	    vsecp, &acl_ids)) != 0) {
184212302SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
184312302SMark.Shellenbaum@Sun.COM 		return (error);
184412302SMark.Shellenbaum@Sun.COM 	}
1845789Sahrens 	/*
1846789Sahrens 	 * First make sure the new directory doesn't exist.
184712302SMark.Shellenbaum@Sun.COM 	 *
184812302SMark.Shellenbaum@Sun.COM 	 * Existence is checked first to make sure we don't return
184912302SMark.Shellenbaum@Sun.COM 	 * EACCES instead of EEXIST which can cause some applications
185012302SMark.Shellenbaum@Sun.COM 	 * to fail.
1851789Sahrens 	 */
18525331Samw top:
18535331Samw 	*vpp = NULL;
18545331Samw 
18555331Samw 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
18565331Samw 	    NULL, NULL)) {
185712302SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
1858789Sahrens 		ZFS_EXIT(zfsvfs);
1859789Sahrens 		return (error);
1860789Sahrens 	}
1861789Sahrens 
18625331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
186312302SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
18641231Smarks 		zfs_dirent_unlock(dl);
18651231Smarks 		ZFS_EXIT(zfsvfs);
18661231Smarks 		return (error);
18671231Smarks 	}
18681231Smarks 
18699396SMatthew.Ahrens@Sun.COM 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
187010143STim.Haley@Sun.COM 		zfs_acl_ids_free(&acl_ids);
18719396SMatthew.Ahrens@Sun.COM 		zfs_dirent_unlock(dl);
18729396SMatthew.Ahrens@Sun.COM 		ZFS_EXIT(zfsvfs);
18739396SMatthew.Ahrens@Sun.COM 		return (EDQUOT);
18749396SMatthew.Ahrens@Sun.COM 	}
18759179SMark.Shellenbaum@Sun.COM 
1876789Sahrens 	/*
1877789Sahrens 	 * Add a new entry to the directory.
1878789Sahrens 	 */
1879789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
18801544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
18811544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
18829179SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
18839396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
18849396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
188511935SMark.Shellenbaum@Sun.COM 	if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
188611935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
188711935SMark.Shellenbaum@Sun.COM 		    acl_ids.z_aclp->z_acl_bytes);
188811935SMark.Shellenbaum@Sun.COM 	}
188911935SMark.Shellenbaum@Sun.COM 
189011935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
189111935SMark.Shellenbaum@Sun.COM 	    ZFS_SA_BASE_ATTR_SIZE);
189211935SMark.Shellenbaum@Sun.COM 
18938227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1894789Sahrens 	if (error) {
1895789Sahrens 		zfs_dirent_unlock(dl);
18968227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
18972113Sahrens 			dmu_tx_wait(tx);
18982113Sahrens 			dmu_tx_abort(tx);
1899789Sahrens 			goto top;
1900789Sahrens 		}
190112302SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
19022113Sahrens 		dmu_tx_abort(tx);
1903789Sahrens 		ZFS_EXIT(zfsvfs);
1904789Sahrens 		return (error);
1905789Sahrens 	}
1906789Sahrens 
1907789Sahrens 	/*
1908789Sahrens 	 * Create new node.
1909789Sahrens 	 */
191011935SMark.Shellenbaum@Sun.COM 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
19119179SMark.Shellenbaum@Sun.COM 
19129179SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
19139179SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
191411935SMark.Shellenbaum@Sun.COM 
1915789Sahrens 	/*
1916789Sahrens 	 * Now put new name in parent dir.
1917789Sahrens 	 */
1918789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1919789Sahrens 
1920789Sahrens 	*vpp = ZTOV(zp);
1921789Sahrens 
19225331Samw 	txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
19235331Samw 	if (flags & FIGNORECASE)
19245331Samw 		txtype |= TX_CI;
19259179SMark.Shellenbaum@Sun.COM 	zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
19269179SMark.Shellenbaum@Sun.COM 	    acl_ids.z_fuidp, vap);
19279179SMark.Shellenbaum@Sun.COM 
19289179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_free(&acl_ids);
192911935SMark.Shellenbaum@Sun.COM 
1930789Sahrens 	dmu_tx_commit(tx);
1931789Sahrens 
1932789Sahrens 	zfs_dirent_unlock(dl);
1933789Sahrens 
193412294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
193512699SNeil.Perrin@Sun.COM 		zil_commit(zilog, 0);
193612294SMark.Musante@Sun.COM 
1937789Sahrens 	ZFS_EXIT(zfsvfs);
1938789Sahrens 	return (0);
1939789Sahrens }
1940789Sahrens 
1941789Sahrens /*
1942789Sahrens  * Remove a directory subdir entry.  If the current working
1943789Sahrens  * directory is the same as the subdir to be removed, the
1944789Sahrens  * remove will fail.
1945789Sahrens  *
1946789Sahrens  *	IN:	dvp	- vnode of directory to remove from.
1947789Sahrens  *		name	- name of directory to be removed.
1948789Sahrens  *		cwd	- vnode of current working directory.
1949789Sahrens  *		cr	- credentials of caller.
19505331Samw  *		ct	- caller context
19515331Samw  *		flags	- case flags
1952789Sahrens  *
1953789Sahrens  *	RETURN:	0 if success
1954789Sahrens  *		error code if failure
1955789Sahrens  *
1956789Sahrens  * Timestamps:
1957789Sahrens  *	dvp - ctime|mtime updated
1958789Sahrens  */
19595331Samw /*ARGSUSED*/
1960789Sahrens static int
zfs_rmdir(vnode_t * dvp,char * name,vnode_t * cwd,cred_t * cr,caller_context_t * ct,int flags)19615331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
19625331Samw     caller_context_t *ct, int flags)
1963789Sahrens {
1964789Sahrens 	znode_t		*dzp = VTOZ(dvp);
1965789Sahrens 	znode_t		*zp;
1966789Sahrens 	vnode_t		*vp;
1967789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
19685326Sek110237 	zilog_t		*zilog;
1969789Sahrens 	zfs_dirlock_t	*dl;
1970789Sahrens 	dmu_tx_t	*tx;
1971789Sahrens 	int		error;
19725331Samw 	int		zflg = ZEXISTS;
1973789Sahrens 
19745367Sahrens 	ZFS_ENTER(zfsvfs);
19755367Sahrens 	ZFS_VERIFY_ZP(dzp);
19765326Sek110237 	zilog = zfsvfs->z_log;
1977789Sahrens 
19785331Samw 	if (flags & FIGNORECASE)
19795331Samw 		zflg |= ZCILOOK;
1980789Sahrens top:
1981789Sahrens 	zp = NULL;
1982789Sahrens 
1983789Sahrens 	/*
1984789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1985789Sahrens 	 */
19865331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
19875331Samw 	    NULL, NULL)) {
1988789Sahrens 		ZFS_EXIT(zfsvfs);
1989789Sahrens 		return (error);
1990789Sahrens 	}
1991789Sahrens 
1992789Sahrens 	vp = ZTOV(zp);
1993789Sahrens 
1994789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1995789Sahrens 		goto out;
1996789Sahrens 	}
1997789Sahrens 
1998789Sahrens 	if (vp->v_type != VDIR) {
1999789Sahrens 		error = ENOTDIR;
2000789Sahrens 		goto out;
2001789Sahrens 	}
2002789Sahrens 
2003789Sahrens 	if (vp == cwd) {
2004789Sahrens 		error = EINVAL;
2005789Sahrens 		goto out;
2006789Sahrens 	}
2007789Sahrens 
20085331Samw 	vnevent_rmdir(vp, dvp, name, ct);
2009789Sahrens 
2010789Sahrens 	/*
20113897Smaybee 	 * Grab a lock on the directory to make sure that noone is
20123897Smaybee 	 * trying to add (or lookup) entries while we are removing it.
20133897Smaybee 	 */
20143897Smaybee 	rw_enter(&zp->z_name_lock, RW_WRITER);
20153897Smaybee 
20163897Smaybee 	/*
20173897Smaybee 	 * Grab a lock on the parent pointer to make sure we play well
2018789Sahrens 	 * with the treewalk and directory rename code.
2019789Sahrens 	 */
2020789Sahrens 	rw_enter(&zp->z_parent_lock, RW_WRITER);
2021789Sahrens 
2022789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
20231544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
202411935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
20253461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
202611935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
202711935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, dzp);
20288227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
2029789Sahrens 	if (error) {
2030789Sahrens 		rw_exit(&zp->z_parent_lock);
20313897Smaybee 		rw_exit(&zp->z_name_lock);
2032789Sahrens 		zfs_dirent_unlock(dl);
2033789Sahrens 		VN_RELE(vp);
20348227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
20352113Sahrens 			dmu_tx_wait(tx);
20362113Sahrens 			dmu_tx_abort(tx);
2037789Sahrens 			goto top;
2038789Sahrens 		}
20392113Sahrens 		dmu_tx_abort(tx);
2040789Sahrens 		ZFS_EXIT(zfsvfs);
2041789Sahrens 		return (error);
2042789Sahrens 	}
2043789Sahrens 
20445331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
20455331Samw 
20465331Samw 	if (error == 0) {
20475331Samw 		uint64_t txtype = TX_RMDIR;
20485331Samw 		if (flags & FIGNORECASE)
20495331Samw 			txtype |= TX_CI;
205012699SNeil.Perrin@Sun.COM 		zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
20515331Samw 	}
2052789Sahrens 
2053789Sahrens 	dmu_tx_commit(tx);
2054789Sahrens 
2055789Sahrens 	rw_exit(&zp->z_parent_lock);
20563897Smaybee 	rw_exit(&zp->z_name_lock);
2057789Sahrens out:
2058789Sahrens 	zfs_dirent_unlock(dl);
2059789Sahrens 
2060789Sahrens 	VN_RELE(vp);
2061789Sahrens 
206212294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
206312699SNeil.Perrin@Sun.COM 		zil_commit(zilog, 0);
206412294SMark.Musante@Sun.COM 
2065789Sahrens 	ZFS_EXIT(zfsvfs);
2066789Sahrens 	return (error);
2067789Sahrens }
2068789Sahrens 
2069789Sahrens /*
2070789Sahrens  * Read as many directory entries as will fit into the provided
2071789Sahrens  * buffer from the given directory cursor position (specified in
2072789Sahrens  * the uio structure.
2073789Sahrens  *
2074789Sahrens  *	IN:	vp	- vnode of directory to read.
2075789Sahrens  *		uio	- structure supplying read location, range info,
2076789Sahrens  *			  and return buffer.
2077789Sahrens  *		cr	- credentials of caller.
20785331Samw  *		ct	- caller context
20795331Samw  *		flags	- case flags
2080789Sahrens  *
2081789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
2082789Sahrens  *		eofp	- set to true if end-of-file detected.
2083789Sahrens  *
2084789Sahrens  *	RETURN:	0 if success
2085789Sahrens  *		error code if failure
2086789Sahrens  *
2087789Sahrens  * Timestamps:
2088789Sahrens  *	vp - atime updated
2089789Sahrens  *
2090789Sahrens  * Note that the low 4 bits of the cookie returned by zap is always zero.
2091789Sahrens  * This allows us to use the low range for "special" directory entries:
2092789Sahrens  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
2093789Sahrens  * we use the offset 2 for the '.zfs' directory.
2094789Sahrens  */
2095789Sahrens /* ARGSUSED */
2096789Sahrens static int
zfs_readdir(vnode_t * vp,uio_t * uio,cred_t * cr,int * eofp,caller_context_t * ct,int flags)20975331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
20985331Samw     caller_context_t *ct, int flags)
2099789Sahrens {
2100789Sahrens 	znode_t		*zp = VTOZ(vp);
2101789Sahrens 	iovec_t		*iovp;
21025331Samw 	edirent_t	*eodp;
2103789Sahrens 	dirent64_t	*odp;
2104789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2105869Sperrin 	objset_t	*os;
2106789Sahrens 	caddr_t		outbuf;
2107789Sahrens 	size_t		bufsize;
2108789Sahrens 	zap_cursor_t	zc;
2109789Sahrens 	zap_attribute_t	zap;
2110789Sahrens 	uint_t		bytes_wanted;
2111789Sahrens 	uint64_t	offset; /* must be unsigned; checks for < 1 */
211211935SMark.Shellenbaum@Sun.COM 	uint64_t	parent;
2113789Sahrens 	int		local_eof;
2114869Sperrin 	int		outcount;
2115869Sperrin 	int		error;
2116869Sperrin 	uint8_t		prefetch;
21175663Sck153898 	boolean_t	check_sysattrs;
2118789Sahrens 
21195367Sahrens 	ZFS_ENTER(zfsvfs);
21205367Sahrens 	ZFS_VERIFY_ZP(zp);
2121789Sahrens 
212211935SMark.Shellenbaum@Sun.COM 	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
212311935SMark.Shellenbaum@Sun.COM 	    &parent, sizeof (parent))) != 0) {
212411935SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
212511935SMark.Shellenbaum@Sun.COM 		return (error);
212611935SMark.Shellenbaum@Sun.COM 	}
212711935SMark.Shellenbaum@Sun.COM 
2128789Sahrens 	/*
2129789Sahrens 	 * If we are not given an eof variable,
2130789Sahrens 	 * use a local one.
2131789Sahrens 	 */
2132789Sahrens 	if (eofp == NULL)
2133789Sahrens 		eofp = &local_eof;
2134789Sahrens 
2135789Sahrens 	/*
2136789Sahrens 	 * Check for valid iov_len.
2137789Sahrens 	 */
2138789Sahrens 	if (uio->uio_iov->iov_len <= 0) {
2139789Sahrens 		ZFS_EXIT(zfsvfs);
2140789Sahrens 		return (EINVAL);
2141789Sahrens 	}
2142789Sahrens 
2143789Sahrens 	/*
2144789Sahrens 	 * Quit if directory has been removed (posix)
2145789Sahrens 	 */
21463461Sahrens 	if ((*eofp = zp->z_unlinked) != 0) {
2147789Sahrens 		ZFS_EXIT(zfsvfs);
2148789Sahrens 		return (0);
2149789Sahrens 	}
2150789Sahrens 
2151869Sperrin 	error = 0;
2152869Sperrin 	os = zfsvfs->z_os;
2153869Sperrin 	offset = uio->uio_loffset;
2154869Sperrin 	prefetch = zp->z_zn_prefetch;
2155869Sperrin 
2156789Sahrens 	/*
2157789Sahrens 	 * Initialize the iterator cursor.
2158789Sahrens 	 */
2159789Sahrens 	if (offset <= 3) {
2160789Sahrens 		/*
2161789Sahrens 		 * Start iteration from the beginning of the directory.
2162789Sahrens 		 */
2163869Sperrin 		zap_cursor_init(&zc, os, zp->z_id);
2164789Sahrens 	} else {
2165789Sahrens 		/*
2166789Sahrens 		 * The offset is a serialized cursor.
2167789Sahrens 		 */
2168869Sperrin 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2169789Sahrens 	}
2170789Sahrens 
2171789Sahrens 	/*
2172789Sahrens 	 * Get space to change directory entries into fs independent format.
2173789Sahrens 	 */
2174789Sahrens 	iovp = uio->uio_iov;
2175789Sahrens 	bytes_wanted = iovp->iov_len;
2176789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2177789Sahrens 		bufsize = bytes_wanted;
2178789Sahrens 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
2179789Sahrens 		odp = (struct dirent64 *)outbuf;
2180789Sahrens 	} else {
2181789Sahrens 		bufsize = bytes_wanted;
2182789Sahrens 		odp = (struct dirent64 *)iovp->iov_base;
2183789Sahrens 	}
21845331Samw 	eodp = (struct edirent *)odp;
2185789Sahrens 
2186789Sahrens 	/*
21877757SJanice.Chang@Sun.COM 	 * If this VFS supports the system attribute view interface; and
21887757SJanice.Chang@Sun.COM 	 * we're looking at an extended attribute directory; and we care
21897757SJanice.Chang@Sun.COM 	 * about normalization conflicts on this vfs; then we must check
21907757SJanice.Chang@Sun.COM 	 * for normalization conflicts with the sysattr name space.
21915663Sck153898 	 */
21927757SJanice.Chang@Sun.COM 	check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
21935663Sck153898 	    (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
21945663Sck153898 	    (flags & V_RDDIR_ENTFLAGS);
21955663Sck153898 
21965663Sck153898 	/*
2197789Sahrens 	 * Transform to file-system independent format
2198789Sahrens 	 */
2199789Sahrens 	outcount = 0;
2200789Sahrens 	while (outcount < bytes_wanted) {
22013912Slling 		ino64_t objnum;
22023912Slling 		ushort_t reclen;
220312799STim.Haley@Sun.COM 		off64_t *next = NULL;
22043912Slling 
2205789Sahrens 		/*
2206789Sahrens 		 * Special case `.', `..', and `.zfs'.
2207789Sahrens 		 */
2208789Sahrens 		if (offset == 0) {
2209789Sahrens 			(void) strcpy(zap.za_name, ".");
22105331Samw 			zap.za_normalization_conflict = 0;
22113912Slling 			objnum = zp->z_id;
2212789Sahrens 		} else if (offset == 1) {
2213789Sahrens 			(void) strcpy(zap.za_name, "..");
22145331Samw 			zap.za_normalization_conflict = 0;
221511935SMark.Shellenbaum@Sun.COM 			objnum = parent;
2216789Sahrens 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
2217789Sahrens 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
22185331Samw 			zap.za_normalization_conflict = 0;
22193912Slling 			objnum = ZFSCTL_INO_ROOT;
2220789Sahrens 		} else {
2221789Sahrens 			/*
2222789Sahrens 			 * Grab next entry.
2223789Sahrens 			 */
2224789Sahrens 			if (error = zap_cursor_retrieve(&zc, &zap)) {
2225789Sahrens 				if ((*eofp = (error == ENOENT)) != 0)
2226789Sahrens 					break;
2227789Sahrens 				else
2228789Sahrens 					goto update;
2229789Sahrens 			}
2230789Sahrens 
2231789Sahrens 			if (zap.za_integer_length != 8 ||
2232789Sahrens 			    zap.za_num_integers != 1) {
2233789Sahrens 				cmn_err(CE_WARN, "zap_readdir: bad directory "
2234789Sahrens 				    "entry, obj = %lld, offset = %lld\n",
2235789Sahrens 				    (u_longlong_t)zp->z_id,
2236789Sahrens 				    (u_longlong_t)offset);
2237789Sahrens 				error = ENXIO;
2238789Sahrens 				goto update;
2239789Sahrens 			}
22403912Slling 
22413912Slling 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
22423912Slling 			/*
22433912Slling 			 * MacOS X can extract the object type here such as:
22443912Slling 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
22453912Slling 			 */
22465663Sck153898 
22475663Sck153898 			if (check_sysattrs && !zap.za_normalization_conflict) {
22485663Sck153898 				zap.za_normalization_conflict =
22495663Sck153898 				    xattr_sysattr_casechk(zap.za_name);
22505663Sck153898 			}
2251789Sahrens 		}
22525331Samw 
22539749STim.Haley@Sun.COM 		if (flags & V_RDDIR_ACCFILTER) {
22549749STim.Haley@Sun.COM 			/*
22559749STim.Haley@Sun.COM 			 * If we have no access at all, don't include
22569749STim.Haley@Sun.COM 			 * this entry in the returned information
22579749STim.Haley@Sun.COM 			 */
22589749STim.Haley@Sun.COM 			znode_t	*ezp;
22599749STim.Haley@Sun.COM 			if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
22609749STim.Haley@Sun.COM 				goto skip_entry;
22619749STim.Haley@Sun.COM 			if (!zfs_has_access(ezp, cr)) {
22629749STim.Haley@Sun.COM 				VN_RELE(ZTOV(ezp));
22639749STim.Haley@Sun.COM 				goto skip_entry;
22649749STim.Haley@Sun.COM 			}
22659749STim.Haley@Sun.COM 			VN_RELE(ZTOV(ezp));
22669749STim.Haley@Sun.COM 		}
22679749STim.Haley@Sun.COM 
22685331Samw 		if (flags & V_RDDIR_ENTFLAGS)
22695331Samw 			reclen = EDIRENT_RECLEN(strlen(zap.za_name));
22705331Samw 		else
22715331Samw 			reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2272789Sahrens 
2273789Sahrens 		/*
2274789Sahrens 		 * Will this entry fit in the buffer?
2275789Sahrens 		 */
22763912Slling 		if (outcount + reclen > bufsize) {
2277789Sahrens 			/*
2278789Sahrens 			 * Did we manage to fit anything in the buffer?
2279789Sahrens 			 */
2280789Sahrens 			if (!outcount) {
2281789Sahrens 				error = EINVAL;
2282789Sahrens 				goto update;
2283789Sahrens 			}
2284789Sahrens 			break;
2285789Sahrens 		}
22865331Samw 		if (flags & V_RDDIR_ENTFLAGS) {
22875331Samw 			/*
22885331Samw 			 * Add extended flag entry:
22895331Samw 			 */
22905331Samw 			eodp->ed_ino = objnum;
22915331Samw 			eodp->ed_reclen = reclen;
22925331Samw 			/* NOTE: ed_off is the offset for the *next* entry */
22935331Samw 			next = &(eodp->ed_off);
22945331Samw 			eodp->ed_eflags = zap.za_normalization_conflict ?
22955331Samw 			    ED_CASE_CONFLICT : 0;
22965331Samw 			(void) strncpy(eodp->ed_name, zap.za_name,
22975331Samw 			    EDIRENT_NAMELEN(reclen));
22985331Samw 			eodp = (edirent_t *)((intptr_t)eodp + reclen);
22995331Samw 		} else {
23005331Samw 			/*
23015331Samw 			 * Add normal entry:
23025331Samw 			 */
23035331Samw 			odp->d_ino = objnum;
23045331Samw 			odp->d_reclen = reclen;
23055331Samw 			/* NOTE: d_off is the offset for the *next* entry */
23065331Samw 			next = &(odp->d_off);
23075331Samw 			(void) strncpy(odp->d_name, zap.za_name,
23085331Samw 			    DIRENT64_NAMELEN(reclen));
23095331Samw 			odp = (dirent64_t *)((intptr_t)odp + reclen);
23105331Samw 		}
23113912Slling 		outcount += reclen;
2312789Sahrens 
2313789Sahrens 		ASSERT(outcount <= bufsize);
2314789Sahrens 
2315789Sahrens 		/* Prefetch znode */
2316869Sperrin 		if (prefetch)
23173912Slling 			dmu_prefetch(os, objnum, 0, 0);
2318789Sahrens 
23199749STim.Haley@Sun.COM 	skip_entry:
2320789Sahrens 		/*
2321789Sahrens 		 * Move to the next entry, fill in the previous offset.
2322789Sahrens 		 */
2323789Sahrens 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2324789Sahrens 			zap_cursor_advance(&zc);
2325789Sahrens 			offset = zap_cursor_serialize(&zc);
2326789Sahrens 		} else {
2327789Sahrens 			offset += 1;
2328789Sahrens 		}
232912799STim.Haley@Sun.COM 		if (next)
233012799STim.Haley@Sun.COM 			*next = offset;
2331789Sahrens 	}
2332869Sperrin 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2333789Sahrens 
2334789Sahrens 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2335789Sahrens 		iovp->iov_base += outcount;
2336789Sahrens 		iovp->iov_len -= outcount;
2337789Sahrens 		uio->uio_resid -= outcount;
2338789Sahrens 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2339789Sahrens 		/*
2340789Sahrens 		 * Reset the pointer.
2341789Sahrens 		 */
2342789Sahrens 		offset = uio->uio_loffset;
2343789Sahrens 	}
2344789Sahrens 
2345789Sahrens update:
2346885Sahrens 	zap_cursor_fini(&zc);
2347789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2348789Sahrens 		kmem_free(outbuf, bufsize);
2349789Sahrens 
2350789Sahrens 	if (error == ENOENT)
2351789Sahrens 		error = 0;
2352789Sahrens 
2353789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2354789Sahrens 
2355789Sahrens 	uio->uio_loffset = offset;
2356789Sahrens 	ZFS_EXIT(zfsvfs);
2357789Sahrens 	return (error);
2358789Sahrens }
2359789Sahrens 
23604720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4;
23614720Sfr157268 
2362789Sahrens static int
zfs_fsync(vnode_t * vp,int syncflag,cred_t * cr,caller_context_t * ct)23635331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2364789Sahrens {
2365789Sahrens 	znode_t	*zp = VTOZ(vp);
2366789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2367789Sahrens 
23681773Seschrock 	/*
23691773Seschrock 	 * Regardless of whether this is required for standards conformance,
23701773Seschrock 	 * this is the logical behavior when fsync() is called on a file with
23711773Seschrock 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
23721773Seschrock 	 * going to be pushed out as part of the zil_commit().
23731773Seschrock 	 */
23741773Seschrock 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
23751773Seschrock 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
23765331Samw 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
23771773Seschrock 
23784720Sfr157268 	(void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
23794720Sfr157268 
238012294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
238112294SMark.Musante@Sun.COM 		ZFS_ENTER(zfsvfs);
238212294SMark.Musante@Sun.COM 		ZFS_VERIFY_ZP(zp);
238312699SNeil.Perrin@Sun.COM 		zil_commit(zfsvfs->z_log, zp->z_id);
238412294SMark.Musante@Sun.COM 		ZFS_EXIT(zfsvfs);
238512294SMark.Musante@Sun.COM 	}
2386789Sahrens 	return (0);
2387789Sahrens }
2388789Sahrens 
23895331Samw 
2390789Sahrens /*
2391789Sahrens  * Get the requested file attributes and place them in the provided
2392789Sahrens  * vattr structure.
2393789Sahrens  *
2394789Sahrens  *	IN:	vp	- vnode of file.
2395789Sahrens  *		vap	- va_mask identifies requested attributes.
23965331Samw  *			  If AT_XVATTR set, then optional attrs are requested
23975331Samw  *		flags	- ATTR_NOACLCHECK (CIFS server context)
2398789Sahrens  *		cr	- credentials of caller.
23995331Samw  *		ct	- caller context
2400789Sahrens  *
2401789Sahrens  *	OUT:	vap	- attribute values.
2402789Sahrens  *
2403789Sahrens  *	RETURN:	0 (always succeeds)
2404789Sahrens  */
2405789Sahrens /* ARGSUSED */
2406789Sahrens static int
zfs_getattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)24075331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
24085331Samw     caller_context_t *ct)
2409789Sahrens {
2410789Sahrens 	znode_t *zp = VTOZ(vp);
2411789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
24125331Samw 	int	error = 0;
24134543Smarks 	uint64_t links;
241411935SMark.Shellenbaum@Sun.COM 	uint64_t mtime[2], ctime[2];
24155331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
24165331Samw 	xoptattr_t *xoap = NULL;
24175331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
241811935SMark.Shellenbaum@Sun.COM 	sa_bulk_attr_t bulk[2];
241911935SMark.Shellenbaum@Sun.COM 	int count = 0;
2420789Sahrens 
24215367Sahrens 	ZFS_ENTER(zfsvfs);
24225367Sahrens 	ZFS_VERIFY_ZP(zp);
242311935SMark.Shellenbaum@Sun.COM 
242413069SMark.Shellenbaum@Oracle.COM 	zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
242513069SMark.Shellenbaum@Oracle.COM 
242611935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
242711935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
242811935SMark.Shellenbaum@Sun.COM 
242911935SMark.Shellenbaum@Sun.COM 	if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
243011935SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
243111935SMark.Shellenbaum@Sun.COM 		return (error);
243211935SMark.Shellenbaum@Sun.COM 	}
2433789Sahrens 
24345331Samw 	/*
24355331Samw 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
24365331Samw 	 * Also, if we are the owner don't bother, since owner should
24375331Samw 	 * always be allowed to read basic attributes of file.
24385331Samw 	 */
243913069SMark.Shellenbaum@Oracle.COM 	if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
244013069SMark.Shellenbaum@Oracle.COM 	    (vap->va_uid != crgetuid(cr))) {
24415331Samw 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
24425331Samw 		    skipaclchk, cr)) {
24435331Samw 			ZFS_EXIT(zfsvfs);
24445331Samw 			return (error);
24455331Samw 		}
24465331Samw 	}
24475331Samw 
2448789Sahrens 	/*
2449789Sahrens 	 * Return all attributes.  It's cheaper to provide the answer
2450789Sahrens 	 * than to determine whether we were asked the question.
2451789Sahrens 	 */
2452789Sahrens 
24539774SRay.Hassan@Sun.COM 	mutex_enter(&zp->z_lock);
2454789Sahrens 	vap->va_type = vp->v_type;
245511935SMark.Shellenbaum@Sun.COM 	vap->va_mode = zp->z_mode & MODEMASK;
2456789Sahrens 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2457789Sahrens 	vap->va_nodeid = zp->z_id;
24584543Smarks 	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
245911935SMark.Shellenbaum@Sun.COM 		links = zp->z_links + 1;
24604543Smarks 	else
246111935SMark.Shellenbaum@Sun.COM 		links = zp->z_links;
24624543Smarks 	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
246311935SMark.Shellenbaum@Sun.COM 	vap->va_size = zp->z_size;
24641816Smarks 	vap->va_rdev = vp->v_rdev;
2465789Sahrens 	vap->va_seq = zp->z_seq;
2466789Sahrens 
24675331Samw 	/*
24685331Samw 	 * Add in any requested optional attributes and the create time.
24695331Samw 	 * Also set the corresponding bits in the returned attribute bitmap.
24705331Samw 	 */
24715331Samw 	if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
24725331Samw 		if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
24735331Samw 			xoap->xoa_archive =
247411935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_ARCHIVE) != 0);
24755331Samw 			XVA_SET_RTN(xvap, XAT_ARCHIVE);
24765331Samw 		}
24775331Samw 
24785331Samw 		if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
24795331Samw 			xoap->xoa_readonly =
248011935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_READONLY) != 0);
24815331Samw 			XVA_SET_RTN(xvap, XAT_READONLY);
24825331Samw 		}
24835331Samw 
24845331Samw 		if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
24855331Samw 			xoap->xoa_system =
248611935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_SYSTEM) != 0);
24875331Samw 			XVA_SET_RTN(xvap, XAT_SYSTEM);
24885331Samw 		}
24895331Samw 
24905331Samw 		if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
24915331Samw 			xoap->xoa_hidden =
249211935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_HIDDEN) != 0);
24935331Samw 			XVA_SET_RTN(xvap, XAT_HIDDEN);
24945331Samw 		}
24955331Samw 
24965331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
24975331Samw 			xoap->xoa_nounlink =
249811935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NOUNLINK) != 0);
24995331Samw 			XVA_SET_RTN(xvap, XAT_NOUNLINK);
25005331Samw 		}
25015331Samw 
25025331Samw 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
25035331Samw 			xoap->xoa_immutable =
250411935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
25055331Samw 			XVA_SET_RTN(xvap, XAT_IMMUTABLE);
25065331Samw 		}
25075331Samw 
25085331Samw 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
25095331Samw 			xoap->xoa_appendonly =
251011935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_APPENDONLY) != 0);
25115331Samw 			XVA_SET_RTN(xvap, XAT_APPENDONLY);
25125331Samw 		}
25135331Samw 
25145331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
25155331Samw 			xoap->xoa_nodump =
251611935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NODUMP) != 0);
25175331Samw 			XVA_SET_RTN(xvap, XAT_NODUMP);
25185331Samw 		}
25195331Samw 
25205331Samw 		if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
25215331Samw 			xoap->xoa_opaque =
252211935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_OPAQUE) != 0);
25235331Samw 			XVA_SET_RTN(xvap, XAT_OPAQUE);
25245331Samw 		}
25255331Samw 
25265331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
25275331Samw 			xoap->xoa_av_quarantined =
252811935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
25295331Samw 			XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
25305331Samw 		}
25315331Samw 
25325331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
25335331Samw 			xoap->xoa_av_modified =
253411935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
25355331Samw 			XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
25365331Samw 		}
25375331Samw 
25385331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
253911935SMark.Shellenbaum@Sun.COM 		    vp->v_type == VREG) {
254011935SMark.Shellenbaum@Sun.COM 			zfs_sa_get_scanstamp(zp, xvap);
25415331Samw 		}
25425331Samw 
25435331Samw 		if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
254411935SMark.Shellenbaum@Sun.COM 			uint64_t times[2];
254511935SMark.Shellenbaum@Sun.COM 
254611935SMark.Shellenbaum@Sun.COM 			(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
254711935SMark.Shellenbaum@Sun.COM 			    times, sizeof (times));
254811935SMark.Shellenbaum@Sun.COM 			ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
25495331Samw 			XVA_SET_RTN(xvap, XAT_CREATETIME);
25505331Samw 		}
255110793Sdai.ngo@sun.com 
255210793Sdai.ngo@sun.com 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
255311935SMark.Shellenbaum@Sun.COM 			xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
255410793Sdai.ngo@sun.com 			XVA_SET_RTN(xvap, XAT_REPARSE);
255510793Sdai.ngo@sun.com 		}
255613043STim.Haley@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
255713043STim.Haley@Sun.COM 			xoap->xoa_generation = zp->z_gen;
255813043STim.Haley@Sun.COM 			XVA_SET_RTN(xvap, XAT_GEN);
255913043STim.Haley@Sun.COM 		}
256013082SJoyce.McIntosh@Sun.COM 
256113082SJoyce.McIntosh@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
256213082SJoyce.McIntosh@Sun.COM 			xoap->xoa_offline =
256313082SJoyce.McIntosh@Sun.COM 			    ((zp->z_pflags & ZFS_OFFLINE) != 0);
256413082SJoyce.McIntosh@Sun.COM 			XVA_SET_RTN(xvap, XAT_OFFLINE);
256513082SJoyce.McIntosh@Sun.COM 		}
256613082SJoyce.McIntosh@Sun.COM 
256713082SJoyce.McIntosh@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
256813082SJoyce.McIntosh@Sun.COM 			xoap->xoa_sparse =
256913082SJoyce.McIntosh@Sun.COM 			    ((zp->z_pflags & ZFS_SPARSE) != 0);
257013082SJoyce.McIntosh@Sun.COM 			XVA_SET_RTN(xvap, XAT_SPARSE);
257113082SJoyce.McIntosh@Sun.COM 		}
25725331Samw 	}
25735331Samw 
257411935SMark.Shellenbaum@Sun.COM 	ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
257511935SMark.Shellenbaum@Sun.COM 	ZFS_TIME_DECODE(&vap->va_mtime, mtime);
257611935SMark.Shellenbaum@Sun.COM 	ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2577789Sahrens 
2578789Sahrens 	mutex_exit(&zp->z_lock);
2579789Sahrens 
258011935SMark.Shellenbaum@Sun.COM 	sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2581789Sahrens 
2582789Sahrens 	if (zp->z_blksz == 0) {
2583789Sahrens 		/*
2584789Sahrens 		 * Block size hasn't been set; suggest maximal I/O transfers.
2585789Sahrens 		 */
2586789Sahrens 		vap->va_blksize = zfsvfs->z_max_blksz;
2587789Sahrens 	}
2588789Sahrens 
2589789Sahrens 	ZFS_EXIT(zfsvfs);
2590789Sahrens 	return (0);
2591789Sahrens }
2592789Sahrens 
2593789Sahrens /*
2594789Sahrens  * Set the file attributes to the values contained in the
2595789Sahrens  * vattr structure.
2596789Sahrens  *
2597789Sahrens  *	IN:	vp	- vnode of file to be modified.
2598789Sahrens  *		vap	- new attribute values.
25995331Samw  *			  If AT_XVATTR set, then optional attrs are being set
2600789Sahrens  *		flags	- ATTR_UTIME set if non-default time values provided.
26015331Samw  *			- ATTR_NOACLCHECK (CIFS context only).
2602789Sahrens  *		cr	- credentials of caller.
26035331Samw  *		ct	- caller context
2604789Sahrens  *
2605789Sahrens  *	RETURN:	0 if success
2606789Sahrens  *		error code if failure
2607789Sahrens  *
2608789Sahrens  * Timestamps:
2609789Sahrens  *	vp - ctime updated, mtime updated if size changed.
2610789Sahrens  */
2611789Sahrens /* ARGSUSED */
2612789Sahrens static int
zfs_setattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)2613789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2614789Sahrens 	caller_context_t *ct)
2615789Sahrens {
26165326Sek110237 	znode_t		*zp = VTOZ(vp);
2617789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
26185326Sek110237 	zilog_t		*zilog;
2619789Sahrens 	dmu_tx_t	*tx;
26201878Smaybee 	vattr_t		oldva;
26218190SMark.Shellenbaum@Sun.COM 	xvattr_t	tmpxvattr;
2622789Sahrens 	uint_t		mask = vap->va_mask;
26231878Smaybee 	uint_t		saved_mask;
26242796Smarks 	int		trim_mask = 0;
2625789Sahrens 	uint64_t	new_mode;
26269179SMark.Shellenbaum@Sun.COM 	uint64_t	new_uid, new_gid;
2627*13089SMark.Shellenbaum@Oracle.COM 	uint64_t	xattr_obj;
262811935SMark.Shellenbaum@Sun.COM 	uint64_t	mtime[2], ctime[2];
26291231Smarks 	znode_t		*attrzp;
2630789Sahrens 	int		need_policy = FALSE;
263111935SMark.Shellenbaum@Sun.COM 	int		err, err2;
26325331Samw 	zfs_fuid_info_t *fuidp = NULL;
26335331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
26345331Samw 	xoptattr_t	*xoap;
2635*13089SMark.Shellenbaum@Oracle.COM 	zfs_acl_t	*aclp;
26365331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
263711935SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied = B_FALSE;
263811935SMark.Shellenbaum@Sun.COM 	sa_bulk_attr_t	bulk[7], xattr_bulk[7];
263911935SMark.Shellenbaum@Sun.COM 	int		count = 0, xattr_count = 0;
2640789Sahrens 
2641789Sahrens 	if (mask == 0)
2642789Sahrens 		return (0);
2643789Sahrens 
2644789Sahrens 	if (mask & AT_NOSET)
2645789Sahrens 		return (EINVAL);
2646789Sahrens 
26475367Sahrens 	ZFS_ENTER(zfsvfs);
26485367Sahrens 	ZFS_VERIFY_ZP(zp);
26495331Samw 
26505331Samw 	zilog = zfsvfs->z_log;
26515331Samw 
26525331Samw 	/*
26535331Samw 	 * Make sure that if we have ephemeral uid/gid or xvattr specified
26545331Samw 	 * that file system is at proper version level
26555331Samw 	 */
26565331Samw 
26575331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
26585331Samw 	    (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
26595331Samw 	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
26605386Stimh 	    (mask & AT_XVATTR))) {
26615386Stimh 		ZFS_EXIT(zfsvfs);
26625331Samw 		return (EINVAL);
26635386Stimh 	}
26645386Stimh 
26655386Stimh 	if (mask & AT_SIZE && vp->v_type == VDIR) {
26665386Stimh 		ZFS_EXIT(zfsvfs);
2667789Sahrens 		return (EISDIR);
26685386Stimh 	}
26695386Stimh 
26705386Stimh 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
26715386Stimh 		ZFS_EXIT(zfsvfs);
26721308Smarks 		return (EINVAL);
26735386Stimh 	}
26741308Smarks 
26755331Samw 	/*
26765331Samw 	 * If this is an xvattr_t, then get a pointer to the structure of
26775331Samw 	 * optional attributes.  If this is NULL, then we have a vattr_t.
26785331Samw 	 */
26795331Samw 	xoap = xva_getxoptattr(xvap);
26805331Samw 
26818190SMark.Shellenbaum@Sun.COM 	xva_init(&tmpxvattr);
26828190SMark.Shellenbaum@Sun.COM 
26835331Samw 	/*
26845331Samw 	 * Immutable files can only alter immutable bit and atime
26855331Samw 	 */
268611935SMark.Shellenbaum@Sun.COM 	if ((zp->z_pflags & ZFS_IMMUTABLE) &&
26875331Samw 	    ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
26885386Stimh 	    ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
26895386Stimh 		ZFS_EXIT(zfsvfs);
26905331Samw 		return (EPERM);
26915386Stimh 	}
26925386Stimh 
269311935SMark.Shellenbaum@Sun.COM 	if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
26945386Stimh 		ZFS_EXIT(zfsvfs);
26955331Samw 		return (EPERM);
26965386Stimh 	}
2697789Sahrens 
26986064Smarks 	/*
26996064Smarks 	 * Verify timestamps doesn't overflow 32 bits.
27006064Smarks 	 * ZFS can handle large timestamps, but 32bit syscalls can't
27016064Smarks 	 * handle times greater than 2039.  This check should be removed
27026064Smarks 	 * once large timestamps are fully supported.
27036064Smarks 	 */
27046064Smarks 	if (mask & (AT_ATIME | AT_MTIME)) {
27056064Smarks 		if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
27066064Smarks 		    ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
27076064Smarks 			ZFS_EXIT(zfsvfs);
27086064Smarks 			return (EOVERFLOW);
27096064Smarks 		}
27106064Smarks 	}
27116064Smarks 
2712789Sahrens top:
27131231Smarks 	attrzp = NULL;
2714*13089SMark.Shellenbaum@Oracle.COM 	aclp = NULL;
2715789Sahrens 
27169981STim.Haley@Sun.COM 	/* Can this be moved to before the top label? */
2717789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2718789Sahrens 		ZFS_EXIT(zfsvfs);
2719789Sahrens 		return (EROFS);
2720789Sahrens 	}
2721789Sahrens 
2722789Sahrens 	/*
2723789Sahrens 	 * First validate permissions
2724789Sahrens 	 */
2725789Sahrens 
2726789Sahrens 	if (mask & AT_SIZE) {
27275331Samw 		err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2728789Sahrens 		if (err) {
2729789Sahrens 			ZFS_EXIT(zfsvfs);
2730789Sahrens 			return (err);
2731789Sahrens 		}
27321878Smaybee 		/*
27331878Smaybee 		 * XXX - Note, we are not providing any open
27341878Smaybee 		 * mode flags here (like FNDELAY), so we may
27351878Smaybee 		 * block if there are locks present... this
27361878Smaybee 		 * should be addressed in openat().
27371878Smaybee 		 */
27386992Smaybee 		/* XXX - would it be OK to generate a log record here? */
27396992Smaybee 		err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
27401878Smaybee 		if (err) {
27411878Smaybee 			ZFS_EXIT(zfsvfs);
27421878Smaybee 			return (err);
27431878Smaybee 		}
2744789Sahrens 	}
2745789Sahrens 
27465331Samw 	if (mask & (AT_ATIME|AT_MTIME) ||
27475331Samw 	    ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
27485331Samw 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
27495331Samw 	    XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
275013082SJoyce.McIntosh@Sun.COM 	    XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
275113082SJoyce.McIntosh@Sun.COM 	    XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
27525331Samw 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
275311935SMark.Shellenbaum@Sun.COM 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
27545331Samw 		need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
27555331Samw 		    skipaclchk, cr);
275611935SMark.Shellenbaum@Sun.COM 	}
2757789Sahrens 
2758789Sahrens 	if (mask & (AT_UID|AT_GID)) {
2759789Sahrens 		int	idmask = (mask & (AT_UID|AT_GID));
2760789Sahrens 		int	take_owner;
2761789Sahrens 		int	take_group;
2762789Sahrens 
2763789Sahrens 		/*
2764913Smarks 		 * NOTE: even if a new mode is being set,
2765913Smarks 		 * we may clear S_ISUID/S_ISGID bits.
2766913Smarks 		 */
2767913Smarks 
2768913Smarks 		if (!(mask & AT_MODE))
276911935SMark.Shellenbaum@Sun.COM 			vap->va_mode = zp->z_mode;
2770913Smarks 
2771913Smarks 		/*
2772789Sahrens 		 * Take ownership or chgrp to group we are a member of
2773789Sahrens 		 */
2774789Sahrens 
2775789Sahrens 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
27765331Samw 		take_group = (mask & AT_GID) &&
27775331Samw 		    zfs_groupmember(zfsvfs, vap->va_gid, cr);
2778789Sahrens 
2779789Sahrens 		/*
2780789Sahrens 		 * If both AT_UID and AT_GID are set then take_owner and
2781789Sahrens 		 * take_group must both be set in order to allow taking
2782789Sahrens 		 * ownership.
2783789Sahrens 		 *
2784789Sahrens 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2785789Sahrens 		 *
2786789Sahrens 		 */
2787789Sahrens 
2788789Sahrens 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2789789Sahrens 		    ((idmask == AT_UID) && take_owner) ||
2790789Sahrens 		    ((idmask == AT_GID) && take_group)) {
27915331Samw 			if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
27925331Samw 			    skipaclchk, cr) == 0) {
2793789Sahrens 				/*
2794789Sahrens 				 * Remove setuid/setgid for non-privileged users
2795789Sahrens 				 */
27961115Smarks 				secpolicy_setid_clear(vap, cr);
27972796Smarks 				trim_mask = (mask & (AT_UID|AT_GID));
2798789Sahrens 			} else {
2799789Sahrens 				need_policy =  TRUE;
2800789Sahrens 			}
2801789Sahrens 		} else {
2802789Sahrens 			need_policy =  TRUE;
2803789Sahrens 		}
2804789Sahrens 	}
2805789Sahrens 
28062796Smarks 	mutex_enter(&zp->z_lock);
280711935SMark.Shellenbaum@Sun.COM 	oldva.va_mode = zp->z_mode;
280813069SMark.Shellenbaum@Oracle.COM 	zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
28095331Samw 	if (mask & AT_XVATTR) {
28108190SMark.Shellenbaum@Sun.COM 		/*
28118190SMark.Shellenbaum@Sun.COM 		 * Update xvattr mask to include only those attributes
28128190SMark.Shellenbaum@Sun.COM 		 * that are actually changing.
28138190SMark.Shellenbaum@Sun.COM 		 *
28148190SMark.Shellenbaum@Sun.COM 		 * the bits will be restored prior to actually setting
28158190SMark.Shellenbaum@Sun.COM 		 * the attributes so the caller thinks they were set.
28168190SMark.Shellenbaum@Sun.COM 		 */
28178190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
28188190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_appendonly !=
281911935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
28208190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
28218190SMark.Shellenbaum@Sun.COM 			} else {
28228190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_APPENDONLY);
28238190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
28248190SMark.Shellenbaum@Sun.COM 			}
28258190SMark.Shellenbaum@Sun.COM 		}
28268190SMark.Shellenbaum@Sun.COM 
28278190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
28288190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_nounlink !=
282911935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
28308190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
28318190SMark.Shellenbaum@Sun.COM 			} else {
28328190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_NOUNLINK);
28338190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
28348190SMark.Shellenbaum@Sun.COM 			}
28358190SMark.Shellenbaum@Sun.COM 		}
28368190SMark.Shellenbaum@Sun.COM 
28378190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
28388190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_immutable !=
283911935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
28408190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
28418190SMark.Shellenbaum@Sun.COM 			} else {
28428190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
28438190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
28448190SMark.Shellenbaum@Sun.COM 			}
28458190SMark.Shellenbaum@Sun.COM 		}
28468190SMark.Shellenbaum@Sun.COM 
28478190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
28488190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_nodump !=
284911935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NODUMP) != 0)) {
28508190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
28518190SMark.Shellenbaum@Sun.COM 			} else {
28528190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_NODUMP);
28538190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
28548190SMark.Shellenbaum@Sun.COM 			}
28558190SMark.Shellenbaum@Sun.COM 		}
28568190SMark.Shellenbaum@Sun.COM 
28578190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
28588190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_av_modified !=
285911935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
28608190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
28618190SMark.Shellenbaum@Sun.COM 			} else {
28628190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
28638190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
28648190SMark.Shellenbaum@Sun.COM 			}
28658190SMark.Shellenbaum@Sun.COM 		}
28668190SMark.Shellenbaum@Sun.COM 
28678190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
28688190SMark.Shellenbaum@Sun.COM 			if ((vp->v_type != VREG &&
28698190SMark.Shellenbaum@Sun.COM 			    xoap->xoa_av_quarantined) ||
28708190SMark.Shellenbaum@Sun.COM 			    xoap->xoa_av_quarantined !=
287111935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
28728190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
28738190SMark.Shellenbaum@Sun.COM 			} else {
28748190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
28758190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
28768190SMark.Shellenbaum@Sun.COM 			}
28778190SMark.Shellenbaum@Sun.COM 		}
28788190SMark.Shellenbaum@Sun.COM 
287910793Sdai.ngo@sun.com 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
288010793Sdai.ngo@sun.com 			mutex_exit(&zp->z_lock);
288110793Sdai.ngo@sun.com 			ZFS_EXIT(zfsvfs);
288210793Sdai.ngo@sun.com 			return (EPERM);
288310793Sdai.ngo@sun.com 		}
288410793Sdai.ngo@sun.com 
28858190SMark.Shellenbaum@Sun.COM 		if (need_policy == FALSE &&
28868190SMark.Shellenbaum@Sun.COM 		    (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
28878190SMark.Shellenbaum@Sun.COM 		    XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
28885331Samw 			need_policy = TRUE;
28895331Samw 		}
28905331Samw 	}
28915331Samw 
28922796Smarks 	mutex_exit(&zp->z_lock);
28932796Smarks 
28942796Smarks 	if (mask & AT_MODE) {
28955331Samw 		if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
28962796Smarks 			err = secpolicy_setid_setsticky_clear(vp, vap,
28972796Smarks 			    &oldva, cr);
28982796Smarks 			if (err) {
28992796Smarks 				ZFS_EXIT(zfsvfs);
29002796Smarks 				return (err);
29012796Smarks 			}
29022796Smarks 			trim_mask |= AT_MODE;
29032796Smarks 		} else {
29042796Smarks 			need_policy = TRUE;
29052796Smarks 		}
29062796Smarks 	}
2907789Sahrens 
2908789Sahrens 	if (need_policy) {
29091115Smarks 		/*
29101115Smarks 		 * If trim_mask is set then take ownership
29112796Smarks 		 * has been granted or write_acl is present and user
29122796Smarks 		 * has the ability to modify mode.  In that case remove
29132796Smarks 		 * UID|GID and or MODE from mask so that
29141115Smarks 		 * secpolicy_vnode_setattr() doesn't revoke it.
29151115Smarks 		 */
29162796Smarks 
29172796Smarks 		if (trim_mask) {
29182796Smarks 			saved_mask = vap->va_mask;
29192796Smarks 			vap->va_mask &= ~trim_mask;
29202796Smarks 		}
2921789Sahrens 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
29225331Samw 		    (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2923789Sahrens 		if (err) {
2924789Sahrens 			ZFS_EXIT(zfsvfs);
2925789Sahrens 			return (err);
2926789Sahrens 		}
29271115Smarks 
29281115Smarks 		if (trim_mask)
29292796Smarks 			vap->va_mask |= saved_mask;
2930789Sahrens 	}
2931789Sahrens 
2932789Sahrens 	/*
2933789Sahrens 	 * secpolicy_vnode_setattr, or take ownership may have
2934789Sahrens 	 * changed va_mask
2935789Sahrens 	 */
2936789Sahrens 	mask = vap->va_mask;
2937789Sahrens 
293811935SMark.Shellenbaum@Sun.COM 	if ((mask & (AT_UID | AT_GID))) {
2939*13089SMark.Shellenbaum@Oracle.COM 		err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
2940*13089SMark.Shellenbaum@Oracle.COM 		    &xattr_obj, sizeof (xattr_obj));
2941*13089SMark.Shellenbaum@Oracle.COM 
2942*13089SMark.Shellenbaum@Oracle.COM 		if (err == 0 && xattr_obj) {
294311935SMark.Shellenbaum@Sun.COM 			err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
29449396SMatthew.Ahrens@Sun.COM 			if (err)
294511935SMark.Shellenbaum@Sun.COM 				goto out2;
29469179SMark.Shellenbaum@Sun.COM 		}
29479179SMark.Shellenbaum@Sun.COM 		if (mask & AT_UID) {
29489179SMark.Shellenbaum@Sun.COM 			new_uid = zfs_fuid_create(zfsvfs,
29499179SMark.Shellenbaum@Sun.COM 			    (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
295013069SMark.Shellenbaum@Oracle.COM 			if (new_uid != zp->z_uid &&
295111935SMark.Shellenbaum@Sun.COM 			    zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
2952*13089SMark.Shellenbaum@Oracle.COM 				if (attrzp)
2953*13089SMark.Shellenbaum@Oracle.COM 					VN_RELE(ZTOV(attrzp));
29549396SMatthew.Ahrens@Sun.COM 				err = EDQUOT;
295511935SMark.Shellenbaum@Sun.COM 				goto out2;
29569396SMatthew.Ahrens@Sun.COM 			}
29571231Smarks 		}
29589396SMatthew.Ahrens@Sun.COM 
29599179SMark.Shellenbaum@Sun.COM 		if (mask & AT_GID) {
29609179SMark.Shellenbaum@Sun.COM 			new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
29619179SMark.Shellenbaum@Sun.COM 			    cr, ZFS_GROUP, &fuidp);
296211935SMark.Shellenbaum@Sun.COM 			if (new_gid != zp->z_gid &&
296311935SMark.Shellenbaum@Sun.COM 			    zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
2964*13089SMark.Shellenbaum@Oracle.COM 				if (attrzp)
2965*13089SMark.Shellenbaum@Oracle.COM 					VN_RELE(ZTOV(attrzp));
29669396SMatthew.Ahrens@Sun.COM 				err = EDQUOT;
296711935SMark.Shellenbaum@Sun.COM 				goto out2;
29689179SMark.Shellenbaum@Sun.COM 			}
29699179SMark.Shellenbaum@Sun.COM 		}
29701231Smarks 	}
297111935SMark.Shellenbaum@Sun.COM 	tx = dmu_tx_create(zfsvfs->z_os);
297211935SMark.Shellenbaum@Sun.COM 
297311935SMark.Shellenbaum@Sun.COM 	if (mask & AT_MODE) {
297411935SMark.Shellenbaum@Sun.COM 		uint64_t pmode = zp->z_mode;
297512620SMark.Shellenbaum@Oracle.COM 		uint64_t acl_obj;
297611935SMark.Shellenbaum@Sun.COM 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
297711935SMark.Shellenbaum@Sun.COM 
2978*13089SMark.Shellenbaum@Oracle.COM 		zfs_acl_chmod_setattr(zp, &aclp, new_mode);
297911935SMark.Shellenbaum@Sun.COM 
298012620SMark.Shellenbaum@Oracle.COM 		mutex_enter(&zp->z_lock);
298112620SMark.Shellenbaum@Oracle.COM 		if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
298211935SMark.Shellenbaum@Sun.COM 			/*
298311935SMark.Shellenbaum@Sun.COM 			 * Are we upgrading ACL from old V0 format
298411935SMark.Shellenbaum@Sun.COM 			 * to V1 format?
298511935SMark.Shellenbaum@Sun.COM 			 */
298613046SMark.Shellenbaum@Oracle.COM 			if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
298712620SMark.Shellenbaum@Oracle.COM 			    zfs_znode_acl_version(zp) ==
298811935SMark.Shellenbaum@Sun.COM 			    ZFS_ACL_VERSION_INITIAL) {
298912620SMark.Shellenbaum@Oracle.COM 				dmu_tx_hold_free(tx, acl_obj, 0,
299011935SMark.Shellenbaum@Sun.COM 				    DMU_OBJECT_END);
299111935SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
299211935SMark.Shellenbaum@Sun.COM 				    0, aclp->z_acl_bytes);
299311935SMark.Shellenbaum@Sun.COM 			} else {
299412620SMark.Shellenbaum@Oracle.COM 				dmu_tx_hold_write(tx, acl_obj, 0,
299511935SMark.Shellenbaum@Sun.COM 				    aclp->z_acl_bytes);
299611935SMark.Shellenbaum@Sun.COM 			}
299711935SMark.Shellenbaum@Sun.COM 		} else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
299811935SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
299911935SMark.Shellenbaum@Sun.COM 			    0, aclp->z_acl_bytes);
300011935SMark.Shellenbaum@Sun.COM 		}
300112620SMark.Shellenbaum@Oracle.COM 		mutex_exit(&zp->z_lock);
300211935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
300311935SMark.Shellenbaum@Sun.COM 	} else {
300411935SMark.Shellenbaum@Sun.COM 		if ((mask & AT_XVATTR) &&
300511935SMark.Shellenbaum@Sun.COM 		    XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
300611935SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
300711935SMark.Shellenbaum@Sun.COM 		else
300811935SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
300911935SMark.Shellenbaum@Sun.COM 	}
301011935SMark.Shellenbaum@Sun.COM 
301111935SMark.Shellenbaum@Sun.COM 	if (attrzp) {
301211935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
301311935SMark.Shellenbaum@Sun.COM 	}
301411935SMark.Shellenbaum@Sun.COM 
301511935SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
301611935SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
301711935SMark.Shellenbaum@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
301811935SMark.Shellenbaum@Sun.COM 
301911935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
30201231Smarks 
30218227SNeil.Perrin@Sun.COM 	err = dmu_tx_assign(tx, TXG_NOWAIT);
3022789Sahrens 	if (err) {
30239396SMatthew.Ahrens@Sun.COM 		if (err == ERESTART)
30242113Sahrens 			dmu_tx_wait(tx);
30259396SMatthew.Ahrens@Sun.COM 		goto out;
3026789Sahrens 	}
3027789Sahrens 
302811935SMark.Shellenbaum@Sun.COM 	count = 0;
3029789Sahrens 	/*
3030789Sahrens 	 * Set each attribute requested.
3031789Sahrens 	 * We group settings according to the locks they need to acquire.
3032789Sahrens 	 *
3033789Sahrens 	 * Note: you cannot set ctime directly, although it will be
3034789Sahrens 	 * updated as a side-effect of calling this function.
3035789Sahrens 	 */
3036789Sahrens 
303712620SMark.Shellenbaum@Oracle.COM 
303812620SMark.Shellenbaum@Oracle.COM 	if (mask & (AT_UID|AT_GID|AT_MODE))
303912620SMark.Shellenbaum@Oracle.COM 		mutex_enter(&zp->z_acl_lock);
3040789Sahrens 	mutex_enter(&zp->z_lock);
3041789Sahrens 
304212394SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
304312394SMark.Shellenbaum@Sun.COM 	    &zp->z_pflags, sizeof (zp->z_pflags));
304412394SMark.Shellenbaum@Sun.COM 
304512394SMark.Shellenbaum@Sun.COM 	if (attrzp) {
304612620SMark.Shellenbaum@Oracle.COM 		if (mask & (AT_UID|AT_GID|AT_MODE))
304712620SMark.Shellenbaum@Oracle.COM 			mutex_enter(&attrzp->z_acl_lock);
304811935SMark.Shellenbaum@Sun.COM 		mutex_enter(&attrzp->z_lock);
304912394SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
305012394SMark.Shellenbaum@Sun.COM 		    SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
305112394SMark.Shellenbaum@Sun.COM 		    sizeof (attrzp->z_pflags));
305212394SMark.Shellenbaum@Sun.COM 	}
305311935SMark.Shellenbaum@Sun.COM 
305412164SMark.Shellenbaum@Sun.COM 	if (mask & (AT_UID|AT_GID)) {
305512164SMark.Shellenbaum@Sun.COM 
305612164SMark.Shellenbaum@Sun.COM 		if (mask & AT_UID) {
305712164SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
305812164SMark.Shellenbaum@Sun.COM 			    &new_uid, sizeof (new_uid));
305913069SMark.Shellenbaum@Oracle.COM 			zp->z_uid = new_uid;
306012164SMark.Shellenbaum@Sun.COM 			if (attrzp) {
306112164SMark.Shellenbaum@Sun.COM 				SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
306212164SMark.Shellenbaum@Sun.COM 				    SA_ZPL_UID(zfsvfs), NULL, &new_uid,
306312164SMark.Shellenbaum@Sun.COM 				    sizeof (new_uid));
306413069SMark.Shellenbaum@Oracle.COM 				attrzp->z_uid = new_uid;
306512164SMark.Shellenbaum@Sun.COM 			}
306611935SMark.Shellenbaum@Sun.COM 		}
306712164SMark.Shellenbaum@Sun.COM 
306812164SMark.Shellenbaum@Sun.COM 		if (mask & AT_GID) {
306912164SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
307012164SMark.Shellenbaum@Sun.COM 			    NULL, &new_gid, sizeof (new_gid));
307113069SMark.Shellenbaum@Oracle.COM 			zp->z_gid = new_gid;
307212164SMark.Shellenbaum@Sun.COM 			if (attrzp) {
307312164SMark.Shellenbaum@Sun.COM 				SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
307412164SMark.Shellenbaum@Sun.COM 				    SA_ZPL_GID(zfsvfs), NULL, &new_gid,
307512164SMark.Shellenbaum@Sun.COM 				    sizeof (new_gid));
307613069SMark.Shellenbaum@Oracle.COM 				attrzp->z_gid = new_gid;
307712164SMark.Shellenbaum@Sun.COM 			}
307812164SMark.Shellenbaum@Sun.COM 		}
307912164SMark.Shellenbaum@Sun.COM 		if (!(mask & AT_MODE)) {
308012164SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
308112164SMark.Shellenbaum@Sun.COM 			    NULL, &new_mode, sizeof (new_mode));
308212164SMark.Shellenbaum@Sun.COM 			new_mode = zp->z_mode;
308312164SMark.Shellenbaum@Sun.COM 		}
308412164SMark.Shellenbaum@Sun.COM 		err = zfs_acl_chown_setattr(zp);
308512164SMark.Shellenbaum@Sun.COM 		ASSERT(err == 0);
308611935SMark.Shellenbaum@Sun.COM 		if (attrzp) {
308712164SMark.Shellenbaum@Sun.COM 			err = zfs_acl_chown_setattr(attrzp);
308812164SMark.Shellenbaum@Sun.COM 			ASSERT(err == 0);
308911935SMark.Shellenbaum@Sun.COM 		}
309011935SMark.Shellenbaum@Sun.COM 	}
309111935SMark.Shellenbaum@Sun.COM 
3092789Sahrens 	if (mask & AT_MODE) {
309311935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
309411935SMark.Shellenbaum@Sun.COM 		    &new_mode, sizeof (new_mode));
309511935SMark.Shellenbaum@Sun.COM 		zp->z_mode = new_mode;
309612164SMark.Shellenbaum@Sun.COM 		ASSERT3U((uintptr_t)aclp, !=, NULL);
30979179SMark.Shellenbaum@Sun.COM 		err = zfs_aclset_common(zp, aclp, cr, tx);
3098789Sahrens 		ASSERT3U(err, ==, 0);
3099*13089SMark.Shellenbaum@Oracle.COM 		if (zp->z_acl_cached)
3100*13089SMark.Shellenbaum@Oracle.COM 			zfs_acl_free(zp->z_acl_cached);
310110143STim.Haley@Sun.COM 		zp->z_acl_cached = aclp;
310210143STim.Haley@Sun.COM 		aclp = NULL;
3103789Sahrens 	}
3104789Sahrens 
310511935SMark.Shellenbaum@Sun.COM 
310611935SMark.Shellenbaum@Sun.COM 	if (mask & AT_ATIME) {
310711935SMark.Shellenbaum@Sun.COM 		ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
310811935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
310911935SMark.Shellenbaum@Sun.COM 		    &zp->z_atime, sizeof (zp->z_atime));
31101231Smarks 	}
31111231Smarks 
311211935SMark.Shellenbaum@Sun.COM 	if (mask & AT_MTIME) {
311311935SMark.Shellenbaum@Sun.COM 		ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
311411935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
311511935SMark.Shellenbaum@Sun.COM 		    mtime, sizeof (mtime));
31161231Smarks 	}
31171231Smarks 
31186992Smaybee 	/* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
311911935SMark.Shellenbaum@Sun.COM 	if (mask & AT_SIZE && !(mask & AT_MTIME)) {
312012394SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
312112394SMark.Shellenbaum@Sun.COM 		    NULL, mtime, sizeof (mtime));
312211935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
312311935SMark.Shellenbaum@Sun.COM 		    &ctime, sizeof (ctime));
312411935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
312511935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
312611935SMark.Shellenbaum@Sun.COM 	} else if (mask != 0) {
312711935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
312811935SMark.Shellenbaum@Sun.COM 		    &ctime, sizeof (ctime));
312911935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
313011935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
313111935SMark.Shellenbaum@Sun.COM 		if (attrzp) {
313211935SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
313311935SMark.Shellenbaum@Sun.COM 			    SA_ZPL_CTIME(zfsvfs), NULL,
313411935SMark.Shellenbaum@Sun.COM 			    &ctime, sizeof (ctime));
313511935SMark.Shellenbaum@Sun.COM 			zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
313611935SMark.Shellenbaum@Sun.COM 			    mtime, ctime, B_TRUE);
313711935SMark.Shellenbaum@Sun.COM 		}
313811935SMark.Shellenbaum@Sun.COM 	}
31395331Samw 	/*
31405331Samw 	 * Do this after setting timestamps to prevent timestamp
31415331Samw 	 * update from toggling bit
31425331Samw 	 */
31435331Samw 
31445331Samw 	if (xoap && (mask & AT_XVATTR)) {
31458190SMark.Shellenbaum@Sun.COM 
31468190SMark.Shellenbaum@Sun.COM 		/*
31478190SMark.Shellenbaum@Sun.COM 		 * restore trimmed off masks
31488190SMark.Shellenbaum@Sun.COM 		 * so that return masks can be set for caller.
31498190SMark.Shellenbaum@Sun.COM 		 */
31508190SMark.Shellenbaum@Sun.COM 
31518190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
31528190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_APPENDONLY);
31538190SMark.Shellenbaum@Sun.COM 		}
31548190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
31558190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_NOUNLINK);
31568190SMark.Shellenbaum@Sun.COM 		}
31578190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
31588190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_IMMUTABLE);
31598190SMark.Shellenbaum@Sun.COM 		}
31608190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
31618190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_NODUMP);
31628190SMark.Shellenbaum@Sun.COM 		}
31638190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
31648190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
31658190SMark.Shellenbaum@Sun.COM 		}
31668190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
31678190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
31688190SMark.Shellenbaum@Sun.COM 		}
31698190SMark.Shellenbaum@Sun.COM 
317011935SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
31715331Samw 			ASSERT(vp->v_type == VREG);
31725331Samw 
317311935SMark.Shellenbaum@Sun.COM 		zfs_xvattr_set(zp, xvap, tx);
31745331Samw 	}
3175789Sahrens 
31769179SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
31779179SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
31789179SMark.Shellenbaum@Sun.COM 
31791878Smaybee 	if (mask != 0)
31805331Samw 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
31815331Samw 
3182789Sahrens 	mutex_exit(&zp->z_lock);
318312620SMark.Shellenbaum@Oracle.COM 	if (mask & (AT_UID|AT_GID|AT_MODE))
318412620SMark.Shellenbaum@Oracle.COM 		mutex_exit(&zp->z_acl_lock);
318512620SMark.Shellenbaum@Oracle.COM 
318612620SMark.Shellenbaum@Oracle.COM 	if (attrzp) {
318712620SMark.Shellenbaum@Oracle.COM 		if (mask & (AT_UID|AT_GID|AT_MODE))
318812620SMark.Shellenbaum@Oracle.COM 			mutex_exit(&attrzp->z_acl_lock);
318912620SMark.Shellenbaum@Oracle.COM 		mutex_exit(&attrzp->z_lock);
319012620SMark.Shellenbaum@Oracle.COM 	}
31919396SMatthew.Ahrens@Sun.COM out:
319211935SMark.Shellenbaum@Sun.COM 	if (err == 0 && attrzp) {
319311935SMark.Shellenbaum@Sun.COM 		err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
319411935SMark.Shellenbaum@Sun.COM 		    xattr_count, tx);
319511935SMark.Shellenbaum@Sun.COM 		ASSERT(err2 == 0);
319611935SMark.Shellenbaum@Sun.COM 	}
319711935SMark.Shellenbaum@Sun.COM 
31981231Smarks 	if (attrzp)
31991231Smarks 		VN_RELE(ZTOV(attrzp));
320010143STim.Haley@Sun.COM 	if (aclp)
320110143STim.Haley@Sun.COM 		zfs_acl_free(aclp);
320210143STim.Haley@Sun.COM 
32039396SMatthew.Ahrens@Sun.COM 	if (fuidp) {
32049396SMatthew.Ahrens@Sun.COM 		zfs_fuid_info_free(fuidp);
32059396SMatthew.Ahrens@Sun.COM 		fuidp = NULL;
32069396SMatthew.Ahrens@Sun.COM 	}
32079396SMatthew.Ahrens@Sun.COM 
320811935SMark.Shellenbaum@Sun.COM 	if (err) {
32099396SMatthew.Ahrens@Sun.COM 		dmu_tx_abort(tx);
321011935SMark.Shellenbaum@Sun.COM 		if (err == ERESTART)
321111935SMark.Shellenbaum@Sun.COM 			goto top;
321211935SMark.Shellenbaum@Sun.COM 	} else {
321311935SMark.Shellenbaum@Sun.COM 		err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
32149396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
321511935SMark.Shellenbaum@Sun.COM 	}
321611935SMark.Shellenbaum@Sun.COM 
321711935SMark.Shellenbaum@Sun.COM out2:
321812294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
321912699SNeil.Perrin@Sun.COM 		zil_commit(zilog, 0);
322012294SMark.Musante@Sun.COM 
3221789Sahrens 	ZFS_EXIT(zfsvfs);
3222789Sahrens 	return (err);
3223789Sahrens }
3224789Sahrens 
32253271Smaybee typedef struct zfs_zlock {
32263271Smaybee 	krwlock_t	*zl_rwlock;	/* lock we acquired */
32273271Smaybee 	znode_t		*zl_znode;	/* znode we held */
32283271Smaybee 	struct zfs_zlock *zl_next;	/* next in list */
32293271Smaybee } zfs_zlock_t;
32303271Smaybee 
32313271Smaybee /*
32323271Smaybee  * Drop locks and release vnodes that were held by zfs_rename_lock().
32333271Smaybee  */
32343271Smaybee static void
zfs_rename_unlock(zfs_zlock_t ** zlpp)32353271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp)
32363271Smaybee {
32373271Smaybee 	zfs_zlock_t *zl;
32383271Smaybee 
32393271Smaybee 	while ((zl = *zlpp) != NULL) {
32403271Smaybee 		if (zl->zl_znode != NULL)
32413271Smaybee 			VN_RELE(ZTOV(zl->zl_znode));
32423271Smaybee 		rw_exit(zl->zl_rwlock);
32433271Smaybee 		*zlpp = zl->zl_next;
32443271Smaybee 		kmem_free(zl, sizeof (*zl));
32453271Smaybee 	}
32463271Smaybee }
32473271Smaybee 
3248789Sahrens /*
3249789Sahrens  * Search back through the directory tree, using the ".." entries.
3250789Sahrens  * Lock each directory in the chain to prevent concurrent renames.
3251789Sahrens  * Fail any attempt to move a directory into one of its own descendants.
3252789Sahrens  * XXX - z_parent_lock can overlap with map or grow locks
3253789Sahrens  */
3254789Sahrens static int
zfs_rename_lock(znode_t * szp,znode_t * tdzp,znode_t * sdzp,zfs_zlock_t ** zlpp)3255789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3256789Sahrens {
3257789Sahrens 	zfs_zlock_t	*zl;
32583638Sbillm 	znode_t		*zp = tdzp;
3259789Sahrens 	uint64_t	rootid = zp->z_zfsvfs->z_root;
326011935SMark.Shellenbaum@Sun.COM 	uint64_t	oidp = zp->z_id;
3261789Sahrens 	krwlock_t	*rwlp = &szp->z_parent_lock;
3262789Sahrens 	krw_t		rw = RW_WRITER;
3263789Sahrens 
3264789Sahrens 	/*
3265789Sahrens 	 * First pass write-locks szp and compares to zp->z_id.
3266789Sahrens 	 * Later passes read-lock zp and compare to zp->z_parent.
3267789Sahrens 	 */
3268789Sahrens 	do {
32693271Smaybee 		if (!rw_tryenter(rwlp, rw)) {
32703271Smaybee 			/*
32713271Smaybee 			 * Another thread is renaming in this path.
32723271Smaybee 			 * Note that if we are a WRITER, we don't have any
32733271Smaybee 			 * parent_locks held yet.
32743271Smaybee 			 */
32753271Smaybee 			if (rw == RW_READER && zp->z_id > szp->z_id) {
32763271Smaybee 				/*
32773271Smaybee 				 * Drop our locks and restart
32783271Smaybee 				 */
32793271Smaybee 				zfs_rename_unlock(&zl);
32803271Smaybee 				*zlpp = NULL;
32813271Smaybee 				zp = tdzp;
328211935SMark.Shellenbaum@Sun.COM 				oidp = zp->z_id;
32833271Smaybee 				rwlp = &szp->z_parent_lock;
32843271Smaybee 				rw = RW_WRITER;
32853271Smaybee 				continue;
32863271Smaybee 			} else {
32873271Smaybee 				/*
32883271Smaybee 				 * Wait for other thread to drop its locks
32893271Smaybee 				 */
32903271Smaybee 				rw_enter(rwlp, rw);
32913271Smaybee 			}
32923271Smaybee 		}
32933271Smaybee 
3294789Sahrens 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3295789Sahrens 		zl->zl_rwlock = rwlp;
3296789Sahrens 		zl->zl_znode = NULL;
3297789Sahrens 		zl->zl_next = *zlpp;
3298789Sahrens 		*zlpp = zl;
3299789Sahrens 
330011935SMark.Shellenbaum@Sun.COM 		if (oidp == szp->z_id)		/* We're a descendant of szp */
3301789Sahrens 			return (EINVAL);
3302789Sahrens 
330311935SMark.Shellenbaum@Sun.COM 		if (oidp == rootid)		/* We've hit the top */
3304789Sahrens 			return (0);
3305789Sahrens 
3306789Sahrens 		if (rw == RW_READER) {		/* i.e. not the first pass */
330711935SMark.Shellenbaum@Sun.COM 			int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3308789Sahrens 			if (error)
3309789Sahrens 				return (error);
3310789Sahrens 			zl->zl_znode = zp;
3311789Sahrens 		}
331211935SMark.Shellenbaum@Sun.COM 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
331311935SMark.Shellenbaum@Sun.COM 		    &oidp, sizeof (oidp));
3314789Sahrens 		rwlp = &zp->z_parent_lock;
3315789Sahrens 		rw = RW_READER;
3316789Sahrens 
3317789Sahrens 	} while (zp->z_id != sdzp->z_id);
3318789Sahrens 
3319789Sahrens 	return (0);
3320789Sahrens }
3321789Sahrens 
3322789Sahrens /*
3323789Sahrens  * Move an entry from the provided source directory to the target
3324789Sahrens  * directory.  Change the entry name as indicated.
3325789Sahrens  *
3326789Sahrens  *	IN:	sdvp	- Source directory containing the "old entry".
3327789Sahrens  *		snm	- Old entry name.
3328789Sahrens  *		tdvp	- Target directory to contain the "new entry".
3329789Sahrens  *		tnm	- New entry name.
3330789Sahrens  *		cr	- credentials of caller.
33315331Samw  *		ct	- caller context
33325331Samw  *		flags	- case flags
3333789Sahrens  *
3334789Sahrens  *	RETURN:	0 if success
3335789Sahrens  *		error code if failure
3336789Sahrens  *
3337789Sahrens  * Timestamps:
3338789Sahrens  *	sdvp,tdvp - ctime|mtime updated
3339789Sahrens  */
33405331Samw /*ARGSUSED*/
3341789Sahrens static int
zfs_rename(vnode_t * sdvp,char * snm,vnode_t * tdvp,char * tnm,cred_t * cr,caller_context_t * ct,int flags)33425331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
33435331Samw     caller_context_t *ct, int flags)
3344789Sahrens {
3345789Sahrens 	znode_t		*tdzp, *szp, *tzp;
3346789Sahrens 	znode_t		*sdzp = VTOZ(sdvp);
3347789Sahrens 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
33485326Sek110237 	zilog_t		*zilog;
3349789Sahrens 	vnode_t		*realvp;
3350789Sahrens 	zfs_dirlock_t	*sdl, *tdl;
3351789Sahrens 	dmu_tx_t	*tx;
3352789Sahrens 	zfs_zlock_t	*zl;
33535331Samw 	int		cmp, serr, terr;
33545331Samw 	int		error = 0;
33555331Samw 	int		zflg = 0;
3356789Sahrens 
33575367Sahrens 	ZFS_ENTER(zfsvfs);
33585367Sahrens 	ZFS_VERIFY_ZP(sdzp);
33595326Sek110237 	zilog = zfsvfs->z_log;
3360789Sahrens 
3361789Sahrens 	/*
3362789Sahrens 	 * Make sure we have the real vp for the target directory.
3363789Sahrens 	 */
33645331Samw 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3365789Sahrens 		tdvp = realvp;
3366789Sahrens 
336712079SMark.Shellenbaum@Sun.COM 	if (tdvp->v_vfsp != sdvp->v_vfsp || zfsctl_is_node(tdvp)) {
3368789Sahrens 		ZFS_EXIT(zfsvfs);
3369789Sahrens 		return (EXDEV);
3370789Sahrens 	}
3371789Sahrens 
3372789Sahrens 	tdzp = VTOZ(tdvp);
33735367Sahrens 	ZFS_VERIFY_ZP(tdzp);
33745498Stimh 	if (zfsvfs->z_utf8 && u8_validate(tnm,
33755331Samw 	    strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
33765331Samw 		ZFS_EXIT(zfsvfs);
33775331Samw 		return (EILSEQ);
33785331Samw 	}
33795331Samw 
33805331Samw 	if (flags & FIGNORECASE)
33815331Samw 		zflg |= ZCILOOK;
33825331Samw 
3383789Sahrens top:
3384789Sahrens 	szp = NULL;
3385789Sahrens 	tzp = NULL;
3386789Sahrens 	zl = NULL;
3387789Sahrens 
3388789Sahrens 	/*
3389789Sahrens 	 * This is to prevent the creation of links into attribute space
3390789Sahrens 	 * by renaming a linked file into/outof an attribute directory.
3391789Sahrens 	 * See the comment in zfs_link() for why this is considered bad.
3392789Sahrens 	 */
339311935SMark.Shellenbaum@Sun.COM 	if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3394789Sahrens 		ZFS_EXIT(zfsvfs);
3395789Sahrens 		return (EINVAL);
3396789Sahrens 	}
3397789Sahrens 
3398789Sahrens 	/*
3399789Sahrens 	 * Lock source and target directory entries.  To prevent deadlock,
3400789Sahrens 	 * a lock ordering must be defined.  We lock the directory with
3401789Sahrens 	 * the smallest object id first, or if it's a tie, the one with
3402789Sahrens 	 * the lexically first name.
3403789Sahrens 	 */
3404789Sahrens 	if (sdzp->z_id < tdzp->z_id) {
3405789Sahrens 		cmp = -1;
3406789Sahrens 	} else if (sdzp->z_id > tdzp->z_id) {
3407789Sahrens 		cmp = 1;
3408789Sahrens 	} else {
34095331Samw 		/*
34105331Samw 		 * First compare the two name arguments without
34115331Samw 		 * considering any case folding.
34125331Samw 		 */
34135331Samw 		int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
34145331Samw 
34155331Samw 		cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
34165498Stimh 		ASSERT(error == 0 || !zfsvfs->z_utf8);
3417789Sahrens 		if (cmp == 0) {
3418789Sahrens 			/*
3419789Sahrens 			 * POSIX: "If the old argument and the new argument
3420789Sahrens 			 * both refer to links to the same existing file,
3421789Sahrens 			 * the rename() function shall return successfully
3422789Sahrens 			 * and perform no other action."
3423789Sahrens 			 */
3424789Sahrens 			ZFS_EXIT(zfsvfs);
3425789Sahrens 			return (0);
3426789Sahrens 		}
34275331Samw 		/*
34285331Samw 		 * If the file system is case-folding, then we may
34295331Samw 		 * have some more checking to do.  A case-folding file
34305331Samw 		 * system is either supporting mixed case sensitivity
34315331Samw 		 * access or is completely case-insensitive.  Note
34325331Samw 		 * that the file system is always case preserving.
34335331Samw 		 *
34345331Samw 		 * In mixed sensitivity mode case sensitive behavior
34355331Samw 		 * is the default.  FIGNORECASE must be used to
34365331Samw 		 * explicitly request case insensitive behavior.
34375331Samw 		 *
34385331Samw 		 * If the source and target names provided differ only
34395331Samw 		 * by case (e.g., a request to rename 'tim' to 'Tim'),
34405331Samw 		 * we will treat this as a special case in the
34415331Samw 		 * case-insensitive mode: as long as the source name
34425331Samw 		 * is an exact match, we will allow this to proceed as
34435331Samw 		 * a name-change request.
34445331Samw 		 */
34455498Stimh 		if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
34465498Stimh 		    (zfsvfs->z_case == ZFS_CASE_MIXED &&
34475498Stimh 		    flags & FIGNORECASE)) &&
34485331Samw 		    u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
34495331Samw 		    &error) == 0) {
34505331Samw 			/*
34515331Samw 			 * case preserving rename request, require exact
34525331Samw 			 * name matches
34535331Samw 			 */
34545331Samw 			zflg |= ZCIEXACT;
34555331Samw 			zflg &= ~ZCILOOK;
34565331Samw 		}
3457789Sahrens 	}
34585331Samw 
345911321SSanjeev.Bagewadi@Sun.COM 	/*
346011321SSanjeev.Bagewadi@Sun.COM 	 * If the source and destination directories are the same, we should
346111321SSanjeev.Bagewadi@Sun.COM 	 * grab the z_name_lock of that directory only once.
346211321SSanjeev.Bagewadi@Sun.COM 	 */
346311321SSanjeev.Bagewadi@Sun.COM 	if (sdzp == tdzp) {
346411321SSanjeev.Bagewadi@Sun.COM 		zflg |= ZHAVELOCK;
346511321SSanjeev.Bagewadi@Sun.COM 		rw_enter(&sdzp->z_name_lock, RW_READER);
346611321SSanjeev.Bagewadi@Sun.COM 	}
346711321SSanjeev.Bagewadi@Sun.COM 
3468789Sahrens 	if (cmp < 0) {
34695331Samw 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
34705331Samw 		    ZEXISTS | zflg, NULL, NULL);
34715331Samw 		terr = zfs_dirent_lock(&tdl,
34725331Samw 		    tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3473789Sahrens 	} else {
34745331Samw 		terr = zfs_dirent_lock(&tdl,
34755331Samw 		    tdzp, tnm, &tzp, zflg, NULL, NULL);
34765331Samw 		serr = zfs_dirent_lock(&sdl,
34775331Samw 		    sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
34785331Samw 		    NULL, NULL);
3479789Sahrens 	}
3480789Sahrens 
3481789Sahrens 	if (serr) {
3482789Sahrens 		/*
3483789Sahrens 		 * Source entry invalid or not there.
3484789Sahrens 		 */
3485789Sahrens 		if (!terr) {
3486789Sahrens 			zfs_dirent_unlock(tdl);
3487789Sahrens 			if (tzp)
3488789Sahrens 				VN_RELE(ZTOV(tzp));
3489789Sahrens 		}
349011321SSanjeev.Bagewadi@Sun.COM 
349111321SSanjeev.Bagewadi@Sun.COM 		if (sdzp == tdzp)
349211321SSanjeev.Bagewadi@Sun.COM 			rw_exit(&sdzp->z_name_lock);
349311321SSanjeev.Bagewadi@Sun.COM 
3494789Sahrens 		if (strcmp(snm, "..") == 0)
3495789Sahrens 			serr = EINVAL;
3496789Sahrens 		ZFS_EXIT(zfsvfs);
3497789Sahrens 		return (serr);
3498789Sahrens 	}
3499789Sahrens 	if (terr) {
3500789Sahrens 		zfs_dirent_unlock(sdl);
3501789Sahrens 		VN_RELE(ZTOV(szp));
350211321SSanjeev.Bagewadi@Sun.COM 
350311321SSanjeev.Bagewadi@Sun.COM 		if (sdzp == tdzp)
350411321SSanjeev.Bagewadi@Sun.COM 			rw_exit(&sdzp->z_name_lock);
350511321SSanjeev.Bagewadi@Sun.COM 
3506789Sahrens 		if (strcmp(tnm, "..") == 0)
3507789Sahrens 			terr = EINVAL;
3508789Sahrens 		ZFS_EXIT(zfsvfs);
3509789Sahrens 		return (terr);
3510789Sahrens 	}
3511789Sahrens 
3512789Sahrens 	/*
3513789Sahrens 	 * Must have write access at the source to remove the old entry
3514789Sahrens 	 * and write access at the target to create the new entry.
3515789Sahrens 	 * Note that if target and source are the same, this can be
3516789Sahrens 	 * done in a single check.
3517789Sahrens 	 */
3518789Sahrens 
3519789Sahrens 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3520789Sahrens 		goto out;
3521789Sahrens 
3522789Sahrens 	if (ZTOV(szp)->v_type == VDIR) {
3523789Sahrens 		/*
3524789Sahrens 		 * Check to make sure rename is valid.
3525789Sahrens 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3526789Sahrens 		 */
3527789Sahrens 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3528789Sahrens 			goto out;
3529789Sahrens 	}
3530789Sahrens 
3531789Sahrens 	/*
3532789Sahrens 	 * Does target exist?
3533789Sahrens 	 */
3534789Sahrens 	if (tzp) {
3535789Sahrens 		/*
3536789Sahrens 		 * Source and target must be the same type.
3537789Sahrens 		 */
3538789Sahrens 		if (ZTOV(szp)->v_type == VDIR) {
3539789Sahrens 			if (ZTOV(tzp)->v_type != VDIR) {
3540789Sahrens 				error = ENOTDIR;
3541789Sahrens 				goto out;
3542789Sahrens 			}
3543789Sahrens 		} else {
3544789Sahrens 			if (ZTOV(tzp)->v_type == VDIR) {
3545789Sahrens 				error = EISDIR;
3546789Sahrens 				goto out;
3547789Sahrens 			}
3548789Sahrens 		}
3549789Sahrens 		/*
3550789Sahrens 		 * POSIX dictates that when the source and target
3551789Sahrens 		 * entries refer to the same file object, rename
3552789Sahrens 		 * must do nothing and exit without error.
3553789Sahrens 		 */
3554789Sahrens 		if (szp->z_id == tzp->z_id) {
3555789Sahrens 			error = 0;
3556789Sahrens 			goto out;
3557789Sahrens 		}
3558789Sahrens 	}
3559789Sahrens 
35605331Samw 	vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3561789Sahrens 	if (tzp)
35625331Samw 		vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
35634863Spraks 
35644863Spraks 	/*
35654863Spraks 	 * notify the target directory if it is not the same
35664863Spraks 	 * as source directory.
35674863Spraks 	 */
35684863Spraks 	if (tdvp != sdvp) {
35695331Samw 		vnevent_rename_dest_dir(tdvp, ct);
35704863Spraks 	}
3571789Sahrens 
3572789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
357311935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
357411935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
35751544Seschrock 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
35761544Seschrock 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
357711935SMark.Shellenbaum@Sun.COM 	if (sdzp != tdzp) {
357811935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
357911935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, tdzp);
358011935SMark.Shellenbaum@Sun.COM 	}
358111935SMark.Shellenbaum@Sun.COM 	if (tzp) {
358211935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
358311935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, tzp);
358411935SMark.Shellenbaum@Sun.COM 	}
358511935SMark.Shellenbaum@Sun.COM 
358611935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, szp);
35873461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
35888227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3589789Sahrens 	if (error) {
3590789Sahrens 		if (zl != NULL)
3591789Sahrens 			zfs_rename_unlock(&zl);
3592789Sahrens 		zfs_dirent_unlock(sdl);
3593789Sahrens 		zfs_dirent_unlock(tdl);
359411321SSanjeev.Bagewadi@Sun.COM 
359511321SSanjeev.Bagewadi@Sun.COM 		if (sdzp == tdzp)
359611321SSanjeev.Bagewadi@Sun.COM 			rw_exit(&sdzp->z_name_lock);
359711321SSanjeev.Bagewadi@Sun.COM 
3598789Sahrens 		VN_RELE(ZTOV(szp));
3599789Sahrens 		if (tzp)
3600789Sahrens 			VN_RELE(ZTOV(tzp));
36018227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
36022113Sahrens 			dmu_tx_wait(tx);
36032113Sahrens 			dmu_tx_abort(tx);
3604789Sahrens 			goto top;
3605789Sahrens 		}
36062113Sahrens 		dmu_tx_abort(tx);
3607789Sahrens 		ZFS_EXIT(zfsvfs);
3608789Sahrens 		return (error);
3609789Sahrens 	}
3610789Sahrens 
3611789Sahrens 	if (tzp)	/* Attempt to remove the existing target */
36125331Samw 		error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3613789Sahrens 
3614789Sahrens 	if (error == 0) {
3615789Sahrens 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3616789Sahrens 		if (error == 0) {
361711935SMark.Shellenbaum@Sun.COM 			szp->z_pflags |= ZFS_AV_MODIFIED;
361811935SMark.Shellenbaum@Sun.COM 
361911935SMark.Shellenbaum@Sun.COM 			error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
362011935SMark.Shellenbaum@Sun.COM 			    (void *)&szp->z_pflags, sizeof (uint64_t), tx);
362111935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error, ==, 0);
36225331Samw 
3623789Sahrens 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
362412413SSam.Falkner@Sun.COM 			if (error == 0) {
362512413SSam.Falkner@Sun.COM 				zfs_log_rename(zilog, tx, TX_RENAME |
362612771SNeil.Perrin@Sun.COM 				    (flags & FIGNORECASE ? TX_CI : 0), sdzp,
362712771SNeil.Perrin@Sun.COM 				    sdl->dl_name, tdzp, tdl->dl_name, szp);
362812413SSam.Falkner@Sun.COM 
362912413SSam.Falkner@Sun.COM 				/*
363012413SSam.Falkner@Sun.COM 				 * Update path information for the target vnode
363112413SSam.Falkner@Sun.COM 				 */
363212413SSam.Falkner@Sun.COM 				vn_renamepath(tdvp, ZTOV(szp), tnm,
363312413SSam.Falkner@Sun.COM 				    strlen(tnm));
363412413SSam.Falkner@Sun.COM 			} else {
363512413SSam.Falkner@Sun.COM 				/*
363612413SSam.Falkner@Sun.COM 				 * At this point, we have successfully created
363712413SSam.Falkner@Sun.COM 				 * the target name, but have failed to remove
363812413SSam.Falkner@Sun.COM 				 * the source name.  Since the create was done
363912413SSam.Falkner@Sun.COM 				 * with the ZRENAMING flag, there are
364012413SSam.Falkner@Sun.COM 				 * complications; for one, the link count is
364112413SSam.Falkner@Sun.COM 				 * wrong.  The easiest way to deal with this
364212413SSam.Falkner@Sun.COM 				 * is to remove the newly created target, and
364312413SSam.Falkner@Sun.COM 				 * return the original error.  This must
364412413SSam.Falkner@Sun.COM 				 * succeed; fortunately, it is very unlikely to
364512413SSam.Falkner@Sun.COM 				 * fail, since we just created it.
364612413SSam.Falkner@Sun.COM 				 */
364712413SSam.Falkner@Sun.COM 				VERIFY3U(zfs_link_destroy(tdl, szp, tx,
364812413SSam.Falkner@Sun.COM 				    ZRENAMING, NULL), ==, 0);
364912413SSam.Falkner@Sun.COM 			}
3650789Sahrens 		}
3651789Sahrens 	}
3652789Sahrens 
3653789Sahrens 	dmu_tx_commit(tx);
3654789Sahrens out:
3655789Sahrens 	if (zl != NULL)
3656789Sahrens 		zfs_rename_unlock(&zl);
3657789Sahrens 
3658789Sahrens 	zfs_dirent_unlock(sdl);
3659789Sahrens 	zfs_dirent_unlock(tdl);
3660789Sahrens 
366111321SSanjeev.Bagewadi@Sun.COM 	if (sdzp == tdzp)
366211321SSanjeev.Bagewadi@Sun.COM 		rw_exit(&sdzp->z_name_lock);
366311321SSanjeev.Bagewadi@Sun.COM 
366411321SSanjeev.Bagewadi@Sun.COM 
3665789Sahrens 	VN_RELE(ZTOV(szp));
3666789Sahrens 	if (tzp)
3667789Sahrens 		VN_RELE(ZTOV(tzp));
3668789Sahrens 
366912294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
367012699SNeil.Perrin@Sun.COM 		zil_commit(zilog, 0);
367112294SMark.Musante@Sun.COM 
3672789Sahrens 	ZFS_EXIT(zfsvfs);
3673789Sahrens 	return (error);
3674789Sahrens }
3675789Sahrens 
3676789Sahrens /*
3677789Sahrens  * Insert the indicated symbolic reference entry into the directory.
3678789Sahrens  *
3679789Sahrens  *	IN:	dvp	- Directory to contain new symbolic link.
3680789Sahrens  *		link	- Name for new symlink entry.
3681789Sahrens  *		vap	- Attributes of new entry.
3682789Sahrens  *		target	- Target path of new symlink.
3683789Sahrens  *		cr	- credentials of caller.
36845331Samw  *		ct	- caller context
36855331Samw  *		flags	- case flags
3686789Sahrens  *
3687789Sahrens  *	RETURN:	0 if success
3688789Sahrens  *		error code if failure
3689789Sahrens  *
3690789Sahrens  * Timestamps:
3691789Sahrens  *	dvp - ctime|mtime updated
3692789Sahrens  */
36935331Samw /*ARGSUSED*/
3694789Sahrens static int
zfs_symlink(vnode_t * dvp,char * name,vattr_t * vap,char * link,cred_t * cr,caller_context_t * ct,int flags)36955331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
36965331Samw     caller_context_t *ct, int flags)
3697789Sahrens {
3698789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
3699789Sahrens 	zfs_dirlock_t	*dl;
3700789Sahrens 	dmu_tx_t	*tx;
3701789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
37025326Sek110237 	zilog_t		*zilog;
370311935SMark.Shellenbaum@Sun.COM 	uint64_t	len = strlen(link);
3704789Sahrens 	int		error;
37055331Samw 	int		zflg = ZNEW;
37069179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t	acl_ids;
37079179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
370811935SMark.Shellenbaum@Sun.COM 	uint64_t	txtype = TX_SYMLINK;
3709789Sahrens 
3710789Sahrens 	ASSERT(vap->va_type == VLNK);
3711789Sahrens 
37125367Sahrens 	ZFS_ENTER(zfsvfs);
37135367Sahrens 	ZFS_VERIFY_ZP(dzp);
37145326Sek110237 	zilog = zfsvfs->z_log;
37155331Samw 
37165498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
37175331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
37185331Samw 		ZFS_EXIT(zfsvfs);
37195331Samw 		return (EILSEQ);
37205331Samw 	}
37215331Samw 	if (flags & FIGNORECASE)
37225331Samw 		zflg |= ZCILOOK;
3723789Sahrens 
3724789Sahrens 	if (len > MAXPATHLEN) {
3725789Sahrens 		ZFS_EXIT(zfsvfs);
3726789Sahrens 		return (ENAMETOOLONG);
3727789Sahrens 	}
3728789Sahrens 
372912302SMark.Shellenbaum@Sun.COM 	if ((error = zfs_acl_ids_create(dzp, 0,
373012302SMark.Shellenbaum@Sun.COM 	    vap, cr, NULL, &acl_ids)) != 0) {
373112302SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
373212302SMark.Shellenbaum@Sun.COM 		return (error);
373312302SMark.Shellenbaum@Sun.COM 	}
373412302SMark.Shellenbaum@Sun.COM top:
3735789Sahrens 	/*
3736789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3737789Sahrens 	 */
37385331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
37395331Samw 	if (error) {
374012302SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
3741789Sahrens 		ZFS_EXIT(zfsvfs);
3742789Sahrens 		return (error);
3743789Sahrens 	}
3744789Sahrens 
374512302SMark.Shellenbaum@Sun.COM 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
374612302SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
374712421SMark.Shellenbaum@Sun.COM 		zfs_dirent_unlock(dl);
374812302SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
374912302SMark.Shellenbaum@Sun.COM 		return (error);
375012302SMark.Shellenbaum@Sun.COM 	}
375112302SMark.Shellenbaum@Sun.COM 
37529396SMatthew.Ahrens@Sun.COM 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
37539396SMatthew.Ahrens@Sun.COM 		zfs_acl_ids_free(&acl_ids);
37549396SMatthew.Ahrens@Sun.COM 		zfs_dirent_unlock(dl);
37559396SMatthew.Ahrens@Sun.COM 		ZFS_EXIT(zfsvfs);
37569396SMatthew.Ahrens@Sun.COM 		return (EDQUOT);
37579396SMatthew.Ahrens@Sun.COM 	}
3758789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
37599179SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
3760789Sahrens 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
37611544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
376211935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
376311935SMark.Shellenbaum@Sun.COM 	    ZFS_SA_BASE_ATTR_SIZE + len);
376411935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
376511935SMark.Shellenbaum@Sun.COM 	if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
376611935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
376711935SMark.Shellenbaum@Sun.COM 		    acl_ids.z_aclp->z_acl_bytes);
376811935SMark.Shellenbaum@Sun.COM 	}
37699396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
37709396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
37718227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3772789Sahrens 	if (error) {
3773789Sahrens 		zfs_dirent_unlock(dl);
37748227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
37752113Sahrens 			dmu_tx_wait(tx);
37762113Sahrens 			dmu_tx_abort(tx);
3777789Sahrens 			goto top;
3778789Sahrens 		}
377912302SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
37802113Sahrens 		dmu_tx_abort(tx);
3781789Sahrens 		ZFS_EXIT(zfsvfs);
3782789Sahrens 		return (error);
3783789Sahrens 	}
3784789Sahrens 
3785789Sahrens 	/*
3786789Sahrens 	 * Create a new object for the symlink.
378711935SMark.Shellenbaum@Sun.COM 	 * for version 4 ZPL datsets the symlink will be an SA attribute
3788789Sahrens 	 */
378911935SMark.Shellenbaum@Sun.COM 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
379011935SMark.Shellenbaum@Sun.COM 
379111935SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
379211935SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
379311935SMark.Shellenbaum@Sun.COM 
379412620SMark.Shellenbaum@Oracle.COM 	mutex_enter(&zp->z_lock);
379511935SMark.Shellenbaum@Sun.COM 	if (zp->z_is_sa)
379611935SMark.Shellenbaum@Sun.COM 		error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
379711935SMark.Shellenbaum@Sun.COM 		    link, len, tx);
379811935SMark.Shellenbaum@Sun.COM 	else
379911935SMark.Shellenbaum@Sun.COM 		zfs_sa_symlink(zp, link, len, tx);
380012620SMark.Shellenbaum@Oracle.COM 	mutex_exit(&zp->z_lock);
380111935SMark.Shellenbaum@Sun.COM 
380211935SMark.Shellenbaum@Sun.COM 	zp->z_size = len;
380311935SMark.Shellenbaum@Sun.COM 	(void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
380411935SMark.Shellenbaum@Sun.COM 	    &zp->z_size, sizeof (zp->z_size), tx);
3805789Sahrens 	/*
3806789Sahrens 	 * Insert the new object into the directory.
3807789Sahrens 	 */
3808789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
380911935SMark.Shellenbaum@Sun.COM 
381011935SMark.Shellenbaum@Sun.COM 	if (flags & FIGNORECASE)
381111935SMark.Shellenbaum@Sun.COM 		txtype |= TX_CI;
381211935SMark.Shellenbaum@Sun.COM 	zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
38139179SMark.Shellenbaum@Sun.COM 
38149179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_free(&acl_ids);
3815789Sahrens 
3816789Sahrens 	dmu_tx_commit(tx);
3817789Sahrens 
3818789Sahrens 	zfs_dirent_unlock(dl);
3819789Sahrens 
3820789Sahrens 	VN_RELE(ZTOV(zp));
3821789Sahrens 
382212294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
382312699SNeil.Perrin@Sun.COM 		zil_commit(zilog, 0);
382412294SMark.Musante@Sun.COM 
3825789Sahrens 	ZFS_EXIT(zfsvfs);
3826789Sahrens 	return (error);
3827789Sahrens }
3828789Sahrens 
3829789Sahrens /*
3830789Sahrens  * Return, in the buffer contained in the provided uio structure,
3831789Sahrens  * the symbolic path referred to by vp.
3832789Sahrens  *
3833789Sahrens  *	IN:	vp	- vnode of symbolic link.
3834789Sahrens  *		uoip	- structure to contain the link path.
3835789Sahrens  *		cr	- credentials of caller.
38365331Samw  *		ct	- caller context
3837789Sahrens  *
3838789Sahrens  *	OUT:	uio	- structure to contain the link path.
3839789Sahrens  *
3840789Sahrens  *	RETURN:	0 if success
3841789Sahrens  *		error code if failure
3842789Sahrens  *
3843789Sahrens  * Timestamps:
3844789Sahrens  *	vp - atime updated
3845789Sahrens  */
3846789Sahrens /* ARGSUSED */
3847789Sahrens static int
zfs_readlink(vnode_t * vp,uio_t * uio,cred_t * cr,caller_context_t * ct)38485331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3849789Sahrens {
3850789Sahrens 	znode_t		*zp = VTOZ(vp);
3851789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3852789Sahrens 	int		error;
3853789Sahrens 
38545367Sahrens 	ZFS_ENTER(zfsvfs);
38555367Sahrens 	ZFS_VERIFY_ZP(zp);
3856789Sahrens 
385712620SMark.Shellenbaum@Oracle.COM 	mutex_enter(&zp->z_lock);
385811935SMark.Shellenbaum@Sun.COM 	if (zp->z_is_sa)
385911935SMark.Shellenbaum@Sun.COM 		error = sa_lookup_uio(zp->z_sa_hdl,
386011935SMark.Shellenbaum@Sun.COM 		    SA_ZPL_SYMLINK(zfsvfs), uio);
386111935SMark.Shellenbaum@Sun.COM 	else
386211935SMark.Shellenbaum@Sun.COM 		error = zfs_sa_readlink(zp, uio);
386312620SMark.Shellenbaum@Oracle.COM 	mutex_exit(&zp->z_lock);
3864789Sahrens 
3865789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
386611935SMark.Shellenbaum@Sun.COM 
3867789Sahrens 	ZFS_EXIT(zfsvfs);
3868789Sahrens 	return (error);
3869789Sahrens }
3870789Sahrens 
3871789Sahrens /*
3872789Sahrens  * Insert a new entry into directory tdvp referencing svp.
3873789Sahrens  *
3874789Sahrens  *	IN:	tdvp	- Directory to contain new entry.
3875789Sahrens  *		svp	- vnode of new entry.
3876789Sahrens  *		name	- name of new entry.
3877789Sahrens  *		cr	- credentials of caller.
38785331Samw  *		ct	- caller context
3879789Sahrens  *
3880789Sahrens  *	RETURN:	0 if success
3881789Sahrens  *		error code if failure
3882789Sahrens  *
3883789Sahrens  * Timestamps:
3884789Sahrens  *	tdvp - ctime|mtime updated
3885789Sahrens  *	 svp - ctime updated
3886789Sahrens  */
3887789Sahrens /* ARGSUSED */
3888789Sahrens static int
zfs_link(vnode_t * tdvp,vnode_t * svp,char * name,cred_t * cr,caller_context_t * ct,int flags)38895331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
38905331Samw     caller_context_t *ct, int flags)
3891789Sahrens {
3892789Sahrens 	znode_t		*dzp = VTOZ(tdvp);
3893789Sahrens 	znode_t		*tzp, *szp;
3894789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
38955326Sek110237 	zilog_t		*zilog;
3896789Sahrens 	zfs_dirlock_t	*dl;
3897789Sahrens 	dmu_tx_t	*tx;
3898789Sahrens 	vnode_t		*realvp;
3899789Sahrens 	int		error;
39005331Samw 	int		zf = ZNEW;
390112079SMark.Shellenbaum@Sun.COM 	uint64_t	parent;
390213069SMark.Shellenbaum@Oracle.COM 	uid_t		owner;
3903789Sahrens 
3904789Sahrens 	ASSERT(tdvp->v_type == VDIR);
3905789Sahrens 
39065367Sahrens 	ZFS_ENTER(zfsvfs);
39075367Sahrens 	ZFS_VERIFY_ZP(dzp);
39085326Sek110237 	zilog = zfsvfs->z_log;
3909789Sahrens 
39105331Samw 	if (VOP_REALVP(svp, &realvp, ct) == 0)
3911789Sahrens 		svp = realvp;
3912789Sahrens 
391312079SMark.Shellenbaum@Sun.COM 	/*
391412079SMark.Shellenbaum@Sun.COM 	 * POSIX dictates that we return EPERM here.
391512079SMark.Shellenbaum@Sun.COM 	 * Better choices include ENOTSUP or EISDIR.
391612079SMark.Shellenbaum@Sun.COM 	 */
391712079SMark.Shellenbaum@Sun.COM 	if (svp->v_type == VDIR) {
391812079SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
391912079SMark.Shellenbaum@Sun.COM 		return (EPERM);
392012079SMark.Shellenbaum@Sun.COM 	}
392112079SMark.Shellenbaum@Sun.COM 
392212079SMark.Shellenbaum@Sun.COM 	if (svp->v_vfsp != tdvp->v_vfsp || zfsctl_is_node(svp)) {
3923789Sahrens 		ZFS_EXIT(zfsvfs);
3924789Sahrens 		return (EXDEV);
3925789Sahrens 	}
392612079SMark.Shellenbaum@Sun.COM 
39275367Sahrens 	szp = VTOZ(svp);
39285367Sahrens 	ZFS_VERIFY_ZP(szp);
3929789Sahrens 
393012079SMark.Shellenbaum@Sun.COM 	/* Prevent links to .zfs/shares files */
393112079SMark.Shellenbaum@Sun.COM 
393212079SMark.Shellenbaum@Sun.COM 	if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
393312079SMark.Shellenbaum@Sun.COM 	    &parent, sizeof (uint64_t))) != 0) {
393412079SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
393512079SMark.Shellenbaum@Sun.COM 		return (error);
393612079SMark.Shellenbaum@Sun.COM 	}
393712079SMark.Shellenbaum@Sun.COM 	if (parent == zfsvfs->z_shares_dir) {
393812079SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
393912079SMark.Shellenbaum@Sun.COM 		return (EPERM);
394012079SMark.Shellenbaum@Sun.COM 	}
394112079SMark.Shellenbaum@Sun.COM 
39425498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name,
39435331Samw 	    strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
39445331Samw 		ZFS_EXIT(zfsvfs);
39455331Samw 		return (EILSEQ);
39465331Samw 	}
39475331Samw 	if (flags & FIGNORECASE)
39485331Samw 		zf |= ZCILOOK;
39495331Samw 
3950789Sahrens 	/*
3951789Sahrens 	 * We do not support links between attributes and non-attributes
3952789Sahrens 	 * because of the potential security risk of creating links
3953789Sahrens 	 * into "normal" file space in order to circumvent restrictions
3954789Sahrens 	 * imposed in attribute space.
3955789Sahrens 	 */
395611935SMark.Shellenbaum@Sun.COM 	if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
3957789Sahrens 		ZFS_EXIT(zfsvfs);
3958789Sahrens 		return (EINVAL);
3959789Sahrens 	}
3960789Sahrens 
3961789Sahrens 
396213069SMark.Shellenbaum@Oracle.COM 	owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
396313069SMark.Shellenbaum@Oracle.COM 	if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
3964789Sahrens 		ZFS_EXIT(zfsvfs);
3965789Sahrens 		return (EPERM);
3966789Sahrens 	}
3967789Sahrens 
39685331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3969789Sahrens 		ZFS_EXIT(zfsvfs);
3970789Sahrens 		return (error);
3971789Sahrens 	}
3972789Sahrens 
397312079SMark.Shellenbaum@Sun.COM top:
3974789Sahrens 	/*
3975789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3976789Sahrens 	 */
39775331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
39785331Samw 	if (error) {
3979789Sahrens 		ZFS_EXIT(zfsvfs);
3980789Sahrens 		return (error);
3981789Sahrens 	}
3982789Sahrens 
3983789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
398411935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
39851544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
398611935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, szp);
398711935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, dzp);
39888227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3989789Sahrens 	if (error) {
3990789Sahrens 		zfs_dirent_unlock(dl);
39918227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
39922113Sahrens 			dmu_tx_wait(tx);
39932113Sahrens 			dmu_tx_abort(tx);
3994789Sahrens 			goto top;
3995789Sahrens 		}
39962113Sahrens 		dmu_tx_abort(tx);
3997789Sahrens 		ZFS_EXIT(zfsvfs);
3998789Sahrens 		return (error);
3999789Sahrens 	}
4000789Sahrens 
4001789Sahrens 	error = zfs_link_create(dl, szp, tx, 0);
4002789Sahrens 
40035331Samw 	if (error == 0) {
40045331Samw 		uint64_t txtype = TX_LINK;
40055331Samw 		if (flags & FIGNORECASE)
40065331Samw 			txtype |= TX_CI;
40075331Samw 		zfs_log_link(zilog, tx, txtype, dzp, szp, name);
40085331Samw 	}
4009789Sahrens 
4010789Sahrens 	dmu_tx_commit(tx);
4011789Sahrens 
4012789Sahrens 	zfs_dirent_unlock(dl);
4013789Sahrens 
40144863Spraks 	if (error == 0) {
40155331Samw 		vnevent_link(svp, ct);
40164863Spraks 	}
40174863Spraks 
401812294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
401912699SNeil.Perrin@Sun.COM 		zil_commit(zilog, 0);
402012294SMark.Musante@Sun.COM 
4021789Sahrens 	ZFS_EXIT(zfsvfs);
4022789Sahrens 	return (error);
4023789Sahrens }
4024789Sahrens 
4025789Sahrens /*
4026789Sahrens  * zfs_null_putapage() is used when the file system has been force
4027789Sahrens  * unmounted. It just drops the pages.
4028789Sahrens  */
4029789Sahrens /* ARGSUSED */
4030789Sahrens static int
zfs_null_putapage(vnode_t * vp,page_t * pp,u_offset_t * offp,size_t * lenp,int flags,cred_t * cr)4031789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4032789Sahrens 		size_t *lenp, int flags, cred_t *cr)
4033789Sahrens {
4034789Sahrens 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
4035789Sahrens 	return (0);
4036789Sahrens }
4037789Sahrens 
40382688Smaybee /*
40392688Smaybee  * Push a page out to disk, klustering if possible.
40402688Smaybee  *
40412688Smaybee  *	IN:	vp	- file to push page to.
40422688Smaybee  *		pp	- page to push.
40432688Smaybee  *		flags	- additional flags.
40442688Smaybee  *		cr	- credentials of caller.
40452688Smaybee  *
40462688Smaybee  *	OUT:	offp	- start of range pushed.
40472688Smaybee  *		lenp	- len of range pushed.
40482688Smaybee  *
40492688Smaybee  *	RETURN:	0 if success
40502688Smaybee  *		error code if failure
40512688Smaybee  *
40522688Smaybee  * NOTE: callers must have locked the page to be pushed.  On
40532688Smaybee  * exit, the page (and all other pages in the kluster) must be
40542688Smaybee  * unlocked.
40552688Smaybee  */
4056789Sahrens /* ARGSUSED */
4057789Sahrens static int
zfs_putapage(vnode_t * vp,page_t * pp,u_offset_t * offp,size_t * lenp,int flags,cred_t * cr)4058789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4059789Sahrens 		size_t *lenp, int flags, cred_t *cr)
4060789Sahrens {
4061789Sahrens 	znode_t		*zp = VTOZ(vp);
4062789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4063789Sahrens 	dmu_tx_t	*tx;
40642688Smaybee 	u_offset_t	off, koff;
40652688Smaybee 	size_t		len, klen;
4066789Sahrens 	int		err;
4067789Sahrens 
40682688Smaybee 	off = pp->p_offset;
40692688Smaybee 	len = PAGESIZE;
40702688Smaybee 	/*
40712688Smaybee 	 * If our blocksize is bigger than the page size, try to kluster
40728227SNeil.Perrin@Sun.COM 	 * multiple pages so that we write a full block (thus avoiding
40732688Smaybee 	 * a read-modify-write).
40742688Smaybee 	 */
407511935SMark.Shellenbaum@Sun.COM 	if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
40768636SMark.Maybee@Sun.COM 		klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
40778636SMark.Maybee@Sun.COM 		koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
407811935SMark.Shellenbaum@Sun.COM 		ASSERT(koff <= zp->z_size);
407911935SMark.Shellenbaum@Sun.COM 		if (koff + klen > zp->z_size)
408011935SMark.Shellenbaum@Sun.COM 			klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
40812688Smaybee 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
40822688Smaybee 	}
40832688Smaybee 	ASSERT3U(btop(len), ==, btopr(len));
40848636SMark.Maybee@Sun.COM 
40851819Smaybee 	/*
40861819Smaybee 	 * Can't push pages past end-of-file.
40871819Smaybee 	 */
408811935SMark.Shellenbaum@Sun.COM 	if (off >= zp->z_size) {
40894709Smaybee 		/* ignore all pages */
40902688Smaybee 		err = 0;
40912688Smaybee 		goto out;
409211935SMark.Shellenbaum@Sun.COM 	} else if (off + len > zp->z_size) {
409311935SMark.Shellenbaum@Sun.COM 		int npages = btopr(zp->z_size - off);
40942688Smaybee 		page_t *trunc;
40952688Smaybee 
40962688Smaybee 		page_list_break(&pp, &trunc, npages);
40974709Smaybee 		/* ignore pages past end of file */
40982688Smaybee 		if (trunc)
40994709Smaybee 			pvn_write_done(trunc, flags);
410011935SMark.Shellenbaum@Sun.COM 		len = zp->z_size - off;
41011819Smaybee 	}
41029396SMatthew.Ahrens@Sun.COM 
410311935SMark.Shellenbaum@Sun.COM 	if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
410411935SMark.Shellenbaum@Sun.COM 	    zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
41059396SMatthew.Ahrens@Sun.COM 		err = EDQUOT;
41069396SMatthew.Ahrens@Sun.COM 		goto out;
41079396SMatthew.Ahrens@Sun.COM 	}
41088636SMark.Maybee@Sun.COM top:
4109789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
4110789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, off, len);
411111935SMark.Shellenbaum@Sun.COM 
411211935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
411311935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
41148227SNeil.Perrin@Sun.COM 	err = dmu_tx_assign(tx, TXG_NOWAIT);
4115789Sahrens 	if (err != 0) {
41168227SNeil.Perrin@Sun.COM 		if (err == ERESTART) {
41172113Sahrens 			dmu_tx_wait(tx);
41182113Sahrens 			dmu_tx_abort(tx);
4119789Sahrens 			goto top;
4120789Sahrens 		}
41212113Sahrens 		dmu_tx_abort(tx);
4122789Sahrens 		goto out;
4123789Sahrens 	}
4124789Sahrens 
41252688Smaybee 	if (zp->z_blksz <= PAGESIZE) {
41267315SJonathan.Adams@Sun.COM 		caddr_t va = zfs_map_page(pp, S_READ);
41272688Smaybee 		ASSERT3U(len, <=, PAGESIZE);
41282688Smaybee 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
41297315SJonathan.Adams@Sun.COM 		zfs_unmap_page(pp, va);
41302688Smaybee 	} else {
41312688Smaybee 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
41322688Smaybee 	}
41332688Smaybee 
41342688Smaybee 	if (err == 0) {
413511935SMark.Shellenbaum@Sun.COM 		uint64_t mtime[2], ctime[2];
413612394SMark.Shellenbaum@Sun.COM 		sa_bulk_attr_t bulk[3];
413711935SMark.Shellenbaum@Sun.COM 		int count = 0;
413811935SMark.Shellenbaum@Sun.COM 
413911935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
414011935SMark.Shellenbaum@Sun.COM 		    &mtime, 16);
414111935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
414211935SMark.Shellenbaum@Sun.COM 		    &ctime, 16);
414312394SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
414412394SMark.Shellenbaum@Sun.COM 		    &zp->z_pflags, 8);
414511935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
414611935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
41478636SMark.Maybee@Sun.COM 		zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
41482688Smaybee 	}
41499951SLin.Ling@Sun.COM 	dmu_tx_commit(tx);
41502688Smaybee 
41512688Smaybee out:
41524709Smaybee 	pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4153789Sahrens 	if (offp)
4154789Sahrens 		*offp = off;
4155789Sahrens 	if (lenp)
4156789Sahrens 		*lenp = len;
4157789Sahrens 
4158789Sahrens 	return (err);
4159789Sahrens }
4160789Sahrens 
4161789Sahrens /*
4162789Sahrens  * Copy the portion of the file indicated from pages into the file.
4163789Sahrens  * The pages are stored in a page list attached to the files vnode.
4164789Sahrens  *
4165789Sahrens  *	IN:	vp	- vnode of file to push page data to.
4166789Sahrens  *		off	- position in file to put data.
4167789Sahrens  *		len	- amount of data to write.
4168789Sahrens  *		flags	- flags to control the operation.
4169789Sahrens  *		cr	- credentials of caller.
41705331Samw  *		ct	- caller context.
4171789Sahrens  *
4172789Sahrens  *	RETURN:	0 if success
4173789Sahrens  *		error code if failure
4174789Sahrens  *
4175789Sahrens  * Timestamps:
4176789Sahrens  *	vp - ctime|mtime updated
4177789Sahrens  */
41785331Samw /*ARGSUSED*/
4179789Sahrens static int
zfs_putpage(vnode_t * vp,offset_t off,size_t len,int flags,cred_t * cr,caller_context_t * ct)41805331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
41815331Samw     caller_context_t *ct)
4182789Sahrens {
4183789Sahrens 	znode_t		*zp = VTOZ(vp);
4184789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4185789Sahrens 	page_t		*pp;
4186789Sahrens 	size_t		io_len;
4187789Sahrens 	u_offset_t	io_off;
41888636SMark.Maybee@Sun.COM 	uint_t		blksz;
41898636SMark.Maybee@Sun.COM 	rl_t		*rl;
4190789Sahrens 	int		error = 0;
4191789Sahrens 
41925367Sahrens 	ZFS_ENTER(zfsvfs);
41935367Sahrens 	ZFS_VERIFY_ZP(zp);
4194789Sahrens 
41958636SMark.Maybee@Sun.COM 	/*
41968636SMark.Maybee@Sun.COM 	 * Align this request to the file block size in case we kluster.
41978636SMark.Maybee@Sun.COM 	 * XXX - this can result in pretty aggresive locking, which can
41988636SMark.Maybee@Sun.COM 	 * impact simultanious read/write access.  One option might be
41998636SMark.Maybee@Sun.COM 	 * to break up long requests (len == 0) into block-by-block
42008636SMark.Maybee@Sun.COM 	 * operations to get narrower locking.
42018636SMark.Maybee@Sun.COM 	 */
42028636SMark.Maybee@Sun.COM 	blksz = zp->z_blksz;
42038636SMark.Maybee@Sun.COM 	if (ISP2(blksz))
42048636SMark.Maybee@Sun.COM 		io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
42058636SMark.Maybee@Sun.COM 	else
42068636SMark.Maybee@Sun.COM 		io_off = 0;
42078636SMark.Maybee@Sun.COM 	if (len > 0 && ISP2(blksz))
42089141SMark.Maybee@Sun.COM 		io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
42098636SMark.Maybee@Sun.COM 	else
42108636SMark.Maybee@Sun.COM 		io_len = 0;
42118636SMark.Maybee@Sun.COM 
42128636SMark.Maybee@Sun.COM 	if (io_len == 0) {
4213789Sahrens 		/*
42148636SMark.Maybee@Sun.COM 		 * Search the entire vp list for pages >= io_off.
4215789Sahrens 		 */
42168636SMark.Maybee@Sun.COM 		rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
42178636SMark.Maybee@Sun.COM 		error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
42181472Sperrin 		goto out;
4219789Sahrens 	}
42208636SMark.Maybee@Sun.COM 	rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
42218636SMark.Maybee@Sun.COM 
422211935SMark.Shellenbaum@Sun.COM 	if (off > zp->z_size) {
4223789Sahrens 		/* past end of file */
42248636SMark.Maybee@Sun.COM 		zfs_range_unlock(rl);
4225789Sahrens 		ZFS_EXIT(zfsvfs);
4226789Sahrens 		return (0);
4227789Sahrens 	}
4228789Sahrens 
422911935SMark.Shellenbaum@Sun.COM 	len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
42308636SMark.Maybee@Sun.COM 
42318636SMark.Maybee@Sun.COM 	for (off = io_off; io_off < off + len; io_off += io_len) {
4232789Sahrens 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
42331669Sperrin 			pp = page_lookup(vp, io_off,
42344339Sperrin 			    (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4235789Sahrens 		} else {
4236789Sahrens 			pp = page_lookup_nowait(vp, io_off,
42374339Sperrin 			    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4238789Sahrens 		}
4239789Sahrens 
4240789Sahrens 		if (pp != NULL && pvn_getdirty(pp, flags)) {
4241789Sahrens 			int err;
4242789Sahrens 
4243789Sahrens 			/*
4244789Sahrens 			 * Found a dirty page to push
4245789Sahrens 			 */
42461669Sperrin 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
42471669Sperrin 			if (err)
4248789Sahrens 				error = err;
4249789Sahrens 		} else {
4250789Sahrens 			io_len = PAGESIZE;
4251789Sahrens 		}
4252789Sahrens 	}
42531472Sperrin out:
42548636SMark.Maybee@Sun.COM 	zfs_range_unlock(rl);
425512294SMark.Musante@Sun.COM 	if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
425612699SNeil.Perrin@Sun.COM 		zil_commit(zfsvfs->z_log, zp->z_id);
4257789Sahrens 	ZFS_EXIT(zfsvfs);
4258789Sahrens 	return (error);
4259789Sahrens }
4260789Sahrens 
42615331Samw /*ARGSUSED*/
4262789Sahrens void
zfs_inactive(vnode_t * vp,cred_t * cr,caller_context_t * ct)42635331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4264789Sahrens {
4265789Sahrens 	znode_t	*zp = VTOZ(vp);
4266789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4267789Sahrens 	int error;
4268789Sahrens 
42695326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
427011935SMark.Shellenbaum@Sun.COM 	if (zp->z_sa_hdl == NULL) {
42715446Sahrens 		/*
42725642Smaybee 		 * The fs has been unmounted, or we did a
42735642Smaybee 		 * suspend/resume and this file no longer exists.
42745446Sahrens 		 */
4275789Sahrens 		if (vn_has_cached_data(vp)) {
4276789Sahrens 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
4277789Sahrens 			    B_INVAL, cr);
4278789Sahrens 		}
4279789Sahrens 
42801544Seschrock 		mutex_enter(&zp->z_lock);
428110369Schris.kirby@sun.com 		mutex_enter(&vp->v_lock);
428210369Schris.kirby@sun.com 		ASSERT(vp->v_count == 1);
428310369Schris.kirby@sun.com 		vp->v_count = 0;
428410369Schris.kirby@sun.com 		mutex_exit(&vp->v_lock);
42855446Sahrens 		mutex_exit(&zp->z_lock);
42865642Smaybee 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
42875446Sahrens 		zfs_znode_free(zp);
4288789Sahrens 		return;
4289789Sahrens 	}
4290789Sahrens 
4291789Sahrens 	/*
4292789Sahrens 	 * Attempt to push any data in the page cache.  If this fails
4293789Sahrens 	 * we will get kicked out later in zfs_zinactive().
4294789Sahrens 	 */
42951298Sperrin 	if (vn_has_cached_data(vp)) {
42961298Sperrin 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
42971298Sperrin 		    cr);
42981298Sperrin 	}
4299789Sahrens 
43003461Sahrens 	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4301789Sahrens 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4302789Sahrens 
430311935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
430411935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, zp);
4305789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
4306789Sahrens 		if (error) {
4307789Sahrens 			dmu_tx_abort(tx);
4308789Sahrens 		} else {
4309789Sahrens 			mutex_enter(&zp->z_lock);
431011935SMark.Shellenbaum@Sun.COM 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
431111935SMark.Shellenbaum@Sun.COM 			    (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4312789Sahrens 			zp->z_atime_dirty = 0;
4313789Sahrens 			mutex_exit(&zp->z_lock);
4314789Sahrens 			dmu_tx_commit(tx);
4315789Sahrens 		}
4316789Sahrens 	}
4317789Sahrens 
4318789Sahrens 	zfs_zinactive(zp);
43195326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
4320789Sahrens }
4321789Sahrens 
4322789Sahrens /*
4323789Sahrens  * Bounds-check the seek operation.
4324789Sahrens  *
4325789Sahrens  *	IN:	vp	- vnode seeking within
4326789Sahrens  *		ooff	- old file offset
4327789Sahrens  *		noffp	- pointer to new file offset
43285331Samw  *		ct	- caller context
4329789Sahrens  *
4330789Sahrens  *	RETURN:	0 if success
4331789Sahrens  *		EINVAL if new offset invalid
4332789Sahrens  */
4333789Sahrens /* ARGSUSED */
4334789Sahrens static int
zfs_seek(vnode_t * vp,offset_t ooff,offset_t * noffp,caller_context_t * ct)43355331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
43365331Samw     caller_context_t *ct)
4337789Sahrens {
4338789Sahrens 	if (vp->v_type == VDIR)
4339789Sahrens 		return (0);
4340789Sahrens 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4341789Sahrens }
4342789Sahrens 
4343789Sahrens /*
4344789Sahrens  * Pre-filter the generic locking function to trap attempts to place
4345789Sahrens  * a mandatory lock on a memory mapped file.
4346789Sahrens  */
4347789Sahrens static int
zfs_frlock(vnode_t * vp,int cmd,flock64_t * bfp,int flag,offset_t offset,flk_callback_t * flk_cbp,cred_t * cr,caller_context_t * ct)4348789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
43495331Samw     flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4350789Sahrens {
4351789Sahrens 	znode_t *zp = VTOZ(vp);
4352789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4353789Sahrens 
43545367Sahrens 	ZFS_ENTER(zfsvfs);
43555367Sahrens 	ZFS_VERIFY_ZP(zp);
4356789Sahrens 
4357789Sahrens 	/*
43581544Seschrock 	 * We are following the UFS semantics with respect to mapcnt
43591544Seschrock 	 * here: If we see that the file is mapped already, then we will
43601544Seschrock 	 * return an error, but we don't worry about races between this
43611544Seschrock 	 * function and zfs_map().
4362789Sahrens 	 */
436311935SMark.Shellenbaum@Sun.COM 	if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4364789Sahrens 		ZFS_EXIT(zfsvfs);
4365789Sahrens 		return (EAGAIN);
4366789Sahrens 	}
4367789Sahrens 	ZFS_EXIT(zfsvfs);
436810896SMark.Shellenbaum@Sun.COM 	return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4369789Sahrens }
4370789Sahrens 
4371789Sahrens /*
4372789Sahrens  * If we can't find a page in the cache, we will create a new page
4373789Sahrens  * and fill it with file data.  For efficiency, we may try to fill
43748636SMark.Maybee@Sun.COM  * multiple pages at once (klustering) to fill up the supplied page
43759265SMark.Maybee@Sun.COM  * list.  Note that the pages to be filled are held with an exclusive
43769265SMark.Maybee@Sun.COM  * lock to prevent access by other threads while they are being filled.
4377789Sahrens  */
4378789Sahrens static int
zfs_fillpage(vnode_t * vp,u_offset_t off,struct seg * seg,caddr_t addr,page_t * pl[],size_t plsz,enum seg_rw rw)4379789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4380789Sahrens     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4381789Sahrens {
4382789Sahrens 	znode_t *zp = VTOZ(vp);
4383789Sahrens 	page_t *pp, *cur_pp;
4384789Sahrens 	objset_t *os = zp->z_zfsvfs->z_os;
4385789Sahrens 	u_offset_t io_off, total;
4386789Sahrens 	size_t io_len;
4387789Sahrens 	int err;
4388789Sahrens 
43892688Smaybee 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
43908636SMark.Maybee@Sun.COM 		/*
43918636SMark.Maybee@Sun.COM 		 * We only have a single page, don't bother klustering
43928636SMark.Maybee@Sun.COM 		 */
4393789Sahrens 		io_off = off;
4394789Sahrens 		io_len = PAGESIZE;
43959265SMark.Maybee@Sun.COM 		pp = page_create_va(vp, io_off, io_len,
43969265SMark.Maybee@Sun.COM 		    PG_EXCL | PG_WAIT, seg, addr);
4397789Sahrens 	} else {
4398789Sahrens 		/*
43998636SMark.Maybee@Sun.COM 		 * Try to find enough pages to fill the page list
4400789Sahrens 		 */
4401789Sahrens 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
44028636SMark.Maybee@Sun.COM 		    &io_len, off, plsz, 0);
4403789Sahrens 	}
4404789Sahrens 	if (pp == NULL) {
4405789Sahrens 		/*
44068636SMark.Maybee@Sun.COM 		 * The page already exists, nothing to do here.
4407789Sahrens 		 */
4408789Sahrens 		*pl = NULL;
4409789Sahrens 		return (0);
4410789Sahrens 	}
4411789Sahrens 
4412789Sahrens 	/*
4413789Sahrens 	 * Fill the pages in the kluster.
4414789Sahrens 	 */
4415789Sahrens 	cur_pp = pp;
4416789Sahrens 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
44178636SMark.Maybee@Sun.COM 		caddr_t va;
44188636SMark.Maybee@Sun.COM 
44192688Smaybee 		ASSERT3U(io_off, ==, cur_pp->p_offset);
44207315SJonathan.Adams@Sun.COM 		va = zfs_map_page(cur_pp, S_WRITE);
44219512SNeil.Perrin@Sun.COM 		err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
44229512SNeil.Perrin@Sun.COM 		    DMU_READ_PREFETCH);
44237315SJonathan.Adams@Sun.COM 		zfs_unmap_page(cur_pp, va);
4424789Sahrens 		if (err) {
4425789Sahrens 			/* On error, toss the entire kluster */
4426789Sahrens 			pvn_read_done(pp, B_ERROR);
44277294Sperrin 			/* convert checksum errors into IO errors */
44287294Sperrin 			if (err == ECKSUM)
44297294Sperrin 				err = EIO;
4430789Sahrens 			return (err);
4431789Sahrens 		}
4432789Sahrens 		cur_pp = cur_pp->p_next;
4433789Sahrens 	}
44348636SMark.Maybee@Sun.COM 
4435789Sahrens 	/*
44368636SMark.Maybee@Sun.COM 	 * Fill in the page list array from the kluster starting
44378636SMark.Maybee@Sun.COM 	 * from the desired offset `off'.
4438789Sahrens 	 * NOTE: the page list will always be null terminated.
4439789Sahrens 	 */
4440789Sahrens 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
44418636SMark.Maybee@Sun.COM 	ASSERT(pl == NULL || (*pl)->p_offset == off);
4442789Sahrens 
4443789Sahrens 	return (0);
4444789Sahrens }
4445789Sahrens 
4446789Sahrens /*
4447789Sahrens  * Return pointers to the pages for the file region [off, off + len]
4448789Sahrens  * in the pl array.  If plsz is greater than len, this function may
44498636SMark.Maybee@Sun.COM  * also return page pointers from after the specified region
44508636SMark.Maybee@Sun.COM  * (i.e. the region [off, off + plsz]).  These additional pages are
44518636SMark.Maybee@Sun.COM  * only returned if they are already in the cache, or were created as
44528636SMark.Maybee@Sun.COM  * part of a klustered read.
4453789Sahrens  *
4454789Sahrens  *	IN:	vp	- vnode of file to get data from.
4455789Sahrens  *		off	- position in file to get data from.
4456789Sahrens  *		len	- amount of data to retrieve.
4457789Sahrens  *		plsz	- length of provided page list.
4458789Sahrens  *		seg	- segment to obtain pages for.
4459789Sahrens  *		addr	- virtual address of fault.
4460789Sahrens  *		rw	- mode of created pages.
4461789Sahrens  *		cr	- credentials of caller.
44625331Samw  *		ct	- caller context.
4463789Sahrens  *
4464789Sahrens  *	OUT:	protp	- protection mode of created pages.
4465789Sahrens  *		pl	- list of pages created.
4466789Sahrens  *
4467789Sahrens  *	RETURN:	0 if success
4468789Sahrens  *		error code if failure
4469789Sahrens  *
4470789Sahrens  * Timestamps:
4471789Sahrens  *	vp - atime updated
4472789Sahrens  */
4473789Sahrens /* ARGSUSED */
4474789Sahrens static int
zfs_getpage(vnode_t * vp,offset_t off,size_t len,uint_t * protp,page_t * pl[],size_t plsz,struct seg * seg,caddr_t addr,enum seg_rw rw,cred_t * cr,caller_context_t * ct)4475789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4476789Sahrens 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
44775331Samw 	enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4478789Sahrens {
4479789Sahrens 	znode_t		*zp = VTOZ(vp);
4480789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
44818636SMark.Maybee@Sun.COM 	page_t		**pl0 = pl;
44828636SMark.Maybee@Sun.COM 	int		err = 0;
44838636SMark.Maybee@Sun.COM 
44848636SMark.Maybee@Sun.COM 	/* we do our own caching, faultahead is unnecessary */
44858636SMark.Maybee@Sun.COM 	if (pl == NULL)
44868636SMark.Maybee@Sun.COM 		return (0);
44878636SMark.Maybee@Sun.COM 	else if (len > plsz)
44888636SMark.Maybee@Sun.COM 		len = plsz;
44898681SMark.Maybee@Sun.COM 	else
44908681SMark.Maybee@Sun.COM 		len = P2ROUNDUP(len, PAGESIZE);
44918636SMark.Maybee@Sun.COM 	ASSERT(plsz >= len);
4492789Sahrens 
44935367Sahrens 	ZFS_ENTER(zfsvfs);
44945367Sahrens 	ZFS_VERIFY_ZP(zp);
4495789Sahrens 
4496789Sahrens 	if (protp)
4497789Sahrens 		*protp = PROT_ALL;
4498789Sahrens 
4499789Sahrens 	/*
45009265SMark.Maybee@Sun.COM 	 * Loop through the requested range [off, off + len) looking
4501789Sahrens 	 * for pages.  If we don't find a page, we will need to create
4502789Sahrens 	 * a new page and fill it with data from the file.
4503789Sahrens 	 */
4504789Sahrens 	while (len > 0) {
45058636SMark.Maybee@Sun.COM 		if (*pl = page_lookup(vp, off, SE_SHARED))
45068636SMark.Maybee@Sun.COM 			*(pl+1) = NULL;
45078636SMark.Maybee@Sun.COM 		else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
45088636SMark.Maybee@Sun.COM 			goto out;
45098636SMark.Maybee@Sun.COM 		while (*pl) {
45108636SMark.Maybee@Sun.COM 			ASSERT3U((*pl)->p_offset, ==, off);
4511789Sahrens 			off += PAGESIZE;
4512789Sahrens 			addr += PAGESIZE;
45138681SMark.Maybee@Sun.COM 			if (len > 0) {
45148681SMark.Maybee@Sun.COM 				ASSERT3U(len, >=, PAGESIZE);
45158636SMark.Maybee@Sun.COM 				len -= PAGESIZE;
45168681SMark.Maybee@Sun.COM 			}
45178636SMark.Maybee@Sun.COM 			ASSERT3U(plsz, >=, PAGESIZE);
4518789Sahrens 			plsz -= PAGESIZE;
45198636SMark.Maybee@Sun.COM 			pl++;
4520789Sahrens 		}
4521789Sahrens 	}
4522789Sahrens 
4523789Sahrens 	/*
4524789Sahrens 	 * Fill out the page array with any pages already in the cache.
4525789Sahrens 	 */
45268636SMark.Maybee@Sun.COM 	while (plsz > 0 &&
45278636SMark.Maybee@Sun.COM 	    (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
45288636SMark.Maybee@Sun.COM 			off += PAGESIZE;
45298636SMark.Maybee@Sun.COM 			plsz -= PAGESIZE;
4530789Sahrens 	}
4531789Sahrens out:
45322752Sperrin 	if (err) {
45332752Sperrin 		/*
45342752Sperrin 		 * Release any pages we have previously locked.
45352752Sperrin 		 */
45362752Sperrin 		while (pl > pl0)
45372752Sperrin 			page_unlock(*--pl);
45388636SMark.Maybee@Sun.COM 	} else {
45398636SMark.Maybee@Sun.COM 		ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
45402752Sperrin 	}
45412752Sperrin 
4542789Sahrens 	*pl = NULL;
4543789Sahrens 
4544789Sahrens 	ZFS_EXIT(zfsvfs);
4545789Sahrens 	return (err);
4546789Sahrens }
4547789Sahrens 
45481544Seschrock /*
45491544Seschrock  * Request a memory map for a section of a file.  This code interacts
45501544Seschrock  * with common code and the VM system as follows:
45511544Seschrock  *
45521544Seschrock  *	common code calls mmap(), which ends up in smmap_common()
45531544Seschrock  *
45541544Seschrock  *	this calls VOP_MAP(), which takes you into (say) zfs
45551544Seschrock  *
45561544Seschrock  *	zfs_map() calls as_map(), passing segvn_create() as the callback
45571544Seschrock  *
45581544Seschrock  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
45591544Seschrock  *
45601544Seschrock  *	zfs_addmap() updates z_mapcnt
45611544Seschrock  */
45625331Samw /*ARGSUSED*/
4563789Sahrens static int
zfs_map(vnode_t * vp,offset_t off,struct as * as,caddr_t * addrp,size_t len,uchar_t prot,uchar_t maxprot,uint_t flags,cred_t * cr,caller_context_t * ct)4564789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
45655331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
45665331Samw     caller_context_t *ct)
4567789Sahrens {
4568789Sahrens 	znode_t *zp = VTOZ(vp);
4569789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4570789Sahrens 	segvn_crargs_t	vn_a;
4571789Sahrens 	int		error;
4572789Sahrens 
45735929Smarks 	ZFS_ENTER(zfsvfs);
45745929Smarks 	ZFS_VERIFY_ZP(zp);
45755929Smarks 
457611935SMark.Shellenbaum@Sun.COM 	if ((prot & PROT_WRITE) && (zp->z_pflags &
457711935SMark.Shellenbaum@Sun.COM 	    (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
45785929Smarks 		ZFS_EXIT(zfsvfs);
45795331Samw 		return (EPERM);
45805929Smarks 	}
45815929Smarks 
45825929Smarks 	if ((prot & (PROT_READ | PROT_EXEC)) &&
458311935SMark.Shellenbaum@Sun.COM 	    (zp->z_pflags & ZFS_AV_QUARANTINED)) {
45845929Smarks 		ZFS_EXIT(zfsvfs);
45855929Smarks 		return (EACCES);
45865929Smarks 	}
4587789Sahrens 
4588789Sahrens 	if (vp->v_flag & VNOMAP) {
4589789Sahrens 		ZFS_EXIT(zfsvfs);
4590789Sahrens 		return (ENOSYS);
4591789Sahrens 	}
4592789Sahrens 
4593789Sahrens 	if (off < 0 || len > MAXOFFSET_T - off) {
4594789Sahrens 		ZFS_EXIT(zfsvfs);
4595789Sahrens 		return (ENXIO);
4596789Sahrens 	}
4597789Sahrens 
4598789Sahrens 	if (vp->v_type != VREG) {
4599789Sahrens 		ZFS_EXIT(zfsvfs);
4600789Sahrens 		return (ENODEV);
4601789Sahrens 	}
4602789Sahrens 
4603789Sahrens 	/*
4604789Sahrens 	 * If file is locked, disallow mapping.
4605789Sahrens 	 */
460611935SMark.Shellenbaum@Sun.COM 	if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
46071544Seschrock 		ZFS_EXIT(zfsvfs);
46081544Seschrock 		return (EAGAIN);
4609789Sahrens 	}
4610789Sahrens 
4611789Sahrens 	as_rangelock(as);
46126036Smec 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
46136036Smec 	if (error != 0) {
46146036Smec 		as_rangeunlock(as);
46156036Smec 		ZFS_EXIT(zfsvfs);
46166036Smec 		return (error);
4617789Sahrens 	}
4618789Sahrens 
4619789Sahrens 	vn_a.vp = vp;
4620789Sahrens 	vn_a.offset = (u_offset_t)off;
4621789Sahrens 	vn_a.type = flags & MAP_TYPE;
4622789Sahrens 	vn_a.prot = prot;
4623789Sahrens 	vn_a.maxprot = maxprot;
4624789Sahrens 	vn_a.cred = cr;
4625789Sahrens 	vn_a.amp = NULL;
4626789Sahrens 	vn_a.flags = flags & ~MAP_TYPE;
46271417Skchow 	vn_a.szc = 0;
46281417Skchow 	vn_a.lgrp_mem_policy_flags = 0;
4629789Sahrens 
4630789Sahrens 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
4631789Sahrens 
4632789Sahrens 	as_rangeunlock(as);
4633789Sahrens 	ZFS_EXIT(zfsvfs);
4634789Sahrens 	return (error);
4635789Sahrens }
4636789Sahrens 
4637789Sahrens /* ARGSUSED */
4638789Sahrens static int
zfs_addmap(vnode_t * vp,offset_t off,struct as * as,caddr_t addr,size_t len,uchar_t prot,uchar_t maxprot,uint_t flags,cred_t * cr,caller_context_t * ct)4639789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
46405331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
46415331Samw     caller_context_t *ct)
4642789Sahrens {
46431544Seschrock 	uint64_t pages = btopr(len);
46441544Seschrock 
46451544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4646789Sahrens 	return (0);
4647789Sahrens }
4648789Sahrens 
46491773Seschrock /*
46501773Seschrock  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
46511773Seschrock  * more accurate mtime for the associated file.  Since we don't have a way of
46521773Seschrock  * detecting when the data was actually modified, we have to resort to
46531773Seschrock  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
46541773Seschrock  * last page is pushed.  The problem occurs when the msync() call is omitted,
46551773Seschrock  * which by far the most common case:
46561773Seschrock  *
46571773Seschrock  * 	open()
46581773Seschrock  * 	mmap()
46591773Seschrock  * 	<modify memory>
46601773Seschrock  * 	munmap()
46611773Seschrock  * 	close()
46621773Seschrock  * 	<time lapse>
46631773Seschrock  * 	putpage() via fsflush
46641773Seschrock  *
46651773Seschrock  * If we wait until fsflush to come along, we can have a modification time that
46661773Seschrock  * is some arbitrary point in the future.  In order to prevent this in the
46671773Seschrock  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
46681773Seschrock  * torn down.
46691773Seschrock  */
4670789Sahrens /* ARGSUSED */
4671789Sahrens static int
zfs_delmap(vnode_t * vp,offset_t off,struct as * as,caddr_t addr,size_t len,uint_t prot,uint_t maxprot,uint_t flags,cred_t * cr,caller_context_t * ct)4672789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
46735331Samw     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
46745331Samw     caller_context_t *ct)
4675789Sahrens {
46761544Seschrock 	uint64_t pages = btopr(len);
46771544Seschrock 
46781544Seschrock 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
46791544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
46801773Seschrock 
46811773Seschrock 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
46821773Seschrock 	    vn_has_cached_data(vp))
46835331Samw 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
46841773Seschrock 
4685789Sahrens 	return (0);
4686789Sahrens }
4687789Sahrens 
4688789Sahrens /*
4689789Sahrens  * Free or allocate space in a file.  Currently, this function only
4690789Sahrens  * supports the `F_FREESP' command.  However, this command is somewhat
4691789Sahrens  * misnamed, as its functionality includes the ability to allocate as
4692789Sahrens  * well as free space.
4693789Sahrens  *
4694789Sahrens  *	IN:	vp	- vnode of file to free data in.
4695789Sahrens  *		cmd	- action to take (only F_FREESP supported).
4696789Sahrens  *		bfp	- section of file to free/alloc.
4697789Sahrens  *		flag	- current file open mode flags.
4698789Sahrens  *		offset	- current file offset.
4699789Sahrens  *		cr	- credentials of caller [UNUSED].
47005331Samw  *		ct	- caller context.
4701789Sahrens  *
4702789Sahrens  *	RETURN:	0 if success
4703789Sahrens  *		error code if failure
4704789Sahrens  *
4705789Sahrens  * Timestamps:
4706789Sahrens  *	vp - ctime|mtime updated
4707789Sahrens  */
4708789Sahrens /* ARGSUSED */
4709789Sahrens static int
zfs_space(vnode_t * vp,int cmd,flock64_t * bfp,int flag,offset_t offset,cred_t * cr,caller_context_t * ct)4710789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4711789Sahrens     offset_t offset, cred_t *cr, caller_context_t *ct)
4712789Sahrens {
4713789Sahrens 	znode_t		*zp = VTOZ(vp);
4714789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4715789Sahrens 	uint64_t	off, len;
4716789Sahrens 	int		error;
4717789Sahrens 
47185367Sahrens 	ZFS_ENTER(zfsvfs);
47195367Sahrens 	ZFS_VERIFY_ZP(zp);
4720789Sahrens 
4721789Sahrens 	if (cmd != F_FREESP) {
4722789Sahrens 		ZFS_EXIT(zfsvfs);
4723789Sahrens 		return (EINVAL);
4724789Sahrens 	}
4725789Sahrens 
4726789Sahrens 	if (error = convoff(vp, bfp, 0, offset)) {
4727789Sahrens 		ZFS_EXIT(zfsvfs);
4728789Sahrens 		return (error);
4729789Sahrens 	}
4730789Sahrens 
4731789Sahrens 	if (bfp->l_len < 0) {
4732789Sahrens 		ZFS_EXIT(zfsvfs);
4733789Sahrens 		return (EINVAL);
4734789Sahrens 	}
4735789Sahrens 
4736789Sahrens 	off = bfp->l_start;
47371669Sperrin 	len = bfp->l_len; /* 0 means from off to end of file */
47381878Smaybee 
47396992Smaybee 	error = zfs_freesp(zp, off, len, flag, TRUE);
4740789Sahrens 
4741789Sahrens 	ZFS_EXIT(zfsvfs);
4742789Sahrens 	return (error);
4743789Sahrens }
4744789Sahrens 
47455331Samw /*ARGSUSED*/
4746789Sahrens static int
zfs_fid(vnode_t * vp,fid_t * fidp,caller_context_t * ct)47475331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4748789Sahrens {
4749789Sahrens 	znode_t		*zp = VTOZ(vp);
4750789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
47515326Sek110237 	uint32_t	gen;
475211935SMark.Shellenbaum@Sun.COM 	uint64_t	gen64;
4753789Sahrens 	uint64_t	object = zp->z_id;
4754789Sahrens 	zfid_short_t	*zfid;
475511935SMark.Shellenbaum@Sun.COM 	int		size, i, error;
4756789Sahrens 
47575367Sahrens 	ZFS_ENTER(zfsvfs);
47585367Sahrens 	ZFS_VERIFY_ZP(zp);
475911935SMark.Shellenbaum@Sun.COM 
476011935SMark.Shellenbaum@Sun.COM 	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
476112218SMark.Shellenbaum@Sun.COM 	    &gen64, sizeof (uint64_t))) != 0) {
476212218SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
476311935SMark.Shellenbaum@Sun.COM 		return (error);
476412218SMark.Shellenbaum@Sun.COM 	}
476511935SMark.Shellenbaum@Sun.COM 
476611935SMark.Shellenbaum@Sun.COM 	gen = (uint32_t)gen64;
4767789Sahrens 
4768789Sahrens 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4769789Sahrens 	if (fidp->fid_len < size) {
4770789Sahrens 		fidp->fid_len = size;
47711512Sek110237 		ZFS_EXIT(zfsvfs);
4772789Sahrens 		return (ENOSPC);
4773789Sahrens 	}
4774789Sahrens 
4775789Sahrens 	zfid = (zfid_short_t *)fidp;
4776789Sahrens 
4777789Sahrens 	zfid->zf_len = size;
4778789Sahrens 
4779789Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
4780789Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4781789Sahrens 
4782789Sahrens 	/* Must have a non-zero generation number to distinguish from .zfs */
4783789Sahrens 	if (gen == 0)
4784789Sahrens 		gen = 1;
4785789Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
4786789Sahrens 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4787789Sahrens 
4788789Sahrens 	if (size == LONG_FID_LEN) {
4789789Sahrens 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
4790789Sahrens 		zfid_long_t	*zlfid;
4791789Sahrens 
4792789Sahrens 		zlfid = (zfid_long_t *)fidp;
4793789Sahrens 
4794789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4795789Sahrens 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4796789Sahrens 
4797789Sahrens 		/* XXX - this should be the generation number for the objset */
4798789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4799789Sahrens 			zlfid->zf_setgen[i] = 0;
4800789Sahrens 	}
4801789Sahrens 
4802789Sahrens 	ZFS_EXIT(zfsvfs);
4803789Sahrens 	return (0);
4804789Sahrens }
4805789Sahrens 
4806789Sahrens static int
zfs_pathconf(vnode_t * vp,int cmd,ulong_t * valp,cred_t * cr,caller_context_t * ct)48075331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
48085331Samw     caller_context_t *ct)
4809789Sahrens {
4810789Sahrens 	znode_t		*zp, *xzp;
4811789Sahrens 	zfsvfs_t	*zfsvfs;
4812789Sahrens 	zfs_dirlock_t	*dl;
4813789Sahrens 	int		error;
4814789Sahrens 
4815789Sahrens 	switch (cmd) {
4816789Sahrens 	case _PC_LINK_MAX:
4817789Sahrens 		*valp = ULONG_MAX;
4818789Sahrens 		return (0);
4819789Sahrens 
4820789Sahrens 	case _PC_FILESIZEBITS:
4821789Sahrens 		*valp = 64;
4822789Sahrens 		return (0);
4823789Sahrens 
4824789Sahrens 	case _PC_XATTR_EXISTS:
4825789Sahrens 		zp = VTOZ(vp);
4826789Sahrens 		zfsvfs = zp->z_zfsvfs;
48275367Sahrens 		ZFS_ENTER(zfsvfs);
48285367Sahrens 		ZFS_VERIFY_ZP(zp);
4829789Sahrens 		*valp = 0;
4830789Sahrens 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
48315331Samw 		    ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4832789Sahrens 		if (error == 0) {
4833789Sahrens 			zfs_dirent_unlock(dl);
4834789Sahrens 			if (!zfs_dirempty(xzp))
4835789Sahrens 				*valp = 1;
4836789Sahrens 			VN_RELE(ZTOV(xzp));
4837789Sahrens 		} else if (error == ENOENT) {
4838789Sahrens 			/*
4839789Sahrens 			 * If there aren't extended attributes, it's the
4840789Sahrens 			 * same as having zero of them.
4841789Sahrens 			 */
4842789Sahrens 			error = 0;
4843789Sahrens 		}
4844789Sahrens 		ZFS_EXIT(zfsvfs);
4845789Sahrens 		return (error);
4846789Sahrens 
48475331Samw 	case _PC_SATTR_ENABLED:
48485331Samw 	case _PC_SATTR_EXISTS:
48497757SJanice.Chang@Sun.COM 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
48505331Samw 		    (vp->v_type == VREG || vp->v_type == VDIR);
48515331Samw 		return (0);
48525331Samw 
48539749STim.Haley@Sun.COM 	case _PC_ACCESS_FILTERING:
48549749STim.Haley@Sun.COM 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
48559749STim.Haley@Sun.COM 		    vp->v_type == VDIR;
48569749STim.Haley@Sun.COM 		return (0);
48579749STim.Haley@Sun.COM 
4858789Sahrens 	case _PC_ACL_ENABLED:
4859789Sahrens 		*valp = _ACL_ACE_ENABLED;
4860789Sahrens 		return (0);
4861789Sahrens 
4862789Sahrens 	case _PC_MIN_HOLE_SIZE:
4863789Sahrens 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
4864789Sahrens 		return (0);
4865789Sahrens 
486610440SRoger.Faulkner@Sun.COM 	case _PC_TIMESTAMP_RESOLUTION:
486710440SRoger.Faulkner@Sun.COM 		/* nanosecond timestamp resolution */
486810440SRoger.Faulkner@Sun.COM 		*valp = 1L;
486910440SRoger.Faulkner@Sun.COM 		return (0);
487010440SRoger.Faulkner@Sun.COM 
4871789Sahrens 	default:
48725331Samw 		return (fs_pathconf(vp, cmd, valp, cr, ct));
4873789Sahrens 	}
4874789Sahrens }
4875789Sahrens 
4876789Sahrens /*ARGSUSED*/
4877789Sahrens static int
zfs_getsecattr(vnode_t * vp,vsecattr_t * vsecp,int flag,cred_t * cr,caller_context_t * ct)48785331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
48795331Samw     caller_context_t *ct)
4880789Sahrens {
4881789Sahrens 	znode_t *zp = VTOZ(vp);
4882789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4883789Sahrens 	int error;
48845331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4885789Sahrens 
48865367Sahrens 	ZFS_ENTER(zfsvfs);
48875367Sahrens 	ZFS_VERIFY_ZP(zp);
48885331Samw 	error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4889789Sahrens 	ZFS_EXIT(zfsvfs);
4890789Sahrens 
4891789Sahrens 	return (error);
4892789Sahrens }
4893789Sahrens 
4894789Sahrens /*ARGSUSED*/
4895789Sahrens static int
zfs_setsecattr(vnode_t * vp,vsecattr_t * vsecp,int flag,cred_t * cr,caller_context_t * ct)48965331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
48975331Samw     caller_context_t *ct)
4898789Sahrens {
4899789Sahrens 	znode_t *zp = VTOZ(vp);
4900789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4901789Sahrens 	int error;
49025331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
490312294SMark.Musante@Sun.COM 	zilog_t	*zilog = zfsvfs->z_log;
4904789Sahrens 
49055367Sahrens 	ZFS_ENTER(zfsvfs);
49065367Sahrens 	ZFS_VERIFY_ZP(zp);
490712294SMark.Musante@Sun.COM 
49085331Samw 	error = zfs_setacl(zp, vsecp, skipaclchk, cr);
490912294SMark.Musante@Sun.COM 
491012294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
491112699SNeil.Perrin@Sun.COM 		zil_commit(zilog, 0);
491212294SMark.Musante@Sun.COM 
4913789Sahrens 	ZFS_EXIT(zfsvfs);
4914789Sahrens 	return (error);
4915789Sahrens }
4916789Sahrens 
4917789Sahrens /*
491811539SChunli.Zhang@Sun.COM  * Tunable, both must be a power of 2.
491911539SChunli.Zhang@Sun.COM  *
492011539SChunli.Zhang@Sun.COM  * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
492111539SChunli.Zhang@Sun.COM  * zcr_blksz_max: if set to less than the file block size, allow loaning out of
492211539SChunli.Zhang@Sun.COM  *                an arcbuf for a partial block read
492311539SChunli.Zhang@Sun.COM  */
492411539SChunli.Zhang@Sun.COM int zcr_blksz_min = (1 << 10);	/* 1K */
492511539SChunli.Zhang@Sun.COM int zcr_blksz_max = (1 << 17);	/* 128K */
492611539SChunli.Zhang@Sun.COM 
492711539SChunli.Zhang@Sun.COM /*ARGSUSED*/
492811539SChunli.Zhang@Sun.COM static int
zfs_reqzcbuf(vnode_t * vp,enum uio_rw ioflag,xuio_t * xuio,cred_t * cr,caller_context_t * ct)492911539SChunli.Zhang@Sun.COM zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
493011539SChunli.Zhang@Sun.COM     caller_context_t *ct)
493111539SChunli.Zhang@Sun.COM {
493211539SChunli.Zhang@Sun.COM 	znode_t	*zp = VTOZ(vp);
493311539SChunli.Zhang@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
493411539SChunli.Zhang@Sun.COM 	int max_blksz = zfsvfs->z_max_blksz;
493511539SChunli.Zhang@Sun.COM 	uio_t *uio = &xuio->xu_uio;
493611539SChunli.Zhang@Sun.COM 	ssize_t size = uio->uio_resid;
493711539SChunli.Zhang@Sun.COM 	offset_t offset = uio->uio_loffset;
493811539SChunli.Zhang@Sun.COM 	int blksz;
493911539SChunli.Zhang@Sun.COM 	int fullblk, i;
494011539SChunli.Zhang@Sun.COM 	arc_buf_t *abuf;
494111539SChunli.Zhang@Sun.COM 	ssize_t maxsize;
494211539SChunli.Zhang@Sun.COM 	int preamble, postamble;
494311539SChunli.Zhang@Sun.COM 
494411539SChunli.Zhang@Sun.COM 	if (xuio->xu_type != UIOTYPE_ZEROCOPY)
494511539SChunli.Zhang@Sun.COM 		return (EINVAL);
494611539SChunli.Zhang@Sun.COM 
494711539SChunli.Zhang@Sun.COM 	ZFS_ENTER(zfsvfs);
494811539SChunli.Zhang@Sun.COM 	ZFS_VERIFY_ZP(zp);
494911539SChunli.Zhang@Sun.COM 	switch (ioflag) {
495011539SChunli.Zhang@Sun.COM 	case UIO_WRITE:
495111539SChunli.Zhang@Sun.COM 		/*
495211539SChunli.Zhang@Sun.COM 		 * Loan out an arc_buf for write if write size is bigger than
495311539SChunli.Zhang@Sun.COM 		 * max_blksz, and the file's block size is also max_blksz.
495411539SChunli.Zhang@Sun.COM 		 */
495511539SChunli.Zhang@Sun.COM 		blksz = max_blksz;
495611539SChunli.Zhang@Sun.COM 		if (size < blksz || zp->z_blksz != blksz) {
495711539SChunli.Zhang@Sun.COM 			ZFS_EXIT(zfsvfs);
495811539SChunli.Zhang@Sun.COM 			return (EINVAL);
495911539SChunli.Zhang@Sun.COM 		}
496011539SChunli.Zhang@Sun.COM 		/*
496111539SChunli.Zhang@Sun.COM 		 * Caller requests buffers for write before knowing where the
496211539SChunli.Zhang@Sun.COM 		 * write offset might be (e.g. NFS TCP write).
496311539SChunli.Zhang@Sun.COM 		 */
496411539SChunli.Zhang@Sun.COM 		if (offset == -1) {
496511539SChunli.Zhang@Sun.COM 			preamble = 0;
496611539SChunli.Zhang@Sun.COM 		} else {
496711539SChunli.Zhang@Sun.COM 			preamble = P2PHASE(offset, blksz);
496811539SChunli.Zhang@Sun.COM 			if (preamble) {
496911539SChunli.Zhang@Sun.COM 				preamble = blksz - preamble;
497011539SChunli.Zhang@Sun.COM 				size -= preamble;
497111539SChunli.Zhang@Sun.COM 			}
497211539SChunli.Zhang@Sun.COM 		}
497311539SChunli.Zhang@Sun.COM 
497411539SChunli.Zhang@Sun.COM 		postamble = P2PHASE(size, blksz);
497511539SChunli.Zhang@Sun.COM 		size -= postamble;
497611539SChunli.Zhang@Sun.COM 
497711539SChunli.Zhang@Sun.COM 		fullblk = size / blksz;
497811576SSurya.Prakki@Sun.COM 		(void) dmu_xuio_init(xuio,
497911539SChunli.Zhang@Sun.COM 		    (preamble != 0) + fullblk + (postamble != 0));
498011539SChunli.Zhang@Sun.COM 		DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
498111539SChunli.Zhang@Sun.COM 		    int, postamble, int,
498211539SChunli.Zhang@Sun.COM 		    (preamble != 0) + fullblk + (postamble != 0));
498311539SChunli.Zhang@Sun.COM 
498411539SChunli.Zhang@Sun.COM 		/*
498511539SChunli.Zhang@Sun.COM 		 * Have to fix iov base/len for partial buffers.  They
498611539SChunli.Zhang@Sun.COM 		 * currently represent full arc_buf's.
498711539SChunli.Zhang@Sun.COM 		 */
498811539SChunli.Zhang@Sun.COM 		if (preamble) {
498911539SChunli.Zhang@Sun.COM 			/* data begins in the middle of the arc_buf */
499011935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
499111935SMark.Shellenbaum@Sun.COM 			    blksz);
499211539SChunli.Zhang@Sun.COM 			ASSERT(abuf);
499311576SSurya.Prakki@Sun.COM 			(void) dmu_xuio_add(xuio, abuf,
499411576SSurya.Prakki@Sun.COM 			    blksz - preamble, preamble);
499511539SChunli.Zhang@Sun.COM 		}
499611539SChunli.Zhang@Sun.COM 
499711539SChunli.Zhang@Sun.COM 		for (i = 0; i < fullblk; i++) {
499811935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
499911935SMark.Shellenbaum@Sun.COM 			    blksz);
500011539SChunli.Zhang@Sun.COM 			ASSERT(abuf);
500111576SSurya.Prakki@Sun.COM 			(void) dmu_xuio_add(xuio, abuf, 0, blksz);
500211539SChunli.Zhang@Sun.COM 		}
500311539SChunli.Zhang@Sun.COM 
500411539SChunli.Zhang@Sun.COM 		if (postamble) {
500511539SChunli.Zhang@Sun.COM 			/* data ends in the middle of the arc_buf */
500611935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
500711935SMark.Shellenbaum@Sun.COM 			    blksz);
500811539SChunli.Zhang@Sun.COM 			ASSERT(abuf);
500911576SSurya.Prakki@Sun.COM 			(void) dmu_xuio_add(xuio, abuf, 0, postamble);
501011539SChunli.Zhang@Sun.COM 		}
501111539SChunli.Zhang@Sun.COM 		break;
501211539SChunli.Zhang@Sun.COM 	case UIO_READ:
501311539SChunli.Zhang@Sun.COM 		/*
501411539SChunli.Zhang@Sun.COM 		 * Loan out an arc_buf for read if the read size is larger than
501511539SChunli.Zhang@Sun.COM 		 * the current file block size.  Block alignment is not
501611539SChunli.Zhang@Sun.COM 		 * considered.  Partial arc_buf will be loaned out for read.
501711539SChunli.Zhang@Sun.COM 		 */
501811539SChunli.Zhang@Sun.COM 		blksz = zp->z_blksz;
501911539SChunli.Zhang@Sun.COM 		if (blksz < zcr_blksz_min)
502011539SChunli.Zhang@Sun.COM 			blksz = zcr_blksz_min;
502111539SChunli.Zhang@Sun.COM 		if (blksz > zcr_blksz_max)
502211539SChunli.Zhang@Sun.COM 			blksz = zcr_blksz_max;
502311539SChunli.Zhang@Sun.COM 		/* avoid potential complexity of dealing with it */
502411539SChunli.Zhang@Sun.COM 		if (blksz > max_blksz) {
502511539SChunli.Zhang@Sun.COM 			ZFS_EXIT(zfsvfs);
502611539SChunli.Zhang@Sun.COM 			return (EINVAL);
502711539SChunli.Zhang@Sun.COM 		}
502811539SChunli.Zhang@Sun.COM 
502911935SMark.Shellenbaum@Sun.COM 		maxsize = zp->z_size - uio->uio_loffset;
503011539SChunli.Zhang@Sun.COM 		if (size > maxsize)
503111539SChunli.Zhang@Sun.COM 			size = maxsize;
503211539SChunli.Zhang@Sun.COM 
503311539SChunli.Zhang@Sun.COM 		if (size < blksz || vn_has_cached_data(vp)) {
503411539SChunli.Zhang@Sun.COM 			ZFS_EXIT(zfsvfs);
503511539SChunli.Zhang@Sun.COM 			return (EINVAL);
503611539SChunli.Zhang@Sun.COM 		}
503711539SChunli.Zhang@Sun.COM 		break;
503811539SChunli.Zhang@Sun.COM 	default:
503911539SChunli.Zhang@Sun.COM 		ZFS_EXIT(zfsvfs);
504011539SChunli.Zhang@Sun.COM 		return (EINVAL);
504111539SChunli.Zhang@Sun.COM 	}
504211539SChunli.Zhang@Sun.COM 
504311539SChunli.Zhang@Sun.COM 	uio->uio_extflg = UIO_XUIO;
504411539SChunli.Zhang@Sun.COM 	XUIO_XUZC_RW(xuio) = ioflag;
504511539SChunli.Zhang@Sun.COM 	ZFS_EXIT(zfsvfs);
504611539SChunli.Zhang@Sun.COM 	return (0);
504711539SChunli.Zhang@Sun.COM }
504811539SChunli.Zhang@Sun.COM 
504911539SChunli.Zhang@Sun.COM /*ARGSUSED*/
505011539SChunli.Zhang@Sun.COM static int
zfs_retzcbuf(vnode_t * vp,xuio_t * xuio,cred_t * cr,caller_context_t * ct)505111539SChunli.Zhang@Sun.COM zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
505211539SChunli.Zhang@Sun.COM {
505311539SChunli.Zhang@Sun.COM 	int i;
505411539SChunli.Zhang@Sun.COM 	arc_buf_t *abuf;
505511539SChunli.Zhang@Sun.COM 	int ioflag = XUIO_XUZC_RW(xuio);
505611539SChunli.Zhang@Sun.COM 
505711539SChunli.Zhang@Sun.COM 	ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
505811539SChunli.Zhang@Sun.COM 
505911539SChunli.Zhang@Sun.COM 	i = dmu_xuio_cnt(xuio);
506011539SChunli.Zhang@Sun.COM 	while (i-- > 0) {
506111539SChunli.Zhang@Sun.COM 		abuf = dmu_xuio_arcbuf(xuio, i);
506211539SChunli.Zhang@Sun.COM 		/*
506311539SChunli.Zhang@Sun.COM 		 * if abuf == NULL, it must be a write buffer
506411539SChunli.Zhang@Sun.COM 		 * that has been returned in zfs_write().
506511539SChunli.Zhang@Sun.COM 		 */
506611539SChunli.Zhang@Sun.COM 		if (abuf)
506711539SChunli.Zhang@Sun.COM 			dmu_return_arcbuf(abuf);
506811539SChunli.Zhang@Sun.COM 		ASSERT(abuf || ioflag == UIO_WRITE);
506911539SChunli.Zhang@Sun.COM 	}
507011539SChunli.Zhang@Sun.COM 
507111539SChunli.Zhang@Sun.COM 	dmu_xuio_fini(xuio);
507211539SChunli.Zhang@Sun.COM 	return (0);
507311539SChunli.Zhang@Sun.COM }
507411539SChunli.Zhang@Sun.COM 
507511539SChunli.Zhang@Sun.COM /*
5076789Sahrens  * Predeclare these here so that the compiler assumes that
5077789Sahrens  * this is an "old style" function declaration that does
5078789Sahrens  * not include arguments => we won't get type mismatch errors
5079789Sahrens  * in the initializations that follow.
5080789Sahrens  */
5081789Sahrens static int zfs_inval();
5082789Sahrens static int zfs_isdir();
5083789Sahrens 
5084789Sahrens static int
zfs_inval()5085789Sahrens zfs_inval()
5086789Sahrens {
5087789Sahrens 	return (EINVAL);
5088789Sahrens }
5089789Sahrens 
5090789Sahrens static int
zfs_isdir()5091789Sahrens zfs_isdir()
5092789Sahrens {
5093789Sahrens 	return (EISDIR);
5094789Sahrens }
5095789Sahrens /*
5096789Sahrens  * Directory vnode operations template
5097789Sahrens  */
5098789Sahrens vnodeops_t *zfs_dvnodeops;
5099789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = {
51003898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
51013898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
51023898Srsb 	VOPNAME_READ,		{ .error = zfs_isdir },
51033898Srsb 	VOPNAME_WRITE,		{ .error = zfs_isdir },
51043898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
51053898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
51063898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
51073898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
51083898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
51093898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
51103898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
51113898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
51123898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
51133898Srsb 	VOPNAME_MKDIR,		{ .vop_mkdir = zfs_mkdir },
51143898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
51153898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
51163898Srsb 	VOPNAME_SYMLINK,	{ .vop_symlink = zfs_symlink },
51173898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
51183898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
51193898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
51203898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
51213898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
51223898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
51233898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
51244863Spraks 	VOPNAME_VNEVENT, 	{ .vop_vnevent = fs_vnevent_support },
51253898Srsb 	NULL,			NULL
5126789Sahrens };
5127789Sahrens 
5128789Sahrens /*
5129789Sahrens  * Regular file vnode operations template
5130789Sahrens  */
5131789Sahrens vnodeops_t *zfs_fvnodeops;
5132789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = {
51333898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
51343898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
51353898Srsb 	VOPNAME_READ,		{ .vop_read = zfs_read },
51363898Srsb 	VOPNAME_WRITE,		{ .vop_write = zfs_write },
51373898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
51383898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
51393898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
51403898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
51413898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
51423898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
51433898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
51443898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
51453898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
51463898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
51473898Srsb 	VOPNAME_FRLOCK,		{ .vop_frlock = zfs_frlock },
51483898Srsb 	VOPNAME_SPACE,		{ .vop_space = zfs_space },
51493898Srsb 	VOPNAME_GETPAGE,	{ .vop_getpage = zfs_getpage },
51503898Srsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = zfs_putpage },
51513898Srsb 	VOPNAME_MAP,		{ .vop_map = zfs_map },
51523898Srsb 	VOPNAME_ADDMAP,		{ .vop_addmap = zfs_addmap },
51533898Srsb 	VOPNAME_DELMAP,		{ .vop_delmap = zfs_delmap },
51543898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
51553898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
51563898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
51573898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
515811539SChunli.Zhang@Sun.COM 	VOPNAME_REQZCBUF, 	{ .vop_reqzcbuf = zfs_reqzcbuf },
515911539SChunli.Zhang@Sun.COM 	VOPNAME_RETZCBUF, 	{ .vop_retzcbuf = zfs_retzcbuf },
51603898Srsb 	NULL,			NULL
5161789Sahrens };
5162789Sahrens 
5163789Sahrens /*
5164789Sahrens  * Symbolic link vnode operations template
5165789Sahrens  */
5166789Sahrens vnodeops_t *zfs_symvnodeops;
5167789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = {
51683898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
51693898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
51703898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
51713898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
51723898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = zfs_readlink },
51733898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
51743898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
51753898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
51763898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
51773898Srsb 	NULL,			NULL
5178789Sahrens };
5179789Sahrens 
5180789Sahrens /*
51818845Samw@Sun.COM  * special share hidden files vnode operations template
51828845Samw@Sun.COM  */
51838845Samw@Sun.COM vnodeops_t *zfs_sharevnodeops;
51848845Samw@Sun.COM const fs_operation_def_t zfs_sharevnodeops_template[] = {
51858845Samw@Sun.COM 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
51868845Samw@Sun.COM 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
51878845Samw@Sun.COM 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
51888845Samw@Sun.COM 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
51898845Samw@Sun.COM 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
51908845Samw@Sun.COM 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
51918845Samw@Sun.COM 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
51928845Samw@Sun.COM 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
51938845Samw@Sun.COM 	NULL,			NULL
51948845Samw@Sun.COM };
51958845Samw@Sun.COM 
51968845Samw@Sun.COM /*
5197789Sahrens  * Extended attribute directory vnode operations template
5198789Sahrens  *	This template is identical to the directory vnodes
5199789Sahrens  *	operation template except for restricted operations:
5200789Sahrens  *		VOP_MKDIR()
5201789Sahrens  *		VOP_SYMLINK()
5202789Sahrens  * Note that there are other restrictions embedded in:
5203789Sahrens  *	zfs_create()	- restrict type to VREG
5204789Sahrens  *	zfs_link()	- no links into/out of attribute space
5205789Sahrens  *	zfs_rename()	- no moves into/out of attribute space
5206789Sahrens  */
5207789Sahrens vnodeops_t *zfs_xdvnodeops;
5208789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = {
52093898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
52103898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
52113898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
52123898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
52133898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
52143898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
52153898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
52163898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
52173898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
52183898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
52193898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
52203898Srsb 	VOPNAME_MKDIR,		{ .error = zfs_inval },
52213898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
52223898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
52233898Srsb 	VOPNAME_SYMLINK,	{ .error = zfs_inval },
52243898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
52253898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
52263898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
52273898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
52283898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
52293898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
52303898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
52313898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
52323898Srsb 	NULL,			NULL
5233789Sahrens };
5234789Sahrens 
5235789Sahrens /*
5236789Sahrens  * Error vnode operations template
5237789Sahrens  */
5238789Sahrens vnodeops_t *zfs_evnodeops;
5239789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = {
52403898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
52413898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
52423898Srsb 	NULL,			NULL
5243789Sahrens };
5244