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