xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 2365:2745d773b6bd)
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 /*
221231Smarks  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens #include <sys/types.h>
29789Sahrens #include <sys/param.h>
30789Sahrens #include <sys/time.h>
31789Sahrens #include <sys/systm.h>
32789Sahrens #include <sys/sysmacros.h>
33789Sahrens #include <sys/resource.h>
34789Sahrens #include <sys/vfs.h>
35789Sahrens #include <sys/vnode.h>
36789Sahrens #include <sys/file.h>
37789Sahrens #include <sys/stat.h>
38789Sahrens #include <sys/kmem.h>
39789Sahrens #include <sys/taskq.h>
40789Sahrens #include <sys/uio.h>
41789Sahrens #include <sys/vmsystm.h>
42789Sahrens #include <sys/atomic.h>
43789Sahrens #include <vm/seg_vn.h>
44789Sahrens #include <vm/pvn.h>
45789Sahrens #include <vm/as.h>
46789Sahrens #include <sys/mman.h>
47789Sahrens #include <sys/pathname.h>
48789Sahrens #include <sys/cmn_err.h>
49789Sahrens #include <sys/errno.h>
50789Sahrens #include <sys/unistd.h>
51789Sahrens #include <sys/zfs_vfsops.h>
52789Sahrens #include <sys/zfs_dir.h>
53789Sahrens #include <sys/zfs_acl.h>
54789Sahrens #include <sys/zfs_ioctl.h>
55789Sahrens #include <sys/fs/zfs.h>
56789Sahrens #include <sys/dmu.h>
57789Sahrens #include <sys/spa.h>
58789Sahrens #include <sys/txg.h>
59789Sahrens #include <sys/dbuf.h>
60789Sahrens #include <sys/zap.h>
61789Sahrens #include <sys/dirent.h>
62789Sahrens #include <sys/policy.h>
63789Sahrens #include <sys/sunddi.h>
64789Sahrens #include <sys/filio.h>
65789Sahrens #include "fs/fs_subr.h"
66789Sahrens #include <sys/zfs_ctldir.h>
671484Sek110237 #include <sys/dnlc.h>
681669Sperrin #include <sys/zfs_rlock.h>
69789Sahrens 
70789Sahrens /*
71789Sahrens  * Programming rules.
72789Sahrens  *
73789Sahrens  * Each vnode op performs some logical unit of work.  To do this, the ZPL must
74789Sahrens  * properly lock its in-core state, create a DMU transaction, do the work,
75789Sahrens  * record this work in the intent log (ZIL), commit the DMU transaction,
76789Sahrens  * and wait the the intent log to commit if it's is a synchronous operation.
77789Sahrens  * Morover, the vnode ops must work in both normal and log replay context.
78789Sahrens  * The ordering of events is important to avoid deadlocks and references
79789Sahrens  * to freed memory.  The example below illustrates the following Big Rules:
80789Sahrens  *
81789Sahrens  *  (1) A check must be made in each zfs thread for a mounted file system.
82789Sahrens  *	This is done avoiding races using ZFS_ENTER(zfsvfs).
83789Sahrens  *	A ZFS_EXIT(zfsvfs) is needed before all returns.
84789Sahrens  *
85789Sahrens  *  (2)	VN_RELE() should always be the last thing except for zil_commit()
86789Sahrens  *	and ZFS_EXIT(). This is for 3 reasons:
87789Sahrens  *	First, if it's the last reference, the vnode/znode
88789Sahrens  *	can be freed, so the zp may point to freed memory.  Second, the last
89789Sahrens  *	reference will call zfs_zinactive(), which may induce a lot of work --
901669Sperrin  *	pushing cached pages (which acquires range locks) and syncing out
91789Sahrens  *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
92789Sahrens  *	which could deadlock the system if you were already holding one.
93789Sahrens  *
941757Sperrin  *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
951757Sperrin  *	as they can span dmu_tx_assign() calls.
961757Sperrin  *
971757Sperrin  *  (4)	Always pass zfsvfs->z_assign as the second argument to dmu_tx_assign().
98789Sahrens  *	In normal operation, this will be TXG_NOWAIT.  During ZIL replay,
99789Sahrens  *	it will be a specific txg.  Either way, dmu_tx_assign() never blocks.
100789Sahrens  *	This is critical because we don't want to block while holding locks.
101789Sahrens  *	Note, in particular, that if a lock is sometimes acquired before
102789Sahrens  *	the tx assigns, and sometimes after (e.g. z_lock), then failing to
103789Sahrens  *	use a non-blocking assign can deadlock the system.  The scenario:
104789Sahrens  *
105789Sahrens  *	Thread A has grabbed a lock before calling dmu_tx_assign().
106789Sahrens  *	Thread B is in an already-assigned tx, and blocks for this lock.
107789Sahrens  *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
108789Sahrens  *	forever, because the previous txg can't quiesce until B's tx commits.
109789Sahrens  *
110789Sahrens  *	If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
1112113Sahrens  *	then drop all locks, call dmu_tx_wait(), and try again.
112789Sahrens  *
1131757Sperrin  *  (5)	If the operation succeeded, generate the intent log entry for it
114789Sahrens  *	before dropping locks.  This ensures that the ordering of events
115789Sahrens  *	in the intent log matches the order in which they actually occurred.
116789Sahrens  *
1171757Sperrin  *  (6)	At the end of each vnode op, the DMU tx must always commit,
118789Sahrens  *	regardless of whether there were any errors.
119789Sahrens  *
1201757Sperrin  *  (7)	After dropping all locks, invoke zil_commit(zilog, seq, ioflag)
121789Sahrens  *	to ensure that synchronous semantics are provided when necessary.
122789Sahrens  *
123789Sahrens  * In general, this is how things should be ordered in each vnode op:
124789Sahrens  *
125789Sahrens  *	ZFS_ENTER(zfsvfs);		// exit if unmounted
126789Sahrens  * top:
127789Sahrens  *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
128789Sahrens  *	rw_enter(...);			// grab any other locks you need
129789Sahrens  *	tx = dmu_tx_create(...);	// get DMU tx
130789Sahrens  *	dmu_tx_hold_*();		// hold each object you might modify
131789Sahrens  *	error = dmu_tx_assign(tx, zfsvfs->z_assign);	// try to assign
132789Sahrens  *	if (error) {
133789Sahrens  *		rw_exit(...);		// drop locks
134789Sahrens  *		zfs_dirent_unlock(dl);	// unlock directory entry
135789Sahrens  *		VN_RELE(...);		// release held vnodes
136789Sahrens  *		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1372113Sahrens  *			dmu_tx_wait(tx);
1382113Sahrens  *			dmu_tx_abort(tx);
139789Sahrens  *			goto top;
140789Sahrens  *		}
1412113Sahrens  *		dmu_tx_abort(tx);	// abort DMU tx
142789Sahrens  *		ZFS_EXIT(zfsvfs);	// finished in zfs
143789Sahrens  *		return (error);		// really out of space
144789Sahrens  *	}
145789Sahrens  *	error = do_real_work();		// do whatever this VOP does
146789Sahrens  *	if (error == 0)
147789Sahrens  *		seq = zfs_log_*(...);	// on success, make ZIL entry
148789Sahrens  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
149789Sahrens  *	rw_exit(...);			// drop locks
150789Sahrens  *	zfs_dirent_unlock(dl);		// unlock directory entry
151789Sahrens  *	VN_RELE(...);			// release held vnodes
152789Sahrens  *	zil_commit(zilog, seq, ioflag);	// synchronous when necessary
153789Sahrens  *	ZFS_EXIT(zfsvfs);		// finished in zfs
154789Sahrens  *	return (error);			// done, report error
155789Sahrens  */
156789Sahrens 
157789Sahrens /* ARGSUSED */
158789Sahrens static int
159789Sahrens zfs_open(vnode_t **vpp, int flag, cred_t *cr)
160789Sahrens {
161789Sahrens 	return (0);
162789Sahrens }
163789Sahrens 
164789Sahrens /* ARGSUSED */
165789Sahrens static int
166789Sahrens zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr)
167789Sahrens {
168789Sahrens 	/*
169789Sahrens 	 * Clean up any locks held by this process on the vp.
170789Sahrens 	 */
171789Sahrens 	cleanlocks(vp, ddi_get_pid(), 0);
172789Sahrens 	cleanshares(vp, ddi_get_pid());
173789Sahrens 
174789Sahrens 	return (0);
175789Sahrens }
176789Sahrens 
177789Sahrens /*
178789Sahrens  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
179789Sahrens  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
180789Sahrens  */
181789Sahrens static int
182789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off)
183789Sahrens {
184789Sahrens 	znode_t	*zp = VTOZ(vp);
185789Sahrens 	uint64_t noff = (uint64_t)*off; /* new offset */
186789Sahrens 	uint64_t file_sz;
187789Sahrens 	int error;
188789Sahrens 	boolean_t hole;
189789Sahrens 
190789Sahrens 	file_sz = zp->z_phys->zp_size;
191789Sahrens 	if (noff >= file_sz)  {
192789Sahrens 		return (ENXIO);
193789Sahrens 	}
194789Sahrens 
195789Sahrens 	if (cmd == _FIO_SEEK_HOLE)
196789Sahrens 		hole = B_TRUE;
197789Sahrens 	else
198789Sahrens 		hole = B_FALSE;
199789Sahrens 
200789Sahrens 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
201789Sahrens 
202789Sahrens 	/* end of file? */
203789Sahrens 	if ((error == ESRCH) || (noff > file_sz)) {
204789Sahrens 		/*
205789Sahrens 		 * Handle the virtual hole at the end of file.
206789Sahrens 		 */
207789Sahrens 		if (hole) {
208789Sahrens 			*off = file_sz;
209789Sahrens 			return (0);
210789Sahrens 		}
211789Sahrens 		return (ENXIO);
212789Sahrens 	}
213789Sahrens 
214789Sahrens 	if (noff < *off)
215789Sahrens 		return (error);
216789Sahrens 	*off = noff;
217789Sahrens 	return (error);
218789Sahrens }
219789Sahrens 
220789Sahrens /* ARGSUSED */
221789Sahrens static int
222789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
223789Sahrens     int *rvalp)
224789Sahrens {
225789Sahrens 	offset_t off;
226789Sahrens 	int error;
227789Sahrens 	zfsvfs_t *zfsvfs;
228789Sahrens 
229789Sahrens 	switch (com) {
230789Sahrens 	    case _FIOFFS:
231789Sahrens 		return (zfs_sync(vp->v_vfsp, 0, cred));
232789Sahrens 
2331544Seschrock 		/*
2341544Seschrock 		 * The following two ioctls are used by bfu.  Faking out,
2351544Seschrock 		 * necessary to avoid bfu errors.
2361544Seschrock 		 */
2371544Seschrock 	    case _FIOGDIO:
2381544Seschrock 	    case _FIOSDIO:
2391544Seschrock 		return (0);
2401544Seschrock 
241789Sahrens 	    case _FIO_SEEK_DATA:
242789Sahrens 	    case _FIO_SEEK_HOLE:
243789Sahrens 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
244789Sahrens 			return (EFAULT);
245789Sahrens 
246789Sahrens 		zfsvfs = VTOZ(vp)->z_zfsvfs;
247789Sahrens 		ZFS_ENTER(zfsvfs);
248789Sahrens 
249789Sahrens 		/* offset parameter is in/out */
250789Sahrens 		error = zfs_holey(vp, com, &off);
251789Sahrens 		ZFS_EXIT(zfsvfs);
252789Sahrens 		if (error)
253789Sahrens 			return (error);
254789Sahrens 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
255789Sahrens 			return (EFAULT);
256789Sahrens 		return (0);
257789Sahrens 	}
258789Sahrens 	return (ENOTTY);
259789Sahrens }
260789Sahrens 
261789Sahrens /*
262789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
263789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
264789Sahrens  *
265789Sahrens  * On Write:	If we find a memory mapped page, we write to *both*
266789Sahrens  *		the page and the dmu buffer.
267789Sahrens  *
268789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
269789Sahrens  *	the file is memory mapped.
270789Sahrens  */
271789Sahrens static int
272789Sahrens mappedwrite(vnode_t *vp, uint64_t woff, int nbytes, uio_t *uio, dmu_tx_t *tx)
273789Sahrens {
274789Sahrens 	znode_t	*zp = VTOZ(vp);
275789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
276789Sahrens 	int64_t	start, off;
277789Sahrens 	int len = nbytes;
278789Sahrens 	int error = 0;
279789Sahrens 
280789Sahrens 	start = uio->uio_loffset;
281789Sahrens 	off = start & PAGEOFFSET;
282789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
283789Sahrens 		page_t *pp;
284789Sahrens 		uint64_t bytes = MIN(PAGESIZE - off, len);
285789Sahrens 
286789Sahrens 		/*
287789Sahrens 		 * We don't want a new page to "appear" in the middle of
288789Sahrens 		 * the file update (because it may not get the write
289789Sahrens 		 * update data), so we grab a lock to block
290789Sahrens 		 * zfs_getpage().
291789Sahrens 		 */
292789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
293789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
294789Sahrens 			caddr_t va;
295789Sahrens 
296789Sahrens 			rw_exit(&zp->z_map_lock);
297789Sahrens 			va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L);
298789Sahrens 			error = uiomove(va+off, bytes, UIO_WRITE, uio);
299789Sahrens 			if (error == 0) {
300789Sahrens 				dmu_write(zfsvfs->z_os, zp->z_id,
301789Sahrens 				    woff, bytes, va+off, tx);
302789Sahrens 			}
303789Sahrens 			ppmapout(va);
304789Sahrens 			page_unlock(pp);
305789Sahrens 		} else {
306789Sahrens 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
307789Sahrens 			    woff, bytes, uio, tx);
308789Sahrens 			rw_exit(&zp->z_map_lock);
309789Sahrens 		}
310789Sahrens 		len -= bytes;
311789Sahrens 		woff += bytes;
312789Sahrens 		off = 0;
313789Sahrens 		if (error)
314789Sahrens 			break;
315789Sahrens 	}
316789Sahrens 	return (error);
317789Sahrens }
318789Sahrens 
319789Sahrens /*
320789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
321789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
322789Sahrens  *
323789Sahrens  * On Read:	We "read" preferentially from memory mapped pages,
324789Sahrens  *		else we default from the dmu buffer.
325789Sahrens  *
326789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
327789Sahrens  *	the file is memory mapped.
328789Sahrens  */
329789Sahrens static int
330789Sahrens mappedread(vnode_t *vp, char *addr, int nbytes, uio_t *uio)
331789Sahrens {
332789Sahrens 	int64_t	start, off, bytes;
333789Sahrens 	int len = nbytes;
334789Sahrens 	int error = 0;
335789Sahrens 
336789Sahrens 	start = uio->uio_loffset;
337789Sahrens 	off = start & PAGEOFFSET;
338789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
339789Sahrens 		page_t *pp;
340789Sahrens 
341789Sahrens 		bytes = MIN(PAGESIZE - off, len);
342789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
343789Sahrens 			caddr_t va;
344789Sahrens 
345789Sahrens 			va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L);
346789Sahrens 			error = uiomove(va + off, bytes, UIO_READ, uio);
347789Sahrens 			ppmapout(va);
348789Sahrens 			page_unlock(pp);
349789Sahrens 		} else {
350789Sahrens 			/* XXX use dmu_read here? */
351789Sahrens 			error = uiomove(addr, bytes, UIO_READ, uio);
352789Sahrens 		}
353789Sahrens 		len -= bytes;
354789Sahrens 		addr += bytes;
355789Sahrens 		off = 0;
356789Sahrens 		if (error)
357789Sahrens 			break;
358789Sahrens 	}
359789Sahrens 	return (error);
360789Sahrens }
361789Sahrens 
362789Sahrens uint_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
363789Sahrens 
364789Sahrens /*
365789Sahrens  * Read bytes from specified file into supplied buffer.
366789Sahrens  *
367789Sahrens  *	IN:	vp	- vnode of file to be read from.
368789Sahrens  *		uio	- structure supplying read location, range info,
369789Sahrens  *			  and return buffer.
370789Sahrens  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
371789Sahrens  *		cr	- credentials of caller.
372789Sahrens  *
373789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
374789Sahrens  *
375789Sahrens  *	RETURN:	0 if success
376789Sahrens  *		error code if failure
377789Sahrens  *
378789Sahrens  * Side Effects:
379789Sahrens  *	vp - atime updated if byte count > 0
380789Sahrens  */
381789Sahrens /* ARGSUSED */
382789Sahrens static int
383789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
384789Sahrens {
385789Sahrens 	znode_t		*zp = VTOZ(vp);
386789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
387789Sahrens 	uint64_t	delta;
388789Sahrens 	ssize_t		n, size, cnt, ndone;
389789Sahrens 	int		error, i, numbufs;
390789Sahrens 	dmu_buf_t	*dbp, **dbpp;
3911669Sperrin 	rl_t		*rl;
392789Sahrens 
393789Sahrens 	ZFS_ENTER(zfsvfs);
394789Sahrens 
395789Sahrens 	/*
396789Sahrens 	 * Validate file offset
397789Sahrens 	 */
398789Sahrens 	if (uio->uio_loffset < (offset_t)0) {
399789Sahrens 		ZFS_EXIT(zfsvfs);
400789Sahrens 		return (EINVAL);
401789Sahrens 	}
402789Sahrens 
403789Sahrens 	/*
404789Sahrens 	 * Fasttrack empty reads
405789Sahrens 	 */
406789Sahrens 	if (uio->uio_resid == 0) {
407789Sahrens 		ZFS_EXIT(zfsvfs);
408789Sahrens 		return (0);
409789Sahrens 	}
410789Sahrens 
411789Sahrens 	/*
4121669Sperrin 	 * Check for mandatory locks
413789Sahrens 	 */
414789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
415789Sahrens 		if (error = chklock(vp, FREAD,
416789Sahrens 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
417789Sahrens 			ZFS_EXIT(zfsvfs);
418789Sahrens 			return (error);
419789Sahrens 		}
420789Sahrens 	}
421789Sahrens 
422789Sahrens 	/*
423789Sahrens 	 * If we're in FRSYNC mode, sync out this znode before reading it.
424789Sahrens 	 */
425789Sahrens 	zil_commit(zfsvfs->z_log, zp->z_last_itx, ioflag & FRSYNC);
426789Sahrens 
427789Sahrens 	/*
4281669Sperrin 	 * Lock the range against changes.
429789Sahrens 	 */
4301669Sperrin 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
4311669Sperrin 
432789Sahrens 	/*
433789Sahrens 	 * If we are reading past end-of-file we can skip
434789Sahrens 	 * to the end; but we might still need to set atime.
435789Sahrens 	 */
436789Sahrens 	if (uio->uio_loffset >= zp->z_phys->zp_size) {
437789Sahrens 		cnt = 0;
438789Sahrens 		error = 0;
439789Sahrens 		goto out;
440789Sahrens 	}
441789Sahrens 
442789Sahrens 	cnt = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset);
443789Sahrens 
444789Sahrens 	for (ndone = 0; ndone < cnt; ndone += zfs_read_chunk_size) {
445789Sahrens 		ASSERT(uio->uio_loffset < zp->z_phys->zp_size);
446789Sahrens 		n = MIN(zfs_read_chunk_size,
447789Sahrens 		    zp->z_phys->zp_size - uio->uio_loffset);
448789Sahrens 		n = MIN(n, cnt);
4491544Seschrock 		error = dmu_buf_hold_array(zfsvfs->z_os, zp->z_id,
4501544Seschrock 		    uio->uio_loffset, n, TRUE, FTAG, &numbufs, &dbpp);
4511544Seschrock 		if (error)
452789Sahrens 			goto out;
453789Sahrens 		/*
454789Sahrens 		 * Compute the adjustment to align the dmu buffers
455789Sahrens 		 * with the uio buffer.
456789Sahrens 		 */
457789Sahrens 		delta = uio->uio_loffset - dbpp[0]->db_offset;
458789Sahrens 
459789Sahrens 		for (i = 0; i < numbufs; i++) {
460789Sahrens 			if (n < 0)
461789Sahrens 				break;
462789Sahrens 			dbp = dbpp[i];
463789Sahrens 			size = dbp->db_size - delta;
464789Sahrens 			/*
465789Sahrens 			 * XXX -- this is correct, but may be suboptimal.
466789Sahrens 			 * If the pages are all clean, we don't need to
467789Sahrens 			 * go through mappedread().  Maybe the VMODSORT
468789Sahrens 			 * stuff can help us here.
469789Sahrens 			 */
470789Sahrens 			if (vn_has_cached_data(vp)) {
471789Sahrens 				error = mappedread(vp, (caddr_t)dbp->db_data +
472789Sahrens 				    delta, (n < size ? n : size), uio);
473789Sahrens 			} else {
474789Sahrens 				error = uiomove((caddr_t)dbp->db_data + delta,
475789Sahrens 					(n < size ? n : size), UIO_READ, uio);
476789Sahrens 			}
477789Sahrens 			if (error) {
4781544Seschrock 				dmu_buf_rele_array(dbpp, numbufs, FTAG);
479789Sahrens 				goto out;
480789Sahrens 			}
481789Sahrens 			n -= dbp->db_size;
482789Sahrens 			if (delta) {
483789Sahrens 				n += delta;
484789Sahrens 				delta = 0;
485789Sahrens 			}
486789Sahrens 		}
4871544Seschrock 		dmu_buf_rele_array(dbpp, numbufs, FTAG);
488789Sahrens 	}
489789Sahrens out:
4902237Smaybee 	zfs_range_unlock(rl);
491789Sahrens 
492789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
493789Sahrens 	ZFS_EXIT(zfsvfs);
494789Sahrens 	return (error);
495789Sahrens }
496789Sahrens 
497789Sahrens /*
498789Sahrens  * Fault in the pages of the first n bytes specified by the uio structure.
499789Sahrens  * 1 byte in each page is touched and the uio struct is unmodified.
500789Sahrens  * Any error will exit this routine as this is only a best
501789Sahrens  * attempt to get the pages resident. This is a copy of ufs_trans_touch().
502789Sahrens  */
503789Sahrens static void
504789Sahrens zfs_prefault_write(ssize_t n, struct uio *uio)
505789Sahrens {
506789Sahrens 	struct iovec *iov;
507789Sahrens 	ulong_t cnt, incr;
508789Sahrens 	caddr_t p;
509789Sahrens 	uint8_t tmp;
510789Sahrens 
511789Sahrens 	iov = uio->uio_iov;
512789Sahrens 
513789Sahrens 	while (n) {
514789Sahrens 		cnt = MIN(iov->iov_len, n);
515789Sahrens 		if (cnt == 0) {
516789Sahrens 			/* empty iov entry */
517789Sahrens 			iov++;
518789Sahrens 			continue;
519789Sahrens 		}
520789Sahrens 		n -= cnt;
521789Sahrens 		/*
522789Sahrens 		 * touch each page in this segment.
523789Sahrens 		 */
524789Sahrens 		p = iov->iov_base;
525789Sahrens 		while (cnt) {
526789Sahrens 			switch (uio->uio_segflg) {
527789Sahrens 			case UIO_USERSPACE:
528789Sahrens 			case UIO_USERISPACE:
529789Sahrens 				if (fuword8(p, &tmp))
530789Sahrens 					return;
531789Sahrens 				break;
532789Sahrens 			case UIO_SYSSPACE:
533789Sahrens 				if (kcopy(p, &tmp, 1))
534789Sahrens 					return;
535789Sahrens 				break;
536789Sahrens 			}
537789Sahrens 			incr = MIN(cnt, PAGESIZE);
538789Sahrens 			p += incr;
539789Sahrens 			cnt -= incr;
540789Sahrens 		}
541789Sahrens 		/*
542789Sahrens 		 * touch the last byte in case it straddles a page.
543789Sahrens 		 */
544789Sahrens 		p--;
545789Sahrens 		switch (uio->uio_segflg) {
546789Sahrens 		case UIO_USERSPACE:
547789Sahrens 		case UIO_USERISPACE:
548789Sahrens 			if (fuword8(p, &tmp))
549789Sahrens 				return;
550789Sahrens 			break;
551789Sahrens 		case UIO_SYSSPACE:
552789Sahrens 			if (kcopy(p, &tmp, 1))
553789Sahrens 				return;
554789Sahrens 			break;
555789Sahrens 		}
556789Sahrens 		iov++;
557789Sahrens 	}
558789Sahrens }
559789Sahrens 
560789Sahrens /*
561789Sahrens  * Write the bytes to a file.
562789Sahrens  *
563789Sahrens  *	IN:	vp	- vnode of file to be written to.
564789Sahrens  *		uio	- structure supplying write location, range info,
565789Sahrens  *			  and data buffer.
566789Sahrens  *		ioflag	- FAPPEND flag set if in append mode.
567789Sahrens  *		cr	- credentials of caller.
568789Sahrens  *
569789Sahrens  *	OUT:	uio	- updated offset and range.
570789Sahrens  *
571789Sahrens  *	RETURN:	0 if success
572789Sahrens  *		error code if failure
573789Sahrens  *
574789Sahrens  * Timestamps:
575789Sahrens  *	vp - ctime|mtime updated if byte count > 0
576789Sahrens  */
577789Sahrens /* ARGSUSED */
578789Sahrens static int
579789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
580789Sahrens {
581789Sahrens 	znode_t		*zp = VTOZ(vp);
582789Sahrens 	rlim64_t	limit = uio->uio_llimit;
583789Sahrens 	ssize_t		start_resid = uio->uio_resid;
584789Sahrens 	ssize_t		tx_bytes;
585789Sahrens 	uint64_t	end_size;
586789Sahrens 	dmu_tx_t	*tx;
587789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
588789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
589789Sahrens 	uint64_t	seq = 0;
590789Sahrens 	offset_t	woff;
591789Sahrens 	ssize_t		n, nbytes;
5921669Sperrin 	rl_t		*rl;
593789Sahrens 	int		max_blksz = zfsvfs->z_max_blksz;
5941669Sperrin 	int		error;
595789Sahrens 
596789Sahrens 	/*
597789Sahrens 	 * Fasttrack empty write
598789Sahrens 	 */
5991669Sperrin 	n = start_resid;
600789Sahrens 	if (n == 0)
601789Sahrens 		return (0);
602789Sahrens 
6031669Sperrin 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
6041669Sperrin 		limit = MAXOFFSET_T;
6051669Sperrin 
606789Sahrens 	ZFS_ENTER(zfsvfs);
607789Sahrens 
608789Sahrens 	/*
6092237Smaybee 	 * Pre-fault the pages to ensure slow (eg NFS) pages
6101669Sperrin 	 * don't hold up txg.
611789Sahrens 	 */
6122237Smaybee 	zfs_prefault_write(n, uio);
613789Sahrens 
614789Sahrens 	/*
615789Sahrens 	 * If in append mode, set the io offset pointer to eof.
616789Sahrens 	 */
6171669Sperrin 	if (ioflag & FAPPEND) {
6181669Sperrin 		/*
6191669Sperrin 		 * Range lock for a file append:
6201669Sperrin 		 * The value for the start of range will be determined by
6211669Sperrin 		 * zfs_range_lock() (to guarantee append semantics).
6221669Sperrin 		 * If this write will cause the block size to increase,
6231669Sperrin 		 * zfs_range_lock() will lock the entire file, so we must
6241669Sperrin 		 * later reduce the range after we grow the block size.
6251669Sperrin 		 */
6261669Sperrin 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
6271669Sperrin 		if (rl->r_len == UINT64_MAX) {
6281669Sperrin 			/* overlocked, zp_size can't change */
6291669Sperrin 			woff = uio->uio_loffset = zp->z_phys->zp_size;
6301669Sperrin 		} else {
6311669Sperrin 			woff = uio->uio_loffset = rl->r_off;
6321669Sperrin 		}
633789Sahrens 	} else {
634789Sahrens 		woff = uio->uio_loffset;
635789Sahrens 		/*
636789Sahrens 		 * Validate file offset
637789Sahrens 		 */
638789Sahrens 		if (woff < 0) {
639789Sahrens 			ZFS_EXIT(zfsvfs);
640789Sahrens 			return (EINVAL);
641789Sahrens 		}
642789Sahrens 
643789Sahrens 		/*
6441669Sperrin 		 * If we need to grow the block size then zfs_range_lock()
6451669Sperrin 		 * will lock a wider range than we request here.
6461669Sperrin 		 * Later after growing the block size we reduce the range.
647789Sahrens 		 */
6481669Sperrin 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
649789Sahrens 	}
650789Sahrens 
651789Sahrens 	if (woff >= limit) {
652789Sahrens 		error = EFBIG;
653789Sahrens 		goto no_tx_done;
654789Sahrens 	}
655789Sahrens 
656789Sahrens 	if ((woff + n) > limit || woff > (limit - n))
657789Sahrens 		n = limit - woff;
658789Sahrens 
659789Sahrens 	/*
6601669Sperrin 	 * Check for mandatory locks
661789Sahrens 	 */
662789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) &&
663789Sahrens 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0)
664789Sahrens 		goto no_tx_done;
6651669Sperrin 	end_size = MAX(zp->z_phys->zp_size, woff + n);
666789Sahrens top:
667789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
668789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
669789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
670789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
671789Sahrens 	if (error) {
672789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
6732113Sahrens 			dmu_tx_wait(tx);
6742113Sahrens 			dmu_tx_abort(tx);
675789Sahrens 			goto top;
676789Sahrens 		}
6772113Sahrens 		dmu_tx_abort(tx);
678789Sahrens 		goto no_tx_done;
679789Sahrens 	}
680789Sahrens 
6811669Sperrin 	/*
6821669Sperrin 	 * If zfs_range_lock() over-locked we grow the blocksize
6831669Sperrin 	 * and then reduce the lock range.
6841669Sperrin 	 */
6851669Sperrin 	if (rl->r_len == UINT64_MAX) {
686789Sahrens 		uint64_t new_blksz;
6871669Sperrin 
688789Sahrens 		if (zp->z_blksz > max_blksz) {
689789Sahrens 			ASSERT(!ISP2(zp->z_blksz));
690789Sahrens 			new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
691789Sahrens 		} else {
692789Sahrens 			new_blksz = MIN(end_size, max_blksz);
693789Sahrens 		}
6941669Sperrin 		zfs_grow_blocksize(zp, new_blksz, tx);
6952237Smaybee 		zfs_range_reduce(rl, woff, n);
696789Sahrens 	}
697789Sahrens 
698789Sahrens 	/*
699789Sahrens 	 * The file data does not fit in the znode "cache", so we
700789Sahrens 	 * will be writing to the file block data buffers.
701789Sahrens 	 * Each buffer will be written in a separate transaction;
702789Sahrens 	 * this keeps the intent log records small and allows us
703789Sahrens 	 * to do more fine-grained space accounting.
704789Sahrens 	 */
705789Sahrens 	while (n > 0) {
706789Sahrens 		/*
707789Sahrens 		 * XXX - should we really limit each write to z_max_blksz?
708789Sahrens 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
709789Sahrens 		 */
710789Sahrens 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
711789Sahrens 		rw_enter(&zp->z_map_lock, RW_READER);
712789Sahrens 
713789Sahrens 		tx_bytes = uio->uio_resid;
714789Sahrens 		if (vn_has_cached_data(vp)) {
715789Sahrens 			rw_exit(&zp->z_map_lock);
716789Sahrens 			error = mappedwrite(vp, woff, nbytes, uio, tx);
717789Sahrens 		} else {
718789Sahrens 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
719789Sahrens 			    woff, nbytes, uio, tx);
720789Sahrens 			rw_exit(&zp->z_map_lock);
721789Sahrens 		}
722789Sahrens 		tx_bytes -= uio->uio_resid;
723789Sahrens 
724789Sahrens 		if (error) {
725789Sahrens 			/* XXX - do we need to "clean up" the dmu buffer? */
726789Sahrens 			break;
727789Sahrens 		}
728789Sahrens 
729789Sahrens 		ASSERT(tx_bytes == nbytes);
730789Sahrens 
7311576Smarks 		/*
7321576Smarks 		 * Clear Set-UID/Set-GID bits on successful write if not
7331576Smarks 		 * privileged and at least one of the excute bits is set.
7341576Smarks 		 *
7351576Smarks 		 * It would be nice to to this after all writes have
7361576Smarks 		 * been done, but that would still expose the ISUID/ISGID
7371576Smarks 		 * to another app after the partial write is committed.
7381576Smarks 		 */
7391576Smarks 
7401576Smarks 		mutex_enter(&zp->z_acl_lock);
7411576Smarks 		if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) |
7421576Smarks 		    (S_IXUSR >> 6))) != 0 &&
7431576Smarks 		    (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 &&
7441576Smarks 		    secpolicy_vnode_setid_retain(cr,
7451576Smarks 		    (zp->z_phys->zp_mode & S_ISUID) != 0 &&
7461576Smarks 		    zp->z_phys->zp_uid == 0) != 0) {
7471576Smarks 			    zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID);
7481576Smarks 		}
7491576Smarks 		mutex_exit(&zp->z_acl_lock);
7501576Smarks 
751789Sahrens 		n -= nbytes;
752789Sahrens 		if (n <= 0)
753789Sahrens 			break;
754789Sahrens 
755789Sahrens 		/*
756789Sahrens 		 * We have more work ahead of us, so wrap up this transaction
757789Sahrens 		 * and start another.  Exact same logic as tx_done below.
758789Sahrens 		 */
759789Sahrens 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) {
760789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
761789Sahrens 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
762789Sahrens 			    uio->uio_loffset);
763789Sahrens 		}
764789Sahrens 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
765789Sahrens 		seq = zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes,
766789Sahrens 		    ioflag, uio);
767789Sahrens 		dmu_tx_commit(tx);
768789Sahrens 
769789Sahrens 		/*
770789Sahrens 		 * Start another transaction.
771789Sahrens 		 */
772789Sahrens 		woff = uio->uio_loffset;
773789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
774789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
775789Sahrens 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
776789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
777789Sahrens 		if (error) {
778789Sahrens 			if (error == ERESTART &&
779789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
7802113Sahrens 				dmu_tx_wait(tx);
7812113Sahrens 				dmu_tx_abort(tx);
782789Sahrens 				goto top;
783789Sahrens 			}
7842113Sahrens 			dmu_tx_abort(tx);
785789Sahrens 			goto no_tx_done;
786789Sahrens 		}
787789Sahrens 	}
788789Sahrens 
789789Sahrens tx_done:
790789Sahrens 
791789Sahrens 	if (tx_bytes != 0) {
792789Sahrens 		/*
793789Sahrens 		 * Update the file size if it has changed; account
794789Sahrens 		 * for possible concurrent updates.
795789Sahrens 		 */
796789Sahrens 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) {
797789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
798789Sahrens 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
799789Sahrens 			    uio->uio_loffset);
800789Sahrens 		}
801789Sahrens 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
802789Sahrens 		seq = zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes,
803789Sahrens 		    ioflag, uio);
804789Sahrens 	}
805789Sahrens 	dmu_tx_commit(tx);
806789Sahrens 
807789Sahrens 
808789Sahrens no_tx_done:
809789Sahrens 
8102237Smaybee 	zfs_range_unlock(rl);
811789Sahrens 
812789Sahrens 	/*
813789Sahrens 	 * If we're in replay mode, or we made no progress, return error.
814789Sahrens 	 * Otherwise, it's at least a partial write, so it's successful.
815789Sahrens 	 */
816789Sahrens 	if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) {
817789Sahrens 		ZFS_EXIT(zfsvfs);
818789Sahrens 		return (error);
819789Sahrens 	}
820789Sahrens 
821789Sahrens 	zil_commit(zilog, seq, ioflag & (FSYNC | FDSYNC));
822789Sahrens 
823789Sahrens 	ZFS_EXIT(zfsvfs);
824789Sahrens 	return (0);
825789Sahrens }
826789Sahrens 
8272237Smaybee void
8282237Smaybee zfs_get_done(dmu_buf_t *db, void *vrl)
8292237Smaybee {
8302237Smaybee 	rl_t *rl = (rl_t *)vrl;
8312237Smaybee 	vnode_t *vp = ZTOV(rl->r_zp);
8322237Smaybee 
8332237Smaybee 	dmu_buf_rele(db, rl);
8342237Smaybee 	zfs_range_unlock(rl);
8352237Smaybee 	VN_RELE(vp);
8362237Smaybee }
8372237Smaybee 
838789Sahrens /*
839789Sahrens  * Get data to generate a TX_WRITE intent log record.
840789Sahrens  */
841789Sahrens int
8422237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
843789Sahrens {
844789Sahrens 	zfsvfs_t *zfsvfs = arg;
845789Sahrens 	objset_t *os = zfsvfs->z_os;
846789Sahrens 	znode_t *zp;
847789Sahrens 	uint64_t off = lr->lr_offset;
8482237Smaybee 	dmu_buf_t *db;
8491669Sperrin 	rl_t *rl;
850789Sahrens 	int dlen = lr->lr_length;  		/* length of user data */
851789Sahrens 	int error = 0;
852789Sahrens 
853789Sahrens 	ASSERT(dlen != 0);
854789Sahrens 
855789Sahrens 	/*
8561669Sperrin 	 * Nothing to do if the file has been removed
857789Sahrens 	 */
858789Sahrens 	if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0)
859789Sahrens 		return (ENOENT);
8601669Sperrin 	if (zp->z_reap) {
861789Sahrens 		VN_RELE(ZTOV(zp));
862789Sahrens 		return (ENOENT);
863789Sahrens 	}
864789Sahrens 
865789Sahrens 	/*
866789Sahrens 	 * Write records come in two flavors: immediate and indirect.
867789Sahrens 	 * For small writes it's cheaper to store the data with the
868789Sahrens 	 * log record (immediate); for large writes it's cheaper to
869789Sahrens 	 * sync the data and get a pointer to it (indirect) so that
870789Sahrens 	 * we don't have to write the data twice.
871789Sahrens 	 */
8721669Sperrin 	if (buf != NULL) { /* immediate write */
8731669Sperrin 		rl = zfs_range_lock(zp, off, dlen, RL_READER);
8741669Sperrin 		/* test for truncation needs to be done while range locked */
8751669Sperrin 		if (off >= zp->z_phys->zp_size) {
8761669Sperrin 			error = ENOENT;
8771669Sperrin 			goto out;
8781669Sperrin 		}
8791544Seschrock 		VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, off, FTAG, &db));
8801669Sperrin 		bcopy((char *)db->db_data + off - db->db_offset, buf, dlen);
8811544Seschrock 		dmu_buf_rele(db, FTAG);
8821669Sperrin 	} else { /* indirect write */
8831669Sperrin 		uint64_t boff; /* block starting offset */
8841669Sperrin 
885789Sahrens 		/*
8861669Sperrin 		 * Have to lock the whole block to ensure when it's
8871669Sperrin 		 * written out and it's checksum is being calculated
8881669Sperrin 		 * that no one can change the data. We need to re-check
8891669Sperrin 		 * blocksize after we get the lock in case it's changed!
890789Sahrens 		 */
8911669Sperrin 		for (;;) {
8921941Sperrin 			if (ISP2(zp->z_blksz)) {
8931941Sperrin 				boff = P2ALIGN_TYPED(off, zp->z_blksz,
8941941Sperrin 				    uint64_t);
8951941Sperrin 			} else {
8961941Sperrin 				boff = 0;
8971941Sperrin 			}
8981669Sperrin 			dlen = zp->z_blksz;
8991669Sperrin 			rl = zfs_range_lock(zp, boff, dlen, RL_READER);
9001669Sperrin 			if (zp->z_blksz == dlen)
9011669Sperrin 				break;
9022237Smaybee 			zfs_range_unlock(rl);
9031669Sperrin 		}
9041669Sperrin 		/* test for truncation needs to be done while range locked */
9051669Sperrin 		if (off >= zp->z_phys->zp_size) {
9061669Sperrin 			error = ENOENT;
9071669Sperrin 			goto out;
9081669Sperrin 		}
9092237Smaybee 		VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, rl, &db));
9102237Smaybee 		ASSERT(boff == db->db_offset);
9112237Smaybee 		lr->lr_blkoff = off - boff;
9122237Smaybee 		error = dmu_sync(zio, db, &lr->lr_blkptr,
9132237Smaybee 		    lr->lr_common.lrc_txg, zio ? zfs_get_done : NULL, rl);
9142237Smaybee 		/*
9152237Smaybee 		 * If we get EINPROGRESS, then we need to wait for a
9162237Smaybee 		 * write IO initiated by dmu_sync() to complete before
9172237Smaybee 		 * we can release this dbuf.  We will finish everthing
9182237Smaybee 		 * up in the zfs_get_done() callback.
9192237Smaybee 		 */
9202237Smaybee 		if (error == EINPROGRESS)
9212237Smaybee 			return (0);
9222237Smaybee 		dmu_buf_rele(db, rl);
923789Sahrens 	}
9241669Sperrin out:
9252237Smaybee 	zfs_range_unlock(rl);
926789Sahrens 	VN_RELE(ZTOV(zp));
927789Sahrens 	return (error);
928789Sahrens }
929789Sahrens 
930789Sahrens /*ARGSUSED*/
931789Sahrens static int
932789Sahrens zfs_access(vnode_t *vp, int mode, int flags, cred_t *cr)
933789Sahrens {
934789Sahrens 	znode_t *zp = VTOZ(vp);
935789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
936789Sahrens 	int error;
937789Sahrens 
938789Sahrens 	ZFS_ENTER(zfsvfs);
939789Sahrens 	error = zfs_zaccess_rwx(zp, mode, cr);
940789Sahrens 	ZFS_EXIT(zfsvfs);
941789Sahrens 	return (error);
942789Sahrens }
943789Sahrens 
944789Sahrens /*
945789Sahrens  * Lookup an entry in a directory, or an extended attribute directory.
946789Sahrens  * If it exists, return a held vnode reference for it.
947789Sahrens  *
948789Sahrens  *	IN:	dvp	- vnode of directory to search.
949789Sahrens  *		nm	- name of entry to lookup.
950789Sahrens  *		pnp	- full pathname to lookup [UNUSED].
951789Sahrens  *		flags	- LOOKUP_XATTR set if looking for an attribute.
952789Sahrens  *		rdir	- root directory vnode [UNUSED].
953789Sahrens  *		cr	- credentials of caller.
954789Sahrens  *
955789Sahrens  *	OUT:	vpp	- vnode of located entry, NULL if not found.
956789Sahrens  *
957789Sahrens  *	RETURN:	0 if success
958789Sahrens  *		error code if failure
959789Sahrens  *
960789Sahrens  * Timestamps:
961789Sahrens  *	NA
962789Sahrens  */
963789Sahrens /* ARGSUSED */
964789Sahrens static int
965789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
966789Sahrens     int flags, vnode_t *rdir, cred_t *cr)
967789Sahrens {
968789Sahrens 
969789Sahrens 	znode_t *zdp = VTOZ(dvp);
970789Sahrens 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
971789Sahrens 	int	error;
972789Sahrens 
973789Sahrens 	ZFS_ENTER(zfsvfs);
974789Sahrens 
975789Sahrens 	*vpp = NULL;
976789Sahrens 
977789Sahrens 	if (flags & LOOKUP_XATTR) {
978789Sahrens 		/*
979789Sahrens 		 * We don't allow recursive attributes..
980789Sahrens 		 * Maybe someday we will.
981789Sahrens 		 */
982789Sahrens 		if (zdp->z_phys->zp_flags & ZFS_XATTR) {
983789Sahrens 			ZFS_EXIT(zfsvfs);
984789Sahrens 			return (EINVAL);
985789Sahrens 		}
986789Sahrens 
987789Sahrens 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr)) {
988789Sahrens 			ZFS_EXIT(zfsvfs);
989789Sahrens 			return (error);
990789Sahrens 		}
991789Sahrens 
992789Sahrens 		/*
993789Sahrens 		 * Do we have permission to get into attribute directory?
994789Sahrens 		 */
995789Sahrens 
996789Sahrens 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, cr)) {
997789Sahrens 			VN_RELE(*vpp);
998789Sahrens 		}
999789Sahrens 
1000789Sahrens 		ZFS_EXIT(zfsvfs);
1001789Sahrens 		return (error);
1002789Sahrens 	}
1003789Sahrens 
10041512Sek110237 	if (dvp->v_type != VDIR) {
10051512Sek110237 		ZFS_EXIT(zfsvfs);
10061460Smarks 		return (ENOTDIR);
10071512Sek110237 	}
10081460Smarks 
1009789Sahrens 	/*
1010789Sahrens 	 * Check accessibility of directory.
1011789Sahrens 	 */
1012789Sahrens 
1013789Sahrens 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, cr)) {
1014789Sahrens 		ZFS_EXIT(zfsvfs);
1015789Sahrens 		return (error);
1016789Sahrens 	}
1017789Sahrens 
1018789Sahrens 	if ((error = zfs_dirlook(zdp, nm, vpp)) == 0) {
1019789Sahrens 
1020789Sahrens 		/*
1021789Sahrens 		 * Convert device special files
1022789Sahrens 		 */
1023789Sahrens 		if (IS_DEVVP(*vpp)) {
1024789Sahrens 			vnode_t	*svp;
1025789Sahrens 
1026789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1027789Sahrens 			VN_RELE(*vpp);
1028789Sahrens 			if (svp == NULL)
1029789Sahrens 				error = ENOSYS;
1030789Sahrens 			else
1031789Sahrens 				*vpp = svp;
1032789Sahrens 		}
1033789Sahrens 	}
1034789Sahrens 
1035789Sahrens 	ZFS_EXIT(zfsvfs);
1036789Sahrens 	return (error);
1037789Sahrens }
1038789Sahrens 
1039789Sahrens /*
1040789Sahrens  * Attempt to create a new entry in a directory.  If the entry
1041789Sahrens  * already exists, truncate the file if permissible, else return
1042789Sahrens  * an error.  Return the vp of the created or trunc'd file.
1043789Sahrens  *
1044789Sahrens  *	IN:	dvp	- vnode of directory to put new file entry in.
1045789Sahrens  *		name	- name of new file entry.
1046789Sahrens  *		vap	- attributes of new file.
1047789Sahrens  *		excl	- flag indicating exclusive or non-exclusive mode.
1048789Sahrens  *		mode	- mode to open file with.
1049789Sahrens  *		cr	- credentials of caller.
1050789Sahrens  *		flag	- large file flag [UNUSED].
1051789Sahrens  *
1052789Sahrens  *	OUT:	vpp	- vnode of created or trunc'd entry.
1053789Sahrens  *
1054789Sahrens  *	RETURN:	0 if success
1055789Sahrens  *		error code if failure
1056789Sahrens  *
1057789Sahrens  * Timestamps:
1058789Sahrens  *	dvp - ctime|mtime updated if new entry created
1059789Sahrens  *	 vp - ctime|mtime always, atime if new
1060789Sahrens  */
1061789Sahrens /* ARGSUSED */
1062789Sahrens static int
1063789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1064789Sahrens     int mode, vnode_t **vpp, cred_t *cr, int flag)
1065789Sahrens {
1066789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1067789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1068789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1069789Sahrens 	uint64_t	seq = 0;
1070789Sahrens 	objset_t	*os = zfsvfs->z_os;
1071789Sahrens 	zfs_dirlock_t	*dl;
1072789Sahrens 	dmu_tx_t	*tx;
1073789Sahrens 	int		error;
1074789Sahrens 	uint64_t	zoid;
1075789Sahrens 
1076789Sahrens 	ZFS_ENTER(zfsvfs);
1077789Sahrens 
1078789Sahrens top:
1079789Sahrens 	*vpp = NULL;
1080789Sahrens 
1081789Sahrens 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1082789Sahrens 		vap->va_mode &= ~VSVTX;
1083789Sahrens 
1084789Sahrens 	if (*name == '\0') {
1085789Sahrens 		/*
1086789Sahrens 		 * Null component name refers to the directory itself.
1087789Sahrens 		 */
1088789Sahrens 		VN_HOLD(dvp);
1089789Sahrens 		zp = dzp;
1090789Sahrens 		dl = NULL;
1091789Sahrens 		error = 0;
1092789Sahrens 	} else {
1093789Sahrens 		/* possible VN_HOLD(zp) */
1094789Sahrens 		if (error = zfs_dirent_lock(&dl, dzp, name, &zp, 0)) {
1095789Sahrens 			if (strcmp(name, "..") == 0)
1096789Sahrens 				error = EISDIR;
1097789Sahrens 			ZFS_EXIT(zfsvfs);
1098789Sahrens 			return (error);
1099789Sahrens 		}
1100789Sahrens 	}
1101789Sahrens 
1102789Sahrens 	zoid = zp ? zp->z_id : -1ULL;
1103789Sahrens 
1104789Sahrens 	if (zp == NULL) {
1105789Sahrens 		/*
1106789Sahrens 		 * Create a new file object and update the directory
1107789Sahrens 		 * to reference it.
1108789Sahrens 		 */
1109789Sahrens 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
1110789Sahrens 			goto out;
1111789Sahrens 		}
1112789Sahrens 
1113789Sahrens 		/*
1114789Sahrens 		 * We only support the creation of regular files in
1115789Sahrens 		 * extended attribute directories.
1116789Sahrens 		 */
1117789Sahrens 		if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1118789Sahrens 		    (vap->va_type != VREG)) {
1119789Sahrens 			error = EINVAL;
1120789Sahrens 			goto out;
1121789Sahrens 		}
1122789Sahrens 
1123789Sahrens 		tx = dmu_tx_create(os);
1124789Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1125789Sahrens 		dmu_tx_hold_bonus(tx, dzp->z_id);
11261544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1127789Sahrens 		if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
1128789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1129789Sahrens 			    0, SPA_MAXBLOCKSIZE);
1130789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
1131789Sahrens 		if (error) {
1132789Sahrens 			zfs_dirent_unlock(dl);
1133789Sahrens 			if (error == ERESTART &&
1134789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
11352113Sahrens 				dmu_tx_wait(tx);
11362113Sahrens 				dmu_tx_abort(tx);
1137789Sahrens 				goto top;
1138789Sahrens 			}
11392113Sahrens 			dmu_tx_abort(tx);
1140789Sahrens 			ZFS_EXIT(zfsvfs);
1141789Sahrens 			return (error);
1142789Sahrens 		}
1143789Sahrens 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
1144789Sahrens 		ASSERT(zp->z_id == zoid);
1145789Sahrens 		(void) zfs_link_create(dl, zp, tx, ZNEW);
1146789Sahrens 		seq = zfs_log_create(zilog, tx, TX_CREATE, dzp, zp, name);
1147789Sahrens 		dmu_tx_commit(tx);
1148789Sahrens 	} else {
1149789Sahrens 		/*
1150789Sahrens 		 * A directory entry already exists for this name.
1151789Sahrens 		 */
1152789Sahrens 		/*
1153789Sahrens 		 * Can't truncate an existing file if in exclusive mode.
1154789Sahrens 		 */
1155789Sahrens 		if (excl == EXCL) {
1156789Sahrens 			error = EEXIST;
1157789Sahrens 			goto out;
1158789Sahrens 		}
1159789Sahrens 		/*
1160789Sahrens 		 * Can't open a directory for writing.
1161789Sahrens 		 */
1162789Sahrens 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1163789Sahrens 			error = EISDIR;
1164789Sahrens 			goto out;
1165789Sahrens 		}
1166789Sahrens 		/*
1167789Sahrens 		 * Verify requested access to file.
1168789Sahrens 		 */
1169789Sahrens 		if (mode && (error = zfs_zaccess_rwx(zp, mode, cr))) {
1170789Sahrens 			goto out;
1171789Sahrens 		}
1172789Sahrens 
1173789Sahrens 		mutex_enter(&dzp->z_lock);
1174789Sahrens 		dzp->z_seq++;
1175789Sahrens 		mutex_exit(&dzp->z_lock);
1176789Sahrens 
11771878Smaybee 		/*
11781878Smaybee 		 * Truncate regular files if requested.
11791878Smaybee 		 */
11801878Smaybee 		if ((ZTOV(zp)->v_type == VREG) &&
11811878Smaybee 		    (zp->z_phys->zp_size != 0) &&
1182789Sahrens 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
11831878Smaybee 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
11841878Smaybee 			if (error == ERESTART &&
11851878Smaybee 			    zfsvfs->z_assign == TXG_NOWAIT) {
11862113Sahrens 				/* NB: we already did dmu_tx_wait() */
11871878Smaybee 				zfs_dirent_unlock(dl);
1188*2365Sperrin 				VN_RELE(ZTOV(zp));
11891878Smaybee 				goto top;
1190789Sahrens 			}
1191789Sahrens 		}
1192789Sahrens 	}
1193789Sahrens out:
1194789Sahrens 
1195789Sahrens 	if (dl)
1196789Sahrens 		zfs_dirent_unlock(dl);
1197789Sahrens 
1198789Sahrens 	if (error) {
1199789Sahrens 		if (zp)
1200789Sahrens 			VN_RELE(ZTOV(zp));
1201789Sahrens 	} else {
1202789Sahrens 		*vpp = ZTOV(zp);
1203789Sahrens 		/*
1204789Sahrens 		 * If vnode is for a device return a specfs vnode instead.
1205789Sahrens 		 */
1206789Sahrens 		if (IS_DEVVP(*vpp)) {
1207789Sahrens 			struct vnode *svp;
1208789Sahrens 
1209789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1210789Sahrens 			VN_RELE(*vpp);
1211789Sahrens 			if (svp == NULL) {
1212789Sahrens 				error = ENOSYS;
1213789Sahrens 			}
1214789Sahrens 			*vpp = svp;
1215789Sahrens 		}
1216789Sahrens 	}
1217789Sahrens 
1218789Sahrens 	zil_commit(zilog, seq, 0);
1219789Sahrens 
1220789Sahrens 	ZFS_EXIT(zfsvfs);
1221789Sahrens 	return (error);
1222789Sahrens }
1223789Sahrens 
1224789Sahrens /*
1225789Sahrens  * Remove an entry from a directory.
1226789Sahrens  *
1227789Sahrens  *	IN:	dvp	- vnode of directory to remove entry from.
1228789Sahrens  *		name	- name of entry to remove.
1229789Sahrens  *		cr	- credentials of caller.
1230789Sahrens  *
1231789Sahrens  *	RETURN:	0 if success
1232789Sahrens  *		error code if failure
1233789Sahrens  *
1234789Sahrens  * Timestamps:
1235789Sahrens  *	dvp - ctime|mtime
1236789Sahrens  *	 vp - ctime (if nlink > 0)
1237789Sahrens  */
1238789Sahrens static int
1239789Sahrens zfs_remove(vnode_t *dvp, char *name, cred_t *cr)
1240789Sahrens {
1241789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1242789Sahrens 	znode_t		*xzp = NULL;
1243789Sahrens 	vnode_t		*vp;
1244789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1245789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1246789Sahrens 	uint64_t	seq = 0;
1247789Sahrens 	uint64_t	acl_obj, xattr_obj;
1248789Sahrens 	zfs_dirlock_t	*dl;
1249789Sahrens 	dmu_tx_t	*tx;
1250789Sahrens 	int		may_delete_now, delete_now = FALSE;
1251789Sahrens 	int		reaped;
1252789Sahrens 	int		error;
1253789Sahrens 
1254789Sahrens 	ZFS_ENTER(zfsvfs);
1255789Sahrens 
1256789Sahrens top:
1257789Sahrens 	/*
1258789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1259789Sahrens 	 */
1260789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
1261789Sahrens 		ZFS_EXIT(zfsvfs);
1262789Sahrens 		return (error);
1263789Sahrens 	}
1264789Sahrens 
1265789Sahrens 	vp = ZTOV(zp);
1266789Sahrens 
1267789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1268789Sahrens 		goto out;
1269789Sahrens 	}
1270789Sahrens 
1271789Sahrens 	/*
1272789Sahrens 	 * Need to use rmdir for removing directories.
1273789Sahrens 	 */
1274789Sahrens 	if (vp->v_type == VDIR) {
1275789Sahrens 		error = EPERM;
1276789Sahrens 		goto out;
1277789Sahrens 	}
1278789Sahrens 
1279789Sahrens 	vnevent_remove(vp);
1280789Sahrens 
12811484Sek110237 	dnlc_remove(dvp, name);
12821484Sek110237 
1283789Sahrens 	mutex_enter(&vp->v_lock);
1284789Sahrens 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1285789Sahrens 	mutex_exit(&vp->v_lock);
1286789Sahrens 
1287789Sahrens 	/*
1288789Sahrens 	 * We may delete the znode now, or we may put it on the delete queue;
1289789Sahrens 	 * it depends on whether we're the last link, and on whether there are
1290789Sahrens 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1291789Sahrens 	 * allow for either case.
1292789Sahrens 	 */
1293789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
12941544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1295789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
1296789Sahrens 	if (may_delete_now)
1297789Sahrens 		dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
1298789Sahrens 
1299789Sahrens 	/* are there any extended attributes? */
1300789Sahrens 	if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1301789Sahrens 		/*
1302789Sahrens 		 * XXX - There is a possibility that the delete
1303789Sahrens 		 * of the parent file could succeed, but then we get
1304789Sahrens 		 * an ENOSPC when we try to delete the xattrs...
1305789Sahrens 		 * so we would need to re-try the deletes periodically
1306789Sahrens 		 */
1307789Sahrens 		/* XXX - do we need this if we are deleting? */
1308789Sahrens 		dmu_tx_hold_bonus(tx, xattr_obj);
1309789Sahrens 	}
1310789Sahrens 
1311789Sahrens 	/* are there any additional acls */
1312789Sahrens 	if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1313789Sahrens 	    may_delete_now)
1314789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1315789Sahrens 
1316789Sahrens 	/* charge as an update -- would be nice not to charge at all */
13171544Seschrock 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
1318789Sahrens 
1319789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1320789Sahrens 	if (error) {
1321789Sahrens 		zfs_dirent_unlock(dl);
1322789Sahrens 		VN_RELE(vp);
1323789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
13242113Sahrens 			dmu_tx_wait(tx);
13252113Sahrens 			dmu_tx_abort(tx);
1326789Sahrens 			goto top;
1327789Sahrens 		}
13282113Sahrens 		dmu_tx_abort(tx);
1329789Sahrens 		ZFS_EXIT(zfsvfs);
1330789Sahrens 		return (error);
1331789Sahrens 	}
1332789Sahrens 
1333789Sahrens 	/*
1334789Sahrens 	 * Remove the directory entry.
1335789Sahrens 	 */
1336789Sahrens 	error = zfs_link_destroy(dl, zp, tx, 0, &reaped);
1337789Sahrens 
1338789Sahrens 	if (error) {
1339789Sahrens 		dmu_tx_commit(tx);
1340789Sahrens 		goto out;
1341789Sahrens 	}
1342789Sahrens 
1343789Sahrens 	if (reaped) {
1344789Sahrens 		mutex_enter(&vp->v_lock);
1345789Sahrens 		delete_now = may_delete_now &&
1346789Sahrens 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1347789Sahrens 		    zp->z_phys->zp_xattr == xattr_obj &&
1348789Sahrens 		    zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1349789Sahrens 		mutex_exit(&vp->v_lock);
1350789Sahrens 	}
1351789Sahrens 
1352789Sahrens 	if (delete_now) {
1353789Sahrens 		if (zp->z_phys->zp_xattr) {
1354789Sahrens 			error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1355789Sahrens 			ASSERT3U(error, ==, 0);
1356789Sahrens 			ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1357789Sahrens 			dmu_buf_will_dirty(xzp->z_dbuf, tx);
1358789Sahrens 			mutex_enter(&xzp->z_lock);
1359789Sahrens 			xzp->z_reap = 1;
1360789Sahrens 			xzp->z_phys->zp_links = 0;
1361789Sahrens 			mutex_exit(&xzp->z_lock);
1362789Sahrens 			zfs_dq_add(xzp, tx);
1363789Sahrens 			zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1364789Sahrens 		}
1365789Sahrens 		mutex_enter(&zp->z_lock);
1366789Sahrens 		mutex_enter(&vp->v_lock);
1367789Sahrens 		vp->v_count--;
1368789Sahrens 		ASSERT3U(vp->v_count, ==, 0);
1369789Sahrens 		mutex_exit(&vp->v_lock);
1370789Sahrens 		zp->z_active = 0;
1371789Sahrens 		mutex_exit(&zp->z_lock);
1372789Sahrens 		zfs_znode_delete(zp, tx);
1373789Sahrens 		VFS_RELE(zfsvfs->z_vfs);
1374789Sahrens 	} else if (reaped) {
1375789Sahrens 		zfs_dq_add(zp, tx);
1376789Sahrens 	}
1377789Sahrens 
1378789Sahrens 	seq = zfs_log_remove(zilog, tx, TX_REMOVE, dzp, name);
1379789Sahrens 
1380789Sahrens 	dmu_tx_commit(tx);
1381789Sahrens out:
1382789Sahrens 	zfs_dirent_unlock(dl);
1383789Sahrens 
1384789Sahrens 	if (!delete_now) {
1385789Sahrens 		VN_RELE(vp);
1386789Sahrens 	} else if (xzp) {
1387789Sahrens 		/* this rele delayed to prevent nesting transactions */
1388789Sahrens 		VN_RELE(ZTOV(xzp));
1389789Sahrens 	}
1390789Sahrens 
1391789Sahrens 	zil_commit(zilog, seq, 0);
1392789Sahrens 
1393789Sahrens 	ZFS_EXIT(zfsvfs);
1394789Sahrens 	return (error);
1395789Sahrens }
1396789Sahrens 
1397789Sahrens /*
1398789Sahrens  * Create a new directory and insert it into dvp using the name
1399789Sahrens  * provided.  Return a pointer to the inserted directory.
1400789Sahrens  *
1401789Sahrens  *	IN:	dvp	- vnode of directory to add subdir to.
1402789Sahrens  *		dirname	- name of new directory.
1403789Sahrens  *		vap	- attributes of new directory.
1404789Sahrens  *		cr	- credentials of caller.
1405789Sahrens  *
1406789Sahrens  *	OUT:	vpp	- vnode of created directory.
1407789Sahrens  *
1408789Sahrens  *	RETURN:	0 if success
1409789Sahrens  *		error code if failure
1410789Sahrens  *
1411789Sahrens  * Timestamps:
1412789Sahrens  *	dvp - ctime|mtime updated
1413789Sahrens  *	 vp - ctime|mtime|atime updated
1414789Sahrens  */
1415789Sahrens static int
1416789Sahrens zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr)
1417789Sahrens {
1418789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1419789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1420789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1421789Sahrens 	uint64_t	seq = 0;
1422789Sahrens 	zfs_dirlock_t	*dl;
1423789Sahrens 	uint64_t	zoid = 0;
1424789Sahrens 	dmu_tx_t	*tx;
1425789Sahrens 	int		error;
1426789Sahrens 
1427789Sahrens 	ASSERT(vap->va_type == VDIR);
1428789Sahrens 
1429789Sahrens 	ZFS_ENTER(zfsvfs);
1430789Sahrens 
1431789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1432789Sahrens 		ZFS_EXIT(zfsvfs);
1433789Sahrens 		return (EINVAL);
1434789Sahrens 	}
1435789Sahrens top:
1436789Sahrens 	*vpp = NULL;
1437789Sahrens 
1438789Sahrens 	/*
1439789Sahrens 	 * First make sure the new directory doesn't exist.
1440789Sahrens 	 */
1441789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, ZNEW)) {
1442789Sahrens 		ZFS_EXIT(zfsvfs);
1443789Sahrens 		return (error);
1444789Sahrens 	}
1445789Sahrens 
14461231Smarks 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, cr)) {
14471231Smarks 		zfs_dirent_unlock(dl);
14481231Smarks 		ZFS_EXIT(zfsvfs);
14491231Smarks 		return (error);
14501231Smarks 	}
14511231Smarks 
1452789Sahrens 	/*
1453789Sahrens 	 * Add a new entry to the directory.
1454789Sahrens 	 */
1455789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
14561544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
14571544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1458789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
1459789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1460789Sahrens 		    0, SPA_MAXBLOCKSIZE);
1461789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1462789Sahrens 	if (error) {
1463789Sahrens 		zfs_dirent_unlock(dl);
1464789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
14652113Sahrens 			dmu_tx_wait(tx);
14662113Sahrens 			dmu_tx_abort(tx);
1467789Sahrens 			goto top;
1468789Sahrens 		}
14692113Sahrens 		dmu_tx_abort(tx);
1470789Sahrens 		ZFS_EXIT(zfsvfs);
1471789Sahrens 		return (error);
1472789Sahrens 	}
1473789Sahrens 
1474789Sahrens 	/*
1475789Sahrens 	 * Create new node.
1476789Sahrens 	 */
1477789Sahrens 	zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
1478789Sahrens 
1479789Sahrens 	/*
1480789Sahrens 	 * Now put new name in parent dir.
1481789Sahrens 	 */
1482789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1483789Sahrens 
1484789Sahrens 	*vpp = ZTOV(zp);
1485789Sahrens 
1486789Sahrens 	seq = zfs_log_create(zilog, tx, TX_MKDIR, dzp, zp, dirname);
1487789Sahrens 	dmu_tx_commit(tx);
1488789Sahrens 
1489789Sahrens 	zfs_dirent_unlock(dl);
1490789Sahrens 
1491789Sahrens 	zil_commit(zilog, seq, 0);
1492789Sahrens 
1493789Sahrens 	ZFS_EXIT(zfsvfs);
1494789Sahrens 	return (0);
1495789Sahrens }
1496789Sahrens 
1497789Sahrens /*
1498789Sahrens  * Remove a directory subdir entry.  If the current working
1499789Sahrens  * directory is the same as the subdir to be removed, the
1500789Sahrens  * remove will fail.
1501789Sahrens  *
1502789Sahrens  *	IN:	dvp	- vnode of directory to remove from.
1503789Sahrens  *		name	- name of directory to be removed.
1504789Sahrens  *		cwd	- vnode of current working directory.
1505789Sahrens  *		cr	- credentials of caller.
1506789Sahrens  *
1507789Sahrens  *	RETURN:	0 if success
1508789Sahrens  *		error code if failure
1509789Sahrens  *
1510789Sahrens  * Timestamps:
1511789Sahrens  *	dvp - ctime|mtime updated
1512789Sahrens  */
1513789Sahrens static int
1514789Sahrens zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr)
1515789Sahrens {
1516789Sahrens 	znode_t		*dzp = VTOZ(dvp);
1517789Sahrens 	znode_t		*zp;
1518789Sahrens 	vnode_t		*vp;
1519789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1520789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1521789Sahrens 	uint64_t	seq = 0;
1522789Sahrens 	zfs_dirlock_t	*dl;
1523789Sahrens 	dmu_tx_t	*tx;
1524789Sahrens 	int		error;
1525789Sahrens 
1526789Sahrens 	ZFS_ENTER(zfsvfs);
1527789Sahrens 
1528789Sahrens top:
1529789Sahrens 	zp = NULL;
1530789Sahrens 
1531789Sahrens 	/*
1532789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1533789Sahrens 	 */
1534789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
1535789Sahrens 		ZFS_EXIT(zfsvfs);
1536789Sahrens 		return (error);
1537789Sahrens 	}
1538789Sahrens 
1539789Sahrens 	vp = ZTOV(zp);
1540789Sahrens 
1541789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1542789Sahrens 		goto out;
1543789Sahrens 	}
1544789Sahrens 
1545789Sahrens 	if (vp->v_type != VDIR) {
1546789Sahrens 		error = ENOTDIR;
1547789Sahrens 		goto out;
1548789Sahrens 	}
1549789Sahrens 
1550789Sahrens 	if (vp == cwd) {
1551789Sahrens 		error = EINVAL;
1552789Sahrens 		goto out;
1553789Sahrens 	}
1554789Sahrens 
1555789Sahrens 	vnevent_rmdir(vp);
1556789Sahrens 
1557789Sahrens 	/*
1558789Sahrens 	 * Grab a lock on the parent pointer make sure we play well
1559789Sahrens 	 * with the treewalk and directory rename code.
1560789Sahrens 	 */
1561789Sahrens 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1562789Sahrens 
1563789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
15641544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1565789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
15661544Seschrock 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
1567789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1568789Sahrens 	if (error) {
1569789Sahrens 		rw_exit(&zp->z_parent_lock);
1570789Sahrens 		zfs_dirent_unlock(dl);
1571789Sahrens 		VN_RELE(vp);
1572789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
15732113Sahrens 			dmu_tx_wait(tx);
15742113Sahrens 			dmu_tx_abort(tx);
1575789Sahrens 			goto top;
1576789Sahrens 		}
15772113Sahrens 		dmu_tx_abort(tx);
1578789Sahrens 		ZFS_EXIT(zfsvfs);
1579789Sahrens 		return (error);
1580789Sahrens 	}
1581789Sahrens 
1582789Sahrens 	error = zfs_link_destroy(dl, zp, tx, 0, NULL);
1583789Sahrens 
1584789Sahrens 	if (error == 0)
1585789Sahrens 		seq = zfs_log_remove(zilog, tx, TX_RMDIR, dzp, name);
1586789Sahrens 
1587789Sahrens 	dmu_tx_commit(tx);
1588789Sahrens 
1589789Sahrens 	rw_exit(&zp->z_parent_lock);
1590789Sahrens out:
1591789Sahrens 	zfs_dirent_unlock(dl);
1592789Sahrens 
1593789Sahrens 	VN_RELE(vp);
1594789Sahrens 
1595789Sahrens 	zil_commit(zilog, seq, 0);
1596789Sahrens 
1597789Sahrens 	ZFS_EXIT(zfsvfs);
1598789Sahrens 	return (error);
1599789Sahrens }
1600789Sahrens 
1601789Sahrens /*
1602789Sahrens  * Read as many directory entries as will fit into the provided
1603789Sahrens  * buffer from the given directory cursor position (specified in
1604789Sahrens  * the uio structure.
1605789Sahrens  *
1606789Sahrens  *	IN:	vp	- vnode of directory to read.
1607789Sahrens  *		uio	- structure supplying read location, range info,
1608789Sahrens  *			  and return buffer.
1609789Sahrens  *		cr	- credentials of caller.
1610789Sahrens  *
1611789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
1612789Sahrens  *		eofp	- set to true if end-of-file detected.
1613789Sahrens  *
1614789Sahrens  *	RETURN:	0 if success
1615789Sahrens  *		error code if failure
1616789Sahrens  *
1617789Sahrens  * Timestamps:
1618789Sahrens  *	vp - atime updated
1619789Sahrens  *
1620789Sahrens  * Note that the low 4 bits of the cookie returned by zap is always zero.
1621789Sahrens  * This allows us to use the low range for "special" directory entries:
1622789Sahrens  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
1623789Sahrens  * we use the offset 2 for the '.zfs' directory.
1624789Sahrens  */
1625789Sahrens /* ARGSUSED */
1626789Sahrens static int
1627789Sahrens zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp)
1628789Sahrens {
1629789Sahrens 	znode_t		*zp = VTOZ(vp);
1630789Sahrens 	iovec_t		*iovp;
1631789Sahrens 	dirent64_t	*odp;
1632789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1633869Sperrin 	objset_t	*os;
1634789Sahrens 	caddr_t		outbuf;
1635789Sahrens 	size_t		bufsize;
1636789Sahrens 	zap_cursor_t	zc;
1637789Sahrens 	zap_attribute_t	zap;
1638789Sahrens 	uint_t		bytes_wanted;
1639789Sahrens 	ushort_t	this_reclen;
1640789Sahrens 	uint64_t	offset; /* must be unsigned; checks for < 1 */
1641789Sahrens 	off64_t		*next;
1642789Sahrens 	int		local_eof;
1643869Sperrin 	int		outcount;
1644869Sperrin 	int		error;
1645869Sperrin 	uint8_t		prefetch;
1646789Sahrens 
1647789Sahrens 	ZFS_ENTER(zfsvfs);
1648789Sahrens 
1649789Sahrens 	/*
1650789Sahrens 	 * If we are not given an eof variable,
1651789Sahrens 	 * use a local one.
1652789Sahrens 	 */
1653789Sahrens 	if (eofp == NULL)
1654789Sahrens 		eofp = &local_eof;
1655789Sahrens 
1656789Sahrens 	/*
1657789Sahrens 	 * Check for valid iov_len.
1658789Sahrens 	 */
1659789Sahrens 	if (uio->uio_iov->iov_len <= 0) {
1660789Sahrens 		ZFS_EXIT(zfsvfs);
1661789Sahrens 		return (EINVAL);
1662789Sahrens 	}
1663789Sahrens 
1664789Sahrens 	/*
1665789Sahrens 	 * Quit if directory has been removed (posix)
1666789Sahrens 	 */
1667789Sahrens 	if ((*eofp = zp->z_reap) != 0) {
1668789Sahrens 		ZFS_EXIT(zfsvfs);
1669789Sahrens 		return (0);
1670789Sahrens 	}
1671789Sahrens 
1672869Sperrin 	error = 0;
1673869Sperrin 	os = zfsvfs->z_os;
1674869Sperrin 	offset = uio->uio_loffset;
1675869Sperrin 	prefetch = zp->z_zn_prefetch;
1676869Sperrin 
1677789Sahrens 	/*
1678789Sahrens 	 * Initialize the iterator cursor.
1679789Sahrens 	 */
1680789Sahrens 	if (offset <= 3) {
1681789Sahrens 		/*
1682789Sahrens 		 * Start iteration from the beginning of the directory.
1683789Sahrens 		 */
1684869Sperrin 		zap_cursor_init(&zc, os, zp->z_id);
1685789Sahrens 	} else {
1686789Sahrens 		/*
1687789Sahrens 		 * The offset is a serialized cursor.
1688789Sahrens 		 */
1689869Sperrin 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1690789Sahrens 	}
1691789Sahrens 
1692789Sahrens 	/*
1693789Sahrens 	 * Get space to change directory entries into fs independent format.
1694789Sahrens 	 */
1695789Sahrens 	iovp = uio->uio_iov;
1696789Sahrens 	bytes_wanted = iovp->iov_len;
1697789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
1698789Sahrens 		bufsize = bytes_wanted;
1699789Sahrens 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
1700789Sahrens 		odp = (struct dirent64 *)outbuf;
1701789Sahrens 	} else {
1702789Sahrens 		bufsize = bytes_wanted;
1703789Sahrens 		odp = (struct dirent64 *)iovp->iov_base;
1704789Sahrens 	}
1705789Sahrens 
1706789Sahrens 	/*
1707789Sahrens 	 * Transform to file-system independent format
1708789Sahrens 	 */
1709789Sahrens 	outcount = 0;
1710789Sahrens 	while (outcount < bytes_wanted) {
1711789Sahrens 		/*
1712789Sahrens 		 * Special case `.', `..', and `.zfs'.
1713789Sahrens 		 */
1714789Sahrens 		if (offset == 0) {
1715789Sahrens 			(void) strcpy(zap.za_name, ".");
1716789Sahrens 			zap.za_first_integer = zp->z_id;
1717789Sahrens 			this_reclen = DIRENT64_RECLEN(1);
1718789Sahrens 		} else if (offset == 1) {
1719789Sahrens 			(void) strcpy(zap.za_name, "..");
1720789Sahrens 			zap.za_first_integer = zp->z_phys->zp_parent;
1721789Sahrens 			this_reclen = DIRENT64_RECLEN(2);
1722789Sahrens 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
1723789Sahrens 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
1724789Sahrens 			zap.za_first_integer = ZFSCTL_INO_ROOT;
1725789Sahrens 			this_reclen =
1726789Sahrens 			    DIRENT64_RECLEN(sizeof (ZFS_CTLDIR_NAME) - 1);
1727789Sahrens 		} else {
1728789Sahrens 			/*
1729789Sahrens 			 * Grab next entry.
1730789Sahrens 			 */
1731789Sahrens 			if (error = zap_cursor_retrieve(&zc, &zap)) {
1732789Sahrens 				if ((*eofp = (error == ENOENT)) != 0)
1733789Sahrens 					break;
1734789Sahrens 				else
1735789Sahrens 					goto update;
1736789Sahrens 			}
1737789Sahrens 
1738789Sahrens 			if (zap.za_integer_length != 8 ||
1739789Sahrens 			    zap.za_num_integers != 1) {
1740789Sahrens 				cmn_err(CE_WARN, "zap_readdir: bad directory "
1741789Sahrens 				    "entry, obj = %lld, offset = %lld\n",
1742789Sahrens 				    (u_longlong_t)zp->z_id,
1743789Sahrens 				    (u_longlong_t)offset);
1744789Sahrens 				error = ENXIO;
1745789Sahrens 				goto update;
1746789Sahrens 			}
1747789Sahrens 			this_reclen = DIRENT64_RECLEN(strlen(zap.za_name));
1748789Sahrens 		}
1749789Sahrens 
1750789Sahrens 		/*
1751789Sahrens 		 * Will this entry fit in the buffer?
1752789Sahrens 		 */
1753789Sahrens 		if (outcount + this_reclen > bufsize) {
1754789Sahrens 			/*
1755789Sahrens 			 * Did we manage to fit anything in the buffer?
1756789Sahrens 			 */
1757789Sahrens 			if (!outcount) {
1758789Sahrens 				error = EINVAL;
1759789Sahrens 				goto update;
1760789Sahrens 			}
1761789Sahrens 			break;
1762789Sahrens 		}
1763789Sahrens 		/*
1764789Sahrens 		 * Add this entry:
1765789Sahrens 		 */
1766789Sahrens 		odp->d_ino = (ino64_t)zap.za_first_integer;
1767789Sahrens 		odp->d_reclen = (ushort_t)this_reclen;
1768789Sahrens 		/* NOTE: d_off is the offset for the *next* entry */
1769789Sahrens 		next = &(odp->d_off);
1770789Sahrens 		(void) strncpy(odp->d_name, zap.za_name,
1771789Sahrens 		    DIRENT64_NAMELEN(this_reclen));
1772789Sahrens 		outcount += this_reclen;
1773789Sahrens 		odp = (dirent64_t *)((intptr_t)odp + this_reclen);
1774789Sahrens 
1775789Sahrens 		ASSERT(outcount <= bufsize);
1776789Sahrens 
1777789Sahrens 		/* Prefetch znode */
1778869Sperrin 		if (prefetch)
1779869Sperrin 			dmu_prefetch(os, zap.za_first_integer, 0, 0);
1780789Sahrens 
1781789Sahrens 		/*
1782789Sahrens 		 * Move to the next entry, fill in the previous offset.
1783789Sahrens 		 */
1784789Sahrens 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
1785789Sahrens 			zap_cursor_advance(&zc);
1786789Sahrens 			offset = zap_cursor_serialize(&zc);
1787789Sahrens 		} else {
1788789Sahrens 			offset += 1;
1789789Sahrens 		}
1790789Sahrens 		*next = offset;
1791789Sahrens 	}
1792869Sperrin 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
1793789Sahrens 
1794789Sahrens 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
1795789Sahrens 		iovp->iov_base += outcount;
1796789Sahrens 		iovp->iov_len -= outcount;
1797789Sahrens 		uio->uio_resid -= outcount;
1798789Sahrens 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
1799789Sahrens 		/*
1800789Sahrens 		 * Reset the pointer.
1801789Sahrens 		 */
1802789Sahrens 		offset = uio->uio_loffset;
1803789Sahrens 	}
1804789Sahrens 
1805789Sahrens update:
1806885Sahrens 	zap_cursor_fini(&zc);
1807789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1808789Sahrens 		kmem_free(outbuf, bufsize);
1809789Sahrens 
1810789Sahrens 	if (error == ENOENT)
1811789Sahrens 		error = 0;
1812789Sahrens 
1813789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
1814789Sahrens 
1815789Sahrens 	uio->uio_loffset = offset;
1816789Sahrens 	ZFS_EXIT(zfsvfs);
1817789Sahrens 	return (error);
1818789Sahrens }
1819789Sahrens 
1820789Sahrens static int
1821789Sahrens zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr)
1822789Sahrens {
1823789Sahrens 	znode_t	*zp = VTOZ(vp);
1824789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1825789Sahrens 
18261773Seschrock 	/*
18271773Seschrock 	 * Regardless of whether this is required for standards conformance,
18281773Seschrock 	 * this is the logical behavior when fsync() is called on a file with
18291773Seschrock 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
18301773Seschrock 	 * going to be pushed out as part of the zil_commit().
18311773Seschrock 	 */
18321773Seschrock 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
18331773Seschrock 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
18341773Seschrock 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr);
18351773Seschrock 
1836789Sahrens 	ZFS_ENTER(zfsvfs);
1837789Sahrens 	zil_commit(zfsvfs->z_log, zp->z_last_itx, FSYNC);
1838789Sahrens 	ZFS_EXIT(zfsvfs);
1839789Sahrens 	return (0);
1840789Sahrens }
1841789Sahrens 
1842789Sahrens /*
1843789Sahrens  * Get the requested file attributes and place them in the provided
1844789Sahrens  * vattr structure.
1845789Sahrens  *
1846789Sahrens  *	IN:	vp	- vnode of file.
1847789Sahrens  *		vap	- va_mask identifies requested attributes.
1848789Sahrens  *		flags	- [UNUSED]
1849789Sahrens  *		cr	- credentials of caller.
1850789Sahrens  *
1851789Sahrens  *	OUT:	vap	- attribute values.
1852789Sahrens  *
1853789Sahrens  *	RETURN:	0 (always succeeds)
1854789Sahrens  */
1855789Sahrens /* ARGSUSED */
1856789Sahrens static int
1857789Sahrens zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
1858789Sahrens {
1859789Sahrens 	znode_t *zp = VTOZ(vp);
1860789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1861789Sahrens 	znode_phys_t *pzp = zp->z_phys;
1862789Sahrens 	int	error;
1863789Sahrens 
1864789Sahrens 	ZFS_ENTER(zfsvfs);
1865789Sahrens 
1866789Sahrens 	/*
1867789Sahrens 	 * Return all attributes.  It's cheaper to provide the answer
1868789Sahrens 	 * than to determine whether we were asked the question.
1869789Sahrens 	 */
1870789Sahrens 	mutex_enter(&zp->z_lock);
1871789Sahrens 
1872789Sahrens 	vap->va_type = vp->v_type;
1873789Sahrens 	vap->va_mode = pzp->zp_mode & MODEMASK;
1874789Sahrens 	vap->va_uid = zp->z_phys->zp_uid;
1875789Sahrens 	vap->va_gid = zp->z_phys->zp_gid;
1876789Sahrens 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
1877789Sahrens 	vap->va_nodeid = zp->z_id;
1878789Sahrens 	vap->va_nlink = MIN(pzp->zp_links, UINT32_MAX);	/* nlink_t limit! */
1879789Sahrens 	vap->va_size = pzp->zp_size;
18801816Smarks 	vap->va_rdev = vp->v_rdev;
1881789Sahrens 	vap->va_seq = zp->z_seq;
1882789Sahrens 
1883789Sahrens 	ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
1884789Sahrens 	ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
1885789Sahrens 	ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
1886789Sahrens 
1887789Sahrens 	/*
1888905Smarks 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
1889905Smarks 	 * Also, if we are the owner don't bother, since owner should
1890905Smarks 	 * always be allowed to read basic attributes of file.
1891789Sahrens 	 */
1892905Smarks 	if (!(zp->z_phys->zp_flags & ZFS_ACL_TRIVIAL) &&
1893905Smarks 	    (zp->z_phys->zp_uid != crgetuid(cr))) {
1894905Smarks 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, cr)) {
1895789Sahrens 			mutex_exit(&zp->z_lock);
1896789Sahrens 			ZFS_EXIT(zfsvfs);
1897789Sahrens 			return (error);
1898789Sahrens 		}
1899789Sahrens 	}
1900789Sahrens 
1901789Sahrens 	mutex_exit(&zp->z_lock);
1902789Sahrens 
1903789Sahrens 	dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks);
1904789Sahrens 
1905789Sahrens 	if (zp->z_blksz == 0) {
1906789Sahrens 		/*
1907789Sahrens 		 * Block size hasn't been set; suggest maximal I/O transfers.
1908789Sahrens 		 */
1909789Sahrens 		vap->va_blksize = zfsvfs->z_max_blksz;
1910789Sahrens 	}
1911789Sahrens 
1912789Sahrens 	ZFS_EXIT(zfsvfs);
1913789Sahrens 	return (0);
1914789Sahrens }
1915789Sahrens 
1916789Sahrens /*
1917789Sahrens  * Set the file attributes to the values contained in the
1918789Sahrens  * vattr structure.
1919789Sahrens  *
1920789Sahrens  *	IN:	vp	- vnode of file to be modified.
1921789Sahrens  *		vap	- new attribute values.
1922789Sahrens  *		flags	- ATTR_UTIME set if non-default time values provided.
1923789Sahrens  *		cr	- credentials of caller.
1924789Sahrens  *
1925789Sahrens  *	RETURN:	0 if success
1926789Sahrens  *		error code if failure
1927789Sahrens  *
1928789Sahrens  * Timestamps:
1929789Sahrens  *	vp - ctime updated, mtime updated if size changed.
1930789Sahrens  */
1931789Sahrens /* ARGSUSED */
1932789Sahrens static int
1933789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1934789Sahrens 	caller_context_t *ct)
1935789Sahrens {
1936789Sahrens 	struct znode	*zp = VTOZ(vp);
1937789Sahrens 	znode_phys_t	*pzp = zp->z_phys;
1938789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1939789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1940789Sahrens 	uint64_t	seq = 0;
1941789Sahrens 	dmu_tx_t	*tx;
19421878Smaybee 	vattr_t		oldva;
1943789Sahrens 	uint_t		mask = vap->va_mask;
19441878Smaybee 	uint_t		saved_mask;
19451115Smarks 	int		trim_mask = FALSE;
1946789Sahrens 	uint64_t	new_mode;
19471231Smarks 	znode_t		*attrzp;
1948789Sahrens 	int		need_policy = FALSE;
1949789Sahrens 	int		err;
1950789Sahrens 
1951789Sahrens 	if (mask == 0)
1952789Sahrens 		return (0);
1953789Sahrens 
1954789Sahrens 	if (mask & AT_NOSET)
1955789Sahrens 		return (EINVAL);
1956789Sahrens 
1957789Sahrens 	if (mask & AT_SIZE && vp->v_type == VDIR)
1958789Sahrens 		return (EISDIR);
1959789Sahrens 
19601394Smarks 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO)
19611308Smarks 		return (EINVAL);
19621308Smarks 
1963789Sahrens 	ZFS_ENTER(zfsvfs);
1964789Sahrens 
1965789Sahrens top:
19661231Smarks 	attrzp = NULL;
1967789Sahrens 
1968789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
1969789Sahrens 		ZFS_EXIT(zfsvfs);
1970789Sahrens 		return (EROFS);
1971789Sahrens 	}
1972789Sahrens 
1973789Sahrens 	/*
1974789Sahrens 	 * First validate permissions
1975789Sahrens 	 */
1976789Sahrens 
1977789Sahrens 	if (mask & AT_SIZE) {
1978789Sahrens 		err = zfs_zaccess(zp, ACE_WRITE_DATA, cr);
1979789Sahrens 		if (err) {
1980789Sahrens 			ZFS_EXIT(zfsvfs);
1981789Sahrens 			return (err);
1982789Sahrens 		}
19831878Smaybee 		/*
19841878Smaybee 		 * XXX - Note, we are not providing any open
19851878Smaybee 		 * mode flags here (like FNDELAY), so we may
19861878Smaybee 		 * block if there are locks present... this
19871878Smaybee 		 * should be addressed in openat().
19881878Smaybee 		 */
19891878Smaybee 		do {
19901878Smaybee 			err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
19912113Sahrens 			/* NB: we already did dmu_tx_wait() if necessary */
19921878Smaybee 		} while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
19931878Smaybee 		if (err) {
19941878Smaybee 			ZFS_EXIT(zfsvfs);
19951878Smaybee 			return (err);
19961878Smaybee 		}
1997789Sahrens 	}
1998789Sahrens 
1999789Sahrens 	if (mask & (AT_ATIME|AT_MTIME))
2000789Sahrens 		need_policy = zfs_zaccess_v4_perm(zp, ACE_WRITE_ATTRIBUTES, cr);
2001789Sahrens 
2002789Sahrens 	if (mask & (AT_UID|AT_GID)) {
2003789Sahrens 		int	idmask = (mask & (AT_UID|AT_GID));
2004789Sahrens 		int	take_owner;
2005789Sahrens 		int	take_group;
2006789Sahrens 
2007789Sahrens 		/*
2008913Smarks 		 * NOTE: even if a new mode is being set,
2009913Smarks 		 * we may clear S_ISUID/S_ISGID bits.
2010913Smarks 		 */
2011913Smarks 
2012913Smarks 		if (!(mask & AT_MODE))
2013913Smarks 			vap->va_mode = pzp->zp_mode;
2014913Smarks 
2015913Smarks 		/*
2016789Sahrens 		 * Take ownership or chgrp to group we are a member of
2017789Sahrens 		 */
2018789Sahrens 
2019789Sahrens 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2020789Sahrens 		take_group = (mask & AT_GID) && groupmember(vap->va_gid, cr);
2021789Sahrens 
2022789Sahrens 		/*
2023789Sahrens 		 * If both AT_UID and AT_GID are set then take_owner and
2024789Sahrens 		 * take_group must both be set in order to allow taking
2025789Sahrens 		 * ownership.
2026789Sahrens 		 *
2027789Sahrens 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2028789Sahrens 		 *
2029789Sahrens 		 */
2030789Sahrens 
2031789Sahrens 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2032789Sahrens 		    ((idmask == AT_UID) && take_owner) ||
2033789Sahrens 		    ((idmask == AT_GID) && take_group)) {
2034789Sahrens 			if (zfs_zaccess_v4_perm(zp, ACE_WRITE_OWNER, cr) == 0) {
2035789Sahrens 				/*
2036789Sahrens 				 * Remove setuid/setgid for non-privileged users
2037789Sahrens 				 */
20381115Smarks 				secpolicy_setid_clear(vap, cr);
20391115Smarks 				trim_mask = TRUE;
20401115Smarks 				saved_mask = vap->va_mask;
2041789Sahrens 			} else {
2042789Sahrens 				need_policy =  TRUE;
2043789Sahrens 			}
2044789Sahrens 		} else {
2045789Sahrens 			need_policy =  TRUE;
2046789Sahrens 		}
2047789Sahrens 	}
2048789Sahrens 
2049789Sahrens 	if (mask & AT_MODE)
2050789Sahrens 		need_policy = TRUE;
2051789Sahrens 
2052789Sahrens 	if (need_policy) {
2053789Sahrens 		mutex_enter(&zp->z_lock);
2054789Sahrens 		oldva.va_mode = pzp->zp_mode;
2055789Sahrens 		oldva.va_uid = zp->z_phys->zp_uid;
2056789Sahrens 		oldva.va_gid = zp->z_phys->zp_gid;
2057789Sahrens 		mutex_exit(&zp->z_lock);
20581115Smarks 
20591115Smarks 		/*
20601115Smarks 		 * If trim_mask is set then take ownership
20611115Smarks 		 * has been granted.  In that case remove
20621115Smarks 		 * UID|GID from mask so that
20631115Smarks 		 * secpolicy_vnode_setattr() doesn't revoke it.
20641115Smarks 		 */
20651115Smarks 		if (trim_mask)
20661115Smarks 			vap->va_mask &= ~(AT_UID|AT_GID);
20671115Smarks 
2068789Sahrens 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2069789Sahrens 		    (int (*)(void *, int, cred_t *))zfs_zaccess_rwx, zp);
2070789Sahrens 		if (err) {
2071789Sahrens 			ZFS_EXIT(zfsvfs);
2072789Sahrens 			return (err);
2073789Sahrens 		}
20741115Smarks 
20751115Smarks 		if (trim_mask)
20761115Smarks 			vap->va_mask |= (saved_mask & (AT_UID|AT_GID));
2077789Sahrens 	}
2078789Sahrens 
2079789Sahrens 	/*
2080789Sahrens 	 * secpolicy_vnode_setattr, or take ownership may have
2081789Sahrens 	 * changed va_mask
2082789Sahrens 	 */
2083789Sahrens 	mask = vap->va_mask;
2084789Sahrens 
2085789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2086789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
2087789Sahrens 
2088789Sahrens 	if (mask & AT_MODE) {
20891576Smarks 		uint64_t pmode = pzp->zp_mode;
20901576Smarks 
20911576Smarks 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2092789Sahrens 
2093789Sahrens 		if (zp->z_phys->zp_acl.z_acl_extern_obj)
2094789Sahrens 			dmu_tx_hold_write(tx,
2095789Sahrens 			    pzp->zp_acl.z_acl_extern_obj, 0, SPA_MAXBLOCKSIZE);
2096789Sahrens 		else
2097789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2098789Sahrens 			    0, ZFS_ACL_SIZE(MAX_ACL_SIZE));
2099789Sahrens 	}
2100789Sahrens 
21011231Smarks 	if ((mask & (AT_UID | AT_GID)) && zp->z_phys->zp_xattr != 0) {
21021231Smarks 		err = zfs_zget(zp->z_zfsvfs, zp->z_phys->zp_xattr, &attrzp);
21031231Smarks 		if (err) {
21041231Smarks 			dmu_tx_abort(tx);
21051231Smarks 			ZFS_EXIT(zfsvfs);
21061231Smarks 			return (err);
21071231Smarks 		}
21081231Smarks 		dmu_tx_hold_bonus(tx, attrzp->z_id);
21091231Smarks 	}
21101231Smarks 
2111789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2112789Sahrens 	if (err) {
21131231Smarks 		if (attrzp)
21141231Smarks 			VN_RELE(ZTOV(attrzp));
2115789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
21162113Sahrens 			dmu_tx_wait(tx);
21172113Sahrens 			dmu_tx_abort(tx);
2118789Sahrens 			goto top;
2119789Sahrens 		}
21202113Sahrens 		dmu_tx_abort(tx);
2121789Sahrens 		ZFS_EXIT(zfsvfs);
2122789Sahrens 		return (err);
2123789Sahrens 	}
2124789Sahrens 
2125789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
2126789Sahrens 
2127789Sahrens 	/*
2128789Sahrens 	 * Set each attribute requested.
2129789Sahrens 	 * We group settings according to the locks they need to acquire.
2130789Sahrens 	 *
2131789Sahrens 	 * Note: you cannot set ctime directly, although it will be
2132789Sahrens 	 * updated as a side-effect of calling this function.
2133789Sahrens 	 */
2134789Sahrens 
2135789Sahrens 	mutex_enter(&zp->z_lock);
2136789Sahrens 
2137789Sahrens 	if (mask & AT_MODE) {
2138789Sahrens 		err = zfs_acl_chmod_setattr(zp, new_mode, tx);
2139789Sahrens 		ASSERT3U(err, ==, 0);
2140789Sahrens 	}
2141789Sahrens 
21421231Smarks 	if (attrzp)
21431231Smarks 		mutex_enter(&attrzp->z_lock);
21441231Smarks 
21451231Smarks 	if (mask & AT_UID) {
2146789Sahrens 		zp->z_phys->zp_uid = (uint64_t)vap->va_uid;
21471231Smarks 		if (attrzp) {
21481231Smarks 			attrzp->z_phys->zp_uid = (uint64_t)vap->va_uid;
21491231Smarks 		}
21501231Smarks 	}
21511231Smarks 
21521231Smarks 	if (mask & AT_GID) {
2153789Sahrens 		zp->z_phys->zp_gid = (uint64_t)vap->va_gid;
21541231Smarks 		if (attrzp)
21551231Smarks 			attrzp->z_phys->zp_gid = (uint64_t)vap->va_gid;
21561231Smarks 	}
21571231Smarks 
21581231Smarks 	if (attrzp)
21591231Smarks 		mutex_exit(&attrzp->z_lock);
2160789Sahrens 
2161789Sahrens 	if (mask & AT_ATIME)
2162789Sahrens 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
2163789Sahrens 
2164789Sahrens 	if (mask & AT_MTIME)
2165789Sahrens 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
2166789Sahrens 
21671878Smaybee 	if (mask & AT_SIZE)
2168789Sahrens 		zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
21691878Smaybee 	else if (mask != 0)
2170789Sahrens 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
2171789Sahrens 
21721878Smaybee 	if (mask != 0)
21731878Smaybee 		seq = zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask);
2174789Sahrens 
2175789Sahrens 	mutex_exit(&zp->z_lock);
2176789Sahrens 
21771231Smarks 	if (attrzp)
21781231Smarks 		VN_RELE(ZTOV(attrzp));
21791231Smarks 
2180789Sahrens 	dmu_tx_commit(tx);
2181789Sahrens 
2182789Sahrens 	zil_commit(zilog, seq, 0);
2183789Sahrens 
2184789Sahrens 	ZFS_EXIT(zfsvfs);
2185789Sahrens 	return (err);
2186789Sahrens }
2187789Sahrens 
2188789Sahrens /*
2189789Sahrens  * Search back through the directory tree, using the ".." entries.
2190789Sahrens  * Lock each directory in the chain to prevent concurrent renames.
2191789Sahrens  * Fail any attempt to move a directory into one of its own descendants.
2192789Sahrens  * XXX - z_parent_lock can overlap with map or grow locks
2193789Sahrens  */
2194789Sahrens typedef struct zfs_zlock {
2195789Sahrens 	krwlock_t	*zl_rwlock;	/* lock we acquired */
2196789Sahrens 	znode_t		*zl_znode;	/* znode we held */
2197789Sahrens 	struct zfs_zlock *zl_next;	/* next in list */
2198789Sahrens } zfs_zlock_t;
2199789Sahrens 
2200789Sahrens static int
2201789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
2202789Sahrens {
2203789Sahrens 	zfs_zlock_t	*zl;
2204789Sahrens 	znode_t 	*zp = tdzp;
2205789Sahrens 	uint64_t	rootid = zp->z_zfsvfs->z_root;
2206789Sahrens 	uint64_t	*oidp = &zp->z_id;
2207789Sahrens 	krwlock_t	*rwlp = &szp->z_parent_lock;
2208789Sahrens 	krw_t		rw = RW_WRITER;
2209789Sahrens 
2210789Sahrens 	/*
2211789Sahrens 	 * First pass write-locks szp and compares to zp->z_id.
2212789Sahrens 	 * Later passes read-lock zp and compare to zp->z_parent.
2213789Sahrens 	 */
2214789Sahrens 	do {
2215789Sahrens 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
2216789Sahrens 		zl->zl_rwlock = rwlp;
2217789Sahrens 		zl->zl_znode = NULL;
2218789Sahrens 		zl->zl_next = *zlpp;
2219789Sahrens 		*zlpp = zl;
2220789Sahrens 
2221789Sahrens 		rw_enter(rwlp, rw);
2222789Sahrens 
2223789Sahrens 		if (*oidp == szp->z_id)		/* We're a descendant of szp */
2224789Sahrens 			return (EINVAL);
2225789Sahrens 
2226789Sahrens 		if (*oidp == rootid)		/* We've hit the top */
2227789Sahrens 			return (0);
2228789Sahrens 
2229789Sahrens 		if (rw == RW_READER) {		/* i.e. not the first pass */
2230789Sahrens 			int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
2231789Sahrens 			if (error)
2232789Sahrens 				return (error);
2233789Sahrens 			zl->zl_znode = zp;
2234789Sahrens 		}
2235789Sahrens 		oidp = &zp->z_phys->zp_parent;
2236789Sahrens 		rwlp = &zp->z_parent_lock;
2237789Sahrens 		rw = RW_READER;
2238789Sahrens 
2239789Sahrens 	} while (zp->z_id != sdzp->z_id);
2240789Sahrens 
2241789Sahrens 	return (0);
2242789Sahrens }
2243789Sahrens 
2244789Sahrens /*
2245789Sahrens  * Drop locks and release vnodes that were held by zfs_rename_lock().
2246789Sahrens  */
2247789Sahrens static void
2248789Sahrens zfs_rename_unlock(zfs_zlock_t **zlpp)
2249789Sahrens {
2250789Sahrens 	zfs_zlock_t *zl;
2251789Sahrens 
2252789Sahrens 	while ((zl = *zlpp) != NULL) {
2253789Sahrens 		if (zl->zl_znode != NULL)
2254789Sahrens 			VN_RELE(ZTOV(zl->zl_znode));
2255789Sahrens 		rw_exit(zl->zl_rwlock);
2256789Sahrens 		*zlpp = zl->zl_next;
2257789Sahrens 		kmem_free(zl, sizeof (*zl));
2258789Sahrens 	}
2259789Sahrens }
2260789Sahrens 
2261789Sahrens /*
2262789Sahrens  * Move an entry from the provided source directory to the target
2263789Sahrens  * directory.  Change the entry name as indicated.
2264789Sahrens  *
2265789Sahrens  *	IN:	sdvp	- Source directory containing the "old entry".
2266789Sahrens  *		snm	- Old entry name.
2267789Sahrens  *		tdvp	- Target directory to contain the "new entry".
2268789Sahrens  *		tnm	- New entry name.
2269789Sahrens  *		cr	- credentials of caller.
2270789Sahrens  *
2271789Sahrens  *	RETURN:	0 if success
2272789Sahrens  *		error code if failure
2273789Sahrens  *
2274789Sahrens  * Timestamps:
2275789Sahrens  *	sdvp,tdvp - ctime|mtime updated
2276789Sahrens  */
2277789Sahrens static int
2278789Sahrens zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr)
2279789Sahrens {
2280789Sahrens 	znode_t		*tdzp, *szp, *tzp;
2281789Sahrens 	znode_t		*sdzp = VTOZ(sdvp);
2282789Sahrens 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
2283789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2284789Sahrens 	uint64_t	seq = 0;
2285789Sahrens 	vnode_t		*realvp;
2286789Sahrens 	zfs_dirlock_t	*sdl, *tdl;
2287789Sahrens 	dmu_tx_t	*tx;
2288789Sahrens 	zfs_zlock_t	*zl;
2289789Sahrens 	int		cmp, serr, terr, error;
2290789Sahrens 
2291789Sahrens 	ZFS_ENTER(zfsvfs);
2292789Sahrens 
2293789Sahrens 	/*
2294789Sahrens 	 * Make sure we have the real vp for the target directory.
2295789Sahrens 	 */
2296789Sahrens 	if (VOP_REALVP(tdvp, &realvp) == 0)
2297789Sahrens 		tdvp = realvp;
2298789Sahrens 
2299789Sahrens 	if (tdvp->v_vfsp != sdvp->v_vfsp) {
2300789Sahrens 		ZFS_EXIT(zfsvfs);
2301789Sahrens 		return (EXDEV);
2302789Sahrens 	}
2303789Sahrens 
2304789Sahrens 	tdzp = VTOZ(tdvp);
2305789Sahrens top:
2306789Sahrens 	szp = NULL;
2307789Sahrens 	tzp = NULL;
2308789Sahrens 	zl = NULL;
2309789Sahrens 
2310789Sahrens 	/*
2311789Sahrens 	 * This is to prevent the creation of links into attribute space
2312789Sahrens 	 * by renaming a linked file into/outof an attribute directory.
2313789Sahrens 	 * See the comment in zfs_link() for why this is considered bad.
2314789Sahrens 	 */
2315789Sahrens 	if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
2316789Sahrens 	    (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
2317789Sahrens 		ZFS_EXIT(zfsvfs);
2318789Sahrens 		return (EINVAL);
2319789Sahrens 	}
2320789Sahrens 
2321789Sahrens 	/*
2322789Sahrens 	 * Lock source and target directory entries.  To prevent deadlock,
2323789Sahrens 	 * a lock ordering must be defined.  We lock the directory with
2324789Sahrens 	 * the smallest object id first, or if it's a tie, the one with
2325789Sahrens 	 * the lexically first name.
2326789Sahrens 	 */
2327789Sahrens 	if (sdzp->z_id < tdzp->z_id) {
2328789Sahrens 		cmp = -1;
2329789Sahrens 	} else if (sdzp->z_id > tdzp->z_id) {
2330789Sahrens 		cmp = 1;
2331789Sahrens 	} else {
2332789Sahrens 		cmp = strcmp(snm, tnm);
2333789Sahrens 		if (cmp == 0) {
2334789Sahrens 			/*
2335789Sahrens 			 * POSIX: "If the old argument and the new argument
2336789Sahrens 			 * both refer to links to the same existing file,
2337789Sahrens 			 * the rename() function shall return successfully
2338789Sahrens 			 * and perform no other action."
2339789Sahrens 			 */
2340789Sahrens 			ZFS_EXIT(zfsvfs);
2341789Sahrens 			return (0);
2342789Sahrens 		}
2343789Sahrens 	}
2344789Sahrens 	if (cmp < 0) {
2345789Sahrens 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
2346789Sahrens 		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
2347789Sahrens 	} else {
2348789Sahrens 		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
2349789Sahrens 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
2350789Sahrens 	}
2351789Sahrens 
2352789Sahrens 	if (serr) {
2353789Sahrens 		/*
2354789Sahrens 		 * Source entry invalid or not there.
2355789Sahrens 		 */
2356789Sahrens 		if (!terr) {
2357789Sahrens 			zfs_dirent_unlock(tdl);
2358789Sahrens 			if (tzp)
2359789Sahrens 				VN_RELE(ZTOV(tzp));
2360789Sahrens 		}
2361789Sahrens 		if (strcmp(snm, "..") == 0)
2362789Sahrens 			serr = EINVAL;
2363789Sahrens 		ZFS_EXIT(zfsvfs);
2364789Sahrens 		return (serr);
2365789Sahrens 	}
2366789Sahrens 	if (terr) {
2367789Sahrens 		zfs_dirent_unlock(sdl);
2368789Sahrens 		VN_RELE(ZTOV(szp));
2369789Sahrens 		if (strcmp(tnm, "..") == 0)
2370789Sahrens 			terr = EINVAL;
2371789Sahrens 		ZFS_EXIT(zfsvfs);
2372789Sahrens 		return (terr);
2373789Sahrens 	}
2374789Sahrens 
2375789Sahrens 	/*
2376789Sahrens 	 * Must have write access at the source to remove the old entry
2377789Sahrens 	 * and write access at the target to create the new entry.
2378789Sahrens 	 * Note that if target and source are the same, this can be
2379789Sahrens 	 * done in a single check.
2380789Sahrens 	 */
2381789Sahrens 
2382789Sahrens 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
2383789Sahrens 		goto out;
2384789Sahrens 
2385789Sahrens 	if (ZTOV(szp)->v_type == VDIR) {
2386789Sahrens 		/*
2387789Sahrens 		 * Check to make sure rename is valid.
2388789Sahrens 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
2389789Sahrens 		 */
2390789Sahrens 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
2391789Sahrens 			goto out;
2392789Sahrens 	}
2393789Sahrens 
2394789Sahrens 	/*
2395789Sahrens 	 * Does target exist?
2396789Sahrens 	 */
2397789Sahrens 	if (tzp) {
2398789Sahrens 		/*
2399789Sahrens 		 * Source and target must be the same type.
2400789Sahrens 		 */
2401789Sahrens 		if (ZTOV(szp)->v_type == VDIR) {
2402789Sahrens 			if (ZTOV(tzp)->v_type != VDIR) {
2403789Sahrens 				error = ENOTDIR;
2404789Sahrens 				goto out;
2405789Sahrens 			}
2406789Sahrens 		} else {
2407789Sahrens 			if (ZTOV(tzp)->v_type == VDIR) {
2408789Sahrens 				error = EISDIR;
2409789Sahrens 				goto out;
2410789Sahrens 			}
2411789Sahrens 		}
2412789Sahrens 		/*
2413789Sahrens 		 * POSIX dictates that when the source and target
2414789Sahrens 		 * entries refer to the same file object, rename
2415789Sahrens 		 * must do nothing and exit without error.
2416789Sahrens 		 */
2417789Sahrens 		if (szp->z_id == tzp->z_id) {
2418789Sahrens 			error = 0;
2419789Sahrens 			goto out;
2420789Sahrens 		}
2421789Sahrens 	}
2422789Sahrens 
2423789Sahrens 	vnevent_rename_src(ZTOV(szp));
2424789Sahrens 	if (tzp)
2425789Sahrens 		vnevent_rename_dest(ZTOV(tzp));
2426789Sahrens 
2427789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2428789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);	/* nlink changes */
2429789Sahrens 	dmu_tx_hold_bonus(tx, sdzp->z_id);	/* nlink changes */
24301544Seschrock 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
24311544Seschrock 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
24321544Seschrock 	if (sdzp != tdzp)
2433789Sahrens 		dmu_tx_hold_bonus(tx, tdzp->z_id);	/* nlink changes */
24341544Seschrock 	if (tzp)
24351544Seschrock 		dmu_tx_hold_bonus(tx, tzp->z_id);	/* parent changes */
24361544Seschrock 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
2437789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2438789Sahrens 	if (error) {
2439789Sahrens 		if (zl != NULL)
2440789Sahrens 			zfs_rename_unlock(&zl);
2441789Sahrens 		zfs_dirent_unlock(sdl);
2442789Sahrens 		zfs_dirent_unlock(tdl);
2443789Sahrens 		VN_RELE(ZTOV(szp));
2444789Sahrens 		if (tzp)
2445789Sahrens 			VN_RELE(ZTOV(tzp));
2446789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
24472113Sahrens 			dmu_tx_wait(tx);
24482113Sahrens 			dmu_tx_abort(tx);
2449789Sahrens 			goto top;
2450789Sahrens 		}
24512113Sahrens 		dmu_tx_abort(tx);
2452789Sahrens 		ZFS_EXIT(zfsvfs);
2453789Sahrens 		return (error);
2454789Sahrens 	}
2455789Sahrens 
2456789Sahrens 	if (tzp)	/* Attempt to remove the existing target */
2457789Sahrens 		error = zfs_link_destroy(tdl, tzp, tx, 0, NULL);
2458789Sahrens 
2459789Sahrens 	if (error == 0) {
2460789Sahrens 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
2461789Sahrens 		if (error == 0) {
2462789Sahrens 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
2463789Sahrens 			ASSERT(error == 0);
2464789Sahrens 			seq = zfs_log_rename(zilog, tx, TX_RENAME,
2465789Sahrens 			    sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
2466789Sahrens 		}
2467789Sahrens 	}
2468789Sahrens 
2469789Sahrens 	dmu_tx_commit(tx);
2470789Sahrens out:
2471789Sahrens 	if (zl != NULL)
2472789Sahrens 		zfs_rename_unlock(&zl);
2473789Sahrens 
2474789Sahrens 	zfs_dirent_unlock(sdl);
2475789Sahrens 	zfs_dirent_unlock(tdl);
2476789Sahrens 
2477789Sahrens 	VN_RELE(ZTOV(szp));
2478789Sahrens 	if (tzp)
2479789Sahrens 		VN_RELE(ZTOV(tzp));
2480789Sahrens 
2481789Sahrens 	zil_commit(zilog, seq, 0);
2482789Sahrens 
2483789Sahrens 	ZFS_EXIT(zfsvfs);
2484789Sahrens 	return (error);
2485789Sahrens }
2486789Sahrens 
2487789Sahrens /*
2488789Sahrens  * Insert the indicated symbolic reference entry into the directory.
2489789Sahrens  *
2490789Sahrens  *	IN:	dvp	- Directory to contain new symbolic link.
2491789Sahrens  *		link	- Name for new symlink entry.
2492789Sahrens  *		vap	- Attributes of new entry.
2493789Sahrens  *		target	- Target path of new symlink.
2494789Sahrens  *		cr	- credentials of caller.
2495789Sahrens  *
2496789Sahrens  *	RETURN:	0 if success
2497789Sahrens  *		error code if failure
2498789Sahrens  *
2499789Sahrens  * Timestamps:
2500789Sahrens  *	dvp - ctime|mtime updated
2501789Sahrens  */
2502789Sahrens static int
2503789Sahrens zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr)
2504789Sahrens {
2505789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
2506789Sahrens 	zfs_dirlock_t	*dl;
2507789Sahrens 	dmu_tx_t	*tx;
2508789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2509789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2510789Sahrens 	uint64_t	seq = 0;
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)
2594789Sahrens 		seq = zfs_log_symlink(zilog, tx, TX_SYMLINK,
2595789Sahrens 		    dzp, zp, name, link);
2596789Sahrens 
2597789Sahrens 	dmu_tx_commit(tx);
2598789Sahrens 
2599789Sahrens 	zfs_dirent_unlock(dl);
2600789Sahrens 
2601789Sahrens 	VN_RELE(ZTOV(zp));
2602789Sahrens 
2603789Sahrens 	zil_commit(zilog, seq, 0);
2604789Sahrens 
2605789Sahrens 	ZFS_EXIT(zfsvfs);
2606789Sahrens 	return (error);
2607789Sahrens }
2608789Sahrens 
2609789Sahrens /*
2610789Sahrens  * Return, in the buffer contained in the provided uio structure,
2611789Sahrens  * the symbolic path referred to by vp.
2612789Sahrens  *
2613789Sahrens  *	IN:	vp	- vnode of symbolic link.
2614789Sahrens  *		uoip	- structure to contain the link path.
2615789Sahrens  *		cr	- credentials of caller.
2616789Sahrens  *
2617789Sahrens  *	OUT:	uio	- structure to contain the link path.
2618789Sahrens  *
2619789Sahrens  *	RETURN:	0 if success
2620789Sahrens  *		error code if failure
2621789Sahrens  *
2622789Sahrens  * Timestamps:
2623789Sahrens  *	vp - atime updated
2624789Sahrens  */
2625789Sahrens /* ARGSUSED */
2626789Sahrens static int
2627789Sahrens zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr)
2628789Sahrens {
2629789Sahrens 	znode_t		*zp = VTOZ(vp);
2630789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2631789Sahrens 	size_t		bufsz;
2632789Sahrens 	int		error;
2633789Sahrens 
2634789Sahrens 	ZFS_ENTER(zfsvfs);
2635789Sahrens 
2636789Sahrens 	bufsz = (size_t)zp->z_phys->zp_size;
2637789Sahrens 	if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
2638789Sahrens 		error = uiomove(zp->z_phys + 1,
2639789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
2640789Sahrens 	} else {
26411544Seschrock 		dmu_buf_t *dbp;
26421544Seschrock 		error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
26431544Seschrock 		if (error) {
2644789Sahrens 			ZFS_EXIT(zfsvfs);
2645789Sahrens 			return (error);
2646789Sahrens 		}
2647789Sahrens 		error = uiomove(dbp->db_data,
2648789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
26491544Seschrock 		dmu_buf_rele(dbp, FTAG);
2650789Sahrens 	}
2651789Sahrens 
2652789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2653789Sahrens 	ZFS_EXIT(zfsvfs);
2654789Sahrens 	return (error);
2655789Sahrens }
2656789Sahrens 
2657789Sahrens /*
2658789Sahrens  * Insert a new entry into directory tdvp referencing svp.
2659789Sahrens  *
2660789Sahrens  *	IN:	tdvp	- Directory to contain new entry.
2661789Sahrens  *		svp	- vnode of new entry.
2662789Sahrens  *		name	- name of new entry.
2663789Sahrens  *		cr	- credentials of caller.
2664789Sahrens  *
2665789Sahrens  *	RETURN:	0 if success
2666789Sahrens  *		error code if failure
2667789Sahrens  *
2668789Sahrens  * Timestamps:
2669789Sahrens  *	tdvp - ctime|mtime updated
2670789Sahrens  *	 svp - ctime updated
2671789Sahrens  */
2672789Sahrens /* ARGSUSED */
2673789Sahrens static int
2674789Sahrens zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr)
2675789Sahrens {
2676789Sahrens 	znode_t		*dzp = VTOZ(tdvp);
2677789Sahrens 	znode_t		*tzp, *szp;
2678789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2679789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2680789Sahrens 	uint64_t	seq = 0;
2681789Sahrens 	zfs_dirlock_t	*dl;
2682789Sahrens 	dmu_tx_t	*tx;
2683789Sahrens 	vnode_t		*realvp;
2684789Sahrens 	int		error;
2685789Sahrens 
2686789Sahrens 	ASSERT(tdvp->v_type == VDIR);
2687789Sahrens 
2688789Sahrens 	ZFS_ENTER(zfsvfs);
2689789Sahrens 
2690789Sahrens 	if (VOP_REALVP(svp, &realvp) == 0)
2691789Sahrens 		svp = realvp;
2692789Sahrens 
2693789Sahrens 	if (svp->v_vfsp != tdvp->v_vfsp) {
2694789Sahrens 		ZFS_EXIT(zfsvfs);
2695789Sahrens 		return (EXDEV);
2696789Sahrens 	}
2697789Sahrens 
2698789Sahrens 	szp = VTOZ(svp);
2699789Sahrens top:
2700789Sahrens 	/*
2701789Sahrens 	 * We do not support links between attributes and non-attributes
2702789Sahrens 	 * because of the potential security risk of creating links
2703789Sahrens 	 * into "normal" file space in order to circumvent restrictions
2704789Sahrens 	 * imposed in attribute space.
2705789Sahrens 	 */
2706789Sahrens 	if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
2707789Sahrens 	    (dzp->z_phys->zp_flags & ZFS_XATTR)) {
2708789Sahrens 		ZFS_EXIT(zfsvfs);
2709789Sahrens 		return (EINVAL);
2710789Sahrens 	}
2711789Sahrens 
2712789Sahrens 	/*
2713789Sahrens 	 * POSIX dictates that we return EPERM here.
2714789Sahrens 	 * Better choices include ENOTSUP or EISDIR.
2715789Sahrens 	 */
2716789Sahrens 	if (svp->v_type == VDIR) {
2717789Sahrens 		ZFS_EXIT(zfsvfs);
2718789Sahrens 		return (EPERM);
2719789Sahrens 	}
2720789Sahrens 
2721789Sahrens 	if ((uid_t)szp->z_phys->zp_uid != crgetuid(cr) &&
2722789Sahrens 	    secpolicy_basic_link(cr) != 0) {
2723789Sahrens 		ZFS_EXIT(zfsvfs);
2724789Sahrens 		return (EPERM);
2725789Sahrens 	}
2726789Sahrens 
2727789Sahrens 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
2728789Sahrens 		ZFS_EXIT(zfsvfs);
2729789Sahrens 		return (error);
2730789Sahrens 	}
2731789Sahrens 
2732789Sahrens 	/*
2733789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
2734789Sahrens 	 */
2735789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &tzp, ZNEW)) {
2736789Sahrens 		ZFS_EXIT(zfsvfs);
2737789Sahrens 		return (error);
2738789Sahrens 	}
2739789Sahrens 
2740789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2741789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);
27421544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
2743789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2744789Sahrens 	if (error) {
2745789Sahrens 		zfs_dirent_unlock(dl);
2746789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
27472113Sahrens 			dmu_tx_wait(tx);
27482113Sahrens 			dmu_tx_abort(tx);
2749789Sahrens 			goto top;
2750789Sahrens 		}
27512113Sahrens 		dmu_tx_abort(tx);
2752789Sahrens 		ZFS_EXIT(zfsvfs);
2753789Sahrens 		return (error);
2754789Sahrens 	}
2755789Sahrens 
2756789Sahrens 	error = zfs_link_create(dl, szp, tx, 0);
2757789Sahrens 
2758789Sahrens 	if (error == 0)
2759789Sahrens 		seq = zfs_log_link(zilog, tx, TX_LINK, dzp, szp, name);
2760789Sahrens 
2761789Sahrens 	dmu_tx_commit(tx);
2762789Sahrens 
2763789Sahrens 	zfs_dirent_unlock(dl);
2764789Sahrens 
2765789Sahrens 	zil_commit(zilog, seq, 0);
2766789Sahrens 
2767789Sahrens 	ZFS_EXIT(zfsvfs);
2768789Sahrens 	return (error);
2769789Sahrens }
2770789Sahrens 
2771789Sahrens /*
2772789Sahrens  * zfs_null_putapage() is used when the file system has been force
2773789Sahrens  * unmounted. It just drops the pages.
2774789Sahrens  */
2775789Sahrens /* ARGSUSED */
2776789Sahrens static int
2777789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
2778789Sahrens 		size_t *lenp, int flags, cred_t *cr)
2779789Sahrens {
2780789Sahrens 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
2781789Sahrens 	return (0);
2782789Sahrens }
2783789Sahrens 
2784789Sahrens /* ARGSUSED */
2785789Sahrens static int
2786789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
2787789Sahrens 		size_t *lenp, int flags, cred_t *cr)
2788789Sahrens {
2789789Sahrens 	znode_t		*zp = VTOZ(vp);
2790789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2791789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2792789Sahrens 	dmu_tx_t	*tx;
27931669Sperrin 	rl_t		*rl;
2794789Sahrens 	u_offset_t	off;
2795789Sahrens 	ssize_t		len;
2796789Sahrens 	caddr_t		va;
2797789Sahrens 	int		err;
2798789Sahrens 
2799789Sahrens top:
2800789Sahrens 	off = pp->p_offset;
28011669Sperrin 	rl = zfs_range_lock(zp, off, PAGESIZE, RL_WRITER);
28021819Smaybee 	/*
28031819Smaybee 	 * Can't push pages past end-of-file.
28041819Smaybee 	 */
28051819Smaybee 	if (off >= zp->z_phys->zp_size) {
28062237Smaybee 		zfs_range_unlock(rl);
28071819Smaybee 		return (EIO);
28081819Smaybee 	}
2809789Sahrens 	len = MIN(PAGESIZE, zp->z_phys->zp_size - off);
2810789Sahrens 
2811789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2812789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, off, len);
2813789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
2814789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2815789Sahrens 	if (err != 0) {
28162237Smaybee 		zfs_range_unlock(rl);
2817789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
28182113Sahrens 			dmu_tx_wait(tx);
28192113Sahrens 			dmu_tx_abort(tx);
2820789Sahrens 			goto top;
2821789Sahrens 		}
28222113Sahrens 		dmu_tx_abort(tx);
2823789Sahrens 		goto out;
2824789Sahrens 	}
2825789Sahrens 
2826789Sahrens 	va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1);
2827789Sahrens 
2828789Sahrens 	dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
2829789Sahrens 
2830789Sahrens 	ppmapout(va);
2831789Sahrens 
2832789Sahrens 	zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
28331472Sperrin 	(void) zfs_log_write(zilog, tx, TX_WRITE, zp, off, len, 0, NULL);
2834789Sahrens 	dmu_tx_commit(tx);
2835789Sahrens 
28362237Smaybee 	zfs_range_unlock(rl);
2837789Sahrens 
2838789Sahrens 	pvn_write_done(pp, B_WRITE | flags);
2839789Sahrens 	if (offp)
2840789Sahrens 		*offp = off;
2841789Sahrens 	if (lenp)
2842789Sahrens 		*lenp = len;
2843789Sahrens 
2844789Sahrens out:
2845789Sahrens 	return (err);
2846789Sahrens }
2847789Sahrens 
2848789Sahrens /*
2849789Sahrens  * Copy the portion of the file indicated from pages into the file.
2850789Sahrens  * The pages are stored in a page list attached to the files vnode.
2851789Sahrens  *
2852789Sahrens  *	IN:	vp	- vnode of file to push page data to.
2853789Sahrens  *		off	- position in file to put data.
2854789Sahrens  *		len	- amount of data to write.
2855789Sahrens  *		flags	- flags to control the operation.
2856789Sahrens  *		cr	- credentials of caller.
2857789Sahrens  *
2858789Sahrens  *	RETURN:	0 if success
2859789Sahrens  *		error code if failure
2860789Sahrens  *
2861789Sahrens  * Timestamps:
2862789Sahrens  *	vp - ctime|mtime updated
2863789Sahrens  */
2864789Sahrens static int
2865789Sahrens zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr)
2866789Sahrens {
2867789Sahrens 	znode_t		*zp = VTOZ(vp);
2868789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2869789Sahrens 	page_t		*pp;
2870789Sahrens 	size_t		io_len;
2871789Sahrens 	u_offset_t	io_off;
28721669Sperrin 	uint64_t	filesz;
2873789Sahrens 	int		error = 0;
2874789Sahrens 
2875789Sahrens 	ZFS_ENTER(zfsvfs);
2876789Sahrens 
2877789Sahrens 	ASSERT(zp->z_dbuf_held && zp->z_phys);
2878789Sahrens 
2879789Sahrens 	if (len == 0) {
2880789Sahrens 		/*
2881789Sahrens 		 * Search the entire vp list for pages >= off.
2882789Sahrens 		 */
2883789Sahrens 		error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage,
2884789Sahrens 		    flags, cr);
28851472Sperrin 		goto out;
2886789Sahrens 	}
2887789Sahrens 
28881669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
28891669Sperrin 	if (off > filesz) {
2890789Sahrens 		/* past end of file */
2891789Sahrens 		ZFS_EXIT(zfsvfs);
2892789Sahrens 		return (0);
2893789Sahrens 	}
2894789Sahrens 
28951669Sperrin 	len = MIN(len, filesz - off);
2896789Sahrens 
28971472Sperrin 	for (io_off = off; io_off < off + len; io_off += io_len) {
2898789Sahrens 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
28991669Sperrin 			pp = page_lookup(vp, io_off,
2900789Sahrens 				(flags & (B_INVAL | B_FREE)) ?
2901789Sahrens 					SE_EXCL : SE_SHARED);
2902789Sahrens 		} else {
2903789Sahrens 			pp = page_lookup_nowait(vp, io_off,
2904789Sahrens 				(flags & B_FREE) ? SE_EXCL : SE_SHARED);
2905789Sahrens 		}
2906789Sahrens 
2907789Sahrens 		if (pp != NULL && pvn_getdirty(pp, flags)) {
2908789Sahrens 			int err;
2909789Sahrens 
2910789Sahrens 			/*
2911789Sahrens 			 * Found a dirty page to push
2912789Sahrens 			 */
29131669Sperrin 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
29141669Sperrin 			if (err)
2915789Sahrens 				error = err;
2916789Sahrens 		} else {
2917789Sahrens 			io_len = PAGESIZE;
2918789Sahrens 		}
2919789Sahrens 	}
29201472Sperrin out:
29211472Sperrin 	zil_commit(zfsvfs->z_log, UINT64_MAX, (flags & B_ASYNC) ? 0 : FDSYNC);
2922789Sahrens 	ZFS_EXIT(zfsvfs);
2923789Sahrens 	return (error);
2924789Sahrens }
2925789Sahrens 
2926789Sahrens void
2927789Sahrens zfs_inactive(vnode_t *vp, cred_t *cr)
2928789Sahrens {
2929789Sahrens 	znode_t	*zp = VTOZ(vp);
2930789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2931789Sahrens 	int error;
2932789Sahrens 
2933789Sahrens 	rw_enter(&zfsvfs->z_um_lock, RW_READER);
2934789Sahrens 	if (zfsvfs->z_unmounted2) {
2935789Sahrens 		ASSERT(zp->z_dbuf_held == 0);
2936789Sahrens 
2937789Sahrens 		if (vn_has_cached_data(vp)) {
2938789Sahrens 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
2939789Sahrens 			    B_INVAL, cr);
2940789Sahrens 		}
2941789Sahrens 
29421544Seschrock 		mutex_enter(&zp->z_lock);
2943789Sahrens 		vp->v_count = 0; /* count arrives as 1 */
29441544Seschrock 		if (zp->z_dbuf == NULL) {
29451544Seschrock 			mutex_exit(&zp->z_lock);
29461544Seschrock 			zfs_znode_free(zp);
29471544Seschrock 		} else {
29481544Seschrock 			mutex_exit(&zp->z_lock);
29491544Seschrock 		}
2950789Sahrens 		rw_exit(&zfsvfs->z_um_lock);
2951789Sahrens 		VFS_RELE(zfsvfs->z_vfs);
2952789Sahrens 		return;
2953789Sahrens 	}
2954789Sahrens 
2955789Sahrens 	/*
2956789Sahrens 	 * Attempt to push any data in the page cache.  If this fails
2957789Sahrens 	 * we will get kicked out later in zfs_zinactive().
2958789Sahrens 	 */
29591298Sperrin 	if (vn_has_cached_data(vp)) {
29601298Sperrin 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
29611298Sperrin 		    cr);
29621298Sperrin 	}
2963789Sahrens 
2964789Sahrens 	if (zp->z_atime_dirty && zp->z_reap == 0) {
2965789Sahrens 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
2966789Sahrens 
2967789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
2968789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
2969789Sahrens 		if (error) {
2970789Sahrens 			dmu_tx_abort(tx);
2971789Sahrens 		} else {
2972789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
2973789Sahrens 			mutex_enter(&zp->z_lock);
2974789Sahrens 			zp->z_atime_dirty = 0;
2975789Sahrens 			mutex_exit(&zp->z_lock);
2976789Sahrens 			dmu_tx_commit(tx);
2977789Sahrens 		}
2978789Sahrens 	}
2979789Sahrens 
2980789Sahrens 	zfs_zinactive(zp);
2981789Sahrens 	rw_exit(&zfsvfs->z_um_lock);
2982789Sahrens }
2983789Sahrens 
2984789Sahrens /*
2985789Sahrens  * Bounds-check the seek operation.
2986789Sahrens  *
2987789Sahrens  *	IN:	vp	- vnode seeking within
2988789Sahrens  *		ooff	- old file offset
2989789Sahrens  *		noffp	- pointer to new file offset
2990789Sahrens  *
2991789Sahrens  *	RETURN:	0 if success
2992789Sahrens  *		EINVAL if new offset invalid
2993789Sahrens  */
2994789Sahrens /* ARGSUSED */
2995789Sahrens static int
2996789Sahrens zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp)
2997789Sahrens {
2998789Sahrens 	if (vp->v_type == VDIR)
2999789Sahrens 		return (0);
3000789Sahrens 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
3001789Sahrens }
3002789Sahrens 
3003789Sahrens /*
3004789Sahrens  * Pre-filter the generic locking function to trap attempts to place
3005789Sahrens  * a mandatory lock on a memory mapped file.
3006789Sahrens  */
3007789Sahrens static int
3008789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
3009789Sahrens     flk_callback_t *flk_cbp, cred_t *cr)
3010789Sahrens {
3011789Sahrens 	znode_t *zp = VTOZ(vp);
3012789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3013789Sahrens 	int error;
3014789Sahrens 
3015789Sahrens 	ZFS_ENTER(zfsvfs);
3016789Sahrens 
3017789Sahrens 	/*
30181544Seschrock 	 * We are following the UFS semantics with respect to mapcnt
30191544Seschrock 	 * here: If we see that the file is mapped already, then we will
30201544Seschrock 	 * return an error, but we don't worry about races between this
30211544Seschrock 	 * function and zfs_map().
3022789Sahrens 	 */
30231544Seschrock 	if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) {
3024789Sahrens 		ZFS_EXIT(zfsvfs);
3025789Sahrens 		return (EAGAIN);
3026789Sahrens 	}
3027789Sahrens 	error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr);
3028789Sahrens 	ZFS_EXIT(zfsvfs);
3029789Sahrens 	return (error);
3030789Sahrens }
3031789Sahrens 
3032789Sahrens /*
3033789Sahrens  * If we can't find a page in the cache, we will create a new page
3034789Sahrens  * and fill it with file data.  For efficiency, we may try to fill
30351669Sperrin  * multiple pages at once (klustering).
3036789Sahrens  */
3037789Sahrens static int
3038789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
3039789Sahrens     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
3040789Sahrens {
3041789Sahrens 	znode_t *zp = VTOZ(vp);
3042789Sahrens 	page_t *pp, *cur_pp;
3043789Sahrens 	objset_t *os = zp->z_zfsvfs->z_os;
3044789Sahrens 	caddr_t va;
3045789Sahrens 	u_offset_t io_off, total;
3046789Sahrens 	uint64_t oid = zp->z_id;
3047789Sahrens 	size_t io_len;
30481669Sperrin 	uint64_t filesz;
3049789Sahrens 	int err;
3050789Sahrens 
3051789Sahrens 	/*
3052789Sahrens 	 * If we are only asking for a single page don't bother klustering.
3053789Sahrens 	 */
30541669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
30551669Sperrin 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE || off > filesz) {
3056789Sahrens 		io_off = off;
3057789Sahrens 		io_len = PAGESIZE;
3058789Sahrens 		pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr);
3059789Sahrens 	} else {
3060789Sahrens 		/*
3061789Sahrens 		 * Try to fill a kluster of pages (a blocks worth).
3062789Sahrens 		 */
3063789Sahrens 		size_t klen;
3064789Sahrens 		u_offset_t koff;
3065789Sahrens 
3066789Sahrens 		if (!ISP2(zp->z_blksz)) {
3067789Sahrens 			/* Only one block in the file. */
3068789Sahrens 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
3069789Sahrens 			koff = 0;
3070789Sahrens 		} else {
3071789Sahrens 			klen = plsz;
3072789Sahrens 			koff = P2ALIGN(off, (u_offset_t)klen);
3073789Sahrens 		}
30741819Smaybee 		ASSERT(koff <= filesz);
30751819Smaybee 		if (koff + klen > filesz)
30761819Smaybee 			klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff;
3077789Sahrens 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
3078789Sahrens 			    &io_len, koff, klen, 0);
3079789Sahrens 	}
3080789Sahrens 	if (pp == NULL) {
3081789Sahrens 		/*
3082789Sahrens 		 * Some other thread entered the page before us.
3083789Sahrens 		 * Return to zfs_getpage to retry the lookup.
3084789Sahrens 		 */
3085789Sahrens 		*pl = NULL;
3086789Sahrens 		return (0);
3087789Sahrens 	}
3088789Sahrens 
3089789Sahrens 	/*
3090789Sahrens 	 * Fill the pages in the kluster.
3091789Sahrens 	 */
3092789Sahrens 	cur_pp = pp;
3093789Sahrens 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
3094789Sahrens 		ASSERT(io_off == cur_pp->p_offset);
3095789Sahrens 		va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1);
30961544Seschrock 		err = dmu_read(os, oid, io_off, PAGESIZE, va);
3097789Sahrens 		ppmapout(va);
3098789Sahrens 		if (err) {
3099789Sahrens 			/* On error, toss the entire kluster */
3100789Sahrens 			pvn_read_done(pp, B_ERROR);
3101789Sahrens 			return (err);
3102789Sahrens 		}
3103789Sahrens 		cur_pp = cur_pp->p_next;
3104789Sahrens 	}
3105789Sahrens out:
3106789Sahrens 	/*
3107789Sahrens 	 * Fill in the page list array from the kluster.  If
3108789Sahrens 	 * there are too many pages in the kluster, return
3109789Sahrens 	 * as many pages as possible starting from the desired
3110789Sahrens 	 * offset `off'.
3111789Sahrens 	 * NOTE: the page list will always be null terminated.
3112789Sahrens 	 */
3113789Sahrens 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
3114789Sahrens 
3115789Sahrens 	return (0);
3116789Sahrens }
3117789Sahrens 
3118789Sahrens /*
3119789Sahrens  * Return pointers to the pages for the file region [off, off + len]
3120789Sahrens  * in the pl array.  If plsz is greater than len, this function may
3121789Sahrens  * also return page pointers from before or after the specified
3122789Sahrens  * region (i.e. some region [off', off' + plsz]).  These additional
3123789Sahrens  * pages are only returned if they are already in the cache, or were
3124789Sahrens  * created as part of a klustered read.
3125789Sahrens  *
3126789Sahrens  *	IN:	vp	- vnode of file to get data from.
3127789Sahrens  *		off	- position in file to get data from.
3128789Sahrens  *		len	- amount of data to retrieve.
3129789Sahrens  *		plsz	- length of provided page list.
3130789Sahrens  *		seg	- segment to obtain pages for.
3131789Sahrens  *		addr	- virtual address of fault.
3132789Sahrens  *		rw	- mode of created pages.
3133789Sahrens  *		cr	- credentials of caller.
3134789Sahrens  *
3135789Sahrens  *	OUT:	protp	- protection mode of created pages.
3136789Sahrens  *		pl	- list of pages created.
3137789Sahrens  *
3138789Sahrens  *	RETURN:	0 if success
3139789Sahrens  *		error code if failure
3140789Sahrens  *
3141789Sahrens  * Timestamps:
3142789Sahrens  *	vp - atime updated
3143789Sahrens  */
3144789Sahrens /* ARGSUSED */
3145789Sahrens static int
3146789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
3147789Sahrens 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
3148789Sahrens 	enum seg_rw rw, cred_t *cr)
3149789Sahrens {
3150789Sahrens 	znode_t		*zp = VTOZ(vp);
3151789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3152789Sahrens 	page_t		*pp, **pl0 = pl;
31531669Sperrin 	rl_t		*rl;
3154789Sahrens 	int		cnt = 0, need_unlock = 0, err = 0;
3155789Sahrens 
3156789Sahrens 	ZFS_ENTER(zfsvfs);
3157789Sahrens 
3158789Sahrens 	if (protp)
3159789Sahrens 		*protp = PROT_ALL;
3160789Sahrens 
3161789Sahrens 	ASSERT(zp->z_dbuf_held && zp->z_phys);
3162789Sahrens 
3163789Sahrens 	/* no faultahead (for now) */
3164789Sahrens 	if (pl == NULL) {
3165789Sahrens 		ZFS_EXIT(zfsvfs);
3166789Sahrens 		return (0);
3167789Sahrens 	}
3168789Sahrens 
31691669Sperrin 	/*
31701669Sperrin 	 * Make sure nobody restructures the file in the middle of the getpage.
31711669Sperrin 	 */
31721669Sperrin 	rl = zfs_range_lock(zp, off, len, RL_READER);
31731669Sperrin 
3174789Sahrens 	/* can't fault past EOF */
3175789Sahrens 	if (off >= zp->z_phys->zp_size) {
31762237Smaybee 		zfs_range_unlock(rl);
3177789Sahrens 		ZFS_EXIT(zfsvfs);
3178789Sahrens 		return (EFAULT);
3179789Sahrens 	}
3180789Sahrens 
3181789Sahrens 	/*
3182789Sahrens 	 * If we already own the lock, then we must be page faulting
3183789Sahrens 	 * in the middle of a write to this file (i.e., we are writing
3184789Sahrens 	 * to this file using data from a mapped region of the file).
3185789Sahrens 	 */
3186789Sahrens 	if (!rw_owner(&zp->z_map_lock)) {
3187789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
3188789Sahrens 		need_unlock = TRUE;
3189789Sahrens 	}
3190789Sahrens 
3191789Sahrens 	/*
3192789Sahrens 	 * Loop through the requested range [off, off + len] looking
3193789Sahrens 	 * for pages.  If we don't find a page, we will need to create
3194789Sahrens 	 * a new page and fill it with data from the file.
3195789Sahrens 	 */
3196789Sahrens 	while (len > 0) {
3197789Sahrens 		if (plsz < PAGESIZE)
3198789Sahrens 			break;
3199789Sahrens 		if (pp = page_lookup(vp, off, SE_SHARED)) {
3200789Sahrens 			*pl++ = pp;
3201789Sahrens 			off += PAGESIZE;
3202789Sahrens 			addr += PAGESIZE;
3203789Sahrens 			len -= PAGESIZE;
3204789Sahrens 			plsz -= PAGESIZE;
3205789Sahrens 		} else {
3206789Sahrens 			err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw);
3207789Sahrens 			/*
3208789Sahrens 			 * klustering may have changed our region
3209789Sahrens 			 * to be block aligned.
3210789Sahrens 			 */
3211789Sahrens 			if (((pp = *pl) != 0) && (off != pp->p_offset)) {
3212789Sahrens 				int delta = off - pp->p_offset;
3213789Sahrens 				len += delta;
3214789Sahrens 				off -= delta;
3215789Sahrens 				addr -= delta;
3216789Sahrens 			}
3217789Sahrens 			while (*pl) {
3218789Sahrens 				pl++;
3219789Sahrens 				cnt++;
3220789Sahrens 				off += PAGESIZE;
3221789Sahrens 				addr += PAGESIZE;
3222789Sahrens 				plsz -= PAGESIZE;
3223789Sahrens 				if (len > PAGESIZE)
3224789Sahrens 					len -= PAGESIZE;
3225789Sahrens 				else
3226789Sahrens 					len = 0;
3227789Sahrens 			}
32281669Sperrin 			if (err) {
32291669Sperrin 				/*
32301669Sperrin 				 * Release any pages we have locked.
32311669Sperrin 				 */
32321669Sperrin 				while (pl > pl0)
32331669Sperrin 					page_unlock(*--pl);
32341669Sperrin 				goto out;
32351669Sperrin 			}
3236789Sahrens 		}
3237789Sahrens 	}
3238789Sahrens 
3239789Sahrens 	/*
3240789Sahrens 	 * Fill out the page array with any pages already in the cache.
3241789Sahrens 	 */
3242789Sahrens 	while (plsz > 0) {
3243789Sahrens 		pp = page_lookup_nowait(vp, off, SE_SHARED);
3244789Sahrens 		if (pp == NULL)
3245789Sahrens 			break;
3246789Sahrens 		*pl++ = pp;
3247789Sahrens 		off += PAGESIZE;
3248789Sahrens 		plsz -= PAGESIZE;
3249789Sahrens 	}
3250789Sahrens 
3251789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3252789Sahrens out:
3253789Sahrens 	*pl = NULL;
3254789Sahrens 
3255789Sahrens 	if (need_unlock)
3256789Sahrens 		rw_exit(&zp->z_map_lock);
32572237Smaybee 	zfs_range_unlock(rl);
3258789Sahrens 
3259789Sahrens 	ZFS_EXIT(zfsvfs);
3260789Sahrens 	return (err);
3261789Sahrens }
3262789Sahrens 
32631544Seschrock /*
32641544Seschrock  * Request a memory map for a section of a file.  This code interacts
32651544Seschrock  * with common code and the VM system as follows:
32661544Seschrock  *
32671544Seschrock  *	common code calls mmap(), which ends up in smmap_common()
32681544Seschrock  *
32691544Seschrock  *	this calls VOP_MAP(), which takes you into (say) zfs
32701544Seschrock  *
32711544Seschrock  *	zfs_map() calls as_map(), passing segvn_create() as the callback
32721544Seschrock  *
32731544Seschrock  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
32741544Seschrock  *
32751544Seschrock  *	zfs_addmap() updates z_mapcnt
32761544Seschrock  */
3277789Sahrens static int
3278789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
3279789Sahrens     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
3280789Sahrens {
3281789Sahrens 	znode_t *zp = VTOZ(vp);
3282789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3283789Sahrens 	segvn_crargs_t	vn_a;
3284789Sahrens 	int		error;
3285789Sahrens 
3286789Sahrens 	ZFS_ENTER(zfsvfs);
3287789Sahrens 
3288789Sahrens 	if (vp->v_flag & VNOMAP) {
3289789Sahrens 		ZFS_EXIT(zfsvfs);
3290789Sahrens 		return (ENOSYS);
3291789Sahrens 	}
3292789Sahrens 
3293789Sahrens 	if (off < 0 || len > MAXOFFSET_T - off) {
3294789Sahrens 		ZFS_EXIT(zfsvfs);
3295789Sahrens 		return (ENXIO);
3296789Sahrens 	}
3297789Sahrens 
3298789Sahrens 	if (vp->v_type != VREG) {
3299789Sahrens 		ZFS_EXIT(zfsvfs);
3300789Sahrens 		return (ENODEV);
3301789Sahrens 	}
3302789Sahrens 
3303789Sahrens 	/*
3304789Sahrens 	 * If file is locked, disallow mapping.
3305789Sahrens 	 */
33061544Seschrock 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) {
33071544Seschrock 		ZFS_EXIT(zfsvfs);
33081544Seschrock 		return (EAGAIN);
3309789Sahrens 	}
3310789Sahrens 
3311789Sahrens 	as_rangelock(as);
3312789Sahrens 	if ((flags & MAP_FIXED) == 0) {
3313789Sahrens 		map_addr(addrp, len, off, 1, flags);
3314789Sahrens 		if (*addrp == NULL) {
3315789Sahrens 			as_rangeunlock(as);
3316789Sahrens 			ZFS_EXIT(zfsvfs);
3317789Sahrens 			return (ENOMEM);
3318789Sahrens 		}
3319789Sahrens 	} else {
3320789Sahrens 		/*
3321789Sahrens 		 * User specified address - blow away any previous mappings
3322789Sahrens 		 */
3323789Sahrens 		(void) as_unmap(as, *addrp, len);
3324789Sahrens 	}
3325789Sahrens 
3326789Sahrens 	vn_a.vp = vp;
3327789Sahrens 	vn_a.offset = (u_offset_t)off;
3328789Sahrens 	vn_a.type = flags & MAP_TYPE;
3329789Sahrens 	vn_a.prot = prot;
3330789Sahrens 	vn_a.maxprot = maxprot;
3331789Sahrens 	vn_a.cred = cr;
3332789Sahrens 	vn_a.amp = NULL;
3333789Sahrens 	vn_a.flags = flags & ~MAP_TYPE;
33341417Skchow 	vn_a.szc = 0;
33351417Skchow 	vn_a.lgrp_mem_policy_flags = 0;
3336789Sahrens 
3337789Sahrens 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
3338789Sahrens 
3339789Sahrens 	as_rangeunlock(as);
3340789Sahrens 	ZFS_EXIT(zfsvfs);
3341789Sahrens 	return (error);
3342789Sahrens }
3343789Sahrens 
3344789Sahrens /* ARGSUSED */
3345789Sahrens static int
3346789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
3347789Sahrens     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
3348789Sahrens {
33491544Seschrock 	uint64_t pages = btopr(len);
33501544Seschrock 
33511544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
3352789Sahrens 	return (0);
3353789Sahrens }
3354789Sahrens 
33551773Seschrock /*
33561773Seschrock  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
33571773Seschrock  * more accurate mtime for the associated file.  Since we don't have a way of
33581773Seschrock  * detecting when the data was actually modified, we have to resort to
33591773Seschrock  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
33601773Seschrock  * last page is pushed.  The problem occurs when the msync() call is omitted,
33611773Seschrock  * which by far the most common case:
33621773Seschrock  *
33631773Seschrock  * 	open()
33641773Seschrock  * 	mmap()
33651773Seschrock  * 	<modify memory>
33661773Seschrock  * 	munmap()
33671773Seschrock  * 	close()
33681773Seschrock  * 	<time lapse>
33691773Seschrock  * 	putpage() via fsflush
33701773Seschrock  *
33711773Seschrock  * If we wait until fsflush to come along, we can have a modification time that
33721773Seschrock  * is some arbitrary point in the future.  In order to prevent this in the
33731773Seschrock  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
33741773Seschrock  * torn down.
33751773Seschrock  */
3376789Sahrens /* ARGSUSED */
3377789Sahrens static int
3378789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
3379789Sahrens     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr)
3380789Sahrens {
33811544Seschrock 	uint64_t pages = btopr(len);
33821544Seschrock 
33831544Seschrock 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
33841544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
33851773Seschrock 
33861773Seschrock 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
33871773Seschrock 	    vn_has_cached_data(vp))
33881773Seschrock 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr);
33891773Seschrock 
3390789Sahrens 	return (0);
3391789Sahrens }
3392789Sahrens 
3393789Sahrens /*
3394789Sahrens  * Free or allocate space in a file.  Currently, this function only
3395789Sahrens  * supports the `F_FREESP' command.  However, this command is somewhat
3396789Sahrens  * misnamed, as its functionality includes the ability to allocate as
3397789Sahrens  * well as free space.
3398789Sahrens  *
3399789Sahrens  *	IN:	vp	- vnode of file to free data in.
3400789Sahrens  *		cmd	- action to take (only F_FREESP supported).
3401789Sahrens  *		bfp	- section of file to free/alloc.
3402789Sahrens  *		flag	- current file open mode flags.
3403789Sahrens  *		offset	- current file offset.
3404789Sahrens  *		cr	- credentials of caller [UNUSED].
3405789Sahrens  *
3406789Sahrens  *	RETURN:	0 if success
3407789Sahrens  *		error code if failure
3408789Sahrens  *
3409789Sahrens  * Timestamps:
3410789Sahrens  *	vp - ctime|mtime updated
3411789Sahrens  */
3412789Sahrens /* ARGSUSED */
3413789Sahrens static int
3414789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
3415789Sahrens     offset_t offset, cred_t *cr, caller_context_t *ct)
3416789Sahrens {
3417789Sahrens 	znode_t		*zp = VTOZ(vp);
3418789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3419789Sahrens 	uint64_t	off, len;
3420789Sahrens 	int		error;
3421789Sahrens 
3422789Sahrens 	ZFS_ENTER(zfsvfs);
3423789Sahrens 
3424789Sahrens top:
3425789Sahrens 	if (cmd != F_FREESP) {
3426789Sahrens 		ZFS_EXIT(zfsvfs);
3427789Sahrens 		return (EINVAL);
3428789Sahrens 	}
3429789Sahrens 
3430789Sahrens 	if (error = convoff(vp, bfp, 0, offset)) {
3431789Sahrens 		ZFS_EXIT(zfsvfs);
3432789Sahrens 		return (error);
3433789Sahrens 	}
3434789Sahrens 
3435789Sahrens 	if (bfp->l_len < 0) {
3436789Sahrens 		ZFS_EXIT(zfsvfs);
3437789Sahrens 		return (EINVAL);
3438789Sahrens 	}
3439789Sahrens 
3440789Sahrens 	off = bfp->l_start;
34411669Sperrin 	len = bfp->l_len; /* 0 means from off to end of file */
34421878Smaybee 
34431878Smaybee 	do {
34441878Smaybee 		error = zfs_freesp(zp, off, len, flag, TRUE);
34452113Sahrens 		/* NB: we already did dmu_tx_wait() if necessary */
34461878Smaybee 	} while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
3447789Sahrens 
3448789Sahrens 	ZFS_EXIT(zfsvfs);
3449789Sahrens 	return (error);
3450789Sahrens }
3451789Sahrens 
3452789Sahrens static int
3453789Sahrens zfs_fid(vnode_t *vp, fid_t *fidp)
3454789Sahrens {
3455789Sahrens 	znode_t		*zp = VTOZ(vp);
3456789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3457789Sahrens 	uint32_t	gen = (uint32_t)zp->z_phys->zp_gen;
3458789Sahrens 	uint64_t	object = zp->z_id;
3459789Sahrens 	zfid_short_t	*zfid;
3460789Sahrens 	int		size, i;
3461789Sahrens 
3462789Sahrens 	ZFS_ENTER(zfsvfs);
3463789Sahrens 
3464789Sahrens 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
3465789Sahrens 	if (fidp->fid_len < size) {
3466789Sahrens 		fidp->fid_len = size;
34671512Sek110237 		ZFS_EXIT(zfsvfs);
3468789Sahrens 		return (ENOSPC);
3469789Sahrens 	}
3470789Sahrens 
3471789Sahrens 	zfid = (zfid_short_t *)fidp;
3472789Sahrens 
3473789Sahrens 	zfid->zf_len = size;
3474789Sahrens 
3475789Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
3476789Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
3477789Sahrens 
3478789Sahrens 	/* Must have a non-zero generation number to distinguish from .zfs */
3479789Sahrens 	if (gen == 0)
3480789Sahrens 		gen = 1;
3481789Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
3482789Sahrens 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
3483789Sahrens 
3484789Sahrens 	if (size == LONG_FID_LEN) {
3485789Sahrens 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
3486789Sahrens 		zfid_long_t	*zlfid;
3487789Sahrens 
3488789Sahrens 		zlfid = (zfid_long_t *)fidp;
3489789Sahrens 
3490789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
3491789Sahrens 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
3492789Sahrens 
3493789Sahrens 		/* XXX - this should be the generation number for the objset */
3494789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
3495789Sahrens 			zlfid->zf_setgen[i] = 0;
3496789Sahrens 	}
3497789Sahrens 
3498789Sahrens 	ZFS_EXIT(zfsvfs);
3499789Sahrens 	return (0);
3500789Sahrens }
3501789Sahrens 
3502789Sahrens static int
3503789Sahrens zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr)
3504789Sahrens {
3505789Sahrens 	znode_t		*zp, *xzp;
3506789Sahrens 	zfsvfs_t	*zfsvfs;
3507789Sahrens 	zfs_dirlock_t	*dl;
3508789Sahrens 	int		error;
3509789Sahrens 
3510789Sahrens 	switch (cmd) {
3511789Sahrens 	case _PC_LINK_MAX:
3512789Sahrens 		*valp = ULONG_MAX;
3513789Sahrens 		return (0);
3514789Sahrens 
3515789Sahrens 	case _PC_FILESIZEBITS:
3516789Sahrens 		*valp = 64;
3517789Sahrens 		return (0);
3518789Sahrens 
3519789Sahrens 	case _PC_XATTR_EXISTS:
3520789Sahrens 		zp = VTOZ(vp);
3521789Sahrens 		zfsvfs = zp->z_zfsvfs;
3522789Sahrens 		ZFS_ENTER(zfsvfs);
3523789Sahrens 		*valp = 0;
3524789Sahrens 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
3525789Sahrens 		    ZXATTR | ZEXISTS | ZSHARED);
3526789Sahrens 		if (error == 0) {
3527789Sahrens 			zfs_dirent_unlock(dl);
3528789Sahrens 			if (!zfs_dirempty(xzp))
3529789Sahrens 				*valp = 1;
3530789Sahrens 			VN_RELE(ZTOV(xzp));
3531789Sahrens 		} else if (error == ENOENT) {
3532789Sahrens 			/*
3533789Sahrens 			 * If there aren't extended attributes, it's the
3534789Sahrens 			 * same as having zero of them.
3535789Sahrens 			 */
3536789Sahrens 			error = 0;
3537789Sahrens 		}
3538789Sahrens 		ZFS_EXIT(zfsvfs);
3539789Sahrens 		return (error);
3540789Sahrens 
3541789Sahrens 	case _PC_ACL_ENABLED:
3542789Sahrens 		*valp = _ACL_ACE_ENABLED;
3543789Sahrens 		return (0);
3544789Sahrens 
3545789Sahrens 	case _PC_MIN_HOLE_SIZE:
3546789Sahrens 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
3547789Sahrens 		return (0);
3548789Sahrens 
3549789Sahrens 	default:
3550789Sahrens 		return (fs_pathconf(vp, cmd, valp, cr));
3551789Sahrens 	}
3552789Sahrens }
3553789Sahrens 
3554789Sahrens /*ARGSUSED*/
3555789Sahrens static int
3556789Sahrens zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
3557789Sahrens {
3558789Sahrens 	znode_t *zp = VTOZ(vp);
3559789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3560789Sahrens 	int error;
3561789Sahrens 
3562789Sahrens 	ZFS_ENTER(zfsvfs);
3563789Sahrens 	error = zfs_getacl(zp, vsecp, cr);
3564789Sahrens 	ZFS_EXIT(zfsvfs);
3565789Sahrens 
3566789Sahrens 	return (error);
3567789Sahrens }
3568789Sahrens 
3569789Sahrens /*ARGSUSED*/
3570789Sahrens static int
3571789Sahrens zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
3572789Sahrens {
3573789Sahrens 	znode_t *zp = VTOZ(vp);
3574789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3575789Sahrens 	int error;
3576789Sahrens 
3577789Sahrens 	ZFS_ENTER(zfsvfs);
3578789Sahrens 	error = zfs_setacl(zp, vsecp, cr);
3579789Sahrens 	ZFS_EXIT(zfsvfs);
3580789Sahrens 	return (error);
3581789Sahrens }
3582789Sahrens 
3583789Sahrens /*
3584789Sahrens  * Predeclare these here so that the compiler assumes that
3585789Sahrens  * this is an "old style" function declaration that does
3586789Sahrens  * not include arguments => we won't get type mismatch errors
3587789Sahrens  * in the initializations that follow.
3588789Sahrens  */
3589789Sahrens static int zfs_inval();
3590789Sahrens static int zfs_isdir();
3591789Sahrens 
3592789Sahrens static int
3593789Sahrens zfs_inval()
3594789Sahrens {
3595789Sahrens 	return (EINVAL);
3596789Sahrens }
3597789Sahrens 
3598789Sahrens static int
3599789Sahrens zfs_isdir()
3600789Sahrens {
3601789Sahrens 	return (EISDIR);
3602789Sahrens }
3603789Sahrens /*
3604789Sahrens  * Directory vnode operations template
3605789Sahrens  */
3606789Sahrens vnodeops_t *zfs_dvnodeops;
3607789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = {
3608789Sahrens 	VOPNAME_OPEN, zfs_open,
3609789Sahrens 	VOPNAME_CLOSE, zfs_close,
3610789Sahrens 	VOPNAME_READ, zfs_isdir,
3611789Sahrens 	VOPNAME_WRITE, zfs_isdir,
3612789Sahrens 	VOPNAME_IOCTL, zfs_ioctl,
3613789Sahrens 	VOPNAME_GETATTR, zfs_getattr,
3614789Sahrens 	VOPNAME_SETATTR, zfs_setattr,
3615789Sahrens 	VOPNAME_ACCESS, zfs_access,
3616789Sahrens 	VOPNAME_LOOKUP, zfs_lookup,
3617789Sahrens 	VOPNAME_CREATE, zfs_create,
3618789Sahrens 	VOPNAME_REMOVE, zfs_remove,
3619789Sahrens 	VOPNAME_LINK, zfs_link,
3620789Sahrens 	VOPNAME_RENAME, zfs_rename,
3621789Sahrens 	VOPNAME_MKDIR, zfs_mkdir,
3622789Sahrens 	VOPNAME_RMDIR, zfs_rmdir,
3623789Sahrens 	VOPNAME_READDIR, zfs_readdir,
3624789Sahrens 	VOPNAME_SYMLINK, zfs_symlink,
3625789Sahrens 	VOPNAME_FSYNC, zfs_fsync,
3626789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3627789Sahrens 	VOPNAME_FID, zfs_fid,
3628789Sahrens 	VOPNAME_SEEK, zfs_seek,
3629789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3630789Sahrens 	VOPNAME_GETSECATTR, zfs_getsecattr,
3631789Sahrens 	VOPNAME_SETSECATTR, zfs_setsecattr,
3632789Sahrens 	NULL, NULL
3633789Sahrens };
3634789Sahrens 
3635789Sahrens /*
3636789Sahrens  * Regular file vnode operations template
3637789Sahrens  */
3638789Sahrens vnodeops_t *zfs_fvnodeops;
3639789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = {
3640789Sahrens 	VOPNAME_OPEN, zfs_open,
3641789Sahrens 	VOPNAME_CLOSE, zfs_close,
3642789Sahrens 	VOPNAME_READ, zfs_read,
3643789Sahrens 	VOPNAME_WRITE, zfs_write,
3644789Sahrens 	VOPNAME_IOCTL, zfs_ioctl,
3645789Sahrens 	VOPNAME_GETATTR, zfs_getattr,
3646789Sahrens 	VOPNAME_SETATTR, zfs_setattr,
3647789Sahrens 	VOPNAME_ACCESS, zfs_access,
3648789Sahrens 	VOPNAME_LOOKUP, zfs_lookup,
3649789Sahrens 	VOPNAME_RENAME, zfs_rename,
3650789Sahrens 	VOPNAME_FSYNC, zfs_fsync,
3651789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p)zfs_inactive,
3652789Sahrens 	VOPNAME_FID, zfs_fid,
3653789Sahrens 	VOPNAME_SEEK, zfs_seek,
3654789Sahrens 	VOPNAME_FRLOCK, zfs_frlock,
3655789Sahrens 	VOPNAME_SPACE, zfs_space,
3656789Sahrens 	VOPNAME_GETPAGE, zfs_getpage,
3657789Sahrens 	VOPNAME_PUTPAGE, zfs_putpage,
3658789Sahrens 	VOPNAME_MAP, (fs_generic_func_p) zfs_map,
3659789Sahrens 	VOPNAME_ADDMAP, (fs_generic_func_p) zfs_addmap,
3660789Sahrens 	VOPNAME_DELMAP, zfs_delmap,
3661789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3662789Sahrens 	VOPNAME_GETSECATTR, zfs_getsecattr,
3663789Sahrens 	VOPNAME_SETSECATTR, zfs_setsecattr,
3664789Sahrens 	VOPNAME_VNEVENT, fs_vnevent_support,
3665789Sahrens 	NULL, NULL
3666789Sahrens };
3667789Sahrens 
3668789Sahrens /*
3669789Sahrens  * Symbolic link vnode operations template
3670789Sahrens  */
3671789Sahrens vnodeops_t *zfs_symvnodeops;
3672789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = {
3673789Sahrens 	VOPNAME_GETATTR, zfs_getattr,
3674789Sahrens 	VOPNAME_SETATTR, zfs_setattr,
3675789Sahrens 	VOPNAME_ACCESS, zfs_access,
3676789Sahrens 	VOPNAME_RENAME, zfs_rename,
3677789Sahrens 	VOPNAME_READLINK, zfs_readlink,
3678789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3679789Sahrens 	VOPNAME_FID, zfs_fid,
3680789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3681789Sahrens 	VOPNAME_VNEVENT, fs_vnevent_support,
3682789Sahrens 	NULL, NULL
3683789Sahrens };
3684789Sahrens 
3685789Sahrens /*
3686789Sahrens  * Extended attribute directory vnode operations template
3687789Sahrens  *	This template is identical to the directory vnodes
3688789Sahrens  *	operation template except for restricted operations:
3689789Sahrens  *		VOP_MKDIR()
3690789Sahrens  *		VOP_SYMLINK()
3691789Sahrens  * Note that there are other restrictions embedded in:
3692789Sahrens  *	zfs_create()	- restrict type to VREG
3693789Sahrens  *	zfs_link()	- no links into/out of attribute space
3694789Sahrens  *	zfs_rename()	- no moves into/out of attribute space
3695789Sahrens  */
3696789Sahrens vnodeops_t *zfs_xdvnodeops;
3697789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = {
3698789Sahrens 	VOPNAME_OPEN, zfs_open,
3699789Sahrens 	VOPNAME_CLOSE, zfs_close,
3700789Sahrens 	VOPNAME_IOCTL, zfs_ioctl,
3701789Sahrens 	VOPNAME_GETATTR, zfs_getattr,
3702789Sahrens 	VOPNAME_SETATTR, zfs_setattr,
3703789Sahrens 	VOPNAME_ACCESS, zfs_access,
3704789Sahrens 	VOPNAME_LOOKUP, zfs_lookup,
3705789Sahrens 	VOPNAME_CREATE, zfs_create,
3706789Sahrens 	VOPNAME_REMOVE, zfs_remove,
3707789Sahrens 	VOPNAME_LINK, zfs_link,
3708789Sahrens 	VOPNAME_RENAME, zfs_rename,
3709789Sahrens 	VOPNAME_MKDIR, zfs_inval,
3710789Sahrens 	VOPNAME_RMDIR, zfs_rmdir,
3711789Sahrens 	VOPNAME_READDIR, zfs_readdir,
3712789Sahrens 	VOPNAME_SYMLINK, zfs_inval,
3713789Sahrens 	VOPNAME_FSYNC, zfs_fsync,
3714789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3715789Sahrens 	VOPNAME_FID, zfs_fid,
3716789Sahrens 	VOPNAME_SEEK, zfs_seek,
3717789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3718789Sahrens 	VOPNAME_GETSECATTR, zfs_getsecattr,
3719789Sahrens 	VOPNAME_SETSECATTR, zfs_setsecattr,
3720789Sahrens 	VOPNAME_VNEVENT, fs_vnevent_support,
3721789Sahrens 	NULL, NULL
3722789Sahrens };
3723789Sahrens 
3724789Sahrens /*
3725789Sahrens  * Error vnode operations template
3726789Sahrens  */
3727789Sahrens vnodeops_t *zfs_evnodeops;
3728789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = {
3729789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3730789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3731789Sahrens 	NULL, NULL
3732789Sahrens };
3733