xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 3280:e93ccc27c51d)
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 /* ARGSUSED */
158789Sahrens static int
159789Sahrens zfs_open(vnode_t **vpp, int flag, cred_t *cr)
160789Sahrens {
1613063Sperrin 	znode_t	*zp = VTOZ(*vpp);
1623063Sperrin 
1633063Sperrin 	/* Keep a count of the synchronous opens in the znode */
1643063Sperrin 	if (flag & (FSYNC | FDSYNC))
1653063Sperrin 		atomic_inc_32(&zp->z_sync_cnt);
166789Sahrens 	return (0);
167789Sahrens }
168789Sahrens 
169789Sahrens /* ARGSUSED */
170789Sahrens static int
171789Sahrens zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr)
172789Sahrens {
1733063Sperrin 	znode_t	*zp = VTOZ(vp);
1743063Sperrin 
1753063Sperrin 	/* Decrement the synchronous opens in the znode */
1763063Sperrin 	if (flag & (FSYNC | FDSYNC))
1773063Sperrin 		atomic_dec_32(&zp->z_sync_cnt);
1783063Sperrin 
179789Sahrens 	/*
180789Sahrens 	 * Clean up any locks held by this process on the vp.
181789Sahrens 	 */
182789Sahrens 	cleanlocks(vp, ddi_get_pid(), 0);
183789Sahrens 	cleanshares(vp, ddi_get_pid());
184789Sahrens 
185789Sahrens 	return (0);
186789Sahrens }
187789Sahrens 
188789Sahrens /*
189789Sahrens  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
190789Sahrens  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
191789Sahrens  */
192789Sahrens static int
193789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off)
194789Sahrens {
195789Sahrens 	znode_t	*zp = VTOZ(vp);
196789Sahrens 	uint64_t noff = (uint64_t)*off; /* new offset */
197789Sahrens 	uint64_t file_sz;
198789Sahrens 	int error;
199789Sahrens 	boolean_t hole;
200789Sahrens 
201789Sahrens 	file_sz = zp->z_phys->zp_size;
202789Sahrens 	if (noff >= file_sz)  {
203789Sahrens 		return (ENXIO);
204789Sahrens 	}
205789Sahrens 
206789Sahrens 	if (cmd == _FIO_SEEK_HOLE)
207789Sahrens 		hole = B_TRUE;
208789Sahrens 	else
209789Sahrens 		hole = B_FALSE;
210789Sahrens 
211789Sahrens 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
212789Sahrens 
213789Sahrens 	/* end of file? */
214789Sahrens 	if ((error == ESRCH) || (noff > file_sz)) {
215789Sahrens 		/*
216789Sahrens 		 * Handle the virtual hole at the end of file.
217789Sahrens 		 */
218789Sahrens 		if (hole) {
219789Sahrens 			*off = file_sz;
220789Sahrens 			return (0);
221789Sahrens 		}
222789Sahrens 		return (ENXIO);
223789Sahrens 	}
224789Sahrens 
225789Sahrens 	if (noff < *off)
226789Sahrens 		return (error);
227789Sahrens 	*off = noff;
228789Sahrens 	return (error);
229789Sahrens }
230789Sahrens 
231789Sahrens /* ARGSUSED */
232789Sahrens static int
233789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
234789Sahrens     int *rvalp)
235789Sahrens {
236789Sahrens 	offset_t off;
237789Sahrens 	int error;
238789Sahrens 	zfsvfs_t *zfsvfs;
239789Sahrens 
240789Sahrens 	switch (com) {
241789Sahrens 	    case _FIOFFS:
242789Sahrens 		return (zfs_sync(vp->v_vfsp, 0, cred));
243789Sahrens 
2441544Seschrock 		/*
2451544Seschrock 		 * The following two ioctls are used by bfu.  Faking out,
2461544Seschrock 		 * necessary to avoid bfu errors.
2471544Seschrock 		 */
2481544Seschrock 	    case _FIOGDIO:
2491544Seschrock 	    case _FIOSDIO:
2501544Seschrock 		return (0);
2511544Seschrock 
252789Sahrens 	    case _FIO_SEEK_DATA:
253789Sahrens 	    case _FIO_SEEK_HOLE:
254789Sahrens 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
255789Sahrens 			return (EFAULT);
256789Sahrens 
257789Sahrens 		zfsvfs = VTOZ(vp)->z_zfsvfs;
258789Sahrens 		ZFS_ENTER(zfsvfs);
259789Sahrens 
260789Sahrens 		/* offset parameter is in/out */
261789Sahrens 		error = zfs_holey(vp, com, &off);
262789Sahrens 		ZFS_EXIT(zfsvfs);
263789Sahrens 		if (error)
264789Sahrens 			return (error);
265789Sahrens 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
266789Sahrens 			return (EFAULT);
267789Sahrens 		return (0);
268789Sahrens 	}
269789Sahrens 	return (ENOTTY);
270789Sahrens }
271789Sahrens 
272789Sahrens /*
273789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
274789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
275789Sahrens  *
276789Sahrens  * On Write:	If we find a memory mapped page, we write to *both*
277789Sahrens  *		the page and the dmu buffer.
278789Sahrens  *
279789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
280789Sahrens  *	the file is memory mapped.
281789Sahrens  */
282789Sahrens static int
283789Sahrens mappedwrite(vnode_t *vp, uint64_t woff, int nbytes, uio_t *uio, dmu_tx_t *tx)
284789Sahrens {
285789Sahrens 	znode_t	*zp = VTOZ(vp);
286789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
287789Sahrens 	int64_t	start, off;
288789Sahrens 	int len = nbytes;
289789Sahrens 	int error = 0;
290789Sahrens 
291789Sahrens 	start = uio->uio_loffset;
292789Sahrens 	off = start & PAGEOFFSET;
293789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
294789Sahrens 		page_t *pp;
295789Sahrens 		uint64_t bytes = MIN(PAGESIZE - off, len);
296789Sahrens 
297789Sahrens 		/*
298789Sahrens 		 * We don't want a new page to "appear" in the middle of
299789Sahrens 		 * the file update (because it may not get the write
300789Sahrens 		 * update data), so we grab a lock to block
301789Sahrens 		 * zfs_getpage().
302789Sahrens 		 */
303789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
304789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
305789Sahrens 			caddr_t va;
306789Sahrens 
307789Sahrens 			rw_exit(&zp->z_map_lock);
308789Sahrens 			va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L);
309789Sahrens 			error = uiomove(va+off, bytes, UIO_WRITE, uio);
310789Sahrens 			if (error == 0) {
311789Sahrens 				dmu_write(zfsvfs->z_os, zp->z_id,
312789Sahrens 				    woff, bytes, va+off, tx);
313789Sahrens 			}
314789Sahrens 			ppmapout(va);
315789Sahrens 			page_unlock(pp);
316789Sahrens 		} else {
317789Sahrens 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
318789Sahrens 			    woff, bytes, uio, tx);
319789Sahrens 			rw_exit(&zp->z_map_lock);
320789Sahrens 		}
321789Sahrens 		len -= bytes;
322789Sahrens 		woff += bytes;
323789Sahrens 		off = 0;
324789Sahrens 		if (error)
325789Sahrens 			break;
326789Sahrens 	}
327789Sahrens 	return (error);
328789Sahrens }
329789Sahrens 
330789Sahrens /*
331789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
332789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
333789Sahrens  *
334789Sahrens  * On Read:	We "read" preferentially from memory mapped pages,
335789Sahrens  *		else we default from the dmu buffer.
336789Sahrens  *
337789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
338789Sahrens  *	the file is memory mapped.
339789Sahrens  */
340789Sahrens static int
341789Sahrens mappedread(vnode_t *vp, char *addr, int nbytes, uio_t *uio)
342789Sahrens {
343789Sahrens 	int64_t	start, off, bytes;
344789Sahrens 	int len = nbytes;
345789Sahrens 	int error = 0;
346789Sahrens 
347789Sahrens 	start = uio->uio_loffset;
348789Sahrens 	off = start & PAGEOFFSET;
349789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
350789Sahrens 		page_t *pp;
351789Sahrens 
352789Sahrens 		bytes = MIN(PAGESIZE - off, len);
353789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
354789Sahrens 			caddr_t va;
355789Sahrens 
3562688Smaybee 			va = ppmapin(pp, PROT_READ, (caddr_t)-1L);
357789Sahrens 			error = uiomove(va + off, bytes, UIO_READ, uio);
358789Sahrens 			ppmapout(va);
359789Sahrens 			page_unlock(pp);
360789Sahrens 		} else {
361789Sahrens 			/* XXX use dmu_read here? */
362789Sahrens 			error = uiomove(addr, bytes, UIO_READ, uio);
363789Sahrens 		}
364789Sahrens 		len -= bytes;
365789Sahrens 		addr += bytes;
366789Sahrens 		off = 0;
367789Sahrens 		if (error)
368789Sahrens 			break;
369789Sahrens 	}
370789Sahrens 	return (error);
371789Sahrens }
372789Sahrens 
373789Sahrens uint_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
374789Sahrens 
375789Sahrens /*
376789Sahrens  * Read bytes from specified file into supplied buffer.
377789Sahrens  *
378789Sahrens  *	IN:	vp	- vnode of file to be read from.
379789Sahrens  *		uio	- structure supplying read location, range info,
380789Sahrens  *			  and return buffer.
381789Sahrens  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
382789Sahrens  *		cr	- credentials of caller.
383789Sahrens  *
384789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
385789Sahrens  *
386789Sahrens  *	RETURN:	0 if success
387789Sahrens  *		error code if failure
388789Sahrens  *
389789Sahrens  * Side Effects:
390789Sahrens  *	vp - atime updated if byte count > 0
391789Sahrens  */
392789Sahrens /* ARGSUSED */
393789Sahrens static int
394789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
395789Sahrens {
396789Sahrens 	znode_t		*zp = VTOZ(vp);
397789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
398789Sahrens 	uint64_t	delta;
399789Sahrens 	ssize_t		n, size, cnt, ndone;
400789Sahrens 	int		error, i, numbufs;
401789Sahrens 	dmu_buf_t	*dbp, **dbpp;
4021669Sperrin 	rl_t		*rl;
403789Sahrens 
404789Sahrens 	ZFS_ENTER(zfsvfs);
405789Sahrens 
406789Sahrens 	/*
407789Sahrens 	 * Validate file offset
408789Sahrens 	 */
409789Sahrens 	if (uio->uio_loffset < (offset_t)0) {
410789Sahrens 		ZFS_EXIT(zfsvfs);
411789Sahrens 		return (EINVAL);
412789Sahrens 	}
413789Sahrens 
414789Sahrens 	/*
415789Sahrens 	 * Fasttrack empty reads
416789Sahrens 	 */
417789Sahrens 	if (uio->uio_resid == 0) {
418789Sahrens 		ZFS_EXIT(zfsvfs);
419789Sahrens 		return (0);
420789Sahrens 	}
421789Sahrens 
422789Sahrens 	/*
4231669Sperrin 	 * Check for mandatory locks
424789Sahrens 	 */
425789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
426789Sahrens 		if (error = chklock(vp, FREAD,
427789Sahrens 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
428789Sahrens 			ZFS_EXIT(zfsvfs);
429789Sahrens 			return (error);
430789Sahrens 		}
431789Sahrens 	}
432789Sahrens 
433789Sahrens 	/*
434789Sahrens 	 * If we're in FRSYNC mode, sync out this znode before reading it.
435789Sahrens 	 */
4362638Sperrin 	if (ioflag & FRSYNC)
4372638Sperrin 		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
438789Sahrens 
439789Sahrens 	/*
4401669Sperrin 	 * Lock the range against changes.
441789Sahrens 	 */
4421669Sperrin 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
4431669Sperrin 
444789Sahrens 	/*
445789Sahrens 	 * If we are reading past end-of-file we can skip
446789Sahrens 	 * to the end; but we might still need to set atime.
447789Sahrens 	 */
448789Sahrens 	if (uio->uio_loffset >= zp->z_phys->zp_size) {
449789Sahrens 		cnt = 0;
450789Sahrens 		error = 0;
451789Sahrens 		goto out;
452789Sahrens 	}
453789Sahrens 
454789Sahrens 	cnt = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset);
455789Sahrens 
456789Sahrens 	for (ndone = 0; ndone < cnt; ndone += zfs_read_chunk_size) {
457789Sahrens 		ASSERT(uio->uio_loffset < zp->z_phys->zp_size);
458789Sahrens 		n = MIN(zfs_read_chunk_size,
459789Sahrens 		    zp->z_phys->zp_size - uio->uio_loffset);
460789Sahrens 		n = MIN(n, cnt);
4612391Smaybee 		error = dmu_buf_hold_array_by_bonus(zp->z_dbuf,
4621544Seschrock 		    uio->uio_loffset, n, TRUE, FTAG, &numbufs, &dbpp);
4631544Seschrock 		if (error)
464789Sahrens 			goto out;
465789Sahrens 		/*
466789Sahrens 		 * Compute the adjustment to align the dmu buffers
467789Sahrens 		 * with the uio buffer.
468789Sahrens 		 */
469789Sahrens 		delta = uio->uio_loffset - dbpp[0]->db_offset;
470789Sahrens 
471789Sahrens 		for (i = 0; i < numbufs; i++) {
472789Sahrens 			if (n < 0)
473789Sahrens 				break;
474789Sahrens 			dbp = dbpp[i];
475789Sahrens 			size = dbp->db_size - delta;
476789Sahrens 			/*
477789Sahrens 			 * XXX -- this is correct, but may be suboptimal.
478789Sahrens 			 * If the pages are all clean, we don't need to
479789Sahrens 			 * go through mappedread().  Maybe the VMODSORT
480789Sahrens 			 * stuff can help us here.
481789Sahrens 			 */
482789Sahrens 			if (vn_has_cached_data(vp)) {
483789Sahrens 				error = mappedread(vp, (caddr_t)dbp->db_data +
484789Sahrens 				    delta, (n < size ? n : size), uio);
485789Sahrens 			} else {
486789Sahrens 				error = uiomove((caddr_t)dbp->db_data + delta,
487789Sahrens 					(n < size ? n : size), UIO_READ, uio);
488789Sahrens 			}
489789Sahrens 			if (error) {
4901544Seschrock 				dmu_buf_rele_array(dbpp, numbufs, FTAG);
491789Sahrens 				goto out;
492789Sahrens 			}
493789Sahrens 			n -= dbp->db_size;
494789Sahrens 			if (delta) {
495789Sahrens 				n += delta;
496789Sahrens 				delta = 0;
497789Sahrens 			}
498789Sahrens 		}
4991544Seschrock 		dmu_buf_rele_array(dbpp, numbufs, FTAG);
500789Sahrens 	}
501789Sahrens out:
5022237Smaybee 	zfs_range_unlock(rl);
503789Sahrens 
504789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
505789Sahrens 	ZFS_EXIT(zfsvfs);
506789Sahrens 	return (error);
507789Sahrens }
508789Sahrens 
509789Sahrens /*
510789Sahrens  * Fault in the pages of the first n bytes specified by the uio structure.
511789Sahrens  * 1 byte in each page is touched and the uio struct is unmodified.
512789Sahrens  * Any error will exit this routine as this is only a best
513789Sahrens  * attempt to get the pages resident. This is a copy of ufs_trans_touch().
514789Sahrens  */
515789Sahrens static void
516789Sahrens zfs_prefault_write(ssize_t n, struct uio *uio)
517789Sahrens {
518789Sahrens 	struct iovec *iov;
519789Sahrens 	ulong_t cnt, incr;
520789Sahrens 	caddr_t p;
521789Sahrens 	uint8_t tmp;
522789Sahrens 
523789Sahrens 	iov = uio->uio_iov;
524789Sahrens 
525789Sahrens 	while (n) {
526789Sahrens 		cnt = MIN(iov->iov_len, n);
527789Sahrens 		if (cnt == 0) {
528789Sahrens 			/* empty iov entry */
529789Sahrens 			iov++;
530789Sahrens 			continue;
531789Sahrens 		}
532789Sahrens 		n -= cnt;
533789Sahrens 		/*
534789Sahrens 		 * touch each page in this segment.
535789Sahrens 		 */
536789Sahrens 		p = iov->iov_base;
537789Sahrens 		while (cnt) {
538789Sahrens 			switch (uio->uio_segflg) {
539789Sahrens 			case UIO_USERSPACE:
540789Sahrens 			case UIO_USERISPACE:
541789Sahrens 				if (fuword8(p, &tmp))
542789Sahrens 					return;
543789Sahrens 				break;
544789Sahrens 			case UIO_SYSSPACE:
545789Sahrens 				if (kcopy(p, &tmp, 1))
546789Sahrens 					return;
547789Sahrens 				break;
548789Sahrens 			}
549789Sahrens 			incr = MIN(cnt, PAGESIZE);
550789Sahrens 			p += incr;
551789Sahrens 			cnt -= incr;
552789Sahrens 		}
553789Sahrens 		/*
554789Sahrens 		 * touch the last byte in case it straddles a page.
555789Sahrens 		 */
556789Sahrens 		p--;
557789Sahrens 		switch (uio->uio_segflg) {
558789Sahrens 		case UIO_USERSPACE:
559789Sahrens 		case UIO_USERISPACE:
560789Sahrens 			if (fuword8(p, &tmp))
561789Sahrens 				return;
562789Sahrens 			break;
563789Sahrens 		case UIO_SYSSPACE:
564789Sahrens 			if (kcopy(p, &tmp, 1))
565789Sahrens 				return;
566789Sahrens 			break;
567789Sahrens 		}
568789Sahrens 		iov++;
569789Sahrens 	}
570789Sahrens }
571789Sahrens 
572789Sahrens /*
573789Sahrens  * Write the bytes to a file.
574789Sahrens  *
575789Sahrens  *	IN:	vp	- vnode of file to be written to.
576789Sahrens  *		uio	- structure supplying write location, range info,
577789Sahrens  *			  and data buffer.
578789Sahrens  *		ioflag	- FAPPEND flag set if in append mode.
579789Sahrens  *		cr	- credentials of caller.
580789Sahrens  *
581789Sahrens  *	OUT:	uio	- updated offset and range.
582789Sahrens  *
583789Sahrens  *	RETURN:	0 if success
584789Sahrens  *		error code if failure
585789Sahrens  *
586789Sahrens  * Timestamps:
587789Sahrens  *	vp - ctime|mtime updated if byte count > 0
588789Sahrens  */
589789Sahrens /* ARGSUSED */
590789Sahrens static int
591789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
592789Sahrens {
593789Sahrens 	znode_t		*zp = VTOZ(vp);
594789Sahrens 	rlim64_t	limit = uio->uio_llimit;
595789Sahrens 	ssize_t		start_resid = uio->uio_resid;
596789Sahrens 	ssize_t		tx_bytes;
597789Sahrens 	uint64_t	end_size;
598789Sahrens 	dmu_tx_t	*tx;
599789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
600789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
601789Sahrens 	offset_t	woff;
602789Sahrens 	ssize_t		n, nbytes;
6031669Sperrin 	rl_t		*rl;
604789Sahrens 	int		max_blksz = zfsvfs->z_max_blksz;
6051669Sperrin 	int		error;
606789Sahrens 
607789Sahrens 	/*
608789Sahrens 	 * Fasttrack empty write
609789Sahrens 	 */
6101669Sperrin 	n = start_resid;
611789Sahrens 	if (n == 0)
612789Sahrens 		return (0);
613789Sahrens 
6141669Sperrin 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
6151669Sperrin 		limit = MAXOFFSET_T;
6161669Sperrin 
617789Sahrens 	ZFS_ENTER(zfsvfs);
618789Sahrens 
619789Sahrens 	/*
6202237Smaybee 	 * Pre-fault the pages to ensure slow (eg NFS) pages
6211669Sperrin 	 * don't hold up txg.
622789Sahrens 	 */
6232237Smaybee 	zfs_prefault_write(n, uio);
624789Sahrens 
625789Sahrens 	/*
626789Sahrens 	 * If in append mode, set the io offset pointer to eof.
627789Sahrens 	 */
6281669Sperrin 	if (ioflag & FAPPEND) {
6291669Sperrin 		/*
6301669Sperrin 		 * Range lock for a file append:
6311669Sperrin 		 * The value for the start of range will be determined by
6321669Sperrin 		 * zfs_range_lock() (to guarantee append semantics).
6331669Sperrin 		 * If this write will cause the block size to increase,
6341669Sperrin 		 * zfs_range_lock() will lock the entire file, so we must
6351669Sperrin 		 * later reduce the range after we grow the block size.
6361669Sperrin 		 */
6371669Sperrin 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
6381669Sperrin 		if (rl->r_len == UINT64_MAX) {
6391669Sperrin 			/* overlocked, zp_size can't change */
6401669Sperrin 			woff = uio->uio_loffset = zp->z_phys->zp_size;
6411669Sperrin 		} else {
6421669Sperrin 			woff = uio->uio_loffset = rl->r_off;
6431669Sperrin 		}
644789Sahrens 	} else {
645789Sahrens 		woff = uio->uio_loffset;
646789Sahrens 		/*
647789Sahrens 		 * Validate file offset
648789Sahrens 		 */
649789Sahrens 		if (woff < 0) {
650789Sahrens 			ZFS_EXIT(zfsvfs);
651789Sahrens 			return (EINVAL);
652789Sahrens 		}
653789Sahrens 
654789Sahrens 		/*
6551669Sperrin 		 * If we need to grow the block size then zfs_range_lock()
6561669Sperrin 		 * will lock a wider range than we request here.
6571669Sperrin 		 * Later after growing the block size we reduce the range.
658789Sahrens 		 */
6591669Sperrin 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
660789Sahrens 	}
661789Sahrens 
662789Sahrens 	if (woff >= limit) {
663789Sahrens 		error = EFBIG;
664789Sahrens 		goto no_tx_done;
665789Sahrens 	}
666789Sahrens 
667789Sahrens 	if ((woff + n) > limit || woff > (limit - n))
668789Sahrens 		n = limit - woff;
669789Sahrens 
670789Sahrens 	/*
6711669Sperrin 	 * Check for mandatory locks
672789Sahrens 	 */
673789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) &&
674789Sahrens 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0)
675789Sahrens 		goto no_tx_done;
6761669Sperrin 	end_size = MAX(zp->z_phys->zp_size, woff + n);
677789Sahrens top:
678789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
679789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
680789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
681789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
682789Sahrens 	if (error) {
683789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
6842113Sahrens 			dmu_tx_wait(tx);
6852113Sahrens 			dmu_tx_abort(tx);
686789Sahrens 			goto top;
687789Sahrens 		}
6882113Sahrens 		dmu_tx_abort(tx);
689789Sahrens 		goto no_tx_done;
690789Sahrens 	}
691789Sahrens 
6921669Sperrin 	/*
6931669Sperrin 	 * If zfs_range_lock() over-locked we grow the blocksize
6941669Sperrin 	 * and then reduce the lock range.
6951669Sperrin 	 */
6961669Sperrin 	if (rl->r_len == UINT64_MAX) {
697789Sahrens 		uint64_t new_blksz;
6981669Sperrin 
699789Sahrens 		if (zp->z_blksz > max_blksz) {
700789Sahrens 			ASSERT(!ISP2(zp->z_blksz));
701789Sahrens 			new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
702789Sahrens 		} else {
703789Sahrens 			new_blksz = MIN(end_size, max_blksz);
704789Sahrens 		}
7051669Sperrin 		zfs_grow_blocksize(zp, new_blksz, tx);
7062237Smaybee 		zfs_range_reduce(rl, woff, n);
707789Sahrens 	}
708789Sahrens 
709789Sahrens 	/*
710789Sahrens 	 * The file data does not fit in the znode "cache", so we
711789Sahrens 	 * will be writing to the file block data buffers.
712789Sahrens 	 * Each buffer will be written in a separate transaction;
713789Sahrens 	 * this keeps the intent log records small and allows us
714789Sahrens 	 * to do more fine-grained space accounting.
715789Sahrens 	 */
716789Sahrens 	while (n > 0) {
717789Sahrens 		/*
718789Sahrens 		 * XXX - should we really limit each write to z_max_blksz?
719789Sahrens 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
720789Sahrens 		 */
721789Sahrens 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
722789Sahrens 		rw_enter(&zp->z_map_lock, RW_READER);
723789Sahrens 
724789Sahrens 		tx_bytes = uio->uio_resid;
725789Sahrens 		if (vn_has_cached_data(vp)) {
726789Sahrens 			rw_exit(&zp->z_map_lock);
727789Sahrens 			error = mappedwrite(vp, woff, nbytes, uio, tx);
728789Sahrens 		} else {
729789Sahrens 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
730789Sahrens 			    woff, nbytes, uio, tx);
731789Sahrens 			rw_exit(&zp->z_map_lock);
732789Sahrens 		}
733789Sahrens 		tx_bytes -= uio->uio_resid;
734789Sahrens 
735789Sahrens 		if (error) {
736789Sahrens 			/* XXX - do we need to "clean up" the dmu buffer? */
737789Sahrens 			break;
738789Sahrens 		}
739789Sahrens 
740789Sahrens 		ASSERT(tx_bytes == nbytes);
741789Sahrens 
7421576Smarks 		/*
7431576Smarks 		 * Clear Set-UID/Set-GID bits on successful write if not
7441576Smarks 		 * privileged and at least one of the excute bits is set.
7451576Smarks 		 *
7461576Smarks 		 * It would be nice to to this after all writes have
7471576Smarks 		 * been done, but that would still expose the ISUID/ISGID
7481576Smarks 		 * to another app after the partial write is committed.
7491576Smarks 		 */
7501576Smarks 
7511576Smarks 		mutex_enter(&zp->z_acl_lock);
7521576Smarks 		if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) |
7531576Smarks 		    (S_IXUSR >> 6))) != 0 &&
7541576Smarks 		    (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 &&
7551576Smarks 		    secpolicy_vnode_setid_retain(cr,
7561576Smarks 		    (zp->z_phys->zp_mode & S_ISUID) != 0 &&
7571576Smarks 		    zp->z_phys->zp_uid == 0) != 0) {
7581576Smarks 			    zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID);
7591576Smarks 		}
7601576Smarks 		mutex_exit(&zp->z_acl_lock);
7611576Smarks 
762789Sahrens 		n -= nbytes;
763789Sahrens 		if (n <= 0)
764789Sahrens 			break;
765789Sahrens 
766789Sahrens 		/*
767789Sahrens 		 * We have more work ahead of us, so wrap up this transaction
768789Sahrens 		 * and start another.  Exact same logic as tx_done below.
769789Sahrens 		 */
770789Sahrens 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) {
771789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
772789Sahrens 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
773789Sahrens 			    uio->uio_loffset);
774789Sahrens 		}
775789Sahrens 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
7762638Sperrin 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes,
777789Sahrens 		    ioflag, uio);
778789Sahrens 		dmu_tx_commit(tx);
779789Sahrens 
780789Sahrens 		/*
781789Sahrens 		 * Start another transaction.
782789Sahrens 		 */
783789Sahrens 		woff = uio->uio_loffset;
784789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
785789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
786789Sahrens 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
787789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
788789Sahrens 		if (error) {
789789Sahrens 			if (error == ERESTART &&
790789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
7912113Sahrens 				dmu_tx_wait(tx);
7922113Sahrens 				dmu_tx_abort(tx);
793789Sahrens 				goto top;
794789Sahrens 			}
7952113Sahrens 			dmu_tx_abort(tx);
796789Sahrens 			goto no_tx_done;
797789Sahrens 		}
798789Sahrens 	}
799789Sahrens 
800789Sahrens tx_done:
801789Sahrens 
802789Sahrens 	if (tx_bytes != 0) {
803789Sahrens 		/*
804789Sahrens 		 * Update the file size if it has changed; account
805789Sahrens 		 * for possible concurrent updates.
806789Sahrens 		 */
807789Sahrens 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) {
808789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
809789Sahrens 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
810789Sahrens 			    uio->uio_loffset);
811789Sahrens 		}
812789Sahrens 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
8132638Sperrin 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes,
814789Sahrens 		    ioflag, uio);
815789Sahrens 	}
816789Sahrens 	dmu_tx_commit(tx);
817789Sahrens 
818789Sahrens 
819789Sahrens no_tx_done:
820789Sahrens 
8212237Smaybee 	zfs_range_unlock(rl);
822789Sahrens 
823789Sahrens 	/*
824789Sahrens 	 * If we're in replay mode, or we made no progress, return error.
825789Sahrens 	 * Otherwise, it's at least a partial write, so it's successful.
826789Sahrens 	 */
827789Sahrens 	if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) {
828789Sahrens 		ZFS_EXIT(zfsvfs);
829789Sahrens 		return (error);
830789Sahrens 	}
831789Sahrens 
8322638Sperrin 	if (ioflag & (FSYNC | FDSYNC))
8332638Sperrin 		zil_commit(zilog, zp->z_last_itx, zp->z_id);
834789Sahrens 
835789Sahrens 	ZFS_EXIT(zfsvfs);
836789Sahrens 	return (0);
837789Sahrens }
838789Sahrens 
8392237Smaybee void
8403063Sperrin zfs_get_done(dmu_buf_t *db, void *vzgd)
8412237Smaybee {
8423063Sperrin 	zgd_t *zgd = (zgd_t *)vzgd;
8433063Sperrin 	rl_t *rl = zgd->zgd_rl;
8442237Smaybee 	vnode_t *vp = ZTOV(rl->r_zp);
8452237Smaybee 
8463063Sperrin 	dmu_buf_rele(db, vzgd);
8472237Smaybee 	zfs_range_unlock(rl);
8482237Smaybee 	VN_RELE(vp);
8493063Sperrin 	zil_add_vdev(zgd->zgd_zilog, DVA_GET_VDEV(BP_IDENTITY(zgd->zgd_bp)));
8503063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
8512237Smaybee }
8522237Smaybee 
853789Sahrens /*
854789Sahrens  * Get data to generate a TX_WRITE intent log record.
855789Sahrens  */
856789Sahrens int
8572237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
858789Sahrens {
859789Sahrens 	zfsvfs_t *zfsvfs = arg;
860789Sahrens 	objset_t *os = zfsvfs->z_os;
861789Sahrens 	znode_t *zp;
862789Sahrens 	uint64_t off = lr->lr_offset;
8632237Smaybee 	dmu_buf_t *db;
8641669Sperrin 	rl_t *rl;
8653063Sperrin 	zgd_t *zgd;
866789Sahrens 	int dlen = lr->lr_length;  		/* length of user data */
867789Sahrens 	int error = 0;
868789Sahrens 
8693063Sperrin 	ASSERT(zio);
870789Sahrens 	ASSERT(dlen != 0);
871789Sahrens 
872789Sahrens 	/*
8731669Sperrin 	 * Nothing to do if the file has been removed
874789Sahrens 	 */
875789Sahrens 	if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0)
876789Sahrens 		return (ENOENT);
8771669Sperrin 	if (zp->z_reap) {
878789Sahrens 		VN_RELE(ZTOV(zp));
879789Sahrens 		return (ENOENT);
880789Sahrens 	}
881789Sahrens 
882789Sahrens 	/*
883789Sahrens 	 * Write records come in two flavors: immediate and indirect.
884789Sahrens 	 * For small writes it's cheaper to store the data with the
885789Sahrens 	 * log record (immediate); for large writes it's cheaper to
886789Sahrens 	 * sync the data and get a pointer to it (indirect) so that
887789Sahrens 	 * we don't have to write the data twice.
888789Sahrens 	 */
8891669Sperrin 	if (buf != NULL) { /* immediate write */
8901669Sperrin 		rl = zfs_range_lock(zp, off, dlen, RL_READER);
8911669Sperrin 		/* test for truncation needs to be done while range locked */
8921669Sperrin 		if (off >= zp->z_phys->zp_size) {
8931669Sperrin 			error = ENOENT;
8941669Sperrin 			goto out;
8951669Sperrin 		}
8962449Smaybee 		VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf));
8971669Sperrin 	} else { /* indirect write */
8981669Sperrin 		uint64_t boff; /* block starting offset */
8991669Sperrin 
9002449Smaybee 		ASSERT3U(dlen, <=, zp->z_blksz);
901789Sahrens 		/*
9021669Sperrin 		 * Have to lock the whole block to ensure when it's
9031669Sperrin 		 * written out and it's checksum is being calculated
9041669Sperrin 		 * that no one can change the data. We need to re-check
9051669Sperrin 		 * blocksize after we get the lock in case it's changed!
906789Sahrens 		 */
9071669Sperrin 		for (;;) {
9081941Sperrin 			if (ISP2(zp->z_blksz)) {
9091941Sperrin 				boff = P2ALIGN_TYPED(off, zp->z_blksz,
9101941Sperrin 				    uint64_t);
9111941Sperrin 			} else {
9121941Sperrin 				boff = 0;
9131941Sperrin 			}
9141669Sperrin 			dlen = zp->z_blksz;
9151669Sperrin 			rl = zfs_range_lock(zp, boff, dlen, RL_READER);
9161669Sperrin 			if (zp->z_blksz == dlen)
9171669Sperrin 				break;
9182237Smaybee 			zfs_range_unlock(rl);
9191669Sperrin 		}
9201669Sperrin 		/* test for truncation needs to be done while range locked */
9211669Sperrin 		if (off >= zp->z_phys->zp_size) {
9221669Sperrin 			error = ENOENT;
9231669Sperrin 			goto out;
9241669Sperrin 		}
9253063Sperrin 		zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
9263063Sperrin 		zgd->zgd_rl = rl;
9273063Sperrin 		zgd->zgd_zilog = zfsvfs->z_log;
9283063Sperrin 		zgd->zgd_bp = &lr->lr_blkptr;
9293063Sperrin 		VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db));
9302237Smaybee 		ASSERT(boff == db->db_offset);
9312237Smaybee 		lr->lr_blkoff = off - boff;
9322237Smaybee 		error = dmu_sync(zio, db, &lr->lr_blkptr,
9333063Sperrin 		    lr->lr_common.lrc_txg, zfs_get_done, zgd);
9343063Sperrin 		if (error == 0) {
9353063Sperrin 			zil_add_vdev(zfsvfs->z_log,
9363063Sperrin 			    DVA_GET_VDEV(BP_IDENTITY(&lr->lr_blkptr)));
9373063Sperrin 		}
9382237Smaybee 		/*
9392237Smaybee 		 * If we get EINPROGRESS, then we need to wait for a
9402237Smaybee 		 * write IO initiated by dmu_sync() to complete before
9412638Sperrin 		 * we can release this dbuf.  We will finish everything
9422237Smaybee 		 * up in the zfs_get_done() callback.
9432237Smaybee 		 */
9442237Smaybee 		if (error == EINPROGRESS)
9452237Smaybee 			return (0);
9463063Sperrin 		dmu_buf_rele(db, zgd);
9473063Sperrin 		kmem_free(zgd, sizeof (zgd_t));
948789Sahrens 	}
9491669Sperrin out:
9502237Smaybee 	zfs_range_unlock(rl);
951789Sahrens 	VN_RELE(ZTOV(zp));
952789Sahrens 	return (error);
953789Sahrens }
954789Sahrens 
955789Sahrens /*ARGSUSED*/
956789Sahrens static int
957789Sahrens zfs_access(vnode_t *vp, int mode, int flags, cred_t *cr)
958789Sahrens {
959789Sahrens 	znode_t *zp = VTOZ(vp);
960789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
961789Sahrens 	int error;
962789Sahrens 
963789Sahrens 	ZFS_ENTER(zfsvfs);
964789Sahrens 	error = zfs_zaccess_rwx(zp, mode, cr);
965789Sahrens 	ZFS_EXIT(zfsvfs);
966789Sahrens 	return (error);
967789Sahrens }
968789Sahrens 
969789Sahrens /*
970789Sahrens  * Lookup an entry in a directory, or an extended attribute directory.
971789Sahrens  * If it exists, return a held vnode reference for it.
972789Sahrens  *
973789Sahrens  *	IN:	dvp	- vnode of directory to search.
974789Sahrens  *		nm	- name of entry to lookup.
975789Sahrens  *		pnp	- full pathname to lookup [UNUSED].
976789Sahrens  *		flags	- LOOKUP_XATTR set if looking for an attribute.
977789Sahrens  *		rdir	- root directory vnode [UNUSED].
978789Sahrens  *		cr	- credentials of caller.
979789Sahrens  *
980789Sahrens  *	OUT:	vpp	- vnode of located entry, NULL if not found.
981789Sahrens  *
982789Sahrens  *	RETURN:	0 if success
983789Sahrens  *		error code if failure
984789Sahrens  *
985789Sahrens  * Timestamps:
986789Sahrens  *	NA
987789Sahrens  */
988789Sahrens /* ARGSUSED */
989789Sahrens static int
990789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
991789Sahrens     int flags, vnode_t *rdir, cred_t *cr)
992789Sahrens {
993789Sahrens 
994789Sahrens 	znode_t *zdp = VTOZ(dvp);
995789Sahrens 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
996789Sahrens 	int	error;
997789Sahrens 
998789Sahrens 	ZFS_ENTER(zfsvfs);
999789Sahrens 
1000789Sahrens 	*vpp = NULL;
1001789Sahrens 
1002789Sahrens 	if (flags & LOOKUP_XATTR) {
1003789Sahrens 		/*
10043234Sck153898 		 * If the xattr property is off, refuse the lookup request.
10053234Sck153898 		 */
10063234Sck153898 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
10073234Sck153898 			ZFS_EXIT(zfsvfs);
10083234Sck153898 			return (EINVAL);
10093234Sck153898 		}
10103234Sck153898 
10113234Sck153898 		/*
1012789Sahrens 		 * We don't allow recursive attributes..
1013789Sahrens 		 * Maybe someday we will.
1014789Sahrens 		 */
1015789Sahrens 		if (zdp->z_phys->zp_flags & ZFS_XATTR) {
1016789Sahrens 			ZFS_EXIT(zfsvfs);
1017789Sahrens 			return (EINVAL);
1018789Sahrens 		}
1019789Sahrens 
1020*3280Sck153898 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1021789Sahrens 			ZFS_EXIT(zfsvfs);
1022789Sahrens 			return (error);
1023789Sahrens 		}
1024789Sahrens 
1025789Sahrens 		/*
1026789Sahrens 		 * Do we have permission to get into attribute directory?
1027789Sahrens 		 */
1028789Sahrens 
1029789Sahrens 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, cr)) {
1030789Sahrens 			VN_RELE(*vpp);
1031789Sahrens 		}
1032789Sahrens 
1033789Sahrens 		ZFS_EXIT(zfsvfs);
1034789Sahrens 		return (error);
1035789Sahrens 	}
1036789Sahrens 
10371512Sek110237 	if (dvp->v_type != VDIR) {
10381512Sek110237 		ZFS_EXIT(zfsvfs);
10391460Smarks 		return (ENOTDIR);
10401512Sek110237 	}
10411460Smarks 
1042789Sahrens 	/*
1043789Sahrens 	 * Check accessibility of directory.
1044789Sahrens 	 */
1045789Sahrens 
1046789Sahrens 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, cr)) {
1047789Sahrens 		ZFS_EXIT(zfsvfs);
1048789Sahrens 		return (error);
1049789Sahrens 	}
1050789Sahrens 
1051789Sahrens 	if ((error = zfs_dirlook(zdp, nm, vpp)) == 0) {
1052789Sahrens 
1053789Sahrens 		/*
1054789Sahrens 		 * Convert device special files
1055789Sahrens 		 */
1056789Sahrens 		if (IS_DEVVP(*vpp)) {
1057789Sahrens 			vnode_t	*svp;
1058789Sahrens 
1059789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1060789Sahrens 			VN_RELE(*vpp);
1061789Sahrens 			if (svp == NULL)
1062789Sahrens 				error = ENOSYS;
1063789Sahrens 			else
1064789Sahrens 				*vpp = svp;
1065789Sahrens 		}
1066789Sahrens 	}
1067789Sahrens 
1068789Sahrens 	ZFS_EXIT(zfsvfs);
1069789Sahrens 	return (error);
1070789Sahrens }
1071789Sahrens 
1072789Sahrens /*
1073789Sahrens  * Attempt to create a new entry in a directory.  If the entry
1074789Sahrens  * already exists, truncate the file if permissible, else return
1075789Sahrens  * an error.  Return the vp of the created or trunc'd file.
1076789Sahrens  *
1077789Sahrens  *	IN:	dvp	- vnode of directory to put new file entry in.
1078789Sahrens  *		name	- name of new file entry.
1079789Sahrens  *		vap	- attributes of new file.
1080789Sahrens  *		excl	- flag indicating exclusive or non-exclusive mode.
1081789Sahrens  *		mode	- mode to open file with.
1082789Sahrens  *		cr	- credentials of caller.
1083789Sahrens  *		flag	- large file flag [UNUSED].
1084789Sahrens  *
1085789Sahrens  *	OUT:	vpp	- vnode of created or trunc'd entry.
1086789Sahrens  *
1087789Sahrens  *	RETURN:	0 if success
1088789Sahrens  *		error code if failure
1089789Sahrens  *
1090789Sahrens  * Timestamps:
1091789Sahrens  *	dvp - ctime|mtime updated if new entry created
1092789Sahrens  *	 vp - ctime|mtime always, atime if new
1093789Sahrens  */
1094789Sahrens /* ARGSUSED */
1095789Sahrens static int
1096789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1097789Sahrens     int mode, vnode_t **vpp, cred_t *cr, int flag)
1098789Sahrens {
1099789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1100789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1101789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1102789Sahrens 	objset_t	*os = zfsvfs->z_os;
1103789Sahrens 	zfs_dirlock_t	*dl;
1104789Sahrens 	dmu_tx_t	*tx;
1105789Sahrens 	int		error;
1106789Sahrens 	uint64_t	zoid;
1107789Sahrens 
1108789Sahrens 	ZFS_ENTER(zfsvfs);
1109789Sahrens 
1110789Sahrens top:
1111789Sahrens 	*vpp = NULL;
1112789Sahrens 
1113789Sahrens 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1114789Sahrens 		vap->va_mode &= ~VSVTX;
1115789Sahrens 
1116789Sahrens 	if (*name == '\0') {
1117789Sahrens 		/*
1118789Sahrens 		 * Null component name refers to the directory itself.
1119789Sahrens 		 */
1120789Sahrens 		VN_HOLD(dvp);
1121789Sahrens 		zp = dzp;
1122789Sahrens 		dl = NULL;
1123789Sahrens 		error = 0;
1124789Sahrens 	} else {
1125789Sahrens 		/* possible VN_HOLD(zp) */
1126789Sahrens 		if (error = zfs_dirent_lock(&dl, dzp, name, &zp, 0)) {
1127789Sahrens 			if (strcmp(name, "..") == 0)
1128789Sahrens 				error = EISDIR;
1129789Sahrens 			ZFS_EXIT(zfsvfs);
1130789Sahrens 			return (error);
1131789Sahrens 		}
1132789Sahrens 	}
1133789Sahrens 
1134789Sahrens 	zoid = zp ? zp->z_id : -1ULL;
1135789Sahrens 
1136789Sahrens 	if (zp == NULL) {
1137789Sahrens 		/*
1138789Sahrens 		 * Create a new file object and update the directory
1139789Sahrens 		 * to reference it.
1140789Sahrens 		 */
1141789Sahrens 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
1142789Sahrens 			goto out;
1143789Sahrens 		}
1144789Sahrens 
1145789Sahrens 		/*
1146789Sahrens 		 * We only support the creation of regular files in
1147789Sahrens 		 * extended attribute directories.
1148789Sahrens 		 */
1149789Sahrens 		if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1150789Sahrens 		    (vap->va_type != VREG)) {
1151789Sahrens 			error = EINVAL;
1152789Sahrens 			goto out;
1153789Sahrens 		}
1154789Sahrens 
1155789Sahrens 		tx = dmu_tx_create(os);
1156789Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1157789Sahrens 		dmu_tx_hold_bonus(tx, dzp->z_id);
11581544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1159789Sahrens 		if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
1160789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1161789Sahrens 			    0, SPA_MAXBLOCKSIZE);
1162789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
1163789Sahrens 		if (error) {
1164789Sahrens 			zfs_dirent_unlock(dl);
1165789Sahrens 			if (error == ERESTART &&
1166789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
11672113Sahrens 				dmu_tx_wait(tx);
11682113Sahrens 				dmu_tx_abort(tx);
1169789Sahrens 				goto top;
1170789Sahrens 			}
11712113Sahrens 			dmu_tx_abort(tx);
1172789Sahrens 			ZFS_EXIT(zfsvfs);
1173789Sahrens 			return (error);
1174789Sahrens 		}
1175789Sahrens 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
1176789Sahrens 		ASSERT(zp->z_id == zoid);
1177789Sahrens 		(void) zfs_link_create(dl, zp, tx, ZNEW);
11782638Sperrin 		zfs_log_create(zilog, tx, TX_CREATE, dzp, zp, name);
1179789Sahrens 		dmu_tx_commit(tx);
1180789Sahrens 	} else {
1181789Sahrens 		/*
1182789Sahrens 		 * A directory entry already exists for this name.
1183789Sahrens 		 */
1184789Sahrens 		/*
1185789Sahrens 		 * Can't truncate an existing file if in exclusive mode.
1186789Sahrens 		 */
1187789Sahrens 		if (excl == EXCL) {
1188789Sahrens 			error = EEXIST;
1189789Sahrens 			goto out;
1190789Sahrens 		}
1191789Sahrens 		/*
1192789Sahrens 		 * Can't open a directory for writing.
1193789Sahrens 		 */
1194789Sahrens 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1195789Sahrens 			error = EISDIR;
1196789Sahrens 			goto out;
1197789Sahrens 		}
1198789Sahrens 		/*
1199789Sahrens 		 * Verify requested access to file.
1200789Sahrens 		 */
1201789Sahrens 		if (mode && (error = zfs_zaccess_rwx(zp, mode, cr))) {
1202789Sahrens 			goto out;
1203789Sahrens 		}
1204789Sahrens 
1205789Sahrens 		mutex_enter(&dzp->z_lock);
1206789Sahrens 		dzp->z_seq++;
1207789Sahrens 		mutex_exit(&dzp->z_lock);
1208789Sahrens 
12091878Smaybee 		/*
12101878Smaybee 		 * Truncate regular files if requested.
12111878Smaybee 		 */
12121878Smaybee 		if ((ZTOV(zp)->v_type == VREG) &&
12131878Smaybee 		    (zp->z_phys->zp_size != 0) &&
1214789Sahrens 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
12151878Smaybee 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
12161878Smaybee 			if (error == ERESTART &&
12171878Smaybee 			    zfsvfs->z_assign == TXG_NOWAIT) {
12182113Sahrens 				/* NB: we already did dmu_tx_wait() */
12191878Smaybee 				zfs_dirent_unlock(dl);
12202365Sperrin 				VN_RELE(ZTOV(zp));
12211878Smaybee 				goto top;
1222789Sahrens 			}
1223789Sahrens 		}
1224789Sahrens 	}
1225789Sahrens out:
1226789Sahrens 
1227789Sahrens 	if (dl)
1228789Sahrens 		zfs_dirent_unlock(dl);
1229789Sahrens 
1230789Sahrens 	if (error) {
1231789Sahrens 		if (zp)
1232789Sahrens 			VN_RELE(ZTOV(zp));
1233789Sahrens 	} else {
1234789Sahrens 		*vpp = ZTOV(zp);
1235789Sahrens 		/*
1236789Sahrens 		 * If vnode is for a device return a specfs vnode instead.
1237789Sahrens 		 */
1238789Sahrens 		if (IS_DEVVP(*vpp)) {
1239789Sahrens 			struct vnode *svp;
1240789Sahrens 
1241789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1242789Sahrens 			VN_RELE(*vpp);
1243789Sahrens 			if (svp == NULL) {
1244789Sahrens 				error = ENOSYS;
1245789Sahrens 			}
1246789Sahrens 			*vpp = svp;
1247789Sahrens 		}
1248789Sahrens 	}
1249789Sahrens 
1250789Sahrens 	ZFS_EXIT(zfsvfs);
1251789Sahrens 	return (error);
1252789Sahrens }
1253789Sahrens 
1254789Sahrens /*
1255789Sahrens  * Remove an entry from a directory.
1256789Sahrens  *
1257789Sahrens  *	IN:	dvp	- vnode of directory to remove entry from.
1258789Sahrens  *		name	- name of entry to remove.
1259789Sahrens  *		cr	- credentials of caller.
1260789Sahrens  *
1261789Sahrens  *	RETURN:	0 if success
1262789Sahrens  *		error code if failure
1263789Sahrens  *
1264789Sahrens  * Timestamps:
1265789Sahrens  *	dvp - ctime|mtime
1266789Sahrens  *	 vp - ctime (if nlink > 0)
1267789Sahrens  */
1268789Sahrens static int
1269789Sahrens zfs_remove(vnode_t *dvp, char *name, cred_t *cr)
1270789Sahrens {
1271789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1272789Sahrens 	znode_t		*xzp = NULL;
1273789Sahrens 	vnode_t		*vp;
1274789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1275789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1276789Sahrens 	uint64_t	acl_obj, xattr_obj;
1277789Sahrens 	zfs_dirlock_t	*dl;
1278789Sahrens 	dmu_tx_t	*tx;
1279789Sahrens 	int		may_delete_now, delete_now = FALSE;
1280789Sahrens 	int		reaped;
1281789Sahrens 	int		error;
1282789Sahrens 
1283789Sahrens 	ZFS_ENTER(zfsvfs);
1284789Sahrens 
1285789Sahrens top:
1286789Sahrens 	/*
1287789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1288789Sahrens 	 */
1289789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
1290789Sahrens 		ZFS_EXIT(zfsvfs);
1291789Sahrens 		return (error);
1292789Sahrens 	}
1293789Sahrens 
1294789Sahrens 	vp = ZTOV(zp);
1295789Sahrens 
1296789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1297789Sahrens 		goto out;
1298789Sahrens 	}
1299789Sahrens 
1300789Sahrens 	/*
1301789Sahrens 	 * Need to use rmdir for removing directories.
1302789Sahrens 	 */
1303789Sahrens 	if (vp->v_type == VDIR) {
1304789Sahrens 		error = EPERM;
1305789Sahrens 		goto out;
1306789Sahrens 	}
1307789Sahrens 
1308789Sahrens 	vnevent_remove(vp);
1309789Sahrens 
13101484Sek110237 	dnlc_remove(dvp, name);
13111484Sek110237 
1312789Sahrens 	mutex_enter(&vp->v_lock);
1313789Sahrens 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1314789Sahrens 	mutex_exit(&vp->v_lock);
1315789Sahrens 
1316789Sahrens 	/*
1317789Sahrens 	 * We may delete the znode now, or we may put it on the delete queue;
1318789Sahrens 	 * it depends on whether we're the last link, and on whether there are
1319789Sahrens 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1320789Sahrens 	 * allow for either case.
1321789Sahrens 	 */
1322789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
13231544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1324789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
1325789Sahrens 	if (may_delete_now)
1326789Sahrens 		dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
1327789Sahrens 
1328789Sahrens 	/* are there any extended attributes? */
1329789Sahrens 	if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1330789Sahrens 		/*
1331789Sahrens 		 * XXX - There is a possibility that the delete
1332789Sahrens 		 * of the parent file could succeed, but then we get
1333789Sahrens 		 * an ENOSPC when we try to delete the xattrs...
1334789Sahrens 		 * so we would need to re-try the deletes periodically
1335789Sahrens 		 */
1336789Sahrens 		/* XXX - do we need this if we are deleting? */
1337789Sahrens 		dmu_tx_hold_bonus(tx, xattr_obj);
1338789Sahrens 	}
1339789Sahrens 
1340789Sahrens 	/* are there any additional acls */
1341789Sahrens 	if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1342789Sahrens 	    may_delete_now)
1343789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1344789Sahrens 
1345789Sahrens 	/* charge as an update -- would be nice not to charge at all */
13461544Seschrock 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
1347789Sahrens 
1348789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1349789Sahrens 	if (error) {
1350789Sahrens 		zfs_dirent_unlock(dl);
1351789Sahrens 		VN_RELE(vp);
1352789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
13532113Sahrens 			dmu_tx_wait(tx);
13542113Sahrens 			dmu_tx_abort(tx);
1355789Sahrens 			goto top;
1356789Sahrens 		}
13572113Sahrens 		dmu_tx_abort(tx);
1358789Sahrens 		ZFS_EXIT(zfsvfs);
1359789Sahrens 		return (error);
1360789Sahrens 	}
1361789Sahrens 
1362789Sahrens 	/*
1363789Sahrens 	 * Remove the directory entry.
1364789Sahrens 	 */
1365789Sahrens 	error = zfs_link_destroy(dl, zp, tx, 0, &reaped);
1366789Sahrens 
1367789Sahrens 	if (error) {
1368789Sahrens 		dmu_tx_commit(tx);
1369789Sahrens 		goto out;
1370789Sahrens 	}
1371789Sahrens 
1372789Sahrens 	if (reaped) {
1373789Sahrens 		mutex_enter(&vp->v_lock);
1374789Sahrens 		delete_now = may_delete_now &&
1375789Sahrens 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1376789Sahrens 		    zp->z_phys->zp_xattr == xattr_obj &&
1377789Sahrens 		    zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1378789Sahrens 		mutex_exit(&vp->v_lock);
1379789Sahrens 	}
1380789Sahrens 
1381789Sahrens 	if (delete_now) {
1382789Sahrens 		if (zp->z_phys->zp_xattr) {
1383789Sahrens 			error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1384789Sahrens 			ASSERT3U(error, ==, 0);
1385789Sahrens 			ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1386789Sahrens 			dmu_buf_will_dirty(xzp->z_dbuf, tx);
1387789Sahrens 			mutex_enter(&xzp->z_lock);
1388789Sahrens 			xzp->z_reap = 1;
1389789Sahrens 			xzp->z_phys->zp_links = 0;
1390789Sahrens 			mutex_exit(&xzp->z_lock);
1391789Sahrens 			zfs_dq_add(xzp, tx);
1392789Sahrens 			zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1393789Sahrens 		}
1394789Sahrens 		mutex_enter(&zp->z_lock);
1395789Sahrens 		mutex_enter(&vp->v_lock);
1396789Sahrens 		vp->v_count--;
1397789Sahrens 		ASSERT3U(vp->v_count, ==, 0);
1398789Sahrens 		mutex_exit(&vp->v_lock);
1399789Sahrens 		mutex_exit(&zp->z_lock);
1400789Sahrens 		zfs_znode_delete(zp, tx);
1401789Sahrens 		VFS_RELE(zfsvfs->z_vfs);
1402789Sahrens 	} else if (reaped) {
1403789Sahrens 		zfs_dq_add(zp, tx);
1404789Sahrens 	}
1405789Sahrens 
14062638Sperrin 	zfs_log_remove(zilog, tx, TX_REMOVE, dzp, name);
1407789Sahrens 
1408789Sahrens 	dmu_tx_commit(tx);
1409789Sahrens out:
1410789Sahrens 	zfs_dirent_unlock(dl);
1411789Sahrens 
1412789Sahrens 	if (!delete_now) {
1413789Sahrens 		VN_RELE(vp);
1414789Sahrens 	} else if (xzp) {
1415789Sahrens 		/* this rele delayed to prevent nesting transactions */
1416789Sahrens 		VN_RELE(ZTOV(xzp));
1417789Sahrens 	}
1418789Sahrens 
1419789Sahrens 	ZFS_EXIT(zfsvfs);
1420789Sahrens 	return (error);
1421789Sahrens }
1422789Sahrens 
1423789Sahrens /*
1424789Sahrens  * Create a new directory and insert it into dvp using the name
1425789Sahrens  * provided.  Return a pointer to the inserted directory.
1426789Sahrens  *
1427789Sahrens  *	IN:	dvp	- vnode of directory to add subdir to.
1428789Sahrens  *		dirname	- name of new directory.
1429789Sahrens  *		vap	- attributes of new directory.
1430789Sahrens  *		cr	- credentials of caller.
1431789Sahrens  *
1432789Sahrens  *	OUT:	vpp	- vnode of created directory.
1433789Sahrens  *
1434789Sahrens  *	RETURN:	0 if success
1435789Sahrens  *		error code if failure
1436789Sahrens  *
1437789Sahrens  * Timestamps:
1438789Sahrens  *	dvp - ctime|mtime updated
1439789Sahrens  *	 vp - ctime|mtime|atime updated
1440789Sahrens  */
1441789Sahrens static int
1442789Sahrens zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr)
1443789Sahrens {
1444789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1445789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1446789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1447789Sahrens 	zfs_dirlock_t	*dl;
1448789Sahrens 	uint64_t	zoid = 0;
1449789Sahrens 	dmu_tx_t	*tx;
1450789Sahrens 	int		error;
1451789Sahrens 
1452789Sahrens 	ASSERT(vap->va_type == VDIR);
1453789Sahrens 
1454789Sahrens 	ZFS_ENTER(zfsvfs);
1455789Sahrens 
1456789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1457789Sahrens 		ZFS_EXIT(zfsvfs);
1458789Sahrens 		return (EINVAL);
1459789Sahrens 	}
1460789Sahrens top:
1461789Sahrens 	*vpp = NULL;
1462789Sahrens 
1463789Sahrens 	/*
1464789Sahrens 	 * First make sure the new directory doesn't exist.
1465789Sahrens 	 */
1466789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, ZNEW)) {
1467789Sahrens 		ZFS_EXIT(zfsvfs);
1468789Sahrens 		return (error);
1469789Sahrens 	}
1470789Sahrens 
14711231Smarks 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, cr)) {
14721231Smarks 		zfs_dirent_unlock(dl);
14731231Smarks 		ZFS_EXIT(zfsvfs);
14741231Smarks 		return (error);
14751231Smarks 	}
14761231Smarks 
1477789Sahrens 	/*
1478789Sahrens 	 * Add a new entry to the directory.
1479789Sahrens 	 */
1480789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
14811544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
14821544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1483789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
1484789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1485789Sahrens 		    0, SPA_MAXBLOCKSIZE);
1486789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1487789Sahrens 	if (error) {
1488789Sahrens 		zfs_dirent_unlock(dl);
1489789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
14902113Sahrens 			dmu_tx_wait(tx);
14912113Sahrens 			dmu_tx_abort(tx);
1492789Sahrens 			goto top;
1493789Sahrens 		}
14942113Sahrens 		dmu_tx_abort(tx);
1495789Sahrens 		ZFS_EXIT(zfsvfs);
1496789Sahrens 		return (error);
1497789Sahrens 	}
1498789Sahrens 
1499789Sahrens 	/*
1500789Sahrens 	 * Create new node.
1501789Sahrens 	 */
1502789Sahrens 	zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
1503789Sahrens 
1504789Sahrens 	/*
1505789Sahrens 	 * Now put new name in parent dir.
1506789Sahrens 	 */
1507789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1508789Sahrens 
1509789Sahrens 	*vpp = ZTOV(zp);
1510789Sahrens 
15112638Sperrin 	zfs_log_create(zilog, tx, TX_MKDIR, dzp, zp, dirname);
1512789Sahrens 	dmu_tx_commit(tx);
1513789Sahrens 
1514789Sahrens 	zfs_dirent_unlock(dl);
1515789Sahrens 
1516789Sahrens 	ZFS_EXIT(zfsvfs);
1517789Sahrens 	return (0);
1518789Sahrens }
1519789Sahrens 
1520789Sahrens /*
1521789Sahrens  * Remove a directory subdir entry.  If the current working
1522789Sahrens  * directory is the same as the subdir to be removed, the
1523789Sahrens  * remove will fail.
1524789Sahrens  *
1525789Sahrens  *	IN:	dvp	- vnode of directory to remove from.
1526789Sahrens  *		name	- name of directory to be removed.
1527789Sahrens  *		cwd	- vnode of current working directory.
1528789Sahrens  *		cr	- credentials of caller.
1529789Sahrens  *
1530789Sahrens  *	RETURN:	0 if success
1531789Sahrens  *		error code if failure
1532789Sahrens  *
1533789Sahrens  * Timestamps:
1534789Sahrens  *	dvp - ctime|mtime updated
1535789Sahrens  */
1536789Sahrens static int
1537789Sahrens zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr)
1538789Sahrens {
1539789Sahrens 	znode_t		*dzp = VTOZ(dvp);
1540789Sahrens 	znode_t		*zp;
1541789Sahrens 	vnode_t		*vp;
1542789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1543789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1544789Sahrens 	zfs_dirlock_t	*dl;
1545789Sahrens 	dmu_tx_t	*tx;
1546789Sahrens 	int		error;
1547789Sahrens 
1548789Sahrens 	ZFS_ENTER(zfsvfs);
1549789Sahrens 
1550789Sahrens top:
1551789Sahrens 	zp = NULL;
1552789Sahrens 
1553789Sahrens 	/*
1554789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1555789Sahrens 	 */
1556789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
1557789Sahrens 		ZFS_EXIT(zfsvfs);
1558789Sahrens 		return (error);
1559789Sahrens 	}
1560789Sahrens 
1561789Sahrens 	vp = ZTOV(zp);
1562789Sahrens 
1563789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1564789Sahrens 		goto out;
1565789Sahrens 	}
1566789Sahrens 
1567789Sahrens 	if (vp->v_type != VDIR) {
1568789Sahrens 		error = ENOTDIR;
1569789Sahrens 		goto out;
1570789Sahrens 	}
1571789Sahrens 
1572789Sahrens 	if (vp == cwd) {
1573789Sahrens 		error = EINVAL;
1574789Sahrens 		goto out;
1575789Sahrens 	}
1576789Sahrens 
1577789Sahrens 	vnevent_rmdir(vp);
1578789Sahrens 
1579789Sahrens 	/*
1580789Sahrens 	 * Grab a lock on the parent pointer make sure we play well
1581789Sahrens 	 * with the treewalk and directory rename code.
1582789Sahrens 	 */
1583789Sahrens 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1584789Sahrens 
1585789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
15861544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1587789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
15881544Seschrock 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
1589789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1590789Sahrens 	if (error) {
1591789Sahrens 		rw_exit(&zp->z_parent_lock);
1592789Sahrens 		zfs_dirent_unlock(dl);
1593789Sahrens 		VN_RELE(vp);
1594789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
15952113Sahrens 			dmu_tx_wait(tx);
15962113Sahrens 			dmu_tx_abort(tx);
1597789Sahrens 			goto top;
1598789Sahrens 		}
15992113Sahrens 		dmu_tx_abort(tx);
1600789Sahrens 		ZFS_EXIT(zfsvfs);
1601789Sahrens 		return (error);
1602789Sahrens 	}
1603789Sahrens 
1604789Sahrens 	error = zfs_link_destroy(dl, zp, tx, 0, NULL);
1605789Sahrens 
1606789Sahrens 	if (error == 0)
16072638Sperrin 		zfs_log_remove(zilog, tx, TX_RMDIR, dzp, name);
1608789Sahrens 
1609789Sahrens 	dmu_tx_commit(tx);
1610789Sahrens 
1611789Sahrens 	rw_exit(&zp->z_parent_lock);
1612789Sahrens out:
1613789Sahrens 	zfs_dirent_unlock(dl);
1614789Sahrens 
1615789Sahrens 	VN_RELE(vp);
1616789Sahrens 
1617789Sahrens 	ZFS_EXIT(zfsvfs);
1618789Sahrens 	return (error);
1619789Sahrens }
1620789Sahrens 
1621789Sahrens /*
1622789Sahrens  * Read as many directory entries as will fit into the provided
1623789Sahrens  * buffer from the given directory cursor position (specified in
1624789Sahrens  * the uio structure.
1625789Sahrens  *
1626789Sahrens  *	IN:	vp	- vnode of directory to read.
1627789Sahrens  *		uio	- structure supplying read location, range info,
1628789Sahrens  *			  and return buffer.
1629789Sahrens  *		cr	- credentials of caller.
1630789Sahrens  *
1631789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
1632789Sahrens  *		eofp	- set to true if end-of-file detected.
1633789Sahrens  *
1634789Sahrens  *	RETURN:	0 if success
1635789Sahrens  *		error code if failure
1636789Sahrens  *
1637789Sahrens  * Timestamps:
1638789Sahrens  *	vp - atime updated
1639789Sahrens  *
1640789Sahrens  * Note that the low 4 bits of the cookie returned by zap is always zero.
1641789Sahrens  * This allows us to use the low range for "special" directory entries:
1642789Sahrens  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
1643789Sahrens  * we use the offset 2 for the '.zfs' directory.
1644789Sahrens  */
1645789Sahrens /* ARGSUSED */
1646789Sahrens static int
1647789Sahrens zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp)
1648789Sahrens {
1649789Sahrens 	znode_t		*zp = VTOZ(vp);
1650789Sahrens 	iovec_t		*iovp;
1651789Sahrens 	dirent64_t	*odp;
1652789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1653869Sperrin 	objset_t	*os;
1654789Sahrens 	caddr_t		outbuf;
1655789Sahrens 	size_t		bufsize;
1656789Sahrens 	zap_cursor_t	zc;
1657789Sahrens 	zap_attribute_t	zap;
1658789Sahrens 	uint_t		bytes_wanted;
1659789Sahrens 	ushort_t	this_reclen;
1660789Sahrens 	uint64_t	offset; /* must be unsigned; checks for < 1 */
1661789Sahrens 	off64_t		*next;
1662789Sahrens 	int		local_eof;
1663869Sperrin 	int		outcount;
1664869Sperrin 	int		error;
1665869Sperrin 	uint8_t		prefetch;
1666789Sahrens 
1667789Sahrens 	ZFS_ENTER(zfsvfs);
1668789Sahrens 
1669789Sahrens 	/*
1670789Sahrens 	 * If we are not given an eof variable,
1671789Sahrens 	 * use a local one.
1672789Sahrens 	 */
1673789Sahrens 	if (eofp == NULL)
1674789Sahrens 		eofp = &local_eof;
1675789Sahrens 
1676789Sahrens 	/*
1677789Sahrens 	 * Check for valid iov_len.
1678789Sahrens 	 */
1679789Sahrens 	if (uio->uio_iov->iov_len <= 0) {
1680789Sahrens 		ZFS_EXIT(zfsvfs);
1681789Sahrens 		return (EINVAL);
1682789Sahrens 	}
1683789Sahrens 
1684789Sahrens 	/*
1685789Sahrens 	 * Quit if directory has been removed (posix)
1686789Sahrens 	 */
1687789Sahrens 	if ((*eofp = zp->z_reap) != 0) {
1688789Sahrens 		ZFS_EXIT(zfsvfs);
1689789Sahrens 		return (0);
1690789Sahrens 	}
1691789Sahrens 
1692869Sperrin 	error = 0;
1693869Sperrin 	os = zfsvfs->z_os;
1694869Sperrin 	offset = uio->uio_loffset;
1695869Sperrin 	prefetch = zp->z_zn_prefetch;
1696869Sperrin 
1697789Sahrens 	/*
1698789Sahrens 	 * Initialize the iterator cursor.
1699789Sahrens 	 */
1700789Sahrens 	if (offset <= 3) {
1701789Sahrens 		/*
1702789Sahrens 		 * Start iteration from the beginning of the directory.
1703789Sahrens 		 */
1704869Sperrin 		zap_cursor_init(&zc, os, zp->z_id);
1705789Sahrens 	} else {
1706789Sahrens 		/*
1707789Sahrens 		 * The offset is a serialized cursor.
1708789Sahrens 		 */
1709869Sperrin 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1710789Sahrens 	}
1711789Sahrens 
1712789Sahrens 	/*
1713789Sahrens 	 * Get space to change directory entries into fs independent format.
1714789Sahrens 	 */
1715789Sahrens 	iovp = uio->uio_iov;
1716789Sahrens 	bytes_wanted = iovp->iov_len;
1717789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
1718789Sahrens 		bufsize = bytes_wanted;
1719789Sahrens 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
1720789Sahrens 		odp = (struct dirent64 *)outbuf;
1721789Sahrens 	} else {
1722789Sahrens 		bufsize = bytes_wanted;
1723789Sahrens 		odp = (struct dirent64 *)iovp->iov_base;
1724789Sahrens 	}
1725789Sahrens 
1726789Sahrens 	/*
1727789Sahrens 	 * Transform to file-system independent format
1728789Sahrens 	 */
1729789Sahrens 	outcount = 0;
1730789Sahrens 	while (outcount < bytes_wanted) {
1731789Sahrens 		/*
1732789Sahrens 		 * Special case `.', `..', and `.zfs'.
1733789Sahrens 		 */
1734789Sahrens 		if (offset == 0) {
1735789Sahrens 			(void) strcpy(zap.za_name, ".");
1736789Sahrens 			zap.za_first_integer = zp->z_id;
1737789Sahrens 			this_reclen = DIRENT64_RECLEN(1);
1738789Sahrens 		} else if (offset == 1) {
1739789Sahrens 			(void) strcpy(zap.za_name, "..");
1740789Sahrens 			zap.za_first_integer = zp->z_phys->zp_parent;
1741789Sahrens 			this_reclen = DIRENT64_RECLEN(2);
1742789Sahrens 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
1743789Sahrens 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
1744789Sahrens 			zap.za_first_integer = ZFSCTL_INO_ROOT;
1745789Sahrens 			this_reclen =
1746789Sahrens 			    DIRENT64_RECLEN(sizeof (ZFS_CTLDIR_NAME) - 1);
1747789Sahrens 		} else {
1748789Sahrens 			/*
1749789Sahrens 			 * Grab next entry.
1750789Sahrens 			 */
1751789Sahrens 			if (error = zap_cursor_retrieve(&zc, &zap)) {
1752789Sahrens 				if ((*eofp = (error == ENOENT)) != 0)
1753789Sahrens 					break;
1754789Sahrens 				else
1755789Sahrens 					goto update;
1756789Sahrens 			}
1757789Sahrens 
1758789Sahrens 			if (zap.za_integer_length != 8 ||
1759789Sahrens 			    zap.za_num_integers != 1) {
1760789Sahrens 				cmn_err(CE_WARN, "zap_readdir: bad directory "
1761789Sahrens 				    "entry, obj = %lld, offset = %lld\n",
1762789Sahrens 				    (u_longlong_t)zp->z_id,
1763789Sahrens 				    (u_longlong_t)offset);
1764789Sahrens 				error = ENXIO;
1765789Sahrens 				goto update;
1766789Sahrens 			}
1767789Sahrens 			this_reclen = DIRENT64_RECLEN(strlen(zap.za_name));
1768789Sahrens 		}
1769789Sahrens 
1770789Sahrens 		/*
1771789Sahrens 		 * Will this entry fit in the buffer?
1772789Sahrens 		 */
1773789Sahrens 		if (outcount + this_reclen > bufsize) {
1774789Sahrens 			/*
1775789Sahrens 			 * Did we manage to fit anything in the buffer?
1776789Sahrens 			 */
1777789Sahrens 			if (!outcount) {
1778789Sahrens 				error = EINVAL;
1779789Sahrens 				goto update;
1780789Sahrens 			}
1781789Sahrens 			break;
1782789Sahrens 		}
1783789Sahrens 		/*
1784789Sahrens 		 * Add this entry:
1785789Sahrens 		 */
1786789Sahrens 		odp->d_ino = (ino64_t)zap.za_first_integer;
1787789Sahrens 		odp->d_reclen = (ushort_t)this_reclen;
1788789Sahrens 		/* NOTE: d_off is the offset for the *next* entry */
1789789Sahrens 		next = &(odp->d_off);
1790789Sahrens 		(void) strncpy(odp->d_name, zap.za_name,
1791789Sahrens 		    DIRENT64_NAMELEN(this_reclen));
1792789Sahrens 		outcount += this_reclen;
1793789Sahrens 		odp = (dirent64_t *)((intptr_t)odp + this_reclen);
1794789Sahrens 
1795789Sahrens 		ASSERT(outcount <= bufsize);
1796789Sahrens 
1797789Sahrens 		/* Prefetch znode */
1798869Sperrin 		if (prefetch)
1799869Sperrin 			dmu_prefetch(os, zap.za_first_integer, 0, 0);
1800789Sahrens 
1801789Sahrens 		/*
1802789Sahrens 		 * Move to the next entry, fill in the previous offset.
1803789Sahrens 		 */
1804789Sahrens 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
1805789Sahrens 			zap_cursor_advance(&zc);
1806789Sahrens 			offset = zap_cursor_serialize(&zc);
1807789Sahrens 		} else {
1808789Sahrens 			offset += 1;
1809789Sahrens 		}
1810789Sahrens 		*next = offset;
1811789Sahrens 	}
1812869Sperrin 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
1813789Sahrens 
1814789Sahrens 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
1815789Sahrens 		iovp->iov_base += outcount;
1816789Sahrens 		iovp->iov_len -= outcount;
1817789Sahrens 		uio->uio_resid -= outcount;
1818789Sahrens 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
1819789Sahrens 		/*
1820789Sahrens 		 * Reset the pointer.
1821789Sahrens 		 */
1822789Sahrens 		offset = uio->uio_loffset;
1823789Sahrens 	}
1824789Sahrens 
1825789Sahrens update:
1826885Sahrens 	zap_cursor_fini(&zc);
1827789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1828789Sahrens 		kmem_free(outbuf, bufsize);
1829789Sahrens 
1830789Sahrens 	if (error == ENOENT)
1831789Sahrens 		error = 0;
1832789Sahrens 
1833789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
1834789Sahrens 
1835789Sahrens 	uio->uio_loffset = offset;
1836789Sahrens 	ZFS_EXIT(zfsvfs);
1837789Sahrens 	return (error);
1838789Sahrens }
1839789Sahrens 
1840789Sahrens static int
1841789Sahrens zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr)
1842789Sahrens {
1843789Sahrens 	znode_t	*zp = VTOZ(vp);
1844789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1845789Sahrens 
18461773Seschrock 	/*
18471773Seschrock 	 * Regardless of whether this is required for standards conformance,
18481773Seschrock 	 * this is the logical behavior when fsync() is called on a file with
18491773Seschrock 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
18501773Seschrock 	 * going to be pushed out as part of the zil_commit().
18511773Seschrock 	 */
18521773Seschrock 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
18531773Seschrock 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
18541773Seschrock 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr);
18551773Seschrock 
1856789Sahrens 	ZFS_ENTER(zfsvfs);
18572638Sperrin 	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
1858789Sahrens 	ZFS_EXIT(zfsvfs);
1859789Sahrens 	return (0);
1860789Sahrens }
1861789Sahrens 
1862789Sahrens /*
1863789Sahrens  * Get the requested file attributes and place them in the provided
1864789Sahrens  * vattr structure.
1865789Sahrens  *
1866789Sahrens  *	IN:	vp	- vnode of file.
1867789Sahrens  *		vap	- va_mask identifies requested attributes.
1868789Sahrens  *		flags	- [UNUSED]
1869789Sahrens  *		cr	- credentials of caller.
1870789Sahrens  *
1871789Sahrens  *	OUT:	vap	- attribute values.
1872789Sahrens  *
1873789Sahrens  *	RETURN:	0 (always succeeds)
1874789Sahrens  */
1875789Sahrens /* ARGSUSED */
1876789Sahrens static int
1877789Sahrens zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
1878789Sahrens {
1879789Sahrens 	znode_t *zp = VTOZ(vp);
1880789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1881789Sahrens 	znode_phys_t *pzp = zp->z_phys;
1882789Sahrens 	int	error;
1883789Sahrens 
1884789Sahrens 	ZFS_ENTER(zfsvfs);
1885789Sahrens 
1886789Sahrens 	/*
1887789Sahrens 	 * Return all attributes.  It's cheaper to provide the answer
1888789Sahrens 	 * than to determine whether we were asked the question.
1889789Sahrens 	 */
1890789Sahrens 	mutex_enter(&zp->z_lock);
1891789Sahrens 
1892789Sahrens 	vap->va_type = vp->v_type;
1893789Sahrens 	vap->va_mode = pzp->zp_mode & MODEMASK;
1894789Sahrens 	vap->va_uid = zp->z_phys->zp_uid;
1895789Sahrens 	vap->va_gid = zp->z_phys->zp_gid;
1896789Sahrens 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
1897789Sahrens 	vap->va_nodeid = zp->z_id;
1898789Sahrens 	vap->va_nlink = MIN(pzp->zp_links, UINT32_MAX);	/* nlink_t limit! */
1899789Sahrens 	vap->va_size = pzp->zp_size;
19001816Smarks 	vap->va_rdev = vp->v_rdev;
1901789Sahrens 	vap->va_seq = zp->z_seq;
1902789Sahrens 
1903789Sahrens 	ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
1904789Sahrens 	ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
1905789Sahrens 	ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
1906789Sahrens 
1907789Sahrens 	/*
1908905Smarks 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
1909905Smarks 	 * Also, if we are the owner don't bother, since owner should
1910905Smarks 	 * always be allowed to read basic attributes of file.
1911789Sahrens 	 */
1912905Smarks 	if (!(zp->z_phys->zp_flags & ZFS_ACL_TRIVIAL) &&
1913905Smarks 	    (zp->z_phys->zp_uid != crgetuid(cr))) {
1914905Smarks 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, cr)) {
1915789Sahrens 			mutex_exit(&zp->z_lock);
1916789Sahrens 			ZFS_EXIT(zfsvfs);
1917789Sahrens 			return (error);
1918789Sahrens 		}
1919789Sahrens 	}
1920789Sahrens 
1921789Sahrens 	mutex_exit(&zp->z_lock);
1922789Sahrens 
1923789Sahrens 	dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks);
1924789Sahrens 
1925789Sahrens 	if (zp->z_blksz == 0) {
1926789Sahrens 		/*
1927789Sahrens 		 * Block size hasn't been set; suggest maximal I/O transfers.
1928789Sahrens 		 */
1929789Sahrens 		vap->va_blksize = zfsvfs->z_max_blksz;
1930789Sahrens 	}
1931789Sahrens 
1932789Sahrens 	ZFS_EXIT(zfsvfs);
1933789Sahrens 	return (0);
1934789Sahrens }
1935789Sahrens 
1936789Sahrens /*
1937789Sahrens  * Set the file attributes to the values contained in the
1938789Sahrens  * vattr structure.
1939789Sahrens  *
1940789Sahrens  *	IN:	vp	- vnode of file to be modified.
1941789Sahrens  *		vap	- new attribute values.
1942789Sahrens  *		flags	- ATTR_UTIME set if non-default time values provided.
1943789Sahrens  *		cr	- credentials of caller.
1944789Sahrens  *
1945789Sahrens  *	RETURN:	0 if success
1946789Sahrens  *		error code if failure
1947789Sahrens  *
1948789Sahrens  * Timestamps:
1949789Sahrens  *	vp - ctime updated, mtime updated if size changed.
1950789Sahrens  */
1951789Sahrens /* ARGSUSED */
1952789Sahrens static int
1953789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1954789Sahrens 	caller_context_t *ct)
1955789Sahrens {
1956789Sahrens 	struct znode	*zp = VTOZ(vp);
1957789Sahrens 	znode_phys_t	*pzp = zp->z_phys;
1958789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1959789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1960789Sahrens 	dmu_tx_t	*tx;
19611878Smaybee 	vattr_t		oldva;
1962789Sahrens 	uint_t		mask = vap->va_mask;
19631878Smaybee 	uint_t		saved_mask;
19642796Smarks 	int		trim_mask = 0;
1965789Sahrens 	uint64_t	new_mode;
19661231Smarks 	znode_t		*attrzp;
1967789Sahrens 	int		need_policy = FALSE;
1968789Sahrens 	int		err;
1969789Sahrens 
1970789Sahrens 	if (mask == 0)
1971789Sahrens 		return (0);
1972789Sahrens 
1973789Sahrens 	if (mask & AT_NOSET)
1974789Sahrens 		return (EINVAL);
1975789Sahrens 
1976789Sahrens 	if (mask & AT_SIZE && vp->v_type == VDIR)
1977789Sahrens 		return (EISDIR);
1978789Sahrens 
19791394Smarks 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO)
19801308Smarks 		return (EINVAL);
19811308Smarks 
1982789Sahrens 	ZFS_ENTER(zfsvfs);
1983789Sahrens 
1984789Sahrens top:
19851231Smarks 	attrzp = NULL;
1986789Sahrens 
1987789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
1988789Sahrens 		ZFS_EXIT(zfsvfs);
1989789Sahrens 		return (EROFS);
1990789Sahrens 	}
1991789Sahrens 
1992789Sahrens 	/*
1993789Sahrens 	 * First validate permissions
1994789Sahrens 	 */
1995789Sahrens 
1996789Sahrens 	if (mask & AT_SIZE) {
1997789Sahrens 		err = zfs_zaccess(zp, ACE_WRITE_DATA, cr);
1998789Sahrens 		if (err) {
1999789Sahrens 			ZFS_EXIT(zfsvfs);
2000789Sahrens 			return (err);
2001789Sahrens 		}
20021878Smaybee 		/*
20031878Smaybee 		 * XXX - Note, we are not providing any open
20041878Smaybee 		 * mode flags here (like FNDELAY), so we may
20051878Smaybee 		 * block if there are locks present... this
20061878Smaybee 		 * should be addressed in openat().
20071878Smaybee 		 */
20081878Smaybee 		do {
20091878Smaybee 			err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
20102113Sahrens 			/* NB: we already did dmu_tx_wait() if necessary */
20111878Smaybee 		} while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
20121878Smaybee 		if (err) {
20131878Smaybee 			ZFS_EXIT(zfsvfs);
20141878Smaybee 			return (err);
20151878Smaybee 		}
2016789Sahrens 	}
2017789Sahrens 
2018789Sahrens 	if (mask & (AT_ATIME|AT_MTIME))
2019789Sahrens 		need_policy = zfs_zaccess_v4_perm(zp, ACE_WRITE_ATTRIBUTES, cr);
2020789Sahrens 
2021789Sahrens 	if (mask & (AT_UID|AT_GID)) {
2022789Sahrens 		int	idmask = (mask & (AT_UID|AT_GID));
2023789Sahrens 		int	take_owner;
2024789Sahrens 		int	take_group;
2025789Sahrens 
2026789Sahrens 		/*
2027913Smarks 		 * NOTE: even if a new mode is being set,
2028913Smarks 		 * we may clear S_ISUID/S_ISGID bits.
2029913Smarks 		 */
2030913Smarks 
2031913Smarks 		if (!(mask & AT_MODE))
2032913Smarks 			vap->va_mode = pzp->zp_mode;
2033913Smarks 
2034913Smarks 		/*
2035789Sahrens 		 * Take ownership or chgrp to group we are a member of
2036789Sahrens 		 */
2037789Sahrens 
2038789Sahrens 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2039789Sahrens 		take_group = (mask & AT_GID) && groupmember(vap->va_gid, cr);
2040789Sahrens 
2041789Sahrens 		/*
2042789Sahrens 		 * If both AT_UID and AT_GID are set then take_owner and
2043789Sahrens 		 * take_group must both be set in order to allow taking
2044789Sahrens 		 * ownership.
2045789Sahrens 		 *
2046789Sahrens 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2047789Sahrens 		 *
2048789Sahrens 		 */
2049789Sahrens 
2050789Sahrens 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2051789Sahrens 		    ((idmask == AT_UID) && take_owner) ||
2052789Sahrens 		    ((idmask == AT_GID) && take_group)) {
2053789Sahrens 			if (zfs_zaccess_v4_perm(zp, ACE_WRITE_OWNER, cr) == 0) {
2054789Sahrens 				/*
2055789Sahrens 				 * Remove setuid/setgid for non-privileged users
2056789Sahrens 				 */
20571115Smarks 				secpolicy_setid_clear(vap, cr);
20582796Smarks 				trim_mask = (mask & (AT_UID|AT_GID));
2059789Sahrens 			} else {
2060789Sahrens 				need_policy =  TRUE;
2061789Sahrens 			}
2062789Sahrens 		} else {
2063789Sahrens 			need_policy =  TRUE;
2064789Sahrens 		}
2065789Sahrens 	}
2066789Sahrens 
20672796Smarks 	mutex_enter(&zp->z_lock);
20682796Smarks 	oldva.va_mode = pzp->zp_mode;
20692796Smarks 	oldva.va_uid = zp->z_phys->zp_uid;
20702796Smarks 	oldva.va_gid = zp->z_phys->zp_gid;
20712796Smarks 	mutex_exit(&zp->z_lock);
20722796Smarks 
20732796Smarks 	if (mask & AT_MODE) {
20742796Smarks 		if (zfs_zaccess_v4_perm(zp, ACE_WRITE_ACL, cr) == 0) {
20752796Smarks 			err = secpolicy_setid_setsticky_clear(vp, vap,
20762796Smarks 			    &oldva, cr);
20772796Smarks 			if (err) {
20782796Smarks 				ZFS_EXIT(zfsvfs);
20792796Smarks 				return (err);
20802796Smarks 			}
20812796Smarks 			trim_mask |= AT_MODE;
20822796Smarks 		} else {
20832796Smarks 			need_policy = TRUE;
20842796Smarks 		}
20852796Smarks 	}
2086789Sahrens 
2087789Sahrens 	if (need_policy) {
20881115Smarks 		/*
20891115Smarks 		 * If trim_mask is set then take ownership
20902796Smarks 		 * has been granted or write_acl is present and user
20912796Smarks 		 * has the ability to modify mode.  In that case remove
20922796Smarks 		 * UID|GID and or MODE from mask so that
20931115Smarks 		 * secpolicy_vnode_setattr() doesn't revoke it.
20941115Smarks 		 */
20952796Smarks 
20962796Smarks 		if (trim_mask) {
20972796Smarks 			saved_mask = vap->va_mask;
20982796Smarks 			vap->va_mask &= ~trim_mask;
20992796Smarks 
21002796Smarks 		}
2101789Sahrens 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2102789Sahrens 		    (int (*)(void *, int, cred_t *))zfs_zaccess_rwx, zp);
2103789Sahrens 		if (err) {
2104789Sahrens 			ZFS_EXIT(zfsvfs);
2105789Sahrens 			return (err);
2106789Sahrens 		}
21071115Smarks 
21081115Smarks 		if (trim_mask)
21092796Smarks 			vap->va_mask |= saved_mask;
2110789Sahrens 	}
2111789Sahrens 
2112789Sahrens 	/*
2113789Sahrens 	 * secpolicy_vnode_setattr, or take ownership may have
2114789Sahrens 	 * changed va_mask
2115789Sahrens 	 */
2116789Sahrens 	mask = vap->va_mask;
2117789Sahrens 
2118789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2119789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
2120789Sahrens 
2121789Sahrens 	if (mask & AT_MODE) {
21221576Smarks 		uint64_t pmode = pzp->zp_mode;
21231576Smarks 
21241576Smarks 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2125789Sahrens 
2126789Sahrens 		if (zp->z_phys->zp_acl.z_acl_extern_obj)
2127789Sahrens 			dmu_tx_hold_write(tx,
2128789Sahrens 			    pzp->zp_acl.z_acl_extern_obj, 0, SPA_MAXBLOCKSIZE);
2129789Sahrens 		else
2130789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2131789Sahrens 			    0, ZFS_ACL_SIZE(MAX_ACL_SIZE));
2132789Sahrens 	}
2133789Sahrens 
21341231Smarks 	if ((mask & (AT_UID | AT_GID)) && zp->z_phys->zp_xattr != 0) {
21351231Smarks 		err = zfs_zget(zp->z_zfsvfs, zp->z_phys->zp_xattr, &attrzp);
21361231Smarks 		if (err) {
21371231Smarks 			dmu_tx_abort(tx);
21381231Smarks 			ZFS_EXIT(zfsvfs);
21391231Smarks 			return (err);
21401231Smarks 		}
21411231Smarks 		dmu_tx_hold_bonus(tx, attrzp->z_id);
21421231Smarks 	}
21431231Smarks 
2144789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2145789Sahrens 	if (err) {
21461231Smarks 		if (attrzp)
21471231Smarks 			VN_RELE(ZTOV(attrzp));
2148789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
21492113Sahrens 			dmu_tx_wait(tx);
21502113Sahrens 			dmu_tx_abort(tx);
2151789Sahrens 			goto top;
2152789Sahrens 		}
21532113Sahrens 		dmu_tx_abort(tx);
2154789Sahrens 		ZFS_EXIT(zfsvfs);
2155789Sahrens 		return (err);
2156789Sahrens 	}
2157789Sahrens 
2158789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
2159789Sahrens 
2160789Sahrens 	/*
2161789Sahrens 	 * Set each attribute requested.
2162789Sahrens 	 * We group settings according to the locks they need to acquire.
2163789Sahrens 	 *
2164789Sahrens 	 * Note: you cannot set ctime directly, although it will be
2165789Sahrens 	 * updated as a side-effect of calling this function.
2166789Sahrens 	 */
2167789Sahrens 
2168789Sahrens 	mutex_enter(&zp->z_lock);
2169789Sahrens 
2170789Sahrens 	if (mask & AT_MODE) {
2171789Sahrens 		err = zfs_acl_chmod_setattr(zp, new_mode, tx);
2172789Sahrens 		ASSERT3U(err, ==, 0);
2173789Sahrens 	}
2174789Sahrens 
21751231Smarks 	if (attrzp)
21761231Smarks 		mutex_enter(&attrzp->z_lock);
21771231Smarks 
21781231Smarks 	if (mask & AT_UID) {
2179789Sahrens 		zp->z_phys->zp_uid = (uint64_t)vap->va_uid;
21801231Smarks 		if (attrzp) {
21811231Smarks 			attrzp->z_phys->zp_uid = (uint64_t)vap->va_uid;
21821231Smarks 		}
21831231Smarks 	}
21841231Smarks 
21851231Smarks 	if (mask & AT_GID) {
2186789Sahrens 		zp->z_phys->zp_gid = (uint64_t)vap->va_gid;
21871231Smarks 		if (attrzp)
21881231Smarks 			attrzp->z_phys->zp_gid = (uint64_t)vap->va_gid;
21891231Smarks 	}
21901231Smarks 
21911231Smarks 	if (attrzp)
21921231Smarks 		mutex_exit(&attrzp->z_lock);
2193789Sahrens 
2194789Sahrens 	if (mask & AT_ATIME)
2195789Sahrens 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
2196789Sahrens 
2197789Sahrens 	if (mask & AT_MTIME)
2198789Sahrens 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
2199789Sahrens 
22001878Smaybee 	if (mask & AT_SIZE)
2201789Sahrens 		zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
22021878Smaybee 	else if (mask != 0)
2203789Sahrens 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
2204789Sahrens 
22051878Smaybee 	if (mask != 0)
22062638Sperrin 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask);
2207789Sahrens 
2208789Sahrens 	mutex_exit(&zp->z_lock);
2209789Sahrens 
22101231Smarks 	if (attrzp)
22111231Smarks 		VN_RELE(ZTOV(attrzp));
22121231Smarks 
2213789Sahrens 	dmu_tx_commit(tx);
2214789Sahrens 
2215789Sahrens 	ZFS_EXIT(zfsvfs);
2216789Sahrens 	return (err);
2217789Sahrens }
2218789Sahrens 
22193271Smaybee typedef struct zfs_zlock {
22203271Smaybee 	krwlock_t	*zl_rwlock;	/* lock we acquired */
22213271Smaybee 	znode_t		*zl_znode;	/* znode we held */
22223271Smaybee 	struct zfs_zlock *zl_next;	/* next in list */
22233271Smaybee } zfs_zlock_t;
22243271Smaybee 
22253271Smaybee /*
22263271Smaybee  * Drop locks and release vnodes that were held by zfs_rename_lock().
22273271Smaybee  */
22283271Smaybee static void
22293271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp)
22303271Smaybee {
22313271Smaybee 	zfs_zlock_t *zl;
22323271Smaybee 
22333271Smaybee 	while ((zl = *zlpp) != NULL) {
22343271Smaybee 		if (zl->zl_znode != NULL)
22353271Smaybee 			VN_RELE(ZTOV(zl->zl_znode));
22363271Smaybee 		rw_exit(zl->zl_rwlock);
22373271Smaybee 		*zlpp = zl->zl_next;
22383271Smaybee 		kmem_free(zl, sizeof (*zl));
22393271Smaybee 	}
22403271Smaybee }
22413271Smaybee 
2242789Sahrens /*
2243789Sahrens  * Search back through the directory tree, using the ".." entries.
2244789Sahrens  * Lock each directory in the chain to prevent concurrent renames.
2245789Sahrens  * Fail any attempt to move a directory into one of its own descendants.
2246789Sahrens  * XXX - z_parent_lock can overlap with map or grow locks
2247789Sahrens  */
2248789Sahrens static int
2249789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
2250789Sahrens {
2251789Sahrens 	zfs_zlock_t	*zl;
2252789Sahrens 	znode_t 	*zp = tdzp;
2253789Sahrens 	uint64_t	rootid = zp->z_zfsvfs->z_root;
2254789Sahrens 	uint64_t	*oidp = &zp->z_id;
2255789Sahrens 	krwlock_t	*rwlp = &szp->z_parent_lock;
2256789Sahrens 	krw_t		rw = RW_WRITER;
2257789Sahrens 
2258789Sahrens 	/*
2259789Sahrens 	 * First pass write-locks szp and compares to zp->z_id.
2260789Sahrens 	 * Later passes read-lock zp and compare to zp->z_parent.
2261789Sahrens 	 */
2262789Sahrens 	do {
22633271Smaybee 		if (!rw_tryenter(rwlp, rw)) {
22643271Smaybee 			/*
22653271Smaybee 			 * Another thread is renaming in this path.
22663271Smaybee 			 * Note that if we are a WRITER, we don't have any
22673271Smaybee 			 * parent_locks held yet.
22683271Smaybee 			 */
22693271Smaybee 			if (rw == RW_READER && zp->z_id > szp->z_id) {
22703271Smaybee 				/*
22713271Smaybee 				 * Drop our locks and restart
22723271Smaybee 				 */
22733271Smaybee 				zfs_rename_unlock(&zl);
22743271Smaybee 				*zlpp = NULL;
22753271Smaybee 				zp = tdzp;
22763271Smaybee 				oidp = &zp->z_id;
22773271Smaybee 				rwlp = &szp->z_parent_lock;
22783271Smaybee 				rw = RW_WRITER;
22793271Smaybee 				continue;
22803271Smaybee 			} else {
22813271Smaybee 				/*
22823271Smaybee 				 * Wait for other thread to drop its locks
22833271Smaybee 				 */
22843271Smaybee 				rw_enter(rwlp, rw);
22853271Smaybee 			}
22863271Smaybee 		}
22873271Smaybee 
2288789Sahrens 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
2289789Sahrens 		zl->zl_rwlock = rwlp;
2290789Sahrens 		zl->zl_znode = NULL;
2291789Sahrens 		zl->zl_next = *zlpp;
2292789Sahrens 		*zlpp = zl;
2293789Sahrens 
2294789Sahrens 		if (*oidp == szp->z_id)		/* We're a descendant of szp */
2295789Sahrens 			return (EINVAL);
2296789Sahrens 
2297789Sahrens 		if (*oidp == rootid)		/* We've hit the top */
2298789Sahrens 			return (0);
2299789Sahrens 
2300789Sahrens 		if (rw == RW_READER) {		/* i.e. not the first pass */
2301789Sahrens 			int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
2302789Sahrens 			if (error)
2303789Sahrens 				return (error);
2304789Sahrens 			zl->zl_znode = zp;
2305789Sahrens 		}
2306789Sahrens 		oidp = &zp->z_phys->zp_parent;
2307789Sahrens 		rwlp = &zp->z_parent_lock;
2308789Sahrens 		rw = RW_READER;
2309789Sahrens 
2310789Sahrens 	} while (zp->z_id != sdzp->z_id);
2311789Sahrens 
2312789Sahrens 	return (0);
2313789Sahrens }
2314789Sahrens 
2315789Sahrens /*
2316789Sahrens  * Move an entry from the provided source directory to the target
2317789Sahrens  * directory.  Change the entry name as indicated.
2318789Sahrens  *
2319789Sahrens  *	IN:	sdvp	- Source directory containing the "old entry".
2320789Sahrens  *		snm	- Old entry name.
2321789Sahrens  *		tdvp	- Target directory to contain the "new entry".
2322789Sahrens  *		tnm	- New entry name.
2323789Sahrens  *		cr	- credentials of caller.
2324789Sahrens  *
2325789Sahrens  *	RETURN:	0 if success
2326789Sahrens  *		error code if failure
2327789Sahrens  *
2328789Sahrens  * Timestamps:
2329789Sahrens  *	sdvp,tdvp - ctime|mtime updated
2330789Sahrens  */
2331789Sahrens static int
2332789Sahrens zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr)
2333789Sahrens {
2334789Sahrens 	znode_t		*tdzp, *szp, *tzp;
2335789Sahrens 	znode_t		*sdzp = VTOZ(sdvp);
2336789Sahrens 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
2337789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2338789Sahrens 	vnode_t		*realvp;
2339789Sahrens 	zfs_dirlock_t	*sdl, *tdl;
2340789Sahrens 	dmu_tx_t	*tx;
2341789Sahrens 	zfs_zlock_t	*zl;
2342789Sahrens 	int		cmp, serr, terr, error;
2343789Sahrens 
2344789Sahrens 	ZFS_ENTER(zfsvfs);
2345789Sahrens 
2346789Sahrens 	/*
2347789Sahrens 	 * Make sure we have the real vp for the target directory.
2348789Sahrens 	 */
2349789Sahrens 	if (VOP_REALVP(tdvp, &realvp) == 0)
2350789Sahrens 		tdvp = realvp;
2351789Sahrens 
2352789Sahrens 	if (tdvp->v_vfsp != sdvp->v_vfsp) {
2353789Sahrens 		ZFS_EXIT(zfsvfs);
2354789Sahrens 		return (EXDEV);
2355789Sahrens 	}
2356789Sahrens 
2357789Sahrens 	tdzp = VTOZ(tdvp);
2358789Sahrens top:
2359789Sahrens 	szp = NULL;
2360789Sahrens 	tzp = NULL;
2361789Sahrens 	zl = NULL;
2362789Sahrens 
2363789Sahrens 	/*
2364789Sahrens 	 * This is to prevent the creation of links into attribute space
2365789Sahrens 	 * by renaming a linked file into/outof an attribute directory.
2366789Sahrens 	 * See the comment in zfs_link() for why this is considered bad.
2367789Sahrens 	 */
2368789Sahrens 	if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
2369789Sahrens 	    (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
2370789Sahrens 		ZFS_EXIT(zfsvfs);
2371789Sahrens 		return (EINVAL);
2372789Sahrens 	}
2373789Sahrens 
2374789Sahrens 	/*
2375789Sahrens 	 * Lock source and target directory entries.  To prevent deadlock,
2376789Sahrens 	 * a lock ordering must be defined.  We lock the directory with
2377789Sahrens 	 * the smallest object id first, or if it's a tie, the one with
2378789Sahrens 	 * the lexically first name.
2379789Sahrens 	 */
2380789Sahrens 	if (sdzp->z_id < tdzp->z_id) {
2381789Sahrens 		cmp = -1;
2382789Sahrens 	} else if (sdzp->z_id > tdzp->z_id) {
2383789Sahrens 		cmp = 1;
2384789Sahrens 	} else {
2385789Sahrens 		cmp = strcmp(snm, tnm);
2386789Sahrens 		if (cmp == 0) {
2387789Sahrens 			/*
2388789Sahrens 			 * POSIX: "If the old argument and the new argument
2389789Sahrens 			 * both refer to links to the same existing file,
2390789Sahrens 			 * the rename() function shall return successfully
2391789Sahrens 			 * and perform no other action."
2392789Sahrens 			 */
2393789Sahrens 			ZFS_EXIT(zfsvfs);
2394789Sahrens 			return (0);
2395789Sahrens 		}
2396789Sahrens 	}
2397789Sahrens 	if (cmp < 0) {
2398789Sahrens 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
2399789Sahrens 		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
2400789Sahrens 	} else {
2401789Sahrens 		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
2402789Sahrens 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
2403789Sahrens 	}
2404789Sahrens 
2405789Sahrens 	if (serr) {
2406789Sahrens 		/*
2407789Sahrens 		 * Source entry invalid or not there.
2408789Sahrens 		 */
2409789Sahrens 		if (!terr) {
2410789Sahrens 			zfs_dirent_unlock(tdl);
2411789Sahrens 			if (tzp)
2412789Sahrens 				VN_RELE(ZTOV(tzp));
2413789Sahrens 		}
2414789Sahrens 		if (strcmp(snm, "..") == 0)
2415789Sahrens 			serr = EINVAL;
2416789Sahrens 		ZFS_EXIT(zfsvfs);
2417789Sahrens 		return (serr);
2418789Sahrens 	}
2419789Sahrens 	if (terr) {
2420789Sahrens 		zfs_dirent_unlock(sdl);
2421789Sahrens 		VN_RELE(ZTOV(szp));
2422789Sahrens 		if (strcmp(tnm, "..") == 0)
2423789Sahrens 			terr = EINVAL;
2424789Sahrens 		ZFS_EXIT(zfsvfs);
2425789Sahrens 		return (terr);
2426789Sahrens 	}
2427789Sahrens 
2428789Sahrens 	/*
2429789Sahrens 	 * Must have write access at the source to remove the old entry
2430789Sahrens 	 * and write access at the target to create the new entry.
2431789Sahrens 	 * Note that if target and source are the same, this can be
2432789Sahrens 	 * done in a single check.
2433789Sahrens 	 */
2434789Sahrens 
2435789Sahrens 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
2436789Sahrens 		goto out;
2437789Sahrens 
2438789Sahrens 	if (ZTOV(szp)->v_type == VDIR) {
2439789Sahrens 		/*
2440789Sahrens 		 * Check to make sure rename is valid.
2441789Sahrens 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
2442789Sahrens 		 */
2443789Sahrens 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
2444789Sahrens 			goto out;
2445789Sahrens 	}
2446789Sahrens 
2447789Sahrens 	/*
2448789Sahrens 	 * Does target exist?
2449789Sahrens 	 */
2450789Sahrens 	if (tzp) {
2451789Sahrens 		/*
2452789Sahrens 		 * Source and target must be the same type.
2453789Sahrens 		 */
2454789Sahrens 		if (ZTOV(szp)->v_type == VDIR) {
2455789Sahrens 			if (ZTOV(tzp)->v_type != VDIR) {
2456789Sahrens 				error = ENOTDIR;
2457789Sahrens 				goto out;
2458789Sahrens 			}
2459789Sahrens 		} else {
2460789Sahrens 			if (ZTOV(tzp)->v_type == VDIR) {
2461789Sahrens 				error = EISDIR;
2462789Sahrens 				goto out;
2463789Sahrens 			}
2464789Sahrens 		}
2465789Sahrens 		/*
2466789Sahrens 		 * POSIX dictates that when the source and target
2467789Sahrens 		 * entries refer to the same file object, rename
2468789Sahrens 		 * must do nothing and exit without error.
2469789Sahrens 		 */
2470789Sahrens 		if (szp->z_id == tzp->z_id) {
2471789Sahrens 			error = 0;
2472789Sahrens 			goto out;
2473789Sahrens 		}
2474789Sahrens 	}
2475789Sahrens 
2476789Sahrens 	vnevent_rename_src(ZTOV(szp));
2477789Sahrens 	if (tzp)
2478789Sahrens 		vnevent_rename_dest(ZTOV(tzp));
2479789Sahrens 
2480789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2481789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);	/* nlink changes */
2482789Sahrens 	dmu_tx_hold_bonus(tx, sdzp->z_id);	/* nlink changes */
24831544Seschrock 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
24841544Seschrock 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
24851544Seschrock 	if (sdzp != tdzp)
2486789Sahrens 		dmu_tx_hold_bonus(tx, tdzp->z_id);	/* nlink changes */
24871544Seschrock 	if (tzp)
24881544Seschrock 		dmu_tx_hold_bonus(tx, tzp->z_id);	/* parent changes */
24891544Seschrock 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
2490789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2491789Sahrens 	if (error) {
2492789Sahrens 		if (zl != NULL)
2493789Sahrens 			zfs_rename_unlock(&zl);
2494789Sahrens 		zfs_dirent_unlock(sdl);
2495789Sahrens 		zfs_dirent_unlock(tdl);
2496789Sahrens 		VN_RELE(ZTOV(szp));
2497789Sahrens 		if (tzp)
2498789Sahrens 			VN_RELE(ZTOV(tzp));
2499789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
25002113Sahrens 			dmu_tx_wait(tx);
25012113Sahrens 			dmu_tx_abort(tx);
2502789Sahrens 			goto top;
2503789Sahrens 		}
25042113Sahrens 		dmu_tx_abort(tx);
2505789Sahrens 		ZFS_EXIT(zfsvfs);
2506789Sahrens 		return (error);
2507789Sahrens 	}
2508789Sahrens 
2509789Sahrens 	if (tzp)	/* Attempt to remove the existing target */
2510789Sahrens 		error = zfs_link_destroy(tdl, tzp, tx, 0, NULL);
2511789Sahrens 
2512789Sahrens 	if (error == 0) {
2513789Sahrens 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
2514789Sahrens 		if (error == 0) {
2515789Sahrens 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
2516789Sahrens 			ASSERT(error == 0);
25172638Sperrin 			zfs_log_rename(zilog, tx, TX_RENAME, sdzp,
25182638Sperrin 			    sdl->dl_name, tdzp, tdl->dl_name, szp);
2519789Sahrens 		}
2520789Sahrens 	}
2521789Sahrens 
2522789Sahrens 	dmu_tx_commit(tx);
2523789Sahrens out:
2524789Sahrens 	if (zl != NULL)
2525789Sahrens 		zfs_rename_unlock(&zl);
2526789Sahrens 
2527789Sahrens 	zfs_dirent_unlock(sdl);
2528789Sahrens 	zfs_dirent_unlock(tdl);
2529789Sahrens 
2530789Sahrens 	VN_RELE(ZTOV(szp));
2531789Sahrens 	if (tzp)
2532789Sahrens 		VN_RELE(ZTOV(tzp));
2533789Sahrens 
2534789Sahrens 	ZFS_EXIT(zfsvfs);
2535789Sahrens 	return (error);
2536789Sahrens }
2537789Sahrens 
2538789Sahrens /*
2539789Sahrens  * Insert the indicated symbolic reference entry into the directory.
2540789Sahrens  *
2541789Sahrens  *	IN:	dvp	- Directory to contain new symbolic link.
2542789Sahrens  *		link	- Name for new symlink entry.
2543789Sahrens  *		vap	- Attributes of new entry.
2544789Sahrens  *		target	- Target path of new symlink.
2545789Sahrens  *		cr	- credentials of caller.
2546789Sahrens  *
2547789Sahrens  *	RETURN:	0 if success
2548789Sahrens  *		error code if failure
2549789Sahrens  *
2550789Sahrens  * Timestamps:
2551789Sahrens  *	dvp - ctime|mtime updated
2552789Sahrens  */
2553789Sahrens static int
2554789Sahrens zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr)
2555789Sahrens {
2556789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
2557789Sahrens 	zfs_dirlock_t	*dl;
2558789Sahrens 	dmu_tx_t	*tx;
2559789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2560789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2561789Sahrens 	uint64_t	zoid;
2562789Sahrens 	int		len = strlen(link);
2563789Sahrens 	int		error;
2564789Sahrens 
2565789Sahrens 	ASSERT(vap->va_type == VLNK);
2566789Sahrens 
2567789Sahrens 	ZFS_ENTER(zfsvfs);
2568789Sahrens top:
2569789Sahrens 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
2570789Sahrens 		ZFS_EXIT(zfsvfs);
2571789Sahrens 		return (error);
2572789Sahrens 	}
2573789Sahrens 
2574789Sahrens 	if (len > MAXPATHLEN) {
2575789Sahrens 		ZFS_EXIT(zfsvfs);
2576789Sahrens 		return (ENAMETOOLONG);
2577789Sahrens 	}
2578789Sahrens 
2579789Sahrens 	/*
2580789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
2581789Sahrens 	 */
2582789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZNEW)) {
2583789Sahrens 		ZFS_EXIT(zfsvfs);
2584789Sahrens 		return (error);
2585789Sahrens 	}
2586789Sahrens 
2587789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2588789Sahrens 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
2589789Sahrens 	dmu_tx_hold_bonus(tx, dzp->z_id);
25901544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
2591789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
2592789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
2593789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2594789Sahrens 	if (error) {
2595789Sahrens 		zfs_dirent_unlock(dl);
2596789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
25972113Sahrens 			dmu_tx_wait(tx);
25982113Sahrens 			dmu_tx_abort(tx);
2599789Sahrens 			goto top;
2600789Sahrens 		}
26012113Sahrens 		dmu_tx_abort(tx);
2602789Sahrens 		ZFS_EXIT(zfsvfs);
2603789Sahrens 		return (error);
2604789Sahrens 	}
2605789Sahrens 
2606789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
2607789Sahrens 
2608789Sahrens 	/*
2609789Sahrens 	 * Create a new object for the symlink.
2610789Sahrens 	 * Put the link content into bonus buffer if it will fit;
2611789Sahrens 	 * otherwise, store it just like any other file data.
2612789Sahrens 	 */
2613789Sahrens 	zoid = 0;
2614789Sahrens 	if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
2615789Sahrens 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, len);
2616789Sahrens 		if (len != 0)
2617789Sahrens 			bcopy(link, zp->z_phys + 1, len);
2618789Sahrens 	} else {
2619789Sahrens 		dmu_buf_t *dbp;
26201669Sperrin 
2621789Sahrens 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
2622789Sahrens 
26231669Sperrin 		/*
26241669Sperrin 		 * Nothing can access the znode yet so no locking needed
26251669Sperrin 		 * for growing the znode's blocksize.
26261669Sperrin 		 */
26271669Sperrin 		zfs_grow_blocksize(zp, len, tx);
2628789Sahrens 
26291544Seschrock 		VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, zoid, 0, FTAG, &dbp));
2630789Sahrens 		dmu_buf_will_dirty(dbp, tx);
2631789Sahrens 
2632789Sahrens 		ASSERT3U(len, <=, dbp->db_size);
2633789Sahrens 		bcopy(link, dbp->db_data, len);
26341544Seschrock 		dmu_buf_rele(dbp, FTAG);
2635789Sahrens 	}
2636789Sahrens 	zp->z_phys->zp_size = len;
2637789Sahrens 
2638789Sahrens 	/*
2639789Sahrens 	 * Insert the new object into the directory.
2640789Sahrens 	 */
2641789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
2642789Sahrens out:
2643789Sahrens 	if (error == 0)
26442638Sperrin 		zfs_log_symlink(zilog, tx, TX_SYMLINK, dzp, zp, name, link);
2645789Sahrens 
2646789Sahrens 	dmu_tx_commit(tx);
2647789Sahrens 
2648789Sahrens 	zfs_dirent_unlock(dl);
2649789Sahrens 
2650789Sahrens 	VN_RELE(ZTOV(zp));
2651789Sahrens 
2652789Sahrens 	ZFS_EXIT(zfsvfs);
2653789Sahrens 	return (error);
2654789Sahrens }
2655789Sahrens 
2656789Sahrens /*
2657789Sahrens  * Return, in the buffer contained in the provided uio structure,
2658789Sahrens  * the symbolic path referred to by vp.
2659789Sahrens  *
2660789Sahrens  *	IN:	vp	- vnode of symbolic link.
2661789Sahrens  *		uoip	- structure to contain the link path.
2662789Sahrens  *		cr	- credentials of caller.
2663789Sahrens  *
2664789Sahrens  *	OUT:	uio	- structure to contain the link path.
2665789Sahrens  *
2666789Sahrens  *	RETURN:	0 if success
2667789Sahrens  *		error code if failure
2668789Sahrens  *
2669789Sahrens  * Timestamps:
2670789Sahrens  *	vp - atime updated
2671789Sahrens  */
2672789Sahrens /* ARGSUSED */
2673789Sahrens static int
2674789Sahrens zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr)
2675789Sahrens {
2676789Sahrens 	znode_t		*zp = VTOZ(vp);
2677789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2678789Sahrens 	size_t		bufsz;
2679789Sahrens 	int		error;
2680789Sahrens 
2681789Sahrens 	ZFS_ENTER(zfsvfs);
2682789Sahrens 
2683789Sahrens 	bufsz = (size_t)zp->z_phys->zp_size;
2684789Sahrens 	if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
2685789Sahrens 		error = uiomove(zp->z_phys + 1,
2686789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
2687789Sahrens 	} else {
26881544Seschrock 		dmu_buf_t *dbp;
26891544Seschrock 		error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
26901544Seschrock 		if (error) {
2691789Sahrens 			ZFS_EXIT(zfsvfs);
2692789Sahrens 			return (error);
2693789Sahrens 		}
2694789Sahrens 		error = uiomove(dbp->db_data,
2695789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
26961544Seschrock 		dmu_buf_rele(dbp, FTAG);
2697789Sahrens 	}
2698789Sahrens 
2699789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2700789Sahrens 	ZFS_EXIT(zfsvfs);
2701789Sahrens 	return (error);
2702789Sahrens }
2703789Sahrens 
2704789Sahrens /*
2705789Sahrens  * Insert a new entry into directory tdvp referencing svp.
2706789Sahrens  *
2707789Sahrens  *	IN:	tdvp	- Directory to contain new entry.
2708789Sahrens  *		svp	- vnode of new entry.
2709789Sahrens  *		name	- name of new entry.
2710789Sahrens  *		cr	- credentials of caller.
2711789Sahrens  *
2712789Sahrens  *	RETURN:	0 if success
2713789Sahrens  *		error code if failure
2714789Sahrens  *
2715789Sahrens  * Timestamps:
2716789Sahrens  *	tdvp - ctime|mtime updated
2717789Sahrens  *	 svp - ctime updated
2718789Sahrens  */
2719789Sahrens /* ARGSUSED */
2720789Sahrens static int
2721789Sahrens zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr)
2722789Sahrens {
2723789Sahrens 	znode_t		*dzp = VTOZ(tdvp);
2724789Sahrens 	znode_t		*tzp, *szp;
2725789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2726789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2727789Sahrens 	zfs_dirlock_t	*dl;
2728789Sahrens 	dmu_tx_t	*tx;
2729789Sahrens 	vnode_t		*realvp;
2730789Sahrens 	int		error;
2731789Sahrens 
2732789Sahrens 	ASSERT(tdvp->v_type == VDIR);
2733789Sahrens 
2734789Sahrens 	ZFS_ENTER(zfsvfs);
2735789Sahrens 
2736789Sahrens 	if (VOP_REALVP(svp, &realvp) == 0)
2737789Sahrens 		svp = realvp;
2738789Sahrens 
2739789Sahrens 	if (svp->v_vfsp != tdvp->v_vfsp) {
2740789Sahrens 		ZFS_EXIT(zfsvfs);
2741789Sahrens 		return (EXDEV);
2742789Sahrens 	}
2743789Sahrens 
2744789Sahrens 	szp = VTOZ(svp);
2745789Sahrens top:
2746789Sahrens 	/*
2747789Sahrens 	 * We do not support links between attributes and non-attributes
2748789Sahrens 	 * because of the potential security risk of creating links
2749789Sahrens 	 * into "normal" file space in order to circumvent restrictions
2750789Sahrens 	 * imposed in attribute space.
2751789Sahrens 	 */
2752789Sahrens 	if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
2753789Sahrens 	    (dzp->z_phys->zp_flags & ZFS_XATTR)) {
2754789Sahrens 		ZFS_EXIT(zfsvfs);
2755789Sahrens 		return (EINVAL);
2756789Sahrens 	}
2757789Sahrens 
2758789Sahrens 	/*
2759789Sahrens 	 * POSIX dictates that we return EPERM here.
2760789Sahrens 	 * Better choices include ENOTSUP or EISDIR.
2761789Sahrens 	 */
2762789Sahrens 	if (svp->v_type == VDIR) {
2763789Sahrens 		ZFS_EXIT(zfsvfs);
2764789Sahrens 		return (EPERM);
2765789Sahrens 	}
2766789Sahrens 
2767789Sahrens 	if ((uid_t)szp->z_phys->zp_uid != crgetuid(cr) &&
2768789Sahrens 	    secpolicy_basic_link(cr) != 0) {
2769789Sahrens 		ZFS_EXIT(zfsvfs);
2770789Sahrens 		return (EPERM);
2771789Sahrens 	}
2772789Sahrens 
2773789Sahrens 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
2774789Sahrens 		ZFS_EXIT(zfsvfs);
2775789Sahrens 		return (error);
2776789Sahrens 	}
2777789Sahrens 
2778789Sahrens 	/*
2779789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
2780789Sahrens 	 */
2781789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &tzp, ZNEW)) {
2782789Sahrens 		ZFS_EXIT(zfsvfs);
2783789Sahrens 		return (error);
2784789Sahrens 	}
2785789Sahrens 
2786789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2787789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);
27881544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
2789789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2790789Sahrens 	if (error) {
2791789Sahrens 		zfs_dirent_unlock(dl);
2792789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
27932113Sahrens 			dmu_tx_wait(tx);
27942113Sahrens 			dmu_tx_abort(tx);
2795789Sahrens 			goto top;
2796789Sahrens 		}
27972113Sahrens 		dmu_tx_abort(tx);
2798789Sahrens 		ZFS_EXIT(zfsvfs);
2799789Sahrens 		return (error);
2800789Sahrens 	}
2801789Sahrens 
2802789Sahrens 	error = zfs_link_create(dl, szp, tx, 0);
2803789Sahrens 
2804789Sahrens 	if (error == 0)
28052638Sperrin 		zfs_log_link(zilog, tx, TX_LINK, dzp, szp, name);
2806789Sahrens 
2807789Sahrens 	dmu_tx_commit(tx);
2808789Sahrens 
2809789Sahrens 	zfs_dirent_unlock(dl);
2810789Sahrens 
2811789Sahrens 	ZFS_EXIT(zfsvfs);
2812789Sahrens 	return (error);
2813789Sahrens }
2814789Sahrens 
2815789Sahrens /*
2816789Sahrens  * zfs_null_putapage() is used when the file system has been force
2817789Sahrens  * unmounted. It just drops the pages.
2818789Sahrens  */
2819789Sahrens /* ARGSUSED */
2820789Sahrens static int
2821789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
2822789Sahrens 		size_t *lenp, int flags, cred_t *cr)
2823789Sahrens {
2824789Sahrens 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
2825789Sahrens 	return (0);
2826789Sahrens }
2827789Sahrens 
28282688Smaybee /*
28292688Smaybee  * Push a page out to disk, klustering if possible.
28302688Smaybee  *
28312688Smaybee  *	IN:	vp	- file to push page to.
28322688Smaybee  *		pp	- page to push.
28332688Smaybee  *		flags	- additional flags.
28342688Smaybee  *		cr	- credentials of caller.
28352688Smaybee  *
28362688Smaybee  *	OUT:	offp	- start of range pushed.
28372688Smaybee  *		lenp	- len of range pushed.
28382688Smaybee  *
28392688Smaybee  *	RETURN:	0 if success
28402688Smaybee  *		error code if failure
28412688Smaybee  *
28422688Smaybee  * NOTE: callers must have locked the page to be pushed.  On
28432688Smaybee  * exit, the page (and all other pages in the kluster) must be
28442688Smaybee  * unlocked.
28452688Smaybee  */
2846789Sahrens /* ARGSUSED */
2847789Sahrens static int
2848789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
2849789Sahrens 		size_t *lenp, int flags, cred_t *cr)
2850789Sahrens {
2851789Sahrens 	znode_t		*zp = VTOZ(vp);
2852789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2853789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2854789Sahrens 	dmu_tx_t	*tx;
28551669Sperrin 	rl_t		*rl;
28562688Smaybee 	u_offset_t	off, koff;
28572688Smaybee 	size_t		len, klen;
2858789Sahrens 	int		err;
2859789Sahrens 
28602688Smaybee 	off = pp->p_offset;
28612688Smaybee 	len = PAGESIZE;
28622688Smaybee 	/*
28632688Smaybee 	 * If our blocksize is bigger than the page size, try to kluster
28642688Smaybee 	 * muiltiple pages so that we write a full block (thus avoiding
28652688Smaybee 	 * a read-modify-write).
28662688Smaybee 	 */
28672688Smaybee 	if (zp->z_blksz > PAGESIZE) {
28682688Smaybee 		uint64_t filesz = zp->z_phys->zp_size;
28692688Smaybee 
28702688Smaybee 		if (!ISP2(zp->z_blksz)) {
28712688Smaybee 			/* Only one block in the file. */
28722688Smaybee 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
28732688Smaybee 			koff = 0;
28742688Smaybee 		} else {
28752688Smaybee 			klen = zp->z_blksz;
28762688Smaybee 			koff = P2ALIGN(off, (u_offset_t)klen);
28772688Smaybee 		}
28782688Smaybee 		ASSERT(koff <= filesz);
28792688Smaybee 		if (koff + klen > filesz)
28802688Smaybee 			klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE);
28812688Smaybee 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
28822688Smaybee 	}
28832688Smaybee 	ASSERT3U(btop(len), ==, btopr(len));
2884789Sahrens top:
28852688Smaybee 	rl = zfs_range_lock(zp, off, len, RL_WRITER);
28861819Smaybee 	/*
28871819Smaybee 	 * Can't push pages past end-of-file.
28881819Smaybee 	 */
28891819Smaybee 	if (off >= zp->z_phys->zp_size) {
28902688Smaybee 		/* discard all pages */
28912688Smaybee 		flags |= B_INVAL;
28922688Smaybee 		err = 0;
28932688Smaybee 		goto out;
28942688Smaybee 	} else if (off + len > zp->z_phys->zp_size) {
28952688Smaybee 		int npages = btopr(zp->z_phys->zp_size - off);
28962688Smaybee 		page_t *trunc;
28972688Smaybee 
28982688Smaybee 		page_list_break(&pp, &trunc, npages);
28992688Smaybee 		/* discard pages past end of file */
29002688Smaybee 		if (trunc)
29012688Smaybee 			pvn_write_done(trunc, B_INVAL | flags);
29022688Smaybee 		len = zp->z_phys->zp_size - off;
29031819Smaybee 	}
2904789Sahrens 
2905789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2906789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, off, len);
2907789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
2908789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2909789Sahrens 	if (err != 0) {
2910789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
29112688Smaybee 			zfs_range_unlock(rl);
29122113Sahrens 			dmu_tx_wait(tx);
29132113Sahrens 			dmu_tx_abort(tx);
29142688Smaybee 			err = 0;
2915789Sahrens 			goto top;
2916789Sahrens 		}
29172113Sahrens 		dmu_tx_abort(tx);
2918789Sahrens 		goto out;
2919789Sahrens 	}
2920789Sahrens 
29212688Smaybee 	if (zp->z_blksz <= PAGESIZE) {
29222688Smaybee 		caddr_t va = ppmapin(pp, PROT_READ, (caddr_t)-1);
29232688Smaybee 		ASSERT3U(len, <=, PAGESIZE);
29242688Smaybee 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
29252688Smaybee 		ppmapout(va);
29262688Smaybee 	} else {
29272688Smaybee 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
29282688Smaybee 	}
29292688Smaybee 
29302688Smaybee 	if (err == 0) {
29312688Smaybee 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
29322688Smaybee 		(void) zfs_log_write(
29332688Smaybee 		    zilog, tx, TX_WRITE, zp, off, len, 0, NULL);
29342688Smaybee 		dmu_tx_commit(tx);
29352688Smaybee 	}
29362688Smaybee 
29372688Smaybee out:
29382237Smaybee 	zfs_range_unlock(rl);
29392688Smaybee 	pvn_write_done(pp, (err ? B_ERROR : 0) | B_WRITE | flags);
2940789Sahrens 	if (offp)
2941789Sahrens 		*offp = off;
2942789Sahrens 	if (lenp)
2943789Sahrens 		*lenp = len;
2944789Sahrens 
2945789Sahrens 	return (err);
2946789Sahrens }
2947789Sahrens 
2948789Sahrens /*
2949789Sahrens  * Copy the portion of the file indicated from pages into the file.
2950789Sahrens  * The pages are stored in a page list attached to the files vnode.
2951789Sahrens  *
2952789Sahrens  *	IN:	vp	- vnode of file to push page data to.
2953789Sahrens  *		off	- position in file to put data.
2954789Sahrens  *		len	- amount of data to write.
2955789Sahrens  *		flags	- flags to control the operation.
2956789Sahrens  *		cr	- credentials of caller.
2957789Sahrens  *
2958789Sahrens  *	RETURN:	0 if success
2959789Sahrens  *		error code if failure
2960789Sahrens  *
2961789Sahrens  * Timestamps:
2962789Sahrens  *	vp - ctime|mtime updated
2963789Sahrens  */
2964789Sahrens static int
2965789Sahrens zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr)
2966789Sahrens {
2967789Sahrens 	znode_t		*zp = VTOZ(vp);
2968789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2969789Sahrens 	page_t		*pp;
2970789Sahrens 	size_t		io_len;
2971789Sahrens 	u_offset_t	io_off;
29721669Sperrin 	uint64_t	filesz;
2973789Sahrens 	int		error = 0;
2974789Sahrens 
2975789Sahrens 	ZFS_ENTER(zfsvfs);
2976789Sahrens 
2977789Sahrens 	ASSERT(zp->z_dbuf_held && zp->z_phys);
2978789Sahrens 
2979789Sahrens 	if (len == 0) {
2980789Sahrens 		/*
2981789Sahrens 		 * Search the entire vp list for pages >= off.
2982789Sahrens 		 */
2983789Sahrens 		error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage,
2984789Sahrens 		    flags, cr);
29851472Sperrin 		goto out;
2986789Sahrens 	}
2987789Sahrens 
29881669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
29891669Sperrin 	if (off > filesz) {
2990789Sahrens 		/* past end of file */
2991789Sahrens 		ZFS_EXIT(zfsvfs);
2992789Sahrens 		return (0);
2993789Sahrens 	}
2994789Sahrens 
29951669Sperrin 	len = MIN(len, filesz - off);
2996789Sahrens 
29971472Sperrin 	for (io_off = off; io_off < off + len; io_off += io_len) {
2998789Sahrens 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
29991669Sperrin 			pp = page_lookup(vp, io_off,
3000789Sahrens 				(flags & (B_INVAL | B_FREE)) ?
3001789Sahrens 					SE_EXCL : SE_SHARED);
3002789Sahrens 		} else {
3003789Sahrens 			pp = page_lookup_nowait(vp, io_off,
3004789Sahrens 				(flags & B_FREE) ? SE_EXCL : SE_SHARED);
3005789Sahrens 		}
3006789Sahrens 
3007789Sahrens 		if (pp != NULL && pvn_getdirty(pp, flags)) {
3008789Sahrens 			int err;
3009789Sahrens 
3010789Sahrens 			/*
3011789Sahrens 			 * Found a dirty page to push
3012789Sahrens 			 */
30131669Sperrin 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
30141669Sperrin 			if (err)
3015789Sahrens 				error = err;
3016789Sahrens 		} else {
3017789Sahrens 			io_len = PAGESIZE;
3018789Sahrens 		}
3019789Sahrens 	}
30201472Sperrin out:
30212638Sperrin 	if ((flags & B_ASYNC) == 0)
30222638Sperrin 		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
3023789Sahrens 	ZFS_EXIT(zfsvfs);
3024789Sahrens 	return (error);
3025789Sahrens }
3026789Sahrens 
3027789Sahrens void
3028789Sahrens zfs_inactive(vnode_t *vp, cred_t *cr)
3029789Sahrens {
3030789Sahrens 	znode_t	*zp = VTOZ(vp);
3031789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3032789Sahrens 	int error;
3033789Sahrens 
3034789Sahrens 	rw_enter(&zfsvfs->z_um_lock, RW_READER);
3035789Sahrens 	if (zfsvfs->z_unmounted2) {
3036789Sahrens 		ASSERT(zp->z_dbuf_held == 0);
3037789Sahrens 
3038789Sahrens 		if (vn_has_cached_data(vp)) {
3039789Sahrens 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
3040789Sahrens 			    B_INVAL, cr);
3041789Sahrens 		}
3042789Sahrens 
30431544Seschrock 		mutex_enter(&zp->z_lock);
3044789Sahrens 		vp->v_count = 0; /* count arrives as 1 */
30451544Seschrock 		if (zp->z_dbuf == NULL) {
30461544Seschrock 			mutex_exit(&zp->z_lock);
30471544Seschrock 			zfs_znode_free(zp);
30481544Seschrock 		} else {
30491544Seschrock 			mutex_exit(&zp->z_lock);
30501544Seschrock 		}
3051789Sahrens 		rw_exit(&zfsvfs->z_um_lock);
3052789Sahrens 		VFS_RELE(zfsvfs->z_vfs);
3053789Sahrens 		return;
3054789Sahrens 	}
3055789Sahrens 
3056789Sahrens 	/*
3057789Sahrens 	 * Attempt to push any data in the page cache.  If this fails
3058789Sahrens 	 * we will get kicked out later in zfs_zinactive().
3059789Sahrens 	 */
30601298Sperrin 	if (vn_has_cached_data(vp)) {
30611298Sperrin 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
30621298Sperrin 		    cr);
30631298Sperrin 	}
3064789Sahrens 
3065789Sahrens 	if (zp->z_atime_dirty && zp->z_reap == 0) {
3066789Sahrens 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3067789Sahrens 
3068789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
3069789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
3070789Sahrens 		if (error) {
3071789Sahrens 			dmu_tx_abort(tx);
3072789Sahrens 		} else {
3073789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
3074789Sahrens 			mutex_enter(&zp->z_lock);
3075789Sahrens 			zp->z_atime_dirty = 0;
3076789Sahrens 			mutex_exit(&zp->z_lock);
3077789Sahrens 			dmu_tx_commit(tx);
3078789Sahrens 		}
3079789Sahrens 	}
3080789Sahrens 
3081789Sahrens 	zfs_zinactive(zp);
3082789Sahrens 	rw_exit(&zfsvfs->z_um_lock);
3083789Sahrens }
3084789Sahrens 
3085789Sahrens /*
3086789Sahrens  * Bounds-check the seek operation.
3087789Sahrens  *
3088789Sahrens  *	IN:	vp	- vnode seeking within
3089789Sahrens  *		ooff	- old file offset
3090789Sahrens  *		noffp	- pointer to new file offset
3091789Sahrens  *
3092789Sahrens  *	RETURN:	0 if success
3093789Sahrens  *		EINVAL if new offset invalid
3094789Sahrens  */
3095789Sahrens /* ARGSUSED */
3096789Sahrens static int
3097789Sahrens zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp)
3098789Sahrens {
3099789Sahrens 	if (vp->v_type == VDIR)
3100789Sahrens 		return (0);
3101789Sahrens 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
3102789Sahrens }
3103789Sahrens 
3104789Sahrens /*
3105789Sahrens  * Pre-filter the generic locking function to trap attempts to place
3106789Sahrens  * a mandatory lock on a memory mapped file.
3107789Sahrens  */
3108789Sahrens static int
3109789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
3110789Sahrens     flk_callback_t *flk_cbp, cred_t *cr)
3111789Sahrens {
3112789Sahrens 	znode_t *zp = VTOZ(vp);
3113789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3114789Sahrens 	int error;
3115789Sahrens 
3116789Sahrens 	ZFS_ENTER(zfsvfs);
3117789Sahrens 
3118789Sahrens 	/*
31191544Seschrock 	 * We are following the UFS semantics with respect to mapcnt
31201544Seschrock 	 * here: If we see that the file is mapped already, then we will
31211544Seschrock 	 * return an error, but we don't worry about races between this
31221544Seschrock 	 * function and zfs_map().
3123789Sahrens 	 */
31241544Seschrock 	if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) {
3125789Sahrens 		ZFS_EXIT(zfsvfs);
3126789Sahrens 		return (EAGAIN);
3127789Sahrens 	}
3128789Sahrens 	error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr);
3129789Sahrens 	ZFS_EXIT(zfsvfs);
3130789Sahrens 	return (error);
3131789Sahrens }
3132789Sahrens 
3133789Sahrens /*
3134789Sahrens  * If we can't find a page in the cache, we will create a new page
3135789Sahrens  * and fill it with file data.  For efficiency, we may try to fill
31361669Sperrin  * multiple pages at once (klustering).
3137789Sahrens  */
3138789Sahrens static int
3139789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
3140789Sahrens     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
3141789Sahrens {
3142789Sahrens 	znode_t *zp = VTOZ(vp);
3143789Sahrens 	page_t *pp, *cur_pp;
3144789Sahrens 	objset_t *os = zp->z_zfsvfs->z_os;
3145789Sahrens 	caddr_t va;
3146789Sahrens 	u_offset_t io_off, total;
3147789Sahrens 	uint64_t oid = zp->z_id;
3148789Sahrens 	size_t io_len;
31491669Sperrin 	uint64_t filesz;
3150789Sahrens 	int err;
3151789Sahrens 
3152789Sahrens 	/*
3153789Sahrens 	 * If we are only asking for a single page don't bother klustering.
3154789Sahrens 	 */
31551669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
31562688Smaybee 	if (off >= filesz)
31572688Smaybee 		return (EFAULT);
31582688Smaybee 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
3159789Sahrens 		io_off = off;
3160789Sahrens 		io_len = PAGESIZE;
3161789Sahrens 		pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr);
3162789Sahrens 	} else {
3163789Sahrens 		/*
3164789Sahrens 		 * Try to fill a kluster of pages (a blocks worth).
3165789Sahrens 		 */
3166789Sahrens 		size_t klen;
3167789Sahrens 		u_offset_t koff;
3168789Sahrens 
3169789Sahrens 		if (!ISP2(zp->z_blksz)) {
3170789Sahrens 			/* Only one block in the file. */
3171789Sahrens 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
3172789Sahrens 			koff = 0;
3173789Sahrens 		} else {
31743131Sgw25295 			/*
31753131Sgw25295 			 * It would be ideal to align our offset to the
31763131Sgw25295 			 * blocksize but doing so has resulted in some
31773131Sgw25295 			 * strange application crashes. For now, we
31783131Sgw25295 			 * leave the offset as is and only adjust the
31793131Sgw25295 			 * length if we are off the end of the file.
31803131Sgw25295 			 */
31813131Sgw25295 			koff = off;
3182789Sahrens 			klen = plsz;
3183789Sahrens 		}
31841819Smaybee 		ASSERT(koff <= filesz);
31851819Smaybee 		if (koff + klen > filesz)
31861819Smaybee 			klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff;
31872688Smaybee 		ASSERT3U(off, >=, koff);
31882688Smaybee 		ASSERT3U(off, <, koff + klen);
3189789Sahrens 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
3190789Sahrens 			    &io_len, koff, klen, 0);
3191789Sahrens 	}
3192789Sahrens 	if (pp == NULL) {
3193789Sahrens 		/*
3194789Sahrens 		 * Some other thread entered the page before us.
3195789Sahrens 		 * Return to zfs_getpage to retry the lookup.
3196789Sahrens 		 */
3197789Sahrens 		*pl = NULL;
3198789Sahrens 		return (0);
3199789Sahrens 	}
3200789Sahrens 
3201789Sahrens 	/*
3202789Sahrens 	 * Fill the pages in the kluster.
3203789Sahrens 	 */
3204789Sahrens 	cur_pp = pp;
3205789Sahrens 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
32062688Smaybee 		ASSERT3U(io_off, ==, cur_pp->p_offset);
3207789Sahrens 		va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1);
32081544Seschrock 		err = dmu_read(os, oid, io_off, PAGESIZE, va);
3209789Sahrens 		ppmapout(va);
3210789Sahrens 		if (err) {
3211789Sahrens 			/* On error, toss the entire kluster */
3212789Sahrens 			pvn_read_done(pp, B_ERROR);
3213789Sahrens 			return (err);
3214789Sahrens 		}
3215789Sahrens 		cur_pp = cur_pp->p_next;
3216789Sahrens 	}
3217789Sahrens out:
3218789Sahrens 	/*
3219789Sahrens 	 * Fill in the page list array from the kluster.  If
3220789Sahrens 	 * there are too many pages in the kluster, return
3221789Sahrens 	 * as many pages as possible starting from the desired
3222789Sahrens 	 * offset `off'.
3223789Sahrens 	 * NOTE: the page list will always be null terminated.
3224789Sahrens 	 */
3225789Sahrens 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
3226789Sahrens 
3227789Sahrens 	return (0);
3228789Sahrens }
3229789Sahrens 
3230789Sahrens /*
3231789Sahrens  * Return pointers to the pages for the file region [off, off + len]
3232789Sahrens  * in the pl array.  If plsz is greater than len, this function may
3233789Sahrens  * also return page pointers from before or after the specified
3234789Sahrens  * region (i.e. some region [off', off' + plsz]).  These additional
3235789Sahrens  * pages are only returned if they are already in the cache, or were
3236789Sahrens  * created as part of a klustered read.
3237789Sahrens  *
3238789Sahrens  *	IN:	vp	- vnode of file to get data from.
3239789Sahrens  *		off	- position in file to get data from.
3240789Sahrens  *		len	- amount of data to retrieve.
3241789Sahrens  *		plsz	- length of provided page list.
3242789Sahrens  *		seg	- segment to obtain pages for.
3243789Sahrens  *		addr	- virtual address of fault.
3244789Sahrens  *		rw	- mode of created pages.
3245789Sahrens  *		cr	- credentials of caller.
3246789Sahrens  *
3247789Sahrens  *	OUT:	protp	- protection mode of created pages.
3248789Sahrens  *		pl	- list of pages created.
3249789Sahrens  *
3250789Sahrens  *	RETURN:	0 if success
3251789Sahrens  *		error code if failure
3252789Sahrens  *
3253789Sahrens  * Timestamps:
3254789Sahrens  *	vp - atime updated
3255789Sahrens  */
3256789Sahrens /* ARGSUSED */
3257789Sahrens static int
3258789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
3259789Sahrens 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
3260789Sahrens 	enum seg_rw rw, cred_t *cr)
3261789Sahrens {
3262789Sahrens 	znode_t		*zp = VTOZ(vp);
3263789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3264789Sahrens 	page_t		*pp, **pl0 = pl;
32652752Sperrin 	int		need_unlock = 0, err = 0;
32662752Sperrin 	offset_t	orig_off;
3267789Sahrens 
3268789Sahrens 	ZFS_ENTER(zfsvfs);
3269789Sahrens 
3270789Sahrens 	if (protp)
3271789Sahrens 		*protp = PROT_ALL;
3272789Sahrens 
3273789Sahrens 	ASSERT(zp->z_dbuf_held && zp->z_phys);
3274789Sahrens 
3275789Sahrens 	/* no faultahead (for now) */
3276789Sahrens 	if (pl == NULL) {
3277789Sahrens 		ZFS_EXIT(zfsvfs);
3278789Sahrens 		return (0);
3279789Sahrens 	}
3280789Sahrens 
3281789Sahrens 	/* can't fault past EOF */
3282789Sahrens 	if (off >= zp->z_phys->zp_size) {
3283789Sahrens 		ZFS_EXIT(zfsvfs);
3284789Sahrens 		return (EFAULT);
3285789Sahrens 	}
32862752Sperrin 	orig_off = off;
3287789Sahrens 
3288789Sahrens 	/*
3289789Sahrens 	 * If we already own the lock, then we must be page faulting
3290789Sahrens 	 * in the middle of a write to this file (i.e., we are writing
3291789Sahrens 	 * to this file using data from a mapped region of the file).
3292789Sahrens 	 */
32932752Sperrin 	if (rw_owner(&zp->z_map_lock) != curthread) {
3294789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
3295789Sahrens 		need_unlock = TRUE;
3296789Sahrens 	}
3297789Sahrens 
3298789Sahrens 	/*
3299789Sahrens 	 * Loop through the requested range [off, off + len] looking
3300789Sahrens 	 * for pages.  If we don't find a page, we will need to create
3301789Sahrens 	 * a new page and fill it with data from the file.
3302789Sahrens 	 */
3303789Sahrens 	while (len > 0) {
3304789Sahrens 		if (plsz < PAGESIZE)
3305789Sahrens 			break;
3306789Sahrens 		if (pp = page_lookup(vp, off, SE_SHARED)) {
3307789Sahrens 			*pl++ = pp;
3308789Sahrens 			off += PAGESIZE;
3309789Sahrens 			addr += PAGESIZE;
3310789Sahrens 			len -= PAGESIZE;
3311789Sahrens 			plsz -= PAGESIZE;
3312789Sahrens 		} else {
3313789Sahrens 			err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw);
33142752Sperrin 			if (err)
33152752Sperrin 				goto out;
3316789Sahrens 			/*
3317789Sahrens 			 * klustering may have changed our region
3318789Sahrens 			 * to be block aligned.
3319789Sahrens 			 */
3320789Sahrens 			if (((pp = *pl) != 0) && (off != pp->p_offset)) {
3321789Sahrens 				int delta = off - pp->p_offset;
3322789Sahrens 				len += delta;
3323789Sahrens 				off -= delta;
3324789Sahrens 				addr -= delta;
3325789Sahrens 			}
3326789Sahrens 			while (*pl) {
3327789Sahrens 				pl++;
3328789Sahrens 				off += PAGESIZE;
3329789Sahrens 				addr += PAGESIZE;
3330789Sahrens 				plsz -= PAGESIZE;
3331789Sahrens 				if (len > PAGESIZE)
3332789Sahrens 					len -= PAGESIZE;
3333789Sahrens 				else
3334789Sahrens 					len = 0;
3335789Sahrens 			}
3336789Sahrens 		}
3337789Sahrens 	}
3338789Sahrens 
3339789Sahrens 	/*
3340789Sahrens 	 * Fill out the page array with any pages already in the cache.
3341789Sahrens 	 */
3342789Sahrens 	while (plsz > 0) {
3343789Sahrens 		pp = page_lookup_nowait(vp, off, SE_SHARED);
3344789Sahrens 		if (pp == NULL)
3345789Sahrens 			break;
3346789Sahrens 		*pl++ = pp;
3347789Sahrens 		off += PAGESIZE;
3348789Sahrens 		plsz -= PAGESIZE;
3349789Sahrens 	}
3350789Sahrens 
3351789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3352789Sahrens out:
33532752Sperrin 	/*
33542752Sperrin 	 * We can't grab the range lock for the page as reader which would
33552752Sperrin 	 * stop truncation as this leads to deadlock. So we need to recheck
33562752Sperrin 	 * the file size.
33572752Sperrin 	 */
33582752Sperrin 	if (orig_off >= zp->z_phys->zp_size)
33592752Sperrin 		err = EFAULT;
33602752Sperrin 	if (err) {
33612752Sperrin 		/*
33622752Sperrin 		 * Release any pages we have previously locked.
33632752Sperrin 		 */
33642752Sperrin 		while (pl > pl0)
33652752Sperrin 			page_unlock(*--pl);
33662752Sperrin 	}
33672752Sperrin 
3368789Sahrens 	*pl = NULL;
3369789Sahrens 
3370789Sahrens 	if (need_unlock)
3371789Sahrens 		rw_exit(&zp->z_map_lock);
3372789Sahrens 
3373789Sahrens 	ZFS_EXIT(zfsvfs);
3374789Sahrens 	return (err);
3375789Sahrens }
3376789Sahrens 
33771544Seschrock /*
33781544Seschrock  * Request a memory map for a section of a file.  This code interacts
33791544Seschrock  * with common code and the VM system as follows:
33801544Seschrock  *
33811544Seschrock  *	common code calls mmap(), which ends up in smmap_common()
33821544Seschrock  *
33831544Seschrock  *	this calls VOP_MAP(), which takes you into (say) zfs
33841544Seschrock  *
33851544Seschrock  *	zfs_map() calls as_map(), passing segvn_create() as the callback
33861544Seschrock  *
33871544Seschrock  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
33881544Seschrock  *
33891544Seschrock  *	zfs_addmap() updates z_mapcnt
33901544Seschrock  */
3391789Sahrens static int
3392789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
3393789Sahrens     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
3394789Sahrens {
3395789Sahrens 	znode_t *zp = VTOZ(vp);
3396789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3397789Sahrens 	segvn_crargs_t	vn_a;
3398789Sahrens 	int		error;
3399789Sahrens 
3400789Sahrens 	ZFS_ENTER(zfsvfs);
3401789Sahrens 
3402789Sahrens 	if (vp->v_flag & VNOMAP) {
3403789Sahrens 		ZFS_EXIT(zfsvfs);
3404789Sahrens 		return (ENOSYS);
3405789Sahrens 	}
3406789Sahrens 
3407789Sahrens 	if (off < 0 || len > MAXOFFSET_T - off) {
3408789Sahrens 		ZFS_EXIT(zfsvfs);
3409789Sahrens 		return (ENXIO);
3410789Sahrens 	}
3411789Sahrens 
3412789Sahrens 	if (vp->v_type != VREG) {
3413789Sahrens 		ZFS_EXIT(zfsvfs);
3414789Sahrens 		return (ENODEV);
3415789Sahrens 	}
3416789Sahrens 
3417789Sahrens 	/*
3418789Sahrens 	 * If file is locked, disallow mapping.
3419789Sahrens 	 */
34201544Seschrock 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) {
34211544Seschrock 		ZFS_EXIT(zfsvfs);
34221544Seschrock 		return (EAGAIN);
3423789Sahrens 	}
3424789Sahrens 
3425789Sahrens 	as_rangelock(as);
3426789Sahrens 	if ((flags & MAP_FIXED) == 0) {
3427789Sahrens 		map_addr(addrp, len, off, 1, flags);
3428789Sahrens 		if (*addrp == NULL) {
3429789Sahrens 			as_rangeunlock(as);
3430789Sahrens 			ZFS_EXIT(zfsvfs);
3431789Sahrens 			return (ENOMEM);
3432789Sahrens 		}
3433789Sahrens 	} else {
3434789Sahrens 		/*
3435789Sahrens 		 * User specified address - blow away any previous mappings
3436789Sahrens 		 */
3437789Sahrens 		(void) as_unmap(as, *addrp, len);
3438789Sahrens 	}
3439789Sahrens 
3440789Sahrens 	vn_a.vp = vp;
3441789Sahrens 	vn_a.offset = (u_offset_t)off;
3442789Sahrens 	vn_a.type = flags & MAP_TYPE;
3443789Sahrens 	vn_a.prot = prot;
3444789Sahrens 	vn_a.maxprot = maxprot;
3445789Sahrens 	vn_a.cred = cr;
3446789Sahrens 	vn_a.amp = NULL;
3447789Sahrens 	vn_a.flags = flags & ~MAP_TYPE;
34481417Skchow 	vn_a.szc = 0;
34491417Skchow 	vn_a.lgrp_mem_policy_flags = 0;
3450789Sahrens 
3451789Sahrens 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
3452789Sahrens 
3453789Sahrens 	as_rangeunlock(as);
3454789Sahrens 	ZFS_EXIT(zfsvfs);
3455789Sahrens 	return (error);
3456789Sahrens }
3457789Sahrens 
3458789Sahrens /* ARGSUSED */
3459789Sahrens static int
3460789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
3461789Sahrens     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
3462789Sahrens {
34631544Seschrock 	uint64_t pages = btopr(len);
34641544Seschrock 
34651544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
3466789Sahrens 	return (0);
3467789Sahrens }
3468789Sahrens 
34691773Seschrock /*
34701773Seschrock  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
34711773Seschrock  * more accurate mtime for the associated file.  Since we don't have a way of
34721773Seschrock  * detecting when the data was actually modified, we have to resort to
34731773Seschrock  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
34741773Seschrock  * last page is pushed.  The problem occurs when the msync() call is omitted,
34751773Seschrock  * which by far the most common case:
34761773Seschrock  *
34771773Seschrock  * 	open()
34781773Seschrock  * 	mmap()
34791773Seschrock  * 	<modify memory>
34801773Seschrock  * 	munmap()
34811773Seschrock  * 	close()
34821773Seschrock  * 	<time lapse>
34831773Seschrock  * 	putpage() via fsflush
34841773Seschrock  *
34851773Seschrock  * If we wait until fsflush to come along, we can have a modification time that
34861773Seschrock  * is some arbitrary point in the future.  In order to prevent this in the
34871773Seschrock  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
34881773Seschrock  * torn down.
34891773Seschrock  */
3490789Sahrens /* ARGSUSED */
3491789Sahrens static int
3492789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
3493789Sahrens     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr)
3494789Sahrens {
34951544Seschrock 	uint64_t pages = btopr(len);
34961544Seschrock 
34971544Seschrock 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
34981544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
34991773Seschrock 
35001773Seschrock 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
35011773Seschrock 	    vn_has_cached_data(vp))
35021773Seschrock 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr);
35031773Seschrock 
3504789Sahrens 	return (0);
3505789Sahrens }
3506789Sahrens 
3507789Sahrens /*
3508789Sahrens  * Free or allocate space in a file.  Currently, this function only
3509789Sahrens  * supports the `F_FREESP' command.  However, this command is somewhat
3510789Sahrens  * misnamed, as its functionality includes the ability to allocate as
3511789Sahrens  * well as free space.
3512789Sahrens  *
3513789Sahrens  *	IN:	vp	- vnode of file to free data in.
3514789Sahrens  *		cmd	- action to take (only F_FREESP supported).
3515789Sahrens  *		bfp	- section of file to free/alloc.
3516789Sahrens  *		flag	- current file open mode flags.
3517789Sahrens  *		offset	- current file offset.
3518789Sahrens  *		cr	- credentials of caller [UNUSED].
3519789Sahrens  *
3520789Sahrens  *	RETURN:	0 if success
3521789Sahrens  *		error code if failure
3522789Sahrens  *
3523789Sahrens  * Timestamps:
3524789Sahrens  *	vp - ctime|mtime updated
3525789Sahrens  */
3526789Sahrens /* ARGSUSED */
3527789Sahrens static int
3528789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
3529789Sahrens     offset_t offset, cred_t *cr, caller_context_t *ct)
3530789Sahrens {
3531789Sahrens 	znode_t		*zp = VTOZ(vp);
3532789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3533789Sahrens 	uint64_t	off, len;
3534789Sahrens 	int		error;
3535789Sahrens 
3536789Sahrens 	ZFS_ENTER(zfsvfs);
3537789Sahrens 
3538789Sahrens top:
3539789Sahrens 	if (cmd != F_FREESP) {
3540789Sahrens 		ZFS_EXIT(zfsvfs);
3541789Sahrens 		return (EINVAL);
3542789Sahrens 	}
3543789Sahrens 
3544789Sahrens 	if (error = convoff(vp, bfp, 0, offset)) {
3545789Sahrens 		ZFS_EXIT(zfsvfs);
3546789Sahrens 		return (error);
3547789Sahrens 	}
3548789Sahrens 
3549789Sahrens 	if (bfp->l_len < 0) {
3550789Sahrens 		ZFS_EXIT(zfsvfs);
3551789Sahrens 		return (EINVAL);
3552789Sahrens 	}
3553789Sahrens 
3554789Sahrens 	off = bfp->l_start;
35551669Sperrin 	len = bfp->l_len; /* 0 means from off to end of file */
35561878Smaybee 
35571878Smaybee 	do {
35581878Smaybee 		error = zfs_freesp(zp, off, len, flag, TRUE);
35592113Sahrens 		/* NB: we already did dmu_tx_wait() if necessary */
35601878Smaybee 	} while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
3561789Sahrens 
3562789Sahrens 	ZFS_EXIT(zfsvfs);
3563789Sahrens 	return (error);
3564789Sahrens }
3565789Sahrens 
3566789Sahrens static int
3567789Sahrens zfs_fid(vnode_t *vp, fid_t *fidp)
3568789Sahrens {
3569789Sahrens 	znode_t		*zp = VTOZ(vp);
3570789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3571789Sahrens 	uint32_t	gen = (uint32_t)zp->z_phys->zp_gen;
3572789Sahrens 	uint64_t	object = zp->z_id;
3573789Sahrens 	zfid_short_t	*zfid;
3574789Sahrens 	int		size, i;
3575789Sahrens 
3576789Sahrens 	ZFS_ENTER(zfsvfs);
3577789Sahrens 
3578789Sahrens 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
3579789Sahrens 	if (fidp->fid_len < size) {
3580789Sahrens 		fidp->fid_len = size;
35811512Sek110237 		ZFS_EXIT(zfsvfs);
3582789Sahrens 		return (ENOSPC);
3583789Sahrens 	}
3584789Sahrens 
3585789Sahrens 	zfid = (zfid_short_t *)fidp;
3586789Sahrens 
3587789Sahrens 	zfid->zf_len = size;
3588789Sahrens 
3589789Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
3590789Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
3591789Sahrens 
3592789Sahrens 	/* Must have a non-zero generation number to distinguish from .zfs */
3593789Sahrens 	if (gen == 0)
3594789Sahrens 		gen = 1;
3595789Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
3596789Sahrens 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
3597789Sahrens 
3598789Sahrens 	if (size == LONG_FID_LEN) {
3599789Sahrens 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
3600789Sahrens 		zfid_long_t	*zlfid;
3601789Sahrens 
3602789Sahrens 		zlfid = (zfid_long_t *)fidp;
3603789Sahrens 
3604789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
3605789Sahrens 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
3606789Sahrens 
3607789Sahrens 		/* XXX - this should be the generation number for the objset */
3608789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
3609789Sahrens 			zlfid->zf_setgen[i] = 0;
3610789Sahrens 	}
3611789Sahrens 
3612789Sahrens 	ZFS_EXIT(zfsvfs);
3613789Sahrens 	return (0);
3614789Sahrens }
3615789Sahrens 
3616789Sahrens static int
3617789Sahrens zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr)
3618789Sahrens {
3619789Sahrens 	znode_t		*zp, *xzp;
3620789Sahrens 	zfsvfs_t	*zfsvfs;
3621789Sahrens 	zfs_dirlock_t	*dl;
3622789Sahrens 	int		error;
3623789Sahrens 
3624789Sahrens 	switch (cmd) {
3625789Sahrens 	case _PC_LINK_MAX:
3626789Sahrens 		*valp = ULONG_MAX;
3627789Sahrens 		return (0);
3628789Sahrens 
3629789Sahrens 	case _PC_FILESIZEBITS:
3630789Sahrens 		*valp = 64;
3631789Sahrens 		return (0);
3632789Sahrens 
3633789Sahrens 	case _PC_XATTR_EXISTS:
3634789Sahrens 		zp = VTOZ(vp);
3635789Sahrens 		zfsvfs = zp->z_zfsvfs;
3636789Sahrens 		ZFS_ENTER(zfsvfs);
3637789Sahrens 		*valp = 0;
3638789Sahrens 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
3639789Sahrens 		    ZXATTR | ZEXISTS | ZSHARED);
3640789Sahrens 		if (error == 0) {
3641789Sahrens 			zfs_dirent_unlock(dl);
3642789Sahrens 			if (!zfs_dirempty(xzp))
3643789Sahrens 				*valp = 1;
3644789Sahrens 			VN_RELE(ZTOV(xzp));
3645789Sahrens 		} else if (error == ENOENT) {
3646789Sahrens 			/*
3647789Sahrens 			 * If there aren't extended attributes, it's the
3648789Sahrens 			 * same as having zero of them.
3649789Sahrens 			 */
3650789Sahrens 			error = 0;
3651789Sahrens 		}
3652789Sahrens 		ZFS_EXIT(zfsvfs);
3653789Sahrens 		return (error);
3654789Sahrens 
3655789Sahrens 	case _PC_ACL_ENABLED:
3656789Sahrens 		*valp = _ACL_ACE_ENABLED;
3657789Sahrens 		return (0);
3658789Sahrens 
3659789Sahrens 	case _PC_MIN_HOLE_SIZE:
3660789Sahrens 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
3661789Sahrens 		return (0);
3662789Sahrens 
3663789Sahrens 	default:
3664789Sahrens 		return (fs_pathconf(vp, cmd, valp, cr));
3665789Sahrens 	}
3666789Sahrens }
3667789Sahrens 
3668789Sahrens /*ARGSUSED*/
3669789Sahrens static int
3670789Sahrens zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
3671789Sahrens {
3672789Sahrens 	znode_t *zp = VTOZ(vp);
3673789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3674789Sahrens 	int error;
3675789Sahrens 
3676789Sahrens 	ZFS_ENTER(zfsvfs);
3677789Sahrens 	error = zfs_getacl(zp, vsecp, cr);
3678789Sahrens 	ZFS_EXIT(zfsvfs);
3679789Sahrens 
3680789Sahrens 	return (error);
3681789Sahrens }
3682789Sahrens 
3683789Sahrens /*ARGSUSED*/
3684789Sahrens static int
3685789Sahrens zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
3686789Sahrens {
3687789Sahrens 	znode_t *zp = VTOZ(vp);
3688789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3689789Sahrens 	int error;
3690789Sahrens 
3691789Sahrens 	ZFS_ENTER(zfsvfs);
3692789Sahrens 	error = zfs_setacl(zp, vsecp, cr);
3693789Sahrens 	ZFS_EXIT(zfsvfs);
3694789Sahrens 	return (error);
3695789Sahrens }
3696789Sahrens 
3697789Sahrens /*
3698789Sahrens  * Predeclare these here so that the compiler assumes that
3699789Sahrens  * this is an "old style" function declaration that does
3700789Sahrens  * not include arguments => we won't get type mismatch errors
3701789Sahrens  * in the initializations that follow.
3702789Sahrens  */
3703789Sahrens static int zfs_inval();
3704789Sahrens static int zfs_isdir();
3705789Sahrens 
3706789Sahrens static int
3707789Sahrens zfs_inval()
3708789Sahrens {
3709789Sahrens 	return (EINVAL);
3710789Sahrens }
3711789Sahrens 
3712789Sahrens static int
3713789Sahrens zfs_isdir()
3714789Sahrens {
3715789Sahrens 	return (EISDIR);
3716789Sahrens }
3717789Sahrens /*
3718789Sahrens  * Directory vnode operations template
3719789Sahrens  */
3720789Sahrens vnodeops_t *zfs_dvnodeops;
3721789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = {
3722789Sahrens 	VOPNAME_OPEN, zfs_open,
3723789Sahrens 	VOPNAME_CLOSE, zfs_close,
3724789Sahrens 	VOPNAME_READ, zfs_isdir,
3725789Sahrens 	VOPNAME_WRITE, zfs_isdir,
3726789Sahrens 	VOPNAME_IOCTL, zfs_ioctl,
3727789Sahrens 	VOPNAME_GETATTR, zfs_getattr,
3728789Sahrens 	VOPNAME_SETATTR, zfs_setattr,
3729789Sahrens 	VOPNAME_ACCESS, zfs_access,
3730789Sahrens 	VOPNAME_LOOKUP, zfs_lookup,
3731789Sahrens 	VOPNAME_CREATE, zfs_create,
3732789Sahrens 	VOPNAME_REMOVE, zfs_remove,
3733789Sahrens 	VOPNAME_LINK, zfs_link,
3734789Sahrens 	VOPNAME_RENAME, zfs_rename,
3735789Sahrens 	VOPNAME_MKDIR, zfs_mkdir,
3736789Sahrens 	VOPNAME_RMDIR, zfs_rmdir,
3737789Sahrens 	VOPNAME_READDIR, zfs_readdir,
3738789Sahrens 	VOPNAME_SYMLINK, zfs_symlink,
3739789Sahrens 	VOPNAME_FSYNC, zfs_fsync,
3740789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3741789Sahrens 	VOPNAME_FID, zfs_fid,
3742789Sahrens 	VOPNAME_SEEK, zfs_seek,
3743789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3744789Sahrens 	VOPNAME_GETSECATTR, zfs_getsecattr,
3745789Sahrens 	VOPNAME_SETSECATTR, zfs_setsecattr,
3746789Sahrens 	NULL, NULL
3747789Sahrens };
3748789Sahrens 
3749789Sahrens /*
3750789Sahrens  * Regular file vnode operations template
3751789Sahrens  */
3752789Sahrens vnodeops_t *zfs_fvnodeops;
3753789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = {
3754789Sahrens 	VOPNAME_OPEN, zfs_open,
3755789Sahrens 	VOPNAME_CLOSE, zfs_close,
3756789Sahrens 	VOPNAME_READ, zfs_read,
3757789Sahrens 	VOPNAME_WRITE, zfs_write,
3758789Sahrens 	VOPNAME_IOCTL, zfs_ioctl,
3759789Sahrens 	VOPNAME_GETATTR, zfs_getattr,
3760789Sahrens 	VOPNAME_SETATTR, zfs_setattr,
3761789Sahrens 	VOPNAME_ACCESS, zfs_access,
3762789Sahrens 	VOPNAME_LOOKUP, zfs_lookup,
3763789Sahrens 	VOPNAME_RENAME, zfs_rename,
3764789Sahrens 	VOPNAME_FSYNC, zfs_fsync,
3765789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p)zfs_inactive,
3766789Sahrens 	VOPNAME_FID, zfs_fid,
3767789Sahrens 	VOPNAME_SEEK, zfs_seek,
3768789Sahrens 	VOPNAME_FRLOCK, zfs_frlock,
3769789Sahrens 	VOPNAME_SPACE, zfs_space,
3770789Sahrens 	VOPNAME_GETPAGE, zfs_getpage,
3771789Sahrens 	VOPNAME_PUTPAGE, zfs_putpage,
3772789Sahrens 	VOPNAME_MAP, (fs_generic_func_p) zfs_map,
3773789Sahrens 	VOPNAME_ADDMAP, (fs_generic_func_p) zfs_addmap,
3774789Sahrens 	VOPNAME_DELMAP, zfs_delmap,
3775789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3776789Sahrens 	VOPNAME_GETSECATTR, zfs_getsecattr,
3777789Sahrens 	VOPNAME_SETSECATTR, zfs_setsecattr,
3778789Sahrens 	VOPNAME_VNEVENT, fs_vnevent_support,
3779789Sahrens 	NULL, NULL
3780789Sahrens };
3781789Sahrens 
3782789Sahrens /*
3783789Sahrens  * Symbolic link vnode operations template
3784789Sahrens  */
3785789Sahrens vnodeops_t *zfs_symvnodeops;
3786789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = {
3787789Sahrens 	VOPNAME_GETATTR, zfs_getattr,
3788789Sahrens 	VOPNAME_SETATTR, zfs_setattr,
3789789Sahrens 	VOPNAME_ACCESS, zfs_access,
3790789Sahrens 	VOPNAME_RENAME, zfs_rename,
3791789Sahrens 	VOPNAME_READLINK, zfs_readlink,
3792789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3793789Sahrens 	VOPNAME_FID, zfs_fid,
3794789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3795789Sahrens 	VOPNAME_VNEVENT, fs_vnevent_support,
3796789Sahrens 	NULL, NULL
3797789Sahrens };
3798789Sahrens 
3799789Sahrens /*
3800789Sahrens  * Extended attribute directory vnode operations template
3801789Sahrens  *	This template is identical to the directory vnodes
3802789Sahrens  *	operation template except for restricted operations:
3803789Sahrens  *		VOP_MKDIR()
3804789Sahrens  *		VOP_SYMLINK()
3805789Sahrens  * Note that there are other restrictions embedded in:
3806789Sahrens  *	zfs_create()	- restrict type to VREG
3807789Sahrens  *	zfs_link()	- no links into/out of attribute space
3808789Sahrens  *	zfs_rename()	- no moves into/out of attribute space
3809789Sahrens  */
3810789Sahrens vnodeops_t *zfs_xdvnodeops;
3811789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = {
3812789Sahrens 	VOPNAME_OPEN, zfs_open,
3813789Sahrens 	VOPNAME_CLOSE, zfs_close,
3814789Sahrens 	VOPNAME_IOCTL, zfs_ioctl,
3815789Sahrens 	VOPNAME_GETATTR, zfs_getattr,
3816789Sahrens 	VOPNAME_SETATTR, zfs_setattr,
3817789Sahrens 	VOPNAME_ACCESS, zfs_access,
3818789Sahrens 	VOPNAME_LOOKUP, zfs_lookup,
3819789Sahrens 	VOPNAME_CREATE, zfs_create,
3820789Sahrens 	VOPNAME_REMOVE, zfs_remove,
3821789Sahrens 	VOPNAME_LINK, zfs_link,
3822789Sahrens 	VOPNAME_RENAME, zfs_rename,
3823789Sahrens 	VOPNAME_MKDIR, zfs_inval,
3824789Sahrens 	VOPNAME_RMDIR, zfs_rmdir,
3825789Sahrens 	VOPNAME_READDIR, zfs_readdir,
3826789Sahrens 	VOPNAME_SYMLINK, zfs_inval,
3827789Sahrens 	VOPNAME_FSYNC, zfs_fsync,
3828789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3829789Sahrens 	VOPNAME_FID, zfs_fid,
3830789Sahrens 	VOPNAME_SEEK, zfs_seek,
3831789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3832789Sahrens 	VOPNAME_GETSECATTR, zfs_getsecattr,
3833789Sahrens 	VOPNAME_SETSECATTR, zfs_setsecattr,
3834789Sahrens 	VOPNAME_VNEVENT, fs_vnevent_support,
3835789Sahrens 	NULL, NULL
3836789Sahrens };
3837789Sahrens 
3838789Sahrens /*
3839789Sahrens  * Error vnode operations template
3840789Sahrens  */
3841789Sahrens vnodeops_t *zfs_evnodeops;
3842789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = {
3843789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3844789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3845789Sahrens 	NULL, NULL
3846789Sahrens };
3847