xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 4339:24b45a78e29e)
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 */
179*4339Sperrin 	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) {
244*4339Sperrin 	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 		 */
251*4339Sperrin 	case _FIOGDIO:
252*4339Sperrin 	case _FIOSDIO:
2531544Seschrock 		return (0);
2541544Seschrock 
255*4339Sperrin 	case _FIO_SEEK_DATA:
256*4339Sperrin 	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) {
740*4339Sperrin 			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);
8783897Smaybee 		ASSERT(error == EEXIST || lr->lr_length <= zp->z_blksz);
8793063Sperrin 		if (error == 0) {
8803063Sperrin 			zil_add_vdev(zfsvfs->z_log,
8813063Sperrin 			    DVA_GET_VDEV(BP_IDENTITY(&lr->lr_blkptr)));
8823063Sperrin 		}
8832237Smaybee 		/*
8842237Smaybee 		 * If we get EINPROGRESS, then we need to wait for a
8852237Smaybee 		 * write IO initiated by dmu_sync() to complete before
8862638Sperrin 		 * we can release this dbuf.  We will finish everything
8872237Smaybee 		 * up in the zfs_get_done() callback.
8882237Smaybee 		 */
8892237Smaybee 		if (error == EINPROGRESS)
8902237Smaybee 			return (0);
8913063Sperrin 		dmu_buf_rele(db, zgd);
8923063Sperrin 		kmem_free(zgd, sizeof (zgd_t));
893789Sahrens 	}
8941669Sperrin out:
8952237Smaybee 	zfs_range_unlock(rl);
896789Sahrens 	VN_RELE(ZTOV(zp));
897789Sahrens 	return (error);
898789Sahrens }
899789Sahrens 
900789Sahrens /*ARGSUSED*/
901789Sahrens static int
902789Sahrens zfs_access(vnode_t *vp, int mode, int flags, cred_t *cr)
903789Sahrens {
904789Sahrens 	znode_t *zp = VTOZ(vp);
905789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
906789Sahrens 	int error;
907789Sahrens 
908789Sahrens 	ZFS_ENTER(zfsvfs);
909789Sahrens 	error = zfs_zaccess_rwx(zp, mode, cr);
910789Sahrens 	ZFS_EXIT(zfsvfs);
911789Sahrens 	return (error);
912789Sahrens }
913789Sahrens 
914789Sahrens /*
915789Sahrens  * Lookup an entry in a directory, or an extended attribute directory.
916789Sahrens  * If it exists, return a held vnode reference for it.
917789Sahrens  *
918789Sahrens  *	IN:	dvp	- vnode of directory to search.
919789Sahrens  *		nm	- name of entry to lookup.
920789Sahrens  *		pnp	- full pathname to lookup [UNUSED].
921789Sahrens  *		flags	- LOOKUP_XATTR set if looking for an attribute.
922789Sahrens  *		rdir	- root directory vnode [UNUSED].
923789Sahrens  *		cr	- credentials of caller.
924789Sahrens  *
925789Sahrens  *	OUT:	vpp	- vnode of located entry, NULL if not found.
926789Sahrens  *
927789Sahrens  *	RETURN:	0 if success
928789Sahrens  *		error code if failure
929789Sahrens  *
930789Sahrens  * Timestamps:
931789Sahrens  *	NA
932789Sahrens  */
933789Sahrens /* ARGSUSED */
934789Sahrens static int
935789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
936789Sahrens     int flags, vnode_t *rdir, cred_t *cr)
937789Sahrens {
938789Sahrens 
939789Sahrens 	znode_t *zdp = VTOZ(dvp);
940789Sahrens 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
941789Sahrens 	int	error;
942789Sahrens 
943789Sahrens 	ZFS_ENTER(zfsvfs);
944789Sahrens 
945789Sahrens 	*vpp = NULL;
946789Sahrens 
947789Sahrens 	if (flags & LOOKUP_XATTR) {
948789Sahrens 		/*
9493234Sck153898 		 * If the xattr property is off, refuse the lookup request.
9503234Sck153898 		 */
9513234Sck153898 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
9523234Sck153898 			ZFS_EXIT(zfsvfs);
9533234Sck153898 			return (EINVAL);
9543234Sck153898 		}
9553234Sck153898 
9563234Sck153898 		/*
957789Sahrens 		 * We don't allow recursive attributes..
958789Sahrens 		 * Maybe someday we will.
959789Sahrens 		 */
960789Sahrens 		if (zdp->z_phys->zp_flags & ZFS_XATTR) {
961789Sahrens 			ZFS_EXIT(zfsvfs);
962789Sahrens 			return (EINVAL);
963789Sahrens 		}
964789Sahrens 
9653280Sck153898 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
966789Sahrens 			ZFS_EXIT(zfsvfs);
967789Sahrens 			return (error);
968789Sahrens 		}
969789Sahrens 
970789Sahrens 		/*
971789Sahrens 		 * Do we have permission to get into attribute directory?
972789Sahrens 		 */
973789Sahrens 
974789Sahrens 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, cr)) {
975789Sahrens 			VN_RELE(*vpp);
976789Sahrens 		}
977789Sahrens 
978789Sahrens 		ZFS_EXIT(zfsvfs);
979789Sahrens 		return (error);
980789Sahrens 	}
981789Sahrens 
9821512Sek110237 	if (dvp->v_type != VDIR) {
9831512Sek110237 		ZFS_EXIT(zfsvfs);
9841460Smarks 		return (ENOTDIR);
9851512Sek110237 	}
9861460Smarks 
987789Sahrens 	/*
988789Sahrens 	 * Check accessibility of directory.
989789Sahrens 	 */
990789Sahrens 
991789Sahrens 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, cr)) {
992789Sahrens 		ZFS_EXIT(zfsvfs);
993789Sahrens 		return (error);
994789Sahrens 	}
995789Sahrens 
996789Sahrens 	if ((error = zfs_dirlook(zdp, nm, vpp)) == 0) {
997789Sahrens 
998789Sahrens 		/*
999789Sahrens 		 * Convert device special files
1000789Sahrens 		 */
1001789Sahrens 		if (IS_DEVVP(*vpp)) {
1002789Sahrens 			vnode_t	*svp;
1003789Sahrens 
1004789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1005789Sahrens 			VN_RELE(*vpp);
1006789Sahrens 			if (svp == NULL)
1007789Sahrens 				error = ENOSYS;
1008789Sahrens 			else
1009789Sahrens 				*vpp = svp;
1010789Sahrens 		}
1011789Sahrens 	}
1012789Sahrens 
1013789Sahrens 	ZFS_EXIT(zfsvfs);
1014789Sahrens 	return (error);
1015789Sahrens }
1016789Sahrens 
1017789Sahrens /*
1018789Sahrens  * Attempt to create a new entry in a directory.  If the entry
1019789Sahrens  * already exists, truncate the file if permissible, else return
1020789Sahrens  * an error.  Return the vp of the created or trunc'd file.
1021789Sahrens  *
1022789Sahrens  *	IN:	dvp	- vnode of directory to put new file entry in.
1023789Sahrens  *		name	- name of new file entry.
1024789Sahrens  *		vap	- attributes of new file.
1025789Sahrens  *		excl	- flag indicating exclusive or non-exclusive mode.
1026789Sahrens  *		mode	- mode to open file with.
1027789Sahrens  *		cr	- credentials of caller.
1028789Sahrens  *		flag	- large file flag [UNUSED].
1029789Sahrens  *
1030789Sahrens  *	OUT:	vpp	- vnode of created or trunc'd entry.
1031789Sahrens  *
1032789Sahrens  *	RETURN:	0 if success
1033789Sahrens  *		error code if failure
1034789Sahrens  *
1035789Sahrens  * Timestamps:
1036789Sahrens  *	dvp - ctime|mtime updated if new entry created
1037789Sahrens  *	 vp - ctime|mtime always, atime if new
1038789Sahrens  */
1039789Sahrens /* ARGSUSED */
1040789Sahrens static int
1041789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1042789Sahrens     int mode, vnode_t **vpp, cred_t *cr, int flag)
1043789Sahrens {
1044789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1045789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1046789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1047789Sahrens 	objset_t	*os = zfsvfs->z_os;
1048789Sahrens 	zfs_dirlock_t	*dl;
1049789Sahrens 	dmu_tx_t	*tx;
1050789Sahrens 	int		error;
1051789Sahrens 	uint64_t	zoid;
1052789Sahrens 
1053789Sahrens 	ZFS_ENTER(zfsvfs);
1054789Sahrens 
1055789Sahrens top:
1056789Sahrens 	*vpp = NULL;
1057789Sahrens 
1058789Sahrens 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1059789Sahrens 		vap->va_mode &= ~VSVTX;
1060789Sahrens 
1061789Sahrens 	if (*name == '\0') {
1062789Sahrens 		/*
1063789Sahrens 		 * Null component name refers to the directory itself.
1064789Sahrens 		 */
1065789Sahrens 		VN_HOLD(dvp);
1066789Sahrens 		zp = dzp;
1067789Sahrens 		dl = NULL;
1068789Sahrens 		error = 0;
1069789Sahrens 	} else {
1070789Sahrens 		/* possible VN_HOLD(zp) */
1071789Sahrens 		if (error = zfs_dirent_lock(&dl, dzp, name, &zp, 0)) {
1072789Sahrens 			if (strcmp(name, "..") == 0)
1073789Sahrens 				error = EISDIR;
1074789Sahrens 			ZFS_EXIT(zfsvfs);
1075789Sahrens 			return (error);
1076789Sahrens 		}
1077789Sahrens 	}
1078789Sahrens 
1079789Sahrens 	zoid = zp ? zp->z_id : -1ULL;
1080789Sahrens 
1081789Sahrens 	if (zp == NULL) {
1082789Sahrens 		/*
1083789Sahrens 		 * Create a new file object and update the directory
1084789Sahrens 		 * to reference it.
1085789Sahrens 		 */
1086789Sahrens 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
1087789Sahrens 			goto out;
1088789Sahrens 		}
1089789Sahrens 
1090789Sahrens 		/*
1091789Sahrens 		 * We only support the creation of regular files in
1092789Sahrens 		 * extended attribute directories.
1093789Sahrens 		 */
1094789Sahrens 		if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1095789Sahrens 		    (vap->va_type != VREG)) {
1096789Sahrens 			error = EINVAL;
1097789Sahrens 			goto out;
1098789Sahrens 		}
1099789Sahrens 
1100789Sahrens 		tx = dmu_tx_create(os);
1101789Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1102789Sahrens 		dmu_tx_hold_bonus(tx, dzp->z_id);
11031544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1104789Sahrens 		if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
1105789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1106789Sahrens 			    0, SPA_MAXBLOCKSIZE);
1107789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
1108789Sahrens 		if (error) {
1109789Sahrens 			zfs_dirent_unlock(dl);
1110789Sahrens 			if (error == ERESTART &&
1111789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
11122113Sahrens 				dmu_tx_wait(tx);
11132113Sahrens 				dmu_tx_abort(tx);
1114789Sahrens 				goto top;
1115789Sahrens 			}
11162113Sahrens 			dmu_tx_abort(tx);
1117789Sahrens 			ZFS_EXIT(zfsvfs);
1118789Sahrens 			return (error);
1119789Sahrens 		}
1120789Sahrens 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
1121789Sahrens 		ASSERT(zp->z_id == zoid);
1122789Sahrens 		(void) zfs_link_create(dl, zp, tx, ZNEW);
11232638Sperrin 		zfs_log_create(zilog, tx, TX_CREATE, dzp, zp, name);
1124789Sahrens 		dmu_tx_commit(tx);
1125789Sahrens 	} else {
1126789Sahrens 		/*
1127789Sahrens 		 * A directory entry already exists for this name.
1128789Sahrens 		 */
1129789Sahrens 		/*
1130789Sahrens 		 * Can't truncate an existing file if in exclusive mode.
1131789Sahrens 		 */
1132789Sahrens 		if (excl == EXCL) {
1133789Sahrens 			error = EEXIST;
1134789Sahrens 			goto out;
1135789Sahrens 		}
1136789Sahrens 		/*
1137789Sahrens 		 * Can't open a directory for writing.
1138789Sahrens 		 */
1139789Sahrens 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1140789Sahrens 			error = EISDIR;
1141789Sahrens 			goto out;
1142789Sahrens 		}
1143789Sahrens 		/*
1144789Sahrens 		 * Verify requested access to file.
1145789Sahrens 		 */
1146789Sahrens 		if (mode && (error = zfs_zaccess_rwx(zp, mode, cr))) {
1147789Sahrens 			goto out;
1148789Sahrens 		}
1149789Sahrens 
1150789Sahrens 		mutex_enter(&dzp->z_lock);
1151789Sahrens 		dzp->z_seq++;
1152789Sahrens 		mutex_exit(&dzp->z_lock);
1153789Sahrens 
11541878Smaybee 		/*
11551878Smaybee 		 * Truncate regular files if requested.
11561878Smaybee 		 */
11571878Smaybee 		if ((ZTOV(zp)->v_type == VREG) &&
1158789Sahrens 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
11591878Smaybee 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
11601878Smaybee 			if (error == ERESTART &&
11611878Smaybee 			    zfsvfs->z_assign == TXG_NOWAIT) {
11622113Sahrens 				/* NB: we already did dmu_tx_wait() */
11631878Smaybee 				zfs_dirent_unlock(dl);
11642365Sperrin 				VN_RELE(ZTOV(zp));
11651878Smaybee 				goto top;
1166789Sahrens 			}
1167789Sahrens 		}
1168789Sahrens 	}
1169789Sahrens out:
1170789Sahrens 
1171789Sahrens 	if (dl)
1172789Sahrens 		zfs_dirent_unlock(dl);
1173789Sahrens 
1174789Sahrens 	if (error) {
1175789Sahrens 		if (zp)
1176789Sahrens 			VN_RELE(ZTOV(zp));
1177789Sahrens 	} else {
1178789Sahrens 		*vpp = ZTOV(zp);
1179789Sahrens 		/*
1180789Sahrens 		 * If vnode is for a device return a specfs vnode instead.
1181789Sahrens 		 */
1182789Sahrens 		if (IS_DEVVP(*vpp)) {
1183789Sahrens 			struct vnode *svp;
1184789Sahrens 
1185789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1186789Sahrens 			VN_RELE(*vpp);
1187789Sahrens 			if (svp == NULL) {
1188789Sahrens 				error = ENOSYS;
1189789Sahrens 			}
1190789Sahrens 			*vpp = svp;
1191789Sahrens 		}
1192789Sahrens 	}
1193789Sahrens 
1194789Sahrens 	ZFS_EXIT(zfsvfs);
1195789Sahrens 	return (error);
1196789Sahrens }
1197789Sahrens 
1198789Sahrens /*
1199789Sahrens  * Remove an entry from a directory.
1200789Sahrens  *
1201789Sahrens  *	IN:	dvp	- vnode of directory to remove entry from.
1202789Sahrens  *		name	- name of entry to remove.
1203789Sahrens  *		cr	- credentials of caller.
1204789Sahrens  *
1205789Sahrens  *	RETURN:	0 if success
1206789Sahrens  *		error code if failure
1207789Sahrens  *
1208789Sahrens  * Timestamps:
1209789Sahrens  *	dvp - ctime|mtime
1210789Sahrens  *	 vp - ctime (if nlink > 0)
1211789Sahrens  */
1212789Sahrens static int
1213789Sahrens zfs_remove(vnode_t *dvp, char *name, cred_t *cr)
1214789Sahrens {
1215789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1216789Sahrens 	znode_t		*xzp = NULL;
1217789Sahrens 	vnode_t		*vp;
1218789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1219789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1220789Sahrens 	uint64_t	acl_obj, xattr_obj;
1221789Sahrens 	zfs_dirlock_t	*dl;
1222789Sahrens 	dmu_tx_t	*tx;
12233461Sahrens 	boolean_t	may_delete_now, delete_now = FALSE;
12243461Sahrens 	boolean_t	unlinked;
1225789Sahrens 	int		error;
1226789Sahrens 
1227789Sahrens 	ZFS_ENTER(zfsvfs);
1228789Sahrens 
1229789Sahrens top:
1230789Sahrens 	/*
1231789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1232789Sahrens 	 */
1233789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
1234789Sahrens 		ZFS_EXIT(zfsvfs);
1235789Sahrens 		return (error);
1236789Sahrens 	}
1237789Sahrens 
1238789Sahrens 	vp = ZTOV(zp);
1239789Sahrens 
1240789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1241789Sahrens 		goto out;
1242789Sahrens 	}
1243789Sahrens 
1244789Sahrens 	/*
1245789Sahrens 	 * Need to use rmdir for removing directories.
1246789Sahrens 	 */
1247789Sahrens 	if (vp->v_type == VDIR) {
1248789Sahrens 		error = EPERM;
1249789Sahrens 		goto out;
1250789Sahrens 	}
1251789Sahrens 
1252789Sahrens 	vnevent_remove(vp);
1253789Sahrens 
12541484Sek110237 	dnlc_remove(dvp, name);
12551484Sek110237 
1256789Sahrens 	mutex_enter(&vp->v_lock);
1257789Sahrens 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1258789Sahrens 	mutex_exit(&vp->v_lock);
1259789Sahrens 
1260789Sahrens 	/*
12613461Sahrens 	 * We may delete the znode now, or we may put it in the unlinked set;
1262789Sahrens 	 * it depends on whether we're the last link, and on whether there are
1263789Sahrens 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1264789Sahrens 	 * allow for either case.
1265789Sahrens 	 */
1266789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
12671544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1268789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
1269789Sahrens 	if (may_delete_now)
1270789Sahrens 		dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
1271789Sahrens 
1272789Sahrens 	/* are there any extended attributes? */
1273789Sahrens 	if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1274789Sahrens 		/* XXX - do we need this if we are deleting? */
1275789Sahrens 		dmu_tx_hold_bonus(tx, xattr_obj);
1276789Sahrens 	}
1277789Sahrens 
1278789Sahrens 	/* are there any additional acls */
1279789Sahrens 	if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1280789Sahrens 	    may_delete_now)
1281789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1282789Sahrens 
1283789Sahrens 	/* charge as an update -- would be nice not to charge at all */
12843461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1285789Sahrens 
1286789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1287789Sahrens 	if (error) {
1288789Sahrens 		zfs_dirent_unlock(dl);
1289789Sahrens 		VN_RELE(vp);
1290789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
12912113Sahrens 			dmu_tx_wait(tx);
12922113Sahrens 			dmu_tx_abort(tx);
1293789Sahrens 			goto top;
1294789Sahrens 		}
12952113Sahrens 		dmu_tx_abort(tx);
1296789Sahrens 		ZFS_EXIT(zfsvfs);
1297789Sahrens 		return (error);
1298789Sahrens 	}
1299789Sahrens 
1300789Sahrens 	/*
1301789Sahrens 	 * Remove the directory entry.
1302789Sahrens 	 */
13033461Sahrens 	error = zfs_link_destroy(dl, zp, tx, 0, &unlinked);
1304789Sahrens 
1305789Sahrens 	if (error) {
1306789Sahrens 		dmu_tx_commit(tx);
1307789Sahrens 		goto out;
1308789Sahrens 	}
1309789Sahrens 
13103461Sahrens 	if (unlinked) {
1311789Sahrens 		mutex_enter(&vp->v_lock);
1312789Sahrens 		delete_now = may_delete_now &&
1313789Sahrens 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1314789Sahrens 		    zp->z_phys->zp_xattr == xattr_obj &&
1315789Sahrens 		    zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1316789Sahrens 		mutex_exit(&vp->v_lock);
1317789Sahrens 	}
1318789Sahrens 
1319789Sahrens 	if (delete_now) {
1320789Sahrens 		if (zp->z_phys->zp_xattr) {
1321789Sahrens 			error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1322789Sahrens 			ASSERT3U(error, ==, 0);
1323789Sahrens 			ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1324789Sahrens 			dmu_buf_will_dirty(xzp->z_dbuf, tx);
1325789Sahrens 			mutex_enter(&xzp->z_lock);
13263461Sahrens 			xzp->z_unlinked = 1;
1327789Sahrens 			xzp->z_phys->zp_links = 0;
1328789Sahrens 			mutex_exit(&xzp->z_lock);
13293461Sahrens 			zfs_unlinked_add(xzp, tx);
1330789Sahrens 			zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1331789Sahrens 		}
1332789Sahrens 		mutex_enter(&zp->z_lock);
1333789Sahrens 		mutex_enter(&vp->v_lock);
1334789Sahrens 		vp->v_count--;
1335789Sahrens 		ASSERT3U(vp->v_count, ==, 0);
1336789Sahrens 		mutex_exit(&vp->v_lock);
1337789Sahrens 		mutex_exit(&zp->z_lock);
1338789Sahrens 		zfs_znode_delete(zp, tx);
1339789Sahrens 		VFS_RELE(zfsvfs->z_vfs);
13403461Sahrens 	} else if (unlinked) {
13413461Sahrens 		zfs_unlinked_add(zp, tx);
1342789Sahrens 	}
1343789Sahrens 
13442638Sperrin 	zfs_log_remove(zilog, tx, TX_REMOVE, dzp, name);
1345789Sahrens 
1346789Sahrens 	dmu_tx_commit(tx);
1347789Sahrens out:
1348789Sahrens 	zfs_dirent_unlock(dl);
1349789Sahrens 
1350789Sahrens 	if (!delete_now) {
1351789Sahrens 		VN_RELE(vp);
1352789Sahrens 	} else if (xzp) {
1353789Sahrens 		/* this rele delayed to prevent nesting transactions */
1354789Sahrens 		VN_RELE(ZTOV(xzp));
1355789Sahrens 	}
1356789Sahrens 
1357789Sahrens 	ZFS_EXIT(zfsvfs);
1358789Sahrens 	return (error);
1359789Sahrens }
1360789Sahrens 
1361789Sahrens /*
1362789Sahrens  * Create a new directory and insert it into dvp using the name
1363789Sahrens  * provided.  Return a pointer to the inserted directory.
1364789Sahrens  *
1365789Sahrens  *	IN:	dvp	- vnode of directory to add subdir to.
1366789Sahrens  *		dirname	- name of new directory.
1367789Sahrens  *		vap	- attributes of new directory.
1368789Sahrens  *		cr	- credentials of caller.
1369789Sahrens  *
1370789Sahrens  *	OUT:	vpp	- vnode of created directory.
1371789Sahrens  *
1372789Sahrens  *	RETURN:	0 if success
1373789Sahrens  *		error code if failure
1374789Sahrens  *
1375789Sahrens  * Timestamps:
1376789Sahrens  *	dvp - ctime|mtime updated
1377789Sahrens  *	 vp - ctime|mtime|atime updated
1378789Sahrens  */
1379789Sahrens static int
1380789Sahrens zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr)
1381789Sahrens {
1382789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1383789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1384789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1385789Sahrens 	zfs_dirlock_t	*dl;
1386789Sahrens 	uint64_t	zoid = 0;
1387789Sahrens 	dmu_tx_t	*tx;
1388789Sahrens 	int		error;
1389789Sahrens 
1390789Sahrens 	ASSERT(vap->va_type == VDIR);
1391789Sahrens 
1392789Sahrens 	ZFS_ENTER(zfsvfs);
1393789Sahrens 
1394789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1395789Sahrens 		ZFS_EXIT(zfsvfs);
1396789Sahrens 		return (EINVAL);
1397789Sahrens 	}
1398789Sahrens top:
1399789Sahrens 	*vpp = NULL;
1400789Sahrens 
1401789Sahrens 	/*
1402789Sahrens 	 * First make sure the new directory doesn't exist.
1403789Sahrens 	 */
1404789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, ZNEW)) {
1405789Sahrens 		ZFS_EXIT(zfsvfs);
1406789Sahrens 		return (error);
1407789Sahrens 	}
1408789Sahrens 
14091231Smarks 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, cr)) {
14101231Smarks 		zfs_dirent_unlock(dl);
14111231Smarks 		ZFS_EXIT(zfsvfs);
14121231Smarks 		return (error);
14131231Smarks 	}
14141231Smarks 
1415789Sahrens 	/*
1416789Sahrens 	 * Add a new entry to the directory.
1417789Sahrens 	 */
1418789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
14191544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
14201544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1421789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
1422789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1423789Sahrens 		    0, SPA_MAXBLOCKSIZE);
1424789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1425789Sahrens 	if (error) {
1426789Sahrens 		zfs_dirent_unlock(dl);
1427789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
14282113Sahrens 			dmu_tx_wait(tx);
14292113Sahrens 			dmu_tx_abort(tx);
1430789Sahrens 			goto top;
1431789Sahrens 		}
14322113Sahrens 		dmu_tx_abort(tx);
1433789Sahrens 		ZFS_EXIT(zfsvfs);
1434789Sahrens 		return (error);
1435789Sahrens 	}
1436789Sahrens 
1437789Sahrens 	/*
1438789Sahrens 	 * Create new node.
1439789Sahrens 	 */
1440789Sahrens 	zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
1441789Sahrens 
1442789Sahrens 	/*
1443789Sahrens 	 * Now put new name in parent dir.
1444789Sahrens 	 */
1445789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1446789Sahrens 
1447789Sahrens 	*vpp = ZTOV(zp);
1448789Sahrens 
14492638Sperrin 	zfs_log_create(zilog, tx, TX_MKDIR, dzp, zp, dirname);
1450789Sahrens 	dmu_tx_commit(tx);
1451789Sahrens 
1452789Sahrens 	zfs_dirent_unlock(dl);
1453789Sahrens 
1454789Sahrens 	ZFS_EXIT(zfsvfs);
1455789Sahrens 	return (0);
1456789Sahrens }
1457789Sahrens 
1458789Sahrens /*
1459789Sahrens  * Remove a directory subdir entry.  If the current working
1460789Sahrens  * directory is the same as the subdir to be removed, the
1461789Sahrens  * remove will fail.
1462789Sahrens  *
1463789Sahrens  *	IN:	dvp	- vnode of directory to remove from.
1464789Sahrens  *		name	- name of directory to be removed.
1465789Sahrens  *		cwd	- vnode of current working directory.
1466789Sahrens  *		cr	- credentials of caller.
1467789Sahrens  *
1468789Sahrens  *	RETURN:	0 if success
1469789Sahrens  *		error code if failure
1470789Sahrens  *
1471789Sahrens  * Timestamps:
1472789Sahrens  *	dvp - ctime|mtime updated
1473789Sahrens  */
1474789Sahrens static int
1475789Sahrens zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr)
1476789Sahrens {
1477789Sahrens 	znode_t		*dzp = VTOZ(dvp);
1478789Sahrens 	znode_t		*zp;
1479789Sahrens 	vnode_t		*vp;
1480789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1481789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1482789Sahrens 	zfs_dirlock_t	*dl;
1483789Sahrens 	dmu_tx_t	*tx;
1484789Sahrens 	int		error;
1485789Sahrens 
1486789Sahrens 	ZFS_ENTER(zfsvfs);
1487789Sahrens 
1488789Sahrens top:
1489789Sahrens 	zp = NULL;
1490789Sahrens 
1491789Sahrens 	/*
1492789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1493789Sahrens 	 */
1494789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
1495789Sahrens 		ZFS_EXIT(zfsvfs);
1496789Sahrens 		return (error);
1497789Sahrens 	}
1498789Sahrens 
1499789Sahrens 	vp = ZTOV(zp);
1500789Sahrens 
1501789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1502789Sahrens 		goto out;
1503789Sahrens 	}
1504789Sahrens 
1505789Sahrens 	if (vp->v_type != VDIR) {
1506789Sahrens 		error = ENOTDIR;
1507789Sahrens 		goto out;
1508789Sahrens 	}
1509789Sahrens 
1510789Sahrens 	if (vp == cwd) {
1511789Sahrens 		error = EINVAL;
1512789Sahrens 		goto out;
1513789Sahrens 	}
1514789Sahrens 
1515789Sahrens 	vnevent_rmdir(vp);
1516789Sahrens 
1517789Sahrens 	/*
15183897Smaybee 	 * Grab a lock on the directory to make sure that noone is
15193897Smaybee 	 * trying to add (or lookup) entries while we are removing it.
15203897Smaybee 	 */
15213897Smaybee 	rw_enter(&zp->z_name_lock, RW_WRITER);
15223897Smaybee 
15233897Smaybee 	/*
15243897Smaybee 	 * Grab a lock on the parent pointer to make sure we play well
1525789Sahrens 	 * with the treewalk and directory rename code.
1526789Sahrens 	 */
1527789Sahrens 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1528789Sahrens 
1529789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
15301544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1531789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
15323461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1533789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1534789Sahrens 	if (error) {
1535789Sahrens 		rw_exit(&zp->z_parent_lock);
15363897Smaybee 		rw_exit(&zp->z_name_lock);
1537789Sahrens 		zfs_dirent_unlock(dl);
1538789Sahrens 		VN_RELE(vp);
1539789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
15402113Sahrens 			dmu_tx_wait(tx);
15412113Sahrens 			dmu_tx_abort(tx);
1542789Sahrens 			goto top;
1543789Sahrens 		}
15442113Sahrens 		dmu_tx_abort(tx);
1545789Sahrens 		ZFS_EXIT(zfsvfs);
1546789Sahrens 		return (error);
1547789Sahrens 	}
1548789Sahrens 
1549789Sahrens 	error = zfs_link_destroy(dl, zp, tx, 0, NULL);
1550789Sahrens 
1551789Sahrens 	if (error == 0)
15522638Sperrin 		zfs_log_remove(zilog, tx, TX_RMDIR, dzp, name);
1553789Sahrens 
1554789Sahrens 	dmu_tx_commit(tx);
1555789Sahrens 
1556789Sahrens 	rw_exit(&zp->z_parent_lock);
15573897Smaybee 	rw_exit(&zp->z_name_lock);
1558789Sahrens out:
1559789Sahrens 	zfs_dirent_unlock(dl);
1560789Sahrens 
1561789Sahrens 	VN_RELE(vp);
1562789Sahrens 
1563789Sahrens 	ZFS_EXIT(zfsvfs);
1564789Sahrens 	return (error);
1565789Sahrens }
1566789Sahrens 
1567789Sahrens /*
1568789Sahrens  * Read as many directory entries as will fit into the provided
1569789Sahrens  * buffer from the given directory cursor position (specified in
1570789Sahrens  * the uio structure.
1571789Sahrens  *
1572789Sahrens  *	IN:	vp	- vnode of directory to read.
1573789Sahrens  *		uio	- structure supplying read location, range info,
1574789Sahrens  *			  and return buffer.
1575789Sahrens  *		cr	- credentials of caller.
1576789Sahrens  *
1577789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
1578789Sahrens  *		eofp	- set to true if end-of-file detected.
1579789Sahrens  *
1580789Sahrens  *	RETURN:	0 if success
1581789Sahrens  *		error code if failure
1582789Sahrens  *
1583789Sahrens  * Timestamps:
1584789Sahrens  *	vp - atime updated
1585789Sahrens  *
1586789Sahrens  * Note that the low 4 bits of the cookie returned by zap is always zero.
1587789Sahrens  * This allows us to use the low range for "special" directory entries:
1588789Sahrens  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
1589789Sahrens  * we use the offset 2 for the '.zfs' directory.
1590789Sahrens  */
1591789Sahrens /* ARGSUSED */
1592789Sahrens static int
1593789Sahrens zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp)
1594789Sahrens {
1595789Sahrens 	znode_t		*zp = VTOZ(vp);
1596789Sahrens 	iovec_t		*iovp;
1597789Sahrens 	dirent64_t	*odp;
1598789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1599869Sperrin 	objset_t	*os;
1600789Sahrens 	caddr_t		outbuf;
1601789Sahrens 	size_t		bufsize;
1602789Sahrens 	zap_cursor_t	zc;
1603789Sahrens 	zap_attribute_t	zap;
1604789Sahrens 	uint_t		bytes_wanted;
1605789Sahrens 	uint64_t	offset; /* must be unsigned; checks for < 1 */
1606789Sahrens 	int		local_eof;
1607869Sperrin 	int		outcount;
1608869Sperrin 	int		error;
1609869Sperrin 	uint8_t		prefetch;
1610789Sahrens 
1611789Sahrens 	ZFS_ENTER(zfsvfs);
1612789Sahrens 
1613789Sahrens 	/*
1614789Sahrens 	 * If we are not given an eof variable,
1615789Sahrens 	 * use a local one.
1616789Sahrens 	 */
1617789Sahrens 	if (eofp == NULL)
1618789Sahrens 		eofp = &local_eof;
1619789Sahrens 
1620789Sahrens 	/*
1621789Sahrens 	 * Check for valid iov_len.
1622789Sahrens 	 */
1623789Sahrens 	if (uio->uio_iov->iov_len <= 0) {
1624789Sahrens 		ZFS_EXIT(zfsvfs);
1625789Sahrens 		return (EINVAL);
1626789Sahrens 	}
1627789Sahrens 
1628789Sahrens 	/*
1629789Sahrens 	 * Quit if directory has been removed (posix)
1630789Sahrens 	 */
16313461Sahrens 	if ((*eofp = zp->z_unlinked) != 0) {
1632789Sahrens 		ZFS_EXIT(zfsvfs);
1633789Sahrens 		return (0);
1634789Sahrens 	}
1635789Sahrens 
1636869Sperrin 	error = 0;
1637869Sperrin 	os = zfsvfs->z_os;
1638869Sperrin 	offset = uio->uio_loffset;
1639869Sperrin 	prefetch = zp->z_zn_prefetch;
1640869Sperrin 
1641789Sahrens 	/*
1642789Sahrens 	 * Initialize the iterator cursor.
1643789Sahrens 	 */
1644789Sahrens 	if (offset <= 3) {
1645789Sahrens 		/*
1646789Sahrens 		 * Start iteration from the beginning of the directory.
1647789Sahrens 		 */
1648869Sperrin 		zap_cursor_init(&zc, os, zp->z_id);
1649789Sahrens 	} else {
1650789Sahrens 		/*
1651789Sahrens 		 * The offset is a serialized cursor.
1652789Sahrens 		 */
1653869Sperrin 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1654789Sahrens 	}
1655789Sahrens 
1656789Sahrens 	/*
1657789Sahrens 	 * Get space to change directory entries into fs independent format.
1658789Sahrens 	 */
1659789Sahrens 	iovp = uio->uio_iov;
1660789Sahrens 	bytes_wanted = iovp->iov_len;
1661789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
1662789Sahrens 		bufsize = bytes_wanted;
1663789Sahrens 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
1664789Sahrens 		odp = (struct dirent64 *)outbuf;
1665789Sahrens 	} else {
1666789Sahrens 		bufsize = bytes_wanted;
1667789Sahrens 		odp = (struct dirent64 *)iovp->iov_base;
1668789Sahrens 	}
1669789Sahrens 
1670789Sahrens 	/*
1671789Sahrens 	 * Transform to file-system independent format
1672789Sahrens 	 */
1673789Sahrens 	outcount = 0;
1674789Sahrens 	while (outcount < bytes_wanted) {
16753912Slling 		ino64_t objnum;
16763912Slling 		ushort_t reclen;
16773912Slling 		off64_t *next;
16783912Slling 
1679789Sahrens 		/*
1680789Sahrens 		 * Special case `.', `..', and `.zfs'.
1681789Sahrens 		 */
1682789Sahrens 		if (offset == 0) {
1683789Sahrens 			(void) strcpy(zap.za_name, ".");
16843912Slling 			objnum = zp->z_id;
1685789Sahrens 		} else if (offset == 1) {
1686789Sahrens 			(void) strcpy(zap.za_name, "..");
16873912Slling 			objnum = zp->z_phys->zp_parent;
1688789Sahrens 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
1689789Sahrens 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
16903912Slling 			objnum = ZFSCTL_INO_ROOT;
1691789Sahrens 		} else {
1692789Sahrens 			/*
1693789Sahrens 			 * Grab next entry.
1694789Sahrens 			 */
1695789Sahrens 			if (error = zap_cursor_retrieve(&zc, &zap)) {
1696789Sahrens 				if ((*eofp = (error == ENOENT)) != 0)
1697789Sahrens 					break;
1698789Sahrens 				else
1699789Sahrens 					goto update;
1700789Sahrens 			}
1701789Sahrens 
1702789Sahrens 			if (zap.za_integer_length != 8 ||
1703789Sahrens 			    zap.za_num_integers != 1) {
1704789Sahrens 				cmn_err(CE_WARN, "zap_readdir: bad directory "
1705789Sahrens 				    "entry, obj = %lld, offset = %lld\n",
1706789Sahrens 				    (u_longlong_t)zp->z_id,
1707789Sahrens 				    (u_longlong_t)offset);
1708789Sahrens 				error = ENXIO;
1709789Sahrens 				goto update;
1710789Sahrens 			}
17113912Slling 
17123912Slling 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
17133912Slling 			/*
17143912Slling 			 * MacOS X can extract the object type here such as:
17153912Slling 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
17163912Slling 			 */
1717789Sahrens 		}
17183912Slling 		reclen = DIRENT64_RECLEN(strlen(zap.za_name));
1719789Sahrens 
1720789Sahrens 		/*
1721789Sahrens 		 * Will this entry fit in the buffer?
1722789Sahrens 		 */
17233912Slling 		if (outcount + reclen > bufsize) {
1724789Sahrens 			/*
1725789Sahrens 			 * Did we manage to fit anything in the buffer?
1726789Sahrens 			 */
1727789Sahrens 			if (!outcount) {
1728789Sahrens 				error = EINVAL;
1729789Sahrens 				goto update;
1730789Sahrens 			}
1731789Sahrens 			break;
1732789Sahrens 		}
1733789Sahrens 		/*
1734789Sahrens 		 * Add this entry:
1735789Sahrens 		 */
17363912Slling 		odp->d_ino = objnum;
17373912Slling 		odp->d_reclen = reclen;
1738789Sahrens 		/* NOTE: d_off is the offset for the *next* entry */
1739789Sahrens 		next = &(odp->d_off);
1740789Sahrens 		(void) strncpy(odp->d_name, zap.za_name,
17413912Slling 		    DIRENT64_NAMELEN(reclen));
17423912Slling 		outcount += reclen;
17433912Slling 		odp = (dirent64_t *)((intptr_t)odp + reclen);
1744789Sahrens 
1745789Sahrens 		ASSERT(outcount <= bufsize);
1746789Sahrens 
1747789Sahrens 		/* Prefetch znode */
1748869Sperrin 		if (prefetch)
17493912Slling 			dmu_prefetch(os, objnum, 0, 0);
1750789Sahrens 
1751789Sahrens 		/*
1752789Sahrens 		 * Move to the next entry, fill in the previous offset.
1753789Sahrens 		 */
1754789Sahrens 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
1755789Sahrens 			zap_cursor_advance(&zc);
1756789Sahrens 			offset = zap_cursor_serialize(&zc);
1757789Sahrens 		} else {
1758789Sahrens 			offset += 1;
1759789Sahrens 		}
1760789Sahrens 		*next = offset;
1761789Sahrens 	}
1762869Sperrin 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
1763789Sahrens 
1764789Sahrens 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
1765789Sahrens 		iovp->iov_base += outcount;
1766789Sahrens 		iovp->iov_len -= outcount;
1767789Sahrens 		uio->uio_resid -= outcount;
1768789Sahrens 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
1769789Sahrens 		/*
1770789Sahrens 		 * Reset the pointer.
1771789Sahrens 		 */
1772789Sahrens 		offset = uio->uio_loffset;
1773789Sahrens 	}
1774789Sahrens 
1775789Sahrens update:
1776885Sahrens 	zap_cursor_fini(&zc);
1777789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1778789Sahrens 		kmem_free(outbuf, bufsize);
1779789Sahrens 
1780789Sahrens 	if (error == ENOENT)
1781789Sahrens 		error = 0;
1782789Sahrens 
1783789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
1784789Sahrens 
1785789Sahrens 	uio->uio_loffset = offset;
1786789Sahrens 	ZFS_EXIT(zfsvfs);
1787789Sahrens 	return (error);
1788789Sahrens }
1789789Sahrens 
1790789Sahrens static int
1791789Sahrens zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr)
1792789Sahrens {
1793789Sahrens 	znode_t	*zp = VTOZ(vp);
1794789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1795789Sahrens 
17961773Seschrock 	/*
17971773Seschrock 	 * Regardless of whether this is required for standards conformance,
17981773Seschrock 	 * this is the logical behavior when fsync() is called on a file with
17991773Seschrock 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
18001773Seschrock 	 * going to be pushed out as part of the zil_commit().
18011773Seschrock 	 */
18021773Seschrock 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
18031773Seschrock 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
18041773Seschrock 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr);
18051773Seschrock 
1806789Sahrens 	ZFS_ENTER(zfsvfs);
18072638Sperrin 	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
1808789Sahrens 	ZFS_EXIT(zfsvfs);
1809789Sahrens 	return (0);
1810789Sahrens }
1811789Sahrens 
1812789Sahrens /*
1813789Sahrens  * Get the requested file attributes and place them in the provided
1814789Sahrens  * vattr structure.
1815789Sahrens  *
1816789Sahrens  *	IN:	vp	- vnode of file.
1817789Sahrens  *		vap	- va_mask identifies requested attributes.
1818789Sahrens  *		flags	- [UNUSED]
1819789Sahrens  *		cr	- credentials of caller.
1820789Sahrens  *
1821789Sahrens  *	OUT:	vap	- attribute values.
1822789Sahrens  *
1823789Sahrens  *	RETURN:	0 (always succeeds)
1824789Sahrens  */
1825789Sahrens /* ARGSUSED */
1826789Sahrens static int
1827789Sahrens zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
1828789Sahrens {
1829789Sahrens 	znode_t *zp = VTOZ(vp);
1830789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1831789Sahrens 	znode_phys_t *pzp = zp->z_phys;
1832789Sahrens 	int	error;
1833789Sahrens 
1834789Sahrens 	ZFS_ENTER(zfsvfs);
1835789Sahrens 
1836789Sahrens 	/*
1837789Sahrens 	 * Return all attributes.  It's cheaper to provide the answer
1838789Sahrens 	 * than to determine whether we were asked the question.
1839789Sahrens 	 */
1840789Sahrens 	mutex_enter(&zp->z_lock);
1841789Sahrens 
1842789Sahrens 	vap->va_type = vp->v_type;
1843789Sahrens 	vap->va_mode = pzp->zp_mode & MODEMASK;
1844789Sahrens 	vap->va_uid = zp->z_phys->zp_uid;
1845789Sahrens 	vap->va_gid = zp->z_phys->zp_gid;
1846789Sahrens 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
1847789Sahrens 	vap->va_nodeid = zp->z_id;
1848789Sahrens 	vap->va_nlink = MIN(pzp->zp_links, UINT32_MAX);	/* nlink_t limit! */
1849789Sahrens 	vap->va_size = pzp->zp_size;
18501816Smarks 	vap->va_rdev = vp->v_rdev;
1851789Sahrens 	vap->va_seq = zp->z_seq;
1852789Sahrens 
1853789Sahrens 	ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
1854789Sahrens 	ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
1855789Sahrens 	ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
1856789Sahrens 
1857789Sahrens 	/*
1858905Smarks 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
1859905Smarks 	 * Also, if we are the owner don't bother, since owner should
1860905Smarks 	 * always be allowed to read basic attributes of file.
1861789Sahrens 	 */
1862905Smarks 	if (!(zp->z_phys->zp_flags & ZFS_ACL_TRIVIAL) &&
1863905Smarks 	    (zp->z_phys->zp_uid != crgetuid(cr))) {
1864905Smarks 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, cr)) {
1865789Sahrens 			mutex_exit(&zp->z_lock);
1866789Sahrens 			ZFS_EXIT(zfsvfs);
1867789Sahrens 			return (error);
1868789Sahrens 		}
1869789Sahrens 	}
1870789Sahrens 
1871789Sahrens 	mutex_exit(&zp->z_lock);
1872789Sahrens 
1873789Sahrens 	dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks);
1874789Sahrens 
1875789Sahrens 	if (zp->z_blksz == 0) {
1876789Sahrens 		/*
1877789Sahrens 		 * Block size hasn't been set; suggest maximal I/O transfers.
1878789Sahrens 		 */
1879789Sahrens 		vap->va_blksize = zfsvfs->z_max_blksz;
1880789Sahrens 	}
1881789Sahrens 
1882789Sahrens 	ZFS_EXIT(zfsvfs);
1883789Sahrens 	return (0);
1884789Sahrens }
1885789Sahrens 
1886789Sahrens /*
1887789Sahrens  * Set the file attributes to the values contained in the
1888789Sahrens  * vattr structure.
1889789Sahrens  *
1890789Sahrens  *	IN:	vp	- vnode of file to be modified.
1891789Sahrens  *		vap	- new attribute values.
1892789Sahrens  *		flags	- ATTR_UTIME set if non-default time values provided.
1893789Sahrens  *		cr	- credentials of caller.
1894789Sahrens  *
1895789Sahrens  *	RETURN:	0 if success
1896789Sahrens  *		error code if failure
1897789Sahrens  *
1898789Sahrens  * Timestamps:
1899789Sahrens  *	vp - ctime updated, mtime updated if size changed.
1900789Sahrens  */
1901789Sahrens /* ARGSUSED */
1902789Sahrens static int
1903789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1904789Sahrens 	caller_context_t *ct)
1905789Sahrens {
1906789Sahrens 	struct znode	*zp = VTOZ(vp);
1907789Sahrens 	znode_phys_t	*pzp = zp->z_phys;
1908789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1909789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1910789Sahrens 	dmu_tx_t	*tx;
19111878Smaybee 	vattr_t		oldva;
1912789Sahrens 	uint_t		mask = vap->va_mask;
19131878Smaybee 	uint_t		saved_mask;
19142796Smarks 	int		trim_mask = 0;
1915789Sahrens 	uint64_t	new_mode;
19161231Smarks 	znode_t		*attrzp;
1917789Sahrens 	int		need_policy = FALSE;
1918789Sahrens 	int		err;
1919789Sahrens 
1920789Sahrens 	if (mask == 0)
1921789Sahrens 		return (0);
1922789Sahrens 
1923789Sahrens 	if (mask & AT_NOSET)
1924789Sahrens 		return (EINVAL);
1925789Sahrens 
1926789Sahrens 	if (mask & AT_SIZE && vp->v_type == VDIR)
1927789Sahrens 		return (EISDIR);
1928789Sahrens 
19291394Smarks 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO)
19301308Smarks 		return (EINVAL);
19311308Smarks 
1932789Sahrens 	ZFS_ENTER(zfsvfs);
1933789Sahrens 
1934789Sahrens top:
19351231Smarks 	attrzp = NULL;
1936789Sahrens 
1937789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
1938789Sahrens 		ZFS_EXIT(zfsvfs);
1939789Sahrens 		return (EROFS);
1940789Sahrens 	}
1941789Sahrens 
1942789Sahrens 	/*
1943789Sahrens 	 * First validate permissions
1944789Sahrens 	 */
1945789Sahrens 
1946789Sahrens 	if (mask & AT_SIZE) {
1947789Sahrens 		err = zfs_zaccess(zp, ACE_WRITE_DATA, cr);
1948789Sahrens 		if (err) {
1949789Sahrens 			ZFS_EXIT(zfsvfs);
1950789Sahrens 			return (err);
1951789Sahrens 		}
19521878Smaybee 		/*
19531878Smaybee 		 * XXX - Note, we are not providing any open
19541878Smaybee 		 * mode flags here (like FNDELAY), so we may
19551878Smaybee 		 * block if there are locks present... this
19561878Smaybee 		 * should be addressed in openat().
19571878Smaybee 		 */
19581878Smaybee 		do {
19591878Smaybee 			err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
19602113Sahrens 			/* NB: we already did dmu_tx_wait() if necessary */
19611878Smaybee 		} while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
19621878Smaybee 		if (err) {
19631878Smaybee 			ZFS_EXIT(zfsvfs);
19641878Smaybee 			return (err);
19651878Smaybee 		}
1966789Sahrens 	}
1967789Sahrens 
1968789Sahrens 	if (mask & (AT_ATIME|AT_MTIME))
1969789Sahrens 		need_policy = zfs_zaccess_v4_perm(zp, ACE_WRITE_ATTRIBUTES, cr);
1970789Sahrens 
1971789Sahrens 	if (mask & (AT_UID|AT_GID)) {
1972789Sahrens 		int	idmask = (mask & (AT_UID|AT_GID));
1973789Sahrens 		int	take_owner;
1974789Sahrens 		int	take_group;
1975789Sahrens 
1976789Sahrens 		/*
1977913Smarks 		 * NOTE: even if a new mode is being set,
1978913Smarks 		 * we may clear S_ISUID/S_ISGID bits.
1979913Smarks 		 */
1980913Smarks 
1981913Smarks 		if (!(mask & AT_MODE))
1982913Smarks 			vap->va_mode = pzp->zp_mode;
1983913Smarks 
1984913Smarks 		/*
1985789Sahrens 		 * Take ownership or chgrp to group we are a member of
1986789Sahrens 		 */
1987789Sahrens 
1988789Sahrens 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
1989789Sahrens 		take_group = (mask & AT_GID) && groupmember(vap->va_gid, cr);
1990789Sahrens 
1991789Sahrens 		/*
1992789Sahrens 		 * If both AT_UID and AT_GID are set then take_owner and
1993789Sahrens 		 * take_group must both be set in order to allow taking
1994789Sahrens 		 * ownership.
1995789Sahrens 		 *
1996789Sahrens 		 * Otherwise, send the check through secpolicy_vnode_setattr()
1997789Sahrens 		 *
1998789Sahrens 		 */
1999789Sahrens 
2000789Sahrens 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2001789Sahrens 		    ((idmask == AT_UID) && take_owner) ||
2002789Sahrens 		    ((idmask == AT_GID) && take_group)) {
2003789Sahrens 			if (zfs_zaccess_v4_perm(zp, ACE_WRITE_OWNER, cr) == 0) {
2004789Sahrens 				/*
2005789Sahrens 				 * Remove setuid/setgid for non-privileged users
2006789Sahrens 				 */
20071115Smarks 				secpolicy_setid_clear(vap, cr);
20082796Smarks 				trim_mask = (mask & (AT_UID|AT_GID));
2009789Sahrens 			} else {
2010789Sahrens 				need_policy =  TRUE;
2011789Sahrens 			}
2012789Sahrens 		} else {
2013789Sahrens 			need_policy =  TRUE;
2014789Sahrens 		}
2015789Sahrens 	}
2016789Sahrens 
20172796Smarks 	mutex_enter(&zp->z_lock);
20182796Smarks 	oldva.va_mode = pzp->zp_mode;
20192796Smarks 	oldva.va_uid = zp->z_phys->zp_uid;
20202796Smarks 	oldva.va_gid = zp->z_phys->zp_gid;
20212796Smarks 	mutex_exit(&zp->z_lock);
20222796Smarks 
20232796Smarks 	if (mask & AT_MODE) {
20242796Smarks 		if (zfs_zaccess_v4_perm(zp, ACE_WRITE_ACL, cr) == 0) {
20252796Smarks 			err = secpolicy_setid_setsticky_clear(vp, vap,
20262796Smarks 			    &oldva, cr);
20272796Smarks 			if (err) {
20282796Smarks 				ZFS_EXIT(zfsvfs);
20292796Smarks 				return (err);
20302796Smarks 			}
20312796Smarks 			trim_mask |= AT_MODE;
20322796Smarks 		} else {
20332796Smarks 			need_policy = TRUE;
20342796Smarks 		}
20352796Smarks 	}
2036789Sahrens 
2037789Sahrens 	if (need_policy) {
20381115Smarks 		/*
20391115Smarks 		 * If trim_mask is set then take ownership
20402796Smarks 		 * has been granted or write_acl is present and user
20412796Smarks 		 * has the ability to modify mode.  In that case remove
20422796Smarks 		 * UID|GID and or MODE from mask so that
20431115Smarks 		 * secpolicy_vnode_setattr() doesn't revoke it.
20441115Smarks 		 */
20452796Smarks 
20462796Smarks 		if (trim_mask) {
20472796Smarks 			saved_mask = vap->va_mask;
20482796Smarks 			vap->va_mask &= ~trim_mask;
20492796Smarks 
20502796Smarks 		}
2051789Sahrens 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2052789Sahrens 		    (int (*)(void *, int, cred_t *))zfs_zaccess_rwx, zp);
2053789Sahrens 		if (err) {
2054789Sahrens 			ZFS_EXIT(zfsvfs);
2055789Sahrens 			return (err);
2056789Sahrens 		}
20571115Smarks 
20581115Smarks 		if (trim_mask)
20592796Smarks 			vap->va_mask |= saved_mask;
2060789Sahrens 	}
2061789Sahrens 
2062789Sahrens 	/*
2063789Sahrens 	 * secpolicy_vnode_setattr, or take ownership may have
2064789Sahrens 	 * changed va_mask
2065789Sahrens 	 */
2066789Sahrens 	mask = vap->va_mask;
2067789Sahrens 
2068789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2069789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
2070789Sahrens 
2071789Sahrens 	if (mask & AT_MODE) {
20721576Smarks 		uint64_t pmode = pzp->zp_mode;
20731576Smarks 
20741576Smarks 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2075789Sahrens 
2076789Sahrens 		if (zp->z_phys->zp_acl.z_acl_extern_obj)
2077789Sahrens 			dmu_tx_hold_write(tx,
2078789Sahrens 			    pzp->zp_acl.z_acl_extern_obj, 0, SPA_MAXBLOCKSIZE);
2079789Sahrens 		else
2080789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2081789Sahrens 			    0, ZFS_ACL_SIZE(MAX_ACL_SIZE));
2082789Sahrens 	}
2083789Sahrens 
20841231Smarks 	if ((mask & (AT_UID | AT_GID)) && zp->z_phys->zp_xattr != 0) {
20851231Smarks 		err = zfs_zget(zp->z_zfsvfs, zp->z_phys->zp_xattr, &attrzp);
20861231Smarks 		if (err) {
20871231Smarks 			dmu_tx_abort(tx);
20881231Smarks 			ZFS_EXIT(zfsvfs);
20891231Smarks 			return (err);
20901231Smarks 		}
20911231Smarks 		dmu_tx_hold_bonus(tx, attrzp->z_id);
20921231Smarks 	}
20931231Smarks 
2094789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2095789Sahrens 	if (err) {
20961231Smarks 		if (attrzp)
20971231Smarks 			VN_RELE(ZTOV(attrzp));
2098789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
20992113Sahrens 			dmu_tx_wait(tx);
21002113Sahrens 			dmu_tx_abort(tx);
2101789Sahrens 			goto top;
2102789Sahrens 		}
21032113Sahrens 		dmu_tx_abort(tx);
2104789Sahrens 		ZFS_EXIT(zfsvfs);
2105789Sahrens 		return (err);
2106789Sahrens 	}
2107789Sahrens 
2108789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
2109789Sahrens 
2110789Sahrens 	/*
2111789Sahrens 	 * Set each attribute requested.
2112789Sahrens 	 * We group settings according to the locks they need to acquire.
2113789Sahrens 	 *
2114789Sahrens 	 * Note: you cannot set ctime directly, although it will be
2115789Sahrens 	 * updated as a side-effect of calling this function.
2116789Sahrens 	 */
2117789Sahrens 
2118789Sahrens 	mutex_enter(&zp->z_lock);
2119789Sahrens 
2120789Sahrens 	if (mask & AT_MODE) {
2121789Sahrens 		err = zfs_acl_chmod_setattr(zp, new_mode, tx);
2122789Sahrens 		ASSERT3U(err, ==, 0);
2123789Sahrens 	}
2124789Sahrens 
21251231Smarks 	if (attrzp)
21261231Smarks 		mutex_enter(&attrzp->z_lock);
21271231Smarks 
21281231Smarks 	if (mask & AT_UID) {
2129789Sahrens 		zp->z_phys->zp_uid = (uint64_t)vap->va_uid;
21301231Smarks 		if (attrzp) {
21311231Smarks 			attrzp->z_phys->zp_uid = (uint64_t)vap->va_uid;
21321231Smarks 		}
21331231Smarks 	}
21341231Smarks 
21351231Smarks 	if (mask & AT_GID) {
2136789Sahrens 		zp->z_phys->zp_gid = (uint64_t)vap->va_gid;
21371231Smarks 		if (attrzp)
21381231Smarks 			attrzp->z_phys->zp_gid = (uint64_t)vap->va_gid;
21391231Smarks 	}
21401231Smarks 
21411231Smarks 	if (attrzp)
21421231Smarks 		mutex_exit(&attrzp->z_lock);
2143789Sahrens 
2144789Sahrens 	if (mask & AT_ATIME)
2145789Sahrens 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
2146789Sahrens 
2147789Sahrens 	if (mask & AT_MTIME)
2148789Sahrens 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
2149789Sahrens 
21501878Smaybee 	if (mask & AT_SIZE)
2151789Sahrens 		zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
21521878Smaybee 	else if (mask != 0)
2153789Sahrens 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
2154789Sahrens 
21551878Smaybee 	if (mask != 0)
21562638Sperrin 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask);
2157789Sahrens 
2158789Sahrens 	mutex_exit(&zp->z_lock);
2159789Sahrens 
21601231Smarks 	if (attrzp)
21611231Smarks 		VN_RELE(ZTOV(attrzp));
21621231Smarks 
2163789Sahrens 	dmu_tx_commit(tx);
2164789Sahrens 
2165789Sahrens 	ZFS_EXIT(zfsvfs);
2166789Sahrens 	return (err);
2167789Sahrens }
2168789Sahrens 
21693271Smaybee typedef struct zfs_zlock {
21703271Smaybee 	krwlock_t	*zl_rwlock;	/* lock we acquired */
21713271Smaybee 	znode_t		*zl_znode;	/* znode we held */
21723271Smaybee 	struct zfs_zlock *zl_next;	/* next in list */
21733271Smaybee } zfs_zlock_t;
21743271Smaybee 
21753271Smaybee /*
21763271Smaybee  * Drop locks and release vnodes that were held by zfs_rename_lock().
21773271Smaybee  */
21783271Smaybee static void
21793271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp)
21803271Smaybee {
21813271Smaybee 	zfs_zlock_t *zl;
21823271Smaybee 
21833271Smaybee 	while ((zl = *zlpp) != NULL) {
21843271Smaybee 		if (zl->zl_znode != NULL)
21853271Smaybee 			VN_RELE(ZTOV(zl->zl_znode));
21863271Smaybee 		rw_exit(zl->zl_rwlock);
21873271Smaybee 		*zlpp = zl->zl_next;
21883271Smaybee 		kmem_free(zl, sizeof (*zl));
21893271Smaybee 	}
21903271Smaybee }
21913271Smaybee 
2192789Sahrens /*
2193789Sahrens  * Search back through the directory tree, using the ".." entries.
2194789Sahrens  * Lock each directory in the chain to prevent concurrent renames.
2195789Sahrens  * Fail any attempt to move a directory into one of its own descendants.
2196789Sahrens  * XXX - z_parent_lock can overlap with map or grow locks
2197789Sahrens  */
2198789Sahrens static int
2199789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
2200789Sahrens {
2201789Sahrens 	zfs_zlock_t	*zl;
22023638Sbillm 	znode_t		*zp = tdzp;
2203789Sahrens 	uint64_t	rootid = zp->z_zfsvfs->z_root;
2204789Sahrens 	uint64_t	*oidp = &zp->z_id;
2205789Sahrens 	krwlock_t	*rwlp = &szp->z_parent_lock;
2206789Sahrens 	krw_t		rw = RW_WRITER;
2207789Sahrens 
2208789Sahrens 	/*
2209789Sahrens 	 * First pass write-locks szp and compares to zp->z_id.
2210789Sahrens 	 * Later passes read-lock zp and compare to zp->z_parent.
2211789Sahrens 	 */
2212789Sahrens 	do {
22133271Smaybee 		if (!rw_tryenter(rwlp, rw)) {
22143271Smaybee 			/*
22153271Smaybee 			 * Another thread is renaming in this path.
22163271Smaybee 			 * Note that if we are a WRITER, we don't have any
22173271Smaybee 			 * parent_locks held yet.
22183271Smaybee 			 */
22193271Smaybee 			if (rw == RW_READER && zp->z_id > szp->z_id) {
22203271Smaybee 				/*
22213271Smaybee 				 * Drop our locks and restart
22223271Smaybee 				 */
22233271Smaybee 				zfs_rename_unlock(&zl);
22243271Smaybee 				*zlpp = NULL;
22253271Smaybee 				zp = tdzp;
22263271Smaybee 				oidp = &zp->z_id;
22273271Smaybee 				rwlp = &szp->z_parent_lock;
22283271Smaybee 				rw = RW_WRITER;
22293271Smaybee 				continue;
22303271Smaybee 			} else {
22313271Smaybee 				/*
22323271Smaybee 				 * Wait for other thread to drop its locks
22333271Smaybee 				 */
22343271Smaybee 				rw_enter(rwlp, rw);
22353271Smaybee 			}
22363271Smaybee 		}
22373271Smaybee 
2238789Sahrens 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
2239789Sahrens 		zl->zl_rwlock = rwlp;
2240789Sahrens 		zl->zl_znode = NULL;
2241789Sahrens 		zl->zl_next = *zlpp;
2242789Sahrens 		*zlpp = zl;
2243789Sahrens 
2244789Sahrens 		if (*oidp == szp->z_id)		/* We're a descendant of szp */
2245789Sahrens 			return (EINVAL);
2246789Sahrens 
2247789Sahrens 		if (*oidp == rootid)		/* We've hit the top */
2248789Sahrens 			return (0);
2249789Sahrens 
2250789Sahrens 		if (rw == RW_READER) {		/* i.e. not the first pass */
2251789Sahrens 			int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
2252789Sahrens 			if (error)
2253789Sahrens 				return (error);
2254789Sahrens 			zl->zl_znode = zp;
2255789Sahrens 		}
2256789Sahrens 		oidp = &zp->z_phys->zp_parent;
2257789Sahrens 		rwlp = &zp->z_parent_lock;
2258789Sahrens 		rw = RW_READER;
2259789Sahrens 
2260789Sahrens 	} while (zp->z_id != sdzp->z_id);
2261789Sahrens 
2262789Sahrens 	return (0);
2263789Sahrens }
2264789Sahrens 
2265789Sahrens /*
2266789Sahrens  * Move an entry from the provided source directory to the target
2267789Sahrens  * directory.  Change the entry name as indicated.
2268789Sahrens  *
2269789Sahrens  *	IN:	sdvp	- Source directory containing the "old entry".
2270789Sahrens  *		snm	- Old entry name.
2271789Sahrens  *		tdvp	- Target directory to contain the "new entry".
2272789Sahrens  *		tnm	- New entry name.
2273789Sahrens  *		cr	- credentials of caller.
2274789Sahrens  *
2275789Sahrens  *	RETURN:	0 if success
2276789Sahrens  *		error code if failure
2277789Sahrens  *
2278789Sahrens  * Timestamps:
2279789Sahrens  *	sdvp,tdvp - ctime|mtime updated
2280789Sahrens  */
2281789Sahrens static int
2282789Sahrens zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr)
2283789Sahrens {
2284789Sahrens 	znode_t		*tdzp, *szp, *tzp;
2285789Sahrens 	znode_t		*sdzp = VTOZ(sdvp);
2286789Sahrens 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
2287789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2288789Sahrens 	vnode_t		*realvp;
2289789Sahrens 	zfs_dirlock_t	*sdl, *tdl;
2290789Sahrens 	dmu_tx_t	*tx;
2291789Sahrens 	zfs_zlock_t	*zl;
2292789Sahrens 	int		cmp, serr, terr, error;
2293789Sahrens 
2294789Sahrens 	ZFS_ENTER(zfsvfs);
2295789Sahrens 
2296789Sahrens 	/*
2297789Sahrens 	 * Make sure we have the real vp for the target directory.
2298789Sahrens 	 */
2299789Sahrens 	if (VOP_REALVP(tdvp, &realvp) == 0)
2300789Sahrens 		tdvp = realvp;
2301789Sahrens 
2302789Sahrens 	if (tdvp->v_vfsp != sdvp->v_vfsp) {
2303789Sahrens 		ZFS_EXIT(zfsvfs);
2304789Sahrens 		return (EXDEV);
2305789Sahrens 	}
2306789Sahrens 
2307789Sahrens 	tdzp = VTOZ(tdvp);
2308789Sahrens top:
2309789Sahrens 	szp = NULL;
2310789Sahrens 	tzp = NULL;
2311789Sahrens 	zl = NULL;
2312789Sahrens 
2313789Sahrens 	/*
2314789Sahrens 	 * This is to prevent the creation of links into attribute space
2315789Sahrens 	 * by renaming a linked file into/outof an attribute directory.
2316789Sahrens 	 * See the comment in zfs_link() for why this is considered bad.
2317789Sahrens 	 */
2318789Sahrens 	if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
2319789Sahrens 	    (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
2320789Sahrens 		ZFS_EXIT(zfsvfs);
2321789Sahrens 		return (EINVAL);
2322789Sahrens 	}
2323789Sahrens 
2324789Sahrens 	/*
2325789Sahrens 	 * Lock source and target directory entries.  To prevent deadlock,
2326789Sahrens 	 * a lock ordering must be defined.  We lock the directory with
2327789Sahrens 	 * the smallest object id first, or if it's a tie, the one with
2328789Sahrens 	 * the lexically first name.
2329789Sahrens 	 */
2330789Sahrens 	if (sdzp->z_id < tdzp->z_id) {
2331789Sahrens 		cmp = -1;
2332789Sahrens 	} else if (sdzp->z_id > tdzp->z_id) {
2333789Sahrens 		cmp = 1;
2334789Sahrens 	} else {
2335789Sahrens 		cmp = strcmp(snm, tnm);
2336789Sahrens 		if (cmp == 0) {
2337789Sahrens 			/*
2338789Sahrens 			 * POSIX: "If the old argument and the new argument
2339789Sahrens 			 * both refer to links to the same existing file,
2340789Sahrens 			 * the rename() function shall return successfully
2341789Sahrens 			 * and perform no other action."
2342789Sahrens 			 */
2343789Sahrens 			ZFS_EXIT(zfsvfs);
2344789Sahrens 			return (0);
2345789Sahrens 		}
2346789Sahrens 	}
2347789Sahrens 	if (cmp < 0) {
2348789Sahrens 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
2349789Sahrens 		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
2350789Sahrens 	} else {
2351789Sahrens 		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
2352789Sahrens 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
2353789Sahrens 	}
2354789Sahrens 
2355789Sahrens 	if (serr) {
2356789Sahrens 		/*
2357789Sahrens 		 * Source entry invalid or not there.
2358789Sahrens 		 */
2359789Sahrens 		if (!terr) {
2360789Sahrens 			zfs_dirent_unlock(tdl);
2361789Sahrens 			if (tzp)
2362789Sahrens 				VN_RELE(ZTOV(tzp));
2363789Sahrens 		}
2364789Sahrens 		if (strcmp(snm, "..") == 0)
2365789Sahrens 			serr = EINVAL;
2366789Sahrens 		ZFS_EXIT(zfsvfs);
2367789Sahrens 		return (serr);
2368789Sahrens 	}
2369789Sahrens 	if (terr) {
2370789Sahrens 		zfs_dirent_unlock(sdl);
2371789Sahrens 		VN_RELE(ZTOV(szp));
2372789Sahrens 		if (strcmp(tnm, "..") == 0)
2373789Sahrens 			terr = EINVAL;
2374789Sahrens 		ZFS_EXIT(zfsvfs);
2375789Sahrens 		return (terr);
2376789Sahrens 	}
2377789Sahrens 
2378789Sahrens 	/*
2379789Sahrens 	 * Must have write access at the source to remove the old entry
2380789Sahrens 	 * and write access at the target to create the new entry.
2381789Sahrens 	 * Note that if target and source are the same, this can be
2382789Sahrens 	 * done in a single check.
2383789Sahrens 	 */
2384789Sahrens 
2385789Sahrens 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
2386789Sahrens 		goto out;
2387789Sahrens 
2388789Sahrens 	if (ZTOV(szp)->v_type == VDIR) {
2389789Sahrens 		/*
2390789Sahrens 		 * Check to make sure rename is valid.
2391789Sahrens 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
2392789Sahrens 		 */
2393789Sahrens 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
2394789Sahrens 			goto out;
2395789Sahrens 	}
2396789Sahrens 
2397789Sahrens 	/*
2398789Sahrens 	 * Does target exist?
2399789Sahrens 	 */
2400789Sahrens 	if (tzp) {
2401789Sahrens 		/*
2402789Sahrens 		 * Source and target must be the same type.
2403789Sahrens 		 */
2404789Sahrens 		if (ZTOV(szp)->v_type == VDIR) {
2405789Sahrens 			if (ZTOV(tzp)->v_type != VDIR) {
2406789Sahrens 				error = ENOTDIR;
2407789Sahrens 				goto out;
2408789Sahrens 			}
2409789Sahrens 		} else {
2410789Sahrens 			if (ZTOV(tzp)->v_type == VDIR) {
2411789Sahrens 				error = EISDIR;
2412789Sahrens 				goto out;
2413789Sahrens 			}
2414789Sahrens 		}
2415789Sahrens 		/*
2416789Sahrens 		 * POSIX dictates that when the source and target
2417789Sahrens 		 * entries refer to the same file object, rename
2418789Sahrens 		 * must do nothing and exit without error.
2419789Sahrens 		 */
2420789Sahrens 		if (szp->z_id == tzp->z_id) {
2421789Sahrens 			error = 0;
2422789Sahrens 			goto out;
2423789Sahrens 		}
2424789Sahrens 	}
2425789Sahrens 
2426789Sahrens 	vnevent_rename_src(ZTOV(szp));
2427789Sahrens 	if (tzp)
2428789Sahrens 		vnevent_rename_dest(ZTOV(tzp));
2429789Sahrens 
2430789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2431789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);	/* nlink changes */
2432789Sahrens 	dmu_tx_hold_bonus(tx, sdzp->z_id);	/* nlink changes */
24331544Seschrock 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
24341544Seschrock 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
24351544Seschrock 	if (sdzp != tdzp)
2436789Sahrens 		dmu_tx_hold_bonus(tx, tdzp->z_id);	/* nlink changes */
24371544Seschrock 	if (tzp)
24381544Seschrock 		dmu_tx_hold_bonus(tx, tzp->z_id);	/* parent changes */
24393461Sahrens 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2440789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2441789Sahrens 	if (error) {
2442789Sahrens 		if (zl != NULL)
2443789Sahrens 			zfs_rename_unlock(&zl);
2444789Sahrens 		zfs_dirent_unlock(sdl);
2445789Sahrens 		zfs_dirent_unlock(tdl);
2446789Sahrens 		VN_RELE(ZTOV(szp));
2447789Sahrens 		if (tzp)
2448789Sahrens 			VN_RELE(ZTOV(tzp));
2449789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
24502113Sahrens 			dmu_tx_wait(tx);
24512113Sahrens 			dmu_tx_abort(tx);
2452789Sahrens 			goto top;
2453789Sahrens 		}
24542113Sahrens 		dmu_tx_abort(tx);
2455789Sahrens 		ZFS_EXIT(zfsvfs);
2456789Sahrens 		return (error);
2457789Sahrens 	}
2458789Sahrens 
2459789Sahrens 	if (tzp)	/* Attempt to remove the existing target */
2460789Sahrens 		error = zfs_link_destroy(tdl, tzp, tx, 0, NULL);
2461789Sahrens 
2462789Sahrens 	if (error == 0) {
2463789Sahrens 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
2464789Sahrens 		if (error == 0) {
2465789Sahrens 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
2466789Sahrens 			ASSERT(error == 0);
24672638Sperrin 			zfs_log_rename(zilog, tx, TX_RENAME, sdzp,
24682638Sperrin 			    sdl->dl_name, tdzp, tdl->dl_name, szp);
2469789Sahrens 		}
2470789Sahrens 	}
2471789Sahrens 
2472789Sahrens 	dmu_tx_commit(tx);
2473789Sahrens out:
2474789Sahrens 	if (zl != NULL)
2475789Sahrens 		zfs_rename_unlock(&zl);
2476789Sahrens 
2477789Sahrens 	zfs_dirent_unlock(sdl);
2478789Sahrens 	zfs_dirent_unlock(tdl);
2479789Sahrens 
2480789Sahrens 	VN_RELE(ZTOV(szp));
2481789Sahrens 	if (tzp)
2482789Sahrens 		VN_RELE(ZTOV(tzp));
2483789Sahrens 
2484789Sahrens 	ZFS_EXIT(zfsvfs);
2485789Sahrens 	return (error);
2486789Sahrens }
2487789Sahrens 
2488789Sahrens /*
2489789Sahrens  * Insert the indicated symbolic reference entry into the directory.
2490789Sahrens  *
2491789Sahrens  *	IN:	dvp	- Directory to contain new symbolic link.
2492789Sahrens  *		link	- Name for new symlink entry.
2493789Sahrens  *		vap	- Attributes of new entry.
2494789Sahrens  *		target	- Target path of new symlink.
2495789Sahrens  *		cr	- credentials of caller.
2496789Sahrens  *
2497789Sahrens  *	RETURN:	0 if success
2498789Sahrens  *		error code if failure
2499789Sahrens  *
2500789Sahrens  * Timestamps:
2501789Sahrens  *	dvp - ctime|mtime updated
2502789Sahrens  */
2503789Sahrens static int
2504789Sahrens zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr)
2505789Sahrens {
2506789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
2507789Sahrens 	zfs_dirlock_t	*dl;
2508789Sahrens 	dmu_tx_t	*tx;
2509789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2510789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2511789Sahrens 	uint64_t	zoid;
2512789Sahrens 	int		len = strlen(link);
2513789Sahrens 	int		error;
2514789Sahrens 
2515789Sahrens 	ASSERT(vap->va_type == VLNK);
2516789Sahrens 
2517789Sahrens 	ZFS_ENTER(zfsvfs);
2518789Sahrens top:
2519789Sahrens 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
2520789Sahrens 		ZFS_EXIT(zfsvfs);
2521789Sahrens 		return (error);
2522789Sahrens 	}
2523789Sahrens 
2524789Sahrens 	if (len > MAXPATHLEN) {
2525789Sahrens 		ZFS_EXIT(zfsvfs);
2526789Sahrens 		return (ENAMETOOLONG);
2527789Sahrens 	}
2528789Sahrens 
2529789Sahrens 	/*
2530789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
2531789Sahrens 	 */
2532789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZNEW)) {
2533789Sahrens 		ZFS_EXIT(zfsvfs);
2534789Sahrens 		return (error);
2535789Sahrens 	}
2536789Sahrens 
2537789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2538789Sahrens 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
2539789Sahrens 	dmu_tx_hold_bonus(tx, dzp->z_id);
25401544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
2541789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
2542789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
2543789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2544789Sahrens 	if (error) {
2545789Sahrens 		zfs_dirent_unlock(dl);
2546789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
25472113Sahrens 			dmu_tx_wait(tx);
25482113Sahrens 			dmu_tx_abort(tx);
2549789Sahrens 			goto top;
2550789Sahrens 		}
25512113Sahrens 		dmu_tx_abort(tx);
2552789Sahrens 		ZFS_EXIT(zfsvfs);
2553789Sahrens 		return (error);
2554789Sahrens 	}
2555789Sahrens 
2556789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
2557789Sahrens 
2558789Sahrens 	/*
2559789Sahrens 	 * Create a new object for the symlink.
2560789Sahrens 	 * Put the link content into bonus buffer if it will fit;
2561789Sahrens 	 * otherwise, store it just like any other file data.
2562789Sahrens 	 */
2563789Sahrens 	zoid = 0;
2564789Sahrens 	if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
2565789Sahrens 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, len);
2566789Sahrens 		if (len != 0)
2567789Sahrens 			bcopy(link, zp->z_phys + 1, len);
2568789Sahrens 	} else {
2569789Sahrens 		dmu_buf_t *dbp;
25701669Sperrin 
2571789Sahrens 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
2572789Sahrens 
25731669Sperrin 		/*
25741669Sperrin 		 * Nothing can access the znode yet so no locking needed
25751669Sperrin 		 * for growing the znode's blocksize.
25761669Sperrin 		 */
25771669Sperrin 		zfs_grow_blocksize(zp, len, tx);
2578789Sahrens 
25791544Seschrock 		VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, zoid, 0, FTAG, &dbp));
2580789Sahrens 		dmu_buf_will_dirty(dbp, tx);
2581789Sahrens 
2582789Sahrens 		ASSERT3U(len, <=, dbp->db_size);
2583789Sahrens 		bcopy(link, dbp->db_data, len);
25841544Seschrock 		dmu_buf_rele(dbp, FTAG);
2585789Sahrens 	}
2586789Sahrens 	zp->z_phys->zp_size = len;
2587789Sahrens 
2588789Sahrens 	/*
2589789Sahrens 	 * Insert the new object into the directory.
2590789Sahrens 	 */
2591789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
2592789Sahrens out:
2593789Sahrens 	if (error == 0)
25942638Sperrin 		zfs_log_symlink(zilog, tx, TX_SYMLINK, dzp, zp, name, link);
2595789Sahrens 
2596789Sahrens 	dmu_tx_commit(tx);
2597789Sahrens 
2598789Sahrens 	zfs_dirent_unlock(dl);
2599789Sahrens 
2600789Sahrens 	VN_RELE(ZTOV(zp));
2601789Sahrens 
2602789Sahrens 	ZFS_EXIT(zfsvfs);
2603789Sahrens 	return (error);
2604789Sahrens }
2605789Sahrens 
2606789Sahrens /*
2607789Sahrens  * Return, in the buffer contained in the provided uio structure,
2608789Sahrens  * the symbolic path referred to by vp.
2609789Sahrens  *
2610789Sahrens  *	IN:	vp	- vnode of symbolic link.
2611789Sahrens  *		uoip	- structure to contain the link path.
2612789Sahrens  *		cr	- credentials of caller.
2613789Sahrens  *
2614789Sahrens  *	OUT:	uio	- structure to contain the link path.
2615789Sahrens  *
2616789Sahrens  *	RETURN:	0 if success
2617789Sahrens  *		error code if failure
2618789Sahrens  *
2619789Sahrens  * Timestamps:
2620789Sahrens  *	vp - atime updated
2621789Sahrens  */
2622789Sahrens /* ARGSUSED */
2623789Sahrens static int
2624789Sahrens zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr)
2625789Sahrens {
2626789Sahrens 	znode_t		*zp = VTOZ(vp);
2627789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2628789Sahrens 	size_t		bufsz;
2629789Sahrens 	int		error;
2630789Sahrens 
2631789Sahrens 	ZFS_ENTER(zfsvfs);
2632789Sahrens 
2633789Sahrens 	bufsz = (size_t)zp->z_phys->zp_size;
2634789Sahrens 	if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
2635789Sahrens 		error = uiomove(zp->z_phys + 1,
2636789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
2637789Sahrens 	} else {
26381544Seschrock 		dmu_buf_t *dbp;
26391544Seschrock 		error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
26401544Seschrock 		if (error) {
2641789Sahrens 			ZFS_EXIT(zfsvfs);
2642789Sahrens 			return (error);
2643789Sahrens 		}
2644789Sahrens 		error = uiomove(dbp->db_data,
2645789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
26461544Seschrock 		dmu_buf_rele(dbp, FTAG);
2647789Sahrens 	}
2648789Sahrens 
2649789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2650789Sahrens 	ZFS_EXIT(zfsvfs);
2651789Sahrens 	return (error);
2652789Sahrens }
2653789Sahrens 
2654789Sahrens /*
2655789Sahrens  * Insert a new entry into directory tdvp referencing svp.
2656789Sahrens  *
2657789Sahrens  *	IN:	tdvp	- Directory to contain new entry.
2658789Sahrens  *		svp	- vnode of new entry.
2659789Sahrens  *		name	- name of new entry.
2660789Sahrens  *		cr	- credentials of caller.
2661789Sahrens  *
2662789Sahrens  *	RETURN:	0 if success
2663789Sahrens  *		error code if failure
2664789Sahrens  *
2665789Sahrens  * Timestamps:
2666789Sahrens  *	tdvp - ctime|mtime updated
2667789Sahrens  *	 svp - ctime updated
2668789Sahrens  */
2669789Sahrens /* ARGSUSED */
2670789Sahrens static int
2671789Sahrens zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr)
2672789Sahrens {
2673789Sahrens 	znode_t		*dzp = VTOZ(tdvp);
2674789Sahrens 	znode_t		*tzp, *szp;
2675789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2676789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2677789Sahrens 	zfs_dirlock_t	*dl;
2678789Sahrens 	dmu_tx_t	*tx;
2679789Sahrens 	vnode_t		*realvp;
2680789Sahrens 	int		error;
2681789Sahrens 
2682789Sahrens 	ASSERT(tdvp->v_type == VDIR);
2683789Sahrens 
2684789Sahrens 	ZFS_ENTER(zfsvfs);
2685789Sahrens 
2686789Sahrens 	if (VOP_REALVP(svp, &realvp) == 0)
2687789Sahrens 		svp = realvp;
2688789Sahrens 
2689789Sahrens 	if (svp->v_vfsp != tdvp->v_vfsp) {
2690789Sahrens 		ZFS_EXIT(zfsvfs);
2691789Sahrens 		return (EXDEV);
2692789Sahrens 	}
2693789Sahrens 
2694789Sahrens 	szp = VTOZ(svp);
2695789Sahrens top:
2696789Sahrens 	/*
2697789Sahrens 	 * We do not support links between attributes and non-attributes
2698789Sahrens 	 * because of the potential security risk of creating links
2699789Sahrens 	 * into "normal" file space in order to circumvent restrictions
2700789Sahrens 	 * imposed in attribute space.
2701789Sahrens 	 */
2702789Sahrens 	if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
2703789Sahrens 	    (dzp->z_phys->zp_flags & ZFS_XATTR)) {
2704789Sahrens 		ZFS_EXIT(zfsvfs);
2705789Sahrens 		return (EINVAL);
2706789Sahrens 	}
2707789Sahrens 
2708789Sahrens 	/*
2709789Sahrens 	 * POSIX dictates that we return EPERM here.
2710789Sahrens 	 * Better choices include ENOTSUP or EISDIR.
2711789Sahrens 	 */
2712789Sahrens 	if (svp->v_type == VDIR) {
2713789Sahrens 		ZFS_EXIT(zfsvfs);
2714789Sahrens 		return (EPERM);
2715789Sahrens 	}
2716789Sahrens 
2717789Sahrens 	if ((uid_t)szp->z_phys->zp_uid != crgetuid(cr) &&
2718789Sahrens 	    secpolicy_basic_link(cr) != 0) {
2719789Sahrens 		ZFS_EXIT(zfsvfs);
2720789Sahrens 		return (EPERM);
2721789Sahrens 	}
2722789Sahrens 
2723789Sahrens 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
2724789Sahrens 		ZFS_EXIT(zfsvfs);
2725789Sahrens 		return (error);
2726789Sahrens 	}
2727789Sahrens 
2728789Sahrens 	/*
2729789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
2730789Sahrens 	 */
2731789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &tzp, ZNEW)) {
2732789Sahrens 		ZFS_EXIT(zfsvfs);
2733789Sahrens 		return (error);
2734789Sahrens 	}
2735789Sahrens 
2736789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2737789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);
27381544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
2739789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2740789Sahrens 	if (error) {
2741789Sahrens 		zfs_dirent_unlock(dl);
2742789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
27432113Sahrens 			dmu_tx_wait(tx);
27442113Sahrens 			dmu_tx_abort(tx);
2745789Sahrens 			goto top;
2746789Sahrens 		}
27472113Sahrens 		dmu_tx_abort(tx);
2748789Sahrens 		ZFS_EXIT(zfsvfs);
2749789Sahrens 		return (error);
2750789Sahrens 	}
2751789Sahrens 
2752789Sahrens 	error = zfs_link_create(dl, szp, tx, 0);
2753789Sahrens 
2754789Sahrens 	if (error == 0)
27552638Sperrin 		zfs_log_link(zilog, tx, TX_LINK, dzp, szp, name);
2756789Sahrens 
2757789Sahrens 	dmu_tx_commit(tx);
2758789Sahrens 
2759789Sahrens 	zfs_dirent_unlock(dl);
2760789Sahrens 
2761789Sahrens 	ZFS_EXIT(zfsvfs);
2762789Sahrens 	return (error);
2763789Sahrens }
2764789Sahrens 
2765789Sahrens /*
2766789Sahrens  * zfs_null_putapage() is used when the file system has been force
2767789Sahrens  * unmounted. It just drops the pages.
2768789Sahrens  */
2769789Sahrens /* ARGSUSED */
2770789Sahrens static int
2771789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
2772789Sahrens 		size_t *lenp, int flags, cred_t *cr)
2773789Sahrens {
2774789Sahrens 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
2775789Sahrens 	return (0);
2776789Sahrens }
2777789Sahrens 
27782688Smaybee /*
27792688Smaybee  * Push a page out to disk, klustering if possible.
27802688Smaybee  *
27812688Smaybee  *	IN:	vp	- file to push page to.
27822688Smaybee  *		pp	- page to push.
27832688Smaybee  *		flags	- additional flags.
27842688Smaybee  *		cr	- credentials of caller.
27852688Smaybee  *
27862688Smaybee  *	OUT:	offp	- start of range pushed.
27872688Smaybee  *		lenp	- len of range pushed.
27882688Smaybee  *
27892688Smaybee  *	RETURN:	0 if success
27902688Smaybee  *		error code if failure
27912688Smaybee  *
27922688Smaybee  * NOTE: callers must have locked the page to be pushed.  On
27932688Smaybee  * exit, the page (and all other pages in the kluster) must be
27942688Smaybee  * unlocked.
27952688Smaybee  */
2796789Sahrens /* ARGSUSED */
2797789Sahrens static int
2798789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
2799789Sahrens 		size_t *lenp, int flags, cred_t *cr)
2800789Sahrens {
2801789Sahrens 	znode_t		*zp = VTOZ(vp);
2802789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2803789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2804789Sahrens 	dmu_tx_t	*tx;
28051669Sperrin 	rl_t		*rl;
28062688Smaybee 	u_offset_t	off, koff;
28072688Smaybee 	size_t		len, klen;
2808789Sahrens 	int		err;
2809789Sahrens 
28102688Smaybee 	off = pp->p_offset;
28112688Smaybee 	len = PAGESIZE;
28122688Smaybee 	/*
28132688Smaybee 	 * If our blocksize is bigger than the page size, try to kluster
28142688Smaybee 	 * muiltiple pages so that we write a full block (thus avoiding
28152688Smaybee 	 * a read-modify-write).
28162688Smaybee 	 */
28172688Smaybee 	if (zp->z_blksz > PAGESIZE) {
28182688Smaybee 		uint64_t filesz = zp->z_phys->zp_size;
28192688Smaybee 
28202688Smaybee 		if (!ISP2(zp->z_blksz)) {
28212688Smaybee 			/* Only one block in the file. */
28222688Smaybee 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
28232688Smaybee 			koff = 0;
28242688Smaybee 		} else {
28252688Smaybee 			klen = zp->z_blksz;
28262688Smaybee 			koff = P2ALIGN(off, (u_offset_t)klen);
28272688Smaybee 		}
28282688Smaybee 		ASSERT(koff <= filesz);
28292688Smaybee 		if (koff + klen > filesz)
28302688Smaybee 			klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE);
28312688Smaybee 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
28322688Smaybee 	}
28332688Smaybee 	ASSERT3U(btop(len), ==, btopr(len));
2834789Sahrens top:
28352688Smaybee 	rl = zfs_range_lock(zp, off, len, RL_WRITER);
28361819Smaybee 	/*
28371819Smaybee 	 * Can't push pages past end-of-file.
28381819Smaybee 	 */
28391819Smaybee 	if (off >= zp->z_phys->zp_size) {
28402688Smaybee 		/* discard all pages */
28412688Smaybee 		flags |= B_INVAL;
28422688Smaybee 		err = 0;
28432688Smaybee 		goto out;
28442688Smaybee 	} else if (off + len > zp->z_phys->zp_size) {
28452688Smaybee 		int npages = btopr(zp->z_phys->zp_size - off);
28462688Smaybee 		page_t *trunc;
28472688Smaybee 
28482688Smaybee 		page_list_break(&pp, &trunc, npages);
28492688Smaybee 		/* discard pages past end of file */
28502688Smaybee 		if (trunc)
28512688Smaybee 			pvn_write_done(trunc, B_INVAL | flags);
28522688Smaybee 		len = zp->z_phys->zp_size - off;
28531819Smaybee 	}
2854789Sahrens 
2855789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2856789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, off, len);
2857789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
2858789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2859789Sahrens 	if (err != 0) {
2860789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
28612688Smaybee 			zfs_range_unlock(rl);
28622113Sahrens 			dmu_tx_wait(tx);
28632113Sahrens 			dmu_tx_abort(tx);
28642688Smaybee 			err = 0;
2865789Sahrens 			goto top;
2866789Sahrens 		}
28672113Sahrens 		dmu_tx_abort(tx);
2868789Sahrens 		goto out;
2869789Sahrens 	}
2870789Sahrens 
28712688Smaybee 	if (zp->z_blksz <= PAGESIZE) {
28722688Smaybee 		caddr_t va = ppmapin(pp, PROT_READ, (caddr_t)-1);
28732688Smaybee 		ASSERT3U(len, <=, PAGESIZE);
28742688Smaybee 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
28752688Smaybee 		ppmapout(va);
28762688Smaybee 	} else {
28772688Smaybee 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
28782688Smaybee 	}
28792688Smaybee 
28802688Smaybee 	if (err == 0) {
28812688Smaybee 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
28823638Sbillm 		zfs_log_write(zilog, tx, TX_WRITE, zp, off, len, 0);
28832688Smaybee 		dmu_tx_commit(tx);
28842688Smaybee 	}
28852688Smaybee 
28862688Smaybee out:
28872237Smaybee 	zfs_range_unlock(rl);
28882688Smaybee 	pvn_write_done(pp, (err ? B_ERROR : 0) | B_WRITE | flags);
2889789Sahrens 	if (offp)
2890789Sahrens 		*offp = off;
2891789Sahrens 	if (lenp)
2892789Sahrens 		*lenp = len;
2893789Sahrens 
2894789Sahrens 	return (err);
2895789Sahrens }
2896789Sahrens 
2897789Sahrens /*
2898789Sahrens  * Copy the portion of the file indicated from pages into the file.
2899789Sahrens  * The pages are stored in a page list attached to the files vnode.
2900789Sahrens  *
2901789Sahrens  *	IN:	vp	- vnode of file to push page data to.
2902789Sahrens  *		off	- position in file to put data.
2903789Sahrens  *		len	- amount of data to write.
2904789Sahrens  *		flags	- flags to control the operation.
2905789Sahrens  *		cr	- credentials of caller.
2906789Sahrens  *
2907789Sahrens  *	RETURN:	0 if success
2908789Sahrens  *		error code if failure
2909789Sahrens  *
2910789Sahrens  * Timestamps:
2911789Sahrens  *	vp - ctime|mtime updated
2912789Sahrens  */
2913789Sahrens static int
2914789Sahrens zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr)
2915789Sahrens {
2916789Sahrens 	znode_t		*zp = VTOZ(vp);
2917789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2918789Sahrens 	page_t		*pp;
2919789Sahrens 	size_t		io_len;
2920789Sahrens 	u_offset_t	io_off;
29211669Sperrin 	uint64_t	filesz;
2922789Sahrens 	int		error = 0;
2923789Sahrens 
2924789Sahrens 	ZFS_ENTER(zfsvfs);
2925789Sahrens 
2926789Sahrens 	ASSERT(zp->z_dbuf_held && zp->z_phys);
2927789Sahrens 
2928789Sahrens 	if (len == 0) {
2929789Sahrens 		/*
2930789Sahrens 		 * Search the entire vp list for pages >= off.
2931789Sahrens 		 */
2932789Sahrens 		error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage,
2933789Sahrens 		    flags, cr);
29341472Sperrin 		goto out;
2935789Sahrens 	}
2936789Sahrens 
29371669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
29381669Sperrin 	if (off > filesz) {
2939789Sahrens 		/* past end of file */
2940789Sahrens 		ZFS_EXIT(zfsvfs);
2941789Sahrens 		return (0);
2942789Sahrens 	}
2943789Sahrens 
29441669Sperrin 	len = MIN(len, filesz - off);
2945789Sahrens 
29461472Sperrin 	for (io_off = off; io_off < off + len; io_off += io_len) {
2947789Sahrens 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
29481669Sperrin 			pp = page_lookup(vp, io_off,
2949*4339Sperrin 			    (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
2950789Sahrens 		} else {
2951789Sahrens 			pp = page_lookup_nowait(vp, io_off,
2952*4339Sperrin 			    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
2953789Sahrens 		}
2954789Sahrens 
2955789Sahrens 		if (pp != NULL && pvn_getdirty(pp, flags)) {
2956789Sahrens 			int err;
2957789Sahrens 
2958789Sahrens 			/*
2959789Sahrens 			 * Found a dirty page to push
2960789Sahrens 			 */
29611669Sperrin 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
29621669Sperrin 			if (err)
2963789Sahrens 				error = err;
2964789Sahrens 		} else {
2965789Sahrens 			io_len = PAGESIZE;
2966789Sahrens 		}
2967789Sahrens 	}
29681472Sperrin out:
29692638Sperrin 	if ((flags & B_ASYNC) == 0)
29702638Sperrin 		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
2971789Sahrens 	ZFS_EXIT(zfsvfs);
2972789Sahrens 	return (error);
2973789Sahrens }
2974789Sahrens 
2975789Sahrens void
2976789Sahrens zfs_inactive(vnode_t *vp, cred_t *cr)
2977789Sahrens {
2978789Sahrens 	znode_t	*zp = VTOZ(vp);
2979789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2980789Sahrens 	int error;
2981789Sahrens 
2982789Sahrens 	rw_enter(&zfsvfs->z_um_lock, RW_READER);
2983789Sahrens 	if (zfsvfs->z_unmounted2) {
2984789Sahrens 		ASSERT(zp->z_dbuf_held == 0);
2985789Sahrens 
2986789Sahrens 		if (vn_has_cached_data(vp)) {
2987789Sahrens 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
2988789Sahrens 			    B_INVAL, cr);
2989789Sahrens 		}
2990789Sahrens 
29911544Seschrock 		mutex_enter(&zp->z_lock);
2992789Sahrens 		vp->v_count = 0; /* count arrives as 1 */
29931544Seschrock 		if (zp->z_dbuf == NULL) {
29941544Seschrock 			mutex_exit(&zp->z_lock);
29951544Seschrock 			zfs_znode_free(zp);
29961544Seschrock 		} else {
29971544Seschrock 			mutex_exit(&zp->z_lock);
29981544Seschrock 		}
2999789Sahrens 		rw_exit(&zfsvfs->z_um_lock);
3000789Sahrens 		VFS_RELE(zfsvfs->z_vfs);
3001789Sahrens 		return;
3002789Sahrens 	}
3003789Sahrens 
3004789Sahrens 	/*
3005789Sahrens 	 * Attempt to push any data in the page cache.  If this fails
3006789Sahrens 	 * we will get kicked out later in zfs_zinactive().
3007789Sahrens 	 */
30081298Sperrin 	if (vn_has_cached_data(vp)) {
30091298Sperrin 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
30101298Sperrin 		    cr);
30111298Sperrin 	}
3012789Sahrens 
30133461Sahrens 	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3014789Sahrens 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3015789Sahrens 
3016789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
3017789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
3018789Sahrens 		if (error) {
3019789Sahrens 			dmu_tx_abort(tx);
3020789Sahrens 		} else {
3021789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
3022789Sahrens 			mutex_enter(&zp->z_lock);
3023789Sahrens 			zp->z_atime_dirty = 0;
3024789Sahrens 			mutex_exit(&zp->z_lock);
3025789Sahrens 			dmu_tx_commit(tx);
3026789Sahrens 		}
3027789Sahrens 	}
3028789Sahrens 
3029789Sahrens 	zfs_zinactive(zp);
3030789Sahrens 	rw_exit(&zfsvfs->z_um_lock);
3031789Sahrens }
3032789Sahrens 
3033789Sahrens /*
3034789Sahrens  * Bounds-check the seek operation.
3035789Sahrens  *
3036789Sahrens  *	IN:	vp	- vnode seeking within
3037789Sahrens  *		ooff	- old file offset
3038789Sahrens  *		noffp	- pointer to new file offset
3039789Sahrens  *
3040789Sahrens  *	RETURN:	0 if success
3041789Sahrens  *		EINVAL if new offset invalid
3042789Sahrens  */
3043789Sahrens /* ARGSUSED */
3044789Sahrens static int
3045789Sahrens zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp)
3046789Sahrens {
3047789Sahrens 	if (vp->v_type == VDIR)
3048789Sahrens 		return (0);
3049789Sahrens 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
3050789Sahrens }
3051789Sahrens 
3052789Sahrens /*
3053789Sahrens  * Pre-filter the generic locking function to trap attempts to place
3054789Sahrens  * a mandatory lock on a memory mapped file.
3055789Sahrens  */
3056789Sahrens static int
3057789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
3058789Sahrens     flk_callback_t *flk_cbp, cred_t *cr)
3059789Sahrens {
3060789Sahrens 	znode_t *zp = VTOZ(vp);
3061789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3062789Sahrens 	int error;
3063789Sahrens 
3064789Sahrens 	ZFS_ENTER(zfsvfs);
3065789Sahrens 
3066789Sahrens 	/*
30671544Seschrock 	 * We are following the UFS semantics with respect to mapcnt
30681544Seschrock 	 * here: If we see that the file is mapped already, then we will
30691544Seschrock 	 * return an error, but we don't worry about races between this
30701544Seschrock 	 * function and zfs_map().
3071789Sahrens 	 */
30721544Seschrock 	if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) {
3073789Sahrens 		ZFS_EXIT(zfsvfs);
3074789Sahrens 		return (EAGAIN);
3075789Sahrens 	}
3076789Sahrens 	error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr);
3077789Sahrens 	ZFS_EXIT(zfsvfs);
3078789Sahrens 	return (error);
3079789Sahrens }
3080789Sahrens 
3081789Sahrens /*
3082789Sahrens  * If we can't find a page in the cache, we will create a new page
3083789Sahrens  * and fill it with file data.  For efficiency, we may try to fill
30841669Sperrin  * multiple pages at once (klustering).
3085789Sahrens  */
3086789Sahrens static int
3087789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
3088789Sahrens     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
3089789Sahrens {
3090789Sahrens 	znode_t *zp = VTOZ(vp);
3091789Sahrens 	page_t *pp, *cur_pp;
3092789Sahrens 	objset_t *os = zp->z_zfsvfs->z_os;
3093789Sahrens 	caddr_t va;
3094789Sahrens 	u_offset_t io_off, total;
3095789Sahrens 	uint64_t oid = zp->z_id;
3096789Sahrens 	size_t io_len;
30971669Sperrin 	uint64_t filesz;
3098789Sahrens 	int err;
3099789Sahrens 
3100789Sahrens 	/*
3101789Sahrens 	 * If we are only asking for a single page don't bother klustering.
3102789Sahrens 	 */
31031669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
31042688Smaybee 	if (off >= filesz)
31052688Smaybee 		return (EFAULT);
31062688Smaybee 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
3107789Sahrens 		io_off = off;
3108789Sahrens 		io_len = PAGESIZE;
3109789Sahrens 		pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr);
3110789Sahrens 	} else {
3111789Sahrens 		/*
3112789Sahrens 		 * Try to fill a kluster of pages (a blocks worth).
3113789Sahrens 		 */
3114789Sahrens 		size_t klen;
3115789Sahrens 		u_offset_t koff;
3116789Sahrens 
3117789Sahrens 		if (!ISP2(zp->z_blksz)) {
3118789Sahrens 			/* Only one block in the file. */
3119789Sahrens 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
3120789Sahrens 			koff = 0;
3121789Sahrens 		} else {
31223131Sgw25295 			/*
31233131Sgw25295 			 * It would be ideal to align our offset to the
31243131Sgw25295 			 * blocksize but doing so has resulted in some
31253131Sgw25295 			 * strange application crashes. For now, we
31263131Sgw25295 			 * leave the offset as is and only adjust the
31273131Sgw25295 			 * length if we are off the end of the file.
31283131Sgw25295 			 */
31293131Sgw25295 			koff = off;
3130789Sahrens 			klen = plsz;
3131789Sahrens 		}
31321819Smaybee 		ASSERT(koff <= filesz);
31331819Smaybee 		if (koff + klen > filesz)
31341819Smaybee 			klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff;
31352688Smaybee 		ASSERT3U(off, >=, koff);
31362688Smaybee 		ASSERT3U(off, <, koff + klen);
3137789Sahrens 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
3138*4339Sperrin 		    &io_len, koff, klen, 0);
3139789Sahrens 	}
3140789Sahrens 	if (pp == NULL) {
3141789Sahrens 		/*
3142789Sahrens 		 * Some other thread entered the page before us.
3143789Sahrens 		 * Return to zfs_getpage to retry the lookup.
3144789Sahrens 		 */
3145789Sahrens 		*pl = NULL;
3146789Sahrens 		return (0);
3147789Sahrens 	}
3148789Sahrens 
3149789Sahrens 	/*
3150789Sahrens 	 * Fill the pages in the kluster.
3151789Sahrens 	 */
3152789Sahrens 	cur_pp = pp;
3153789Sahrens 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
31542688Smaybee 		ASSERT3U(io_off, ==, cur_pp->p_offset);
3155789Sahrens 		va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1);
31561544Seschrock 		err = dmu_read(os, oid, io_off, PAGESIZE, va);
3157789Sahrens 		ppmapout(va);
3158789Sahrens 		if (err) {
3159789Sahrens 			/* On error, toss the entire kluster */
3160789Sahrens 			pvn_read_done(pp, B_ERROR);
3161789Sahrens 			return (err);
3162789Sahrens 		}
3163789Sahrens 		cur_pp = cur_pp->p_next;
3164789Sahrens 	}
3165789Sahrens out:
3166789Sahrens 	/*
3167789Sahrens 	 * Fill in the page list array from the kluster.  If
3168789Sahrens 	 * there are too many pages in the kluster, return
3169789Sahrens 	 * as many pages as possible starting from the desired
3170789Sahrens 	 * offset `off'.
3171789Sahrens 	 * NOTE: the page list will always be null terminated.
3172789Sahrens 	 */
3173789Sahrens 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
3174789Sahrens 
3175789Sahrens 	return (0);
3176789Sahrens }
3177789Sahrens 
3178789Sahrens /*
3179789Sahrens  * Return pointers to the pages for the file region [off, off + len]
3180789Sahrens  * in the pl array.  If plsz is greater than len, this function may
3181789Sahrens  * also return page pointers from before or after the specified
3182789Sahrens  * region (i.e. some region [off', off' + plsz]).  These additional
3183789Sahrens  * pages are only returned if they are already in the cache, or were
3184789Sahrens  * created as part of a klustered read.
3185789Sahrens  *
3186789Sahrens  *	IN:	vp	- vnode of file to get data from.
3187789Sahrens  *		off	- position in file to get data from.
3188789Sahrens  *		len	- amount of data to retrieve.
3189789Sahrens  *		plsz	- length of provided page list.
3190789Sahrens  *		seg	- segment to obtain pages for.
3191789Sahrens  *		addr	- virtual address of fault.
3192789Sahrens  *		rw	- mode of created pages.
3193789Sahrens  *		cr	- credentials of caller.
3194789Sahrens  *
3195789Sahrens  *	OUT:	protp	- protection mode of created pages.
3196789Sahrens  *		pl	- list of pages created.
3197789Sahrens  *
3198789Sahrens  *	RETURN:	0 if success
3199789Sahrens  *		error code if failure
3200789Sahrens  *
3201789Sahrens  * Timestamps:
3202789Sahrens  *	vp - atime updated
3203789Sahrens  */
3204789Sahrens /* ARGSUSED */
3205789Sahrens static int
3206789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
3207789Sahrens 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
3208789Sahrens 	enum seg_rw rw, cred_t *cr)
3209789Sahrens {
3210789Sahrens 	znode_t		*zp = VTOZ(vp);
3211789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3212789Sahrens 	page_t		*pp, **pl0 = pl;
32132752Sperrin 	int		need_unlock = 0, err = 0;
32142752Sperrin 	offset_t	orig_off;
3215789Sahrens 
3216789Sahrens 	ZFS_ENTER(zfsvfs);
3217789Sahrens 
3218789Sahrens 	if (protp)
3219789Sahrens 		*protp = PROT_ALL;
3220789Sahrens 
3221789Sahrens 	ASSERT(zp->z_dbuf_held && zp->z_phys);
3222789Sahrens 
3223789Sahrens 	/* no faultahead (for now) */
3224789Sahrens 	if (pl == NULL) {
3225789Sahrens 		ZFS_EXIT(zfsvfs);
3226789Sahrens 		return (0);
3227789Sahrens 	}
3228789Sahrens 
3229789Sahrens 	/* can't fault past EOF */
3230789Sahrens 	if (off >= zp->z_phys->zp_size) {
3231789Sahrens 		ZFS_EXIT(zfsvfs);
3232789Sahrens 		return (EFAULT);
3233789Sahrens 	}
32342752Sperrin 	orig_off = off;
3235789Sahrens 
3236789Sahrens 	/*
3237789Sahrens 	 * If we already own the lock, then we must be page faulting
3238789Sahrens 	 * in the middle of a write to this file (i.e., we are writing
3239789Sahrens 	 * to this file using data from a mapped region of the file).
3240789Sahrens 	 */
32412752Sperrin 	if (rw_owner(&zp->z_map_lock) != curthread) {
3242789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
3243789Sahrens 		need_unlock = TRUE;
3244789Sahrens 	}
3245789Sahrens 
3246789Sahrens 	/*
3247789Sahrens 	 * Loop through the requested range [off, off + len] looking
3248789Sahrens 	 * for pages.  If we don't find a page, we will need to create
3249789Sahrens 	 * a new page and fill it with data from the file.
3250789Sahrens 	 */
3251789Sahrens 	while (len > 0) {
3252789Sahrens 		if (plsz < PAGESIZE)
3253789Sahrens 			break;
3254789Sahrens 		if (pp = page_lookup(vp, off, SE_SHARED)) {
3255789Sahrens 			*pl++ = pp;
3256789Sahrens 			off += PAGESIZE;
3257789Sahrens 			addr += PAGESIZE;
3258789Sahrens 			len -= PAGESIZE;
3259789Sahrens 			plsz -= PAGESIZE;
3260789Sahrens 		} else {
3261789Sahrens 			err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw);
32622752Sperrin 			if (err)
32632752Sperrin 				goto out;
3264789Sahrens 			/*
3265789Sahrens 			 * klustering may have changed our region
3266789Sahrens 			 * to be block aligned.
3267789Sahrens 			 */
3268789Sahrens 			if (((pp = *pl) != 0) && (off != pp->p_offset)) {
3269789Sahrens 				int delta = off - pp->p_offset;
3270789Sahrens 				len += delta;
3271789Sahrens 				off -= delta;
3272789Sahrens 				addr -= delta;
3273789Sahrens 			}
3274789Sahrens 			while (*pl) {
3275789Sahrens 				pl++;
3276789Sahrens 				off += PAGESIZE;
3277789Sahrens 				addr += PAGESIZE;
3278789Sahrens 				plsz -= PAGESIZE;
3279789Sahrens 				if (len > PAGESIZE)
3280789Sahrens 					len -= PAGESIZE;
3281789Sahrens 				else
3282789Sahrens 					len = 0;
3283789Sahrens 			}
3284789Sahrens 		}
3285789Sahrens 	}
3286789Sahrens 
3287789Sahrens 	/*
3288789Sahrens 	 * Fill out the page array with any pages already in the cache.
3289789Sahrens 	 */
3290789Sahrens 	while (plsz > 0) {
3291789Sahrens 		pp = page_lookup_nowait(vp, off, SE_SHARED);
3292789Sahrens 		if (pp == NULL)
3293789Sahrens 			break;
3294789Sahrens 		*pl++ = pp;
3295789Sahrens 		off += PAGESIZE;
3296789Sahrens 		plsz -= PAGESIZE;
3297789Sahrens 	}
3298789Sahrens 
3299789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3300789Sahrens out:
33012752Sperrin 	/*
33022752Sperrin 	 * We can't grab the range lock for the page as reader which would
33032752Sperrin 	 * stop truncation as this leads to deadlock. So we need to recheck
33042752Sperrin 	 * the file size.
33052752Sperrin 	 */
33062752Sperrin 	if (orig_off >= zp->z_phys->zp_size)
33072752Sperrin 		err = EFAULT;
33082752Sperrin 	if (err) {
33092752Sperrin 		/*
33102752Sperrin 		 * Release any pages we have previously locked.
33112752Sperrin 		 */
33122752Sperrin 		while (pl > pl0)
33132752Sperrin 			page_unlock(*--pl);
33142752Sperrin 	}
33152752Sperrin 
3316789Sahrens 	*pl = NULL;
3317789Sahrens 
3318789Sahrens 	if (need_unlock)
3319789Sahrens 		rw_exit(&zp->z_map_lock);
3320789Sahrens 
3321789Sahrens 	ZFS_EXIT(zfsvfs);
3322789Sahrens 	return (err);
3323789Sahrens }
3324789Sahrens 
33251544Seschrock /*
33261544Seschrock  * Request a memory map for a section of a file.  This code interacts
33271544Seschrock  * with common code and the VM system as follows:
33281544Seschrock  *
33291544Seschrock  *	common code calls mmap(), which ends up in smmap_common()
33301544Seschrock  *
33311544Seschrock  *	this calls VOP_MAP(), which takes you into (say) zfs
33321544Seschrock  *
33331544Seschrock  *	zfs_map() calls as_map(), passing segvn_create() as the callback
33341544Seschrock  *
33351544Seschrock  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
33361544Seschrock  *
33371544Seschrock  *	zfs_addmap() updates z_mapcnt
33381544Seschrock  */
3339789Sahrens static int
3340789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
3341789Sahrens     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
3342789Sahrens {
3343789Sahrens 	znode_t *zp = VTOZ(vp);
3344789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3345789Sahrens 	segvn_crargs_t	vn_a;
3346789Sahrens 	int		error;
3347789Sahrens 
3348789Sahrens 	ZFS_ENTER(zfsvfs);
3349789Sahrens 
3350789Sahrens 	if (vp->v_flag & VNOMAP) {
3351789Sahrens 		ZFS_EXIT(zfsvfs);
3352789Sahrens 		return (ENOSYS);
3353789Sahrens 	}
3354789Sahrens 
3355789Sahrens 	if (off < 0 || len > MAXOFFSET_T - off) {
3356789Sahrens 		ZFS_EXIT(zfsvfs);
3357789Sahrens 		return (ENXIO);
3358789Sahrens 	}
3359789Sahrens 
3360789Sahrens 	if (vp->v_type != VREG) {
3361789Sahrens 		ZFS_EXIT(zfsvfs);
3362789Sahrens 		return (ENODEV);
3363789Sahrens 	}
3364789Sahrens 
3365789Sahrens 	/*
3366789Sahrens 	 * If file is locked, disallow mapping.
3367789Sahrens 	 */
33681544Seschrock 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) {
33691544Seschrock 		ZFS_EXIT(zfsvfs);
33701544Seschrock 		return (EAGAIN);
3371789Sahrens 	}
3372789Sahrens 
3373789Sahrens 	as_rangelock(as);
3374789Sahrens 	if ((flags & MAP_FIXED) == 0) {
3375789Sahrens 		map_addr(addrp, len, off, 1, flags);
3376789Sahrens 		if (*addrp == NULL) {
3377789Sahrens 			as_rangeunlock(as);
3378789Sahrens 			ZFS_EXIT(zfsvfs);
3379789Sahrens 			return (ENOMEM);
3380789Sahrens 		}
3381789Sahrens 	} else {
3382789Sahrens 		/*
3383789Sahrens 		 * User specified address - blow away any previous mappings
3384789Sahrens 		 */
3385789Sahrens 		(void) as_unmap(as, *addrp, len);
3386789Sahrens 	}
3387789Sahrens 
3388789Sahrens 	vn_a.vp = vp;
3389789Sahrens 	vn_a.offset = (u_offset_t)off;
3390789Sahrens 	vn_a.type = flags & MAP_TYPE;
3391789Sahrens 	vn_a.prot = prot;
3392789Sahrens 	vn_a.maxprot = maxprot;
3393789Sahrens 	vn_a.cred = cr;
3394789Sahrens 	vn_a.amp = NULL;
3395789Sahrens 	vn_a.flags = flags & ~MAP_TYPE;
33961417Skchow 	vn_a.szc = 0;
33971417Skchow 	vn_a.lgrp_mem_policy_flags = 0;
3398789Sahrens 
3399789Sahrens 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
3400789Sahrens 
3401789Sahrens 	as_rangeunlock(as);
3402789Sahrens 	ZFS_EXIT(zfsvfs);
3403789Sahrens 	return (error);
3404789Sahrens }
3405789Sahrens 
3406789Sahrens /* ARGSUSED */
3407789Sahrens static int
3408789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
3409789Sahrens     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
3410789Sahrens {
34111544Seschrock 	uint64_t pages = btopr(len);
34121544Seschrock 
34131544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
3414789Sahrens 	return (0);
3415789Sahrens }
3416789Sahrens 
34171773Seschrock /*
34181773Seschrock  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
34191773Seschrock  * more accurate mtime for the associated file.  Since we don't have a way of
34201773Seschrock  * detecting when the data was actually modified, we have to resort to
34211773Seschrock  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
34221773Seschrock  * last page is pushed.  The problem occurs when the msync() call is omitted,
34231773Seschrock  * which by far the most common case:
34241773Seschrock  *
34251773Seschrock  * 	open()
34261773Seschrock  * 	mmap()
34271773Seschrock  * 	<modify memory>
34281773Seschrock  * 	munmap()
34291773Seschrock  * 	close()
34301773Seschrock  * 	<time lapse>
34311773Seschrock  * 	putpage() via fsflush
34321773Seschrock  *
34331773Seschrock  * If we wait until fsflush to come along, we can have a modification time that
34341773Seschrock  * is some arbitrary point in the future.  In order to prevent this in the
34351773Seschrock  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
34361773Seschrock  * torn down.
34371773Seschrock  */
3438789Sahrens /* ARGSUSED */
3439789Sahrens static int
3440789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
3441789Sahrens     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr)
3442789Sahrens {
34431544Seschrock 	uint64_t pages = btopr(len);
34441544Seschrock 
34451544Seschrock 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
34461544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
34471773Seschrock 
34481773Seschrock 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
34491773Seschrock 	    vn_has_cached_data(vp))
34501773Seschrock 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr);
34511773Seschrock 
3452789Sahrens 	return (0);
3453789Sahrens }
3454789Sahrens 
3455789Sahrens /*
3456789Sahrens  * Free or allocate space in a file.  Currently, this function only
3457789Sahrens  * supports the `F_FREESP' command.  However, this command is somewhat
3458789Sahrens  * misnamed, as its functionality includes the ability to allocate as
3459789Sahrens  * well as free space.
3460789Sahrens  *
3461789Sahrens  *	IN:	vp	- vnode of file to free data in.
3462789Sahrens  *		cmd	- action to take (only F_FREESP supported).
3463789Sahrens  *		bfp	- section of file to free/alloc.
3464789Sahrens  *		flag	- current file open mode flags.
3465789Sahrens  *		offset	- current file offset.
3466789Sahrens  *		cr	- credentials of caller [UNUSED].
3467789Sahrens  *
3468789Sahrens  *	RETURN:	0 if success
3469789Sahrens  *		error code if failure
3470789Sahrens  *
3471789Sahrens  * Timestamps:
3472789Sahrens  *	vp - ctime|mtime updated
3473789Sahrens  */
3474789Sahrens /* ARGSUSED */
3475789Sahrens static int
3476789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
3477789Sahrens     offset_t offset, cred_t *cr, caller_context_t *ct)
3478789Sahrens {
3479789Sahrens 	znode_t		*zp = VTOZ(vp);
3480789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3481789Sahrens 	uint64_t	off, len;
3482789Sahrens 	int		error;
3483789Sahrens 
3484789Sahrens 	ZFS_ENTER(zfsvfs);
3485789Sahrens 
3486789Sahrens top:
3487789Sahrens 	if (cmd != F_FREESP) {
3488789Sahrens 		ZFS_EXIT(zfsvfs);
3489789Sahrens 		return (EINVAL);
3490789Sahrens 	}
3491789Sahrens 
3492789Sahrens 	if (error = convoff(vp, bfp, 0, offset)) {
3493789Sahrens 		ZFS_EXIT(zfsvfs);
3494789Sahrens 		return (error);
3495789Sahrens 	}
3496789Sahrens 
3497789Sahrens 	if (bfp->l_len < 0) {
3498789Sahrens 		ZFS_EXIT(zfsvfs);
3499789Sahrens 		return (EINVAL);
3500789Sahrens 	}
3501789Sahrens 
3502789Sahrens 	off = bfp->l_start;
35031669Sperrin 	len = bfp->l_len; /* 0 means from off to end of file */
35041878Smaybee 
35051878Smaybee 	do {
35061878Smaybee 		error = zfs_freesp(zp, off, len, flag, TRUE);
35072113Sahrens 		/* NB: we already did dmu_tx_wait() if necessary */
35081878Smaybee 	} while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
3509789Sahrens 
3510789Sahrens 	ZFS_EXIT(zfsvfs);
3511789Sahrens 	return (error);
3512789Sahrens }
3513789Sahrens 
3514789Sahrens static int
3515789Sahrens zfs_fid(vnode_t *vp, fid_t *fidp)
3516789Sahrens {
3517789Sahrens 	znode_t		*zp = VTOZ(vp);
3518789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3519789Sahrens 	uint32_t	gen = (uint32_t)zp->z_phys->zp_gen;
3520789Sahrens 	uint64_t	object = zp->z_id;
3521789Sahrens 	zfid_short_t	*zfid;
3522789Sahrens 	int		size, i;
3523789Sahrens 
3524789Sahrens 	ZFS_ENTER(zfsvfs);
3525789Sahrens 
3526789Sahrens 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
3527789Sahrens 	if (fidp->fid_len < size) {
3528789Sahrens 		fidp->fid_len = size;
35291512Sek110237 		ZFS_EXIT(zfsvfs);
3530789Sahrens 		return (ENOSPC);
3531789Sahrens 	}
3532789Sahrens 
3533789Sahrens 	zfid = (zfid_short_t *)fidp;
3534789Sahrens 
3535789Sahrens 	zfid->zf_len = size;
3536789Sahrens 
3537789Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
3538789Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
3539789Sahrens 
3540789Sahrens 	/* Must have a non-zero generation number to distinguish from .zfs */
3541789Sahrens 	if (gen == 0)
3542789Sahrens 		gen = 1;
3543789Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
3544789Sahrens 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
3545789Sahrens 
3546789Sahrens 	if (size == LONG_FID_LEN) {
3547789Sahrens 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
3548789Sahrens 		zfid_long_t	*zlfid;
3549789Sahrens 
3550789Sahrens 		zlfid = (zfid_long_t *)fidp;
3551789Sahrens 
3552789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
3553789Sahrens 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
3554789Sahrens 
3555789Sahrens 		/* XXX - this should be the generation number for the objset */
3556789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
3557789Sahrens 			zlfid->zf_setgen[i] = 0;
3558789Sahrens 	}
3559789Sahrens 
3560789Sahrens 	ZFS_EXIT(zfsvfs);
3561789Sahrens 	return (0);
3562789Sahrens }
3563789Sahrens 
3564789Sahrens static int
3565789Sahrens zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr)
3566789Sahrens {
3567789Sahrens 	znode_t		*zp, *xzp;
3568789Sahrens 	zfsvfs_t	*zfsvfs;
3569789Sahrens 	zfs_dirlock_t	*dl;
3570789Sahrens 	int		error;
3571789Sahrens 
3572789Sahrens 	switch (cmd) {
3573789Sahrens 	case _PC_LINK_MAX:
3574789Sahrens 		*valp = ULONG_MAX;
3575789Sahrens 		return (0);
3576789Sahrens 
3577789Sahrens 	case _PC_FILESIZEBITS:
3578789Sahrens 		*valp = 64;
3579789Sahrens 		return (0);
3580789Sahrens 
3581789Sahrens 	case _PC_XATTR_EXISTS:
3582789Sahrens 		zp = VTOZ(vp);
3583789Sahrens 		zfsvfs = zp->z_zfsvfs;
3584789Sahrens 		ZFS_ENTER(zfsvfs);
3585789Sahrens 		*valp = 0;
3586789Sahrens 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
3587789Sahrens 		    ZXATTR | ZEXISTS | ZSHARED);
3588789Sahrens 		if (error == 0) {
3589789Sahrens 			zfs_dirent_unlock(dl);
3590789Sahrens 			if (!zfs_dirempty(xzp))
3591789Sahrens 				*valp = 1;
3592789Sahrens 			VN_RELE(ZTOV(xzp));
3593789Sahrens 		} else if (error == ENOENT) {
3594789Sahrens 			/*
3595789Sahrens 			 * If there aren't extended attributes, it's the
3596789Sahrens 			 * same as having zero of them.
3597789Sahrens 			 */
3598789Sahrens 			error = 0;
3599789Sahrens 		}
3600789Sahrens 		ZFS_EXIT(zfsvfs);
3601789Sahrens 		return (error);
3602789Sahrens 
3603789Sahrens 	case _PC_ACL_ENABLED:
3604789Sahrens 		*valp = _ACL_ACE_ENABLED;
3605789Sahrens 		return (0);
3606789Sahrens 
3607789Sahrens 	case _PC_MIN_HOLE_SIZE:
3608789Sahrens 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
3609789Sahrens 		return (0);
3610789Sahrens 
3611789Sahrens 	default:
3612789Sahrens 		return (fs_pathconf(vp, cmd, valp, cr));
3613789Sahrens 	}
3614789Sahrens }
3615789Sahrens 
3616789Sahrens /*ARGSUSED*/
3617789Sahrens static int
3618789Sahrens zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
3619789Sahrens {
3620789Sahrens 	znode_t *zp = VTOZ(vp);
3621789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3622789Sahrens 	int error;
3623789Sahrens 
3624789Sahrens 	ZFS_ENTER(zfsvfs);
3625789Sahrens 	error = zfs_getacl(zp, vsecp, cr);
3626789Sahrens 	ZFS_EXIT(zfsvfs);
3627789Sahrens 
3628789Sahrens 	return (error);
3629789Sahrens }
3630789Sahrens 
3631789Sahrens /*ARGSUSED*/
3632789Sahrens static int
3633789Sahrens zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
3634789Sahrens {
3635789Sahrens 	znode_t *zp = VTOZ(vp);
3636789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3637789Sahrens 	int error;
3638789Sahrens 
3639789Sahrens 	ZFS_ENTER(zfsvfs);
3640789Sahrens 	error = zfs_setacl(zp, vsecp, cr);
3641789Sahrens 	ZFS_EXIT(zfsvfs);
3642789Sahrens 	return (error);
3643789Sahrens }
3644789Sahrens 
3645789Sahrens /*
3646789Sahrens  * Predeclare these here so that the compiler assumes that
3647789Sahrens  * this is an "old style" function declaration that does
3648789Sahrens  * not include arguments => we won't get type mismatch errors
3649789Sahrens  * in the initializations that follow.
3650789Sahrens  */
3651789Sahrens static int zfs_inval();
3652789Sahrens static int zfs_isdir();
3653789Sahrens 
3654789Sahrens static int
3655789Sahrens zfs_inval()
3656789Sahrens {
3657789Sahrens 	return (EINVAL);
3658789Sahrens }
3659789Sahrens 
3660789Sahrens static int
3661789Sahrens zfs_isdir()
3662789Sahrens {
3663789Sahrens 	return (EISDIR);
3664789Sahrens }
3665789Sahrens /*
3666789Sahrens  * Directory vnode operations template
3667789Sahrens  */
3668789Sahrens vnodeops_t *zfs_dvnodeops;
3669789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = {
36703898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
36713898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
36723898Srsb 	VOPNAME_READ,		{ .error = zfs_isdir },
36733898Srsb 	VOPNAME_WRITE,		{ .error = zfs_isdir },
36743898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
36753898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
36763898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
36773898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
36783898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
36793898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
36803898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
36813898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
36823898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
36833898Srsb 	VOPNAME_MKDIR,		{ .vop_mkdir = zfs_mkdir },
36843898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
36853898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
36863898Srsb 	VOPNAME_SYMLINK,	{ .vop_symlink = zfs_symlink },
36873898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
36883898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
36893898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
36903898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
36913898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
36923898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
36933898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
36943898Srsb 	NULL,			NULL
3695789Sahrens };
3696789Sahrens 
3697789Sahrens /*
3698789Sahrens  * Regular file vnode operations template
3699789Sahrens  */
3700789Sahrens vnodeops_t *zfs_fvnodeops;
3701789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = {
37023898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
37033898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
37043898Srsb 	VOPNAME_READ,		{ .vop_read = zfs_read },
37053898Srsb 	VOPNAME_WRITE,		{ .vop_write = zfs_write },
37063898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
37073898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
37083898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
37093898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
37103898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
37113898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
37123898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
37133898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
37143898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
37153898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
37163898Srsb 	VOPNAME_FRLOCK,		{ .vop_frlock = zfs_frlock },
37173898Srsb 	VOPNAME_SPACE,		{ .vop_space = zfs_space },
37183898Srsb 	VOPNAME_GETPAGE,	{ .vop_getpage = zfs_getpage },
37193898Srsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = zfs_putpage },
37203898Srsb 	VOPNAME_MAP,		{ .vop_map = zfs_map },
37213898Srsb 	VOPNAME_ADDMAP,		{ .vop_addmap = zfs_addmap },
37223898Srsb 	VOPNAME_DELMAP,		{ .vop_delmap = zfs_delmap },
37233898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
37243898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
37253898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
37263898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
37273898Srsb 	NULL,			NULL
3728789Sahrens };
3729789Sahrens 
3730789Sahrens /*
3731789Sahrens  * Symbolic link vnode operations template
3732789Sahrens  */
3733789Sahrens vnodeops_t *zfs_symvnodeops;
3734789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = {
37353898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
37363898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
37373898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
37383898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
37393898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = zfs_readlink },
37403898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
37413898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
37423898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
37433898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
37443898Srsb 	NULL,			NULL
3745789Sahrens };
3746789Sahrens 
3747789Sahrens /*
3748789Sahrens  * Extended attribute directory vnode operations template
3749789Sahrens  *	This template is identical to the directory vnodes
3750789Sahrens  *	operation template except for restricted operations:
3751789Sahrens  *		VOP_MKDIR()
3752789Sahrens  *		VOP_SYMLINK()
3753789Sahrens  * Note that there are other restrictions embedded in:
3754789Sahrens  *	zfs_create()	- restrict type to VREG
3755789Sahrens  *	zfs_link()	- no links into/out of attribute space
3756789Sahrens  *	zfs_rename()	- no moves into/out of attribute space
3757789Sahrens  */
3758789Sahrens vnodeops_t *zfs_xdvnodeops;
3759789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = {
37603898Srsb 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
37613898Srsb 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
37623898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
37633898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
37643898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
37653898Srsb 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
37663898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
37673898Srsb 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
37683898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
37693898Srsb 	VOPNAME_LINK,		{ .vop_link = zfs_link },
37703898Srsb 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
37713898Srsb 	VOPNAME_MKDIR,		{ .error = zfs_inval },
37723898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
37733898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
37743898Srsb 	VOPNAME_SYMLINK,	{ .error = zfs_inval },
37753898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
37763898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
37773898Srsb 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
37783898Srsb 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
37793898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
37803898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
37813898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
37823898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
37833898Srsb 	NULL,			NULL
3784789Sahrens };
3785789Sahrens 
3786789Sahrens /*
3787789Sahrens  * Error vnode operations template
3788789Sahrens  */
3789789Sahrens vnodeops_t *zfs_evnodeops;
3790789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = {
37913898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
37923898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
37933898Srsb 	NULL,			NULL
3794789Sahrens };
3795