xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 2113:0510bb40c993)
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,
111*2113Sahrens  *	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) {
137*2113Sahrens  *			dmu_tx_wait(tx);
138*2113Sahrens  *			dmu_tx_abort(tx);
139789Sahrens  *			goto top;
140789Sahrens  *		}
141*2113Sahrens  *		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:
4901669Sperrin 	zfs_range_unlock(zp, 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 	/*
6091669Sperrin 	 * Pre-fault the initial pages to ensure slow (eg NFS) pages
6101669Sperrin 	 * don't hold up txg.
611789Sahrens 	 */
612789Sahrens 	zfs_prefault_write(MIN(start_resid, SPA_MAXBLOCKSIZE), 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) {
673*2113Sahrens 			dmu_tx_wait(tx);
674*2113Sahrens 			dmu_tx_abort(tx);
675789Sahrens 			goto top;
676789Sahrens 		}
677*2113Sahrens 		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);
6951669Sperrin 		zfs_range_reduce(zp, 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 		/* Pre-fault the next set of pages */
770789Sahrens 		zfs_prefault_write(MIN(n, SPA_MAXBLOCKSIZE), uio);
771789Sahrens 
772789Sahrens 		/*
773789Sahrens 		 * Start another transaction.
774789Sahrens 		 */
775789Sahrens 		woff = uio->uio_loffset;
776789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
777789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
778789Sahrens 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
779789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
780789Sahrens 		if (error) {
781789Sahrens 			if (error == ERESTART &&
782789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
783*2113Sahrens 				dmu_tx_wait(tx);
784*2113Sahrens 				dmu_tx_abort(tx);
785789Sahrens 				goto top;
786789Sahrens 			}
787*2113Sahrens 			dmu_tx_abort(tx);
788789Sahrens 			goto no_tx_done;
789789Sahrens 		}
790789Sahrens 	}
791789Sahrens 
792789Sahrens tx_done:
793789Sahrens 
794789Sahrens 	if (tx_bytes != 0) {
795789Sahrens 		/*
796789Sahrens 		 * Update the file size if it has changed; account
797789Sahrens 		 * for possible concurrent updates.
798789Sahrens 		 */
799789Sahrens 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) {
800789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
801789Sahrens 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
802789Sahrens 			    uio->uio_loffset);
803789Sahrens 		}
804789Sahrens 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
805789Sahrens 		seq = zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes,
806789Sahrens 		    ioflag, uio);
807789Sahrens 	}
808789Sahrens 	dmu_tx_commit(tx);
809789Sahrens 
810789Sahrens 
811789Sahrens no_tx_done:
812789Sahrens 
8131669Sperrin 	zfs_range_unlock(zp, rl);
814789Sahrens 
815789Sahrens 	/*
816789Sahrens 	 * If we're in replay mode, or we made no progress, return error.
817789Sahrens 	 * Otherwise, it's at least a partial write, so it's successful.
818789Sahrens 	 */
819789Sahrens 	if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) {
820789Sahrens 		ZFS_EXIT(zfsvfs);
821789Sahrens 		return (error);
822789Sahrens 	}
823789Sahrens 
824789Sahrens 	zil_commit(zilog, seq, ioflag & (FSYNC | FDSYNC));
825789Sahrens 
826789Sahrens 	ZFS_EXIT(zfsvfs);
827789Sahrens 	return (0);
828789Sahrens }
829789Sahrens 
830789Sahrens /*
831789Sahrens  * Get data to generate a TX_WRITE intent log record.
832789Sahrens  */
833789Sahrens int
8341669Sperrin zfs_get_data(void *arg, lr_write_t *lr, char *buf)
835789Sahrens {
836789Sahrens 	zfsvfs_t *zfsvfs = arg;
837789Sahrens 	objset_t *os = zfsvfs->z_os;
838789Sahrens 	znode_t *zp;
839789Sahrens 	uint64_t off = lr->lr_offset;
8401669Sperrin 	rl_t *rl;
841789Sahrens 	int dlen = lr->lr_length;  		/* length of user data */
842789Sahrens 	int error = 0;
843789Sahrens 
844789Sahrens 	ASSERT(dlen != 0);
845789Sahrens 
846789Sahrens 	/*
8471669Sperrin 	 * Nothing to do if the file has been removed
848789Sahrens 	 */
849789Sahrens 	if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0)
850789Sahrens 		return (ENOENT);
8511669Sperrin 	if (zp->z_reap) {
852789Sahrens 		VN_RELE(ZTOV(zp));
853789Sahrens 		return (ENOENT);
854789Sahrens 	}
855789Sahrens 
856789Sahrens 	/*
857789Sahrens 	 * Write records come in two flavors: immediate and indirect.
858789Sahrens 	 * For small writes it's cheaper to store the data with the
859789Sahrens 	 * log record (immediate); for large writes it's cheaper to
860789Sahrens 	 * sync the data and get a pointer to it (indirect) so that
861789Sahrens 	 * we don't have to write the data twice.
862789Sahrens 	 */
8631669Sperrin 	if (buf != NULL) { /* immediate write */
8641544Seschrock 		dmu_buf_t *db;
8651669Sperrin 
8661669Sperrin 		rl = zfs_range_lock(zp, off, dlen, RL_READER);
8671669Sperrin 		/* test for truncation needs to be done while range locked */
8681669Sperrin 		if (off >= zp->z_phys->zp_size) {
8691669Sperrin 			error = ENOENT;
8701669Sperrin 			goto out;
8711669Sperrin 		}
8721544Seschrock 		VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, off, FTAG, &db));
8731669Sperrin 		bcopy((char *)db->db_data + off - db->db_offset, buf, dlen);
8741544Seschrock 		dmu_buf_rele(db, FTAG);
8751669Sperrin 	} else { /* indirect write */
8761669Sperrin 		uint64_t boff; /* block starting offset */
8771669Sperrin 
878789Sahrens 		/*
8791669Sperrin 		 * Have to lock the whole block to ensure when it's
8801669Sperrin 		 * written out and it's checksum is being calculated
8811669Sperrin 		 * that no one can change the data. We need to re-check
8821669Sperrin 		 * blocksize after we get the lock in case it's changed!
883789Sahrens 		 */
8841669Sperrin 		for (;;) {
8851941Sperrin 			if (ISP2(zp->z_blksz)) {
8861941Sperrin 				boff = P2ALIGN_TYPED(off, zp->z_blksz,
8871941Sperrin 				    uint64_t);
8881941Sperrin 			} else {
8891941Sperrin 				boff = 0;
8901941Sperrin 			}
8911669Sperrin 			dlen = zp->z_blksz;
8921669Sperrin 			rl = zfs_range_lock(zp, boff, dlen, RL_READER);
8931669Sperrin 			if (zp->z_blksz == dlen)
8941669Sperrin 				break;
8951669Sperrin 			zfs_range_unlock(zp, rl);
8961669Sperrin 		}
8971669Sperrin 		/* test for truncation needs to be done while range locked */
8981669Sperrin 		if (off >= zp->z_phys->zp_size) {
8991669Sperrin 			error = ENOENT;
9001669Sperrin 			goto out;
9011669Sperrin 		}
902789Sahrens 		txg_suspend(dmu_objset_pool(os));
903789Sahrens 		error = dmu_sync(os, lr->lr_foid, off, &lr->lr_blkoff,
904789Sahrens 		    &lr->lr_blkptr, lr->lr_common.lrc_txg);
905789Sahrens 		txg_resume(dmu_objset_pool(os));
906789Sahrens 	}
9071669Sperrin out:
9081669Sperrin 	zfs_range_unlock(zp, rl);
909789Sahrens 	VN_RELE(ZTOV(zp));
910789Sahrens 	return (error);
911789Sahrens }
912789Sahrens 
913789Sahrens /*ARGSUSED*/
914789Sahrens static int
915789Sahrens zfs_access(vnode_t *vp, int mode, int flags, cred_t *cr)
916789Sahrens {
917789Sahrens 	znode_t *zp = VTOZ(vp);
918789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
919789Sahrens 	int error;
920789Sahrens 
921789Sahrens 	ZFS_ENTER(zfsvfs);
922789Sahrens 	error = zfs_zaccess_rwx(zp, mode, cr);
923789Sahrens 	ZFS_EXIT(zfsvfs);
924789Sahrens 	return (error);
925789Sahrens }
926789Sahrens 
927789Sahrens /*
928789Sahrens  * Lookup an entry in a directory, or an extended attribute directory.
929789Sahrens  * If it exists, return a held vnode reference for it.
930789Sahrens  *
931789Sahrens  *	IN:	dvp	- vnode of directory to search.
932789Sahrens  *		nm	- name of entry to lookup.
933789Sahrens  *		pnp	- full pathname to lookup [UNUSED].
934789Sahrens  *		flags	- LOOKUP_XATTR set if looking for an attribute.
935789Sahrens  *		rdir	- root directory vnode [UNUSED].
936789Sahrens  *		cr	- credentials of caller.
937789Sahrens  *
938789Sahrens  *	OUT:	vpp	- vnode of located entry, NULL if not found.
939789Sahrens  *
940789Sahrens  *	RETURN:	0 if success
941789Sahrens  *		error code if failure
942789Sahrens  *
943789Sahrens  * Timestamps:
944789Sahrens  *	NA
945789Sahrens  */
946789Sahrens /* ARGSUSED */
947789Sahrens static int
948789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
949789Sahrens     int flags, vnode_t *rdir, cred_t *cr)
950789Sahrens {
951789Sahrens 
952789Sahrens 	znode_t *zdp = VTOZ(dvp);
953789Sahrens 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
954789Sahrens 	int	error;
955789Sahrens 
956789Sahrens 	ZFS_ENTER(zfsvfs);
957789Sahrens 
958789Sahrens 	*vpp = NULL;
959789Sahrens 
960789Sahrens 	if (flags & LOOKUP_XATTR) {
961789Sahrens 		/*
962789Sahrens 		 * We don't allow recursive attributes..
963789Sahrens 		 * Maybe someday we will.
964789Sahrens 		 */
965789Sahrens 		if (zdp->z_phys->zp_flags & ZFS_XATTR) {
966789Sahrens 			ZFS_EXIT(zfsvfs);
967789Sahrens 			return (EINVAL);
968789Sahrens 		}
969789Sahrens 
970789Sahrens 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr)) {
971789Sahrens 			ZFS_EXIT(zfsvfs);
972789Sahrens 			return (error);
973789Sahrens 		}
974789Sahrens 
975789Sahrens 		/*
976789Sahrens 		 * Do we have permission to get into attribute directory?
977789Sahrens 		 */
978789Sahrens 
979789Sahrens 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, cr)) {
980789Sahrens 			VN_RELE(*vpp);
981789Sahrens 		}
982789Sahrens 
983789Sahrens 		ZFS_EXIT(zfsvfs);
984789Sahrens 		return (error);
985789Sahrens 	}
986789Sahrens 
9871512Sek110237 	if (dvp->v_type != VDIR) {
9881512Sek110237 		ZFS_EXIT(zfsvfs);
9891460Smarks 		return (ENOTDIR);
9901512Sek110237 	}
9911460Smarks 
992789Sahrens 	/*
993789Sahrens 	 * Check accessibility of directory.
994789Sahrens 	 */
995789Sahrens 
996789Sahrens 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, cr)) {
997789Sahrens 		ZFS_EXIT(zfsvfs);
998789Sahrens 		return (error);
999789Sahrens 	}
1000789Sahrens 
1001789Sahrens 	if ((error = zfs_dirlook(zdp, nm, vpp)) == 0) {
1002789Sahrens 
1003789Sahrens 		/*
1004789Sahrens 		 * Convert device special files
1005789Sahrens 		 */
1006789Sahrens 		if (IS_DEVVP(*vpp)) {
1007789Sahrens 			vnode_t	*svp;
1008789Sahrens 
1009789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1010789Sahrens 			VN_RELE(*vpp);
1011789Sahrens 			if (svp == NULL)
1012789Sahrens 				error = ENOSYS;
1013789Sahrens 			else
1014789Sahrens 				*vpp = svp;
1015789Sahrens 		}
1016789Sahrens 	}
1017789Sahrens 
1018789Sahrens 	ZFS_EXIT(zfsvfs);
1019789Sahrens 	return (error);
1020789Sahrens }
1021789Sahrens 
1022789Sahrens /*
1023789Sahrens  * Attempt to create a new entry in a directory.  If the entry
1024789Sahrens  * already exists, truncate the file if permissible, else return
1025789Sahrens  * an error.  Return the vp of the created or trunc'd file.
1026789Sahrens  *
1027789Sahrens  *	IN:	dvp	- vnode of directory to put new file entry in.
1028789Sahrens  *		name	- name of new file entry.
1029789Sahrens  *		vap	- attributes of new file.
1030789Sahrens  *		excl	- flag indicating exclusive or non-exclusive mode.
1031789Sahrens  *		mode	- mode to open file with.
1032789Sahrens  *		cr	- credentials of caller.
1033789Sahrens  *		flag	- large file flag [UNUSED].
1034789Sahrens  *
1035789Sahrens  *	OUT:	vpp	- vnode of created or trunc'd entry.
1036789Sahrens  *
1037789Sahrens  *	RETURN:	0 if success
1038789Sahrens  *		error code if failure
1039789Sahrens  *
1040789Sahrens  * Timestamps:
1041789Sahrens  *	dvp - ctime|mtime updated if new entry created
1042789Sahrens  *	 vp - ctime|mtime always, atime if new
1043789Sahrens  */
1044789Sahrens /* ARGSUSED */
1045789Sahrens static int
1046789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1047789Sahrens     int mode, vnode_t **vpp, cred_t *cr, int flag)
1048789Sahrens {
1049789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1050789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1051789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1052789Sahrens 	uint64_t	seq = 0;
1053789Sahrens 	objset_t	*os = zfsvfs->z_os;
1054789Sahrens 	zfs_dirlock_t	*dl;
1055789Sahrens 	dmu_tx_t	*tx;
1056789Sahrens 	int		error;
1057789Sahrens 	uint64_t	zoid;
1058789Sahrens 
1059789Sahrens 	ZFS_ENTER(zfsvfs);
1060789Sahrens 
1061789Sahrens top:
1062789Sahrens 	*vpp = NULL;
1063789Sahrens 
1064789Sahrens 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1065789Sahrens 		vap->va_mode &= ~VSVTX;
1066789Sahrens 
1067789Sahrens 	if (*name == '\0') {
1068789Sahrens 		/*
1069789Sahrens 		 * Null component name refers to the directory itself.
1070789Sahrens 		 */
1071789Sahrens 		VN_HOLD(dvp);
1072789Sahrens 		zp = dzp;
1073789Sahrens 		dl = NULL;
1074789Sahrens 		error = 0;
1075789Sahrens 	} else {
1076789Sahrens 		/* possible VN_HOLD(zp) */
1077789Sahrens 		if (error = zfs_dirent_lock(&dl, dzp, name, &zp, 0)) {
1078789Sahrens 			if (strcmp(name, "..") == 0)
1079789Sahrens 				error = EISDIR;
1080789Sahrens 			ZFS_EXIT(zfsvfs);
1081789Sahrens 			return (error);
1082789Sahrens 		}
1083789Sahrens 	}
1084789Sahrens 
1085789Sahrens 	zoid = zp ? zp->z_id : -1ULL;
1086789Sahrens 
1087789Sahrens 	if (zp == NULL) {
1088789Sahrens 		/*
1089789Sahrens 		 * Create a new file object and update the directory
1090789Sahrens 		 * to reference it.
1091789Sahrens 		 */
1092789Sahrens 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
1093789Sahrens 			goto out;
1094789Sahrens 		}
1095789Sahrens 
1096789Sahrens 		/*
1097789Sahrens 		 * We only support the creation of regular files in
1098789Sahrens 		 * extended attribute directories.
1099789Sahrens 		 */
1100789Sahrens 		if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1101789Sahrens 		    (vap->va_type != VREG)) {
1102789Sahrens 			error = EINVAL;
1103789Sahrens 			goto out;
1104789Sahrens 		}
1105789Sahrens 
1106789Sahrens 		tx = dmu_tx_create(os);
1107789Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1108789Sahrens 		dmu_tx_hold_bonus(tx, dzp->z_id);
11091544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1110789Sahrens 		if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
1111789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1112789Sahrens 			    0, SPA_MAXBLOCKSIZE);
1113789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
1114789Sahrens 		if (error) {
1115789Sahrens 			zfs_dirent_unlock(dl);
1116789Sahrens 			if (error == ERESTART &&
1117789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
1118*2113Sahrens 				dmu_tx_wait(tx);
1119*2113Sahrens 				dmu_tx_abort(tx);
1120789Sahrens 				goto top;
1121789Sahrens 			}
1122*2113Sahrens 			dmu_tx_abort(tx);
1123789Sahrens 			ZFS_EXIT(zfsvfs);
1124789Sahrens 			return (error);
1125789Sahrens 		}
1126789Sahrens 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
1127789Sahrens 		ASSERT(zp->z_id == zoid);
1128789Sahrens 		(void) zfs_link_create(dl, zp, tx, ZNEW);
1129789Sahrens 		seq = zfs_log_create(zilog, tx, TX_CREATE, dzp, zp, name);
1130789Sahrens 		dmu_tx_commit(tx);
1131789Sahrens 	} else {
1132789Sahrens 		/*
1133789Sahrens 		 * A directory entry already exists for this name.
1134789Sahrens 		 */
1135789Sahrens 		/*
1136789Sahrens 		 * Can't truncate an existing file if in exclusive mode.
1137789Sahrens 		 */
1138789Sahrens 		if (excl == EXCL) {
1139789Sahrens 			error = EEXIST;
1140789Sahrens 			goto out;
1141789Sahrens 		}
1142789Sahrens 		/*
1143789Sahrens 		 * Can't open a directory for writing.
1144789Sahrens 		 */
1145789Sahrens 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1146789Sahrens 			error = EISDIR;
1147789Sahrens 			goto out;
1148789Sahrens 		}
1149789Sahrens 		/*
1150789Sahrens 		 * Verify requested access to file.
1151789Sahrens 		 */
1152789Sahrens 		if (mode && (error = zfs_zaccess_rwx(zp, mode, cr))) {
1153789Sahrens 			goto out;
1154789Sahrens 		}
1155789Sahrens 
1156789Sahrens 		mutex_enter(&dzp->z_lock);
1157789Sahrens 		dzp->z_seq++;
1158789Sahrens 		mutex_exit(&dzp->z_lock);
1159789Sahrens 
11601878Smaybee 		/*
11611878Smaybee 		 * Truncate regular files if requested.
11621878Smaybee 		 */
11631878Smaybee 		if ((ZTOV(zp)->v_type == VREG) &&
11641878Smaybee 		    (zp->z_phys->zp_size != 0) &&
1165789Sahrens 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
11661878Smaybee 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
11671878Smaybee 			if (error == ERESTART &&
11681878Smaybee 			    zfsvfs->z_assign == TXG_NOWAIT) {
1169*2113Sahrens 				/* NB: we already did dmu_tx_wait() */
11701878Smaybee 				zfs_dirent_unlock(dl);
11711878Smaybee 				goto top;
1172789Sahrens 			}
1173789Sahrens 		}
1174789Sahrens 	}
1175789Sahrens out:
1176789Sahrens 
1177789Sahrens 	if (dl)
1178789Sahrens 		zfs_dirent_unlock(dl);
1179789Sahrens 
1180789Sahrens 	if (error) {
1181789Sahrens 		if (zp)
1182789Sahrens 			VN_RELE(ZTOV(zp));
1183789Sahrens 	} else {
1184789Sahrens 		*vpp = ZTOV(zp);
1185789Sahrens 		/*
1186789Sahrens 		 * If vnode is for a device return a specfs vnode instead.
1187789Sahrens 		 */
1188789Sahrens 		if (IS_DEVVP(*vpp)) {
1189789Sahrens 			struct vnode *svp;
1190789Sahrens 
1191789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1192789Sahrens 			VN_RELE(*vpp);
1193789Sahrens 			if (svp == NULL) {
1194789Sahrens 				error = ENOSYS;
1195789Sahrens 			}
1196789Sahrens 			*vpp = svp;
1197789Sahrens 		}
1198789Sahrens 	}
1199789Sahrens 
1200789Sahrens 	zil_commit(zilog, seq, 0);
1201789Sahrens 
1202789Sahrens 	ZFS_EXIT(zfsvfs);
1203789Sahrens 	return (error);
1204789Sahrens }
1205789Sahrens 
1206789Sahrens /*
1207789Sahrens  * Remove an entry from a directory.
1208789Sahrens  *
1209789Sahrens  *	IN:	dvp	- vnode of directory to remove entry from.
1210789Sahrens  *		name	- name of entry to remove.
1211789Sahrens  *		cr	- credentials of caller.
1212789Sahrens  *
1213789Sahrens  *	RETURN:	0 if success
1214789Sahrens  *		error code if failure
1215789Sahrens  *
1216789Sahrens  * Timestamps:
1217789Sahrens  *	dvp - ctime|mtime
1218789Sahrens  *	 vp - ctime (if nlink > 0)
1219789Sahrens  */
1220789Sahrens static int
1221789Sahrens zfs_remove(vnode_t *dvp, char *name, cred_t *cr)
1222789Sahrens {
1223789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1224789Sahrens 	znode_t		*xzp = NULL;
1225789Sahrens 	vnode_t		*vp;
1226789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1227789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1228789Sahrens 	uint64_t	seq = 0;
1229789Sahrens 	uint64_t	acl_obj, xattr_obj;
1230789Sahrens 	zfs_dirlock_t	*dl;
1231789Sahrens 	dmu_tx_t	*tx;
1232789Sahrens 	int		may_delete_now, delete_now = FALSE;
1233789Sahrens 	int		reaped;
1234789Sahrens 	int		error;
1235789Sahrens 
1236789Sahrens 	ZFS_ENTER(zfsvfs);
1237789Sahrens 
1238789Sahrens top:
1239789Sahrens 	/*
1240789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1241789Sahrens 	 */
1242789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
1243789Sahrens 		ZFS_EXIT(zfsvfs);
1244789Sahrens 		return (error);
1245789Sahrens 	}
1246789Sahrens 
1247789Sahrens 	vp = ZTOV(zp);
1248789Sahrens 
1249789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1250789Sahrens 		goto out;
1251789Sahrens 	}
1252789Sahrens 
1253789Sahrens 	/*
1254789Sahrens 	 * Need to use rmdir for removing directories.
1255789Sahrens 	 */
1256789Sahrens 	if (vp->v_type == VDIR) {
1257789Sahrens 		error = EPERM;
1258789Sahrens 		goto out;
1259789Sahrens 	}
1260789Sahrens 
1261789Sahrens 	vnevent_remove(vp);
1262789Sahrens 
12631484Sek110237 	dnlc_remove(dvp, name);
12641484Sek110237 
1265789Sahrens 	mutex_enter(&vp->v_lock);
1266789Sahrens 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1267789Sahrens 	mutex_exit(&vp->v_lock);
1268789Sahrens 
1269789Sahrens 	/*
1270789Sahrens 	 * We may delete the znode now, or we may put it on the delete queue;
1271789Sahrens 	 * it depends on whether we're the last link, and on whether there are
1272789Sahrens 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1273789Sahrens 	 * allow for either case.
1274789Sahrens 	 */
1275789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
12761544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1277789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
1278789Sahrens 	if (may_delete_now)
1279789Sahrens 		dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
1280789Sahrens 
1281789Sahrens 	/* are there any extended attributes? */
1282789Sahrens 	if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1283789Sahrens 		/*
1284789Sahrens 		 * XXX - There is a possibility that the delete
1285789Sahrens 		 * of the parent file could succeed, but then we get
1286789Sahrens 		 * an ENOSPC when we try to delete the xattrs...
1287789Sahrens 		 * so we would need to re-try the deletes periodically
1288789Sahrens 		 */
1289789Sahrens 		/* XXX - do we need this if we are deleting? */
1290789Sahrens 		dmu_tx_hold_bonus(tx, xattr_obj);
1291789Sahrens 	}
1292789Sahrens 
1293789Sahrens 	/* are there any additional acls */
1294789Sahrens 	if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1295789Sahrens 	    may_delete_now)
1296789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1297789Sahrens 
1298789Sahrens 	/* charge as an update -- would be nice not to charge at all */
12991544Seschrock 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
1300789Sahrens 
1301789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1302789Sahrens 	if (error) {
1303789Sahrens 		zfs_dirent_unlock(dl);
1304789Sahrens 		VN_RELE(vp);
1305789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1306*2113Sahrens 			dmu_tx_wait(tx);
1307*2113Sahrens 			dmu_tx_abort(tx);
1308789Sahrens 			goto top;
1309789Sahrens 		}
1310*2113Sahrens 		dmu_tx_abort(tx);
1311789Sahrens 		ZFS_EXIT(zfsvfs);
1312789Sahrens 		return (error);
1313789Sahrens 	}
1314789Sahrens 
1315789Sahrens 	/*
1316789Sahrens 	 * Remove the directory entry.
1317789Sahrens 	 */
1318789Sahrens 	error = zfs_link_destroy(dl, zp, tx, 0, &reaped);
1319789Sahrens 
1320789Sahrens 	if (error) {
1321789Sahrens 		dmu_tx_commit(tx);
1322789Sahrens 		goto out;
1323789Sahrens 	}
1324789Sahrens 
1325789Sahrens 	if (reaped) {
1326789Sahrens 		mutex_enter(&vp->v_lock);
1327789Sahrens 		delete_now = may_delete_now &&
1328789Sahrens 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1329789Sahrens 		    zp->z_phys->zp_xattr == xattr_obj &&
1330789Sahrens 		    zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1331789Sahrens 		mutex_exit(&vp->v_lock);
1332789Sahrens 	}
1333789Sahrens 
1334789Sahrens 	if (delete_now) {
1335789Sahrens 		if (zp->z_phys->zp_xattr) {
1336789Sahrens 			error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1337789Sahrens 			ASSERT3U(error, ==, 0);
1338789Sahrens 			ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1339789Sahrens 			dmu_buf_will_dirty(xzp->z_dbuf, tx);
1340789Sahrens 			mutex_enter(&xzp->z_lock);
1341789Sahrens 			xzp->z_reap = 1;
1342789Sahrens 			xzp->z_phys->zp_links = 0;
1343789Sahrens 			mutex_exit(&xzp->z_lock);
1344789Sahrens 			zfs_dq_add(xzp, tx);
1345789Sahrens 			zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1346789Sahrens 		}
1347789Sahrens 		mutex_enter(&zp->z_lock);
1348789Sahrens 		mutex_enter(&vp->v_lock);
1349789Sahrens 		vp->v_count--;
1350789Sahrens 		ASSERT3U(vp->v_count, ==, 0);
1351789Sahrens 		mutex_exit(&vp->v_lock);
1352789Sahrens 		zp->z_active = 0;
1353789Sahrens 		mutex_exit(&zp->z_lock);
1354789Sahrens 		zfs_znode_delete(zp, tx);
1355789Sahrens 		VFS_RELE(zfsvfs->z_vfs);
1356789Sahrens 	} else if (reaped) {
1357789Sahrens 		zfs_dq_add(zp, tx);
1358789Sahrens 	}
1359789Sahrens 
1360789Sahrens 	seq = zfs_log_remove(zilog, tx, TX_REMOVE, dzp, name);
1361789Sahrens 
1362789Sahrens 	dmu_tx_commit(tx);
1363789Sahrens out:
1364789Sahrens 	zfs_dirent_unlock(dl);
1365789Sahrens 
1366789Sahrens 	if (!delete_now) {
1367789Sahrens 		VN_RELE(vp);
1368789Sahrens 	} else if (xzp) {
1369789Sahrens 		/* this rele delayed to prevent nesting transactions */
1370789Sahrens 		VN_RELE(ZTOV(xzp));
1371789Sahrens 	}
1372789Sahrens 
1373789Sahrens 	zil_commit(zilog, seq, 0);
1374789Sahrens 
1375789Sahrens 	ZFS_EXIT(zfsvfs);
1376789Sahrens 	return (error);
1377789Sahrens }
1378789Sahrens 
1379789Sahrens /*
1380789Sahrens  * Create a new directory and insert it into dvp using the name
1381789Sahrens  * provided.  Return a pointer to the inserted directory.
1382789Sahrens  *
1383789Sahrens  *	IN:	dvp	- vnode of directory to add subdir to.
1384789Sahrens  *		dirname	- name of new directory.
1385789Sahrens  *		vap	- attributes of new directory.
1386789Sahrens  *		cr	- credentials of caller.
1387789Sahrens  *
1388789Sahrens  *	OUT:	vpp	- vnode of created directory.
1389789Sahrens  *
1390789Sahrens  *	RETURN:	0 if success
1391789Sahrens  *		error code if failure
1392789Sahrens  *
1393789Sahrens  * Timestamps:
1394789Sahrens  *	dvp - ctime|mtime updated
1395789Sahrens  *	 vp - ctime|mtime|atime updated
1396789Sahrens  */
1397789Sahrens static int
1398789Sahrens zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr)
1399789Sahrens {
1400789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1401789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1402789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1403789Sahrens 	uint64_t	seq = 0;
1404789Sahrens 	zfs_dirlock_t	*dl;
1405789Sahrens 	uint64_t	zoid = 0;
1406789Sahrens 	dmu_tx_t	*tx;
1407789Sahrens 	int		error;
1408789Sahrens 
1409789Sahrens 	ASSERT(vap->va_type == VDIR);
1410789Sahrens 
1411789Sahrens 	ZFS_ENTER(zfsvfs);
1412789Sahrens 
1413789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1414789Sahrens 		ZFS_EXIT(zfsvfs);
1415789Sahrens 		return (EINVAL);
1416789Sahrens 	}
1417789Sahrens top:
1418789Sahrens 	*vpp = NULL;
1419789Sahrens 
1420789Sahrens 	/*
1421789Sahrens 	 * First make sure the new directory doesn't exist.
1422789Sahrens 	 */
1423789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, ZNEW)) {
1424789Sahrens 		ZFS_EXIT(zfsvfs);
1425789Sahrens 		return (error);
1426789Sahrens 	}
1427789Sahrens 
14281231Smarks 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, cr)) {
14291231Smarks 		zfs_dirent_unlock(dl);
14301231Smarks 		ZFS_EXIT(zfsvfs);
14311231Smarks 		return (error);
14321231Smarks 	}
14331231Smarks 
1434789Sahrens 	/*
1435789Sahrens 	 * Add a new entry to the directory.
1436789Sahrens 	 */
1437789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
14381544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
14391544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1440789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
1441789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1442789Sahrens 		    0, SPA_MAXBLOCKSIZE);
1443789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1444789Sahrens 	if (error) {
1445789Sahrens 		zfs_dirent_unlock(dl);
1446789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1447*2113Sahrens 			dmu_tx_wait(tx);
1448*2113Sahrens 			dmu_tx_abort(tx);
1449789Sahrens 			goto top;
1450789Sahrens 		}
1451*2113Sahrens 		dmu_tx_abort(tx);
1452789Sahrens 		ZFS_EXIT(zfsvfs);
1453789Sahrens 		return (error);
1454789Sahrens 	}
1455789Sahrens 
1456789Sahrens 	/*
1457789Sahrens 	 * Create new node.
1458789Sahrens 	 */
1459789Sahrens 	zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
1460789Sahrens 
1461789Sahrens 	/*
1462789Sahrens 	 * Now put new name in parent dir.
1463789Sahrens 	 */
1464789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1465789Sahrens 
1466789Sahrens 	*vpp = ZTOV(zp);
1467789Sahrens 
1468789Sahrens 	seq = zfs_log_create(zilog, tx, TX_MKDIR, dzp, zp, dirname);
1469789Sahrens 	dmu_tx_commit(tx);
1470789Sahrens 
1471789Sahrens 	zfs_dirent_unlock(dl);
1472789Sahrens 
1473789Sahrens 	zil_commit(zilog, seq, 0);
1474789Sahrens 
1475789Sahrens 	ZFS_EXIT(zfsvfs);
1476789Sahrens 	return (0);
1477789Sahrens }
1478789Sahrens 
1479789Sahrens /*
1480789Sahrens  * Remove a directory subdir entry.  If the current working
1481789Sahrens  * directory is the same as the subdir to be removed, the
1482789Sahrens  * remove will fail.
1483789Sahrens  *
1484789Sahrens  *	IN:	dvp	- vnode of directory to remove from.
1485789Sahrens  *		name	- name of directory to be removed.
1486789Sahrens  *		cwd	- vnode of current working directory.
1487789Sahrens  *		cr	- credentials of caller.
1488789Sahrens  *
1489789Sahrens  *	RETURN:	0 if success
1490789Sahrens  *		error code if failure
1491789Sahrens  *
1492789Sahrens  * Timestamps:
1493789Sahrens  *	dvp - ctime|mtime updated
1494789Sahrens  */
1495789Sahrens static int
1496789Sahrens zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr)
1497789Sahrens {
1498789Sahrens 	znode_t		*dzp = VTOZ(dvp);
1499789Sahrens 	znode_t		*zp;
1500789Sahrens 	vnode_t		*vp;
1501789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1502789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1503789Sahrens 	uint64_t	seq = 0;
1504789Sahrens 	zfs_dirlock_t	*dl;
1505789Sahrens 	dmu_tx_t	*tx;
1506789Sahrens 	int		error;
1507789Sahrens 
1508789Sahrens 	ZFS_ENTER(zfsvfs);
1509789Sahrens 
1510789Sahrens top:
1511789Sahrens 	zp = NULL;
1512789Sahrens 
1513789Sahrens 	/*
1514789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1515789Sahrens 	 */
1516789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
1517789Sahrens 		ZFS_EXIT(zfsvfs);
1518789Sahrens 		return (error);
1519789Sahrens 	}
1520789Sahrens 
1521789Sahrens 	vp = ZTOV(zp);
1522789Sahrens 
1523789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1524789Sahrens 		goto out;
1525789Sahrens 	}
1526789Sahrens 
1527789Sahrens 	if (vp->v_type != VDIR) {
1528789Sahrens 		error = ENOTDIR;
1529789Sahrens 		goto out;
1530789Sahrens 	}
1531789Sahrens 
1532789Sahrens 	if (vp == cwd) {
1533789Sahrens 		error = EINVAL;
1534789Sahrens 		goto out;
1535789Sahrens 	}
1536789Sahrens 
1537789Sahrens 	vnevent_rmdir(vp);
1538789Sahrens 
1539789Sahrens 	/*
1540789Sahrens 	 * Grab a lock on the parent pointer make sure we play well
1541789Sahrens 	 * with the treewalk and directory rename code.
1542789Sahrens 	 */
1543789Sahrens 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1544789Sahrens 
1545789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
15461544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1547789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
15481544Seschrock 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
1549789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1550789Sahrens 	if (error) {
1551789Sahrens 		rw_exit(&zp->z_parent_lock);
1552789Sahrens 		zfs_dirent_unlock(dl);
1553789Sahrens 		VN_RELE(vp);
1554789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1555*2113Sahrens 			dmu_tx_wait(tx);
1556*2113Sahrens 			dmu_tx_abort(tx);
1557789Sahrens 			goto top;
1558789Sahrens 		}
1559*2113Sahrens 		dmu_tx_abort(tx);
1560789Sahrens 		ZFS_EXIT(zfsvfs);
1561789Sahrens 		return (error);
1562789Sahrens 	}
1563789Sahrens 
1564789Sahrens 	error = zfs_link_destroy(dl, zp, tx, 0, NULL);
1565789Sahrens 
1566789Sahrens 	if (error == 0)
1567789Sahrens 		seq = zfs_log_remove(zilog, tx, TX_RMDIR, dzp, name);
1568789Sahrens 
1569789Sahrens 	dmu_tx_commit(tx);
1570789Sahrens 
1571789Sahrens 	rw_exit(&zp->z_parent_lock);
1572789Sahrens out:
1573789Sahrens 	zfs_dirent_unlock(dl);
1574789Sahrens 
1575789Sahrens 	VN_RELE(vp);
1576789Sahrens 
1577789Sahrens 	zil_commit(zilog, seq, 0);
1578789Sahrens 
1579789Sahrens 	ZFS_EXIT(zfsvfs);
1580789Sahrens 	return (error);
1581789Sahrens }
1582789Sahrens 
1583789Sahrens /*
1584789Sahrens  * Read as many directory entries as will fit into the provided
1585789Sahrens  * buffer from the given directory cursor position (specified in
1586789Sahrens  * the uio structure.
1587789Sahrens  *
1588789Sahrens  *	IN:	vp	- vnode of directory to read.
1589789Sahrens  *		uio	- structure supplying read location, range info,
1590789Sahrens  *			  and return buffer.
1591789Sahrens  *		cr	- credentials of caller.
1592789Sahrens  *
1593789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
1594789Sahrens  *		eofp	- set to true if end-of-file detected.
1595789Sahrens  *
1596789Sahrens  *	RETURN:	0 if success
1597789Sahrens  *		error code if failure
1598789Sahrens  *
1599789Sahrens  * Timestamps:
1600789Sahrens  *	vp - atime updated
1601789Sahrens  *
1602789Sahrens  * Note that the low 4 bits of the cookie returned by zap is always zero.
1603789Sahrens  * This allows us to use the low range for "special" directory entries:
1604789Sahrens  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
1605789Sahrens  * we use the offset 2 for the '.zfs' directory.
1606789Sahrens  */
1607789Sahrens /* ARGSUSED */
1608789Sahrens static int
1609789Sahrens zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp)
1610789Sahrens {
1611789Sahrens 	znode_t		*zp = VTOZ(vp);
1612789Sahrens 	iovec_t		*iovp;
1613789Sahrens 	dirent64_t	*odp;
1614789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1615869Sperrin 	objset_t	*os;
1616789Sahrens 	caddr_t		outbuf;
1617789Sahrens 	size_t		bufsize;
1618789Sahrens 	zap_cursor_t	zc;
1619789Sahrens 	zap_attribute_t	zap;
1620789Sahrens 	uint_t		bytes_wanted;
1621789Sahrens 	ushort_t	this_reclen;
1622789Sahrens 	uint64_t	offset; /* must be unsigned; checks for < 1 */
1623789Sahrens 	off64_t		*next;
1624789Sahrens 	int		local_eof;
1625869Sperrin 	int		outcount;
1626869Sperrin 	int		error;
1627869Sperrin 	uint8_t		prefetch;
1628789Sahrens 
1629789Sahrens 	ZFS_ENTER(zfsvfs);
1630789Sahrens 
1631789Sahrens 	/*
1632789Sahrens 	 * If we are not given an eof variable,
1633789Sahrens 	 * use a local one.
1634789Sahrens 	 */
1635789Sahrens 	if (eofp == NULL)
1636789Sahrens 		eofp = &local_eof;
1637789Sahrens 
1638789Sahrens 	/*
1639789Sahrens 	 * Check for valid iov_len.
1640789Sahrens 	 */
1641789Sahrens 	if (uio->uio_iov->iov_len <= 0) {
1642789Sahrens 		ZFS_EXIT(zfsvfs);
1643789Sahrens 		return (EINVAL);
1644789Sahrens 	}
1645789Sahrens 
1646789Sahrens 	/*
1647789Sahrens 	 * Quit if directory has been removed (posix)
1648789Sahrens 	 */
1649789Sahrens 	if ((*eofp = zp->z_reap) != 0) {
1650789Sahrens 		ZFS_EXIT(zfsvfs);
1651789Sahrens 		return (0);
1652789Sahrens 	}
1653789Sahrens 
1654869Sperrin 	error = 0;
1655869Sperrin 	os = zfsvfs->z_os;
1656869Sperrin 	offset = uio->uio_loffset;
1657869Sperrin 	prefetch = zp->z_zn_prefetch;
1658869Sperrin 
1659789Sahrens 	/*
1660789Sahrens 	 * Initialize the iterator cursor.
1661789Sahrens 	 */
1662789Sahrens 	if (offset <= 3) {
1663789Sahrens 		/*
1664789Sahrens 		 * Start iteration from the beginning of the directory.
1665789Sahrens 		 */
1666869Sperrin 		zap_cursor_init(&zc, os, zp->z_id);
1667789Sahrens 	} else {
1668789Sahrens 		/*
1669789Sahrens 		 * The offset is a serialized cursor.
1670789Sahrens 		 */
1671869Sperrin 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1672789Sahrens 	}
1673789Sahrens 
1674789Sahrens 	/*
1675789Sahrens 	 * Get space to change directory entries into fs independent format.
1676789Sahrens 	 */
1677789Sahrens 	iovp = uio->uio_iov;
1678789Sahrens 	bytes_wanted = iovp->iov_len;
1679789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
1680789Sahrens 		bufsize = bytes_wanted;
1681789Sahrens 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
1682789Sahrens 		odp = (struct dirent64 *)outbuf;
1683789Sahrens 	} else {
1684789Sahrens 		bufsize = bytes_wanted;
1685789Sahrens 		odp = (struct dirent64 *)iovp->iov_base;
1686789Sahrens 	}
1687789Sahrens 
1688789Sahrens 	/*
1689789Sahrens 	 * Transform to file-system independent format
1690789Sahrens 	 */
1691789Sahrens 	outcount = 0;
1692789Sahrens 	while (outcount < bytes_wanted) {
1693789Sahrens 		/*
1694789Sahrens 		 * Special case `.', `..', and `.zfs'.
1695789Sahrens 		 */
1696789Sahrens 		if (offset == 0) {
1697789Sahrens 			(void) strcpy(zap.za_name, ".");
1698789Sahrens 			zap.za_first_integer = zp->z_id;
1699789Sahrens 			this_reclen = DIRENT64_RECLEN(1);
1700789Sahrens 		} else if (offset == 1) {
1701789Sahrens 			(void) strcpy(zap.za_name, "..");
1702789Sahrens 			zap.za_first_integer = zp->z_phys->zp_parent;
1703789Sahrens 			this_reclen = DIRENT64_RECLEN(2);
1704789Sahrens 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
1705789Sahrens 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
1706789Sahrens 			zap.za_first_integer = ZFSCTL_INO_ROOT;
1707789Sahrens 			this_reclen =
1708789Sahrens 			    DIRENT64_RECLEN(sizeof (ZFS_CTLDIR_NAME) - 1);
1709789Sahrens 		} else {
1710789Sahrens 			/*
1711789Sahrens 			 * Grab next entry.
1712789Sahrens 			 */
1713789Sahrens 			if (error = zap_cursor_retrieve(&zc, &zap)) {
1714789Sahrens 				if ((*eofp = (error == ENOENT)) != 0)
1715789Sahrens 					break;
1716789Sahrens 				else
1717789Sahrens 					goto update;
1718789Sahrens 			}
1719789Sahrens 
1720789Sahrens 			if (zap.za_integer_length != 8 ||
1721789Sahrens 			    zap.za_num_integers != 1) {
1722789Sahrens 				cmn_err(CE_WARN, "zap_readdir: bad directory "
1723789Sahrens 				    "entry, obj = %lld, offset = %lld\n",
1724789Sahrens 				    (u_longlong_t)zp->z_id,
1725789Sahrens 				    (u_longlong_t)offset);
1726789Sahrens 				error = ENXIO;
1727789Sahrens 				goto update;
1728789Sahrens 			}
1729789Sahrens 			this_reclen = DIRENT64_RECLEN(strlen(zap.za_name));
1730789Sahrens 		}
1731789Sahrens 
1732789Sahrens 		/*
1733789Sahrens 		 * Will this entry fit in the buffer?
1734789Sahrens 		 */
1735789Sahrens 		if (outcount + this_reclen > bufsize) {
1736789Sahrens 			/*
1737789Sahrens 			 * Did we manage to fit anything in the buffer?
1738789Sahrens 			 */
1739789Sahrens 			if (!outcount) {
1740789Sahrens 				error = EINVAL;
1741789Sahrens 				goto update;
1742789Sahrens 			}
1743789Sahrens 			break;
1744789Sahrens 		}
1745789Sahrens 		/*
1746789Sahrens 		 * Add this entry:
1747789Sahrens 		 */
1748789Sahrens 		odp->d_ino = (ino64_t)zap.za_first_integer;
1749789Sahrens 		odp->d_reclen = (ushort_t)this_reclen;
1750789Sahrens 		/* NOTE: d_off is the offset for the *next* entry */
1751789Sahrens 		next = &(odp->d_off);
1752789Sahrens 		(void) strncpy(odp->d_name, zap.za_name,
1753789Sahrens 		    DIRENT64_NAMELEN(this_reclen));
1754789Sahrens 		outcount += this_reclen;
1755789Sahrens 		odp = (dirent64_t *)((intptr_t)odp + this_reclen);
1756789Sahrens 
1757789Sahrens 		ASSERT(outcount <= bufsize);
1758789Sahrens 
1759789Sahrens 		/* Prefetch znode */
1760869Sperrin 		if (prefetch)
1761869Sperrin 			dmu_prefetch(os, zap.za_first_integer, 0, 0);
1762789Sahrens 
1763789Sahrens 		/*
1764789Sahrens 		 * Move to the next entry, fill in the previous offset.
1765789Sahrens 		 */
1766789Sahrens 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
1767789Sahrens 			zap_cursor_advance(&zc);
1768789Sahrens 			offset = zap_cursor_serialize(&zc);
1769789Sahrens 		} else {
1770789Sahrens 			offset += 1;
1771789Sahrens 		}
1772789Sahrens 		*next = offset;
1773789Sahrens 	}
1774869Sperrin 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
1775789Sahrens 
1776789Sahrens 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
1777789Sahrens 		iovp->iov_base += outcount;
1778789Sahrens 		iovp->iov_len -= outcount;
1779789Sahrens 		uio->uio_resid -= outcount;
1780789Sahrens 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
1781789Sahrens 		/*
1782789Sahrens 		 * Reset the pointer.
1783789Sahrens 		 */
1784789Sahrens 		offset = uio->uio_loffset;
1785789Sahrens 	}
1786789Sahrens 
1787789Sahrens update:
1788885Sahrens 	zap_cursor_fini(&zc);
1789789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1790789Sahrens 		kmem_free(outbuf, bufsize);
1791789Sahrens 
1792789Sahrens 	if (error == ENOENT)
1793789Sahrens 		error = 0;
1794789Sahrens 
1795789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
1796789Sahrens 
1797789Sahrens 	uio->uio_loffset = offset;
1798789Sahrens 	ZFS_EXIT(zfsvfs);
1799789Sahrens 	return (error);
1800789Sahrens }
1801789Sahrens 
1802789Sahrens static int
1803789Sahrens zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr)
1804789Sahrens {
1805789Sahrens 	znode_t	*zp = VTOZ(vp);
1806789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1807789Sahrens 
18081773Seschrock 	/*
18091773Seschrock 	 * Regardless of whether this is required for standards conformance,
18101773Seschrock 	 * this is the logical behavior when fsync() is called on a file with
18111773Seschrock 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
18121773Seschrock 	 * going to be pushed out as part of the zil_commit().
18131773Seschrock 	 */
18141773Seschrock 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
18151773Seschrock 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
18161773Seschrock 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr);
18171773Seschrock 
1818789Sahrens 	ZFS_ENTER(zfsvfs);
1819789Sahrens 	zil_commit(zfsvfs->z_log, zp->z_last_itx, FSYNC);
1820789Sahrens 	ZFS_EXIT(zfsvfs);
1821789Sahrens 	return (0);
1822789Sahrens }
1823789Sahrens 
1824789Sahrens /*
1825789Sahrens  * Get the requested file attributes and place them in the provided
1826789Sahrens  * vattr structure.
1827789Sahrens  *
1828789Sahrens  *	IN:	vp	- vnode of file.
1829789Sahrens  *		vap	- va_mask identifies requested attributes.
1830789Sahrens  *		flags	- [UNUSED]
1831789Sahrens  *		cr	- credentials of caller.
1832789Sahrens  *
1833789Sahrens  *	OUT:	vap	- attribute values.
1834789Sahrens  *
1835789Sahrens  *	RETURN:	0 (always succeeds)
1836789Sahrens  */
1837789Sahrens /* ARGSUSED */
1838789Sahrens static int
1839789Sahrens zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
1840789Sahrens {
1841789Sahrens 	znode_t *zp = VTOZ(vp);
1842789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1843789Sahrens 	znode_phys_t *pzp = zp->z_phys;
1844789Sahrens 	int	error;
1845789Sahrens 
1846789Sahrens 	ZFS_ENTER(zfsvfs);
1847789Sahrens 
1848789Sahrens 	/*
1849789Sahrens 	 * Return all attributes.  It's cheaper to provide the answer
1850789Sahrens 	 * than to determine whether we were asked the question.
1851789Sahrens 	 */
1852789Sahrens 	mutex_enter(&zp->z_lock);
1853789Sahrens 
1854789Sahrens 	vap->va_type = vp->v_type;
1855789Sahrens 	vap->va_mode = pzp->zp_mode & MODEMASK;
1856789Sahrens 	vap->va_uid = zp->z_phys->zp_uid;
1857789Sahrens 	vap->va_gid = zp->z_phys->zp_gid;
1858789Sahrens 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
1859789Sahrens 	vap->va_nodeid = zp->z_id;
1860789Sahrens 	vap->va_nlink = MIN(pzp->zp_links, UINT32_MAX);	/* nlink_t limit! */
1861789Sahrens 	vap->va_size = pzp->zp_size;
18621816Smarks 	vap->va_rdev = vp->v_rdev;
1863789Sahrens 	vap->va_seq = zp->z_seq;
1864789Sahrens 
1865789Sahrens 	ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
1866789Sahrens 	ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
1867789Sahrens 	ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
1868789Sahrens 
1869789Sahrens 	/*
1870905Smarks 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
1871905Smarks 	 * Also, if we are the owner don't bother, since owner should
1872905Smarks 	 * always be allowed to read basic attributes of file.
1873789Sahrens 	 */
1874905Smarks 	if (!(zp->z_phys->zp_flags & ZFS_ACL_TRIVIAL) &&
1875905Smarks 	    (zp->z_phys->zp_uid != crgetuid(cr))) {
1876905Smarks 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, cr)) {
1877789Sahrens 			mutex_exit(&zp->z_lock);
1878789Sahrens 			ZFS_EXIT(zfsvfs);
1879789Sahrens 			return (error);
1880789Sahrens 		}
1881789Sahrens 	}
1882789Sahrens 
1883789Sahrens 	mutex_exit(&zp->z_lock);
1884789Sahrens 
1885789Sahrens 	dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks);
1886789Sahrens 
1887789Sahrens 	if (zp->z_blksz == 0) {
1888789Sahrens 		/*
1889789Sahrens 		 * Block size hasn't been set; suggest maximal I/O transfers.
1890789Sahrens 		 */
1891789Sahrens 		vap->va_blksize = zfsvfs->z_max_blksz;
1892789Sahrens 	}
1893789Sahrens 
1894789Sahrens 	ZFS_EXIT(zfsvfs);
1895789Sahrens 	return (0);
1896789Sahrens }
1897789Sahrens 
1898789Sahrens /*
1899789Sahrens  * Set the file attributes to the values contained in the
1900789Sahrens  * vattr structure.
1901789Sahrens  *
1902789Sahrens  *	IN:	vp	- vnode of file to be modified.
1903789Sahrens  *		vap	- new attribute values.
1904789Sahrens  *		flags	- ATTR_UTIME set if non-default time values provided.
1905789Sahrens  *		cr	- credentials of caller.
1906789Sahrens  *
1907789Sahrens  *	RETURN:	0 if success
1908789Sahrens  *		error code if failure
1909789Sahrens  *
1910789Sahrens  * Timestamps:
1911789Sahrens  *	vp - ctime updated, mtime updated if size changed.
1912789Sahrens  */
1913789Sahrens /* ARGSUSED */
1914789Sahrens static int
1915789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1916789Sahrens 	caller_context_t *ct)
1917789Sahrens {
1918789Sahrens 	struct znode	*zp = VTOZ(vp);
1919789Sahrens 	znode_phys_t	*pzp = zp->z_phys;
1920789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1921789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1922789Sahrens 	uint64_t	seq = 0;
1923789Sahrens 	dmu_tx_t	*tx;
19241878Smaybee 	vattr_t		oldva;
1925789Sahrens 	uint_t		mask = vap->va_mask;
19261878Smaybee 	uint_t		saved_mask;
19271115Smarks 	int		trim_mask = FALSE;
1928789Sahrens 	uint64_t	new_mode;
19291231Smarks 	znode_t		*attrzp;
1930789Sahrens 	int		need_policy = FALSE;
1931789Sahrens 	int		err;
1932789Sahrens 
1933789Sahrens 	if (mask == 0)
1934789Sahrens 		return (0);
1935789Sahrens 
1936789Sahrens 	if (mask & AT_NOSET)
1937789Sahrens 		return (EINVAL);
1938789Sahrens 
1939789Sahrens 	if (mask & AT_SIZE && vp->v_type == VDIR)
1940789Sahrens 		return (EISDIR);
1941789Sahrens 
19421394Smarks 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO)
19431308Smarks 		return (EINVAL);
19441308Smarks 
1945789Sahrens 	ZFS_ENTER(zfsvfs);
1946789Sahrens 
1947789Sahrens top:
19481231Smarks 	attrzp = NULL;
1949789Sahrens 
1950789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
1951789Sahrens 		ZFS_EXIT(zfsvfs);
1952789Sahrens 		return (EROFS);
1953789Sahrens 	}
1954789Sahrens 
1955789Sahrens 	/*
1956789Sahrens 	 * First validate permissions
1957789Sahrens 	 */
1958789Sahrens 
1959789Sahrens 	if (mask & AT_SIZE) {
1960789Sahrens 		err = zfs_zaccess(zp, ACE_WRITE_DATA, cr);
1961789Sahrens 		if (err) {
1962789Sahrens 			ZFS_EXIT(zfsvfs);
1963789Sahrens 			return (err);
1964789Sahrens 		}
19651878Smaybee 		/*
19661878Smaybee 		 * XXX - Note, we are not providing any open
19671878Smaybee 		 * mode flags here (like FNDELAY), so we may
19681878Smaybee 		 * block if there are locks present... this
19691878Smaybee 		 * should be addressed in openat().
19701878Smaybee 		 */
19711878Smaybee 		do {
19721878Smaybee 			err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
1973*2113Sahrens 			/* NB: we already did dmu_tx_wait() if necessary */
19741878Smaybee 		} while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
19751878Smaybee 		if (err) {
19761878Smaybee 			ZFS_EXIT(zfsvfs);
19771878Smaybee 			return (err);
19781878Smaybee 		}
1979789Sahrens 	}
1980789Sahrens 
1981789Sahrens 	if (mask & (AT_ATIME|AT_MTIME))
1982789Sahrens 		need_policy = zfs_zaccess_v4_perm(zp, ACE_WRITE_ATTRIBUTES, cr);
1983789Sahrens 
1984789Sahrens 	if (mask & (AT_UID|AT_GID)) {
1985789Sahrens 		int	idmask = (mask & (AT_UID|AT_GID));
1986789Sahrens 		int	take_owner;
1987789Sahrens 		int	take_group;
1988789Sahrens 
1989789Sahrens 		/*
1990913Smarks 		 * NOTE: even if a new mode is being set,
1991913Smarks 		 * we may clear S_ISUID/S_ISGID bits.
1992913Smarks 		 */
1993913Smarks 
1994913Smarks 		if (!(mask & AT_MODE))
1995913Smarks 			vap->va_mode = pzp->zp_mode;
1996913Smarks 
1997913Smarks 		/*
1998789Sahrens 		 * Take ownership or chgrp to group we are a member of
1999789Sahrens 		 */
2000789Sahrens 
2001789Sahrens 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2002789Sahrens 		take_group = (mask & AT_GID) && groupmember(vap->va_gid, cr);
2003789Sahrens 
2004789Sahrens 		/*
2005789Sahrens 		 * If both AT_UID and AT_GID are set then take_owner and
2006789Sahrens 		 * take_group must both be set in order to allow taking
2007789Sahrens 		 * ownership.
2008789Sahrens 		 *
2009789Sahrens 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2010789Sahrens 		 *
2011789Sahrens 		 */
2012789Sahrens 
2013789Sahrens 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2014789Sahrens 		    ((idmask == AT_UID) && take_owner) ||
2015789Sahrens 		    ((idmask == AT_GID) && take_group)) {
2016789Sahrens 			if (zfs_zaccess_v4_perm(zp, ACE_WRITE_OWNER, cr) == 0) {
2017789Sahrens 				/*
2018789Sahrens 				 * Remove setuid/setgid for non-privileged users
2019789Sahrens 				 */
20201115Smarks 				secpolicy_setid_clear(vap, cr);
20211115Smarks 				trim_mask = TRUE;
20221115Smarks 				saved_mask = vap->va_mask;
2023789Sahrens 			} else {
2024789Sahrens 				need_policy =  TRUE;
2025789Sahrens 			}
2026789Sahrens 		} else {
2027789Sahrens 			need_policy =  TRUE;
2028789Sahrens 		}
2029789Sahrens 	}
2030789Sahrens 
2031789Sahrens 	if (mask & AT_MODE)
2032789Sahrens 		need_policy = TRUE;
2033789Sahrens 
2034789Sahrens 	if (need_policy) {
2035789Sahrens 		mutex_enter(&zp->z_lock);
2036789Sahrens 		oldva.va_mode = pzp->zp_mode;
2037789Sahrens 		oldva.va_uid = zp->z_phys->zp_uid;
2038789Sahrens 		oldva.va_gid = zp->z_phys->zp_gid;
2039789Sahrens 		mutex_exit(&zp->z_lock);
20401115Smarks 
20411115Smarks 		/*
20421115Smarks 		 * If trim_mask is set then take ownership
20431115Smarks 		 * has been granted.  In that case remove
20441115Smarks 		 * UID|GID from mask so that
20451115Smarks 		 * secpolicy_vnode_setattr() doesn't revoke it.
20461115Smarks 		 */
20471115Smarks 		if (trim_mask)
20481115Smarks 			vap->va_mask &= ~(AT_UID|AT_GID);
20491115Smarks 
2050789Sahrens 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2051789Sahrens 		    (int (*)(void *, int, cred_t *))zfs_zaccess_rwx, zp);
2052789Sahrens 		if (err) {
2053789Sahrens 			ZFS_EXIT(zfsvfs);
2054789Sahrens 			return (err);
2055789Sahrens 		}
20561115Smarks 
20571115Smarks 		if (trim_mask)
20581115Smarks 			vap->va_mask |= (saved_mask & (AT_UID|AT_GID));
2059789Sahrens 	}
2060789Sahrens 
2061789Sahrens 	/*
2062789Sahrens 	 * secpolicy_vnode_setattr, or take ownership may have
2063789Sahrens 	 * changed va_mask
2064789Sahrens 	 */
2065789Sahrens 	mask = vap->va_mask;
2066789Sahrens 
2067789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2068789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
2069789Sahrens 
2070789Sahrens 	if (mask & AT_MODE) {
20711576Smarks 		uint64_t pmode = pzp->zp_mode;
20721576Smarks 
20731576Smarks 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2074789Sahrens 
2075789Sahrens 		if (zp->z_phys->zp_acl.z_acl_extern_obj)
2076789Sahrens 			dmu_tx_hold_write(tx,
2077789Sahrens 			    pzp->zp_acl.z_acl_extern_obj, 0, SPA_MAXBLOCKSIZE);
2078789Sahrens 		else
2079789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2080789Sahrens 			    0, ZFS_ACL_SIZE(MAX_ACL_SIZE));
2081789Sahrens 	}
2082789Sahrens 
20831231Smarks 	if ((mask & (AT_UID | AT_GID)) && zp->z_phys->zp_xattr != 0) {
20841231Smarks 		err = zfs_zget(zp->z_zfsvfs, zp->z_phys->zp_xattr, &attrzp);
20851231Smarks 		if (err) {
20861231Smarks 			dmu_tx_abort(tx);
20871231Smarks 			ZFS_EXIT(zfsvfs);
20881231Smarks 			return (err);
20891231Smarks 		}
20901231Smarks 		dmu_tx_hold_bonus(tx, attrzp->z_id);
20911231Smarks 	}
20921231Smarks 
2093789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2094789Sahrens 	if (err) {
20951231Smarks 		if (attrzp)
20961231Smarks 			VN_RELE(ZTOV(attrzp));
2097789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2098*2113Sahrens 			dmu_tx_wait(tx);
2099*2113Sahrens 			dmu_tx_abort(tx);
2100789Sahrens 			goto top;
2101789Sahrens 		}
2102*2113Sahrens 		dmu_tx_abort(tx);
2103789Sahrens 		ZFS_EXIT(zfsvfs);
2104789Sahrens 		return (err);
2105789Sahrens 	}
2106789Sahrens 
2107789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
2108789Sahrens 
2109789Sahrens 	/*
2110789Sahrens 	 * Set each attribute requested.
2111789Sahrens 	 * We group settings according to the locks they need to acquire.
2112789Sahrens 	 *
2113789Sahrens 	 * Note: you cannot set ctime directly, although it will be
2114789Sahrens 	 * updated as a side-effect of calling this function.
2115789Sahrens 	 */
2116789Sahrens 
2117789Sahrens 	mutex_enter(&zp->z_lock);
2118789Sahrens 
2119789Sahrens 	if (mask & AT_MODE) {
2120789Sahrens 		err = zfs_acl_chmod_setattr(zp, new_mode, tx);
2121789Sahrens 		ASSERT3U(err, ==, 0);
2122789Sahrens 	}
2123789Sahrens 
21241231Smarks 	if (attrzp)
21251231Smarks 		mutex_enter(&attrzp->z_lock);
21261231Smarks 
21271231Smarks 	if (mask & AT_UID) {
2128789Sahrens 		zp->z_phys->zp_uid = (uint64_t)vap->va_uid;
21291231Smarks 		if (attrzp) {
21301231Smarks 			attrzp->z_phys->zp_uid = (uint64_t)vap->va_uid;
21311231Smarks 		}
21321231Smarks 	}
21331231Smarks 
21341231Smarks 	if (mask & AT_GID) {
2135789Sahrens 		zp->z_phys->zp_gid = (uint64_t)vap->va_gid;
21361231Smarks 		if (attrzp)
21371231Smarks 			attrzp->z_phys->zp_gid = (uint64_t)vap->va_gid;
21381231Smarks 	}
21391231Smarks 
21401231Smarks 	if (attrzp)
21411231Smarks 		mutex_exit(&attrzp->z_lock);
2142789Sahrens 
2143789Sahrens 	if (mask & AT_ATIME)
2144789Sahrens 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
2145789Sahrens 
2146789Sahrens 	if (mask & AT_MTIME)
2147789Sahrens 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
2148789Sahrens 
21491878Smaybee 	if (mask & AT_SIZE)
2150789Sahrens 		zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
21511878Smaybee 	else if (mask != 0)
2152789Sahrens 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
2153789Sahrens 
21541878Smaybee 	if (mask != 0)
21551878Smaybee 		seq = zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask);
2156789Sahrens 
2157789Sahrens 	mutex_exit(&zp->z_lock);
2158789Sahrens 
21591231Smarks 	if (attrzp)
21601231Smarks 		VN_RELE(ZTOV(attrzp));
21611231Smarks 
2162789Sahrens 	dmu_tx_commit(tx);
2163789Sahrens 
2164789Sahrens 	zil_commit(zilog, seq, 0);
2165789Sahrens 
2166789Sahrens 	ZFS_EXIT(zfsvfs);
2167789Sahrens 	return (err);
2168789Sahrens }
2169789Sahrens 
2170789Sahrens /*
2171789Sahrens  * Search back through the directory tree, using the ".." entries.
2172789Sahrens  * Lock each directory in the chain to prevent concurrent renames.
2173789Sahrens  * Fail any attempt to move a directory into one of its own descendants.
2174789Sahrens  * XXX - z_parent_lock can overlap with map or grow locks
2175789Sahrens  */
2176789Sahrens typedef struct zfs_zlock {
2177789Sahrens 	krwlock_t	*zl_rwlock;	/* lock we acquired */
2178789Sahrens 	znode_t		*zl_znode;	/* znode we held */
2179789Sahrens 	struct zfs_zlock *zl_next;	/* next in list */
2180789Sahrens } zfs_zlock_t;
2181789Sahrens 
2182789Sahrens static int
2183789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
2184789Sahrens {
2185789Sahrens 	zfs_zlock_t	*zl;
2186789Sahrens 	znode_t 	*zp = tdzp;
2187789Sahrens 	uint64_t	rootid = zp->z_zfsvfs->z_root;
2188789Sahrens 	uint64_t	*oidp = &zp->z_id;
2189789Sahrens 	krwlock_t	*rwlp = &szp->z_parent_lock;
2190789Sahrens 	krw_t		rw = RW_WRITER;
2191789Sahrens 
2192789Sahrens 	/*
2193789Sahrens 	 * First pass write-locks szp and compares to zp->z_id.
2194789Sahrens 	 * Later passes read-lock zp and compare to zp->z_parent.
2195789Sahrens 	 */
2196789Sahrens 	do {
2197789Sahrens 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
2198789Sahrens 		zl->zl_rwlock = rwlp;
2199789Sahrens 		zl->zl_znode = NULL;
2200789Sahrens 		zl->zl_next = *zlpp;
2201789Sahrens 		*zlpp = zl;
2202789Sahrens 
2203789Sahrens 		rw_enter(rwlp, rw);
2204789Sahrens 
2205789Sahrens 		if (*oidp == szp->z_id)		/* We're a descendant of szp */
2206789Sahrens 			return (EINVAL);
2207789Sahrens 
2208789Sahrens 		if (*oidp == rootid)		/* We've hit the top */
2209789Sahrens 			return (0);
2210789Sahrens 
2211789Sahrens 		if (rw == RW_READER) {		/* i.e. not the first pass */
2212789Sahrens 			int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
2213789Sahrens 			if (error)
2214789Sahrens 				return (error);
2215789Sahrens 			zl->zl_znode = zp;
2216789Sahrens 		}
2217789Sahrens 		oidp = &zp->z_phys->zp_parent;
2218789Sahrens 		rwlp = &zp->z_parent_lock;
2219789Sahrens 		rw = RW_READER;
2220789Sahrens 
2221789Sahrens 	} while (zp->z_id != sdzp->z_id);
2222789Sahrens 
2223789Sahrens 	return (0);
2224789Sahrens }
2225789Sahrens 
2226789Sahrens /*
2227789Sahrens  * Drop locks and release vnodes that were held by zfs_rename_lock().
2228789Sahrens  */
2229789Sahrens static void
2230789Sahrens zfs_rename_unlock(zfs_zlock_t **zlpp)
2231789Sahrens {
2232789Sahrens 	zfs_zlock_t *zl;
2233789Sahrens 
2234789Sahrens 	while ((zl = *zlpp) != NULL) {
2235789Sahrens 		if (zl->zl_znode != NULL)
2236789Sahrens 			VN_RELE(ZTOV(zl->zl_znode));
2237789Sahrens 		rw_exit(zl->zl_rwlock);
2238789Sahrens 		*zlpp = zl->zl_next;
2239789Sahrens 		kmem_free(zl, sizeof (*zl));
2240789Sahrens 	}
2241789Sahrens }
2242789Sahrens 
2243789Sahrens /*
2244789Sahrens  * Move an entry from the provided source directory to the target
2245789Sahrens  * directory.  Change the entry name as indicated.
2246789Sahrens  *
2247789Sahrens  *	IN:	sdvp	- Source directory containing the "old entry".
2248789Sahrens  *		snm	- Old entry name.
2249789Sahrens  *		tdvp	- Target directory to contain the "new entry".
2250789Sahrens  *		tnm	- New entry name.
2251789Sahrens  *		cr	- credentials of caller.
2252789Sahrens  *
2253789Sahrens  *	RETURN:	0 if success
2254789Sahrens  *		error code if failure
2255789Sahrens  *
2256789Sahrens  * Timestamps:
2257789Sahrens  *	sdvp,tdvp - ctime|mtime updated
2258789Sahrens  */
2259789Sahrens static int
2260789Sahrens zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr)
2261789Sahrens {
2262789Sahrens 	znode_t		*tdzp, *szp, *tzp;
2263789Sahrens 	znode_t		*sdzp = VTOZ(sdvp);
2264789Sahrens 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
2265789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2266789Sahrens 	uint64_t	seq = 0;
2267789Sahrens 	vnode_t		*realvp;
2268789Sahrens 	zfs_dirlock_t	*sdl, *tdl;
2269789Sahrens 	dmu_tx_t	*tx;
2270789Sahrens 	zfs_zlock_t	*zl;
2271789Sahrens 	int		cmp, serr, terr, error;
2272789Sahrens 
2273789Sahrens 	ZFS_ENTER(zfsvfs);
2274789Sahrens 
2275789Sahrens 	/*
2276789Sahrens 	 * Make sure we have the real vp for the target directory.
2277789Sahrens 	 */
2278789Sahrens 	if (VOP_REALVP(tdvp, &realvp) == 0)
2279789Sahrens 		tdvp = realvp;
2280789Sahrens 
2281789Sahrens 	if (tdvp->v_vfsp != sdvp->v_vfsp) {
2282789Sahrens 		ZFS_EXIT(zfsvfs);
2283789Sahrens 		return (EXDEV);
2284789Sahrens 	}
2285789Sahrens 
2286789Sahrens 	tdzp = VTOZ(tdvp);
2287789Sahrens top:
2288789Sahrens 	szp = NULL;
2289789Sahrens 	tzp = NULL;
2290789Sahrens 	zl = NULL;
2291789Sahrens 
2292789Sahrens 	/*
2293789Sahrens 	 * This is to prevent the creation of links into attribute space
2294789Sahrens 	 * by renaming a linked file into/outof an attribute directory.
2295789Sahrens 	 * See the comment in zfs_link() for why this is considered bad.
2296789Sahrens 	 */
2297789Sahrens 	if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
2298789Sahrens 	    (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
2299789Sahrens 		ZFS_EXIT(zfsvfs);
2300789Sahrens 		return (EINVAL);
2301789Sahrens 	}
2302789Sahrens 
2303789Sahrens 	/*
2304789Sahrens 	 * Lock source and target directory entries.  To prevent deadlock,
2305789Sahrens 	 * a lock ordering must be defined.  We lock the directory with
2306789Sahrens 	 * the smallest object id first, or if it's a tie, the one with
2307789Sahrens 	 * the lexically first name.
2308789Sahrens 	 */
2309789Sahrens 	if (sdzp->z_id < tdzp->z_id) {
2310789Sahrens 		cmp = -1;
2311789Sahrens 	} else if (sdzp->z_id > tdzp->z_id) {
2312789Sahrens 		cmp = 1;
2313789Sahrens 	} else {
2314789Sahrens 		cmp = strcmp(snm, tnm);
2315789Sahrens 		if (cmp == 0) {
2316789Sahrens 			/*
2317789Sahrens 			 * POSIX: "If the old argument and the new argument
2318789Sahrens 			 * both refer to links to the same existing file,
2319789Sahrens 			 * the rename() function shall return successfully
2320789Sahrens 			 * and perform no other action."
2321789Sahrens 			 */
2322789Sahrens 			ZFS_EXIT(zfsvfs);
2323789Sahrens 			return (0);
2324789Sahrens 		}
2325789Sahrens 	}
2326789Sahrens 	if (cmp < 0) {
2327789Sahrens 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
2328789Sahrens 		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
2329789Sahrens 	} else {
2330789Sahrens 		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
2331789Sahrens 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
2332789Sahrens 	}
2333789Sahrens 
2334789Sahrens 	if (serr) {
2335789Sahrens 		/*
2336789Sahrens 		 * Source entry invalid or not there.
2337789Sahrens 		 */
2338789Sahrens 		if (!terr) {
2339789Sahrens 			zfs_dirent_unlock(tdl);
2340789Sahrens 			if (tzp)
2341789Sahrens 				VN_RELE(ZTOV(tzp));
2342789Sahrens 		}
2343789Sahrens 		if (strcmp(snm, "..") == 0)
2344789Sahrens 			serr = EINVAL;
2345789Sahrens 		ZFS_EXIT(zfsvfs);
2346789Sahrens 		return (serr);
2347789Sahrens 	}
2348789Sahrens 	if (terr) {
2349789Sahrens 		zfs_dirent_unlock(sdl);
2350789Sahrens 		VN_RELE(ZTOV(szp));
2351789Sahrens 		if (strcmp(tnm, "..") == 0)
2352789Sahrens 			terr = EINVAL;
2353789Sahrens 		ZFS_EXIT(zfsvfs);
2354789Sahrens 		return (terr);
2355789Sahrens 	}
2356789Sahrens 
2357789Sahrens 	/*
2358789Sahrens 	 * Must have write access at the source to remove the old entry
2359789Sahrens 	 * and write access at the target to create the new entry.
2360789Sahrens 	 * Note that if target and source are the same, this can be
2361789Sahrens 	 * done in a single check.
2362789Sahrens 	 */
2363789Sahrens 
2364789Sahrens 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
2365789Sahrens 		goto out;
2366789Sahrens 
2367789Sahrens 	if (ZTOV(szp)->v_type == VDIR) {
2368789Sahrens 		/*
2369789Sahrens 		 * Check to make sure rename is valid.
2370789Sahrens 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
2371789Sahrens 		 */
2372789Sahrens 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
2373789Sahrens 			goto out;
2374789Sahrens 	}
2375789Sahrens 
2376789Sahrens 	/*
2377789Sahrens 	 * Does target exist?
2378789Sahrens 	 */
2379789Sahrens 	if (tzp) {
2380789Sahrens 		/*
2381789Sahrens 		 * Source and target must be the same type.
2382789Sahrens 		 */
2383789Sahrens 		if (ZTOV(szp)->v_type == VDIR) {
2384789Sahrens 			if (ZTOV(tzp)->v_type != VDIR) {
2385789Sahrens 				error = ENOTDIR;
2386789Sahrens 				goto out;
2387789Sahrens 			}
2388789Sahrens 		} else {
2389789Sahrens 			if (ZTOV(tzp)->v_type == VDIR) {
2390789Sahrens 				error = EISDIR;
2391789Sahrens 				goto out;
2392789Sahrens 			}
2393789Sahrens 		}
2394789Sahrens 		/*
2395789Sahrens 		 * POSIX dictates that when the source and target
2396789Sahrens 		 * entries refer to the same file object, rename
2397789Sahrens 		 * must do nothing and exit without error.
2398789Sahrens 		 */
2399789Sahrens 		if (szp->z_id == tzp->z_id) {
2400789Sahrens 			error = 0;
2401789Sahrens 			goto out;
2402789Sahrens 		}
2403789Sahrens 	}
2404789Sahrens 
2405789Sahrens 	vnevent_rename_src(ZTOV(szp));
2406789Sahrens 	if (tzp)
2407789Sahrens 		vnevent_rename_dest(ZTOV(tzp));
2408789Sahrens 
2409789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2410789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);	/* nlink changes */
2411789Sahrens 	dmu_tx_hold_bonus(tx, sdzp->z_id);	/* nlink changes */
24121544Seschrock 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
24131544Seschrock 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
24141544Seschrock 	if (sdzp != tdzp)
2415789Sahrens 		dmu_tx_hold_bonus(tx, tdzp->z_id);	/* nlink changes */
24161544Seschrock 	if (tzp)
24171544Seschrock 		dmu_tx_hold_bonus(tx, tzp->z_id);	/* parent changes */
24181544Seschrock 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
2419789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2420789Sahrens 	if (error) {
2421789Sahrens 		if (zl != NULL)
2422789Sahrens 			zfs_rename_unlock(&zl);
2423789Sahrens 		zfs_dirent_unlock(sdl);
2424789Sahrens 		zfs_dirent_unlock(tdl);
2425789Sahrens 		VN_RELE(ZTOV(szp));
2426789Sahrens 		if (tzp)
2427789Sahrens 			VN_RELE(ZTOV(tzp));
2428789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2429*2113Sahrens 			dmu_tx_wait(tx);
2430*2113Sahrens 			dmu_tx_abort(tx);
2431789Sahrens 			goto top;
2432789Sahrens 		}
2433*2113Sahrens 		dmu_tx_abort(tx);
2434789Sahrens 		ZFS_EXIT(zfsvfs);
2435789Sahrens 		return (error);
2436789Sahrens 	}
2437789Sahrens 
2438789Sahrens 	if (tzp)	/* Attempt to remove the existing target */
2439789Sahrens 		error = zfs_link_destroy(tdl, tzp, tx, 0, NULL);
2440789Sahrens 
2441789Sahrens 	if (error == 0) {
2442789Sahrens 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
2443789Sahrens 		if (error == 0) {
2444789Sahrens 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
2445789Sahrens 			ASSERT(error == 0);
2446789Sahrens 			seq = zfs_log_rename(zilog, tx, TX_RENAME,
2447789Sahrens 			    sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
2448789Sahrens 		}
2449789Sahrens 	}
2450789Sahrens 
2451789Sahrens 	dmu_tx_commit(tx);
2452789Sahrens out:
2453789Sahrens 	if (zl != NULL)
2454789Sahrens 		zfs_rename_unlock(&zl);
2455789Sahrens 
2456789Sahrens 	zfs_dirent_unlock(sdl);
2457789Sahrens 	zfs_dirent_unlock(tdl);
2458789Sahrens 
2459789Sahrens 	VN_RELE(ZTOV(szp));
2460789Sahrens 	if (tzp)
2461789Sahrens 		VN_RELE(ZTOV(tzp));
2462789Sahrens 
2463789Sahrens 	zil_commit(zilog, seq, 0);
2464789Sahrens 
2465789Sahrens 	ZFS_EXIT(zfsvfs);
2466789Sahrens 	return (error);
2467789Sahrens }
2468789Sahrens 
2469789Sahrens /*
2470789Sahrens  * Insert the indicated symbolic reference entry into the directory.
2471789Sahrens  *
2472789Sahrens  *	IN:	dvp	- Directory to contain new symbolic link.
2473789Sahrens  *		link	- Name for new symlink entry.
2474789Sahrens  *		vap	- Attributes of new entry.
2475789Sahrens  *		target	- Target path of new symlink.
2476789Sahrens  *		cr	- credentials of caller.
2477789Sahrens  *
2478789Sahrens  *	RETURN:	0 if success
2479789Sahrens  *		error code if failure
2480789Sahrens  *
2481789Sahrens  * Timestamps:
2482789Sahrens  *	dvp - ctime|mtime updated
2483789Sahrens  */
2484789Sahrens static int
2485789Sahrens zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr)
2486789Sahrens {
2487789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
2488789Sahrens 	zfs_dirlock_t	*dl;
2489789Sahrens 	dmu_tx_t	*tx;
2490789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2491789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2492789Sahrens 	uint64_t	seq = 0;
2493789Sahrens 	uint64_t	zoid;
2494789Sahrens 	int		len = strlen(link);
2495789Sahrens 	int		error;
2496789Sahrens 
2497789Sahrens 	ASSERT(vap->va_type == VLNK);
2498789Sahrens 
2499789Sahrens 	ZFS_ENTER(zfsvfs);
2500789Sahrens top:
2501789Sahrens 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
2502789Sahrens 		ZFS_EXIT(zfsvfs);
2503789Sahrens 		return (error);
2504789Sahrens 	}
2505789Sahrens 
2506789Sahrens 	if (len > MAXPATHLEN) {
2507789Sahrens 		ZFS_EXIT(zfsvfs);
2508789Sahrens 		return (ENAMETOOLONG);
2509789Sahrens 	}
2510789Sahrens 
2511789Sahrens 	/*
2512789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
2513789Sahrens 	 */
2514789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZNEW)) {
2515789Sahrens 		ZFS_EXIT(zfsvfs);
2516789Sahrens 		return (error);
2517789Sahrens 	}
2518789Sahrens 
2519789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2520789Sahrens 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
2521789Sahrens 	dmu_tx_hold_bonus(tx, dzp->z_id);
25221544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
2523789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
2524789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
2525789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2526789Sahrens 	if (error) {
2527789Sahrens 		zfs_dirent_unlock(dl);
2528789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2529*2113Sahrens 			dmu_tx_wait(tx);
2530*2113Sahrens 			dmu_tx_abort(tx);
2531789Sahrens 			goto top;
2532789Sahrens 		}
2533*2113Sahrens 		dmu_tx_abort(tx);
2534789Sahrens 		ZFS_EXIT(zfsvfs);
2535789Sahrens 		return (error);
2536789Sahrens 	}
2537789Sahrens 
2538789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
2539789Sahrens 
2540789Sahrens 	/*
2541789Sahrens 	 * Create a new object for the symlink.
2542789Sahrens 	 * Put the link content into bonus buffer if it will fit;
2543789Sahrens 	 * otherwise, store it just like any other file data.
2544789Sahrens 	 */
2545789Sahrens 	zoid = 0;
2546789Sahrens 	if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
2547789Sahrens 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, len);
2548789Sahrens 		if (len != 0)
2549789Sahrens 			bcopy(link, zp->z_phys + 1, len);
2550789Sahrens 	} else {
2551789Sahrens 		dmu_buf_t *dbp;
25521669Sperrin 
2553789Sahrens 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
2554789Sahrens 
25551669Sperrin 		/*
25561669Sperrin 		 * Nothing can access the znode yet so no locking needed
25571669Sperrin 		 * for growing the znode's blocksize.
25581669Sperrin 		 */
25591669Sperrin 		zfs_grow_blocksize(zp, len, tx);
2560789Sahrens 
25611544Seschrock 		VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, zoid, 0, FTAG, &dbp));
2562789Sahrens 		dmu_buf_will_dirty(dbp, tx);
2563789Sahrens 
2564789Sahrens 		ASSERT3U(len, <=, dbp->db_size);
2565789Sahrens 		bcopy(link, dbp->db_data, len);
25661544Seschrock 		dmu_buf_rele(dbp, FTAG);
2567789Sahrens 	}
2568789Sahrens 	zp->z_phys->zp_size = len;
2569789Sahrens 
2570789Sahrens 	/*
2571789Sahrens 	 * Insert the new object into the directory.
2572789Sahrens 	 */
2573789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
2574789Sahrens out:
2575789Sahrens 	if (error == 0)
2576789Sahrens 		seq = zfs_log_symlink(zilog, tx, TX_SYMLINK,
2577789Sahrens 		    dzp, zp, name, link);
2578789Sahrens 
2579789Sahrens 	dmu_tx_commit(tx);
2580789Sahrens 
2581789Sahrens 	zfs_dirent_unlock(dl);
2582789Sahrens 
2583789Sahrens 	VN_RELE(ZTOV(zp));
2584789Sahrens 
2585789Sahrens 	zil_commit(zilog, seq, 0);
2586789Sahrens 
2587789Sahrens 	ZFS_EXIT(zfsvfs);
2588789Sahrens 	return (error);
2589789Sahrens }
2590789Sahrens 
2591789Sahrens /*
2592789Sahrens  * Return, in the buffer contained in the provided uio structure,
2593789Sahrens  * the symbolic path referred to by vp.
2594789Sahrens  *
2595789Sahrens  *	IN:	vp	- vnode of symbolic link.
2596789Sahrens  *		uoip	- structure to contain the link path.
2597789Sahrens  *		cr	- credentials of caller.
2598789Sahrens  *
2599789Sahrens  *	OUT:	uio	- structure to contain the link path.
2600789Sahrens  *
2601789Sahrens  *	RETURN:	0 if success
2602789Sahrens  *		error code if failure
2603789Sahrens  *
2604789Sahrens  * Timestamps:
2605789Sahrens  *	vp - atime updated
2606789Sahrens  */
2607789Sahrens /* ARGSUSED */
2608789Sahrens static int
2609789Sahrens zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr)
2610789Sahrens {
2611789Sahrens 	znode_t		*zp = VTOZ(vp);
2612789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2613789Sahrens 	size_t		bufsz;
2614789Sahrens 	int		error;
2615789Sahrens 
2616789Sahrens 	ZFS_ENTER(zfsvfs);
2617789Sahrens 
2618789Sahrens 	bufsz = (size_t)zp->z_phys->zp_size;
2619789Sahrens 	if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
2620789Sahrens 		error = uiomove(zp->z_phys + 1,
2621789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
2622789Sahrens 	} else {
26231544Seschrock 		dmu_buf_t *dbp;
26241544Seschrock 		error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
26251544Seschrock 		if (error) {
2626789Sahrens 			ZFS_EXIT(zfsvfs);
2627789Sahrens 			return (error);
2628789Sahrens 		}
2629789Sahrens 		error = uiomove(dbp->db_data,
2630789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
26311544Seschrock 		dmu_buf_rele(dbp, FTAG);
2632789Sahrens 	}
2633789Sahrens 
2634789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2635789Sahrens 	ZFS_EXIT(zfsvfs);
2636789Sahrens 	return (error);
2637789Sahrens }
2638789Sahrens 
2639789Sahrens /*
2640789Sahrens  * Insert a new entry into directory tdvp referencing svp.
2641789Sahrens  *
2642789Sahrens  *	IN:	tdvp	- Directory to contain new entry.
2643789Sahrens  *		svp	- vnode of new entry.
2644789Sahrens  *		name	- name of new entry.
2645789Sahrens  *		cr	- credentials of caller.
2646789Sahrens  *
2647789Sahrens  *	RETURN:	0 if success
2648789Sahrens  *		error code if failure
2649789Sahrens  *
2650789Sahrens  * Timestamps:
2651789Sahrens  *	tdvp - ctime|mtime updated
2652789Sahrens  *	 svp - ctime updated
2653789Sahrens  */
2654789Sahrens /* ARGSUSED */
2655789Sahrens static int
2656789Sahrens zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr)
2657789Sahrens {
2658789Sahrens 	znode_t		*dzp = VTOZ(tdvp);
2659789Sahrens 	znode_t		*tzp, *szp;
2660789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2661789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2662789Sahrens 	uint64_t	seq = 0;
2663789Sahrens 	zfs_dirlock_t	*dl;
2664789Sahrens 	dmu_tx_t	*tx;
2665789Sahrens 	vnode_t		*realvp;
2666789Sahrens 	int		error;
2667789Sahrens 
2668789Sahrens 	ASSERT(tdvp->v_type == VDIR);
2669789Sahrens 
2670789Sahrens 	ZFS_ENTER(zfsvfs);
2671789Sahrens 
2672789Sahrens 	if (VOP_REALVP(svp, &realvp) == 0)
2673789Sahrens 		svp = realvp;
2674789Sahrens 
2675789Sahrens 	if (svp->v_vfsp != tdvp->v_vfsp) {
2676789Sahrens 		ZFS_EXIT(zfsvfs);
2677789Sahrens 		return (EXDEV);
2678789Sahrens 	}
2679789Sahrens 
2680789Sahrens 	szp = VTOZ(svp);
2681789Sahrens top:
2682789Sahrens 	/*
2683789Sahrens 	 * We do not support links between attributes and non-attributes
2684789Sahrens 	 * because of the potential security risk of creating links
2685789Sahrens 	 * into "normal" file space in order to circumvent restrictions
2686789Sahrens 	 * imposed in attribute space.
2687789Sahrens 	 */
2688789Sahrens 	if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
2689789Sahrens 	    (dzp->z_phys->zp_flags & ZFS_XATTR)) {
2690789Sahrens 		ZFS_EXIT(zfsvfs);
2691789Sahrens 		return (EINVAL);
2692789Sahrens 	}
2693789Sahrens 
2694789Sahrens 	/*
2695789Sahrens 	 * POSIX dictates that we return EPERM here.
2696789Sahrens 	 * Better choices include ENOTSUP or EISDIR.
2697789Sahrens 	 */
2698789Sahrens 	if (svp->v_type == VDIR) {
2699789Sahrens 		ZFS_EXIT(zfsvfs);
2700789Sahrens 		return (EPERM);
2701789Sahrens 	}
2702789Sahrens 
2703789Sahrens 	if ((uid_t)szp->z_phys->zp_uid != crgetuid(cr) &&
2704789Sahrens 	    secpolicy_basic_link(cr) != 0) {
2705789Sahrens 		ZFS_EXIT(zfsvfs);
2706789Sahrens 		return (EPERM);
2707789Sahrens 	}
2708789Sahrens 
2709789Sahrens 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
2710789Sahrens 		ZFS_EXIT(zfsvfs);
2711789Sahrens 		return (error);
2712789Sahrens 	}
2713789Sahrens 
2714789Sahrens 	/*
2715789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
2716789Sahrens 	 */
2717789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &tzp, ZNEW)) {
2718789Sahrens 		ZFS_EXIT(zfsvfs);
2719789Sahrens 		return (error);
2720789Sahrens 	}
2721789Sahrens 
2722789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2723789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);
27241544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
2725789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2726789Sahrens 	if (error) {
2727789Sahrens 		zfs_dirent_unlock(dl);
2728789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2729*2113Sahrens 			dmu_tx_wait(tx);
2730*2113Sahrens 			dmu_tx_abort(tx);
2731789Sahrens 			goto top;
2732789Sahrens 		}
2733*2113Sahrens 		dmu_tx_abort(tx);
2734789Sahrens 		ZFS_EXIT(zfsvfs);
2735789Sahrens 		return (error);
2736789Sahrens 	}
2737789Sahrens 
2738789Sahrens 	error = zfs_link_create(dl, szp, tx, 0);
2739789Sahrens 
2740789Sahrens 	if (error == 0)
2741789Sahrens 		seq = zfs_log_link(zilog, tx, TX_LINK, dzp, szp, name);
2742789Sahrens 
2743789Sahrens 	dmu_tx_commit(tx);
2744789Sahrens 
2745789Sahrens 	zfs_dirent_unlock(dl);
2746789Sahrens 
2747789Sahrens 	zil_commit(zilog, seq, 0);
2748789Sahrens 
2749789Sahrens 	ZFS_EXIT(zfsvfs);
2750789Sahrens 	return (error);
2751789Sahrens }
2752789Sahrens 
2753789Sahrens /*
2754789Sahrens  * zfs_null_putapage() is used when the file system has been force
2755789Sahrens  * unmounted. It just drops the pages.
2756789Sahrens  */
2757789Sahrens /* ARGSUSED */
2758789Sahrens static int
2759789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
2760789Sahrens 		size_t *lenp, int flags, cred_t *cr)
2761789Sahrens {
2762789Sahrens 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
2763789Sahrens 	return (0);
2764789Sahrens }
2765789Sahrens 
2766789Sahrens /* ARGSUSED */
2767789Sahrens static int
2768789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
2769789Sahrens 		size_t *lenp, int flags, cred_t *cr)
2770789Sahrens {
2771789Sahrens 	znode_t		*zp = VTOZ(vp);
2772789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2773789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2774789Sahrens 	dmu_tx_t	*tx;
27751669Sperrin 	rl_t		*rl;
2776789Sahrens 	u_offset_t	off;
2777789Sahrens 	ssize_t		len;
2778789Sahrens 	caddr_t		va;
2779789Sahrens 	int		err;
2780789Sahrens 
2781789Sahrens top:
2782789Sahrens 	off = pp->p_offset;
27831669Sperrin 	rl = zfs_range_lock(zp, off, PAGESIZE, RL_WRITER);
27841819Smaybee 	/*
27851819Smaybee 	 * Can't push pages past end-of-file.
27861819Smaybee 	 */
27871819Smaybee 	if (off >= zp->z_phys->zp_size) {
27881819Smaybee 		zfs_range_unlock(zp, rl);
27891819Smaybee 		return (EIO);
27901819Smaybee 	}
2791789Sahrens 	len = MIN(PAGESIZE, zp->z_phys->zp_size - off);
2792789Sahrens 
2793789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2794789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, off, len);
2795789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
2796789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2797789Sahrens 	if (err != 0) {
27981669Sperrin 		zfs_range_unlock(zp, rl);
2799789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2800*2113Sahrens 			dmu_tx_wait(tx);
2801*2113Sahrens 			dmu_tx_abort(tx);
2802789Sahrens 			goto top;
2803789Sahrens 		}
2804*2113Sahrens 		dmu_tx_abort(tx);
2805789Sahrens 		goto out;
2806789Sahrens 	}
2807789Sahrens 
2808789Sahrens 	va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1);
2809789Sahrens 
2810789Sahrens 	dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
2811789Sahrens 
2812789Sahrens 	ppmapout(va);
2813789Sahrens 
2814789Sahrens 	zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
28151472Sperrin 	(void) zfs_log_write(zilog, tx, TX_WRITE, zp, off, len, 0, NULL);
2816789Sahrens 	dmu_tx_commit(tx);
2817789Sahrens 
28181669Sperrin 	zfs_range_unlock(zp, rl);
2819789Sahrens 
2820789Sahrens 	pvn_write_done(pp, B_WRITE | flags);
2821789Sahrens 	if (offp)
2822789Sahrens 		*offp = off;
2823789Sahrens 	if (lenp)
2824789Sahrens 		*lenp = len;
2825789Sahrens 
2826789Sahrens out:
2827789Sahrens 	return (err);
2828789Sahrens }
2829789Sahrens 
2830789Sahrens /*
2831789Sahrens  * Copy the portion of the file indicated from pages into the file.
2832789Sahrens  * The pages are stored in a page list attached to the files vnode.
2833789Sahrens  *
2834789Sahrens  *	IN:	vp	- vnode of file to push page data to.
2835789Sahrens  *		off	- position in file to put data.
2836789Sahrens  *		len	- amount of data to write.
2837789Sahrens  *		flags	- flags to control the operation.
2838789Sahrens  *		cr	- credentials of caller.
2839789Sahrens  *
2840789Sahrens  *	RETURN:	0 if success
2841789Sahrens  *		error code if failure
2842789Sahrens  *
2843789Sahrens  * Timestamps:
2844789Sahrens  *	vp - ctime|mtime updated
2845789Sahrens  */
2846789Sahrens static int
2847789Sahrens zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr)
2848789Sahrens {
2849789Sahrens 	znode_t		*zp = VTOZ(vp);
2850789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2851789Sahrens 	page_t		*pp;
2852789Sahrens 	size_t		io_len;
2853789Sahrens 	u_offset_t	io_off;
28541669Sperrin 	uint64_t	filesz;
2855789Sahrens 	int		error = 0;
2856789Sahrens 
2857789Sahrens 	ZFS_ENTER(zfsvfs);
2858789Sahrens 
2859789Sahrens 	ASSERT(zp->z_dbuf_held && zp->z_phys);
2860789Sahrens 
2861789Sahrens 	if (len == 0) {
2862789Sahrens 		/*
2863789Sahrens 		 * Search the entire vp list for pages >= off.
2864789Sahrens 		 */
2865789Sahrens 		error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage,
2866789Sahrens 		    flags, cr);
28671472Sperrin 		goto out;
2868789Sahrens 	}
2869789Sahrens 
28701669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
28711669Sperrin 	if (off > filesz) {
2872789Sahrens 		/* past end of file */
2873789Sahrens 		ZFS_EXIT(zfsvfs);
2874789Sahrens 		return (0);
2875789Sahrens 	}
2876789Sahrens 
28771669Sperrin 	len = MIN(len, filesz - off);
2878789Sahrens 
28791472Sperrin 	for (io_off = off; io_off < off + len; io_off += io_len) {
2880789Sahrens 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
28811669Sperrin 			pp = page_lookup(vp, io_off,
2882789Sahrens 				(flags & (B_INVAL | B_FREE)) ?
2883789Sahrens 					SE_EXCL : SE_SHARED);
2884789Sahrens 		} else {
2885789Sahrens 			pp = page_lookup_nowait(vp, io_off,
2886789Sahrens 				(flags & B_FREE) ? SE_EXCL : SE_SHARED);
2887789Sahrens 		}
2888789Sahrens 
2889789Sahrens 		if (pp != NULL && pvn_getdirty(pp, flags)) {
2890789Sahrens 			int err;
2891789Sahrens 
2892789Sahrens 			/*
2893789Sahrens 			 * Found a dirty page to push
2894789Sahrens 			 */
28951669Sperrin 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
28961669Sperrin 			if (err)
2897789Sahrens 				error = err;
2898789Sahrens 		} else {
2899789Sahrens 			io_len = PAGESIZE;
2900789Sahrens 		}
2901789Sahrens 	}
29021472Sperrin out:
29031472Sperrin 	zil_commit(zfsvfs->z_log, UINT64_MAX, (flags & B_ASYNC) ? 0 : FDSYNC);
2904789Sahrens 	ZFS_EXIT(zfsvfs);
2905789Sahrens 	return (error);
2906789Sahrens }
2907789Sahrens 
2908789Sahrens void
2909789Sahrens zfs_inactive(vnode_t *vp, cred_t *cr)
2910789Sahrens {
2911789Sahrens 	znode_t	*zp = VTOZ(vp);
2912789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2913789Sahrens 	int error;
2914789Sahrens 
2915789Sahrens 	rw_enter(&zfsvfs->z_um_lock, RW_READER);
2916789Sahrens 	if (zfsvfs->z_unmounted2) {
2917789Sahrens 		ASSERT(zp->z_dbuf_held == 0);
2918789Sahrens 
2919789Sahrens 		if (vn_has_cached_data(vp)) {
2920789Sahrens 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
2921789Sahrens 			    B_INVAL, cr);
2922789Sahrens 		}
2923789Sahrens 
29241544Seschrock 		mutex_enter(&zp->z_lock);
2925789Sahrens 		vp->v_count = 0; /* count arrives as 1 */
29261544Seschrock 		if (zp->z_dbuf == NULL) {
29271544Seschrock 			mutex_exit(&zp->z_lock);
29281544Seschrock 			zfs_znode_free(zp);
29291544Seschrock 		} else {
29301544Seschrock 			mutex_exit(&zp->z_lock);
29311544Seschrock 		}
2932789Sahrens 		rw_exit(&zfsvfs->z_um_lock);
2933789Sahrens 		VFS_RELE(zfsvfs->z_vfs);
2934789Sahrens 		return;
2935789Sahrens 	}
2936789Sahrens 
2937789Sahrens 	/*
2938789Sahrens 	 * Attempt to push any data in the page cache.  If this fails
2939789Sahrens 	 * we will get kicked out later in zfs_zinactive().
2940789Sahrens 	 */
29411298Sperrin 	if (vn_has_cached_data(vp)) {
29421298Sperrin 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
29431298Sperrin 		    cr);
29441298Sperrin 	}
2945789Sahrens 
2946789Sahrens 	if (zp->z_atime_dirty && zp->z_reap == 0) {
2947789Sahrens 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
2948789Sahrens 
2949789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
2950789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
2951789Sahrens 		if (error) {
2952789Sahrens 			dmu_tx_abort(tx);
2953789Sahrens 		} else {
2954789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
2955789Sahrens 			mutex_enter(&zp->z_lock);
2956789Sahrens 			zp->z_atime_dirty = 0;
2957789Sahrens 			mutex_exit(&zp->z_lock);
2958789Sahrens 			dmu_tx_commit(tx);
2959789Sahrens 		}
2960789Sahrens 	}
2961789Sahrens 
2962789Sahrens 	zfs_zinactive(zp);
2963789Sahrens 	rw_exit(&zfsvfs->z_um_lock);
2964789Sahrens }
2965789Sahrens 
2966789Sahrens /*
2967789Sahrens  * Bounds-check the seek operation.
2968789Sahrens  *
2969789Sahrens  *	IN:	vp	- vnode seeking within
2970789Sahrens  *		ooff	- old file offset
2971789Sahrens  *		noffp	- pointer to new file offset
2972789Sahrens  *
2973789Sahrens  *	RETURN:	0 if success
2974789Sahrens  *		EINVAL if new offset invalid
2975789Sahrens  */
2976789Sahrens /* ARGSUSED */
2977789Sahrens static int
2978789Sahrens zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp)
2979789Sahrens {
2980789Sahrens 	if (vp->v_type == VDIR)
2981789Sahrens 		return (0);
2982789Sahrens 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
2983789Sahrens }
2984789Sahrens 
2985789Sahrens /*
2986789Sahrens  * Pre-filter the generic locking function to trap attempts to place
2987789Sahrens  * a mandatory lock on a memory mapped file.
2988789Sahrens  */
2989789Sahrens static int
2990789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
2991789Sahrens     flk_callback_t *flk_cbp, cred_t *cr)
2992789Sahrens {
2993789Sahrens 	znode_t *zp = VTOZ(vp);
2994789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2995789Sahrens 	int error;
2996789Sahrens 
2997789Sahrens 	ZFS_ENTER(zfsvfs);
2998789Sahrens 
2999789Sahrens 	/*
30001544Seschrock 	 * We are following the UFS semantics with respect to mapcnt
30011544Seschrock 	 * here: If we see that the file is mapped already, then we will
30021544Seschrock 	 * return an error, but we don't worry about races between this
30031544Seschrock 	 * function and zfs_map().
3004789Sahrens 	 */
30051544Seschrock 	if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) {
3006789Sahrens 		ZFS_EXIT(zfsvfs);
3007789Sahrens 		return (EAGAIN);
3008789Sahrens 	}
3009789Sahrens 	error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr);
3010789Sahrens 	ZFS_EXIT(zfsvfs);
3011789Sahrens 	return (error);
3012789Sahrens }
3013789Sahrens 
3014789Sahrens /*
3015789Sahrens  * If we can't find a page in the cache, we will create a new page
3016789Sahrens  * and fill it with file data.  For efficiency, we may try to fill
30171669Sperrin  * multiple pages at once (klustering).
3018789Sahrens  */
3019789Sahrens static int
3020789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
3021789Sahrens     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
3022789Sahrens {
3023789Sahrens 	znode_t *zp = VTOZ(vp);
3024789Sahrens 	page_t *pp, *cur_pp;
3025789Sahrens 	objset_t *os = zp->z_zfsvfs->z_os;
3026789Sahrens 	caddr_t va;
3027789Sahrens 	u_offset_t io_off, total;
3028789Sahrens 	uint64_t oid = zp->z_id;
3029789Sahrens 	size_t io_len;
30301669Sperrin 	uint64_t filesz;
3031789Sahrens 	int err;
3032789Sahrens 
3033789Sahrens 	/*
3034789Sahrens 	 * If we are only asking for a single page don't bother klustering.
3035789Sahrens 	 */
30361669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
30371669Sperrin 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE || off > filesz) {
3038789Sahrens 		io_off = off;
3039789Sahrens 		io_len = PAGESIZE;
3040789Sahrens 		pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr);
3041789Sahrens 	} else {
3042789Sahrens 		/*
3043789Sahrens 		 * Try to fill a kluster of pages (a blocks worth).
3044789Sahrens 		 */
3045789Sahrens 		size_t klen;
3046789Sahrens 		u_offset_t koff;
3047789Sahrens 
3048789Sahrens 		if (!ISP2(zp->z_blksz)) {
3049789Sahrens 			/* Only one block in the file. */
3050789Sahrens 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
3051789Sahrens 			koff = 0;
3052789Sahrens 		} else {
3053789Sahrens 			klen = plsz;
3054789Sahrens 			koff = P2ALIGN(off, (u_offset_t)klen);
3055789Sahrens 		}
30561819Smaybee 		ASSERT(koff <= filesz);
30571819Smaybee 		if (koff + klen > filesz)
30581819Smaybee 			klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff;
3059789Sahrens 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
3060789Sahrens 			    &io_len, koff, klen, 0);
3061789Sahrens 	}
3062789Sahrens 	if (pp == NULL) {
3063789Sahrens 		/*
3064789Sahrens 		 * Some other thread entered the page before us.
3065789Sahrens 		 * Return to zfs_getpage to retry the lookup.
3066789Sahrens 		 */
3067789Sahrens 		*pl = NULL;
3068789Sahrens 		return (0);
3069789Sahrens 	}
3070789Sahrens 
3071789Sahrens 	/*
3072789Sahrens 	 * Fill the pages in the kluster.
3073789Sahrens 	 */
3074789Sahrens 	cur_pp = pp;
3075789Sahrens 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
3076789Sahrens 		ASSERT(io_off == cur_pp->p_offset);
3077789Sahrens 		va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1);
30781544Seschrock 		err = dmu_read(os, oid, io_off, PAGESIZE, va);
3079789Sahrens 		ppmapout(va);
3080789Sahrens 		if (err) {
3081789Sahrens 			/* On error, toss the entire kluster */
3082789Sahrens 			pvn_read_done(pp, B_ERROR);
3083789Sahrens 			return (err);
3084789Sahrens 		}
3085789Sahrens 		cur_pp = cur_pp->p_next;
3086789Sahrens 	}
3087789Sahrens out:
3088789Sahrens 	/*
3089789Sahrens 	 * Fill in the page list array from the kluster.  If
3090789Sahrens 	 * there are too many pages in the kluster, return
3091789Sahrens 	 * as many pages as possible starting from the desired
3092789Sahrens 	 * offset `off'.
3093789Sahrens 	 * NOTE: the page list will always be null terminated.
3094789Sahrens 	 */
3095789Sahrens 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
3096789Sahrens 
3097789Sahrens 	return (0);
3098789Sahrens }
3099789Sahrens 
3100789Sahrens /*
3101789Sahrens  * Return pointers to the pages for the file region [off, off + len]
3102789Sahrens  * in the pl array.  If plsz is greater than len, this function may
3103789Sahrens  * also return page pointers from before or after the specified
3104789Sahrens  * region (i.e. some region [off', off' + plsz]).  These additional
3105789Sahrens  * pages are only returned if they are already in the cache, or were
3106789Sahrens  * created as part of a klustered read.
3107789Sahrens  *
3108789Sahrens  *	IN:	vp	- vnode of file to get data from.
3109789Sahrens  *		off	- position in file to get data from.
3110789Sahrens  *		len	- amount of data to retrieve.
3111789Sahrens  *		plsz	- length of provided page list.
3112789Sahrens  *		seg	- segment to obtain pages for.
3113789Sahrens  *		addr	- virtual address of fault.
3114789Sahrens  *		rw	- mode of created pages.
3115789Sahrens  *		cr	- credentials of caller.
3116789Sahrens  *
3117789Sahrens  *	OUT:	protp	- protection mode of created pages.
3118789Sahrens  *		pl	- list of pages created.
3119789Sahrens  *
3120789Sahrens  *	RETURN:	0 if success
3121789Sahrens  *		error code if failure
3122789Sahrens  *
3123789Sahrens  * Timestamps:
3124789Sahrens  *	vp - atime updated
3125789Sahrens  */
3126789Sahrens /* ARGSUSED */
3127789Sahrens static int
3128789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
3129789Sahrens 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
3130789Sahrens 	enum seg_rw rw, cred_t *cr)
3131789Sahrens {
3132789Sahrens 	znode_t		*zp = VTOZ(vp);
3133789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3134789Sahrens 	page_t		*pp, **pl0 = pl;
31351669Sperrin 	rl_t		*rl;
3136789Sahrens 	int		cnt = 0, need_unlock = 0, err = 0;
3137789Sahrens 
3138789Sahrens 	ZFS_ENTER(zfsvfs);
3139789Sahrens 
3140789Sahrens 	if (protp)
3141789Sahrens 		*protp = PROT_ALL;
3142789Sahrens 
3143789Sahrens 	ASSERT(zp->z_dbuf_held && zp->z_phys);
3144789Sahrens 
3145789Sahrens 	/* no faultahead (for now) */
3146789Sahrens 	if (pl == NULL) {
3147789Sahrens 		ZFS_EXIT(zfsvfs);
3148789Sahrens 		return (0);
3149789Sahrens 	}
3150789Sahrens 
31511669Sperrin 	/*
31521669Sperrin 	 * Make sure nobody restructures the file in the middle of the getpage.
31531669Sperrin 	 */
31541669Sperrin 	rl = zfs_range_lock(zp, off, len, RL_READER);
31551669Sperrin 
3156789Sahrens 	/* can't fault past EOF */
3157789Sahrens 	if (off >= zp->z_phys->zp_size) {
31581669Sperrin 		zfs_range_unlock(zp, rl);
3159789Sahrens 		ZFS_EXIT(zfsvfs);
3160789Sahrens 		return (EFAULT);
3161789Sahrens 	}
3162789Sahrens 
3163789Sahrens 	/*
3164789Sahrens 	 * If we already own the lock, then we must be page faulting
3165789Sahrens 	 * in the middle of a write to this file (i.e., we are writing
3166789Sahrens 	 * to this file using data from a mapped region of the file).
3167789Sahrens 	 */
3168789Sahrens 	if (!rw_owner(&zp->z_map_lock)) {
3169789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
3170789Sahrens 		need_unlock = TRUE;
3171789Sahrens 	}
3172789Sahrens 
3173789Sahrens 	/*
3174789Sahrens 	 * Loop through the requested range [off, off + len] looking
3175789Sahrens 	 * for pages.  If we don't find a page, we will need to create
3176789Sahrens 	 * a new page and fill it with data from the file.
3177789Sahrens 	 */
3178789Sahrens 	while (len > 0) {
3179789Sahrens 		if (plsz < PAGESIZE)
3180789Sahrens 			break;
3181789Sahrens 		if (pp = page_lookup(vp, off, SE_SHARED)) {
3182789Sahrens 			*pl++ = pp;
3183789Sahrens 			off += PAGESIZE;
3184789Sahrens 			addr += PAGESIZE;
3185789Sahrens 			len -= PAGESIZE;
3186789Sahrens 			plsz -= PAGESIZE;
3187789Sahrens 		} else {
3188789Sahrens 			err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw);
3189789Sahrens 			/*
3190789Sahrens 			 * klustering may have changed our region
3191789Sahrens 			 * to be block aligned.
3192789Sahrens 			 */
3193789Sahrens 			if (((pp = *pl) != 0) && (off != pp->p_offset)) {
3194789Sahrens 				int delta = off - pp->p_offset;
3195789Sahrens 				len += delta;
3196789Sahrens 				off -= delta;
3197789Sahrens 				addr -= delta;
3198789Sahrens 			}
3199789Sahrens 			while (*pl) {
3200789Sahrens 				pl++;
3201789Sahrens 				cnt++;
3202789Sahrens 				off += PAGESIZE;
3203789Sahrens 				addr += PAGESIZE;
3204789Sahrens 				plsz -= PAGESIZE;
3205789Sahrens 				if (len > PAGESIZE)
3206789Sahrens 					len -= PAGESIZE;
3207789Sahrens 				else
3208789Sahrens 					len = 0;
3209789Sahrens 			}
32101669Sperrin 			if (err) {
32111669Sperrin 				/*
32121669Sperrin 				 * Release any pages we have locked.
32131669Sperrin 				 */
32141669Sperrin 				while (pl > pl0)
32151669Sperrin 					page_unlock(*--pl);
32161669Sperrin 				goto out;
32171669Sperrin 			}
3218789Sahrens 		}
3219789Sahrens 	}
3220789Sahrens 
3221789Sahrens 	/*
3222789Sahrens 	 * Fill out the page array with any pages already in the cache.
3223789Sahrens 	 */
3224789Sahrens 	while (plsz > 0) {
3225789Sahrens 		pp = page_lookup_nowait(vp, off, SE_SHARED);
3226789Sahrens 		if (pp == NULL)
3227789Sahrens 			break;
3228789Sahrens 		*pl++ = pp;
3229789Sahrens 		off += PAGESIZE;
3230789Sahrens 		plsz -= PAGESIZE;
3231789Sahrens 	}
3232789Sahrens 
3233789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3234789Sahrens out:
3235789Sahrens 	*pl = NULL;
3236789Sahrens 
3237789Sahrens 	if (need_unlock)
3238789Sahrens 		rw_exit(&zp->z_map_lock);
32391669Sperrin 	zfs_range_unlock(zp, rl);
3240789Sahrens 
3241789Sahrens 	ZFS_EXIT(zfsvfs);
3242789Sahrens 	return (err);
3243789Sahrens }
3244789Sahrens 
32451544Seschrock /*
32461544Seschrock  * Request a memory map for a section of a file.  This code interacts
32471544Seschrock  * with common code and the VM system as follows:
32481544Seschrock  *
32491544Seschrock  *	common code calls mmap(), which ends up in smmap_common()
32501544Seschrock  *
32511544Seschrock  *	this calls VOP_MAP(), which takes you into (say) zfs
32521544Seschrock  *
32531544Seschrock  *	zfs_map() calls as_map(), passing segvn_create() as the callback
32541544Seschrock  *
32551544Seschrock  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
32561544Seschrock  *
32571544Seschrock  *	zfs_addmap() updates z_mapcnt
32581544Seschrock  */
3259789Sahrens static int
3260789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
3261789Sahrens     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
3262789Sahrens {
3263789Sahrens 	znode_t *zp = VTOZ(vp);
3264789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3265789Sahrens 	segvn_crargs_t	vn_a;
3266789Sahrens 	int		error;
3267789Sahrens 
3268789Sahrens 	ZFS_ENTER(zfsvfs);
3269789Sahrens 
3270789Sahrens 	if (vp->v_flag & VNOMAP) {
3271789Sahrens 		ZFS_EXIT(zfsvfs);
3272789Sahrens 		return (ENOSYS);
3273789Sahrens 	}
3274789Sahrens 
3275789Sahrens 	if (off < 0 || len > MAXOFFSET_T - off) {
3276789Sahrens 		ZFS_EXIT(zfsvfs);
3277789Sahrens 		return (ENXIO);
3278789Sahrens 	}
3279789Sahrens 
3280789Sahrens 	if (vp->v_type != VREG) {
3281789Sahrens 		ZFS_EXIT(zfsvfs);
3282789Sahrens 		return (ENODEV);
3283789Sahrens 	}
3284789Sahrens 
3285789Sahrens 	/*
3286789Sahrens 	 * If file is locked, disallow mapping.
3287789Sahrens 	 */
32881544Seschrock 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) {
32891544Seschrock 		ZFS_EXIT(zfsvfs);
32901544Seschrock 		return (EAGAIN);
3291789Sahrens 	}
3292789Sahrens 
3293789Sahrens 	as_rangelock(as);
3294789Sahrens 	if ((flags & MAP_FIXED) == 0) {
3295789Sahrens 		map_addr(addrp, len, off, 1, flags);
3296789Sahrens 		if (*addrp == NULL) {
3297789Sahrens 			as_rangeunlock(as);
3298789Sahrens 			ZFS_EXIT(zfsvfs);
3299789Sahrens 			return (ENOMEM);
3300789Sahrens 		}
3301789Sahrens 	} else {
3302789Sahrens 		/*
3303789Sahrens 		 * User specified address - blow away any previous mappings
3304789Sahrens 		 */
3305789Sahrens 		(void) as_unmap(as, *addrp, len);
3306789Sahrens 	}
3307789Sahrens 
3308789Sahrens 	vn_a.vp = vp;
3309789Sahrens 	vn_a.offset = (u_offset_t)off;
3310789Sahrens 	vn_a.type = flags & MAP_TYPE;
3311789Sahrens 	vn_a.prot = prot;
3312789Sahrens 	vn_a.maxprot = maxprot;
3313789Sahrens 	vn_a.cred = cr;
3314789Sahrens 	vn_a.amp = NULL;
3315789Sahrens 	vn_a.flags = flags & ~MAP_TYPE;
33161417Skchow 	vn_a.szc = 0;
33171417Skchow 	vn_a.lgrp_mem_policy_flags = 0;
3318789Sahrens 
3319789Sahrens 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
3320789Sahrens 
3321789Sahrens 	as_rangeunlock(as);
3322789Sahrens 	ZFS_EXIT(zfsvfs);
3323789Sahrens 	return (error);
3324789Sahrens }
3325789Sahrens 
3326789Sahrens /* ARGSUSED */
3327789Sahrens static int
3328789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
3329789Sahrens     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
3330789Sahrens {
33311544Seschrock 	uint64_t pages = btopr(len);
33321544Seschrock 
33331544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
3334789Sahrens 	return (0);
3335789Sahrens }
3336789Sahrens 
33371773Seschrock /*
33381773Seschrock  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
33391773Seschrock  * more accurate mtime for the associated file.  Since we don't have a way of
33401773Seschrock  * detecting when the data was actually modified, we have to resort to
33411773Seschrock  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
33421773Seschrock  * last page is pushed.  The problem occurs when the msync() call is omitted,
33431773Seschrock  * which by far the most common case:
33441773Seschrock  *
33451773Seschrock  * 	open()
33461773Seschrock  * 	mmap()
33471773Seschrock  * 	<modify memory>
33481773Seschrock  * 	munmap()
33491773Seschrock  * 	close()
33501773Seschrock  * 	<time lapse>
33511773Seschrock  * 	putpage() via fsflush
33521773Seschrock  *
33531773Seschrock  * If we wait until fsflush to come along, we can have a modification time that
33541773Seschrock  * is some arbitrary point in the future.  In order to prevent this in the
33551773Seschrock  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
33561773Seschrock  * torn down.
33571773Seschrock  */
3358789Sahrens /* ARGSUSED */
3359789Sahrens static int
3360789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
3361789Sahrens     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr)
3362789Sahrens {
33631544Seschrock 	uint64_t pages = btopr(len);
33641544Seschrock 
33651544Seschrock 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
33661544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
33671773Seschrock 
33681773Seschrock 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
33691773Seschrock 	    vn_has_cached_data(vp))
33701773Seschrock 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr);
33711773Seschrock 
3372789Sahrens 	return (0);
3373789Sahrens }
3374789Sahrens 
3375789Sahrens /*
3376789Sahrens  * Free or allocate space in a file.  Currently, this function only
3377789Sahrens  * supports the `F_FREESP' command.  However, this command is somewhat
3378789Sahrens  * misnamed, as its functionality includes the ability to allocate as
3379789Sahrens  * well as free space.
3380789Sahrens  *
3381789Sahrens  *	IN:	vp	- vnode of file to free data in.
3382789Sahrens  *		cmd	- action to take (only F_FREESP supported).
3383789Sahrens  *		bfp	- section of file to free/alloc.
3384789Sahrens  *		flag	- current file open mode flags.
3385789Sahrens  *		offset	- current file offset.
3386789Sahrens  *		cr	- credentials of caller [UNUSED].
3387789Sahrens  *
3388789Sahrens  *	RETURN:	0 if success
3389789Sahrens  *		error code if failure
3390789Sahrens  *
3391789Sahrens  * Timestamps:
3392789Sahrens  *	vp - ctime|mtime updated
3393789Sahrens  */
3394789Sahrens /* ARGSUSED */
3395789Sahrens static int
3396789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
3397789Sahrens     offset_t offset, cred_t *cr, caller_context_t *ct)
3398789Sahrens {
3399789Sahrens 	znode_t		*zp = VTOZ(vp);
3400789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3401789Sahrens 	uint64_t	off, len;
3402789Sahrens 	int		error;
3403789Sahrens 
3404789Sahrens 	ZFS_ENTER(zfsvfs);
3405789Sahrens 
3406789Sahrens top:
3407789Sahrens 	if (cmd != F_FREESP) {
3408789Sahrens 		ZFS_EXIT(zfsvfs);
3409789Sahrens 		return (EINVAL);
3410789Sahrens 	}
3411789Sahrens 
3412789Sahrens 	if (error = convoff(vp, bfp, 0, offset)) {
3413789Sahrens 		ZFS_EXIT(zfsvfs);
3414789Sahrens 		return (error);
3415789Sahrens 	}
3416789Sahrens 
3417789Sahrens 	if (bfp->l_len < 0) {
3418789Sahrens 		ZFS_EXIT(zfsvfs);
3419789Sahrens 		return (EINVAL);
3420789Sahrens 	}
3421789Sahrens 
3422789Sahrens 	off = bfp->l_start;
34231669Sperrin 	len = bfp->l_len; /* 0 means from off to end of file */
34241878Smaybee 
34251878Smaybee 	do {
34261878Smaybee 		error = zfs_freesp(zp, off, len, flag, TRUE);
3427*2113Sahrens 		/* NB: we already did dmu_tx_wait() if necessary */
34281878Smaybee 	} while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
3429789Sahrens 
3430789Sahrens 	ZFS_EXIT(zfsvfs);
3431789Sahrens 	return (error);
3432789Sahrens }
3433789Sahrens 
3434789Sahrens static int
3435789Sahrens zfs_fid(vnode_t *vp, fid_t *fidp)
3436789Sahrens {
3437789Sahrens 	znode_t		*zp = VTOZ(vp);
3438789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3439789Sahrens 	uint32_t	gen = (uint32_t)zp->z_phys->zp_gen;
3440789Sahrens 	uint64_t	object = zp->z_id;
3441789Sahrens 	zfid_short_t	*zfid;
3442789Sahrens 	int		size, i;
3443789Sahrens 
3444789Sahrens 	ZFS_ENTER(zfsvfs);
3445789Sahrens 
3446789Sahrens 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
3447789Sahrens 	if (fidp->fid_len < size) {
3448789Sahrens 		fidp->fid_len = size;
34491512Sek110237 		ZFS_EXIT(zfsvfs);
3450789Sahrens 		return (ENOSPC);
3451789Sahrens 	}
3452789Sahrens 
3453789Sahrens 	zfid = (zfid_short_t *)fidp;
3454789Sahrens 
3455789Sahrens 	zfid->zf_len = size;
3456789Sahrens 
3457789Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
3458789Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
3459789Sahrens 
3460789Sahrens 	/* Must have a non-zero generation number to distinguish from .zfs */
3461789Sahrens 	if (gen == 0)
3462789Sahrens 		gen = 1;
3463789Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
3464789Sahrens 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
3465789Sahrens 
3466789Sahrens 	if (size == LONG_FID_LEN) {
3467789Sahrens 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
3468789Sahrens 		zfid_long_t	*zlfid;
3469789Sahrens 
3470789Sahrens 		zlfid = (zfid_long_t *)fidp;
3471789Sahrens 
3472789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
3473789Sahrens 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
3474789Sahrens 
3475789Sahrens 		/* XXX - this should be the generation number for the objset */
3476789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
3477789Sahrens 			zlfid->zf_setgen[i] = 0;
3478789Sahrens 	}
3479789Sahrens 
3480789Sahrens 	ZFS_EXIT(zfsvfs);
3481789Sahrens 	return (0);
3482789Sahrens }
3483789Sahrens 
3484789Sahrens static int
3485789Sahrens zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr)
3486789Sahrens {
3487789Sahrens 	znode_t		*zp, *xzp;
3488789Sahrens 	zfsvfs_t	*zfsvfs;
3489789Sahrens 	zfs_dirlock_t	*dl;
3490789Sahrens 	int		error;
3491789Sahrens 
3492789Sahrens 	switch (cmd) {
3493789Sahrens 	case _PC_LINK_MAX:
3494789Sahrens 		*valp = ULONG_MAX;
3495789Sahrens 		return (0);
3496789Sahrens 
3497789Sahrens 	case _PC_FILESIZEBITS:
3498789Sahrens 		*valp = 64;
3499789Sahrens 		return (0);
3500789Sahrens 
3501789Sahrens 	case _PC_XATTR_EXISTS:
3502789Sahrens 		zp = VTOZ(vp);
3503789Sahrens 		zfsvfs = zp->z_zfsvfs;
3504789Sahrens 		ZFS_ENTER(zfsvfs);
3505789Sahrens 		*valp = 0;
3506789Sahrens 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
3507789Sahrens 		    ZXATTR | ZEXISTS | ZSHARED);
3508789Sahrens 		if (error == 0) {
3509789Sahrens 			zfs_dirent_unlock(dl);
3510789Sahrens 			if (!zfs_dirempty(xzp))
3511789Sahrens 				*valp = 1;
3512789Sahrens 			VN_RELE(ZTOV(xzp));
3513789Sahrens 		} else if (error == ENOENT) {
3514789Sahrens 			/*
3515789Sahrens 			 * If there aren't extended attributes, it's the
3516789Sahrens 			 * same as having zero of them.
3517789Sahrens 			 */
3518789Sahrens 			error = 0;
3519789Sahrens 		}
3520789Sahrens 		ZFS_EXIT(zfsvfs);
3521789Sahrens 		return (error);
3522789Sahrens 
3523789Sahrens 	case _PC_ACL_ENABLED:
3524789Sahrens 		*valp = _ACL_ACE_ENABLED;
3525789Sahrens 		return (0);
3526789Sahrens 
3527789Sahrens 	case _PC_MIN_HOLE_SIZE:
3528789Sahrens 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
3529789Sahrens 		return (0);
3530789Sahrens 
3531789Sahrens 	default:
3532789Sahrens 		return (fs_pathconf(vp, cmd, valp, cr));
3533789Sahrens 	}
3534789Sahrens }
3535789Sahrens 
3536789Sahrens /*ARGSUSED*/
3537789Sahrens static int
3538789Sahrens zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
3539789Sahrens {
3540789Sahrens 	znode_t *zp = VTOZ(vp);
3541789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3542789Sahrens 	int error;
3543789Sahrens 
3544789Sahrens 	ZFS_ENTER(zfsvfs);
3545789Sahrens 	error = zfs_getacl(zp, vsecp, cr);
3546789Sahrens 	ZFS_EXIT(zfsvfs);
3547789Sahrens 
3548789Sahrens 	return (error);
3549789Sahrens }
3550789Sahrens 
3551789Sahrens /*ARGSUSED*/
3552789Sahrens static int
3553789Sahrens zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
3554789Sahrens {
3555789Sahrens 	znode_t *zp = VTOZ(vp);
3556789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3557789Sahrens 	int error;
3558789Sahrens 
3559789Sahrens 	ZFS_ENTER(zfsvfs);
3560789Sahrens 	error = zfs_setacl(zp, vsecp, cr);
3561789Sahrens 	ZFS_EXIT(zfsvfs);
3562789Sahrens 	return (error);
3563789Sahrens }
3564789Sahrens 
3565789Sahrens /*
3566789Sahrens  * Predeclare these here so that the compiler assumes that
3567789Sahrens  * this is an "old style" function declaration that does
3568789Sahrens  * not include arguments => we won't get type mismatch errors
3569789Sahrens  * in the initializations that follow.
3570789Sahrens  */
3571789Sahrens static int zfs_inval();
3572789Sahrens static int zfs_isdir();
3573789Sahrens 
3574789Sahrens static int
3575789Sahrens zfs_inval()
3576789Sahrens {
3577789Sahrens 	return (EINVAL);
3578789Sahrens }
3579789Sahrens 
3580789Sahrens static int
3581789Sahrens zfs_isdir()
3582789Sahrens {
3583789Sahrens 	return (EISDIR);
3584789Sahrens }
3585789Sahrens /*
3586789Sahrens  * Directory vnode operations template
3587789Sahrens  */
3588789Sahrens vnodeops_t *zfs_dvnodeops;
3589789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = {
3590789Sahrens 	VOPNAME_OPEN, zfs_open,
3591789Sahrens 	VOPNAME_CLOSE, zfs_close,
3592789Sahrens 	VOPNAME_READ, zfs_isdir,
3593789Sahrens 	VOPNAME_WRITE, zfs_isdir,
3594789Sahrens 	VOPNAME_IOCTL, zfs_ioctl,
3595789Sahrens 	VOPNAME_GETATTR, zfs_getattr,
3596789Sahrens 	VOPNAME_SETATTR, zfs_setattr,
3597789Sahrens 	VOPNAME_ACCESS, zfs_access,
3598789Sahrens 	VOPNAME_LOOKUP, zfs_lookup,
3599789Sahrens 	VOPNAME_CREATE, zfs_create,
3600789Sahrens 	VOPNAME_REMOVE, zfs_remove,
3601789Sahrens 	VOPNAME_LINK, zfs_link,
3602789Sahrens 	VOPNAME_RENAME, zfs_rename,
3603789Sahrens 	VOPNAME_MKDIR, zfs_mkdir,
3604789Sahrens 	VOPNAME_RMDIR, zfs_rmdir,
3605789Sahrens 	VOPNAME_READDIR, zfs_readdir,
3606789Sahrens 	VOPNAME_SYMLINK, zfs_symlink,
3607789Sahrens 	VOPNAME_FSYNC, zfs_fsync,
3608789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3609789Sahrens 	VOPNAME_FID, zfs_fid,
3610789Sahrens 	VOPNAME_SEEK, zfs_seek,
3611789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3612789Sahrens 	VOPNAME_GETSECATTR, zfs_getsecattr,
3613789Sahrens 	VOPNAME_SETSECATTR, zfs_setsecattr,
3614789Sahrens 	NULL, NULL
3615789Sahrens };
3616789Sahrens 
3617789Sahrens /*
3618789Sahrens  * Regular file vnode operations template
3619789Sahrens  */
3620789Sahrens vnodeops_t *zfs_fvnodeops;
3621789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = {
3622789Sahrens 	VOPNAME_OPEN, zfs_open,
3623789Sahrens 	VOPNAME_CLOSE, zfs_close,
3624789Sahrens 	VOPNAME_READ, zfs_read,
3625789Sahrens 	VOPNAME_WRITE, zfs_write,
3626789Sahrens 	VOPNAME_IOCTL, zfs_ioctl,
3627789Sahrens 	VOPNAME_GETATTR, zfs_getattr,
3628789Sahrens 	VOPNAME_SETATTR, zfs_setattr,
3629789Sahrens 	VOPNAME_ACCESS, zfs_access,
3630789Sahrens 	VOPNAME_LOOKUP, zfs_lookup,
3631789Sahrens 	VOPNAME_RENAME, zfs_rename,
3632789Sahrens 	VOPNAME_FSYNC, zfs_fsync,
3633789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p)zfs_inactive,
3634789Sahrens 	VOPNAME_FID, zfs_fid,
3635789Sahrens 	VOPNAME_SEEK, zfs_seek,
3636789Sahrens 	VOPNAME_FRLOCK, zfs_frlock,
3637789Sahrens 	VOPNAME_SPACE, zfs_space,
3638789Sahrens 	VOPNAME_GETPAGE, zfs_getpage,
3639789Sahrens 	VOPNAME_PUTPAGE, zfs_putpage,
3640789Sahrens 	VOPNAME_MAP, (fs_generic_func_p) zfs_map,
3641789Sahrens 	VOPNAME_ADDMAP, (fs_generic_func_p) zfs_addmap,
3642789Sahrens 	VOPNAME_DELMAP, zfs_delmap,
3643789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3644789Sahrens 	VOPNAME_GETSECATTR, zfs_getsecattr,
3645789Sahrens 	VOPNAME_SETSECATTR, zfs_setsecattr,
3646789Sahrens 	VOPNAME_VNEVENT, fs_vnevent_support,
3647789Sahrens 	NULL, NULL
3648789Sahrens };
3649789Sahrens 
3650789Sahrens /*
3651789Sahrens  * Symbolic link vnode operations template
3652789Sahrens  */
3653789Sahrens vnodeops_t *zfs_symvnodeops;
3654789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = {
3655789Sahrens 	VOPNAME_GETATTR, zfs_getattr,
3656789Sahrens 	VOPNAME_SETATTR, zfs_setattr,
3657789Sahrens 	VOPNAME_ACCESS, zfs_access,
3658789Sahrens 	VOPNAME_RENAME, zfs_rename,
3659789Sahrens 	VOPNAME_READLINK, zfs_readlink,
3660789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3661789Sahrens 	VOPNAME_FID, zfs_fid,
3662789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3663789Sahrens 	VOPNAME_VNEVENT, fs_vnevent_support,
3664789Sahrens 	NULL, NULL
3665789Sahrens };
3666789Sahrens 
3667789Sahrens /*
3668789Sahrens  * Extended attribute directory vnode operations template
3669789Sahrens  *	This template is identical to the directory vnodes
3670789Sahrens  *	operation template except for restricted operations:
3671789Sahrens  *		VOP_MKDIR()
3672789Sahrens  *		VOP_SYMLINK()
3673789Sahrens  * Note that there are other restrictions embedded in:
3674789Sahrens  *	zfs_create()	- restrict type to VREG
3675789Sahrens  *	zfs_link()	- no links into/out of attribute space
3676789Sahrens  *	zfs_rename()	- no moves into/out of attribute space
3677789Sahrens  */
3678789Sahrens vnodeops_t *zfs_xdvnodeops;
3679789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = {
3680789Sahrens 	VOPNAME_OPEN, zfs_open,
3681789Sahrens 	VOPNAME_CLOSE, zfs_close,
3682789Sahrens 	VOPNAME_IOCTL, zfs_ioctl,
3683789Sahrens 	VOPNAME_GETATTR, zfs_getattr,
3684789Sahrens 	VOPNAME_SETATTR, zfs_setattr,
3685789Sahrens 	VOPNAME_ACCESS, zfs_access,
3686789Sahrens 	VOPNAME_LOOKUP, zfs_lookup,
3687789Sahrens 	VOPNAME_CREATE, zfs_create,
3688789Sahrens 	VOPNAME_REMOVE, zfs_remove,
3689789Sahrens 	VOPNAME_LINK, zfs_link,
3690789Sahrens 	VOPNAME_RENAME, zfs_rename,
3691789Sahrens 	VOPNAME_MKDIR, zfs_inval,
3692789Sahrens 	VOPNAME_RMDIR, zfs_rmdir,
3693789Sahrens 	VOPNAME_READDIR, zfs_readdir,
3694789Sahrens 	VOPNAME_SYMLINK, zfs_inval,
3695789Sahrens 	VOPNAME_FSYNC, zfs_fsync,
3696789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3697789Sahrens 	VOPNAME_FID, zfs_fid,
3698789Sahrens 	VOPNAME_SEEK, zfs_seek,
3699789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3700789Sahrens 	VOPNAME_GETSECATTR, zfs_getsecattr,
3701789Sahrens 	VOPNAME_SETSECATTR, zfs_setsecattr,
3702789Sahrens 	VOPNAME_VNEVENT, fs_vnevent_support,
3703789Sahrens 	NULL, NULL
3704789Sahrens };
3705789Sahrens 
3706789Sahrens /*
3707789Sahrens  * Error vnode operations template
3708789Sahrens  */
3709789Sahrens vnodeops_t *zfs_evnodeops;
3710789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = {
3711789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3712789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3713789Sahrens 	NULL, NULL
3714789Sahrens };
3715