xref: /titanic_50/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 657b1f3d64bcf8eaa2385dba72a6047f089433b2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/time.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/resource.h>
34 #include <sys/vfs.h>
35 #include <sys/vnode.h>
36 #include <sys/file.h>
37 #include <sys/stat.h>
38 #include <sys/kmem.h>
39 #include <sys/taskq.h>
40 #include <sys/uio.h>
41 #include <sys/vmsystm.h>
42 #include <sys/atomic.h>
43 #include <sys/vm.h>
44 #include <vm/seg_vn.h>
45 #include <vm/pvn.h>
46 #include <vm/as.h>
47 #include <sys/mman.h>
48 #include <sys/pathname.h>
49 #include <sys/cmn_err.h>
50 #include <sys/errno.h>
51 #include <sys/unistd.h>
52 #include <sys/zfs_vfsops.h>
53 #include <sys/zfs_dir.h>
54 #include <sys/zfs_acl.h>
55 #include <sys/zfs_ioctl.h>
56 #include <sys/fs/zfs.h>
57 #include <sys/dmu.h>
58 #include <sys/spa.h>
59 #include <sys/txg.h>
60 #include <sys/dbuf.h>
61 #include <sys/zap.h>
62 #include <sys/dirent.h>
63 #include <sys/policy.h>
64 #include <sys/sunddi.h>
65 #include <sys/filio.h>
66 #include "fs/fs_subr.h"
67 #include <sys/zfs_ctldir.h>
68 #include <sys/dnlc.h>
69 #include <sys/zfs_rlock.h>
70 
71 /*
72  * Programming rules.
73  *
74  * Each vnode op performs some logical unit of work.  To do this, the ZPL must
75  * properly lock its in-core state, create a DMU transaction, do the work,
76  * record this work in the intent log (ZIL), commit the DMU transaction,
77  * and wait the the intent log to commit if it's is a synchronous operation.
78  * Morover, the vnode ops must work in both normal and log replay context.
79  * The ordering of events is important to avoid deadlocks and references
80  * to freed memory.  The example below illustrates the following Big Rules:
81  *
82  *  (1) A check must be made in each zfs thread for a mounted file system.
83  *	This is done avoiding races using ZFS_ENTER(zfsvfs).
84  *	A ZFS_EXIT(zfsvfs) is needed before all returns.
85  *
86  *  (2)	VN_RELE() should always be the last thing except for zil_commit()
87  *	(if necessary) and ZFS_EXIT(). This is for 3 reasons:
88  *	First, if it's the last reference, the vnode/znode
89  *	can be freed, so the zp may point to freed memory.  Second, the last
90  *	reference will call zfs_zinactive(), which may induce a lot of work --
91  *	pushing cached pages (which acquires range locks) and syncing out
92  *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
93  *	which could deadlock the system if you were already holding one.
94  *
95  *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
96  *	as they can span dmu_tx_assign() calls.
97  *
98  *  (4)	Always pass zfsvfs->z_assign as the second argument to dmu_tx_assign().
99  *	In normal operation, this will be TXG_NOWAIT.  During ZIL replay,
100  *	it will be a specific txg.  Either way, dmu_tx_assign() never blocks.
101  *	This is critical because we don't want to block while holding locks.
102  *	Note, in particular, that if a lock is sometimes acquired before
103  *	the tx assigns, and sometimes after (e.g. z_lock), then failing to
104  *	use a non-blocking assign can deadlock the system.  The scenario:
105  *
106  *	Thread A has grabbed a lock before calling dmu_tx_assign().
107  *	Thread B is in an already-assigned tx, and blocks for this lock.
108  *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
109  *	forever, because the previous txg can't quiesce until B's tx commits.
110  *
111  *	If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
112  *	then drop all locks, call dmu_tx_wait(), and try again.
113  *
114  *  (5)	If the operation succeeded, generate the intent log entry for it
115  *	before dropping locks.  This ensures that the ordering of events
116  *	in the intent log matches the order in which they actually occurred.
117  *
118  *  (6)	At the end of each vnode op, the DMU tx must always commit,
119  *	regardless of whether there were any errors.
120  *
121  *  (7)	After dropping all locks, invoke zil_commit(zilog, seq, foid)
122  *	to ensure that synchronous semantics are provided when necessary.
123  *
124  * In general, this is how things should be ordered in each vnode op:
125  *
126  *	ZFS_ENTER(zfsvfs);		// exit if unmounted
127  * top:
128  *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
129  *	rw_enter(...);			// grab any other locks you need
130  *	tx = dmu_tx_create(...);	// get DMU tx
131  *	dmu_tx_hold_*();		// hold each object you might modify
132  *	error = dmu_tx_assign(tx, zfsvfs->z_assign);	// try to assign
133  *	if (error) {
134  *		rw_exit(...);		// drop locks
135  *		zfs_dirent_unlock(dl);	// unlock directory entry
136  *		VN_RELE(...);		// release held vnodes
137  *		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
138  *			dmu_tx_wait(tx);
139  *			dmu_tx_abort(tx);
140  *			goto top;
141  *		}
142  *		dmu_tx_abort(tx);	// abort DMU tx
143  *		ZFS_EXIT(zfsvfs);	// finished in zfs
144  *		return (error);		// really out of space
145  *	}
146  *	error = do_real_work();		// do whatever this VOP does
147  *	if (error == 0)
148  *		zfs_log_*(...);		// on success, make ZIL entry
149  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
150  *	rw_exit(...);			// drop locks
151  *	zfs_dirent_unlock(dl);		// unlock directory entry
152  *	VN_RELE(...);			// release held vnodes
153  *	zil_commit(zilog, seq, foid);	// synchronous when necessary
154  *	ZFS_EXIT(zfsvfs);		// finished in zfs
155  *	return (error);			// done, report error
156  */
157 /* ARGSUSED */
158 static int
159 zfs_open(vnode_t **vpp, int flag, cred_t *cr)
160 {
161 	znode_t	*zp = VTOZ(*vpp);
162 
163 	/* Keep a count of the synchronous opens in the znode */
164 	if (flag & (FSYNC | FDSYNC))
165 		atomic_inc_32(&zp->z_sync_cnt);
166 	return (0);
167 }
168 
169 /* ARGSUSED */
170 static int
171 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr)
172 {
173 	znode_t	*zp = VTOZ(vp);
174 
175 	/* Decrement the synchronous opens in the znode */
176 	if (flag & (FSYNC | FDSYNC))
177 		atomic_dec_32(&zp->z_sync_cnt);
178 
179 	/*
180 	 * Clean up any locks held by this process on the vp.
181 	 */
182 	cleanlocks(vp, ddi_get_pid(), 0);
183 	cleanshares(vp, ddi_get_pid());
184 
185 	return (0);
186 }
187 
188 /*
189  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
190  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
191  */
192 static int
193 zfs_holey(vnode_t *vp, int cmd, offset_t *off)
194 {
195 	znode_t	*zp = VTOZ(vp);
196 	uint64_t noff = (uint64_t)*off; /* new offset */
197 	uint64_t file_sz;
198 	int error;
199 	boolean_t hole;
200 
201 	file_sz = zp->z_phys->zp_size;
202 	if (noff >= file_sz)  {
203 		return (ENXIO);
204 	}
205 
206 	if (cmd == _FIO_SEEK_HOLE)
207 		hole = B_TRUE;
208 	else
209 		hole = B_FALSE;
210 
211 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
212 
213 	/* end of file? */
214 	if ((error == ESRCH) || (noff > file_sz)) {
215 		/*
216 		 * Handle the virtual hole at the end of file.
217 		 */
218 		if (hole) {
219 			*off = file_sz;
220 			return (0);
221 		}
222 		return (ENXIO);
223 	}
224 
225 	if (noff < *off)
226 		return (error);
227 	*off = noff;
228 	return (error);
229 }
230 
231 /* ARGSUSED */
232 static int
233 zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
234     int *rvalp)
235 {
236 	offset_t off;
237 	int error;
238 	zfsvfs_t *zfsvfs;
239 
240 	switch (com) {
241 	    case _FIOFFS:
242 		return (zfs_sync(vp->v_vfsp, 0, cred));
243 
244 		/*
245 		 * The following two ioctls are used by bfu.  Faking out,
246 		 * necessary to avoid bfu errors.
247 		 */
248 	    case _FIOGDIO:
249 	    case _FIOSDIO:
250 		return (0);
251 
252 	    case _FIO_SEEK_DATA:
253 	    case _FIO_SEEK_HOLE:
254 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
255 			return (EFAULT);
256 
257 		zfsvfs = VTOZ(vp)->z_zfsvfs;
258 		ZFS_ENTER(zfsvfs);
259 
260 		/* offset parameter is in/out */
261 		error = zfs_holey(vp, com, &off);
262 		ZFS_EXIT(zfsvfs);
263 		if (error)
264 			return (error);
265 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
266 			return (EFAULT);
267 		return (0);
268 	}
269 	return (ENOTTY);
270 }
271 
272 /*
273  * When a file is memory mapped, we must keep the IO data synchronized
274  * between the DMU cache and the memory mapped pages.  What this means:
275  *
276  * On Write:	If we find a memory mapped page, we write to *both*
277  *		the page and the dmu buffer.
278  *
279  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
280  *	the file is memory mapped.
281  */
282 static int
283 mappedwrite(vnode_t *vp, uint64_t woff, int nbytes, uio_t *uio, dmu_tx_t *tx)
284 {
285 	znode_t	*zp = VTOZ(vp);
286 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
287 	int64_t	start, off;
288 	int len = nbytes;
289 	int error = 0;
290 
291 	start = uio->uio_loffset;
292 	off = start & PAGEOFFSET;
293 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
294 		page_t *pp;
295 		uint64_t bytes = MIN(PAGESIZE - off, len);
296 
297 		/*
298 		 * We don't want a new page to "appear" in the middle of
299 		 * the file update (because it may not get the write
300 		 * update data), so we grab a lock to block
301 		 * zfs_getpage().
302 		 */
303 		rw_enter(&zp->z_map_lock, RW_WRITER);
304 		if (pp = page_lookup(vp, start, SE_SHARED)) {
305 			caddr_t va;
306 
307 			rw_exit(&zp->z_map_lock);
308 			va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L);
309 			error = uiomove(va+off, bytes, UIO_WRITE, uio);
310 			if (error == 0) {
311 				dmu_write(zfsvfs->z_os, zp->z_id,
312 				    woff, bytes, va+off, tx);
313 			}
314 			ppmapout(va);
315 			page_unlock(pp);
316 		} else {
317 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
318 			    woff, bytes, uio, tx);
319 			rw_exit(&zp->z_map_lock);
320 		}
321 		len -= bytes;
322 		woff += bytes;
323 		off = 0;
324 		if (error)
325 			break;
326 	}
327 	return (error);
328 }
329 
330 /*
331  * When a file is memory mapped, we must keep the IO data synchronized
332  * between the DMU cache and the memory mapped pages.  What this means:
333  *
334  * On Read:	We "read" preferentially from memory mapped pages,
335  *		else we default from the dmu buffer.
336  *
337  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
338  *	the file is memory mapped.
339  */
340 static int
341 mappedread(vnode_t *vp, char *addr, int nbytes, uio_t *uio)
342 {
343 	int64_t	start, off, bytes;
344 	int len = nbytes;
345 	int error = 0;
346 
347 	start = uio->uio_loffset;
348 	off = start & PAGEOFFSET;
349 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
350 		page_t *pp;
351 
352 		bytes = MIN(PAGESIZE - off, len);
353 		if (pp = page_lookup(vp, start, SE_SHARED)) {
354 			caddr_t va;
355 
356 			va = ppmapin(pp, PROT_READ, (caddr_t)-1L);
357 			error = uiomove(va + off, bytes, UIO_READ, uio);
358 			ppmapout(va);
359 			page_unlock(pp);
360 		} else {
361 			/* XXX use dmu_read here? */
362 			error = uiomove(addr, bytes, UIO_READ, uio);
363 		}
364 		len -= bytes;
365 		addr += bytes;
366 		off = 0;
367 		if (error)
368 			break;
369 	}
370 	return (error);
371 }
372 
373 uint_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
374 
375 /*
376  * Read bytes from specified file into supplied buffer.
377  *
378  *	IN:	vp	- vnode of file to be read from.
379  *		uio	- structure supplying read location, range info,
380  *			  and return buffer.
381  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
382  *		cr	- credentials of caller.
383  *
384  *	OUT:	uio	- updated offset and range, buffer filled.
385  *
386  *	RETURN:	0 if success
387  *		error code if failure
388  *
389  * Side Effects:
390  *	vp - atime updated if byte count > 0
391  */
392 /* ARGSUSED */
393 static int
394 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
395 {
396 	znode_t		*zp = VTOZ(vp);
397 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
398 	uint64_t	delta;
399 	ssize_t		n, size, cnt, ndone;
400 	int		error, i, numbufs;
401 	dmu_buf_t	*dbp, **dbpp;
402 	rl_t		*rl;
403 
404 	ZFS_ENTER(zfsvfs);
405 
406 	/*
407 	 * Validate file offset
408 	 */
409 	if (uio->uio_loffset < (offset_t)0) {
410 		ZFS_EXIT(zfsvfs);
411 		return (EINVAL);
412 	}
413 
414 	/*
415 	 * Fasttrack empty reads
416 	 */
417 	if (uio->uio_resid == 0) {
418 		ZFS_EXIT(zfsvfs);
419 		return (0);
420 	}
421 
422 	/*
423 	 * Check for mandatory locks
424 	 */
425 	if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
426 		if (error = chklock(vp, FREAD,
427 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
428 			ZFS_EXIT(zfsvfs);
429 			return (error);
430 		}
431 	}
432 
433 	/*
434 	 * If we're in FRSYNC mode, sync out this znode before reading it.
435 	 */
436 	if (ioflag & FRSYNC)
437 		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
438 
439 	/*
440 	 * Lock the range against changes.
441 	 */
442 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
443 
444 	/*
445 	 * If we are reading past end-of-file we can skip
446 	 * to the end; but we might still need to set atime.
447 	 */
448 	if (uio->uio_loffset >= zp->z_phys->zp_size) {
449 		cnt = 0;
450 		error = 0;
451 		goto out;
452 	}
453 
454 	cnt = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset);
455 
456 	for (ndone = 0; ndone < cnt; ndone += zfs_read_chunk_size) {
457 		ASSERT(uio->uio_loffset < zp->z_phys->zp_size);
458 		n = MIN(zfs_read_chunk_size,
459 		    zp->z_phys->zp_size - uio->uio_loffset);
460 		n = MIN(n, cnt);
461 		error = dmu_buf_hold_array_by_bonus(zp->z_dbuf,
462 		    uio->uio_loffset, n, TRUE, FTAG, &numbufs, &dbpp);
463 		if (error)
464 			goto out;
465 		/*
466 		 * Compute the adjustment to align the dmu buffers
467 		 * with the uio buffer.
468 		 */
469 		delta = uio->uio_loffset - dbpp[0]->db_offset;
470 
471 		for (i = 0; i < numbufs; i++) {
472 			if (n < 0)
473 				break;
474 			dbp = dbpp[i];
475 			size = dbp->db_size - delta;
476 			/*
477 			 * XXX -- this is correct, but may be suboptimal.
478 			 * If the pages are all clean, we don't need to
479 			 * go through mappedread().  Maybe the VMODSORT
480 			 * stuff can help us here.
481 			 */
482 			if (vn_has_cached_data(vp)) {
483 				error = mappedread(vp, (caddr_t)dbp->db_data +
484 				    delta, (n < size ? n : size), uio);
485 			} else {
486 				error = uiomove((caddr_t)dbp->db_data + delta,
487 					(n < size ? n : size), UIO_READ, uio);
488 			}
489 			if (error) {
490 				dmu_buf_rele_array(dbpp, numbufs, FTAG);
491 				goto out;
492 			}
493 			n -= dbp->db_size;
494 			if (delta) {
495 				n += delta;
496 				delta = 0;
497 			}
498 		}
499 		dmu_buf_rele_array(dbpp, numbufs, FTAG);
500 	}
501 out:
502 	zfs_range_unlock(rl);
503 
504 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
505 	ZFS_EXIT(zfsvfs);
506 	return (error);
507 }
508 
509 /*
510  * Fault in the pages of the first n bytes specified by the uio structure.
511  * 1 byte in each page is touched and the uio struct is unmodified.
512  * Any error will exit this routine as this is only a best
513  * attempt to get the pages resident. This is a copy of ufs_trans_touch().
514  */
515 static void
516 zfs_prefault_write(ssize_t n, struct uio *uio)
517 {
518 	struct iovec *iov;
519 	ulong_t cnt, incr;
520 	caddr_t p;
521 	uint8_t tmp;
522 
523 	iov = uio->uio_iov;
524 
525 	while (n) {
526 		cnt = MIN(iov->iov_len, n);
527 		if (cnt == 0) {
528 			/* empty iov entry */
529 			iov++;
530 			continue;
531 		}
532 		n -= cnt;
533 		/*
534 		 * touch each page in this segment.
535 		 */
536 		p = iov->iov_base;
537 		while (cnt) {
538 			switch (uio->uio_segflg) {
539 			case UIO_USERSPACE:
540 			case UIO_USERISPACE:
541 				if (fuword8(p, &tmp))
542 					return;
543 				break;
544 			case UIO_SYSSPACE:
545 				if (kcopy(p, &tmp, 1))
546 					return;
547 				break;
548 			}
549 			incr = MIN(cnt, PAGESIZE);
550 			p += incr;
551 			cnt -= incr;
552 		}
553 		/*
554 		 * touch the last byte in case it straddles a page.
555 		 */
556 		p--;
557 		switch (uio->uio_segflg) {
558 		case UIO_USERSPACE:
559 		case UIO_USERISPACE:
560 			if (fuword8(p, &tmp))
561 				return;
562 			break;
563 		case UIO_SYSSPACE:
564 			if (kcopy(p, &tmp, 1))
565 				return;
566 			break;
567 		}
568 		iov++;
569 	}
570 }
571 
572 /*
573  * Write the bytes to a file.
574  *
575  *	IN:	vp	- vnode of file to be written to.
576  *		uio	- structure supplying write location, range info,
577  *			  and data buffer.
578  *		ioflag	- FAPPEND flag set if in append mode.
579  *		cr	- credentials of caller.
580  *
581  *	OUT:	uio	- updated offset and range.
582  *
583  *	RETURN:	0 if success
584  *		error code if failure
585  *
586  * Timestamps:
587  *	vp - ctime|mtime updated if byte count > 0
588  */
589 /* ARGSUSED */
590 static int
591 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
592 {
593 	znode_t		*zp = VTOZ(vp);
594 	rlim64_t	limit = uio->uio_llimit;
595 	ssize_t		start_resid = uio->uio_resid;
596 	ssize_t		tx_bytes;
597 	uint64_t	end_size;
598 	dmu_tx_t	*tx;
599 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
600 	zilog_t		*zilog = zfsvfs->z_log;
601 	offset_t	woff;
602 	ssize_t		n, nbytes;
603 	rl_t		*rl;
604 	int		max_blksz = zfsvfs->z_max_blksz;
605 	int		error;
606 
607 	/*
608 	 * Fasttrack empty write
609 	 */
610 	n = start_resid;
611 	if (n == 0)
612 		return (0);
613 
614 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
615 		limit = MAXOFFSET_T;
616 
617 	ZFS_ENTER(zfsvfs);
618 
619 	/*
620 	 * Pre-fault the pages to ensure slow (eg NFS) pages
621 	 * don't hold up txg.
622 	 */
623 	zfs_prefault_write(n, uio);
624 
625 	/*
626 	 * If in append mode, set the io offset pointer to eof.
627 	 */
628 	if (ioflag & FAPPEND) {
629 		/*
630 		 * Range lock for a file append:
631 		 * The value for the start of range will be determined by
632 		 * zfs_range_lock() (to guarantee append semantics).
633 		 * If this write will cause the block size to increase,
634 		 * zfs_range_lock() will lock the entire file, so we must
635 		 * later reduce the range after we grow the block size.
636 		 */
637 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
638 		if (rl->r_len == UINT64_MAX) {
639 			/* overlocked, zp_size can't change */
640 			woff = uio->uio_loffset = zp->z_phys->zp_size;
641 		} else {
642 			woff = uio->uio_loffset = rl->r_off;
643 		}
644 	} else {
645 		woff = uio->uio_loffset;
646 		/*
647 		 * Validate file offset
648 		 */
649 		if (woff < 0) {
650 			ZFS_EXIT(zfsvfs);
651 			return (EINVAL);
652 		}
653 
654 		/*
655 		 * If we need to grow the block size then zfs_range_lock()
656 		 * will lock a wider range than we request here.
657 		 * Later after growing the block size we reduce the range.
658 		 */
659 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
660 	}
661 
662 	if (woff >= limit) {
663 		error = EFBIG;
664 		goto no_tx_done;
665 	}
666 
667 	if ((woff + n) > limit || woff > (limit - n))
668 		n = limit - woff;
669 
670 	/*
671 	 * Check for mandatory locks
672 	 */
673 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) &&
674 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0)
675 		goto no_tx_done;
676 	end_size = MAX(zp->z_phys->zp_size, woff + n);
677 top:
678 	tx = dmu_tx_create(zfsvfs->z_os);
679 	dmu_tx_hold_bonus(tx, zp->z_id);
680 	dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
681 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
682 	if (error) {
683 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
684 			dmu_tx_wait(tx);
685 			dmu_tx_abort(tx);
686 			goto top;
687 		}
688 		dmu_tx_abort(tx);
689 		goto no_tx_done;
690 	}
691 
692 	/*
693 	 * If zfs_range_lock() over-locked we grow the blocksize
694 	 * and then reduce the lock range.
695 	 */
696 	if (rl->r_len == UINT64_MAX) {
697 		uint64_t new_blksz;
698 
699 		if (zp->z_blksz > max_blksz) {
700 			ASSERT(!ISP2(zp->z_blksz));
701 			new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
702 		} else {
703 			new_blksz = MIN(end_size, max_blksz);
704 		}
705 		zfs_grow_blocksize(zp, new_blksz, tx);
706 		zfs_range_reduce(rl, woff, n);
707 	}
708 
709 	/*
710 	 * The file data does not fit in the znode "cache", so we
711 	 * will be writing to the file block data buffers.
712 	 * Each buffer will be written in a separate transaction;
713 	 * this keeps the intent log records small and allows us
714 	 * to do more fine-grained space accounting.
715 	 */
716 	while (n > 0) {
717 		/*
718 		 * XXX - should we really limit each write to z_max_blksz?
719 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
720 		 */
721 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
722 		rw_enter(&zp->z_map_lock, RW_READER);
723 
724 		tx_bytes = uio->uio_resid;
725 		if (vn_has_cached_data(vp)) {
726 			rw_exit(&zp->z_map_lock);
727 			error = mappedwrite(vp, woff, nbytes, uio, tx);
728 		} else {
729 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
730 			    woff, nbytes, uio, tx);
731 			rw_exit(&zp->z_map_lock);
732 		}
733 		tx_bytes -= uio->uio_resid;
734 
735 		if (error) {
736 			/* XXX - do we need to "clean up" the dmu buffer? */
737 			break;
738 		}
739 
740 		ASSERT(tx_bytes == nbytes);
741 
742 		/*
743 		 * Clear Set-UID/Set-GID bits on successful write if not
744 		 * privileged and at least one of the excute bits is set.
745 		 *
746 		 * It would be nice to to this after all writes have
747 		 * been done, but that would still expose the ISUID/ISGID
748 		 * to another app after the partial write is committed.
749 		 */
750 
751 		mutex_enter(&zp->z_acl_lock);
752 		if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) |
753 		    (S_IXUSR >> 6))) != 0 &&
754 		    (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 &&
755 		    secpolicy_vnode_setid_retain(cr,
756 		    (zp->z_phys->zp_mode & S_ISUID) != 0 &&
757 		    zp->z_phys->zp_uid == 0) != 0) {
758 			    zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID);
759 		}
760 		mutex_exit(&zp->z_acl_lock);
761 
762 		n -= nbytes;
763 		if (n <= 0)
764 			break;
765 
766 		/*
767 		 * We have more work ahead of us, so wrap up this transaction
768 		 * and start another.  Exact same logic as tx_done below.
769 		 */
770 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) {
771 			dmu_buf_will_dirty(zp->z_dbuf, tx);
772 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
773 			    uio->uio_loffset);
774 		}
775 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
776 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes,
777 		    ioflag, uio);
778 		dmu_tx_commit(tx);
779 
780 		/*
781 		 * Start another transaction.
782 		 */
783 		woff = uio->uio_loffset;
784 		tx = dmu_tx_create(zfsvfs->z_os);
785 		dmu_tx_hold_bonus(tx, zp->z_id);
786 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
787 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
788 		if (error) {
789 			if (error == ERESTART &&
790 			    zfsvfs->z_assign == TXG_NOWAIT) {
791 				dmu_tx_wait(tx);
792 				dmu_tx_abort(tx);
793 				goto top;
794 			}
795 			dmu_tx_abort(tx);
796 			goto no_tx_done;
797 		}
798 	}
799 
800 tx_done:
801 
802 	if (tx_bytes != 0) {
803 		/*
804 		 * Update the file size if it has changed; account
805 		 * for possible concurrent updates.
806 		 */
807 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) {
808 			dmu_buf_will_dirty(zp->z_dbuf, tx);
809 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
810 			    uio->uio_loffset);
811 		}
812 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
813 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes,
814 		    ioflag, uio);
815 	}
816 	dmu_tx_commit(tx);
817 
818 
819 no_tx_done:
820 
821 	zfs_range_unlock(rl);
822 
823 	/*
824 	 * If we're in replay mode, or we made no progress, return error.
825 	 * Otherwise, it's at least a partial write, so it's successful.
826 	 */
827 	if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) {
828 		ZFS_EXIT(zfsvfs);
829 		return (error);
830 	}
831 
832 	if (ioflag & (FSYNC | FDSYNC))
833 		zil_commit(zilog, zp->z_last_itx, zp->z_id);
834 
835 	ZFS_EXIT(zfsvfs);
836 	return (0);
837 }
838 
839 void
840 zfs_get_done(dmu_buf_t *db, void *vzgd)
841 {
842 	zgd_t *zgd = (zgd_t *)vzgd;
843 	rl_t *rl = zgd->zgd_rl;
844 	vnode_t *vp = ZTOV(rl->r_zp);
845 
846 	dmu_buf_rele(db, vzgd);
847 	zfs_range_unlock(rl);
848 	VN_RELE(vp);
849 	zil_add_vdev(zgd->zgd_zilog, DVA_GET_VDEV(BP_IDENTITY(zgd->zgd_bp)));
850 	kmem_free(zgd, sizeof (zgd_t));
851 }
852 
853 /*
854  * Get data to generate a TX_WRITE intent log record.
855  */
856 int
857 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
858 {
859 	zfsvfs_t *zfsvfs = arg;
860 	objset_t *os = zfsvfs->z_os;
861 	znode_t *zp;
862 	uint64_t off = lr->lr_offset;
863 	dmu_buf_t *db;
864 	rl_t *rl;
865 	zgd_t *zgd;
866 	int dlen = lr->lr_length;  		/* length of user data */
867 	int error = 0;
868 
869 	ASSERT(zio);
870 	ASSERT(dlen != 0);
871 
872 	/*
873 	 * Nothing to do if the file has been removed
874 	 */
875 	if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0)
876 		return (ENOENT);
877 	if (zp->z_reap) {
878 		VN_RELE(ZTOV(zp));
879 		return (ENOENT);
880 	}
881 
882 	/*
883 	 * Write records come in two flavors: immediate and indirect.
884 	 * For small writes it's cheaper to store the data with the
885 	 * log record (immediate); for large writes it's cheaper to
886 	 * sync the data and get a pointer to it (indirect) so that
887 	 * we don't have to write the data twice.
888 	 */
889 	if (buf != NULL) { /* immediate write */
890 		rl = zfs_range_lock(zp, off, dlen, RL_READER);
891 		/* test for truncation needs to be done while range locked */
892 		if (off >= zp->z_phys->zp_size) {
893 			error = ENOENT;
894 			goto out;
895 		}
896 		VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf));
897 	} else { /* indirect write */
898 		uint64_t boff; /* block starting offset */
899 
900 		ASSERT3U(dlen, <=, zp->z_blksz);
901 		/*
902 		 * Have to lock the whole block to ensure when it's
903 		 * written out and it's checksum is being calculated
904 		 * that no one can change the data. We need to re-check
905 		 * blocksize after we get the lock in case it's changed!
906 		 */
907 		for (;;) {
908 			if (ISP2(zp->z_blksz)) {
909 				boff = P2ALIGN_TYPED(off, zp->z_blksz,
910 				    uint64_t);
911 			} else {
912 				boff = 0;
913 			}
914 			dlen = zp->z_blksz;
915 			rl = zfs_range_lock(zp, boff, dlen, RL_READER);
916 			if (zp->z_blksz == dlen)
917 				break;
918 			zfs_range_unlock(rl);
919 		}
920 		/* test for truncation needs to be done while range locked */
921 		if (off >= zp->z_phys->zp_size) {
922 			error = ENOENT;
923 			goto out;
924 		}
925 		zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
926 		zgd->zgd_rl = rl;
927 		zgd->zgd_zilog = zfsvfs->z_log;
928 		zgd->zgd_bp = &lr->lr_blkptr;
929 		VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db));
930 		ASSERT(boff == db->db_offset);
931 		lr->lr_blkoff = off - boff;
932 		error = dmu_sync(zio, db, &lr->lr_blkptr,
933 		    lr->lr_common.lrc_txg, zfs_get_done, zgd);
934 		if (error == 0) {
935 			zil_add_vdev(zfsvfs->z_log,
936 			    DVA_GET_VDEV(BP_IDENTITY(&lr->lr_blkptr)));
937 		}
938 		/*
939 		 * If we get EINPROGRESS, then we need to wait for a
940 		 * write IO initiated by dmu_sync() to complete before
941 		 * we can release this dbuf.  We will finish everything
942 		 * up in the zfs_get_done() callback.
943 		 */
944 		if (error == EINPROGRESS)
945 			return (0);
946 		dmu_buf_rele(db, zgd);
947 		kmem_free(zgd, sizeof (zgd_t));
948 	}
949 out:
950 	zfs_range_unlock(rl);
951 	VN_RELE(ZTOV(zp));
952 	return (error);
953 }
954 
955 /*ARGSUSED*/
956 static int
957 zfs_access(vnode_t *vp, int mode, int flags, cred_t *cr)
958 {
959 	znode_t *zp = VTOZ(vp);
960 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
961 	int error;
962 
963 	ZFS_ENTER(zfsvfs);
964 	error = zfs_zaccess_rwx(zp, mode, cr);
965 	ZFS_EXIT(zfsvfs);
966 	return (error);
967 }
968 
969 /*
970  * Lookup an entry in a directory, or an extended attribute directory.
971  * If it exists, return a held vnode reference for it.
972  *
973  *	IN:	dvp	- vnode of directory to search.
974  *		nm	- name of entry to lookup.
975  *		pnp	- full pathname to lookup [UNUSED].
976  *		flags	- LOOKUP_XATTR set if looking for an attribute.
977  *		rdir	- root directory vnode [UNUSED].
978  *		cr	- credentials of caller.
979  *
980  *	OUT:	vpp	- vnode of located entry, NULL if not found.
981  *
982  *	RETURN:	0 if success
983  *		error code if failure
984  *
985  * Timestamps:
986  *	NA
987  */
988 /* ARGSUSED */
989 static int
990 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
991     int flags, vnode_t *rdir, cred_t *cr)
992 {
993 
994 	znode_t *zdp = VTOZ(dvp);
995 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
996 	int	error;
997 
998 	ZFS_ENTER(zfsvfs);
999 
1000 	*vpp = NULL;
1001 
1002 	if (flags & LOOKUP_XATTR) {
1003 		/*
1004 		 * If the xattr property is off, refuse the lookup request.
1005 		 */
1006 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1007 			ZFS_EXIT(zfsvfs);
1008 			return (EINVAL);
1009 		}
1010 
1011 		/*
1012 		 * We don't allow recursive attributes..
1013 		 * Maybe someday we will.
1014 		 */
1015 		if (zdp->z_phys->zp_flags & ZFS_XATTR) {
1016 			ZFS_EXIT(zfsvfs);
1017 			return (EINVAL);
1018 		}
1019 
1020 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr)) {
1021 			ZFS_EXIT(zfsvfs);
1022 			return (error);
1023 		}
1024 
1025 		/*
1026 		 * Do we have permission to get into attribute directory?
1027 		 */
1028 
1029 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, cr)) {
1030 			VN_RELE(*vpp);
1031 		}
1032 
1033 		ZFS_EXIT(zfsvfs);
1034 		return (error);
1035 	}
1036 
1037 	if (dvp->v_type != VDIR) {
1038 		ZFS_EXIT(zfsvfs);
1039 		return (ENOTDIR);
1040 	}
1041 
1042 	/*
1043 	 * Check accessibility of directory.
1044 	 */
1045 
1046 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, cr)) {
1047 		ZFS_EXIT(zfsvfs);
1048 		return (error);
1049 	}
1050 
1051 	if ((error = zfs_dirlook(zdp, nm, vpp)) == 0) {
1052 
1053 		/*
1054 		 * Convert device special files
1055 		 */
1056 		if (IS_DEVVP(*vpp)) {
1057 			vnode_t	*svp;
1058 
1059 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1060 			VN_RELE(*vpp);
1061 			if (svp == NULL)
1062 				error = ENOSYS;
1063 			else
1064 				*vpp = svp;
1065 		}
1066 	}
1067 
1068 	ZFS_EXIT(zfsvfs);
1069 	return (error);
1070 }
1071 
1072 /*
1073  * Attempt to create a new entry in a directory.  If the entry
1074  * already exists, truncate the file if permissible, else return
1075  * an error.  Return the vp of the created or trunc'd file.
1076  *
1077  *	IN:	dvp	- vnode of directory to put new file entry in.
1078  *		name	- name of new file entry.
1079  *		vap	- attributes of new file.
1080  *		excl	- flag indicating exclusive or non-exclusive mode.
1081  *		mode	- mode to open file with.
1082  *		cr	- credentials of caller.
1083  *		flag	- large file flag [UNUSED].
1084  *
1085  *	OUT:	vpp	- vnode of created or trunc'd entry.
1086  *
1087  *	RETURN:	0 if success
1088  *		error code if failure
1089  *
1090  * Timestamps:
1091  *	dvp - ctime|mtime updated if new entry created
1092  *	 vp - ctime|mtime always, atime if new
1093  */
1094 /* ARGSUSED */
1095 static int
1096 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1097     int mode, vnode_t **vpp, cred_t *cr, int flag)
1098 {
1099 	znode_t		*zp, *dzp = VTOZ(dvp);
1100 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1101 	zilog_t		*zilog = zfsvfs->z_log;
1102 	objset_t	*os = zfsvfs->z_os;
1103 	zfs_dirlock_t	*dl;
1104 	dmu_tx_t	*tx;
1105 	int		error;
1106 	uint64_t	zoid;
1107 
1108 	ZFS_ENTER(zfsvfs);
1109 
1110 top:
1111 	*vpp = NULL;
1112 
1113 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1114 		vap->va_mode &= ~VSVTX;
1115 
1116 	if (*name == '\0') {
1117 		/*
1118 		 * Null component name refers to the directory itself.
1119 		 */
1120 		VN_HOLD(dvp);
1121 		zp = dzp;
1122 		dl = NULL;
1123 		error = 0;
1124 	} else {
1125 		/* possible VN_HOLD(zp) */
1126 		if (error = zfs_dirent_lock(&dl, dzp, name, &zp, 0)) {
1127 			if (strcmp(name, "..") == 0)
1128 				error = EISDIR;
1129 			ZFS_EXIT(zfsvfs);
1130 			return (error);
1131 		}
1132 	}
1133 
1134 	zoid = zp ? zp->z_id : -1ULL;
1135 
1136 	if (zp == NULL) {
1137 		/*
1138 		 * Create a new file object and update the directory
1139 		 * to reference it.
1140 		 */
1141 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
1142 			goto out;
1143 		}
1144 
1145 		/*
1146 		 * We only support the creation of regular files in
1147 		 * extended attribute directories.
1148 		 */
1149 		if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1150 		    (vap->va_type != VREG)) {
1151 			error = EINVAL;
1152 			goto out;
1153 		}
1154 
1155 		tx = dmu_tx_create(os);
1156 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1157 		dmu_tx_hold_bonus(tx, dzp->z_id);
1158 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1159 		if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
1160 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1161 			    0, SPA_MAXBLOCKSIZE);
1162 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
1163 		if (error) {
1164 			zfs_dirent_unlock(dl);
1165 			if (error == ERESTART &&
1166 			    zfsvfs->z_assign == TXG_NOWAIT) {
1167 				dmu_tx_wait(tx);
1168 				dmu_tx_abort(tx);
1169 				goto top;
1170 			}
1171 			dmu_tx_abort(tx);
1172 			ZFS_EXIT(zfsvfs);
1173 			return (error);
1174 		}
1175 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
1176 		ASSERT(zp->z_id == zoid);
1177 		(void) zfs_link_create(dl, zp, tx, ZNEW);
1178 		zfs_log_create(zilog, tx, TX_CREATE, dzp, zp, name);
1179 		dmu_tx_commit(tx);
1180 	} else {
1181 		/*
1182 		 * A directory entry already exists for this name.
1183 		 */
1184 		/*
1185 		 * Can't truncate an existing file if in exclusive mode.
1186 		 */
1187 		if (excl == EXCL) {
1188 			error = EEXIST;
1189 			goto out;
1190 		}
1191 		/*
1192 		 * Can't open a directory for writing.
1193 		 */
1194 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1195 			error = EISDIR;
1196 			goto out;
1197 		}
1198 		/*
1199 		 * Verify requested access to file.
1200 		 */
1201 		if (mode && (error = zfs_zaccess_rwx(zp, mode, cr))) {
1202 			goto out;
1203 		}
1204 
1205 		mutex_enter(&dzp->z_lock);
1206 		dzp->z_seq++;
1207 		mutex_exit(&dzp->z_lock);
1208 
1209 		/*
1210 		 * Truncate regular files if requested.
1211 		 */
1212 		if ((ZTOV(zp)->v_type == VREG) &&
1213 		    (zp->z_phys->zp_size != 0) &&
1214 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1215 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
1216 			if (error == ERESTART &&
1217 			    zfsvfs->z_assign == TXG_NOWAIT) {
1218 				/* NB: we already did dmu_tx_wait() */
1219 				zfs_dirent_unlock(dl);
1220 				VN_RELE(ZTOV(zp));
1221 				goto top;
1222 			}
1223 		}
1224 	}
1225 out:
1226 
1227 	if (dl)
1228 		zfs_dirent_unlock(dl);
1229 
1230 	if (error) {
1231 		if (zp)
1232 			VN_RELE(ZTOV(zp));
1233 	} else {
1234 		*vpp = ZTOV(zp);
1235 		/*
1236 		 * If vnode is for a device return a specfs vnode instead.
1237 		 */
1238 		if (IS_DEVVP(*vpp)) {
1239 			struct vnode *svp;
1240 
1241 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1242 			VN_RELE(*vpp);
1243 			if (svp == NULL) {
1244 				error = ENOSYS;
1245 			}
1246 			*vpp = svp;
1247 		}
1248 	}
1249 
1250 	ZFS_EXIT(zfsvfs);
1251 	return (error);
1252 }
1253 
1254 /*
1255  * Remove an entry from a directory.
1256  *
1257  *	IN:	dvp	- vnode of directory to remove entry from.
1258  *		name	- name of entry to remove.
1259  *		cr	- credentials of caller.
1260  *
1261  *	RETURN:	0 if success
1262  *		error code if failure
1263  *
1264  * Timestamps:
1265  *	dvp - ctime|mtime
1266  *	 vp - ctime (if nlink > 0)
1267  */
1268 static int
1269 zfs_remove(vnode_t *dvp, char *name, cred_t *cr)
1270 {
1271 	znode_t		*zp, *dzp = VTOZ(dvp);
1272 	znode_t		*xzp = NULL;
1273 	vnode_t		*vp;
1274 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1275 	zilog_t		*zilog = zfsvfs->z_log;
1276 	uint64_t	acl_obj, xattr_obj;
1277 	zfs_dirlock_t	*dl;
1278 	dmu_tx_t	*tx;
1279 	int		may_delete_now, delete_now = FALSE;
1280 	int		reaped;
1281 	int		error;
1282 
1283 	ZFS_ENTER(zfsvfs);
1284 
1285 top:
1286 	/*
1287 	 * Attempt to lock directory; fail if entry doesn't exist.
1288 	 */
1289 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
1290 		ZFS_EXIT(zfsvfs);
1291 		return (error);
1292 	}
1293 
1294 	vp = ZTOV(zp);
1295 
1296 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1297 		goto out;
1298 	}
1299 
1300 	/*
1301 	 * Need to use rmdir for removing directories.
1302 	 */
1303 	if (vp->v_type == VDIR) {
1304 		error = EPERM;
1305 		goto out;
1306 	}
1307 
1308 	vnevent_remove(vp);
1309 
1310 	dnlc_remove(dvp, name);
1311 
1312 	mutex_enter(&vp->v_lock);
1313 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1314 	mutex_exit(&vp->v_lock);
1315 
1316 	/*
1317 	 * We may delete the znode now, or we may put it on the delete queue;
1318 	 * it depends on whether we're the last link, and on whether there are
1319 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1320 	 * allow for either case.
1321 	 */
1322 	tx = dmu_tx_create(zfsvfs->z_os);
1323 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1324 	dmu_tx_hold_bonus(tx, zp->z_id);
1325 	if (may_delete_now)
1326 		dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
1327 
1328 	/* are there any extended attributes? */
1329 	if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1330 		/*
1331 		 * XXX - There is a possibility that the delete
1332 		 * of the parent file could succeed, but then we get
1333 		 * an ENOSPC when we try to delete the xattrs...
1334 		 * so we would need to re-try the deletes periodically
1335 		 */
1336 		/* XXX - do we need this if we are deleting? */
1337 		dmu_tx_hold_bonus(tx, xattr_obj);
1338 	}
1339 
1340 	/* are there any additional acls */
1341 	if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1342 	    may_delete_now)
1343 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1344 
1345 	/* charge as an update -- would be nice not to charge at all */
1346 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
1347 
1348 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1349 	if (error) {
1350 		zfs_dirent_unlock(dl);
1351 		VN_RELE(vp);
1352 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1353 			dmu_tx_wait(tx);
1354 			dmu_tx_abort(tx);
1355 			goto top;
1356 		}
1357 		dmu_tx_abort(tx);
1358 		ZFS_EXIT(zfsvfs);
1359 		return (error);
1360 	}
1361 
1362 	/*
1363 	 * Remove the directory entry.
1364 	 */
1365 	error = zfs_link_destroy(dl, zp, tx, 0, &reaped);
1366 
1367 	if (error) {
1368 		dmu_tx_commit(tx);
1369 		goto out;
1370 	}
1371 
1372 	if (reaped) {
1373 		mutex_enter(&vp->v_lock);
1374 		delete_now = may_delete_now &&
1375 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1376 		    zp->z_phys->zp_xattr == xattr_obj &&
1377 		    zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1378 		mutex_exit(&vp->v_lock);
1379 	}
1380 
1381 	if (delete_now) {
1382 		if (zp->z_phys->zp_xattr) {
1383 			error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1384 			ASSERT3U(error, ==, 0);
1385 			ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1386 			dmu_buf_will_dirty(xzp->z_dbuf, tx);
1387 			mutex_enter(&xzp->z_lock);
1388 			xzp->z_reap = 1;
1389 			xzp->z_phys->zp_links = 0;
1390 			mutex_exit(&xzp->z_lock);
1391 			zfs_dq_add(xzp, tx);
1392 			zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1393 		}
1394 		mutex_enter(&zp->z_lock);
1395 		mutex_enter(&vp->v_lock);
1396 		vp->v_count--;
1397 		ASSERT3U(vp->v_count, ==, 0);
1398 		mutex_exit(&vp->v_lock);
1399 		mutex_exit(&zp->z_lock);
1400 		zfs_znode_delete(zp, tx);
1401 		VFS_RELE(zfsvfs->z_vfs);
1402 	} else if (reaped) {
1403 		zfs_dq_add(zp, tx);
1404 	}
1405 
1406 	zfs_log_remove(zilog, tx, TX_REMOVE, dzp, name);
1407 
1408 	dmu_tx_commit(tx);
1409 out:
1410 	zfs_dirent_unlock(dl);
1411 
1412 	if (!delete_now) {
1413 		VN_RELE(vp);
1414 	} else if (xzp) {
1415 		/* this rele delayed to prevent nesting transactions */
1416 		VN_RELE(ZTOV(xzp));
1417 	}
1418 
1419 	ZFS_EXIT(zfsvfs);
1420 	return (error);
1421 }
1422 
1423 /*
1424  * Create a new directory and insert it into dvp using the name
1425  * provided.  Return a pointer to the inserted directory.
1426  *
1427  *	IN:	dvp	- vnode of directory to add subdir to.
1428  *		dirname	- name of new directory.
1429  *		vap	- attributes of new directory.
1430  *		cr	- credentials of caller.
1431  *
1432  *	OUT:	vpp	- vnode of created directory.
1433  *
1434  *	RETURN:	0 if success
1435  *		error code if failure
1436  *
1437  * Timestamps:
1438  *	dvp - ctime|mtime updated
1439  *	 vp - ctime|mtime|atime updated
1440  */
1441 static int
1442 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr)
1443 {
1444 	znode_t		*zp, *dzp = VTOZ(dvp);
1445 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1446 	zilog_t		*zilog = zfsvfs->z_log;
1447 	zfs_dirlock_t	*dl;
1448 	uint64_t	zoid = 0;
1449 	dmu_tx_t	*tx;
1450 	int		error;
1451 
1452 	ASSERT(vap->va_type == VDIR);
1453 
1454 	ZFS_ENTER(zfsvfs);
1455 
1456 	if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1457 		ZFS_EXIT(zfsvfs);
1458 		return (EINVAL);
1459 	}
1460 top:
1461 	*vpp = NULL;
1462 
1463 	/*
1464 	 * First make sure the new directory doesn't exist.
1465 	 */
1466 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, ZNEW)) {
1467 		ZFS_EXIT(zfsvfs);
1468 		return (error);
1469 	}
1470 
1471 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, cr)) {
1472 		zfs_dirent_unlock(dl);
1473 		ZFS_EXIT(zfsvfs);
1474 		return (error);
1475 	}
1476 
1477 	/*
1478 	 * Add a new entry to the directory.
1479 	 */
1480 	tx = dmu_tx_create(zfsvfs->z_os);
1481 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1482 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1483 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
1484 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1485 		    0, SPA_MAXBLOCKSIZE);
1486 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1487 	if (error) {
1488 		zfs_dirent_unlock(dl);
1489 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1490 			dmu_tx_wait(tx);
1491 			dmu_tx_abort(tx);
1492 			goto top;
1493 		}
1494 		dmu_tx_abort(tx);
1495 		ZFS_EXIT(zfsvfs);
1496 		return (error);
1497 	}
1498 
1499 	/*
1500 	 * Create new node.
1501 	 */
1502 	zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
1503 
1504 	/*
1505 	 * Now put new name in parent dir.
1506 	 */
1507 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1508 
1509 	*vpp = ZTOV(zp);
1510 
1511 	zfs_log_create(zilog, tx, TX_MKDIR, dzp, zp, dirname);
1512 	dmu_tx_commit(tx);
1513 
1514 	zfs_dirent_unlock(dl);
1515 
1516 	ZFS_EXIT(zfsvfs);
1517 	return (0);
1518 }
1519 
1520 /*
1521  * Remove a directory subdir entry.  If the current working
1522  * directory is the same as the subdir to be removed, the
1523  * remove will fail.
1524  *
1525  *	IN:	dvp	- vnode of directory to remove from.
1526  *		name	- name of directory to be removed.
1527  *		cwd	- vnode of current working directory.
1528  *		cr	- credentials of caller.
1529  *
1530  *	RETURN:	0 if success
1531  *		error code if failure
1532  *
1533  * Timestamps:
1534  *	dvp - ctime|mtime updated
1535  */
1536 static int
1537 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr)
1538 {
1539 	znode_t		*dzp = VTOZ(dvp);
1540 	znode_t		*zp;
1541 	vnode_t		*vp;
1542 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1543 	zilog_t		*zilog = zfsvfs->z_log;
1544 	zfs_dirlock_t	*dl;
1545 	dmu_tx_t	*tx;
1546 	int		error;
1547 
1548 	ZFS_ENTER(zfsvfs);
1549 
1550 top:
1551 	zp = NULL;
1552 
1553 	/*
1554 	 * Attempt to lock directory; fail if entry doesn't exist.
1555 	 */
1556 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
1557 		ZFS_EXIT(zfsvfs);
1558 		return (error);
1559 	}
1560 
1561 	vp = ZTOV(zp);
1562 
1563 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1564 		goto out;
1565 	}
1566 
1567 	if (vp->v_type != VDIR) {
1568 		error = ENOTDIR;
1569 		goto out;
1570 	}
1571 
1572 	if (vp == cwd) {
1573 		error = EINVAL;
1574 		goto out;
1575 	}
1576 
1577 	vnevent_rmdir(vp);
1578 
1579 	/*
1580 	 * Grab a lock on the parent pointer make sure we play well
1581 	 * with the treewalk and directory rename code.
1582 	 */
1583 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1584 
1585 	tx = dmu_tx_create(zfsvfs->z_os);
1586 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1587 	dmu_tx_hold_bonus(tx, zp->z_id);
1588 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
1589 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1590 	if (error) {
1591 		rw_exit(&zp->z_parent_lock);
1592 		zfs_dirent_unlock(dl);
1593 		VN_RELE(vp);
1594 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1595 			dmu_tx_wait(tx);
1596 			dmu_tx_abort(tx);
1597 			goto top;
1598 		}
1599 		dmu_tx_abort(tx);
1600 		ZFS_EXIT(zfsvfs);
1601 		return (error);
1602 	}
1603 
1604 	error = zfs_link_destroy(dl, zp, tx, 0, NULL);
1605 
1606 	if (error == 0)
1607 		zfs_log_remove(zilog, tx, TX_RMDIR, dzp, name);
1608 
1609 	dmu_tx_commit(tx);
1610 
1611 	rw_exit(&zp->z_parent_lock);
1612 out:
1613 	zfs_dirent_unlock(dl);
1614 
1615 	VN_RELE(vp);
1616 
1617 	ZFS_EXIT(zfsvfs);
1618 	return (error);
1619 }
1620 
1621 /*
1622  * Read as many directory entries as will fit into the provided
1623  * buffer from the given directory cursor position (specified in
1624  * the uio structure.
1625  *
1626  *	IN:	vp	- vnode of directory to read.
1627  *		uio	- structure supplying read location, range info,
1628  *			  and return buffer.
1629  *		cr	- credentials of caller.
1630  *
1631  *	OUT:	uio	- updated offset and range, buffer filled.
1632  *		eofp	- set to true if end-of-file detected.
1633  *
1634  *	RETURN:	0 if success
1635  *		error code if failure
1636  *
1637  * Timestamps:
1638  *	vp - atime updated
1639  *
1640  * Note that the low 4 bits of the cookie returned by zap is always zero.
1641  * This allows us to use the low range for "special" directory entries:
1642  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
1643  * we use the offset 2 for the '.zfs' directory.
1644  */
1645 /* ARGSUSED */
1646 static int
1647 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp)
1648 {
1649 	znode_t		*zp = VTOZ(vp);
1650 	iovec_t		*iovp;
1651 	dirent64_t	*odp;
1652 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1653 	objset_t	*os;
1654 	caddr_t		outbuf;
1655 	size_t		bufsize;
1656 	zap_cursor_t	zc;
1657 	zap_attribute_t	zap;
1658 	uint_t		bytes_wanted;
1659 	ushort_t	this_reclen;
1660 	uint64_t	offset; /* must be unsigned; checks for < 1 */
1661 	off64_t		*next;
1662 	int		local_eof;
1663 	int		outcount;
1664 	int		error;
1665 	uint8_t		prefetch;
1666 
1667 	ZFS_ENTER(zfsvfs);
1668 
1669 	/*
1670 	 * If we are not given an eof variable,
1671 	 * use a local one.
1672 	 */
1673 	if (eofp == NULL)
1674 		eofp = &local_eof;
1675 
1676 	/*
1677 	 * Check for valid iov_len.
1678 	 */
1679 	if (uio->uio_iov->iov_len <= 0) {
1680 		ZFS_EXIT(zfsvfs);
1681 		return (EINVAL);
1682 	}
1683 
1684 	/*
1685 	 * Quit if directory has been removed (posix)
1686 	 */
1687 	if ((*eofp = zp->z_reap) != 0) {
1688 		ZFS_EXIT(zfsvfs);
1689 		return (0);
1690 	}
1691 
1692 	error = 0;
1693 	os = zfsvfs->z_os;
1694 	offset = uio->uio_loffset;
1695 	prefetch = zp->z_zn_prefetch;
1696 
1697 	/*
1698 	 * Initialize the iterator cursor.
1699 	 */
1700 	if (offset <= 3) {
1701 		/*
1702 		 * Start iteration from the beginning of the directory.
1703 		 */
1704 		zap_cursor_init(&zc, os, zp->z_id);
1705 	} else {
1706 		/*
1707 		 * The offset is a serialized cursor.
1708 		 */
1709 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1710 	}
1711 
1712 	/*
1713 	 * Get space to change directory entries into fs independent format.
1714 	 */
1715 	iovp = uio->uio_iov;
1716 	bytes_wanted = iovp->iov_len;
1717 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
1718 		bufsize = bytes_wanted;
1719 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
1720 		odp = (struct dirent64 *)outbuf;
1721 	} else {
1722 		bufsize = bytes_wanted;
1723 		odp = (struct dirent64 *)iovp->iov_base;
1724 	}
1725 
1726 	/*
1727 	 * Transform to file-system independent format
1728 	 */
1729 	outcount = 0;
1730 	while (outcount < bytes_wanted) {
1731 		/*
1732 		 * Special case `.', `..', and `.zfs'.
1733 		 */
1734 		if (offset == 0) {
1735 			(void) strcpy(zap.za_name, ".");
1736 			zap.za_first_integer = zp->z_id;
1737 			this_reclen = DIRENT64_RECLEN(1);
1738 		} else if (offset == 1) {
1739 			(void) strcpy(zap.za_name, "..");
1740 			zap.za_first_integer = zp->z_phys->zp_parent;
1741 			this_reclen = DIRENT64_RECLEN(2);
1742 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
1743 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
1744 			zap.za_first_integer = ZFSCTL_INO_ROOT;
1745 			this_reclen =
1746 			    DIRENT64_RECLEN(sizeof (ZFS_CTLDIR_NAME) - 1);
1747 		} else {
1748 			/*
1749 			 * Grab next entry.
1750 			 */
1751 			if (error = zap_cursor_retrieve(&zc, &zap)) {
1752 				if ((*eofp = (error == ENOENT)) != 0)
1753 					break;
1754 				else
1755 					goto update;
1756 			}
1757 
1758 			if (zap.za_integer_length != 8 ||
1759 			    zap.za_num_integers != 1) {
1760 				cmn_err(CE_WARN, "zap_readdir: bad directory "
1761 				    "entry, obj = %lld, offset = %lld\n",
1762 				    (u_longlong_t)zp->z_id,
1763 				    (u_longlong_t)offset);
1764 				error = ENXIO;
1765 				goto update;
1766 			}
1767 			this_reclen = DIRENT64_RECLEN(strlen(zap.za_name));
1768 		}
1769 
1770 		/*
1771 		 * Will this entry fit in the buffer?
1772 		 */
1773 		if (outcount + this_reclen > bufsize) {
1774 			/*
1775 			 * Did we manage to fit anything in the buffer?
1776 			 */
1777 			if (!outcount) {
1778 				error = EINVAL;
1779 				goto update;
1780 			}
1781 			break;
1782 		}
1783 		/*
1784 		 * Add this entry:
1785 		 */
1786 		odp->d_ino = (ino64_t)zap.za_first_integer;
1787 		odp->d_reclen = (ushort_t)this_reclen;
1788 		/* NOTE: d_off is the offset for the *next* entry */
1789 		next = &(odp->d_off);
1790 		(void) strncpy(odp->d_name, zap.za_name,
1791 		    DIRENT64_NAMELEN(this_reclen));
1792 		outcount += this_reclen;
1793 		odp = (dirent64_t *)((intptr_t)odp + this_reclen);
1794 
1795 		ASSERT(outcount <= bufsize);
1796 
1797 		/* Prefetch znode */
1798 		if (prefetch)
1799 			dmu_prefetch(os, zap.za_first_integer, 0, 0);
1800 
1801 		/*
1802 		 * Move to the next entry, fill in the previous offset.
1803 		 */
1804 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
1805 			zap_cursor_advance(&zc);
1806 			offset = zap_cursor_serialize(&zc);
1807 		} else {
1808 			offset += 1;
1809 		}
1810 		*next = offset;
1811 	}
1812 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
1813 
1814 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
1815 		iovp->iov_base += outcount;
1816 		iovp->iov_len -= outcount;
1817 		uio->uio_resid -= outcount;
1818 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
1819 		/*
1820 		 * Reset the pointer.
1821 		 */
1822 		offset = uio->uio_loffset;
1823 	}
1824 
1825 update:
1826 	zap_cursor_fini(&zc);
1827 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1828 		kmem_free(outbuf, bufsize);
1829 
1830 	if (error == ENOENT)
1831 		error = 0;
1832 
1833 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
1834 
1835 	uio->uio_loffset = offset;
1836 	ZFS_EXIT(zfsvfs);
1837 	return (error);
1838 }
1839 
1840 static int
1841 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr)
1842 {
1843 	znode_t	*zp = VTOZ(vp);
1844 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1845 
1846 	/*
1847 	 * Regardless of whether this is required for standards conformance,
1848 	 * this is the logical behavior when fsync() is called on a file with
1849 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
1850 	 * going to be pushed out as part of the zil_commit().
1851 	 */
1852 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
1853 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
1854 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr);
1855 
1856 	ZFS_ENTER(zfsvfs);
1857 	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
1858 	ZFS_EXIT(zfsvfs);
1859 	return (0);
1860 }
1861 
1862 /*
1863  * Get the requested file attributes and place them in the provided
1864  * vattr structure.
1865  *
1866  *	IN:	vp	- vnode of file.
1867  *		vap	- va_mask identifies requested attributes.
1868  *		flags	- [UNUSED]
1869  *		cr	- credentials of caller.
1870  *
1871  *	OUT:	vap	- attribute values.
1872  *
1873  *	RETURN:	0 (always succeeds)
1874  */
1875 /* ARGSUSED */
1876 static int
1877 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
1878 {
1879 	znode_t *zp = VTOZ(vp);
1880 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1881 	znode_phys_t *pzp = zp->z_phys;
1882 	int	error;
1883 
1884 	ZFS_ENTER(zfsvfs);
1885 
1886 	/*
1887 	 * Return all attributes.  It's cheaper to provide the answer
1888 	 * than to determine whether we were asked the question.
1889 	 */
1890 	mutex_enter(&zp->z_lock);
1891 
1892 	vap->va_type = vp->v_type;
1893 	vap->va_mode = pzp->zp_mode & MODEMASK;
1894 	vap->va_uid = zp->z_phys->zp_uid;
1895 	vap->va_gid = zp->z_phys->zp_gid;
1896 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
1897 	vap->va_nodeid = zp->z_id;
1898 	vap->va_nlink = MIN(pzp->zp_links, UINT32_MAX);	/* nlink_t limit! */
1899 	vap->va_size = pzp->zp_size;
1900 	vap->va_rdev = vp->v_rdev;
1901 	vap->va_seq = zp->z_seq;
1902 
1903 	ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
1904 	ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
1905 	ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
1906 
1907 	/*
1908 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
1909 	 * Also, if we are the owner don't bother, since owner should
1910 	 * always be allowed to read basic attributes of file.
1911 	 */
1912 	if (!(zp->z_phys->zp_flags & ZFS_ACL_TRIVIAL) &&
1913 	    (zp->z_phys->zp_uid != crgetuid(cr))) {
1914 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, cr)) {
1915 			mutex_exit(&zp->z_lock);
1916 			ZFS_EXIT(zfsvfs);
1917 			return (error);
1918 		}
1919 	}
1920 
1921 	mutex_exit(&zp->z_lock);
1922 
1923 	dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks);
1924 
1925 	if (zp->z_blksz == 0) {
1926 		/*
1927 		 * Block size hasn't been set; suggest maximal I/O transfers.
1928 		 */
1929 		vap->va_blksize = zfsvfs->z_max_blksz;
1930 	}
1931 
1932 	ZFS_EXIT(zfsvfs);
1933 	return (0);
1934 }
1935 
1936 /*
1937  * Set the file attributes to the values contained in the
1938  * vattr structure.
1939  *
1940  *	IN:	vp	- vnode of file to be modified.
1941  *		vap	- new attribute values.
1942  *		flags	- ATTR_UTIME set if non-default time values provided.
1943  *		cr	- credentials of caller.
1944  *
1945  *	RETURN:	0 if success
1946  *		error code if failure
1947  *
1948  * Timestamps:
1949  *	vp - ctime updated, mtime updated if size changed.
1950  */
1951 /* ARGSUSED */
1952 static int
1953 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1954 	caller_context_t *ct)
1955 {
1956 	struct znode	*zp = VTOZ(vp);
1957 	znode_phys_t	*pzp = zp->z_phys;
1958 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1959 	zilog_t		*zilog = zfsvfs->z_log;
1960 	dmu_tx_t	*tx;
1961 	vattr_t		oldva;
1962 	uint_t		mask = vap->va_mask;
1963 	uint_t		saved_mask;
1964 	int		trim_mask = 0;
1965 	uint64_t	new_mode;
1966 	znode_t		*attrzp;
1967 	int		need_policy = FALSE;
1968 	int		err;
1969 
1970 	if (mask == 0)
1971 		return (0);
1972 
1973 	if (mask & AT_NOSET)
1974 		return (EINVAL);
1975 
1976 	if (mask & AT_SIZE && vp->v_type == VDIR)
1977 		return (EISDIR);
1978 
1979 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO)
1980 		return (EINVAL);
1981 
1982 	ZFS_ENTER(zfsvfs);
1983 
1984 top:
1985 	attrzp = NULL;
1986 
1987 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
1988 		ZFS_EXIT(zfsvfs);
1989 		return (EROFS);
1990 	}
1991 
1992 	/*
1993 	 * First validate permissions
1994 	 */
1995 
1996 	if (mask & AT_SIZE) {
1997 		err = zfs_zaccess(zp, ACE_WRITE_DATA, cr);
1998 		if (err) {
1999 			ZFS_EXIT(zfsvfs);
2000 			return (err);
2001 		}
2002 		/*
2003 		 * XXX - Note, we are not providing any open
2004 		 * mode flags here (like FNDELAY), so we may
2005 		 * block if there are locks present... this
2006 		 * should be addressed in openat().
2007 		 */
2008 		do {
2009 			err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2010 			/* NB: we already did dmu_tx_wait() if necessary */
2011 		} while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
2012 		if (err) {
2013 			ZFS_EXIT(zfsvfs);
2014 			return (err);
2015 		}
2016 	}
2017 
2018 	if (mask & (AT_ATIME|AT_MTIME))
2019 		need_policy = zfs_zaccess_v4_perm(zp, ACE_WRITE_ATTRIBUTES, cr);
2020 
2021 	if (mask & (AT_UID|AT_GID)) {
2022 		int	idmask = (mask & (AT_UID|AT_GID));
2023 		int	take_owner;
2024 		int	take_group;
2025 
2026 		/*
2027 		 * NOTE: even if a new mode is being set,
2028 		 * we may clear S_ISUID/S_ISGID bits.
2029 		 */
2030 
2031 		if (!(mask & AT_MODE))
2032 			vap->va_mode = pzp->zp_mode;
2033 
2034 		/*
2035 		 * Take ownership or chgrp to group we are a member of
2036 		 */
2037 
2038 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2039 		take_group = (mask & AT_GID) && groupmember(vap->va_gid, cr);
2040 
2041 		/*
2042 		 * If both AT_UID and AT_GID are set then take_owner and
2043 		 * take_group must both be set in order to allow taking
2044 		 * ownership.
2045 		 *
2046 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2047 		 *
2048 		 */
2049 
2050 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2051 		    ((idmask == AT_UID) && take_owner) ||
2052 		    ((idmask == AT_GID) && take_group)) {
2053 			if (zfs_zaccess_v4_perm(zp, ACE_WRITE_OWNER, cr) == 0) {
2054 				/*
2055 				 * Remove setuid/setgid for non-privileged users
2056 				 */
2057 				secpolicy_setid_clear(vap, cr);
2058 				trim_mask = (mask & (AT_UID|AT_GID));
2059 			} else {
2060 				need_policy =  TRUE;
2061 			}
2062 		} else {
2063 			need_policy =  TRUE;
2064 		}
2065 	}
2066 
2067 	mutex_enter(&zp->z_lock);
2068 	oldva.va_mode = pzp->zp_mode;
2069 	oldva.va_uid = zp->z_phys->zp_uid;
2070 	oldva.va_gid = zp->z_phys->zp_gid;
2071 	mutex_exit(&zp->z_lock);
2072 
2073 	if (mask & AT_MODE) {
2074 		if (zfs_zaccess_v4_perm(zp, ACE_WRITE_ACL, cr) == 0) {
2075 			err = secpolicy_setid_setsticky_clear(vp, vap,
2076 			    &oldva, cr);
2077 			if (err) {
2078 				ZFS_EXIT(zfsvfs);
2079 				return (err);
2080 			}
2081 			trim_mask |= AT_MODE;
2082 		} else {
2083 			need_policy = TRUE;
2084 		}
2085 	}
2086 
2087 	if (need_policy) {
2088 		/*
2089 		 * If trim_mask is set then take ownership
2090 		 * has been granted or write_acl is present and user
2091 		 * has the ability to modify mode.  In that case remove
2092 		 * UID|GID and or MODE from mask so that
2093 		 * secpolicy_vnode_setattr() doesn't revoke it.
2094 		 */
2095 
2096 		if (trim_mask) {
2097 			saved_mask = vap->va_mask;
2098 			vap->va_mask &= ~trim_mask;
2099 
2100 		}
2101 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2102 		    (int (*)(void *, int, cred_t *))zfs_zaccess_rwx, zp);
2103 		if (err) {
2104 			ZFS_EXIT(zfsvfs);
2105 			return (err);
2106 		}
2107 
2108 		if (trim_mask)
2109 			vap->va_mask |= saved_mask;
2110 	}
2111 
2112 	/*
2113 	 * secpolicy_vnode_setattr, or take ownership may have
2114 	 * changed va_mask
2115 	 */
2116 	mask = vap->va_mask;
2117 
2118 	tx = dmu_tx_create(zfsvfs->z_os);
2119 	dmu_tx_hold_bonus(tx, zp->z_id);
2120 
2121 	if (mask & AT_MODE) {
2122 		uint64_t pmode = pzp->zp_mode;
2123 
2124 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2125 
2126 		if (zp->z_phys->zp_acl.z_acl_extern_obj)
2127 			dmu_tx_hold_write(tx,
2128 			    pzp->zp_acl.z_acl_extern_obj, 0, SPA_MAXBLOCKSIZE);
2129 		else
2130 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2131 			    0, ZFS_ACL_SIZE(MAX_ACL_SIZE));
2132 	}
2133 
2134 	if ((mask & (AT_UID | AT_GID)) && zp->z_phys->zp_xattr != 0) {
2135 		err = zfs_zget(zp->z_zfsvfs, zp->z_phys->zp_xattr, &attrzp);
2136 		if (err) {
2137 			dmu_tx_abort(tx);
2138 			ZFS_EXIT(zfsvfs);
2139 			return (err);
2140 		}
2141 		dmu_tx_hold_bonus(tx, attrzp->z_id);
2142 	}
2143 
2144 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2145 	if (err) {
2146 		if (attrzp)
2147 			VN_RELE(ZTOV(attrzp));
2148 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2149 			dmu_tx_wait(tx);
2150 			dmu_tx_abort(tx);
2151 			goto top;
2152 		}
2153 		dmu_tx_abort(tx);
2154 		ZFS_EXIT(zfsvfs);
2155 		return (err);
2156 	}
2157 
2158 	dmu_buf_will_dirty(zp->z_dbuf, tx);
2159 
2160 	/*
2161 	 * Set each attribute requested.
2162 	 * We group settings according to the locks they need to acquire.
2163 	 *
2164 	 * Note: you cannot set ctime directly, although it will be
2165 	 * updated as a side-effect of calling this function.
2166 	 */
2167 
2168 	mutex_enter(&zp->z_lock);
2169 
2170 	if (mask & AT_MODE) {
2171 		err = zfs_acl_chmod_setattr(zp, new_mode, tx);
2172 		ASSERT3U(err, ==, 0);
2173 	}
2174 
2175 	if (attrzp)
2176 		mutex_enter(&attrzp->z_lock);
2177 
2178 	if (mask & AT_UID) {
2179 		zp->z_phys->zp_uid = (uint64_t)vap->va_uid;
2180 		if (attrzp) {
2181 			attrzp->z_phys->zp_uid = (uint64_t)vap->va_uid;
2182 		}
2183 	}
2184 
2185 	if (mask & AT_GID) {
2186 		zp->z_phys->zp_gid = (uint64_t)vap->va_gid;
2187 		if (attrzp)
2188 			attrzp->z_phys->zp_gid = (uint64_t)vap->va_gid;
2189 	}
2190 
2191 	if (attrzp)
2192 		mutex_exit(&attrzp->z_lock);
2193 
2194 	if (mask & AT_ATIME)
2195 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
2196 
2197 	if (mask & AT_MTIME)
2198 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
2199 
2200 	if (mask & AT_SIZE)
2201 		zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
2202 	else if (mask != 0)
2203 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
2204 
2205 	if (mask != 0)
2206 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask);
2207 
2208 	mutex_exit(&zp->z_lock);
2209 
2210 	if (attrzp)
2211 		VN_RELE(ZTOV(attrzp));
2212 
2213 	dmu_tx_commit(tx);
2214 
2215 	ZFS_EXIT(zfsvfs);
2216 	return (err);
2217 }
2218 
2219 /*
2220  * Search back through the directory tree, using the ".." entries.
2221  * Lock each directory in the chain to prevent concurrent renames.
2222  * Fail any attempt to move a directory into one of its own descendants.
2223  * XXX - z_parent_lock can overlap with map or grow locks
2224  */
2225 typedef struct zfs_zlock {
2226 	krwlock_t	*zl_rwlock;	/* lock we acquired */
2227 	znode_t		*zl_znode;	/* znode we held */
2228 	struct zfs_zlock *zl_next;	/* next in list */
2229 } zfs_zlock_t;
2230 
2231 static int
2232 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
2233 {
2234 	zfs_zlock_t	*zl;
2235 	znode_t 	*zp = tdzp;
2236 	uint64_t	rootid = zp->z_zfsvfs->z_root;
2237 	uint64_t	*oidp = &zp->z_id;
2238 	krwlock_t	*rwlp = &szp->z_parent_lock;
2239 	krw_t		rw = RW_WRITER;
2240 
2241 	/*
2242 	 * First pass write-locks szp and compares to zp->z_id.
2243 	 * Later passes read-lock zp and compare to zp->z_parent.
2244 	 */
2245 	do {
2246 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
2247 		zl->zl_rwlock = rwlp;
2248 		zl->zl_znode = NULL;
2249 		zl->zl_next = *zlpp;
2250 		*zlpp = zl;
2251 
2252 		rw_enter(rwlp, rw);
2253 
2254 		if (*oidp == szp->z_id)		/* We're a descendant of szp */
2255 			return (EINVAL);
2256 
2257 		if (*oidp == rootid)		/* We've hit the top */
2258 			return (0);
2259 
2260 		if (rw == RW_READER) {		/* i.e. not the first pass */
2261 			int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
2262 			if (error)
2263 				return (error);
2264 			zl->zl_znode = zp;
2265 		}
2266 		oidp = &zp->z_phys->zp_parent;
2267 		rwlp = &zp->z_parent_lock;
2268 		rw = RW_READER;
2269 
2270 	} while (zp->z_id != sdzp->z_id);
2271 
2272 	return (0);
2273 }
2274 
2275 /*
2276  * Drop locks and release vnodes that were held by zfs_rename_lock().
2277  */
2278 static void
2279 zfs_rename_unlock(zfs_zlock_t **zlpp)
2280 {
2281 	zfs_zlock_t *zl;
2282 
2283 	while ((zl = *zlpp) != NULL) {
2284 		if (zl->zl_znode != NULL)
2285 			VN_RELE(ZTOV(zl->zl_znode));
2286 		rw_exit(zl->zl_rwlock);
2287 		*zlpp = zl->zl_next;
2288 		kmem_free(zl, sizeof (*zl));
2289 	}
2290 }
2291 
2292 /*
2293  * Move an entry from the provided source directory to the target
2294  * directory.  Change the entry name as indicated.
2295  *
2296  *	IN:	sdvp	- Source directory containing the "old entry".
2297  *		snm	- Old entry name.
2298  *		tdvp	- Target directory to contain the "new entry".
2299  *		tnm	- New entry name.
2300  *		cr	- credentials of caller.
2301  *
2302  *	RETURN:	0 if success
2303  *		error code if failure
2304  *
2305  * Timestamps:
2306  *	sdvp,tdvp - ctime|mtime updated
2307  */
2308 static int
2309 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr)
2310 {
2311 	znode_t		*tdzp, *szp, *tzp;
2312 	znode_t		*sdzp = VTOZ(sdvp);
2313 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
2314 	zilog_t		*zilog = zfsvfs->z_log;
2315 	vnode_t		*realvp;
2316 	zfs_dirlock_t	*sdl, *tdl;
2317 	dmu_tx_t	*tx;
2318 	zfs_zlock_t	*zl;
2319 	int		cmp, serr, terr, error;
2320 
2321 	ZFS_ENTER(zfsvfs);
2322 
2323 	/*
2324 	 * Make sure we have the real vp for the target directory.
2325 	 */
2326 	if (VOP_REALVP(tdvp, &realvp) == 0)
2327 		tdvp = realvp;
2328 
2329 	if (tdvp->v_vfsp != sdvp->v_vfsp) {
2330 		ZFS_EXIT(zfsvfs);
2331 		return (EXDEV);
2332 	}
2333 
2334 	tdzp = VTOZ(tdvp);
2335 top:
2336 	szp = NULL;
2337 	tzp = NULL;
2338 	zl = NULL;
2339 
2340 	/*
2341 	 * This is to prevent the creation of links into attribute space
2342 	 * by renaming a linked file into/outof an attribute directory.
2343 	 * See the comment in zfs_link() for why this is considered bad.
2344 	 */
2345 	if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
2346 	    (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
2347 		ZFS_EXIT(zfsvfs);
2348 		return (EINVAL);
2349 	}
2350 
2351 	/*
2352 	 * Lock source and target directory entries.  To prevent deadlock,
2353 	 * a lock ordering must be defined.  We lock the directory with
2354 	 * the smallest object id first, or if it's a tie, the one with
2355 	 * the lexically first name.
2356 	 */
2357 	if (sdzp->z_id < tdzp->z_id) {
2358 		cmp = -1;
2359 	} else if (sdzp->z_id > tdzp->z_id) {
2360 		cmp = 1;
2361 	} else {
2362 		cmp = strcmp(snm, tnm);
2363 		if (cmp == 0) {
2364 			/*
2365 			 * POSIX: "If the old argument and the new argument
2366 			 * both refer to links to the same existing file,
2367 			 * the rename() function shall return successfully
2368 			 * and perform no other action."
2369 			 */
2370 			ZFS_EXIT(zfsvfs);
2371 			return (0);
2372 		}
2373 	}
2374 	if (cmp < 0) {
2375 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
2376 		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
2377 	} else {
2378 		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
2379 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
2380 	}
2381 
2382 	if (serr) {
2383 		/*
2384 		 * Source entry invalid or not there.
2385 		 */
2386 		if (!terr) {
2387 			zfs_dirent_unlock(tdl);
2388 			if (tzp)
2389 				VN_RELE(ZTOV(tzp));
2390 		}
2391 		if (strcmp(snm, "..") == 0)
2392 			serr = EINVAL;
2393 		ZFS_EXIT(zfsvfs);
2394 		return (serr);
2395 	}
2396 	if (terr) {
2397 		zfs_dirent_unlock(sdl);
2398 		VN_RELE(ZTOV(szp));
2399 		if (strcmp(tnm, "..") == 0)
2400 			terr = EINVAL;
2401 		ZFS_EXIT(zfsvfs);
2402 		return (terr);
2403 	}
2404 
2405 	/*
2406 	 * Must have write access at the source to remove the old entry
2407 	 * and write access at the target to create the new entry.
2408 	 * Note that if target and source are the same, this can be
2409 	 * done in a single check.
2410 	 */
2411 
2412 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
2413 		goto out;
2414 
2415 	if (ZTOV(szp)->v_type == VDIR) {
2416 		/*
2417 		 * Check to make sure rename is valid.
2418 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
2419 		 */
2420 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
2421 			goto out;
2422 	}
2423 
2424 	/*
2425 	 * Does target exist?
2426 	 */
2427 	if (tzp) {
2428 		/*
2429 		 * Source and target must be the same type.
2430 		 */
2431 		if (ZTOV(szp)->v_type == VDIR) {
2432 			if (ZTOV(tzp)->v_type != VDIR) {
2433 				error = ENOTDIR;
2434 				goto out;
2435 			}
2436 		} else {
2437 			if (ZTOV(tzp)->v_type == VDIR) {
2438 				error = EISDIR;
2439 				goto out;
2440 			}
2441 		}
2442 		/*
2443 		 * POSIX dictates that when the source and target
2444 		 * entries refer to the same file object, rename
2445 		 * must do nothing and exit without error.
2446 		 */
2447 		if (szp->z_id == tzp->z_id) {
2448 			error = 0;
2449 			goto out;
2450 		}
2451 	}
2452 
2453 	vnevent_rename_src(ZTOV(szp));
2454 	if (tzp)
2455 		vnevent_rename_dest(ZTOV(tzp));
2456 
2457 	tx = dmu_tx_create(zfsvfs->z_os);
2458 	dmu_tx_hold_bonus(tx, szp->z_id);	/* nlink changes */
2459 	dmu_tx_hold_bonus(tx, sdzp->z_id);	/* nlink changes */
2460 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
2461 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
2462 	if (sdzp != tdzp)
2463 		dmu_tx_hold_bonus(tx, tdzp->z_id);	/* nlink changes */
2464 	if (tzp)
2465 		dmu_tx_hold_bonus(tx, tzp->z_id);	/* parent changes */
2466 	dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL);
2467 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2468 	if (error) {
2469 		if (zl != NULL)
2470 			zfs_rename_unlock(&zl);
2471 		zfs_dirent_unlock(sdl);
2472 		zfs_dirent_unlock(tdl);
2473 		VN_RELE(ZTOV(szp));
2474 		if (tzp)
2475 			VN_RELE(ZTOV(tzp));
2476 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2477 			dmu_tx_wait(tx);
2478 			dmu_tx_abort(tx);
2479 			goto top;
2480 		}
2481 		dmu_tx_abort(tx);
2482 		ZFS_EXIT(zfsvfs);
2483 		return (error);
2484 	}
2485 
2486 	if (tzp)	/* Attempt to remove the existing target */
2487 		error = zfs_link_destroy(tdl, tzp, tx, 0, NULL);
2488 
2489 	if (error == 0) {
2490 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
2491 		if (error == 0) {
2492 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
2493 			ASSERT(error == 0);
2494 			zfs_log_rename(zilog, tx, TX_RENAME, sdzp,
2495 			    sdl->dl_name, tdzp, tdl->dl_name, szp);
2496 		}
2497 	}
2498 
2499 	dmu_tx_commit(tx);
2500 out:
2501 	if (zl != NULL)
2502 		zfs_rename_unlock(&zl);
2503 
2504 	zfs_dirent_unlock(sdl);
2505 	zfs_dirent_unlock(tdl);
2506 
2507 	VN_RELE(ZTOV(szp));
2508 	if (tzp)
2509 		VN_RELE(ZTOV(tzp));
2510 
2511 	ZFS_EXIT(zfsvfs);
2512 	return (error);
2513 }
2514 
2515 /*
2516  * Insert the indicated symbolic reference entry into the directory.
2517  *
2518  *	IN:	dvp	- Directory to contain new symbolic link.
2519  *		link	- Name for new symlink entry.
2520  *		vap	- Attributes of new entry.
2521  *		target	- Target path of new symlink.
2522  *		cr	- credentials of caller.
2523  *
2524  *	RETURN:	0 if success
2525  *		error code if failure
2526  *
2527  * Timestamps:
2528  *	dvp - ctime|mtime updated
2529  */
2530 static int
2531 zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr)
2532 {
2533 	znode_t		*zp, *dzp = VTOZ(dvp);
2534 	zfs_dirlock_t	*dl;
2535 	dmu_tx_t	*tx;
2536 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2537 	zilog_t		*zilog = zfsvfs->z_log;
2538 	uint64_t	zoid;
2539 	int		len = strlen(link);
2540 	int		error;
2541 
2542 	ASSERT(vap->va_type == VLNK);
2543 
2544 	ZFS_ENTER(zfsvfs);
2545 top:
2546 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
2547 		ZFS_EXIT(zfsvfs);
2548 		return (error);
2549 	}
2550 
2551 	if (len > MAXPATHLEN) {
2552 		ZFS_EXIT(zfsvfs);
2553 		return (ENAMETOOLONG);
2554 	}
2555 
2556 	/*
2557 	 * Attempt to lock directory; fail if entry already exists.
2558 	 */
2559 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZNEW)) {
2560 		ZFS_EXIT(zfsvfs);
2561 		return (error);
2562 	}
2563 
2564 	tx = dmu_tx_create(zfsvfs->z_os);
2565 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
2566 	dmu_tx_hold_bonus(tx, dzp->z_id);
2567 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
2568 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
2569 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
2570 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2571 	if (error) {
2572 		zfs_dirent_unlock(dl);
2573 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2574 			dmu_tx_wait(tx);
2575 			dmu_tx_abort(tx);
2576 			goto top;
2577 		}
2578 		dmu_tx_abort(tx);
2579 		ZFS_EXIT(zfsvfs);
2580 		return (error);
2581 	}
2582 
2583 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
2584 
2585 	/*
2586 	 * Create a new object for the symlink.
2587 	 * Put the link content into bonus buffer if it will fit;
2588 	 * otherwise, store it just like any other file data.
2589 	 */
2590 	zoid = 0;
2591 	if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
2592 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, len);
2593 		if (len != 0)
2594 			bcopy(link, zp->z_phys + 1, len);
2595 	} else {
2596 		dmu_buf_t *dbp;
2597 
2598 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
2599 
2600 		/*
2601 		 * Nothing can access the znode yet so no locking needed
2602 		 * for growing the znode's blocksize.
2603 		 */
2604 		zfs_grow_blocksize(zp, len, tx);
2605 
2606 		VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, zoid, 0, FTAG, &dbp));
2607 		dmu_buf_will_dirty(dbp, tx);
2608 
2609 		ASSERT3U(len, <=, dbp->db_size);
2610 		bcopy(link, dbp->db_data, len);
2611 		dmu_buf_rele(dbp, FTAG);
2612 	}
2613 	zp->z_phys->zp_size = len;
2614 
2615 	/*
2616 	 * Insert the new object into the directory.
2617 	 */
2618 	(void) zfs_link_create(dl, zp, tx, ZNEW);
2619 out:
2620 	if (error == 0)
2621 		zfs_log_symlink(zilog, tx, TX_SYMLINK, dzp, zp, name, link);
2622 
2623 	dmu_tx_commit(tx);
2624 
2625 	zfs_dirent_unlock(dl);
2626 
2627 	VN_RELE(ZTOV(zp));
2628 
2629 	ZFS_EXIT(zfsvfs);
2630 	return (error);
2631 }
2632 
2633 /*
2634  * Return, in the buffer contained in the provided uio structure,
2635  * the symbolic path referred to by vp.
2636  *
2637  *	IN:	vp	- vnode of symbolic link.
2638  *		uoip	- structure to contain the link path.
2639  *		cr	- credentials of caller.
2640  *
2641  *	OUT:	uio	- structure to contain the link path.
2642  *
2643  *	RETURN:	0 if success
2644  *		error code if failure
2645  *
2646  * Timestamps:
2647  *	vp - atime updated
2648  */
2649 /* ARGSUSED */
2650 static int
2651 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr)
2652 {
2653 	znode_t		*zp = VTOZ(vp);
2654 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2655 	size_t		bufsz;
2656 	int		error;
2657 
2658 	ZFS_ENTER(zfsvfs);
2659 
2660 	bufsz = (size_t)zp->z_phys->zp_size;
2661 	if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
2662 		error = uiomove(zp->z_phys + 1,
2663 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
2664 	} else {
2665 		dmu_buf_t *dbp;
2666 		error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
2667 		if (error) {
2668 			ZFS_EXIT(zfsvfs);
2669 			return (error);
2670 		}
2671 		error = uiomove(dbp->db_data,
2672 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
2673 		dmu_buf_rele(dbp, FTAG);
2674 	}
2675 
2676 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2677 	ZFS_EXIT(zfsvfs);
2678 	return (error);
2679 }
2680 
2681 /*
2682  * Insert a new entry into directory tdvp referencing svp.
2683  *
2684  *	IN:	tdvp	- Directory to contain new entry.
2685  *		svp	- vnode of new entry.
2686  *		name	- name of new entry.
2687  *		cr	- credentials of caller.
2688  *
2689  *	RETURN:	0 if success
2690  *		error code if failure
2691  *
2692  * Timestamps:
2693  *	tdvp - ctime|mtime updated
2694  *	 svp - ctime updated
2695  */
2696 /* ARGSUSED */
2697 static int
2698 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr)
2699 {
2700 	znode_t		*dzp = VTOZ(tdvp);
2701 	znode_t		*tzp, *szp;
2702 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2703 	zilog_t		*zilog = zfsvfs->z_log;
2704 	zfs_dirlock_t	*dl;
2705 	dmu_tx_t	*tx;
2706 	vnode_t		*realvp;
2707 	int		error;
2708 
2709 	ASSERT(tdvp->v_type == VDIR);
2710 
2711 	ZFS_ENTER(zfsvfs);
2712 
2713 	if (VOP_REALVP(svp, &realvp) == 0)
2714 		svp = realvp;
2715 
2716 	if (svp->v_vfsp != tdvp->v_vfsp) {
2717 		ZFS_EXIT(zfsvfs);
2718 		return (EXDEV);
2719 	}
2720 
2721 	szp = VTOZ(svp);
2722 top:
2723 	/*
2724 	 * We do not support links between attributes and non-attributes
2725 	 * because of the potential security risk of creating links
2726 	 * into "normal" file space in order to circumvent restrictions
2727 	 * imposed in attribute space.
2728 	 */
2729 	if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
2730 	    (dzp->z_phys->zp_flags & ZFS_XATTR)) {
2731 		ZFS_EXIT(zfsvfs);
2732 		return (EINVAL);
2733 	}
2734 
2735 	/*
2736 	 * POSIX dictates that we return EPERM here.
2737 	 * Better choices include ENOTSUP or EISDIR.
2738 	 */
2739 	if (svp->v_type == VDIR) {
2740 		ZFS_EXIT(zfsvfs);
2741 		return (EPERM);
2742 	}
2743 
2744 	if ((uid_t)szp->z_phys->zp_uid != crgetuid(cr) &&
2745 	    secpolicy_basic_link(cr) != 0) {
2746 		ZFS_EXIT(zfsvfs);
2747 		return (EPERM);
2748 	}
2749 
2750 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
2751 		ZFS_EXIT(zfsvfs);
2752 		return (error);
2753 	}
2754 
2755 	/*
2756 	 * Attempt to lock directory; fail if entry already exists.
2757 	 */
2758 	if (error = zfs_dirent_lock(&dl, dzp, name, &tzp, ZNEW)) {
2759 		ZFS_EXIT(zfsvfs);
2760 		return (error);
2761 	}
2762 
2763 	tx = dmu_tx_create(zfsvfs->z_os);
2764 	dmu_tx_hold_bonus(tx, szp->z_id);
2765 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
2766 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2767 	if (error) {
2768 		zfs_dirent_unlock(dl);
2769 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2770 			dmu_tx_wait(tx);
2771 			dmu_tx_abort(tx);
2772 			goto top;
2773 		}
2774 		dmu_tx_abort(tx);
2775 		ZFS_EXIT(zfsvfs);
2776 		return (error);
2777 	}
2778 
2779 	error = zfs_link_create(dl, szp, tx, 0);
2780 
2781 	if (error == 0)
2782 		zfs_log_link(zilog, tx, TX_LINK, dzp, szp, name);
2783 
2784 	dmu_tx_commit(tx);
2785 
2786 	zfs_dirent_unlock(dl);
2787 
2788 	ZFS_EXIT(zfsvfs);
2789 	return (error);
2790 }
2791 
2792 /*
2793  * zfs_null_putapage() is used when the file system has been force
2794  * unmounted. It just drops the pages.
2795  */
2796 /* ARGSUSED */
2797 static int
2798 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
2799 		size_t *lenp, int flags, cred_t *cr)
2800 {
2801 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
2802 	return (0);
2803 }
2804 
2805 /*
2806  * Push a page out to disk, klustering if possible.
2807  *
2808  *	IN:	vp	- file to push page to.
2809  *		pp	- page to push.
2810  *		flags	- additional flags.
2811  *		cr	- credentials of caller.
2812  *
2813  *	OUT:	offp	- start of range pushed.
2814  *		lenp	- len of range pushed.
2815  *
2816  *	RETURN:	0 if success
2817  *		error code if failure
2818  *
2819  * NOTE: callers must have locked the page to be pushed.  On
2820  * exit, the page (and all other pages in the kluster) must be
2821  * unlocked.
2822  */
2823 /* ARGSUSED */
2824 static int
2825 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
2826 		size_t *lenp, int flags, cred_t *cr)
2827 {
2828 	znode_t		*zp = VTOZ(vp);
2829 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2830 	zilog_t		*zilog = zfsvfs->z_log;
2831 	dmu_tx_t	*tx;
2832 	rl_t		*rl;
2833 	u_offset_t	off, koff;
2834 	size_t		len, klen;
2835 	int		err;
2836 
2837 	off = pp->p_offset;
2838 	len = PAGESIZE;
2839 	/*
2840 	 * If our blocksize is bigger than the page size, try to kluster
2841 	 * muiltiple pages so that we write a full block (thus avoiding
2842 	 * a read-modify-write).
2843 	 */
2844 	if (zp->z_blksz > PAGESIZE) {
2845 		uint64_t filesz = zp->z_phys->zp_size;
2846 
2847 		if (!ISP2(zp->z_blksz)) {
2848 			/* Only one block in the file. */
2849 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
2850 			koff = 0;
2851 		} else {
2852 			klen = zp->z_blksz;
2853 			koff = P2ALIGN(off, (u_offset_t)klen);
2854 		}
2855 		ASSERT(koff <= filesz);
2856 		if (koff + klen > filesz)
2857 			klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE);
2858 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
2859 	}
2860 	ASSERT3U(btop(len), ==, btopr(len));
2861 top:
2862 	rl = zfs_range_lock(zp, off, len, RL_WRITER);
2863 	/*
2864 	 * Can't push pages past end-of-file.
2865 	 */
2866 	if (off >= zp->z_phys->zp_size) {
2867 		/* discard all pages */
2868 		flags |= B_INVAL;
2869 		err = 0;
2870 		goto out;
2871 	} else if (off + len > zp->z_phys->zp_size) {
2872 		int npages = btopr(zp->z_phys->zp_size - off);
2873 		page_t *trunc;
2874 
2875 		page_list_break(&pp, &trunc, npages);
2876 		/* discard pages past end of file */
2877 		if (trunc)
2878 			pvn_write_done(trunc, B_INVAL | flags);
2879 		len = zp->z_phys->zp_size - off;
2880 	}
2881 
2882 	tx = dmu_tx_create(zfsvfs->z_os);
2883 	dmu_tx_hold_write(tx, zp->z_id, off, len);
2884 	dmu_tx_hold_bonus(tx, zp->z_id);
2885 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2886 	if (err != 0) {
2887 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2888 			zfs_range_unlock(rl);
2889 			dmu_tx_wait(tx);
2890 			dmu_tx_abort(tx);
2891 			err = 0;
2892 			goto top;
2893 		}
2894 		dmu_tx_abort(tx);
2895 		goto out;
2896 	}
2897 
2898 	if (zp->z_blksz <= PAGESIZE) {
2899 		caddr_t va = ppmapin(pp, PROT_READ, (caddr_t)-1);
2900 		ASSERT3U(len, <=, PAGESIZE);
2901 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
2902 		ppmapout(va);
2903 	} else {
2904 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
2905 	}
2906 
2907 	if (err == 0) {
2908 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
2909 		(void) zfs_log_write(
2910 		    zilog, tx, TX_WRITE, zp, off, len, 0, NULL);
2911 		dmu_tx_commit(tx);
2912 	}
2913 
2914 out:
2915 	zfs_range_unlock(rl);
2916 	pvn_write_done(pp, (err ? B_ERROR : 0) | B_WRITE | flags);
2917 	if (offp)
2918 		*offp = off;
2919 	if (lenp)
2920 		*lenp = len;
2921 
2922 	return (err);
2923 }
2924 
2925 /*
2926  * Copy the portion of the file indicated from pages into the file.
2927  * The pages are stored in a page list attached to the files vnode.
2928  *
2929  *	IN:	vp	- vnode of file to push page data to.
2930  *		off	- position in file to put data.
2931  *		len	- amount of data to write.
2932  *		flags	- flags to control the operation.
2933  *		cr	- credentials of caller.
2934  *
2935  *	RETURN:	0 if success
2936  *		error code if failure
2937  *
2938  * Timestamps:
2939  *	vp - ctime|mtime updated
2940  */
2941 static int
2942 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr)
2943 {
2944 	znode_t		*zp = VTOZ(vp);
2945 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2946 	page_t		*pp;
2947 	size_t		io_len;
2948 	u_offset_t	io_off;
2949 	uint64_t	filesz;
2950 	int		error = 0;
2951 
2952 	ZFS_ENTER(zfsvfs);
2953 
2954 	ASSERT(zp->z_dbuf_held && zp->z_phys);
2955 
2956 	if (len == 0) {
2957 		/*
2958 		 * Search the entire vp list for pages >= off.
2959 		 */
2960 		error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage,
2961 		    flags, cr);
2962 		goto out;
2963 	}
2964 
2965 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
2966 	if (off > filesz) {
2967 		/* past end of file */
2968 		ZFS_EXIT(zfsvfs);
2969 		return (0);
2970 	}
2971 
2972 	len = MIN(len, filesz - off);
2973 
2974 	for (io_off = off; io_off < off + len; io_off += io_len) {
2975 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
2976 			pp = page_lookup(vp, io_off,
2977 				(flags & (B_INVAL | B_FREE)) ?
2978 					SE_EXCL : SE_SHARED);
2979 		} else {
2980 			pp = page_lookup_nowait(vp, io_off,
2981 				(flags & B_FREE) ? SE_EXCL : SE_SHARED);
2982 		}
2983 
2984 		if (pp != NULL && pvn_getdirty(pp, flags)) {
2985 			int err;
2986 
2987 			/*
2988 			 * Found a dirty page to push
2989 			 */
2990 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
2991 			if (err)
2992 				error = err;
2993 		} else {
2994 			io_len = PAGESIZE;
2995 		}
2996 	}
2997 out:
2998 	if ((flags & B_ASYNC) == 0)
2999 		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
3000 	ZFS_EXIT(zfsvfs);
3001 	return (error);
3002 }
3003 
3004 void
3005 zfs_inactive(vnode_t *vp, cred_t *cr)
3006 {
3007 	znode_t	*zp = VTOZ(vp);
3008 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3009 	int error;
3010 
3011 	rw_enter(&zfsvfs->z_um_lock, RW_READER);
3012 	if (zfsvfs->z_unmounted2) {
3013 		ASSERT(zp->z_dbuf_held == 0);
3014 
3015 		if (vn_has_cached_data(vp)) {
3016 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
3017 			    B_INVAL, cr);
3018 		}
3019 
3020 		mutex_enter(&zp->z_lock);
3021 		vp->v_count = 0; /* count arrives as 1 */
3022 		if (zp->z_dbuf == NULL) {
3023 			mutex_exit(&zp->z_lock);
3024 			zfs_znode_free(zp);
3025 		} else {
3026 			mutex_exit(&zp->z_lock);
3027 		}
3028 		rw_exit(&zfsvfs->z_um_lock);
3029 		VFS_RELE(zfsvfs->z_vfs);
3030 		return;
3031 	}
3032 
3033 	/*
3034 	 * Attempt to push any data in the page cache.  If this fails
3035 	 * we will get kicked out later in zfs_zinactive().
3036 	 */
3037 	if (vn_has_cached_data(vp)) {
3038 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
3039 		    cr);
3040 	}
3041 
3042 	if (zp->z_atime_dirty && zp->z_reap == 0) {
3043 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3044 
3045 		dmu_tx_hold_bonus(tx, zp->z_id);
3046 		error = dmu_tx_assign(tx, TXG_WAIT);
3047 		if (error) {
3048 			dmu_tx_abort(tx);
3049 		} else {
3050 			dmu_buf_will_dirty(zp->z_dbuf, tx);
3051 			mutex_enter(&zp->z_lock);
3052 			zp->z_atime_dirty = 0;
3053 			mutex_exit(&zp->z_lock);
3054 			dmu_tx_commit(tx);
3055 		}
3056 	}
3057 
3058 	zfs_zinactive(zp);
3059 	rw_exit(&zfsvfs->z_um_lock);
3060 }
3061 
3062 /*
3063  * Bounds-check the seek operation.
3064  *
3065  *	IN:	vp	- vnode seeking within
3066  *		ooff	- old file offset
3067  *		noffp	- pointer to new file offset
3068  *
3069  *	RETURN:	0 if success
3070  *		EINVAL if new offset invalid
3071  */
3072 /* ARGSUSED */
3073 static int
3074 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp)
3075 {
3076 	if (vp->v_type == VDIR)
3077 		return (0);
3078 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
3079 }
3080 
3081 /*
3082  * Pre-filter the generic locking function to trap attempts to place
3083  * a mandatory lock on a memory mapped file.
3084  */
3085 static int
3086 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
3087     flk_callback_t *flk_cbp, cred_t *cr)
3088 {
3089 	znode_t *zp = VTOZ(vp);
3090 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3091 	int error;
3092 
3093 	ZFS_ENTER(zfsvfs);
3094 
3095 	/*
3096 	 * We are following the UFS semantics with respect to mapcnt
3097 	 * here: If we see that the file is mapped already, then we will
3098 	 * return an error, but we don't worry about races between this
3099 	 * function and zfs_map().
3100 	 */
3101 	if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) {
3102 		ZFS_EXIT(zfsvfs);
3103 		return (EAGAIN);
3104 	}
3105 	error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr);
3106 	ZFS_EXIT(zfsvfs);
3107 	return (error);
3108 }
3109 
3110 /*
3111  * If we can't find a page in the cache, we will create a new page
3112  * and fill it with file data.  For efficiency, we may try to fill
3113  * multiple pages at once (klustering).
3114  */
3115 static int
3116 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
3117     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
3118 {
3119 	znode_t *zp = VTOZ(vp);
3120 	page_t *pp, *cur_pp;
3121 	objset_t *os = zp->z_zfsvfs->z_os;
3122 	caddr_t va;
3123 	u_offset_t io_off, total;
3124 	uint64_t oid = zp->z_id;
3125 	size_t io_len;
3126 	uint64_t filesz;
3127 	int err;
3128 
3129 	/*
3130 	 * If we are only asking for a single page don't bother klustering.
3131 	 */
3132 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
3133 	if (off >= filesz)
3134 		return (EFAULT);
3135 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
3136 		io_off = off;
3137 		io_len = PAGESIZE;
3138 		pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr);
3139 	} else {
3140 		/*
3141 		 * Try to fill a kluster of pages (a blocks worth).
3142 		 */
3143 		size_t klen;
3144 		u_offset_t koff;
3145 
3146 		if (!ISP2(zp->z_blksz)) {
3147 			/* Only one block in the file. */
3148 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
3149 			koff = 0;
3150 		} else {
3151 			/*
3152 			 * It would be ideal to align our offset to the
3153 			 * blocksize but doing so has resulted in some
3154 			 * strange application crashes. For now, we
3155 			 * leave the offset as is and only adjust the
3156 			 * length if we are off the end of the file.
3157 			 */
3158 			koff = off;
3159 			klen = plsz;
3160 		}
3161 		ASSERT(koff <= filesz);
3162 		if (koff + klen > filesz)
3163 			klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff;
3164 		ASSERT3U(off, >=, koff);
3165 		ASSERT3U(off, <, koff + klen);
3166 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
3167 			    &io_len, koff, klen, 0);
3168 	}
3169 	if (pp == NULL) {
3170 		/*
3171 		 * Some other thread entered the page before us.
3172 		 * Return to zfs_getpage to retry the lookup.
3173 		 */
3174 		*pl = NULL;
3175 		return (0);
3176 	}
3177 
3178 	/*
3179 	 * Fill the pages in the kluster.
3180 	 */
3181 	cur_pp = pp;
3182 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
3183 		ASSERT3U(io_off, ==, cur_pp->p_offset);
3184 		va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1);
3185 		err = dmu_read(os, oid, io_off, PAGESIZE, va);
3186 		ppmapout(va);
3187 		if (err) {
3188 			/* On error, toss the entire kluster */
3189 			pvn_read_done(pp, B_ERROR);
3190 			return (err);
3191 		}
3192 		cur_pp = cur_pp->p_next;
3193 	}
3194 out:
3195 	/*
3196 	 * Fill in the page list array from the kluster.  If
3197 	 * there are too many pages in the kluster, return
3198 	 * as many pages as possible starting from the desired
3199 	 * offset `off'.
3200 	 * NOTE: the page list will always be null terminated.
3201 	 */
3202 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
3203 
3204 	return (0);
3205 }
3206 
3207 /*
3208  * Return pointers to the pages for the file region [off, off + len]
3209  * in the pl array.  If plsz is greater than len, this function may
3210  * also return page pointers from before or after the specified
3211  * region (i.e. some region [off', off' + plsz]).  These additional
3212  * pages are only returned if they are already in the cache, or were
3213  * created as part of a klustered read.
3214  *
3215  *	IN:	vp	- vnode of file to get data from.
3216  *		off	- position in file to get data from.
3217  *		len	- amount of data to retrieve.
3218  *		plsz	- length of provided page list.
3219  *		seg	- segment to obtain pages for.
3220  *		addr	- virtual address of fault.
3221  *		rw	- mode of created pages.
3222  *		cr	- credentials of caller.
3223  *
3224  *	OUT:	protp	- protection mode of created pages.
3225  *		pl	- list of pages created.
3226  *
3227  *	RETURN:	0 if success
3228  *		error code if failure
3229  *
3230  * Timestamps:
3231  *	vp - atime updated
3232  */
3233 /* ARGSUSED */
3234 static int
3235 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
3236 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
3237 	enum seg_rw rw, cred_t *cr)
3238 {
3239 	znode_t		*zp = VTOZ(vp);
3240 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3241 	page_t		*pp, **pl0 = pl;
3242 	int		need_unlock = 0, err = 0;
3243 	offset_t	orig_off;
3244 
3245 	ZFS_ENTER(zfsvfs);
3246 
3247 	if (protp)
3248 		*protp = PROT_ALL;
3249 
3250 	ASSERT(zp->z_dbuf_held && zp->z_phys);
3251 
3252 	/* no faultahead (for now) */
3253 	if (pl == NULL) {
3254 		ZFS_EXIT(zfsvfs);
3255 		return (0);
3256 	}
3257 
3258 	/* can't fault past EOF */
3259 	if (off >= zp->z_phys->zp_size) {
3260 		ZFS_EXIT(zfsvfs);
3261 		return (EFAULT);
3262 	}
3263 	orig_off = off;
3264 
3265 	/*
3266 	 * If we already own the lock, then we must be page faulting
3267 	 * in the middle of a write to this file (i.e., we are writing
3268 	 * to this file using data from a mapped region of the file).
3269 	 */
3270 	if (rw_owner(&zp->z_map_lock) != curthread) {
3271 		rw_enter(&zp->z_map_lock, RW_WRITER);
3272 		need_unlock = TRUE;
3273 	}
3274 
3275 	/*
3276 	 * Loop through the requested range [off, off + len] looking
3277 	 * for pages.  If we don't find a page, we will need to create
3278 	 * a new page and fill it with data from the file.
3279 	 */
3280 	while (len > 0) {
3281 		if (plsz < PAGESIZE)
3282 			break;
3283 		if (pp = page_lookup(vp, off, SE_SHARED)) {
3284 			*pl++ = pp;
3285 			off += PAGESIZE;
3286 			addr += PAGESIZE;
3287 			len -= PAGESIZE;
3288 			plsz -= PAGESIZE;
3289 		} else {
3290 			err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw);
3291 			if (err)
3292 				goto out;
3293 			/*
3294 			 * klustering may have changed our region
3295 			 * to be block aligned.
3296 			 */
3297 			if (((pp = *pl) != 0) && (off != pp->p_offset)) {
3298 				int delta = off - pp->p_offset;
3299 				len += delta;
3300 				off -= delta;
3301 				addr -= delta;
3302 			}
3303 			while (*pl) {
3304 				pl++;
3305 				off += PAGESIZE;
3306 				addr += PAGESIZE;
3307 				plsz -= PAGESIZE;
3308 				if (len > PAGESIZE)
3309 					len -= PAGESIZE;
3310 				else
3311 					len = 0;
3312 			}
3313 		}
3314 	}
3315 
3316 	/*
3317 	 * Fill out the page array with any pages already in the cache.
3318 	 */
3319 	while (plsz > 0) {
3320 		pp = page_lookup_nowait(vp, off, SE_SHARED);
3321 		if (pp == NULL)
3322 			break;
3323 		*pl++ = pp;
3324 		off += PAGESIZE;
3325 		plsz -= PAGESIZE;
3326 	}
3327 
3328 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3329 out:
3330 	/*
3331 	 * We can't grab the range lock for the page as reader which would
3332 	 * stop truncation as this leads to deadlock. So we need to recheck
3333 	 * the file size.
3334 	 */
3335 	if (orig_off >= zp->z_phys->zp_size)
3336 		err = EFAULT;
3337 	if (err) {
3338 		/*
3339 		 * Release any pages we have previously locked.
3340 		 */
3341 		while (pl > pl0)
3342 			page_unlock(*--pl);
3343 	}
3344 
3345 	*pl = NULL;
3346 
3347 	if (need_unlock)
3348 		rw_exit(&zp->z_map_lock);
3349 
3350 	ZFS_EXIT(zfsvfs);
3351 	return (err);
3352 }
3353 
3354 /*
3355  * Request a memory map for a section of a file.  This code interacts
3356  * with common code and the VM system as follows:
3357  *
3358  *	common code calls mmap(), which ends up in smmap_common()
3359  *
3360  *	this calls VOP_MAP(), which takes you into (say) zfs
3361  *
3362  *	zfs_map() calls as_map(), passing segvn_create() as the callback
3363  *
3364  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
3365  *
3366  *	zfs_addmap() updates z_mapcnt
3367  */
3368 static int
3369 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
3370     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
3371 {
3372 	znode_t *zp = VTOZ(vp);
3373 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3374 	segvn_crargs_t	vn_a;
3375 	int		error;
3376 
3377 	ZFS_ENTER(zfsvfs);
3378 
3379 	if (vp->v_flag & VNOMAP) {
3380 		ZFS_EXIT(zfsvfs);
3381 		return (ENOSYS);
3382 	}
3383 
3384 	if (off < 0 || len > MAXOFFSET_T - off) {
3385 		ZFS_EXIT(zfsvfs);
3386 		return (ENXIO);
3387 	}
3388 
3389 	if (vp->v_type != VREG) {
3390 		ZFS_EXIT(zfsvfs);
3391 		return (ENODEV);
3392 	}
3393 
3394 	/*
3395 	 * If file is locked, disallow mapping.
3396 	 */
3397 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) {
3398 		ZFS_EXIT(zfsvfs);
3399 		return (EAGAIN);
3400 	}
3401 
3402 	as_rangelock(as);
3403 	if ((flags & MAP_FIXED) == 0) {
3404 		map_addr(addrp, len, off, 1, flags);
3405 		if (*addrp == NULL) {
3406 			as_rangeunlock(as);
3407 			ZFS_EXIT(zfsvfs);
3408 			return (ENOMEM);
3409 		}
3410 	} else {
3411 		/*
3412 		 * User specified address - blow away any previous mappings
3413 		 */
3414 		(void) as_unmap(as, *addrp, len);
3415 	}
3416 
3417 	vn_a.vp = vp;
3418 	vn_a.offset = (u_offset_t)off;
3419 	vn_a.type = flags & MAP_TYPE;
3420 	vn_a.prot = prot;
3421 	vn_a.maxprot = maxprot;
3422 	vn_a.cred = cr;
3423 	vn_a.amp = NULL;
3424 	vn_a.flags = flags & ~MAP_TYPE;
3425 	vn_a.szc = 0;
3426 	vn_a.lgrp_mem_policy_flags = 0;
3427 
3428 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
3429 
3430 	as_rangeunlock(as);
3431 	ZFS_EXIT(zfsvfs);
3432 	return (error);
3433 }
3434 
3435 /* ARGSUSED */
3436 static int
3437 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
3438     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
3439 {
3440 	uint64_t pages = btopr(len);
3441 
3442 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
3443 	return (0);
3444 }
3445 
3446 /*
3447  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
3448  * more accurate mtime for the associated file.  Since we don't have a way of
3449  * detecting when the data was actually modified, we have to resort to
3450  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
3451  * last page is pushed.  The problem occurs when the msync() call is omitted,
3452  * which by far the most common case:
3453  *
3454  * 	open()
3455  * 	mmap()
3456  * 	<modify memory>
3457  * 	munmap()
3458  * 	close()
3459  * 	<time lapse>
3460  * 	putpage() via fsflush
3461  *
3462  * If we wait until fsflush to come along, we can have a modification time that
3463  * is some arbitrary point in the future.  In order to prevent this in the
3464  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
3465  * torn down.
3466  */
3467 /* ARGSUSED */
3468 static int
3469 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
3470     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr)
3471 {
3472 	uint64_t pages = btopr(len);
3473 
3474 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
3475 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
3476 
3477 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
3478 	    vn_has_cached_data(vp))
3479 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr);
3480 
3481 	return (0);
3482 }
3483 
3484 /*
3485  * Free or allocate space in a file.  Currently, this function only
3486  * supports the `F_FREESP' command.  However, this command is somewhat
3487  * misnamed, as its functionality includes the ability to allocate as
3488  * well as free space.
3489  *
3490  *	IN:	vp	- vnode of file to free data in.
3491  *		cmd	- action to take (only F_FREESP supported).
3492  *		bfp	- section of file to free/alloc.
3493  *		flag	- current file open mode flags.
3494  *		offset	- current file offset.
3495  *		cr	- credentials of caller [UNUSED].
3496  *
3497  *	RETURN:	0 if success
3498  *		error code if failure
3499  *
3500  * Timestamps:
3501  *	vp - ctime|mtime updated
3502  */
3503 /* ARGSUSED */
3504 static int
3505 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
3506     offset_t offset, cred_t *cr, caller_context_t *ct)
3507 {
3508 	znode_t		*zp = VTOZ(vp);
3509 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3510 	uint64_t	off, len;
3511 	int		error;
3512 
3513 	ZFS_ENTER(zfsvfs);
3514 
3515 top:
3516 	if (cmd != F_FREESP) {
3517 		ZFS_EXIT(zfsvfs);
3518 		return (EINVAL);
3519 	}
3520 
3521 	if (error = convoff(vp, bfp, 0, offset)) {
3522 		ZFS_EXIT(zfsvfs);
3523 		return (error);
3524 	}
3525 
3526 	if (bfp->l_len < 0) {
3527 		ZFS_EXIT(zfsvfs);
3528 		return (EINVAL);
3529 	}
3530 
3531 	off = bfp->l_start;
3532 	len = bfp->l_len; /* 0 means from off to end of file */
3533 
3534 	do {
3535 		error = zfs_freesp(zp, off, len, flag, TRUE);
3536 		/* NB: we already did dmu_tx_wait() if necessary */
3537 	} while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
3538 
3539 	ZFS_EXIT(zfsvfs);
3540 	return (error);
3541 }
3542 
3543 static int
3544 zfs_fid(vnode_t *vp, fid_t *fidp)
3545 {
3546 	znode_t		*zp = VTOZ(vp);
3547 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3548 	uint32_t	gen = (uint32_t)zp->z_phys->zp_gen;
3549 	uint64_t	object = zp->z_id;
3550 	zfid_short_t	*zfid;
3551 	int		size, i;
3552 
3553 	ZFS_ENTER(zfsvfs);
3554 
3555 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
3556 	if (fidp->fid_len < size) {
3557 		fidp->fid_len = size;
3558 		ZFS_EXIT(zfsvfs);
3559 		return (ENOSPC);
3560 	}
3561 
3562 	zfid = (zfid_short_t *)fidp;
3563 
3564 	zfid->zf_len = size;
3565 
3566 	for (i = 0; i < sizeof (zfid->zf_object); i++)
3567 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
3568 
3569 	/* Must have a non-zero generation number to distinguish from .zfs */
3570 	if (gen == 0)
3571 		gen = 1;
3572 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
3573 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
3574 
3575 	if (size == LONG_FID_LEN) {
3576 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
3577 		zfid_long_t	*zlfid;
3578 
3579 		zlfid = (zfid_long_t *)fidp;
3580 
3581 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
3582 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
3583 
3584 		/* XXX - this should be the generation number for the objset */
3585 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
3586 			zlfid->zf_setgen[i] = 0;
3587 	}
3588 
3589 	ZFS_EXIT(zfsvfs);
3590 	return (0);
3591 }
3592 
3593 static int
3594 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr)
3595 {
3596 	znode_t		*zp, *xzp;
3597 	zfsvfs_t	*zfsvfs;
3598 	zfs_dirlock_t	*dl;
3599 	int		error;
3600 
3601 	switch (cmd) {
3602 	case _PC_LINK_MAX:
3603 		*valp = ULONG_MAX;
3604 		return (0);
3605 
3606 	case _PC_FILESIZEBITS:
3607 		*valp = 64;
3608 		return (0);
3609 
3610 	case _PC_XATTR_EXISTS:
3611 		zp = VTOZ(vp);
3612 		zfsvfs = zp->z_zfsvfs;
3613 		ZFS_ENTER(zfsvfs);
3614 		*valp = 0;
3615 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
3616 		    ZXATTR | ZEXISTS | ZSHARED);
3617 		if (error == 0) {
3618 			zfs_dirent_unlock(dl);
3619 			if (!zfs_dirempty(xzp))
3620 				*valp = 1;
3621 			VN_RELE(ZTOV(xzp));
3622 		} else if (error == ENOENT) {
3623 			/*
3624 			 * If there aren't extended attributes, it's the
3625 			 * same as having zero of them.
3626 			 */
3627 			error = 0;
3628 		}
3629 		ZFS_EXIT(zfsvfs);
3630 		return (error);
3631 
3632 	case _PC_ACL_ENABLED:
3633 		*valp = _ACL_ACE_ENABLED;
3634 		return (0);
3635 
3636 	case _PC_MIN_HOLE_SIZE:
3637 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
3638 		return (0);
3639 
3640 	default:
3641 		return (fs_pathconf(vp, cmd, valp, cr));
3642 	}
3643 }
3644 
3645 /*ARGSUSED*/
3646 static int
3647 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
3648 {
3649 	znode_t *zp = VTOZ(vp);
3650 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3651 	int error;
3652 
3653 	ZFS_ENTER(zfsvfs);
3654 	error = zfs_getacl(zp, vsecp, cr);
3655 	ZFS_EXIT(zfsvfs);
3656 
3657 	return (error);
3658 }
3659 
3660 /*ARGSUSED*/
3661 static int
3662 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
3663 {
3664 	znode_t *zp = VTOZ(vp);
3665 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3666 	int error;
3667 
3668 	ZFS_ENTER(zfsvfs);
3669 	error = zfs_setacl(zp, vsecp, cr);
3670 	ZFS_EXIT(zfsvfs);
3671 	return (error);
3672 }
3673 
3674 /*
3675  * Predeclare these here so that the compiler assumes that
3676  * this is an "old style" function declaration that does
3677  * not include arguments => we won't get type mismatch errors
3678  * in the initializations that follow.
3679  */
3680 static int zfs_inval();
3681 static int zfs_isdir();
3682 
3683 static int
3684 zfs_inval()
3685 {
3686 	return (EINVAL);
3687 }
3688 
3689 static int
3690 zfs_isdir()
3691 {
3692 	return (EISDIR);
3693 }
3694 /*
3695  * Directory vnode operations template
3696  */
3697 vnodeops_t *zfs_dvnodeops;
3698 const fs_operation_def_t zfs_dvnodeops_template[] = {
3699 	VOPNAME_OPEN, zfs_open,
3700 	VOPNAME_CLOSE, zfs_close,
3701 	VOPNAME_READ, zfs_isdir,
3702 	VOPNAME_WRITE, zfs_isdir,
3703 	VOPNAME_IOCTL, zfs_ioctl,
3704 	VOPNAME_GETATTR, zfs_getattr,
3705 	VOPNAME_SETATTR, zfs_setattr,
3706 	VOPNAME_ACCESS, zfs_access,
3707 	VOPNAME_LOOKUP, zfs_lookup,
3708 	VOPNAME_CREATE, zfs_create,
3709 	VOPNAME_REMOVE, zfs_remove,
3710 	VOPNAME_LINK, zfs_link,
3711 	VOPNAME_RENAME, zfs_rename,
3712 	VOPNAME_MKDIR, zfs_mkdir,
3713 	VOPNAME_RMDIR, zfs_rmdir,
3714 	VOPNAME_READDIR, zfs_readdir,
3715 	VOPNAME_SYMLINK, zfs_symlink,
3716 	VOPNAME_FSYNC, zfs_fsync,
3717 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3718 	VOPNAME_FID, zfs_fid,
3719 	VOPNAME_SEEK, zfs_seek,
3720 	VOPNAME_PATHCONF, zfs_pathconf,
3721 	VOPNAME_GETSECATTR, zfs_getsecattr,
3722 	VOPNAME_SETSECATTR, zfs_setsecattr,
3723 	NULL, NULL
3724 };
3725 
3726 /*
3727  * Regular file vnode operations template
3728  */
3729 vnodeops_t *zfs_fvnodeops;
3730 const fs_operation_def_t zfs_fvnodeops_template[] = {
3731 	VOPNAME_OPEN, zfs_open,
3732 	VOPNAME_CLOSE, zfs_close,
3733 	VOPNAME_READ, zfs_read,
3734 	VOPNAME_WRITE, zfs_write,
3735 	VOPNAME_IOCTL, zfs_ioctl,
3736 	VOPNAME_GETATTR, zfs_getattr,
3737 	VOPNAME_SETATTR, zfs_setattr,
3738 	VOPNAME_ACCESS, zfs_access,
3739 	VOPNAME_LOOKUP, zfs_lookup,
3740 	VOPNAME_RENAME, zfs_rename,
3741 	VOPNAME_FSYNC, zfs_fsync,
3742 	VOPNAME_INACTIVE, (fs_generic_func_p)zfs_inactive,
3743 	VOPNAME_FID, zfs_fid,
3744 	VOPNAME_SEEK, zfs_seek,
3745 	VOPNAME_FRLOCK, zfs_frlock,
3746 	VOPNAME_SPACE, zfs_space,
3747 	VOPNAME_GETPAGE, zfs_getpage,
3748 	VOPNAME_PUTPAGE, zfs_putpage,
3749 	VOPNAME_MAP, (fs_generic_func_p) zfs_map,
3750 	VOPNAME_ADDMAP, (fs_generic_func_p) zfs_addmap,
3751 	VOPNAME_DELMAP, zfs_delmap,
3752 	VOPNAME_PATHCONF, zfs_pathconf,
3753 	VOPNAME_GETSECATTR, zfs_getsecattr,
3754 	VOPNAME_SETSECATTR, zfs_setsecattr,
3755 	VOPNAME_VNEVENT, fs_vnevent_support,
3756 	NULL, NULL
3757 };
3758 
3759 /*
3760  * Symbolic link vnode operations template
3761  */
3762 vnodeops_t *zfs_symvnodeops;
3763 const fs_operation_def_t zfs_symvnodeops_template[] = {
3764 	VOPNAME_GETATTR, zfs_getattr,
3765 	VOPNAME_SETATTR, zfs_setattr,
3766 	VOPNAME_ACCESS, zfs_access,
3767 	VOPNAME_RENAME, zfs_rename,
3768 	VOPNAME_READLINK, zfs_readlink,
3769 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3770 	VOPNAME_FID, zfs_fid,
3771 	VOPNAME_PATHCONF, zfs_pathconf,
3772 	VOPNAME_VNEVENT, fs_vnevent_support,
3773 	NULL, NULL
3774 };
3775 
3776 /*
3777  * Extended attribute directory vnode operations template
3778  *	This template is identical to the directory vnodes
3779  *	operation template except for restricted operations:
3780  *		VOP_MKDIR()
3781  *		VOP_SYMLINK()
3782  * Note that there are other restrictions embedded in:
3783  *	zfs_create()	- restrict type to VREG
3784  *	zfs_link()	- no links into/out of attribute space
3785  *	zfs_rename()	- no moves into/out of attribute space
3786  */
3787 vnodeops_t *zfs_xdvnodeops;
3788 const fs_operation_def_t zfs_xdvnodeops_template[] = {
3789 	VOPNAME_OPEN, zfs_open,
3790 	VOPNAME_CLOSE, zfs_close,
3791 	VOPNAME_IOCTL, zfs_ioctl,
3792 	VOPNAME_GETATTR, zfs_getattr,
3793 	VOPNAME_SETATTR, zfs_setattr,
3794 	VOPNAME_ACCESS, zfs_access,
3795 	VOPNAME_LOOKUP, zfs_lookup,
3796 	VOPNAME_CREATE, zfs_create,
3797 	VOPNAME_REMOVE, zfs_remove,
3798 	VOPNAME_LINK, zfs_link,
3799 	VOPNAME_RENAME, zfs_rename,
3800 	VOPNAME_MKDIR, zfs_inval,
3801 	VOPNAME_RMDIR, zfs_rmdir,
3802 	VOPNAME_READDIR, zfs_readdir,
3803 	VOPNAME_SYMLINK, zfs_inval,
3804 	VOPNAME_FSYNC, zfs_fsync,
3805 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3806 	VOPNAME_FID, zfs_fid,
3807 	VOPNAME_SEEK, zfs_seek,
3808 	VOPNAME_PATHCONF, zfs_pathconf,
3809 	VOPNAME_GETSECATTR, zfs_getsecattr,
3810 	VOPNAME_SETSECATTR, zfs_setsecattr,
3811 	VOPNAME_VNEVENT, fs_vnevent_support,
3812 	NULL, NULL
3813 };
3814 
3815 /*
3816  * Error vnode operations template
3817  */
3818 vnodeops_t *zfs_evnodeops;
3819 const fs_operation_def_t zfs_evnodeops_template[] = {
3820 	VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive,
3821 	VOPNAME_PATHCONF, zfs_pathconf,
3822 	NULL, NULL
3823 };
3824