xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 5545:62533366078f)
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 /*
223461Sahrens  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
264144Speteh /* Portions Copyright 2007 Jeremy Teo */
274144Speteh 
28789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
29789Sahrens 
30789Sahrens #include <sys/types.h>
31789Sahrens #include <sys/param.h>
32789Sahrens #include <sys/time.h>
33789Sahrens #include <sys/systm.h>
34789Sahrens #include <sys/sysmacros.h>
35789Sahrens #include <sys/resource.h>
36789Sahrens #include <sys/vfs.h>
373898Srsb #include <sys/vfs_opreg.h>
38789Sahrens #include <sys/vnode.h>
39789Sahrens #include <sys/file.h>
40789Sahrens #include <sys/stat.h>
41789Sahrens #include <sys/kmem.h>
42789Sahrens #include <sys/taskq.h>
43789Sahrens #include <sys/uio.h>
44789Sahrens #include <sys/vmsystm.h>
45789Sahrens #include <sys/atomic.h>
462688Smaybee #include <sys/vm.h>
47789Sahrens #include <vm/seg_vn.h>
48789Sahrens #include <vm/pvn.h>
49789Sahrens #include <vm/as.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>
60789Sahrens #include <sys/spa.h>
61789Sahrens #include <sys/txg.h>
62789Sahrens #include <sys/dbuf.h>
63789Sahrens #include <sys/zap.h>
64789Sahrens #include <sys/dirent.h>
65789Sahrens #include <sys/policy.h>
66789Sahrens #include <sys/sunddi.h>
67789Sahrens #include <sys/filio.h>
68789Sahrens #include "fs/fs_subr.h"
69789Sahrens #include <sys/zfs_ctldir.h>
705331Samw #include <sys/zfs_fuid.h>
711484Sek110237 #include <sys/dnlc.h>
721669Sperrin #include <sys/zfs_rlock.h>
735331Samw #include <sys/extdirent.h>
745331Samw #include <sys/kidmap.h>
755331Samw #include <sys/cred_impl.h>
76789Sahrens 
77789Sahrens /*
78789Sahrens  * Programming rules.
79789Sahrens  *
80789Sahrens  * Each vnode op performs some logical unit of work.  To do this, the ZPL must
81789Sahrens  * properly lock its in-core state, create a DMU transaction, do the work,
82789Sahrens  * record this work in the intent log (ZIL), commit the DMU transaction,
835331Samw  * and wait for the intent log to commit if it is a synchronous operation.
845331Samw  * Moreover, the vnode ops must work in both normal and log replay context.
85789Sahrens  * The ordering of events is important to avoid deadlocks and references
86789Sahrens  * to freed memory.  The example below illustrates the following Big Rules:
87789Sahrens  *
88789Sahrens  *  (1) A check must be made in each zfs thread for a mounted file system.
895367Sahrens  *	This is done avoiding races using ZFS_ENTER(zfsvfs).
905367Sahrens  *      A ZFS_EXIT(zfsvfs) is needed before all returns.  Any znodes
915367Sahrens  *      must be checked with ZFS_VERIFY_ZP(zp).  Both of these macros
925367Sahrens  *      can return EIO from the calling function.
93789Sahrens  *
94789Sahrens  *  (2)	VN_RELE() should always be the last thing except for zil_commit()
952638Sperrin  *	(if necessary) and ZFS_EXIT(). This is for 3 reasons:
96789Sahrens  *	First, if it's the last reference, the vnode/znode
97789Sahrens  *	can be freed, so the zp may point to freed memory.  Second, the last
98789Sahrens  *	reference will call zfs_zinactive(), which may induce a lot of work --
991669Sperrin  *	pushing cached pages (which acquires range locks) and syncing out
100789Sahrens  *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
101789Sahrens  *	which could deadlock the system if you were already holding one.
102789Sahrens  *
1031757Sperrin  *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
1041757Sperrin  *	as they can span dmu_tx_assign() calls.
1051757Sperrin  *
1061757Sperrin  *  (4)	Always pass zfsvfs->z_assign as the second argument to dmu_tx_assign().
107789Sahrens  *	In normal operation, this will be TXG_NOWAIT.  During ZIL replay,
108789Sahrens  *	it will be a specific txg.  Either way, dmu_tx_assign() never blocks.
109789Sahrens  *	This is critical because we don't want to block while holding locks.
110789Sahrens  *	Note, in particular, that if a lock is sometimes acquired before
111789Sahrens  *	the tx assigns, and sometimes after (e.g. z_lock), then failing to
112789Sahrens  *	use a non-blocking assign can deadlock the system.  The scenario:
113789Sahrens  *
114789Sahrens  *	Thread A has grabbed a lock before calling dmu_tx_assign().
115789Sahrens  *	Thread B is in an already-assigned tx, and blocks for this lock.
116789Sahrens  *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
117789Sahrens  *	forever, because the previous txg can't quiesce until B's tx commits.
118789Sahrens  *
119789Sahrens  *	If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
1202113Sahrens  *	then drop all locks, call dmu_tx_wait(), and try again.
121789Sahrens  *
1221757Sperrin  *  (5)	If the operation succeeded, generate the intent log entry for it
123789Sahrens  *	before dropping locks.  This ensures that the ordering of events
124789Sahrens  *	in the intent log matches the order in which they actually occurred.
125789Sahrens  *
1261757Sperrin  *  (6)	At the end of each vnode op, the DMU tx must always commit,
127789Sahrens  *	regardless of whether there were any errors.
128789Sahrens  *
1292638Sperrin  *  (7)	After dropping all locks, invoke zil_commit(zilog, seq, foid)
130789Sahrens  *	to ensure that synchronous semantics are provided when necessary.
131789Sahrens  *
132789Sahrens  * In general, this is how things should be ordered in each vnode op:
133789Sahrens  *
134789Sahrens  *	ZFS_ENTER(zfsvfs);		// exit if unmounted
135789Sahrens  * top:
136789Sahrens  *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
137789Sahrens  *	rw_enter(...);			// grab any other locks you need
138789Sahrens  *	tx = dmu_tx_create(...);	// get DMU tx
139789Sahrens  *	dmu_tx_hold_*();		// hold each object you might modify
140789Sahrens  *	error = dmu_tx_assign(tx, zfsvfs->z_assign);	// try to assign
141789Sahrens  *	if (error) {
142789Sahrens  *		rw_exit(...);		// drop locks
143789Sahrens  *		zfs_dirent_unlock(dl);	// unlock directory entry
144789Sahrens  *		VN_RELE(...);		// release held vnodes
145789Sahrens  *		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1462113Sahrens  *			dmu_tx_wait(tx);
1472113Sahrens  *			dmu_tx_abort(tx);
148789Sahrens  *			goto top;
149789Sahrens  *		}
1502113Sahrens  *		dmu_tx_abort(tx);	// abort DMU tx
151789Sahrens  *		ZFS_EXIT(zfsvfs);	// finished in zfs
152789Sahrens  *		return (error);		// really out of space
153789Sahrens  *	}
154789Sahrens  *	error = do_real_work();		// do whatever this VOP does
155789Sahrens  *	if (error == 0)
1562638Sperrin  *		zfs_log_*(...);		// on success, make ZIL entry
157789Sahrens  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
158789Sahrens  *	rw_exit(...);			// drop locks
159789Sahrens  *	zfs_dirent_unlock(dl);		// unlock directory entry
160789Sahrens  *	VN_RELE(...);			// release held vnodes
1612638Sperrin  *	zil_commit(zilog, seq, foid);	// synchronous when necessary
162789Sahrens  *	ZFS_EXIT(zfsvfs);		// finished in zfs
163789Sahrens  *	return (error);			// done, report error
164789Sahrens  */
1655367Sahrens 
166789Sahrens /* ARGSUSED */
167789Sahrens static int
1685331Samw zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
169789Sahrens {
1703063Sperrin 	znode_t	*zp = VTOZ(*vpp);
1713063Sperrin 
1725331Samw 	if ((flag & FWRITE) && (zp->z_phys->zp_flags & ZFS_APPENDONLY) &&
1735331Samw 	    ((flag & FAPPEND) == 0)) {
1745331Samw 		return (EPERM);
1755331Samw 	}
1765331Samw 
1775331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
1785331Samw 	    ZTOV(zp)->v_type == VREG &&
1795331Samw 	    !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
1805331Samw 	    zp->z_phys->zp_size > 0)
1815331Samw 		if (fs_vscan(*vpp, cr, 0) != 0)
1825331Samw 			return (EACCES);
1835331Samw 
1843063Sperrin 	/* Keep a count of the synchronous opens in the znode */
1853063Sperrin 	if (flag & (FSYNC | FDSYNC))
1863063Sperrin 		atomic_inc_32(&zp->z_sync_cnt);
1875331Samw 
188789Sahrens 	return (0);
189789Sahrens }
190789Sahrens 
191789Sahrens /* ARGSUSED */
192789Sahrens static int
1935331Samw zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
1945331Samw     caller_context_t *ct)
195789Sahrens {
1963063Sperrin 	znode_t	*zp = VTOZ(vp);
1973063Sperrin 
1983063Sperrin 	/* Decrement the synchronous opens in the znode */
1994339Sperrin 	if ((flag & (FSYNC | FDSYNC)) && (count == 1))
2003063Sperrin 		atomic_dec_32(&zp->z_sync_cnt);
2013063Sperrin 
202789Sahrens 	/*
203789Sahrens 	 * Clean up any locks held by this process on the vp.
204789Sahrens 	 */
205789Sahrens 	cleanlocks(vp, ddi_get_pid(), 0);
206789Sahrens 	cleanshares(vp, ddi_get_pid());
207789Sahrens 
2085331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
2095331Samw 	    ZTOV(zp)->v_type == VREG &&
2105331Samw 	    !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
2115331Samw 	    zp->z_phys->zp_size > 0)
2125331Samw 		VERIFY(fs_vscan(vp, cr, 1) == 0);
2135331Samw 
214789Sahrens 	return (0);
215789Sahrens }
216789Sahrens 
217789Sahrens /*
218789Sahrens  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
219789Sahrens  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
220789Sahrens  */
221789Sahrens static int
222789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off)
223789Sahrens {
224789Sahrens 	znode_t	*zp = VTOZ(vp);
225789Sahrens 	uint64_t noff = (uint64_t)*off; /* new offset */
226789Sahrens 	uint64_t file_sz;
227789Sahrens 	int error;
228789Sahrens 	boolean_t hole;
229789Sahrens 
230789Sahrens 	file_sz = zp->z_phys->zp_size;
231789Sahrens 	if (noff >= file_sz)  {
232789Sahrens 		return (ENXIO);
233789Sahrens 	}
234789Sahrens 
235789Sahrens 	if (cmd == _FIO_SEEK_HOLE)
236789Sahrens 		hole = B_TRUE;
237789Sahrens 	else
238789Sahrens 		hole = B_FALSE;
239789Sahrens 
240789Sahrens 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
241789Sahrens 
242789Sahrens 	/* end of file? */
243789Sahrens 	if ((error == ESRCH) || (noff > file_sz)) {
244789Sahrens 		/*
245789Sahrens 		 * Handle the virtual hole at the end of file.
246789Sahrens 		 */
247789Sahrens 		if (hole) {
248789Sahrens 			*off = file_sz;
249789Sahrens 			return (0);
250789Sahrens 		}
251789Sahrens 		return (ENXIO);
252789Sahrens 	}
253789Sahrens 
254789Sahrens 	if (noff < *off)
255789Sahrens 		return (error);
256789Sahrens 	*off = noff;
257789Sahrens 	return (error);
258789Sahrens }
259789Sahrens 
260789Sahrens /* ARGSUSED */
261789Sahrens static int
262789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
2635331Samw     int *rvalp, caller_context_t *ct)
264789Sahrens {
265789Sahrens 	offset_t off;
266789Sahrens 	int error;
267789Sahrens 	zfsvfs_t *zfsvfs;
2685326Sek110237 	znode_t *zp;
269789Sahrens 
270789Sahrens 	switch (com) {
2714339Sperrin 	case _FIOFFS:
272789Sahrens 		return (zfs_sync(vp->v_vfsp, 0, cred));
273789Sahrens 
2741544Seschrock 		/*
2751544Seschrock 		 * The following two ioctls are used by bfu.  Faking out,
2761544Seschrock 		 * necessary to avoid bfu errors.
2771544Seschrock 		 */
2784339Sperrin 	case _FIOGDIO:
2794339Sperrin 	case _FIOSDIO:
2801544Seschrock 		return (0);
2811544Seschrock 
2824339Sperrin 	case _FIO_SEEK_DATA:
2834339Sperrin 	case _FIO_SEEK_HOLE:
284789Sahrens 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
285789Sahrens 			return (EFAULT);
286789Sahrens 
2875326Sek110237 		zp = VTOZ(vp);
2885326Sek110237 		zfsvfs = zp->z_zfsvfs;
2895367Sahrens 		ZFS_ENTER(zfsvfs);
2905367Sahrens 		ZFS_VERIFY_ZP(zp);
291789Sahrens 
292789Sahrens 		/* offset parameter is in/out */
293789Sahrens 		error = zfs_holey(vp, com, &off);
294789Sahrens 		ZFS_EXIT(zfsvfs);
295789Sahrens 		if (error)
296789Sahrens 			return (error);
297789Sahrens 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
298789Sahrens 			return (EFAULT);
299789Sahrens 		return (0);
300789Sahrens 	}
301789Sahrens 	return (ENOTTY);
302789Sahrens }
303789Sahrens 
304789Sahrens /*
305789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
306789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
307789Sahrens  *
308789Sahrens  * On Write:	If we find a memory mapped page, we write to *both*
309789Sahrens  *		the page and the dmu buffer.
310789Sahrens  *
311789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
312789Sahrens  *	the file is memory mapped.
313789Sahrens  */
314789Sahrens static int
3153638Sbillm mappedwrite(vnode_t *vp, int nbytes, uio_t *uio, dmu_tx_t *tx)
316789Sahrens {
317789Sahrens 	znode_t	*zp = VTOZ(vp);
318789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
319789Sahrens 	int64_t	start, off;
320789Sahrens 	int len = nbytes;
321789Sahrens 	int error = 0;
322789Sahrens 
323789Sahrens 	start = uio->uio_loffset;
324789Sahrens 	off = start & PAGEOFFSET;
325789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
326789Sahrens 		page_t *pp;
327789Sahrens 		uint64_t bytes = MIN(PAGESIZE - off, len);
3283638Sbillm 		uint64_t woff = uio->uio_loffset;
329789Sahrens 
330789Sahrens 		/*
331789Sahrens 		 * We don't want a new page to "appear" in the middle of
332789Sahrens 		 * the file update (because it may not get the write
333789Sahrens 		 * update data), so we grab a lock to block
334789Sahrens 		 * zfs_getpage().
335789Sahrens 		 */
336789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
337789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
338789Sahrens 			caddr_t va;
339789Sahrens 
340789Sahrens 			rw_exit(&zp->z_map_lock);
341789Sahrens 			va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L);
342789Sahrens 			error = uiomove(va+off, bytes, UIO_WRITE, uio);
343789Sahrens 			if (error == 0) {
344789Sahrens 				dmu_write(zfsvfs->z_os, zp->z_id,
345789Sahrens 				    woff, bytes, va+off, tx);
346789Sahrens 			}
347789Sahrens 			ppmapout(va);
348789Sahrens 			page_unlock(pp);
349789Sahrens 		} else {
350789Sahrens 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
3513638Sbillm 			    uio, bytes, tx);
352789Sahrens 			rw_exit(&zp->z_map_lock);
353789Sahrens 		}
354789Sahrens 		len -= bytes;
355789Sahrens 		off = 0;
356789Sahrens 		if (error)
357789Sahrens 			break;
358789Sahrens 	}
359789Sahrens 	return (error);
360789Sahrens }
361789Sahrens 
362789Sahrens /*
363789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
364789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
365789Sahrens  *
366789Sahrens  * On Read:	We "read" preferentially from memory mapped pages,
367789Sahrens  *		else we default from the dmu buffer.
368789Sahrens  *
369789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
370789Sahrens  *	the file is memory mapped.
371789Sahrens  */
372789Sahrens static int
3733638Sbillm mappedread(vnode_t *vp, int nbytes, uio_t *uio)
374789Sahrens {
3753638Sbillm 	znode_t *zp = VTOZ(vp);
3763638Sbillm 	objset_t *os = zp->z_zfsvfs->z_os;
3773638Sbillm 	int64_t	start, off;
378789Sahrens 	int len = nbytes;
379789Sahrens 	int error = 0;
380789Sahrens 
381789Sahrens 	start = uio->uio_loffset;
382789Sahrens 	off = start & PAGEOFFSET;
383789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
384789Sahrens 		page_t *pp;
3853638Sbillm 		uint64_t bytes = MIN(PAGESIZE - off, len);
3863638Sbillm 
387789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
388789Sahrens 			caddr_t va;
389789Sahrens 
3902688Smaybee 			va = ppmapin(pp, PROT_READ, (caddr_t)-1L);
391789Sahrens 			error = uiomove(va + off, bytes, UIO_READ, uio);
392789Sahrens 			ppmapout(va);
393789Sahrens 			page_unlock(pp);
394789Sahrens 		} else {
3953638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, bytes);
396789Sahrens 		}
397789Sahrens 		len -= bytes;
398789Sahrens 		off = 0;
399789Sahrens 		if (error)
400789Sahrens 			break;
401789Sahrens 	}
402789Sahrens 	return (error);
403789Sahrens }
404789Sahrens 
4053638Sbillm offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
406789Sahrens 
407789Sahrens /*
408789Sahrens  * Read bytes from specified file into supplied buffer.
409789Sahrens  *
410789Sahrens  *	IN:	vp	- vnode of file to be read from.
411789Sahrens  *		uio	- structure supplying read location, range info,
412789Sahrens  *			  and return buffer.
413789Sahrens  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
414789Sahrens  *		cr	- credentials of caller.
4155331Samw  *		ct	- caller context
416789Sahrens  *
417789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
418789Sahrens  *
419789Sahrens  *	RETURN:	0 if success
420789Sahrens  *		error code if failure
421789Sahrens  *
422789Sahrens  * Side Effects:
423789Sahrens  *	vp - atime updated if byte count > 0
424789Sahrens  */
425789Sahrens /* ARGSUSED */
426789Sahrens static int
427789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
428789Sahrens {
429789Sahrens 	znode_t		*zp = VTOZ(vp);
430789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4315326Sek110237 	objset_t	*os;
4323638Sbillm 	ssize_t		n, nbytes;
4333638Sbillm 	int		error;
4341669Sperrin 	rl_t		*rl;
435789Sahrens 
4365367Sahrens 	ZFS_ENTER(zfsvfs);
4375367Sahrens 	ZFS_VERIFY_ZP(zp);
4385326Sek110237 	os = zfsvfs->z_os;
439789Sahrens 
440789Sahrens 	/*
441789Sahrens 	 * Validate file offset
442789Sahrens 	 */
443789Sahrens 	if (uio->uio_loffset < (offset_t)0) {
444789Sahrens 		ZFS_EXIT(zfsvfs);
445789Sahrens 		return (EINVAL);
446789Sahrens 	}
447789Sahrens 
448789Sahrens 	/*
449789Sahrens 	 * Fasttrack empty reads
450789Sahrens 	 */
451789Sahrens 	if (uio->uio_resid == 0) {
452789Sahrens 		ZFS_EXIT(zfsvfs);
453789Sahrens 		return (0);
454789Sahrens 	}
455789Sahrens 
456789Sahrens 	/*
4571669Sperrin 	 * Check for mandatory locks
458789Sahrens 	 */
459789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
460789Sahrens 		if (error = chklock(vp, FREAD,
461789Sahrens 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
462789Sahrens 			ZFS_EXIT(zfsvfs);
463789Sahrens 			return (error);
464789Sahrens 		}
465789Sahrens 	}
466789Sahrens 
467789Sahrens 	/*
468789Sahrens 	 * If we're in FRSYNC mode, sync out this znode before reading it.
469789Sahrens 	 */
4702638Sperrin 	if (ioflag & FRSYNC)
4712638Sperrin 		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
472789Sahrens 
473789Sahrens 	/*
4741669Sperrin 	 * Lock the range against changes.
475789Sahrens 	 */
4761669Sperrin 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
4771669Sperrin 
478789Sahrens 	/*
479789Sahrens 	 * If we are reading past end-of-file we can skip
480789Sahrens 	 * to the end; but we might still need to set atime.
481789Sahrens 	 */
482789Sahrens 	if (uio->uio_loffset >= zp->z_phys->zp_size) {
483789Sahrens 		error = 0;
484789Sahrens 		goto out;
485789Sahrens 	}
486789Sahrens 
4873638Sbillm 	ASSERT(uio->uio_loffset < zp->z_phys->zp_size);
4883638Sbillm 	n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset);
4893638Sbillm 
4903638Sbillm 	while (n > 0) {
4913638Sbillm 		nbytes = MIN(n, zfs_read_chunk_size -
4923638Sbillm 		    P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
4933638Sbillm 
4943638Sbillm 		if (vn_has_cached_data(vp))
4953638Sbillm 			error = mappedread(vp, nbytes, uio);
4963638Sbillm 		else
4973638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, nbytes);
4981544Seschrock 		if (error)
4993638Sbillm 			break;
5003638Sbillm 
5013638Sbillm 		n -= nbytes;
502789Sahrens 	}
5033638Sbillm 
504789Sahrens out:
5052237Smaybee 	zfs_range_unlock(rl);
506789Sahrens 
507789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
508789Sahrens 	ZFS_EXIT(zfsvfs);
509789Sahrens 	return (error);
510789Sahrens }
511789Sahrens 
512789Sahrens /*
513789Sahrens  * Fault in the pages of the first n bytes specified by the uio structure.
514789Sahrens  * 1 byte in each page is touched and the uio struct is unmodified.
515789Sahrens  * Any error will exit this routine as this is only a best
516789Sahrens  * attempt to get the pages resident. This is a copy of ufs_trans_touch().
517789Sahrens  */
518789Sahrens static void
519789Sahrens zfs_prefault_write(ssize_t n, struct uio *uio)
520789Sahrens {
521789Sahrens 	struct iovec *iov;
522789Sahrens 	ulong_t cnt, incr;
523789Sahrens 	caddr_t p;
524789Sahrens 	uint8_t tmp;
525789Sahrens 
526789Sahrens 	iov = uio->uio_iov;
527789Sahrens 
528789Sahrens 	while (n) {
529789Sahrens 		cnt = MIN(iov->iov_len, n);
530789Sahrens 		if (cnt == 0) {
531789Sahrens 			/* empty iov entry */
532789Sahrens 			iov++;
533789Sahrens 			continue;
534789Sahrens 		}
535789Sahrens 		n -= cnt;
536789Sahrens 		/*
537789Sahrens 		 * touch each page in this segment.
538789Sahrens 		 */
539789Sahrens 		p = iov->iov_base;
540789Sahrens 		while (cnt) {
541789Sahrens 			switch (uio->uio_segflg) {
542789Sahrens 			case UIO_USERSPACE:
543789Sahrens 			case UIO_USERISPACE:
544789Sahrens 				if (fuword8(p, &tmp))
545789Sahrens 					return;
546789Sahrens 				break;
547789Sahrens 			case UIO_SYSSPACE:
548789Sahrens 				if (kcopy(p, &tmp, 1))
549789Sahrens 					return;
550789Sahrens 				break;
551789Sahrens 			}
552789Sahrens 			incr = MIN(cnt, PAGESIZE);
553789Sahrens 			p += incr;
554789Sahrens 			cnt -= incr;
555789Sahrens 		}
556789Sahrens 		/*
557789Sahrens 		 * touch the last byte in case it straddles a page.
558789Sahrens 		 */
559789Sahrens 		p--;
560789Sahrens 		switch (uio->uio_segflg) {
561789Sahrens 		case UIO_USERSPACE:
562789Sahrens 		case UIO_USERISPACE:
563789Sahrens 			if (fuword8(p, &tmp))
564789Sahrens 				return;
565789Sahrens 			break;
566789Sahrens 		case UIO_SYSSPACE:
567789Sahrens 			if (kcopy(p, &tmp, 1))
568789Sahrens 				return;
569789Sahrens 			break;
570789Sahrens 		}
571789Sahrens 		iov++;
572789Sahrens 	}
573789Sahrens }
574789Sahrens 
575789Sahrens /*
576789Sahrens  * Write the bytes to a file.
577789Sahrens  *
578789Sahrens  *	IN:	vp	- vnode of file to be written to.
579789Sahrens  *		uio	- structure supplying write location, range info,
580789Sahrens  *			  and data buffer.
581789Sahrens  *		ioflag	- FAPPEND flag set if in append mode.
582789Sahrens  *		cr	- credentials of caller.
5835331Samw  *		ct	- caller context (NFS/CIFS fem monitor only)
584789Sahrens  *
585789Sahrens  *	OUT:	uio	- updated offset and range.
586789Sahrens  *
587789Sahrens  *	RETURN:	0 if success
588789Sahrens  *		error code if failure
589789Sahrens  *
590789Sahrens  * Timestamps:
591789Sahrens  *	vp - ctime|mtime updated if byte count > 0
592789Sahrens  */
593789Sahrens /* ARGSUSED */
594789Sahrens static int
595789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
596789Sahrens {
597789Sahrens 	znode_t		*zp = VTOZ(vp);
598789Sahrens 	rlim64_t	limit = uio->uio_llimit;
599789Sahrens 	ssize_t		start_resid = uio->uio_resid;
600789Sahrens 	ssize_t		tx_bytes;
601789Sahrens 	uint64_t	end_size;
602789Sahrens 	dmu_tx_t	*tx;
603789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
6045326Sek110237 	zilog_t		*zilog;
605789Sahrens 	offset_t	woff;
606789Sahrens 	ssize_t		n, nbytes;
6071669Sperrin 	rl_t		*rl;
608789Sahrens 	int		max_blksz = zfsvfs->z_max_blksz;
6095331Samw 	uint64_t	pflags = zp->z_phys->zp_flags;
6101669Sperrin 	int		error;
611789Sahrens 
612789Sahrens 	/*
6135331Samw 	 * If immutable or not appending then return EPERM
6145331Samw 	 */
6155331Samw 	if ((pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
6165331Samw 	    ((pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
6175331Samw 	    (uio->uio_loffset < zp->z_phys->zp_size)))
6185331Samw 		return (EPERM);
6195331Samw 
6205331Samw 	/*
621789Sahrens 	 * Fasttrack empty write
622789Sahrens 	 */
6231669Sperrin 	n = start_resid;
624789Sahrens 	if (n == 0)
625789Sahrens 		return (0);
626789Sahrens 
6271669Sperrin 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
6281669Sperrin 		limit = MAXOFFSET_T;
6291669Sperrin 
6305367Sahrens 	ZFS_ENTER(zfsvfs);
6315367Sahrens 	ZFS_VERIFY_ZP(zp);
6325326Sek110237 	zilog = zfsvfs->z_log;
633789Sahrens 
634789Sahrens 	/*
6352237Smaybee 	 * Pre-fault the pages to ensure slow (eg NFS) pages
6361669Sperrin 	 * don't hold up txg.
637789Sahrens 	 */
6382237Smaybee 	zfs_prefault_write(n, uio);
639789Sahrens 
640789Sahrens 	/*
641789Sahrens 	 * If in append mode, set the io offset pointer to eof.
642789Sahrens 	 */
6431669Sperrin 	if (ioflag & FAPPEND) {
6441669Sperrin 		/*
6451669Sperrin 		 * Range lock for a file append:
6461669Sperrin 		 * The value for the start of range will be determined by
6471669Sperrin 		 * zfs_range_lock() (to guarantee append semantics).
6481669Sperrin 		 * If this write will cause the block size to increase,
6491669Sperrin 		 * zfs_range_lock() will lock the entire file, so we must
6501669Sperrin 		 * later reduce the range after we grow the block size.
6511669Sperrin 		 */
6521669Sperrin 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
6531669Sperrin 		if (rl->r_len == UINT64_MAX) {
6541669Sperrin 			/* overlocked, zp_size can't change */
6551669Sperrin 			woff = uio->uio_loffset = zp->z_phys->zp_size;
6561669Sperrin 		} else {
6571669Sperrin 			woff = uio->uio_loffset = rl->r_off;
6581669Sperrin 		}
659789Sahrens 	} else {
660789Sahrens 		woff = uio->uio_loffset;
661789Sahrens 		/*
662789Sahrens 		 * Validate file offset
663789Sahrens 		 */
664789Sahrens 		if (woff < 0) {
665789Sahrens 			ZFS_EXIT(zfsvfs);
666789Sahrens 			return (EINVAL);
667789Sahrens 		}
668789Sahrens 
669789Sahrens 		/*
6701669Sperrin 		 * If we need to grow the block size then zfs_range_lock()
6711669Sperrin 		 * will lock a wider range than we request here.
6721669Sperrin 		 * Later after growing the block size we reduce the range.
673789Sahrens 		 */
6741669Sperrin 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
675789Sahrens 	}
676789Sahrens 
677789Sahrens 	if (woff >= limit) {
6783638Sbillm 		zfs_range_unlock(rl);
6793638Sbillm 		ZFS_EXIT(zfsvfs);
6803638Sbillm 		return (EFBIG);
681789Sahrens 	}
682789Sahrens 
683789Sahrens 	if ((woff + n) > limit || woff > (limit - n))
684789Sahrens 		n = limit - woff;
685789Sahrens 
686789Sahrens 	/*
6871669Sperrin 	 * Check for mandatory locks
688789Sahrens 	 */
689789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) &&
6903638Sbillm 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
6913638Sbillm 		zfs_range_unlock(rl);
6923638Sbillm 		ZFS_EXIT(zfsvfs);
6933638Sbillm 		return (error);
6943638Sbillm 	}
6951669Sperrin 	end_size = MAX(zp->z_phys->zp_size, woff + n);
696789Sahrens 
6971669Sperrin 	/*
6983638Sbillm 	 * Write the file in reasonable size chunks.  Each chunk is written
6993638Sbillm 	 * in a separate transaction; this keeps the intent log records small
7003638Sbillm 	 * and allows us to do more fine-grained space accounting.
701789Sahrens 	 */
702789Sahrens 	while (n > 0) {
703789Sahrens 		/*
7043638Sbillm 		 * Start a transaction.
705789Sahrens 		 */
706789Sahrens 		woff = uio->uio_loffset;
707789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
708789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
709789Sahrens 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
710789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
711789Sahrens 		if (error) {
712789Sahrens 			if (error == ERESTART &&
713789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
7142113Sahrens 				dmu_tx_wait(tx);
7152113Sahrens 				dmu_tx_abort(tx);
7163638Sbillm 				continue;
717789Sahrens 			}
7182113Sahrens 			dmu_tx_abort(tx);
7193638Sbillm 			break;
7203638Sbillm 		}
7213638Sbillm 
7223638Sbillm 		/*
7233638Sbillm 		 * If zfs_range_lock() over-locked we grow the blocksize
7243638Sbillm 		 * and then reduce the lock range.  This will only happen
7253638Sbillm 		 * on the first iteration since zfs_range_reduce() will
7263638Sbillm 		 * shrink down r_len to the appropriate size.
7273638Sbillm 		 */
7283638Sbillm 		if (rl->r_len == UINT64_MAX) {
7293638Sbillm 			uint64_t new_blksz;
7303638Sbillm 
7313638Sbillm 			if (zp->z_blksz > max_blksz) {
7323638Sbillm 				ASSERT(!ISP2(zp->z_blksz));
7333638Sbillm 				new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
7343638Sbillm 			} else {
7353638Sbillm 				new_blksz = MIN(end_size, max_blksz);
7363638Sbillm 			}
7373638Sbillm 			zfs_grow_blocksize(zp, new_blksz, tx);
7383638Sbillm 			zfs_range_reduce(rl, woff, n);
7393638Sbillm 		}
7403638Sbillm 
7413638Sbillm 		/*
7423638Sbillm 		 * XXX - should we really limit each write to z_max_blksz?
7433638Sbillm 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
7443638Sbillm 		 */
7453638Sbillm 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
7463638Sbillm 		rw_enter(&zp->z_map_lock, RW_READER);
7473638Sbillm 
7483638Sbillm 		tx_bytes = uio->uio_resid;
7493638Sbillm 		if (vn_has_cached_data(vp)) {
7503638Sbillm 			rw_exit(&zp->z_map_lock);
7513638Sbillm 			error = mappedwrite(vp, nbytes, uio, tx);
7523638Sbillm 		} else {
7533638Sbillm 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
7543638Sbillm 			    uio, nbytes, tx);
7553638Sbillm 			rw_exit(&zp->z_map_lock);
756789Sahrens 		}
7573638Sbillm 		tx_bytes -= uio->uio_resid;
7583638Sbillm 
7593638Sbillm 		/*
7603638Sbillm 		 * If we made no progress, we're done.  If we made even
7613638Sbillm 		 * partial progress, update the znode and ZIL accordingly.
7623638Sbillm 		 */
7633638Sbillm 		if (tx_bytes == 0) {
7643897Smaybee 			dmu_tx_commit(tx);
7653638Sbillm 			ASSERT(error != 0);
7663638Sbillm 			break;
7673638Sbillm 		}
7683638Sbillm 
769789Sahrens 		/*
7703638Sbillm 		 * Clear Set-UID/Set-GID bits on successful write if not
7713638Sbillm 		 * privileged and at least one of the excute bits is set.
7723638Sbillm 		 *
7733638Sbillm 		 * It would be nice to to this after all writes have
7743638Sbillm 		 * been done, but that would still expose the ISUID/ISGID
7753638Sbillm 		 * to another app after the partial write is committed.
7765331Samw 		 *
7775331Samw 		 * Note: we don't call zfs_fuid_map_id() here because
7785331Samw 		 * user 0 is not an ephemeral uid.
779789Sahrens 		 */
7803638Sbillm 		mutex_enter(&zp->z_acl_lock);
7813638Sbillm 		if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) |
7823638Sbillm 		    (S_IXUSR >> 6))) != 0 &&
7833638Sbillm 		    (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 &&
7843638Sbillm 		    secpolicy_vnode_setid_retain(cr,
7853638Sbillm 		    (zp->z_phys->zp_mode & S_ISUID) != 0 &&
7863638Sbillm 		    zp->z_phys->zp_uid == 0) != 0) {
7874339Sperrin 			zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID);
7883638Sbillm 		}
7893638Sbillm 		mutex_exit(&zp->z_acl_lock);
7903638Sbillm 
7913638Sbillm 		/*
7923638Sbillm 		 * Update time stamp.  NOTE: This marks the bonus buffer as
7933638Sbillm 		 * dirty, so we don't have to do it again for zp_size.
7943638Sbillm 		 */
7953638Sbillm 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
7963638Sbillm 
7973638Sbillm 		/*
7983638Sbillm 		 * Update the file size (zp_size) if it has changed;
7993638Sbillm 		 * account for possible concurrent updates.
8003638Sbillm 		 */
8013638Sbillm 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset)
802789Sahrens 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
803789Sahrens 			    uio->uio_loffset);
8043638Sbillm 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
8053638Sbillm 		dmu_tx_commit(tx);
8063638Sbillm 
8073638Sbillm 		if (error != 0)
8083638Sbillm 			break;
8093638Sbillm 		ASSERT(tx_bytes == nbytes);
8103638Sbillm 		n -= nbytes;
811789Sahrens 	}
812789Sahrens 
8132237Smaybee 	zfs_range_unlock(rl);
814789Sahrens 
815789Sahrens 	/*
816789Sahrens 	 * If we're in replay mode, or we made no progress, return error.
817789Sahrens 	 * Otherwise, it's at least a partial write, so it's successful.
818789Sahrens 	 */
819789Sahrens 	if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) {
820789Sahrens 		ZFS_EXIT(zfsvfs);
821789Sahrens 		return (error);
822789Sahrens 	}
823789Sahrens 
8242638Sperrin 	if (ioflag & (FSYNC | FDSYNC))
8252638Sperrin 		zil_commit(zilog, zp->z_last_itx, zp->z_id);
826789Sahrens 
827789Sahrens 	ZFS_EXIT(zfsvfs);
828789Sahrens 	return (0);
829789Sahrens }
830789Sahrens 
8312237Smaybee void
8323063Sperrin zfs_get_done(dmu_buf_t *db, void *vzgd)
8332237Smaybee {
8343063Sperrin 	zgd_t *zgd = (zgd_t *)vzgd;
8353063Sperrin 	rl_t *rl = zgd->zgd_rl;
8362237Smaybee 	vnode_t *vp = ZTOV(rl->r_zp);
8372237Smaybee 
8383063Sperrin 	dmu_buf_rele(db, vzgd);
8392237Smaybee 	zfs_range_unlock(rl);
8402237Smaybee 	VN_RELE(vp);
8413063Sperrin 	zil_add_vdev(zgd->zgd_zilog, DVA_GET_VDEV(BP_IDENTITY(zgd->zgd_bp)));
8423063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
8432237Smaybee }
8442237Smaybee 
845789Sahrens /*
846789Sahrens  * Get data to generate a TX_WRITE intent log record.
847789Sahrens  */
848789Sahrens int
8492237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
850789Sahrens {
851789Sahrens 	zfsvfs_t *zfsvfs = arg;
852789Sahrens 	objset_t *os = zfsvfs->z_os;
853789Sahrens 	znode_t *zp;
854789Sahrens 	uint64_t off = lr->lr_offset;
8552237Smaybee 	dmu_buf_t *db;
8561669Sperrin 	rl_t *rl;
8573063Sperrin 	zgd_t *zgd;
8583638Sbillm 	int dlen = lr->lr_length;		/* length of user data */
859789Sahrens 	int error = 0;
860789Sahrens 
8613063Sperrin 	ASSERT(zio);
862789Sahrens 	ASSERT(dlen != 0);
863789Sahrens 
864789Sahrens 	/*
8651669Sperrin 	 * Nothing to do if the file has been removed
866789Sahrens 	 */
867789Sahrens 	if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0)
868789Sahrens 		return (ENOENT);
8693461Sahrens 	if (zp->z_unlinked) {
870789Sahrens 		VN_RELE(ZTOV(zp));
871789Sahrens 		return (ENOENT);
872789Sahrens 	}
873789Sahrens 
874789Sahrens 	/*
875789Sahrens 	 * Write records come in two flavors: immediate and indirect.
876789Sahrens 	 * For small writes it's cheaper to store the data with the
877789Sahrens 	 * log record (immediate); for large writes it's cheaper to
878789Sahrens 	 * sync the data and get a pointer to it (indirect) so that
879789Sahrens 	 * we don't have to write the data twice.
880789Sahrens 	 */
8811669Sperrin 	if (buf != NULL) { /* immediate write */
8821669Sperrin 		rl = zfs_range_lock(zp, off, dlen, RL_READER);
8831669Sperrin 		/* test for truncation needs to be done while range locked */
8841669Sperrin 		if (off >= zp->z_phys->zp_size) {
8851669Sperrin 			error = ENOENT;
8861669Sperrin 			goto out;
8871669Sperrin 		}
8882449Smaybee 		VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf));
8891669Sperrin 	} else { /* indirect write */
8901669Sperrin 		uint64_t boff; /* block starting offset */
8911669Sperrin 
892789Sahrens 		/*
8931669Sperrin 		 * Have to lock the whole block to ensure when it's
8941669Sperrin 		 * written out and it's checksum is being calculated
8951669Sperrin 		 * that no one can change the data. We need to re-check
8961669Sperrin 		 * blocksize after we get the lock in case it's changed!
897789Sahrens 		 */
8981669Sperrin 		for (;;) {
8991941Sperrin 			if (ISP2(zp->z_blksz)) {
9001941Sperrin 				boff = P2ALIGN_TYPED(off, zp->z_blksz,
9011941Sperrin 				    uint64_t);
9021941Sperrin 			} else {
9031941Sperrin 				boff = 0;
9041941Sperrin 			}
9051669Sperrin 			dlen = zp->z_blksz;
9061669Sperrin 			rl = zfs_range_lock(zp, boff, dlen, RL_READER);
9071669Sperrin 			if (zp->z_blksz == dlen)
9081669Sperrin 				break;
9092237Smaybee 			zfs_range_unlock(rl);
9101669Sperrin 		}
9111669Sperrin 		/* test for truncation needs to be done while range locked */
9121669Sperrin 		if (off >= zp->z_phys->zp_size) {
9131669Sperrin 			error = ENOENT;
9141669Sperrin 			goto out;
9151669Sperrin 		}
9163063Sperrin 		zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
9173063Sperrin 		zgd->zgd_rl = rl;
9183063Sperrin 		zgd->zgd_zilog = zfsvfs->z_log;
9193063Sperrin 		zgd->zgd_bp = &lr->lr_blkptr;
9203063Sperrin 		VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db));
9212237Smaybee 		ASSERT(boff == db->db_offset);
9222237Smaybee 		lr->lr_blkoff = off - boff;
9232237Smaybee 		error = dmu_sync(zio, db, &lr->lr_blkptr,
9243063Sperrin 		    lr->lr_common.lrc_txg, zfs_get_done, zgd);
9254709Smaybee 		ASSERT((error && error != EINPROGRESS) ||
9264709Smaybee 		    lr->lr_length <= zp->z_blksz);
9273063Sperrin 		if (error == 0) {
9283063Sperrin 			zil_add_vdev(zfsvfs->z_log,
9293063Sperrin 			    DVA_GET_VDEV(BP_IDENTITY(&lr->lr_blkptr)));
9303063Sperrin 		}
9312237Smaybee 		/*
9322237Smaybee 		 * If we get EINPROGRESS, then we need to wait for a
9332237Smaybee 		 * write IO initiated by dmu_sync() to complete before
9342638Sperrin 		 * we can release this dbuf.  We will finish everything
9352237Smaybee 		 * up in the zfs_get_done() callback.
9362237Smaybee 		 */
9372237Smaybee 		if (error == EINPROGRESS)
9382237Smaybee 			return (0);
9393063Sperrin 		dmu_buf_rele(db, zgd);
9403063Sperrin 		kmem_free(zgd, sizeof (zgd_t));
941789Sahrens 	}
9421669Sperrin out:
9432237Smaybee 	zfs_range_unlock(rl);
944789Sahrens 	VN_RELE(ZTOV(zp));
945789Sahrens 	return (error);
946789Sahrens }
947789Sahrens 
948789Sahrens /*ARGSUSED*/
949789Sahrens static int
9505331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
9515331Samw     caller_context_t *ct)
952789Sahrens {
953789Sahrens 	znode_t *zp = VTOZ(vp);
954789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
955789Sahrens 	int error;
956789Sahrens 
9575367Sahrens 	ZFS_ENTER(zfsvfs);
9585367Sahrens 	ZFS_VERIFY_ZP(zp);
9595331Samw 
9605331Samw 	if (flag & V_ACE_MASK)
9615331Samw 		error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
9625331Samw 	else
9635331Samw 		error = zfs_zaccess_rwx(zp, mode, flag, cr);
9645331Samw 
965789Sahrens 	ZFS_EXIT(zfsvfs);
966789Sahrens 	return (error);
967789Sahrens }
968789Sahrens 
969789Sahrens /*
970789Sahrens  * Lookup an entry in a directory, or an extended attribute directory.
971789Sahrens  * If it exists, return a held vnode reference for it.
972789Sahrens  *
973789Sahrens  *	IN:	dvp	- vnode of directory to search.
974789Sahrens  *		nm	- name of entry to lookup.
975789Sahrens  *		pnp	- full pathname to lookup [UNUSED].
976789Sahrens  *		flags	- LOOKUP_XATTR set if looking for an attribute.
977789Sahrens  *		rdir	- root directory vnode [UNUSED].
978789Sahrens  *		cr	- credentials of caller.
9795331Samw  *		ct	- caller context
9805331Samw  *		direntflags - directory lookup flags
9815331Samw  *		realpnp - returned pathname.
982789Sahrens  *
983789Sahrens  *	OUT:	vpp	- vnode of located entry, NULL if not found.
984789Sahrens  *
985789Sahrens  *	RETURN:	0 if success
986789Sahrens  *		error code if failure
987789Sahrens  *
988789Sahrens  * Timestamps:
989789Sahrens  *	NA
990789Sahrens  */
991789Sahrens /* ARGSUSED */
992789Sahrens static int
993789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
9945331Samw     int flags, vnode_t *rdir, cred_t *cr,  caller_context_t *ct,
9955331Samw     int *direntflags, pathname_t *realpnp)
996789Sahrens {
997789Sahrens 	znode_t *zdp = VTOZ(dvp);
998789Sahrens 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
999789Sahrens 	int	error;
1000789Sahrens 
10015367Sahrens 	ZFS_ENTER(zfsvfs);
10025367Sahrens 	ZFS_VERIFY_ZP(zdp);
1003789Sahrens 
1004789Sahrens 	*vpp = NULL;
1005789Sahrens 
1006789Sahrens 	if (flags & LOOKUP_XATTR) {
1007789Sahrens 		/*
10083234Sck153898 		 * If the xattr property is off, refuse the lookup request.
10093234Sck153898 		 */
10103234Sck153898 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
10113234Sck153898 			ZFS_EXIT(zfsvfs);
10123234Sck153898 			return (EINVAL);
10133234Sck153898 		}
10143234Sck153898 
10153234Sck153898 		/*
1016789Sahrens 		 * We don't allow recursive attributes..
1017789Sahrens 		 * Maybe someday we will.
1018789Sahrens 		 */
1019789Sahrens 		if (zdp->z_phys->zp_flags & ZFS_XATTR) {
1020789Sahrens 			ZFS_EXIT(zfsvfs);
1021789Sahrens 			return (EINVAL);
1022789Sahrens 		}
1023789Sahrens 
10243280Sck153898 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1025789Sahrens 			ZFS_EXIT(zfsvfs);
1026789Sahrens 			return (error);
1027789Sahrens 		}
1028789Sahrens 
1029789Sahrens 		/*
1030789Sahrens 		 * Do we have permission to get into attribute directory?
1031789Sahrens 		 */
1032789Sahrens 
10335331Samw 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
10345331Samw 		    B_FALSE, cr)) {
1035789Sahrens 			VN_RELE(*vpp);
10365331Samw 			*vpp = NULL;
1037789Sahrens 		}
1038789Sahrens 
1039789Sahrens 		ZFS_EXIT(zfsvfs);
1040789Sahrens 		return (error);
1041789Sahrens 	}
1042789Sahrens 
10431512Sek110237 	if (dvp->v_type != VDIR) {
10441512Sek110237 		ZFS_EXIT(zfsvfs);
10451460Smarks 		return (ENOTDIR);
10461512Sek110237 	}
10471460Smarks 
1048789Sahrens 	/*
1049789Sahrens 	 * Check accessibility of directory.
1050789Sahrens 	 */
1051789Sahrens 
10525331Samw 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1053789Sahrens 		ZFS_EXIT(zfsvfs);
1054789Sahrens 		return (error);
1055789Sahrens 	}
1056789Sahrens 
10575498Stimh 	if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
10585331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
10595331Samw 		ZFS_EXIT(zfsvfs);
10605331Samw 		return (EILSEQ);
10615331Samw 	}
10625331Samw 
10635331Samw 	error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
10645331Samw 	if (error == 0) {
1065789Sahrens 		/*
1066789Sahrens 		 * Convert device special files
1067789Sahrens 		 */
1068789Sahrens 		if (IS_DEVVP(*vpp)) {
1069789Sahrens 			vnode_t	*svp;
1070789Sahrens 
1071789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1072789Sahrens 			VN_RELE(*vpp);
1073789Sahrens 			if (svp == NULL)
1074789Sahrens 				error = ENOSYS;
1075789Sahrens 			else
1076789Sahrens 				*vpp = svp;
1077789Sahrens 		}
1078789Sahrens 	}
1079789Sahrens 
1080789Sahrens 	ZFS_EXIT(zfsvfs);
1081789Sahrens 	return (error);
1082789Sahrens }
1083789Sahrens 
1084789Sahrens /*
1085789Sahrens  * Attempt to create a new entry in a directory.  If the entry
1086789Sahrens  * already exists, truncate the file if permissible, else return
1087789Sahrens  * an error.  Return the vp of the created or trunc'd file.
1088789Sahrens  *
1089789Sahrens  *	IN:	dvp	- vnode of directory to put new file entry in.
1090789Sahrens  *		name	- name of new file entry.
1091789Sahrens  *		vap	- attributes of new file.
1092789Sahrens  *		excl	- flag indicating exclusive or non-exclusive mode.
1093789Sahrens  *		mode	- mode to open file with.
1094789Sahrens  *		cr	- credentials of caller.
1095789Sahrens  *		flag	- large file flag [UNUSED].
10965331Samw  *		ct	- caller context
10975331Samw  *		vsecp 	- ACL to be set
1098789Sahrens  *
1099789Sahrens  *	OUT:	vpp	- vnode of created or trunc'd entry.
1100789Sahrens  *
1101789Sahrens  *	RETURN:	0 if success
1102789Sahrens  *		error code if failure
1103789Sahrens  *
1104789Sahrens  * Timestamps:
1105789Sahrens  *	dvp - ctime|mtime updated if new entry created
1106789Sahrens  *	 vp - ctime|mtime always, atime if new
1107789Sahrens  */
11085331Samw 
1109789Sahrens /* ARGSUSED */
1110789Sahrens static int
1111789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
11125331Samw     int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
11135331Samw     vsecattr_t *vsecp)
1114789Sahrens {
1115789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1116789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
11175326Sek110237 	zilog_t		*zilog;
11185326Sek110237 	objset_t	*os;
1119789Sahrens 	zfs_dirlock_t	*dl;
1120789Sahrens 	dmu_tx_t	*tx;
1121789Sahrens 	int		error;
11225331Samw 	zfs_acl_t	*aclp = NULL;
11235331Samw 	zfs_fuid_info_t *fuidp = NULL;
11245331Samw 
11255331Samw 	/*
11265331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
11275331Samw 	 * make sure file system is at proper version
11285331Samw 	 */
11295331Samw 
11305331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
11315331Samw 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
11325331Samw 	    IS_EPHEMERAL(crgetuid(cr)) || IS_EPHEMERAL(crgetgid(cr))))
11335331Samw 		return (EINVAL);
1134789Sahrens 
11355367Sahrens 	ZFS_ENTER(zfsvfs);
11365367Sahrens 	ZFS_VERIFY_ZP(dzp);
11375326Sek110237 	os = zfsvfs->z_os;
11385326Sek110237 	zilog = zfsvfs->z_log;
1139789Sahrens 
11405498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
11415331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
11425331Samw 		ZFS_EXIT(zfsvfs);
11435331Samw 		return (EILSEQ);
11445331Samw 	}
11455331Samw 
11465331Samw 	if (vap->va_mask & AT_XVATTR) {
11475331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
11485331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
11495331Samw 			ZFS_EXIT(zfsvfs);
11505331Samw 			return (error);
11515331Samw 		}
11525331Samw 	}
1153789Sahrens top:
1154789Sahrens 	*vpp = NULL;
1155789Sahrens 
1156789Sahrens 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1157789Sahrens 		vap->va_mode &= ~VSVTX;
1158789Sahrens 
1159789Sahrens 	if (*name == '\0') {
1160789Sahrens 		/*
1161789Sahrens 		 * Null component name refers to the directory itself.
1162789Sahrens 		 */
1163789Sahrens 		VN_HOLD(dvp);
1164789Sahrens 		zp = dzp;
1165789Sahrens 		dl = NULL;
1166789Sahrens 		error = 0;
1167789Sahrens 	} else {
1168789Sahrens 		/* possible VN_HOLD(zp) */
11695331Samw 		int zflg = 0;
11705331Samw 
11715331Samw 		if (flag & FIGNORECASE)
11725331Samw 			zflg |= ZCILOOK;
11735331Samw 
11745331Samw 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
11755331Samw 		    NULL, NULL);
11765331Samw 		if (error) {
1177789Sahrens 			if (strcmp(name, "..") == 0)
1178789Sahrens 				error = EISDIR;
1179789Sahrens 			ZFS_EXIT(zfsvfs);
11805331Samw 			if (aclp)
11815331Samw 				zfs_acl_free(aclp);
1182789Sahrens 			return (error);
1183789Sahrens 		}
1184789Sahrens 	}
11855331Samw 	if (vsecp && aclp == NULL) {
11865331Samw 		error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp);
11875331Samw 		if (error) {
11885331Samw 			ZFS_EXIT(zfsvfs);
11895331Samw 			if (dl)
11905331Samw 				zfs_dirent_unlock(dl);
11915331Samw 			return (error);
11925331Samw 		}
11935331Samw 	}
1194789Sahrens 
1195789Sahrens 	if (zp == NULL) {
11965331Samw 		uint64_t txtype;
11975331Samw 
1198789Sahrens 		/*
1199789Sahrens 		 * Create a new file object and update the directory
1200789Sahrens 		 * to reference it.
1201789Sahrens 		 */
12025331Samw 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1203789Sahrens 			goto out;
1204789Sahrens 		}
1205789Sahrens 
1206789Sahrens 		/*
1207789Sahrens 		 * We only support the creation of regular files in
1208789Sahrens 		 * extended attribute directories.
1209789Sahrens 		 */
1210789Sahrens 		if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1211789Sahrens 		    (vap->va_type != VREG)) {
1212789Sahrens 			error = EINVAL;
1213789Sahrens 			goto out;
1214789Sahrens 		}
1215789Sahrens 
1216789Sahrens 		tx = dmu_tx_create(os);
1217789Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
12185331Samw 		if (zfsvfs->z_fuid_obj == 0) {
12195331Samw 			dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
12205331Samw 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
12215331Samw 			    SPA_MAXBLOCKSIZE);
12225331Samw 			dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
12235331Samw 		} else {
12245331Samw 			dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
12255331Samw 			dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
12265331Samw 			    SPA_MAXBLOCKSIZE);
12275331Samw 		}
1228789Sahrens 		dmu_tx_hold_bonus(tx, dzp->z_id);
12291544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
12305331Samw 		if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp) {
1231789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1232789Sahrens 			    0, SPA_MAXBLOCKSIZE);
12335331Samw 		}
1234789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
1235789Sahrens 		if (error) {
1236789Sahrens 			zfs_dirent_unlock(dl);
1237789Sahrens 			if (error == ERESTART &&
1238789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
12392113Sahrens 				dmu_tx_wait(tx);
12402113Sahrens 				dmu_tx_abort(tx);
1241789Sahrens 				goto top;
1242789Sahrens 			}
12432113Sahrens 			dmu_tx_abort(tx);
1244789Sahrens 			ZFS_EXIT(zfsvfs);
12455331Samw 			if (aclp)
12465331Samw 				zfs_acl_free(aclp);
1247789Sahrens 			return (error);
1248789Sahrens 		}
12495446Sahrens 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, aclp, &fuidp);
1250789Sahrens 		(void) zfs_link_create(dl, zp, tx, ZNEW);
12515331Samw 		txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
12525331Samw 		if (flag & FIGNORECASE)
12535331Samw 			txtype |= TX_CI;
12545331Samw 		zfs_log_create(zilog, tx, txtype, dzp, zp, name,
12555331Samw 		    vsecp, fuidp, vap);
12565331Samw 		if (fuidp)
12575331Samw 			zfs_fuid_info_free(fuidp);
1258789Sahrens 		dmu_tx_commit(tx);
1259789Sahrens 	} else {
12605331Samw 		int aflags = (flag & FAPPEND) ? V_APPEND : 0;
12615331Samw 
1262789Sahrens 		/*
1263789Sahrens 		 * A directory entry already exists for this name.
1264789Sahrens 		 */
1265789Sahrens 		/*
1266789Sahrens 		 * Can't truncate an existing file if in exclusive mode.
1267789Sahrens 		 */
1268789Sahrens 		if (excl == EXCL) {
1269789Sahrens 			error = EEXIST;
1270789Sahrens 			goto out;
1271789Sahrens 		}
1272789Sahrens 		/*
1273789Sahrens 		 * Can't open a directory for writing.
1274789Sahrens 		 */
1275789Sahrens 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1276789Sahrens 			error = EISDIR;
1277789Sahrens 			goto out;
1278789Sahrens 		}
1279789Sahrens 		/*
1280789Sahrens 		 * Verify requested access to file.
1281789Sahrens 		 */
12825331Samw 		if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1283789Sahrens 			goto out;
1284789Sahrens 		}
1285789Sahrens 
1286789Sahrens 		mutex_enter(&dzp->z_lock);
1287789Sahrens 		dzp->z_seq++;
1288789Sahrens 		mutex_exit(&dzp->z_lock);
1289789Sahrens 
12901878Smaybee 		/*
12911878Smaybee 		 * Truncate regular files if requested.
12921878Smaybee 		 */
12931878Smaybee 		if ((ZTOV(zp)->v_type == VREG) &&
1294789Sahrens 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
12951878Smaybee 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
12961878Smaybee 			if (error == ERESTART &&
12971878Smaybee 			    zfsvfs->z_assign == TXG_NOWAIT) {
12982113Sahrens 				/* NB: we already did dmu_tx_wait() */
12991878Smaybee 				zfs_dirent_unlock(dl);
13002365Sperrin 				VN_RELE(ZTOV(zp));
13011878Smaybee 				goto top;
1302789Sahrens 			}
13034863Spraks 
13044863Spraks 			if (error == 0) {
13055331Samw 				vnevent_create(ZTOV(zp), ct);
13064863Spraks 			}
1307789Sahrens 		}
1308789Sahrens 	}
1309789Sahrens out:
1310789Sahrens 
1311789Sahrens 	if (dl)
1312789Sahrens 		zfs_dirent_unlock(dl);
1313789Sahrens 
1314789Sahrens 	if (error) {
1315789Sahrens 		if (zp)
1316789Sahrens 			VN_RELE(ZTOV(zp));
1317789Sahrens 	} else {
1318789Sahrens 		*vpp = ZTOV(zp);
1319789Sahrens 		/*
1320789Sahrens 		 * If vnode is for a device return a specfs vnode instead.
1321789Sahrens 		 */
1322789Sahrens 		if (IS_DEVVP(*vpp)) {
1323789Sahrens 			struct vnode *svp;
1324789Sahrens 
1325789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1326789Sahrens 			VN_RELE(*vpp);
1327789Sahrens 			if (svp == NULL) {
1328789Sahrens 				error = ENOSYS;
1329789Sahrens 			}
1330789Sahrens 			*vpp = svp;
1331789Sahrens 		}
1332789Sahrens 	}
13335331Samw 	if (aclp)
13345331Samw 		zfs_acl_free(aclp);
1335789Sahrens 
1336789Sahrens 	ZFS_EXIT(zfsvfs);
1337789Sahrens 	return (error);
1338789Sahrens }
1339789Sahrens 
1340789Sahrens /*
1341789Sahrens  * Remove an entry from a directory.
1342789Sahrens  *
1343789Sahrens  *	IN:	dvp	- vnode of directory to remove entry from.
1344789Sahrens  *		name	- name of entry to remove.
1345789Sahrens  *		cr	- credentials of caller.
13465331Samw  *		ct	- caller context
13475331Samw  *		flags	- case flags
1348789Sahrens  *
1349789Sahrens  *	RETURN:	0 if success
1350789Sahrens  *		error code if failure
1351789Sahrens  *
1352789Sahrens  * Timestamps:
1353789Sahrens  *	dvp - ctime|mtime
1354789Sahrens  *	 vp - ctime (if nlink > 0)
1355789Sahrens  */
13565331Samw /*ARGSUSED*/
1357789Sahrens static int
13585331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
13595331Samw     int flags)
1360789Sahrens {
1361789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1362789Sahrens 	znode_t		*xzp = NULL;
1363789Sahrens 	vnode_t		*vp;
1364789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
13655326Sek110237 	zilog_t		*zilog;
1366789Sahrens 	uint64_t	acl_obj, xattr_obj;
1367789Sahrens 	zfs_dirlock_t	*dl;
1368789Sahrens 	dmu_tx_t	*tx;
13693461Sahrens 	boolean_t	may_delete_now, delete_now = FALSE;
13703461Sahrens 	boolean_t	unlinked;
13715331Samw 	uint64_t	txtype;
13725331Samw 	pathname_t	*realnmp = NULL;
13735331Samw 	pathname_t	realnm;
1374789Sahrens 	int		error;
13755331Samw 	int		zflg = ZEXISTS;
1376789Sahrens 
13775367Sahrens 	ZFS_ENTER(zfsvfs);
13785367Sahrens 	ZFS_VERIFY_ZP(dzp);
13795326Sek110237 	zilog = zfsvfs->z_log;
1380789Sahrens 
13815331Samw 	if (flags & FIGNORECASE) {
13825331Samw 		zflg |= ZCILOOK;
13835331Samw 		pn_alloc(&realnm);
13845331Samw 		realnmp = &realnm;
13855331Samw 	}
13865331Samw 
1387789Sahrens top:
1388789Sahrens 	/*
1389789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1390789Sahrens 	 */
13915331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
13925331Samw 	    NULL, realnmp)) {
13935331Samw 		if (realnmp)
13945331Samw 			pn_free(realnmp);
1395789Sahrens 		ZFS_EXIT(zfsvfs);
1396789Sahrens 		return (error);
1397789Sahrens 	}
1398789Sahrens 
1399789Sahrens 	vp = ZTOV(zp);
1400789Sahrens 
1401789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1402789Sahrens 		goto out;
1403789Sahrens 	}
1404789Sahrens 
1405789Sahrens 	/*
1406789Sahrens 	 * Need to use rmdir for removing directories.
1407789Sahrens 	 */
1408789Sahrens 	if (vp->v_type == VDIR) {
1409789Sahrens 		error = EPERM;
1410789Sahrens 		goto out;
1411789Sahrens 	}
1412789Sahrens 
14135331Samw 	vnevent_remove(vp, dvp, name, ct);
14145331Samw 
14155331Samw 	if (realnmp)
14165331Samw 		dnlc_remove(dvp, realnmp->pn_path);
14175331Samw 	else
14185331Samw 		dnlc_remove(dvp, name);
14191484Sek110237 
1420789Sahrens 	mutex_enter(&vp->v_lock);
1421789Sahrens 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1422789Sahrens 	mutex_exit(&vp->v_lock);
1423789Sahrens 
1424789Sahrens 	/*
14253461Sahrens 	 * We may delete the znode now, or we may put it in the unlinked set;
1426789Sahrens 	 * it depends on whether we're the last link, and on whether there are
1427789Sahrens 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1428789Sahrens 	 * allow for either case.
1429789Sahrens 	 */
1430789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
14311544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1432789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
1433789Sahrens 	if (may_delete_now)
1434789Sahrens 		dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
1435789Sahrens 
1436789Sahrens 	/* are there any extended attributes? */
1437789Sahrens 	if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1438789Sahrens 		/* XXX - do we need this if we are deleting? */
1439789Sahrens 		dmu_tx_hold_bonus(tx, xattr_obj);
1440789Sahrens 	}
1441789Sahrens 
1442789Sahrens 	/* are there any additional acls */
1443789Sahrens 	if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1444789Sahrens 	    may_delete_now)
1445789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1446789Sahrens 
1447789Sahrens 	/* charge as an update -- would be nice not to charge at all */
14483461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1449789Sahrens 
1450789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1451789Sahrens 	if (error) {
1452789Sahrens 		zfs_dirent_unlock(dl);
1453789Sahrens 		VN_RELE(vp);
1454789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
14552113Sahrens 			dmu_tx_wait(tx);
14562113Sahrens 			dmu_tx_abort(tx);
1457789Sahrens 			goto top;
1458789Sahrens 		}
14595331Samw 		if (realnmp)
14605331Samw 			pn_free(realnmp);
14612113Sahrens 		dmu_tx_abort(tx);
1462789Sahrens 		ZFS_EXIT(zfsvfs);
1463789Sahrens 		return (error);
1464789Sahrens 	}
1465789Sahrens 
1466789Sahrens 	/*
1467789Sahrens 	 * Remove the directory entry.
1468789Sahrens 	 */
14695331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1470789Sahrens 
1471789Sahrens 	if (error) {
1472789Sahrens 		dmu_tx_commit(tx);
1473789Sahrens 		goto out;
1474789Sahrens 	}
1475789Sahrens 
14763461Sahrens 	if (unlinked) {
1477789Sahrens 		mutex_enter(&vp->v_lock);
1478789Sahrens 		delete_now = may_delete_now &&
1479789Sahrens 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1480789Sahrens 		    zp->z_phys->zp_xattr == xattr_obj &&
1481789Sahrens 		    zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1482789Sahrens 		mutex_exit(&vp->v_lock);
1483789Sahrens 	}
1484789Sahrens 
1485789Sahrens 	if (delete_now) {
1486789Sahrens 		if (zp->z_phys->zp_xattr) {
1487789Sahrens 			error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1488789Sahrens 			ASSERT3U(error, ==, 0);
1489789Sahrens 			ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1490789Sahrens 			dmu_buf_will_dirty(xzp->z_dbuf, tx);
1491789Sahrens 			mutex_enter(&xzp->z_lock);
14923461Sahrens 			xzp->z_unlinked = 1;
1493789Sahrens 			xzp->z_phys->zp_links = 0;
1494789Sahrens 			mutex_exit(&xzp->z_lock);
14953461Sahrens 			zfs_unlinked_add(xzp, tx);
1496789Sahrens 			zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1497789Sahrens 		}
1498789Sahrens 		mutex_enter(&zp->z_lock);
1499789Sahrens 		mutex_enter(&vp->v_lock);
1500789Sahrens 		vp->v_count--;
1501789Sahrens 		ASSERT3U(vp->v_count, ==, 0);
1502789Sahrens 		mutex_exit(&vp->v_lock);
1503789Sahrens 		mutex_exit(&zp->z_lock);
1504789Sahrens 		zfs_znode_delete(zp, tx);
1505789Sahrens 		VFS_RELE(zfsvfs->z_vfs);
15063461Sahrens 	} else if (unlinked) {
15073461Sahrens 		zfs_unlinked_add(zp, tx);
1508789Sahrens 	}
1509789Sahrens 
15105331Samw 	txtype = TX_REMOVE;
15115331Samw 	if (flags & FIGNORECASE)
15125331Samw 		txtype |= TX_CI;
15135331Samw 	zfs_log_remove(zilog, tx, txtype, dzp, name);
1514789Sahrens 
1515789Sahrens 	dmu_tx_commit(tx);
1516789Sahrens out:
15175331Samw 	if (realnmp)
15185331Samw 		pn_free(realnmp);
15195331Samw 
1520789Sahrens 	zfs_dirent_unlock(dl);
1521789Sahrens 
1522789Sahrens 	if (!delete_now) {
1523789Sahrens 		VN_RELE(vp);
1524789Sahrens 	} else if (xzp) {
1525789Sahrens 		/* this rele delayed to prevent nesting transactions */
1526789Sahrens 		VN_RELE(ZTOV(xzp));
1527789Sahrens 	}
1528789Sahrens 
1529789Sahrens 	ZFS_EXIT(zfsvfs);
1530789Sahrens 	return (error);
1531789Sahrens }
1532789Sahrens 
1533789Sahrens /*
1534789Sahrens  * Create a new directory and insert it into dvp using the name
1535789Sahrens  * provided.  Return a pointer to the inserted directory.
1536789Sahrens  *
1537789Sahrens  *	IN:	dvp	- vnode of directory to add subdir to.
1538789Sahrens  *		dirname	- name of new directory.
1539789Sahrens  *		vap	- attributes of new directory.
1540789Sahrens  *		cr	- credentials of caller.
15415331Samw  *		ct	- caller context
15425331Samw  *		vsecp	- ACL to be set
1543789Sahrens  *
1544789Sahrens  *	OUT:	vpp	- vnode of created directory.
1545789Sahrens  *
1546789Sahrens  *	RETURN:	0 if success
1547789Sahrens  *		error code if failure
1548789Sahrens  *
1549789Sahrens  * Timestamps:
1550789Sahrens  *	dvp - ctime|mtime updated
1551789Sahrens  *	 vp - ctime|mtime|atime updated
1552789Sahrens  */
15535331Samw /*ARGSUSED*/
1554789Sahrens static int
15555331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
15565331Samw     caller_context_t *ct, int flags, vsecattr_t *vsecp)
1557789Sahrens {
1558789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1559789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
15605326Sek110237 	zilog_t		*zilog;
1561789Sahrens 	zfs_dirlock_t	*dl;
15625331Samw 	uint64_t	txtype;
1563789Sahrens 	dmu_tx_t	*tx;
1564789Sahrens 	int		error;
15655331Samw 	zfs_acl_t	*aclp = NULL;
15665331Samw 	zfs_fuid_info_t	*fuidp = NULL;
15675331Samw 	int		zf = ZNEW;
1568789Sahrens 
1569789Sahrens 	ASSERT(vap->va_type == VDIR);
1570789Sahrens 
15715331Samw 	/*
15725331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
15735331Samw 	 * make sure file system is at proper version
15745331Samw 	 */
15755331Samw 
15765331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
15775331Samw 	    (vsecp || (vap->va_mask & AT_XVATTR) || IS_EPHEMERAL(crgetuid(cr))||
15785331Samw 	    IS_EPHEMERAL(crgetgid(cr))))
15795331Samw 		return (EINVAL);
15805331Samw 
15815367Sahrens 	ZFS_ENTER(zfsvfs);
15825367Sahrens 	ZFS_VERIFY_ZP(dzp);
15835326Sek110237 	zilog = zfsvfs->z_log;
1584789Sahrens 
1585789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1586789Sahrens 		ZFS_EXIT(zfsvfs);
1587789Sahrens 		return (EINVAL);
1588789Sahrens 	}
15895331Samw 
15905498Stimh 	if (zfsvfs->z_utf8 && u8_validate(dirname,
15915331Samw 	    strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
15925331Samw 		ZFS_EXIT(zfsvfs);
15935331Samw 		return (EILSEQ);
15945331Samw 	}
15955331Samw 	if (flags & FIGNORECASE)
15965331Samw 		zf |= ZCILOOK;
15975331Samw 
15985331Samw 	if (vap->va_mask & AT_XVATTR)
15995331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
16005331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
16015331Samw 			ZFS_EXIT(zfsvfs);
16025331Samw 			return (error);
16035331Samw 		}
1604789Sahrens 
1605789Sahrens 	/*
1606789Sahrens 	 * First make sure the new directory doesn't exist.
1607789Sahrens 	 */
16085331Samw top:
16095331Samw 	*vpp = NULL;
16105331Samw 
16115331Samw 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
16125331Samw 	    NULL, NULL)) {
1613789Sahrens 		ZFS_EXIT(zfsvfs);
1614789Sahrens 		return (error);
1615789Sahrens 	}
1616789Sahrens 
16175331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
16181231Smarks 		zfs_dirent_unlock(dl);
16191231Smarks 		ZFS_EXIT(zfsvfs);
16201231Smarks 		return (error);
16211231Smarks 	}
16221231Smarks 
16235331Samw 	if (vsecp && aclp == NULL) {
16245331Samw 		error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp);
16255331Samw 		if (error) {
16265331Samw 			zfs_dirent_unlock(dl);
16275331Samw 			ZFS_EXIT(zfsvfs);
16285331Samw 			return (error);
16295331Samw 		}
16305331Samw 	}
1631789Sahrens 	/*
1632789Sahrens 	 * Add a new entry to the directory.
1633789Sahrens 	 */
1634789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
16351544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
16361544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
16375331Samw 	if (zfsvfs->z_fuid_obj == 0) {
16385331Samw 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
16395331Samw 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
16405331Samw 		    SPA_MAXBLOCKSIZE);
16415331Samw 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
16425331Samw 	} else {
16435331Samw 		dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
16445331Samw 		dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
16455331Samw 		    SPA_MAXBLOCKSIZE);
16465331Samw 	}
16475331Samw 	if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp)
1648789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1649789Sahrens 		    0, SPA_MAXBLOCKSIZE);
1650789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1651789Sahrens 	if (error) {
1652789Sahrens 		zfs_dirent_unlock(dl);
1653789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
16542113Sahrens 			dmu_tx_wait(tx);
16552113Sahrens 			dmu_tx_abort(tx);
1656789Sahrens 			goto top;
1657789Sahrens 		}
16582113Sahrens 		dmu_tx_abort(tx);
1659789Sahrens 		ZFS_EXIT(zfsvfs);
16605331Samw 		if (aclp)
16615331Samw 			zfs_acl_free(aclp);
1662789Sahrens 		return (error);
1663789Sahrens 	}
1664789Sahrens 
1665789Sahrens 	/*
1666789Sahrens 	 * Create new node.
1667789Sahrens 	 */
16685446Sahrens 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, aclp, &fuidp);
16695331Samw 
16705331Samw 	if (aclp)
16715331Samw 		zfs_acl_free(aclp);
1672789Sahrens 
1673789Sahrens 	/*
1674789Sahrens 	 * Now put new name in parent dir.
1675789Sahrens 	 */
1676789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1677789Sahrens 
1678789Sahrens 	*vpp = ZTOV(zp);
1679789Sahrens 
16805331Samw 	txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
16815331Samw 	if (flags & FIGNORECASE)
16825331Samw 		txtype |= TX_CI;
16835331Samw 	zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, fuidp, vap);
16845331Samw 
16855331Samw 	if (fuidp)
16865331Samw 		zfs_fuid_info_free(fuidp);
1687789Sahrens 	dmu_tx_commit(tx);
1688789Sahrens 
1689789Sahrens 	zfs_dirent_unlock(dl);
1690789Sahrens 
1691789Sahrens 	ZFS_EXIT(zfsvfs);
1692789Sahrens 	return (0);
1693789Sahrens }
1694789Sahrens 
1695789Sahrens /*
1696789Sahrens  * Remove a directory subdir entry.  If the current working
1697789Sahrens  * directory is the same as the subdir to be removed, the
1698789Sahrens  * remove will fail.
1699789Sahrens  *
1700789Sahrens  *	IN:	dvp	- vnode of directory to remove from.
1701789Sahrens  *		name	- name of directory to be removed.
1702789Sahrens  *		cwd	- vnode of current working directory.
1703789Sahrens  *		cr	- credentials of caller.
17045331Samw  *		ct	- caller context
17055331Samw  *		flags	- case flags
1706789Sahrens  *
1707789Sahrens  *	RETURN:	0 if success
1708789Sahrens  *		error code if failure
1709789Sahrens  *
1710789Sahrens  * Timestamps:
1711789Sahrens  *	dvp - ctime|mtime updated
1712789Sahrens  */
17135331Samw /*ARGSUSED*/
1714789Sahrens static int
17155331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
17165331Samw     caller_context_t *ct, int flags)
1717789Sahrens {
1718789Sahrens 	znode_t		*dzp = VTOZ(dvp);
1719789Sahrens 	znode_t		*zp;
1720789Sahrens 	vnode_t		*vp;
1721789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
17225326Sek110237 	zilog_t		*zilog;
1723789Sahrens 	zfs_dirlock_t	*dl;
1724789Sahrens 	dmu_tx_t	*tx;
1725789Sahrens 	int		error;
17265331Samw 	int		zflg = ZEXISTS;
1727789Sahrens 
17285367Sahrens 	ZFS_ENTER(zfsvfs);
17295367Sahrens 	ZFS_VERIFY_ZP(dzp);
17305326Sek110237 	zilog = zfsvfs->z_log;
1731789Sahrens 
17325331Samw 	if (flags & FIGNORECASE)
17335331Samw 		zflg |= ZCILOOK;
1734789Sahrens top:
1735789Sahrens 	zp = NULL;
1736789Sahrens 
1737789Sahrens 	/*
1738789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1739789Sahrens 	 */
17405331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
17415331Samw 	    NULL, NULL)) {
1742789Sahrens 		ZFS_EXIT(zfsvfs);
1743789Sahrens 		return (error);
1744789Sahrens 	}
1745789Sahrens 
1746789Sahrens 	vp = ZTOV(zp);
1747789Sahrens 
1748789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1749789Sahrens 		goto out;
1750789Sahrens 	}
1751789Sahrens 
1752789Sahrens 	if (vp->v_type != VDIR) {
1753789Sahrens 		error = ENOTDIR;
1754789Sahrens 		goto out;
1755789Sahrens 	}
1756789Sahrens 
1757789Sahrens 	if (vp == cwd) {
1758789Sahrens 		error = EINVAL;
1759789Sahrens 		goto out;
1760789Sahrens 	}
1761789Sahrens 
17625331Samw 	vnevent_rmdir(vp, dvp, name, ct);
1763789Sahrens 
1764789Sahrens 	/*
17653897Smaybee 	 * Grab a lock on the directory to make sure that noone is
17663897Smaybee 	 * trying to add (or lookup) entries while we are removing it.
17673897Smaybee 	 */
17683897Smaybee 	rw_enter(&zp->z_name_lock, RW_WRITER);
17693897Smaybee 
17703897Smaybee 	/*
17713897Smaybee 	 * Grab a lock on the parent pointer to make sure we play well
1772789Sahrens 	 * with the treewalk and directory rename code.
1773789Sahrens 	 */
1774789Sahrens 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1775789Sahrens 
1776789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
17771544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1778789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
17793461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1780789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1781789Sahrens 	if (error) {
1782789Sahrens 		rw_exit(&zp->z_parent_lock);
17833897Smaybee 		rw_exit(&zp->z_name_lock);
1784789Sahrens 		zfs_dirent_unlock(dl);
1785789Sahrens 		VN_RELE(vp);
1786789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
17872113Sahrens 			dmu_tx_wait(tx);
17882113Sahrens 			dmu_tx_abort(tx);
1789789Sahrens 			goto top;
1790789Sahrens 		}
17912113Sahrens 		dmu_tx_abort(tx);
1792789Sahrens 		ZFS_EXIT(zfsvfs);
1793789Sahrens 		return (error);
1794789Sahrens 	}
1795789Sahrens 
17965331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
17975331Samw 
17985331Samw 	if (error == 0) {
17995331Samw 		uint64_t txtype = TX_RMDIR;
18005331Samw 		if (flags & FIGNORECASE)
18015331Samw 			txtype |= TX_CI;
18025331Samw 		zfs_log_remove(zilog, tx, txtype, dzp, name);
18035331Samw 	}
1804789Sahrens 
1805789Sahrens 	dmu_tx_commit(tx);
1806789Sahrens 
1807789Sahrens 	rw_exit(&zp->z_parent_lock);
18083897Smaybee 	rw_exit(&zp->z_name_lock);
1809789Sahrens out:
1810789Sahrens 	zfs_dirent_unlock(dl);
1811789Sahrens 
1812789Sahrens 	VN_RELE(vp);
1813789Sahrens 
1814789Sahrens 	ZFS_EXIT(zfsvfs);
1815789Sahrens 	return (error);
1816789Sahrens }
1817789Sahrens 
1818789Sahrens /*
1819789Sahrens  * Read as many directory entries as will fit into the provided
1820789Sahrens  * buffer from the given directory cursor position (specified in
1821789Sahrens  * the uio structure.
1822789Sahrens  *
1823789Sahrens  *	IN:	vp	- vnode of directory to read.
1824789Sahrens  *		uio	- structure supplying read location, range info,
1825789Sahrens  *			  and return buffer.
1826789Sahrens  *		cr	- credentials of caller.
18275331Samw  *		ct	- caller context
18285331Samw  *		flags	- case flags
1829789Sahrens  *
1830789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
1831789Sahrens  *		eofp	- set to true if end-of-file detected.
1832789Sahrens  *
1833789Sahrens  *	RETURN:	0 if success
1834789Sahrens  *		error code if failure
1835789Sahrens  *
1836789Sahrens  * Timestamps:
1837789Sahrens  *	vp - atime updated
1838789Sahrens  *
1839789Sahrens  * Note that the low 4 bits of the cookie returned by zap is always zero.
1840789Sahrens  * This allows us to use the low range for "special" directory entries:
1841789Sahrens  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
1842789Sahrens  * we use the offset 2 for the '.zfs' directory.
1843789Sahrens  */
1844789Sahrens /* ARGSUSED */
1845789Sahrens static int
18465331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
18475331Samw     caller_context_t *ct, int flags)
1848789Sahrens {
1849789Sahrens 	znode_t		*zp = VTOZ(vp);
1850789Sahrens 	iovec_t		*iovp;
18515331Samw 	edirent_t	*eodp;
1852789Sahrens 	dirent64_t	*odp;
1853789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1854869Sperrin 	objset_t	*os;
1855789Sahrens 	caddr_t		outbuf;
1856789Sahrens 	size_t		bufsize;
1857789Sahrens 	zap_cursor_t	zc;
1858789Sahrens 	zap_attribute_t	zap;
1859789Sahrens 	uint_t		bytes_wanted;
1860789Sahrens 	uint64_t	offset; /* must be unsigned; checks for < 1 */
1861789Sahrens 	int		local_eof;
1862869Sperrin 	int		outcount;
1863869Sperrin 	int		error;
1864869Sperrin 	uint8_t		prefetch;
1865789Sahrens 
18665367Sahrens 	ZFS_ENTER(zfsvfs);
18675367Sahrens 	ZFS_VERIFY_ZP(zp);
1868789Sahrens 
1869789Sahrens 	/*
1870789Sahrens 	 * If we are not given an eof variable,
1871789Sahrens 	 * use a local one.
1872789Sahrens 	 */
1873789Sahrens 	if (eofp == NULL)
1874789Sahrens 		eofp = &local_eof;
1875789Sahrens 
1876789Sahrens 	/*
1877789Sahrens 	 * Check for valid iov_len.
1878789Sahrens 	 */
1879789Sahrens 	if (uio->uio_iov->iov_len <= 0) {
1880789Sahrens 		ZFS_EXIT(zfsvfs);
1881789Sahrens 		return (EINVAL);
1882789Sahrens 	}
1883789Sahrens 
1884789Sahrens 	/*
1885789Sahrens 	 * Quit if directory has been removed (posix)
1886789Sahrens 	 */
18873461Sahrens 	if ((*eofp = zp->z_unlinked) != 0) {
1888789Sahrens 		ZFS_EXIT(zfsvfs);
1889789Sahrens 		return (0);
1890789Sahrens 	}
1891789Sahrens 
1892869Sperrin 	error = 0;
1893869Sperrin 	os = zfsvfs->z_os;
1894869Sperrin 	offset = uio->uio_loffset;
1895869Sperrin 	prefetch = zp->z_zn_prefetch;
1896869Sperrin 
1897789Sahrens 	/*
1898789Sahrens 	 * Initialize the iterator cursor.
1899789Sahrens 	 */
1900789Sahrens 	if (offset <= 3) {
1901789Sahrens 		/*
1902789Sahrens 		 * Start iteration from the beginning of the directory.
1903789Sahrens 		 */
1904869Sperrin 		zap_cursor_init(&zc, os, zp->z_id);
1905789Sahrens 	} else {
1906789Sahrens 		/*
1907789Sahrens 		 * The offset is a serialized cursor.
1908789Sahrens 		 */
1909869Sperrin 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1910789Sahrens 	}
1911789Sahrens 
1912789Sahrens 	/*
1913789Sahrens 	 * Get space to change directory entries into fs independent format.
1914789Sahrens 	 */
1915789Sahrens 	iovp = uio->uio_iov;
1916789Sahrens 	bytes_wanted = iovp->iov_len;
1917789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
1918789Sahrens 		bufsize = bytes_wanted;
1919789Sahrens 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
1920789Sahrens 		odp = (struct dirent64 *)outbuf;
1921789Sahrens 	} else {
1922789Sahrens 		bufsize = bytes_wanted;
1923789Sahrens 		odp = (struct dirent64 *)iovp->iov_base;
1924789Sahrens 	}
19255331Samw 	eodp = (struct edirent *)odp;
1926789Sahrens 
1927789Sahrens 	/*
1928789Sahrens 	 * Transform to file-system independent format
1929789Sahrens 	 */
1930789Sahrens 	outcount = 0;
1931789Sahrens 	while (outcount < bytes_wanted) {
19323912Slling 		ino64_t objnum;
19333912Slling 		ushort_t reclen;
19343912Slling 		off64_t *next;
19353912Slling 
1936789Sahrens 		/*
1937789Sahrens 		 * Special case `.', `..', and `.zfs'.
1938789Sahrens 		 */
1939789Sahrens 		if (offset == 0) {
1940789Sahrens 			(void) strcpy(zap.za_name, ".");
19415331Samw 			zap.za_normalization_conflict = 0;
19423912Slling 			objnum = zp->z_id;
1943789Sahrens 		} else if (offset == 1) {
1944789Sahrens 			(void) strcpy(zap.za_name, "..");
19455331Samw 			zap.za_normalization_conflict = 0;
19463912Slling 			objnum = zp->z_phys->zp_parent;
1947789Sahrens 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
1948789Sahrens 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
19495331Samw 			zap.za_normalization_conflict = 0;
19503912Slling 			objnum = ZFSCTL_INO_ROOT;
1951789Sahrens 		} else {
1952789Sahrens 			/*
1953789Sahrens 			 * Grab next entry.
1954789Sahrens 			 */
1955789Sahrens 			if (error = zap_cursor_retrieve(&zc, &zap)) {
1956789Sahrens 				if ((*eofp = (error == ENOENT)) != 0)
1957789Sahrens 					break;
1958789Sahrens 				else
1959789Sahrens 					goto update;
1960789Sahrens 			}
1961789Sahrens 
1962789Sahrens 			if (zap.za_integer_length != 8 ||
1963789Sahrens 			    zap.za_num_integers != 1) {
1964789Sahrens 				cmn_err(CE_WARN, "zap_readdir: bad directory "
1965789Sahrens 				    "entry, obj = %lld, offset = %lld\n",
1966789Sahrens 				    (u_longlong_t)zp->z_id,
1967789Sahrens 				    (u_longlong_t)offset);
1968789Sahrens 				error = ENXIO;
1969789Sahrens 				goto update;
1970789Sahrens 			}
19713912Slling 
19723912Slling 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
19733912Slling 			/*
19743912Slling 			 * MacOS X can extract the object type here such as:
19753912Slling 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
19763912Slling 			 */
1977789Sahrens 		}
19785331Samw 
19795331Samw 		if (flags & V_RDDIR_ENTFLAGS)
19805331Samw 			reclen = EDIRENT_RECLEN(strlen(zap.za_name));
19815331Samw 		else
19825331Samw 			reclen = DIRENT64_RECLEN(strlen(zap.za_name));
1983789Sahrens 
1984789Sahrens 		/*
1985789Sahrens 		 * Will this entry fit in the buffer?
1986789Sahrens 		 */
19873912Slling 		if (outcount + reclen > bufsize) {
1988789Sahrens 			/*
1989789Sahrens 			 * Did we manage to fit anything in the buffer?
1990789Sahrens 			 */
1991789Sahrens 			if (!outcount) {
1992789Sahrens 				error = EINVAL;
1993789Sahrens 				goto update;
1994789Sahrens 			}
1995789Sahrens 			break;
1996789Sahrens 		}
19975331Samw 		if (flags & V_RDDIR_ENTFLAGS) {
19985331Samw 			/*
19995331Samw 			 * Add extended flag entry:
20005331Samw 			 */
20015331Samw 			eodp->ed_ino = objnum;
20025331Samw 			eodp->ed_reclen = reclen;
20035331Samw 			/* NOTE: ed_off is the offset for the *next* entry */
20045331Samw 			next = &(eodp->ed_off);
20055331Samw 			eodp->ed_eflags = zap.za_normalization_conflict ?
20065331Samw 			    ED_CASE_CONFLICT : 0;
20075331Samw 			(void) strncpy(eodp->ed_name, zap.za_name,
20085331Samw 			    EDIRENT_NAMELEN(reclen));
20095331Samw 			eodp = (edirent_t *)((intptr_t)eodp + reclen);
20105331Samw 		} else {
20115331Samw 			/*
20125331Samw 			 * Add normal entry:
20135331Samw 			 */
20145331Samw 			odp->d_ino = objnum;
20155331Samw 			odp->d_reclen = reclen;
20165331Samw 			/* NOTE: d_off is the offset for the *next* entry */
20175331Samw 			next = &(odp->d_off);
20185331Samw 			(void) strncpy(odp->d_name, zap.za_name,
20195331Samw 			    DIRENT64_NAMELEN(reclen));
20205331Samw 			odp = (dirent64_t *)((intptr_t)odp + reclen);
20215331Samw 		}
20223912Slling 		outcount += reclen;
2023789Sahrens 
2024789Sahrens 		ASSERT(outcount <= bufsize);
2025789Sahrens 
2026789Sahrens 		/* Prefetch znode */
2027869Sperrin 		if (prefetch)
20283912Slling 			dmu_prefetch(os, objnum, 0, 0);
2029789Sahrens 
2030789Sahrens 		/*
2031789Sahrens 		 * Move to the next entry, fill in the previous offset.
2032789Sahrens 		 */
2033789Sahrens 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2034789Sahrens 			zap_cursor_advance(&zc);
2035789Sahrens 			offset = zap_cursor_serialize(&zc);
2036789Sahrens 		} else {
2037789Sahrens 			offset += 1;
2038789Sahrens 		}
2039789Sahrens 		*next = offset;
2040789Sahrens 	}
2041869Sperrin 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2042789Sahrens 
2043789Sahrens 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2044789Sahrens 		iovp->iov_base += outcount;
2045789Sahrens 		iovp->iov_len -= outcount;
2046789Sahrens 		uio->uio_resid -= outcount;
2047789Sahrens 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2048789Sahrens 		/*
2049789Sahrens 		 * Reset the pointer.
2050789Sahrens 		 */
2051789Sahrens 		offset = uio->uio_loffset;
2052789Sahrens 	}
2053789Sahrens 
2054789Sahrens update:
2055885Sahrens 	zap_cursor_fini(&zc);
2056789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2057789Sahrens 		kmem_free(outbuf, bufsize);
2058789Sahrens 
2059789Sahrens 	if (error == ENOENT)
2060789Sahrens 		error = 0;
2061789Sahrens 
2062789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2063789Sahrens 
2064789Sahrens 	uio->uio_loffset = offset;
2065789Sahrens 	ZFS_EXIT(zfsvfs);
2066789Sahrens 	return (error);
2067789Sahrens }
2068789Sahrens 
20694720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4;
20704720Sfr157268 
2071789Sahrens static int
20725331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2073789Sahrens {
2074789Sahrens 	znode_t	*zp = VTOZ(vp);
2075789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2076789Sahrens 
20771773Seschrock 	/*
20781773Seschrock 	 * Regardless of whether this is required for standards conformance,
20791773Seschrock 	 * this is the logical behavior when fsync() is called on a file with
20801773Seschrock 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
20811773Seschrock 	 * going to be pushed out as part of the zil_commit().
20821773Seschrock 	 */
20831773Seschrock 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
20841773Seschrock 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
20855331Samw 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
20861773Seschrock 
20874720Sfr157268 	(void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
20884720Sfr157268 
20895367Sahrens 	ZFS_ENTER(zfsvfs);
20905367Sahrens 	ZFS_VERIFY_ZP(zp);
20912638Sperrin 	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
2092789Sahrens 	ZFS_EXIT(zfsvfs);
2093789Sahrens 	return (0);
2094789Sahrens }
2095789Sahrens 
20965331Samw 
2097789Sahrens /*
2098789Sahrens  * Get the requested file attributes and place them in the provided
2099789Sahrens  * vattr structure.
2100789Sahrens  *
2101789Sahrens  *	IN:	vp	- vnode of file.
2102789Sahrens  *		vap	- va_mask identifies requested attributes.
21035331Samw  *			  If AT_XVATTR set, then optional attrs are requested
21045331Samw  *		flags	- ATTR_NOACLCHECK (CIFS server context)
2105789Sahrens  *		cr	- credentials of caller.
21065331Samw  *		ct	- caller context
2107789Sahrens  *
2108789Sahrens  *	OUT:	vap	- attribute values.
2109789Sahrens  *
2110789Sahrens  *	RETURN:	0 (always succeeds)
2111789Sahrens  */
2112789Sahrens /* ARGSUSED */
2113789Sahrens static int
21145331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
21155331Samw     caller_context_t *ct)
2116789Sahrens {
2117789Sahrens 	znode_t *zp = VTOZ(vp);
2118789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
21195326Sek110237 	znode_phys_t *pzp;
21205331Samw 	int	error = 0;
21214543Smarks 	uint64_t links;
21225331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
21235331Samw 	xoptattr_t *xoap = NULL;
21245331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2125789Sahrens 
21265367Sahrens 	ZFS_ENTER(zfsvfs);
21275367Sahrens 	ZFS_VERIFY_ZP(zp);
21285326Sek110237 	pzp = zp->z_phys;
2129789Sahrens 
21305331Samw 	mutex_enter(&zp->z_lock);
21315331Samw 
21325331Samw 	/*
21335331Samw 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
21345331Samw 	 * Also, if we are the owner don't bother, since owner should
21355331Samw 	 * always be allowed to read basic attributes of file.
21365331Samw 	 */
21375331Samw 	if (!(pzp->zp_flags & ZFS_ACL_TRIVIAL) &&
21385331Samw 	    (pzp->zp_uid != crgetuid(cr))) {
21395331Samw 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
21405331Samw 		    skipaclchk, cr)) {
21415331Samw 			mutex_exit(&zp->z_lock);
21425331Samw 			ZFS_EXIT(zfsvfs);
21435331Samw 			return (error);
21445331Samw 		}
21455331Samw 	}
21465331Samw 
2147789Sahrens 	/*
2148789Sahrens 	 * Return all attributes.  It's cheaper to provide the answer
2149789Sahrens 	 * than to determine whether we were asked the question.
2150789Sahrens 	 */
2151789Sahrens 
2152789Sahrens 	vap->va_type = vp->v_type;
2153789Sahrens 	vap->va_mode = pzp->zp_mode & MODEMASK;
21545331Samw 	zfs_fuid_map_ids(zp, &vap->va_uid, &vap->va_gid);
2155789Sahrens 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2156789Sahrens 	vap->va_nodeid = zp->z_id;
21574543Smarks 	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
21584543Smarks 		links = pzp->zp_links + 1;
21594543Smarks 	else
21604543Smarks 		links = pzp->zp_links;
21614543Smarks 	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
2162789Sahrens 	vap->va_size = pzp->zp_size;
21631816Smarks 	vap->va_rdev = vp->v_rdev;
2164789Sahrens 	vap->va_seq = zp->z_seq;
2165789Sahrens 
21665331Samw 	/*
21675331Samw 	 * Add in any requested optional attributes and the create time.
21685331Samw 	 * Also set the corresponding bits in the returned attribute bitmap.
21695331Samw 	 */
21705331Samw 	if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
21715331Samw 		if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
21725331Samw 			xoap->xoa_archive =
21735331Samw 			    ((pzp->zp_flags & ZFS_ARCHIVE) != 0);
21745331Samw 			XVA_SET_RTN(xvap, XAT_ARCHIVE);
21755331Samw 		}
21765331Samw 
21775331Samw 		if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
21785331Samw 			xoap->xoa_readonly =
21795331Samw 			    ((pzp->zp_flags & ZFS_READONLY) != 0);
21805331Samw 			XVA_SET_RTN(xvap, XAT_READONLY);
21815331Samw 		}
21825331Samw 
21835331Samw 		if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
21845331Samw 			xoap->xoa_system =
21855331Samw 			    ((pzp->zp_flags & ZFS_SYSTEM) != 0);
21865331Samw 			XVA_SET_RTN(xvap, XAT_SYSTEM);
21875331Samw 		}
21885331Samw 
21895331Samw 		if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
21905331Samw 			xoap->xoa_hidden =
21915331Samw 			    ((pzp->zp_flags & ZFS_HIDDEN) != 0);
21925331Samw 			XVA_SET_RTN(xvap, XAT_HIDDEN);
21935331Samw 		}
21945331Samw 
21955331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
21965331Samw 			xoap->xoa_nounlink =
21975331Samw 			    ((pzp->zp_flags & ZFS_NOUNLINK) != 0);
21985331Samw 			XVA_SET_RTN(xvap, XAT_NOUNLINK);
21995331Samw 		}
22005331Samw 
22015331Samw 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
22025331Samw 			xoap->xoa_immutable =
22035331Samw 			    ((pzp->zp_flags & ZFS_IMMUTABLE) != 0);
22045331Samw 			XVA_SET_RTN(xvap, XAT_IMMUTABLE);
22055331Samw 		}
22065331Samw 
22075331Samw 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
22085331Samw 			xoap->xoa_appendonly =
22095331Samw 			    ((pzp->zp_flags & ZFS_APPENDONLY) != 0);
22105331Samw 			XVA_SET_RTN(xvap, XAT_APPENDONLY);
22115331Samw 		}
22125331Samw 
22135331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
22145331Samw 			xoap->xoa_nodump =
22155331Samw 			    ((pzp->zp_flags & ZFS_NODUMP) != 0);
22165331Samw 			XVA_SET_RTN(xvap, XAT_NODUMP);
22175331Samw 		}
22185331Samw 
22195331Samw 		if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
22205331Samw 			xoap->xoa_opaque =
22215331Samw 			    ((pzp->zp_flags & ZFS_OPAQUE) != 0);
22225331Samw 			XVA_SET_RTN(xvap, XAT_OPAQUE);
22235331Samw 		}
22245331Samw 
22255331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
22265331Samw 			xoap->xoa_av_quarantined =
22275331Samw 			    ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0);
22285331Samw 			XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
22295331Samw 		}
22305331Samw 
22315331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
22325331Samw 			xoap->xoa_av_modified =
22335331Samw 			    ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0);
22345331Samw 			XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
22355331Samw 		}
22365331Samw 
22375331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
22385331Samw 		    vp->v_type == VREG &&
22395331Samw 		    (pzp->zp_flags & ZFS_BONUS_SCANSTAMP)) {
22405331Samw 			size_t len;
22415331Samw 			dmu_object_info_t doi;
22425331Samw 
22435331Samw 			/*
22445331Samw 			 * Only VREG files have anti-virus scanstamps, so we
22455331Samw 			 * won't conflict with symlinks in the bonus buffer.
22465331Samw 			 */
22475331Samw 			dmu_object_info_from_db(zp->z_dbuf, &doi);
22485331Samw 			len = sizeof (xoap->xoa_av_scanstamp) +
22495331Samw 			    sizeof (znode_phys_t);
22505331Samw 			if (len <= doi.doi_bonus_size) {
22515331Samw 				/*
22525331Samw 				 * pzp points to the start of the
22535331Samw 				 * znode_phys_t. pzp + 1 points to the
22545331Samw 				 * first byte after the znode_phys_t.
22555331Samw 				 */
22565331Samw 				(void) memcpy(xoap->xoa_av_scanstamp,
22575331Samw 				    pzp + 1,
22585331Samw 				    sizeof (xoap->xoa_av_scanstamp));
22595331Samw 				XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
22605331Samw 			}
22615331Samw 		}
22625331Samw 
22635331Samw 		if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
22645331Samw 			ZFS_TIME_DECODE(&xoap->xoa_createtime, pzp->zp_crtime);
22655331Samw 			XVA_SET_RTN(xvap, XAT_CREATETIME);
22665331Samw 		}
22675331Samw 	}
22685331Samw 
2269789Sahrens 	ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
2270789Sahrens 	ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
2271789Sahrens 	ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
2272789Sahrens 
2273789Sahrens 	mutex_exit(&zp->z_lock);
2274789Sahrens 
2275789Sahrens 	dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks);
2276789Sahrens 
2277789Sahrens 	if (zp->z_blksz == 0) {
2278789Sahrens 		/*
2279789Sahrens 		 * Block size hasn't been set; suggest maximal I/O transfers.
2280789Sahrens 		 */
2281789Sahrens 		vap->va_blksize = zfsvfs->z_max_blksz;
2282789Sahrens 	}
2283789Sahrens 
2284789Sahrens 	ZFS_EXIT(zfsvfs);
2285789Sahrens 	return (0);
2286789Sahrens }
2287789Sahrens 
2288789Sahrens /*
2289789Sahrens  * Set the file attributes to the values contained in the
2290789Sahrens  * vattr structure.
2291789Sahrens  *
2292789Sahrens  *	IN:	vp	- vnode of file to be modified.
2293789Sahrens  *		vap	- new attribute values.
22945331Samw  *			  If AT_XVATTR set, then optional attrs are being set
2295789Sahrens  *		flags	- ATTR_UTIME set if non-default time values provided.
22965331Samw  *			- ATTR_NOACLCHECK (CIFS context only).
2297789Sahrens  *		cr	- credentials of caller.
22985331Samw  *		ct	- caller context
2299789Sahrens  *
2300789Sahrens  *	RETURN:	0 if success
2301789Sahrens  *		error code if failure
2302789Sahrens  *
2303789Sahrens  * Timestamps:
2304789Sahrens  *	vp - ctime updated, mtime updated if size changed.
2305789Sahrens  */
2306789Sahrens /* ARGSUSED */
2307789Sahrens static int
2308789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2309789Sahrens 	caller_context_t *ct)
2310789Sahrens {
23115326Sek110237 	znode_t		*zp = VTOZ(vp);
23125326Sek110237 	znode_phys_t	*pzp;
2313789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
23145326Sek110237 	zilog_t		*zilog;
2315789Sahrens 	dmu_tx_t	*tx;
23161878Smaybee 	vattr_t		oldva;
2317789Sahrens 	uint_t		mask = vap->va_mask;
23181878Smaybee 	uint_t		saved_mask;
23192796Smarks 	int		trim_mask = 0;
2320789Sahrens 	uint64_t	new_mode;
23211231Smarks 	znode_t		*attrzp;
2322789Sahrens 	int		need_policy = FALSE;
2323789Sahrens 	int		err;
23245331Samw 	zfs_fuid_info_t *fuidp = NULL;
23255331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
23265331Samw 	xoptattr_t	*xoap;
23275331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2328789Sahrens 
2329789Sahrens 	if (mask == 0)
2330789Sahrens 		return (0);
2331789Sahrens 
2332789Sahrens 	if (mask & AT_NOSET)
2333789Sahrens 		return (EINVAL);
2334789Sahrens 
23355367Sahrens 	ZFS_ENTER(zfsvfs);
23365367Sahrens 	ZFS_VERIFY_ZP(zp);
23375331Samw 
23385331Samw 	pzp = zp->z_phys;
23395331Samw 	zilog = zfsvfs->z_log;
23405331Samw 
23415331Samw 	/*
23425331Samw 	 * Make sure that if we have ephemeral uid/gid or xvattr specified
23435331Samw 	 * that file system is at proper version level
23445331Samw 	 */
23455331Samw 
23465331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
23475331Samw 	    (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
23485331Samw 	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
23495386Stimh 	    (mask & AT_XVATTR))) {
23505386Stimh 		ZFS_EXIT(zfsvfs);
23515331Samw 		return (EINVAL);
23525386Stimh 	}
23535386Stimh 
23545386Stimh 	if (mask & AT_SIZE && vp->v_type == VDIR) {
23555386Stimh 		ZFS_EXIT(zfsvfs);
2356789Sahrens 		return (EISDIR);
23575386Stimh 	}
23585386Stimh 
23595386Stimh 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
23605386Stimh 		ZFS_EXIT(zfsvfs);
23611308Smarks 		return (EINVAL);
23625386Stimh 	}
23631308Smarks 
23645331Samw 	/*
23655331Samw 	 * If this is an xvattr_t, then get a pointer to the structure of
23665331Samw 	 * optional attributes.  If this is NULL, then we have a vattr_t.
23675331Samw 	 */
23685331Samw 	xoap = xva_getxoptattr(xvap);
23695331Samw 
23705331Samw 	/*
23715331Samw 	 * Immutable files can only alter immutable bit and atime
23725331Samw 	 */
23735331Samw 	if ((pzp->zp_flags & ZFS_IMMUTABLE) &&
23745331Samw 	    ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
23755386Stimh 	    ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
23765386Stimh 		ZFS_EXIT(zfsvfs);
23775331Samw 		return (EPERM);
23785386Stimh 	}
23795386Stimh 
23805386Stimh 	if ((mask & AT_SIZE) && (pzp->zp_flags & ZFS_READONLY)) {
23815386Stimh 		ZFS_EXIT(zfsvfs);
23825331Samw 		return (EPERM);
23835386Stimh 	}
2384789Sahrens 
2385789Sahrens top:
23861231Smarks 	attrzp = NULL;
2387789Sahrens 
2388789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2389789Sahrens 		ZFS_EXIT(zfsvfs);
2390789Sahrens 		return (EROFS);
2391789Sahrens 	}
2392789Sahrens 
2393789Sahrens 	/*
2394789Sahrens 	 * First validate permissions
2395789Sahrens 	 */
2396789Sahrens 
2397789Sahrens 	if (mask & AT_SIZE) {
23985331Samw 		err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2399789Sahrens 		if (err) {
2400789Sahrens 			ZFS_EXIT(zfsvfs);
2401789Sahrens 			return (err);
2402789Sahrens 		}
24031878Smaybee 		/*
24041878Smaybee 		 * XXX - Note, we are not providing any open
24051878Smaybee 		 * mode flags here (like FNDELAY), so we may
24061878Smaybee 		 * block if there are locks present... this
24071878Smaybee 		 * should be addressed in openat().
24081878Smaybee 		 */
24091878Smaybee 		do {
24101878Smaybee 			err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
24112113Sahrens 			/* NB: we already did dmu_tx_wait() if necessary */
24121878Smaybee 		} while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
24131878Smaybee 		if (err) {
24141878Smaybee 			ZFS_EXIT(zfsvfs);
24151878Smaybee 			return (err);
24161878Smaybee 		}
2417789Sahrens 	}
2418789Sahrens 
24195331Samw 	if (mask & (AT_ATIME|AT_MTIME) ||
24205331Samw 	    ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
24215331Samw 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
24225331Samw 	    XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
24235331Samw 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
24245331Samw 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM))))
24255331Samw 		need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
24265331Samw 		    skipaclchk, cr);
2427789Sahrens 
2428789Sahrens 	if (mask & (AT_UID|AT_GID)) {
2429789Sahrens 		int	idmask = (mask & (AT_UID|AT_GID));
2430789Sahrens 		int	take_owner;
2431789Sahrens 		int	take_group;
2432789Sahrens 
2433789Sahrens 		/*
2434913Smarks 		 * NOTE: even if a new mode is being set,
2435913Smarks 		 * we may clear S_ISUID/S_ISGID bits.
2436913Smarks 		 */
2437913Smarks 
2438913Smarks 		if (!(mask & AT_MODE))
2439913Smarks 			vap->va_mode = pzp->zp_mode;
2440913Smarks 
2441913Smarks 		/*
2442789Sahrens 		 * Take ownership or chgrp to group we are a member of
2443789Sahrens 		 */
2444789Sahrens 
2445789Sahrens 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
24465331Samw 		take_group = (mask & AT_GID) &&
24475331Samw 		    zfs_groupmember(zfsvfs, vap->va_gid, cr);
2448789Sahrens 
2449789Sahrens 		/*
2450789Sahrens 		 * If both AT_UID and AT_GID are set then take_owner and
2451789Sahrens 		 * take_group must both be set in order to allow taking
2452789Sahrens 		 * ownership.
2453789Sahrens 		 *
2454789Sahrens 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2455789Sahrens 		 *
2456789Sahrens 		 */
2457789Sahrens 
2458789Sahrens 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2459789Sahrens 		    ((idmask == AT_UID) && take_owner) ||
2460789Sahrens 		    ((idmask == AT_GID) && take_group)) {
24615331Samw 			if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
24625331Samw 			    skipaclchk, cr) == 0) {
2463789Sahrens 				/*
2464789Sahrens 				 * Remove setuid/setgid for non-privileged users
2465789Sahrens 				 */
24661115Smarks 				secpolicy_setid_clear(vap, cr);
24672796Smarks 				trim_mask = (mask & (AT_UID|AT_GID));
2468789Sahrens 			} else {
2469789Sahrens 				need_policy =  TRUE;
2470789Sahrens 			}
2471789Sahrens 		} else {
2472789Sahrens 			need_policy =  TRUE;
2473789Sahrens 		}
2474789Sahrens 	}
2475789Sahrens 
24762796Smarks 	mutex_enter(&zp->z_lock);
24772796Smarks 	oldva.va_mode = pzp->zp_mode;
24785331Samw 	zfs_fuid_map_ids(zp, &oldva.va_uid, &oldva.va_gid);
24795331Samw 	if (mask & AT_XVATTR) {
24805331Samw 		if ((need_policy == FALSE) &&
24815331Samw 		    (XVA_ISSET_REQ(xvap, XAT_APPENDONLY) &&
24825331Samw 		    xoap->xoa_appendonly !=
24835331Samw 		    ((pzp->zp_flags & ZFS_APPENDONLY) != 0)) ||
24845331Samw 		    (XVA_ISSET_REQ(xvap, XAT_NOUNLINK) &&
24855331Samw 		    xoap->xoa_nounlink !=
24865331Samw 		    ((pzp->zp_flags & ZFS_NOUNLINK) != 0)) ||
24875331Samw 		    (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE) &&
24885331Samw 		    xoap->xoa_immutable !=
24895331Samw 		    ((pzp->zp_flags & ZFS_IMMUTABLE) != 0)) ||
24905331Samw 		    (XVA_ISSET_REQ(xvap, XAT_NODUMP) &&
24915331Samw 		    xoap->xoa_nodump !=
24925331Samw 		    ((pzp->zp_flags & ZFS_NODUMP) != 0)) ||
24935331Samw 		    (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED) &&
24945331Samw 		    xoap->xoa_av_modified !=
24955331Samw 		    ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0)) ||
2496*5545Smarks 		    ((XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED) &&
2497*5545Smarks 		    ((vp->v_type != VREG && xoap->xoa_av_quarantined) ||
24985331Samw 		    xoap->xoa_av_quarantined !=
2499*5545Smarks 		    ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0)))) ||
25005331Samw 		    (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) ||
25015331Samw 		    (XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
25025331Samw 			need_policy = TRUE;
25035331Samw 		}
25045331Samw 	}
25055331Samw 
25062796Smarks 	mutex_exit(&zp->z_lock);
25072796Smarks 
25082796Smarks 	if (mask & AT_MODE) {
25095331Samw 		if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
25102796Smarks 			err = secpolicy_setid_setsticky_clear(vp, vap,
25112796Smarks 			    &oldva, cr);
25122796Smarks 			if (err) {
25132796Smarks 				ZFS_EXIT(zfsvfs);
25142796Smarks 				return (err);
25152796Smarks 			}
25162796Smarks 			trim_mask |= AT_MODE;
25172796Smarks 		} else {
25182796Smarks 			need_policy = TRUE;
25192796Smarks 		}
25202796Smarks 	}
2521789Sahrens 
2522789Sahrens 	if (need_policy) {
25231115Smarks 		/*
25241115Smarks 		 * If trim_mask is set then take ownership
25252796Smarks 		 * has been granted or write_acl is present and user
25262796Smarks 		 * has the ability to modify mode.  In that case remove
25272796Smarks 		 * UID|GID and or MODE from mask so that
25281115Smarks 		 * secpolicy_vnode_setattr() doesn't revoke it.
25291115Smarks 		 */
25302796Smarks 
25312796Smarks 		if (trim_mask) {
25322796Smarks 			saved_mask = vap->va_mask;
25332796Smarks 			vap->va_mask &= ~trim_mask;
25342796Smarks 		}
2535789Sahrens 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
25365331Samw 		    (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2537789Sahrens 		if (err) {
2538789Sahrens 			ZFS_EXIT(zfsvfs);
2539789Sahrens 			return (err);
2540789Sahrens 		}
25411115Smarks 
25421115Smarks 		if (trim_mask)
25432796Smarks 			vap->va_mask |= saved_mask;
2544789Sahrens 	}
2545789Sahrens 
2546789Sahrens 	/*
2547789Sahrens 	 * secpolicy_vnode_setattr, or take ownership may have
2548789Sahrens 	 * changed va_mask
2549789Sahrens 	 */
2550789Sahrens 	mask = vap->va_mask;
2551789Sahrens 
2552789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2553789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
25545331Samw 	if (zfsvfs->z_fuid_obj == 0) {
25555331Samw 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
25565331Samw 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
25575331Samw 		    SPA_MAXBLOCKSIZE);
25585331Samw 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
25595331Samw 	} else {
25605331Samw 		dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
25615331Samw 		dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
25625331Samw 		    SPA_MAXBLOCKSIZE);
25635331Samw 	}
2564789Sahrens 
2565789Sahrens 	if (mask & AT_MODE) {
25661576Smarks 		uint64_t pmode = pzp->zp_mode;
25671576Smarks 
25681576Smarks 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2569789Sahrens 
25705331Samw 		if (pzp->zp_acl.z_acl_extern_obj) {
25715331Samw 			/* Are we upgrading ACL from old V0 format to new V1 */
25725331Samw 			if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
25735331Samw 			    pzp->zp_acl.z_acl_version ==
25745331Samw 			    ZFS_ACL_VERSION_INITIAL) {
25755331Samw 				dmu_tx_hold_free(tx,
25765331Samw 				    pzp->zp_acl.z_acl_extern_obj, 0,
25775331Samw 				    DMU_OBJECT_END);
25785331Samw 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
25795331Samw 				    0, sizeof (zfs_object_ace_t) * 2048 + 6);
25805331Samw 			} else {
25815331Samw 				dmu_tx_hold_write(tx,
25825331Samw 				    pzp->zp_acl.z_acl_extern_obj, 0,
25835331Samw 				    SPA_MAXBLOCKSIZE);
25845331Samw 			}
25855331Samw 		} else {
2586789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
25875331Samw 			    0, sizeof (zfs_object_ace_t) * 2048 + 6);
25885331Samw 		}
2589789Sahrens 	}
2590789Sahrens 
25915331Samw 	if ((mask & (AT_UID | AT_GID)) && pzp->zp_xattr != 0) {
25925331Samw 		err = zfs_zget(zp->z_zfsvfs, pzp->zp_xattr, &attrzp);
25931231Smarks 		if (err) {
25941231Smarks 			dmu_tx_abort(tx);
25951231Smarks 			ZFS_EXIT(zfsvfs);
25961231Smarks 			return (err);
25971231Smarks 		}
25981231Smarks 		dmu_tx_hold_bonus(tx, attrzp->z_id);
25991231Smarks 	}
26001231Smarks 
2601789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2602789Sahrens 	if (err) {
26031231Smarks 		if (attrzp)
26041231Smarks 			VN_RELE(ZTOV(attrzp));
2605789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
26062113Sahrens 			dmu_tx_wait(tx);
26072113Sahrens 			dmu_tx_abort(tx);
2608789Sahrens 			goto top;
2609789Sahrens 		}
26102113Sahrens 		dmu_tx_abort(tx);
2611789Sahrens 		ZFS_EXIT(zfsvfs);
2612789Sahrens 		return (err);
2613789Sahrens 	}
2614789Sahrens 
2615789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
2616789Sahrens 
2617789Sahrens 	/*
2618789Sahrens 	 * Set each attribute requested.
2619789Sahrens 	 * We group settings according to the locks they need to acquire.
2620789Sahrens 	 *
2621789Sahrens 	 * Note: you cannot set ctime directly, although it will be
2622789Sahrens 	 * updated as a side-effect of calling this function.
2623789Sahrens 	 */
2624789Sahrens 
2625789Sahrens 	mutex_enter(&zp->z_lock);
2626789Sahrens 
2627789Sahrens 	if (mask & AT_MODE) {
2628789Sahrens 		err = zfs_acl_chmod_setattr(zp, new_mode, tx);
2629789Sahrens 		ASSERT3U(err, ==, 0);
2630789Sahrens 	}
2631789Sahrens 
26321231Smarks 	if (attrzp)
26331231Smarks 		mutex_enter(&attrzp->z_lock);
26341231Smarks 
26351231Smarks 	if (mask & AT_UID) {
26365331Samw 		pzp->zp_uid = zfs_fuid_create(zfsvfs,
26375331Samw 		    vap->va_uid, ZFS_OWNER, tx, &fuidp);
26381231Smarks 		if (attrzp) {
26395331Samw 			attrzp->z_phys->zp_uid = zfs_fuid_create(zfsvfs,
26405331Samw 			    vap->va_uid,  ZFS_OWNER, tx, &fuidp);
26411231Smarks 		}
26421231Smarks 	}
26431231Smarks 
26441231Smarks 	if (mask & AT_GID) {
26455331Samw 		pzp->zp_gid = zfs_fuid_create(zfsvfs, vap->va_gid,
26465331Samw 		    ZFS_GROUP, tx, &fuidp);
26471231Smarks 		if (attrzp)
26485331Samw 			attrzp->z_phys->zp_gid = zfs_fuid_create(zfsvfs,
26495331Samw 			    vap->va_gid, ZFS_GROUP, tx, &fuidp);
26501231Smarks 	}
26511231Smarks 
26521231Smarks 	if (attrzp)
26531231Smarks 		mutex_exit(&attrzp->z_lock);
2654789Sahrens 
2655789Sahrens 	if (mask & AT_ATIME)
2656789Sahrens 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
2657789Sahrens 
2658789Sahrens 	if (mask & AT_MTIME)
2659789Sahrens 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
2660789Sahrens 
26611878Smaybee 	if (mask & AT_SIZE)
2662789Sahrens 		zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
26631878Smaybee 	else if (mask != 0)
2664789Sahrens 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
26655331Samw 	/*
26665331Samw 	 * Do this after setting timestamps to prevent timestamp
26675331Samw 	 * update from toggling bit
26685331Samw 	 */
26695331Samw 
26705331Samw 	if (xoap && (mask & AT_XVATTR)) {
26715331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
26725331Samw 			size_t len;
26735331Samw 			dmu_object_info_t doi;
26745331Samw 
26755331Samw 			ASSERT(vp->v_type == VREG);
26765331Samw 
26775331Samw 			/* Grow the bonus buffer if necessary. */
26785331Samw 			dmu_object_info_from_db(zp->z_dbuf, &doi);
26795331Samw 			len = sizeof (xoap->xoa_av_scanstamp) +
26805331Samw 			    sizeof (znode_phys_t);
26815331Samw 			if (len > doi.doi_bonus_size)
26825331Samw 				VERIFY(dmu_set_bonus(zp->z_dbuf, len, tx) == 0);
26835331Samw 		}
26845331Samw 		zfs_xvattr_set(zp, xvap);
26855331Samw 	}
2686789Sahrens 
26871878Smaybee 	if (mask != 0)
26885331Samw 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
26895331Samw 
26905331Samw 	if (fuidp)
26915331Samw 		zfs_fuid_info_free(fuidp);
2692789Sahrens 	mutex_exit(&zp->z_lock);
2693789Sahrens 
26941231Smarks 	if (attrzp)
26951231Smarks 		VN_RELE(ZTOV(attrzp));
26961231Smarks 
2697789Sahrens 	dmu_tx_commit(tx);
2698789Sahrens 
2699789Sahrens 	ZFS_EXIT(zfsvfs);
2700789Sahrens 	return (err);
2701789Sahrens }
2702789Sahrens 
27033271Smaybee typedef struct zfs_zlock {
27043271Smaybee 	krwlock_t	*zl_rwlock;	/* lock we acquired */
27053271Smaybee 	znode_t		*zl_znode;	/* znode we held */
27063271Smaybee 	struct zfs_zlock *zl_next;	/* next in list */
27073271Smaybee } zfs_zlock_t;
27083271Smaybee 
27093271Smaybee /*
27103271Smaybee  * Drop locks and release vnodes that were held by zfs_rename_lock().
27113271Smaybee  */
27123271Smaybee static void
27133271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp)
27143271Smaybee {
27153271Smaybee 	zfs_zlock_t *zl;
27163271Smaybee 
27173271Smaybee 	while ((zl = *zlpp) != NULL) {
27183271Smaybee 		if (zl->zl_znode != NULL)
27193271Smaybee 			VN_RELE(ZTOV(zl->zl_znode));
27203271Smaybee 		rw_exit(zl->zl_rwlock);
27213271Smaybee 		*zlpp = zl->zl_next;
27223271Smaybee 		kmem_free(zl, sizeof (*zl));
27233271Smaybee 	}
27243271Smaybee }
27253271Smaybee 
2726789Sahrens /*
2727789Sahrens  * Search back through the directory tree, using the ".." entries.
2728789Sahrens  * Lock each directory in the chain to prevent concurrent renames.
2729789Sahrens  * Fail any attempt to move a directory into one of its own descendants.
2730789Sahrens  * XXX - z_parent_lock can overlap with map or grow locks
2731789Sahrens  */
2732789Sahrens static int
2733789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
2734789Sahrens {
2735789Sahrens 	zfs_zlock_t	*zl;
27363638Sbillm 	znode_t		*zp = tdzp;
2737789Sahrens 	uint64_t	rootid = zp->z_zfsvfs->z_root;
2738789Sahrens 	uint64_t	*oidp = &zp->z_id;
2739789Sahrens 	krwlock_t	*rwlp = &szp->z_parent_lock;
2740789Sahrens 	krw_t		rw = RW_WRITER;
2741789Sahrens 
2742789Sahrens 	/*
2743789Sahrens 	 * First pass write-locks szp and compares to zp->z_id.
2744789Sahrens 	 * Later passes read-lock zp and compare to zp->z_parent.
2745789Sahrens 	 */
2746789Sahrens 	do {
27473271Smaybee 		if (!rw_tryenter(rwlp, rw)) {
27483271Smaybee 			/*
27493271Smaybee 			 * Another thread is renaming in this path.
27503271Smaybee 			 * Note that if we are a WRITER, we don't have any
27513271Smaybee 			 * parent_locks held yet.
27523271Smaybee 			 */
27533271Smaybee 			if (rw == RW_READER && zp->z_id > szp->z_id) {
27543271Smaybee 				/*
27553271Smaybee 				 * Drop our locks and restart
27563271Smaybee 				 */
27573271Smaybee 				zfs_rename_unlock(&zl);
27583271Smaybee 				*zlpp = NULL;
27593271Smaybee 				zp = tdzp;
27603271Smaybee 				oidp = &zp->z_id;
27613271Smaybee 				rwlp = &szp->z_parent_lock;
27623271Smaybee 				rw = RW_WRITER;
27633271Smaybee 				continue;
27643271Smaybee 			} else {
27653271Smaybee 				/*
27663271Smaybee 				 * Wait for other thread to drop its locks
27673271Smaybee 				 */
27683271Smaybee 				rw_enter(rwlp, rw);
27693271Smaybee 			}
27703271Smaybee 		}
27713271Smaybee 
2772789Sahrens 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
2773789Sahrens 		zl->zl_rwlock = rwlp;
2774789Sahrens 		zl->zl_znode = NULL;
2775789Sahrens 		zl->zl_next = *zlpp;
2776789Sahrens 		*zlpp = zl;
2777789Sahrens 
2778789Sahrens 		if (*oidp == szp->z_id)		/* We're a descendant of szp */
2779789Sahrens 			return (EINVAL);
2780789Sahrens 
2781789Sahrens 		if (*oidp == rootid)		/* We've hit the top */
2782789Sahrens 			return (0);
2783789Sahrens 
2784789Sahrens 		if (rw == RW_READER) {		/* i.e. not the first pass */
2785789Sahrens 			int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
2786789Sahrens 			if (error)
2787789Sahrens 				return (error);
2788789Sahrens 			zl->zl_znode = zp;
2789789Sahrens 		}
2790789Sahrens 		oidp = &zp->z_phys->zp_parent;
2791789Sahrens 		rwlp = &zp->z_parent_lock;
2792789Sahrens 		rw = RW_READER;
2793789Sahrens 
2794789Sahrens 	} while (zp->z_id != sdzp->z_id);
2795789Sahrens 
2796789Sahrens 	return (0);
2797789Sahrens }
2798789Sahrens 
2799789Sahrens /*
2800789Sahrens  * Move an entry from the provided source directory to the target
2801789Sahrens  * directory.  Change the entry name as indicated.
2802789Sahrens  *
2803789Sahrens  *	IN:	sdvp	- Source directory containing the "old entry".
2804789Sahrens  *		snm	- Old entry name.
2805789Sahrens  *		tdvp	- Target directory to contain the "new entry".
2806789Sahrens  *		tnm	- New entry name.
2807789Sahrens  *		cr	- credentials of caller.
28085331Samw  *		ct	- caller context
28095331Samw  *		flags	- case flags
2810789Sahrens  *
2811789Sahrens  *	RETURN:	0 if success
2812789Sahrens  *		error code if failure
2813789Sahrens  *
2814789Sahrens  * Timestamps:
2815789Sahrens  *	sdvp,tdvp - ctime|mtime updated
2816789Sahrens  */
28175331Samw /*ARGSUSED*/
2818789Sahrens static int
28195331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
28205331Samw     caller_context_t *ct, int flags)
2821789Sahrens {
2822789Sahrens 	znode_t		*tdzp, *szp, *tzp;
2823789Sahrens 	znode_t		*sdzp = VTOZ(sdvp);
2824789Sahrens 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
28255326Sek110237 	zilog_t		*zilog;
2826789Sahrens 	vnode_t		*realvp;
2827789Sahrens 	zfs_dirlock_t	*sdl, *tdl;
2828789Sahrens 	dmu_tx_t	*tx;
2829789Sahrens 	zfs_zlock_t	*zl;
28305331Samw 	int		cmp, serr, terr;
28315331Samw 	int		error = 0;
28325331Samw 	int		zflg = 0;
2833789Sahrens 
28345367Sahrens 	ZFS_ENTER(zfsvfs);
28355367Sahrens 	ZFS_VERIFY_ZP(sdzp);
28365326Sek110237 	zilog = zfsvfs->z_log;
2837789Sahrens 
2838789Sahrens 	/*
2839789Sahrens 	 * Make sure we have the real vp for the target directory.
2840789Sahrens 	 */
28415331Samw 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
2842789Sahrens 		tdvp = realvp;
2843789Sahrens 
2844789Sahrens 	if (tdvp->v_vfsp != sdvp->v_vfsp) {
2845789Sahrens 		ZFS_EXIT(zfsvfs);
2846789Sahrens 		return (EXDEV);
2847789Sahrens 	}
2848789Sahrens 
2849789Sahrens 	tdzp = VTOZ(tdvp);
28505367Sahrens 	ZFS_VERIFY_ZP(tdzp);
28515498Stimh 	if (zfsvfs->z_utf8 && u8_validate(tnm,
28525331Samw 	    strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
28535331Samw 		ZFS_EXIT(zfsvfs);
28545331Samw 		return (EILSEQ);
28555331Samw 	}
28565331Samw 
28575331Samw 	if (flags & FIGNORECASE)
28585331Samw 		zflg |= ZCILOOK;
28595331Samw 
2860789Sahrens top:
2861789Sahrens 	szp = NULL;
2862789Sahrens 	tzp = NULL;
2863789Sahrens 	zl = NULL;
2864789Sahrens 
2865789Sahrens 	/*
2866789Sahrens 	 * This is to prevent the creation of links into attribute space
2867789Sahrens 	 * by renaming a linked file into/outof an attribute directory.
2868789Sahrens 	 * See the comment in zfs_link() for why this is considered bad.
2869789Sahrens 	 */
2870789Sahrens 	if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
2871789Sahrens 	    (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
2872789Sahrens 		ZFS_EXIT(zfsvfs);
2873789Sahrens 		return (EINVAL);
2874789Sahrens 	}
2875789Sahrens 
2876789Sahrens 	/*
2877789Sahrens 	 * Lock source and target directory entries.  To prevent deadlock,
2878789Sahrens 	 * a lock ordering must be defined.  We lock the directory with
2879789Sahrens 	 * the smallest object id first, or if it's a tie, the one with
2880789Sahrens 	 * the lexically first name.
2881789Sahrens 	 */
2882789Sahrens 	if (sdzp->z_id < tdzp->z_id) {
2883789Sahrens 		cmp = -1;
2884789Sahrens 	} else if (sdzp->z_id > tdzp->z_id) {
2885789Sahrens 		cmp = 1;
2886789Sahrens 	} else {
28875331Samw 		/*
28885331Samw 		 * First compare the two name arguments without
28895331Samw 		 * considering any case folding.
28905331Samw 		 */
28915331Samw 		int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
28925331Samw 
28935331Samw 		cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
28945498Stimh 		ASSERT(error == 0 || !zfsvfs->z_utf8);
2895789Sahrens 		if (cmp == 0) {
2896789Sahrens 			/*
2897789Sahrens 			 * POSIX: "If the old argument and the new argument
2898789Sahrens 			 * both refer to links to the same existing file,
2899789Sahrens 			 * the rename() function shall return successfully
2900789Sahrens 			 * and perform no other action."
2901789Sahrens 			 */
2902789Sahrens 			ZFS_EXIT(zfsvfs);
2903789Sahrens 			return (0);
2904789Sahrens 		}
29055331Samw 		/*
29065331Samw 		 * If the file system is case-folding, then we may
29075331Samw 		 * have some more checking to do.  A case-folding file
29085331Samw 		 * system is either supporting mixed case sensitivity
29095331Samw 		 * access or is completely case-insensitive.  Note
29105331Samw 		 * that the file system is always case preserving.
29115331Samw 		 *
29125331Samw 		 * In mixed sensitivity mode case sensitive behavior
29135331Samw 		 * is the default.  FIGNORECASE must be used to
29145331Samw 		 * explicitly request case insensitive behavior.
29155331Samw 		 *
29165331Samw 		 * If the source and target names provided differ only
29175331Samw 		 * by case (e.g., a request to rename 'tim' to 'Tim'),
29185331Samw 		 * we will treat this as a special case in the
29195331Samw 		 * case-insensitive mode: as long as the source name
29205331Samw 		 * is an exact match, we will allow this to proceed as
29215331Samw 		 * a name-change request.
29225331Samw 		 */
29235498Stimh 		if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
29245498Stimh 		    (zfsvfs->z_case == ZFS_CASE_MIXED &&
29255498Stimh 		    flags & FIGNORECASE)) &&
29265331Samw 		    u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
29275331Samw 		    &error) == 0) {
29285331Samw 			/*
29295331Samw 			 * case preserving rename request, require exact
29305331Samw 			 * name matches
29315331Samw 			 */
29325331Samw 			zflg |= ZCIEXACT;
29335331Samw 			zflg &= ~ZCILOOK;
29345331Samw 		}
2935789Sahrens 	}
29365331Samw 
2937789Sahrens 	if (cmp < 0) {
29385331Samw 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
29395331Samw 		    ZEXISTS | zflg, NULL, NULL);
29405331Samw 		terr = zfs_dirent_lock(&tdl,
29415331Samw 		    tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
2942789Sahrens 	} else {
29435331Samw 		terr = zfs_dirent_lock(&tdl,
29445331Samw 		    tdzp, tnm, &tzp, zflg, NULL, NULL);
29455331Samw 		serr = zfs_dirent_lock(&sdl,
29465331Samw 		    sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
29475331Samw 		    NULL, NULL);
2948789Sahrens 	}
2949789Sahrens 
2950789Sahrens 	if (serr) {
2951789Sahrens 		/*
2952789Sahrens 		 * Source entry invalid or not there.
2953789Sahrens 		 */
2954789Sahrens 		if (!terr) {
2955789Sahrens 			zfs_dirent_unlock(tdl);
2956789Sahrens 			if (tzp)
2957789Sahrens 				VN_RELE(ZTOV(tzp));
2958789Sahrens 		}
2959789Sahrens 		if (strcmp(snm, "..") == 0)
2960789Sahrens 			serr = EINVAL;
2961789Sahrens 		ZFS_EXIT(zfsvfs);
2962789Sahrens 		return (serr);
2963789Sahrens 	}
2964789Sahrens 	if (terr) {
2965789Sahrens 		zfs_dirent_unlock(sdl);
2966789Sahrens 		VN_RELE(ZTOV(szp));
2967789Sahrens 		if (strcmp(tnm, "..") == 0)
2968789Sahrens 			terr = EINVAL;
2969789Sahrens 		ZFS_EXIT(zfsvfs);
2970789Sahrens 		return (terr);
2971789Sahrens 	}
2972789Sahrens 
2973789Sahrens 	/*
2974789Sahrens 	 * Must have write access at the source to remove the old entry
2975789Sahrens 	 * and write access at the target to create the new entry.
2976789Sahrens 	 * Note that if target and source are the same, this can be
2977789Sahrens 	 * done in a single check.
2978789Sahrens 	 */
2979789Sahrens 
2980789Sahrens 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
2981789Sahrens 		goto out;
2982789Sahrens 
2983789Sahrens 	if (ZTOV(szp)->v_type == VDIR) {
2984789Sahrens 		/*
2985789Sahrens 		 * Check to make sure rename is valid.
2986789Sahrens 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
2987789Sahrens 		 */
2988789Sahrens 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
2989789Sahrens 			goto out;
2990789Sahrens 	}
2991789Sahrens 
2992789Sahrens 	/*
2993789Sahrens 	 * Does target exist?
2994789Sahrens 	 */
2995789Sahrens 	if (tzp) {
2996789Sahrens 		/*
2997789Sahrens 		 * Source and target must be the same type.
2998789Sahrens 		 */
2999789Sahrens 		if (ZTOV(szp)->v_type == VDIR) {
3000789Sahrens 			if (ZTOV(tzp)->v_type != VDIR) {
3001789Sahrens 				error = ENOTDIR;
3002789Sahrens 				goto out;
3003789Sahrens 			}
3004789Sahrens 		} else {
3005789Sahrens 			if (ZTOV(tzp)->v_type == VDIR) {
3006789Sahrens 				error = EISDIR;
3007789Sahrens 				goto out;
3008789Sahrens 			}
3009789Sahrens 		}
3010789Sahrens 		/*
3011789Sahrens 		 * POSIX dictates that when the source and target
3012789Sahrens 		 * entries refer to the same file object, rename
3013789Sahrens 		 * must do nothing and exit without error.
3014789Sahrens 		 */
3015789Sahrens 		if (szp->z_id == tzp->z_id) {
3016789Sahrens 			error = 0;
3017789Sahrens 			goto out;
3018789Sahrens 		}
3019789Sahrens 	}
3020789Sahrens 
30215331Samw 	vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3022789Sahrens 	if (tzp)
30235331Samw 		vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
30244863Spraks 
30254863Spraks 	/*
30264863Spraks 	 * notify the target directory if it is not the same
30274863Spraks 	 * as source directory.
30284863Spraks 	 */
30294863Spraks 	if (tdvp != sdvp) {
30305331Samw 		vnevent_rename_dest_dir(tdvp, ct);
30314863Spraks 	}
3032789Sahrens 
3033789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3034789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);	/* nlink changes */
3035789Sahrens 	dmu_tx_hold_bonus(tx, sdzp->z_id);	/* nlink changes */
30361544Seschrock 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
30371544Seschrock 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
30381544Seschrock 	if (sdzp != tdzp)
3039789Sahrens 		dmu_tx_hold_bonus(tx, tdzp->z_id);	/* nlink changes */
30401544Seschrock 	if (tzp)
30411544Seschrock 		dmu_tx_hold_bonus(tx, tzp->z_id);	/* parent changes */
30423461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3043789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
3044789Sahrens 	if (error) {
3045789Sahrens 		if (zl != NULL)
3046789Sahrens 			zfs_rename_unlock(&zl);
3047789Sahrens 		zfs_dirent_unlock(sdl);
3048789Sahrens 		zfs_dirent_unlock(tdl);
3049789Sahrens 		VN_RELE(ZTOV(szp));
3050789Sahrens 		if (tzp)
3051789Sahrens 			VN_RELE(ZTOV(tzp));
3052789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
30532113Sahrens 			dmu_tx_wait(tx);
30542113Sahrens 			dmu_tx_abort(tx);
3055789Sahrens 			goto top;
3056789Sahrens 		}
30572113Sahrens 		dmu_tx_abort(tx);
3058789Sahrens 		ZFS_EXIT(zfsvfs);
3059789Sahrens 		return (error);
3060789Sahrens 	}
3061789Sahrens 
3062789Sahrens 	if (tzp)	/* Attempt to remove the existing target */
30635331Samw 		error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3064789Sahrens 
3065789Sahrens 	if (error == 0) {
3066789Sahrens 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3067789Sahrens 		if (error == 0) {
30685331Samw 			szp->z_phys->zp_flags |= ZFS_AV_MODIFIED;
30695331Samw 
3070789Sahrens 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3071789Sahrens 			ASSERT(error == 0);
30725331Samw 
30735331Samw 			zfs_log_rename(zilog, tx,
30745331Samw 			    TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0),
30755331Samw 			    sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
3076789Sahrens 		}
3077789Sahrens 	}
3078789Sahrens 
3079789Sahrens 	dmu_tx_commit(tx);
3080789Sahrens out:
3081789Sahrens 	if (zl != NULL)
3082789Sahrens 		zfs_rename_unlock(&zl);
3083789Sahrens 
3084789Sahrens 	zfs_dirent_unlock(sdl);
3085789Sahrens 	zfs_dirent_unlock(tdl);
3086789Sahrens 
3087789Sahrens 	VN_RELE(ZTOV(szp));
3088789Sahrens 	if (tzp)
3089789Sahrens 		VN_RELE(ZTOV(tzp));
3090789Sahrens 
3091789Sahrens 	ZFS_EXIT(zfsvfs);
3092789Sahrens 	return (error);
3093789Sahrens }
3094789Sahrens 
3095789Sahrens /*
3096789Sahrens  * Insert the indicated symbolic reference entry into the directory.
3097789Sahrens  *
3098789Sahrens  *	IN:	dvp	- Directory to contain new symbolic link.
3099789Sahrens  *		link	- Name for new symlink entry.
3100789Sahrens  *		vap	- Attributes of new entry.
3101789Sahrens  *		target	- Target path of new symlink.
3102789Sahrens  *		cr	- credentials of caller.
31035331Samw  *		ct	- caller context
31045331Samw  *		flags	- case flags
3105789Sahrens  *
3106789Sahrens  *	RETURN:	0 if success
3107789Sahrens  *		error code if failure
3108789Sahrens  *
3109789Sahrens  * Timestamps:
3110789Sahrens  *	dvp - ctime|mtime updated
3111789Sahrens  */
31125331Samw /*ARGSUSED*/
3113789Sahrens static int
31145331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
31155331Samw     caller_context_t *ct, int flags)
3116789Sahrens {
3117789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
3118789Sahrens 	zfs_dirlock_t	*dl;
3119789Sahrens 	dmu_tx_t	*tx;
3120789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
31215326Sek110237 	zilog_t		*zilog;
3122789Sahrens 	int		len = strlen(link);
3123789Sahrens 	int		error;
31245331Samw 	int		zflg = ZNEW;
31255331Samw 	zfs_fuid_info_t *fuidp = NULL;
3126789Sahrens 
3127789Sahrens 	ASSERT(vap->va_type == VLNK);
3128789Sahrens 
31295367Sahrens 	ZFS_ENTER(zfsvfs);
31305367Sahrens 	ZFS_VERIFY_ZP(dzp);
31315326Sek110237 	zilog = zfsvfs->z_log;
31325331Samw 
31335498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
31345331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
31355331Samw 		ZFS_EXIT(zfsvfs);
31365331Samw 		return (EILSEQ);
31375331Samw 	}
31385331Samw 	if (flags & FIGNORECASE)
31395331Samw 		zflg |= ZCILOOK;
3140789Sahrens top:
31415331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3142789Sahrens 		ZFS_EXIT(zfsvfs);
3143789Sahrens 		return (error);
3144789Sahrens 	}
3145789Sahrens 
3146789Sahrens 	if (len > MAXPATHLEN) {
3147789Sahrens 		ZFS_EXIT(zfsvfs);
3148789Sahrens 		return (ENAMETOOLONG);
3149789Sahrens 	}
3150789Sahrens 
3151789Sahrens 	/*
3152789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3153789Sahrens 	 */
31545331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
31555331Samw 	if (error) {
3156789Sahrens 		ZFS_EXIT(zfsvfs);
3157789Sahrens 		return (error);
3158789Sahrens 	}
3159789Sahrens 
3160789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3161789Sahrens 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3162789Sahrens 	dmu_tx_hold_bonus(tx, dzp->z_id);
31631544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3164789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
3165789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
31665331Samw 	if (zfsvfs->z_fuid_obj == 0) {
31675331Samw 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
31685331Samw 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
31695331Samw 		    SPA_MAXBLOCKSIZE);
31705331Samw 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
31715331Samw 	} else {
31725331Samw 		dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
31735331Samw 		dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
31745331Samw 		    SPA_MAXBLOCKSIZE);
31755331Samw 	}
3176789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
3177789Sahrens 	if (error) {
3178789Sahrens 		zfs_dirent_unlock(dl);
3179789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
31802113Sahrens 			dmu_tx_wait(tx);
31812113Sahrens 			dmu_tx_abort(tx);
3182789Sahrens 			goto top;
3183789Sahrens 		}
31842113Sahrens 		dmu_tx_abort(tx);
3185789Sahrens 		ZFS_EXIT(zfsvfs);
3186789Sahrens 		return (error);
3187789Sahrens 	}
3188789Sahrens 
3189789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
3190789Sahrens 
3191789Sahrens 	/*
3192789Sahrens 	 * Create a new object for the symlink.
3193789Sahrens 	 * Put the link content into bonus buffer if it will fit;
3194789Sahrens 	 * otherwise, store it just like any other file data.
3195789Sahrens 	 */
3196789Sahrens 	if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
31975446Sahrens 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, len, NULL, &fuidp);
3198789Sahrens 		if (len != 0)
3199789Sahrens 			bcopy(link, zp->z_phys + 1, len);
3200789Sahrens 	} else {
3201789Sahrens 		dmu_buf_t *dbp;
32021669Sperrin 
32035446Sahrens 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, NULL, &fuidp);
32041669Sperrin 		/*
32051669Sperrin 		 * Nothing can access the znode yet so no locking needed
32061669Sperrin 		 * for growing the znode's blocksize.
32071669Sperrin 		 */
32081669Sperrin 		zfs_grow_blocksize(zp, len, tx);
3209789Sahrens 
32105446Sahrens 		VERIFY(0 == dmu_buf_hold(zfsvfs->z_os,
32115446Sahrens 		    zp->z_id, 0, FTAG, &dbp));
3212789Sahrens 		dmu_buf_will_dirty(dbp, tx);
3213789Sahrens 
3214789Sahrens 		ASSERT3U(len, <=, dbp->db_size);
3215789Sahrens 		bcopy(link, dbp->db_data, len);
32161544Seschrock 		dmu_buf_rele(dbp, FTAG);
3217789Sahrens 	}
3218789Sahrens 	zp->z_phys->zp_size = len;
3219789Sahrens 
3220789Sahrens 	/*
3221789Sahrens 	 * Insert the new object into the directory.
3222789Sahrens 	 */
3223789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
3224789Sahrens out:
32255331Samw 	if (error == 0) {
32265331Samw 		uint64_t txtype = TX_SYMLINK;
32275331Samw 		if (flags & FIGNORECASE)
32285331Samw 			txtype |= TX_CI;
32295331Samw 		zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
32305331Samw 	}
32315331Samw 	if (fuidp)
32325331Samw 		zfs_fuid_info_free(fuidp);
3233789Sahrens 
3234789Sahrens 	dmu_tx_commit(tx);
3235789Sahrens 
3236789Sahrens 	zfs_dirent_unlock(dl);
3237789Sahrens 
3238789Sahrens 	VN_RELE(ZTOV(zp));
3239789Sahrens 
3240789Sahrens 	ZFS_EXIT(zfsvfs);
3241789Sahrens 	return (error);
3242789Sahrens }
3243789Sahrens 
3244789Sahrens /*
3245789Sahrens  * Return, in the buffer contained in the provided uio structure,
3246789Sahrens  * the symbolic path referred to by vp.
3247789Sahrens  *
3248789Sahrens  *	IN:	vp	- vnode of symbolic link.
3249789Sahrens  *		uoip	- structure to contain the link path.
3250789Sahrens  *		cr	- credentials of caller.
32515331Samw  *		ct	- caller context
3252789Sahrens  *
3253789Sahrens  *	OUT:	uio	- structure to contain the link path.
3254789Sahrens  *
3255789Sahrens  *	RETURN:	0 if success
3256789Sahrens  *		error code if failure
3257789Sahrens  *
3258789Sahrens  * Timestamps:
3259789Sahrens  *	vp - atime updated
3260789Sahrens  */
3261789Sahrens /* ARGSUSED */
3262789Sahrens static int
32635331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3264789Sahrens {
3265789Sahrens 	znode_t		*zp = VTOZ(vp);
3266789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3267789Sahrens 	size_t		bufsz;
3268789Sahrens 	int		error;
3269789Sahrens 
32705367Sahrens 	ZFS_ENTER(zfsvfs);
32715367Sahrens 	ZFS_VERIFY_ZP(zp);
3272789Sahrens 
3273789Sahrens 	bufsz = (size_t)zp->z_phys->zp_size;
3274789Sahrens 	if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
3275789Sahrens 		error = uiomove(zp->z_phys + 1,
3276789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
3277789Sahrens 	} else {
32781544Seschrock 		dmu_buf_t *dbp;
32791544Seschrock 		error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
32801544Seschrock 		if (error) {
3281789Sahrens 			ZFS_EXIT(zfsvfs);
3282789Sahrens 			return (error);
3283789Sahrens 		}
3284789Sahrens 		error = uiomove(dbp->db_data,
3285789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
32861544Seschrock 		dmu_buf_rele(dbp, FTAG);
3287789Sahrens 	}
3288789Sahrens 
3289789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3290789Sahrens 	ZFS_EXIT(zfsvfs);
3291789Sahrens 	return (error);
3292789Sahrens }
3293789Sahrens 
3294789Sahrens /*
3295789Sahrens  * Insert a new entry into directory tdvp referencing svp.
3296789Sahrens  *
3297789Sahrens  *	IN:	tdvp	- Directory to contain new entry.
3298789Sahrens  *		svp	- vnode of new entry.
3299789Sahrens  *		name	- name of new entry.
3300789Sahrens  *		cr	- credentials of caller.
33015331Samw  *		ct	- caller context
3302789Sahrens  *
3303789Sahrens  *	RETURN:	0 if success
3304789Sahrens  *		error code if failure
3305789Sahrens  *
3306789Sahrens  * Timestamps:
3307789Sahrens  *	tdvp - ctime|mtime updated
3308789Sahrens  *	 svp - ctime updated
3309789Sahrens  */
3310789Sahrens /* ARGSUSED */
3311789Sahrens static int
33125331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
33135331Samw     caller_context_t *ct, int flags)
3314789Sahrens {
3315789Sahrens 	znode_t		*dzp = VTOZ(tdvp);
3316789Sahrens 	znode_t		*tzp, *szp;
3317789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
33185326Sek110237 	zilog_t		*zilog;
3319789Sahrens 	zfs_dirlock_t	*dl;
3320789Sahrens 	dmu_tx_t	*tx;
3321789Sahrens 	vnode_t		*realvp;
3322789Sahrens 	int		error;
33235331Samw 	int		zf = ZNEW;
33245331Samw 	uid_t		owner;
3325789Sahrens 
3326789Sahrens 	ASSERT(tdvp->v_type == VDIR);
3327789Sahrens 
33285367Sahrens 	ZFS_ENTER(zfsvfs);
33295367Sahrens 	ZFS_VERIFY_ZP(dzp);
33305326Sek110237 	zilog = zfsvfs->z_log;
3331789Sahrens 
33325331Samw 	if (VOP_REALVP(svp, &realvp, ct) == 0)
3333789Sahrens 		svp = realvp;
3334789Sahrens 
3335789Sahrens 	if (svp->v_vfsp != tdvp->v_vfsp) {
3336789Sahrens 		ZFS_EXIT(zfsvfs);
3337789Sahrens 		return (EXDEV);
3338789Sahrens 	}
33395367Sahrens 	szp = VTOZ(svp);
33405367Sahrens 	ZFS_VERIFY_ZP(szp);
3341789Sahrens 
33425498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name,
33435331Samw 	    strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
33445331Samw 		ZFS_EXIT(zfsvfs);
33455331Samw 		return (EILSEQ);
33465331Samw 	}
33475331Samw 	if (flags & FIGNORECASE)
33485331Samw 		zf |= ZCILOOK;
33495331Samw 
3350789Sahrens top:
3351789Sahrens 	/*
3352789Sahrens 	 * We do not support links between attributes and non-attributes
3353789Sahrens 	 * because of the potential security risk of creating links
3354789Sahrens 	 * into "normal" file space in order to circumvent restrictions
3355789Sahrens 	 * imposed in attribute space.
3356789Sahrens 	 */
3357789Sahrens 	if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
3358789Sahrens 	    (dzp->z_phys->zp_flags & ZFS_XATTR)) {
3359789Sahrens 		ZFS_EXIT(zfsvfs);
3360789Sahrens 		return (EINVAL);
3361789Sahrens 	}
3362789Sahrens 
3363789Sahrens 	/*
3364789Sahrens 	 * POSIX dictates that we return EPERM here.
3365789Sahrens 	 * Better choices include ENOTSUP or EISDIR.
3366789Sahrens 	 */
3367789Sahrens 	if (svp->v_type == VDIR) {
3368789Sahrens 		ZFS_EXIT(zfsvfs);
3369789Sahrens 		return (EPERM);
3370789Sahrens 	}
3371789Sahrens 
33725331Samw 	zfs_fuid_map_id(zfsvfs, szp->z_phys->zp_uid, ZFS_OWNER, &owner);
33735331Samw 	if (owner != crgetuid(cr) &&
3374789Sahrens 	    secpolicy_basic_link(cr) != 0) {
3375789Sahrens 		ZFS_EXIT(zfsvfs);
3376789Sahrens 		return (EPERM);
3377789Sahrens 	}
3378789Sahrens 
33795331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3380789Sahrens 		ZFS_EXIT(zfsvfs);
3381789Sahrens 		return (error);
3382789Sahrens 	}
3383789Sahrens 
3384789Sahrens 	/*
3385789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3386789Sahrens 	 */
33875331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
33885331Samw 	if (error) {
3389789Sahrens 		ZFS_EXIT(zfsvfs);
3390789Sahrens 		return (error);
3391789Sahrens 	}
3392789Sahrens 
3393789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3394789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);
33951544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3396789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
3397789Sahrens 	if (error) {
3398789Sahrens 		zfs_dirent_unlock(dl);
3399789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
34002113Sahrens 			dmu_tx_wait(tx);
34012113Sahrens 			dmu_tx_abort(tx);
3402789Sahrens 			goto top;
3403789Sahrens 		}
34042113Sahrens 		dmu_tx_abort(tx);
3405789Sahrens 		ZFS_EXIT(zfsvfs);
3406789Sahrens 		return (error);
3407789Sahrens 	}
3408789Sahrens 
3409789Sahrens 	error = zfs_link_create(dl, szp, tx, 0);
3410789Sahrens 
34115331Samw 	if (error == 0) {
34125331Samw 		uint64_t txtype = TX_LINK;
34135331Samw 		if (flags & FIGNORECASE)
34145331Samw 			txtype |= TX_CI;
34155331Samw 		zfs_log_link(zilog, tx, txtype, dzp, szp, name);
34165331Samw 	}
3417789Sahrens 
3418789Sahrens 	dmu_tx_commit(tx);
3419789Sahrens 
3420789Sahrens 	zfs_dirent_unlock(dl);
3421789Sahrens 
34224863Spraks 	if (error == 0) {
34235331Samw 		vnevent_link(svp, ct);
34244863Spraks 	}
34254863Spraks 
3426789Sahrens 	ZFS_EXIT(zfsvfs);
3427789Sahrens 	return (error);
3428789Sahrens }
3429789Sahrens 
3430789Sahrens /*
3431789Sahrens  * zfs_null_putapage() is used when the file system has been force
3432789Sahrens  * unmounted. It just drops the pages.
3433789Sahrens  */
3434789Sahrens /* ARGSUSED */
3435789Sahrens static int
3436789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3437789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3438789Sahrens {
3439789Sahrens 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
3440789Sahrens 	return (0);
3441789Sahrens }
3442789Sahrens 
34432688Smaybee /*
34442688Smaybee  * Push a page out to disk, klustering if possible.
34452688Smaybee  *
34462688Smaybee  *	IN:	vp	- file to push page to.
34472688Smaybee  *		pp	- page to push.
34482688Smaybee  *		flags	- additional flags.
34492688Smaybee  *		cr	- credentials of caller.
34502688Smaybee  *
34512688Smaybee  *	OUT:	offp	- start of range pushed.
34522688Smaybee  *		lenp	- len of range pushed.
34532688Smaybee  *
34542688Smaybee  *	RETURN:	0 if success
34552688Smaybee  *		error code if failure
34562688Smaybee  *
34572688Smaybee  * NOTE: callers must have locked the page to be pushed.  On
34582688Smaybee  * exit, the page (and all other pages in the kluster) must be
34592688Smaybee  * unlocked.
34602688Smaybee  */
3461789Sahrens /* ARGSUSED */
3462789Sahrens static int
3463789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3464789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3465789Sahrens {
3466789Sahrens 	znode_t		*zp = VTOZ(vp);
3467789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3468789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
3469789Sahrens 	dmu_tx_t	*tx;
34701669Sperrin 	rl_t		*rl;
34712688Smaybee 	u_offset_t	off, koff;
34722688Smaybee 	size_t		len, klen;
34734709Smaybee 	uint64_t	filesz;
3474789Sahrens 	int		err;
3475789Sahrens 
34764709Smaybee 	filesz = zp->z_phys->zp_size;
34772688Smaybee 	off = pp->p_offset;
34782688Smaybee 	len = PAGESIZE;
34792688Smaybee 	/*
34802688Smaybee 	 * If our blocksize is bigger than the page size, try to kluster
34812688Smaybee 	 * muiltiple pages so that we write a full block (thus avoiding
34822688Smaybee 	 * a read-modify-write).
34832688Smaybee 	 */
34844709Smaybee 	if (off < filesz && zp->z_blksz > PAGESIZE) {
34852688Smaybee 		if (!ISP2(zp->z_blksz)) {
34862688Smaybee 			/* Only one block in the file. */
34872688Smaybee 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
34882688Smaybee 			koff = 0;
34892688Smaybee 		} else {
34902688Smaybee 			klen = zp->z_blksz;
34912688Smaybee 			koff = P2ALIGN(off, (u_offset_t)klen);
34922688Smaybee 		}
34932688Smaybee 		ASSERT(koff <= filesz);
34942688Smaybee 		if (koff + klen > filesz)
34952688Smaybee 			klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE);
34962688Smaybee 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
34972688Smaybee 	}
34982688Smaybee 	ASSERT3U(btop(len), ==, btopr(len));
3499789Sahrens top:
35002688Smaybee 	rl = zfs_range_lock(zp, off, len, RL_WRITER);
35011819Smaybee 	/*
35021819Smaybee 	 * Can't push pages past end-of-file.
35031819Smaybee 	 */
35044709Smaybee 	filesz = zp->z_phys->zp_size;
35054709Smaybee 	if (off >= filesz) {
35064709Smaybee 		/* ignore all pages */
35072688Smaybee 		err = 0;
35082688Smaybee 		goto out;
35094709Smaybee 	} else if (off + len > filesz) {
35104709Smaybee 		int npages = btopr(filesz - off);
35112688Smaybee 		page_t *trunc;
35122688Smaybee 
35132688Smaybee 		page_list_break(&pp, &trunc, npages);
35144709Smaybee 		/* ignore pages past end of file */
35152688Smaybee 		if (trunc)
35164709Smaybee 			pvn_write_done(trunc, flags);
35174709Smaybee 		len = filesz - off;
35181819Smaybee 	}
3519789Sahrens 
3520789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3521789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, off, len);
3522789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
3523789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
3524789Sahrens 	if (err != 0) {
3525789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
35262688Smaybee 			zfs_range_unlock(rl);
35272113Sahrens 			dmu_tx_wait(tx);
35282113Sahrens 			dmu_tx_abort(tx);
35292688Smaybee 			err = 0;
3530789Sahrens 			goto top;
3531789Sahrens 		}
35322113Sahrens 		dmu_tx_abort(tx);
3533789Sahrens 		goto out;
3534789Sahrens 	}
3535789Sahrens 
35362688Smaybee 	if (zp->z_blksz <= PAGESIZE) {
35372688Smaybee 		caddr_t va = ppmapin(pp, PROT_READ, (caddr_t)-1);
35382688Smaybee 		ASSERT3U(len, <=, PAGESIZE);
35392688Smaybee 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
35402688Smaybee 		ppmapout(va);
35412688Smaybee 	} else {
35422688Smaybee 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
35432688Smaybee 	}
35442688Smaybee 
35452688Smaybee 	if (err == 0) {
35462688Smaybee 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
35473638Sbillm 		zfs_log_write(zilog, tx, TX_WRITE, zp, off, len, 0);
35482688Smaybee 		dmu_tx_commit(tx);
35492688Smaybee 	}
35502688Smaybee 
35512688Smaybee out:
35522237Smaybee 	zfs_range_unlock(rl);
35534709Smaybee 	pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
3554789Sahrens 	if (offp)
3555789Sahrens 		*offp = off;
3556789Sahrens 	if (lenp)
3557789Sahrens 		*lenp = len;
3558789Sahrens 
3559789Sahrens 	return (err);
3560789Sahrens }
3561789Sahrens 
3562789Sahrens /*
3563789Sahrens  * Copy the portion of the file indicated from pages into the file.
3564789Sahrens  * The pages are stored in a page list attached to the files vnode.
3565789Sahrens  *
3566789Sahrens  *	IN:	vp	- vnode of file to push page data to.
3567789Sahrens  *		off	- position in file to put data.
3568789Sahrens  *		len	- amount of data to write.
3569789Sahrens  *		flags	- flags to control the operation.
3570789Sahrens  *		cr	- credentials of caller.
35715331Samw  *		ct	- caller context.
3572789Sahrens  *
3573789Sahrens  *	RETURN:	0 if success
3574789Sahrens  *		error code if failure
3575789Sahrens  *
3576789Sahrens  * Timestamps:
3577789Sahrens  *	vp - ctime|mtime updated
3578789Sahrens  */
35795331Samw /*ARGSUSED*/
3580789Sahrens static int
35815331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
35825331Samw     caller_context_t *ct)
3583789Sahrens {
3584789Sahrens 	znode_t		*zp = VTOZ(vp);
3585789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3586789Sahrens 	page_t		*pp;
3587789Sahrens 	size_t		io_len;
3588789Sahrens 	u_offset_t	io_off;
35891669Sperrin 	uint64_t	filesz;
3590789Sahrens 	int		error = 0;
3591789Sahrens 
35925367Sahrens 	ZFS_ENTER(zfsvfs);
35935367Sahrens 	ZFS_VERIFY_ZP(zp);
3594789Sahrens 
3595789Sahrens 	if (len == 0) {
3596789Sahrens 		/*
3597789Sahrens 		 * Search the entire vp list for pages >= off.
3598789Sahrens 		 */
3599789Sahrens 		error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage,
3600789Sahrens 		    flags, cr);
36011472Sperrin 		goto out;
3602789Sahrens 	}
3603789Sahrens 
36041669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
36051669Sperrin 	if (off > filesz) {
3606789Sahrens 		/* past end of file */
3607789Sahrens 		ZFS_EXIT(zfsvfs);
3608789Sahrens 		return (0);
3609789Sahrens 	}
3610789Sahrens 
36111669Sperrin 	len = MIN(len, filesz - off);
3612789Sahrens 
36131472Sperrin 	for (io_off = off; io_off < off + len; io_off += io_len) {
3614789Sahrens 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
36151669Sperrin 			pp = page_lookup(vp, io_off,
36164339Sperrin 			    (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
3617789Sahrens 		} else {
3618789Sahrens 			pp = page_lookup_nowait(vp, io_off,
36194339Sperrin 			    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
3620789Sahrens 		}
3621789Sahrens 
3622789Sahrens 		if (pp != NULL && pvn_getdirty(pp, flags)) {
3623789Sahrens 			int err;
3624789Sahrens 
3625789Sahrens 			/*
3626789Sahrens 			 * Found a dirty page to push
3627789Sahrens 			 */
36281669Sperrin 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
36291669Sperrin 			if (err)
3630789Sahrens 				error = err;
3631789Sahrens 		} else {
3632789Sahrens 			io_len = PAGESIZE;
3633789Sahrens 		}
3634789Sahrens 	}
36351472Sperrin out:
36362638Sperrin 	if ((flags & B_ASYNC) == 0)
36372638Sperrin 		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
3638789Sahrens 	ZFS_EXIT(zfsvfs);
3639789Sahrens 	return (error);
3640789Sahrens }
3641789Sahrens 
36425331Samw /*ARGSUSED*/
3643789Sahrens void
36445331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
3645789Sahrens {
3646789Sahrens 	znode_t	*zp = VTOZ(vp);
3647789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3648789Sahrens 	int error;
3649789Sahrens 
36505326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
36515446Sahrens 	if (zp->z_dbuf == NULL) {
36525446Sahrens 		/*
36535446Sahrens 		 * This fs has been unmounted, or we did
36545446Sahrens 		 * zfs_suspend/resume and it no longer exists.
36555446Sahrens 		 */
3656789Sahrens 		if (vn_has_cached_data(vp)) {
3657789Sahrens 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
3658789Sahrens 			    B_INVAL, cr);
3659789Sahrens 		}
3660789Sahrens 
36611544Seschrock 		mutex_enter(&zp->z_lock);
3662789Sahrens 		vp->v_count = 0; /* count arrives as 1 */
36635446Sahrens 		mutex_exit(&zp->z_lock);
36645446Sahrens 		zfs_znode_free(zp);
36655326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
3666789Sahrens 		VFS_RELE(zfsvfs->z_vfs);
3667789Sahrens 		return;
3668789Sahrens 	}
3669789Sahrens 
3670789Sahrens 	/*
3671789Sahrens 	 * Attempt to push any data in the page cache.  If this fails
3672789Sahrens 	 * we will get kicked out later in zfs_zinactive().
3673789Sahrens 	 */
36741298Sperrin 	if (vn_has_cached_data(vp)) {
36751298Sperrin 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
36761298Sperrin 		    cr);
36771298Sperrin 	}
3678789Sahrens 
36793461Sahrens 	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3680789Sahrens 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3681789Sahrens 
3682789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
3683789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
3684789Sahrens 		if (error) {
3685789Sahrens 			dmu_tx_abort(tx);
3686789Sahrens 		} else {
3687789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
3688789Sahrens 			mutex_enter(&zp->z_lock);
3689789Sahrens 			zp->z_atime_dirty = 0;
3690789Sahrens 			mutex_exit(&zp->z_lock);
3691789Sahrens 			dmu_tx_commit(tx);
3692789Sahrens 		}
3693789Sahrens 	}
3694789Sahrens 
3695789Sahrens 	zfs_zinactive(zp);
36965326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
3697789Sahrens }
3698789Sahrens 
3699789Sahrens /*
3700789Sahrens  * Bounds-check the seek operation.
3701789Sahrens  *
3702789Sahrens  *	IN:	vp	- vnode seeking within
3703789Sahrens  *		ooff	- old file offset
3704789Sahrens  *		noffp	- pointer to new file offset
37055331Samw  *		ct	- caller context
3706789Sahrens  *
3707789Sahrens  *	RETURN:	0 if success
3708789Sahrens  *		EINVAL if new offset invalid
3709789Sahrens  */
3710789Sahrens /* ARGSUSED */
3711789Sahrens static int
37125331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
37135331Samw     caller_context_t *ct)
3714789Sahrens {
3715789Sahrens 	if (vp->v_type == VDIR)
3716789Sahrens 		return (0);
3717789Sahrens 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
3718789Sahrens }
3719789Sahrens 
3720789Sahrens /*
3721789Sahrens  * Pre-filter the generic locking function to trap attempts to place
3722789Sahrens  * a mandatory lock on a memory mapped file.
3723789Sahrens  */
3724789Sahrens static int
3725789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
37265331Samw     flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
3727789Sahrens {
3728789Sahrens 	znode_t *zp = VTOZ(vp);
3729789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3730789Sahrens 	int error;
3731789Sahrens 
37325367Sahrens 	ZFS_ENTER(zfsvfs);
37335367Sahrens 	ZFS_VERIFY_ZP(zp);
3734789Sahrens 
3735789Sahrens 	/*
37361544Seschrock 	 * We are following the UFS semantics with respect to mapcnt
37371544Seschrock 	 * here: If we see that the file is mapped already, then we will
37381544Seschrock 	 * return an error, but we don't worry about races between this
37391544Seschrock 	 * function and zfs_map().
3740789Sahrens 	 */
37411544Seschrock 	if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) {
3742789Sahrens 		ZFS_EXIT(zfsvfs);
3743789Sahrens 		return (EAGAIN);
3744789Sahrens 	}
37455331Samw 	error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct);
3746789Sahrens 	ZFS_EXIT(zfsvfs);
3747789Sahrens 	return (error);
3748789Sahrens }
3749789Sahrens 
3750789Sahrens /*
3751789Sahrens  * If we can't find a page in the cache, we will create a new page
3752789Sahrens  * and fill it with file data.  For efficiency, we may try to fill
37531669Sperrin  * multiple pages at once (klustering).
3754789Sahrens  */
3755789Sahrens static int
3756789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
3757789Sahrens     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
3758789Sahrens {
3759789Sahrens 	znode_t *zp = VTOZ(vp);
3760789Sahrens 	page_t *pp, *cur_pp;
3761789Sahrens 	objset_t *os = zp->z_zfsvfs->z_os;
3762789Sahrens 	caddr_t va;
3763789Sahrens 	u_offset_t io_off, total;
3764789Sahrens 	uint64_t oid = zp->z_id;
3765789Sahrens 	size_t io_len;
37661669Sperrin 	uint64_t filesz;
3767789Sahrens 	int err;
3768789Sahrens 
3769789Sahrens 	/*
3770789Sahrens 	 * If we are only asking for a single page don't bother klustering.
3771789Sahrens 	 */
37721669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
37732688Smaybee 	if (off >= filesz)
37742688Smaybee 		return (EFAULT);
37752688Smaybee 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
3776789Sahrens 		io_off = off;
3777789Sahrens 		io_len = PAGESIZE;
3778789Sahrens 		pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr);
3779789Sahrens 	} else {
3780789Sahrens 		/*
3781789Sahrens 		 * Try to fill a kluster of pages (a blocks worth).
3782789Sahrens 		 */
3783789Sahrens 		size_t klen;
3784789Sahrens 		u_offset_t koff;
3785789Sahrens 
3786789Sahrens 		if (!ISP2(zp->z_blksz)) {
3787789Sahrens 			/* Only one block in the file. */
3788789Sahrens 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
3789789Sahrens 			koff = 0;
3790789Sahrens 		} else {
37913131Sgw25295 			/*
37923131Sgw25295 			 * It would be ideal to align our offset to the
37933131Sgw25295 			 * blocksize but doing so has resulted in some
37943131Sgw25295 			 * strange application crashes. For now, we
37953131Sgw25295 			 * leave the offset as is and only adjust the
37963131Sgw25295 			 * length if we are off the end of the file.
37973131Sgw25295 			 */
37983131Sgw25295 			koff = off;
3799789Sahrens 			klen = plsz;
3800789Sahrens 		}
38011819Smaybee 		ASSERT(koff <= filesz);
38021819Smaybee 		if (koff + klen > filesz)
38031819Smaybee 			klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff;
38042688Smaybee 		ASSERT3U(off, >=, koff);
38052688Smaybee 		ASSERT3U(off, <, koff + klen);
3806789Sahrens 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
38074339Sperrin 		    &io_len, koff, klen, 0);
3808789Sahrens 	}
3809789Sahrens 	if (pp == NULL) {
3810789Sahrens 		/*
3811789Sahrens 		 * Some other thread entered the page before us.
3812789Sahrens 		 * Return to zfs_getpage to retry the lookup.
3813789Sahrens 		 */
3814789Sahrens 		*pl = NULL;
3815789Sahrens 		return (0);
3816789Sahrens 	}
3817789Sahrens 
3818789Sahrens 	/*
3819789Sahrens 	 * Fill the pages in the kluster.
3820789Sahrens 	 */
3821789Sahrens 	cur_pp = pp;
3822789Sahrens 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
38232688Smaybee 		ASSERT3U(io_off, ==, cur_pp->p_offset);
3824789Sahrens 		va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1);
38251544Seschrock 		err = dmu_read(os, oid, io_off, PAGESIZE, va);
3826789Sahrens 		ppmapout(va);
3827789Sahrens 		if (err) {
3828789Sahrens 			/* On error, toss the entire kluster */
3829789Sahrens 			pvn_read_done(pp, B_ERROR);
3830789Sahrens 			return (err);
3831789Sahrens 		}
3832789Sahrens 		cur_pp = cur_pp->p_next;
3833789Sahrens 	}
3834789Sahrens out:
3835789Sahrens 	/*
3836789Sahrens 	 * Fill in the page list array from the kluster.  If
3837789Sahrens 	 * there are too many pages in the kluster, return
3838789Sahrens 	 * as many pages as possible starting from the desired
3839789Sahrens 	 * offset `off'.
3840789Sahrens 	 * NOTE: the page list will always be null terminated.
3841789Sahrens 	 */
3842789Sahrens 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
3843789Sahrens 
3844789Sahrens 	return (0);
3845789Sahrens }
3846789Sahrens 
3847789Sahrens /*
3848789Sahrens  * Return pointers to the pages for the file region [off, off + len]
3849789Sahrens  * in the pl array.  If plsz is greater than len, this function may
3850789Sahrens  * also return page pointers from before or after the specified
3851789Sahrens  * region (i.e. some region [off', off' + plsz]).  These additional
3852789Sahrens  * pages are only returned if they are already in the cache, or were
3853789Sahrens  * created as part of a klustered read.
3854789Sahrens  *
3855789Sahrens  *	IN:	vp	- vnode of file to get data from.
3856789Sahrens  *		off	- position in file to get data from.
3857789Sahrens  *		len	- amount of data to retrieve.
3858789Sahrens  *		plsz	- length of provided page list.
3859789Sahrens  *		seg	- segment to obtain pages for.
3860789Sahrens  *		addr	- virtual address of fault.
3861789Sahrens  *		rw	- mode of created pages.
3862789Sahrens  *		cr	- credentials of caller.
38635331Samw  *		ct	- caller context.
3864789Sahrens  *
3865789Sahrens  *	OUT:	protp	- protection mode of created pages.
3866789Sahrens  *		pl	- list of pages created.
3867789Sahrens  *
3868789Sahrens  *	RETURN:	0 if success
3869789Sahrens  *		error code if failure
3870789Sahrens  *
3871789Sahrens  * Timestamps:
3872789Sahrens  *	vp - atime updated
3873789Sahrens  */
3874789Sahrens /* ARGSUSED */
3875789Sahrens static int
3876789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
3877789Sahrens 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
38785331Samw 	enum seg_rw rw, cred_t *cr, caller_context_t *ct)
3879789Sahrens {
3880789Sahrens 	znode_t		*zp = VTOZ(vp);
3881789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3882789Sahrens 	page_t		*pp, **pl0 = pl;
38832752Sperrin 	int		need_unlock = 0, err = 0;
38842752Sperrin 	offset_t	orig_off;
3885789Sahrens 
38865367Sahrens 	ZFS_ENTER(zfsvfs);
38875367Sahrens 	ZFS_VERIFY_ZP(zp);
3888789Sahrens 
3889789Sahrens 	if (protp)
3890789Sahrens 		*protp = PROT_ALL;
3891789Sahrens 
3892789Sahrens 	/* no faultahead (for now) */
3893789Sahrens 	if (pl == NULL) {
3894789Sahrens 		ZFS_EXIT(zfsvfs);
3895789Sahrens 		return (0);
3896789Sahrens 	}
3897789Sahrens 
3898789Sahrens 	/* can't fault past EOF */
3899789Sahrens 	if (off >= zp->z_phys->zp_size) {
3900789Sahrens 		ZFS_EXIT(zfsvfs);
3901789Sahrens 		return (EFAULT);
3902789Sahrens 	}
39032752Sperrin 	orig_off = off;
3904789Sahrens 
3905789Sahrens 	/*
3906789Sahrens 	 * If we already own the lock, then we must be page faulting
3907789Sahrens 	 * in the middle of a write to this file (i.e., we are writing
3908789Sahrens 	 * to this file using data from a mapped region of the file).
3909789Sahrens 	 */
39102752Sperrin 	if (rw_owner(&zp->z_map_lock) != curthread) {
3911789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
3912789Sahrens 		need_unlock = TRUE;
3913789Sahrens 	}
3914789Sahrens 
3915789Sahrens 	/*
3916789Sahrens 	 * Loop through the requested range [off, off + len] looking
3917789Sahrens 	 * for pages.  If we don't find a page, we will need to create
3918789Sahrens 	 * a new page and fill it with data from the file.
3919789Sahrens 	 */
3920789Sahrens 	while (len > 0) {
3921789Sahrens 		if (plsz < PAGESIZE)
3922789Sahrens 			break;
3923789Sahrens 		if (pp = page_lookup(vp, off, SE_SHARED)) {
3924789Sahrens 			*pl++ = pp;
3925789Sahrens 			off += PAGESIZE;
3926789Sahrens 			addr += PAGESIZE;
3927789Sahrens 			len -= PAGESIZE;
3928789Sahrens 			plsz -= PAGESIZE;
3929789Sahrens 		} else {
3930789Sahrens 			err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw);
39312752Sperrin 			if (err)
39322752Sperrin 				goto out;
3933789Sahrens 			/*
3934789Sahrens 			 * klustering may have changed our region
3935789Sahrens 			 * to be block aligned.
3936789Sahrens 			 */
3937789Sahrens 			if (((pp = *pl) != 0) && (off != pp->p_offset)) {
3938789Sahrens 				int delta = off - pp->p_offset;
3939789Sahrens 				len += delta;
3940789Sahrens 				off -= delta;
3941789Sahrens 				addr -= delta;
3942789Sahrens 			}
3943789Sahrens 			while (*pl) {
3944789Sahrens 				pl++;
3945789Sahrens 				off += PAGESIZE;
3946789Sahrens 				addr += PAGESIZE;
3947789Sahrens 				plsz -= PAGESIZE;
3948789Sahrens 				if (len > PAGESIZE)
3949789Sahrens 					len -= PAGESIZE;
3950789Sahrens 				else
3951789Sahrens 					len = 0;
3952789Sahrens 			}
3953789Sahrens 		}
3954789Sahrens 	}
3955789Sahrens 
3956789Sahrens 	/*
3957789Sahrens 	 * Fill out the page array with any pages already in the cache.
3958789Sahrens 	 */
3959789Sahrens 	while (plsz > 0) {
3960789Sahrens 		pp = page_lookup_nowait(vp, off, SE_SHARED);
3961789Sahrens 		if (pp == NULL)
3962789Sahrens 			break;
3963789Sahrens 		*pl++ = pp;
3964789Sahrens 		off += PAGESIZE;
3965789Sahrens 		plsz -= PAGESIZE;
3966789Sahrens 	}
3967789Sahrens 
3968789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3969789Sahrens out:
39702752Sperrin 	/*
39712752Sperrin 	 * We can't grab the range lock for the page as reader which would
39722752Sperrin 	 * stop truncation as this leads to deadlock. So we need to recheck
39732752Sperrin 	 * the file size.
39742752Sperrin 	 */
39752752Sperrin 	if (orig_off >= zp->z_phys->zp_size)
39762752Sperrin 		err = EFAULT;
39772752Sperrin 	if (err) {
39782752Sperrin 		/*
39792752Sperrin 		 * Release any pages we have previously locked.
39802752Sperrin 		 */
39812752Sperrin 		while (pl > pl0)
39822752Sperrin 			page_unlock(*--pl);
39832752Sperrin 	}
39842752Sperrin 
3985789Sahrens 	*pl = NULL;
3986789Sahrens 
3987789Sahrens 	if (need_unlock)
3988789Sahrens 		rw_exit(&zp->z_map_lock);
3989789Sahrens 
3990789Sahrens 	ZFS_EXIT(zfsvfs);
3991789Sahrens 	return (err);
3992789Sahrens }
3993789Sahrens 
39941544Seschrock /*
39951544Seschrock  * Request a memory map for a section of a file.  This code interacts
39961544Seschrock  * with common code and the VM system as follows:
39971544Seschrock  *
39981544Seschrock  *	common code calls mmap(), which ends up in smmap_common()
39991544Seschrock  *
40001544Seschrock  *	this calls VOP_MAP(), which takes you into (say) zfs
40011544Seschrock  *
40021544Seschrock  *	zfs_map() calls as_map(), passing segvn_create() as the callback
40031544Seschrock  *
40041544Seschrock  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
40051544Seschrock  *
40061544Seschrock  *	zfs_addmap() updates z_mapcnt
40071544Seschrock  */
40085331Samw /*ARGSUSED*/
4009789Sahrens static int
4010789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
40115331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
40125331Samw     caller_context_t *ct)
4013789Sahrens {
4014789Sahrens 	znode_t *zp = VTOZ(vp);
4015789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4016789Sahrens 	segvn_crargs_t	vn_a;
4017789Sahrens 	int		error;
4018789Sahrens 
40195331Samw 	if ((prot & PROT_WRITE) &&
40205331Samw 	    (zp->z_phys->zp_flags & (ZFS_IMMUTABLE | ZFS_READONLY |
40215331Samw 	    ZFS_APPENDONLY)))
40225331Samw 		return (EPERM);
40235331Samw 
40245367Sahrens 	ZFS_ENTER(zfsvfs);
40255367Sahrens 	ZFS_VERIFY_ZP(zp);
4026789Sahrens 
4027789Sahrens 	if (vp->v_flag & VNOMAP) {
4028789Sahrens 		ZFS_EXIT(zfsvfs);
4029789Sahrens 		return (ENOSYS);
4030789Sahrens 	}
4031789Sahrens 
4032789Sahrens 	if (off < 0 || len > MAXOFFSET_T - off) {
4033789Sahrens 		ZFS_EXIT(zfsvfs);
4034789Sahrens 		return (ENXIO);
4035789Sahrens 	}
4036789Sahrens 
4037789Sahrens 	if (vp->v_type != VREG) {
4038789Sahrens 		ZFS_EXIT(zfsvfs);
4039789Sahrens 		return (ENODEV);
4040789Sahrens 	}
4041789Sahrens 
4042789Sahrens 	/*
4043789Sahrens 	 * If file is locked, disallow mapping.
4044789Sahrens 	 */
40451544Seschrock 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) {
40461544Seschrock 		ZFS_EXIT(zfsvfs);
40471544Seschrock 		return (EAGAIN);
4048789Sahrens 	}
4049789Sahrens 
4050789Sahrens 	as_rangelock(as);
4051789Sahrens 	if ((flags & MAP_FIXED) == 0) {
4052789Sahrens 		map_addr(addrp, len, off, 1, flags);
4053789Sahrens 		if (*addrp == NULL) {
4054789Sahrens 			as_rangeunlock(as);
4055789Sahrens 			ZFS_EXIT(zfsvfs);
4056789Sahrens 			return (ENOMEM);
4057789Sahrens 		}
4058789Sahrens 	} else {
4059789Sahrens 		/*
4060789Sahrens 		 * User specified address - blow away any previous mappings
4061789Sahrens 		 */
4062789Sahrens 		(void) as_unmap(as, *addrp, len);
4063789Sahrens 	}
4064789Sahrens 
4065789Sahrens 	vn_a.vp = vp;
4066789Sahrens 	vn_a.offset = (u_offset_t)off;
4067789Sahrens 	vn_a.type = flags & MAP_TYPE;
4068789Sahrens 	vn_a.prot = prot;
4069789Sahrens 	vn_a.maxprot = maxprot;
4070789Sahrens 	vn_a.cred = cr;
4071789Sahrens 	vn_a.amp = NULL;
4072789Sahrens 	vn_a.flags = flags & ~MAP_TYPE;
40731417Skchow 	vn_a.szc = 0;
40741417Skchow 	vn_a.lgrp_mem_policy_flags = 0;
4075789Sahrens 
4076789Sahrens 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
4077789Sahrens 
4078789Sahrens 	as_rangeunlock(as);
4079789Sahrens 	ZFS_EXIT(zfsvfs);
4080789Sahrens 	return (error);
4081789Sahrens }
4082789Sahrens 
4083789Sahrens /* ARGSUSED */
4084789Sahrens static int
4085789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
40865331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
40875331Samw     caller_context_t *ct)
4088789Sahrens {
40891544Seschrock 	uint64_t pages = btopr(len);
40901544Seschrock 
40911544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4092789Sahrens 	return (0);
4093789Sahrens }
4094789Sahrens 
40951773Seschrock /*
40961773Seschrock  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
40971773Seschrock  * more accurate mtime for the associated file.  Since we don't have a way of
40981773Seschrock  * detecting when the data was actually modified, we have to resort to
40991773Seschrock  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
41001773Seschrock  * last page is pushed.  The problem occurs when the msync() call is omitted,
41011773Seschrock  * which by far the most common case:
41021773Seschrock  *
41031773Seschrock  * 	open()
41041773Seschrock  * 	mmap()
41051773Seschrock  * 	<modify memory>
41061773Seschrock  * 	munmap()
41071773Seschrock  * 	close()
41081773Seschrock  * 	<time lapse>
41091773Seschrock  * 	putpage() via fsflush
41101773Seschrock  *
41111773Seschrock  * If we wait until fsflush to come along, we can have a modification time that
41121773Seschrock  * is some arbitrary point in the future.  In order to prevent this in the
41131773Seschrock  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
41141773Seschrock  * torn down.
41151773Seschrock  */
4116789Sahrens /* ARGSUSED */
4117789Sahrens static int
4118789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
41195331Samw     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
41205331Samw     caller_context_t *ct)
4121789Sahrens {
41221544Seschrock 	uint64_t pages = btopr(len);
41231544Seschrock 
41241544Seschrock 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
41251544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
41261773Seschrock 
41271773Seschrock 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
41281773Seschrock 	    vn_has_cached_data(vp))
41295331Samw 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
41301773Seschrock 
4131789Sahrens 	return (0);
4132789Sahrens }
4133789Sahrens 
4134789Sahrens /*
4135789Sahrens  * Free or allocate space in a file.  Currently, this function only
4136789Sahrens  * supports the `F_FREESP' command.  However, this command is somewhat
4137789Sahrens  * misnamed, as its functionality includes the ability to allocate as
4138789Sahrens  * well as free space.
4139789Sahrens  *
4140789Sahrens  *	IN:	vp	- vnode of file to free data in.
4141789Sahrens  *		cmd	- action to take (only F_FREESP supported).
4142789Sahrens  *		bfp	- section of file to free/alloc.
4143789Sahrens  *		flag	- current file open mode flags.
4144789Sahrens  *		offset	- current file offset.
4145789Sahrens  *		cr	- credentials of caller [UNUSED].
41465331Samw  *		ct	- caller context.
4147789Sahrens  *
4148789Sahrens  *	RETURN:	0 if success
4149789Sahrens  *		error code if failure
4150789Sahrens  *
4151789Sahrens  * Timestamps:
4152789Sahrens  *	vp - ctime|mtime updated
4153789Sahrens  */
4154789Sahrens /* ARGSUSED */
4155789Sahrens static int
4156789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4157789Sahrens     offset_t offset, cred_t *cr, caller_context_t *ct)
4158789Sahrens {
4159789Sahrens 	znode_t		*zp = VTOZ(vp);
4160789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4161789Sahrens 	uint64_t	off, len;
4162789Sahrens 	int		error;
4163789Sahrens 
41645367Sahrens 	ZFS_ENTER(zfsvfs);
41655367Sahrens 	ZFS_VERIFY_ZP(zp);
4166789Sahrens 
4167789Sahrens top:
4168789Sahrens 	if (cmd != F_FREESP) {
4169789Sahrens 		ZFS_EXIT(zfsvfs);
4170789Sahrens 		return (EINVAL);
4171789Sahrens 	}
4172789Sahrens 
4173789Sahrens 	if (error = convoff(vp, bfp, 0, offset)) {
4174789Sahrens 		ZFS_EXIT(zfsvfs);
4175789Sahrens 		return (error);
4176789Sahrens 	}
4177789Sahrens 
4178789Sahrens 	if (bfp->l_len < 0) {
4179789Sahrens 		ZFS_EXIT(zfsvfs);
4180789Sahrens 		return (EINVAL);
4181789Sahrens 	}
4182789Sahrens 
4183789Sahrens 	off = bfp->l_start;
41841669Sperrin 	len = bfp->l_len; /* 0 means from off to end of file */
41851878Smaybee 
41861878Smaybee 	do {
41871878Smaybee 		error = zfs_freesp(zp, off, len, flag, TRUE);
41882113Sahrens 		/* NB: we already did dmu_tx_wait() if necessary */
41891878Smaybee 	} while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
4190789Sahrens 
4191789Sahrens 	ZFS_EXIT(zfsvfs);
4192789Sahrens 	return (error);
4193789Sahrens }
4194789Sahrens 
41955331Samw /*ARGSUSED*/
4196789Sahrens static int
41975331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4198789Sahrens {
4199789Sahrens 	znode_t		*zp = VTOZ(vp);
4200789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
42015326Sek110237 	uint32_t	gen;
4202789Sahrens 	uint64_t	object = zp->z_id;
4203789Sahrens 	zfid_short_t	*zfid;
4204789Sahrens 	int		size, i;
4205789Sahrens 
42065367Sahrens 	ZFS_ENTER(zfsvfs);
42075367Sahrens 	ZFS_VERIFY_ZP(zp);
42085326Sek110237 	gen = (uint32_t)zp->z_gen;
4209789Sahrens 
4210789Sahrens 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4211789Sahrens 	if (fidp->fid_len < size) {
4212789Sahrens 		fidp->fid_len = size;
42131512Sek110237 		ZFS_EXIT(zfsvfs);
4214789Sahrens 		return (ENOSPC);
4215789Sahrens 	}
4216789Sahrens 
4217789Sahrens 	zfid = (zfid_short_t *)fidp;
4218789Sahrens 
4219789Sahrens 	zfid->zf_len = size;
4220789Sahrens 
4221789Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
4222789Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4223789Sahrens 
4224789Sahrens 	/* Must have a non-zero generation number to distinguish from .zfs */
4225789Sahrens 	if (gen == 0)
4226789Sahrens 		gen = 1;
4227789Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
4228789Sahrens 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4229789Sahrens 
4230789Sahrens 	if (size == LONG_FID_LEN) {
4231789Sahrens 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
4232789Sahrens 		zfid_long_t	*zlfid;
4233789Sahrens 
4234789Sahrens 		zlfid = (zfid_long_t *)fidp;
4235789Sahrens 
4236789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4237789Sahrens 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4238789Sahrens 
4239789Sahrens 		/* XXX - this should be the generation number for the objset */
4240789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4241789Sahrens 			zlfid->zf_setgen[i] = 0;
4242789Sahrens 	}
4243789Sahrens 
4244789Sahrens 	ZFS_EXIT(zfsvfs);
4245789Sahrens 	return (0);
4246789Sahrens }
4247789Sahrens 
4248789Sahrens static int
42495331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
42505331Samw     caller_context_t *ct)
4251789Sahrens {
4252789Sahrens 	znode_t		*zp, *xzp;
4253789Sahrens 	zfsvfs_t	*zfsvfs;
4254789Sahrens 	zfs_dirlock_t	*dl;
4255789Sahrens 	int		error;
4256789Sahrens 
4257789Sahrens 	switch (cmd) {
4258789Sahrens 	case _PC_LINK_MAX:
4259789Sahrens 		*valp = ULONG_MAX;
4260789Sahrens 		return (0);
4261789Sahrens 
4262789Sahrens 	case _PC_FILESIZEBITS:
4263789Sahrens 		*valp = 64;
4264789Sahrens 		return (0);
4265789Sahrens 
4266789Sahrens 	case _PC_XATTR_EXISTS:
4267789Sahrens 		zp = VTOZ(vp);
4268789Sahrens 		zfsvfs = zp->z_zfsvfs;
42695367Sahrens 		ZFS_ENTER(zfsvfs);
42705367Sahrens 		ZFS_VERIFY_ZP(zp);
4271789Sahrens 		*valp = 0;
4272789Sahrens 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
42735331Samw 		    ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4274789Sahrens 		if (error == 0) {
4275789Sahrens 			zfs_dirent_unlock(dl);
4276789Sahrens 			if (!zfs_dirempty(xzp))
4277789Sahrens 				*valp = 1;
4278789Sahrens 			VN_RELE(ZTOV(xzp));
4279789Sahrens 		} else if (error == ENOENT) {
4280789Sahrens 			/*
4281789Sahrens 			 * If there aren't extended attributes, it's the
4282789Sahrens 			 * same as having zero of them.
4283789Sahrens 			 */
4284789Sahrens 			error = 0;
4285789Sahrens 		}
4286789Sahrens 		ZFS_EXIT(zfsvfs);
4287789Sahrens 		return (error);
4288789Sahrens 
42895331Samw 	case _PC_SATTR_ENABLED:
42905331Samw 	case _PC_SATTR_EXISTS:
42915331Samw 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) &&
42925331Samw 		    (vp->v_type == VREG || vp->v_type == VDIR);
42935331Samw 		return (0);
42945331Samw 
4295789Sahrens 	case _PC_ACL_ENABLED:
4296789Sahrens 		*valp = _ACL_ACE_ENABLED;
4297789Sahrens 		return (0);
4298789Sahrens 
4299789Sahrens 	case _PC_MIN_HOLE_SIZE:
4300789Sahrens 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
4301789Sahrens 		return (0);
4302789Sahrens 
4303789Sahrens 	default:
43045331Samw 		return (fs_pathconf(vp, cmd, valp, cr, ct));
4305789Sahrens 	}
4306789Sahrens }
4307789Sahrens 
4308789Sahrens /*ARGSUSED*/
4309789Sahrens static int
43105331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
43115331Samw     caller_context_t *ct)
4312789Sahrens {
4313789Sahrens 	znode_t *zp = VTOZ(vp);
4314789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4315789Sahrens 	int error;
43165331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4317789Sahrens 
43185367Sahrens 	ZFS_ENTER(zfsvfs);
43195367Sahrens 	ZFS_VERIFY_ZP(zp);
43205331Samw 	error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4321789Sahrens 	ZFS_EXIT(zfsvfs);
4322789Sahrens 
4323789Sahrens 	return (error);
4324789Sahrens }
4325789Sahrens 
4326789Sahrens /*ARGSUSED*/
4327789Sahrens static int
43285331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
43295331Samw     caller_context_t *ct)
4330789Sahrens {
4331789Sahrens 	znode_t *zp = VTOZ(vp);
4332789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4333789Sahrens 	int error;
43345331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4335789Sahrens 
43365367Sahrens 	ZFS_ENTER(zfsvfs);
43375367Sahrens 	ZFS_VERIFY_ZP(zp);
43385331Samw 	error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4339789Sahrens 	ZFS_EXIT(zfsvfs);
4340789Sahrens 	return (error);
4341789Sahrens }
4342789Sahrens 
4343789Sahrens /*
4344789Sahrens  * Predeclare these here so that the compiler assumes that
4345789Sahrens  * this is an "old style" function declaration that does
4346789Sahrens  * not include arguments => we won't get type mismatch errors
4347789Sahrens  * in the initializations that follow.
4348789Sahrens  */
4349789Sahrens static int zfs_inval();
4350789Sahrens static int zfs_isdir();
4351789Sahrens 
4352789Sahrens static int
4353789Sahrens zfs_inval()
4354789Sahrens {
4355789Sahrens 	return (EINVAL);
4356789Sahrens }
4357789Sahrens 
4358789Sahrens static int
4359789Sahrens zfs_isdir()
4360789Sahrens {
4361789Sahrens 	return (EISDIR);
4362789Sahrens }
4363789Sahrens /*
4364789Sahrens  * Directory vnode operations template
4365789Sahrens  */
4366789Sahrens vnodeops_t *zfs_dvnodeops;
4367789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = {
43683898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
43693898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
43703898Srsb 	VOPNAME_READ,		{ .error = zfs_isdir },
43713898Srsb 	VOPNAME_WRITE,		{ .error = zfs_isdir },
43723898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
43733898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
43743898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
43753898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
43763898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
43773898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
43783898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
43793898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
43803898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
43813898Srsb 	VOPNAME_MKDIR,		{ .vop_mkdir = zfs_mkdir },
43823898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
43833898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
43843898Srsb 	VOPNAME_SYMLINK,	{ .vop_symlink = zfs_symlink },
43853898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
43863898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
43873898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
43883898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
43893898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
43903898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
43913898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
43924863Spraks 	VOPNAME_VNEVENT, 	{ .vop_vnevent = fs_vnevent_support },
43933898Srsb 	NULL,			NULL
4394789Sahrens };
4395789Sahrens 
4396789Sahrens /*
4397789Sahrens  * Regular file vnode operations template
4398789Sahrens  */
4399789Sahrens vnodeops_t *zfs_fvnodeops;
4400789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = {
44013898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
44023898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
44033898Srsb 	VOPNAME_READ,		{ .vop_read = zfs_read },
44043898Srsb 	VOPNAME_WRITE,		{ .vop_write = zfs_write },
44053898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
44063898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
44073898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
44083898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
44093898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
44103898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
44113898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
44123898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
44133898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
44143898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
44153898Srsb 	VOPNAME_FRLOCK,		{ .vop_frlock = zfs_frlock },
44163898Srsb 	VOPNAME_SPACE,		{ .vop_space = zfs_space },
44173898Srsb 	VOPNAME_GETPAGE,	{ .vop_getpage = zfs_getpage },
44183898Srsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = zfs_putpage },
44193898Srsb 	VOPNAME_MAP,		{ .vop_map = zfs_map },
44203898Srsb 	VOPNAME_ADDMAP,		{ .vop_addmap = zfs_addmap },
44213898Srsb 	VOPNAME_DELMAP,		{ .vop_delmap = zfs_delmap },
44223898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
44233898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
44243898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
44253898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
44263898Srsb 	NULL,			NULL
4427789Sahrens };
4428789Sahrens 
4429789Sahrens /*
4430789Sahrens  * Symbolic link vnode operations template
4431789Sahrens  */
4432789Sahrens vnodeops_t *zfs_symvnodeops;
4433789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = {
44343898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
44353898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
44363898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
44373898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
44383898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = zfs_readlink },
44393898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
44403898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
44413898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
44423898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
44433898Srsb 	NULL,			NULL
4444789Sahrens };
4445789Sahrens 
4446789Sahrens /*
4447789Sahrens  * Extended attribute directory vnode operations template
4448789Sahrens  *	This template is identical to the directory vnodes
4449789Sahrens  *	operation template except for restricted operations:
4450789Sahrens  *		VOP_MKDIR()
4451789Sahrens  *		VOP_SYMLINK()
4452789Sahrens  * Note that there are other restrictions embedded in:
4453789Sahrens  *	zfs_create()	- restrict type to VREG
4454789Sahrens  *	zfs_link()	- no links into/out of attribute space
4455789Sahrens  *	zfs_rename()	- no moves into/out of attribute space
4456789Sahrens  */
4457789Sahrens vnodeops_t *zfs_xdvnodeops;
4458789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = {
44593898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
44603898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
44613898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
44623898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
44633898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
44643898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
44653898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
44663898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
44673898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
44683898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
44693898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
44703898Srsb 	VOPNAME_MKDIR,		{ .error = zfs_inval },
44713898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
44723898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
44733898Srsb 	VOPNAME_SYMLINK,	{ .error = zfs_inval },
44743898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
44753898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
44763898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
44773898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
44783898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
44793898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
44803898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
44813898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
44823898Srsb 	NULL,			NULL
4483789Sahrens };
4484789Sahrens 
4485789Sahrens /*
4486789Sahrens  * Error vnode operations template
4487789Sahrens  */
4488789Sahrens vnodeops_t *zfs_evnodeops;
4489789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = {
44903898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
44913898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
44923898Srsb 	NULL,			NULL
4493789Sahrens };
4494