xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 9909:aa280f585a3e)
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 /*
228636SMark.Maybee@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
264144Speteh /* Portions Copyright 2007 Jeremy Teo */
274144Speteh 
28789Sahrens #include <sys/types.h>
29789Sahrens #include <sys/param.h>
30789Sahrens #include <sys/time.h>
31789Sahrens #include <sys/systm.h>
32789Sahrens #include <sys/sysmacros.h>
33789Sahrens #include <sys/resource.h>
34789Sahrens #include <sys/vfs.h>
353898Srsb #include <sys/vfs_opreg.h>
36789Sahrens #include <sys/vnode.h>
37789Sahrens #include <sys/file.h>
38789Sahrens #include <sys/stat.h>
39789Sahrens #include <sys/kmem.h>
40789Sahrens #include <sys/taskq.h>
41789Sahrens #include <sys/uio.h>
42789Sahrens #include <sys/vmsystm.h>
43789Sahrens #include <sys/atomic.h>
442688Smaybee #include <sys/vm.h>
45789Sahrens #include <vm/seg_vn.h>
46789Sahrens #include <vm/pvn.h>
47789Sahrens #include <vm/as.h>
487315SJonathan.Adams@Sun.COM #include <vm/kpm.h>
497315SJonathan.Adams@Sun.COM #include <vm/seg_kpm.h>
50789Sahrens #include <sys/mman.h>
51789Sahrens #include <sys/pathname.h>
52789Sahrens #include <sys/cmn_err.h>
53789Sahrens #include <sys/errno.h>
54789Sahrens #include <sys/unistd.h>
55789Sahrens #include <sys/zfs_dir.h>
56789Sahrens #include <sys/zfs_acl.h>
57789Sahrens #include <sys/zfs_ioctl.h>
58789Sahrens #include <sys/fs/zfs.h>
59789Sahrens #include <sys/dmu.h>
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>
687847SMark.Shellenbaum@Sun.COM #include <sys/sid.h>
69789Sahrens #include "fs/fs_subr.h"
70789Sahrens #include <sys/zfs_ctldir.h>
715331Samw #include <sys/zfs_fuid.h>
721484Sek110237 #include <sys/dnlc.h>
731669Sperrin #include <sys/zfs_rlock.h>
745331Samw #include <sys/extdirent.h>
755331Samw #include <sys/kidmap.h>
765331Samw #include <sys/cred_impl.h>
775663Sck153898 #include <sys/attr.h>
78789Sahrens 
79789Sahrens /*
80789Sahrens  * Programming rules.
81789Sahrens  *
82789Sahrens  * Each vnode op performs some logical unit of work.  To do this, the ZPL must
83789Sahrens  * properly lock its in-core state, create a DMU transaction, do the work,
84789Sahrens  * record this work in the intent log (ZIL), commit the DMU transaction,
855331Samw  * and wait for the intent log to commit if it is a synchronous operation.
865331Samw  * Moreover, the vnode ops must work in both normal and log replay context.
87789Sahrens  * The ordering of events is important to avoid deadlocks and references
88789Sahrens  * to freed memory.  The example below illustrates the following Big Rules:
89789Sahrens  *
90789Sahrens  *  (1) A check must be made in each zfs thread for a mounted file system.
915367Sahrens  *	This is done avoiding races using ZFS_ENTER(zfsvfs).
925367Sahrens  *      A ZFS_EXIT(zfsvfs) is needed before all returns.  Any znodes
935367Sahrens  *      must be checked with ZFS_VERIFY_ZP(zp).  Both of these macros
945367Sahrens  *      can return EIO from the calling function.
95789Sahrens  *
96789Sahrens  *  (2)	VN_RELE() should always be the last thing except for zil_commit()
972638Sperrin  *	(if necessary) and ZFS_EXIT(). This is for 3 reasons:
98789Sahrens  *	First, if it's the last reference, the vnode/znode
99789Sahrens  *	can be freed, so the zp may point to freed memory.  Second, the last
100789Sahrens  *	reference will call zfs_zinactive(), which may induce a lot of work --
1011669Sperrin  *	pushing cached pages (which acquires range locks) and syncing out
102789Sahrens  *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
103789Sahrens  *	which could deadlock the system if you were already holding one.
1049321SNeil.Perrin@Sun.COM  *	If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
105789Sahrens  *
1061757Sperrin  *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
1071757Sperrin  *	as they can span dmu_tx_assign() calls.
1081757Sperrin  *
1098227SNeil.Perrin@Sun.COM  *  (4)	Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
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.
1268227SNeil.Perrin@Sun.COM  *      During ZIL replay the zfs_log_* functions will update the sequence
1278227SNeil.Perrin@Sun.COM  *	number to indicate the zil transaction has replayed.
128789Sahrens  *
1291757Sperrin  *  (6)	At the end of each vnode op, the DMU tx must always commit,
130789Sahrens  *	regardless of whether there were any errors.
131789Sahrens  *
1322638Sperrin  *  (7)	After dropping all locks, invoke zil_commit(zilog, seq, foid)
133789Sahrens  *	to ensure that synchronous semantics are provided when necessary.
134789Sahrens  *
135789Sahrens  * In general, this is how things should be ordered in each vnode op:
136789Sahrens  *
137789Sahrens  *	ZFS_ENTER(zfsvfs);		// exit if unmounted
138789Sahrens  * top:
139789Sahrens  *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
140789Sahrens  *	rw_enter(...);			// grab any other locks you need
141789Sahrens  *	tx = dmu_tx_create(...);	// get DMU tx
142789Sahrens  *	dmu_tx_hold_*();		// hold each object you might modify
1438227SNeil.Perrin@Sun.COM  *	error = dmu_tx_assign(tx, TXG_NOWAIT);	// try to assign
144789Sahrens  *	if (error) {
145789Sahrens  *		rw_exit(...);		// drop locks
146789Sahrens  *		zfs_dirent_unlock(dl);	// unlock directory entry
147789Sahrens  *		VN_RELE(...);		// release held vnodes
1488227SNeil.Perrin@Sun.COM  *		if (error == ERESTART) {
1492113Sahrens  *			dmu_tx_wait(tx);
1502113Sahrens  *			dmu_tx_abort(tx);
151789Sahrens  *			goto top;
152789Sahrens  *		}
1532113Sahrens  *		dmu_tx_abort(tx);	// abort DMU tx
154789Sahrens  *		ZFS_EXIT(zfsvfs);	// finished in zfs
155789Sahrens  *		return (error);		// really out of space
156789Sahrens  *	}
157789Sahrens  *	error = do_real_work();		// do whatever this VOP does
158789Sahrens  *	if (error == 0)
1592638Sperrin  *		zfs_log_*(...);		// on success, make ZIL entry
160789Sahrens  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
161789Sahrens  *	rw_exit(...);			// drop locks
162789Sahrens  *	zfs_dirent_unlock(dl);		// unlock directory entry
163789Sahrens  *	VN_RELE(...);			// release held vnodes
1642638Sperrin  *	zil_commit(zilog, seq, foid);	// synchronous when necessary
165789Sahrens  *	ZFS_EXIT(zfsvfs);		// finished in zfs
166789Sahrens  *	return (error);			// done, report error
167789Sahrens  */
1685367Sahrens 
169789Sahrens /* ARGSUSED */
170789Sahrens static int
1715331Samw zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
172789Sahrens {
1733063Sperrin 	znode_t	*zp = VTOZ(*vpp);
1747844SMark.Shellenbaum@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1757844SMark.Shellenbaum@Sun.COM 
1767844SMark.Shellenbaum@Sun.COM 	ZFS_ENTER(zfsvfs);
1777844SMark.Shellenbaum@Sun.COM 	ZFS_VERIFY_ZP(zp);
1783063Sperrin 
1795331Samw 	if ((flag & FWRITE) && (zp->z_phys->zp_flags & ZFS_APPENDONLY) &&
1805331Samw 	    ((flag & FAPPEND) == 0)) {
1817844SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
1825331Samw 		return (EPERM);
1835331Samw 	}
1845331Samw 
1855331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
1865331Samw 	    ZTOV(zp)->v_type == VREG &&
1875331Samw 	    !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
1887844SMark.Shellenbaum@Sun.COM 	    zp->z_phys->zp_size > 0) {
1897844SMark.Shellenbaum@Sun.COM 		if (fs_vscan(*vpp, cr, 0) != 0) {
1907844SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
1915331Samw 			return (EACCES);
1927844SMark.Shellenbaum@Sun.COM 		}
1937844SMark.Shellenbaum@Sun.COM 	}
1945331Samw 
1953063Sperrin 	/* Keep a count of the synchronous opens in the znode */
1963063Sperrin 	if (flag & (FSYNC | FDSYNC))
1973063Sperrin 		atomic_inc_32(&zp->z_sync_cnt);
1985331Samw 
1997844SMark.Shellenbaum@Sun.COM 	ZFS_EXIT(zfsvfs);
200789Sahrens 	return (0);
201789Sahrens }
202789Sahrens 
203789Sahrens /* ARGSUSED */
204789Sahrens static int
2055331Samw zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
2065331Samw     caller_context_t *ct)
207789Sahrens {
2083063Sperrin 	znode_t	*zp = VTOZ(vp);
2097844SMark.Shellenbaum@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2107844SMark.Shellenbaum@Sun.COM 
211*9909Schris.kirby@sun.com 	/*
212*9909Schris.kirby@sun.com 	 * Clean up any locks held by this process on the vp.
213*9909Schris.kirby@sun.com 	 */
214*9909Schris.kirby@sun.com 	cleanlocks(vp, ddi_get_pid(), 0);
215*9909Schris.kirby@sun.com 	cleanshares(vp, ddi_get_pid());
216*9909Schris.kirby@sun.com 
2177844SMark.Shellenbaum@Sun.COM 	ZFS_ENTER(zfsvfs);
2187844SMark.Shellenbaum@Sun.COM 	ZFS_VERIFY_ZP(zp);
2193063Sperrin 
2203063Sperrin 	/* Decrement the synchronous opens in the znode */
2214339Sperrin 	if ((flag & (FSYNC | FDSYNC)) && (count == 1))
2223063Sperrin 		atomic_dec_32(&zp->z_sync_cnt);
2233063Sperrin 
2245331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
2255331Samw 	    ZTOV(zp)->v_type == VREG &&
2265331Samw 	    !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
2275331Samw 	    zp->z_phys->zp_size > 0)
2285331Samw 		VERIFY(fs_vscan(vp, cr, 1) == 0);
2295331Samw 
2307844SMark.Shellenbaum@Sun.COM 	ZFS_EXIT(zfsvfs);
231789Sahrens 	return (0);
232789Sahrens }
233789Sahrens 
234789Sahrens /*
235789Sahrens  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
236789Sahrens  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
237789Sahrens  */
238789Sahrens static int
239789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off)
240789Sahrens {
241789Sahrens 	znode_t	*zp = VTOZ(vp);
242789Sahrens 	uint64_t noff = (uint64_t)*off; /* new offset */
243789Sahrens 	uint64_t file_sz;
244789Sahrens 	int error;
245789Sahrens 	boolean_t hole;
246789Sahrens 
247789Sahrens 	file_sz = zp->z_phys->zp_size;
248789Sahrens 	if (noff >= file_sz)  {
249789Sahrens 		return (ENXIO);
250789Sahrens 	}
251789Sahrens 
252789Sahrens 	if (cmd == _FIO_SEEK_HOLE)
253789Sahrens 		hole = B_TRUE;
254789Sahrens 	else
255789Sahrens 		hole = B_FALSE;
256789Sahrens 
257789Sahrens 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
258789Sahrens 
259789Sahrens 	/* end of file? */
260789Sahrens 	if ((error == ESRCH) || (noff > file_sz)) {
261789Sahrens 		/*
262789Sahrens 		 * Handle the virtual hole at the end of file.
263789Sahrens 		 */
264789Sahrens 		if (hole) {
265789Sahrens 			*off = file_sz;
266789Sahrens 			return (0);
267789Sahrens 		}
268789Sahrens 		return (ENXIO);
269789Sahrens 	}
270789Sahrens 
271789Sahrens 	if (noff < *off)
272789Sahrens 		return (error);
273789Sahrens 	*off = noff;
274789Sahrens 	return (error);
275789Sahrens }
276789Sahrens 
277789Sahrens /* ARGSUSED */
278789Sahrens static int
279789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
2805331Samw     int *rvalp, caller_context_t *ct)
281789Sahrens {
282789Sahrens 	offset_t off;
283789Sahrens 	int error;
284789Sahrens 	zfsvfs_t *zfsvfs;
2855326Sek110237 	znode_t *zp;
286789Sahrens 
287789Sahrens 	switch (com) {
2884339Sperrin 	case _FIOFFS:
289789Sahrens 		return (zfs_sync(vp->v_vfsp, 0, cred));
290789Sahrens 
2911544Seschrock 		/*
2921544Seschrock 		 * The following two ioctls are used by bfu.  Faking out,
2931544Seschrock 		 * necessary to avoid bfu errors.
2941544Seschrock 		 */
2954339Sperrin 	case _FIOGDIO:
2964339Sperrin 	case _FIOSDIO:
2971544Seschrock 		return (0);
2981544Seschrock 
2994339Sperrin 	case _FIO_SEEK_DATA:
3004339Sperrin 	case _FIO_SEEK_HOLE:
301789Sahrens 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
302789Sahrens 			return (EFAULT);
303789Sahrens 
3045326Sek110237 		zp = VTOZ(vp);
3055326Sek110237 		zfsvfs = zp->z_zfsvfs;
3065367Sahrens 		ZFS_ENTER(zfsvfs);
3075367Sahrens 		ZFS_VERIFY_ZP(zp);
308789Sahrens 
309789Sahrens 		/* offset parameter is in/out */
310789Sahrens 		error = zfs_holey(vp, com, &off);
311789Sahrens 		ZFS_EXIT(zfsvfs);
312789Sahrens 		if (error)
313789Sahrens 			return (error);
314789Sahrens 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
315789Sahrens 			return (EFAULT);
316789Sahrens 		return (0);
317789Sahrens 	}
318789Sahrens 	return (ENOTTY);
319789Sahrens }
320789Sahrens 
321789Sahrens /*
3227315SJonathan.Adams@Sun.COM  * Utility functions to map and unmap a single physical page.  These
3237315SJonathan.Adams@Sun.COM  * are used to manage the mappable copies of ZFS file data, and therefore
3247315SJonathan.Adams@Sun.COM  * do not update ref/mod bits.
3257315SJonathan.Adams@Sun.COM  */
3267315SJonathan.Adams@Sun.COM caddr_t
3277315SJonathan.Adams@Sun.COM zfs_map_page(page_t *pp, enum seg_rw rw)
3287315SJonathan.Adams@Sun.COM {
3297315SJonathan.Adams@Sun.COM 	if (kpm_enable)
3307315SJonathan.Adams@Sun.COM 		return (hat_kpm_mapin(pp, 0));
3317315SJonathan.Adams@Sun.COM 	ASSERT(rw == S_READ || rw == S_WRITE);
3327315SJonathan.Adams@Sun.COM 	return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0),
3337315SJonathan.Adams@Sun.COM 	    (caddr_t)-1));
3347315SJonathan.Adams@Sun.COM }
3357315SJonathan.Adams@Sun.COM 
3367315SJonathan.Adams@Sun.COM void
3377315SJonathan.Adams@Sun.COM zfs_unmap_page(page_t *pp, caddr_t addr)
3387315SJonathan.Adams@Sun.COM {
3397315SJonathan.Adams@Sun.COM 	if (kpm_enable) {
3407315SJonathan.Adams@Sun.COM 		hat_kpm_mapout(pp, 0, addr);
3417315SJonathan.Adams@Sun.COM 	} else {
3427315SJonathan.Adams@Sun.COM 		ppmapout(addr);
3437315SJonathan.Adams@Sun.COM 	}
3447315SJonathan.Adams@Sun.COM }
3457315SJonathan.Adams@Sun.COM 
3467315SJonathan.Adams@Sun.COM /*
347789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
348789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
349789Sahrens  *
350789Sahrens  * On Write:	If we find a memory mapped page, we write to *both*
351789Sahrens  *		the page and the dmu buffer.
352789Sahrens  */
3538636SMark.Maybee@Sun.COM static void
3548636SMark.Maybee@Sun.COM update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid)
355789Sahrens {
3568636SMark.Maybee@Sun.COM 	int64_t	off;
3578636SMark.Maybee@Sun.COM 
358789Sahrens 	off = start & PAGEOFFSET;
359789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
360789Sahrens 		page_t *pp;
3618636SMark.Maybee@Sun.COM 		uint64_t nbytes = MIN(PAGESIZE - off, len);
3628636SMark.Maybee@Sun.COM 
363789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
364789Sahrens 			caddr_t va;
365789Sahrens 
3667315SJonathan.Adams@Sun.COM 			va = zfs_map_page(pp, S_WRITE);
3679512SNeil.Perrin@Sun.COM 			(void) dmu_read(os, oid, start+off, nbytes, va+off,
3689512SNeil.Perrin@Sun.COM 			    DMU_READ_PREFETCH);
3697315SJonathan.Adams@Sun.COM 			zfs_unmap_page(pp, va);
370789Sahrens 			page_unlock(pp);
371789Sahrens 		}
3728636SMark.Maybee@Sun.COM 		len -= nbytes;
373789Sahrens 		off = 0;
374789Sahrens 	}
375789Sahrens }
376789Sahrens 
377789Sahrens /*
378789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
379789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
380789Sahrens  *
381789Sahrens  * On Read:	We "read" preferentially from memory mapped pages,
382789Sahrens  *		else we default from the dmu buffer.
383789Sahrens  *
384789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
385789Sahrens  *	the file is memory mapped.
386789Sahrens  */
387789Sahrens static int
3883638Sbillm mappedread(vnode_t *vp, int nbytes, uio_t *uio)
389789Sahrens {
3903638Sbillm 	znode_t *zp = VTOZ(vp);
3913638Sbillm 	objset_t *os = zp->z_zfsvfs->z_os;
3923638Sbillm 	int64_t	start, off;
393789Sahrens 	int len = nbytes;
394789Sahrens 	int error = 0;
395789Sahrens 
396789Sahrens 	start = uio->uio_loffset;
397789Sahrens 	off = start & PAGEOFFSET;
398789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
399789Sahrens 		page_t *pp;
4003638Sbillm 		uint64_t bytes = MIN(PAGESIZE - off, len);
4013638Sbillm 
402789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
403789Sahrens 			caddr_t va;
404789Sahrens 
4057315SJonathan.Adams@Sun.COM 			va = zfs_map_page(pp, S_READ);
406789Sahrens 			error = uiomove(va + off, bytes, UIO_READ, uio);
4077315SJonathan.Adams@Sun.COM 			zfs_unmap_page(pp, va);
408789Sahrens 			page_unlock(pp);
409789Sahrens 		} else {
4103638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, bytes);
411789Sahrens 		}
412789Sahrens 		len -= bytes;
413789Sahrens 		off = 0;
414789Sahrens 		if (error)
415789Sahrens 			break;
416789Sahrens 	}
417789Sahrens 	return (error);
418789Sahrens }
419789Sahrens 
4203638Sbillm offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
421789Sahrens 
422789Sahrens /*
423789Sahrens  * Read bytes from specified file into supplied buffer.
424789Sahrens  *
425789Sahrens  *	IN:	vp	- vnode of file to be read from.
426789Sahrens  *		uio	- structure supplying read location, range info,
427789Sahrens  *			  and return buffer.
428789Sahrens  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
429789Sahrens  *		cr	- credentials of caller.
4305331Samw  *		ct	- caller context
431789Sahrens  *
432789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
433789Sahrens  *
434789Sahrens  *	RETURN:	0 if success
435789Sahrens  *		error code if failure
436789Sahrens  *
437789Sahrens  * Side Effects:
438789Sahrens  *	vp - atime updated if byte count > 0
439789Sahrens  */
440789Sahrens /* ARGSUSED */
441789Sahrens static int
442789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
443789Sahrens {
444789Sahrens 	znode_t		*zp = VTOZ(vp);
445789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4465326Sek110237 	objset_t	*os;
4473638Sbillm 	ssize_t		n, nbytes;
4483638Sbillm 	int		error;
4491669Sperrin 	rl_t		*rl;
450789Sahrens 
4515367Sahrens 	ZFS_ENTER(zfsvfs);
4525367Sahrens 	ZFS_VERIFY_ZP(zp);
4535326Sek110237 	os = zfsvfs->z_os;
454789Sahrens 
4555929Smarks 	if (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) {
4565929Smarks 		ZFS_EXIT(zfsvfs);
4575929Smarks 		return (EACCES);
4585929Smarks 	}
4595929Smarks 
460789Sahrens 	/*
461789Sahrens 	 * Validate file offset
462789Sahrens 	 */
463789Sahrens 	if (uio->uio_loffset < (offset_t)0) {
464789Sahrens 		ZFS_EXIT(zfsvfs);
465789Sahrens 		return (EINVAL);
466789Sahrens 	}
467789Sahrens 
468789Sahrens 	/*
469789Sahrens 	 * Fasttrack empty reads
470789Sahrens 	 */
471789Sahrens 	if (uio->uio_resid == 0) {
472789Sahrens 		ZFS_EXIT(zfsvfs);
473789Sahrens 		return (0);
474789Sahrens 	}
475789Sahrens 
476789Sahrens 	/*
4771669Sperrin 	 * Check for mandatory locks
478789Sahrens 	 */
479789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
480789Sahrens 		if (error = chklock(vp, FREAD,
481789Sahrens 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
482789Sahrens 			ZFS_EXIT(zfsvfs);
483789Sahrens 			return (error);
484789Sahrens 		}
485789Sahrens 	}
486789Sahrens 
487789Sahrens 	/*
488789Sahrens 	 * If we're in FRSYNC mode, sync out this znode before reading it.
489789Sahrens 	 */
4902638Sperrin 	if (ioflag & FRSYNC)
4912638Sperrin 		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
492789Sahrens 
493789Sahrens 	/*
4941669Sperrin 	 * Lock the range against changes.
495789Sahrens 	 */
4961669Sperrin 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
4971669Sperrin 
498789Sahrens 	/*
499789Sahrens 	 * If we are reading past end-of-file we can skip
500789Sahrens 	 * to the end; but we might still need to set atime.
501789Sahrens 	 */
502789Sahrens 	if (uio->uio_loffset >= zp->z_phys->zp_size) {
503789Sahrens 		error = 0;
504789Sahrens 		goto out;
505789Sahrens 	}
506789Sahrens 
5073638Sbillm 	ASSERT(uio->uio_loffset < zp->z_phys->zp_size);
5083638Sbillm 	n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset);
5093638Sbillm 
5103638Sbillm 	while (n > 0) {
5113638Sbillm 		nbytes = MIN(n, zfs_read_chunk_size -
5123638Sbillm 		    P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
5133638Sbillm 
5143638Sbillm 		if (vn_has_cached_data(vp))
5153638Sbillm 			error = mappedread(vp, nbytes, uio);
5163638Sbillm 		else
5173638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, nbytes);
5187294Sperrin 		if (error) {
5197294Sperrin 			/* convert checksum errors into IO errors */
5207294Sperrin 			if (error == ECKSUM)
5217294Sperrin 				error = EIO;
5223638Sbillm 			break;
5237294Sperrin 		}
5243638Sbillm 
5253638Sbillm 		n -= nbytes;
526789Sahrens 	}
5273638Sbillm 
528789Sahrens out:
5292237Smaybee 	zfs_range_unlock(rl);
530789Sahrens 
531789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
532789Sahrens 	ZFS_EXIT(zfsvfs);
533789Sahrens 	return (error);
534789Sahrens }
535789Sahrens 
536789Sahrens /*
537789Sahrens  * Write the bytes to a file.
538789Sahrens  *
539789Sahrens  *	IN:	vp	- vnode of file to be written to.
540789Sahrens  *		uio	- structure supplying write location, range info,
541789Sahrens  *			  and data buffer.
542789Sahrens  *		ioflag	- FAPPEND flag set if in append mode.
543789Sahrens  *		cr	- credentials of caller.
5445331Samw  *		ct	- caller context (NFS/CIFS fem monitor only)
545789Sahrens  *
546789Sahrens  *	OUT:	uio	- updated offset and range.
547789Sahrens  *
548789Sahrens  *	RETURN:	0 if success
549789Sahrens  *		error code if failure
550789Sahrens  *
551789Sahrens  * Timestamps:
552789Sahrens  *	vp - ctime|mtime updated if byte count > 0
553789Sahrens  */
554789Sahrens /* ARGSUSED */
555789Sahrens static int
556789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
557789Sahrens {
558789Sahrens 	znode_t		*zp = VTOZ(vp);
559789Sahrens 	rlim64_t	limit = uio->uio_llimit;
560789Sahrens 	ssize_t		start_resid = uio->uio_resid;
561789Sahrens 	ssize_t		tx_bytes;
562789Sahrens 	uint64_t	end_size;
563789Sahrens 	dmu_tx_t	*tx;
564789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
5655326Sek110237 	zilog_t		*zilog;
566789Sahrens 	offset_t	woff;
567789Sahrens 	ssize_t		n, nbytes;
5681669Sperrin 	rl_t		*rl;
569789Sahrens 	int		max_blksz = zfsvfs->z_max_blksz;
5706743Smarks 	uint64_t	pflags;
5711669Sperrin 	int		error;
5729412SAleksandr.Guzovskiy@Sun.COM 	arc_buf_t	*abuf;
573789Sahrens 
574789Sahrens 	/*
575789Sahrens 	 * Fasttrack empty write
576789Sahrens 	 */
5771669Sperrin 	n = start_resid;
578789Sahrens 	if (n == 0)
579789Sahrens 		return (0);
580789Sahrens 
5811669Sperrin 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
5821669Sperrin 		limit = MAXOFFSET_T;
5831669Sperrin 
5845367Sahrens 	ZFS_ENTER(zfsvfs);
5855367Sahrens 	ZFS_VERIFY_ZP(zp);
5866743Smarks 
5876743Smarks 	/*
5886743Smarks 	 * If immutable or not appending then return EPERM
5896743Smarks 	 */
5906743Smarks 	pflags = zp->z_phys->zp_flags;
5916743Smarks 	if ((pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
5926743Smarks 	    ((pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
5936743Smarks 	    (uio->uio_loffset < zp->z_phys->zp_size))) {
5946743Smarks 		ZFS_EXIT(zfsvfs);
5956743Smarks 		return (EPERM);
5966743Smarks 	}
5976743Smarks 
5985326Sek110237 	zilog = zfsvfs->z_log;
599789Sahrens 
600789Sahrens 	/*
6012237Smaybee 	 * Pre-fault the pages to ensure slow (eg NFS) pages
6021669Sperrin 	 * don't hold up txg.
603789Sahrens 	 */
6048059SDonghai.Qiao@Sun.COM 	uio_prefaultpages(n, uio);
605789Sahrens 
606789Sahrens 	/*
607789Sahrens 	 * If in append mode, set the io offset pointer to eof.
608789Sahrens 	 */
6091669Sperrin 	if (ioflag & FAPPEND) {
6101669Sperrin 		/*
6111669Sperrin 		 * Range lock for a file append:
6121669Sperrin 		 * The value for the start of range will be determined by
6131669Sperrin 		 * zfs_range_lock() (to guarantee append semantics).
6141669Sperrin 		 * If this write will cause the block size to increase,
6151669Sperrin 		 * zfs_range_lock() will lock the entire file, so we must
6161669Sperrin 		 * later reduce the range after we grow the block size.
6171669Sperrin 		 */
6181669Sperrin 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
6191669Sperrin 		if (rl->r_len == UINT64_MAX) {
6201669Sperrin 			/* overlocked, zp_size can't change */
6211669Sperrin 			woff = uio->uio_loffset = zp->z_phys->zp_size;
6221669Sperrin 		} else {
6231669Sperrin 			woff = uio->uio_loffset = rl->r_off;
6241669Sperrin 		}
625789Sahrens 	} else {
626789Sahrens 		woff = uio->uio_loffset;
627789Sahrens 		/*
628789Sahrens 		 * Validate file offset
629789Sahrens 		 */
630789Sahrens 		if (woff < 0) {
631789Sahrens 			ZFS_EXIT(zfsvfs);
632789Sahrens 			return (EINVAL);
633789Sahrens 		}
634789Sahrens 
635789Sahrens 		/*
6361669Sperrin 		 * If we need to grow the block size then zfs_range_lock()
6371669Sperrin 		 * will lock a wider range than we request here.
6381669Sperrin 		 * Later after growing the block size we reduce the range.
639789Sahrens 		 */
6401669Sperrin 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
641789Sahrens 	}
642789Sahrens 
643789Sahrens 	if (woff >= limit) {
6443638Sbillm 		zfs_range_unlock(rl);
6453638Sbillm 		ZFS_EXIT(zfsvfs);
6463638Sbillm 		return (EFBIG);
647789Sahrens 	}
648789Sahrens 
649789Sahrens 	if ((woff + n) > limit || woff > (limit - n))
650789Sahrens 		n = limit - woff;
651789Sahrens 
652789Sahrens 	/*
6531669Sperrin 	 * Check for mandatory locks
654789Sahrens 	 */
655789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) &&
6563638Sbillm 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
6573638Sbillm 		zfs_range_unlock(rl);
6583638Sbillm 		ZFS_EXIT(zfsvfs);
6593638Sbillm 		return (error);
6603638Sbillm 	}
6611669Sperrin 	end_size = MAX(zp->z_phys->zp_size, woff + n);
662789Sahrens 
6631669Sperrin 	/*
6643638Sbillm 	 * Write the file in reasonable size chunks.  Each chunk is written
6653638Sbillm 	 * in a separate transaction; this keeps the intent log records small
6663638Sbillm 	 * and allows us to do more fine-grained space accounting.
667789Sahrens 	 */
668789Sahrens 	while (n > 0) {
6699412SAleksandr.Guzovskiy@Sun.COM 		abuf = NULL;
6709412SAleksandr.Guzovskiy@Sun.COM 		woff = uio->uio_loffset;
6719412SAleksandr.Guzovskiy@Sun.COM 
6729412SAleksandr.Guzovskiy@Sun.COM again:
6739396SMatthew.Ahrens@Sun.COM 		if (zfs_usergroup_overquota(zfsvfs,
6749396SMatthew.Ahrens@Sun.COM 		    B_FALSE, zp->z_phys->zp_uid) ||
6759396SMatthew.Ahrens@Sun.COM 		    zfs_usergroup_overquota(zfsvfs,
6769396SMatthew.Ahrens@Sun.COM 		    B_TRUE, zp->z_phys->zp_gid)) {
6779412SAleksandr.Guzovskiy@Sun.COM 			if (abuf != NULL)
6789412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
6799396SMatthew.Ahrens@Sun.COM 			error = EDQUOT;
6809396SMatthew.Ahrens@Sun.COM 			break;
6819396SMatthew.Ahrens@Sun.COM 		}
6829412SAleksandr.Guzovskiy@Sun.COM 
6839412SAleksandr.Guzovskiy@Sun.COM 		/*
6849412SAleksandr.Guzovskiy@Sun.COM 		 * If dmu_assign_arcbuf() is expected to execute with minimum
6859412SAleksandr.Guzovskiy@Sun.COM 		 * overhead loan an arc buffer and copy user data to it before
6869412SAleksandr.Guzovskiy@Sun.COM 		 * we enter a txg.  This avoids holding a txg forever while we
6879412SAleksandr.Guzovskiy@Sun.COM 		 * pagefault on a hanging NFS server mapping.
6889412SAleksandr.Guzovskiy@Sun.COM 		 */
6899412SAleksandr.Guzovskiy@Sun.COM 		if (abuf == NULL && n >= max_blksz &&
6909412SAleksandr.Guzovskiy@Sun.COM 		    woff >= zp->z_phys->zp_size &&
6919412SAleksandr.Guzovskiy@Sun.COM 		    P2PHASE(woff, max_blksz) == 0 &&
6929412SAleksandr.Guzovskiy@Sun.COM 		    zp->z_blksz == max_blksz) {
6939412SAleksandr.Guzovskiy@Sun.COM 			size_t cbytes;
6949412SAleksandr.Guzovskiy@Sun.COM 
6959412SAleksandr.Guzovskiy@Sun.COM 			abuf = dmu_request_arcbuf(zp->z_dbuf, max_blksz);
6969412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(abuf != NULL);
6979412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(arc_buf_size(abuf) == max_blksz);
6989412SAleksandr.Guzovskiy@Sun.COM 			if (error = uiocopy(abuf->b_data, max_blksz,
6999412SAleksandr.Guzovskiy@Sun.COM 			    UIO_WRITE, uio, &cbytes)) {
7009412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7019412SAleksandr.Guzovskiy@Sun.COM 				break;
7029412SAleksandr.Guzovskiy@Sun.COM 			}
7039412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(cbytes == max_blksz);
7049412SAleksandr.Guzovskiy@Sun.COM 		}
7059412SAleksandr.Guzovskiy@Sun.COM 
7069412SAleksandr.Guzovskiy@Sun.COM 		/*
7079412SAleksandr.Guzovskiy@Sun.COM 		 * Start a transaction.
7089412SAleksandr.Guzovskiy@Sun.COM 		 */
709789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
710789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
711789Sahrens 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
7128227SNeil.Perrin@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
713789Sahrens 		if (error) {
7148227SNeil.Perrin@Sun.COM 			if (error == ERESTART) {
7152113Sahrens 				dmu_tx_wait(tx);
7162113Sahrens 				dmu_tx_abort(tx);
7179412SAleksandr.Guzovskiy@Sun.COM 				goto again;
718789Sahrens 			}
7192113Sahrens 			dmu_tx_abort(tx);
7209412SAleksandr.Guzovskiy@Sun.COM 			if (abuf != NULL)
7219412SAleksandr.Guzovskiy@Sun.COM 				dmu_return_arcbuf(abuf);
7223638Sbillm 			break;
7233638Sbillm 		}
7243638Sbillm 
7253638Sbillm 		/*
7263638Sbillm 		 * If zfs_range_lock() over-locked we grow the blocksize
7273638Sbillm 		 * and then reduce the lock range.  This will only happen
7283638Sbillm 		 * on the first iteration since zfs_range_reduce() will
7293638Sbillm 		 * shrink down r_len to the appropriate size.
7303638Sbillm 		 */
7313638Sbillm 		if (rl->r_len == UINT64_MAX) {
7323638Sbillm 			uint64_t new_blksz;
7333638Sbillm 
7343638Sbillm 			if (zp->z_blksz > max_blksz) {
7353638Sbillm 				ASSERT(!ISP2(zp->z_blksz));
7363638Sbillm 				new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
7373638Sbillm 			} else {
7383638Sbillm 				new_blksz = MIN(end_size, max_blksz);
7393638Sbillm 			}
7403638Sbillm 			zfs_grow_blocksize(zp, new_blksz, tx);
7413638Sbillm 			zfs_range_reduce(rl, woff, n);
7423638Sbillm 		}
7433638Sbillm 
7443638Sbillm 		/*
7453638Sbillm 		 * XXX - should we really limit each write to z_max_blksz?
7463638Sbillm 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
7473638Sbillm 		 */
7483638Sbillm 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
7493638Sbillm 
7509412SAleksandr.Guzovskiy@Sun.COM 		if (abuf == NULL) {
7519412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes = uio->uio_resid;
7529412SAleksandr.Guzovskiy@Sun.COM 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id, uio,
7539412SAleksandr.Guzovskiy@Sun.COM 			    nbytes, tx);
7549412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes -= uio->uio_resid;
7559412SAleksandr.Guzovskiy@Sun.COM 		} else {
7569412SAleksandr.Guzovskiy@Sun.COM 			tx_bytes = nbytes;
7579412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(tx_bytes == max_blksz);
7589412SAleksandr.Guzovskiy@Sun.COM 			dmu_assign_arcbuf(zp->z_dbuf, woff, abuf, tx);
7599412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(tx_bytes <= uio->uio_resid);
7609412SAleksandr.Guzovskiy@Sun.COM 			uioskip(uio, tx_bytes);
7619412SAleksandr.Guzovskiy@Sun.COM 		}
7629412SAleksandr.Guzovskiy@Sun.COM 		if (tx_bytes && vn_has_cached_data(vp)) {
7638636SMark.Maybee@Sun.COM 			update_pages(vp, woff,
7648636SMark.Maybee@Sun.COM 			    tx_bytes, zfsvfs->z_os, zp->z_id);
7659412SAleksandr.Guzovskiy@Sun.COM 		}
7663638Sbillm 
7673638Sbillm 		/*
7683638Sbillm 		 * If we made no progress, we're done.  If we made even
7693638Sbillm 		 * partial progress, update the znode and ZIL accordingly.
7703638Sbillm 		 */
7713638Sbillm 		if (tx_bytes == 0) {
7723897Smaybee 			dmu_tx_commit(tx);
7733638Sbillm 			ASSERT(error != 0);
7743638Sbillm 			break;
7753638Sbillm 		}
7763638Sbillm 
777789Sahrens 		/*
7783638Sbillm 		 * Clear Set-UID/Set-GID bits on successful write if not
7793638Sbillm 		 * privileged and at least one of the excute bits is set.
7803638Sbillm 		 *
7813638Sbillm 		 * It would be nice to to this after all writes have
7823638Sbillm 		 * been done, but that would still expose the ISUID/ISGID
7833638Sbillm 		 * to another app after the partial write is committed.
7845331Samw 		 *
7855331Samw 		 * Note: we don't call zfs_fuid_map_id() here because
7865331Samw 		 * user 0 is not an ephemeral uid.
787789Sahrens 		 */
7883638Sbillm 		mutex_enter(&zp->z_acl_lock);
7893638Sbillm 		if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) |
7903638Sbillm 		    (S_IXUSR >> 6))) != 0 &&
7913638Sbillm 		    (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 &&
7923638Sbillm 		    secpolicy_vnode_setid_retain(cr,
7933638Sbillm 		    (zp->z_phys->zp_mode & S_ISUID) != 0 &&
7943638Sbillm 		    zp->z_phys->zp_uid == 0) != 0) {
7954339Sperrin 			zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID);
7963638Sbillm 		}
7973638Sbillm 		mutex_exit(&zp->z_acl_lock);
7983638Sbillm 
7993638Sbillm 		/*
8003638Sbillm 		 * Update time stamp.  NOTE: This marks the bonus buffer as
8013638Sbillm 		 * dirty, so we don't have to do it again for zp_size.
8023638Sbillm 		 */
8033638Sbillm 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
8043638Sbillm 
8053638Sbillm 		/*
8063638Sbillm 		 * Update the file size (zp_size) if it has changed;
8073638Sbillm 		 * account for possible concurrent updates.
8083638Sbillm 		 */
8093638Sbillm 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset)
810789Sahrens 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
811789Sahrens 			    uio->uio_loffset);
8123638Sbillm 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
8133638Sbillm 		dmu_tx_commit(tx);
8143638Sbillm 
8153638Sbillm 		if (error != 0)
8163638Sbillm 			break;
8173638Sbillm 		ASSERT(tx_bytes == nbytes);
8183638Sbillm 		n -= nbytes;
819789Sahrens 	}
820789Sahrens 
8212237Smaybee 	zfs_range_unlock(rl);
822789Sahrens 
823789Sahrens 	/*
824789Sahrens 	 * If we're in replay mode, or we made no progress, return error.
825789Sahrens 	 * Otherwise, it's at least a partial write, so it's successful.
826789Sahrens 	 */
8278227SNeil.Perrin@Sun.COM 	if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
828789Sahrens 		ZFS_EXIT(zfsvfs);
829789Sahrens 		return (error);
830789Sahrens 	}
831789Sahrens 
8322638Sperrin 	if (ioflag & (FSYNC | FDSYNC))
8332638Sperrin 		zil_commit(zilog, zp->z_last_itx, zp->z_id);
834789Sahrens 
835789Sahrens 	ZFS_EXIT(zfsvfs);
836789Sahrens 	return (0);
837789Sahrens }
838789Sahrens 
8392237Smaybee void
8403063Sperrin zfs_get_done(dmu_buf_t *db, void *vzgd)
8412237Smaybee {
8423063Sperrin 	zgd_t *zgd = (zgd_t *)vzgd;
8433063Sperrin 	rl_t *rl = zgd->zgd_rl;
8442237Smaybee 	vnode_t *vp = ZTOV(rl->r_zp);
8459321SNeil.Perrin@Sun.COM 	objset_t *os = rl->r_zp->z_zfsvfs->z_os;
8462237Smaybee 
8473063Sperrin 	dmu_buf_rele(db, vzgd);
8482237Smaybee 	zfs_range_unlock(rl);
8499321SNeil.Perrin@Sun.COM 	/*
8509321SNeil.Perrin@Sun.COM 	 * Release the vnode asynchronously as we currently have the
8519321SNeil.Perrin@Sun.COM 	 * txg stopped from syncing.
8529321SNeil.Perrin@Sun.COM 	 */
8539321SNeil.Perrin@Sun.COM 	VN_RELE_ASYNC(vp, dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
8545688Sbonwick 	zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
8553063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
8562237Smaybee }
8572237Smaybee 
858789Sahrens /*
859789Sahrens  * Get data to generate a TX_WRITE intent log record.
860789Sahrens  */
861789Sahrens int
8622237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
863789Sahrens {
864789Sahrens 	zfsvfs_t *zfsvfs = arg;
865789Sahrens 	objset_t *os = zfsvfs->z_os;
866789Sahrens 	znode_t *zp;
867789Sahrens 	uint64_t off = lr->lr_offset;
8682237Smaybee 	dmu_buf_t *db;
8691669Sperrin 	rl_t *rl;
8703063Sperrin 	zgd_t *zgd;
8713638Sbillm 	int dlen = lr->lr_length;		/* length of user data */
872789Sahrens 	int error = 0;
873789Sahrens 
8743063Sperrin 	ASSERT(zio);
875789Sahrens 	ASSERT(dlen != 0);
876789Sahrens 
877789Sahrens 	/*
8781669Sperrin 	 * Nothing to do if the file has been removed
879789Sahrens 	 */
880789Sahrens 	if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0)
881789Sahrens 		return (ENOENT);
8823461Sahrens 	if (zp->z_unlinked) {
8839321SNeil.Perrin@Sun.COM 		/*
8849321SNeil.Perrin@Sun.COM 		 * Release the vnode asynchronously as we currently have the
8859321SNeil.Perrin@Sun.COM 		 * txg stopped from syncing.
8869321SNeil.Perrin@Sun.COM 		 */
8879321SNeil.Perrin@Sun.COM 		VN_RELE_ASYNC(ZTOV(zp),
8889321SNeil.Perrin@Sun.COM 		    dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
889789Sahrens 		return (ENOENT);
890789Sahrens 	}
891789Sahrens 
892789Sahrens 	/*
893789Sahrens 	 * Write records come in two flavors: immediate and indirect.
894789Sahrens 	 * For small writes it's cheaper to store the data with the
895789Sahrens 	 * log record (immediate); for large writes it's cheaper to
896789Sahrens 	 * sync the data and get a pointer to it (indirect) so that
897789Sahrens 	 * we don't have to write the data twice.
898789Sahrens 	 */
8991669Sperrin 	if (buf != NULL) { /* immediate write */
9001669Sperrin 		rl = zfs_range_lock(zp, off, dlen, RL_READER);
9011669Sperrin 		/* test for truncation needs to be done while range locked */
9021669Sperrin 		if (off >= zp->z_phys->zp_size) {
9031669Sperrin 			error = ENOENT;
9041669Sperrin 			goto out;
9051669Sperrin 		}
9069512SNeil.Perrin@Sun.COM 		VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf,
9079512SNeil.Perrin@Sun.COM 		    DMU_READ_NO_PREFETCH));
9081669Sperrin 	} else { /* indirect write */
9091669Sperrin 		uint64_t boff; /* block starting offset */
9101669Sperrin 
911789Sahrens 		/*
9121669Sperrin 		 * Have to lock the whole block to ensure when it's
9131669Sperrin 		 * written out and it's checksum is being calculated
9141669Sperrin 		 * that no one can change the data. We need to re-check
9151669Sperrin 		 * blocksize after we get the lock in case it's changed!
916789Sahrens 		 */
9171669Sperrin 		for (;;) {
9181941Sperrin 			if (ISP2(zp->z_blksz)) {
9191941Sperrin 				boff = P2ALIGN_TYPED(off, zp->z_blksz,
9201941Sperrin 				    uint64_t);
9211941Sperrin 			} else {
9221941Sperrin 				boff = 0;
9231941Sperrin 			}
9241669Sperrin 			dlen = zp->z_blksz;
9251669Sperrin 			rl = zfs_range_lock(zp, boff, dlen, RL_READER);
9261669Sperrin 			if (zp->z_blksz == dlen)
9271669Sperrin 				break;
9282237Smaybee 			zfs_range_unlock(rl);
9291669Sperrin 		}
9301669Sperrin 		/* test for truncation needs to be done while range locked */
9311669Sperrin 		if (off >= zp->z_phys->zp_size) {
9321669Sperrin 			error = ENOENT;
9331669Sperrin 			goto out;
9341669Sperrin 		}
9353063Sperrin 		zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
9363063Sperrin 		zgd->zgd_rl = rl;
9373063Sperrin 		zgd->zgd_zilog = zfsvfs->z_log;
9383063Sperrin 		zgd->zgd_bp = &lr->lr_blkptr;
9393063Sperrin 		VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db));
9402237Smaybee 		ASSERT(boff == db->db_offset);
9412237Smaybee 		lr->lr_blkoff = off - boff;
9422237Smaybee 		error = dmu_sync(zio, db, &lr->lr_blkptr,
9433063Sperrin 		    lr->lr_common.lrc_txg, zfs_get_done, zgd);
9444709Smaybee 		ASSERT((error && error != EINPROGRESS) ||
9454709Smaybee 		    lr->lr_length <= zp->z_blksz);
9465688Sbonwick 		if (error == 0)
9475688Sbonwick 			zil_add_block(zfsvfs->z_log, &lr->lr_blkptr);
9482237Smaybee 		/*
9492237Smaybee 		 * If we get EINPROGRESS, then we need to wait for a
9502237Smaybee 		 * write IO initiated by dmu_sync() to complete before
9512638Sperrin 		 * we can release this dbuf.  We will finish everything
9522237Smaybee 		 * up in the zfs_get_done() callback.
9532237Smaybee 		 */
9542237Smaybee 		if (error == EINPROGRESS)
9552237Smaybee 			return (0);
9563063Sperrin 		dmu_buf_rele(db, zgd);
9573063Sperrin 		kmem_free(zgd, sizeof (zgd_t));
958789Sahrens 	}
9591669Sperrin out:
9602237Smaybee 	zfs_range_unlock(rl);
9619321SNeil.Perrin@Sun.COM 	/*
9629321SNeil.Perrin@Sun.COM 	 * Release the vnode asynchronously as we currently have the
9639321SNeil.Perrin@Sun.COM 	 * txg stopped from syncing.
9649321SNeil.Perrin@Sun.COM 	 */
9659321SNeil.Perrin@Sun.COM 	VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
966789Sahrens 	return (error);
967789Sahrens }
968789Sahrens 
969789Sahrens /*ARGSUSED*/
970789Sahrens static int
9715331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
9725331Samw     caller_context_t *ct)
973789Sahrens {
974789Sahrens 	znode_t *zp = VTOZ(vp);
975789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
976789Sahrens 	int error;
977789Sahrens 
9785367Sahrens 	ZFS_ENTER(zfsvfs);
9795367Sahrens 	ZFS_VERIFY_ZP(zp);
9805331Samw 
9815331Samw 	if (flag & V_ACE_MASK)
9825331Samw 		error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
9835331Samw 	else
9845331Samw 		error = zfs_zaccess_rwx(zp, mode, flag, cr);
9855331Samw 
986789Sahrens 	ZFS_EXIT(zfsvfs);
987789Sahrens 	return (error);
988789Sahrens }
989789Sahrens 
990789Sahrens /*
991789Sahrens  * Lookup an entry in a directory, or an extended attribute directory.
992789Sahrens  * If it exists, return a held vnode reference for it.
993789Sahrens  *
994789Sahrens  *	IN:	dvp	- vnode of directory to search.
995789Sahrens  *		nm	- name of entry to lookup.
996789Sahrens  *		pnp	- full pathname to lookup [UNUSED].
997789Sahrens  *		flags	- LOOKUP_XATTR set if looking for an attribute.
998789Sahrens  *		rdir	- root directory vnode [UNUSED].
999789Sahrens  *		cr	- credentials of caller.
10005331Samw  *		ct	- caller context
10015331Samw  *		direntflags - directory lookup flags
10025331Samw  *		realpnp - returned pathname.
1003789Sahrens  *
1004789Sahrens  *	OUT:	vpp	- vnode of located entry, NULL if not found.
1005789Sahrens  *
1006789Sahrens  *	RETURN:	0 if success
1007789Sahrens  *		error code if failure
1008789Sahrens  *
1009789Sahrens  * Timestamps:
1010789Sahrens  *	NA
1011789Sahrens  */
1012789Sahrens /* ARGSUSED */
1013789Sahrens static int
1014789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
10155331Samw     int flags, vnode_t *rdir, cred_t *cr,  caller_context_t *ct,
10165331Samw     int *direntflags, pathname_t *realpnp)
1017789Sahrens {
1018789Sahrens 	znode_t *zdp = VTOZ(dvp);
1019789Sahrens 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1020789Sahrens 	int	error;
1021789Sahrens 
10225367Sahrens 	ZFS_ENTER(zfsvfs);
10235367Sahrens 	ZFS_VERIFY_ZP(zdp);
1024789Sahrens 
1025789Sahrens 	*vpp = NULL;
1026789Sahrens 
1027789Sahrens 	if (flags & LOOKUP_XATTR) {
1028789Sahrens 		/*
10293234Sck153898 		 * If the xattr property is off, refuse the lookup request.
10303234Sck153898 		 */
10313234Sck153898 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
10323234Sck153898 			ZFS_EXIT(zfsvfs);
10333234Sck153898 			return (EINVAL);
10343234Sck153898 		}
10353234Sck153898 
10363234Sck153898 		/*
1037789Sahrens 		 * We don't allow recursive attributes..
1038789Sahrens 		 * Maybe someday we will.
1039789Sahrens 		 */
1040789Sahrens 		if (zdp->z_phys->zp_flags & ZFS_XATTR) {
1041789Sahrens 			ZFS_EXIT(zfsvfs);
1042789Sahrens 			return (EINVAL);
1043789Sahrens 		}
1044789Sahrens 
10453280Sck153898 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1046789Sahrens 			ZFS_EXIT(zfsvfs);
1047789Sahrens 			return (error);
1048789Sahrens 		}
1049789Sahrens 
1050789Sahrens 		/*
1051789Sahrens 		 * Do we have permission to get into attribute directory?
1052789Sahrens 		 */
1053789Sahrens 
10545331Samw 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
10555331Samw 		    B_FALSE, cr)) {
1056789Sahrens 			VN_RELE(*vpp);
10575331Samw 			*vpp = NULL;
1058789Sahrens 		}
1059789Sahrens 
1060789Sahrens 		ZFS_EXIT(zfsvfs);
1061789Sahrens 		return (error);
1062789Sahrens 	}
1063789Sahrens 
10641512Sek110237 	if (dvp->v_type != VDIR) {
10651512Sek110237 		ZFS_EXIT(zfsvfs);
10661460Smarks 		return (ENOTDIR);
10671512Sek110237 	}
10681460Smarks 
1069789Sahrens 	/*
1070789Sahrens 	 * Check accessibility of directory.
1071789Sahrens 	 */
1072789Sahrens 
10735331Samw 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1074789Sahrens 		ZFS_EXIT(zfsvfs);
1075789Sahrens 		return (error);
1076789Sahrens 	}
1077789Sahrens 
10785498Stimh 	if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
10795331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
10805331Samw 		ZFS_EXIT(zfsvfs);
10815331Samw 		return (EILSEQ);
10825331Samw 	}
10835331Samw 
10845331Samw 	error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
10855331Samw 	if (error == 0) {
1086789Sahrens 		/*
1087789Sahrens 		 * Convert device special files
1088789Sahrens 		 */
1089789Sahrens 		if (IS_DEVVP(*vpp)) {
1090789Sahrens 			vnode_t	*svp;
1091789Sahrens 
1092789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1093789Sahrens 			VN_RELE(*vpp);
1094789Sahrens 			if (svp == NULL)
1095789Sahrens 				error = ENOSYS;
1096789Sahrens 			else
1097789Sahrens 				*vpp = svp;
1098789Sahrens 		}
1099789Sahrens 	}
1100789Sahrens 
1101789Sahrens 	ZFS_EXIT(zfsvfs);
1102789Sahrens 	return (error);
1103789Sahrens }
1104789Sahrens 
1105789Sahrens /*
1106789Sahrens  * Attempt to create a new entry in a directory.  If the entry
1107789Sahrens  * already exists, truncate the file if permissible, else return
1108789Sahrens  * an error.  Return the vp of the created or trunc'd file.
1109789Sahrens  *
1110789Sahrens  *	IN:	dvp	- vnode of directory to put new file entry in.
1111789Sahrens  *		name	- name of new file entry.
1112789Sahrens  *		vap	- attributes of new file.
1113789Sahrens  *		excl	- flag indicating exclusive or non-exclusive mode.
1114789Sahrens  *		mode	- mode to open file with.
1115789Sahrens  *		cr	- credentials of caller.
1116789Sahrens  *		flag	- large file flag [UNUSED].
11175331Samw  *		ct	- caller context
11185331Samw  *		vsecp 	- ACL to be set
1119789Sahrens  *
1120789Sahrens  *	OUT:	vpp	- vnode of created or trunc'd entry.
1121789Sahrens  *
1122789Sahrens  *	RETURN:	0 if success
1123789Sahrens  *		error code if failure
1124789Sahrens  *
1125789Sahrens  * Timestamps:
1126789Sahrens  *	dvp - ctime|mtime updated if new entry created
1127789Sahrens  *	 vp - ctime|mtime always, atime if new
1128789Sahrens  */
11295331Samw 
1130789Sahrens /* ARGSUSED */
1131789Sahrens static int
1132789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
11335331Samw     int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
11345331Samw     vsecattr_t *vsecp)
1135789Sahrens {
1136789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1137789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
11385326Sek110237 	zilog_t		*zilog;
11395326Sek110237 	objset_t	*os;
1140789Sahrens 	zfs_dirlock_t	*dl;
1141789Sahrens 	dmu_tx_t	*tx;
1142789Sahrens 	int		error;
11437847SMark.Shellenbaum@Sun.COM 	ksid_t		*ksid;
11447847SMark.Shellenbaum@Sun.COM 	uid_t		uid;
11457847SMark.Shellenbaum@Sun.COM 	gid_t		gid = crgetgid(cr);
11469179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t	acl_ids;
11479179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
11485331Samw 
11495331Samw 	/*
11505331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
11515331Samw 	 * make sure file system is at proper version
11525331Samw 	 */
11535331Samw 
11547847SMark.Shellenbaum@Sun.COM 	ksid = crgetsid(cr, KSID_OWNER);
11557847SMark.Shellenbaum@Sun.COM 	if (ksid)
11567847SMark.Shellenbaum@Sun.COM 		uid = ksid_getid(ksid);
11577847SMark.Shellenbaum@Sun.COM 	else
11587847SMark.Shellenbaum@Sun.COM 		uid = crgetuid(cr);
11597847SMark.Shellenbaum@Sun.COM 
11605331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
11615331Samw 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
11627847SMark.Shellenbaum@Sun.COM 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
11635331Samw 		return (EINVAL);
1164789Sahrens 
11655367Sahrens 	ZFS_ENTER(zfsvfs);
11665367Sahrens 	ZFS_VERIFY_ZP(dzp);
11675326Sek110237 	os = zfsvfs->z_os;
11685326Sek110237 	zilog = zfsvfs->z_log;
1169789Sahrens 
11705498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
11715331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
11725331Samw 		ZFS_EXIT(zfsvfs);
11735331Samw 		return (EILSEQ);
11745331Samw 	}
11755331Samw 
11765331Samw 	if (vap->va_mask & AT_XVATTR) {
11775331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
11785331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
11795331Samw 			ZFS_EXIT(zfsvfs);
11805331Samw 			return (error);
11815331Samw 		}
11825331Samw 	}
1183789Sahrens top:
1184789Sahrens 	*vpp = NULL;
1185789Sahrens 
1186789Sahrens 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1187789Sahrens 		vap->va_mode &= ~VSVTX;
1188789Sahrens 
1189789Sahrens 	if (*name == '\0') {
1190789Sahrens 		/*
1191789Sahrens 		 * Null component name refers to the directory itself.
1192789Sahrens 		 */
1193789Sahrens 		VN_HOLD(dvp);
1194789Sahrens 		zp = dzp;
1195789Sahrens 		dl = NULL;
1196789Sahrens 		error = 0;
1197789Sahrens 	} else {
1198789Sahrens 		/* possible VN_HOLD(zp) */
11995331Samw 		int zflg = 0;
12005331Samw 
12015331Samw 		if (flag & FIGNORECASE)
12025331Samw 			zflg |= ZCILOOK;
12035331Samw 
12045331Samw 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
12055331Samw 		    NULL, NULL);
12065331Samw 		if (error) {
1207789Sahrens 			if (strcmp(name, "..") == 0)
1208789Sahrens 				error = EISDIR;
1209789Sahrens 			ZFS_EXIT(zfsvfs);
1210789Sahrens 			return (error);
1211789Sahrens 		}
1212789Sahrens 	}
1213789Sahrens 	if (zp == NULL) {
12145331Samw 		uint64_t txtype;
12155331Samw 
1216789Sahrens 		/*
1217789Sahrens 		 * Create a new file object and update the directory
1218789Sahrens 		 * to reference it.
1219789Sahrens 		 */
12205331Samw 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1221789Sahrens 			goto out;
1222789Sahrens 		}
1223789Sahrens 
1224789Sahrens 		/*
1225789Sahrens 		 * We only support the creation of regular files in
1226789Sahrens 		 * extended attribute directories.
1227789Sahrens 		 */
1228789Sahrens 		if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1229789Sahrens 		    (vap->va_type != VREG)) {
1230789Sahrens 			error = EINVAL;
1231789Sahrens 			goto out;
1232789Sahrens 		}
1233789Sahrens 
12349179SMark.Shellenbaum@Sun.COM 		if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
12359179SMark.Shellenbaum@Sun.COM 		    &acl_ids)) != 0)
12369179SMark.Shellenbaum@Sun.COM 			goto out;
12379396SMatthew.Ahrens@Sun.COM 		if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
12389396SMatthew.Ahrens@Sun.COM 			error = EDQUOT;
12399396SMatthew.Ahrens@Sun.COM 			goto out;
12409396SMatthew.Ahrens@Sun.COM 		}
12419179SMark.Shellenbaum@Sun.COM 
1242789Sahrens 		tx = dmu_tx_create(os);
1243789Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
12449179SMark.Shellenbaum@Sun.COM 		fuid_dirtied = zfsvfs->z_fuid_dirty;
12459396SMatthew.Ahrens@Sun.COM 		if (fuid_dirtied)
12469396SMatthew.Ahrens@Sun.COM 			zfs_fuid_txhold(zfsvfs, tx);
1247789Sahrens 		dmu_tx_hold_bonus(tx, dzp->z_id);
12481544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
12499179SMark.Shellenbaum@Sun.COM 		if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1250789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1251789Sahrens 			    0, SPA_MAXBLOCKSIZE);
12525331Samw 		}
12538227SNeil.Perrin@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
1254789Sahrens 		if (error) {
12559179SMark.Shellenbaum@Sun.COM 			zfs_acl_ids_free(&acl_ids);
1256789Sahrens 			zfs_dirent_unlock(dl);
12578227SNeil.Perrin@Sun.COM 			if (error == ERESTART) {
12582113Sahrens 				dmu_tx_wait(tx);
12592113Sahrens 				dmu_tx_abort(tx);
1260789Sahrens 				goto top;
1261789Sahrens 			}
12622113Sahrens 			dmu_tx_abort(tx);
1263789Sahrens 			ZFS_EXIT(zfsvfs);
1264789Sahrens 			return (error);
1265789Sahrens 		}
12669179SMark.Shellenbaum@Sun.COM 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids);
12679179SMark.Shellenbaum@Sun.COM 
12689179SMark.Shellenbaum@Sun.COM 		if (fuid_dirtied)
12699179SMark.Shellenbaum@Sun.COM 			zfs_fuid_sync(zfsvfs, tx);
12709179SMark.Shellenbaum@Sun.COM 
1271789Sahrens 		(void) zfs_link_create(dl, zp, tx, ZNEW);
12729179SMark.Shellenbaum@Sun.COM 
12735331Samw 		txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
12745331Samw 		if (flag & FIGNORECASE)
12755331Samw 			txtype |= TX_CI;
12765331Samw 		zfs_log_create(zilog, tx, txtype, dzp, zp, name,
12779179SMark.Shellenbaum@Sun.COM 		    vsecp, acl_ids.z_fuidp, vap);
12789179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
1279789Sahrens 		dmu_tx_commit(tx);
1280789Sahrens 	} else {
12815331Samw 		int aflags = (flag & FAPPEND) ? V_APPEND : 0;
12825331Samw 
1283789Sahrens 		/*
1284789Sahrens 		 * A directory entry already exists for this name.
1285789Sahrens 		 */
1286789Sahrens 		/*
1287789Sahrens 		 * Can't truncate an existing file if in exclusive mode.
1288789Sahrens 		 */
1289789Sahrens 		if (excl == EXCL) {
1290789Sahrens 			error = EEXIST;
1291789Sahrens 			goto out;
1292789Sahrens 		}
1293789Sahrens 		/*
1294789Sahrens 		 * Can't open a directory for writing.
1295789Sahrens 		 */
1296789Sahrens 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1297789Sahrens 			error = EISDIR;
1298789Sahrens 			goto out;
1299789Sahrens 		}
1300789Sahrens 		/*
1301789Sahrens 		 * Verify requested access to file.
1302789Sahrens 		 */
13035331Samw 		if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1304789Sahrens 			goto out;
1305789Sahrens 		}
1306789Sahrens 
1307789Sahrens 		mutex_enter(&dzp->z_lock);
1308789Sahrens 		dzp->z_seq++;
1309789Sahrens 		mutex_exit(&dzp->z_lock);
1310789Sahrens 
13111878Smaybee 		/*
13121878Smaybee 		 * Truncate regular files if requested.
13131878Smaybee 		 */
13141878Smaybee 		if ((ZTOV(zp)->v_type == VREG) &&
1315789Sahrens 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
13166992Smaybee 			/* we can't hold any locks when calling zfs_freesp() */
13176992Smaybee 			zfs_dirent_unlock(dl);
13186992Smaybee 			dl = NULL;
13191878Smaybee 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
13204863Spraks 			if (error == 0) {
13215331Samw 				vnevent_create(ZTOV(zp), ct);
13224863Spraks 			}
1323789Sahrens 		}
1324789Sahrens 	}
1325789Sahrens out:
1326789Sahrens 
1327789Sahrens 	if (dl)
1328789Sahrens 		zfs_dirent_unlock(dl);
1329789Sahrens 
1330789Sahrens 	if (error) {
1331789Sahrens 		if (zp)
1332789Sahrens 			VN_RELE(ZTOV(zp));
1333789Sahrens 	} else {
1334789Sahrens 		*vpp = ZTOV(zp);
1335789Sahrens 		/*
1336789Sahrens 		 * If vnode is for a device return a specfs vnode instead.
1337789Sahrens 		 */
1338789Sahrens 		if (IS_DEVVP(*vpp)) {
1339789Sahrens 			struct vnode *svp;
1340789Sahrens 
1341789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1342789Sahrens 			VN_RELE(*vpp);
1343789Sahrens 			if (svp == NULL) {
1344789Sahrens 				error = ENOSYS;
1345789Sahrens 			}
1346789Sahrens 			*vpp = svp;
1347789Sahrens 		}
1348789Sahrens 	}
1349789Sahrens 
1350789Sahrens 	ZFS_EXIT(zfsvfs);
1351789Sahrens 	return (error);
1352789Sahrens }
1353789Sahrens 
1354789Sahrens /*
1355789Sahrens  * Remove an entry from a directory.
1356789Sahrens  *
1357789Sahrens  *	IN:	dvp	- vnode of directory to remove entry from.
1358789Sahrens  *		name	- name of entry to remove.
1359789Sahrens  *		cr	- credentials of caller.
13605331Samw  *		ct	- caller context
13615331Samw  *		flags	- case flags
1362789Sahrens  *
1363789Sahrens  *	RETURN:	0 if success
1364789Sahrens  *		error code if failure
1365789Sahrens  *
1366789Sahrens  * Timestamps:
1367789Sahrens  *	dvp - ctime|mtime
1368789Sahrens  *	 vp - ctime (if nlink > 0)
1369789Sahrens  */
13705331Samw /*ARGSUSED*/
1371789Sahrens static int
13725331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
13735331Samw     int flags)
1374789Sahrens {
1375789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1376789Sahrens 	znode_t		*xzp = NULL;
1377789Sahrens 	vnode_t		*vp;
1378789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
13795326Sek110237 	zilog_t		*zilog;
1380789Sahrens 	uint64_t	acl_obj, xattr_obj;
1381789Sahrens 	zfs_dirlock_t	*dl;
1382789Sahrens 	dmu_tx_t	*tx;
13833461Sahrens 	boolean_t	may_delete_now, delete_now = FALSE;
13846992Smaybee 	boolean_t	unlinked, toobig = FALSE;
13855331Samw 	uint64_t	txtype;
13865331Samw 	pathname_t	*realnmp = NULL;
13875331Samw 	pathname_t	realnm;
1388789Sahrens 	int		error;
13895331Samw 	int		zflg = ZEXISTS;
1390789Sahrens 
13915367Sahrens 	ZFS_ENTER(zfsvfs);
13925367Sahrens 	ZFS_VERIFY_ZP(dzp);
13935326Sek110237 	zilog = zfsvfs->z_log;
1394789Sahrens 
13955331Samw 	if (flags & FIGNORECASE) {
13965331Samw 		zflg |= ZCILOOK;
13975331Samw 		pn_alloc(&realnm);
13985331Samw 		realnmp = &realnm;
13995331Samw 	}
14005331Samw 
1401789Sahrens top:
1402789Sahrens 	/*
1403789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1404789Sahrens 	 */
14055331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
14065331Samw 	    NULL, realnmp)) {
14075331Samw 		if (realnmp)
14085331Samw 			pn_free(realnmp);
1409789Sahrens 		ZFS_EXIT(zfsvfs);
1410789Sahrens 		return (error);
1411789Sahrens 	}
1412789Sahrens 
1413789Sahrens 	vp = ZTOV(zp);
1414789Sahrens 
1415789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1416789Sahrens 		goto out;
1417789Sahrens 	}
1418789Sahrens 
1419789Sahrens 	/*
1420789Sahrens 	 * Need to use rmdir for removing directories.
1421789Sahrens 	 */
1422789Sahrens 	if (vp->v_type == VDIR) {
1423789Sahrens 		error = EPERM;
1424789Sahrens 		goto out;
1425789Sahrens 	}
1426789Sahrens 
14275331Samw 	vnevent_remove(vp, dvp, name, ct);
14285331Samw 
14295331Samw 	if (realnmp)
14306492Stimh 		dnlc_remove(dvp, realnmp->pn_buf);
14315331Samw 	else
14325331Samw 		dnlc_remove(dvp, name);
14331484Sek110237 
1434789Sahrens 	mutex_enter(&vp->v_lock);
1435789Sahrens 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1436789Sahrens 	mutex_exit(&vp->v_lock);
1437789Sahrens 
1438789Sahrens 	/*
14393461Sahrens 	 * We may delete the znode now, or we may put it in the unlinked set;
1440789Sahrens 	 * it depends on whether we're the last link, and on whether there are
1441789Sahrens 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1442789Sahrens 	 * allow for either case.
1443789Sahrens 	 */
1444789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
14451544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1446789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
14476992Smaybee 	if (may_delete_now) {
14486992Smaybee 		toobig =
14496992Smaybee 		    zp->z_phys->zp_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
14506992Smaybee 		/* if the file is too big, only hold_free a token amount */
14516992Smaybee 		dmu_tx_hold_free(tx, zp->z_id, 0,
14526992Smaybee 		    (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
14536992Smaybee 	}
1454789Sahrens 
1455789Sahrens 	/* are there any extended attributes? */
1456789Sahrens 	if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1457789Sahrens 		/* XXX - do we need this if we are deleting? */
1458789Sahrens 		dmu_tx_hold_bonus(tx, xattr_obj);
1459789Sahrens 	}
1460789Sahrens 
1461789Sahrens 	/* are there any additional acls */
1462789Sahrens 	if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1463789Sahrens 	    may_delete_now)
1464789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1465789Sahrens 
1466789Sahrens 	/* charge as an update -- would be nice not to charge at all */
14673461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1468789Sahrens 
14698227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1470789Sahrens 	if (error) {
1471789Sahrens 		zfs_dirent_unlock(dl);
1472789Sahrens 		VN_RELE(vp);
14738227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
14742113Sahrens 			dmu_tx_wait(tx);
14752113Sahrens 			dmu_tx_abort(tx);
1476789Sahrens 			goto top;
1477789Sahrens 		}
14785331Samw 		if (realnmp)
14795331Samw 			pn_free(realnmp);
14802113Sahrens 		dmu_tx_abort(tx);
1481789Sahrens 		ZFS_EXIT(zfsvfs);
1482789Sahrens 		return (error);
1483789Sahrens 	}
1484789Sahrens 
1485789Sahrens 	/*
1486789Sahrens 	 * Remove the directory entry.
1487789Sahrens 	 */
14885331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1489789Sahrens 
1490789Sahrens 	if (error) {
1491789Sahrens 		dmu_tx_commit(tx);
1492789Sahrens 		goto out;
1493789Sahrens 	}
1494789Sahrens 
14953461Sahrens 	if (unlinked) {
1496789Sahrens 		mutex_enter(&vp->v_lock);
14976992Smaybee 		delete_now = may_delete_now && !toobig &&
1498789Sahrens 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1499789Sahrens 		    zp->z_phys->zp_xattr == xattr_obj &&
1500789Sahrens 		    zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1501789Sahrens 		mutex_exit(&vp->v_lock);
1502789Sahrens 	}
1503789Sahrens 
1504789Sahrens 	if (delete_now) {
1505789Sahrens 		if (zp->z_phys->zp_xattr) {
1506789Sahrens 			error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1507789Sahrens 			ASSERT3U(error, ==, 0);
1508789Sahrens 			ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1509789Sahrens 			dmu_buf_will_dirty(xzp->z_dbuf, tx);
1510789Sahrens 			mutex_enter(&xzp->z_lock);
15113461Sahrens 			xzp->z_unlinked = 1;
1512789Sahrens 			xzp->z_phys->zp_links = 0;
1513789Sahrens 			mutex_exit(&xzp->z_lock);
15143461Sahrens 			zfs_unlinked_add(xzp, tx);
1515789Sahrens 			zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1516789Sahrens 		}
1517789Sahrens 		mutex_enter(&zp->z_lock);
1518789Sahrens 		mutex_enter(&vp->v_lock);
1519789Sahrens 		vp->v_count--;
1520789Sahrens 		ASSERT3U(vp->v_count, ==, 0);
1521789Sahrens 		mutex_exit(&vp->v_lock);
1522789Sahrens 		mutex_exit(&zp->z_lock);
1523789Sahrens 		zfs_znode_delete(zp, tx);
15243461Sahrens 	} else if (unlinked) {
15253461Sahrens 		zfs_unlinked_add(zp, tx);
1526789Sahrens 	}
1527789Sahrens 
15285331Samw 	txtype = TX_REMOVE;
15295331Samw 	if (flags & FIGNORECASE)
15305331Samw 		txtype |= TX_CI;
15315331Samw 	zfs_log_remove(zilog, tx, txtype, dzp, name);
1532789Sahrens 
1533789Sahrens 	dmu_tx_commit(tx);
1534789Sahrens out:
15355331Samw 	if (realnmp)
15365331Samw 		pn_free(realnmp);
15375331Samw 
1538789Sahrens 	zfs_dirent_unlock(dl);
1539789Sahrens 
1540789Sahrens 	if (!delete_now) {
1541789Sahrens 		VN_RELE(vp);
1542789Sahrens 	} else if (xzp) {
15436992Smaybee 		/* this rele is delayed to prevent nesting transactions */
1544789Sahrens 		VN_RELE(ZTOV(xzp));
1545789Sahrens 	}
1546789Sahrens 
1547789Sahrens 	ZFS_EXIT(zfsvfs);
1548789Sahrens 	return (error);
1549789Sahrens }
1550789Sahrens 
1551789Sahrens /*
1552789Sahrens  * Create a new directory and insert it into dvp using the name
1553789Sahrens  * provided.  Return a pointer to the inserted directory.
1554789Sahrens  *
1555789Sahrens  *	IN:	dvp	- vnode of directory to add subdir to.
1556789Sahrens  *		dirname	- name of new directory.
1557789Sahrens  *		vap	- attributes of new directory.
1558789Sahrens  *		cr	- credentials of caller.
15595331Samw  *		ct	- caller context
15605331Samw  *		vsecp	- ACL to be set
1561789Sahrens  *
1562789Sahrens  *	OUT:	vpp	- vnode of created directory.
1563789Sahrens  *
1564789Sahrens  *	RETURN:	0 if success
1565789Sahrens  *		error code if failure
1566789Sahrens  *
1567789Sahrens  * Timestamps:
1568789Sahrens  *	dvp - ctime|mtime updated
1569789Sahrens  *	 vp - ctime|mtime|atime updated
1570789Sahrens  */
15715331Samw /*ARGSUSED*/
1572789Sahrens static int
15735331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
15745331Samw     caller_context_t *ct, int flags, vsecattr_t *vsecp)
1575789Sahrens {
1576789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1577789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
15785326Sek110237 	zilog_t		*zilog;
1579789Sahrens 	zfs_dirlock_t	*dl;
15805331Samw 	uint64_t	txtype;
1581789Sahrens 	dmu_tx_t	*tx;
1582789Sahrens 	int		error;
15835331Samw 	int		zf = ZNEW;
15847847SMark.Shellenbaum@Sun.COM 	ksid_t		*ksid;
15857847SMark.Shellenbaum@Sun.COM 	uid_t		uid;
15867847SMark.Shellenbaum@Sun.COM 	gid_t		gid = crgetgid(cr);
15879179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t	acl_ids;
15889179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
1589789Sahrens 
1590789Sahrens 	ASSERT(vap->va_type == VDIR);
1591789Sahrens 
15925331Samw 	/*
15935331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
15945331Samw 	 * make sure file system is at proper version
15955331Samw 	 */
15965331Samw 
15977847SMark.Shellenbaum@Sun.COM 	ksid = crgetsid(cr, KSID_OWNER);
15987847SMark.Shellenbaum@Sun.COM 	if (ksid)
15997847SMark.Shellenbaum@Sun.COM 		uid = ksid_getid(ksid);
16007847SMark.Shellenbaum@Sun.COM 	else
16017847SMark.Shellenbaum@Sun.COM 		uid = crgetuid(cr);
16025331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
16037847SMark.Shellenbaum@Sun.COM 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
16047876SMark.Shellenbaum@Sun.COM 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
16055331Samw 		return (EINVAL);
16065331Samw 
16075367Sahrens 	ZFS_ENTER(zfsvfs);
16085367Sahrens 	ZFS_VERIFY_ZP(dzp);
16095326Sek110237 	zilog = zfsvfs->z_log;
1610789Sahrens 
1611789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1612789Sahrens 		ZFS_EXIT(zfsvfs);
1613789Sahrens 		return (EINVAL);
1614789Sahrens 	}
16155331Samw 
16165498Stimh 	if (zfsvfs->z_utf8 && u8_validate(dirname,
16175331Samw 	    strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
16185331Samw 		ZFS_EXIT(zfsvfs);
16195331Samw 		return (EILSEQ);
16205331Samw 	}
16215331Samw 	if (flags & FIGNORECASE)
16225331Samw 		zf |= ZCILOOK;
16235331Samw 
16245331Samw 	if (vap->va_mask & AT_XVATTR)
16255331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
16265331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
16275331Samw 			ZFS_EXIT(zfsvfs);
16285331Samw 			return (error);
16295331Samw 		}
1630789Sahrens 
1631789Sahrens 	/*
1632789Sahrens 	 * First make sure the new directory doesn't exist.
1633789Sahrens 	 */
16345331Samw top:
16355331Samw 	*vpp = NULL;
16365331Samw 
16375331Samw 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
16385331Samw 	    NULL, NULL)) {
1639789Sahrens 		ZFS_EXIT(zfsvfs);
1640789Sahrens 		return (error);
1641789Sahrens 	}
1642789Sahrens 
16435331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
16441231Smarks 		zfs_dirent_unlock(dl);
16451231Smarks 		ZFS_EXIT(zfsvfs);
16461231Smarks 		return (error);
16471231Smarks 	}
16481231Smarks 
16499179SMark.Shellenbaum@Sun.COM 	if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
16509179SMark.Shellenbaum@Sun.COM 	    &acl_ids)) != 0) {
16519179SMark.Shellenbaum@Sun.COM 		zfs_dirent_unlock(dl);
16529179SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
16539179SMark.Shellenbaum@Sun.COM 		return (error);
16545331Samw 	}
16559396SMatthew.Ahrens@Sun.COM 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
16569396SMatthew.Ahrens@Sun.COM 		zfs_dirent_unlock(dl);
16579396SMatthew.Ahrens@Sun.COM 		ZFS_EXIT(zfsvfs);
16589396SMatthew.Ahrens@Sun.COM 		return (EDQUOT);
16599396SMatthew.Ahrens@Sun.COM 	}
16609179SMark.Shellenbaum@Sun.COM 
1661789Sahrens 	/*
1662789Sahrens 	 * Add a new entry to the directory.
1663789Sahrens 	 */
1664789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
16651544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
16661544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
16679179SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
16689396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
16699396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
16709179SMark.Shellenbaum@Sun.COM 	if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE)
1671789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1672789Sahrens 		    0, SPA_MAXBLOCKSIZE);
16738227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1674789Sahrens 	if (error) {
16759179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
1676789Sahrens 		zfs_dirent_unlock(dl);
16778227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
16782113Sahrens 			dmu_tx_wait(tx);
16792113Sahrens 			dmu_tx_abort(tx);
1680789Sahrens 			goto top;
1681789Sahrens 		}
16822113Sahrens 		dmu_tx_abort(tx);
1683789Sahrens 		ZFS_EXIT(zfsvfs);
1684789Sahrens 		return (error);
1685789Sahrens 	}
1686789Sahrens 
1687789Sahrens 	/*
1688789Sahrens 	 * Create new node.
1689789Sahrens 	 */
16909179SMark.Shellenbaum@Sun.COM 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids);
16919179SMark.Shellenbaum@Sun.COM 
16929179SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
16939179SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
1694789Sahrens 	/*
1695789Sahrens 	 * Now put new name in parent dir.
1696789Sahrens 	 */
1697789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1698789Sahrens 
1699789Sahrens 	*vpp = ZTOV(zp);
1700789Sahrens 
17015331Samw 	txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
17025331Samw 	if (flags & FIGNORECASE)
17035331Samw 		txtype |= TX_CI;
17049179SMark.Shellenbaum@Sun.COM 	zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
17059179SMark.Shellenbaum@Sun.COM 	    acl_ids.z_fuidp, vap);
17069179SMark.Shellenbaum@Sun.COM 
17079179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_free(&acl_ids);
1708789Sahrens 	dmu_tx_commit(tx);
1709789Sahrens 
1710789Sahrens 	zfs_dirent_unlock(dl);
1711789Sahrens 
1712789Sahrens 	ZFS_EXIT(zfsvfs);
1713789Sahrens 	return (0);
1714789Sahrens }
1715789Sahrens 
1716789Sahrens /*
1717789Sahrens  * Remove a directory subdir entry.  If the current working
1718789Sahrens  * directory is the same as the subdir to be removed, the
1719789Sahrens  * remove will fail.
1720789Sahrens  *
1721789Sahrens  *	IN:	dvp	- vnode of directory to remove from.
1722789Sahrens  *		name	- name of directory to be removed.
1723789Sahrens  *		cwd	- vnode of current working directory.
1724789Sahrens  *		cr	- credentials of caller.
17255331Samw  *		ct	- caller context
17265331Samw  *		flags	- case flags
1727789Sahrens  *
1728789Sahrens  *	RETURN:	0 if success
1729789Sahrens  *		error code if failure
1730789Sahrens  *
1731789Sahrens  * Timestamps:
1732789Sahrens  *	dvp - ctime|mtime updated
1733789Sahrens  */
17345331Samw /*ARGSUSED*/
1735789Sahrens static int
17365331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
17375331Samw     caller_context_t *ct, int flags)
1738789Sahrens {
1739789Sahrens 	znode_t		*dzp = VTOZ(dvp);
1740789Sahrens 	znode_t		*zp;
1741789Sahrens 	vnode_t		*vp;
1742789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
17435326Sek110237 	zilog_t		*zilog;
1744789Sahrens 	zfs_dirlock_t	*dl;
1745789Sahrens 	dmu_tx_t	*tx;
1746789Sahrens 	int		error;
17475331Samw 	int		zflg = ZEXISTS;
1748789Sahrens 
17495367Sahrens 	ZFS_ENTER(zfsvfs);
17505367Sahrens 	ZFS_VERIFY_ZP(dzp);
17515326Sek110237 	zilog = zfsvfs->z_log;
1752789Sahrens 
17535331Samw 	if (flags & FIGNORECASE)
17545331Samw 		zflg |= ZCILOOK;
1755789Sahrens top:
1756789Sahrens 	zp = NULL;
1757789Sahrens 
1758789Sahrens 	/*
1759789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1760789Sahrens 	 */
17615331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
17625331Samw 	    NULL, NULL)) {
1763789Sahrens 		ZFS_EXIT(zfsvfs);
1764789Sahrens 		return (error);
1765789Sahrens 	}
1766789Sahrens 
1767789Sahrens 	vp = ZTOV(zp);
1768789Sahrens 
1769789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1770789Sahrens 		goto out;
1771789Sahrens 	}
1772789Sahrens 
1773789Sahrens 	if (vp->v_type != VDIR) {
1774789Sahrens 		error = ENOTDIR;
1775789Sahrens 		goto out;
1776789Sahrens 	}
1777789Sahrens 
1778789Sahrens 	if (vp == cwd) {
1779789Sahrens 		error = EINVAL;
1780789Sahrens 		goto out;
1781789Sahrens 	}
1782789Sahrens 
17835331Samw 	vnevent_rmdir(vp, dvp, name, ct);
1784789Sahrens 
1785789Sahrens 	/*
17863897Smaybee 	 * Grab a lock on the directory to make sure that noone is
17873897Smaybee 	 * trying to add (or lookup) entries while we are removing it.
17883897Smaybee 	 */
17893897Smaybee 	rw_enter(&zp->z_name_lock, RW_WRITER);
17903897Smaybee 
17913897Smaybee 	/*
17923897Smaybee 	 * Grab a lock on the parent pointer to make sure we play well
1793789Sahrens 	 * with the treewalk and directory rename code.
1794789Sahrens 	 */
1795789Sahrens 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1796789Sahrens 
1797789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
17981544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1799789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
18003461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
18018227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1802789Sahrens 	if (error) {
1803789Sahrens 		rw_exit(&zp->z_parent_lock);
18043897Smaybee 		rw_exit(&zp->z_name_lock);
1805789Sahrens 		zfs_dirent_unlock(dl);
1806789Sahrens 		VN_RELE(vp);
18078227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
18082113Sahrens 			dmu_tx_wait(tx);
18092113Sahrens 			dmu_tx_abort(tx);
1810789Sahrens 			goto top;
1811789Sahrens 		}
18122113Sahrens 		dmu_tx_abort(tx);
1813789Sahrens 		ZFS_EXIT(zfsvfs);
1814789Sahrens 		return (error);
1815789Sahrens 	}
1816789Sahrens 
18175331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
18185331Samw 
18195331Samw 	if (error == 0) {
18205331Samw 		uint64_t txtype = TX_RMDIR;
18215331Samw 		if (flags & FIGNORECASE)
18225331Samw 			txtype |= TX_CI;
18235331Samw 		zfs_log_remove(zilog, tx, txtype, dzp, name);
18245331Samw 	}
1825789Sahrens 
1826789Sahrens 	dmu_tx_commit(tx);
1827789Sahrens 
1828789Sahrens 	rw_exit(&zp->z_parent_lock);
18293897Smaybee 	rw_exit(&zp->z_name_lock);
1830789Sahrens out:
1831789Sahrens 	zfs_dirent_unlock(dl);
1832789Sahrens 
1833789Sahrens 	VN_RELE(vp);
1834789Sahrens 
1835789Sahrens 	ZFS_EXIT(zfsvfs);
1836789Sahrens 	return (error);
1837789Sahrens }
1838789Sahrens 
1839789Sahrens /*
1840789Sahrens  * Read as many directory entries as will fit into the provided
1841789Sahrens  * buffer from the given directory cursor position (specified in
1842789Sahrens  * the uio structure.
1843789Sahrens  *
1844789Sahrens  *	IN:	vp	- vnode of directory to read.
1845789Sahrens  *		uio	- structure supplying read location, range info,
1846789Sahrens  *			  and return buffer.
1847789Sahrens  *		cr	- credentials of caller.
18485331Samw  *		ct	- caller context
18495331Samw  *		flags	- case flags
1850789Sahrens  *
1851789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
1852789Sahrens  *		eofp	- set to true if end-of-file detected.
1853789Sahrens  *
1854789Sahrens  *	RETURN:	0 if success
1855789Sahrens  *		error code if failure
1856789Sahrens  *
1857789Sahrens  * Timestamps:
1858789Sahrens  *	vp - atime updated
1859789Sahrens  *
1860789Sahrens  * Note that the low 4 bits of the cookie returned by zap is always zero.
1861789Sahrens  * This allows us to use the low range for "special" directory entries:
1862789Sahrens  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
1863789Sahrens  * we use the offset 2 for the '.zfs' directory.
1864789Sahrens  */
1865789Sahrens /* ARGSUSED */
1866789Sahrens static int
18675331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
18685331Samw     caller_context_t *ct, int flags)
1869789Sahrens {
1870789Sahrens 	znode_t		*zp = VTOZ(vp);
1871789Sahrens 	iovec_t		*iovp;
18725331Samw 	edirent_t	*eodp;
1873789Sahrens 	dirent64_t	*odp;
1874789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1875869Sperrin 	objset_t	*os;
1876789Sahrens 	caddr_t		outbuf;
1877789Sahrens 	size_t		bufsize;
1878789Sahrens 	zap_cursor_t	zc;
1879789Sahrens 	zap_attribute_t	zap;
1880789Sahrens 	uint_t		bytes_wanted;
1881789Sahrens 	uint64_t	offset; /* must be unsigned; checks for < 1 */
1882789Sahrens 	int		local_eof;
1883869Sperrin 	int		outcount;
1884869Sperrin 	int		error;
1885869Sperrin 	uint8_t		prefetch;
18865663Sck153898 	boolean_t	check_sysattrs;
1887789Sahrens 
18885367Sahrens 	ZFS_ENTER(zfsvfs);
18895367Sahrens 	ZFS_VERIFY_ZP(zp);
1890789Sahrens 
1891789Sahrens 	/*
1892789Sahrens 	 * If we are not given an eof variable,
1893789Sahrens 	 * use a local one.
1894789Sahrens 	 */
1895789Sahrens 	if (eofp == NULL)
1896789Sahrens 		eofp = &local_eof;
1897789Sahrens 
1898789Sahrens 	/*
1899789Sahrens 	 * Check for valid iov_len.
1900789Sahrens 	 */
1901789Sahrens 	if (uio->uio_iov->iov_len <= 0) {
1902789Sahrens 		ZFS_EXIT(zfsvfs);
1903789Sahrens 		return (EINVAL);
1904789Sahrens 	}
1905789Sahrens 
1906789Sahrens 	/*
1907789Sahrens 	 * Quit if directory has been removed (posix)
1908789Sahrens 	 */
19093461Sahrens 	if ((*eofp = zp->z_unlinked) != 0) {
1910789Sahrens 		ZFS_EXIT(zfsvfs);
1911789Sahrens 		return (0);
1912789Sahrens 	}
1913789Sahrens 
1914869Sperrin 	error = 0;
1915869Sperrin 	os = zfsvfs->z_os;
1916869Sperrin 	offset = uio->uio_loffset;
1917869Sperrin 	prefetch = zp->z_zn_prefetch;
1918869Sperrin 
1919789Sahrens 	/*
1920789Sahrens 	 * Initialize the iterator cursor.
1921789Sahrens 	 */
1922789Sahrens 	if (offset <= 3) {
1923789Sahrens 		/*
1924789Sahrens 		 * Start iteration from the beginning of the directory.
1925789Sahrens 		 */
1926869Sperrin 		zap_cursor_init(&zc, os, zp->z_id);
1927789Sahrens 	} else {
1928789Sahrens 		/*
1929789Sahrens 		 * The offset is a serialized cursor.
1930789Sahrens 		 */
1931869Sperrin 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1932789Sahrens 	}
1933789Sahrens 
1934789Sahrens 	/*
1935789Sahrens 	 * Get space to change directory entries into fs independent format.
1936789Sahrens 	 */
1937789Sahrens 	iovp = uio->uio_iov;
1938789Sahrens 	bytes_wanted = iovp->iov_len;
1939789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
1940789Sahrens 		bufsize = bytes_wanted;
1941789Sahrens 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
1942789Sahrens 		odp = (struct dirent64 *)outbuf;
1943789Sahrens 	} else {
1944789Sahrens 		bufsize = bytes_wanted;
1945789Sahrens 		odp = (struct dirent64 *)iovp->iov_base;
1946789Sahrens 	}
19475331Samw 	eodp = (struct edirent *)odp;
1948789Sahrens 
1949789Sahrens 	/*
19507757SJanice.Chang@Sun.COM 	 * If this VFS supports the system attribute view interface; and
19517757SJanice.Chang@Sun.COM 	 * we're looking at an extended attribute directory; and we care
19527757SJanice.Chang@Sun.COM 	 * about normalization conflicts on this vfs; then we must check
19537757SJanice.Chang@Sun.COM 	 * for normalization conflicts with the sysattr name space.
19545663Sck153898 	 */
19557757SJanice.Chang@Sun.COM 	check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
19565663Sck153898 	    (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
19575663Sck153898 	    (flags & V_RDDIR_ENTFLAGS);
19585663Sck153898 
19595663Sck153898 	/*
1960789Sahrens 	 * Transform to file-system independent format
1961789Sahrens 	 */
1962789Sahrens 	outcount = 0;
1963789Sahrens 	while (outcount < bytes_wanted) {
19643912Slling 		ino64_t objnum;
19653912Slling 		ushort_t reclen;
19663912Slling 		off64_t *next;
19673912Slling 
1968789Sahrens 		/*
1969789Sahrens 		 * Special case `.', `..', and `.zfs'.
1970789Sahrens 		 */
1971789Sahrens 		if (offset == 0) {
1972789Sahrens 			(void) strcpy(zap.za_name, ".");
19735331Samw 			zap.za_normalization_conflict = 0;
19743912Slling 			objnum = zp->z_id;
1975789Sahrens 		} else if (offset == 1) {
1976789Sahrens 			(void) strcpy(zap.za_name, "..");
19775331Samw 			zap.za_normalization_conflict = 0;
19783912Slling 			objnum = zp->z_phys->zp_parent;
1979789Sahrens 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
1980789Sahrens 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
19815331Samw 			zap.za_normalization_conflict = 0;
19823912Slling 			objnum = ZFSCTL_INO_ROOT;
1983789Sahrens 		} else {
1984789Sahrens 			/*
1985789Sahrens 			 * Grab next entry.
1986789Sahrens 			 */
1987789Sahrens 			if (error = zap_cursor_retrieve(&zc, &zap)) {
1988789Sahrens 				if ((*eofp = (error == ENOENT)) != 0)
1989789Sahrens 					break;
1990789Sahrens 				else
1991789Sahrens 					goto update;
1992789Sahrens 			}
1993789Sahrens 
1994789Sahrens 			if (zap.za_integer_length != 8 ||
1995789Sahrens 			    zap.za_num_integers != 1) {
1996789Sahrens 				cmn_err(CE_WARN, "zap_readdir: bad directory "
1997789Sahrens 				    "entry, obj = %lld, offset = %lld\n",
1998789Sahrens 				    (u_longlong_t)zp->z_id,
1999789Sahrens 				    (u_longlong_t)offset);
2000789Sahrens 				error = ENXIO;
2001789Sahrens 				goto update;
2002789Sahrens 			}
20033912Slling 
20043912Slling 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
20053912Slling 			/*
20063912Slling 			 * MacOS X can extract the object type here such as:
20073912Slling 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
20083912Slling 			 */
20095663Sck153898 
20105663Sck153898 			if (check_sysattrs && !zap.za_normalization_conflict) {
20115663Sck153898 				zap.za_normalization_conflict =
20125663Sck153898 				    xattr_sysattr_casechk(zap.za_name);
20135663Sck153898 			}
2014789Sahrens 		}
20155331Samw 
20169749STim.Haley@Sun.COM 		if (flags & V_RDDIR_ACCFILTER) {
20179749STim.Haley@Sun.COM 			/*
20189749STim.Haley@Sun.COM 			 * If we have no access at all, don't include
20199749STim.Haley@Sun.COM 			 * this entry in the returned information
20209749STim.Haley@Sun.COM 			 */
20219749STim.Haley@Sun.COM 			znode_t	*ezp;
20229749STim.Haley@Sun.COM 			if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
20239749STim.Haley@Sun.COM 				goto skip_entry;
20249749STim.Haley@Sun.COM 			if (!zfs_has_access(ezp, cr)) {
20259749STim.Haley@Sun.COM 				VN_RELE(ZTOV(ezp));
20269749STim.Haley@Sun.COM 				goto skip_entry;
20279749STim.Haley@Sun.COM 			}
20289749STim.Haley@Sun.COM 			VN_RELE(ZTOV(ezp));
20299749STim.Haley@Sun.COM 		}
20309749STim.Haley@Sun.COM 
20315331Samw 		if (flags & V_RDDIR_ENTFLAGS)
20325331Samw 			reclen = EDIRENT_RECLEN(strlen(zap.za_name));
20335331Samw 		else
20345331Samw 			reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2035789Sahrens 
2036789Sahrens 		/*
2037789Sahrens 		 * Will this entry fit in the buffer?
2038789Sahrens 		 */
20393912Slling 		if (outcount + reclen > bufsize) {
2040789Sahrens 			/*
2041789Sahrens 			 * Did we manage to fit anything in the buffer?
2042789Sahrens 			 */
2043789Sahrens 			if (!outcount) {
2044789Sahrens 				error = EINVAL;
2045789Sahrens 				goto update;
2046789Sahrens 			}
2047789Sahrens 			break;
2048789Sahrens 		}
20495331Samw 		if (flags & V_RDDIR_ENTFLAGS) {
20505331Samw 			/*
20515331Samw 			 * Add extended flag entry:
20525331Samw 			 */
20535331Samw 			eodp->ed_ino = objnum;
20545331Samw 			eodp->ed_reclen = reclen;
20555331Samw 			/* NOTE: ed_off is the offset for the *next* entry */
20565331Samw 			next = &(eodp->ed_off);
20575331Samw 			eodp->ed_eflags = zap.za_normalization_conflict ?
20585331Samw 			    ED_CASE_CONFLICT : 0;
20595331Samw 			(void) strncpy(eodp->ed_name, zap.za_name,
20605331Samw 			    EDIRENT_NAMELEN(reclen));
20615331Samw 			eodp = (edirent_t *)((intptr_t)eodp + reclen);
20625331Samw 		} else {
20635331Samw 			/*
20645331Samw 			 * Add normal entry:
20655331Samw 			 */
20665331Samw 			odp->d_ino = objnum;
20675331Samw 			odp->d_reclen = reclen;
20685331Samw 			/* NOTE: d_off is the offset for the *next* entry */
20695331Samw 			next = &(odp->d_off);
20705331Samw 			(void) strncpy(odp->d_name, zap.za_name,
20715331Samw 			    DIRENT64_NAMELEN(reclen));
20725331Samw 			odp = (dirent64_t *)((intptr_t)odp + reclen);
20735331Samw 		}
20743912Slling 		outcount += reclen;
2075789Sahrens 
2076789Sahrens 		ASSERT(outcount <= bufsize);
2077789Sahrens 
2078789Sahrens 		/* Prefetch znode */
2079869Sperrin 		if (prefetch)
20803912Slling 			dmu_prefetch(os, objnum, 0, 0);
2081789Sahrens 
20829749STim.Haley@Sun.COM 	skip_entry:
2083789Sahrens 		/*
2084789Sahrens 		 * Move to the next entry, fill in the previous offset.
2085789Sahrens 		 */
2086789Sahrens 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2087789Sahrens 			zap_cursor_advance(&zc);
2088789Sahrens 			offset = zap_cursor_serialize(&zc);
2089789Sahrens 		} else {
2090789Sahrens 			offset += 1;
2091789Sahrens 		}
2092789Sahrens 		*next = offset;
2093789Sahrens 	}
2094869Sperrin 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2095789Sahrens 
2096789Sahrens 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2097789Sahrens 		iovp->iov_base += outcount;
2098789Sahrens 		iovp->iov_len -= outcount;
2099789Sahrens 		uio->uio_resid -= outcount;
2100789Sahrens 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2101789Sahrens 		/*
2102789Sahrens 		 * Reset the pointer.
2103789Sahrens 		 */
2104789Sahrens 		offset = uio->uio_loffset;
2105789Sahrens 	}
2106789Sahrens 
2107789Sahrens update:
2108885Sahrens 	zap_cursor_fini(&zc);
2109789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2110789Sahrens 		kmem_free(outbuf, bufsize);
2111789Sahrens 
2112789Sahrens 	if (error == ENOENT)
2113789Sahrens 		error = 0;
2114789Sahrens 
2115789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2116789Sahrens 
2117789Sahrens 	uio->uio_loffset = offset;
2118789Sahrens 	ZFS_EXIT(zfsvfs);
2119789Sahrens 	return (error);
2120789Sahrens }
2121789Sahrens 
21224720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4;
21234720Sfr157268 
2124789Sahrens static int
21255331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2126789Sahrens {
2127789Sahrens 	znode_t	*zp = VTOZ(vp);
2128789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2129789Sahrens 
21301773Seschrock 	/*
21311773Seschrock 	 * Regardless of whether this is required for standards conformance,
21321773Seschrock 	 * this is the logical behavior when fsync() is called on a file with
21331773Seschrock 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
21341773Seschrock 	 * going to be pushed out as part of the zil_commit().
21351773Seschrock 	 */
21361773Seschrock 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
21371773Seschrock 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
21385331Samw 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
21391773Seschrock 
21404720Sfr157268 	(void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
21414720Sfr157268 
21425367Sahrens 	ZFS_ENTER(zfsvfs);
21435367Sahrens 	ZFS_VERIFY_ZP(zp);
21442638Sperrin 	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
2145789Sahrens 	ZFS_EXIT(zfsvfs);
2146789Sahrens 	return (0);
2147789Sahrens }
2148789Sahrens 
21495331Samw 
2150789Sahrens /*
2151789Sahrens  * Get the requested file attributes and place them in the provided
2152789Sahrens  * vattr structure.
2153789Sahrens  *
2154789Sahrens  *	IN:	vp	- vnode of file.
2155789Sahrens  *		vap	- va_mask identifies requested attributes.
21565331Samw  *			  If AT_XVATTR set, then optional attrs are requested
21575331Samw  *		flags	- ATTR_NOACLCHECK (CIFS server context)
2158789Sahrens  *		cr	- credentials of caller.
21595331Samw  *		ct	- caller context
2160789Sahrens  *
2161789Sahrens  *	OUT:	vap	- attribute values.
2162789Sahrens  *
2163789Sahrens  *	RETURN:	0 (always succeeds)
2164789Sahrens  */
2165789Sahrens /* ARGSUSED */
2166789Sahrens static int
21675331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
21685331Samw     caller_context_t *ct)
2169789Sahrens {
2170789Sahrens 	znode_t *zp = VTOZ(vp);
2171789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
21725326Sek110237 	znode_phys_t *pzp;
21735331Samw 	int	error = 0;
21744543Smarks 	uint64_t links;
21755331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
21765331Samw 	xoptattr_t *xoap = NULL;
21775331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2178789Sahrens 
21795367Sahrens 	ZFS_ENTER(zfsvfs);
21805367Sahrens 	ZFS_VERIFY_ZP(zp);
21815326Sek110237 	pzp = zp->z_phys;
2182789Sahrens 
21835331Samw 	/*
21845331Samw 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
21855331Samw 	 * Also, if we are the owner don't bother, since owner should
21865331Samw 	 * always be allowed to read basic attributes of file.
21875331Samw 	 */
21885331Samw 	if (!(pzp->zp_flags & ZFS_ACL_TRIVIAL) &&
21895331Samw 	    (pzp->zp_uid != crgetuid(cr))) {
21905331Samw 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
21915331Samw 		    skipaclchk, cr)) {
21925331Samw 			ZFS_EXIT(zfsvfs);
21935331Samw 			return (error);
21945331Samw 		}
21955331Samw 	}
21965331Samw 
2197789Sahrens 	/*
2198789Sahrens 	 * Return all attributes.  It's cheaper to provide the answer
2199789Sahrens 	 * than to determine whether we were asked the question.
2200789Sahrens 	 */
2201789Sahrens 
22029774SRay.Hassan@Sun.COM 	mutex_enter(&zp->z_lock);
2203789Sahrens 	vap->va_type = vp->v_type;
2204789Sahrens 	vap->va_mode = pzp->zp_mode & MODEMASK;
22055771Sjp151216 	zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2206789Sahrens 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2207789Sahrens 	vap->va_nodeid = zp->z_id;
22084543Smarks 	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
22094543Smarks 		links = pzp->zp_links + 1;
22104543Smarks 	else
22114543Smarks 		links = pzp->zp_links;
22124543Smarks 	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
2213789Sahrens 	vap->va_size = pzp->zp_size;
22141816Smarks 	vap->va_rdev = vp->v_rdev;
2215789Sahrens 	vap->va_seq = zp->z_seq;
2216789Sahrens 
22175331Samw 	/*
22185331Samw 	 * Add in any requested optional attributes and the create time.
22195331Samw 	 * Also set the corresponding bits in the returned attribute bitmap.
22205331Samw 	 */
22215331Samw 	if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
22225331Samw 		if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
22235331Samw 			xoap->xoa_archive =
22245331Samw 			    ((pzp->zp_flags & ZFS_ARCHIVE) != 0);
22255331Samw 			XVA_SET_RTN(xvap, XAT_ARCHIVE);
22265331Samw 		}
22275331Samw 
22285331Samw 		if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
22295331Samw 			xoap->xoa_readonly =
22305331Samw 			    ((pzp->zp_flags & ZFS_READONLY) != 0);
22315331Samw 			XVA_SET_RTN(xvap, XAT_READONLY);
22325331Samw 		}
22335331Samw 
22345331Samw 		if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
22355331Samw 			xoap->xoa_system =
22365331Samw 			    ((pzp->zp_flags & ZFS_SYSTEM) != 0);
22375331Samw 			XVA_SET_RTN(xvap, XAT_SYSTEM);
22385331Samw 		}
22395331Samw 
22405331Samw 		if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
22415331Samw 			xoap->xoa_hidden =
22425331Samw 			    ((pzp->zp_flags & ZFS_HIDDEN) != 0);
22435331Samw 			XVA_SET_RTN(xvap, XAT_HIDDEN);
22445331Samw 		}
22455331Samw 
22465331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
22475331Samw 			xoap->xoa_nounlink =
22485331Samw 			    ((pzp->zp_flags & ZFS_NOUNLINK) != 0);
22495331Samw 			XVA_SET_RTN(xvap, XAT_NOUNLINK);
22505331Samw 		}
22515331Samw 
22525331Samw 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
22535331Samw 			xoap->xoa_immutable =
22545331Samw 			    ((pzp->zp_flags & ZFS_IMMUTABLE) != 0);
22555331Samw 			XVA_SET_RTN(xvap, XAT_IMMUTABLE);
22565331Samw 		}
22575331Samw 
22585331Samw 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
22595331Samw 			xoap->xoa_appendonly =
22605331Samw 			    ((pzp->zp_flags & ZFS_APPENDONLY) != 0);
22615331Samw 			XVA_SET_RTN(xvap, XAT_APPENDONLY);
22625331Samw 		}
22635331Samw 
22645331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
22655331Samw 			xoap->xoa_nodump =
22665331Samw 			    ((pzp->zp_flags & ZFS_NODUMP) != 0);
22675331Samw 			XVA_SET_RTN(xvap, XAT_NODUMP);
22685331Samw 		}
22695331Samw 
22705331Samw 		if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
22715331Samw 			xoap->xoa_opaque =
22725331Samw 			    ((pzp->zp_flags & ZFS_OPAQUE) != 0);
22735331Samw 			XVA_SET_RTN(xvap, XAT_OPAQUE);
22745331Samw 		}
22755331Samw 
22765331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
22775331Samw 			xoap->xoa_av_quarantined =
22785331Samw 			    ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0);
22795331Samw 			XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
22805331Samw 		}
22815331Samw 
22825331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
22835331Samw 			xoap->xoa_av_modified =
22845331Samw 			    ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0);
22855331Samw 			XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
22865331Samw 		}
22875331Samw 
22885331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
22895331Samw 		    vp->v_type == VREG &&
22905331Samw 		    (pzp->zp_flags & ZFS_BONUS_SCANSTAMP)) {
22915331Samw 			size_t len;
22925331Samw 			dmu_object_info_t doi;
22935331Samw 
22945331Samw 			/*
22955331Samw 			 * Only VREG files have anti-virus scanstamps, so we
22965331Samw 			 * won't conflict with symlinks in the bonus buffer.
22975331Samw 			 */
22985331Samw 			dmu_object_info_from_db(zp->z_dbuf, &doi);
22995331Samw 			len = sizeof (xoap->xoa_av_scanstamp) +
23005331Samw 			    sizeof (znode_phys_t);
23015331Samw 			if (len <= doi.doi_bonus_size) {
23025331Samw 				/*
23035331Samw 				 * pzp points to the start of the
23045331Samw 				 * znode_phys_t. pzp + 1 points to the
23055331Samw 				 * first byte after the znode_phys_t.
23065331Samw 				 */
23075331Samw 				(void) memcpy(xoap->xoa_av_scanstamp,
23085331Samw 				    pzp + 1,
23095331Samw 				    sizeof (xoap->xoa_av_scanstamp));
23105331Samw 				XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
23115331Samw 			}
23125331Samw 		}
23135331Samw 
23145331Samw 		if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
23155331Samw 			ZFS_TIME_DECODE(&xoap->xoa_createtime, pzp->zp_crtime);
23165331Samw 			XVA_SET_RTN(xvap, XAT_CREATETIME);
23175331Samw 		}
23185331Samw 	}
23195331Samw 
2320789Sahrens 	ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
2321789Sahrens 	ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
2322789Sahrens 	ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
2323789Sahrens 
2324789Sahrens 	mutex_exit(&zp->z_lock);
2325789Sahrens 
2326789Sahrens 	dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks);
2327789Sahrens 
2328789Sahrens 	if (zp->z_blksz == 0) {
2329789Sahrens 		/*
2330789Sahrens 		 * Block size hasn't been set; suggest maximal I/O transfers.
2331789Sahrens 		 */
2332789Sahrens 		vap->va_blksize = zfsvfs->z_max_blksz;
2333789Sahrens 	}
2334789Sahrens 
2335789Sahrens 	ZFS_EXIT(zfsvfs);
2336789Sahrens 	return (0);
2337789Sahrens }
2338789Sahrens 
2339789Sahrens /*
2340789Sahrens  * Set the file attributes to the values contained in the
2341789Sahrens  * vattr structure.
2342789Sahrens  *
2343789Sahrens  *	IN:	vp	- vnode of file to be modified.
2344789Sahrens  *		vap	- new attribute values.
23455331Samw  *			  If AT_XVATTR set, then optional attrs are being set
2346789Sahrens  *		flags	- ATTR_UTIME set if non-default time values provided.
23475331Samw  *			- ATTR_NOACLCHECK (CIFS context only).
2348789Sahrens  *		cr	- credentials of caller.
23495331Samw  *		ct	- caller context
2350789Sahrens  *
2351789Sahrens  *	RETURN:	0 if success
2352789Sahrens  *		error code if failure
2353789Sahrens  *
2354789Sahrens  * Timestamps:
2355789Sahrens  *	vp - ctime updated, mtime updated if size changed.
2356789Sahrens  */
2357789Sahrens /* ARGSUSED */
2358789Sahrens static int
2359789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2360789Sahrens 	caller_context_t *ct)
2361789Sahrens {
23625326Sek110237 	znode_t		*zp = VTOZ(vp);
23635326Sek110237 	znode_phys_t	*pzp;
2364789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
23655326Sek110237 	zilog_t		*zilog;
2366789Sahrens 	dmu_tx_t	*tx;
23671878Smaybee 	vattr_t		oldva;
23688190SMark.Shellenbaum@Sun.COM 	xvattr_t	tmpxvattr;
2369789Sahrens 	uint_t		mask = vap->va_mask;
23701878Smaybee 	uint_t		saved_mask;
23712796Smarks 	int		trim_mask = 0;
2372789Sahrens 	uint64_t	new_mode;
23739179SMark.Shellenbaum@Sun.COM 	uint64_t	new_uid, new_gid;
23741231Smarks 	znode_t		*attrzp;
2375789Sahrens 	int		need_policy = FALSE;
2376789Sahrens 	int		err;
23775331Samw 	zfs_fuid_info_t *fuidp = NULL;
23785331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
23795331Samw 	xoptattr_t	*xoap;
23805824Smarks 	zfs_acl_t	*aclp = NULL;
23815331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
23829179SMark.Shellenbaum@Sun.COM 	boolean_t fuid_dirtied = B_FALSE;
2383789Sahrens 
2384789Sahrens 	if (mask == 0)
2385789Sahrens 		return (0);
2386789Sahrens 
2387789Sahrens 	if (mask & AT_NOSET)
2388789Sahrens 		return (EINVAL);
2389789Sahrens 
23905367Sahrens 	ZFS_ENTER(zfsvfs);
23915367Sahrens 	ZFS_VERIFY_ZP(zp);
23925331Samw 
23935331Samw 	pzp = zp->z_phys;
23945331Samw 	zilog = zfsvfs->z_log;
23955331Samw 
23965331Samw 	/*
23975331Samw 	 * Make sure that if we have ephemeral uid/gid or xvattr specified
23985331Samw 	 * that file system is at proper version level
23995331Samw 	 */
24005331Samw 
24015331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
24025331Samw 	    (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
24035331Samw 	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
24045386Stimh 	    (mask & AT_XVATTR))) {
24055386Stimh 		ZFS_EXIT(zfsvfs);
24065331Samw 		return (EINVAL);
24075386Stimh 	}
24085386Stimh 
24095386Stimh 	if (mask & AT_SIZE && vp->v_type == VDIR) {
24105386Stimh 		ZFS_EXIT(zfsvfs);
2411789Sahrens 		return (EISDIR);
24125386Stimh 	}
24135386Stimh 
24145386Stimh 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
24155386Stimh 		ZFS_EXIT(zfsvfs);
24161308Smarks 		return (EINVAL);
24175386Stimh 	}
24181308Smarks 
24195331Samw 	/*
24205331Samw 	 * If this is an xvattr_t, then get a pointer to the structure of
24215331Samw 	 * optional attributes.  If this is NULL, then we have a vattr_t.
24225331Samw 	 */
24235331Samw 	xoap = xva_getxoptattr(xvap);
24245331Samw 
24258190SMark.Shellenbaum@Sun.COM 	xva_init(&tmpxvattr);
24268190SMark.Shellenbaum@Sun.COM 
24275331Samw 	/*
24285331Samw 	 * Immutable files can only alter immutable bit and atime
24295331Samw 	 */
24305331Samw 	if ((pzp->zp_flags & ZFS_IMMUTABLE) &&
24315331Samw 	    ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
24325386Stimh 	    ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
24335386Stimh 		ZFS_EXIT(zfsvfs);
24345331Samw 		return (EPERM);
24355386Stimh 	}
24365386Stimh 
24375386Stimh 	if ((mask & AT_SIZE) && (pzp->zp_flags & ZFS_READONLY)) {
24385386Stimh 		ZFS_EXIT(zfsvfs);
24395331Samw 		return (EPERM);
24405386Stimh 	}
2441789Sahrens 
24426064Smarks 	/*
24436064Smarks 	 * Verify timestamps doesn't overflow 32 bits.
24446064Smarks 	 * ZFS can handle large timestamps, but 32bit syscalls can't
24456064Smarks 	 * handle times greater than 2039.  This check should be removed
24466064Smarks 	 * once large timestamps are fully supported.
24476064Smarks 	 */
24486064Smarks 	if (mask & (AT_ATIME | AT_MTIME)) {
24496064Smarks 		if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
24506064Smarks 		    ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
24516064Smarks 			ZFS_EXIT(zfsvfs);
24526064Smarks 			return (EOVERFLOW);
24536064Smarks 		}
24546064Smarks 	}
24556064Smarks 
2456789Sahrens top:
24571231Smarks 	attrzp = NULL;
2458789Sahrens 
2459789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2460789Sahrens 		ZFS_EXIT(zfsvfs);
2461789Sahrens 		return (EROFS);
2462789Sahrens 	}
2463789Sahrens 
2464789Sahrens 	/*
2465789Sahrens 	 * First validate permissions
2466789Sahrens 	 */
2467789Sahrens 
2468789Sahrens 	if (mask & AT_SIZE) {
24695331Samw 		err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2470789Sahrens 		if (err) {
2471789Sahrens 			ZFS_EXIT(zfsvfs);
2472789Sahrens 			return (err);
2473789Sahrens 		}
24741878Smaybee 		/*
24751878Smaybee 		 * XXX - Note, we are not providing any open
24761878Smaybee 		 * mode flags here (like FNDELAY), so we may
24771878Smaybee 		 * block if there are locks present... this
24781878Smaybee 		 * should be addressed in openat().
24791878Smaybee 		 */
24806992Smaybee 		/* XXX - would it be OK to generate a log record here? */
24816992Smaybee 		err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
24821878Smaybee 		if (err) {
24831878Smaybee 			ZFS_EXIT(zfsvfs);
24841878Smaybee 			return (err);
24851878Smaybee 		}
2486789Sahrens 	}
2487789Sahrens 
24885331Samw 	if (mask & (AT_ATIME|AT_MTIME) ||
24895331Samw 	    ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
24905331Samw 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
24915331Samw 	    XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
24925331Samw 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
24935331Samw 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM))))
24945331Samw 		need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
24955331Samw 		    skipaclchk, cr);
2496789Sahrens 
2497789Sahrens 	if (mask & (AT_UID|AT_GID)) {
2498789Sahrens 		int	idmask = (mask & (AT_UID|AT_GID));
2499789Sahrens 		int	take_owner;
2500789Sahrens 		int	take_group;
2501789Sahrens 
2502789Sahrens 		/*
2503913Smarks 		 * NOTE: even if a new mode is being set,
2504913Smarks 		 * we may clear S_ISUID/S_ISGID bits.
2505913Smarks 		 */
2506913Smarks 
2507913Smarks 		if (!(mask & AT_MODE))
2508913Smarks 			vap->va_mode = pzp->zp_mode;
2509913Smarks 
2510913Smarks 		/*
2511789Sahrens 		 * Take ownership or chgrp to group we are a member of
2512789Sahrens 		 */
2513789Sahrens 
2514789Sahrens 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
25155331Samw 		take_group = (mask & AT_GID) &&
25165331Samw 		    zfs_groupmember(zfsvfs, vap->va_gid, cr);
2517789Sahrens 
2518789Sahrens 		/*
2519789Sahrens 		 * If both AT_UID and AT_GID are set then take_owner and
2520789Sahrens 		 * take_group must both be set in order to allow taking
2521789Sahrens 		 * ownership.
2522789Sahrens 		 *
2523789Sahrens 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2524789Sahrens 		 *
2525789Sahrens 		 */
2526789Sahrens 
2527789Sahrens 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2528789Sahrens 		    ((idmask == AT_UID) && take_owner) ||
2529789Sahrens 		    ((idmask == AT_GID) && take_group)) {
25305331Samw 			if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
25315331Samw 			    skipaclchk, cr) == 0) {
2532789Sahrens 				/*
2533789Sahrens 				 * Remove setuid/setgid for non-privileged users
2534789Sahrens 				 */
25351115Smarks 				secpolicy_setid_clear(vap, cr);
25362796Smarks 				trim_mask = (mask & (AT_UID|AT_GID));
2537789Sahrens 			} else {
2538789Sahrens 				need_policy =  TRUE;
2539789Sahrens 			}
2540789Sahrens 		} else {
2541789Sahrens 			need_policy =  TRUE;
2542789Sahrens 		}
2543789Sahrens 	}
2544789Sahrens 
25452796Smarks 	mutex_enter(&zp->z_lock);
25462796Smarks 	oldva.va_mode = pzp->zp_mode;
25475771Sjp151216 	zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
25485331Samw 	if (mask & AT_XVATTR) {
25498190SMark.Shellenbaum@Sun.COM 		/*
25508190SMark.Shellenbaum@Sun.COM 		 * Update xvattr mask to include only those attributes
25518190SMark.Shellenbaum@Sun.COM 		 * that are actually changing.
25528190SMark.Shellenbaum@Sun.COM 		 *
25538190SMark.Shellenbaum@Sun.COM 		 * the bits will be restored prior to actually setting
25548190SMark.Shellenbaum@Sun.COM 		 * the attributes so the caller thinks they were set.
25558190SMark.Shellenbaum@Sun.COM 		 */
25568190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
25578190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_appendonly !=
25588190SMark.Shellenbaum@Sun.COM 			    ((pzp->zp_flags & ZFS_APPENDONLY) != 0)) {
25598190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
25608190SMark.Shellenbaum@Sun.COM 			} else {
25618190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_APPENDONLY);
25628190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
25638190SMark.Shellenbaum@Sun.COM 			}
25648190SMark.Shellenbaum@Sun.COM 		}
25658190SMark.Shellenbaum@Sun.COM 
25668190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
25678190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_nounlink !=
25688190SMark.Shellenbaum@Sun.COM 			    ((pzp->zp_flags & ZFS_NOUNLINK) != 0)) {
25698190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
25708190SMark.Shellenbaum@Sun.COM 			} else {
25718190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_NOUNLINK);
25728190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
25738190SMark.Shellenbaum@Sun.COM 			}
25748190SMark.Shellenbaum@Sun.COM 		}
25758190SMark.Shellenbaum@Sun.COM 
25768190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
25778190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_immutable !=
25788190SMark.Shellenbaum@Sun.COM 			    ((pzp->zp_flags & ZFS_IMMUTABLE) != 0)) {
25798190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
25808190SMark.Shellenbaum@Sun.COM 			} else {
25818190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
25828190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
25838190SMark.Shellenbaum@Sun.COM 			}
25848190SMark.Shellenbaum@Sun.COM 		}
25858190SMark.Shellenbaum@Sun.COM 
25868190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
25878190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_nodump !=
25888190SMark.Shellenbaum@Sun.COM 			    ((pzp->zp_flags & ZFS_NODUMP) != 0)) {
25898190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
25908190SMark.Shellenbaum@Sun.COM 			} else {
25918190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_NODUMP);
25928190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
25938190SMark.Shellenbaum@Sun.COM 			}
25948190SMark.Shellenbaum@Sun.COM 		}
25958190SMark.Shellenbaum@Sun.COM 
25968190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
25978190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_av_modified !=
25988190SMark.Shellenbaum@Sun.COM 			    ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0)) {
25998190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
26008190SMark.Shellenbaum@Sun.COM 			} else {
26018190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
26028190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
26038190SMark.Shellenbaum@Sun.COM 			}
26048190SMark.Shellenbaum@Sun.COM 		}
26058190SMark.Shellenbaum@Sun.COM 
26068190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
26078190SMark.Shellenbaum@Sun.COM 			if ((vp->v_type != VREG &&
26088190SMark.Shellenbaum@Sun.COM 			    xoap->xoa_av_quarantined) ||
26098190SMark.Shellenbaum@Sun.COM 			    xoap->xoa_av_quarantined !=
26108190SMark.Shellenbaum@Sun.COM 			    ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0)) {
26118190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
26128190SMark.Shellenbaum@Sun.COM 			} else {
26138190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
26148190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
26158190SMark.Shellenbaum@Sun.COM 			}
26168190SMark.Shellenbaum@Sun.COM 		}
26178190SMark.Shellenbaum@Sun.COM 
26188190SMark.Shellenbaum@Sun.COM 		if (need_policy == FALSE &&
26198190SMark.Shellenbaum@Sun.COM 		    (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
26208190SMark.Shellenbaum@Sun.COM 		    XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
26215331Samw 			need_policy = TRUE;
26225331Samw 		}
26235331Samw 	}
26245331Samw 
26252796Smarks 	mutex_exit(&zp->z_lock);
26262796Smarks 
26272796Smarks 	if (mask & AT_MODE) {
26285331Samw 		if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
26292796Smarks 			err = secpolicy_setid_setsticky_clear(vp, vap,
26302796Smarks 			    &oldva, cr);
26312796Smarks 			if (err) {
26322796Smarks 				ZFS_EXIT(zfsvfs);
26332796Smarks 				return (err);
26342796Smarks 			}
26352796Smarks 			trim_mask |= AT_MODE;
26362796Smarks 		} else {
26372796Smarks 			need_policy = TRUE;
26382796Smarks 		}
26392796Smarks 	}
2640789Sahrens 
2641789Sahrens 	if (need_policy) {
26421115Smarks 		/*
26431115Smarks 		 * If trim_mask is set then take ownership
26442796Smarks 		 * has been granted or write_acl is present and user
26452796Smarks 		 * has the ability to modify mode.  In that case remove
26462796Smarks 		 * UID|GID and or MODE from mask so that
26471115Smarks 		 * secpolicy_vnode_setattr() doesn't revoke it.
26481115Smarks 		 */
26492796Smarks 
26502796Smarks 		if (trim_mask) {
26512796Smarks 			saved_mask = vap->va_mask;
26522796Smarks 			vap->va_mask &= ~trim_mask;
26532796Smarks 		}
2654789Sahrens 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
26555331Samw 		    (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2656789Sahrens 		if (err) {
2657789Sahrens 			ZFS_EXIT(zfsvfs);
2658789Sahrens 			return (err);
2659789Sahrens 		}
26601115Smarks 
26611115Smarks 		if (trim_mask)
26622796Smarks 			vap->va_mask |= saved_mask;
2663789Sahrens 	}
2664789Sahrens 
2665789Sahrens 	/*
2666789Sahrens 	 * secpolicy_vnode_setattr, or take ownership may have
2667789Sahrens 	 * changed va_mask
2668789Sahrens 	 */
2669789Sahrens 	mask = vap->va_mask;
2670789Sahrens 
2671789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2672789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
2673789Sahrens 
2674789Sahrens 	if (mask & AT_MODE) {
26751576Smarks 		uint64_t pmode = pzp->zp_mode;
26761576Smarks 
26771576Smarks 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2678789Sahrens 
26799396SMatthew.Ahrens@Sun.COM 		if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
26809396SMatthew.Ahrens@Sun.COM 			goto out;
26815331Samw 		if (pzp->zp_acl.z_acl_extern_obj) {
26825331Samw 			/* Are we upgrading ACL from old V0 format to new V1 */
26835331Samw 			if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
26845331Samw 			    pzp->zp_acl.z_acl_version ==
26855331Samw 			    ZFS_ACL_VERSION_INITIAL) {
26865331Samw 				dmu_tx_hold_free(tx,
26875331Samw 				    pzp->zp_acl.z_acl_extern_obj, 0,
26885331Samw 				    DMU_OBJECT_END);
26895331Samw 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
26905824Smarks 				    0, aclp->z_acl_bytes);
26915331Samw 			} else {
26925331Samw 				dmu_tx_hold_write(tx,
26935331Samw 				    pzp->zp_acl.z_acl_extern_obj, 0,
26945824Smarks 				    aclp->z_acl_bytes);
26955331Samw 			}
26966180Smarks 		} else if (aclp->z_acl_bytes > ZFS_ACE_SPACE) {
26976180Smarks 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
26986180Smarks 			    0, aclp->z_acl_bytes);
26995331Samw 		}
2700789Sahrens 	}
2701789Sahrens 
27029179SMark.Shellenbaum@Sun.COM 	if (mask & (AT_UID | AT_GID)) {
27039179SMark.Shellenbaum@Sun.COM 		if (pzp->zp_xattr) {
27049179SMark.Shellenbaum@Sun.COM 			err = zfs_zget(zp->z_zfsvfs, pzp->zp_xattr, &attrzp);
27059396SMatthew.Ahrens@Sun.COM 			if (err)
27069396SMatthew.Ahrens@Sun.COM 				goto out;
27079179SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_bonus(tx, attrzp->z_id);
27089179SMark.Shellenbaum@Sun.COM 		}
27099179SMark.Shellenbaum@Sun.COM 		if (mask & AT_UID) {
27109179SMark.Shellenbaum@Sun.COM 			new_uid = zfs_fuid_create(zfsvfs,
27119179SMark.Shellenbaum@Sun.COM 			    (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
27129396SMatthew.Ahrens@Sun.COM 			if (new_uid != pzp->zp_uid &&
27139396SMatthew.Ahrens@Sun.COM 			    zfs_usergroup_overquota(zfsvfs, B_FALSE, new_uid)) {
27149396SMatthew.Ahrens@Sun.COM 				err = EDQUOT;
27159396SMatthew.Ahrens@Sun.COM 				goto out;
27169396SMatthew.Ahrens@Sun.COM 			}
27171231Smarks 		}
27189396SMatthew.Ahrens@Sun.COM 
27199179SMark.Shellenbaum@Sun.COM 		if (mask & AT_GID) {
27209179SMark.Shellenbaum@Sun.COM 			new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
27219179SMark.Shellenbaum@Sun.COM 			    cr, ZFS_GROUP, &fuidp);
27229396SMatthew.Ahrens@Sun.COM 			if (new_gid != pzp->zp_gid &&
27239396SMatthew.Ahrens@Sun.COM 			    zfs_usergroup_overquota(zfsvfs, B_TRUE, new_gid)) {
27249396SMatthew.Ahrens@Sun.COM 				err = EDQUOT;
27259396SMatthew.Ahrens@Sun.COM 				goto out;
27269396SMatthew.Ahrens@Sun.COM 			}
27279179SMark.Shellenbaum@Sun.COM 		}
27289179SMark.Shellenbaum@Sun.COM 		fuid_dirtied = zfsvfs->z_fuid_dirty;
27299179SMark.Shellenbaum@Sun.COM 		if (fuid_dirtied) {
27309179SMark.Shellenbaum@Sun.COM 			if (zfsvfs->z_fuid_obj == 0) {
27319179SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
27329179SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
27339179SMark.Shellenbaum@Sun.COM 				    FUID_SIZE_ESTIMATE(zfsvfs));
27349179SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_zap(tx, MASTER_NODE_OBJ,
27359179SMark.Shellenbaum@Sun.COM 				    FALSE, NULL);
27369179SMark.Shellenbaum@Sun.COM 			} else {
27379179SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
27389179SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
27399179SMark.Shellenbaum@Sun.COM 				    FUID_SIZE_ESTIMATE(zfsvfs));
27409179SMark.Shellenbaum@Sun.COM 			}
27419179SMark.Shellenbaum@Sun.COM 		}
27421231Smarks 	}
27431231Smarks 
27448227SNeil.Perrin@Sun.COM 	err = dmu_tx_assign(tx, TXG_NOWAIT);
2745789Sahrens 	if (err) {
27469396SMatthew.Ahrens@Sun.COM 		if (err == ERESTART)
27472113Sahrens 			dmu_tx_wait(tx);
27489396SMatthew.Ahrens@Sun.COM 		goto out;
2749789Sahrens 	}
2750789Sahrens 
2751789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
2752789Sahrens 
2753789Sahrens 	/*
2754789Sahrens 	 * Set each attribute requested.
2755789Sahrens 	 * We group settings according to the locks they need to acquire.
2756789Sahrens 	 *
2757789Sahrens 	 * Note: you cannot set ctime directly, although it will be
2758789Sahrens 	 * updated as a side-effect of calling this function.
2759789Sahrens 	 */
2760789Sahrens 
2761789Sahrens 	mutex_enter(&zp->z_lock);
2762789Sahrens 
2763789Sahrens 	if (mask & AT_MODE) {
27645824Smarks 		mutex_enter(&zp->z_acl_lock);
27655824Smarks 		zp->z_phys->zp_mode = new_mode;
27669179SMark.Shellenbaum@Sun.COM 		err = zfs_aclset_common(zp, aclp, cr, tx);
2767789Sahrens 		ASSERT3U(err, ==, 0);
27685824Smarks 		mutex_exit(&zp->z_acl_lock);
2769789Sahrens 	}
2770789Sahrens 
27711231Smarks 	if (attrzp)
27721231Smarks 		mutex_enter(&attrzp->z_lock);
27731231Smarks 
27741231Smarks 	if (mask & AT_UID) {
27759179SMark.Shellenbaum@Sun.COM 		pzp->zp_uid = new_uid;
27769179SMark.Shellenbaum@Sun.COM 		if (attrzp)
27779179SMark.Shellenbaum@Sun.COM 			attrzp->z_phys->zp_uid = new_uid;
27781231Smarks 	}
27791231Smarks 
27801231Smarks 	if (mask & AT_GID) {
27819179SMark.Shellenbaum@Sun.COM 		pzp->zp_gid = new_gid;
27821231Smarks 		if (attrzp)
27839179SMark.Shellenbaum@Sun.COM 			attrzp->z_phys->zp_gid = new_gid;
27841231Smarks 	}
27851231Smarks 
27861231Smarks 	if (attrzp)
27871231Smarks 		mutex_exit(&attrzp->z_lock);
2788789Sahrens 
2789789Sahrens 	if (mask & AT_ATIME)
2790789Sahrens 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
2791789Sahrens 
2792789Sahrens 	if (mask & AT_MTIME)
2793789Sahrens 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
2794789Sahrens 
27956992Smaybee 	/* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
27961878Smaybee 	if (mask & AT_SIZE)
2797789Sahrens 		zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
27981878Smaybee 	else if (mask != 0)
2799789Sahrens 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
28005331Samw 	/*
28015331Samw 	 * Do this after setting timestamps to prevent timestamp
28025331Samw 	 * update from toggling bit
28035331Samw 	 */
28045331Samw 
28055331Samw 	if (xoap && (mask & AT_XVATTR)) {
28068190SMark.Shellenbaum@Sun.COM 
28078190SMark.Shellenbaum@Sun.COM 		/*
28088190SMark.Shellenbaum@Sun.COM 		 * restore trimmed off masks
28098190SMark.Shellenbaum@Sun.COM 		 * so that return masks can be set for caller.
28108190SMark.Shellenbaum@Sun.COM 		 */
28118190SMark.Shellenbaum@Sun.COM 
28128190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
28138190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_APPENDONLY);
28148190SMark.Shellenbaum@Sun.COM 		}
28158190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
28168190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_NOUNLINK);
28178190SMark.Shellenbaum@Sun.COM 		}
28188190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
28198190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_IMMUTABLE);
28208190SMark.Shellenbaum@Sun.COM 		}
28218190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
28228190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_NODUMP);
28238190SMark.Shellenbaum@Sun.COM 		}
28248190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
28258190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
28268190SMark.Shellenbaum@Sun.COM 		}
28278190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
28288190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
28298190SMark.Shellenbaum@Sun.COM 		}
28308190SMark.Shellenbaum@Sun.COM 
28315331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
28325331Samw 			size_t len;
28335331Samw 			dmu_object_info_t doi;
28345331Samw 
28355331Samw 			ASSERT(vp->v_type == VREG);
28365331Samw 
28375331Samw 			/* Grow the bonus buffer if necessary. */
28385331Samw 			dmu_object_info_from_db(zp->z_dbuf, &doi);
28395331Samw 			len = sizeof (xoap->xoa_av_scanstamp) +
28405331Samw 			    sizeof (znode_phys_t);
28415331Samw 			if (len > doi.doi_bonus_size)
28425331Samw 				VERIFY(dmu_set_bonus(zp->z_dbuf, len, tx) == 0);
28435331Samw 		}
28445331Samw 		zfs_xvattr_set(zp, xvap);
28455331Samw 	}
2846789Sahrens 
28479179SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
28489179SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
28499179SMark.Shellenbaum@Sun.COM 
28501878Smaybee 	if (mask != 0)
28515331Samw 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
28525331Samw 
2853789Sahrens 	mutex_exit(&zp->z_lock);
2854789Sahrens 
28559396SMatthew.Ahrens@Sun.COM out:
28561231Smarks 	if (attrzp)
28571231Smarks 		VN_RELE(ZTOV(attrzp));
28581231Smarks 
28599396SMatthew.Ahrens@Sun.COM 	if (aclp) {
28609396SMatthew.Ahrens@Sun.COM 		zfs_acl_free(aclp);
28619396SMatthew.Ahrens@Sun.COM 		aclp = NULL;
28629396SMatthew.Ahrens@Sun.COM 	}
28639396SMatthew.Ahrens@Sun.COM 
28649396SMatthew.Ahrens@Sun.COM 	if (fuidp) {
28659396SMatthew.Ahrens@Sun.COM 		zfs_fuid_info_free(fuidp);
28669396SMatthew.Ahrens@Sun.COM 		fuidp = NULL;
28679396SMatthew.Ahrens@Sun.COM 	}
28689396SMatthew.Ahrens@Sun.COM 
28699396SMatthew.Ahrens@Sun.COM 	if (err)
28709396SMatthew.Ahrens@Sun.COM 		dmu_tx_abort(tx);
28719396SMatthew.Ahrens@Sun.COM 	else
28729396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
28739396SMatthew.Ahrens@Sun.COM 
28749396SMatthew.Ahrens@Sun.COM 	if (err == ERESTART)
28759396SMatthew.Ahrens@Sun.COM 		goto top;
2876789Sahrens 
2877789Sahrens 	ZFS_EXIT(zfsvfs);
2878789Sahrens 	return (err);
2879789Sahrens }
2880789Sahrens 
28813271Smaybee typedef struct zfs_zlock {
28823271Smaybee 	krwlock_t	*zl_rwlock;	/* lock we acquired */
28833271Smaybee 	znode_t		*zl_znode;	/* znode we held */
28843271Smaybee 	struct zfs_zlock *zl_next;	/* next in list */
28853271Smaybee } zfs_zlock_t;
28863271Smaybee 
28873271Smaybee /*
28883271Smaybee  * Drop locks and release vnodes that were held by zfs_rename_lock().
28893271Smaybee  */
28903271Smaybee static void
28913271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp)
28923271Smaybee {
28933271Smaybee 	zfs_zlock_t *zl;
28943271Smaybee 
28953271Smaybee 	while ((zl = *zlpp) != NULL) {
28963271Smaybee 		if (zl->zl_znode != NULL)
28973271Smaybee 			VN_RELE(ZTOV(zl->zl_znode));
28983271Smaybee 		rw_exit(zl->zl_rwlock);
28993271Smaybee 		*zlpp = zl->zl_next;
29003271Smaybee 		kmem_free(zl, sizeof (*zl));
29013271Smaybee 	}
29023271Smaybee }
29033271Smaybee 
2904789Sahrens /*
2905789Sahrens  * Search back through the directory tree, using the ".." entries.
2906789Sahrens  * Lock each directory in the chain to prevent concurrent renames.
2907789Sahrens  * Fail any attempt to move a directory into one of its own descendants.
2908789Sahrens  * XXX - z_parent_lock can overlap with map or grow locks
2909789Sahrens  */
2910789Sahrens static int
2911789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
2912789Sahrens {
2913789Sahrens 	zfs_zlock_t	*zl;
29143638Sbillm 	znode_t		*zp = tdzp;
2915789Sahrens 	uint64_t	rootid = zp->z_zfsvfs->z_root;
2916789Sahrens 	uint64_t	*oidp = &zp->z_id;
2917789Sahrens 	krwlock_t	*rwlp = &szp->z_parent_lock;
2918789Sahrens 	krw_t		rw = RW_WRITER;
2919789Sahrens 
2920789Sahrens 	/*
2921789Sahrens 	 * First pass write-locks szp and compares to zp->z_id.
2922789Sahrens 	 * Later passes read-lock zp and compare to zp->z_parent.
2923789Sahrens 	 */
2924789Sahrens 	do {
29253271Smaybee 		if (!rw_tryenter(rwlp, rw)) {
29263271Smaybee 			/*
29273271Smaybee 			 * Another thread is renaming in this path.
29283271Smaybee 			 * Note that if we are a WRITER, we don't have any
29293271Smaybee 			 * parent_locks held yet.
29303271Smaybee 			 */
29313271Smaybee 			if (rw == RW_READER && zp->z_id > szp->z_id) {
29323271Smaybee 				/*
29333271Smaybee 				 * Drop our locks and restart
29343271Smaybee 				 */
29353271Smaybee 				zfs_rename_unlock(&zl);
29363271Smaybee 				*zlpp = NULL;
29373271Smaybee 				zp = tdzp;
29383271Smaybee 				oidp = &zp->z_id;
29393271Smaybee 				rwlp = &szp->z_parent_lock;
29403271Smaybee 				rw = RW_WRITER;
29413271Smaybee 				continue;
29423271Smaybee 			} else {
29433271Smaybee 				/*
29443271Smaybee 				 * Wait for other thread to drop its locks
29453271Smaybee 				 */
29463271Smaybee 				rw_enter(rwlp, rw);
29473271Smaybee 			}
29483271Smaybee 		}
29493271Smaybee 
2950789Sahrens 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
2951789Sahrens 		zl->zl_rwlock = rwlp;
2952789Sahrens 		zl->zl_znode = NULL;
2953789Sahrens 		zl->zl_next = *zlpp;
2954789Sahrens 		*zlpp = zl;
2955789Sahrens 
2956789Sahrens 		if (*oidp == szp->z_id)		/* We're a descendant of szp */
2957789Sahrens 			return (EINVAL);
2958789Sahrens 
2959789Sahrens 		if (*oidp == rootid)		/* We've hit the top */
2960789Sahrens 			return (0);
2961789Sahrens 
2962789Sahrens 		if (rw == RW_READER) {		/* i.e. not the first pass */
2963789Sahrens 			int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
2964789Sahrens 			if (error)
2965789Sahrens 				return (error);
2966789Sahrens 			zl->zl_znode = zp;
2967789Sahrens 		}
2968789Sahrens 		oidp = &zp->z_phys->zp_parent;
2969789Sahrens 		rwlp = &zp->z_parent_lock;
2970789Sahrens 		rw = RW_READER;
2971789Sahrens 
2972789Sahrens 	} while (zp->z_id != sdzp->z_id);
2973789Sahrens 
2974789Sahrens 	return (0);
2975789Sahrens }
2976789Sahrens 
2977789Sahrens /*
2978789Sahrens  * Move an entry from the provided source directory to the target
2979789Sahrens  * directory.  Change the entry name as indicated.
2980789Sahrens  *
2981789Sahrens  *	IN:	sdvp	- Source directory containing the "old entry".
2982789Sahrens  *		snm	- Old entry name.
2983789Sahrens  *		tdvp	- Target directory to contain the "new entry".
2984789Sahrens  *		tnm	- New entry name.
2985789Sahrens  *		cr	- credentials of caller.
29865331Samw  *		ct	- caller context
29875331Samw  *		flags	- case flags
2988789Sahrens  *
2989789Sahrens  *	RETURN:	0 if success
2990789Sahrens  *		error code if failure
2991789Sahrens  *
2992789Sahrens  * Timestamps:
2993789Sahrens  *	sdvp,tdvp - ctime|mtime updated
2994789Sahrens  */
29955331Samw /*ARGSUSED*/
2996789Sahrens static int
29975331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
29985331Samw     caller_context_t *ct, int flags)
2999789Sahrens {
3000789Sahrens 	znode_t		*tdzp, *szp, *tzp;
3001789Sahrens 	znode_t		*sdzp = VTOZ(sdvp);
3002789Sahrens 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
30035326Sek110237 	zilog_t		*zilog;
3004789Sahrens 	vnode_t		*realvp;
3005789Sahrens 	zfs_dirlock_t	*sdl, *tdl;
3006789Sahrens 	dmu_tx_t	*tx;
3007789Sahrens 	zfs_zlock_t	*zl;
30085331Samw 	int		cmp, serr, terr;
30095331Samw 	int		error = 0;
30105331Samw 	int		zflg = 0;
3011789Sahrens 
30125367Sahrens 	ZFS_ENTER(zfsvfs);
30135367Sahrens 	ZFS_VERIFY_ZP(sdzp);
30145326Sek110237 	zilog = zfsvfs->z_log;
3015789Sahrens 
3016789Sahrens 	/*
3017789Sahrens 	 * Make sure we have the real vp for the target directory.
3018789Sahrens 	 */
30195331Samw 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3020789Sahrens 		tdvp = realvp;
3021789Sahrens 
3022789Sahrens 	if (tdvp->v_vfsp != sdvp->v_vfsp) {
3023789Sahrens 		ZFS_EXIT(zfsvfs);
3024789Sahrens 		return (EXDEV);
3025789Sahrens 	}
3026789Sahrens 
3027789Sahrens 	tdzp = VTOZ(tdvp);
30285367Sahrens 	ZFS_VERIFY_ZP(tdzp);
30295498Stimh 	if (zfsvfs->z_utf8 && u8_validate(tnm,
30305331Samw 	    strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
30315331Samw 		ZFS_EXIT(zfsvfs);
30325331Samw 		return (EILSEQ);
30335331Samw 	}
30345331Samw 
30355331Samw 	if (flags & FIGNORECASE)
30365331Samw 		zflg |= ZCILOOK;
30375331Samw 
3038789Sahrens top:
3039789Sahrens 	szp = NULL;
3040789Sahrens 	tzp = NULL;
3041789Sahrens 	zl = NULL;
3042789Sahrens 
3043789Sahrens 	/*
3044789Sahrens 	 * This is to prevent the creation of links into attribute space
3045789Sahrens 	 * by renaming a linked file into/outof an attribute directory.
3046789Sahrens 	 * See the comment in zfs_link() for why this is considered bad.
3047789Sahrens 	 */
3048789Sahrens 	if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
3049789Sahrens 	    (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
3050789Sahrens 		ZFS_EXIT(zfsvfs);
3051789Sahrens 		return (EINVAL);
3052789Sahrens 	}
3053789Sahrens 
3054789Sahrens 	/*
3055789Sahrens 	 * Lock source and target directory entries.  To prevent deadlock,
3056789Sahrens 	 * a lock ordering must be defined.  We lock the directory with
3057789Sahrens 	 * the smallest object id first, or if it's a tie, the one with
3058789Sahrens 	 * the lexically first name.
3059789Sahrens 	 */
3060789Sahrens 	if (sdzp->z_id < tdzp->z_id) {
3061789Sahrens 		cmp = -1;
3062789Sahrens 	} else if (sdzp->z_id > tdzp->z_id) {
3063789Sahrens 		cmp = 1;
3064789Sahrens 	} else {
30655331Samw 		/*
30665331Samw 		 * First compare the two name arguments without
30675331Samw 		 * considering any case folding.
30685331Samw 		 */
30695331Samw 		int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
30705331Samw 
30715331Samw 		cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
30725498Stimh 		ASSERT(error == 0 || !zfsvfs->z_utf8);
3073789Sahrens 		if (cmp == 0) {
3074789Sahrens 			/*
3075789Sahrens 			 * POSIX: "If the old argument and the new argument
3076789Sahrens 			 * both refer to links to the same existing file,
3077789Sahrens 			 * the rename() function shall return successfully
3078789Sahrens 			 * and perform no other action."
3079789Sahrens 			 */
3080789Sahrens 			ZFS_EXIT(zfsvfs);
3081789Sahrens 			return (0);
3082789Sahrens 		}
30835331Samw 		/*
30845331Samw 		 * If the file system is case-folding, then we may
30855331Samw 		 * have some more checking to do.  A case-folding file
30865331Samw 		 * system is either supporting mixed case sensitivity
30875331Samw 		 * access or is completely case-insensitive.  Note
30885331Samw 		 * that the file system is always case preserving.
30895331Samw 		 *
30905331Samw 		 * In mixed sensitivity mode case sensitive behavior
30915331Samw 		 * is the default.  FIGNORECASE must be used to
30925331Samw 		 * explicitly request case insensitive behavior.
30935331Samw 		 *
30945331Samw 		 * If the source and target names provided differ only
30955331Samw 		 * by case (e.g., a request to rename 'tim' to 'Tim'),
30965331Samw 		 * we will treat this as a special case in the
30975331Samw 		 * case-insensitive mode: as long as the source name
30985331Samw 		 * is an exact match, we will allow this to proceed as
30995331Samw 		 * a name-change request.
31005331Samw 		 */
31015498Stimh 		if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
31025498Stimh 		    (zfsvfs->z_case == ZFS_CASE_MIXED &&
31035498Stimh 		    flags & FIGNORECASE)) &&
31045331Samw 		    u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
31055331Samw 		    &error) == 0) {
31065331Samw 			/*
31075331Samw 			 * case preserving rename request, require exact
31085331Samw 			 * name matches
31095331Samw 			 */
31105331Samw 			zflg |= ZCIEXACT;
31115331Samw 			zflg &= ~ZCILOOK;
31125331Samw 		}
3113789Sahrens 	}
31145331Samw 
3115789Sahrens 	if (cmp < 0) {
31165331Samw 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
31175331Samw 		    ZEXISTS | zflg, NULL, NULL);
31185331Samw 		terr = zfs_dirent_lock(&tdl,
31195331Samw 		    tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3120789Sahrens 	} else {
31215331Samw 		terr = zfs_dirent_lock(&tdl,
31225331Samw 		    tdzp, tnm, &tzp, zflg, NULL, NULL);
31235331Samw 		serr = zfs_dirent_lock(&sdl,
31245331Samw 		    sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
31255331Samw 		    NULL, NULL);
3126789Sahrens 	}
3127789Sahrens 
3128789Sahrens 	if (serr) {
3129789Sahrens 		/*
3130789Sahrens 		 * Source entry invalid or not there.
3131789Sahrens 		 */
3132789Sahrens 		if (!terr) {
3133789Sahrens 			zfs_dirent_unlock(tdl);
3134789Sahrens 			if (tzp)
3135789Sahrens 				VN_RELE(ZTOV(tzp));
3136789Sahrens 		}
3137789Sahrens 		if (strcmp(snm, "..") == 0)
3138789Sahrens 			serr = EINVAL;
3139789Sahrens 		ZFS_EXIT(zfsvfs);
3140789Sahrens 		return (serr);
3141789Sahrens 	}
3142789Sahrens 	if (terr) {
3143789Sahrens 		zfs_dirent_unlock(sdl);
3144789Sahrens 		VN_RELE(ZTOV(szp));
3145789Sahrens 		if (strcmp(tnm, "..") == 0)
3146789Sahrens 			terr = EINVAL;
3147789Sahrens 		ZFS_EXIT(zfsvfs);
3148789Sahrens 		return (terr);
3149789Sahrens 	}
3150789Sahrens 
3151789Sahrens 	/*
3152789Sahrens 	 * Must have write access at the source to remove the old entry
3153789Sahrens 	 * and write access at the target to create the new entry.
3154789Sahrens 	 * Note that if target and source are the same, this can be
3155789Sahrens 	 * done in a single check.
3156789Sahrens 	 */
3157789Sahrens 
3158789Sahrens 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3159789Sahrens 		goto out;
3160789Sahrens 
3161789Sahrens 	if (ZTOV(szp)->v_type == VDIR) {
3162789Sahrens 		/*
3163789Sahrens 		 * Check to make sure rename is valid.
3164789Sahrens 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3165789Sahrens 		 */
3166789Sahrens 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3167789Sahrens 			goto out;
3168789Sahrens 	}
3169789Sahrens 
3170789Sahrens 	/*
3171789Sahrens 	 * Does target exist?
3172789Sahrens 	 */
3173789Sahrens 	if (tzp) {
3174789Sahrens 		/*
3175789Sahrens 		 * Source and target must be the same type.
3176789Sahrens 		 */
3177789Sahrens 		if (ZTOV(szp)->v_type == VDIR) {
3178789Sahrens 			if (ZTOV(tzp)->v_type != VDIR) {
3179789Sahrens 				error = ENOTDIR;
3180789Sahrens 				goto out;
3181789Sahrens 			}
3182789Sahrens 		} else {
3183789Sahrens 			if (ZTOV(tzp)->v_type == VDIR) {
3184789Sahrens 				error = EISDIR;
3185789Sahrens 				goto out;
3186789Sahrens 			}
3187789Sahrens 		}
3188789Sahrens 		/*
3189789Sahrens 		 * POSIX dictates that when the source and target
3190789Sahrens 		 * entries refer to the same file object, rename
3191789Sahrens 		 * must do nothing and exit without error.
3192789Sahrens 		 */
3193789Sahrens 		if (szp->z_id == tzp->z_id) {
3194789Sahrens 			error = 0;
3195789Sahrens 			goto out;
3196789Sahrens 		}
3197789Sahrens 	}
3198789Sahrens 
31995331Samw 	vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3200789Sahrens 	if (tzp)
32015331Samw 		vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
32024863Spraks 
32034863Spraks 	/*
32044863Spraks 	 * notify the target directory if it is not the same
32054863Spraks 	 * as source directory.
32064863Spraks 	 */
32074863Spraks 	if (tdvp != sdvp) {
32085331Samw 		vnevent_rename_dest_dir(tdvp, ct);
32094863Spraks 	}
3210789Sahrens 
3211789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3212789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);	/* nlink changes */
3213789Sahrens 	dmu_tx_hold_bonus(tx, sdzp->z_id);	/* nlink changes */
32141544Seschrock 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
32151544Seschrock 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
32161544Seschrock 	if (sdzp != tdzp)
3217789Sahrens 		dmu_tx_hold_bonus(tx, tdzp->z_id);	/* nlink changes */
32181544Seschrock 	if (tzp)
32191544Seschrock 		dmu_tx_hold_bonus(tx, tzp->z_id);	/* parent changes */
32203461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
32218227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3222789Sahrens 	if (error) {
3223789Sahrens 		if (zl != NULL)
3224789Sahrens 			zfs_rename_unlock(&zl);
3225789Sahrens 		zfs_dirent_unlock(sdl);
3226789Sahrens 		zfs_dirent_unlock(tdl);
3227789Sahrens 		VN_RELE(ZTOV(szp));
3228789Sahrens 		if (tzp)
3229789Sahrens 			VN_RELE(ZTOV(tzp));
32308227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
32312113Sahrens 			dmu_tx_wait(tx);
32322113Sahrens 			dmu_tx_abort(tx);
3233789Sahrens 			goto top;
3234789Sahrens 		}
32352113Sahrens 		dmu_tx_abort(tx);
3236789Sahrens 		ZFS_EXIT(zfsvfs);
3237789Sahrens 		return (error);
3238789Sahrens 	}
3239789Sahrens 
3240789Sahrens 	if (tzp)	/* Attempt to remove the existing target */
32415331Samw 		error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3242789Sahrens 
3243789Sahrens 	if (error == 0) {
3244789Sahrens 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3245789Sahrens 		if (error == 0) {
32465331Samw 			szp->z_phys->zp_flags |= ZFS_AV_MODIFIED;
32475331Samw 
3248789Sahrens 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3249789Sahrens 			ASSERT(error == 0);
32505331Samw 
32515331Samw 			zfs_log_rename(zilog, tx,
32525331Samw 			    TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0),
32535331Samw 			    sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
32546976Seschrock 
32556976Seschrock 			/* Update path information for the target vnode */
32566976Seschrock 			vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm));
3257789Sahrens 		}
3258789Sahrens 	}
3259789Sahrens 
3260789Sahrens 	dmu_tx_commit(tx);
3261789Sahrens out:
3262789Sahrens 	if (zl != NULL)
3263789Sahrens 		zfs_rename_unlock(&zl);
3264789Sahrens 
3265789Sahrens 	zfs_dirent_unlock(sdl);
3266789Sahrens 	zfs_dirent_unlock(tdl);
3267789Sahrens 
3268789Sahrens 	VN_RELE(ZTOV(szp));
3269789Sahrens 	if (tzp)
3270789Sahrens 		VN_RELE(ZTOV(tzp));
3271789Sahrens 
3272789Sahrens 	ZFS_EXIT(zfsvfs);
3273789Sahrens 	return (error);
3274789Sahrens }
3275789Sahrens 
3276789Sahrens /*
3277789Sahrens  * Insert the indicated symbolic reference entry into the directory.
3278789Sahrens  *
3279789Sahrens  *	IN:	dvp	- Directory to contain new symbolic link.
3280789Sahrens  *		link	- Name for new symlink entry.
3281789Sahrens  *		vap	- Attributes of new entry.
3282789Sahrens  *		target	- Target path of new symlink.
3283789Sahrens  *		cr	- credentials of caller.
32845331Samw  *		ct	- caller context
32855331Samw  *		flags	- case flags
3286789Sahrens  *
3287789Sahrens  *	RETURN:	0 if success
3288789Sahrens  *		error code if failure
3289789Sahrens  *
3290789Sahrens  * Timestamps:
3291789Sahrens  *	dvp - ctime|mtime updated
3292789Sahrens  */
32935331Samw /*ARGSUSED*/
3294789Sahrens static int
32955331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
32965331Samw     caller_context_t *ct, int flags)
3297789Sahrens {
3298789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
3299789Sahrens 	zfs_dirlock_t	*dl;
3300789Sahrens 	dmu_tx_t	*tx;
3301789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
33025326Sek110237 	zilog_t		*zilog;
3303789Sahrens 	int		len = strlen(link);
3304789Sahrens 	int		error;
33055331Samw 	int		zflg = ZNEW;
33069179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t	acl_ids;
33079179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
3308789Sahrens 
3309789Sahrens 	ASSERT(vap->va_type == VLNK);
3310789Sahrens 
33115367Sahrens 	ZFS_ENTER(zfsvfs);
33125367Sahrens 	ZFS_VERIFY_ZP(dzp);
33135326Sek110237 	zilog = zfsvfs->z_log;
33145331Samw 
33155498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
33165331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
33175331Samw 		ZFS_EXIT(zfsvfs);
33185331Samw 		return (EILSEQ);
33195331Samw 	}
33205331Samw 	if (flags & FIGNORECASE)
33215331Samw 		zflg |= ZCILOOK;
3322789Sahrens top:
33235331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3324789Sahrens 		ZFS_EXIT(zfsvfs);
3325789Sahrens 		return (error);
3326789Sahrens 	}
3327789Sahrens 
3328789Sahrens 	if (len > MAXPATHLEN) {
3329789Sahrens 		ZFS_EXIT(zfsvfs);
3330789Sahrens 		return (ENAMETOOLONG);
3331789Sahrens 	}
3332789Sahrens 
3333789Sahrens 	/*
3334789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3335789Sahrens 	 */
33365331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
33375331Samw 	if (error) {
3338789Sahrens 		ZFS_EXIT(zfsvfs);
3339789Sahrens 		return (error);
3340789Sahrens 	}
3341789Sahrens 
33429179SMark.Shellenbaum@Sun.COM 	VERIFY(0 == zfs_acl_ids_create(dzp, 0, vap, cr, NULL, &acl_ids));
33439396SMatthew.Ahrens@Sun.COM 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
33449396SMatthew.Ahrens@Sun.COM 		zfs_acl_ids_free(&acl_ids);
33459396SMatthew.Ahrens@Sun.COM 		zfs_dirent_unlock(dl);
33469396SMatthew.Ahrens@Sun.COM 		ZFS_EXIT(zfsvfs);
33479396SMatthew.Ahrens@Sun.COM 		return (EDQUOT);
33489396SMatthew.Ahrens@Sun.COM 	}
3349789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
33509179SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
3351789Sahrens 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3352789Sahrens 	dmu_tx_hold_bonus(tx, dzp->z_id);
33531544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
33549179SMark.Shellenbaum@Sun.COM 	if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE)
3355789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
33569396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
33579396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
33588227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3359789Sahrens 	if (error) {
33609179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
3361789Sahrens 		zfs_dirent_unlock(dl);
33628227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
33632113Sahrens 			dmu_tx_wait(tx);
33642113Sahrens 			dmu_tx_abort(tx);
3365789Sahrens 			goto top;
3366789Sahrens 		}
33672113Sahrens 		dmu_tx_abort(tx);
3368789Sahrens 		ZFS_EXIT(zfsvfs);
3369789Sahrens 		return (error);
3370789Sahrens 	}
3371789Sahrens 
3372789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
3373789Sahrens 
3374789Sahrens 	/*
3375789Sahrens 	 * Create a new object for the symlink.
3376789Sahrens 	 * Put the link content into bonus buffer if it will fit;
3377789Sahrens 	 * otherwise, store it just like any other file data.
3378789Sahrens 	 */
3379789Sahrens 	if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
33809179SMark.Shellenbaum@Sun.COM 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, len, &acl_ids);
3381789Sahrens 		if (len != 0)
3382789Sahrens 			bcopy(link, zp->z_phys + 1, len);
3383789Sahrens 	} else {
3384789Sahrens 		dmu_buf_t *dbp;
33851669Sperrin 
33869179SMark.Shellenbaum@Sun.COM 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids);
33879179SMark.Shellenbaum@Sun.COM 
33889179SMark.Shellenbaum@Sun.COM 		if (fuid_dirtied)
33899179SMark.Shellenbaum@Sun.COM 			zfs_fuid_sync(zfsvfs, tx);
33901669Sperrin 		/*
33911669Sperrin 		 * Nothing can access the znode yet so no locking needed
33921669Sperrin 		 * for growing the znode's blocksize.
33931669Sperrin 		 */
33941669Sperrin 		zfs_grow_blocksize(zp, len, tx);
3395789Sahrens 
33965446Sahrens 		VERIFY(0 == dmu_buf_hold(zfsvfs->z_os,
33975446Sahrens 		    zp->z_id, 0, FTAG, &dbp));
3398789Sahrens 		dmu_buf_will_dirty(dbp, tx);
3399789Sahrens 
3400789Sahrens 		ASSERT3U(len, <=, dbp->db_size);
3401789Sahrens 		bcopy(link, dbp->db_data, len);
34021544Seschrock 		dmu_buf_rele(dbp, FTAG);
3403789Sahrens 	}
3404789Sahrens 	zp->z_phys->zp_size = len;
3405789Sahrens 
3406789Sahrens 	/*
3407789Sahrens 	 * Insert the new object into the directory.
3408789Sahrens 	 */
3409789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
34105331Samw 	if (error == 0) {
34115331Samw 		uint64_t txtype = TX_SYMLINK;
34125331Samw 		if (flags & FIGNORECASE)
34135331Samw 			txtype |= TX_CI;
34145331Samw 		zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
34155331Samw 	}
34169179SMark.Shellenbaum@Sun.COM 
34179179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_free(&acl_ids);
3418789Sahrens 
3419789Sahrens 	dmu_tx_commit(tx);
3420789Sahrens 
3421789Sahrens 	zfs_dirent_unlock(dl);
3422789Sahrens 
3423789Sahrens 	VN_RELE(ZTOV(zp));
3424789Sahrens 
3425789Sahrens 	ZFS_EXIT(zfsvfs);
3426789Sahrens 	return (error);
3427789Sahrens }
3428789Sahrens 
3429789Sahrens /*
3430789Sahrens  * Return, in the buffer contained in the provided uio structure,
3431789Sahrens  * the symbolic path referred to by vp.
3432789Sahrens  *
3433789Sahrens  *	IN:	vp	- vnode of symbolic link.
3434789Sahrens  *		uoip	- structure to contain the link path.
3435789Sahrens  *		cr	- credentials of caller.
34365331Samw  *		ct	- caller context
3437789Sahrens  *
3438789Sahrens  *	OUT:	uio	- structure to contain the link path.
3439789Sahrens  *
3440789Sahrens  *	RETURN:	0 if success
3441789Sahrens  *		error code if failure
3442789Sahrens  *
3443789Sahrens  * Timestamps:
3444789Sahrens  *	vp - atime updated
3445789Sahrens  */
3446789Sahrens /* ARGSUSED */
3447789Sahrens static int
34485331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3449789Sahrens {
3450789Sahrens 	znode_t		*zp = VTOZ(vp);
3451789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3452789Sahrens 	size_t		bufsz;
3453789Sahrens 	int		error;
3454789Sahrens 
34555367Sahrens 	ZFS_ENTER(zfsvfs);
34565367Sahrens 	ZFS_VERIFY_ZP(zp);
3457789Sahrens 
3458789Sahrens 	bufsz = (size_t)zp->z_phys->zp_size;
3459789Sahrens 	if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
3460789Sahrens 		error = uiomove(zp->z_phys + 1,
3461789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
3462789Sahrens 	} else {
34631544Seschrock 		dmu_buf_t *dbp;
34641544Seschrock 		error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
34651544Seschrock 		if (error) {
3466789Sahrens 			ZFS_EXIT(zfsvfs);
3467789Sahrens 			return (error);
3468789Sahrens 		}
3469789Sahrens 		error = uiomove(dbp->db_data,
3470789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
34711544Seschrock 		dmu_buf_rele(dbp, FTAG);
3472789Sahrens 	}
3473789Sahrens 
3474789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3475789Sahrens 	ZFS_EXIT(zfsvfs);
3476789Sahrens 	return (error);
3477789Sahrens }
3478789Sahrens 
3479789Sahrens /*
3480789Sahrens  * Insert a new entry into directory tdvp referencing svp.
3481789Sahrens  *
3482789Sahrens  *	IN:	tdvp	- Directory to contain new entry.
3483789Sahrens  *		svp	- vnode of new entry.
3484789Sahrens  *		name	- name of new entry.
3485789Sahrens  *		cr	- credentials of caller.
34865331Samw  *		ct	- caller context
3487789Sahrens  *
3488789Sahrens  *	RETURN:	0 if success
3489789Sahrens  *		error code if failure
3490789Sahrens  *
3491789Sahrens  * Timestamps:
3492789Sahrens  *	tdvp - ctime|mtime updated
3493789Sahrens  *	 svp - ctime updated
3494789Sahrens  */
3495789Sahrens /* ARGSUSED */
3496789Sahrens static int
34975331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
34985331Samw     caller_context_t *ct, int flags)
3499789Sahrens {
3500789Sahrens 	znode_t		*dzp = VTOZ(tdvp);
3501789Sahrens 	znode_t		*tzp, *szp;
3502789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
35035326Sek110237 	zilog_t		*zilog;
3504789Sahrens 	zfs_dirlock_t	*dl;
3505789Sahrens 	dmu_tx_t	*tx;
3506789Sahrens 	vnode_t		*realvp;
3507789Sahrens 	int		error;
35085331Samw 	int		zf = ZNEW;
35095331Samw 	uid_t		owner;
3510789Sahrens 
3511789Sahrens 	ASSERT(tdvp->v_type == VDIR);
3512789Sahrens 
35135367Sahrens 	ZFS_ENTER(zfsvfs);
35145367Sahrens 	ZFS_VERIFY_ZP(dzp);
35155326Sek110237 	zilog = zfsvfs->z_log;
3516789Sahrens 
35175331Samw 	if (VOP_REALVP(svp, &realvp, ct) == 0)
3518789Sahrens 		svp = realvp;
3519789Sahrens 
3520789Sahrens 	if (svp->v_vfsp != tdvp->v_vfsp) {
3521789Sahrens 		ZFS_EXIT(zfsvfs);
3522789Sahrens 		return (EXDEV);
3523789Sahrens 	}
35245367Sahrens 	szp = VTOZ(svp);
35255367Sahrens 	ZFS_VERIFY_ZP(szp);
3526789Sahrens 
35275498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name,
35285331Samw 	    strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
35295331Samw 		ZFS_EXIT(zfsvfs);
35305331Samw 		return (EILSEQ);
35315331Samw 	}
35325331Samw 	if (flags & FIGNORECASE)
35335331Samw 		zf |= ZCILOOK;
35345331Samw 
3535789Sahrens top:
3536789Sahrens 	/*
3537789Sahrens 	 * We do not support links between attributes and non-attributes
3538789Sahrens 	 * because of the potential security risk of creating links
3539789Sahrens 	 * into "normal" file space in order to circumvent restrictions
3540789Sahrens 	 * imposed in attribute space.
3541789Sahrens 	 */
3542789Sahrens 	if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
3543789Sahrens 	    (dzp->z_phys->zp_flags & ZFS_XATTR)) {
3544789Sahrens 		ZFS_EXIT(zfsvfs);
3545789Sahrens 		return (EINVAL);
3546789Sahrens 	}
3547789Sahrens 
3548789Sahrens 	/*
3549789Sahrens 	 * POSIX dictates that we return EPERM here.
3550789Sahrens 	 * Better choices include ENOTSUP or EISDIR.
3551789Sahrens 	 */
3552789Sahrens 	if (svp->v_type == VDIR) {
3553789Sahrens 		ZFS_EXIT(zfsvfs);
3554789Sahrens 		return (EPERM);
3555789Sahrens 	}
3556789Sahrens 
35575959Smarks 	owner = zfs_fuid_map_id(zfsvfs, szp->z_phys->zp_uid, cr, ZFS_OWNER);
35585331Samw 	if (owner != crgetuid(cr) &&
3559789Sahrens 	    secpolicy_basic_link(cr) != 0) {
3560789Sahrens 		ZFS_EXIT(zfsvfs);
3561789Sahrens 		return (EPERM);
3562789Sahrens 	}
3563789Sahrens 
35645331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3565789Sahrens 		ZFS_EXIT(zfsvfs);
3566789Sahrens 		return (error);
3567789Sahrens 	}
3568789Sahrens 
3569789Sahrens 	/*
3570789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3571789Sahrens 	 */
35725331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
35735331Samw 	if (error) {
3574789Sahrens 		ZFS_EXIT(zfsvfs);
3575789Sahrens 		return (error);
3576789Sahrens 	}
3577789Sahrens 
3578789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3579789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);
35801544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
35818227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3582789Sahrens 	if (error) {
3583789Sahrens 		zfs_dirent_unlock(dl);
35848227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
35852113Sahrens 			dmu_tx_wait(tx);
35862113Sahrens 			dmu_tx_abort(tx);
3587789Sahrens 			goto top;
3588789Sahrens 		}
35892113Sahrens 		dmu_tx_abort(tx);
3590789Sahrens 		ZFS_EXIT(zfsvfs);
3591789Sahrens 		return (error);
3592789Sahrens 	}
3593789Sahrens 
3594789Sahrens 	error = zfs_link_create(dl, szp, tx, 0);
3595789Sahrens 
35965331Samw 	if (error == 0) {
35975331Samw 		uint64_t txtype = TX_LINK;
35985331Samw 		if (flags & FIGNORECASE)
35995331Samw 			txtype |= TX_CI;
36005331Samw 		zfs_log_link(zilog, tx, txtype, dzp, szp, name);
36015331Samw 	}
3602789Sahrens 
3603789Sahrens 	dmu_tx_commit(tx);
3604789Sahrens 
3605789Sahrens 	zfs_dirent_unlock(dl);
3606789Sahrens 
36074863Spraks 	if (error == 0) {
36085331Samw 		vnevent_link(svp, ct);
36094863Spraks 	}
36104863Spraks 
3611789Sahrens 	ZFS_EXIT(zfsvfs);
3612789Sahrens 	return (error);
3613789Sahrens }
3614789Sahrens 
3615789Sahrens /*
3616789Sahrens  * zfs_null_putapage() is used when the file system has been force
3617789Sahrens  * unmounted. It just drops the pages.
3618789Sahrens  */
3619789Sahrens /* ARGSUSED */
3620789Sahrens static int
3621789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3622789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3623789Sahrens {
3624789Sahrens 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
3625789Sahrens 	return (0);
3626789Sahrens }
3627789Sahrens 
36282688Smaybee /*
36292688Smaybee  * Push a page out to disk, klustering if possible.
36302688Smaybee  *
36312688Smaybee  *	IN:	vp	- file to push page to.
36322688Smaybee  *		pp	- page to push.
36332688Smaybee  *		flags	- additional flags.
36342688Smaybee  *		cr	- credentials of caller.
36352688Smaybee  *
36362688Smaybee  *	OUT:	offp	- start of range pushed.
36372688Smaybee  *		lenp	- len of range pushed.
36382688Smaybee  *
36392688Smaybee  *	RETURN:	0 if success
36402688Smaybee  *		error code if failure
36412688Smaybee  *
36422688Smaybee  * NOTE: callers must have locked the page to be pushed.  On
36432688Smaybee  * exit, the page (and all other pages in the kluster) must be
36442688Smaybee  * unlocked.
36452688Smaybee  */
3646789Sahrens /* ARGSUSED */
3647789Sahrens static int
3648789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3649789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3650789Sahrens {
3651789Sahrens 	znode_t		*zp = VTOZ(vp);
3652789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3653789Sahrens 	dmu_tx_t	*tx;
36542688Smaybee 	u_offset_t	off, koff;
36552688Smaybee 	size_t		len, klen;
36564709Smaybee 	uint64_t	filesz;
3657789Sahrens 	int		err;
3658789Sahrens 
36594709Smaybee 	filesz = zp->z_phys->zp_size;
36602688Smaybee 	off = pp->p_offset;
36612688Smaybee 	len = PAGESIZE;
36622688Smaybee 	/*
36632688Smaybee 	 * If our blocksize is bigger than the page size, try to kluster
36648227SNeil.Perrin@Sun.COM 	 * multiple pages so that we write a full block (thus avoiding
36652688Smaybee 	 * a read-modify-write).
36662688Smaybee 	 */
36674709Smaybee 	if (off < filesz && zp->z_blksz > PAGESIZE) {
36688636SMark.Maybee@Sun.COM 		klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
36698636SMark.Maybee@Sun.COM 		koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
36702688Smaybee 		ASSERT(koff <= filesz);
36712688Smaybee 		if (koff + klen > filesz)
36722688Smaybee 			klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE);
36732688Smaybee 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
36742688Smaybee 	}
36752688Smaybee 	ASSERT3U(btop(len), ==, btopr(len));
36768636SMark.Maybee@Sun.COM 
36771819Smaybee 	/*
36781819Smaybee 	 * Can't push pages past end-of-file.
36791819Smaybee 	 */
36804709Smaybee 	if (off >= filesz) {
36814709Smaybee 		/* ignore all pages */
36822688Smaybee 		err = 0;
36832688Smaybee 		goto out;
36844709Smaybee 	} else if (off + len > filesz) {
36854709Smaybee 		int npages = btopr(filesz - off);
36862688Smaybee 		page_t *trunc;
36872688Smaybee 
36882688Smaybee 		page_list_break(&pp, &trunc, npages);
36894709Smaybee 		/* ignore pages past end of file */
36902688Smaybee 		if (trunc)
36914709Smaybee 			pvn_write_done(trunc, flags);
36924709Smaybee 		len = filesz - off;
36931819Smaybee 	}
36949396SMatthew.Ahrens@Sun.COM 
36959396SMatthew.Ahrens@Sun.COM 	if (zfs_usergroup_overquota(zfsvfs, B_FALSE, zp->z_phys->zp_uid) ||
36969396SMatthew.Ahrens@Sun.COM 	    zfs_usergroup_overquota(zfsvfs, B_TRUE, zp->z_phys->zp_gid)) {
36979396SMatthew.Ahrens@Sun.COM 		err = EDQUOT;
36989396SMatthew.Ahrens@Sun.COM 		goto out;
36999396SMatthew.Ahrens@Sun.COM 	}
37008636SMark.Maybee@Sun.COM top:
3701789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3702789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, off, len);
3703789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
37048227SNeil.Perrin@Sun.COM 	err = dmu_tx_assign(tx, TXG_NOWAIT);
3705789Sahrens 	if (err != 0) {
37068227SNeil.Perrin@Sun.COM 		if (err == ERESTART) {
37072113Sahrens 			dmu_tx_wait(tx);
37082113Sahrens 			dmu_tx_abort(tx);
3709789Sahrens 			goto top;
3710789Sahrens 		}
37112113Sahrens 		dmu_tx_abort(tx);
3712789Sahrens 		goto out;
3713789Sahrens 	}
3714789Sahrens 
37152688Smaybee 	if (zp->z_blksz <= PAGESIZE) {
37167315SJonathan.Adams@Sun.COM 		caddr_t va = zfs_map_page(pp, S_READ);
37172688Smaybee 		ASSERT3U(len, <=, PAGESIZE);
37182688Smaybee 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
37197315SJonathan.Adams@Sun.COM 		zfs_unmap_page(pp, va);
37202688Smaybee 	} else {
37212688Smaybee 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
37222688Smaybee 	}
37232688Smaybee 
37242688Smaybee 	if (err == 0) {
37252688Smaybee 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
37268636SMark.Maybee@Sun.COM 		zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
37272688Smaybee 		dmu_tx_commit(tx);
37282688Smaybee 	}
37292688Smaybee 
37302688Smaybee out:
37314709Smaybee 	pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
3732789Sahrens 	if (offp)
3733789Sahrens 		*offp = off;
3734789Sahrens 	if (lenp)
3735789Sahrens 		*lenp = len;
3736789Sahrens 
3737789Sahrens 	return (err);
3738789Sahrens }
3739789Sahrens 
3740789Sahrens /*
3741789Sahrens  * Copy the portion of the file indicated from pages into the file.
3742789Sahrens  * The pages are stored in a page list attached to the files vnode.
3743789Sahrens  *
3744789Sahrens  *	IN:	vp	- vnode of file to push page data to.
3745789Sahrens  *		off	- position in file to put data.
3746789Sahrens  *		len	- amount of data to write.
3747789Sahrens  *		flags	- flags to control the operation.
3748789Sahrens  *		cr	- credentials of caller.
37495331Samw  *		ct	- caller context.
3750789Sahrens  *
3751789Sahrens  *	RETURN:	0 if success
3752789Sahrens  *		error code if failure
3753789Sahrens  *
3754789Sahrens  * Timestamps:
3755789Sahrens  *	vp - ctime|mtime updated
3756789Sahrens  */
37575331Samw /*ARGSUSED*/
3758789Sahrens static int
37595331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
37605331Samw     caller_context_t *ct)
3761789Sahrens {
3762789Sahrens 	znode_t		*zp = VTOZ(vp);
3763789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3764789Sahrens 	page_t		*pp;
3765789Sahrens 	size_t		io_len;
3766789Sahrens 	u_offset_t	io_off;
37678636SMark.Maybee@Sun.COM 	uint_t		blksz;
37688636SMark.Maybee@Sun.COM 	rl_t		*rl;
3769789Sahrens 	int		error = 0;
3770789Sahrens 
37715367Sahrens 	ZFS_ENTER(zfsvfs);
37725367Sahrens 	ZFS_VERIFY_ZP(zp);
3773789Sahrens 
37748636SMark.Maybee@Sun.COM 	/*
37758636SMark.Maybee@Sun.COM 	 * Align this request to the file block size in case we kluster.
37768636SMark.Maybee@Sun.COM 	 * XXX - this can result in pretty aggresive locking, which can
37778636SMark.Maybee@Sun.COM 	 * impact simultanious read/write access.  One option might be
37788636SMark.Maybee@Sun.COM 	 * to break up long requests (len == 0) into block-by-block
37798636SMark.Maybee@Sun.COM 	 * operations to get narrower locking.
37808636SMark.Maybee@Sun.COM 	 */
37818636SMark.Maybee@Sun.COM 	blksz = zp->z_blksz;
37828636SMark.Maybee@Sun.COM 	if (ISP2(blksz))
37838636SMark.Maybee@Sun.COM 		io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
37848636SMark.Maybee@Sun.COM 	else
37858636SMark.Maybee@Sun.COM 		io_off = 0;
37868636SMark.Maybee@Sun.COM 	if (len > 0 && ISP2(blksz))
37879141SMark.Maybee@Sun.COM 		io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
37888636SMark.Maybee@Sun.COM 	else
37898636SMark.Maybee@Sun.COM 		io_len = 0;
37908636SMark.Maybee@Sun.COM 
37918636SMark.Maybee@Sun.COM 	if (io_len == 0) {
3792789Sahrens 		/*
37938636SMark.Maybee@Sun.COM 		 * Search the entire vp list for pages >= io_off.
3794789Sahrens 		 */
37958636SMark.Maybee@Sun.COM 		rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
37968636SMark.Maybee@Sun.COM 		error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
37971472Sperrin 		goto out;
3798789Sahrens 	}
37998636SMark.Maybee@Sun.COM 	rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
38008636SMark.Maybee@Sun.COM 
38018636SMark.Maybee@Sun.COM 	if (off > zp->z_phys->zp_size) {
3802789Sahrens 		/* past end of file */
38038636SMark.Maybee@Sun.COM 		zfs_range_unlock(rl);
3804789Sahrens 		ZFS_EXIT(zfsvfs);
3805789Sahrens 		return (0);
3806789Sahrens 	}
3807789Sahrens 
38088636SMark.Maybee@Sun.COM 	len = MIN(io_len, P2ROUNDUP(zp->z_phys->zp_size, PAGESIZE) - io_off);
38098636SMark.Maybee@Sun.COM 
38108636SMark.Maybee@Sun.COM 	for (off = io_off; io_off < off + len; io_off += io_len) {
3811789Sahrens 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
38121669Sperrin 			pp = page_lookup(vp, io_off,
38134339Sperrin 			    (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
3814789Sahrens 		} else {
3815789Sahrens 			pp = page_lookup_nowait(vp, io_off,
38164339Sperrin 			    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
3817789Sahrens 		}
3818789Sahrens 
3819789Sahrens 		if (pp != NULL && pvn_getdirty(pp, flags)) {
3820789Sahrens 			int err;
3821789Sahrens 
3822789Sahrens 			/*
3823789Sahrens 			 * Found a dirty page to push
3824789Sahrens 			 */
38251669Sperrin 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
38261669Sperrin 			if (err)
3827789Sahrens 				error = err;
3828789Sahrens 		} else {
3829789Sahrens 			io_len = PAGESIZE;
3830789Sahrens 		}
3831789Sahrens 	}
38321472Sperrin out:
38338636SMark.Maybee@Sun.COM 	zfs_range_unlock(rl);
38342638Sperrin 	if ((flags & B_ASYNC) == 0)
38352638Sperrin 		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
3836789Sahrens 	ZFS_EXIT(zfsvfs);
3837789Sahrens 	return (error);
3838789Sahrens }
3839789Sahrens 
38405331Samw /*ARGSUSED*/
3841789Sahrens void
38425331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
3843789Sahrens {
3844789Sahrens 	znode_t	*zp = VTOZ(vp);
3845789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3846789Sahrens 	int error;
3847789Sahrens 
38485326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
38495446Sahrens 	if (zp->z_dbuf == NULL) {
38505446Sahrens 		/*
38515642Smaybee 		 * The fs has been unmounted, or we did a
38525642Smaybee 		 * suspend/resume and this file no longer exists.
38535446Sahrens 		 */
3854789Sahrens 		if (vn_has_cached_data(vp)) {
3855789Sahrens 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
3856789Sahrens 			    B_INVAL, cr);
3857789Sahrens 		}
3858789Sahrens 
38591544Seschrock 		mutex_enter(&zp->z_lock);
3860789Sahrens 		vp->v_count = 0; /* count arrives as 1 */
38615446Sahrens 		mutex_exit(&zp->z_lock);
38625642Smaybee 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
38635446Sahrens 		zfs_znode_free(zp);
3864789Sahrens 		return;
3865789Sahrens 	}
3866789Sahrens 
3867789Sahrens 	/*
3868789Sahrens 	 * Attempt to push any data in the page cache.  If this fails
3869789Sahrens 	 * we will get kicked out later in zfs_zinactive().
3870789Sahrens 	 */
38711298Sperrin 	if (vn_has_cached_data(vp)) {
38721298Sperrin 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
38731298Sperrin 		    cr);
38741298Sperrin 	}
3875789Sahrens 
38763461Sahrens 	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3877789Sahrens 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3878789Sahrens 
3879789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
3880789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
3881789Sahrens 		if (error) {
3882789Sahrens 			dmu_tx_abort(tx);
3883789Sahrens 		} else {
3884789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
3885789Sahrens 			mutex_enter(&zp->z_lock);
3886789Sahrens 			zp->z_atime_dirty = 0;
3887789Sahrens 			mutex_exit(&zp->z_lock);
3888789Sahrens 			dmu_tx_commit(tx);
3889789Sahrens 		}
3890789Sahrens 	}
3891789Sahrens 
3892789Sahrens 	zfs_zinactive(zp);
38935326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
3894789Sahrens }
3895789Sahrens 
3896789Sahrens /*
3897789Sahrens  * Bounds-check the seek operation.
3898789Sahrens  *
3899789Sahrens  *	IN:	vp	- vnode seeking within
3900789Sahrens  *		ooff	- old file offset
3901789Sahrens  *		noffp	- pointer to new file offset
39025331Samw  *		ct	- caller context
3903789Sahrens  *
3904789Sahrens  *	RETURN:	0 if success
3905789Sahrens  *		EINVAL if new offset invalid
3906789Sahrens  */
3907789Sahrens /* ARGSUSED */
3908789Sahrens static int
39095331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
39105331Samw     caller_context_t *ct)
3911789Sahrens {
3912789Sahrens 	if (vp->v_type == VDIR)
3913789Sahrens 		return (0);
3914789Sahrens 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
3915789Sahrens }
3916789Sahrens 
3917789Sahrens /*
3918789Sahrens  * Pre-filter the generic locking function to trap attempts to place
3919789Sahrens  * a mandatory lock on a memory mapped file.
3920789Sahrens  */
3921789Sahrens static int
3922789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
39235331Samw     flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
3924789Sahrens {
3925789Sahrens 	znode_t *zp = VTOZ(vp);
3926789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3927789Sahrens 	int error;
3928789Sahrens 
39295367Sahrens 	ZFS_ENTER(zfsvfs);
39305367Sahrens 	ZFS_VERIFY_ZP(zp);
3931789Sahrens 
3932789Sahrens 	/*
39331544Seschrock 	 * We are following the UFS semantics with respect to mapcnt
39341544Seschrock 	 * here: If we see that the file is mapped already, then we will
39351544Seschrock 	 * return an error, but we don't worry about races between this
39361544Seschrock 	 * function and zfs_map().
3937789Sahrens 	 */
39381544Seschrock 	if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) {
3939789Sahrens 		ZFS_EXIT(zfsvfs);
3940789Sahrens 		return (EAGAIN);
3941789Sahrens 	}
39425331Samw 	error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct);
3943789Sahrens 	ZFS_EXIT(zfsvfs);
3944789Sahrens 	return (error);
3945789Sahrens }
3946789Sahrens 
3947789Sahrens /*
3948789Sahrens  * If we can't find a page in the cache, we will create a new page
3949789Sahrens  * and fill it with file data.  For efficiency, we may try to fill
39508636SMark.Maybee@Sun.COM  * multiple pages at once (klustering) to fill up the supplied page
39519265SMark.Maybee@Sun.COM  * list.  Note that the pages to be filled are held with an exclusive
39529265SMark.Maybee@Sun.COM  * lock to prevent access by other threads while they are being filled.
3953789Sahrens  */
3954789Sahrens static int
3955789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
3956789Sahrens     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
3957789Sahrens {
3958789Sahrens 	znode_t *zp = VTOZ(vp);
3959789Sahrens 	page_t *pp, *cur_pp;
3960789Sahrens 	objset_t *os = zp->z_zfsvfs->z_os;
3961789Sahrens 	u_offset_t io_off, total;
3962789Sahrens 	size_t io_len;
3963789Sahrens 	int err;
3964789Sahrens 
39652688Smaybee 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
39668636SMark.Maybee@Sun.COM 		/*
39678636SMark.Maybee@Sun.COM 		 * We only have a single page, don't bother klustering
39688636SMark.Maybee@Sun.COM 		 */
3969789Sahrens 		io_off = off;
3970789Sahrens 		io_len = PAGESIZE;
39719265SMark.Maybee@Sun.COM 		pp = page_create_va(vp, io_off, io_len,
39729265SMark.Maybee@Sun.COM 		    PG_EXCL | PG_WAIT, seg, addr);
3973789Sahrens 	} else {
3974789Sahrens 		/*
39758636SMark.Maybee@Sun.COM 		 * Try to find enough pages to fill the page list
3976789Sahrens 		 */
3977789Sahrens 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
39788636SMark.Maybee@Sun.COM 		    &io_len, off, plsz, 0);
3979789Sahrens 	}
3980789Sahrens 	if (pp == NULL) {
3981789Sahrens 		/*
39828636SMark.Maybee@Sun.COM 		 * The page already exists, nothing to do here.
3983789Sahrens 		 */
3984789Sahrens 		*pl = NULL;
3985789Sahrens 		return (0);
3986789Sahrens 	}
3987789Sahrens 
3988789Sahrens 	/*
3989789Sahrens 	 * Fill the pages in the kluster.
3990789Sahrens 	 */
3991789Sahrens 	cur_pp = pp;
3992789Sahrens 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
39938636SMark.Maybee@Sun.COM 		caddr_t va;
39948636SMark.Maybee@Sun.COM 
39952688Smaybee 		ASSERT3U(io_off, ==, cur_pp->p_offset);
39967315SJonathan.Adams@Sun.COM 		va = zfs_map_page(cur_pp, S_WRITE);
39979512SNeil.Perrin@Sun.COM 		err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
39989512SNeil.Perrin@Sun.COM 		    DMU_READ_PREFETCH);
39997315SJonathan.Adams@Sun.COM 		zfs_unmap_page(cur_pp, va);
4000789Sahrens 		if (err) {
4001789Sahrens 			/* On error, toss the entire kluster */
4002789Sahrens 			pvn_read_done(pp, B_ERROR);
40037294Sperrin 			/* convert checksum errors into IO errors */
40047294Sperrin 			if (err == ECKSUM)
40057294Sperrin 				err = EIO;
4006789Sahrens 			return (err);
4007789Sahrens 		}
4008789Sahrens 		cur_pp = cur_pp->p_next;
4009789Sahrens 	}
40108636SMark.Maybee@Sun.COM 
4011789Sahrens 	/*
40128636SMark.Maybee@Sun.COM 	 * Fill in the page list array from the kluster starting
40138636SMark.Maybee@Sun.COM 	 * from the desired offset `off'.
4014789Sahrens 	 * NOTE: the page list will always be null terminated.
4015789Sahrens 	 */
4016789Sahrens 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
40178636SMark.Maybee@Sun.COM 	ASSERT(pl == NULL || (*pl)->p_offset == off);
4018789Sahrens 
4019789Sahrens 	return (0);
4020789Sahrens }
4021789Sahrens 
4022789Sahrens /*
4023789Sahrens  * Return pointers to the pages for the file region [off, off + len]
4024789Sahrens  * in the pl array.  If plsz is greater than len, this function may
40258636SMark.Maybee@Sun.COM  * also return page pointers from after the specified region
40268636SMark.Maybee@Sun.COM  * (i.e. the region [off, off + plsz]).  These additional pages are
40278636SMark.Maybee@Sun.COM  * only returned if they are already in the cache, or were created as
40288636SMark.Maybee@Sun.COM  * part of a klustered read.
4029789Sahrens  *
4030789Sahrens  *	IN:	vp	- vnode of file to get data from.
4031789Sahrens  *		off	- position in file to get data from.
4032789Sahrens  *		len	- amount of data to retrieve.
4033789Sahrens  *		plsz	- length of provided page list.
4034789Sahrens  *		seg	- segment to obtain pages for.
4035789Sahrens  *		addr	- virtual address of fault.
4036789Sahrens  *		rw	- mode of created pages.
4037789Sahrens  *		cr	- credentials of caller.
40385331Samw  *		ct	- caller context.
4039789Sahrens  *
4040789Sahrens  *	OUT:	protp	- protection mode of created pages.
4041789Sahrens  *		pl	- list of pages created.
4042789Sahrens  *
4043789Sahrens  *	RETURN:	0 if success
4044789Sahrens  *		error code if failure
4045789Sahrens  *
4046789Sahrens  * Timestamps:
4047789Sahrens  *	vp - atime updated
4048789Sahrens  */
4049789Sahrens /* ARGSUSED */
4050789Sahrens static int
4051789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4052789Sahrens 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
40535331Samw 	enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4054789Sahrens {
4055789Sahrens 	znode_t		*zp = VTOZ(vp);
4056789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
40578636SMark.Maybee@Sun.COM 	page_t		**pl0 = pl;
40588636SMark.Maybee@Sun.COM 	int		err = 0;
40598636SMark.Maybee@Sun.COM 
40608636SMark.Maybee@Sun.COM 	/* we do our own caching, faultahead is unnecessary */
40618636SMark.Maybee@Sun.COM 	if (pl == NULL)
40628636SMark.Maybee@Sun.COM 		return (0);
40638636SMark.Maybee@Sun.COM 	else if (len > plsz)
40648636SMark.Maybee@Sun.COM 		len = plsz;
40658681SMark.Maybee@Sun.COM 	else
40668681SMark.Maybee@Sun.COM 		len = P2ROUNDUP(len, PAGESIZE);
40678636SMark.Maybee@Sun.COM 	ASSERT(plsz >= len);
4068789Sahrens 
40695367Sahrens 	ZFS_ENTER(zfsvfs);
40705367Sahrens 	ZFS_VERIFY_ZP(zp);
4071789Sahrens 
4072789Sahrens 	if (protp)
4073789Sahrens 		*protp = PROT_ALL;
4074789Sahrens 
4075789Sahrens 	/*
40769265SMark.Maybee@Sun.COM 	 * Loop through the requested range [off, off + len) looking
4077789Sahrens 	 * for pages.  If we don't find a page, we will need to create
4078789Sahrens 	 * a new page and fill it with data from the file.
4079789Sahrens 	 */
4080789Sahrens 	while (len > 0) {
40818636SMark.Maybee@Sun.COM 		if (*pl = page_lookup(vp, off, SE_SHARED))
40828636SMark.Maybee@Sun.COM 			*(pl+1) = NULL;
40838636SMark.Maybee@Sun.COM 		else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
40848636SMark.Maybee@Sun.COM 			goto out;
40858636SMark.Maybee@Sun.COM 		while (*pl) {
40868636SMark.Maybee@Sun.COM 			ASSERT3U((*pl)->p_offset, ==, off);
4087789Sahrens 			off += PAGESIZE;
4088789Sahrens 			addr += PAGESIZE;
40898681SMark.Maybee@Sun.COM 			if (len > 0) {
40908681SMark.Maybee@Sun.COM 				ASSERT3U(len, >=, PAGESIZE);
40918636SMark.Maybee@Sun.COM 				len -= PAGESIZE;
40928681SMark.Maybee@Sun.COM 			}
40938636SMark.Maybee@Sun.COM 			ASSERT3U(plsz, >=, PAGESIZE);
4094789Sahrens 			plsz -= PAGESIZE;
40958636SMark.Maybee@Sun.COM 			pl++;
4096789Sahrens 		}
4097789Sahrens 	}
4098789Sahrens 
4099789Sahrens 	/*
4100789Sahrens 	 * Fill out the page array with any pages already in the cache.
4101789Sahrens 	 */
41028636SMark.Maybee@Sun.COM 	while (plsz > 0 &&
41038636SMark.Maybee@Sun.COM 	    (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
41048636SMark.Maybee@Sun.COM 			off += PAGESIZE;
41058636SMark.Maybee@Sun.COM 			plsz -= PAGESIZE;
4106789Sahrens 	}
4107789Sahrens out:
41082752Sperrin 	if (err) {
41092752Sperrin 		/*
41102752Sperrin 		 * Release any pages we have previously locked.
41112752Sperrin 		 */
41122752Sperrin 		while (pl > pl0)
41132752Sperrin 			page_unlock(*--pl);
41148636SMark.Maybee@Sun.COM 	} else {
41158636SMark.Maybee@Sun.COM 		ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
41162752Sperrin 	}
41172752Sperrin 
4118789Sahrens 	*pl = NULL;
4119789Sahrens 
4120789Sahrens 	ZFS_EXIT(zfsvfs);
4121789Sahrens 	return (err);
4122789Sahrens }
4123789Sahrens 
41241544Seschrock /*
41251544Seschrock  * Request a memory map for a section of a file.  This code interacts
41261544Seschrock  * with common code and the VM system as follows:
41271544Seschrock  *
41281544Seschrock  *	common code calls mmap(), which ends up in smmap_common()
41291544Seschrock  *
41301544Seschrock  *	this calls VOP_MAP(), which takes you into (say) zfs
41311544Seschrock  *
41321544Seschrock  *	zfs_map() calls as_map(), passing segvn_create() as the callback
41331544Seschrock  *
41341544Seschrock  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
41351544Seschrock  *
41361544Seschrock  *	zfs_addmap() updates z_mapcnt
41371544Seschrock  */
41385331Samw /*ARGSUSED*/
4139789Sahrens static int
4140789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
41415331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
41425331Samw     caller_context_t *ct)
4143789Sahrens {
4144789Sahrens 	znode_t *zp = VTOZ(vp);
4145789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4146789Sahrens 	segvn_crargs_t	vn_a;
4147789Sahrens 	int		error;
4148789Sahrens 
41495929Smarks 	ZFS_ENTER(zfsvfs);
41505929Smarks 	ZFS_VERIFY_ZP(zp);
41515929Smarks 
41525331Samw 	if ((prot & PROT_WRITE) &&
41535331Samw 	    (zp->z_phys->zp_flags & (ZFS_IMMUTABLE | ZFS_READONLY |
41545929Smarks 	    ZFS_APPENDONLY))) {
41555929Smarks 		ZFS_EXIT(zfsvfs);
41565331Samw 		return (EPERM);
41575929Smarks 	}
41585929Smarks 
41595929Smarks 	if ((prot & (PROT_READ | PROT_EXEC)) &&
41605929Smarks 	    (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED)) {
41615929Smarks 		ZFS_EXIT(zfsvfs);
41625929Smarks 		return (EACCES);
41635929Smarks 	}
4164789Sahrens 
4165789Sahrens 	if (vp->v_flag & VNOMAP) {
4166789Sahrens 		ZFS_EXIT(zfsvfs);
4167789Sahrens 		return (ENOSYS);
4168789Sahrens 	}
4169789Sahrens 
4170789Sahrens 	if (off < 0 || len > MAXOFFSET_T - off) {
4171789Sahrens 		ZFS_EXIT(zfsvfs);
4172789Sahrens 		return (ENXIO);
4173789Sahrens 	}
4174789Sahrens 
4175789Sahrens 	if (vp->v_type != VREG) {
4176789Sahrens 		ZFS_EXIT(zfsvfs);
4177789Sahrens 		return (ENODEV);
4178789Sahrens 	}
4179789Sahrens 
4180789Sahrens 	/*
4181789Sahrens 	 * If file is locked, disallow mapping.
4182789Sahrens 	 */
41831544Seschrock 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) {
41841544Seschrock 		ZFS_EXIT(zfsvfs);
41851544Seschrock 		return (EAGAIN);
4186789Sahrens 	}
4187789Sahrens 
4188789Sahrens 	as_rangelock(as);
41896036Smec 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
41906036Smec 	if (error != 0) {
41916036Smec 		as_rangeunlock(as);
41926036Smec 		ZFS_EXIT(zfsvfs);
41936036Smec 		return (error);
4194789Sahrens 	}
4195789Sahrens 
4196789Sahrens 	vn_a.vp = vp;
4197789Sahrens 	vn_a.offset = (u_offset_t)off;
4198789Sahrens 	vn_a.type = flags & MAP_TYPE;
4199789Sahrens 	vn_a.prot = prot;
4200789Sahrens 	vn_a.maxprot = maxprot;
4201789Sahrens 	vn_a.cred = cr;
4202789Sahrens 	vn_a.amp = NULL;
4203789Sahrens 	vn_a.flags = flags & ~MAP_TYPE;
42041417Skchow 	vn_a.szc = 0;
42051417Skchow 	vn_a.lgrp_mem_policy_flags = 0;
4206789Sahrens 
4207789Sahrens 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
4208789Sahrens 
4209789Sahrens 	as_rangeunlock(as);
4210789Sahrens 	ZFS_EXIT(zfsvfs);
4211789Sahrens 	return (error);
4212789Sahrens }
4213789Sahrens 
4214789Sahrens /* ARGSUSED */
4215789Sahrens static int
4216789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
42175331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
42185331Samw     caller_context_t *ct)
4219789Sahrens {
42201544Seschrock 	uint64_t pages = btopr(len);
42211544Seschrock 
42221544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4223789Sahrens 	return (0);
4224789Sahrens }
4225789Sahrens 
42261773Seschrock /*
42271773Seschrock  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
42281773Seschrock  * more accurate mtime for the associated file.  Since we don't have a way of
42291773Seschrock  * detecting when the data was actually modified, we have to resort to
42301773Seschrock  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
42311773Seschrock  * last page is pushed.  The problem occurs when the msync() call is omitted,
42321773Seschrock  * which by far the most common case:
42331773Seschrock  *
42341773Seschrock  * 	open()
42351773Seschrock  * 	mmap()
42361773Seschrock  * 	<modify memory>
42371773Seschrock  * 	munmap()
42381773Seschrock  * 	close()
42391773Seschrock  * 	<time lapse>
42401773Seschrock  * 	putpage() via fsflush
42411773Seschrock  *
42421773Seschrock  * If we wait until fsflush to come along, we can have a modification time that
42431773Seschrock  * is some arbitrary point in the future.  In order to prevent this in the
42441773Seschrock  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
42451773Seschrock  * torn down.
42461773Seschrock  */
4247789Sahrens /* ARGSUSED */
4248789Sahrens static int
4249789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
42505331Samw     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
42515331Samw     caller_context_t *ct)
4252789Sahrens {
42531544Seschrock 	uint64_t pages = btopr(len);
42541544Seschrock 
42551544Seschrock 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
42561544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
42571773Seschrock 
42581773Seschrock 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
42591773Seschrock 	    vn_has_cached_data(vp))
42605331Samw 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
42611773Seschrock 
4262789Sahrens 	return (0);
4263789Sahrens }
4264789Sahrens 
4265789Sahrens /*
4266789Sahrens  * Free or allocate space in a file.  Currently, this function only
4267789Sahrens  * supports the `F_FREESP' command.  However, this command is somewhat
4268789Sahrens  * misnamed, as its functionality includes the ability to allocate as
4269789Sahrens  * well as free space.
4270789Sahrens  *
4271789Sahrens  *	IN:	vp	- vnode of file to free data in.
4272789Sahrens  *		cmd	- action to take (only F_FREESP supported).
4273789Sahrens  *		bfp	- section of file to free/alloc.
4274789Sahrens  *		flag	- current file open mode flags.
4275789Sahrens  *		offset	- current file offset.
4276789Sahrens  *		cr	- credentials of caller [UNUSED].
42775331Samw  *		ct	- caller context.
4278789Sahrens  *
4279789Sahrens  *	RETURN:	0 if success
4280789Sahrens  *		error code if failure
4281789Sahrens  *
4282789Sahrens  * Timestamps:
4283789Sahrens  *	vp - ctime|mtime updated
4284789Sahrens  */
4285789Sahrens /* ARGSUSED */
4286789Sahrens static int
4287789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4288789Sahrens     offset_t offset, cred_t *cr, caller_context_t *ct)
4289789Sahrens {
4290789Sahrens 	znode_t		*zp = VTOZ(vp);
4291789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4292789Sahrens 	uint64_t	off, len;
4293789Sahrens 	int		error;
4294789Sahrens 
42955367Sahrens 	ZFS_ENTER(zfsvfs);
42965367Sahrens 	ZFS_VERIFY_ZP(zp);
4297789Sahrens 
4298789Sahrens 	if (cmd != F_FREESP) {
4299789Sahrens 		ZFS_EXIT(zfsvfs);
4300789Sahrens 		return (EINVAL);
4301789Sahrens 	}
4302789Sahrens 
4303789Sahrens 	if (error = convoff(vp, bfp, 0, offset)) {
4304789Sahrens 		ZFS_EXIT(zfsvfs);
4305789Sahrens 		return (error);
4306789Sahrens 	}
4307789Sahrens 
4308789Sahrens 	if (bfp->l_len < 0) {
4309789Sahrens 		ZFS_EXIT(zfsvfs);
4310789Sahrens 		return (EINVAL);
4311789Sahrens 	}
4312789Sahrens 
4313789Sahrens 	off = bfp->l_start;
43141669Sperrin 	len = bfp->l_len; /* 0 means from off to end of file */
43151878Smaybee 
43166992Smaybee 	error = zfs_freesp(zp, off, len, flag, TRUE);
4317789Sahrens 
4318789Sahrens 	ZFS_EXIT(zfsvfs);
4319789Sahrens 	return (error);
4320789Sahrens }
4321789Sahrens 
43225331Samw /*ARGSUSED*/
4323789Sahrens static int
43245331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4325789Sahrens {
4326789Sahrens 	znode_t		*zp = VTOZ(vp);
4327789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
43285326Sek110237 	uint32_t	gen;
4329789Sahrens 	uint64_t	object = zp->z_id;
4330789Sahrens 	zfid_short_t	*zfid;
4331789Sahrens 	int		size, i;
4332789Sahrens 
43335367Sahrens 	ZFS_ENTER(zfsvfs);
43345367Sahrens 	ZFS_VERIFY_ZP(zp);
43355326Sek110237 	gen = (uint32_t)zp->z_gen;
4336789Sahrens 
4337789Sahrens 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4338789Sahrens 	if (fidp->fid_len < size) {
4339789Sahrens 		fidp->fid_len = size;
43401512Sek110237 		ZFS_EXIT(zfsvfs);
4341789Sahrens 		return (ENOSPC);
4342789Sahrens 	}
4343789Sahrens 
4344789Sahrens 	zfid = (zfid_short_t *)fidp;
4345789Sahrens 
4346789Sahrens 	zfid->zf_len = size;
4347789Sahrens 
4348789Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
4349789Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4350789Sahrens 
4351789Sahrens 	/* Must have a non-zero generation number to distinguish from .zfs */
4352789Sahrens 	if (gen == 0)
4353789Sahrens 		gen = 1;
4354789Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
4355789Sahrens 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4356789Sahrens 
4357789Sahrens 	if (size == LONG_FID_LEN) {
4358789Sahrens 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
4359789Sahrens 		zfid_long_t	*zlfid;
4360789Sahrens 
4361789Sahrens 		zlfid = (zfid_long_t *)fidp;
4362789Sahrens 
4363789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4364789Sahrens 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4365789Sahrens 
4366789Sahrens 		/* XXX - this should be the generation number for the objset */
4367789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4368789Sahrens 			zlfid->zf_setgen[i] = 0;
4369789Sahrens 	}
4370789Sahrens 
4371789Sahrens 	ZFS_EXIT(zfsvfs);
4372789Sahrens 	return (0);
4373789Sahrens }
4374789Sahrens 
4375789Sahrens static int
43765331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
43775331Samw     caller_context_t *ct)
4378789Sahrens {
4379789Sahrens 	znode_t		*zp, *xzp;
4380789Sahrens 	zfsvfs_t	*zfsvfs;
4381789Sahrens 	zfs_dirlock_t	*dl;
4382789Sahrens 	int		error;
4383789Sahrens 
4384789Sahrens 	switch (cmd) {
4385789Sahrens 	case _PC_LINK_MAX:
4386789Sahrens 		*valp = ULONG_MAX;
4387789Sahrens 		return (0);
4388789Sahrens 
4389789Sahrens 	case _PC_FILESIZEBITS:
4390789Sahrens 		*valp = 64;
4391789Sahrens 		return (0);
4392789Sahrens 
4393789Sahrens 	case _PC_XATTR_EXISTS:
4394789Sahrens 		zp = VTOZ(vp);
4395789Sahrens 		zfsvfs = zp->z_zfsvfs;
43965367Sahrens 		ZFS_ENTER(zfsvfs);
43975367Sahrens 		ZFS_VERIFY_ZP(zp);
4398789Sahrens 		*valp = 0;
4399789Sahrens 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
44005331Samw 		    ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4401789Sahrens 		if (error == 0) {
4402789Sahrens 			zfs_dirent_unlock(dl);
4403789Sahrens 			if (!zfs_dirempty(xzp))
4404789Sahrens 				*valp = 1;
4405789Sahrens 			VN_RELE(ZTOV(xzp));
4406789Sahrens 		} else if (error == ENOENT) {
4407789Sahrens 			/*
4408789Sahrens 			 * If there aren't extended attributes, it's the
4409789Sahrens 			 * same as having zero of them.
4410789Sahrens 			 */
4411789Sahrens 			error = 0;
4412789Sahrens 		}
4413789Sahrens 		ZFS_EXIT(zfsvfs);
4414789Sahrens 		return (error);
4415789Sahrens 
44165331Samw 	case _PC_SATTR_ENABLED:
44175331Samw 	case _PC_SATTR_EXISTS:
44187757SJanice.Chang@Sun.COM 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
44195331Samw 		    (vp->v_type == VREG || vp->v_type == VDIR);
44205331Samw 		return (0);
44215331Samw 
44229749STim.Haley@Sun.COM 	case _PC_ACCESS_FILTERING:
44239749STim.Haley@Sun.COM 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
44249749STim.Haley@Sun.COM 		    vp->v_type == VDIR;
44259749STim.Haley@Sun.COM 		return (0);
44269749STim.Haley@Sun.COM 
4427789Sahrens 	case _PC_ACL_ENABLED:
4428789Sahrens 		*valp = _ACL_ACE_ENABLED;
4429789Sahrens 		return (0);
4430789Sahrens 
4431789Sahrens 	case _PC_MIN_HOLE_SIZE:
4432789Sahrens 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
4433789Sahrens 		return (0);
4434789Sahrens 
4435789Sahrens 	default:
44365331Samw 		return (fs_pathconf(vp, cmd, valp, cr, ct));
4437789Sahrens 	}
4438789Sahrens }
4439789Sahrens 
4440789Sahrens /*ARGSUSED*/
4441789Sahrens static int
44425331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
44435331Samw     caller_context_t *ct)
4444789Sahrens {
4445789Sahrens 	znode_t *zp = VTOZ(vp);
4446789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4447789Sahrens 	int error;
44485331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4449789Sahrens 
44505367Sahrens 	ZFS_ENTER(zfsvfs);
44515367Sahrens 	ZFS_VERIFY_ZP(zp);
44525331Samw 	error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4453789Sahrens 	ZFS_EXIT(zfsvfs);
4454789Sahrens 
4455789Sahrens 	return (error);
4456789Sahrens }
4457789Sahrens 
4458789Sahrens /*ARGSUSED*/
4459789Sahrens static int
44605331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
44615331Samw     caller_context_t *ct)
4462789Sahrens {
4463789Sahrens 	znode_t *zp = VTOZ(vp);
4464789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4465789Sahrens 	int error;
44665331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4467789Sahrens 
44685367Sahrens 	ZFS_ENTER(zfsvfs);
44695367Sahrens 	ZFS_VERIFY_ZP(zp);
44705331Samw 	error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4471789Sahrens 	ZFS_EXIT(zfsvfs);
4472789Sahrens 	return (error);
4473789Sahrens }
4474789Sahrens 
4475789Sahrens /*
4476789Sahrens  * Predeclare these here so that the compiler assumes that
4477789Sahrens  * this is an "old style" function declaration that does
4478789Sahrens  * not include arguments => we won't get type mismatch errors
4479789Sahrens  * in the initializations that follow.
4480789Sahrens  */
4481789Sahrens static int zfs_inval();
4482789Sahrens static int zfs_isdir();
4483789Sahrens 
4484789Sahrens static int
4485789Sahrens zfs_inval()
4486789Sahrens {
4487789Sahrens 	return (EINVAL);
4488789Sahrens }
4489789Sahrens 
4490789Sahrens static int
4491789Sahrens zfs_isdir()
4492789Sahrens {
4493789Sahrens 	return (EISDIR);
4494789Sahrens }
4495789Sahrens /*
4496789Sahrens  * Directory vnode operations template
4497789Sahrens  */
4498789Sahrens vnodeops_t *zfs_dvnodeops;
4499789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = {
45003898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
45013898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
45023898Srsb 	VOPNAME_READ,		{ .error = zfs_isdir },
45033898Srsb 	VOPNAME_WRITE,		{ .error = zfs_isdir },
45043898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
45053898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
45063898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
45073898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
45083898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
45093898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
45103898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
45113898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
45123898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
45133898Srsb 	VOPNAME_MKDIR,		{ .vop_mkdir = zfs_mkdir },
45143898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
45153898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
45163898Srsb 	VOPNAME_SYMLINK,	{ .vop_symlink = zfs_symlink },
45173898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
45183898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
45193898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
45203898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
45213898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
45223898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
45233898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
45244863Spraks 	VOPNAME_VNEVENT, 	{ .vop_vnevent = fs_vnevent_support },
45253898Srsb 	NULL,			NULL
4526789Sahrens };
4527789Sahrens 
4528789Sahrens /*
4529789Sahrens  * Regular file vnode operations template
4530789Sahrens  */
4531789Sahrens vnodeops_t *zfs_fvnodeops;
4532789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = {
45333898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
45343898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
45353898Srsb 	VOPNAME_READ,		{ .vop_read = zfs_read },
45363898Srsb 	VOPNAME_WRITE,		{ .vop_write = zfs_write },
45373898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
45383898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
45393898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
45403898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
45413898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
45423898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
45433898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
45443898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
45453898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
45463898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
45473898Srsb 	VOPNAME_FRLOCK,		{ .vop_frlock = zfs_frlock },
45483898Srsb 	VOPNAME_SPACE,		{ .vop_space = zfs_space },
45493898Srsb 	VOPNAME_GETPAGE,	{ .vop_getpage = zfs_getpage },
45503898Srsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = zfs_putpage },
45513898Srsb 	VOPNAME_MAP,		{ .vop_map = zfs_map },
45523898Srsb 	VOPNAME_ADDMAP,		{ .vop_addmap = zfs_addmap },
45533898Srsb 	VOPNAME_DELMAP,		{ .vop_delmap = zfs_delmap },
45543898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
45553898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
45563898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
45573898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
45583898Srsb 	NULL,			NULL
4559789Sahrens };
4560789Sahrens 
4561789Sahrens /*
4562789Sahrens  * Symbolic link vnode operations template
4563789Sahrens  */
4564789Sahrens vnodeops_t *zfs_symvnodeops;
4565789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = {
45663898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
45673898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
45683898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
45693898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
45703898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = zfs_readlink },
45713898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
45723898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
45733898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
45743898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
45753898Srsb 	NULL,			NULL
4576789Sahrens };
4577789Sahrens 
4578789Sahrens /*
45798845Samw@Sun.COM  * special share hidden files vnode operations template
45808845Samw@Sun.COM  */
45818845Samw@Sun.COM vnodeops_t *zfs_sharevnodeops;
45828845Samw@Sun.COM const fs_operation_def_t zfs_sharevnodeops_template[] = {
45838845Samw@Sun.COM 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
45848845Samw@Sun.COM 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
45858845Samw@Sun.COM 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
45868845Samw@Sun.COM 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
45878845Samw@Sun.COM 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
45888845Samw@Sun.COM 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
45898845Samw@Sun.COM 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
45908845Samw@Sun.COM 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
45918845Samw@Sun.COM 	NULL,			NULL
45928845Samw@Sun.COM };
45938845Samw@Sun.COM 
45948845Samw@Sun.COM /*
4595789Sahrens  * Extended attribute directory vnode operations template
4596789Sahrens  *	This template is identical to the directory vnodes
4597789Sahrens  *	operation template except for restricted operations:
4598789Sahrens  *		VOP_MKDIR()
4599789Sahrens  *		VOP_SYMLINK()
4600789Sahrens  * Note that there are other restrictions embedded in:
4601789Sahrens  *	zfs_create()	- restrict type to VREG
4602789Sahrens  *	zfs_link()	- no links into/out of attribute space
4603789Sahrens  *	zfs_rename()	- no moves into/out of attribute space
4604789Sahrens  */
4605789Sahrens vnodeops_t *zfs_xdvnodeops;
4606789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = {
46073898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
46083898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
46093898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
46103898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
46113898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
46123898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
46133898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
46143898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
46153898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
46163898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
46173898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
46183898Srsb 	VOPNAME_MKDIR,		{ .error = zfs_inval },
46193898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
46203898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
46213898Srsb 	VOPNAME_SYMLINK,	{ .error = zfs_inval },
46223898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
46233898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
46243898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
46253898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
46263898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
46273898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
46283898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
46293898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
46303898Srsb 	NULL,			NULL
4631789Sahrens };
4632789Sahrens 
4633789Sahrens /*
4634789Sahrens  * Error vnode operations template
4635789Sahrens  */
4636789Sahrens vnodeops_t *zfs_evnodeops;
4637789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = {
46383898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
46393898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
46403898Srsb 	NULL,			NULL
4641789Sahrens };
4642