xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 12302:44e9cbdf497d)
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  *
1352638Sperrin  *  (7)	After dropping all locks, invoke zil_commit(zilog, seq, foid)
136789Sahrens  *	to ensure that synchronous semantics are provided when necessary.
137789Sahrens  *
138789Sahrens  * In general, this is how things should be ordered in each vnode op:
139789Sahrens  *
140789Sahrens  *	ZFS_ENTER(zfsvfs);		// exit if unmounted
141789Sahrens  * top:
142789Sahrens  *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
143789Sahrens  *	rw_enter(...);			// grab any other locks you need
144789Sahrens  *	tx = dmu_tx_create(...);	// get DMU tx
145789Sahrens  *	dmu_tx_hold_*();		// hold each object you might modify
1468227SNeil.Perrin@Sun.COM  *	error = dmu_tx_assign(tx, TXG_NOWAIT);	// try to assign
147789Sahrens  *	if (error) {
148789Sahrens  *		rw_exit(...);		// drop locks
149789Sahrens  *		zfs_dirent_unlock(dl);	// unlock directory entry
150789Sahrens  *		VN_RELE(...);		// release held vnodes
1518227SNeil.Perrin@Sun.COM  *		if (error == ERESTART) {
1522113Sahrens  *			dmu_tx_wait(tx);
1532113Sahrens  *			dmu_tx_abort(tx);
154789Sahrens  *			goto top;
155789Sahrens  *		}
1562113Sahrens  *		dmu_tx_abort(tx);	// abort DMU tx
157789Sahrens  *		ZFS_EXIT(zfsvfs);	// finished in zfs
158789Sahrens  *		return (error);		// really out of space
159789Sahrens  *	}
160789Sahrens  *	error = do_real_work();		// do whatever this VOP does
161789Sahrens  *	if (error == 0)
1622638Sperrin  *		zfs_log_*(...);		// on success, make ZIL entry
163789Sahrens  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
164789Sahrens  *	rw_exit(...);			// drop locks
165789Sahrens  *	zfs_dirent_unlock(dl);		// unlock directory entry
166789Sahrens  *	VN_RELE(...);			// release held vnodes
1672638Sperrin  *	zil_commit(zilog, seq, foid);	// synchronous when necessary
168789Sahrens  *	ZFS_EXIT(zfsvfs);		// finished in zfs
169789Sahrens  *	return (error);			// done, report error
170789Sahrens  */
1715367Sahrens 
172789Sahrens /* ARGSUSED */
173789Sahrens static int
1745331Samw zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
175789Sahrens {
1763063Sperrin 	znode_t	*zp = VTOZ(*vpp);
1777844SMark.Shellenbaum@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1787844SMark.Shellenbaum@Sun.COM 
1797844SMark.Shellenbaum@Sun.COM 	ZFS_ENTER(zfsvfs);
1807844SMark.Shellenbaum@Sun.COM 	ZFS_VERIFY_ZP(zp);
1813063Sperrin 
18211935SMark.Shellenbaum@Sun.COM 	if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
1835331Samw 	    ((flag & FAPPEND) == 0)) {
1847844SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
1855331Samw 		return (EPERM);
1865331Samw 	}
1875331Samw 
1885331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
1895331Samw 	    ZTOV(zp)->v_type == VREG &&
19011935SMark.Shellenbaum@Sun.COM 	    !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
1917844SMark.Shellenbaum@Sun.COM 		if (fs_vscan(*vpp, cr, 0) != 0) {
1927844SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
1935331Samw 			return (EACCES);
1947844SMark.Shellenbaum@Sun.COM 		}
1957844SMark.Shellenbaum@Sun.COM 	}
1965331Samw 
1973063Sperrin 	/* Keep a count of the synchronous opens in the znode */
1983063Sperrin 	if (flag & (FSYNC | FDSYNC))
1993063Sperrin 		atomic_inc_32(&zp->z_sync_cnt);
2005331Samw 
2017844SMark.Shellenbaum@Sun.COM 	ZFS_EXIT(zfsvfs);
202789Sahrens 	return (0);
203789Sahrens }
204789Sahrens 
205789Sahrens /* ARGSUSED */
206789Sahrens static int
2075331Samw zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
2085331Samw     caller_context_t *ct)
209789Sahrens {
2103063Sperrin 	znode_t	*zp = VTOZ(vp);
2117844SMark.Shellenbaum@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2127844SMark.Shellenbaum@Sun.COM 
2139909Schris.kirby@sun.com 	/*
2149909Schris.kirby@sun.com 	 * Clean up any locks held by this process on the vp.
2159909Schris.kirby@sun.com 	 */
2169909Schris.kirby@sun.com 	cleanlocks(vp, ddi_get_pid(), 0);
2179909Schris.kirby@sun.com 	cleanshares(vp, ddi_get_pid());
2189909Schris.kirby@sun.com 
2197844SMark.Shellenbaum@Sun.COM 	ZFS_ENTER(zfsvfs);
2207844SMark.Shellenbaum@Sun.COM 	ZFS_VERIFY_ZP(zp);
2213063Sperrin 
2223063Sperrin 	/* Decrement the synchronous opens in the znode */
2234339Sperrin 	if ((flag & (FSYNC | FDSYNC)) && (count == 1))
2243063Sperrin 		atomic_dec_32(&zp->z_sync_cnt);
2253063Sperrin 
2265331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
2275331Samw 	    ZTOV(zp)->v_type == VREG &&
22811935SMark.Shellenbaum@Sun.COM 	    !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
2295331Samw 		VERIFY(fs_vscan(vp, cr, 1) == 0);
2305331Samw 
2317844SMark.Shellenbaum@Sun.COM 	ZFS_EXIT(zfsvfs);
232789Sahrens 	return (0);
233789Sahrens }
234789Sahrens 
235789Sahrens /*
236789Sahrens  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
237789Sahrens  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
238789Sahrens  */
239789Sahrens static int
240789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off)
241789Sahrens {
242789Sahrens 	znode_t	*zp = VTOZ(vp);
243789Sahrens 	uint64_t noff = (uint64_t)*off; /* new offset */
244789Sahrens 	uint64_t file_sz;
245789Sahrens 	int error;
246789Sahrens 	boolean_t hole;
247789Sahrens 
24811935SMark.Shellenbaum@Sun.COM 	file_sz = zp->z_size;
249789Sahrens 	if (noff >= file_sz)  {
250789Sahrens 		return (ENXIO);
251789Sahrens 	}
252789Sahrens 
253789Sahrens 	if (cmd == _FIO_SEEK_HOLE)
254789Sahrens 		hole = B_TRUE;
255789Sahrens 	else
256789Sahrens 		hole = B_FALSE;
257789Sahrens 
258789Sahrens 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
259789Sahrens 
260789Sahrens 	/* end of file? */
261789Sahrens 	if ((error == ESRCH) || (noff > file_sz)) {
262789Sahrens 		/*
263789Sahrens 		 * Handle the virtual hole at the end of file.
264789Sahrens 		 */
265789Sahrens 		if (hole) {
266789Sahrens 			*off = file_sz;
267789Sahrens 			return (0);
268789Sahrens 		}
269789Sahrens 		return (ENXIO);
270789Sahrens 	}
271789Sahrens 
272789Sahrens 	if (noff < *off)
273789Sahrens 		return (error);
274789Sahrens 	*off = noff;
275789Sahrens 	return (error);
276789Sahrens }
277789Sahrens 
278789Sahrens /* ARGSUSED */
279789Sahrens static int
280789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
2815331Samw     int *rvalp, caller_context_t *ct)
282789Sahrens {
283789Sahrens 	offset_t off;
284789Sahrens 	int error;
285789Sahrens 	zfsvfs_t *zfsvfs;
2865326Sek110237 	znode_t *zp;
287789Sahrens 
288789Sahrens 	switch (com) {
2894339Sperrin 	case _FIOFFS:
290789Sahrens 		return (zfs_sync(vp->v_vfsp, 0, cred));
291789Sahrens 
2921544Seschrock 		/*
2931544Seschrock 		 * The following two ioctls are used by bfu.  Faking out,
2941544Seschrock 		 * necessary to avoid bfu errors.
2951544Seschrock 		 */
2964339Sperrin 	case _FIOGDIO:
2974339Sperrin 	case _FIOSDIO:
2981544Seschrock 		return (0);
2991544Seschrock 
3004339Sperrin 	case _FIO_SEEK_DATA:
3014339Sperrin 	case _FIO_SEEK_HOLE:
302789Sahrens 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
303789Sahrens 			return (EFAULT);
304789Sahrens 
3055326Sek110237 		zp = VTOZ(vp);
3065326Sek110237 		zfsvfs = zp->z_zfsvfs;
3075367Sahrens 		ZFS_ENTER(zfsvfs);
3085367Sahrens 		ZFS_VERIFY_ZP(zp);
309789Sahrens 
310789Sahrens 		/* offset parameter is in/out */
311789Sahrens 		error = zfs_holey(vp, com, &off);
312789Sahrens 		ZFS_EXIT(zfsvfs);
313789Sahrens 		if (error)
314789Sahrens 			return (error);
315789Sahrens 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
316789Sahrens 			return (EFAULT);
317789Sahrens 		return (0);
318789Sahrens 	}
319789Sahrens 	return (ENOTTY);
320789Sahrens }
321789Sahrens 
322789Sahrens /*
3237315SJonathan.Adams@Sun.COM  * Utility functions to map and unmap a single physical page.  These
3247315SJonathan.Adams@Sun.COM  * are used to manage the mappable copies of ZFS file data, and therefore
3257315SJonathan.Adams@Sun.COM  * do not update ref/mod bits.
3267315SJonathan.Adams@Sun.COM  */
3277315SJonathan.Adams@Sun.COM caddr_t
3287315SJonathan.Adams@Sun.COM zfs_map_page(page_t *pp, enum seg_rw rw)
3297315SJonathan.Adams@Sun.COM {
3307315SJonathan.Adams@Sun.COM 	if (kpm_enable)
3317315SJonathan.Adams@Sun.COM 		return (hat_kpm_mapin(pp, 0));
3327315SJonathan.Adams@Sun.COM 	ASSERT(rw == S_READ || rw == S_WRITE);
3337315SJonathan.Adams@Sun.COM 	return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0),
3347315SJonathan.Adams@Sun.COM 	    (caddr_t)-1));
3357315SJonathan.Adams@Sun.COM }
3367315SJonathan.Adams@Sun.COM 
3377315SJonathan.Adams@Sun.COM void
3387315SJonathan.Adams@Sun.COM zfs_unmap_page(page_t *pp, caddr_t addr)
3397315SJonathan.Adams@Sun.COM {
3407315SJonathan.Adams@Sun.COM 	if (kpm_enable) {
3417315SJonathan.Adams@Sun.COM 		hat_kpm_mapout(pp, 0, addr);
3427315SJonathan.Adams@Sun.COM 	} else {
3437315SJonathan.Adams@Sun.COM 		ppmapout(addr);
3447315SJonathan.Adams@Sun.COM 	}
3457315SJonathan.Adams@Sun.COM }
3467315SJonathan.Adams@Sun.COM 
3477315SJonathan.Adams@Sun.COM /*
348789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
349789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
350789Sahrens  *
351789Sahrens  * On Write:	If we find a memory mapped page, we write to *both*
352789Sahrens  *		the page and the dmu buffer.
353789Sahrens  */
3548636SMark.Maybee@Sun.COM static void
3558636SMark.Maybee@Sun.COM update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid)
356789Sahrens {
3578636SMark.Maybee@Sun.COM 	int64_t	off;
3588636SMark.Maybee@Sun.COM 
359789Sahrens 	off = start & PAGEOFFSET;
360789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
361789Sahrens 		page_t *pp;
3628636SMark.Maybee@Sun.COM 		uint64_t nbytes = MIN(PAGESIZE - off, len);
3638636SMark.Maybee@Sun.COM 
364789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
365789Sahrens 			caddr_t va;
366789Sahrens 
3677315SJonathan.Adams@Sun.COM 			va = zfs_map_page(pp, S_WRITE);
3689512SNeil.Perrin@Sun.COM 			(void) dmu_read(os, oid, start+off, nbytes, va+off,
3699512SNeil.Perrin@Sun.COM 			    DMU_READ_PREFETCH);
3707315SJonathan.Adams@Sun.COM 			zfs_unmap_page(pp, va);
371789Sahrens 			page_unlock(pp);
372789Sahrens 		}
3738636SMark.Maybee@Sun.COM 		len -= nbytes;
374789Sahrens 		off = 0;
375789Sahrens 	}
376789Sahrens }
377789Sahrens 
378789Sahrens /*
379789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
380789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
381789Sahrens  *
382789Sahrens  * On Read:	We "read" preferentially from memory mapped pages,
383789Sahrens  *		else we default from the dmu buffer.
384789Sahrens  *
385789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
386789Sahrens  *	the file is memory mapped.
387789Sahrens  */
388789Sahrens static int
3893638Sbillm mappedread(vnode_t *vp, int nbytes, uio_t *uio)
390789Sahrens {
3913638Sbillm 	znode_t *zp = VTOZ(vp);
3923638Sbillm 	objset_t *os = zp->z_zfsvfs->z_os;
3933638Sbillm 	int64_t	start, off;
394789Sahrens 	int len = nbytes;
395789Sahrens 	int error = 0;
396789Sahrens 
397789Sahrens 	start = uio->uio_loffset;
398789Sahrens 	off = start & PAGEOFFSET;
399789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
400789Sahrens 		page_t *pp;
4013638Sbillm 		uint64_t bytes = MIN(PAGESIZE - off, len);
4023638Sbillm 
403789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
404789Sahrens 			caddr_t va;
405789Sahrens 
4067315SJonathan.Adams@Sun.COM 			va = zfs_map_page(pp, S_READ);
407789Sahrens 			error = uiomove(va + off, bytes, UIO_READ, uio);
4087315SJonathan.Adams@Sun.COM 			zfs_unmap_page(pp, va);
409789Sahrens 			page_unlock(pp);
410789Sahrens 		} else {
4113638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, bytes);
412789Sahrens 		}
413789Sahrens 		len -= bytes;
414789Sahrens 		off = 0;
415789Sahrens 		if (error)
416789Sahrens 			break;
417789Sahrens 	}
418789Sahrens 	return (error);
419789Sahrens }
420789Sahrens 
4213638Sbillm offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
422789Sahrens 
423789Sahrens /*
424789Sahrens  * Read bytes from specified file into supplied buffer.
425789Sahrens  *
426789Sahrens  *	IN:	vp	- vnode of file to be read from.
427789Sahrens  *		uio	- structure supplying read location, range info,
428789Sahrens  *			  and return buffer.
429789Sahrens  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
430789Sahrens  *		cr	- credentials of caller.
4315331Samw  *		ct	- caller context
432789Sahrens  *
433789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
434789Sahrens  *
435789Sahrens  *	RETURN:	0 if success
436789Sahrens  *		error code if failure
437789Sahrens  *
438789Sahrens  * Side Effects:
439789Sahrens  *	vp - atime updated if byte count > 0
440789Sahrens  */
441789Sahrens /* ARGSUSED */
442789Sahrens static int
443789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
444789Sahrens {
445789Sahrens 	znode_t		*zp = VTOZ(vp);
446789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4475326Sek110237 	objset_t	*os;
4483638Sbillm 	ssize_t		n, nbytes;
4493638Sbillm 	int		error;
4501669Sperrin 	rl_t		*rl;
45111539SChunli.Zhang@Sun.COM 	xuio_t		*xuio = NULL;
452789Sahrens 
4535367Sahrens 	ZFS_ENTER(zfsvfs);
4545367Sahrens 	ZFS_VERIFY_ZP(zp);
4555326Sek110237 	os = zfsvfs->z_os;
456789Sahrens 
45711935SMark.Shellenbaum@Sun.COM 	if (zp->z_pflags & ZFS_AV_QUARANTINED) {
4585929Smarks 		ZFS_EXIT(zfsvfs);
4595929Smarks 		return (EACCES);
4605929Smarks 	}
4615929Smarks 
462789Sahrens 	/*
463789Sahrens 	 * Validate file offset
464789Sahrens 	 */
465789Sahrens 	if (uio->uio_loffset < (offset_t)0) {
466789Sahrens 		ZFS_EXIT(zfsvfs);
467789Sahrens 		return (EINVAL);
468789Sahrens 	}
469789Sahrens 
470789Sahrens 	/*
471789Sahrens 	 * Fasttrack empty reads
472789Sahrens 	 */
473789Sahrens 	if (uio->uio_resid == 0) {
474789Sahrens 		ZFS_EXIT(zfsvfs);
475789Sahrens 		return (0);
476789Sahrens 	}
477789Sahrens 
478789Sahrens 	/*
4791669Sperrin 	 * Check for mandatory locks
480789Sahrens 	 */
48111935SMark.Shellenbaum@Sun.COM 	if (MANDMODE(zp->z_mode)) {
482789Sahrens 		if (error = chklock(vp, FREAD,
483789Sahrens 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
484789Sahrens 			ZFS_EXIT(zfsvfs);
485789Sahrens 			return (error);
486789Sahrens 		}
487789Sahrens 	}
488789Sahrens 
489789Sahrens 	/*
490789Sahrens 	 * If we're in FRSYNC mode, sync out this znode before reading it.
491789Sahrens 	 */
49212294SMark.Musante@Sun.COM 	if (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4932638Sperrin 		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
494789Sahrens 
495789Sahrens 	/*
4961669Sperrin 	 * Lock the range against changes.
497789Sahrens 	 */
4981669Sperrin 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
4991669Sperrin 
500789Sahrens 	/*
501789Sahrens 	 * If we are reading past end-of-file we can skip
502789Sahrens 	 * to the end; but we might still need to set atime.
503789Sahrens 	 */
50411935SMark.Shellenbaum@Sun.COM 	if (uio->uio_loffset >= zp->z_size) {
505789Sahrens 		error = 0;
506789Sahrens 		goto out;
507789Sahrens 	}
508789Sahrens 
50911935SMark.Shellenbaum@Sun.COM 	ASSERT(uio->uio_loffset < zp->z_size);
51011935SMark.Shellenbaum@Sun.COM 	n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
5113638Sbillm 
51211539SChunli.Zhang@Sun.COM 	if ((uio->uio_extflg == UIO_XUIO) &&
51311539SChunli.Zhang@Sun.COM 	    (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
51411539SChunli.Zhang@Sun.COM 		int nblk;
51511539SChunli.Zhang@Sun.COM 		int blksz = zp->z_blksz;
51611539SChunli.Zhang@Sun.COM 		uint64_t offset = uio->uio_loffset;
51711539SChunli.Zhang@Sun.COM 
51811539SChunli.Zhang@Sun.COM 		xuio = (xuio_t *)uio;
51911539SChunli.Zhang@Sun.COM 		if ((ISP2(blksz))) {
52011539SChunli.Zhang@Sun.COM 			nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
52111539SChunli.Zhang@Sun.COM 			    blksz)) / blksz;
52211539SChunli.Zhang@Sun.COM 		} else {
52311539SChunli.Zhang@Sun.COM 			ASSERT(offset + n <= blksz);
52411539SChunli.Zhang@Sun.COM 			nblk = 1;
52511539SChunli.Zhang@Sun.COM 		}
52611576SSurya.Prakki@Sun.COM 		(void) dmu_xuio_init(xuio, nblk);
52711539SChunli.Zhang@Sun.COM 
52811539SChunli.Zhang@Sun.COM 		if (vn_has_cached_data(vp)) {
52911539SChunli.Zhang@Sun.COM 			/*
53011539SChunli.Zhang@Sun.COM 			 * For simplicity, we always allocate a full buffer
53111539SChunli.Zhang@Sun.COM 			 * even if we only expect to read a portion of a block.
53211539SChunli.Zhang@Sun.COM 			 */
53311539SChunli.Zhang@Sun.COM 			while (--nblk >= 0) {
53411576SSurya.Prakki@Sun.COM 				(void) dmu_xuio_add(xuio,
53511935SMark.Shellenbaum@Sun.COM 				    dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
53611935SMark.Shellenbaum@Sun.COM 				    blksz), 0, blksz);
53711539SChunli.Zhang@Sun.COM 			}
53811539SChunli.Zhang@Sun.COM 		}
53911539SChunli.Zhang@Sun.COM 	}
54011539SChunli.Zhang@Sun.COM 
5413638Sbillm 	while (n > 0) {
5423638Sbillm 		nbytes = MIN(n, zfs_read_chunk_size -
5433638Sbillm 		    P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
5443638Sbillm 
5453638Sbillm 		if (vn_has_cached_data(vp))
5463638Sbillm 			error = mappedread(vp, nbytes, uio);
5473638Sbillm 		else
5483638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, nbytes);
5497294Sperrin 		if (error) {
5507294Sperrin 			/* convert checksum errors into IO errors */
5517294Sperrin 			if (error == ECKSUM)
5527294Sperrin 				error = EIO;
5533638Sbillm 			break;
5547294Sperrin 		}
5553638Sbillm 
5563638Sbillm 		n -= nbytes;
557789Sahrens 	}
558789Sahrens out:
5592237Smaybee 	zfs_range_unlock(rl);
560789Sahrens 
561789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
562789Sahrens 	ZFS_EXIT(zfsvfs);
563789Sahrens 	return (error);
564789Sahrens }
565789Sahrens 
566789Sahrens /*
567789Sahrens  * Write the bytes to a file.
568789Sahrens  *
569789Sahrens  *	IN:	vp	- vnode of file to be written to.
570789Sahrens  *		uio	- structure supplying write location, range info,
571789Sahrens  *			  and data buffer.
572789Sahrens  *		ioflag	- FAPPEND flag set if in append mode.
573789Sahrens  *		cr	- credentials of caller.
5745331Samw  *		ct	- caller context (NFS/CIFS fem monitor only)
575789Sahrens  *
576789Sahrens  *	OUT:	uio	- updated offset and range.
577789Sahrens  *
578789Sahrens  *	RETURN:	0 if success
579789Sahrens  *		error code if failure
580789Sahrens  *
581789Sahrens  * Timestamps:
582789Sahrens  *	vp - ctime|mtime updated if byte count > 0
583789Sahrens  */
58411935SMark.Shellenbaum@Sun.COM 
585789Sahrens /* ARGSUSED */
586789Sahrens static int
587789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
588789Sahrens {
589789Sahrens 	znode_t		*zp = VTOZ(vp);
590789Sahrens 	rlim64_t	limit = uio->uio_llimit;
591789Sahrens 	ssize_t		start_resid = uio->uio_resid;
592789Sahrens 	ssize_t		tx_bytes;
593789Sahrens 	uint64_t	end_size;
594789Sahrens 	dmu_tx_t	*tx;
595789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
5965326Sek110237 	zilog_t		*zilog;
597789Sahrens 	offset_t	woff;
598789Sahrens 	ssize_t		n, nbytes;
5991669Sperrin 	rl_t		*rl;
600789Sahrens 	int		max_blksz = zfsvfs->z_max_blksz;
6011669Sperrin 	int		error;
6029412SAleksandr.Guzovskiy@Sun.COM 	arc_buf_t	*abuf;
60311539SChunli.Zhang@Sun.COM 	iovec_t		*aiov;
60411539SChunli.Zhang@Sun.COM 	xuio_t		*xuio = NULL;
60511539SChunli.Zhang@Sun.COM 	int		i_iov = 0;
60611539SChunli.Zhang@Sun.COM 	int		iovcnt = uio->uio_iovcnt;
60711539SChunli.Zhang@Sun.COM 	iovec_t		*iovp = uio->uio_iov;
60811539SChunli.Zhang@Sun.COM 	int		write_eof;
60911935SMark.Shellenbaum@Sun.COM 	int		count = 0;
61011935SMark.Shellenbaum@Sun.COM 	sa_bulk_attr_t	bulk[4];
61111935SMark.Shellenbaum@Sun.COM 	uint64_t	mtime[2], ctime[2];
612789Sahrens 
613789Sahrens 	/*
614789Sahrens 	 * Fasttrack empty write
615789Sahrens 	 */
6161669Sperrin 	n = start_resid;
617789Sahrens 	if (n == 0)
618789Sahrens 		return (0);
619789Sahrens 
6201669Sperrin 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
6211669Sperrin 		limit = MAXOFFSET_T;
6221669Sperrin 
6235367Sahrens 	ZFS_ENTER(zfsvfs);
6245367Sahrens 	ZFS_VERIFY_ZP(zp);
6256743Smarks 
62611935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
62711935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
62811935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
62911935SMark.Shellenbaum@Sun.COM 	    &zp->z_size, 8);
63011935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
63111935SMark.Shellenbaum@Sun.COM 	    &zp->z_pflags, 8);
63211935SMark.Shellenbaum@Sun.COM 
6336743Smarks 	/*
6346743Smarks 	 * If immutable or not appending then return EPERM
6356743Smarks 	 */
63611935SMark.Shellenbaum@Sun.COM 	if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
63711935SMark.Shellenbaum@Sun.COM 	    ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
63811935SMark.Shellenbaum@Sun.COM 	    (uio->uio_loffset < zp->z_size))) {
6396743Smarks 		ZFS_EXIT(zfsvfs);
6406743Smarks 		return (EPERM);
6416743Smarks 	}
6426743Smarks 
6435326Sek110237 	zilog = zfsvfs->z_log;
644789Sahrens 
645789Sahrens 	/*
64611083Swilliam.gorrell@sun.com 	 * Validate file offset
64711083Swilliam.gorrell@sun.com 	 */
64811935SMark.Shellenbaum@Sun.COM 	woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
64911083Swilliam.gorrell@sun.com 	if (woff < 0) {
65011083Swilliam.gorrell@sun.com 		ZFS_EXIT(zfsvfs);
65111083Swilliam.gorrell@sun.com 		return (EINVAL);
65211083Swilliam.gorrell@sun.com 	}
65311083Swilliam.gorrell@sun.com 
65411083Swilliam.gorrell@sun.com 	/*
65511083Swilliam.gorrell@sun.com 	 * Check for mandatory locks before calling zfs_range_lock()
65611083Swilliam.gorrell@sun.com 	 * in order to prevent a deadlock with locks set via fcntl().
65711083Swilliam.gorrell@sun.com 	 */
65811935SMark.Shellenbaum@Sun.COM 	if (MANDMODE((mode_t)zp->z_mode) &&
65911083Swilliam.gorrell@sun.com 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
66011083Swilliam.gorrell@sun.com 		ZFS_EXIT(zfsvfs);
66111083Swilliam.gorrell@sun.com 		return (error);
66211083Swilliam.gorrell@sun.com 	}
66311083Swilliam.gorrell@sun.com 
66411083Swilliam.gorrell@sun.com 	/*
6652237Smaybee 	 * Pre-fault the pages to ensure slow (eg NFS) pages
6661669Sperrin 	 * don't hold up txg.
66711539SChunli.Zhang@Sun.COM 	 * Skip this if uio contains loaned arc_buf.
668789Sahrens 	 */
66911539SChunli.Zhang@Sun.COM 	if ((uio->uio_extflg == UIO_XUIO) &&
67011539SChunli.Zhang@Sun.COM 	    (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
67111539SChunli.Zhang@Sun.COM 		xuio = (xuio_t *)uio;
67211539SChunli.Zhang@Sun.COM 	else
67311539SChunli.Zhang@Sun.COM 		uio_prefaultpages(n, uio);
674789Sahrens 
675789Sahrens 	/*
676789Sahrens 	 * If in append mode, set the io offset pointer to eof.
677789Sahrens 	 */
6781669Sperrin 	if (ioflag & FAPPEND) {
6791669Sperrin 		/*
68011083Swilliam.gorrell@sun.com 		 * Obtain an appending range lock to guarantee file append
68111083Swilliam.gorrell@sun.com 		 * semantics.  We reset the write offset once we have the lock.
6821669Sperrin 		 */
6831669Sperrin 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
68411083Swilliam.gorrell@sun.com 		woff = rl->r_off;
6851669Sperrin 		if (rl->r_len == UINT64_MAX) {
68611083Swilliam.gorrell@sun.com 			/*
68711083Swilliam.gorrell@sun.com 			 * We overlocked the file because this write will cause
68811083Swilliam.gorrell@sun.com 			 * the file block size to increase.
68911083Swilliam.gorrell@sun.com 			 * Note that zp_size cannot change with this lock held.
69011083Swilliam.gorrell@sun.com 			 */
69111935SMark.Shellenbaum@Sun.COM 			woff = zp->z_size;
6921669Sperrin 		}
69311083Swilliam.gorrell@sun.com 		uio->uio_loffset = woff;
694789Sahrens 	} else {
695789Sahrens 		/*
69611083Swilliam.gorrell@sun.com 		 * Note that if the file block size will change as a result of
69711083Swilliam.gorrell@sun.com 		 * this write, then this range lock will lock the entire file
69811083Swilliam.gorrell@sun.com 		 * so that we can re-write the block safely.
699789Sahrens 		 */
7001669Sperrin 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
701789Sahrens 	}
702789Sahrens 
703789Sahrens 	if (woff >= limit) {
7043638Sbillm 		zfs_range_unlock(rl);
7053638Sbillm 		ZFS_EXIT(zfsvfs);
7063638Sbillm 		return (EFBIG);
707789Sahrens 	}
708789Sahrens 
709789Sahrens 	if ((woff + n) > limit || woff > (limit - n))
710789Sahrens 		n = limit - woff;
711789Sahrens 
71211539SChunli.Zhang@Sun.COM 	/* Will this write extend the file length? */
71311935SMark.Shellenbaum@Sun.COM 	write_eof = (woff + n > zp->z_size);
71411935SMark.Shellenbaum@Sun.COM 
71511935SMark.Shellenbaum@Sun.COM 	end_size = MAX(zp->z_size, woff + n);
716789Sahrens 
7171669Sperrin 	/*
7183638Sbillm 	 * Write the file in reasonable size chunks.  Each chunk is written
7193638Sbillm 	 * in a separate transaction; this keeps the intent log records small
7203638Sbillm 	 * and allows us to do more fine-grained space accounting.
721789Sahrens 	 */
722789Sahrens 	while (n > 0) {
7239412SAleksandr.Guzovskiy@Sun.COM 		abuf = NULL;
7249412SAleksandr.Guzovskiy@Sun.COM 		woff = uio->uio_loffset;
7259412SAleksandr.Guzovskiy@Sun.COM again:
72611935SMark.Shellenbaum@Sun.COM 		if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
72711935SMark.Shellenbaum@Sun.COM 		    zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
7289412SAleksandr.Guzovskiy@Sun.COM 			if (abuf != NULL)
7299412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7309396SMatthew.Ahrens@Sun.COM 			error = EDQUOT;
7319396SMatthew.Ahrens@Sun.COM 			break;
7329396SMatthew.Ahrens@Sun.COM 		}
7339412SAleksandr.Guzovskiy@Sun.COM 
73411539SChunli.Zhang@Sun.COM 		if (xuio && abuf == NULL) {
73511539SChunli.Zhang@Sun.COM 			ASSERT(i_iov < iovcnt);
73611539SChunli.Zhang@Sun.COM 			aiov = &iovp[i_iov];
73711539SChunli.Zhang@Sun.COM 			abuf = dmu_xuio_arcbuf(xuio, i_iov);
73811539SChunli.Zhang@Sun.COM 			dmu_xuio_clear(xuio, i_iov);
73911539SChunli.Zhang@Sun.COM 			DTRACE_PROBE3(zfs_cp_write, int, i_iov,
74011539SChunli.Zhang@Sun.COM 			    iovec_t *, aiov, arc_buf_t *, abuf);
74111539SChunli.Zhang@Sun.COM 			ASSERT((aiov->iov_base == abuf->b_data) ||
74211539SChunli.Zhang@Sun.COM 			    ((char *)aiov->iov_base - (char *)abuf->b_data +
74311539SChunli.Zhang@Sun.COM 			    aiov->iov_len == arc_buf_size(abuf)));
74411539SChunli.Zhang@Sun.COM 			i_iov++;
74511539SChunli.Zhang@Sun.COM 		} else if (abuf == NULL && n >= max_blksz &&
74611935SMark.Shellenbaum@Sun.COM 		    woff >= zp->z_size &&
7479412SAleksandr.Guzovskiy@Sun.COM 		    P2PHASE(woff, max_blksz) == 0 &&
7489412SAleksandr.Guzovskiy@Sun.COM 		    zp->z_blksz == max_blksz) {
74911539SChunli.Zhang@Sun.COM 			/*
75011539SChunli.Zhang@Sun.COM 			 * This write covers a full block.  "Borrow" a buffer
75111539SChunli.Zhang@Sun.COM 			 * from the dmu so that we can fill it before we enter
75211539SChunli.Zhang@Sun.COM 			 * a transaction.  This avoids the possibility of
75311539SChunli.Zhang@Sun.COM 			 * holding up the transaction if the data copy hangs
75411539SChunli.Zhang@Sun.COM 			 * up on a pagefault (e.g., from an NFS server mapping).
75511539SChunli.Zhang@Sun.COM 			 */
7569412SAleksandr.Guzovskiy@Sun.COM 			size_t cbytes;
7579412SAleksandr.Guzovskiy@Sun.COM 
75811935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
75911935SMark.Shellenbaum@Sun.COM 			    max_blksz);
7609412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(abuf != NULL);
7619412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(arc_buf_size(abuf) == max_blksz);
7629412SAleksandr.Guzovskiy@Sun.COM 			if (error = uiocopy(abuf->b_data, max_blksz,
7639412SAleksandr.Guzovskiy@Sun.COM 			    UIO_WRITE, uio, &cbytes)) {
7649412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7659412SAleksandr.Guzovskiy@Sun.COM 				break;
7669412SAleksandr.Guzovskiy@Sun.COM 			}
7679412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(cbytes == max_blksz);
7689412SAleksandr.Guzovskiy@Sun.COM 		}
7699412SAleksandr.Guzovskiy@Sun.COM 
7709412SAleksandr.Guzovskiy@Sun.COM 		/*
7719412SAleksandr.Guzovskiy@Sun.COM 		 * Start a transaction.
7729412SAleksandr.Guzovskiy@Sun.COM 		 */
773789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
77411935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
775789Sahrens 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
77611935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, zp);
7778227SNeil.Perrin@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
778789Sahrens 		if (error) {
7798227SNeil.Perrin@Sun.COM 			if (error == ERESTART) {
7802113Sahrens 				dmu_tx_wait(tx);
7812113Sahrens 				dmu_tx_abort(tx);
7829412SAleksandr.Guzovskiy@Sun.COM 				goto again;
783789Sahrens 			}
7842113Sahrens 			dmu_tx_abort(tx);
7859412SAleksandr.Guzovskiy@Sun.COM 			if (abuf != NULL)
7869412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7873638Sbillm 			break;
7883638Sbillm 		}
7893638Sbillm 
7903638Sbillm 		/*
7913638Sbillm 		 * If zfs_range_lock() over-locked we grow the blocksize
7923638Sbillm 		 * and then reduce the lock range.  This will only happen
7933638Sbillm 		 * on the first iteration since zfs_range_reduce() will
7943638Sbillm 		 * shrink down r_len to the appropriate size.
7953638Sbillm 		 */
7963638Sbillm 		if (rl->r_len == UINT64_MAX) {
7973638Sbillm 			uint64_t new_blksz;
7983638Sbillm 
7993638Sbillm 			if (zp->z_blksz > max_blksz) {
8003638Sbillm 				ASSERT(!ISP2(zp->z_blksz));
8013638Sbillm 				new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
8023638Sbillm 			} else {
8033638Sbillm 				new_blksz = MIN(end_size, max_blksz);
8043638Sbillm 			}
8053638Sbillm 			zfs_grow_blocksize(zp, new_blksz, tx);
8063638Sbillm 			zfs_range_reduce(rl, woff, n);
8073638Sbillm 		}
8083638Sbillm 
8093638Sbillm 		/*
8103638Sbillm 		 * XXX - should we really limit each write to z_max_blksz?
8113638Sbillm 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
8123638Sbillm 		 */
8133638Sbillm 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
8143638Sbillm 
8159412SAleksandr.Guzovskiy@Sun.COM 		if (abuf == NULL) {
8169412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes = uio->uio_resid;
81712123STim.Haley@Sun.COM 			error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
81812123STim.Haley@Sun.COM 			    uio, nbytes, tx);
8199412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes -= uio->uio_resid;
8209412SAleksandr.Guzovskiy@Sun.COM 		} else {
8219412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes = nbytes;
82211539SChunli.Zhang@Sun.COM 			ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
82311539SChunli.Zhang@Sun.COM 			/*
82411539SChunli.Zhang@Sun.COM 			 * If this is not a full block write, but we are
82511539SChunli.Zhang@Sun.COM 			 * extending the file past EOF and this data starts
82611539SChunli.Zhang@Sun.COM 			 * block-aligned, use assign_arcbuf().  Otherwise,
82711539SChunli.Zhang@Sun.COM 			 * write via dmu_write().
82811539SChunli.Zhang@Sun.COM 			 */
82911539SChunli.Zhang@Sun.COM 			if (tx_bytes < max_blksz && (!write_eof ||
83011539SChunli.Zhang@Sun.COM 			    aiov->iov_base != abuf->b_data)) {
83111539SChunli.Zhang@Sun.COM 				ASSERT(xuio);
83211539SChunli.Zhang@Sun.COM 				dmu_write(zfsvfs->z_os, zp->z_id, woff,
83311539SChunli.Zhang@Sun.COM 				    aiov->iov_len, aiov->iov_base, tx);
83411539SChunli.Zhang@Sun.COM 				dmu_return_arcbuf(abuf);
83511539SChunli.Zhang@Sun.COM 				xuio_stat_wbuf_copied();
83611539SChunli.Zhang@Sun.COM 			} else {
83711539SChunli.Zhang@Sun.COM 				ASSERT(xuio || tx_bytes == max_blksz);
83811935SMark.Shellenbaum@Sun.COM 				dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
83911935SMark.Shellenbaum@Sun.COM 				    woff, abuf, tx);
84011539SChunli.Zhang@Sun.COM 			}
8419412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(tx_bytes <= uio->uio_resid);
8429412SAleksandr.Guzovskiy@Sun.COM 			uioskip(uio, tx_bytes);
8439412SAleksandr.Guzovskiy@Sun.COM 		}
8449412SAleksandr.Guzovskiy@Sun.COM 		if (tx_bytes && vn_has_cached_data(vp)) {
8458636SMark.Maybee@Sun.COM 			update_pages(vp, woff,
8468636SMark.Maybee@Sun.COM 			    tx_bytes, zfsvfs->z_os, zp->z_id);
8479412SAleksandr.Guzovskiy@Sun.COM 		}
8483638Sbillm 
8493638Sbillm 		/*
8503638Sbillm 		 * If we made no progress, we're done.  If we made even
8513638Sbillm 		 * partial progress, update the znode and ZIL accordingly.
8523638Sbillm 		 */
8533638Sbillm 		if (tx_bytes == 0) {
85411935SMark.Shellenbaum@Sun.COM 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
85511935SMark.Shellenbaum@Sun.COM 			    (void *)&zp->z_size, sizeof (uint64_t), tx);
8563897Smaybee 			dmu_tx_commit(tx);
8573638Sbillm 			ASSERT(error != 0);
8583638Sbillm 			break;
8593638Sbillm 		}
8603638Sbillm 
861789Sahrens 		/*
8623638Sbillm 		 * Clear Set-UID/Set-GID bits on successful write if not
8633638Sbillm 		 * privileged and at least one of the excute bits is set.
8643638Sbillm 		 *
8653638Sbillm 		 * It would be nice to to this after all writes have
8663638Sbillm 		 * been done, but that would still expose the ISUID/ISGID
8673638Sbillm 		 * to another app after the partial write is committed.
8685331Samw 		 *
869789Sahrens 		 */
8703638Sbillm 		mutex_enter(&zp->z_acl_lock);
87111935SMark.Shellenbaum@Sun.COM 		if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
8723638Sbillm 		    (S_IXUSR >> 6))) != 0 &&
87311935SMark.Shellenbaum@Sun.COM 		    (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
8743638Sbillm 		    secpolicy_vnode_setid_retain(cr,
87511935SMark.Shellenbaum@Sun.COM 		    (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
87611935SMark.Shellenbaum@Sun.COM 			uint64_t newmode;
87711935SMark.Shellenbaum@Sun.COM 			zp->z_mode &= ~(S_ISUID | S_ISGID);
87811935SMark.Shellenbaum@Sun.COM 			newmode = zp->z_mode;
87911935SMark.Shellenbaum@Sun.COM 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
88011935SMark.Shellenbaum@Sun.COM 			    (void *)&newmode, sizeof (uint64_t), tx);
8813638Sbillm 		}
8823638Sbillm 		mutex_exit(&zp->z_acl_lock);
8833638Sbillm 
88411935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
88511935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
8863638Sbillm 
8873638Sbillm 		/*
8883638Sbillm 		 * Update the file size (zp_size) if it has changed;
8893638Sbillm 		 * account for possible concurrent updates.
8903638Sbillm 		 */
89111935SMark.Shellenbaum@Sun.COM 		while ((end_size = zp->z_size) < uio->uio_loffset) {
89211935SMark.Shellenbaum@Sun.COM 			(void) atomic_cas_64(&zp->z_size, end_size,
893789Sahrens 			    uio->uio_loffset);
89411935SMark.Shellenbaum@Sun.COM 			ASSERT(error == 0);
89511935SMark.Shellenbaum@Sun.COM 		}
89611935SMark.Shellenbaum@Sun.COM 		error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
89711935SMark.Shellenbaum@Sun.COM 
8983638Sbillm 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
8993638Sbillm 		dmu_tx_commit(tx);
9003638Sbillm 
9013638Sbillm 		if (error != 0)
9023638Sbillm 			break;
9033638Sbillm 		ASSERT(tx_bytes == nbytes);
9043638Sbillm 		n -= nbytes;
905789Sahrens 	}
906789Sahrens 
9072237Smaybee 	zfs_range_unlock(rl);
908789Sahrens 
909789Sahrens 	/*
910789Sahrens 	 * If we're in replay mode, or we made no progress, return error.
911789Sahrens 	 * Otherwise, it's at least a partial write, so it's successful.
912789Sahrens 	 */
9138227SNeil.Perrin@Sun.COM 	if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
914789Sahrens 		ZFS_EXIT(zfsvfs);
915789Sahrens 		return (error);
916789Sahrens 	}
917789Sahrens 
91812294SMark.Musante@Sun.COM 	if (ioflag & (FSYNC | FDSYNC) ||
91912294SMark.Musante@Sun.COM 	    zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
9202638Sperrin 		zil_commit(zilog, zp->z_last_itx, zp->z_id);
921789Sahrens 
922789Sahrens 	ZFS_EXIT(zfsvfs);
923789Sahrens 	return (0);
924789Sahrens }
925789Sahrens 
9262237Smaybee void
92710922SJeff.Bonwick@Sun.COM zfs_get_done(zgd_t *zgd, int error)
9282237Smaybee {
92910922SJeff.Bonwick@Sun.COM 	znode_t *zp = zgd->zgd_private;
93010922SJeff.Bonwick@Sun.COM 	objset_t *os = zp->z_zfsvfs->z_os;
93110922SJeff.Bonwick@Sun.COM 
93210922SJeff.Bonwick@Sun.COM 	if (zgd->zgd_db)
93310922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(zgd->zgd_db, zgd);
93410922SJeff.Bonwick@Sun.COM 
93510922SJeff.Bonwick@Sun.COM 	zfs_range_unlock(zgd->zgd_rl);
93610922SJeff.Bonwick@Sun.COM 
9379321SNeil.Perrin@Sun.COM 	/*
9389321SNeil.Perrin@Sun.COM 	 * Release the vnode asynchronously as we currently have the
9399321SNeil.Perrin@Sun.COM 	 * txg stopped from syncing.
9409321SNeil.Perrin@Sun.COM 	 */
94110922SJeff.Bonwick@Sun.COM 	VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
94210922SJeff.Bonwick@Sun.COM 
94310922SJeff.Bonwick@Sun.COM 	if (error == 0 && zgd->zgd_bp)
94410922SJeff.Bonwick@Sun.COM 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
94510922SJeff.Bonwick@Sun.COM 
9463063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
9472237Smaybee }
9482237Smaybee 
94910209SMark.Musante@Sun.COM #ifdef DEBUG
95010209SMark.Musante@Sun.COM static int zil_fault_io = 0;
95110209SMark.Musante@Sun.COM #endif
95210209SMark.Musante@Sun.COM 
953789Sahrens /*
954789Sahrens  * Get data to generate a TX_WRITE intent log record.
955789Sahrens  */
956789Sahrens int
9572237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
958789Sahrens {
959789Sahrens 	zfsvfs_t *zfsvfs = arg;
960789Sahrens 	objset_t *os = zfsvfs->z_os;
961789Sahrens 	znode_t *zp;
96210922SJeff.Bonwick@Sun.COM 	uint64_t object = lr->lr_foid;
96310922SJeff.Bonwick@Sun.COM 	uint64_t offset = lr->lr_offset;
96410922SJeff.Bonwick@Sun.COM 	uint64_t size = lr->lr_length;
96510922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = &lr->lr_blkptr;
9662237Smaybee 	dmu_buf_t *db;
9673063Sperrin 	zgd_t *zgd;
968789Sahrens 	int error = 0;
969789Sahrens 
97010922SJeff.Bonwick@Sun.COM 	ASSERT(zio != NULL);
97110922SJeff.Bonwick@Sun.COM 	ASSERT(size != 0);
972789Sahrens 
973789Sahrens 	/*
9741669Sperrin 	 * Nothing to do if the file has been removed
975789Sahrens 	 */
97610922SJeff.Bonwick@Sun.COM 	if (zfs_zget(zfsvfs, object, &zp) != 0)
977789Sahrens 		return (ENOENT);
9783461Sahrens 	if (zp->z_unlinked) {
9799321SNeil.Perrin@Sun.COM 		/*
9809321SNeil.Perrin@Sun.COM 		 * Release the vnode asynchronously as we currently have the
9819321SNeil.Perrin@Sun.COM 		 * txg stopped from syncing.
9829321SNeil.Perrin@Sun.COM 		 */
9839321SNeil.Perrin@Sun.COM 		VN_RELE_ASYNC(ZTOV(zp),
9849321SNeil.Perrin@Sun.COM 		    dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
985789Sahrens 		return (ENOENT);
986789Sahrens 	}
987789Sahrens 
98810922SJeff.Bonwick@Sun.COM 	zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
98910922SJeff.Bonwick@Sun.COM 	zgd->zgd_zilog = zfsvfs->z_log;
99010922SJeff.Bonwick@Sun.COM 	zgd->zgd_private = zp;
99110922SJeff.Bonwick@Sun.COM 
992789Sahrens 	/*
993789Sahrens 	 * Write records come in two flavors: immediate and indirect.
994789Sahrens 	 * For small writes it's cheaper to store the data with the
995789Sahrens 	 * log record (immediate); for large writes it's cheaper to
996789Sahrens 	 * sync the data and get a pointer to it (indirect) so that
997789Sahrens 	 * we don't have to write the data twice.
998789Sahrens 	 */
9991669Sperrin 	if (buf != NULL) { /* immediate write */
100010922SJeff.Bonwick@Sun.COM 		zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
10011669Sperrin 		/* test for truncation needs to be done while range locked */
100211935SMark.Shellenbaum@Sun.COM 		if (offset >= zp->z_size) {
10031669Sperrin 			error = ENOENT;
100410922SJeff.Bonwick@Sun.COM 		} else {
100510922SJeff.Bonwick@Sun.COM 			error = dmu_read(os, object, offset, size, buf,
100610922SJeff.Bonwick@Sun.COM 			    DMU_READ_NO_PREFETCH);
10071669Sperrin 		}
100810922SJeff.Bonwick@Sun.COM 		ASSERT(error == 0 || error == ENOENT);
10091669Sperrin 	} else { /* indirect write */
1010789Sahrens 		/*
10111669Sperrin 		 * Have to lock the whole block to ensure when it's
10121669Sperrin 		 * written out and it's checksum is being calculated
10131669Sperrin 		 * that no one can change the data. We need to re-check
10141669Sperrin 		 * blocksize after we get the lock in case it's changed!
1015789Sahrens 		 */
10161669Sperrin 		for (;;) {
101710922SJeff.Bonwick@Sun.COM 			uint64_t blkoff;
101810922SJeff.Bonwick@Sun.COM 			size = zp->z_blksz;
101910945SJeff.Bonwick@Sun.COM 			blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
102010922SJeff.Bonwick@Sun.COM 			offset -= blkoff;
102110922SJeff.Bonwick@Sun.COM 			zgd->zgd_rl = zfs_range_lock(zp, offset, size,
102210922SJeff.Bonwick@Sun.COM 			    RL_READER);
102310922SJeff.Bonwick@Sun.COM 			if (zp->z_blksz == size)
10241669Sperrin 				break;
102510922SJeff.Bonwick@Sun.COM 			offset += blkoff;
102610922SJeff.Bonwick@Sun.COM 			zfs_range_unlock(zgd->zgd_rl);
10271669Sperrin 		}
10281669Sperrin 		/* test for truncation needs to be done while range locked */
102911935SMark.Shellenbaum@Sun.COM 		if (lr->lr_offset >= zp->z_size)
10301669Sperrin 			error = ENOENT;
103110209SMark.Musante@Sun.COM #ifdef DEBUG
103210209SMark.Musante@Sun.COM 		if (zil_fault_io) {
103310209SMark.Musante@Sun.COM 			error = EIO;
103410209SMark.Musante@Sun.COM 			zil_fault_io = 0;
103510209SMark.Musante@Sun.COM 		}
103610209SMark.Musante@Sun.COM #endif
103710922SJeff.Bonwick@Sun.COM 		if (error == 0)
103812285SJeff.Bonwick@Sun.COM 			error = dmu_buf_hold(os, object, offset, zgd, &db,
103912285SJeff.Bonwick@Sun.COM 			    DMU_READ_NO_PREFETCH);
104010922SJeff.Bonwick@Sun.COM 
104110800SNeil.Perrin@Sun.COM 		if (error == 0) {
104210922SJeff.Bonwick@Sun.COM 			zgd->zgd_db = db;
104310922SJeff.Bonwick@Sun.COM 			zgd->zgd_bp = bp;
104410922SJeff.Bonwick@Sun.COM 
104510922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_offset == offset);
104610922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_size == size);
104710922SJeff.Bonwick@Sun.COM 
104810922SJeff.Bonwick@Sun.COM 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
104910922SJeff.Bonwick@Sun.COM 			    zfs_get_done, zgd);
105010922SJeff.Bonwick@Sun.COM 			ASSERT(error || lr->lr_length <= zp->z_blksz);
105110922SJeff.Bonwick@Sun.COM 
105210800SNeil.Perrin@Sun.COM 			/*
105310922SJeff.Bonwick@Sun.COM 			 * On success, we need to wait for the write I/O
105410922SJeff.Bonwick@Sun.COM 			 * initiated by dmu_sync() to complete before we can
105510922SJeff.Bonwick@Sun.COM 			 * release this dbuf.  We will finish everything up
105610922SJeff.Bonwick@Sun.COM 			 * in the zfs_get_done() callback.
105710800SNeil.Perrin@Sun.COM 			 */
105810922SJeff.Bonwick@Sun.COM 			if (error == 0)
105910922SJeff.Bonwick@Sun.COM 				return (0);
106010922SJeff.Bonwick@Sun.COM 
106110922SJeff.Bonwick@Sun.COM 			if (error == EALREADY) {
106210922SJeff.Bonwick@Sun.COM 				lr->lr_common.lrc_txtype = TX_WRITE2;
106310922SJeff.Bonwick@Sun.COM 				error = 0;
106410922SJeff.Bonwick@Sun.COM 			}
106510800SNeil.Perrin@Sun.COM 		}
1066789Sahrens 	}
106710922SJeff.Bonwick@Sun.COM 
106810922SJeff.Bonwick@Sun.COM 	zfs_get_done(zgd, error);
106910922SJeff.Bonwick@Sun.COM 
1070789Sahrens 	return (error);
1071789Sahrens }
1072789Sahrens 
1073789Sahrens /*ARGSUSED*/
1074789Sahrens static int
10755331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
10765331Samw     caller_context_t *ct)
1077789Sahrens {
1078789Sahrens 	znode_t *zp = VTOZ(vp);
1079789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1080789Sahrens 	int error;
1081789Sahrens 
10825367Sahrens 	ZFS_ENTER(zfsvfs);
10835367Sahrens 	ZFS_VERIFY_ZP(zp);
10845331Samw 
10855331Samw 	if (flag & V_ACE_MASK)
10865331Samw 		error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
10875331Samw 	else
10885331Samw 		error = zfs_zaccess_rwx(zp, mode, flag, cr);
10895331Samw 
1090789Sahrens 	ZFS_EXIT(zfsvfs);
1091789Sahrens 	return (error);
1092789Sahrens }
1093789Sahrens 
1094789Sahrens /*
10959981STim.Haley@Sun.COM  * If vnode is for a device return a specfs vnode instead.
10969981STim.Haley@Sun.COM  */
10979981STim.Haley@Sun.COM static int
10989981STim.Haley@Sun.COM specvp_check(vnode_t **vpp, cred_t *cr)
10999981STim.Haley@Sun.COM {
11009981STim.Haley@Sun.COM 	int error = 0;
11019981STim.Haley@Sun.COM 
11029981STim.Haley@Sun.COM 	if (IS_DEVVP(*vpp)) {
11039981STim.Haley@Sun.COM 		struct vnode *svp;
11049981STim.Haley@Sun.COM 
11059981STim.Haley@Sun.COM 		svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
11069981STim.Haley@Sun.COM 		VN_RELE(*vpp);
11079981STim.Haley@Sun.COM 		if (svp == NULL)
11089981STim.Haley@Sun.COM 			error = ENOSYS;
11099981STim.Haley@Sun.COM 		*vpp = svp;
11109981STim.Haley@Sun.COM 	}
11119981STim.Haley@Sun.COM 	return (error);
11129981STim.Haley@Sun.COM }
11139981STim.Haley@Sun.COM 
11149981STim.Haley@Sun.COM 
11159981STim.Haley@Sun.COM /*
1116789Sahrens  * Lookup an entry in a directory, or an extended attribute directory.
1117789Sahrens  * If it exists, return a held vnode reference for it.
1118789Sahrens  *
1119789Sahrens  *	IN:	dvp	- vnode of directory to search.
1120789Sahrens  *		nm	- name of entry to lookup.
1121789Sahrens  *		pnp	- full pathname to lookup [UNUSED].
1122789Sahrens  *		flags	- LOOKUP_XATTR set if looking for an attribute.
1123789Sahrens  *		rdir	- root directory vnode [UNUSED].
1124789Sahrens  *		cr	- credentials of caller.
11255331Samw  *		ct	- caller context
11265331Samw  *		direntflags - directory lookup flags
11275331Samw  *		realpnp - returned pathname.
1128789Sahrens  *
1129789Sahrens  *	OUT:	vpp	- vnode of located entry, NULL if not found.
1130789Sahrens  *
1131789Sahrens  *	RETURN:	0 if success
1132789Sahrens  *		error code if failure
1133789Sahrens  *
1134789Sahrens  * Timestamps:
1135789Sahrens  *	NA
1136789Sahrens  */
1137789Sahrens /* ARGSUSED */
1138789Sahrens static int
1139789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
11405331Samw     int flags, vnode_t *rdir, cred_t *cr,  caller_context_t *ct,
11415331Samw     int *direntflags, pathname_t *realpnp)
1142789Sahrens {
1143789Sahrens 	znode_t *zdp = VTOZ(dvp);
1144789Sahrens 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
11459981STim.Haley@Sun.COM 	int	error = 0;
11469981STim.Haley@Sun.COM 
11479981STim.Haley@Sun.COM 	/* fast path */
11489981STim.Haley@Sun.COM 	if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
11499981STim.Haley@Sun.COM 
11509981STim.Haley@Sun.COM 		if (dvp->v_type != VDIR) {
11519981STim.Haley@Sun.COM 			return (ENOTDIR);
115211935SMark.Shellenbaum@Sun.COM 		} else if (zdp->z_sa_hdl == NULL) {
11539981STim.Haley@Sun.COM 			return (EIO);
11549981STim.Haley@Sun.COM 		}
11559981STim.Haley@Sun.COM 
11569981STim.Haley@Sun.COM 		if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
11579981STim.Haley@Sun.COM 			error = zfs_fastaccesschk_execute(zdp, cr);
11589981STim.Haley@Sun.COM 			if (!error) {
11599981STim.Haley@Sun.COM 				*vpp = dvp;
11609981STim.Haley@Sun.COM 				VN_HOLD(*vpp);
11619981STim.Haley@Sun.COM 				return (0);
11629981STim.Haley@Sun.COM 			}
11639981STim.Haley@Sun.COM 			return (error);
11649981STim.Haley@Sun.COM 		} else {
11659981STim.Haley@Sun.COM 			vnode_t *tvp = dnlc_lookup(dvp, nm);
11669981STim.Haley@Sun.COM 
11679981STim.Haley@Sun.COM 			if (tvp) {
11689981STim.Haley@Sun.COM 				error = zfs_fastaccesschk_execute(zdp, cr);
11699981STim.Haley@Sun.COM 				if (error) {
11709981STim.Haley@Sun.COM 					VN_RELE(tvp);
11719981STim.Haley@Sun.COM 					return (error);
11729981STim.Haley@Sun.COM 				}
11739981STim.Haley@Sun.COM 				if (tvp == DNLC_NO_VNODE) {
11749981STim.Haley@Sun.COM 					VN_RELE(tvp);
11759981STim.Haley@Sun.COM 					return (ENOENT);
11769981STim.Haley@Sun.COM 				} else {
11779981STim.Haley@Sun.COM 					*vpp = tvp;
11789981STim.Haley@Sun.COM 					return (specvp_check(vpp, cr));
11799981STim.Haley@Sun.COM 				}
11809981STim.Haley@Sun.COM 			}
11819981STim.Haley@Sun.COM 		}
11829981STim.Haley@Sun.COM 	}
11839981STim.Haley@Sun.COM 
11849981STim.Haley@Sun.COM 	DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1185789Sahrens 
11865367Sahrens 	ZFS_ENTER(zfsvfs);
11875367Sahrens 	ZFS_VERIFY_ZP(zdp);
1188789Sahrens 
1189789Sahrens 	*vpp = NULL;
1190789Sahrens 
1191789Sahrens 	if (flags & LOOKUP_XATTR) {
1192789Sahrens 		/*
11933234Sck153898 		 * If the xattr property is off, refuse the lookup request.
11943234Sck153898 		 */
11953234Sck153898 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
11963234Sck153898 			ZFS_EXIT(zfsvfs);
11973234Sck153898 			return (EINVAL);
11983234Sck153898 		}
11993234Sck153898 
12003234Sck153898 		/*
1201789Sahrens 		 * We don't allow recursive attributes..
1202789Sahrens 		 * Maybe someday we will.
1203789Sahrens 		 */
120411935SMark.Shellenbaum@Sun.COM 		if (zdp->z_pflags & ZFS_XATTR) {
1205789Sahrens 			ZFS_EXIT(zfsvfs);
1206789Sahrens 			return (EINVAL);
1207789Sahrens 		}
1208789Sahrens 
12093280Sck153898 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1210789Sahrens 			ZFS_EXIT(zfsvfs);
1211789Sahrens 			return (error);
1212789Sahrens 		}
1213789Sahrens 
1214789Sahrens 		/*
1215789Sahrens 		 * Do we have permission to get into attribute directory?
1216789Sahrens 		 */
1217789Sahrens 
12185331Samw 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
12195331Samw 		    B_FALSE, cr)) {
1220789Sahrens 			VN_RELE(*vpp);
12215331Samw 			*vpp = NULL;
1222789Sahrens 		}
1223789Sahrens 
1224789Sahrens 		ZFS_EXIT(zfsvfs);
1225789Sahrens 		return (error);
1226789Sahrens 	}
1227789Sahrens 
12281512Sek110237 	if (dvp->v_type != VDIR) {
12291512Sek110237 		ZFS_EXIT(zfsvfs);
12301460Smarks 		return (ENOTDIR);
12311512Sek110237 	}
12321460Smarks 
1233789Sahrens 	/*
1234789Sahrens 	 * Check accessibility of directory.
1235789Sahrens 	 */
1236789Sahrens 
12375331Samw 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1238789Sahrens 		ZFS_EXIT(zfsvfs);
1239789Sahrens 		return (error);
1240789Sahrens 	}
1241789Sahrens 
12425498Stimh 	if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
12435331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
12445331Samw 		ZFS_EXIT(zfsvfs);
12455331Samw 		return (EILSEQ);
12465331Samw 	}
12475331Samw 
12485331Samw 	error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
12499981STim.Haley@Sun.COM 	if (error == 0)
12509981STim.Haley@Sun.COM 		error = specvp_check(vpp, cr);
1251789Sahrens 
1252789Sahrens 	ZFS_EXIT(zfsvfs);
1253789Sahrens 	return (error);
1254789Sahrens }
1255789Sahrens 
1256789Sahrens /*
1257789Sahrens  * Attempt to create a new entry in a directory.  If the entry
1258789Sahrens  * already exists, truncate the file if permissible, else return
1259789Sahrens  * an error.  Return the vp of the created or trunc'd file.
1260789Sahrens  *
1261789Sahrens  *	IN:	dvp	- vnode of directory to put new file entry in.
1262789Sahrens  *		name	- name of new file entry.
1263789Sahrens  *		vap	- attributes of new file.
1264789Sahrens  *		excl	- flag indicating exclusive or non-exclusive mode.
1265789Sahrens  *		mode	- mode to open file with.
1266789Sahrens  *		cr	- credentials of caller.
1267789Sahrens  *		flag	- large file flag [UNUSED].
12685331Samw  *		ct	- caller context
12695331Samw  *		vsecp 	- ACL to be set
1270789Sahrens  *
1271789Sahrens  *	OUT:	vpp	- vnode of created or trunc'd entry.
1272789Sahrens  *
1273789Sahrens  *	RETURN:	0 if success
1274789Sahrens  *		error code if failure
1275789Sahrens  *
1276789Sahrens  * Timestamps:
1277789Sahrens  *	dvp - ctime|mtime updated if new entry created
1278789Sahrens  *	 vp - ctime|mtime always, atime if new
1279789Sahrens  */
12805331Samw 
1281789Sahrens /* ARGSUSED */
1282789Sahrens static int
1283789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
12845331Samw     int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
12855331Samw     vsecattr_t *vsecp)
1286789Sahrens {
1287789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1288789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
12895326Sek110237 	zilog_t		*zilog;
12905326Sek110237 	objset_t	*os;
1291789Sahrens 	zfs_dirlock_t	*dl;
1292789Sahrens 	dmu_tx_t	*tx;
1293789Sahrens 	int		error;
12947847SMark.Shellenbaum@Sun.COM 	ksid_t		*ksid;
12957847SMark.Shellenbaum@Sun.COM 	uid_t		uid;
12967847SMark.Shellenbaum@Sun.COM 	gid_t		gid = crgetgid(cr);
129711935SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t   acl_ids;
12989179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
1299*12302SMark.Shellenbaum@Sun.COM 	boolean_t	have_acl = B_FALSE;
13005331Samw 
13015331Samw 	/*
13025331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
13035331Samw 	 * make sure file system is at proper version
13045331Samw 	 */
13055331Samw 
13067847SMark.Shellenbaum@Sun.COM 	ksid = crgetsid(cr, KSID_OWNER);
13077847SMark.Shellenbaum@Sun.COM 	if (ksid)
13087847SMark.Shellenbaum@Sun.COM 		uid = ksid_getid(ksid);
13097847SMark.Shellenbaum@Sun.COM 	else
13107847SMark.Shellenbaum@Sun.COM 		uid = crgetuid(cr);
13117847SMark.Shellenbaum@Sun.COM 
13125331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
13135331Samw 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
13147847SMark.Shellenbaum@Sun.COM 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
13155331Samw 		return (EINVAL);
1316789Sahrens 
13175367Sahrens 	ZFS_ENTER(zfsvfs);
13185367Sahrens 	ZFS_VERIFY_ZP(dzp);
13195326Sek110237 	os = zfsvfs->z_os;
13205326Sek110237 	zilog = zfsvfs->z_log;
1321789Sahrens 
13225498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
13235331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
13245331Samw 		ZFS_EXIT(zfsvfs);
13255331Samw 		return (EILSEQ);
13265331Samw 	}
13275331Samw 
13285331Samw 	if (vap->va_mask & AT_XVATTR) {
13295331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
13305331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
13315331Samw 			ZFS_EXIT(zfsvfs);
13325331Samw 			return (error);
13335331Samw 		}
13345331Samw 	}
1335789Sahrens top:
1336789Sahrens 	*vpp = NULL;
1337789Sahrens 
1338789Sahrens 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1339789Sahrens 		vap->va_mode &= ~VSVTX;
1340789Sahrens 
1341789Sahrens 	if (*name == '\0') {
1342789Sahrens 		/*
1343789Sahrens 		 * Null component name refers to the directory itself.
1344789Sahrens 		 */
1345789Sahrens 		VN_HOLD(dvp);
1346789Sahrens 		zp = dzp;
1347789Sahrens 		dl = NULL;
1348789Sahrens 		error = 0;
1349789Sahrens 	} else {
1350789Sahrens 		/* possible VN_HOLD(zp) */
13515331Samw 		int zflg = 0;
13525331Samw 
13535331Samw 		if (flag & FIGNORECASE)
13545331Samw 			zflg |= ZCILOOK;
13555331Samw 
13565331Samw 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
13575331Samw 		    NULL, NULL);
13585331Samw 		if (error) {
1359789Sahrens 			if (strcmp(name, "..") == 0)
1360789Sahrens 				error = EISDIR;
1361789Sahrens 			ZFS_EXIT(zfsvfs);
1362789Sahrens 			return (error);
1363789Sahrens 		}
1364789Sahrens 	}
136511935SMark.Shellenbaum@Sun.COM 
1366789Sahrens 	if (zp == NULL) {
13675331Samw 		uint64_t txtype;
13685331Samw 
1369789Sahrens 		/*
1370789Sahrens 		 * Create a new file object and update the directory
1371789Sahrens 		 * to reference it.
1372789Sahrens 		 */
13735331Samw 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1374789Sahrens 			goto out;
1375789Sahrens 		}
1376789Sahrens 
1377789Sahrens 		/*
1378789Sahrens 		 * We only support the creation of regular files in
1379789Sahrens 		 * extended attribute directories.
1380789Sahrens 		 */
138111935SMark.Shellenbaum@Sun.COM 
138211935SMark.Shellenbaum@Sun.COM 		if ((dzp->z_pflags & ZFS_XATTR) &&
1383789Sahrens 		    (vap->va_type != VREG)) {
1384789Sahrens 			error = EINVAL;
1385789Sahrens 			goto out;
1386789Sahrens 		}
1387789Sahrens 
1388*12302SMark.Shellenbaum@Sun.COM 		if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1389*12302SMark.Shellenbaum@Sun.COM 		    cr, vsecp, &acl_ids)) != 0)
13909179SMark.Shellenbaum@Sun.COM 			goto out;
1391*12302SMark.Shellenbaum@Sun.COM 		have_acl = B_TRUE;
1392*12302SMark.Shellenbaum@Sun.COM 
13939396SMatthew.Ahrens@Sun.COM 		if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
139410143STim.Haley@Sun.COM 			zfs_acl_ids_free(&acl_ids);
13959396SMatthew.Ahrens@Sun.COM 			error = EDQUOT;
13969396SMatthew.Ahrens@Sun.COM 			goto out;
13979396SMatthew.Ahrens@Sun.COM 		}
13989179SMark.Shellenbaum@Sun.COM 
1399789Sahrens 		tx = dmu_tx_create(os);
140011935SMark.Shellenbaum@Sun.COM 
140111935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
140211935SMark.Shellenbaum@Sun.COM 		    ZFS_SA_BASE_ATTR_SIZE);
140311935SMark.Shellenbaum@Sun.COM 
14049179SMark.Shellenbaum@Sun.COM 		fuid_dirtied = zfsvfs->z_fuid_dirty;
14059396SMatthew.Ahrens@Sun.COM 		if (fuid_dirtied)
14069396SMatthew.Ahrens@Sun.COM 			zfs_fuid_txhold(zfsvfs, tx);
14071544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
140811935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
140911935SMark.Shellenbaum@Sun.COM 		if (!zfsvfs->z_use_sa &&
141011935SMark.Shellenbaum@Sun.COM 		    acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1411789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
141211935SMark.Shellenbaum@Sun.COM 			    0, acl_ids.z_aclp->z_acl_bytes);
14135331Samw 		}
14148227SNeil.Perrin@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
1415789Sahrens 		if (error) {
1416789Sahrens 			zfs_dirent_unlock(dl);
14178227SNeil.Perrin@Sun.COM 			if (error == ERESTART) {
14182113Sahrens 				dmu_tx_wait(tx);
14192113Sahrens 				dmu_tx_abort(tx);
1420789Sahrens 				goto top;
1421789Sahrens 			}
1422*12302SMark.Shellenbaum@Sun.COM 			zfs_acl_ids_free(&acl_ids);
14232113Sahrens 			dmu_tx_abort(tx);
1424789Sahrens 			ZFS_EXIT(zfsvfs);
1425789Sahrens 			return (error);
1426789Sahrens 		}
142711935SMark.Shellenbaum@Sun.COM 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
14289179SMark.Shellenbaum@Sun.COM 
14299179SMark.Shellenbaum@Sun.COM 		if (fuid_dirtied)
14309179SMark.Shellenbaum@Sun.COM 			zfs_fuid_sync(zfsvfs, tx);
14319179SMark.Shellenbaum@Sun.COM 
1432789Sahrens 		(void) zfs_link_create(dl, zp, tx, ZNEW);
14335331Samw 		txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
14345331Samw 		if (flag & FIGNORECASE)
14355331Samw 			txtype |= TX_CI;
14365331Samw 		zfs_log_create(zilog, tx, txtype, dzp, zp, name,
14379179SMark.Shellenbaum@Sun.COM 		    vsecp, acl_ids.z_fuidp, vap);
14389179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
1439789Sahrens 		dmu_tx_commit(tx);
1440789Sahrens 	} else {
14415331Samw 		int aflags = (flag & FAPPEND) ? V_APPEND : 0;
14425331Samw 
1443789Sahrens 		/*
1444789Sahrens 		 * A directory entry already exists for this name.
1445789Sahrens 		 */
1446789Sahrens 		/*
1447789Sahrens 		 * Can't truncate an existing file if in exclusive mode.
1448789Sahrens 		 */
1449789Sahrens 		if (excl == EXCL) {
1450789Sahrens 			error = EEXIST;
1451789Sahrens 			goto out;
1452789Sahrens 		}
1453789Sahrens 		/*
1454789Sahrens 		 * Can't open a directory for writing.
1455789Sahrens 		 */
1456789Sahrens 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1457789Sahrens 			error = EISDIR;
1458789Sahrens 			goto out;
1459789Sahrens 		}
1460789Sahrens 		/*
1461789Sahrens 		 * Verify requested access to file.
1462789Sahrens 		 */
14635331Samw 		if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1464789Sahrens 			goto out;
1465789Sahrens 		}
1466789Sahrens 
1467789Sahrens 		mutex_enter(&dzp->z_lock);
1468789Sahrens 		dzp->z_seq++;
1469789Sahrens 		mutex_exit(&dzp->z_lock);
1470789Sahrens 
14711878Smaybee 		/*
14721878Smaybee 		 * Truncate regular files if requested.
14731878Smaybee 		 */
14741878Smaybee 		if ((ZTOV(zp)->v_type == VREG) &&
1475789Sahrens 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
14766992Smaybee 			/* we can't hold any locks when calling zfs_freesp() */
14776992Smaybee 			zfs_dirent_unlock(dl);
14786992Smaybee 			dl = NULL;
14791878Smaybee 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
14804863Spraks 			if (error == 0) {
14815331Samw 				vnevent_create(ZTOV(zp), ct);
14824863Spraks 			}
1483789Sahrens 		}
1484789Sahrens 	}
1485789Sahrens out:
1486789Sahrens 
1487789Sahrens 	if (dl)
1488789Sahrens 		zfs_dirent_unlock(dl);
1489789Sahrens 
1490789Sahrens 	if (error) {
1491789Sahrens 		if (zp)
1492789Sahrens 			VN_RELE(ZTOV(zp));
1493789Sahrens 	} else {
1494789Sahrens 		*vpp = ZTOV(zp);
14959981STim.Haley@Sun.COM 		error = specvp_check(vpp, cr);
1496789Sahrens 	}
1497789Sahrens 
149812294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
149912294SMark.Musante@Sun.COM 		zil_commit(zilog, UINT64_MAX, 0);
150012294SMark.Musante@Sun.COM 
1501789Sahrens 	ZFS_EXIT(zfsvfs);
1502789Sahrens 	return (error);
1503789Sahrens }
1504789Sahrens 
1505789Sahrens /*
1506789Sahrens  * Remove an entry from a directory.
1507789Sahrens  *
1508789Sahrens  *	IN:	dvp	- vnode of directory to remove entry from.
1509789Sahrens  *		name	- name of entry to remove.
1510789Sahrens  *		cr	- credentials of caller.
15115331Samw  *		ct	- caller context
15125331Samw  *		flags	- case flags
1513789Sahrens  *
1514789Sahrens  *	RETURN:	0 if success
1515789Sahrens  *		error code if failure
1516789Sahrens  *
1517789Sahrens  * Timestamps:
1518789Sahrens  *	dvp - ctime|mtime
1519789Sahrens  *	 vp - ctime (if nlink > 0)
1520789Sahrens  */
152111935SMark.Shellenbaum@Sun.COM 
152211935SMark.Shellenbaum@Sun.COM uint64_t null_xattr = 0;
152311935SMark.Shellenbaum@Sun.COM 
15245331Samw /*ARGSUSED*/
1525789Sahrens static int
15265331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
15275331Samw     int flags)
1528789Sahrens {
1529789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1530789Sahrens 	znode_t		*xzp = NULL;
1531789Sahrens 	vnode_t		*vp;
1532789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
15335326Sek110237 	zilog_t		*zilog;
153411935SMark.Shellenbaum@Sun.COM 	uint64_t	acl_obj, xattr_obj = 0;
153511935SMark.Shellenbaum@Sun.COM 	uint64_t 	xattr_obj_unlinked = 0;
1536789Sahrens 	zfs_dirlock_t	*dl;
1537789Sahrens 	dmu_tx_t	*tx;
15383461Sahrens 	boolean_t	may_delete_now, delete_now = FALSE;
15396992Smaybee 	boolean_t	unlinked, toobig = FALSE;
15405331Samw 	uint64_t	txtype;
15415331Samw 	pathname_t	*realnmp = NULL;
15425331Samw 	pathname_t	realnm;
1543789Sahrens 	int		error;
15445331Samw 	int		zflg = ZEXISTS;
1545789Sahrens 
15465367Sahrens 	ZFS_ENTER(zfsvfs);
15475367Sahrens 	ZFS_VERIFY_ZP(dzp);
15485326Sek110237 	zilog = zfsvfs->z_log;
1549789Sahrens 
15505331Samw 	if (flags & FIGNORECASE) {
15515331Samw 		zflg |= ZCILOOK;
15525331Samw 		pn_alloc(&realnm);
15535331Samw 		realnmp = &realnm;
15545331Samw 	}
15555331Samw 
1556789Sahrens top:
1557789Sahrens 	/*
1558789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1559789Sahrens 	 */
15605331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
15615331Samw 	    NULL, realnmp)) {
15625331Samw 		if (realnmp)
15635331Samw 			pn_free(realnmp);
1564789Sahrens 		ZFS_EXIT(zfsvfs);
1565789Sahrens 		return (error);
1566789Sahrens 	}
1567789Sahrens 
1568789Sahrens 	vp = ZTOV(zp);
1569789Sahrens 
1570789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1571789Sahrens 		goto out;
1572789Sahrens 	}
1573789Sahrens 
1574789Sahrens 	/*
1575789Sahrens 	 * Need to use rmdir for removing directories.
1576789Sahrens 	 */
1577789Sahrens 	if (vp->v_type == VDIR) {
1578789Sahrens 		error = EPERM;
1579789Sahrens 		goto out;
1580789Sahrens 	}
1581789Sahrens 
15825331Samw 	vnevent_remove(vp, dvp, name, ct);
15835331Samw 
15845331Samw 	if (realnmp)
15856492Stimh 		dnlc_remove(dvp, realnmp->pn_buf);
15865331Samw 	else
15875331Samw 		dnlc_remove(dvp, name);
15881484Sek110237 
1589789Sahrens 	mutex_enter(&vp->v_lock);
1590789Sahrens 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1591789Sahrens 	mutex_exit(&vp->v_lock);
1592789Sahrens 
1593789Sahrens 	/*
15943461Sahrens 	 * We may delete the znode now, or we may put it in the unlinked set;
1595789Sahrens 	 * it depends on whether we're the last link, and on whether there are
1596789Sahrens 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1597789Sahrens 	 * allow for either case.
1598789Sahrens 	 */
1599789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
16001544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
160111935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
160211935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
160311935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, dzp);
16046992Smaybee 	if (may_delete_now) {
16056992Smaybee 		toobig =
160611935SMark.Shellenbaum@Sun.COM 		    zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
16076992Smaybee 		/* if the file is too big, only hold_free a token amount */
16086992Smaybee 		dmu_tx_hold_free(tx, zp->z_id, 0,
16096992Smaybee 		    (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
16106992Smaybee 	}
1611789Sahrens 
1612789Sahrens 	/* are there any extended attributes? */
161311935SMark.Shellenbaum@Sun.COM 	error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
161411935SMark.Shellenbaum@Sun.COM 	    &xattr_obj, sizeof (xattr_obj));
161511935SMark.Shellenbaum@Sun.COM 	if (xattr_obj) {
161611935SMark.Shellenbaum@Sun.COM 		error = zfs_zget(zfsvfs, xattr_obj, &xzp);
161711935SMark.Shellenbaum@Sun.COM 		ASSERT3U(error, ==, 0);
161811935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
161911935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1620789Sahrens 	}
1621789Sahrens 
1622789Sahrens 	/* are there any additional acls */
162311935SMark.Shellenbaum@Sun.COM 	if ((acl_obj = ZFS_EXTERNAL_ACL(zp)) != 0 && may_delete_now)
1624789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1625789Sahrens 
1626789Sahrens 	/* charge as an update -- would be nice not to charge at all */
16273461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1628789Sahrens 
16298227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1630789Sahrens 	if (error) {
1631789Sahrens 		zfs_dirent_unlock(dl);
1632789Sahrens 		VN_RELE(vp);
16338227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
16342113Sahrens 			dmu_tx_wait(tx);
16352113Sahrens 			dmu_tx_abort(tx);
1636789Sahrens 			goto top;
1637789Sahrens 		}
16385331Samw 		if (realnmp)
16395331Samw 			pn_free(realnmp);
16402113Sahrens 		dmu_tx_abort(tx);
1641789Sahrens 		ZFS_EXIT(zfsvfs);
1642789Sahrens 		return (error);
1643789Sahrens 	}
1644789Sahrens 
1645789Sahrens 	/*
1646789Sahrens 	 * Remove the directory entry.
1647789Sahrens 	 */
16485331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1649789Sahrens 
1650789Sahrens 	if (error) {
1651789Sahrens 		dmu_tx_commit(tx);
1652789Sahrens 		goto out;
1653789Sahrens 	}
1654789Sahrens 
16553461Sahrens 	if (unlinked) {
165611935SMark.Shellenbaum@Sun.COM 
1657789Sahrens 		mutex_enter(&vp->v_lock);
165811935SMark.Shellenbaum@Sun.COM 
165911935SMark.Shellenbaum@Sun.COM 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
166011935SMark.Shellenbaum@Sun.COM 		    &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
16616992Smaybee 		delete_now = may_delete_now && !toobig &&
1662789Sahrens 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
166311935SMark.Shellenbaum@Sun.COM 		    xattr_obj == xattr_obj_unlinked && ZFS_EXTERNAL_ACL(zp) ==
166411935SMark.Shellenbaum@Sun.COM 		    acl_obj;
1665789Sahrens 		mutex_exit(&vp->v_lock);
1666789Sahrens 	}
1667789Sahrens 
1668789Sahrens 	if (delete_now) {
166911935SMark.Shellenbaum@Sun.COM 		if (xattr_obj_unlinked) {
167011935SMark.Shellenbaum@Sun.COM 			ASSERT3U(xzp->z_links, ==, 2);
1671789Sahrens 			mutex_enter(&xzp->z_lock);
16723461Sahrens 			xzp->z_unlinked = 1;
167311935SMark.Shellenbaum@Sun.COM 			xzp->z_links = 0;
167411935SMark.Shellenbaum@Sun.COM 			error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
167511935SMark.Shellenbaum@Sun.COM 			    &xzp->z_links, sizeof (xzp->z_links), tx);
167611935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error,  ==,  0);
1677789Sahrens 			mutex_exit(&xzp->z_lock);
16783461Sahrens 			zfs_unlinked_add(xzp, tx);
167911935SMark.Shellenbaum@Sun.COM 			if (zp->z_is_sa)
168011935SMark.Shellenbaum@Sun.COM 				error = sa_remove(zp->z_sa_hdl,
168111935SMark.Shellenbaum@Sun.COM 				    SA_ZPL_XATTR(zfsvfs), tx);
168211935SMark.Shellenbaum@Sun.COM 			else
168311935SMark.Shellenbaum@Sun.COM 				error = sa_update(zp->z_sa_hdl,
168411935SMark.Shellenbaum@Sun.COM 				    SA_ZPL_XATTR(zfsvfs), &null_xattr,
168511935SMark.Shellenbaum@Sun.COM 				    sizeof (uint64_t), tx);
168611935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error, ==, 0);
1687789Sahrens 		}
1688789Sahrens 		mutex_enter(&zp->z_lock);
1689789Sahrens 		mutex_enter(&vp->v_lock);
1690789Sahrens 		vp->v_count--;
1691789Sahrens 		ASSERT3U(vp->v_count, ==, 0);
1692789Sahrens 		mutex_exit(&vp->v_lock);
1693789Sahrens 		mutex_exit(&zp->z_lock);
1694789Sahrens 		zfs_znode_delete(zp, tx);
16953461Sahrens 	} else if (unlinked) {
16963461Sahrens 		zfs_unlinked_add(zp, tx);
1697789Sahrens 	}
1698789Sahrens 
16995331Samw 	txtype = TX_REMOVE;
17005331Samw 	if (flags & FIGNORECASE)
17015331Samw 		txtype |= TX_CI;
17025331Samw 	zfs_log_remove(zilog, tx, txtype, dzp, name);
1703789Sahrens 
1704789Sahrens 	dmu_tx_commit(tx);
1705789Sahrens out:
17065331Samw 	if (realnmp)
17075331Samw 		pn_free(realnmp);
17085331Samw 
1709789Sahrens 	zfs_dirent_unlock(dl);
1710789Sahrens 
171112178SMark.Shellenbaum@Sun.COM 	if (!delete_now)
1712789Sahrens 		VN_RELE(vp);
171312178SMark.Shellenbaum@Sun.COM 	if (xzp)
1714789Sahrens 		VN_RELE(ZTOV(xzp));
1715789Sahrens 
171612294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
171712294SMark.Musante@Sun.COM 		zil_commit(zilog, UINT64_MAX, 0);
171812294SMark.Musante@Sun.COM 
1719789Sahrens 	ZFS_EXIT(zfsvfs);
1720789Sahrens 	return (error);
1721789Sahrens }
1722789Sahrens 
1723789Sahrens /*
1724789Sahrens  * Create a new directory and insert it into dvp using the name
1725789Sahrens  * provided.  Return a pointer to the inserted directory.
1726789Sahrens  *
1727789Sahrens  *	IN:	dvp	- vnode of directory to add subdir to.
1728789Sahrens  *		dirname	- name of new directory.
1729789Sahrens  *		vap	- attributes of new directory.
1730789Sahrens  *		cr	- credentials of caller.
17315331Samw  *		ct	- caller context
17325331Samw  *		vsecp	- ACL to be set
1733789Sahrens  *
1734789Sahrens  *	OUT:	vpp	- vnode of created directory.
1735789Sahrens  *
1736789Sahrens  *	RETURN:	0 if success
1737789Sahrens  *		error code if failure
1738789Sahrens  *
1739789Sahrens  * Timestamps:
1740789Sahrens  *	dvp - ctime|mtime updated
1741789Sahrens  *	 vp - ctime|mtime|atime updated
1742789Sahrens  */
17435331Samw /*ARGSUSED*/
1744789Sahrens static int
17455331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
17465331Samw     caller_context_t *ct, int flags, vsecattr_t *vsecp)
1747789Sahrens {
1748789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1749789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
17505326Sek110237 	zilog_t		*zilog;
1751789Sahrens 	zfs_dirlock_t	*dl;
17525331Samw 	uint64_t	txtype;
1753789Sahrens 	dmu_tx_t	*tx;
1754789Sahrens 	int		error;
17555331Samw 	int		zf = ZNEW;
17567847SMark.Shellenbaum@Sun.COM 	ksid_t		*ksid;
17577847SMark.Shellenbaum@Sun.COM 	uid_t		uid;
17587847SMark.Shellenbaum@Sun.COM 	gid_t		gid = crgetgid(cr);
175911935SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t   acl_ids;
17609179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
1761789Sahrens 
1762789Sahrens 	ASSERT(vap->va_type == VDIR);
1763789Sahrens 
17645331Samw 	/*
17655331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
17665331Samw 	 * make sure file system is at proper version
17675331Samw 	 */
17685331Samw 
17697847SMark.Shellenbaum@Sun.COM 	ksid = crgetsid(cr, KSID_OWNER);
17707847SMark.Shellenbaum@Sun.COM 	if (ksid)
17717847SMark.Shellenbaum@Sun.COM 		uid = ksid_getid(ksid);
17727847SMark.Shellenbaum@Sun.COM 	else
17737847SMark.Shellenbaum@Sun.COM 		uid = crgetuid(cr);
17745331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
17757847SMark.Shellenbaum@Sun.COM 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
17767876SMark.Shellenbaum@Sun.COM 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
17775331Samw 		return (EINVAL);
17785331Samw 
17795367Sahrens 	ZFS_ENTER(zfsvfs);
17805367Sahrens 	ZFS_VERIFY_ZP(dzp);
17815326Sek110237 	zilog = zfsvfs->z_log;
1782789Sahrens 
178311935SMark.Shellenbaum@Sun.COM 	if (dzp->z_pflags & ZFS_XATTR) {
1784789Sahrens 		ZFS_EXIT(zfsvfs);
1785789Sahrens 		return (EINVAL);
1786789Sahrens 	}
17875331Samw 
17885498Stimh 	if (zfsvfs->z_utf8 && u8_validate(dirname,
17895331Samw 	    strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
17905331Samw 		ZFS_EXIT(zfsvfs);
17915331Samw 		return (EILSEQ);
17925331Samw 	}
17935331Samw 	if (flags & FIGNORECASE)
17945331Samw 		zf |= ZCILOOK;
17955331Samw 
1796*12302SMark.Shellenbaum@Sun.COM 	if (vap->va_mask & AT_XVATTR) {
17975331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
17985331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
17995331Samw 			ZFS_EXIT(zfsvfs);
18005331Samw 			return (error);
18015331Samw 		}
1802*12302SMark.Shellenbaum@Sun.COM 	}
1803*12302SMark.Shellenbaum@Sun.COM 
1804*12302SMark.Shellenbaum@Sun.COM 	if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1805*12302SMark.Shellenbaum@Sun.COM 	    vsecp, &acl_ids)) != 0) {
1806*12302SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
1807*12302SMark.Shellenbaum@Sun.COM 		return (error);
1808*12302SMark.Shellenbaum@Sun.COM 	}
1809789Sahrens 	/*
1810789Sahrens 	 * First make sure the new directory doesn't exist.
1811*12302SMark.Shellenbaum@Sun.COM 	 *
1812*12302SMark.Shellenbaum@Sun.COM 	 * Existence is checked first to make sure we don't return
1813*12302SMark.Shellenbaum@Sun.COM 	 * EACCES instead of EEXIST which can cause some applications
1814*12302SMark.Shellenbaum@Sun.COM 	 * to fail.
1815789Sahrens 	 */
18165331Samw top:
18175331Samw 	*vpp = NULL;
18185331Samw 
18195331Samw 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
18205331Samw 	    NULL, NULL)) {
1821*12302SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
1822789Sahrens 		ZFS_EXIT(zfsvfs);
1823789Sahrens 		return (error);
1824789Sahrens 	}
1825789Sahrens 
18265331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
1827*12302SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
18281231Smarks 		zfs_dirent_unlock(dl);
18291231Smarks 		ZFS_EXIT(zfsvfs);
18301231Smarks 		return (error);
18311231Smarks 	}
18321231Smarks 
18339396SMatthew.Ahrens@Sun.COM 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
183410143STim.Haley@Sun.COM 		zfs_acl_ids_free(&acl_ids);
18359396SMatthew.Ahrens@Sun.COM 		zfs_dirent_unlock(dl);
18369396SMatthew.Ahrens@Sun.COM 		ZFS_EXIT(zfsvfs);
18379396SMatthew.Ahrens@Sun.COM 		return (EDQUOT);
18389396SMatthew.Ahrens@Sun.COM 	}
18399179SMark.Shellenbaum@Sun.COM 
1840789Sahrens 	/*
1841789Sahrens 	 * Add a new entry to the directory.
1842789Sahrens 	 */
1843789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
18441544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
18451544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
18469179SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
18479396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
18489396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
184911935SMark.Shellenbaum@Sun.COM 	if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
185011935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
185111935SMark.Shellenbaum@Sun.COM 		    acl_ids.z_aclp->z_acl_bytes);
185211935SMark.Shellenbaum@Sun.COM 	}
185311935SMark.Shellenbaum@Sun.COM 
185411935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
185511935SMark.Shellenbaum@Sun.COM 	    ZFS_SA_BASE_ATTR_SIZE);
185611935SMark.Shellenbaum@Sun.COM 
18578227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1858789Sahrens 	if (error) {
1859789Sahrens 		zfs_dirent_unlock(dl);
18608227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
18612113Sahrens 			dmu_tx_wait(tx);
18622113Sahrens 			dmu_tx_abort(tx);
1863789Sahrens 			goto top;
1864789Sahrens 		}
1865*12302SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
18662113Sahrens 		dmu_tx_abort(tx);
1867789Sahrens 		ZFS_EXIT(zfsvfs);
1868789Sahrens 		return (error);
1869789Sahrens 	}
1870789Sahrens 
1871789Sahrens 	/*
1872789Sahrens 	 * Create new node.
1873789Sahrens 	 */
187411935SMark.Shellenbaum@Sun.COM 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
18759179SMark.Shellenbaum@Sun.COM 
18769179SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
18779179SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
187811935SMark.Shellenbaum@Sun.COM 
1879789Sahrens 	/*
1880789Sahrens 	 * Now put new name in parent dir.
1881789Sahrens 	 */
1882789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1883789Sahrens 
1884789Sahrens 	*vpp = ZTOV(zp);
1885789Sahrens 
18865331Samw 	txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
18875331Samw 	if (flags & FIGNORECASE)
18885331Samw 		txtype |= TX_CI;
18899179SMark.Shellenbaum@Sun.COM 	zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
18909179SMark.Shellenbaum@Sun.COM 	    acl_ids.z_fuidp, vap);
18919179SMark.Shellenbaum@Sun.COM 
18929179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_free(&acl_ids);
189311935SMark.Shellenbaum@Sun.COM 
1894789Sahrens 	dmu_tx_commit(tx);
1895789Sahrens 
1896789Sahrens 	zfs_dirent_unlock(dl);
1897789Sahrens 
189812294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
189912294SMark.Musante@Sun.COM 		zil_commit(zilog, UINT64_MAX, 0);
190012294SMark.Musante@Sun.COM 
1901789Sahrens 	ZFS_EXIT(zfsvfs);
1902789Sahrens 	return (0);
1903789Sahrens }
1904789Sahrens 
1905789Sahrens /*
1906789Sahrens  * Remove a directory subdir entry.  If the current working
1907789Sahrens  * directory is the same as the subdir to be removed, the
1908789Sahrens  * remove will fail.
1909789Sahrens  *
1910789Sahrens  *	IN:	dvp	- vnode of directory to remove from.
1911789Sahrens  *		name	- name of directory to be removed.
1912789Sahrens  *		cwd	- vnode of current working directory.
1913789Sahrens  *		cr	- credentials of caller.
19145331Samw  *		ct	- caller context
19155331Samw  *		flags	- case flags
1916789Sahrens  *
1917789Sahrens  *	RETURN:	0 if success
1918789Sahrens  *		error code if failure
1919789Sahrens  *
1920789Sahrens  * Timestamps:
1921789Sahrens  *	dvp - ctime|mtime updated
1922789Sahrens  */
19235331Samw /*ARGSUSED*/
1924789Sahrens static int
19255331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
19265331Samw     caller_context_t *ct, int flags)
1927789Sahrens {
1928789Sahrens 	znode_t		*dzp = VTOZ(dvp);
1929789Sahrens 	znode_t		*zp;
1930789Sahrens 	vnode_t		*vp;
1931789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
19325326Sek110237 	zilog_t		*zilog;
1933789Sahrens 	zfs_dirlock_t	*dl;
1934789Sahrens 	dmu_tx_t	*tx;
1935789Sahrens 	int		error;
19365331Samw 	int		zflg = ZEXISTS;
1937789Sahrens 
19385367Sahrens 	ZFS_ENTER(zfsvfs);
19395367Sahrens 	ZFS_VERIFY_ZP(dzp);
19405326Sek110237 	zilog = zfsvfs->z_log;
1941789Sahrens 
19425331Samw 	if (flags & FIGNORECASE)
19435331Samw 		zflg |= ZCILOOK;
1944789Sahrens top:
1945789Sahrens 	zp = NULL;
1946789Sahrens 
1947789Sahrens 	/*
1948789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1949789Sahrens 	 */
19505331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
19515331Samw 	    NULL, NULL)) {
1952789Sahrens 		ZFS_EXIT(zfsvfs);
1953789Sahrens 		return (error);
1954789Sahrens 	}
1955789Sahrens 
1956789Sahrens 	vp = ZTOV(zp);
1957789Sahrens 
1958789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1959789Sahrens 		goto out;
1960789Sahrens 	}
1961789Sahrens 
1962789Sahrens 	if (vp->v_type != VDIR) {
1963789Sahrens 		error = ENOTDIR;
1964789Sahrens 		goto out;
1965789Sahrens 	}
1966789Sahrens 
1967789Sahrens 	if (vp == cwd) {
1968789Sahrens 		error = EINVAL;
1969789Sahrens 		goto out;
1970789Sahrens 	}
1971789Sahrens 
19725331Samw 	vnevent_rmdir(vp, dvp, name, ct);
1973789Sahrens 
1974789Sahrens 	/*
19753897Smaybee 	 * Grab a lock on the directory to make sure that noone is
19763897Smaybee 	 * trying to add (or lookup) entries while we are removing it.
19773897Smaybee 	 */
19783897Smaybee 	rw_enter(&zp->z_name_lock, RW_WRITER);
19793897Smaybee 
19803897Smaybee 	/*
19813897Smaybee 	 * Grab a lock on the parent pointer to make sure we play well
1982789Sahrens 	 * with the treewalk and directory rename code.
1983789Sahrens 	 */
1984789Sahrens 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1985789Sahrens 
1986789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
19871544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
198811935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
19893461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
199011935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
199111935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, dzp);
19928227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1993789Sahrens 	if (error) {
1994789Sahrens 		rw_exit(&zp->z_parent_lock);
19953897Smaybee 		rw_exit(&zp->z_name_lock);
1996789Sahrens 		zfs_dirent_unlock(dl);
1997789Sahrens 		VN_RELE(vp);
19988227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
19992113Sahrens 			dmu_tx_wait(tx);
20002113Sahrens 			dmu_tx_abort(tx);
2001789Sahrens 			goto top;
2002789Sahrens 		}
20032113Sahrens 		dmu_tx_abort(tx);
2004789Sahrens 		ZFS_EXIT(zfsvfs);
2005789Sahrens 		return (error);
2006789Sahrens 	}
2007789Sahrens 
20085331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
20095331Samw 
20105331Samw 	if (error == 0) {
20115331Samw 		uint64_t txtype = TX_RMDIR;
20125331Samw 		if (flags & FIGNORECASE)
20135331Samw 			txtype |= TX_CI;
20145331Samw 		zfs_log_remove(zilog, tx, txtype, dzp, name);
20155331Samw 	}
2016789Sahrens 
2017789Sahrens 	dmu_tx_commit(tx);
2018789Sahrens 
2019789Sahrens 	rw_exit(&zp->z_parent_lock);
20203897Smaybee 	rw_exit(&zp->z_name_lock);
2021789Sahrens out:
2022789Sahrens 	zfs_dirent_unlock(dl);
2023789Sahrens 
2024789Sahrens 	VN_RELE(vp);
2025789Sahrens 
202612294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
202712294SMark.Musante@Sun.COM 		zil_commit(zilog, UINT64_MAX, 0);
202812294SMark.Musante@Sun.COM 
2029789Sahrens 	ZFS_EXIT(zfsvfs);
2030789Sahrens 	return (error);
2031789Sahrens }
2032789Sahrens 
2033789Sahrens /*
2034789Sahrens  * Read as many directory entries as will fit into the provided
2035789Sahrens  * buffer from the given directory cursor position (specified in
2036789Sahrens  * the uio structure.
2037789Sahrens  *
2038789Sahrens  *	IN:	vp	- vnode of directory to read.
2039789Sahrens  *		uio	- structure supplying read location, range info,
2040789Sahrens  *			  and return buffer.
2041789Sahrens  *		cr	- credentials of caller.
20425331Samw  *		ct	- caller context
20435331Samw  *		flags	- case flags
2044789Sahrens  *
2045789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
2046789Sahrens  *		eofp	- set to true if end-of-file detected.
2047789Sahrens  *
2048789Sahrens  *	RETURN:	0 if success
2049789Sahrens  *		error code if failure
2050789Sahrens  *
2051789Sahrens  * Timestamps:
2052789Sahrens  *	vp - atime updated
2053789Sahrens  *
2054789Sahrens  * Note that the low 4 bits of the cookie returned by zap is always zero.
2055789Sahrens  * This allows us to use the low range for "special" directory entries:
2056789Sahrens  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
2057789Sahrens  * we use the offset 2 for the '.zfs' directory.
2058789Sahrens  */
2059789Sahrens /* ARGSUSED */
2060789Sahrens static int
20615331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
20625331Samw     caller_context_t *ct, int flags)
2063789Sahrens {
2064789Sahrens 	znode_t		*zp = VTOZ(vp);
2065789Sahrens 	iovec_t		*iovp;
20665331Samw 	edirent_t	*eodp;
2067789Sahrens 	dirent64_t	*odp;
2068789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2069869Sperrin 	objset_t	*os;
2070789Sahrens 	caddr_t		outbuf;
2071789Sahrens 	size_t		bufsize;
2072789Sahrens 	zap_cursor_t	zc;
2073789Sahrens 	zap_attribute_t	zap;
2074789Sahrens 	uint_t		bytes_wanted;
2075789Sahrens 	uint64_t	offset; /* must be unsigned; checks for < 1 */
207611935SMark.Shellenbaum@Sun.COM 	uint64_t	parent;
2077789Sahrens 	int		local_eof;
2078869Sperrin 	int		outcount;
2079869Sperrin 	int		error;
2080869Sperrin 	uint8_t		prefetch;
20815663Sck153898 	boolean_t	check_sysattrs;
2082789Sahrens 
20835367Sahrens 	ZFS_ENTER(zfsvfs);
20845367Sahrens 	ZFS_VERIFY_ZP(zp);
2085789Sahrens 
208611935SMark.Shellenbaum@Sun.COM 	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
208711935SMark.Shellenbaum@Sun.COM 	    &parent, sizeof (parent))) != 0) {
208811935SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
208911935SMark.Shellenbaum@Sun.COM 		return (error);
209011935SMark.Shellenbaum@Sun.COM 	}
209111935SMark.Shellenbaum@Sun.COM 
2092789Sahrens 	/*
2093789Sahrens 	 * If we are not given an eof variable,
2094789Sahrens 	 * use a local one.
2095789Sahrens 	 */
2096789Sahrens 	if (eofp == NULL)
2097789Sahrens 		eofp = &local_eof;
2098789Sahrens 
2099789Sahrens 	/*
2100789Sahrens 	 * Check for valid iov_len.
2101789Sahrens 	 */
2102789Sahrens 	if (uio->uio_iov->iov_len <= 0) {
2103789Sahrens 		ZFS_EXIT(zfsvfs);
2104789Sahrens 		return (EINVAL);
2105789Sahrens 	}
2106789Sahrens 
2107789Sahrens 	/*
2108789Sahrens 	 * Quit if directory has been removed (posix)
2109789Sahrens 	 */
21103461Sahrens 	if ((*eofp = zp->z_unlinked) != 0) {
2111789Sahrens 		ZFS_EXIT(zfsvfs);
2112789Sahrens 		return (0);
2113789Sahrens 	}
2114789Sahrens 
2115869Sperrin 	error = 0;
2116869Sperrin 	os = zfsvfs->z_os;
2117869Sperrin 	offset = uio->uio_loffset;
2118869Sperrin 	prefetch = zp->z_zn_prefetch;
2119869Sperrin 
2120789Sahrens 	/*
2121789Sahrens 	 * Initialize the iterator cursor.
2122789Sahrens 	 */
2123789Sahrens 	if (offset <= 3) {
2124789Sahrens 		/*
2125789Sahrens 		 * Start iteration from the beginning of the directory.
2126789Sahrens 		 */
2127869Sperrin 		zap_cursor_init(&zc, os, zp->z_id);
2128789Sahrens 	} else {
2129789Sahrens 		/*
2130789Sahrens 		 * The offset is a serialized cursor.
2131789Sahrens 		 */
2132869Sperrin 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2133789Sahrens 	}
2134789Sahrens 
2135789Sahrens 	/*
2136789Sahrens 	 * Get space to change directory entries into fs independent format.
2137789Sahrens 	 */
2138789Sahrens 	iovp = uio->uio_iov;
2139789Sahrens 	bytes_wanted = iovp->iov_len;
2140789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2141789Sahrens 		bufsize = bytes_wanted;
2142789Sahrens 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
2143789Sahrens 		odp = (struct dirent64 *)outbuf;
2144789Sahrens 	} else {
2145789Sahrens 		bufsize = bytes_wanted;
2146789Sahrens 		odp = (struct dirent64 *)iovp->iov_base;
2147789Sahrens 	}
21485331Samw 	eodp = (struct edirent *)odp;
2149789Sahrens 
2150789Sahrens 	/*
21517757SJanice.Chang@Sun.COM 	 * If this VFS supports the system attribute view interface; and
21527757SJanice.Chang@Sun.COM 	 * we're looking at an extended attribute directory; and we care
21537757SJanice.Chang@Sun.COM 	 * about normalization conflicts on this vfs; then we must check
21547757SJanice.Chang@Sun.COM 	 * for normalization conflicts with the sysattr name space.
21555663Sck153898 	 */
21567757SJanice.Chang@Sun.COM 	check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
21575663Sck153898 	    (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
21585663Sck153898 	    (flags & V_RDDIR_ENTFLAGS);
21595663Sck153898 
21605663Sck153898 	/*
2161789Sahrens 	 * Transform to file-system independent format
2162789Sahrens 	 */
2163789Sahrens 	outcount = 0;
2164789Sahrens 	while (outcount < bytes_wanted) {
21653912Slling 		ino64_t objnum;
21663912Slling 		ushort_t reclen;
21673912Slling 		off64_t *next;
21683912Slling 
2169789Sahrens 		/*
2170789Sahrens 		 * Special case `.', `..', and `.zfs'.
2171789Sahrens 		 */
2172789Sahrens 		if (offset == 0) {
2173789Sahrens 			(void) strcpy(zap.za_name, ".");
21745331Samw 			zap.za_normalization_conflict = 0;
21753912Slling 			objnum = zp->z_id;
2176789Sahrens 		} else if (offset == 1) {
2177789Sahrens 			(void) strcpy(zap.za_name, "..");
21785331Samw 			zap.za_normalization_conflict = 0;
217911935SMark.Shellenbaum@Sun.COM 			objnum = parent;
2180789Sahrens 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
2181789Sahrens 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
21825331Samw 			zap.za_normalization_conflict = 0;
21833912Slling 			objnum = ZFSCTL_INO_ROOT;
2184789Sahrens 		} else {
2185789Sahrens 			/*
2186789Sahrens 			 * Grab next entry.
2187789Sahrens 			 */
2188789Sahrens 			if (error = zap_cursor_retrieve(&zc, &zap)) {
2189789Sahrens 				if ((*eofp = (error == ENOENT)) != 0)
2190789Sahrens 					break;
2191789Sahrens 				else
2192789Sahrens 					goto update;
2193789Sahrens 			}
2194789Sahrens 
2195789Sahrens 			if (zap.za_integer_length != 8 ||
2196789Sahrens 			    zap.za_num_integers != 1) {
2197789Sahrens 				cmn_err(CE_WARN, "zap_readdir: bad directory "
2198789Sahrens 				    "entry, obj = %lld, offset = %lld\n",
2199789Sahrens 				    (u_longlong_t)zp->z_id,
2200789Sahrens 				    (u_longlong_t)offset);
2201789Sahrens 				error = ENXIO;
2202789Sahrens 				goto update;
2203789Sahrens 			}
22043912Slling 
22053912Slling 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
22063912Slling 			/*
22073912Slling 			 * MacOS X can extract the object type here such as:
22083912Slling 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
22093912Slling 			 */
22105663Sck153898 
22115663Sck153898 			if (check_sysattrs && !zap.za_normalization_conflict) {
22125663Sck153898 				zap.za_normalization_conflict =
22135663Sck153898 				    xattr_sysattr_casechk(zap.za_name);
22145663Sck153898 			}
2215789Sahrens 		}
22165331Samw 
22179749STim.Haley@Sun.COM 		if (flags & V_RDDIR_ACCFILTER) {
22189749STim.Haley@Sun.COM 			/*
22199749STim.Haley@Sun.COM 			 * If we have no access at all, don't include
22209749STim.Haley@Sun.COM 			 * this entry in the returned information
22219749STim.Haley@Sun.COM 			 */
22229749STim.Haley@Sun.COM 			znode_t	*ezp;
22239749STim.Haley@Sun.COM 			if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
22249749STim.Haley@Sun.COM 				goto skip_entry;
22259749STim.Haley@Sun.COM 			if (!zfs_has_access(ezp, cr)) {
22269749STim.Haley@Sun.COM 				VN_RELE(ZTOV(ezp));
22279749STim.Haley@Sun.COM 				goto skip_entry;
22289749STim.Haley@Sun.COM 			}
22299749STim.Haley@Sun.COM 			VN_RELE(ZTOV(ezp));
22309749STim.Haley@Sun.COM 		}
22319749STim.Haley@Sun.COM 
22325331Samw 		if (flags & V_RDDIR_ENTFLAGS)
22335331Samw 			reclen = EDIRENT_RECLEN(strlen(zap.za_name));
22345331Samw 		else
22355331Samw 			reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2236789Sahrens 
2237789Sahrens 		/*
2238789Sahrens 		 * Will this entry fit in the buffer?
2239789Sahrens 		 */
22403912Slling 		if (outcount + reclen > bufsize) {
2241789Sahrens 			/*
2242789Sahrens 			 * Did we manage to fit anything in the buffer?
2243789Sahrens 			 */
2244789Sahrens 			if (!outcount) {
2245789Sahrens 				error = EINVAL;
2246789Sahrens 				goto update;
2247789Sahrens 			}
2248789Sahrens 			break;
2249789Sahrens 		}
22505331Samw 		if (flags & V_RDDIR_ENTFLAGS) {
22515331Samw 			/*
22525331Samw 			 * Add extended flag entry:
22535331Samw 			 */
22545331Samw 			eodp->ed_ino = objnum;
22555331Samw 			eodp->ed_reclen = reclen;
22565331Samw 			/* NOTE: ed_off is the offset for the *next* entry */
22575331Samw 			next = &(eodp->ed_off);
22585331Samw 			eodp->ed_eflags = zap.za_normalization_conflict ?
22595331Samw 			    ED_CASE_CONFLICT : 0;
22605331Samw 			(void) strncpy(eodp->ed_name, zap.za_name,
22615331Samw 			    EDIRENT_NAMELEN(reclen));
22625331Samw 			eodp = (edirent_t *)((intptr_t)eodp + reclen);
22635331Samw 		} else {
22645331Samw 			/*
22655331Samw 			 * Add normal entry:
22665331Samw 			 */
22675331Samw 			odp->d_ino = objnum;
22685331Samw 			odp->d_reclen = reclen;
22695331Samw 			/* NOTE: d_off is the offset for the *next* entry */
22705331Samw 			next = &(odp->d_off);
22715331Samw 			(void) strncpy(odp->d_name, zap.za_name,
22725331Samw 			    DIRENT64_NAMELEN(reclen));
22735331Samw 			odp = (dirent64_t *)((intptr_t)odp + reclen);
22745331Samw 		}
22753912Slling 		outcount += reclen;
2276789Sahrens 
2277789Sahrens 		ASSERT(outcount <= bufsize);
2278789Sahrens 
2279789Sahrens 		/* Prefetch znode */
2280869Sperrin 		if (prefetch)
22813912Slling 			dmu_prefetch(os, objnum, 0, 0);
2282789Sahrens 
22839749STim.Haley@Sun.COM 	skip_entry:
2284789Sahrens 		/*
2285789Sahrens 		 * Move to the next entry, fill in the previous offset.
2286789Sahrens 		 */
2287789Sahrens 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2288789Sahrens 			zap_cursor_advance(&zc);
2289789Sahrens 			offset = zap_cursor_serialize(&zc);
2290789Sahrens 		} else {
2291789Sahrens 			offset += 1;
2292789Sahrens 		}
2293789Sahrens 		*next = offset;
2294789Sahrens 	}
2295869Sperrin 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2296789Sahrens 
2297789Sahrens 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2298789Sahrens 		iovp->iov_base += outcount;
2299789Sahrens 		iovp->iov_len -= outcount;
2300789Sahrens 		uio->uio_resid -= outcount;
2301789Sahrens 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2302789Sahrens 		/*
2303789Sahrens 		 * Reset the pointer.
2304789Sahrens 		 */
2305789Sahrens 		offset = uio->uio_loffset;
2306789Sahrens 	}
2307789Sahrens 
2308789Sahrens update:
2309885Sahrens 	zap_cursor_fini(&zc);
2310789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2311789Sahrens 		kmem_free(outbuf, bufsize);
2312789Sahrens 
2313789Sahrens 	if (error == ENOENT)
2314789Sahrens 		error = 0;
2315789Sahrens 
2316789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2317789Sahrens 
2318789Sahrens 	uio->uio_loffset = offset;
2319789Sahrens 	ZFS_EXIT(zfsvfs);
2320789Sahrens 	return (error);
2321789Sahrens }
2322789Sahrens 
23234720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4;
23244720Sfr157268 
2325789Sahrens static int
23265331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2327789Sahrens {
2328789Sahrens 	znode_t	*zp = VTOZ(vp);
2329789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2330789Sahrens 
23311773Seschrock 	/*
23321773Seschrock 	 * Regardless of whether this is required for standards conformance,
23331773Seschrock 	 * this is the logical behavior when fsync() is called on a file with
23341773Seschrock 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
23351773Seschrock 	 * going to be pushed out as part of the zil_commit().
23361773Seschrock 	 */
23371773Seschrock 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
23381773Seschrock 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
23395331Samw 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
23401773Seschrock 
23414720Sfr157268 	(void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
23424720Sfr157268 
234312294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
234412294SMark.Musante@Sun.COM 		ZFS_ENTER(zfsvfs);
234512294SMark.Musante@Sun.COM 		ZFS_VERIFY_ZP(zp);
234612294SMark.Musante@Sun.COM 		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
234712294SMark.Musante@Sun.COM 		ZFS_EXIT(zfsvfs);
234812294SMark.Musante@Sun.COM 	}
2349789Sahrens 	return (0);
2350789Sahrens }
2351789Sahrens 
23525331Samw 
2353789Sahrens /*
2354789Sahrens  * Get the requested file attributes and place them in the provided
2355789Sahrens  * vattr structure.
2356789Sahrens  *
2357789Sahrens  *	IN:	vp	- vnode of file.
2358789Sahrens  *		vap	- va_mask identifies requested attributes.
23595331Samw  *			  If AT_XVATTR set, then optional attrs are requested
23605331Samw  *		flags	- ATTR_NOACLCHECK (CIFS server context)
2361789Sahrens  *		cr	- credentials of caller.
23625331Samw  *		ct	- caller context
2363789Sahrens  *
2364789Sahrens  *	OUT:	vap	- attribute values.
2365789Sahrens  *
2366789Sahrens  *	RETURN:	0 (always succeeds)
2367789Sahrens  */
2368789Sahrens /* ARGSUSED */
2369789Sahrens static int
23705331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
23715331Samw     caller_context_t *ct)
2372789Sahrens {
2373789Sahrens 	znode_t *zp = VTOZ(vp);
2374789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
23755331Samw 	int	error = 0;
23764543Smarks 	uint64_t links;
237711935SMark.Shellenbaum@Sun.COM 	uint64_t mtime[2], ctime[2];
23785331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
23795331Samw 	xoptattr_t *xoap = NULL;
23805331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
238111935SMark.Shellenbaum@Sun.COM 	sa_bulk_attr_t bulk[2];
238211935SMark.Shellenbaum@Sun.COM 	int count = 0;
2383789Sahrens 
23845367Sahrens 	ZFS_ENTER(zfsvfs);
23855367Sahrens 	ZFS_VERIFY_ZP(zp);
238611935SMark.Shellenbaum@Sun.COM 
238711935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
238811935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
238911935SMark.Shellenbaum@Sun.COM 
239011935SMark.Shellenbaum@Sun.COM 	if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
239111935SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
239211935SMark.Shellenbaum@Sun.COM 		return (error);
239311935SMark.Shellenbaum@Sun.COM 	}
2394789Sahrens 
23955331Samw 	/*
23965331Samw 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
23975331Samw 	 * Also, if we are the owner don't bother, since owner should
23985331Samw 	 * always be allowed to read basic attributes of file.
23995331Samw 	 */
240011935SMark.Shellenbaum@Sun.COM 	if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) && (zp->z_uid != crgetuid(cr))) {
24015331Samw 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
24025331Samw 		    skipaclchk, cr)) {
24035331Samw 			ZFS_EXIT(zfsvfs);
24045331Samw 			return (error);
24055331Samw 		}
24065331Samw 	}
24075331Samw 
2408789Sahrens 	/*
2409789Sahrens 	 * Return all attributes.  It's cheaper to provide the answer
2410789Sahrens 	 * than to determine whether we were asked the question.
2411789Sahrens 	 */
2412789Sahrens 
24139774SRay.Hassan@Sun.COM 	mutex_enter(&zp->z_lock);
2414789Sahrens 	vap->va_type = vp->v_type;
241511935SMark.Shellenbaum@Sun.COM 	vap->va_mode = zp->z_mode & MODEMASK;
241611935SMark.Shellenbaum@Sun.COM 	vap->va_uid = zp->z_uid;
241711935SMark.Shellenbaum@Sun.COM 	vap->va_gid = zp->z_gid;
2418789Sahrens 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2419789Sahrens 	vap->va_nodeid = zp->z_id;
24204543Smarks 	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
242111935SMark.Shellenbaum@Sun.COM 		links = zp->z_links + 1;
24224543Smarks 	else
242311935SMark.Shellenbaum@Sun.COM 		links = zp->z_links;
24244543Smarks 	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
242511935SMark.Shellenbaum@Sun.COM 	vap->va_size = zp->z_size;
24261816Smarks 	vap->va_rdev = vp->v_rdev;
2427789Sahrens 	vap->va_seq = zp->z_seq;
2428789Sahrens 
24295331Samw 	/*
24305331Samw 	 * Add in any requested optional attributes and the create time.
24315331Samw 	 * Also set the corresponding bits in the returned attribute bitmap.
24325331Samw 	 */
24335331Samw 	if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
24345331Samw 		if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
24355331Samw 			xoap->xoa_archive =
243611935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_ARCHIVE) != 0);
24375331Samw 			XVA_SET_RTN(xvap, XAT_ARCHIVE);
24385331Samw 		}
24395331Samw 
24405331Samw 		if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
24415331Samw 			xoap->xoa_readonly =
244211935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_READONLY) != 0);
24435331Samw 			XVA_SET_RTN(xvap, XAT_READONLY);
24445331Samw 		}
24455331Samw 
24465331Samw 		if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
24475331Samw 			xoap->xoa_system =
244811935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_SYSTEM) != 0);
24495331Samw 			XVA_SET_RTN(xvap, XAT_SYSTEM);
24505331Samw 		}
24515331Samw 
24525331Samw 		if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
24535331Samw 			xoap->xoa_hidden =
245411935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_HIDDEN) != 0);
24555331Samw 			XVA_SET_RTN(xvap, XAT_HIDDEN);
24565331Samw 		}
24575331Samw 
24585331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
24595331Samw 			xoap->xoa_nounlink =
246011935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NOUNLINK) != 0);
24615331Samw 			XVA_SET_RTN(xvap, XAT_NOUNLINK);
24625331Samw 		}
24635331Samw 
24645331Samw 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
24655331Samw 			xoap->xoa_immutable =
246611935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
24675331Samw 			XVA_SET_RTN(xvap, XAT_IMMUTABLE);
24685331Samw 		}
24695331Samw 
24705331Samw 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
24715331Samw 			xoap->xoa_appendonly =
247211935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_APPENDONLY) != 0);
24735331Samw 			XVA_SET_RTN(xvap, XAT_APPENDONLY);
24745331Samw 		}
24755331Samw 
24765331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
24775331Samw 			xoap->xoa_nodump =
247811935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NODUMP) != 0);
24795331Samw 			XVA_SET_RTN(xvap, XAT_NODUMP);
24805331Samw 		}
24815331Samw 
24825331Samw 		if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
24835331Samw 			xoap->xoa_opaque =
248411935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_OPAQUE) != 0);
24855331Samw 			XVA_SET_RTN(xvap, XAT_OPAQUE);
24865331Samw 		}
24875331Samw 
24885331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
24895331Samw 			xoap->xoa_av_quarantined =
249011935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
24915331Samw 			XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
24925331Samw 		}
24935331Samw 
24945331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
24955331Samw 			xoap->xoa_av_modified =
249611935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
24975331Samw 			XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
24985331Samw 		}
24995331Samw 
25005331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
250111935SMark.Shellenbaum@Sun.COM 		    vp->v_type == VREG) {
250211935SMark.Shellenbaum@Sun.COM 			zfs_sa_get_scanstamp(zp, xvap);
25035331Samw 		}
25045331Samw 
25055331Samw 		if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
250611935SMark.Shellenbaum@Sun.COM 			uint64_t times[2];
250711935SMark.Shellenbaum@Sun.COM 
250811935SMark.Shellenbaum@Sun.COM 			(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
250911935SMark.Shellenbaum@Sun.COM 			    times, sizeof (times));
251011935SMark.Shellenbaum@Sun.COM 			ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
25115331Samw 			XVA_SET_RTN(xvap, XAT_CREATETIME);
25125331Samw 		}
251310793Sdai.ngo@sun.com 
251410793Sdai.ngo@sun.com 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
251511935SMark.Shellenbaum@Sun.COM 			xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
251610793Sdai.ngo@sun.com 			XVA_SET_RTN(xvap, XAT_REPARSE);
251710793Sdai.ngo@sun.com 		}
25185331Samw 	}
25195331Samw 
252011935SMark.Shellenbaum@Sun.COM 	ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
252111935SMark.Shellenbaum@Sun.COM 	ZFS_TIME_DECODE(&vap->va_mtime, mtime);
252211935SMark.Shellenbaum@Sun.COM 	ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2523789Sahrens 
2524789Sahrens 	mutex_exit(&zp->z_lock);
2525789Sahrens 
252611935SMark.Shellenbaum@Sun.COM 	sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2527789Sahrens 
2528789Sahrens 	if (zp->z_blksz == 0) {
2529789Sahrens 		/*
2530789Sahrens 		 * Block size hasn't been set; suggest maximal I/O transfers.
2531789Sahrens 		 */
2532789Sahrens 		vap->va_blksize = zfsvfs->z_max_blksz;
2533789Sahrens 	}
2534789Sahrens 
2535789Sahrens 	ZFS_EXIT(zfsvfs);
2536789Sahrens 	return (0);
2537789Sahrens }
2538789Sahrens 
2539789Sahrens /*
2540789Sahrens  * Set the file attributes to the values contained in the
2541789Sahrens  * vattr structure.
2542789Sahrens  *
2543789Sahrens  *	IN:	vp	- vnode of file to be modified.
2544789Sahrens  *		vap	- new attribute values.
25455331Samw  *			  If AT_XVATTR set, then optional attrs are being set
2546789Sahrens  *		flags	- ATTR_UTIME set if non-default time values provided.
25475331Samw  *			- ATTR_NOACLCHECK (CIFS context only).
2548789Sahrens  *		cr	- credentials of caller.
25495331Samw  *		ct	- caller context
2550789Sahrens  *
2551789Sahrens  *	RETURN:	0 if success
2552789Sahrens  *		error code if failure
2553789Sahrens  *
2554789Sahrens  * Timestamps:
2555789Sahrens  *	vp - ctime updated, mtime updated if size changed.
2556789Sahrens  */
2557789Sahrens /* ARGSUSED */
2558789Sahrens static int
2559789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2560789Sahrens 	caller_context_t *ct)
2561789Sahrens {
25625326Sek110237 	znode_t		*zp = VTOZ(vp);
2563789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
25645326Sek110237 	zilog_t		*zilog;
2565789Sahrens 	dmu_tx_t	*tx;
25661878Smaybee 	vattr_t		oldva;
25678190SMark.Shellenbaum@Sun.COM 	xvattr_t	tmpxvattr;
2568789Sahrens 	uint_t		mask = vap->va_mask;
25691878Smaybee 	uint_t		saved_mask;
25702796Smarks 	int		trim_mask = 0;
2571789Sahrens 	uint64_t	new_mode;
25729179SMark.Shellenbaum@Sun.COM 	uint64_t	new_uid, new_gid;
257311935SMark.Shellenbaum@Sun.COM 	uint64_t	xattr_obj = 0;
257411935SMark.Shellenbaum@Sun.COM 	uint64_t	mtime[2], ctime[2];
25751231Smarks 	znode_t		*attrzp;
2576789Sahrens 	int		need_policy = FALSE;
257711935SMark.Shellenbaum@Sun.COM 	int		err, err2;
25785331Samw 	zfs_fuid_info_t *fuidp = NULL;
25795331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
25805331Samw 	xoptattr_t	*xoap;
25815824Smarks 	zfs_acl_t	*aclp = NULL;
25825331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
258311935SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied = B_FALSE;
258411935SMark.Shellenbaum@Sun.COM 	sa_bulk_attr_t	bulk[7], xattr_bulk[7];
258511935SMark.Shellenbaum@Sun.COM 	int		count = 0, xattr_count = 0;
2586789Sahrens 
2587789Sahrens 	if (mask == 0)
2588789Sahrens 		return (0);
2589789Sahrens 
2590789Sahrens 	if (mask & AT_NOSET)
2591789Sahrens 		return (EINVAL);
2592789Sahrens 
25935367Sahrens 	ZFS_ENTER(zfsvfs);
25945367Sahrens 	ZFS_VERIFY_ZP(zp);
25955331Samw 
25965331Samw 	zilog = zfsvfs->z_log;
25975331Samw 
25985331Samw 	/*
25995331Samw 	 * Make sure that if we have ephemeral uid/gid or xvattr specified
26005331Samw 	 * that file system is at proper version level
26015331Samw 	 */
26025331Samw 
26035331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
26045331Samw 	    (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
26055331Samw 	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
26065386Stimh 	    (mask & AT_XVATTR))) {
26075386Stimh 		ZFS_EXIT(zfsvfs);
26085331Samw 		return (EINVAL);
26095386Stimh 	}
26105386Stimh 
26115386Stimh 	if (mask & AT_SIZE && vp->v_type == VDIR) {
26125386Stimh 		ZFS_EXIT(zfsvfs);
2613789Sahrens 		return (EISDIR);
26145386Stimh 	}
26155386Stimh 
26165386Stimh 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
26175386Stimh 		ZFS_EXIT(zfsvfs);
26181308Smarks 		return (EINVAL);
26195386Stimh 	}
26201308Smarks 
26215331Samw 	/*
26225331Samw 	 * If this is an xvattr_t, then get a pointer to the structure of
26235331Samw 	 * optional attributes.  If this is NULL, then we have a vattr_t.
26245331Samw 	 */
26255331Samw 	xoap = xva_getxoptattr(xvap);
26265331Samw 
26278190SMark.Shellenbaum@Sun.COM 	xva_init(&tmpxvattr);
26288190SMark.Shellenbaum@Sun.COM 
26295331Samw 	/*
26305331Samw 	 * Immutable files can only alter immutable bit and atime
26315331Samw 	 */
263211935SMark.Shellenbaum@Sun.COM 	if ((zp->z_pflags & ZFS_IMMUTABLE) &&
26335331Samw 	    ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
26345386Stimh 	    ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
26355386Stimh 		ZFS_EXIT(zfsvfs);
26365331Samw 		return (EPERM);
26375386Stimh 	}
26385386Stimh 
263911935SMark.Shellenbaum@Sun.COM 	if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
26405386Stimh 		ZFS_EXIT(zfsvfs);
26415331Samw 		return (EPERM);
26425386Stimh 	}
2643789Sahrens 
26446064Smarks 	/*
26456064Smarks 	 * Verify timestamps doesn't overflow 32 bits.
26466064Smarks 	 * ZFS can handle large timestamps, but 32bit syscalls can't
26476064Smarks 	 * handle times greater than 2039.  This check should be removed
26486064Smarks 	 * once large timestamps are fully supported.
26496064Smarks 	 */
26506064Smarks 	if (mask & (AT_ATIME | AT_MTIME)) {
26516064Smarks 		if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
26526064Smarks 		    ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
26536064Smarks 			ZFS_EXIT(zfsvfs);
26546064Smarks 			return (EOVERFLOW);
26556064Smarks 		}
26566064Smarks 	}
26576064Smarks 
2658789Sahrens top:
26591231Smarks 	attrzp = NULL;
2660789Sahrens 
26619981STim.Haley@Sun.COM 	/* Can this be moved to before the top label? */
2662789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2663789Sahrens 		ZFS_EXIT(zfsvfs);
2664789Sahrens 		return (EROFS);
2665789Sahrens 	}
2666789Sahrens 
2667789Sahrens 	/*
2668789Sahrens 	 * First validate permissions
2669789Sahrens 	 */
2670789Sahrens 
2671789Sahrens 	if (mask & AT_SIZE) {
26725331Samw 		err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2673789Sahrens 		if (err) {
2674789Sahrens 			ZFS_EXIT(zfsvfs);
2675789Sahrens 			return (err);
2676789Sahrens 		}
26771878Smaybee 		/*
26781878Smaybee 		 * XXX - Note, we are not providing any open
26791878Smaybee 		 * mode flags here (like FNDELAY), so we may
26801878Smaybee 		 * block if there are locks present... this
26811878Smaybee 		 * should be addressed in openat().
26821878Smaybee 		 */
26836992Smaybee 		/* XXX - would it be OK to generate a log record here? */
26846992Smaybee 		err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
26851878Smaybee 		if (err) {
26861878Smaybee 			ZFS_EXIT(zfsvfs);
26871878Smaybee 			return (err);
26881878Smaybee 		}
2689789Sahrens 	}
2690789Sahrens 
26915331Samw 	if (mask & (AT_ATIME|AT_MTIME) ||
26925331Samw 	    ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
26935331Samw 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
26945331Samw 	    XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
26955331Samw 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
269611935SMark.Shellenbaum@Sun.COM 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
26975331Samw 		need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
26985331Samw 		    skipaclchk, cr);
269911935SMark.Shellenbaum@Sun.COM 	}
2700789Sahrens 
2701789Sahrens 	if (mask & (AT_UID|AT_GID)) {
2702789Sahrens 		int	idmask = (mask & (AT_UID|AT_GID));
2703789Sahrens 		int	take_owner;
2704789Sahrens 		int	take_group;
2705789Sahrens 
2706789Sahrens 		/*
2707913Smarks 		 * NOTE: even if a new mode is being set,
2708913Smarks 		 * we may clear S_ISUID/S_ISGID bits.
2709913Smarks 		 */
2710913Smarks 
2711913Smarks 		if (!(mask & AT_MODE))
271211935SMark.Shellenbaum@Sun.COM 			vap->va_mode = zp->z_mode;
2713913Smarks 
2714913Smarks 		/*
2715789Sahrens 		 * Take ownership or chgrp to group we are a member of
2716789Sahrens 		 */
2717789Sahrens 
2718789Sahrens 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
27195331Samw 		take_group = (mask & AT_GID) &&
27205331Samw 		    zfs_groupmember(zfsvfs, vap->va_gid, cr);
2721789Sahrens 
2722789Sahrens 		/*
2723789Sahrens 		 * If both AT_UID and AT_GID are set then take_owner and
2724789Sahrens 		 * take_group must both be set in order to allow taking
2725789Sahrens 		 * ownership.
2726789Sahrens 		 *
2727789Sahrens 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2728789Sahrens 		 *
2729789Sahrens 		 */
2730789Sahrens 
2731789Sahrens 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2732789Sahrens 		    ((idmask == AT_UID) && take_owner) ||
2733789Sahrens 		    ((idmask == AT_GID) && take_group)) {
27345331Samw 			if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
27355331Samw 			    skipaclchk, cr) == 0) {
2736789Sahrens 				/*
2737789Sahrens 				 * Remove setuid/setgid for non-privileged users
2738789Sahrens 				 */
27391115Smarks 				secpolicy_setid_clear(vap, cr);
27402796Smarks 				trim_mask = (mask & (AT_UID|AT_GID));
2741789Sahrens 			} else {
2742789Sahrens 				need_policy =  TRUE;
2743789Sahrens 			}
2744789Sahrens 		} else {
2745789Sahrens 			need_policy =  TRUE;
2746789Sahrens 		}
2747789Sahrens 	}
2748789Sahrens 
27492796Smarks 	mutex_enter(&zp->z_lock);
275011935SMark.Shellenbaum@Sun.COM 	oldva.va_mode = zp->z_mode;
275111935SMark.Shellenbaum@Sun.COM 	oldva.va_uid = zp->z_uid;
275211935SMark.Shellenbaum@Sun.COM 	oldva.va_gid = zp->z_gid;
27535331Samw 	if (mask & AT_XVATTR) {
27548190SMark.Shellenbaum@Sun.COM 		/*
27558190SMark.Shellenbaum@Sun.COM 		 * Update xvattr mask to include only those attributes
27568190SMark.Shellenbaum@Sun.COM 		 * that are actually changing.
27578190SMark.Shellenbaum@Sun.COM 		 *
27588190SMark.Shellenbaum@Sun.COM 		 * the bits will be restored prior to actually setting
27598190SMark.Shellenbaum@Sun.COM 		 * the attributes so the caller thinks they were set.
27608190SMark.Shellenbaum@Sun.COM 		 */
27618190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
27628190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_appendonly !=
276311935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
27648190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27658190SMark.Shellenbaum@Sun.COM 			} else {
27668190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_APPENDONLY);
27678190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
27688190SMark.Shellenbaum@Sun.COM 			}
27698190SMark.Shellenbaum@Sun.COM 		}
27708190SMark.Shellenbaum@Sun.COM 
27718190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
27728190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_nounlink !=
277311935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
27748190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27758190SMark.Shellenbaum@Sun.COM 			} else {
27768190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_NOUNLINK);
27778190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
27788190SMark.Shellenbaum@Sun.COM 			}
27798190SMark.Shellenbaum@Sun.COM 		}
27808190SMark.Shellenbaum@Sun.COM 
27818190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
27828190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_immutable !=
278311935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
27848190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27858190SMark.Shellenbaum@Sun.COM 			} else {
27868190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
27878190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
27888190SMark.Shellenbaum@Sun.COM 			}
27898190SMark.Shellenbaum@Sun.COM 		}
27908190SMark.Shellenbaum@Sun.COM 
27918190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
27928190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_nodump !=
279311935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_NODUMP) != 0)) {
27948190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
27958190SMark.Shellenbaum@Sun.COM 			} else {
27968190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_NODUMP);
27978190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
27988190SMark.Shellenbaum@Sun.COM 			}
27998190SMark.Shellenbaum@Sun.COM 		}
28008190SMark.Shellenbaum@Sun.COM 
28018190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
28028190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_av_modified !=
280311935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
28048190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
28058190SMark.Shellenbaum@Sun.COM 			} else {
28068190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
28078190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
28088190SMark.Shellenbaum@Sun.COM 			}
28098190SMark.Shellenbaum@Sun.COM 		}
28108190SMark.Shellenbaum@Sun.COM 
28118190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
28128190SMark.Shellenbaum@Sun.COM 			if ((vp->v_type != VREG &&
28138190SMark.Shellenbaum@Sun.COM 			    xoap->xoa_av_quarantined) ||
28148190SMark.Shellenbaum@Sun.COM 			    xoap->xoa_av_quarantined !=
281511935SMark.Shellenbaum@Sun.COM 			    ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
28168190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
28178190SMark.Shellenbaum@Sun.COM 			} else {
28188190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
28198190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
28208190SMark.Shellenbaum@Sun.COM 			}
28218190SMark.Shellenbaum@Sun.COM 		}
28228190SMark.Shellenbaum@Sun.COM 
282310793Sdai.ngo@sun.com 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
282410793Sdai.ngo@sun.com 			mutex_exit(&zp->z_lock);
282510793Sdai.ngo@sun.com 			ZFS_EXIT(zfsvfs);
282610793Sdai.ngo@sun.com 			return (EPERM);
282710793Sdai.ngo@sun.com 		}
282810793Sdai.ngo@sun.com 
28298190SMark.Shellenbaum@Sun.COM 		if (need_policy == FALSE &&
28308190SMark.Shellenbaum@Sun.COM 		    (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
28318190SMark.Shellenbaum@Sun.COM 		    XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
28325331Samw 			need_policy = TRUE;
28335331Samw 		}
28345331Samw 	}
28355331Samw 
28362796Smarks 	mutex_exit(&zp->z_lock);
28372796Smarks 
28382796Smarks 	if (mask & AT_MODE) {
28395331Samw 		if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
28402796Smarks 			err = secpolicy_setid_setsticky_clear(vp, vap,
28412796Smarks 			    &oldva, cr);
28422796Smarks 			if (err) {
28432796Smarks 				ZFS_EXIT(zfsvfs);
28442796Smarks 				return (err);
28452796Smarks 			}
28462796Smarks 			trim_mask |= AT_MODE;
28472796Smarks 		} else {
28482796Smarks 			need_policy = TRUE;
28492796Smarks 		}
28502796Smarks 	}
2851789Sahrens 
2852789Sahrens 	if (need_policy) {
28531115Smarks 		/*
28541115Smarks 		 * If trim_mask is set then take ownership
28552796Smarks 		 * has been granted or write_acl is present and user
28562796Smarks 		 * has the ability to modify mode.  In that case remove
28572796Smarks 		 * UID|GID and or MODE from mask so that
28581115Smarks 		 * secpolicy_vnode_setattr() doesn't revoke it.
28591115Smarks 		 */
28602796Smarks 
28612796Smarks 		if (trim_mask) {
28622796Smarks 			saved_mask = vap->va_mask;
28632796Smarks 			vap->va_mask &= ~trim_mask;
28642796Smarks 		}
2865789Sahrens 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
28665331Samw 		    (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2867789Sahrens 		if (err) {
2868789Sahrens 			ZFS_EXIT(zfsvfs);
2869789Sahrens 			return (err);
2870789Sahrens 		}
28711115Smarks 
28721115Smarks 		if (trim_mask)
28732796Smarks 			vap->va_mask |= saved_mask;
2874789Sahrens 	}
2875789Sahrens 
2876789Sahrens 	/*
2877789Sahrens 	 * secpolicy_vnode_setattr, or take ownership may have
2878789Sahrens 	 * changed va_mask
2879789Sahrens 	 */
2880789Sahrens 	mask = vap->va_mask;
2881789Sahrens 
288211935SMark.Shellenbaum@Sun.COM 	if ((mask & (AT_UID | AT_GID))) {
288311935SMark.Shellenbaum@Sun.COM 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xattr_obj,
288411935SMark.Shellenbaum@Sun.COM 		    sizeof (xattr_obj));
288511935SMark.Shellenbaum@Sun.COM 
288611935SMark.Shellenbaum@Sun.COM 		if (xattr_obj) {
288711935SMark.Shellenbaum@Sun.COM 			err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
28889396SMatthew.Ahrens@Sun.COM 			if (err)
288911935SMark.Shellenbaum@Sun.COM 				goto out2;
28909179SMark.Shellenbaum@Sun.COM 		}
28919179SMark.Shellenbaum@Sun.COM 		if (mask & AT_UID) {
28929179SMark.Shellenbaum@Sun.COM 			new_uid = zfs_fuid_create(zfsvfs,
28939179SMark.Shellenbaum@Sun.COM 			    (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
289411935SMark.Shellenbaum@Sun.COM 			if (vap->va_uid != zp->z_uid &&
289511935SMark.Shellenbaum@Sun.COM 			    zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
28969396SMatthew.Ahrens@Sun.COM 				err = EDQUOT;
289711935SMark.Shellenbaum@Sun.COM 				goto out2;
28989396SMatthew.Ahrens@Sun.COM 			}
28991231Smarks 		}
29009396SMatthew.Ahrens@Sun.COM 
29019179SMark.Shellenbaum@Sun.COM 		if (mask & AT_GID) {
29029179SMark.Shellenbaum@Sun.COM 			new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
29039179SMark.Shellenbaum@Sun.COM 			    cr, ZFS_GROUP, &fuidp);
290411935SMark.Shellenbaum@Sun.COM 			if (new_gid != zp->z_gid &&
290511935SMark.Shellenbaum@Sun.COM 			    zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
29069396SMatthew.Ahrens@Sun.COM 				err = EDQUOT;
290711935SMark.Shellenbaum@Sun.COM 				goto out2;
29089179SMark.Shellenbaum@Sun.COM 			}
29099179SMark.Shellenbaum@Sun.COM 		}
29101231Smarks 	}
291111935SMark.Shellenbaum@Sun.COM 	tx = dmu_tx_create(zfsvfs->z_os);
291211935SMark.Shellenbaum@Sun.COM 
291311935SMark.Shellenbaum@Sun.COM 	if (mask & AT_MODE) {
291411935SMark.Shellenbaum@Sun.COM 		uint64_t pmode = zp->z_mode;
291511935SMark.Shellenbaum@Sun.COM 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
291611935SMark.Shellenbaum@Sun.COM 
291711935SMark.Shellenbaum@Sun.COM 		if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
291811935SMark.Shellenbaum@Sun.COM 			goto out;
291911935SMark.Shellenbaum@Sun.COM 
292011935SMark.Shellenbaum@Sun.COM 		if (!zp->z_is_sa && ZFS_EXTERNAL_ACL(zp)) {
292111935SMark.Shellenbaum@Sun.COM 			/*
292211935SMark.Shellenbaum@Sun.COM 			 * Are we upgrading ACL from old V0 format
292311935SMark.Shellenbaum@Sun.COM 			 * to V1 format?
292411935SMark.Shellenbaum@Sun.COM 			 */
292511935SMark.Shellenbaum@Sun.COM 			if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
292611935SMark.Shellenbaum@Sun.COM 			    ZNODE_ACL_VERSION(zp) ==
292711935SMark.Shellenbaum@Sun.COM 			    ZFS_ACL_VERSION_INITIAL) {
292811935SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_free(tx,
292911935SMark.Shellenbaum@Sun.COM 				    ZFS_EXTERNAL_ACL(zp), 0,
293011935SMark.Shellenbaum@Sun.COM 				    DMU_OBJECT_END);
293111935SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
293211935SMark.Shellenbaum@Sun.COM 				    0, aclp->z_acl_bytes);
293311935SMark.Shellenbaum@Sun.COM 			} else {
293411935SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_write(tx, ZFS_EXTERNAL_ACL(zp), 0,
293511935SMark.Shellenbaum@Sun.COM 				    aclp->z_acl_bytes);
293611935SMark.Shellenbaum@Sun.COM 			}
293711935SMark.Shellenbaum@Sun.COM 		} else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
293811935SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
293911935SMark.Shellenbaum@Sun.COM 			    0, aclp->z_acl_bytes);
294011935SMark.Shellenbaum@Sun.COM 		}
294111935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
294211935SMark.Shellenbaum@Sun.COM 	} else {
294311935SMark.Shellenbaum@Sun.COM 		if ((mask & AT_XVATTR) &&
294411935SMark.Shellenbaum@Sun.COM 		    XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
294511935SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
294611935SMark.Shellenbaum@Sun.COM 		else
294711935SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
294811935SMark.Shellenbaum@Sun.COM 	}
294911935SMark.Shellenbaum@Sun.COM 
295011935SMark.Shellenbaum@Sun.COM 	if (attrzp) {
295111935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
295211935SMark.Shellenbaum@Sun.COM 	}
295311935SMark.Shellenbaum@Sun.COM 
295411935SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
295511935SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
295611935SMark.Shellenbaum@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
295711935SMark.Shellenbaum@Sun.COM 
295811935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
29591231Smarks 
29608227SNeil.Perrin@Sun.COM 	err = dmu_tx_assign(tx, TXG_NOWAIT);
2961789Sahrens 	if (err) {
29629396SMatthew.Ahrens@Sun.COM 		if (err == ERESTART)
29632113Sahrens 			dmu_tx_wait(tx);
29649396SMatthew.Ahrens@Sun.COM 		goto out;
2965789Sahrens 	}
2966789Sahrens 
296711935SMark.Shellenbaum@Sun.COM 	count = 0;
2968789Sahrens 	/*
2969789Sahrens 	 * Set each attribute requested.
2970789Sahrens 	 * We group settings according to the locks they need to acquire.
2971789Sahrens 	 *
2972789Sahrens 	 * Note: you cannot set ctime directly, although it will be
2973789Sahrens 	 * updated as a side-effect of calling this function.
2974789Sahrens 	 */
2975789Sahrens 
2976789Sahrens 	mutex_enter(&zp->z_lock);
2977789Sahrens 
297811935SMark.Shellenbaum@Sun.COM 	if (attrzp)
297911935SMark.Shellenbaum@Sun.COM 		mutex_enter(&attrzp->z_lock);
298011935SMark.Shellenbaum@Sun.COM 
298112164SMark.Shellenbaum@Sun.COM 	if (mask & (AT_UID|AT_GID)) {
298212164SMark.Shellenbaum@Sun.COM 
298312164SMark.Shellenbaum@Sun.COM 		if (mask & AT_UID) {
298412164SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
298512164SMark.Shellenbaum@Sun.COM 			    &new_uid, sizeof (new_uid));
298612164SMark.Shellenbaum@Sun.COM 			zp->z_uid = zfs_fuid_map_id(zfsvfs, new_uid,
298712164SMark.Shellenbaum@Sun.COM 			    cr, ZFS_OWNER);
298812164SMark.Shellenbaum@Sun.COM 			if (attrzp) {
298912164SMark.Shellenbaum@Sun.COM 				SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
299012164SMark.Shellenbaum@Sun.COM 				    SA_ZPL_UID(zfsvfs), NULL, &new_uid,
299112164SMark.Shellenbaum@Sun.COM 				    sizeof (new_uid));
299212164SMark.Shellenbaum@Sun.COM 				attrzp->z_gid = zp->z_uid;
299312164SMark.Shellenbaum@Sun.COM 			}
299411935SMark.Shellenbaum@Sun.COM 		}
299512164SMark.Shellenbaum@Sun.COM 
299612164SMark.Shellenbaum@Sun.COM 		if (mask & AT_GID) {
299712164SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
299812164SMark.Shellenbaum@Sun.COM 			    NULL, &new_gid, sizeof (new_gid));
299912164SMark.Shellenbaum@Sun.COM 			zp->z_gid = zfs_fuid_map_id(zfsvfs, new_gid, cr,
300012164SMark.Shellenbaum@Sun.COM 			    ZFS_GROUP);
300112164SMark.Shellenbaum@Sun.COM 			if (attrzp) {
300212164SMark.Shellenbaum@Sun.COM 				SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
300312164SMark.Shellenbaum@Sun.COM 				    SA_ZPL_GID(zfsvfs), NULL, &new_gid,
300412164SMark.Shellenbaum@Sun.COM 				    sizeof (new_gid));
300512164SMark.Shellenbaum@Sun.COM 				attrzp->z_gid = zp->z_gid;
300612164SMark.Shellenbaum@Sun.COM 			}
300712164SMark.Shellenbaum@Sun.COM 		}
300812164SMark.Shellenbaum@Sun.COM 		if (!(mask & AT_MODE)) {
300912164SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
301012164SMark.Shellenbaum@Sun.COM 			    NULL, &new_mode, sizeof (new_mode));
301112164SMark.Shellenbaum@Sun.COM 			new_mode = zp->z_mode;
301212164SMark.Shellenbaum@Sun.COM 		}
301312164SMark.Shellenbaum@Sun.COM 		err = zfs_acl_chown_setattr(zp);
301412164SMark.Shellenbaum@Sun.COM 		ASSERT(err == 0);
301511935SMark.Shellenbaum@Sun.COM 		if (attrzp) {
301612164SMark.Shellenbaum@Sun.COM 			err = zfs_acl_chown_setattr(attrzp);
301712164SMark.Shellenbaum@Sun.COM 			ASSERT(err == 0);
301811935SMark.Shellenbaum@Sun.COM 		}
301911935SMark.Shellenbaum@Sun.COM 	}
302011935SMark.Shellenbaum@Sun.COM 
3021789Sahrens 	if (mask & AT_MODE) {
30225824Smarks 		mutex_enter(&zp->z_acl_lock);
302311935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
302411935SMark.Shellenbaum@Sun.COM 		    &new_mode, sizeof (new_mode));
302511935SMark.Shellenbaum@Sun.COM 		zp->z_mode = new_mode;
302612164SMark.Shellenbaum@Sun.COM 		ASSERT3U((uintptr_t)aclp, !=, NULL);
30279179SMark.Shellenbaum@Sun.COM 		err = zfs_aclset_common(zp, aclp, cr, tx);
3028789Sahrens 		ASSERT3U(err, ==, 0);
302910143STim.Haley@Sun.COM 		zp->z_acl_cached = aclp;
303010143STim.Haley@Sun.COM 		aclp = NULL;
30315824Smarks 		mutex_exit(&zp->z_acl_lock);
3032789Sahrens 	}
3033789Sahrens 
30341231Smarks 	if (attrzp)
303511935SMark.Shellenbaum@Sun.COM 		mutex_exit(&attrzp->z_lock);
303611935SMark.Shellenbaum@Sun.COM 
303711935SMark.Shellenbaum@Sun.COM 	if (mask & AT_ATIME) {
303811935SMark.Shellenbaum@Sun.COM 		ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
303911935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
304011935SMark.Shellenbaum@Sun.COM 		    &zp->z_atime, sizeof (zp->z_atime));
30411231Smarks 	}
30421231Smarks 
304311935SMark.Shellenbaum@Sun.COM 	if (mask & AT_MTIME) {
304411935SMark.Shellenbaum@Sun.COM 		ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
304511935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
304611935SMark.Shellenbaum@Sun.COM 		    mtime, sizeof (mtime));
30471231Smarks 	}
30481231Smarks 
30496992Smaybee 	/* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
305011935SMark.Shellenbaum@Sun.COM 	if (mask & AT_SIZE && !(mask & AT_MTIME)) {
305111935SMark.Shellenbaum@Sun.COM 		if (!(mask & AT_MTIME))
305211935SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
305311935SMark.Shellenbaum@Sun.COM 			    NULL, mtime, sizeof (mtime));
305411935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
305511935SMark.Shellenbaum@Sun.COM 		    &ctime, sizeof (ctime));
305611935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
305711935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
305811935SMark.Shellenbaum@Sun.COM 	} else if (mask != 0) {
305911935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
306011935SMark.Shellenbaum@Sun.COM 		    &ctime, sizeof (ctime));
306111935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
306211935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
306311935SMark.Shellenbaum@Sun.COM 		if (attrzp) {
306411935SMark.Shellenbaum@Sun.COM 			SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
306511935SMark.Shellenbaum@Sun.COM 			    SA_ZPL_CTIME(zfsvfs), NULL,
306611935SMark.Shellenbaum@Sun.COM 			    &ctime, sizeof (ctime));
306711935SMark.Shellenbaum@Sun.COM 			zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
306811935SMark.Shellenbaum@Sun.COM 			    mtime, ctime, B_TRUE);
306911935SMark.Shellenbaum@Sun.COM 		}
307011935SMark.Shellenbaum@Sun.COM 	}
30715331Samw 	/*
30725331Samw 	 * Do this after setting timestamps to prevent timestamp
30735331Samw 	 * update from toggling bit
30745331Samw 	 */
30755331Samw 
30765331Samw 	if (xoap && (mask & AT_XVATTR)) {
30778190SMark.Shellenbaum@Sun.COM 
30788190SMark.Shellenbaum@Sun.COM 		/*
30798190SMark.Shellenbaum@Sun.COM 		 * restore trimmed off masks
30808190SMark.Shellenbaum@Sun.COM 		 * so that return masks can be set for caller.
30818190SMark.Shellenbaum@Sun.COM 		 */
30828190SMark.Shellenbaum@Sun.COM 
30838190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
30848190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_APPENDONLY);
30858190SMark.Shellenbaum@Sun.COM 		}
30868190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
30878190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_NOUNLINK);
30888190SMark.Shellenbaum@Sun.COM 		}
30898190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
30908190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_IMMUTABLE);
30918190SMark.Shellenbaum@Sun.COM 		}
30928190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
30938190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_NODUMP);
30948190SMark.Shellenbaum@Sun.COM 		}
30958190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
30968190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
30978190SMark.Shellenbaum@Sun.COM 		}
30988190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
30998190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
31008190SMark.Shellenbaum@Sun.COM 		}
31018190SMark.Shellenbaum@Sun.COM 
310211935SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
31035331Samw 			ASSERT(vp->v_type == VREG);
31045331Samw 
310511935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
310611935SMark.Shellenbaum@Sun.COM 		    &zp->z_pflags, sizeof (zp->z_pflags));
310711935SMark.Shellenbaum@Sun.COM 		zfs_xvattr_set(zp, xvap, tx);
31085331Samw 	}
3109789Sahrens 
31109179SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
31119179SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
31129179SMark.Shellenbaum@Sun.COM 
31131878Smaybee 	if (mask != 0)
31145331Samw 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
31155331Samw 
3116789Sahrens 	mutex_exit(&zp->z_lock);
3117789Sahrens 
31189396SMatthew.Ahrens@Sun.COM out:
311911935SMark.Shellenbaum@Sun.COM 	if (err == 0 && attrzp) {
312011935SMark.Shellenbaum@Sun.COM 		err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
312111935SMark.Shellenbaum@Sun.COM 		    xattr_count, tx);
312211935SMark.Shellenbaum@Sun.COM 		ASSERT(err2 == 0);
312311935SMark.Shellenbaum@Sun.COM 	}
312411935SMark.Shellenbaum@Sun.COM 
31251231Smarks 	if (attrzp)
31261231Smarks 		VN_RELE(ZTOV(attrzp));
312710143STim.Haley@Sun.COM 	if (aclp)
312810143STim.Haley@Sun.COM 		zfs_acl_free(aclp);
312910143STim.Haley@Sun.COM 
31309396SMatthew.Ahrens@Sun.COM 	if (fuidp) {
31319396SMatthew.Ahrens@Sun.COM 		zfs_fuid_info_free(fuidp);
31329396SMatthew.Ahrens@Sun.COM 		fuidp = NULL;
31339396SMatthew.Ahrens@Sun.COM 	}
31349396SMatthew.Ahrens@Sun.COM 
313511935SMark.Shellenbaum@Sun.COM 	if (err) {
31369396SMatthew.Ahrens@Sun.COM 		dmu_tx_abort(tx);
313711935SMark.Shellenbaum@Sun.COM 		if (err == ERESTART)
313811935SMark.Shellenbaum@Sun.COM 			goto top;
313911935SMark.Shellenbaum@Sun.COM 	} else {
314011935SMark.Shellenbaum@Sun.COM 		err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
31419396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
314211935SMark.Shellenbaum@Sun.COM 	}
314311935SMark.Shellenbaum@Sun.COM 
314411935SMark.Shellenbaum@Sun.COM 
314511935SMark.Shellenbaum@Sun.COM out2:
314612294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
314712294SMark.Musante@Sun.COM 		zil_commit(zilog, UINT64_MAX, 0);
314812294SMark.Musante@Sun.COM 
3149789Sahrens 	ZFS_EXIT(zfsvfs);
3150789Sahrens 	return (err);
3151789Sahrens }
3152789Sahrens 
31533271Smaybee typedef struct zfs_zlock {
31543271Smaybee 	krwlock_t	*zl_rwlock;	/* lock we acquired */
31553271Smaybee 	znode_t		*zl_znode;	/* znode we held */
31563271Smaybee 	struct zfs_zlock *zl_next;	/* next in list */
31573271Smaybee } zfs_zlock_t;
31583271Smaybee 
31593271Smaybee /*
31603271Smaybee  * Drop locks and release vnodes that were held by zfs_rename_lock().
31613271Smaybee  */
31623271Smaybee static void
31633271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp)
31643271Smaybee {
31653271Smaybee 	zfs_zlock_t *zl;
31663271Smaybee 
31673271Smaybee 	while ((zl = *zlpp) != NULL) {
31683271Smaybee 		if (zl->zl_znode != NULL)
31693271Smaybee 			VN_RELE(ZTOV(zl->zl_znode));
31703271Smaybee 		rw_exit(zl->zl_rwlock);
31713271Smaybee 		*zlpp = zl->zl_next;
31723271Smaybee 		kmem_free(zl, sizeof (*zl));
31733271Smaybee 	}
31743271Smaybee }
31753271Smaybee 
3176789Sahrens /*
3177789Sahrens  * Search back through the directory tree, using the ".." entries.
3178789Sahrens  * Lock each directory in the chain to prevent concurrent renames.
3179789Sahrens  * Fail any attempt to move a directory into one of its own descendants.
3180789Sahrens  * XXX - z_parent_lock can overlap with map or grow locks
3181789Sahrens  */
3182789Sahrens static int
3183789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3184789Sahrens {
3185789Sahrens 	zfs_zlock_t	*zl;
31863638Sbillm 	znode_t		*zp = tdzp;
3187789Sahrens 	uint64_t	rootid = zp->z_zfsvfs->z_root;
318811935SMark.Shellenbaum@Sun.COM 	uint64_t	oidp = zp->z_id;
3189789Sahrens 	krwlock_t	*rwlp = &szp->z_parent_lock;
3190789Sahrens 	krw_t		rw = RW_WRITER;
3191789Sahrens 
3192789Sahrens 	/*
3193789Sahrens 	 * First pass write-locks szp and compares to zp->z_id.
3194789Sahrens 	 * Later passes read-lock zp and compare to zp->z_parent.
3195789Sahrens 	 */
3196789Sahrens 	do {
31973271Smaybee 		if (!rw_tryenter(rwlp, rw)) {
31983271Smaybee 			/*
31993271Smaybee 			 * Another thread is renaming in this path.
32003271Smaybee 			 * Note that if we are a WRITER, we don't have any
32013271Smaybee 			 * parent_locks held yet.
32023271Smaybee 			 */
32033271Smaybee 			if (rw == RW_READER && zp->z_id > szp->z_id) {
32043271Smaybee 				/*
32053271Smaybee 				 * Drop our locks and restart
32063271Smaybee 				 */
32073271Smaybee 				zfs_rename_unlock(&zl);
32083271Smaybee 				*zlpp = NULL;
32093271Smaybee 				zp = tdzp;
321011935SMark.Shellenbaum@Sun.COM 				oidp = zp->z_id;
32113271Smaybee 				rwlp = &szp->z_parent_lock;
32123271Smaybee 				rw = RW_WRITER;
32133271Smaybee 				continue;
32143271Smaybee 			} else {
32153271Smaybee 				/*
32163271Smaybee 				 * Wait for other thread to drop its locks
32173271Smaybee 				 */
32183271Smaybee 				rw_enter(rwlp, rw);
32193271Smaybee 			}
32203271Smaybee 		}
32213271Smaybee 
3222789Sahrens 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3223789Sahrens 		zl->zl_rwlock = rwlp;
3224789Sahrens 		zl->zl_znode = NULL;
3225789Sahrens 		zl->zl_next = *zlpp;
3226789Sahrens 		*zlpp = zl;
3227789Sahrens 
322811935SMark.Shellenbaum@Sun.COM 		if (oidp == szp->z_id)		/* We're a descendant of szp */
3229789Sahrens 			return (EINVAL);
3230789Sahrens 
323111935SMark.Shellenbaum@Sun.COM 		if (oidp == rootid)		/* We've hit the top */
3232789Sahrens 			return (0);
3233789Sahrens 
3234789Sahrens 		if (rw == RW_READER) {		/* i.e. not the first pass */
323511935SMark.Shellenbaum@Sun.COM 			int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3236789Sahrens 			if (error)
3237789Sahrens 				return (error);
3238789Sahrens 			zl->zl_znode = zp;
3239789Sahrens 		}
324011935SMark.Shellenbaum@Sun.COM 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
324111935SMark.Shellenbaum@Sun.COM 		    &oidp, sizeof (oidp));
3242789Sahrens 		rwlp = &zp->z_parent_lock;
3243789Sahrens 		rw = RW_READER;
3244789Sahrens 
3245789Sahrens 	} while (zp->z_id != sdzp->z_id);
3246789Sahrens 
3247789Sahrens 	return (0);
3248789Sahrens }
3249789Sahrens 
3250789Sahrens /*
3251789Sahrens  * Move an entry from the provided source directory to the target
3252789Sahrens  * directory.  Change the entry name as indicated.
3253789Sahrens  *
3254789Sahrens  *	IN:	sdvp	- Source directory containing the "old entry".
3255789Sahrens  *		snm	- Old entry name.
3256789Sahrens  *		tdvp	- Target directory to contain the "new entry".
3257789Sahrens  *		tnm	- New entry name.
3258789Sahrens  *		cr	- credentials of caller.
32595331Samw  *		ct	- caller context
32605331Samw  *		flags	- case flags
3261789Sahrens  *
3262789Sahrens  *	RETURN:	0 if success
3263789Sahrens  *		error code if failure
3264789Sahrens  *
3265789Sahrens  * Timestamps:
3266789Sahrens  *	sdvp,tdvp - ctime|mtime updated
3267789Sahrens  */
32685331Samw /*ARGSUSED*/
3269789Sahrens static int
32705331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
32715331Samw     caller_context_t *ct, int flags)
3272789Sahrens {
3273789Sahrens 	znode_t		*tdzp, *szp, *tzp;
3274789Sahrens 	znode_t		*sdzp = VTOZ(sdvp);
3275789Sahrens 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
32765326Sek110237 	zilog_t		*zilog;
3277789Sahrens 	vnode_t		*realvp;
3278789Sahrens 	zfs_dirlock_t	*sdl, *tdl;
3279789Sahrens 	dmu_tx_t	*tx;
3280789Sahrens 	zfs_zlock_t	*zl;
32815331Samw 	int		cmp, serr, terr;
32825331Samw 	int		error = 0;
32835331Samw 	int		zflg = 0;
3284789Sahrens 
32855367Sahrens 	ZFS_ENTER(zfsvfs);
32865367Sahrens 	ZFS_VERIFY_ZP(sdzp);
32875326Sek110237 	zilog = zfsvfs->z_log;
3288789Sahrens 
3289789Sahrens 	/*
3290789Sahrens 	 * Make sure we have the real vp for the target directory.
3291789Sahrens 	 */
32925331Samw 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3293789Sahrens 		tdvp = realvp;
3294789Sahrens 
329512079SMark.Shellenbaum@Sun.COM 	if (tdvp->v_vfsp != sdvp->v_vfsp || zfsctl_is_node(tdvp)) {
3296789Sahrens 		ZFS_EXIT(zfsvfs);
3297789Sahrens 		return (EXDEV);
3298789Sahrens 	}
3299789Sahrens 
3300789Sahrens 	tdzp = VTOZ(tdvp);
33015367Sahrens 	ZFS_VERIFY_ZP(tdzp);
33025498Stimh 	if (zfsvfs->z_utf8 && u8_validate(tnm,
33035331Samw 	    strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
33045331Samw 		ZFS_EXIT(zfsvfs);
33055331Samw 		return (EILSEQ);
33065331Samw 	}
33075331Samw 
33085331Samw 	if (flags & FIGNORECASE)
33095331Samw 		zflg |= ZCILOOK;
33105331Samw 
3311789Sahrens top:
3312789Sahrens 	szp = NULL;
3313789Sahrens 	tzp = NULL;
3314789Sahrens 	zl = NULL;
3315789Sahrens 
3316789Sahrens 	/*
3317789Sahrens 	 * This is to prevent the creation of links into attribute space
3318789Sahrens 	 * by renaming a linked file into/outof an attribute directory.
3319789Sahrens 	 * See the comment in zfs_link() for why this is considered bad.
3320789Sahrens 	 */
332111935SMark.Shellenbaum@Sun.COM 	if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3322789Sahrens 		ZFS_EXIT(zfsvfs);
3323789Sahrens 		return (EINVAL);
3324789Sahrens 	}
3325789Sahrens 
3326789Sahrens 	/*
3327789Sahrens 	 * Lock source and target directory entries.  To prevent deadlock,
3328789Sahrens 	 * a lock ordering must be defined.  We lock the directory with
3329789Sahrens 	 * the smallest object id first, or if it's a tie, the one with
3330789Sahrens 	 * the lexically first name.
3331789Sahrens 	 */
3332789Sahrens 	if (sdzp->z_id < tdzp->z_id) {
3333789Sahrens 		cmp = -1;
3334789Sahrens 	} else if (sdzp->z_id > tdzp->z_id) {
3335789Sahrens 		cmp = 1;
3336789Sahrens 	} else {
33375331Samw 		/*
33385331Samw 		 * First compare the two name arguments without
33395331Samw 		 * considering any case folding.
33405331Samw 		 */
33415331Samw 		int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
33425331Samw 
33435331Samw 		cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
33445498Stimh 		ASSERT(error == 0 || !zfsvfs->z_utf8);
3345789Sahrens 		if (cmp == 0) {
3346789Sahrens 			/*
3347789Sahrens 			 * POSIX: "If the old argument and the new argument
3348789Sahrens 			 * both refer to links to the same existing file,
3349789Sahrens 			 * the rename() function shall return successfully
3350789Sahrens 			 * and perform no other action."
3351789Sahrens 			 */
3352789Sahrens 			ZFS_EXIT(zfsvfs);
3353789Sahrens 			return (0);
3354789Sahrens 		}
33555331Samw 		/*
33565331Samw 		 * If the file system is case-folding, then we may
33575331Samw 		 * have some more checking to do.  A case-folding file
33585331Samw 		 * system is either supporting mixed case sensitivity
33595331Samw 		 * access or is completely case-insensitive.  Note
33605331Samw 		 * that the file system is always case preserving.
33615331Samw 		 *
33625331Samw 		 * In mixed sensitivity mode case sensitive behavior
33635331Samw 		 * is the default.  FIGNORECASE must be used to
33645331Samw 		 * explicitly request case insensitive behavior.
33655331Samw 		 *
33665331Samw 		 * If the source and target names provided differ only
33675331Samw 		 * by case (e.g., a request to rename 'tim' to 'Tim'),
33685331Samw 		 * we will treat this as a special case in the
33695331Samw 		 * case-insensitive mode: as long as the source name
33705331Samw 		 * is an exact match, we will allow this to proceed as
33715331Samw 		 * a name-change request.
33725331Samw 		 */
33735498Stimh 		if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
33745498Stimh 		    (zfsvfs->z_case == ZFS_CASE_MIXED &&
33755498Stimh 		    flags & FIGNORECASE)) &&
33765331Samw 		    u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
33775331Samw 		    &error) == 0) {
33785331Samw 			/*
33795331Samw 			 * case preserving rename request, require exact
33805331Samw 			 * name matches
33815331Samw 			 */
33825331Samw 			zflg |= ZCIEXACT;
33835331Samw 			zflg &= ~ZCILOOK;
33845331Samw 		}
3385789Sahrens 	}
33865331Samw 
338711321SSanjeev.Bagewadi@Sun.COM 	/*
338811321SSanjeev.Bagewadi@Sun.COM 	 * If the source and destination directories are the same, we should
338911321SSanjeev.Bagewadi@Sun.COM 	 * grab the z_name_lock of that directory only once.
339011321SSanjeev.Bagewadi@Sun.COM 	 */
339111321SSanjeev.Bagewadi@Sun.COM 	if (sdzp == tdzp) {
339211321SSanjeev.Bagewadi@Sun.COM 		zflg |= ZHAVELOCK;
339311321SSanjeev.Bagewadi@Sun.COM 		rw_enter(&sdzp->z_name_lock, RW_READER);
339411321SSanjeev.Bagewadi@Sun.COM 	}
339511321SSanjeev.Bagewadi@Sun.COM 
3396789Sahrens 	if (cmp < 0) {
33975331Samw 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
33985331Samw 		    ZEXISTS | zflg, NULL, NULL);
33995331Samw 		terr = zfs_dirent_lock(&tdl,
34005331Samw 		    tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3401789Sahrens 	} else {
34025331Samw 		terr = zfs_dirent_lock(&tdl,
34035331Samw 		    tdzp, tnm, &tzp, zflg, NULL, NULL);
34045331Samw 		serr = zfs_dirent_lock(&sdl,
34055331Samw 		    sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
34065331Samw 		    NULL, NULL);
3407789Sahrens 	}
3408789Sahrens 
3409789Sahrens 	if (serr) {
3410789Sahrens 		/*
3411789Sahrens 		 * Source entry invalid or not there.
3412789Sahrens 		 */
3413789Sahrens 		if (!terr) {
3414789Sahrens 			zfs_dirent_unlock(tdl);
3415789Sahrens 			if (tzp)
3416789Sahrens 				VN_RELE(ZTOV(tzp));
3417789Sahrens 		}
341811321SSanjeev.Bagewadi@Sun.COM 
341911321SSanjeev.Bagewadi@Sun.COM 		if (sdzp == tdzp)
342011321SSanjeev.Bagewadi@Sun.COM 			rw_exit(&sdzp->z_name_lock);
342111321SSanjeev.Bagewadi@Sun.COM 
3422789Sahrens 		if (strcmp(snm, "..") == 0)
3423789Sahrens 			serr = EINVAL;
3424789Sahrens 		ZFS_EXIT(zfsvfs);
3425789Sahrens 		return (serr);
3426789Sahrens 	}
3427789Sahrens 	if (terr) {
3428789Sahrens 		zfs_dirent_unlock(sdl);
3429789Sahrens 		VN_RELE(ZTOV(szp));
343011321SSanjeev.Bagewadi@Sun.COM 
343111321SSanjeev.Bagewadi@Sun.COM 		if (sdzp == tdzp)
343211321SSanjeev.Bagewadi@Sun.COM 			rw_exit(&sdzp->z_name_lock);
343311321SSanjeev.Bagewadi@Sun.COM 
3434789Sahrens 		if (strcmp(tnm, "..") == 0)
3435789Sahrens 			terr = EINVAL;
3436789Sahrens 		ZFS_EXIT(zfsvfs);
3437789Sahrens 		return (terr);
3438789Sahrens 	}
3439789Sahrens 
3440789Sahrens 	/*
3441789Sahrens 	 * Must have write access at the source to remove the old entry
3442789Sahrens 	 * and write access at the target to create the new entry.
3443789Sahrens 	 * Note that if target and source are the same, this can be
3444789Sahrens 	 * done in a single check.
3445789Sahrens 	 */
3446789Sahrens 
3447789Sahrens 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3448789Sahrens 		goto out;
3449789Sahrens 
3450789Sahrens 	if (ZTOV(szp)->v_type == VDIR) {
3451789Sahrens 		/*
3452789Sahrens 		 * Check to make sure rename is valid.
3453789Sahrens 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3454789Sahrens 		 */
3455789Sahrens 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3456789Sahrens 			goto out;
3457789Sahrens 	}
3458789Sahrens 
3459789Sahrens 	/*
3460789Sahrens 	 * Does target exist?
3461789Sahrens 	 */
3462789Sahrens 	if (tzp) {
3463789Sahrens 		/*
3464789Sahrens 		 * Source and target must be the same type.
3465789Sahrens 		 */
3466789Sahrens 		if (ZTOV(szp)->v_type == VDIR) {
3467789Sahrens 			if (ZTOV(tzp)->v_type != VDIR) {
3468789Sahrens 				error = ENOTDIR;
3469789Sahrens 				goto out;
3470789Sahrens 			}
3471789Sahrens 		} else {
3472789Sahrens 			if (ZTOV(tzp)->v_type == VDIR) {
3473789Sahrens 				error = EISDIR;
3474789Sahrens 				goto out;
3475789Sahrens 			}
3476789Sahrens 		}
3477789Sahrens 		/*
3478789Sahrens 		 * POSIX dictates that when the source and target
3479789Sahrens 		 * entries refer to the same file object, rename
3480789Sahrens 		 * must do nothing and exit without error.
3481789Sahrens 		 */
3482789Sahrens 		if (szp->z_id == tzp->z_id) {
3483789Sahrens 			error = 0;
3484789Sahrens 			goto out;
3485789Sahrens 		}
3486789Sahrens 	}
3487789Sahrens 
34885331Samw 	vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3489789Sahrens 	if (tzp)
34905331Samw 		vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
34914863Spraks 
34924863Spraks 	/*
34934863Spraks 	 * notify the target directory if it is not the same
34944863Spraks 	 * as source directory.
34954863Spraks 	 */
34964863Spraks 	if (tdvp != sdvp) {
34975331Samw 		vnevent_rename_dest_dir(tdvp, ct);
34984863Spraks 	}
3499789Sahrens 
3500789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
350111935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
350211935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
35031544Seschrock 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
35041544Seschrock 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
350511935SMark.Shellenbaum@Sun.COM 	if (sdzp != tdzp) {
350611935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
350711935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, tdzp);
350811935SMark.Shellenbaum@Sun.COM 	}
350911935SMark.Shellenbaum@Sun.COM 	if (tzp) {
351011935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
351111935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, tzp);
351211935SMark.Shellenbaum@Sun.COM 	}
351311935SMark.Shellenbaum@Sun.COM 
351411935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, szp);
35153461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
35168227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3517789Sahrens 	if (error) {
3518789Sahrens 		if (zl != NULL)
3519789Sahrens 			zfs_rename_unlock(&zl);
3520789Sahrens 		zfs_dirent_unlock(sdl);
3521789Sahrens 		zfs_dirent_unlock(tdl);
352211321SSanjeev.Bagewadi@Sun.COM 
352311321SSanjeev.Bagewadi@Sun.COM 		if (sdzp == tdzp)
352411321SSanjeev.Bagewadi@Sun.COM 			rw_exit(&sdzp->z_name_lock);
352511321SSanjeev.Bagewadi@Sun.COM 
3526789Sahrens 		VN_RELE(ZTOV(szp));
3527789Sahrens 		if (tzp)
3528789Sahrens 			VN_RELE(ZTOV(tzp));
35298227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
35302113Sahrens 			dmu_tx_wait(tx);
35312113Sahrens 			dmu_tx_abort(tx);
3532789Sahrens 			goto top;
3533789Sahrens 		}
35342113Sahrens 		dmu_tx_abort(tx);
3535789Sahrens 		ZFS_EXIT(zfsvfs);
3536789Sahrens 		return (error);
3537789Sahrens 	}
3538789Sahrens 
3539789Sahrens 	if (tzp)	/* Attempt to remove the existing target */
35405331Samw 		error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3541789Sahrens 
3542789Sahrens 	if (error == 0) {
3543789Sahrens 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3544789Sahrens 		if (error == 0) {
354511935SMark.Shellenbaum@Sun.COM 			szp->z_pflags |= ZFS_AV_MODIFIED;
354611935SMark.Shellenbaum@Sun.COM 
354711935SMark.Shellenbaum@Sun.COM 			error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
354811935SMark.Shellenbaum@Sun.COM 			    (void *)&szp->z_pflags, sizeof (uint64_t), tx);
354911935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error, ==, 0);
35505331Samw 
3551789Sahrens 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
355211935SMark.Shellenbaum@Sun.COM 			ASSERT3U(error, ==, 0);
35535331Samw 
35545331Samw 			zfs_log_rename(zilog, tx,
35555331Samw 			    TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0),
35565331Samw 			    sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
35576976Seschrock 
35586976Seschrock 			/* Update path information for the target vnode */
35596976Seschrock 			vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm));
3560789Sahrens 		}
3561789Sahrens 	}
3562789Sahrens 
3563789Sahrens 	dmu_tx_commit(tx);
3564789Sahrens out:
3565789Sahrens 	if (zl != NULL)
3566789Sahrens 		zfs_rename_unlock(&zl);
3567789Sahrens 
3568789Sahrens 	zfs_dirent_unlock(sdl);
3569789Sahrens 	zfs_dirent_unlock(tdl);
3570789Sahrens 
357111321SSanjeev.Bagewadi@Sun.COM 	if (sdzp == tdzp)
357211321SSanjeev.Bagewadi@Sun.COM 		rw_exit(&sdzp->z_name_lock);
357311321SSanjeev.Bagewadi@Sun.COM 
357411321SSanjeev.Bagewadi@Sun.COM 
3575789Sahrens 	VN_RELE(ZTOV(szp));
3576789Sahrens 	if (tzp)
3577789Sahrens 		VN_RELE(ZTOV(tzp));
3578789Sahrens 
357912294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
358012294SMark.Musante@Sun.COM 		zil_commit(zilog, UINT64_MAX, 0);
358112294SMark.Musante@Sun.COM 
3582789Sahrens 	ZFS_EXIT(zfsvfs);
3583789Sahrens 	return (error);
3584789Sahrens }
3585789Sahrens 
3586789Sahrens /*
3587789Sahrens  * Insert the indicated symbolic reference entry into the directory.
3588789Sahrens  *
3589789Sahrens  *	IN:	dvp	- Directory to contain new symbolic link.
3590789Sahrens  *		link	- Name for new symlink entry.
3591789Sahrens  *		vap	- Attributes of new entry.
3592789Sahrens  *		target	- Target path of new symlink.
3593789Sahrens  *		cr	- credentials of caller.
35945331Samw  *		ct	- caller context
35955331Samw  *		flags	- case flags
3596789Sahrens  *
3597789Sahrens  *	RETURN:	0 if success
3598789Sahrens  *		error code if failure
3599789Sahrens  *
3600789Sahrens  * Timestamps:
3601789Sahrens  *	dvp - ctime|mtime updated
3602789Sahrens  */
36035331Samw /*ARGSUSED*/
3604789Sahrens static int
36055331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
36065331Samw     caller_context_t *ct, int flags)
3607789Sahrens {
3608789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
3609789Sahrens 	zfs_dirlock_t	*dl;
3610789Sahrens 	dmu_tx_t	*tx;
3611789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
36125326Sek110237 	zilog_t		*zilog;
361311935SMark.Shellenbaum@Sun.COM 	uint64_t	len = strlen(link);
3614789Sahrens 	int		error;
36155331Samw 	int		zflg = ZNEW;
36169179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t	acl_ids;
36179179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
361811935SMark.Shellenbaum@Sun.COM 	uint64_t	txtype = TX_SYMLINK;
3619789Sahrens 
3620789Sahrens 	ASSERT(vap->va_type == VLNK);
3621789Sahrens 
36225367Sahrens 	ZFS_ENTER(zfsvfs);
36235367Sahrens 	ZFS_VERIFY_ZP(dzp);
36245326Sek110237 	zilog = zfsvfs->z_log;
36255331Samw 
36265498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
36275331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
36285331Samw 		ZFS_EXIT(zfsvfs);
36295331Samw 		return (EILSEQ);
36305331Samw 	}
36315331Samw 	if (flags & FIGNORECASE)
36325331Samw 		zflg |= ZCILOOK;
3633789Sahrens 
3634789Sahrens 	if (len > MAXPATHLEN) {
3635789Sahrens 		ZFS_EXIT(zfsvfs);
3636789Sahrens 		return (ENAMETOOLONG);
3637789Sahrens 	}
3638789Sahrens 
3639*12302SMark.Shellenbaum@Sun.COM 	if ((error = zfs_acl_ids_create(dzp, 0,
3640*12302SMark.Shellenbaum@Sun.COM 	    vap, cr, NULL, &acl_ids)) != 0) {
3641*12302SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
3642*12302SMark.Shellenbaum@Sun.COM 		return (error);
3643*12302SMark.Shellenbaum@Sun.COM 	}
3644*12302SMark.Shellenbaum@Sun.COM top:
3645789Sahrens 	/*
3646789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3647789Sahrens 	 */
36485331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
36495331Samw 	if (error) {
3650*12302SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
3651789Sahrens 		ZFS_EXIT(zfsvfs);
3652789Sahrens 		return (error);
3653789Sahrens 	}
3654789Sahrens 
3655*12302SMark.Shellenbaum@Sun.COM 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3656*12302SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
3657*12302SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
3658*12302SMark.Shellenbaum@Sun.COM 		return (error);
3659*12302SMark.Shellenbaum@Sun.COM 	}
3660*12302SMark.Shellenbaum@Sun.COM 
36619396SMatthew.Ahrens@Sun.COM 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
36629396SMatthew.Ahrens@Sun.COM 		zfs_acl_ids_free(&acl_ids);
36639396SMatthew.Ahrens@Sun.COM 		zfs_dirent_unlock(dl);
36649396SMatthew.Ahrens@Sun.COM 		ZFS_EXIT(zfsvfs);
36659396SMatthew.Ahrens@Sun.COM 		return (EDQUOT);
36669396SMatthew.Ahrens@Sun.COM 	}
3667789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
36689179SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
3669789Sahrens 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
36701544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
367111935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
367211935SMark.Shellenbaum@Sun.COM 	    ZFS_SA_BASE_ATTR_SIZE + len);
367311935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
367411935SMark.Shellenbaum@Sun.COM 	if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
367511935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
367611935SMark.Shellenbaum@Sun.COM 		    acl_ids.z_aclp->z_acl_bytes);
367711935SMark.Shellenbaum@Sun.COM 	}
36789396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
36799396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
36808227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3681789Sahrens 	if (error) {
3682789Sahrens 		zfs_dirent_unlock(dl);
36838227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
36842113Sahrens 			dmu_tx_wait(tx);
36852113Sahrens 			dmu_tx_abort(tx);
3686789Sahrens 			goto top;
3687789Sahrens 		}
3688*12302SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
36892113Sahrens 		dmu_tx_abort(tx);
3690789Sahrens 		ZFS_EXIT(zfsvfs);
3691789Sahrens 		return (error);
3692789Sahrens 	}
3693789Sahrens 
3694789Sahrens 	/*
3695789Sahrens 	 * Create a new object for the symlink.
369611935SMark.Shellenbaum@Sun.COM 	 * for version 4 ZPL datsets the symlink will be an SA attribute
3697789Sahrens 	 */
369811935SMark.Shellenbaum@Sun.COM 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
369911935SMark.Shellenbaum@Sun.COM 
370011935SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
370111935SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
370211935SMark.Shellenbaum@Sun.COM 
370311935SMark.Shellenbaum@Sun.COM 	if (zp->z_is_sa)
370411935SMark.Shellenbaum@Sun.COM 		error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
370511935SMark.Shellenbaum@Sun.COM 		    link, len, tx);
370611935SMark.Shellenbaum@Sun.COM 	else
370711935SMark.Shellenbaum@Sun.COM 		zfs_sa_symlink(zp, link, len, tx);
370811935SMark.Shellenbaum@Sun.COM 
370911935SMark.Shellenbaum@Sun.COM 	zp->z_size = len;
371011935SMark.Shellenbaum@Sun.COM 	(void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
371111935SMark.Shellenbaum@Sun.COM 	    &zp->z_size, sizeof (zp->z_size), tx);
3712789Sahrens 	/*
3713789Sahrens 	 * Insert the new object into the directory.
3714789Sahrens 	 */
3715789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
371611935SMark.Shellenbaum@Sun.COM 
371711935SMark.Shellenbaum@Sun.COM 	if (flags & FIGNORECASE)
371811935SMark.Shellenbaum@Sun.COM 		txtype |= TX_CI;
371911935SMark.Shellenbaum@Sun.COM 	zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
37209179SMark.Shellenbaum@Sun.COM 
37219179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_free(&acl_ids);
3722789Sahrens 
3723789Sahrens 	dmu_tx_commit(tx);
3724789Sahrens 
3725789Sahrens 	zfs_dirent_unlock(dl);
3726789Sahrens 
3727789Sahrens 	VN_RELE(ZTOV(zp));
3728789Sahrens 
372912294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
373012294SMark.Musante@Sun.COM 		zil_commit(zilog, UINT64_MAX, 0);
373112294SMark.Musante@Sun.COM 
3732789Sahrens 	ZFS_EXIT(zfsvfs);
3733789Sahrens 	return (error);
3734789Sahrens }
3735789Sahrens 
3736789Sahrens /*
3737789Sahrens  * Return, in the buffer contained in the provided uio structure,
3738789Sahrens  * the symbolic path referred to by vp.
3739789Sahrens  *
3740789Sahrens  *	IN:	vp	- vnode of symbolic link.
3741789Sahrens  *		uoip	- structure to contain the link path.
3742789Sahrens  *		cr	- credentials of caller.
37435331Samw  *		ct	- caller context
3744789Sahrens  *
3745789Sahrens  *	OUT:	uio	- structure to contain the link path.
3746789Sahrens  *
3747789Sahrens  *	RETURN:	0 if success
3748789Sahrens  *		error code if failure
3749789Sahrens  *
3750789Sahrens  * Timestamps:
3751789Sahrens  *	vp - atime updated
3752789Sahrens  */
3753789Sahrens /* ARGSUSED */
3754789Sahrens static int
37555331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3756789Sahrens {
3757789Sahrens 	znode_t		*zp = VTOZ(vp);
3758789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3759789Sahrens 	int		error;
3760789Sahrens 
37615367Sahrens 	ZFS_ENTER(zfsvfs);
37625367Sahrens 	ZFS_VERIFY_ZP(zp);
3763789Sahrens 
376411935SMark.Shellenbaum@Sun.COM 	if (zp->z_is_sa)
376511935SMark.Shellenbaum@Sun.COM 		error = sa_lookup_uio(zp->z_sa_hdl,
376611935SMark.Shellenbaum@Sun.COM 		    SA_ZPL_SYMLINK(zfsvfs), uio);
376711935SMark.Shellenbaum@Sun.COM 	else
376811935SMark.Shellenbaum@Sun.COM 		error = zfs_sa_readlink(zp, uio);
3769789Sahrens 
3770789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
377111935SMark.Shellenbaum@Sun.COM 
3772789Sahrens 	ZFS_EXIT(zfsvfs);
3773789Sahrens 	return (error);
3774789Sahrens }
3775789Sahrens 
3776789Sahrens /*
3777789Sahrens  * Insert a new entry into directory tdvp referencing svp.
3778789Sahrens  *
3779789Sahrens  *	IN:	tdvp	- Directory to contain new entry.
3780789Sahrens  *		svp	- vnode of new entry.
3781789Sahrens  *		name	- name of new entry.
3782789Sahrens  *		cr	- credentials of caller.
37835331Samw  *		ct	- caller context
3784789Sahrens  *
3785789Sahrens  *	RETURN:	0 if success
3786789Sahrens  *		error code if failure
3787789Sahrens  *
3788789Sahrens  * Timestamps:
3789789Sahrens  *	tdvp - ctime|mtime updated
3790789Sahrens  *	 svp - ctime updated
3791789Sahrens  */
3792789Sahrens /* ARGSUSED */
3793789Sahrens static int
37945331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
37955331Samw     caller_context_t *ct, int flags)
3796789Sahrens {
3797789Sahrens 	znode_t		*dzp = VTOZ(tdvp);
3798789Sahrens 	znode_t		*tzp, *szp;
3799789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
38005326Sek110237 	zilog_t		*zilog;
3801789Sahrens 	zfs_dirlock_t	*dl;
3802789Sahrens 	dmu_tx_t	*tx;
3803789Sahrens 	vnode_t		*realvp;
3804789Sahrens 	int		error;
38055331Samw 	int		zf = ZNEW;
380612079SMark.Shellenbaum@Sun.COM 	uint64_t	parent;
3807789Sahrens 
3808789Sahrens 	ASSERT(tdvp->v_type == VDIR);
3809789Sahrens 
38105367Sahrens 	ZFS_ENTER(zfsvfs);
38115367Sahrens 	ZFS_VERIFY_ZP(dzp);
38125326Sek110237 	zilog = zfsvfs->z_log;
3813789Sahrens 
38145331Samw 	if (VOP_REALVP(svp, &realvp, ct) == 0)
3815789Sahrens 		svp = realvp;
3816789Sahrens 
381712079SMark.Shellenbaum@Sun.COM 	/*
381812079SMark.Shellenbaum@Sun.COM 	 * POSIX dictates that we return EPERM here.
381912079SMark.Shellenbaum@Sun.COM 	 * Better choices include ENOTSUP or EISDIR.
382012079SMark.Shellenbaum@Sun.COM 	 */
382112079SMark.Shellenbaum@Sun.COM 	if (svp->v_type == VDIR) {
382212079SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
382312079SMark.Shellenbaum@Sun.COM 		return (EPERM);
382412079SMark.Shellenbaum@Sun.COM 	}
382512079SMark.Shellenbaum@Sun.COM 
382612079SMark.Shellenbaum@Sun.COM 	if (svp->v_vfsp != tdvp->v_vfsp || zfsctl_is_node(svp)) {
3827789Sahrens 		ZFS_EXIT(zfsvfs);
3828789Sahrens 		return (EXDEV);
3829789Sahrens 	}
383012079SMark.Shellenbaum@Sun.COM 
38315367Sahrens 	szp = VTOZ(svp);
38325367Sahrens 	ZFS_VERIFY_ZP(szp);
3833789Sahrens 
383412079SMark.Shellenbaum@Sun.COM 	/* Prevent links to .zfs/shares files */
383512079SMark.Shellenbaum@Sun.COM 
383612079SMark.Shellenbaum@Sun.COM 	if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
383712079SMark.Shellenbaum@Sun.COM 	    &parent, sizeof (uint64_t))) != 0) {
383812079SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
383912079SMark.Shellenbaum@Sun.COM 		return (error);
384012079SMark.Shellenbaum@Sun.COM 	}
384112079SMark.Shellenbaum@Sun.COM 	if (parent == zfsvfs->z_shares_dir) {
384212079SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
384312079SMark.Shellenbaum@Sun.COM 		return (EPERM);
384412079SMark.Shellenbaum@Sun.COM 	}
384512079SMark.Shellenbaum@Sun.COM 
38465498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name,
38475331Samw 	    strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
38485331Samw 		ZFS_EXIT(zfsvfs);
38495331Samw 		return (EILSEQ);
38505331Samw 	}
38515331Samw 	if (flags & FIGNORECASE)
38525331Samw 		zf |= ZCILOOK;
38535331Samw 
3854789Sahrens 	/*
3855789Sahrens 	 * We do not support links between attributes and non-attributes
3856789Sahrens 	 * because of the potential security risk of creating links
3857789Sahrens 	 * into "normal" file space in order to circumvent restrictions
3858789Sahrens 	 * imposed in attribute space.
3859789Sahrens 	 */
386011935SMark.Shellenbaum@Sun.COM 	if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
3861789Sahrens 		ZFS_EXIT(zfsvfs);
3862789Sahrens 		return (EINVAL);
3863789Sahrens 	}
3864789Sahrens 
3865789Sahrens 
386611935SMark.Shellenbaum@Sun.COM 	if (szp->z_uid != crgetuid(cr) &&
3867789Sahrens 	    secpolicy_basic_link(cr) != 0) {
3868789Sahrens 		ZFS_EXIT(zfsvfs);
3869789Sahrens 		return (EPERM);
3870789Sahrens 	}
3871789Sahrens 
38725331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3873789Sahrens 		ZFS_EXIT(zfsvfs);
3874789Sahrens 		return (error);
3875789Sahrens 	}
3876789Sahrens 
387712079SMark.Shellenbaum@Sun.COM top:
3878789Sahrens 	/*
3879789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3880789Sahrens 	 */
38815331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
38825331Samw 	if (error) {
3883789Sahrens 		ZFS_EXIT(zfsvfs);
3884789Sahrens 		return (error);
3885789Sahrens 	}
3886789Sahrens 
3887789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
388811935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
38891544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
389011935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, szp);
389111935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, dzp);
38928227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3893789Sahrens 	if (error) {
3894789Sahrens 		zfs_dirent_unlock(dl);
38958227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
38962113Sahrens 			dmu_tx_wait(tx);
38972113Sahrens 			dmu_tx_abort(tx);
3898789Sahrens 			goto top;
3899789Sahrens 		}
39002113Sahrens 		dmu_tx_abort(tx);
3901789Sahrens 		ZFS_EXIT(zfsvfs);
3902789Sahrens 		return (error);
3903789Sahrens 	}
3904789Sahrens 
3905789Sahrens 	error = zfs_link_create(dl, szp, tx, 0);
3906789Sahrens 
39075331Samw 	if (error == 0) {
39085331Samw 		uint64_t txtype = TX_LINK;
39095331Samw 		if (flags & FIGNORECASE)
39105331Samw 			txtype |= TX_CI;
39115331Samw 		zfs_log_link(zilog, tx, txtype, dzp, szp, name);
39125331Samw 	}
3913789Sahrens 
3914789Sahrens 	dmu_tx_commit(tx);
3915789Sahrens 
3916789Sahrens 	zfs_dirent_unlock(dl);
3917789Sahrens 
39184863Spraks 	if (error == 0) {
39195331Samw 		vnevent_link(svp, ct);
39204863Spraks 	}
39214863Spraks 
392212294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
392312294SMark.Musante@Sun.COM 		zil_commit(zilog, UINT64_MAX, 0);
392412294SMark.Musante@Sun.COM 
3925789Sahrens 	ZFS_EXIT(zfsvfs);
3926789Sahrens 	return (error);
3927789Sahrens }
3928789Sahrens 
3929789Sahrens /*
3930789Sahrens  * zfs_null_putapage() is used when the file system has been force
3931789Sahrens  * unmounted. It just drops the pages.
3932789Sahrens  */
3933789Sahrens /* ARGSUSED */
3934789Sahrens static int
3935789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3936789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3937789Sahrens {
3938789Sahrens 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
3939789Sahrens 	return (0);
3940789Sahrens }
3941789Sahrens 
39422688Smaybee /*
39432688Smaybee  * Push a page out to disk, klustering if possible.
39442688Smaybee  *
39452688Smaybee  *	IN:	vp	- file to push page to.
39462688Smaybee  *		pp	- page to push.
39472688Smaybee  *		flags	- additional flags.
39482688Smaybee  *		cr	- credentials of caller.
39492688Smaybee  *
39502688Smaybee  *	OUT:	offp	- start of range pushed.
39512688Smaybee  *		lenp	- len of range pushed.
39522688Smaybee  *
39532688Smaybee  *	RETURN:	0 if success
39542688Smaybee  *		error code if failure
39552688Smaybee  *
39562688Smaybee  * NOTE: callers must have locked the page to be pushed.  On
39572688Smaybee  * exit, the page (and all other pages in the kluster) must be
39582688Smaybee  * unlocked.
39592688Smaybee  */
3960789Sahrens /* ARGSUSED */
3961789Sahrens static int
3962789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3963789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3964789Sahrens {
3965789Sahrens 	znode_t		*zp = VTOZ(vp);
3966789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3967789Sahrens 	dmu_tx_t	*tx;
39682688Smaybee 	u_offset_t	off, koff;
39692688Smaybee 	size_t		len, klen;
3970789Sahrens 	int		err;
3971789Sahrens 
39722688Smaybee 	off = pp->p_offset;
39732688Smaybee 	len = PAGESIZE;
39742688Smaybee 	/*
39752688Smaybee 	 * If our blocksize is bigger than the page size, try to kluster
39768227SNeil.Perrin@Sun.COM 	 * multiple pages so that we write a full block (thus avoiding
39772688Smaybee 	 * a read-modify-write).
39782688Smaybee 	 */
397911935SMark.Shellenbaum@Sun.COM 	if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
39808636SMark.Maybee@Sun.COM 		klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
39818636SMark.Maybee@Sun.COM 		koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
398211935SMark.Shellenbaum@Sun.COM 		ASSERT(koff <= zp->z_size);
398311935SMark.Shellenbaum@Sun.COM 		if (koff + klen > zp->z_size)
398411935SMark.Shellenbaum@Sun.COM 			klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
39852688Smaybee 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
39862688Smaybee 	}
39872688Smaybee 	ASSERT3U(btop(len), ==, btopr(len));
39888636SMark.Maybee@Sun.COM 
39891819Smaybee 	/*
39901819Smaybee 	 * Can't push pages past end-of-file.
39911819Smaybee 	 */
399211935SMark.Shellenbaum@Sun.COM 	if (off >= zp->z_size) {
39934709Smaybee 		/* ignore all pages */
39942688Smaybee 		err = 0;
39952688Smaybee 		goto out;
399611935SMark.Shellenbaum@Sun.COM 	} else if (off + len > zp->z_size) {
399711935SMark.Shellenbaum@Sun.COM 		int npages = btopr(zp->z_size - off);
39982688Smaybee 		page_t *trunc;
39992688Smaybee 
40002688Smaybee 		page_list_break(&pp, &trunc, npages);
40014709Smaybee 		/* ignore pages past end of file */
40022688Smaybee 		if (trunc)
40034709Smaybee 			pvn_write_done(trunc, flags);
400411935SMark.Shellenbaum@Sun.COM 		len = zp->z_size - off;
40051819Smaybee 	}
40069396SMatthew.Ahrens@Sun.COM 
400711935SMark.Shellenbaum@Sun.COM 	if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
400811935SMark.Shellenbaum@Sun.COM 	    zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
40099396SMatthew.Ahrens@Sun.COM 		err = EDQUOT;
40109396SMatthew.Ahrens@Sun.COM 		goto out;
40119396SMatthew.Ahrens@Sun.COM 	}
40128636SMark.Maybee@Sun.COM top:
4013789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
4014789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, off, len);
401511935SMark.Shellenbaum@Sun.COM 
401611935SMark.Shellenbaum@Sun.COM 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
401711935SMark.Shellenbaum@Sun.COM 	zfs_sa_upgrade_txholds(tx, zp);
40188227SNeil.Perrin@Sun.COM 	err = dmu_tx_assign(tx, TXG_NOWAIT);
4019789Sahrens 	if (err != 0) {
40208227SNeil.Perrin@Sun.COM 		if (err == ERESTART) {
40212113Sahrens 			dmu_tx_wait(tx);
40222113Sahrens 			dmu_tx_abort(tx);
4023789Sahrens 			goto top;
4024789Sahrens 		}
40252113Sahrens 		dmu_tx_abort(tx);
4026789Sahrens 		goto out;
4027789Sahrens 	}
4028789Sahrens 
40292688Smaybee 	if (zp->z_blksz <= PAGESIZE) {
40307315SJonathan.Adams@Sun.COM 		caddr_t va = zfs_map_page(pp, S_READ);
40312688Smaybee 		ASSERT3U(len, <=, PAGESIZE);
40322688Smaybee 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
40337315SJonathan.Adams@Sun.COM 		zfs_unmap_page(pp, va);
40342688Smaybee 	} else {
40352688Smaybee 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
40362688Smaybee 	}
40372688Smaybee 
40382688Smaybee 	if (err == 0) {
403911935SMark.Shellenbaum@Sun.COM 		uint64_t mtime[2], ctime[2];
404011935SMark.Shellenbaum@Sun.COM 		sa_bulk_attr_t bulk[2];
404111935SMark.Shellenbaum@Sun.COM 		int count = 0;
404211935SMark.Shellenbaum@Sun.COM 
404311935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
404411935SMark.Shellenbaum@Sun.COM 		    &mtime, 16);
404511935SMark.Shellenbaum@Sun.COM 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
404611935SMark.Shellenbaum@Sun.COM 		    &ctime, 16);
404711935SMark.Shellenbaum@Sun.COM 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
404811935SMark.Shellenbaum@Sun.COM 		    B_TRUE);
40498636SMark.Maybee@Sun.COM 		zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
40502688Smaybee 	}
40519951SLin.Ling@Sun.COM 	dmu_tx_commit(tx);
40522688Smaybee 
40532688Smaybee out:
40544709Smaybee 	pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4055789Sahrens 	if (offp)
4056789Sahrens 		*offp = off;
4057789Sahrens 	if (lenp)
4058789Sahrens 		*lenp = len;
4059789Sahrens 
4060789Sahrens 	return (err);
4061789Sahrens }
4062789Sahrens 
4063789Sahrens /*
4064789Sahrens  * Copy the portion of the file indicated from pages into the file.
4065789Sahrens  * The pages are stored in a page list attached to the files vnode.
4066789Sahrens  *
4067789Sahrens  *	IN:	vp	- vnode of file to push page data to.
4068789Sahrens  *		off	- position in file to put data.
4069789Sahrens  *		len	- amount of data to write.
4070789Sahrens  *		flags	- flags to control the operation.
4071789Sahrens  *		cr	- credentials of caller.
40725331Samw  *		ct	- caller context.
4073789Sahrens  *
4074789Sahrens  *	RETURN:	0 if success
4075789Sahrens  *		error code if failure
4076789Sahrens  *
4077789Sahrens  * Timestamps:
4078789Sahrens  *	vp - ctime|mtime updated
4079789Sahrens  */
40805331Samw /*ARGSUSED*/
4081789Sahrens static int
40825331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
40835331Samw     caller_context_t *ct)
4084789Sahrens {
4085789Sahrens 	znode_t		*zp = VTOZ(vp);
4086789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4087789Sahrens 	page_t		*pp;
4088789Sahrens 	size_t		io_len;
4089789Sahrens 	u_offset_t	io_off;
40908636SMark.Maybee@Sun.COM 	uint_t		blksz;
40918636SMark.Maybee@Sun.COM 	rl_t		*rl;
4092789Sahrens 	int		error = 0;
4093789Sahrens 
40945367Sahrens 	ZFS_ENTER(zfsvfs);
40955367Sahrens 	ZFS_VERIFY_ZP(zp);
4096789Sahrens 
40978636SMark.Maybee@Sun.COM 	/*
40988636SMark.Maybee@Sun.COM 	 * Align this request to the file block size in case we kluster.
40998636SMark.Maybee@Sun.COM 	 * XXX - this can result in pretty aggresive locking, which can
41008636SMark.Maybee@Sun.COM 	 * impact simultanious read/write access.  One option might be
41018636SMark.Maybee@Sun.COM 	 * to break up long requests (len == 0) into block-by-block
41028636SMark.Maybee@Sun.COM 	 * operations to get narrower locking.
41038636SMark.Maybee@Sun.COM 	 */
41048636SMark.Maybee@Sun.COM 	blksz = zp->z_blksz;
41058636SMark.Maybee@Sun.COM 	if (ISP2(blksz))
41068636SMark.Maybee@Sun.COM 		io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
41078636SMark.Maybee@Sun.COM 	else
41088636SMark.Maybee@Sun.COM 		io_off = 0;
41098636SMark.Maybee@Sun.COM 	if (len > 0 && ISP2(blksz))
41109141SMark.Maybee@Sun.COM 		io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
41118636SMark.Maybee@Sun.COM 	else
41128636SMark.Maybee@Sun.COM 		io_len = 0;
41138636SMark.Maybee@Sun.COM 
41148636SMark.Maybee@Sun.COM 	if (io_len == 0) {
4115789Sahrens 		/*
41168636SMark.Maybee@Sun.COM 		 * Search the entire vp list for pages >= io_off.
4117789Sahrens 		 */
41188636SMark.Maybee@Sun.COM 		rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
41198636SMark.Maybee@Sun.COM 		error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
41201472Sperrin 		goto out;
4121789Sahrens 	}
41228636SMark.Maybee@Sun.COM 	rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
41238636SMark.Maybee@Sun.COM 
412411935SMark.Shellenbaum@Sun.COM 	if (off > zp->z_size) {
4125789Sahrens 		/* past end of file */
41268636SMark.Maybee@Sun.COM 		zfs_range_unlock(rl);
4127789Sahrens 		ZFS_EXIT(zfsvfs);
4128789Sahrens 		return (0);
4129789Sahrens 	}
4130789Sahrens 
413111935SMark.Shellenbaum@Sun.COM 	len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
41328636SMark.Maybee@Sun.COM 
41338636SMark.Maybee@Sun.COM 	for (off = io_off; io_off < off + len; io_off += io_len) {
4134789Sahrens 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
41351669Sperrin 			pp = page_lookup(vp, io_off,
41364339Sperrin 			    (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4137789Sahrens 		} else {
4138789Sahrens 			pp = page_lookup_nowait(vp, io_off,
41394339Sperrin 			    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4140789Sahrens 		}
4141789Sahrens 
4142789Sahrens 		if (pp != NULL && pvn_getdirty(pp, flags)) {
4143789Sahrens 			int err;
4144789Sahrens 
4145789Sahrens 			/*
4146789Sahrens 			 * Found a dirty page to push
4147789Sahrens 			 */
41481669Sperrin 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
41491669Sperrin 			if (err)
4150789Sahrens 				error = err;
4151789Sahrens 		} else {
4152789Sahrens 			io_len = PAGESIZE;
4153789Sahrens 		}
4154789Sahrens 	}
41551472Sperrin out:
41568636SMark.Maybee@Sun.COM 	zfs_range_unlock(rl);
415712294SMark.Musante@Sun.COM 	if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
41582638Sperrin 		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
4159789Sahrens 	ZFS_EXIT(zfsvfs);
4160789Sahrens 	return (error);
4161789Sahrens }
4162789Sahrens 
41635331Samw /*ARGSUSED*/
4164789Sahrens void
41655331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4166789Sahrens {
4167789Sahrens 	znode_t	*zp = VTOZ(vp);
4168789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4169789Sahrens 	int error;
4170789Sahrens 
41715326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
417211935SMark.Shellenbaum@Sun.COM 	if (zp->z_sa_hdl == NULL) {
41735446Sahrens 		/*
41745642Smaybee 		 * The fs has been unmounted, or we did a
41755642Smaybee 		 * suspend/resume and this file no longer exists.
41765446Sahrens 		 */
4177789Sahrens 		if (vn_has_cached_data(vp)) {
4178789Sahrens 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
4179789Sahrens 			    B_INVAL, cr);
4180789Sahrens 		}
4181789Sahrens 
41821544Seschrock 		mutex_enter(&zp->z_lock);
418310369Schris.kirby@sun.com 		mutex_enter(&vp->v_lock);
418410369Schris.kirby@sun.com 		ASSERT(vp->v_count == 1);
418510369Schris.kirby@sun.com 		vp->v_count = 0;
418610369Schris.kirby@sun.com 		mutex_exit(&vp->v_lock);
41875446Sahrens 		mutex_exit(&zp->z_lock);
41885642Smaybee 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
41895446Sahrens 		zfs_znode_free(zp);
4190789Sahrens 		return;
4191789Sahrens 	}
4192789Sahrens 
4193789Sahrens 	/*
4194789Sahrens 	 * Attempt to push any data in the page cache.  If this fails
4195789Sahrens 	 * we will get kicked out later in zfs_zinactive().
4196789Sahrens 	 */
41971298Sperrin 	if (vn_has_cached_data(vp)) {
41981298Sperrin 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
41991298Sperrin 		    cr);
42001298Sperrin 	}
4201789Sahrens 
42023461Sahrens 	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4203789Sahrens 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4204789Sahrens 
420511935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
420611935SMark.Shellenbaum@Sun.COM 		zfs_sa_upgrade_txholds(tx, zp);
4207789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
4208789Sahrens 		if (error) {
4209789Sahrens 			dmu_tx_abort(tx);
4210789Sahrens 		} else {
4211789Sahrens 			mutex_enter(&zp->z_lock);
421211935SMark.Shellenbaum@Sun.COM 			(void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
421311935SMark.Shellenbaum@Sun.COM 			    (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4214789Sahrens 			zp->z_atime_dirty = 0;
4215789Sahrens 			mutex_exit(&zp->z_lock);
4216789Sahrens 			dmu_tx_commit(tx);
4217789Sahrens 		}
4218789Sahrens 	}
4219789Sahrens 
4220789Sahrens 	zfs_zinactive(zp);
42215326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
4222789Sahrens }
4223789Sahrens 
4224789Sahrens /*
4225789Sahrens  * Bounds-check the seek operation.
4226789Sahrens  *
4227789Sahrens  *	IN:	vp	- vnode seeking within
4228789Sahrens  *		ooff	- old file offset
4229789Sahrens  *		noffp	- pointer to new file offset
42305331Samw  *		ct	- caller context
4231789Sahrens  *
4232789Sahrens  *	RETURN:	0 if success
4233789Sahrens  *		EINVAL if new offset invalid
4234789Sahrens  */
4235789Sahrens /* ARGSUSED */
4236789Sahrens static int
42375331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
42385331Samw     caller_context_t *ct)
4239789Sahrens {
4240789Sahrens 	if (vp->v_type == VDIR)
4241789Sahrens 		return (0);
4242789Sahrens 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4243789Sahrens }
4244789Sahrens 
4245789Sahrens /*
4246789Sahrens  * Pre-filter the generic locking function to trap attempts to place
4247789Sahrens  * a mandatory lock on a memory mapped file.
4248789Sahrens  */
4249789Sahrens static int
4250789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
42515331Samw     flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4252789Sahrens {
4253789Sahrens 	znode_t *zp = VTOZ(vp);
4254789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4255789Sahrens 
42565367Sahrens 	ZFS_ENTER(zfsvfs);
42575367Sahrens 	ZFS_VERIFY_ZP(zp);
4258789Sahrens 
4259789Sahrens 	/*
42601544Seschrock 	 * We are following the UFS semantics with respect to mapcnt
42611544Seschrock 	 * here: If we see that the file is mapped already, then we will
42621544Seschrock 	 * return an error, but we don't worry about races between this
42631544Seschrock 	 * function and zfs_map().
4264789Sahrens 	 */
426511935SMark.Shellenbaum@Sun.COM 	if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4266789Sahrens 		ZFS_EXIT(zfsvfs);
4267789Sahrens 		return (EAGAIN);
4268789Sahrens 	}
4269789Sahrens 	ZFS_EXIT(zfsvfs);
427010896SMark.Shellenbaum@Sun.COM 	return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4271789Sahrens }
4272789Sahrens 
4273789Sahrens /*
4274789Sahrens  * If we can't find a page in the cache, we will create a new page
4275789Sahrens  * and fill it with file data.  For efficiency, we may try to fill
42768636SMark.Maybee@Sun.COM  * multiple pages at once (klustering) to fill up the supplied page
42779265SMark.Maybee@Sun.COM  * list.  Note that the pages to be filled are held with an exclusive
42789265SMark.Maybee@Sun.COM  * lock to prevent access by other threads while they are being filled.
4279789Sahrens  */
4280789Sahrens static int
4281789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4282789Sahrens     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4283789Sahrens {
4284789Sahrens 	znode_t *zp = VTOZ(vp);
4285789Sahrens 	page_t *pp, *cur_pp;
4286789Sahrens 	objset_t *os = zp->z_zfsvfs->z_os;
4287789Sahrens 	u_offset_t io_off, total;
4288789Sahrens 	size_t io_len;
4289789Sahrens 	int err;
4290789Sahrens 
42912688Smaybee 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
42928636SMark.Maybee@Sun.COM 		/*
42938636SMark.Maybee@Sun.COM 		 * We only have a single page, don't bother klustering
42948636SMark.Maybee@Sun.COM 		 */
4295789Sahrens 		io_off = off;
4296789Sahrens 		io_len = PAGESIZE;
42979265SMark.Maybee@Sun.COM 		pp = page_create_va(vp, io_off, io_len,
42989265SMark.Maybee@Sun.COM 		    PG_EXCL | PG_WAIT, seg, addr);
4299789Sahrens 	} else {
4300789Sahrens 		/*
43018636SMark.Maybee@Sun.COM 		 * Try to find enough pages to fill the page list
4302789Sahrens 		 */
4303789Sahrens 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
43048636SMark.Maybee@Sun.COM 		    &io_len, off, plsz, 0);
4305789Sahrens 	}
4306789Sahrens 	if (pp == NULL) {
4307789Sahrens 		/*
43088636SMark.Maybee@Sun.COM 		 * The page already exists, nothing to do here.
4309789Sahrens 		 */
4310789Sahrens 		*pl = NULL;
4311789Sahrens 		return (0);
4312789Sahrens 	}
4313789Sahrens 
4314789Sahrens 	/*
4315789Sahrens 	 * Fill the pages in the kluster.
4316789Sahrens 	 */
4317789Sahrens 	cur_pp = pp;
4318789Sahrens 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
43198636SMark.Maybee@Sun.COM 		caddr_t va;
43208636SMark.Maybee@Sun.COM 
43212688Smaybee 		ASSERT3U(io_off, ==, cur_pp->p_offset);
43227315SJonathan.Adams@Sun.COM 		va = zfs_map_page(cur_pp, S_WRITE);
43239512SNeil.Perrin@Sun.COM 		err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
43249512SNeil.Perrin@Sun.COM 		    DMU_READ_PREFETCH);
43257315SJonathan.Adams@Sun.COM 		zfs_unmap_page(cur_pp, va);
4326789Sahrens 		if (err) {
4327789Sahrens 			/* On error, toss the entire kluster */
4328789Sahrens 			pvn_read_done(pp, B_ERROR);
43297294Sperrin 			/* convert checksum errors into IO errors */
43307294Sperrin 			if (err == ECKSUM)
43317294Sperrin 				err = EIO;
4332789Sahrens 			return (err);
4333789Sahrens 		}
4334789Sahrens 		cur_pp = cur_pp->p_next;
4335789Sahrens 	}
43368636SMark.Maybee@Sun.COM 
4337789Sahrens 	/*
43388636SMark.Maybee@Sun.COM 	 * Fill in the page list array from the kluster starting
43398636SMark.Maybee@Sun.COM 	 * from the desired offset `off'.
4340789Sahrens 	 * NOTE: the page list will always be null terminated.
4341789Sahrens 	 */
4342789Sahrens 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
43438636SMark.Maybee@Sun.COM 	ASSERT(pl == NULL || (*pl)->p_offset == off);
4344789Sahrens 
4345789Sahrens 	return (0);
4346789Sahrens }
4347789Sahrens 
4348789Sahrens /*
4349789Sahrens  * Return pointers to the pages for the file region [off, off + len]
4350789Sahrens  * in the pl array.  If plsz is greater than len, this function may
43518636SMark.Maybee@Sun.COM  * also return page pointers from after the specified region
43528636SMark.Maybee@Sun.COM  * (i.e. the region [off, off + plsz]).  These additional pages are
43538636SMark.Maybee@Sun.COM  * only returned if they are already in the cache, or were created as
43548636SMark.Maybee@Sun.COM  * part of a klustered read.
4355789Sahrens  *
4356789Sahrens  *	IN:	vp	- vnode of file to get data from.
4357789Sahrens  *		off	- position in file to get data from.
4358789Sahrens  *		len	- amount of data to retrieve.
4359789Sahrens  *		plsz	- length of provided page list.
4360789Sahrens  *		seg	- segment to obtain pages for.
4361789Sahrens  *		addr	- virtual address of fault.
4362789Sahrens  *		rw	- mode of created pages.
4363789Sahrens  *		cr	- credentials of caller.
43645331Samw  *		ct	- caller context.
4365789Sahrens  *
4366789Sahrens  *	OUT:	protp	- protection mode of created pages.
4367789Sahrens  *		pl	- list of pages created.
4368789Sahrens  *
4369789Sahrens  *	RETURN:	0 if success
4370789Sahrens  *		error code if failure
4371789Sahrens  *
4372789Sahrens  * Timestamps:
4373789Sahrens  *	vp - atime updated
4374789Sahrens  */
4375789Sahrens /* ARGSUSED */
4376789Sahrens static int
4377789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4378789Sahrens 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
43795331Samw 	enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4380789Sahrens {
4381789Sahrens 	znode_t		*zp = VTOZ(vp);
4382789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
43838636SMark.Maybee@Sun.COM 	page_t		**pl0 = pl;
43848636SMark.Maybee@Sun.COM 	int		err = 0;
43858636SMark.Maybee@Sun.COM 
43868636SMark.Maybee@Sun.COM 	/* we do our own caching, faultahead is unnecessary */
43878636SMark.Maybee@Sun.COM 	if (pl == NULL)
43888636SMark.Maybee@Sun.COM 		return (0);
43898636SMark.Maybee@Sun.COM 	else if (len > plsz)
43908636SMark.Maybee@Sun.COM 		len = plsz;
43918681SMark.Maybee@Sun.COM 	else
43928681SMark.Maybee@Sun.COM 		len = P2ROUNDUP(len, PAGESIZE);
43938636SMark.Maybee@Sun.COM 	ASSERT(plsz >= len);
4394789Sahrens 
43955367Sahrens 	ZFS_ENTER(zfsvfs);
43965367Sahrens 	ZFS_VERIFY_ZP(zp);
4397789Sahrens 
4398789Sahrens 	if (protp)
4399789Sahrens 		*protp = PROT_ALL;
4400789Sahrens 
4401789Sahrens 	/*
44029265SMark.Maybee@Sun.COM 	 * Loop through the requested range [off, off + len) looking
4403789Sahrens 	 * for pages.  If we don't find a page, we will need to create
4404789Sahrens 	 * a new page and fill it with data from the file.
4405789Sahrens 	 */
4406789Sahrens 	while (len > 0) {
44078636SMark.Maybee@Sun.COM 		if (*pl = page_lookup(vp, off, SE_SHARED))
44088636SMark.Maybee@Sun.COM 			*(pl+1) = NULL;
44098636SMark.Maybee@Sun.COM 		else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
44108636SMark.Maybee@Sun.COM 			goto out;
44118636SMark.Maybee@Sun.COM 		while (*pl) {
44128636SMark.Maybee@Sun.COM 			ASSERT3U((*pl)->p_offset, ==, off);
4413789Sahrens 			off += PAGESIZE;
4414789Sahrens 			addr += PAGESIZE;
44158681SMark.Maybee@Sun.COM 			if (len > 0) {
44168681SMark.Maybee@Sun.COM 				ASSERT3U(len, >=, PAGESIZE);
44178636SMark.Maybee@Sun.COM 				len -= PAGESIZE;
44188681SMark.Maybee@Sun.COM 			}
44198636SMark.Maybee@Sun.COM 			ASSERT3U(plsz, >=, PAGESIZE);
4420789Sahrens 			plsz -= PAGESIZE;
44218636SMark.Maybee@Sun.COM 			pl++;
4422789Sahrens 		}
4423789Sahrens 	}
4424789Sahrens 
4425789Sahrens 	/*
4426789Sahrens 	 * Fill out the page array with any pages already in the cache.
4427789Sahrens 	 */
44288636SMark.Maybee@Sun.COM 	while (plsz > 0 &&
44298636SMark.Maybee@Sun.COM 	    (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
44308636SMark.Maybee@Sun.COM 			off += PAGESIZE;
44318636SMark.Maybee@Sun.COM 			plsz -= PAGESIZE;
4432789Sahrens 	}
4433789Sahrens out:
44342752Sperrin 	if (err) {
44352752Sperrin 		/*
44362752Sperrin 		 * Release any pages we have previously locked.
44372752Sperrin 		 */
44382752Sperrin 		while (pl > pl0)
44392752Sperrin 			page_unlock(*--pl);
44408636SMark.Maybee@Sun.COM 	} else {
44418636SMark.Maybee@Sun.COM 		ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
44422752Sperrin 	}
44432752Sperrin 
4444789Sahrens 	*pl = NULL;
4445789Sahrens 
4446789Sahrens 	ZFS_EXIT(zfsvfs);
4447789Sahrens 	return (err);
4448789Sahrens }
4449789Sahrens 
44501544Seschrock /*
44511544Seschrock  * Request a memory map for a section of a file.  This code interacts
44521544Seschrock  * with common code and the VM system as follows:
44531544Seschrock  *
44541544Seschrock  *	common code calls mmap(), which ends up in smmap_common()
44551544Seschrock  *
44561544Seschrock  *	this calls VOP_MAP(), which takes you into (say) zfs
44571544Seschrock  *
44581544Seschrock  *	zfs_map() calls as_map(), passing segvn_create() as the callback
44591544Seschrock  *
44601544Seschrock  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
44611544Seschrock  *
44621544Seschrock  *	zfs_addmap() updates z_mapcnt
44631544Seschrock  */
44645331Samw /*ARGSUSED*/
4465789Sahrens static int
4466789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
44675331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
44685331Samw     caller_context_t *ct)
4469789Sahrens {
4470789Sahrens 	znode_t *zp = VTOZ(vp);
4471789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4472789Sahrens 	segvn_crargs_t	vn_a;
4473789Sahrens 	int		error;
4474789Sahrens 
44755929Smarks 	ZFS_ENTER(zfsvfs);
44765929Smarks 	ZFS_VERIFY_ZP(zp);
44775929Smarks 
447811935SMark.Shellenbaum@Sun.COM 	if ((prot & PROT_WRITE) && (zp->z_pflags &
447911935SMark.Shellenbaum@Sun.COM 	    (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
44805929Smarks 		ZFS_EXIT(zfsvfs);
44815331Samw 		return (EPERM);
44825929Smarks 	}
44835929Smarks 
44845929Smarks 	if ((prot & (PROT_READ | PROT_EXEC)) &&
448511935SMark.Shellenbaum@Sun.COM 	    (zp->z_pflags & ZFS_AV_QUARANTINED)) {
44865929Smarks 		ZFS_EXIT(zfsvfs);
44875929Smarks 		return (EACCES);
44885929Smarks 	}
4489789Sahrens 
4490789Sahrens 	if (vp->v_flag & VNOMAP) {
4491789Sahrens 		ZFS_EXIT(zfsvfs);
4492789Sahrens 		return (ENOSYS);
4493789Sahrens 	}
4494789Sahrens 
4495789Sahrens 	if (off < 0 || len > MAXOFFSET_T - off) {
4496789Sahrens 		ZFS_EXIT(zfsvfs);
4497789Sahrens 		return (ENXIO);
4498789Sahrens 	}
4499789Sahrens 
4500789Sahrens 	if (vp->v_type != VREG) {
4501789Sahrens 		ZFS_EXIT(zfsvfs);
4502789Sahrens 		return (ENODEV);
4503789Sahrens 	}
4504789Sahrens 
4505789Sahrens 	/*
4506789Sahrens 	 * If file is locked, disallow mapping.
4507789Sahrens 	 */
450811935SMark.Shellenbaum@Sun.COM 	if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
45091544Seschrock 		ZFS_EXIT(zfsvfs);
45101544Seschrock 		return (EAGAIN);
4511789Sahrens 	}
4512789Sahrens 
4513789Sahrens 	as_rangelock(as);
45146036Smec 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
45156036Smec 	if (error != 0) {
45166036Smec 		as_rangeunlock(as);
45176036Smec 		ZFS_EXIT(zfsvfs);
45186036Smec 		return (error);
4519789Sahrens 	}
4520789Sahrens 
4521789Sahrens 	vn_a.vp = vp;
4522789Sahrens 	vn_a.offset = (u_offset_t)off;
4523789Sahrens 	vn_a.type = flags & MAP_TYPE;
4524789Sahrens 	vn_a.prot = prot;
4525789Sahrens 	vn_a.maxprot = maxprot;
4526789Sahrens 	vn_a.cred = cr;
4527789Sahrens 	vn_a.amp = NULL;
4528789Sahrens 	vn_a.flags = flags & ~MAP_TYPE;
45291417Skchow 	vn_a.szc = 0;
45301417Skchow 	vn_a.lgrp_mem_policy_flags = 0;
4531789Sahrens 
4532789Sahrens 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
4533789Sahrens 
4534789Sahrens 	as_rangeunlock(as);
4535789Sahrens 	ZFS_EXIT(zfsvfs);
4536789Sahrens 	return (error);
4537789Sahrens }
4538789Sahrens 
4539789Sahrens /* ARGSUSED */
4540789Sahrens static int
4541789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
45425331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
45435331Samw     caller_context_t *ct)
4544789Sahrens {
45451544Seschrock 	uint64_t pages = btopr(len);
45461544Seschrock 
45471544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4548789Sahrens 	return (0);
4549789Sahrens }
4550789Sahrens 
45511773Seschrock /*
45521773Seschrock  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
45531773Seschrock  * more accurate mtime for the associated file.  Since we don't have a way of
45541773Seschrock  * detecting when the data was actually modified, we have to resort to
45551773Seschrock  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
45561773Seschrock  * last page is pushed.  The problem occurs when the msync() call is omitted,
45571773Seschrock  * which by far the most common case:
45581773Seschrock  *
45591773Seschrock  * 	open()
45601773Seschrock  * 	mmap()
45611773Seschrock  * 	<modify memory>
45621773Seschrock  * 	munmap()
45631773Seschrock  * 	close()
45641773Seschrock  * 	<time lapse>
45651773Seschrock  * 	putpage() via fsflush
45661773Seschrock  *
45671773Seschrock  * If we wait until fsflush to come along, we can have a modification time that
45681773Seschrock  * is some arbitrary point in the future.  In order to prevent this in the
45691773Seschrock  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
45701773Seschrock  * torn down.
45711773Seschrock  */
4572789Sahrens /* ARGSUSED */
4573789Sahrens static int
4574789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
45755331Samw     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
45765331Samw     caller_context_t *ct)
4577789Sahrens {
45781544Seschrock 	uint64_t pages = btopr(len);
45791544Seschrock 
45801544Seschrock 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
45811544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
45821773Seschrock 
45831773Seschrock 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
45841773Seschrock 	    vn_has_cached_data(vp))
45855331Samw 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
45861773Seschrock 
4587789Sahrens 	return (0);
4588789Sahrens }
4589789Sahrens 
4590789Sahrens /*
4591789Sahrens  * Free or allocate space in a file.  Currently, this function only
4592789Sahrens  * supports the `F_FREESP' command.  However, this command is somewhat
4593789Sahrens  * misnamed, as its functionality includes the ability to allocate as
4594789Sahrens  * well as free space.
4595789Sahrens  *
4596789Sahrens  *	IN:	vp	- vnode of file to free data in.
4597789Sahrens  *		cmd	- action to take (only F_FREESP supported).
4598789Sahrens  *		bfp	- section of file to free/alloc.
4599789Sahrens  *		flag	- current file open mode flags.
4600789Sahrens  *		offset	- current file offset.
4601789Sahrens  *		cr	- credentials of caller [UNUSED].
46025331Samw  *		ct	- caller context.
4603789Sahrens  *
4604789Sahrens  *	RETURN:	0 if success
4605789Sahrens  *		error code if failure
4606789Sahrens  *
4607789Sahrens  * Timestamps:
4608789Sahrens  *	vp - ctime|mtime updated
4609789Sahrens  */
4610789Sahrens /* ARGSUSED */
4611789Sahrens static int
4612789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4613789Sahrens     offset_t offset, cred_t *cr, caller_context_t *ct)
4614789Sahrens {
4615789Sahrens 	znode_t		*zp = VTOZ(vp);
4616789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4617789Sahrens 	uint64_t	off, len;
4618789Sahrens 	int		error;
4619789Sahrens 
46205367Sahrens 	ZFS_ENTER(zfsvfs);
46215367Sahrens 	ZFS_VERIFY_ZP(zp);
4622789Sahrens 
4623789Sahrens 	if (cmd != F_FREESP) {
4624789Sahrens 		ZFS_EXIT(zfsvfs);
4625789Sahrens 		return (EINVAL);
4626789Sahrens 	}
4627789Sahrens 
4628789Sahrens 	if (error = convoff(vp, bfp, 0, offset)) {
4629789Sahrens 		ZFS_EXIT(zfsvfs);
4630789Sahrens 		return (error);
4631789Sahrens 	}
4632789Sahrens 
4633789Sahrens 	if (bfp->l_len < 0) {
4634789Sahrens 		ZFS_EXIT(zfsvfs);
4635789Sahrens 		return (EINVAL);
4636789Sahrens 	}
4637789Sahrens 
4638789Sahrens 	off = bfp->l_start;
46391669Sperrin 	len = bfp->l_len; /* 0 means from off to end of file */
46401878Smaybee 
46416992Smaybee 	error = zfs_freesp(zp, off, len, flag, TRUE);
4642789Sahrens 
4643789Sahrens 	ZFS_EXIT(zfsvfs);
4644789Sahrens 	return (error);
4645789Sahrens }
4646789Sahrens 
46475331Samw /*ARGSUSED*/
4648789Sahrens static int
46495331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4650789Sahrens {
4651789Sahrens 	znode_t		*zp = VTOZ(vp);
4652789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
46535326Sek110237 	uint32_t	gen;
465411935SMark.Shellenbaum@Sun.COM 	uint64_t	gen64;
4655789Sahrens 	uint64_t	object = zp->z_id;
4656789Sahrens 	zfid_short_t	*zfid;
465711935SMark.Shellenbaum@Sun.COM 	int		size, i, error;
4658789Sahrens 
46595367Sahrens 	ZFS_ENTER(zfsvfs);
46605367Sahrens 	ZFS_VERIFY_ZP(zp);
466111935SMark.Shellenbaum@Sun.COM 
466211935SMark.Shellenbaum@Sun.COM 	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
466312218SMark.Shellenbaum@Sun.COM 	    &gen64, sizeof (uint64_t))) != 0) {
466412218SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
466511935SMark.Shellenbaum@Sun.COM 		return (error);
466612218SMark.Shellenbaum@Sun.COM 	}
466711935SMark.Shellenbaum@Sun.COM 
466811935SMark.Shellenbaum@Sun.COM 	gen = (uint32_t)gen64;
4669789Sahrens 
4670789Sahrens 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4671789Sahrens 	if (fidp->fid_len < size) {
4672789Sahrens 		fidp->fid_len = size;
46731512Sek110237 		ZFS_EXIT(zfsvfs);
4674789Sahrens 		return (ENOSPC);
4675789Sahrens 	}
4676789Sahrens 
4677789Sahrens 	zfid = (zfid_short_t *)fidp;
4678789Sahrens 
4679789Sahrens 	zfid->zf_len = size;
4680789Sahrens 
4681789Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
4682789Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4683789Sahrens 
4684789Sahrens 	/* Must have a non-zero generation number to distinguish from .zfs */
4685789Sahrens 	if (gen == 0)
4686789Sahrens 		gen = 1;
4687789Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
4688789Sahrens 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4689789Sahrens 
4690789Sahrens 	if (size == LONG_FID_LEN) {
4691789Sahrens 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
4692789Sahrens 		zfid_long_t	*zlfid;
4693789Sahrens 
4694789Sahrens 		zlfid = (zfid_long_t *)fidp;
4695789Sahrens 
4696789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4697789Sahrens 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4698789Sahrens 
4699789Sahrens 		/* XXX - this should be the generation number for the objset */
4700789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4701789Sahrens 			zlfid->zf_setgen[i] = 0;
4702789Sahrens 	}
4703789Sahrens 
4704789Sahrens 	ZFS_EXIT(zfsvfs);
4705789Sahrens 	return (0);
4706789Sahrens }
4707789Sahrens 
4708789Sahrens static int
47095331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
47105331Samw     caller_context_t *ct)
4711789Sahrens {
4712789Sahrens 	znode_t		*zp, *xzp;
4713789Sahrens 	zfsvfs_t	*zfsvfs;
4714789Sahrens 	zfs_dirlock_t	*dl;
4715789Sahrens 	int		error;
4716789Sahrens 
4717789Sahrens 	switch (cmd) {
4718789Sahrens 	case _PC_LINK_MAX:
4719789Sahrens 		*valp = ULONG_MAX;
4720789Sahrens 		return (0);
4721789Sahrens 
4722789Sahrens 	case _PC_FILESIZEBITS:
4723789Sahrens 		*valp = 64;
4724789Sahrens 		return (0);
4725789Sahrens 
4726789Sahrens 	case _PC_XATTR_EXISTS:
4727789Sahrens 		zp = VTOZ(vp);
4728789Sahrens 		zfsvfs = zp->z_zfsvfs;
47295367Sahrens 		ZFS_ENTER(zfsvfs);
47305367Sahrens 		ZFS_VERIFY_ZP(zp);
4731789Sahrens 		*valp = 0;
4732789Sahrens 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
47335331Samw 		    ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4734789Sahrens 		if (error == 0) {
4735789Sahrens 			zfs_dirent_unlock(dl);
4736789Sahrens 			if (!zfs_dirempty(xzp))
4737789Sahrens 				*valp = 1;
4738789Sahrens 			VN_RELE(ZTOV(xzp));
4739789Sahrens 		} else if (error == ENOENT) {
4740789Sahrens 			/*
4741789Sahrens 			 * If there aren't extended attributes, it's the
4742789Sahrens 			 * same as having zero of them.
4743789Sahrens 			 */
4744789Sahrens 			error = 0;
4745789Sahrens 		}
4746789Sahrens 		ZFS_EXIT(zfsvfs);
4747789Sahrens 		return (error);
4748789Sahrens 
47495331Samw 	case _PC_SATTR_ENABLED:
47505331Samw 	case _PC_SATTR_EXISTS:
47517757SJanice.Chang@Sun.COM 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
47525331Samw 		    (vp->v_type == VREG || vp->v_type == VDIR);
47535331Samw 		return (0);
47545331Samw 
47559749STim.Haley@Sun.COM 	case _PC_ACCESS_FILTERING:
47569749STim.Haley@Sun.COM 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
47579749STim.Haley@Sun.COM 		    vp->v_type == VDIR;
47589749STim.Haley@Sun.COM 		return (0);
47599749STim.Haley@Sun.COM 
4760789Sahrens 	case _PC_ACL_ENABLED:
4761789Sahrens 		*valp = _ACL_ACE_ENABLED;
4762789Sahrens 		return (0);
4763789Sahrens 
4764789Sahrens 	case _PC_MIN_HOLE_SIZE:
4765789Sahrens 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
4766789Sahrens 		return (0);
4767789Sahrens 
476810440SRoger.Faulkner@Sun.COM 	case _PC_TIMESTAMP_RESOLUTION:
476910440SRoger.Faulkner@Sun.COM 		/* nanosecond timestamp resolution */
477010440SRoger.Faulkner@Sun.COM 		*valp = 1L;
477110440SRoger.Faulkner@Sun.COM 		return (0);
477210440SRoger.Faulkner@Sun.COM 
4773789Sahrens 	default:
47745331Samw 		return (fs_pathconf(vp, cmd, valp, cr, ct));
4775789Sahrens 	}
4776789Sahrens }
4777789Sahrens 
4778789Sahrens /*ARGSUSED*/
4779789Sahrens static int
47805331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
47815331Samw     caller_context_t *ct)
4782789Sahrens {
4783789Sahrens 	znode_t *zp = VTOZ(vp);
4784789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4785789Sahrens 	int error;
47865331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4787789Sahrens 
47885367Sahrens 	ZFS_ENTER(zfsvfs);
47895367Sahrens 	ZFS_VERIFY_ZP(zp);
47905331Samw 	error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4791789Sahrens 	ZFS_EXIT(zfsvfs);
4792789Sahrens 
4793789Sahrens 	return (error);
4794789Sahrens }
4795789Sahrens 
4796789Sahrens /*ARGSUSED*/
4797789Sahrens static int
47985331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
47995331Samw     caller_context_t *ct)
4800789Sahrens {
4801789Sahrens 	znode_t *zp = VTOZ(vp);
4802789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4803789Sahrens 	int error;
48045331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
480512294SMark.Musante@Sun.COM 	zilog_t	*zilog = zfsvfs->z_log;
4806789Sahrens 
48075367Sahrens 	ZFS_ENTER(zfsvfs);
48085367Sahrens 	ZFS_VERIFY_ZP(zp);
480912294SMark.Musante@Sun.COM 
48105331Samw 	error = zfs_setacl(zp, vsecp, skipaclchk, cr);
481112294SMark.Musante@Sun.COM 
481212294SMark.Musante@Sun.COM 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
481312294SMark.Musante@Sun.COM 		zil_commit(zilog, UINT64_MAX, 0);
481412294SMark.Musante@Sun.COM 
4815789Sahrens 	ZFS_EXIT(zfsvfs);
4816789Sahrens 	return (error);
4817789Sahrens }
4818789Sahrens 
4819789Sahrens /*
482011539SChunli.Zhang@Sun.COM  * Tunable, both must be a power of 2.
482111539SChunli.Zhang@Sun.COM  *
482211539SChunli.Zhang@Sun.COM  * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
482311539SChunli.Zhang@Sun.COM  * zcr_blksz_max: if set to less than the file block size, allow loaning out of
482411539SChunli.Zhang@Sun.COM  *                an arcbuf for a partial block read
482511539SChunli.Zhang@Sun.COM  */
482611539SChunli.Zhang@Sun.COM int zcr_blksz_min = (1 << 10);	/* 1K */
482711539SChunli.Zhang@Sun.COM int zcr_blksz_max = (1 << 17);	/* 128K */
482811539SChunli.Zhang@Sun.COM 
482911539SChunli.Zhang@Sun.COM /*ARGSUSED*/
483011539SChunli.Zhang@Sun.COM static int
483111539SChunli.Zhang@Sun.COM zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
483211539SChunli.Zhang@Sun.COM     caller_context_t *ct)
483311539SChunli.Zhang@Sun.COM {
483411539SChunli.Zhang@Sun.COM 	znode_t	*zp = VTOZ(vp);
483511539SChunli.Zhang@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
483611539SChunli.Zhang@Sun.COM 	int max_blksz = zfsvfs->z_max_blksz;
483711539SChunli.Zhang@Sun.COM 	uio_t *uio = &xuio->xu_uio;
483811539SChunli.Zhang@Sun.COM 	ssize_t size = uio->uio_resid;
483911539SChunli.Zhang@Sun.COM 	offset_t offset = uio->uio_loffset;
484011539SChunli.Zhang@Sun.COM 	int blksz;
484111539SChunli.Zhang@Sun.COM 	int fullblk, i;
484211539SChunli.Zhang@Sun.COM 	arc_buf_t *abuf;
484311539SChunli.Zhang@Sun.COM 	ssize_t maxsize;
484411539SChunli.Zhang@Sun.COM 	int preamble, postamble;
484511539SChunli.Zhang@Sun.COM 
484611539SChunli.Zhang@Sun.COM 	if (xuio->xu_type != UIOTYPE_ZEROCOPY)
484711539SChunli.Zhang@Sun.COM 		return (EINVAL);
484811539SChunli.Zhang@Sun.COM 
484911539SChunli.Zhang@Sun.COM 	ZFS_ENTER(zfsvfs);
485011539SChunli.Zhang@Sun.COM 	ZFS_VERIFY_ZP(zp);
485111539SChunli.Zhang@Sun.COM 	switch (ioflag) {
485211539SChunli.Zhang@Sun.COM 	case UIO_WRITE:
485311539SChunli.Zhang@Sun.COM 		/*
485411539SChunli.Zhang@Sun.COM 		 * Loan out an arc_buf for write if write size is bigger than
485511539SChunli.Zhang@Sun.COM 		 * max_blksz, and the file's block size is also max_blksz.
485611539SChunli.Zhang@Sun.COM 		 */
485711539SChunli.Zhang@Sun.COM 		blksz = max_blksz;
485811539SChunli.Zhang@Sun.COM 		if (size < blksz || zp->z_blksz != blksz) {
485911539SChunli.Zhang@Sun.COM 			ZFS_EXIT(zfsvfs);
486011539SChunli.Zhang@Sun.COM 			return (EINVAL);
486111539SChunli.Zhang@Sun.COM 		}
486211539SChunli.Zhang@Sun.COM 		/*
486311539SChunli.Zhang@Sun.COM 		 * Caller requests buffers for write before knowing where the
486411539SChunli.Zhang@Sun.COM 		 * write offset might be (e.g. NFS TCP write).
486511539SChunli.Zhang@Sun.COM 		 */
486611539SChunli.Zhang@Sun.COM 		if (offset == -1) {
486711539SChunli.Zhang@Sun.COM 			preamble = 0;
486811539SChunli.Zhang@Sun.COM 		} else {
486911539SChunli.Zhang@Sun.COM 			preamble = P2PHASE(offset, blksz);
487011539SChunli.Zhang@Sun.COM 			if (preamble) {
487111539SChunli.Zhang@Sun.COM 				preamble = blksz - preamble;
487211539SChunli.Zhang@Sun.COM 				size -= preamble;
487311539SChunli.Zhang@Sun.COM 			}
487411539SChunli.Zhang@Sun.COM 		}
487511539SChunli.Zhang@Sun.COM 
487611539SChunli.Zhang@Sun.COM 		postamble = P2PHASE(size, blksz);
487711539SChunli.Zhang@Sun.COM 		size -= postamble;
487811539SChunli.Zhang@Sun.COM 
487911539SChunli.Zhang@Sun.COM 		fullblk = size / blksz;
488011576SSurya.Prakki@Sun.COM 		(void) dmu_xuio_init(xuio,
488111539SChunli.Zhang@Sun.COM 		    (preamble != 0) + fullblk + (postamble != 0));
488211539SChunli.Zhang@Sun.COM 		DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
488311539SChunli.Zhang@Sun.COM 		    int, postamble, int,
488411539SChunli.Zhang@Sun.COM 		    (preamble != 0) + fullblk + (postamble != 0));
488511539SChunli.Zhang@Sun.COM 
488611539SChunli.Zhang@Sun.COM 		/*
488711539SChunli.Zhang@Sun.COM 		 * Have to fix iov base/len for partial buffers.  They
488811539SChunli.Zhang@Sun.COM 		 * currently represent full arc_buf's.
488911539SChunli.Zhang@Sun.COM 		 */
489011539SChunli.Zhang@Sun.COM 		if (preamble) {
489111539SChunli.Zhang@Sun.COM 			/* data begins in the middle of the arc_buf */
489211935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
489311935SMark.Shellenbaum@Sun.COM 			    blksz);
489411539SChunli.Zhang@Sun.COM 			ASSERT(abuf);
489511576SSurya.Prakki@Sun.COM 			(void) dmu_xuio_add(xuio, abuf,
489611576SSurya.Prakki@Sun.COM 			    blksz - preamble, preamble);
489711539SChunli.Zhang@Sun.COM 		}
489811539SChunli.Zhang@Sun.COM 
489911539SChunli.Zhang@Sun.COM 		for (i = 0; i < fullblk; i++) {
490011935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
490111935SMark.Shellenbaum@Sun.COM 			    blksz);
490211539SChunli.Zhang@Sun.COM 			ASSERT(abuf);
490311576SSurya.Prakki@Sun.COM 			(void) dmu_xuio_add(xuio, abuf, 0, blksz);
490411539SChunli.Zhang@Sun.COM 		}
490511539SChunli.Zhang@Sun.COM 
490611539SChunli.Zhang@Sun.COM 		if (postamble) {
490711539SChunli.Zhang@Sun.COM 			/* data ends in the middle of the arc_buf */
490811935SMark.Shellenbaum@Sun.COM 			abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
490911935SMark.Shellenbaum@Sun.COM 			    blksz);
491011539SChunli.Zhang@Sun.COM 			ASSERT(abuf);
491111576SSurya.Prakki@Sun.COM 			(void) dmu_xuio_add(xuio, abuf, 0, postamble);
491211539SChunli.Zhang@Sun.COM 		}
491311539SChunli.Zhang@Sun.COM 		break;
491411539SChunli.Zhang@Sun.COM 	case UIO_READ:
491511539SChunli.Zhang@Sun.COM 		/*
491611539SChunli.Zhang@Sun.COM 		 * Loan out an arc_buf for read if the read size is larger than
491711539SChunli.Zhang@Sun.COM 		 * the current file block size.  Block alignment is not
491811539SChunli.Zhang@Sun.COM 		 * considered.  Partial arc_buf will be loaned out for read.
491911539SChunli.Zhang@Sun.COM 		 */
492011539SChunli.Zhang@Sun.COM 		blksz = zp->z_blksz;
492111539SChunli.Zhang@Sun.COM 		if (blksz < zcr_blksz_min)
492211539SChunli.Zhang@Sun.COM 			blksz = zcr_blksz_min;
492311539SChunli.Zhang@Sun.COM 		if (blksz > zcr_blksz_max)
492411539SChunli.Zhang@Sun.COM 			blksz = zcr_blksz_max;
492511539SChunli.Zhang@Sun.COM 		/* avoid potential complexity of dealing with it */
492611539SChunli.Zhang@Sun.COM 		if (blksz > max_blksz) {
492711539SChunli.Zhang@Sun.COM 			ZFS_EXIT(zfsvfs);
492811539SChunli.Zhang@Sun.COM 			return (EINVAL);
492911539SChunli.Zhang@Sun.COM 		}
493011539SChunli.Zhang@Sun.COM 
493111935SMark.Shellenbaum@Sun.COM 		maxsize = zp->z_size - uio->uio_loffset;
493211539SChunli.Zhang@Sun.COM 		if (size > maxsize)
493311539SChunli.Zhang@Sun.COM 			size = maxsize;
493411539SChunli.Zhang@Sun.COM 
493511539SChunli.Zhang@Sun.COM 		if (size < blksz || vn_has_cached_data(vp)) {
493611539SChunli.Zhang@Sun.COM 			ZFS_EXIT(zfsvfs);
493711539SChunli.Zhang@Sun.COM 			return (EINVAL);
493811539SChunli.Zhang@Sun.COM 		}
493911539SChunli.Zhang@Sun.COM 		break;
494011539SChunli.Zhang@Sun.COM 	default:
494111539SChunli.Zhang@Sun.COM 		ZFS_EXIT(zfsvfs);
494211539SChunli.Zhang@Sun.COM 		return (EINVAL);
494311539SChunli.Zhang@Sun.COM 	}
494411539SChunli.Zhang@Sun.COM 
494511539SChunli.Zhang@Sun.COM 	uio->uio_extflg = UIO_XUIO;
494611539SChunli.Zhang@Sun.COM 	XUIO_XUZC_RW(xuio) = ioflag;
494711539SChunli.Zhang@Sun.COM 	ZFS_EXIT(zfsvfs);
494811539SChunli.Zhang@Sun.COM 	return (0);
494911539SChunli.Zhang@Sun.COM }
495011539SChunli.Zhang@Sun.COM 
495111539SChunli.Zhang@Sun.COM /*ARGSUSED*/
495211539SChunli.Zhang@Sun.COM static int
495311539SChunli.Zhang@Sun.COM zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
495411539SChunli.Zhang@Sun.COM {
495511539SChunli.Zhang@Sun.COM 	int i;
495611539SChunli.Zhang@Sun.COM 	arc_buf_t *abuf;
495711539SChunli.Zhang@Sun.COM 	int ioflag = XUIO_XUZC_RW(xuio);
495811539SChunli.Zhang@Sun.COM 
495911539SChunli.Zhang@Sun.COM 	ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
496011539SChunli.Zhang@Sun.COM 
496111539SChunli.Zhang@Sun.COM 	i = dmu_xuio_cnt(xuio);
496211539SChunli.Zhang@Sun.COM 	while (i-- > 0) {
496311539SChunli.Zhang@Sun.COM 		abuf = dmu_xuio_arcbuf(xuio, i);
496411539SChunli.Zhang@Sun.COM 		/*
496511539SChunli.Zhang@Sun.COM 		 * if abuf == NULL, it must be a write buffer
496611539SChunli.Zhang@Sun.COM 		 * that has been returned in zfs_write().
496711539SChunli.Zhang@Sun.COM 		 */
496811539SChunli.Zhang@Sun.COM 		if (abuf)
496911539SChunli.Zhang@Sun.COM 			dmu_return_arcbuf(abuf);
497011539SChunli.Zhang@Sun.COM 		ASSERT(abuf || ioflag == UIO_WRITE);
497111539SChunli.Zhang@Sun.COM 	}
497211539SChunli.Zhang@Sun.COM 
497311539SChunli.Zhang@Sun.COM 	dmu_xuio_fini(xuio);
497411539SChunli.Zhang@Sun.COM 	return (0);
497511539SChunli.Zhang@Sun.COM }
497611539SChunli.Zhang@Sun.COM 
497711539SChunli.Zhang@Sun.COM /*
4978789Sahrens  * Predeclare these here so that the compiler assumes that
4979789Sahrens  * this is an "old style" function declaration that does
4980789Sahrens  * not include arguments => we won't get type mismatch errors
4981789Sahrens  * in the initializations that follow.
4982789Sahrens  */
4983789Sahrens static int zfs_inval();
4984789Sahrens static int zfs_isdir();
4985789Sahrens 
4986789Sahrens static int
4987789Sahrens zfs_inval()
4988789Sahrens {
4989789Sahrens 	return (EINVAL);
4990789Sahrens }
4991789Sahrens 
4992789Sahrens static int
4993789Sahrens zfs_isdir()
4994789Sahrens {
4995789Sahrens 	return (EISDIR);
4996789Sahrens }
4997789Sahrens /*
4998789Sahrens  * Directory vnode operations template
4999789Sahrens  */
5000789Sahrens vnodeops_t *zfs_dvnodeops;
5001789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = {
50023898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
50033898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
50043898Srsb 	VOPNAME_READ,		{ .error = zfs_isdir },
50053898Srsb 	VOPNAME_WRITE,		{ .error = zfs_isdir },
50063898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
50073898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
50083898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
50093898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
50103898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
50113898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
50123898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
50133898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
50143898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
50153898Srsb 	VOPNAME_MKDIR,		{ .vop_mkdir = zfs_mkdir },
50163898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
50173898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
50183898Srsb 	VOPNAME_SYMLINK,	{ .vop_symlink = zfs_symlink },
50193898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
50203898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
50213898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
50223898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
50233898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50243898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
50253898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
50264863Spraks 	VOPNAME_VNEVENT, 	{ .vop_vnevent = fs_vnevent_support },
50273898Srsb 	NULL,			NULL
5028789Sahrens };
5029789Sahrens 
5030789Sahrens /*
5031789Sahrens  * Regular file vnode operations template
5032789Sahrens  */
5033789Sahrens vnodeops_t *zfs_fvnodeops;
5034789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = {
50353898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
50363898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
50373898Srsb 	VOPNAME_READ,		{ .vop_read = zfs_read },
50383898Srsb 	VOPNAME_WRITE,		{ .vop_write = zfs_write },
50393898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
50403898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
50413898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
50423898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
50433898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
50443898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
50453898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
50463898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
50473898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
50483898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
50493898Srsb 	VOPNAME_FRLOCK,		{ .vop_frlock = zfs_frlock },
50503898Srsb 	VOPNAME_SPACE,		{ .vop_space = zfs_space },
50513898Srsb 	VOPNAME_GETPAGE,	{ .vop_getpage = zfs_getpage },
50523898Srsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = zfs_putpage },
50533898Srsb 	VOPNAME_MAP,		{ .vop_map = zfs_map },
50543898Srsb 	VOPNAME_ADDMAP,		{ .vop_addmap = zfs_addmap },
50553898Srsb 	VOPNAME_DELMAP,		{ .vop_delmap = zfs_delmap },
50563898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50573898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
50583898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
50593898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
506011539SChunli.Zhang@Sun.COM 	VOPNAME_REQZCBUF, 	{ .vop_reqzcbuf = zfs_reqzcbuf },
506111539SChunli.Zhang@Sun.COM 	VOPNAME_RETZCBUF, 	{ .vop_retzcbuf = zfs_retzcbuf },
50623898Srsb 	NULL,			NULL
5063789Sahrens };
5064789Sahrens 
5065789Sahrens /*
5066789Sahrens  * Symbolic link vnode operations template
5067789Sahrens  */
5068789Sahrens vnodeops_t *zfs_symvnodeops;
5069789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = {
50703898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
50713898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
50723898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
50733898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
50743898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = zfs_readlink },
50753898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
50763898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
50773898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50783898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
50793898Srsb 	NULL,			NULL
5080789Sahrens };
5081789Sahrens 
5082789Sahrens /*
50838845Samw@Sun.COM  * special share hidden files vnode operations template
50848845Samw@Sun.COM  */
50858845Samw@Sun.COM vnodeops_t *zfs_sharevnodeops;
50868845Samw@Sun.COM const fs_operation_def_t zfs_sharevnodeops_template[] = {
50878845Samw@Sun.COM 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
50888845Samw@Sun.COM 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
50898845Samw@Sun.COM 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
50908845Samw@Sun.COM 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
50918845Samw@Sun.COM 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
50928845Samw@Sun.COM 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
50938845Samw@Sun.COM 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
50948845Samw@Sun.COM 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
50958845Samw@Sun.COM 	NULL,			NULL
50968845Samw@Sun.COM };
50978845Samw@Sun.COM 
50988845Samw@Sun.COM /*
5099789Sahrens  * Extended attribute directory vnode operations template
5100789Sahrens  *	This template is identical to the directory vnodes
5101789Sahrens  *	operation template except for restricted operations:
5102789Sahrens  *		VOP_MKDIR()
5103789Sahrens  *		VOP_SYMLINK()
5104789Sahrens  * Note that there are other restrictions embedded in:
5105789Sahrens  *	zfs_create()	- restrict type to VREG
5106789Sahrens  *	zfs_link()	- no links into/out of attribute space
5107789Sahrens  *	zfs_rename()	- no moves into/out of attribute space
5108789Sahrens  */
5109789Sahrens vnodeops_t *zfs_xdvnodeops;
5110789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = {
51113898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
51123898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
51133898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
51143898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
51153898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
51163898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
51173898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
51183898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
51193898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
51203898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
51213898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
51223898Srsb 	VOPNAME_MKDIR,		{ .error = zfs_inval },
51233898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
51243898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
51253898Srsb 	VOPNAME_SYMLINK,	{ .error = zfs_inval },
51263898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
51273898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
51283898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
51293898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
51303898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
51313898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
51323898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
51333898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
51343898Srsb 	NULL,			NULL
5135789Sahrens };
5136789Sahrens 
5137789Sahrens /*
5138789Sahrens  * Error vnode operations template
5139789Sahrens  */
5140789Sahrens vnodeops_t *zfs_evnodeops;
5141789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = {
51423898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
51433898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
51443898Srsb 	NULL,			NULL
5145789Sahrens };
5146