xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 9396:f41cf682d0d3)
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 
2117844SMark.Shellenbaum@Sun.COM 	ZFS_ENTER(zfsvfs);
2127844SMark.Shellenbaum@Sun.COM 	ZFS_VERIFY_ZP(zp);
2133063Sperrin 
2143063Sperrin 	/* Decrement the synchronous opens in the znode */
2154339Sperrin 	if ((flag & (FSYNC | FDSYNC)) && (count == 1))
2163063Sperrin 		atomic_dec_32(&zp->z_sync_cnt);
2173063Sperrin 
218789Sahrens 	/*
219789Sahrens 	 * Clean up any locks held by this process on the vp.
220789Sahrens 	 */
221789Sahrens 	cleanlocks(vp, ddi_get_pid(), 0);
222789Sahrens 	cleanshares(vp, ddi_get_pid());
223789Sahrens 
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);
3678636SMark.Maybee@Sun.COM 			(void) dmu_read(os, oid, start+off, nbytes, va+off);
3687315SJonathan.Adams@Sun.COM 			zfs_unmap_page(pp, va);
369789Sahrens 			page_unlock(pp);
370789Sahrens 		}
3718636SMark.Maybee@Sun.COM 		len -= nbytes;
372789Sahrens 		off = 0;
373789Sahrens 	}
374789Sahrens }
375789Sahrens 
376789Sahrens /*
377789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
378789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
379789Sahrens  *
380789Sahrens  * On Read:	We "read" preferentially from memory mapped pages,
381789Sahrens  *		else we default from the dmu buffer.
382789Sahrens  *
383789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
384789Sahrens  *	the file is memory mapped.
385789Sahrens  */
386789Sahrens static int
3873638Sbillm mappedread(vnode_t *vp, int nbytes, uio_t *uio)
388789Sahrens {
3893638Sbillm 	znode_t *zp = VTOZ(vp);
3903638Sbillm 	objset_t *os = zp->z_zfsvfs->z_os;
3913638Sbillm 	int64_t	start, off;
392789Sahrens 	int len = nbytes;
393789Sahrens 	int error = 0;
394789Sahrens 
395789Sahrens 	start = uio->uio_loffset;
396789Sahrens 	off = start & PAGEOFFSET;
397789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
398789Sahrens 		page_t *pp;
3993638Sbillm 		uint64_t bytes = MIN(PAGESIZE - off, len);
4003638Sbillm 
401789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
402789Sahrens 			caddr_t va;
403789Sahrens 
4047315SJonathan.Adams@Sun.COM 			va = zfs_map_page(pp, S_READ);
405789Sahrens 			error = uiomove(va + off, bytes, UIO_READ, uio);
4067315SJonathan.Adams@Sun.COM 			zfs_unmap_page(pp, va);
407789Sahrens 			page_unlock(pp);
408789Sahrens 		} else {
4093638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, bytes);
410789Sahrens 		}
411789Sahrens 		len -= bytes;
412789Sahrens 		off = 0;
413789Sahrens 		if (error)
414789Sahrens 			break;
415789Sahrens 	}
416789Sahrens 	return (error);
417789Sahrens }
418789Sahrens 
4193638Sbillm offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
420789Sahrens 
421789Sahrens /*
422789Sahrens  * Read bytes from specified file into supplied buffer.
423789Sahrens  *
424789Sahrens  *	IN:	vp	- vnode of file to be read from.
425789Sahrens  *		uio	- structure supplying read location, range info,
426789Sahrens  *			  and return buffer.
427789Sahrens  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
428789Sahrens  *		cr	- credentials of caller.
4295331Samw  *		ct	- caller context
430789Sahrens  *
431789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
432789Sahrens  *
433789Sahrens  *	RETURN:	0 if success
434789Sahrens  *		error code if failure
435789Sahrens  *
436789Sahrens  * Side Effects:
437789Sahrens  *	vp - atime updated if byte count > 0
438789Sahrens  */
439789Sahrens /* ARGSUSED */
440789Sahrens static int
441789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
442789Sahrens {
443789Sahrens 	znode_t		*zp = VTOZ(vp);
444789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4455326Sek110237 	objset_t	*os;
4463638Sbillm 	ssize_t		n, nbytes;
4473638Sbillm 	int		error;
4481669Sperrin 	rl_t		*rl;
449789Sahrens 
4505367Sahrens 	ZFS_ENTER(zfsvfs);
4515367Sahrens 	ZFS_VERIFY_ZP(zp);
4525326Sek110237 	os = zfsvfs->z_os;
453789Sahrens 
4545929Smarks 	if (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) {
4555929Smarks 		ZFS_EXIT(zfsvfs);
4565929Smarks 		return (EACCES);
4575929Smarks 	}
4585929Smarks 
459789Sahrens 	/*
460789Sahrens 	 * Validate file offset
461789Sahrens 	 */
462789Sahrens 	if (uio->uio_loffset < (offset_t)0) {
463789Sahrens 		ZFS_EXIT(zfsvfs);
464789Sahrens 		return (EINVAL);
465789Sahrens 	}
466789Sahrens 
467789Sahrens 	/*
468789Sahrens 	 * Fasttrack empty reads
469789Sahrens 	 */
470789Sahrens 	if (uio->uio_resid == 0) {
471789Sahrens 		ZFS_EXIT(zfsvfs);
472789Sahrens 		return (0);
473789Sahrens 	}
474789Sahrens 
475789Sahrens 	/*
4761669Sperrin 	 * Check for mandatory locks
477789Sahrens 	 */
478789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
479789Sahrens 		if (error = chklock(vp, FREAD,
480789Sahrens 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
481789Sahrens 			ZFS_EXIT(zfsvfs);
482789Sahrens 			return (error);
483789Sahrens 		}
484789Sahrens 	}
485789Sahrens 
486789Sahrens 	/*
487789Sahrens 	 * If we're in FRSYNC mode, sync out this znode before reading it.
488789Sahrens 	 */
4892638Sperrin 	if (ioflag & FRSYNC)
4902638Sperrin 		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
491789Sahrens 
492789Sahrens 	/*
4931669Sperrin 	 * Lock the range against changes.
494789Sahrens 	 */
4951669Sperrin 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
4961669Sperrin 
497789Sahrens 	/*
498789Sahrens 	 * If we are reading past end-of-file we can skip
499789Sahrens 	 * to the end; but we might still need to set atime.
500789Sahrens 	 */
501789Sahrens 	if (uio->uio_loffset >= zp->z_phys->zp_size) {
502789Sahrens 		error = 0;
503789Sahrens 		goto out;
504789Sahrens 	}
505789Sahrens 
5063638Sbillm 	ASSERT(uio->uio_loffset < zp->z_phys->zp_size);
5073638Sbillm 	n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset);
5083638Sbillm 
5093638Sbillm 	while (n > 0) {
5103638Sbillm 		nbytes = MIN(n, zfs_read_chunk_size -
5113638Sbillm 		    P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
5123638Sbillm 
5133638Sbillm 		if (vn_has_cached_data(vp))
5143638Sbillm 			error = mappedread(vp, nbytes, uio);
5153638Sbillm 		else
5163638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, nbytes);
5177294Sperrin 		if (error) {
5187294Sperrin 			/* convert checksum errors into IO errors */
5197294Sperrin 			if (error == ECKSUM)
5207294Sperrin 				error = EIO;
5213638Sbillm 			break;
5227294Sperrin 		}
5233638Sbillm 
5243638Sbillm 		n -= nbytes;
525789Sahrens 	}
5263638Sbillm 
527789Sahrens out:
5282237Smaybee 	zfs_range_unlock(rl);
529789Sahrens 
530789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
531789Sahrens 	ZFS_EXIT(zfsvfs);
532789Sahrens 	return (error);
533789Sahrens }
534789Sahrens 
535789Sahrens /*
536789Sahrens  * Write the bytes to a file.
537789Sahrens  *
538789Sahrens  *	IN:	vp	- vnode of file to be written to.
539789Sahrens  *		uio	- structure supplying write location, range info,
540789Sahrens  *			  and data buffer.
541789Sahrens  *		ioflag	- FAPPEND flag set if in append mode.
542789Sahrens  *		cr	- credentials of caller.
5435331Samw  *		ct	- caller context (NFS/CIFS fem monitor only)
544789Sahrens  *
545789Sahrens  *	OUT:	uio	- updated offset and range.
546789Sahrens  *
547789Sahrens  *	RETURN:	0 if success
548789Sahrens  *		error code if failure
549789Sahrens  *
550789Sahrens  * Timestamps:
551789Sahrens  *	vp - ctime|mtime updated if byte count > 0
552789Sahrens  */
553789Sahrens /* ARGSUSED */
554789Sahrens static int
555789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
556789Sahrens {
557789Sahrens 	znode_t		*zp = VTOZ(vp);
558789Sahrens 	rlim64_t	limit = uio->uio_llimit;
559789Sahrens 	ssize_t		start_resid = uio->uio_resid;
560789Sahrens 	ssize_t		tx_bytes;
561789Sahrens 	uint64_t	end_size;
562789Sahrens 	dmu_tx_t	*tx;
563789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
5645326Sek110237 	zilog_t		*zilog;
565789Sahrens 	offset_t	woff;
566789Sahrens 	ssize_t		n, nbytes;
5671669Sperrin 	rl_t		*rl;
568789Sahrens 	int		max_blksz = zfsvfs->z_max_blksz;
5696743Smarks 	uint64_t	pflags;
5701669Sperrin 	int		error;
571789Sahrens 
572789Sahrens 	/*
573789Sahrens 	 * Fasttrack empty write
574789Sahrens 	 */
5751669Sperrin 	n = start_resid;
576789Sahrens 	if (n == 0)
577789Sahrens 		return (0);
578789Sahrens 
5791669Sperrin 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
5801669Sperrin 		limit = MAXOFFSET_T;
5811669Sperrin 
5825367Sahrens 	ZFS_ENTER(zfsvfs);
5835367Sahrens 	ZFS_VERIFY_ZP(zp);
5846743Smarks 
5856743Smarks 	/*
5866743Smarks 	 * If immutable or not appending then return EPERM
5876743Smarks 	 */
5886743Smarks 	pflags = zp->z_phys->zp_flags;
5896743Smarks 	if ((pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
5906743Smarks 	    ((pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
5916743Smarks 	    (uio->uio_loffset < zp->z_phys->zp_size))) {
5926743Smarks 		ZFS_EXIT(zfsvfs);
5936743Smarks 		return (EPERM);
5946743Smarks 	}
5956743Smarks 
5965326Sek110237 	zilog = zfsvfs->z_log;
597789Sahrens 
598789Sahrens 	/*
5992237Smaybee 	 * Pre-fault the pages to ensure slow (eg NFS) pages
6001669Sperrin 	 * don't hold up txg.
601789Sahrens 	 */
6028059SDonghai.Qiao@Sun.COM 	uio_prefaultpages(n, uio);
603789Sahrens 
604789Sahrens 	/*
605789Sahrens 	 * If in append mode, set the io offset pointer to eof.
606789Sahrens 	 */
6071669Sperrin 	if (ioflag & FAPPEND) {
6081669Sperrin 		/*
6091669Sperrin 		 * Range lock for a file append:
6101669Sperrin 		 * The value for the start of range will be determined by
6111669Sperrin 		 * zfs_range_lock() (to guarantee append semantics).
6121669Sperrin 		 * If this write will cause the block size to increase,
6131669Sperrin 		 * zfs_range_lock() will lock the entire file, so we must
6141669Sperrin 		 * later reduce the range after we grow the block size.
6151669Sperrin 		 */
6161669Sperrin 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
6171669Sperrin 		if (rl->r_len == UINT64_MAX) {
6181669Sperrin 			/* overlocked, zp_size can't change */
6191669Sperrin 			woff = uio->uio_loffset = zp->z_phys->zp_size;
6201669Sperrin 		} else {
6211669Sperrin 			woff = uio->uio_loffset = rl->r_off;
6221669Sperrin 		}
623789Sahrens 	} else {
624789Sahrens 		woff = uio->uio_loffset;
625789Sahrens 		/*
626789Sahrens 		 * Validate file offset
627789Sahrens 		 */
628789Sahrens 		if (woff < 0) {
629789Sahrens 			ZFS_EXIT(zfsvfs);
630789Sahrens 			return (EINVAL);
631789Sahrens 		}
632789Sahrens 
633789Sahrens 		/*
6341669Sperrin 		 * If we need to grow the block size then zfs_range_lock()
6351669Sperrin 		 * will lock a wider range than we request here.
6361669Sperrin 		 * Later after growing the block size we reduce the range.
637789Sahrens 		 */
6381669Sperrin 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
639789Sahrens 	}
640789Sahrens 
641789Sahrens 	if (woff >= limit) {
6423638Sbillm 		zfs_range_unlock(rl);
6433638Sbillm 		ZFS_EXIT(zfsvfs);
6443638Sbillm 		return (EFBIG);
645789Sahrens 	}
646789Sahrens 
647789Sahrens 	if ((woff + n) > limit || woff > (limit - n))
648789Sahrens 		n = limit - woff;
649789Sahrens 
650789Sahrens 	/*
6511669Sperrin 	 * Check for mandatory locks
652789Sahrens 	 */
653789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) &&
6543638Sbillm 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
6553638Sbillm 		zfs_range_unlock(rl);
6563638Sbillm 		ZFS_EXIT(zfsvfs);
6573638Sbillm 		return (error);
6583638Sbillm 	}
6591669Sperrin 	end_size = MAX(zp->z_phys->zp_size, woff + n);
660789Sahrens 
6611669Sperrin 	/*
6623638Sbillm 	 * Write the file in reasonable size chunks.  Each chunk is written
6633638Sbillm 	 * in a separate transaction; this keeps the intent log records small
6643638Sbillm 	 * and allows us to do more fine-grained space accounting.
665789Sahrens 	 */
666789Sahrens 	while (n > 0) {
667789Sahrens 		/*
6683638Sbillm 		 * Start a transaction.
669789Sahrens 		 */
670*9396SMatthew.Ahrens@Sun.COM 		if (zfs_usergroup_overquota(zfsvfs,
671*9396SMatthew.Ahrens@Sun.COM 		    B_FALSE, zp->z_phys->zp_uid) ||
672*9396SMatthew.Ahrens@Sun.COM 		    zfs_usergroup_overquota(zfsvfs,
673*9396SMatthew.Ahrens@Sun.COM 		    B_TRUE, zp->z_phys->zp_gid)) {
674*9396SMatthew.Ahrens@Sun.COM 			error = EDQUOT;
675*9396SMatthew.Ahrens@Sun.COM 			break;
676*9396SMatthew.Ahrens@Sun.COM 		}
677789Sahrens 		woff = uio->uio_loffset;
678789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
679789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
680789Sahrens 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
6818227SNeil.Perrin@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
682789Sahrens 		if (error) {
6838227SNeil.Perrin@Sun.COM 			if (error == ERESTART) {
6842113Sahrens 				dmu_tx_wait(tx);
6852113Sahrens 				dmu_tx_abort(tx);
6863638Sbillm 				continue;
687789Sahrens 			}
6882113Sahrens 			dmu_tx_abort(tx);
6893638Sbillm 			break;
6903638Sbillm 		}
6913638Sbillm 
6923638Sbillm 		/*
6933638Sbillm 		 * If zfs_range_lock() over-locked we grow the blocksize
6943638Sbillm 		 * and then reduce the lock range.  This will only happen
6953638Sbillm 		 * on the first iteration since zfs_range_reduce() will
6963638Sbillm 		 * shrink down r_len to the appropriate size.
6973638Sbillm 		 */
6983638Sbillm 		if (rl->r_len == UINT64_MAX) {
6993638Sbillm 			uint64_t new_blksz;
7003638Sbillm 
7013638Sbillm 			if (zp->z_blksz > max_blksz) {
7023638Sbillm 				ASSERT(!ISP2(zp->z_blksz));
7033638Sbillm 				new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
7043638Sbillm 			} else {
7053638Sbillm 				new_blksz = MIN(end_size, max_blksz);
7063638Sbillm 			}
7073638Sbillm 			zfs_grow_blocksize(zp, new_blksz, tx);
7083638Sbillm 			zfs_range_reduce(rl, woff, n);
7093638Sbillm 		}
7103638Sbillm 
7113638Sbillm 		/*
7123638Sbillm 		 * XXX - should we really limit each write to z_max_blksz?
7133638Sbillm 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
7143638Sbillm 		 */
7153638Sbillm 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
7163638Sbillm 
7173638Sbillm 		tx_bytes = uio->uio_resid;
7188636SMark.Maybee@Sun.COM 		error = dmu_write_uio(zfsvfs->z_os, zp->z_id, uio, nbytes, tx);
7193638Sbillm 		tx_bytes -= uio->uio_resid;
7208636SMark.Maybee@Sun.COM 		if (tx_bytes && vn_has_cached_data(vp))
7218636SMark.Maybee@Sun.COM 			update_pages(vp, woff,
7228636SMark.Maybee@Sun.COM 			    tx_bytes, zfsvfs->z_os, zp->z_id);
7233638Sbillm 
7243638Sbillm 		/*
7253638Sbillm 		 * If we made no progress, we're done.  If we made even
7263638Sbillm 		 * partial progress, update the znode and ZIL accordingly.
7273638Sbillm 		 */
7283638Sbillm 		if (tx_bytes == 0) {
7293897Smaybee 			dmu_tx_commit(tx);
7303638Sbillm 			ASSERT(error != 0);
7313638Sbillm 			break;
7323638Sbillm 		}
7333638Sbillm 
734789Sahrens 		/*
7353638Sbillm 		 * Clear Set-UID/Set-GID bits on successful write if not
7363638Sbillm 		 * privileged and at least one of the excute bits is set.
7373638Sbillm 		 *
7383638Sbillm 		 * It would be nice to to this after all writes have
7393638Sbillm 		 * been done, but that would still expose the ISUID/ISGID
7403638Sbillm 		 * to another app after the partial write is committed.
7415331Samw 		 *
7425331Samw 		 * Note: we don't call zfs_fuid_map_id() here because
7435331Samw 		 * user 0 is not an ephemeral uid.
744789Sahrens 		 */
7453638Sbillm 		mutex_enter(&zp->z_acl_lock);
7463638Sbillm 		if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) |
7473638Sbillm 		    (S_IXUSR >> 6))) != 0 &&
7483638Sbillm 		    (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 &&
7493638Sbillm 		    secpolicy_vnode_setid_retain(cr,
7503638Sbillm 		    (zp->z_phys->zp_mode & S_ISUID) != 0 &&
7513638Sbillm 		    zp->z_phys->zp_uid == 0) != 0) {
7524339Sperrin 			zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID);
7533638Sbillm 		}
7543638Sbillm 		mutex_exit(&zp->z_acl_lock);
7553638Sbillm 
7563638Sbillm 		/*
7573638Sbillm 		 * Update time stamp.  NOTE: This marks the bonus buffer as
7583638Sbillm 		 * dirty, so we don't have to do it again for zp_size.
7593638Sbillm 		 */
7603638Sbillm 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
7613638Sbillm 
7623638Sbillm 		/*
7633638Sbillm 		 * Update the file size (zp_size) if it has changed;
7643638Sbillm 		 * account for possible concurrent updates.
7653638Sbillm 		 */
7663638Sbillm 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset)
767789Sahrens 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
768789Sahrens 			    uio->uio_loffset);
7693638Sbillm 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
7703638Sbillm 		dmu_tx_commit(tx);
7713638Sbillm 
7723638Sbillm 		if (error != 0)
7733638Sbillm 			break;
7743638Sbillm 		ASSERT(tx_bytes == nbytes);
7753638Sbillm 		n -= nbytes;
776789Sahrens 	}
777789Sahrens 
7782237Smaybee 	zfs_range_unlock(rl);
779789Sahrens 
780789Sahrens 	/*
781789Sahrens 	 * If we're in replay mode, or we made no progress, return error.
782789Sahrens 	 * Otherwise, it's at least a partial write, so it's successful.
783789Sahrens 	 */
7848227SNeil.Perrin@Sun.COM 	if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
785789Sahrens 		ZFS_EXIT(zfsvfs);
786789Sahrens 		return (error);
787789Sahrens 	}
788789Sahrens 
7892638Sperrin 	if (ioflag & (FSYNC | FDSYNC))
7902638Sperrin 		zil_commit(zilog, zp->z_last_itx, zp->z_id);
791789Sahrens 
792789Sahrens 	ZFS_EXIT(zfsvfs);
793789Sahrens 	return (0);
794789Sahrens }
795789Sahrens 
7962237Smaybee void
7973063Sperrin zfs_get_done(dmu_buf_t *db, void *vzgd)
7982237Smaybee {
7993063Sperrin 	zgd_t *zgd = (zgd_t *)vzgd;
8003063Sperrin 	rl_t *rl = zgd->zgd_rl;
8012237Smaybee 	vnode_t *vp = ZTOV(rl->r_zp);
8029321SNeil.Perrin@Sun.COM 	objset_t *os = rl->r_zp->z_zfsvfs->z_os;
8032237Smaybee 
8043063Sperrin 	dmu_buf_rele(db, vzgd);
8052237Smaybee 	zfs_range_unlock(rl);
8069321SNeil.Perrin@Sun.COM 	/*
8079321SNeil.Perrin@Sun.COM 	 * Release the vnode asynchronously as we currently have the
8089321SNeil.Perrin@Sun.COM 	 * txg stopped from syncing.
8099321SNeil.Perrin@Sun.COM 	 */
8109321SNeil.Perrin@Sun.COM 	VN_RELE_ASYNC(vp, dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
8115688Sbonwick 	zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
8123063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
8132237Smaybee }
8142237Smaybee 
815789Sahrens /*
816789Sahrens  * Get data to generate a TX_WRITE intent log record.
817789Sahrens  */
818789Sahrens int
8192237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
820789Sahrens {
821789Sahrens 	zfsvfs_t *zfsvfs = arg;
822789Sahrens 	objset_t *os = zfsvfs->z_os;
823789Sahrens 	znode_t *zp;
824789Sahrens 	uint64_t off = lr->lr_offset;
8252237Smaybee 	dmu_buf_t *db;
8261669Sperrin 	rl_t *rl;
8273063Sperrin 	zgd_t *zgd;
8283638Sbillm 	int dlen = lr->lr_length;		/* length of user data */
829789Sahrens 	int error = 0;
830789Sahrens 
8313063Sperrin 	ASSERT(zio);
832789Sahrens 	ASSERT(dlen != 0);
833789Sahrens 
834789Sahrens 	/*
8351669Sperrin 	 * Nothing to do if the file has been removed
836789Sahrens 	 */
837789Sahrens 	if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0)
838789Sahrens 		return (ENOENT);
8393461Sahrens 	if (zp->z_unlinked) {
8409321SNeil.Perrin@Sun.COM 		/*
8419321SNeil.Perrin@Sun.COM 		 * Release the vnode asynchronously as we currently have the
8429321SNeil.Perrin@Sun.COM 		 * txg stopped from syncing.
8439321SNeil.Perrin@Sun.COM 		 */
8449321SNeil.Perrin@Sun.COM 		VN_RELE_ASYNC(ZTOV(zp),
8459321SNeil.Perrin@Sun.COM 		    dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
846789Sahrens 		return (ENOENT);
847789Sahrens 	}
848789Sahrens 
849789Sahrens 	/*
850789Sahrens 	 * Write records come in two flavors: immediate and indirect.
851789Sahrens 	 * For small writes it's cheaper to store the data with the
852789Sahrens 	 * log record (immediate); for large writes it's cheaper to
853789Sahrens 	 * sync the data and get a pointer to it (indirect) so that
854789Sahrens 	 * we don't have to write the data twice.
855789Sahrens 	 */
8561669Sperrin 	if (buf != NULL) { /* immediate write */
8571669Sperrin 		rl = zfs_range_lock(zp, off, dlen, RL_READER);
8581669Sperrin 		/* test for truncation needs to be done while range locked */
8591669Sperrin 		if (off >= zp->z_phys->zp_size) {
8601669Sperrin 			error = ENOENT;
8611669Sperrin 			goto out;
8621669Sperrin 		}
8632449Smaybee 		VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf));
8641669Sperrin 	} else { /* indirect write */
8651669Sperrin 		uint64_t boff; /* block starting offset */
8661669Sperrin 
867789Sahrens 		/*
8681669Sperrin 		 * Have to lock the whole block to ensure when it's
8691669Sperrin 		 * written out and it's checksum is being calculated
8701669Sperrin 		 * that no one can change the data. We need to re-check
8711669Sperrin 		 * blocksize after we get the lock in case it's changed!
872789Sahrens 		 */
8731669Sperrin 		for (;;) {
8741941Sperrin 			if (ISP2(zp->z_blksz)) {
8751941Sperrin 				boff = P2ALIGN_TYPED(off, zp->z_blksz,
8761941Sperrin 				    uint64_t);
8771941Sperrin 			} else {
8781941Sperrin 				boff = 0;
8791941Sperrin 			}
8801669Sperrin 			dlen = zp->z_blksz;
8811669Sperrin 			rl = zfs_range_lock(zp, boff, dlen, RL_READER);
8821669Sperrin 			if (zp->z_blksz == dlen)
8831669Sperrin 				break;
8842237Smaybee 			zfs_range_unlock(rl);
8851669Sperrin 		}
8861669Sperrin 		/* test for truncation needs to be done while range locked */
8871669Sperrin 		if (off >= zp->z_phys->zp_size) {
8881669Sperrin 			error = ENOENT;
8891669Sperrin 			goto out;
8901669Sperrin 		}
8913063Sperrin 		zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
8923063Sperrin 		zgd->zgd_rl = rl;
8933063Sperrin 		zgd->zgd_zilog = zfsvfs->z_log;
8943063Sperrin 		zgd->zgd_bp = &lr->lr_blkptr;
8953063Sperrin 		VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db));
8962237Smaybee 		ASSERT(boff == db->db_offset);
8972237Smaybee 		lr->lr_blkoff = off - boff;
8982237Smaybee 		error = dmu_sync(zio, db, &lr->lr_blkptr,
8993063Sperrin 		    lr->lr_common.lrc_txg, zfs_get_done, zgd);
9004709Smaybee 		ASSERT((error && error != EINPROGRESS) ||
9014709Smaybee 		    lr->lr_length <= zp->z_blksz);
9025688Sbonwick 		if (error == 0)
9035688Sbonwick 			zil_add_block(zfsvfs->z_log, &lr->lr_blkptr);
9042237Smaybee 		/*
9052237Smaybee 		 * If we get EINPROGRESS, then we need to wait for a
9062237Smaybee 		 * write IO initiated by dmu_sync() to complete before
9072638Sperrin 		 * we can release this dbuf.  We will finish everything
9082237Smaybee 		 * up in the zfs_get_done() callback.
9092237Smaybee 		 */
9102237Smaybee 		if (error == EINPROGRESS)
9112237Smaybee 			return (0);
9123063Sperrin 		dmu_buf_rele(db, zgd);
9133063Sperrin 		kmem_free(zgd, sizeof (zgd_t));
914789Sahrens 	}
9151669Sperrin out:
9162237Smaybee 	zfs_range_unlock(rl);
9179321SNeil.Perrin@Sun.COM 	/*
9189321SNeil.Perrin@Sun.COM 	 * Release the vnode asynchronously as we currently have the
9199321SNeil.Perrin@Sun.COM 	 * txg stopped from syncing.
9209321SNeil.Perrin@Sun.COM 	 */
9219321SNeil.Perrin@Sun.COM 	VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
922789Sahrens 	return (error);
923789Sahrens }
924789Sahrens 
925789Sahrens /*ARGSUSED*/
926789Sahrens static int
9275331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
9285331Samw     caller_context_t *ct)
929789Sahrens {
930789Sahrens 	znode_t *zp = VTOZ(vp);
931789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
932789Sahrens 	int error;
933789Sahrens 
9345367Sahrens 	ZFS_ENTER(zfsvfs);
9355367Sahrens 	ZFS_VERIFY_ZP(zp);
9365331Samw 
9375331Samw 	if (flag & V_ACE_MASK)
9385331Samw 		error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
9395331Samw 	else
9405331Samw 		error = zfs_zaccess_rwx(zp, mode, flag, cr);
9415331Samw 
942789Sahrens 	ZFS_EXIT(zfsvfs);
943789Sahrens 	return (error);
944789Sahrens }
945789Sahrens 
946789Sahrens /*
947789Sahrens  * Lookup an entry in a directory, or an extended attribute directory.
948789Sahrens  * If it exists, return a held vnode reference for it.
949789Sahrens  *
950789Sahrens  *	IN:	dvp	- vnode of directory to search.
951789Sahrens  *		nm	- name of entry to lookup.
952789Sahrens  *		pnp	- full pathname to lookup [UNUSED].
953789Sahrens  *		flags	- LOOKUP_XATTR set if looking for an attribute.
954789Sahrens  *		rdir	- root directory vnode [UNUSED].
955789Sahrens  *		cr	- credentials of caller.
9565331Samw  *		ct	- caller context
9575331Samw  *		direntflags - directory lookup flags
9585331Samw  *		realpnp - returned pathname.
959789Sahrens  *
960789Sahrens  *	OUT:	vpp	- vnode of located entry, NULL if not found.
961789Sahrens  *
962789Sahrens  *	RETURN:	0 if success
963789Sahrens  *		error code if failure
964789Sahrens  *
965789Sahrens  * Timestamps:
966789Sahrens  *	NA
967789Sahrens  */
968789Sahrens /* ARGSUSED */
969789Sahrens static int
970789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
9715331Samw     int flags, vnode_t *rdir, cred_t *cr,  caller_context_t *ct,
9725331Samw     int *direntflags, pathname_t *realpnp)
973789Sahrens {
974789Sahrens 	znode_t *zdp = VTOZ(dvp);
975789Sahrens 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
976789Sahrens 	int	error;
977789Sahrens 
9785367Sahrens 	ZFS_ENTER(zfsvfs);
9795367Sahrens 	ZFS_VERIFY_ZP(zdp);
980789Sahrens 
981789Sahrens 	*vpp = NULL;
982789Sahrens 
983789Sahrens 	if (flags & LOOKUP_XATTR) {
984789Sahrens 		/*
9853234Sck153898 		 * If the xattr property is off, refuse the lookup request.
9863234Sck153898 		 */
9873234Sck153898 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
9883234Sck153898 			ZFS_EXIT(zfsvfs);
9893234Sck153898 			return (EINVAL);
9903234Sck153898 		}
9913234Sck153898 
9923234Sck153898 		/*
993789Sahrens 		 * We don't allow recursive attributes..
994789Sahrens 		 * Maybe someday we will.
995789Sahrens 		 */
996789Sahrens 		if (zdp->z_phys->zp_flags & ZFS_XATTR) {
997789Sahrens 			ZFS_EXIT(zfsvfs);
998789Sahrens 			return (EINVAL);
999789Sahrens 		}
1000789Sahrens 
10013280Sck153898 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1002789Sahrens 			ZFS_EXIT(zfsvfs);
1003789Sahrens 			return (error);
1004789Sahrens 		}
1005789Sahrens 
1006789Sahrens 		/*
1007789Sahrens 		 * Do we have permission to get into attribute directory?
1008789Sahrens 		 */
1009789Sahrens 
10105331Samw 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
10115331Samw 		    B_FALSE, cr)) {
1012789Sahrens 			VN_RELE(*vpp);
10135331Samw 			*vpp = NULL;
1014789Sahrens 		}
1015789Sahrens 
1016789Sahrens 		ZFS_EXIT(zfsvfs);
1017789Sahrens 		return (error);
1018789Sahrens 	}
1019789Sahrens 
10201512Sek110237 	if (dvp->v_type != VDIR) {
10211512Sek110237 		ZFS_EXIT(zfsvfs);
10221460Smarks 		return (ENOTDIR);
10231512Sek110237 	}
10241460Smarks 
1025789Sahrens 	/*
1026789Sahrens 	 * Check accessibility of directory.
1027789Sahrens 	 */
1028789Sahrens 
10295331Samw 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1030789Sahrens 		ZFS_EXIT(zfsvfs);
1031789Sahrens 		return (error);
1032789Sahrens 	}
1033789Sahrens 
10345498Stimh 	if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
10355331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
10365331Samw 		ZFS_EXIT(zfsvfs);
10375331Samw 		return (EILSEQ);
10385331Samw 	}
10395331Samw 
10405331Samw 	error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
10415331Samw 	if (error == 0) {
1042789Sahrens 		/*
1043789Sahrens 		 * Convert device special files
1044789Sahrens 		 */
1045789Sahrens 		if (IS_DEVVP(*vpp)) {
1046789Sahrens 			vnode_t	*svp;
1047789Sahrens 
1048789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1049789Sahrens 			VN_RELE(*vpp);
1050789Sahrens 			if (svp == NULL)
1051789Sahrens 				error = ENOSYS;
1052789Sahrens 			else
1053789Sahrens 				*vpp = svp;
1054789Sahrens 		}
1055789Sahrens 	}
1056789Sahrens 
1057789Sahrens 	ZFS_EXIT(zfsvfs);
1058789Sahrens 	return (error);
1059789Sahrens }
1060789Sahrens 
1061789Sahrens /*
1062789Sahrens  * Attempt to create a new entry in a directory.  If the entry
1063789Sahrens  * already exists, truncate the file if permissible, else return
1064789Sahrens  * an error.  Return the vp of the created or trunc'd file.
1065789Sahrens  *
1066789Sahrens  *	IN:	dvp	- vnode of directory to put new file entry in.
1067789Sahrens  *		name	- name of new file entry.
1068789Sahrens  *		vap	- attributes of new file.
1069789Sahrens  *		excl	- flag indicating exclusive or non-exclusive mode.
1070789Sahrens  *		mode	- mode to open file with.
1071789Sahrens  *		cr	- credentials of caller.
1072789Sahrens  *		flag	- large file flag [UNUSED].
10735331Samw  *		ct	- caller context
10745331Samw  *		vsecp 	- ACL to be set
1075789Sahrens  *
1076789Sahrens  *	OUT:	vpp	- vnode of created or trunc'd entry.
1077789Sahrens  *
1078789Sahrens  *	RETURN:	0 if success
1079789Sahrens  *		error code if failure
1080789Sahrens  *
1081789Sahrens  * Timestamps:
1082789Sahrens  *	dvp - ctime|mtime updated if new entry created
1083789Sahrens  *	 vp - ctime|mtime always, atime if new
1084789Sahrens  */
10855331Samw 
1086789Sahrens /* ARGSUSED */
1087789Sahrens static int
1088789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
10895331Samw     int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
10905331Samw     vsecattr_t *vsecp)
1091789Sahrens {
1092789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1093789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
10945326Sek110237 	zilog_t		*zilog;
10955326Sek110237 	objset_t	*os;
1096789Sahrens 	zfs_dirlock_t	*dl;
1097789Sahrens 	dmu_tx_t	*tx;
1098789Sahrens 	int		error;
10997847SMark.Shellenbaum@Sun.COM 	ksid_t		*ksid;
11007847SMark.Shellenbaum@Sun.COM 	uid_t		uid;
11017847SMark.Shellenbaum@Sun.COM 	gid_t		gid = crgetgid(cr);
11029179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t	acl_ids;
11039179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
11045331Samw 
11055331Samw 	/*
11065331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
11075331Samw 	 * make sure file system is at proper version
11085331Samw 	 */
11095331Samw 
11107847SMark.Shellenbaum@Sun.COM 	ksid = crgetsid(cr, KSID_OWNER);
11117847SMark.Shellenbaum@Sun.COM 	if (ksid)
11127847SMark.Shellenbaum@Sun.COM 		uid = ksid_getid(ksid);
11137847SMark.Shellenbaum@Sun.COM 	else
11147847SMark.Shellenbaum@Sun.COM 		uid = crgetuid(cr);
11157847SMark.Shellenbaum@Sun.COM 
11165331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
11175331Samw 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
11187847SMark.Shellenbaum@Sun.COM 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
11195331Samw 		return (EINVAL);
1120789Sahrens 
11215367Sahrens 	ZFS_ENTER(zfsvfs);
11225367Sahrens 	ZFS_VERIFY_ZP(dzp);
11235326Sek110237 	os = zfsvfs->z_os;
11245326Sek110237 	zilog = zfsvfs->z_log;
1125789Sahrens 
11265498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
11275331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
11285331Samw 		ZFS_EXIT(zfsvfs);
11295331Samw 		return (EILSEQ);
11305331Samw 	}
11315331Samw 
11325331Samw 	if (vap->va_mask & AT_XVATTR) {
11335331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
11345331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
11355331Samw 			ZFS_EXIT(zfsvfs);
11365331Samw 			return (error);
11375331Samw 		}
11385331Samw 	}
1139789Sahrens top:
1140789Sahrens 	*vpp = NULL;
1141789Sahrens 
1142789Sahrens 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1143789Sahrens 		vap->va_mode &= ~VSVTX;
1144789Sahrens 
1145789Sahrens 	if (*name == '\0') {
1146789Sahrens 		/*
1147789Sahrens 		 * Null component name refers to the directory itself.
1148789Sahrens 		 */
1149789Sahrens 		VN_HOLD(dvp);
1150789Sahrens 		zp = dzp;
1151789Sahrens 		dl = NULL;
1152789Sahrens 		error = 0;
1153789Sahrens 	} else {
1154789Sahrens 		/* possible VN_HOLD(zp) */
11555331Samw 		int zflg = 0;
11565331Samw 
11575331Samw 		if (flag & FIGNORECASE)
11585331Samw 			zflg |= ZCILOOK;
11595331Samw 
11605331Samw 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
11615331Samw 		    NULL, NULL);
11625331Samw 		if (error) {
1163789Sahrens 			if (strcmp(name, "..") == 0)
1164789Sahrens 				error = EISDIR;
1165789Sahrens 			ZFS_EXIT(zfsvfs);
1166789Sahrens 			return (error);
1167789Sahrens 		}
1168789Sahrens 	}
1169789Sahrens 	if (zp == NULL) {
11705331Samw 		uint64_t txtype;
11715331Samw 
1172789Sahrens 		/*
1173789Sahrens 		 * Create a new file object and update the directory
1174789Sahrens 		 * to reference it.
1175789Sahrens 		 */
11765331Samw 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1177789Sahrens 			goto out;
1178789Sahrens 		}
1179789Sahrens 
1180789Sahrens 		/*
1181789Sahrens 		 * We only support the creation of regular files in
1182789Sahrens 		 * extended attribute directories.
1183789Sahrens 		 */
1184789Sahrens 		if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1185789Sahrens 		    (vap->va_type != VREG)) {
1186789Sahrens 			error = EINVAL;
1187789Sahrens 			goto out;
1188789Sahrens 		}
1189789Sahrens 
11909179SMark.Shellenbaum@Sun.COM 		if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
11919179SMark.Shellenbaum@Sun.COM 		    &acl_ids)) != 0)
11929179SMark.Shellenbaum@Sun.COM 			goto out;
1193*9396SMatthew.Ahrens@Sun.COM 		if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1194*9396SMatthew.Ahrens@Sun.COM 			error = EDQUOT;
1195*9396SMatthew.Ahrens@Sun.COM 			goto out;
1196*9396SMatthew.Ahrens@Sun.COM 		}
11979179SMark.Shellenbaum@Sun.COM 
1198789Sahrens 		tx = dmu_tx_create(os);
1199789Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
12009179SMark.Shellenbaum@Sun.COM 		fuid_dirtied = zfsvfs->z_fuid_dirty;
1201*9396SMatthew.Ahrens@Sun.COM 		if (fuid_dirtied)
1202*9396SMatthew.Ahrens@Sun.COM 			zfs_fuid_txhold(zfsvfs, tx);
1203789Sahrens 		dmu_tx_hold_bonus(tx, dzp->z_id);
12041544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
12059179SMark.Shellenbaum@Sun.COM 		if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1206789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1207789Sahrens 			    0, SPA_MAXBLOCKSIZE);
12085331Samw 		}
12098227SNeil.Perrin@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
1210789Sahrens 		if (error) {
12119179SMark.Shellenbaum@Sun.COM 			zfs_acl_ids_free(&acl_ids);
1212789Sahrens 			zfs_dirent_unlock(dl);
12138227SNeil.Perrin@Sun.COM 			if (error == ERESTART) {
12142113Sahrens 				dmu_tx_wait(tx);
12152113Sahrens 				dmu_tx_abort(tx);
1216789Sahrens 				goto top;
1217789Sahrens 			}
12182113Sahrens 			dmu_tx_abort(tx);
1219789Sahrens 			ZFS_EXIT(zfsvfs);
1220789Sahrens 			return (error);
1221789Sahrens 		}
12229179SMark.Shellenbaum@Sun.COM 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids);
12239179SMark.Shellenbaum@Sun.COM 
12249179SMark.Shellenbaum@Sun.COM 		if (fuid_dirtied)
12259179SMark.Shellenbaum@Sun.COM 			zfs_fuid_sync(zfsvfs, tx);
12269179SMark.Shellenbaum@Sun.COM 
1227789Sahrens 		(void) zfs_link_create(dl, zp, tx, ZNEW);
12289179SMark.Shellenbaum@Sun.COM 
12295331Samw 		txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
12305331Samw 		if (flag & FIGNORECASE)
12315331Samw 			txtype |= TX_CI;
12325331Samw 		zfs_log_create(zilog, tx, txtype, dzp, zp, name,
12339179SMark.Shellenbaum@Sun.COM 		    vsecp, acl_ids.z_fuidp, vap);
12349179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
1235789Sahrens 		dmu_tx_commit(tx);
1236789Sahrens 	} else {
12375331Samw 		int aflags = (flag & FAPPEND) ? V_APPEND : 0;
12385331Samw 
1239789Sahrens 		/*
1240789Sahrens 		 * A directory entry already exists for this name.
1241789Sahrens 		 */
1242789Sahrens 		/*
1243789Sahrens 		 * Can't truncate an existing file if in exclusive mode.
1244789Sahrens 		 */
1245789Sahrens 		if (excl == EXCL) {
1246789Sahrens 			error = EEXIST;
1247789Sahrens 			goto out;
1248789Sahrens 		}
1249789Sahrens 		/*
1250789Sahrens 		 * Can't open a directory for writing.
1251789Sahrens 		 */
1252789Sahrens 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1253789Sahrens 			error = EISDIR;
1254789Sahrens 			goto out;
1255789Sahrens 		}
1256789Sahrens 		/*
1257789Sahrens 		 * Verify requested access to file.
1258789Sahrens 		 */
12595331Samw 		if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1260789Sahrens 			goto out;
1261789Sahrens 		}
1262789Sahrens 
1263789Sahrens 		mutex_enter(&dzp->z_lock);
1264789Sahrens 		dzp->z_seq++;
1265789Sahrens 		mutex_exit(&dzp->z_lock);
1266789Sahrens 
12671878Smaybee 		/*
12681878Smaybee 		 * Truncate regular files if requested.
12691878Smaybee 		 */
12701878Smaybee 		if ((ZTOV(zp)->v_type == VREG) &&
1271789Sahrens 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
12726992Smaybee 			/* we can't hold any locks when calling zfs_freesp() */
12736992Smaybee 			zfs_dirent_unlock(dl);
12746992Smaybee 			dl = NULL;
12751878Smaybee 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
12764863Spraks 			if (error == 0) {
12775331Samw 				vnevent_create(ZTOV(zp), ct);
12784863Spraks 			}
1279789Sahrens 		}
1280789Sahrens 	}
1281789Sahrens out:
1282789Sahrens 
1283789Sahrens 	if (dl)
1284789Sahrens 		zfs_dirent_unlock(dl);
1285789Sahrens 
1286789Sahrens 	if (error) {
1287789Sahrens 		if (zp)
1288789Sahrens 			VN_RELE(ZTOV(zp));
1289789Sahrens 	} else {
1290789Sahrens 		*vpp = ZTOV(zp);
1291789Sahrens 		/*
1292789Sahrens 		 * If vnode is for a device return a specfs vnode instead.
1293789Sahrens 		 */
1294789Sahrens 		if (IS_DEVVP(*vpp)) {
1295789Sahrens 			struct vnode *svp;
1296789Sahrens 
1297789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1298789Sahrens 			VN_RELE(*vpp);
1299789Sahrens 			if (svp == NULL) {
1300789Sahrens 				error = ENOSYS;
1301789Sahrens 			}
1302789Sahrens 			*vpp = svp;
1303789Sahrens 		}
1304789Sahrens 	}
1305789Sahrens 
1306789Sahrens 	ZFS_EXIT(zfsvfs);
1307789Sahrens 	return (error);
1308789Sahrens }
1309789Sahrens 
1310789Sahrens /*
1311789Sahrens  * Remove an entry from a directory.
1312789Sahrens  *
1313789Sahrens  *	IN:	dvp	- vnode of directory to remove entry from.
1314789Sahrens  *		name	- name of entry to remove.
1315789Sahrens  *		cr	- credentials of caller.
13165331Samw  *		ct	- caller context
13175331Samw  *		flags	- case flags
1318789Sahrens  *
1319789Sahrens  *	RETURN:	0 if success
1320789Sahrens  *		error code if failure
1321789Sahrens  *
1322789Sahrens  * Timestamps:
1323789Sahrens  *	dvp - ctime|mtime
1324789Sahrens  *	 vp - ctime (if nlink > 0)
1325789Sahrens  */
13265331Samw /*ARGSUSED*/
1327789Sahrens static int
13285331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
13295331Samw     int flags)
1330789Sahrens {
1331789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1332789Sahrens 	znode_t		*xzp = NULL;
1333789Sahrens 	vnode_t		*vp;
1334789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
13355326Sek110237 	zilog_t		*zilog;
1336789Sahrens 	uint64_t	acl_obj, xattr_obj;
1337789Sahrens 	zfs_dirlock_t	*dl;
1338789Sahrens 	dmu_tx_t	*tx;
13393461Sahrens 	boolean_t	may_delete_now, delete_now = FALSE;
13406992Smaybee 	boolean_t	unlinked, toobig = FALSE;
13415331Samw 	uint64_t	txtype;
13425331Samw 	pathname_t	*realnmp = NULL;
13435331Samw 	pathname_t	realnm;
1344789Sahrens 	int		error;
13455331Samw 	int		zflg = ZEXISTS;
1346789Sahrens 
13475367Sahrens 	ZFS_ENTER(zfsvfs);
13485367Sahrens 	ZFS_VERIFY_ZP(dzp);
13495326Sek110237 	zilog = zfsvfs->z_log;
1350789Sahrens 
13515331Samw 	if (flags & FIGNORECASE) {
13525331Samw 		zflg |= ZCILOOK;
13535331Samw 		pn_alloc(&realnm);
13545331Samw 		realnmp = &realnm;
13555331Samw 	}
13565331Samw 
1357789Sahrens top:
1358789Sahrens 	/*
1359789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1360789Sahrens 	 */
13615331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
13625331Samw 	    NULL, realnmp)) {
13635331Samw 		if (realnmp)
13645331Samw 			pn_free(realnmp);
1365789Sahrens 		ZFS_EXIT(zfsvfs);
1366789Sahrens 		return (error);
1367789Sahrens 	}
1368789Sahrens 
1369789Sahrens 	vp = ZTOV(zp);
1370789Sahrens 
1371789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1372789Sahrens 		goto out;
1373789Sahrens 	}
1374789Sahrens 
1375789Sahrens 	/*
1376789Sahrens 	 * Need to use rmdir for removing directories.
1377789Sahrens 	 */
1378789Sahrens 	if (vp->v_type == VDIR) {
1379789Sahrens 		error = EPERM;
1380789Sahrens 		goto out;
1381789Sahrens 	}
1382789Sahrens 
13835331Samw 	vnevent_remove(vp, dvp, name, ct);
13845331Samw 
13855331Samw 	if (realnmp)
13866492Stimh 		dnlc_remove(dvp, realnmp->pn_buf);
13875331Samw 	else
13885331Samw 		dnlc_remove(dvp, name);
13891484Sek110237 
1390789Sahrens 	mutex_enter(&vp->v_lock);
1391789Sahrens 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1392789Sahrens 	mutex_exit(&vp->v_lock);
1393789Sahrens 
1394789Sahrens 	/*
13953461Sahrens 	 * We may delete the znode now, or we may put it in the unlinked set;
1396789Sahrens 	 * it depends on whether we're the last link, and on whether there are
1397789Sahrens 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1398789Sahrens 	 * allow for either case.
1399789Sahrens 	 */
1400789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
14011544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1402789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
14036992Smaybee 	if (may_delete_now) {
14046992Smaybee 		toobig =
14056992Smaybee 		    zp->z_phys->zp_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
14066992Smaybee 		/* if the file is too big, only hold_free a token amount */
14076992Smaybee 		dmu_tx_hold_free(tx, zp->z_id, 0,
14086992Smaybee 		    (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
14096992Smaybee 	}
1410789Sahrens 
1411789Sahrens 	/* are there any extended attributes? */
1412789Sahrens 	if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1413789Sahrens 		/* XXX - do we need this if we are deleting? */
1414789Sahrens 		dmu_tx_hold_bonus(tx, xattr_obj);
1415789Sahrens 	}
1416789Sahrens 
1417789Sahrens 	/* are there any additional acls */
1418789Sahrens 	if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1419789Sahrens 	    may_delete_now)
1420789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1421789Sahrens 
1422789Sahrens 	/* charge as an update -- would be nice not to charge at all */
14233461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1424789Sahrens 
14258227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1426789Sahrens 	if (error) {
1427789Sahrens 		zfs_dirent_unlock(dl);
1428789Sahrens 		VN_RELE(vp);
14298227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
14302113Sahrens 			dmu_tx_wait(tx);
14312113Sahrens 			dmu_tx_abort(tx);
1432789Sahrens 			goto top;
1433789Sahrens 		}
14345331Samw 		if (realnmp)
14355331Samw 			pn_free(realnmp);
14362113Sahrens 		dmu_tx_abort(tx);
1437789Sahrens 		ZFS_EXIT(zfsvfs);
1438789Sahrens 		return (error);
1439789Sahrens 	}
1440789Sahrens 
1441789Sahrens 	/*
1442789Sahrens 	 * Remove the directory entry.
1443789Sahrens 	 */
14445331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1445789Sahrens 
1446789Sahrens 	if (error) {
1447789Sahrens 		dmu_tx_commit(tx);
1448789Sahrens 		goto out;
1449789Sahrens 	}
1450789Sahrens 
14513461Sahrens 	if (unlinked) {
1452789Sahrens 		mutex_enter(&vp->v_lock);
14536992Smaybee 		delete_now = may_delete_now && !toobig &&
1454789Sahrens 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1455789Sahrens 		    zp->z_phys->zp_xattr == xattr_obj &&
1456789Sahrens 		    zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1457789Sahrens 		mutex_exit(&vp->v_lock);
1458789Sahrens 	}
1459789Sahrens 
1460789Sahrens 	if (delete_now) {
1461789Sahrens 		if (zp->z_phys->zp_xattr) {
1462789Sahrens 			error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1463789Sahrens 			ASSERT3U(error, ==, 0);
1464789Sahrens 			ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1465789Sahrens 			dmu_buf_will_dirty(xzp->z_dbuf, tx);
1466789Sahrens 			mutex_enter(&xzp->z_lock);
14673461Sahrens 			xzp->z_unlinked = 1;
1468789Sahrens 			xzp->z_phys->zp_links = 0;
1469789Sahrens 			mutex_exit(&xzp->z_lock);
14703461Sahrens 			zfs_unlinked_add(xzp, tx);
1471789Sahrens 			zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1472789Sahrens 		}
1473789Sahrens 		mutex_enter(&zp->z_lock);
1474789Sahrens 		mutex_enter(&vp->v_lock);
1475789Sahrens 		vp->v_count--;
1476789Sahrens 		ASSERT3U(vp->v_count, ==, 0);
1477789Sahrens 		mutex_exit(&vp->v_lock);
1478789Sahrens 		mutex_exit(&zp->z_lock);
1479789Sahrens 		zfs_znode_delete(zp, tx);
14803461Sahrens 	} else if (unlinked) {
14813461Sahrens 		zfs_unlinked_add(zp, tx);
1482789Sahrens 	}
1483789Sahrens 
14845331Samw 	txtype = TX_REMOVE;
14855331Samw 	if (flags & FIGNORECASE)
14865331Samw 		txtype |= TX_CI;
14875331Samw 	zfs_log_remove(zilog, tx, txtype, dzp, name);
1488789Sahrens 
1489789Sahrens 	dmu_tx_commit(tx);
1490789Sahrens out:
14915331Samw 	if (realnmp)
14925331Samw 		pn_free(realnmp);
14935331Samw 
1494789Sahrens 	zfs_dirent_unlock(dl);
1495789Sahrens 
1496789Sahrens 	if (!delete_now) {
1497789Sahrens 		VN_RELE(vp);
1498789Sahrens 	} else if (xzp) {
14996992Smaybee 		/* this rele is delayed to prevent nesting transactions */
1500789Sahrens 		VN_RELE(ZTOV(xzp));
1501789Sahrens 	}
1502789Sahrens 
1503789Sahrens 	ZFS_EXIT(zfsvfs);
1504789Sahrens 	return (error);
1505789Sahrens }
1506789Sahrens 
1507789Sahrens /*
1508789Sahrens  * Create a new directory and insert it into dvp using the name
1509789Sahrens  * provided.  Return a pointer to the inserted directory.
1510789Sahrens  *
1511789Sahrens  *	IN:	dvp	- vnode of directory to add subdir to.
1512789Sahrens  *		dirname	- name of new directory.
1513789Sahrens  *		vap	- attributes of new directory.
1514789Sahrens  *		cr	- credentials of caller.
15155331Samw  *		ct	- caller context
15165331Samw  *		vsecp	- ACL to be set
1517789Sahrens  *
1518789Sahrens  *	OUT:	vpp	- vnode of created directory.
1519789Sahrens  *
1520789Sahrens  *	RETURN:	0 if success
1521789Sahrens  *		error code if failure
1522789Sahrens  *
1523789Sahrens  * Timestamps:
1524789Sahrens  *	dvp - ctime|mtime updated
1525789Sahrens  *	 vp - ctime|mtime|atime updated
1526789Sahrens  */
15275331Samw /*ARGSUSED*/
1528789Sahrens static int
15295331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
15305331Samw     caller_context_t *ct, int flags, vsecattr_t *vsecp)
1531789Sahrens {
1532789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1533789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
15345326Sek110237 	zilog_t		*zilog;
1535789Sahrens 	zfs_dirlock_t	*dl;
15365331Samw 	uint64_t	txtype;
1537789Sahrens 	dmu_tx_t	*tx;
1538789Sahrens 	int		error;
15395331Samw 	int		zf = ZNEW;
15407847SMark.Shellenbaum@Sun.COM 	ksid_t		*ksid;
15417847SMark.Shellenbaum@Sun.COM 	uid_t		uid;
15427847SMark.Shellenbaum@Sun.COM 	gid_t		gid = crgetgid(cr);
15439179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t	acl_ids;
15449179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
1545789Sahrens 
1546789Sahrens 	ASSERT(vap->va_type == VDIR);
1547789Sahrens 
15485331Samw 	/*
15495331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
15505331Samw 	 * make sure file system is at proper version
15515331Samw 	 */
15525331Samw 
15537847SMark.Shellenbaum@Sun.COM 	ksid = crgetsid(cr, KSID_OWNER);
15547847SMark.Shellenbaum@Sun.COM 	if (ksid)
15557847SMark.Shellenbaum@Sun.COM 		uid = ksid_getid(ksid);
15567847SMark.Shellenbaum@Sun.COM 	else
15577847SMark.Shellenbaum@Sun.COM 		uid = crgetuid(cr);
15585331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
15597847SMark.Shellenbaum@Sun.COM 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
15607876SMark.Shellenbaum@Sun.COM 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
15615331Samw 		return (EINVAL);
15625331Samw 
15635367Sahrens 	ZFS_ENTER(zfsvfs);
15645367Sahrens 	ZFS_VERIFY_ZP(dzp);
15655326Sek110237 	zilog = zfsvfs->z_log;
1566789Sahrens 
1567789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1568789Sahrens 		ZFS_EXIT(zfsvfs);
1569789Sahrens 		return (EINVAL);
1570789Sahrens 	}
15715331Samw 
15725498Stimh 	if (zfsvfs->z_utf8 && u8_validate(dirname,
15735331Samw 	    strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
15745331Samw 		ZFS_EXIT(zfsvfs);
15755331Samw 		return (EILSEQ);
15765331Samw 	}
15775331Samw 	if (flags & FIGNORECASE)
15785331Samw 		zf |= ZCILOOK;
15795331Samw 
15805331Samw 	if (vap->va_mask & AT_XVATTR)
15815331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
15825331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
15835331Samw 			ZFS_EXIT(zfsvfs);
15845331Samw 			return (error);
15855331Samw 		}
1586789Sahrens 
1587789Sahrens 	/*
1588789Sahrens 	 * First make sure the new directory doesn't exist.
1589789Sahrens 	 */
15905331Samw top:
15915331Samw 	*vpp = NULL;
15925331Samw 
15935331Samw 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
15945331Samw 	    NULL, NULL)) {
1595789Sahrens 		ZFS_EXIT(zfsvfs);
1596789Sahrens 		return (error);
1597789Sahrens 	}
1598789Sahrens 
15995331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
16001231Smarks 		zfs_dirent_unlock(dl);
16011231Smarks 		ZFS_EXIT(zfsvfs);
16021231Smarks 		return (error);
16031231Smarks 	}
16041231Smarks 
16059179SMark.Shellenbaum@Sun.COM 	if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
16069179SMark.Shellenbaum@Sun.COM 	    &acl_ids)) != 0) {
16079179SMark.Shellenbaum@Sun.COM 		zfs_dirent_unlock(dl);
16089179SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
16099179SMark.Shellenbaum@Sun.COM 		return (error);
16105331Samw 	}
1611*9396SMatthew.Ahrens@Sun.COM 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1612*9396SMatthew.Ahrens@Sun.COM 		zfs_dirent_unlock(dl);
1613*9396SMatthew.Ahrens@Sun.COM 		ZFS_EXIT(zfsvfs);
1614*9396SMatthew.Ahrens@Sun.COM 		return (EDQUOT);
1615*9396SMatthew.Ahrens@Sun.COM 	}
16169179SMark.Shellenbaum@Sun.COM 
1617789Sahrens 	/*
1618789Sahrens 	 * Add a new entry to the directory.
1619789Sahrens 	 */
1620789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
16211544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
16221544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
16239179SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
1624*9396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
1625*9396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
16269179SMark.Shellenbaum@Sun.COM 	if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE)
1627789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1628789Sahrens 		    0, SPA_MAXBLOCKSIZE);
16298227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1630789Sahrens 	if (error) {
16319179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
1632789Sahrens 		zfs_dirent_unlock(dl);
16338227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
16342113Sahrens 			dmu_tx_wait(tx);
16352113Sahrens 			dmu_tx_abort(tx);
1636789Sahrens 			goto top;
1637789Sahrens 		}
16382113Sahrens 		dmu_tx_abort(tx);
1639789Sahrens 		ZFS_EXIT(zfsvfs);
1640789Sahrens 		return (error);
1641789Sahrens 	}
1642789Sahrens 
1643789Sahrens 	/*
1644789Sahrens 	 * Create new node.
1645789Sahrens 	 */
16469179SMark.Shellenbaum@Sun.COM 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids);
16479179SMark.Shellenbaum@Sun.COM 
16489179SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
16499179SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
1650789Sahrens 	/*
1651789Sahrens 	 * Now put new name in parent dir.
1652789Sahrens 	 */
1653789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1654789Sahrens 
1655789Sahrens 	*vpp = ZTOV(zp);
1656789Sahrens 
16575331Samw 	txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
16585331Samw 	if (flags & FIGNORECASE)
16595331Samw 		txtype |= TX_CI;
16609179SMark.Shellenbaum@Sun.COM 	zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
16619179SMark.Shellenbaum@Sun.COM 	    acl_ids.z_fuidp, vap);
16629179SMark.Shellenbaum@Sun.COM 
16639179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_free(&acl_ids);
1664789Sahrens 	dmu_tx_commit(tx);
1665789Sahrens 
1666789Sahrens 	zfs_dirent_unlock(dl);
1667789Sahrens 
1668789Sahrens 	ZFS_EXIT(zfsvfs);
1669789Sahrens 	return (0);
1670789Sahrens }
1671789Sahrens 
1672789Sahrens /*
1673789Sahrens  * Remove a directory subdir entry.  If the current working
1674789Sahrens  * directory is the same as the subdir to be removed, the
1675789Sahrens  * remove will fail.
1676789Sahrens  *
1677789Sahrens  *	IN:	dvp	- vnode of directory to remove from.
1678789Sahrens  *		name	- name of directory to be removed.
1679789Sahrens  *		cwd	- vnode of current working directory.
1680789Sahrens  *		cr	- credentials of caller.
16815331Samw  *		ct	- caller context
16825331Samw  *		flags	- case flags
1683789Sahrens  *
1684789Sahrens  *	RETURN:	0 if success
1685789Sahrens  *		error code if failure
1686789Sahrens  *
1687789Sahrens  * Timestamps:
1688789Sahrens  *	dvp - ctime|mtime updated
1689789Sahrens  */
16905331Samw /*ARGSUSED*/
1691789Sahrens static int
16925331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
16935331Samw     caller_context_t *ct, int flags)
1694789Sahrens {
1695789Sahrens 	znode_t		*dzp = VTOZ(dvp);
1696789Sahrens 	znode_t		*zp;
1697789Sahrens 	vnode_t		*vp;
1698789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
16995326Sek110237 	zilog_t		*zilog;
1700789Sahrens 	zfs_dirlock_t	*dl;
1701789Sahrens 	dmu_tx_t	*tx;
1702789Sahrens 	int		error;
17035331Samw 	int		zflg = ZEXISTS;
1704789Sahrens 
17055367Sahrens 	ZFS_ENTER(zfsvfs);
17065367Sahrens 	ZFS_VERIFY_ZP(dzp);
17075326Sek110237 	zilog = zfsvfs->z_log;
1708789Sahrens 
17095331Samw 	if (flags & FIGNORECASE)
17105331Samw 		zflg |= ZCILOOK;
1711789Sahrens top:
1712789Sahrens 	zp = NULL;
1713789Sahrens 
1714789Sahrens 	/*
1715789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1716789Sahrens 	 */
17175331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
17185331Samw 	    NULL, NULL)) {
1719789Sahrens 		ZFS_EXIT(zfsvfs);
1720789Sahrens 		return (error);
1721789Sahrens 	}
1722789Sahrens 
1723789Sahrens 	vp = ZTOV(zp);
1724789Sahrens 
1725789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1726789Sahrens 		goto out;
1727789Sahrens 	}
1728789Sahrens 
1729789Sahrens 	if (vp->v_type != VDIR) {
1730789Sahrens 		error = ENOTDIR;
1731789Sahrens 		goto out;
1732789Sahrens 	}
1733789Sahrens 
1734789Sahrens 	if (vp == cwd) {
1735789Sahrens 		error = EINVAL;
1736789Sahrens 		goto out;
1737789Sahrens 	}
1738789Sahrens 
17395331Samw 	vnevent_rmdir(vp, dvp, name, ct);
1740789Sahrens 
1741789Sahrens 	/*
17423897Smaybee 	 * Grab a lock on the directory to make sure that noone is
17433897Smaybee 	 * trying to add (or lookup) entries while we are removing it.
17443897Smaybee 	 */
17453897Smaybee 	rw_enter(&zp->z_name_lock, RW_WRITER);
17463897Smaybee 
17473897Smaybee 	/*
17483897Smaybee 	 * Grab a lock on the parent pointer to make sure we play well
1749789Sahrens 	 * with the treewalk and directory rename code.
1750789Sahrens 	 */
1751789Sahrens 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1752789Sahrens 
1753789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
17541544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1755789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
17563461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
17578227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1758789Sahrens 	if (error) {
1759789Sahrens 		rw_exit(&zp->z_parent_lock);
17603897Smaybee 		rw_exit(&zp->z_name_lock);
1761789Sahrens 		zfs_dirent_unlock(dl);
1762789Sahrens 		VN_RELE(vp);
17638227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
17642113Sahrens 			dmu_tx_wait(tx);
17652113Sahrens 			dmu_tx_abort(tx);
1766789Sahrens 			goto top;
1767789Sahrens 		}
17682113Sahrens 		dmu_tx_abort(tx);
1769789Sahrens 		ZFS_EXIT(zfsvfs);
1770789Sahrens 		return (error);
1771789Sahrens 	}
1772789Sahrens 
17735331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
17745331Samw 
17755331Samw 	if (error == 0) {
17765331Samw 		uint64_t txtype = TX_RMDIR;
17775331Samw 		if (flags & FIGNORECASE)
17785331Samw 			txtype |= TX_CI;
17795331Samw 		zfs_log_remove(zilog, tx, txtype, dzp, name);
17805331Samw 	}
1781789Sahrens 
1782789Sahrens 	dmu_tx_commit(tx);
1783789Sahrens 
1784789Sahrens 	rw_exit(&zp->z_parent_lock);
17853897Smaybee 	rw_exit(&zp->z_name_lock);
1786789Sahrens out:
1787789Sahrens 	zfs_dirent_unlock(dl);
1788789Sahrens 
1789789Sahrens 	VN_RELE(vp);
1790789Sahrens 
1791789Sahrens 	ZFS_EXIT(zfsvfs);
1792789Sahrens 	return (error);
1793789Sahrens }
1794789Sahrens 
1795789Sahrens /*
1796789Sahrens  * Read as many directory entries as will fit into the provided
1797789Sahrens  * buffer from the given directory cursor position (specified in
1798789Sahrens  * the uio structure.
1799789Sahrens  *
1800789Sahrens  *	IN:	vp	- vnode of directory to read.
1801789Sahrens  *		uio	- structure supplying read location, range info,
1802789Sahrens  *			  and return buffer.
1803789Sahrens  *		cr	- credentials of caller.
18045331Samw  *		ct	- caller context
18055331Samw  *		flags	- case flags
1806789Sahrens  *
1807789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
1808789Sahrens  *		eofp	- set to true if end-of-file detected.
1809789Sahrens  *
1810789Sahrens  *	RETURN:	0 if success
1811789Sahrens  *		error code if failure
1812789Sahrens  *
1813789Sahrens  * Timestamps:
1814789Sahrens  *	vp - atime updated
1815789Sahrens  *
1816789Sahrens  * Note that the low 4 bits of the cookie returned by zap is always zero.
1817789Sahrens  * This allows us to use the low range for "special" directory entries:
1818789Sahrens  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
1819789Sahrens  * we use the offset 2 for the '.zfs' directory.
1820789Sahrens  */
1821789Sahrens /* ARGSUSED */
1822789Sahrens static int
18235331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
18245331Samw     caller_context_t *ct, int flags)
1825789Sahrens {
1826789Sahrens 	znode_t		*zp = VTOZ(vp);
1827789Sahrens 	iovec_t		*iovp;
18285331Samw 	edirent_t	*eodp;
1829789Sahrens 	dirent64_t	*odp;
1830789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1831869Sperrin 	objset_t	*os;
1832789Sahrens 	caddr_t		outbuf;
1833789Sahrens 	size_t		bufsize;
1834789Sahrens 	zap_cursor_t	zc;
1835789Sahrens 	zap_attribute_t	zap;
1836789Sahrens 	uint_t		bytes_wanted;
1837789Sahrens 	uint64_t	offset; /* must be unsigned; checks for < 1 */
1838789Sahrens 	int		local_eof;
1839869Sperrin 	int		outcount;
1840869Sperrin 	int		error;
1841869Sperrin 	uint8_t		prefetch;
18425663Sck153898 	boolean_t	check_sysattrs;
1843789Sahrens 
18445367Sahrens 	ZFS_ENTER(zfsvfs);
18455367Sahrens 	ZFS_VERIFY_ZP(zp);
1846789Sahrens 
1847789Sahrens 	/*
1848789Sahrens 	 * If we are not given an eof variable,
1849789Sahrens 	 * use a local one.
1850789Sahrens 	 */
1851789Sahrens 	if (eofp == NULL)
1852789Sahrens 		eofp = &local_eof;
1853789Sahrens 
1854789Sahrens 	/*
1855789Sahrens 	 * Check for valid iov_len.
1856789Sahrens 	 */
1857789Sahrens 	if (uio->uio_iov->iov_len <= 0) {
1858789Sahrens 		ZFS_EXIT(zfsvfs);
1859789Sahrens 		return (EINVAL);
1860789Sahrens 	}
1861789Sahrens 
1862789Sahrens 	/*
1863789Sahrens 	 * Quit if directory has been removed (posix)
1864789Sahrens 	 */
18653461Sahrens 	if ((*eofp = zp->z_unlinked) != 0) {
1866789Sahrens 		ZFS_EXIT(zfsvfs);
1867789Sahrens 		return (0);
1868789Sahrens 	}
1869789Sahrens 
1870869Sperrin 	error = 0;
1871869Sperrin 	os = zfsvfs->z_os;
1872869Sperrin 	offset = uio->uio_loffset;
1873869Sperrin 	prefetch = zp->z_zn_prefetch;
1874869Sperrin 
1875789Sahrens 	/*
1876789Sahrens 	 * Initialize the iterator cursor.
1877789Sahrens 	 */
1878789Sahrens 	if (offset <= 3) {
1879789Sahrens 		/*
1880789Sahrens 		 * Start iteration from the beginning of the directory.
1881789Sahrens 		 */
1882869Sperrin 		zap_cursor_init(&zc, os, zp->z_id);
1883789Sahrens 	} else {
1884789Sahrens 		/*
1885789Sahrens 		 * The offset is a serialized cursor.
1886789Sahrens 		 */
1887869Sperrin 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1888789Sahrens 	}
1889789Sahrens 
1890789Sahrens 	/*
1891789Sahrens 	 * Get space to change directory entries into fs independent format.
1892789Sahrens 	 */
1893789Sahrens 	iovp = uio->uio_iov;
1894789Sahrens 	bytes_wanted = iovp->iov_len;
1895789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
1896789Sahrens 		bufsize = bytes_wanted;
1897789Sahrens 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
1898789Sahrens 		odp = (struct dirent64 *)outbuf;
1899789Sahrens 	} else {
1900789Sahrens 		bufsize = bytes_wanted;
1901789Sahrens 		odp = (struct dirent64 *)iovp->iov_base;
1902789Sahrens 	}
19035331Samw 	eodp = (struct edirent *)odp;
1904789Sahrens 
1905789Sahrens 	/*
19067757SJanice.Chang@Sun.COM 	 * If this VFS supports the system attribute view interface; and
19077757SJanice.Chang@Sun.COM 	 * we're looking at an extended attribute directory; and we care
19087757SJanice.Chang@Sun.COM 	 * about normalization conflicts on this vfs; then we must check
19097757SJanice.Chang@Sun.COM 	 * for normalization conflicts with the sysattr name space.
19105663Sck153898 	 */
19117757SJanice.Chang@Sun.COM 	check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
19125663Sck153898 	    (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
19135663Sck153898 	    (flags & V_RDDIR_ENTFLAGS);
19145663Sck153898 
19155663Sck153898 	/*
1916789Sahrens 	 * Transform to file-system independent format
1917789Sahrens 	 */
1918789Sahrens 	outcount = 0;
1919789Sahrens 	while (outcount < bytes_wanted) {
19203912Slling 		ino64_t objnum;
19213912Slling 		ushort_t reclen;
19223912Slling 		off64_t *next;
19233912Slling 
1924789Sahrens 		/*
1925789Sahrens 		 * Special case `.', `..', and `.zfs'.
1926789Sahrens 		 */
1927789Sahrens 		if (offset == 0) {
1928789Sahrens 			(void) strcpy(zap.za_name, ".");
19295331Samw 			zap.za_normalization_conflict = 0;
19303912Slling 			objnum = zp->z_id;
1931789Sahrens 		} else if (offset == 1) {
1932789Sahrens 			(void) strcpy(zap.za_name, "..");
19335331Samw 			zap.za_normalization_conflict = 0;
19343912Slling 			objnum = zp->z_phys->zp_parent;
1935789Sahrens 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
1936789Sahrens 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
19375331Samw 			zap.za_normalization_conflict = 0;
19383912Slling 			objnum = ZFSCTL_INO_ROOT;
1939789Sahrens 		} else {
1940789Sahrens 			/*
1941789Sahrens 			 * Grab next entry.
1942789Sahrens 			 */
1943789Sahrens 			if (error = zap_cursor_retrieve(&zc, &zap)) {
1944789Sahrens 				if ((*eofp = (error == ENOENT)) != 0)
1945789Sahrens 					break;
1946789Sahrens 				else
1947789Sahrens 					goto update;
1948789Sahrens 			}
1949789Sahrens 
1950789Sahrens 			if (zap.za_integer_length != 8 ||
1951789Sahrens 			    zap.za_num_integers != 1) {
1952789Sahrens 				cmn_err(CE_WARN, "zap_readdir: bad directory "
1953789Sahrens 				    "entry, obj = %lld, offset = %lld\n",
1954789Sahrens 				    (u_longlong_t)zp->z_id,
1955789Sahrens 				    (u_longlong_t)offset);
1956789Sahrens 				error = ENXIO;
1957789Sahrens 				goto update;
1958789Sahrens 			}
19593912Slling 
19603912Slling 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
19613912Slling 			/*
19623912Slling 			 * MacOS X can extract the object type here such as:
19633912Slling 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
19643912Slling 			 */
19655663Sck153898 
19665663Sck153898 			if (check_sysattrs && !zap.za_normalization_conflict) {
19675663Sck153898 				zap.za_normalization_conflict =
19685663Sck153898 				    xattr_sysattr_casechk(zap.za_name);
19695663Sck153898 			}
1970789Sahrens 		}
19715331Samw 
19725331Samw 		if (flags & V_RDDIR_ENTFLAGS)
19735331Samw 			reclen = EDIRENT_RECLEN(strlen(zap.za_name));
19745331Samw 		else
19755331Samw 			reclen = DIRENT64_RECLEN(strlen(zap.za_name));
1976789Sahrens 
1977789Sahrens 		/*
1978789Sahrens 		 * Will this entry fit in the buffer?
1979789Sahrens 		 */
19803912Slling 		if (outcount + reclen > bufsize) {
1981789Sahrens 			/*
1982789Sahrens 			 * Did we manage to fit anything in the buffer?
1983789Sahrens 			 */
1984789Sahrens 			if (!outcount) {
1985789Sahrens 				error = EINVAL;
1986789Sahrens 				goto update;
1987789Sahrens 			}
1988789Sahrens 			break;
1989789Sahrens 		}
19905331Samw 		if (flags & V_RDDIR_ENTFLAGS) {
19915331Samw 			/*
19925331Samw 			 * Add extended flag entry:
19935331Samw 			 */
19945331Samw 			eodp->ed_ino = objnum;
19955331Samw 			eodp->ed_reclen = reclen;
19965331Samw 			/* NOTE: ed_off is the offset for the *next* entry */
19975331Samw 			next = &(eodp->ed_off);
19985331Samw 			eodp->ed_eflags = zap.za_normalization_conflict ?
19995331Samw 			    ED_CASE_CONFLICT : 0;
20005331Samw 			(void) strncpy(eodp->ed_name, zap.za_name,
20015331Samw 			    EDIRENT_NAMELEN(reclen));
20025331Samw 			eodp = (edirent_t *)((intptr_t)eodp + reclen);
20035331Samw 		} else {
20045331Samw 			/*
20055331Samw 			 * Add normal entry:
20065331Samw 			 */
20075331Samw 			odp->d_ino = objnum;
20085331Samw 			odp->d_reclen = reclen;
20095331Samw 			/* NOTE: d_off is the offset for the *next* entry */
20105331Samw 			next = &(odp->d_off);
20115331Samw 			(void) strncpy(odp->d_name, zap.za_name,
20125331Samw 			    DIRENT64_NAMELEN(reclen));
20135331Samw 			odp = (dirent64_t *)((intptr_t)odp + reclen);
20145331Samw 		}
20153912Slling 		outcount += reclen;
2016789Sahrens 
2017789Sahrens 		ASSERT(outcount <= bufsize);
2018789Sahrens 
2019789Sahrens 		/* Prefetch znode */
2020869Sperrin 		if (prefetch)
20213912Slling 			dmu_prefetch(os, objnum, 0, 0);
2022789Sahrens 
2023789Sahrens 		/*
2024789Sahrens 		 * Move to the next entry, fill in the previous offset.
2025789Sahrens 		 */
2026789Sahrens 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2027789Sahrens 			zap_cursor_advance(&zc);
2028789Sahrens 			offset = zap_cursor_serialize(&zc);
2029789Sahrens 		} else {
2030789Sahrens 			offset += 1;
2031789Sahrens 		}
2032789Sahrens 		*next = offset;
2033789Sahrens 	}
2034869Sperrin 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2035789Sahrens 
2036789Sahrens 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2037789Sahrens 		iovp->iov_base += outcount;
2038789Sahrens 		iovp->iov_len -= outcount;
2039789Sahrens 		uio->uio_resid -= outcount;
2040789Sahrens 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2041789Sahrens 		/*
2042789Sahrens 		 * Reset the pointer.
2043789Sahrens 		 */
2044789Sahrens 		offset = uio->uio_loffset;
2045789Sahrens 	}
2046789Sahrens 
2047789Sahrens update:
2048885Sahrens 	zap_cursor_fini(&zc);
2049789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2050789Sahrens 		kmem_free(outbuf, bufsize);
2051789Sahrens 
2052789Sahrens 	if (error == ENOENT)
2053789Sahrens 		error = 0;
2054789Sahrens 
2055789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2056789Sahrens 
2057789Sahrens 	uio->uio_loffset = offset;
2058789Sahrens 	ZFS_EXIT(zfsvfs);
2059789Sahrens 	return (error);
2060789Sahrens }
2061789Sahrens 
20624720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4;
20634720Sfr157268 
2064789Sahrens static int
20655331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2066789Sahrens {
2067789Sahrens 	znode_t	*zp = VTOZ(vp);
2068789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2069789Sahrens 
20701773Seschrock 	/*
20711773Seschrock 	 * Regardless of whether this is required for standards conformance,
20721773Seschrock 	 * this is the logical behavior when fsync() is called on a file with
20731773Seschrock 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
20741773Seschrock 	 * going to be pushed out as part of the zil_commit().
20751773Seschrock 	 */
20761773Seschrock 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
20771773Seschrock 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
20785331Samw 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
20791773Seschrock 
20804720Sfr157268 	(void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
20814720Sfr157268 
20825367Sahrens 	ZFS_ENTER(zfsvfs);
20835367Sahrens 	ZFS_VERIFY_ZP(zp);
20842638Sperrin 	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
2085789Sahrens 	ZFS_EXIT(zfsvfs);
2086789Sahrens 	return (0);
2087789Sahrens }
2088789Sahrens 
20895331Samw 
2090789Sahrens /*
2091789Sahrens  * Get the requested file attributes and place them in the provided
2092789Sahrens  * vattr structure.
2093789Sahrens  *
2094789Sahrens  *	IN:	vp	- vnode of file.
2095789Sahrens  *		vap	- va_mask identifies requested attributes.
20965331Samw  *			  If AT_XVATTR set, then optional attrs are requested
20975331Samw  *		flags	- ATTR_NOACLCHECK (CIFS server context)
2098789Sahrens  *		cr	- credentials of caller.
20995331Samw  *		ct	- caller context
2100789Sahrens  *
2101789Sahrens  *	OUT:	vap	- attribute values.
2102789Sahrens  *
2103789Sahrens  *	RETURN:	0 (always succeeds)
2104789Sahrens  */
2105789Sahrens /* ARGSUSED */
2106789Sahrens static int
21075331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
21085331Samw     caller_context_t *ct)
2109789Sahrens {
2110789Sahrens 	znode_t *zp = VTOZ(vp);
2111789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
21125326Sek110237 	znode_phys_t *pzp;
21135331Samw 	int	error = 0;
21144543Smarks 	uint64_t links;
21155331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
21165331Samw 	xoptattr_t *xoap = NULL;
21175331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2118789Sahrens 
21195367Sahrens 	ZFS_ENTER(zfsvfs);
21205367Sahrens 	ZFS_VERIFY_ZP(zp);
21215326Sek110237 	pzp = zp->z_phys;
2122789Sahrens 
21235331Samw 	mutex_enter(&zp->z_lock);
21245331Samw 
21255331Samw 	/*
21265331Samw 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
21275331Samw 	 * Also, if we are the owner don't bother, since owner should
21285331Samw 	 * always be allowed to read basic attributes of file.
21295331Samw 	 */
21305331Samw 	if (!(pzp->zp_flags & ZFS_ACL_TRIVIAL) &&
21315331Samw 	    (pzp->zp_uid != crgetuid(cr))) {
21325331Samw 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
21335331Samw 		    skipaclchk, cr)) {
21345331Samw 			mutex_exit(&zp->z_lock);
21355331Samw 			ZFS_EXIT(zfsvfs);
21365331Samw 			return (error);
21375331Samw 		}
21385331Samw 	}
21395331Samw 
2140789Sahrens 	/*
2141789Sahrens 	 * Return all attributes.  It's cheaper to provide the answer
2142789Sahrens 	 * than to determine whether we were asked the question.
2143789Sahrens 	 */
2144789Sahrens 
2145789Sahrens 	vap->va_type = vp->v_type;
2146789Sahrens 	vap->va_mode = pzp->zp_mode & MODEMASK;
21475771Sjp151216 	zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2148789Sahrens 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2149789Sahrens 	vap->va_nodeid = zp->z_id;
21504543Smarks 	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
21514543Smarks 		links = pzp->zp_links + 1;
21524543Smarks 	else
21534543Smarks 		links = pzp->zp_links;
21544543Smarks 	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
2155789Sahrens 	vap->va_size = pzp->zp_size;
21561816Smarks 	vap->va_rdev = vp->v_rdev;
2157789Sahrens 	vap->va_seq = zp->z_seq;
2158789Sahrens 
21595331Samw 	/*
21605331Samw 	 * Add in any requested optional attributes and the create time.
21615331Samw 	 * Also set the corresponding bits in the returned attribute bitmap.
21625331Samw 	 */
21635331Samw 	if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
21645331Samw 		if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
21655331Samw 			xoap->xoa_archive =
21665331Samw 			    ((pzp->zp_flags & ZFS_ARCHIVE) != 0);
21675331Samw 			XVA_SET_RTN(xvap, XAT_ARCHIVE);
21685331Samw 		}
21695331Samw 
21705331Samw 		if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
21715331Samw 			xoap->xoa_readonly =
21725331Samw 			    ((pzp->zp_flags & ZFS_READONLY) != 0);
21735331Samw 			XVA_SET_RTN(xvap, XAT_READONLY);
21745331Samw 		}
21755331Samw 
21765331Samw 		if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
21775331Samw 			xoap->xoa_system =
21785331Samw 			    ((pzp->zp_flags & ZFS_SYSTEM) != 0);
21795331Samw 			XVA_SET_RTN(xvap, XAT_SYSTEM);
21805331Samw 		}
21815331Samw 
21825331Samw 		if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
21835331Samw 			xoap->xoa_hidden =
21845331Samw 			    ((pzp->zp_flags & ZFS_HIDDEN) != 0);
21855331Samw 			XVA_SET_RTN(xvap, XAT_HIDDEN);
21865331Samw 		}
21875331Samw 
21885331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
21895331Samw 			xoap->xoa_nounlink =
21905331Samw 			    ((pzp->zp_flags & ZFS_NOUNLINK) != 0);
21915331Samw 			XVA_SET_RTN(xvap, XAT_NOUNLINK);
21925331Samw 		}
21935331Samw 
21945331Samw 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
21955331Samw 			xoap->xoa_immutable =
21965331Samw 			    ((pzp->zp_flags & ZFS_IMMUTABLE) != 0);
21975331Samw 			XVA_SET_RTN(xvap, XAT_IMMUTABLE);
21985331Samw 		}
21995331Samw 
22005331Samw 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
22015331Samw 			xoap->xoa_appendonly =
22025331Samw 			    ((pzp->zp_flags & ZFS_APPENDONLY) != 0);
22035331Samw 			XVA_SET_RTN(xvap, XAT_APPENDONLY);
22045331Samw 		}
22055331Samw 
22065331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
22075331Samw 			xoap->xoa_nodump =
22085331Samw 			    ((pzp->zp_flags & ZFS_NODUMP) != 0);
22095331Samw 			XVA_SET_RTN(xvap, XAT_NODUMP);
22105331Samw 		}
22115331Samw 
22125331Samw 		if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
22135331Samw 			xoap->xoa_opaque =
22145331Samw 			    ((pzp->zp_flags & ZFS_OPAQUE) != 0);
22155331Samw 			XVA_SET_RTN(xvap, XAT_OPAQUE);
22165331Samw 		}
22175331Samw 
22185331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
22195331Samw 			xoap->xoa_av_quarantined =
22205331Samw 			    ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0);
22215331Samw 			XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
22225331Samw 		}
22235331Samw 
22245331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
22255331Samw 			xoap->xoa_av_modified =
22265331Samw 			    ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0);
22275331Samw 			XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
22285331Samw 		}
22295331Samw 
22305331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
22315331Samw 		    vp->v_type == VREG &&
22325331Samw 		    (pzp->zp_flags & ZFS_BONUS_SCANSTAMP)) {
22335331Samw 			size_t len;
22345331Samw 			dmu_object_info_t doi;
22355331Samw 
22365331Samw 			/*
22375331Samw 			 * Only VREG files have anti-virus scanstamps, so we
22385331Samw 			 * won't conflict with symlinks in the bonus buffer.
22395331Samw 			 */
22405331Samw 			dmu_object_info_from_db(zp->z_dbuf, &doi);
22415331Samw 			len = sizeof (xoap->xoa_av_scanstamp) +
22425331Samw 			    sizeof (znode_phys_t);
22435331Samw 			if (len <= doi.doi_bonus_size) {
22445331Samw 				/*
22455331Samw 				 * pzp points to the start of the
22465331Samw 				 * znode_phys_t. pzp + 1 points to the
22475331Samw 				 * first byte after the znode_phys_t.
22485331Samw 				 */
22495331Samw 				(void) memcpy(xoap->xoa_av_scanstamp,
22505331Samw 				    pzp + 1,
22515331Samw 				    sizeof (xoap->xoa_av_scanstamp));
22525331Samw 				XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
22535331Samw 			}
22545331Samw 		}
22555331Samw 
22565331Samw 		if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
22575331Samw 			ZFS_TIME_DECODE(&xoap->xoa_createtime, pzp->zp_crtime);
22585331Samw 			XVA_SET_RTN(xvap, XAT_CREATETIME);
22595331Samw 		}
22605331Samw 	}
22615331Samw 
2262789Sahrens 	ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
2263789Sahrens 	ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
2264789Sahrens 	ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
2265789Sahrens 
2266789Sahrens 	mutex_exit(&zp->z_lock);
2267789Sahrens 
2268789Sahrens 	dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks);
2269789Sahrens 
2270789Sahrens 	if (zp->z_blksz == 0) {
2271789Sahrens 		/*
2272789Sahrens 		 * Block size hasn't been set; suggest maximal I/O transfers.
2273789Sahrens 		 */
2274789Sahrens 		vap->va_blksize = zfsvfs->z_max_blksz;
2275789Sahrens 	}
2276789Sahrens 
2277789Sahrens 	ZFS_EXIT(zfsvfs);
2278789Sahrens 	return (0);
2279789Sahrens }
2280789Sahrens 
2281789Sahrens /*
2282789Sahrens  * Set the file attributes to the values contained in the
2283789Sahrens  * vattr structure.
2284789Sahrens  *
2285789Sahrens  *	IN:	vp	- vnode of file to be modified.
2286789Sahrens  *		vap	- new attribute values.
22875331Samw  *			  If AT_XVATTR set, then optional attrs are being set
2288789Sahrens  *		flags	- ATTR_UTIME set if non-default time values provided.
22895331Samw  *			- ATTR_NOACLCHECK (CIFS context only).
2290789Sahrens  *		cr	- credentials of caller.
22915331Samw  *		ct	- caller context
2292789Sahrens  *
2293789Sahrens  *	RETURN:	0 if success
2294789Sahrens  *		error code if failure
2295789Sahrens  *
2296789Sahrens  * Timestamps:
2297789Sahrens  *	vp - ctime updated, mtime updated if size changed.
2298789Sahrens  */
2299789Sahrens /* ARGSUSED */
2300789Sahrens static int
2301789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2302789Sahrens 	caller_context_t *ct)
2303789Sahrens {
23045326Sek110237 	znode_t		*zp = VTOZ(vp);
23055326Sek110237 	znode_phys_t	*pzp;
2306789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
23075326Sek110237 	zilog_t		*zilog;
2308789Sahrens 	dmu_tx_t	*tx;
23091878Smaybee 	vattr_t		oldva;
23108190SMark.Shellenbaum@Sun.COM 	xvattr_t	tmpxvattr;
2311789Sahrens 	uint_t		mask = vap->va_mask;
23121878Smaybee 	uint_t		saved_mask;
23132796Smarks 	int		trim_mask = 0;
2314789Sahrens 	uint64_t	new_mode;
23159179SMark.Shellenbaum@Sun.COM 	uint64_t	new_uid, new_gid;
23161231Smarks 	znode_t		*attrzp;
2317789Sahrens 	int		need_policy = FALSE;
2318789Sahrens 	int		err;
23195331Samw 	zfs_fuid_info_t *fuidp = NULL;
23205331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
23215331Samw 	xoptattr_t	*xoap;
23225824Smarks 	zfs_acl_t	*aclp = NULL;
23235331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
23249179SMark.Shellenbaum@Sun.COM 	boolean_t fuid_dirtied = B_FALSE;
2325789Sahrens 
2326789Sahrens 	if (mask == 0)
2327789Sahrens 		return (0);
2328789Sahrens 
2329789Sahrens 	if (mask & AT_NOSET)
2330789Sahrens 		return (EINVAL);
2331789Sahrens 
23325367Sahrens 	ZFS_ENTER(zfsvfs);
23335367Sahrens 	ZFS_VERIFY_ZP(zp);
23345331Samw 
23355331Samw 	pzp = zp->z_phys;
23365331Samw 	zilog = zfsvfs->z_log;
23375331Samw 
23385331Samw 	/*
23395331Samw 	 * Make sure that if we have ephemeral uid/gid or xvattr specified
23405331Samw 	 * that file system is at proper version level
23415331Samw 	 */
23425331Samw 
23435331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
23445331Samw 	    (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
23455331Samw 	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
23465386Stimh 	    (mask & AT_XVATTR))) {
23475386Stimh 		ZFS_EXIT(zfsvfs);
23485331Samw 		return (EINVAL);
23495386Stimh 	}
23505386Stimh 
23515386Stimh 	if (mask & AT_SIZE && vp->v_type == VDIR) {
23525386Stimh 		ZFS_EXIT(zfsvfs);
2353789Sahrens 		return (EISDIR);
23545386Stimh 	}
23555386Stimh 
23565386Stimh 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
23575386Stimh 		ZFS_EXIT(zfsvfs);
23581308Smarks 		return (EINVAL);
23595386Stimh 	}
23601308Smarks 
23615331Samw 	/*
23625331Samw 	 * If this is an xvattr_t, then get a pointer to the structure of
23635331Samw 	 * optional attributes.  If this is NULL, then we have a vattr_t.
23645331Samw 	 */
23655331Samw 	xoap = xva_getxoptattr(xvap);
23665331Samw 
23678190SMark.Shellenbaum@Sun.COM 	xva_init(&tmpxvattr);
23688190SMark.Shellenbaum@Sun.COM 
23695331Samw 	/*
23705331Samw 	 * Immutable files can only alter immutable bit and atime
23715331Samw 	 */
23725331Samw 	if ((pzp->zp_flags & ZFS_IMMUTABLE) &&
23735331Samw 	    ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
23745386Stimh 	    ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
23755386Stimh 		ZFS_EXIT(zfsvfs);
23765331Samw 		return (EPERM);
23775386Stimh 	}
23785386Stimh 
23795386Stimh 	if ((mask & AT_SIZE) && (pzp->zp_flags & ZFS_READONLY)) {
23805386Stimh 		ZFS_EXIT(zfsvfs);
23815331Samw 		return (EPERM);
23825386Stimh 	}
2383789Sahrens 
23846064Smarks 	/*
23856064Smarks 	 * Verify timestamps doesn't overflow 32 bits.
23866064Smarks 	 * ZFS can handle large timestamps, but 32bit syscalls can't
23876064Smarks 	 * handle times greater than 2039.  This check should be removed
23886064Smarks 	 * once large timestamps are fully supported.
23896064Smarks 	 */
23906064Smarks 	if (mask & (AT_ATIME | AT_MTIME)) {
23916064Smarks 		if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
23926064Smarks 		    ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
23936064Smarks 			ZFS_EXIT(zfsvfs);
23946064Smarks 			return (EOVERFLOW);
23956064Smarks 		}
23966064Smarks 	}
23976064Smarks 
2398789Sahrens top:
23991231Smarks 	attrzp = NULL;
2400789Sahrens 
2401789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2402789Sahrens 		ZFS_EXIT(zfsvfs);
2403789Sahrens 		return (EROFS);
2404789Sahrens 	}
2405789Sahrens 
2406789Sahrens 	/*
2407789Sahrens 	 * First validate permissions
2408789Sahrens 	 */
2409789Sahrens 
2410789Sahrens 	if (mask & AT_SIZE) {
24115331Samw 		err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2412789Sahrens 		if (err) {
2413789Sahrens 			ZFS_EXIT(zfsvfs);
2414789Sahrens 			return (err);
2415789Sahrens 		}
24161878Smaybee 		/*
24171878Smaybee 		 * XXX - Note, we are not providing any open
24181878Smaybee 		 * mode flags here (like FNDELAY), so we may
24191878Smaybee 		 * block if there are locks present... this
24201878Smaybee 		 * should be addressed in openat().
24211878Smaybee 		 */
24226992Smaybee 		/* XXX - would it be OK to generate a log record here? */
24236992Smaybee 		err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
24241878Smaybee 		if (err) {
24251878Smaybee 			ZFS_EXIT(zfsvfs);
24261878Smaybee 			return (err);
24271878Smaybee 		}
2428789Sahrens 	}
2429789Sahrens 
24305331Samw 	if (mask & (AT_ATIME|AT_MTIME) ||
24315331Samw 	    ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
24325331Samw 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
24335331Samw 	    XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
24345331Samw 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
24355331Samw 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM))))
24365331Samw 		need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
24375331Samw 		    skipaclchk, cr);
2438789Sahrens 
2439789Sahrens 	if (mask & (AT_UID|AT_GID)) {
2440789Sahrens 		int	idmask = (mask & (AT_UID|AT_GID));
2441789Sahrens 		int	take_owner;
2442789Sahrens 		int	take_group;
2443789Sahrens 
2444789Sahrens 		/*
2445913Smarks 		 * NOTE: even if a new mode is being set,
2446913Smarks 		 * we may clear S_ISUID/S_ISGID bits.
2447913Smarks 		 */
2448913Smarks 
2449913Smarks 		if (!(mask & AT_MODE))
2450913Smarks 			vap->va_mode = pzp->zp_mode;
2451913Smarks 
2452913Smarks 		/*
2453789Sahrens 		 * Take ownership or chgrp to group we are a member of
2454789Sahrens 		 */
2455789Sahrens 
2456789Sahrens 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
24575331Samw 		take_group = (mask & AT_GID) &&
24585331Samw 		    zfs_groupmember(zfsvfs, vap->va_gid, cr);
2459789Sahrens 
2460789Sahrens 		/*
2461789Sahrens 		 * If both AT_UID and AT_GID are set then take_owner and
2462789Sahrens 		 * take_group must both be set in order to allow taking
2463789Sahrens 		 * ownership.
2464789Sahrens 		 *
2465789Sahrens 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2466789Sahrens 		 *
2467789Sahrens 		 */
2468789Sahrens 
2469789Sahrens 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2470789Sahrens 		    ((idmask == AT_UID) && take_owner) ||
2471789Sahrens 		    ((idmask == AT_GID) && take_group)) {
24725331Samw 			if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
24735331Samw 			    skipaclchk, cr) == 0) {
2474789Sahrens 				/*
2475789Sahrens 				 * Remove setuid/setgid for non-privileged users
2476789Sahrens 				 */
24771115Smarks 				secpolicy_setid_clear(vap, cr);
24782796Smarks 				trim_mask = (mask & (AT_UID|AT_GID));
2479789Sahrens 			} else {
2480789Sahrens 				need_policy =  TRUE;
2481789Sahrens 			}
2482789Sahrens 		} else {
2483789Sahrens 			need_policy =  TRUE;
2484789Sahrens 		}
2485789Sahrens 	}
2486789Sahrens 
24872796Smarks 	mutex_enter(&zp->z_lock);
24882796Smarks 	oldva.va_mode = pzp->zp_mode;
24895771Sjp151216 	zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
24905331Samw 	if (mask & AT_XVATTR) {
24918190SMark.Shellenbaum@Sun.COM 		/*
24928190SMark.Shellenbaum@Sun.COM 		 * Update xvattr mask to include only those attributes
24938190SMark.Shellenbaum@Sun.COM 		 * that are actually changing.
24948190SMark.Shellenbaum@Sun.COM 		 *
24958190SMark.Shellenbaum@Sun.COM 		 * the bits will be restored prior to actually setting
24968190SMark.Shellenbaum@Sun.COM 		 * the attributes so the caller thinks they were set.
24978190SMark.Shellenbaum@Sun.COM 		 */
24988190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
24998190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_appendonly !=
25008190SMark.Shellenbaum@Sun.COM 			    ((pzp->zp_flags & ZFS_APPENDONLY) != 0)) {
25018190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
25028190SMark.Shellenbaum@Sun.COM 			} else {
25038190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_APPENDONLY);
25048190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
25058190SMark.Shellenbaum@Sun.COM 			}
25068190SMark.Shellenbaum@Sun.COM 		}
25078190SMark.Shellenbaum@Sun.COM 
25088190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
25098190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_nounlink !=
25108190SMark.Shellenbaum@Sun.COM 			    ((pzp->zp_flags & ZFS_NOUNLINK) != 0)) {
25118190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
25128190SMark.Shellenbaum@Sun.COM 			} else {
25138190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_NOUNLINK);
25148190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
25158190SMark.Shellenbaum@Sun.COM 			}
25168190SMark.Shellenbaum@Sun.COM 		}
25178190SMark.Shellenbaum@Sun.COM 
25188190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
25198190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_immutable !=
25208190SMark.Shellenbaum@Sun.COM 			    ((pzp->zp_flags & ZFS_IMMUTABLE) != 0)) {
25218190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
25228190SMark.Shellenbaum@Sun.COM 			} else {
25238190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
25248190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
25258190SMark.Shellenbaum@Sun.COM 			}
25268190SMark.Shellenbaum@Sun.COM 		}
25278190SMark.Shellenbaum@Sun.COM 
25288190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
25298190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_nodump !=
25308190SMark.Shellenbaum@Sun.COM 			    ((pzp->zp_flags & ZFS_NODUMP) != 0)) {
25318190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
25328190SMark.Shellenbaum@Sun.COM 			} else {
25338190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_NODUMP);
25348190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
25358190SMark.Shellenbaum@Sun.COM 			}
25368190SMark.Shellenbaum@Sun.COM 		}
25378190SMark.Shellenbaum@Sun.COM 
25388190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
25398190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_av_modified !=
25408190SMark.Shellenbaum@Sun.COM 			    ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0)) {
25418190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
25428190SMark.Shellenbaum@Sun.COM 			} else {
25438190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
25448190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
25458190SMark.Shellenbaum@Sun.COM 			}
25468190SMark.Shellenbaum@Sun.COM 		}
25478190SMark.Shellenbaum@Sun.COM 
25488190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
25498190SMark.Shellenbaum@Sun.COM 			if ((vp->v_type != VREG &&
25508190SMark.Shellenbaum@Sun.COM 			    xoap->xoa_av_quarantined) ||
25518190SMark.Shellenbaum@Sun.COM 			    xoap->xoa_av_quarantined !=
25528190SMark.Shellenbaum@Sun.COM 			    ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0)) {
25538190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
25548190SMark.Shellenbaum@Sun.COM 			} else {
25558190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
25568190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
25578190SMark.Shellenbaum@Sun.COM 			}
25588190SMark.Shellenbaum@Sun.COM 		}
25598190SMark.Shellenbaum@Sun.COM 
25608190SMark.Shellenbaum@Sun.COM 		if (need_policy == FALSE &&
25618190SMark.Shellenbaum@Sun.COM 		    (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
25628190SMark.Shellenbaum@Sun.COM 		    XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
25635331Samw 			need_policy = TRUE;
25645331Samw 		}
25655331Samw 	}
25665331Samw 
25672796Smarks 	mutex_exit(&zp->z_lock);
25682796Smarks 
25692796Smarks 	if (mask & AT_MODE) {
25705331Samw 		if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
25712796Smarks 			err = secpolicy_setid_setsticky_clear(vp, vap,
25722796Smarks 			    &oldva, cr);
25732796Smarks 			if (err) {
25742796Smarks 				ZFS_EXIT(zfsvfs);
25752796Smarks 				return (err);
25762796Smarks 			}
25772796Smarks 			trim_mask |= AT_MODE;
25782796Smarks 		} else {
25792796Smarks 			need_policy = TRUE;
25802796Smarks 		}
25812796Smarks 	}
2582789Sahrens 
2583789Sahrens 	if (need_policy) {
25841115Smarks 		/*
25851115Smarks 		 * If trim_mask is set then take ownership
25862796Smarks 		 * has been granted or write_acl is present and user
25872796Smarks 		 * has the ability to modify mode.  In that case remove
25882796Smarks 		 * UID|GID and or MODE from mask so that
25891115Smarks 		 * secpolicy_vnode_setattr() doesn't revoke it.
25901115Smarks 		 */
25912796Smarks 
25922796Smarks 		if (trim_mask) {
25932796Smarks 			saved_mask = vap->va_mask;
25942796Smarks 			vap->va_mask &= ~trim_mask;
25952796Smarks 		}
2596789Sahrens 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
25975331Samw 		    (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2598789Sahrens 		if (err) {
2599789Sahrens 			ZFS_EXIT(zfsvfs);
2600789Sahrens 			return (err);
2601789Sahrens 		}
26021115Smarks 
26031115Smarks 		if (trim_mask)
26042796Smarks 			vap->va_mask |= saved_mask;
2605789Sahrens 	}
2606789Sahrens 
2607789Sahrens 	/*
2608789Sahrens 	 * secpolicy_vnode_setattr, or take ownership may have
2609789Sahrens 	 * changed va_mask
2610789Sahrens 	 */
2611789Sahrens 	mask = vap->va_mask;
2612789Sahrens 
2613789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2614789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
2615789Sahrens 
2616789Sahrens 	if (mask & AT_MODE) {
26171576Smarks 		uint64_t pmode = pzp->zp_mode;
26181576Smarks 
26191576Smarks 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2620789Sahrens 
2621*9396SMatthew.Ahrens@Sun.COM 		if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
2622*9396SMatthew.Ahrens@Sun.COM 			goto out;
26235331Samw 		if (pzp->zp_acl.z_acl_extern_obj) {
26245331Samw 			/* Are we upgrading ACL from old V0 format to new V1 */
26255331Samw 			if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
26265331Samw 			    pzp->zp_acl.z_acl_version ==
26275331Samw 			    ZFS_ACL_VERSION_INITIAL) {
26285331Samw 				dmu_tx_hold_free(tx,
26295331Samw 				    pzp->zp_acl.z_acl_extern_obj, 0,
26305331Samw 				    DMU_OBJECT_END);
26315331Samw 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
26325824Smarks 				    0, aclp->z_acl_bytes);
26335331Samw 			} else {
26345331Samw 				dmu_tx_hold_write(tx,
26355331Samw 				    pzp->zp_acl.z_acl_extern_obj, 0,
26365824Smarks 				    aclp->z_acl_bytes);
26375331Samw 			}
26386180Smarks 		} else if (aclp->z_acl_bytes > ZFS_ACE_SPACE) {
26396180Smarks 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
26406180Smarks 			    0, aclp->z_acl_bytes);
26415331Samw 		}
2642789Sahrens 	}
2643789Sahrens 
26449179SMark.Shellenbaum@Sun.COM 	if (mask & (AT_UID | AT_GID)) {
26459179SMark.Shellenbaum@Sun.COM 		if (pzp->zp_xattr) {
26469179SMark.Shellenbaum@Sun.COM 			err = zfs_zget(zp->z_zfsvfs, pzp->zp_xattr, &attrzp);
2647*9396SMatthew.Ahrens@Sun.COM 			if (err)
2648*9396SMatthew.Ahrens@Sun.COM 				goto out;
26499179SMark.Shellenbaum@Sun.COM 			dmu_tx_hold_bonus(tx, attrzp->z_id);
26509179SMark.Shellenbaum@Sun.COM 		}
26519179SMark.Shellenbaum@Sun.COM 		if (mask & AT_UID) {
26529179SMark.Shellenbaum@Sun.COM 			new_uid = zfs_fuid_create(zfsvfs,
26539179SMark.Shellenbaum@Sun.COM 			    (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2654*9396SMatthew.Ahrens@Sun.COM 			if (new_uid != pzp->zp_uid &&
2655*9396SMatthew.Ahrens@Sun.COM 			    zfs_usergroup_overquota(zfsvfs, B_FALSE, new_uid)) {
2656*9396SMatthew.Ahrens@Sun.COM 				err = EDQUOT;
2657*9396SMatthew.Ahrens@Sun.COM 				goto out;
2658*9396SMatthew.Ahrens@Sun.COM 			}
26591231Smarks 		}
2660*9396SMatthew.Ahrens@Sun.COM 
26619179SMark.Shellenbaum@Sun.COM 		if (mask & AT_GID) {
26629179SMark.Shellenbaum@Sun.COM 			new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
26639179SMark.Shellenbaum@Sun.COM 			    cr, ZFS_GROUP, &fuidp);
2664*9396SMatthew.Ahrens@Sun.COM 			if (new_gid != pzp->zp_gid &&
2665*9396SMatthew.Ahrens@Sun.COM 			    zfs_usergroup_overquota(zfsvfs, B_TRUE, new_gid)) {
2666*9396SMatthew.Ahrens@Sun.COM 				err = EDQUOT;
2667*9396SMatthew.Ahrens@Sun.COM 				goto out;
2668*9396SMatthew.Ahrens@Sun.COM 			}
26699179SMark.Shellenbaum@Sun.COM 		}
26709179SMark.Shellenbaum@Sun.COM 		fuid_dirtied = zfsvfs->z_fuid_dirty;
26719179SMark.Shellenbaum@Sun.COM 		if (fuid_dirtied) {
26729179SMark.Shellenbaum@Sun.COM 			if (zfsvfs->z_fuid_obj == 0) {
26739179SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
26749179SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
26759179SMark.Shellenbaum@Sun.COM 				    FUID_SIZE_ESTIMATE(zfsvfs));
26769179SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_zap(tx, MASTER_NODE_OBJ,
26779179SMark.Shellenbaum@Sun.COM 				    FALSE, NULL);
26789179SMark.Shellenbaum@Sun.COM 			} else {
26799179SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
26809179SMark.Shellenbaum@Sun.COM 				dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
26819179SMark.Shellenbaum@Sun.COM 				    FUID_SIZE_ESTIMATE(zfsvfs));
26829179SMark.Shellenbaum@Sun.COM 			}
26839179SMark.Shellenbaum@Sun.COM 		}
26841231Smarks 	}
26851231Smarks 
26868227SNeil.Perrin@Sun.COM 	err = dmu_tx_assign(tx, TXG_NOWAIT);
2687789Sahrens 	if (err) {
2688*9396SMatthew.Ahrens@Sun.COM 		if (err == ERESTART)
26892113Sahrens 			dmu_tx_wait(tx);
2690*9396SMatthew.Ahrens@Sun.COM 		goto out;
2691789Sahrens 	}
2692789Sahrens 
2693789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
2694789Sahrens 
2695789Sahrens 	/*
2696789Sahrens 	 * Set each attribute requested.
2697789Sahrens 	 * We group settings according to the locks they need to acquire.
2698789Sahrens 	 *
2699789Sahrens 	 * Note: you cannot set ctime directly, although it will be
2700789Sahrens 	 * updated as a side-effect of calling this function.
2701789Sahrens 	 */
2702789Sahrens 
2703789Sahrens 	mutex_enter(&zp->z_lock);
2704789Sahrens 
2705789Sahrens 	if (mask & AT_MODE) {
27065824Smarks 		mutex_enter(&zp->z_acl_lock);
27075824Smarks 		zp->z_phys->zp_mode = new_mode;
27089179SMark.Shellenbaum@Sun.COM 		err = zfs_aclset_common(zp, aclp, cr, tx);
2709789Sahrens 		ASSERT3U(err, ==, 0);
27105824Smarks 		mutex_exit(&zp->z_acl_lock);
2711789Sahrens 	}
2712789Sahrens 
27131231Smarks 	if (attrzp)
27141231Smarks 		mutex_enter(&attrzp->z_lock);
27151231Smarks 
27161231Smarks 	if (mask & AT_UID) {
27179179SMark.Shellenbaum@Sun.COM 		pzp->zp_uid = new_uid;
27189179SMark.Shellenbaum@Sun.COM 		if (attrzp)
27199179SMark.Shellenbaum@Sun.COM 			attrzp->z_phys->zp_uid = new_uid;
27201231Smarks 	}
27211231Smarks 
27221231Smarks 	if (mask & AT_GID) {
27239179SMark.Shellenbaum@Sun.COM 		pzp->zp_gid = new_gid;
27241231Smarks 		if (attrzp)
27259179SMark.Shellenbaum@Sun.COM 			attrzp->z_phys->zp_gid = new_gid;
27261231Smarks 	}
27271231Smarks 
27281231Smarks 	if (attrzp)
27291231Smarks 		mutex_exit(&attrzp->z_lock);
2730789Sahrens 
2731789Sahrens 	if (mask & AT_ATIME)
2732789Sahrens 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
2733789Sahrens 
2734789Sahrens 	if (mask & AT_MTIME)
2735789Sahrens 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
2736789Sahrens 
27376992Smaybee 	/* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
27381878Smaybee 	if (mask & AT_SIZE)
2739789Sahrens 		zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
27401878Smaybee 	else if (mask != 0)
2741789Sahrens 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
27425331Samw 	/*
27435331Samw 	 * Do this after setting timestamps to prevent timestamp
27445331Samw 	 * update from toggling bit
27455331Samw 	 */
27465331Samw 
27475331Samw 	if (xoap && (mask & AT_XVATTR)) {
27488190SMark.Shellenbaum@Sun.COM 
27498190SMark.Shellenbaum@Sun.COM 		/*
27508190SMark.Shellenbaum@Sun.COM 		 * restore trimmed off masks
27518190SMark.Shellenbaum@Sun.COM 		 * so that return masks can be set for caller.
27528190SMark.Shellenbaum@Sun.COM 		 */
27538190SMark.Shellenbaum@Sun.COM 
27548190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
27558190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_APPENDONLY);
27568190SMark.Shellenbaum@Sun.COM 		}
27578190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
27588190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_NOUNLINK);
27598190SMark.Shellenbaum@Sun.COM 		}
27608190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
27618190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_IMMUTABLE);
27628190SMark.Shellenbaum@Sun.COM 		}
27638190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
27648190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_NODUMP);
27658190SMark.Shellenbaum@Sun.COM 		}
27668190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
27678190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
27688190SMark.Shellenbaum@Sun.COM 		}
27698190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
27708190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
27718190SMark.Shellenbaum@Sun.COM 		}
27728190SMark.Shellenbaum@Sun.COM 
27735331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
27745331Samw 			size_t len;
27755331Samw 			dmu_object_info_t doi;
27765331Samw 
27775331Samw 			ASSERT(vp->v_type == VREG);
27785331Samw 
27795331Samw 			/* Grow the bonus buffer if necessary. */
27805331Samw 			dmu_object_info_from_db(zp->z_dbuf, &doi);
27815331Samw 			len = sizeof (xoap->xoa_av_scanstamp) +
27825331Samw 			    sizeof (znode_phys_t);
27835331Samw 			if (len > doi.doi_bonus_size)
27845331Samw 				VERIFY(dmu_set_bonus(zp->z_dbuf, len, tx) == 0);
27855331Samw 		}
27865331Samw 		zfs_xvattr_set(zp, xvap);
27875331Samw 	}
2788789Sahrens 
27899179SMark.Shellenbaum@Sun.COM 	if (fuid_dirtied)
27909179SMark.Shellenbaum@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
27919179SMark.Shellenbaum@Sun.COM 
27921878Smaybee 	if (mask != 0)
27935331Samw 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
27945331Samw 
2795789Sahrens 	mutex_exit(&zp->z_lock);
2796789Sahrens 
2797*9396SMatthew.Ahrens@Sun.COM out:
27981231Smarks 	if (attrzp)
27991231Smarks 		VN_RELE(ZTOV(attrzp));
28001231Smarks 
2801*9396SMatthew.Ahrens@Sun.COM 	if (aclp) {
2802*9396SMatthew.Ahrens@Sun.COM 		zfs_acl_free(aclp);
2803*9396SMatthew.Ahrens@Sun.COM 		aclp = NULL;
2804*9396SMatthew.Ahrens@Sun.COM 	}
2805*9396SMatthew.Ahrens@Sun.COM 
2806*9396SMatthew.Ahrens@Sun.COM 	if (fuidp) {
2807*9396SMatthew.Ahrens@Sun.COM 		zfs_fuid_info_free(fuidp);
2808*9396SMatthew.Ahrens@Sun.COM 		fuidp = NULL;
2809*9396SMatthew.Ahrens@Sun.COM 	}
2810*9396SMatthew.Ahrens@Sun.COM 
2811*9396SMatthew.Ahrens@Sun.COM 	if (err)
2812*9396SMatthew.Ahrens@Sun.COM 		dmu_tx_abort(tx);
2813*9396SMatthew.Ahrens@Sun.COM 	else
2814*9396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
2815*9396SMatthew.Ahrens@Sun.COM 
2816*9396SMatthew.Ahrens@Sun.COM 	if (err == ERESTART)
2817*9396SMatthew.Ahrens@Sun.COM 		goto top;
2818789Sahrens 
2819789Sahrens 	ZFS_EXIT(zfsvfs);
2820789Sahrens 	return (err);
2821789Sahrens }
2822789Sahrens 
28233271Smaybee typedef struct zfs_zlock {
28243271Smaybee 	krwlock_t	*zl_rwlock;	/* lock we acquired */
28253271Smaybee 	znode_t		*zl_znode;	/* znode we held */
28263271Smaybee 	struct zfs_zlock *zl_next;	/* next in list */
28273271Smaybee } zfs_zlock_t;
28283271Smaybee 
28293271Smaybee /*
28303271Smaybee  * Drop locks and release vnodes that were held by zfs_rename_lock().
28313271Smaybee  */
28323271Smaybee static void
28333271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp)
28343271Smaybee {
28353271Smaybee 	zfs_zlock_t *zl;
28363271Smaybee 
28373271Smaybee 	while ((zl = *zlpp) != NULL) {
28383271Smaybee 		if (zl->zl_znode != NULL)
28393271Smaybee 			VN_RELE(ZTOV(zl->zl_znode));
28403271Smaybee 		rw_exit(zl->zl_rwlock);
28413271Smaybee 		*zlpp = zl->zl_next;
28423271Smaybee 		kmem_free(zl, sizeof (*zl));
28433271Smaybee 	}
28443271Smaybee }
28453271Smaybee 
2846789Sahrens /*
2847789Sahrens  * Search back through the directory tree, using the ".." entries.
2848789Sahrens  * Lock each directory in the chain to prevent concurrent renames.
2849789Sahrens  * Fail any attempt to move a directory into one of its own descendants.
2850789Sahrens  * XXX - z_parent_lock can overlap with map or grow locks
2851789Sahrens  */
2852789Sahrens static int
2853789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
2854789Sahrens {
2855789Sahrens 	zfs_zlock_t	*zl;
28563638Sbillm 	znode_t		*zp = tdzp;
2857789Sahrens 	uint64_t	rootid = zp->z_zfsvfs->z_root;
2858789Sahrens 	uint64_t	*oidp = &zp->z_id;
2859789Sahrens 	krwlock_t	*rwlp = &szp->z_parent_lock;
2860789Sahrens 	krw_t		rw = RW_WRITER;
2861789Sahrens 
2862789Sahrens 	/*
2863789Sahrens 	 * First pass write-locks szp and compares to zp->z_id.
2864789Sahrens 	 * Later passes read-lock zp and compare to zp->z_parent.
2865789Sahrens 	 */
2866789Sahrens 	do {
28673271Smaybee 		if (!rw_tryenter(rwlp, rw)) {
28683271Smaybee 			/*
28693271Smaybee 			 * Another thread is renaming in this path.
28703271Smaybee 			 * Note that if we are a WRITER, we don't have any
28713271Smaybee 			 * parent_locks held yet.
28723271Smaybee 			 */
28733271Smaybee 			if (rw == RW_READER && zp->z_id > szp->z_id) {
28743271Smaybee 				/*
28753271Smaybee 				 * Drop our locks and restart
28763271Smaybee 				 */
28773271Smaybee 				zfs_rename_unlock(&zl);
28783271Smaybee 				*zlpp = NULL;
28793271Smaybee 				zp = tdzp;
28803271Smaybee 				oidp = &zp->z_id;
28813271Smaybee 				rwlp = &szp->z_parent_lock;
28823271Smaybee 				rw = RW_WRITER;
28833271Smaybee 				continue;
28843271Smaybee 			} else {
28853271Smaybee 				/*
28863271Smaybee 				 * Wait for other thread to drop its locks
28873271Smaybee 				 */
28883271Smaybee 				rw_enter(rwlp, rw);
28893271Smaybee 			}
28903271Smaybee 		}
28913271Smaybee 
2892789Sahrens 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
2893789Sahrens 		zl->zl_rwlock = rwlp;
2894789Sahrens 		zl->zl_znode = NULL;
2895789Sahrens 		zl->zl_next = *zlpp;
2896789Sahrens 		*zlpp = zl;
2897789Sahrens 
2898789Sahrens 		if (*oidp == szp->z_id)		/* We're a descendant of szp */
2899789Sahrens 			return (EINVAL);
2900789Sahrens 
2901789Sahrens 		if (*oidp == rootid)		/* We've hit the top */
2902789Sahrens 			return (0);
2903789Sahrens 
2904789Sahrens 		if (rw == RW_READER) {		/* i.e. not the first pass */
2905789Sahrens 			int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
2906789Sahrens 			if (error)
2907789Sahrens 				return (error);
2908789Sahrens 			zl->zl_znode = zp;
2909789Sahrens 		}
2910789Sahrens 		oidp = &zp->z_phys->zp_parent;
2911789Sahrens 		rwlp = &zp->z_parent_lock;
2912789Sahrens 		rw = RW_READER;
2913789Sahrens 
2914789Sahrens 	} while (zp->z_id != sdzp->z_id);
2915789Sahrens 
2916789Sahrens 	return (0);
2917789Sahrens }
2918789Sahrens 
2919789Sahrens /*
2920789Sahrens  * Move an entry from the provided source directory to the target
2921789Sahrens  * directory.  Change the entry name as indicated.
2922789Sahrens  *
2923789Sahrens  *	IN:	sdvp	- Source directory containing the "old entry".
2924789Sahrens  *		snm	- Old entry name.
2925789Sahrens  *		tdvp	- Target directory to contain the "new entry".
2926789Sahrens  *		tnm	- New entry name.
2927789Sahrens  *		cr	- credentials of caller.
29285331Samw  *		ct	- caller context
29295331Samw  *		flags	- case flags
2930789Sahrens  *
2931789Sahrens  *	RETURN:	0 if success
2932789Sahrens  *		error code if failure
2933789Sahrens  *
2934789Sahrens  * Timestamps:
2935789Sahrens  *	sdvp,tdvp - ctime|mtime updated
2936789Sahrens  */
29375331Samw /*ARGSUSED*/
2938789Sahrens static int
29395331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
29405331Samw     caller_context_t *ct, int flags)
2941789Sahrens {
2942789Sahrens 	znode_t		*tdzp, *szp, *tzp;
2943789Sahrens 	znode_t		*sdzp = VTOZ(sdvp);
2944789Sahrens 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
29455326Sek110237 	zilog_t		*zilog;
2946789Sahrens 	vnode_t		*realvp;
2947789Sahrens 	zfs_dirlock_t	*sdl, *tdl;
2948789Sahrens 	dmu_tx_t	*tx;
2949789Sahrens 	zfs_zlock_t	*zl;
29505331Samw 	int		cmp, serr, terr;
29515331Samw 	int		error = 0;
29525331Samw 	int		zflg = 0;
2953789Sahrens 
29545367Sahrens 	ZFS_ENTER(zfsvfs);
29555367Sahrens 	ZFS_VERIFY_ZP(sdzp);
29565326Sek110237 	zilog = zfsvfs->z_log;
2957789Sahrens 
2958789Sahrens 	/*
2959789Sahrens 	 * Make sure we have the real vp for the target directory.
2960789Sahrens 	 */
29615331Samw 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
2962789Sahrens 		tdvp = realvp;
2963789Sahrens 
2964789Sahrens 	if (tdvp->v_vfsp != sdvp->v_vfsp) {
2965789Sahrens 		ZFS_EXIT(zfsvfs);
2966789Sahrens 		return (EXDEV);
2967789Sahrens 	}
2968789Sahrens 
2969789Sahrens 	tdzp = VTOZ(tdvp);
29705367Sahrens 	ZFS_VERIFY_ZP(tdzp);
29715498Stimh 	if (zfsvfs->z_utf8 && u8_validate(tnm,
29725331Samw 	    strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
29735331Samw 		ZFS_EXIT(zfsvfs);
29745331Samw 		return (EILSEQ);
29755331Samw 	}
29765331Samw 
29775331Samw 	if (flags & FIGNORECASE)
29785331Samw 		zflg |= ZCILOOK;
29795331Samw 
2980789Sahrens top:
2981789Sahrens 	szp = NULL;
2982789Sahrens 	tzp = NULL;
2983789Sahrens 	zl = NULL;
2984789Sahrens 
2985789Sahrens 	/*
2986789Sahrens 	 * This is to prevent the creation of links into attribute space
2987789Sahrens 	 * by renaming a linked file into/outof an attribute directory.
2988789Sahrens 	 * See the comment in zfs_link() for why this is considered bad.
2989789Sahrens 	 */
2990789Sahrens 	if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
2991789Sahrens 	    (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
2992789Sahrens 		ZFS_EXIT(zfsvfs);
2993789Sahrens 		return (EINVAL);
2994789Sahrens 	}
2995789Sahrens 
2996789Sahrens 	/*
2997789Sahrens 	 * Lock source and target directory entries.  To prevent deadlock,
2998789Sahrens 	 * a lock ordering must be defined.  We lock the directory with
2999789Sahrens 	 * the smallest object id first, or if it's a tie, the one with
3000789Sahrens 	 * the lexically first name.
3001789Sahrens 	 */
3002789Sahrens 	if (sdzp->z_id < tdzp->z_id) {
3003789Sahrens 		cmp = -1;
3004789Sahrens 	} else if (sdzp->z_id > tdzp->z_id) {
3005789Sahrens 		cmp = 1;
3006789Sahrens 	} else {
30075331Samw 		/*
30085331Samw 		 * First compare the two name arguments without
30095331Samw 		 * considering any case folding.
30105331Samw 		 */
30115331Samw 		int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
30125331Samw 
30135331Samw 		cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
30145498Stimh 		ASSERT(error == 0 || !zfsvfs->z_utf8);
3015789Sahrens 		if (cmp == 0) {
3016789Sahrens 			/*
3017789Sahrens 			 * POSIX: "If the old argument and the new argument
3018789Sahrens 			 * both refer to links to the same existing file,
3019789Sahrens 			 * the rename() function shall return successfully
3020789Sahrens 			 * and perform no other action."
3021789Sahrens 			 */
3022789Sahrens 			ZFS_EXIT(zfsvfs);
3023789Sahrens 			return (0);
3024789Sahrens 		}
30255331Samw 		/*
30265331Samw 		 * If the file system is case-folding, then we may
30275331Samw 		 * have some more checking to do.  A case-folding file
30285331Samw 		 * system is either supporting mixed case sensitivity
30295331Samw 		 * access or is completely case-insensitive.  Note
30305331Samw 		 * that the file system is always case preserving.
30315331Samw 		 *
30325331Samw 		 * In mixed sensitivity mode case sensitive behavior
30335331Samw 		 * is the default.  FIGNORECASE must be used to
30345331Samw 		 * explicitly request case insensitive behavior.
30355331Samw 		 *
30365331Samw 		 * If the source and target names provided differ only
30375331Samw 		 * by case (e.g., a request to rename 'tim' to 'Tim'),
30385331Samw 		 * we will treat this as a special case in the
30395331Samw 		 * case-insensitive mode: as long as the source name
30405331Samw 		 * is an exact match, we will allow this to proceed as
30415331Samw 		 * a name-change request.
30425331Samw 		 */
30435498Stimh 		if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
30445498Stimh 		    (zfsvfs->z_case == ZFS_CASE_MIXED &&
30455498Stimh 		    flags & FIGNORECASE)) &&
30465331Samw 		    u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
30475331Samw 		    &error) == 0) {
30485331Samw 			/*
30495331Samw 			 * case preserving rename request, require exact
30505331Samw 			 * name matches
30515331Samw 			 */
30525331Samw 			zflg |= ZCIEXACT;
30535331Samw 			zflg &= ~ZCILOOK;
30545331Samw 		}
3055789Sahrens 	}
30565331Samw 
3057789Sahrens 	if (cmp < 0) {
30585331Samw 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
30595331Samw 		    ZEXISTS | zflg, NULL, NULL);
30605331Samw 		terr = zfs_dirent_lock(&tdl,
30615331Samw 		    tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3062789Sahrens 	} else {
30635331Samw 		terr = zfs_dirent_lock(&tdl,
30645331Samw 		    tdzp, tnm, &tzp, zflg, NULL, NULL);
30655331Samw 		serr = zfs_dirent_lock(&sdl,
30665331Samw 		    sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
30675331Samw 		    NULL, NULL);
3068789Sahrens 	}
3069789Sahrens 
3070789Sahrens 	if (serr) {
3071789Sahrens 		/*
3072789Sahrens 		 * Source entry invalid or not there.
3073789Sahrens 		 */
3074789Sahrens 		if (!terr) {
3075789Sahrens 			zfs_dirent_unlock(tdl);
3076789Sahrens 			if (tzp)
3077789Sahrens 				VN_RELE(ZTOV(tzp));
3078789Sahrens 		}
3079789Sahrens 		if (strcmp(snm, "..") == 0)
3080789Sahrens 			serr = EINVAL;
3081789Sahrens 		ZFS_EXIT(zfsvfs);
3082789Sahrens 		return (serr);
3083789Sahrens 	}
3084789Sahrens 	if (terr) {
3085789Sahrens 		zfs_dirent_unlock(sdl);
3086789Sahrens 		VN_RELE(ZTOV(szp));
3087789Sahrens 		if (strcmp(tnm, "..") == 0)
3088789Sahrens 			terr = EINVAL;
3089789Sahrens 		ZFS_EXIT(zfsvfs);
3090789Sahrens 		return (terr);
3091789Sahrens 	}
3092789Sahrens 
3093789Sahrens 	/*
3094789Sahrens 	 * Must have write access at the source to remove the old entry
3095789Sahrens 	 * and write access at the target to create the new entry.
3096789Sahrens 	 * Note that if target and source are the same, this can be
3097789Sahrens 	 * done in a single check.
3098789Sahrens 	 */
3099789Sahrens 
3100789Sahrens 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3101789Sahrens 		goto out;
3102789Sahrens 
3103789Sahrens 	if (ZTOV(szp)->v_type == VDIR) {
3104789Sahrens 		/*
3105789Sahrens 		 * Check to make sure rename is valid.
3106789Sahrens 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3107789Sahrens 		 */
3108789Sahrens 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3109789Sahrens 			goto out;
3110789Sahrens 	}
3111789Sahrens 
3112789Sahrens 	/*
3113789Sahrens 	 * Does target exist?
3114789Sahrens 	 */
3115789Sahrens 	if (tzp) {
3116789Sahrens 		/*
3117789Sahrens 		 * Source and target must be the same type.
3118789Sahrens 		 */
3119789Sahrens 		if (ZTOV(szp)->v_type == VDIR) {
3120789Sahrens 			if (ZTOV(tzp)->v_type != VDIR) {
3121789Sahrens 				error = ENOTDIR;
3122789Sahrens 				goto out;
3123789Sahrens 			}
3124789Sahrens 		} else {
3125789Sahrens 			if (ZTOV(tzp)->v_type == VDIR) {
3126789Sahrens 				error = EISDIR;
3127789Sahrens 				goto out;
3128789Sahrens 			}
3129789Sahrens 		}
3130789Sahrens 		/*
3131789Sahrens 		 * POSIX dictates that when the source and target
3132789Sahrens 		 * entries refer to the same file object, rename
3133789Sahrens 		 * must do nothing and exit without error.
3134789Sahrens 		 */
3135789Sahrens 		if (szp->z_id == tzp->z_id) {
3136789Sahrens 			error = 0;
3137789Sahrens 			goto out;
3138789Sahrens 		}
3139789Sahrens 	}
3140789Sahrens 
31415331Samw 	vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3142789Sahrens 	if (tzp)
31435331Samw 		vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
31444863Spraks 
31454863Spraks 	/*
31464863Spraks 	 * notify the target directory if it is not the same
31474863Spraks 	 * as source directory.
31484863Spraks 	 */
31494863Spraks 	if (tdvp != sdvp) {
31505331Samw 		vnevent_rename_dest_dir(tdvp, ct);
31514863Spraks 	}
3152789Sahrens 
3153789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3154789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);	/* nlink changes */
3155789Sahrens 	dmu_tx_hold_bonus(tx, sdzp->z_id);	/* nlink changes */
31561544Seschrock 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
31571544Seschrock 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
31581544Seschrock 	if (sdzp != tdzp)
3159789Sahrens 		dmu_tx_hold_bonus(tx, tdzp->z_id);	/* nlink changes */
31601544Seschrock 	if (tzp)
31611544Seschrock 		dmu_tx_hold_bonus(tx, tzp->z_id);	/* parent changes */
31623461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
31638227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3164789Sahrens 	if (error) {
3165789Sahrens 		if (zl != NULL)
3166789Sahrens 			zfs_rename_unlock(&zl);
3167789Sahrens 		zfs_dirent_unlock(sdl);
3168789Sahrens 		zfs_dirent_unlock(tdl);
3169789Sahrens 		VN_RELE(ZTOV(szp));
3170789Sahrens 		if (tzp)
3171789Sahrens 			VN_RELE(ZTOV(tzp));
31728227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
31732113Sahrens 			dmu_tx_wait(tx);
31742113Sahrens 			dmu_tx_abort(tx);
3175789Sahrens 			goto top;
3176789Sahrens 		}
31772113Sahrens 		dmu_tx_abort(tx);
3178789Sahrens 		ZFS_EXIT(zfsvfs);
3179789Sahrens 		return (error);
3180789Sahrens 	}
3181789Sahrens 
3182789Sahrens 	if (tzp)	/* Attempt to remove the existing target */
31835331Samw 		error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3184789Sahrens 
3185789Sahrens 	if (error == 0) {
3186789Sahrens 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3187789Sahrens 		if (error == 0) {
31885331Samw 			szp->z_phys->zp_flags |= ZFS_AV_MODIFIED;
31895331Samw 
3190789Sahrens 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3191789Sahrens 			ASSERT(error == 0);
31925331Samw 
31935331Samw 			zfs_log_rename(zilog, tx,
31945331Samw 			    TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0),
31955331Samw 			    sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
31966976Seschrock 
31976976Seschrock 			/* Update path information for the target vnode */
31986976Seschrock 			vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm));
3199789Sahrens 		}
3200789Sahrens 	}
3201789Sahrens 
3202789Sahrens 	dmu_tx_commit(tx);
3203789Sahrens out:
3204789Sahrens 	if (zl != NULL)
3205789Sahrens 		zfs_rename_unlock(&zl);
3206789Sahrens 
3207789Sahrens 	zfs_dirent_unlock(sdl);
3208789Sahrens 	zfs_dirent_unlock(tdl);
3209789Sahrens 
3210789Sahrens 	VN_RELE(ZTOV(szp));
3211789Sahrens 	if (tzp)
3212789Sahrens 		VN_RELE(ZTOV(tzp));
3213789Sahrens 
3214789Sahrens 	ZFS_EXIT(zfsvfs);
3215789Sahrens 	return (error);
3216789Sahrens }
3217789Sahrens 
3218789Sahrens /*
3219789Sahrens  * Insert the indicated symbolic reference entry into the directory.
3220789Sahrens  *
3221789Sahrens  *	IN:	dvp	- Directory to contain new symbolic link.
3222789Sahrens  *		link	- Name for new symlink entry.
3223789Sahrens  *		vap	- Attributes of new entry.
3224789Sahrens  *		target	- Target path of new symlink.
3225789Sahrens  *		cr	- credentials of caller.
32265331Samw  *		ct	- caller context
32275331Samw  *		flags	- case flags
3228789Sahrens  *
3229789Sahrens  *	RETURN:	0 if success
3230789Sahrens  *		error code if failure
3231789Sahrens  *
3232789Sahrens  * Timestamps:
3233789Sahrens  *	dvp - ctime|mtime updated
3234789Sahrens  */
32355331Samw /*ARGSUSED*/
3236789Sahrens static int
32375331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
32385331Samw     caller_context_t *ct, int flags)
3239789Sahrens {
3240789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
3241789Sahrens 	zfs_dirlock_t	*dl;
3242789Sahrens 	dmu_tx_t	*tx;
3243789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
32445326Sek110237 	zilog_t		*zilog;
3245789Sahrens 	int		len = strlen(link);
3246789Sahrens 	int		error;
32475331Samw 	int		zflg = ZNEW;
32489179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_t	acl_ids;
32499179SMark.Shellenbaum@Sun.COM 	boolean_t	fuid_dirtied;
3250789Sahrens 
3251789Sahrens 	ASSERT(vap->va_type == VLNK);
3252789Sahrens 
32535367Sahrens 	ZFS_ENTER(zfsvfs);
32545367Sahrens 	ZFS_VERIFY_ZP(dzp);
32555326Sek110237 	zilog = zfsvfs->z_log;
32565331Samw 
32575498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
32585331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
32595331Samw 		ZFS_EXIT(zfsvfs);
32605331Samw 		return (EILSEQ);
32615331Samw 	}
32625331Samw 	if (flags & FIGNORECASE)
32635331Samw 		zflg |= ZCILOOK;
3264789Sahrens top:
32655331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3266789Sahrens 		ZFS_EXIT(zfsvfs);
3267789Sahrens 		return (error);
3268789Sahrens 	}
3269789Sahrens 
3270789Sahrens 	if (len > MAXPATHLEN) {
3271789Sahrens 		ZFS_EXIT(zfsvfs);
3272789Sahrens 		return (ENAMETOOLONG);
3273789Sahrens 	}
3274789Sahrens 
3275789Sahrens 	/*
3276789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3277789Sahrens 	 */
32785331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
32795331Samw 	if (error) {
3280789Sahrens 		ZFS_EXIT(zfsvfs);
3281789Sahrens 		return (error);
3282789Sahrens 	}
3283789Sahrens 
32849179SMark.Shellenbaum@Sun.COM 	VERIFY(0 == zfs_acl_ids_create(dzp, 0, vap, cr, NULL, &acl_ids));
3285*9396SMatthew.Ahrens@Sun.COM 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
3286*9396SMatthew.Ahrens@Sun.COM 		zfs_acl_ids_free(&acl_ids);
3287*9396SMatthew.Ahrens@Sun.COM 		zfs_dirent_unlock(dl);
3288*9396SMatthew.Ahrens@Sun.COM 		ZFS_EXIT(zfsvfs);
3289*9396SMatthew.Ahrens@Sun.COM 		return (EDQUOT);
3290*9396SMatthew.Ahrens@Sun.COM 	}
3291789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
32929179SMark.Shellenbaum@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
3293789Sahrens 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3294789Sahrens 	dmu_tx_hold_bonus(tx, dzp->z_id);
32951544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
32969179SMark.Shellenbaum@Sun.COM 	if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE)
3297789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
3298*9396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
3299*9396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
33008227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3301789Sahrens 	if (error) {
33029179SMark.Shellenbaum@Sun.COM 		zfs_acl_ids_free(&acl_ids);
3303789Sahrens 		zfs_dirent_unlock(dl);
33048227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
33052113Sahrens 			dmu_tx_wait(tx);
33062113Sahrens 			dmu_tx_abort(tx);
3307789Sahrens 			goto top;
3308789Sahrens 		}
33092113Sahrens 		dmu_tx_abort(tx);
3310789Sahrens 		ZFS_EXIT(zfsvfs);
3311789Sahrens 		return (error);
3312789Sahrens 	}
3313789Sahrens 
3314789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
3315789Sahrens 
3316789Sahrens 	/*
3317789Sahrens 	 * Create a new object for the symlink.
3318789Sahrens 	 * Put the link content into bonus buffer if it will fit;
3319789Sahrens 	 * otherwise, store it just like any other file data.
3320789Sahrens 	 */
3321789Sahrens 	if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
33229179SMark.Shellenbaum@Sun.COM 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, len, &acl_ids);
3323789Sahrens 		if (len != 0)
3324789Sahrens 			bcopy(link, zp->z_phys + 1, len);
3325789Sahrens 	} else {
3326789Sahrens 		dmu_buf_t *dbp;
33271669Sperrin 
33289179SMark.Shellenbaum@Sun.COM 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids);
33299179SMark.Shellenbaum@Sun.COM 
33309179SMark.Shellenbaum@Sun.COM 		if (fuid_dirtied)
33319179SMark.Shellenbaum@Sun.COM 			zfs_fuid_sync(zfsvfs, tx);
33321669Sperrin 		/*
33331669Sperrin 		 * Nothing can access the znode yet so no locking needed
33341669Sperrin 		 * for growing the znode's blocksize.
33351669Sperrin 		 */
33361669Sperrin 		zfs_grow_blocksize(zp, len, tx);
3337789Sahrens 
33385446Sahrens 		VERIFY(0 == dmu_buf_hold(zfsvfs->z_os,
33395446Sahrens 		    zp->z_id, 0, FTAG, &dbp));
3340789Sahrens 		dmu_buf_will_dirty(dbp, tx);
3341789Sahrens 
3342789Sahrens 		ASSERT3U(len, <=, dbp->db_size);
3343789Sahrens 		bcopy(link, dbp->db_data, len);
33441544Seschrock 		dmu_buf_rele(dbp, FTAG);
3345789Sahrens 	}
3346789Sahrens 	zp->z_phys->zp_size = len;
3347789Sahrens 
3348789Sahrens 	/*
3349789Sahrens 	 * Insert the new object into the directory.
3350789Sahrens 	 */
3351789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
33525331Samw 	if (error == 0) {
33535331Samw 		uint64_t txtype = TX_SYMLINK;
33545331Samw 		if (flags & FIGNORECASE)
33555331Samw 			txtype |= TX_CI;
33565331Samw 		zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
33575331Samw 	}
33589179SMark.Shellenbaum@Sun.COM 
33599179SMark.Shellenbaum@Sun.COM 	zfs_acl_ids_free(&acl_ids);
3360789Sahrens 
3361789Sahrens 	dmu_tx_commit(tx);
3362789Sahrens 
3363789Sahrens 	zfs_dirent_unlock(dl);
3364789Sahrens 
3365789Sahrens 	VN_RELE(ZTOV(zp));
3366789Sahrens 
3367789Sahrens 	ZFS_EXIT(zfsvfs);
3368789Sahrens 	return (error);
3369789Sahrens }
3370789Sahrens 
3371789Sahrens /*
3372789Sahrens  * Return, in the buffer contained in the provided uio structure,
3373789Sahrens  * the symbolic path referred to by vp.
3374789Sahrens  *
3375789Sahrens  *	IN:	vp	- vnode of symbolic link.
3376789Sahrens  *		uoip	- structure to contain the link path.
3377789Sahrens  *		cr	- credentials of caller.
33785331Samw  *		ct	- caller context
3379789Sahrens  *
3380789Sahrens  *	OUT:	uio	- structure to contain the link path.
3381789Sahrens  *
3382789Sahrens  *	RETURN:	0 if success
3383789Sahrens  *		error code if failure
3384789Sahrens  *
3385789Sahrens  * Timestamps:
3386789Sahrens  *	vp - atime updated
3387789Sahrens  */
3388789Sahrens /* ARGSUSED */
3389789Sahrens static int
33905331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3391789Sahrens {
3392789Sahrens 	znode_t		*zp = VTOZ(vp);
3393789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3394789Sahrens 	size_t		bufsz;
3395789Sahrens 	int		error;
3396789Sahrens 
33975367Sahrens 	ZFS_ENTER(zfsvfs);
33985367Sahrens 	ZFS_VERIFY_ZP(zp);
3399789Sahrens 
3400789Sahrens 	bufsz = (size_t)zp->z_phys->zp_size;
3401789Sahrens 	if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
3402789Sahrens 		error = uiomove(zp->z_phys + 1,
3403789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
3404789Sahrens 	} else {
34051544Seschrock 		dmu_buf_t *dbp;
34061544Seschrock 		error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
34071544Seschrock 		if (error) {
3408789Sahrens 			ZFS_EXIT(zfsvfs);
3409789Sahrens 			return (error);
3410789Sahrens 		}
3411789Sahrens 		error = uiomove(dbp->db_data,
3412789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
34131544Seschrock 		dmu_buf_rele(dbp, FTAG);
3414789Sahrens 	}
3415789Sahrens 
3416789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3417789Sahrens 	ZFS_EXIT(zfsvfs);
3418789Sahrens 	return (error);
3419789Sahrens }
3420789Sahrens 
3421789Sahrens /*
3422789Sahrens  * Insert a new entry into directory tdvp referencing svp.
3423789Sahrens  *
3424789Sahrens  *	IN:	tdvp	- Directory to contain new entry.
3425789Sahrens  *		svp	- vnode of new entry.
3426789Sahrens  *		name	- name of new entry.
3427789Sahrens  *		cr	- credentials of caller.
34285331Samw  *		ct	- caller context
3429789Sahrens  *
3430789Sahrens  *	RETURN:	0 if success
3431789Sahrens  *		error code if failure
3432789Sahrens  *
3433789Sahrens  * Timestamps:
3434789Sahrens  *	tdvp - ctime|mtime updated
3435789Sahrens  *	 svp - ctime updated
3436789Sahrens  */
3437789Sahrens /* ARGSUSED */
3438789Sahrens static int
34395331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
34405331Samw     caller_context_t *ct, int flags)
3441789Sahrens {
3442789Sahrens 	znode_t		*dzp = VTOZ(tdvp);
3443789Sahrens 	znode_t		*tzp, *szp;
3444789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
34455326Sek110237 	zilog_t		*zilog;
3446789Sahrens 	zfs_dirlock_t	*dl;
3447789Sahrens 	dmu_tx_t	*tx;
3448789Sahrens 	vnode_t		*realvp;
3449789Sahrens 	int		error;
34505331Samw 	int		zf = ZNEW;
34515331Samw 	uid_t		owner;
3452789Sahrens 
3453789Sahrens 	ASSERT(tdvp->v_type == VDIR);
3454789Sahrens 
34555367Sahrens 	ZFS_ENTER(zfsvfs);
34565367Sahrens 	ZFS_VERIFY_ZP(dzp);
34575326Sek110237 	zilog = zfsvfs->z_log;
3458789Sahrens 
34595331Samw 	if (VOP_REALVP(svp, &realvp, ct) == 0)
3460789Sahrens 		svp = realvp;
3461789Sahrens 
3462789Sahrens 	if (svp->v_vfsp != tdvp->v_vfsp) {
3463789Sahrens 		ZFS_EXIT(zfsvfs);
3464789Sahrens 		return (EXDEV);
3465789Sahrens 	}
34665367Sahrens 	szp = VTOZ(svp);
34675367Sahrens 	ZFS_VERIFY_ZP(szp);
3468789Sahrens 
34695498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name,
34705331Samw 	    strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
34715331Samw 		ZFS_EXIT(zfsvfs);
34725331Samw 		return (EILSEQ);
34735331Samw 	}
34745331Samw 	if (flags & FIGNORECASE)
34755331Samw 		zf |= ZCILOOK;
34765331Samw 
3477789Sahrens top:
3478789Sahrens 	/*
3479789Sahrens 	 * We do not support links between attributes and non-attributes
3480789Sahrens 	 * because of the potential security risk of creating links
3481789Sahrens 	 * into "normal" file space in order to circumvent restrictions
3482789Sahrens 	 * imposed in attribute space.
3483789Sahrens 	 */
3484789Sahrens 	if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
3485789Sahrens 	    (dzp->z_phys->zp_flags & ZFS_XATTR)) {
3486789Sahrens 		ZFS_EXIT(zfsvfs);
3487789Sahrens 		return (EINVAL);
3488789Sahrens 	}
3489789Sahrens 
3490789Sahrens 	/*
3491789Sahrens 	 * POSIX dictates that we return EPERM here.
3492789Sahrens 	 * Better choices include ENOTSUP or EISDIR.
3493789Sahrens 	 */
3494789Sahrens 	if (svp->v_type == VDIR) {
3495789Sahrens 		ZFS_EXIT(zfsvfs);
3496789Sahrens 		return (EPERM);
3497789Sahrens 	}
3498789Sahrens 
34995959Smarks 	owner = zfs_fuid_map_id(zfsvfs, szp->z_phys->zp_uid, cr, ZFS_OWNER);
35005331Samw 	if (owner != crgetuid(cr) &&
3501789Sahrens 	    secpolicy_basic_link(cr) != 0) {
3502789Sahrens 		ZFS_EXIT(zfsvfs);
3503789Sahrens 		return (EPERM);
3504789Sahrens 	}
3505789Sahrens 
35065331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3507789Sahrens 		ZFS_EXIT(zfsvfs);
3508789Sahrens 		return (error);
3509789Sahrens 	}
3510789Sahrens 
3511789Sahrens 	/*
3512789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3513789Sahrens 	 */
35145331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
35155331Samw 	if (error) {
3516789Sahrens 		ZFS_EXIT(zfsvfs);
3517789Sahrens 		return (error);
3518789Sahrens 	}
3519789Sahrens 
3520789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3521789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);
35221544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
35238227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3524789Sahrens 	if (error) {
3525789Sahrens 		zfs_dirent_unlock(dl);
35268227SNeil.Perrin@Sun.COM 		if (error == ERESTART) {
35272113Sahrens 			dmu_tx_wait(tx);
35282113Sahrens 			dmu_tx_abort(tx);
3529789Sahrens 			goto top;
3530789Sahrens 		}
35312113Sahrens 		dmu_tx_abort(tx);
3532789Sahrens 		ZFS_EXIT(zfsvfs);
3533789Sahrens 		return (error);
3534789Sahrens 	}
3535789Sahrens 
3536789Sahrens 	error = zfs_link_create(dl, szp, tx, 0);
3537789Sahrens 
35385331Samw 	if (error == 0) {
35395331Samw 		uint64_t txtype = TX_LINK;
35405331Samw 		if (flags & FIGNORECASE)
35415331Samw 			txtype |= TX_CI;
35425331Samw 		zfs_log_link(zilog, tx, txtype, dzp, szp, name);
35435331Samw 	}
3544789Sahrens 
3545789Sahrens 	dmu_tx_commit(tx);
3546789Sahrens 
3547789Sahrens 	zfs_dirent_unlock(dl);
3548789Sahrens 
35494863Spraks 	if (error == 0) {
35505331Samw 		vnevent_link(svp, ct);
35514863Spraks 	}
35524863Spraks 
3553789Sahrens 	ZFS_EXIT(zfsvfs);
3554789Sahrens 	return (error);
3555789Sahrens }
3556789Sahrens 
3557789Sahrens /*
3558789Sahrens  * zfs_null_putapage() is used when the file system has been force
3559789Sahrens  * unmounted. It just drops the pages.
3560789Sahrens  */
3561789Sahrens /* ARGSUSED */
3562789Sahrens static int
3563789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3564789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3565789Sahrens {
3566789Sahrens 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
3567789Sahrens 	return (0);
3568789Sahrens }
3569789Sahrens 
35702688Smaybee /*
35712688Smaybee  * Push a page out to disk, klustering if possible.
35722688Smaybee  *
35732688Smaybee  *	IN:	vp	- file to push page to.
35742688Smaybee  *		pp	- page to push.
35752688Smaybee  *		flags	- additional flags.
35762688Smaybee  *		cr	- credentials of caller.
35772688Smaybee  *
35782688Smaybee  *	OUT:	offp	- start of range pushed.
35792688Smaybee  *		lenp	- len of range pushed.
35802688Smaybee  *
35812688Smaybee  *	RETURN:	0 if success
35822688Smaybee  *		error code if failure
35832688Smaybee  *
35842688Smaybee  * NOTE: callers must have locked the page to be pushed.  On
35852688Smaybee  * exit, the page (and all other pages in the kluster) must be
35862688Smaybee  * unlocked.
35872688Smaybee  */
3588789Sahrens /* ARGSUSED */
3589789Sahrens static int
3590789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3591789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3592789Sahrens {
3593789Sahrens 	znode_t		*zp = VTOZ(vp);
3594789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3595789Sahrens 	dmu_tx_t	*tx;
35962688Smaybee 	u_offset_t	off, koff;
35972688Smaybee 	size_t		len, klen;
35984709Smaybee 	uint64_t	filesz;
3599789Sahrens 	int		err;
3600789Sahrens 
36014709Smaybee 	filesz = zp->z_phys->zp_size;
36022688Smaybee 	off = pp->p_offset;
36032688Smaybee 	len = PAGESIZE;
36042688Smaybee 	/*
36052688Smaybee 	 * If our blocksize is bigger than the page size, try to kluster
36068227SNeil.Perrin@Sun.COM 	 * multiple pages so that we write a full block (thus avoiding
36072688Smaybee 	 * a read-modify-write).
36082688Smaybee 	 */
36094709Smaybee 	if (off < filesz && zp->z_blksz > PAGESIZE) {
36108636SMark.Maybee@Sun.COM 		klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
36118636SMark.Maybee@Sun.COM 		koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
36122688Smaybee 		ASSERT(koff <= filesz);
36132688Smaybee 		if (koff + klen > filesz)
36142688Smaybee 			klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE);
36152688Smaybee 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
36162688Smaybee 	}
36172688Smaybee 	ASSERT3U(btop(len), ==, btopr(len));
36188636SMark.Maybee@Sun.COM 
36191819Smaybee 	/*
36201819Smaybee 	 * Can't push pages past end-of-file.
36211819Smaybee 	 */
36224709Smaybee 	if (off >= filesz) {
36234709Smaybee 		/* ignore all pages */
36242688Smaybee 		err = 0;
36252688Smaybee 		goto out;
36264709Smaybee 	} else if (off + len > filesz) {
36274709Smaybee 		int npages = btopr(filesz - off);
36282688Smaybee 		page_t *trunc;
36292688Smaybee 
36302688Smaybee 		page_list_break(&pp, &trunc, npages);
36314709Smaybee 		/* ignore pages past end of file */
36322688Smaybee 		if (trunc)
36334709Smaybee 			pvn_write_done(trunc, flags);
36344709Smaybee 		len = filesz - off;
36351819Smaybee 	}
3636*9396SMatthew.Ahrens@Sun.COM 
3637*9396SMatthew.Ahrens@Sun.COM 	if (zfs_usergroup_overquota(zfsvfs, B_FALSE, zp->z_phys->zp_uid) ||
3638*9396SMatthew.Ahrens@Sun.COM 	    zfs_usergroup_overquota(zfsvfs, B_TRUE, zp->z_phys->zp_gid)) {
3639*9396SMatthew.Ahrens@Sun.COM 		err = EDQUOT;
3640*9396SMatthew.Ahrens@Sun.COM 		goto out;
3641*9396SMatthew.Ahrens@Sun.COM 	}
36428636SMark.Maybee@Sun.COM top:
3643789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3644789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, off, len);
3645789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
36468227SNeil.Perrin@Sun.COM 	err = dmu_tx_assign(tx, TXG_NOWAIT);
3647789Sahrens 	if (err != 0) {
36488227SNeil.Perrin@Sun.COM 		if (err == ERESTART) {
36492113Sahrens 			dmu_tx_wait(tx);
36502113Sahrens 			dmu_tx_abort(tx);
3651789Sahrens 			goto top;
3652789Sahrens 		}
36532113Sahrens 		dmu_tx_abort(tx);
3654789Sahrens 		goto out;
3655789Sahrens 	}
3656789Sahrens 
36572688Smaybee 	if (zp->z_blksz <= PAGESIZE) {
36587315SJonathan.Adams@Sun.COM 		caddr_t va = zfs_map_page(pp, S_READ);
36592688Smaybee 		ASSERT3U(len, <=, PAGESIZE);
36602688Smaybee 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
36617315SJonathan.Adams@Sun.COM 		zfs_unmap_page(pp, va);
36622688Smaybee 	} else {
36632688Smaybee 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
36642688Smaybee 	}
36652688Smaybee 
36662688Smaybee 	if (err == 0) {
36672688Smaybee 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
36688636SMark.Maybee@Sun.COM 		zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
36692688Smaybee 		dmu_tx_commit(tx);
36702688Smaybee 	}
36712688Smaybee 
36722688Smaybee out:
36734709Smaybee 	pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
3674789Sahrens 	if (offp)
3675789Sahrens 		*offp = off;
3676789Sahrens 	if (lenp)
3677789Sahrens 		*lenp = len;
3678789Sahrens 
3679789Sahrens 	return (err);
3680789Sahrens }
3681789Sahrens 
3682789Sahrens /*
3683789Sahrens  * Copy the portion of the file indicated from pages into the file.
3684789Sahrens  * The pages are stored in a page list attached to the files vnode.
3685789Sahrens  *
3686789Sahrens  *	IN:	vp	- vnode of file to push page data to.
3687789Sahrens  *		off	- position in file to put data.
3688789Sahrens  *		len	- amount of data to write.
3689789Sahrens  *		flags	- flags to control the operation.
3690789Sahrens  *		cr	- credentials of caller.
36915331Samw  *		ct	- caller context.
3692789Sahrens  *
3693789Sahrens  *	RETURN:	0 if success
3694789Sahrens  *		error code if failure
3695789Sahrens  *
3696789Sahrens  * Timestamps:
3697789Sahrens  *	vp - ctime|mtime updated
3698789Sahrens  */
36995331Samw /*ARGSUSED*/
3700789Sahrens static int
37015331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
37025331Samw     caller_context_t *ct)
3703789Sahrens {
3704789Sahrens 	znode_t		*zp = VTOZ(vp);
3705789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3706789Sahrens 	page_t		*pp;
3707789Sahrens 	size_t		io_len;
3708789Sahrens 	u_offset_t	io_off;
37098636SMark.Maybee@Sun.COM 	uint_t		blksz;
37108636SMark.Maybee@Sun.COM 	rl_t		*rl;
3711789Sahrens 	int		error = 0;
3712789Sahrens 
37135367Sahrens 	ZFS_ENTER(zfsvfs);
37145367Sahrens 	ZFS_VERIFY_ZP(zp);
3715789Sahrens 
37168636SMark.Maybee@Sun.COM 	/*
37178636SMark.Maybee@Sun.COM 	 * Align this request to the file block size in case we kluster.
37188636SMark.Maybee@Sun.COM 	 * XXX - this can result in pretty aggresive locking, which can
37198636SMark.Maybee@Sun.COM 	 * impact simultanious read/write access.  One option might be
37208636SMark.Maybee@Sun.COM 	 * to break up long requests (len == 0) into block-by-block
37218636SMark.Maybee@Sun.COM 	 * operations to get narrower locking.
37228636SMark.Maybee@Sun.COM 	 */
37238636SMark.Maybee@Sun.COM 	blksz = zp->z_blksz;
37248636SMark.Maybee@Sun.COM 	if (ISP2(blksz))
37258636SMark.Maybee@Sun.COM 		io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
37268636SMark.Maybee@Sun.COM 	else
37278636SMark.Maybee@Sun.COM 		io_off = 0;
37288636SMark.Maybee@Sun.COM 	if (len > 0 && ISP2(blksz))
37299141SMark.Maybee@Sun.COM 		io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
37308636SMark.Maybee@Sun.COM 	else
37318636SMark.Maybee@Sun.COM 		io_len = 0;
37328636SMark.Maybee@Sun.COM 
37338636SMark.Maybee@Sun.COM 	if (io_len == 0) {
3734789Sahrens 		/*
37358636SMark.Maybee@Sun.COM 		 * Search the entire vp list for pages >= io_off.
3736789Sahrens 		 */
37378636SMark.Maybee@Sun.COM 		rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
37388636SMark.Maybee@Sun.COM 		error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
37391472Sperrin 		goto out;
3740789Sahrens 	}
37418636SMark.Maybee@Sun.COM 	rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
37428636SMark.Maybee@Sun.COM 
37438636SMark.Maybee@Sun.COM 	if (off > zp->z_phys->zp_size) {
3744789Sahrens 		/* past end of file */
37458636SMark.Maybee@Sun.COM 		zfs_range_unlock(rl);
3746789Sahrens 		ZFS_EXIT(zfsvfs);
3747789Sahrens 		return (0);
3748789Sahrens 	}
3749789Sahrens 
37508636SMark.Maybee@Sun.COM 	len = MIN(io_len, P2ROUNDUP(zp->z_phys->zp_size, PAGESIZE) - io_off);
37518636SMark.Maybee@Sun.COM 
37528636SMark.Maybee@Sun.COM 	for (off = io_off; io_off < off + len; io_off += io_len) {
3753789Sahrens 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
37541669Sperrin 			pp = page_lookup(vp, io_off,
37554339Sperrin 			    (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
3756789Sahrens 		} else {
3757789Sahrens 			pp = page_lookup_nowait(vp, io_off,
37584339Sperrin 			    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
3759789Sahrens 		}
3760789Sahrens 
3761789Sahrens 		if (pp != NULL && pvn_getdirty(pp, flags)) {
3762789Sahrens 			int err;
3763789Sahrens 
3764789Sahrens 			/*
3765789Sahrens 			 * Found a dirty page to push
3766789Sahrens 			 */
37671669Sperrin 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
37681669Sperrin 			if (err)
3769789Sahrens 				error = err;
3770789Sahrens 		} else {
3771789Sahrens 			io_len = PAGESIZE;
3772789Sahrens 		}
3773789Sahrens 	}
37741472Sperrin out:
37758636SMark.Maybee@Sun.COM 	zfs_range_unlock(rl);
37762638Sperrin 	if ((flags & B_ASYNC) == 0)
37772638Sperrin 		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
3778789Sahrens 	ZFS_EXIT(zfsvfs);
3779789Sahrens 	return (error);
3780789Sahrens }
3781789Sahrens 
37825331Samw /*ARGSUSED*/
3783789Sahrens void
37845331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
3785789Sahrens {
3786789Sahrens 	znode_t	*zp = VTOZ(vp);
3787789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3788789Sahrens 	int error;
3789789Sahrens 
37905326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
37915446Sahrens 	if (zp->z_dbuf == NULL) {
37925446Sahrens 		/*
37935642Smaybee 		 * The fs has been unmounted, or we did a
37945642Smaybee 		 * suspend/resume and this file no longer exists.
37955446Sahrens 		 */
3796789Sahrens 		if (vn_has_cached_data(vp)) {
3797789Sahrens 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
3798789Sahrens 			    B_INVAL, cr);
3799789Sahrens 		}
3800789Sahrens 
38011544Seschrock 		mutex_enter(&zp->z_lock);
3802789Sahrens 		vp->v_count = 0; /* count arrives as 1 */
38035446Sahrens 		mutex_exit(&zp->z_lock);
38045642Smaybee 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
38055446Sahrens 		zfs_znode_free(zp);
3806789Sahrens 		return;
3807789Sahrens 	}
3808789Sahrens 
3809789Sahrens 	/*
3810789Sahrens 	 * Attempt to push any data in the page cache.  If this fails
3811789Sahrens 	 * we will get kicked out later in zfs_zinactive().
3812789Sahrens 	 */
38131298Sperrin 	if (vn_has_cached_data(vp)) {
38141298Sperrin 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
38151298Sperrin 		    cr);
38161298Sperrin 	}
3817789Sahrens 
38183461Sahrens 	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3819789Sahrens 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3820789Sahrens 
3821789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
3822789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
3823789Sahrens 		if (error) {
3824789Sahrens 			dmu_tx_abort(tx);
3825789Sahrens 		} else {
3826789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
3827789Sahrens 			mutex_enter(&zp->z_lock);
3828789Sahrens 			zp->z_atime_dirty = 0;
3829789Sahrens 			mutex_exit(&zp->z_lock);
3830789Sahrens 			dmu_tx_commit(tx);
3831789Sahrens 		}
3832789Sahrens 	}
3833789Sahrens 
3834789Sahrens 	zfs_zinactive(zp);
38355326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
3836789Sahrens }
3837789Sahrens 
3838789Sahrens /*
3839789Sahrens  * Bounds-check the seek operation.
3840789Sahrens  *
3841789Sahrens  *	IN:	vp	- vnode seeking within
3842789Sahrens  *		ooff	- old file offset
3843789Sahrens  *		noffp	- pointer to new file offset
38445331Samw  *		ct	- caller context
3845789Sahrens  *
3846789Sahrens  *	RETURN:	0 if success
3847789Sahrens  *		EINVAL if new offset invalid
3848789Sahrens  */
3849789Sahrens /* ARGSUSED */
3850789Sahrens static int
38515331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
38525331Samw     caller_context_t *ct)
3853789Sahrens {
3854789Sahrens 	if (vp->v_type == VDIR)
3855789Sahrens 		return (0);
3856789Sahrens 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
3857789Sahrens }
3858789Sahrens 
3859789Sahrens /*
3860789Sahrens  * Pre-filter the generic locking function to trap attempts to place
3861789Sahrens  * a mandatory lock on a memory mapped file.
3862789Sahrens  */
3863789Sahrens static int
3864789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
38655331Samw     flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
3866789Sahrens {
3867789Sahrens 	znode_t *zp = VTOZ(vp);
3868789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3869789Sahrens 	int error;
3870789Sahrens 
38715367Sahrens 	ZFS_ENTER(zfsvfs);
38725367Sahrens 	ZFS_VERIFY_ZP(zp);
3873789Sahrens 
3874789Sahrens 	/*
38751544Seschrock 	 * We are following the UFS semantics with respect to mapcnt
38761544Seschrock 	 * here: If we see that the file is mapped already, then we will
38771544Seschrock 	 * return an error, but we don't worry about races between this
38781544Seschrock 	 * function and zfs_map().
3879789Sahrens 	 */
38801544Seschrock 	if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) {
3881789Sahrens 		ZFS_EXIT(zfsvfs);
3882789Sahrens 		return (EAGAIN);
3883789Sahrens 	}
38845331Samw 	error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct);
3885789Sahrens 	ZFS_EXIT(zfsvfs);
3886789Sahrens 	return (error);
3887789Sahrens }
3888789Sahrens 
3889789Sahrens /*
3890789Sahrens  * If we can't find a page in the cache, we will create a new page
3891789Sahrens  * and fill it with file data.  For efficiency, we may try to fill
38928636SMark.Maybee@Sun.COM  * multiple pages at once (klustering) to fill up the supplied page
38939265SMark.Maybee@Sun.COM  * list.  Note that the pages to be filled are held with an exclusive
38949265SMark.Maybee@Sun.COM  * lock to prevent access by other threads while they are being filled.
3895789Sahrens  */
3896789Sahrens static int
3897789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
3898789Sahrens     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
3899789Sahrens {
3900789Sahrens 	znode_t *zp = VTOZ(vp);
3901789Sahrens 	page_t *pp, *cur_pp;
3902789Sahrens 	objset_t *os = zp->z_zfsvfs->z_os;
3903789Sahrens 	u_offset_t io_off, total;
3904789Sahrens 	size_t io_len;
3905789Sahrens 	int err;
3906789Sahrens 
39072688Smaybee 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
39088636SMark.Maybee@Sun.COM 		/*
39098636SMark.Maybee@Sun.COM 		 * We only have a single page, don't bother klustering
39108636SMark.Maybee@Sun.COM 		 */
3911789Sahrens 		io_off = off;
3912789Sahrens 		io_len = PAGESIZE;
39139265SMark.Maybee@Sun.COM 		pp = page_create_va(vp, io_off, io_len,
39149265SMark.Maybee@Sun.COM 		    PG_EXCL | PG_WAIT, seg, addr);
3915789Sahrens 	} else {
3916789Sahrens 		/*
39178636SMark.Maybee@Sun.COM 		 * Try to find enough pages to fill the page list
3918789Sahrens 		 */
3919789Sahrens 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
39208636SMark.Maybee@Sun.COM 		    &io_len, off, plsz, 0);
3921789Sahrens 	}
3922789Sahrens 	if (pp == NULL) {
3923789Sahrens 		/*
39248636SMark.Maybee@Sun.COM 		 * The page already exists, nothing to do here.
3925789Sahrens 		 */
3926789Sahrens 		*pl = NULL;
3927789Sahrens 		return (0);
3928789Sahrens 	}
3929789Sahrens 
3930789Sahrens 	/*
3931789Sahrens 	 * Fill the pages in the kluster.
3932789Sahrens 	 */
3933789Sahrens 	cur_pp = pp;
3934789Sahrens 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
39358636SMark.Maybee@Sun.COM 		caddr_t va;
39368636SMark.Maybee@Sun.COM 
39372688Smaybee 		ASSERT3U(io_off, ==, cur_pp->p_offset);
39387315SJonathan.Adams@Sun.COM 		va = zfs_map_page(cur_pp, S_WRITE);
39398636SMark.Maybee@Sun.COM 		err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va);
39407315SJonathan.Adams@Sun.COM 		zfs_unmap_page(cur_pp, va);
3941789Sahrens 		if (err) {
3942789Sahrens 			/* On error, toss the entire kluster */
3943789Sahrens 			pvn_read_done(pp, B_ERROR);
39447294Sperrin 			/* convert checksum errors into IO errors */
39457294Sperrin 			if (err == ECKSUM)
39467294Sperrin 				err = EIO;
3947789Sahrens 			return (err);
3948789Sahrens 		}
3949789Sahrens 		cur_pp = cur_pp->p_next;
3950789Sahrens 	}
39518636SMark.Maybee@Sun.COM 
3952789Sahrens 	/*
39538636SMark.Maybee@Sun.COM 	 * Fill in the page list array from the kluster starting
39548636SMark.Maybee@Sun.COM 	 * from the desired offset `off'.
3955789Sahrens 	 * NOTE: the page list will always be null terminated.
3956789Sahrens 	 */
3957789Sahrens 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
39588636SMark.Maybee@Sun.COM 	ASSERT(pl == NULL || (*pl)->p_offset == off);
3959789Sahrens 
3960789Sahrens 	return (0);
3961789Sahrens }
3962789Sahrens 
3963789Sahrens /*
3964789Sahrens  * Return pointers to the pages for the file region [off, off + len]
3965789Sahrens  * in the pl array.  If plsz is greater than len, this function may
39668636SMark.Maybee@Sun.COM  * also return page pointers from after the specified region
39678636SMark.Maybee@Sun.COM  * (i.e. the region [off, off + plsz]).  These additional pages are
39688636SMark.Maybee@Sun.COM  * only returned if they are already in the cache, or were created as
39698636SMark.Maybee@Sun.COM  * part of a klustered read.
3970789Sahrens  *
3971789Sahrens  *	IN:	vp	- vnode of file to get data from.
3972789Sahrens  *		off	- position in file to get data from.
3973789Sahrens  *		len	- amount of data to retrieve.
3974789Sahrens  *		plsz	- length of provided page list.
3975789Sahrens  *		seg	- segment to obtain pages for.
3976789Sahrens  *		addr	- virtual address of fault.
3977789Sahrens  *		rw	- mode of created pages.
3978789Sahrens  *		cr	- credentials of caller.
39795331Samw  *		ct	- caller context.
3980789Sahrens  *
3981789Sahrens  *	OUT:	protp	- protection mode of created pages.
3982789Sahrens  *		pl	- list of pages created.
3983789Sahrens  *
3984789Sahrens  *	RETURN:	0 if success
3985789Sahrens  *		error code if failure
3986789Sahrens  *
3987789Sahrens  * Timestamps:
3988789Sahrens  *	vp - atime updated
3989789Sahrens  */
3990789Sahrens /* ARGSUSED */
3991789Sahrens static int
3992789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
3993789Sahrens 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
39945331Samw 	enum seg_rw rw, cred_t *cr, caller_context_t *ct)
3995789Sahrens {
3996789Sahrens 	znode_t		*zp = VTOZ(vp);
3997789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
39988636SMark.Maybee@Sun.COM 	page_t		**pl0 = pl;
39998636SMark.Maybee@Sun.COM 	int		err = 0;
40008636SMark.Maybee@Sun.COM 
40018636SMark.Maybee@Sun.COM 	/* we do our own caching, faultahead is unnecessary */
40028636SMark.Maybee@Sun.COM 	if (pl == NULL)
40038636SMark.Maybee@Sun.COM 		return (0);
40048636SMark.Maybee@Sun.COM 	else if (len > plsz)
40058636SMark.Maybee@Sun.COM 		len = plsz;
40068681SMark.Maybee@Sun.COM 	else
40078681SMark.Maybee@Sun.COM 		len = P2ROUNDUP(len, PAGESIZE);
40088636SMark.Maybee@Sun.COM 	ASSERT(plsz >= len);
4009789Sahrens 
40105367Sahrens 	ZFS_ENTER(zfsvfs);
40115367Sahrens 	ZFS_VERIFY_ZP(zp);
4012789Sahrens 
4013789Sahrens 	if (protp)
4014789Sahrens 		*protp = PROT_ALL;
4015789Sahrens 
4016789Sahrens 	/*
40179265SMark.Maybee@Sun.COM 	 * Loop through the requested range [off, off + len) looking
4018789Sahrens 	 * for pages.  If we don't find a page, we will need to create
4019789Sahrens 	 * a new page and fill it with data from the file.
4020789Sahrens 	 */
4021789Sahrens 	while (len > 0) {
40228636SMark.Maybee@Sun.COM 		if (*pl = page_lookup(vp, off, SE_SHARED))
40238636SMark.Maybee@Sun.COM 			*(pl+1) = NULL;
40248636SMark.Maybee@Sun.COM 		else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
40258636SMark.Maybee@Sun.COM 			goto out;
40268636SMark.Maybee@Sun.COM 		while (*pl) {
40278636SMark.Maybee@Sun.COM 			ASSERT3U((*pl)->p_offset, ==, off);
4028789Sahrens 			off += PAGESIZE;
4029789Sahrens 			addr += PAGESIZE;
40308681SMark.Maybee@Sun.COM 			if (len > 0) {
40318681SMark.Maybee@Sun.COM 				ASSERT3U(len, >=, PAGESIZE);
40328636SMark.Maybee@Sun.COM 				len -= PAGESIZE;
40338681SMark.Maybee@Sun.COM 			}
40348636SMark.Maybee@Sun.COM 			ASSERT3U(plsz, >=, PAGESIZE);
4035789Sahrens 			plsz -= PAGESIZE;
40368636SMark.Maybee@Sun.COM 			pl++;
4037789Sahrens 		}
4038789Sahrens 	}
4039789Sahrens 
4040789Sahrens 	/*
4041789Sahrens 	 * Fill out the page array with any pages already in the cache.
4042789Sahrens 	 */
40438636SMark.Maybee@Sun.COM 	while (plsz > 0 &&
40448636SMark.Maybee@Sun.COM 	    (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
40458636SMark.Maybee@Sun.COM 			off += PAGESIZE;
40468636SMark.Maybee@Sun.COM 			plsz -= PAGESIZE;
4047789Sahrens 	}
4048789Sahrens out:
40492752Sperrin 	if (err) {
40502752Sperrin 		/*
40512752Sperrin 		 * Release any pages we have previously locked.
40522752Sperrin 		 */
40532752Sperrin 		while (pl > pl0)
40542752Sperrin 			page_unlock(*--pl);
40558636SMark.Maybee@Sun.COM 	} else {
40568636SMark.Maybee@Sun.COM 		ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
40572752Sperrin 	}
40582752Sperrin 
4059789Sahrens 	*pl = NULL;
4060789Sahrens 
4061789Sahrens 	ZFS_EXIT(zfsvfs);
4062789Sahrens 	return (err);
4063789Sahrens }
4064789Sahrens 
40651544Seschrock /*
40661544Seschrock  * Request a memory map for a section of a file.  This code interacts
40671544Seschrock  * with common code and the VM system as follows:
40681544Seschrock  *
40691544Seschrock  *	common code calls mmap(), which ends up in smmap_common()
40701544Seschrock  *
40711544Seschrock  *	this calls VOP_MAP(), which takes you into (say) zfs
40721544Seschrock  *
40731544Seschrock  *	zfs_map() calls as_map(), passing segvn_create() as the callback
40741544Seschrock  *
40751544Seschrock  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
40761544Seschrock  *
40771544Seschrock  *	zfs_addmap() updates z_mapcnt
40781544Seschrock  */
40795331Samw /*ARGSUSED*/
4080789Sahrens static int
4081789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
40825331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
40835331Samw     caller_context_t *ct)
4084789Sahrens {
4085789Sahrens 	znode_t *zp = VTOZ(vp);
4086789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4087789Sahrens 	segvn_crargs_t	vn_a;
4088789Sahrens 	int		error;
4089789Sahrens 
40905929Smarks 	ZFS_ENTER(zfsvfs);
40915929Smarks 	ZFS_VERIFY_ZP(zp);
40925929Smarks 
40935331Samw 	if ((prot & PROT_WRITE) &&
40945331Samw 	    (zp->z_phys->zp_flags & (ZFS_IMMUTABLE | ZFS_READONLY |
40955929Smarks 	    ZFS_APPENDONLY))) {
40965929Smarks 		ZFS_EXIT(zfsvfs);
40975331Samw 		return (EPERM);
40985929Smarks 	}
40995929Smarks 
41005929Smarks 	if ((prot & (PROT_READ | PROT_EXEC)) &&
41015929Smarks 	    (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED)) {
41025929Smarks 		ZFS_EXIT(zfsvfs);
41035929Smarks 		return (EACCES);
41045929Smarks 	}
4105789Sahrens 
4106789Sahrens 	if (vp->v_flag & VNOMAP) {
4107789Sahrens 		ZFS_EXIT(zfsvfs);
4108789Sahrens 		return (ENOSYS);
4109789Sahrens 	}
4110789Sahrens 
4111789Sahrens 	if (off < 0 || len > MAXOFFSET_T - off) {
4112789Sahrens 		ZFS_EXIT(zfsvfs);
4113789Sahrens 		return (ENXIO);
4114789Sahrens 	}
4115789Sahrens 
4116789Sahrens 	if (vp->v_type != VREG) {
4117789Sahrens 		ZFS_EXIT(zfsvfs);
4118789Sahrens 		return (ENODEV);
4119789Sahrens 	}
4120789Sahrens 
4121789Sahrens 	/*
4122789Sahrens 	 * If file is locked, disallow mapping.
4123789Sahrens 	 */
41241544Seschrock 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) {
41251544Seschrock 		ZFS_EXIT(zfsvfs);
41261544Seschrock 		return (EAGAIN);
4127789Sahrens 	}
4128789Sahrens 
4129789Sahrens 	as_rangelock(as);
41306036Smec 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
41316036Smec 	if (error != 0) {
41326036Smec 		as_rangeunlock(as);
41336036Smec 		ZFS_EXIT(zfsvfs);
41346036Smec 		return (error);
4135789Sahrens 	}
4136789Sahrens 
4137789Sahrens 	vn_a.vp = vp;
4138789Sahrens 	vn_a.offset = (u_offset_t)off;
4139789Sahrens 	vn_a.type = flags & MAP_TYPE;
4140789Sahrens 	vn_a.prot = prot;
4141789Sahrens 	vn_a.maxprot = maxprot;
4142789Sahrens 	vn_a.cred = cr;
4143789Sahrens 	vn_a.amp = NULL;
4144789Sahrens 	vn_a.flags = flags & ~MAP_TYPE;
41451417Skchow 	vn_a.szc = 0;
41461417Skchow 	vn_a.lgrp_mem_policy_flags = 0;
4147789Sahrens 
4148789Sahrens 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
4149789Sahrens 
4150789Sahrens 	as_rangeunlock(as);
4151789Sahrens 	ZFS_EXIT(zfsvfs);
4152789Sahrens 	return (error);
4153789Sahrens }
4154789Sahrens 
4155789Sahrens /* ARGSUSED */
4156789Sahrens static int
4157789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
41585331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
41595331Samw     caller_context_t *ct)
4160789Sahrens {
41611544Seschrock 	uint64_t pages = btopr(len);
41621544Seschrock 
41631544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4164789Sahrens 	return (0);
4165789Sahrens }
4166789Sahrens 
41671773Seschrock /*
41681773Seschrock  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
41691773Seschrock  * more accurate mtime for the associated file.  Since we don't have a way of
41701773Seschrock  * detecting when the data was actually modified, we have to resort to
41711773Seschrock  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
41721773Seschrock  * last page is pushed.  The problem occurs when the msync() call is omitted,
41731773Seschrock  * which by far the most common case:
41741773Seschrock  *
41751773Seschrock  * 	open()
41761773Seschrock  * 	mmap()
41771773Seschrock  * 	<modify memory>
41781773Seschrock  * 	munmap()
41791773Seschrock  * 	close()
41801773Seschrock  * 	<time lapse>
41811773Seschrock  * 	putpage() via fsflush
41821773Seschrock  *
41831773Seschrock  * If we wait until fsflush to come along, we can have a modification time that
41841773Seschrock  * is some arbitrary point in the future.  In order to prevent this in the
41851773Seschrock  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
41861773Seschrock  * torn down.
41871773Seschrock  */
4188789Sahrens /* ARGSUSED */
4189789Sahrens static int
4190789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
41915331Samw     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
41925331Samw     caller_context_t *ct)
4193789Sahrens {
41941544Seschrock 	uint64_t pages = btopr(len);
41951544Seschrock 
41961544Seschrock 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
41971544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
41981773Seschrock 
41991773Seschrock 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
42001773Seschrock 	    vn_has_cached_data(vp))
42015331Samw 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
42021773Seschrock 
4203789Sahrens 	return (0);
4204789Sahrens }
4205789Sahrens 
4206789Sahrens /*
4207789Sahrens  * Free or allocate space in a file.  Currently, this function only
4208789Sahrens  * supports the `F_FREESP' command.  However, this command is somewhat
4209789Sahrens  * misnamed, as its functionality includes the ability to allocate as
4210789Sahrens  * well as free space.
4211789Sahrens  *
4212789Sahrens  *	IN:	vp	- vnode of file to free data in.
4213789Sahrens  *		cmd	- action to take (only F_FREESP supported).
4214789Sahrens  *		bfp	- section of file to free/alloc.
4215789Sahrens  *		flag	- current file open mode flags.
4216789Sahrens  *		offset	- current file offset.
4217789Sahrens  *		cr	- credentials of caller [UNUSED].
42185331Samw  *		ct	- caller context.
4219789Sahrens  *
4220789Sahrens  *	RETURN:	0 if success
4221789Sahrens  *		error code if failure
4222789Sahrens  *
4223789Sahrens  * Timestamps:
4224789Sahrens  *	vp - ctime|mtime updated
4225789Sahrens  */
4226789Sahrens /* ARGSUSED */
4227789Sahrens static int
4228789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4229789Sahrens     offset_t offset, cred_t *cr, caller_context_t *ct)
4230789Sahrens {
4231789Sahrens 	znode_t		*zp = VTOZ(vp);
4232789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4233789Sahrens 	uint64_t	off, len;
4234789Sahrens 	int		error;
4235789Sahrens 
42365367Sahrens 	ZFS_ENTER(zfsvfs);
42375367Sahrens 	ZFS_VERIFY_ZP(zp);
4238789Sahrens 
4239789Sahrens 	if (cmd != F_FREESP) {
4240789Sahrens 		ZFS_EXIT(zfsvfs);
4241789Sahrens 		return (EINVAL);
4242789Sahrens 	}
4243789Sahrens 
4244789Sahrens 	if (error = convoff(vp, bfp, 0, offset)) {
4245789Sahrens 		ZFS_EXIT(zfsvfs);
4246789Sahrens 		return (error);
4247789Sahrens 	}
4248789Sahrens 
4249789Sahrens 	if (bfp->l_len < 0) {
4250789Sahrens 		ZFS_EXIT(zfsvfs);
4251789Sahrens 		return (EINVAL);
4252789Sahrens 	}
4253789Sahrens 
4254789Sahrens 	off = bfp->l_start;
42551669Sperrin 	len = bfp->l_len; /* 0 means from off to end of file */
42561878Smaybee 
42576992Smaybee 	error = zfs_freesp(zp, off, len, flag, TRUE);
4258789Sahrens 
4259789Sahrens 	ZFS_EXIT(zfsvfs);
4260789Sahrens 	return (error);
4261789Sahrens }
4262789Sahrens 
42635331Samw /*ARGSUSED*/
4264789Sahrens static int
42655331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4266789Sahrens {
4267789Sahrens 	znode_t		*zp = VTOZ(vp);
4268789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
42695326Sek110237 	uint32_t	gen;
4270789Sahrens 	uint64_t	object = zp->z_id;
4271789Sahrens 	zfid_short_t	*zfid;
4272789Sahrens 	int		size, i;
4273789Sahrens 
42745367Sahrens 	ZFS_ENTER(zfsvfs);
42755367Sahrens 	ZFS_VERIFY_ZP(zp);
42765326Sek110237 	gen = (uint32_t)zp->z_gen;
4277789Sahrens 
4278789Sahrens 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4279789Sahrens 	if (fidp->fid_len < size) {
4280789Sahrens 		fidp->fid_len = size;
42811512Sek110237 		ZFS_EXIT(zfsvfs);
4282789Sahrens 		return (ENOSPC);
4283789Sahrens 	}
4284789Sahrens 
4285789Sahrens 	zfid = (zfid_short_t *)fidp;
4286789Sahrens 
4287789Sahrens 	zfid->zf_len = size;
4288789Sahrens 
4289789Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
4290789Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4291789Sahrens 
4292789Sahrens 	/* Must have a non-zero generation number to distinguish from .zfs */
4293789Sahrens 	if (gen == 0)
4294789Sahrens 		gen = 1;
4295789Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
4296789Sahrens 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4297789Sahrens 
4298789Sahrens 	if (size == LONG_FID_LEN) {
4299789Sahrens 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
4300789Sahrens 		zfid_long_t	*zlfid;
4301789Sahrens 
4302789Sahrens 		zlfid = (zfid_long_t *)fidp;
4303789Sahrens 
4304789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4305789Sahrens 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4306789Sahrens 
4307789Sahrens 		/* XXX - this should be the generation number for the objset */
4308789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4309789Sahrens 			zlfid->zf_setgen[i] = 0;
4310789Sahrens 	}
4311789Sahrens 
4312789Sahrens 	ZFS_EXIT(zfsvfs);
4313789Sahrens 	return (0);
4314789Sahrens }
4315789Sahrens 
4316789Sahrens static int
43175331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
43185331Samw     caller_context_t *ct)
4319789Sahrens {
4320789Sahrens 	znode_t		*zp, *xzp;
4321789Sahrens 	zfsvfs_t	*zfsvfs;
4322789Sahrens 	zfs_dirlock_t	*dl;
4323789Sahrens 	int		error;
4324789Sahrens 
4325789Sahrens 	switch (cmd) {
4326789Sahrens 	case _PC_LINK_MAX:
4327789Sahrens 		*valp = ULONG_MAX;
4328789Sahrens 		return (0);
4329789Sahrens 
4330789Sahrens 	case _PC_FILESIZEBITS:
4331789Sahrens 		*valp = 64;
4332789Sahrens 		return (0);
4333789Sahrens 
4334789Sahrens 	case _PC_XATTR_EXISTS:
4335789Sahrens 		zp = VTOZ(vp);
4336789Sahrens 		zfsvfs = zp->z_zfsvfs;
43375367Sahrens 		ZFS_ENTER(zfsvfs);
43385367Sahrens 		ZFS_VERIFY_ZP(zp);
4339789Sahrens 		*valp = 0;
4340789Sahrens 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
43415331Samw 		    ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4342789Sahrens 		if (error == 0) {
4343789Sahrens 			zfs_dirent_unlock(dl);
4344789Sahrens 			if (!zfs_dirempty(xzp))
4345789Sahrens 				*valp = 1;
4346789Sahrens 			VN_RELE(ZTOV(xzp));
4347789Sahrens 		} else if (error == ENOENT) {
4348789Sahrens 			/*
4349789Sahrens 			 * If there aren't extended attributes, it's the
4350789Sahrens 			 * same as having zero of them.
4351789Sahrens 			 */
4352789Sahrens 			error = 0;
4353789Sahrens 		}
4354789Sahrens 		ZFS_EXIT(zfsvfs);
4355789Sahrens 		return (error);
4356789Sahrens 
43575331Samw 	case _PC_SATTR_ENABLED:
43585331Samw 	case _PC_SATTR_EXISTS:
43597757SJanice.Chang@Sun.COM 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
43605331Samw 		    (vp->v_type == VREG || vp->v_type == VDIR);
43615331Samw 		return (0);
43625331Samw 
4363789Sahrens 	case _PC_ACL_ENABLED:
4364789Sahrens 		*valp = _ACL_ACE_ENABLED;
4365789Sahrens 		return (0);
4366789Sahrens 
4367789Sahrens 	case _PC_MIN_HOLE_SIZE:
4368789Sahrens 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
4369789Sahrens 		return (0);
4370789Sahrens 
4371789Sahrens 	default:
43725331Samw 		return (fs_pathconf(vp, cmd, valp, cr, ct));
4373789Sahrens 	}
4374789Sahrens }
4375789Sahrens 
4376789Sahrens /*ARGSUSED*/
4377789Sahrens static int
43785331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
43795331Samw     caller_context_t *ct)
4380789Sahrens {
4381789Sahrens 	znode_t *zp = VTOZ(vp);
4382789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4383789Sahrens 	int error;
43845331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4385789Sahrens 
43865367Sahrens 	ZFS_ENTER(zfsvfs);
43875367Sahrens 	ZFS_VERIFY_ZP(zp);
43885331Samw 	error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4389789Sahrens 	ZFS_EXIT(zfsvfs);
4390789Sahrens 
4391789Sahrens 	return (error);
4392789Sahrens }
4393789Sahrens 
4394789Sahrens /*ARGSUSED*/
4395789Sahrens static int
43965331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
43975331Samw     caller_context_t *ct)
4398789Sahrens {
4399789Sahrens 	znode_t *zp = VTOZ(vp);
4400789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4401789Sahrens 	int error;
44025331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4403789Sahrens 
44045367Sahrens 	ZFS_ENTER(zfsvfs);
44055367Sahrens 	ZFS_VERIFY_ZP(zp);
44065331Samw 	error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4407789Sahrens 	ZFS_EXIT(zfsvfs);
4408789Sahrens 	return (error);
4409789Sahrens }
4410789Sahrens 
4411789Sahrens /*
4412789Sahrens  * Predeclare these here so that the compiler assumes that
4413789Sahrens  * this is an "old style" function declaration that does
4414789Sahrens  * not include arguments => we won't get type mismatch errors
4415789Sahrens  * in the initializations that follow.
4416789Sahrens  */
4417789Sahrens static int zfs_inval();
4418789Sahrens static int zfs_isdir();
4419789Sahrens 
4420789Sahrens static int
4421789Sahrens zfs_inval()
4422789Sahrens {
4423789Sahrens 	return (EINVAL);
4424789Sahrens }
4425789Sahrens 
4426789Sahrens static int
4427789Sahrens zfs_isdir()
4428789Sahrens {
4429789Sahrens 	return (EISDIR);
4430789Sahrens }
4431789Sahrens /*
4432789Sahrens  * Directory vnode operations template
4433789Sahrens  */
4434789Sahrens vnodeops_t *zfs_dvnodeops;
4435789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = {
44363898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
44373898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
44383898Srsb 	VOPNAME_READ,		{ .error = zfs_isdir },
44393898Srsb 	VOPNAME_WRITE,		{ .error = zfs_isdir },
44403898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
44413898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
44423898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
44433898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
44443898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
44453898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
44463898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
44473898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
44483898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
44493898Srsb 	VOPNAME_MKDIR,		{ .vop_mkdir = zfs_mkdir },
44503898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
44513898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
44523898Srsb 	VOPNAME_SYMLINK,	{ .vop_symlink = zfs_symlink },
44533898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
44543898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
44553898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
44563898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
44573898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
44583898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
44593898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
44604863Spraks 	VOPNAME_VNEVENT, 	{ .vop_vnevent = fs_vnevent_support },
44613898Srsb 	NULL,			NULL
4462789Sahrens };
4463789Sahrens 
4464789Sahrens /*
4465789Sahrens  * Regular file vnode operations template
4466789Sahrens  */
4467789Sahrens vnodeops_t *zfs_fvnodeops;
4468789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = {
44693898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
44703898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
44713898Srsb 	VOPNAME_READ,		{ .vop_read = zfs_read },
44723898Srsb 	VOPNAME_WRITE,		{ .vop_write = zfs_write },
44733898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
44743898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
44753898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
44763898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
44773898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
44783898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
44793898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
44803898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
44813898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
44823898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
44833898Srsb 	VOPNAME_FRLOCK,		{ .vop_frlock = zfs_frlock },
44843898Srsb 	VOPNAME_SPACE,		{ .vop_space = zfs_space },
44853898Srsb 	VOPNAME_GETPAGE,	{ .vop_getpage = zfs_getpage },
44863898Srsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = zfs_putpage },
44873898Srsb 	VOPNAME_MAP,		{ .vop_map = zfs_map },
44883898Srsb 	VOPNAME_ADDMAP,		{ .vop_addmap = zfs_addmap },
44893898Srsb 	VOPNAME_DELMAP,		{ .vop_delmap = zfs_delmap },
44903898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
44913898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
44923898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
44933898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
44943898Srsb 	NULL,			NULL
4495789Sahrens };
4496789Sahrens 
4497789Sahrens /*
4498789Sahrens  * Symbolic link vnode operations template
4499789Sahrens  */
4500789Sahrens vnodeops_t *zfs_symvnodeops;
4501789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = {
45023898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
45033898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
45043898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
45053898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
45063898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = zfs_readlink },
45073898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
45083898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
45093898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
45103898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
45113898Srsb 	NULL,			NULL
4512789Sahrens };
4513789Sahrens 
4514789Sahrens /*
45158845Samw@Sun.COM  * special share hidden files vnode operations template
45168845Samw@Sun.COM  */
45178845Samw@Sun.COM vnodeops_t *zfs_sharevnodeops;
45188845Samw@Sun.COM const fs_operation_def_t zfs_sharevnodeops_template[] = {
45198845Samw@Sun.COM 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
45208845Samw@Sun.COM 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
45218845Samw@Sun.COM 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
45228845Samw@Sun.COM 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
45238845Samw@Sun.COM 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
45248845Samw@Sun.COM 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
45258845Samw@Sun.COM 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
45268845Samw@Sun.COM 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
45278845Samw@Sun.COM 	NULL,			NULL
45288845Samw@Sun.COM };
45298845Samw@Sun.COM 
45308845Samw@Sun.COM /*
4531789Sahrens  * Extended attribute directory vnode operations template
4532789Sahrens  *	This template is identical to the directory vnodes
4533789Sahrens  *	operation template except for restricted operations:
4534789Sahrens  *		VOP_MKDIR()
4535789Sahrens  *		VOP_SYMLINK()
4536789Sahrens  * Note that there are other restrictions embedded in:
4537789Sahrens  *	zfs_create()	- restrict type to VREG
4538789Sahrens  *	zfs_link()	- no links into/out of attribute space
4539789Sahrens  *	zfs_rename()	- no moves into/out of attribute space
4540789Sahrens  */
4541789Sahrens vnodeops_t *zfs_xdvnodeops;
4542789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = {
45433898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
45443898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
45453898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
45463898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
45473898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
45483898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
45493898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
45503898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
45513898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
45523898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
45533898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
45543898Srsb 	VOPNAME_MKDIR,		{ .error = zfs_inval },
45553898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
45563898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
45573898Srsb 	VOPNAME_SYMLINK,	{ .error = zfs_inval },
45583898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
45593898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
45603898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
45613898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
45623898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
45633898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
45643898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
45653898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
45663898Srsb 	NULL,			NULL
4567789Sahrens };
4568789Sahrens 
4569789Sahrens /*
4570789Sahrens  * Error vnode operations template
4571789Sahrens  */
4572789Sahrens vnodeops_t *zfs_evnodeops;
4573789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = {
45743898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
45753898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
45763898Srsb 	NULL,			NULL
4577789Sahrens };
4578