xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 2752:2b25a96fc433)
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>
432688Smaybee #include <sys/vm.h>
44789Sahrens #include <vm/seg_vn.h>
45789Sahrens #include <vm/pvn.h>
46789Sahrens #include <vm/as.h>
47789Sahrens #include <sys/mman.h>
48789Sahrens #include <sys/pathname.h>
49789Sahrens #include <sys/cmn_err.h>
50789Sahrens #include <sys/errno.h>
51789Sahrens #include <sys/unistd.h>
52789Sahrens #include <sys/zfs_vfsops.h>
53789Sahrens #include <sys/zfs_dir.h>
54789Sahrens #include <sys/zfs_acl.h>
55789Sahrens #include <sys/zfs_ioctl.h>
56789Sahrens #include <sys/fs/zfs.h>
57789Sahrens #include <sys/dmu.h>
58789Sahrens #include <sys/spa.h>
59789Sahrens #include <sys/txg.h>
60789Sahrens #include <sys/dbuf.h>
61789Sahrens #include <sys/zap.h>
62789Sahrens #include <sys/dirent.h>
63789Sahrens #include <sys/policy.h>
64789Sahrens #include <sys/sunddi.h>
65789Sahrens #include <sys/filio.h>
66789Sahrens #include "fs/fs_subr.h"
67789Sahrens #include <sys/zfs_ctldir.h>
681484Sek110237 #include <sys/dnlc.h>
691669Sperrin #include <sys/zfs_rlock.h>
70789Sahrens 
71789Sahrens /*
72789Sahrens  * Programming rules.
73789Sahrens  *
74789Sahrens  * Each vnode op performs some logical unit of work.  To do this, the ZPL must
75789Sahrens  * properly lock its in-core state, create a DMU transaction, do the work,
76789Sahrens  * record this work in the intent log (ZIL), commit the DMU transaction,
77789Sahrens  * and wait the the intent log to commit if it's is a synchronous operation.
78789Sahrens  * Morover, the vnode ops must work in both normal and log replay context.
79789Sahrens  * The ordering of events is important to avoid deadlocks and references
80789Sahrens  * to freed memory.  The example below illustrates the following Big Rules:
81789Sahrens  *
82789Sahrens  *  (1) A check must be made in each zfs thread for a mounted file system.
83789Sahrens  *	This is done avoiding races using ZFS_ENTER(zfsvfs).
84789Sahrens  *	A ZFS_EXIT(zfsvfs) is needed before all returns.
85789Sahrens  *
86789Sahrens  *  (2)	VN_RELE() should always be the last thing except for zil_commit()
872638Sperrin  *	(if necessary) and ZFS_EXIT(). This is for 3 reasons:
88789Sahrens  *	First, if it's the last reference, the vnode/znode
89789Sahrens  *	can be freed, so the zp may point to freed memory.  Second, the last
90789Sahrens  *	reference will call zfs_zinactive(), which may induce a lot of work --
911669Sperrin  *	pushing cached pages (which acquires range locks) and syncing out
92789Sahrens  *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
93789Sahrens  *	which could deadlock the system if you were already holding one.
94789Sahrens  *
951757Sperrin  *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
961757Sperrin  *	as they can span dmu_tx_assign() calls.
971757Sperrin  *
981757Sperrin  *  (4)	Always pass zfsvfs->z_assign as the second argument to dmu_tx_assign().
99789Sahrens  *	In normal operation, this will be TXG_NOWAIT.  During ZIL replay,
100789Sahrens  *	it will be a specific txg.  Either way, dmu_tx_assign() never blocks.
101789Sahrens  *	This is critical because we don't want to block while holding locks.
102789Sahrens  *	Note, in particular, that if a lock is sometimes acquired before
103789Sahrens  *	the tx assigns, and sometimes after (e.g. z_lock), then failing to
104789Sahrens  *	use a non-blocking assign can deadlock the system.  The scenario:
105789Sahrens  *
106789Sahrens  *	Thread A has grabbed a lock before calling dmu_tx_assign().
107789Sahrens  *	Thread B is in an already-assigned tx, and blocks for this lock.
108789Sahrens  *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
109789Sahrens  *	forever, because the previous txg can't quiesce until B's tx commits.
110789Sahrens  *
111789Sahrens  *	If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
1122113Sahrens  *	then drop all locks, call dmu_tx_wait(), and try again.
113789Sahrens  *
1141757Sperrin  *  (5)	If the operation succeeded, generate the intent log entry for it
115789Sahrens  *	before dropping locks.  This ensures that the ordering of events
116789Sahrens  *	in the intent log matches the order in which they actually occurred.
117789Sahrens  *
1181757Sperrin  *  (6)	At the end of each vnode op, the DMU tx must always commit,
119789Sahrens  *	regardless of whether there were any errors.
120789Sahrens  *
1212638Sperrin  *  (7)	After dropping all locks, invoke zil_commit(zilog, seq, foid)
122789Sahrens  *	to ensure that synchronous semantics are provided when necessary.
123789Sahrens  *
124789Sahrens  * In general, this is how things should be ordered in each vnode op:
125789Sahrens  *
126789Sahrens  *	ZFS_ENTER(zfsvfs);		// exit if unmounted
127789Sahrens  * top:
128789Sahrens  *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
129789Sahrens  *	rw_enter(...);			// grab any other locks you need
130789Sahrens  *	tx = dmu_tx_create(...);	// get DMU tx
131789Sahrens  *	dmu_tx_hold_*();		// hold each object you might modify
132789Sahrens  *	error = dmu_tx_assign(tx, zfsvfs->z_assign);	// try to assign
133789Sahrens  *	if (error) {
134789Sahrens  *		rw_exit(...);		// drop locks
135789Sahrens  *		zfs_dirent_unlock(dl);	// unlock directory entry
136789Sahrens  *		VN_RELE(...);		// release held vnodes
137789Sahrens  *		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1382113Sahrens  *			dmu_tx_wait(tx);
1392113Sahrens  *			dmu_tx_abort(tx);
140789Sahrens  *			goto top;
141789Sahrens  *		}
1422113Sahrens  *		dmu_tx_abort(tx);	// abort DMU tx
143789Sahrens  *		ZFS_EXIT(zfsvfs);	// finished in zfs
144789Sahrens  *		return (error);		// really out of space
145789Sahrens  *	}
146789Sahrens  *	error = do_real_work();		// do whatever this VOP does
147789Sahrens  *	if (error == 0)
1482638Sperrin  *		zfs_log_*(...);		// on success, make ZIL entry
149789Sahrens  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
150789Sahrens  *	rw_exit(...);			// drop locks
151789Sahrens  *	zfs_dirent_unlock(dl);		// unlock directory entry
152789Sahrens  *	VN_RELE(...);			// release held vnodes
1532638Sperrin  *	zil_commit(zilog, seq, foid);	// synchronous when necessary
154789Sahrens  *	ZFS_EXIT(zfsvfs);		// finished in zfs
155789Sahrens  *	return (error);			// done, report error
156789Sahrens  */
157789Sahrens 
158789Sahrens /* ARGSUSED */
159789Sahrens static int
160789Sahrens zfs_open(vnode_t **vpp, int flag, cred_t *cr)
161789Sahrens {
162789Sahrens 	return (0);
163789Sahrens }
164789Sahrens 
165789Sahrens /* ARGSUSED */
166789Sahrens static int
167789Sahrens zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr)
168789Sahrens {
169789Sahrens 	/*
170789Sahrens 	 * Clean up any locks held by this process on the vp.
171789Sahrens 	 */
172789Sahrens 	cleanlocks(vp, ddi_get_pid(), 0);
173789Sahrens 	cleanshares(vp, ddi_get_pid());
174789Sahrens 
175789Sahrens 	return (0);
176789Sahrens }
177789Sahrens 
178789Sahrens /*
179789Sahrens  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
180789Sahrens  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
181789Sahrens  */
182789Sahrens static int
183789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off)
184789Sahrens {
185789Sahrens 	znode_t	*zp = VTOZ(vp);
186789Sahrens 	uint64_t noff = (uint64_t)*off; /* new offset */
187789Sahrens 	uint64_t file_sz;
188789Sahrens 	int error;
189789Sahrens 	boolean_t hole;
190789Sahrens 
191789Sahrens 	file_sz = zp->z_phys->zp_size;
192789Sahrens 	if (noff >= file_sz)  {
193789Sahrens 		return (ENXIO);
194789Sahrens 	}
195789Sahrens 
196789Sahrens 	if (cmd == _FIO_SEEK_HOLE)
197789Sahrens 		hole = B_TRUE;
198789Sahrens 	else
199789Sahrens 		hole = B_FALSE;
200789Sahrens 
201789Sahrens 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
202789Sahrens 
203789Sahrens 	/* end of file? */
204789Sahrens 	if ((error == ESRCH) || (noff > file_sz)) {
205789Sahrens 		/*
206789Sahrens 		 * Handle the virtual hole at the end of file.
207789Sahrens 		 */
208789Sahrens 		if (hole) {
209789Sahrens 			*off = file_sz;
210789Sahrens 			return (0);
211789Sahrens 		}
212789Sahrens 		return (ENXIO);
213789Sahrens 	}
214789Sahrens 
215789Sahrens 	if (noff < *off)
216789Sahrens 		return (error);
217789Sahrens 	*off = noff;
218789Sahrens 	return (error);
219789Sahrens }
220789Sahrens 
221789Sahrens /* ARGSUSED */
222789Sahrens static int
223789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
224789Sahrens     int *rvalp)
225789Sahrens {
226789Sahrens 	offset_t off;
227789Sahrens 	int error;
228789Sahrens 	zfsvfs_t *zfsvfs;
229789Sahrens 
230789Sahrens 	switch (com) {
231789Sahrens 	    case _FIOFFS:
232789Sahrens 		return (zfs_sync(vp->v_vfsp, 0, cred));
233789Sahrens 
2341544Seschrock 		/*
2351544Seschrock 		 * The following two ioctls are used by bfu.  Faking out,
2361544Seschrock 		 * necessary to avoid bfu errors.
2371544Seschrock 		 */
2381544Seschrock 	    case _FIOGDIO:
2391544Seschrock 	    case _FIOSDIO:
2401544Seschrock 		return (0);
2411544Seschrock 
242789Sahrens 	    case _FIO_SEEK_DATA:
243789Sahrens 	    case _FIO_SEEK_HOLE:
244789Sahrens 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
245789Sahrens 			return (EFAULT);
246789Sahrens 
247789Sahrens 		zfsvfs = VTOZ(vp)->z_zfsvfs;
248789Sahrens 		ZFS_ENTER(zfsvfs);
249789Sahrens 
250789Sahrens 		/* offset parameter is in/out */
251789Sahrens 		error = zfs_holey(vp, com, &off);
252789Sahrens 		ZFS_EXIT(zfsvfs);
253789Sahrens 		if (error)
254789Sahrens 			return (error);
255789Sahrens 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
256789Sahrens 			return (EFAULT);
257789Sahrens 		return (0);
258789Sahrens 	}
259789Sahrens 	return (ENOTTY);
260789Sahrens }
261789Sahrens 
262789Sahrens /*
263789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
264789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
265789Sahrens  *
266789Sahrens  * On Write:	If we find a memory mapped page, we write to *both*
267789Sahrens  *		the page and the dmu buffer.
268789Sahrens  *
269789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
270789Sahrens  *	the file is memory mapped.
271789Sahrens  */
272789Sahrens static int
273789Sahrens mappedwrite(vnode_t *vp, uint64_t woff, int nbytes, uio_t *uio, dmu_tx_t *tx)
274789Sahrens {
275789Sahrens 	znode_t	*zp = VTOZ(vp);
276789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
277789Sahrens 	int64_t	start, off;
278789Sahrens 	int len = nbytes;
279789Sahrens 	int error = 0;
280789Sahrens 
281789Sahrens 	start = uio->uio_loffset;
282789Sahrens 	off = start & PAGEOFFSET;
283789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
284789Sahrens 		page_t *pp;
285789Sahrens 		uint64_t bytes = MIN(PAGESIZE - off, len);
286789Sahrens 
287789Sahrens 		/*
288789Sahrens 		 * We don't want a new page to "appear" in the middle of
289789Sahrens 		 * the file update (because it may not get the write
290789Sahrens 		 * update data), so we grab a lock to block
291789Sahrens 		 * zfs_getpage().
292789Sahrens 		 */
293789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
294789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
295789Sahrens 			caddr_t va;
296789Sahrens 
297789Sahrens 			rw_exit(&zp->z_map_lock);
298789Sahrens 			va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L);
299789Sahrens 			error = uiomove(va+off, bytes, UIO_WRITE, uio);
300789Sahrens 			if (error == 0) {
301789Sahrens 				dmu_write(zfsvfs->z_os, zp->z_id,
302789Sahrens 				    woff, bytes, va+off, tx);
303789Sahrens 			}
304789Sahrens 			ppmapout(va);
305789Sahrens 			page_unlock(pp);
306789Sahrens 		} else {
307789Sahrens 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
308789Sahrens 			    woff, bytes, uio, tx);
309789Sahrens 			rw_exit(&zp->z_map_lock);
310789Sahrens 		}
311789Sahrens 		len -= bytes;
312789Sahrens 		woff += bytes;
313789Sahrens 		off = 0;
314789Sahrens 		if (error)
315789Sahrens 			break;
316789Sahrens 	}
317789Sahrens 	return (error);
318789Sahrens }
319789Sahrens 
320789Sahrens /*
321789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
322789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
323789Sahrens  *
324789Sahrens  * On Read:	We "read" preferentially from memory mapped pages,
325789Sahrens  *		else we default from the dmu buffer.
326789Sahrens  *
327789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
328789Sahrens  *	the file is memory mapped.
329789Sahrens  */
330789Sahrens static int
331789Sahrens mappedread(vnode_t *vp, char *addr, int nbytes, uio_t *uio)
332789Sahrens {
333789Sahrens 	int64_t	start, off, bytes;
334789Sahrens 	int len = nbytes;
335789Sahrens 	int error = 0;
336789Sahrens 
337789Sahrens 	start = uio->uio_loffset;
338789Sahrens 	off = start & PAGEOFFSET;
339789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
340789Sahrens 		page_t *pp;
341789Sahrens 
342789Sahrens 		bytes = MIN(PAGESIZE - off, len);
343789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
344789Sahrens 			caddr_t va;
345789Sahrens 
3462688Smaybee 			va = ppmapin(pp, PROT_READ, (caddr_t)-1L);
347789Sahrens 			error = uiomove(va + off, bytes, UIO_READ, uio);
348789Sahrens 			ppmapout(va);
349789Sahrens 			page_unlock(pp);
350789Sahrens 		} else {
351789Sahrens 			/* XXX use dmu_read here? */
352789Sahrens 			error = uiomove(addr, bytes, UIO_READ, uio);
353789Sahrens 		}
354789Sahrens 		len -= bytes;
355789Sahrens 		addr += bytes;
356789Sahrens 		off = 0;
357789Sahrens 		if (error)
358789Sahrens 			break;
359789Sahrens 	}
360789Sahrens 	return (error);
361789Sahrens }
362789Sahrens 
363789Sahrens uint_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
364789Sahrens 
365789Sahrens /*
366789Sahrens  * Read bytes from specified file into supplied buffer.
367789Sahrens  *
368789Sahrens  *	IN:	vp	- vnode of file to be read from.
369789Sahrens  *		uio	- structure supplying read location, range info,
370789Sahrens  *			  and return buffer.
371789Sahrens  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
372789Sahrens  *		cr	- credentials of caller.
373789Sahrens  *
374789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
375789Sahrens  *
376789Sahrens  *	RETURN:	0 if success
377789Sahrens  *		error code if failure
378789Sahrens  *
379789Sahrens  * Side Effects:
380789Sahrens  *	vp - atime updated if byte count > 0
381789Sahrens  */
382789Sahrens /* ARGSUSED */
383789Sahrens static int
384789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
385789Sahrens {
386789Sahrens 	znode_t		*zp = VTOZ(vp);
387789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
388789Sahrens 	uint64_t	delta;
389789Sahrens 	ssize_t		n, size, cnt, ndone;
390789Sahrens 	int		error, i, numbufs;
391789Sahrens 	dmu_buf_t	*dbp, **dbpp;
3921669Sperrin 	rl_t		*rl;
393789Sahrens 
394789Sahrens 	ZFS_ENTER(zfsvfs);
395789Sahrens 
396789Sahrens 	/*
397789Sahrens 	 * Validate file offset
398789Sahrens 	 */
399789Sahrens 	if (uio->uio_loffset < (offset_t)0) {
400789Sahrens 		ZFS_EXIT(zfsvfs);
401789Sahrens 		return (EINVAL);
402789Sahrens 	}
403789Sahrens 
404789Sahrens 	/*
405789Sahrens 	 * Fasttrack empty reads
406789Sahrens 	 */
407789Sahrens 	if (uio->uio_resid == 0) {
408789Sahrens 		ZFS_EXIT(zfsvfs);
409789Sahrens 		return (0);
410789Sahrens 	}
411789Sahrens 
412789Sahrens 	/*
4131669Sperrin 	 * Check for mandatory locks
414789Sahrens 	 */
415789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
416789Sahrens 		if (error = chklock(vp, FREAD,
417789Sahrens 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
418789Sahrens 			ZFS_EXIT(zfsvfs);
419789Sahrens 			return (error);
420789Sahrens 		}
421789Sahrens 	}
422789Sahrens 
423789Sahrens 	/*
424789Sahrens 	 * If we're in FRSYNC mode, sync out this znode before reading it.
425789Sahrens 	 */
4262638Sperrin 	if (ioflag & FRSYNC)
4272638Sperrin 		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
428789Sahrens 
429789Sahrens 	/*
4301669Sperrin 	 * Lock the range against changes.
431789Sahrens 	 */
4321669Sperrin 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
4331669Sperrin 
434789Sahrens 	/*
435789Sahrens 	 * If we are reading past end-of-file we can skip
436789Sahrens 	 * to the end; but we might still need to set atime.
437789Sahrens 	 */
438789Sahrens 	if (uio->uio_loffset >= zp->z_phys->zp_size) {
439789Sahrens 		cnt = 0;
440789Sahrens 		error = 0;
441789Sahrens 		goto out;
442789Sahrens 	}
443789Sahrens 
444789Sahrens 	cnt = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset);
445789Sahrens 
446789Sahrens 	for (ndone = 0; ndone < cnt; ndone += zfs_read_chunk_size) {
447789Sahrens 		ASSERT(uio->uio_loffset < zp->z_phys->zp_size);
448789Sahrens 		n = MIN(zfs_read_chunk_size,
449789Sahrens 		    zp->z_phys->zp_size - uio->uio_loffset);
450789Sahrens 		n = MIN(n, cnt);
4512391Smaybee 		error = dmu_buf_hold_array_by_bonus(zp->z_dbuf,
4521544Seschrock 		    uio->uio_loffset, n, TRUE, FTAG, &numbufs, &dbpp);
4531544Seschrock 		if (error)
454789Sahrens 			goto out;
455789Sahrens 		/*
456789Sahrens 		 * Compute the adjustment to align the dmu buffers
457789Sahrens 		 * with the uio buffer.
458789Sahrens 		 */
459789Sahrens 		delta = uio->uio_loffset - dbpp[0]->db_offset;
460789Sahrens 
461789Sahrens 		for (i = 0; i < numbufs; i++) {
462789Sahrens 			if (n < 0)
463789Sahrens 				break;
464789Sahrens 			dbp = dbpp[i];
465789Sahrens 			size = dbp->db_size - delta;
466789Sahrens 			/*
467789Sahrens 			 * XXX -- this is correct, but may be suboptimal.
468789Sahrens 			 * If the pages are all clean, we don't need to
469789Sahrens 			 * go through mappedread().  Maybe the VMODSORT
470789Sahrens 			 * stuff can help us here.
471789Sahrens 			 */
472789Sahrens 			if (vn_has_cached_data(vp)) {
473789Sahrens 				error = mappedread(vp, (caddr_t)dbp->db_data +
474789Sahrens 				    delta, (n < size ? n : size), uio);
475789Sahrens 			} else {
476789Sahrens 				error = uiomove((caddr_t)dbp->db_data + delta,
477789Sahrens 					(n < size ? n : size), UIO_READ, uio);
478789Sahrens 			}
479789Sahrens 			if (error) {
4801544Seschrock 				dmu_buf_rele_array(dbpp, numbufs, FTAG);
481789Sahrens 				goto out;
482789Sahrens 			}
483789Sahrens 			n -= dbp->db_size;
484789Sahrens 			if (delta) {
485789Sahrens 				n += delta;
486789Sahrens 				delta = 0;
487789Sahrens 			}
488789Sahrens 		}
4891544Seschrock 		dmu_buf_rele_array(dbpp, numbufs, FTAG);
490789Sahrens 	}
491789Sahrens out:
4922237Smaybee 	zfs_range_unlock(rl);
493789Sahrens 
494789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
495789Sahrens 	ZFS_EXIT(zfsvfs);
496789Sahrens 	return (error);
497789Sahrens }
498789Sahrens 
499789Sahrens /*
500789Sahrens  * Fault in the pages of the first n bytes specified by the uio structure.
501789Sahrens  * 1 byte in each page is touched and the uio struct is unmodified.
502789Sahrens  * Any error will exit this routine as this is only a best
503789Sahrens  * attempt to get the pages resident. This is a copy of ufs_trans_touch().
504789Sahrens  */
505789Sahrens static void
506789Sahrens zfs_prefault_write(ssize_t n, struct uio *uio)
507789Sahrens {
508789Sahrens 	struct iovec *iov;
509789Sahrens 	ulong_t cnt, incr;
510789Sahrens 	caddr_t p;
511789Sahrens 	uint8_t tmp;
512789Sahrens 
513789Sahrens 	iov = uio->uio_iov;
514789Sahrens 
515789Sahrens 	while (n) {
516789Sahrens 		cnt = MIN(iov->iov_len, n);
517789Sahrens 		if (cnt == 0) {
518789Sahrens 			/* empty iov entry */
519789Sahrens 			iov++;
520789Sahrens 			continue;
521789Sahrens 		}
522789Sahrens 		n -= cnt;
523789Sahrens 		/*
524789Sahrens 		 * touch each page in this segment.
525789Sahrens 		 */
526789Sahrens 		p = iov->iov_base;
527789Sahrens 		while (cnt) {
528789Sahrens 			switch (uio->uio_segflg) {
529789Sahrens 			case UIO_USERSPACE:
530789Sahrens 			case UIO_USERISPACE:
531789Sahrens 				if (fuword8(p, &tmp))
532789Sahrens 					return;
533789Sahrens 				break;
534789Sahrens 			case UIO_SYSSPACE:
535789Sahrens 				if (kcopy(p, &tmp, 1))
536789Sahrens 					return;
537789Sahrens 				break;
538789Sahrens 			}
539789Sahrens 			incr = MIN(cnt, PAGESIZE);
540789Sahrens 			p += incr;
541789Sahrens 			cnt -= incr;
542789Sahrens 		}
543789Sahrens 		/*
544789Sahrens 		 * touch the last byte in case it straddles a page.
545789Sahrens 		 */
546789Sahrens 		p--;
547789Sahrens 		switch (uio->uio_segflg) {
548789Sahrens 		case UIO_USERSPACE:
549789Sahrens 		case UIO_USERISPACE:
550789Sahrens 			if (fuword8(p, &tmp))
551789Sahrens 				return;
552789Sahrens 			break;
553789Sahrens 		case UIO_SYSSPACE:
554789Sahrens 			if (kcopy(p, &tmp, 1))
555789Sahrens 				return;
556789Sahrens 			break;
557789Sahrens 		}
558789Sahrens 		iov++;
559789Sahrens 	}
560789Sahrens }
561789Sahrens 
562789Sahrens /*
563789Sahrens  * Write the bytes to a file.
564789Sahrens  *
565789Sahrens  *	IN:	vp	- vnode of file to be written to.
566789Sahrens  *		uio	- structure supplying write location, range info,
567789Sahrens  *			  and data buffer.
568789Sahrens  *		ioflag	- FAPPEND flag set if in append mode.
569789Sahrens  *		cr	- credentials of caller.
570789Sahrens  *
571789Sahrens  *	OUT:	uio	- updated offset and range.
572789Sahrens  *
573789Sahrens  *	RETURN:	0 if success
574789Sahrens  *		error code if failure
575789Sahrens  *
576789Sahrens  * Timestamps:
577789Sahrens  *	vp - ctime|mtime updated if byte count > 0
578789Sahrens  */
579789Sahrens /* ARGSUSED */
580789Sahrens static int
581789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
582789Sahrens {
583789Sahrens 	znode_t		*zp = VTOZ(vp);
584789Sahrens 	rlim64_t	limit = uio->uio_llimit;
585789Sahrens 	ssize_t		start_resid = uio->uio_resid;
586789Sahrens 	ssize_t		tx_bytes;
587789Sahrens 	uint64_t	end_size;
588789Sahrens 	dmu_tx_t	*tx;
589789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
590789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
591789Sahrens 	offset_t	woff;
592789Sahrens 	ssize_t		n, nbytes;
5931669Sperrin 	rl_t		*rl;
594789Sahrens 	int		max_blksz = zfsvfs->z_max_blksz;
5951669Sperrin 	int		error;
596789Sahrens 
597789Sahrens 	/*
598789Sahrens 	 * Fasttrack empty write
599789Sahrens 	 */
6001669Sperrin 	n = start_resid;
601789Sahrens 	if (n == 0)
602789Sahrens 		return (0);
603789Sahrens 
6041669Sperrin 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
6051669Sperrin 		limit = MAXOFFSET_T;
6061669Sperrin 
607789Sahrens 	ZFS_ENTER(zfsvfs);
608789Sahrens 
609789Sahrens 	/*
6102237Smaybee 	 * Pre-fault the pages to ensure slow (eg NFS) pages
6111669Sperrin 	 * don't hold up txg.
612789Sahrens 	 */
6132237Smaybee 	zfs_prefault_write(n, uio);
614789Sahrens 
615789Sahrens 	/*
616789Sahrens 	 * If in append mode, set the io offset pointer to eof.
617789Sahrens 	 */
6181669Sperrin 	if (ioflag & FAPPEND) {
6191669Sperrin 		/*
6201669Sperrin 		 * Range lock for a file append:
6211669Sperrin 		 * The value for the start of range will be determined by
6221669Sperrin 		 * zfs_range_lock() (to guarantee append semantics).
6231669Sperrin 		 * If this write will cause the block size to increase,
6241669Sperrin 		 * zfs_range_lock() will lock the entire file, so we must
6251669Sperrin 		 * later reduce the range after we grow the block size.
6261669Sperrin 		 */
6271669Sperrin 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
6281669Sperrin 		if (rl->r_len == UINT64_MAX) {
6291669Sperrin 			/* overlocked, zp_size can't change */
6301669Sperrin 			woff = uio->uio_loffset = zp->z_phys->zp_size;
6311669Sperrin 		} else {
6321669Sperrin 			woff = uio->uio_loffset = rl->r_off;
6331669Sperrin 		}
634789Sahrens 	} else {
635789Sahrens 		woff = uio->uio_loffset;
636789Sahrens 		/*
637789Sahrens 		 * Validate file offset
638789Sahrens 		 */
639789Sahrens 		if (woff < 0) {
640789Sahrens 			ZFS_EXIT(zfsvfs);
641789Sahrens 			return (EINVAL);
642789Sahrens 		}
643789Sahrens 
644789Sahrens 		/*
6451669Sperrin 		 * If we need to grow the block size then zfs_range_lock()
6461669Sperrin 		 * will lock a wider range than we request here.
6471669Sperrin 		 * Later after growing the block size we reduce the range.
648789Sahrens 		 */
6491669Sperrin 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
650789Sahrens 	}
651789Sahrens 
652789Sahrens 	if (woff >= limit) {
653789Sahrens 		error = EFBIG;
654789Sahrens 		goto no_tx_done;
655789Sahrens 	}
656789Sahrens 
657789Sahrens 	if ((woff + n) > limit || woff > (limit - n))
658789Sahrens 		n = limit - woff;
659789Sahrens 
660789Sahrens 	/*
6611669Sperrin 	 * Check for mandatory locks
662789Sahrens 	 */
663789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) &&
664789Sahrens 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0)
665789Sahrens 		goto no_tx_done;
6661669Sperrin 	end_size = MAX(zp->z_phys->zp_size, woff + n);
667789Sahrens top:
668789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
669789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
670789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
671789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
672789Sahrens 	if (error) {
673789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
6742113Sahrens 			dmu_tx_wait(tx);
6752113Sahrens 			dmu_tx_abort(tx);
676789Sahrens 			goto top;
677789Sahrens 		}
6782113Sahrens 		dmu_tx_abort(tx);
679789Sahrens 		goto no_tx_done;
680789Sahrens 	}
681789Sahrens 
6821669Sperrin 	/*
6831669Sperrin 	 * If zfs_range_lock() over-locked we grow the blocksize
6841669Sperrin 	 * and then reduce the lock range.
6851669Sperrin 	 */
6861669Sperrin 	if (rl->r_len == UINT64_MAX) {
687789Sahrens 		uint64_t new_blksz;
6881669Sperrin 
689789Sahrens 		if (zp->z_blksz > max_blksz) {
690789Sahrens 			ASSERT(!ISP2(zp->z_blksz));
691789Sahrens 			new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
692789Sahrens 		} else {
693789Sahrens 			new_blksz = MIN(end_size, max_blksz);
694789Sahrens 		}
6951669Sperrin 		zfs_grow_blocksize(zp, new_blksz, tx);
6962237Smaybee 		zfs_range_reduce(rl, woff, n);
697789Sahrens 	}
698789Sahrens 
699789Sahrens 	/*
700789Sahrens 	 * The file data does not fit in the znode "cache", so we
701789Sahrens 	 * will be writing to the file block data buffers.
702789Sahrens 	 * Each buffer will be written in a separate transaction;
703789Sahrens 	 * this keeps the intent log records small and allows us
704789Sahrens 	 * to do more fine-grained space accounting.
705789Sahrens 	 */
706789Sahrens 	while (n > 0) {
707789Sahrens 		/*
708789Sahrens 		 * XXX - should we really limit each write to z_max_blksz?
709789Sahrens 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
710789Sahrens 		 */
711789Sahrens 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
712789Sahrens 		rw_enter(&zp->z_map_lock, RW_READER);
713789Sahrens 
714789Sahrens 		tx_bytes = uio->uio_resid;
715789Sahrens 		if (vn_has_cached_data(vp)) {
716789Sahrens 			rw_exit(&zp->z_map_lock);
717789Sahrens 			error = mappedwrite(vp, woff, nbytes, uio, tx);
718789Sahrens 		} else {
719789Sahrens 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
720789Sahrens 			    woff, nbytes, uio, tx);
721789Sahrens 			rw_exit(&zp->z_map_lock);
722789Sahrens 		}
723789Sahrens 		tx_bytes -= uio->uio_resid;
724789Sahrens 
725789Sahrens 		if (error) {
726789Sahrens 			/* XXX - do we need to "clean up" the dmu buffer? */
727789Sahrens 			break;
728789Sahrens 		}
729789Sahrens 
730789Sahrens 		ASSERT(tx_bytes == nbytes);
731789Sahrens 
7321576Smarks 		/*
7331576Smarks 		 * Clear Set-UID/Set-GID bits on successful write if not
7341576Smarks 		 * privileged and at least one of the excute bits is set.
7351576Smarks 		 *
7361576Smarks 		 * It would be nice to to this after all writes have
7371576Smarks 		 * been done, but that would still expose the ISUID/ISGID
7381576Smarks 		 * to another app after the partial write is committed.
7391576Smarks 		 */
7401576Smarks 
7411576Smarks 		mutex_enter(&zp->z_acl_lock);
7421576Smarks 		if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) |
7431576Smarks 		    (S_IXUSR >> 6))) != 0 &&
7441576Smarks 		    (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 &&
7451576Smarks 		    secpolicy_vnode_setid_retain(cr,
7461576Smarks 		    (zp->z_phys->zp_mode & S_ISUID) != 0 &&
7471576Smarks 		    zp->z_phys->zp_uid == 0) != 0) {
7481576Smarks 			    zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID);
7491576Smarks 		}
7501576Smarks 		mutex_exit(&zp->z_acl_lock);
7511576Smarks 
752789Sahrens 		n -= nbytes;
753789Sahrens 		if (n <= 0)
754789Sahrens 			break;
755789Sahrens 
756789Sahrens 		/*
757789Sahrens 		 * We have more work ahead of us, so wrap up this transaction
758789Sahrens 		 * and start another.  Exact same logic as tx_done below.
759789Sahrens 		 */
760789Sahrens 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) {
761789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
762789Sahrens 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
763789Sahrens 			    uio->uio_loffset);
764789Sahrens 		}
765789Sahrens 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
7662638Sperrin 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes,
767789Sahrens 		    ioflag, uio);
768789Sahrens 		dmu_tx_commit(tx);
769789Sahrens 
770789Sahrens 		/*
771789Sahrens 		 * Start another transaction.
772789Sahrens 		 */
773789Sahrens 		woff = uio->uio_loffset;
774789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
775789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
776789Sahrens 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
777789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
778789Sahrens 		if (error) {
779789Sahrens 			if (error == ERESTART &&
780789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
7812113Sahrens 				dmu_tx_wait(tx);
7822113Sahrens 				dmu_tx_abort(tx);
783789Sahrens 				goto top;
784789Sahrens 			}
7852113Sahrens 			dmu_tx_abort(tx);
786789Sahrens 			goto no_tx_done;
787789Sahrens 		}
788789Sahrens 	}
789789Sahrens 
790789Sahrens tx_done:
791789Sahrens 
792789Sahrens 	if (tx_bytes != 0) {
793789Sahrens 		/*
794789Sahrens 		 * Update the file size if it has changed; account
795789Sahrens 		 * for possible concurrent updates.
796789Sahrens 		 */
797789Sahrens 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) {
798789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
799789Sahrens 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
800789Sahrens 			    uio->uio_loffset);
801789Sahrens 		}
802789Sahrens 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
8032638Sperrin 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes,
804789Sahrens 		    ioflag, uio);
805789Sahrens 	}
806789Sahrens 	dmu_tx_commit(tx);
807789Sahrens 
808789Sahrens 
809789Sahrens no_tx_done:
810789Sahrens 
8112237Smaybee 	zfs_range_unlock(rl);
812789Sahrens 
813789Sahrens 	/*
814789Sahrens 	 * If we're in replay mode, or we made no progress, return error.
815789Sahrens 	 * Otherwise, it's at least a partial write, so it's successful.
816789Sahrens 	 */
817789Sahrens 	if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) {
818789Sahrens 		ZFS_EXIT(zfsvfs);
819789Sahrens 		return (error);
820789Sahrens 	}
821789Sahrens 
8222638Sperrin 	if (ioflag & (FSYNC | FDSYNC))
8232638Sperrin 		zil_commit(zilog, zp->z_last_itx, zp->z_id);
824789Sahrens 
825789Sahrens 	ZFS_EXIT(zfsvfs);
826789Sahrens 	return (0);
827789Sahrens }
828789Sahrens 
8292237Smaybee void
8302237Smaybee zfs_get_done(dmu_buf_t *db, void *vrl)
8312237Smaybee {
8322237Smaybee 	rl_t *rl = (rl_t *)vrl;
8332237Smaybee 	vnode_t *vp = ZTOV(rl->r_zp);
8342237Smaybee 
8352237Smaybee 	dmu_buf_rele(db, rl);
8362237Smaybee 	zfs_range_unlock(rl);
8372237Smaybee 	VN_RELE(vp);
8382237Smaybee }
8392237Smaybee 
840789Sahrens /*
841789Sahrens  * Get data to generate a TX_WRITE intent log record.
842789Sahrens  */
843789Sahrens int
8442237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
845789Sahrens {
846789Sahrens 	zfsvfs_t *zfsvfs = arg;
847789Sahrens 	objset_t *os = zfsvfs->z_os;
848789Sahrens 	znode_t *zp;
849789Sahrens 	uint64_t off = lr->lr_offset;
8502237Smaybee 	dmu_buf_t *db;
8511669Sperrin 	rl_t *rl;
852789Sahrens 	int dlen = lr->lr_length;  		/* length of user data */
853789Sahrens 	int error = 0;
854789Sahrens 
855789Sahrens 	ASSERT(dlen != 0);
856789Sahrens 
857789Sahrens 	/*
8581669Sperrin 	 * Nothing to do if the file has been removed
859789Sahrens 	 */
860789Sahrens 	if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0)
861789Sahrens 		return (ENOENT);
8621669Sperrin 	if (zp->z_reap) {
863789Sahrens 		VN_RELE(ZTOV(zp));
864789Sahrens 		return (ENOENT);
865789Sahrens 	}
866789Sahrens 
867789Sahrens 	/*
868789Sahrens 	 * Write records come in two flavors: immediate and indirect.
869789Sahrens 	 * For small writes it's cheaper to store the data with the
870789Sahrens 	 * log record (immediate); for large writes it's cheaper to
871789Sahrens 	 * sync the data and get a pointer to it (indirect) so that
872789Sahrens 	 * we don't have to write the data twice.
873789Sahrens 	 */
8741669Sperrin 	if (buf != NULL) { /* immediate write */
8751669Sperrin 		rl = zfs_range_lock(zp, off, dlen, RL_READER);
8761669Sperrin 		/* test for truncation needs to be done while range locked */
8771669Sperrin 		if (off >= zp->z_phys->zp_size) {
8781669Sperrin 			error = ENOENT;
8791669Sperrin 			goto out;
8801669Sperrin 		}
8812449Smaybee 		VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf));
8821669Sperrin 	} else { /* indirect write */
8831669Sperrin 		uint64_t boff; /* block starting offset */
8841669Sperrin 
8852449Smaybee 		ASSERT3U(dlen, <=, zp->z_blksz);
886789Sahrens 		/*
8871669Sperrin 		 * Have to lock the whole block to ensure when it's
8881669Sperrin 		 * written out and it's checksum is being calculated
8891669Sperrin 		 * that no one can change the data. We need to re-check
8901669Sperrin 		 * blocksize after we get the lock in case it's changed!
891789Sahrens 		 */
8921669Sperrin 		for (;;) {
8931941Sperrin 			if (ISP2(zp->z_blksz)) {
8941941Sperrin 				boff = P2ALIGN_TYPED(off, zp->z_blksz,
8951941Sperrin 				    uint64_t);
8961941Sperrin 			} else {
8971941Sperrin 				boff = 0;
8981941Sperrin 			}
8991669Sperrin 			dlen = zp->z_blksz;
9001669Sperrin 			rl = zfs_range_lock(zp, boff, dlen, RL_READER);
9011669Sperrin 			if (zp->z_blksz == dlen)
9021669Sperrin 				break;
9032237Smaybee 			zfs_range_unlock(rl);
9041669Sperrin 		}
9051669Sperrin 		/* test for truncation needs to be done while range locked */
9061669Sperrin 		if (off >= zp->z_phys->zp_size) {
9071669Sperrin 			error = ENOENT;
9081669Sperrin 			goto out;
9091669Sperrin 		}
9102237Smaybee 		VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, rl, &db));
9112237Smaybee 		ASSERT(boff == db->db_offset);
9122237Smaybee 		lr->lr_blkoff = off - boff;
9132237Smaybee 		error = dmu_sync(zio, db, &lr->lr_blkptr,
9142237Smaybee 		    lr->lr_common.lrc_txg, zio ? zfs_get_done : NULL, rl);
9152237Smaybee 		/*
9162237Smaybee 		 * If we get EINPROGRESS, then we need to wait for a
9172237Smaybee 		 * write IO initiated by dmu_sync() to complete before
9182638Sperrin 		 * we can release this dbuf.  We will finish everything
9192237Smaybee 		 * up in the zfs_get_done() callback.
9202237Smaybee 		 */
9212237Smaybee 		if (error == EINPROGRESS)
9222237Smaybee 			return (0);
9232237Smaybee 		dmu_buf_rele(db, rl);
924789Sahrens 	}
9251669Sperrin out:
9262237Smaybee 	zfs_range_unlock(rl);
927789Sahrens 	VN_RELE(ZTOV(zp));
928789Sahrens 	return (error);
929789Sahrens }
930789Sahrens 
931789Sahrens /*ARGSUSED*/
932789Sahrens static int
933789Sahrens zfs_access(vnode_t *vp, int mode, int flags, cred_t *cr)
934789Sahrens {
935789Sahrens 	znode_t *zp = VTOZ(vp);
936789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
937789Sahrens 	int error;
938789Sahrens 
939789Sahrens 	ZFS_ENTER(zfsvfs);
940789Sahrens 	error = zfs_zaccess_rwx(zp, mode, cr);
941789Sahrens 	ZFS_EXIT(zfsvfs);
942789Sahrens 	return (error);
943789Sahrens }
944789Sahrens 
945789Sahrens /*
946789Sahrens  * Lookup an entry in a directory, or an extended attribute directory.
947789Sahrens  * If it exists, return a held vnode reference for it.
948789Sahrens  *
949789Sahrens  *	IN:	dvp	- vnode of directory to search.
950789Sahrens  *		nm	- name of entry to lookup.
951789Sahrens  *		pnp	- full pathname to lookup [UNUSED].
952789Sahrens  *		flags	- LOOKUP_XATTR set if looking for an attribute.
953789Sahrens  *		rdir	- root directory vnode [UNUSED].
954789Sahrens  *		cr	- credentials of caller.
955789Sahrens  *
956789Sahrens  *	OUT:	vpp	- vnode of located entry, NULL if not found.
957789Sahrens  *
958789Sahrens  *	RETURN:	0 if success
959789Sahrens  *		error code if failure
960789Sahrens  *
961789Sahrens  * Timestamps:
962789Sahrens  *	NA
963789Sahrens  */
964789Sahrens /* ARGSUSED */
965789Sahrens static int
966789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
967789Sahrens     int flags, vnode_t *rdir, cred_t *cr)
968789Sahrens {
969789Sahrens 
970789Sahrens 	znode_t *zdp = VTOZ(dvp);
971789Sahrens 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
972789Sahrens 	int	error;
973789Sahrens 
974789Sahrens 	ZFS_ENTER(zfsvfs);
975789Sahrens 
976789Sahrens 	*vpp = NULL;
977789Sahrens 
978789Sahrens 	if (flags & LOOKUP_XATTR) {
979789Sahrens 		/*
980789Sahrens 		 * We don't allow recursive attributes..
981789Sahrens 		 * Maybe someday we will.
982789Sahrens 		 */
983789Sahrens 		if (zdp->z_phys->zp_flags & ZFS_XATTR) {
984789Sahrens 			ZFS_EXIT(zfsvfs);
985789Sahrens 			return (EINVAL);
986789Sahrens 		}
987789Sahrens 
988789Sahrens 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr)) {
989789Sahrens 			ZFS_EXIT(zfsvfs);
990789Sahrens 			return (error);
991789Sahrens 		}
992789Sahrens 
993789Sahrens 		/*
994789Sahrens 		 * Do we have permission to get into attribute directory?
995789Sahrens 		 */
996789Sahrens 
997789Sahrens 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, cr)) {
998789Sahrens 			VN_RELE(*vpp);
999789Sahrens 		}
1000789Sahrens 
1001789Sahrens 		ZFS_EXIT(zfsvfs);
1002789Sahrens 		return (error);
1003789Sahrens 	}
1004789Sahrens 
10051512Sek110237 	if (dvp->v_type != VDIR) {
10061512Sek110237 		ZFS_EXIT(zfsvfs);
10071460Smarks 		return (ENOTDIR);
10081512Sek110237 	}
10091460Smarks 
1010789Sahrens 	/*
1011789Sahrens 	 * Check accessibility of directory.
1012789Sahrens 	 */
1013789Sahrens 
1014789Sahrens 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, cr)) {
1015789Sahrens 		ZFS_EXIT(zfsvfs);
1016789Sahrens 		return (error);
1017789Sahrens 	}
1018789Sahrens 
1019789Sahrens 	if ((error = zfs_dirlook(zdp, nm, vpp)) == 0) {
1020789Sahrens 
1021789Sahrens 		/*
1022789Sahrens 		 * Convert device special files
1023789Sahrens 		 */
1024789Sahrens 		if (IS_DEVVP(*vpp)) {
1025789Sahrens 			vnode_t	*svp;
1026789Sahrens 
1027789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1028789Sahrens 			VN_RELE(*vpp);
1029789Sahrens 			if (svp == NULL)
1030789Sahrens 				error = ENOSYS;
1031789Sahrens 			else
1032789Sahrens 				*vpp = svp;
1033789Sahrens 		}
1034789Sahrens 	}
1035789Sahrens 
1036789Sahrens 	ZFS_EXIT(zfsvfs);
1037789Sahrens 	return (error);
1038789Sahrens }
1039789Sahrens 
1040789Sahrens /*
1041789Sahrens  * Attempt to create a new entry in a directory.  If the entry
1042789Sahrens  * already exists, truncate the file if permissible, else return
1043789Sahrens  * an error.  Return the vp of the created or trunc'd file.
1044789Sahrens  *
1045789Sahrens  *	IN:	dvp	- vnode of directory to put new file entry in.
1046789Sahrens  *		name	- name of new file entry.
1047789Sahrens  *		vap	- attributes of new file.
1048789Sahrens  *		excl	- flag indicating exclusive or non-exclusive mode.
1049789Sahrens  *		mode	- mode to open file with.
1050789Sahrens  *		cr	- credentials of caller.
1051789Sahrens  *		flag	- large file flag [UNUSED].
1052789Sahrens  *
1053789Sahrens  *	OUT:	vpp	- vnode of created or trunc'd entry.
1054789Sahrens  *
1055789Sahrens  *	RETURN:	0 if success
1056789Sahrens  *		error code if failure
1057789Sahrens  *
1058789Sahrens  * Timestamps:
1059789Sahrens  *	dvp - ctime|mtime updated if new entry created
1060789Sahrens  *	 vp - ctime|mtime always, atime if new
1061789Sahrens  */
1062789Sahrens /* ARGSUSED */
1063789Sahrens static int
1064789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1065789Sahrens     int mode, vnode_t **vpp, cred_t *cr, int flag)
1066789Sahrens {
1067789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1068789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1069789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1070789Sahrens 	objset_t	*os = zfsvfs->z_os;
1071789Sahrens 	zfs_dirlock_t	*dl;
1072789Sahrens 	dmu_tx_t	*tx;
1073789Sahrens 	int		error;
1074789Sahrens 	uint64_t	zoid;
1075789Sahrens 
1076789Sahrens 	ZFS_ENTER(zfsvfs);
1077789Sahrens 
1078789Sahrens top:
1079789Sahrens 	*vpp = NULL;
1080789Sahrens 
1081789Sahrens 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1082789Sahrens 		vap->va_mode &= ~VSVTX;
1083789Sahrens 
1084789Sahrens 	if (*name == '\0') {
1085789Sahrens 		/*
1086789Sahrens 		 * Null component name refers to the directory itself.
1087789Sahrens 		 */
1088789Sahrens 		VN_HOLD(dvp);
1089789Sahrens 		zp = dzp;
1090789Sahrens 		dl = NULL;
1091789Sahrens 		error = 0;
1092789Sahrens 	} else {
1093789Sahrens 		/* possible VN_HOLD(zp) */
1094789Sahrens 		if (error = zfs_dirent_lock(&dl, dzp, name, &zp, 0)) {
1095789Sahrens 			if (strcmp(name, "..") == 0)
1096789Sahrens 				error = EISDIR;
1097789Sahrens 			ZFS_EXIT(zfsvfs);
1098789Sahrens 			return (error);
1099789Sahrens 		}
1100789Sahrens 	}
1101789Sahrens 
1102789Sahrens 	zoid = zp ? zp->z_id : -1ULL;
1103789Sahrens 
1104789Sahrens 	if (zp == NULL) {
1105789Sahrens 		/*
1106789Sahrens 		 * Create a new file object and update the directory
1107789Sahrens 		 * to reference it.
1108789Sahrens 		 */
1109789Sahrens 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
1110789Sahrens 			goto out;
1111789Sahrens 		}
1112789Sahrens 
1113789Sahrens 		/*
1114789Sahrens 		 * We only support the creation of regular files in
1115789Sahrens 		 * extended attribute directories.
1116789Sahrens 		 */
1117789Sahrens 		if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1118789Sahrens 		    (vap->va_type != VREG)) {
1119789Sahrens 			error = EINVAL;
1120789Sahrens 			goto out;
1121789Sahrens 		}
1122789Sahrens 
1123789Sahrens 		tx = dmu_tx_create(os);
1124789Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1125789Sahrens 		dmu_tx_hold_bonus(tx, dzp->z_id);
11261544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1127789Sahrens 		if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
1128789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1129789Sahrens 			    0, SPA_MAXBLOCKSIZE);
1130789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
1131789Sahrens 		if (error) {
1132789Sahrens 			zfs_dirent_unlock(dl);
1133789Sahrens 			if (error == ERESTART &&
1134789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
11352113Sahrens 				dmu_tx_wait(tx);
11362113Sahrens 				dmu_tx_abort(tx);
1137789Sahrens 				goto top;
1138789Sahrens 			}
11392113Sahrens 			dmu_tx_abort(tx);
1140789Sahrens 			ZFS_EXIT(zfsvfs);
1141789Sahrens 			return (error);
1142789Sahrens 		}
1143789Sahrens 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
1144789Sahrens 		ASSERT(zp->z_id == zoid);
1145789Sahrens 		(void) zfs_link_create(dl, zp, tx, ZNEW);
11462638Sperrin 		zfs_log_create(zilog, tx, TX_CREATE, dzp, zp, name);
1147789Sahrens 		dmu_tx_commit(tx);
1148789Sahrens 	} else {
1149789Sahrens 		/*
1150789Sahrens 		 * A directory entry already exists for this name.
1151789Sahrens 		 */
1152789Sahrens 		/*
1153789Sahrens 		 * Can't truncate an existing file if in exclusive mode.
1154789Sahrens 		 */
1155789Sahrens 		if (excl == EXCL) {
1156789Sahrens 			error = EEXIST;
1157789Sahrens 			goto out;
1158789Sahrens 		}
1159789Sahrens 		/*
1160789Sahrens 		 * Can't open a directory for writing.
1161789Sahrens 		 */
1162789Sahrens 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1163789Sahrens 			error = EISDIR;
1164789Sahrens 			goto out;
1165789Sahrens 		}
1166789Sahrens 		/*
1167789Sahrens 		 * Verify requested access to file.
1168789Sahrens 		 */
1169789Sahrens 		if (mode && (error = zfs_zaccess_rwx(zp, mode, cr))) {
1170789Sahrens 			goto out;
1171789Sahrens 		}
1172789Sahrens 
1173789Sahrens 		mutex_enter(&dzp->z_lock);
1174789Sahrens 		dzp->z_seq++;
1175789Sahrens 		mutex_exit(&dzp->z_lock);
1176789Sahrens 
11771878Smaybee 		/*
11781878Smaybee 		 * Truncate regular files if requested.
11791878Smaybee 		 */
11801878Smaybee 		if ((ZTOV(zp)->v_type == VREG) &&
11811878Smaybee 		    (zp->z_phys->zp_size != 0) &&
1182789Sahrens 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
11831878Smaybee 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
11841878Smaybee 			if (error == ERESTART &&
11851878Smaybee 			    zfsvfs->z_assign == TXG_NOWAIT) {
11862113Sahrens 				/* NB: we already did dmu_tx_wait() */
11871878Smaybee 				zfs_dirent_unlock(dl);
11882365Sperrin 				VN_RELE(ZTOV(zp));
11891878Smaybee 				goto top;
1190789Sahrens 			}
1191789Sahrens 		}
1192789Sahrens 	}
1193789Sahrens out:
1194789Sahrens 
1195789Sahrens 	if (dl)
1196789Sahrens 		zfs_dirent_unlock(dl);
1197789Sahrens 
1198789Sahrens 	if (error) {
1199789Sahrens 		if (zp)
1200789Sahrens 			VN_RELE(ZTOV(zp));
1201789Sahrens 	} else {
1202789Sahrens 		*vpp = ZTOV(zp);
1203789Sahrens 		/*
1204789Sahrens 		 * If vnode is for a device return a specfs vnode instead.
1205789Sahrens 		 */
1206789Sahrens 		if (IS_DEVVP(*vpp)) {
1207789Sahrens 			struct vnode *svp;
1208789Sahrens 
1209789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1210789Sahrens 			VN_RELE(*vpp);
1211789Sahrens 			if (svp == NULL) {
1212789Sahrens 				error = ENOSYS;
1213789Sahrens 			}
1214789Sahrens 			*vpp = svp;
1215789Sahrens 		}
1216789Sahrens 	}
1217789Sahrens 
1218789Sahrens 	ZFS_EXIT(zfsvfs);
1219789Sahrens 	return (error);
1220789Sahrens }
1221789Sahrens 
1222789Sahrens /*
1223789Sahrens  * Remove an entry from a directory.
1224789Sahrens  *
1225789Sahrens  *	IN:	dvp	- vnode of directory to remove entry from.
1226789Sahrens  *		name	- name of entry to remove.
1227789Sahrens  *		cr	- credentials of caller.
1228789Sahrens  *
1229789Sahrens  *	RETURN:	0 if success
1230789Sahrens  *		error code if failure
1231789Sahrens  *
1232789Sahrens  * Timestamps:
1233789Sahrens  *	dvp - ctime|mtime
1234789Sahrens  *	 vp - ctime (if nlink > 0)
1235789Sahrens  */
1236789Sahrens static int
1237789Sahrens zfs_remove(vnode_t *dvp, char *name, cred_t *cr)
1238789Sahrens {
1239789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1240789Sahrens 	znode_t		*xzp = NULL;
1241789Sahrens 	vnode_t		*vp;
1242789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1243789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1244789Sahrens 	uint64_t	acl_obj, xattr_obj;
1245789Sahrens 	zfs_dirlock_t	*dl;
1246789Sahrens 	dmu_tx_t	*tx;
1247789Sahrens 	int		may_delete_now, delete_now = FALSE;
1248789Sahrens 	int		reaped;
1249789Sahrens 	int		error;
1250789Sahrens 
1251789Sahrens 	ZFS_ENTER(zfsvfs);
1252789Sahrens 
1253789Sahrens top:
1254789Sahrens 	/*
1255789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1256789Sahrens 	 */
1257789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
1258789Sahrens 		ZFS_EXIT(zfsvfs);
1259789Sahrens 		return (error);
1260789Sahrens 	}
1261789Sahrens 
1262789Sahrens 	vp = ZTOV(zp);
1263789Sahrens 
1264789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1265789Sahrens 		goto out;
1266789Sahrens 	}
1267789Sahrens 
1268789Sahrens 	/*
1269789Sahrens 	 * Need to use rmdir for removing directories.
1270789Sahrens 	 */
1271789Sahrens 	if (vp->v_type == VDIR) {
1272789Sahrens 		error = EPERM;
1273789Sahrens 		goto out;
1274789Sahrens 	}
1275789Sahrens 
1276789Sahrens 	vnevent_remove(vp);
1277789Sahrens 
12781484Sek110237 	dnlc_remove(dvp, name);
12791484Sek110237 
1280789Sahrens 	mutex_enter(&vp->v_lock);
1281789Sahrens 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1282789Sahrens 	mutex_exit(&vp->v_lock);
1283789Sahrens 
1284789Sahrens 	/*
1285789Sahrens 	 * We may delete the znode now, or we may put it on the delete queue;
1286789Sahrens 	 * it depends on whether we're the last link, and on whether there are
1287789Sahrens 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1288789Sahrens 	 * allow for either case.
1289789Sahrens 	 */
1290789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
12911544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1292789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
1293789Sahrens 	if (may_delete_now)
1294789Sahrens 		dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
1295789Sahrens 
1296789Sahrens 	/* are there any extended attributes? */
1297789Sahrens 	if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1298789Sahrens 		/*
1299789Sahrens 		 * XXX - There is a possibility that the delete
1300789Sahrens 		 * of the parent file could succeed, but then we get
1301789Sahrens 		 * an ENOSPC when we try to delete the xattrs...
1302789Sahrens 		 * so we would need to re-try the deletes periodically
1303789Sahrens 		 */
1304789Sahrens 		/* XXX - do we need this if we are deleting? */
1305789Sahrens 		dmu_tx_hold_bonus(tx, xattr_obj);
1306789Sahrens 	}
1307789Sahrens 
1308789Sahrens 	/* are there any additional acls */
1309789Sahrens 	if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1310789Sahrens 	    may_delete_now)
1311789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1312789Sahrens 
1313789Sahrens 	/* charge as an update -- would be nice not to charge at all */
13141544Seschrock 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
1315789Sahrens 
1316789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1317789Sahrens 	if (error) {
1318789Sahrens 		zfs_dirent_unlock(dl);
1319789Sahrens 		VN_RELE(vp);
1320789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
13212113Sahrens 			dmu_tx_wait(tx);
13222113Sahrens 			dmu_tx_abort(tx);
1323789Sahrens 			goto top;
1324789Sahrens 		}
13252113Sahrens 		dmu_tx_abort(tx);
1326789Sahrens 		ZFS_EXIT(zfsvfs);
1327789Sahrens 		return (error);
1328789Sahrens 	}
1329789Sahrens 
1330789Sahrens 	/*
1331789Sahrens 	 * Remove the directory entry.
1332789Sahrens 	 */
1333789Sahrens 	error = zfs_link_destroy(dl, zp, tx, 0, &reaped);
1334789Sahrens 
1335789Sahrens 	if (error) {
1336789Sahrens 		dmu_tx_commit(tx);
1337789Sahrens 		goto out;
1338789Sahrens 	}
1339789Sahrens 
1340789Sahrens 	if (reaped) {
1341789Sahrens 		mutex_enter(&vp->v_lock);
1342789Sahrens 		delete_now = may_delete_now &&
1343789Sahrens 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1344789Sahrens 		    zp->z_phys->zp_xattr == xattr_obj &&
1345789Sahrens 		    zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1346789Sahrens 		mutex_exit(&vp->v_lock);
1347789Sahrens 	}
1348789Sahrens 
1349789Sahrens 	if (delete_now) {
1350789Sahrens 		if (zp->z_phys->zp_xattr) {
1351789Sahrens 			error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1352789Sahrens 			ASSERT3U(error, ==, 0);
1353789Sahrens 			ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1354789Sahrens 			dmu_buf_will_dirty(xzp->z_dbuf, tx);
1355789Sahrens 			mutex_enter(&xzp->z_lock);
1356789Sahrens 			xzp->z_reap = 1;
1357789Sahrens 			xzp->z_phys->zp_links = 0;
1358789Sahrens 			mutex_exit(&xzp->z_lock);
1359789Sahrens 			zfs_dq_add(xzp, tx);
1360789Sahrens 			zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1361789Sahrens 		}
1362789Sahrens 		mutex_enter(&zp->z_lock);
1363789Sahrens 		mutex_enter(&vp->v_lock);
1364789Sahrens 		vp->v_count--;
1365789Sahrens 		ASSERT3U(vp->v_count, ==, 0);
1366789Sahrens 		mutex_exit(&vp->v_lock);
1367789Sahrens 		mutex_exit(&zp->z_lock);
1368789Sahrens 		zfs_znode_delete(zp, tx);
1369789Sahrens 		VFS_RELE(zfsvfs->z_vfs);
1370789Sahrens 	} else if (reaped) {
1371789Sahrens 		zfs_dq_add(zp, tx);
1372789Sahrens 	}
1373789Sahrens 
13742638Sperrin 	zfs_log_remove(zilog, tx, TX_REMOVE, dzp, name);
1375789Sahrens 
1376789Sahrens 	dmu_tx_commit(tx);
1377789Sahrens out:
1378789Sahrens 	zfs_dirent_unlock(dl);
1379789Sahrens 
1380789Sahrens 	if (!delete_now) {
1381789Sahrens 		VN_RELE(vp);
1382789Sahrens 	} else if (xzp) {
1383789Sahrens 		/* this rele delayed to prevent nesting transactions */
1384789Sahrens 		VN_RELE(ZTOV(xzp));
1385789Sahrens 	}
1386789Sahrens 
1387789Sahrens 	ZFS_EXIT(zfsvfs);
1388789Sahrens 	return (error);
1389789Sahrens }
1390789Sahrens 
1391789Sahrens /*
1392789Sahrens  * Create a new directory and insert it into dvp using the name
1393789Sahrens  * provided.  Return a pointer to the inserted directory.
1394789Sahrens  *
1395789Sahrens  *	IN:	dvp	- vnode of directory to add subdir to.
1396789Sahrens  *		dirname	- name of new directory.
1397789Sahrens  *		vap	- attributes of new directory.
1398789Sahrens  *		cr	- credentials of caller.
1399789Sahrens  *
1400789Sahrens  *	OUT:	vpp	- vnode of created directory.
1401789Sahrens  *
1402789Sahrens  *	RETURN:	0 if success
1403789Sahrens  *		error code if failure
1404789Sahrens  *
1405789Sahrens  * Timestamps:
1406789Sahrens  *	dvp - ctime|mtime updated
1407789Sahrens  *	 vp - ctime|mtime|atime updated
1408789Sahrens  */
1409789Sahrens static int
1410789Sahrens zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr)
1411789Sahrens {
1412789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1413789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1414789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1415789Sahrens 	zfs_dirlock_t	*dl;
1416789Sahrens 	uint64_t	zoid = 0;
1417789Sahrens 	dmu_tx_t	*tx;
1418789Sahrens 	int		error;
1419789Sahrens 
1420789Sahrens 	ASSERT(vap->va_type == VDIR);
1421789Sahrens 
1422789Sahrens 	ZFS_ENTER(zfsvfs);
1423789Sahrens 
1424789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1425789Sahrens 		ZFS_EXIT(zfsvfs);
1426789Sahrens 		return (EINVAL);
1427789Sahrens 	}
1428789Sahrens top:
1429789Sahrens 	*vpp = NULL;
1430789Sahrens 
1431789Sahrens 	/*
1432789Sahrens 	 * First make sure the new directory doesn't exist.
1433789Sahrens 	 */
1434789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, ZNEW)) {
1435789Sahrens 		ZFS_EXIT(zfsvfs);
1436789Sahrens 		return (error);
1437789Sahrens 	}
1438789Sahrens 
14391231Smarks 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, cr)) {
14401231Smarks 		zfs_dirent_unlock(dl);
14411231Smarks 		ZFS_EXIT(zfsvfs);
14421231Smarks 		return (error);
14431231Smarks 	}
14441231Smarks 
1445789Sahrens 	/*
1446789Sahrens 	 * Add a new entry to the directory.
1447789Sahrens 	 */
1448789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
14491544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
14501544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1451789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
1452789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1453789Sahrens 		    0, SPA_MAXBLOCKSIZE);
1454789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1455789Sahrens 	if (error) {
1456789Sahrens 		zfs_dirent_unlock(dl);
1457789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
14582113Sahrens 			dmu_tx_wait(tx);
14592113Sahrens 			dmu_tx_abort(tx);
1460789Sahrens 			goto top;
1461789Sahrens 		}
14622113Sahrens 		dmu_tx_abort(tx);
1463789Sahrens 		ZFS_EXIT(zfsvfs);
1464789Sahrens 		return (error);
1465789Sahrens 	}
1466789Sahrens 
1467789Sahrens 	/*
1468789Sahrens 	 * Create new node.
1469789Sahrens 	 */
1470789Sahrens 	zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
1471789Sahrens 
1472789Sahrens 	/*
1473789Sahrens 	 * Now put new name in parent dir.
1474789Sahrens 	 */
1475789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1476789Sahrens 
1477789Sahrens 	*vpp = ZTOV(zp);
1478789Sahrens 
14792638Sperrin 	zfs_log_create(zilog, tx, TX_MKDIR, dzp, zp, dirname);
1480789Sahrens 	dmu_tx_commit(tx);
1481789Sahrens 
1482789Sahrens 	zfs_dirent_unlock(dl);
1483789Sahrens 
1484789Sahrens 	ZFS_EXIT(zfsvfs);
1485789Sahrens 	return (0);
1486789Sahrens }
1487789Sahrens 
1488789Sahrens /*
1489789Sahrens  * Remove a directory subdir entry.  If the current working
1490789Sahrens  * directory is the same as the subdir to be removed, the
1491789Sahrens  * remove will fail.
1492789Sahrens  *
1493789Sahrens  *	IN:	dvp	- vnode of directory to remove from.
1494789Sahrens  *		name	- name of directory to be removed.
1495789Sahrens  *		cwd	- vnode of current working directory.
1496789Sahrens  *		cr	- credentials of caller.
1497789Sahrens  *
1498789Sahrens  *	RETURN:	0 if success
1499789Sahrens  *		error code if failure
1500789Sahrens  *
1501789Sahrens  * Timestamps:
1502789Sahrens  *	dvp - ctime|mtime updated
1503789Sahrens  */
1504789Sahrens static int
1505789Sahrens zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr)
1506789Sahrens {
1507789Sahrens 	znode_t		*dzp = VTOZ(dvp);
1508789Sahrens 	znode_t		*zp;
1509789Sahrens 	vnode_t		*vp;
1510789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1511789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1512789Sahrens 	zfs_dirlock_t	*dl;
1513789Sahrens 	dmu_tx_t	*tx;
1514789Sahrens 	int		error;
1515789Sahrens 
1516789Sahrens 	ZFS_ENTER(zfsvfs);
1517789Sahrens 
1518789Sahrens top:
1519789Sahrens 	zp = NULL;
1520789Sahrens 
1521789Sahrens 	/*
1522789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1523789Sahrens 	 */
1524789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
1525789Sahrens 		ZFS_EXIT(zfsvfs);
1526789Sahrens 		return (error);
1527789Sahrens 	}
1528789Sahrens 
1529789Sahrens 	vp = ZTOV(zp);
1530789Sahrens 
1531789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1532789Sahrens 		goto out;
1533789Sahrens 	}
1534789Sahrens 
1535789Sahrens 	if (vp->v_type != VDIR) {
1536789Sahrens 		error = ENOTDIR;
1537789Sahrens 		goto out;
1538789Sahrens 	}
1539789Sahrens 
1540789Sahrens 	if (vp == cwd) {
1541789Sahrens 		error = EINVAL;
1542789Sahrens 		goto out;
1543789Sahrens 	}
1544789Sahrens 
1545789Sahrens 	vnevent_rmdir(vp);
1546789Sahrens 
1547789Sahrens 	/*
1548789Sahrens 	 * Grab a lock on the parent pointer make sure we play well
1549789Sahrens 	 * with the treewalk and directory rename code.
1550789Sahrens 	 */
1551789Sahrens 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1552789Sahrens 
1553789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
15541544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1555789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
15561544Seschrock 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
1557789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1558789Sahrens 	if (error) {
1559789Sahrens 		rw_exit(&zp->z_parent_lock);
1560789Sahrens 		zfs_dirent_unlock(dl);
1561789Sahrens 		VN_RELE(vp);
1562789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
15632113Sahrens 			dmu_tx_wait(tx);
15642113Sahrens 			dmu_tx_abort(tx);
1565789Sahrens 			goto top;
1566789Sahrens 		}
15672113Sahrens 		dmu_tx_abort(tx);
1568789Sahrens 		ZFS_EXIT(zfsvfs);
1569789Sahrens 		return (error);
1570789Sahrens 	}
1571789Sahrens 
1572789Sahrens 	error = zfs_link_destroy(dl, zp, tx, 0, NULL);
1573789Sahrens 
1574789Sahrens 	if (error == 0)
15752638Sperrin 		zfs_log_remove(zilog, tx, TX_RMDIR, dzp, name);
1576789Sahrens 
1577789Sahrens 	dmu_tx_commit(tx);
1578789Sahrens 
1579789Sahrens 	rw_exit(&zp->z_parent_lock);
1580789Sahrens out:
1581789Sahrens 	zfs_dirent_unlock(dl);
1582789Sahrens 
1583789Sahrens 	VN_RELE(vp);
1584789Sahrens 
1585789Sahrens 	ZFS_EXIT(zfsvfs);
1586789Sahrens 	return (error);
1587789Sahrens }
1588789Sahrens 
1589789Sahrens /*
1590789Sahrens  * Read as many directory entries as will fit into the provided
1591789Sahrens  * buffer from the given directory cursor position (specified in
1592789Sahrens  * the uio structure.
1593789Sahrens  *
1594789Sahrens  *	IN:	vp	- vnode of directory to read.
1595789Sahrens  *		uio	- structure supplying read location, range info,
1596789Sahrens  *			  and return buffer.
1597789Sahrens  *		cr	- credentials of caller.
1598789Sahrens  *
1599789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
1600789Sahrens  *		eofp	- set to true if end-of-file detected.
1601789Sahrens  *
1602789Sahrens  *	RETURN:	0 if success
1603789Sahrens  *		error code if failure
1604789Sahrens  *
1605789Sahrens  * Timestamps:
1606789Sahrens  *	vp - atime updated
1607789Sahrens  *
1608789Sahrens  * Note that the low 4 bits of the cookie returned by zap is always zero.
1609789Sahrens  * This allows us to use the low range for "special" directory entries:
1610789Sahrens  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
1611789Sahrens  * we use the offset 2 for the '.zfs' directory.
1612789Sahrens  */
1613789Sahrens /* ARGSUSED */
1614789Sahrens static int
1615789Sahrens zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp)
1616789Sahrens {
1617789Sahrens 	znode_t		*zp = VTOZ(vp);
1618789Sahrens 	iovec_t		*iovp;
1619789Sahrens 	dirent64_t	*odp;
1620789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1621869Sperrin 	objset_t	*os;
1622789Sahrens 	caddr_t		outbuf;
1623789Sahrens 	size_t		bufsize;
1624789Sahrens 	zap_cursor_t	zc;
1625789Sahrens 	zap_attribute_t	zap;
1626789Sahrens 	uint_t		bytes_wanted;
1627789Sahrens 	ushort_t	this_reclen;
1628789Sahrens 	uint64_t	offset; /* must be unsigned; checks for < 1 */
1629789Sahrens 	off64_t		*next;
1630789Sahrens 	int		local_eof;
1631869Sperrin 	int		outcount;
1632869Sperrin 	int		error;
1633869Sperrin 	uint8_t		prefetch;
1634789Sahrens 
1635789Sahrens 	ZFS_ENTER(zfsvfs);
1636789Sahrens 
1637789Sahrens 	/*
1638789Sahrens 	 * If we are not given an eof variable,
1639789Sahrens 	 * use a local one.
1640789Sahrens 	 */
1641789Sahrens 	if (eofp == NULL)
1642789Sahrens 		eofp = &local_eof;
1643789Sahrens 
1644789Sahrens 	/*
1645789Sahrens 	 * Check for valid iov_len.
1646789Sahrens 	 */
1647789Sahrens 	if (uio->uio_iov->iov_len <= 0) {
1648789Sahrens 		ZFS_EXIT(zfsvfs);
1649789Sahrens 		return (EINVAL);
1650789Sahrens 	}
1651789Sahrens 
1652789Sahrens 	/*
1653789Sahrens 	 * Quit if directory has been removed (posix)
1654789Sahrens 	 */
1655789Sahrens 	if ((*eofp = zp->z_reap) != 0) {
1656789Sahrens 		ZFS_EXIT(zfsvfs);
1657789Sahrens 		return (0);
1658789Sahrens 	}
1659789Sahrens 
1660869Sperrin 	error = 0;
1661869Sperrin 	os = zfsvfs->z_os;
1662869Sperrin 	offset = uio->uio_loffset;
1663869Sperrin 	prefetch = zp->z_zn_prefetch;
1664869Sperrin 
1665789Sahrens 	/*
1666789Sahrens 	 * Initialize the iterator cursor.
1667789Sahrens 	 */
1668789Sahrens 	if (offset <= 3) {
1669789Sahrens 		/*
1670789Sahrens 		 * Start iteration from the beginning of the directory.
1671789Sahrens 		 */
1672869Sperrin 		zap_cursor_init(&zc, os, zp->z_id);
1673789Sahrens 	} else {
1674789Sahrens 		/*
1675789Sahrens 		 * The offset is a serialized cursor.
1676789Sahrens 		 */
1677869Sperrin 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1678789Sahrens 	}
1679789Sahrens 
1680789Sahrens 	/*
1681789Sahrens 	 * Get space to change directory entries into fs independent format.
1682789Sahrens 	 */
1683789Sahrens 	iovp = uio->uio_iov;
1684789Sahrens 	bytes_wanted = iovp->iov_len;
1685789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
1686789Sahrens 		bufsize = bytes_wanted;
1687789Sahrens 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
1688789Sahrens 		odp = (struct dirent64 *)outbuf;
1689789Sahrens 	} else {
1690789Sahrens 		bufsize = bytes_wanted;
1691789Sahrens 		odp = (struct dirent64 *)iovp->iov_base;
1692789Sahrens 	}
1693789Sahrens 
1694789Sahrens 	/*
1695789Sahrens 	 * Transform to file-system independent format
1696789Sahrens 	 */
1697789Sahrens 	outcount = 0;
1698789Sahrens 	while (outcount < bytes_wanted) {
1699789Sahrens 		/*
1700789Sahrens 		 * Special case `.', `..', and `.zfs'.
1701789Sahrens 		 */
1702789Sahrens 		if (offset == 0) {
1703789Sahrens 			(void) strcpy(zap.za_name, ".");
1704789Sahrens 			zap.za_first_integer = zp->z_id;
1705789Sahrens 			this_reclen = DIRENT64_RECLEN(1);
1706789Sahrens 		} else if (offset == 1) {
1707789Sahrens 			(void) strcpy(zap.za_name, "..");
1708789Sahrens 			zap.za_first_integer = zp->z_phys->zp_parent;
1709789Sahrens 			this_reclen = DIRENT64_RECLEN(2);
1710789Sahrens 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
1711789Sahrens 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
1712789Sahrens 			zap.za_first_integer = ZFSCTL_INO_ROOT;
1713789Sahrens 			this_reclen =
1714789Sahrens 			    DIRENT64_RECLEN(sizeof (ZFS_CTLDIR_NAME) - 1);
1715789Sahrens 		} else {
1716789Sahrens 			/*
1717789Sahrens 			 * Grab next entry.
1718789Sahrens 			 */
1719789Sahrens 			if (error = zap_cursor_retrieve(&zc, &zap)) {
1720789Sahrens 				if ((*eofp = (error == ENOENT)) != 0)
1721789Sahrens 					break;
1722789Sahrens 				else
1723789Sahrens 					goto update;
1724789Sahrens 			}
1725789Sahrens 
1726789Sahrens 			if (zap.za_integer_length != 8 ||
1727789Sahrens 			    zap.za_num_integers != 1) {
1728789Sahrens 				cmn_err(CE_WARN, "zap_readdir: bad directory "
1729789Sahrens 				    "entry, obj = %lld, offset = %lld\n",
1730789Sahrens 				    (u_longlong_t)zp->z_id,
1731789Sahrens 				    (u_longlong_t)offset);
1732789Sahrens 				error = ENXIO;
1733789Sahrens 				goto update;
1734789Sahrens 			}
1735789Sahrens 			this_reclen = DIRENT64_RECLEN(strlen(zap.za_name));
1736789Sahrens 		}
1737789Sahrens 
1738789Sahrens 		/*
1739789Sahrens 		 * Will this entry fit in the buffer?
1740789Sahrens 		 */
1741789Sahrens 		if (outcount + this_reclen > bufsize) {
1742789Sahrens 			/*
1743789Sahrens 			 * Did we manage to fit anything in the buffer?
1744789Sahrens 			 */
1745789Sahrens 			if (!outcount) {
1746789Sahrens 				error = EINVAL;
1747789Sahrens 				goto update;
1748789Sahrens 			}
1749789Sahrens 			break;
1750789Sahrens 		}
1751789Sahrens 		/*
1752789Sahrens 		 * Add this entry:
1753789Sahrens 		 */
1754789Sahrens 		odp->d_ino = (ino64_t)zap.za_first_integer;
1755789Sahrens 		odp->d_reclen = (ushort_t)this_reclen;
1756789Sahrens 		/* NOTE: d_off is the offset for the *next* entry */
1757789Sahrens 		next = &(odp->d_off);
1758789Sahrens 		(void) strncpy(odp->d_name, zap.za_name,
1759789Sahrens 		    DIRENT64_NAMELEN(this_reclen));
1760789Sahrens 		outcount += this_reclen;
1761789Sahrens 		odp = (dirent64_t *)((intptr_t)odp + this_reclen);
1762789Sahrens 
1763789Sahrens 		ASSERT(outcount <= bufsize);
1764789Sahrens 
1765789Sahrens 		/* Prefetch znode */
1766869Sperrin 		if (prefetch)
1767869Sperrin 			dmu_prefetch(os, zap.za_first_integer, 0, 0);
1768789Sahrens 
1769789Sahrens 		/*
1770789Sahrens 		 * Move to the next entry, fill in the previous offset.
1771789Sahrens 		 */
1772789Sahrens 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
1773789Sahrens 			zap_cursor_advance(&zc);
1774789Sahrens 			offset = zap_cursor_serialize(&zc);
1775789Sahrens 		} else {
1776789Sahrens 			offset += 1;
1777789Sahrens 		}
1778789Sahrens 		*next = offset;
1779789Sahrens 	}
1780869Sperrin 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
1781789Sahrens 
1782789Sahrens 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
1783789Sahrens 		iovp->iov_base += outcount;
1784789Sahrens 		iovp->iov_len -= outcount;
1785789Sahrens 		uio->uio_resid -= outcount;
1786789Sahrens 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
1787789Sahrens 		/*
1788789Sahrens 		 * Reset the pointer.
1789789Sahrens 		 */
1790789Sahrens 		offset = uio->uio_loffset;
1791789Sahrens 	}
1792789Sahrens 
1793789Sahrens update:
1794885Sahrens 	zap_cursor_fini(&zc);
1795789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1796789Sahrens 		kmem_free(outbuf, bufsize);
1797789Sahrens 
1798789Sahrens 	if (error == ENOENT)
1799789Sahrens 		error = 0;
1800789Sahrens 
1801789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
1802789Sahrens 
1803789Sahrens 	uio->uio_loffset = offset;
1804789Sahrens 	ZFS_EXIT(zfsvfs);
1805789Sahrens 	return (error);
1806789Sahrens }
1807789Sahrens 
1808789Sahrens static int
1809789Sahrens zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr)
1810789Sahrens {
1811789Sahrens 	znode_t	*zp = VTOZ(vp);
1812789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1813789Sahrens 
18141773Seschrock 	/*
18151773Seschrock 	 * Regardless of whether this is required for standards conformance,
18161773Seschrock 	 * this is the logical behavior when fsync() is called on a file with
18171773Seschrock 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
18181773Seschrock 	 * going to be pushed out as part of the zil_commit().
18191773Seschrock 	 */
18201773Seschrock 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
18211773Seschrock 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
18221773Seschrock 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr);
18231773Seschrock 
1824789Sahrens 	ZFS_ENTER(zfsvfs);
18252638Sperrin 	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
1826789Sahrens 	ZFS_EXIT(zfsvfs);
1827789Sahrens 	return (0);
1828789Sahrens }
1829789Sahrens 
1830789Sahrens /*
1831789Sahrens  * Get the requested file attributes and place them in the provided
1832789Sahrens  * vattr structure.
1833789Sahrens  *
1834789Sahrens  *	IN:	vp	- vnode of file.
1835789Sahrens  *		vap	- va_mask identifies requested attributes.
1836789Sahrens  *		flags	- [UNUSED]
1837789Sahrens  *		cr	- credentials of caller.
1838789Sahrens  *
1839789Sahrens  *	OUT:	vap	- attribute values.
1840789Sahrens  *
1841789Sahrens  *	RETURN:	0 (always succeeds)
1842789Sahrens  */
1843789Sahrens /* ARGSUSED */
1844789Sahrens static int
1845789Sahrens zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
1846789Sahrens {
1847789Sahrens 	znode_t *zp = VTOZ(vp);
1848789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1849789Sahrens 	znode_phys_t *pzp = zp->z_phys;
1850789Sahrens 	int	error;
1851789Sahrens 
1852789Sahrens 	ZFS_ENTER(zfsvfs);
1853789Sahrens 
1854789Sahrens 	/*
1855789Sahrens 	 * Return all attributes.  It's cheaper to provide the answer
1856789Sahrens 	 * than to determine whether we were asked the question.
1857789Sahrens 	 */
1858789Sahrens 	mutex_enter(&zp->z_lock);
1859789Sahrens 
1860789Sahrens 	vap->va_type = vp->v_type;
1861789Sahrens 	vap->va_mode = pzp->zp_mode & MODEMASK;
1862789Sahrens 	vap->va_uid = zp->z_phys->zp_uid;
1863789Sahrens 	vap->va_gid = zp->z_phys->zp_gid;
1864789Sahrens 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
1865789Sahrens 	vap->va_nodeid = zp->z_id;
1866789Sahrens 	vap->va_nlink = MIN(pzp->zp_links, UINT32_MAX);	/* nlink_t limit! */
1867789Sahrens 	vap->va_size = pzp->zp_size;
18681816Smarks 	vap->va_rdev = vp->v_rdev;
1869789Sahrens 	vap->va_seq = zp->z_seq;
1870789Sahrens 
1871789Sahrens 	ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
1872789Sahrens 	ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
1873789Sahrens 	ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
1874789Sahrens 
1875789Sahrens 	/*
1876905Smarks 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
1877905Smarks 	 * Also, if we are the owner don't bother, since owner should
1878905Smarks 	 * always be allowed to read basic attributes of file.
1879789Sahrens 	 */
1880905Smarks 	if (!(zp->z_phys->zp_flags & ZFS_ACL_TRIVIAL) &&
1881905Smarks 	    (zp->z_phys->zp_uid != crgetuid(cr))) {
1882905Smarks 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, cr)) {
1883789Sahrens 			mutex_exit(&zp->z_lock);
1884789Sahrens 			ZFS_EXIT(zfsvfs);
1885789Sahrens 			return (error);
1886789Sahrens 		}
1887789Sahrens 	}
1888789Sahrens 
1889789Sahrens 	mutex_exit(&zp->z_lock);
1890789Sahrens 
1891789Sahrens 	dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks);
1892789Sahrens 
1893789Sahrens 	if (zp->z_blksz == 0) {
1894789Sahrens 		/*
1895789Sahrens 		 * Block size hasn't been set; suggest maximal I/O transfers.
1896789Sahrens 		 */
1897789Sahrens 		vap->va_blksize = zfsvfs->z_max_blksz;
1898789Sahrens 	}
1899789Sahrens 
1900789Sahrens 	ZFS_EXIT(zfsvfs);
1901789Sahrens 	return (0);
1902789Sahrens }
1903789Sahrens 
1904789Sahrens /*
1905789Sahrens  * Set the file attributes to the values contained in the
1906789Sahrens  * vattr structure.
1907789Sahrens  *
1908789Sahrens  *	IN:	vp	- vnode of file to be modified.
1909789Sahrens  *		vap	- new attribute values.
1910789Sahrens  *		flags	- ATTR_UTIME set if non-default time values provided.
1911789Sahrens  *		cr	- credentials of caller.
1912789Sahrens  *
1913789Sahrens  *	RETURN:	0 if success
1914789Sahrens  *		error code if failure
1915789Sahrens  *
1916789Sahrens  * Timestamps:
1917789Sahrens  *	vp - ctime updated, mtime updated if size changed.
1918789Sahrens  */
1919789Sahrens /* ARGSUSED */
1920789Sahrens static int
1921789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1922789Sahrens 	caller_context_t *ct)
1923789Sahrens {
1924789Sahrens 	struct znode	*zp = VTOZ(vp);
1925789Sahrens 	znode_phys_t	*pzp = zp->z_phys;
1926789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1927789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1928789Sahrens 	dmu_tx_t	*tx;
19291878Smaybee 	vattr_t		oldva;
1930789Sahrens 	uint_t		mask = vap->va_mask;
19311878Smaybee 	uint_t		saved_mask;
19321115Smarks 	int		trim_mask = FALSE;
1933789Sahrens 	uint64_t	new_mode;
19341231Smarks 	znode_t		*attrzp;
1935789Sahrens 	int		need_policy = FALSE;
1936789Sahrens 	int		err;
1937789Sahrens 
1938789Sahrens 	if (mask == 0)
1939789Sahrens 		return (0);
1940789Sahrens 
1941789Sahrens 	if (mask & AT_NOSET)
1942789Sahrens 		return (EINVAL);
1943789Sahrens 
1944789Sahrens 	if (mask & AT_SIZE && vp->v_type == VDIR)
1945789Sahrens 		return (EISDIR);
1946789Sahrens 
19471394Smarks 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO)
19481308Smarks 		return (EINVAL);
19491308Smarks 
1950789Sahrens 	ZFS_ENTER(zfsvfs);
1951789Sahrens 
1952789Sahrens top:
19531231Smarks 	attrzp = NULL;
1954789Sahrens 
1955789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
1956789Sahrens 		ZFS_EXIT(zfsvfs);
1957789Sahrens 		return (EROFS);
1958789Sahrens 	}
1959789Sahrens 
1960789Sahrens 	/*
1961789Sahrens 	 * First validate permissions
1962789Sahrens 	 */
1963789Sahrens 
1964789Sahrens 	if (mask & AT_SIZE) {
1965789Sahrens 		err = zfs_zaccess(zp, ACE_WRITE_DATA, cr);
1966789Sahrens 		if (err) {
1967789Sahrens 			ZFS_EXIT(zfsvfs);
1968789Sahrens 			return (err);
1969789Sahrens 		}
19701878Smaybee 		/*
19711878Smaybee 		 * XXX - Note, we are not providing any open
19721878Smaybee 		 * mode flags here (like FNDELAY), so we may
19731878Smaybee 		 * block if there are locks present... this
19741878Smaybee 		 * should be addressed in openat().
19751878Smaybee 		 */
19761878Smaybee 		do {
19771878Smaybee 			err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
19782113Sahrens 			/* NB: we already did dmu_tx_wait() if necessary */
19791878Smaybee 		} while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
19801878Smaybee 		if (err) {
19811878Smaybee 			ZFS_EXIT(zfsvfs);
19821878Smaybee 			return (err);
19831878Smaybee 		}
1984789Sahrens 	}
1985789Sahrens 
1986789Sahrens 	if (mask & (AT_ATIME|AT_MTIME))
1987789Sahrens 		need_policy = zfs_zaccess_v4_perm(zp, ACE_WRITE_ATTRIBUTES, cr);
1988789Sahrens 
1989789Sahrens 	if (mask & (AT_UID|AT_GID)) {
1990789Sahrens 		int	idmask = (mask & (AT_UID|AT_GID));
1991789Sahrens 		int	take_owner;
1992789Sahrens 		int	take_group;
1993789Sahrens 
1994789Sahrens 		/*
1995913Smarks 		 * NOTE: even if a new mode is being set,
1996913Smarks 		 * we may clear S_ISUID/S_ISGID bits.
1997913Smarks 		 */
1998913Smarks 
1999913Smarks 		if (!(mask & AT_MODE))
2000913Smarks 			vap->va_mode = pzp->zp_mode;
2001913Smarks 
2002913Smarks 		/*
2003789Sahrens 		 * Take ownership or chgrp to group we are a member of
2004789Sahrens 		 */
2005789Sahrens 
2006789Sahrens 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2007789Sahrens 		take_group = (mask & AT_GID) && groupmember(vap->va_gid, cr);
2008789Sahrens 
2009789Sahrens 		/*
2010789Sahrens 		 * If both AT_UID and AT_GID are set then take_owner and
2011789Sahrens 		 * take_group must both be set in order to allow taking
2012789Sahrens 		 * ownership.
2013789Sahrens 		 *
2014789Sahrens 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2015789Sahrens 		 *
2016789Sahrens 		 */
2017789Sahrens 
2018789Sahrens 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2019789Sahrens 		    ((idmask == AT_UID) && take_owner) ||
2020789Sahrens 		    ((idmask == AT_GID) && take_group)) {
2021789Sahrens 			if (zfs_zaccess_v4_perm(zp, ACE_WRITE_OWNER, cr) == 0) {
2022789Sahrens 				/*
2023789Sahrens 				 * Remove setuid/setgid for non-privileged users
2024789Sahrens 				 */
20251115Smarks 				secpolicy_setid_clear(vap, cr);
20261115Smarks 				trim_mask = TRUE;
20271115Smarks 				saved_mask = vap->va_mask;
2028789Sahrens 			} else {
2029789Sahrens 				need_policy =  TRUE;
2030789Sahrens 			}
2031789Sahrens 		} else {
2032789Sahrens 			need_policy =  TRUE;
2033789Sahrens 		}
2034789Sahrens 	}
2035789Sahrens 
2036789Sahrens 	if (mask & AT_MODE)
2037789Sahrens 		need_policy = TRUE;
2038789Sahrens 
2039789Sahrens 	if (need_policy) {
2040789Sahrens 		mutex_enter(&zp->z_lock);
2041789Sahrens 		oldva.va_mode = pzp->zp_mode;
2042789Sahrens 		oldva.va_uid = zp->z_phys->zp_uid;
2043789Sahrens 		oldva.va_gid = zp->z_phys->zp_gid;
2044789Sahrens 		mutex_exit(&zp->z_lock);
20451115Smarks 
20461115Smarks 		/*
20471115Smarks 		 * If trim_mask is set then take ownership
20481115Smarks 		 * has been granted.  In that case remove
20491115Smarks 		 * UID|GID from mask so that
20501115Smarks 		 * secpolicy_vnode_setattr() doesn't revoke it.
20511115Smarks 		 */
20521115Smarks 		if (trim_mask)
20531115Smarks 			vap->va_mask &= ~(AT_UID|AT_GID);
20541115Smarks 
2055789Sahrens 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2056789Sahrens 		    (int (*)(void *, int, cred_t *))zfs_zaccess_rwx, zp);
2057789Sahrens 		if (err) {
2058789Sahrens 			ZFS_EXIT(zfsvfs);
2059789Sahrens 			return (err);
2060789Sahrens 		}
20611115Smarks 
20621115Smarks 		if (trim_mask)
20631115Smarks 			vap->va_mask |= (saved_mask & (AT_UID|AT_GID));
2064789Sahrens 	}
2065789Sahrens 
2066789Sahrens 	/*
2067789Sahrens 	 * secpolicy_vnode_setattr, or take ownership may have
2068789Sahrens 	 * changed va_mask
2069789Sahrens 	 */
2070789Sahrens 	mask = vap->va_mask;
2071789Sahrens 
2072789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2073789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
2074789Sahrens 
2075789Sahrens 	if (mask & AT_MODE) {
20761576Smarks 		uint64_t pmode = pzp->zp_mode;
20771576Smarks 
20781576Smarks 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2079789Sahrens 
2080789Sahrens 		if (zp->z_phys->zp_acl.z_acl_extern_obj)
2081789Sahrens 			dmu_tx_hold_write(tx,
2082789Sahrens 			    pzp->zp_acl.z_acl_extern_obj, 0, SPA_MAXBLOCKSIZE);
2083789Sahrens 		else
2084789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2085789Sahrens 			    0, ZFS_ACL_SIZE(MAX_ACL_SIZE));
2086789Sahrens 	}
2087789Sahrens 
20881231Smarks 	if ((mask & (AT_UID | AT_GID)) && zp->z_phys->zp_xattr != 0) {
20891231Smarks 		err = zfs_zget(zp->z_zfsvfs, zp->z_phys->zp_xattr, &attrzp);
20901231Smarks 		if (err) {
20911231Smarks 			dmu_tx_abort(tx);
20921231Smarks 			ZFS_EXIT(zfsvfs);
20931231Smarks 			return (err);
20941231Smarks 		}
20951231Smarks 		dmu_tx_hold_bonus(tx, attrzp->z_id);
20961231Smarks 	}
20971231Smarks 
2098789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2099789Sahrens 	if (err) {
21001231Smarks 		if (attrzp)
21011231Smarks 			VN_RELE(ZTOV(attrzp));
2102789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
21032113Sahrens 			dmu_tx_wait(tx);
21042113Sahrens 			dmu_tx_abort(tx);
2105789Sahrens 			goto top;
2106789Sahrens 		}
21072113Sahrens 		dmu_tx_abort(tx);
2108789Sahrens 		ZFS_EXIT(zfsvfs);
2109789Sahrens 		return (err);
2110789Sahrens 	}
2111789Sahrens 
2112789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
2113789Sahrens 
2114789Sahrens 	/*
2115789Sahrens 	 * Set each attribute requested.
2116789Sahrens 	 * We group settings according to the locks they need to acquire.
2117789Sahrens 	 *
2118789Sahrens 	 * Note: you cannot set ctime directly, although it will be
2119789Sahrens 	 * updated as a side-effect of calling this function.
2120789Sahrens 	 */
2121789Sahrens 
2122789Sahrens 	mutex_enter(&zp->z_lock);
2123789Sahrens 
2124789Sahrens 	if (mask & AT_MODE) {
2125789Sahrens 		err = zfs_acl_chmod_setattr(zp, new_mode, tx);
2126789Sahrens 		ASSERT3U(err, ==, 0);
2127789Sahrens 	}
2128789Sahrens 
21291231Smarks 	if (attrzp)
21301231Smarks 		mutex_enter(&attrzp->z_lock);
21311231Smarks 
21321231Smarks 	if (mask & AT_UID) {
2133789Sahrens 		zp->z_phys->zp_uid = (uint64_t)vap->va_uid;
21341231Smarks 		if (attrzp) {
21351231Smarks 			attrzp->z_phys->zp_uid = (uint64_t)vap->va_uid;
21361231Smarks 		}
21371231Smarks 	}
21381231Smarks 
21391231Smarks 	if (mask & AT_GID) {
2140789Sahrens 		zp->z_phys->zp_gid = (uint64_t)vap->va_gid;
21411231Smarks 		if (attrzp)
21421231Smarks 			attrzp->z_phys->zp_gid = (uint64_t)vap->va_gid;
21431231Smarks 	}
21441231Smarks 
21451231Smarks 	if (attrzp)
21461231Smarks 		mutex_exit(&attrzp->z_lock);
2147789Sahrens 
2148789Sahrens 	if (mask & AT_ATIME)
2149789Sahrens 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
2150789Sahrens 
2151789Sahrens 	if (mask & AT_MTIME)
2152789Sahrens 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
2153789Sahrens 
21541878Smaybee 	if (mask & AT_SIZE)
2155789Sahrens 		zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
21561878Smaybee 	else if (mask != 0)
2157789Sahrens 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
2158789Sahrens 
21591878Smaybee 	if (mask != 0)
21602638Sperrin 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask);
2161789Sahrens 
2162789Sahrens 	mutex_exit(&zp->z_lock);
2163789Sahrens 
21641231Smarks 	if (attrzp)
21651231Smarks 		VN_RELE(ZTOV(attrzp));
21661231Smarks 
2167789Sahrens 	dmu_tx_commit(tx);
2168789Sahrens 
2169789Sahrens 	ZFS_EXIT(zfsvfs);
2170789Sahrens 	return (err);
2171789Sahrens }
2172789Sahrens 
2173789Sahrens /*
2174789Sahrens  * Search back through the directory tree, using the ".." entries.
2175789Sahrens  * Lock each directory in the chain to prevent concurrent renames.
2176789Sahrens  * Fail any attempt to move a directory into one of its own descendants.
2177789Sahrens  * XXX - z_parent_lock can overlap with map or grow locks
2178789Sahrens  */
2179789Sahrens typedef struct zfs_zlock {
2180789Sahrens 	krwlock_t	*zl_rwlock;	/* lock we acquired */
2181789Sahrens 	znode_t		*zl_znode;	/* znode we held */
2182789Sahrens 	struct zfs_zlock *zl_next;	/* next in list */
2183789Sahrens } zfs_zlock_t;
2184789Sahrens 
2185789Sahrens static int
2186789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
2187789Sahrens {
2188789Sahrens 	zfs_zlock_t	*zl;
2189789Sahrens 	znode_t 	*zp = tdzp;
2190789Sahrens 	uint64_t	rootid = zp->z_zfsvfs->z_root;
2191789Sahrens 	uint64_t	*oidp = &zp->z_id;
2192789Sahrens 	krwlock_t	*rwlp = &szp->z_parent_lock;
2193789Sahrens 	krw_t		rw = RW_WRITER;
2194789Sahrens 
2195789Sahrens 	/*
2196789Sahrens 	 * First pass write-locks szp and compares to zp->z_id.
2197789Sahrens 	 * Later passes read-lock zp and compare to zp->z_parent.
2198789Sahrens 	 */
2199789Sahrens 	do {
2200789Sahrens 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
2201789Sahrens 		zl->zl_rwlock = rwlp;
2202789Sahrens 		zl->zl_znode = NULL;
2203789Sahrens 		zl->zl_next = *zlpp;
2204789Sahrens 		*zlpp = zl;
2205789Sahrens 
2206789Sahrens 		rw_enter(rwlp, rw);
2207789Sahrens 
2208789Sahrens 		if (*oidp == szp->z_id)		/* We're a descendant of szp */
2209789Sahrens 			return (EINVAL);
2210789Sahrens 
2211789Sahrens 		if (*oidp == rootid)		/* We've hit the top */
2212789Sahrens 			return (0);
2213789Sahrens 
2214789Sahrens 		if (rw == RW_READER) {		/* i.e. not the first pass */
2215789Sahrens 			int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
2216789Sahrens 			if (error)
2217789Sahrens 				return (error);
2218789Sahrens 			zl->zl_znode = zp;
2219789Sahrens 		}
2220789Sahrens 		oidp = &zp->z_phys->zp_parent;
2221789Sahrens 		rwlp = &zp->z_parent_lock;
2222789Sahrens 		rw = RW_READER;
2223789Sahrens 
2224789Sahrens 	} while (zp->z_id != sdzp->z_id);
2225789Sahrens 
2226789Sahrens 	return (0);
2227789Sahrens }
2228789Sahrens 
2229789Sahrens /*
2230789Sahrens  * Drop locks and release vnodes that were held by zfs_rename_lock().
2231789Sahrens  */
2232789Sahrens static void
2233789Sahrens zfs_rename_unlock(zfs_zlock_t **zlpp)
2234789Sahrens {
2235789Sahrens 	zfs_zlock_t *zl;
2236789Sahrens 
2237789Sahrens 	while ((zl = *zlpp) != NULL) {
2238789Sahrens 		if (zl->zl_znode != NULL)
2239789Sahrens 			VN_RELE(ZTOV(zl->zl_znode));
2240789Sahrens 		rw_exit(zl->zl_rwlock);
2241789Sahrens 		*zlpp = zl->zl_next;
2242789Sahrens 		kmem_free(zl, sizeof (*zl));
2243789Sahrens 	}
2244789Sahrens }
2245789Sahrens 
2246789Sahrens /*
2247789Sahrens  * Move an entry from the provided source directory to the target
2248789Sahrens  * directory.  Change the entry name as indicated.
2249789Sahrens  *
2250789Sahrens  *	IN:	sdvp	- Source directory containing the "old entry".
2251789Sahrens  *		snm	- Old entry name.
2252789Sahrens  *		tdvp	- Target directory to contain the "new entry".
2253789Sahrens  *		tnm	- New entry name.
2254789Sahrens  *		cr	- credentials of caller.
2255789Sahrens  *
2256789Sahrens  *	RETURN:	0 if success
2257789Sahrens  *		error code if failure
2258789Sahrens  *
2259789Sahrens  * Timestamps:
2260789Sahrens  *	sdvp,tdvp - ctime|mtime updated
2261789Sahrens  */
2262789Sahrens static int
2263789Sahrens zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr)
2264789Sahrens {
2265789Sahrens 	znode_t		*tdzp, *szp, *tzp;
2266789Sahrens 	znode_t		*sdzp = VTOZ(sdvp);
2267789Sahrens 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
2268789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2269789Sahrens 	vnode_t		*realvp;
2270789Sahrens 	zfs_dirlock_t	*sdl, *tdl;
2271789Sahrens 	dmu_tx_t	*tx;
2272789Sahrens 	zfs_zlock_t	*zl;
2273789Sahrens 	int		cmp, serr, terr, error;
2274789Sahrens 
2275789Sahrens 	ZFS_ENTER(zfsvfs);
2276789Sahrens 
2277789Sahrens 	/*
2278789Sahrens 	 * Make sure we have the real vp for the target directory.
2279789Sahrens 	 */
2280789Sahrens 	if (VOP_REALVP(tdvp, &realvp) == 0)
2281789Sahrens 		tdvp = realvp;
2282789Sahrens 
2283789Sahrens 	if (tdvp->v_vfsp != sdvp->v_vfsp) {
2284789Sahrens 		ZFS_EXIT(zfsvfs);
2285789Sahrens 		return (EXDEV);
2286789Sahrens 	}
2287789Sahrens 
2288789Sahrens 	tdzp = VTOZ(tdvp);
2289789Sahrens top:
2290789Sahrens 	szp = NULL;
2291789Sahrens 	tzp = NULL;
2292789Sahrens 	zl = NULL;
2293789Sahrens 
2294789Sahrens 	/*
2295789Sahrens 	 * This is to prevent the creation of links into attribute space
2296789Sahrens 	 * by renaming a linked file into/outof an attribute directory.
2297789Sahrens 	 * See the comment in zfs_link() for why this is considered bad.
2298789Sahrens 	 */
2299789Sahrens 	if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
2300789Sahrens 	    (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
2301789Sahrens 		ZFS_EXIT(zfsvfs);
2302789Sahrens 		return (EINVAL);
2303789Sahrens 	}
2304789Sahrens 
2305789Sahrens 	/*
2306789Sahrens 	 * Lock source and target directory entries.  To prevent deadlock,
2307789Sahrens 	 * a lock ordering must be defined.  We lock the directory with
2308789Sahrens 	 * the smallest object id first, or if it's a tie, the one with
2309789Sahrens 	 * the lexically first name.
2310789Sahrens 	 */
2311789Sahrens 	if (sdzp->z_id < tdzp->z_id) {
2312789Sahrens 		cmp = -1;
2313789Sahrens 	} else if (sdzp->z_id > tdzp->z_id) {
2314789Sahrens 		cmp = 1;
2315789Sahrens 	} else {
2316789Sahrens 		cmp = strcmp(snm, tnm);
2317789Sahrens 		if (cmp == 0) {
2318789Sahrens 			/*
2319789Sahrens 			 * POSIX: "If the old argument and the new argument
2320789Sahrens 			 * both refer to links to the same existing file,
2321789Sahrens 			 * the rename() function shall return successfully
2322789Sahrens 			 * and perform no other action."
2323789Sahrens 			 */
2324789Sahrens 			ZFS_EXIT(zfsvfs);
2325789Sahrens 			return (0);
2326789Sahrens 		}
2327789Sahrens 	}
2328789Sahrens 	if (cmp < 0) {
2329789Sahrens 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
2330789Sahrens 		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
2331789Sahrens 	} else {
2332789Sahrens 		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
2333789Sahrens 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
2334789Sahrens 	}
2335789Sahrens 
2336789Sahrens 	if (serr) {
2337789Sahrens 		/*
2338789Sahrens 		 * Source entry invalid or not there.
2339789Sahrens 		 */
2340789Sahrens 		if (!terr) {
2341789Sahrens 			zfs_dirent_unlock(tdl);
2342789Sahrens 			if (tzp)
2343789Sahrens 				VN_RELE(ZTOV(tzp));
2344789Sahrens 		}
2345789Sahrens 		if (strcmp(snm, "..") == 0)
2346789Sahrens 			serr = EINVAL;
2347789Sahrens 		ZFS_EXIT(zfsvfs);
2348789Sahrens 		return (serr);
2349789Sahrens 	}
2350789Sahrens 	if (terr) {
2351789Sahrens 		zfs_dirent_unlock(sdl);
2352789Sahrens 		VN_RELE(ZTOV(szp));
2353789Sahrens 		if (strcmp(tnm, "..") == 0)
2354789Sahrens 			terr = EINVAL;
2355789Sahrens 		ZFS_EXIT(zfsvfs);
2356789Sahrens 		return (terr);
2357789Sahrens 	}
2358789Sahrens 
2359789Sahrens 	/*
2360789Sahrens 	 * Must have write access at the source to remove the old entry
2361789Sahrens 	 * and write access at the target to create the new entry.
2362789Sahrens 	 * Note that if target and source are the same, this can be
2363789Sahrens 	 * done in a single check.
2364789Sahrens 	 */
2365789Sahrens 
2366789Sahrens 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
2367789Sahrens 		goto out;
2368789Sahrens 
2369789Sahrens 	if (ZTOV(szp)->v_type == VDIR) {
2370789Sahrens 		/*
2371789Sahrens 		 * Check to make sure rename is valid.
2372789Sahrens 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
2373789Sahrens 		 */
2374789Sahrens 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
2375789Sahrens 			goto out;
2376789Sahrens 	}
2377789Sahrens 
2378789Sahrens 	/*
2379789Sahrens 	 * Does target exist?
2380789Sahrens 	 */
2381789Sahrens 	if (tzp) {
2382789Sahrens 		/*
2383789Sahrens 		 * Source and target must be the same type.
2384789Sahrens 		 */
2385789Sahrens 		if (ZTOV(szp)->v_type == VDIR) {
2386789Sahrens 			if (ZTOV(tzp)->v_type != VDIR) {
2387789Sahrens 				error = ENOTDIR;
2388789Sahrens 				goto out;
2389789Sahrens 			}
2390789Sahrens 		} else {
2391789Sahrens 			if (ZTOV(tzp)->v_type == VDIR) {
2392789Sahrens 				error = EISDIR;
2393789Sahrens 				goto out;
2394789Sahrens 			}
2395789Sahrens 		}
2396789Sahrens 		/*
2397789Sahrens 		 * POSIX dictates that when the source and target
2398789Sahrens 		 * entries refer to the same file object, rename
2399789Sahrens 		 * must do nothing and exit without error.
2400789Sahrens 		 */
2401789Sahrens 		if (szp->z_id == tzp->z_id) {
2402789Sahrens 			error = 0;
2403789Sahrens 			goto out;
2404789Sahrens 		}
2405789Sahrens 	}
2406789Sahrens 
2407789Sahrens 	vnevent_rename_src(ZTOV(szp));
2408789Sahrens 	if (tzp)
2409789Sahrens 		vnevent_rename_dest(ZTOV(tzp));
2410789Sahrens 
2411789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2412789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);	/* nlink changes */
2413789Sahrens 	dmu_tx_hold_bonus(tx, sdzp->z_id);	/* nlink changes */
24141544Seschrock 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
24151544Seschrock 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
24161544Seschrock 	if (sdzp != tdzp)
2417789Sahrens 		dmu_tx_hold_bonus(tx, tdzp->z_id);	/* nlink changes */
24181544Seschrock 	if (tzp)
24191544Seschrock 		dmu_tx_hold_bonus(tx, tzp->z_id);	/* parent changes */
24201544Seschrock 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
2421789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2422789Sahrens 	if (error) {
2423789Sahrens 		if (zl != NULL)
2424789Sahrens 			zfs_rename_unlock(&zl);
2425789Sahrens 		zfs_dirent_unlock(sdl);
2426789Sahrens 		zfs_dirent_unlock(tdl);
2427789Sahrens 		VN_RELE(ZTOV(szp));
2428789Sahrens 		if (tzp)
2429789Sahrens 			VN_RELE(ZTOV(tzp));
2430789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
24312113Sahrens 			dmu_tx_wait(tx);
24322113Sahrens 			dmu_tx_abort(tx);
2433789Sahrens 			goto top;
2434789Sahrens 		}
24352113Sahrens 		dmu_tx_abort(tx);
2436789Sahrens 		ZFS_EXIT(zfsvfs);
2437789Sahrens 		return (error);
2438789Sahrens 	}
2439789Sahrens 
2440789Sahrens 	if (tzp)	/* Attempt to remove the existing target */
2441789Sahrens 		error = zfs_link_destroy(tdl, tzp, tx, 0, NULL);
2442789Sahrens 
2443789Sahrens 	if (error == 0) {
2444789Sahrens 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
2445789Sahrens 		if (error == 0) {
2446789Sahrens 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
2447789Sahrens 			ASSERT(error == 0);
24482638Sperrin 			zfs_log_rename(zilog, tx, TX_RENAME, sdzp,
24492638Sperrin 			    sdl->dl_name, tdzp, tdl->dl_name, szp);
2450789Sahrens 		}
2451789Sahrens 	}
2452789Sahrens 
2453789Sahrens 	dmu_tx_commit(tx);
2454789Sahrens out:
2455789Sahrens 	if (zl != NULL)
2456789Sahrens 		zfs_rename_unlock(&zl);
2457789Sahrens 
2458789Sahrens 	zfs_dirent_unlock(sdl);
2459789Sahrens 	zfs_dirent_unlock(tdl);
2460789Sahrens 
2461789Sahrens 	VN_RELE(ZTOV(szp));
2462789Sahrens 	if (tzp)
2463789Sahrens 		VN_RELE(ZTOV(tzp));
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	zoid;
2493789Sahrens 	int		len = strlen(link);
2494789Sahrens 	int		error;
2495789Sahrens 
2496789Sahrens 	ASSERT(vap->va_type == VLNK);
2497789Sahrens 
2498789Sahrens 	ZFS_ENTER(zfsvfs);
2499789Sahrens top:
2500789Sahrens 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
2501789Sahrens 		ZFS_EXIT(zfsvfs);
2502789Sahrens 		return (error);
2503789Sahrens 	}
2504789Sahrens 
2505789Sahrens 	if (len > MAXPATHLEN) {
2506789Sahrens 		ZFS_EXIT(zfsvfs);
2507789Sahrens 		return (ENAMETOOLONG);
2508789Sahrens 	}
2509789Sahrens 
2510789Sahrens 	/*
2511789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
2512789Sahrens 	 */
2513789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZNEW)) {
2514789Sahrens 		ZFS_EXIT(zfsvfs);
2515789Sahrens 		return (error);
2516789Sahrens 	}
2517789Sahrens 
2518789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2519789Sahrens 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
2520789Sahrens 	dmu_tx_hold_bonus(tx, dzp->z_id);
25211544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
2522789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
2523789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
2524789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2525789Sahrens 	if (error) {
2526789Sahrens 		zfs_dirent_unlock(dl);
2527789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
25282113Sahrens 			dmu_tx_wait(tx);
25292113Sahrens 			dmu_tx_abort(tx);
2530789Sahrens 			goto top;
2531789Sahrens 		}
25322113Sahrens 		dmu_tx_abort(tx);
2533789Sahrens 		ZFS_EXIT(zfsvfs);
2534789Sahrens 		return (error);
2535789Sahrens 	}
2536789Sahrens 
2537789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
2538789Sahrens 
2539789Sahrens 	/*
2540789Sahrens 	 * Create a new object for the symlink.
2541789Sahrens 	 * Put the link content into bonus buffer if it will fit;
2542789Sahrens 	 * otherwise, store it just like any other file data.
2543789Sahrens 	 */
2544789Sahrens 	zoid = 0;
2545789Sahrens 	if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
2546789Sahrens 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, len);
2547789Sahrens 		if (len != 0)
2548789Sahrens 			bcopy(link, zp->z_phys + 1, len);
2549789Sahrens 	} else {
2550789Sahrens 		dmu_buf_t *dbp;
25511669Sperrin 
2552789Sahrens 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
2553789Sahrens 
25541669Sperrin 		/*
25551669Sperrin 		 * Nothing can access the znode yet so no locking needed
25561669Sperrin 		 * for growing the znode's blocksize.
25571669Sperrin 		 */
25581669Sperrin 		zfs_grow_blocksize(zp, len, tx);
2559789Sahrens 
25601544Seschrock 		VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, zoid, 0, FTAG, &dbp));
2561789Sahrens 		dmu_buf_will_dirty(dbp, tx);
2562789Sahrens 
2563789Sahrens 		ASSERT3U(len, <=, dbp->db_size);
2564789Sahrens 		bcopy(link, dbp->db_data, len);
25651544Seschrock 		dmu_buf_rele(dbp, FTAG);
2566789Sahrens 	}
2567789Sahrens 	zp->z_phys->zp_size = len;
2568789Sahrens 
2569789Sahrens 	/*
2570789Sahrens 	 * Insert the new object into the directory.
2571789Sahrens 	 */
2572789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
2573789Sahrens out:
2574789Sahrens 	if (error == 0)
25752638Sperrin 		zfs_log_symlink(zilog, tx, TX_SYMLINK, dzp, zp, name, link);
2576789Sahrens 
2577789Sahrens 	dmu_tx_commit(tx);
2578789Sahrens 
2579789Sahrens 	zfs_dirent_unlock(dl);
2580789Sahrens 
2581789Sahrens 	VN_RELE(ZTOV(zp));
2582789Sahrens 
2583789Sahrens 	ZFS_EXIT(zfsvfs);
2584789Sahrens 	return (error);
2585789Sahrens }
2586789Sahrens 
2587789Sahrens /*
2588789Sahrens  * Return, in the buffer contained in the provided uio structure,
2589789Sahrens  * the symbolic path referred to by vp.
2590789Sahrens  *
2591789Sahrens  *	IN:	vp	- vnode of symbolic link.
2592789Sahrens  *		uoip	- structure to contain the link path.
2593789Sahrens  *		cr	- credentials of caller.
2594789Sahrens  *
2595789Sahrens  *	OUT:	uio	- structure to contain the link path.
2596789Sahrens  *
2597789Sahrens  *	RETURN:	0 if success
2598789Sahrens  *		error code if failure
2599789Sahrens  *
2600789Sahrens  * Timestamps:
2601789Sahrens  *	vp - atime updated
2602789Sahrens  */
2603789Sahrens /* ARGSUSED */
2604789Sahrens static int
2605789Sahrens zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr)
2606789Sahrens {
2607789Sahrens 	znode_t		*zp = VTOZ(vp);
2608789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2609789Sahrens 	size_t		bufsz;
2610789Sahrens 	int		error;
2611789Sahrens 
2612789Sahrens 	ZFS_ENTER(zfsvfs);
2613789Sahrens 
2614789Sahrens 	bufsz = (size_t)zp->z_phys->zp_size;
2615789Sahrens 	if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
2616789Sahrens 		error = uiomove(zp->z_phys + 1,
2617789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
2618789Sahrens 	} else {
26191544Seschrock 		dmu_buf_t *dbp;
26201544Seschrock 		error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
26211544Seschrock 		if (error) {
2622789Sahrens 			ZFS_EXIT(zfsvfs);
2623789Sahrens 			return (error);
2624789Sahrens 		}
2625789Sahrens 		error = uiomove(dbp->db_data,
2626789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
26271544Seschrock 		dmu_buf_rele(dbp, FTAG);
2628789Sahrens 	}
2629789Sahrens 
2630789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2631789Sahrens 	ZFS_EXIT(zfsvfs);
2632789Sahrens 	return (error);
2633789Sahrens }
2634789Sahrens 
2635789Sahrens /*
2636789Sahrens  * Insert a new entry into directory tdvp referencing svp.
2637789Sahrens  *
2638789Sahrens  *	IN:	tdvp	- Directory to contain new entry.
2639789Sahrens  *		svp	- vnode of new entry.
2640789Sahrens  *		name	- name of new entry.
2641789Sahrens  *		cr	- credentials of caller.
2642789Sahrens  *
2643789Sahrens  *	RETURN:	0 if success
2644789Sahrens  *		error code if failure
2645789Sahrens  *
2646789Sahrens  * Timestamps:
2647789Sahrens  *	tdvp - ctime|mtime updated
2648789Sahrens  *	 svp - ctime updated
2649789Sahrens  */
2650789Sahrens /* ARGSUSED */
2651789Sahrens static int
2652789Sahrens zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr)
2653789Sahrens {
2654789Sahrens 	znode_t		*dzp = VTOZ(tdvp);
2655789Sahrens 	znode_t		*tzp, *szp;
2656789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2657789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2658789Sahrens 	zfs_dirlock_t	*dl;
2659789Sahrens 	dmu_tx_t	*tx;
2660789Sahrens 	vnode_t		*realvp;
2661789Sahrens 	int		error;
2662789Sahrens 
2663789Sahrens 	ASSERT(tdvp->v_type == VDIR);
2664789Sahrens 
2665789Sahrens 	ZFS_ENTER(zfsvfs);
2666789Sahrens 
2667789Sahrens 	if (VOP_REALVP(svp, &realvp) == 0)
2668789Sahrens 		svp = realvp;
2669789Sahrens 
2670789Sahrens 	if (svp->v_vfsp != tdvp->v_vfsp) {
2671789Sahrens 		ZFS_EXIT(zfsvfs);
2672789Sahrens 		return (EXDEV);
2673789Sahrens 	}
2674789Sahrens 
2675789Sahrens 	szp = VTOZ(svp);
2676789Sahrens top:
2677789Sahrens 	/*
2678789Sahrens 	 * We do not support links between attributes and non-attributes
2679789Sahrens 	 * because of the potential security risk of creating links
2680789Sahrens 	 * into "normal" file space in order to circumvent restrictions
2681789Sahrens 	 * imposed in attribute space.
2682789Sahrens 	 */
2683789Sahrens 	if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
2684789Sahrens 	    (dzp->z_phys->zp_flags & ZFS_XATTR)) {
2685789Sahrens 		ZFS_EXIT(zfsvfs);
2686789Sahrens 		return (EINVAL);
2687789Sahrens 	}
2688789Sahrens 
2689789Sahrens 	/*
2690789Sahrens 	 * POSIX dictates that we return EPERM here.
2691789Sahrens 	 * Better choices include ENOTSUP or EISDIR.
2692789Sahrens 	 */
2693789Sahrens 	if (svp->v_type == VDIR) {
2694789Sahrens 		ZFS_EXIT(zfsvfs);
2695789Sahrens 		return (EPERM);
2696789Sahrens 	}
2697789Sahrens 
2698789Sahrens 	if ((uid_t)szp->z_phys->zp_uid != crgetuid(cr) &&
2699789Sahrens 	    secpolicy_basic_link(cr) != 0) {
2700789Sahrens 		ZFS_EXIT(zfsvfs);
2701789Sahrens 		return (EPERM);
2702789Sahrens 	}
2703789Sahrens 
2704789Sahrens 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
2705789Sahrens 		ZFS_EXIT(zfsvfs);
2706789Sahrens 		return (error);
2707789Sahrens 	}
2708789Sahrens 
2709789Sahrens 	/*
2710789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
2711789Sahrens 	 */
2712789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &tzp, ZNEW)) {
2713789Sahrens 		ZFS_EXIT(zfsvfs);
2714789Sahrens 		return (error);
2715789Sahrens 	}
2716789Sahrens 
2717789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2718789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);
27191544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
2720789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2721789Sahrens 	if (error) {
2722789Sahrens 		zfs_dirent_unlock(dl);
2723789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
27242113Sahrens 			dmu_tx_wait(tx);
27252113Sahrens 			dmu_tx_abort(tx);
2726789Sahrens 			goto top;
2727789Sahrens 		}
27282113Sahrens 		dmu_tx_abort(tx);
2729789Sahrens 		ZFS_EXIT(zfsvfs);
2730789Sahrens 		return (error);
2731789Sahrens 	}
2732789Sahrens 
2733789Sahrens 	error = zfs_link_create(dl, szp, tx, 0);
2734789Sahrens 
2735789Sahrens 	if (error == 0)
27362638Sperrin 		zfs_log_link(zilog, tx, TX_LINK, dzp, szp, name);
2737789Sahrens 
2738789Sahrens 	dmu_tx_commit(tx);
2739789Sahrens 
2740789Sahrens 	zfs_dirent_unlock(dl);
2741789Sahrens 
2742789Sahrens 	ZFS_EXIT(zfsvfs);
2743789Sahrens 	return (error);
2744789Sahrens }
2745789Sahrens 
2746789Sahrens /*
2747789Sahrens  * zfs_null_putapage() is used when the file system has been force
2748789Sahrens  * unmounted. It just drops the pages.
2749789Sahrens  */
2750789Sahrens /* ARGSUSED */
2751789Sahrens static int
2752789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
2753789Sahrens 		size_t *lenp, int flags, cred_t *cr)
2754789Sahrens {
2755789Sahrens 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
2756789Sahrens 	return (0);
2757789Sahrens }
2758789Sahrens 
27592688Smaybee /*
27602688Smaybee  * Push a page out to disk, klustering if possible.
27612688Smaybee  *
27622688Smaybee  *	IN:	vp	- file to push page to.
27632688Smaybee  *		pp	- page to push.
27642688Smaybee  *		flags	- additional flags.
27652688Smaybee  *		cr	- credentials of caller.
27662688Smaybee  *
27672688Smaybee  *	OUT:	offp	- start of range pushed.
27682688Smaybee  *		lenp	- len of range pushed.
27692688Smaybee  *
27702688Smaybee  *	RETURN:	0 if success
27712688Smaybee  *		error code if failure
27722688Smaybee  *
27732688Smaybee  * NOTE: callers must have locked the page to be pushed.  On
27742688Smaybee  * exit, the page (and all other pages in the kluster) must be
27752688Smaybee  * unlocked.
27762688Smaybee  */
2777789Sahrens /* ARGSUSED */
2778789Sahrens static int
2779789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
2780789Sahrens 		size_t *lenp, int flags, cred_t *cr)
2781789Sahrens {
2782789Sahrens 	znode_t		*zp = VTOZ(vp);
2783789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2784789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2785789Sahrens 	dmu_tx_t	*tx;
27861669Sperrin 	rl_t		*rl;
27872688Smaybee 	u_offset_t	off, koff;
27882688Smaybee 	size_t		len, klen;
2789789Sahrens 	int		err;
2790789Sahrens 
27912688Smaybee 	off = pp->p_offset;
27922688Smaybee 	len = PAGESIZE;
27932688Smaybee 	/*
27942688Smaybee 	 * If our blocksize is bigger than the page size, try to kluster
27952688Smaybee 	 * muiltiple pages so that we write a full block (thus avoiding
27962688Smaybee 	 * a read-modify-write).
27972688Smaybee 	 */
27982688Smaybee 	if (zp->z_blksz > PAGESIZE) {
27992688Smaybee 		uint64_t filesz = zp->z_phys->zp_size;
28002688Smaybee 
28012688Smaybee 		if (!ISP2(zp->z_blksz)) {
28022688Smaybee 			/* Only one block in the file. */
28032688Smaybee 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
28042688Smaybee 			koff = 0;
28052688Smaybee 		} else {
28062688Smaybee 			klen = zp->z_blksz;
28072688Smaybee 			koff = P2ALIGN(off, (u_offset_t)klen);
28082688Smaybee 		}
28092688Smaybee 		ASSERT(koff <= filesz);
28102688Smaybee 		if (koff + klen > filesz)
28112688Smaybee 			klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE);
28122688Smaybee 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
28132688Smaybee 	}
28142688Smaybee 	ASSERT3U(btop(len), ==, btopr(len));
2815789Sahrens top:
28162688Smaybee 	rl = zfs_range_lock(zp, off, len, RL_WRITER);
28171819Smaybee 	/*
28181819Smaybee 	 * Can't push pages past end-of-file.
28191819Smaybee 	 */
28201819Smaybee 	if (off >= zp->z_phys->zp_size) {
28212688Smaybee 		/* discard all pages */
28222688Smaybee 		flags |= B_INVAL;
28232688Smaybee 		err = 0;
28242688Smaybee 		goto out;
28252688Smaybee 	} else if (off + len > zp->z_phys->zp_size) {
28262688Smaybee 		int npages = btopr(zp->z_phys->zp_size - off);
28272688Smaybee 		page_t *trunc;
28282688Smaybee 
28292688Smaybee 		page_list_break(&pp, &trunc, npages);
28302688Smaybee 		/* discard pages past end of file */
28312688Smaybee 		if (trunc)
28322688Smaybee 			pvn_write_done(trunc, B_INVAL | flags);
28332688Smaybee 		len = zp->z_phys->zp_size - off;
28341819Smaybee 	}
2835789Sahrens 
2836789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2837789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, off, len);
2838789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
2839789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2840789Sahrens 	if (err != 0) {
2841789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
28422688Smaybee 			zfs_range_unlock(rl);
28432113Sahrens 			dmu_tx_wait(tx);
28442113Sahrens 			dmu_tx_abort(tx);
28452688Smaybee 			err = 0;
2846789Sahrens 			goto top;
2847789Sahrens 		}
28482113Sahrens 		dmu_tx_abort(tx);
2849789Sahrens 		goto out;
2850789Sahrens 	}
2851789Sahrens 
28522688Smaybee 	if (zp->z_blksz <= PAGESIZE) {
28532688Smaybee 		caddr_t va = ppmapin(pp, PROT_READ, (caddr_t)-1);
28542688Smaybee 		ASSERT3U(len, <=, PAGESIZE);
28552688Smaybee 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
28562688Smaybee 		ppmapout(va);
28572688Smaybee 	} else {
28582688Smaybee 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
28592688Smaybee 	}
28602688Smaybee 
28612688Smaybee 	if (err == 0) {
28622688Smaybee 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
28632688Smaybee 		(void) zfs_log_write(
28642688Smaybee 		    zilog, tx, TX_WRITE, zp, off, len, 0, NULL);
28652688Smaybee 		dmu_tx_commit(tx);
28662688Smaybee 	}
28672688Smaybee 
28682688Smaybee out:
28692237Smaybee 	zfs_range_unlock(rl);
28702688Smaybee 	pvn_write_done(pp, (err ? B_ERROR : 0) | B_WRITE | flags);
2871789Sahrens 	if (offp)
2872789Sahrens 		*offp = off;
2873789Sahrens 	if (lenp)
2874789Sahrens 		*lenp = len;
2875789Sahrens 
2876789Sahrens 	return (err);
2877789Sahrens }
2878789Sahrens 
2879789Sahrens /*
2880789Sahrens  * Copy the portion of the file indicated from pages into the file.
2881789Sahrens  * The pages are stored in a page list attached to the files vnode.
2882789Sahrens  *
2883789Sahrens  *	IN:	vp	- vnode of file to push page data to.
2884789Sahrens  *		off	- position in file to put data.
2885789Sahrens  *		len	- amount of data to write.
2886789Sahrens  *		flags	- flags to control the operation.
2887789Sahrens  *		cr	- credentials of caller.
2888789Sahrens  *
2889789Sahrens  *	RETURN:	0 if success
2890789Sahrens  *		error code if failure
2891789Sahrens  *
2892789Sahrens  * Timestamps:
2893789Sahrens  *	vp - ctime|mtime updated
2894789Sahrens  */
2895789Sahrens static int
2896789Sahrens zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr)
2897789Sahrens {
2898789Sahrens 	znode_t		*zp = VTOZ(vp);
2899789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2900789Sahrens 	page_t		*pp;
2901789Sahrens 	size_t		io_len;
2902789Sahrens 	u_offset_t	io_off;
29031669Sperrin 	uint64_t	filesz;
2904789Sahrens 	int		error = 0;
2905789Sahrens 
2906789Sahrens 	ZFS_ENTER(zfsvfs);
2907789Sahrens 
2908789Sahrens 	ASSERT(zp->z_dbuf_held && zp->z_phys);
2909789Sahrens 
2910789Sahrens 	if (len == 0) {
2911789Sahrens 		/*
2912789Sahrens 		 * Search the entire vp list for pages >= off.
2913789Sahrens 		 */
2914789Sahrens 		error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage,
2915789Sahrens 		    flags, cr);
29161472Sperrin 		goto out;
2917789Sahrens 	}
2918789Sahrens 
29191669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
29201669Sperrin 	if (off > filesz) {
2921789Sahrens 		/* past end of file */
2922789Sahrens 		ZFS_EXIT(zfsvfs);
2923789Sahrens 		return (0);
2924789Sahrens 	}
2925789Sahrens 
29261669Sperrin 	len = MIN(len, filesz - off);
2927789Sahrens 
29281472Sperrin 	for (io_off = off; io_off < off + len; io_off += io_len) {
2929789Sahrens 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
29301669Sperrin 			pp = page_lookup(vp, io_off,
2931789Sahrens 				(flags & (B_INVAL | B_FREE)) ?
2932789Sahrens 					SE_EXCL : SE_SHARED);
2933789Sahrens 		} else {
2934789Sahrens 			pp = page_lookup_nowait(vp, io_off,
2935789Sahrens 				(flags & B_FREE) ? SE_EXCL : SE_SHARED);
2936789Sahrens 		}
2937789Sahrens 
2938789Sahrens 		if (pp != NULL && pvn_getdirty(pp, flags)) {
2939789Sahrens 			int err;
2940789Sahrens 
2941789Sahrens 			/*
2942789Sahrens 			 * Found a dirty page to push
2943789Sahrens 			 */
29441669Sperrin 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
29451669Sperrin 			if (err)
2946789Sahrens 				error = err;
2947789Sahrens 		} else {
2948789Sahrens 			io_len = PAGESIZE;
2949789Sahrens 		}
2950789Sahrens 	}
29511472Sperrin out:
29522638Sperrin 	if ((flags & B_ASYNC) == 0)
29532638Sperrin 		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
2954789Sahrens 	ZFS_EXIT(zfsvfs);
2955789Sahrens 	return (error);
2956789Sahrens }
2957789Sahrens 
2958789Sahrens void
2959789Sahrens zfs_inactive(vnode_t *vp, cred_t *cr)
2960789Sahrens {
2961789Sahrens 	znode_t	*zp = VTOZ(vp);
2962789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2963789Sahrens 	int error;
2964789Sahrens 
2965789Sahrens 	rw_enter(&zfsvfs->z_um_lock, RW_READER);
2966789Sahrens 	if (zfsvfs->z_unmounted2) {
2967789Sahrens 		ASSERT(zp->z_dbuf_held == 0);
2968789Sahrens 
2969789Sahrens 		if (vn_has_cached_data(vp)) {
2970789Sahrens 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
2971789Sahrens 			    B_INVAL, cr);
2972789Sahrens 		}
2973789Sahrens 
29741544Seschrock 		mutex_enter(&zp->z_lock);
2975789Sahrens 		vp->v_count = 0; /* count arrives as 1 */
29761544Seschrock 		if (zp->z_dbuf == NULL) {
29771544Seschrock 			mutex_exit(&zp->z_lock);
29781544Seschrock 			zfs_znode_free(zp);
29791544Seschrock 		} else {
29801544Seschrock 			mutex_exit(&zp->z_lock);
29811544Seschrock 		}
2982789Sahrens 		rw_exit(&zfsvfs->z_um_lock);
2983789Sahrens 		VFS_RELE(zfsvfs->z_vfs);
2984789Sahrens 		return;
2985789Sahrens 	}
2986789Sahrens 
2987789Sahrens 	/*
2988789Sahrens 	 * Attempt to push any data in the page cache.  If this fails
2989789Sahrens 	 * we will get kicked out later in zfs_zinactive().
2990789Sahrens 	 */
29911298Sperrin 	if (vn_has_cached_data(vp)) {
29921298Sperrin 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
29931298Sperrin 		    cr);
29941298Sperrin 	}
2995789Sahrens 
2996789Sahrens 	if (zp->z_atime_dirty && zp->z_reap == 0) {
2997789Sahrens 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
2998789Sahrens 
2999789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
3000789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
3001789Sahrens 		if (error) {
3002789Sahrens 			dmu_tx_abort(tx);
3003789Sahrens 		} else {
3004789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
3005789Sahrens 			mutex_enter(&zp->z_lock);
3006789Sahrens 			zp->z_atime_dirty = 0;
3007789Sahrens 			mutex_exit(&zp->z_lock);
3008789Sahrens 			dmu_tx_commit(tx);
3009789Sahrens 		}
3010789Sahrens 	}
3011789Sahrens 
3012789Sahrens 	zfs_zinactive(zp);
3013789Sahrens 	rw_exit(&zfsvfs->z_um_lock);
3014789Sahrens }
3015789Sahrens 
3016789Sahrens /*
3017789Sahrens  * Bounds-check the seek operation.
3018789Sahrens  *
3019789Sahrens  *	IN:	vp	- vnode seeking within
3020789Sahrens  *		ooff	- old file offset
3021789Sahrens  *		noffp	- pointer to new file offset
3022789Sahrens  *
3023789Sahrens  *	RETURN:	0 if success
3024789Sahrens  *		EINVAL if new offset invalid
3025789Sahrens  */
3026789Sahrens /* ARGSUSED */
3027789Sahrens static int
3028789Sahrens zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp)
3029789Sahrens {
3030789Sahrens 	if (vp->v_type == VDIR)
3031789Sahrens 		return (0);
3032789Sahrens 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
3033789Sahrens }
3034789Sahrens 
3035789Sahrens /*
3036789Sahrens  * Pre-filter the generic locking function to trap attempts to place
3037789Sahrens  * a mandatory lock on a memory mapped file.
3038789Sahrens  */
3039789Sahrens static int
3040789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
3041789Sahrens     flk_callback_t *flk_cbp, cred_t *cr)
3042789Sahrens {
3043789Sahrens 	znode_t *zp = VTOZ(vp);
3044789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3045789Sahrens 	int error;
3046789Sahrens 
3047789Sahrens 	ZFS_ENTER(zfsvfs);
3048789Sahrens 
3049789Sahrens 	/*
30501544Seschrock 	 * We are following the UFS semantics with respect to mapcnt
30511544Seschrock 	 * here: If we see that the file is mapped already, then we will
30521544Seschrock 	 * return an error, but we don't worry about races between this
30531544Seschrock 	 * function and zfs_map().
3054789Sahrens 	 */
30551544Seschrock 	if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) {
3056789Sahrens 		ZFS_EXIT(zfsvfs);
3057789Sahrens 		return (EAGAIN);
3058789Sahrens 	}
3059789Sahrens 	error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr);
3060789Sahrens 	ZFS_EXIT(zfsvfs);
3061789Sahrens 	return (error);
3062789Sahrens }
3063789Sahrens 
3064789Sahrens /*
3065789Sahrens  * If we can't find a page in the cache, we will create a new page
3066789Sahrens  * and fill it with file data.  For efficiency, we may try to fill
30671669Sperrin  * multiple pages at once (klustering).
3068789Sahrens  */
3069789Sahrens static int
3070789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
3071789Sahrens     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
3072789Sahrens {
3073789Sahrens 	znode_t *zp = VTOZ(vp);
3074789Sahrens 	page_t *pp, *cur_pp;
3075789Sahrens 	objset_t *os = zp->z_zfsvfs->z_os;
3076789Sahrens 	caddr_t va;
3077789Sahrens 	u_offset_t io_off, total;
3078789Sahrens 	uint64_t oid = zp->z_id;
3079789Sahrens 	size_t io_len;
30801669Sperrin 	uint64_t filesz;
3081789Sahrens 	int err;
3082789Sahrens 
3083789Sahrens 	/*
3084789Sahrens 	 * If we are only asking for a single page don't bother klustering.
3085789Sahrens 	 */
30861669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
30872688Smaybee 	if (off >= filesz)
30882688Smaybee 		return (EFAULT);
30892688Smaybee 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
3090789Sahrens 		io_off = off;
3091789Sahrens 		io_len = PAGESIZE;
3092789Sahrens 		pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr);
3093789Sahrens 	} else {
3094789Sahrens 		/*
3095789Sahrens 		 * Try to fill a kluster of pages (a blocks worth).
3096789Sahrens 		 */
3097789Sahrens 		size_t klen;
3098789Sahrens 		u_offset_t koff;
3099789Sahrens 
3100789Sahrens 		if (!ISP2(zp->z_blksz)) {
3101789Sahrens 			/* Only one block in the file. */
3102789Sahrens 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
3103789Sahrens 			koff = 0;
3104789Sahrens 		} else {
3105789Sahrens 			klen = plsz;
3106789Sahrens 			koff = P2ALIGN(off, (u_offset_t)klen);
3107789Sahrens 		}
31081819Smaybee 		ASSERT(koff <= filesz);
31091819Smaybee 		if (koff + klen > filesz)
31101819Smaybee 			klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff;
31112688Smaybee 		ASSERT3U(off, >=, koff);
31122688Smaybee 		ASSERT3U(off, <, koff + klen);
3113789Sahrens 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
3114789Sahrens 			    &io_len, koff, klen, 0);
3115789Sahrens 	}
3116789Sahrens 	if (pp == NULL) {
3117789Sahrens 		/*
3118789Sahrens 		 * Some other thread entered the page before us.
3119789Sahrens 		 * Return to zfs_getpage to retry the lookup.
3120789Sahrens 		 */
3121789Sahrens 		*pl = NULL;
3122789Sahrens 		return (0);
3123789Sahrens 	}
3124789Sahrens 
3125789Sahrens 	/*
3126789Sahrens 	 * Fill the pages in the kluster.
3127789Sahrens 	 */
3128789Sahrens 	cur_pp = pp;
3129789Sahrens 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
31302688Smaybee 		ASSERT3U(io_off, ==, cur_pp->p_offset);
3131789Sahrens 		va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1);
31321544Seschrock 		err = dmu_read(os, oid, io_off, PAGESIZE, va);
3133789Sahrens 		ppmapout(va);
3134789Sahrens 		if (err) {
3135789Sahrens 			/* On error, toss the entire kluster */
3136789Sahrens 			pvn_read_done(pp, B_ERROR);
3137789Sahrens 			return (err);
3138789Sahrens 		}
3139789Sahrens 		cur_pp = cur_pp->p_next;
3140789Sahrens 	}
3141789Sahrens out:
3142789Sahrens 	/*
3143789Sahrens 	 * Fill in the page list array from the kluster.  If
3144789Sahrens 	 * there are too many pages in the kluster, return
3145789Sahrens 	 * as many pages as possible starting from the desired
3146789Sahrens 	 * offset `off'.
3147789Sahrens 	 * NOTE: the page list will always be null terminated.
3148789Sahrens 	 */
3149789Sahrens 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
3150789Sahrens 
3151789Sahrens 	return (0);
3152789Sahrens }
3153789Sahrens 
3154789Sahrens /*
3155789Sahrens  * Return pointers to the pages for the file region [off, off + len]
3156789Sahrens  * in the pl array.  If plsz is greater than len, this function may
3157789Sahrens  * also return page pointers from before or after the specified
3158789Sahrens  * region (i.e. some region [off', off' + plsz]).  These additional
3159789Sahrens  * pages are only returned if they are already in the cache, or were
3160789Sahrens  * created as part of a klustered read.
3161789Sahrens  *
3162789Sahrens  *	IN:	vp	- vnode of file to get data from.
3163789Sahrens  *		off	- position in file to get data from.
3164789Sahrens  *		len	- amount of data to retrieve.
3165789Sahrens  *		plsz	- length of provided page list.
3166789Sahrens  *		seg	- segment to obtain pages for.
3167789Sahrens  *		addr	- virtual address of fault.
3168789Sahrens  *		rw	- mode of created pages.
3169789Sahrens  *		cr	- credentials of caller.
3170789Sahrens  *
3171789Sahrens  *	OUT:	protp	- protection mode of created pages.
3172789Sahrens  *		pl	- list of pages created.
3173789Sahrens  *
3174789Sahrens  *	RETURN:	0 if success
3175789Sahrens  *		error code if failure
3176789Sahrens  *
3177789Sahrens  * Timestamps:
3178789Sahrens  *	vp - atime updated
3179789Sahrens  */
3180789Sahrens /* ARGSUSED */
3181789Sahrens static int
3182789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
3183789Sahrens 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
3184789Sahrens 	enum seg_rw rw, cred_t *cr)
3185789Sahrens {
3186789Sahrens 	znode_t		*zp = VTOZ(vp);
3187789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3188789Sahrens 	page_t		*pp, **pl0 = pl;
3189*2752Sperrin 	int		need_unlock = 0, err = 0;
3190*2752Sperrin 	offset_t	orig_off;
3191789Sahrens 
3192789Sahrens 	ZFS_ENTER(zfsvfs);
3193789Sahrens 
3194789Sahrens 	if (protp)
3195789Sahrens 		*protp = PROT_ALL;
3196789Sahrens 
3197789Sahrens 	ASSERT(zp->z_dbuf_held && zp->z_phys);
3198789Sahrens 
3199789Sahrens 	/* no faultahead (for now) */
3200789Sahrens 	if (pl == NULL) {
3201789Sahrens 		ZFS_EXIT(zfsvfs);
3202789Sahrens 		return (0);
3203789Sahrens 	}
3204789Sahrens 
3205789Sahrens 	/* can't fault past EOF */
3206789Sahrens 	if (off >= zp->z_phys->zp_size) {
3207789Sahrens 		ZFS_EXIT(zfsvfs);
3208789Sahrens 		return (EFAULT);
3209789Sahrens 	}
3210*2752Sperrin 	orig_off = off;
3211789Sahrens 
3212789Sahrens 	/*
3213789Sahrens 	 * If we already own the lock, then we must be page faulting
3214789Sahrens 	 * in the middle of a write to this file (i.e., we are writing
3215789Sahrens 	 * to this file using data from a mapped region of the file).
3216789Sahrens 	 */
3217*2752Sperrin 	if (rw_owner(&zp->z_map_lock) != curthread) {
3218789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
3219789Sahrens 		need_unlock = TRUE;
3220789Sahrens 	}
3221789Sahrens 
3222789Sahrens 	/*
3223789Sahrens 	 * Loop through the requested range [off, off + len] looking
3224789Sahrens 	 * for pages.  If we don't find a page, we will need to create
3225789Sahrens 	 * a new page and fill it with data from the file.
3226789Sahrens 	 */
3227789Sahrens 	while (len > 0) {
3228789Sahrens 		if (plsz < PAGESIZE)
3229789Sahrens 			break;
3230789Sahrens 		if (pp = page_lookup(vp, off, SE_SHARED)) {
3231789Sahrens 			*pl++ = pp;
3232789Sahrens 			off += PAGESIZE;
3233789Sahrens 			addr += PAGESIZE;
3234789Sahrens 			len -= PAGESIZE;
3235789Sahrens 			plsz -= PAGESIZE;
3236789Sahrens 		} else {
3237789Sahrens 			err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw);
3238*2752Sperrin 			if (err)
3239*2752Sperrin 				goto out;
3240789Sahrens 			/*
3241789Sahrens 			 * klustering may have changed our region
3242789Sahrens 			 * to be block aligned.
3243789Sahrens 			 */
3244789Sahrens 			if (((pp = *pl) != 0) && (off != pp->p_offset)) {
3245789Sahrens 				int delta = off - pp->p_offset;
3246789Sahrens 				len += delta;
3247789Sahrens 				off -= delta;
3248789Sahrens 				addr -= delta;
3249789Sahrens 			}
3250789Sahrens 			while (*pl) {
3251789Sahrens 				pl++;
3252789Sahrens 				off += PAGESIZE;
3253789Sahrens 				addr += PAGESIZE;
3254789Sahrens 				plsz -= PAGESIZE;
3255789Sahrens 				if (len > PAGESIZE)
3256789Sahrens 					len -= PAGESIZE;
3257789Sahrens 				else
3258789Sahrens 					len = 0;
3259789Sahrens 			}
3260789Sahrens 		}
3261789Sahrens 	}
3262789Sahrens 
3263789Sahrens 	/*
3264789Sahrens 	 * Fill out the page array with any pages already in the cache.
3265789Sahrens 	 */
3266789Sahrens 	while (plsz > 0) {
3267789Sahrens 		pp = page_lookup_nowait(vp, off, SE_SHARED);
3268789Sahrens 		if (pp == NULL)
3269789Sahrens 			break;
3270789Sahrens 		*pl++ = pp;
3271789Sahrens 		off += PAGESIZE;
3272789Sahrens 		plsz -= PAGESIZE;
3273789Sahrens 	}
3274789Sahrens 
3275789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3276789Sahrens out:
3277*2752Sperrin 	/*
3278*2752Sperrin 	 * We can't grab the range lock for the page as reader which would
3279*2752Sperrin 	 * stop truncation as this leads to deadlock. So we need to recheck
3280*2752Sperrin 	 * the file size.
3281*2752Sperrin 	 */
3282*2752Sperrin 	if (orig_off >= zp->z_phys->zp_size)
3283*2752Sperrin 		err = EFAULT;
3284*2752Sperrin 	if (err) {
3285*2752Sperrin 		/*
3286*2752Sperrin 		 * Release any pages we have previously locked.
3287*2752Sperrin 		 */
3288*2752Sperrin 		while (pl > pl0)
3289*2752Sperrin 			page_unlock(*--pl);
3290*2752Sperrin 	}
3291*2752Sperrin 
3292789Sahrens 	*pl = NULL;
3293789Sahrens 
3294789Sahrens 	if (need_unlock)
3295789Sahrens 		rw_exit(&zp->z_map_lock);
3296789Sahrens 
3297789Sahrens 	ZFS_EXIT(zfsvfs);
3298789Sahrens 	return (err);
3299789Sahrens }
3300789Sahrens 
33011544Seschrock /*
33021544Seschrock  * Request a memory map for a section of a file.  This code interacts
33031544Seschrock  * with common code and the VM system as follows:
33041544Seschrock  *
33051544Seschrock  *	common code calls mmap(), which ends up in smmap_common()
33061544Seschrock  *
33071544Seschrock  *	this calls VOP_MAP(), which takes you into (say) zfs
33081544Seschrock  *
33091544Seschrock  *	zfs_map() calls as_map(), passing segvn_create() as the callback
33101544Seschrock  *
33111544Seschrock  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
33121544Seschrock  *
33131544Seschrock  *	zfs_addmap() updates z_mapcnt
33141544Seschrock  */
3315789Sahrens static int
3316789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
3317789Sahrens     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
3318789Sahrens {
3319789Sahrens 	znode_t *zp = VTOZ(vp);
3320789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3321789Sahrens 	segvn_crargs_t	vn_a;
3322789Sahrens 	int		error;
3323789Sahrens 
3324789Sahrens 	ZFS_ENTER(zfsvfs);
3325789Sahrens 
3326789Sahrens 	if (vp->v_flag & VNOMAP) {
3327789Sahrens 		ZFS_EXIT(zfsvfs);
3328789Sahrens 		return (ENOSYS);
3329789Sahrens 	}
3330789Sahrens 
3331789Sahrens 	if (off < 0 || len > MAXOFFSET_T - off) {
3332789Sahrens 		ZFS_EXIT(zfsvfs);
3333789Sahrens 		return (ENXIO);
3334789Sahrens 	}
3335789Sahrens 
3336789Sahrens 	if (vp->v_type != VREG) {
3337789Sahrens 		ZFS_EXIT(zfsvfs);
3338789Sahrens 		return (ENODEV);
3339789Sahrens 	}
3340789Sahrens 
3341789Sahrens 	/*
3342789Sahrens 	 * If file is locked, disallow mapping.
3343789Sahrens 	 */
33441544Seschrock 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) {
33451544Seschrock 		ZFS_EXIT(zfsvfs);
33461544Seschrock 		return (EAGAIN);
3347789Sahrens 	}
3348789Sahrens 
3349789Sahrens 	as_rangelock(as);
3350789Sahrens 	if ((flags & MAP_FIXED) == 0) {
3351789Sahrens 		map_addr(addrp, len, off, 1, flags);
3352789Sahrens 		if (*addrp == NULL) {
3353789Sahrens 			as_rangeunlock(as);
3354789Sahrens 			ZFS_EXIT(zfsvfs);
3355789Sahrens 			return (ENOMEM);
3356789Sahrens 		}
3357789Sahrens 	} else {
3358789Sahrens 		/*
3359789Sahrens 		 * User specified address - blow away any previous mappings
3360789Sahrens 		 */
3361789Sahrens 		(void) as_unmap(as, *addrp, len);
3362789Sahrens 	}
3363789Sahrens 
3364789Sahrens 	vn_a.vp = vp;
3365789Sahrens 	vn_a.offset = (u_offset_t)off;
3366789Sahrens 	vn_a.type = flags & MAP_TYPE;
3367789Sahrens 	vn_a.prot = prot;
3368789Sahrens 	vn_a.maxprot = maxprot;
3369789Sahrens 	vn_a.cred = cr;
3370789Sahrens 	vn_a.amp = NULL;
3371789Sahrens 	vn_a.flags = flags & ~MAP_TYPE;
33721417Skchow 	vn_a.szc = 0;
33731417Skchow 	vn_a.lgrp_mem_policy_flags = 0;
3374789Sahrens 
3375789Sahrens 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
3376789Sahrens 
3377789Sahrens 	as_rangeunlock(as);
3378789Sahrens 	ZFS_EXIT(zfsvfs);
3379789Sahrens 	return (error);
3380789Sahrens }
3381789Sahrens 
3382789Sahrens /* ARGSUSED */
3383789Sahrens static int
3384789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
3385789Sahrens     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
3386789Sahrens {
33871544Seschrock 	uint64_t pages = btopr(len);
33881544Seschrock 
33891544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
3390789Sahrens 	return (0);
3391789Sahrens }
3392789Sahrens 
33931773Seschrock /*
33941773Seschrock  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
33951773Seschrock  * more accurate mtime for the associated file.  Since we don't have a way of
33961773Seschrock  * detecting when the data was actually modified, we have to resort to
33971773Seschrock  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
33981773Seschrock  * last page is pushed.  The problem occurs when the msync() call is omitted,
33991773Seschrock  * which by far the most common case:
34001773Seschrock  *
34011773Seschrock  * 	open()
34021773Seschrock  * 	mmap()
34031773Seschrock  * 	<modify memory>
34041773Seschrock  * 	munmap()
34051773Seschrock  * 	close()
34061773Seschrock  * 	<time lapse>
34071773Seschrock  * 	putpage() via fsflush
34081773Seschrock  *
34091773Seschrock  * If we wait until fsflush to come along, we can have a modification time that
34101773Seschrock  * is some arbitrary point in the future.  In order to prevent this in the
34111773Seschrock  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
34121773Seschrock  * torn down.
34131773Seschrock  */
3414789Sahrens /* ARGSUSED */
3415789Sahrens static int
3416789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
3417789Sahrens     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr)
3418789Sahrens {
34191544Seschrock 	uint64_t pages = btopr(len);
34201544Seschrock 
34211544Seschrock 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
34221544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
34231773Seschrock 
34241773Seschrock 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
34251773Seschrock 	    vn_has_cached_data(vp))
34261773Seschrock 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr);
34271773Seschrock 
3428789Sahrens 	return (0);
3429789Sahrens }
3430789Sahrens 
3431789Sahrens /*
3432789Sahrens  * Free or allocate space in a file.  Currently, this function only
3433789Sahrens  * supports the `F_FREESP' command.  However, this command is somewhat
3434789Sahrens  * misnamed, as its functionality includes the ability to allocate as
3435789Sahrens  * well as free space.
3436789Sahrens  *
3437789Sahrens  *	IN:	vp	- vnode of file to free data in.
3438789Sahrens  *		cmd	- action to take (only F_FREESP supported).
3439789Sahrens  *		bfp	- section of file to free/alloc.
3440789Sahrens  *		flag	- current file open mode flags.
3441789Sahrens  *		offset	- current file offset.
3442789Sahrens  *		cr	- credentials of caller [UNUSED].
3443789Sahrens  *
3444789Sahrens  *	RETURN:	0 if success
3445789Sahrens  *		error code if failure
3446789Sahrens  *
3447789Sahrens  * Timestamps:
3448789Sahrens  *	vp - ctime|mtime updated
3449789Sahrens  */
3450789Sahrens /* ARGSUSED */
3451789Sahrens static int
3452789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
3453789Sahrens     offset_t offset, cred_t *cr, caller_context_t *ct)
3454789Sahrens {
3455789Sahrens 	znode_t		*zp = VTOZ(vp);
3456789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3457789Sahrens 	uint64_t	off, len;
3458789Sahrens 	int		error;
3459789Sahrens 
3460789Sahrens 	ZFS_ENTER(zfsvfs);
3461789Sahrens 
3462789Sahrens top:
3463789Sahrens 	if (cmd != F_FREESP) {
3464789Sahrens 		ZFS_EXIT(zfsvfs);
3465789Sahrens 		return (EINVAL);
3466789Sahrens 	}
3467789Sahrens 
3468789Sahrens 	if (error = convoff(vp, bfp, 0, offset)) {
3469789Sahrens 		ZFS_EXIT(zfsvfs);
3470789Sahrens 		return (error);
3471789Sahrens 	}
3472789Sahrens 
3473789Sahrens 	if (bfp->l_len < 0) {
3474789Sahrens 		ZFS_EXIT(zfsvfs);
3475789Sahrens 		return (EINVAL);
3476789Sahrens 	}
3477789Sahrens 
3478789Sahrens 	off = bfp->l_start;
34791669Sperrin 	len = bfp->l_len; /* 0 means from off to end of file */
34801878Smaybee 
34811878Smaybee 	do {
34821878Smaybee 		error = zfs_freesp(zp, off, len, flag, TRUE);
34832113Sahrens 		/* NB: we already did dmu_tx_wait() if necessary */
34841878Smaybee 	} while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
3485789Sahrens 
3486789Sahrens 	ZFS_EXIT(zfsvfs);
3487789Sahrens 	return (error);
3488789Sahrens }
3489789Sahrens 
3490789Sahrens static int
3491789Sahrens zfs_fid(vnode_t *vp, fid_t *fidp)
3492789Sahrens {
3493789Sahrens 	znode_t		*zp = VTOZ(vp);
3494789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3495789Sahrens 	uint32_t	gen = (uint32_t)zp->z_phys->zp_gen;
3496789Sahrens 	uint64_t	object = zp->z_id;
3497789Sahrens 	zfid_short_t	*zfid;
3498789Sahrens 	int		size, i;
3499789Sahrens 
3500789Sahrens 	ZFS_ENTER(zfsvfs);
3501789Sahrens 
3502789Sahrens 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
3503789Sahrens 	if (fidp->fid_len < size) {
3504789Sahrens 		fidp->fid_len = size;
35051512Sek110237 		ZFS_EXIT(zfsvfs);
3506789Sahrens 		return (ENOSPC);
3507789Sahrens 	}
3508789Sahrens 
3509789Sahrens 	zfid = (zfid_short_t *)fidp;
3510789Sahrens 
3511789Sahrens 	zfid->zf_len = size;
3512789Sahrens 
3513789Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
3514789Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
3515789Sahrens 
3516789Sahrens 	/* Must have a non-zero generation number to distinguish from .zfs */
3517789Sahrens 	if (gen == 0)
3518789Sahrens 		gen = 1;
3519789Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
3520789Sahrens 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
3521789Sahrens 
3522789Sahrens 	if (size == LONG_FID_LEN) {
3523789Sahrens 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
3524789Sahrens 		zfid_long_t	*zlfid;
3525789Sahrens 
3526789Sahrens 		zlfid = (zfid_long_t *)fidp;
3527789Sahrens 
3528789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
3529789Sahrens 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
3530789Sahrens 
3531789Sahrens 		/* XXX - this should be the generation number for the objset */
3532789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
3533789Sahrens 			zlfid->zf_setgen[i] = 0;
3534789Sahrens 	}
3535789Sahrens 
3536789Sahrens 	ZFS_EXIT(zfsvfs);
3537789Sahrens 	return (0);
3538789Sahrens }
3539789Sahrens 
3540789Sahrens static int
3541789Sahrens zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr)
3542789Sahrens {
3543789Sahrens 	znode_t		*zp, *xzp;
3544789Sahrens 	zfsvfs_t	*zfsvfs;
3545789Sahrens 	zfs_dirlock_t	*dl;
3546789Sahrens 	int		error;
3547789Sahrens 
3548789Sahrens 	switch (cmd) {
3549789Sahrens 	case _PC_LINK_MAX:
3550789Sahrens 		*valp = ULONG_MAX;
3551789Sahrens 		return (0);
3552789Sahrens 
3553789Sahrens 	case _PC_FILESIZEBITS:
3554789Sahrens 		*valp = 64;
3555789Sahrens 		return (0);
3556789Sahrens 
3557789Sahrens 	case _PC_XATTR_EXISTS:
3558789Sahrens 		zp = VTOZ(vp);
3559789Sahrens 		zfsvfs = zp->z_zfsvfs;
3560789Sahrens 		ZFS_ENTER(zfsvfs);
3561789Sahrens 		*valp = 0;
3562789Sahrens 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
3563789Sahrens 		    ZXATTR | ZEXISTS | ZSHARED);
3564789Sahrens 		if (error == 0) {
3565789Sahrens 			zfs_dirent_unlock(dl);
3566789Sahrens 			if (!zfs_dirempty(xzp))
3567789Sahrens 				*valp = 1;
3568789Sahrens 			VN_RELE(ZTOV(xzp));
3569789Sahrens 		} else if (error == ENOENT) {
3570789Sahrens 			/*
3571789Sahrens 			 * If there aren't extended attributes, it's the
3572789Sahrens 			 * same as having zero of them.
3573789Sahrens 			 */
3574789Sahrens 			error = 0;
3575789Sahrens 		}
3576789Sahrens 		ZFS_EXIT(zfsvfs);
3577789Sahrens 		return (error);
3578789Sahrens 
3579789Sahrens 	case _PC_ACL_ENABLED:
3580789Sahrens 		*valp = _ACL_ACE_ENABLED;
3581789Sahrens 		return (0);
3582789Sahrens 
3583789Sahrens 	case _PC_MIN_HOLE_SIZE:
3584789Sahrens 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
3585789Sahrens 		return (0);
3586789Sahrens 
3587789Sahrens 	default:
3588789Sahrens 		return (fs_pathconf(vp, cmd, valp, cr));
3589789Sahrens 	}
3590789Sahrens }
3591789Sahrens 
3592789Sahrens /*ARGSUSED*/
3593789Sahrens static int
3594789Sahrens zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
3595789Sahrens {
3596789Sahrens 	znode_t *zp = VTOZ(vp);
3597789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3598789Sahrens 	int error;
3599789Sahrens 
3600789Sahrens 	ZFS_ENTER(zfsvfs);
3601789Sahrens 	error = zfs_getacl(zp, vsecp, cr);
3602789Sahrens 	ZFS_EXIT(zfsvfs);
3603789Sahrens 
3604789Sahrens 	return (error);
3605789Sahrens }
3606789Sahrens 
3607789Sahrens /*ARGSUSED*/
3608789Sahrens static int
3609789Sahrens zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
3610789Sahrens {
3611789Sahrens 	znode_t *zp = VTOZ(vp);
3612789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3613789Sahrens 	int error;
3614789Sahrens 
3615789Sahrens 	ZFS_ENTER(zfsvfs);
3616789Sahrens 	error = zfs_setacl(zp, vsecp, cr);
3617789Sahrens 	ZFS_EXIT(zfsvfs);
3618789Sahrens 	return (error);
3619789Sahrens }
3620789Sahrens 
3621789Sahrens /*
3622789Sahrens  * Predeclare these here so that the compiler assumes that
3623789Sahrens  * this is an "old style" function declaration that does
3624789Sahrens  * not include arguments => we won't get type mismatch errors
3625789Sahrens  * in the initializations that follow.
3626789Sahrens  */
3627789Sahrens static int zfs_inval();
3628789Sahrens static int zfs_isdir();
3629789Sahrens 
3630789Sahrens static int
3631789Sahrens zfs_inval()
3632789Sahrens {
3633789Sahrens 	return (EINVAL);
3634789Sahrens }
3635789Sahrens 
3636789Sahrens static int
3637789Sahrens zfs_isdir()
3638789Sahrens {
3639789Sahrens 	return (EISDIR);
3640789Sahrens }
3641789Sahrens /*
3642789Sahrens  * Directory vnode operations template
3643789Sahrens  */
3644789Sahrens vnodeops_t *zfs_dvnodeops;
3645789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = {
3646789Sahrens 	VOPNAME_OPEN, zfs_open,
3647789Sahrens 	VOPNAME_CLOSE, zfs_close,
3648789Sahrens 	VOPNAME_READ, zfs_isdir,
3649789Sahrens 	VOPNAME_WRITE, zfs_isdir,
3650789Sahrens 	VOPNAME_IOCTL, zfs_ioctl,
3651789Sahrens 	VOPNAME_GETATTR, zfs_getattr,
3652789Sahrens 	VOPNAME_SETATTR, zfs_setattr,
3653789Sahrens 	VOPNAME_ACCESS, zfs_access,
3654789Sahrens 	VOPNAME_LOOKUP, zfs_lookup,
3655789Sahrens 	VOPNAME_CREATE, zfs_create,
3656789Sahrens 	VOPNAME_REMOVE, zfs_remove,
3657789Sahrens 	VOPNAME_LINK, zfs_link,
3658789Sahrens 	VOPNAME_RENAME, zfs_rename,
3659789Sahrens 	VOPNAME_MKDIR, zfs_mkdir,
3660789Sahrens 	VOPNAME_RMDIR, zfs_rmdir,
3661789Sahrens 	VOPNAME_READDIR, zfs_readdir,
3662789Sahrens 	VOPNAME_SYMLINK, zfs_symlink,
3663789Sahrens 	VOPNAME_FSYNC, zfs_fsync,
3664789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3665789Sahrens 	VOPNAME_FID, zfs_fid,
3666789Sahrens 	VOPNAME_SEEK, zfs_seek,
3667789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3668789Sahrens 	VOPNAME_GETSECATTR, zfs_getsecattr,
3669789Sahrens 	VOPNAME_SETSECATTR, zfs_setsecattr,
3670789Sahrens 	NULL, NULL
3671789Sahrens };
3672789Sahrens 
3673789Sahrens /*
3674789Sahrens  * Regular file vnode operations template
3675789Sahrens  */
3676789Sahrens vnodeops_t *zfs_fvnodeops;
3677789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = {
3678789Sahrens 	VOPNAME_OPEN, zfs_open,
3679789Sahrens 	VOPNAME_CLOSE, zfs_close,
3680789Sahrens 	VOPNAME_READ, zfs_read,
3681789Sahrens 	VOPNAME_WRITE, zfs_write,
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_RENAME, zfs_rename,
3688789Sahrens 	VOPNAME_FSYNC, zfs_fsync,
3689789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p)zfs_inactive,
3690789Sahrens 	VOPNAME_FID, zfs_fid,
3691789Sahrens 	VOPNAME_SEEK, zfs_seek,
3692789Sahrens 	VOPNAME_FRLOCK, zfs_frlock,
3693789Sahrens 	VOPNAME_SPACE, zfs_space,
3694789Sahrens 	VOPNAME_GETPAGE, zfs_getpage,
3695789Sahrens 	VOPNAME_PUTPAGE, zfs_putpage,
3696789Sahrens 	VOPNAME_MAP, (fs_generic_func_p) zfs_map,
3697789Sahrens 	VOPNAME_ADDMAP, (fs_generic_func_p) zfs_addmap,
3698789Sahrens 	VOPNAME_DELMAP, zfs_delmap,
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  * Symbolic link vnode operations template
3708789Sahrens  */
3709789Sahrens vnodeops_t *zfs_symvnodeops;
3710789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = {
3711789Sahrens 	VOPNAME_GETATTR, zfs_getattr,
3712789Sahrens 	VOPNAME_SETATTR, zfs_setattr,
3713789Sahrens 	VOPNAME_ACCESS, zfs_access,
3714789Sahrens 	VOPNAME_RENAME, zfs_rename,
3715789Sahrens 	VOPNAME_READLINK, zfs_readlink,
3716789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3717789Sahrens 	VOPNAME_FID, zfs_fid,
3718789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3719789Sahrens 	VOPNAME_VNEVENT, fs_vnevent_support,
3720789Sahrens 	NULL, NULL
3721789Sahrens };
3722789Sahrens 
3723789Sahrens /*
3724789Sahrens  * Extended attribute directory vnode operations template
3725789Sahrens  *	This template is identical to the directory vnodes
3726789Sahrens  *	operation template except for restricted operations:
3727789Sahrens  *		VOP_MKDIR()
3728789Sahrens  *		VOP_SYMLINK()
3729789Sahrens  * Note that there are other restrictions embedded in:
3730789Sahrens  *	zfs_create()	- restrict type to VREG
3731789Sahrens  *	zfs_link()	- no links into/out of attribute space
3732789Sahrens  *	zfs_rename()	- no moves into/out of attribute space
3733789Sahrens  */
3734789Sahrens vnodeops_t *zfs_xdvnodeops;
3735789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = {
3736789Sahrens 	VOPNAME_OPEN, zfs_open,
3737789Sahrens 	VOPNAME_CLOSE, zfs_close,
3738789Sahrens 	VOPNAME_IOCTL, zfs_ioctl,
3739789Sahrens 	VOPNAME_GETATTR, zfs_getattr,
3740789Sahrens 	VOPNAME_SETATTR, zfs_setattr,
3741789Sahrens 	VOPNAME_ACCESS, zfs_access,
3742789Sahrens 	VOPNAME_LOOKUP, zfs_lookup,
3743789Sahrens 	VOPNAME_CREATE, zfs_create,
3744789Sahrens 	VOPNAME_REMOVE, zfs_remove,
3745789Sahrens 	VOPNAME_LINK, zfs_link,
3746789Sahrens 	VOPNAME_RENAME, zfs_rename,
3747789Sahrens 	VOPNAME_MKDIR, zfs_inval,
3748789Sahrens 	VOPNAME_RMDIR, zfs_rmdir,
3749789Sahrens 	VOPNAME_READDIR, zfs_readdir,
3750789Sahrens 	VOPNAME_SYMLINK, zfs_inval,
3751789Sahrens 	VOPNAME_FSYNC, zfs_fsync,
3752789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3753789Sahrens 	VOPNAME_FID, zfs_fid,
3754789Sahrens 	VOPNAME_SEEK, zfs_seek,
3755789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3756789Sahrens 	VOPNAME_GETSECATTR, zfs_getsecattr,
3757789Sahrens 	VOPNAME_SETSECATTR, zfs_setsecattr,
3758789Sahrens 	VOPNAME_VNEVENT, fs_vnevent_support,
3759789Sahrens 	NULL, NULL
3760789Sahrens };
3761789Sahrens 
3762789Sahrens /*
3763789Sahrens  * Error vnode operations template
3764789Sahrens  */
3765789Sahrens vnodeops_t *zfs_evnodeops;
3766789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = {
3767789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3768789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3769789Sahrens 	NULL, NULL
3770789Sahrens };
3771