xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 4720:8edc0d2e6f3f)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51460Smarks  * Common Development and Distribution License (the "License").
61460Smarks  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
223461Sahrens  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
264144Speteh /* Portions Copyright 2007 Jeremy Teo */
274144Speteh 
28789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
29789Sahrens 
30789Sahrens #include <sys/types.h>
31789Sahrens #include <sys/param.h>
32789Sahrens #include <sys/time.h>
33789Sahrens #include <sys/systm.h>
34789Sahrens #include <sys/sysmacros.h>
35789Sahrens #include <sys/resource.h>
36789Sahrens #include <sys/vfs.h>
373898Srsb #include <sys/vfs_opreg.h>
38789Sahrens #include <sys/vnode.h>
39789Sahrens #include <sys/file.h>
40789Sahrens #include <sys/stat.h>
41789Sahrens #include <sys/kmem.h>
42789Sahrens #include <sys/taskq.h>
43789Sahrens #include <sys/uio.h>
44789Sahrens #include <sys/vmsystm.h>
45789Sahrens #include <sys/atomic.h>
462688Smaybee #include <sys/vm.h>
47789Sahrens #include <vm/seg_vn.h>
48789Sahrens #include <vm/pvn.h>
49789Sahrens #include <vm/as.h>
50789Sahrens #include <sys/mman.h>
51789Sahrens #include <sys/pathname.h>
52789Sahrens #include <sys/cmn_err.h>
53789Sahrens #include <sys/errno.h>
54789Sahrens #include <sys/unistd.h>
55789Sahrens #include <sys/zfs_vfsops.h>
56789Sahrens #include <sys/zfs_dir.h>
57789Sahrens #include <sys/zfs_acl.h>
58789Sahrens #include <sys/zfs_ioctl.h>
59789Sahrens #include <sys/fs/zfs.h>
60789Sahrens #include <sys/dmu.h>
61789Sahrens #include <sys/spa.h>
62789Sahrens #include <sys/txg.h>
63789Sahrens #include <sys/dbuf.h>
64789Sahrens #include <sys/zap.h>
65789Sahrens #include <sys/dirent.h>
66789Sahrens #include <sys/policy.h>
67789Sahrens #include <sys/sunddi.h>
68789Sahrens #include <sys/filio.h>
69789Sahrens #include "fs/fs_subr.h"
70789Sahrens #include <sys/zfs_ctldir.h>
711484Sek110237 #include <sys/dnlc.h>
721669Sperrin #include <sys/zfs_rlock.h>
73789Sahrens 
74789Sahrens /*
75789Sahrens  * Programming rules.
76789Sahrens  *
77789Sahrens  * Each vnode op performs some logical unit of work.  To do this, the ZPL must
78789Sahrens  * properly lock its in-core state, create a DMU transaction, do the work,
79789Sahrens  * record this work in the intent log (ZIL), commit the DMU transaction,
80789Sahrens  * and wait the the intent log to commit if it's is a synchronous operation.
81789Sahrens  * Morover, the vnode ops must work in both normal and log replay context.
82789Sahrens  * The ordering of events is important to avoid deadlocks and references
83789Sahrens  * to freed memory.  The example below illustrates the following Big Rules:
84789Sahrens  *
85789Sahrens  *  (1) A check must be made in each zfs thread for a mounted file system.
86789Sahrens  *	This is done avoiding races using ZFS_ENTER(zfsvfs).
87789Sahrens  *	A ZFS_EXIT(zfsvfs) is needed before all returns.
88789Sahrens  *
89789Sahrens  *  (2)	VN_RELE() should always be the last thing except for zil_commit()
902638Sperrin  *	(if necessary) and ZFS_EXIT(). This is for 3 reasons:
91789Sahrens  *	First, if it's the last reference, the vnode/znode
92789Sahrens  *	can be freed, so the zp may point to freed memory.  Second, the last
93789Sahrens  *	reference will call zfs_zinactive(), which may induce a lot of work --
941669Sperrin  *	pushing cached pages (which acquires range locks) and syncing out
95789Sahrens  *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
96789Sahrens  *	which could deadlock the system if you were already holding one.
97789Sahrens  *
981757Sperrin  *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
991757Sperrin  *	as they can span dmu_tx_assign() calls.
1001757Sperrin  *
1011757Sperrin  *  (4)	Always pass zfsvfs->z_assign as the second argument to dmu_tx_assign().
102789Sahrens  *	In normal operation, this will be TXG_NOWAIT.  During ZIL replay,
103789Sahrens  *	it will be a specific txg.  Either way, dmu_tx_assign() never blocks.
104789Sahrens  *	This is critical because we don't want to block while holding locks.
105789Sahrens  *	Note, in particular, that if a lock is sometimes acquired before
106789Sahrens  *	the tx assigns, and sometimes after (e.g. z_lock), then failing to
107789Sahrens  *	use a non-blocking assign can deadlock the system.  The scenario:
108789Sahrens  *
109789Sahrens  *	Thread A has grabbed a lock before calling dmu_tx_assign().
110789Sahrens  *	Thread B is in an already-assigned tx, and blocks for this lock.
111789Sahrens  *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
112789Sahrens  *	forever, because the previous txg can't quiesce until B's tx commits.
113789Sahrens  *
114789Sahrens  *	If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
1152113Sahrens  *	then drop all locks, call dmu_tx_wait(), and try again.
116789Sahrens  *
1171757Sperrin  *  (5)	If the operation succeeded, generate the intent log entry for it
118789Sahrens  *	before dropping locks.  This ensures that the ordering of events
119789Sahrens  *	in the intent log matches the order in which they actually occurred.
120789Sahrens  *
1211757Sperrin  *  (6)	At the end of each vnode op, the DMU tx must always commit,
122789Sahrens  *	regardless of whether there were any errors.
123789Sahrens  *
1242638Sperrin  *  (7)	After dropping all locks, invoke zil_commit(zilog, seq, foid)
125789Sahrens  *	to ensure that synchronous semantics are provided when necessary.
126789Sahrens  *
127789Sahrens  * In general, this is how things should be ordered in each vnode op:
128789Sahrens  *
129789Sahrens  *	ZFS_ENTER(zfsvfs);		// exit if unmounted
130789Sahrens  * top:
131789Sahrens  *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
132789Sahrens  *	rw_enter(...);			// grab any other locks you need
133789Sahrens  *	tx = dmu_tx_create(...);	// get DMU tx
134789Sahrens  *	dmu_tx_hold_*();		// hold each object you might modify
135789Sahrens  *	error = dmu_tx_assign(tx, zfsvfs->z_assign);	// try to assign
136789Sahrens  *	if (error) {
137789Sahrens  *		rw_exit(...);		// drop locks
138789Sahrens  *		zfs_dirent_unlock(dl);	// unlock directory entry
139789Sahrens  *		VN_RELE(...);		// release held vnodes
140789Sahrens  *		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1412113Sahrens  *			dmu_tx_wait(tx);
1422113Sahrens  *			dmu_tx_abort(tx);
143789Sahrens  *			goto top;
144789Sahrens  *		}
1452113Sahrens  *		dmu_tx_abort(tx);	// abort DMU tx
146789Sahrens  *		ZFS_EXIT(zfsvfs);	// finished in zfs
147789Sahrens  *		return (error);		// really out of space
148789Sahrens  *	}
149789Sahrens  *	error = do_real_work();		// do whatever this VOP does
150789Sahrens  *	if (error == 0)
1512638Sperrin  *		zfs_log_*(...);		// on success, make ZIL entry
152789Sahrens  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
153789Sahrens  *	rw_exit(...);			// drop locks
154789Sahrens  *	zfs_dirent_unlock(dl);		// unlock directory entry
155789Sahrens  *	VN_RELE(...);			// release held vnodes
1562638Sperrin  *	zil_commit(zilog, seq, foid);	// synchronous when necessary
157789Sahrens  *	ZFS_EXIT(zfsvfs);		// finished in zfs
158789Sahrens  *	return (error);			// done, report error
159789Sahrens  */
160789Sahrens /* ARGSUSED */
161789Sahrens static int
162789Sahrens zfs_open(vnode_t **vpp, int flag, cred_t *cr)
163789Sahrens {
1643063Sperrin 	znode_t	*zp = VTOZ(*vpp);
1653063Sperrin 
1663063Sperrin 	/* Keep a count of the synchronous opens in the znode */
1673063Sperrin 	if (flag & (FSYNC | FDSYNC))
1683063Sperrin 		atomic_inc_32(&zp->z_sync_cnt);
169789Sahrens 	return (0);
170789Sahrens }
171789Sahrens 
172789Sahrens /* ARGSUSED */
173789Sahrens static int
174789Sahrens zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr)
175789Sahrens {
1763063Sperrin 	znode_t	*zp = VTOZ(vp);
1773063Sperrin 
1783063Sperrin 	/* Decrement the synchronous opens in the znode */
1794339Sperrin 	if ((flag & (FSYNC | FDSYNC)) && (count == 1))
1803063Sperrin 		atomic_dec_32(&zp->z_sync_cnt);
1813063Sperrin 
182789Sahrens 	/*
183789Sahrens 	 * Clean up any locks held by this process on the vp.
184789Sahrens 	 */
185789Sahrens 	cleanlocks(vp, ddi_get_pid(), 0);
186789Sahrens 	cleanshares(vp, ddi_get_pid());
187789Sahrens 
188789Sahrens 	return (0);
189789Sahrens }
190789Sahrens 
191789Sahrens /*
192789Sahrens  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
193789Sahrens  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
194789Sahrens  */
195789Sahrens static int
196789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off)
197789Sahrens {
198789Sahrens 	znode_t	*zp = VTOZ(vp);
199789Sahrens 	uint64_t noff = (uint64_t)*off; /* new offset */
200789Sahrens 	uint64_t file_sz;
201789Sahrens 	int error;
202789Sahrens 	boolean_t hole;
203789Sahrens 
204789Sahrens 	file_sz = zp->z_phys->zp_size;
205789Sahrens 	if (noff >= file_sz)  {
206789Sahrens 		return (ENXIO);
207789Sahrens 	}
208789Sahrens 
209789Sahrens 	if (cmd == _FIO_SEEK_HOLE)
210789Sahrens 		hole = B_TRUE;
211789Sahrens 	else
212789Sahrens 		hole = B_FALSE;
213789Sahrens 
214789Sahrens 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
215789Sahrens 
216789Sahrens 	/* end of file? */
217789Sahrens 	if ((error == ESRCH) || (noff > file_sz)) {
218789Sahrens 		/*
219789Sahrens 		 * Handle the virtual hole at the end of file.
220789Sahrens 		 */
221789Sahrens 		if (hole) {
222789Sahrens 			*off = file_sz;
223789Sahrens 			return (0);
224789Sahrens 		}
225789Sahrens 		return (ENXIO);
226789Sahrens 	}
227789Sahrens 
228789Sahrens 	if (noff < *off)
229789Sahrens 		return (error);
230789Sahrens 	*off = noff;
231789Sahrens 	return (error);
232789Sahrens }
233789Sahrens 
234789Sahrens /* ARGSUSED */
235789Sahrens static int
236789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
237789Sahrens     int *rvalp)
238789Sahrens {
239789Sahrens 	offset_t off;
240789Sahrens 	int error;
241789Sahrens 	zfsvfs_t *zfsvfs;
242789Sahrens 
243789Sahrens 	switch (com) {
2444339Sperrin 	case _FIOFFS:
245789Sahrens 		return (zfs_sync(vp->v_vfsp, 0, cred));
246789Sahrens 
2471544Seschrock 		/*
2481544Seschrock 		 * The following two ioctls are used by bfu.  Faking out,
2491544Seschrock 		 * necessary to avoid bfu errors.
2501544Seschrock 		 */
2514339Sperrin 	case _FIOGDIO:
2524339Sperrin 	case _FIOSDIO:
2531544Seschrock 		return (0);
2541544Seschrock 
2554339Sperrin 	case _FIO_SEEK_DATA:
2564339Sperrin 	case _FIO_SEEK_HOLE:
257789Sahrens 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
258789Sahrens 			return (EFAULT);
259789Sahrens 
260789Sahrens 		zfsvfs = VTOZ(vp)->z_zfsvfs;
261789Sahrens 		ZFS_ENTER(zfsvfs);
262789Sahrens 
263789Sahrens 		/* offset parameter is in/out */
264789Sahrens 		error = zfs_holey(vp, com, &off);
265789Sahrens 		ZFS_EXIT(zfsvfs);
266789Sahrens 		if (error)
267789Sahrens 			return (error);
268789Sahrens 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
269789Sahrens 			return (EFAULT);
270789Sahrens 		return (0);
271789Sahrens 	}
272789Sahrens 	return (ENOTTY);
273789Sahrens }
274789Sahrens 
275789Sahrens /*
276789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
277789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
278789Sahrens  *
279789Sahrens  * On Write:	If we find a memory mapped page, we write to *both*
280789Sahrens  *		the page and the dmu buffer.
281789Sahrens  *
282789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
283789Sahrens  *	the file is memory mapped.
284789Sahrens  */
285789Sahrens static int
2863638Sbillm mappedwrite(vnode_t *vp, int nbytes, uio_t *uio, dmu_tx_t *tx)
287789Sahrens {
288789Sahrens 	znode_t	*zp = VTOZ(vp);
289789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
290789Sahrens 	int64_t	start, off;
291789Sahrens 	int len = nbytes;
292789Sahrens 	int error = 0;
293789Sahrens 
294789Sahrens 	start = uio->uio_loffset;
295789Sahrens 	off = start & PAGEOFFSET;
296789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
297789Sahrens 		page_t *pp;
298789Sahrens 		uint64_t bytes = MIN(PAGESIZE - off, len);
2993638Sbillm 		uint64_t woff = uio->uio_loffset;
300789Sahrens 
301789Sahrens 		/*
302789Sahrens 		 * We don't want a new page to "appear" in the middle of
303789Sahrens 		 * the file update (because it may not get the write
304789Sahrens 		 * update data), so we grab a lock to block
305789Sahrens 		 * zfs_getpage().
306789Sahrens 		 */
307789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
308789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
309789Sahrens 			caddr_t va;
310789Sahrens 
311789Sahrens 			rw_exit(&zp->z_map_lock);
312789Sahrens 			va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L);
313789Sahrens 			error = uiomove(va+off, bytes, UIO_WRITE, uio);
314789Sahrens 			if (error == 0) {
315789Sahrens 				dmu_write(zfsvfs->z_os, zp->z_id,
316789Sahrens 				    woff, bytes, va+off, tx);
317789Sahrens 			}
318789Sahrens 			ppmapout(va);
319789Sahrens 			page_unlock(pp);
320789Sahrens 		} else {
321789Sahrens 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
3223638Sbillm 			    uio, bytes, tx);
323789Sahrens 			rw_exit(&zp->z_map_lock);
324789Sahrens 		}
325789Sahrens 		len -= bytes;
326789Sahrens 		off = 0;
327789Sahrens 		if (error)
328789Sahrens 			break;
329789Sahrens 	}
330789Sahrens 	return (error);
331789Sahrens }
332789Sahrens 
333789Sahrens /*
334789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
335789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
336789Sahrens  *
337789Sahrens  * On Read:	We "read" preferentially from memory mapped pages,
338789Sahrens  *		else we default from the dmu buffer.
339789Sahrens  *
340789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
341789Sahrens  *	the file is memory mapped.
342789Sahrens  */
343789Sahrens static int
3443638Sbillm mappedread(vnode_t *vp, int nbytes, uio_t *uio)
345789Sahrens {
3463638Sbillm 	znode_t *zp = VTOZ(vp);
3473638Sbillm 	objset_t *os = zp->z_zfsvfs->z_os;
3483638Sbillm 	int64_t	start, off;
349789Sahrens 	int len = nbytes;
350789Sahrens 	int error = 0;
351789Sahrens 
352789Sahrens 	start = uio->uio_loffset;
353789Sahrens 	off = start & PAGEOFFSET;
354789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
355789Sahrens 		page_t *pp;
3563638Sbillm 		uint64_t bytes = MIN(PAGESIZE - off, len);
3573638Sbillm 
358789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
359789Sahrens 			caddr_t va;
360789Sahrens 
3612688Smaybee 			va = ppmapin(pp, PROT_READ, (caddr_t)-1L);
362789Sahrens 			error = uiomove(va + off, bytes, UIO_READ, uio);
363789Sahrens 			ppmapout(va);
364789Sahrens 			page_unlock(pp);
365789Sahrens 		} else {
3663638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, bytes);
367789Sahrens 		}
368789Sahrens 		len -= bytes;
369789Sahrens 		off = 0;
370789Sahrens 		if (error)
371789Sahrens 			break;
372789Sahrens 	}
373789Sahrens 	return (error);
374789Sahrens }
375789Sahrens 
3763638Sbillm offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
377789Sahrens 
378789Sahrens /*
379789Sahrens  * Read bytes from specified file into supplied buffer.
380789Sahrens  *
381789Sahrens  *	IN:	vp	- vnode of file to be read from.
382789Sahrens  *		uio	- structure supplying read location, range info,
383789Sahrens  *			  and return buffer.
384789Sahrens  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
385789Sahrens  *		cr	- credentials of caller.
386789Sahrens  *
387789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
388789Sahrens  *
389789Sahrens  *	RETURN:	0 if success
390789Sahrens  *		error code if failure
391789Sahrens  *
392789Sahrens  * Side Effects:
393789Sahrens  *	vp - atime updated if byte count > 0
394789Sahrens  */
395789Sahrens /* ARGSUSED */
396789Sahrens static int
397789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
398789Sahrens {
399789Sahrens 	znode_t		*zp = VTOZ(vp);
400789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4013638Sbillm 	objset_t	*os = zfsvfs->z_os;
4023638Sbillm 	ssize_t		n, nbytes;
4033638Sbillm 	int		error;
4041669Sperrin 	rl_t		*rl;
405789Sahrens 
406789Sahrens 	ZFS_ENTER(zfsvfs);
407789Sahrens 
408789Sahrens 	/*
409789Sahrens 	 * Validate file offset
410789Sahrens 	 */
411789Sahrens 	if (uio->uio_loffset < (offset_t)0) {
412789Sahrens 		ZFS_EXIT(zfsvfs);
413789Sahrens 		return (EINVAL);
414789Sahrens 	}
415789Sahrens 
416789Sahrens 	/*
417789Sahrens 	 * Fasttrack empty reads
418789Sahrens 	 */
419789Sahrens 	if (uio->uio_resid == 0) {
420789Sahrens 		ZFS_EXIT(zfsvfs);
421789Sahrens 		return (0);
422789Sahrens 	}
423789Sahrens 
424789Sahrens 	/*
4251669Sperrin 	 * Check for mandatory locks
426789Sahrens 	 */
427789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
428789Sahrens 		if (error = chklock(vp, FREAD,
429789Sahrens 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
430789Sahrens 			ZFS_EXIT(zfsvfs);
431789Sahrens 			return (error);
432789Sahrens 		}
433789Sahrens 	}
434789Sahrens 
435789Sahrens 	/*
436789Sahrens 	 * If we're in FRSYNC mode, sync out this znode before reading it.
437789Sahrens 	 */
4382638Sperrin 	if (ioflag & FRSYNC)
4392638Sperrin 		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
440789Sahrens 
441789Sahrens 	/*
4421669Sperrin 	 * Lock the range against changes.
443789Sahrens 	 */
4441669Sperrin 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
4451669Sperrin 
446789Sahrens 	/*
447789Sahrens 	 * If we are reading past end-of-file we can skip
448789Sahrens 	 * to the end; but we might still need to set atime.
449789Sahrens 	 */
450789Sahrens 	if (uio->uio_loffset >= zp->z_phys->zp_size) {
451789Sahrens 		error = 0;
452789Sahrens 		goto out;
453789Sahrens 	}
454789Sahrens 
4553638Sbillm 	ASSERT(uio->uio_loffset < zp->z_phys->zp_size);
4563638Sbillm 	n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset);
4573638Sbillm 
4583638Sbillm 	while (n > 0) {
4593638Sbillm 		nbytes = MIN(n, zfs_read_chunk_size -
4603638Sbillm 		    P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
4613638Sbillm 
4623638Sbillm 		if (vn_has_cached_data(vp))
4633638Sbillm 			error = mappedread(vp, nbytes, uio);
4643638Sbillm 		else
4653638Sbillm 			error = dmu_read_uio(os, zp->z_id, uio, nbytes);
4661544Seschrock 		if (error)
4673638Sbillm 			break;
4683638Sbillm 
4693638Sbillm 		n -= nbytes;
470789Sahrens 	}
4713638Sbillm 
472789Sahrens out:
4732237Smaybee 	zfs_range_unlock(rl);
474789Sahrens 
475789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
476789Sahrens 	ZFS_EXIT(zfsvfs);
477789Sahrens 	return (error);
478789Sahrens }
479789Sahrens 
480789Sahrens /*
481789Sahrens  * Fault in the pages of the first n bytes specified by the uio structure.
482789Sahrens  * 1 byte in each page is touched and the uio struct is unmodified.
483789Sahrens  * Any error will exit this routine as this is only a best
484789Sahrens  * attempt to get the pages resident. This is a copy of ufs_trans_touch().
485789Sahrens  */
486789Sahrens static void
487789Sahrens zfs_prefault_write(ssize_t n, struct uio *uio)
488789Sahrens {
489789Sahrens 	struct iovec *iov;
490789Sahrens 	ulong_t cnt, incr;
491789Sahrens 	caddr_t p;
492789Sahrens 	uint8_t tmp;
493789Sahrens 
494789Sahrens 	iov = uio->uio_iov;
495789Sahrens 
496789Sahrens 	while (n) {
497789Sahrens 		cnt = MIN(iov->iov_len, n);
498789Sahrens 		if (cnt == 0) {
499789Sahrens 			/* empty iov entry */
500789Sahrens 			iov++;
501789Sahrens 			continue;
502789Sahrens 		}
503789Sahrens 		n -= cnt;
504789Sahrens 		/*
505789Sahrens 		 * touch each page in this segment.
506789Sahrens 		 */
507789Sahrens 		p = iov->iov_base;
508789Sahrens 		while (cnt) {
509789Sahrens 			switch (uio->uio_segflg) {
510789Sahrens 			case UIO_USERSPACE:
511789Sahrens 			case UIO_USERISPACE:
512789Sahrens 				if (fuword8(p, &tmp))
513789Sahrens 					return;
514789Sahrens 				break;
515789Sahrens 			case UIO_SYSSPACE:
516789Sahrens 				if (kcopy(p, &tmp, 1))
517789Sahrens 					return;
518789Sahrens 				break;
519789Sahrens 			}
520789Sahrens 			incr = MIN(cnt, PAGESIZE);
521789Sahrens 			p += incr;
522789Sahrens 			cnt -= incr;
523789Sahrens 		}
524789Sahrens 		/*
525789Sahrens 		 * touch the last byte in case it straddles a page.
526789Sahrens 		 */
527789Sahrens 		p--;
528789Sahrens 		switch (uio->uio_segflg) {
529789Sahrens 		case UIO_USERSPACE:
530789Sahrens 		case UIO_USERISPACE:
531789Sahrens 			if (fuword8(p, &tmp))
532789Sahrens 				return;
533789Sahrens 			break;
534789Sahrens 		case UIO_SYSSPACE:
535789Sahrens 			if (kcopy(p, &tmp, 1))
536789Sahrens 				return;
537789Sahrens 			break;
538789Sahrens 		}
539789Sahrens 		iov++;
540789Sahrens 	}
541789Sahrens }
542789Sahrens 
543789Sahrens /*
544789Sahrens  * Write the bytes to a file.
545789Sahrens  *
546789Sahrens  *	IN:	vp	- vnode of file to be written to.
547789Sahrens  *		uio	- structure supplying write location, range info,
548789Sahrens  *			  and data buffer.
549789Sahrens  *		ioflag	- FAPPEND flag set if in append mode.
550789Sahrens  *		cr	- credentials of caller.
551789Sahrens  *
552789Sahrens  *	OUT:	uio	- updated offset and range.
553789Sahrens  *
554789Sahrens  *	RETURN:	0 if success
555789Sahrens  *		error code if failure
556789Sahrens  *
557789Sahrens  * Timestamps:
558789Sahrens  *	vp - ctime|mtime updated if byte count > 0
559789Sahrens  */
560789Sahrens /* ARGSUSED */
561789Sahrens static int
562789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
563789Sahrens {
564789Sahrens 	znode_t		*zp = VTOZ(vp);
565789Sahrens 	rlim64_t	limit = uio->uio_llimit;
566789Sahrens 	ssize_t		start_resid = uio->uio_resid;
567789Sahrens 	ssize_t		tx_bytes;
568789Sahrens 	uint64_t	end_size;
569789Sahrens 	dmu_tx_t	*tx;
570789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
571789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
572789Sahrens 	offset_t	woff;
573789Sahrens 	ssize_t		n, nbytes;
5741669Sperrin 	rl_t		*rl;
575789Sahrens 	int		max_blksz = zfsvfs->z_max_blksz;
5761669Sperrin 	int		error;
577789Sahrens 
578789Sahrens 	/*
579789Sahrens 	 * Fasttrack empty write
580789Sahrens 	 */
5811669Sperrin 	n = start_resid;
582789Sahrens 	if (n == 0)
583789Sahrens 		return (0);
584789Sahrens 
5851669Sperrin 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
5861669Sperrin 		limit = MAXOFFSET_T;
5871669Sperrin 
588789Sahrens 	ZFS_ENTER(zfsvfs);
589789Sahrens 
590789Sahrens 	/*
5912237Smaybee 	 * Pre-fault the pages to ensure slow (eg NFS) pages
5921669Sperrin 	 * don't hold up txg.
593789Sahrens 	 */
5942237Smaybee 	zfs_prefault_write(n, uio);
595789Sahrens 
596789Sahrens 	/*
597789Sahrens 	 * If in append mode, set the io offset pointer to eof.
598789Sahrens 	 */
5991669Sperrin 	if (ioflag & FAPPEND) {
6001669Sperrin 		/*
6011669Sperrin 		 * Range lock for a file append:
6021669Sperrin 		 * The value for the start of range will be determined by
6031669Sperrin 		 * zfs_range_lock() (to guarantee append semantics).
6041669Sperrin 		 * If this write will cause the block size to increase,
6051669Sperrin 		 * zfs_range_lock() will lock the entire file, so we must
6061669Sperrin 		 * later reduce the range after we grow the block size.
6071669Sperrin 		 */
6081669Sperrin 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
6091669Sperrin 		if (rl->r_len == UINT64_MAX) {
6101669Sperrin 			/* overlocked, zp_size can't change */
6111669Sperrin 			woff = uio->uio_loffset = zp->z_phys->zp_size;
6121669Sperrin 		} else {
6131669Sperrin 			woff = uio->uio_loffset = rl->r_off;
6141669Sperrin 		}
615789Sahrens 	} else {
616789Sahrens 		woff = uio->uio_loffset;
617789Sahrens 		/*
618789Sahrens 		 * Validate file offset
619789Sahrens 		 */
620789Sahrens 		if (woff < 0) {
621789Sahrens 			ZFS_EXIT(zfsvfs);
622789Sahrens 			return (EINVAL);
623789Sahrens 		}
624789Sahrens 
625789Sahrens 		/*
6261669Sperrin 		 * If we need to grow the block size then zfs_range_lock()
6271669Sperrin 		 * will lock a wider range than we request here.
6281669Sperrin 		 * Later after growing the block size we reduce the range.
629789Sahrens 		 */
6301669Sperrin 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
631789Sahrens 	}
632789Sahrens 
633789Sahrens 	if (woff >= limit) {
6343638Sbillm 		zfs_range_unlock(rl);
6353638Sbillm 		ZFS_EXIT(zfsvfs);
6363638Sbillm 		return (EFBIG);
637789Sahrens 	}
638789Sahrens 
639789Sahrens 	if ((woff + n) > limit || woff > (limit - n))
640789Sahrens 		n = limit - woff;
641789Sahrens 
642789Sahrens 	/*
6431669Sperrin 	 * Check for mandatory locks
644789Sahrens 	 */
645789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) &&
6463638Sbillm 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
6473638Sbillm 		zfs_range_unlock(rl);
6483638Sbillm 		ZFS_EXIT(zfsvfs);
6493638Sbillm 		return (error);
6503638Sbillm 	}
6511669Sperrin 	end_size = MAX(zp->z_phys->zp_size, woff + n);
652789Sahrens 
6531669Sperrin 	/*
6543638Sbillm 	 * Write the file in reasonable size chunks.  Each chunk is written
6553638Sbillm 	 * in a separate transaction; this keeps the intent log records small
6563638Sbillm 	 * and allows us to do more fine-grained space accounting.
657789Sahrens 	 */
658789Sahrens 	while (n > 0) {
659789Sahrens 		/*
6603638Sbillm 		 * Start a transaction.
661789Sahrens 		 */
662789Sahrens 		woff = uio->uio_loffset;
663789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
664789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
665789Sahrens 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
666789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
667789Sahrens 		if (error) {
668789Sahrens 			if (error == ERESTART &&
669789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
6702113Sahrens 				dmu_tx_wait(tx);
6712113Sahrens 				dmu_tx_abort(tx);
6723638Sbillm 				continue;
673789Sahrens 			}
6742113Sahrens 			dmu_tx_abort(tx);
6753638Sbillm 			break;
6763638Sbillm 		}
6773638Sbillm 
6783638Sbillm 		/*
6793638Sbillm 		 * If zfs_range_lock() over-locked we grow the blocksize
6803638Sbillm 		 * and then reduce the lock range.  This will only happen
6813638Sbillm 		 * on the first iteration since zfs_range_reduce() will
6823638Sbillm 		 * shrink down r_len to the appropriate size.
6833638Sbillm 		 */
6843638Sbillm 		if (rl->r_len == UINT64_MAX) {
6853638Sbillm 			uint64_t new_blksz;
6863638Sbillm 
6873638Sbillm 			if (zp->z_blksz > max_blksz) {
6883638Sbillm 				ASSERT(!ISP2(zp->z_blksz));
6893638Sbillm 				new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
6903638Sbillm 			} else {
6913638Sbillm 				new_blksz = MIN(end_size, max_blksz);
6923638Sbillm 			}
6933638Sbillm 			zfs_grow_blocksize(zp, new_blksz, tx);
6943638Sbillm 			zfs_range_reduce(rl, woff, n);
6953638Sbillm 		}
6963638Sbillm 
6973638Sbillm 		/*
6983638Sbillm 		 * XXX - should we really limit each write to z_max_blksz?
6993638Sbillm 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
7003638Sbillm 		 */
7013638Sbillm 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
7023638Sbillm 		rw_enter(&zp->z_map_lock, RW_READER);
7033638Sbillm 
7043638Sbillm 		tx_bytes = uio->uio_resid;
7053638Sbillm 		if (vn_has_cached_data(vp)) {
7063638Sbillm 			rw_exit(&zp->z_map_lock);
7073638Sbillm 			error = mappedwrite(vp, nbytes, uio, tx);
7083638Sbillm 		} else {
7093638Sbillm 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
7103638Sbillm 			    uio, nbytes, tx);
7113638Sbillm 			rw_exit(&zp->z_map_lock);
712789Sahrens 		}
7133638Sbillm 		tx_bytes -= uio->uio_resid;
7143638Sbillm 
7153638Sbillm 		/*
7163638Sbillm 		 * If we made no progress, we're done.  If we made even
7173638Sbillm 		 * partial progress, update the znode and ZIL accordingly.
7183638Sbillm 		 */
7193638Sbillm 		if (tx_bytes == 0) {
7203897Smaybee 			dmu_tx_commit(tx);
7213638Sbillm 			ASSERT(error != 0);
7223638Sbillm 			break;
7233638Sbillm 		}
7243638Sbillm 
725789Sahrens 		/*
7263638Sbillm 		 * Clear Set-UID/Set-GID bits on successful write if not
7273638Sbillm 		 * privileged and at least one of the excute bits is set.
7283638Sbillm 		 *
7293638Sbillm 		 * It would be nice to to this after all writes have
7303638Sbillm 		 * been done, but that would still expose the ISUID/ISGID
7313638Sbillm 		 * to another app after the partial write is committed.
732789Sahrens 		 */
7333638Sbillm 		mutex_enter(&zp->z_acl_lock);
7343638Sbillm 		if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) |
7353638Sbillm 		    (S_IXUSR >> 6))) != 0 &&
7363638Sbillm 		    (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 &&
7373638Sbillm 		    secpolicy_vnode_setid_retain(cr,
7383638Sbillm 		    (zp->z_phys->zp_mode & S_ISUID) != 0 &&
7393638Sbillm 		    zp->z_phys->zp_uid == 0) != 0) {
7404339Sperrin 			zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID);
7413638Sbillm 		}
7423638Sbillm 		mutex_exit(&zp->z_acl_lock);
7433638Sbillm 
7443638Sbillm 		/*
7453638Sbillm 		 * Update time stamp.  NOTE: This marks the bonus buffer as
7463638Sbillm 		 * dirty, so we don't have to do it again for zp_size.
7473638Sbillm 		 */
7483638Sbillm 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
7493638Sbillm 
7503638Sbillm 		/*
7513638Sbillm 		 * Update the file size (zp_size) if it has changed;
7523638Sbillm 		 * account for possible concurrent updates.
7533638Sbillm 		 */
7543638Sbillm 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset)
755789Sahrens 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
756789Sahrens 			    uio->uio_loffset);
7573638Sbillm 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
7583638Sbillm 		dmu_tx_commit(tx);
7593638Sbillm 
7603638Sbillm 		if (error != 0)
7613638Sbillm 			break;
7623638Sbillm 		ASSERT(tx_bytes == nbytes);
7633638Sbillm 		n -= nbytes;
764789Sahrens 	}
765789Sahrens 
7662237Smaybee 	zfs_range_unlock(rl);
767789Sahrens 
768789Sahrens 	/*
769789Sahrens 	 * If we're in replay mode, or we made no progress, return error.
770789Sahrens 	 * Otherwise, it's at least a partial write, so it's successful.
771789Sahrens 	 */
772789Sahrens 	if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) {
773789Sahrens 		ZFS_EXIT(zfsvfs);
774789Sahrens 		return (error);
775789Sahrens 	}
776789Sahrens 
7772638Sperrin 	if (ioflag & (FSYNC | FDSYNC))
7782638Sperrin 		zil_commit(zilog, zp->z_last_itx, zp->z_id);
779789Sahrens 
780789Sahrens 	ZFS_EXIT(zfsvfs);
781789Sahrens 	return (0);
782789Sahrens }
783789Sahrens 
7842237Smaybee void
7853063Sperrin zfs_get_done(dmu_buf_t *db, void *vzgd)
7862237Smaybee {
7873063Sperrin 	zgd_t *zgd = (zgd_t *)vzgd;
7883063Sperrin 	rl_t *rl = zgd->zgd_rl;
7892237Smaybee 	vnode_t *vp = ZTOV(rl->r_zp);
7902237Smaybee 
7913063Sperrin 	dmu_buf_rele(db, vzgd);
7922237Smaybee 	zfs_range_unlock(rl);
7932237Smaybee 	VN_RELE(vp);
7943063Sperrin 	zil_add_vdev(zgd->zgd_zilog, DVA_GET_VDEV(BP_IDENTITY(zgd->zgd_bp)));
7953063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
7962237Smaybee }
7972237Smaybee 
798789Sahrens /*
799789Sahrens  * Get data to generate a TX_WRITE intent log record.
800789Sahrens  */
801789Sahrens int
8022237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
803789Sahrens {
804789Sahrens 	zfsvfs_t *zfsvfs = arg;
805789Sahrens 	objset_t *os = zfsvfs->z_os;
806789Sahrens 	znode_t *zp;
807789Sahrens 	uint64_t off = lr->lr_offset;
8082237Smaybee 	dmu_buf_t *db;
8091669Sperrin 	rl_t *rl;
8103063Sperrin 	zgd_t *zgd;
8113638Sbillm 	int dlen = lr->lr_length;		/* length of user data */
812789Sahrens 	int error = 0;
813789Sahrens 
8143063Sperrin 	ASSERT(zio);
815789Sahrens 	ASSERT(dlen != 0);
816789Sahrens 
817789Sahrens 	/*
8181669Sperrin 	 * Nothing to do if the file has been removed
819789Sahrens 	 */
820789Sahrens 	if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0)
821789Sahrens 		return (ENOENT);
8223461Sahrens 	if (zp->z_unlinked) {
823789Sahrens 		VN_RELE(ZTOV(zp));
824789Sahrens 		return (ENOENT);
825789Sahrens 	}
826789Sahrens 
827789Sahrens 	/*
828789Sahrens 	 * Write records come in two flavors: immediate and indirect.
829789Sahrens 	 * For small writes it's cheaper to store the data with the
830789Sahrens 	 * log record (immediate); for large writes it's cheaper to
831789Sahrens 	 * sync the data and get a pointer to it (indirect) so that
832789Sahrens 	 * we don't have to write the data twice.
833789Sahrens 	 */
8341669Sperrin 	if (buf != NULL) { /* immediate write */
8351669Sperrin 		rl = zfs_range_lock(zp, off, dlen, RL_READER);
8361669Sperrin 		/* test for truncation needs to be done while range locked */
8371669Sperrin 		if (off >= zp->z_phys->zp_size) {
8381669Sperrin 			error = ENOENT;
8391669Sperrin 			goto out;
8401669Sperrin 		}
8412449Smaybee 		VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf));
8421669Sperrin 	} else { /* indirect write */
8431669Sperrin 		uint64_t boff; /* block starting offset */
8441669Sperrin 
845789Sahrens 		/*
8461669Sperrin 		 * Have to lock the whole block to ensure when it's
8471669Sperrin 		 * written out and it's checksum is being calculated
8481669Sperrin 		 * that no one can change the data. We need to re-check
8491669Sperrin 		 * blocksize after we get the lock in case it's changed!
850789Sahrens 		 */
8511669Sperrin 		for (;;) {
8521941Sperrin 			if (ISP2(zp->z_blksz)) {
8531941Sperrin 				boff = P2ALIGN_TYPED(off, zp->z_blksz,
8541941Sperrin 				    uint64_t);
8551941Sperrin 			} else {
8561941Sperrin 				boff = 0;
8571941Sperrin 			}
8581669Sperrin 			dlen = zp->z_blksz;
8591669Sperrin 			rl = zfs_range_lock(zp, boff, dlen, RL_READER);
8601669Sperrin 			if (zp->z_blksz == dlen)
8611669Sperrin 				break;
8622237Smaybee 			zfs_range_unlock(rl);
8631669Sperrin 		}
8641669Sperrin 		/* test for truncation needs to be done while range locked */
8651669Sperrin 		if (off >= zp->z_phys->zp_size) {
8661669Sperrin 			error = ENOENT;
8671669Sperrin 			goto out;
8681669Sperrin 		}
8693063Sperrin 		zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
8703063Sperrin 		zgd->zgd_rl = rl;
8713063Sperrin 		zgd->zgd_zilog = zfsvfs->z_log;
8723063Sperrin 		zgd->zgd_bp = &lr->lr_blkptr;
8733063Sperrin 		VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db));
8742237Smaybee 		ASSERT(boff == db->db_offset);
8752237Smaybee 		lr->lr_blkoff = off - boff;
8762237Smaybee 		error = dmu_sync(zio, db, &lr->lr_blkptr,
8773063Sperrin 		    lr->lr_common.lrc_txg, zfs_get_done, zgd);
8784709Smaybee 		ASSERT((error && error != EINPROGRESS) ||
8794709Smaybee 		    lr->lr_length <= zp->z_blksz);
8803063Sperrin 		if (error == 0) {
8813063Sperrin 			zil_add_vdev(zfsvfs->z_log,
8823063Sperrin 			    DVA_GET_VDEV(BP_IDENTITY(&lr->lr_blkptr)));
8833063Sperrin 		}
8842237Smaybee 		/*
8852237Smaybee 		 * If we get EINPROGRESS, then we need to wait for a
8862237Smaybee 		 * write IO initiated by dmu_sync() to complete before
8872638Sperrin 		 * we can release this dbuf.  We will finish everything
8882237Smaybee 		 * up in the zfs_get_done() callback.
8892237Smaybee 		 */
8902237Smaybee 		if (error == EINPROGRESS)
8912237Smaybee 			return (0);
8923063Sperrin 		dmu_buf_rele(db, zgd);
8933063Sperrin 		kmem_free(zgd, sizeof (zgd_t));
894789Sahrens 	}
8951669Sperrin out:
8962237Smaybee 	zfs_range_unlock(rl);
897789Sahrens 	VN_RELE(ZTOV(zp));
898789Sahrens 	return (error);
899789Sahrens }
900789Sahrens 
901789Sahrens /*ARGSUSED*/
902789Sahrens static int
903789Sahrens zfs_access(vnode_t *vp, int mode, int flags, cred_t *cr)
904789Sahrens {
905789Sahrens 	znode_t *zp = VTOZ(vp);
906789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
907789Sahrens 	int error;
908789Sahrens 
909789Sahrens 	ZFS_ENTER(zfsvfs);
910789Sahrens 	error = zfs_zaccess_rwx(zp, mode, cr);
911789Sahrens 	ZFS_EXIT(zfsvfs);
912789Sahrens 	return (error);
913789Sahrens }
914789Sahrens 
915789Sahrens /*
916789Sahrens  * Lookup an entry in a directory, or an extended attribute directory.
917789Sahrens  * If it exists, return a held vnode reference for it.
918789Sahrens  *
919789Sahrens  *	IN:	dvp	- vnode of directory to search.
920789Sahrens  *		nm	- name of entry to lookup.
921789Sahrens  *		pnp	- full pathname to lookup [UNUSED].
922789Sahrens  *		flags	- LOOKUP_XATTR set if looking for an attribute.
923789Sahrens  *		rdir	- root directory vnode [UNUSED].
924789Sahrens  *		cr	- credentials of caller.
925789Sahrens  *
926789Sahrens  *	OUT:	vpp	- vnode of located entry, NULL if not found.
927789Sahrens  *
928789Sahrens  *	RETURN:	0 if success
929789Sahrens  *		error code if failure
930789Sahrens  *
931789Sahrens  * Timestamps:
932789Sahrens  *	NA
933789Sahrens  */
934789Sahrens /* ARGSUSED */
935789Sahrens static int
936789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
937789Sahrens     int flags, vnode_t *rdir, cred_t *cr)
938789Sahrens {
939789Sahrens 
940789Sahrens 	znode_t *zdp = VTOZ(dvp);
941789Sahrens 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
942789Sahrens 	int	error;
943789Sahrens 
944789Sahrens 	ZFS_ENTER(zfsvfs);
945789Sahrens 
946789Sahrens 	*vpp = NULL;
947789Sahrens 
948789Sahrens 	if (flags & LOOKUP_XATTR) {
949789Sahrens 		/*
9503234Sck153898 		 * If the xattr property is off, refuse the lookup request.
9513234Sck153898 		 */
9523234Sck153898 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
9533234Sck153898 			ZFS_EXIT(zfsvfs);
9543234Sck153898 			return (EINVAL);
9553234Sck153898 		}
9563234Sck153898 
9573234Sck153898 		/*
958789Sahrens 		 * We don't allow recursive attributes..
959789Sahrens 		 * Maybe someday we will.
960789Sahrens 		 */
961789Sahrens 		if (zdp->z_phys->zp_flags & ZFS_XATTR) {
962789Sahrens 			ZFS_EXIT(zfsvfs);
963789Sahrens 			return (EINVAL);
964789Sahrens 		}
965789Sahrens 
9663280Sck153898 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
967789Sahrens 			ZFS_EXIT(zfsvfs);
968789Sahrens 			return (error);
969789Sahrens 		}
970789Sahrens 
971789Sahrens 		/*
972789Sahrens 		 * Do we have permission to get into attribute directory?
973789Sahrens 		 */
974789Sahrens 
975789Sahrens 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, cr)) {
976789Sahrens 			VN_RELE(*vpp);
977789Sahrens 		}
978789Sahrens 
979789Sahrens 		ZFS_EXIT(zfsvfs);
980789Sahrens 		return (error);
981789Sahrens 	}
982789Sahrens 
9831512Sek110237 	if (dvp->v_type != VDIR) {
9841512Sek110237 		ZFS_EXIT(zfsvfs);
9851460Smarks 		return (ENOTDIR);
9861512Sek110237 	}
9871460Smarks 
988789Sahrens 	/*
989789Sahrens 	 * Check accessibility of directory.
990789Sahrens 	 */
991789Sahrens 
992789Sahrens 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, cr)) {
993789Sahrens 		ZFS_EXIT(zfsvfs);
994789Sahrens 		return (error);
995789Sahrens 	}
996789Sahrens 
997789Sahrens 	if ((error = zfs_dirlook(zdp, nm, vpp)) == 0) {
998789Sahrens 
999789Sahrens 		/*
1000789Sahrens 		 * Convert device special files
1001789Sahrens 		 */
1002789Sahrens 		if (IS_DEVVP(*vpp)) {
1003789Sahrens 			vnode_t	*svp;
1004789Sahrens 
1005789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1006789Sahrens 			VN_RELE(*vpp);
1007789Sahrens 			if (svp == NULL)
1008789Sahrens 				error = ENOSYS;
1009789Sahrens 			else
1010789Sahrens 				*vpp = svp;
1011789Sahrens 		}
1012789Sahrens 	}
1013789Sahrens 
1014789Sahrens 	ZFS_EXIT(zfsvfs);
1015789Sahrens 	return (error);
1016789Sahrens }
1017789Sahrens 
1018789Sahrens /*
1019789Sahrens  * Attempt to create a new entry in a directory.  If the entry
1020789Sahrens  * already exists, truncate the file if permissible, else return
1021789Sahrens  * an error.  Return the vp of the created or trunc'd file.
1022789Sahrens  *
1023789Sahrens  *	IN:	dvp	- vnode of directory to put new file entry in.
1024789Sahrens  *		name	- name of new file entry.
1025789Sahrens  *		vap	- attributes of new file.
1026789Sahrens  *		excl	- flag indicating exclusive or non-exclusive mode.
1027789Sahrens  *		mode	- mode to open file with.
1028789Sahrens  *		cr	- credentials of caller.
1029789Sahrens  *		flag	- large file flag [UNUSED].
1030789Sahrens  *
1031789Sahrens  *	OUT:	vpp	- vnode of created or trunc'd entry.
1032789Sahrens  *
1033789Sahrens  *	RETURN:	0 if success
1034789Sahrens  *		error code if failure
1035789Sahrens  *
1036789Sahrens  * Timestamps:
1037789Sahrens  *	dvp - ctime|mtime updated if new entry created
1038789Sahrens  *	 vp - ctime|mtime always, atime if new
1039789Sahrens  */
1040789Sahrens /* ARGSUSED */
1041789Sahrens static int
1042789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1043789Sahrens     int mode, vnode_t **vpp, cred_t *cr, int flag)
1044789Sahrens {
1045789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1046789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1047789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1048789Sahrens 	objset_t	*os = zfsvfs->z_os;
1049789Sahrens 	zfs_dirlock_t	*dl;
1050789Sahrens 	dmu_tx_t	*tx;
1051789Sahrens 	int		error;
1052789Sahrens 	uint64_t	zoid;
1053789Sahrens 
1054789Sahrens 	ZFS_ENTER(zfsvfs);
1055789Sahrens 
1056789Sahrens top:
1057789Sahrens 	*vpp = NULL;
1058789Sahrens 
1059789Sahrens 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1060789Sahrens 		vap->va_mode &= ~VSVTX;
1061789Sahrens 
1062789Sahrens 	if (*name == '\0') {
1063789Sahrens 		/*
1064789Sahrens 		 * Null component name refers to the directory itself.
1065789Sahrens 		 */
1066789Sahrens 		VN_HOLD(dvp);
1067789Sahrens 		zp = dzp;
1068789Sahrens 		dl = NULL;
1069789Sahrens 		error = 0;
1070789Sahrens 	} else {
1071789Sahrens 		/* possible VN_HOLD(zp) */
1072789Sahrens 		if (error = zfs_dirent_lock(&dl, dzp, name, &zp, 0)) {
1073789Sahrens 			if (strcmp(name, "..") == 0)
1074789Sahrens 				error = EISDIR;
1075789Sahrens 			ZFS_EXIT(zfsvfs);
1076789Sahrens 			return (error);
1077789Sahrens 		}
1078789Sahrens 	}
1079789Sahrens 
1080789Sahrens 	zoid = zp ? zp->z_id : -1ULL;
1081789Sahrens 
1082789Sahrens 	if (zp == NULL) {
1083789Sahrens 		/*
1084789Sahrens 		 * Create a new file object and update the directory
1085789Sahrens 		 * to reference it.
1086789Sahrens 		 */
1087789Sahrens 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
1088789Sahrens 			goto out;
1089789Sahrens 		}
1090789Sahrens 
1091789Sahrens 		/*
1092789Sahrens 		 * We only support the creation of regular files in
1093789Sahrens 		 * extended attribute directories.
1094789Sahrens 		 */
1095789Sahrens 		if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1096789Sahrens 		    (vap->va_type != VREG)) {
1097789Sahrens 			error = EINVAL;
1098789Sahrens 			goto out;
1099789Sahrens 		}
1100789Sahrens 
1101789Sahrens 		tx = dmu_tx_create(os);
1102789Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1103789Sahrens 		dmu_tx_hold_bonus(tx, dzp->z_id);
11041544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1105789Sahrens 		if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
1106789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1107789Sahrens 			    0, SPA_MAXBLOCKSIZE);
1108789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
1109789Sahrens 		if (error) {
1110789Sahrens 			zfs_dirent_unlock(dl);
1111789Sahrens 			if (error == ERESTART &&
1112789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
11132113Sahrens 				dmu_tx_wait(tx);
11142113Sahrens 				dmu_tx_abort(tx);
1115789Sahrens 				goto top;
1116789Sahrens 			}
11172113Sahrens 			dmu_tx_abort(tx);
1118789Sahrens 			ZFS_EXIT(zfsvfs);
1119789Sahrens 			return (error);
1120789Sahrens 		}
1121789Sahrens 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
1122789Sahrens 		ASSERT(zp->z_id == zoid);
1123789Sahrens 		(void) zfs_link_create(dl, zp, tx, ZNEW);
11242638Sperrin 		zfs_log_create(zilog, tx, TX_CREATE, dzp, zp, name);
1125789Sahrens 		dmu_tx_commit(tx);
1126789Sahrens 	} else {
1127789Sahrens 		/*
1128789Sahrens 		 * A directory entry already exists for this name.
1129789Sahrens 		 */
1130789Sahrens 		/*
1131789Sahrens 		 * Can't truncate an existing file if in exclusive mode.
1132789Sahrens 		 */
1133789Sahrens 		if (excl == EXCL) {
1134789Sahrens 			error = EEXIST;
1135789Sahrens 			goto out;
1136789Sahrens 		}
1137789Sahrens 		/*
1138789Sahrens 		 * Can't open a directory for writing.
1139789Sahrens 		 */
1140789Sahrens 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1141789Sahrens 			error = EISDIR;
1142789Sahrens 			goto out;
1143789Sahrens 		}
1144789Sahrens 		/*
1145789Sahrens 		 * Verify requested access to file.
1146789Sahrens 		 */
1147789Sahrens 		if (mode && (error = zfs_zaccess_rwx(zp, mode, cr))) {
1148789Sahrens 			goto out;
1149789Sahrens 		}
1150789Sahrens 
1151789Sahrens 		mutex_enter(&dzp->z_lock);
1152789Sahrens 		dzp->z_seq++;
1153789Sahrens 		mutex_exit(&dzp->z_lock);
1154789Sahrens 
11551878Smaybee 		/*
11561878Smaybee 		 * Truncate regular files if requested.
11571878Smaybee 		 */
11581878Smaybee 		if ((ZTOV(zp)->v_type == VREG) &&
1159789Sahrens 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
11601878Smaybee 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
11611878Smaybee 			if (error == ERESTART &&
11621878Smaybee 			    zfsvfs->z_assign == TXG_NOWAIT) {
11632113Sahrens 				/* NB: we already did dmu_tx_wait() */
11641878Smaybee 				zfs_dirent_unlock(dl);
11652365Sperrin 				VN_RELE(ZTOV(zp));
11661878Smaybee 				goto top;
1167789Sahrens 			}
1168789Sahrens 		}
1169789Sahrens 	}
1170789Sahrens out:
1171789Sahrens 
1172789Sahrens 	if (dl)
1173789Sahrens 		zfs_dirent_unlock(dl);
1174789Sahrens 
1175789Sahrens 	if (error) {
1176789Sahrens 		if (zp)
1177789Sahrens 			VN_RELE(ZTOV(zp));
1178789Sahrens 	} else {
1179789Sahrens 		*vpp = ZTOV(zp);
1180789Sahrens 		/*
1181789Sahrens 		 * If vnode is for a device return a specfs vnode instead.
1182789Sahrens 		 */
1183789Sahrens 		if (IS_DEVVP(*vpp)) {
1184789Sahrens 			struct vnode *svp;
1185789Sahrens 
1186789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1187789Sahrens 			VN_RELE(*vpp);
1188789Sahrens 			if (svp == NULL) {
1189789Sahrens 				error = ENOSYS;
1190789Sahrens 			}
1191789Sahrens 			*vpp = svp;
1192789Sahrens 		}
1193789Sahrens 	}
1194789Sahrens 
1195789Sahrens 	ZFS_EXIT(zfsvfs);
1196789Sahrens 	return (error);
1197789Sahrens }
1198789Sahrens 
1199789Sahrens /*
1200789Sahrens  * Remove an entry from a directory.
1201789Sahrens  *
1202789Sahrens  *	IN:	dvp	- vnode of directory to remove entry from.
1203789Sahrens  *		name	- name of entry to remove.
1204789Sahrens  *		cr	- credentials of caller.
1205789Sahrens  *
1206789Sahrens  *	RETURN:	0 if success
1207789Sahrens  *		error code if failure
1208789Sahrens  *
1209789Sahrens  * Timestamps:
1210789Sahrens  *	dvp - ctime|mtime
1211789Sahrens  *	 vp - ctime (if nlink > 0)
1212789Sahrens  */
1213789Sahrens static int
1214789Sahrens zfs_remove(vnode_t *dvp, char *name, cred_t *cr)
1215789Sahrens {
1216789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1217789Sahrens 	znode_t		*xzp = NULL;
1218789Sahrens 	vnode_t		*vp;
1219789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1220789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1221789Sahrens 	uint64_t	acl_obj, xattr_obj;
1222789Sahrens 	zfs_dirlock_t	*dl;
1223789Sahrens 	dmu_tx_t	*tx;
12243461Sahrens 	boolean_t	may_delete_now, delete_now = FALSE;
12253461Sahrens 	boolean_t	unlinked;
1226789Sahrens 	int		error;
1227789Sahrens 
1228789Sahrens 	ZFS_ENTER(zfsvfs);
1229789Sahrens 
1230789Sahrens top:
1231789Sahrens 	/*
1232789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1233789Sahrens 	 */
1234789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
1235789Sahrens 		ZFS_EXIT(zfsvfs);
1236789Sahrens 		return (error);
1237789Sahrens 	}
1238789Sahrens 
1239789Sahrens 	vp = ZTOV(zp);
1240789Sahrens 
1241789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1242789Sahrens 		goto out;
1243789Sahrens 	}
1244789Sahrens 
1245789Sahrens 	/*
1246789Sahrens 	 * Need to use rmdir for removing directories.
1247789Sahrens 	 */
1248789Sahrens 	if (vp->v_type == VDIR) {
1249789Sahrens 		error = EPERM;
1250789Sahrens 		goto out;
1251789Sahrens 	}
1252789Sahrens 
1253789Sahrens 	vnevent_remove(vp);
1254789Sahrens 
12551484Sek110237 	dnlc_remove(dvp, name);
12561484Sek110237 
1257789Sahrens 	mutex_enter(&vp->v_lock);
1258789Sahrens 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1259789Sahrens 	mutex_exit(&vp->v_lock);
1260789Sahrens 
1261789Sahrens 	/*
12623461Sahrens 	 * We may delete the znode now, or we may put it in the unlinked set;
1263789Sahrens 	 * it depends on whether we're the last link, and on whether there are
1264789Sahrens 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1265789Sahrens 	 * allow for either case.
1266789Sahrens 	 */
1267789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
12681544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1269789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
1270789Sahrens 	if (may_delete_now)
1271789Sahrens 		dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
1272789Sahrens 
1273789Sahrens 	/* are there any extended attributes? */
1274789Sahrens 	if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1275789Sahrens 		/* XXX - do we need this if we are deleting? */
1276789Sahrens 		dmu_tx_hold_bonus(tx, xattr_obj);
1277789Sahrens 	}
1278789Sahrens 
1279789Sahrens 	/* are there any additional acls */
1280789Sahrens 	if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1281789Sahrens 	    may_delete_now)
1282789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1283789Sahrens 
1284789Sahrens 	/* charge as an update -- would be nice not to charge at all */
12853461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1286789Sahrens 
1287789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1288789Sahrens 	if (error) {
1289789Sahrens 		zfs_dirent_unlock(dl);
1290789Sahrens 		VN_RELE(vp);
1291789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
12922113Sahrens 			dmu_tx_wait(tx);
12932113Sahrens 			dmu_tx_abort(tx);
1294789Sahrens 			goto top;
1295789Sahrens 		}
12962113Sahrens 		dmu_tx_abort(tx);
1297789Sahrens 		ZFS_EXIT(zfsvfs);
1298789Sahrens 		return (error);
1299789Sahrens 	}
1300789Sahrens 
1301789Sahrens 	/*
1302789Sahrens 	 * Remove the directory entry.
1303789Sahrens 	 */
13043461Sahrens 	error = zfs_link_destroy(dl, zp, tx, 0, &unlinked);
1305789Sahrens 
1306789Sahrens 	if (error) {
1307789Sahrens 		dmu_tx_commit(tx);
1308789Sahrens 		goto out;
1309789Sahrens 	}
1310789Sahrens 
13113461Sahrens 	if (unlinked) {
1312789Sahrens 		mutex_enter(&vp->v_lock);
1313789Sahrens 		delete_now = may_delete_now &&
1314789Sahrens 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1315789Sahrens 		    zp->z_phys->zp_xattr == xattr_obj &&
1316789Sahrens 		    zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1317789Sahrens 		mutex_exit(&vp->v_lock);
1318789Sahrens 	}
1319789Sahrens 
1320789Sahrens 	if (delete_now) {
1321789Sahrens 		if (zp->z_phys->zp_xattr) {
1322789Sahrens 			error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1323789Sahrens 			ASSERT3U(error, ==, 0);
1324789Sahrens 			ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1325789Sahrens 			dmu_buf_will_dirty(xzp->z_dbuf, tx);
1326789Sahrens 			mutex_enter(&xzp->z_lock);
13273461Sahrens 			xzp->z_unlinked = 1;
1328789Sahrens 			xzp->z_phys->zp_links = 0;
1329789Sahrens 			mutex_exit(&xzp->z_lock);
13303461Sahrens 			zfs_unlinked_add(xzp, tx);
1331789Sahrens 			zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1332789Sahrens 		}
1333789Sahrens 		mutex_enter(&zp->z_lock);
1334789Sahrens 		mutex_enter(&vp->v_lock);
1335789Sahrens 		vp->v_count--;
1336789Sahrens 		ASSERT3U(vp->v_count, ==, 0);
1337789Sahrens 		mutex_exit(&vp->v_lock);
1338789Sahrens 		mutex_exit(&zp->z_lock);
1339789Sahrens 		zfs_znode_delete(zp, tx);
1340789Sahrens 		VFS_RELE(zfsvfs->z_vfs);
13413461Sahrens 	} else if (unlinked) {
13423461Sahrens 		zfs_unlinked_add(zp, tx);
1343789Sahrens 	}
1344789Sahrens 
13452638Sperrin 	zfs_log_remove(zilog, tx, TX_REMOVE, dzp, name);
1346789Sahrens 
1347789Sahrens 	dmu_tx_commit(tx);
1348789Sahrens out:
1349789Sahrens 	zfs_dirent_unlock(dl);
1350789Sahrens 
1351789Sahrens 	if (!delete_now) {
1352789Sahrens 		VN_RELE(vp);
1353789Sahrens 	} else if (xzp) {
1354789Sahrens 		/* this rele delayed to prevent nesting transactions */
1355789Sahrens 		VN_RELE(ZTOV(xzp));
1356789Sahrens 	}
1357789Sahrens 
1358789Sahrens 	ZFS_EXIT(zfsvfs);
1359789Sahrens 	return (error);
1360789Sahrens }
1361789Sahrens 
1362789Sahrens /*
1363789Sahrens  * Create a new directory and insert it into dvp using the name
1364789Sahrens  * provided.  Return a pointer to the inserted directory.
1365789Sahrens  *
1366789Sahrens  *	IN:	dvp	- vnode of directory to add subdir to.
1367789Sahrens  *		dirname	- name of new directory.
1368789Sahrens  *		vap	- attributes of new directory.
1369789Sahrens  *		cr	- credentials of caller.
1370789Sahrens  *
1371789Sahrens  *	OUT:	vpp	- vnode of created directory.
1372789Sahrens  *
1373789Sahrens  *	RETURN:	0 if success
1374789Sahrens  *		error code if failure
1375789Sahrens  *
1376789Sahrens  * Timestamps:
1377789Sahrens  *	dvp - ctime|mtime updated
1378789Sahrens  *	 vp - ctime|mtime|atime updated
1379789Sahrens  */
1380789Sahrens static int
1381789Sahrens zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr)
1382789Sahrens {
1383789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1384789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1385789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1386789Sahrens 	zfs_dirlock_t	*dl;
1387789Sahrens 	uint64_t	zoid = 0;
1388789Sahrens 	dmu_tx_t	*tx;
1389789Sahrens 	int		error;
1390789Sahrens 
1391789Sahrens 	ASSERT(vap->va_type == VDIR);
1392789Sahrens 
1393789Sahrens 	ZFS_ENTER(zfsvfs);
1394789Sahrens 
1395789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1396789Sahrens 		ZFS_EXIT(zfsvfs);
1397789Sahrens 		return (EINVAL);
1398789Sahrens 	}
1399789Sahrens top:
1400789Sahrens 	*vpp = NULL;
1401789Sahrens 
1402789Sahrens 	/*
1403789Sahrens 	 * First make sure the new directory doesn't exist.
1404789Sahrens 	 */
1405789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, ZNEW)) {
1406789Sahrens 		ZFS_EXIT(zfsvfs);
1407789Sahrens 		return (error);
1408789Sahrens 	}
1409789Sahrens 
14101231Smarks 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, cr)) {
14111231Smarks 		zfs_dirent_unlock(dl);
14121231Smarks 		ZFS_EXIT(zfsvfs);
14131231Smarks 		return (error);
14141231Smarks 	}
14151231Smarks 
1416789Sahrens 	/*
1417789Sahrens 	 * Add a new entry to the directory.
1418789Sahrens 	 */
1419789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
14201544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
14211544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1422789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
1423789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1424789Sahrens 		    0, SPA_MAXBLOCKSIZE);
1425789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1426789Sahrens 	if (error) {
1427789Sahrens 		zfs_dirent_unlock(dl);
1428789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
14292113Sahrens 			dmu_tx_wait(tx);
14302113Sahrens 			dmu_tx_abort(tx);
1431789Sahrens 			goto top;
1432789Sahrens 		}
14332113Sahrens 		dmu_tx_abort(tx);
1434789Sahrens 		ZFS_EXIT(zfsvfs);
1435789Sahrens 		return (error);
1436789Sahrens 	}
1437789Sahrens 
1438789Sahrens 	/*
1439789Sahrens 	 * Create new node.
1440789Sahrens 	 */
1441789Sahrens 	zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
1442789Sahrens 
1443789Sahrens 	/*
1444789Sahrens 	 * Now put new name in parent dir.
1445789Sahrens 	 */
1446789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1447789Sahrens 
1448789Sahrens 	*vpp = ZTOV(zp);
1449789Sahrens 
14502638Sperrin 	zfs_log_create(zilog, tx, TX_MKDIR, dzp, zp, dirname);
1451789Sahrens 	dmu_tx_commit(tx);
1452789Sahrens 
1453789Sahrens 	zfs_dirent_unlock(dl);
1454789Sahrens 
1455789Sahrens 	ZFS_EXIT(zfsvfs);
1456789Sahrens 	return (0);
1457789Sahrens }
1458789Sahrens 
1459789Sahrens /*
1460789Sahrens  * Remove a directory subdir entry.  If the current working
1461789Sahrens  * directory is the same as the subdir to be removed, the
1462789Sahrens  * remove will fail.
1463789Sahrens  *
1464789Sahrens  *	IN:	dvp	- vnode of directory to remove from.
1465789Sahrens  *		name	- name of directory to be removed.
1466789Sahrens  *		cwd	- vnode of current working directory.
1467789Sahrens  *		cr	- credentials of caller.
1468789Sahrens  *
1469789Sahrens  *	RETURN:	0 if success
1470789Sahrens  *		error code if failure
1471789Sahrens  *
1472789Sahrens  * Timestamps:
1473789Sahrens  *	dvp - ctime|mtime updated
1474789Sahrens  */
1475789Sahrens static int
1476789Sahrens zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr)
1477789Sahrens {
1478789Sahrens 	znode_t		*dzp = VTOZ(dvp);
1479789Sahrens 	znode_t		*zp;
1480789Sahrens 	vnode_t		*vp;
1481789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1482789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1483789Sahrens 	zfs_dirlock_t	*dl;
1484789Sahrens 	dmu_tx_t	*tx;
1485789Sahrens 	int		error;
1486789Sahrens 
1487789Sahrens 	ZFS_ENTER(zfsvfs);
1488789Sahrens 
1489789Sahrens top:
1490789Sahrens 	zp = NULL;
1491789Sahrens 
1492789Sahrens 	/*
1493789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1494789Sahrens 	 */
1495789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
1496789Sahrens 		ZFS_EXIT(zfsvfs);
1497789Sahrens 		return (error);
1498789Sahrens 	}
1499789Sahrens 
1500789Sahrens 	vp = ZTOV(zp);
1501789Sahrens 
1502789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1503789Sahrens 		goto out;
1504789Sahrens 	}
1505789Sahrens 
1506789Sahrens 	if (vp->v_type != VDIR) {
1507789Sahrens 		error = ENOTDIR;
1508789Sahrens 		goto out;
1509789Sahrens 	}
1510789Sahrens 
1511789Sahrens 	if (vp == cwd) {
1512789Sahrens 		error = EINVAL;
1513789Sahrens 		goto out;
1514789Sahrens 	}
1515789Sahrens 
1516789Sahrens 	vnevent_rmdir(vp);
1517789Sahrens 
1518789Sahrens 	/*
15193897Smaybee 	 * Grab a lock on the directory to make sure that noone is
15203897Smaybee 	 * trying to add (or lookup) entries while we are removing it.
15213897Smaybee 	 */
15223897Smaybee 	rw_enter(&zp->z_name_lock, RW_WRITER);
15233897Smaybee 
15243897Smaybee 	/*
15253897Smaybee 	 * Grab a lock on the parent pointer to make sure we play well
1526789Sahrens 	 * with the treewalk and directory rename code.
1527789Sahrens 	 */
1528789Sahrens 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1529789Sahrens 
1530789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
15311544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1532789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
15333461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1534789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1535789Sahrens 	if (error) {
1536789Sahrens 		rw_exit(&zp->z_parent_lock);
15373897Smaybee 		rw_exit(&zp->z_name_lock);
1538789Sahrens 		zfs_dirent_unlock(dl);
1539789Sahrens 		VN_RELE(vp);
1540789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
15412113Sahrens 			dmu_tx_wait(tx);
15422113Sahrens 			dmu_tx_abort(tx);
1543789Sahrens 			goto top;
1544789Sahrens 		}
15452113Sahrens 		dmu_tx_abort(tx);
1546789Sahrens 		ZFS_EXIT(zfsvfs);
1547789Sahrens 		return (error);
1548789Sahrens 	}
1549789Sahrens 
1550789Sahrens 	error = zfs_link_destroy(dl, zp, tx, 0, NULL);
1551789Sahrens 
1552789Sahrens 	if (error == 0)
15532638Sperrin 		zfs_log_remove(zilog, tx, TX_RMDIR, dzp, name);
1554789Sahrens 
1555789Sahrens 	dmu_tx_commit(tx);
1556789Sahrens 
1557789Sahrens 	rw_exit(&zp->z_parent_lock);
15583897Smaybee 	rw_exit(&zp->z_name_lock);
1559789Sahrens out:
1560789Sahrens 	zfs_dirent_unlock(dl);
1561789Sahrens 
1562789Sahrens 	VN_RELE(vp);
1563789Sahrens 
1564789Sahrens 	ZFS_EXIT(zfsvfs);
1565789Sahrens 	return (error);
1566789Sahrens }
1567789Sahrens 
1568789Sahrens /*
1569789Sahrens  * Read as many directory entries as will fit into the provided
1570789Sahrens  * buffer from the given directory cursor position (specified in
1571789Sahrens  * the uio structure.
1572789Sahrens  *
1573789Sahrens  *	IN:	vp	- vnode of directory to read.
1574789Sahrens  *		uio	- structure supplying read location, range info,
1575789Sahrens  *			  and return buffer.
1576789Sahrens  *		cr	- credentials of caller.
1577789Sahrens  *
1578789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
1579789Sahrens  *		eofp	- set to true if end-of-file detected.
1580789Sahrens  *
1581789Sahrens  *	RETURN:	0 if success
1582789Sahrens  *		error code if failure
1583789Sahrens  *
1584789Sahrens  * Timestamps:
1585789Sahrens  *	vp - atime updated
1586789Sahrens  *
1587789Sahrens  * Note that the low 4 bits of the cookie returned by zap is always zero.
1588789Sahrens  * This allows us to use the low range for "special" directory entries:
1589789Sahrens  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
1590789Sahrens  * we use the offset 2 for the '.zfs' directory.
1591789Sahrens  */
1592789Sahrens /* ARGSUSED */
1593789Sahrens static int
1594789Sahrens zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp)
1595789Sahrens {
1596789Sahrens 	znode_t		*zp = VTOZ(vp);
1597789Sahrens 	iovec_t		*iovp;
1598789Sahrens 	dirent64_t	*odp;
1599789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1600869Sperrin 	objset_t	*os;
1601789Sahrens 	caddr_t		outbuf;
1602789Sahrens 	size_t		bufsize;
1603789Sahrens 	zap_cursor_t	zc;
1604789Sahrens 	zap_attribute_t	zap;
1605789Sahrens 	uint_t		bytes_wanted;
1606789Sahrens 	uint64_t	offset; /* must be unsigned; checks for < 1 */
1607789Sahrens 	int		local_eof;
1608869Sperrin 	int		outcount;
1609869Sperrin 	int		error;
1610869Sperrin 	uint8_t		prefetch;
1611789Sahrens 
1612789Sahrens 	ZFS_ENTER(zfsvfs);
1613789Sahrens 
1614789Sahrens 	/*
1615789Sahrens 	 * If we are not given an eof variable,
1616789Sahrens 	 * use a local one.
1617789Sahrens 	 */
1618789Sahrens 	if (eofp == NULL)
1619789Sahrens 		eofp = &local_eof;
1620789Sahrens 
1621789Sahrens 	/*
1622789Sahrens 	 * Check for valid iov_len.
1623789Sahrens 	 */
1624789Sahrens 	if (uio->uio_iov->iov_len <= 0) {
1625789Sahrens 		ZFS_EXIT(zfsvfs);
1626789Sahrens 		return (EINVAL);
1627789Sahrens 	}
1628789Sahrens 
1629789Sahrens 	/*
1630789Sahrens 	 * Quit if directory has been removed (posix)
1631789Sahrens 	 */
16323461Sahrens 	if ((*eofp = zp->z_unlinked) != 0) {
1633789Sahrens 		ZFS_EXIT(zfsvfs);
1634789Sahrens 		return (0);
1635789Sahrens 	}
1636789Sahrens 
1637869Sperrin 	error = 0;
1638869Sperrin 	os = zfsvfs->z_os;
1639869Sperrin 	offset = uio->uio_loffset;
1640869Sperrin 	prefetch = zp->z_zn_prefetch;
1641869Sperrin 
1642789Sahrens 	/*
1643789Sahrens 	 * Initialize the iterator cursor.
1644789Sahrens 	 */
1645789Sahrens 	if (offset <= 3) {
1646789Sahrens 		/*
1647789Sahrens 		 * Start iteration from the beginning of the directory.
1648789Sahrens 		 */
1649869Sperrin 		zap_cursor_init(&zc, os, zp->z_id);
1650789Sahrens 	} else {
1651789Sahrens 		/*
1652789Sahrens 		 * The offset is a serialized cursor.
1653789Sahrens 		 */
1654869Sperrin 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1655789Sahrens 	}
1656789Sahrens 
1657789Sahrens 	/*
1658789Sahrens 	 * Get space to change directory entries into fs independent format.
1659789Sahrens 	 */
1660789Sahrens 	iovp = uio->uio_iov;
1661789Sahrens 	bytes_wanted = iovp->iov_len;
1662789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
1663789Sahrens 		bufsize = bytes_wanted;
1664789Sahrens 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
1665789Sahrens 		odp = (struct dirent64 *)outbuf;
1666789Sahrens 	} else {
1667789Sahrens 		bufsize = bytes_wanted;
1668789Sahrens 		odp = (struct dirent64 *)iovp->iov_base;
1669789Sahrens 	}
1670789Sahrens 
1671789Sahrens 	/*
1672789Sahrens 	 * Transform to file-system independent format
1673789Sahrens 	 */
1674789Sahrens 	outcount = 0;
1675789Sahrens 	while (outcount < bytes_wanted) {
16763912Slling 		ino64_t objnum;
16773912Slling 		ushort_t reclen;
16783912Slling 		off64_t *next;
16793912Slling 
1680789Sahrens 		/*
1681789Sahrens 		 * Special case `.', `..', and `.zfs'.
1682789Sahrens 		 */
1683789Sahrens 		if (offset == 0) {
1684789Sahrens 			(void) strcpy(zap.za_name, ".");
16853912Slling 			objnum = zp->z_id;
1686789Sahrens 		} else if (offset == 1) {
1687789Sahrens 			(void) strcpy(zap.za_name, "..");
16883912Slling 			objnum = zp->z_phys->zp_parent;
1689789Sahrens 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
1690789Sahrens 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
16913912Slling 			objnum = ZFSCTL_INO_ROOT;
1692789Sahrens 		} else {
1693789Sahrens 			/*
1694789Sahrens 			 * Grab next entry.
1695789Sahrens 			 */
1696789Sahrens 			if (error = zap_cursor_retrieve(&zc, &zap)) {
1697789Sahrens 				if ((*eofp = (error == ENOENT)) != 0)
1698789Sahrens 					break;
1699789Sahrens 				else
1700789Sahrens 					goto update;
1701789Sahrens 			}
1702789Sahrens 
1703789Sahrens 			if (zap.za_integer_length != 8 ||
1704789Sahrens 			    zap.za_num_integers != 1) {
1705789Sahrens 				cmn_err(CE_WARN, "zap_readdir: bad directory "
1706789Sahrens 				    "entry, obj = %lld, offset = %lld\n",
1707789Sahrens 				    (u_longlong_t)zp->z_id,
1708789Sahrens 				    (u_longlong_t)offset);
1709789Sahrens 				error = ENXIO;
1710789Sahrens 				goto update;
1711789Sahrens 			}
17123912Slling 
17133912Slling 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
17143912Slling 			/*
17153912Slling 			 * MacOS X can extract the object type here such as:
17163912Slling 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
17173912Slling 			 */
1718789Sahrens 		}
17193912Slling 		reclen = DIRENT64_RECLEN(strlen(zap.za_name));
1720789Sahrens 
1721789Sahrens 		/*
1722789Sahrens 		 * Will this entry fit in the buffer?
1723789Sahrens 		 */
17243912Slling 		if (outcount + reclen > bufsize) {
1725789Sahrens 			/*
1726789Sahrens 			 * Did we manage to fit anything in the buffer?
1727789Sahrens 			 */
1728789Sahrens 			if (!outcount) {
1729789Sahrens 				error = EINVAL;
1730789Sahrens 				goto update;
1731789Sahrens 			}
1732789Sahrens 			break;
1733789Sahrens 		}
1734789Sahrens 		/*
1735789Sahrens 		 * Add this entry:
1736789Sahrens 		 */
17373912Slling 		odp->d_ino = objnum;
17383912Slling 		odp->d_reclen = reclen;
1739789Sahrens 		/* NOTE: d_off is the offset for the *next* entry */
1740789Sahrens 		next = &(odp->d_off);
1741789Sahrens 		(void) strncpy(odp->d_name, zap.za_name,
17423912Slling 		    DIRENT64_NAMELEN(reclen));
17433912Slling 		outcount += reclen;
17443912Slling 		odp = (dirent64_t *)((intptr_t)odp + reclen);
1745789Sahrens 
1746789Sahrens 		ASSERT(outcount <= bufsize);
1747789Sahrens 
1748789Sahrens 		/* Prefetch znode */
1749869Sperrin 		if (prefetch)
17503912Slling 			dmu_prefetch(os, objnum, 0, 0);
1751789Sahrens 
1752789Sahrens 		/*
1753789Sahrens 		 * Move to the next entry, fill in the previous offset.
1754789Sahrens 		 */
1755789Sahrens 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
1756789Sahrens 			zap_cursor_advance(&zc);
1757789Sahrens 			offset = zap_cursor_serialize(&zc);
1758789Sahrens 		} else {
1759789Sahrens 			offset += 1;
1760789Sahrens 		}
1761789Sahrens 		*next = offset;
1762789Sahrens 	}
1763869Sperrin 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
1764789Sahrens 
1765789Sahrens 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
1766789Sahrens 		iovp->iov_base += outcount;
1767789Sahrens 		iovp->iov_len -= outcount;
1768789Sahrens 		uio->uio_resid -= outcount;
1769789Sahrens 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
1770789Sahrens 		/*
1771789Sahrens 		 * Reset the pointer.
1772789Sahrens 		 */
1773789Sahrens 		offset = uio->uio_loffset;
1774789Sahrens 	}
1775789Sahrens 
1776789Sahrens update:
1777885Sahrens 	zap_cursor_fini(&zc);
1778789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1779789Sahrens 		kmem_free(outbuf, bufsize);
1780789Sahrens 
1781789Sahrens 	if (error == ENOENT)
1782789Sahrens 		error = 0;
1783789Sahrens 
1784789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
1785789Sahrens 
1786789Sahrens 	uio->uio_loffset = offset;
1787789Sahrens 	ZFS_EXIT(zfsvfs);
1788789Sahrens 	return (error);
1789789Sahrens }
1790789Sahrens 
1791*4720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4;
1792*4720Sfr157268 
1793789Sahrens static int
1794789Sahrens zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr)
1795789Sahrens {
1796789Sahrens 	znode_t	*zp = VTOZ(vp);
1797789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1798789Sahrens 
17991773Seschrock 	/*
18001773Seschrock 	 * Regardless of whether this is required for standards conformance,
18011773Seschrock 	 * this is the logical behavior when fsync() is called on a file with
18021773Seschrock 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
18031773Seschrock 	 * going to be pushed out as part of the zil_commit().
18041773Seschrock 	 */
18051773Seschrock 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
18061773Seschrock 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
18071773Seschrock 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr);
18081773Seschrock 
1809*4720Sfr157268 	(void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
1810*4720Sfr157268 
1811789Sahrens 	ZFS_ENTER(zfsvfs);
18122638Sperrin 	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
1813789Sahrens 	ZFS_EXIT(zfsvfs);
1814789Sahrens 	return (0);
1815789Sahrens }
1816789Sahrens 
1817789Sahrens /*
1818789Sahrens  * Get the requested file attributes and place them in the provided
1819789Sahrens  * vattr structure.
1820789Sahrens  *
1821789Sahrens  *	IN:	vp	- vnode of file.
1822789Sahrens  *		vap	- va_mask identifies requested attributes.
1823789Sahrens  *		flags	- [UNUSED]
1824789Sahrens  *		cr	- credentials of caller.
1825789Sahrens  *
1826789Sahrens  *	OUT:	vap	- attribute values.
1827789Sahrens  *
1828789Sahrens  *	RETURN:	0 (always succeeds)
1829789Sahrens  */
1830789Sahrens /* ARGSUSED */
1831789Sahrens static int
1832789Sahrens zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
1833789Sahrens {
1834789Sahrens 	znode_t *zp = VTOZ(vp);
1835789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1836789Sahrens 	znode_phys_t *pzp = zp->z_phys;
1837789Sahrens 	int	error;
18384543Smarks 	uint64_t links;
1839789Sahrens 
1840789Sahrens 	ZFS_ENTER(zfsvfs);
1841789Sahrens 
1842789Sahrens 	/*
1843789Sahrens 	 * Return all attributes.  It's cheaper to provide the answer
1844789Sahrens 	 * than to determine whether we were asked the question.
1845789Sahrens 	 */
1846789Sahrens 	mutex_enter(&zp->z_lock);
1847789Sahrens 
1848789Sahrens 	vap->va_type = vp->v_type;
1849789Sahrens 	vap->va_mode = pzp->zp_mode & MODEMASK;
1850789Sahrens 	vap->va_uid = zp->z_phys->zp_uid;
1851789Sahrens 	vap->va_gid = zp->z_phys->zp_gid;
1852789Sahrens 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
1853789Sahrens 	vap->va_nodeid = zp->z_id;
18544543Smarks 	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
18554543Smarks 		links = pzp->zp_links + 1;
18564543Smarks 	else
18574543Smarks 		links = pzp->zp_links;
18584543Smarks 	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
1859789Sahrens 	vap->va_size = pzp->zp_size;
18601816Smarks 	vap->va_rdev = vp->v_rdev;
1861789Sahrens 	vap->va_seq = zp->z_seq;
1862789Sahrens 
1863789Sahrens 	ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
1864789Sahrens 	ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
1865789Sahrens 	ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
1866789Sahrens 
1867789Sahrens 	/*
1868905Smarks 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
1869905Smarks 	 * Also, if we are the owner don't bother, since owner should
1870905Smarks 	 * always be allowed to read basic attributes of file.
1871789Sahrens 	 */
1872905Smarks 	if (!(zp->z_phys->zp_flags & ZFS_ACL_TRIVIAL) &&
1873905Smarks 	    (zp->z_phys->zp_uid != crgetuid(cr))) {
1874905Smarks 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, cr)) {
1875789Sahrens 			mutex_exit(&zp->z_lock);
1876789Sahrens 			ZFS_EXIT(zfsvfs);
1877789Sahrens 			return (error);
1878789Sahrens 		}
1879789Sahrens 	}
1880789Sahrens 
1881789Sahrens 	mutex_exit(&zp->z_lock);
1882789Sahrens 
1883789Sahrens 	dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks);
1884789Sahrens 
1885789Sahrens 	if (zp->z_blksz == 0) {
1886789Sahrens 		/*
1887789Sahrens 		 * Block size hasn't been set; suggest maximal I/O transfers.
1888789Sahrens 		 */
1889789Sahrens 		vap->va_blksize = zfsvfs->z_max_blksz;
1890789Sahrens 	}
1891789Sahrens 
1892789Sahrens 	ZFS_EXIT(zfsvfs);
1893789Sahrens 	return (0);
1894789Sahrens }
1895789Sahrens 
1896789Sahrens /*
1897789Sahrens  * Set the file attributes to the values contained in the
1898789Sahrens  * vattr structure.
1899789Sahrens  *
1900789Sahrens  *	IN:	vp	- vnode of file to be modified.
1901789Sahrens  *		vap	- new attribute values.
1902789Sahrens  *		flags	- ATTR_UTIME set if non-default time values provided.
1903789Sahrens  *		cr	- credentials of caller.
1904789Sahrens  *
1905789Sahrens  *	RETURN:	0 if success
1906789Sahrens  *		error code if failure
1907789Sahrens  *
1908789Sahrens  * Timestamps:
1909789Sahrens  *	vp - ctime updated, mtime updated if size changed.
1910789Sahrens  */
1911789Sahrens /* ARGSUSED */
1912789Sahrens static int
1913789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1914789Sahrens 	caller_context_t *ct)
1915789Sahrens {
1916789Sahrens 	struct znode	*zp = VTOZ(vp);
1917789Sahrens 	znode_phys_t	*pzp = zp->z_phys;
1918789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1919789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1920789Sahrens 	dmu_tx_t	*tx;
19211878Smaybee 	vattr_t		oldva;
1922789Sahrens 	uint_t		mask = vap->va_mask;
19231878Smaybee 	uint_t		saved_mask;
19242796Smarks 	int		trim_mask = 0;
1925789Sahrens 	uint64_t	new_mode;
19261231Smarks 	znode_t		*attrzp;
1927789Sahrens 	int		need_policy = FALSE;
1928789Sahrens 	int		err;
1929789Sahrens 
1930789Sahrens 	if (mask == 0)
1931789Sahrens 		return (0);
1932789Sahrens 
1933789Sahrens 	if (mask & AT_NOSET)
1934789Sahrens 		return (EINVAL);
1935789Sahrens 
1936789Sahrens 	if (mask & AT_SIZE && vp->v_type == VDIR)
1937789Sahrens 		return (EISDIR);
1938789Sahrens 
19391394Smarks 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO)
19401308Smarks 		return (EINVAL);
19411308Smarks 
1942789Sahrens 	ZFS_ENTER(zfsvfs);
1943789Sahrens 
1944789Sahrens top:
19451231Smarks 	attrzp = NULL;
1946789Sahrens 
1947789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
1948789Sahrens 		ZFS_EXIT(zfsvfs);
1949789Sahrens 		return (EROFS);
1950789Sahrens 	}
1951789Sahrens 
1952789Sahrens 	/*
1953789Sahrens 	 * First validate permissions
1954789Sahrens 	 */
1955789Sahrens 
1956789Sahrens 	if (mask & AT_SIZE) {
1957789Sahrens 		err = zfs_zaccess(zp, ACE_WRITE_DATA, cr);
1958789Sahrens 		if (err) {
1959789Sahrens 			ZFS_EXIT(zfsvfs);
1960789Sahrens 			return (err);
1961789Sahrens 		}
19621878Smaybee 		/*
19631878Smaybee 		 * XXX - Note, we are not providing any open
19641878Smaybee 		 * mode flags here (like FNDELAY), so we may
19651878Smaybee 		 * block if there are locks present... this
19661878Smaybee 		 * should be addressed in openat().
19671878Smaybee 		 */
19681878Smaybee 		do {
19691878Smaybee 			err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
19702113Sahrens 			/* NB: we already did dmu_tx_wait() if necessary */
19711878Smaybee 		} while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
19721878Smaybee 		if (err) {
19731878Smaybee 			ZFS_EXIT(zfsvfs);
19741878Smaybee 			return (err);
19751878Smaybee 		}
1976789Sahrens 	}
1977789Sahrens 
1978789Sahrens 	if (mask & (AT_ATIME|AT_MTIME))
1979789Sahrens 		need_policy = zfs_zaccess_v4_perm(zp, ACE_WRITE_ATTRIBUTES, cr);
1980789Sahrens 
1981789Sahrens 	if (mask & (AT_UID|AT_GID)) {
1982789Sahrens 		int	idmask = (mask & (AT_UID|AT_GID));
1983789Sahrens 		int	take_owner;
1984789Sahrens 		int	take_group;
1985789Sahrens 
1986789Sahrens 		/*
1987913Smarks 		 * NOTE: even if a new mode is being set,
1988913Smarks 		 * we may clear S_ISUID/S_ISGID bits.
1989913Smarks 		 */
1990913Smarks 
1991913Smarks 		if (!(mask & AT_MODE))
1992913Smarks 			vap->va_mode = pzp->zp_mode;
1993913Smarks 
1994913Smarks 		/*
1995789Sahrens 		 * Take ownership or chgrp to group we are a member of
1996789Sahrens 		 */
1997789Sahrens 
1998789Sahrens 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
1999789Sahrens 		take_group = (mask & AT_GID) && groupmember(vap->va_gid, cr);
2000789Sahrens 
2001789Sahrens 		/*
2002789Sahrens 		 * If both AT_UID and AT_GID are set then take_owner and
2003789Sahrens 		 * take_group must both be set in order to allow taking
2004789Sahrens 		 * ownership.
2005789Sahrens 		 *
2006789Sahrens 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2007789Sahrens 		 *
2008789Sahrens 		 */
2009789Sahrens 
2010789Sahrens 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2011789Sahrens 		    ((idmask == AT_UID) && take_owner) ||
2012789Sahrens 		    ((idmask == AT_GID) && take_group)) {
2013789Sahrens 			if (zfs_zaccess_v4_perm(zp, ACE_WRITE_OWNER, cr) == 0) {
2014789Sahrens 				/*
2015789Sahrens 				 * Remove setuid/setgid for non-privileged users
2016789Sahrens 				 */
20171115Smarks 				secpolicy_setid_clear(vap, cr);
20182796Smarks 				trim_mask = (mask & (AT_UID|AT_GID));
2019789Sahrens 			} else {
2020789Sahrens 				need_policy =  TRUE;
2021789Sahrens 			}
2022789Sahrens 		} else {
2023789Sahrens 			need_policy =  TRUE;
2024789Sahrens 		}
2025789Sahrens 	}
2026789Sahrens 
20272796Smarks 	mutex_enter(&zp->z_lock);
20282796Smarks 	oldva.va_mode = pzp->zp_mode;
20292796Smarks 	oldva.va_uid = zp->z_phys->zp_uid;
20302796Smarks 	oldva.va_gid = zp->z_phys->zp_gid;
20312796Smarks 	mutex_exit(&zp->z_lock);
20322796Smarks 
20332796Smarks 	if (mask & AT_MODE) {
20342796Smarks 		if (zfs_zaccess_v4_perm(zp, ACE_WRITE_ACL, cr) == 0) {
20352796Smarks 			err = secpolicy_setid_setsticky_clear(vp, vap,
20362796Smarks 			    &oldva, cr);
20372796Smarks 			if (err) {
20382796Smarks 				ZFS_EXIT(zfsvfs);
20392796Smarks 				return (err);
20402796Smarks 			}
20412796Smarks 			trim_mask |= AT_MODE;
20422796Smarks 		} else {
20432796Smarks 			need_policy = TRUE;
20442796Smarks 		}
20452796Smarks 	}
2046789Sahrens 
2047789Sahrens 	if (need_policy) {
20481115Smarks 		/*
20491115Smarks 		 * If trim_mask is set then take ownership
20502796Smarks 		 * has been granted or write_acl is present and user
20512796Smarks 		 * has the ability to modify mode.  In that case remove
20522796Smarks 		 * UID|GID and or MODE from mask so that
20531115Smarks 		 * secpolicy_vnode_setattr() doesn't revoke it.
20541115Smarks 		 */
20552796Smarks 
20562796Smarks 		if (trim_mask) {
20572796Smarks 			saved_mask = vap->va_mask;
20582796Smarks 			vap->va_mask &= ~trim_mask;
20592796Smarks 
20602796Smarks 		}
2061789Sahrens 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2062789Sahrens 		    (int (*)(void *, int, cred_t *))zfs_zaccess_rwx, zp);
2063789Sahrens 		if (err) {
2064789Sahrens 			ZFS_EXIT(zfsvfs);
2065789Sahrens 			return (err);
2066789Sahrens 		}
20671115Smarks 
20681115Smarks 		if (trim_mask)
20692796Smarks 			vap->va_mask |= saved_mask;
2070789Sahrens 	}
2071789Sahrens 
2072789Sahrens 	/*
2073789Sahrens 	 * secpolicy_vnode_setattr, or take ownership may have
2074789Sahrens 	 * changed va_mask
2075789Sahrens 	 */
2076789Sahrens 	mask = vap->va_mask;
2077789Sahrens 
2078789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2079789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
2080789Sahrens 
2081789Sahrens 	if (mask & AT_MODE) {
20821576Smarks 		uint64_t pmode = pzp->zp_mode;
20831576Smarks 
20841576Smarks 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2085789Sahrens 
2086789Sahrens 		if (zp->z_phys->zp_acl.z_acl_extern_obj)
2087789Sahrens 			dmu_tx_hold_write(tx,
2088789Sahrens 			    pzp->zp_acl.z_acl_extern_obj, 0, SPA_MAXBLOCKSIZE);
2089789Sahrens 		else
2090789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2091789Sahrens 			    0, ZFS_ACL_SIZE(MAX_ACL_SIZE));
2092789Sahrens 	}
2093789Sahrens 
20941231Smarks 	if ((mask & (AT_UID | AT_GID)) && zp->z_phys->zp_xattr != 0) {
20951231Smarks 		err = zfs_zget(zp->z_zfsvfs, zp->z_phys->zp_xattr, &attrzp);
20961231Smarks 		if (err) {
20971231Smarks 			dmu_tx_abort(tx);
20981231Smarks 			ZFS_EXIT(zfsvfs);
20991231Smarks 			return (err);
21001231Smarks 		}
21011231Smarks 		dmu_tx_hold_bonus(tx, attrzp->z_id);
21021231Smarks 	}
21031231Smarks 
2104789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2105789Sahrens 	if (err) {
21061231Smarks 		if (attrzp)
21071231Smarks 			VN_RELE(ZTOV(attrzp));
2108789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
21092113Sahrens 			dmu_tx_wait(tx);
21102113Sahrens 			dmu_tx_abort(tx);
2111789Sahrens 			goto top;
2112789Sahrens 		}
21132113Sahrens 		dmu_tx_abort(tx);
2114789Sahrens 		ZFS_EXIT(zfsvfs);
2115789Sahrens 		return (err);
2116789Sahrens 	}
2117789Sahrens 
2118789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
2119789Sahrens 
2120789Sahrens 	/*
2121789Sahrens 	 * Set each attribute requested.
2122789Sahrens 	 * We group settings according to the locks they need to acquire.
2123789Sahrens 	 *
2124789Sahrens 	 * Note: you cannot set ctime directly, although it will be
2125789Sahrens 	 * updated as a side-effect of calling this function.
2126789Sahrens 	 */
2127789Sahrens 
2128789Sahrens 	mutex_enter(&zp->z_lock);
2129789Sahrens 
2130789Sahrens 	if (mask & AT_MODE) {
2131789Sahrens 		err = zfs_acl_chmod_setattr(zp, new_mode, tx);
2132789Sahrens 		ASSERT3U(err, ==, 0);
2133789Sahrens 	}
2134789Sahrens 
21351231Smarks 	if (attrzp)
21361231Smarks 		mutex_enter(&attrzp->z_lock);
21371231Smarks 
21381231Smarks 	if (mask & AT_UID) {
2139789Sahrens 		zp->z_phys->zp_uid = (uint64_t)vap->va_uid;
21401231Smarks 		if (attrzp) {
21411231Smarks 			attrzp->z_phys->zp_uid = (uint64_t)vap->va_uid;
21421231Smarks 		}
21431231Smarks 	}
21441231Smarks 
21451231Smarks 	if (mask & AT_GID) {
2146789Sahrens 		zp->z_phys->zp_gid = (uint64_t)vap->va_gid;
21471231Smarks 		if (attrzp)
21481231Smarks 			attrzp->z_phys->zp_gid = (uint64_t)vap->va_gid;
21491231Smarks 	}
21501231Smarks 
21511231Smarks 	if (attrzp)
21521231Smarks 		mutex_exit(&attrzp->z_lock);
2153789Sahrens 
2154789Sahrens 	if (mask & AT_ATIME)
2155789Sahrens 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
2156789Sahrens 
2157789Sahrens 	if (mask & AT_MTIME)
2158789Sahrens 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
2159789Sahrens 
21601878Smaybee 	if (mask & AT_SIZE)
2161789Sahrens 		zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
21621878Smaybee 	else if (mask != 0)
2163789Sahrens 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
2164789Sahrens 
21651878Smaybee 	if (mask != 0)
21662638Sperrin 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask);
2167789Sahrens 
2168789Sahrens 	mutex_exit(&zp->z_lock);
2169789Sahrens 
21701231Smarks 	if (attrzp)
21711231Smarks 		VN_RELE(ZTOV(attrzp));
21721231Smarks 
2173789Sahrens 	dmu_tx_commit(tx);
2174789Sahrens 
2175789Sahrens 	ZFS_EXIT(zfsvfs);
2176789Sahrens 	return (err);
2177789Sahrens }
2178789Sahrens 
21793271Smaybee typedef struct zfs_zlock {
21803271Smaybee 	krwlock_t	*zl_rwlock;	/* lock we acquired */
21813271Smaybee 	znode_t		*zl_znode;	/* znode we held */
21823271Smaybee 	struct zfs_zlock *zl_next;	/* next in list */
21833271Smaybee } zfs_zlock_t;
21843271Smaybee 
21853271Smaybee /*
21863271Smaybee  * Drop locks and release vnodes that were held by zfs_rename_lock().
21873271Smaybee  */
21883271Smaybee static void
21893271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp)
21903271Smaybee {
21913271Smaybee 	zfs_zlock_t *zl;
21923271Smaybee 
21933271Smaybee 	while ((zl = *zlpp) != NULL) {
21943271Smaybee 		if (zl->zl_znode != NULL)
21953271Smaybee 			VN_RELE(ZTOV(zl->zl_znode));
21963271Smaybee 		rw_exit(zl->zl_rwlock);
21973271Smaybee 		*zlpp = zl->zl_next;
21983271Smaybee 		kmem_free(zl, sizeof (*zl));
21993271Smaybee 	}
22003271Smaybee }
22013271Smaybee 
2202789Sahrens /*
2203789Sahrens  * Search back through the directory tree, using the ".." entries.
2204789Sahrens  * Lock each directory in the chain to prevent concurrent renames.
2205789Sahrens  * Fail any attempt to move a directory into one of its own descendants.
2206789Sahrens  * XXX - z_parent_lock can overlap with map or grow locks
2207789Sahrens  */
2208789Sahrens static int
2209789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
2210789Sahrens {
2211789Sahrens 	zfs_zlock_t	*zl;
22123638Sbillm 	znode_t		*zp = tdzp;
2213789Sahrens 	uint64_t	rootid = zp->z_zfsvfs->z_root;
2214789Sahrens 	uint64_t	*oidp = &zp->z_id;
2215789Sahrens 	krwlock_t	*rwlp = &szp->z_parent_lock;
2216789Sahrens 	krw_t		rw = RW_WRITER;
2217789Sahrens 
2218789Sahrens 	/*
2219789Sahrens 	 * First pass write-locks szp and compares to zp->z_id.
2220789Sahrens 	 * Later passes read-lock zp and compare to zp->z_parent.
2221789Sahrens 	 */
2222789Sahrens 	do {
22233271Smaybee 		if (!rw_tryenter(rwlp, rw)) {
22243271Smaybee 			/*
22253271Smaybee 			 * Another thread is renaming in this path.
22263271Smaybee 			 * Note that if we are a WRITER, we don't have any
22273271Smaybee 			 * parent_locks held yet.
22283271Smaybee 			 */
22293271Smaybee 			if (rw == RW_READER && zp->z_id > szp->z_id) {
22303271Smaybee 				/*
22313271Smaybee 				 * Drop our locks and restart
22323271Smaybee 				 */
22333271Smaybee 				zfs_rename_unlock(&zl);
22343271Smaybee 				*zlpp = NULL;
22353271Smaybee 				zp = tdzp;
22363271Smaybee 				oidp = &zp->z_id;
22373271Smaybee 				rwlp = &szp->z_parent_lock;
22383271Smaybee 				rw = RW_WRITER;
22393271Smaybee 				continue;
22403271Smaybee 			} else {
22413271Smaybee 				/*
22423271Smaybee 				 * Wait for other thread to drop its locks
22433271Smaybee 				 */
22443271Smaybee 				rw_enter(rwlp, rw);
22453271Smaybee 			}
22463271Smaybee 		}
22473271Smaybee 
2248789Sahrens 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
2249789Sahrens 		zl->zl_rwlock = rwlp;
2250789Sahrens 		zl->zl_znode = NULL;
2251789Sahrens 		zl->zl_next = *zlpp;
2252789Sahrens 		*zlpp = zl;
2253789Sahrens 
2254789Sahrens 		if (*oidp == szp->z_id)		/* We're a descendant of szp */
2255789Sahrens 			return (EINVAL);
2256789Sahrens 
2257789Sahrens 		if (*oidp == rootid)		/* We've hit the top */
2258789Sahrens 			return (0);
2259789Sahrens 
2260789Sahrens 		if (rw == RW_READER) {		/* i.e. not the first pass */
2261789Sahrens 			int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
2262789Sahrens 			if (error)
2263789Sahrens 				return (error);
2264789Sahrens 			zl->zl_znode = zp;
2265789Sahrens 		}
2266789Sahrens 		oidp = &zp->z_phys->zp_parent;
2267789Sahrens 		rwlp = &zp->z_parent_lock;
2268789Sahrens 		rw = RW_READER;
2269789Sahrens 
2270789Sahrens 	} while (zp->z_id != sdzp->z_id);
2271789Sahrens 
2272789Sahrens 	return (0);
2273789Sahrens }
2274789Sahrens 
2275789Sahrens /*
2276789Sahrens  * Move an entry from the provided source directory to the target
2277789Sahrens  * directory.  Change the entry name as indicated.
2278789Sahrens  *
2279789Sahrens  *	IN:	sdvp	- Source directory containing the "old entry".
2280789Sahrens  *		snm	- Old entry name.
2281789Sahrens  *		tdvp	- Target directory to contain the "new entry".
2282789Sahrens  *		tnm	- New entry name.
2283789Sahrens  *		cr	- credentials of caller.
2284789Sahrens  *
2285789Sahrens  *	RETURN:	0 if success
2286789Sahrens  *		error code if failure
2287789Sahrens  *
2288789Sahrens  * Timestamps:
2289789Sahrens  *	sdvp,tdvp - ctime|mtime updated
2290789Sahrens  */
2291789Sahrens static int
2292789Sahrens zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr)
2293789Sahrens {
2294789Sahrens 	znode_t		*tdzp, *szp, *tzp;
2295789Sahrens 	znode_t		*sdzp = VTOZ(sdvp);
2296789Sahrens 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
2297789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2298789Sahrens 	vnode_t		*realvp;
2299789Sahrens 	zfs_dirlock_t	*sdl, *tdl;
2300789Sahrens 	dmu_tx_t	*tx;
2301789Sahrens 	zfs_zlock_t	*zl;
2302789Sahrens 	int		cmp, serr, terr, error;
2303789Sahrens 
2304789Sahrens 	ZFS_ENTER(zfsvfs);
2305789Sahrens 
2306789Sahrens 	/*
2307789Sahrens 	 * Make sure we have the real vp for the target directory.
2308789Sahrens 	 */
2309789Sahrens 	if (VOP_REALVP(tdvp, &realvp) == 0)
2310789Sahrens 		tdvp = realvp;
2311789Sahrens 
2312789Sahrens 	if (tdvp->v_vfsp != sdvp->v_vfsp) {
2313789Sahrens 		ZFS_EXIT(zfsvfs);
2314789Sahrens 		return (EXDEV);
2315789Sahrens 	}
2316789Sahrens 
2317789Sahrens 	tdzp = VTOZ(tdvp);
2318789Sahrens top:
2319789Sahrens 	szp = NULL;
2320789Sahrens 	tzp = NULL;
2321789Sahrens 	zl = NULL;
2322789Sahrens 
2323789Sahrens 	/*
2324789Sahrens 	 * This is to prevent the creation of links into attribute space
2325789Sahrens 	 * by renaming a linked file into/outof an attribute directory.
2326789Sahrens 	 * See the comment in zfs_link() for why this is considered bad.
2327789Sahrens 	 */
2328789Sahrens 	if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
2329789Sahrens 	    (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
2330789Sahrens 		ZFS_EXIT(zfsvfs);
2331789Sahrens 		return (EINVAL);
2332789Sahrens 	}
2333789Sahrens 
2334789Sahrens 	/*
2335789Sahrens 	 * Lock source and target directory entries.  To prevent deadlock,
2336789Sahrens 	 * a lock ordering must be defined.  We lock the directory with
2337789Sahrens 	 * the smallest object id first, or if it's a tie, the one with
2338789Sahrens 	 * the lexically first name.
2339789Sahrens 	 */
2340789Sahrens 	if (sdzp->z_id < tdzp->z_id) {
2341789Sahrens 		cmp = -1;
2342789Sahrens 	} else if (sdzp->z_id > tdzp->z_id) {
2343789Sahrens 		cmp = 1;
2344789Sahrens 	} else {
2345789Sahrens 		cmp = strcmp(snm, tnm);
2346789Sahrens 		if (cmp == 0) {
2347789Sahrens 			/*
2348789Sahrens 			 * POSIX: "If the old argument and the new argument
2349789Sahrens 			 * both refer to links to the same existing file,
2350789Sahrens 			 * the rename() function shall return successfully
2351789Sahrens 			 * and perform no other action."
2352789Sahrens 			 */
2353789Sahrens 			ZFS_EXIT(zfsvfs);
2354789Sahrens 			return (0);
2355789Sahrens 		}
2356789Sahrens 	}
2357789Sahrens 	if (cmp < 0) {
2358789Sahrens 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
2359789Sahrens 		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
2360789Sahrens 	} else {
2361789Sahrens 		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
2362789Sahrens 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
2363789Sahrens 	}
2364789Sahrens 
2365789Sahrens 	if (serr) {
2366789Sahrens 		/*
2367789Sahrens 		 * Source entry invalid or not there.
2368789Sahrens 		 */
2369789Sahrens 		if (!terr) {
2370789Sahrens 			zfs_dirent_unlock(tdl);
2371789Sahrens 			if (tzp)
2372789Sahrens 				VN_RELE(ZTOV(tzp));
2373789Sahrens 		}
2374789Sahrens 		if (strcmp(snm, "..") == 0)
2375789Sahrens 			serr = EINVAL;
2376789Sahrens 		ZFS_EXIT(zfsvfs);
2377789Sahrens 		return (serr);
2378789Sahrens 	}
2379789Sahrens 	if (terr) {
2380789Sahrens 		zfs_dirent_unlock(sdl);
2381789Sahrens 		VN_RELE(ZTOV(szp));
2382789Sahrens 		if (strcmp(tnm, "..") == 0)
2383789Sahrens 			terr = EINVAL;
2384789Sahrens 		ZFS_EXIT(zfsvfs);
2385789Sahrens 		return (terr);
2386789Sahrens 	}
2387789Sahrens 
2388789Sahrens 	/*
2389789Sahrens 	 * Must have write access at the source to remove the old entry
2390789Sahrens 	 * and write access at the target to create the new entry.
2391789Sahrens 	 * Note that if target and source are the same, this can be
2392789Sahrens 	 * done in a single check.
2393789Sahrens 	 */
2394789Sahrens 
2395789Sahrens 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
2396789Sahrens 		goto out;
2397789Sahrens 
2398789Sahrens 	if (ZTOV(szp)->v_type == VDIR) {
2399789Sahrens 		/*
2400789Sahrens 		 * Check to make sure rename is valid.
2401789Sahrens 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
2402789Sahrens 		 */
2403789Sahrens 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
2404789Sahrens 			goto out;
2405789Sahrens 	}
2406789Sahrens 
2407789Sahrens 	/*
2408789Sahrens 	 * Does target exist?
2409789Sahrens 	 */
2410789Sahrens 	if (tzp) {
2411789Sahrens 		/*
2412789Sahrens 		 * Source and target must be the same type.
2413789Sahrens 		 */
2414789Sahrens 		if (ZTOV(szp)->v_type == VDIR) {
2415789Sahrens 			if (ZTOV(tzp)->v_type != VDIR) {
2416789Sahrens 				error = ENOTDIR;
2417789Sahrens 				goto out;
2418789Sahrens 			}
2419789Sahrens 		} else {
2420789Sahrens 			if (ZTOV(tzp)->v_type == VDIR) {
2421789Sahrens 				error = EISDIR;
2422789Sahrens 				goto out;
2423789Sahrens 			}
2424789Sahrens 		}
2425789Sahrens 		/*
2426789Sahrens 		 * POSIX dictates that when the source and target
2427789Sahrens 		 * entries refer to the same file object, rename
2428789Sahrens 		 * must do nothing and exit without error.
2429789Sahrens 		 */
2430789Sahrens 		if (szp->z_id == tzp->z_id) {
2431789Sahrens 			error = 0;
2432789Sahrens 			goto out;
2433789Sahrens 		}
2434789Sahrens 	}
2435789Sahrens 
2436789Sahrens 	vnevent_rename_src(ZTOV(szp));
2437789Sahrens 	if (tzp)
2438789Sahrens 		vnevent_rename_dest(ZTOV(tzp));
2439789Sahrens 
2440789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2441789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);	/* nlink changes */
2442789Sahrens 	dmu_tx_hold_bonus(tx, sdzp->z_id);	/* nlink changes */
24431544Seschrock 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
24441544Seschrock 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
24451544Seschrock 	if (sdzp != tdzp)
2446789Sahrens 		dmu_tx_hold_bonus(tx, tdzp->z_id);	/* nlink changes */
24471544Seschrock 	if (tzp)
24481544Seschrock 		dmu_tx_hold_bonus(tx, tzp->z_id);	/* parent changes */
24493461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2450789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2451789Sahrens 	if (error) {
2452789Sahrens 		if (zl != NULL)
2453789Sahrens 			zfs_rename_unlock(&zl);
2454789Sahrens 		zfs_dirent_unlock(sdl);
2455789Sahrens 		zfs_dirent_unlock(tdl);
2456789Sahrens 		VN_RELE(ZTOV(szp));
2457789Sahrens 		if (tzp)
2458789Sahrens 			VN_RELE(ZTOV(tzp));
2459789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
24602113Sahrens 			dmu_tx_wait(tx);
24612113Sahrens 			dmu_tx_abort(tx);
2462789Sahrens 			goto top;
2463789Sahrens 		}
24642113Sahrens 		dmu_tx_abort(tx);
2465789Sahrens 		ZFS_EXIT(zfsvfs);
2466789Sahrens 		return (error);
2467789Sahrens 	}
2468789Sahrens 
2469789Sahrens 	if (tzp)	/* Attempt to remove the existing target */
2470789Sahrens 		error = zfs_link_destroy(tdl, tzp, tx, 0, NULL);
2471789Sahrens 
2472789Sahrens 	if (error == 0) {
2473789Sahrens 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
2474789Sahrens 		if (error == 0) {
2475789Sahrens 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
2476789Sahrens 			ASSERT(error == 0);
24772638Sperrin 			zfs_log_rename(zilog, tx, TX_RENAME, sdzp,
24782638Sperrin 			    sdl->dl_name, tdzp, tdl->dl_name, szp);
2479789Sahrens 		}
2480789Sahrens 	}
2481789Sahrens 
2482789Sahrens 	dmu_tx_commit(tx);
2483789Sahrens out:
2484789Sahrens 	if (zl != NULL)
2485789Sahrens 		zfs_rename_unlock(&zl);
2486789Sahrens 
2487789Sahrens 	zfs_dirent_unlock(sdl);
2488789Sahrens 	zfs_dirent_unlock(tdl);
2489789Sahrens 
2490789Sahrens 	VN_RELE(ZTOV(szp));
2491789Sahrens 	if (tzp)
2492789Sahrens 		VN_RELE(ZTOV(tzp));
2493789Sahrens 
2494789Sahrens 	ZFS_EXIT(zfsvfs);
2495789Sahrens 	return (error);
2496789Sahrens }
2497789Sahrens 
2498789Sahrens /*
2499789Sahrens  * Insert the indicated symbolic reference entry into the directory.
2500789Sahrens  *
2501789Sahrens  *	IN:	dvp	- Directory to contain new symbolic link.
2502789Sahrens  *		link	- Name for new symlink entry.
2503789Sahrens  *		vap	- Attributes of new entry.
2504789Sahrens  *		target	- Target path of new symlink.
2505789Sahrens  *		cr	- credentials of caller.
2506789Sahrens  *
2507789Sahrens  *	RETURN:	0 if success
2508789Sahrens  *		error code if failure
2509789Sahrens  *
2510789Sahrens  * Timestamps:
2511789Sahrens  *	dvp - ctime|mtime updated
2512789Sahrens  */
2513789Sahrens static int
2514789Sahrens zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr)
2515789Sahrens {
2516789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
2517789Sahrens 	zfs_dirlock_t	*dl;
2518789Sahrens 	dmu_tx_t	*tx;
2519789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2520789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2521789Sahrens 	uint64_t	zoid;
2522789Sahrens 	int		len = strlen(link);
2523789Sahrens 	int		error;
2524789Sahrens 
2525789Sahrens 	ASSERT(vap->va_type == VLNK);
2526789Sahrens 
2527789Sahrens 	ZFS_ENTER(zfsvfs);
2528789Sahrens top:
2529789Sahrens 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
2530789Sahrens 		ZFS_EXIT(zfsvfs);
2531789Sahrens 		return (error);
2532789Sahrens 	}
2533789Sahrens 
2534789Sahrens 	if (len > MAXPATHLEN) {
2535789Sahrens 		ZFS_EXIT(zfsvfs);
2536789Sahrens 		return (ENAMETOOLONG);
2537789Sahrens 	}
2538789Sahrens 
2539789Sahrens 	/*
2540789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
2541789Sahrens 	 */
2542789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZNEW)) {
2543789Sahrens 		ZFS_EXIT(zfsvfs);
2544789Sahrens 		return (error);
2545789Sahrens 	}
2546789Sahrens 
2547789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2548789Sahrens 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
2549789Sahrens 	dmu_tx_hold_bonus(tx, dzp->z_id);
25501544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
2551789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
2552789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
2553789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2554789Sahrens 	if (error) {
2555789Sahrens 		zfs_dirent_unlock(dl);
2556789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
25572113Sahrens 			dmu_tx_wait(tx);
25582113Sahrens 			dmu_tx_abort(tx);
2559789Sahrens 			goto top;
2560789Sahrens 		}
25612113Sahrens 		dmu_tx_abort(tx);
2562789Sahrens 		ZFS_EXIT(zfsvfs);
2563789Sahrens 		return (error);
2564789Sahrens 	}
2565789Sahrens 
2566789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
2567789Sahrens 
2568789Sahrens 	/*
2569789Sahrens 	 * Create a new object for the symlink.
2570789Sahrens 	 * Put the link content into bonus buffer if it will fit;
2571789Sahrens 	 * otherwise, store it just like any other file data.
2572789Sahrens 	 */
2573789Sahrens 	zoid = 0;
2574789Sahrens 	if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
2575789Sahrens 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, len);
2576789Sahrens 		if (len != 0)
2577789Sahrens 			bcopy(link, zp->z_phys + 1, len);
2578789Sahrens 	} else {
2579789Sahrens 		dmu_buf_t *dbp;
25801669Sperrin 
2581789Sahrens 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
2582789Sahrens 
25831669Sperrin 		/*
25841669Sperrin 		 * Nothing can access the znode yet so no locking needed
25851669Sperrin 		 * for growing the znode's blocksize.
25861669Sperrin 		 */
25871669Sperrin 		zfs_grow_blocksize(zp, len, tx);
2588789Sahrens 
25891544Seschrock 		VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, zoid, 0, FTAG, &dbp));
2590789Sahrens 		dmu_buf_will_dirty(dbp, tx);
2591789Sahrens 
2592789Sahrens 		ASSERT3U(len, <=, dbp->db_size);
2593789Sahrens 		bcopy(link, dbp->db_data, len);
25941544Seschrock 		dmu_buf_rele(dbp, FTAG);
2595789Sahrens 	}
2596789Sahrens 	zp->z_phys->zp_size = len;
2597789Sahrens 
2598789Sahrens 	/*
2599789Sahrens 	 * Insert the new object into the directory.
2600789Sahrens 	 */
2601789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
2602789Sahrens out:
2603789Sahrens 	if (error == 0)
26042638Sperrin 		zfs_log_symlink(zilog, tx, TX_SYMLINK, dzp, zp, name, link);
2605789Sahrens 
2606789Sahrens 	dmu_tx_commit(tx);
2607789Sahrens 
2608789Sahrens 	zfs_dirent_unlock(dl);
2609789Sahrens 
2610789Sahrens 	VN_RELE(ZTOV(zp));
2611789Sahrens 
2612789Sahrens 	ZFS_EXIT(zfsvfs);
2613789Sahrens 	return (error);
2614789Sahrens }
2615789Sahrens 
2616789Sahrens /*
2617789Sahrens  * Return, in the buffer contained in the provided uio structure,
2618789Sahrens  * the symbolic path referred to by vp.
2619789Sahrens  *
2620789Sahrens  *	IN:	vp	- vnode of symbolic link.
2621789Sahrens  *		uoip	- structure to contain the link path.
2622789Sahrens  *		cr	- credentials of caller.
2623789Sahrens  *
2624789Sahrens  *	OUT:	uio	- structure to contain the link path.
2625789Sahrens  *
2626789Sahrens  *	RETURN:	0 if success
2627789Sahrens  *		error code if failure
2628789Sahrens  *
2629789Sahrens  * Timestamps:
2630789Sahrens  *	vp - atime updated
2631789Sahrens  */
2632789Sahrens /* ARGSUSED */
2633789Sahrens static int
2634789Sahrens zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr)
2635789Sahrens {
2636789Sahrens 	znode_t		*zp = VTOZ(vp);
2637789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2638789Sahrens 	size_t		bufsz;
2639789Sahrens 	int		error;
2640789Sahrens 
2641789Sahrens 	ZFS_ENTER(zfsvfs);
2642789Sahrens 
2643789Sahrens 	bufsz = (size_t)zp->z_phys->zp_size;
2644789Sahrens 	if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
2645789Sahrens 		error = uiomove(zp->z_phys + 1,
2646789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
2647789Sahrens 	} else {
26481544Seschrock 		dmu_buf_t *dbp;
26491544Seschrock 		error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
26501544Seschrock 		if (error) {
2651789Sahrens 			ZFS_EXIT(zfsvfs);
2652789Sahrens 			return (error);
2653789Sahrens 		}
2654789Sahrens 		error = uiomove(dbp->db_data,
2655789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
26561544Seschrock 		dmu_buf_rele(dbp, FTAG);
2657789Sahrens 	}
2658789Sahrens 
2659789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2660789Sahrens 	ZFS_EXIT(zfsvfs);
2661789Sahrens 	return (error);
2662789Sahrens }
2663789Sahrens 
2664789Sahrens /*
2665789Sahrens  * Insert a new entry into directory tdvp referencing svp.
2666789Sahrens  *
2667789Sahrens  *	IN:	tdvp	- Directory to contain new entry.
2668789Sahrens  *		svp	- vnode of new entry.
2669789Sahrens  *		name	- name of new entry.
2670789Sahrens  *		cr	- credentials of caller.
2671789Sahrens  *
2672789Sahrens  *	RETURN:	0 if success
2673789Sahrens  *		error code if failure
2674789Sahrens  *
2675789Sahrens  * Timestamps:
2676789Sahrens  *	tdvp - ctime|mtime updated
2677789Sahrens  *	 svp - ctime updated
2678789Sahrens  */
2679789Sahrens /* ARGSUSED */
2680789Sahrens static int
2681789Sahrens zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr)
2682789Sahrens {
2683789Sahrens 	znode_t		*dzp = VTOZ(tdvp);
2684789Sahrens 	znode_t		*tzp, *szp;
2685789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2686789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2687789Sahrens 	zfs_dirlock_t	*dl;
2688789Sahrens 	dmu_tx_t	*tx;
2689789Sahrens 	vnode_t		*realvp;
2690789Sahrens 	int		error;
2691789Sahrens 
2692789Sahrens 	ASSERT(tdvp->v_type == VDIR);
2693789Sahrens 
2694789Sahrens 	ZFS_ENTER(zfsvfs);
2695789Sahrens 
2696789Sahrens 	if (VOP_REALVP(svp, &realvp) == 0)
2697789Sahrens 		svp = realvp;
2698789Sahrens 
2699789Sahrens 	if (svp->v_vfsp != tdvp->v_vfsp) {
2700789Sahrens 		ZFS_EXIT(zfsvfs);
2701789Sahrens 		return (EXDEV);
2702789Sahrens 	}
2703789Sahrens 
2704789Sahrens 	szp = VTOZ(svp);
2705789Sahrens top:
2706789Sahrens 	/*
2707789Sahrens 	 * We do not support links between attributes and non-attributes
2708789Sahrens 	 * because of the potential security risk of creating links
2709789Sahrens 	 * into "normal" file space in order to circumvent restrictions
2710789Sahrens 	 * imposed in attribute space.
2711789Sahrens 	 */
2712789Sahrens 	if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
2713789Sahrens 	    (dzp->z_phys->zp_flags & ZFS_XATTR)) {
2714789Sahrens 		ZFS_EXIT(zfsvfs);
2715789Sahrens 		return (EINVAL);
2716789Sahrens 	}
2717789Sahrens 
2718789Sahrens 	/*
2719789Sahrens 	 * POSIX dictates that we return EPERM here.
2720789Sahrens 	 * Better choices include ENOTSUP or EISDIR.
2721789Sahrens 	 */
2722789Sahrens 	if (svp->v_type == VDIR) {
2723789Sahrens 		ZFS_EXIT(zfsvfs);
2724789Sahrens 		return (EPERM);
2725789Sahrens 	}
2726789Sahrens 
2727789Sahrens 	if ((uid_t)szp->z_phys->zp_uid != crgetuid(cr) &&
2728789Sahrens 	    secpolicy_basic_link(cr) != 0) {
2729789Sahrens 		ZFS_EXIT(zfsvfs);
2730789Sahrens 		return (EPERM);
2731789Sahrens 	}
2732789Sahrens 
2733789Sahrens 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
2734789Sahrens 		ZFS_EXIT(zfsvfs);
2735789Sahrens 		return (error);
2736789Sahrens 	}
2737789Sahrens 
2738789Sahrens 	/*
2739789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
2740789Sahrens 	 */
2741789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &tzp, ZNEW)) {
2742789Sahrens 		ZFS_EXIT(zfsvfs);
2743789Sahrens 		return (error);
2744789Sahrens 	}
2745789Sahrens 
2746789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2747789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);
27481544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
2749789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2750789Sahrens 	if (error) {
2751789Sahrens 		zfs_dirent_unlock(dl);
2752789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
27532113Sahrens 			dmu_tx_wait(tx);
27542113Sahrens 			dmu_tx_abort(tx);
2755789Sahrens 			goto top;
2756789Sahrens 		}
27572113Sahrens 		dmu_tx_abort(tx);
2758789Sahrens 		ZFS_EXIT(zfsvfs);
2759789Sahrens 		return (error);
2760789Sahrens 	}
2761789Sahrens 
2762789Sahrens 	error = zfs_link_create(dl, szp, tx, 0);
2763789Sahrens 
2764789Sahrens 	if (error == 0)
27652638Sperrin 		zfs_log_link(zilog, tx, TX_LINK, dzp, szp, name);
2766789Sahrens 
2767789Sahrens 	dmu_tx_commit(tx);
2768789Sahrens 
2769789Sahrens 	zfs_dirent_unlock(dl);
2770789Sahrens 
2771789Sahrens 	ZFS_EXIT(zfsvfs);
2772789Sahrens 	return (error);
2773789Sahrens }
2774789Sahrens 
2775789Sahrens /*
2776789Sahrens  * zfs_null_putapage() is used when the file system has been force
2777789Sahrens  * unmounted. It just drops the pages.
2778789Sahrens  */
2779789Sahrens /* ARGSUSED */
2780789Sahrens static int
2781789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
2782789Sahrens 		size_t *lenp, int flags, cred_t *cr)
2783789Sahrens {
2784789Sahrens 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
2785789Sahrens 	return (0);
2786789Sahrens }
2787789Sahrens 
27882688Smaybee /*
27892688Smaybee  * Push a page out to disk, klustering if possible.
27902688Smaybee  *
27912688Smaybee  *	IN:	vp	- file to push page to.
27922688Smaybee  *		pp	- page to push.
27932688Smaybee  *		flags	- additional flags.
27942688Smaybee  *		cr	- credentials of caller.
27952688Smaybee  *
27962688Smaybee  *	OUT:	offp	- start of range pushed.
27972688Smaybee  *		lenp	- len of range pushed.
27982688Smaybee  *
27992688Smaybee  *	RETURN:	0 if success
28002688Smaybee  *		error code if failure
28012688Smaybee  *
28022688Smaybee  * NOTE: callers must have locked the page to be pushed.  On
28032688Smaybee  * exit, the page (and all other pages in the kluster) must be
28042688Smaybee  * unlocked.
28052688Smaybee  */
2806789Sahrens /* ARGSUSED */
2807789Sahrens static int
2808789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
2809789Sahrens 		size_t *lenp, int flags, cred_t *cr)
2810789Sahrens {
2811789Sahrens 	znode_t		*zp = VTOZ(vp);
2812789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2813789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2814789Sahrens 	dmu_tx_t	*tx;
28151669Sperrin 	rl_t		*rl;
28162688Smaybee 	u_offset_t	off, koff;
28172688Smaybee 	size_t		len, klen;
28184709Smaybee 	uint64_t	filesz;
2819789Sahrens 	int		err;
2820789Sahrens 
28214709Smaybee 	filesz = zp->z_phys->zp_size;
28222688Smaybee 	off = pp->p_offset;
28232688Smaybee 	len = PAGESIZE;
28242688Smaybee 	/*
28252688Smaybee 	 * If our blocksize is bigger than the page size, try to kluster
28262688Smaybee 	 * muiltiple pages so that we write a full block (thus avoiding
28272688Smaybee 	 * a read-modify-write).
28282688Smaybee 	 */
28294709Smaybee 	if (off < filesz && zp->z_blksz > PAGESIZE) {
28302688Smaybee 		if (!ISP2(zp->z_blksz)) {
28312688Smaybee 			/* Only one block in the file. */
28322688Smaybee 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
28332688Smaybee 			koff = 0;
28342688Smaybee 		} else {
28352688Smaybee 			klen = zp->z_blksz;
28362688Smaybee 			koff = P2ALIGN(off, (u_offset_t)klen);
28372688Smaybee 		}
28382688Smaybee 		ASSERT(koff <= filesz);
28392688Smaybee 		if (koff + klen > filesz)
28402688Smaybee 			klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE);
28412688Smaybee 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
28422688Smaybee 	}
28432688Smaybee 	ASSERT3U(btop(len), ==, btopr(len));
2844789Sahrens top:
28452688Smaybee 	rl = zfs_range_lock(zp, off, len, RL_WRITER);
28461819Smaybee 	/*
28471819Smaybee 	 * Can't push pages past end-of-file.
28481819Smaybee 	 */
28494709Smaybee 	filesz = zp->z_phys->zp_size;
28504709Smaybee 	if (off >= filesz) {
28514709Smaybee 		/* ignore all pages */
28522688Smaybee 		err = 0;
28532688Smaybee 		goto out;
28544709Smaybee 	} else if (off + len > filesz) {
28554709Smaybee 		int npages = btopr(filesz - off);
28562688Smaybee 		page_t *trunc;
28572688Smaybee 
28582688Smaybee 		page_list_break(&pp, &trunc, npages);
28594709Smaybee 		/* ignore pages past end of file */
28602688Smaybee 		if (trunc)
28614709Smaybee 			pvn_write_done(trunc, flags);
28624709Smaybee 		len = filesz - off;
28631819Smaybee 	}
2864789Sahrens 
2865789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2866789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, off, len);
2867789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
2868789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2869789Sahrens 	if (err != 0) {
2870789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
28712688Smaybee 			zfs_range_unlock(rl);
28722113Sahrens 			dmu_tx_wait(tx);
28732113Sahrens 			dmu_tx_abort(tx);
28742688Smaybee 			err = 0;
2875789Sahrens 			goto top;
2876789Sahrens 		}
28772113Sahrens 		dmu_tx_abort(tx);
2878789Sahrens 		goto out;
2879789Sahrens 	}
2880789Sahrens 
28812688Smaybee 	if (zp->z_blksz <= PAGESIZE) {
28822688Smaybee 		caddr_t va = ppmapin(pp, PROT_READ, (caddr_t)-1);
28832688Smaybee 		ASSERT3U(len, <=, PAGESIZE);
28842688Smaybee 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
28852688Smaybee 		ppmapout(va);
28862688Smaybee 	} else {
28872688Smaybee 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
28882688Smaybee 	}
28892688Smaybee 
28902688Smaybee 	if (err == 0) {
28912688Smaybee 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
28923638Sbillm 		zfs_log_write(zilog, tx, TX_WRITE, zp, off, len, 0);
28932688Smaybee 		dmu_tx_commit(tx);
28942688Smaybee 	}
28952688Smaybee 
28962688Smaybee out:
28972237Smaybee 	zfs_range_unlock(rl);
28984709Smaybee 	pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
2899789Sahrens 	if (offp)
2900789Sahrens 		*offp = off;
2901789Sahrens 	if (lenp)
2902789Sahrens 		*lenp = len;
2903789Sahrens 
2904789Sahrens 	return (err);
2905789Sahrens }
2906789Sahrens 
2907789Sahrens /*
2908789Sahrens  * Copy the portion of the file indicated from pages into the file.
2909789Sahrens  * The pages are stored in a page list attached to the files vnode.
2910789Sahrens  *
2911789Sahrens  *	IN:	vp	- vnode of file to push page data to.
2912789Sahrens  *		off	- position in file to put data.
2913789Sahrens  *		len	- amount of data to write.
2914789Sahrens  *		flags	- flags to control the operation.
2915789Sahrens  *		cr	- credentials of caller.
2916789Sahrens  *
2917789Sahrens  *	RETURN:	0 if success
2918789Sahrens  *		error code if failure
2919789Sahrens  *
2920789Sahrens  * Timestamps:
2921789Sahrens  *	vp - ctime|mtime updated
2922789Sahrens  */
2923789Sahrens static int
2924789Sahrens zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr)
2925789Sahrens {
2926789Sahrens 	znode_t		*zp = VTOZ(vp);
2927789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2928789Sahrens 	page_t		*pp;
2929789Sahrens 	size_t		io_len;
2930789Sahrens 	u_offset_t	io_off;
29311669Sperrin 	uint64_t	filesz;
2932789Sahrens 	int		error = 0;
2933789Sahrens 
2934789Sahrens 	ZFS_ENTER(zfsvfs);
2935789Sahrens 
2936789Sahrens 	ASSERT(zp->z_dbuf_held && zp->z_phys);
2937789Sahrens 
2938789Sahrens 	if (len == 0) {
2939789Sahrens 		/*
2940789Sahrens 		 * Search the entire vp list for pages >= off.
2941789Sahrens 		 */
2942789Sahrens 		error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage,
2943789Sahrens 		    flags, cr);
29441472Sperrin 		goto out;
2945789Sahrens 	}
2946789Sahrens 
29471669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
29481669Sperrin 	if (off > filesz) {
2949789Sahrens 		/* past end of file */
2950789Sahrens 		ZFS_EXIT(zfsvfs);
2951789Sahrens 		return (0);
2952789Sahrens 	}
2953789Sahrens 
29541669Sperrin 	len = MIN(len, filesz - off);
2955789Sahrens 
29561472Sperrin 	for (io_off = off; io_off < off + len; io_off += io_len) {
2957789Sahrens 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
29581669Sperrin 			pp = page_lookup(vp, io_off,
29594339Sperrin 			    (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
2960789Sahrens 		} else {
2961789Sahrens 			pp = page_lookup_nowait(vp, io_off,
29624339Sperrin 			    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
2963789Sahrens 		}
2964789Sahrens 
2965789Sahrens 		if (pp != NULL && pvn_getdirty(pp, flags)) {
2966789Sahrens 			int err;
2967789Sahrens 
2968789Sahrens 			/*
2969789Sahrens 			 * Found a dirty page to push
2970789Sahrens 			 */
29711669Sperrin 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
29721669Sperrin 			if (err)
2973789Sahrens 				error = err;
2974789Sahrens 		} else {
2975789Sahrens 			io_len = PAGESIZE;
2976789Sahrens 		}
2977789Sahrens 	}
29781472Sperrin out:
29792638Sperrin 	if ((flags & B_ASYNC) == 0)
29802638Sperrin 		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
2981789Sahrens 	ZFS_EXIT(zfsvfs);
2982789Sahrens 	return (error);
2983789Sahrens }
2984789Sahrens 
2985789Sahrens void
2986789Sahrens zfs_inactive(vnode_t *vp, cred_t *cr)
2987789Sahrens {
2988789Sahrens 	znode_t	*zp = VTOZ(vp);
2989789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2990789Sahrens 	int error;
2991789Sahrens 
2992789Sahrens 	rw_enter(&zfsvfs->z_um_lock, RW_READER);
2993789Sahrens 	if (zfsvfs->z_unmounted2) {
2994789Sahrens 		ASSERT(zp->z_dbuf_held == 0);
2995789Sahrens 
2996789Sahrens 		if (vn_has_cached_data(vp)) {
2997789Sahrens 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
2998789Sahrens 			    B_INVAL, cr);
2999789Sahrens 		}
3000789Sahrens 
30011544Seschrock 		mutex_enter(&zp->z_lock);
3002789Sahrens 		vp->v_count = 0; /* count arrives as 1 */
30031544Seschrock 		if (zp->z_dbuf == NULL) {
30041544Seschrock 			mutex_exit(&zp->z_lock);
30051544Seschrock 			zfs_znode_free(zp);
30061544Seschrock 		} else {
30071544Seschrock 			mutex_exit(&zp->z_lock);
30081544Seschrock 		}
3009789Sahrens 		rw_exit(&zfsvfs->z_um_lock);
3010789Sahrens 		VFS_RELE(zfsvfs->z_vfs);
3011789Sahrens 		return;
3012789Sahrens 	}
3013789Sahrens 
3014789Sahrens 	/*
3015789Sahrens 	 * Attempt to push any data in the page cache.  If this fails
3016789Sahrens 	 * we will get kicked out later in zfs_zinactive().
3017789Sahrens 	 */
30181298Sperrin 	if (vn_has_cached_data(vp)) {
30191298Sperrin 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
30201298Sperrin 		    cr);
30211298Sperrin 	}
3022789Sahrens 
30233461Sahrens 	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3024789Sahrens 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3025789Sahrens 
3026789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
3027789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
3028789Sahrens 		if (error) {
3029789Sahrens 			dmu_tx_abort(tx);
3030789Sahrens 		} else {
3031789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
3032789Sahrens 			mutex_enter(&zp->z_lock);
3033789Sahrens 			zp->z_atime_dirty = 0;
3034789Sahrens 			mutex_exit(&zp->z_lock);
3035789Sahrens 			dmu_tx_commit(tx);
3036789Sahrens 		}
3037789Sahrens 	}
3038789Sahrens 
3039789Sahrens 	zfs_zinactive(zp);
3040789Sahrens 	rw_exit(&zfsvfs->z_um_lock);
3041789Sahrens }
3042789Sahrens 
3043789Sahrens /*
3044789Sahrens  * Bounds-check the seek operation.
3045789Sahrens  *
3046789Sahrens  *	IN:	vp	- vnode seeking within
3047789Sahrens  *		ooff	- old file offset
3048789Sahrens  *		noffp	- pointer to new file offset
3049789Sahrens  *
3050789Sahrens  *	RETURN:	0 if success
3051789Sahrens  *		EINVAL if new offset invalid
3052789Sahrens  */
3053789Sahrens /* ARGSUSED */
3054789Sahrens static int
3055789Sahrens zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp)
3056789Sahrens {
3057789Sahrens 	if (vp->v_type == VDIR)
3058789Sahrens 		return (0);
3059789Sahrens 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
3060789Sahrens }
3061789Sahrens 
3062789Sahrens /*
3063789Sahrens  * Pre-filter the generic locking function to trap attempts to place
3064789Sahrens  * a mandatory lock on a memory mapped file.
3065789Sahrens  */
3066789Sahrens static int
3067789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
3068789Sahrens     flk_callback_t *flk_cbp, cred_t *cr)
3069789Sahrens {
3070789Sahrens 	znode_t *zp = VTOZ(vp);
3071789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3072789Sahrens 	int error;
3073789Sahrens 
3074789Sahrens 	ZFS_ENTER(zfsvfs);
3075789Sahrens 
3076789Sahrens 	/*
30771544Seschrock 	 * We are following the UFS semantics with respect to mapcnt
30781544Seschrock 	 * here: If we see that the file is mapped already, then we will
30791544Seschrock 	 * return an error, but we don't worry about races between this
30801544Seschrock 	 * function and zfs_map().
3081789Sahrens 	 */
30821544Seschrock 	if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) {
3083789Sahrens 		ZFS_EXIT(zfsvfs);
3084789Sahrens 		return (EAGAIN);
3085789Sahrens 	}
3086789Sahrens 	error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr);
3087789Sahrens 	ZFS_EXIT(zfsvfs);
3088789Sahrens 	return (error);
3089789Sahrens }
3090789Sahrens 
3091789Sahrens /*
3092789Sahrens  * If we can't find a page in the cache, we will create a new page
3093789Sahrens  * and fill it with file data.  For efficiency, we may try to fill
30941669Sperrin  * multiple pages at once (klustering).
3095789Sahrens  */
3096789Sahrens static int
3097789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
3098789Sahrens     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
3099789Sahrens {
3100789Sahrens 	znode_t *zp = VTOZ(vp);
3101789Sahrens 	page_t *pp, *cur_pp;
3102789Sahrens 	objset_t *os = zp->z_zfsvfs->z_os;
3103789Sahrens 	caddr_t va;
3104789Sahrens 	u_offset_t io_off, total;
3105789Sahrens 	uint64_t oid = zp->z_id;
3106789Sahrens 	size_t io_len;
31071669Sperrin 	uint64_t filesz;
3108789Sahrens 	int err;
3109789Sahrens 
3110789Sahrens 	/*
3111789Sahrens 	 * If we are only asking for a single page don't bother klustering.
3112789Sahrens 	 */
31131669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
31142688Smaybee 	if (off >= filesz)
31152688Smaybee 		return (EFAULT);
31162688Smaybee 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
3117789Sahrens 		io_off = off;
3118789Sahrens 		io_len = PAGESIZE;
3119789Sahrens 		pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr);
3120789Sahrens 	} else {
3121789Sahrens 		/*
3122789Sahrens 		 * Try to fill a kluster of pages (a blocks worth).
3123789Sahrens 		 */
3124789Sahrens 		size_t klen;
3125789Sahrens 		u_offset_t koff;
3126789Sahrens 
3127789Sahrens 		if (!ISP2(zp->z_blksz)) {
3128789Sahrens 			/* Only one block in the file. */
3129789Sahrens 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
3130789Sahrens 			koff = 0;
3131789Sahrens 		} else {
31323131Sgw25295 			/*
31333131Sgw25295 			 * It would be ideal to align our offset to the
31343131Sgw25295 			 * blocksize but doing so has resulted in some
31353131Sgw25295 			 * strange application crashes. For now, we
31363131Sgw25295 			 * leave the offset as is and only adjust the
31373131Sgw25295 			 * length if we are off the end of the file.
31383131Sgw25295 			 */
31393131Sgw25295 			koff = off;
3140789Sahrens 			klen = plsz;
3141789Sahrens 		}
31421819Smaybee 		ASSERT(koff <= filesz);
31431819Smaybee 		if (koff + klen > filesz)
31441819Smaybee 			klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff;
31452688Smaybee 		ASSERT3U(off, >=, koff);
31462688Smaybee 		ASSERT3U(off, <, koff + klen);
3147789Sahrens 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
31484339Sperrin 		    &io_len, koff, klen, 0);
3149789Sahrens 	}
3150789Sahrens 	if (pp == NULL) {
3151789Sahrens 		/*
3152789Sahrens 		 * Some other thread entered the page before us.
3153789Sahrens 		 * Return to zfs_getpage to retry the lookup.
3154789Sahrens 		 */
3155789Sahrens 		*pl = NULL;
3156789Sahrens 		return (0);
3157789Sahrens 	}
3158789Sahrens 
3159789Sahrens 	/*
3160789Sahrens 	 * Fill the pages in the kluster.
3161789Sahrens 	 */
3162789Sahrens 	cur_pp = pp;
3163789Sahrens 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
31642688Smaybee 		ASSERT3U(io_off, ==, cur_pp->p_offset);
3165789Sahrens 		va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1);
31661544Seschrock 		err = dmu_read(os, oid, io_off, PAGESIZE, va);
3167789Sahrens 		ppmapout(va);
3168789Sahrens 		if (err) {
3169789Sahrens 			/* On error, toss the entire kluster */
3170789Sahrens 			pvn_read_done(pp, B_ERROR);
3171789Sahrens 			return (err);
3172789Sahrens 		}
3173789Sahrens 		cur_pp = cur_pp->p_next;
3174789Sahrens 	}
3175789Sahrens out:
3176789Sahrens 	/*
3177789Sahrens 	 * Fill in the page list array from the kluster.  If
3178789Sahrens 	 * there are too many pages in the kluster, return
3179789Sahrens 	 * as many pages as possible starting from the desired
3180789Sahrens 	 * offset `off'.
3181789Sahrens 	 * NOTE: the page list will always be null terminated.
3182789Sahrens 	 */
3183789Sahrens 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
3184789Sahrens 
3185789Sahrens 	return (0);
3186789Sahrens }
3187789Sahrens 
3188789Sahrens /*
3189789Sahrens  * Return pointers to the pages for the file region [off, off + len]
3190789Sahrens  * in the pl array.  If plsz is greater than len, this function may
3191789Sahrens  * also return page pointers from before or after the specified
3192789Sahrens  * region (i.e. some region [off', off' + plsz]).  These additional
3193789Sahrens  * pages are only returned if they are already in the cache, or were
3194789Sahrens  * created as part of a klustered read.
3195789Sahrens  *
3196789Sahrens  *	IN:	vp	- vnode of file to get data from.
3197789Sahrens  *		off	- position in file to get data from.
3198789Sahrens  *		len	- amount of data to retrieve.
3199789Sahrens  *		plsz	- length of provided page list.
3200789Sahrens  *		seg	- segment to obtain pages for.
3201789Sahrens  *		addr	- virtual address of fault.
3202789Sahrens  *		rw	- mode of created pages.
3203789Sahrens  *		cr	- credentials of caller.
3204789Sahrens  *
3205789Sahrens  *	OUT:	protp	- protection mode of created pages.
3206789Sahrens  *		pl	- list of pages created.
3207789Sahrens  *
3208789Sahrens  *	RETURN:	0 if success
3209789Sahrens  *		error code if failure
3210789Sahrens  *
3211789Sahrens  * Timestamps:
3212789Sahrens  *	vp - atime updated
3213789Sahrens  */
3214789Sahrens /* ARGSUSED */
3215789Sahrens static int
3216789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
3217789Sahrens 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
3218789Sahrens 	enum seg_rw rw, cred_t *cr)
3219789Sahrens {
3220789Sahrens 	znode_t		*zp = VTOZ(vp);
3221789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3222789Sahrens 	page_t		*pp, **pl0 = pl;
32232752Sperrin 	int		need_unlock = 0, err = 0;
32242752Sperrin 	offset_t	orig_off;
3225789Sahrens 
3226789Sahrens 	ZFS_ENTER(zfsvfs);
3227789Sahrens 
3228789Sahrens 	if (protp)
3229789Sahrens 		*protp = PROT_ALL;
3230789Sahrens 
3231789Sahrens 	ASSERT(zp->z_dbuf_held && zp->z_phys);
3232789Sahrens 
3233789Sahrens 	/* no faultahead (for now) */
3234789Sahrens 	if (pl == NULL) {
3235789Sahrens 		ZFS_EXIT(zfsvfs);
3236789Sahrens 		return (0);
3237789Sahrens 	}
3238789Sahrens 
3239789Sahrens 	/* can't fault past EOF */
3240789Sahrens 	if (off >= zp->z_phys->zp_size) {
3241789Sahrens 		ZFS_EXIT(zfsvfs);
3242789Sahrens 		return (EFAULT);
3243789Sahrens 	}
32442752Sperrin 	orig_off = off;
3245789Sahrens 
3246789Sahrens 	/*
3247789Sahrens 	 * If we already own the lock, then we must be page faulting
3248789Sahrens 	 * in the middle of a write to this file (i.e., we are writing
3249789Sahrens 	 * to this file using data from a mapped region of the file).
3250789Sahrens 	 */
32512752Sperrin 	if (rw_owner(&zp->z_map_lock) != curthread) {
3252789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
3253789Sahrens 		need_unlock = TRUE;
3254789Sahrens 	}
3255789Sahrens 
3256789Sahrens 	/*
3257789Sahrens 	 * Loop through the requested range [off, off + len] looking
3258789Sahrens 	 * for pages.  If we don't find a page, we will need to create
3259789Sahrens 	 * a new page and fill it with data from the file.
3260789Sahrens 	 */
3261789Sahrens 	while (len > 0) {
3262789Sahrens 		if (plsz < PAGESIZE)
3263789Sahrens 			break;
3264789Sahrens 		if (pp = page_lookup(vp, off, SE_SHARED)) {
3265789Sahrens 			*pl++ = pp;
3266789Sahrens 			off += PAGESIZE;
3267789Sahrens 			addr += PAGESIZE;
3268789Sahrens 			len -= PAGESIZE;
3269789Sahrens 			plsz -= PAGESIZE;
3270789Sahrens 		} else {
3271789Sahrens 			err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw);
32722752Sperrin 			if (err)
32732752Sperrin 				goto out;
3274789Sahrens 			/*
3275789Sahrens 			 * klustering may have changed our region
3276789Sahrens 			 * to be block aligned.
3277789Sahrens 			 */
3278789Sahrens 			if (((pp = *pl) != 0) && (off != pp->p_offset)) {
3279789Sahrens 				int delta = off - pp->p_offset;
3280789Sahrens 				len += delta;
3281789Sahrens 				off -= delta;
3282789Sahrens 				addr -= delta;
3283789Sahrens 			}
3284789Sahrens 			while (*pl) {
3285789Sahrens 				pl++;
3286789Sahrens 				off += PAGESIZE;
3287789Sahrens 				addr += PAGESIZE;
3288789Sahrens 				plsz -= PAGESIZE;
3289789Sahrens 				if (len > PAGESIZE)
3290789Sahrens 					len -= PAGESIZE;
3291789Sahrens 				else
3292789Sahrens 					len = 0;
3293789Sahrens 			}
3294789Sahrens 		}
3295789Sahrens 	}
3296789Sahrens 
3297789Sahrens 	/*
3298789Sahrens 	 * Fill out the page array with any pages already in the cache.
3299789Sahrens 	 */
3300789Sahrens 	while (plsz > 0) {
3301789Sahrens 		pp = page_lookup_nowait(vp, off, SE_SHARED);
3302789Sahrens 		if (pp == NULL)
3303789Sahrens 			break;
3304789Sahrens 		*pl++ = pp;
3305789Sahrens 		off += PAGESIZE;
3306789Sahrens 		plsz -= PAGESIZE;
3307789Sahrens 	}
3308789Sahrens 
3309789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3310789Sahrens out:
33112752Sperrin 	/*
33122752Sperrin 	 * We can't grab the range lock for the page as reader which would
33132752Sperrin 	 * stop truncation as this leads to deadlock. So we need to recheck
33142752Sperrin 	 * the file size.
33152752Sperrin 	 */
33162752Sperrin 	if (orig_off >= zp->z_phys->zp_size)
33172752Sperrin 		err = EFAULT;
33182752Sperrin 	if (err) {
33192752Sperrin 		/*
33202752Sperrin 		 * Release any pages we have previously locked.
33212752Sperrin 		 */
33222752Sperrin 		while (pl > pl0)
33232752Sperrin 			page_unlock(*--pl);
33242752Sperrin 	}
33252752Sperrin 
3326789Sahrens 	*pl = NULL;
3327789Sahrens 
3328789Sahrens 	if (need_unlock)
3329789Sahrens 		rw_exit(&zp->z_map_lock);
3330789Sahrens 
3331789Sahrens 	ZFS_EXIT(zfsvfs);
3332789Sahrens 	return (err);
3333789Sahrens }
3334789Sahrens 
33351544Seschrock /*
33361544Seschrock  * Request a memory map for a section of a file.  This code interacts
33371544Seschrock  * with common code and the VM system as follows:
33381544Seschrock  *
33391544Seschrock  *	common code calls mmap(), which ends up in smmap_common()
33401544Seschrock  *
33411544Seschrock  *	this calls VOP_MAP(), which takes you into (say) zfs
33421544Seschrock  *
33431544Seschrock  *	zfs_map() calls as_map(), passing segvn_create() as the callback
33441544Seschrock  *
33451544Seschrock  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
33461544Seschrock  *
33471544Seschrock  *	zfs_addmap() updates z_mapcnt
33481544Seschrock  */
3349789Sahrens static int
3350789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
3351789Sahrens     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
3352789Sahrens {
3353789Sahrens 	znode_t *zp = VTOZ(vp);
3354789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3355789Sahrens 	segvn_crargs_t	vn_a;
3356789Sahrens 	int		error;
3357789Sahrens 
3358789Sahrens 	ZFS_ENTER(zfsvfs);
3359789Sahrens 
3360789Sahrens 	if (vp->v_flag & VNOMAP) {
3361789Sahrens 		ZFS_EXIT(zfsvfs);
3362789Sahrens 		return (ENOSYS);
3363789Sahrens 	}
3364789Sahrens 
3365789Sahrens 	if (off < 0 || len > MAXOFFSET_T - off) {
3366789Sahrens 		ZFS_EXIT(zfsvfs);
3367789Sahrens 		return (ENXIO);
3368789Sahrens 	}
3369789Sahrens 
3370789Sahrens 	if (vp->v_type != VREG) {
3371789Sahrens 		ZFS_EXIT(zfsvfs);
3372789Sahrens 		return (ENODEV);
3373789Sahrens 	}
3374789Sahrens 
3375789Sahrens 	/*
3376789Sahrens 	 * If file is locked, disallow mapping.
3377789Sahrens 	 */
33781544Seschrock 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) {
33791544Seschrock 		ZFS_EXIT(zfsvfs);
33801544Seschrock 		return (EAGAIN);
3381789Sahrens 	}
3382789Sahrens 
3383789Sahrens 	as_rangelock(as);
3384789Sahrens 	if ((flags & MAP_FIXED) == 0) {
3385789Sahrens 		map_addr(addrp, len, off, 1, flags);
3386789Sahrens 		if (*addrp == NULL) {
3387789Sahrens 			as_rangeunlock(as);
3388789Sahrens 			ZFS_EXIT(zfsvfs);
3389789Sahrens 			return (ENOMEM);
3390789Sahrens 		}
3391789Sahrens 	} else {
3392789Sahrens 		/*
3393789Sahrens 		 * User specified address - blow away any previous mappings
3394789Sahrens 		 */
3395789Sahrens 		(void) as_unmap(as, *addrp, len);
3396789Sahrens 	}
3397789Sahrens 
3398789Sahrens 	vn_a.vp = vp;
3399789Sahrens 	vn_a.offset = (u_offset_t)off;
3400789Sahrens 	vn_a.type = flags & MAP_TYPE;
3401789Sahrens 	vn_a.prot = prot;
3402789Sahrens 	vn_a.maxprot = maxprot;
3403789Sahrens 	vn_a.cred = cr;
3404789Sahrens 	vn_a.amp = NULL;
3405789Sahrens 	vn_a.flags = flags & ~MAP_TYPE;
34061417Skchow 	vn_a.szc = 0;
34071417Skchow 	vn_a.lgrp_mem_policy_flags = 0;
3408789Sahrens 
3409789Sahrens 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
3410789Sahrens 
3411789Sahrens 	as_rangeunlock(as);
3412789Sahrens 	ZFS_EXIT(zfsvfs);
3413789Sahrens 	return (error);
3414789Sahrens }
3415789Sahrens 
3416789Sahrens /* ARGSUSED */
3417789Sahrens static int
3418789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
3419789Sahrens     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
3420789Sahrens {
34211544Seschrock 	uint64_t pages = btopr(len);
34221544Seschrock 
34231544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
3424789Sahrens 	return (0);
3425789Sahrens }
3426789Sahrens 
34271773Seschrock /*
34281773Seschrock  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
34291773Seschrock  * more accurate mtime for the associated file.  Since we don't have a way of
34301773Seschrock  * detecting when the data was actually modified, we have to resort to
34311773Seschrock  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
34321773Seschrock  * last page is pushed.  The problem occurs when the msync() call is omitted,
34331773Seschrock  * which by far the most common case:
34341773Seschrock  *
34351773Seschrock  * 	open()
34361773Seschrock  * 	mmap()
34371773Seschrock  * 	<modify memory>
34381773Seschrock  * 	munmap()
34391773Seschrock  * 	close()
34401773Seschrock  * 	<time lapse>
34411773Seschrock  * 	putpage() via fsflush
34421773Seschrock  *
34431773Seschrock  * If we wait until fsflush to come along, we can have a modification time that
34441773Seschrock  * is some arbitrary point in the future.  In order to prevent this in the
34451773Seschrock  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
34461773Seschrock  * torn down.
34471773Seschrock  */
3448789Sahrens /* ARGSUSED */
3449789Sahrens static int
3450789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
3451789Sahrens     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr)
3452789Sahrens {
34531544Seschrock 	uint64_t pages = btopr(len);
34541544Seschrock 
34551544Seschrock 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
34561544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
34571773Seschrock 
34581773Seschrock 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
34591773Seschrock 	    vn_has_cached_data(vp))
34601773Seschrock 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr);
34611773Seschrock 
3462789Sahrens 	return (0);
3463789Sahrens }
3464789Sahrens 
3465789Sahrens /*
3466789Sahrens  * Free or allocate space in a file.  Currently, this function only
3467789Sahrens  * supports the `F_FREESP' command.  However, this command is somewhat
3468789Sahrens  * misnamed, as its functionality includes the ability to allocate as
3469789Sahrens  * well as free space.
3470789Sahrens  *
3471789Sahrens  *	IN:	vp	- vnode of file to free data in.
3472789Sahrens  *		cmd	- action to take (only F_FREESP supported).
3473789Sahrens  *		bfp	- section of file to free/alloc.
3474789Sahrens  *		flag	- current file open mode flags.
3475789Sahrens  *		offset	- current file offset.
3476789Sahrens  *		cr	- credentials of caller [UNUSED].
3477789Sahrens  *
3478789Sahrens  *	RETURN:	0 if success
3479789Sahrens  *		error code if failure
3480789Sahrens  *
3481789Sahrens  * Timestamps:
3482789Sahrens  *	vp - ctime|mtime updated
3483789Sahrens  */
3484789Sahrens /* ARGSUSED */
3485789Sahrens static int
3486789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
3487789Sahrens     offset_t offset, cred_t *cr, caller_context_t *ct)
3488789Sahrens {
3489789Sahrens 	znode_t		*zp = VTOZ(vp);
3490789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3491789Sahrens 	uint64_t	off, len;
3492789Sahrens 	int		error;
3493789Sahrens 
3494789Sahrens 	ZFS_ENTER(zfsvfs);
3495789Sahrens 
3496789Sahrens top:
3497789Sahrens 	if (cmd != F_FREESP) {
3498789Sahrens 		ZFS_EXIT(zfsvfs);
3499789Sahrens 		return (EINVAL);
3500789Sahrens 	}
3501789Sahrens 
3502789Sahrens 	if (error = convoff(vp, bfp, 0, offset)) {
3503789Sahrens 		ZFS_EXIT(zfsvfs);
3504789Sahrens 		return (error);
3505789Sahrens 	}
3506789Sahrens 
3507789Sahrens 	if (bfp->l_len < 0) {
3508789Sahrens 		ZFS_EXIT(zfsvfs);
3509789Sahrens 		return (EINVAL);
3510789Sahrens 	}
3511789Sahrens 
3512789Sahrens 	off = bfp->l_start;
35131669Sperrin 	len = bfp->l_len; /* 0 means from off to end of file */
35141878Smaybee 
35151878Smaybee 	do {
35161878Smaybee 		error = zfs_freesp(zp, off, len, flag, TRUE);
35172113Sahrens 		/* NB: we already did dmu_tx_wait() if necessary */
35181878Smaybee 	} while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
3519789Sahrens 
3520789Sahrens 	ZFS_EXIT(zfsvfs);
3521789Sahrens 	return (error);
3522789Sahrens }
3523789Sahrens 
3524789Sahrens static int
3525789Sahrens zfs_fid(vnode_t *vp, fid_t *fidp)
3526789Sahrens {
3527789Sahrens 	znode_t		*zp = VTOZ(vp);
3528789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3529789Sahrens 	uint32_t	gen = (uint32_t)zp->z_phys->zp_gen;
3530789Sahrens 	uint64_t	object = zp->z_id;
3531789Sahrens 	zfid_short_t	*zfid;
3532789Sahrens 	int		size, i;
3533789Sahrens 
3534789Sahrens 	ZFS_ENTER(zfsvfs);
3535789Sahrens 
3536789Sahrens 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
3537789Sahrens 	if (fidp->fid_len < size) {
3538789Sahrens 		fidp->fid_len = size;
35391512Sek110237 		ZFS_EXIT(zfsvfs);
3540789Sahrens 		return (ENOSPC);
3541789Sahrens 	}
3542789Sahrens 
3543789Sahrens 	zfid = (zfid_short_t *)fidp;
3544789Sahrens 
3545789Sahrens 	zfid->zf_len = size;
3546789Sahrens 
3547789Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
3548789Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
3549789Sahrens 
3550789Sahrens 	/* Must have a non-zero generation number to distinguish from .zfs */
3551789Sahrens 	if (gen == 0)
3552789Sahrens 		gen = 1;
3553789Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
3554789Sahrens 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
3555789Sahrens 
3556789Sahrens 	if (size == LONG_FID_LEN) {
3557789Sahrens 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
3558789Sahrens 		zfid_long_t	*zlfid;
3559789Sahrens 
3560789Sahrens 		zlfid = (zfid_long_t *)fidp;
3561789Sahrens 
3562789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
3563789Sahrens 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
3564789Sahrens 
3565789Sahrens 		/* XXX - this should be the generation number for the objset */
3566789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
3567789Sahrens 			zlfid->zf_setgen[i] = 0;
3568789Sahrens 	}
3569789Sahrens 
3570789Sahrens 	ZFS_EXIT(zfsvfs);
3571789Sahrens 	return (0);
3572789Sahrens }
3573789Sahrens 
3574789Sahrens static int
3575789Sahrens zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr)
3576789Sahrens {
3577789Sahrens 	znode_t		*zp, *xzp;
3578789Sahrens 	zfsvfs_t	*zfsvfs;
3579789Sahrens 	zfs_dirlock_t	*dl;
3580789Sahrens 	int		error;
3581789Sahrens 
3582789Sahrens 	switch (cmd) {
3583789Sahrens 	case _PC_LINK_MAX:
3584789Sahrens 		*valp = ULONG_MAX;
3585789Sahrens 		return (0);
3586789Sahrens 
3587789Sahrens 	case _PC_FILESIZEBITS:
3588789Sahrens 		*valp = 64;
3589789Sahrens 		return (0);
3590789Sahrens 
3591789Sahrens 	case _PC_XATTR_EXISTS:
3592789Sahrens 		zp = VTOZ(vp);
3593789Sahrens 		zfsvfs = zp->z_zfsvfs;
3594789Sahrens 		ZFS_ENTER(zfsvfs);
3595789Sahrens 		*valp = 0;
3596789Sahrens 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
3597789Sahrens 		    ZXATTR | ZEXISTS | ZSHARED);
3598789Sahrens 		if (error == 0) {
3599789Sahrens 			zfs_dirent_unlock(dl);
3600789Sahrens 			if (!zfs_dirempty(xzp))
3601789Sahrens 				*valp = 1;
3602789Sahrens 			VN_RELE(ZTOV(xzp));
3603789Sahrens 		} else if (error == ENOENT) {
3604789Sahrens 			/*
3605789Sahrens 			 * If there aren't extended attributes, it's the
3606789Sahrens 			 * same as having zero of them.
3607789Sahrens 			 */
3608789Sahrens 			error = 0;
3609789Sahrens 		}
3610789Sahrens 		ZFS_EXIT(zfsvfs);
3611789Sahrens 		return (error);
3612789Sahrens 
3613789Sahrens 	case _PC_ACL_ENABLED:
3614789Sahrens 		*valp = _ACL_ACE_ENABLED;
3615789Sahrens 		return (0);
3616789Sahrens 
3617789Sahrens 	case _PC_MIN_HOLE_SIZE:
3618789Sahrens 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
3619789Sahrens 		return (0);
3620789Sahrens 
3621789Sahrens 	default:
3622789Sahrens 		return (fs_pathconf(vp, cmd, valp, cr));
3623789Sahrens 	}
3624789Sahrens }
3625789Sahrens 
3626789Sahrens /*ARGSUSED*/
3627789Sahrens static int
3628789Sahrens zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
3629789Sahrens {
3630789Sahrens 	znode_t *zp = VTOZ(vp);
3631789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3632789Sahrens 	int error;
3633789Sahrens 
3634789Sahrens 	ZFS_ENTER(zfsvfs);
3635789Sahrens 	error = zfs_getacl(zp, vsecp, cr);
3636789Sahrens 	ZFS_EXIT(zfsvfs);
3637789Sahrens 
3638789Sahrens 	return (error);
3639789Sahrens }
3640789Sahrens 
3641789Sahrens /*ARGSUSED*/
3642789Sahrens static int
3643789Sahrens zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
3644789Sahrens {
3645789Sahrens 	znode_t *zp = VTOZ(vp);
3646789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3647789Sahrens 	int error;
3648789Sahrens 
3649789Sahrens 	ZFS_ENTER(zfsvfs);
3650789Sahrens 	error = zfs_setacl(zp, vsecp, cr);
3651789Sahrens 	ZFS_EXIT(zfsvfs);
3652789Sahrens 	return (error);
3653789Sahrens }
3654789Sahrens 
3655789Sahrens /*
3656789Sahrens  * Predeclare these here so that the compiler assumes that
3657789Sahrens  * this is an "old style" function declaration that does
3658789Sahrens  * not include arguments => we won't get type mismatch errors
3659789Sahrens  * in the initializations that follow.
3660789Sahrens  */
3661789Sahrens static int zfs_inval();
3662789Sahrens static int zfs_isdir();
3663789Sahrens 
3664789Sahrens static int
3665789Sahrens zfs_inval()
3666789Sahrens {
3667789Sahrens 	return (EINVAL);
3668789Sahrens }
3669789Sahrens 
3670789Sahrens static int
3671789Sahrens zfs_isdir()
3672789Sahrens {
3673789Sahrens 	return (EISDIR);
3674789Sahrens }
3675789Sahrens /*
3676789Sahrens  * Directory vnode operations template
3677789Sahrens  */
3678789Sahrens vnodeops_t *zfs_dvnodeops;
3679789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = {
36803898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
36813898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
36823898Srsb 	VOPNAME_READ,		{ .error = zfs_isdir },
36833898Srsb 	VOPNAME_WRITE,		{ .error = zfs_isdir },
36843898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
36853898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
36863898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
36873898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
36883898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
36893898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
36903898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
36913898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
36923898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
36933898Srsb 	VOPNAME_MKDIR,		{ .vop_mkdir = zfs_mkdir },
36943898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
36953898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
36963898Srsb 	VOPNAME_SYMLINK,	{ .vop_symlink = zfs_symlink },
36973898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
36983898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
36993898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
37003898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
37013898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
37023898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
37033898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
37043898Srsb 	NULL,			NULL
3705789Sahrens };
3706789Sahrens 
3707789Sahrens /*
3708789Sahrens  * Regular file vnode operations template
3709789Sahrens  */
3710789Sahrens vnodeops_t *zfs_fvnodeops;
3711789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = {
37123898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
37133898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
37143898Srsb 	VOPNAME_READ,		{ .vop_read = zfs_read },
37153898Srsb 	VOPNAME_WRITE,		{ .vop_write = zfs_write },
37163898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
37173898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
37183898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
37193898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
37203898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
37213898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
37223898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
37233898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
37243898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
37253898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
37263898Srsb 	VOPNAME_FRLOCK,		{ .vop_frlock = zfs_frlock },
37273898Srsb 	VOPNAME_SPACE,		{ .vop_space = zfs_space },
37283898Srsb 	VOPNAME_GETPAGE,	{ .vop_getpage = zfs_getpage },
37293898Srsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = zfs_putpage },
37303898Srsb 	VOPNAME_MAP,		{ .vop_map = zfs_map },
37313898Srsb 	VOPNAME_ADDMAP,		{ .vop_addmap = zfs_addmap },
37323898Srsb 	VOPNAME_DELMAP,		{ .vop_delmap = zfs_delmap },
37333898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
37343898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
37353898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
37363898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
37373898Srsb 	NULL,			NULL
3738789Sahrens };
3739789Sahrens 
3740789Sahrens /*
3741789Sahrens  * Symbolic link vnode operations template
3742789Sahrens  */
3743789Sahrens vnodeops_t *zfs_symvnodeops;
3744789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = {
37453898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
37463898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
37473898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
37483898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
37493898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = zfs_readlink },
37503898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
37513898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
37523898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
37533898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
37543898Srsb 	NULL,			NULL
3755789Sahrens };
3756789Sahrens 
3757789Sahrens /*
3758789Sahrens  * Extended attribute directory vnode operations template
3759789Sahrens  *	This template is identical to the directory vnodes
3760789Sahrens  *	operation template except for restricted operations:
3761789Sahrens  *		VOP_MKDIR()
3762789Sahrens  *		VOP_SYMLINK()
3763789Sahrens  * Note that there are other restrictions embedded in:
3764789Sahrens  *	zfs_create()	- restrict type to VREG
3765789Sahrens  *	zfs_link()	- no links into/out of attribute space
3766789Sahrens  *	zfs_rename()	- no moves into/out of attribute space
3767789Sahrens  */
3768789Sahrens vnodeops_t *zfs_xdvnodeops;
3769789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = {
37703898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
37713898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
37723898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
37733898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
37743898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
37753898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
37763898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
37773898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
37783898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
37793898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
37803898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
37813898Srsb 	VOPNAME_MKDIR,		{ .error = zfs_inval },
37823898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
37833898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
37843898Srsb 	VOPNAME_SYMLINK,	{ .error = zfs_inval },
37853898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
37863898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
37873898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
37883898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
37893898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
37903898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
37913898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
37923898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
37933898Srsb 	NULL,			NULL
3794789Sahrens };
3795789Sahrens 
3796789Sahrens /*
3797789Sahrens  * Error vnode operations template
3798789Sahrens  */
3799789Sahrens vnodeops_t *zfs_evnodeops;
3800789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = {
38013898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
38023898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
38033898Srsb 	NULL,			NULL
3804789Sahrens };
3805