xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 8190:6eeea43ced42)
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 /*
225771Sjp151216  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
264144Speteh /* Portions Copyright 2007 Jeremy Teo */
274144Speteh 
28789Sahrens #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.
104789Sahrens  *
1051757Sperrin  *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
1061757Sperrin  *	as they can span dmu_tx_assign() calls.
1071757Sperrin  *
1081757Sperrin  *  (4)	Always pass zfsvfs->z_assign as the second argument to dmu_tx_assign().
109789Sahrens  *	In normal operation, this will be TXG_NOWAIT.  During ZIL replay,
110789Sahrens  *	it will be a specific txg.  Either way, dmu_tx_assign() never blocks.
111789Sahrens  *	This is critical because we don't want to block while holding locks.
112789Sahrens  *	Note, in particular, that if a lock is sometimes acquired before
113789Sahrens  *	the tx assigns, and sometimes after (e.g. z_lock), then failing to
114789Sahrens  *	use a non-blocking assign can deadlock the system.  The scenario:
115789Sahrens  *
116789Sahrens  *	Thread A has grabbed a lock before calling dmu_tx_assign().
117789Sahrens  *	Thread B is in an already-assigned tx, and blocks for this lock.
118789Sahrens  *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
119789Sahrens  *	forever, because the previous txg can't quiesce until B's tx commits.
120789Sahrens  *
121789Sahrens  *	If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
1222113Sahrens  *	then drop all locks, call dmu_tx_wait(), and try again.
123789Sahrens  *
1241757Sperrin  *  (5)	If the operation succeeded, generate the intent log entry for it
125789Sahrens  *	before dropping locks.  This ensures that the ordering of events
126789Sahrens  *	in the intent log matches the order in which they actually occurred.
127789Sahrens  *
1281757Sperrin  *  (6)	At the end of each vnode op, the DMU tx must always commit,
129789Sahrens  *	regardless of whether there were any errors.
130789Sahrens  *
1312638Sperrin  *  (7)	After dropping all locks, invoke zil_commit(zilog, seq, foid)
132789Sahrens  *	to ensure that synchronous semantics are provided when necessary.
133789Sahrens  *
134789Sahrens  * In general, this is how things should be ordered in each vnode op:
135789Sahrens  *
136789Sahrens  *	ZFS_ENTER(zfsvfs);		// exit if unmounted
137789Sahrens  * top:
138789Sahrens  *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
139789Sahrens  *	rw_enter(...);			// grab any other locks you need
140789Sahrens  *	tx = dmu_tx_create(...);	// get DMU tx
141789Sahrens  *	dmu_tx_hold_*();		// hold each object you might modify
142789Sahrens  *	error = dmu_tx_assign(tx, zfsvfs->z_assign);	// try to assign
143789Sahrens  *	if (error) {
144789Sahrens  *		rw_exit(...);		// drop locks
145789Sahrens  *		zfs_dirent_unlock(dl);	// unlock directory entry
146789Sahrens  *		VN_RELE(...);		// release held vnodes
147789Sahrens  *		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1482113Sahrens  *			dmu_tx_wait(tx);
1492113Sahrens  *			dmu_tx_abort(tx);
150789Sahrens  *			goto top;
151789Sahrens  *		}
1522113Sahrens  *		dmu_tx_abort(tx);	// abort DMU tx
153789Sahrens  *		ZFS_EXIT(zfsvfs);	// finished in zfs
154789Sahrens  *		return (error);		// really out of space
155789Sahrens  *	}
156789Sahrens  *	error = do_real_work();		// do whatever this VOP does
157789Sahrens  *	if (error == 0)
1582638Sperrin  *		zfs_log_*(...);		// on success, make ZIL entry
159789Sahrens  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
160789Sahrens  *	rw_exit(...);			// drop locks
161789Sahrens  *	zfs_dirent_unlock(dl);		// unlock directory entry
162789Sahrens  *	VN_RELE(...);			// release held vnodes
1632638Sperrin  *	zil_commit(zilog, seq, foid);	// synchronous when necessary
164789Sahrens  *	ZFS_EXIT(zfsvfs);		// finished in zfs
165789Sahrens  *	return (error);			// done, report error
166789Sahrens  */
1675367Sahrens 
168789Sahrens /* ARGSUSED */
169789Sahrens static int
1705331Samw zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
171789Sahrens {
1723063Sperrin 	znode_t	*zp = VTOZ(*vpp);
1737844SMark.Shellenbaum@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1747844SMark.Shellenbaum@Sun.COM 
1757844SMark.Shellenbaum@Sun.COM 	ZFS_ENTER(zfsvfs);
1767844SMark.Shellenbaum@Sun.COM 	ZFS_VERIFY_ZP(zp);
1773063Sperrin 
1785331Samw 	if ((flag & FWRITE) && (zp->z_phys->zp_flags & ZFS_APPENDONLY) &&
1795331Samw 	    ((flag & FAPPEND) == 0)) {
1807844SMark.Shellenbaum@Sun.COM 		ZFS_EXIT(zfsvfs);
1815331Samw 		return (EPERM);
1825331Samw 	}
1835331Samw 
1845331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
1855331Samw 	    ZTOV(zp)->v_type == VREG &&
1865331Samw 	    !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
1877844SMark.Shellenbaum@Sun.COM 	    zp->z_phys->zp_size > 0) {
1887844SMark.Shellenbaum@Sun.COM 		if (fs_vscan(*vpp, cr, 0) != 0) {
1897844SMark.Shellenbaum@Sun.COM 			ZFS_EXIT(zfsvfs);
1905331Samw 			return (EACCES);
1917844SMark.Shellenbaum@Sun.COM 		}
1927844SMark.Shellenbaum@Sun.COM 	}
1935331Samw 
1943063Sperrin 	/* Keep a count of the synchronous opens in the znode */
1953063Sperrin 	if (flag & (FSYNC | FDSYNC))
1963063Sperrin 		atomic_inc_32(&zp->z_sync_cnt);
1975331Samw 
1987844SMark.Shellenbaum@Sun.COM 	ZFS_EXIT(zfsvfs);
199789Sahrens 	return (0);
200789Sahrens }
201789Sahrens 
202789Sahrens /* ARGSUSED */
203789Sahrens static int
2045331Samw zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
2055331Samw     caller_context_t *ct)
206789Sahrens {
2073063Sperrin 	znode_t	*zp = VTOZ(vp);
2087844SMark.Shellenbaum@Sun.COM 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2097844SMark.Shellenbaum@Sun.COM 
2107844SMark.Shellenbaum@Sun.COM 	ZFS_ENTER(zfsvfs);
2117844SMark.Shellenbaum@Sun.COM 	ZFS_VERIFY_ZP(zp);
2123063Sperrin 
2133063Sperrin 	/* Decrement the synchronous opens in the znode */
2144339Sperrin 	if ((flag & (FSYNC | FDSYNC)) && (count == 1))
2153063Sperrin 		atomic_dec_32(&zp->z_sync_cnt);
2163063Sperrin 
217789Sahrens 	/*
218789Sahrens 	 * Clean up any locks held by this process on the vp.
219789Sahrens 	 */
220789Sahrens 	cleanlocks(vp, ddi_get_pid(), 0);
221789Sahrens 	cleanshares(vp, ddi_get_pid());
222789Sahrens 
2235331Samw 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
2245331Samw 	    ZTOV(zp)->v_type == VREG &&
2255331Samw 	    !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
2265331Samw 	    zp->z_phys->zp_size > 0)
2275331Samw 		VERIFY(fs_vscan(vp, cr, 1) == 0);
2285331Samw 
2297844SMark.Shellenbaum@Sun.COM 	ZFS_EXIT(zfsvfs);
230789Sahrens 	return (0);
231789Sahrens }
232789Sahrens 
233789Sahrens /*
234789Sahrens  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
235789Sahrens  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
236789Sahrens  */
237789Sahrens static int
238789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off)
239789Sahrens {
240789Sahrens 	znode_t	*zp = VTOZ(vp);
241789Sahrens 	uint64_t noff = (uint64_t)*off; /* new offset */
242789Sahrens 	uint64_t file_sz;
243789Sahrens 	int error;
244789Sahrens 	boolean_t hole;
245789Sahrens 
246789Sahrens 	file_sz = zp->z_phys->zp_size;
247789Sahrens 	if (noff >= file_sz)  {
248789Sahrens 		return (ENXIO);
249789Sahrens 	}
250789Sahrens 
251789Sahrens 	if (cmd == _FIO_SEEK_HOLE)
252789Sahrens 		hole = B_TRUE;
253789Sahrens 	else
254789Sahrens 		hole = B_FALSE;
255789Sahrens 
256789Sahrens 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
257789Sahrens 
258789Sahrens 	/* end of file? */
259789Sahrens 	if ((error == ESRCH) || (noff > file_sz)) {
260789Sahrens 		/*
261789Sahrens 		 * Handle the virtual hole at the end of file.
262789Sahrens 		 */
263789Sahrens 		if (hole) {
264789Sahrens 			*off = file_sz;
265789Sahrens 			return (0);
266789Sahrens 		}
267789Sahrens 		return (ENXIO);
268789Sahrens 	}
269789Sahrens 
270789Sahrens 	if (noff < *off)
271789Sahrens 		return (error);
272789Sahrens 	*off = noff;
273789Sahrens 	return (error);
274789Sahrens }
275789Sahrens 
276789Sahrens /* ARGSUSED */
277789Sahrens static int
278789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
2795331Samw     int *rvalp, caller_context_t *ct)
280789Sahrens {
281789Sahrens 	offset_t off;
282789Sahrens 	int error;
283789Sahrens 	zfsvfs_t *zfsvfs;
2845326Sek110237 	znode_t *zp;
285789Sahrens 
286789Sahrens 	switch (com) {
2874339Sperrin 	case _FIOFFS:
288789Sahrens 		return (zfs_sync(vp->v_vfsp, 0, cred));
289789Sahrens 
2901544Seschrock 		/*
2911544Seschrock 		 * The following two ioctls are used by bfu.  Faking out,
2921544Seschrock 		 * necessary to avoid bfu errors.
2931544Seschrock 		 */
2944339Sperrin 	case _FIOGDIO:
2954339Sperrin 	case _FIOSDIO:
2961544Seschrock 		return (0);
2971544Seschrock 
2984339Sperrin 	case _FIO_SEEK_DATA:
2994339Sperrin 	case _FIO_SEEK_HOLE:
300789Sahrens 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
301789Sahrens 			return (EFAULT);
302789Sahrens 
3035326Sek110237 		zp = VTOZ(vp);
3045326Sek110237 		zfsvfs = zp->z_zfsvfs;
3055367Sahrens 		ZFS_ENTER(zfsvfs);
3065367Sahrens 		ZFS_VERIFY_ZP(zp);
307789Sahrens 
308789Sahrens 		/* offset parameter is in/out */
309789Sahrens 		error = zfs_holey(vp, com, &off);
310789Sahrens 		ZFS_EXIT(zfsvfs);
311789Sahrens 		if (error)
312789Sahrens 			return (error);
313789Sahrens 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
314789Sahrens 			return (EFAULT);
315789Sahrens 		return (0);
316789Sahrens 	}
317789Sahrens 	return (ENOTTY);
318789Sahrens }
319789Sahrens 
320789Sahrens /*
3217315SJonathan.Adams@Sun.COM  * Utility functions to map and unmap a single physical page.  These
3227315SJonathan.Adams@Sun.COM  * are used to manage the mappable copies of ZFS file data, and therefore
3237315SJonathan.Adams@Sun.COM  * do not update ref/mod bits.
3247315SJonathan.Adams@Sun.COM  */
3257315SJonathan.Adams@Sun.COM caddr_t
3267315SJonathan.Adams@Sun.COM zfs_map_page(page_t *pp, enum seg_rw rw)
3277315SJonathan.Adams@Sun.COM {
3287315SJonathan.Adams@Sun.COM 	if (kpm_enable)
3297315SJonathan.Adams@Sun.COM 		return (hat_kpm_mapin(pp, 0));
3307315SJonathan.Adams@Sun.COM 	ASSERT(rw == S_READ || rw == S_WRITE);
3317315SJonathan.Adams@Sun.COM 	return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0),
3327315SJonathan.Adams@Sun.COM 	    (caddr_t)-1));
3337315SJonathan.Adams@Sun.COM }
3347315SJonathan.Adams@Sun.COM 
3357315SJonathan.Adams@Sun.COM void
3367315SJonathan.Adams@Sun.COM zfs_unmap_page(page_t *pp, caddr_t addr)
3377315SJonathan.Adams@Sun.COM {
3387315SJonathan.Adams@Sun.COM 	if (kpm_enable) {
3397315SJonathan.Adams@Sun.COM 		hat_kpm_mapout(pp, 0, addr);
3407315SJonathan.Adams@Sun.COM 	} else {
3417315SJonathan.Adams@Sun.COM 		ppmapout(addr);
3427315SJonathan.Adams@Sun.COM 	}
3437315SJonathan.Adams@Sun.COM }
3447315SJonathan.Adams@Sun.COM 
3457315SJonathan.Adams@Sun.COM /*
346789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
347789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
348789Sahrens  *
349789Sahrens  * On Write:	If we find a memory mapped page, we write to *both*
350789Sahrens  *		the page and the dmu buffer.
351789Sahrens  *
352789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
353789Sahrens  *	the file is memory mapped.
354789Sahrens  */
355789Sahrens static int
3563638Sbillm mappedwrite(vnode_t *vp, int nbytes, uio_t *uio, dmu_tx_t *tx)
357789Sahrens {
358789Sahrens 	znode_t	*zp = VTOZ(vp);
359789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
360789Sahrens 	int64_t	start, off;
361789Sahrens 	int len = nbytes;
362789Sahrens 	int error = 0;
363789Sahrens 
364789Sahrens 	start = uio->uio_loffset;
365789Sahrens 	off = start & PAGEOFFSET;
366789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
367789Sahrens 		page_t *pp;
368789Sahrens 		uint64_t bytes = MIN(PAGESIZE - off, len);
3693638Sbillm 		uint64_t woff = uio->uio_loffset;
370789Sahrens 
371789Sahrens 		/*
372789Sahrens 		 * We don't want a new page to "appear" in the middle of
373789Sahrens 		 * the file update (because it may not get the write
374789Sahrens 		 * update data), so we grab a lock to block
375789Sahrens 		 * zfs_getpage().
376789Sahrens 		 */
377789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
378789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
379789Sahrens 			caddr_t va;
380789Sahrens 
381789Sahrens 			rw_exit(&zp->z_map_lock);
3827315SJonathan.Adams@Sun.COM 			va = zfs_map_page(pp, S_WRITE);
383789Sahrens 			error = uiomove(va+off, bytes, UIO_WRITE, uio);
384789Sahrens 			if (error == 0) {
385789Sahrens 				dmu_write(zfsvfs->z_os, zp->z_id,
386789Sahrens 				    woff, bytes, va+off, tx);
387789Sahrens 			}
3887315SJonathan.Adams@Sun.COM 			zfs_unmap_page(pp, va);
389789Sahrens 			page_unlock(pp);
390789Sahrens 		} else {
391789Sahrens 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
3923638Sbillm 			    uio, bytes, tx);
393789Sahrens 			rw_exit(&zp->z_map_lock);
394789Sahrens 		}
395789Sahrens 		len -= bytes;
396789Sahrens 		off = 0;
397789Sahrens 		if (error)
398789Sahrens 			break;
399789Sahrens 	}
400789Sahrens 	return (error);
401789Sahrens }
402789Sahrens 
403789Sahrens /*
404789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
405789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
406789Sahrens  *
407789Sahrens  * On Read:	We "read" preferentially from memory mapped pages,
408789Sahrens  *		else we default from the dmu buffer.
409789Sahrens  *
410789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
411789Sahrens  *	the file is memory mapped.
412789Sahrens  */
413789Sahrens static int
4143638Sbillm mappedread(vnode_t *vp, int nbytes, uio_t *uio)
415789Sahrens {
4163638Sbillm 	znode_t *zp = VTOZ(vp);
4173638Sbillm 	objset_t *os = zp->z_zfsvfs->z_os;
4183638Sbillm 	int64_t	start, off;
419789Sahrens 	int len = nbytes;
420789Sahrens 	int error = 0;
421789Sahrens 
422789Sahrens 	start = uio->uio_loffset;
423789Sahrens 	off = start & PAGEOFFSET;
424789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
425789Sahrens 		page_t *pp;
4263638Sbillm 		uint64_t bytes = MIN(PAGESIZE - off, len);
4273638Sbillm 
428789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
429789Sahrens 			caddr_t va;
430789Sahrens 
4317315SJonathan.Adams@Sun.COM 			va = zfs_map_page(pp, S_READ);
432789Sahrens 			error = uiomove(va + off, bytes, UIO_READ, uio);
4337315SJonathan.Adams@Sun.COM 			zfs_unmap_page(pp, va);
434789Sahrens 			page_unlock(pp);
435789Sahrens 		} else {
4363638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, bytes);
437789Sahrens 		}
438789Sahrens 		len -= bytes;
439789Sahrens 		off = 0;
440789Sahrens 		if (error)
441789Sahrens 			break;
442789Sahrens 	}
443789Sahrens 	return (error);
444789Sahrens }
445789Sahrens 
4463638Sbillm offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
447789Sahrens 
448789Sahrens /*
449789Sahrens  * Read bytes from specified file into supplied buffer.
450789Sahrens  *
451789Sahrens  *	IN:	vp	- vnode of file to be read from.
452789Sahrens  *		uio	- structure supplying read location, range info,
453789Sahrens  *			  and return buffer.
454789Sahrens  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
455789Sahrens  *		cr	- credentials of caller.
4565331Samw  *		ct	- caller context
457789Sahrens  *
458789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
459789Sahrens  *
460789Sahrens  *	RETURN:	0 if success
461789Sahrens  *		error code if failure
462789Sahrens  *
463789Sahrens  * Side Effects:
464789Sahrens  *	vp - atime updated if byte count > 0
465789Sahrens  */
466789Sahrens /* ARGSUSED */
467789Sahrens static int
468789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
469789Sahrens {
470789Sahrens 	znode_t		*zp = VTOZ(vp);
471789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4725326Sek110237 	objset_t	*os;
4733638Sbillm 	ssize_t		n, nbytes;
4743638Sbillm 	int		error;
4751669Sperrin 	rl_t		*rl;
476789Sahrens 
4775367Sahrens 	ZFS_ENTER(zfsvfs);
4785367Sahrens 	ZFS_VERIFY_ZP(zp);
4795326Sek110237 	os = zfsvfs->z_os;
480789Sahrens 
4815929Smarks 	if (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) {
4825929Smarks 		ZFS_EXIT(zfsvfs);
4835929Smarks 		return (EACCES);
4845929Smarks 	}
4855929Smarks 
486789Sahrens 	/*
487789Sahrens 	 * Validate file offset
488789Sahrens 	 */
489789Sahrens 	if (uio->uio_loffset < (offset_t)0) {
490789Sahrens 		ZFS_EXIT(zfsvfs);
491789Sahrens 		return (EINVAL);
492789Sahrens 	}
493789Sahrens 
494789Sahrens 	/*
495789Sahrens 	 * Fasttrack empty reads
496789Sahrens 	 */
497789Sahrens 	if (uio->uio_resid == 0) {
498789Sahrens 		ZFS_EXIT(zfsvfs);
499789Sahrens 		return (0);
500789Sahrens 	}
501789Sahrens 
502789Sahrens 	/*
5031669Sperrin 	 * Check for mandatory locks
504789Sahrens 	 */
505789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
506789Sahrens 		if (error = chklock(vp, FREAD,
507789Sahrens 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
508789Sahrens 			ZFS_EXIT(zfsvfs);
509789Sahrens 			return (error);
510789Sahrens 		}
511789Sahrens 	}
512789Sahrens 
513789Sahrens 	/*
514789Sahrens 	 * If we're in FRSYNC mode, sync out this znode before reading it.
515789Sahrens 	 */
5162638Sperrin 	if (ioflag & FRSYNC)
5172638Sperrin 		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
518789Sahrens 
519789Sahrens 	/*
5201669Sperrin 	 * Lock the range against changes.
521789Sahrens 	 */
5221669Sperrin 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
5231669Sperrin 
524789Sahrens 	/*
525789Sahrens 	 * If we are reading past end-of-file we can skip
526789Sahrens 	 * to the end; but we might still need to set atime.
527789Sahrens 	 */
528789Sahrens 	if (uio->uio_loffset >= zp->z_phys->zp_size) {
529789Sahrens 		error = 0;
530789Sahrens 		goto out;
531789Sahrens 	}
532789Sahrens 
5333638Sbillm 	ASSERT(uio->uio_loffset < zp->z_phys->zp_size);
5343638Sbillm 	n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset);
5353638Sbillm 
5363638Sbillm 	while (n > 0) {
5373638Sbillm 		nbytes = MIN(n, zfs_read_chunk_size -
5383638Sbillm 		    P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
5393638Sbillm 
5403638Sbillm 		if (vn_has_cached_data(vp))
5413638Sbillm 			error = mappedread(vp, nbytes, uio);
5423638Sbillm 		else
5433638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, nbytes);
5447294Sperrin 		if (error) {
5457294Sperrin 			/* convert checksum errors into IO errors */
5467294Sperrin 			if (error == ECKSUM)
5477294Sperrin 				error = EIO;
5483638Sbillm 			break;
5497294Sperrin 		}
5503638Sbillm 
5513638Sbillm 		n -= nbytes;
552789Sahrens 	}
5533638Sbillm 
554789Sahrens out:
5552237Smaybee 	zfs_range_unlock(rl);
556789Sahrens 
557789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
558789Sahrens 	ZFS_EXIT(zfsvfs);
559789Sahrens 	return (error);
560789Sahrens }
561789Sahrens 
562789Sahrens /*
563789Sahrens  * Write the bytes to a file.
564789Sahrens  *
565789Sahrens  *	IN:	vp	- vnode of file to be written to.
566789Sahrens  *		uio	- structure supplying write location, range info,
567789Sahrens  *			  and data buffer.
568789Sahrens  *		ioflag	- FAPPEND flag set if in append mode.
569789Sahrens  *		cr	- credentials of caller.
5705331Samw  *		ct	- caller context (NFS/CIFS fem monitor only)
571789Sahrens  *
572789Sahrens  *	OUT:	uio	- updated offset and range.
573789Sahrens  *
574789Sahrens  *	RETURN:	0 if success
575789Sahrens  *		error code if failure
576789Sahrens  *
577789Sahrens  * Timestamps:
578789Sahrens  *	vp - ctime|mtime updated if byte count > 0
579789Sahrens  */
580789Sahrens /* ARGSUSED */
581789Sahrens static int
582789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
583789Sahrens {
584789Sahrens 	znode_t		*zp = VTOZ(vp);
585789Sahrens 	rlim64_t	limit = uio->uio_llimit;
586789Sahrens 	ssize_t		start_resid = uio->uio_resid;
587789Sahrens 	ssize_t		tx_bytes;
588789Sahrens 	uint64_t	end_size;
589789Sahrens 	dmu_tx_t	*tx;
590789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
5915326Sek110237 	zilog_t		*zilog;
592789Sahrens 	offset_t	woff;
593789Sahrens 	ssize_t		n, nbytes;
5941669Sperrin 	rl_t		*rl;
595789Sahrens 	int		max_blksz = zfsvfs->z_max_blksz;
5966743Smarks 	uint64_t	pflags;
5971669Sperrin 	int		error;
598789Sahrens 
599789Sahrens 	/*
600789Sahrens 	 * Fasttrack empty write
601789Sahrens 	 */
6021669Sperrin 	n = start_resid;
603789Sahrens 	if (n == 0)
604789Sahrens 		return (0);
605789Sahrens 
6061669Sperrin 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
6071669Sperrin 		limit = MAXOFFSET_T;
6081669Sperrin 
6095367Sahrens 	ZFS_ENTER(zfsvfs);
6105367Sahrens 	ZFS_VERIFY_ZP(zp);
6116743Smarks 
6126743Smarks 	/*
6136743Smarks 	 * If immutable or not appending then return EPERM
6146743Smarks 	 */
6156743Smarks 	pflags = zp->z_phys->zp_flags;
6166743Smarks 	if ((pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
6176743Smarks 	    ((pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
6186743Smarks 	    (uio->uio_loffset < zp->z_phys->zp_size))) {
6196743Smarks 		ZFS_EXIT(zfsvfs);
6206743Smarks 		return (EPERM);
6216743Smarks 	}
6226743Smarks 
6235326Sek110237 	zilog = zfsvfs->z_log;
624789Sahrens 
625789Sahrens 	/*
6262237Smaybee 	 * Pre-fault the pages to ensure slow (eg NFS) pages
6271669Sperrin 	 * don't hold up txg.
628789Sahrens 	 */
6298059SDonghai.Qiao@Sun.COM 	uio_prefaultpages(n, uio);
630789Sahrens 
631789Sahrens 	/*
632789Sahrens 	 * If in append mode, set the io offset pointer to eof.
633789Sahrens 	 */
6341669Sperrin 	if (ioflag & FAPPEND) {
6351669Sperrin 		/*
6361669Sperrin 		 * Range lock for a file append:
6371669Sperrin 		 * The value for the start of range will be determined by
6381669Sperrin 		 * zfs_range_lock() (to guarantee append semantics).
6391669Sperrin 		 * If this write will cause the block size to increase,
6401669Sperrin 		 * zfs_range_lock() will lock the entire file, so we must
6411669Sperrin 		 * later reduce the range after we grow the block size.
6421669Sperrin 		 */
6431669Sperrin 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
6441669Sperrin 		if (rl->r_len == UINT64_MAX) {
6451669Sperrin 			/* overlocked, zp_size can't change */
6461669Sperrin 			woff = uio->uio_loffset = zp->z_phys->zp_size;
6471669Sperrin 		} else {
6481669Sperrin 			woff = uio->uio_loffset = rl->r_off;
6491669Sperrin 		}
650789Sahrens 	} else {
651789Sahrens 		woff = uio->uio_loffset;
652789Sahrens 		/*
653789Sahrens 		 * Validate file offset
654789Sahrens 		 */
655789Sahrens 		if (woff < 0) {
656789Sahrens 			ZFS_EXIT(zfsvfs);
657789Sahrens 			return (EINVAL);
658789Sahrens 		}
659789Sahrens 
660789Sahrens 		/*
6611669Sperrin 		 * If we need to grow the block size then zfs_range_lock()
6621669Sperrin 		 * will lock a wider range than we request here.
6631669Sperrin 		 * Later after growing the block size we reduce the range.
664789Sahrens 		 */
6651669Sperrin 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
666789Sahrens 	}
667789Sahrens 
668789Sahrens 	if (woff >= limit) {
6693638Sbillm 		zfs_range_unlock(rl);
6703638Sbillm 		ZFS_EXIT(zfsvfs);
6713638Sbillm 		return (EFBIG);
672789Sahrens 	}
673789Sahrens 
674789Sahrens 	if ((woff + n) > limit || woff > (limit - n))
675789Sahrens 		n = limit - woff;
676789Sahrens 
677789Sahrens 	/*
6781669Sperrin 	 * Check for mandatory locks
679789Sahrens 	 */
680789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) &&
6813638Sbillm 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
6823638Sbillm 		zfs_range_unlock(rl);
6833638Sbillm 		ZFS_EXIT(zfsvfs);
6843638Sbillm 		return (error);
6853638Sbillm 	}
6861669Sperrin 	end_size = MAX(zp->z_phys->zp_size, woff + n);
687789Sahrens 
6881669Sperrin 	/*
6893638Sbillm 	 * Write the file in reasonable size chunks.  Each chunk is written
6903638Sbillm 	 * in a separate transaction; this keeps the intent log records small
6913638Sbillm 	 * and allows us to do more fine-grained space accounting.
692789Sahrens 	 */
693789Sahrens 	while (n > 0) {
694789Sahrens 		/*
6953638Sbillm 		 * Start a transaction.
696789Sahrens 		 */
697789Sahrens 		woff = uio->uio_loffset;
698789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
699789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
700789Sahrens 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
701789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
702789Sahrens 		if (error) {
703789Sahrens 			if (error == ERESTART &&
704789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
7052113Sahrens 				dmu_tx_wait(tx);
7062113Sahrens 				dmu_tx_abort(tx);
7073638Sbillm 				continue;
708789Sahrens 			}
7092113Sahrens 			dmu_tx_abort(tx);
7103638Sbillm 			break;
7113638Sbillm 		}
7123638Sbillm 
7133638Sbillm 		/*
7143638Sbillm 		 * If zfs_range_lock() over-locked we grow the blocksize
7153638Sbillm 		 * and then reduce the lock range.  This will only happen
7163638Sbillm 		 * on the first iteration since zfs_range_reduce() will
7173638Sbillm 		 * shrink down r_len to the appropriate size.
7183638Sbillm 		 */
7193638Sbillm 		if (rl->r_len == UINT64_MAX) {
7203638Sbillm 			uint64_t new_blksz;
7213638Sbillm 
7223638Sbillm 			if (zp->z_blksz > max_blksz) {
7233638Sbillm 				ASSERT(!ISP2(zp->z_blksz));
7243638Sbillm 				new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
7253638Sbillm 			} else {
7263638Sbillm 				new_blksz = MIN(end_size, max_blksz);
7273638Sbillm 			}
7283638Sbillm 			zfs_grow_blocksize(zp, new_blksz, tx);
7293638Sbillm 			zfs_range_reduce(rl, woff, n);
7303638Sbillm 		}
7313638Sbillm 
7323638Sbillm 		/*
7333638Sbillm 		 * XXX - should we really limit each write to z_max_blksz?
7343638Sbillm 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
7353638Sbillm 		 */
7363638Sbillm 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
7373638Sbillm 		rw_enter(&zp->z_map_lock, RW_READER);
7383638Sbillm 
7393638Sbillm 		tx_bytes = uio->uio_resid;
7403638Sbillm 		if (vn_has_cached_data(vp)) {
7413638Sbillm 			rw_exit(&zp->z_map_lock);
7423638Sbillm 			error = mappedwrite(vp, nbytes, uio, tx);
7433638Sbillm 		} else {
7443638Sbillm 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
7453638Sbillm 			    uio, nbytes, tx);
7463638Sbillm 			rw_exit(&zp->z_map_lock);
747789Sahrens 		}
7483638Sbillm 		tx_bytes -= uio->uio_resid;
7493638Sbillm 
7503638Sbillm 		/*
7513638Sbillm 		 * If we made no progress, we're done.  If we made even
7523638Sbillm 		 * partial progress, update the znode and ZIL accordingly.
7533638Sbillm 		 */
7543638Sbillm 		if (tx_bytes == 0) {
7553897Smaybee 			dmu_tx_commit(tx);
7563638Sbillm 			ASSERT(error != 0);
7573638Sbillm 			break;
7583638Sbillm 		}
7593638Sbillm 
760789Sahrens 		/*
7613638Sbillm 		 * Clear Set-UID/Set-GID bits on successful write if not
7623638Sbillm 		 * privileged and at least one of the excute bits is set.
7633638Sbillm 		 *
7643638Sbillm 		 * It would be nice to to this after all writes have
7653638Sbillm 		 * been done, but that would still expose the ISUID/ISGID
7663638Sbillm 		 * to another app after the partial write is committed.
7675331Samw 		 *
7685331Samw 		 * Note: we don't call zfs_fuid_map_id() here because
7695331Samw 		 * user 0 is not an ephemeral uid.
770789Sahrens 		 */
7713638Sbillm 		mutex_enter(&zp->z_acl_lock);
7723638Sbillm 		if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) |
7733638Sbillm 		    (S_IXUSR >> 6))) != 0 &&
7743638Sbillm 		    (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 &&
7753638Sbillm 		    secpolicy_vnode_setid_retain(cr,
7763638Sbillm 		    (zp->z_phys->zp_mode & S_ISUID) != 0 &&
7773638Sbillm 		    zp->z_phys->zp_uid == 0) != 0) {
7784339Sperrin 			zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID);
7793638Sbillm 		}
7803638Sbillm 		mutex_exit(&zp->z_acl_lock);
7813638Sbillm 
7823638Sbillm 		/*
7833638Sbillm 		 * Update time stamp.  NOTE: This marks the bonus buffer as
7843638Sbillm 		 * dirty, so we don't have to do it again for zp_size.
7853638Sbillm 		 */
7863638Sbillm 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
7873638Sbillm 
7883638Sbillm 		/*
7893638Sbillm 		 * Update the file size (zp_size) if it has changed;
7903638Sbillm 		 * account for possible concurrent updates.
7913638Sbillm 		 */
7923638Sbillm 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset)
793789Sahrens 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
794789Sahrens 			    uio->uio_loffset);
7953638Sbillm 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
7963638Sbillm 		dmu_tx_commit(tx);
7973638Sbillm 
7983638Sbillm 		if (error != 0)
7993638Sbillm 			break;
8003638Sbillm 		ASSERT(tx_bytes == nbytes);
8013638Sbillm 		n -= nbytes;
802789Sahrens 	}
803789Sahrens 
8042237Smaybee 	zfs_range_unlock(rl);
805789Sahrens 
806789Sahrens 	/*
807789Sahrens 	 * If we're in replay mode, or we made no progress, return error.
808789Sahrens 	 * Otherwise, it's at least a partial write, so it's successful.
809789Sahrens 	 */
810789Sahrens 	if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) {
811789Sahrens 		ZFS_EXIT(zfsvfs);
812789Sahrens 		return (error);
813789Sahrens 	}
814789Sahrens 
8152638Sperrin 	if (ioflag & (FSYNC | FDSYNC))
8162638Sperrin 		zil_commit(zilog, zp->z_last_itx, zp->z_id);
817789Sahrens 
818789Sahrens 	ZFS_EXIT(zfsvfs);
819789Sahrens 	return (0);
820789Sahrens }
821789Sahrens 
8222237Smaybee void
8233063Sperrin zfs_get_done(dmu_buf_t *db, void *vzgd)
8242237Smaybee {
8253063Sperrin 	zgd_t *zgd = (zgd_t *)vzgd;
8263063Sperrin 	rl_t *rl = zgd->zgd_rl;
8272237Smaybee 	vnode_t *vp = ZTOV(rl->r_zp);
8282237Smaybee 
8293063Sperrin 	dmu_buf_rele(db, vzgd);
8302237Smaybee 	zfs_range_unlock(rl);
8312237Smaybee 	VN_RELE(vp);
8325688Sbonwick 	zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
8333063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
8342237Smaybee }
8352237Smaybee 
836789Sahrens /*
837789Sahrens  * Get data to generate a TX_WRITE intent log record.
838789Sahrens  */
839789Sahrens int
8402237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
841789Sahrens {
842789Sahrens 	zfsvfs_t *zfsvfs = arg;
843789Sahrens 	objset_t *os = zfsvfs->z_os;
844789Sahrens 	znode_t *zp;
845789Sahrens 	uint64_t off = lr->lr_offset;
8462237Smaybee 	dmu_buf_t *db;
8471669Sperrin 	rl_t *rl;
8483063Sperrin 	zgd_t *zgd;
8493638Sbillm 	int dlen = lr->lr_length;		/* length of user data */
850789Sahrens 	int error = 0;
851789Sahrens 
8523063Sperrin 	ASSERT(zio);
853789Sahrens 	ASSERT(dlen != 0);
854789Sahrens 
855789Sahrens 	/*
8561669Sperrin 	 * Nothing to do if the file has been removed
857789Sahrens 	 */
858789Sahrens 	if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0)
859789Sahrens 		return (ENOENT);
8603461Sahrens 	if (zp->z_unlinked) {
861789Sahrens 		VN_RELE(ZTOV(zp));
862789Sahrens 		return (ENOENT);
863789Sahrens 	}
864789Sahrens 
865789Sahrens 	/*
866789Sahrens 	 * Write records come in two flavors: immediate and indirect.
867789Sahrens 	 * For small writes it's cheaper to store the data with the
868789Sahrens 	 * log record (immediate); for large writes it's cheaper to
869789Sahrens 	 * sync the data and get a pointer to it (indirect) so that
870789Sahrens 	 * we don't have to write the data twice.
871789Sahrens 	 */
8721669Sperrin 	if (buf != NULL) { /* immediate write */
8731669Sperrin 		rl = zfs_range_lock(zp, off, dlen, RL_READER);
8741669Sperrin 		/* test for truncation needs to be done while range locked */
8751669Sperrin 		if (off >= zp->z_phys->zp_size) {
8761669Sperrin 			error = ENOENT;
8771669Sperrin 			goto out;
8781669Sperrin 		}
8792449Smaybee 		VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf));
8801669Sperrin 	} else { /* indirect write */
8811669Sperrin 		uint64_t boff; /* block starting offset */
8821669Sperrin 
883789Sahrens 		/*
8841669Sperrin 		 * Have to lock the whole block to ensure when it's
8851669Sperrin 		 * written out and it's checksum is being calculated
8861669Sperrin 		 * that no one can change the data. We need to re-check
8871669Sperrin 		 * blocksize after we get the lock in case it's changed!
888789Sahrens 		 */
8891669Sperrin 		for (;;) {
8901941Sperrin 			if (ISP2(zp->z_blksz)) {
8911941Sperrin 				boff = P2ALIGN_TYPED(off, zp->z_blksz,
8921941Sperrin 				    uint64_t);
8931941Sperrin 			} else {
8941941Sperrin 				boff = 0;
8951941Sperrin 			}
8961669Sperrin 			dlen = zp->z_blksz;
8971669Sperrin 			rl = zfs_range_lock(zp, boff, dlen, RL_READER);
8981669Sperrin 			if (zp->z_blksz == dlen)
8991669Sperrin 				break;
9002237Smaybee 			zfs_range_unlock(rl);
9011669Sperrin 		}
9021669Sperrin 		/* test for truncation needs to be done while range locked */
9031669Sperrin 		if (off >= zp->z_phys->zp_size) {
9041669Sperrin 			error = ENOENT;
9051669Sperrin 			goto out;
9061669Sperrin 		}
9073063Sperrin 		zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
9083063Sperrin 		zgd->zgd_rl = rl;
9093063Sperrin 		zgd->zgd_zilog = zfsvfs->z_log;
9103063Sperrin 		zgd->zgd_bp = &lr->lr_blkptr;
9113063Sperrin 		VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db));
9122237Smaybee 		ASSERT(boff == db->db_offset);
9132237Smaybee 		lr->lr_blkoff = off - boff;
9142237Smaybee 		error = dmu_sync(zio, db, &lr->lr_blkptr,
9153063Sperrin 		    lr->lr_common.lrc_txg, zfs_get_done, zgd);
9164709Smaybee 		ASSERT((error && error != EINPROGRESS) ||
9174709Smaybee 		    lr->lr_length <= zp->z_blksz);
9185688Sbonwick 		if (error == 0)
9195688Sbonwick 			zil_add_block(zfsvfs->z_log, &lr->lr_blkptr);
9202237Smaybee 		/*
9212237Smaybee 		 * If we get EINPROGRESS, then we need to wait for a
9222237Smaybee 		 * write IO initiated by dmu_sync() to complete before
9232638Sperrin 		 * we can release this dbuf.  We will finish everything
9242237Smaybee 		 * up in the zfs_get_done() callback.
9252237Smaybee 		 */
9262237Smaybee 		if (error == EINPROGRESS)
9272237Smaybee 			return (0);
9283063Sperrin 		dmu_buf_rele(db, zgd);
9293063Sperrin 		kmem_free(zgd, sizeof (zgd_t));
930789Sahrens 	}
9311669Sperrin out:
9322237Smaybee 	zfs_range_unlock(rl);
933789Sahrens 	VN_RELE(ZTOV(zp));
934789Sahrens 	return (error);
935789Sahrens }
936789Sahrens 
937789Sahrens /*ARGSUSED*/
938789Sahrens static int
9395331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
9405331Samw     caller_context_t *ct)
941789Sahrens {
942789Sahrens 	znode_t *zp = VTOZ(vp);
943789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
944789Sahrens 	int error;
945789Sahrens 
9465367Sahrens 	ZFS_ENTER(zfsvfs);
9475367Sahrens 	ZFS_VERIFY_ZP(zp);
9485331Samw 
9495331Samw 	if (flag & V_ACE_MASK)
9505331Samw 		error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
9515331Samw 	else
9525331Samw 		error = zfs_zaccess_rwx(zp, mode, flag, cr);
9535331Samw 
954789Sahrens 	ZFS_EXIT(zfsvfs);
955789Sahrens 	return (error);
956789Sahrens }
957789Sahrens 
958789Sahrens /*
959789Sahrens  * Lookup an entry in a directory, or an extended attribute directory.
960789Sahrens  * If it exists, return a held vnode reference for it.
961789Sahrens  *
962789Sahrens  *	IN:	dvp	- vnode of directory to search.
963789Sahrens  *		nm	- name of entry to lookup.
964789Sahrens  *		pnp	- full pathname to lookup [UNUSED].
965789Sahrens  *		flags	- LOOKUP_XATTR set if looking for an attribute.
966789Sahrens  *		rdir	- root directory vnode [UNUSED].
967789Sahrens  *		cr	- credentials of caller.
9685331Samw  *		ct	- caller context
9695331Samw  *		direntflags - directory lookup flags
9705331Samw  *		realpnp - returned pathname.
971789Sahrens  *
972789Sahrens  *	OUT:	vpp	- vnode of located entry, NULL if not found.
973789Sahrens  *
974789Sahrens  *	RETURN:	0 if success
975789Sahrens  *		error code if failure
976789Sahrens  *
977789Sahrens  * Timestamps:
978789Sahrens  *	NA
979789Sahrens  */
980789Sahrens /* ARGSUSED */
981789Sahrens static int
982789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
9835331Samw     int flags, vnode_t *rdir, cred_t *cr,  caller_context_t *ct,
9845331Samw     int *direntflags, pathname_t *realpnp)
985789Sahrens {
986789Sahrens 	znode_t *zdp = VTOZ(dvp);
987789Sahrens 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
988789Sahrens 	int	error;
989789Sahrens 
9905367Sahrens 	ZFS_ENTER(zfsvfs);
9915367Sahrens 	ZFS_VERIFY_ZP(zdp);
992789Sahrens 
993789Sahrens 	*vpp = NULL;
994789Sahrens 
995789Sahrens 	if (flags & LOOKUP_XATTR) {
996789Sahrens 		/*
9973234Sck153898 		 * If the xattr property is off, refuse the lookup request.
9983234Sck153898 		 */
9993234Sck153898 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
10003234Sck153898 			ZFS_EXIT(zfsvfs);
10013234Sck153898 			return (EINVAL);
10023234Sck153898 		}
10033234Sck153898 
10043234Sck153898 		/*
1005789Sahrens 		 * We don't allow recursive attributes..
1006789Sahrens 		 * Maybe someday we will.
1007789Sahrens 		 */
1008789Sahrens 		if (zdp->z_phys->zp_flags & ZFS_XATTR) {
1009789Sahrens 			ZFS_EXIT(zfsvfs);
1010789Sahrens 			return (EINVAL);
1011789Sahrens 		}
1012789Sahrens 
10133280Sck153898 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1014789Sahrens 			ZFS_EXIT(zfsvfs);
1015789Sahrens 			return (error);
1016789Sahrens 		}
1017789Sahrens 
1018789Sahrens 		/*
1019789Sahrens 		 * Do we have permission to get into attribute directory?
1020789Sahrens 		 */
1021789Sahrens 
10225331Samw 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
10235331Samw 		    B_FALSE, cr)) {
1024789Sahrens 			VN_RELE(*vpp);
10255331Samw 			*vpp = NULL;
1026789Sahrens 		}
1027789Sahrens 
1028789Sahrens 		ZFS_EXIT(zfsvfs);
1029789Sahrens 		return (error);
1030789Sahrens 	}
1031789Sahrens 
10321512Sek110237 	if (dvp->v_type != VDIR) {
10331512Sek110237 		ZFS_EXIT(zfsvfs);
10341460Smarks 		return (ENOTDIR);
10351512Sek110237 	}
10361460Smarks 
1037789Sahrens 	/*
1038789Sahrens 	 * Check accessibility of directory.
1039789Sahrens 	 */
1040789Sahrens 
10415331Samw 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1042789Sahrens 		ZFS_EXIT(zfsvfs);
1043789Sahrens 		return (error);
1044789Sahrens 	}
1045789Sahrens 
10465498Stimh 	if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
10475331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
10485331Samw 		ZFS_EXIT(zfsvfs);
10495331Samw 		return (EILSEQ);
10505331Samw 	}
10515331Samw 
10525331Samw 	error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
10535331Samw 	if (error == 0) {
1054789Sahrens 		/*
1055789Sahrens 		 * Convert device special files
1056789Sahrens 		 */
1057789Sahrens 		if (IS_DEVVP(*vpp)) {
1058789Sahrens 			vnode_t	*svp;
1059789Sahrens 
1060789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1061789Sahrens 			VN_RELE(*vpp);
1062789Sahrens 			if (svp == NULL)
1063789Sahrens 				error = ENOSYS;
1064789Sahrens 			else
1065789Sahrens 				*vpp = svp;
1066789Sahrens 		}
1067789Sahrens 	}
1068789Sahrens 
1069789Sahrens 	ZFS_EXIT(zfsvfs);
1070789Sahrens 	return (error);
1071789Sahrens }
1072789Sahrens 
1073789Sahrens /*
1074789Sahrens  * Attempt to create a new entry in a directory.  If the entry
1075789Sahrens  * already exists, truncate the file if permissible, else return
1076789Sahrens  * an error.  Return the vp of the created or trunc'd file.
1077789Sahrens  *
1078789Sahrens  *	IN:	dvp	- vnode of directory to put new file entry in.
1079789Sahrens  *		name	- name of new file entry.
1080789Sahrens  *		vap	- attributes of new file.
1081789Sahrens  *		excl	- flag indicating exclusive or non-exclusive mode.
1082789Sahrens  *		mode	- mode to open file with.
1083789Sahrens  *		cr	- credentials of caller.
1084789Sahrens  *		flag	- large file flag [UNUSED].
10855331Samw  *		ct	- caller context
10865331Samw  *		vsecp 	- ACL to be set
1087789Sahrens  *
1088789Sahrens  *	OUT:	vpp	- vnode of created or trunc'd entry.
1089789Sahrens  *
1090789Sahrens  *	RETURN:	0 if success
1091789Sahrens  *		error code if failure
1092789Sahrens  *
1093789Sahrens  * Timestamps:
1094789Sahrens  *	dvp - ctime|mtime updated if new entry created
1095789Sahrens  *	 vp - ctime|mtime always, atime if new
1096789Sahrens  */
10975331Samw 
1098789Sahrens /* ARGSUSED */
1099789Sahrens static int
1100789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
11015331Samw     int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
11025331Samw     vsecattr_t *vsecp)
1103789Sahrens {
1104789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1105789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
11065326Sek110237 	zilog_t		*zilog;
11075326Sek110237 	objset_t	*os;
1108789Sahrens 	zfs_dirlock_t	*dl;
1109789Sahrens 	dmu_tx_t	*tx;
1110789Sahrens 	int		error;
11115331Samw 	zfs_acl_t	*aclp = NULL;
11125331Samw 	zfs_fuid_info_t *fuidp = NULL;
11137847SMark.Shellenbaum@Sun.COM 	ksid_t		*ksid;
11147847SMark.Shellenbaum@Sun.COM 	uid_t		uid;
11157847SMark.Shellenbaum@Sun.COM 	gid_t		gid = crgetgid(cr);
11165331Samw 
11175331Samw 	/*
11185331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
11195331Samw 	 * make sure file system is at proper version
11205331Samw 	 */
11215331Samw 
11227847SMark.Shellenbaum@Sun.COM 	ksid = crgetsid(cr, KSID_OWNER);
11237847SMark.Shellenbaum@Sun.COM 	if (ksid)
11247847SMark.Shellenbaum@Sun.COM 		uid = ksid_getid(ksid);
11257847SMark.Shellenbaum@Sun.COM 	else
11267847SMark.Shellenbaum@Sun.COM 		uid = crgetuid(cr);
11277847SMark.Shellenbaum@Sun.COM 
11285331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
11295331Samw 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
11307847SMark.Shellenbaum@Sun.COM 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
11315331Samw 		return (EINVAL);
1132789Sahrens 
11335367Sahrens 	ZFS_ENTER(zfsvfs);
11345367Sahrens 	ZFS_VERIFY_ZP(dzp);
11355326Sek110237 	os = zfsvfs->z_os;
11365326Sek110237 	zilog = zfsvfs->z_log;
1137789Sahrens 
11385498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
11395331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
11405331Samw 		ZFS_EXIT(zfsvfs);
11415331Samw 		return (EILSEQ);
11425331Samw 	}
11435331Samw 
11445331Samw 	if (vap->va_mask & AT_XVATTR) {
11455331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
11465331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
11475331Samw 			ZFS_EXIT(zfsvfs);
11485331Samw 			return (error);
11495331Samw 		}
11505331Samw 	}
1151789Sahrens top:
1152789Sahrens 	*vpp = NULL;
1153789Sahrens 
1154789Sahrens 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1155789Sahrens 		vap->va_mode &= ~VSVTX;
1156789Sahrens 
1157789Sahrens 	if (*name == '\0') {
1158789Sahrens 		/*
1159789Sahrens 		 * Null component name refers to the directory itself.
1160789Sahrens 		 */
1161789Sahrens 		VN_HOLD(dvp);
1162789Sahrens 		zp = dzp;
1163789Sahrens 		dl = NULL;
1164789Sahrens 		error = 0;
1165789Sahrens 	} else {
1166789Sahrens 		/* possible VN_HOLD(zp) */
11675331Samw 		int zflg = 0;
11685331Samw 
11695331Samw 		if (flag & FIGNORECASE)
11705331Samw 			zflg |= ZCILOOK;
11715331Samw 
11725331Samw 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
11735331Samw 		    NULL, NULL);
11745331Samw 		if (error) {
1175789Sahrens 			if (strcmp(name, "..") == 0)
1176789Sahrens 				error = EISDIR;
1177789Sahrens 			ZFS_EXIT(zfsvfs);
11785331Samw 			if (aclp)
11795331Samw 				zfs_acl_free(aclp);
1180789Sahrens 			return (error);
1181789Sahrens 		}
1182789Sahrens 	}
11835331Samw 	if (vsecp && aclp == NULL) {
11845331Samw 		error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp);
11855331Samw 		if (error) {
11865331Samw 			ZFS_EXIT(zfsvfs);
11875331Samw 			if (dl)
11885331Samw 				zfs_dirent_unlock(dl);
11895331Samw 			return (error);
11905331Samw 		}
11915331Samw 	}
1192789Sahrens 
1193789Sahrens 	if (zp == NULL) {
11945331Samw 		uint64_t txtype;
11955331Samw 
1196789Sahrens 		/*
1197789Sahrens 		 * Create a new file object and update the directory
1198789Sahrens 		 * to reference it.
1199789Sahrens 		 */
12005331Samw 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1201789Sahrens 			goto out;
1202789Sahrens 		}
1203789Sahrens 
1204789Sahrens 		/*
1205789Sahrens 		 * We only support the creation of regular files in
1206789Sahrens 		 * extended attribute directories.
1207789Sahrens 		 */
1208789Sahrens 		if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1209789Sahrens 		    (vap->va_type != VREG)) {
1210789Sahrens 			error = EINVAL;
1211789Sahrens 			goto out;
1212789Sahrens 		}
1213789Sahrens 
1214789Sahrens 		tx = dmu_tx_create(os);
1215789Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
12167847SMark.Shellenbaum@Sun.COM 		if ((aclp && aclp->z_has_fuids) || IS_EPHEMERAL(uid) ||
12177847SMark.Shellenbaum@Sun.COM 		    IS_EPHEMERAL(gid)) {
12185824Smarks 			if (zfsvfs->z_fuid_obj == 0) {
12195824Smarks 				dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
12205824Smarks 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
12215824Smarks 				    FUID_SIZE_ESTIMATE(zfsvfs));
12225824Smarks 				dmu_tx_hold_zap(tx, MASTER_NODE_OBJ,
12235824Smarks 				    FALSE, NULL);
12245824Smarks 			} else {
12255824Smarks 				dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
12265824Smarks 				dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
12275824Smarks 				    FUID_SIZE_ESTIMATE(zfsvfs));
12285824Smarks 			}
12295331Samw 		}
1230789Sahrens 		dmu_tx_hold_bonus(tx, dzp->z_id);
12311544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
12325331Samw 		if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp) {
1233789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1234789Sahrens 			    0, SPA_MAXBLOCKSIZE);
12355331Samw 		}
1236789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
1237789Sahrens 		if (error) {
1238789Sahrens 			zfs_dirent_unlock(dl);
1239789Sahrens 			if (error == ERESTART &&
1240789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
12412113Sahrens 				dmu_tx_wait(tx);
12422113Sahrens 				dmu_tx_abort(tx);
1243789Sahrens 				goto top;
1244789Sahrens 			}
12452113Sahrens 			dmu_tx_abort(tx);
1246789Sahrens 			ZFS_EXIT(zfsvfs);
12475331Samw 			if (aclp)
12485331Samw 				zfs_acl_free(aclp);
1249789Sahrens 			return (error);
1250789Sahrens 		}
12515446Sahrens 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, aclp, &fuidp);
1252789Sahrens 		(void) zfs_link_create(dl, zp, tx, ZNEW);
12535331Samw 		txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
12545331Samw 		if (flag & FIGNORECASE)
12555331Samw 			txtype |= TX_CI;
12565331Samw 		zfs_log_create(zilog, tx, txtype, dzp, zp, name,
12575331Samw 		    vsecp, fuidp, vap);
12585331Samw 		if (fuidp)
12595331Samw 			zfs_fuid_info_free(fuidp);
1260789Sahrens 		dmu_tx_commit(tx);
1261789Sahrens 	} else {
12625331Samw 		int aflags = (flag & FAPPEND) ? V_APPEND : 0;
12635331Samw 
1264789Sahrens 		/*
1265789Sahrens 		 * A directory entry already exists for this name.
1266789Sahrens 		 */
1267789Sahrens 		/*
1268789Sahrens 		 * Can't truncate an existing file if in exclusive mode.
1269789Sahrens 		 */
1270789Sahrens 		if (excl == EXCL) {
1271789Sahrens 			error = EEXIST;
1272789Sahrens 			goto out;
1273789Sahrens 		}
1274789Sahrens 		/*
1275789Sahrens 		 * Can't open a directory for writing.
1276789Sahrens 		 */
1277789Sahrens 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1278789Sahrens 			error = EISDIR;
1279789Sahrens 			goto out;
1280789Sahrens 		}
1281789Sahrens 		/*
1282789Sahrens 		 * Verify requested access to file.
1283789Sahrens 		 */
12845331Samw 		if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1285789Sahrens 			goto out;
1286789Sahrens 		}
1287789Sahrens 
1288789Sahrens 		mutex_enter(&dzp->z_lock);
1289789Sahrens 		dzp->z_seq++;
1290789Sahrens 		mutex_exit(&dzp->z_lock);
1291789Sahrens 
12921878Smaybee 		/*
12931878Smaybee 		 * Truncate regular files if requested.
12941878Smaybee 		 */
12951878Smaybee 		if ((ZTOV(zp)->v_type == VREG) &&
1296789Sahrens 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
12976992Smaybee 			/* we can't hold any locks when calling zfs_freesp() */
12986992Smaybee 			zfs_dirent_unlock(dl);
12996992Smaybee 			dl = NULL;
13001878Smaybee 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
13014863Spraks 			if (error == 0) {
13025331Samw 				vnevent_create(ZTOV(zp), ct);
13034863Spraks 			}
1304789Sahrens 		}
1305789Sahrens 	}
1306789Sahrens out:
1307789Sahrens 
1308789Sahrens 	if (dl)
1309789Sahrens 		zfs_dirent_unlock(dl);
1310789Sahrens 
1311789Sahrens 	if (error) {
1312789Sahrens 		if (zp)
1313789Sahrens 			VN_RELE(ZTOV(zp));
1314789Sahrens 	} else {
1315789Sahrens 		*vpp = ZTOV(zp);
1316789Sahrens 		/*
1317789Sahrens 		 * If vnode is for a device return a specfs vnode instead.
1318789Sahrens 		 */
1319789Sahrens 		if (IS_DEVVP(*vpp)) {
1320789Sahrens 			struct vnode *svp;
1321789Sahrens 
1322789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1323789Sahrens 			VN_RELE(*vpp);
1324789Sahrens 			if (svp == NULL) {
1325789Sahrens 				error = ENOSYS;
1326789Sahrens 			}
1327789Sahrens 			*vpp = svp;
1328789Sahrens 		}
1329789Sahrens 	}
13305331Samw 	if (aclp)
13315331Samw 		zfs_acl_free(aclp);
1332789Sahrens 
1333789Sahrens 	ZFS_EXIT(zfsvfs);
1334789Sahrens 	return (error);
1335789Sahrens }
1336789Sahrens 
1337789Sahrens /*
1338789Sahrens  * Remove an entry from a directory.
1339789Sahrens  *
1340789Sahrens  *	IN:	dvp	- vnode of directory to remove entry from.
1341789Sahrens  *		name	- name of entry to remove.
1342789Sahrens  *		cr	- credentials of caller.
13435331Samw  *		ct	- caller context
13445331Samw  *		flags	- case flags
1345789Sahrens  *
1346789Sahrens  *	RETURN:	0 if success
1347789Sahrens  *		error code if failure
1348789Sahrens  *
1349789Sahrens  * Timestamps:
1350789Sahrens  *	dvp - ctime|mtime
1351789Sahrens  *	 vp - ctime (if nlink > 0)
1352789Sahrens  */
13535331Samw /*ARGSUSED*/
1354789Sahrens static int
13555331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
13565331Samw     int flags)
1357789Sahrens {
1358789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1359789Sahrens 	znode_t		*xzp = NULL;
1360789Sahrens 	vnode_t		*vp;
1361789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
13625326Sek110237 	zilog_t		*zilog;
1363789Sahrens 	uint64_t	acl_obj, xattr_obj;
1364789Sahrens 	zfs_dirlock_t	*dl;
1365789Sahrens 	dmu_tx_t	*tx;
13663461Sahrens 	boolean_t	may_delete_now, delete_now = FALSE;
13676992Smaybee 	boolean_t	unlinked, toobig = FALSE;
13685331Samw 	uint64_t	txtype;
13695331Samw 	pathname_t	*realnmp = NULL;
13705331Samw 	pathname_t	realnm;
1371789Sahrens 	int		error;
13725331Samw 	int		zflg = ZEXISTS;
1373789Sahrens 
13745367Sahrens 	ZFS_ENTER(zfsvfs);
13755367Sahrens 	ZFS_VERIFY_ZP(dzp);
13765326Sek110237 	zilog = zfsvfs->z_log;
1377789Sahrens 
13785331Samw 	if (flags & FIGNORECASE) {
13795331Samw 		zflg |= ZCILOOK;
13805331Samw 		pn_alloc(&realnm);
13815331Samw 		realnmp = &realnm;
13825331Samw 	}
13835331Samw 
1384789Sahrens top:
1385789Sahrens 	/*
1386789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1387789Sahrens 	 */
13885331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
13895331Samw 	    NULL, realnmp)) {
13905331Samw 		if (realnmp)
13915331Samw 			pn_free(realnmp);
1392789Sahrens 		ZFS_EXIT(zfsvfs);
1393789Sahrens 		return (error);
1394789Sahrens 	}
1395789Sahrens 
1396789Sahrens 	vp = ZTOV(zp);
1397789Sahrens 
1398789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1399789Sahrens 		goto out;
1400789Sahrens 	}
1401789Sahrens 
1402789Sahrens 	/*
1403789Sahrens 	 * Need to use rmdir for removing directories.
1404789Sahrens 	 */
1405789Sahrens 	if (vp->v_type == VDIR) {
1406789Sahrens 		error = EPERM;
1407789Sahrens 		goto out;
1408789Sahrens 	}
1409789Sahrens 
14105331Samw 	vnevent_remove(vp, dvp, name, ct);
14115331Samw 
14125331Samw 	if (realnmp)
14136492Stimh 		dnlc_remove(dvp, realnmp->pn_buf);
14145331Samw 	else
14155331Samw 		dnlc_remove(dvp, name);
14161484Sek110237 
1417789Sahrens 	mutex_enter(&vp->v_lock);
1418789Sahrens 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1419789Sahrens 	mutex_exit(&vp->v_lock);
1420789Sahrens 
1421789Sahrens 	/*
14223461Sahrens 	 * We may delete the znode now, or we may put it in the unlinked set;
1423789Sahrens 	 * it depends on whether we're the last link, and on whether there are
1424789Sahrens 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1425789Sahrens 	 * allow for either case.
1426789Sahrens 	 */
1427789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
14281544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1429789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
14306992Smaybee 	if (may_delete_now) {
14316992Smaybee 		toobig =
14326992Smaybee 		    zp->z_phys->zp_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
14336992Smaybee 		/* if the file is too big, only hold_free a token amount */
14346992Smaybee 		dmu_tx_hold_free(tx, zp->z_id, 0,
14356992Smaybee 		    (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
14366992Smaybee 	}
1437789Sahrens 
1438789Sahrens 	/* are there any extended attributes? */
1439789Sahrens 	if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1440789Sahrens 		/* XXX - do we need this if we are deleting? */
1441789Sahrens 		dmu_tx_hold_bonus(tx, xattr_obj);
1442789Sahrens 	}
1443789Sahrens 
1444789Sahrens 	/* are there any additional acls */
1445789Sahrens 	if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1446789Sahrens 	    may_delete_now)
1447789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1448789Sahrens 
1449789Sahrens 	/* charge as an update -- would be nice not to charge at all */
14503461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1451789Sahrens 
1452789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1453789Sahrens 	if (error) {
1454789Sahrens 		zfs_dirent_unlock(dl);
1455789Sahrens 		VN_RELE(vp);
1456789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
14572113Sahrens 			dmu_tx_wait(tx);
14582113Sahrens 			dmu_tx_abort(tx);
1459789Sahrens 			goto top;
1460789Sahrens 		}
14615331Samw 		if (realnmp)
14625331Samw 			pn_free(realnmp);
14632113Sahrens 		dmu_tx_abort(tx);
1464789Sahrens 		ZFS_EXIT(zfsvfs);
1465789Sahrens 		return (error);
1466789Sahrens 	}
1467789Sahrens 
1468789Sahrens 	/*
1469789Sahrens 	 * Remove the directory entry.
1470789Sahrens 	 */
14715331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1472789Sahrens 
1473789Sahrens 	if (error) {
1474789Sahrens 		dmu_tx_commit(tx);
1475789Sahrens 		goto out;
1476789Sahrens 	}
1477789Sahrens 
14783461Sahrens 	if (unlinked) {
1479789Sahrens 		mutex_enter(&vp->v_lock);
14806992Smaybee 		delete_now = may_delete_now && !toobig &&
1481789Sahrens 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1482789Sahrens 		    zp->z_phys->zp_xattr == xattr_obj &&
1483789Sahrens 		    zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1484789Sahrens 		mutex_exit(&vp->v_lock);
1485789Sahrens 	}
1486789Sahrens 
1487789Sahrens 	if (delete_now) {
1488789Sahrens 		if (zp->z_phys->zp_xattr) {
1489789Sahrens 			error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1490789Sahrens 			ASSERT3U(error, ==, 0);
1491789Sahrens 			ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1492789Sahrens 			dmu_buf_will_dirty(xzp->z_dbuf, tx);
1493789Sahrens 			mutex_enter(&xzp->z_lock);
14943461Sahrens 			xzp->z_unlinked = 1;
1495789Sahrens 			xzp->z_phys->zp_links = 0;
1496789Sahrens 			mutex_exit(&xzp->z_lock);
14973461Sahrens 			zfs_unlinked_add(xzp, tx);
1498789Sahrens 			zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1499789Sahrens 		}
1500789Sahrens 		mutex_enter(&zp->z_lock);
1501789Sahrens 		mutex_enter(&vp->v_lock);
1502789Sahrens 		vp->v_count--;
1503789Sahrens 		ASSERT3U(vp->v_count, ==, 0);
1504789Sahrens 		mutex_exit(&vp->v_lock);
1505789Sahrens 		mutex_exit(&zp->z_lock);
1506789Sahrens 		zfs_znode_delete(zp, tx);
15073461Sahrens 	} else if (unlinked) {
15083461Sahrens 		zfs_unlinked_add(zp, tx);
1509789Sahrens 	}
1510789Sahrens 
15115331Samw 	txtype = TX_REMOVE;
15125331Samw 	if (flags & FIGNORECASE)
15135331Samw 		txtype |= TX_CI;
15145331Samw 	zfs_log_remove(zilog, tx, txtype, dzp, name);
1515789Sahrens 
1516789Sahrens 	dmu_tx_commit(tx);
1517789Sahrens out:
15185331Samw 	if (realnmp)
15195331Samw 		pn_free(realnmp);
15205331Samw 
1521789Sahrens 	zfs_dirent_unlock(dl);
1522789Sahrens 
1523789Sahrens 	if (!delete_now) {
1524789Sahrens 		VN_RELE(vp);
1525789Sahrens 	} else if (xzp) {
15266992Smaybee 		/* this rele is delayed to prevent nesting transactions */
1527789Sahrens 		VN_RELE(ZTOV(xzp));
1528789Sahrens 	}
1529789Sahrens 
1530789Sahrens 	ZFS_EXIT(zfsvfs);
1531789Sahrens 	return (error);
1532789Sahrens }
1533789Sahrens 
1534789Sahrens /*
1535789Sahrens  * Create a new directory and insert it into dvp using the name
1536789Sahrens  * provided.  Return a pointer to the inserted directory.
1537789Sahrens  *
1538789Sahrens  *	IN:	dvp	- vnode of directory to add subdir to.
1539789Sahrens  *		dirname	- name of new directory.
1540789Sahrens  *		vap	- attributes of new directory.
1541789Sahrens  *		cr	- credentials of caller.
15425331Samw  *		ct	- caller context
15435331Samw  *		vsecp	- ACL to be set
1544789Sahrens  *
1545789Sahrens  *	OUT:	vpp	- vnode of created directory.
1546789Sahrens  *
1547789Sahrens  *	RETURN:	0 if success
1548789Sahrens  *		error code if failure
1549789Sahrens  *
1550789Sahrens  * Timestamps:
1551789Sahrens  *	dvp - ctime|mtime updated
1552789Sahrens  *	 vp - ctime|mtime|atime updated
1553789Sahrens  */
15545331Samw /*ARGSUSED*/
1555789Sahrens static int
15565331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
15575331Samw     caller_context_t *ct, int flags, vsecattr_t *vsecp)
1558789Sahrens {
1559789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1560789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
15615326Sek110237 	zilog_t		*zilog;
1562789Sahrens 	zfs_dirlock_t	*dl;
15635331Samw 	uint64_t	txtype;
1564789Sahrens 	dmu_tx_t	*tx;
1565789Sahrens 	int		error;
15665331Samw 	zfs_acl_t	*aclp = NULL;
15675331Samw 	zfs_fuid_info_t	*fuidp = NULL;
15685331Samw 	int		zf = ZNEW;
15697847SMark.Shellenbaum@Sun.COM 	ksid_t		*ksid;
15707847SMark.Shellenbaum@Sun.COM 	uid_t		uid;
15717847SMark.Shellenbaum@Sun.COM 	gid_t		gid = crgetgid(cr);
1572789Sahrens 
1573789Sahrens 	ASSERT(vap->va_type == VDIR);
1574789Sahrens 
15755331Samw 	/*
15765331Samw 	 * If we have an ephemeral id, ACL, or XVATTR then
15775331Samw 	 * make sure file system is at proper version
15785331Samw 	 */
15795331Samw 
15807847SMark.Shellenbaum@Sun.COM 	ksid = crgetsid(cr, KSID_OWNER);
15817847SMark.Shellenbaum@Sun.COM 	if (ksid)
15827847SMark.Shellenbaum@Sun.COM 		uid = ksid_getid(ksid);
15837847SMark.Shellenbaum@Sun.COM 	else
15847847SMark.Shellenbaum@Sun.COM 		uid = crgetuid(cr);
15855331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
15867847SMark.Shellenbaum@Sun.COM 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
15877876SMark.Shellenbaum@Sun.COM 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
15885331Samw 		return (EINVAL);
15895331Samw 
15905367Sahrens 	ZFS_ENTER(zfsvfs);
15915367Sahrens 	ZFS_VERIFY_ZP(dzp);
15925326Sek110237 	zilog = zfsvfs->z_log;
1593789Sahrens 
1594789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1595789Sahrens 		ZFS_EXIT(zfsvfs);
1596789Sahrens 		return (EINVAL);
1597789Sahrens 	}
15985331Samw 
15995498Stimh 	if (zfsvfs->z_utf8 && u8_validate(dirname,
16005331Samw 	    strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
16015331Samw 		ZFS_EXIT(zfsvfs);
16025331Samw 		return (EILSEQ);
16035331Samw 	}
16045331Samw 	if (flags & FIGNORECASE)
16055331Samw 		zf |= ZCILOOK;
16065331Samw 
16075331Samw 	if (vap->va_mask & AT_XVATTR)
16085331Samw 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
16095331Samw 		    crgetuid(cr), cr, vap->va_type)) != 0) {
16105331Samw 			ZFS_EXIT(zfsvfs);
16115331Samw 			return (error);
16125331Samw 		}
1613789Sahrens 
1614789Sahrens 	/*
1615789Sahrens 	 * First make sure the new directory doesn't exist.
1616789Sahrens 	 */
16175331Samw top:
16185331Samw 	*vpp = NULL;
16195331Samw 
16205331Samw 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
16215331Samw 	    NULL, NULL)) {
1622789Sahrens 		ZFS_EXIT(zfsvfs);
1623789Sahrens 		return (error);
1624789Sahrens 	}
1625789Sahrens 
16265331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
16271231Smarks 		zfs_dirent_unlock(dl);
16281231Smarks 		ZFS_EXIT(zfsvfs);
16291231Smarks 		return (error);
16301231Smarks 	}
16311231Smarks 
16325331Samw 	if (vsecp && aclp == NULL) {
16335331Samw 		error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp);
16345331Samw 		if (error) {
16355331Samw 			zfs_dirent_unlock(dl);
16365331Samw 			ZFS_EXIT(zfsvfs);
16375331Samw 			return (error);
16385331Samw 		}
16395331Samw 	}
1640789Sahrens 	/*
1641789Sahrens 	 * Add a new entry to the directory.
1642789Sahrens 	 */
1643789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
16441544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
16451544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
16467847SMark.Shellenbaum@Sun.COM 	if ((aclp && aclp->z_has_fuids) || IS_EPHEMERAL(uid) ||
16477847SMark.Shellenbaum@Sun.COM 	    IS_EPHEMERAL(gid)) {
16485824Smarks 		if (zfsvfs->z_fuid_obj == 0) {
16495824Smarks 			dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
16505824Smarks 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
16515824Smarks 			    FUID_SIZE_ESTIMATE(zfsvfs));
16525824Smarks 			dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
16535824Smarks 		} else {
16545824Smarks 			dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
16555824Smarks 			dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
16565824Smarks 			    FUID_SIZE_ESTIMATE(zfsvfs));
16575824Smarks 		}
16585331Samw 	}
16595331Samw 	if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp)
1660789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1661789Sahrens 		    0, SPA_MAXBLOCKSIZE);
1662789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1663789Sahrens 	if (error) {
1664789Sahrens 		zfs_dirent_unlock(dl);
1665789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
16662113Sahrens 			dmu_tx_wait(tx);
16672113Sahrens 			dmu_tx_abort(tx);
1668789Sahrens 			goto top;
1669789Sahrens 		}
16702113Sahrens 		dmu_tx_abort(tx);
1671789Sahrens 		ZFS_EXIT(zfsvfs);
16725331Samw 		if (aclp)
16735331Samw 			zfs_acl_free(aclp);
1674789Sahrens 		return (error);
1675789Sahrens 	}
1676789Sahrens 
1677789Sahrens 	/*
1678789Sahrens 	 * Create new node.
1679789Sahrens 	 */
16805446Sahrens 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, aclp, &fuidp);
16815331Samw 
16825331Samw 	if (aclp)
16835331Samw 		zfs_acl_free(aclp);
1684789Sahrens 
1685789Sahrens 	/*
1686789Sahrens 	 * Now put new name in parent dir.
1687789Sahrens 	 */
1688789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1689789Sahrens 
1690789Sahrens 	*vpp = ZTOV(zp);
1691789Sahrens 
16925331Samw 	txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
16935331Samw 	if (flags & FIGNORECASE)
16945331Samw 		txtype |= TX_CI;
16955331Samw 	zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, fuidp, vap);
16965331Samw 
16975331Samw 	if (fuidp)
16985331Samw 		zfs_fuid_info_free(fuidp);
1699789Sahrens 	dmu_tx_commit(tx);
1700789Sahrens 
1701789Sahrens 	zfs_dirent_unlock(dl);
1702789Sahrens 
1703789Sahrens 	ZFS_EXIT(zfsvfs);
1704789Sahrens 	return (0);
1705789Sahrens }
1706789Sahrens 
1707789Sahrens /*
1708789Sahrens  * Remove a directory subdir entry.  If the current working
1709789Sahrens  * directory is the same as the subdir to be removed, the
1710789Sahrens  * remove will fail.
1711789Sahrens  *
1712789Sahrens  *	IN:	dvp	- vnode of directory to remove from.
1713789Sahrens  *		name	- name of directory to be removed.
1714789Sahrens  *		cwd	- vnode of current working directory.
1715789Sahrens  *		cr	- credentials of caller.
17165331Samw  *		ct	- caller context
17175331Samw  *		flags	- case flags
1718789Sahrens  *
1719789Sahrens  *	RETURN:	0 if success
1720789Sahrens  *		error code if failure
1721789Sahrens  *
1722789Sahrens  * Timestamps:
1723789Sahrens  *	dvp - ctime|mtime updated
1724789Sahrens  */
17255331Samw /*ARGSUSED*/
1726789Sahrens static int
17275331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
17285331Samw     caller_context_t *ct, int flags)
1729789Sahrens {
1730789Sahrens 	znode_t		*dzp = VTOZ(dvp);
1731789Sahrens 	znode_t		*zp;
1732789Sahrens 	vnode_t		*vp;
1733789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
17345326Sek110237 	zilog_t		*zilog;
1735789Sahrens 	zfs_dirlock_t	*dl;
1736789Sahrens 	dmu_tx_t	*tx;
1737789Sahrens 	int		error;
17385331Samw 	int		zflg = ZEXISTS;
1739789Sahrens 
17405367Sahrens 	ZFS_ENTER(zfsvfs);
17415367Sahrens 	ZFS_VERIFY_ZP(dzp);
17425326Sek110237 	zilog = zfsvfs->z_log;
1743789Sahrens 
17445331Samw 	if (flags & FIGNORECASE)
17455331Samw 		zflg |= ZCILOOK;
1746789Sahrens top:
1747789Sahrens 	zp = NULL;
1748789Sahrens 
1749789Sahrens 	/*
1750789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1751789Sahrens 	 */
17525331Samw 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
17535331Samw 	    NULL, NULL)) {
1754789Sahrens 		ZFS_EXIT(zfsvfs);
1755789Sahrens 		return (error);
1756789Sahrens 	}
1757789Sahrens 
1758789Sahrens 	vp = ZTOV(zp);
1759789Sahrens 
1760789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1761789Sahrens 		goto out;
1762789Sahrens 	}
1763789Sahrens 
1764789Sahrens 	if (vp->v_type != VDIR) {
1765789Sahrens 		error = ENOTDIR;
1766789Sahrens 		goto out;
1767789Sahrens 	}
1768789Sahrens 
1769789Sahrens 	if (vp == cwd) {
1770789Sahrens 		error = EINVAL;
1771789Sahrens 		goto out;
1772789Sahrens 	}
1773789Sahrens 
17745331Samw 	vnevent_rmdir(vp, dvp, name, ct);
1775789Sahrens 
1776789Sahrens 	/*
17773897Smaybee 	 * Grab a lock on the directory to make sure that noone is
17783897Smaybee 	 * trying to add (or lookup) entries while we are removing it.
17793897Smaybee 	 */
17803897Smaybee 	rw_enter(&zp->z_name_lock, RW_WRITER);
17813897Smaybee 
17823897Smaybee 	/*
17833897Smaybee 	 * Grab a lock on the parent pointer to make sure we play well
1784789Sahrens 	 * with the treewalk and directory rename code.
1785789Sahrens 	 */
1786789Sahrens 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1787789Sahrens 
1788789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
17891544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1790789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
17913461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1792789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1793789Sahrens 	if (error) {
1794789Sahrens 		rw_exit(&zp->z_parent_lock);
17953897Smaybee 		rw_exit(&zp->z_name_lock);
1796789Sahrens 		zfs_dirent_unlock(dl);
1797789Sahrens 		VN_RELE(vp);
1798789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
17992113Sahrens 			dmu_tx_wait(tx);
18002113Sahrens 			dmu_tx_abort(tx);
1801789Sahrens 			goto top;
1802789Sahrens 		}
18032113Sahrens 		dmu_tx_abort(tx);
1804789Sahrens 		ZFS_EXIT(zfsvfs);
1805789Sahrens 		return (error);
1806789Sahrens 	}
1807789Sahrens 
18085331Samw 	error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
18095331Samw 
18105331Samw 	if (error == 0) {
18115331Samw 		uint64_t txtype = TX_RMDIR;
18125331Samw 		if (flags & FIGNORECASE)
18135331Samw 			txtype |= TX_CI;
18145331Samw 		zfs_log_remove(zilog, tx, txtype, dzp, name);
18155331Samw 	}
1816789Sahrens 
1817789Sahrens 	dmu_tx_commit(tx);
1818789Sahrens 
1819789Sahrens 	rw_exit(&zp->z_parent_lock);
18203897Smaybee 	rw_exit(&zp->z_name_lock);
1821789Sahrens out:
1822789Sahrens 	zfs_dirent_unlock(dl);
1823789Sahrens 
1824789Sahrens 	VN_RELE(vp);
1825789Sahrens 
1826789Sahrens 	ZFS_EXIT(zfsvfs);
1827789Sahrens 	return (error);
1828789Sahrens }
1829789Sahrens 
1830789Sahrens /*
1831789Sahrens  * Read as many directory entries as will fit into the provided
1832789Sahrens  * buffer from the given directory cursor position (specified in
1833789Sahrens  * the uio structure.
1834789Sahrens  *
1835789Sahrens  *	IN:	vp	- vnode of directory to read.
1836789Sahrens  *		uio	- structure supplying read location, range info,
1837789Sahrens  *			  and return buffer.
1838789Sahrens  *		cr	- credentials of caller.
18395331Samw  *		ct	- caller context
18405331Samw  *		flags	- case flags
1841789Sahrens  *
1842789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
1843789Sahrens  *		eofp	- set to true if end-of-file detected.
1844789Sahrens  *
1845789Sahrens  *	RETURN:	0 if success
1846789Sahrens  *		error code if failure
1847789Sahrens  *
1848789Sahrens  * Timestamps:
1849789Sahrens  *	vp - atime updated
1850789Sahrens  *
1851789Sahrens  * Note that the low 4 bits of the cookie returned by zap is always zero.
1852789Sahrens  * This allows us to use the low range for "special" directory entries:
1853789Sahrens  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
1854789Sahrens  * we use the offset 2 for the '.zfs' directory.
1855789Sahrens  */
1856789Sahrens /* ARGSUSED */
1857789Sahrens static int
18585331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
18595331Samw     caller_context_t *ct, int flags)
1860789Sahrens {
1861789Sahrens 	znode_t		*zp = VTOZ(vp);
1862789Sahrens 	iovec_t		*iovp;
18635331Samw 	edirent_t	*eodp;
1864789Sahrens 	dirent64_t	*odp;
1865789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1866869Sperrin 	objset_t	*os;
1867789Sahrens 	caddr_t		outbuf;
1868789Sahrens 	size_t		bufsize;
1869789Sahrens 	zap_cursor_t	zc;
1870789Sahrens 	zap_attribute_t	zap;
1871789Sahrens 	uint_t		bytes_wanted;
1872789Sahrens 	uint64_t	offset; /* must be unsigned; checks for < 1 */
1873789Sahrens 	int		local_eof;
1874869Sperrin 	int		outcount;
1875869Sperrin 	int		error;
1876869Sperrin 	uint8_t		prefetch;
18775663Sck153898 	boolean_t	check_sysattrs;
1878789Sahrens 
18795367Sahrens 	ZFS_ENTER(zfsvfs);
18805367Sahrens 	ZFS_VERIFY_ZP(zp);
1881789Sahrens 
1882789Sahrens 	/*
1883789Sahrens 	 * If we are not given an eof variable,
1884789Sahrens 	 * use a local one.
1885789Sahrens 	 */
1886789Sahrens 	if (eofp == NULL)
1887789Sahrens 		eofp = &local_eof;
1888789Sahrens 
1889789Sahrens 	/*
1890789Sahrens 	 * Check for valid iov_len.
1891789Sahrens 	 */
1892789Sahrens 	if (uio->uio_iov->iov_len <= 0) {
1893789Sahrens 		ZFS_EXIT(zfsvfs);
1894789Sahrens 		return (EINVAL);
1895789Sahrens 	}
1896789Sahrens 
1897789Sahrens 	/*
1898789Sahrens 	 * Quit if directory has been removed (posix)
1899789Sahrens 	 */
19003461Sahrens 	if ((*eofp = zp->z_unlinked) != 0) {
1901789Sahrens 		ZFS_EXIT(zfsvfs);
1902789Sahrens 		return (0);
1903789Sahrens 	}
1904789Sahrens 
1905869Sperrin 	error = 0;
1906869Sperrin 	os = zfsvfs->z_os;
1907869Sperrin 	offset = uio->uio_loffset;
1908869Sperrin 	prefetch = zp->z_zn_prefetch;
1909869Sperrin 
1910789Sahrens 	/*
1911789Sahrens 	 * Initialize the iterator cursor.
1912789Sahrens 	 */
1913789Sahrens 	if (offset <= 3) {
1914789Sahrens 		/*
1915789Sahrens 		 * Start iteration from the beginning of the directory.
1916789Sahrens 		 */
1917869Sperrin 		zap_cursor_init(&zc, os, zp->z_id);
1918789Sahrens 	} else {
1919789Sahrens 		/*
1920789Sahrens 		 * The offset is a serialized cursor.
1921789Sahrens 		 */
1922869Sperrin 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1923789Sahrens 	}
1924789Sahrens 
1925789Sahrens 	/*
1926789Sahrens 	 * Get space to change directory entries into fs independent format.
1927789Sahrens 	 */
1928789Sahrens 	iovp = uio->uio_iov;
1929789Sahrens 	bytes_wanted = iovp->iov_len;
1930789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
1931789Sahrens 		bufsize = bytes_wanted;
1932789Sahrens 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
1933789Sahrens 		odp = (struct dirent64 *)outbuf;
1934789Sahrens 	} else {
1935789Sahrens 		bufsize = bytes_wanted;
1936789Sahrens 		odp = (struct dirent64 *)iovp->iov_base;
1937789Sahrens 	}
19385331Samw 	eodp = (struct edirent *)odp;
1939789Sahrens 
1940789Sahrens 	/*
19417757SJanice.Chang@Sun.COM 	 * If this VFS supports the system attribute view interface; and
19427757SJanice.Chang@Sun.COM 	 * we're looking at an extended attribute directory; and we care
19437757SJanice.Chang@Sun.COM 	 * about normalization conflicts on this vfs; then we must check
19447757SJanice.Chang@Sun.COM 	 * for normalization conflicts with the sysattr name space.
19455663Sck153898 	 */
19467757SJanice.Chang@Sun.COM 	check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
19475663Sck153898 	    (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
19485663Sck153898 	    (flags & V_RDDIR_ENTFLAGS);
19495663Sck153898 
19505663Sck153898 	/*
1951789Sahrens 	 * Transform to file-system independent format
1952789Sahrens 	 */
1953789Sahrens 	outcount = 0;
1954789Sahrens 	while (outcount < bytes_wanted) {
19553912Slling 		ino64_t objnum;
19563912Slling 		ushort_t reclen;
19573912Slling 		off64_t *next;
19583912Slling 
1959789Sahrens 		/*
1960789Sahrens 		 * Special case `.', `..', and `.zfs'.
1961789Sahrens 		 */
1962789Sahrens 		if (offset == 0) {
1963789Sahrens 			(void) strcpy(zap.za_name, ".");
19645331Samw 			zap.za_normalization_conflict = 0;
19653912Slling 			objnum = zp->z_id;
1966789Sahrens 		} else if (offset == 1) {
1967789Sahrens 			(void) strcpy(zap.za_name, "..");
19685331Samw 			zap.za_normalization_conflict = 0;
19693912Slling 			objnum = zp->z_phys->zp_parent;
1970789Sahrens 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
1971789Sahrens 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
19725331Samw 			zap.za_normalization_conflict = 0;
19733912Slling 			objnum = ZFSCTL_INO_ROOT;
1974789Sahrens 		} else {
1975789Sahrens 			/*
1976789Sahrens 			 * Grab next entry.
1977789Sahrens 			 */
1978789Sahrens 			if (error = zap_cursor_retrieve(&zc, &zap)) {
1979789Sahrens 				if ((*eofp = (error == ENOENT)) != 0)
1980789Sahrens 					break;
1981789Sahrens 				else
1982789Sahrens 					goto update;
1983789Sahrens 			}
1984789Sahrens 
1985789Sahrens 			if (zap.za_integer_length != 8 ||
1986789Sahrens 			    zap.za_num_integers != 1) {
1987789Sahrens 				cmn_err(CE_WARN, "zap_readdir: bad directory "
1988789Sahrens 				    "entry, obj = %lld, offset = %lld\n",
1989789Sahrens 				    (u_longlong_t)zp->z_id,
1990789Sahrens 				    (u_longlong_t)offset);
1991789Sahrens 				error = ENXIO;
1992789Sahrens 				goto update;
1993789Sahrens 			}
19943912Slling 
19953912Slling 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
19963912Slling 			/*
19973912Slling 			 * MacOS X can extract the object type here such as:
19983912Slling 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
19993912Slling 			 */
20005663Sck153898 
20015663Sck153898 			if (check_sysattrs && !zap.za_normalization_conflict) {
20025663Sck153898 				zap.za_normalization_conflict =
20035663Sck153898 				    xattr_sysattr_casechk(zap.za_name);
20045663Sck153898 			}
2005789Sahrens 		}
20065331Samw 
20075331Samw 		if (flags & V_RDDIR_ENTFLAGS)
20085331Samw 			reclen = EDIRENT_RECLEN(strlen(zap.za_name));
20095331Samw 		else
20105331Samw 			reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2011789Sahrens 
2012789Sahrens 		/*
2013789Sahrens 		 * Will this entry fit in the buffer?
2014789Sahrens 		 */
20153912Slling 		if (outcount + reclen > bufsize) {
2016789Sahrens 			/*
2017789Sahrens 			 * Did we manage to fit anything in the buffer?
2018789Sahrens 			 */
2019789Sahrens 			if (!outcount) {
2020789Sahrens 				error = EINVAL;
2021789Sahrens 				goto update;
2022789Sahrens 			}
2023789Sahrens 			break;
2024789Sahrens 		}
20255331Samw 		if (flags & V_RDDIR_ENTFLAGS) {
20265331Samw 			/*
20275331Samw 			 * Add extended flag entry:
20285331Samw 			 */
20295331Samw 			eodp->ed_ino = objnum;
20305331Samw 			eodp->ed_reclen = reclen;
20315331Samw 			/* NOTE: ed_off is the offset for the *next* entry */
20325331Samw 			next = &(eodp->ed_off);
20335331Samw 			eodp->ed_eflags = zap.za_normalization_conflict ?
20345331Samw 			    ED_CASE_CONFLICT : 0;
20355331Samw 			(void) strncpy(eodp->ed_name, zap.za_name,
20365331Samw 			    EDIRENT_NAMELEN(reclen));
20375331Samw 			eodp = (edirent_t *)((intptr_t)eodp + reclen);
20385331Samw 		} else {
20395331Samw 			/*
20405331Samw 			 * Add normal entry:
20415331Samw 			 */
20425331Samw 			odp->d_ino = objnum;
20435331Samw 			odp->d_reclen = reclen;
20445331Samw 			/* NOTE: d_off is the offset for the *next* entry */
20455331Samw 			next = &(odp->d_off);
20465331Samw 			(void) strncpy(odp->d_name, zap.za_name,
20475331Samw 			    DIRENT64_NAMELEN(reclen));
20485331Samw 			odp = (dirent64_t *)((intptr_t)odp + reclen);
20495331Samw 		}
20503912Slling 		outcount += reclen;
2051789Sahrens 
2052789Sahrens 		ASSERT(outcount <= bufsize);
2053789Sahrens 
2054789Sahrens 		/* Prefetch znode */
2055869Sperrin 		if (prefetch)
20563912Slling 			dmu_prefetch(os, objnum, 0, 0);
2057789Sahrens 
2058789Sahrens 		/*
2059789Sahrens 		 * Move to the next entry, fill in the previous offset.
2060789Sahrens 		 */
2061789Sahrens 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2062789Sahrens 			zap_cursor_advance(&zc);
2063789Sahrens 			offset = zap_cursor_serialize(&zc);
2064789Sahrens 		} else {
2065789Sahrens 			offset += 1;
2066789Sahrens 		}
2067789Sahrens 		*next = offset;
2068789Sahrens 	}
2069869Sperrin 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2070789Sahrens 
2071789Sahrens 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2072789Sahrens 		iovp->iov_base += outcount;
2073789Sahrens 		iovp->iov_len -= outcount;
2074789Sahrens 		uio->uio_resid -= outcount;
2075789Sahrens 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2076789Sahrens 		/*
2077789Sahrens 		 * Reset the pointer.
2078789Sahrens 		 */
2079789Sahrens 		offset = uio->uio_loffset;
2080789Sahrens 	}
2081789Sahrens 
2082789Sahrens update:
2083885Sahrens 	zap_cursor_fini(&zc);
2084789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2085789Sahrens 		kmem_free(outbuf, bufsize);
2086789Sahrens 
2087789Sahrens 	if (error == ENOENT)
2088789Sahrens 		error = 0;
2089789Sahrens 
2090789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2091789Sahrens 
2092789Sahrens 	uio->uio_loffset = offset;
2093789Sahrens 	ZFS_EXIT(zfsvfs);
2094789Sahrens 	return (error);
2095789Sahrens }
2096789Sahrens 
20974720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4;
20984720Sfr157268 
2099789Sahrens static int
21005331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2101789Sahrens {
2102789Sahrens 	znode_t	*zp = VTOZ(vp);
2103789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2104789Sahrens 
21051773Seschrock 	/*
21061773Seschrock 	 * Regardless of whether this is required for standards conformance,
21071773Seschrock 	 * this is the logical behavior when fsync() is called on a file with
21081773Seschrock 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
21091773Seschrock 	 * going to be pushed out as part of the zil_commit().
21101773Seschrock 	 */
21111773Seschrock 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
21121773Seschrock 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
21135331Samw 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
21141773Seschrock 
21154720Sfr157268 	(void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
21164720Sfr157268 
21175367Sahrens 	ZFS_ENTER(zfsvfs);
21185367Sahrens 	ZFS_VERIFY_ZP(zp);
21192638Sperrin 	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
2120789Sahrens 	ZFS_EXIT(zfsvfs);
2121789Sahrens 	return (0);
2122789Sahrens }
2123789Sahrens 
21245331Samw 
2125789Sahrens /*
2126789Sahrens  * Get the requested file attributes and place them in the provided
2127789Sahrens  * vattr structure.
2128789Sahrens  *
2129789Sahrens  *	IN:	vp	- vnode of file.
2130789Sahrens  *		vap	- va_mask identifies requested attributes.
21315331Samw  *			  If AT_XVATTR set, then optional attrs are requested
21325331Samw  *		flags	- ATTR_NOACLCHECK (CIFS server context)
2133789Sahrens  *		cr	- credentials of caller.
21345331Samw  *		ct	- caller context
2135789Sahrens  *
2136789Sahrens  *	OUT:	vap	- attribute values.
2137789Sahrens  *
2138789Sahrens  *	RETURN:	0 (always succeeds)
2139789Sahrens  */
2140789Sahrens /* ARGSUSED */
2141789Sahrens static int
21425331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
21435331Samw     caller_context_t *ct)
2144789Sahrens {
2145789Sahrens 	znode_t *zp = VTOZ(vp);
2146789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
21475326Sek110237 	znode_phys_t *pzp;
21485331Samw 	int	error = 0;
21494543Smarks 	uint64_t links;
21505331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
21515331Samw 	xoptattr_t *xoap = NULL;
21525331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2153789Sahrens 
21545367Sahrens 	ZFS_ENTER(zfsvfs);
21555367Sahrens 	ZFS_VERIFY_ZP(zp);
21565326Sek110237 	pzp = zp->z_phys;
2157789Sahrens 
21585331Samw 	mutex_enter(&zp->z_lock);
21595331Samw 
21605331Samw 	/*
21615331Samw 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
21625331Samw 	 * Also, if we are the owner don't bother, since owner should
21635331Samw 	 * always be allowed to read basic attributes of file.
21645331Samw 	 */
21655331Samw 	if (!(pzp->zp_flags & ZFS_ACL_TRIVIAL) &&
21665331Samw 	    (pzp->zp_uid != crgetuid(cr))) {
21675331Samw 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
21685331Samw 		    skipaclchk, cr)) {
21695331Samw 			mutex_exit(&zp->z_lock);
21705331Samw 			ZFS_EXIT(zfsvfs);
21715331Samw 			return (error);
21725331Samw 		}
21735331Samw 	}
21745331Samw 
2175789Sahrens 	/*
2176789Sahrens 	 * Return all attributes.  It's cheaper to provide the answer
2177789Sahrens 	 * than to determine whether we were asked the question.
2178789Sahrens 	 */
2179789Sahrens 
2180789Sahrens 	vap->va_type = vp->v_type;
2181789Sahrens 	vap->va_mode = pzp->zp_mode & MODEMASK;
21825771Sjp151216 	zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2183789Sahrens 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2184789Sahrens 	vap->va_nodeid = zp->z_id;
21854543Smarks 	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
21864543Smarks 		links = pzp->zp_links + 1;
21874543Smarks 	else
21884543Smarks 		links = pzp->zp_links;
21894543Smarks 	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
2190789Sahrens 	vap->va_size = pzp->zp_size;
21911816Smarks 	vap->va_rdev = vp->v_rdev;
2192789Sahrens 	vap->va_seq = zp->z_seq;
2193789Sahrens 
21945331Samw 	/*
21955331Samw 	 * Add in any requested optional attributes and the create time.
21965331Samw 	 * Also set the corresponding bits in the returned attribute bitmap.
21975331Samw 	 */
21985331Samw 	if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
21995331Samw 		if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
22005331Samw 			xoap->xoa_archive =
22015331Samw 			    ((pzp->zp_flags & ZFS_ARCHIVE) != 0);
22025331Samw 			XVA_SET_RTN(xvap, XAT_ARCHIVE);
22035331Samw 		}
22045331Samw 
22055331Samw 		if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
22065331Samw 			xoap->xoa_readonly =
22075331Samw 			    ((pzp->zp_flags & ZFS_READONLY) != 0);
22085331Samw 			XVA_SET_RTN(xvap, XAT_READONLY);
22095331Samw 		}
22105331Samw 
22115331Samw 		if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
22125331Samw 			xoap->xoa_system =
22135331Samw 			    ((pzp->zp_flags & ZFS_SYSTEM) != 0);
22145331Samw 			XVA_SET_RTN(xvap, XAT_SYSTEM);
22155331Samw 		}
22165331Samw 
22175331Samw 		if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
22185331Samw 			xoap->xoa_hidden =
22195331Samw 			    ((pzp->zp_flags & ZFS_HIDDEN) != 0);
22205331Samw 			XVA_SET_RTN(xvap, XAT_HIDDEN);
22215331Samw 		}
22225331Samw 
22235331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
22245331Samw 			xoap->xoa_nounlink =
22255331Samw 			    ((pzp->zp_flags & ZFS_NOUNLINK) != 0);
22265331Samw 			XVA_SET_RTN(xvap, XAT_NOUNLINK);
22275331Samw 		}
22285331Samw 
22295331Samw 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
22305331Samw 			xoap->xoa_immutable =
22315331Samw 			    ((pzp->zp_flags & ZFS_IMMUTABLE) != 0);
22325331Samw 			XVA_SET_RTN(xvap, XAT_IMMUTABLE);
22335331Samw 		}
22345331Samw 
22355331Samw 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
22365331Samw 			xoap->xoa_appendonly =
22375331Samw 			    ((pzp->zp_flags & ZFS_APPENDONLY) != 0);
22385331Samw 			XVA_SET_RTN(xvap, XAT_APPENDONLY);
22395331Samw 		}
22405331Samw 
22415331Samw 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
22425331Samw 			xoap->xoa_nodump =
22435331Samw 			    ((pzp->zp_flags & ZFS_NODUMP) != 0);
22445331Samw 			XVA_SET_RTN(xvap, XAT_NODUMP);
22455331Samw 		}
22465331Samw 
22475331Samw 		if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
22485331Samw 			xoap->xoa_opaque =
22495331Samw 			    ((pzp->zp_flags & ZFS_OPAQUE) != 0);
22505331Samw 			XVA_SET_RTN(xvap, XAT_OPAQUE);
22515331Samw 		}
22525331Samw 
22535331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
22545331Samw 			xoap->xoa_av_quarantined =
22555331Samw 			    ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0);
22565331Samw 			XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
22575331Samw 		}
22585331Samw 
22595331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
22605331Samw 			xoap->xoa_av_modified =
22615331Samw 			    ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0);
22625331Samw 			XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
22635331Samw 		}
22645331Samw 
22655331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
22665331Samw 		    vp->v_type == VREG &&
22675331Samw 		    (pzp->zp_flags & ZFS_BONUS_SCANSTAMP)) {
22685331Samw 			size_t len;
22695331Samw 			dmu_object_info_t doi;
22705331Samw 
22715331Samw 			/*
22725331Samw 			 * Only VREG files have anti-virus scanstamps, so we
22735331Samw 			 * won't conflict with symlinks in the bonus buffer.
22745331Samw 			 */
22755331Samw 			dmu_object_info_from_db(zp->z_dbuf, &doi);
22765331Samw 			len = sizeof (xoap->xoa_av_scanstamp) +
22775331Samw 			    sizeof (znode_phys_t);
22785331Samw 			if (len <= doi.doi_bonus_size) {
22795331Samw 				/*
22805331Samw 				 * pzp points to the start of the
22815331Samw 				 * znode_phys_t. pzp + 1 points to the
22825331Samw 				 * first byte after the znode_phys_t.
22835331Samw 				 */
22845331Samw 				(void) memcpy(xoap->xoa_av_scanstamp,
22855331Samw 				    pzp + 1,
22865331Samw 				    sizeof (xoap->xoa_av_scanstamp));
22875331Samw 				XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
22885331Samw 			}
22895331Samw 		}
22905331Samw 
22915331Samw 		if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
22925331Samw 			ZFS_TIME_DECODE(&xoap->xoa_createtime, pzp->zp_crtime);
22935331Samw 			XVA_SET_RTN(xvap, XAT_CREATETIME);
22945331Samw 		}
22955331Samw 	}
22965331Samw 
2297789Sahrens 	ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
2298789Sahrens 	ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
2299789Sahrens 	ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
2300789Sahrens 
2301789Sahrens 	mutex_exit(&zp->z_lock);
2302789Sahrens 
2303789Sahrens 	dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks);
2304789Sahrens 
2305789Sahrens 	if (zp->z_blksz == 0) {
2306789Sahrens 		/*
2307789Sahrens 		 * Block size hasn't been set; suggest maximal I/O transfers.
2308789Sahrens 		 */
2309789Sahrens 		vap->va_blksize = zfsvfs->z_max_blksz;
2310789Sahrens 	}
2311789Sahrens 
2312789Sahrens 	ZFS_EXIT(zfsvfs);
2313789Sahrens 	return (0);
2314789Sahrens }
2315789Sahrens 
2316789Sahrens /*
2317789Sahrens  * Set the file attributes to the values contained in the
2318789Sahrens  * vattr structure.
2319789Sahrens  *
2320789Sahrens  *	IN:	vp	- vnode of file to be modified.
2321789Sahrens  *		vap	- new attribute values.
23225331Samw  *			  If AT_XVATTR set, then optional attrs are being set
2323789Sahrens  *		flags	- ATTR_UTIME set if non-default time values provided.
23245331Samw  *			- ATTR_NOACLCHECK (CIFS context only).
2325789Sahrens  *		cr	- credentials of caller.
23265331Samw  *		ct	- caller context
2327789Sahrens  *
2328789Sahrens  *	RETURN:	0 if success
2329789Sahrens  *		error code if failure
2330789Sahrens  *
2331789Sahrens  * Timestamps:
2332789Sahrens  *	vp - ctime updated, mtime updated if size changed.
2333789Sahrens  */
2334789Sahrens /* ARGSUSED */
2335789Sahrens static int
2336789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2337789Sahrens 	caller_context_t *ct)
2338789Sahrens {
23395326Sek110237 	znode_t		*zp = VTOZ(vp);
23405326Sek110237 	znode_phys_t	*pzp;
2341789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
23425326Sek110237 	zilog_t		*zilog;
2343789Sahrens 	dmu_tx_t	*tx;
23441878Smaybee 	vattr_t		oldva;
2345*8190SMark.Shellenbaum@Sun.COM 	xvattr_t	tmpxvattr;
2346789Sahrens 	uint_t		mask = vap->va_mask;
23471878Smaybee 	uint_t		saved_mask;
23482796Smarks 	int		trim_mask = 0;
2349789Sahrens 	uint64_t	new_mode;
23501231Smarks 	znode_t		*attrzp;
2351789Sahrens 	int		need_policy = FALSE;
2352789Sahrens 	int		err;
23535331Samw 	zfs_fuid_info_t *fuidp = NULL;
23545331Samw 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
23555331Samw 	xoptattr_t	*xoap;
23565824Smarks 	zfs_acl_t	*aclp = NULL;
23575331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2358789Sahrens 
2359789Sahrens 	if (mask == 0)
2360789Sahrens 		return (0);
2361789Sahrens 
2362789Sahrens 	if (mask & AT_NOSET)
2363789Sahrens 		return (EINVAL);
2364789Sahrens 
23655367Sahrens 	ZFS_ENTER(zfsvfs);
23665367Sahrens 	ZFS_VERIFY_ZP(zp);
23675331Samw 
23685331Samw 	pzp = zp->z_phys;
23695331Samw 	zilog = zfsvfs->z_log;
23705331Samw 
23715331Samw 	/*
23725331Samw 	 * Make sure that if we have ephemeral uid/gid or xvattr specified
23735331Samw 	 * that file system is at proper version level
23745331Samw 	 */
23755331Samw 
23765331Samw 	if (zfsvfs->z_use_fuids == B_FALSE &&
23775331Samw 	    (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
23785331Samw 	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
23795386Stimh 	    (mask & AT_XVATTR))) {
23805386Stimh 		ZFS_EXIT(zfsvfs);
23815331Samw 		return (EINVAL);
23825386Stimh 	}
23835386Stimh 
23845386Stimh 	if (mask & AT_SIZE && vp->v_type == VDIR) {
23855386Stimh 		ZFS_EXIT(zfsvfs);
2386789Sahrens 		return (EISDIR);
23875386Stimh 	}
23885386Stimh 
23895386Stimh 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
23905386Stimh 		ZFS_EXIT(zfsvfs);
23911308Smarks 		return (EINVAL);
23925386Stimh 	}
23931308Smarks 
23945331Samw 	/*
23955331Samw 	 * If this is an xvattr_t, then get a pointer to the structure of
23965331Samw 	 * optional attributes.  If this is NULL, then we have a vattr_t.
23975331Samw 	 */
23985331Samw 	xoap = xva_getxoptattr(xvap);
23995331Samw 
2400*8190SMark.Shellenbaum@Sun.COM 	xva_init(&tmpxvattr);
2401*8190SMark.Shellenbaum@Sun.COM 
24025331Samw 	/*
24035331Samw 	 * Immutable files can only alter immutable bit and atime
24045331Samw 	 */
24055331Samw 	if ((pzp->zp_flags & ZFS_IMMUTABLE) &&
24065331Samw 	    ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
24075386Stimh 	    ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
24085386Stimh 		ZFS_EXIT(zfsvfs);
24095331Samw 		return (EPERM);
24105386Stimh 	}
24115386Stimh 
24125386Stimh 	if ((mask & AT_SIZE) && (pzp->zp_flags & ZFS_READONLY)) {
24135386Stimh 		ZFS_EXIT(zfsvfs);
24145331Samw 		return (EPERM);
24155386Stimh 	}
2416789Sahrens 
24176064Smarks 	/*
24186064Smarks 	 * Verify timestamps doesn't overflow 32 bits.
24196064Smarks 	 * ZFS can handle large timestamps, but 32bit syscalls can't
24206064Smarks 	 * handle times greater than 2039.  This check should be removed
24216064Smarks 	 * once large timestamps are fully supported.
24226064Smarks 	 */
24236064Smarks 	if (mask & (AT_ATIME | AT_MTIME)) {
24246064Smarks 		if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
24256064Smarks 		    ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
24266064Smarks 			ZFS_EXIT(zfsvfs);
24276064Smarks 			return (EOVERFLOW);
24286064Smarks 		}
24296064Smarks 	}
24306064Smarks 
2431789Sahrens top:
24321231Smarks 	attrzp = NULL;
2433789Sahrens 
2434789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2435789Sahrens 		ZFS_EXIT(zfsvfs);
2436789Sahrens 		return (EROFS);
2437789Sahrens 	}
2438789Sahrens 
2439789Sahrens 	/*
2440789Sahrens 	 * First validate permissions
2441789Sahrens 	 */
2442789Sahrens 
2443789Sahrens 	if (mask & AT_SIZE) {
24445331Samw 		err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2445789Sahrens 		if (err) {
2446789Sahrens 			ZFS_EXIT(zfsvfs);
2447789Sahrens 			return (err);
2448789Sahrens 		}
24491878Smaybee 		/*
24501878Smaybee 		 * XXX - Note, we are not providing any open
24511878Smaybee 		 * mode flags here (like FNDELAY), so we may
24521878Smaybee 		 * block if there are locks present... this
24531878Smaybee 		 * should be addressed in openat().
24541878Smaybee 		 */
24556992Smaybee 		/* XXX - would it be OK to generate a log record here? */
24566992Smaybee 		err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
24571878Smaybee 		if (err) {
24581878Smaybee 			ZFS_EXIT(zfsvfs);
24591878Smaybee 			return (err);
24601878Smaybee 		}
2461789Sahrens 	}
2462789Sahrens 
24635331Samw 	if (mask & (AT_ATIME|AT_MTIME) ||
24645331Samw 	    ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
24655331Samw 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
24665331Samw 	    XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
24675331Samw 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
24685331Samw 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM))))
24695331Samw 		need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
24705331Samw 		    skipaclchk, cr);
2471789Sahrens 
2472789Sahrens 	if (mask & (AT_UID|AT_GID)) {
2473789Sahrens 		int	idmask = (mask & (AT_UID|AT_GID));
2474789Sahrens 		int	take_owner;
2475789Sahrens 		int	take_group;
2476789Sahrens 
2477789Sahrens 		/*
2478913Smarks 		 * NOTE: even if a new mode is being set,
2479913Smarks 		 * we may clear S_ISUID/S_ISGID bits.
2480913Smarks 		 */
2481913Smarks 
2482913Smarks 		if (!(mask & AT_MODE))
2483913Smarks 			vap->va_mode = pzp->zp_mode;
2484913Smarks 
2485913Smarks 		/*
2486789Sahrens 		 * Take ownership or chgrp to group we are a member of
2487789Sahrens 		 */
2488789Sahrens 
2489789Sahrens 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
24905331Samw 		take_group = (mask & AT_GID) &&
24915331Samw 		    zfs_groupmember(zfsvfs, vap->va_gid, cr);
2492789Sahrens 
2493789Sahrens 		/*
2494789Sahrens 		 * If both AT_UID and AT_GID are set then take_owner and
2495789Sahrens 		 * take_group must both be set in order to allow taking
2496789Sahrens 		 * ownership.
2497789Sahrens 		 *
2498789Sahrens 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2499789Sahrens 		 *
2500789Sahrens 		 */
2501789Sahrens 
2502789Sahrens 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2503789Sahrens 		    ((idmask == AT_UID) && take_owner) ||
2504789Sahrens 		    ((idmask == AT_GID) && take_group)) {
25055331Samw 			if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
25065331Samw 			    skipaclchk, cr) == 0) {
2507789Sahrens 				/*
2508789Sahrens 				 * Remove setuid/setgid for non-privileged users
2509789Sahrens 				 */
25101115Smarks 				secpolicy_setid_clear(vap, cr);
25112796Smarks 				trim_mask = (mask & (AT_UID|AT_GID));
2512789Sahrens 			} else {
2513789Sahrens 				need_policy =  TRUE;
2514789Sahrens 			}
2515789Sahrens 		} else {
2516789Sahrens 			need_policy =  TRUE;
2517789Sahrens 		}
2518789Sahrens 	}
2519789Sahrens 
25202796Smarks 	mutex_enter(&zp->z_lock);
25212796Smarks 	oldva.va_mode = pzp->zp_mode;
25225771Sjp151216 	zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
25235331Samw 	if (mask & AT_XVATTR) {
2524*8190SMark.Shellenbaum@Sun.COM 		/*
2525*8190SMark.Shellenbaum@Sun.COM 		 * Update xvattr mask to include only those attributes
2526*8190SMark.Shellenbaum@Sun.COM 		 * that are actually changing.
2527*8190SMark.Shellenbaum@Sun.COM 		 *
2528*8190SMark.Shellenbaum@Sun.COM 		 * the bits will be restored prior to actually setting
2529*8190SMark.Shellenbaum@Sun.COM 		 * the attributes so the caller thinks they were set.
2530*8190SMark.Shellenbaum@Sun.COM 		 */
2531*8190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2532*8190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_appendonly !=
2533*8190SMark.Shellenbaum@Sun.COM 			    ((pzp->zp_flags & ZFS_APPENDONLY) != 0)) {
2534*8190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
2535*8190SMark.Shellenbaum@Sun.COM 			} else {
2536*8190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2537*8190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
2538*8190SMark.Shellenbaum@Sun.COM 			}
2539*8190SMark.Shellenbaum@Sun.COM 		}
2540*8190SMark.Shellenbaum@Sun.COM 
2541*8190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2542*8190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_nounlink !=
2543*8190SMark.Shellenbaum@Sun.COM 			    ((pzp->zp_flags & ZFS_NOUNLINK) != 0)) {
2544*8190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
2545*8190SMark.Shellenbaum@Sun.COM 			} else {
2546*8190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2547*8190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
2548*8190SMark.Shellenbaum@Sun.COM 			}
2549*8190SMark.Shellenbaum@Sun.COM 		}
2550*8190SMark.Shellenbaum@Sun.COM 
2551*8190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2552*8190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_immutable !=
2553*8190SMark.Shellenbaum@Sun.COM 			    ((pzp->zp_flags & ZFS_IMMUTABLE) != 0)) {
2554*8190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
2555*8190SMark.Shellenbaum@Sun.COM 			} else {
2556*8190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2557*8190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
2558*8190SMark.Shellenbaum@Sun.COM 			}
2559*8190SMark.Shellenbaum@Sun.COM 		}
2560*8190SMark.Shellenbaum@Sun.COM 
2561*8190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2562*8190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_nodump !=
2563*8190SMark.Shellenbaum@Sun.COM 			    ((pzp->zp_flags & ZFS_NODUMP) != 0)) {
2564*8190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
2565*8190SMark.Shellenbaum@Sun.COM 			} else {
2566*8190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_NODUMP);
2567*8190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
2568*8190SMark.Shellenbaum@Sun.COM 			}
2569*8190SMark.Shellenbaum@Sun.COM 		}
2570*8190SMark.Shellenbaum@Sun.COM 
2571*8190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2572*8190SMark.Shellenbaum@Sun.COM 			if (xoap->xoa_av_modified !=
2573*8190SMark.Shellenbaum@Sun.COM 			    ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0)) {
2574*8190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
2575*8190SMark.Shellenbaum@Sun.COM 			} else {
2576*8190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2577*8190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
2578*8190SMark.Shellenbaum@Sun.COM 			}
2579*8190SMark.Shellenbaum@Sun.COM 		}
2580*8190SMark.Shellenbaum@Sun.COM 
2581*8190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2582*8190SMark.Shellenbaum@Sun.COM 			if ((vp->v_type != VREG &&
2583*8190SMark.Shellenbaum@Sun.COM 			    xoap->xoa_av_quarantined) ||
2584*8190SMark.Shellenbaum@Sun.COM 			    xoap->xoa_av_quarantined !=
2585*8190SMark.Shellenbaum@Sun.COM 			    ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0)) {
2586*8190SMark.Shellenbaum@Sun.COM 				need_policy = TRUE;
2587*8190SMark.Shellenbaum@Sun.COM 			} else {
2588*8190SMark.Shellenbaum@Sun.COM 				XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2589*8190SMark.Shellenbaum@Sun.COM 				XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
2590*8190SMark.Shellenbaum@Sun.COM 			}
2591*8190SMark.Shellenbaum@Sun.COM 		}
2592*8190SMark.Shellenbaum@Sun.COM 
2593*8190SMark.Shellenbaum@Sun.COM 		if (need_policy == FALSE &&
2594*8190SMark.Shellenbaum@Sun.COM 		    (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2595*8190SMark.Shellenbaum@Sun.COM 		    XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
25965331Samw 			need_policy = TRUE;
25975331Samw 		}
25985331Samw 	}
25995331Samw 
26002796Smarks 	mutex_exit(&zp->z_lock);
26012796Smarks 
26022796Smarks 	if (mask & AT_MODE) {
26035331Samw 		if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
26042796Smarks 			err = secpolicy_setid_setsticky_clear(vp, vap,
26052796Smarks 			    &oldva, cr);
26062796Smarks 			if (err) {
26072796Smarks 				ZFS_EXIT(zfsvfs);
26082796Smarks 				return (err);
26092796Smarks 			}
26102796Smarks 			trim_mask |= AT_MODE;
26112796Smarks 		} else {
26122796Smarks 			need_policy = TRUE;
26132796Smarks 		}
26142796Smarks 	}
2615789Sahrens 
2616789Sahrens 	if (need_policy) {
26171115Smarks 		/*
26181115Smarks 		 * If trim_mask is set then take ownership
26192796Smarks 		 * has been granted or write_acl is present and user
26202796Smarks 		 * has the ability to modify mode.  In that case remove
26212796Smarks 		 * UID|GID and or MODE from mask so that
26221115Smarks 		 * secpolicy_vnode_setattr() doesn't revoke it.
26231115Smarks 		 */
26242796Smarks 
26252796Smarks 		if (trim_mask) {
26262796Smarks 			saved_mask = vap->va_mask;
26272796Smarks 			vap->va_mask &= ~trim_mask;
26282796Smarks 		}
2629789Sahrens 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
26305331Samw 		    (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2631789Sahrens 		if (err) {
2632789Sahrens 			ZFS_EXIT(zfsvfs);
2633789Sahrens 			return (err);
2634789Sahrens 		}
26351115Smarks 
26361115Smarks 		if (trim_mask)
26372796Smarks 			vap->va_mask |= saved_mask;
2638789Sahrens 	}
2639789Sahrens 
2640789Sahrens 	/*
2641789Sahrens 	 * secpolicy_vnode_setattr, or take ownership may have
2642789Sahrens 	 * changed va_mask
2643789Sahrens 	 */
2644789Sahrens 	mask = vap->va_mask;
2645789Sahrens 
2646789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2647789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
26485824Smarks 	if (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
26495824Smarks 	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid))) {
26505824Smarks 		if (zfsvfs->z_fuid_obj == 0) {
26515824Smarks 			dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
26525824Smarks 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
26535824Smarks 			    FUID_SIZE_ESTIMATE(zfsvfs));
26545824Smarks 			dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
26555824Smarks 		} else {
26565824Smarks 			dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
26575824Smarks 			dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
26585824Smarks 			    FUID_SIZE_ESTIMATE(zfsvfs));
26595824Smarks 		}
26605331Samw 	}
2661789Sahrens 
2662789Sahrens 	if (mask & AT_MODE) {
26631576Smarks 		uint64_t pmode = pzp->zp_mode;
26641576Smarks 
26651576Smarks 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2666789Sahrens 
26675824Smarks 		if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)) {
26685824Smarks 			dmu_tx_abort(tx);
26695824Smarks 			ZFS_EXIT(zfsvfs);
26705824Smarks 			return (err);
26715824Smarks 		}
26725331Samw 		if (pzp->zp_acl.z_acl_extern_obj) {
26735331Samw 			/* Are we upgrading ACL from old V0 format to new V1 */
26745331Samw 			if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
26755331Samw 			    pzp->zp_acl.z_acl_version ==
26765331Samw 			    ZFS_ACL_VERSION_INITIAL) {
26775331Samw 				dmu_tx_hold_free(tx,
26785331Samw 				    pzp->zp_acl.z_acl_extern_obj, 0,
26795331Samw 				    DMU_OBJECT_END);
26805331Samw 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
26815824Smarks 				    0, aclp->z_acl_bytes);
26825331Samw 			} else {
26835331Samw 				dmu_tx_hold_write(tx,
26845331Samw 				    pzp->zp_acl.z_acl_extern_obj, 0,
26855824Smarks 				    aclp->z_acl_bytes);
26865331Samw 			}
26876180Smarks 		} else if (aclp->z_acl_bytes > ZFS_ACE_SPACE) {
26886180Smarks 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
26896180Smarks 			    0, aclp->z_acl_bytes);
26905331Samw 		}
2691789Sahrens 	}
2692789Sahrens 
26935331Samw 	if ((mask & (AT_UID | AT_GID)) && pzp->zp_xattr != 0) {
26945331Samw 		err = zfs_zget(zp->z_zfsvfs, pzp->zp_xattr, &attrzp);
26951231Smarks 		if (err) {
26961231Smarks 			dmu_tx_abort(tx);
26971231Smarks 			ZFS_EXIT(zfsvfs);
26985824Smarks 			if (aclp)
26995824Smarks 				zfs_acl_free(aclp);
27001231Smarks 			return (err);
27011231Smarks 		}
27021231Smarks 		dmu_tx_hold_bonus(tx, attrzp->z_id);
27031231Smarks 	}
27041231Smarks 
2705789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2706789Sahrens 	if (err) {
27071231Smarks 		if (attrzp)
27081231Smarks 			VN_RELE(ZTOV(attrzp));
27095824Smarks 
27105824Smarks 		if (aclp) {
27115824Smarks 			zfs_acl_free(aclp);
27125824Smarks 			aclp = NULL;
27135824Smarks 		}
27145824Smarks 
2715789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
27162113Sahrens 			dmu_tx_wait(tx);
27172113Sahrens 			dmu_tx_abort(tx);
2718789Sahrens 			goto top;
2719789Sahrens 		}
27202113Sahrens 		dmu_tx_abort(tx);
2721789Sahrens 		ZFS_EXIT(zfsvfs);
2722789Sahrens 		return (err);
2723789Sahrens 	}
2724789Sahrens 
2725789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
2726789Sahrens 
2727789Sahrens 	/*
2728789Sahrens 	 * Set each attribute requested.
2729789Sahrens 	 * We group settings according to the locks they need to acquire.
2730789Sahrens 	 *
2731789Sahrens 	 * Note: you cannot set ctime directly, although it will be
2732789Sahrens 	 * updated as a side-effect of calling this function.
2733789Sahrens 	 */
2734789Sahrens 
2735789Sahrens 	mutex_enter(&zp->z_lock);
2736789Sahrens 
2737789Sahrens 	if (mask & AT_MODE) {
27385824Smarks 		mutex_enter(&zp->z_acl_lock);
27395824Smarks 		zp->z_phys->zp_mode = new_mode;
27405824Smarks 		err = zfs_aclset_common(zp, aclp, cr, &fuidp, tx);
2741789Sahrens 		ASSERT3U(err, ==, 0);
27425824Smarks 		mutex_exit(&zp->z_acl_lock);
2743789Sahrens 	}
2744789Sahrens 
27451231Smarks 	if (attrzp)
27461231Smarks 		mutex_enter(&attrzp->z_lock);
27471231Smarks 
27481231Smarks 	if (mask & AT_UID) {
27495331Samw 		pzp->zp_uid = zfs_fuid_create(zfsvfs,
27505771Sjp151216 		    vap->va_uid, cr, ZFS_OWNER, tx, &fuidp);
27511231Smarks 		if (attrzp) {
27525331Samw 			attrzp->z_phys->zp_uid = zfs_fuid_create(zfsvfs,
27535771Sjp151216 			    vap->va_uid,  cr, ZFS_OWNER, tx, &fuidp);
27541231Smarks 		}
27551231Smarks 	}
27561231Smarks 
27571231Smarks 	if (mask & AT_GID) {
27585331Samw 		pzp->zp_gid = zfs_fuid_create(zfsvfs, vap->va_gid,
27595771Sjp151216 		    cr, ZFS_GROUP, tx, &fuidp);
27601231Smarks 		if (attrzp)
27615331Samw 			attrzp->z_phys->zp_gid = zfs_fuid_create(zfsvfs,
27625771Sjp151216 			    vap->va_gid, cr, ZFS_GROUP, tx, &fuidp);
27631231Smarks 	}
27641231Smarks 
27655824Smarks 	if (aclp)
27665824Smarks 		zfs_acl_free(aclp);
27675824Smarks 
27681231Smarks 	if (attrzp)
27691231Smarks 		mutex_exit(&attrzp->z_lock);
2770789Sahrens 
2771789Sahrens 	if (mask & AT_ATIME)
2772789Sahrens 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
2773789Sahrens 
2774789Sahrens 	if (mask & AT_MTIME)
2775789Sahrens 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
2776789Sahrens 
27776992Smaybee 	/* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
27781878Smaybee 	if (mask & AT_SIZE)
2779789Sahrens 		zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
27801878Smaybee 	else if (mask != 0)
2781789Sahrens 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
27825331Samw 	/*
27835331Samw 	 * Do this after setting timestamps to prevent timestamp
27845331Samw 	 * update from toggling bit
27855331Samw 	 */
27865331Samw 
27875331Samw 	if (xoap && (mask & AT_XVATTR)) {
2788*8190SMark.Shellenbaum@Sun.COM 
2789*8190SMark.Shellenbaum@Sun.COM 		/*
2790*8190SMark.Shellenbaum@Sun.COM 		 * restore trimmed off masks
2791*8190SMark.Shellenbaum@Sun.COM 		 * so that return masks can be set for caller.
2792*8190SMark.Shellenbaum@Sun.COM 		 */
2793*8190SMark.Shellenbaum@Sun.COM 
2794*8190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
2795*8190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_APPENDONLY);
2796*8190SMark.Shellenbaum@Sun.COM 		}
2797*8190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
2798*8190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_NOUNLINK);
2799*8190SMark.Shellenbaum@Sun.COM 		}
2800*8190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
2801*8190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_IMMUTABLE);
2802*8190SMark.Shellenbaum@Sun.COM 		}
2803*8190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
2804*8190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_NODUMP);
2805*8190SMark.Shellenbaum@Sun.COM 		}
2806*8190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
2807*8190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
2808*8190SMark.Shellenbaum@Sun.COM 		}
2809*8190SMark.Shellenbaum@Sun.COM 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
2810*8190SMark.Shellenbaum@Sun.COM 			XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
2811*8190SMark.Shellenbaum@Sun.COM 		}
2812*8190SMark.Shellenbaum@Sun.COM 
28135331Samw 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
28145331Samw 			size_t len;
28155331Samw 			dmu_object_info_t doi;
28165331Samw 
28175331Samw 			ASSERT(vp->v_type == VREG);
28185331Samw 
28195331Samw 			/* Grow the bonus buffer if necessary. */
28205331Samw 			dmu_object_info_from_db(zp->z_dbuf, &doi);
28215331Samw 			len = sizeof (xoap->xoa_av_scanstamp) +
28225331Samw 			    sizeof (znode_phys_t);
28235331Samw 			if (len > doi.doi_bonus_size)
28245331Samw 				VERIFY(dmu_set_bonus(zp->z_dbuf, len, tx) == 0);
28255331Samw 		}
28265331Samw 		zfs_xvattr_set(zp, xvap);
28275331Samw 	}
2828789Sahrens 
28291878Smaybee 	if (mask != 0)
28305331Samw 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
28315331Samw 
28325331Samw 	if (fuidp)
28335331Samw 		zfs_fuid_info_free(fuidp);
2834789Sahrens 	mutex_exit(&zp->z_lock);
2835789Sahrens 
28361231Smarks 	if (attrzp)
28371231Smarks 		VN_RELE(ZTOV(attrzp));
28381231Smarks 
2839789Sahrens 	dmu_tx_commit(tx);
2840789Sahrens 
2841789Sahrens 	ZFS_EXIT(zfsvfs);
2842789Sahrens 	return (err);
2843789Sahrens }
2844789Sahrens 
28453271Smaybee typedef struct zfs_zlock {
28463271Smaybee 	krwlock_t	*zl_rwlock;	/* lock we acquired */
28473271Smaybee 	znode_t		*zl_znode;	/* znode we held */
28483271Smaybee 	struct zfs_zlock *zl_next;	/* next in list */
28493271Smaybee } zfs_zlock_t;
28503271Smaybee 
28513271Smaybee /*
28523271Smaybee  * Drop locks and release vnodes that were held by zfs_rename_lock().
28533271Smaybee  */
28543271Smaybee static void
28553271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp)
28563271Smaybee {
28573271Smaybee 	zfs_zlock_t *zl;
28583271Smaybee 
28593271Smaybee 	while ((zl = *zlpp) != NULL) {
28603271Smaybee 		if (zl->zl_znode != NULL)
28613271Smaybee 			VN_RELE(ZTOV(zl->zl_znode));
28623271Smaybee 		rw_exit(zl->zl_rwlock);
28633271Smaybee 		*zlpp = zl->zl_next;
28643271Smaybee 		kmem_free(zl, sizeof (*zl));
28653271Smaybee 	}
28663271Smaybee }
28673271Smaybee 
2868789Sahrens /*
2869789Sahrens  * Search back through the directory tree, using the ".." entries.
2870789Sahrens  * Lock each directory in the chain to prevent concurrent renames.
2871789Sahrens  * Fail any attempt to move a directory into one of its own descendants.
2872789Sahrens  * XXX - z_parent_lock can overlap with map or grow locks
2873789Sahrens  */
2874789Sahrens static int
2875789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
2876789Sahrens {
2877789Sahrens 	zfs_zlock_t	*zl;
28783638Sbillm 	znode_t		*zp = tdzp;
2879789Sahrens 	uint64_t	rootid = zp->z_zfsvfs->z_root;
2880789Sahrens 	uint64_t	*oidp = &zp->z_id;
2881789Sahrens 	krwlock_t	*rwlp = &szp->z_parent_lock;
2882789Sahrens 	krw_t		rw = RW_WRITER;
2883789Sahrens 
2884789Sahrens 	/*
2885789Sahrens 	 * First pass write-locks szp and compares to zp->z_id.
2886789Sahrens 	 * Later passes read-lock zp and compare to zp->z_parent.
2887789Sahrens 	 */
2888789Sahrens 	do {
28893271Smaybee 		if (!rw_tryenter(rwlp, rw)) {
28903271Smaybee 			/*
28913271Smaybee 			 * Another thread is renaming in this path.
28923271Smaybee 			 * Note that if we are a WRITER, we don't have any
28933271Smaybee 			 * parent_locks held yet.
28943271Smaybee 			 */
28953271Smaybee 			if (rw == RW_READER && zp->z_id > szp->z_id) {
28963271Smaybee 				/*
28973271Smaybee 				 * Drop our locks and restart
28983271Smaybee 				 */
28993271Smaybee 				zfs_rename_unlock(&zl);
29003271Smaybee 				*zlpp = NULL;
29013271Smaybee 				zp = tdzp;
29023271Smaybee 				oidp = &zp->z_id;
29033271Smaybee 				rwlp = &szp->z_parent_lock;
29043271Smaybee 				rw = RW_WRITER;
29053271Smaybee 				continue;
29063271Smaybee 			} else {
29073271Smaybee 				/*
29083271Smaybee 				 * Wait for other thread to drop its locks
29093271Smaybee 				 */
29103271Smaybee 				rw_enter(rwlp, rw);
29113271Smaybee 			}
29123271Smaybee 		}
29133271Smaybee 
2914789Sahrens 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
2915789Sahrens 		zl->zl_rwlock = rwlp;
2916789Sahrens 		zl->zl_znode = NULL;
2917789Sahrens 		zl->zl_next = *zlpp;
2918789Sahrens 		*zlpp = zl;
2919789Sahrens 
2920789Sahrens 		if (*oidp == szp->z_id)		/* We're a descendant of szp */
2921789Sahrens 			return (EINVAL);
2922789Sahrens 
2923789Sahrens 		if (*oidp == rootid)		/* We've hit the top */
2924789Sahrens 			return (0);
2925789Sahrens 
2926789Sahrens 		if (rw == RW_READER) {		/* i.e. not the first pass */
2927789Sahrens 			int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
2928789Sahrens 			if (error)
2929789Sahrens 				return (error);
2930789Sahrens 			zl->zl_znode = zp;
2931789Sahrens 		}
2932789Sahrens 		oidp = &zp->z_phys->zp_parent;
2933789Sahrens 		rwlp = &zp->z_parent_lock;
2934789Sahrens 		rw = RW_READER;
2935789Sahrens 
2936789Sahrens 	} while (zp->z_id != sdzp->z_id);
2937789Sahrens 
2938789Sahrens 	return (0);
2939789Sahrens }
2940789Sahrens 
2941789Sahrens /*
2942789Sahrens  * Move an entry from the provided source directory to the target
2943789Sahrens  * directory.  Change the entry name as indicated.
2944789Sahrens  *
2945789Sahrens  *	IN:	sdvp	- Source directory containing the "old entry".
2946789Sahrens  *		snm	- Old entry name.
2947789Sahrens  *		tdvp	- Target directory to contain the "new entry".
2948789Sahrens  *		tnm	- New entry name.
2949789Sahrens  *		cr	- credentials of caller.
29505331Samw  *		ct	- caller context
29515331Samw  *		flags	- case flags
2952789Sahrens  *
2953789Sahrens  *	RETURN:	0 if success
2954789Sahrens  *		error code if failure
2955789Sahrens  *
2956789Sahrens  * Timestamps:
2957789Sahrens  *	sdvp,tdvp - ctime|mtime updated
2958789Sahrens  */
29595331Samw /*ARGSUSED*/
2960789Sahrens static int
29615331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
29625331Samw     caller_context_t *ct, int flags)
2963789Sahrens {
2964789Sahrens 	znode_t		*tdzp, *szp, *tzp;
2965789Sahrens 	znode_t		*sdzp = VTOZ(sdvp);
2966789Sahrens 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
29675326Sek110237 	zilog_t		*zilog;
2968789Sahrens 	vnode_t		*realvp;
2969789Sahrens 	zfs_dirlock_t	*sdl, *tdl;
2970789Sahrens 	dmu_tx_t	*tx;
2971789Sahrens 	zfs_zlock_t	*zl;
29725331Samw 	int		cmp, serr, terr;
29735331Samw 	int		error = 0;
29745331Samw 	int		zflg = 0;
2975789Sahrens 
29765367Sahrens 	ZFS_ENTER(zfsvfs);
29775367Sahrens 	ZFS_VERIFY_ZP(sdzp);
29785326Sek110237 	zilog = zfsvfs->z_log;
2979789Sahrens 
2980789Sahrens 	/*
2981789Sahrens 	 * Make sure we have the real vp for the target directory.
2982789Sahrens 	 */
29835331Samw 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
2984789Sahrens 		tdvp = realvp;
2985789Sahrens 
2986789Sahrens 	if (tdvp->v_vfsp != sdvp->v_vfsp) {
2987789Sahrens 		ZFS_EXIT(zfsvfs);
2988789Sahrens 		return (EXDEV);
2989789Sahrens 	}
2990789Sahrens 
2991789Sahrens 	tdzp = VTOZ(tdvp);
29925367Sahrens 	ZFS_VERIFY_ZP(tdzp);
29935498Stimh 	if (zfsvfs->z_utf8 && u8_validate(tnm,
29945331Samw 	    strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
29955331Samw 		ZFS_EXIT(zfsvfs);
29965331Samw 		return (EILSEQ);
29975331Samw 	}
29985331Samw 
29995331Samw 	if (flags & FIGNORECASE)
30005331Samw 		zflg |= ZCILOOK;
30015331Samw 
3002789Sahrens top:
3003789Sahrens 	szp = NULL;
3004789Sahrens 	tzp = NULL;
3005789Sahrens 	zl = NULL;
3006789Sahrens 
3007789Sahrens 	/*
3008789Sahrens 	 * This is to prevent the creation of links into attribute space
3009789Sahrens 	 * by renaming a linked file into/outof an attribute directory.
3010789Sahrens 	 * See the comment in zfs_link() for why this is considered bad.
3011789Sahrens 	 */
3012789Sahrens 	if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
3013789Sahrens 	    (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
3014789Sahrens 		ZFS_EXIT(zfsvfs);
3015789Sahrens 		return (EINVAL);
3016789Sahrens 	}
3017789Sahrens 
3018789Sahrens 	/*
3019789Sahrens 	 * Lock source and target directory entries.  To prevent deadlock,
3020789Sahrens 	 * a lock ordering must be defined.  We lock the directory with
3021789Sahrens 	 * the smallest object id first, or if it's a tie, the one with
3022789Sahrens 	 * the lexically first name.
3023789Sahrens 	 */
3024789Sahrens 	if (sdzp->z_id < tdzp->z_id) {
3025789Sahrens 		cmp = -1;
3026789Sahrens 	} else if (sdzp->z_id > tdzp->z_id) {
3027789Sahrens 		cmp = 1;
3028789Sahrens 	} else {
30295331Samw 		/*
30305331Samw 		 * First compare the two name arguments without
30315331Samw 		 * considering any case folding.
30325331Samw 		 */
30335331Samw 		int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
30345331Samw 
30355331Samw 		cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
30365498Stimh 		ASSERT(error == 0 || !zfsvfs->z_utf8);
3037789Sahrens 		if (cmp == 0) {
3038789Sahrens 			/*
3039789Sahrens 			 * POSIX: "If the old argument and the new argument
3040789Sahrens 			 * both refer to links to the same existing file,
3041789Sahrens 			 * the rename() function shall return successfully
3042789Sahrens 			 * and perform no other action."
3043789Sahrens 			 */
3044789Sahrens 			ZFS_EXIT(zfsvfs);
3045789Sahrens 			return (0);
3046789Sahrens 		}
30475331Samw 		/*
30485331Samw 		 * If the file system is case-folding, then we may
30495331Samw 		 * have some more checking to do.  A case-folding file
30505331Samw 		 * system is either supporting mixed case sensitivity
30515331Samw 		 * access or is completely case-insensitive.  Note
30525331Samw 		 * that the file system is always case preserving.
30535331Samw 		 *
30545331Samw 		 * In mixed sensitivity mode case sensitive behavior
30555331Samw 		 * is the default.  FIGNORECASE must be used to
30565331Samw 		 * explicitly request case insensitive behavior.
30575331Samw 		 *
30585331Samw 		 * If the source and target names provided differ only
30595331Samw 		 * by case (e.g., a request to rename 'tim' to 'Tim'),
30605331Samw 		 * we will treat this as a special case in the
30615331Samw 		 * case-insensitive mode: as long as the source name
30625331Samw 		 * is an exact match, we will allow this to proceed as
30635331Samw 		 * a name-change request.
30645331Samw 		 */
30655498Stimh 		if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
30665498Stimh 		    (zfsvfs->z_case == ZFS_CASE_MIXED &&
30675498Stimh 		    flags & FIGNORECASE)) &&
30685331Samw 		    u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
30695331Samw 		    &error) == 0) {
30705331Samw 			/*
30715331Samw 			 * case preserving rename request, require exact
30725331Samw 			 * name matches
30735331Samw 			 */
30745331Samw 			zflg |= ZCIEXACT;
30755331Samw 			zflg &= ~ZCILOOK;
30765331Samw 		}
3077789Sahrens 	}
30785331Samw 
3079789Sahrens 	if (cmp < 0) {
30805331Samw 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
30815331Samw 		    ZEXISTS | zflg, NULL, NULL);
30825331Samw 		terr = zfs_dirent_lock(&tdl,
30835331Samw 		    tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3084789Sahrens 	} else {
30855331Samw 		terr = zfs_dirent_lock(&tdl,
30865331Samw 		    tdzp, tnm, &tzp, zflg, NULL, NULL);
30875331Samw 		serr = zfs_dirent_lock(&sdl,
30885331Samw 		    sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
30895331Samw 		    NULL, NULL);
3090789Sahrens 	}
3091789Sahrens 
3092789Sahrens 	if (serr) {
3093789Sahrens 		/*
3094789Sahrens 		 * Source entry invalid or not there.
3095789Sahrens 		 */
3096789Sahrens 		if (!terr) {
3097789Sahrens 			zfs_dirent_unlock(tdl);
3098789Sahrens 			if (tzp)
3099789Sahrens 				VN_RELE(ZTOV(tzp));
3100789Sahrens 		}
3101789Sahrens 		if (strcmp(snm, "..") == 0)
3102789Sahrens 			serr = EINVAL;
3103789Sahrens 		ZFS_EXIT(zfsvfs);
3104789Sahrens 		return (serr);
3105789Sahrens 	}
3106789Sahrens 	if (terr) {
3107789Sahrens 		zfs_dirent_unlock(sdl);
3108789Sahrens 		VN_RELE(ZTOV(szp));
3109789Sahrens 		if (strcmp(tnm, "..") == 0)
3110789Sahrens 			terr = EINVAL;
3111789Sahrens 		ZFS_EXIT(zfsvfs);
3112789Sahrens 		return (terr);
3113789Sahrens 	}
3114789Sahrens 
3115789Sahrens 	/*
3116789Sahrens 	 * Must have write access at the source to remove the old entry
3117789Sahrens 	 * and write access at the target to create the new entry.
3118789Sahrens 	 * Note that if target and source are the same, this can be
3119789Sahrens 	 * done in a single check.
3120789Sahrens 	 */
3121789Sahrens 
3122789Sahrens 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3123789Sahrens 		goto out;
3124789Sahrens 
3125789Sahrens 	if (ZTOV(szp)->v_type == VDIR) {
3126789Sahrens 		/*
3127789Sahrens 		 * Check to make sure rename is valid.
3128789Sahrens 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3129789Sahrens 		 */
3130789Sahrens 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3131789Sahrens 			goto out;
3132789Sahrens 	}
3133789Sahrens 
3134789Sahrens 	/*
3135789Sahrens 	 * Does target exist?
3136789Sahrens 	 */
3137789Sahrens 	if (tzp) {
3138789Sahrens 		/*
3139789Sahrens 		 * Source and target must be the same type.
3140789Sahrens 		 */
3141789Sahrens 		if (ZTOV(szp)->v_type == VDIR) {
3142789Sahrens 			if (ZTOV(tzp)->v_type != VDIR) {
3143789Sahrens 				error = ENOTDIR;
3144789Sahrens 				goto out;
3145789Sahrens 			}
3146789Sahrens 		} else {
3147789Sahrens 			if (ZTOV(tzp)->v_type == VDIR) {
3148789Sahrens 				error = EISDIR;
3149789Sahrens 				goto out;
3150789Sahrens 			}
3151789Sahrens 		}
3152789Sahrens 		/*
3153789Sahrens 		 * POSIX dictates that when the source and target
3154789Sahrens 		 * entries refer to the same file object, rename
3155789Sahrens 		 * must do nothing and exit without error.
3156789Sahrens 		 */
3157789Sahrens 		if (szp->z_id == tzp->z_id) {
3158789Sahrens 			error = 0;
3159789Sahrens 			goto out;
3160789Sahrens 		}
3161789Sahrens 	}
3162789Sahrens 
31635331Samw 	vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3164789Sahrens 	if (tzp)
31655331Samw 		vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
31664863Spraks 
31674863Spraks 	/*
31684863Spraks 	 * notify the target directory if it is not the same
31694863Spraks 	 * as source directory.
31704863Spraks 	 */
31714863Spraks 	if (tdvp != sdvp) {
31725331Samw 		vnevent_rename_dest_dir(tdvp, ct);
31734863Spraks 	}
3174789Sahrens 
3175789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3176789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);	/* nlink changes */
3177789Sahrens 	dmu_tx_hold_bonus(tx, sdzp->z_id);	/* nlink changes */
31781544Seschrock 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
31791544Seschrock 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
31801544Seschrock 	if (sdzp != tdzp)
3181789Sahrens 		dmu_tx_hold_bonus(tx, tdzp->z_id);	/* nlink changes */
31821544Seschrock 	if (tzp)
31831544Seschrock 		dmu_tx_hold_bonus(tx, tzp->z_id);	/* parent changes */
31843461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3185789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
3186789Sahrens 	if (error) {
3187789Sahrens 		if (zl != NULL)
3188789Sahrens 			zfs_rename_unlock(&zl);
3189789Sahrens 		zfs_dirent_unlock(sdl);
3190789Sahrens 		zfs_dirent_unlock(tdl);
3191789Sahrens 		VN_RELE(ZTOV(szp));
3192789Sahrens 		if (tzp)
3193789Sahrens 			VN_RELE(ZTOV(tzp));
3194789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
31952113Sahrens 			dmu_tx_wait(tx);
31962113Sahrens 			dmu_tx_abort(tx);
3197789Sahrens 			goto top;
3198789Sahrens 		}
31992113Sahrens 		dmu_tx_abort(tx);
3200789Sahrens 		ZFS_EXIT(zfsvfs);
3201789Sahrens 		return (error);
3202789Sahrens 	}
3203789Sahrens 
3204789Sahrens 	if (tzp)	/* Attempt to remove the existing target */
32055331Samw 		error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3206789Sahrens 
3207789Sahrens 	if (error == 0) {
3208789Sahrens 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3209789Sahrens 		if (error == 0) {
32105331Samw 			szp->z_phys->zp_flags |= ZFS_AV_MODIFIED;
32115331Samw 
3212789Sahrens 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3213789Sahrens 			ASSERT(error == 0);
32145331Samw 
32155331Samw 			zfs_log_rename(zilog, tx,
32165331Samw 			    TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0),
32175331Samw 			    sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
32186976Seschrock 
32196976Seschrock 			/* Update path information for the target vnode */
32206976Seschrock 			vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm));
3221789Sahrens 		}
3222789Sahrens 	}
3223789Sahrens 
3224789Sahrens 	dmu_tx_commit(tx);
3225789Sahrens out:
3226789Sahrens 	if (zl != NULL)
3227789Sahrens 		zfs_rename_unlock(&zl);
3228789Sahrens 
3229789Sahrens 	zfs_dirent_unlock(sdl);
3230789Sahrens 	zfs_dirent_unlock(tdl);
3231789Sahrens 
3232789Sahrens 	VN_RELE(ZTOV(szp));
3233789Sahrens 	if (tzp)
3234789Sahrens 		VN_RELE(ZTOV(tzp));
3235789Sahrens 
3236789Sahrens 	ZFS_EXIT(zfsvfs);
3237789Sahrens 	return (error);
3238789Sahrens }
3239789Sahrens 
3240789Sahrens /*
3241789Sahrens  * Insert the indicated symbolic reference entry into the directory.
3242789Sahrens  *
3243789Sahrens  *	IN:	dvp	- Directory to contain new symbolic link.
3244789Sahrens  *		link	- Name for new symlink entry.
3245789Sahrens  *		vap	- Attributes of new entry.
3246789Sahrens  *		target	- Target path of new symlink.
3247789Sahrens  *		cr	- credentials of caller.
32485331Samw  *		ct	- caller context
32495331Samw  *		flags	- case flags
3250789Sahrens  *
3251789Sahrens  *	RETURN:	0 if success
3252789Sahrens  *		error code if failure
3253789Sahrens  *
3254789Sahrens  * Timestamps:
3255789Sahrens  *	dvp - ctime|mtime updated
3256789Sahrens  */
32575331Samw /*ARGSUSED*/
3258789Sahrens static int
32595331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
32605331Samw     caller_context_t *ct, int flags)
3261789Sahrens {
3262789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
3263789Sahrens 	zfs_dirlock_t	*dl;
3264789Sahrens 	dmu_tx_t	*tx;
3265789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
32665326Sek110237 	zilog_t		*zilog;
3267789Sahrens 	int		len = strlen(link);
3268789Sahrens 	int		error;
32695331Samw 	int		zflg = ZNEW;
32705331Samw 	zfs_fuid_info_t *fuidp = NULL;
3271789Sahrens 
3272789Sahrens 	ASSERT(vap->va_type == VLNK);
3273789Sahrens 
32745367Sahrens 	ZFS_ENTER(zfsvfs);
32755367Sahrens 	ZFS_VERIFY_ZP(dzp);
32765326Sek110237 	zilog = zfsvfs->z_log;
32775331Samw 
32785498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
32795331Samw 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
32805331Samw 		ZFS_EXIT(zfsvfs);
32815331Samw 		return (EILSEQ);
32825331Samw 	}
32835331Samw 	if (flags & FIGNORECASE)
32845331Samw 		zflg |= ZCILOOK;
3285789Sahrens top:
32865331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3287789Sahrens 		ZFS_EXIT(zfsvfs);
3288789Sahrens 		return (error);
3289789Sahrens 	}
3290789Sahrens 
3291789Sahrens 	if (len > MAXPATHLEN) {
3292789Sahrens 		ZFS_EXIT(zfsvfs);
3293789Sahrens 		return (ENAMETOOLONG);
3294789Sahrens 	}
3295789Sahrens 
3296789Sahrens 	/*
3297789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3298789Sahrens 	 */
32995331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
33005331Samw 	if (error) {
3301789Sahrens 		ZFS_EXIT(zfsvfs);
3302789Sahrens 		return (error);
3303789Sahrens 	}
3304789Sahrens 
3305789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3306789Sahrens 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3307789Sahrens 	dmu_tx_hold_bonus(tx, dzp->z_id);
33081544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3309789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
3310789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
33115824Smarks 	if (IS_EPHEMERAL(crgetuid(cr)) || IS_EPHEMERAL(crgetgid(cr))) {
33125824Smarks 		if (zfsvfs->z_fuid_obj == 0) {
33135824Smarks 			dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
33145824Smarks 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
33155824Smarks 			    FUID_SIZE_ESTIMATE(zfsvfs));
33165824Smarks 			dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
33175824Smarks 		} else {
33185824Smarks 			dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
33195824Smarks 			dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
33205824Smarks 			    FUID_SIZE_ESTIMATE(zfsvfs));
33215824Smarks 		}
33225331Samw 	}
3323789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
3324789Sahrens 	if (error) {
3325789Sahrens 		zfs_dirent_unlock(dl);
3326789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
33272113Sahrens 			dmu_tx_wait(tx);
33282113Sahrens 			dmu_tx_abort(tx);
3329789Sahrens 			goto top;
3330789Sahrens 		}
33312113Sahrens 		dmu_tx_abort(tx);
3332789Sahrens 		ZFS_EXIT(zfsvfs);
3333789Sahrens 		return (error);
3334789Sahrens 	}
3335789Sahrens 
3336789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
3337789Sahrens 
3338789Sahrens 	/*
3339789Sahrens 	 * Create a new object for the symlink.
3340789Sahrens 	 * Put the link content into bonus buffer if it will fit;
3341789Sahrens 	 * otherwise, store it just like any other file data.
3342789Sahrens 	 */
3343789Sahrens 	if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
33445446Sahrens 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, len, NULL, &fuidp);
3345789Sahrens 		if (len != 0)
3346789Sahrens 			bcopy(link, zp->z_phys + 1, len);
3347789Sahrens 	} else {
3348789Sahrens 		dmu_buf_t *dbp;
33491669Sperrin 
33505446Sahrens 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, NULL, &fuidp);
33511669Sperrin 		/*
33521669Sperrin 		 * Nothing can access the znode yet so no locking needed
33531669Sperrin 		 * for growing the znode's blocksize.
33541669Sperrin 		 */
33551669Sperrin 		zfs_grow_blocksize(zp, len, tx);
3356789Sahrens 
33575446Sahrens 		VERIFY(0 == dmu_buf_hold(zfsvfs->z_os,
33585446Sahrens 		    zp->z_id, 0, FTAG, &dbp));
3359789Sahrens 		dmu_buf_will_dirty(dbp, tx);
3360789Sahrens 
3361789Sahrens 		ASSERT3U(len, <=, dbp->db_size);
3362789Sahrens 		bcopy(link, dbp->db_data, len);
33631544Seschrock 		dmu_buf_rele(dbp, FTAG);
3364789Sahrens 	}
3365789Sahrens 	zp->z_phys->zp_size = len;
3366789Sahrens 
3367789Sahrens 	/*
3368789Sahrens 	 * Insert the new object into the directory.
3369789Sahrens 	 */
3370789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
3371789Sahrens out:
33725331Samw 	if (error == 0) {
33735331Samw 		uint64_t txtype = TX_SYMLINK;
33745331Samw 		if (flags & FIGNORECASE)
33755331Samw 			txtype |= TX_CI;
33765331Samw 		zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
33775331Samw 	}
33785331Samw 	if (fuidp)
33795331Samw 		zfs_fuid_info_free(fuidp);
3380789Sahrens 
3381789Sahrens 	dmu_tx_commit(tx);
3382789Sahrens 
3383789Sahrens 	zfs_dirent_unlock(dl);
3384789Sahrens 
3385789Sahrens 	VN_RELE(ZTOV(zp));
3386789Sahrens 
3387789Sahrens 	ZFS_EXIT(zfsvfs);
3388789Sahrens 	return (error);
3389789Sahrens }
3390789Sahrens 
3391789Sahrens /*
3392789Sahrens  * Return, in the buffer contained in the provided uio structure,
3393789Sahrens  * the symbolic path referred to by vp.
3394789Sahrens  *
3395789Sahrens  *	IN:	vp	- vnode of symbolic link.
3396789Sahrens  *		uoip	- structure to contain the link path.
3397789Sahrens  *		cr	- credentials of caller.
33985331Samw  *		ct	- caller context
3399789Sahrens  *
3400789Sahrens  *	OUT:	uio	- structure to contain the link path.
3401789Sahrens  *
3402789Sahrens  *	RETURN:	0 if success
3403789Sahrens  *		error code if failure
3404789Sahrens  *
3405789Sahrens  * Timestamps:
3406789Sahrens  *	vp - atime updated
3407789Sahrens  */
3408789Sahrens /* ARGSUSED */
3409789Sahrens static int
34105331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3411789Sahrens {
3412789Sahrens 	znode_t		*zp = VTOZ(vp);
3413789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3414789Sahrens 	size_t		bufsz;
3415789Sahrens 	int		error;
3416789Sahrens 
34175367Sahrens 	ZFS_ENTER(zfsvfs);
34185367Sahrens 	ZFS_VERIFY_ZP(zp);
3419789Sahrens 
3420789Sahrens 	bufsz = (size_t)zp->z_phys->zp_size;
3421789Sahrens 	if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
3422789Sahrens 		error = uiomove(zp->z_phys + 1,
3423789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
3424789Sahrens 	} else {
34251544Seschrock 		dmu_buf_t *dbp;
34261544Seschrock 		error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
34271544Seschrock 		if (error) {
3428789Sahrens 			ZFS_EXIT(zfsvfs);
3429789Sahrens 			return (error);
3430789Sahrens 		}
3431789Sahrens 		error = uiomove(dbp->db_data,
3432789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
34331544Seschrock 		dmu_buf_rele(dbp, FTAG);
3434789Sahrens 	}
3435789Sahrens 
3436789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3437789Sahrens 	ZFS_EXIT(zfsvfs);
3438789Sahrens 	return (error);
3439789Sahrens }
3440789Sahrens 
3441789Sahrens /*
3442789Sahrens  * Insert a new entry into directory tdvp referencing svp.
3443789Sahrens  *
3444789Sahrens  *	IN:	tdvp	- Directory to contain new entry.
3445789Sahrens  *		svp	- vnode of new entry.
3446789Sahrens  *		name	- name of new entry.
3447789Sahrens  *		cr	- credentials of caller.
34485331Samw  *		ct	- caller context
3449789Sahrens  *
3450789Sahrens  *	RETURN:	0 if success
3451789Sahrens  *		error code if failure
3452789Sahrens  *
3453789Sahrens  * Timestamps:
3454789Sahrens  *	tdvp - ctime|mtime updated
3455789Sahrens  *	 svp - ctime updated
3456789Sahrens  */
3457789Sahrens /* ARGSUSED */
3458789Sahrens static int
34595331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
34605331Samw     caller_context_t *ct, int flags)
3461789Sahrens {
3462789Sahrens 	znode_t		*dzp = VTOZ(tdvp);
3463789Sahrens 	znode_t		*tzp, *szp;
3464789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
34655326Sek110237 	zilog_t		*zilog;
3466789Sahrens 	zfs_dirlock_t	*dl;
3467789Sahrens 	dmu_tx_t	*tx;
3468789Sahrens 	vnode_t		*realvp;
3469789Sahrens 	int		error;
34705331Samw 	int		zf = ZNEW;
34715331Samw 	uid_t		owner;
3472789Sahrens 
3473789Sahrens 	ASSERT(tdvp->v_type == VDIR);
3474789Sahrens 
34755367Sahrens 	ZFS_ENTER(zfsvfs);
34765367Sahrens 	ZFS_VERIFY_ZP(dzp);
34775326Sek110237 	zilog = zfsvfs->z_log;
3478789Sahrens 
34795331Samw 	if (VOP_REALVP(svp, &realvp, ct) == 0)
3480789Sahrens 		svp = realvp;
3481789Sahrens 
3482789Sahrens 	if (svp->v_vfsp != tdvp->v_vfsp) {
3483789Sahrens 		ZFS_EXIT(zfsvfs);
3484789Sahrens 		return (EXDEV);
3485789Sahrens 	}
34865367Sahrens 	szp = VTOZ(svp);
34875367Sahrens 	ZFS_VERIFY_ZP(szp);
3488789Sahrens 
34895498Stimh 	if (zfsvfs->z_utf8 && u8_validate(name,
34905331Samw 	    strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
34915331Samw 		ZFS_EXIT(zfsvfs);
34925331Samw 		return (EILSEQ);
34935331Samw 	}
34945331Samw 	if (flags & FIGNORECASE)
34955331Samw 		zf |= ZCILOOK;
34965331Samw 
3497789Sahrens top:
3498789Sahrens 	/*
3499789Sahrens 	 * We do not support links between attributes and non-attributes
3500789Sahrens 	 * because of the potential security risk of creating links
3501789Sahrens 	 * into "normal" file space in order to circumvent restrictions
3502789Sahrens 	 * imposed in attribute space.
3503789Sahrens 	 */
3504789Sahrens 	if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
3505789Sahrens 	    (dzp->z_phys->zp_flags & ZFS_XATTR)) {
3506789Sahrens 		ZFS_EXIT(zfsvfs);
3507789Sahrens 		return (EINVAL);
3508789Sahrens 	}
3509789Sahrens 
3510789Sahrens 	/*
3511789Sahrens 	 * POSIX dictates that we return EPERM here.
3512789Sahrens 	 * Better choices include ENOTSUP or EISDIR.
3513789Sahrens 	 */
3514789Sahrens 	if (svp->v_type == VDIR) {
3515789Sahrens 		ZFS_EXIT(zfsvfs);
3516789Sahrens 		return (EPERM);
3517789Sahrens 	}
3518789Sahrens 
35195959Smarks 	owner = zfs_fuid_map_id(zfsvfs, szp->z_phys->zp_uid, cr, ZFS_OWNER);
35205331Samw 	if (owner != crgetuid(cr) &&
3521789Sahrens 	    secpolicy_basic_link(cr) != 0) {
3522789Sahrens 		ZFS_EXIT(zfsvfs);
3523789Sahrens 		return (EPERM);
3524789Sahrens 	}
3525789Sahrens 
35265331Samw 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3527789Sahrens 		ZFS_EXIT(zfsvfs);
3528789Sahrens 		return (error);
3529789Sahrens 	}
3530789Sahrens 
3531789Sahrens 	/*
3532789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
3533789Sahrens 	 */
35345331Samw 	error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
35355331Samw 	if (error) {
3536789Sahrens 		ZFS_EXIT(zfsvfs);
3537789Sahrens 		return (error);
3538789Sahrens 	}
3539789Sahrens 
3540789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3541789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);
35421544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3543789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
3544789Sahrens 	if (error) {
3545789Sahrens 		zfs_dirent_unlock(dl);
3546789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
35472113Sahrens 			dmu_tx_wait(tx);
35482113Sahrens 			dmu_tx_abort(tx);
3549789Sahrens 			goto top;
3550789Sahrens 		}
35512113Sahrens 		dmu_tx_abort(tx);
3552789Sahrens 		ZFS_EXIT(zfsvfs);
3553789Sahrens 		return (error);
3554789Sahrens 	}
3555789Sahrens 
3556789Sahrens 	error = zfs_link_create(dl, szp, tx, 0);
3557789Sahrens 
35585331Samw 	if (error == 0) {
35595331Samw 		uint64_t txtype = TX_LINK;
35605331Samw 		if (flags & FIGNORECASE)
35615331Samw 			txtype |= TX_CI;
35625331Samw 		zfs_log_link(zilog, tx, txtype, dzp, szp, name);
35635331Samw 	}
3564789Sahrens 
3565789Sahrens 	dmu_tx_commit(tx);
3566789Sahrens 
3567789Sahrens 	zfs_dirent_unlock(dl);
3568789Sahrens 
35694863Spraks 	if (error == 0) {
35705331Samw 		vnevent_link(svp, ct);
35714863Spraks 	}
35724863Spraks 
3573789Sahrens 	ZFS_EXIT(zfsvfs);
3574789Sahrens 	return (error);
3575789Sahrens }
3576789Sahrens 
3577789Sahrens /*
3578789Sahrens  * zfs_null_putapage() is used when the file system has been force
3579789Sahrens  * unmounted. It just drops the pages.
3580789Sahrens  */
3581789Sahrens /* ARGSUSED */
3582789Sahrens static int
3583789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3584789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3585789Sahrens {
3586789Sahrens 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
3587789Sahrens 	return (0);
3588789Sahrens }
3589789Sahrens 
35902688Smaybee /*
35912688Smaybee  * Push a page out to disk, klustering if possible.
35922688Smaybee  *
35932688Smaybee  *	IN:	vp	- file to push page to.
35942688Smaybee  *		pp	- page to push.
35952688Smaybee  *		flags	- additional flags.
35962688Smaybee  *		cr	- credentials of caller.
35972688Smaybee  *
35982688Smaybee  *	OUT:	offp	- start of range pushed.
35992688Smaybee  *		lenp	- len of range pushed.
36002688Smaybee  *
36012688Smaybee  *	RETURN:	0 if success
36022688Smaybee  *		error code if failure
36032688Smaybee  *
36042688Smaybee  * NOTE: callers must have locked the page to be pushed.  On
36052688Smaybee  * exit, the page (and all other pages in the kluster) must be
36062688Smaybee  * unlocked.
36072688Smaybee  */
3608789Sahrens /* ARGSUSED */
3609789Sahrens static int
3610789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3611789Sahrens 		size_t *lenp, int flags, cred_t *cr)
3612789Sahrens {
3613789Sahrens 	znode_t		*zp = VTOZ(vp);
3614789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3615789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
3616789Sahrens 	dmu_tx_t	*tx;
36171669Sperrin 	rl_t		*rl;
36182688Smaybee 	u_offset_t	off, koff;
36192688Smaybee 	size_t		len, klen;
36204709Smaybee 	uint64_t	filesz;
3621789Sahrens 	int		err;
3622789Sahrens 
36234709Smaybee 	filesz = zp->z_phys->zp_size;
36242688Smaybee 	off = pp->p_offset;
36252688Smaybee 	len = PAGESIZE;
36262688Smaybee 	/*
36272688Smaybee 	 * If our blocksize is bigger than the page size, try to kluster
36282688Smaybee 	 * muiltiple pages so that we write a full block (thus avoiding
36292688Smaybee 	 * a read-modify-write).
36302688Smaybee 	 */
36314709Smaybee 	if (off < filesz && zp->z_blksz > PAGESIZE) {
36322688Smaybee 		if (!ISP2(zp->z_blksz)) {
36332688Smaybee 			/* Only one block in the file. */
36342688Smaybee 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
36352688Smaybee 			koff = 0;
36362688Smaybee 		} else {
36372688Smaybee 			klen = zp->z_blksz;
36382688Smaybee 			koff = P2ALIGN(off, (u_offset_t)klen);
36392688Smaybee 		}
36402688Smaybee 		ASSERT(koff <= filesz);
36412688Smaybee 		if (koff + klen > filesz)
36422688Smaybee 			klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE);
36432688Smaybee 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
36442688Smaybee 	}
36452688Smaybee 	ASSERT3U(btop(len), ==, btopr(len));
3646789Sahrens top:
36472688Smaybee 	rl = zfs_range_lock(zp, off, len, RL_WRITER);
36481819Smaybee 	/*
36491819Smaybee 	 * Can't push pages past end-of-file.
36501819Smaybee 	 */
36514709Smaybee 	filesz = zp->z_phys->zp_size;
36524709Smaybee 	if (off >= filesz) {
36534709Smaybee 		/* ignore all pages */
36542688Smaybee 		err = 0;
36552688Smaybee 		goto out;
36564709Smaybee 	} else if (off + len > filesz) {
36574709Smaybee 		int npages = btopr(filesz - off);
36582688Smaybee 		page_t *trunc;
36592688Smaybee 
36602688Smaybee 		page_list_break(&pp, &trunc, npages);
36614709Smaybee 		/* ignore pages past end of file */
36622688Smaybee 		if (trunc)
36634709Smaybee 			pvn_write_done(trunc, flags);
36644709Smaybee 		len = filesz - off;
36651819Smaybee 	}
3666789Sahrens 
3667789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
3668789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, off, len);
3669789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
3670789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
3671789Sahrens 	if (err != 0) {
3672789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
36732688Smaybee 			zfs_range_unlock(rl);
36742113Sahrens 			dmu_tx_wait(tx);
36752113Sahrens 			dmu_tx_abort(tx);
36762688Smaybee 			err = 0;
3677789Sahrens 			goto top;
3678789Sahrens 		}
36792113Sahrens 		dmu_tx_abort(tx);
3680789Sahrens 		goto out;
3681789Sahrens 	}
3682789Sahrens 
36832688Smaybee 	if (zp->z_blksz <= PAGESIZE) {
36847315SJonathan.Adams@Sun.COM 		caddr_t va = zfs_map_page(pp, S_READ);
36852688Smaybee 		ASSERT3U(len, <=, PAGESIZE);
36862688Smaybee 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
36877315SJonathan.Adams@Sun.COM 		zfs_unmap_page(pp, va);
36882688Smaybee 	} else {
36892688Smaybee 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
36902688Smaybee 	}
36912688Smaybee 
36922688Smaybee 	if (err == 0) {
36932688Smaybee 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
36943638Sbillm 		zfs_log_write(zilog, tx, TX_WRITE, zp, off, len, 0);
36952688Smaybee 		dmu_tx_commit(tx);
36962688Smaybee 	}
36972688Smaybee 
36982688Smaybee out:
36992237Smaybee 	zfs_range_unlock(rl);
37004709Smaybee 	pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
3701789Sahrens 	if (offp)
3702789Sahrens 		*offp = off;
3703789Sahrens 	if (lenp)
3704789Sahrens 		*lenp = len;
3705789Sahrens 
3706789Sahrens 	return (err);
3707789Sahrens }
3708789Sahrens 
3709789Sahrens /*
3710789Sahrens  * Copy the portion of the file indicated from pages into the file.
3711789Sahrens  * The pages are stored in a page list attached to the files vnode.
3712789Sahrens  *
3713789Sahrens  *	IN:	vp	- vnode of file to push page data to.
3714789Sahrens  *		off	- position in file to put data.
3715789Sahrens  *		len	- amount of data to write.
3716789Sahrens  *		flags	- flags to control the operation.
3717789Sahrens  *		cr	- credentials of caller.
37185331Samw  *		ct	- caller context.
3719789Sahrens  *
3720789Sahrens  *	RETURN:	0 if success
3721789Sahrens  *		error code if failure
3722789Sahrens  *
3723789Sahrens  * Timestamps:
3724789Sahrens  *	vp - ctime|mtime updated
3725789Sahrens  */
37265331Samw /*ARGSUSED*/
3727789Sahrens static int
37285331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
37295331Samw     caller_context_t *ct)
3730789Sahrens {
3731789Sahrens 	znode_t		*zp = VTOZ(vp);
3732789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3733789Sahrens 	page_t		*pp;
3734789Sahrens 	size_t		io_len;
3735789Sahrens 	u_offset_t	io_off;
37361669Sperrin 	uint64_t	filesz;
3737789Sahrens 	int		error = 0;
3738789Sahrens 
37395367Sahrens 	ZFS_ENTER(zfsvfs);
37405367Sahrens 	ZFS_VERIFY_ZP(zp);
3741789Sahrens 
3742789Sahrens 	if (len == 0) {
3743789Sahrens 		/*
3744789Sahrens 		 * Search the entire vp list for pages >= off.
3745789Sahrens 		 */
3746789Sahrens 		error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage,
3747789Sahrens 		    flags, cr);
37481472Sperrin 		goto out;
3749789Sahrens 	}
3750789Sahrens 
37511669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
37521669Sperrin 	if (off > filesz) {
3753789Sahrens 		/* past end of file */
3754789Sahrens 		ZFS_EXIT(zfsvfs);
3755789Sahrens 		return (0);
3756789Sahrens 	}
3757789Sahrens 
37581669Sperrin 	len = MIN(len, filesz - off);
3759789Sahrens 
37601472Sperrin 	for (io_off = off; io_off < off + len; io_off += io_len) {
3761789Sahrens 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
37621669Sperrin 			pp = page_lookup(vp, io_off,
37634339Sperrin 			    (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
3764789Sahrens 		} else {
3765789Sahrens 			pp = page_lookup_nowait(vp, io_off,
37664339Sperrin 			    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
3767789Sahrens 		}
3768789Sahrens 
3769789Sahrens 		if (pp != NULL && pvn_getdirty(pp, flags)) {
3770789Sahrens 			int err;
3771789Sahrens 
3772789Sahrens 			/*
3773789Sahrens 			 * Found a dirty page to push
3774789Sahrens 			 */
37751669Sperrin 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
37761669Sperrin 			if (err)
3777789Sahrens 				error = err;
3778789Sahrens 		} else {
3779789Sahrens 			io_len = PAGESIZE;
3780789Sahrens 		}
3781789Sahrens 	}
37821472Sperrin out:
37832638Sperrin 	if ((flags & B_ASYNC) == 0)
37842638Sperrin 		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
3785789Sahrens 	ZFS_EXIT(zfsvfs);
3786789Sahrens 	return (error);
3787789Sahrens }
3788789Sahrens 
37895331Samw /*ARGSUSED*/
3790789Sahrens void
37915331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
3792789Sahrens {
3793789Sahrens 	znode_t	*zp = VTOZ(vp);
3794789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3795789Sahrens 	int error;
3796789Sahrens 
37975326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
37985446Sahrens 	if (zp->z_dbuf == NULL) {
37995446Sahrens 		/*
38005642Smaybee 		 * The fs has been unmounted, or we did a
38015642Smaybee 		 * suspend/resume and this file no longer exists.
38025446Sahrens 		 */
3803789Sahrens 		if (vn_has_cached_data(vp)) {
3804789Sahrens 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
3805789Sahrens 			    B_INVAL, cr);
3806789Sahrens 		}
3807789Sahrens 
38081544Seschrock 		mutex_enter(&zp->z_lock);
3809789Sahrens 		vp->v_count = 0; /* count arrives as 1 */
38105446Sahrens 		mutex_exit(&zp->z_lock);
38115642Smaybee 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
38125446Sahrens 		zfs_znode_free(zp);
3813789Sahrens 		return;
3814789Sahrens 	}
3815789Sahrens 
3816789Sahrens 	/*
3817789Sahrens 	 * Attempt to push any data in the page cache.  If this fails
3818789Sahrens 	 * we will get kicked out later in zfs_zinactive().
3819789Sahrens 	 */
38201298Sperrin 	if (vn_has_cached_data(vp)) {
38211298Sperrin 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
38221298Sperrin 		    cr);
38231298Sperrin 	}
3824789Sahrens 
38253461Sahrens 	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3826789Sahrens 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3827789Sahrens 
3828789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
3829789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
3830789Sahrens 		if (error) {
3831789Sahrens 			dmu_tx_abort(tx);
3832789Sahrens 		} else {
3833789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
3834789Sahrens 			mutex_enter(&zp->z_lock);
3835789Sahrens 			zp->z_atime_dirty = 0;
3836789Sahrens 			mutex_exit(&zp->z_lock);
3837789Sahrens 			dmu_tx_commit(tx);
3838789Sahrens 		}
3839789Sahrens 	}
3840789Sahrens 
3841789Sahrens 	zfs_zinactive(zp);
38425326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
3843789Sahrens }
3844789Sahrens 
3845789Sahrens /*
3846789Sahrens  * Bounds-check the seek operation.
3847789Sahrens  *
3848789Sahrens  *	IN:	vp	- vnode seeking within
3849789Sahrens  *		ooff	- old file offset
3850789Sahrens  *		noffp	- pointer to new file offset
38515331Samw  *		ct	- caller context
3852789Sahrens  *
3853789Sahrens  *	RETURN:	0 if success
3854789Sahrens  *		EINVAL if new offset invalid
3855789Sahrens  */
3856789Sahrens /* ARGSUSED */
3857789Sahrens static int
38585331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
38595331Samw     caller_context_t *ct)
3860789Sahrens {
3861789Sahrens 	if (vp->v_type == VDIR)
3862789Sahrens 		return (0);
3863789Sahrens 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
3864789Sahrens }
3865789Sahrens 
3866789Sahrens /*
3867789Sahrens  * Pre-filter the generic locking function to trap attempts to place
3868789Sahrens  * a mandatory lock on a memory mapped file.
3869789Sahrens  */
3870789Sahrens static int
3871789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
38725331Samw     flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
3873789Sahrens {
3874789Sahrens 	znode_t *zp = VTOZ(vp);
3875789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3876789Sahrens 	int error;
3877789Sahrens 
38785367Sahrens 	ZFS_ENTER(zfsvfs);
38795367Sahrens 	ZFS_VERIFY_ZP(zp);
3880789Sahrens 
3881789Sahrens 	/*
38821544Seschrock 	 * We are following the UFS semantics with respect to mapcnt
38831544Seschrock 	 * here: If we see that the file is mapped already, then we will
38841544Seschrock 	 * return an error, but we don't worry about races between this
38851544Seschrock 	 * function and zfs_map().
3886789Sahrens 	 */
38871544Seschrock 	if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) {
3888789Sahrens 		ZFS_EXIT(zfsvfs);
3889789Sahrens 		return (EAGAIN);
3890789Sahrens 	}
38915331Samw 	error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct);
3892789Sahrens 	ZFS_EXIT(zfsvfs);
3893789Sahrens 	return (error);
3894789Sahrens }
3895789Sahrens 
3896789Sahrens /*
3897789Sahrens  * If we can't find a page in the cache, we will create a new page
3898789Sahrens  * and fill it with file data.  For efficiency, we may try to fill
38991669Sperrin  * multiple pages at once (klustering).
3900789Sahrens  */
3901789Sahrens static int
3902789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
3903789Sahrens     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
3904789Sahrens {
3905789Sahrens 	znode_t *zp = VTOZ(vp);
3906789Sahrens 	page_t *pp, *cur_pp;
3907789Sahrens 	objset_t *os = zp->z_zfsvfs->z_os;
3908789Sahrens 	caddr_t va;
3909789Sahrens 	u_offset_t io_off, total;
3910789Sahrens 	uint64_t oid = zp->z_id;
3911789Sahrens 	size_t io_len;
39121669Sperrin 	uint64_t filesz;
3913789Sahrens 	int err;
3914789Sahrens 
3915789Sahrens 	/*
3916789Sahrens 	 * If we are only asking for a single page don't bother klustering.
3917789Sahrens 	 */
39181669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
39192688Smaybee 	if (off >= filesz)
39202688Smaybee 		return (EFAULT);
39212688Smaybee 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
3922789Sahrens 		io_off = off;
3923789Sahrens 		io_len = PAGESIZE;
3924789Sahrens 		pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr);
3925789Sahrens 	} else {
3926789Sahrens 		/*
3927789Sahrens 		 * Try to fill a kluster of pages (a blocks worth).
3928789Sahrens 		 */
3929789Sahrens 		size_t klen;
3930789Sahrens 		u_offset_t koff;
3931789Sahrens 
3932789Sahrens 		if (!ISP2(zp->z_blksz)) {
3933789Sahrens 			/* Only one block in the file. */
3934789Sahrens 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
3935789Sahrens 			koff = 0;
3936789Sahrens 		} else {
39373131Sgw25295 			/*
39383131Sgw25295 			 * It would be ideal to align our offset to the
39393131Sgw25295 			 * blocksize but doing so has resulted in some
39403131Sgw25295 			 * strange application crashes. For now, we
39413131Sgw25295 			 * leave the offset as is and only adjust the
39423131Sgw25295 			 * length if we are off the end of the file.
39433131Sgw25295 			 */
39443131Sgw25295 			koff = off;
3945789Sahrens 			klen = plsz;
3946789Sahrens 		}
39471819Smaybee 		ASSERT(koff <= filesz);
39481819Smaybee 		if (koff + klen > filesz)
39491819Smaybee 			klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff;
39502688Smaybee 		ASSERT3U(off, >=, koff);
39512688Smaybee 		ASSERT3U(off, <, koff + klen);
3952789Sahrens 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
39534339Sperrin 		    &io_len, koff, klen, 0);
3954789Sahrens 	}
3955789Sahrens 	if (pp == NULL) {
3956789Sahrens 		/*
3957789Sahrens 		 * Some other thread entered the page before us.
3958789Sahrens 		 * Return to zfs_getpage to retry the lookup.
3959789Sahrens 		 */
3960789Sahrens 		*pl = NULL;
3961789Sahrens 		return (0);
3962789Sahrens 	}
3963789Sahrens 
3964789Sahrens 	/*
3965789Sahrens 	 * Fill the pages in the kluster.
3966789Sahrens 	 */
3967789Sahrens 	cur_pp = pp;
3968789Sahrens 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
39692688Smaybee 		ASSERT3U(io_off, ==, cur_pp->p_offset);
39707315SJonathan.Adams@Sun.COM 		va = zfs_map_page(cur_pp, S_WRITE);
39711544Seschrock 		err = dmu_read(os, oid, io_off, PAGESIZE, va);
39727315SJonathan.Adams@Sun.COM 		zfs_unmap_page(cur_pp, va);
3973789Sahrens 		if (err) {
3974789Sahrens 			/* On error, toss the entire kluster */
3975789Sahrens 			pvn_read_done(pp, B_ERROR);
39767294Sperrin 			/* convert checksum errors into IO errors */
39777294Sperrin 			if (err == ECKSUM)
39787294Sperrin 				err = EIO;
3979789Sahrens 			return (err);
3980789Sahrens 		}
3981789Sahrens 		cur_pp = cur_pp->p_next;
3982789Sahrens 	}
3983789Sahrens out:
3984789Sahrens 	/*
3985789Sahrens 	 * Fill in the page list array from the kluster.  If
3986789Sahrens 	 * there are too many pages in the kluster, return
3987789Sahrens 	 * as many pages as possible starting from the desired
3988789Sahrens 	 * offset `off'.
3989789Sahrens 	 * NOTE: the page list will always be null terminated.
3990789Sahrens 	 */
3991789Sahrens 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
3992789Sahrens 
3993789Sahrens 	return (0);
3994789Sahrens }
3995789Sahrens 
3996789Sahrens /*
3997789Sahrens  * Return pointers to the pages for the file region [off, off + len]
3998789Sahrens  * in the pl array.  If plsz is greater than len, this function may
3999789Sahrens  * also return page pointers from before or after the specified
4000789Sahrens  * region (i.e. some region [off', off' + plsz]).  These additional
4001789Sahrens  * pages are only returned if they are already in the cache, or were
4002789Sahrens  * created as part of a klustered read.
4003789Sahrens  *
4004789Sahrens  *	IN:	vp	- vnode of file to get data from.
4005789Sahrens  *		off	- position in file to get data from.
4006789Sahrens  *		len	- amount of data to retrieve.
4007789Sahrens  *		plsz	- length of provided page list.
4008789Sahrens  *		seg	- segment to obtain pages for.
4009789Sahrens  *		addr	- virtual address of fault.
4010789Sahrens  *		rw	- mode of created pages.
4011789Sahrens  *		cr	- credentials of caller.
40125331Samw  *		ct	- caller context.
4013789Sahrens  *
4014789Sahrens  *	OUT:	protp	- protection mode of created pages.
4015789Sahrens  *		pl	- list of pages created.
4016789Sahrens  *
4017789Sahrens  *	RETURN:	0 if success
4018789Sahrens  *		error code if failure
4019789Sahrens  *
4020789Sahrens  * Timestamps:
4021789Sahrens  *	vp - atime updated
4022789Sahrens  */
4023789Sahrens /* ARGSUSED */
4024789Sahrens static int
4025789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4026789Sahrens 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
40275331Samw 	enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4028789Sahrens {
4029789Sahrens 	znode_t		*zp = VTOZ(vp);
4030789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4031789Sahrens 	page_t		*pp, **pl0 = pl;
40322752Sperrin 	int		need_unlock = 0, err = 0;
40332752Sperrin 	offset_t	orig_off;
4034789Sahrens 
40355367Sahrens 	ZFS_ENTER(zfsvfs);
40365367Sahrens 	ZFS_VERIFY_ZP(zp);
4037789Sahrens 
4038789Sahrens 	if (protp)
4039789Sahrens 		*protp = PROT_ALL;
4040789Sahrens 
4041789Sahrens 	/* no faultahead (for now) */
4042789Sahrens 	if (pl == NULL) {
4043789Sahrens 		ZFS_EXIT(zfsvfs);
4044789Sahrens 		return (0);
4045789Sahrens 	}
4046789Sahrens 
4047789Sahrens 	/* can't fault past EOF */
4048789Sahrens 	if (off >= zp->z_phys->zp_size) {
4049789Sahrens 		ZFS_EXIT(zfsvfs);
4050789Sahrens 		return (EFAULT);
4051789Sahrens 	}
40522752Sperrin 	orig_off = off;
4053789Sahrens 
4054789Sahrens 	/*
4055789Sahrens 	 * If we already own the lock, then we must be page faulting
4056789Sahrens 	 * in the middle of a write to this file (i.e., we are writing
4057789Sahrens 	 * to this file using data from a mapped region of the file).
4058789Sahrens 	 */
40592752Sperrin 	if (rw_owner(&zp->z_map_lock) != curthread) {
4060789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
4061789Sahrens 		need_unlock = TRUE;
4062789Sahrens 	}
4063789Sahrens 
4064789Sahrens 	/*
4065789Sahrens 	 * Loop through the requested range [off, off + len] looking
4066789Sahrens 	 * for pages.  If we don't find a page, we will need to create
4067789Sahrens 	 * a new page and fill it with data from the file.
4068789Sahrens 	 */
4069789Sahrens 	while (len > 0) {
4070789Sahrens 		if (plsz < PAGESIZE)
4071789Sahrens 			break;
4072789Sahrens 		if (pp = page_lookup(vp, off, SE_SHARED)) {
4073789Sahrens 			*pl++ = pp;
4074789Sahrens 			off += PAGESIZE;
4075789Sahrens 			addr += PAGESIZE;
4076789Sahrens 			len -= PAGESIZE;
4077789Sahrens 			plsz -= PAGESIZE;
4078789Sahrens 		} else {
4079789Sahrens 			err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw);
40802752Sperrin 			if (err)
40812752Sperrin 				goto out;
4082789Sahrens 			/*
4083789Sahrens 			 * klustering may have changed our region
4084789Sahrens 			 * to be block aligned.
4085789Sahrens 			 */
4086789Sahrens 			if (((pp = *pl) != 0) && (off != pp->p_offset)) {
4087789Sahrens 				int delta = off - pp->p_offset;
4088789Sahrens 				len += delta;
4089789Sahrens 				off -= delta;
4090789Sahrens 				addr -= delta;
4091789Sahrens 			}
4092789Sahrens 			while (*pl) {
4093789Sahrens 				pl++;
4094789Sahrens 				off += PAGESIZE;
4095789Sahrens 				addr += PAGESIZE;
4096789Sahrens 				plsz -= PAGESIZE;
4097789Sahrens 				if (len > PAGESIZE)
4098789Sahrens 					len -= PAGESIZE;
4099789Sahrens 				else
4100789Sahrens 					len = 0;
4101789Sahrens 			}
4102789Sahrens 		}
4103789Sahrens 	}
4104789Sahrens 
4105789Sahrens 	/*
4106789Sahrens 	 * Fill out the page array with any pages already in the cache.
4107789Sahrens 	 */
4108789Sahrens 	while (plsz > 0) {
4109789Sahrens 		pp = page_lookup_nowait(vp, off, SE_SHARED);
4110789Sahrens 		if (pp == NULL)
4111789Sahrens 			break;
4112789Sahrens 		*pl++ = pp;
4113789Sahrens 		off += PAGESIZE;
4114789Sahrens 		plsz -= PAGESIZE;
4115789Sahrens 	}
4116789Sahrens 
4117789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4118789Sahrens out:
41192752Sperrin 	/*
41202752Sperrin 	 * We can't grab the range lock for the page as reader which would
41212752Sperrin 	 * stop truncation as this leads to deadlock. So we need to recheck
41222752Sperrin 	 * the file size.
41232752Sperrin 	 */
41242752Sperrin 	if (orig_off >= zp->z_phys->zp_size)
41252752Sperrin 		err = EFAULT;
41262752Sperrin 	if (err) {
41272752Sperrin 		/*
41282752Sperrin 		 * Release any pages we have previously locked.
41292752Sperrin 		 */
41302752Sperrin 		while (pl > pl0)
41312752Sperrin 			page_unlock(*--pl);
41322752Sperrin 	}
41332752Sperrin 
4134789Sahrens 	*pl = NULL;
4135789Sahrens 
4136789Sahrens 	if (need_unlock)
4137789Sahrens 		rw_exit(&zp->z_map_lock);
4138789Sahrens 
4139789Sahrens 	ZFS_EXIT(zfsvfs);
4140789Sahrens 	return (err);
4141789Sahrens }
4142789Sahrens 
41431544Seschrock /*
41441544Seschrock  * Request a memory map for a section of a file.  This code interacts
41451544Seschrock  * with common code and the VM system as follows:
41461544Seschrock  *
41471544Seschrock  *	common code calls mmap(), which ends up in smmap_common()
41481544Seschrock  *
41491544Seschrock  *	this calls VOP_MAP(), which takes you into (say) zfs
41501544Seschrock  *
41511544Seschrock  *	zfs_map() calls as_map(), passing segvn_create() as the callback
41521544Seschrock  *
41531544Seschrock  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
41541544Seschrock  *
41551544Seschrock  *	zfs_addmap() updates z_mapcnt
41561544Seschrock  */
41575331Samw /*ARGSUSED*/
4158789Sahrens static int
4159789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
41605331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
41615331Samw     caller_context_t *ct)
4162789Sahrens {
4163789Sahrens 	znode_t *zp = VTOZ(vp);
4164789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4165789Sahrens 	segvn_crargs_t	vn_a;
4166789Sahrens 	int		error;
4167789Sahrens 
41685929Smarks 	ZFS_ENTER(zfsvfs);
41695929Smarks 	ZFS_VERIFY_ZP(zp);
41705929Smarks 
41715331Samw 	if ((prot & PROT_WRITE) &&
41725331Samw 	    (zp->z_phys->zp_flags & (ZFS_IMMUTABLE | ZFS_READONLY |
41735929Smarks 	    ZFS_APPENDONLY))) {
41745929Smarks 		ZFS_EXIT(zfsvfs);
41755331Samw 		return (EPERM);
41765929Smarks 	}
41775929Smarks 
41785929Smarks 	if ((prot & (PROT_READ | PROT_EXEC)) &&
41795929Smarks 	    (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED)) {
41805929Smarks 		ZFS_EXIT(zfsvfs);
41815929Smarks 		return (EACCES);
41825929Smarks 	}
4183789Sahrens 
4184789Sahrens 	if (vp->v_flag & VNOMAP) {
4185789Sahrens 		ZFS_EXIT(zfsvfs);
4186789Sahrens 		return (ENOSYS);
4187789Sahrens 	}
4188789Sahrens 
4189789Sahrens 	if (off < 0 || len > MAXOFFSET_T - off) {
4190789Sahrens 		ZFS_EXIT(zfsvfs);
4191789Sahrens 		return (ENXIO);
4192789Sahrens 	}
4193789Sahrens 
4194789Sahrens 	if (vp->v_type != VREG) {
4195789Sahrens 		ZFS_EXIT(zfsvfs);
4196789Sahrens 		return (ENODEV);
4197789Sahrens 	}
4198789Sahrens 
4199789Sahrens 	/*
4200789Sahrens 	 * If file is locked, disallow mapping.
4201789Sahrens 	 */
42021544Seschrock 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) {
42031544Seschrock 		ZFS_EXIT(zfsvfs);
42041544Seschrock 		return (EAGAIN);
4205789Sahrens 	}
4206789Sahrens 
4207789Sahrens 	as_rangelock(as);
42086036Smec 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
42096036Smec 	if (error != 0) {
42106036Smec 		as_rangeunlock(as);
42116036Smec 		ZFS_EXIT(zfsvfs);
42126036Smec 		return (error);
4213789Sahrens 	}
4214789Sahrens 
4215789Sahrens 	vn_a.vp = vp;
4216789Sahrens 	vn_a.offset = (u_offset_t)off;
4217789Sahrens 	vn_a.type = flags & MAP_TYPE;
4218789Sahrens 	vn_a.prot = prot;
4219789Sahrens 	vn_a.maxprot = maxprot;
4220789Sahrens 	vn_a.cred = cr;
4221789Sahrens 	vn_a.amp = NULL;
4222789Sahrens 	vn_a.flags = flags & ~MAP_TYPE;
42231417Skchow 	vn_a.szc = 0;
42241417Skchow 	vn_a.lgrp_mem_policy_flags = 0;
4225789Sahrens 
4226789Sahrens 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
4227789Sahrens 
4228789Sahrens 	as_rangeunlock(as);
4229789Sahrens 	ZFS_EXIT(zfsvfs);
4230789Sahrens 	return (error);
4231789Sahrens }
4232789Sahrens 
4233789Sahrens /* ARGSUSED */
4234789Sahrens static int
4235789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
42365331Samw     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
42375331Samw     caller_context_t *ct)
4238789Sahrens {
42391544Seschrock 	uint64_t pages = btopr(len);
42401544Seschrock 
42411544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4242789Sahrens 	return (0);
4243789Sahrens }
4244789Sahrens 
42451773Seschrock /*
42461773Seschrock  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
42471773Seschrock  * more accurate mtime for the associated file.  Since we don't have a way of
42481773Seschrock  * detecting when the data was actually modified, we have to resort to
42491773Seschrock  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
42501773Seschrock  * last page is pushed.  The problem occurs when the msync() call is omitted,
42511773Seschrock  * which by far the most common case:
42521773Seschrock  *
42531773Seschrock  * 	open()
42541773Seschrock  * 	mmap()
42551773Seschrock  * 	<modify memory>
42561773Seschrock  * 	munmap()
42571773Seschrock  * 	close()
42581773Seschrock  * 	<time lapse>
42591773Seschrock  * 	putpage() via fsflush
42601773Seschrock  *
42611773Seschrock  * If we wait until fsflush to come along, we can have a modification time that
42621773Seschrock  * is some arbitrary point in the future.  In order to prevent this in the
42631773Seschrock  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
42641773Seschrock  * torn down.
42651773Seschrock  */
4266789Sahrens /* ARGSUSED */
4267789Sahrens static int
4268789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
42695331Samw     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
42705331Samw     caller_context_t *ct)
4271789Sahrens {
42721544Seschrock 	uint64_t pages = btopr(len);
42731544Seschrock 
42741544Seschrock 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
42751544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
42761773Seschrock 
42771773Seschrock 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
42781773Seschrock 	    vn_has_cached_data(vp))
42795331Samw 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
42801773Seschrock 
4281789Sahrens 	return (0);
4282789Sahrens }
4283789Sahrens 
4284789Sahrens /*
4285789Sahrens  * Free or allocate space in a file.  Currently, this function only
4286789Sahrens  * supports the `F_FREESP' command.  However, this command is somewhat
4287789Sahrens  * misnamed, as its functionality includes the ability to allocate as
4288789Sahrens  * well as free space.
4289789Sahrens  *
4290789Sahrens  *	IN:	vp	- vnode of file to free data in.
4291789Sahrens  *		cmd	- action to take (only F_FREESP supported).
4292789Sahrens  *		bfp	- section of file to free/alloc.
4293789Sahrens  *		flag	- current file open mode flags.
4294789Sahrens  *		offset	- current file offset.
4295789Sahrens  *		cr	- credentials of caller [UNUSED].
42965331Samw  *		ct	- caller context.
4297789Sahrens  *
4298789Sahrens  *	RETURN:	0 if success
4299789Sahrens  *		error code if failure
4300789Sahrens  *
4301789Sahrens  * Timestamps:
4302789Sahrens  *	vp - ctime|mtime updated
4303789Sahrens  */
4304789Sahrens /* ARGSUSED */
4305789Sahrens static int
4306789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4307789Sahrens     offset_t offset, cred_t *cr, caller_context_t *ct)
4308789Sahrens {
4309789Sahrens 	znode_t		*zp = VTOZ(vp);
4310789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4311789Sahrens 	uint64_t	off, len;
4312789Sahrens 	int		error;
4313789Sahrens 
43145367Sahrens 	ZFS_ENTER(zfsvfs);
43155367Sahrens 	ZFS_VERIFY_ZP(zp);
4316789Sahrens 
4317789Sahrens 	if (cmd != F_FREESP) {
4318789Sahrens 		ZFS_EXIT(zfsvfs);
4319789Sahrens 		return (EINVAL);
4320789Sahrens 	}
4321789Sahrens 
4322789Sahrens 	if (error = convoff(vp, bfp, 0, offset)) {
4323789Sahrens 		ZFS_EXIT(zfsvfs);
4324789Sahrens 		return (error);
4325789Sahrens 	}
4326789Sahrens 
4327789Sahrens 	if (bfp->l_len < 0) {
4328789Sahrens 		ZFS_EXIT(zfsvfs);
4329789Sahrens 		return (EINVAL);
4330789Sahrens 	}
4331789Sahrens 
4332789Sahrens 	off = bfp->l_start;
43331669Sperrin 	len = bfp->l_len; /* 0 means from off to end of file */
43341878Smaybee 
43356992Smaybee 	error = zfs_freesp(zp, off, len, flag, TRUE);
4336789Sahrens 
4337789Sahrens 	ZFS_EXIT(zfsvfs);
4338789Sahrens 	return (error);
4339789Sahrens }
4340789Sahrens 
43415331Samw /*ARGSUSED*/
4342789Sahrens static int
43435331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4344789Sahrens {
4345789Sahrens 	znode_t		*zp = VTOZ(vp);
4346789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
43475326Sek110237 	uint32_t	gen;
4348789Sahrens 	uint64_t	object = zp->z_id;
4349789Sahrens 	zfid_short_t	*zfid;
4350789Sahrens 	int		size, i;
4351789Sahrens 
43525367Sahrens 	ZFS_ENTER(zfsvfs);
43535367Sahrens 	ZFS_VERIFY_ZP(zp);
43545326Sek110237 	gen = (uint32_t)zp->z_gen;
4355789Sahrens 
4356789Sahrens 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4357789Sahrens 	if (fidp->fid_len < size) {
4358789Sahrens 		fidp->fid_len = size;
43591512Sek110237 		ZFS_EXIT(zfsvfs);
4360789Sahrens 		return (ENOSPC);
4361789Sahrens 	}
4362789Sahrens 
4363789Sahrens 	zfid = (zfid_short_t *)fidp;
4364789Sahrens 
4365789Sahrens 	zfid->zf_len = size;
4366789Sahrens 
4367789Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
4368789Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4369789Sahrens 
4370789Sahrens 	/* Must have a non-zero generation number to distinguish from .zfs */
4371789Sahrens 	if (gen == 0)
4372789Sahrens 		gen = 1;
4373789Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
4374789Sahrens 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4375789Sahrens 
4376789Sahrens 	if (size == LONG_FID_LEN) {
4377789Sahrens 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
4378789Sahrens 		zfid_long_t	*zlfid;
4379789Sahrens 
4380789Sahrens 		zlfid = (zfid_long_t *)fidp;
4381789Sahrens 
4382789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4383789Sahrens 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4384789Sahrens 
4385789Sahrens 		/* XXX - this should be the generation number for the objset */
4386789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4387789Sahrens 			zlfid->zf_setgen[i] = 0;
4388789Sahrens 	}
4389789Sahrens 
4390789Sahrens 	ZFS_EXIT(zfsvfs);
4391789Sahrens 	return (0);
4392789Sahrens }
4393789Sahrens 
4394789Sahrens static int
43955331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
43965331Samw     caller_context_t *ct)
4397789Sahrens {
4398789Sahrens 	znode_t		*zp, *xzp;
4399789Sahrens 	zfsvfs_t	*zfsvfs;
4400789Sahrens 	zfs_dirlock_t	*dl;
4401789Sahrens 	int		error;
4402789Sahrens 
4403789Sahrens 	switch (cmd) {
4404789Sahrens 	case _PC_LINK_MAX:
4405789Sahrens 		*valp = ULONG_MAX;
4406789Sahrens 		return (0);
4407789Sahrens 
4408789Sahrens 	case _PC_FILESIZEBITS:
4409789Sahrens 		*valp = 64;
4410789Sahrens 		return (0);
4411789Sahrens 
4412789Sahrens 	case _PC_XATTR_EXISTS:
4413789Sahrens 		zp = VTOZ(vp);
4414789Sahrens 		zfsvfs = zp->z_zfsvfs;
44155367Sahrens 		ZFS_ENTER(zfsvfs);
44165367Sahrens 		ZFS_VERIFY_ZP(zp);
4417789Sahrens 		*valp = 0;
4418789Sahrens 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
44195331Samw 		    ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4420789Sahrens 		if (error == 0) {
4421789Sahrens 			zfs_dirent_unlock(dl);
4422789Sahrens 			if (!zfs_dirempty(xzp))
4423789Sahrens 				*valp = 1;
4424789Sahrens 			VN_RELE(ZTOV(xzp));
4425789Sahrens 		} else if (error == ENOENT) {
4426789Sahrens 			/*
4427789Sahrens 			 * If there aren't extended attributes, it's the
4428789Sahrens 			 * same as having zero of them.
4429789Sahrens 			 */
4430789Sahrens 			error = 0;
4431789Sahrens 		}
4432789Sahrens 		ZFS_EXIT(zfsvfs);
4433789Sahrens 		return (error);
4434789Sahrens 
44355331Samw 	case _PC_SATTR_ENABLED:
44365331Samw 	case _PC_SATTR_EXISTS:
44377757SJanice.Chang@Sun.COM 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
44385331Samw 		    (vp->v_type == VREG || vp->v_type == VDIR);
44395331Samw 		return (0);
44405331Samw 
4441789Sahrens 	case _PC_ACL_ENABLED:
4442789Sahrens 		*valp = _ACL_ACE_ENABLED;
4443789Sahrens 		return (0);
4444789Sahrens 
4445789Sahrens 	case _PC_MIN_HOLE_SIZE:
4446789Sahrens 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
4447789Sahrens 		return (0);
4448789Sahrens 
4449789Sahrens 	default:
44505331Samw 		return (fs_pathconf(vp, cmd, valp, cr, ct));
4451789Sahrens 	}
4452789Sahrens }
4453789Sahrens 
4454789Sahrens /*ARGSUSED*/
4455789Sahrens static int
44565331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
44575331Samw     caller_context_t *ct)
4458789Sahrens {
4459789Sahrens 	znode_t *zp = VTOZ(vp);
4460789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4461789Sahrens 	int error;
44625331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4463789Sahrens 
44645367Sahrens 	ZFS_ENTER(zfsvfs);
44655367Sahrens 	ZFS_VERIFY_ZP(zp);
44665331Samw 	error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4467789Sahrens 	ZFS_EXIT(zfsvfs);
4468789Sahrens 
4469789Sahrens 	return (error);
4470789Sahrens }
4471789Sahrens 
4472789Sahrens /*ARGSUSED*/
4473789Sahrens static int
44745331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
44755331Samw     caller_context_t *ct)
4476789Sahrens {
4477789Sahrens 	znode_t *zp = VTOZ(vp);
4478789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4479789Sahrens 	int error;
44805331Samw 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4481789Sahrens 
44825367Sahrens 	ZFS_ENTER(zfsvfs);
44835367Sahrens 	ZFS_VERIFY_ZP(zp);
44845331Samw 	error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4485789Sahrens 	ZFS_EXIT(zfsvfs);
4486789Sahrens 	return (error);
4487789Sahrens }
4488789Sahrens 
4489789Sahrens /*
4490789Sahrens  * Predeclare these here so that the compiler assumes that
4491789Sahrens  * this is an "old style" function declaration that does
4492789Sahrens  * not include arguments => we won't get type mismatch errors
4493789Sahrens  * in the initializations that follow.
4494789Sahrens  */
4495789Sahrens static int zfs_inval();
4496789Sahrens static int zfs_isdir();
4497789Sahrens 
4498789Sahrens static int
4499789Sahrens zfs_inval()
4500789Sahrens {
4501789Sahrens 	return (EINVAL);
4502789Sahrens }
4503789Sahrens 
4504789Sahrens static int
4505789Sahrens zfs_isdir()
4506789Sahrens {
4507789Sahrens 	return (EISDIR);
4508789Sahrens }
4509789Sahrens /*
4510789Sahrens  * Directory vnode operations template
4511789Sahrens  */
4512789Sahrens vnodeops_t *zfs_dvnodeops;
4513789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = {
45143898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
45153898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
45163898Srsb 	VOPNAME_READ,		{ .error = zfs_isdir },
45173898Srsb 	VOPNAME_WRITE,		{ .error = zfs_isdir },
45183898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
45193898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
45203898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
45213898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
45223898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
45233898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
45243898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
45253898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
45263898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
45273898Srsb 	VOPNAME_MKDIR,		{ .vop_mkdir = zfs_mkdir },
45283898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
45293898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
45303898Srsb 	VOPNAME_SYMLINK,	{ .vop_symlink = zfs_symlink },
45313898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
45323898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
45333898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
45343898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
45353898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
45363898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
45373898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
45384863Spraks 	VOPNAME_VNEVENT, 	{ .vop_vnevent = fs_vnevent_support },
45393898Srsb 	NULL,			NULL
4540789Sahrens };
4541789Sahrens 
4542789Sahrens /*
4543789Sahrens  * Regular file vnode operations template
4544789Sahrens  */
4545789Sahrens vnodeops_t *zfs_fvnodeops;
4546789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = {
45473898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
45483898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
45493898Srsb 	VOPNAME_READ,		{ .vop_read = zfs_read },
45503898Srsb 	VOPNAME_WRITE,		{ .vop_write = zfs_write },
45513898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
45523898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
45533898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
45543898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
45553898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
45563898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
45573898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
45583898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
45593898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
45603898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
45613898Srsb 	VOPNAME_FRLOCK,		{ .vop_frlock = zfs_frlock },
45623898Srsb 	VOPNAME_SPACE,		{ .vop_space = zfs_space },
45633898Srsb 	VOPNAME_GETPAGE,	{ .vop_getpage = zfs_getpage },
45643898Srsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = zfs_putpage },
45653898Srsb 	VOPNAME_MAP,		{ .vop_map = zfs_map },
45663898Srsb 	VOPNAME_ADDMAP,		{ .vop_addmap = zfs_addmap },
45673898Srsb 	VOPNAME_DELMAP,		{ .vop_delmap = zfs_delmap },
45683898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
45693898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
45703898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
45713898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
45723898Srsb 	NULL,			NULL
4573789Sahrens };
4574789Sahrens 
4575789Sahrens /*
4576789Sahrens  * Symbolic link vnode operations template
4577789Sahrens  */
4578789Sahrens vnodeops_t *zfs_symvnodeops;
4579789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = {
45803898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
45813898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
45823898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
45833898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
45843898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = zfs_readlink },
45853898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
45863898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
45873898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
45883898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
45893898Srsb 	NULL,			NULL
4590789Sahrens };
4591789Sahrens 
4592789Sahrens /*
4593789Sahrens  * Extended attribute directory vnode operations template
4594789Sahrens  *	This template is identical to the directory vnodes
4595789Sahrens  *	operation template except for restricted operations:
4596789Sahrens  *		VOP_MKDIR()
4597789Sahrens  *		VOP_SYMLINK()
4598789Sahrens  * Note that there are other restrictions embedded in:
4599789Sahrens  *	zfs_create()	- restrict type to VREG
4600789Sahrens  *	zfs_link()	- no links into/out of attribute space
4601789Sahrens  *	zfs_rename()	- no moves into/out of attribute space
4602789Sahrens  */
4603789Sahrens vnodeops_t *zfs_xdvnodeops;
4604789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = {
46053898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
46063898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
46073898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
46083898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
46093898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
46103898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
46113898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
46123898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
46133898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
46143898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
46153898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
46163898Srsb 	VOPNAME_MKDIR,		{ .error = zfs_inval },
46173898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
46183898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
46193898Srsb 	VOPNAME_SYMLINK,	{ .error = zfs_inval },
46203898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
46213898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
46223898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
46233898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
46243898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
46253898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
46263898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
46273898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
46283898Srsb 	NULL,			NULL
4629789Sahrens };
4630789Sahrens 
4631789Sahrens /*
4632789Sahrens  * Error vnode operations template
4633789Sahrens  */
4634789Sahrens vnodeops_t *zfs_evnodeops;
4635789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = {
46363898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
46373898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
46383898Srsb 	NULL,			NULL
4639789Sahrens };
4640