xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 1816:8c14b56c8515)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51460Smarks  * Common Development and Distribution License (the "License").
61460Smarks  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
221231Smarks  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens #include <sys/types.h>
29789Sahrens #include <sys/param.h>
30789Sahrens #include <sys/time.h>
31789Sahrens #include <sys/systm.h>
32789Sahrens #include <sys/sysmacros.h>
33789Sahrens #include <sys/resource.h>
34789Sahrens #include <sys/vfs.h>
35789Sahrens #include <sys/vnode.h>
36789Sahrens #include <sys/file.h>
37789Sahrens #include <sys/stat.h>
38789Sahrens #include <sys/kmem.h>
39789Sahrens #include <sys/taskq.h>
40789Sahrens #include <sys/uio.h>
41789Sahrens #include <sys/vmsystm.h>
42789Sahrens #include <sys/atomic.h>
43789Sahrens #include <vm/seg_vn.h>
44789Sahrens #include <vm/pvn.h>
45789Sahrens #include <vm/as.h>
46789Sahrens #include <sys/mman.h>
47789Sahrens #include <sys/pathname.h>
48789Sahrens #include <sys/cmn_err.h>
49789Sahrens #include <sys/errno.h>
50789Sahrens #include <sys/unistd.h>
51789Sahrens #include <sys/zfs_vfsops.h>
52789Sahrens #include <sys/zfs_dir.h>
53789Sahrens #include <sys/zfs_acl.h>
54789Sahrens #include <sys/zfs_ioctl.h>
55789Sahrens #include <sys/fs/zfs.h>
56789Sahrens #include <sys/dmu.h>
57789Sahrens #include <sys/spa.h>
58789Sahrens #include <sys/txg.h>
59789Sahrens #include <sys/dbuf.h>
60789Sahrens #include <sys/zap.h>
61789Sahrens #include <sys/dirent.h>
62789Sahrens #include <sys/policy.h>
63789Sahrens #include <sys/sunddi.h>
64789Sahrens #include <sys/filio.h>
65789Sahrens #include "fs/fs_subr.h"
66789Sahrens #include <sys/zfs_ctldir.h>
671484Sek110237 #include <sys/dnlc.h>
681669Sperrin #include <sys/zfs_rlock.h>
69789Sahrens 
70789Sahrens /*
71789Sahrens  * Programming rules.
72789Sahrens  *
73789Sahrens  * Each vnode op performs some logical unit of work.  To do this, the ZPL must
74789Sahrens  * properly lock its in-core state, create a DMU transaction, do the work,
75789Sahrens  * record this work in the intent log (ZIL), commit the DMU transaction,
76789Sahrens  * and wait the the intent log to commit if it's is a synchronous operation.
77789Sahrens  * Morover, the vnode ops must work in both normal and log replay context.
78789Sahrens  * The ordering of events is important to avoid deadlocks and references
79789Sahrens  * to freed memory.  The example below illustrates the following Big Rules:
80789Sahrens  *
81789Sahrens  *  (1) A check must be made in each zfs thread for a mounted file system.
82789Sahrens  *	This is done avoiding races using ZFS_ENTER(zfsvfs).
83789Sahrens  *	A ZFS_EXIT(zfsvfs) is needed before all returns.
84789Sahrens  *
85789Sahrens  *  (2)	VN_RELE() should always be the last thing except for zil_commit()
86789Sahrens  *	and ZFS_EXIT(). This is for 3 reasons:
87789Sahrens  *	First, if it's the last reference, the vnode/znode
88789Sahrens  *	can be freed, so the zp may point to freed memory.  Second, the last
89789Sahrens  *	reference will call zfs_zinactive(), which may induce a lot of work --
901669Sperrin  *	pushing cached pages (which acquires range locks) and syncing out
91789Sahrens  *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
92789Sahrens  *	which could deadlock the system if you were already holding one.
93789Sahrens  *
941757Sperrin  *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
951757Sperrin  *	as they can span dmu_tx_assign() calls.
961757Sperrin  *
971757Sperrin  *  (4)	Always pass zfsvfs->z_assign as the second argument to dmu_tx_assign().
98789Sahrens  *	In normal operation, this will be TXG_NOWAIT.  During ZIL replay,
99789Sahrens  *	it will be a specific txg.  Either way, dmu_tx_assign() never blocks.
100789Sahrens  *	This is critical because we don't want to block while holding locks.
101789Sahrens  *	Note, in particular, that if a lock is sometimes acquired before
102789Sahrens  *	the tx assigns, and sometimes after (e.g. z_lock), then failing to
103789Sahrens  *	use a non-blocking assign can deadlock the system.  The scenario:
104789Sahrens  *
105789Sahrens  *	Thread A has grabbed a lock before calling dmu_tx_assign().
106789Sahrens  *	Thread B is in an already-assigned tx, and blocks for this lock.
107789Sahrens  *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
108789Sahrens  *	forever, because the previous txg can't quiesce until B's tx commits.
109789Sahrens  *
110789Sahrens  *	If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
111789Sahrens  *	then drop all locks, call txg_wait_open(), and try again.
112789Sahrens  *
1131757Sperrin  *  (5)	If the operation succeeded, generate the intent log entry for it
114789Sahrens  *	before dropping locks.  This ensures that the ordering of events
115789Sahrens  *	in the intent log matches the order in which they actually occurred.
116789Sahrens  *
1171757Sperrin  *  (6)	At the end of each vnode op, the DMU tx must always commit,
118789Sahrens  *	regardless of whether there were any errors.
119789Sahrens  *
1201757Sperrin  *  (7)	After dropping all locks, invoke zil_commit(zilog, seq, ioflag)
121789Sahrens  *	to ensure that synchronous semantics are provided when necessary.
122789Sahrens  *
123789Sahrens  * In general, this is how things should be ordered in each vnode op:
124789Sahrens  *
125789Sahrens  *	ZFS_ENTER(zfsvfs);		// exit if unmounted
126789Sahrens  * top:
127789Sahrens  *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
128789Sahrens  *	rw_enter(...);			// grab any other locks you need
129789Sahrens  *	tx = dmu_tx_create(...);	// get DMU tx
130789Sahrens  *	dmu_tx_hold_*();		// hold each object you might modify
131789Sahrens  *	error = dmu_tx_assign(tx, zfsvfs->z_assign);	// try to assign
132789Sahrens  *	if (error) {
133789Sahrens  *		dmu_tx_abort(tx);	// abort DMU tx
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) {
138789Sahrens  *			txg_wait_open(dmu_objset_pool(os), 0);
139789Sahrens  *			goto top;
140789Sahrens  *		}
141789Sahrens  *		ZFS_EXIT(zfsvfs);	// finished in zfs
142789Sahrens  *		return (error);		// really out of space
143789Sahrens  *	}
144789Sahrens  *	error = do_real_work();		// do whatever this VOP does
145789Sahrens  *	if (error == 0)
146789Sahrens  *		seq = zfs_log_*(...);	// on success, make ZIL entry
147789Sahrens  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
148789Sahrens  *	rw_exit(...);			// drop locks
149789Sahrens  *	zfs_dirent_unlock(dl);		// unlock directory entry
150789Sahrens  *	VN_RELE(...);			// release held vnodes
151789Sahrens  *	zil_commit(zilog, seq, ioflag);	// synchronous when necessary
152789Sahrens  *	ZFS_EXIT(zfsvfs);		// finished in zfs
153789Sahrens  *	return (error);			// done, report error
154789Sahrens  */
155789Sahrens 
156789Sahrens /* ARGSUSED */
157789Sahrens static int
158789Sahrens zfs_open(vnode_t **vpp, int flag, cred_t *cr)
159789Sahrens {
160789Sahrens 	return (0);
161789Sahrens }
162789Sahrens 
163789Sahrens /* ARGSUSED */
164789Sahrens static int
165789Sahrens zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr)
166789Sahrens {
167789Sahrens 	/*
168789Sahrens 	 * Clean up any locks held by this process on the vp.
169789Sahrens 	 */
170789Sahrens 	cleanlocks(vp, ddi_get_pid(), 0);
171789Sahrens 	cleanshares(vp, ddi_get_pid());
172789Sahrens 
173789Sahrens 	return (0);
174789Sahrens }
175789Sahrens 
176789Sahrens /*
177789Sahrens  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
178789Sahrens  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
179789Sahrens  */
180789Sahrens static int
181789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off)
182789Sahrens {
183789Sahrens 	znode_t	*zp = VTOZ(vp);
184789Sahrens 	uint64_t noff = (uint64_t)*off; /* new offset */
185789Sahrens 	uint64_t file_sz;
186789Sahrens 	int error;
187789Sahrens 	boolean_t hole;
188789Sahrens 
189789Sahrens 	file_sz = zp->z_phys->zp_size;
190789Sahrens 	if (noff >= file_sz)  {
191789Sahrens 		return (ENXIO);
192789Sahrens 	}
193789Sahrens 
194789Sahrens 	if (cmd == _FIO_SEEK_HOLE)
195789Sahrens 		hole = B_TRUE;
196789Sahrens 	else
197789Sahrens 		hole = B_FALSE;
198789Sahrens 
199789Sahrens 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
200789Sahrens 
201789Sahrens 	/* end of file? */
202789Sahrens 	if ((error == ESRCH) || (noff > file_sz)) {
203789Sahrens 		/*
204789Sahrens 		 * Handle the virtual hole at the end of file.
205789Sahrens 		 */
206789Sahrens 		if (hole) {
207789Sahrens 			*off = file_sz;
208789Sahrens 			return (0);
209789Sahrens 		}
210789Sahrens 		return (ENXIO);
211789Sahrens 	}
212789Sahrens 
213789Sahrens 	if (noff < *off)
214789Sahrens 		return (error);
215789Sahrens 	*off = noff;
216789Sahrens 	return (error);
217789Sahrens }
218789Sahrens 
219789Sahrens /* ARGSUSED */
220789Sahrens static int
221789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
222789Sahrens     int *rvalp)
223789Sahrens {
224789Sahrens 	offset_t off;
225789Sahrens 	int error;
226789Sahrens 	zfsvfs_t *zfsvfs;
227789Sahrens 
228789Sahrens 	switch (com) {
229789Sahrens 	    case _FIOFFS:
230789Sahrens 		return (zfs_sync(vp->v_vfsp, 0, cred));
231789Sahrens 
2321544Seschrock 		/*
2331544Seschrock 		 * The following two ioctls are used by bfu.  Faking out,
2341544Seschrock 		 * necessary to avoid bfu errors.
2351544Seschrock 		 */
2361544Seschrock 	    case _FIOGDIO:
2371544Seschrock 	    case _FIOSDIO:
2381544Seschrock 		return (0);
2391544Seschrock 
240789Sahrens 	    case _FIO_SEEK_DATA:
241789Sahrens 	    case _FIO_SEEK_HOLE:
242789Sahrens 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
243789Sahrens 			return (EFAULT);
244789Sahrens 
245789Sahrens 		zfsvfs = VTOZ(vp)->z_zfsvfs;
246789Sahrens 		ZFS_ENTER(zfsvfs);
247789Sahrens 
248789Sahrens 		/* offset parameter is in/out */
249789Sahrens 		error = zfs_holey(vp, com, &off);
250789Sahrens 		ZFS_EXIT(zfsvfs);
251789Sahrens 		if (error)
252789Sahrens 			return (error);
253789Sahrens 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
254789Sahrens 			return (EFAULT);
255789Sahrens 		return (0);
256789Sahrens 	}
257789Sahrens 	return (ENOTTY);
258789Sahrens }
259789Sahrens 
260789Sahrens /*
261789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
262789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
263789Sahrens  *
264789Sahrens  * On Write:	If we find a memory mapped page, we write to *both*
265789Sahrens  *		the page and the dmu buffer.
266789Sahrens  *
267789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
268789Sahrens  *	the file is memory mapped.
269789Sahrens  */
270789Sahrens static int
271789Sahrens mappedwrite(vnode_t *vp, uint64_t woff, int nbytes, uio_t *uio, dmu_tx_t *tx)
272789Sahrens {
273789Sahrens 	znode_t	*zp = VTOZ(vp);
274789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
275789Sahrens 	int64_t	start, off;
276789Sahrens 	int len = nbytes;
277789Sahrens 	int error = 0;
278789Sahrens 
279789Sahrens 	start = uio->uio_loffset;
280789Sahrens 	off = start & PAGEOFFSET;
281789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
282789Sahrens 		page_t *pp;
283789Sahrens 		uint64_t bytes = MIN(PAGESIZE - off, len);
284789Sahrens 
285789Sahrens 		/*
286789Sahrens 		 * We don't want a new page to "appear" in the middle of
287789Sahrens 		 * the file update (because it may not get the write
288789Sahrens 		 * update data), so we grab a lock to block
289789Sahrens 		 * zfs_getpage().
290789Sahrens 		 */
291789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
292789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
293789Sahrens 			caddr_t va;
294789Sahrens 
295789Sahrens 			rw_exit(&zp->z_map_lock);
296789Sahrens 			va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L);
297789Sahrens 			error = uiomove(va+off, bytes, UIO_WRITE, uio);
298789Sahrens 			if (error == 0) {
299789Sahrens 				dmu_write(zfsvfs->z_os, zp->z_id,
300789Sahrens 				    woff, bytes, va+off, tx);
301789Sahrens 			}
302789Sahrens 			ppmapout(va);
303789Sahrens 			page_unlock(pp);
304789Sahrens 		} else {
305789Sahrens 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
306789Sahrens 			    woff, bytes, uio, tx);
307789Sahrens 			rw_exit(&zp->z_map_lock);
308789Sahrens 		}
309789Sahrens 		len -= bytes;
310789Sahrens 		woff += bytes;
311789Sahrens 		off = 0;
312789Sahrens 		if (error)
313789Sahrens 			break;
314789Sahrens 	}
315789Sahrens 	return (error);
316789Sahrens }
317789Sahrens 
318789Sahrens /*
319789Sahrens  * When a file is memory mapped, we must keep the IO data synchronized
320789Sahrens  * between the DMU cache and the memory mapped pages.  What this means:
321789Sahrens  *
322789Sahrens  * On Read:	We "read" preferentially from memory mapped pages,
323789Sahrens  *		else we default from the dmu buffer.
324789Sahrens  *
325789Sahrens  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
326789Sahrens  *	the file is memory mapped.
327789Sahrens  */
328789Sahrens static int
329789Sahrens mappedread(vnode_t *vp, char *addr, int nbytes, uio_t *uio)
330789Sahrens {
331789Sahrens 	int64_t	start, off, bytes;
332789Sahrens 	int len = nbytes;
333789Sahrens 	int error = 0;
334789Sahrens 
335789Sahrens 	start = uio->uio_loffset;
336789Sahrens 	off = start & PAGEOFFSET;
337789Sahrens 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
338789Sahrens 		page_t *pp;
339789Sahrens 
340789Sahrens 		bytes = MIN(PAGESIZE - off, len);
341789Sahrens 		if (pp = page_lookup(vp, start, SE_SHARED)) {
342789Sahrens 			caddr_t va;
343789Sahrens 
344789Sahrens 			va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L);
345789Sahrens 			error = uiomove(va + off, bytes, UIO_READ, uio);
346789Sahrens 			ppmapout(va);
347789Sahrens 			page_unlock(pp);
348789Sahrens 		} else {
349789Sahrens 			/* XXX use dmu_read here? */
350789Sahrens 			error = uiomove(addr, bytes, UIO_READ, uio);
351789Sahrens 		}
352789Sahrens 		len -= bytes;
353789Sahrens 		addr += bytes;
354789Sahrens 		off = 0;
355789Sahrens 		if (error)
356789Sahrens 			break;
357789Sahrens 	}
358789Sahrens 	return (error);
359789Sahrens }
360789Sahrens 
361789Sahrens uint_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
362789Sahrens 
363789Sahrens /*
364789Sahrens  * Read bytes from specified file into supplied buffer.
365789Sahrens  *
366789Sahrens  *	IN:	vp	- vnode of file to be read from.
367789Sahrens  *		uio	- structure supplying read location, range info,
368789Sahrens  *			  and return buffer.
369789Sahrens  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
370789Sahrens  *		cr	- credentials of caller.
371789Sahrens  *
372789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
373789Sahrens  *
374789Sahrens  *	RETURN:	0 if success
375789Sahrens  *		error code if failure
376789Sahrens  *
377789Sahrens  * Side Effects:
378789Sahrens  *	vp - atime updated if byte count > 0
379789Sahrens  */
380789Sahrens /* ARGSUSED */
381789Sahrens static int
382789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
383789Sahrens {
384789Sahrens 	znode_t		*zp = VTOZ(vp);
385789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
386789Sahrens 	uint64_t	delta;
387789Sahrens 	ssize_t		n, size, cnt, ndone;
388789Sahrens 	int		error, i, numbufs;
389789Sahrens 	dmu_buf_t	*dbp, **dbpp;
3901669Sperrin 	rl_t		*rl;
391789Sahrens 
392789Sahrens 	ZFS_ENTER(zfsvfs);
393789Sahrens 
394789Sahrens 	/*
395789Sahrens 	 * Validate file offset
396789Sahrens 	 */
397789Sahrens 	if (uio->uio_loffset < (offset_t)0) {
398789Sahrens 		ZFS_EXIT(zfsvfs);
399789Sahrens 		return (EINVAL);
400789Sahrens 	}
401789Sahrens 
402789Sahrens 	/*
403789Sahrens 	 * Fasttrack empty reads
404789Sahrens 	 */
405789Sahrens 	if (uio->uio_resid == 0) {
406789Sahrens 		ZFS_EXIT(zfsvfs);
407789Sahrens 		return (0);
408789Sahrens 	}
409789Sahrens 
410789Sahrens 	/*
4111669Sperrin 	 * Check for mandatory locks
412789Sahrens 	 */
413789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
414789Sahrens 		if (error = chklock(vp, FREAD,
415789Sahrens 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
416789Sahrens 			ZFS_EXIT(zfsvfs);
417789Sahrens 			return (error);
418789Sahrens 		}
419789Sahrens 	}
420789Sahrens 
421789Sahrens 	/*
422789Sahrens 	 * If we're in FRSYNC mode, sync out this znode before reading it.
423789Sahrens 	 */
424789Sahrens 	zil_commit(zfsvfs->z_log, zp->z_last_itx, ioflag & FRSYNC);
425789Sahrens 
426789Sahrens 	/*
4271669Sperrin 	 * Lock the range against changes.
428789Sahrens 	 */
4291669Sperrin 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
4301669Sperrin 
431789Sahrens 	/*
432789Sahrens 	 * If we are reading past end-of-file we can skip
433789Sahrens 	 * to the end; but we might still need to set atime.
434789Sahrens 	 */
435789Sahrens 	if (uio->uio_loffset >= zp->z_phys->zp_size) {
436789Sahrens 		cnt = 0;
437789Sahrens 		error = 0;
438789Sahrens 		goto out;
439789Sahrens 	}
440789Sahrens 
441789Sahrens 	cnt = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset);
442789Sahrens 
443789Sahrens 	for (ndone = 0; ndone < cnt; ndone += zfs_read_chunk_size) {
444789Sahrens 		ASSERT(uio->uio_loffset < zp->z_phys->zp_size);
445789Sahrens 		n = MIN(zfs_read_chunk_size,
446789Sahrens 		    zp->z_phys->zp_size - uio->uio_loffset);
447789Sahrens 		n = MIN(n, cnt);
4481544Seschrock 		error = dmu_buf_hold_array(zfsvfs->z_os, zp->z_id,
4491544Seschrock 		    uio->uio_loffset, n, TRUE, FTAG, &numbufs, &dbpp);
4501544Seschrock 		if (error)
451789Sahrens 			goto out;
452789Sahrens 		/*
453789Sahrens 		 * Compute the adjustment to align the dmu buffers
454789Sahrens 		 * with the uio buffer.
455789Sahrens 		 */
456789Sahrens 		delta = uio->uio_loffset - dbpp[0]->db_offset;
457789Sahrens 
458789Sahrens 		for (i = 0; i < numbufs; i++) {
459789Sahrens 			if (n < 0)
460789Sahrens 				break;
461789Sahrens 			dbp = dbpp[i];
462789Sahrens 			size = dbp->db_size - delta;
463789Sahrens 			/*
464789Sahrens 			 * XXX -- this is correct, but may be suboptimal.
465789Sahrens 			 * If the pages are all clean, we don't need to
466789Sahrens 			 * go through mappedread().  Maybe the VMODSORT
467789Sahrens 			 * stuff can help us here.
468789Sahrens 			 */
469789Sahrens 			if (vn_has_cached_data(vp)) {
470789Sahrens 				error = mappedread(vp, (caddr_t)dbp->db_data +
471789Sahrens 				    delta, (n < size ? n : size), uio);
472789Sahrens 			} else {
473789Sahrens 				error = uiomove((caddr_t)dbp->db_data + delta,
474789Sahrens 					(n < size ? n : size), UIO_READ, uio);
475789Sahrens 			}
476789Sahrens 			if (error) {
4771544Seschrock 				dmu_buf_rele_array(dbpp, numbufs, FTAG);
478789Sahrens 				goto out;
479789Sahrens 			}
480789Sahrens 			n -= dbp->db_size;
481789Sahrens 			if (delta) {
482789Sahrens 				n += delta;
483789Sahrens 				delta = 0;
484789Sahrens 			}
485789Sahrens 		}
4861544Seschrock 		dmu_buf_rele_array(dbpp, numbufs, FTAG);
487789Sahrens 	}
488789Sahrens out:
4891669Sperrin 	zfs_range_unlock(zp, rl);
490789Sahrens 
491789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
492789Sahrens 	ZFS_EXIT(zfsvfs);
493789Sahrens 	return (error);
494789Sahrens }
495789Sahrens 
496789Sahrens /*
497789Sahrens  * Fault in the pages of the first n bytes specified by the uio structure.
498789Sahrens  * 1 byte in each page is touched and the uio struct is unmodified.
499789Sahrens  * Any error will exit this routine as this is only a best
500789Sahrens  * attempt to get the pages resident. This is a copy of ufs_trans_touch().
501789Sahrens  */
502789Sahrens static void
503789Sahrens zfs_prefault_write(ssize_t n, struct uio *uio)
504789Sahrens {
505789Sahrens 	struct iovec *iov;
506789Sahrens 	ulong_t cnt, incr;
507789Sahrens 	caddr_t p;
508789Sahrens 	uint8_t tmp;
509789Sahrens 
510789Sahrens 	iov = uio->uio_iov;
511789Sahrens 
512789Sahrens 	while (n) {
513789Sahrens 		cnt = MIN(iov->iov_len, n);
514789Sahrens 		if (cnt == 0) {
515789Sahrens 			/* empty iov entry */
516789Sahrens 			iov++;
517789Sahrens 			continue;
518789Sahrens 		}
519789Sahrens 		n -= cnt;
520789Sahrens 		/*
521789Sahrens 		 * touch each page in this segment.
522789Sahrens 		 */
523789Sahrens 		p = iov->iov_base;
524789Sahrens 		while (cnt) {
525789Sahrens 			switch (uio->uio_segflg) {
526789Sahrens 			case UIO_USERSPACE:
527789Sahrens 			case UIO_USERISPACE:
528789Sahrens 				if (fuword8(p, &tmp))
529789Sahrens 					return;
530789Sahrens 				break;
531789Sahrens 			case UIO_SYSSPACE:
532789Sahrens 				if (kcopy(p, &tmp, 1))
533789Sahrens 					return;
534789Sahrens 				break;
535789Sahrens 			}
536789Sahrens 			incr = MIN(cnt, PAGESIZE);
537789Sahrens 			p += incr;
538789Sahrens 			cnt -= incr;
539789Sahrens 		}
540789Sahrens 		/*
541789Sahrens 		 * touch the last byte in case it straddles a page.
542789Sahrens 		 */
543789Sahrens 		p--;
544789Sahrens 		switch (uio->uio_segflg) {
545789Sahrens 		case UIO_USERSPACE:
546789Sahrens 		case UIO_USERISPACE:
547789Sahrens 			if (fuword8(p, &tmp))
548789Sahrens 				return;
549789Sahrens 			break;
550789Sahrens 		case UIO_SYSSPACE:
551789Sahrens 			if (kcopy(p, &tmp, 1))
552789Sahrens 				return;
553789Sahrens 			break;
554789Sahrens 		}
555789Sahrens 		iov++;
556789Sahrens 	}
557789Sahrens }
558789Sahrens 
559789Sahrens /*
560789Sahrens  * Write the bytes to a file.
561789Sahrens  *
562789Sahrens  *	IN:	vp	- vnode of file to be written to.
563789Sahrens  *		uio	- structure supplying write location, range info,
564789Sahrens  *			  and data buffer.
565789Sahrens  *		ioflag	- FAPPEND flag set if in append mode.
566789Sahrens  *		cr	- credentials of caller.
567789Sahrens  *
568789Sahrens  *	OUT:	uio	- updated offset and range.
569789Sahrens  *
570789Sahrens  *	RETURN:	0 if success
571789Sahrens  *		error code if failure
572789Sahrens  *
573789Sahrens  * Timestamps:
574789Sahrens  *	vp - ctime|mtime updated if byte count > 0
575789Sahrens  */
576789Sahrens /* ARGSUSED */
577789Sahrens static int
578789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
579789Sahrens {
580789Sahrens 	znode_t		*zp = VTOZ(vp);
581789Sahrens 	rlim64_t	limit = uio->uio_llimit;
582789Sahrens 	ssize_t		start_resid = uio->uio_resid;
583789Sahrens 	ssize_t		tx_bytes;
584789Sahrens 	uint64_t	end_size;
585789Sahrens 	dmu_tx_t	*tx;
586789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
587789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
588789Sahrens 	uint64_t	seq = 0;
589789Sahrens 	offset_t	woff;
590789Sahrens 	ssize_t		n, nbytes;
5911669Sperrin 	rl_t		*rl;
592789Sahrens 	int		max_blksz = zfsvfs->z_max_blksz;
5931669Sperrin 	int		error;
594789Sahrens 
595789Sahrens 	/*
596789Sahrens 	 * Fasttrack empty write
597789Sahrens 	 */
5981669Sperrin 	n = start_resid;
599789Sahrens 	if (n == 0)
600789Sahrens 		return (0);
601789Sahrens 
6021669Sperrin 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
6031669Sperrin 		limit = MAXOFFSET_T;
6041669Sperrin 
605789Sahrens 	ZFS_ENTER(zfsvfs);
606789Sahrens 
607789Sahrens 	/*
6081669Sperrin 	 * Pre-fault the initial pages to ensure slow (eg NFS) pages
6091669Sperrin 	 * don't hold up txg.
610789Sahrens 	 */
611789Sahrens 	zfs_prefault_write(MIN(start_resid, SPA_MAXBLOCKSIZE), uio);
612789Sahrens 
613789Sahrens 	/*
614789Sahrens 	 * If in append mode, set the io offset pointer to eof.
615789Sahrens 	 */
6161669Sperrin 	if (ioflag & FAPPEND) {
6171669Sperrin 		/*
6181669Sperrin 		 * Range lock for a file append:
6191669Sperrin 		 * The value for the start of range will be determined by
6201669Sperrin 		 * zfs_range_lock() (to guarantee append semantics).
6211669Sperrin 		 * If this write will cause the block size to increase,
6221669Sperrin 		 * zfs_range_lock() will lock the entire file, so we must
6231669Sperrin 		 * later reduce the range after we grow the block size.
6241669Sperrin 		 */
6251669Sperrin 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
6261669Sperrin 		if (rl->r_len == UINT64_MAX) {
6271669Sperrin 			/* overlocked, zp_size can't change */
6281669Sperrin 			woff = uio->uio_loffset = zp->z_phys->zp_size;
6291669Sperrin 		} else {
6301669Sperrin 			woff = uio->uio_loffset = rl->r_off;
6311669Sperrin 		}
632789Sahrens 	} else {
633789Sahrens 		woff = uio->uio_loffset;
634789Sahrens 		/*
635789Sahrens 		 * Validate file offset
636789Sahrens 		 */
637789Sahrens 		if (woff < 0) {
638789Sahrens 			ZFS_EXIT(zfsvfs);
639789Sahrens 			return (EINVAL);
640789Sahrens 		}
641789Sahrens 
642789Sahrens 		/*
6431669Sperrin 		 * If we need to grow the block size then zfs_range_lock()
6441669Sperrin 		 * will lock a wider range than we request here.
6451669Sperrin 		 * Later after growing the block size we reduce the range.
646789Sahrens 		 */
6471669Sperrin 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
648789Sahrens 	}
649789Sahrens 
650789Sahrens 	if (woff >= limit) {
651789Sahrens 		error = EFBIG;
652789Sahrens 		goto no_tx_done;
653789Sahrens 	}
654789Sahrens 
655789Sahrens 	if ((woff + n) > limit || woff > (limit - n))
656789Sahrens 		n = limit - woff;
657789Sahrens 
658789Sahrens 	/*
6591669Sperrin 	 * Check for mandatory locks
660789Sahrens 	 */
661789Sahrens 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) &&
662789Sahrens 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0)
663789Sahrens 		goto no_tx_done;
6641669Sperrin 	end_size = MAX(zp->z_phys->zp_size, woff + n);
665789Sahrens top:
666789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
667789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
668789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
669789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
670789Sahrens 	if (error) {
671789Sahrens 		dmu_tx_abort(tx);
672789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
673789Sahrens 			txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0);
674789Sahrens 			goto top;
675789Sahrens 		}
676789Sahrens 		goto no_tx_done;
677789Sahrens 	}
678789Sahrens 
6791669Sperrin 	/*
6801669Sperrin 	 * If zfs_range_lock() over-locked we grow the blocksize
6811669Sperrin 	 * and then reduce the lock range.
6821669Sperrin 	 */
6831669Sperrin 	if (rl->r_len == UINT64_MAX) {
684789Sahrens 		uint64_t new_blksz;
6851669Sperrin 
686789Sahrens 		if (zp->z_blksz > max_blksz) {
687789Sahrens 			ASSERT(!ISP2(zp->z_blksz));
688789Sahrens 			new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
689789Sahrens 		} else {
690789Sahrens 			new_blksz = MIN(end_size, max_blksz);
691789Sahrens 		}
6921669Sperrin 		zfs_grow_blocksize(zp, new_blksz, tx);
6931669Sperrin 		zfs_range_reduce(zp, rl, woff, n);
694789Sahrens 	}
695789Sahrens 
696789Sahrens 	/*
697789Sahrens 	 * The file data does not fit in the znode "cache", so we
698789Sahrens 	 * will be writing to the file block data buffers.
699789Sahrens 	 * Each buffer will be written in a separate transaction;
700789Sahrens 	 * this keeps the intent log records small and allows us
701789Sahrens 	 * to do more fine-grained space accounting.
702789Sahrens 	 */
703789Sahrens 	while (n > 0) {
704789Sahrens 		/*
705789Sahrens 		 * XXX - should we really limit each write to z_max_blksz?
706789Sahrens 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
707789Sahrens 		 */
708789Sahrens 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
709789Sahrens 		rw_enter(&zp->z_map_lock, RW_READER);
710789Sahrens 
711789Sahrens 		tx_bytes = uio->uio_resid;
712789Sahrens 		if (vn_has_cached_data(vp)) {
713789Sahrens 			rw_exit(&zp->z_map_lock);
714789Sahrens 			error = mappedwrite(vp, woff, nbytes, uio, tx);
715789Sahrens 		} else {
716789Sahrens 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
717789Sahrens 			    woff, nbytes, uio, tx);
718789Sahrens 			rw_exit(&zp->z_map_lock);
719789Sahrens 		}
720789Sahrens 		tx_bytes -= uio->uio_resid;
721789Sahrens 
722789Sahrens 		if (error) {
723789Sahrens 			/* XXX - do we need to "clean up" the dmu buffer? */
724789Sahrens 			break;
725789Sahrens 		}
726789Sahrens 
727789Sahrens 		ASSERT(tx_bytes == nbytes);
728789Sahrens 
7291576Smarks 		/*
7301576Smarks 		 * Clear Set-UID/Set-GID bits on successful write if not
7311576Smarks 		 * privileged and at least one of the excute bits is set.
7321576Smarks 		 *
7331576Smarks 		 * It would be nice to to this after all writes have
7341576Smarks 		 * been done, but that would still expose the ISUID/ISGID
7351576Smarks 		 * to another app after the partial write is committed.
7361576Smarks 		 */
7371576Smarks 
7381576Smarks 		mutex_enter(&zp->z_acl_lock);
7391576Smarks 		if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) |
7401576Smarks 		    (S_IXUSR >> 6))) != 0 &&
7411576Smarks 		    (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 &&
7421576Smarks 		    secpolicy_vnode_setid_retain(cr,
7431576Smarks 		    (zp->z_phys->zp_mode & S_ISUID) != 0 &&
7441576Smarks 		    zp->z_phys->zp_uid == 0) != 0) {
7451576Smarks 			    zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID);
7461576Smarks 		}
7471576Smarks 		mutex_exit(&zp->z_acl_lock);
7481576Smarks 
749789Sahrens 		n -= nbytes;
750789Sahrens 		if (n <= 0)
751789Sahrens 			break;
752789Sahrens 
753789Sahrens 		/*
754789Sahrens 		 * We have more work ahead of us, so wrap up this transaction
755789Sahrens 		 * and start another.  Exact same logic as tx_done below.
756789Sahrens 		 */
757789Sahrens 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) {
758789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
759789Sahrens 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
760789Sahrens 			    uio->uio_loffset);
761789Sahrens 		}
762789Sahrens 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
763789Sahrens 		seq = zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes,
764789Sahrens 		    ioflag, uio);
765789Sahrens 		dmu_tx_commit(tx);
766789Sahrens 
767789Sahrens 		/* Pre-fault the next set of pages */
768789Sahrens 		zfs_prefault_write(MIN(n, SPA_MAXBLOCKSIZE), uio);
769789Sahrens 
770789Sahrens 		/*
771789Sahrens 		 * Start another transaction.
772789Sahrens 		 */
773789Sahrens 		woff = uio->uio_loffset;
774789Sahrens 		tx = dmu_tx_create(zfsvfs->z_os);
775789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
776789Sahrens 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
777789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
778789Sahrens 		if (error) {
779789Sahrens 			dmu_tx_abort(tx);
780789Sahrens 			if (error == ERESTART &&
781789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
782789Sahrens 				txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0);
783789Sahrens 				goto top;
784789Sahrens 			}
785789Sahrens 			goto no_tx_done;
786789Sahrens 		}
787789Sahrens 	}
788789Sahrens 
789789Sahrens tx_done:
790789Sahrens 
791789Sahrens 	if (tx_bytes != 0) {
792789Sahrens 		/*
793789Sahrens 		 * Update the file size if it has changed; account
794789Sahrens 		 * for possible concurrent updates.
795789Sahrens 		 */
796789Sahrens 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) {
797789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
798789Sahrens 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
799789Sahrens 			    uio->uio_loffset);
800789Sahrens 		}
801789Sahrens 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
802789Sahrens 		seq = zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes,
803789Sahrens 		    ioflag, uio);
804789Sahrens 	}
805789Sahrens 	dmu_tx_commit(tx);
806789Sahrens 
807789Sahrens 
808789Sahrens no_tx_done:
809789Sahrens 
8101669Sperrin 	zfs_range_unlock(zp, rl);
811789Sahrens 
812789Sahrens 	/*
813789Sahrens 	 * If we're in replay mode, or we made no progress, return error.
814789Sahrens 	 * Otherwise, it's at least a partial write, so it's successful.
815789Sahrens 	 */
816789Sahrens 	if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) {
817789Sahrens 		ZFS_EXIT(zfsvfs);
818789Sahrens 		return (error);
819789Sahrens 	}
820789Sahrens 
821789Sahrens 	zil_commit(zilog, seq, ioflag & (FSYNC | FDSYNC));
822789Sahrens 
823789Sahrens 	ZFS_EXIT(zfsvfs);
824789Sahrens 	return (0);
825789Sahrens }
826789Sahrens 
827789Sahrens /*
828789Sahrens  * Get data to generate a TX_WRITE intent log record.
829789Sahrens  */
830789Sahrens int
8311669Sperrin zfs_get_data(void *arg, lr_write_t *lr, char *buf)
832789Sahrens {
833789Sahrens 	zfsvfs_t *zfsvfs = arg;
834789Sahrens 	objset_t *os = zfsvfs->z_os;
835789Sahrens 	znode_t *zp;
836789Sahrens 	uint64_t off = lr->lr_offset;
8371669Sperrin 	rl_t *rl;
838789Sahrens 	int dlen = lr->lr_length;  		/* length of user data */
839789Sahrens 	int error = 0;
840789Sahrens 
841789Sahrens 	ASSERT(dlen != 0);
842789Sahrens 
843789Sahrens 	/*
8441669Sperrin 	 * Nothing to do if the file has been removed
845789Sahrens 	 */
846789Sahrens 	if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0)
847789Sahrens 		return (ENOENT);
8481669Sperrin 	if (zp->z_reap) {
849789Sahrens 		VN_RELE(ZTOV(zp));
850789Sahrens 		return (ENOENT);
851789Sahrens 	}
852789Sahrens 
853789Sahrens 	/*
854789Sahrens 	 * Write records come in two flavors: immediate and indirect.
855789Sahrens 	 * For small writes it's cheaper to store the data with the
856789Sahrens 	 * log record (immediate); for large writes it's cheaper to
857789Sahrens 	 * sync the data and get a pointer to it (indirect) so that
858789Sahrens 	 * we don't have to write the data twice.
859789Sahrens 	 */
8601669Sperrin 	if (buf != NULL) { /* immediate write */
8611544Seschrock 		dmu_buf_t *db;
8621669Sperrin 
8631669Sperrin 		rl = zfs_range_lock(zp, off, dlen, RL_READER);
8641669Sperrin 		/* test for truncation needs to be done while range locked */
8651669Sperrin 		if (off >= zp->z_phys->zp_size) {
8661669Sperrin 			error = ENOENT;
8671669Sperrin 			goto out;
8681669Sperrin 		}
8691544Seschrock 		VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, off, FTAG, &db));
8701669Sperrin 		bcopy((char *)db->db_data + off - db->db_offset, buf, dlen);
8711544Seschrock 		dmu_buf_rele(db, FTAG);
8721669Sperrin 	} else { /* indirect write */
8731669Sperrin 		uint64_t boff; /* block starting offset */
8741669Sperrin 
875789Sahrens 		/*
8761669Sperrin 		 * Have to lock the whole block to ensure when it's
8771669Sperrin 		 * written out and it's checksum is being calculated
8781669Sperrin 		 * that no one can change the data. We need to re-check
8791669Sperrin 		 * blocksize after we get the lock in case it's changed!
880789Sahrens 		 */
8811669Sperrin 		for (;;) {
8821669Sperrin 			boff = off & ~(zp->z_blksz - 1);
8831669Sperrin 			dlen = zp->z_blksz;
8841669Sperrin 			rl = zfs_range_lock(zp, boff, dlen, RL_READER);
8851669Sperrin 			if (zp->z_blksz == dlen)
8861669Sperrin 				break;
8871669Sperrin 			zfs_range_unlock(zp, rl);
8881669Sperrin 		}
8891669Sperrin 		/* test for truncation needs to be done while range locked */
8901669Sperrin 		if (off >= zp->z_phys->zp_size) {
8911669Sperrin 			error = ENOENT;
8921669Sperrin 			goto out;
8931669Sperrin 		}
894789Sahrens 		txg_suspend(dmu_objset_pool(os));
895789Sahrens 		error = dmu_sync(os, lr->lr_foid, off, &lr->lr_blkoff,
896789Sahrens 		    &lr->lr_blkptr, lr->lr_common.lrc_txg);
897789Sahrens 		txg_resume(dmu_objset_pool(os));
898789Sahrens 	}
8991669Sperrin out:
9001669Sperrin 	zfs_range_unlock(zp, rl);
901789Sahrens 	VN_RELE(ZTOV(zp));
902789Sahrens 	return (error);
903789Sahrens }
904789Sahrens 
905789Sahrens /*ARGSUSED*/
906789Sahrens static int
907789Sahrens zfs_access(vnode_t *vp, int mode, int flags, cred_t *cr)
908789Sahrens {
909789Sahrens 	znode_t *zp = VTOZ(vp);
910789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
911789Sahrens 	int error;
912789Sahrens 
913789Sahrens 	ZFS_ENTER(zfsvfs);
914789Sahrens 	error = zfs_zaccess_rwx(zp, mode, cr);
915789Sahrens 	ZFS_EXIT(zfsvfs);
916789Sahrens 	return (error);
917789Sahrens }
918789Sahrens 
919789Sahrens /*
920789Sahrens  * Lookup an entry in a directory, or an extended attribute directory.
921789Sahrens  * If it exists, return a held vnode reference for it.
922789Sahrens  *
923789Sahrens  *	IN:	dvp	- vnode of directory to search.
924789Sahrens  *		nm	- name of entry to lookup.
925789Sahrens  *		pnp	- full pathname to lookup [UNUSED].
926789Sahrens  *		flags	- LOOKUP_XATTR set if looking for an attribute.
927789Sahrens  *		rdir	- root directory vnode [UNUSED].
928789Sahrens  *		cr	- credentials of caller.
929789Sahrens  *
930789Sahrens  *	OUT:	vpp	- vnode of located entry, NULL if not found.
931789Sahrens  *
932789Sahrens  *	RETURN:	0 if success
933789Sahrens  *		error code if failure
934789Sahrens  *
935789Sahrens  * Timestamps:
936789Sahrens  *	NA
937789Sahrens  */
938789Sahrens /* ARGSUSED */
939789Sahrens static int
940789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
941789Sahrens     int flags, vnode_t *rdir, cred_t *cr)
942789Sahrens {
943789Sahrens 
944789Sahrens 	znode_t *zdp = VTOZ(dvp);
945789Sahrens 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
946789Sahrens 	int	error;
947789Sahrens 
948789Sahrens 	ZFS_ENTER(zfsvfs);
949789Sahrens 
950789Sahrens 	*vpp = NULL;
951789Sahrens 
952789Sahrens 	if (flags & LOOKUP_XATTR) {
953789Sahrens 		/*
954789Sahrens 		 * We don't allow recursive attributes..
955789Sahrens 		 * Maybe someday we will.
956789Sahrens 		 */
957789Sahrens 		if (zdp->z_phys->zp_flags & ZFS_XATTR) {
958789Sahrens 			ZFS_EXIT(zfsvfs);
959789Sahrens 			return (EINVAL);
960789Sahrens 		}
961789Sahrens 
962789Sahrens 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr)) {
963789Sahrens 			ZFS_EXIT(zfsvfs);
964789Sahrens 			return (error);
965789Sahrens 		}
966789Sahrens 
967789Sahrens 		/*
968789Sahrens 		 * Do we have permission to get into attribute directory?
969789Sahrens 		 */
970789Sahrens 
971789Sahrens 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, cr)) {
972789Sahrens 			VN_RELE(*vpp);
973789Sahrens 		}
974789Sahrens 
975789Sahrens 		ZFS_EXIT(zfsvfs);
976789Sahrens 		return (error);
977789Sahrens 	}
978789Sahrens 
9791512Sek110237 	if (dvp->v_type != VDIR) {
9801512Sek110237 		ZFS_EXIT(zfsvfs);
9811460Smarks 		return (ENOTDIR);
9821512Sek110237 	}
9831460Smarks 
984789Sahrens 	/*
985789Sahrens 	 * Check accessibility of directory.
986789Sahrens 	 */
987789Sahrens 
988789Sahrens 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, cr)) {
989789Sahrens 		ZFS_EXIT(zfsvfs);
990789Sahrens 		return (error);
991789Sahrens 	}
992789Sahrens 
993789Sahrens 	if ((error = zfs_dirlook(zdp, nm, vpp)) == 0) {
994789Sahrens 
995789Sahrens 		/*
996789Sahrens 		 * Convert device special files
997789Sahrens 		 */
998789Sahrens 		if (IS_DEVVP(*vpp)) {
999789Sahrens 			vnode_t	*svp;
1000789Sahrens 
1001789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1002789Sahrens 			VN_RELE(*vpp);
1003789Sahrens 			if (svp == NULL)
1004789Sahrens 				error = ENOSYS;
1005789Sahrens 			else
1006789Sahrens 				*vpp = svp;
1007789Sahrens 		}
1008789Sahrens 	}
1009789Sahrens 
1010789Sahrens 	ZFS_EXIT(zfsvfs);
1011789Sahrens 	return (error);
1012789Sahrens }
1013789Sahrens 
1014789Sahrens /*
1015789Sahrens  * Attempt to create a new entry in a directory.  If the entry
1016789Sahrens  * already exists, truncate the file if permissible, else return
1017789Sahrens  * an error.  Return the vp of the created or trunc'd file.
1018789Sahrens  *
1019789Sahrens  *	IN:	dvp	- vnode of directory to put new file entry in.
1020789Sahrens  *		name	- name of new file entry.
1021789Sahrens  *		vap	- attributes of new file.
1022789Sahrens  *		excl	- flag indicating exclusive or non-exclusive mode.
1023789Sahrens  *		mode	- mode to open file with.
1024789Sahrens  *		cr	- credentials of caller.
1025789Sahrens  *		flag	- large file flag [UNUSED].
1026789Sahrens  *
1027789Sahrens  *	OUT:	vpp	- vnode of created or trunc'd entry.
1028789Sahrens  *
1029789Sahrens  *	RETURN:	0 if success
1030789Sahrens  *		error code if failure
1031789Sahrens  *
1032789Sahrens  * Timestamps:
1033789Sahrens  *	dvp - ctime|mtime updated if new entry created
1034789Sahrens  *	 vp - ctime|mtime always, atime if new
1035789Sahrens  */
1036789Sahrens /* ARGSUSED */
1037789Sahrens static int
1038789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1039789Sahrens     int mode, vnode_t **vpp, cred_t *cr, int flag)
1040789Sahrens {
1041789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1042789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1043789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1044789Sahrens 	uint64_t	seq = 0;
1045789Sahrens 	objset_t	*os = zfsvfs->z_os;
1046789Sahrens 	zfs_dirlock_t	*dl;
1047789Sahrens 	dmu_tx_t	*tx;
10481669Sperrin 	rl_t		*rl;
1049789Sahrens 	int		error;
1050789Sahrens 	uint64_t	zoid;
1051789Sahrens 
1052789Sahrens 	ZFS_ENTER(zfsvfs);
1053789Sahrens 
1054789Sahrens top:
1055789Sahrens 	*vpp = NULL;
1056789Sahrens 
1057789Sahrens 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1058789Sahrens 		vap->va_mode &= ~VSVTX;
1059789Sahrens 
1060789Sahrens 	if (*name == '\0') {
1061789Sahrens 		/*
1062789Sahrens 		 * Null component name refers to the directory itself.
1063789Sahrens 		 */
1064789Sahrens 		VN_HOLD(dvp);
1065789Sahrens 		zp = dzp;
1066789Sahrens 		dl = NULL;
1067789Sahrens 		error = 0;
1068789Sahrens 	} else {
1069789Sahrens 		/* possible VN_HOLD(zp) */
1070789Sahrens 		if (error = zfs_dirent_lock(&dl, dzp, name, &zp, 0)) {
1071789Sahrens 			if (strcmp(name, "..") == 0)
1072789Sahrens 				error = EISDIR;
1073789Sahrens 			ZFS_EXIT(zfsvfs);
1074789Sahrens 			return (error);
1075789Sahrens 		}
1076789Sahrens 	}
1077789Sahrens 
1078789Sahrens 	zoid = zp ? zp->z_id : -1ULL;
1079789Sahrens 
1080789Sahrens 	if (zp == NULL) {
1081789Sahrens 		/*
1082789Sahrens 		 * Create a new file object and update the directory
1083789Sahrens 		 * to reference it.
1084789Sahrens 		 */
1085789Sahrens 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
1086789Sahrens 			goto out;
1087789Sahrens 		}
1088789Sahrens 
1089789Sahrens 		/*
1090789Sahrens 		 * We only support the creation of regular files in
1091789Sahrens 		 * extended attribute directories.
1092789Sahrens 		 */
1093789Sahrens 		if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1094789Sahrens 		    (vap->va_type != VREG)) {
1095789Sahrens 			error = EINVAL;
1096789Sahrens 			goto out;
1097789Sahrens 		}
1098789Sahrens 
1099789Sahrens 		tx = dmu_tx_create(os);
1100789Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1101789Sahrens 		dmu_tx_hold_bonus(tx, dzp->z_id);
11021544Seschrock 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1103789Sahrens 		if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
1104789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1105789Sahrens 			    0, SPA_MAXBLOCKSIZE);
1106789Sahrens 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
1107789Sahrens 		if (error) {
1108789Sahrens 			dmu_tx_abort(tx);
1109789Sahrens 			zfs_dirent_unlock(dl);
1110789Sahrens 			if (error == ERESTART &&
1111789Sahrens 			    zfsvfs->z_assign == TXG_NOWAIT) {
1112789Sahrens 				txg_wait_open(dmu_objset_pool(os), 0);
1113789Sahrens 				goto top;
1114789Sahrens 			}
1115789Sahrens 			ZFS_EXIT(zfsvfs);
1116789Sahrens 			return (error);
1117789Sahrens 		}
1118789Sahrens 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
1119789Sahrens 		ASSERT(zp->z_id == zoid);
1120789Sahrens 		(void) zfs_link_create(dl, zp, tx, ZNEW);
1121789Sahrens 		seq = zfs_log_create(zilog, tx, TX_CREATE, dzp, zp, name);
1122789Sahrens 		dmu_tx_commit(tx);
1123789Sahrens 	} else {
1124789Sahrens 		/*
1125789Sahrens 		 * A directory entry already exists for this name.
1126789Sahrens 		 */
1127789Sahrens 		/*
1128789Sahrens 		 * Can't truncate an existing file if in exclusive mode.
1129789Sahrens 		 */
1130789Sahrens 		if (excl == EXCL) {
1131789Sahrens 			error = EEXIST;
1132789Sahrens 			goto out;
1133789Sahrens 		}
1134789Sahrens 		/*
1135789Sahrens 		 * Can't open a directory for writing.
1136789Sahrens 		 */
1137789Sahrens 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1138789Sahrens 			error = EISDIR;
1139789Sahrens 			goto out;
1140789Sahrens 		}
1141789Sahrens 		/*
1142789Sahrens 		 * Verify requested access to file.
1143789Sahrens 		 */
1144789Sahrens 		if (mode && (error = zfs_zaccess_rwx(zp, mode, cr))) {
1145789Sahrens 			goto out;
1146789Sahrens 		}
1147789Sahrens 		/*
1148789Sahrens 		 * Truncate regular files if requested.
1149789Sahrens 		 */
1150789Sahrens 
1151789Sahrens 		/*
1152789Sahrens 		 * Need to update dzp->z_seq?
1153789Sahrens 		 */
1154789Sahrens 
1155789Sahrens 		mutex_enter(&dzp->z_lock);
1156789Sahrens 		dzp->z_seq++;
1157789Sahrens 		mutex_exit(&dzp->z_lock);
1158789Sahrens 
1159789Sahrens 		if ((ZTOV(zp)->v_type == VREG) && (zp->z_phys->zp_size != 0) &&
1160789Sahrens 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1161789Sahrens 			/*
1162789Sahrens 			 * Truncate the file.
1163789Sahrens 			 */
1164789Sahrens 			tx = dmu_tx_create(os);
1165789Sahrens 			dmu_tx_hold_bonus(tx, zoid);
1166789Sahrens 			dmu_tx_hold_free(tx, zoid, 0, DMU_OBJECT_END);
11671757Sperrin 			/* Lock the whole range of the file */
11681757Sperrin 			rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1169789Sahrens 			error = dmu_tx_assign(tx, zfsvfs->z_assign);
1170789Sahrens 			if (error) {
1171789Sahrens 				dmu_tx_abort(tx);
11721757Sperrin 				zfs_range_unlock(zp, rl);
1173789Sahrens 				if (dl)
1174789Sahrens 					zfs_dirent_unlock(dl);
1175789Sahrens 				VN_RELE(ZTOV(zp));
1176789Sahrens 				if (error == ERESTART &&
1177789Sahrens 				    zfsvfs->z_assign == TXG_NOWAIT) {
1178789Sahrens 					txg_wait_open(dmu_objset_pool(os), 0);
1179789Sahrens 					goto top;
1180789Sahrens 				}
1181789Sahrens 				ZFS_EXIT(zfsvfs);
1182789Sahrens 				return (error);
1183789Sahrens 			}
1184789Sahrens 			error = zfs_freesp(zp, 0, 0, mode, tx, cr);
1185789Sahrens 			if (error == 0) {
1186789Sahrens 				zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
1187789Sahrens 				seq = zfs_log_truncate(zilog, tx,
1188789Sahrens 				    TX_TRUNCATE, zp, 0, 0);
1189789Sahrens 			}
11901669Sperrin 			zfs_range_unlock(zp, rl);
1191789Sahrens 			dmu_tx_commit(tx);
1192789Sahrens 		}
1193789Sahrens 	}
1194789Sahrens out:
1195789Sahrens 
1196789Sahrens 	if (dl)
1197789Sahrens 		zfs_dirent_unlock(dl);
1198789Sahrens 
1199789Sahrens 	if (error) {
1200789Sahrens 		if (zp)
1201789Sahrens 			VN_RELE(ZTOV(zp));
1202789Sahrens 	} else {
1203789Sahrens 		*vpp = ZTOV(zp);
1204789Sahrens 		/*
1205789Sahrens 		 * If vnode is for a device return a specfs vnode instead.
1206789Sahrens 		 */
1207789Sahrens 		if (IS_DEVVP(*vpp)) {
1208789Sahrens 			struct vnode *svp;
1209789Sahrens 
1210789Sahrens 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1211789Sahrens 			VN_RELE(*vpp);
1212789Sahrens 			if (svp == NULL) {
1213789Sahrens 				error = ENOSYS;
1214789Sahrens 			}
1215789Sahrens 			*vpp = svp;
1216789Sahrens 		}
1217789Sahrens 	}
1218789Sahrens 
1219789Sahrens 	zil_commit(zilog, seq, 0);
1220789Sahrens 
1221789Sahrens 	ZFS_EXIT(zfsvfs);
1222789Sahrens 	return (error);
1223789Sahrens }
1224789Sahrens 
1225789Sahrens /*
1226789Sahrens  * Remove an entry from a directory.
1227789Sahrens  *
1228789Sahrens  *	IN:	dvp	- vnode of directory to remove entry from.
1229789Sahrens  *		name	- name of entry to remove.
1230789Sahrens  *		cr	- credentials of caller.
1231789Sahrens  *
1232789Sahrens  *	RETURN:	0 if success
1233789Sahrens  *		error code if failure
1234789Sahrens  *
1235789Sahrens  * Timestamps:
1236789Sahrens  *	dvp - ctime|mtime
1237789Sahrens  *	 vp - ctime (if nlink > 0)
1238789Sahrens  */
1239789Sahrens static int
1240789Sahrens zfs_remove(vnode_t *dvp, char *name, cred_t *cr)
1241789Sahrens {
1242789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1243789Sahrens 	znode_t		*xzp = NULL;
1244789Sahrens 	vnode_t		*vp;
1245789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1246789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1247789Sahrens 	uint64_t	seq = 0;
1248789Sahrens 	uint64_t	acl_obj, xattr_obj;
1249789Sahrens 	zfs_dirlock_t	*dl;
1250789Sahrens 	dmu_tx_t	*tx;
1251789Sahrens 	int		may_delete_now, delete_now = FALSE;
1252789Sahrens 	int		reaped;
1253789Sahrens 	int		error;
1254789Sahrens 
1255789Sahrens 	ZFS_ENTER(zfsvfs);
1256789Sahrens 
1257789Sahrens top:
1258789Sahrens 	/*
1259789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1260789Sahrens 	 */
1261789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
1262789Sahrens 		ZFS_EXIT(zfsvfs);
1263789Sahrens 		return (error);
1264789Sahrens 	}
1265789Sahrens 
1266789Sahrens 	vp = ZTOV(zp);
1267789Sahrens 
1268789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1269789Sahrens 		goto out;
1270789Sahrens 	}
1271789Sahrens 
1272789Sahrens 	/*
1273789Sahrens 	 * Need to use rmdir for removing directories.
1274789Sahrens 	 */
1275789Sahrens 	if (vp->v_type == VDIR) {
1276789Sahrens 		error = EPERM;
1277789Sahrens 		goto out;
1278789Sahrens 	}
1279789Sahrens 
1280789Sahrens 	vnevent_remove(vp);
1281789Sahrens 
12821484Sek110237 	dnlc_remove(dvp, name);
12831484Sek110237 
1284789Sahrens 	mutex_enter(&vp->v_lock);
1285789Sahrens 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1286789Sahrens 	mutex_exit(&vp->v_lock);
1287789Sahrens 
1288789Sahrens 	/*
1289789Sahrens 	 * We may delete the znode now, or we may put it on the delete queue;
1290789Sahrens 	 * it depends on whether we're the last link, and on whether there are
1291789Sahrens 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1292789Sahrens 	 * allow for either case.
1293789Sahrens 	 */
1294789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
12951544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1296789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
1297789Sahrens 	if (may_delete_now)
1298789Sahrens 		dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
1299789Sahrens 
1300789Sahrens 	/* are there any extended attributes? */
1301789Sahrens 	if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1302789Sahrens 		/*
1303789Sahrens 		 * XXX - There is a possibility that the delete
1304789Sahrens 		 * of the parent file could succeed, but then we get
1305789Sahrens 		 * an ENOSPC when we try to delete the xattrs...
1306789Sahrens 		 * so we would need to re-try the deletes periodically
1307789Sahrens 		 */
1308789Sahrens 		/* XXX - do we need this if we are deleting? */
1309789Sahrens 		dmu_tx_hold_bonus(tx, xattr_obj);
1310789Sahrens 	}
1311789Sahrens 
1312789Sahrens 	/* are there any additional acls */
1313789Sahrens 	if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1314789Sahrens 	    may_delete_now)
1315789Sahrens 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1316789Sahrens 
1317789Sahrens 	/* charge as an update -- would be nice not to charge at all */
13181544Seschrock 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
1319789Sahrens 
1320789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1321789Sahrens 	if (error) {
1322789Sahrens 		dmu_tx_abort(tx);
1323789Sahrens 		zfs_dirent_unlock(dl);
1324789Sahrens 		VN_RELE(vp);
1325789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1326789Sahrens 			txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0);
1327789Sahrens 			goto top;
1328789Sahrens 		}
1329789Sahrens 		ZFS_EXIT(zfsvfs);
1330789Sahrens 		return (error);
1331789Sahrens 	}
1332789Sahrens 
1333789Sahrens 	/*
1334789Sahrens 	 * Remove the directory entry.
1335789Sahrens 	 */
1336789Sahrens 	error = zfs_link_destroy(dl, zp, tx, 0, &reaped);
1337789Sahrens 
1338789Sahrens 	if (error) {
1339789Sahrens 		dmu_tx_commit(tx);
1340789Sahrens 		goto out;
1341789Sahrens 	}
1342789Sahrens 
1343789Sahrens 	if (reaped) {
1344789Sahrens 		mutex_enter(&vp->v_lock);
1345789Sahrens 		delete_now = may_delete_now &&
1346789Sahrens 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1347789Sahrens 		    zp->z_phys->zp_xattr == xattr_obj &&
1348789Sahrens 		    zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1349789Sahrens 		mutex_exit(&vp->v_lock);
1350789Sahrens 	}
1351789Sahrens 
1352789Sahrens 	if (delete_now) {
1353789Sahrens 		if (zp->z_phys->zp_xattr) {
1354789Sahrens 			error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1355789Sahrens 			ASSERT3U(error, ==, 0);
1356789Sahrens 			ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1357789Sahrens 			dmu_buf_will_dirty(xzp->z_dbuf, tx);
1358789Sahrens 			mutex_enter(&xzp->z_lock);
1359789Sahrens 			xzp->z_reap = 1;
1360789Sahrens 			xzp->z_phys->zp_links = 0;
1361789Sahrens 			mutex_exit(&xzp->z_lock);
1362789Sahrens 			zfs_dq_add(xzp, tx);
1363789Sahrens 			zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1364789Sahrens 		}
1365789Sahrens 		mutex_enter(&zp->z_lock);
1366789Sahrens 		mutex_enter(&vp->v_lock);
1367789Sahrens 		vp->v_count--;
1368789Sahrens 		ASSERT3U(vp->v_count, ==, 0);
1369789Sahrens 		mutex_exit(&vp->v_lock);
1370789Sahrens 		zp->z_active = 0;
1371789Sahrens 		mutex_exit(&zp->z_lock);
1372789Sahrens 		zfs_znode_delete(zp, tx);
1373789Sahrens 		VFS_RELE(zfsvfs->z_vfs);
1374789Sahrens 	} else if (reaped) {
1375789Sahrens 		zfs_dq_add(zp, tx);
1376789Sahrens 	}
1377789Sahrens 
1378789Sahrens 	seq = zfs_log_remove(zilog, tx, TX_REMOVE, dzp, name);
1379789Sahrens 
1380789Sahrens 	dmu_tx_commit(tx);
1381789Sahrens out:
1382789Sahrens 	zfs_dirent_unlock(dl);
1383789Sahrens 
1384789Sahrens 	if (!delete_now) {
1385789Sahrens 		VN_RELE(vp);
1386789Sahrens 	} else if (xzp) {
1387789Sahrens 		/* this rele delayed to prevent nesting transactions */
1388789Sahrens 		VN_RELE(ZTOV(xzp));
1389789Sahrens 	}
1390789Sahrens 
1391789Sahrens 	zil_commit(zilog, seq, 0);
1392789Sahrens 
1393789Sahrens 	ZFS_EXIT(zfsvfs);
1394789Sahrens 	return (error);
1395789Sahrens }
1396789Sahrens 
1397789Sahrens /*
1398789Sahrens  * Create a new directory and insert it into dvp using the name
1399789Sahrens  * provided.  Return a pointer to the inserted directory.
1400789Sahrens  *
1401789Sahrens  *	IN:	dvp	- vnode of directory to add subdir to.
1402789Sahrens  *		dirname	- name of new directory.
1403789Sahrens  *		vap	- attributes of new directory.
1404789Sahrens  *		cr	- credentials of caller.
1405789Sahrens  *
1406789Sahrens  *	OUT:	vpp	- vnode of created directory.
1407789Sahrens  *
1408789Sahrens  *	RETURN:	0 if success
1409789Sahrens  *		error code if failure
1410789Sahrens  *
1411789Sahrens  * Timestamps:
1412789Sahrens  *	dvp - ctime|mtime updated
1413789Sahrens  *	 vp - ctime|mtime|atime updated
1414789Sahrens  */
1415789Sahrens static int
1416789Sahrens zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr)
1417789Sahrens {
1418789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
1419789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1420789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1421789Sahrens 	uint64_t	seq = 0;
1422789Sahrens 	zfs_dirlock_t	*dl;
1423789Sahrens 	uint64_t	zoid = 0;
1424789Sahrens 	dmu_tx_t	*tx;
1425789Sahrens 	int		error;
1426789Sahrens 
1427789Sahrens 	ASSERT(vap->va_type == VDIR);
1428789Sahrens 
1429789Sahrens 	ZFS_ENTER(zfsvfs);
1430789Sahrens 
1431789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1432789Sahrens 		ZFS_EXIT(zfsvfs);
1433789Sahrens 		return (EINVAL);
1434789Sahrens 	}
1435789Sahrens top:
1436789Sahrens 	*vpp = NULL;
1437789Sahrens 
1438789Sahrens 	/*
1439789Sahrens 	 * First make sure the new directory doesn't exist.
1440789Sahrens 	 */
1441789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, ZNEW)) {
1442789Sahrens 		ZFS_EXIT(zfsvfs);
1443789Sahrens 		return (error);
1444789Sahrens 	}
1445789Sahrens 
14461231Smarks 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, cr)) {
14471231Smarks 		zfs_dirent_unlock(dl);
14481231Smarks 		ZFS_EXIT(zfsvfs);
14491231Smarks 		return (error);
14501231Smarks 	}
14511231Smarks 
1452789Sahrens 	/*
1453789Sahrens 	 * Add a new entry to the directory.
1454789Sahrens 	 */
1455789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
14561544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
14571544Seschrock 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1458789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
1459789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1460789Sahrens 		    0, SPA_MAXBLOCKSIZE);
1461789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1462789Sahrens 	if (error) {
1463789Sahrens 		dmu_tx_abort(tx);
1464789Sahrens 		zfs_dirent_unlock(dl);
1465789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1466789Sahrens 			txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0);
1467789Sahrens 			goto top;
1468789Sahrens 		}
1469789Sahrens 		ZFS_EXIT(zfsvfs);
1470789Sahrens 		return (error);
1471789Sahrens 	}
1472789Sahrens 
1473789Sahrens 	/*
1474789Sahrens 	 * Create new node.
1475789Sahrens 	 */
1476789Sahrens 	zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
1477789Sahrens 
1478789Sahrens 	/*
1479789Sahrens 	 * Now put new name in parent dir.
1480789Sahrens 	 */
1481789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1482789Sahrens 
1483789Sahrens 	*vpp = ZTOV(zp);
1484789Sahrens 
1485789Sahrens 	seq = zfs_log_create(zilog, tx, TX_MKDIR, dzp, zp, dirname);
1486789Sahrens 	dmu_tx_commit(tx);
1487789Sahrens 
1488789Sahrens 	zfs_dirent_unlock(dl);
1489789Sahrens 
1490789Sahrens 	zil_commit(zilog, seq, 0);
1491789Sahrens 
1492789Sahrens 	ZFS_EXIT(zfsvfs);
1493789Sahrens 	return (0);
1494789Sahrens }
1495789Sahrens 
1496789Sahrens /*
1497789Sahrens  * Remove a directory subdir entry.  If the current working
1498789Sahrens  * directory is the same as the subdir to be removed, the
1499789Sahrens  * remove will fail.
1500789Sahrens  *
1501789Sahrens  *	IN:	dvp	- vnode of directory to remove from.
1502789Sahrens  *		name	- name of directory to be removed.
1503789Sahrens  *		cwd	- vnode of current working directory.
1504789Sahrens  *		cr	- credentials of caller.
1505789Sahrens  *
1506789Sahrens  *	RETURN:	0 if success
1507789Sahrens  *		error code if failure
1508789Sahrens  *
1509789Sahrens  * Timestamps:
1510789Sahrens  *	dvp - ctime|mtime updated
1511789Sahrens  */
1512789Sahrens static int
1513789Sahrens zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr)
1514789Sahrens {
1515789Sahrens 	znode_t		*dzp = VTOZ(dvp);
1516789Sahrens 	znode_t		*zp;
1517789Sahrens 	vnode_t		*vp;
1518789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1519789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1520789Sahrens 	uint64_t	seq = 0;
1521789Sahrens 	zfs_dirlock_t	*dl;
1522789Sahrens 	dmu_tx_t	*tx;
1523789Sahrens 	int		error;
1524789Sahrens 
1525789Sahrens 	ZFS_ENTER(zfsvfs);
1526789Sahrens 
1527789Sahrens top:
1528789Sahrens 	zp = NULL;
1529789Sahrens 
1530789Sahrens 	/*
1531789Sahrens 	 * Attempt to lock directory; fail if entry doesn't exist.
1532789Sahrens 	 */
1533789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
1534789Sahrens 		ZFS_EXIT(zfsvfs);
1535789Sahrens 		return (error);
1536789Sahrens 	}
1537789Sahrens 
1538789Sahrens 	vp = ZTOV(zp);
1539789Sahrens 
1540789Sahrens 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1541789Sahrens 		goto out;
1542789Sahrens 	}
1543789Sahrens 
1544789Sahrens 	if (vp->v_type != VDIR) {
1545789Sahrens 		error = ENOTDIR;
1546789Sahrens 		goto out;
1547789Sahrens 	}
1548789Sahrens 
1549789Sahrens 	if (vp == cwd) {
1550789Sahrens 		error = EINVAL;
1551789Sahrens 		goto out;
1552789Sahrens 	}
1553789Sahrens 
1554789Sahrens 	vnevent_rmdir(vp);
1555789Sahrens 
1556789Sahrens 	/*
1557789Sahrens 	 * Grab a lock on the parent pointer make sure we play well
1558789Sahrens 	 * with the treewalk and directory rename code.
1559789Sahrens 	 */
1560789Sahrens 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1561789Sahrens 
1562789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
15631544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1564789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
15651544Seschrock 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
1566789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1567789Sahrens 	if (error) {
1568789Sahrens 		dmu_tx_abort(tx);
1569789Sahrens 		rw_exit(&zp->z_parent_lock);
1570789Sahrens 		zfs_dirent_unlock(dl);
1571789Sahrens 		VN_RELE(vp);
1572789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1573789Sahrens 			txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0);
1574789Sahrens 			goto top;
1575789Sahrens 		}
1576789Sahrens 		ZFS_EXIT(zfsvfs);
1577789Sahrens 		return (error);
1578789Sahrens 	}
1579789Sahrens 
1580789Sahrens 	error = zfs_link_destroy(dl, zp, tx, 0, NULL);
1581789Sahrens 
1582789Sahrens 	if (error == 0)
1583789Sahrens 		seq = zfs_log_remove(zilog, tx, TX_RMDIR, dzp, name);
1584789Sahrens 
1585789Sahrens 	dmu_tx_commit(tx);
1586789Sahrens 
1587789Sahrens 	rw_exit(&zp->z_parent_lock);
1588789Sahrens out:
1589789Sahrens 	zfs_dirent_unlock(dl);
1590789Sahrens 
1591789Sahrens 	VN_RELE(vp);
1592789Sahrens 
1593789Sahrens 	zil_commit(zilog, seq, 0);
1594789Sahrens 
1595789Sahrens 	ZFS_EXIT(zfsvfs);
1596789Sahrens 	return (error);
1597789Sahrens }
1598789Sahrens 
1599789Sahrens /*
1600789Sahrens  * Read as many directory entries as will fit into the provided
1601789Sahrens  * buffer from the given directory cursor position (specified in
1602789Sahrens  * the uio structure.
1603789Sahrens  *
1604789Sahrens  *	IN:	vp	- vnode of directory to read.
1605789Sahrens  *		uio	- structure supplying read location, range info,
1606789Sahrens  *			  and return buffer.
1607789Sahrens  *		cr	- credentials of caller.
1608789Sahrens  *
1609789Sahrens  *	OUT:	uio	- updated offset and range, buffer filled.
1610789Sahrens  *		eofp	- set to true if end-of-file detected.
1611789Sahrens  *
1612789Sahrens  *	RETURN:	0 if success
1613789Sahrens  *		error code if failure
1614789Sahrens  *
1615789Sahrens  * Timestamps:
1616789Sahrens  *	vp - atime updated
1617789Sahrens  *
1618789Sahrens  * Note that the low 4 bits of the cookie returned by zap is always zero.
1619789Sahrens  * This allows us to use the low range for "special" directory entries:
1620789Sahrens  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
1621789Sahrens  * we use the offset 2 for the '.zfs' directory.
1622789Sahrens  */
1623789Sahrens /* ARGSUSED */
1624789Sahrens static int
1625789Sahrens zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp)
1626789Sahrens {
1627789Sahrens 	znode_t		*zp = VTOZ(vp);
1628789Sahrens 	iovec_t		*iovp;
1629789Sahrens 	dirent64_t	*odp;
1630789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1631869Sperrin 	objset_t	*os;
1632789Sahrens 	caddr_t		outbuf;
1633789Sahrens 	size_t		bufsize;
1634789Sahrens 	zap_cursor_t	zc;
1635789Sahrens 	zap_attribute_t	zap;
1636789Sahrens 	uint_t		bytes_wanted;
1637789Sahrens 	ushort_t	this_reclen;
1638789Sahrens 	uint64_t	offset; /* must be unsigned; checks for < 1 */
1639789Sahrens 	off64_t		*next;
1640789Sahrens 	int		local_eof;
1641869Sperrin 	int		outcount;
1642869Sperrin 	int		error;
1643869Sperrin 	uint8_t		prefetch;
1644789Sahrens 
1645789Sahrens 	ZFS_ENTER(zfsvfs);
1646789Sahrens 
1647789Sahrens 	/*
1648789Sahrens 	 * If we are not given an eof variable,
1649789Sahrens 	 * use a local one.
1650789Sahrens 	 */
1651789Sahrens 	if (eofp == NULL)
1652789Sahrens 		eofp = &local_eof;
1653789Sahrens 
1654789Sahrens 	/*
1655789Sahrens 	 * Check for valid iov_len.
1656789Sahrens 	 */
1657789Sahrens 	if (uio->uio_iov->iov_len <= 0) {
1658789Sahrens 		ZFS_EXIT(zfsvfs);
1659789Sahrens 		return (EINVAL);
1660789Sahrens 	}
1661789Sahrens 
1662789Sahrens 	/*
1663789Sahrens 	 * Quit if directory has been removed (posix)
1664789Sahrens 	 */
1665789Sahrens 	if ((*eofp = zp->z_reap) != 0) {
1666789Sahrens 		ZFS_EXIT(zfsvfs);
1667789Sahrens 		return (0);
1668789Sahrens 	}
1669789Sahrens 
1670869Sperrin 	error = 0;
1671869Sperrin 	os = zfsvfs->z_os;
1672869Sperrin 	offset = uio->uio_loffset;
1673869Sperrin 	prefetch = zp->z_zn_prefetch;
1674869Sperrin 
1675789Sahrens 	/*
1676789Sahrens 	 * Initialize the iterator cursor.
1677789Sahrens 	 */
1678789Sahrens 	if (offset <= 3) {
1679789Sahrens 		/*
1680789Sahrens 		 * Start iteration from the beginning of the directory.
1681789Sahrens 		 */
1682869Sperrin 		zap_cursor_init(&zc, os, zp->z_id);
1683789Sahrens 	} else {
1684789Sahrens 		/*
1685789Sahrens 		 * The offset is a serialized cursor.
1686789Sahrens 		 */
1687869Sperrin 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1688789Sahrens 	}
1689789Sahrens 
1690789Sahrens 	/*
1691789Sahrens 	 * Get space to change directory entries into fs independent format.
1692789Sahrens 	 */
1693789Sahrens 	iovp = uio->uio_iov;
1694789Sahrens 	bytes_wanted = iovp->iov_len;
1695789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
1696789Sahrens 		bufsize = bytes_wanted;
1697789Sahrens 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
1698789Sahrens 		odp = (struct dirent64 *)outbuf;
1699789Sahrens 	} else {
1700789Sahrens 		bufsize = bytes_wanted;
1701789Sahrens 		odp = (struct dirent64 *)iovp->iov_base;
1702789Sahrens 	}
1703789Sahrens 
1704789Sahrens 	/*
1705789Sahrens 	 * Transform to file-system independent format
1706789Sahrens 	 */
1707789Sahrens 	outcount = 0;
1708789Sahrens 	while (outcount < bytes_wanted) {
1709789Sahrens 		/*
1710789Sahrens 		 * Special case `.', `..', and `.zfs'.
1711789Sahrens 		 */
1712789Sahrens 		if (offset == 0) {
1713789Sahrens 			(void) strcpy(zap.za_name, ".");
1714789Sahrens 			zap.za_first_integer = zp->z_id;
1715789Sahrens 			this_reclen = DIRENT64_RECLEN(1);
1716789Sahrens 		} else if (offset == 1) {
1717789Sahrens 			(void) strcpy(zap.za_name, "..");
1718789Sahrens 			zap.za_first_integer = zp->z_phys->zp_parent;
1719789Sahrens 			this_reclen = DIRENT64_RECLEN(2);
1720789Sahrens 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
1721789Sahrens 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
1722789Sahrens 			zap.za_first_integer = ZFSCTL_INO_ROOT;
1723789Sahrens 			this_reclen =
1724789Sahrens 			    DIRENT64_RECLEN(sizeof (ZFS_CTLDIR_NAME) - 1);
1725789Sahrens 		} else {
1726789Sahrens 			/*
1727789Sahrens 			 * Grab next entry.
1728789Sahrens 			 */
1729789Sahrens 			if (error = zap_cursor_retrieve(&zc, &zap)) {
1730789Sahrens 				if ((*eofp = (error == ENOENT)) != 0)
1731789Sahrens 					break;
1732789Sahrens 				else
1733789Sahrens 					goto update;
1734789Sahrens 			}
1735789Sahrens 
1736789Sahrens 			if (zap.za_integer_length != 8 ||
1737789Sahrens 			    zap.za_num_integers != 1) {
1738789Sahrens 				cmn_err(CE_WARN, "zap_readdir: bad directory "
1739789Sahrens 				    "entry, obj = %lld, offset = %lld\n",
1740789Sahrens 				    (u_longlong_t)zp->z_id,
1741789Sahrens 				    (u_longlong_t)offset);
1742789Sahrens 				error = ENXIO;
1743789Sahrens 				goto update;
1744789Sahrens 			}
1745789Sahrens 			this_reclen = DIRENT64_RECLEN(strlen(zap.za_name));
1746789Sahrens 		}
1747789Sahrens 
1748789Sahrens 		/*
1749789Sahrens 		 * Will this entry fit in the buffer?
1750789Sahrens 		 */
1751789Sahrens 		if (outcount + this_reclen > bufsize) {
1752789Sahrens 			/*
1753789Sahrens 			 * Did we manage to fit anything in the buffer?
1754789Sahrens 			 */
1755789Sahrens 			if (!outcount) {
1756789Sahrens 				error = EINVAL;
1757789Sahrens 				goto update;
1758789Sahrens 			}
1759789Sahrens 			break;
1760789Sahrens 		}
1761789Sahrens 		/*
1762789Sahrens 		 * Add this entry:
1763789Sahrens 		 */
1764789Sahrens 		odp->d_ino = (ino64_t)zap.za_first_integer;
1765789Sahrens 		odp->d_reclen = (ushort_t)this_reclen;
1766789Sahrens 		/* NOTE: d_off is the offset for the *next* entry */
1767789Sahrens 		next = &(odp->d_off);
1768789Sahrens 		(void) strncpy(odp->d_name, zap.za_name,
1769789Sahrens 		    DIRENT64_NAMELEN(this_reclen));
1770789Sahrens 		outcount += this_reclen;
1771789Sahrens 		odp = (dirent64_t *)((intptr_t)odp + this_reclen);
1772789Sahrens 
1773789Sahrens 		ASSERT(outcount <= bufsize);
1774789Sahrens 
1775789Sahrens 		/* Prefetch znode */
1776869Sperrin 		if (prefetch)
1777869Sperrin 			dmu_prefetch(os, zap.za_first_integer, 0, 0);
1778789Sahrens 
1779789Sahrens 		/*
1780789Sahrens 		 * Move to the next entry, fill in the previous offset.
1781789Sahrens 		 */
1782789Sahrens 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
1783789Sahrens 			zap_cursor_advance(&zc);
1784789Sahrens 			offset = zap_cursor_serialize(&zc);
1785789Sahrens 		} else {
1786789Sahrens 			offset += 1;
1787789Sahrens 		}
1788789Sahrens 		*next = offset;
1789789Sahrens 	}
1790869Sperrin 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
1791789Sahrens 
1792789Sahrens 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
1793789Sahrens 		iovp->iov_base += outcount;
1794789Sahrens 		iovp->iov_len -= outcount;
1795789Sahrens 		uio->uio_resid -= outcount;
1796789Sahrens 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
1797789Sahrens 		/*
1798789Sahrens 		 * Reset the pointer.
1799789Sahrens 		 */
1800789Sahrens 		offset = uio->uio_loffset;
1801789Sahrens 	}
1802789Sahrens 
1803789Sahrens update:
1804885Sahrens 	zap_cursor_fini(&zc);
1805789Sahrens 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1806789Sahrens 		kmem_free(outbuf, bufsize);
1807789Sahrens 
1808789Sahrens 	if (error == ENOENT)
1809789Sahrens 		error = 0;
1810789Sahrens 
1811789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
1812789Sahrens 
1813789Sahrens 	uio->uio_loffset = offset;
1814789Sahrens 	ZFS_EXIT(zfsvfs);
1815789Sahrens 	return (error);
1816789Sahrens }
1817789Sahrens 
1818789Sahrens static int
1819789Sahrens zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr)
1820789Sahrens {
1821789Sahrens 	znode_t	*zp = VTOZ(vp);
1822789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1823789Sahrens 
18241773Seschrock 	/*
18251773Seschrock 	 * Regardless of whether this is required for standards conformance,
18261773Seschrock 	 * this is the logical behavior when fsync() is called on a file with
18271773Seschrock 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
18281773Seschrock 	 * going to be pushed out as part of the zil_commit().
18291773Seschrock 	 */
18301773Seschrock 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
18311773Seschrock 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
18321773Seschrock 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr);
18331773Seschrock 
1834789Sahrens 	ZFS_ENTER(zfsvfs);
1835789Sahrens 	zil_commit(zfsvfs->z_log, zp->z_last_itx, FSYNC);
1836789Sahrens 	ZFS_EXIT(zfsvfs);
1837789Sahrens 	return (0);
1838789Sahrens }
1839789Sahrens 
1840789Sahrens /*
1841789Sahrens  * Get the requested file attributes and place them in the provided
1842789Sahrens  * vattr structure.
1843789Sahrens  *
1844789Sahrens  *	IN:	vp	- vnode of file.
1845789Sahrens  *		vap	- va_mask identifies requested attributes.
1846789Sahrens  *		flags	- [UNUSED]
1847789Sahrens  *		cr	- credentials of caller.
1848789Sahrens  *
1849789Sahrens  *	OUT:	vap	- attribute values.
1850789Sahrens  *
1851789Sahrens  *	RETURN:	0 (always succeeds)
1852789Sahrens  */
1853789Sahrens /* ARGSUSED */
1854789Sahrens static int
1855789Sahrens zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
1856789Sahrens {
1857789Sahrens 	znode_t *zp = VTOZ(vp);
1858789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1859789Sahrens 	znode_phys_t *pzp = zp->z_phys;
1860789Sahrens 	int	error;
1861789Sahrens 
1862789Sahrens 	ZFS_ENTER(zfsvfs);
1863789Sahrens 
1864789Sahrens 	/*
1865789Sahrens 	 * Return all attributes.  It's cheaper to provide the answer
1866789Sahrens 	 * than to determine whether we were asked the question.
1867789Sahrens 	 */
1868789Sahrens 	mutex_enter(&zp->z_lock);
1869789Sahrens 
1870789Sahrens 	vap->va_type = vp->v_type;
1871789Sahrens 	vap->va_mode = pzp->zp_mode & MODEMASK;
1872789Sahrens 	vap->va_uid = zp->z_phys->zp_uid;
1873789Sahrens 	vap->va_gid = zp->z_phys->zp_gid;
1874789Sahrens 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
1875789Sahrens 	vap->va_nodeid = zp->z_id;
1876789Sahrens 	vap->va_nlink = MIN(pzp->zp_links, UINT32_MAX);	/* nlink_t limit! */
1877789Sahrens 	vap->va_size = pzp->zp_size;
1878*1816Smarks 	vap->va_rdev = vp->v_rdev;
1879789Sahrens 	vap->va_seq = zp->z_seq;
1880789Sahrens 
1881789Sahrens 	ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
1882789Sahrens 	ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
1883789Sahrens 	ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
1884789Sahrens 
1885789Sahrens 	/*
1886905Smarks 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
1887905Smarks 	 * Also, if we are the owner don't bother, since owner should
1888905Smarks 	 * always be allowed to read basic attributes of file.
1889789Sahrens 	 */
1890905Smarks 	if (!(zp->z_phys->zp_flags & ZFS_ACL_TRIVIAL) &&
1891905Smarks 	    (zp->z_phys->zp_uid != crgetuid(cr))) {
1892905Smarks 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, cr)) {
1893789Sahrens 			mutex_exit(&zp->z_lock);
1894789Sahrens 			ZFS_EXIT(zfsvfs);
1895789Sahrens 			return (error);
1896789Sahrens 		}
1897789Sahrens 	}
1898789Sahrens 
1899789Sahrens 	mutex_exit(&zp->z_lock);
1900789Sahrens 
1901789Sahrens 	dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks);
1902789Sahrens 
1903789Sahrens 	if (zp->z_blksz == 0) {
1904789Sahrens 		/*
1905789Sahrens 		 * Block size hasn't been set; suggest maximal I/O transfers.
1906789Sahrens 		 */
1907789Sahrens 		vap->va_blksize = zfsvfs->z_max_blksz;
1908789Sahrens 	}
1909789Sahrens 
1910789Sahrens 	ZFS_EXIT(zfsvfs);
1911789Sahrens 	return (0);
1912789Sahrens }
1913789Sahrens 
1914789Sahrens /*
1915789Sahrens  * Set the file attributes to the values contained in the
1916789Sahrens  * vattr structure.
1917789Sahrens  *
1918789Sahrens  *	IN:	vp	- vnode of file to be modified.
1919789Sahrens  *		vap	- new attribute values.
1920789Sahrens  *		flags	- ATTR_UTIME set if non-default time values provided.
1921789Sahrens  *		cr	- credentials of caller.
1922789Sahrens  *
1923789Sahrens  *	RETURN:	0 if success
1924789Sahrens  *		error code if failure
1925789Sahrens  *
1926789Sahrens  * Timestamps:
1927789Sahrens  *	vp - ctime updated, mtime updated if size changed.
1928789Sahrens  */
1929789Sahrens /* ARGSUSED */
1930789Sahrens static int
1931789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1932789Sahrens 	caller_context_t *ct)
1933789Sahrens {
1934789Sahrens 	struct znode	*zp = VTOZ(vp);
1935789Sahrens 	znode_phys_t	*pzp = zp->z_phys;
1936789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1937789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
1938789Sahrens 	uint64_t	seq = 0;
1939789Sahrens 	dmu_tx_t	*tx;
19401669Sperrin 	rl_t		*rl;
1941789Sahrens 	uint_t		mask = vap->va_mask;
1942789Sahrens 	uint_t		mask_applied = 0;
1943789Sahrens 	vattr_t		oldva;
19441115Smarks 	int		trim_mask = FALSE;
19451115Smarks 	int		saved_mask;
1946789Sahrens 	uint64_t	new_mode;
19471231Smarks 	znode_t		*attrzp;
1948789Sahrens 	int		need_policy = FALSE;
1949789Sahrens 	int		err;
1950789Sahrens 
1951789Sahrens 	if (mask == 0)
1952789Sahrens 		return (0);
1953789Sahrens 
1954789Sahrens 	if (mask & AT_NOSET)
1955789Sahrens 		return (EINVAL);
1956789Sahrens 
1957789Sahrens 	if (mask & AT_SIZE && vp->v_type == VDIR)
1958789Sahrens 		return (EISDIR);
1959789Sahrens 
19601394Smarks 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO)
19611308Smarks 		return (EINVAL);
19621308Smarks 
1963789Sahrens 	ZFS_ENTER(zfsvfs);
1964789Sahrens 
1965789Sahrens top:
19661669Sperrin 	rl = NULL;
19671231Smarks 	attrzp = NULL;
1968789Sahrens 
1969789Sahrens 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
1970789Sahrens 		ZFS_EXIT(zfsvfs);
1971789Sahrens 		return (EROFS);
1972789Sahrens 	}
1973789Sahrens 
1974789Sahrens 	/*
1975789Sahrens 	 * First validate permissions
1976789Sahrens 	 */
1977789Sahrens 
1978789Sahrens 	if (mask & AT_SIZE) {
1979789Sahrens 		err = zfs_zaccess(zp, ACE_WRITE_DATA, cr);
1980789Sahrens 		if (err) {
1981789Sahrens 			ZFS_EXIT(zfsvfs);
1982789Sahrens 			return (err);
1983789Sahrens 		}
1984789Sahrens 	}
1985789Sahrens 
1986789Sahrens 	if (mask & (AT_ATIME|AT_MTIME))
1987789Sahrens 		need_policy = zfs_zaccess_v4_perm(zp, ACE_WRITE_ATTRIBUTES, cr);
1988789Sahrens 
1989789Sahrens 	if (mask & (AT_UID|AT_GID)) {
1990789Sahrens 		int	idmask = (mask & (AT_UID|AT_GID));
1991789Sahrens 		int	take_owner;
1992789Sahrens 		int	take_group;
1993789Sahrens 
1994789Sahrens 		/*
1995913Smarks 		 * NOTE: even if a new mode is being set,
1996913Smarks 		 * we may clear S_ISUID/S_ISGID bits.
1997913Smarks 		 */
1998913Smarks 
1999913Smarks 		if (!(mask & AT_MODE))
2000913Smarks 			vap->va_mode = pzp->zp_mode;
2001913Smarks 
2002913Smarks 		/*
2003789Sahrens 		 * Take ownership or chgrp to group we are a member of
2004789Sahrens 		 */
2005789Sahrens 
2006789Sahrens 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2007789Sahrens 		take_group = (mask & AT_GID) && groupmember(vap->va_gid, cr);
2008789Sahrens 
2009789Sahrens 		/*
2010789Sahrens 		 * If both AT_UID and AT_GID are set then take_owner and
2011789Sahrens 		 * take_group must both be set in order to allow taking
2012789Sahrens 		 * ownership.
2013789Sahrens 		 *
2014789Sahrens 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2015789Sahrens 		 *
2016789Sahrens 		 */
2017789Sahrens 
2018789Sahrens 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2019789Sahrens 		    ((idmask == AT_UID) && take_owner) ||
2020789Sahrens 		    ((idmask == AT_GID) && take_group)) {
2021789Sahrens 			if (zfs_zaccess_v4_perm(zp, ACE_WRITE_OWNER, cr) == 0) {
2022789Sahrens 				/*
2023789Sahrens 				 * Remove setuid/setgid for non-privileged users
2024789Sahrens 				 */
20251115Smarks 				secpolicy_setid_clear(vap, cr);
20261115Smarks 				trim_mask = TRUE;
20271115Smarks 				saved_mask = vap->va_mask;
2028789Sahrens 			} else {
2029789Sahrens 				need_policy =  TRUE;
2030789Sahrens 			}
2031789Sahrens 		} else {
2032789Sahrens 			need_policy =  TRUE;
2033789Sahrens 		}
2034789Sahrens 	}
2035789Sahrens 
2036789Sahrens 	if (mask & AT_MODE)
2037789Sahrens 		need_policy = TRUE;
2038789Sahrens 
2039789Sahrens 	if (need_policy) {
2040789Sahrens 		mutex_enter(&zp->z_lock);
2041789Sahrens 		oldva.va_mode = pzp->zp_mode;
2042789Sahrens 		oldva.va_uid = zp->z_phys->zp_uid;
2043789Sahrens 		oldva.va_gid = zp->z_phys->zp_gid;
2044789Sahrens 		mutex_exit(&zp->z_lock);
20451115Smarks 
20461115Smarks 		/*
20471115Smarks 		 * If trim_mask is set then take ownership
20481115Smarks 		 * has been granted.  In that case remove
20491115Smarks 		 * UID|GID from mask so that
20501115Smarks 		 * secpolicy_vnode_setattr() doesn't revoke it.
20511115Smarks 		 */
20521115Smarks 		if (trim_mask)
20531115Smarks 			vap->va_mask &= ~(AT_UID|AT_GID);
20541115Smarks 
2055789Sahrens 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2056789Sahrens 		    (int (*)(void *, int, cred_t *))zfs_zaccess_rwx, zp);
2057789Sahrens 		if (err) {
2058789Sahrens 			ZFS_EXIT(zfsvfs);
2059789Sahrens 			return (err);
2060789Sahrens 		}
20611115Smarks 
20621115Smarks 		if (trim_mask)
20631115Smarks 			vap->va_mask |= (saved_mask & (AT_UID|AT_GID));
2064789Sahrens 	}
2065789Sahrens 
2066789Sahrens 	/*
2067789Sahrens 	 * secpolicy_vnode_setattr, or take ownership may have
2068789Sahrens 	 * changed va_mask
2069789Sahrens 	 */
2070789Sahrens 	mask = vap->va_mask;
2071789Sahrens 
2072789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2073789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
2074789Sahrens 
2075789Sahrens 	if (mask & AT_MODE) {
20761576Smarks 		uint64_t pmode = pzp->zp_mode;
20771576Smarks 
20781576Smarks 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2079789Sahrens 
2080789Sahrens 		if (zp->z_phys->zp_acl.z_acl_extern_obj)
2081789Sahrens 			dmu_tx_hold_write(tx,
2082789Sahrens 			    pzp->zp_acl.z_acl_extern_obj, 0, SPA_MAXBLOCKSIZE);
2083789Sahrens 		else
2084789Sahrens 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2085789Sahrens 			    0, ZFS_ACL_SIZE(MAX_ACL_SIZE));
2086789Sahrens 	}
2087789Sahrens 
2088789Sahrens 	if (mask & AT_SIZE) {
2089789Sahrens 		uint64_t off = vap->va_size;
2090789Sahrens 		/*
20911669Sperrin 		 * Range lock the entire file, to ensure the truncate
20921669Sperrin 		 * is serialised.
2093789Sahrens 		 */
20941669Sperrin 		rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
20951669Sperrin 		ASSERT(rl != NULL);
2096789Sahrens 		if (off < zp->z_phys->zp_size)
2097789Sahrens 			dmu_tx_hold_free(tx, zp->z_id, off, DMU_OBJECT_END);
20981544Seschrock 		else if (zp->z_blksz < zfsvfs->z_max_blksz && off > zp->z_blksz)
2099789Sahrens 			/* we will rewrite this block if we grow */
2100789Sahrens 			dmu_tx_hold_write(tx, zp->z_id, 0, zp->z_phys->zp_size);
2101789Sahrens 	}
2102789Sahrens 
21031231Smarks 	if ((mask & (AT_UID | AT_GID)) && zp->z_phys->zp_xattr != 0) {
21041231Smarks 		err = zfs_zget(zp->z_zfsvfs, zp->z_phys->zp_xattr, &attrzp);
21051231Smarks 		if (err) {
21061231Smarks 			dmu_tx_abort(tx);
21071669Sperrin 			if (rl != NULL)
21081669Sperrin 				zfs_range_unlock(zp, rl);
21091231Smarks 			ZFS_EXIT(zfsvfs);
21101231Smarks 			return (err);
21111231Smarks 		}
21121231Smarks 		dmu_tx_hold_bonus(tx, attrzp->z_id);
21131231Smarks 	}
21141231Smarks 
2115789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2116789Sahrens 	if (err) {
21171231Smarks 		if (attrzp)
21181231Smarks 			VN_RELE(ZTOV(attrzp));
2119789Sahrens 		dmu_tx_abort(tx);
21201669Sperrin 		if (rl != NULL)
21211669Sperrin 			zfs_range_unlock(zp, rl);
2122789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2123789Sahrens 			txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0);
2124789Sahrens 			goto top;
2125789Sahrens 		}
2126789Sahrens 		ZFS_EXIT(zfsvfs);
2127789Sahrens 		return (err);
2128789Sahrens 	}
2129789Sahrens 
2130789Sahrens 	dmu_buf_will_dirty(zp->z_dbuf, tx);
2131789Sahrens 
2132789Sahrens 	/*
2133789Sahrens 	 * Set each attribute requested.
2134789Sahrens 	 * We group settings according to the locks they need to acquire.
2135789Sahrens 	 *
2136789Sahrens 	 * Note: you cannot set ctime directly, although it will be
2137789Sahrens 	 * updated as a side-effect of calling this function.
2138789Sahrens 	 */
2139789Sahrens 	if (mask & AT_SIZE) {
2140789Sahrens 		/*
2141789Sahrens 		 * XXX - Note, we are not providing any open
2142789Sahrens 		 * mode flags here (like FNDELAY), so we may
2143789Sahrens 		 * block if there are locks present... this
2144789Sahrens 		 * should be addressed in openat().
2145789Sahrens 		 */
2146789Sahrens 		err = zfs_freesp(zp, vap->va_size, 0, 0, tx, cr);
2147789Sahrens 		if (err) {
2148789Sahrens 			mutex_enter(&zp->z_lock);
2149789Sahrens 			goto out;
2150789Sahrens 		}
2151789Sahrens 		mask_applied |= AT_SIZE;
2152789Sahrens 	}
2153789Sahrens 
2154789Sahrens 	mask_applied = mask;	/* no errors after this point */
2155789Sahrens 
2156789Sahrens 	mutex_enter(&zp->z_lock);
2157789Sahrens 
2158789Sahrens 	if (mask & AT_MODE) {
2159789Sahrens 		err = zfs_acl_chmod_setattr(zp, new_mode, tx);
2160789Sahrens 		ASSERT3U(err, ==, 0);
2161789Sahrens 	}
2162789Sahrens 
21631231Smarks 	if (attrzp)
21641231Smarks 		mutex_enter(&attrzp->z_lock);
21651231Smarks 
21661231Smarks 	if (mask & AT_UID) {
2167789Sahrens 		zp->z_phys->zp_uid = (uint64_t)vap->va_uid;
21681231Smarks 		if (attrzp) {
21691231Smarks 			attrzp->z_phys->zp_uid = (uint64_t)vap->va_uid;
21701231Smarks 		}
21711231Smarks 	}
21721231Smarks 
21731231Smarks 	if (mask & AT_GID) {
2174789Sahrens 		zp->z_phys->zp_gid = (uint64_t)vap->va_gid;
21751231Smarks 		if (attrzp)
21761231Smarks 			attrzp->z_phys->zp_gid = (uint64_t)vap->va_gid;
21771231Smarks 	}
21781231Smarks 
21791231Smarks 	if (attrzp)
21801231Smarks 		mutex_exit(&attrzp->z_lock);
2181789Sahrens 
2182789Sahrens 	if (mask & AT_ATIME)
2183789Sahrens 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
2184789Sahrens 
2185789Sahrens 	if (mask & AT_MTIME)
2186789Sahrens 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
2187789Sahrens 
2188789Sahrens 	if (mask_applied & AT_SIZE)
2189789Sahrens 		zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
2190789Sahrens 	else if (mask_applied != 0)
2191789Sahrens 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
2192789Sahrens 
2193789Sahrens out:
21941231Smarks 
2195789Sahrens 	if (mask_applied != 0)
2196789Sahrens 		seq = zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap,
2197789Sahrens 		    mask_applied);
2198789Sahrens 
2199789Sahrens 	mutex_exit(&zp->z_lock);
2200789Sahrens 
22011231Smarks 	if (attrzp)
22021231Smarks 		VN_RELE(ZTOV(attrzp));
22031231Smarks 
22041669Sperrin 	if (rl != NULL)
22051669Sperrin 		zfs_range_unlock(zp, rl);
2206789Sahrens 
2207789Sahrens 	dmu_tx_commit(tx);
2208789Sahrens 
2209789Sahrens 	zil_commit(zilog, seq, 0);
2210789Sahrens 
2211789Sahrens 	ZFS_EXIT(zfsvfs);
2212789Sahrens 	return (err);
2213789Sahrens }
2214789Sahrens 
2215789Sahrens /*
2216789Sahrens  * Search back through the directory tree, using the ".." entries.
2217789Sahrens  * Lock each directory in the chain to prevent concurrent renames.
2218789Sahrens  * Fail any attempt to move a directory into one of its own descendants.
2219789Sahrens  * XXX - z_parent_lock can overlap with map or grow locks
2220789Sahrens  */
2221789Sahrens typedef struct zfs_zlock {
2222789Sahrens 	krwlock_t	*zl_rwlock;	/* lock we acquired */
2223789Sahrens 	znode_t		*zl_znode;	/* znode we held */
2224789Sahrens 	struct zfs_zlock *zl_next;	/* next in list */
2225789Sahrens } zfs_zlock_t;
2226789Sahrens 
2227789Sahrens static int
2228789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
2229789Sahrens {
2230789Sahrens 	zfs_zlock_t	*zl;
2231789Sahrens 	znode_t 	*zp = tdzp;
2232789Sahrens 	uint64_t	rootid = zp->z_zfsvfs->z_root;
2233789Sahrens 	uint64_t	*oidp = &zp->z_id;
2234789Sahrens 	krwlock_t	*rwlp = &szp->z_parent_lock;
2235789Sahrens 	krw_t		rw = RW_WRITER;
2236789Sahrens 
2237789Sahrens 	/*
2238789Sahrens 	 * First pass write-locks szp and compares to zp->z_id.
2239789Sahrens 	 * Later passes read-lock zp and compare to zp->z_parent.
2240789Sahrens 	 */
2241789Sahrens 	do {
2242789Sahrens 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
2243789Sahrens 		zl->zl_rwlock = rwlp;
2244789Sahrens 		zl->zl_znode = NULL;
2245789Sahrens 		zl->zl_next = *zlpp;
2246789Sahrens 		*zlpp = zl;
2247789Sahrens 
2248789Sahrens 		rw_enter(rwlp, rw);
2249789Sahrens 
2250789Sahrens 		if (*oidp == szp->z_id)		/* We're a descendant of szp */
2251789Sahrens 			return (EINVAL);
2252789Sahrens 
2253789Sahrens 		if (*oidp == rootid)		/* We've hit the top */
2254789Sahrens 			return (0);
2255789Sahrens 
2256789Sahrens 		if (rw == RW_READER) {		/* i.e. not the first pass */
2257789Sahrens 			int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
2258789Sahrens 			if (error)
2259789Sahrens 				return (error);
2260789Sahrens 			zl->zl_znode = zp;
2261789Sahrens 		}
2262789Sahrens 		oidp = &zp->z_phys->zp_parent;
2263789Sahrens 		rwlp = &zp->z_parent_lock;
2264789Sahrens 		rw = RW_READER;
2265789Sahrens 
2266789Sahrens 	} while (zp->z_id != sdzp->z_id);
2267789Sahrens 
2268789Sahrens 	return (0);
2269789Sahrens }
2270789Sahrens 
2271789Sahrens /*
2272789Sahrens  * Drop locks and release vnodes that were held by zfs_rename_lock().
2273789Sahrens  */
2274789Sahrens static void
2275789Sahrens zfs_rename_unlock(zfs_zlock_t **zlpp)
2276789Sahrens {
2277789Sahrens 	zfs_zlock_t *zl;
2278789Sahrens 
2279789Sahrens 	while ((zl = *zlpp) != NULL) {
2280789Sahrens 		if (zl->zl_znode != NULL)
2281789Sahrens 			VN_RELE(ZTOV(zl->zl_znode));
2282789Sahrens 		rw_exit(zl->zl_rwlock);
2283789Sahrens 		*zlpp = zl->zl_next;
2284789Sahrens 		kmem_free(zl, sizeof (*zl));
2285789Sahrens 	}
2286789Sahrens }
2287789Sahrens 
2288789Sahrens /*
2289789Sahrens  * Move an entry from the provided source directory to the target
2290789Sahrens  * directory.  Change the entry name as indicated.
2291789Sahrens  *
2292789Sahrens  *	IN:	sdvp	- Source directory containing the "old entry".
2293789Sahrens  *		snm	- Old entry name.
2294789Sahrens  *		tdvp	- Target directory to contain the "new entry".
2295789Sahrens  *		tnm	- New entry name.
2296789Sahrens  *		cr	- credentials of caller.
2297789Sahrens  *
2298789Sahrens  *	RETURN:	0 if success
2299789Sahrens  *		error code if failure
2300789Sahrens  *
2301789Sahrens  * Timestamps:
2302789Sahrens  *	sdvp,tdvp - ctime|mtime updated
2303789Sahrens  */
2304789Sahrens static int
2305789Sahrens zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr)
2306789Sahrens {
2307789Sahrens 	znode_t		*tdzp, *szp, *tzp;
2308789Sahrens 	znode_t		*sdzp = VTOZ(sdvp);
2309789Sahrens 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
2310789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2311789Sahrens 	uint64_t	seq = 0;
2312789Sahrens 	vnode_t		*realvp;
2313789Sahrens 	zfs_dirlock_t	*sdl, *tdl;
2314789Sahrens 	dmu_tx_t	*tx;
2315789Sahrens 	zfs_zlock_t	*zl;
2316789Sahrens 	int		cmp, serr, terr, error;
2317789Sahrens 
2318789Sahrens 	ZFS_ENTER(zfsvfs);
2319789Sahrens 
2320789Sahrens 	/*
2321789Sahrens 	 * Make sure we have the real vp for the target directory.
2322789Sahrens 	 */
2323789Sahrens 	if (VOP_REALVP(tdvp, &realvp) == 0)
2324789Sahrens 		tdvp = realvp;
2325789Sahrens 
2326789Sahrens 	if (tdvp->v_vfsp != sdvp->v_vfsp) {
2327789Sahrens 		ZFS_EXIT(zfsvfs);
2328789Sahrens 		return (EXDEV);
2329789Sahrens 	}
2330789Sahrens 
2331789Sahrens 	tdzp = VTOZ(tdvp);
2332789Sahrens top:
2333789Sahrens 	szp = NULL;
2334789Sahrens 	tzp = NULL;
2335789Sahrens 	zl = NULL;
2336789Sahrens 
2337789Sahrens 	/*
2338789Sahrens 	 * This is to prevent the creation of links into attribute space
2339789Sahrens 	 * by renaming a linked file into/outof an attribute directory.
2340789Sahrens 	 * See the comment in zfs_link() for why this is considered bad.
2341789Sahrens 	 */
2342789Sahrens 	if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
2343789Sahrens 	    (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
2344789Sahrens 		ZFS_EXIT(zfsvfs);
2345789Sahrens 		return (EINVAL);
2346789Sahrens 	}
2347789Sahrens 
2348789Sahrens 	/*
2349789Sahrens 	 * Lock source and target directory entries.  To prevent deadlock,
2350789Sahrens 	 * a lock ordering must be defined.  We lock the directory with
2351789Sahrens 	 * the smallest object id first, or if it's a tie, the one with
2352789Sahrens 	 * the lexically first name.
2353789Sahrens 	 */
2354789Sahrens 	if (sdzp->z_id < tdzp->z_id) {
2355789Sahrens 		cmp = -1;
2356789Sahrens 	} else if (sdzp->z_id > tdzp->z_id) {
2357789Sahrens 		cmp = 1;
2358789Sahrens 	} else {
2359789Sahrens 		cmp = strcmp(snm, tnm);
2360789Sahrens 		if (cmp == 0) {
2361789Sahrens 			/*
2362789Sahrens 			 * POSIX: "If the old argument and the new argument
2363789Sahrens 			 * both refer to links to the same existing file,
2364789Sahrens 			 * the rename() function shall return successfully
2365789Sahrens 			 * and perform no other action."
2366789Sahrens 			 */
2367789Sahrens 			ZFS_EXIT(zfsvfs);
2368789Sahrens 			return (0);
2369789Sahrens 		}
2370789Sahrens 	}
2371789Sahrens 	if (cmp < 0) {
2372789Sahrens 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
2373789Sahrens 		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
2374789Sahrens 	} else {
2375789Sahrens 		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
2376789Sahrens 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
2377789Sahrens 	}
2378789Sahrens 
2379789Sahrens 	if (serr) {
2380789Sahrens 		/*
2381789Sahrens 		 * Source entry invalid or not there.
2382789Sahrens 		 */
2383789Sahrens 		if (!terr) {
2384789Sahrens 			zfs_dirent_unlock(tdl);
2385789Sahrens 			if (tzp)
2386789Sahrens 				VN_RELE(ZTOV(tzp));
2387789Sahrens 		}
2388789Sahrens 		if (strcmp(snm, "..") == 0)
2389789Sahrens 			serr = EINVAL;
2390789Sahrens 		ZFS_EXIT(zfsvfs);
2391789Sahrens 		return (serr);
2392789Sahrens 	}
2393789Sahrens 	if (terr) {
2394789Sahrens 		zfs_dirent_unlock(sdl);
2395789Sahrens 		VN_RELE(ZTOV(szp));
2396789Sahrens 		if (strcmp(tnm, "..") == 0)
2397789Sahrens 			terr = EINVAL;
2398789Sahrens 		ZFS_EXIT(zfsvfs);
2399789Sahrens 		return (terr);
2400789Sahrens 	}
2401789Sahrens 
2402789Sahrens 	/*
2403789Sahrens 	 * Must have write access at the source to remove the old entry
2404789Sahrens 	 * and write access at the target to create the new entry.
2405789Sahrens 	 * Note that if target and source are the same, this can be
2406789Sahrens 	 * done in a single check.
2407789Sahrens 	 */
2408789Sahrens 
2409789Sahrens 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
2410789Sahrens 		goto out;
2411789Sahrens 
2412789Sahrens 	if (ZTOV(szp)->v_type == VDIR) {
2413789Sahrens 		/*
2414789Sahrens 		 * Check to make sure rename is valid.
2415789Sahrens 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
2416789Sahrens 		 */
2417789Sahrens 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
2418789Sahrens 			goto out;
2419789Sahrens 	}
2420789Sahrens 
2421789Sahrens 	/*
2422789Sahrens 	 * Does target exist?
2423789Sahrens 	 */
2424789Sahrens 	if (tzp) {
2425789Sahrens 		/*
2426789Sahrens 		 * Source and target must be the same type.
2427789Sahrens 		 */
2428789Sahrens 		if (ZTOV(szp)->v_type == VDIR) {
2429789Sahrens 			if (ZTOV(tzp)->v_type != VDIR) {
2430789Sahrens 				error = ENOTDIR;
2431789Sahrens 				goto out;
2432789Sahrens 			}
2433789Sahrens 		} else {
2434789Sahrens 			if (ZTOV(tzp)->v_type == VDIR) {
2435789Sahrens 				error = EISDIR;
2436789Sahrens 				goto out;
2437789Sahrens 			}
2438789Sahrens 		}
2439789Sahrens 		/*
2440789Sahrens 		 * POSIX dictates that when the source and target
2441789Sahrens 		 * entries refer to the same file object, rename
2442789Sahrens 		 * must do nothing and exit without error.
2443789Sahrens 		 */
2444789Sahrens 		if (szp->z_id == tzp->z_id) {
2445789Sahrens 			error = 0;
2446789Sahrens 			goto out;
2447789Sahrens 		}
2448789Sahrens 	}
2449789Sahrens 
2450789Sahrens 	vnevent_rename_src(ZTOV(szp));
2451789Sahrens 	if (tzp)
2452789Sahrens 		vnevent_rename_dest(ZTOV(tzp));
2453789Sahrens 
2454789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2455789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);	/* nlink changes */
2456789Sahrens 	dmu_tx_hold_bonus(tx, sdzp->z_id);	/* nlink changes */
24571544Seschrock 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
24581544Seschrock 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
24591544Seschrock 	if (sdzp != tdzp)
2460789Sahrens 		dmu_tx_hold_bonus(tx, tdzp->z_id);	/* nlink changes */
24611544Seschrock 	if (tzp)
24621544Seschrock 		dmu_tx_hold_bonus(tx, tzp->z_id);	/* parent changes */
24631544Seschrock 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
2464789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2465789Sahrens 	if (error) {
2466789Sahrens 		dmu_tx_abort(tx);
2467789Sahrens 		if (zl != NULL)
2468789Sahrens 			zfs_rename_unlock(&zl);
2469789Sahrens 		zfs_dirent_unlock(sdl);
2470789Sahrens 		zfs_dirent_unlock(tdl);
2471789Sahrens 		VN_RELE(ZTOV(szp));
2472789Sahrens 		if (tzp)
2473789Sahrens 			VN_RELE(ZTOV(tzp));
2474789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2475789Sahrens 			txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0);
2476789Sahrens 			goto top;
2477789Sahrens 		}
2478789Sahrens 		ZFS_EXIT(zfsvfs);
2479789Sahrens 		return (error);
2480789Sahrens 	}
2481789Sahrens 
2482789Sahrens 	if (tzp)	/* Attempt to remove the existing target */
2483789Sahrens 		error = zfs_link_destroy(tdl, tzp, tx, 0, NULL);
2484789Sahrens 
2485789Sahrens 	if (error == 0) {
2486789Sahrens 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
2487789Sahrens 		if (error == 0) {
2488789Sahrens 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
2489789Sahrens 			ASSERT(error == 0);
2490789Sahrens 			seq = zfs_log_rename(zilog, tx, TX_RENAME,
2491789Sahrens 			    sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
2492789Sahrens 		}
2493789Sahrens 	}
2494789Sahrens 
2495789Sahrens 	dmu_tx_commit(tx);
2496789Sahrens out:
2497789Sahrens 	if (zl != NULL)
2498789Sahrens 		zfs_rename_unlock(&zl);
2499789Sahrens 
2500789Sahrens 	zfs_dirent_unlock(sdl);
2501789Sahrens 	zfs_dirent_unlock(tdl);
2502789Sahrens 
2503789Sahrens 	VN_RELE(ZTOV(szp));
2504789Sahrens 	if (tzp)
2505789Sahrens 		VN_RELE(ZTOV(tzp));
2506789Sahrens 
2507789Sahrens 	zil_commit(zilog, seq, 0);
2508789Sahrens 
2509789Sahrens 	ZFS_EXIT(zfsvfs);
2510789Sahrens 	return (error);
2511789Sahrens }
2512789Sahrens 
2513789Sahrens /*
2514789Sahrens  * Insert the indicated symbolic reference entry into the directory.
2515789Sahrens  *
2516789Sahrens  *	IN:	dvp	- Directory to contain new symbolic link.
2517789Sahrens  *		link	- Name for new symlink entry.
2518789Sahrens  *		vap	- Attributes of new entry.
2519789Sahrens  *		target	- Target path of new symlink.
2520789Sahrens  *		cr	- credentials of caller.
2521789Sahrens  *
2522789Sahrens  *	RETURN:	0 if success
2523789Sahrens  *		error code if failure
2524789Sahrens  *
2525789Sahrens  * Timestamps:
2526789Sahrens  *	dvp - ctime|mtime updated
2527789Sahrens  */
2528789Sahrens static int
2529789Sahrens zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr)
2530789Sahrens {
2531789Sahrens 	znode_t		*zp, *dzp = VTOZ(dvp);
2532789Sahrens 	zfs_dirlock_t	*dl;
2533789Sahrens 	dmu_tx_t	*tx;
2534789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2535789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2536789Sahrens 	uint64_t	seq = 0;
2537789Sahrens 	uint64_t	zoid;
2538789Sahrens 	int		len = strlen(link);
2539789Sahrens 	int		error;
2540789Sahrens 
2541789Sahrens 	ASSERT(vap->va_type == VLNK);
2542789Sahrens 
2543789Sahrens 	ZFS_ENTER(zfsvfs);
2544789Sahrens top:
2545789Sahrens 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
2546789Sahrens 		ZFS_EXIT(zfsvfs);
2547789Sahrens 		return (error);
2548789Sahrens 	}
2549789Sahrens 
2550789Sahrens 	if (len > MAXPATHLEN) {
2551789Sahrens 		ZFS_EXIT(zfsvfs);
2552789Sahrens 		return (ENAMETOOLONG);
2553789Sahrens 	}
2554789Sahrens 
2555789Sahrens 	/*
2556789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
2557789Sahrens 	 */
2558789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZNEW)) {
2559789Sahrens 		ZFS_EXIT(zfsvfs);
2560789Sahrens 		return (error);
2561789Sahrens 	}
2562789Sahrens 
2563789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2564789Sahrens 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
2565789Sahrens 	dmu_tx_hold_bonus(tx, dzp->z_id);
25661544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
2567789Sahrens 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
2568789Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
2569789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2570789Sahrens 	if (error) {
2571789Sahrens 		dmu_tx_abort(tx);
2572789Sahrens 		zfs_dirent_unlock(dl);
2573789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2574789Sahrens 			txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0);
2575789Sahrens 			goto top;
2576789Sahrens 		}
2577789Sahrens 		ZFS_EXIT(zfsvfs);
2578789Sahrens 		return (error);
2579789Sahrens 	}
2580789Sahrens 
2581789Sahrens 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
2582789Sahrens 
2583789Sahrens 	/*
2584789Sahrens 	 * Create a new object for the symlink.
2585789Sahrens 	 * Put the link content into bonus buffer if it will fit;
2586789Sahrens 	 * otherwise, store it just like any other file data.
2587789Sahrens 	 */
2588789Sahrens 	zoid = 0;
2589789Sahrens 	if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
2590789Sahrens 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, len);
2591789Sahrens 		if (len != 0)
2592789Sahrens 			bcopy(link, zp->z_phys + 1, len);
2593789Sahrens 	} else {
2594789Sahrens 		dmu_buf_t *dbp;
25951669Sperrin 
2596789Sahrens 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
2597789Sahrens 
25981669Sperrin 		/*
25991669Sperrin 		 * Nothing can access the znode yet so no locking needed
26001669Sperrin 		 * for growing the znode's blocksize.
26011669Sperrin 		 */
26021669Sperrin 		zfs_grow_blocksize(zp, len, tx);
2603789Sahrens 
26041544Seschrock 		VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, zoid, 0, FTAG, &dbp));
2605789Sahrens 		dmu_buf_will_dirty(dbp, tx);
2606789Sahrens 
2607789Sahrens 		ASSERT3U(len, <=, dbp->db_size);
2608789Sahrens 		bcopy(link, dbp->db_data, len);
26091544Seschrock 		dmu_buf_rele(dbp, FTAG);
2610789Sahrens 	}
2611789Sahrens 	zp->z_phys->zp_size = len;
2612789Sahrens 
2613789Sahrens 	/*
2614789Sahrens 	 * Insert the new object into the directory.
2615789Sahrens 	 */
2616789Sahrens 	(void) zfs_link_create(dl, zp, tx, ZNEW);
2617789Sahrens out:
2618789Sahrens 	if (error == 0)
2619789Sahrens 		seq = zfs_log_symlink(zilog, tx, TX_SYMLINK,
2620789Sahrens 		    dzp, zp, name, link);
2621789Sahrens 
2622789Sahrens 	dmu_tx_commit(tx);
2623789Sahrens 
2624789Sahrens 	zfs_dirent_unlock(dl);
2625789Sahrens 
2626789Sahrens 	VN_RELE(ZTOV(zp));
2627789Sahrens 
2628789Sahrens 	zil_commit(zilog, seq, 0);
2629789Sahrens 
2630789Sahrens 	ZFS_EXIT(zfsvfs);
2631789Sahrens 	return (error);
2632789Sahrens }
2633789Sahrens 
2634789Sahrens /*
2635789Sahrens  * Return, in the buffer contained in the provided uio structure,
2636789Sahrens  * the symbolic path referred to by vp.
2637789Sahrens  *
2638789Sahrens  *	IN:	vp	- vnode of symbolic link.
2639789Sahrens  *		uoip	- structure to contain the link path.
2640789Sahrens  *		cr	- credentials of caller.
2641789Sahrens  *
2642789Sahrens  *	OUT:	uio	- structure to contain the link path.
2643789Sahrens  *
2644789Sahrens  *	RETURN:	0 if success
2645789Sahrens  *		error code if failure
2646789Sahrens  *
2647789Sahrens  * Timestamps:
2648789Sahrens  *	vp - atime updated
2649789Sahrens  */
2650789Sahrens /* ARGSUSED */
2651789Sahrens static int
2652789Sahrens zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr)
2653789Sahrens {
2654789Sahrens 	znode_t		*zp = VTOZ(vp);
2655789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2656789Sahrens 	size_t		bufsz;
2657789Sahrens 	int		error;
2658789Sahrens 
2659789Sahrens 	ZFS_ENTER(zfsvfs);
2660789Sahrens 
2661789Sahrens 	bufsz = (size_t)zp->z_phys->zp_size;
2662789Sahrens 	if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
2663789Sahrens 		error = uiomove(zp->z_phys + 1,
2664789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
2665789Sahrens 	} else {
26661544Seschrock 		dmu_buf_t *dbp;
26671544Seschrock 		error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
26681544Seschrock 		if (error) {
2669789Sahrens 			ZFS_EXIT(zfsvfs);
2670789Sahrens 			return (error);
2671789Sahrens 		}
2672789Sahrens 		error = uiomove(dbp->db_data,
2673789Sahrens 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
26741544Seschrock 		dmu_buf_rele(dbp, FTAG);
2675789Sahrens 	}
2676789Sahrens 
2677789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2678789Sahrens 	ZFS_EXIT(zfsvfs);
2679789Sahrens 	return (error);
2680789Sahrens }
2681789Sahrens 
2682789Sahrens /*
2683789Sahrens  * Insert a new entry into directory tdvp referencing svp.
2684789Sahrens  *
2685789Sahrens  *	IN:	tdvp	- Directory to contain new entry.
2686789Sahrens  *		svp	- vnode of new entry.
2687789Sahrens  *		name	- name of new entry.
2688789Sahrens  *		cr	- credentials of caller.
2689789Sahrens  *
2690789Sahrens  *	RETURN:	0 if success
2691789Sahrens  *		error code if failure
2692789Sahrens  *
2693789Sahrens  * Timestamps:
2694789Sahrens  *	tdvp - ctime|mtime updated
2695789Sahrens  *	 svp - ctime updated
2696789Sahrens  */
2697789Sahrens /* ARGSUSED */
2698789Sahrens static int
2699789Sahrens zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr)
2700789Sahrens {
2701789Sahrens 	znode_t		*dzp = VTOZ(tdvp);
2702789Sahrens 	znode_t		*tzp, *szp;
2703789Sahrens 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2704789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2705789Sahrens 	uint64_t	seq = 0;
2706789Sahrens 	zfs_dirlock_t	*dl;
2707789Sahrens 	dmu_tx_t	*tx;
2708789Sahrens 	vnode_t		*realvp;
2709789Sahrens 	int		error;
2710789Sahrens 
2711789Sahrens 	ASSERT(tdvp->v_type == VDIR);
2712789Sahrens 
2713789Sahrens 	ZFS_ENTER(zfsvfs);
2714789Sahrens 
2715789Sahrens 	if (VOP_REALVP(svp, &realvp) == 0)
2716789Sahrens 		svp = realvp;
2717789Sahrens 
2718789Sahrens 	if (svp->v_vfsp != tdvp->v_vfsp) {
2719789Sahrens 		ZFS_EXIT(zfsvfs);
2720789Sahrens 		return (EXDEV);
2721789Sahrens 	}
2722789Sahrens 
2723789Sahrens 	szp = VTOZ(svp);
2724789Sahrens top:
2725789Sahrens 	/*
2726789Sahrens 	 * We do not support links between attributes and non-attributes
2727789Sahrens 	 * because of the potential security risk of creating links
2728789Sahrens 	 * into "normal" file space in order to circumvent restrictions
2729789Sahrens 	 * imposed in attribute space.
2730789Sahrens 	 */
2731789Sahrens 	if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
2732789Sahrens 	    (dzp->z_phys->zp_flags & ZFS_XATTR)) {
2733789Sahrens 		ZFS_EXIT(zfsvfs);
2734789Sahrens 		return (EINVAL);
2735789Sahrens 	}
2736789Sahrens 
2737789Sahrens 	/*
2738789Sahrens 	 * POSIX dictates that we return EPERM here.
2739789Sahrens 	 * Better choices include ENOTSUP or EISDIR.
2740789Sahrens 	 */
2741789Sahrens 	if (svp->v_type == VDIR) {
2742789Sahrens 		ZFS_EXIT(zfsvfs);
2743789Sahrens 		return (EPERM);
2744789Sahrens 	}
2745789Sahrens 
2746789Sahrens 	if ((uid_t)szp->z_phys->zp_uid != crgetuid(cr) &&
2747789Sahrens 	    secpolicy_basic_link(cr) != 0) {
2748789Sahrens 		ZFS_EXIT(zfsvfs);
2749789Sahrens 		return (EPERM);
2750789Sahrens 	}
2751789Sahrens 
2752789Sahrens 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
2753789Sahrens 		ZFS_EXIT(zfsvfs);
2754789Sahrens 		return (error);
2755789Sahrens 	}
2756789Sahrens 
2757789Sahrens 	/*
2758789Sahrens 	 * Attempt to lock directory; fail if entry already exists.
2759789Sahrens 	 */
2760789Sahrens 	if (error = zfs_dirent_lock(&dl, dzp, name, &tzp, ZNEW)) {
2761789Sahrens 		ZFS_EXIT(zfsvfs);
2762789Sahrens 		return (error);
2763789Sahrens 	}
2764789Sahrens 
2765789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2766789Sahrens 	dmu_tx_hold_bonus(tx, szp->z_id);
27671544Seschrock 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
2768789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2769789Sahrens 	if (error) {
2770789Sahrens 		dmu_tx_abort(tx);
2771789Sahrens 		zfs_dirent_unlock(dl);
2772789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2773789Sahrens 			txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0);
2774789Sahrens 			goto top;
2775789Sahrens 		}
2776789Sahrens 		ZFS_EXIT(zfsvfs);
2777789Sahrens 		return (error);
2778789Sahrens 	}
2779789Sahrens 
2780789Sahrens 	error = zfs_link_create(dl, szp, tx, 0);
2781789Sahrens 
2782789Sahrens 	if (error == 0)
2783789Sahrens 		seq = zfs_log_link(zilog, tx, TX_LINK, dzp, szp, name);
2784789Sahrens 
2785789Sahrens 	dmu_tx_commit(tx);
2786789Sahrens 
2787789Sahrens 	zfs_dirent_unlock(dl);
2788789Sahrens 
2789789Sahrens 	zil_commit(zilog, seq, 0);
2790789Sahrens 
2791789Sahrens 	ZFS_EXIT(zfsvfs);
2792789Sahrens 	return (error);
2793789Sahrens }
2794789Sahrens 
2795789Sahrens /*
2796789Sahrens  * zfs_null_putapage() is used when the file system has been force
2797789Sahrens  * unmounted. It just drops the pages.
2798789Sahrens  */
2799789Sahrens /* ARGSUSED */
2800789Sahrens static int
2801789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
2802789Sahrens 		size_t *lenp, int flags, cred_t *cr)
2803789Sahrens {
2804789Sahrens 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
2805789Sahrens 	return (0);
2806789Sahrens }
2807789Sahrens 
2808789Sahrens /* ARGSUSED */
2809789Sahrens static int
2810789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
2811789Sahrens 		size_t *lenp, int flags, cred_t *cr)
2812789Sahrens {
2813789Sahrens 	znode_t		*zp = VTOZ(vp);
2814789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2815789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
2816789Sahrens 	dmu_tx_t	*tx;
28171669Sperrin 	rl_t		*rl;
2818789Sahrens 	u_offset_t	off;
2819789Sahrens 	ssize_t		len;
2820789Sahrens 	caddr_t		va;
2821789Sahrens 	int		err;
2822789Sahrens 
2823789Sahrens top:
2824789Sahrens 	off = pp->p_offset;
28251669Sperrin 	rl = zfs_range_lock(zp, off, PAGESIZE, RL_WRITER);
2826789Sahrens 	len = MIN(PAGESIZE, zp->z_phys->zp_size - off);
2827789Sahrens 
2828789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
2829789Sahrens 	dmu_tx_hold_write(tx, zp->z_id, off, len);
2830789Sahrens 	dmu_tx_hold_bonus(tx, zp->z_id);
2831789Sahrens 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2832789Sahrens 	if (err != 0) {
2833789Sahrens 		dmu_tx_abort(tx);
28341669Sperrin 		zfs_range_unlock(zp, rl);
2835789Sahrens 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2836789Sahrens 			txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0);
2837789Sahrens 			goto top;
2838789Sahrens 		}
2839789Sahrens 		goto out;
2840789Sahrens 	}
2841789Sahrens 
2842789Sahrens 	va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1);
2843789Sahrens 
2844789Sahrens 	dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
2845789Sahrens 
2846789Sahrens 	ppmapout(va);
2847789Sahrens 
2848789Sahrens 	zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
28491472Sperrin 	(void) zfs_log_write(zilog, tx, TX_WRITE, zp, off, len, 0, NULL);
2850789Sahrens 	dmu_tx_commit(tx);
2851789Sahrens 
28521669Sperrin 	zfs_range_unlock(zp, rl);
2853789Sahrens 
2854789Sahrens 	pvn_write_done(pp, B_WRITE | flags);
2855789Sahrens 	if (offp)
2856789Sahrens 		*offp = off;
2857789Sahrens 	if (lenp)
2858789Sahrens 		*lenp = len;
2859789Sahrens 
2860789Sahrens out:
2861789Sahrens 	return (err);
2862789Sahrens }
2863789Sahrens 
2864789Sahrens /*
2865789Sahrens  * Copy the portion of the file indicated from pages into the file.
2866789Sahrens  * The pages are stored in a page list attached to the files vnode.
2867789Sahrens  *
2868789Sahrens  *	IN:	vp	- vnode of file to push page data to.
2869789Sahrens  *		off	- position in file to put data.
2870789Sahrens  *		len	- amount of data to write.
2871789Sahrens  *		flags	- flags to control the operation.
2872789Sahrens  *		cr	- credentials of caller.
2873789Sahrens  *
2874789Sahrens  *	RETURN:	0 if success
2875789Sahrens  *		error code if failure
2876789Sahrens  *
2877789Sahrens  * Timestamps:
2878789Sahrens  *	vp - ctime|mtime updated
2879789Sahrens  */
2880789Sahrens static int
2881789Sahrens zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr)
2882789Sahrens {
2883789Sahrens 	znode_t		*zp = VTOZ(vp);
2884789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2885789Sahrens 	page_t		*pp;
2886789Sahrens 	size_t		io_len;
2887789Sahrens 	u_offset_t	io_off;
28881669Sperrin 	uint64_t	filesz;
2889789Sahrens 	int		error = 0;
2890789Sahrens 
2891789Sahrens 	ZFS_ENTER(zfsvfs);
2892789Sahrens 
2893789Sahrens 	ASSERT(zp->z_dbuf_held && zp->z_phys);
2894789Sahrens 
2895789Sahrens 	if (len == 0) {
2896789Sahrens 		/*
2897789Sahrens 		 * Search the entire vp list for pages >= off.
2898789Sahrens 		 */
2899789Sahrens 		error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage,
2900789Sahrens 		    flags, cr);
29011472Sperrin 		goto out;
2902789Sahrens 	}
2903789Sahrens 
29041669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
29051669Sperrin 	if (off > filesz) {
2906789Sahrens 		/* past end of file */
2907789Sahrens 		ZFS_EXIT(zfsvfs);
2908789Sahrens 		return (0);
2909789Sahrens 	}
2910789Sahrens 
29111669Sperrin 	len = MIN(len, filesz - off);
2912789Sahrens 
29131472Sperrin 	for (io_off = off; io_off < off + len; io_off += io_len) {
2914789Sahrens 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
29151669Sperrin 			pp = page_lookup(vp, io_off,
2916789Sahrens 				(flags & (B_INVAL | B_FREE)) ?
2917789Sahrens 					SE_EXCL : SE_SHARED);
2918789Sahrens 		} else {
2919789Sahrens 			pp = page_lookup_nowait(vp, io_off,
2920789Sahrens 				(flags & B_FREE) ? SE_EXCL : SE_SHARED);
2921789Sahrens 		}
2922789Sahrens 
2923789Sahrens 		if (pp != NULL && pvn_getdirty(pp, flags)) {
2924789Sahrens 			int err;
2925789Sahrens 
2926789Sahrens 			/*
2927789Sahrens 			 * Found a dirty page to push
2928789Sahrens 			 */
29291669Sperrin 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
29301669Sperrin 			if (err)
2931789Sahrens 				error = err;
2932789Sahrens 		} else {
2933789Sahrens 			io_len = PAGESIZE;
2934789Sahrens 		}
2935789Sahrens 	}
29361472Sperrin out:
29371472Sperrin 	zil_commit(zfsvfs->z_log, UINT64_MAX, (flags & B_ASYNC) ? 0 : FDSYNC);
2938789Sahrens 	ZFS_EXIT(zfsvfs);
2939789Sahrens 	return (error);
2940789Sahrens }
2941789Sahrens 
2942789Sahrens void
2943789Sahrens zfs_inactive(vnode_t *vp, cred_t *cr)
2944789Sahrens {
2945789Sahrens 	znode_t	*zp = VTOZ(vp);
2946789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2947789Sahrens 	int error;
2948789Sahrens 
2949789Sahrens 	rw_enter(&zfsvfs->z_um_lock, RW_READER);
2950789Sahrens 	if (zfsvfs->z_unmounted2) {
2951789Sahrens 		ASSERT(zp->z_dbuf_held == 0);
2952789Sahrens 
2953789Sahrens 		if (vn_has_cached_data(vp)) {
2954789Sahrens 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
2955789Sahrens 			    B_INVAL, cr);
2956789Sahrens 		}
2957789Sahrens 
29581544Seschrock 		mutex_enter(&zp->z_lock);
2959789Sahrens 		vp->v_count = 0; /* count arrives as 1 */
29601544Seschrock 		if (zp->z_dbuf == NULL) {
29611544Seschrock 			mutex_exit(&zp->z_lock);
29621544Seschrock 			zfs_znode_free(zp);
29631544Seschrock 		} else {
29641544Seschrock 			mutex_exit(&zp->z_lock);
29651544Seschrock 		}
2966789Sahrens 		rw_exit(&zfsvfs->z_um_lock);
2967789Sahrens 		VFS_RELE(zfsvfs->z_vfs);
2968789Sahrens 		return;
2969789Sahrens 	}
2970789Sahrens 
2971789Sahrens 	/*
2972789Sahrens 	 * Attempt to push any data in the page cache.  If this fails
2973789Sahrens 	 * we will get kicked out later in zfs_zinactive().
2974789Sahrens 	 */
29751298Sperrin 	if (vn_has_cached_data(vp)) {
29761298Sperrin 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
29771298Sperrin 		    cr);
29781298Sperrin 	}
2979789Sahrens 
2980789Sahrens 	if (zp->z_atime_dirty && zp->z_reap == 0) {
2981789Sahrens 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
2982789Sahrens 
2983789Sahrens 		dmu_tx_hold_bonus(tx, zp->z_id);
2984789Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
2985789Sahrens 		if (error) {
2986789Sahrens 			dmu_tx_abort(tx);
2987789Sahrens 		} else {
2988789Sahrens 			dmu_buf_will_dirty(zp->z_dbuf, tx);
2989789Sahrens 			mutex_enter(&zp->z_lock);
2990789Sahrens 			zp->z_atime_dirty = 0;
2991789Sahrens 			mutex_exit(&zp->z_lock);
2992789Sahrens 			dmu_tx_commit(tx);
2993789Sahrens 		}
2994789Sahrens 	}
2995789Sahrens 
2996789Sahrens 	zfs_zinactive(zp);
2997789Sahrens 	rw_exit(&zfsvfs->z_um_lock);
2998789Sahrens }
2999789Sahrens 
3000789Sahrens /*
3001789Sahrens  * Bounds-check the seek operation.
3002789Sahrens  *
3003789Sahrens  *	IN:	vp	- vnode seeking within
3004789Sahrens  *		ooff	- old file offset
3005789Sahrens  *		noffp	- pointer to new file offset
3006789Sahrens  *
3007789Sahrens  *	RETURN:	0 if success
3008789Sahrens  *		EINVAL if new offset invalid
3009789Sahrens  */
3010789Sahrens /* ARGSUSED */
3011789Sahrens static int
3012789Sahrens zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp)
3013789Sahrens {
3014789Sahrens 	if (vp->v_type == VDIR)
3015789Sahrens 		return (0);
3016789Sahrens 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
3017789Sahrens }
3018789Sahrens 
3019789Sahrens /*
3020789Sahrens  * Pre-filter the generic locking function to trap attempts to place
3021789Sahrens  * a mandatory lock on a memory mapped file.
3022789Sahrens  */
3023789Sahrens static int
3024789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
3025789Sahrens     flk_callback_t *flk_cbp, cred_t *cr)
3026789Sahrens {
3027789Sahrens 	znode_t *zp = VTOZ(vp);
3028789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3029789Sahrens 	int error;
3030789Sahrens 
3031789Sahrens 	ZFS_ENTER(zfsvfs);
3032789Sahrens 
3033789Sahrens 	/*
30341544Seschrock 	 * We are following the UFS semantics with respect to mapcnt
30351544Seschrock 	 * here: If we see that the file is mapped already, then we will
30361544Seschrock 	 * return an error, but we don't worry about races between this
30371544Seschrock 	 * function and zfs_map().
3038789Sahrens 	 */
30391544Seschrock 	if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) {
3040789Sahrens 		ZFS_EXIT(zfsvfs);
3041789Sahrens 		return (EAGAIN);
3042789Sahrens 	}
3043789Sahrens 	error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr);
3044789Sahrens 	ZFS_EXIT(zfsvfs);
3045789Sahrens 	return (error);
3046789Sahrens }
3047789Sahrens 
3048789Sahrens /*
3049789Sahrens  * If we can't find a page in the cache, we will create a new page
3050789Sahrens  * and fill it with file data.  For efficiency, we may try to fill
30511669Sperrin  * multiple pages at once (klustering).
3052789Sahrens  */
3053789Sahrens static int
3054789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
3055789Sahrens     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
3056789Sahrens {
3057789Sahrens 	znode_t *zp = VTOZ(vp);
3058789Sahrens 	page_t *pp, *cur_pp;
3059789Sahrens 	objset_t *os = zp->z_zfsvfs->z_os;
3060789Sahrens 	caddr_t va;
3061789Sahrens 	u_offset_t io_off, total;
3062789Sahrens 	uint64_t oid = zp->z_id;
3063789Sahrens 	size_t io_len;
30641669Sperrin 	uint64_t filesz;
3065789Sahrens 	int err;
3066789Sahrens 
3067789Sahrens 	/*
3068789Sahrens 	 * If we are only asking for a single page don't bother klustering.
3069789Sahrens 	 */
30701669Sperrin 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
30711669Sperrin 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE || off > filesz) {
3072789Sahrens 		io_off = off;
3073789Sahrens 		io_len = PAGESIZE;
3074789Sahrens 		pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr);
3075789Sahrens 	} else {
3076789Sahrens 		/*
3077789Sahrens 		 * Try to fill a kluster of pages (a blocks worth).
3078789Sahrens 		 */
3079789Sahrens 		size_t klen;
3080789Sahrens 		u_offset_t koff;
3081789Sahrens 
3082789Sahrens 		if (!ISP2(zp->z_blksz)) {
3083789Sahrens 			/* Only one block in the file. */
3084789Sahrens 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
3085789Sahrens 			koff = 0;
3086789Sahrens 		} else {
3087789Sahrens 			klen = plsz;
3088789Sahrens 			koff = P2ALIGN(off, (u_offset_t)klen);
3089789Sahrens 		}
30901669Sperrin 		if (klen > filesz)
30911669Sperrin 			klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE);
3092789Sahrens 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
3093789Sahrens 			    &io_len, koff, klen, 0);
3094789Sahrens 	}
3095789Sahrens 	if (pp == NULL) {
3096789Sahrens 		/*
3097789Sahrens 		 * Some other thread entered the page before us.
3098789Sahrens 		 * Return to zfs_getpage to retry the lookup.
3099789Sahrens 		 */
3100789Sahrens 		*pl = NULL;
3101789Sahrens 		return (0);
3102789Sahrens 	}
3103789Sahrens 
3104789Sahrens 	/*
3105789Sahrens 	 * Fill the pages in the kluster.
3106789Sahrens 	 */
3107789Sahrens 	cur_pp = pp;
3108789Sahrens 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
3109789Sahrens 		ASSERT(io_off == cur_pp->p_offset);
3110789Sahrens 		va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1);
31111544Seschrock 		err = dmu_read(os, oid, io_off, PAGESIZE, va);
3112789Sahrens 		ppmapout(va);
3113789Sahrens 		if (err) {
3114789Sahrens 			/* On error, toss the entire kluster */
3115789Sahrens 			pvn_read_done(pp, B_ERROR);
3116789Sahrens 			return (err);
3117789Sahrens 		}
3118789Sahrens 		cur_pp = cur_pp->p_next;
3119789Sahrens 	}
3120789Sahrens out:
3121789Sahrens 	/*
3122789Sahrens 	 * Fill in the page list array from the kluster.  If
3123789Sahrens 	 * there are too many pages in the kluster, return
3124789Sahrens 	 * as many pages as possible starting from the desired
3125789Sahrens 	 * offset `off'.
3126789Sahrens 	 * NOTE: the page list will always be null terminated.
3127789Sahrens 	 */
3128789Sahrens 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
3129789Sahrens 
3130789Sahrens 	return (0);
3131789Sahrens }
3132789Sahrens 
3133789Sahrens /*
3134789Sahrens  * Return pointers to the pages for the file region [off, off + len]
3135789Sahrens  * in the pl array.  If plsz is greater than len, this function may
3136789Sahrens  * also return page pointers from before or after the specified
3137789Sahrens  * region (i.e. some region [off', off' + plsz]).  These additional
3138789Sahrens  * pages are only returned if they are already in the cache, or were
3139789Sahrens  * created as part of a klustered read.
3140789Sahrens  *
3141789Sahrens  *	IN:	vp	- vnode of file to get data from.
3142789Sahrens  *		off	- position in file to get data from.
3143789Sahrens  *		len	- amount of data to retrieve.
3144789Sahrens  *		plsz	- length of provided page list.
3145789Sahrens  *		seg	- segment to obtain pages for.
3146789Sahrens  *		addr	- virtual address of fault.
3147789Sahrens  *		rw	- mode of created pages.
3148789Sahrens  *		cr	- credentials of caller.
3149789Sahrens  *
3150789Sahrens  *	OUT:	protp	- protection mode of created pages.
3151789Sahrens  *		pl	- list of pages created.
3152789Sahrens  *
3153789Sahrens  *	RETURN:	0 if success
3154789Sahrens  *		error code if failure
3155789Sahrens  *
3156789Sahrens  * Timestamps:
3157789Sahrens  *	vp - atime updated
3158789Sahrens  */
3159789Sahrens /* ARGSUSED */
3160789Sahrens static int
3161789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
3162789Sahrens 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
3163789Sahrens 	enum seg_rw rw, cred_t *cr)
3164789Sahrens {
3165789Sahrens 	znode_t		*zp = VTOZ(vp);
3166789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3167789Sahrens 	page_t		*pp, **pl0 = pl;
31681669Sperrin 	rl_t		*rl;
3169789Sahrens 	int		cnt = 0, need_unlock = 0, err = 0;
3170789Sahrens 
3171789Sahrens 	ZFS_ENTER(zfsvfs);
3172789Sahrens 
3173789Sahrens 	if (protp)
3174789Sahrens 		*protp = PROT_ALL;
3175789Sahrens 
3176789Sahrens 	ASSERT(zp->z_dbuf_held && zp->z_phys);
3177789Sahrens 
3178789Sahrens 	/* no faultahead (for now) */
3179789Sahrens 	if (pl == NULL) {
3180789Sahrens 		ZFS_EXIT(zfsvfs);
3181789Sahrens 		return (0);
3182789Sahrens 	}
3183789Sahrens 
31841669Sperrin 	/*
31851669Sperrin 	 * Make sure nobody restructures the file in the middle of the getpage.
31861669Sperrin 	 */
31871669Sperrin 	rl = zfs_range_lock(zp, off, len, RL_READER);
31881669Sperrin 
3189789Sahrens 	/* can't fault past EOF */
3190789Sahrens 	if (off >= zp->z_phys->zp_size) {
31911669Sperrin 		zfs_range_unlock(zp, rl);
3192789Sahrens 		ZFS_EXIT(zfsvfs);
3193789Sahrens 		return (EFAULT);
3194789Sahrens 	}
3195789Sahrens 
3196789Sahrens 	/*
3197789Sahrens 	 * If we already own the lock, then we must be page faulting
3198789Sahrens 	 * in the middle of a write to this file (i.e., we are writing
3199789Sahrens 	 * to this file using data from a mapped region of the file).
3200789Sahrens 	 */
3201789Sahrens 	if (!rw_owner(&zp->z_map_lock)) {
3202789Sahrens 		rw_enter(&zp->z_map_lock, RW_WRITER);
3203789Sahrens 		need_unlock = TRUE;
3204789Sahrens 	}
3205789Sahrens 
3206789Sahrens 	/*
3207789Sahrens 	 * Loop through the requested range [off, off + len] looking
3208789Sahrens 	 * for pages.  If we don't find a page, we will need to create
3209789Sahrens 	 * a new page and fill it with data from the file.
3210789Sahrens 	 */
3211789Sahrens 	while (len > 0) {
3212789Sahrens 		if (plsz < PAGESIZE)
3213789Sahrens 			break;
3214789Sahrens 		if (pp = page_lookup(vp, off, SE_SHARED)) {
3215789Sahrens 			*pl++ = pp;
3216789Sahrens 			off += PAGESIZE;
3217789Sahrens 			addr += PAGESIZE;
3218789Sahrens 			len -= PAGESIZE;
3219789Sahrens 			plsz -= PAGESIZE;
3220789Sahrens 		} else {
3221789Sahrens 			err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw);
3222789Sahrens 			/*
3223789Sahrens 			 * klustering may have changed our region
3224789Sahrens 			 * to be block aligned.
3225789Sahrens 			 */
3226789Sahrens 			if (((pp = *pl) != 0) && (off != pp->p_offset)) {
3227789Sahrens 				int delta = off - pp->p_offset;
3228789Sahrens 				len += delta;
3229789Sahrens 				off -= delta;
3230789Sahrens 				addr -= delta;
3231789Sahrens 			}
3232789Sahrens 			while (*pl) {
3233789Sahrens 				pl++;
3234789Sahrens 				cnt++;
3235789Sahrens 				off += PAGESIZE;
3236789Sahrens 				addr += PAGESIZE;
3237789Sahrens 				plsz -= PAGESIZE;
3238789Sahrens 				if (len > PAGESIZE)
3239789Sahrens 					len -= PAGESIZE;
3240789Sahrens 				else
3241789Sahrens 					len = 0;
3242789Sahrens 			}
32431669Sperrin 			if (err) {
32441669Sperrin 				/*
32451669Sperrin 				 * Release any pages we have locked.
32461669Sperrin 				 */
32471669Sperrin 				while (pl > pl0)
32481669Sperrin 					page_unlock(*--pl);
32491669Sperrin 				goto out;
32501669Sperrin 			}
3251789Sahrens 		}
3252789Sahrens 	}
3253789Sahrens 
3254789Sahrens 	/*
3255789Sahrens 	 * Fill out the page array with any pages already in the cache.
3256789Sahrens 	 */
3257789Sahrens 	while (plsz > 0) {
3258789Sahrens 		pp = page_lookup_nowait(vp, off, SE_SHARED);
3259789Sahrens 		if (pp == NULL)
3260789Sahrens 			break;
3261789Sahrens 		*pl++ = pp;
3262789Sahrens 		off += PAGESIZE;
3263789Sahrens 		plsz -= PAGESIZE;
3264789Sahrens 	}
3265789Sahrens 
3266789Sahrens 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3267789Sahrens out:
3268789Sahrens 	*pl = NULL;
3269789Sahrens 
3270789Sahrens 	if (need_unlock)
3271789Sahrens 		rw_exit(&zp->z_map_lock);
32721669Sperrin 	zfs_range_unlock(zp, rl);
3273789Sahrens 
3274789Sahrens 	ZFS_EXIT(zfsvfs);
3275789Sahrens 	return (err);
3276789Sahrens }
3277789Sahrens 
32781544Seschrock /*
32791544Seschrock  * Request a memory map for a section of a file.  This code interacts
32801544Seschrock  * with common code and the VM system as follows:
32811544Seschrock  *
32821544Seschrock  *	common code calls mmap(), which ends up in smmap_common()
32831544Seschrock  *
32841544Seschrock  *	this calls VOP_MAP(), which takes you into (say) zfs
32851544Seschrock  *
32861544Seschrock  *	zfs_map() calls as_map(), passing segvn_create() as the callback
32871544Seschrock  *
32881544Seschrock  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
32891544Seschrock  *
32901544Seschrock  *	zfs_addmap() updates z_mapcnt
32911544Seschrock  */
3292789Sahrens static int
3293789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
3294789Sahrens     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
3295789Sahrens {
3296789Sahrens 	znode_t *zp = VTOZ(vp);
3297789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3298789Sahrens 	segvn_crargs_t	vn_a;
3299789Sahrens 	int		error;
3300789Sahrens 
3301789Sahrens 	ZFS_ENTER(zfsvfs);
3302789Sahrens 
3303789Sahrens 	if (vp->v_flag & VNOMAP) {
3304789Sahrens 		ZFS_EXIT(zfsvfs);
3305789Sahrens 		return (ENOSYS);
3306789Sahrens 	}
3307789Sahrens 
3308789Sahrens 	if (off < 0 || len > MAXOFFSET_T - off) {
3309789Sahrens 		ZFS_EXIT(zfsvfs);
3310789Sahrens 		return (ENXIO);
3311789Sahrens 	}
3312789Sahrens 
3313789Sahrens 	if (vp->v_type != VREG) {
3314789Sahrens 		ZFS_EXIT(zfsvfs);
3315789Sahrens 		return (ENODEV);
3316789Sahrens 	}
3317789Sahrens 
3318789Sahrens 	/*
3319789Sahrens 	 * If file is locked, disallow mapping.
3320789Sahrens 	 */
33211544Seschrock 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) {
33221544Seschrock 		ZFS_EXIT(zfsvfs);
33231544Seschrock 		return (EAGAIN);
3324789Sahrens 	}
3325789Sahrens 
3326789Sahrens 	as_rangelock(as);
3327789Sahrens 	if ((flags & MAP_FIXED) == 0) {
3328789Sahrens 		map_addr(addrp, len, off, 1, flags);
3329789Sahrens 		if (*addrp == NULL) {
3330789Sahrens 			as_rangeunlock(as);
3331789Sahrens 			ZFS_EXIT(zfsvfs);
3332789Sahrens 			return (ENOMEM);
3333789Sahrens 		}
3334789Sahrens 	} else {
3335789Sahrens 		/*
3336789Sahrens 		 * User specified address - blow away any previous mappings
3337789Sahrens 		 */
3338789Sahrens 		(void) as_unmap(as, *addrp, len);
3339789Sahrens 	}
3340789Sahrens 
3341789Sahrens 	vn_a.vp = vp;
3342789Sahrens 	vn_a.offset = (u_offset_t)off;
3343789Sahrens 	vn_a.type = flags & MAP_TYPE;
3344789Sahrens 	vn_a.prot = prot;
3345789Sahrens 	vn_a.maxprot = maxprot;
3346789Sahrens 	vn_a.cred = cr;
3347789Sahrens 	vn_a.amp = NULL;
3348789Sahrens 	vn_a.flags = flags & ~MAP_TYPE;
33491417Skchow 	vn_a.szc = 0;
33501417Skchow 	vn_a.lgrp_mem_policy_flags = 0;
3351789Sahrens 
3352789Sahrens 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
3353789Sahrens 
3354789Sahrens 	as_rangeunlock(as);
3355789Sahrens 	ZFS_EXIT(zfsvfs);
3356789Sahrens 	return (error);
3357789Sahrens }
3358789Sahrens 
3359789Sahrens /* ARGSUSED */
3360789Sahrens static int
3361789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
3362789Sahrens     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
3363789Sahrens {
33641544Seschrock 	uint64_t pages = btopr(len);
33651544Seschrock 
33661544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
3367789Sahrens 	return (0);
3368789Sahrens }
3369789Sahrens 
33701773Seschrock /*
33711773Seschrock  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
33721773Seschrock  * more accurate mtime for the associated file.  Since we don't have a way of
33731773Seschrock  * detecting when the data was actually modified, we have to resort to
33741773Seschrock  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
33751773Seschrock  * last page is pushed.  The problem occurs when the msync() call is omitted,
33761773Seschrock  * which by far the most common case:
33771773Seschrock  *
33781773Seschrock  * 	open()
33791773Seschrock  * 	mmap()
33801773Seschrock  * 	<modify memory>
33811773Seschrock  * 	munmap()
33821773Seschrock  * 	close()
33831773Seschrock  * 	<time lapse>
33841773Seschrock  * 	putpage() via fsflush
33851773Seschrock  *
33861773Seschrock  * If we wait until fsflush to come along, we can have a modification time that
33871773Seschrock  * is some arbitrary point in the future.  In order to prevent this in the
33881773Seschrock  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
33891773Seschrock  * torn down.
33901773Seschrock  */
3391789Sahrens /* ARGSUSED */
3392789Sahrens static int
3393789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
3394789Sahrens     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr)
3395789Sahrens {
33961544Seschrock 	uint64_t pages = btopr(len);
33971544Seschrock 
33981544Seschrock 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
33991544Seschrock 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
34001773Seschrock 
34011773Seschrock 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
34021773Seschrock 	    vn_has_cached_data(vp))
34031773Seschrock 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr);
34041773Seschrock 
3405789Sahrens 	return (0);
3406789Sahrens }
3407789Sahrens 
3408789Sahrens /*
3409789Sahrens  * Free or allocate space in a file.  Currently, this function only
3410789Sahrens  * supports the `F_FREESP' command.  However, this command is somewhat
3411789Sahrens  * misnamed, as its functionality includes the ability to allocate as
3412789Sahrens  * well as free space.
3413789Sahrens  *
3414789Sahrens  *	IN:	vp	- vnode of file to free data in.
3415789Sahrens  *		cmd	- action to take (only F_FREESP supported).
3416789Sahrens  *		bfp	- section of file to free/alloc.
3417789Sahrens  *		flag	- current file open mode flags.
3418789Sahrens  *		offset	- current file offset.
3419789Sahrens  *		cr	- credentials of caller [UNUSED].
3420789Sahrens  *
3421789Sahrens  *	RETURN:	0 if success
3422789Sahrens  *		error code if failure
3423789Sahrens  *
3424789Sahrens  * Timestamps:
3425789Sahrens  *	vp - ctime|mtime updated
3426789Sahrens  */
3427789Sahrens /* ARGSUSED */
3428789Sahrens static int
3429789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
3430789Sahrens     offset_t offset, cred_t *cr, caller_context_t *ct)
3431789Sahrens {
3432789Sahrens 	dmu_tx_t	*tx;
3433789Sahrens 	znode_t		*zp = VTOZ(vp);
3434789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3435789Sahrens 	zilog_t		*zilog = zfsvfs->z_log;
34361669Sperrin 	rl_t		*rl;
3437789Sahrens 	uint64_t	seq = 0;
3438789Sahrens 	uint64_t	off, len;
3439789Sahrens 	int		error;
3440789Sahrens 
3441789Sahrens 	ZFS_ENTER(zfsvfs);
3442789Sahrens 
3443789Sahrens top:
3444789Sahrens 	if (cmd != F_FREESP) {
3445789Sahrens 		ZFS_EXIT(zfsvfs);
3446789Sahrens 		return (EINVAL);
3447789Sahrens 	}
3448789Sahrens 
3449789Sahrens 	if (error = convoff(vp, bfp, 0, offset)) {
3450789Sahrens 		ZFS_EXIT(zfsvfs);
3451789Sahrens 		return (error);
3452789Sahrens 	}
3453789Sahrens 
3454789Sahrens 	if (bfp->l_len < 0) {
3455789Sahrens 		ZFS_EXIT(zfsvfs);
3456789Sahrens 		return (EINVAL);
3457789Sahrens 	}
3458789Sahrens 
3459789Sahrens 	off = bfp->l_start;
34601669Sperrin 	len = bfp->l_len; /* 0 means from off to end of file */
3461789Sahrens 	tx = dmu_tx_create(zfsvfs->z_os);
34621669Sperrin 	dmu_tx_hold_bonus(tx, zp->z_id);
3463789Sahrens 	/*
34641669Sperrin 	 * If we will change zp_size (in zfs_freesp) then lock the whole file,
34651669Sperrin 	 * otherwise just lock the range being freed.
3466789Sahrens 	 */
34671669Sperrin 	if (len == 0 || off + len > zp->z_phys->zp_size) {
34681669Sperrin 		rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
34691669Sperrin 	} else {
34701669Sperrin 		rl = zfs_range_lock(zp, off, len, RL_WRITER);
34711669Sperrin 		/* recheck, in case zp_size changed */
34721669Sperrin 		if (off + len > zp->z_phys->zp_size) {
34731669Sperrin 			/* lost race: file size changed, lock whole file */
34741669Sperrin 			zfs_range_unlock(zp, rl);
34751669Sperrin 			rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
34761669Sperrin 		}
34771669Sperrin 	}
34781669Sperrin 
3479789Sahrens 	if (off + len > zp->z_blksz && zp->z_blksz < zfsvfs->z_max_blksz &&
3480789Sahrens 	    off >= zp->z_phys->zp_size) {
3481789Sahrens 		/*
3482789Sahrens 		 * We are increasing the length of the file,
3483789Sahrens 		 * and this may mean a block size increase.
3484789Sahrens 		 */
3485789Sahrens 		dmu_tx_hold_write(tx, zp->z_id, 0,
3486789Sahrens 		    MIN(off + len, zfsvfs->z_max_blksz));
3487789Sahrens 	} else if (off < zp->z_phys->zp_size) {
3488789Sahrens 		/*
3489789Sahrens 		 * If len == 0, we are truncating the file.
3490789Sahrens 		 */
3491789Sahrens 		dmu_tx_hold_free(tx, zp->z_id, off, len ? len : DMU_OBJECT_END);
3492789Sahrens 	}
3493789Sahrens 
3494789Sahrens 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
3495789Sahrens 	if (error) {
3496789Sahrens 		dmu_tx_abort(tx);
34971669Sperrin 		zfs_range_unlock(zp, rl);
3498789Sahrens 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
3499789Sahrens 			txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0);
3500789Sahrens 			goto top;
3501789Sahrens 		}
3502789Sahrens 		ZFS_EXIT(zfsvfs);
3503789Sahrens 		return (error);
3504789Sahrens 	}
3505789Sahrens 
3506789Sahrens 	error = zfs_freesp(zp, off, len, flag, tx, cr);
3507789Sahrens 
3508789Sahrens 	if (error == 0) {
3509789Sahrens 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
3510789Sahrens 		seq = zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
3511789Sahrens 	}
3512789Sahrens 
35131669Sperrin 	zfs_range_unlock(zp, rl);
3514789Sahrens 
3515789Sahrens 	dmu_tx_commit(tx);
3516789Sahrens 
3517789Sahrens 	zil_commit(zilog, seq, 0);
3518789Sahrens 
3519789Sahrens 	ZFS_EXIT(zfsvfs);
3520789Sahrens 	return (error);
3521789Sahrens }
3522789Sahrens 
3523789Sahrens static int
3524789Sahrens zfs_fid(vnode_t *vp, fid_t *fidp)
3525789Sahrens {
3526789Sahrens 	znode_t		*zp = VTOZ(vp);
3527789Sahrens 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3528789Sahrens 	uint32_t	gen = (uint32_t)zp->z_phys->zp_gen;
3529789Sahrens 	uint64_t	object = zp->z_id;
3530789Sahrens 	zfid_short_t	*zfid;
3531789Sahrens 	int		size, i;
3532789Sahrens 
3533789Sahrens 	ZFS_ENTER(zfsvfs);
3534789Sahrens 
3535789Sahrens 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
3536789Sahrens 	if (fidp->fid_len < size) {
3537789Sahrens 		fidp->fid_len = size;
35381512Sek110237 		ZFS_EXIT(zfsvfs);
3539789Sahrens 		return (ENOSPC);
3540789Sahrens 	}
3541789Sahrens 
3542789Sahrens 	zfid = (zfid_short_t *)fidp;
3543789Sahrens 
3544789Sahrens 	zfid->zf_len = size;
3545789Sahrens 
3546789Sahrens 	for (i = 0; i < sizeof (zfid->zf_object); i++)
3547789Sahrens 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
3548789Sahrens 
3549789Sahrens 	/* Must have a non-zero generation number to distinguish from .zfs */
3550789Sahrens 	if (gen == 0)
3551789Sahrens 		gen = 1;
3552789Sahrens 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
3553789Sahrens 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
3554789Sahrens 
3555789Sahrens 	if (size == LONG_FID_LEN) {
3556789Sahrens 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
3557789Sahrens 		zfid_long_t	*zlfid;
3558789Sahrens 
3559789Sahrens 		zlfid = (zfid_long_t *)fidp;
3560789Sahrens 
3561789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
3562789Sahrens 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
3563789Sahrens 
3564789Sahrens 		/* XXX - this should be the generation number for the objset */
3565789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
3566789Sahrens 			zlfid->zf_setgen[i] = 0;
3567789Sahrens 	}
3568789Sahrens 
3569789Sahrens 	ZFS_EXIT(zfsvfs);
3570789Sahrens 	return (0);
3571789Sahrens }
3572789Sahrens 
3573789Sahrens static int
3574789Sahrens zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr)
3575789Sahrens {
3576789Sahrens 	znode_t		*zp, *xzp;
3577789Sahrens 	zfsvfs_t	*zfsvfs;
3578789Sahrens 	zfs_dirlock_t	*dl;
3579789Sahrens 	int		error;
3580789Sahrens 
3581789Sahrens 	switch (cmd) {
3582789Sahrens 	case _PC_LINK_MAX:
3583789Sahrens 		*valp = ULONG_MAX;
3584789Sahrens 		return (0);
3585789Sahrens 
3586789Sahrens 	case _PC_FILESIZEBITS:
3587789Sahrens 		*valp = 64;
3588789Sahrens 		return (0);
3589789Sahrens 
3590789Sahrens 	case _PC_XATTR_EXISTS:
3591789Sahrens 		zp = VTOZ(vp);
3592789Sahrens 		zfsvfs = zp->z_zfsvfs;
3593789Sahrens 		ZFS_ENTER(zfsvfs);
3594789Sahrens 		*valp = 0;
3595789Sahrens 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
3596789Sahrens 		    ZXATTR | ZEXISTS | ZSHARED);
3597789Sahrens 		if (error == 0) {
3598789Sahrens 			zfs_dirent_unlock(dl);
3599789Sahrens 			if (!zfs_dirempty(xzp))
3600789Sahrens 				*valp = 1;
3601789Sahrens 			VN_RELE(ZTOV(xzp));
3602789Sahrens 		} else if (error == ENOENT) {
3603789Sahrens 			/*
3604789Sahrens 			 * If there aren't extended attributes, it's the
3605789Sahrens 			 * same as having zero of them.
3606789Sahrens 			 */
3607789Sahrens 			error = 0;
3608789Sahrens 		}
3609789Sahrens 		ZFS_EXIT(zfsvfs);
3610789Sahrens 		return (error);
3611789Sahrens 
3612789Sahrens 	case _PC_ACL_ENABLED:
3613789Sahrens 		*valp = _ACL_ACE_ENABLED;
3614789Sahrens 		return (0);
3615789Sahrens 
3616789Sahrens 	case _PC_MIN_HOLE_SIZE:
3617789Sahrens 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
3618789Sahrens 		return (0);
3619789Sahrens 
3620789Sahrens 	default:
3621789Sahrens 		return (fs_pathconf(vp, cmd, valp, cr));
3622789Sahrens 	}
3623789Sahrens }
3624789Sahrens 
3625789Sahrens /*ARGSUSED*/
3626789Sahrens static int
3627789Sahrens zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
3628789Sahrens {
3629789Sahrens 	znode_t *zp = VTOZ(vp);
3630789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3631789Sahrens 	int error;
3632789Sahrens 
3633789Sahrens 	ZFS_ENTER(zfsvfs);
3634789Sahrens 	error = zfs_getacl(zp, vsecp, cr);
3635789Sahrens 	ZFS_EXIT(zfsvfs);
3636789Sahrens 
3637789Sahrens 	return (error);
3638789Sahrens }
3639789Sahrens 
3640789Sahrens /*ARGSUSED*/
3641789Sahrens static int
3642789Sahrens zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
3643789Sahrens {
3644789Sahrens 	znode_t *zp = VTOZ(vp);
3645789Sahrens 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3646789Sahrens 	int error;
3647789Sahrens 
3648789Sahrens 	ZFS_ENTER(zfsvfs);
3649789Sahrens 	error = zfs_setacl(zp, vsecp, cr);
3650789Sahrens 	ZFS_EXIT(zfsvfs);
3651789Sahrens 	return (error);
3652789Sahrens }
3653789Sahrens 
3654789Sahrens /*
3655789Sahrens  * Predeclare these here so that the compiler assumes that
3656789Sahrens  * this is an "old style" function declaration that does
3657789Sahrens  * not include arguments => we won't get type mismatch errors
3658789Sahrens  * in the initializations that follow.
3659789Sahrens  */
3660789Sahrens static int zfs_inval();
3661789Sahrens static int zfs_isdir();
3662789Sahrens 
3663789Sahrens static int
3664789Sahrens zfs_inval()
3665789Sahrens {
3666789Sahrens 	return (EINVAL);
3667789Sahrens }
3668789Sahrens 
3669789Sahrens static int
3670789Sahrens zfs_isdir()
3671789Sahrens {
3672789Sahrens 	return (EISDIR);
3673789Sahrens }
3674789Sahrens /*
3675789Sahrens  * Directory vnode operations template
3676789Sahrens  */
3677789Sahrens vnodeops_t *zfs_dvnodeops;
3678789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = {
3679789Sahrens 	VOPNAME_OPEN, zfs_open,
3680789Sahrens 	VOPNAME_CLOSE, zfs_close,
3681789Sahrens 	VOPNAME_READ, zfs_isdir,
3682789Sahrens 	VOPNAME_WRITE, zfs_isdir,
3683789Sahrens 	VOPNAME_IOCTL, zfs_ioctl,
3684789Sahrens 	VOPNAME_GETATTR, zfs_getattr,
3685789Sahrens 	VOPNAME_SETATTR, zfs_setattr,
3686789Sahrens 	VOPNAME_ACCESS, zfs_access,
3687789Sahrens 	VOPNAME_LOOKUP, zfs_lookup,
3688789Sahrens 	VOPNAME_CREATE, zfs_create,
3689789Sahrens 	VOPNAME_REMOVE, zfs_remove,
3690789Sahrens 	VOPNAME_LINK, zfs_link,
3691789Sahrens 	VOPNAME_RENAME, zfs_rename,
3692789Sahrens 	VOPNAME_MKDIR, zfs_mkdir,
3693789Sahrens 	VOPNAME_RMDIR, zfs_rmdir,
3694789Sahrens 	VOPNAME_READDIR, zfs_readdir,
3695789Sahrens 	VOPNAME_SYMLINK, zfs_symlink,
3696789Sahrens 	VOPNAME_FSYNC, zfs_fsync,
3697789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3698789Sahrens 	VOPNAME_FID, zfs_fid,
3699789Sahrens 	VOPNAME_SEEK, zfs_seek,
3700789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3701789Sahrens 	VOPNAME_GETSECATTR, zfs_getsecattr,
3702789Sahrens 	VOPNAME_SETSECATTR, zfs_setsecattr,
3703789Sahrens 	NULL, NULL
3704789Sahrens };
3705789Sahrens 
3706789Sahrens /*
3707789Sahrens  * Regular file vnode operations template
3708789Sahrens  */
3709789Sahrens vnodeops_t *zfs_fvnodeops;
3710789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = {
3711789Sahrens 	VOPNAME_OPEN, zfs_open,
3712789Sahrens 	VOPNAME_CLOSE, zfs_close,
3713789Sahrens 	VOPNAME_READ, zfs_read,
3714789Sahrens 	VOPNAME_WRITE, zfs_write,
3715789Sahrens 	VOPNAME_IOCTL, zfs_ioctl,
3716789Sahrens 	VOPNAME_GETATTR, zfs_getattr,
3717789Sahrens 	VOPNAME_SETATTR, zfs_setattr,
3718789Sahrens 	VOPNAME_ACCESS, zfs_access,
3719789Sahrens 	VOPNAME_LOOKUP, zfs_lookup,
3720789Sahrens 	VOPNAME_RENAME, zfs_rename,
3721789Sahrens 	VOPNAME_FSYNC, zfs_fsync,
3722789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p)zfs_inactive,
3723789Sahrens 	VOPNAME_FID, zfs_fid,
3724789Sahrens 	VOPNAME_SEEK, zfs_seek,
3725789Sahrens 	VOPNAME_FRLOCK, zfs_frlock,
3726789Sahrens 	VOPNAME_SPACE, zfs_space,
3727789Sahrens 	VOPNAME_GETPAGE, zfs_getpage,
3728789Sahrens 	VOPNAME_PUTPAGE, zfs_putpage,
3729789Sahrens 	VOPNAME_MAP, (fs_generic_func_p) zfs_map,
3730789Sahrens 	VOPNAME_ADDMAP, (fs_generic_func_p) zfs_addmap,
3731789Sahrens 	VOPNAME_DELMAP, zfs_delmap,
3732789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3733789Sahrens 	VOPNAME_GETSECATTR, zfs_getsecattr,
3734789Sahrens 	VOPNAME_SETSECATTR, zfs_setsecattr,
3735789Sahrens 	VOPNAME_VNEVENT, fs_vnevent_support,
3736789Sahrens 	NULL, NULL
3737789Sahrens };
3738789Sahrens 
3739789Sahrens /*
3740789Sahrens  * Symbolic link vnode operations template
3741789Sahrens  */
3742789Sahrens vnodeops_t *zfs_symvnodeops;
3743789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = {
3744789Sahrens 	VOPNAME_GETATTR, zfs_getattr,
3745789Sahrens 	VOPNAME_SETATTR, zfs_setattr,
3746789Sahrens 	VOPNAME_ACCESS, zfs_access,
3747789Sahrens 	VOPNAME_RENAME, zfs_rename,
3748789Sahrens 	VOPNAME_READLINK, zfs_readlink,
3749789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3750789Sahrens 	VOPNAME_FID, zfs_fid,
3751789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3752789Sahrens 	VOPNAME_VNEVENT, fs_vnevent_support,
3753789Sahrens 	NULL, NULL
3754789Sahrens };
3755789Sahrens 
3756789Sahrens /*
3757789Sahrens  * Extended attribute directory vnode operations template
3758789Sahrens  *	This template is identical to the directory vnodes
3759789Sahrens  *	operation template except for restricted operations:
3760789Sahrens  *		VOP_MKDIR()
3761789Sahrens  *		VOP_SYMLINK()
3762789Sahrens  * Note that there are other restrictions embedded in:
3763789Sahrens  *	zfs_create()	- restrict type to VREG
3764789Sahrens  *	zfs_link()	- no links into/out of attribute space
3765789Sahrens  *	zfs_rename()	- no moves into/out of attribute space
3766789Sahrens  */
3767789Sahrens vnodeops_t *zfs_xdvnodeops;
3768789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = {
3769789Sahrens 	VOPNAME_OPEN, zfs_open,
3770789Sahrens 	VOPNAME_CLOSE, zfs_close,
3771789Sahrens 	VOPNAME_IOCTL, zfs_ioctl,
3772789Sahrens 	VOPNAME_GETATTR, zfs_getattr,
3773789Sahrens 	VOPNAME_SETATTR, zfs_setattr,
3774789Sahrens 	VOPNAME_ACCESS, zfs_access,
3775789Sahrens 	VOPNAME_LOOKUP, zfs_lookup,
3776789Sahrens 	VOPNAME_CREATE, zfs_create,
3777789Sahrens 	VOPNAME_REMOVE, zfs_remove,
3778789Sahrens 	VOPNAME_LINK, zfs_link,
3779789Sahrens 	VOPNAME_RENAME, zfs_rename,
3780789Sahrens 	VOPNAME_MKDIR, zfs_inval,
3781789Sahrens 	VOPNAME_RMDIR, zfs_rmdir,
3782789Sahrens 	VOPNAME_READDIR, zfs_readdir,
3783789Sahrens 	VOPNAME_SYMLINK, zfs_inval,
3784789Sahrens 	VOPNAME_FSYNC, zfs_fsync,
3785789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3786789Sahrens 	VOPNAME_FID, zfs_fid,
3787789Sahrens 	VOPNAME_SEEK, zfs_seek,
3788789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3789789Sahrens 	VOPNAME_GETSECATTR, zfs_getsecattr,
3790789Sahrens 	VOPNAME_SETSECATTR, zfs_setsecattr,
3791789Sahrens 	VOPNAME_VNEVENT, fs_vnevent_support,
3792789Sahrens 	NULL, NULL
3793789Sahrens };
3794789Sahrens 
3795789Sahrens /*
3796789Sahrens  * Error vnode operations template
3797789Sahrens  */
3798789Sahrens vnodeops_t *zfs_evnodeops;
3799789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = {
3800789Sahrens 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3801789Sahrens 	VOPNAME_PATHCONF, zfs_pathconf,
3802789Sahrens 	NULL, NULL
3803789Sahrens };
3804