xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 5367:c40abbe796be)
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_vfsops.h>
56789Sahrens #include <sys/zfs_dir.h>
57789Sahrens #include <sys/zfs_acl.h>
58789Sahrens #include <sys/zfs_ioctl.h>
595331Samw #include <sys/zfs_i18n.h>
60789Sahrens #include <sys/fs/zfs.h>
61789Sahrens #include <sys/dmu.h>
62789Sahrens #include <sys/spa.h>
63789Sahrens #include <sys/txg.h>
64789Sahrens #include <sys/dbuf.h>
65789Sahrens #include <sys/zap.h>
66789Sahrens #include <sys/dirent.h>
67789Sahrens #include <sys/policy.h>
68789Sahrens #include <sys/sunddi.h>
69789Sahrens #include <sys/filio.h>
70789Sahrens #include "fs/fs_subr.h"
71789Sahrens #include <sys/zfs_ctldir.h>
725331Samw #include <sys/zfs_fuid.h>
731484Sek110237 #include <sys/dnlc.h>
741669Sperrin #include <sys/zfs_rlock.h>
755331Samw #include <sys/extdirent.h>
765331Samw #include <sys/kidmap.h>
775331Samw #include <sys/cred_impl.h>
78789Sahrens 
79789Sahrens /*
80789Sahrens  * Programming rules.
81789Sahrens  *
82789Sahrens  * Each vnode op performs some logical unit of work.  To do this, the ZPL must
83789Sahrens  * properly lock its in-core state, create a DMU transaction, do the work,
84789Sahrens  * record this work in the intent log (ZIL), commit the DMU transaction,
855331Samw  * and wait for the intent log to commit if it is a synchronous operation.
865331Samw  * Moreover, the vnode ops must work in both normal and log replay context.
87789Sahrens  * The ordering of events is important to avoid deadlocks and references
88789Sahrens  * to freed memory.  The example below illustrates the following Big Rules:
89789Sahrens  *
90789Sahrens  *  (1) A check must be made in each zfs thread for a mounted file system.
91*5367Sahrens  *	This is done avoiding races using ZFS_ENTER(zfsvfs).
92*5367Sahrens  *      A ZFS_EXIT(zfsvfs) is needed before all returns.  Any znodes
93*5367Sahrens  *      must be checked with ZFS_VERIFY_ZP(zp).  Both of these macros
94*5367Sahrens  *      can return EIO from the calling function.
95789Sahrens  *
96789Sahrens  *  (2)	VN_RELE() should always be the last thing except for zil_commit()
972638Sperrin  *	(if necessary) and ZFS_EXIT(). This is for 3 reasons:
98789Sahrens  *	First, if it's the last reference, the vnode/znode
99789Sahrens  *	can be freed, so the zp may point to freed memory.  Second, the last
100789Sahrens  *	reference will call zfs_zinactive(), which may induce a lot of work --
1011669Sperrin  *	pushing cached pages (which acquires range locks) and syncing out
102789Sahrens  *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
103789Sahrens  *	which could deadlock the system if you were already holding one.
104789Sahrens  *
1051757Sperrin  *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
1061757Sperrin  *	as they can span dmu_tx_assign() calls.
1071757Sperrin  *
1081757Sperrin  *  (4)	Always pass zfsvfs->z_assign as the second argument to dmu_tx_assign().
109789Sahrens  *	In normal operation, this will be TXG_NOWAIT.  During ZIL replay,
110789Sahrens  *	it will be a specific txg.  Either way, dmu_tx_assign() never blocks.
111789Sahrens  *	This is critical because we don't want to block while holding locks.
112789Sahrens  *	Note, in particular, that if a lock is sometimes acquired before
113789Sahrens  *	the tx assigns, and sometimes after (e.g. z_lock), then failing to
114789Sahrens  *	use a non-blocking assign can deadlock the system.  The scenario:
115789Sahrens  *
116789Sahrens  *	Thread A has grabbed a lock before calling dmu_tx_assign().
117789Sahrens  *	Thread B is in an already-assigned tx, and blocks for this lock.
118789Sahrens  *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
119789Sahrens  *	forever, because the previous txg can't quiesce until B's tx commits.
120789Sahrens  *
121789Sahrens  *	If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
1222113Sahrens  *	then drop all locks, call dmu_tx_wait(), and try again.
123789Sahrens  *
1241757Sperrin  *  (5)	If the operation succeeded, generate the intent log entry for it
125789Sahrens  *	before dropping locks.  This ensures that the ordering of events
126789Sahrens  *	in the intent log matches the order in which they actually occurred.
127789Sahrens  *
1281757Sperrin  *  (6)	At the end of each vnode op, the DMU tx must always commit,
129789Sahrens  *	regardless of whether there were any errors.
130789Sahrens  *
1312638Sperrin  *  (7)	After dropping all locks, invoke zil_commit(zilog, seq, foid)
132789Sahrens  *	to ensure that synchronous semantics are provided when necessary.
133789Sahrens  *
134789Sahrens  * In general, this is how things should be ordered in each vnode op:
135789Sahrens  *
136789Sahrens  *	ZFS_ENTER(zfsvfs);		// exit if unmounted
137789Sahrens  * top:
138789Sahrens  *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
139789Sahrens  *	rw_enter(...);			// grab any other locks you need
140789Sahrens  *	tx = dmu_tx_create(...);	// get DMU tx
141789Sahrens  *	dmu_tx_hold_*();		// hold each object you might modify
142789Sahrens  *	error = dmu_tx_assign(tx, zfsvfs->z_assign);	// try to assign
143789Sahrens  *	if (error) {
144789Sahrens  *		rw_exit(...);		// drop locks
145789Sahrens  *		zfs_dirent_unlock(dl);	// unlock directory entry
146789Sahrens  *		VN_RELE(...);		// release held vnodes
147789Sahrens  *		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1482113Sahrens  *			dmu_tx_wait(tx);
1492113Sahrens  *			dmu_tx_abort(tx);
150789Sahrens  *			goto top;
151789Sahrens  *		}
1522113Sahrens  *		dmu_tx_abort(tx);	// abort DMU tx
153789Sahrens  *		ZFS_EXIT(zfsvfs);	// finished in zfs
154789Sahrens  *		return (error);		// really out of space
155789Sahrens  *	}
156789Sahrens  *	error = do_real_work();		// do whatever this VOP does
157789Sahrens  *	if (error == 0)
1582638Sperrin  *		zfs_log_*(...);		// on success, make ZIL entry
159789Sahrens  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
160789Sahrens  *	rw_exit(...);			// drop locks
161789Sahrens  *	zfs_dirent_unlock(dl);		// unlock directory entry
162789Sahrens  *	VN_RELE(...);			// release held vnodes
1632638Sperrin  *	zil_commit(zilog, seq, foid);	// synchronous when necessary
164789Sahrens  *	ZFS_EXIT(zfsvfs);		// finished in zfs
165789Sahrens  *	return (error);			// done, report error
166789Sahrens  */
167*5367Sahrens 
168789Sahrens /* ARGSUSED */
169789Sahrens static int
1705331Samw zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
171789Sahrens {
1723063Sperrin 	znode_t	*zp = VTOZ(*vpp);
1733063Sperrin 
1745331Samw 	if ((flag & FWRITE) && (zp->z_phys->zp_flags & ZFS_APPENDONLY) &&
1755331Samw 	    ((flag & FAPPEND) == 0)) {
1765331Samw 		return (EPERM);
1775331Samw 	}
1785331Samw 
1795331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
1805331Samw 	    ZTOV(zp)->v_type == VREG &&
1815331Samw 	    !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
1825331Samw 	    zp->z_phys->zp_size > 0)
1835331Samw 		if (fs_vscan(*vpp, cr, 0) != 0)
1845331Samw 			return (EACCES);
1855331Samw 
1863063Sperrin 	/* Keep a count of the synchronous opens in the znode */
1873063Sperrin 	if (flag & (FSYNC | FDSYNC))
1883063Sperrin 		atomic_inc_32(&zp->z_sync_cnt);
1895331Samw 
190789Sahrens 	return (0);
191789Sahrens }
192789Sahrens 
193789Sahrens /* ARGSUSED */
194789Sahrens static int
1955331Samw zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
1965331Samw     caller_context_t *ct)
197789Sahrens {
1983063Sperrin 	znode_t	*zp = VTOZ(vp);
1993063Sperrin 
2003063Sperrin 	/* Decrement the synchronous opens in the znode */
2014339Sperrin 	if ((flag & (FSYNC | FDSYNC)) && (count == 1))
2023063Sperrin 		atomic_dec_32(&zp->z_sync_cnt);
2033063Sperrin 
204789Sahrens 	/*
205789Sahrens 	 * Clean up any locks held by this process on the vp.
206789Sahrens 	 */
207789Sahrens 	cleanlocks(vp, ddi_get_pid(), 0);
208789Sahrens 	cleanshares(vp, ddi_get_pid());
209789Sahrens 
2105331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
2115331Samw 	    ZTOV(zp)->v_type == VREG &&
2125331Samw 	    !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
2135331Samw 	    zp->z_phys->zp_size > 0)
2145331Samw 		VERIFY(fs_vscan(vp, cr, 1) == 0);
2155331Samw 
216789Sahrens 	return (0);
217789Sahrens }
218789Sahrens 
219789Sahrens /*
220789Sahrens  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
221789Sahrens  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
222789Sahrens  */
223789Sahrens static int
224789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off)
225789Sahrens {
226789Sahrens 	znode_t	*zp = VTOZ(vp);
227789Sahrens 	uint64_t noff = (uint64_t)*off; /* new offset */
228789Sahrens 	uint64_t file_sz;
229789Sahrens 	int error;
230789Sahrens 	boolean_t hole;
231789Sahrens 
232789Sahrens 	file_sz = zp->z_phys->zp_size;
233789Sahrens 	if (noff >= file_sz)  {
234789Sahrens 		return (ENXIO);
235789Sahrens 	}
236789Sahrens 
237789Sahrens 	if (cmd == _FIO_SEEK_HOLE)
238789Sahrens 		hole = B_TRUE;
239789Sahrens 	else
240789Sahrens 		hole = B_FALSE;
241789Sahrens 
242789Sahrens 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
243789Sahrens 
244789Sahrens 	/* end of file? */
245789Sahrens 	if ((error == ESRCH) || (noff > file_sz)) {
246789Sahrens 		/*
247789Sahrens 		 * Handle the virtual hole at the end of file.
248789Sahrens 		 */
249789Sahrens 		if (hole) {
250789Sahrens 			*off = file_sz;
251789Sahrens 			return (0);
252789Sahrens 		}
253789Sahrens 		return (ENXIO);
254789Sahrens 	}
255789Sahrens 
256789Sahrens 	if (noff < *off)
257789Sahrens 		return (error);
258789Sahrens 	*off = noff;
259789Sahrens 	return (error);
260789Sahrens }
261789Sahrens 
262789Sahrens /* ARGSUSED */
263789Sahrens static int
264789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
2655331Samw     int *rvalp, caller_context_t *ct)
266789Sahrens {
267789Sahrens 	offset_t off;
268789Sahrens 	int error;
269789Sahrens 	zfsvfs_t *zfsvfs;
2705326Sek110237 	znode_t *zp;
271789Sahrens 
272789Sahrens 	switch (com) {
2734339Sperrin 	case _FIOFFS:
274789Sahrens 		return (zfs_sync(vp->v_vfsp, 0, cred));
275789Sahrens 
2761544Seschrock 		/*
2771544Seschrock 		 * The following two ioctls are used by bfu.  Faking out,
2781544Seschrock 		 * necessary to avoid bfu errors.
2791544Seschrock 		 */
2804339Sperrin 	case _FIOGDIO:
2814339Sperrin 	case _FIOSDIO:
2821544Seschrock 		return (0);
2831544Seschrock 
2844339Sperrin 	case _FIO_SEEK_DATA:
2854339Sperrin 	case _FIO_SEEK_HOLE:
286789Sahrens 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
287789Sahrens 			return (EFAULT);
288789Sahrens 
2895326Sek110237 		zp = VTOZ(vp);
2905326Sek110237 		zfsvfs = zp->z_zfsvfs;
291*5367Sahrens 		ZFS_ENTER(zfsvfs);
292*5367Sahrens 		ZFS_VERIFY_ZP(zp);
293789Sahrens 
294789Sahrens 		/* offset parameter is in/out */
295789Sahrens 		error = zfs_holey(vp, com, &off);
296789Sahrens 		ZFS_EXIT(zfsvfs);
297789Sahrens 		if (error)
298789Sahrens 			return (error);
299789Sahrens 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
300789Sahrens 			return (EFAULT);
301789Sahrens 		return (0);
302789Sahrens 	}
303789Sahrens 	return (ENOTTY);
304789Sahrens }
305789Sahrens 
306789Sahrens /*
307789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
308789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
309789Sahrens  *
310789Sahrens  * On Write:	If we find a memory mapped page, we write to *both*
311789Sahrens  *		the page and the dmu buffer.
312789Sahrens  *
313789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
314789Sahrens  *	the file is memory mapped.
315789Sahrens  */
316789Sahrens static int
3173638Sbillm mappedwrite(vnode_t *vp, int nbytes, uio_t *uio, dmu_tx_t *tx)
318789Sahrens {
319789Sahrens 	znode_t	*zp = VTOZ(vp);
320789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
321789Sahrens 	int64_t	start, off;
322789Sahrens 	int len = nbytes;
323789Sahrens 	int error = 0;
324789Sahrens 
325789Sahrens 	start = uio->uio_loffset;
326789Sahrens 	off = start & PAGEOFFSET;
327789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
328789Sahrens 		page_t *pp;
329789Sahrens 		uint64_t bytes = MIN(PAGESIZE - off, len);
3303638Sbillm 		uint64_t woff = uio->uio_loffset;
331789Sahrens 
332789Sahrens 		/*
333789Sahrens 		 * We don't want a new page to "appear" in the middle of
334789Sahrens 		 * the file update (because it may not get the write
335789Sahrens 		 * update data), so we grab a lock to block
336789Sahrens 		 * zfs_getpage().
337789Sahrens 		 */
338789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
339789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
340789Sahrens 			caddr_t va;
341789Sahrens 
342789Sahrens 			rw_exit(&zp->z_map_lock);
343789Sahrens 			va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L);
344789Sahrens 			error = uiomove(va+off, bytes, UIO_WRITE, uio);
345789Sahrens 			if (error == 0) {
346789Sahrens 				dmu_write(zfsvfs->z_os, zp->z_id,
347789Sahrens 				    woff, bytes, va+off, tx);
348789Sahrens 			}
349789Sahrens 			ppmapout(va);
350789Sahrens 			page_unlock(pp);
351789Sahrens 		} else {
352789Sahrens 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
3533638Sbillm 			    uio, bytes, tx);
354789Sahrens 			rw_exit(&zp->z_map_lock);
355789Sahrens 		}
356789Sahrens 		len -= bytes;
357789Sahrens 		off = 0;
358789Sahrens 		if (error)
359789Sahrens 			break;
360789Sahrens 	}
361789Sahrens 	return (error);
362789Sahrens }
363789Sahrens 
364789Sahrens /*
365789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
366789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
367789Sahrens  *
368789Sahrens  * On Read:	We "read" preferentially from memory mapped pages,
369789Sahrens  *		else we default from the dmu buffer.
370789Sahrens  *
371789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
372789Sahrens  *	the file is memory mapped.
373789Sahrens  */
374789Sahrens static int
3753638Sbillm mappedread(vnode_t *vp, int nbytes, uio_t *uio)
376789Sahrens {
3773638Sbillm 	znode_t *zp = VTOZ(vp);
3783638Sbillm 	objset_t *os = zp->z_zfsvfs->z_os;
3793638Sbillm 	int64_t	start, off;
380789Sahrens 	int len = nbytes;
381789Sahrens 	int error = 0;
382789Sahrens 
383789Sahrens 	start = uio->uio_loffset;
384789Sahrens 	off = start & PAGEOFFSET;
385789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
386789Sahrens 		page_t *pp;
3873638Sbillm 		uint64_t bytes = MIN(PAGESIZE - off, len);
3883638Sbillm 
389789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
390789Sahrens 			caddr_t va;
391789Sahrens 
3922688Smaybee 			va = ppmapin(pp, PROT_READ, (caddr_t)-1L);
393789Sahrens 			error = uiomove(va + off, bytes, UIO_READ, uio);
394789Sahrens 			ppmapout(va);
395789Sahrens 			page_unlock(pp);
396789Sahrens 		} else {
3973638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, bytes);
398789Sahrens 		}
399789Sahrens 		len -= bytes;
400789Sahrens 		off = 0;
401789Sahrens 		if (error)
402789Sahrens 			break;
403789Sahrens 	}
404789Sahrens 	return (error);
405789Sahrens }
406789Sahrens 
4073638Sbillm offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
408789Sahrens 
409789Sahrens /*
410789Sahrens  * Read bytes from specified file into supplied buffer.
411789Sahrens  *
412789Sahrens  *	IN:	vp	- vnode of file to be read from.
413789Sahrens  *		uio	- structure supplying read location, range info,
414789Sahrens  *			  and return buffer.
415789Sahrens  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
416789Sahrens  *		cr	- credentials of caller.
4175331Samw  *		ct	- caller context
418789Sahrens  *
419789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
420789Sahrens  *
421789Sahrens  *	RETURN:	0 if success
422789Sahrens  *		error code if failure
423789Sahrens  *
424789Sahrens  * Side Effects:
425789Sahrens  *	vp - atime updated if byte count > 0
426789Sahrens  */
427789Sahrens /* ARGSUSED */
428789Sahrens static int
429789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
430789Sahrens {
431789Sahrens 	znode_t		*zp = VTOZ(vp);
432789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4335326Sek110237 	objset_t	*os;
4343638Sbillm 	ssize_t		n, nbytes;
4353638Sbillm 	int		error;
4361669Sperrin 	rl_t		*rl;
437789Sahrens 
438*5367Sahrens 	ZFS_ENTER(zfsvfs);
439*5367Sahrens 	ZFS_VERIFY_ZP(zp);
4405326Sek110237 	os = zfsvfs->z_os;
441789Sahrens 
442789Sahrens 	/*
443789Sahrens 	 * Validate file offset
444789Sahrens 	 */
445789Sahrens 	if (uio->uio_loffset < (offset_t)0) {
446789Sahrens 		ZFS_EXIT(zfsvfs);
447789Sahrens 		return (EINVAL);
448789Sahrens 	}
449789Sahrens 
450789Sahrens 	/*
451789Sahrens 	 * Fasttrack empty reads
452789Sahrens 	 */
453789Sahrens 	if (uio->uio_resid == 0) {
454789Sahrens 		ZFS_EXIT(zfsvfs);
455789Sahrens 		return (0);
456789Sahrens 	}
457789Sahrens 
458789Sahrens 	/*
4591669Sperrin 	 * Check for mandatory locks
460789Sahrens 	 */
461789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
462789Sahrens 		if (error = chklock(vp, FREAD,
463789Sahrens 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
464789Sahrens 			ZFS_EXIT(zfsvfs);
465789Sahrens 			return (error);
466789Sahrens 		}
467789Sahrens 	}
468789Sahrens 
469789Sahrens 	/*
470789Sahrens 	 * If we're in FRSYNC mode, sync out this znode before reading it.
471789Sahrens 	 */
4722638Sperrin 	if (ioflag & FRSYNC)
4732638Sperrin 		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
474789Sahrens 
475789Sahrens 	/*
4761669Sperrin 	 * Lock the range against changes.
477789Sahrens 	 */
4781669Sperrin 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
4791669Sperrin 
480789Sahrens 	/*
481789Sahrens 	 * If we are reading past end-of-file we can skip
482789Sahrens 	 * to the end; but we might still need to set atime.
483789Sahrens 	 */
484789Sahrens 	if (uio->uio_loffset >= zp->z_phys->zp_size) {
485789Sahrens 		error = 0;
486789Sahrens 		goto out;
487789Sahrens 	}
488789Sahrens 
4893638Sbillm 	ASSERT(uio->uio_loffset < zp->z_phys->zp_size);
4903638Sbillm 	n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset);
4913638Sbillm 
4923638Sbillm 	while (n > 0) {
4933638Sbillm 		nbytes = MIN(n, zfs_read_chunk_size -
4943638Sbillm 		    P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
4953638Sbillm 
4963638Sbillm 		if (vn_has_cached_data(vp))
4973638Sbillm 			error = mappedread(vp, nbytes, uio);
4983638Sbillm 		else
4993638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, nbytes);
5001544Seschrock 		if (error)
5013638Sbillm 			break;
5023638Sbillm 
5033638Sbillm 		n -= nbytes;
504789Sahrens 	}
5053638Sbillm 
506789Sahrens out:
5072237Smaybee 	zfs_range_unlock(rl);
508789Sahrens 
509789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
510789Sahrens 	ZFS_EXIT(zfsvfs);
511789Sahrens 	return (error);
512789Sahrens }
513789Sahrens 
514789Sahrens /*
515789Sahrens  * Fault in the pages of the first n bytes specified by the uio structure.
516789Sahrens  * 1 byte in each page is touched and the uio struct is unmodified.
517789Sahrens  * Any error will exit this routine as this is only a best
518789Sahrens  * attempt to get the pages resident. This is a copy of ufs_trans_touch().
519789Sahrens  */
520789Sahrens static void
521789Sahrens zfs_prefault_write(ssize_t n, struct uio *uio)
522789Sahrens {
523789Sahrens 	struct iovec *iov;
524789Sahrens 	ulong_t cnt, incr;
525789Sahrens 	caddr_t p;
526789Sahrens 	uint8_t tmp;
527789Sahrens 
528789Sahrens 	iov = uio->uio_iov;
529789Sahrens 
530789Sahrens 	while (n) {
531789Sahrens 		cnt = MIN(iov->iov_len, n);
532789Sahrens 		if (cnt == 0) {
533789Sahrens 			/* empty iov entry */
534789Sahrens 			iov++;
535789Sahrens 			continue;
536789Sahrens 		}
537789Sahrens 		n -= cnt;
538789Sahrens 		/*
539789Sahrens 		 * touch each page in this segment.
540789Sahrens 		 */
541789Sahrens 		p = iov->iov_base;
542789Sahrens 		while (cnt) {
543789Sahrens 			switch (uio->uio_segflg) {
544789Sahrens 			case UIO_USERSPACE:
545789Sahrens 			case UIO_USERISPACE:
546789Sahrens 				if (fuword8(p, &tmp))
547789Sahrens 					return;
548789Sahrens 				break;
549789Sahrens 			case UIO_SYSSPACE:
550789Sahrens 				if (kcopy(p, &tmp, 1))
551789Sahrens 					return;
552789Sahrens 				break;
553789Sahrens 			}
554789Sahrens 			incr = MIN(cnt, PAGESIZE);
555789Sahrens 			p += incr;
556789Sahrens 			cnt -= incr;
557789Sahrens 		}
558789Sahrens 		/*
559789Sahrens 		 * touch the last byte in case it straddles a page.
560789Sahrens 		 */
561789Sahrens 		p--;
562789Sahrens 		switch (uio->uio_segflg) {
563789Sahrens 		case UIO_USERSPACE:
564789Sahrens 		case UIO_USERISPACE:
565789Sahrens 			if (fuword8(p, &tmp))
566789Sahrens 				return;
567789Sahrens 			break;
568789Sahrens 		case UIO_SYSSPACE:
569789Sahrens 			if (kcopy(p, &tmp, 1))
570789Sahrens 				return;
571789Sahrens 			break;
572789Sahrens 		}
573789Sahrens 		iov++;
574789Sahrens 	}
575789Sahrens }
576789Sahrens 
577789Sahrens /*
578789Sahrens  * Write the bytes to a file.
579789Sahrens  *
580789Sahrens  *	IN:	vp	- vnode of file to be written to.
581789Sahrens  *		uio	- structure supplying write location, range info,
582789Sahrens  *			  and data buffer.
583789Sahrens  *		ioflag	- FAPPEND flag set if in append mode.
584789Sahrens  *		cr	- credentials of caller.
5855331Samw  *		ct	- caller context (NFS/CIFS fem monitor only)
586789Sahrens  *
587789Sahrens  *	OUT:	uio	- updated offset and range.
588789Sahrens  *
589789Sahrens  *	RETURN:	0 if success
590789Sahrens  *		error code if failure
591789Sahrens  *
592789Sahrens  * Timestamps:
593789Sahrens  *	vp - ctime|mtime updated if byte count > 0
594789Sahrens  */
595789Sahrens /* ARGSUSED */
596789Sahrens static int
597789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
598789Sahrens {
599789Sahrens 	znode_t		*zp = VTOZ(vp);
600789Sahrens 	rlim64_t	limit = uio->uio_llimit;
601789Sahrens 	ssize_t		start_resid = uio->uio_resid;
602789Sahrens 	ssize_t		tx_bytes;
603789Sahrens 	uint64_t	end_size;
604789Sahrens 	dmu_tx_t	*tx;
605789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
6065326Sek110237 	zilog_t		*zilog;
607789Sahrens 	offset_t	woff;
608789Sahrens 	ssize_t		n, nbytes;
6091669Sperrin 	rl_t		*rl;
610789Sahrens 	int		max_blksz = zfsvfs->z_max_blksz;
6115331Samw 	uint64_t	pflags = zp->z_phys->zp_flags;
6121669Sperrin 	int		error;
613789Sahrens 
614789Sahrens 	/*
6155331Samw 	 * If immutable or not appending then return EPERM
6165331Samw 	 */
6175331Samw 	if ((pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
6185331Samw 	    ((pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
6195331Samw 	    (uio->uio_loffset < zp->z_phys->zp_size)))
6205331Samw 		return (EPERM);
6215331Samw 
6225331Samw 	/*
623789Sahrens 	 * Fasttrack empty write
624789Sahrens 	 */
6251669Sperrin 	n = start_resid;
626789Sahrens 	if (n == 0)
627789Sahrens 		return (0);
628789Sahrens 
6291669Sperrin 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
6301669Sperrin 		limit = MAXOFFSET_T;
6311669Sperrin 
632*5367Sahrens 	ZFS_ENTER(zfsvfs);
633*5367Sahrens 	ZFS_VERIFY_ZP(zp);
6345326Sek110237 	zilog = zfsvfs->z_log;
635789Sahrens 
636789Sahrens 	/*
6372237Smaybee 	 * Pre-fault the pages to ensure slow (eg NFS) pages
6381669Sperrin 	 * don't hold up txg.
639789Sahrens 	 */
6402237Smaybee 	zfs_prefault_write(n, uio);
641789Sahrens 
642789Sahrens 	/*
643789Sahrens 	 * If in append mode, set the io offset pointer to eof.
644789Sahrens 	 */
6451669Sperrin 	if (ioflag & FAPPEND) {
6461669Sperrin 		/*
6471669Sperrin 		 * Range lock for a file append:
6481669Sperrin 		 * The value for the start of range will be determined by
6491669Sperrin 		 * zfs_range_lock() (to guarantee append semantics).
6501669Sperrin 		 * If this write will cause the block size to increase,
6511669Sperrin 		 * zfs_range_lock() will lock the entire file, so we must
6521669Sperrin 		 * later reduce the range after we grow the block size.
6531669Sperrin 		 */
6541669Sperrin 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
6551669Sperrin 		if (rl->r_len == UINT64_MAX) {
6561669Sperrin 			/* overlocked, zp_size can't change */
6571669Sperrin 			woff = uio->uio_loffset = zp->z_phys->zp_size;
6581669Sperrin 		} else {
6591669Sperrin 			woff = uio->uio_loffset = rl->r_off;
6601669Sperrin 		}
661789Sahrens 	} else {
662789Sahrens 		woff = uio->uio_loffset;
663789Sahrens 		/*
664789Sahrens 		 * Validate file offset
665789Sahrens 		 */
666789Sahrens 		if (woff < 0) {
667789Sahrens 			ZFS_EXIT(zfsvfs);
668789Sahrens 			return (EINVAL);
669789Sahrens 		}
670789Sahrens 
671789Sahrens 		/*
6721669Sperrin 		 * If we need to grow the block size then zfs_range_lock()
6731669Sperrin 		 * will lock a wider range than we request here.
6741669Sperrin 		 * Later after growing the block size we reduce the range.
675789Sahrens 		 */
6761669Sperrin 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
677789Sahrens 	}
678789Sahrens 
679789Sahrens 	if (woff >= limit) {
6803638Sbillm 		zfs_range_unlock(rl);
6813638Sbillm 		ZFS_EXIT(zfsvfs);
6823638Sbillm 		return (EFBIG);
683789Sahrens 	}
684789Sahrens 
685789Sahrens 	if ((woff + n) > limit || woff > (limit - n))
686789Sahrens 		n = limit - woff;
687789Sahrens 
688789Sahrens 	/*
6891669Sperrin 	 * Check for mandatory locks
690789Sahrens 	 */
691789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) &&
6923638Sbillm 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
6933638Sbillm 		zfs_range_unlock(rl);
6943638Sbillm 		ZFS_EXIT(zfsvfs);
6953638Sbillm 		return (error);
6963638Sbillm 	}
6971669Sperrin 	end_size = MAX(zp->z_phys->zp_size, woff + n);
698789Sahrens 
6991669Sperrin 	/*
7003638Sbillm 	 * Write the file in reasonable size chunks.  Each chunk is written
7013638Sbillm 	 * in a separate transaction; this keeps the intent log records small
7023638Sbillm 	 * and allows us to do more fine-grained space accounting.
703789Sahrens 	 */
704789Sahrens 	while (n > 0) {
705789Sahrens 		/*
7063638Sbillm 		 * Start a transaction.
707789Sahrens 		 */
708789Sahrens 		woff = uio->uio_loffset;
709789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
710789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
711789Sahrens 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
712789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
713789Sahrens 		if (error) {
714789Sahrens 			if (error == ERESTART &&
715789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
7162113Sahrens 				dmu_tx_wait(tx);
7172113Sahrens 				dmu_tx_abort(tx);
7183638Sbillm 				continue;
719789Sahrens 			}
7202113Sahrens 			dmu_tx_abort(tx);
7213638Sbillm 			break;
7223638Sbillm 		}
7233638Sbillm 
7243638Sbillm 		/*
7253638Sbillm 		 * If zfs_range_lock() over-locked we grow the blocksize
7263638Sbillm 		 * and then reduce the lock range.  This will only happen
7273638Sbillm 		 * on the first iteration since zfs_range_reduce() will
7283638Sbillm 		 * shrink down r_len to the appropriate size.
7293638Sbillm 		 */
7303638Sbillm 		if (rl->r_len == UINT64_MAX) {
7313638Sbillm 			uint64_t new_blksz;
7323638Sbillm 
7333638Sbillm 			if (zp->z_blksz > max_blksz) {
7343638Sbillm 				ASSERT(!ISP2(zp->z_blksz));
7353638Sbillm 				new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
7363638Sbillm 			} else {
7373638Sbillm 				new_blksz = MIN(end_size, max_blksz);
7383638Sbillm 			}
7393638Sbillm 			zfs_grow_blocksize(zp, new_blksz, tx);
7403638Sbillm 			zfs_range_reduce(rl, woff, n);
7413638Sbillm 		}
7423638Sbillm 
7433638Sbillm 		/*
7443638Sbillm 		 * XXX - should we really limit each write to z_max_blksz?
7453638Sbillm 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
7463638Sbillm 		 */
7473638Sbillm 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
7483638Sbillm 		rw_enter(&zp->z_map_lock, RW_READER);
7493638Sbillm 
7503638Sbillm 		tx_bytes = uio->uio_resid;
7513638Sbillm 		if (vn_has_cached_data(vp)) {
7523638Sbillm 			rw_exit(&zp->z_map_lock);
7533638Sbillm 			error = mappedwrite(vp, nbytes, uio, tx);
7543638Sbillm 		} else {
7553638Sbillm 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
7563638Sbillm 			    uio, nbytes, tx);
7573638Sbillm 			rw_exit(&zp->z_map_lock);
758789Sahrens 		}
7593638Sbillm 		tx_bytes -= uio->uio_resid;
7603638Sbillm 
7613638Sbillm 		/*
7623638Sbillm 		 * If we made no progress, we're done.  If we made even
7633638Sbillm 		 * partial progress, update the znode and ZIL accordingly.
7643638Sbillm 		 */
7653638Sbillm 		if (tx_bytes == 0) {
7663897Smaybee 			dmu_tx_commit(tx);
7673638Sbillm 			ASSERT(error != 0);
7683638Sbillm 			break;
7693638Sbillm 		}
7703638Sbillm 
771789Sahrens 		/*
7723638Sbillm 		 * Clear Set-UID/Set-GID bits on successful write if not
7733638Sbillm 		 * privileged and at least one of the excute bits is set.
7743638Sbillm 		 *
7753638Sbillm 		 * It would be nice to to this after all writes have
7763638Sbillm 		 * been done, but that would still expose the ISUID/ISGID
7773638Sbillm 		 * to another app after the partial write is committed.
7785331Samw 		 *
7795331Samw 		 * Note: we don't call zfs_fuid_map_id() here because
7805331Samw 		 * user 0 is not an ephemeral uid.
781789Sahrens 		 */
7823638Sbillm 		mutex_enter(&zp->z_acl_lock);
7833638Sbillm 		if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) |
7843638Sbillm 		    (S_IXUSR >> 6))) != 0 &&
7853638Sbillm 		    (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 &&
7863638Sbillm 		    secpolicy_vnode_setid_retain(cr,
7873638Sbillm 		    (zp->z_phys->zp_mode & S_ISUID) != 0 &&
7883638Sbillm 		    zp->z_phys->zp_uid == 0) != 0) {
7894339Sperrin 			zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID);
7903638Sbillm 		}
7913638Sbillm 		mutex_exit(&zp->z_acl_lock);
7923638Sbillm 
7933638Sbillm 		/*
7943638Sbillm 		 * Update time stamp.  NOTE: This marks the bonus buffer as
7953638Sbillm 		 * dirty, so we don't have to do it again for zp_size.
7963638Sbillm 		 */
7973638Sbillm 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
7983638Sbillm 
7993638Sbillm 		/*
8003638Sbillm 		 * Update the file size (zp_size) if it has changed;
8013638Sbillm 		 * account for possible concurrent updates.
8023638Sbillm 		 */
8033638Sbillm 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset)
804789Sahrens 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
805789Sahrens 			    uio->uio_loffset);
8063638Sbillm 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
8073638Sbillm 		dmu_tx_commit(tx);
8083638Sbillm 
8093638Sbillm 		if (error != 0)
8103638Sbillm 			break;
8113638Sbillm 		ASSERT(tx_bytes == nbytes);
8123638Sbillm 		n -= nbytes;
813789Sahrens 	}
814789Sahrens 
8152237Smaybee 	zfs_range_unlock(rl);
816789Sahrens 
817789Sahrens 	/*
818789Sahrens 	 * If we're in replay mode, or we made no progress, return error.
819789Sahrens 	 * Otherwise, it's at least a partial write, so it's successful.
820789Sahrens 	 */
821789Sahrens 	if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) {
822789Sahrens 		ZFS_EXIT(zfsvfs);
823789Sahrens 		return (error);
824789Sahrens 	}
825789Sahrens 
8262638Sperrin 	if (ioflag & (FSYNC | FDSYNC))
8272638Sperrin 		zil_commit(zilog, zp->z_last_itx, zp->z_id);
828789Sahrens 
829789Sahrens 	ZFS_EXIT(zfsvfs);
830789Sahrens 	return (0);
831789Sahrens }
832789Sahrens 
8332237Smaybee void
8343063Sperrin zfs_get_done(dmu_buf_t *db, void *vzgd)
8352237Smaybee {
8363063Sperrin 	zgd_t *zgd = (zgd_t *)vzgd;
8373063Sperrin 	rl_t *rl = zgd->zgd_rl;
8382237Smaybee 	vnode_t *vp = ZTOV(rl->r_zp);
8392237Smaybee 
8403063Sperrin 	dmu_buf_rele(db, vzgd);
8412237Smaybee 	zfs_range_unlock(rl);
8422237Smaybee 	VN_RELE(vp);
8433063Sperrin 	zil_add_vdev(zgd->zgd_zilog, DVA_GET_VDEV(BP_IDENTITY(zgd->zgd_bp)));
8443063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
8452237Smaybee }
8462237Smaybee 
847789Sahrens /*
848789Sahrens  * Get data to generate a TX_WRITE intent log record.
849789Sahrens  */
850789Sahrens int
8512237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
852789Sahrens {
853789Sahrens 	zfsvfs_t *zfsvfs = arg;
854789Sahrens 	objset_t *os = zfsvfs->z_os;
855789Sahrens 	znode_t *zp;
856789Sahrens 	uint64_t off = lr->lr_offset;
8572237Smaybee 	dmu_buf_t *db;
8581669Sperrin 	rl_t *rl;
8593063Sperrin 	zgd_t *zgd;
8603638Sbillm 	int dlen = lr->lr_length;		/* length of user data */
861789Sahrens 	int error = 0;
862789Sahrens 
8633063Sperrin 	ASSERT(zio);
864789Sahrens 	ASSERT(dlen != 0);
865789Sahrens 
866789Sahrens 	/*
8671669Sperrin 	 * Nothing to do if the file has been removed
868789Sahrens 	 */
869789Sahrens 	if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0)
870789Sahrens 		return (ENOENT);
8713461Sahrens 	if (zp->z_unlinked) {
872789Sahrens 		VN_RELE(ZTOV(zp));
873789Sahrens 		return (ENOENT);
874789Sahrens 	}
875789Sahrens 
876789Sahrens 	/*
877789Sahrens 	 * Write records come in two flavors: immediate and indirect.
878789Sahrens 	 * For small writes it's cheaper to store the data with the
879789Sahrens 	 * log record (immediate); for large writes it's cheaper to
880789Sahrens 	 * sync the data and get a pointer to it (indirect) so that
881789Sahrens 	 * we don't have to write the data twice.
882789Sahrens 	 */
8831669Sperrin 	if (buf != NULL) { /* immediate write */
8841669Sperrin 		rl = zfs_range_lock(zp, off, dlen, RL_READER);
8851669Sperrin 		/* test for truncation needs to be done while range locked */
8861669Sperrin 		if (off >= zp->z_phys->zp_size) {
8871669Sperrin 			error = ENOENT;
8881669Sperrin 			goto out;
8891669Sperrin 		}
8902449Smaybee 		VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf));
8911669Sperrin 	} else { /* indirect write */
8921669Sperrin 		uint64_t boff; /* block starting offset */
8931669Sperrin 
894789Sahrens 		/*
8951669Sperrin 		 * Have to lock the whole block to ensure when it's
8961669Sperrin 		 * written out and it's checksum is being calculated
8971669Sperrin 		 * that no one can change the data. We need to re-check
8981669Sperrin 		 * blocksize after we get the lock in case it's changed!
899789Sahrens 		 */
9001669Sperrin 		for (;;) {
9011941Sperrin 			if (ISP2(zp->z_blksz)) {
9021941Sperrin 				boff = P2ALIGN_TYPED(off, zp->z_blksz,
9031941Sperrin 				    uint64_t);
9041941Sperrin 			} else {
9051941Sperrin 				boff = 0;
9061941Sperrin 			}
9071669Sperrin 			dlen = zp->z_blksz;
9081669Sperrin 			rl = zfs_range_lock(zp, boff, dlen, RL_READER);
9091669Sperrin 			if (zp->z_blksz == dlen)
9101669Sperrin 				break;
9112237Smaybee 			zfs_range_unlock(rl);
9121669Sperrin 		}
9131669Sperrin 		/* test for truncation needs to be done while range locked */
9141669Sperrin 		if (off >= zp->z_phys->zp_size) {
9151669Sperrin 			error = ENOENT;
9161669Sperrin 			goto out;
9171669Sperrin 		}
9183063Sperrin 		zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
9193063Sperrin 		zgd->zgd_rl = rl;
9203063Sperrin 		zgd->zgd_zilog = zfsvfs->z_log;
9213063Sperrin 		zgd->zgd_bp = &lr->lr_blkptr;
9223063Sperrin 		VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db));
9232237Smaybee 		ASSERT(boff == db->db_offset);
9242237Smaybee 		lr->lr_blkoff = off - boff;
9252237Smaybee 		error = dmu_sync(zio, db, &lr->lr_blkptr,
9263063Sperrin 		    lr->lr_common.lrc_txg, zfs_get_done, zgd);
9274709Smaybee 		ASSERT((error && error != EINPROGRESS) ||
9284709Smaybee 		    lr->lr_length <= zp->z_blksz);
9293063Sperrin 		if (error == 0) {
9303063Sperrin 			zil_add_vdev(zfsvfs->z_log,
9313063Sperrin 			    DVA_GET_VDEV(BP_IDENTITY(&lr->lr_blkptr)));
9323063Sperrin 		}
9332237Smaybee 		/*
9342237Smaybee 		 * If we get EINPROGRESS, then we need to wait for a
9352237Smaybee 		 * write IO initiated by dmu_sync() to complete before
9362638Sperrin 		 * we can release this dbuf.  We will finish everything
9372237Smaybee 		 * up in the zfs_get_done() callback.
9382237Smaybee 		 */
9392237Smaybee 		if (error == EINPROGRESS)
9402237Smaybee 			return (0);
9413063Sperrin 		dmu_buf_rele(db, zgd);
9423063Sperrin 		kmem_free(zgd, sizeof (zgd_t));
943789Sahrens 	}
9441669Sperrin out:
9452237Smaybee 	zfs_range_unlock(rl);
946789Sahrens 	VN_RELE(ZTOV(zp));
947789Sahrens 	return (error);
948789Sahrens }
949789Sahrens 
950789Sahrens /*ARGSUSED*/
951789Sahrens static int
9525331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
9535331Samw     caller_context_t *ct)
954789Sahrens {
955789Sahrens 	znode_t *zp = VTOZ(vp);
956789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
957789Sahrens 	int error;
958789Sahrens 
959*5367Sahrens 	ZFS_ENTER(zfsvfs);
960*5367Sahrens 	ZFS_VERIFY_ZP(zp);
9615331Samw 
9625331Samw 	if (flag & V_ACE_MASK)
9635331Samw 		error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
9645331Samw 	else
9655331Samw 		error = zfs_zaccess_rwx(zp, mode, flag, cr);
9665331Samw 
967789Sahrens 	ZFS_EXIT(zfsvfs);
968789Sahrens 	return (error);
969789Sahrens }
970789Sahrens 
971789Sahrens /*
972789Sahrens  * Lookup an entry in a directory, or an extended attribute directory.
973789Sahrens  * If it exists, return a held vnode reference for it.
974789Sahrens  *
975789Sahrens  *	IN:	dvp	- vnode of directory to search.
976789Sahrens  *		nm	- name of entry to lookup.
977789Sahrens  *		pnp	- full pathname to lookup [UNUSED].
978789Sahrens  *		flags	- LOOKUP_XATTR set if looking for an attribute.
979789Sahrens  *		rdir	- root directory vnode [UNUSED].
980789Sahrens  *		cr	- credentials of caller.
9815331Samw  *		ct	- caller context
9825331Samw  *		direntflags - directory lookup flags
9835331Samw  *		realpnp - returned pathname.
984789Sahrens  *
985789Sahrens  *	OUT:	vpp	- vnode of located entry, NULL if not found.
986789Sahrens  *
987789Sahrens  *	RETURN:	0 if success
988789Sahrens  *		error code if failure
989789Sahrens  *
990789Sahrens  * Timestamps:
991789Sahrens  *	NA
992789Sahrens  */
993789Sahrens /* ARGSUSED */
994789Sahrens static int
995789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
9965331Samw     int flags, vnode_t *rdir, cred_t *cr,  caller_context_t *ct,
9975331Samw     int *direntflags, pathname_t *realpnp)
998789Sahrens {
999789Sahrens 	znode_t *zdp = VTOZ(dvp);
1000789Sahrens 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1001789Sahrens 	int	error;
1002789Sahrens 
1003*5367Sahrens 	ZFS_ENTER(zfsvfs);
1004*5367Sahrens 	ZFS_VERIFY_ZP(zdp);
1005789Sahrens 
1006789Sahrens 	*vpp = NULL;
1007789Sahrens 
1008789Sahrens 	if (flags & LOOKUP_XATTR) {
1009789Sahrens 		/*
10103234Sck153898 		 * If the xattr property is off, refuse the lookup request.
10113234Sck153898 		 */
10123234Sck153898 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
10133234Sck153898 			ZFS_EXIT(zfsvfs);
10143234Sck153898 			return (EINVAL);
10153234Sck153898 		}
10163234Sck153898 
10173234Sck153898 		/*
1018789Sahrens 		 * We don't allow recursive attributes..
1019789Sahrens 		 * Maybe someday we will.
1020789Sahrens 		 */
1021789Sahrens 		if (zdp->z_phys->zp_flags & ZFS_XATTR) {
1022789Sahrens 			ZFS_EXIT(zfsvfs);
1023789Sahrens 			return (EINVAL);
1024789Sahrens 		}
1025789Sahrens 
10263280Sck153898 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1027789Sahrens 			ZFS_EXIT(zfsvfs);
1028789Sahrens 			return (error);
1029789Sahrens 		}
1030789Sahrens 
1031789Sahrens 		/*
1032789Sahrens 		 * Do we have permission to get into attribute directory?
1033789Sahrens 		 */
1034789Sahrens 
10355331Samw 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
10365331Samw 		    B_FALSE, cr)) {
1037789Sahrens 			VN_RELE(*vpp);
10385331Samw 			*vpp = NULL;
1039789Sahrens 		}
1040789Sahrens 
1041789Sahrens 		ZFS_EXIT(zfsvfs);
1042789Sahrens 		return (error);
1043789Sahrens 	}
1044789Sahrens 
10451512Sek110237 	if (dvp->v_type != VDIR) {
10461512Sek110237 		ZFS_EXIT(zfsvfs);
10471460Smarks 		return (ENOTDIR);
10481512Sek110237 	}
10491460Smarks 
1050789Sahrens 	/*
1051789Sahrens 	 * Check accessibility of directory.
1052789Sahrens 	 */
1053789Sahrens 
10545331Samw 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1055789Sahrens 		ZFS_EXIT(zfsvfs);
1056789Sahrens 		return (error);
1057789Sahrens 	}
1058789Sahrens 
10595331Samw 	if (zfsvfs->z_case & ZFS_UTF8_ONLY && u8_validate(nm, strlen(nm),
10605331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
10615331Samw 		ZFS_EXIT(zfsvfs);
10625331Samw 		return (EILSEQ);
10635331Samw 	}
10645331Samw 
10655331Samw 	error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
10665331Samw 	if (error == 0) {
1067789Sahrens 		/*
1068789Sahrens 		 * Convert device special files
1069789Sahrens 		 */
1070789Sahrens 		if (IS_DEVVP(*vpp)) {
1071789Sahrens 			vnode_t	*svp;
1072789Sahrens 
1073789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1074789Sahrens 			VN_RELE(*vpp);
1075789Sahrens 			if (svp == NULL)
1076789Sahrens 				error = ENOSYS;
1077789Sahrens 			else
1078789Sahrens 				*vpp = svp;
1079789Sahrens 		}
1080789Sahrens 	}
1081789Sahrens 
1082789Sahrens 	ZFS_EXIT(zfsvfs);
1083789Sahrens 	return (error);
1084789Sahrens }
1085789Sahrens 
1086789Sahrens /*
1087789Sahrens  * Attempt to create a new entry in a directory.  If the entry
1088789Sahrens  * already exists, truncate the file if permissible, else return
1089789Sahrens  * an error.  Return the vp of the created or trunc'd file.
1090789Sahrens  *
1091789Sahrens  *	IN:	dvp	- vnode of directory to put new file entry in.
1092789Sahrens  *		name	- name of new file entry.
1093789Sahrens  *		vap	- attributes of new file.
1094789Sahrens  *		excl	- flag indicating exclusive or non-exclusive mode.
1095789Sahrens  *		mode	- mode to open file with.
1096789Sahrens  *		cr	- credentials of caller.
1097789Sahrens  *		flag	- large file flag [UNUSED].
10985331Samw  *		ct	- caller context
10995331Samw  *		vsecp 	- ACL to be set
1100789Sahrens  *
1101789Sahrens  *	OUT:	vpp	- vnode of created or trunc'd entry.
1102789Sahrens  *
1103789Sahrens  *	RETURN:	0 if success
1104789Sahrens  *		error code if failure
1105789Sahrens  *
1106789Sahrens  * Timestamps:
1107789Sahrens  *	dvp - ctime|mtime updated if new entry created
1108789Sahrens  *	 vp - ctime|mtime always, atime if new
1109789Sahrens  */
11105331Samw 
1111789Sahrens /* ARGSUSED */
1112789Sahrens static int
1113789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
11145331Samw     int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
11155331Samw     vsecattr_t *vsecp)
1116789Sahrens {
1117789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1118789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
11195326Sek110237 	zilog_t		*zilog;
11205326Sek110237 	objset_t	*os;
1121789Sahrens 	zfs_dirlock_t	*dl;
1122789Sahrens 	dmu_tx_t	*tx;
1123789Sahrens 	int		error;
1124789Sahrens 	uint64_t	zoid;
11255331Samw 	zfs_acl_t	*aclp = NULL;
11265331Samw 	zfs_fuid_info_t *fuidp = NULL;
11275331Samw 
11285331Samw 	/*
11295331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
11305331Samw 	 * make sure file system is at proper version
11315331Samw 	 */
11325331Samw 
11335331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
11345331Samw 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
11355331Samw 	    IS_EPHEMERAL(crgetuid(cr)) || IS_EPHEMERAL(crgetgid(cr))))
11365331Samw 		return (EINVAL);
1137789Sahrens 
1138*5367Sahrens 	ZFS_ENTER(zfsvfs);
1139*5367Sahrens 	ZFS_VERIFY_ZP(dzp);
11405326Sek110237 	os = zfsvfs->z_os;
11415326Sek110237 	zilog = zfsvfs->z_log;
1142789Sahrens 
11435331Samw 	if (zfsvfs->z_case & ZFS_UTF8_ONLY && u8_validate(name, strlen(name),
11445331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
11455331Samw 		ZFS_EXIT(zfsvfs);
11465331Samw 		return (EILSEQ);
11475331Samw 	}
11485331Samw 
11495331Samw 	if (vap->va_mask & AT_XVATTR) {
11505331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
11515331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
11525331Samw 			ZFS_EXIT(zfsvfs);
11535331Samw 			return (error);
11545331Samw 		}
11555331Samw 	}
1156789Sahrens top:
1157789Sahrens 	*vpp = NULL;
1158789Sahrens 
1159789Sahrens 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1160789Sahrens 		vap->va_mode &= ~VSVTX;
1161789Sahrens 
1162789Sahrens 	if (*name == '\0') {
1163789Sahrens 		/*
1164789Sahrens 		 * Null component name refers to the directory itself.
1165789Sahrens 		 */
1166789Sahrens 		VN_HOLD(dvp);
1167789Sahrens 		zp = dzp;
1168789Sahrens 		dl = NULL;
1169789Sahrens 		error = 0;
1170789Sahrens 	} else {
1171789Sahrens 		/* possible VN_HOLD(zp) */
11725331Samw 		int zflg = 0;
11735331Samw 
11745331Samw 		if (flag & FIGNORECASE)
11755331Samw 			zflg |= ZCILOOK;
11765331Samw 
11775331Samw 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
11785331Samw 		    NULL, NULL);
11795331Samw 		if (error) {
1180789Sahrens 			if (strcmp(name, "..") == 0)
1181789Sahrens 				error = EISDIR;
1182789Sahrens 			ZFS_EXIT(zfsvfs);
11835331Samw 			if (aclp)
11845331Samw 				zfs_acl_free(aclp);
1185789Sahrens 			return (error);
1186789Sahrens 		}
1187789Sahrens 	}
11885331Samw 	if (vsecp && aclp == NULL) {
11895331Samw 		error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp);
11905331Samw 		if (error) {
11915331Samw 			ZFS_EXIT(zfsvfs);
11925331Samw 			if (dl)
11935331Samw 				zfs_dirent_unlock(dl);
11945331Samw 			return (error);
11955331Samw 		}
11965331Samw 	}
1197789Sahrens 	zoid = zp ? zp->z_id : -1ULL;
1198789Sahrens 
1199789Sahrens 	if (zp == NULL) {
12005331Samw 		uint64_t txtype;
12015331Samw 
1202789Sahrens 		/*
1203789Sahrens 		 * Create a new file object and update the directory
1204789Sahrens 		 * to reference it.
1205789Sahrens 		 */
12065331Samw 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1207789Sahrens 			goto out;
1208789Sahrens 		}
1209789Sahrens 
1210789Sahrens 		/*
1211789Sahrens 		 * We only support the creation of regular files in
1212789Sahrens 		 * extended attribute directories.
1213789Sahrens 		 */
1214789Sahrens 		if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1215789Sahrens 		    (vap->va_type != VREG)) {
1216789Sahrens 			error = EINVAL;
1217789Sahrens 			goto out;
1218789Sahrens 		}
1219789Sahrens 
1220789Sahrens 		tx = dmu_tx_create(os);
1221789Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
12225331Samw 		if (zfsvfs->z_fuid_obj == 0) {
12235331Samw 			dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
12245331Samw 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
12255331Samw 			    SPA_MAXBLOCKSIZE);
12265331Samw 			dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
12275331Samw 		} else {
12285331Samw 			dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
12295331Samw 			dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
12305331Samw 			    SPA_MAXBLOCKSIZE);
12315331Samw 		}
1232789Sahrens 		dmu_tx_hold_bonus(tx, dzp->z_id);
12331544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
12345331Samw 		if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp) {
1235789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1236789Sahrens 			    0, SPA_MAXBLOCKSIZE);
12375331Samw 		}
1238789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
1239789Sahrens 		if (error) {
1240789Sahrens 			zfs_dirent_unlock(dl);
1241789Sahrens 			if (error == ERESTART &&
1242789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
12432113Sahrens 				dmu_tx_wait(tx);
12442113Sahrens 				dmu_tx_abort(tx);
1245789Sahrens 				goto top;
1246789Sahrens 			}
12472113Sahrens 			dmu_tx_abort(tx);
1248789Sahrens 			ZFS_EXIT(zfsvfs);
12495331Samw 			if (aclp)
12505331Samw 				zfs_acl_free(aclp);
1251789Sahrens 			return (error);
1252789Sahrens 		}
12535331Samw 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0, aclp, &fuidp);
1254789Sahrens 		ASSERT(zp->z_id == zoid);
1255789Sahrens 		(void) zfs_link_create(dl, zp, tx, ZNEW);
12565331Samw 		txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
12575331Samw 		if (flag & FIGNORECASE)
12585331Samw 			txtype |= TX_CI;
12595331Samw 		zfs_log_create(zilog, tx, txtype, dzp, zp, name,
12605331Samw 		    vsecp, fuidp, vap);
12615331Samw 		if (fuidp)
12625331Samw 			zfs_fuid_info_free(fuidp);
1263789Sahrens 		dmu_tx_commit(tx);
1264789Sahrens 	} else {
12655331Samw 		int aflags = (flag & FAPPEND) ? V_APPEND : 0;
12665331Samw 
1267789Sahrens 		/*
1268789Sahrens 		 * A directory entry already exists for this name.
1269789Sahrens 		 */
1270789Sahrens 		/*
1271789Sahrens 		 * Can't truncate an existing file if in exclusive mode.
1272789Sahrens 		 */
1273789Sahrens 		if (excl == EXCL) {
1274789Sahrens 			error = EEXIST;
1275789Sahrens 			goto out;
1276789Sahrens 		}
1277789Sahrens 		/*
1278789Sahrens 		 * Can't open a directory for writing.
1279789Sahrens 		 */
1280789Sahrens 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1281789Sahrens 			error = EISDIR;
1282789Sahrens 			goto out;
1283789Sahrens 		}
1284789Sahrens 		/*
1285789Sahrens 		 * Verify requested access to file.
1286789Sahrens 		 */
12875331Samw 		if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1288789Sahrens 			goto out;
1289789Sahrens 		}
1290789Sahrens 
1291789Sahrens 		mutex_enter(&dzp->z_lock);
1292789Sahrens 		dzp->z_seq++;
1293789Sahrens 		mutex_exit(&dzp->z_lock);
1294789Sahrens 
12951878Smaybee 		/*
12961878Smaybee 		 * Truncate regular files if requested.
12971878Smaybee 		 */
12981878Smaybee 		if ((ZTOV(zp)->v_type == VREG) &&
1299789Sahrens 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
13001878Smaybee 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
13011878Smaybee 			if (error == ERESTART &&
13021878Smaybee 			    zfsvfs->z_assign == TXG_NOWAIT) {
13032113Sahrens 				/* NB: we already did dmu_tx_wait() */
13041878Smaybee 				zfs_dirent_unlock(dl);
13052365Sperrin 				VN_RELE(ZTOV(zp));
13061878Smaybee 				goto top;
1307789Sahrens 			}
13084863Spraks 
13094863Spraks 			if (error == 0) {
13105331Samw 				vnevent_create(ZTOV(zp), ct);
13114863Spraks 			}
1312789Sahrens 		}
1313789Sahrens 	}
1314789Sahrens out:
1315789Sahrens 
1316789Sahrens 	if (dl)
1317789Sahrens 		zfs_dirent_unlock(dl);
1318789Sahrens 
1319789Sahrens 	if (error) {
1320789Sahrens 		if (zp)
1321789Sahrens 			VN_RELE(ZTOV(zp));
1322789Sahrens 	} else {
1323789Sahrens 		*vpp = ZTOV(zp);
1324789Sahrens 		/*
1325789Sahrens 		 * If vnode is for a device return a specfs vnode instead.
1326789Sahrens 		 */
1327789Sahrens 		if (IS_DEVVP(*vpp)) {
1328789Sahrens 			struct vnode *svp;
1329789Sahrens 
1330789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1331789Sahrens 			VN_RELE(*vpp);
1332789Sahrens 			if (svp == NULL) {
1333789Sahrens 				error = ENOSYS;
1334789Sahrens 			}
1335789Sahrens 			*vpp = svp;
1336789Sahrens 		}
1337789Sahrens 	}
13385331Samw 	if (aclp)
13395331Samw 		zfs_acl_free(aclp);
1340789Sahrens 
1341789Sahrens 	ZFS_EXIT(zfsvfs);
1342789Sahrens 	return (error);
1343789Sahrens }
1344789Sahrens 
1345789Sahrens /*
1346789Sahrens  * Remove an entry from a directory.
1347789Sahrens  *
1348789Sahrens  *	IN:	dvp	- vnode of directory to remove entry from.
1349789Sahrens  *		name	- name of entry to remove.
1350789Sahrens  *		cr	- credentials of caller.
13515331Samw  *		ct	- caller context
13525331Samw  *		flags	- case flags
1353789Sahrens  *
1354789Sahrens  *	RETURN:	0 if success
1355789Sahrens  *		error code if failure
1356789Sahrens  *
1357789Sahrens  * Timestamps:
1358789Sahrens  *	dvp - ctime|mtime
1359789Sahrens  *	 vp - ctime (if nlink > 0)
1360789Sahrens  */
13615331Samw /*ARGSUSED*/
1362789Sahrens static int
13635331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
13645331Samw     int flags)
1365789Sahrens {
1366789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1367789Sahrens 	znode_t		*xzp = NULL;
1368789Sahrens 	vnode_t		*vp;
1369789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
13705326Sek110237 	zilog_t		*zilog;
1371789Sahrens 	uint64_t	acl_obj, xattr_obj;
1372789Sahrens 	zfs_dirlock_t	*dl;
1373789Sahrens 	dmu_tx_t	*tx;
13743461Sahrens 	boolean_t	may_delete_now, delete_now = FALSE;
13753461Sahrens 	boolean_t	unlinked;
13765331Samw 	uint64_t	txtype;
13775331Samw 	pathname_t	*realnmp = NULL;
13785331Samw 	pathname_t	realnm;
1379789Sahrens 	int		error;
13805331Samw 	int		zflg = ZEXISTS;
1381789Sahrens 
1382*5367Sahrens 	ZFS_ENTER(zfsvfs);
1383*5367Sahrens 	ZFS_VERIFY_ZP(dzp);
13845326Sek110237 	zilog = zfsvfs->z_log;
1385789Sahrens 
13865331Samw 	if (flags & FIGNORECASE) {
13875331Samw 		zflg |= ZCILOOK;
13885331Samw 		pn_alloc(&realnm);
13895331Samw 		realnmp = &realnm;
13905331Samw 	}
13915331Samw 
1392789Sahrens top:
1393789Sahrens 	/*
1394789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1395789Sahrens 	 */
13965331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
13975331Samw 	    NULL, realnmp)) {
13985331Samw 		if (realnmp)
13995331Samw 			pn_free(realnmp);
1400789Sahrens 		ZFS_EXIT(zfsvfs);
1401789Sahrens 		return (error);
1402789Sahrens 	}
1403789Sahrens 
1404789Sahrens 	vp = ZTOV(zp);
1405789Sahrens 
1406789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1407789Sahrens 		goto out;
1408789Sahrens 	}
1409789Sahrens 
1410789Sahrens 	/*
1411789Sahrens 	 * Need to use rmdir for removing directories.
1412789Sahrens 	 */
1413789Sahrens 	if (vp->v_type == VDIR) {
1414789Sahrens 		error = EPERM;
1415789Sahrens 		goto out;
1416789Sahrens 	}
1417789Sahrens 
14185331Samw 	vnevent_remove(vp, dvp, name, ct);
14195331Samw 
14205331Samw 	if (realnmp)
14215331Samw 		dnlc_remove(dvp, realnmp->pn_path);
14225331Samw 	else
14235331Samw 		dnlc_remove(dvp, name);
14241484Sek110237 
1425789Sahrens 	mutex_enter(&vp->v_lock);
1426789Sahrens 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1427789Sahrens 	mutex_exit(&vp->v_lock);
1428789Sahrens 
1429789Sahrens 	/*
14303461Sahrens 	 * We may delete the znode now, or we may put it in the unlinked set;
1431789Sahrens 	 * it depends on whether we're the last link, and on whether there are
1432789Sahrens 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1433789Sahrens 	 * allow for either case.
1434789Sahrens 	 */
1435789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
14361544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1437789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
1438789Sahrens 	if (may_delete_now)
1439789Sahrens 		dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
1440789Sahrens 
1441789Sahrens 	/* are there any extended attributes? */
1442789Sahrens 	if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1443789Sahrens 		/* XXX - do we need this if we are deleting? */
1444789Sahrens 		dmu_tx_hold_bonus(tx, xattr_obj);
1445789Sahrens 	}
1446789Sahrens 
1447789Sahrens 	/* are there any additional acls */
1448789Sahrens 	if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1449789Sahrens 	    may_delete_now)
1450789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1451789Sahrens 
1452789Sahrens 	/* charge as an update -- would be nice not to charge at all */
14533461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1454789Sahrens 
1455789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1456789Sahrens 	if (error) {
1457789Sahrens 		zfs_dirent_unlock(dl);
1458789Sahrens 		VN_RELE(vp);
1459789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
14602113Sahrens 			dmu_tx_wait(tx);
14612113Sahrens 			dmu_tx_abort(tx);
1462789Sahrens 			goto top;
1463789Sahrens 		}
14645331Samw 		if (realnmp)
14655331Samw 			pn_free(realnmp);
14662113Sahrens 		dmu_tx_abort(tx);
1467789Sahrens 		ZFS_EXIT(zfsvfs);
1468789Sahrens 		return (error);
1469789Sahrens 	}
1470789Sahrens 
1471789Sahrens 	/*
1472789Sahrens 	 * Remove the directory entry.
1473789Sahrens 	 */
14745331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1475789Sahrens 
1476789Sahrens 	if (error) {
1477789Sahrens 		dmu_tx_commit(tx);
1478789Sahrens 		goto out;
1479789Sahrens 	}
1480789Sahrens 
14813461Sahrens 	if (unlinked) {
1482789Sahrens 		mutex_enter(&vp->v_lock);
1483789Sahrens 		delete_now = may_delete_now &&
1484789Sahrens 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1485789Sahrens 		    zp->z_phys->zp_xattr == xattr_obj &&
1486789Sahrens 		    zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1487789Sahrens 		mutex_exit(&vp->v_lock);
1488789Sahrens 	}
1489789Sahrens 
1490789Sahrens 	if (delete_now) {
1491789Sahrens 		if (zp->z_phys->zp_xattr) {
1492789Sahrens 			error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1493789Sahrens 			ASSERT3U(error, ==, 0);
1494789Sahrens 			ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1495789Sahrens 			dmu_buf_will_dirty(xzp->z_dbuf, tx);
1496789Sahrens 			mutex_enter(&xzp->z_lock);
14973461Sahrens 			xzp->z_unlinked = 1;
1498789Sahrens 			xzp->z_phys->zp_links = 0;
1499789Sahrens 			mutex_exit(&xzp->z_lock);
15003461Sahrens 			zfs_unlinked_add(xzp, tx);
1501789Sahrens 			zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1502789Sahrens 		}
1503789Sahrens 		mutex_enter(&zp->z_lock);
1504789Sahrens 		mutex_enter(&vp->v_lock);
1505789Sahrens 		vp->v_count--;
1506789Sahrens 		ASSERT3U(vp->v_count, ==, 0);
1507789Sahrens 		mutex_exit(&vp->v_lock);
1508789Sahrens 		mutex_exit(&zp->z_lock);
1509789Sahrens 		zfs_znode_delete(zp, tx);
1510789Sahrens 		VFS_RELE(zfsvfs->z_vfs);
15113461Sahrens 	} else if (unlinked) {
15123461Sahrens 		zfs_unlinked_add(zp, tx);
1513789Sahrens 	}
1514789Sahrens 
15155331Samw 	txtype = TX_REMOVE;
15165331Samw 	if (flags & FIGNORECASE)
15175331Samw 		txtype |= TX_CI;
15185331Samw 	zfs_log_remove(zilog, tx, txtype, dzp, name);
1519789Sahrens 
1520789Sahrens 	dmu_tx_commit(tx);
1521789Sahrens out:
15225331Samw 	if (realnmp)
15235331Samw 		pn_free(realnmp);
15245331Samw 
1525789Sahrens 	zfs_dirent_unlock(dl);
1526789Sahrens 
1527789Sahrens 	if (!delete_now) {
1528789Sahrens 		VN_RELE(vp);
1529789Sahrens 	} else if (xzp) {
1530789Sahrens 		/* this rele delayed to prevent nesting transactions */
1531789Sahrens 		VN_RELE(ZTOV(xzp));
1532789Sahrens 	}
1533789Sahrens 
1534789Sahrens 	ZFS_EXIT(zfsvfs);
1535789Sahrens 	return (error);
1536789Sahrens }
1537789Sahrens 
1538789Sahrens /*
1539789Sahrens  * Create a new directory and insert it into dvp using the name
1540789Sahrens  * provided.  Return a pointer to the inserted directory.
1541789Sahrens  *
1542789Sahrens  *	IN:	dvp	- vnode of directory to add subdir to.
1543789Sahrens  *		dirname	- name of new directory.
1544789Sahrens  *		vap	- attributes of new directory.
1545789Sahrens  *		cr	- credentials of caller.
15465331Samw  *		ct	- caller context
15475331Samw  *		vsecp	- ACL to be set
1548789Sahrens  *
1549789Sahrens  *	OUT:	vpp	- vnode of created directory.
1550789Sahrens  *
1551789Sahrens  *	RETURN:	0 if success
1552789Sahrens  *		error code if failure
1553789Sahrens  *
1554789Sahrens  * Timestamps:
1555789Sahrens  *	dvp - ctime|mtime updated
1556789Sahrens  *	 vp - ctime|mtime|atime updated
1557789Sahrens  */
15585331Samw /*ARGSUSED*/
1559789Sahrens static int
15605331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
15615331Samw     caller_context_t *ct, int flags, vsecattr_t *vsecp)
1562789Sahrens {
1563789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1564789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
15655326Sek110237 	zilog_t		*zilog;
1566789Sahrens 	zfs_dirlock_t	*dl;
1567789Sahrens 	uint64_t	zoid = 0;
15685331Samw 	uint64_t	txtype;
1569789Sahrens 	dmu_tx_t	*tx;
1570789Sahrens 	int		error;
15715331Samw 	zfs_acl_t	*aclp = NULL;
15725331Samw 	zfs_fuid_info_t	*fuidp = NULL;
15735331Samw 	int		zf = ZNEW;
1574789Sahrens 
1575789Sahrens 	ASSERT(vap->va_type == VDIR);
1576789Sahrens 
15775331Samw 	/*
15785331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
15795331Samw 	 * make sure file system is at proper version
15805331Samw 	 */
15815331Samw 
15825331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
15835331Samw 	    (vsecp || (vap->va_mask & AT_XVATTR) || IS_EPHEMERAL(crgetuid(cr))||
15845331Samw 	    IS_EPHEMERAL(crgetgid(cr))))
15855331Samw 		return (EINVAL);
15865331Samw 
1587*5367Sahrens 	ZFS_ENTER(zfsvfs);
1588*5367Sahrens 	ZFS_VERIFY_ZP(dzp);
15895326Sek110237 	zilog = zfsvfs->z_log;
1590789Sahrens 
1591789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1592789Sahrens 		ZFS_EXIT(zfsvfs);
1593789Sahrens 		return (EINVAL);
1594789Sahrens 	}
15955331Samw 
15965331Samw 	if (zfsvfs->z_case & ZFS_UTF8_ONLY && u8_validate(dirname,
15975331Samw 	    strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
15985331Samw 		ZFS_EXIT(zfsvfs);
15995331Samw 		return (EILSEQ);
16005331Samw 	}
16015331Samw 	if (flags & FIGNORECASE)
16025331Samw 		zf |= ZCILOOK;
16035331Samw 
16045331Samw 	if (vap->va_mask & AT_XVATTR)
16055331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
16065331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
16075331Samw 			ZFS_EXIT(zfsvfs);
16085331Samw 			return (error);
16095331Samw 		}
1610789Sahrens 
1611789Sahrens 	/*
1612789Sahrens 	 * First make sure the new directory doesn't exist.
1613789Sahrens 	 */
16145331Samw top:
16155331Samw 	*vpp = NULL;
16165331Samw 
16175331Samw 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
16185331Samw 	    NULL, NULL)) {
1619789Sahrens 		ZFS_EXIT(zfsvfs);
1620789Sahrens 		return (error);
1621789Sahrens 	}
1622789Sahrens 
16235331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
16241231Smarks 		zfs_dirent_unlock(dl);
16251231Smarks 		ZFS_EXIT(zfsvfs);
16261231Smarks 		return (error);
16271231Smarks 	}
16281231Smarks 
16295331Samw 	if (vsecp && aclp == NULL) {
16305331Samw 		error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp);
16315331Samw 		if (error) {
16325331Samw 			zfs_dirent_unlock(dl);
16335331Samw 			ZFS_EXIT(zfsvfs);
16345331Samw 			return (error);
16355331Samw 		}
16365331Samw 	}
1637789Sahrens 	/*
1638789Sahrens 	 * Add a new entry to the directory.
1639789Sahrens 	 */
1640789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
16411544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
16421544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
16435331Samw 	if (zfsvfs->z_fuid_obj == 0) {
16445331Samw 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
16455331Samw 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
16465331Samw 		    SPA_MAXBLOCKSIZE);
16475331Samw 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
16485331Samw 	} else {
16495331Samw 		dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
16505331Samw 		dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
16515331Samw 		    SPA_MAXBLOCKSIZE);
16525331Samw 	}
16535331Samw 	if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp)
1654789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1655789Sahrens 		    0, SPA_MAXBLOCKSIZE);
1656789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1657789Sahrens 	if (error) {
1658789Sahrens 		zfs_dirent_unlock(dl);
1659789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
16602113Sahrens 			dmu_tx_wait(tx);
16612113Sahrens 			dmu_tx_abort(tx);
1662789Sahrens 			goto top;
1663789Sahrens 		}
16642113Sahrens 		dmu_tx_abort(tx);
1665789Sahrens 		ZFS_EXIT(zfsvfs);
16665331Samw 		if (aclp)
16675331Samw 			zfs_acl_free(aclp);
1668789Sahrens 		return (error);
1669789Sahrens 	}
1670789Sahrens 
1671789Sahrens 	/*
1672789Sahrens 	 * Create new node.
1673789Sahrens 	 */
16745331Samw 	zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0, aclp, &fuidp);
16755331Samw 
16765331Samw 	if (aclp)
16775331Samw 		zfs_acl_free(aclp);
1678789Sahrens 
1679789Sahrens 	/*
1680789Sahrens 	 * Now put new name in parent dir.
1681789Sahrens 	 */
1682789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1683789Sahrens 
1684789Sahrens 	*vpp = ZTOV(zp);
1685789Sahrens 
16865331Samw 	txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
16875331Samw 	if (flags & FIGNORECASE)
16885331Samw 		txtype |= TX_CI;
16895331Samw 	zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, fuidp, vap);
16905331Samw 
16915331Samw 	if (fuidp)
16925331Samw 		zfs_fuid_info_free(fuidp);
1693789Sahrens 	dmu_tx_commit(tx);
1694789Sahrens 
1695789Sahrens 	zfs_dirent_unlock(dl);
1696789Sahrens 
1697789Sahrens 	ZFS_EXIT(zfsvfs);
1698789Sahrens 	return (0);
1699789Sahrens }
1700789Sahrens 
1701789Sahrens /*
1702789Sahrens  * Remove a directory subdir entry.  If the current working
1703789Sahrens  * directory is the same as the subdir to be removed, the
1704789Sahrens  * remove will fail.
1705789Sahrens  *
1706789Sahrens  *	IN:	dvp	- vnode of directory to remove from.
1707789Sahrens  *		name	- name of directory to be removed.
1708789Sahrens  *		cwd	- vnode of current working directory.
1709789Sahrens  *		cr	- credentials of caller.
17105331Samw  *		ct	- caller context
17115331Samw  *		flags	- case flags
1712789Sahrens  *
1713789Sahrens  *	RETURN:	0 if success
1714789Sahrens  *		error code if failure
1715789Sahrens  *
1716789Sahrens  * Timestamps:
1717789Sahrens  *	dvp - ctime|mtime updated
1718789Sahrens  */
17195331Samw /*ARGSUSED*/
1720789Sahrens static int
17215331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
17225331Samw     caller_context_t *ct, int flags)
1723789Sahrens {
1724789Sahrens 	znode_t		*dzp = VTOZ(dvp);
1725789Sahrens 	znode_t		*zp;
1726789Sahrens 	vnode_t		*vp;
1727789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
17285326Sek110237 	zilog_t		*zilog;
1729789Sahrens 	zfs_dirlock_t	*dl;
1730789Sahrens 	dmu_tx_t	*tx;
1731789Sahrens 	int		error;
17325331Samw 	int		zflg = ZEXISTS;
1733789Sahrens 
1734*5367Sahrens 	ZFS_ENTER(zfsvfs);
1735*5367Sahrens 	ZFS_VERIFY_ZP(dzp);
17365326Sek110237 	zilog = zfsvfs->z_log;
1737789Sahrens 
17385331Samw 	if (flags & FIGNORECASE)
17395331Samw 		zflg |= ZCILOOK;
1740789Sahrens top:
1741789Sahrens 	zp = NULL;
1742789Sahrens 
1743789Sahrens 	/*
1744789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1745789Sahrens 	 */
17465331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
17475331Samw 	    NULL, NULL)) {
1748789Sahrens 		ZFS_EXIT(zfsvfs);
1749789Sahrens 		return (error);
1750789Sahrens 	}
1751789Sahrens 
1752789Sahrens 	vp = ZTOV(zp);
1753789Sahrens 
1754789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1755789Sahrens 		goto out;
1756789Sahrens 	}
1757789Sahrens 
1758789Sahrens 	if (vp->v_type != VDIR) {
1759789Sahrens 		error = ENOTDIR;
1760789Sahrens 		goto out;
1761789Sahrens 	}
1762789Sahrens 
1763789Sahrens 	if (vp == cwd) {
1764789Sahrens 		error = EINVAL;
1765789Sahrens 		goto out;
1766789Sahrens 	}
1767789Sahrens 
17685331Samw 	vnevent_rmdir(vp, dvp, name, ct);
1769789Sahrens 
1770789Sahrens 	/*
17713897Smaybee 	 * Grab a lock on the directory to make sure that noone is
17723897Smaybee 	 * trying to add (or lookup) entries while we are removing it.
17733897Smaybee 	 */
17743897Smaybee 	rw_enter(&zp->z_name_lock, RW_WRITER);
17753897Smaybee 
17763897Smaybee 	/*
17773897Smaybee 	 * Grab a lock on the parent pointer to make sure we play well
1778789Sahrens 	 * with the treewalk and directory rename code.
1779789Sahrens 	 */
1780789Sahrens 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1781789Sahrens 
1782789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
17831544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1784789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
17853461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1786789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1787789Sahrens 	if (error) {
1788789Sahrens 		rw_exit(&zp->z_parent_lock);
17893897Smaybee 		rw_exit(&zp->z_name_lock);
1790789Sahrens 		zfs_dirent_unlock(dl);
1791789Sahrens 		VN_RELE(vp);
1792789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
17932113Sahrens 			dmu_tx_wait(tx);
17942113Sahrens 			dmu_tx_abort(tx);
1795789Sahrens 			goto top;
1796789Sahrens 		}
17972113Sahrens 		dmu_tx_abort(tx);
1798789Sahrens 		ZFS_EXIT(zfsvfs);
1799789Sahrens 		return (error);
1800789Sahrens 	}
1801789Sahrens 
18025331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
18035331Samw 
18045331Samw 	if (error == 0) {
18055331Samw 		uint64_t txtype = TX_RMDIR;
18065331Samw 		if (flags & FIGNORECASE)
18075331Samw 			txtype |= TX_CI;
18085331Samw 		zfs_log_remove(zilog, tx, txtype, dzp, name);
18095331Samw 	}
1810789Sahrens 
1811789Sahrens 	dmu_tx_commit(tx);
1812789Sahrens 
1813789Sahrens 	rw_exit(&zp->z_parent_lock);
18143897Smaybee 	rw_exit(&zp->z_name_lock);
1815789Sahrens out:
1816789Sahrens 	zfs_dirent_unlock(dl);
1817789Sahrens 
1818789Sahrens 	VN_RELE(vp);
1819789Sahrens 
1820789Sahrens 	ZFS_EXIT(zfsvfs);
1821789Sahrens 	return (error);
1822789Sahrens }
1823789Sahrens 
1824789Sahrens /*
1825789Sahrens  * Read as many directory entries as will fit into the provided
1826789Sahrens  * buffer from the given directory cursor position (specified in
1827789Sahrens  * the uio structure.
1828789Sahrens  *
1829789Sahrens  *	IN:	vp	- vnode of directory to read.
1830789Sahrens  *		uio	- structure supplying read location, range info,
1831789Sahrens  *			  and return buffer.
1832789Sahrens  *		cr	- credentials of caller.
18335331Samw  *		ct	- caller context
18345331Samw  *		flags	- case flags
1835789Sahrens  *
1836789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
1837789Sahrens  *		eofp	- set to true if end-of-file detected.
1838789Sahrens  *
1839789Sahrens  *	RETURN:	0 if success
1840789Sahrens  *		error code if failure
1841789Sahrens  *
1842789Sahrens  * Timestamps:
1843789Sahrens  *	vp - atime updated
1844789Sahrens  *
1845789Sahrens  * Note that the low 4 bits of the cookie returned by zap is always zero.
1846789Sahrens  * This allows us to use the low range for "special" directory entries:
1847789Sahrens  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
1848789Sahrens  * we use the offset 2 for the '.zfs' directory.
1849789Sahrens  */
1850789Sahrens /* ARGSUSED */
1851789Sahrens static int
18525331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
18535331Samw     caller_context_t *ct, int flags)
1854789Sahrens {
1855789Sahrens 	znode_t		*zp = VTOZ(vp);
1856789Sahrens 	iovec_t		*iovp;
18575331Samw 	edirent_t	*eodp;
1858789Sahrens 	dirent64_t	*odp;
1859789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1860869Sperrin 	objset_t	*os;
1861789Sahrens 	caddr_t		outbuf;
1862789Sahrens 	size_t		bufsize;
1863789Sahrens 	zap_cursor_t	zc;
1864789Sahrens 	zap_attribute_t	zap;
1865789Sahrens 	uint_t		bytes_wanted;
1866789Sahrens 	uint64_t	offset; /* must be unsigned; checks for < 1 */
1867789Sahrens 	int		local_eof;
1868869Sperrin 	int		outcount;
1869869Sperrin 	int		error;
1870869Sperrin 	uint8_t		prefetch;
1871789Sahrens 
1872*5367Sahrens 	ZFS_ENTER(zfsvfs);
1873*5367Sahrens 	ZFS_VERIFY_ZP(zp);
1874789Sahrens 
1875789Sahrens 	/*
1876789Sahrens 	 * If we are not given an eof variable,
1877789Sahrens 	 * use a local one.
1878789Sahrens 	 */
1879789Sahrens 	if (eofp == NULL)
1880789Sahrens 		eofp = &local_eof;
1881789Sahrens 
1882789Sahrens 	/*
1883789Sahrens 	 * Check for valid iov_len.
1884789Sahrens 	 */
1885789Sahrens 	if (uio->uio_iov->iov_len <= 0) {
1886789Sahrens 		ZFS_EXIT(zfsvfs);
1887789Sahrens 		return (EINVAL);
1888789Sahrens 	}
1889789Sahrens 
1890789Sahrens 	/*
1891789Sahrens 	 * Quit if directory has been removed (posix)
1892789Sahrens 	 */
18933461Sahrens 	if ((*eofp = zp->z_unlinked) != 0) {
1894789Sahrens 		ZFS_EXIT(zfsvfs);
1895789Sahrens 		return (0);
1896789Sahrens 	}
1897789Sahrens 
1898869Sperrin 	error = 0;
1899869Sperrin 	os = zfsvfs->z_os;
1900869Sperrin 	offset = uio->uio_loffset;
1901869Sperrin 	prefetch = zp->z_zn_prefetch;
1902869Sperrin 
1903789Sahrens 	/*
1904789Sahrens 	 * Initialize the iterator cursor.
1905789Sahrens 	 */
1906789Sahrens 	if (offset <= 3) {
1907789Sahrens 		/*
1908789Sahrens 		 * Start iteration from the beginning of the directory.
1909789Sahrens 		 */
1910869Sperrin 		zap_cursor_init(&zc, os, zp->z_id);
1911789Sahrens 	} else {
1912789Sahrens 		/*
1913789Sahrens 		 * The offset is a serialized cursor.
1914789Sahrens 		 */
1915869Sperrin 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1916789Sahrens 	}
1917789Sahrens 
1918789Sahrens 	/*
1919789Sahrens 	 * Get space to change directory entries into fs independent format.
1920789Sahrens 	 */
1921789Sahrens 	iovp = uio->uio_iov;
1922789Sahrens 	bytes_wanted = iovp->iov_len;
1923789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
1924789Sahrens 		bufsize = bytes_wanted;
1925789Sahrens 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
1926789Sahrens 		odp = (struct dirent64 *)outbuf;
1927789Sahrens 	} else {
1928789Sahrens 		bufsize = bytes_wanted;
1929789Sahrens 		odp = (struct dirent64 *)iovp->iov_base;
1930789Sahrens 	}
19315331Samw 	eodp = (struct edirent *)odp;
1932789Sahrens 
1933789Sahrens 	/*
1934789Sahrens 	 * Transform to file-system independent format
1935789Sahrens 	 */
1936789Sahrens 	outcount = 0;
1937789Sahrens 	while (outcount < bytes_wanted) {
19383912Slling 		ino64_t objnum;
19393912Slling 		ushort_t reclen;
19403912Slling 		off64_t *next;
19413912Slling 
1942789Sahrens 		/*
1943789Sahrens 		 * Special case `.', `..', and `.zfs'.
1944789Sahrens 		 */
1945789Sahrens 		if (offset == 0) {
1946789Sahrens 			(void) strcpy(zap.za_name, ".");
19475331Samw 			zap.za_normalization_conflict = 0;
19483912Slling 			objnum = zp->z_id;
1949789Sahrens 		} else if (offset == 1) {
1950789Sahrens 			(void) strcpy(zap.za_name, "..");
19515331Samw 			zap.za_normalization_conflict = 0;
19523912Slling 			objnum = zp->z_phys->zp_parent;
1953789Sahrens 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
1954789Sahrens 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
19555331Samw 			zap.za_normalization_conflict = 0;
19563912Slling 			objnum = ZFSCTL_INO_ROOT;
1957789Sahrens 		} else {
1958789Sahrens 			/*
1959789Sahrens 			 * Grab next entry.
1960789Sahrens 			 */
1961789Sahrens 			if (error = zap_cursor_retrieve(&zc, &zap)) {
1962789Sahrens 				if ((*eofp = (error == ENOENT)) != 0)
1963789Sahrens 					break;
1964789Sahrens 				else
1965789Sahrens 					goto update;
1966789Sahrens 			}
1967789Sahrens 
1968789Sahrens 			if (zap.za_integer_length != 8 ||
1969789Sahrens 			    zap.za_num_integers != 1) {
1970789Sahrens 				cmn_err(CE_WARN, "zap_readdir: bad directory "
1971789Sahrens 				    "entry, obj = %lld, offset = %lld\n",
1972789Sahrens 				    (u_longlong_t)zp->z_id,
1973789Sahrens 				    (u_longlong_t)offset);
1974789Sahrens 				error = ENXIO;
1975789Sahrens 				goto update;
1976789Sahrens 			}
19773912Slling 
19783912Slling 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
19793912Slling 			/*
19803912Slling 			 * MacOS X can extract the object type here such as:
19813912Slling 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
19823912Slling 			 */
1983789Sahrens 		}
19845331Samw 
19855331Samw 		if (flags & V_RDDIR_ENTFLAGS)
19865331Samw 			reclen = EDIRENT_RECLEN(strlen(zap.za_name));
19875331Samw 		else
19885331Samw 			reclen = DIRENT64_RECLEN(strlen(zap.za_name));
1989789Sahrens 
1990789Sahrens 		/*
1991789Sahrens 		 * Will this entry fit in the buffer?
1992789Sahrens 		 */
19933912Slling 		if (outcount + reclen > bufsize) {
1994789Sahrens 			/*
1995789Sahrens 			 * Did we manage to fit anything in the buffer?
1996789Sahrens 			 */
1997789Sahrens 			if (!outcount) {
1998789Sahrens 				error = EINVAL;
1999789Sahrens 				goto update;
2000789Sahrens 			}
2001789Sahrens 			break;
2002789Sahrens 		}
20035331Samw 		if (flags & V_RDDIR_ENTFLAGS) {
20045331Samw 			/*
20055331Samw 			 * Add extended flag entry:
20065331Samw 			 */
20075331Samw 			eodp->ed_ino = objnum;
20085331Samw 			eodp->ed_reclen = reclen;
20095331Samw 			/* NOTE: ed_off is the offset for the *next* entry */
20105331Samw 			next = &(eodp->ed_off);
20115331Samw 			eodp->ed_eflags = zap.za_normalization_conflict ?
20125331Samw 			    ED_CASE_CONFLICT : 0;
20135331Samw 			(void) strncpy(eodp->ed_name, zap.za_name,
20145331Samw 			    EDIRENT_NAMELEN(reclen));
20155331Samw 			eodp = (edirent_t *)((intptr_t)eodp + reclen);
20165331Samw 		} else {
20175331Samw 			/*
20185331Samw 			 * Add normal entry:
20195331Samw 			 */
20205331Samw 			odp->d_ino = objnum;
20215331Samw 			odp->d_reclen = reclen;
20225331Samw 			/* NOTE: d_off is the offset for the *next* entry */
20235331Samw 			next = &(odp->d_off);
20245331Samw 			(void) strncpy(odp->d_name, zap.za_name,
20255331Samw 			    DIRENT64_NAMELEN(reclen));
20265331Samw 			odp = (dirent64_t *)((intptr_t)odp + reclen);
20275331Samw 		}
20283912Slling 		outcount += reclen;
2029789Sahrens 
2030789Sahrens 		ASSERT(outcount <= bufsize);
2031789Sahrens 
2032789Sahrens 		/* Prefetch znode */
2033869Sperrin 		if (prefetch)
20343912Slling 			dmu_prefetch(os, objnum, 0, 0);
2035789Sahrens 
2036789Sahrens 		/*
2037789Sahrens 		 * Move to the next entry, fill in the previous offset.
2038789Sahrens 		 */
2039789Sahrens 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2040789Sahrens 			zap_cursor_advance(&zc);
2041789Sahrens 			offset = zap_cursor_serialize(&zc);
2042789Sahrens 		} else {
2043789Sahrens 			offset += 1;
2044789Sahrens 		}
2045789Sahrens 		*next = offset;
2046789Sahrens 	}
2047869Sperrin 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2048789Sahrens 
2049789Sahrens 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2050789Sahrens 		iovp->iov_base += outcount;
2051789Sahrens 		iovp->iov_len -= outcount;
2052789Sahrens 		uio->uio_resid -= outcount;
2053789Sahrens 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2054789Sahrens 		/*
2055789Sahrens 		 * Reset the pointer.
2056789Sahrens 		 */
2057789Sahrens 		offset = uio->uio_loffset;
2058789Sahrens 	}
2059789Sahrens 
2060789Sahrens update:
2061885Sahrens 	zap_cursor_fini(&zc);
2062789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2063789Sahrens 		kmem_free(outbuf, bufsize);
2064789Sahrens 
2065789Sahrens 	if (error == ENOENT)
2066789Sahrens 		error = 0;
2067789Sahrens 
2068789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2069789Sahrens 
2070789Sahrens 	uio->uio_loffset = offset;
2071789Sahrens 	ZFS_EXIT(zfsvfs);
2072789Sahrens 	return (error);
2073789Sahrens }
2074789Sahrens 
20754720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4;
20764720Sfr157268 
2077789Sahrens static int
20785331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2079789Sahrens {
2080789Sahrens 	znode_t	*zp = VTOZ(vp);
2081789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2082789Sahrens 
20831773Seschrock 	/*
20841773Seschrock 	 * Regardless of whether this is required for standards conformance,
20851773Seschrock 	 * this is the logical behavior when fsync() is called on a file with
20861773Seschrock 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
20871773Seschrock 	 * going to be pushed out as part of the zil_commit().
20881773Seschrock 	 */
20891773Seschrock 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
20901773Seschrock 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
20915331Samw 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
20921773Seschrock 
20934720Sfr157268 	(void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
20944720Sfr157268 
2095*5367Sahrens 	ZFS_ENTER(zfsvfs);
2096*5367Sahrens 	ZFS_VERIFY_ZP(zp);
20972638Sperrin 	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
2098789Sahrens 	ZFS_EXIT(zfsvfs);
2099789Sahrens 	return (0);
2100789Sahrens }
2101789Sahrens 
21025331Samw 
2103789Sahrens /*
2104789Sahrens  * Get the requested file attributes and place them in the provided
2105789Sahrens  * vattr structure.
2106789Sahrens  *
2107789Sahrens  *	IN:	vp	- vnode of file.
2108789Sahrens  *		vap	- va_mask identifies requested attributes.
21095331Samw  *			  If AT_XVATTR set, then optional attrs are requested
21105331Samw  *		flags	- ATTR_NOACLCHECK (CIFS server context)
2111789Sahrens  *		cr	- credentials of caller.
21125331Samw  *		ct	- caller context
2113789Sahrens  *
2114789Sahrens  *	OUT:	vap	- attribute values.
2115789Sahrens  *
2116789Sahrens  *	RETURN:	0 (always succeeds)
2117789Sahrens  */
2118789Sahrens /* ARGSUSED */
2119789Sahrens static int
21205331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
21215331Samw     caller_context_t *ct)
2122789Sahrens {
2123789Sahrens 	znode_t *zp = VTOZ(vp);
2124789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
21255326Sek110237 	znode_phys_t *pzp;
21265331Samw 	int	error = 0;
21274543Smarks 	uint64_t links;
21285331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
21295331Samw 	xoptattr_t *xoap = NULL;
21305331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2131789Sahrens 
2132*5367Sahrens 	ZFS_ENTER(zfsvfs);
2133*5367Sahrens 	ZFS_VERIFY_ZP(zp);
21345326Sek110237 	pzp = zp->z_phys;
2135789Sahrens 
21365331Samw 	mutex_enter(&zp->z_lock);
21375331Samw 
21385331Samw 	/*
21395331Samw 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
21405331Samw 	 * Also, if we are the owner don't bother, since owner should
21415331Samw 	 * always be allowed to read basic attributes of file.
21425331Samw 	 */
21435331Samw 	if (!(pzp->zp_flags & ZFS_ACL_TRIVIAL) &&
21445331Samw 	    (pzp->zp_uid != crgetuid(cr))) {
21455331Samw 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
21465331Samw 		    skipaclchk, cr)) {
21475331Samw 			mutex_exit(&zp->z_lock);
21485331Samw 			ZFS_EXIT(zfsvfs);
21495331Samw 			return (error);
21505331Samw 		}
21515331Samw 	}
21525331Samw 
2153789Sahrens 	/*
2154789Sahrens 	 * Return all attributes.  It's cheaper to provide the answer
2155789Sahrens 	 * than to determine whether we were asked the question.
2156789Sahrens 	 */
2157789Sahrens 
2158789Sahrens 	vap->va_type = vp->v_type;
2159789Sahrens 	vap->va_mode = pzp->zp_mode & MODEMASK;
21605331Samw 	zfs_fuid_map_ids(zp, &vap->va_uid, &vap->va_gid);
2161789Sahrens 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2162789Sahrens 	vap->va_nodeid = zp->z_id;
21634543Smarks 	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
21644543Smarks 		links = pzp->zp_links + 1;
21654543Smarks 	else
21664543Smarks 		links = pzp->zp_links;
21674543Smarks 	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
2168789Sahrens 	vap->va_size = pzp->zp_size;
21691816Smarks 	vap->va_rdev = vp->v_rdev;
2170789Sahrens 	vap->va_seq = zp->z_seq;
2171789Sahrens 
21725331Samw 	/*
21735331Samw 	 * Add in any requested optional attributes and the create time.
21745331Samw 	 * Also set the corresponding bits in the returned attribute bitmap.
21755331Samw 	 */
21765331Samw 	if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
21775331Samw 		if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
21785331Samw 			xoap->xoa_archive =
21795331Samw 			    ((pzp->zp_flags & ZFS_ARCHIVE) != 0);
21805331Samw 			XVA_SET_RTN(xvap, XAT_ARCHIVE);
21815331Samw 		}
21825331Samw 
21835331Samw 		if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
21845331Samw 			xoap->xoa_readonly =
21855331Samw 			    ((pzp->zp_flags & ZFS_READONLY) != 0);
21865331Samw 			XVA_SET_RTN(xvap, XAT_READONLY);
21875331Samw 		}
21885331Samw 
21895331Samw 		if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
21905331Samw 			xoap->xoa_system =
21915331Samw 			    ((pzp->zp_flags & ZFS_SYSTEM) != 0);
21925331Samw 			XVA_SET_RTN(xvap, XAT_SYSTEM);
21935331Samw 		}
21945331Samw 
21955331Samw 		if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
21965331Samw 			xoap->xoa_hidden =
21975331Samw 			    ((pzp->zp_flags & ZFS_HIDDEN) != 0);
21985331Samw 			XVA_SET_RTN(xvap, XAT_HIDDEN);
21995331Samw 		}
22005331Samw 
22015331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
22025331Samw 			xoap->xoa_nounlink =
22035331Samw 			    ((pzp->zp_flags & ZFS_NOUNLINK) != 0);
22045331Samw 			XVA_SET_RTN(xvap, XAT_NOUNLINK);
22055331Samw 		}
22065331Samw 
22075331Samw 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
22085331Samw 			xoap->xoa_immutable =
22095331Samw 			    ((pzp->zp_flags & ZFS_IMMUTABLE) != 0);
22105331Samw 			XVA_SET_RTN(xvap, XAT_IMMUTABLE);
22115331Samw 		}
22125331Samw 
22135331Samw 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
22145331Samw 			xoap->xoa_appendonly =
22155331Samw 			    ((pzp->zp_flags & ZFS_APPENDONLY) != 0);
22165331Samw 			XVA_SET_RTN(xvap, XAT_APPENDONLY);
22175331Samw 		}
22185331Samw 
22195331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
22205331Samw 			xoap->xoa_nodump =
22215331Samw 			    ((pzp->zp_flags & ZFS_NODUMP) != 0);
22225331Samw 			XVA_SET_RTN(xvap, XAT_NODUMP);
22235331Samw 		}
22245331Samw 
22255331Samw 		if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
22265331Samw 			xoap->xoa_opaque =
22275331Samw 			    ((pzp->zp_flags & ZFS_OPAQUE) != 0);
22285331Samw 			XVA_SET_RTN(xvap, XAT_OPAQUE);
22295331Samw 		}
22305331Samw 
22315331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
22325331Samw 			xoap->xoa_av_quarantined =
22335331Samw 			    ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0);
22345331Samw 			XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
22355331Samw 		}
22365331Samw 
22375331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
22385331Samw 			xoap->xoa_av_modified =
22395331Samw 			    ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0);
22405331Samw 			XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
22415331Samw 		}
22425331Samw 
22435331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
22445331Samw 		    vp->v_type == VREG &&
22455331Samw 		    (pzp->zp_flags & ZFS_BONUS_SCANSTAMP)) {
22465331Samw 			size_t len;
22475331Samw 			dmu_object_info_t doi;
22485331Samw 
22495331Samw 			/*
22505331Samw 			 * Only VREG files have anti-virus scanstamps, so we
22515331Samw 			 * won't conflict with symlinks in the bonus buffer.
22525331Samw 			 */
22535331Samw 			dmu_object_info_from_db(zp->z_dbuf, &doi);
22545331Samw 			len = sizeof (xoap->xoa_av_scanstamp) +
22555331Samw 			    sizeof (znode_phys_t);
22565331Samw 			if (len <= doi.doi_bonus_size) {
22575331Samw 				/*
22585331Samw 				 * pzp points to the start of the
22595331Samw 				 * znode_phys_t. pzp + 1 points to the
22605331Samw 				 * first byte after the znode_phys_t.
22615331Samw 				 */
22625331Samw 				(void) memcpy(xoap->xoa_av_scanstamp,
22635331Samw 				    pzp + 1,
22645331Samw 				    sizeof (xoap->xoa_av_scanstamp));
22655331Samw 				XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
22665331Samw 			}
22675331Samw 		}
22685331Samw 
22695331Samw 		if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
22705331Samw 			ZFS_TIME_DECODE(&xoap->xoa_createtime, pzp->zp_crtime);
22715331Samw 			XVA_SET_RTN(xvap, XAT_CREATETIME);
22725331Samw 		}
22735331Samw 	}
22745331Samw 
2275789Sahrens 	ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
2276789Sahrens 	ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
2277789Sahrens 	ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
2278789Sahrens 
2279789Sahrens 	mutex_exit(&zp->z_lock);
2280789Sahrens 
2281789Sahrens 	dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks);
2282789Sahrens 
2283789Sahrens 	if (zp->z_blksz == 0) {
2284789Sahrens 		/*
2285789Sahrens 		 * Block size hasn't been set; suggest maximal I/O transfers.
2286789Sahrens 		 */
2287789Sahrens 		vap->va_blksize = zfsvfs->z_max_blksz;
2288789Sahrens 	}
2289789Sahrens 
2290789Sahrens 	ZFS_EXIT(zfsvfs);
2291789Sahrens 	return (0);
2292789Sahrens }
2293789Sahrens 
2294789Sahrens /*
2295789Sahrens  * Set the file attributes to the values contained in the
2296789Sahrens  * vattr structure.
2297789Sahrens  *
2298789Sahrens  *	IN:	vp	- vnode of file to be modified.
2299789Sahrens  *		vap	- new attribute values.
23005331Samw  *			  If AT_XVATTR set, then optional attrs are being set
2301789Sahrens  *		flags	- ATTR_UTIME set if non-default time values provided.
23025331Samw  *			- ATTR_NOACLCHECK (CIFS context only).
2303789Sahrens  *		cr	- credentials of caller.
23045331Samw  *		ct	- caller context
2305789Sahrens  *
2306789Sahrens  *	RETURN:	0 if success
2307789Sahrens  *		error code if failure
2308789Sahrens  *
2309789Sahrens  * Timestamps:
2310789Sahrens  *	vp - ctime updated, mtime updated if size changed.
2311789Sahrens  */
2312789Sahrens /* ARGSUSED */
2313789Sahrens static int
2314789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2315789Sahrens 	caller_context_t *ct)
2316789Sahrens {
23175326Sek110237 	znode_t		*zp = VTOZ(vp);
23185326Sek110237 	znode_phys_t	*pzp;
2319789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
23205326Sek110237 	zilog_t		*zilog;
2321789Sahrens 	dmu_tx_t	*tx;
23221878Smaybee 	vattr_t		oldva;
2323789Sahrens 	uint_t		mask = vap->va_mask;
23241878Smaybee 	uint_t		saved_mask;
23252796Smarks 	int		trim_mask = 0;
2326789Sahrens 	uint64_t	new_mode;
23271231Smarks 	znode_t		*attrzp;
2328789Sahrens 	int		need_policy = FALSE;
2329789Sahrens 	int		err;
23305331Samw 	zfs_fuid_info_t *fuidp = NULL;
23315331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
23325331Samw 	xoptattr_t	*xoap;
23335331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2334789Sahrens 
2335789Sahrens 	if (mask == 0)
2336789Sahrens 		return (0);
2337789Sahrens 
2338789Sahrens 	if (mask & AT_NOSET)
2339789Sahrens 		return (EINVAL);
2340789Sahrens 
2341*5367Sahrens 	ZFS_ENTER(zfsvfs);
2342*5367Sahrens 	ZFS_VERIFY_ZP(zp);
23435331Samw 
23445331Samw 	pzp = zp->z_phys;
23455331Samw 	zilog = zfsvfs->z_log;
23465331Samw 
23475331Samw 	/*
23485331Samw 	 * Make sure that if we have ephemeral uid/gid or xvattr specified
23495331Samw 	 * that file system is at proper version level
23505331Samw 	 */
23515331Samw 
23525331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
23535331Samw 	    (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
23545331Samw 	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
23555331Samw 	    (mask & AT_XVATTR)))
23565331Samw 		return (EINVAL);
23575331Samw 
2358789Sahrens 	if (mask & AT_SIZE && vp->v_type == VDIR)
2359789Sahrens 		return (EISDIR);
2360789Sahrens 
23611394Smarks 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO)
23621308Smarks 		return (EINVAL);
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)) ||
23755331Samw 	    ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME))))
23765331Samw 		return (EPERM);
23775331Samw 
23785331Samw 	if ((mask & AT_SIZE) && (pzp->zp_flags & ZFS_READONLY))
23795331Samw 		return (EPERM);
2380789Sahrens 
2381789Sahrens top:
23821231Smarks 	attrzp = NULL;
2383789Sahrens 
2384789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2385789Sahrens 		ZFS_EXIT(zfsvfs);
2386789Sahrens 		return (EROFS);
2387789Sahrens 	}
2388789Sahrens 
2389789Sahrens 	/*
2390789Sahrens 	 * First validate permissions
2391789Sahrens 	 */
2392789Sahrens 
2393789Sahrens 	if (mask & AT_SIZE) {
23945331Samw 		err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2395789Sahrens 		if (err) {
2396789Sahrens 			ZFS_EXIT(zfsvfs);
2397789Sahrens 			return (err);
2398789Sahrens 		}
23991878Smaybee 		/*
24001878Smaybee 		 * XXX - Note, we are not providing any open
24011878Smaybee 		 * mode flags here (like FNDELAY), so we may
24021878Smaybee 		 * block if there are locks present... this
24031878Smaybee 		 * should be addressed in openat().
24041878Smaybee 		 */
24051878Smaybee 		do {
24061878Smaybee 			err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
24072113Sahrens 			/* NB: we already did dmu_tx_wait() if necessary */
24081878Smaybee 		} while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
24091878Smaybee 		if (err) {
24101878Smaybee 			ZFS_EXIT(zfsvfs);
24111878Smaybee 			return (err);
24121878Smaybee 		}
2413789Sahrens 	}
2414789Sahrens 
24155331Samw 	if (mask & (AT_ATIME|AT_MTIME) ||
24165331Samw 	    ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
24175331Samw 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
24185331Samw 	    XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
24195331Samw 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
24205331Samw 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM))))
24215331Samw 		need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
24225331Samw 		    skipaclchk, cr);
2423789Sahrens 
2424789Sahrens 	if (mask & (AT_UID|AT_GID)) {
2425789Sahrens 		int	idmask = (mask & (AT_UID|AT_GID));
2426789Sahrens 		int	take_owner;
2427789Sahrens 		int	take_group;
2428789Sahrens 
2429789Sahrens 		/*
2430913Smarks 		 * NOTE: even if a new mode is being set,
2431913Smarks 		 * we may clear S_ISUID/S_ISGID bits.
2432913Smarks 		 */
2433913Smarks 
2434913Smarks 		if (!(mask & AT_MODE))
2435913Smarks 			vap->va_mode = pzp->zp_mode;
2436913Smarks 
2437913Smarks 		/*
2438789Sahrens 		 * Take ownership or chgrp to group we are a member of
2439789Sahrens 		 */
2440789Sahrens 
2441789Sahrens 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
24425331Samw 		take_group = (mask & AT_GID) &&
24435331Samw 		    zfs_groupmember(zfsvfs, vap->va_gid, cr);
2444789Sahrens 
2445789Sahrens 		/*
2446789Sahrens 		 * If both AT_UID and AT_GID are set then take_owner and
2447789Sahrens 		 * take_group must both be set in order to allow taking
2448789Sahrens 		 * ownership.
2449789Sahrens 		 *
2450789Sahrens 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2451789Sahrens 		 *
2452789Sahrens 		 */
2453789Sahrens 
2454789Sahrens 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2455789Sahrens 		    ((idmask == AT_UID) && take_owner) ||
2456789Sahrens 		    ((idmask == AT_GID) && take_group)) {
24575331Samw 			if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
24585331Samw 			    skipaclchk, cr) == 0) {
2459789Sahrens 				/*
2460789Sahrens 				 * Remove setuid/setgid for non-privileged users
2461789Sahrens 				 */
24621115Smarks 				secpolicy_setid_clear(vap, cr);
24632796Smarks 				trim_mask = (mask & (AT_UID|AT_GID));
2464789Sahrens 			} else {
2465789Sahrens 				need_policy =  TRUE;
2466789Sahrens 			}
2467789Sahrens 		} else {
2468789Sahrens 			need_policy =  TRUE;
2469789Sahrens 		}
2470789Sahrens 	}
2471789Sahrens 
24722796Smarks 	mutex_enter(&zp->z_lock);
24732796Smarks 	oldva.va_mode = pzp->zp_mode;
24745331Samw 	zfs_fuid_map_ids(zp, &oldva.va_uid, &oldva.va_gid);
24755331Samw 	if (mask & AT_XVATTR) {
24765331Samw 		if ((need_policy == FALSE) &&
24775331Samw 		    (XVA_ISSET_REQ(xvap, XAT_APPENDONLY) &&
24785331Samw 		    xoap->xoa_appendonly !=
24795331Samw 		    ((pzp->zp_flags & ZFS_APPENDONLY) != 0)) ||
24805331Samw 		    (XVA_ISSET_REQ(xvap, XAT_NOUNLINK) &&
24815331Samw 		    xoap->xoa_nounlink !=
24825331Samw 		    ((pzp->zp_flags & ZFS_NOUNLINK) != 0)) ||
24835331Samw 		    (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE) &&
24845331Samw 		    xoap->xoa_immutable !=
24855331Samw 		    ((pzp->zp_flags & ZFS_IMMUTABLE) != 0)) ||
24865331Samw 		    (XVA_ISSET_REQ(xvap, XAT_NODUMP) &&
24875331Samw 		    xoap->xoa_nodump !=
24885331Samw 		    ((pzp->zp_flags & ZFS_NODUMP) != 0)) ||
24895331Samw 		    (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED) &&
24905331Samw 		    xoap->xoa_av_modified !=
24915331Samw 		    ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0)) ||
24925331Samw 		    (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED) &&
24935331Samw 		    xoap->xoa_av_quarantined !=
24945331Samw 		    ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0)) ||
24955331Samw 		    (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) ||
24965331Samw 		    (XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
24975331Samw 			need_policy = TRUE;
24985331Samw 		}
24995331Samw 	}
25005331Samw 
25012796Smarks 	mutex_exit(&zp->z_lock);
25022796Smarks 
25032796Smarks 	if (mask & AT_MODE) {
25045331Samw 		if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
25052796Smarks 			err = secpolicy_setid_setsticky_clear(vp, vap,
25062796Smarks 			    &oldva, cr);
25072796Smarks 			if (err) {
25082796Smarks 				ZFS_EXIT(zfsvfs);
25092796Smarks 				return (err);
25102796Smarks 			}
25112796Smarks 			trim_mask |= AT_MODE;
25122796Smarks 		} else {
25132796Smarks 			need_policy = TRUE;
25142796Smarks 		}
25152796Smarks 	}
2516789Sahrens 
2517789Sahrens 	if (need_policy) {
25181115Smarks 		/*
25191115Smarks 		 * If trim_mask is set then take ownership
25202796Smarks 		 * has been granted or write_acl is present and user
25212796Smarks 		 * has the ability to modify mode.  In that case remove
25222796Smarks 		 * UID|GID and or MODE from mask so that
25231115Smarks 		 * secpolicy_vnode_setattr() doesn't revoke it.
25241115Smarks 		 */
25252796Smarks 
25262796Smarks 		if (trim_mask) {
25272796Smarks 			saved_mask = vap->va_mask;
25282796Smarks 			vap->va_mask &= ~trim_mask;
25292796Smarks 		}
2530789Sahrens 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
25315331Samw 		    (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2532789Sahrens 		if (err) {
2533789Sahrens 			ZFS_EXIT(zfsvfs);
2534789Sahrens 			return (err);
2535789Sahrens 		}
25361115Smarks 
25371115Smarks 		if (trim_mask)
25382796Smarks 			vap->va_mask |= saved_mask;
2539789Sahrens 	}
2540789Sahrens 
2541789Sahrens 	/*
2542789Sahrens 	 * secpolicy_vnode_setattr, or take ownership may have
2543789Sahrens 	 * changed va_mask
2544789Sahrens 	 */
2545789Sahrens 	mask = vap->va_mask;
2546789Sahrens 
2547789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2548789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
25495331Samw 	if (zfsvfs->z_fuid_obj == 0) {
25505331Samw 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
25515331Samw 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
25525331Samw 		    SPA_MAXBLOCKSIZE);
25535331Samw 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
25545331Samw 	} else {
25555331Samw 		dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
25565331Samw 		dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
25575331Samw 		    SPA_MAXBLOCKSIZE);
25585331Samw 	}
2559789Sahrens 
2560789Sahrens 	if (mask & AT_MODE) {
25611576Smarks 		uint64_t pmode = pzp->zp_mode;
25621576Smarks 
25631576Smarks 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2564789Sahrens 
25655331Samw 		if (pzp->zp_acl.z_acl_extern_obj) {
25665331Samw 			/* Are we upgrading ACL from old V0 format to new V1 */
25675331Samw 			if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
25685331Samw 			    pzp->zp_acl.z_acl_version ==
25695331Samw 			    ZFS_ACL_VERSION_INITIAL) {
25705331Samw 				dmu_tx_hold_free(tx,
25715331Samw 				    pzp->zp_acl.z_acl_extern_obj, 0,
25725331Samw 				    DMU_OBJECT_END);
25735331Samw 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
25745331Samw 				    0, sizeof (zfs_object_ace_t) * 2048 + 6);
25755331Samw 			} else {
25765331Samw 				dmu_tx_hold_write(tx,
25775331Samw 				    pzp->zp_acl.z_acl_extern_obj, 0,
25785331Samw 				    SPA_MAXBLOCKSIZE);
25795331Samw 			}
25805331Samw 		} else {
2581789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
25825331Samw 			    0, sizeof (zfs_object_ace_t) * 2048 + 6);
25835331Samw 		}
2584789Sahrens 	}
2585789Sahrens 
25865331Samw 	if ((mask & (AT_UID | AT_GID)) && pzp->zp_xattr != 0) {
25875331Samw 		err = zfs_zget(zp->z_zfsvfs, pzp->zp_xattr, &attrzp);
25881231Smarks 		if (err) {
25891231Smarks 			dmu_tx_abort(tx);
25901231Smarks 			ZFS_EXIT(zfsvfs);
25911231Smarks 			return (err);
25921231Smarks 		}
25931231Smarks 		dmu_tx_hold_bonus(tx, attrzp->z_id);
25941231Smarks 	}
25951231Smarks 
2596789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2597789Sahrens 	if (err) {
25981231Smarks 		if (attrzp)
25991231Smarks 			VN_RELE(ZTOV(attrzp));
2600789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
26012113Sahrens 			dmu_tx_wait(tx);
26022113Sahrens 			dmu_tx_abort(tx);
2603789Sahrens 			goto top;
2604789Sahrens 		}
26052113Sahrens 		dmu_tx_abort(tx);
2606789Sahrens 		ZFS_EXIT(zfsvfs);
2607789Sahrens 		return (err);
2608789Sahrens 	}
2609789Sahrens 
2610789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
2611789Sahrens 
2612789Sahrens 	/*
2613789Sahrens 	 * Set each attribute requested.
2614789Sahrens 	 * We group settings according to the locks they need to acquire.
2615789Sahrens 	 *
2616789Sahrens 	 * Note: you cannot set ctime directly, although it will be
2617789Sahrens 	 * updated as a side-effect of calling this function.
2618789Sahrens 	 */
2619789Sahrens 
2620789Sahrens 	mutex_enter(&zp->z_lock);
2621789Sahrens 
2622789Sahrens 	if (mask & AT_MODE) {
2623789Sahrens 		err = zfs_acl_chmod_setattr(zp, new_mode, tx);
2624789Sahrens 		ASSERT3U(err, ==, 0);
2625789Sahrens 	}
2626789Sahrens 
26271231Smarks 	if (attrzp)
26281231Smarks 		mutex_enter(&attrzp->z_lock);
26291231Smarks 
26301231Smarks 	if (mask & AT_UID) {
26315331Samw 		pzp->zp_uid = zfs_fuid_create(zfsvfs,
26325331Samw 		    vap->va_uid, ZFS_OWNER, tx, &fuidp);
26331231Smarks 		if (attrzp) {
26345331Samw 			attrzp->z_phys->zp_uid = zfs_fuid_create(zfsvfs,
26355331Samw 			    vap->va_uid,  ZFS_OWNER, tx, &fuidp);
26361231Smarks 		}
26371231Smarks 	}
26381231Smarks 
26391231Smarks 	if (mask & AT_GID) {
26405331Samw 		pzp->zp_gid = zfs_fuid_create(zfsvfs, vap->va_gid,
26415331Samw 		    ZFS_GROUP, tx, &fuidp);
26421231Smarks 		if (attrzp)
26435331Samw 			attrzp->z_phys->zp_gid = zfs_fuid_create(zfsvfs,
26445331Samw 			    vap->va_gid, ZFS_GROUP, tx, &fuidp);
26451231Smarks 	}
26461231Smarks 
26471231Smarks 	if (attrzp)
26481231Smarks 		mutex_exit(&attrzp->z_lock);
2649789Sahrens 
2650789Sahrens 	if (mask & AT_ATIME)
2651789Sahrens 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
2652789Sahrens 
2653789Sahrens 	if (mask & AT_MTIME)
2654789Sahrens 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
2655789Sahrens 
26561878Smaybee 	if (mask & AT_SIZE)
2657789Sahrens 		zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
26581878Smaybee 	else if (mask != 0)
2659789Sahrens 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
26605331Samw 	/*
26615331Samw 	 * Do this after setting timestamps to prevent timestamp
26625331Samw 	 * update from toggling bit
26635331Samw 	 */
26645331Samw 
26655331Samw 	if (xoap && (mask & AT_XVATTR)) {
26665331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
26675331Samw 			size_t len;
26685331Samw 			dmu_object_info_t doi;
26695331Samw 
26705331Samw 			ASSERT(vp->v_type == VREG);
26715331Samw 
26725331Samw 			/* Grow the bonus buffer if necessary. */
26735331Samw 			dmu_object_info_from_db(zp->z_dbuf, &doi);
26745331Samw 			len = sizeof (xoap->xoa_av_scanstamp) +
26755331Samw 			    sizeof (znode_phys_t);
26765331Samw 			if (len > doi.doi_bonus_size)
26775331Samw 				VERIFY(dmu_set_bonus(zp->z_dbuf, len, tx) == 0);
26785331Samw 		}
26795331Samw 		zfs_xvattr_set(zp, xvap);
26805331Samw 	}
2681789Sahrens 
26821878Smaybee 	if (mask != 0)
26835331Samw 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
26845331Samw 
26855331Samw 	if (fuidp)
26865331Samw 		zfs_fuid_info_free(fuidp);
2687789Sahrens 	mutex_exit(&zp->z_lock);
2688789Sahrens 
26891231Smarks 	if (attrzp)
26901231Smarks 		VN_RELE(ZTOV(attrzp));
26911231Smarks 
2692789Sahrens 	dmu_tx_commit(tx);
2693789Sahrens 
2694789Sahrens 	ZFS_EXIT(zfsvfs);
2695789Sahrens 	return (err);
2696789Sahrens }
2697789Sahrens 
26983271Smaybee typedef struct zfs_zlock {
26993271Smaybee 	krwlock_t	*zl_rwlock;	/* lock we acquired */
27003271Smaybee 	znode_t		*zl_znode;	/* znode we held */
27013271Smaybee 	struct zfs_zlock *zl_next;	/* next in list */
27023271Smaybee } zfs_zlock_t;
27033271Smaybee 
27043271Smaybee /*
27053271Smaybee  * Drop locks and release vnodes that were held by zfs_rename_lock().
27063271Smaybee  */
27073271Smaybee static void
27083271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp)
27093271Smaybee {
27103271Smaybee 	zfs_zlock_t *zl;
27113271Smaybee 
27123271Smaybee 	while ((zl = *zlpp) != NULL) {
27133271Smaybee 		if (zl->zl_znode != NULL)
27143271Smaybee 			VN_RELE(ZTOV(zl->zl_znode));
27153271Smaybee 		rw_exit(zl->zl_rwlock);
27163271Smaybee 		*zlpp = zl->zl_next;
27173271Smaybee 		kmem_free(zl, sizeof (*zl));
27183271Smaybee 	}
27193271Smaybee }
27203271Smaybee 
2721789Sahrens /*
2722789Sahrens  * Search back through the directory tree, using the ".." entries.
2723789Sahrens  * Lock each directory in the chain to prevent concurrent renames.
2724789Sahrens  * Fail any attempt to move a directory into one of its own descendants.
2725789Sahrens  * XXX - z_parent_lock can overlap with map or grow locks
2726789Sahrens  */
2727789Sahrens static int
2728789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
2729789Sahrens {
2730789Sahrens 	zfs_zlock_t	*zl;
27313638Sbillm 	znode_t		*zp = tdzp;
2732789Sahrens 	uint64_t	rootid = zp->z_zfsvfs->z_root;
2733789Sahrens 	uint64_t	*oidp = &zp->z_id;
2734789Sahrens 	krwlock_t	*rwlp = &szp->z_parent_lock;
2735789Sahrens 	krw_t		rw = RW_WRITER;
2736789Sahrens 
2737789Sahrens 	/*
2738789Sahrens 	 * First pass write-locks szp and compares to zp->z_id.
2739789Sahrens 	 * Later passes read-lock zp and compare to zp->z_parent.
2740789Sahrens 	 */
2741789Sahrens 	do {
27423271Smaybee 		if (!rw_tryenter(rwlp, rw)) {
27433271Smaybee 			/*
27443271Smaybee 			 * Another thread is renaming in this path.
27453271Smaybee 			 * Note that if we are a WRITER, we don't have any
27463271Smaybee 			 * parent_locks held yet.
27473271Smaybee 			 */
27483271Smaybee 			if (rw == RW_READER && zp->z_id > szp->z_id) {
27493271Smaybee 				/*
27503271Smaybee 				 * Drop our locks and restart
27513271Smaybee 				 */
27523271Smaybee 				zfs_rename_unlock(&zl);
27533271Smaybee 				*zlpp = NULL;
27543271Smaybee 				zp = tdzp;
27553271Smaybee 				oidp = &zp->z_id;
27563271Smaybee 				rwlp = &szp->z_parent_lock;
27573271Smaybee 				rw = RW_WRITER;
27583271Smaybee 				continue;
27593271Smaybee 			} else {
27603271Smaybee 				/*
27613271Smaybee 				 * Wait for other thread to drop its locks
27623271Smaybee 				 */
27633271Smaybee 				rw_enter(rwlp, rw);
27643271Smaybee 			}
27653271Smaybee 		}
27663271Smaybee 
2767789Sahrens 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
2768789Sahrens 		zl->zl_rwlock = rwlp;
2769789Sahrens 		zl->zl_znode = NULL;
2770789Sahrens 		zl->zl_next = *zlpp;
2771789Sahrens 		*zlpp = zl;
2772789Sahrens 
2773789Sahrens 		if (*oidp == szp->z_id)		/* We're a descendant of szp */
2774789Sahrens 			return (EINVAL);
2775789Sahrens 
2776789Sahrens 		if (*oidp == rootid)		/* We've hit the top */
2777789Sahrens 			return (0);
2778789Sahrens 
2779789Sahrens 		if (rw == RW_READER) {		/* i.e. not the first pass */
2780789Sahrens 			int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
2781789Sahrens 			if (error)
2782789Sahrens 				return (error);
2783789Sahrens 			zl->zl_znode = zp;
2784789Sahrens 		}
2785789Sahrens 		oidp = &zp->z_phys->zp_parent;
2786789Sahrens 		rwlp = &zp->z_parent_lock;
2787789Sahrens 		rw = RW_READER;
2788789Sahrens 
2789789Sahrens 	} while (zp->z_id != sdzp->z_id);
2790789Sahrens 
2791789Sahrens 	return (0);
2792789Sahrens }
2793789Sahrens 
2794789Sahrens /*
2795789Sahrens  * Move an entry from the provided source directory to the target
2796789Sahrens  * directory.  Change the entry name as indicated.
2797789Sahrens  *
2798789Sahrens  *	IN:	sdvp	- Source directory containing the "old entry".
2799789Sahrens  *		snm	- Old entry name.
2800789Sahrens  *		tdvp	- Target directory to contain the "new entry".
2801789Sahrens  *		tnm	- New entry name.
2802789Sahrens  *		cr	- credentials of caller.
28035331Samw  *		ct	- caller context
28045331Samw  *		flags	- case flags
2805789Sahrens  *
2806789Sahrens  *	RETURN:	0 if success
2807789Sahrens  *		error code if failure
2808789Sahrens  *
2809789Sahrens  * Timestamps:
2810789Sahrens  *	sdvp,tdvp - ctime|mtime updated
2811789Sahrens  */
28125331Samw /*ARGSUSED*/
2813789Sahrens static int
28145331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
28155331Samw     caller_context_t *ct, int flags)
2816789Sahrens {
2817789Sahrens 	znode_t		*tdzp, *szp, *tzp;
2818789Sahrens 	znode_t		*sdzp = VTOZ(sdvp);
2819789Sahrens 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
28205326Sek110237 	zilog_t		*zilog;
2821789Sahrens 	vnode_t		*realvp;
2822789Sahrens 	zfs_dirlock_t	*sdl, *tdl;
2823789Sahrens 	dmu_tx_t	*tx;
2824789Sahrens 	zfs_zlock_t	*zl;
28255331Samw 	int		cmp, serr, terr;
28265331Samw 	int		error = 0;
28275331Samw 	int		zflg = 0;
2828789Sahrens 
2829*5367Sahrens 	ZFS_ENTER(zfsvfs);
2830*5367Sahrens 	ZFS_VERIFY_ZP(sdzp);
28315326Sek110237 	zilog = zfsvfs->z_log;
2832789Sahrens 
2833789Sahrens 	/*
2834789Sahrens 	 * Make sure we have the real vp for the target directory.
2835789Sahrens 	 */
28365331Samw 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
2837789Sahrens 		tdvp = realvp;
2838789Sahrens 
2839789Sahrens 	if (tdvp->v_vfsp != sdvp->v_vfsp) {
2840789Sahrens 		ZFS_EXIT(zfsvfs);
2841789Sahrens 		return (EXDEV);
2842789Sahrens 	}
2843789Sahrens 
2844789Sahrens 	tdzp = VTOZ(tdvp);
2845*5367Sahrens 	ZFS_VERIFY_ZP(tdzp);
28465331Samw 	if (zfsvfs->z_case & ZFS_UTF8_ONLY && u8_validate(tnm,
28475331Samw 	    strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
28485331Samw 		ZFS_EXIT(zfsvfs);
28495331Samw 		return (EILSEQ);
28505331Samw 	}
28515331Samw 
28525331Samw 	if (flags & FIGNORECASE)
28535331Samw 		zflg |= ZCILOOK;
28545331Samw 
2855789Sahrens top:
2856789Sahrens 	szp = NULL;
2857789Sahrens 	tzp = NULL;
2858789Sahrens 	zl = NULL;
2859789Sahrens 
2860789Sahrens 	/*
2861789Sahrens 	 * This is to prevent the creation of links into attribute space
2862789Sahrens 	 * by renaming a linked file into/outof an attribute directory.
2863789Sahrens 	 * See the comment in zfs_link() for why this is considered bad.
2864789Sahrens 	 */
2865789Sahrens 	if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
2866789Sahrens 	    (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
2867789Sahrens 		ZFS_EXIT(zfsvfs);
2868789Sahrens 		return (EINVAL);
2869789Sahrens 	}
2870789Sahrens 
2871789Sahrens 	/*
2872789Sahrens 	 * Lock source and target directory entries.  To prevent deadlock,
2873789Sahrens 	 * a lock ordering must be defined.  We lock the directory with
2874789Sahrens 	 * the smallest object id first, or if it's a tie, the one with
2875789Sahrens 	 * the lexically first name.
2876789Sahrens 	 */
2877789Sahrens 	if (sdzp->z_id < tdzp->z_id) {
2878789Sahrens 		cmp = -1;
2879789Sahrens 	} else if (sdzp->z_id > tdzp->z_id) {
2880789Sahrens 		cmp = 1;
2881789Sahrens 	} else {
28825331Samw 		/*
28835331Samw 		 * First compare the two name arguments without
28845331Samw 		 * considering any case folding.
28855331Samw 		 */
28865331Samw 		int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
28875331Samw 
28885331Samw 		cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
28895331Samw 		ASSERT(error == 0 || !(zfsvfs->z_case & ZFS_UTF8_ONLY));
2890789Sahrens 		if (cmp == 0) {
2891789Sahrens 			/*
2892789Sahrens 			 * POSIX: "If the old argument and the new argument
2893789Sahrens 			 * both refer to links to the same existing file,
2894789Sahrens 			 * the rename() function shall return successfully
2895789Sahrens 			 * and perform no other action."
2896789Sahrens 			 */
2897789Sahrens 			ZFS_EXIT(zfsvfs);
2898789Sahrens 			return (0);
2899789Sahrens 		}
29005331Samw 		/*
29015331Samw 		 * If the file system is case-folding, then we may
29025331Samw 		 * have some more checking to do.  A case-folding file
29035331Samw 		 * system is either supporting mixed case sensitivity
29045331Samw 		 * access or is completely case-insensitive.  Note
29055331Samw 		 * that the file system is always case preserving.
29065331Samw 		 *
29075331Samw 		 * In mixed sensitivity mode case sensitive behavior
29085331Samw 		 * is the default.  FIGNORECASE must be used to
29095331Samw 		 * explicitly request case insensitive behavior.
29105331Samw 		 *
29115331Samw 		 * If the source and target names provided differ only
29125331Samw 		 * by case (e.g., a request to rename 'tim' to 'Tim'),
29135331Samw 		 * we will treat this as a special case in the
29145331Samw 		 * case-insensitive mode: as long as the source name
29155331Samw 		 * is an exact match, we will allow this to proceed as
29165331Samw 		 * a name-change request.
29175331Samw 		 */
29185331Samw 		if ((zfsvfs->z_case & ZFS_CI_ONLY ||
29195331Samw 		    (zfsvfs->z_case & ZFS_CI_MIXD && flags & FIGNORECASE)) &&
29205331Samw 		    u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
29215331Samw 		    &error) == 0) {
29225331Samw 			/*
29235331Samw 			 * case preserving rename request, require exact
29245331Samw 			 * name matches
29255331Samw 			 */
29265331Samw 			zflg |= ZCIEXACT;
29275331Samw 			zflg &= ~ZCILOOK;
29285331Samw 		}
2929789Sahrens 	}
29305331Samw 
2931789Sahrens 	if (cmp < 0) {
29325331Samw 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
29335331Samw 		    ZEXISTS | zflg, NULL, NULL);
29345331Samw 		terr = zfs_dirent_lock(&tdl,
29355331Samw 		    tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
2936789Sahrens 	} else {
29375331Samw 		terr = zfs_dirent_lock(&tdl,
29385331Samw 		    tdzp, tnm, &tzp, zflg, NULL, NULL);
29395331Samw 		serr = zfs_dirent_lock(&sdl,
29405331Samw 		    sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
29415331Samw 		    NULL, NULL);
2942789Sahrens 	}
2943789Sahrens 
2944789Sahrens 	if (serr) {
2945789Sahrens 		/*
2946789Sahrens 		 * Source entry invalid or not there.
2947789Sahrens 		 */
2948789Sahrens 		if (!terr) {
2949789Sahrens 			zfs_dirent_unlock(tdl);
2950789Sahrens 			if (tzp)
2951789Sahrens 				VN_RELE(ZTOV(tzp));
2952789Sahrens 		}
2953789Sahrens 		if (strcmp(snm, "..") == 0)
2954789Sahrens 			serr = EINVAL;
2955789Sahrens 		ZFS_EXIT(zfsvfs);
2956789Sahrens 		return (serr);
2957789Sahrens 	}
2958789Sahrens 	if (terr) {
2959789Sahrens 		zfs_dirent_unlock(sdl);
2960789Sahrens 		VN_RELE(ZTOV(szp));
2961789Sahrens 		if (strcmp(tnm, "..") == 0)
2962789Sahrens 			terr = EINVAL;
2963789Sahrens 		ZFS_EXIT(zfsvfs);
2964789Sahrens 		return (terr);
2965789Sahrens 	}
2966789Sahrens 
2967789Sahrens 	/*
2968789Sahrens 	 * Must have write access at the source to remove the old entry
2969789Sahrens 	 * and write access at the target to create the new entry.
2970789Sahrens 	 * Note that if target and source are the same, this can be
2971789Sahrens 	 * done in a single check.
2972789Sahrens 	 */
2973789Sahrens 
2974789Sahrens 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
2975789Sahrens 		goto out;
2976789Sahrens 
2977789Sahrens 	if (ZTOV(szp)->v_type == VDIR) {
2978789Sahrens 		/*
2979789Sahrens 		 * Check to make sure rename is valid.
2980789Sahrens 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
2981789Sahrens 		 */
2982789Sahrens 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
2983789Sahrens 			goto out;
2984789Sahrens 	}
2985789Sahrens 
2986789Sahrens 	/*
2987789Sahrens 	 * Does target exist?
2988789Sahrens 	 */
2989789Sahrens 	if (tzp) {
2990789Sahrens 		/*
2991789Sahrens 		 * Source and target must be the same type.
2992789Sahrens 		 */
2993789Sahrens 		if (ZTOV(szp)->v_type == VDIR) {
2994789Sahrens 			if (ZTOV(tzp)->v_type != VDIR) {
2995789Sahrens 				error = ENOTDIR;
2996789Sahrens 				goto out;
2997789Sahrens 			}
2998789Sahrens 		} else {
2999789Sahrens 			if (ZTOV(tzp)->v_type == VDIR) {
3000789Sahrens 				error = EISDIR;
3001789Sahrens 				goto out;
3002789Sahrens 			}
3003789Sahrens 		}
3004789Sahrens 		/*
3005789Sahrens 		 * POSIX dictates that when the source and target
3006789Sahrens 		 * entries refer to the same file object, rename
3007789Sahrens 		 * must do nothing and exit without error.
3008789Sahrens 		 */
3009789Sahrens 		if (szp->z_id == tzp->z_id) {
3010789Sahrens 			error = 0;
3011789Sahrens 			goto out;
3012789Sahrens 		}
3013789Sahrens 	}
3014789Sahrens 
30155331Samw 	vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3016789Sahrens 	if (tzp)
30175331Samw 		vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
30184863Spraks 
30194863Spraks 	/*
30204863Spraks 	 * notify the target directory if it is not the same
30214863Spraks 	 * as source directory.
30224863Spraks 	 */
30234863Spraks 	if (tdvp != sdvp) {
30245331Samw 		vnevent_rename_dest_dir(tdvp, ct);
30254863Spraks 	}
3026789Sahrens 
3027789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3028789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);	/* nlink changes */
3029789Sahrens 	dmu_tx_hold_bonus(tx, sdzp->z_id);	/* nlink changes */
30301544Seschrock 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
30311544Seschrock 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
30321544Seschrock 	if (sdzp != tdzp)
3033789Sahrens 		dmu_tx_hold_bonus(tx, tdzp->z_id);	/* nlink changes */
30341544Seschrock 	if (tzp)
30351544Seschrock 		dmu_tx_hold_bonus(tx, tzp->z_id);	/* parent changes */
30363461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3037789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
3038789Sahrens 	if (error) {
3039789Sahrens 		if (zl != NULL)
3040789Sahrens 			zfs_rename_unlock(&zl);
3041789Sahrens 		zfs_dirent_unlock(sdl);
3042789Sahrens 		zfs_dirent_unlock(tdl);
3043789Sahrens 		VN_RELE(ZTOV(szp));
3044789Sahrens 		if (tzp)
3045789Sahrens 			VN_RELE(ZTOV(tzp));
3046789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
30472113Sahrens 			dmu_tx_wait(tx);
30482113Sahrens 			dmu_tx_abort(tx);
3049789Sahrens 			goto top;
3050789Sahrens 		}
30512113Sahrens 		dmu_tx_abort(tx);
3052789Sahrens 		ZFS_EXIT(zfsvfs);
3053789Sahrens 		return (error);
3054789Sahrens 	}
3055789Sahrens 
3056789Sahrens 	if (tzp)	/* Attempt to remove the existing target */
30575331Samw 		error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3058789Sahrens 
3059789Sahrens 	if (error == 0) {
3060789Sahrens 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3061789Sahrens 		if (error == 0) {
30625331Samw 			szp->z_phys->zp_flags |= ZFS_AV_MODIFIED;
30635331Samw 
3064789Sahrens 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3065789Sahrens 			ASSERT(error == 0);
30665331Samw 
30675331Samw 			zfs_log_rename(zilog, tx,
30685331Samw 			    TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0),
30695331Samw 			    sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
3070789Sahrens 		}
3071789Sahrens 	}
3072789Sahrens 
3073789Sahrens 	dmu_tx_commit(tx);
3074789Sahrens out:
3075789Sahrens 	if (zl != NULL)
3076789Sahrens 		zfs_rename_unlock(&zl);
3077789Sahrens 
3078789Sahrens 	zfs_dirent_unlock(sdl);
3079789Sahrens 	zfs_dirent_unlock(tdl);
3080789Sahrens 
3081789Sahrens 	VN_RELE(ZTOV(szp));
3082789Sahrens 	if (tzp)
3083789Sahrens 		VN_RELE(ZTOV(tzp));
3084789Sahrens 
3085789Sahrens 	ZFS_EXIT(zfsvfs);
3086789Sahrens 	return (error);
3087789Sahrens }
3088789Sahrens 
3089789Sahrens /*
3090789Sahrens  * Insert the indicated symbolic reference entry into the directory.
3091789Sahrens  *
3092789Sahrens  *	IN:	dvp	- Directory to contain new symbolic link.
3093789Sahrens  *		link	- Name for new symlink entry.
3094789Sahrens  *		vap	- Attributes of new entry.
3095789Sahrens  *		target	- Target path of new symlink.
3096789Sahrens  *		cr	- credentials of caller.
30975331Samw  *		ct	- caller context
30985331Samw  *		flags	- case flags
3099789Sahrens  *
3100789Sahrens  *	RETURN:	0 if success
3101789Sahrens  *		error code if failure
3102789Sahrens  *
3103789Sahrens  * Timestamps:
3104789Sahrens  *	dvp - ctime|mtime updated
3105789Sahrens  */
31065331Samw /*ARGSUSED*/
3107789Sahrens static int
31085331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
31095331Samw     caller_context_t *ct, int flags)
3110789Sahrens {
3111789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
3112789Sahrens 	zfs_dirlock_t	*dl;
3113789Sahrens 	dmu_tx_t	*tx;
3114789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
31155326Sek110237 	zilog_t		*zilog;
3116789Sahrens 	uint64_t	zoid;
3117789Sahrens 	int		len = strlen(link);
3118789Sahrens 	int		error;
31195331Samw 	int		zflg = ZNEW;
31205331Samw 	zfs_fuid_info_t *fuidp = NULL;
3121789Sahrens 
3122789Sahrens 	ASSERT(vap->va_type == VLNK);
3123789Sahrens 
3124*5367Sahrens 	ZFS_ENTER(zfsvfs);
3125*5367Sahrens 	ZFS_VERIFY_ZP(dzp);
31265326Sek110237 	zilog = zfsvfs->z_log;
31275331Samw 
31285331Samw 	if (zfsvfs->z_case & ZFS_UTF8_ONLY && u8_validate(name, strlen(name),
31295331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
31305331Samw 		ZFS_EXIT(zfsvfs);
31315331Samw 		return (EILSEQ);
31325331Samw 	}
31335331Samw 	if (flags & FIGNORECASE)
31345331Samw 		zflg |= ZCILOOK;
3135789Sahrens top:
31365331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3137789Sahrens 		ZFS_EXIT(zfsvfs);
3138789Sahrens 		return (error);
3139789Sahrens 	}
3140789Sahrens 
3141789Sahrens 	if (len > MAXPATHLEN) {
3142789Sahrens 		ZFS_EXIT(zfsvfs);
3143789Sahrens 		return (ENAMETOOLONG);
3144789Sahrens 	}
3145789Sahrens 
3146789Sahrens 	/*
3147789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3148789Sahrens 	 */
31495331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
31505331Samw 	if (error) {
3151789Sahrens 		ZFS_EXIT(zfsvfs);
3152789Sahrens 		return (error);
3153789Sahrens 	}
3154789Sahrens 
3155789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3156789Sahrens 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3157789Sahrens 	dmu_tx_hold_bonus(tx, dzp->z_id);
31581544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3159789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
3160789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
31615331Samw 	if (zfsvfs->z_fuid_obj == 0) {
31625331Samw 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
31635331Samw 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
31645331Samw 		    SPA_MAXBLOCKSIZE);
31655331Samw 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
31665331Samw 	} else {
31675331Samw 		dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
31685331Samw 		dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
31695331Samw 		    SPA_MAXBLOCKSIZE);
31705331Samw 	}
3171789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
3172789Sahrens 	if (error) {
3173789Sahrens 		zfs_dirent_unlock(dl);
3174789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
31752113Sahrens 			dmu_tx_wait(tx);
31762113Sahrens 			dmu_tx_abort(tx);
3177789Sahrens 			goto top;
3178789Sahrens 		}
31792113Sahrens 		dmu_tx_abort(tx);
3180789Sahrens 		ZFS_EXIT(zfsvfs);
3181789Sahrens 		return (error);
3182789Sahrens 	}
3183789Sahrens 
3184789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
3185789Sahrens 
3186789Sahrens 	/*
3187789Sahrens 	 * Create a new object for the symlink.
3188789Sahrens 	 * Put the link content into bonus buffer if it will fit;
3189789Sahrens 	 * otherwise, store it just like any other file data.
3190789Sahrens 	 */
3191789Sahrens 	zoid = 0;
3192789Sahrens 	if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
31935331Samw 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, len, NULL, &fuidp);
3194789Sahrens 		if (len != 0)
3195789Sahrens 			bcopy(link, zp->z_phys + 1, len);
3196789Sahrens 	} else {
3197789Sahrens 		dmu_buf_t *dbp;
31981669Sperrin 
31995331Samw 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0, NULL, &fuidp);
32001669Sperrin 		/*
32011669Sperrin 		 * Nothing can access the znode yet so no locking needed
32021669Sperrin 		 * for growing the znode's blocksize.
32031669Sperrin 		 */
32041669Sperrin 		zfs_grow_blocksize(zp, len, tx);
3205789Sahrens 
32061544Seschrock 		VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, zoid, 0, FTAG, &dbp));
3207789Sahrens 		dmu_buf_will_dirty(dbp, tx);
3208789Sahrens 
3209789Sahrens 		ASSERT3U(len, <=, dbp->db_size);
3210789Sahrens 		bcopy(link, dbp->db_data, len);
32111544Seschrock 		dmu_buf_rele(dbp, FTAG);
3212789Sahrens 	}
3213789Sahrens 	zp->z_phys->zp_size = len;
3214789Sahrens 
3215789Sahrens 	/*
3216789Sahrens 	 * Insert the new object into the directory.
3217789Sahrens 	 */
3218789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
3219789Sahrens out:
32205331Samw 	if (error == 0) {
32215331Samw 		uint64_t txtype = TX_SYMLINK;
32225331Samw 		if (flags & FIGNORECASE)
32235331Samw 			txtype |= TX_CI;
32245331Samw 		zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
32255331Samw 	}
32265331Samw 	if (fuidp)
32275331Samw 		zfs_fuid_info_free(fuidp);
3228789Sahrens 
3229789Sahrens 	dmu_tx_commit(tx);
3230789Sahrens 
3231789Sahrens 	zfs_dirent_unlock(dl);
3232789Sahrens 
3233789Sahrens 	VN_RELE(ZTOV(zp));
3234789Sahrens 
3235789Sahrens 	ZFS_EXIT(zfsvfs);
3236789Sahrens 	return (error);
3237789Sahrens }
3238789Sahrens 
3239789Sahrens /*
3240789Sahrens  * Return, in the buffer contained in the provided uio structure,
3241789Sahrens  * the symbolic path referred to by vp.
3242789Sahrens  *
3243789Sahrens  *	IN:	vp	- vnode of symbolic link.
3244789Sahrens  *		uoip	- structure to contain the link path.
3245789Sahrens  *		cr	- credentials of caller.
32465331Samw  *		ct	- caller context
3247789Sahrens  *
3248789Sahrens  *	OUT:	uio	- structure to contain the link path.
3249789Sahrens  *
3250789Sahrens  *	RETURN:	0 if success
3251789Sahrens  *		error code if failure
3252789Sahrens  *
3253789Sahrens  * Timestamps:
3254789Sahrens  *	vp - atime updated
3255789Sahrens  */
3256789Sahrens /* ARGSUSED */
3257789Sahrens static int
32585331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3259789Sahrens {
3260789Sahrens 	znode_t		*zp = VTOZ(vp);
3261789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3262789Sahrens 	size_t		bufsz;
3263789Sahrens 	int		error;
3264789Sahrens 
3265*5367Sahrens 	ZFS_ENTER(zfsvfs);
3266*5367Sahrens 	ZFS_VERIFY_ZP(zp);
3267789Sahrens 
3268789Sahrens 	bufsz = (size_t)zp->z_phys->zp_size;
3269789Sahrens 	if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
3270789Sahrens 		error = uiomove(zp->z_phys + 1,
3271789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
3272789Sahrens 	} else {
32731544Seschrock 		dmu_buf_t *dbp;
32741544Seschrock 		error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
32751544Seschrock 		if (error) {
3276789Sahrens 			ZFS_EXIT(zfsvfs);
3277789Sahrens 			return (error);
3278789Sahrens 		}
3279789Sahrens 		error = uiomove(dbp->db_data,
3280789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
32811544Seschrock 		dmu_buf_rele(dbp, FTAG);
3282789Sahrens 	}
3283789Sahrens 
3284789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3285789Sahrens 	ZFS_EXIT(zfsvfs);
3286789Sahrens 	return (error);
3287789Sahrens }
3288789Sahrens 
3289789Sahrens /*
3290789Sahrens  * Insert a new entry into directory tdvp referencing svp.
3291789Sahrens  *
3292789Sahrens  *	IN:	tdvp	- Directory to contain new entry.
3293789Sahrens  *		svp	- vnode of new entry.
3294789Sahrens  *		name	- name of new entry.
3295789Sahrens  *		cr	- credentials of caller.
32965331Samw  *		ct	- caller context
3297789Sahrens  *
3298789Sahrens  *	RETURN:	0 if success
3299789Sahrens  *		error code if failure
3300789Sahrens  *
3301789Sahrens  * Timestamps:
3302789Sahrens  *	tdvp - ctime|mtime updated
3303789Sahrens  *	 svp - ctime updated
3304789Sahrens  */
3305789Sahrens /* ARGSUSED */
3306789Sahrens static int
33075331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
33085331Samw     caller_context_t *ct, int flags)
3309789Sahrens {
3310789Sahrens 	znode_t		*dzp = VTOZ(tdvp);
3311789Sahrens 	znode_t		*tzp, *szp;
3312789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
33135326Sek110237 	zilog_t		*zilog;
3314789Sahrens 	zfs_dirlock_t	*dl;
3315789Sahrens 	dmu_tx_t	*tx;
3316789Sahrens 	vnode_t		*realvp;
3317789Sahrens 	int		error;
33185331Samw 	int		zf = ZNEW;
33195331Samw 	uid_t		owner;
3320789Sahrens 
3321789Sahrens 	ASSERT(tdvp->v_type == VDIR);
3322789Sahrens 
3323*5367Sahrens 	ZFS_ENTER(zfsvfs);
3324*5367Sahrens 	ZFS_VERIFY_ZP(dzp);
33255326Sek110237 	zilog = zfsvfs->z_log;
3326789Sahrens 
33275331Samw 	if (VOP_REALVP(svp, &realvp, ct) == 0)
3328789Sahrens 		svp = realvp;
3329789Sahrens 
3330789Sahrens 	if (svp->v_vfsp != tdvp->v_vfsp) {
3331789Sahrens 		ZFS_EXIT(zfsvfs);
3332789Sahrens 		return (EXDEV);
3333789Sahrens 	}
3334*5367Sahrens 	szp = VTOZ(svp);
3335*5367Sahrens 	ZFS_VERIFY_ZP(szp);
3336789Sahrens 
33375331Samw 	if (zfsvfs->z_case & ZFS_UTF8_ONLY && u8_validate(name,
33385331Samw 	    strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
33395331Samw 		ZFS_EXIT(zfsvfs);
33405331Samw 		return (EILSEQ);
33415331Samw 	}
33425331Samw 	if (flags & FIGNORECASE)
33435331Samw 		zf |= ZCILOOK;
33445331Samw 
3345789Sahrens top:
3346789Sahrens 	/*
3347789Sahrens 	 * We do not support links between attributes and non-attributes
3348789Sahrens 	 * because of the potential security risk of creating links
3349789Sahrens 	 * into "normal" file space in order to circumvent restrictions
3350789Sahrens 	 * imposed in attribute space.
3351789Sahrens 	 */
3352789Sahrens 	if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
3353789Sahrens 	    (dzp->z_phys->zp_flags & ZFS_XATTR)) {
3354789Sahrens 		ZFS_EXIT(zfsvfs);
3355789Sahrens 		return (EINVAL);
3356789Sahrens 	}
3357789Sahrens 
3358789Sahrens 	/*
3359789Sahrens 	 * POSIX dictates that we return EPERM here.
3360789Sahrens 	 * Better choices include ENOTSUP or EISDIR.
3361789Sahrens 	 */
3362789Sahrens 	if (svp->v_type == VDIR) {
3363789Sahrens 		ZFS_EXIT(zfsvfs);
3364789Sahrens 		return (EPERM);
3365789Sahrens 	}
3366789Sahrens 
33675331Samw 	zfs_fuid_map_id(zfsvfs, szp->z_phys->zp_uid, ZFS_OWNER, &owner);
33685331Samw 	if (owner != crgetuid(cr) &&
3369789Sahrens 	    secpolicy_basic_link(cr) != 0) {
3370789Sahrens 		ZFS_EXIT(zfsvfs);
3371789Sahrens 		return (EPERM);
3372789Sahrens 	}
3373789Sahrens 
33745331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3375789Sahrens 		ZFS_EXIT(zfsvfs);
3376789Sahrens 		return (error);
3377789Sahrens 	}
3378789Sahrens 
3379789Sahrens 	/*
3380789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3381789Sahrens 	 */
33825331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
33835331Samw 	if (error) {
3384789Sahrens 		ZFS_EXIT(zfsvfs);
3385789Sahrens 		return (error);
3386789Sahrens 	}
3387789Sahrens 
3388789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3389789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);
33901544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3391789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
3392789Sahrens 	if (error) {
3393789Sahrens 		zfs_dirent_unlock(dl);
3394789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
33952113Sahrens 			dmu_tx_wait(tx);
33962113Sahrens 			dmu_tx_abort(tx);
3397789Sahrens 			goto top;
3398789Sahrens 		}
33992113Sahrens 		dmu_tx_abort(tx);
3400789Sahrens 		ZFS_EXIT(zfsvfs);
3401789Sahrens 		return (error);
3402789Sahrens 	}
3403789Sahrens 
3404789Sahrens 	error = zfs_link_create(dl, szp, tx, 0);
3405789Sahrens 
34065331Samw 	if (error == 0) {
34075331Samw 		uint64_t txtype = TX_LINK;
34085331Samw 		if (flags & FIGNORECASE)
34095331Samw 			txtype |= TX_CI;
34105331Samw 		zfs_log_link(zilog, tx, txtype, dzp, szp, name);
34115331Samw 	}
3412789Sahrens 
3413789Sahrens 	dmu_tx_commit(tx);
3414789Sahrens 
3415789Sahrens 	zfs_dirent_unlock(dl);
3416789Sahrens 
34174863Spraks 	if (error == 0) {
34185331Samw 		vnevent_link(svp, ct);
34194863Spraks 	}
34204863Spraks 
3421789Sahrens 	ZFS_EXIT(zfsvfs);
3422789Sahrens 	return (error);
3423789Sahrens }
3424789Sahrens 
3425789Sahrens /*
3426789Sahrens  * zfs_null_putapage() is used when the file system has been force
3427789Sahrens  * unmounted. It just drops the pages.
3428789Sahrens  */
3429789Sahrens /* ARGSUSED */
3430789Sahrens static int
3431789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3432789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3433789Sahrens {
3434789Sahrens 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
3435789Sahrens 	return (0);
3436789Sahrens }
3437789Sahrens 
34382688Smaybee /*
34392688Smaybee  * Push a page out to disk, klustering if possible.
34402688Smaybee  *
34412688Smaybee  *	IN:	vp	- file to push page to.
34422688Smaybee  *		pp	- page to push.
34432688Smaybee  *		flags	- additional flags.
34442688Smaybee  *		cr	- credentials of caller.
34452688Smaybee  *
34462688Smaybee  *	OUT:	offp	- start of range pushed.
34472688Smaybee  *		lenp	- len of range pushed.
34482688Smaybee  *
34492688Smaybee  *	RETURN:	0 if success
34502688Smaybee  *		error code if failure
34512688Smaybee  *
34522688Smaybee  * NOTE: callers must have locked the page to be pushed.  On
34532688Smaybee  * exit, the page (and all other pages in the kluster) must be
34542688Smaybee  * unlocked.
34552688Smaybee  */
3456789Sahrens /* ARGSUSED */
3457789Sahrens static int
3458789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3459789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3460789Sahrens {
3461789Sahrens 	znode_t		*zp = VTOZ(vp);
3462789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3463789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
3464789Sahrens 	dmu_tx_t	*tx;
34651669Sperrin 	rl_t		*rl;
34662688Smaybee 	u_offset_t	off, koff;
34672688Smaybee 	size_t		len, klen;
34684709Smaybee 	uint64_t	filesz;
3469789Sahrens 	int		err;
3470789Sahrens 
34714709Smaybee 	filesz = zp->z_phys->zp_size;
34722688Smaybee 	off = pp->p_offset;
34732688Smaybee 	len = PAGESIZE;
34742688Smaybee 	/*
34752688Smaybee 	 * If our blocksize is bigger than the page size, try to kluster
34762688Smaybee 	 * muiltiple pages so that we write a full block (thus avoiding
34772688Smaybee 	 * a read-modify-write).
34782688Smaybee 	 */
34794709Smaybee 	if (off < filesz && zp->z_blksz > PAGESIZE) {
34802688Smaybee 		if (!ISP2(zp->z_blksz)) {
34812688Smaybee 			/* Only one block in the file. */
34822688Smaybee 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
34832688Smaybee 			koff = 0;
34842688Smaybee 		} else {
34852688Smaybee 			klen = zp->z_blksz;
34862688Smaybee 			koff = P2ALIGN(off, (u_offset_t)klen);
34872688Smaybee 		}
34882688Smaybee 		ASSERT(koff <= filesz);
34892688Smaybee 		if (koff + klen > filesz)
34902688Smaybee 			klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE);
34912688Smaybee 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
34922688Smaybee 	}
34932688Smaybee 	ASSERT3U(btop(len), ==, btopr(len));
3494789Sahrens top:
34952688Smaybee 	rl = zfs_range_lock(zp, off, len, RL_WRITER);
34961819Smaybee 	/*
34971819Smaybee 	 * Can't push pages past end-of-file.
34981819Smaybee 	 */
34994709Smaybee 	filesz = zp->z_phys->zp_size;
35004709Smaybee 	if (off >= filesz) {
35014709Smaybee 		/* ignore all pages */
35022688Smaybee 		err = 0;
35032688Smaybee 		goto out;
35044709Smaybee 	} else if (off + len > filesz) {
35054709Smaybee 		int npages = btopr(filesz - off);
35062688Smaybee 		page_t *trunc;
35072688Smaybee 
35082688Smaybee 		page_list_break(&pp, &trunc, npages);
35094709Smaybee 		/* ignore pages past end of file */
35102688Smaybee 		if (trunc)
35114709Smaybee 			pvn_write_done(trunc, flags);
35124709Smaybee 		len = filesz - off;
35131819Smaybee 	}
3514789Sahrens 
3515789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3516789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, off, len);
3517789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
3518789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
3519789Sahrens 	if (err != 0) {
3520789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
35212688Smaybee 			zfs_range_unlock(rl);
35222113Sahrens 			dmu_tx_wait(tx);
35232113Sahrens 			dmu_tx_abort(tx);
35242688Smaybee 			err = 0;
3525789Sahrens 			goto top;
3526789Sahrens 		}
35272113Sahrens 		dmu_tx_abort(tx);
3528789Sahrens 		goto out;
3529789Sahrens 	}
3530789Sahrens 
35312688Smaybee 	if (zp->z_blksz <= PAGESIZE) {
35322688Smaybee 		caddr_t va = ppmapin(pp, PROT_READ, (caddr_t)-1);
35332688Smaybee 		ASSERT3U(len, <=, PAGESIZE);
35342688Smaybee 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
35352688Smaybee 		ppmapout(va);
35362688Smaybee 	} else {
35372688Smaybee 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
35382688Smaybee 	}
35392688Smaybee 
35402688Smaybee 	if (err == 0) {
35412688Smaybee 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
35423638Sbillm 		zfs_log_write(zilog, tx, TX_WRITE, zp, off, len, 0);
35432688Smaybee 		dmu_tx_commit(tx);
35442688Smaybee 	}
35452688Smaybee 
35462688Smaybee out:
35472237Smaybee 	zfs_range_unlock(rl);
35484709Smaybee 	pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
3549789Sahrens 	if (offp)
3550789Sahrens 		*offp = off;
3551789Sahrens 	if (lenp)
3552789Sahrens 		*lenp = len;
3553789Sahrens 
3554789Sahrens 	return (err);
3555789Sahrens }
3556789Sahrens 
3557789Sahrens /*
3558789Sahrens  * Copy the portion of the file indicated from pages into the file.
3559789Sahrens  * The pages are stored in a page list attached to the files vnode.
3560789Sahrens  *
3561789Sahrens  *	IN:	vp	- vnode of file to push page data to.
3562789Sahrens  *		off	- position in file to put data.
3563789Sahrens  *		len	- amount of data to write.
3564789Sahrens  *		flags	- flags to control the operation.
3565789Sahrens  *		cr	- credentials of caller.
35665331Samw  *		ct	- caller context.
3567789Sahrens  *
3568789Sahrens  *	RETURN:	0 if success
3569789Sahrens  *		error code if failure
3570789Sahrens  *
3571789Sahrens  * Timestamps:
3572789Sahrens  *	vp - ctime|mtime updated
3573789Sahrens  */
35745331Samw /*ARGSUSED*/
3575789Sahrens static int
35765331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
35775331Samw     caller_context_t *ct)
3578789Sahrens {
3579789Sahrens 	znode_t		*zp = VTOZ(vp);
3580789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3581789Sahrens 	page_t		*pp;
3582789Sahrens 	size_t		io_len;
3583789Sahrens 	u_offset_t	io_off;
35841669Sperrin 	uint64_t	filesz;
3585789Sahrens 	int		error = 0;
3586789Sahrens 
3587*5367Sahrens 	ZFS_ENTER(zfsvfs);
3588*5367Sahrens 	ZFS_VERIFY_ZP(zp);
3589789Sahrens 
3590789Sahrens 	if (len == 0) {
3591789Sahrens 		/*
3592789Sahrens 		 * Search the entire vp list for pages >= off.
3593789Sahrens 		 */
3594789Sahrens 		error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage,
3595789Sahrens 		    flags, cr);
35961472Sperrin 		goto out;
3597789Sahrens 	}
3598789Sahrens 
35991669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
36001669Sperrin 	if (off > filesz) {
3601789Sahrens 		/* past end of file */
3602789Sahrens 		ZFS_EXIT(zfsvfs);
3603789Sahrens 		return (0);
3604789Sahrens 	}
3605789Sahrens 
36061669Sperrin 	len = MIN(len, filesz - off);
3607789Sahrens 
36081472Sperrin 	for (io_off = off; io_off < off + len; io_off += io_len) {
3609789Sahrens 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
36101669Sperrin 			pp = page_lookup(vp, io_off,
36114339Sperrin 			    (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
3612789Sahrens 		} else {
3613789Sahrens 			pp = page_lookup_nowait(vp, io_off,
36144339Sperrin 			    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
3615789Sahrens 		}
3616789Sahrens 
3617789Sahrens 		if (pp != NULL && pvn_getdirty(pp, flags)) {
3618789Sahrens 			int err;
3619789Sahrens 
3620789Sahrens 			/*
3621789Sahrens 			 * Found a dirty page to push
3622789Sahrens 			 */
36231669Sperrin 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
36241669Sperrin 			if (err)
3625789Sahrens 				error = err;
3626789Sahrens 		} else {
3627789Sahrens 			io_len = PAGESIZE;
3628789Sahrens 		}
3629789Sahrens 	}
36301472Sperrin out:
36312638Sperrin 	if ((flags & B_ASYNC) == 0)
36322638Sperrin 		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
3633789Sahrens 	ZFS_EXIT(zfsvfs);
3634789Sahrens 	return (error);
3635789Sahrens }
3636789Sahrens 
36375331Samw /*ARGSUSED*/
3638789Sahrens void
36395331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
3640789Sahrens {
3641789Sahrens 	znode_t	*zp = VTOZ(vp);
3642789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3643789Sahrens 	int error;
3644789Sahrens 
36455326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
36465326Sek110237 	if (zp->z_dbuf_held == 0) {
3647789Sahrens 		if (vn_has_cached_data(vp)) {
3648789Sahrens 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
3649789Sahrens 			    B_INVAL, cr);
3650789Sahrens 		}
3651789Sahrens 
36521544Seschrock 		mutex_enter(&zp->z_lock);
3653789Sahrens 		vp->v_count = 0; /* count arrives as 1 */
36541544Seschrock 		if (zp->z_dbuf == NULL) {
36551544Seschrock 			mutex_exit(&zp->z_lock);
36561544Seschrock 			zfs_znode_free(zp);
36571544Seschrock 		} else {
36581544Seschrock 			mutex_exit(&zp->z_lock);
36591544Seschrock 		}
36605326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
3661789Sahrens 		VFS_RELE(zfsvfs->z_vfs);
3662789Sahrens 		return;
3663789Sahrens 	}
3664789Sahrens 
3665789Sahrens 	/*
3666789Sahrens 	 * Attempt to push any data in the page cache.  If this fails
3667789Sahrens 	 * we will get kicked out later in zfs_zinactive().
3668789Sahrens 	 */
36691298Sperrin 	if (vn_has_cached_data(vp)) {
36701298Sperrin 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
36711298Sperrin 		    cr);
36721298Sperrin 	}
3673789Sahrens 
36743461Sahrens 	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3675789Sahrens 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3676789Sahrens 
3677789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
3678789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
3679789Sahrens 		if (error) {
3680789Sahrens 			dmu_tx_abort(tx);
3681789Sahrens 		} else {
3682789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
3683789Sahrens 			mutex_enter(&zp->z_lock);
3684789Sahrens 			zp->z_atime_dirty = 0;
3685789Sahrens 			mutex_exit(&zp->z_lock);
3686789Sahrens 			dmu_tx_commit(tx);
3687789Sahrens 		}
3688789Sahrens 	}
3689789Sahrens 
3690789Sahrens 	zfs_zinactive(zp);
36915326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
3692789Sahrens }
3693789Sahrens 
3694789Sahrens /*
3695789Sahrens  * Bounds-check the seek operation.
3696789Sahrens  *
3697789Sahrens  *	IN:	vp	- vnode seeking within
3698789Sahrens  *		ooff	- old file offset
3699789Sahrens  *		noffp	- pointer to new file offset
37005331Samw  *		ct	- caller context
3701789Sahrens  *
3702789Sahrens  *	RETURN:	0 if success
3703789Sahrens  *		EINVAL if new offset invalid
3704789Sahrens  */
3705789Sahrens /* ARGSUSED */
3706789Sahrens static int
37075331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
37085331Samw     caller_context_t *ct)
3709789Sahrens {
3710789Sahrens 	if (vp->v_type == VDIR)
3711789Sahrens 		return (0);
3712789Sahrens 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
3713789Sahrens }
3714789Sahrens 
3715789Sahrens /*
3716789Sahrens  * Pre-filter the generic locking function to trap attempts to place
3717789Sahrens  * a mandatory lock on a memory mapped file.
3718789Sahrens  */
3719789Sahrens static int
3720789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
37215331Samw     flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
3722789Sahrens {
3723789Sahrens 	znode_t *zp = VTOZ(vp);
3724789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3725789Sahrens 	int error;
3726789Sahrens 
3727*5367Sahrens 	ZFS_ENTER(zfsvfs);
3728*5367Sahrens 	ZFS_VERIFY_ZP(zp);
3729789Sahrens 
3730789Sahrens 	/*
37311544Seschrock 	 * We are following the UFS semantics with respect to mapcnt
37321544Seschrock 	 * here: If we see that the file is mapped already, then we will
37331544Seschrock 	 * return an error, but we don't worry about races between this
37341544Seschrock 	 * function and zfs_map().
3735789Sahrens 	 */
37361544Seschrock 	if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) {
3737789Sahrens 		ZFS_EXIT(zfsvfs);
3738789Sahrens 		return (EAGAIN);
3739789Sahrens 	}
37405331Samw 	error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct);
3741789Sahrens 	ZFS_EXIT(zfsvfs);
3742789Sahrens 	return (error);
3743789Sahrens }
3744789Sahrens 
3745789Sahrens /*
3746789Sahrens  * If we can't find a page in the cache, we will create a new page
3747789Sahrens  * and fill it with file data.  For efficiency, we may try to fill
37481669Sperrin  * multiple pages at once (klustering).
3749789Sahrens  */
3750789Sahrens static int
3751789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
3752789Sahrens     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
3753789Sahrens {
3754789Sahrens 	znode_t *zp = VTOZ(vp);
3755789Sahrens 	page_t *pp, *cur_pp;
3756789Sahrens 	objset_t *os = zp->z_zfsvfs->z_os;
3757789Sahrens 	caddr_t va;
3758789Sahrens 	u_offset_t io_off, total;
3759789Sahrens 	uint64_t oid = zp->z_id;
3760789Sahrens 	size_t io_len;
37611669Sperrin 	uint64_t filesz;
3762789Sahrens 	int err;
3763789Sahrens 
3764789Sahrens 	/*
3765789Sahrens 	 * If we are only asking for a single page don't bother klustering.
3766789Sahrens 	 */
37671669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
37682688Smaybee 	if (off >= filesz)
37692688Smaybee 		return (EFAULT);
37702688Smaybee 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
3771789Sahrens 		io_off = off;
3772789Sahrens 		io_len = PAGESIZE;
3773789Sahrens 		pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr);
3774789Sahrens 	} else {
3775789Sahrens 		/*
3776789Sahrens 		 * Try to fill a kluster of pages (a blocks worth).
3777789Sahrens 		 */
3778789Sahrens 		size_t klen;
3779789Sahrens 		u_offset_t koff;
3780789Sahrens 
3781789Sahrens 		if (!ISP2(zp->z_blksz)) {
3782789Sahrens 			/* Only one block in the file. */
3783789Sahrens 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
3784789Sahrens 			koff = 0;
3785789Sahrens 		} else {
37863131Sgw25295 			/*
37873131Sgw25295 			 * It would be ideal to align our offset to the
37883131Sgw25295 			 * blocksize but doing so has resulted in some
37893131Sgw25295 			 * strange application crashes. For now, we
37903131Sgw25295 			 * leave the offset as is and only adjust the
37913131Sgw25295 			 * length if we are off the end of the file.
37923131Sgw25295 			 */
37933131Sgw25295 			koff = off;
3794789Sahrens 			klen = plsz;
3795789Sahrens 		}
37961819Smaybee 		ASSERT(koff <= filesz);
37971819Smaybee 		if (koff + klen > filesz)
37981819Smaybee 			klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff;
37992688Smaybee 		ASSERT3U(off, >=, koff);
38002688Smaybee 		ASSERT3U(off, <, koff + klen);
3801789Sahrens 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
38024339Sperrin 		    &io_len, koff, klen, 0);
3803789Sahrens 	}
3804789Sahrens 	if (pp == NULL) {
3805789Sahrens 		/*
3806789Sahrens 		 * Some other thread entered the page before us.
3807789Sahrens 		 * Return to zfs_getpage to retry the lookup.
3808789Sahrens 		 */
3809789Sahrens 		*pl = NULL;
3810789Sahrens 		return (0);
3811789Sahrens 	}
3812789Sahrens 
3813789Sahrens 	/*
3814789Sahrens 	 * Fill the pages in the kluster.
3815789Sahrens 	 */
3816789Sahrens 	cur_pp = pp;
3817789Sahrens 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
38182688Smaybee 		ASSERT3U(io_off, ==, cur_pp->p_offset);
3819789Sahrens 		va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1);
38201544Seschrock 		err = dmu_read(os, oid, io_off, PAGESIZE, va);
3821789Sahrens 		ppmapout(va);
3822789Sahrens 		if (err) {
3823789Sahrens 			/* On error, toss the entire kluster */
3824789Sahrens 			pvn_read_done(pp, B_ERROR);
3825789Sahrens 			return (err);
3826789Sahrens 		}
3827789Sahrens 		cur_pp = cur_pp->p_next;
3828789Sahrens 	}
3829789Sahrens out:
3830789Sahrens 	/*
3831789Sahrens 	 * Fill in the page list array from the kluster.  If
3832789Sahrens 	 * there are too many pages in the kluster, return
3833789Sahrens 	 * as many pages as possible starting from the desired
3834789Sahrens 	 * offset `off'.
3835789Sahrens 	 * NOTE: the page list will always be null terminated.
3836789Sahrens 	 */
3837789Sahrens 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
3838789Sahrens 
3839789Sahrens 	return (0);
3840789Sahrens }
3841789Sahrens 
3842789Sahrens /*
3843789Sahrens  * Return pointers to the pages for the file region [off, off + len]
3844789Sahrens  * in the pl array.  If plsz is greater than len, this function may
3845789Sahrens  * also return page pointers from before or after the specified
3846789Sahrens  * region (i.e. some region [off', off' + plsz]).  These additional
3847789Sahrens  * pages are only returned if they are already in the cache, or were
3848789Sahrens  * created as part of a klustered read.
3849789Sahrens  *
3850789Sahrens  *	IN:	vp	- vnode of file to get data from.
3851789Sahrens  *		off	- position in file to get data from.
3852789Sahrens  *		len	- amount of data to retrieve.
3853789Sahrens  *		plsz	- length of provided page list.
3854789Sahrens  *		seg	- segment to obtain pages for.
3855789Sahrens  *		addr	- virtual address of fault.
3856789Sahrens  *		rw	- mode of created pages.
3857789Sahrens  *		cr	- credentials of caller.
38585331Samw  *		ct	- caller context.
3859789Sahrens  *
3860789Sahrens  *	OUT:	protp	- protection mode of created pages.
3861789Sahrens  *		pl	- list of pages created.
3862789Sahrens  *
3863789Sahrens  *	RETURN:	0 if success
3864789Sahrens  *		error code if failure
3865789Sahrens  *
3866789Sahrens  * Timestamps:
3867789Sahrens  *	vp - atime updated
3868789Sahrens  */
3869789Sahrens /* ARGSUSED */
3870789Sahrens static int
3871789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
3872789Sahrens 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
38735331Samw 	enum seg_rw rw, cred_t *cr, caller_context_t *ct)
3874789Sahrens {
3875789Sahrens 	znode_t		*zp = VTOZ(vp);
3876789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3877789Sahrens 	page_t		*pp, **pl0 = pl;
38782752Sperrin 	int		need_unlock = 0, err = 0;
38792752Sperrin 	offset_t	orig_off;
3880789Sahrens 
3881*5367Sahrens 	ZFS_ENTER(zfsvfs);
3882*5367Sahrens 	ZFS_VERIFY_ZP(zp);
3883789Sahrens 
3884789Sahrens 	if (protp)
3885789Sahrens 		*protp = PROT_ALL;
3886789Sahrens 
3887789Sahrens 	/* no faultahead (for now) */
3888789Sahrens 	if (pl == NULL) {
3889789Sahrens 		ZFS_EXIT(zfsvfs);
3890789Sahrens 		return (0);
3891789Sahrens 	}
3892789Sahrens 
3893789Sahrens 	/* can't fault past EOF */
3894789Sahrens 	if (off >= zp->z_phys->zp_size) {
3895789Sahrens 		ZFS_EXIT(zfsvfs);
3896789Sahrens 		return (EFAULT);
3897789Sahrens 	}
38982752Sperrin 	orig_off = off;
3899789Sahrens 
3900789Sahrens 	/*
3901789Sahrens 	 * If we already own the lock, then we must be page faulting
3902789Sahrens 	 * in the middle of a write to this file (i.e., we are writing
3903789Sahrens 	 * to this file using data from a mapped region of the file).
3904789Sahrens 	 */
39052752Sperrin 	if (rw_owner(&zp->z_map_lock) != curthread) {
3906789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
3907789Sahrens 		need_unlock = TRUE;
3908789Sahrens 	}
3909789Sahrens 
3910789Sahrens 	/*
3911789Sahrens 	 * Loop through the requested range [off, off + len] looking
3912789Sahrens 	 * for pages.  If we don't find a page, we will need to create
3913789Sahrens 	 * a new page and fill it with data from the file.
3914789Sahrens 	 */
3915789Sahrens 	while (len > 0) {
3916789Sahrens 		if (plsz < PAGESIZE)
3917789Sahrens 			break;
3918789Sahrens 		if (pp = page_lookup(vp, off, SE_SHARED)) {
3919789Sahrens 			*pl++ = pp;
3920789Sahrens 			off += PAGESIZE;
3921789Sahrens 			addr += PAGESIZE;
3922789Sahrens 			len -= PAGESIZE;
3923789Sahrens 			plsz -= PAGESIZE;
3924789Sahrens 		} else {
3925789Sahrens 			err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw);
39262752Sperrin 			if (err)
39272752Sperrin 				goto out;
3928789Sahrens 			/*
3929789Sahrens 			 * klustering may have changed our region
3930789Sahrens 			 * to be block aligned.
3931789Sahrens 			 */
3932789Sahrens 			if (((pp = *pl) != 0) && (off != pp->p_offset)) {
3933789Sahrens 				int delta = off - pp->p_offset;
3934789Sahrens 				len += delta;
3935789Sahrens 				off -= delta;
3936789Sahrens 				addr -= delta;
3937789Sahrens 			}
3938789Sahrens 			while (*pl) {
3939789Sahrens 				pl++;
3940789Sahrens 				off += PAGESIZE;
3941789Sahrens 				addr += PAGESIZE;
3942789Sahrens 				plsz -= PAGESIZE;
3943789Sahrens 				if (len > PAGESIZE)
3944789Sahrens 					len -= PAGESIZE;
3945789Sahrens 				else
3946789Sahrens 					len = 0;
3947789Sahrens 			}
3948789Sahrens 		}
3949789Sahrens 	}
3950789Sahrens 
3951789Sahrens 	/*
3952789Sahrens 	 * Fill out the page array with any pages already in the cache.
3953789Sahrens 	 */
3954789Sahrens 	while (plsz > 0) {
3955789Sahrens 		pp = page_lookup_nowait(vp, off, SE_SHARED);
3956789Sahrens 		if (pp == NULL)
3957789Sahrens 			break;
3958789Sahrens 		*pl++ = pp;
3959789Sahrens 		off += PAGESIZE;
3960789Sahrens 		plsz -= PAGESIZE;
3961789Sahrens 	}
3962789Sahrens 
3963789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3964789Sahrens out:
39652752Sperrin 	/*
39662752Sperrin 	 * We can't grab the range lock for the page as reader which would
39672752Sperrin 	 * stop truncation as this leads to deadlock. So we need to recheck
39682752Sperrin 	 * the file size.
39692752Sperrin 	 */
39702752Sperrin 	if (orig_off >= zp->z_phys->zp_size)
39712752Sperrin 		err = EFAULT;
39722752Sperrin 	if (err) {
39732752Sperrin 		/*
39742752Sperrin 		 * Release any pages we have previously locked.
39752752Sperrin 		 */
39762752Sperrin 		while (pl > pl0)
39772752Sperrin 			page_unlock(*--pl);
39782752Sperrin 	}
39792752Sperrin 
3980789Sahrens 	*pl = NULL;
3981789Sahrens 
3982789Sahrens 	if (need_unlock)
3983789Sahrens 		rw_exit(&zp->z_map_lock);
3984789Sahrens 
3985789Sahrens 	ZFS_EXIT(zfsvfs);
3986789Sahrens 	return (err);
3987789Sahrens }
3988789Sahrens 
39891544Seschrock /*
39901544Seschrock  * Request a memory map for a section of a file.  This code interacts
39911544Seschrock  * with common code and the VM system as follows:
39921544Seschrock  *
39931544Seschrock  *	common code calls mmap(), which ends up in smmap_common()
39941544Seschrock  *
39951544Seschrock  *	this calls VOP_MAP(), which takes you into (say) zfs
39961544Seschrock  *
39971544Seschrock  *	zfs_map() calls as_map(), passing segvn_create() as the callback
39981544Seschrock  *
39991544Seschrock  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
40001544Seschrock  *
40011544Seschrock  *	zfs_addmap() updates z_mapcnt
40021544Seschrock  */
40035331Samw /*ARGSUSED*/
4004789Sahrens static int
4005789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
40065331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
40075331Samw     caller_context_t *ct)
4008789Sahrens {
4009789Sahrens 	znode_t *zp = VTOZ(vp);
4010789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4011789Sahrens 	segvn_crargs_t	vn_a;
4012789Sahrens 	int		error;
4013789Sahrens 
40145331Samw 	if ((prot & PROT_WRITE) &&
40155331Samw 	    (zp->z_phys->zp_flags & (ZFS_IMMUTABLE | ZFS_READONLY |
40165331Samw 	    ZFS_APPENDONLY)))
40175331Samw 		return (EPERM);
40185331Samw 
4019*5367Sahrens 	ZFS_ENTER(zfsvfs);
4020*5367Sahrens 	ZFS_VERIFY_ZP(zp);
4021789Sahrens 
4022789Sahrens 	if (vp->v_flag & VNOMAP) {
4023789Sahrens 		ZFS_EXIT(zfsvfs);
4024789Sahrens 		return (ENOSYS);
4025789Sahrens 	}
4026789Sahrens 
4027789Sahrens 	if (off < 0 || len > MAXOFFSET_T - off) {
4028789Sahrens 		ZFS_EXIT(zfsvfs);
4029789Sahrens 		return (ENXIO);
4030789Sahrens 	}
4031789Sahrens 
4032789Sahrens 	if (vp->v_type != VREG) {
4033789Sahrens 		ZFS_EXIT(zfsvfs);
4034789Sahrens 		return (ENODEV);
4035789Sahrens 	}
4036789Sahrens 
4037789Sahrens 	/*
4038789Sahrens 	 * If file is locked, disallow mapping.
4039789Sahrens 	 */
40401544Seschrock 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) {
40411544Seschrock 		ZFS_EXIT(zfsvfs);
40421544Seschrock 		return (EAGAIN);
4043789Sahrens 	}
4044789Sahrens 
4045789Sahrens 	as_rangelock(as);
4046789Sahrens 	if ((flags & MAP_FIXED) == 0) {
4047789Sahrens 		map_addr(addrp, len, off, 1, flags);
4048789Sahrens 		if (*addrp == NULL) {
4049789Sahrens 			as_rangeunlock(as);
4050789Sahrens 			ZFS_EXIT(zfsvfs);
4051789Sahrens 			return (ENOMEM);
4052789Sahrens 		}
4053789Sahrens 	} else {
4054789Sahrens 		/*
4055789Sahrens 		 * User specified address - blow away any previous mappings
4056789Sahrens 		 */
4057789Sahrens 		(void) as_unmap(as, *addrp, len);
4058789Sahrens 	}
4059789Sahrens 
4060789Sahrens 	vn_a.vp = vp;
4061789Sahrens 	vn_a.offset = (u_offset_t)off;
4062789Sahrens 	vn_a.type = flags & MAP_TYPE;
4063789Sahrens 	vn_a.prot = prot;
4064789Sahrens 	vn_a.maxprot = maxprot;
4065789Sahrens 	vn_a.cred = cr;
4066789Sahrens 	vn_a.amp = NULL;
4067789Sahrens 	vn_a.flags = flags & ~MAP_TYPE;
40681417Skchow 	vn_a.szc = 0;
40691417Skchow 	vn_a.lgrp_mem_policy_flags = 0;
4070789Sahrens 
4071789Sahrens 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
4072789Sahrens 
4073789Sahrens 	as_rangeunlock(as);
4074789Sahrens 	ZFS_EXIT(zfsvfs);
4075789Sahrens 	return (error);
4076789Sahrens }
4077789Sahrens 
4078789Sahrens /* ARGSUSED */
4079789Sahrens static int
4080789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
40815331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
40825331Samw     caller_context_t *ct)
4083789Sahrens {
40841544Seschrock 	uint64_t pages = btopr(len);
40851544Seschrock 
40861544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4087789Sahrens 	return (0);
4088789Sahrens }
4089789Sahrens 
40901773Seschrock /*
40911773Seschrock  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
40921773Seschrock  * more accurate mtime for the associated file.  Since we don't have a way of
40931773Seschrock  * detecting when the data was actually modified, we have to resort to
40941773Seschrock  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
40951773Seschrock  * last page is pushed.  The problem occurs when the msync() call is omitted,
40961773Seschrock  * which by far the most common case:
40971773Seschrock  *
40981773Seschrock  * 	open()
40991773Seschrock  * 	mmap()
41001773Seschrock  * 	<modify memory>
41011773Seschrock  * 	munmap()
41021773Seschrock  * 	close()
41031773Seschrock  * 	<time lapse>
41041773Seschrock  * 	putpage() via fsflush
41051773Seschrock  *
41061773Seschrock  * If we wait until fsflush to come along, we can have a modification time that
41071773Seschrock  * is some arbitrary point in the future.  In order to prevent this in the
41081773Seschrock  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
41091773Seschrock  * torn down.
41101773Seschrock  */
4111789Sahrens /* ARGSUSED */
4112789Sahrens static int
4113789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
41145331Samw     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
41155331Samw     caller_context_t *ct)
4116789Sahrens {
41171544Seschrock 	uint64_t pages = btopr(len);
41181544Seschrock 
41191544Seschrock 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
41201544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
41211773Seschrock 
41221773Seschrock 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
41231773Seschrock 	    vn_has_cached_data(vp))
41245331Samw 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
41251773Seschrock 
4126789Sahrens 	return (0);
4127789Sahrens }
4128789Sahrens 
4129789Sahrens /*
4130789Sahrens  * Free or allocate space in a file.  Currently, this function only
4131789Sahrens  * supports the `F_FREESP' command.  However, this command is somewhat
4132789Sahrens  * misnamed, as its functionality includes the ability to allocate as
4133789Sahrens  * well as free space.
4134789Sahrens  *
4135789Sahrens  *	IN:	vp	- vnode of file to free data in.
4136789Sahrens  *		cmd	- action to take (only F_FREESP supported).
4137789Sahrens  *		bfp	- section of file to free/alloc.
4138789Sahrens  *		flag	- current file open mode flags.
4139789Sahrens  *		offset	- current file offset.
4140789Sahrens  *		cr	- credentials of caller [UNUSED].
41415331Samw  *		ct	- caller context.
4142789Sahrens  *
4143789Sahrens  *	RETURN:	0 if success
4144789Sahrens  *		error code if failure
4145789Sahrens  *
4146789Sahrens  * Timestamps:
4147789Sahrens  *	vp - ctime|mtime updated
4148789Sahrens  */
4149789Sahrens /* ARGSUSED */
4150789Sahrens static int
4151789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4152789Sahrens     offset_t offset, cred_t *cr, caller_context_t *ct)
4153789Sahrens {
4154789Sahrens 	znode_t		*zp = VTOZ(vp);
4155789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4156789Sahrens 	uint64_t	off, len;
4157789Sahrens 	int		error;
4158789Sahrens 
4159*5367Sahrens 	ZFS_ENTER(zfsvfs);
4160*5367Sahrens 	ZFS_VERIFY_ZP(zp);
4161789Sahrens 
4162789Sahrens top:
4163789Sahrens 	if (cmd != F_FREESP) {
4164789Sahrens 		ZFS_EXIT(zfsvfs);
4165789Sahrens 		return (EINVAL);
4166789Sahrens 	}
4167789Sahrens 
4168789Sahrens 	if (error = convoff(vp, bfp, 0, offset)) {
4169789Sahrens 		ZFS_EXIT(zfsvfs);
4170789Sahrens 		return (error);
4171789Sahrens 	}
4172789Sahrens 
4173789Sahrens 	if (bfp->l_len < 0) {
4174789Sahrens 		ZFS_EXIT(zfsvfs);
4175789Sahrens 		return (EINVAL);
4176789Sahrens 	}
4177789Sahrens 
4178789Sahrens 	off = bfp->l_start;
41791669Sperrin 	len = bfp->l_len; /* 0 means from off to end of file */
41801878Smaybee 
41811878Smaybee 	do {
41821878Smaybee 		error = zfs_freesp(zp, off, len, flag, TRUE);
41832113Sahrens 		/* NB: we already did dmu_tx_wait() if necessary */
41841878Smaybee 	} while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
4185789Sahrens 
4186789Sahrens 	ZFS_EXIT(zfsvfs);
4187789Sahrens 	return (error);
4188789Sahrens }
4189789Sahrens 
41905331Samw /*ARGSUSED*/
4191789Sahrens static int
41925331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4193789Sahrens {
4194789Sahrens 	znode_t		*zp = VTOZ(vp);
4195789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
41965326Sek110237 	uint32_t	gen;
4197789Sahrens 	uint64_t	object = zp->z_id;
4198789Sahrens 	zfid_short_t	*zfid;
4199789Sahrens 	int		size, i;
4200789Sahrens 
4201*5367Sahrens 	ZFS_ENTER(zfsvfs);
4202*5367Sahrens 	ZFS_VERIFY_ZP(zp);
42035326Sek110237 	gen = (uint32_t)zp->z_gen;
4204789Sahrens 
4205789Sahrens 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4206789Sahrens 	if (fidp->fid_len < size) {
4207789Sahrens 		fidp->fid_len = size;
42081512Sek110237 		ZFS_EXIT(zfsvfs);
4209789Sahrens 		return (ENOSPC);
4210789Sahrens 	}
4211789Sahrens 
4212789Sahrens 	zfid = (zfid_short_t *)fidp;
4213789Sahrens 
4214789Sahrens 	zfid->zf_len = size;
4215789Sahrens 
4216789Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
4217789Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4218789Sahrens 
4219789Sahrens 	/* Must have a non-zero generation number to distinguish from .zfs */
4220789Sahrens 	if (gen == 0)
4221789Sahrens 		gen = 1;
4222789Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
4223789Sahrens 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4224789Sahrens 
4225789Sahrens 	if (size == LONG_FID_LEN) {
4226789Sahrens 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
4227789Sahrens 		zfid_long_t	*zlfid;
4228789Sahrens 
4229789Sahrens 		zlfid = (zfid_long_t *)fidp;
4230789Sahrens 
4231789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4232789Sahrens 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4233789Sahrens 
4234789Sahrens 		/* XXX - this should be the generation number for the objset */
4235789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4236789Sahrens 			zlfid->zf_setgen[i] = 0;
4237789Sahrens 	}
4238789Sahrens 
4239789Sahrens 	ZFS_EXIT(zfsvfs);
4240789Sahrens 	return (0);
4241789Sahrens }
4242789Sahrens 
4243789Sahrens static int
42445331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
42455331Samw     caller_context_t *ct)
4246789Sahrens {
4247789Sahrens 	znode_t		*zp, *xzp;
4248789Sahrens 	zfsvfs_t	*zfsvfs;
4249789Sahrens 	zfs_dirlock_t	*dl;
4250789Sahrens 	int		error;
4251789Sahrens 
4252789Sahrens 	switch (cmd) {
4253789Sahrens 	case _PC_LINK_MAX:
4254789Sahrens 		*valp = ULONG_MAX;
4255789Sahrens 		return (0);
4256789Sahrens 
4257789Sahrens 	case _PC_FILESIZEBITS:
4258789Sahrens 		*valp = 64;
4259789Sahrens 		return (0);
4260789Sahrens 
4261789Sahrens 	case _PC_XATTR_EXISTS:
4262789Sahrens 		zp = VTOZ(vp);
4263789Sahrens 		zfsvfs = zp->z_zfsvfs;
4264*5367Sahrens 		ZFS_ENTER(zfsvfs);
4265*5367Sahrens 		ZFS_VERIFY_ZP(zp);
4266789Sahrens 		*valp = 0;
4267789Sahrens 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
42685331Samw 		    ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4269789Sahrens 		if (error == 0) {
4270789Sahrens 			zfs_dirent_unlock(dl);
4271789Sahrens 			if (!zfs_dirempty(xzp))
4272789Sahrens 				*valp = 1;
4273789Sahrens 			VN_RELE(ZTOV(xzp));
4274789Sahrens 		} else if (error == ENOENT) {
4275789Sahrens 			/*
4276789Sahrens 			 * If there aren't extended attributes, it's the
4277789Sahrens 			 * same as having zero of them.
4278789Sahrens 			 */
4279789Sahrens 			error = 0;
4280789Sahrens 		}
4281789Sahrens 		ZFS_EXIT(zfsvfs);
4282789Sahrens 		return (error);
4283789Sahrens 
42845331Samw 	case _PC_SATTR_ENABLED:
42855331Samw 	case _PC_SATTR_EXISTS:
42865331Samw 		zp = VTOZ(vp);
42875331Samw 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) &&
42885331Samw 		    (vp->v_type == VREG || vp->v_type == VDIR);
42895331Samw 		return (0);
42905331Samw 
4291789Sahrens 	case _PC_ACL_ENABLED:
4292789Sahrens 		*valp = _ACL_ACE_ENABLED;
4293789Sahrens 		return (0);
4294789Sahrens 
4295789Sahrens 	case _PC_MIN_HOLE_SIZE:
4296789Sahrens 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
4297789Sahrens 		return (0);
4298789Sahrens 
4299789Sahrens 	default:
43005331Samw 		return (fs_pathconf(vp, cmd, valp, cr, ct));
4301789Sahrens 	}
4302789Sahrens }
4303789Sahrens 
4304789Sahrens /*ARGSUSED*/
4305789Sahrens static int
43065331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
43075331Samw     caller_context_t *ct)
4308789Sahrens {
4309789Sahrens 	znode_t *zp = VTOZ(vp);
4310789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4311789Sahrens 	int error;
43125331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4313789Sahrens 
4314*5367Sahrens 	ZFS_ENTER(zfsvfs);
4315*5367Sahrens 	ZFS_VERIFY_ZP(zp);
43165331Samw 	error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4317789Sahrens 	ZFS_EXIT(zfsvfs);
4318789Sahrens 
4319789Sahrens 	return (error);
4320789Sahrens }
4321789Sahrens 
4322789Sahrens /*ARGSUSED*/
4323789Sahrens static int
43245331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
43255331Samw     caller_context_t *ct)
4326789Sahrens {
4327789Sahrens 	znode_t *zp = VTOZ(vp);
4328789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4329789Sahrens 	int error;
43305331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4331789Sahrens 
4332*5367Sahrens 	ZFS_ENTER(zfsvfs);
4333*5367Sahrens 	ZFS_VERIFY_ZP(zp);
43345331Samw 	error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4335789Sahrens 	ZFS_EXIT(zfsvfs);
4336789Sahrens 	return (error);
4337789Sahrens }
4338789Sahrens 
4339789Sahrens /*
4340789Sahrens  * Predeclare these here so that the compiler assumes that
4341789Sahrens  * this is an "old style" function declaration that does
4342789Sahrens  * not include arguments => we won't get type mismatch errors
4343789Sahrens  * in the initializations that follow.
4344789Sahrens  */
4345789Sahrens static int zfs_inval();
4346789Sahrens static int zfs_isdir();
4347789Sahrens 
4348789Sahrens static int
4349789Sahrens zfs_inval()
4350789Sahrens {
4351789Sahrens 	return (EINVAL);
4352789Sahrens }
4353789Sahrens 
4354789Sahrens static int
4355789Sahrens zfs_isdir()
4356789Sahrens {
4357789Sahrens 	return (EISDIR);
4358789Sahrens }
4359789Sahrens /*
4360789Sahrens  * Directory vnode operations template
4361789Sahrens  */
4362789Sahrens vnodeops_t *zfs_dvnodeops;
4363789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = {
43643898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
43653898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
43663898Srsb 	VOPNAME_READ,		{ .error = zfs_isdir },
43673898Srsb 	VOPNAME_WRITE,		{ .error = zfs_isdir },
43683898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
43693898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
43703898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
43713898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
43723898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
43733898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
43743898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
43753898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
43763898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
43773898Srsb 	VOPNAME_MKDIR,		{ .vop_mkdir = zfs_mkdir },
43783898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
43793898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
43803898Srsb 	VOPNAME_SYMLINK,	{ .vop_symlink = zfs_symlink },
43813898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
43823898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
43833898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
43843898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
43853898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
43863898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
43873898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
43884863Spraks 	VOPNAME_VNEVENT, 	{ .vop_vnevent = fs_vnevent_support },
43893898Srsb 	NULL,			NULL
4390789Sahrens };
4391789Sahrens 
4392789Sahrens /*
4393789Sahrens  * Regular file vnode operations template
4394789Sahrens  */
4395789Sahrens vnodeops_t *zfs_fvnodeops;
4396789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = {
43973898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
43983898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
43993898Srsb 	VOPNAME_READ,		{ .vop_read = zfs_read },
44003898Srsb 	VOPNAME_WRITE,		{ .vop_write = zfs_write },
44013898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
44023898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
44033898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
44043898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
44053898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
44063898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
44073898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
44083898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
44093898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
44103898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
44113898Srsb 	VOPNAME_FRLOCK,		{ .vop_frlock = zfs_frlock },
44123898Srsb 	VOPNAME_SPACE,		{ .vop_space = zfs_space },
44133898Srsb 	VOPNAME_GETPAGE,	{ .vop_getpage = zfs_getpage },
44143898Srsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = zfs_putpage },
44153898Srsb 	VOPNAME_MAP,		{ .vop_map = zfs_map },
44163898Srsb 	VOPNAME_ADDMAP,		{ .vop_addmap = zfs_addmap },
44173898Srsb 	VOPNAME_DELMAP,		{ .vop_delmap = zfs_delmap },
44183898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
44193898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
44203898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
44213898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
44223898Srsb 	NULL,			NULL
4423789Sahrens };
4424789Sahrens 
4425789Sahrens /*
4426789Sahrens  * Symbolic link vnode operations template
4427789Sahrens  */
4428789Sahrens vnodeops_t *zfs_symvnodeops;
4429789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = {
44303898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
44313898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
44323898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
44333898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
44343898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = zfs_readlink },
44353898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
44363898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
44373898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
44383898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
44393898Srsb 	NULL,			NULL
4440789Sahrens };
4441789Sahrens 
4442789Sahrens /*
4443789Sahrens  * Extended attribute directory vnode operations template
4444789Sahrens  *	This template is identical to the directory vnodes
4445789Sahrens  *	operation template except for restricted operations:
4446789Sahrens  *		VOP_MKDIR()
4447789Sahrens  *		VOP_SYMLINK()
4448789Sahrens  * Note that there are other restrictions embedded in:
4449789Sahrens  *	zfs_create()	- restrict type to VREG
4450789Sahrens  *	zfs_link()	- no links into/out of attribute space
4451789Sahrens  *	zfs_rename()	- no moves into/out of attribute space
4452789Sahrens  */
4453789Sahrens vnodeops_t *zfs_xdvnodeops;
4454789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = {
44553898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
44563898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
44573898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
44583898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
44593898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
44603898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
44613898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
44623898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
44633898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
44643898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
44653898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
44663898Srsb 	VOPNAME_MKDIR,		{ .error = zfs_inval },
44673898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
44683898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
44693898Srsb 	VOPNAME_SYMLINK,	{ .error = zfs_inval },
44703898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
44713898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
44723898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
44733898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
44743898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
44753898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
44763898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
44773898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
44783898Srsb 	NULL,			NULL
4479789Sahrens };
4480789Sahrens 
4481789Sahrens /*
4482789Sahrens  * Error vnode operations template
4483789Sahrens  */
4484789Sahrens vnodeops_t *zfs_evnodeops;
4485789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = {
44863898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
44873898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
44883898Srsb 	NULL,			NULL
4489789Sahrens };
4490