xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 5771:7ba3a2c57d6a)
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 /*
22*5771Sjp151216  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
264144Speteh /* Portions Copyright 2007 Jeremy Teo */
274144Speteh 
28789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
29789Sahrens 
30789Sahrens #include <sys/types.h>
31789Sahrens #include <sys/param.h>
32789Sahrens #include <sys/time.h>
33789Sahrens #include <sys/systm.h>
34789Sahrens #include <sys/sysmacros.h>
35789Sahrens #include <sys/resource.h>
36789Sahrens #include <sys/vfs.h>
373898Srsb #include <sys/vfs_opreg.h>
38789Sahrens #include <sys/vnode.h>
39789Sahrens #include <sys/file.h>
40789Sahrens #include <sys/stat.h>
41789Sahrens #include <sys/kmem.h>
42789Sahrens #include <sys/taskq.h>
43789Sahrens #include <sys/uio.h>
44789Sahrens #include <sys/vmsystm.h>
45789Sahrens #include <sys/atomic.h>
462688Smaybee #include <sys/vm.h>
47789Sahrens #include <vm/seg_vn.h>
48789Sahrens #include <vm/pvn.h>
49789Sahrens #include <vm/as.h>
50789Sahrens #include <sys/mman.h>
51789Sahrens #include <sys/pathname.h>
52789Sahrens #include <sys/cmn_err.h>
53789Sahrens #include <sys/errno.h>
54789Sahrens #include <sys/unistd.h>
55789Sahrens #include <sys/zfs_dir.h>
56789Sahrens #include <sys/zfs_acl.h>
57789Sahrens #include <sys/zfs_ioctl.h>
58789Sahrens #include <sys/fs/zfs.h>
59789Sahrens #include <sys/dmu.h>
60789Sahrens #include <sys/spa.h>
61789Sahrens #include <sys/txg.h>
62789Sahrens #include <sys/dbuf.h>
63789Sahrens #include <sys/zap.h>
64789Sahrens #include <sys/dirent.h>
65789Sahrens #include <sys/policy.h>
66789Sahrens #include <sys/sunddi.h>
67789Sahrens #include <sys/filio.h>
68789Sahrens #include "fs/fs_subr.h"
69789Sahrens #include <sys/zfs_ctldir.h>
705331Samw #include <sys/zfs_fuid.h>
711484Sek110237 #include <sys/dnlc.h>
721669Sperrin #include <sys/zfs_rlock.h>
735331Samw #include <sys/extdirent.h>
745331Samw #include <sys/kidmap.h>
755331Samw #include <sys/cred_impl.h>
765663Sck153898 #include <sys/attr.h>
77789Sahrens 
78789Sahrens /*
79789Sahrens  * Programming rules.
80789Sahrens  *
81789Sahrens  * Each vnode op performs some logical unit of work.  To do this, the ZPL must
82789Sahrens  * properly lock its in-core state, create a DMU transaction, do the work,
83789Sahrens  * record this work in the intent log (ZIL), commit the DMU transaction,
845331Samw  * and wait for the intent log to commit if it is a synchronous operation.
855331Samw  * Moreover, the vnode ops must work in both normal and log replay context.
86789Sahrens  * The ordering of events is important to avoid deadlocks and references
87789Sahrens  * to freed memory.  The example below illustrates the following Big Rules:
88789Sahrens  *
89789Sahrens  *  (1) A check must be made in each zfs thread for a mounted file system.
905367Sahrens  *	This is done avoiding races using ZFS_ENTER(zfsvfs).
915367Sahrens  *      A ZFS_EXIT(zfsvfs) is needed before all returns.  Any znodes
925367Sahrens  *      must be checked with ZFS_VERIFY_ZP(zp).  Both of these macros
935367Sahrens  *      can return EIO from the calling function.
94789Sahrens  *
95789Sahrens  *  (2)	VN_RELE() should always be the last thing except for zil_commit()
962638Sperrin  *	(if necessary) and ZFS_EXIT(). This is for 3 reasons:
97789Sahrens  *	First, if it's the last reference, the vnode/znode
98789Sahrens  *	can be freed, so the zp may point to freed memory.  Second, the last
99789Sahrens  *	reference will call zfs_zinactive(), which may induce a lot of work --
1001669Sperrin  *	pushing cached pages (which acquires range locks) and syncing out
101789Sahrens  *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
102789Sahrens  *	which could deadlock the system if you were already holding one.
103789Sahrens  *
1041757Sperrin  *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
1051757Sperrin  *	as they can span dmu_tx_assign() calls.
1061757Sperrin  *
1071757Sperrin  *  (4)	Always pass zfsvfs->z_assign as the second argument to dmu_tx_assign().
108789Sahrens  *	In normal operation, this will be TXG_NOWAIT.  During ZIL replay,
109789Sahrens  *	it will be a specific txg.  Either way, dmu_tx_assign() never blocks.
110789Sahrens  *	This is critical because we don't want to block while holding locks.
111789Sahrens  *	Note, in particular, that if a lock is sometimes acquired before
112789Sahrens  *	the tx assigns, and sometimes after (e.g. z_lock), then failing to
113789Sahrens  *	use a non-blocking assign can deadlock the system.  The scenario:
114789Sahrens  *
115789Sahrens  *	Thread A has grabbed a lock before calling dmu_tx_assign().
116789Sahrens  *	Thread B is in an already-assigned tx, and blocks for this lock.
117789Sahrens  *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
118789Sahrens  *	forever, because the previous txg can't quiesce until B's tx commits.
119789Sahrens  *
120789Sahrens  *	If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
1212113Sahrens  *	then drop all locks, call dmu_tx_wait(), and try again.
122789Sahrens  *
1231757Sperrin  *  (5)	If the operation succeeded, generate the intent log entry for it
124789Sahrens  *	before dropping locks.  This ensures that the ordering of events
125789Sahrens  *	in the intent log matches the order in which they actually occurred.
126789Sahrens  *
1271757Sperrin  *  (6)	At the end of each vnode op, the DMU tx must always commit,
128789Sahrens  *	regardless of whether there were any errors.
129789Sahrens  *
1302638Sperrin  *  (7)	After dropping all locks, invoke zil_commit(zilog, seq, foid)
131789Sahrens  *	to ensure that synchronous semantics are provided when necessary.
132789Sahrens  *
133789Sahrens  * In general, this is how things should be ordered in each vnode op:
134789Sahrens  *
135789Sahrens  *	ZFS_ENTER(zfsvfs);		// exit if unmounted
136789Sahrens  * top:
137789Sahrens  *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
138789Sahrens  *	rw_enter(...);			// grab any other locks you need
139789Sahrens  *	tx = dmu_tx_create(...);	// get DMU tx
140789Sahrens  *	dmu_tx_hold_*();		// hold each object you might modify
141789Sahrens  *	error = dmu_tx_assign(tx, zfsvfs->z_assign);	// try to assign
142789Sahrens  *	if (error) {
143789Sahrens  *		rw_exit(...);		// drop locks
144789Sahrens  *		zfs_dirent_unlock(dl);	// unlock directory entry
145789Sahrens  *		VN_RELE(...);		// release held vnodes
146789Sahrens  *		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1472113Sahrens  *			dmu_tx_wait(tx);
1482113Sahrens  *			dmu_tx_abort(tx);
149789Sahrens  *			goto top;
150789Sahrens  *		}
1512113Sahrens  *		dmu_tx_abort(tx);	// abort DMU tx
152789Sahrens  *		ZFS_EXIT(zfsvfs);	// finished in zfs
153789Sahrens  *		return (error);		// really out of space
154789Sahrens  *	}
155789Sahrens  *	error = do_real_work();		// do whatever this VOP does
156789Sahrens  *	if (error == 0)
1572638Sperrin  *		zfs_log_*(...);		// on success, make ZIL entry
158789Sahrens  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
159789Sahrens  *	rw_exit(...);			// drop locks
160789Sahrens  *	zfs_dirent_unlock(dl);		// unlock directory entry
161789Sahrens  *	VN_RELE(...);			// release held vnodes
1622638Sperrin  *	zil_commit(zilog, seq, foid);	// synchronous when necessary
163789Sahrens  *	ZFS_EXIT(zfsvfs);		// finished in zfs
164789Sahrens  *	return (error);			// done, report error
165789Sahrens  */
1665367Sahrens 
167789Sahrens /* ARGSUSED */
168789Sahrens static int
1695331Samw zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
170789Sahrens {
1713063Sperrin 	znode_t	*zp = VTOZ(*vpp);
1723063Sperrin 
1735331Samw 	if ((flag & FWRITE) && (zp->z_phys->zp_flags & ZFS_APPENDONLY) &&
1745331Samw 	    ((flag & FAPPEND) == 0)) {
1755331Samw 		return (EPERM);
1765331Samw 	}
1775331Samw 
1785331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
1795331Samw 	    ZTOV(zp)->v_type == VREG &&
1805331Samw 	    !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
1815331Samw 	    zp->z_phys->zp_size > 0)
1825331Samw 		if (fs_vscan(*vpp, cr, 0) != 0)
1835331Samw 			return (EACCES);
1845331Samw 
1853063Sperrin 	/* Keep a count of the synchronous opens in the znode */
1863063Sperrin 	if (flag & (FSYNC | FDSYNC))
1873063Sperrin 		atomic_inc_32(&zp->z_sync_cnt);
1885331Samw 
189789Sahrens 	return (0);
190789Sahrens }
191789Sahrens 
192789Sahrens /* ARGSUSED */
193789Sahrens static int
1945331Samw zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
1955331Samw     caller_context_t *ct)
196789Sahrens {
1973063Sperrin 	znode_t	*zp = VTOZ(vp);
1983063Sperrin 
1993063Sperrin 	/* Decrement the synchronous opens in the znode */
2004339Sperrin 	if ((flag & (FSYNC | FDSYNC)) && (count == 1))
2013063Sperrin 		atomic_dec_32(&zp->z_sync_cnt);
2023063Sperrin 
203789Sahrens 	/*
204789Sahrens 	 * Clean up any locks held by this process on the vp.
205789Sahrens 	 */
206789Sahrens 	cleanlocks(vp, ddi_get_pid(), 0);
207789Sahrens 	cleanshares(vp, ddi_get_pid());
208789Sahrens 
2095331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
2105331Samw 	    ZTOV(zp)->v_type == VREG &&
2115331Samw 	    !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
2125331Samw 	    zp->z_phys->zp_size > 0)
2135331Samw 		VERIFY(fs_vscan(vp, cr, 1) == 0);
2145331Samw 
215789Sahrens 	return (0);
216789Sahrens }
217789Sahrens 
218789Sahrens /*
219789Sahrens  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
220789Sahrens  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
221789Sahrens  */
222789Sahrens static int
223789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off)
224789Sahrens {
225789Sahrens 	znode_t	*zp = VTOZ(vp);
226789Sahrens 	uint64_t noff = (uint64_t)*off; /* new offset */
227789Sahrens 	uint64_t file_sz;
228789Sahrens 	int error;
229789Sahrens 	boolean_t hole;
230789Sahrens 
231789Sahrens 	file_sz = zp->z_phys->zp_size;
232789Sahrens 	if (noff >= file_sz)  {
233789Sahrens 		return (ENXIO);
234789Sahrens 	}
235789Sahrens 
236789Sahrens 	if (cmd == _FIO_SEEK_HOLE)
237789Sahrens 		hole = B_TRUE;
238789Sahrens 	else
239789Sahrens 		hole = B_FALSE;
240789Sahrens 
241789Sahrens 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
242789Sahrens 
243789Sahrens 	/* end of file? */
244789Sahrens 	if ((error == ESRCH) || (noff > file_sz)) {
245789Sahrens 		/*
246789Sahrens 		 * Handle the virtual hole at the end of file.
247789Sahrens 		 */
248789Sahrens 		if (hole) {
249789Sahrens 			*off = file_sz;
250789Sahrens 			return (0);
251789Sahrens 		}
252789Sahrens 		return (ENXIO);
253789Sahrens 	}
254789Sahrens 
255789Sahrens 	if (noff < *off)
256789Sahrens 		return (error);
257789Sahrens 	*off = noff;
258789Sahrens 	return (error);
259789Sahrens }
260789Sahrens 
261789Sahrens /* ARGSUSED */
262789Sahrens static int
263789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
2645331Samw     int *rvalp, caller_context_t *ct)
265789Sahrens {
266789Sahrens 	offset_t off;
267789Sahrens 	int error;
268789Sahrens 	zfsvfs_t *zfsvfs;
2695326Sek110237 	znode_t *zp;
270789Sahrens 
271789Sahrens 	switch (com) {
2724339Sperrin 	case _FIOFFS:
273789Sahrens 		return (zfs_sync(vp->v_vfsp, 0, cred));
274789Sahrens 
2751544Seschrock 		/*
2761544Seschrock 		 * The following two ioctls are used by bfu.  Faking out,
2771544Seschrock 		 * necessary to avoid bfu errors.
2781544Seschrock 		 */
2794339Sperrin 	case _FIOGDIO:
2804339Sperrin 	case _FIOSDIO:
2811544Seschrock 		return (0);
2821544Seschrock 
2834339Sperrin 	case _FIO_SEEK_DATA:
2844339Sperrin 	case _FIO_SEEK_HOLE:
285789Sahrens 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
286789Sahrens 			return (EFAULT);
287789Sahrens 
2885326Sek110237 		zp = VTOZ(vp);
2895326Sek110237 		zfsvfs = zp->z_zfsvfs;
2905367Sahrens 		ZFS_ENTER(zfsvfs);
2915367Sahrens 		ZFS_VERIFY_ZP(zp);
292789Sahrens 
293789Sahrens 		/* offset parameter is in/out */
294789Sahrens 		error = zfs_holey(vp, com, &off);
295789Sahrens 		ZFS_EXIT(zfsvfs);
296789Sahrens 		if (error)
297789Sahrens 			return (error);
298789Sahrens 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
299789Sahrens 			return (EFAULT);
300789Sahrens 		return (0);
301789Sahrens 	}
302789Sahrens 	return (ENOTTY);
303789Sahrens }
304789Sahrens 
305789Sahrens /*
306789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
307789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
308789Sahrens  *
309789Sahrens  * On Write:	If we find a memory mapped page, we write to *both*
310789Sahrens  *		the page and the dmu buffer.
311789Sahrens  *
312789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
313789Sahrens  *	the file is memory mapped.
314789Sahrens  */
315789Sahrens static int
3163638Sbillm mappedwrite(vnode_t *vp, int nbytes, uio_t *uio, dmu_tx_t *tx)
317789Sahrens {
318789Sahrens 	znode_t	*zp = VTOZ(vp);
319789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
320789Sahrens 	int64_t	start, off;
321789Sahrens 	int len = nbytes;
322789Sahrens 	int error = 0;
323789Sahrens 
324789Sahrens 	start = uio->uio_loffset;
325789Sahrens 	off = start & PAGEOFFSET;
326789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
327789Sahrens 		page_t *pp;
328789Sahrens 		uint64_t bytes = MIN(PAGESIZE - off, len);
3293638Sbillm 		uint64_t woff = uio->uio_loffset;
330789Sahrens 
331789Sahrens 		/*
332789Sahrens 		 * We don't want a new page to "appear" in the middle of
333789Sahrens 		 * the file update (because it may not get the write
334789Sahrens 		 * update data), so we grab a lock to block
335789Sahrens 		 * zfs_getpage().
336789Sahrens 		 */
337789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
338789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
339789Sahrens 			caddr_t va;
340789Sahrens 
341789Sahrens 			rw_exit(&zp->z_map_lock);
342789Sahrens 			va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L);
343789Sahrens 			error = uiomove(va+off, bytes, UIO_WRITE, uio);
344789Sahrens 			if (error == 0) {
345789Sahrens 				dmu_write(zfsvfs->z_os, zp->z_id,
346789Sahrens 				    woff, bytes, va+off, tx);
347789Sahrens 			}
348789Sahrens 			ppmapout(va);
349789Sahrens 			page_unlock(pp);
350789Sahrens 		} else {
351789Sahrens 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
3523638Sbillm 			    uio, bytes, tx);
353789Sahrens 			rw_exit(&zp->z_map_lock);
354789Sahrens 		}
355789Sahrens 		len -= bytes;
356789Sahrens 		off = 0;
357789Sahrens 		if (error)
358789Sahrens 			break;
359789Sahrens 	}
360789Sahrens 	return (error);
361789Sahrens }
362789Sahrens 
363789Sahrens /*
364789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
365789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
366789Sahrens  *
367789Sahrens  * On Read:	We "read" preferentially from memory mapped pages,
368789Sahrens  *		else we default from the dmu buffer.
369789Sahrens  *
370789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
371789Sahrens  *	the file is memory mapped.
372789Sahrens  */
373789Sahrens static int
3743638Sbillm mappedread(vnode_t *vp, int nbytes, uio_t *uio)
375789Sahrens {
3763638Sbillm 	znode_t *zp = VTOZ(vp);
3773638Sbillm 	objset_t *os = zp->z_zfsvfs->z_os;
3783638Sbillm 	int64_t	start, off;
379789Sahrens 	int len = nbytes;
380789Sahrens 	int error = 0;
381789Sahrens 
382789Sahrens 	start = uio->uio_loffset;
383789Sahrens 	off = start & PAGEOFFSET;
384789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
385789Sahrens 		page_t *pp;
3863638Sbillm 		uint64_t bytes = MIN(PAGESIZE - off, len);
3873638Sbillm 
388789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
389789Sahrens 			caddr_t va;
390789Sahrens 
3912688Smaybee 			va = ppmapin(pp, PROT_READ, (caddr_t)-1L);
392789Sahrens 			error = uiomove(va + off, bytes, UIO_READ, uio);
393789Sahrens 			ppmapout(va);
394789Sahrens 			page_unlock(pp);
395789Sahrens 		} else {
3963638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, bytes);
397789Sahrens 		}
398789Sahrens 		len -= bytes;
399789Sahrens 		off = 0;
400789Sahrens 		if (error)
401789Sahrens 			break;
402789Sahrens 	}
403789Sahrens 	return (error);
404789Sahrens }
405789Sahrens 
4063638Sbillm offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
407789Sahrens 
408789Sahrens /*
409789Sahrens  * Read bytes from specified file into supplied buffer.
410789Sahrens  *
411789Sahrens  *	IN:	vp	- vnode of file to be read from.
412789Sahrens  *		uio	- structure supplying read location, range info,
413789Sahrens  *			  and return buffer.
414789Sahrens  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
415789Sahrens  *		cr	- credentials of caller.
4165331Samw  *		ct	- caller context
417789Sahrens  *
418789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
419789Sahrens  *
420789Sahrens  *	RETURN:	0 if success
421789Sahrens  *		error code if failure
422789Sahrens  *
423789Sahrens  * Side Effects:
424789Sahrens  *	vp - atime updated if byte count > 0
425789Sahrens  */
426789Sahrens /* ARGSUSED */
427789Sahrens static int
428789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
429789Sahrens {
430789Sahrens 	znode_t		*zp = VTOZ(vp);
431789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4325326Sek110237 	objset_t	*os;
4333638Sbillm 	ssize_t		n, nbytes;
4343638Sbillm 	int		error;
4351669Sperrin 	rl_t		*rl;
436789Sahrens 
4375367Sahrens 	ZFS_ENTER(zfsvfs);
4385367Sahrens 	ZFS_VERIFY_ZP(zp);
4395326Sek110237 	os = zfsvfs->z_os;
440789Sahrens 
441789Sahrens 	/*
442789Sahrens 	 * Validate file offset
443789Sahrens 	 */
444789Sahrens 	if (uio->uio_loffset < (offset_t)0) {
445789Sahrens 		ZFS_EXIT(zfsvfs);
446789Sahrens 		return (EINVAL);
447789Sahrens 	}
448789Sahrens 
449789Sahrens 	/*
450789Sahrens 	 * Fasttrack empty reads
451789Sahrens 	 */
452789Sahrens 	if (uio->uio_resid == 0) {
453789Sahrens 		ZFS_EXIT(zfsvfs);
454789Sahrens 		return (0);
455789Sahrens 	}
456789Sahrens 
457789Sahrens 	/*
4581669Sperrin 	 * Check for mandatory locks
459789Sahrens 	 */
460789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
461789Sahrens 		if (error = chklock(vp, FREAD,
462789Sahrens 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
463789Sahrens 			ZFS_EXIT(zfsvfs);
464789Sahrens 			return (error);
465789Sahrens 		}
466789Sahrens 	}
467789Sahrens 
468789Sahrens 	/*
469789Sahrens 	 * If we're in FRSYNC mode, sync out this znode before reading it.
470789Sahrens 	 */
4712638Sperrin 	if (ioflag & FRSYNC)
4722638Sperrin 		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
473789Sahrens 
474789Sahrens 	/*
4751669Sperrin 	 * Lock the range against changes.
476789Sahrens 	 */
4771669Sperrin 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
4781669Sperrin 
479789Sahrens 	/*
480789Sahrens 	 * If we are reading past end-of-file we can skip
481789Sahrens 	 * to the end; but we might still need to set atime.
482789Sahrens 	 */
483789Sahrens 	if (uio->uio_loffset >= zp->z_phys->zp_size) {
484789Sahrens 		error = 0;
485789Sahrens 		goto out;
486789Sahrens 	}
487789Sahrens 
4883638Sbillm 	ASSERT(uio->uio_loffset < zp->z_phys->zp_size);
4893638Sbillm 	n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset);
4903638Sbillm 
4913638Sbillm 	while (n > 0) {
4923638Sbillm 		nbytes = MIN(n, zfs_read_chunk_size -
4933638Sbillm 		    P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
4943638Sbillm 
4953638Sbillm 		if (vn_has_cached_data(vp))
4963638Sbillm 			error = mappedread(vp, nbytes, uio);
4973638Sbillm 		else
4983638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, nbytes);
4991544Seschrock 		if (error)
5003638Sbillm 			break;
5013638Sbillm 
5023638Sbillm 		n -= nbytes;
503789Sahrens 	}
5043638Sbillm 
505789Sahrens out:
5062237Smaybee 	zfs_range_unlock(rl);
507789Sahrens 
508789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
509789Sahrens 	ZFS_EXIT(zfsvfs);
510789Sahrens 	return (error);
511789Sahrens }
512789Sahrens 
513789Sahrens /*
514789Sahrens  * Fault in the pages of the first n bytes specified by the uio structure.
515789Sahrens  * 1 byte in each page is touched and the uio struct is unmodified.
516789Sahrens  * Any error will exit this routine as this is only a best
517789Sahrens  * attempt to get the pages resident. This is a copy of ufs_trans_touch().
518789Sahrens  */
519789Sahrens static void
520789Sahrens zfs_prefault_write(ssize_t n, struct uio *uio)
521789Sahrens {
522789Sahrens 	struct iovec *iov;
523789Sahrens 	ulong_t cnt, incr;
524789Sahrens 	caddr_t p;
525789Sahrens 	uint8_t tmp;
526789Sahrens 
527789Sahrens 	iov = uio->uio_iov;
528789Sahrens 
529789Sahrens 	while (n) {
530789Sahrens 		cnt = MIN(iov->iov_len, n);
531789Sahrens 		if (cnt == 0) {
532789Sahrens 			/* empty iov entry */
533789Sahrens 			iov++;
534789Sahrens 			continue;
535789Sahrens 		}
536789Sahrens 		n -= cnt;
537789Sahrens 		/*
538789Sahrens 		 * touch each page in this segment.
539789Sahrens 		 */
540789Sahrens 		p = iov->iov_base;
541789Sahrens 		while (cnt) {
542789Sahrens 			switch (uio->uio_segflg) {
543789Sahrens 			case UIO_USERSPACE:
544789Sahrens 			case UIO_USERISPACE:
545789Sahrens 				if (fuword8(p, &tmp))
546789Sahrens 					return;
547789Sahrens 				break;
548789Sahrens 			case UIO_SYSSPACE:
549789Sahrens 				if (kcopy(p, &tmp, 1))
550789Sahrens 					return;
551789Sahrens 				break;
552789Sahrens 			}
553789Sahrens 			incr = MIN(cnt, PAGESIZE);
554789Sahrens 			p += incr;
555789Sahrens 			cnt -= incr;
556789Sahrens 		}
557789Sahrens 		/*
558789Sahrens 		 * touch the last byte in case it straddles a page.
559789Sahrens 		 */
560789Sahrens 		p--;
561789Sahrens 		switch (uio->uio_segflg) {
562789Sahrens 		case UIO_USERSPACE:
563789Sahrens 		case UIO_USERISPACE:
564789Sahrens 			if (fuword8(p, &tmp))
565789Sahrens 				return;
566789Sahrens 			break;
567789Sahrens 		case UIO_SYSSPACE:
568789Sahrens 			if (kcopy(p, &tmp, 1))
569789Sahrens 				return;
570789Sahrens 			break;
571789Sahrens 		}
572789Sahrens 		iov++;
573789Sahrens 	}
574789Sahrens }
575789Sahrens 
576789Sahrens /*
577789Sahrens  * Write the bytes to a file.
578789Sahrens  *
579789Sahrens  *	IN:	vp	- vnode of file to be written to.
580789Sahrens  *		uio	- structure supplying write location, range info,
581789Sahrens  *			  and data buffer.
582789Sahrens  *		ioflag	- FAPPEND flag set if in append mode.
583789Sahrens  *		cr	- credentials of caller.
5845331Samw  *		ct	- caller context (NFS/CIFS fem monitor only)
585789Sahrens  *
586789Sahrens  *	OUT:	uio	- updated offset and range.
587789Sahrens  *
588789Sahrens  *	RETURN:	0 if success
589789Sahrens  *		error code if failure
590789Sahrens  *
591789Sahrens  * Timestamps:
592789Sahrens  *	vp - ctime|mtime updated if byte count > 0
593789Sahrens  */
594789Sahrens /* ARGSUSED */
595789Sahrens static int
596789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
597789Sahrens {
598789Sahrens 	znode_t		*zp = VTOZ(vp);
599789Sahrens 	rlim64_t	limit = uio->uio_llimit;
600789Sahrens 	ssize_t		start_resid = uio->uio_resid;
601789Sahrens 	ssize_t		tx_bytes;
602789Sahrens 	uint64_t	end_size;
603789Sahrens 	dmu_tx_t	*tx;
604789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
6055326Sek110237 	zilog_t		*zilog;
606789Sahrens 	offset_t	woff;
607789Sahrens 	ssize_t		n, nbytes;
6081669Sperrin 	rl_t		*rl;
609789Sahrens 	int		max_blksz = zfsvfs->z_max_blksz;
6105331Samw 	uint64_t	pflags = zp->z_phys->zp_flags;
6111669Sperrin 	int		error;
612789Sahrens 
613789Sahrens 	/*
6145331Samw 	 * If immutable or not appending then return EPERM
6155331Samw 	 */
6165331Samw 	if ((pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
6175331Samw 	    ((pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
6185331Samw 	    (uio->uio_loffset < zp->z_phys->zp_size)))
6195331Samw 		return (EPERM);
6205331Samw 
6215331Samw 	/*
622789Sahrens 	 * Fasttrack empty write
623789Sahrens 	 */
6241669Sperrin 	n = start_resid;
625789Sahrens 	if (n == 0)
626789Sahrens 		return (0);
627789Sahrens 
6281669Sperrin 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
6291669Sperrin 		limit = MAXOFFSET_T;
6301669Sperrin 
6315367Sahrens 	ZFS_ENTER(zfsvfs);
6325367Sahrens 	ZFS_VERIFY_ZP(zp);
6335326Sek110237 	zilog = zfsvfs->z_log;
634789Sahrens 
635789Sahrens 	/*
6362237Smaybee 	 * Pre-fault the pages to ensure slow (eg NFS) pages
6371669Sperrin 	 * don't hold up txg.
638789Sahrens 	 */
6392237Smaybee 	zfs_prefault_write(n, uio);
640789Sahrens 
641789Sahrens 	/*
642789Sahrens 	 * If in append mode, set the io offset pointer to eof.
643789Sahrens 	 */
6441669Sperrin 	if (ioflag & FAPPEND) {
6451669Sperrin 		/*
6461669Sperrin 		 * Range lock for a file append:
6471669Sperrin 		 * The value for the start of range will be determined by
6481669Sperrin 		 * zfs_range_lock() (to guarantee append semantics).
6491669Sperrin 		 * If this write will cause the block size to increase,
6501669Sperrin 		 * zfs_range_lock() will lock the entire file, so we must
6511669Sperrin 		 * later reduce the range after we grow the block size.
6521669Sperrin 		 */
6531669Sperrin 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
6541669Sperrin 		if (rl->r_len == UINT64_MAX) {
6551669Sperrin 			/* overlocked, zp_size can't change */
6561669Sperrin 			woff = uio->uio_loffset = zp->z_phys->zp_size;
6571669Sperrin 		} else {
6581669Sperrin 			woff = uio->uio_loffset = rl->r_off;
6591669Sperrin 		}
660789Sahrens 	} else {
661789Sahrens 		woff = uio->uio_loffset;
662789Sahrens 		/*
663789Sahrens 		 * Validate file offset
664789Sahrens 		 */
665789Sahrens 		if (woff < 0) {
666789Sahrens 			ZFS_EXIT(zfsvfs);
667789Sahrens 			return (EINVAL);
668789Sahrens 		}
669789Sahrens 
670789Sahrens 		/*
6711669Sperrin 		 * If we need to grow the block size then zfs_range_lock()
6721669Sperrin 		 * will lock a wider range than we request here.
6731669Sperrin 		 * Later after growing the block size we reduce the range.
674789Sahrens 		 */
6751669Sperrin 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
676789Sahrens 	}
677789Sahrens 
678789Sahrens 	if (woff >= limit) {
6793638Sbillm 		zfs_range_unlock(rl);
6803638Sbillm 		ZFS_EXIT(zfsvfs);
6813638Sbillm 		return (EFBIG);
682789Sahrens 	}
683789Sahrens 
684789Sahrens 	if ((woff + n) > limit || woff > (limit - n))
685789Sahrens 		n = limit - woff;
686789Sahrens 
687789Sahrens 	/*
6881669Sperrin 	 * Check for mandatory locks
689789Sahrens 	 */
690789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) &&
6913638Sbillm 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
6923638Sbillm 		zfs_range_unlock(rl);
6933638Sbillm 		ZFS_EXIT(zfsvfs);
6943638Sbillm 		return (error);
6953638Sbillm 	}
6961669Sperrin 	end_size = MAX(zp->z_phys->zp_size, woff + n);
697789Sahrens 
6981669Sperrin 	/*
6993638Sbillm 	 * Write the file in reasonable size chunks.  Each chunk is written
7003638Sbillm 	 * in a separate transaction; this keeps the intent log records small
7013638Sbillm 	 * and allows us to do more fine-grained space accounting.
702789Sahrens 	 */
703789Sahrens 	while (n > 0) {
704789Sahrens 		/*
7053638Sbillm 		 * Start a transaction.
706789Sahrens 		 */
707789Sahrens 		woff = uio->uio_loffset;
708789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
709789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
710789Sahrens 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
711789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
712789Sahrens 		if (error) {
713789Sahrens 			if (error == ERESTART &&
714789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
7152113Sahrens 				dmu_tx_wait(tx);
7162113Sahrens 				dmu_tx_abort(tx);
7173638Sbillm 				continue;
718789Sahrens 			}
7192113Sahrens 			dmu_tx_abort(tx);
7203638Sbillm 			break;
7213638Sbillm 		}
7223638Sbillm 
7233638Sbillm 		/*
7243638Sbillm 		 * If zfs_range_lock() over-locked we grow the blocksize
7253638Sbillm 		 * and then reduce the lock range.  This will only happen
7263638Sbillm 		 * on the first iteration since zfs_range_reduce() will
7273638Sbillm 		 * shrink down r_len to the appropriate size.
7283638Sbillm 		 */
7293638Sbillm 		if (rl->r_len == UINT64_MAX) {
7303638Sbillm 			uint64_t new_blksz;
7313638Sbillm 
7323638Sbillm 			if (zp->z_blksz > max_blksz) {
7333638Sbillm 				ASSERT(!ISP2(zp->z_blksz));
7343638Sbillm 				new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
7353638Sbillm 			} else {
7363638Sbillm 				new_blksz = MIN(end_size, max_blksz);
7373638Sbillm 			}
7383638Sbillm 			zfs_grow_blocksize(zp, new_blksz, tx);
7393638Sbillm 			zfs_range_reduce(rl, woff, n);
7403638Sbillm 		}
7413638Sbillm 
7423638Sbillm 		/*
7433638Sbillm 		 * XXX - should we really limit each write to z_max_blksz?
7443638Sbillm 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
7453638Sbillm 		 */
7463638Sbillm 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
7473638Sbillm 		rw_enter(&zp->z_map_lock, RW_READER);
7483638Sbillm 
7493638Sbillm 		tx_bytes = uio->uio_resid;
7503638Sbillm 		if (vn_has_cached_data(vp)) {
7513638Sbillm 			rw_exit(&zp->z_map_lock);
7523638Sbillm 			error = mappedwrite(vp, nbytes, uio, tx);
7533638Sbillm 		} else {
7543638Sbillm 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
7553638Sbillm 			    uio, nbytes, tx);
7563638Sbillm 			rw_exit(&zp->z_map_lock);
757789Sahrens 		}
7583638Sbillm 		tx_bytes -= uio->uio_resid;
7593638Sbillm 
7603638Sbillm 		/*
7613638Sbillm 		 * If we made no progress, we're done.  If we made even
7623638Sbillm 		 * partial progress, update the znode and ZIL accordingly.
7633638Sbillm 		 */
7643638Sbillm 		if (tx_bytes == 0) {
7653897Smaybee 			dmu_tx_commit(tx);
7663638Sbillm 			ASSERT(error != 0);
7673638Sbillm 			break;
7683638Sbillm 		}
7693638Sbillm 
770789Sahrens 		/*
7713638Sbillm 		 * Clear Set-UID/Set-GID bits on successful write if not
7723638Sbillm 		 * privileged and at least one of the excute bits is set.
7733638Sbillm 		 *
7743638Sbillm 		 * It would be nice to to this after all writes have
7753638Sbillm 		 * been done, but that would still expose the ISUID/ISGID
7763638Sbillm 		 * to another app after the partial write is committed.
7775331Samw 		 *
7785331Samw 		 * Note: we don't call zfs_fuid_map_id() here because
7795331Samw 		 * user 0 is not an ephemeral uid.
780789Sahrens 		 */
7813638Sbillm 		mutex_enter(&zp->z_acl_lock);
7823638Sbillm 		if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) |
7833638Sbillm 		    (S_IXUSR >> 6))) != 0 &&
7843638Sbillm 		    (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 &&
7853638Sbillm 		    secpolicy_vnode_setid_retain(cr,
7863638Sbillm 		    (zp->z_phys->zp_mode & S_ISUID) != 0 &&
7873638Sbillm 		    zp->z_phys->zp_uid == 0) != 0) {
7884339Sperrin 			zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID);
7893638Sbillm 		}
7903638Sbillm 		mutex_exit(&zp->z_acl_lock);
7913638Sbillm 
7923638Sbillm 		/*
7933638Sbillm 		 * Update time stamp.  NOTE: This marks the bonus buffer as
7943638Sbillm 		 * dirty, so we don't have to do it again for zp_size.
7953638Sbillm 		 */
7963638Sbillm 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
7973638Sbillm 
7983638Sbillm 		/*
7993638Sbillm 		 * Update the file size (zp_size) if it has changed;
8003638Sbillm 		 * account for possible concurrent updates.
8013638Sbillm 		 */
8023638Sbillm 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset)
803789Sahrens 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
804789Sahrens 			    uio->uio_loffset);
8053638Sbillm 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
8063638Sbillm 		dmu_tx_commit(tx);
8073638Sbillm 
8083638Sbillm 		if (error != 0)
8093638Sbillm 			break;
8103638Sbillm 		ASSERT(tx_bytes == nbytes);
8113638Sbillm 		n -= nbytes;
812789Sahrens 	}
813789Sahrens 
8142237Smaybee 	zfs_range_unlock(rl);
815789Sahrens 
816789Sahrens 	/*
817789Sahrens 	 * If we're in replay mode, or we made no progress, return error.
818789Sahrens 	 * Otherwise, it's at least a partial write, so it's successful.
819789Sahrens 	 */
820789Sahrens 	if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) {
821789Sahrens 		ZFS_EXIT(zfsvfs);
822789Sahrens 		return (error);
823789Sahrens 	}
824789Sahrens 
8252638Sperrin 	if (ioflag & (FSYNC | FDSYNC))
8262638Sperrin 		zil_commit(zilog, zp->z_last_itx, zp->z_id);
827789Sahrens 
828789Sahrens 	ZFS_EXIT(zfsvfs);
829789Sahrens 	return (0);
830789Sahrens }
831789Sahrens 
8322237Smaybee void
8333063Sperrin zfs_get_done(dmu_buf_t *db, void *vzgd)
8342237Smaybee {
8353063Sperrin 	zgd_t *zgd = (zgd_t *)vzgd;
8363063Sperrin 	rl_t *rl = zgd->zgd_rl;
8372237Smaybee 	vnode_t *vp = ZTOV(rl->r_zp);
8382237Smaybee 
8393063Sperrin 	dmu_buf_rele(db, vzgd);
8402237Smaybee 	zfs_range_unlock(rl);
8412237Smaybee 	VN_RELE(vp);
8425688Sbonwick 	zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
8433063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
8442237Smaybee }
8452237Smaybee 
846789Sahrens /*
847789Sahrens  * Get data to generate a TX_WRITE intent log record.
848789Sahrens  */
849789Sahrens int
8502237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
851789Sahrens {
852789Sahrens 	zfsvfs_t *zfsvfs = arg;
853789Sahrens 	objset_t *os = zfsvfs->z_os;
854789Sahrens 	znode_t *zp;
855789Sahrens 	uint64_t off = lr->lr_offset;
8562237Smaybee 	dmu_buf_t *db;
8571669Sperrin 	rl_t *rl;
8583063Sperrin 	zgd_t *zgd;
8593638Sbillm 	int dlen = lr->lr_length;		/* length of user data */
860789Sahrens 	int error = 0;
861789Sahrens 
8623063Sperrin 	ASSERT(zio);
863789Sahrens 	ASSERT(dlen != 0);
864789Sahrens 
865789Sahrens 	/*
8661669Sperrin 	 * Nothing to do if the file has been removed
867789Sahrens 	 */
868789Sahrens 	if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0)
869789Sahrens 		return (ENOENT);
8703461Sahrens 	if (zp->z_unlinked) {
871789Sahrens 		VN_RELE(ZTOV(zp));
872789Sahrens 		return (ENOENT);
873789Sahrens 	}
874789Sahrens 
875789Sahrens 	/*
876789Sahrens 	 * Write records come in two flavors: immediate and indirect.
877789Sahrens 	 * For small writes it's cheaper to store the data with the
878789Sahrens 	 * log record (immediate); for large writes it's cheaper to
879789Sahrens 	 * sync the data and get a pointer to it (indirect) so that
880789Sahrens 	 * we don't have to write the data twice.
881789Sahrens 	 */
8821669Sperrin 	if (buf != NULL) { /* immediate write */
8831669Sperrin 		rl = zfs_range_lock(zp, off, dlen, RL_READER);
8841669Sperrin 		/* test for truncation needs to be done while range locked */
8851669Sperrin 		if (off >= zp->z_phys->zp_size) {
8861669Sperrin 			error = ENOENT;
8871669Sperrin 			goto out;
8881669Sperrin 		}
8892449Smaybee 		VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf));
8901669Sperrin 	} else { /* indirect write */
8911669Sperrin 		uint64_t boff; /* block starting offset */
8921669Sperrin 
893789Sahrens 		/*
8941669Sperrin 		 * Have to lock the whole block to ensure when it's
8951669Sperrin 		 * written out and it's checksum is being calculated
8961669Sperrin 		 * that no one can change the data. We need to re-check
8971669Sperrin 		 * blocksize after we get the lock in case it's changed!
898789Sahrens 		 */
8991669Sperrin 		for (;;) {
9001941Sperrin 			if (ISP2(zp->z_blksz)) {
9011941Sperrin 				boff = P2ALIGN_TYPED(off, zp->z_blksz,
9021941Sperrin 				    uint64_t);
9031941Sperrin 			} else {
9041941Sperrin 				boff = 0;
9051941Sperrin 			}
9061669Sperrin 			dlen = zp->z_blksz;
9071669Sperrin 			rl = zfs_range_lock(zp, boff, dlen, RL_READER);
9081669Sperrin 			if (zp->z_blksz == dlen)
9091669Sperrin 				break;
9102237Smaybee 			zfs_range_unlock(rl);
9111669Sperrin 		}
9121669Sperrin 		/* test for truncation needs to be done while range locked */
9131669Sperrin 		if (off >= zp->z_phys->zp_size) {
9141669Sperrin 			error = ENOENT;
9151669Sperrin 			goto out;
9161669Sperrin 		}
9173063Sperrin 		zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
9183063Sperrin 		zgd->zgd_rl = rl;
9193063Sperrin 		zgd->zgd_zilog = zfsvfs->z_log;
9203063Sperrin 		zgd->zgd_bp = &lr->lr_blkptr;
9213063Sperrin 		VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db));
9222237Smaybee 		ASSERT(boff == db->db_offset);
9232237Smaybee 		lr->lr_blkoff = off - boff;
9242237Smaybee 		error = dmu_sync(zio, db, &lr->lr_blkptr,
9253063Sperrin 		    lr->lr_common.lrc_txg, zfs_get_done, zgd);
9264709Smaybee 		ASSERT((error && error != EINPROGRESS) ||
9274709Smaybee 		    lr->lr_length <= zp->z_blksz);
9285688Sbonwick 		if (error == 0)
9295688Sbonwick 			zil_add_block(zfsvfs->z_log, &lr->lr_blkptr);
9302237Smaybee 		/*
9312237Smaybee 		 * If we get EINPROGRESS, then we need to wait for a
9322237Smaybee 		 * write IO initiated by dmu_sync() to complete before
9332638Sperrin 		 * we can release this dbuf.  We will finish everything
9342237Smaybee 		 * up in the zfs_get_done() callback.
9352237Smaybee 		 */
9362237Smaybee 		if (error == EINPROGRESS)
9372237Smaybee 			return (0);
9383063Sperrin 		dmu_buf_rele(db, zgd);
9393063Sperrin 		kmem_free(zgd, sizeof (zgd_t));
940789Sahrens 	}
9411669Sperrin out:
9422237Smaybee 	zfs_range_unlock(rl);
943789Sahrens 	VN_RELE(ZTOV(zp));
944789Sahrens 	return (error);
945789Sahrens }
946789Sahrens 
947789Sahrens /*ARGSUSED*/
948789Sahrens static int
9495331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
9505331Samw     caller_context_t *ct)
951789Sahrens {
952789Sahrens 	znode_t *zp = VTOZ(vp);
953789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
954789Sahrens 	int error;
955789Sahrens 
9565367Sahrens 	ZFS_ENTER(zfsvfs);
9575367Sahrens 	ZFS_VERIFY_ZP(zp);
9585331Samw 
9595331Samw 	if (flag & V_ACE_MASK)
9605331Samw 		error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
9615331Samw 	else
9625331Samw 		error = zfs_zaccess_rwx(zp, mode, flag, cr);
9635331Samw 
964789Sahrens 	ZFS_EXIT(zfsvfs);
965789Sahrens 	return (error);
966789Sahrens }
967789Sahrens 
968789Sahrens /*
969789Sahrens  * Lookup an entry in a directory, or an extended attribute directory.
970789Sahrens  * If it exists, return a held vnode reference for it.
971789Sahrens  *
972789Sahrens  *	IN:	dvp	- vnode of directory to search.
973789Sahrens  *		nm	- name of entry to lookup.
974789Sahrens  *		pnp	- full pathname to lookup [UNUSED].
975789Sahrens  *		flags	- LOOKUP_XATTR set if looking for an attribute.
976789Sahrens  *		rdir	- root directory vnode [UNUSED].
977789Sahrens  *		cr	- credentials of caller.
9785331Samw  *		ct	- caller context
9795331Samw  *		direntflags - directory lookup flags
9805331Samw  *		realpnp - returned pathname.
981789Sahrens  *
982789Sahrens  *	OUT:	vpp	- vnode of located entry, NULL if not found.
983789Sahrens  *
984789Sahrens  *	RETURN:	0 if success
985789Sahrens  *		error code if failure
986789Sahrens  *
987789Sahrens  * Timestamps:
988789Sahrens  *	NA
989789Sahrens  */
990789Sahrens /* ARGSUSED */
991789Sahrens static int
992789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
9935331Samw     int flags, vnode_t *rdir, cred_t *cr,  caller_context_t *ct,
9945331Samw     int *direntflags, pathname_t *realpnp)
995789Sahrens {
996789Sahrens 	znode_t *zdp = VTOZ(dvp);
997789Sahrens 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
998789Sahrens 	int	error;
999789Sahrens 
10005367Sahrens 	ZFS_ENTER(zfsvfs);
10015367Sahrens 	ZFS_VERIFY_ZP(zdp);
1002789Sahrens 
1003789Sahrens 	*vpp = NULL;
1004789Sahrens 
1005789Sahrens 	if (flags & LOOKUP_XATTR) {
1006789Sahrens 		/*
10073234Sck153898 		 * If the xattr property is off, refuse the lookup request.
10083234Sck153898 		 */
10093234Sck153898 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
10103234Sck153898 			ZFS_EXIT(zfsvfs);
10113234Sck153898 			return (EINVAL);
10123234Sck153898 		}
10133234Sck153898 
10143234Sck153898 		/*
1015789Sahrens 		 * We don't allow recursive attributes..
1016789Sahrens 		 * Maybe someday we will.
1017789Sahrens 		 */
1018789Sahrens 		if (zdp->z_phys->zp_flags & ZFS_XATTR) {
1019789Sahrens 			ZFS_EXIT(zfsvfs);
1020789Sahrens 			return (EINVAL);
1021789Sahrens 		}
1022789Sahrens 
10233280Sck153898 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1024789Sahrens 			ZFS_EXIT(zfsvfs);
1025789Sahrens 			return (error);
1026789Sahrens 		}
1027789Sahrens 
1028789Sahrens 		/*
1029789Sahrens 		 * Do we have permission to get into attribute directory?
1030789Sahrens 		 */
1031789Sahrens 
10325331Samw 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
10335331Samw 		    B_FALSE, cr)) {
1034789Sahrens 			VN_RELE(*vpp);
10355331Samw 			*vpp = NULL;
1036789Sahrens 		}
1037789Sahrens 
1038789Sahrens 		ZFS_EXIT(zfsvfs);
1039789Sahrens 		return (error);
1040789Sahrens 	}
1041789Sahrens 
10421512Sek110237 	if (dvp->v_type != VDIR) {
10431512Sek110237 		ZFS_EXIT(zfsvfs);
10441460Smarks 		return (ENOTDIR);
10451512Sek110237 	}
10461460Smarks 
1047789Sahrens 	/*
1048789Sahrens 	 * Check accessibility of directory.
1049789Sahrens 	 */
1050789Sahrens 
10515331Samw 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1052789Sahrens 		ZFS_EXIT(zfsvfs);
1053789Sahrens 		return (error);
1054789Sahrens 	}
1055789Sahrens 
10565498Stimh 	if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
10575331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
10585331Samw 		ZFS_EXIT(zfsvfs);
10595331Samw 		return (EILSEQ);
10605331Samw 	}
10615331Samw 
10625331Samw 	error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
10635331Samw 	if (error == 0) {
1064789Sahrens 		/*
1065789Sahrens 		 * Convert device special files
1066789Sahrens 		 */
1067789Sahrens 		if (IS_DEVVP(*vpp)) {
1068789Sahrens 			vnode_t	*svp;
1069789Sahrens 
1070789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1071789Sahrens 			VN_RELE(*vpp);
1072789Sahrens 			if (svp == NULL)
1073789Sahrens 				error = ENOSYS;
1074789Sahrens 			else
1075789Sahrens 				*vpp = svp;
1076789Sahrens 		}
1077789Sahrens 	}
1078789Sahrens 
1079789Sahrens 	ZFS_EXIT(zfsvfs);
1080789Sahrens 	return (error);
1081789Sahrens }
1082789Sahrens 
1083789Sahrens /*
1084789Sahrens  * Attempt to create a new entry in a directory.  If the entry
1085789Sahrens  * already exists, truncate the file if permissible, else return
1086789Sahrens  * an error.  Return the vp of the created or trunc'd file.
1087789Sahrens  *
1088789Sahrens  *	IN:	dvp	- vnode of directory to put new file entry in.
1089789Sahrens  *		name	- name of new file entry.
1090789Sahrens  *		vap	- attributes of new file.
1091789Sahrens  *		excl	- flag indicating exclusive or non-exclusive mode.
1092789Sahrens  *		mode	- mode to open file with.
1093789Sahrens  *		cr	- credentials of caller.
1094789Sahrens  *		flag	- large file flag [UNUSED].
10955331Samw  *		ct	- caller context
10965331Samw  *		vsecp 	- ACL to be set
1097789Sahrens  *
1098789Sahrens  *	OUT:	vpp	- vnode of created or trunc'd entry.
1099789Sahrens  *
1100789Sahrens  *	RETURN:	0 if success
1101789Sahrens  *		error code if failure
1102789Sahrens  *
1103789Sahrens  * Timestamps:
1104789Sahrens  *	dvp - ctime|mtime updated if new entry created
1105789Sahrens  *	 vp - ctime|mtime always, atime if new
1106789Sahrens  */
11075331Samw 
1108789Sahrens /* ARGSUSED */
1109789Sahrens static int
1110789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
11115331Samw     int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
11125331Samw     vsecattr_t *vsecp)
1113789Sahrens {
1114789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1115789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
11165326Sek110237 	zilog_t		*zilog;
11175326Sek110237 	objset_t	*os;
1118789Sahrens 	zfs_dirlock_t	*dl;
1119789Sahrens 	dmu_tx_t	*tx;
1120789Sahrens 	int		error;
11215331Samw 	zfs_acl_t	*aclp = NULL;
11225331Samw 	zfs_fuid_info_t *fuidp = NULL;
11235331Samw 
11245331Samw 	/*
11255331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
11265331Samw 	 * make sure file system is at proper version
11275331Samw 	 */
11285331Samw 
11295331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
11305331Samw 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
11315331Samw 	    IS_EPHEMERAL(crgetuid(cr)) || IS_EPHEMERAL(crgetgid(cr))))
11325331Samw 		return (EINVAL);
1133789Sahrens 
11345367Sahrens 	ZFS_ENTER(zfsvfs);
11355367Sahrens 	ZFS_VERIFY_ZP(dzp);
11365326Sek110237 	os = zfsvfs->z_os;
11375326Sek110237 	zilog = zfsvfs->z_log;
1138789Sahrens 
11395498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
11405331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
11415331Samw 		ZFS_EXIT(zfsvfs);
11425331Samw 		return (EILSEQ);
11435331Samw 	}
11445331Samw 
11455331Samw 	if (vap->va_mask & AT_XVATTR) {
11465331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
11475331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
11485331Samw 			ZFS_EXIT(zfsvfs);
11495331Samw 			return (error);
11505331Samw 		}
11515331Samw 	}
1152789Sahrens top:
1153789Sahrens 	*vpp = NULL;
1154789Sahrens 
1155789Sahrens 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1156789Sahrens 		vap->va_mode &= ~VSVTX;
1157789Sahrens 
1158789Sahrens 	if (*name == '\0') {
1159789Sahrens 		/*
1160789Sahrens 		 * Null component name refers to the directory itself.
1161789Sahrens 		 */
1162789Sahrens 		VN_HOLD(dvp);
1163789Sahrens 		zp = dzp;
1164789Sahrens 		dl = NULL;
1165789Sahrens 		error = 0;
1166789Sahrens 	} else {
1167789Sahrens 		/* possible VN_HOLD(zp) */
11685331Samw 		int zflg = 0;
11695331Samw 
11705331Samw 		if (flag & FIGNORECASE)
11715331Samw 			zflg |= ZCILOOK;
11725331Samw 
11735331Samw 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
11745331Samw 		    NULL, NULL);
11755331Samw 		if (error) {
1176789Sahrens 			if (strcmp(name, "..") == 0)
1177789Sahrens 				error = EISDIR;
1178789Sahrens 			ZFS_EXIT(zfsvfs);
11795331Samw 			if (aclp)
11805331Samw 				zfs_acl_free(aclp);
1181789Sahrens 			return (error);
1182789Sahrens 		}
1183789Sahrens 	}
11845331Samw 	if (vsecp && aclp == NULL) {
11855331Samw 		error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp);
11865331Samw 		if (error) {
11875331Samw 			ZFS_EXIT(zfsvfs);
11885331Samw 			if (dl)
11895331Samw 				zfs_dirent_unlock(dl);
11905331Samw 			return (error);
11915331Samw 		}
11925331Samw 	}
1193789Sahrens 
1194789Sahrens 	if (zp == NULL) {
11955331Samw 		uint64_t txtype;
11965331Samw 
1197789Sahrens 		/*
1198789Sahrens 		 * Create a new file object and update the directory
1199789Sahrens 		 * to reference it.
1200789Sahrens 		 */
12015331Samw 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1202789Sahrens 			goto out;
1203789Sahrens 		}
1204789Sahrens 
1205789Sahrens 		/*
1206789Sahrens 		 * We only support the creation of regular files in
1207789Sahrens 		 * extended attribute directories.
1208789Sahrens 		 */
1209789Sahrens 		if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1210789Sahrens 		    (vap->va_type != VREG)) {
1211789Sahrens 			error = EINVAL;
1212789Sahrens 			goto out;
1213789Sahrens 		}
1214789Sahrens 
1215789Sahrens 		tx = dmu_tx_create(os);
1216789Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
12175331Samw 		if (zfsvfs->z_fuid_obj == 0) {
12185331Samw 			dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
12195331Samw 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
12205331Samw 			    SPA_MAXBLOCKSIZE);
12215331Samw 			dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
12225331Samw 		} else {
12235331Samw 			dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
12245331Samw 			dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
12255331Samw 			    SPA_MAXBLOCKSIZE);
12265331Samw 		}
1227789Sahrens 		dmu_tx_hold_bonus(tx, dzp->z_id);
12281544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
12295331Samw 		if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp) {
1230789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1231789Sahrens 			    0, SPA_MAXBLOCKSIZE);
12325331Samw 		}
1233789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
1234789Sahrens 		if (error) {
1235789Sahrens 			zfs_dirent_unlock(dl);
1236789Sahrens 			if (error == ERESTART &&
1237789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
12382113Sahrens 				dmu_tx_wait(tx);
12392113Sahrens 				dmu_tx_abort(tx);
1240789Sahrens 				goto top;
1241789Sahrens 			}
12422113Sahrens 			dmu_tx_abort(tx);
1243789Sahrens 			ZFS_EXIT(zfsvfs);
12445331Samw 			if (aclp)
12455331Samw 				zfs_acl_free(aclp);
1246789Sahrens 			return (error);
1247789Sahrens 		}
12485446Sahrens 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, aclp, &fuidp);
1249789Sahrens 		(void) zfs_link_create(dl, zp, tx, ZNEW);
12505331Samw 		txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
12515331Samw 		if (flag & FIGNORECASE)
12525331Samw 			txtype |= TX_CI;
12535331Samw 		zfs_log_create(zilog, tx, txtype, dzp, zp, name,
12545331Samw 		    vsecp, fuidp, vap);
12555331Samw 		if (fuidp)
12565331Samw 			zfs_fuid_info_free(fuidp);
1257789Sahrens 		dmu_tx_commit(tx);
1258789Sahrens 	} else {
12595331Samw 		int aflags = (flag & FAPPEND) ? V_APPEND : 0;
12605331Samw 
1261789Sahrens 		/*
1262789Sahrens 		 * A directory entry already exists for this name.
1263789Sahrens 		 */
1264789Sahrens 		/*
1265789Sahrens 		 * Can't truncate an existing file if in exclusive mode.
1266789Sahrens 		 */
1267789Sahrens 		if (excl == EXCL) {
1268789Sahrens 			error = EEXIST;
1269789Sahrens 			goto out;
1270789Sahrens 		}
1271789Sahrens 		/*
1272789Sahrens 		 * Can't open a directory for writing.
1273789Sahrens 		 */
1274789Sahrens 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1275789Sahrens 			error = EISDIR;
1276789Sahrens 			goto out;
1277789Sahrens 		}
1278789Sahrens 		/*
1279789Sahrens 		 * Verify requested access to file.
1280789Sahrens 		 */
12815331Samw 		if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1282789Sahrens 			goto out;
1283789Sahrens 		}
1284789Sahrens 
1285789Sahrens 		mutex_enter(&dzp->z_lock);
1286789Sahrens 		dzp->z_seq++;
1287789Sahrens 		mutex_exit(&dzp->z_lock);
1288789Sahrens 
12891878Smaybee 		/*
12901878Smaybee 		 * Truncate regular files if requested.
12911878Smaybee 		 */
12921878Smaybee 		if ((ZTOV(zp)->v_type == VREG) &&
1293789Sahrens 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
12941878Smaybee 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
12951878Smaybee 			if (error == ERESTART &&
12961878Smaybee 			    zfsvfs->z_assign == TXG_NOWAIT) {
12972113Sahrens 				/* NB: we already did dmu_tx_wait() */
12981878Smaybee 				zfs_dirent_unlock(dl);
12992365Sperrin 				VN_RELE(ZTOV(zp));
13001878Smaybee 				goto top;
1301789Sahrens 			}
13024863Spraks 
13034863Spraks 			if (error == 0) {
13045331Samw 				vnevent_create(ZTOV(zp), ct);
13054863Spraks 			}
1306789Sahrens 		}
1307789Sahrens 	}
1308789Sahrens out:
1309789Sahrens 
1310789Sahrens 	if (dl)
1311789Sahrens 		zfs_dirent_unlock(dl);
1312789Sahrens 
1313789Sahrens 	if (error) {
1314789Sahrens 		if (zp)
1315789Sahrens 			VN_RELE(ZTOV(zp));
1316789Sahrens 	} else {
1317789Sahrens 		*vpp = ZTOV(zp);
1318789Sahrens 		/*
1319789Sahrens 		 * If vnode is for a device return a specfs vnode instead.
1320789Sahrens 		 */
1321789Sahrens 		if (IS_DEVVP(*vpp)) {
1322789Sahrens 			struct vnode *svp;
1323789Sahrens 
1324789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1325789Sahrens 			VN_RELE(*vpp);
1326789Sahrens 			if (svp == NULL) {
1327789Sahrens 				error = ENOSYS;
1328789Sahrens 			}
1329789Sahrens 			*vpp = svp;
1330789Sahrens 		}
1331789Sahrens 	}
13325331Samw 	if (aclp)
13335331Samw 		zfs_acl_free(aclp);
1334789Sahrens 
1335789Sahrens 	ZFS_EXIT(zfsvfs);
1336789Sahrens 	return (error);
1337789Sahrens }
1338789Sahrens 
1339789Sahrens /*
1340789Sahrens  * Remove an entry from a directory.
1341789Sahrens  *
1342789Sahrens  *	IN:	dvp	- vnode of directory to remove entry from.
1343789Sahrens  *		name	- name of entry to remove.
1344789Sahrens  *		cr	- credentials of caller.
13455331Samw  *		ct	- caller context
13465331Samw  *		flags	- case flags
1347789Sahrens  *
1348789Sahrens  *	RETURN:	0 if success
1349789Sahrens  *		error code if failure
1350789Sahrens  *
1351789Sahrens  * Timestamps:
1352789Sahrens  *	dvp - ctime|mtime
1353789Sahrens  *	 vp - ctime (if nlink > 0)
1354789Sahrens  */
13555331Samw /*ARGSUSED*/
1356789Sahrens static int
13575331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
13585331Samw     int flags)
1359789Sahrens {
1360789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1361789Sahrens 	znode_t		*xzp = NULL;
1362789Sahrens 	vnode_t		*vp;
1363789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
13645326Sek110237 	zilog_t		*zilog;
1365789Sahrens 	uint64_t	acl_obj, xattr_obj;
1366789Sahrens 	zfs_dirlock_t	*dl;
1367789Sahrens 	dmu_tx_t	*tx;
13683461Sahrens 	boolean_t	may_delete_now, delete_now = FALSE;
13693461Sahrens 	boolean_t	unlinked;
13705331Samw 	uint64_t	txtype;
13715331Samw 	pathname_t	*realnmp = NULL;
13725331Samw 	pathname_t	realnm;
1373789Sahrens 	int		error;
13745331Samw 	int		zflg = ZEXISTS;
1375789Sahrens 
13765367Sahrens 	ZFS_ENTER(zfsvfs);
13775367Sahrens 	ZFS_VERIFY_ZP(dzp);
13785326Sek110237 	zilog = zfsvfs->z_log;
1379789Sahrens 
13805331Samw 	if (flags & FIGNORECASE) {
13815331Samw 		zflg |= ZCILOOK;
13825331Samw 		pn_alloc(&realnm);
13835331Samw 		realnmp = &realnm;
13845331Samw 	}
13855331Samw 
1386789Sahrens top:
1387789Sahrens 	/*
1388789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1389789Sahrens 	 */
13905331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
13915331Samw 	    NULL, realnmp)) {
13925331Samw 		if (realnmp)
13935331Samw 			pn_free(realnmp);
1394789Sahrens 		ZFS_EXIT(zfsvfs);
1395789Sahrens 		return (error);
1396789Sahrens 	}
1397789Sahrens 
1398789Sahrens 	vp = ZTOV(zp);
1399789Sahrens 
1400789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1401789Sahrens 		goto out;
1402789Sahrens 	}
1403789Sahrens 
1404789Sahrens 	/*
1405789Sahrens 	 * Need to use rmdir for removing directories.
1406789Sahrens 	 */
1407789Sahrens 	if (vp->v_type == VDIR) {
1408789Sahrens 		error = EPERM;
1409789Sahrens 		goto out;
1410789Sahrens 	}
1411789Sahrens 
14125331Samw 	vnevent_remove(vp, dvp, name, ct);
14135331Samw 
14145331Samw 	if (realnmp)
14155331Samw 		dnlc_remove(dvp, realnmp->pn_path);
14165331Samw 	else
14175331Samw 		dnlc_remove(dvp, name);
14181484Sek110237 
1419789Sahrens 	mutex_enter(&vp->v_lock);
1420789Sahrens 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1421789Sahrens 	mutex_exit(&vp->v_lock);
1422789Sahrens 
1423789Sahrens 	/*
14243461Sahrens 	 * We may delete the znode now, or we may put it in the unlinked set;
1425789Sahrens 	 * it depends on whether we're the last link, and on whether there are
1426789Sahrens 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1427789Sahrens 	 * allow for either case.
1428789Sahrens 	 */
1429789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
14301544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1431789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
1432789Sahrens 	if (may_delete_now)
1433789Sahrens 		dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
1434789Sahrens 
1435789Sahrens 	/* are there any extended attributes? */
1436789Sahrens 	if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1437789Sahrens 		/* XXX - do we need this if we are deleting? */
1438789Sahrens 		dmu_tx_hold_bonus(tx, xattr_obj);
1439789Sahrens 	}
1440789Sahrens 
1441789Sahrens 	/* are there any additional acls */
1442789Sahrens 	if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1443789Sahrens 	    may_delete_now)
1444789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1445789Sahrens 
1446789Sahrens 	/* charge as an update -- would be nice not to charge at all */
14473461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1448789Sahrens 
1449789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1450789Sahrens 	if (error) {
1451789Sahrens 		zfs_dirent_unlock(dl);
1452789Sahrens 		VN_RELE(vp);
1453789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
14542113Sahrens 			dmu_tx_wait(tx);
14552113Sahrens 			dmu_tx_abort(tx);
1456789Sahrens 			goto top;
1457789Sahrens 		}
14585331Samw 		if (realnmp)
14595331Samw 			pn_free(realnmp);
14602113Sahrens 		dmu_tx_abort(tx);
1461789Sahrens 		ZFS_EXIT(zfsvfs);
1462789Sahrens 		return (error);
1463789Sahrens 	}
1464789Sahrens 
1465789Sahrens 	/*
1466789Sahrens 	 * Remove the directory entry.
1467789Sahrens 	 */
14685331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1469789Sahrens 
1470789Sahrens 	if (error) {
1471789Sahrens 		dmu_tx_commit(tx);
1472789Sahrens 		goto out;
1473789Sahrens 	}
1474789Sahrens 
14753461Sahrens 	if (unlinked) {
1476789Sahrens 		mutex_enter(&vp->v_lock);
1477789Sahrens 		delete_now = may_delete_now &&
1478789Sahrens 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1479789Sahrens 		    zp->z_phys->zp_xattr == xattr_obj &&
1480789Sahrens 		    zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1481789Sahrens 		mutex_exit(&vp->v_lock);
1482789Sahrens 	}
1483789Sahrens 
1484789Sahrens 	if (delete_now) {
1485789Sahrens 		if (zp->z_phys->zp_xattr) {
1486789Sahrens 			error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1487789Sahrens 			ASSERT3U(error, ==, 0);
1488789Sahrens 			ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1489789Sahrens 			dmu_buf_will_dirty(xzp->z_dbuf, tx);
1490789Sahrens 			mutex_enter(&xzp->z_lock);
14913461Sahrens 			xzp->z_unlinked = 1;
1492789Sahrens 			xzp->z_phys->zp_links = 0;
1493789Sahrens 			mutex_exit(&xzp->z_lock);
14943461Sahrens 			zfs_unlinked_add(xzp, tx);
1495789Sahrens 			zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1496789Sahrens 		}
1497789Sahrens 		mutex_enter(&zp->z_lock);
1498789Sahrens 		mutex_enter(&vp->v_lock);
1499789Sahrens 		vp->v_count--;
1500789Sahrens 		ASSERT3U(vp->v_count, ==, 0);
1501789Sahrens 		mutex_exit(&vp->v_lock);
1502789Sahrens 		mutex_exit(&zp->z_lock);
1503789Sahrens 		zfs_znode_delete(zp, tx);
15043461Sahrens 	} else if (unlinked) {
15053461Sahrens 		zfs_unlinked_add(zp, tx);
1506789Sahrens 	}
1507789Sahrens 
15085331Samw 	txtype = TX_REMOVE;
15095331Samw 	if (flags & FIGNORECASE)
15105331Samw 		txtype |= TX_CI;
15115331Samw 	zfs_log_remove(zilog, tx, txtype, dzp, name);
1512789Sahrens 
1513789Sahrens 	dmu_tx_commit(tx);
1514789Sahrens out:
15155331Samw 	if (realnmp)
15165331Samw 		pn_free(realnmp);
15175331Samw 
1518789Sahrens 	zfs_dirent_unlock(dl);
1519789Sahrens 
1520789Sahrens 	if (!delete_now) {
1521789Sahrens 		VN_RELE(vp);
1522789Sahrens 	} else if (xzp) {
1523789Sahrens 		/* this rele delayed to prevent nesting transactions */
1524789Sahrens 		VN_RELE(ZTOV(xzp));
1525789Sahrens 	}
1526789Sahrens 
1527789Sahrens 	ZFS_EXIT(zfsvfs);
1528789Sahrens 	return (error);
1529789Sahrens }
1530789Sahrens 
1531789Sahrens /*
1532789Sahrens  * Create a new directory and insert it into dvp using the name
1533789Sahrens  * provided.  Return a pointer to the inserted directory.
1534789Sahrens  *
1535789Sahrens  *	IN:	dvp	- vnode of directory to add subdir to.
1536789Sahrens  *		dirname	- name of new directory.
1537789Sahrens  *		vap	- attributes of new directory.
1538789Sahrens  *		cr	- credentials of caller.
15395331Samw  *		ct	- caller context
15405331Samw  *		vsecp	- ACL to be set
1541789Sahrens  *
1542789Sahrens  *	OUT:	vpp	- vnode of created directory.
1543789Sahrens  *
1544789Sahrens  *	RETURN:	0 if success
1545789Sahrens  *		error code if failure
1546789Sahrens  *
1547789Sahrens  * Timestamps:
1548789Sahrens  *	dvp - ctime|mtime updated
1549789Sahrens  *	 vp - ctime|mtime|atime updated
1550789Sahrens  */
15515331Samw /*ARGSUSED*/
1552789Sahrens static int
15535331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
15545331Samw     caller_context_t *ct, int flags, vsecattr_t *vsecp)
1555789Sahrens {
1556789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1557789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
15585326Sek110237 	zilog_t		*zilog;
1559789Sahrens 	zfs_dirlock_t	*dl;
15605331Samw 	uint64_t	txtype;
1561789Sahrens 	dmu_tx_t	*tx;
1562789Sahrens 	int		error;
15635331Samw 	zfs_acl_t	*aclp = NULL;
15645331Samw 	zfs_fuid_info_t	*fuidp = NULL;
15655331Samw 	int		zf = ZNEW;
1566789Sahrens 
1567789Sahrens 	ASSERT(vap->va_type == VDIR);
1568789Sahrens 
15695331Samw 	/*
15705331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
15715331Samw 	 * make sure file system is at proper version
15725331Samw 	 */
15735331Samw 
15745331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
15755331Samw 	    (vsecp || (vap->va_mask & AT_XVATTR) || IS_EPHEMERAL(crgetuid(cr))||
15765331Samw 	    IS_EPHEMERAL(crgetgid(cr))))
15775331Samw 		return (EINVAL);
15785331Samw 
15795367Sahrens 	ZFS_ENTER(zfsvfs);
15805367Sahrens 	ZFS_VERIFY_ZP(dzp);
15815326Sek110237 	zilog = zfsvfs->z_log;
1582789Sahrens 
1583789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1584789Sahrens 		ZFS_EXIT(zfsvfs);
1585789Sahrens 		return (EINVAL);
1586789Sahrens 	}
15875331Samw 
15885498Stimh 	if (zfsvfs->z_utf8 && u8_validate(dirname,
15895331Samw 	    strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
15905331Samw 		ZFS_EXIT(zfsvfs);
15915331Samw 		return (EILSEQ);
15925331Samw 	}
15935331Samw 	if (flags & FIGNORECASE)
15945331Samw 		zf |= ZCILOOK;
15955331Samw 
15965331Samw 	if (vap->va_mask & AT_XVATTR)
15975331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
15985331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
15995331Samw 			ZFS_EXIT(zfsvfs);
16005331Samw 			return (error);
16015331Samw 		}
1602789Sahrens 
1603789Sahrens 	/*
1604789Sahrens 	 * First make sure the new directory doesn't exist.
1605789Sahrens 	 */
16065331Samw top:
16075331Samw 	*vpp = NULL;
16085331Samw 
16095331Samw 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
16105331Samw 	    NULL, NULL)) {
1611789Sahrens 		ZFS_EXIT(zfsvfs);
1612789Sahrens 		return (error);
1613789Sahrens 	}
1614789Sahrens 
16155331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
16161231Smarks 		zfs_dirent_unlock(dl);
16171231Smarks 		ZFS_EXIT(zfsvfs);
16181231Smarks 		return (error);
16191231Smarks 	}
16201231Smarks 
16215331Samw 	if (vsecp && aclp == NULL) {
16225331Samw 		error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp);
16235331Samw 		if (error) {
16245331Samw 			zfs_dirent_unlock(dl);
16255331Samw 			ZFS_EXIT(zfsvfs);
16265331Samw 			return (error);
16275331Samw 		}
16285331Samw 	}
1629789Sahrens 	/*
1630789Sahrens 	 * Add a new entry to the directory.
1631789Sahrens 	 */
1632789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
16331544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
16341544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
16355331Samw 	if (zfsvfs->z_fuid_obj == 0) {
16365331Samw 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
16375331Samw 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
16385331Samw 		    SPA_MAXBLOCKSIZE);
16395331Samw 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
16405331Samw 	} else {
16415331Samw 		dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
16425331Samw 		dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
16435331Samw 		    SPA_MAXBLOCKSIZE);
16445331Samw 	}
16455331Samw 	if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp)
1646789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1647789Sahrens 		    0, SPA_MAXBLOCKSIZE);
1648789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1649789Sahrens 	if (error) {
1650789Sahrens 		zfs_dirent_unlock(dl);
1651789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
16522113Sahrens 			dmu_tx_wait(tx);
16532113Sahrens 			dmu_tx_abort(tx);
1654789Sahrens 			goto top;
1655789Sahrens 		}
16562113Sahrens 		dmu_tx_abort(tx);
1657789Sahrens 		ZFS_EXIT(zfsvfs);
16585331Samw 		if (aclp)
16595331Samw 			zfs_acl_free(aclp);
1660789Sahrens 		return (error);
1661789Sahrens 	}
1662789Sahrens 
1663789Sahrens 	/*
1664789Sahrens 	 * Create new node.
1665789Sahrens 	 */
16665446Sahrens 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, aclp, &fuidp);
16675331Samw 
16685331Samw 	if (aclp)
16695331Samw 		zfs_acl_free(aclp);
1670789Sahrens 
1671789Sahrens 	/*
1672789Sahrens 	 * Now put new name in parent dir.
1673789Sahrens 	 */
1674789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1675789Sahrens 
1676789Sahrens 	*vpp = ZTOV(zp);
1677789Sahrens 
16785331Samw 	txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
16795331Samw 	if (flags & FIGNORECASE)
16805331Samw 		txtype |= TX_CI;
16815331Samw 	zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, fuidp, vap);
16825331Samw 
16835331Samw 	if (fuidp)
16845331Samw 		zfs_fuid_info_free(fuidp);
1685789Sahrens 	dmu_tx_commit(tx);
1686789Sahrens 
1687789Sahrens 	zfs_dirent_unlock(dl);
1688789Sahrens 
1689789Sahrens 	ZFS_EXIT(zfsvfs);
1690789Sahrens 	return (0);
1691789Sahrens }
1692789Sahrens 
1693789Sahrens /*
1694789Sahrens  * Remove a directory subdir entry.  If the current working
1695789Sahrens  * directory is the same as the subdir to be removed, the
1696789Sahrens  * remove will fail.
1697789Sahrens  *
1698789Sahrens  *	IN:	dvp	- vnode of directory to remove from.
1699789Sahrens  *		name	- name of directory to be removed.
1700789Sahrens  *		cwd	- vnode of current working directory.
1701789Sahrens  *		cr	- credentials of caller.
17025331Samw  *		ct	- caller context
17035331Samw  *		flags	- case flags
1704789Sahrens  *
1705789Sahrens  *	RETURN:	0 if success
1706789Sahrens  *		error code if failure
1707789Sahrens  *
1708789Sahrens  * Timestamps:
1709789Sahrens  *	dvp - ctime|mtime updated
1710789Sahrens  */
17115331Samw /*ARGSUSED*/
1712789Sahrens static int
17135331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
17145331Samw     caller_context_t *ct, int flags)
1715789Sahrens {
1716789Sahrens 	znode_t		*dzp = VTOZ(dvp);
1717789Sahrens 	znode_t		*zp;
1718789Sahrens 	vnode_t		*vp;
1719789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
17205326Sek110237 	zilog_t		*zilog;
1721789Sahrens 	zfs_dirlock_t	*dl;
1722789Sahrens 	dmu_tx_t	*tx;
1723789Sahrens 	int		error;
17245331Samw 	int		zflg = ZEXISTS;
1725789Sahrens 
17265367Sahrens 	ZFS_ENTER(zfsvfs);
17275367Sahrens 	ZFS_VERIFY_ZP(dzp);
17285326Sek110237 	zilog = zfsvfs->z_log;
1729789Sahrens 
17305331Samw 	if (flags & FIGNORECASE)
17315331Samw 		zflg |= ZCILOOK;
1732789Sahrens top:
1733789Sahrens 	zp = NULL;
1734789Sahrens 
1735789Sahrens 	/*
1736789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1737789Sahrens 	 */
17385331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
17395331Samw 	    NULL, NULL)) {
1740789Sahrens 		ZFS_EXIT(zfsvfs);
1741789Sahrens 		return (error);
1742789Sahrens 	}
1743789Sahrens 
1744789Sahrens 	vp = ZTOV(zp);
1745789Sahrens 
1746789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1747789Sahrens 		goto out;
1748789Sahrens 	}
1749789Sahrens 
1750789Sahrens 	if (vp->v_type != VDIR) {
1751789Sahrens 		error = ENOTDIR;
1752789Sahrens 		goto out;
1753789Sahrens 	}
1754789Sahrens 
1755789Sahrens 	if (vp == cwd) {
1756789Sahrens 		error = EINVAL;
1757789Sahrens 		goto out;
1758789Sahrens 	}
1759789Sahrens 
17605331Samw 	vnevent_rmdir(vp, dvp, name, ct);
1761789Sahrens 
1762789Sahrens 	/*
17633897Smaybee 	 * Grab a lock on the directory to make sure that noone is
17643897Smaybee 	 * trying to add (or lookup) entries while we are removing it.
17653897Smaybee 	 */
17663897Smaybee 	rw_enter(&zp->z_name_lock, RW_WRITER);
17673897Smaybee 
17683897Smaybee 	/*
17693897Smaybee 	 * Grab a lock on the parent pointer to make sure we play well
1770789Sahrens 	 * with the treewalk and directory rename code.
1771789Sahrens 	 */
1772789Sahrens 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1773789Sahrens 
1774789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
17751544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1776789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
17773461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1778789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1779789Sahrens 	if (error) {
1780789Sahrens 		rw_exit(&zp->z_parent_lock);
17813897Smaybee 		rw_exit(&zp->z_name_lock);
1782789Sahrens 		zfs_dirent_unlock(dl);
1783789Sahrens 		VN_RELE(vp);
1784789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
17852113Sahrens 			dmu_tx_wait(tx);
17862113Sahrens 			dmu_tx_abort(tx);
1787789Sahrens 			goto top;
1788789Sahrens 		}
17892113Sahrens 		dmu_tx_abort(tx);
1790789Sahrens 		ZFS_EXIT(zfsvfs);
1791789Sahrens 		return (error);
1792789Sahrens 	}
1793789Sahrens 
17945331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
17955331Samw 
17965331Samw 	if (error == 0) {
17975331Samw 		uint64_t txtype = TX_RMDIR;
17985331Samw 		if (flags & FIGNORECASE)
17995331Samw 			txtype |= TX_CI;
18005331Samw 		zfs_log_remove(zilog, tx, txtype, dzp, name);
18015331Samw 	}
1802789Sahrens 
1803789Sahrens 	dmu_tx_commit(tx);
1804789Sahrens 
1805789Sahrens 	rw_exit(&zp->z_parent_lock);
18063897Smaybee 	rw_exit(&zp->z_name_lock);
1807789Sahrens out:
1808789Sahrens 	zfs_dirent_unlock(dl);
1809789Sahrens 
1810789Sahrens 	VN_RELE(vp);
1811789Sahrens 
1812789Sahrens 	ZFS_EXIT(zfsvfs);
1813789Sahrens 	return (error);
1814789Sahrens }
1815789Sahrens 
1816789Sahrens /*
1817789Sahrens  * Read as many directory entries as will fit into the provided
1818789Sahrens  * buffer from the given directory cursor position (specified in
1819789Sahrens  * the uio structure.
1820789Sahrens  *
1821789Sahrens  *	IN:	vp	- vnode of directory to read.
1822789Sahrens  *		uio	- structure supplying read location, range info,
1823789Sahrens  *			  and return buffer.
1824789Sahrens  *		cr	- credentials of caller.
18255331Samw  *		ct	- caller context
18265331Samw  *		flags	- case flags
1827789Sahrens  *
1828789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
1829789Sahrens  *		eofp	- set to true if end-of-file detected.
1830789Sahrens  *
1831789Sahrens  *	RETURN:	0 if success
1832789Sahrens  *		error code if failure
1833789Sahrens  *
1834789Sahrens  * Timestamps:
1835789Sahrens  *	vp - atime updated
1836789Sahrens  *
1837789Sahrens  * Note that the low 4 bits of the cookie returned by zap is always zero.
1838789Sahrens  * This allows us to use the low range for "special" directory entries:
1839789Sahrens  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
1840789Sahrens  * we use the offset 2 for the '.zfs' directory.
1841789Sahrens  */
1842789Sahrens /* ARGSUSED */
1843789Sahrens static int
18445331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
18455331Samw     caller_context_t *ct, int flags)
1846789Sahrens {
1847789Sahrens 	znode_t		*zp = VTOZ(vp);
1848789Sahrens 	iovec_t		*iovp;
18495331Samw 	edirent_t	*eodp;
1850789Sahrens 	dirent64_t	*odp;
1851789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1852869Sperrin 	objset_t	*os;
1853789Sahrens 	caddr_t		outbuf;
1854789Sahrens 	size_t		bufsize;
1855789Sahrens 	zap_cursor_t	zc;
1856789Sahrens 	zap_attribute_t	zap;
1857789Sahrens 	uint_t		bytes_wanted;
1858789Sahrens 	uint64_t	offset; /* must be unsigned; checks for < 1 */
1859789Sahrens 	int		local_eof;
1860869Sperrin 	int		outcount;
1861869Sperrin 	int		error;
1862869Sperrin 	uint8_t		prefetch;
18635663Sck153898 	boolean_t	check_sysattrs;
1864789Sahrens 
18655367Sahrens 	ZFS_ENTER(zfsvfs);
18665367Sahrens 	ZFS_VERIFY_ZP(zp);
1867789Sahrens 
1868789Sahrens 	/*
1869789Sahrens 	 * If we are not given an eof variable,
1870789Sahrens 	 * use a local one.
1871789Sahrens 	 */
1872789Sahrens 	if (eofp == NULL)
1873789Sahrens 		eofp = &local_eof;
1874789Sahrens 
1875789Sahrens 	/*
1876789Sahrens 	 * Check for valid iov_len.
1877789Sahrens 	 */
1878789Sahrens 	if (uio->uio_iov->iov_len <= 0) {
1879789Sahrens 		ZFS_EXIT(zfsvfs);
1880789Sahrens 		return (EINVAL);
1881789Sahrens 	}
1882789Sahrens 
1883789Sahrens 	/*
1884789Sahrens 	 * Quit if directory has been removed (posix)
1885789Sahrens 	 */
18863461Sahrens 	if ((*eofp = zp->z_unlinked) != 0) {
1887789Sahrens 		ZFS_EXIT(zfsvfs);
1888789Sahrens 		return (0);
1889789Sahrens 	}
1890789Sahrens 
1891869Sperrin 	error = 0;
1892869Sperrin 	os = zfsvfs->z_os;
1893869Sperrin 	offset = uio->uio_loffset;
1894869Sperrin 	prefetch = zp->z_zn_prefetch;
1895869Sperrin 
1896789Sahrens 	/*
1897789Sahrens 	 * Initialize the iterator cursor.
1898789Sahrens 	 */
1899789Sahrens 	if (offset <= 3) {
1900789Sahrens 		/*
1901789Sahrens 		 * Start iteration from the beginning of the directory.
1902789Sahrens 		 */
1903869Sperrin 		zap_cursor_init(&zc, os, zp->z_id);
1904789Sahrens 	} else {
1905789Sahrens 		/*
1906789Sahrens 		 * The offset is a serialized cursor.
1907789Sahrens 		 */
1908869Sperrin 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1909789Sahrens 	}
1910789Sahrens 
1911789Sahrens 	/*
1912789Sahrens 	 * Get space to change directory entries into fs independent format.
1913789Sahrens 	 */
1914789Sahrens 	iovp = uio->uio_iov;
1915789Sahrens 	bytes_wanted = iovp->iov_len;
1916789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
1917789Sahrens 		bufsize = bytes_wanted;
1918789Sahrens 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
1919789Sahrens 		odp = (struct dirent64 *)outbuf;
1920789Sahrens 	} else {
1921789Sahrens 		bufsize = bytes_wanted;
1922789Sahrens 		odp = (struct dirent64 *)iovp->iov_base;
1923789Sahrens 	}
19245331Samw 	eodp = (struct edirent *)odp;
1925789Sahrens 
1926789Sahrens 	/*
19275663Sck153898 	 * If this VFS supports system attributes; and we're looking at an
19285663Sck153898 	 * extended attribute directory; and we care about normalization
19295663Sck153898 	 * conflicts on this vfs; then we must check for normalization
19305663Sck153898 	 * conflicts with the sysattr name space.
19315663Sck153898 	 */
19325663Sck153898 	check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) &&
19335663Sck153898 	    (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
19345663Sck153898 	    (flags & V_RDDIR_ENTFLAGS);
19355663Sck153898 
19365663Sck153898 	/*
1937789Sahrens 	 * Transform to file-system independent format
1938789Sahrens 	 */
1939789Sahrens 	outcount = 0;
1940789Sahrens 	while (outcount < bytes_wanted) {
19413912Slling 		ino64_t objnum;
19423912Slling 		ushort_t reclen;
19433912Slling 		off64_t *next;
19443912Slling 
1945789Sahrens 		/*
1946789Sahrens 		 * Special case `.', `..', and `.zfs'.
1947789Sahrens 		 */
1948789Sahrens 		if (offset == 0) {
1949789Sahrens 			(void) strcpy(zap.za_name, ".");
19505331Samw 			zap.za_normalization_conflict = 0;
19513912Slling 			objnum = zp->z_id;
1952789Sahrens 		} else if (offset == 1) {
1953789Sahrens 			(void) strcpy(zap.za_name, "..");
19545331Samw 			zap.za_normalization_conflict = 0;
19553912Slling 			objnum = zp->z_phys->zp_parent;
1956789Sahrens 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
1957789Sahrens 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
19585331Samw 			zap.za_normalization_conflict = 0;
19593912Slling 			objnum = ZFSCTL_INO_ROOT;
1960789Sahrens 		} else {
1961789Sahrens 			/*
1962789Sahrens 			 * Grab next entry.
1963789Sahrens 			 */
1964789Sahrens 			if (error = zap_cursor_retrieve(&zc, &zap)) {
1965789Sahrens 				if ((*eofp = (error == ENOENT)) != 0)
1966789Sahrens 					break;
1967789Sahrens 				else
1968789Sahrens 					goto update;
1969789Sahrens 			}
1970789Sahrens 
1971789Sahrens 			if (zap.za_integer_length != 8 ||
1972789Sahrens 			    zap.za_num_integers != 1) {
1973789Sahrens 				cmn_err(CE_WARN, "zap_readdir: bad directory "
1974789Sahrens 				    "entry, obj = %lld, offset = %lld\n",
1975789Sahrens 				    (u_longlong_t)zp->z_id,
1976789Sahrens 				    (u_longlong_t)offset);
1977789Sahrens 				error = ENXIO;
1978789Sahrens 				goto update;
1979789Sahrens 			}
19803912Slling 
19813912Slling 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
19823912Slling 			/*
19833912Slling 			 * MacOS X can extract the object type here such as:
19843912Slling 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
19853912Slling 			 */
19865663Sck153898 
19875663Sck153898 			if (check_sysattrs && !zap.za_normalization_conflict) {
19885663Sck153898 				zap.za_normalization_conflict =
19895663Sck153898 				    xattr_sysattr_casechk(zap.za_name);
19905663Sck153898 			}
1991789Sahrens 		}
19925331Samw 
19935331Samw 		if (flags & V_RDDIR_ENTFLAGS)
19945331Samw 			reclen = EDIRENT_RECLEN(strlen(zap.za_name));
19955331Samw 		else
19965331Samw 			reclen = DIRENT64_RECLEN(strlen(zap.za_name));
1997789Sahrens 
1998789Sahrens 		/*
1999789Sahrens 		 * Will this entry fit in the buffer?
2000789Sahrens 		 */
20013912Slling 		if (outcount + reclen > bufsize) {
2002789Sahrens 			/*
2003789Sahrens 			 * Did we manage to fit anything in the buffer?
2004789Sahrens 			 */
2005789Sahrens 			if (!outcount) {
2006789Sahrens 				error = EINVAL;
2007789Sahrens 				goto update;
2008789Sahrens 			}
2009789Sahrens 			break;
2010789Sahrens 		}
20115331Samw 		if (flags & V_RDDIR_ENTFLAGS) {
20125331Samw 			/*
20135331Samw 			 * Add extended flag entry:
20145331Samw 			 */
20155331Samw 			eodp->ed_ino = objnum;
20165331Samw 			eodp->ed_reclen = reclen;
20175331Samw 			/* NOTE: ed_off is the offset for the *next* entry */
20185331Samw 			next = &(eodp->ed_off);
20195331Samw 			eodp->ed_eflags = zap.za_normalization_conflict ?
20205331Samw 			    ED_CASE_CONFLICT : 0;
20215331Samw 			(void) strncpy(eodp->ed_name, zap.za_name,
20225331Samw 			    EDIRENT_NAMELEN(reclen));
20235331Samw 			eodp = (edirent_t *)((intptr_t)eodp + reclen);
20245331Samw 		} else {
20255331Samw 			/*
20265331Samw 			 * Add normal entry:
20275331Samw 			 */
20285331Samw 			odp->d_ino = objnum;
20295331Samw 			odp->d_reclen = reclen;
20305331Samw 			/* NOTE: d_off is the offset for the *next* entry */
20315331Samw 			next = &(odp->d_off);
20325331Samw 			(void) strncpy(odp->d_name, zap.za_name,
20335331Samw 			    DIRENT64_NAMELEN(reclen));
20345331Samw 			odp = (dirent64_t *)((intptr_t)odp + reclen);
20355331Samw 		}
20363912Slling 		outcount += reclen;
2037789Sahrens 
2038789Sahrens 		ASSERT(outcount <= bufsize);
2039789Sahrens 
2040789Sahrens 		/* Prefetch znode */
2041869Sperrin 		if (prefetch)
20423912Slling 			dmu_prefetch(os, objnum, 0, 0);
2043789Sahrens 
2044789Sahrens 		/*
2045789Sahrens 		 * Move to the next entry, fill in the previous offset.
2046789Sahrens 		 */
2047789Sahrens 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2048789Sahrens 			zap_cursor_advance(&zc);
2049789Sahrens 			offset = zap_cursor_serialize(&zc);
2050789Sahrens 		} else {
2051789Sahrens 			offset += 1;
2052789Sahrens 		}
2053789Sahrens 		*next = offset;
2054789Sahrens 	}
2055869Sperrin 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2056789Sahrens 
2057789Sahrens 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2058789Sahrens 		iovp->iov_base += outcount;
2059789Sahrens 		iovp->iov_len -= outcount;
2060789Sahrens 		uio->uio_resid -= outcount;
2061789Sahrens 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2062789Sahrens 		/*
2063789Sahrens 		 * Reset the pointer.
2064789Sahrens 		 */
2065789Sahrens 		offset = uio->uio_loffset;
2066789Sahrens 	}
2067789Sahrens 
2068789Sahrens update:
2069885Sahrens 	zap_cursor_fini(&zc);
2070789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2071789Sahrens 		kmem_free(outbuf, bufsize);
2072789Sahrens 
2073789Sahrens 	if (error == ENOENT)
2074789Sahrens 		error = 0;
2075789Sahrens 
2076789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2077789Sahrens 
2078789Sahrens 	uio->uio_loffset = offset;
2079789Sahrens 	ZFS_EXIT(zfsvfs);
2080789Sahrens 	return (error);
2081789Sahrens }
2082789Sahrens 
20834720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4;
20844720Sfr157268 
2085789Sahrens static int
20865331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2087789Sahrens {
2088789Sahrens 	znode_t	*zp = VTOZ(vp);
2089789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2090789Sahrens 
20911773Seschrock 	/*
20921773Seschrock 	 * Regardless of whether this is required for standards conformance,
20931773Seschrock 	 * this is the logical behavior when fsync() is called on a file with
20941773Seschrock 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
20951773Seschrock 	 * going to be pushed out as part of the zil_commit().
20961773Seschrock 	 */
20971773Seschrock 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
20981773Seschrock 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
20995331Samw 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
21001773Seschrock 
21014720Sfr157268 	(void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
21024720Sfr157268 
21035367Sahrens 	ZFS_ENTER(zfsvfs);
21045367Sahrens 	ZFS_VERIFY_ZP(zp);
21052638Sperrin 	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
2106789Sahrens 	ZFS_EXIT(zfsvfs);
2107789Sahrens 	return (0);
2108789Sahrens }
2109789Sahrens 
21105331Samw 
2111789Sahrens /*
2112789Sahrens  * Get the requested file attributes and place them in the provided
2113789Sahrens  * vattr structure.
2114789Sahrens  *
2115789Sahrens  *	IN:	vp	- vnode of file.
2116789Sahrens  *		vap	- va_mask identifies requested attributes.
21175331Samw  *			  If AT_XVATTR set, then optional attrs are requested
21185331Samw  *		flags	- ATTR_NOACLCHECK (CIFS server context)
2119789Sahrens  *		cr	- credentials of caller.
21205331Samw  *		ct	- caller context
2121789Sahrens  *
2122789Sahrens  *	OUT:	vap	- attribute values.
2123789Sahrens  *
2124789Sahrens  *	RETURN:	0 (always succeeds)
2125789Sahrens  */
2126789Sahrens /* ARGSUSED */
2127789Sahrens static int
21285331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
21295331Samw     caller_context_t *ct)
2130789Sahrens {
2131789Sahrens 	znode_t *zp = VTOZ(vp);
2132789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
21335326Sek110237 	znode_phys_t *pzp;
21345331Samw 	int	error = 0;
21354543Smarks 	uint64_t links;
21365331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
21375331Samw 	xoptattr_t *xoap = NULL;
21385331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2139789Sahrens 
21405367Sahrens 	ZFS_ENTER(zfsvfs);
21415367Sahrens 	ZFS_VERIFY_ZP(zp);
21425326Sek110237 	pzp = zp->z_phys;
2143789Sahrens 
21445331Samw 	mutex_enter(&zp->z_lock);
21455331Samw 
21465331Samw 	/*
21475331Samw 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
21485331Samw 	 * Also, if we are the owner don't bother, since owner should
21495331Samw 	 * always be allowed to read basic attributes of file.
21505331Samw 	 */
21515331Samw 	if (!(pzp->zp_flags & ZFS_ACL_TRIVIAL) &&
21525331Samw 	    (pzp->zp_uid != crgetuid(cr))) {
21535331Samw 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
21545331Samw 		    skipaclchk, cr)) {
21555331Samw 			mutex_exit(&zp->z_lock);
21565331Samw 			ZFS_EXIT(zfsvfs);
21575331Samw 			return (error);
21585331Samw 		}
21595331Samw 	}
21605331Samw 
2161789Sahrens 	/*
2162789Sahrens 	 * Return all attributes.  It's cheaper to provide the answer
2163789Sahrens 	 * than to determine whether we were asked the question.
2164789Sahrens 	 */
2165789Sahrens 
2166789Sahrens 	vap->va_type = vp->v_type;
2167789Sahrens 	vap->va_mode = pzp->zp_mode & MODEMASK;
2168*5771Sjp151216 	zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2169789Sahrens 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2170789Sahrens 	vap->va_nodeid = zp->z_id;
21714543Smarks 	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
21724543Smarks 		links = pzp->zp_links + 1;
21734543Smarks 	else
21744543Smarks 		links = pzp->zp_links;
21754543Smarks 	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
2176789Sahrens 	vap->va_size = pzp->zp_size;
21771816Smarks 	vap->va_rdev = vp->v_rdev;
2178789Sahrens 	vap->va_seq = zp->z_seq;
2179789Sahrens 
21805331Samw 	/*
21815331Samw 	 * Add in any requested optional attributes and the create time.
21825331Samw 	 * Also set the corresponding bits in the returned attribute bitmap.
21835331Samw 	 */
21845331Samw 	if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
21855331Samw 		if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
21865331Samw 			xoap->xoa_archive =
21875331Samw 			    ((pzp->zp_flags & ZFS_ARCHIVE) != 0);
21885331Samw 			XVA_SET_RTN(xvap, XAT_ARCHIVE);
21895331Samw 		}
21905331Samw 
21915331Samw 		if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
21925331Samw 			xoap->xoa_readonly =
21935331Samw 			    ((pzp->zp_flags & ZFS_READONLY) != 0);
21945331Samw 			XVA_SET_RTN(xvap, XAT_READONLY);
21955331Samw 		}
21965331Samw 
21975331Samw 		if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
21985331Samw 			xoap->xoa_system =
21995331Samw 			    ((pzp->zp_flags & ZFS_SYSTEM) != 0);
22005331Samw 			XVA_SET_RTN(xvap, XAT_SYSTEM);
22015331Samw 		}
22025331Samw 
22035331Samw 		if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
22045331Samw 			xoap->xoa_hidden =
22055331Samw 			    ((pzp->zp_flags & ZFS_HIDDEN) != 0);
22065331Samw 			XVA_SET_RTN(xvap, XAT_HIDDEN);
22075331Samw 		}
22085331Samw 
22095331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
22105331Samw 			xoap->xoa_nounlink =
22115331Samw 			    ((pzp->zp_flags & ZFS_NOUNLINK) != 0);
22125331Samw 			XVA_SET_RTN(xvap, XAT_NOUNLINK);
22135331Samw 		}
22145331Samw 
22155331Samw 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
22165331Samw 			xoap->xoa_immutable =
22175331Samw 			    ((pzp->zp_flags & ZFS_IMMUTABLE) != 0);
22185331Samw 			XVA_SET_RTN(xvap, XAT_IMMUTABLE);
22195331Samw 		}
22205331Samw 
22215331Samw 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
22225331Samw 			xoap->xoa_appendonly =
22235331Samw 			    ((pzp->zp_flags & ZFS_APPENDONLY) != 0);
22245331Samw 			XVA_SET_RTN(xvap, XAT_APPENDONLY);
22255331Samw 		}
22265331Samw 
22275331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
22285331Samw 			xoap->xoa_nodump =
22295331Samw 			    ((pzp->zp_flags & ZFS_NODUMP) != 0);
22305331Samw 			XVA_SET_RTN(xvap, XAT_NODUMP);
22315331Samw 		}
22325331Samw 
22335331Samw 		if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
22345331Samw 			xoap->xoa_opaque =
22355331Samw 			    ((pzp->zp_flags & ZFS_OPAQUE) != 0);
22365331Samw 			XVA_SET_RTN(xvap, XAT_OPAQUE);
22375331Samw 		}
22385331Samw 
22395331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
22405331Samw 			xoap->xoa_av_quarantined =
22415331Samw 			    ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0);
22425331Samw 			XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
22435331Samw 		}
22445331Samw 
22455331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
22465331Samw 			xoap->xoa_av_modified =
22475331Samw 			    ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0);
22485331Samw 			XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
22495331Samw 		}
22505331Samw 
22515331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
22525331Samw 		    vp->v_type == VREG &&
22535331Samw 		    (pzp->zp_flags & ZFS_BONUS_SCANSTAMP)) {
22545331Samw 			size_t len;
22555331Samw 			dmu_object_info_t doi;
22565331Samw 
22575331Samw 			/*
22585331Samw 			 * Only VREG files have anti-virus scanstamps, so we
22595331Samw 			 * won't conflict with symlinks in the bonus buffer.
22605331Samw 			 */
22615331Samw 			dmu_object_info_from_db(zp->z_dbuf, &doi);
22625331Samw 			len = sizeof (xoap->xoa_av_scanstamp) +
22635331Samw 			    sizeof (znode_phys_t);
22645331Samw 			if (len <= doi.doi_bonus_size) {
22655331Samw 				/*
22665331Samw 				 * pzp points to the start of the
22675331Samw 				 * znode_phys_t. pzp + 1 points to the
22685331Samw 				 * first byte after the znode_phys_t.
22695331Samw 				 */
22705331Samw 				(void) memcpy(xoap->xoa_av_scanstamp,
22715331Samw 				    pzp + 1,
22725331Samw 				    sizeof (xoap->xoa_av_scanstamp));
22735331Samw 				XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
22745331Samw 			}
22755331Samw 		}
22765331Samw 
22775331Samw 		if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
22785331Samw 			ZFS_TIME_DECODE(&xoap->xoa_createtime, pzp->zp_crtime);
22795331Samw 			XVA_SET_RTN(xvap, XAT_CREATETIME);
22805331Samw 		}
22815331Samw 	}
22825331Samw 
2283789Sahrens 	ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
2284789Sahrens 	ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
2285789Sahrens 	ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
2286789Sahrens 
2287789Sahrens 	mutex_exit(&zp->z_lock);
2288789Sahrens 
2289789Sahrens 	dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks);
2290789Sahrens 
2291789Sahrens 	if (zp->z_blksz == 0) {
2292789Sahrens 		/*
2293789Sahrens 		 * Block size hasn't been set; suggest maximal I/O transfers.
2294789Sahrens 		 */
2295789Sahrens 		vap->va_blksize = zfsvfs->z_max_blksz;
2296789Sahrens 	}
2297789Sahrens 
2298789Sahrens 	ZFS_EXIT(zfsvfs);
2299789Sahrens 	return (0);
2300789Sahrens }
2301789Sahrens 
2302789Sahrens /*
2303789Sahrens  * Set the file attributes to the values contained in the
2304789Sahrens  * vattr structure.
2305789Sahrens  *
2306789Sahrens  *	IN:	vp	- vnode of file to be modified.
2307789Sahrens  *		vap	- new attribute values.
23085331Samw  *			  If AT_XVATTR set, then optional attrs are being set
2309789Sahrens  *		flags	- ATTR_UTIME set if non-default time values provided.
23105331Samw  *			- ATTR_NOACLCHECK (CIFS context only).
2311789Sahrens  *		cr	- credentials of caller.
23125331Samw  *		ct	- caller context
2313789Sahrens  *
2314789Sahrens  *	RETURN:	0 if success
2315789Sahrens  *		error code if failure
2316789Sahrens  *
2317789Sahrens  * Timestamps:
2318789Sahrens  *	vp - ctime updated, mtime updated if size changed.
2319789Sahrens  */
2320789Sahrens /* ARGSUSED */
2321789Sahrens static int
2322789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2323789Sahrens 	caller_context_t *ct)
2324789Sahrens {
23255326Sek110237 	znode_t		*zp = VTOZ(vp);
23265326Sek110237 	znode_phys_t	*pzp;
2327789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
23285326Sek110237 	zilog_t		*zilog;
2329789Sahrens 	dmu_tx_t	*tx;
23301878Smaybee 	vattr_t		oldva;
2331789Sahrens 	uint_t		mask = vap->va_mask;
23321878Smaybee 	uint_t		saved_mask;
23332796Smarks 	int		trim_mask = 0;
2334789Sahrens 	uint64_t	new_mode;
23351231Smarks 	znode_t		*attrzp;
2336789Sahrens 	int		need_policy = FALSE;
2337789Sahrens 	int		err;
23385331Samw 	zfs_fuid_info_t *fuidp = NULL;
23395331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
23405331Samw 	xoptattr_t	*xoap;
23415331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2342789Sahrens 
2343789Sahrens 	if (mask == 0)
2344789Sahrens 		return (0);
2345789Sahrens 
2346789Sahrens 	if (mask & AT_NOSET)
2347789Sahrens 		return (EINVAL);
2348789Sahrens 
23495367Sahrens 	ZFS_ENTER(zfsvfs);
23505367Sahrens 	ZFS_VERIFY_ZP(zp);
23515331Samw 
23525331Samw 	pzp = zp->z_phys;
23535331Samw 	zilog = zfsvfs->z_log;
23545331Samw 
23555331Samw 	/*
23565331Samw 	 * Make sure that if we have ephemeral uid/gid or xvattr specified
23575331Samw 	 * that file system is at proper version level
23585331Samw 	 */
23595331Samw 
23605331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
23615331Samw 	    (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
23625331Samw 	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
23635386Stimh 	    (mask & AT_XVATTR))) {
23645386Stimh 		ZFS_EXIT(zfsvfs);
23655331Samw 		return (EINVAL);
23665386Stimh 	}
23675386Stimh 
23685386Stimh 	if (mask & AT_SIZE && vp->v_type == VDIR) {
23695386Stimh 		ZFS_EXIT(zfsvfs);
2370789Sahrens 		return (EISDIR);
23715386Stimh 	}
23725386Stimh 
23735386Stimh 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
23745386Stimh 		ZFS_EXIT(zfsvfs);
23751308Smarks 		return (EINVAL);
23765386Stimh 	}
23771308Smarks 
23785331Samw 	/*
23795331Samw 	 * If this is an xvattr_t, then get a pointer to the structure of
23805331Samw 	 * optional attributes.  If this is NULL, then we have a vattr_t.
23815331Samw 	 */
23825331Samw 	xoap = xva_getxoptattr(xvap);
23835331Samw 
23845331Samw 	/*
23855331Samw 	 * Immutable files can only alter immutable bit and atime
23865331Samw 	 */
23875331Samw 	if ((pzp->zp_flags & ZFS_IMMUTABLE) &&
23885331Samw 	    ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
23895386Stimh 	    ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
23905386Stimh 		ZFS_EXIT(zfsvfs);
23915331Samw 		return (EPERM);
23925386Stimh 	}
23935386Stimh 
23945386Stimh 	if ((mask & AT_SIZE) && (pzp->zp_flags & ZFS_READONLY)) {
23955386Stimh 		ZFS_EXIT(zfsvfs);
23965331Samw 		return (EPERM);
23975386Stimh 	}
2398789Sahrens 
2399789Sahrens top:
24001231Smarks 	attrzp = NULL;
2401789Sahrens 
2402789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2403789Sahrens 		ZFS_EXIT(zfsvfs);
2404789Sahrens 		return (EROFS);
2405789Sahrens 	}
2406789Sahrens 
2407789Sahrens 	/*
2408789Sahrens 	 * First validate permissions
2409789Sahrens 	 */
2410789Sahrens 
2411789Sahrens 	if (mask & AT_SIZE) {
24125331Samw 		err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2413789Sahrens 		if (err) {
2414789Sahrens 			ZFS_EXIT(zfsvfs);
2415789Sahrens 			return (err);
2416789Sahrens 		}
24171878Smaybee 		/*
24181878Smaybee 		 * XXX - Note, we are not providing any open
24191878Smaybee 		 * mode flags here (like FNDELAY), so we may
24201878Smaybee 		 * block if there are locks present... this
24211878Smaybee 		 * should be addressed in openat().
24221878Smaybee 		 */
24231878Smaybee 		do {
24241878Smaybee 			err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
24252113Sahrens 			/* NB: we already did dmu_tx_wait() if necessary */
24261878Smaybee 		} while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
24271878Smaybee 		if (err) {
24281878Smaybee 			ZFS_EXIT(zfsvfs);
24291878Smaybee 			return (err);
24301878Smaybee 		}
2431789Sahrens 	}
2432789Sahrens 
24335331Samw 	if (mask & (AT_ATIME|AT_MTIME) ||
24345331Samw 	    ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
24355331Samw 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
24365331Samw 	    XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
24375331Samw 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
24385331Samw 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM))))
24395331Samw 		need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
24405331Samw 		    skipaclchk, cr);
2441789Sahrens 
2442789Sahrens 	if (mask & (AT_UID|AT_GID)) {
2443789Sahrens 		int	idmask = (mask & (AT_UID|AT_GID));
2444789Sahrens 		int	take_owner;
2445789Sahrens 		int	take_group;
2446789Sahrens 
2447789Sahrens 		/*
2448913Smarks 		 * NOTE: even if a new mode is being set,
2449913Smarks 		 * we may clear S_ISUID/S_ISGID bits.
2450913Smarks 		 */
2451913Smarks 
2452913Smarks 		if (!(mask & AT_MODE))
2453913Smarks 			vap->va_mode = pzp->zp_mode;
2454913Smarks 
2455913Smarks 		/*
2456789Sahrens 		 * Take ownership or chgrp to group we are a member of
2457789Sahrens 		 */
2458789Sahrens 
2459789Sahrens 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
24605331Samw 		take_group = (mask & AT_GID) &&
24615331Samw 		    zfs_groupmember(zfsvfs, vap->va_gid, cr);
2462789Sahrens 
2463789Sahrens 		/*
2464789Sahrens 		 * If both AT_UID and AT_GID are set then take_owner and
2465789Sahrens 		 * take_group must both be set in order to allow taking
2466789Sahrens 		 * ownership.
2467789Sahrens 		 *
2468789Sahrens 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2469789Sahrens 		 *
2470789Sahrens 		 */
2471789Sahrens 
2472789Sahrens 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2473789Sahrens 		    ((idmask == AT_UID) && take_owner) ||
2474789Sahrens 		    ((idmask == AT_GID) && take_group)) {
24755331Samw 			if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
24765331Samw 			    skipaclchk, cr) == 0) {
2477789Sahrens 				/*
2478789Sahrens 				 * Remove setuid/setgid for non-privileged users
2479789Sahrens 				 */
24801115Smarks 				secpolicy_setid_clear(vap, cr);
24812796Smarks 				trim_mask = (mask & (AT_UID|AT_GID));
2482789Sahrens 			} else {
2483789Sahrens 				need_policy =  TRUE;
2484789Sahrens 			}
2485789Sahrens 		} else {
2486789Sahrens 			need_policy =  TRUE;
2487789Sahrens 		}
2488789Sahrens 	}
2489789Sahrens 
24902796Smarks 	mutex_enter(&zp->z_lock);
24912796Smarks 	oldva.va_mode = pzp->zp_mode;
2492*5771Sjp151216 	zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
24935331Samw 	if (mask & AT_XVATTR) {
24945331Samw 		if ((need_policy == FALSE) &&
24955331Samw 		    (XVA_ISSET_REQ(xvap, XAT_APPENDONLY) &&
24965331Samw 		    xoap->xoa_appendonly !=
24975331Samw 		    ((pzp->zp_flags & ZFS_APPENDONLY) != 0)) ||
24985331Samw 		    (XVA_ISSET_REQ(xvap, XAT_NOUNLINK) &&
24995331Samw 		    xoap->xoa_nounlink !=
25005331Samw 		    ((pzp->zp_flags & ZFS_NOUNLINK) != 0)) ||
25015331Samw 		    (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE) &&
25025331Samw 		    xoap->xoa_immutable !=
25035331Samw 		    ((pzp->zp_flags & ZFS_IMMUTABLE) != 0)) ||
25045331Samw 		    (XVA_ISSET_REQ(xvap, XAT_NODUMP) &&
25055331Samw 		    xoap->xoa_nodump !=
25065331Samw 		    ((pzp->zp_flags & ZFS_NODUMP) != 0)) ||
25075331Samw 		    (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED) &&
25085331Samw 		    xoap->xoa_av_modified !=
25095331Samw 		    ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0)) ||
25105545Smarks 		    ((XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED) &&
25115545Smarks 		    ((vp->v_type != VREG && xoap->xoa_av_quarantined) ||
25125331Samw 		    xoap->xoa_av_quarantined !=
25135545Smarks 		    ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0)))) ||
25145331Samw 		    (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) ||
25155331Samw 		    (XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
25165331Samw 			need_policy = TRUE;
25175331Samw 		}
25185331Samw 	}
25195331Samw 
25202796Smarks 	mutex_exit(&zp->z_lock);
25212796Smarks 
25222796Smarks 	if (mask & AT_MODE) {
25235331Samw 		if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
25242796Smarks 			err = secpolicy_setid_setsticky_clear(vp, vap,
25252796Smarks 			    &oldva, cr);
25262796Smarks 			if (err) {
25272796Smarks 				ZFS_EXIT(zfsvfs);
25282796Smarks 				return (err);
25292796Smarks 			}
25302796Smarks 			trim_mask |= AT_MODE;
25312796Smarks 		} else {
25322796Smarks 			need_policy = TRUE;
25332796Smarks 		}
25342796Smarks 	}
2535789Sahrens 
2536789Sahrens 	if (need_policy) {
25371115Smarks 		/*
25381115Smarks 		 * If trim_mask is set then take ownership
25392796Smarks 		 * has been granted or write_acl is present and user
25402796Smarks 		 * has the ability to modify mode.  In that case remove
25412796Smarks 		 * UID|GID and or MODE from mask so that
25421115Smarks 		 * secpolicy_vnode_setattr() doesn't revoke it.
25431115Smarks 		 */
25442796Smarks 
25452796Smarks 		if (trim_mask) {
25462796Smarks 			saved_mask = vap->va_mask;
25472796Smarks 			vap->va_mask &= ~trim_mask;
25482796Smarks 		}
2549789Sahrens 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
25505331Samw 		    (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2551789Sahrens 		if (err) {
2552789Sahrens 			ZFS_EXIT(zfsvfs);
2553789Sahrens 			return (err);
2554789Sahrens 		}
25551115Smarks 
25561115Smarks 		if (trim_mask)
25572796Smarks 			vap->va_mask |= saved_mask;
2558789Sahrens 	}
2559789Sahrens 
2560789Sahrens 	/*
2561789Sahrens 	 * secpolicy_vnode_setattr, or take ownership may have
2562789Sahrens 	 * changed va_mask
2563789Sahrens 	 */
2564789Sahrens 	mask = vap->va_mask;
2565789Sahrens 
2566789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2567789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
25685331Samw 	if (zfsvfs->z_fuid_obj == 0) {
25695331Samw 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
25705331Samw 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
25715331Samw 		    SPA_MAXBLOCKSIZE);
25725331Samw 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
25735331Samw 	} else {
25745331Samw 		dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
25755331Samw 		dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
25765331Samw 		    SPA_MAXBLOCKSIZE);
25775331Samw 	}
2578789Sahrens 
2579789Sahrens 	if (mask & AT_MODE) {
25801576Smarks 		uint64_t pmode = pzp->zp_mode;
25811576Smarks 
25821576Smarks 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2583789Sahrens 
25845331Samw 		if (pzp->zp_acl.z_acl_extern_obj) {
25855331Samw 			/* Are we upgrading ACL from old V0 format to new V1 */
25865331Samw 			if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
25875331Samw 			    pzp->zp_acl.z_acl_version ==
25885331Samw 			    ZFS_ACL_VERSION_INITIAL) {
25895331Samw 				dmu_tx_hold_free(tx,
25905331Samw 				    pzp->zp_acl.z_acl_extern_obj, 0,
25915331Samw 				    DMU_OBJECT_END);
25925331Samw 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
25935331Samw 				    0, sizeof (zfs_object_ace_t) * 2048 + 6);
25945331Samw 			} else {
25955331Samw 				dmu_tx_hold_write(tx,
25965331Samw 				    pzp->zp_acl.z_acl_extern_obj, 0,
25975331Samw 				    SPA_MAXBLOCKSIZE);
25985331Samw 			}
25995331Samw 		} else {
2600789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
26015331Samw 			    0, sizeof (zfs_object_ace_t) * 2048 + 6);
26025331Samw 		}
2603789Sahrens 	}
2604789Sahrens 
26055331Samw 	if ((mask & (AT_UID | AT_GID)) && pzp->zp_xattr != 0) {
26065331Samw 		err = zfs_zget(zp->z_zfsvfs, pzp->zp_xattr, &attrzp);
26071231Smarks 		if (err) {
26081231Smarks 			dmu_tx_abort(tx);
26091231Smarks 			ZFS_EXIT(zfsvfs);
26101231Smarks 			return (err);
26111231Smarks 		}
26121231Smarks 		dmu_tx_hold_bonus(tx, attrzp->z_id);
26131231Smarks 	}
26141231Smarks 
2615789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2616789Sahrens 	if (err) {
26171231Smarks 		if (attrzp)
26181231Smarks 			VN_RELE(ZTOV(attrzp));
2619789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
26202113Sahrens 			dmu_tx_wait(tx);
26212113Sahrens 			dmu_tx_abort(tx);
2622789Sahrens 			goto top;
2623789Sahrens 		}
26242113Sahrens 		dmu_tx_abort(tx);
2625789Sahrens 		ZFS_EXIT(zfsvfs);
2626789Sahrens 		return (err);
2627789Sahrens 	}
2628789Sahrens 
2629789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
2630789Sahrens 
2631789Sahrens 	/*
2632789Sahrens 	 * Set each attribute requested.
2633789Sahrens 	 * We group settings according to the locks they need to acquire.
2634789Sahrens 	 *
2635789Sahrens 	 * Note: you cannot set ctime directly, although it will be
2636789Sahrens 	 * updated as a side-effect of calling this function.
2637789Sahrens 	 */
2638789Sahrens 
2639789Sahrens 	mutex_enter(&zp->z_lock);
2640789Sahrens 
2641789Sahrens 	if (mask & AT_MODE) {
2642*5771Sjp151216 		err = zfs_acl_chmod_setattr(zp, new_mode, tx, cr);
2643789Sahrens 		ASSERT3U(err, ==, 0);
2644789Sahrens 	}
2645789Sahrens 
26461231Smarks 	if (attrzp)
26471231Smarks 		mutex_enter(&attrzp->z_lock);
26481231Smarks 
26491231Smarks 	if (mask & AT_UID) {
26505331Samw 		pzp->zp_uid = zfs_fuid_create(zfsvfs,
2651*5771Sjp151216 		    vap->va_uid, cr, ZFS_OWNER, tx, &fuidp);
26521231Smarks 		if (attrzp) {
26535331Samw 			attrzp->z_phys->zp_uid = zfs_fuid_create(zfsvfs,
2654*5771Sjp151216 			    vap->va_uid,  cr, ZFS_OWNER, tx, &fuidp);
26551231Smarks 		}
26561231Smarks 	}
26571231Smarks 
26581231Smarks 	if (mask & AT_GID) {
26595331Samw 		pzp->zp_gid = zfs_fuid_create(zfsvfs, vap->va_gid,
2660*5771Sjp151216 		    cr, ZFS_GROUP, tx, &fuidp);
26611231Smarks 		if (attrzp)
26625331Samw 			attrzp->z_phys->zp_gid = zfs_fuid_create(zfsvfs,
2663*5771Sjp151216 			    vap->va_gid, cr, ZFS_GROUP, tx, &fuidp);
26641231Smarks 	}
26651231Smarks 
26661231Smarks 	if (attrzp)
26671231Smarks 		mutex_exit(&attrzp->z_lock);
2668789Sahrens 
2669789Sahrens 	if (mask & AT_ATIME)
2670789Sahrens 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
2671789Sahrens 
2672789Sahrens 	if (mask & AT_MTIME)
2673789Sahrens 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
2674789Sahrens 
26751878Smaybee 	if (mask & AT_SIZE)
2676789Sahrens 		zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
26771878Smaybee 	else if (mask != 0)
2678789Sahrens 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
26795331Samw 	/*
26805331Samw 	 * Do this after setting timestamps to prevent timestamp
26815331Samw 	 * update from toggling bit
26825331Samw 	 */
26835331Samw 
26845331Samw 	if (xoap && (mask & AT_XVATTR)) {
26855331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
26865331Samw 			size_t len;
26875331Samw 			dmu_object_info_t doi;
26885331Samw 
26895331Samw 			ASSERT(vp->v_type == VREG);
26905331Samw 
26915331Samw 			/* Grow the bonus buffer if necessary. */
26925331Samw 			dmu_object_info_from_db(zp->z_dbuf, &doi);
26935331Samw 			len = sizeof (xoap->xoa_av_scanstamp) +
26945331Samw 			    sizeof (znode_phys_t);
26955331Samw 			if (len > doi.doi_bonus_size)
26965331Samw 				VERIFY(dmu_set_bonus(zp->z_dbuf, len, tx) == 0);
26975331Samw 		}
26985331Samw 		zfs_xvattr_set(zp, xvap);
26995331Samw 	}
2700789Sahrens 
27011878Smaybee 	if (mask != 0)
27025331Samw 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
27035331Samw 
27045331Samw 	if (fuidp)
27055331Samw 		zfs_fuid_info_free(fuidp);
2706789Sahrens 	mutex_exit(&zp->z_lock);
2707789Sahrens 
27081231Smarks 	if (attrzp)
27091231Smarks 		VN_RELE(ZTOV(attrzp));
27101231Smarks 
2711789Sahrens 	dmu_tx_commit(tx);
2712789Sahrens 
2713789Sahrens 	ZFS_EXIT(zfsvfs);
2714789Sahrens 	return (err);
2715789Sahrens }
2716789Sahrens 
27173271Smaybee typedef struct zfs_zlock {
27183271Smaybee 	krwlock_t	*zl_rwlock;	/* lock we acquired */
27193271Smaybee 	znode_t		*zl_znode;	/* znode we held */
27203271Smaybee 	struct zfs_zlock *zl_next;	/* next in list */
27213271Smaybee } zfs_zlock_t;
27223271Smaybee 
27233271Smaybee /*
27243271Smaybee  * Drop locks and release vnodes that were held by zfs_rename_lock().
27253271Smaybee  */
27263271Smaybee static void
27273271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp)
27283271Smaybee {
27293271Smaybee 	zfs_zlock_t *zl;
27303271Smaybee 
27313271Smaybee 	while ((zl = *zlpp) != NULL) {
27323271Smaybee 		if (zl->zl_znode != NULL)
27333271Smaybee 			VN_RELE(ZTOV(zl->zl_znode));
27343271Smaybee 		rw_exit(zl->zl_rwlock);
27353271Smaybee 		*zlpp = zl->zl_next;
27363271Smaybee 		kmem_free(zl, sizeof (*zl));
27373271Smaybee 	}
27383271Smaybee }
27393271Smaybee 
2740789Sahrens /*
2741789Sahrens  * Search back through the directory tree, using the ".." entries.
2742789Sahrens  * Lock each directory in the chain to prevent concurrent renames.
2743789Sahrens  * Fail any attempt to move a directory into one of its own descendants.
2744789Sahrens  * XXX - z_parent_lock can overlap with map or grow locks
2745789Sahrens  */
2746789Sahrens static int
2747789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
2748789Sahrens {
2749789Sahrens 	zfs_zlock_t	*zl;
27503638Sbillm 	znode_t		*zp = tdzp;
2751789Sahrens 	uint64_t	rootid = zp->z_zfsvfs->z_root;
2752789Sahrens 	uint64_t	*oidp = &zp->z_id;
2753789Sahrens 	krwlock_t	*rwlp = &szp->z_parent_lock;
2754789Sahrens 	krw_t		rw = RW_WRITER;
2755789Sahrens 
2756789Sahrens 	/*
2757789Sahrens 	 * First pass write-locks szp and compares to zp->z_id.
2758789Sahrens 	 * Later passes read-lock zp and compare to zp->z_parent.
2759789Sahrens 	 */
2760789Sahrens 	do {
27613271Smaybee 		if (!rw_tryenter(rwlp, rw)) {
27623271Smaybee 			/*
27633271Smaybee 			 * Another thread is renaming in this path.
27643271Smaybee 			 * Note that if we are a WRITER, we don't have any
27653271Smaybee 			 * parent_locks held yet.
27663271Smaybee 			 */
27673271Smaybee 			if (rw == RW_READER && zp->z_id > szp->z_id) {
27683271Smaybee 				/*
27693271Smaybee 				 * Drop our locks and restart
27703271Smaybee 				 */
27713271Smaybee 				zfs_rename_unlock(&zl);
27723271Smaybee 				*zlpp = NULL;
27733271Smaybee 				zp = tdzp;
27743271Smaybee 				oidp = &zp->z_id;
27753271Smaybee 				rwlp = &szp->z_parent_lock;
27763271Smaybee 				rw = RW_WRITER;
27773271Smaybee 				continue;
27783271Smaybee 			} else {
27793271Smaybee 				/*
27803271Smaybee 				 * Wait for other thread to drop its locks
27813271Smaybee 				 */
27823271Smaybee 				rw_enter(rwlp, rw);
27833271Smaybee 			}
27843271Smaybee 		}
27853271Smaybee 
2786789Sahrens 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
2787789Sahrens 		zl->zl_rwlock = rwlp;
2788789Sahrens 		zl->zl_znode = NULL;
2789789Sahrens 		zl->zl_next = *zlpp;
2790789Sahrens 		*zlpp = zl;
2791789Sahrens 
2792789Sahrens 		if (*oidp == szp->z_id)		/* We're a descendant of szp */
2793789Sahrens 			return (EINVAL);
2794789Sahrens 
2795789Sahrens 		if (*oidp == rootid)		/* We've hit the top */
2796789Sahrens 			return (0);
2797789Sahrens 
2798789Sahrens 		if (rw == RW_READER) {		/* i.e. not the first pass */
2799789Sahrens 			int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
2800789Sahrens 			if (error)
2801789Sahrens 				return (error);
2802789Sahrens 			zl->zl_znode = zp;
2803789Sahrens 		}
2804789Sahrens 		oidp = &zp->z_phys->zp_parent;
2805789Sahrens 		rwlp = &zp->z_parent_lock;
2806789Sahrens 		rw = RW_READER;
2807789Sahrens 
2808789Sahrens 	} while (zp->z_id != sdzp->z_id);
2809789Sahrens 
2810789Sahrens 	return (0);
2811789Sahrens }
2812789Sahrens 
2813789Sahrens /*
2814789Sahrens  * Move an entry from the provided source directory to the target
2815789Sahrens  * directory.  Change the entry name as indicated.
2816789Sahrens  *
2817789Sahrens  *	IN:	sdvp	- Source directory containing the "old entry".
2818789Sahrens  *		snm	- Old entry name.
2819789Sahrens  *		tdvp	- Target directory to contain the "new entry".
2820789Sahrens  *		tnm	- New entry name.
2821789Sahrens  *		cr	- credentials of caller.
28225331Samw  *		ct	- caller context
28235331Samw  *		flags	- case flags
2824789Sahrens  *
2825789Sahrens  *	RETURN:	0 if success
2826789Sahrens  *		error code if failure
2827789Sahrens  *
2828789Sahrens  * Timestamps:
2829789Sahrens  *	sdvp,tdvp - ctime|mtime updated
2830789Sahrens  */
28315331Samw /*ARGSUSED*/
2832789Sahrens static int
28335331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
28345331Samw     caller_context_t *ct, int flags)
2835789Sahrens {
2836789Sahrens 	znode_t		*tdzp, *szp, *tzp;
2837789Sahrens 	znode_t		*sdzp = VTOZ(sdvp);
2838789Sahrens 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
28395326Sek110237 	zilog_t		*zilog;
2840789Sahrens 	vnode_t		*realvp;
2841789Sahrens 	zfs_dirlock_t	*sdl, *tdl;
2842789Sahrens 	dmu_tx_t	*tx;
2843789Sahrens 	zfs_zlock_t	*zl;
28445331Samw 	int		cmp, serr, terr;
28455331Samw 	int		error = 0;
28465331Samw 	int		zflg = 0;
2847789Sahrens 
28485367Sahrens 	ZFS_ENTER(zfsvfs);
28495367Sahrens 	ZFS_VERIFY_ZP(sdzp);
28505326Sek110237 	zilog = zfsvfs->z_log;
2851789Sahrens 
2852789Sahrens 	/*
2853789Sahrens 	 * Make sure we have the real vp for the target directory.
2854789Sahrens 	 */
28555331Samw 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
2856789Sahrens 		tdvp = realvp;
2857789Sahrens 
2858789Sahrens 	if (tdvp->v_vfsp != sdvp->v_vfsp) {
2859789Sahrens 		ZFS_EXIT(zfsvfs);
2860789Sahrens 		return (EXDEV);
2861789Sahrens 	}
2862789Sahrens 
2863789Sahrens 	tdzp = VTOZ(tdvp);
28645367Sahrens 	ZFS_VERIFY_ZP(tdzp);
28655498Stimh 	if (zfsvfs->z_utf8 && u8_validate(tnm,
28665331Samw 	    strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
28675331Samw 		ZFS_EXIT(zfsvfs);
28685331Samw 		return (EILSEQ);
28695331Samw 	}
28705331Samw 
28715331Samw 	if (flags & FIGNORECASE)
28725331Samw 		zflg |= ZCILOOK;
28735331Samw 
2874789Sahrens top:
2875789Sahrens 	szp = NULL;
2876789Sahrens 	tzp = NULL;
2877789Sahrens 	zl = NULL;
2878789Sahrens 
2879789Sahrens 	/*
2880789Sahrens 	 * This is to prevent the creation of links into attribute space
2881789Sahrens 	 * by renaming a linked file into/outof an attribute directory.
2882789Sahrens 	 * See the comment in zfs_link() for why this is considered bad.
2883789Sahrens 	 */
2884789Sahrens 	if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
2885789Sahrens 	    (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
2886789Sahrens 		ZFS_EXIT(zfsvfs);
2887789Sahrens 		return (EINVAL);
2888789Sahrens 	}
2889789Sahrens 
2890789Sahrens 	/*
2891789Sahrens 	 * Lock source and target directory entries.  To prevent deadlock,
2892789Sahrens 	 * a lock ordering must be defined.  We lock the directory with
2893789Sahrens 	 * the smallest object id first, or if it's a tie, the one with
2894789Sahrens 	 * the lexically first name.
2895789Sahrens 	 */
2896789Sahrens 	if (sdzp->z_id < tdzp->z_id) {
2897789Sahrens 		cmp = -1;
2898789Sahrens 	} else if (sdzp->z_id > tdzp->z_id) {
2899789Sahrens 		cmp = 1;
2900789Sahrens 	} else {
29015331Samw 		/*
29025331Samw 		 * First compare the two name arguments without
29035331Samw 		 * considering any case folding.
29045331Samw 		 */
29055331Samw 		int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
29065331Samw 
29075331Samw 		cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
29085498Stimh 		ASSERT(error == 0 || !zfsvfs->z_utf8);
2909789Sahrens 		if (cmp == 0) {
2910789Sahrens 			/*
2911789Sahrens 			 * POSIX: "If the old argument and the new argument
2912789Sahrens 			 * both refer to links to the same existing file,
2913789Sahrens 			 * the rename() function shall return successfully
2914789Sahrens 			 * and perform no other action."
2915789Sahrens 			 */
2916789Sahrens 			ZFS_EXIT(zfsvfs);
2917789Sahrens 			return (0);
2918789Sahrens 		}
29195331Samw 		/*
29205331Samw 		 * If the file system is case-folding, then we may
29215331Samw 		 * have some more checking to do.  A case-folding file
29225331Samw 		 * system is either supporting mixed case sensitivity
29235331Samw 		 * access or is completely case-insensitive.  Note
29245331Samw 		 * that the file system is always case preserving.
29255331Samw 		 *
29265331Samw 		 * In mixed sensitivity mode case sensitive behavior
29275331Samw 		 * is the default.  FIGNORECASE must be used to
29285331Samw 		 * explicitly request case insensitive behavior.
29295331Samw 		 *
29305331Samw 		 * If the source and target names provided differ only
29315331Samw 		 * by case (e.g., a request to rename 'tim' to 'Tim'),
29325331Samw 		 * we will treat this as a special case in the
29335331Samw 		 * case-insensitive mode: as long as the source name
29345331Samw 		 * is an exact match, we will allow this to proceed as
29355331Samw 		 * a name-change request.
29365331Samw 		 */
29375498Stimh 		if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
29385498Stimh 		    (zfsvfs->z_case == ZFS_CASE_MIXED &&
29395498Stimh 		    flags & FIGNORECASE)) &&
29405331Samw 		    u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
29415331Samw 		    &error) == 0) {
29425331Samw 			/*
29435331Samw 			 * case preserving rename request, require exact
29445331Samw 			 * name matches
29455331Samw 			 */
29465331Samw 			zflg |= ZCIEXACT;
29475331Samw 			zflg &= ~ZCILOOK;
29485331Samw 		}
2949789Sahrens 	}
29505331Samw 
2951789Sahrens 	if (cmp < 0) {
29525331Samw 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
29535331Samw 		    ZEXISTS | zflg, NULL, NULL);
29545331Samw 		terr = zfs_dirent_lock(&tdl,
29555331Samw 		    tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
2956789Sahrens 	} else {
29575331Samw 		terr = zfs_dirent_lock(&tdl,
29585331Samw 		    tdzp, tnm, &tzp, zflg, NULL, NULL);
29595331Samw 		serr = zfs_dirent_lock(&sdl,
29605331Samw 		    sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
29615331Samw 		    NULL, NULL);
2962789Sahrens 	}
2963789Sahrens 
2964789Sahrens 	if (serr) {
2965789Sahrens 		/*
2966789Sahrens 		 * Source entry invalid or not there.
2967789Sahrens 		 */
2968789Sahrens 		if (!terr) {
2969789Sahrens 			zfs_dirent_unlock(tdl);
2970789Sahrens 			if (tzp)
2971789Sahrens 				VN_RELE(ZTOV(tzp));
2972789Sahrens 		}
2973789Sahrens 		if (strcmp(snm, "..") == 0)
2974789Sahrens 			serr = EINVAL;
2975789Sahrens 		ZFS_EXIT(zfsvfs);
2976789Sahrens 		return (serr);
2977789Sahrens 	}
2978789Sahrens 	if (terr) {
2979789Sahrens 		zfs_dirent_unlock(sdl);
2980789Sahrens 		VN_RELE(ZTOV(szp));
2981789Sahrens 		if (strcmp(tnm, "..") == 0)
2982789Sahrens 			terr = EINVAL;
2983789Sahrens 		ZFS_EXIT(zfsvfs);
2984789Sahrens 		return (terr);
2985789Sahrens 	}
2986789Sahrens 
2987789Sahrens 	/*
2988789Sahrens 	 * Must have write access at the source to remove the old entry
2989789Sahrens 	 * and write access at the target to create the new entry.
2990789Sahrens 	 * Note that if target and source are the same, this can be
2991789Sahrens 	 * done in a single check.
2992789Sahrens 	 */
2993789Sahrens 
2994789Sahrens 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
2995789Sahrens 		goto out;
2996789Sahrens 
2997789Sahrens 	if (ZTOV(szp)->v_type == VDIR) {
2998789Sahrens 		/*
2999789Sahrens 		 * Check to make sure rename is valid.
3000789Sahrens 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3001789Sahrens 		 */
3002789Sahrens 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3003789Sahrens 			goto out;
3004789Sahrens 	}
3005789Sahrens 
3006789Sahrens 	/*
3007789Sahrens 	 * Does target exist?
3008789Sahrens 	 */
3009789Sahrens 	if (tzp) {
3010789Sahrens 		/*
3011789Sahrens 		 * Source and target must be the same type.
3012789Sahrens 		 */
3013789Sahrens 		if (ZTOV(szp)->v_type == VDIR) {
3014789Sahrens 			if (ZTOV(tzp)->v_type != VDIR) {
3015789Sahrens 				error = ENOTDIR;
3016789Sahrens 				goto out;
3017789Sahrens 			}
3018789Sahrens 		} else {
3019789Sahrens 			if (ZTOV(tzp)->v_type == VDIR) {
3020789Sahrens 				error = EISDIR;
3021789Sahrens 				goto out;
3022789Sahrens 			}
3023789Sahrens 		}
3024789Sahrens 		/*
3025789Sahrens 		 * POSIX dictates that when the source and target
3026789Sahrens 		 * entries refer to the same file object, rename
3027789Sahrens 		 * must do nothing and exit without error.
3028789Sahrens 		 */
3029789Sahrens 		if (szp->z_id == tzp->z_id) {
3030789Sahrens 			error = 0;
3031789Sahrens 			goto out;
3032789Sahrens 		}
3033789Sahrens 	}
3034789Sahrens 
30355331Samw 	vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3036789Sahrens 	if (tzp)
30375331Samw 		vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
30384863Spraks 
30394863Spraks 	/*
30404863Spraks 	 * notify the target directory if it is not the same
30414863Spraks 	 * as source directory.
30424863Spraks 	 */
30434863Spraks 	if (tdvp != sdvp) {
30445331Samw 		vnevent_rename_dest_dir(tdvp, ct);
30454863Spraks 	}
3046789Sahrens 
3047789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3048789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);	/* nlink changes */
3049789Sahrens 	dmu_tx_hold_bonus(tx, sdzp->z_id);	/* nlink changes */
30501544Seschrock 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
30511544Seschrock 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
30521544Seschrock 	if (sdzp != tdzp)
3053789Sahrens 		dmu_tx_hold_bonus(tx, tdzp->z_id);	/* nlink changes */
30541544Seschrock 	if (tzp)
30551544Seschrock 		dmu_tx_hold_bonus(tx, tzp->z_id);	/* parent changes */
30563461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3057789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
3058789Sahrens 	if (error) {
3059789Sahrens 		if (zl != NULL)
3060789Sahrens 			zfs_rename_unlock(&zl);
3061789Sahrens 		zfs_dirent_unlock(sdl);
3062789Sahrens 		zfs_dirent_unlock(tdl);
3063789Sahrens 		VN_RELE(ZTOV(szp));
3064789Sahrens 		if (tzp)
3065789Sahrens 			VN_RELE(ZTOV(tzp));
3066789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
30672113Sahrens 			dmu_tx_wait(tx);
30682113Sahrens 			dmu_tx_abort(tx);
3069789Sahrens 			goto top;
3070789Sahrens 		}
30712113Sahrens 		dmu_tx_abort(tx);
3072789Sahrens 		ZFS_EXIT(zfsvfs);
3073789Sahrens 		return (error);
3074789Sahrens 	}
3075789Sahrens 
3076789Sahrens 	if (tzp)	/* Attempt to remove the existing target */
30775331Samw 		error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3078789Sahrens 
3079789Sahrens 	if (error == 0) {
3080789Sahrens 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3081789Sahrens 		if (error == 0) {
30825331Samw 			szp->z_phys->zp_flags |= ZFS_AV_MODIFIED;
30835331Samw 
3084789Sahrens 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3085789Sahrens 			ASSERT(error == 0);
30865331Samw 
30875331Samw 			zfs_log_rename(zilog, tx,
30885331Samw 			    TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0),
30895331Samw 			    sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
3090789Sahrens 		}
3091789Sahrens 	}
3092789Sahrens 
3093789Sahrens 	dmu_tx_commit(tx);
3094789Sahrens out:
3095789Sahrens 	if (zl != NULL)
3096789Sahrens 		zfs_rename_unlock(&zl);
3097789Sahrens 
3098789Sahrens 	zfs_dirent_unlock(sdl);
3099789Sahrens 	zfs_dirent_unlock(tdl);
3100789Sahrens 
3101789Sahrens 	VN_RELE(ZTOV(szp));
3102789Sahrens 	if (tzp)
3103789Sahrens 		VN_RELE(ZTOV(tzp));
3104789Sahrens 
3105789Sahrens 	ZFS_EXIT(zfsvfs);
3106789Sahrens 	return (error);
3107789Sahrens }
3108789Sahrens 
3109789Sahrens /*
3110789Sahrens  * Insert the indicated symbolic reference entry into the directory.
3111789Sahrens  *
3112789Sahrens  *	IN:	dvp	- Directory to contain new symbolic link.
3113789Sahrens  *		link	- Name for new symlink entry.
3114789Sahrens  *		vap	- Attributes of new entry.
3115789Sahrens  *		target	- Target path of new symlink.
3116789Sahrens  *		cr	- credentials of caller.
31175331Samw  *		ct	- caller context
31185331Samw  *		flags	- case flags
3119789Sahrens  *
3120789Sahrens  *	RETURN:	0 if success
3121789Sahrens  *		error code if failure
3122789Sahrens  *
3123789Sahrens  * Timestamps:
3124789Sahrens  *	dvp - ctime|mtime updated
3125789Sahrens  */
31265331Samw /*ARGSUSED*/
3127789Sahrens static int
31285331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
31295331Samw     caller_context_t *ct, int flags)
3130789Sahrens {
3131789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
3132789Sahrens 	zfs_dirlock_t	*dl;
3133789Sahrens 	dmu_tx_t	*tx;
3134789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
31355326Sek110237 	zilog_t		*zilog;
3136789Sahrens 	int		len = strlen(link);
3137789Sahrens 	int		error;
31385331Samw 	int		zflg = ZNEW;
31395331Samw 	zfs_fuid_info_t *fuidp = NULL;
3140789Sahrens 
3141789Sahrens 	ASSERT(vap->va_type == VLNK);
3142789Sahrens 
31435367Sahrens 	ZFS_ENTER(zfsvfs);
31445367Sahrens 	ZFS_VERIFY_ZP(dzp);
31455326Sek110237 	zilog = zfsvfs->z_log;
31465331Samw 
31475498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
31485331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
31495331Samw 		ZFS_EXIT(zfsvfs);
31505331Samw 		return (EILSEQ);
31515331Samw 	}
31525331Samw 	if (flags & FIGNORECASE)
31535331Samw 		zflg |= ZCILOOK;
3154789Sahrens top:
31555331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3156789Sahrens 		ZFS_EXIT(zfsvfs);
3157789Sahrens 		return (error);
3158789Sahrens 	}
3159789Sahrens 
3160789Sahrens 	if (len > MAXPATHLEN) {
3161789Sahrens 		ZFS_EXIT(zfsvfs);
3162789Sahrens 		return (ENAMETOOLONG);
3163789Sahrens 	}
3164789Sahrens 
3165789Sahrens 	/*
3166789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3167789Sahrens 	 */
31685331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
31695331Samw 	if (error) {
3170789Sahrens 		ZFS_EXIT(zfsvfs);
3171789Sahrens 		return (error);
3172789Sahrens 	}
3173789Sahrens 
3174789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3175789Sahrens 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3176789Sahrens 	dmu_tx_hold_bonus(tx, dzp->z_id);
31771544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3178789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
3179789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
31805331Samw 	if (zfsvfs->z_fuid_obj == 0) {
31815331Samw 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
31825331Samw 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
31835331Samw 		    SPA_MAXBLOCKSIZE);
31845331Samw 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
31855331Samw 	} else {
31865331Samw 		dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
31875331Samw 		dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
31885331Samw 		    SPA_MAXBLOCKSIZE);
31895331Samw 	}
3190789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
3191789Sahrens 	if (error) {
3192789Sahrens 		zfs_dirent_unlock(dl);
3193789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
31942113Sahrens 			dmu_tx_wait(tx);
31952113Sahrens 			dmu_tx_abort(tx);
3196789Sahrens 			goto top;
3197789Sahrens 		}
31982113Sahrens 		dmu_tx_abort(tx);
3199789Sahrens 		ZFS_EXIT(zfsvfs);
3200789Sahrens 		return (error);
3201789Sahrens 	}
3202789Sahrens 
3203789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
3204789Sahrens 
3205789Sahrens 	/*
3206789Sahrens 	 * Create a new object for the symlink.
3207789Sahrens 	 * Put the link content into bonus buffer if it will fit;
3208789Sahrens 	 * otherwise, store it just like any other file data.
3209789Sahrens 	 */
3210789Sahrens 	if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
32115446Sahrens 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, len, NULL, &fuidp);
3212789Sahrens 		if (len != 0)
3213789Sahrens 			bcopy(link, zp->z_phys + 1, len);
3214789Sahrens 	} else {
3215789Sahrens 		dmu_buf_t *dbp;
32161669Sperrin 
32175446Sahrens 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, NULL, &fuidp);
32181669Sperrin 		/*
32191669Sperrin 		 * Nothing can access the znode yet so no locking needed
32201669Sperrin 		 * for growing the znode's blocksize.
32211669Sperrin 		 */
32221669Sperrin 		zfs_grow_blocksize(zp, len, tx);
3223789Sahrens 
32245446Sahrens 		VERIFY(0 == dmu_buf_hold(zfsvfs->z_os,
32255446Sahrens 		    zp->z_id, 0, FTAG, &dbp));
3226789Sahrens 		dmu_buf_will_dirty(dbp, tx);
3227789Sahrens 
3228789Sahrens 		ASSERT3U(len, <=, dbp->db_size);
3229789Sahrens 		bcopy(link, dbp->db_data, len);
32301544Seschrock 		dmu_buf_rele(dbp, FTAG);
3231789Sahrens 	}
3232789Sahrens 	zp->z_phys->zp_size = len;
3233789Sahrens 
3234789Sahrens 	/*
3235789Sahrens 	 * Insert the new object into the directory.
3236789Sahrens 	 */
3237789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
3238789Sahrens out:
32395331Samw 	if (error == 0) {
32405331Samw 		uint64_t txtype = TX_SYMLINK;
32415331Samw 		if (flags & FIGNORECASE)
32425331Samw 			txtype |= TX_CI;
32435331Samw 		zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
32445331Samw 	}
32455331Samw 	if (fuidp)
32465331Samw 		zfs_fuid_info_free(fuidp);
3247789Sahrens 
3248789Sahrens 	dmu_tx_commit(tx);
3249789Sahrens 
3250789Sahrens 	zfs_dirent_unlock(dl);
3251789Sahrens 
3252789Sahrens 	VN_RELE(ZTOV(zp));
3253789Sahrens 
3254789Sahrens 	ZFS_EXIT(zfsvfs);
3255789Sahrens 	return (error);
3256789Sahrens }
3257789Sahrens 
3258789Sahrens /*
3259789Sahrens  * Return, in the buffer contained in the provided uio structure,
3260789Sahrens  * the symbolic path referred to by vp.
3261789Sahrens  *
3262789Sahrens  *	IN:	vp	- vnode of symbolic link.
3263789Sahrens  *		uoip	- structure to contain the link path.
3264789Sahrens  *		cr	- credentials of caller.
32655331Samw  *		ct	- caller context
3266789Sahrens  *
3267789Sahrens  *	OUT:	uio	- structure to contain the link path.
3268789Sahrens  *
3269789Sahrens  *	RETURN:	0 if success
3270789Sahrens  *		error code if failure
3271789Sahrens  *
3272789Sahrens  * Timestamps:
3273789Sahrens  *	vp - atime updated
3274789Sahrens  */
3275789Sahrens /* ARGSUSED */
3276789Sahrens static int
32775331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3278789Sahrens {
3279789Sahrens 	znode_t		*zp = VTOZ(vp);
3280789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3281789Sahrens 	size_t		bufsz;
3282789Sahrens 	int		error;
3283789Sahrens 
32845367Sahrens 	ZFS_ENTER(zfsvfs);
32855367Sahrens 	ZFS_VERIFY_ZP(zp);
3286789Sahrens 
3287789Sahrens 	bufsz = (size_t)zp->z_phys->zp_size;
3288789Sahrens 	if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
3289789Sahrens 		error = uiomove(zp->z_phys + 1,
3290789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
3291789Sahrens 	} else {
32921544Seschrock 		dmu_buf_t *dbp;
32931544Seschrock 		error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
32941544Seschrock 		if (error) {
3295789Sahrens 			ZFS_EXIT(zfsvfs);
3296789Sahrens 			return (error);
3297789Sahrens 		}
3298789Sahrens 		error = uiomove(dbp->db_data,
3299789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
33001544Seschrock 		dmu_buf_rele(dbp, FTAG);
3301789Sahrens 	}
3302789Sahrens 
3303789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3304789Sahrens 	ZFS_EXIT(zfsvfs);
3305789Sahrens 	return (error);
3306789Sahrens }
3307789Sahrens 
3308789Sahrens /*
3309789Sahrens  * Insert a new entry into directory tdvp referencing svp.
3310789Sahrens  *
3311789Sahrens  *	IN:	tdvp	- Directory to contain new entry.
3312789Sahrens  *		svp	- vnode of new entry.
3313789Sahrens  *		name	- name of new entry.
3314789Sahrens  *		cr	- credentials of caller.
33155331Samw  *		ct	- caller context
3316789Sahrens  *
3317789Sahrens  *	RETURN:	0 if success
3318789Sahrens  *		error code if failure
3319789Sahrens  *
3320789Sahrens  * Timestamps:
3321789Sahrens  *	tdvp - ctime|mtime updated
3322789Sahrens  *	 svp - ctime updated
3323789Sahrens  */
3324789Sahrens /* ARGSUSED */
3325789Sahrens static int
33265331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
33275331Samw     caller_context_t *ct, int flags)
3328789Sahrens {
3329789Sahrens 	znode_t		*dzp = VTOZ(tdvp);
3330789Sahrens 	znode_t		*tzp, *szp;
3331789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
33325326Sek110237 	zilog_t		*zilog;
3333789Sahrens 	zfs_dirlock_t	*dl;
3334789Sahrens 	dmu_tx_t	*tx;
3335789Sahrens 	vnode_t		*realvp;
3336789Sahrens 	int		error;
33375331Samw 	int		zf = ZNEW;
33385331Samw 	uid_t		owner;
3339789Sahrens 
3340789Sahrens 	ASSERT(tdvp->v_type == VDIR);
3341789Sahrens 
33425367Sahrens 	ZFS_ENTER(zfsvfs);
33435367Sahrens 	ZFS_VERIFY_ZP(dzp);
33445326Sek110237 	zilog = zfsvfs->z_log;
3345789Sahrens 
33465331Samw 	if (VOP_REALVP(svp, &realvp, ct) == 0)
3347789Sahrens 		svp = realvp;
3348789Sahrens 
3349789Sahrens 	if (svp->v_vfsp != tdvp->v_vfsp) {
3350789Sahrens 		ZFS_EXIT(zfsvfs);
3351789Sahrens 		return (EXDEV);
3352789Sahrens 	}
33535367Sahrens 	szp = VTOZ(svp);
33545367Sahrens 	ZFS_VERIFY_ZP(szp);
3355789Sahrens 
33565498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name,
33575331Samw 	    strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
33585331Samw 		ZFS_EXIT(zfsvfs);
33595331Samw 		return (EILSEQ);
33605331Samw 	}
33615331Samw 	if (flags & FIGNORECASE)
33625331Samw 		zf |= ZCILOOK;
33635331Samw 
3364789Sahrens top:
3365789Sahrens 	/*
3366789Sahrens 	 * We do not support links between attributes and non-attributes
3367789Sahrens 	 * because of the potential security risk of creating links
3368789Sahrens 	 * into "normal" file space in order to circumvent restrictions
3369789Sahrens 	 * imposed in attribute space.
3370789Sahrens 	 */
3371789Sahrens 	if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
3372789Sahrens 	    (dzp->z_phys->zp_flags & ZFS_XATTR)) {
3373789Sahrens 		ZFS_EXIT(zfsvfs);
3374789Sahrens 		return (EINVAL);
3375789Sahrens 	}
3376789Sahrens 
3377789Sahrens 	/*
3378789Sahrens 	 * POSIX dictates that we return EPERM here.
3379789Sahrens 	 * Better choices include ENOTSUP or EISDIR.
3380789Sahrens 	 */
3381789Sahrens 	if (svp->v_type == VDIR) {
3382789Sahrens 		ZFS_EXIT(zfsvfs);
3383789Sahrens 		return (EPERM);
3384789Sahrens 	}
3385789Sahrens 
3386*5771Sjp151216 	zfs_fuid_map_id(zfsvfs, szp->z_phys->zp_uid, cr, ZFS_OWNER, &owner);
33875331Samw 	if (owner != crgetuid(cr) &&
3388789Sahrens 	    secpolicy_basic_link(cr) != 0) {
3389789Sahrens 		ZFS_EXIT(zfsvfs);
3390789Sahrens 		return (EPERM);
3391789Sahrens 	}
3392789Sahrens 
33935331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3394789Sahrens 		ZFS_EXIT(zfsvfs);
3395789Sahrens 		return (error);
3396789Sahrens 	}
3397789Sahrens 
3398789Sahrens 	/*
3399789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3400789Sahrens 	 */
34015331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
34025331Samw 	if (error) {
3403789Sahrens 		ZFS_EXIT(zfsvfs);
3404789Sahrens 		return (error);
3405789Sahrens 	}
3406789Sahrens 
3407789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3408789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);
34091544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3410789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
3411789Sahrens 	if (error) {
3412789Sahrens 		zfs_dirent_unlock(dl);
3413789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
34142113Sahrens 			dmu_tx_wait(tx);
34152113Sahrens 			dmu_tx_abort(tx);
3416789Sahrens 			goto top;
3417789Sahrens 		}
34182113Sahrens 		dmu_tx_abort(tx);
3419789Sahrens 		ZFS_EXIT(zfsvfs);
3420789Sahrens 		return (error);
3421789Sahrens 	}
3422789Sahrens 
3423789Sahrens 	error = zfs_link_create(dl, szp, tx, 0);
3424789Sahrens 
34255331Samw 	if (error == 0) {
34265331Samw 		uint64_t txtype = TX_LINK;
34275331Samw 		if (flags & FIGNORECASE)
34285331Samw 			txtype |= TX_CI;
34295331Samw 		zfs_log_link(zilog, tx, txtype, dzp, szp, name);
34305331Samw 	}
3431789Sahrens 
3432789Sahrens 	dmu_tx_commit(tx);
3433789Sahrens 
3434789Sahrens 	zfs_dirent_unlock(dl);
3435789Sahrens 
34364863Spraks 	if (error == 0) {
34375331Samw 		vnevent_link(svp, ct);
34384863Spraks 	}
34394863Spraks 
3440789Sahrens 	ZFS_EXIT(zfsvfs);
3441789Sahrens 	return (error);
3442789Sahrens }
3443789Sahrens 
3444789Sahrens /*
3445789Sahrens  * zfs_null_putapage() is used when the file system has been force
3446789Sahrens  * unmounted. It just drops the pages.
3447789Sahrens  */
3448789Sahrens /* ARGSUSED */
3449789Sahrens static int
3450789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3451789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3452789Sahrens {
3453789Sahrens 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
3454789Sahrens 	return (0);
3455789Sahrens }
3456789Sahrens 
34572688Smaybee /*
34582688Smaybee  * Push a page out to disk, klustering if possible.
34592688Smaybee  *
34602688Smaybee  *	IN:	vp	- file to push page to.
34612688Smaybee  *		pp	- page to push.
34622688Smaybee  *		flags	- additional flags.
34632688Smaybee  *		cr	- credentials of caller.
34642688Smaybee  *
34652688Smaybee  *	OUT:	offp	- start of range pushed.
34662688Smaybee  *		lenp	- len of range pushed.
34672688Smaybee  *
34682688Smaybee  *	RETURN:	0 if success
34692688Smaybee  *		error code if failure
34702688Smaybee  *
34712688Smaybee  * NOTE: callers must have locked the page to be pushed.  On
34722688Smaybee  * exit, the page (and all other pages in the kluster) must be
34732688Smaybee  * unlocked.
34742688Smaybee  */
3475789Sahrens /* ARGSUSED */
3476789Sahrens static int
3477789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3478789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3479789Sahrens {
3480789Sahrens 	znode_t		*zp = VTOZ(vp);
3481789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3482789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
3483789Sahrens 	dmu_tx_t	*tx;
34841669Sperrin 	rl_t		*rl;
34852688Smaybee 	u_offset_t	off, koff;
34862688Smaybee 	size_t		len, klen;
34874709Smaybee 	uint64_t	filesz;
3488789Sahrens 	int		err;
3489789Sahrens 
34904709Smaybee 	filesz = zp->z_phys->zp_size;
34912688Smaybee 	off = pp->p_offset;
34922688Smaybee 	len = PAGESIZE;
34932688Smaybee 	/*
34942688Smaybee 	 * If our blocksize is bigger than the page size, try to kluster
34952688Smaybee 	 * muiltiple pages so that we write a full block (thus avoiding
34962688Smaybee 	 * a read-modify-write).
34972688Smaybee 	 */
34984709Smaybee 	if (off < filesz && zp->z_blksz > PAGESIZE) {
34992688Smaybee 		if (!ISP2(zp->z_blksz)) {
35002688Smaybee 			/* Only one block in the file. */
35012688Smaybee 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
35022688Smaybee 			koff = 0;
35032688Smaybee 		} else {
35042688Smaybee 			klen = zp->z_blksz;
35052688Smaybee 			koff = P2ALIGN(off, (u_offset_t)klen);
35062688Smaybee 		}
35072688Smaybee 		ASSERT(koff <= filesz);
35082688Smaybee 		if (koff + klen > filesz)
35092688Smaybee 			klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE);
35102688Smaybee 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
35112688Smaybee 	}
35122688Smaybee 	ASSERT3U(btop(len), ==, btopr(len));
3513789Sahrens top:
35142688Smaybee 	rl = zfs_range_lock(zp, off, len, RL_WRITER);
35151819Smaybee 	/*
35161819Smaybee 	 * Can't push pages past end-of-file.
35171819Smaybee 	 */
35184709Smaybee 	filesz = zp->z_phys->zp_size;
35194709Smaybee 	if (off >= filesz) {
35204709Smaybee 		/* ignore all pages */
35212688Smaybee 		err = 0;
35222688Smaybee 		goto out;
35234709Smaybee 	} else if (off + len > filesz) {
35244709Smaybee 		int npages = btopr(filesz - off);
35252688Smaybee 		page_t *trunc;
35262688Smaybee 
35272688Smaybee 		page_list_break(&pp, &trunc, npages);
35284709Smaybee 		/* ignore pages past end of file */
35292688Smaybee 		if (trunc)
35304709Smaybee 			pvn_write_done(trunc, flags);
35314709Smaybee 		len = filesz - off;
35321819Smaybee 	}
3533789Sahrens 
3534789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3535789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, off, len);
3536789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
3537789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
3538789Sahrens 	if (err != 0) {
3539789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
35402688Smaybee 			zfs_range_unlock(rl);
35412113Sahrens 			dmu_tx_wait(tx);
35422113Sahrens 			dmu_tx_abort(tx);
35432688Smaybee 			err = 0;
3544789Sahrens 			goto top;
3545789Sahrens 		}
35462113Sahrens 		dmu_tx_abort(tx);
3547789Sahrens 		goto out;
3548789Sahrens 	}
3549789Sahrens 
35502688Smaybee 	if (zp->z_blksz <= PAGESIZE) {
35512688Smaybee 		caddr_t va = ppmapin(pp, PROT_READ, (caddr_t)-1);
35522688Smaybee 		ASSERT3U(len, <=, PAGESIZE);
35532688Smaybee 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
35542688Smaybee 		ppmapout(va);
35552688Smaybee 	} else {
35562688Smaybee 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
35572688Smaybee 	}
35582688Smaybee 
35592688Smaybee 	if (err == 0) {
35602688Smaybee 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
35613638Sbillm 		zfs_log_write(zilog, tx, TX_WRITE, zp, off, len, 0);
35622688Smaybee 		dmu_tx_commit(tx);
35632688Smaybee 	}
35642688Smaybee 
35652688Smaybee out:
35662237Smaybee 	zfs_range_unlock(rl);
35674709Smaybee 	pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
3568789Sahrens 	if (offp)
3569789Sahrens 		*offp = off;
3570789Sahrens 	if (lenp)
3571789Sahrens 		*lenp = len;
3572789Sahrens 
3573789Sahrens 	return (err);
3574789Sahrens }
3575789Sahrens 
3576789Sahrens /*
3577789Sahrens  * Copy the portion of the file indicated from pages into the file.
3578789Sahrens  * The pages are stored in a page list attached to the files vnode.
3579789Sahrens  *
3580789Sahrens  *	IN:	vp	- vnode of file to push page data to.
3581789Sahrens  *		off	- position in file to put data.
3582789Sahrens  *		len	- amount of data to write.
3583789Sahrens  *		flags	- flags to control the operation.
3584789Sahrens  *		cr	- credentials of caller.
35855331Samw  *		ct	- caller context.
3586789Sahrens  *
3587789Sahrens  *	RETURN:	0 if success
3588789Sahrens  *		error code if failure
3589789Sahrens  *
3590789Sahrens  * Timestamps:
3591789Sahrens  *	vp - ctime|mtime updated
3592789Sahrens  */
35935331Samw /*ARGSUSED*/
3594789Sahrens static int
35955331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
35965331Samw     caller_context_t *ct)
3597789Sahrens {
3598789Sahrens 	znode_t		*zp = VTOZ(vp);
3599789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3600789Sahrens 	page_t		*pp;
3601789Sahrens 	size_t		io_len;
3602789Sahrens 	u_offset_t	io_off;
36031669Sperrin 	uint64_t	filesz;
3604789Sahrens 	int		error = 0;
3605789Sahrens 
36065367Sahrens 	ZFS_ENTER(zfsvfs);
36075367Sahrens 	ZFS_VERIFY_ZP(zp);
3608789Sahrens 
3609789Sahrens 	if (len == 0) {
3610789Sahrens 		/*
3611789Sahrens 		 * Search the entire vp list for pages >= off.
3612789Sahrens 		 */
3613789Sahrens 		error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage,
3614789Sahrens 		    flags, cr);
36151472Sperrin 		goto out;
3616789Sahrens 	}
3617789Sahrens 
36181669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
36191669Sperrin 	if (off > filesz) {
3620789Sahrens 		/* past end of file */
3621789Sahrens 		ZFS_EXIT(zfsvfs);
3622789Sahrens 		return (0);
3623789Sahrens 	}
3624789Sahrens 
36251669Sperrin 	len = MIN(len, filesz - off);
3626789Sahrens 
36271472Sperrin 	for (io_off = off; io_off < off + len; io_off += io_len) {
3628789Sahrens 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
36291669Sperrin 			pp = page_lookup(vp, io_off,
36304339Sperrin 			    (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
3631789Sahrens 		} else {
3632789Sahrens 			pp = page_lookup_nowait(vp, io_off,
36334339Sperrin 			    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
3634789Sahrens 		}
3635789Sahrens 
3636789Sahrens 		if (pp != NULL && pvn_getdirty(pp, flags)) {
3637789Sahrens 			int err;
3638789Sahrens 
3639789Sahrens 			/*
3640789Sahrens 			 * Found a dirty page to push
3641789Sahrens 			 */
36421669Sperrin 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
36431669Sperrin 			if (err)
3644789Sahrens 				error = err;
3645789Sahrens 		} else {
3646789Sahrens 			io_len = PAGESIZE;
3647789Sahrens 		}
3648789Sahrens 	}
36491472Sperrin out:
36502638Sperrin 	if ((flags & B_ASYNC) == 0)
36512638Sperrin 		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
3652789Sahrens 	ZFS_EXIT(zfsvfs);
3653789Sahrens 	return (error);
3654789Sahrens }
3655789Sahrens 
36565331Samw /*ARGSUSED*/
3657789Sahrens void
36585331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
3659789Sahrens {
3660789Sahrens 	znode_t	*zp = VTOZ(vp);
3661789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3662789Sahrens 	int error;
3663789Sahrens 
36645326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
36655446Sahrens 	if (zp->z_dbuf == NULL) {
36665446Sahrens 		/*
36675642Smaybee 		 * The fs has been unmounted, or we did a
36685642Smaybee 		 * suspend/resume and this file no longer exists.
36695446Sahrens 		 */
3670789Sahrens 		if (vn_has_cached_data(vp)) {
3671789Sahrens 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
3672789Sahrens 			    B_INVAL, cr);
3673789Sahrens 		}
3674789Sahrens 
36751544Seschrock 		mutex_enter(&zp->z_lock);
3676789Sahrens 		vp->v_count = 0; /* count arrives as 1 */
36775446Sahrens 		mutex_exit(&zp->z_lock);
36785642Smaybee 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
36795446Sahrens 		zfs_znode_free(zp);
3680789Sahrens 		return;
3681789Sahrens 	}
3682789Sahrens 
3683789Sahrens 	/*
3684789Sahrens 	 * Attempt to push any data in the page cache.  If this fails
3685789Sahrens 	 * we will get kicked out later in zfs_zinactive().
3686789Sahrens 	 */
36871298Sperrin 	if (vn_has_cached_data(vp)) {
36881298Sperrin 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
36891298Sperrin 		    cr);
36901298Sperrin 	}
3691789Sahrens 
36923461Sahrens 	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3693789Sahrens 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3694789Sahrens 
3695789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
3696789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
3697789Sahrens 		if (error) {
3698789Sahrens 			dmu_tx_abort(tx);
3699789Sahrens 		} else {
3700789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
3701789Sahrens 			mutex_enter(&zp->z_lock);
3702789Sahrens 			zp->z_atime_dirty = 0;
3703789Sahrens 			mutex_exit(&zp->z_lock);
3704789Sahrens 			dmu_tx_commit(tx);
3705789Sahrens 		}
3706789Sahrens 	}
3707789Sahrens 
3708789Sahrens 	zfs_zinactive(zp);
37095326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
3710789Sahrens }
3711789Sahrens 
3712789Sahrens /*
3713789Sahrens  * Bounds-check the seek operation.
3714789Sahrens  *
3715789Sahrens  *	IN:	vp	- vnode seeking within
3716789Sahrens  *		ooff	- old file offset
3717789Sahrens  *		noffp	- pointer to new file offset
37185331Samw  *		ct	- caller context
3719789Sahrens  *
3720789Sahrens  *	RETURN:	0 if success
3721789Sahrens  *		EINVAL if new offset invalid
3722789Sahrens  */
3723789Sahrens /* ARGSUSED */
3724789Sahrens static int
37255331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
37265331Samw     caller_context_t *ct)
3727789Sahrens {
3728789Sahrens 	if (vp->v_type == VDIR)
3729789Sahrens 		return (0);
3730789Sahrens 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
3731789Sahrens }
3732789Sahrens 
3733789Sahrens /*
3734789Sahrens  * Pre-filter the generic locking function to trap attempts to place
3735789Sahrens  * a mandatory lock on a memory mapped file.
3736789Sahrens  */
3737789Sahrens static int
3738789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
37395331Samw     flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
3740789Sahrens {
3741789Sahrens 	znode_t *zp = VTOZ(vp);
3742789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3743789Sahrens 	int error;
3744789Sahrens 
37455367Sahrens 	ZFS_ENTER(zfsvfs);
37465367Sahrens 	ZFS_VERIFY_ZP(zp);
3747789Sahrens 
3748789Sahrens 	/*
37491544Seschrock 	 * We are following the UFS semantics with respect to mapcnt
37501544Seschrock 	 * here: If we see that the file is mapped already, then we will
37511544Seschrock 	 * return an error, but we don't worry about races between this
37521544Seschrock 	 * function and zfs_map().
3753789Sahrens 	 */
37541544Seschrock 	if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) {
3755789Sahrens 		ZFS_EXIT(zfsvfs);
3756789Sahrens 		return (EAGAIN);
3757789Sahrens 	}
37585331Samw 	error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct);
3759789Sahrens 	ZFS_EXIT(zfsvfs);
3760789Sahrens 	return (error);
3761789Sahrens }
3762789Sahrens 
3763789Sahrens /*
3764789Sahrens  * If we can't find a page in the cache, we will create a new page
3765789Sahrens  * and fill it with file data.  For efficiency, we may try to fill
37661669Sperrin  * multiple pages at once (klustering).
3767789Sahrens  */
3768789Sahrens static int
3769789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
3770789Sahrens     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
3771789Sahrens {
3772789Sahrens 	znode_t *zp = VTOZ(vp);
3773789Sahrens 	page_t *pp, *cur_pp;
3774789Sahrens 	objset_t *os = zp->z_zfsvfs->z_os;
3775789Sahrens 	caddr_t va;
3776789Sahrens 	u_offset_t io_off, total;
3777789Sahrens 	uint64_t oid = zp->z_id;
3778789Sahrens 	size_t io_len;
37791669Sperrin 	uint64_t filesz;
3780789Sahrens 	int err;
3781789Sahrens 
3782789Sahrens 	/*
3783789Sahrens 	 * If we are only asking for a single page don't bother klustering.
3784789Sahrens 	 */
37851669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
37862688Smaybee 	if (off >= filesz)
37872688Smaybee 		return (EFAULT);
37882688Smaybee 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
3789789Sahrens 		io_off = off;
3790789Sahrens 		io_len = PAGESIZE;
3791789Sahrens 		pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr);
3792789Sahrens 	} else {
3793789Sahrens 		/*
3794789Sahrens 		 * Try to fill a kluster of pages (a blocks worth).
3795789Sahrens 		 */
3796789Sahrens 		size_t klen;
3797789Sahrens 		u_offset_t koff;
3798789Sahrens 
3799789Sahrens 		if (!ISP2(zp->z_blksz)) {
3800789Sahrens 			/* Only one block in the file. */
3801789Sahrens 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
3802789Sahrens 			koff = 0;
3803789Sahrens 		} else {
38043131Sgw25295 			/*
38053131Sgw25295 			 * It would be ideal to align our offset to the
38063131Sgw25295 			 * blocksize but doing so has resulted in some
38073131Sgw25295 			 * strange application crashes. For now, we
38083131Sgw25295 			 * leave the offset as is and only adjust the
38093131Sgw25295 			 * length if we are off the end of the file.
38103131Sgw25295 			 */
38113131Sgw25295 			koff = off;
3812789Sahrens 			klen = plsz;
3813789Sahrens 		}
38141819Smaybee 		ASSERT(koff <= filesz);
38151819Smaybee 		if (koff + klen > filesz)
38161819Smaybee 			klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff;
38172688Smaybee 		ASSERT3U(off, >=, koff);
38182688Smaybee 		ASSERT3U(off, <, koff + klen);
3819789Sahrens 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
38204339Sperrin 		    &io_len, koff, klen, 0);
3821789Sahrens 	}
3822789Sahrens 	if (pp == NULL) {
3823789Sahrens 		/*
3824789Sahrens 		 * Some other thread entered the page before us.
3825789Sahrens 		 * Return to zfs_getpage to retry the lookup.
3826789Sahrens 		 */
3827789Sahrens 		*pl = NULL;
3828789Sahrens 		return (0);
3829789Sahrens 	}
3830789Sahrens 
3831789Sahrens 	/*
3832789Sahrens 	 * Fill the pages in the kluster.
3833789Sahrens 	 */
3834789Sahrens 	cur_pp = pp;
3835789Sahrens 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
38362688Smaybee 		ASSERT3U(io_off, ==, cur_pp->p_offset);
3837789Sahrens 		va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1);
38381544Seschrock 		err = dmu_read(os, oid, io_off, PAGESIZE, va);
3839789Sahrens 		ppmapout(va);
3840789Sahrens 		if (err) {
3841789Sahrens 			/* On error, toss the entire kluster */
3842789Sahrens 			pvn_read_done(pp, B_ERROR);
3843789Sahrens 			return (err);
3844789Sahrens 		}
3845789Sahrens 		cur_pp = cur_pp->p_next;
3846789Sahrens 	}
3847789Sahrens out:
3848789Sahrens 	/*
3849789Sahrens 	 * Fill in the page list array from the kluster.  If
3850789Sahrens 	 * there are too many pages in the kluster, return
3851789Sahrens 	 * as many pages as possible starting from the desired
3852789Sahrens 	 * offset `off'.
3853789Sahrens 	 * NOTE: the page list will always be null terminated.
3854789Sahrens 	 */
3855789Sahrens 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
3856789Sahrens 
3857789Sahrens 	return (0);
3858789Sahrens }
3859789Sahrens 
3860789Sahrens /*
3861789Sahrens  * Return pointers to the pages for the file region [off, off + len]
3862789Sahrens  * in the pl array.  If plsz is greater than len, this function may
3863789Sahrens  * also return page pointers from before or after the specified
3864789Sahrens  * region (i.e. some region [off', off' + plsz]).  These additional
3865789Sahrens  * pages are only returned if they are already in the cache, or were
3866789Sahrens  * created as part of a klustered read.
3867789Sahrens  *
3868789Sahrens  *	IN:	vp	- vnode of file to get data from.
3869789Sahrens  *		off	- position in file to get data from.
3870789Sahrens  *		len	- amount of data to retrieve.
3871789Sahrens  *		plsz	- length of provided page list.
3872789Sahrens  *		seg	- segment to obtain pages for.
3873789Sahrens  *		addr	- virtual address of fault.
3874789Sahrens  *		rw	- mode of created pages.
3875789Sahrens  *		cr	- credentials of caller.
38765331Samw  *		ct	- caller context.
3877789Sahrens  *
3878789Sahrens  *	OUT:	protp	- protection mode of created pages.
3879789Sahrens  *		pl	- list of pages created.
3880789Sahrens  *
3881789Sahrens  *	RETURN:	0 if success
3882789Sahrens  *		error code if failure
3883789Sahrens  *
3884789Sahrens  * Timestamps:
3885789Sahrens  *	vp - atime updated
3886789Sahrens  */
3887789Sahrens /* ARGSUSED */
3888789Sahrens static int
3889789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
3890789Sahrens 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
38915331Samw 	enum seg_rw rw, cred_t *cr, caller_context_t *ct)
3892789Sahrens {
3893789Sahrens 	znode_t		*zp = VTOZ(vp);
3894789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3895789Sahrens 	page_t		*pp, **pl0 = pl;
38962752Sperrin 	int		need_unlock = 0, err = 0;
38972752Sperrin 	offset_t	orig_off;
3898789Sahrens 
38995367Sahrens 	ZFS_ENTER(zfsvfs);
39005367Sahrens 	ZFS_VERIFY_ZP(zp);
3901789Sahrens 
3902789Sahrens 	if (protp)
3903789Sahrens 		*protp = PROT_ALL;
3904789Sahrens 
3905789Sahrens 	/* no faultahead (for now) */
3906789Sahrens 	if (pl == NULL) {
3907789Sahrens 		ZFS_EXIT(zfsvfs);
3908789Sahrens 		return (0);
3909789Sahrens 	}
3910789Sahrens 
3911789Sahrens 	/* can't fault past EOF */
3912789Sahrens 	if (off >= zp->z_phys->zp_size) {
3913789Sahrens 		ZFS_EXIT(zfsvfs);
3914789Sahrens 		return (EFAULT);
3915789Sahrens 	}
39162752Sperrin 	orig_off = off;
3917789Sahrens 
3918789Sahrens 	/*
3919789Sahrens 	 * If we already own the lock, then we must be page faulting
3920789Sahrens 	 * in the middle of a write to this file (i.e., we are writing
3921789Sahrens 	 * to this file using data from a mapped region of the file).
3922789Sahrens 	 */
39232752Sperrin 	if (rw_owner(&zp->z_map_lock) != curthread) {
3924789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
3925789Sahrens 		need_unlock = TRUE;
3926789Sahrens 	}
3927789Sahrens 
3928789Sahrens 	/*
3929789Sahrens 	 * Loop through the requested range [off, off + len] looking
3930789Sahrens 	 * for pages.  If we don't find a page, we will need to create
3931789Sahrens 	 * a new page and fill it with data from the file.
3932789Sahrens 	 */
3933789Sahrens 	while (len > 0) {
3934789Sahrens 		if (plsz < PAGESIZE)
3935789Sahrens 			break;
3936789Sahrens 		if (pp = page_lookup(vp, off, SE_SHARED)) {
3937789Sahrens 			*pl++ = pp;
3938789Sahrens 			off += PAGESIZE;
3939789Sahrens 			addr += PAGESIZE;
3940789Sahrens 			len -= PAGESIZE;
3941789Sahrens 			plsz -= PAGESIZE;
3942789Sahrens 		} else {
3943789Sahrens 			err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw);
39442752Sperrin 			if (err)
39452752Sperrin 				goto out;
3946789Sahrens 			/*
3947789Sahrens 			 * klustering may have changed our region
3948789Sahrens 			 * to be block aligned.
3949789Sahrens 			 */
3950789Sahrens 			if (((pp = *pl) != 0) && (off != pp->p_offset)) {
3951789Sahrens 				int delta = off - pp->p_offset;
3952789Sahrens 				len += delta;
3953789Sahrens 				off -= delta;
3954789Sahrens 				addr -= delta;
3955789Sahrens 			}
3956789Sahrens 			while (*pl) {
3957789Sahrens 				pl++;
3958789Sahrens 				off += PAGESIZE;
3959789Sahrens 				addr += PAGESIZE;
3960789Sahrens 				plsz -= PAGESIZE;
3961789Sahrens 				if (len > PAGESIZE)
3962789Sahrens 					len -= PAGESIZE;
3963789Sahrens 				else
3964789Sahrens 					len = 0;
3965789Sahrens 			}
3966789Sahrens 		}
3967789Sahrens 	}
3968789Sahrens 
3969789Sahrens 	/*
3970789Sahrens 	 * Fill out the page array with any pages already in the cache.
3971789Sahrens 	 */
3972789Sahrens 	while (plsz > 0) {
3973789Sahrens 		pp = page_lookup_nowait(vp, off, SE_SHARED);
3974789Sahrens 		if (pp == NULL)
3975789Sahrens 			break;
3976789Sahrens 		*pl++ = pp;
3977789Sahrens 		off += PAGESIZE;
3978789Sahrens 		plsz -= PAGESIZE;
3979789Sahrens 	}
3980789Sahrens 
3981789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3982789Sahrens out:
39832752Sperrin 	/*
39842752Sperrin 	 * We can't grab the range lock for the page as reader which would
39852752Sperrin 	 * stop truncation as this leads to deadlock. So we need to recheck
39862752Sperrin 	 * the file size.
39872752Sperrin 	 */
39882752Sperrin 	if (orig_off >= zp->z_phys->zp_size)
39892752Sperrin 		err = EFAULT;
39902752Sperrin 	if (err) {
39912752Sperrin 		/*
39922752Sperrin 		 * Release any pages we have previously locked.
39932752Sperrin 		 */
39942752Sperrin 		while (pl > pl0)
39952752Sperrin 			page_unlock(*--pl);
39962752Sperrin 	}
39972752Sperrin 
3998789Sahrens 	*pl = NULL;
3999789Sahrens 
4000789Sahrens 	if (need_unlock)
4001789Sahrens 		rw_exit(&zp->z_map_lock);
4002789Sahrens 
4003789Sahrens 	ZFS_EXIT(zfsvfs);
4004789Sahrens 	return (err);
4005789Sahrens }
4006789Sahrens 
40071544Seschrock /*
40081544Seschrock  * Request a memory map for a section of a file.  This code interacts
40091544Seschrock  * with common code and the VM system as follows:
40101544Seschrock  *
40111544Seschrock  *	common code calls mmap(), which ends up in smmap_common()
40121544Seschrock  *
40131544Seschrock  *	this calls VOP_MAP(), which takes you into (say) zfs
40141544Seschrock  *
40151544Seschrock  *	zfs_map() calls as_map(), passing segvn_create() as the callback
40161544Seschrock  *
40171544Seschrock  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
40181544Seschrock  *
40191544Seschrock  *	zfs_addmap() updates z_mapcnt
40201544Seschrock  */
40215331Samw /*ARGSUSED*/
4022789Sahrens static int
4023789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
40245331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
40255331Samw     caller_context_t *ct)
4026789Sahrens {
4027789Sahrens 	znode_t *zp = VTOZ(vp);
4028789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4029789Sahrens 	segvn_crargs_t	vn_a;
4030789Sahrens 	int		error;
4031789Sahrens 
40325331Samw 	if ((prot & PROT_WRITE) &&
40335331Samw 	    (zp->z_phys->zp_flags & (ZFS_IMMUTABLE | ZFS_READONLY |
40345331Samw 	    ZFS_APPENDONLY)))
40355331Samw 		return (EPERM);
40365331Samw 
40375367Sahrens 	ZFS_ENTER(zfsvfs);
40385367Sahrens 	ZFS_VERIFY_ZP(zp);
4039789Sahrens 
4040789Sahrens 	if (vp->v_flag & VNOMAP) {
4041789Sahrens 		ZFS_EXIT(zfsvfs);
4042789Sahrens 		return (ENOSYS);
4043789Sahrens 	}
4044789Sahrens 
4045789Sahrens 	if (off < 0 || len > MAXOFFSET_T - off) {
4046789Sahrens 		ZFS_EXIT(zfsvfs);
4047789Sahrens 		return (ENXIO);
4048789Sahrens 	}
4049789Sahrens 
4050789Sahrens 	if (vp->v_type != VREG) {
4051789Sahrens 		ZFS_EXIT(zfsvfs);
4052789Sahrens 		return (ENODEV);
4053789Sahrens 	}
4054789Sahrens 
4055789Sahrens 	/*
4056789Sahrens 	 * If file is locked, disallow mapping.
4057789Sahrens 	 */
40581544Seschrock 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) {
40591544Seschrock 		ZFS_EXIT(zfsvfs);
40601544Seschrock 		return (EAGAIN);
4061789Sahrens 	}
4062789Sahrens 
4063789Sahrens 	as_rangelock(as);
4064789Sahrens 	if ((flags & MAP_FIXED) == 0) {
4065789Sahrens 		map_addr(addrp, len, off, 1, flags);
4066789Sahrens 		if (*addrp == NULL) {
4067789Sahrens 			as_rangeunlock(as);
4068789Sahrens 			ZFS_EXIT(zfsvfs);
4069789Sahrens 			return (ENOMEM);
4070789Sahrens 		}
4071789Sahrens 	} else {
4072789Sahrens 		/*
4073789Sahrens 		 * User specified address - blow away any previous mappings
4074789Sahrens 		 */
4075789Sahrens 		(void) as_unmap(as, *addrp, len);
4076789Sahrens 	}
4077789Sahrens 
4078789Sahrens 	vn_a.vp = vp;
4079789Sahrens 	vn_a.offset = (u_offset_t)off;
4080789Sahrens 	vn_a.type = flags & MAP_TYPE;
4081789Sahrens 	vn_a.prot = prot;
4082789Sahrens 	vn_a.maxprot = maxprot;
4083789Sahrens 	vn_a.cred = cr;
4084789Sahrens 	vn_a.amp = NULL;
4085789Sahrens 	vn_a.flags = flags & ~MAP_TYPE;
40861417Skchow 	vn_a.szc = 0;
40871417Skchow 	vn_a.lgrp_mem_policy_flags = 0;
4088789Sahrens 
4089789Sahrens 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
4090789Sahrens 
4091789Sahrens 	as_rangeunlock(as);
4092789Sahrens 	ZFS_EXIT(zfsvfs);
4093789Sahrens 	return (error);
4094789Sahrens }
4095789Sahrens 
4096789Sahrens /* ARGSUSED */
4097789Sahrens static int
4098789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
40995331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
41005331Samw     caller_context_t *ct)
4101789Sahrens {
41021544Seschrock 	uint64_t pages = btopr(len);
41031544Seschrock 
41041544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4105789Sahrens 	return (0);
4106789Sahrens }
4107789Sahrens 
41081773Seschrock /*
41091773Seschrock  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
41101773Seschrock  * more accurate mtime for the associated file.  Since we don't have a way of
41111773Seschrock  * detecting when the data was actually modified, we have to resort to
41121773Seschrock  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
41131773Seschrock  * last page is pushed.  The problem occurs when the msync() call is omitted,
41141773Seschrock  * which by far the most common case:
41151773Seschrock  *
41161773Seschrock  * 	open()
41171773Seschrock  * 	mmap()
41181773Seschrock  * 	<modify memory>
41191773Seschrock  * 	munmap()
41201773Seschrock  * 	close()
41211773Seschrock  * 	<time lapse>
41221773Seschrock  * 	putpage() via fsflush
41231773Seschrock  *
41241773Seschrock  * If we wait until fsflush to come along, we can have a modification time that
41251773Seschrock  * is some arbitrary point in the future.  In order to prevent this in the
41261773Seschrock  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
41271773Seschrock  * torn down.
41281773Seschrock  */
4129789Sahrens /* ARGSUSED */
4130789Sahrens static int
4131789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
41325331Samw     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
41335331Samw     caller_context_t *ct)
4134789Sahrens {
41351544Seschrock 	uint64_t pages = btopr(len);
41361544Seschrock 
41371544Seschrock 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
41381544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
41391773Seschrock 
41401773Seschrock 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
41411773Seschrock 	    vn_has_cached_data(vp))
41425331Samw 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
41431773Seschrock 
4144789Sahrens 	return (0);
4145789Sahrens }
4146789Sahrens 
4147789Sahrens /*
4148789Sahrens  * Free or allocate space in a file.  Currently, this function only
4149789Sahrens  * supports the `F_FREESP' command.  However, this command is somewhat
4150789Sahrens  * misnamed, as its functionality includes the ability to allocate as
4151789Sahrens  * well as free space.
4152789Sahrens  *
4153789Sahrens  *	IN:	vp	- vnode of file to free data in.
4154789Sahrens  *		cmd	- action to take (only F_FREESP supported).
4155789Sahrens  *		bfp	- section of file to free/alloc.
4156789Sahrens  *		flag	- current file open mode flags.
4157789Sahrens  *		offset	- current file offset.
4158789Sahrens  *		cr	- credentials of caller [UNUSED].
41595331Samw  *		ct	- caller context.
4160789Sahrens  *
4161789Sahrens  *	RETURN:	0 if success
4162789Sahrens  *		error code if failure
4163789Sahrens  *
4164789Sahrens  * Timestamps:
4165789Sahrens  *	vp - ctime|mtime updated
4166789Sahrens  */
4167789Sahrens /* ARGSUSED */
4168789Sahrens static int
4169789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4170789Sahrens     offset_t offset, cred_t *cr, caller_context_t *ct)
4171789Sahrens {
4172789Sahrens 	znode_t		*zp = VTOZ(vp);
4173789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4174789Sahrens 	uint64_t	off, len;
4175789Sahrens 	int		error;
4176789Sahrens 
41775367Sahrens 	ZFS_ENTER(zfsvfs);
41785367Sahrens 	ZFS_VERIFY_ZP(zp);
4179789Sahrens 
4180789Sahrens top:
4181789Sahrens 	if (cmd != F_FREESP) {
4182789Sahrens 		ZFS_EXIT(zfsvfs);
4183789Sahrens 		return (EINVAL);
4184789Sahrens 	}
4185789Sahrens 
4186789Sahrens 	if (error = convoff(vp, bfp, 0, offset)) {
4187789Sahrens 		ZFS_EXIT(zfsvfs);
4188789Sahrens 		return (error);
4189789Sahrens 	}
4190789Sahrens 
4191789Sahrens 	if (bfp->l_len < 0) {
4192789Sahrens 		ZFS_EXIT(zfsvfs);
4193789Sahrens 		return (EINVAL);
4194789Sahrens 	}
4195789Sahrens 
4196789Sahrens 	off = bfp->l_start;
41971669Sperrin 	len = bfp->l_len; /* 0 means from off to end of file */
41981878Smaybee 
41991878Smaybee 	do {
42001878Smaybee 		error = zfs_freesp(zp, off, len, flag, TRUE);
42012113Sahrens 		/* NB: we already did dmu_tx_wait() if necessary */
42021878Smaybee 	} while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
4203789Sahrens 
4204789Sahrens 	ZFS_EXIT(zfsvfs);
4205789Sahrens 	return (error);
4206789Sahrens }
4207789Sahrens 
42085331Samw /*ARGSUSED*/
4209789Sahrens static int
42105331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4211789Sahrens {
4212789Sahrens 	znode_t		*zp = VTOZ(vp);
4213789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
42145326Sek110237 	uint32_t	gen;
4215789Sahrens 	uint64_t	object = zp->z_id;
4216789Sahrens 	zfid_short_t	*zfid;
4217789Sahrens 	int		size, i;
4218789Sahrens 
42195367Sahrens 	ZFS_ENTER(zfsvfs);
42205367Sahrens 	ZFS_VERIFY_ZP(zp);
42215326Sek110237 	gen = (uint32_t)zp->z_gen;
4222789Sahrens 
4223789Sahrens 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4224789Sahrens 	if (fidp->fid_len < size) {
4225789Sahrens 		fidp->fid_len = size;
42261512Sek110237 		ZFS_EXIT(zfsvfs);
4227789Sahrens 		return (ENOSPC);
4228789Sahrens 	}
4229789Sahrens 
4230789Sahrens 	zfid = (zfid_short_t *)fidp;
4231789Sahrens 
4232789Sahrens 	zfid->zf_len = size;
4233789Sahrens 
4234789Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
4235789Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4236789Sahrens 
4237789Sahrens 	/* Must have a non-zero generation number to distinguish from .zfs */
4238789Sahrens 	if (gen == 0)
4239789Sahrens 		gen = 1;
4240789Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
4241789Sahrens 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4242789Sahrens 
4243789Sahrens 	if (size == LONG_FID_LEN) {
4244789Sahrens 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
4245789Sahrens 		zfid_long_t	*zlfid;
4246789Sahrens 
4247789Sahrens 		zlfid = (zfid_long_t *)fidp;
4248789Sahrens 
4249789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4250789Sahrens 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4251789Sahrens 
4252789Sahrens 		/* XXX - this should be the generation number for the objset */
4253789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4254789Sahrens 			zlfid->zf_setgen[i] = 0;
4255789Sahrens 	}
4256789Sahrens 
4257789Sahrens 	ZFS_EXIT(zfsvfs);
4258789Sahrens 	return (0);
4259789Sahrens }
4260789Sahrens 
4261789Sahrens static int
42625331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
42635331Samw     caller_context_t *ct)
4264789Sahrens {
4265789Sahrens 	znode_t		*zp, *xzp;
4266789Sahrens 	zfsvfs_t	*zfsvfs;
4267789Sahrens 	zfs_dirlock_t	*dl;
4268789Sahrens 	int		error;
4269789Sahrens 
4270789Sahrens 	switch (cmd) {
4271789Sahrens 	case _PC_LINK_MAX:
4272789Sahrens 		*valp = ULONG_MAX;
4273789Sahrens 		return (0);
4274789Sahrens 
4275789Sahrens 	case _PC_FILESIZEBITS:
4276789Sahrens 		*valp = 64;
4277789Sahrens 		return (0);
4278789Sahrens 
4279789Sahrens 	case _PC_XATTR_EXISTS:
4280789Sahrens 		zp = VTOZ(vp);
4281789Sahrens 		zfsvfs = zp->z_zfsvfs;
42825367Sahrens 		ZFS_ENTER(zfsvfs);
42835367Sahrens 		ZFS_VERIFY_ZP(zp);
4284789Sahrens 		*valp = 0;
4285789Sahrens 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
42865331Samw 		    ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4287789Sahrens 		if (error == 0) {
4288789Sahrens 			zfs_dirent_unlock(dl);
4289789Sahrens 			if (!zfs_dirempty(xzp))
4290789Sahrens 				*valp = 1;
4291789Sahrens 			VN_RELE(ZTOV(xzp));
4292789Sahrens 		} else if (error == ENOENT) {
4293789Sahrens 			/*
4294789Sahrens 			 * If there aren't extended attributes, it's the
4295789Sahrens 			 * same as having zero of them.
4296789Sahrens 			 */
4297789Sahrens 			error = 0;
4298789Sahrens 		}
4299789Sahrens 		ZFS_EXIT(zfsvfs);
4300789Sahrens 		return (error);
4301789Sahrens 
43025331Samw 	case _PC_SATTR_ENABLED:
43035331Samw 	case _PC_SATTR_EXISTS:
43045331Samw 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) &&
43055331Samw 		    (vp->v_type == VREG || vp->v_type == VDIR);
43065331Samw 		return (0);
43075331Samw 
4308789Sahrens 	case _PC_ACL_ENABLED:
4309789Sahrens 		*valp = _ACL_ACE_ENABLED;
4310789Sahrens 		return (0);
4311789Sahrens 
4312789Sahrens 	case _PC_MIN_HOLE_SIZE:
4313789Sahrens 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
4314789Sahrens 		return (0);
4315789Sahrens 
4316789Sahrens 	default:
43175331Samw 		return (fs_pathconf(vp, cmd, valp, cr, ct));
4318789Sahrens 	}
4319789Sahrens }
4320789Sahrens 
4321789Sahrens /*ARGSUSED*/
4322789Sahrens static int
43235331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
43245331Samw     caller_context_t *ct)
4325789Sahrens {
4326789Sahrens 	znode_t *zp = VTOZ(vp);
4327789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4328789Sahrens 	int error;
43295331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4330789Sahrens 
43315367Sahrens 	ZFS_ENTER(zfsvfs);
43325367Sahrens 	ZFS_VERIFY_ZP(zp);
43335331Samw 	error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4334789Sahrens 	ZFS_EXIT(zfsvfs);
4335789Sahrens 
4336789Sahrens 	return (error);
4337789Sahrens }
4338789Sahrens 
4339789Sahrens /*ARGSUSED*/
4340789Sahrens static int
43415331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
43425331Samw     caller_context_t *ct)
4343789Sahrens {
4344789Sahrens 	znode_t *zp = VTOZ(vp);
4345789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4346789Sahrens 	int error;
43475331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4348789Sahrens 
43495367Sahrens 	ZFS_ENTER(zfsvfs);
43505367Sahrens 	ZFS_VERIFY_ZP(zp);
43515331Samw 	error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4352789Sahrens 	ZFS_EXIT(zfsvfs);
4353789Sahrens 	return (error);
4354789Sahrens }
4355789Sahrens 
4356789Sahrens /*
4357789Sahrens  * Predeclare these here so that the compiler assumes that
4358789Sahrens  * this is an "old style" function declaration that does
4359789Sahrens  * not include arguments => we won't get type mismatch errors
4360789Sahrens  * in the initializations that follow.
4361789Sahrens  */
4362789Sahrens static int zfs_inval();
4363789Sahrens static int zfs_isdir();
4364789Sahrens 
4365789Sahrens static int
4366789Sahrens zfs_inval()
4367789Sahrens {
4368789Sahrens 	return (EINVAL);
4369789Sahrens }
4370789Sahrens 
4371789Sahrens static int
4372789Sahrens zfs_isdir()
4373789Sahrens {
4374789Sahrens 	return (EISDIR);
4375789Sahrens }
4376789Sahrens /*
4377789Sahrens  * Directory vnode operations template
4378789Sahrens  */
4379789Sahrens vnodeops_t *zfs_dvnodeops;
4380789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = {
43813898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
43823898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
43833898Srsb 	VOPNAME_READ,		{ .error = zfs_isdir },
43843898Srsb 	VOPNAME_WRITE,		{ .error = zfs_isdir },
43853898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
43863898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
43873898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
43883898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
43893898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
43903898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
43913898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
43923898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
43933898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
43943898Srsb 	VOPNAME_MKDIR,		{ .vop_mkdir = zfs_mkdir },
43953898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
43963898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
43973898Srsb 	VOPNAME_SYMLINK,	{ .vop_symlink = zfs_symlink },
43983898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
43993898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
44003898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
44013898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
44023898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
44033898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
44043898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
44054863Spraks 	VOPNAME_VNEVENT, 	{ .vop_vnevent = fs_vnevent_support },
44063898Srsb 	NULL,			NULL
4407789Sahrens };
4408789Sahrens 
4409789Sahrens /*
4410789Sahrens  * Regular file vnode operations template
4411789Sahrens  */
4412789Sahrens vnodeops_t *zfs_fvnodeops;
4413789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = {
44143898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
44153898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
44163898Srsb 	VOPNAME_READ,		{ .vop_read = zfs_read },
44173898Srsb 	VOPNAME_WRITE,		{ .vop_write = zfs_write },
44183898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
44193898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
44203898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
44213898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
44223898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
44233898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
44243898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
44253898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
44263898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
44273898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
44283898Srsb 	VOPNAME_FRLOCK,		{ .vop_frlock = zfs_frlock },
44293898Srsb 	VOPNAME_SPACE,		{ .vop_space = zfs_space },
44303898Srsb 	VOPNAME_GETPAGE,	{ .vop_getpage = zfs_getpage },
44313898Srsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = zfs_putpage },
44323898Srsb 	VOPNAME_MAP,		{ .vop_map = zfs_map },
44333898Srsb 	VOPNAME_ADDMAP,		{ .vop_addmap = zfs_addmap },
44343898Srsb 	VOPNAME_DELMAP,		{ .vop_delmap = zfs_delmap },
44353898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
44363898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
44373898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
44383898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
44393898Srsb 	NULL,			NULL
4440789Sahrens };
4441789Sahrens 
4442789Sahrens /*
4443789Sahrens  * Symbolic link vnode operations template
4444789Sahrens  */
4445789Sahrens vnodeops_t *zfs_symvnodeops;
4446789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = {
44473898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
44483898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
44493898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
44503898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
44513898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = zfs_readlink },
44523898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
44533898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
44543898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
44553898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
44563898Srsb 	NULL,			NULL
4457789Sahrens };
4458789Sahrens 
4459789Sahrens /*
4460789Sahrens  * Extended attribute directory vnode operations template
4461789Sahrens  *	This template is identical to the directory vnodes
4462789Sahrens  *	operation template except for restricted operations:
4463789Sahrens  *		VOP_MKDIR()
4464789Sahrens  *		VOP_SYMLINK()
4465789Sahrens  * Note that there are other restrictions embedded in:
4466789Sahrens  *	zfs_create()	- restrict type to VREG
4467789Sahrens  *	zfs_link()	- no links into/out of attribute space
4468789Sahrens  *	zfs_rename()	- no moves into/out of attribute space
4469789Sahrens  */
4470789Sahrens vnodeops_t *zfs_xdvnodeops;
4471789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = {
44723898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
44733898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
44743898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
44753898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
44763898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
44773898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
44783898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
44793898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
44803898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
44813898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
44823898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
44833898Srsb 	VOPNAME_MKDIR,		{ .error = zfs_inval },
44843898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
44853898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
44863898Srsb 	VOPNAME_SYMLINK,	{ .error = zfs_inval },
44873898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
44883898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
44893898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
44903898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
44913898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
44923898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
44933898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
44943898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
44953898Srsb 	NULL,			NULL
4496789Sahrens };
4497789Sahrens 
4498789Sahrens /*
4499789Sahrens  * Error vnode operations template
4500789Sahrens  */
4501789Sahrens vnodeops_t *zfs_evnodeops;
4502789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = {
45033898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
45043898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
45053898Srsb 	NULL,			NULL
4506789Sahrens };
4507