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