xref: /titanic_44/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision e5ba14ff435beeefdaa2e6649e175c74afe02c76)
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 && error != EINPROGRESS) ||
879 		    lr->lr_length <= zp->z_blksz);
880 		if (error == 0) {
881 			zil_add_vdev(zfsvfs->z_log,
882 			    DVA_GET_VDEV(BP_IDENTITY(&lr->lr_blkptr)));
883 		}
884 		/*
885 		 * If we get EINPROGRESS, then we need to wait for a
886 		 * write IO initiated by dmu_sync() to complete before
887 		 * we can release this dbuf.  We will finish everything
888 		 * up in the zfs_get_done() callback.
889 		 */
890 		if (error == EINPROGRESS)
891 			return (0);
892 		dmu_buf_rele(db, zgd);
893 		kmem_free(zgd, sizeof (zgd_t));
894 	}
895 out:
896 	zfs_range_unlock(rl);
897 	VN_RELE(ZTOV(zp));
898 	return (error);
899 }
900 
901 /*ARGSUSED*/
902 static int
903 zfs_access(vnode_t *vp, int mode, int flags, cred_t *cr)
904 {
905 	znode_t *zp = VTOZ(vp);
906 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
907 	int error;
908 
909 	ZFS_ENTER(zfsvfs);
910 	error = zfs_zaccess_rwx(zp, mode, cr);
911 	ZFS_EXIT(zfsvfs);
912 	return (error);
913 }
914 
915 /*
916  * Lookup an entry in a directory, or an extended attribute directory.
917  * If it exists, return a held vnode reference for it.
918  *
919  *	IN:	dvp	- vnode of directory to search.
920  *		nm	- name of entry to lookup.
921  *		pnp	- full pathname to lookup [UNUSED].
922  *		flags	- LOOKUP_XATTR set if looking for an attribute.
923  *		rdir	- root directory vnode [UNUSED].
924  *		cr	- credentials of caller.
925  *
926  *	OUT:	vpp	- vnode of located entry, NULL if not found.
927  *
928  *	RETURN:	0 if success
929  *		error code if failure
930  *
931  * Timestamps:
932  *	NA
933  */
934 /* ARGSUSED */
935 static int
936 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
937     int flags, vnode_t *rdir, cred_t *cr)
938 {
939 
940 	znode_t *zdp = VTOZ(dvp);
941 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
942 	int	error;
943 
944 	ZFS_ENTER(zfsvfs);
945 
946 	*vpp = NULL;
947 
948 	if (flags & LOOKUP_XATTR) {
949 		/*
950 		 * If the xattr property is off, refuse the lookup request.
951 		 */
952 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
953 			ZFS_EXIT(zfsvfs);
954 			return (EINVAL);
955 		}
956 
957 		/*
958 		 * We don't allow recursive attributes..
959 		 * Maybe someday we will.
960 		 */
961 		if (zdp->z_phys->zp_flags & ZFS_XATTR) {
962 			ZFS_EXIT(zfsvfs);
963 			return (EINVAL);
964 		}
965 
966 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
967 			ZFS_EXIT(zfsvfs);
968 			return (error);
969 		}
970 
971 		/*
972 		 * Do we have permission to get into attribute directory?
973 		 */
974 
975 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, cr)) {
976 			VN_RELE(*vpp);
977 		}
978 
979 		ZFS_EXIT(zfsvfs);
980 		return (error);
981 	}
982 
983 	if (dvp->v_type != VDIR) {
984 		ZFS_EXIT(zfsvfs);
985 		return (ENOTDIR);
986 	}
987 
988 	/*
989 	 * Check accessibility of directory.
990 	 */
991 
992 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, cr)) {
993 		ZFS_EXIT(zfsvfs);
994 		return (error);
995 	}
996 
997 	if ((error = zfs_dirlook(zdp, nm, vpp)) == 0) {
998 
999 		/*
1000 		 * Convert device special files
1001 		 */
1002 		if (IS_DEVVP(*vpp)) {
1003 			vnode_t	*svp;
1004 
1005 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1006 			VN_RELE(*vpp);
1007 			if (svp == NULL)
1008 				error = ENOSYS;
1009 			else
1010 				*vpp = svp;
1011 		}
1012 	}
1013 
1014 	ZFS_EXIT(zfsvfs);
1015 	return (error);
1016 }
1017 
1018 /*
1019  * Attempt to create a new entry in a directory.  If the entry
1020  * already exists, truncate the file if permissible, else return
1021  * an error.  Return the vp of the created or trunc'd file.
1022  *
1023  *	IN:	dvp	- vnode of directory to put new file entry in.
1024  *		name	- name of new file entry.
1025  *		vap	- attributes of new file.
1026  *		excl	- flag indicating exclusive or non-exclusive mode.
1027  *		mode	- mode to open file with.
1028  *		cr	- credentials of caller.
1029  *		flag	- large file flag [UNUSED].
1030  *
1031  *	OUT:	vpp	- vnode of created or trunc'd entry.
1032  *
1033  *	RETURN:	0 if success
1034  *		error code if failure
1035  *
1036  * Timestamps:
1037  *	dvp - ctime|mtime updated if new entry created
1038  *	 vp - ctime|mtime always, atime if new
1039  */
1040 /* ARGSUSED */
1041 static int
1042 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1043     int mode, vnode_t **vpp, cred_t *cr, int flag)
1044 {
1045 	znode_t		*zp, *dzp = VTOZ(dvp);
1046 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1047 	zilog_t		*zilog = zfsvfs->z_log;
1048 	objset_t	*os = zfsvfs->z_os;
1049 	zfs_dirlock_t	*dl;
1050 	dmu_tx_t	*tx;
1051 	int		error;
1052 	uint64_t	zoid;
1053 
1054 	ZFS_ENTER(zfsvfs);
1055 
1056 top:
1057 	*vpp = NULL;
1058 
1059 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1060 		vap->va_mode &= ~VSVTX;
1061 
1062 	if (*name == '\0') {
1063 		/*
1064 		 * Null component name refers to the directory itself.
1065 		 */
1066 		VN_HOLD(dvp);
1067 		zp = dzp;
1068 		dl = NULL;
1069 		error = 0;
1070 	} else {
1071 		/* possible VN_HOLD(zp) */
1072 		if (error = zfs_dirent_lock(&dl, dzp, name, &zp, 0)) {
1073 			if (strcmp(name, "..") == 0)
1074 				error = EISDIR;
1075 			ZFS_EXIT(zfsvfs);
1076 			return (error);
1077 		}
1078 	}
1079 
1080 	zoid = zp ? zp->z_id : -1ULL;
1081 
1082 	if (zp == NULL) {
1083 		/*
1084 		 * Create a new file object and update the directory
1085 		 * to reference it.
1086 		 */
1087 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
1088 			goto out;
1089 		}
1090 
1091 		/*
1092 		 * We only support the creation of regular files in
1093 		 * extended attribute directories.
1094 		 */
1095 		if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1096 		    (vap->va_type != VREG)) {
1097 			error = EINVAL;
1098 			goto out;
1099 		}
1100 
1101 		tx = dmu_tx_create(os);
1102 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1103 		dmu_tx_hold_bonus(tx, dzp->z_id);
1104 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1105 		if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
1106 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1107 			    0, SPA_MAXBLOCKSIZE);
1108 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
1109 		if (error) {
1110 			zfs_dirent_unlock(dl);
1111 			if (error == ERESTART &&
1112 			    zfsvfs->z_assign == TXG_NOWAIT) {
1113 				dmu_tx_wait(tx);
1114 				dmu_tx_abort(tx);
1115 				goto top;
1116 			}
1117 			dmu_tx_abort(tx);
1118 			ZFS_EXIT(zfsvfs);
1119 			return (error);
1120 		}
1121 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
1122 		ASSERT(zp->z_id == zoid);
1123 		(void) zfs_link_create(dl, zp, tx, ZNEW);
1124 		zfs_log_create(zilog, tx, TX_CREATE, dzp, zp, name);
1125 		dmu_tx_commit(tx);
1126 	} else {
1127 		/*
1128 		 * A directory entry already exists for this name.
1129 		 */
1130 		/*
1131 		 * Can't truncate an existing file if in exclusive mode.
1132 		 */
1133 		if (excl == EXCL) {
1134 			error = EEXIST;
1135 			goto out;
1136 		}
1137 		/*
1138 		 * Can't open a directory for writing.
1139 		 */
1140 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1141 			error = EISDIR;
1142 			goto out;
1143 		}
1144 		/*
1145 		 * Verify requested access to file.
1146 		 */
1147 		if (mode && (error = zfs_zaccess_rwx(zp, mode, cr))) {
1148 			goto out;
1149 		}
1150 
1151 		mutex_enter(&dzp->z_lock);
1152 		dzp->z_seq++;
1153 		mutex_exit(&dzp->z_lock);
1154 
1155 		/*
1156 		 * Truncate regular files if requested.
1157 		 */
1158 		if ((ZTOV(zp)->v_type == VREG) &&
1159 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1160 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
1161 			if (error == ERESTART &&
1162 			    zfsvfs->z_assign == TXG_NOWAIT) {
1163 				/* NB: we already did dmu_tx_wait() */
1164 				zfs_dirent_unlock(dl);
1165 				VN_RELE(ZTOV(zp));
1166 				goto top;
1167 			}
1168 
1169 			if (error == 0) {
1170 				vnevent_create(ZTOV(zp));
1171 			}
1172 		}
1173 	}
1174 out:
1175 
1176 	if (dl)
1177 		zfs_dirent_unlock(dl);
1178 
1179 	if (error) {
1180 		if (zp)
1181 			VN_RELE(ZTOV(zp));
1182 	} else {
1183 		*vpp = ZTOV(zp);
1184 		/*
1185 		 * If vnode is for a device return a specfs vnode instead.
1186 		 */
1187 		if (IS_DEVVP(*vpp)) {
1188 			struct vnode *svp;
1189 
1190 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1191 			VN_RELE(*vpp);
1192 			if (svp == NULL) {
1193 				error = ENOSYS;
1194 			}
1195 			*vpp = svp;
1196 		}
1197 	}
1198 
1199 	ZFS_EXIT(zfsvfs);
1200 	return (error);
1201 }
1202 
1203 /*
1204  * Remove an entry from a directory.
1205  *
1206  *	IN:	dvp	- vnode of directory to remove entry from.
1207  *		name	- name of entry to remove.
1208  *		cr	- credentials of caller.
1209  *
1210  *	RETURN:	0 if success
1211  *		error code if failure
1212  *
1213  * Timestamps:
1214  *	dvp - ctime|mtime
1215  *	 vp - ctime (if nlink > 0)
1216  */
1217 static int
1218 zfs_remove(vnode_t *dvp, char *name, cred_t *cr)
1219 {
1220 	znode_t		*zp, *dzp = VTOZ(dvp);
1221 	znode_t		*xzp = NULL;
1222 	vnode_t		*vp;
1223 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1224 	zilog_t		*zilog = zfsvfs->z_log;
1225 	uint64_t	acl_obj, xattr_obj;
1226 	zfs_dirlock_t	*dl;
1227 	dmu_tx_t	*tx;
1228 	boolean_t	may_delete_now, delete_now = FALSE;
1229 	boolean_t	unlinked;
1230 	int		error;
1231 
1232 	ZFS_ENTER(zfsvfs);
1233 
1234 top:
1235 	/*
1236 	 * Attempt to lock directory; fail if entry doesn't exist.
1237 	 */
1238 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
1239 		ZFS_EXIT(zfsvfs);
1240 		return (error);
1241 	}
1242 
1243 	vp = ZTOV(zp);
1244 
1245 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1246 		goto out;
1247 	}
1248 
1249 	/*
1250 	 * Need to use rmdir for removing directories.
1251 	 */
1252 	if (vp->v_type == VDIR) {
1253 		error = EPERM;
1254 		goto out;
1255 	}
1256 
1257 	vnevent_remove(vp, dvp, name);
1258 
1259 	dnlc_remove(dvp, name);
1260 
1261 	mutex_enter(&vp->v_lock);
1262 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1263 	mutex_exit(&vp->v_lock);
1264 
1265 	/*
1266 	 * We may delete the znode now, or we may put it in the unlinked set;
1267 	 * it depends on whether we're the last link, and on whether there are
1268 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1269 	 * allow for either case.
1270 	 */
1271 	tx = dmu_tx_create(zfsvfs->z_os);
1272 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1273 	dmu_tx_hold_bonus(tx, zp->z_id);
1274 	if (may_delete_now)
1275 		dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
1276 
1277 	/* are there any extended attributes? */
1278 	if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1279 		/* XXX - do we need this if we are deleting? */
1280 		dmu_tx_hold_bonus(tx, xattr_obj);
1281 	}
1282 
1283 	/* are there any additional acls */
1284 	if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1285 	    may_delete_now)
1286 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1287 
1288 	/* charge as an update -- would be nice not to charge at all */
1289 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1290 
1291 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1292 	if (error) {
1293 		zfs_dirent_unlock(dl);
1294 		VN_RELE(vp);
1295 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1296 			dmu_tx_wait(tx);
1297 			dmu_tx_abort(tx);
1298 			goto top;
1299 		}
1300 		dmu_tx_abort(tx);
1301 		ZFS_EXIT(zfsvfs);
1302 		return (error);
1303 	}
1304 
1305 	/*
1306 	 * Remove the directory entry.
1307 	 */
1308 	error = zfs_link_destroy(dl, zp, tx, 0, &unlinked);
1309 
1310 	if (error) {
1311 		dmu_tx_commit(tx);
1312 		goto out;
1313 	}
1314 
1315 	if (unlinked) {
1316 		mutex_enter(&vp->v_lock);
1317 		delete_now = may_delete_now &&
1318 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1319 		    zp->z_phys->zp_xattr == xattr_obj &&
1320 		    zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1321 		mutex_exit(&vp->v_lock);
1322 	}
1323 
1324 	if (delete_now) {
1325 		if (zp->z_phys->zp_xattr) {
1326 			error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1327 			ASSERT3U(error, ==, 0);
1328 			ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1329 			dmu_buf_will_dirty(xzp->z_dbuf, tx);
1330 			mutex_enter(&xzp->z_lock);
1331 			xzp->z_unlinked = 1;
1332 			xzp->z_phys->zp_links = 0;
1333 			mutex_exit(&xzp->z_lock);
1334 			zfs_unlinked_add(xzp, tx);
1335 			zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1336 		}
1337 		mutex_enter(&zp->z_lock);
1338 		mutex_enter(&vp->v_lock);
1339 		vp->v_count--;
1340 		ASSERT3U(vp->v_count, ==, 0);
1341 		mutex_exit(&vp->v_lock);
1342 		mutex_exit(&zp->z_lock);
1343 		zfs_znode_delete(zp, tx);
1344 		VFS_RELE(zfsvfs->z_vfs);
1345 	} else if (unlinked) {
1346 		zfs_unlinked_add(zp, tx);
1347 	}
1348 
1349 	zfs_log_remove(zilog, tx, TX_REMOVE, dzp, name);
1350 
1351 	dmu_tx_commit(tx);
1352 out:
1353 	zfs_dirent_unlock(dl);
1354 
1355 	if (!delete_now) {
1356 		VN_RELE(vp);
1357 	} else if (xzp) {
1358 		/* this rele delayed to prevent nesting transactions */
1359 		VN_RELE(ZTOV(xzp));
1360 	}
1361 
1362 	ZFS_EXIT(zfsvfs);
1363 	return (error);
1364 }
1365 
1366 /*
1367  * Create a new directory and insert it into dvp using the name
1368  * provided.  Return a pointer to the inserted directory.
1369  *
1370  *	IN:	dvp	- vnode of directory to add subdir to.
1371  *		dirname	- name of new directory.
1372  *		vap	- attributes of new directory.
1373  *		cr	- credentials of caller.
1374  *
1375  *	OUT:	vpp	- vnode of created directory.
1376  *
1377  *	RETURN:	0 if success
1378  *		error code if failure
1379  *
1380  * Timestamps:
1381  *	dvp - ctime|mtime updated
1382  *	 vp - ctime|mtime|atime updated
1383  */
1384 static int
1385 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr)
1386 {
1387 	znode_t		*zp, *dzp = VTOZ(dvp);
1388 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1389 	zilog_t		*zilog = zfsvfs->z_log;
1390 	zfs_dirlock_t	*dl;
1391 	uint64_t	zoid = 0;
1392 	dmu_tx_t	*tx;
1393 	int		error;
1394 
1395 	ASSERT(vap->va_type == VDIR);
1396 
1397 	ZFS_ENTER(zfsvfs);
1398 
1399 	if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1400 		ZFS_EXIT(zfsvfs);
1401 		return (EINVAL);
1402 	}
1403 top:
1404 	*vpp = NULL;
1405 
1406 	/*
1407 	 * First make sure the new directory doesn't exist.
1408 	 */
1409 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, ZNEW)) {
1410 		ZFS_EXIT(zfsvfs);
1411 		return (error);
1412 	}
1413 
1414 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, cr)) {
1415 		zfs_dirent_unlock(dl);
1416 		ZFS_EXIT(zfsvfs);
1417 		return (error);
1418 	}
1419 
1420 	/*
1421 	 * Add a new entry to the directory.
1422 	 */
1423 	tx = dmu_tx_create(zfsvfs->z_os);
1424 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1425 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1426 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
1427 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1428 		    0, SPA_MAXBLOCKSIZE);
1429 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1430 	if (error) {
1431 		zfs_dirent_unlock(dl);
1432 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1433 			dmu_tx_wait(tx);
1434 			dmu_tx_abort(tx);
1435 			goto top;
1436 		}
1437 		dmu_tx_abort(tx);
1438 		ZFS_EXIT(zfsvfs);
1439 		return (error);
1440 	}
1441 
1442 	/*
1443 	 * Create new node.
1444 	 */
1445 	zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
1446 
1447 	/*
1448 	 * Now put new name in parent dir.
1449 	 */
1450 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1451 
1452 	*vpp = ZTOV(zp);
1453 
1454 	zfs_log_create(zilog, tx, TX_MKDIR, dzp, zp, dirname);
1455 	dmu_tx_commit(tx);
1456 
1457 	zfs_dirent_unlock(dl);
1458 
1459 	ZFS_EXIT(zfsvfs);
1460 	return (0);
1461 }
1462 
1463 /*
1464  * Remove a directory subdir entry.  If the current working
1465  * directory is the same as the subdir to be removed, the
1466  * remove will fail.
1467  *
1468  *	IN:	dvp	- vnode of directory to remove from.
1469  *		name	- name of directory to be removed.
1470  *		cwd	- vnode of current working directory.
1471  *		cr	- credentials of caller.
1472  *
1473  *	RETURN:	0 if success
1474  *		error code if failure
1475  *
1476  * Timestamps:
1477  *	dvp - ctime|mtime updated
1478  */
1479 static int
1480 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr)
1481 {
1482 	znode_t		*dzp = VTOZ(dvp);
1483 	znode_t		*zp;
1484 	vnode_t		*vp;
1485 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1486 	zilog_t		*zilog = zfsvfs->z_log;
1487 	zfs_dirlock_t	*dl;
1488 	dmu_tx_t	*tx;
1489 	int		error;
1490 
1491 	ZFS_ENTER(zfsvfs);
1492 
1493 top:
1494 	zp = NULL;
1495 
1496 	/*
1497 	 * Attempt to lock directory; fail if entry doesn't exist.
1498 	 */
1499 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
1500 		ZFS_EXIT(zfsvfs);
1501 		return (error);
1502 	}
1503 
1504 	vp = ZTOV(zp);
1505 
1506 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1507 		goto out;
1508 	}
1509 
1510 	if (vp->v_type != VDIR) {
1511 		error = ENOTDIR;
1512 		goto out;
1513 	}
1514 
1515 	if (vp == cwd) {
1516 		error = EINVAL;
1517 		goto out;
1518 	}
1519 
1520 	vnevent_rmdir(vp, dvp, name);
1521 
1522 	/*
1523 	 * Grab a lock on the directory to make sure that noone is
1524 	 * trying to add (or lookup) entries while we are removing it.
1525 	 */
1526 	rw_enter(&zp->z_name_lock, RW_WRITER);
1527 
1528 	/*
1529 	 * Grab a lock on the parent pointer to make sure we play well
1530 	 * with the treewalk and directory rename code.
1531 	 */
1532 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1533 
1534 	tx = dmu_tx_create(zfsvfs->z_os);
1535 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1536 	dmu_tx_hold_bonus(tx, zp->z_id);
1537 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1538 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1539 	if (error) {
1540 		rw_exit(&zp->z_parent_lock);
1541 		rw_exit(&zp->z_name_lock);
1542 		zfs_dirent_unlock(dl);
1543 		VN_RELE(vp);
1544 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1545 			dmu_tx_wait(tx);
1546 			dmu_tx_abort(tx);
1547 			goto top;
1548 		}
1549 		dmu_tx_abort(tx);
1550 		ZFS_EXIT(zfsvfs);
1551 		return (error);
1552 	}
1553 
1554 	error = zfs_link_destroy(dl, zp, tx, 0, NULL);
1555 
1556 	if (error == 0)
1557 		zfs_log_remove(zilog, tx, TX_RMDIR, dzp, name);
1558 
1559 	dmu_tx_commit(tx);
1560 
1561 	rw_exit(&zp->z_parent_lock);
1562 	rw_exit(&zp->z_name_lock);
1563 out:
1564 	zfs_dirent_unlock(dl);
1565 
1566 	VN_RELE(vp);
1567 
1568 	ZFS_EXIT(zfsvfs);
1569 	return (error);
1570 }
1571 
1572 /*
1573  * Read as many directory entries as will fit into the provided
1574  * buffer from the given directory cursor position (specified in
1575  * the uio structure.
1576  *
1577  *	IN:	vp	- vnode of directory to read.
1578  *		uio	- structure supplying read location, range info,
1579  *			  and return buffer.
1580  *		cr	- credentials of caller.
1581  *
1582  *	OUT:	uio	- updated offset and range, buffer filled.
1583  *		eofp	- set to true if end-of-file detected.
1584  *
1585  *	RETURN:	0 if success
1586  *		error code if failure
1587  *
1588  * Timestamps:
1589  *	vp - atime updated
1590  *
1591  * Note that the low 4 bits of the cookie returned by zap is always zero.
1592  * This allows us to use the low range for "special" directory entries:
1593  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
1594  * we use the offset 2 for the '.zfs' directory.
1595  */
1596 /* ARGSUSED */
1597 static int
1598 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp)
1599 {
1600 	znode_t		*zp = VTOZ(vp);
1601 	iovec_t		*iovp;
1602 	dirent64_t	*odp;
1603 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1604 	objset_t	*os;
1605 	caddr_t		outbuf;
1606 	size_t		bufsize;
1607 	zap_cursor_t	zc;
1608 	zap_attribute_t	zap;
1609 	uint_t		bytes_wanted;
1610 	uint64_t	offset; /* must be unsigned; checks for < 1 */
1611 	int		local_eof;
1612 	int		outcount;
1613 	int		error;
1614 	uint8_t		prefetch;
1615 
1616 	ZFS_ENTER(zfsvfs);
1617 
1618 	/*
1619 	 * If we are not given an eof variable,
1620 	 * use a local one.
1621 	 */
1622 	if (eofp == NULL)
1623 		eofp = &local_eof;
1624 
1625 	/*
1626 	 * Check for valid iov_len.
1627 	 */
1628 	if (uio->uio_iov->iov_len <= 0) {
1629 		ZFS_EXIT(zfsvfs);
1630 		return (EINVAL);
1631 	}
1632 
1633 	/*
1634 	 * Quit if directory has been removed (posix)
1635 	 */
1636 	if ((*eofp = zp->z_unlinked) != 0) {
1637 		ZFS_EXIT(zfsvfs);
1638 		return (0);
1639 	}
1640 
1641 	error = 0;
1642 	os = zfsvfs->z_os;
1643 	offset = uio->uio_loffset;
1644 	prefetch = zp->z_zn_prefetch;
1645 
1646 	/*
1647 	 * Initialize the iterator cursor.
1648 	 */
1649 	if (offset <= 3) {
1650 		/*
1651 		 * Start iteration from the beginning of the directory.
1652 		 */
1653 		zap_cursor_init(&zc, os, zp->z_id);
1654 	} else {
1655 		/*
1656 		 * The offset is a serialized cursor.
1657 		 */
1658 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1659 	}
1660 
1661 	/*
1662 	 * Get space to change directory entries into fs independent format.
1663 	 */
1664 	iovp = uio->uio_iov;
1665 	bytes_wanted = iovp->iov_len;
1666 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
1667 		bufsize = bytes_wanted;
1668 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
1669 		odp = (struct dirent64 *)outbuf;
1670 	} else {
1671 		bufsize = bytes_wanted;
1672 		odp = (struct dirent64 *)iovp->iov_base;
1673 	}
1674 
1675 	/*
1676 	 * Transform to file-system independent format
1677 	 */
1678 	outcount = 0;
1679 	while (outcount < bytes_wanted) {
1680 		ino64_t objnum;
1681 		ushort_t reclen;
1682 		off64_t *next;
1683 
1684 		/*
1685 		 * Special case `.', `..', and `.zfs'.
1686 		 */
1687 		if (offset == 0) {
1688 			(void) strcpy(zap.za_name, ".");
1689 			objnum = zp->z_id;
1690 		} else if (offset == 1) {
1691 			(void) strcpy(zap.za_name, "..");
1692 			objnum = zp->z_phys->zp_parent;
1693 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
1694 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
1695 			objnum = ZFSCTL_INO_ROOT;
1696 		} else {
1697 			/*
1698 			 * Grab next entry.
1699 			 */
1700 			if (error = zap_cursor_retrieve(&zc, &zap)) {
1701 				if ((*eofp = (error == ENOENT)) != 0)
1702 					break;
1703 				else
1704 					goto update;
1705 			}
1706 
1707 			if (zap.za_integer_length != 8 ||
1708 			    zap.za_num_integers != 1) {
1709 				cmn_err(CE_WARN, "zap_readdir: bad directory "
1710 				    "entry, obj = %lld, offset = %lld\n",
1711 				    (u_longlong_t)zp->z_id,
1712 				    (u_longlong_t)offset);
1713 				error = ENXIO;
1714 				goto update;
1715 			}
1716 
1717 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
1718 			/*
1719 			 * MacOS X can extract the object type here such as:
1720 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
1721 			 */
1722 		}
1723 		reclen = DIRENT64_RECLEN(strlen(zap.za_name));
1724 
1725 		/*
1726 		 * Will this entry fit in the buffer?
1727 		 */
1728 		if (outcount + reclen > bufsize) {
1729 			/*
1730 			 * Did we manage to fit anything in the buffer?
1731 			 */
1732 			if (!outcount) {
1733 				error = EINVAL;
1734 				goto update;
1735 			}
1736 			break;
1737 		}
1738 		/*
1739 		 * Add this entry:
1740 		 */
1741 		odp->d_ino = objnum;
1742 		odp->d_reclen = reclen;
1743 		/* NOTE: d_off is the offset for the *next* entry */
1744 		next = &(odp->d_off);
1745 		(void) strncpy(odp->d_name, zap.za_name,
1746 		    DIRENT64_NAMELEN(reclen));
1747 		outcount += reclen;
1748 		odp = (dirent64_t *)((intptr_t)odp + reclen);
1749 
1750 		ASSERT(outcount <= bufsize);
1751 
1752 		/* Prefetch znode */
1753 		if (prefetch)
1754 			dmu_prefetch(os, objnum, 0, 0);
1755 
1756 		/*
1757 		 * Move to the next entry, fill in the previous offset.
1758 		 */
1759 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
1760 			zap_cursor_advance(&zc);
1761 			offset = zap_cursor_serialize(&zc);
1762 		} else {
1763 			offset += 1;
1764 		}
1765 		*next = offset;
1766 	}
1767 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
1768 
1769 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
1770 		iovp->iov_base += outcount;
1771 		iovp->iov_len -= outcount;
1772 		uio->uio_resid -= outcount;
1773 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
1774 		/*
1775 		 * Reset the pointer.
1776 		 */
1777 		offset = uio->uio_loffset;
1778 	}
1779 
1780 update:
1781 	zap_cursor_fini(&zc);
1782 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1783 		kmem_free(outbuf, bufsize);
1784 
1785 	if (error == ENOENT)
1786 		error = 0;
1787 
1788 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
1789 
1790 	uio->uio_loffset = offset;
1791 	ZFS_EXIT(zfsvfs);
1792 	return (error);
1793 }
1794 
1795 ulong_t zfs_fsync_sync_cnt = 4;
1796 
1797 static int
1798 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr)
1799 {
1800 	znode_t	*zp = VTOZ(vp);
1801 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1802 
1803 	/*
1804 	 * Regardless of whether this is required for standards conformance,
1805 	 * this is the logical behavior when fsync() is called on a file with
1806 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
1807 	 * going to be pushed out as part of the zil_commit().
1808 	 */
1809 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
1810 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
1811 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr);
1812 
1813 	(void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
1814 
1815 	ZFS_ENTER(zfsvfs);
1816 	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
1817 	ZFS_EXIT(zfsvfs);
1818 	return (0);
1819 }
1820 
1821 /*
1822  * Get the requested file attributes and place them in the provided
1823  * vattr structure.
1824  *
1825  *	IN:	vp	- vnode of file.
1826  *		vap	- va_mask identifies requested attributes.
1827  *		flags	- [UNUSED]
1828  *		cr	- credentials of caller.
1829  *
1830  *	OUT:	vap	- attribute values.
1831  *
1832  *	RETURN:	0 (always succeeds)
1833  */
1834 /* ARGSUSED */
1835 static int
1836 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
1837 {
1838 	znode_t *zp = VTOZ(vp);
1839 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1840 	znode_phys_t *pzp = zp->z_phys;
1841 	int	error;
1842 	uint64_t links;
1843 
1844 	ZFS_ENTER(zfsvfs);
1845 
1846 	/*
1847 	 * Return all attributes.  It's cheaper to provide the answer
1848 	 * than to determine whether we were asked the question.
1849 	 */
1850 	mutex_enter(&zp->z_lock);
1851 
1852 	vap->va_type = vp->v_type;
1853 	vap->va_mode = pzp->zp_mode & MODEMASK;
1854 	vap->va_uid = zp->z_phys->zp_uid;
1855 	vap->va_gid = zp->z_phys->zp_gid;
1856 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
1857 	vap->va_nodeid = zp->z_id;
1858 	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
1859 		links = pzp->zp_links + 1;
1860 	else
1861 		links = pzp->zp_links;
1862 	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
1863 	vap->va_size = pzp->zp_size;
1864 	vap->va_rdev = vp->v_rdev;
1865 	vap->va_seq = zp->z_seq;
1866 
1867 	ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
1868 	ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
1869 	ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
1870 
1871 	/*
1872 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
1873 	 * Also, if we are the owner don't bother, since owner should
1874 	 * always be allowed to read basic attributes of file.
1875 	 */
1876 	if (!(zp->z_phys->zp_flags & ZFS_ACL_TRIVIAL) &&
1877 	    (zp->z_phys->zp_uid != crgetuid(cr))) {
1878 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, cr)) {
1879 			mutex_exit(&zp->z_lock);
1880 			ZFS_EXIT(zfsvfs);
1881 			return (error);
1882 		}
1883 	}
1884 
1885 	mutex_exit(&zp->z_lock);
1886 
1887 	dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks);
1888 
1889 	if (zp->z_blksz == 0) {
1890 		/*
1891 		 * Block size hasn't been set; suggest maximal I/O transfers.
1892 		 */
1893 		vap->va_blksize = zfsvfs->z_max_blksz;
1894 	}
1895 
1896 	ZFS_EXIT(zfsvfs);
1897 	return (0);
1898 }
1899 
1900 /*
1901  * Set the file attributes to the values contained in the
1902  * vattr structure.
1903  *
1904  *	IN:	vp	- vnode of file to be modified.
1905  *		vap	- new attribute values.
1906  *		flags	- ATTR_UTIME set if non-default time values provided.
1907  *		cr	- credentials of caller.
1908  *
1909  *	RETURN:	0 if success
1910  *		error code if failure
1911  *
1912  * Timestamps:
1913  *	vp - ctime updated, mtime updated if size changed.
1914  */
1915 /* ARGSUSED */
1916 static int
1917 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1918 	caller_context_t *ct)
1919 {
1920 	struct znode	*zp = VTOZ(vp);
1921 	znode_phys_t	*pzp = zp->z_phys;
1922 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1923 	zilog_t		*zilog = zfsvfs->z_log;
1924 	dmu_tx_t	*tx;
1925 	vattr_t		oldva;
1926 	uint_t		mask = vap->va_mask;
1927 	uint_t		saved_mask;
1928 	int		trim_mask = 0;
1929 	uint64_t	new_mode;
1930 	znode_t		*attrzp;
1931 	int		need_policy = FALSE;
1932 	int		err;
1933 
1934 	if (mask == 0)
1935 		return (0);
1936 
1937 	if (mask & AT_NOSET)
1938 		return (EINVAL);
1939 
1940 	if (mask & AT_SIZE && vp->v_type == VDIR)
1941 		return (EISDIR);
1942 
1943 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO)
1944 		return (EINVAL);
1945 
1946 	ZFS_ENTER(zfsvfs);
1947 
1948 top:
1949 	attrzp = NULL;
1950 
1951 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
1952 		ZFS_EXIT(zfsvfs);
1953 		return (EROFS);
1954 	}
1955 
1956 	/*
1957 	 * First validate permissions
1958 	 */
1959 
1960 	if (mask & AT_SIZE) {
1961 		err = zfs_zaccess(zp, ACE_WRITE_DATA, cr);
1962 		if (err) {
1963 			ZFS_EXIT(zfsvfs);
1964 			return (err);
1965 		}
1966 		/*
1967 		 * XXX - Note, we are not providing any open
1968 		 * mode flags here (like FNDELAY), so we may
1969 		 * block if there are locks present... this
1970 		 * should be addressed in openat().
1971 		 */
1972 		do {
1973 			err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
1974 			/* NB: we already did dmu_tx_wait() if necessary */
1975 		} while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
1976 		if (err) {
1977 			ZFS_EXIT(zfsvfs);
1978 			return (err);
1979 		}
1980 	}
1981 
1982 	if (mask & (AT_ATIME|AT_MTIME))
1983 		need_policy = zfs_zaccess_v4_perm(zp, ACE_WRITE_ATTRIBUTES, cr);
1984 
1985 	if (mask & (AT_UID|AT_GID)) {
1986 		int	idmask = (mask & (AT_UID|AT_GID));
1987 		int	take_owner;
1988 		int	take_group;
1989 
1990 		/*
1991 		 * NOTE: even if a new mode is being set,
1992 		 * we may clear S_ISUID/S_ISGID bits.
1993 		 */
1994 
1995 		if (!(mask & AT_MODE))
1996 			vap->va_mode = pzp->zp_mode;
1997 
1998 		/*
1999 		 * Take ownership or chgrp to group we are a member of
2000 		 */
2001 
2002 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2003 		take_group = (mask & AT_GID) && groupmember(vap->va_gid, cr);
2004 
2005 		/*
2006 		 * If both AT_UID and AT_GID are set then take_owner and
2007 		 * take_group must both be set in order to allow taking
2008 		 * ownership.
2009 		 *
2010 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2011 		 *
2012 		 */
2013 
2014 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2015 		    ((idmask == AT_UID) && take_owner) ||
2016 		    ((idmask == AT_GID) && take_group)) {
2017 			if (zfs_zaccess_v4_perm(zp, ACE_WRITE_OWNER, cr) == 0) {
2018 				/*
2019 				 * Remove setuid/setgid for non-privileged users
2020 				 */
2021 				secpolicy_setid_clear(vap, cr);
2022 				trim_mask = (mask & (AT_UID|AT_GID));
2023 			} else {
2024 				need_policy =  TRUE;
2025 			}
2026 		} else {
2027 			need_policy =  TRUE;
2028 		}
2029 	}
2030 
2031 	mutex_enter(&zp->z_lock);
2032 	oldva.va_mode = pzp->zp_mode;
2033 	oldva.va_uid = zp->z_phys->zp_uid;
2034 	oldva.va_gid = zp->z_phys->zp_gid;
2035 	mutex_exit(&zp->z_lock);
2036 
2037 	if (mask & AT_MODE) {
2038 		if (zfs_zaccess_v4_perm(zp, ACE_WRITE_ACL, cr) == 0) {
2039 			err = secpolicy_setid_setsticky_clear(vp, vap,
2040 			    &oldva, cr);
2041 			if (err) {
2042 				ZFS_EXIT(zfsvfs);
2043 				return (err);
2044 			}
2045 			trim_mask |= AT_MODE;
2046 		} else {
2047 			need_policy = TRUE;
2048 		}
2049 	}
2050 
2051 	if (need_policy) {
2052 		/*
2053 		 * If trim_mask is set then take ownership
2054 		 * has been granted or write_acl is present and user
2055 		 * has the ability to modify mode.  In that case remove
2056 		 * UID|GID and or MODE from mask so that
2057 		 * secpolicy_vnode_setattr() doesn't revoke it.
2058 		 */
2059 
2060 		if (trim_mask) {
2061 			saved_mask = vap->va_mask;
2062 			vap->va_mask &= ~trim_mask;
2063 
2064 		}
2065 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2066 		    (int (*)(void *, int, cred_t *))zfs_zaccess_rwx, zp);
2067 		if (err) {
2068 			ZFS_EXIT(zfsvfs);
2069 			return (err);
2070 		}
2071 
2072 		if (trim_mask)
2073 			vap->va_mask |= saved_mask;
2074 	}
2075 
2076 	/*
2077 	 * secpolicy_vnode_setattr, or take ownership may have
2078 	 * changed va_mask
2079 	 */
2080 	mask = vap->va_mask;
2081 
2082 	tx = dmu_tx_create(zfsvfs->z_os);
2083 	dmu_tx_hold_bonus(tx, zp->z_id);
2084 
2085 	if (mask & AT_MODE) {
2086 		uint64_t pmode = pzp->zp_mode;
2087 
2088 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2089 
2090 		if (zp->z_phys->zp_acl.z_acl_extern_obj)
2091 			dmu_tx_hold_write(tx,
2092 			    pzp->zp_acl.z_acl_extern_obj, 0, SPA_MAXBLOCKSIZE);
2093 		else
2094 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2095 			    0, ZFS_ACL_SIZE(MAX_ACL_SIZE));
2096 	}
2097 
2098 	if ((mask & (AT_UID | AT_GID)) && zp->z_phys->zp_xattr != 0) {
2099 		err = zfs_zget(zp->z_zfsvfs, zp->z_phys->zp_xattr, &attrzp);
2100 		if (err) {
2101 			dmu_tx_abort(tx);
2102 			ZFS_EXIT(zfsvfs);
2103 			return (err);
2104 		}
2105 		dmu_tx_hold_bonus(tx, attrzp->z_id);
2106 	}
2107 
2108 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2109 	if (err) {
2110 		if (attrzp)
2111 			VN_RELE(ZTOV(attrzp));
2112 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2113 			dmu_tx_wait(tx);
2114 			dmu_tx_abort(tx);
2115 			goto top;
2116 		}
2117 		dmu_tx_abort(tx);
2118 		ZFS_EXIT(zfsvfs);
2119 		return (err);
2120 	}
2121 
2122 	dmu_buf_will_dirty(zp->z_dbuf, tx);
2123 
2124 	/*
2125 	 * Set each attribute requested.
2126 	 * We group settings according to the locks they need to acquire.
2127 	 *
2128 	 * Note: you cannot set ctime directly, although it will be
2129 	 * updated as a side-effect of calling this function.
2130 	 */
2131 
2132 	mutex_enter(&zp->z_lock);
2133 
2134 	if (mask & AT_MODE) {
2135 		err = zfs_acl_chmod_setattr(zp, new_mode, tx);
2136 		ASSERT3U(err, ==, 0);
2137 	}
2138 
2139 	if (attrzp)
2140 		mutex_enter(&attrzp->z_lock);
2141 
2142 	if (mask & AT_UID) {
2143 		zp->z_phys->zp_uid = (uint64_t)vap->va_uid;
2144 		if (attrzp) {
2145 			attrzp->z_phys->zp_uid = (uint64_t)vap->va_uid;
2146 		}
2147 	}
2148 
2149 	if (mask & AT_GID) {
2150 		zp->z_phys->zp_gid = (uint64_t)vap->va_gid;
2151 		if (attrzp)
2152 			attrzp->z_phys->zp_gid = (uint64_t)vap->va_gid;
2153 	}
2154 
2155 	if (attrzp)
2156 		mutex_exit(&attrzp->z_lock);
2157 
2158 	if (mask & AT_ATIME)
2159 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
2160 
2161 	if (mask & AT_MTIME)
2162 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
2163 
2164 	if (mask & AT_SIZE)
2165 		zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
2166 	else if (mask != 0)
2167 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
2168 
2169 	if (mask != 0)
2170 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask);
2171 
2172 	mutex_exit(&zp->z_lock);
2173 
2174 	if (attrzp)
2175 		VN_RELE(ZTOV(attrzp));
2176 
2177 	dmu_tx_commit(tx);
2178 
2179 	ZFS_EXIT(zfsvfs);
2180 	return (err);
2181 }
2182 
2183 typedef struct zfs_zlock {
2184 	krwlock_t	*zl_rwlock;	/* lock we acquired */
2185 	znode_t		*zl_znode;	/* znode we held */
2186 	struct zfs_zlock *zl_next;	/* next in list */
2187 } zfs_zlock_t;
2188 
2189 /*
2190  * Drop locks and release vnodes that were held by zfs_rename_lock().
2191  */
2192 static void
2193 zfs_rename_unlock(zfs_zlock_t **zlpp)
2194 {
2195 	zfs_zlock_t *zl;
2196 
2197 	while ((zl = *zlpp) != NULL) {
2198 		if (zl->zl_znode != NULL)
2199 			VN_RELE(ZTOV(zl->zl_znode));
2200 		rw_exit(zl->zl_rwlock);
2201 		*zlpp = zl->zl_next;
2202 		kmem_free(zl, sizeof (*zl));
2203 	}
2204 }
2205 
2206 /*
2207  * Search back through the directory tree, using the ".." entries.
2208  * Lock each directory in the chain to prevent concurrent renames.
2209  * Fail any attempt to move a directory into one of its own descendants.
2210  * XXX - z_parent_lock can overlap with map or grow locks
2211  */
2212 static int
2213 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
2214 {
2215 	zfs_zlock_t	*zl;
2216 	znode_t		*zp = tdzp;
2217 	uint64_t	rootid = zp->z_zfsvfs->z_root;
2218 	uint64_t	*oidp = &zp->z_id;
2219 	krwlock_t	*rwlp = &szp->z_parent_lock;
2220 	krw_t		rw = RW_WRITER;
2221 
2222 	/*
2223 	 * First pass write-locks szp and compares to zp->z_id.
2224 	 * Later passes read-lock zp and compare to zp->z_parent.
2225 	 */
2226 	do {
2227 		if (!rw_tryenter(rwlp, rw)) {
2228 			/*
2229 			 * Another thread is renaming in this path.
2230 			 * Note that if we are a WRITER, we don't have any
2231 			 * parent_locks held yet.
2232 			 */
2233 			if (rw == RW_READER && zp->z_id > szp->z_id) {
2234 				/*
2235 				 * Drop our locks and restart
2236 				 */
2237 				zfs_rename_unlock(&zl);
2238 				*zlpp = NULL;
2239 				zp = tdzp;
2240 				oidp = &zp->z_id;
2241 				rwlp = &szp->z_parent_lock;
2242 				rw = RW_WRITER;
2243 				continue;
2244 			} else {
2245 				/*
2246 				 * Wait for other thread to drop its locks
2247 				 */
2248 				rw_enter(rwlp, rw);
2249 			}
2250 		}
2251 
2252 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
2253 		zl->zl_rwlock = rwlp;
2254 		zl->zl_znode = NULL;
2255 		zl->zl_next = *zlpp;
2256 		*zlpp = zl;
2257 
2258 		if (*oidp == szp->z_id)		/* We're a descendant of szp */
2259 			return (EINVAL);
2260 
2261 		if (*oidp == rootid)		/* We've hit the top */
2262 			return (0);
2263 
2264 		if (rw == RW_READER) {		/* i.e. not the first pass */
2265 			int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
2266 			if (error)
2267 				return (error);
2268 			zl->zl_znode = zp;
2269 		}
2270 		oidp = &zp->z_phys->zp_parent;
2271 		rwlp = &zp->z_parent_lock;
2272 		rw = RW_READER;
2273 
2274 	} while (zp->z_id != sdzp->z_id);
2275 
2276 	return (0);
2277 }
2278 
2279 /*
2280  * Move an entry from the provided source directory to the target
2281  * directory.  Change the entry name as indicated.
2282  *
2283  *	IN:	sdvp	- Source directory containing the "old entry".
2284  *		snm	- Old entry name.
2285  *		tdvp	- Target directory to contain the "new entry".
2286  *		tnm	- New entry name.
2287  *		cr	- credentials of caller.
2288  *
2289  *	RETURN:	0 if success
2290  *		error code if failure
2291  *
2292  * Timestamps:
2293  *	sdvp,tdvp - ctime|mtime updated
2294  */
2295 static int
2296 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr)
2297 {
2298 	znode_t		*tdzp, *szp, *tzp;
2299 	znode_t		*sdzp = VTOZ(sdvp);
2300 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
2301 	zilog_t		*zilog = zfsvfs->z_log;
2302 	vnode_t		*realvp;
2303 	zfs_dirlock_t	*sdl, *tdl;
2304 	dmu_tx_t	*tx;
2305 	zfs_zlock_t	*zl;
2306 	int		cmp, serr, terr, error;
2307 
2308 	ZFS_ENTER(zfsvfs);
2309 
2310 	/*
2311 	 * Make sure we have the real vp for the target directory.
2312 	 */
2313 	if (VOP_REALVP(tdvp, &realvp) == 0)
2314 		tdvp = realvp;
2315 
2316 	if (tdvp->v_vfsp != sdvp->v_vfsp) {
2317 		ZFS_EXIT(zfsvfs);
2318 		return (EXDEV);
2319 	}
2320 
2321 	tdzp = VTOZ(tdvp);
2322 top:
2323 	szp = NULL;
2324 	tzp = NULL;
2325 	zl = NULL;
2326 
2327 	/*
2328 	 * This is to prevent the creation of links into attribute space
2329 	 * by renaming a linked file into/outof an attribute directory.
2330 	 * See the comment in zfs_link() for why this is considered bad.
2331 	 */
2332 	if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
2333 	    (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
2334 		ZFS_EXIT(zfsvfs);
2335 		return (EINVAL);
2336 	}
2337 
2338 	/*
2339 	 * Lock source and target directory entries.  To prevent deadlock,
2340 	 * a lock ordering must be defined.  We lock the directory with
2341 	 * the smallest object id first, or if it's a tie, the one with
2342 	 * the lexically first name.
2343 	 */
2344 	if (sdzp->z_id < tdzp->z_id) {
2345 		cmp = -1;
2346 	} else if (sdzp->z_id > tdzp->z_id) {
2347 		cmp = 1;
2348 	} else {
2349 		cmp = strcmp(snm, tnm);
2350 		if (cmp == 0) {
2351 			/*
2352 			 * POSIX: "If the old argument and the new argument
2353 			 * both refer to links to the same existing file,
2354 			 * the rename() function shall return successfully
2355 			 * and perform no other action."
2356 			 */
2357 			ZFS_EXIT(zfsvfs);
2358 			return (0);
2359 		}
2360 	}
2361 	if (cmp < 0) {
2362 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
2363 		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
2364 	} else {
2365 		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
2366 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
2367 	}
2368 
2369 	if (serr) {
2370 		/*
2371 		 * Source entry invalid or not there.
2372 		 */
2373 		if (!terr) {
2374 			zfs_dirent_unlock(tdl);
2375 			if (tzp)
2376 				VN_RELE(ZTOV(tzp));
2377 		}
2378 		if (strcmp(snm, "..") == 0)
2379 			serr = EINVAL;
2380 		ZFS_EXIT(zfsvfs);
2381 		return (serr);
2382 	}
2383 	if (terr) {
2384 		zfs_dirent_unlock(sdl);
2385 		VN_RELE(ZTOV(szp));
2386 		if (strcmp(tnm, "..") == 0)
2387 			terr = EINVAL;
2388 		ZFS_EXIT(zfsvfs);
2389 		return (terr);
2390 	}
2391 
2392 	/*
2393 	 * Must have write access at the source to remove the old entry
2394 	 * and write access at the target to create the new entry.
2395 	 * Note that if target and source are the same, this can be
2396 	 * done in a single check.
2397 	 */
2398 
2399 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
2400 		goto out;
2401 
2402 	if (ZTOV(szp)->v_type == VDIR) {
2403 		/*
2404 		 * Check to make sure rename is valid.
2405 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
2406 		 */
2407 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
2408 			goto out;
2409 	}
2410 
2411 	/*
2412 	 * Does target exist?
2413 	 */
2414 	if (tzp) {
2415 		/*
2416 		 * Source and target must be the same type.
2417 		 */
2418 		if (ZTOV(szp)->v_type == VDIR) {
2419 			if (ZTOV(tzp)->v_type != VDIR) {
2420 				error = ENOTDIR;
2421 				goto out;
2422 			}
2423 		} else {
2424 			if (ZTOV(tzp)->v_type == VDIR) {
2425 				error = EISDIR;
2426 				goto out;
2427 			}
2428 		}
2429 		/*
2430 		 * POSIX dictates that when the source and target
2431 		 * entries refer to the same file object, rename
2432 		 * must do nothing and exit without error.
2433 		 */
2434 		if (szp->z_id == tzp->z_id) {
2435 			error = 0;
2436 			goto out;
2437 		}
2438 	}
2439 
2440 	vnevent_rename_src(ZTOV(szp), sdvp, snm);
2441 	if (tzp)
2442 		vnevent_rename_dest(ZTOV(tzp), tdvp, tnm);
2443 
2444 	/*
2445 	 * notify the target directory if it is not the same
2446 	 * as source directory.
2447 	 */
2448 	if (tdvp != sdvp) {
2449 		vnevent_rename_dest_dir(tdvp);
2450 	}
2451 
2452 	tx = dmu_tx_create(zfsvfs->z_os);
2453 	dmu_tx_hold_bonus(tx, szp->z_id);	/* nlink changes */
2454 	dmu_tx_hold_bonus(tx, sdzp->z_id);	/* nlink changes */
2455 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
2456 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
2457 	if (sdzp != tdzp)
2458 		dmu_tx_hold_bonus(tx, tdzp->z_id);	/* nlink changes */
2459 	if (tzp)
2460 		dmu_tx_hold_bonus(tx, tzp->z_id);	/* parent changes */
2461 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2462 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2463 	if (error) {
2464 		if (zl != NULL)
2465 			zfs_rename_unlock(&zl);
2466 		zfs_dirent_unlock(sdl);
2467 		zfs_dirent_unlock(tdl);
2468 		VN_RELE(ZTOV(szp));
2469 		if (tzp)
2470 			VN_RELE(ZTOV(tzp));
2471 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2472 			dmu_tx_wait(tx);
2473 			dmu_tx_abort(tx);
2474 			goto top;
2475 		}
2476 		dmu_tx_abort(tx);
2477 		ZFS_EXIT(zfsvfs);
2478 		return (error);
2479 	}
2480 
2481 	if (tzp)	/* Attempt to remove the existing target */
2482 		error = zfs_link_destroy(tdl, tzp, tx, 0, NULL);
2483 
2484 	if (error == 0) {
2485 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
2486 		if (error == 0) {
2487 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
2488 			ASSERT(error == 0);
2489 			zfs_log_rename(zilog, tx, TX_RENAME, sdzp,
2490 			    sdl->dl_name, tdzp, tdl->dl_name, szp);
2491 		}
2492 	}
2493 
2494 	dmu_tx_commit(tx);
2495 out:
2496 	if (zl != NULL)
2497 		zfs_rename_unlock(&zl);
2498 
2499 	zfs_dirent_unlock(sdl);
2500 	zfs_dirent_unlock(tdl);
2501 
2502 	VN_RELE(ZTOV(szp));
2503 	if (tzp)
2504 		VN_RELE(ZTOV(tzp));
2505 
2506 	ZFS_EXIT(zfsvfs);
2507 	return (error);
2508 }
2509 
2510 /*
2511  * Insert the indicated symbolic reference entry into the directory.
2512  *
2513  *	IN:	dvp	- Directory to contain new symbolic link.
2514  *		link	- Name for new symlink entry.
2515  *		vap	- Attributes of new entry.
2516  *		target	- Target path of new symlink.
2517  *		cr	- credentials of caller.
2518  *
2519  *	RETURN:	0 if success
2520  *		error code if failure
2521  *
2522  * Timestamps:
2523  *	dvp - ctime|mtime updated
2524  */
2525 static int
2526 zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr)
2527 {
2528 	znode_t		*zp, *dzp = VTOZ(dvp);
2529 	zfs_dirlock_t	*dl;
2530 	dmu_tx_t	*tx;
2531 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2532 	zilog_t		*zilog = zfsvfs->z_log;
2533 	uint64_t	zoid;
2534 	int		len = strlen(link);
2535 	int		error;
2536 
2537 	ASSERT(vap->va_type == VLNK);
2538 
2539 	ZFS_ENTER(zfsvfs);
2540 top:
2541 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
2542 		ZFS_EXIT(zfsvfs);
2543 		return (error);
2544 	}
2545 
2546 	if (len > MAXPATHLEN) {
2547 		ZFS_EXIT(zfsvfs);
2548 		return (ENAMETOOLONG);
2549 	}
2550 
2551 	/*
2552 	 * Attempt to lock directory; fail if entry already exists.
2553 	 */
2554 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZNEW)) {
2555 		ZFS_EXIT(zfsvfs);
2556 		return (error);
2557 	}
2558 
2559 	tx = dmu_tx_create(zfsvfs->z_os);
2560 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
2561 	dmu_tx_hold_bonus(tx, dzp->z_id);
2562 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
2563 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
2564 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
2565 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2566 	if (error) {
2567 		zfs_dirent_unlock(dl);
2568 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2569 			dmu_tx_wait(tx);
2570 			dmu_tx_abort(tx);
2571 			goto top;
2572 		}
2573 		dmu_tx_abort(tx);
2574 		ZFS_EXIT(zfsvfs);
2575 		return (error);
2576 	}
2577 
2578 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
2579 
2580 	/*
2581 	 * Create a new object for the symlink.
2582 	 * Put the link content into bonus buffer if it will fit;
2583 	 * otherwise, store it just like any other file data.
2584 	 */
2585 	zoid = 0;
2586 	if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
2587 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, len);
2588 		if (len != 0)
2589 			bcopy(link, zp->z_phys + 1, len);
2590 	} else {
2591 		dmu_buf_t *dbp;
2592 
2593 		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
2594 
2595 		/*
2596 		 * Nothing can access the znode yet so no locking needed
2597 		 * for growing the znode's blocksize.
2598 		 */
2599 		zfs_grow_blocksize(zp, len, tx);
2600 
2601 		VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, zoid, 0, FTAG, &dbp));
2602 		dmu_buf_will_dirty(dbp, tx);
2603 
2604 		ASSERT3U(len, <=, dbp->db_size);
2605 		bcopy(link, dbp->db_data, len);
2606 		dmu_buf_rele(dbp, FTAG);
2607 	}
2608 	zp->z_phys->zp_size = len;
2609 
2610 	/*
2611 	 * Insert the new object into the directory.
2612 	 */
2613 	(void) zfs_link_create(dl, zp, tx, ZNEW);
2614 out:
2615 	if (error == 0)
2616 		zfs_log_symlink(zilog, tx, TX_SYMLINK, dzp, zp, name, link);
2617 
2618 	dmu_tx_commit(tx);
2619 
2620 	zfs_dirent_unlock(dl);
2621 
2622 	VN_RELE(ZTOV(zp));
2623 
2624 	ZFS_EXIT(zfsvfs);
2625 	return (error);
2626 }
2627 
2628 /*
2629  * Return, in the buffer contained in the provided uio structure,
2630  * the symbolic path referred to by vp.
2631  *
2632  *	IN:	vp	- vnode of symbolic link.
2633  *		uoip	- structure to contain the link path.
2634  *		cr	- credentials of caller.
2635  *
2636  *	OUT:	uio	- structure to contain the link path.
2637  *
2638  *	RETURN:	0 if success
2639  *		error code if failure
2640  *
2641  * Timestamps:
2642  *	vp - atime updated
2643  */
2644 /* ARGSUSED */
2645 static int
2646 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr)
2647 {
2648 	znode_t		*zp = VTOZ(vp);
2649 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2650 	size_t		bufsz;
2651 	int		error;
2652 
2653 	ZFS_ENTER(zfsvfs);
2654 
2655 	bufsz = (size_t)zp->z_phys->zp_size;
2656 	if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
2657 		error = uiomove(zp->z_phys + 1,
2658 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
2659 	} else {
2660 		dmu_buf_t *dbp;
2661 		error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
2662 		if (error) {
2663 			ZFS_EXIT(zfsvfs);
2664 			return (error);
2665 		}
2666 		error = uiomove(dbp->db_data,
2667 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
2668 		dmu_buf_rele(dbp, FTAG);
2669 	}
2670 
2671 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2672 	ZFS_EXIT(zfsvfs);
2673 	return (error);
2674 }
2675 
2676 /*
2677  * Insert a new entry into directory tdvp referencing svp.
2678  *
2679  *	IN:	tdvp	- Directory to contain new entry.
2680  *		svp	- vnode of new entry.
2681  *		name	- name of new entry.
2682  *		cr	- credentials of caller.
2683  *
2684  *	RETURN:	0 if success
2685  *		error code if failure
2686  *
2687  * Timestamps:
2688  *	tdvp - ctime|mtime updated
2689  *	 svp - ctime updated
2690  */
2691 /* ARGSUSED */
2692 static int
2693 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr)
2694 {
2695 	znode_t		*dzp = VTOZ(tdvp);
2696 	znode_t		*tzp, *szp;
2697 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
2698 	zilog_t		*zilog = zfsvfs->z_log;
2699 	zfs_dirlock_t	*dl;
2700 	dmu_tx_t	*tx;
2701 	vnode_t		*realvp;
2702 	int		error;
2703 
2704 	ASSERT(tdvp->v_type == VDIR);
2705 
2706 	ZFS_ENTER(zfsvfs);
2707 
2708 	if (VOP_REALVP(svp, &realvp) == 0)
2709 		svp = realvp;
2710 
2711 	if (svp->v_vfsp != tdvp->v_vfsp) {
2712 		ZFS_EXIT(zfsvfs);
2713 		return (EXDEV);
2714 	}
2715 
2716 	szp = VTOZ(svp);
2717 top:
2718 	/*
2719 	 * We do not support links between attributes and non-attributes
2720 	 * because of the potential security risk of creating links
2721 	 * into "normal" file space in order to circumvent restrictions
2722 	 * imposed in attribute space.
2723 	 */
2724 	if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
2725 	    (dzp->z_phys->zp_flags & ZFS_XATTR)) {
2726 		ZFS_EXIT(zfsvfs);
2727 		return (EINVAL);
2728 	}
2729 
2730 	/*
2731 	 * POSIX dictates that we return EPERM here.
2732 	 * Better choices include ENOTSUP or EISDIR.
2733 	 */
2734 	if (svp->v_type == VDIR) {
2735 		ZFS_EXIT(zfsvfs);
2736 		return (EPERM);
2737 	}
2738 
2739 	if ((uid_t)szp->z_phys->zp_uid != crgetuid(cr) &&
2740 	    secpolicy_basic_link(cr) != 0) {
2741 		ZFS_EXIT(zfsvfs);
2742 		return (EPERM);
2743 	}
2744 
2745 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
2746 		ZFS_EXIT(zfsvfs);
2747 		return (error);
2748 	}
2749 
2750 	/*
2751 	 * Attempt to lock directory; fail if entry already exists.
2752 	 */
2753 	if (error = zfs_dirent_lock(&dl, dzp, name, &tzp, ZNEW)) {
2754 		ZFS_EXIT(zfsvfs);
2755 		return (error);
2756 	}
2757 
2758 	tx = dmu_tx_create(zfsvfs->z_os);
2759 	dmu_tx_hold_bonus(tx, szp->z_id);
2760 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
2761 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
2762 	if (error) {
2763 		zfs_dirent_unlock(dl);
2764 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2765 			dmu_tx_wait(tx);
2766 			dmu_tx_abort(tx);
2767 			goto top;
2768 		}
2769 		dmu_tx_abort(tx);
2770 		ZFS_EXIT(zfsvfs);
2771 		return (error);
2772 	}
2773 
2774 	error = zfs_link_create(dl, szp, tx, 0);
2775 
2776 	if (error == 0)
2777 		zfs_log_link(zilog, tx, TX_LINK, dzp, szp, name);
2778 
2779 	dmu_tx_commit(tx);
2780 
2781 	zfs_dirent_unlock(dl);
2782 
2783 	if (error == 0) {
2784 		vnevent_link(svp);
2785 	}
2786 
2787 	ZFS_EXIT(zfsvfs);
2788 	return (error);
2789 }
2790 
2791 /*
2792  * zfs_null_putapage() is used when the file system has been force
2793  * unmounted. It just drops the pages.
2794  */
2795 /* ARGSUSED */
2796 static int
2797 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
2798 		size_t *lenp, int flags, cred_t *cr)
2799 {
2800 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
2801 	return (0);
2802 }
2803 
2804 /*
2805  * Push a page out to disk, klustering if possible.
2806  *
2807  *	IN:	vp	- file to push page to.
2808  *		pp	- page to push.
2809  *		flags	- additional flags.
2810  *		cr	- credentials of caller.
2811  *
2812  *	OUT:	offp	- start of range pushed.
2813  *		lenp	- len of range pushed.
2814  *
2815  *	RETURN:	0 if success
2816  *		error code if failure
2817  *
2818  * NOTE: callers must have locked the page to be pushed.  On
2819  * exit, the page (and all other pages in the kluster) must be
2820  * unlocked.
2821  */
2822 /* ARGSUSED */
2823 static int
2824 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
2825 		size_t *lenp, int flags, cred_t *cr)
2826 {
2827 	znode_t		*zp = VTOZ(vp);
2828 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2829 	zilog_t		*zilog = zfsvfs->z_log;
2830 	dmu_tx_t	*tx;
2831 	rl_t		*rl;
2832 	u_offset_t	off, koff;
2833 	size_t		len, klen;
2834 	uint64_t	filesz;
2835 	int		err;
2836 
2837 	filesz = zp->z_phys->zp_size;
2838 	off = pp->p_offset;
2839 	len = PAGESIZE;
2840 	/*
2841 	 * If our blocksize is bigger than the page size, try to kluster
2842 	 * muiltiple pages so that we write a full block (thus avoiding
2843 	 * a read-modify-write).
2844 	 */
2845 	if (off < filesz && zp->z_blksz > PAGESIZE) {
2846 		if (!ISP2(zp->z_blksz)) {
2847 			/* Only one block in the file. */
2848 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
2849 			koff = 0;
2850 		} else {
2851 			klen = zp->z_blksz;
2852 			koff = P2ALIGN(off, (u_offset_t)klen);
2853 		}
2854 		ASSERT(koff <= filesz);
2855 		if (koff + klen > filesz)
2856 			klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE);
2857 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
2858 	}
2859 	ASSERT3U(btop(len), ==, btopr(len));
2860 top:
2861 	rl = zfs_range_lock(zp, off, len, RL_WRITER);
2862 	/*
2863 	 * Can't push pages past end-of-file.
2864 	 */
2865 	filesz = zp->z_phys->zp_size;
2866 	if (off >= filesz) {
2867 		/* ignore all pages */
2868 		err = 0;
2869 		goto out;
2870 	} else if (off + len > filesz) {
2871 		int npages = btopr(filesz - off);
2872 		page_t *trunc;
2873 
2874 		page_list_break(&pp, &trunc, npages);
2875 		/* ignore pages past end of file */
2876 		if (trunc)
2877 			pvn_write_done(trunc, flags);
2878 		len = filesz - off;
2879 	}
2880 
2881 	tx = dmu_tx_create(zfsvfs->z_os);
2882 	dmu_tx_hold_write(tx, zp->z_id, off, len);
2883 	dmu_tx_hold_bonus(tx, zp->z_id);
2884 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2885 	if (err != 0) {
2886 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2887 			zfs_range_unlock(rl);
2888 			dmu_tx_wait(tx);
2889 			dmu_tx_abort(tx);
2890 			err = 0;
2891 			goto top;
2892 		}
2893 		dmu_tx_abort(tx);
2894 		goto out;
2895 	}
2896 
2897 	if (zp->z_blksz <= PAGESIZE) {
2898 		caddr_t va = ppmapin(pp, PROT_READ, (caddr_t)-1);
2899 		ASSERT3U(len, <=, PAGESIZE);
2900 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
2901 		ppmapout(va);
2902 	} else {
2903 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
2904 	}
2905 
2906 	if (err == 0) {
2907 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
2908 		zfs_log_write(zilog, tx, TX_WRITE, zp, off, len, 0);
2909 		dmu_tx_commit(tx);
2910 	}
2911 
2912 out:
2913 	zfs_range_unlock(rl);
2914 	pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
2915 	if (offp)
2916 		*offp = off;
2917 	if (lenp)
2918 		*lenp = len;
2919 
2920 	return (err);
2921 }
2922 
2923 /*
2924  * Copy the portion of the file indicated from pages into the file.
2925  * The pages are stored in a page list attached to the files vnode.
2926  *
2927  *	IN:	vp	- vnode of file to push page data to.
2928  *		off	- position in file to put data.
2929  *		len	- amount of data to write.
2930  *		flags	- flags to control the operation.
2931  *		cr	- credentials of caller.
2932  *
2933  *	RETURN:	0 if success
2934  *		error code if failure
2935  *
2936  * Timestamps:
2937  *	vp - ctime|mtime updated
2938  */
2939 static int
2940 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr)
2941 {
2942 	znode_t		*zp = VTOZ(vp);
2943 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2944 	page_t		*pp;
2945 	size_t		io_len;
2946 	u_offset_t	io_off;
2947 	uint64_t	filesz;
2948 	int		error = 0;
2949 
2950 	ZFS_ENTER(zfsvfs);
2951 
2952 	ASSERT(zp->z_dbuf_held && zp->z_phys);
2953 
2954 	if (len == 0) {
2955 		/*
2956 		 * Search the entire vp list for pages >= off.
2957 		 */
2958 		error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage,
2959 		    flags, cr);
2960 		goto out;
2961 	}
2962 
2963 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
2964 	if (off > filesz) {
2965 		/* past end of file */
2966 		ZFS_EXIT(zfsvfs);
2967 		return (0);
2968 	}
2969 
2970 	len = MIN(len, filesz - off);
2971 
2972 	for (io_off = off; io_off < off + len; io_off += io_len) {
2973 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
2974 			pp = page_lookup(vp, io_off,
2975 			    (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
2976 		} else {
2977 			pp = page_lookup_nowait(vp, io_off,
2978 			    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
2979 		}
2980 
2981 		if (pp != NULL && pvn_getdirty(pp, flags)) {
2982 			int err;
2983 
2984 			/*
2985 			 * Found a dirty page to push
2986 			 */
2987 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
2988 			if (err)
2989 				error = err;
2990 		} else {
2991 			io_len = PAGESIZE;
2992 		}
2993 	}
2994 out:
2995 	if ((flags & B_ASYNC) == 0)
2996 		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
2997 	ZFS_EXIT(zfsvfs);
2998 	return (error);
2999 }
3000 
3001 void
3002 zfs_inactive(vnode_t *vp, cred_t *cr)
3003 {
3004 	znode_t	*zp = VTOZ(vp);
3005 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3006 	int error;
3007 
3008 	rw_enter(&zfsvfs->z_unmount_inactive_lock, RW_READER);
3009 	if (zfsvfs->z_unmounted) {
3010 		ASSERT(zp->z_dbuf_held == 0);
3011 
3012 		if (vn_has_cached_data(vp)) {
3013 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
3014 			    B_INVAL, cr);
3015 		}
3016 
3017 		mutex_enter(&zp->z_lock);
3018 		vp->v_count = 0; /* count arrives as 1 */
3019 		if (zp->z_dbuf == NULL) {
3020 			mutex_exit(&zp->z_lock);
3021 			zfs_znode_free(zp);
3022 		} else {
3023 			mutex_exit(&zp->z_lock);
3024 		}
3025 		rw_exit(&zfsvfs->z_unmount_inactive_lock);
3026 		VFS_RELE(zfsvfs->z_vfs);
3027 		return;
3028 	}
3029 
3030 	/*
3031 	 * Attempt to push any data in the page cache.  If this fails
3032 	 * we will get kicked out later in zfs_zinactive().
3033 	 */
3034 	if (vn_has_cached_data(vp)) {
3035 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
3036 		    cr);
3037 	}
3038 
3039 	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3040 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3041 
3042 		dmu_tx_hold_bonus(tx, zp->z_id);
3043 		error = dmu_tx_assign(tx, TXG_WAIT);
3044 		if (error) {
3045 			dmu_tx_abort(tx);
3046 		} else {
3047 			dmu_buf_will_dirty(zp->z_dbuf, tx);
3048 			mutex_enter(&zp->z_lock);
3049 			zp->z_atime_dirty = 0;
3050 			mutex_exit(&zp->z_lock);
3051 			dmu_tx_commit(tx);
3052 		}
3053 	}
3054 
3055 	zfs_zinactive(zp);
3056 	rw_exit(&zfsvfs->z_unmount_inactive_lock);
3057 }
3058 
3059 /*
3060  * Bounds-check the seek operation.
3061  *
3062  *	IN:	vp	- vnode seeking within
3063  *		ooff	- old file offset
3064  *		noffp	- pointer to new file offset
3065  *
3066  *	RETURN:	0 if success
3067  *		EINVAL if new offset invalid
3068  */
3069 /* ARGSUSED */
3070 static int
3071 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp)
3072 {
3073 	if (vp->v_type == VDIR)
3074 		return (0);
3075 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
3076 }
3077 
3078 /*
3079  * Pre-filter the generic locking function to trap attempts to place
3080  * a mandatory lock on a memory mapped file.
3081  */
3082 static int
3083 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
3084     flk_callback_t *flk_cbp, cred_t *cr)
3085 {
3086 	znode_t *zp = VTOZ(vp);
3087 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3088 	int error;
3089 
3090 	ZFS_ENTER(zfsvfs);
3091 
3092 	/*
3093 	 * We are following the UFS semantics with respect to mapcnt
3094 	 * here: If we see that the file is mapped already, then we will
3095 	 * return an error, but we don't worry about races between this
3096 	 * function and zfs_map().
3097 	 */
3098 	if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) {
3099 		ZFS_EXIT(zfsvfs);
3100 		return (EAGAIN);
3101 	}
3102 	error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr);
3103 	ZFS_EXIT(zfsvfs);
3104 	return (error);
3105 }
3106 
3107 /*
3108  * If we can't find a page in the cache, we will create a new page
3109  * and fill it with file data.  For efficiency, we may try to fill
3110  * multiple pages at once (klustering).
3111  */
3112 static int
3113 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
3114     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
3115 {
3116 	znode_t *zp = VTOZ(vp);
3117 	page_t *pp, *cur_pp;
3118 	objset_t *os = zp->z_zfsvfs->z_os;
3119 	caddr_t va;
3120 	u_offset_t io_off, total;
3121 	uint64_t oid = zp->z_id;
3122 	size_t io_len;
3123 	uint64_t filesz;
3124 	int err;
3125 
3126 	/*
3127 	 * If we are only asking for a single page don't bother klustering.
3128 	 */
3129 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
3130 	if (off >= filesz)
3131 		return (EFAULT);
3132 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
3133 		io_off = off;
3134 		io_len = PAGESIZE;
3135 		pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr);
3136 	} else {
3137 		/*
3138 		 * Try to fill a kluster of pages (a blocks worth).
3139 		 */
3140 		size_t klen;
3141 		u_offset_t koff;
3142 
3143 		if (!ISP2(zp->z_blksz)) {
3144 			/* Only one block in the file. */
3145 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
3146 			koff = 0;
3147 		} else {
3148 			/*
3149 			 * It would be ideal to align our offset to the
3150 			 * blocksize but doing so has resulted in some
3151 			 * strange application crashes. For now, we
3152 			 * leave the offset as is and only adjust the
3153 			 * length if we are off the end of the file.
3154 			 */
3155 			koff = off;
3156 			klen = plsz;
3157 		}
3158 		ASSERT(koff <= filesz);
3159 		if (koff + klen > filesz)
3160 			klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff;
3161 		ASSERT3U(off, >=, koff);
3162 		ASSERT3U(off, <, koff + klen);
3163 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
3164 		    &io_len, koff, klen, 0);
3165 	}
3166 	if (pp == NULL) {
3167 		/*
3168 		 * Some other thread entered the page before us.
3169 		 * Return to zfs_getpage to retry the lookup.
3170 		 */
3171 		*pl = NULL;
3172 		return (0);
3173 	}
3174 
3175 	/*
3176 	 * Fill the pages in the kluster.
3177 	 */
3178 	cur_pp = pp;
3179 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
3180 		ASSERT3U(io_off, ==, cur_pp->p_offset);
3181 		va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1);
3182 		err = dmu_read(os, oid, io_off, PAGESIZE, va);
3183 		ppmapout(va);
3184 		if (err) {
3185 			/* On error, toss the entire kluster */
3186 			pvn_read_done(pp, B_ERROR);
3187 			return (err);
3188 		}
3189 		cur_pp = cur_pp->p_next;
3190 	}
3191 out:
3192 	/*
3193 	 * Fill in the page list array from the kluster.  If
3194 	 * there are too many pages in the kluster, return
3195 	 * as many pages as possible starting from the desired
3196 	 * offset `off'.
3197 	 * NOTE: the page list will always be null terminated.
3198 	 */
3199 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
3200 
3201 	return (0);
3202 }
3203 
3204 /*
3205  * Return pointers to the pages for the file region [off, off + len]
3206  * in the pl array.  If plsz is greater than len, this function may
3207  * also return page pointers from before or after the specified
3208  * region (i.e. some region [off', off' + plsz]).  These additional
3209  * pages are only returned if they are already in the cache, or were
3210  * created as part of a klustered read.
3211  *
3212  *	IN:	vp	- vnode of file to get data from.
3213  *		off	- position in file to get data from.
3214  *		len	- amount of data to retrieve.
3215  *		plsz	- length of provided page list.
3216  *		seg	- segment to obtain pages for.
3217  *		addr	- virtual address of fault.
3218  *		rw	- mode of created pages.
3219  *		cr	- credentials of caller.
3220  *
3221  *	OUT:	protp	- protection mode of created pages.
3222  *		pl	- list of pages created.
3223  *
3224  *	RETURN:	0 if success
3225  *		error code if failure
3226  *
3227  * Timestamps:
3228  *	vp - atime updated
3229  */
3230 /* ARGSUSED */
3231 static int
3232 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
3233 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
3234 	enum seg_rw rw, cred_t *cr)
3235 {
3236 	znode_t		*zp = VTOZ(vp);
3237 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3238 	page_t		*pp, **pl0 = pl;
3239 	int		need_unlock = 0, err = 0;
3240 	offset_t	orig_off;
3241 
3242 	ZFS_ENTER(zfsvfs);
3243 
3244 	if (protp)
3245 		*protp = PROT_ALL;
3246 
3247 	ASSERT(zp->z_dbuf_held && zp->z_phys);
3248 
3249 	/* no faultahead (for now) */
3250 	if (pl == NULL) {
3251 		ZFS_EXIT(zfsvfs);
3252 		return (0);
3253 	}
3254 
3255 	/* can't fault past EOF */
3256 	if (off >= zp->z_phys->zp_size) {
3257 		ZFS_EXIT(zfsvfs);
3258 		return (EFAULT);
3259 	}
3260 	orig_off = off;
3261 
3262 	/*
3263 	 * If we already own the lock, then we must be page faulting
3264 	 * in the middle of a write to this file (i.e., we are writing
3265 	 * to this file using data from a mapped region of the file).
3266 	 */
3267 	if (rw_owner(&zp->z_map_lock) != curthread) {
3268 		rw_enter(&zp->z_map_lock, RW_WRITER);
3269 		need_unlock = TRUE;
3270 	}
3271 
3272 	/*
3273 	 * Loop through the requested range [off, off + len] looking
3274 	 * for pages.  If we don't find a page, we will need to create
3275 	 * a new page and fill it with data from the file.
3276 	 */
3277 	while (len > 0) {
3278 		if (plsz < PAGESIZE)
3279 			break;
3280 		if (pp = page_lookup(vp, off, SE_SHARED)) {
3281 			*pl++ = pp;
3282 			off += PAGESIZE;
3283 			addr += PAGESIZE;
3284 			len -= PAGESIZE;
3285 			plsz -= PAGESIZE;
3286 		} else {
3287 			err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw);
3288 			if (err)
3289 				goto out;
3290 			/*
3291 			 * klustering may have changed our region
3292 			 * to be block aligned.
3293 			 */
3294 			if (((pp = *pl) != 0) && (off != pp->p_offset)) {
3295 				int delta = off - pp->p_offset;
3296 				len += delta;
3297 				off -= delta;
3298 				addr -= delta;
3299 			}
3300 			while (*pl) {
3301 				pl++;
3302 				off += PAGESIZE;
3303 				addr += PAGESIZE;
3304 				plsz -= PAGESIZE;
3305 				if (len > PAGESIZE)
3306 					len -= PAGESIZE;
3307 				else
3308 					len = 0;
3309 			}
3310 		}
3311 	}
3312 
3313 	/*
3314 	 * Fill out the page array with any pages already in the cache.
3315 	 */
3316 	while (plsz > 0) {
3317 		pp = page_lookup_nowait(vp, off, SE_SHARED);
3318 		if (pp == NULL)
3319 			break;
3320 		*pl++ = pp;
3321 		off += PAGESIZE;
3322 		plsz -= PAGESIZE;
3323 	}
3324 
3325 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3326 out:
3327 	/*
3328 	 * We can't grab the range lock for the page as reader which would
3329 	 * stop truncation as this leads to deadlock. So we need to recheck
3330 	 * the file size.
3331 	 */
3332 	if (orig_off >= zp->z_phys->zp_size)
3333 		err = EFAULT;
3334 	if (err) {
3335 		/*
3336 		 * Release any pages we have previously locked.
3337 		 */
3338 		while (pl > pl0)
3339 			page_unlock(*--pl);
3340 	}
3341 
3342 	*pl = NULL;
3343 
3344 	if (need_unlock)
3345 		rw_exit(&zp->z_map_lock);
3346 
3347 	ZFS_EXIT(zfsvfs);
3348 	return (err);
3349 }
3350 
3351 /*
3352  * Request a memory map for a section of a file.  This code interacts
3353  * with common code and the VM system as follows:
3354  *
3355  *	common code calls mmap(), which ends up in smmap_common()
3356  *
3357  *	this calls VOP_MAP(), which takes you into (say) zfs
3358  *
3359  *	zfs_map() calls as_map(), passing segvn_create() as the callback
3360  *
3361  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
3362  *
3363  *	zfs_addmap() updates z_mapcnt
3364  */
3365 static int
3366 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
3367     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
3368 {
3369 	znode_t *zp = VTOZ(vp);
3370 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3371 	segvn_crargs_t	vn_a;
3372 	int		error;
3373 
3374 	ZFS_ENTER(zfsvfs);
3375 
3376 	if (vp->v_flag & VNOMAP) {
3377 		ZFS_EXIT(zfsvfs);
3378 		return (ENOSYS);
3379 	}
3380 
3381 	if (off < 0 || len > MAXOFFSET_T - off) {
3382 		ZFS_EXIT(zfsvfs);
3383 		return (ENXIO);
3384 	}
3385 
3386 	if (vp->v_type != VREG) {
3387 		ZFS_EXIT(zfsvfs);
3388 		return (ENODEV);
3389 	}
3390 
3391 	/*
3392 	 * If file is locked, disallow mapping.
3393 	 */
3394 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) {
3395 		ZFS_EXIT(zfsvfs);
3396 		return (EAGAIN);
3397 	}
3398 
3399 	as_rangelock(as);
3400 	if ((flags & MAP_FIXED) == 0) {
3401 		map_addr(addrp, len, off, 1, flags);
3402 		if (*addrp == NULL) {
3403 			as_rangeunlock(as);
3404 			ZFS_EXIT(zfsvfs);
3405 			return (ENOMEM);
3406 		}
3407 	} else {
3408 		/*
3409 		 * User specified address - blow away any previous mappings
3410 		 */
3411 		(void) as_unmap(as, *addrp, len);
3412 	}
3413 
3414 	vn_a.vp = vp;
3415 	vn_a.offset = (u_offset_t)off;
3416 	vn_a.type = flags & MAP_TYPE;
3417 	vn_a.prot = prot;
3418 	vn_a.maxprot = maxprot;
3419 	vn_a.cred = cr;
3420 	vn_a.amp = NULL;
3421 	vn_a.flags = flags & ~MAP_TYPE;
3422 	vn_a.szc = 0;
3423 	vn_a.lgrp_mem_policy_flags = 0;
3424 
3425 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
3426 
3427 	as_rangeunlock(as);
3428 	ZFS_EXIT(zfsvfs);
3429 	return (error);
3430 }
3431 
3432 /* ARGSUSED */
3433 static int
3434 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
3435     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
3436 {
3437 	uint64_t pages = btopr(len);
3438 
3439 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
3440 	return (0);
3441 }
3442 
3443 /*
3444  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
3445  * more accurate mtime for the associated file.  Since we don't have a way of
3446  * detecting when the data was actually modified, we have to resort to
3447  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
3448  * last page is pushed.  The problem occurs when the msync() call is omitted,
3449  * which by far the most common case:
3450  *
3451  * 	open()
3452  * 	mmap()
3453  * 	<modify memory>
3454  * 	munmap()
3455  * 	close()
3456  * 	<time lapse>
3457  * 	putpage() via fsflush
3458  *
3459  * If we wait until fsflush to come along, we can have a modification time that
3460  * is some arbitrary point in the future.  In order to prevent this in the
3461  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
3462  * torn down.
3463  */
3464 /* ARGSUSED */
3465 static int
3466 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
3467     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr)
3468 {
3469 	uint64_t pages = btopr(len);
3470 
3471 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
3472 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
3473 
3474 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
3475 	    vn_has_cached_data(vp))
3476 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr);
3477 
3478 	return (0);
3479 }
3480 
3481 /*
3482  * Free or allocate space in a file.  Currently, this function only
3483  * supports the `F_FREESP' command.  However, this command is somewhat
3484  * misnamed, as its functionality includes the ability to allocate as
3485  * well as free space.
3486  *
3487  *	IN:	vp	- vnode of file to free data in.
3488  *		cmd	- action to take (only F_FREESP supported).
3489  *		bfp	- section of file to free/alloc.
3490  *		flag	- current file open mode flags.
3491  *		offset	- current file offset.
3492  *		cr	- credentials of caller [UNUSED].
3493  *
3494  *	RETURN:	0 if success
3495  *		error code if failure
3496  *
3497  * Timestamps:
3498  *	vp - ctime|mtime updated
3499  */
3500 /* ARGSUSED */
3501 static int
3502 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
3503     offset_t offset, cred_t *cr, caller_context_t *ct)
3504 {
3505 	znode_t		*zp = VTOZ(vp);
3506 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3507 	uint64_t	off, len;
3508 	int		error;
3509 
3510 	ZFS_ENTER(zfsvfs);
3511 
3512 top:
3513 	if (cmd != F_FREESP) {
3514 		ZFS_EXIT(zfsvfs);
3515 		return (EINVAL);
3516 	}
3517 
3518 	if (error = convoff(vp, bfp, 0, offset)) {
3519 		ZFS_EXIT(zfsvfs);
3520 		return (error);
3521 	}
3522 
3523 	if (bfp->l_len < 0) {
3524 		ZFS_EXIT(zfsvfs);
3525 		return (EINVAL);
3526 	}
3527 
3528 	off = bfp->l_start;
3529 	len = bfp->l_len; /* 0 means from off to end of file */
3530 
3531 	do {
3532 		error = zfs_freesp(zp, off, len, flag, TRUE);
3533 		/* NB: we already did dmu_tx_wait() if necessary */
3534 	} while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
3535 
3536 	ZFS_EXIT(zfsvfs);
3537 	return (error);
3538 }
3539 
3540 static int
3541 zfs_fid(vnode_t *vp, fid_t *fidp)
3542 {
3543 	znode_t		*zp = VTOZ(vp);
3544 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3545 	uint32_t	gen = (uint32_t)zp->z_phys->zp_gen;
3546 	uint64_t	object = zp->z_id;
3547 	zfid_short_t	*zfid;
3548 	int		size, i;
3549 
3550 	ZFS_ENTER(zfsvfs);
3551 
3552 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
3553 	if (fidp->fid_len < size) {
3554 		fidp->fid_len = size;
3555 		ZFS_EXIT(zfsvfs);
3556 		return (ENOSPC);
3557 	}
3558 
3559 	zfid = (zfid_short_t *)fidp;
3560 
3561 	zfid->zf_len = size;
3562 
3563 	for (i = 0; i < sizeof (zfid->zf_object); i++)
3564 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
3565 
3566 	/* Must have a non-zero generation number to distinguish from .zfs */
3567 	if (gen == 0)
3568 		gen = 1;
3569 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
3570 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
3571 
3572 	if (size == LONG_FID_LEN) {
3573 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
3574 		zfid_long_t	*zlfid;
3575 
3576 		zlfid = (zfid_long_t *)fidp;
3577 
3578 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
3579 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
3580 
3581 		/* XXX - this should be the generation number for the objset */
3582 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
3583 			zlfid->zf_setgen[i] = 0;
3584 	}
3585 
3586 	ZFS_EXIT(zfsvfs);
3587 	return (0);
3588 }
3589 
3590 static int
3591 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr)
3592 {
3593 	znode_t		*zp, *xzp;
3594 	zfsvfs_t	*zfsvfs;
3595 	zfs_dirlock_t	*dl;
3596 	int		error;
3597 
3598 	switch (cmd) {
3599 	case _PC_LINK_MAX:
3600 		*valp = ULONG_MAX;
3601 		return (0);
3602 
3603 	case _PC_FILESIZEBITS:
3604 		*valp = 64;
3605 		return (0);
3606 
3607 	case _PC_XATTR_EXISTS:
3608 		zp = VTOZ(vp);
3609 		zfsvfs = zp->z_zfsvfs;
3610 		ZFS_ENTER(zfsvfs);
3611 		*valp = 0;
3612 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
3613 		    ZXATTR | ZEXISTS | ZSHARED);
3614 		if (error == 0) {
3615 			zfs_dirent_unlock(dl);
3616 			if (!zfs_dirempty(xzp))
3617 				*valp = 1;
3618 			VN_RELE(ZTOV(xzp));
3619 		} else if (error == ENOENT) {
3620 			/*
3621 			 * If there aren't extended attributes, it's the
3622 			 * same as having zero of them.
3623 			 */
3624 			error = 0;
3625 		}
3626 		ZFS_EXIT(zfsvfs);
3627 		return (error);
3628 
3629 	case _PC_ACL_ENABLED:
3630 		*valp = _ACL_ACE_ENABLED;
3631 		return (0);
3632 
3633 	case _PC_MIN_HOLE_SIZE:
3634 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
3635 		return (0);
3636 
3637 	default:
3638 		return (fs_pathconf(vp, cmd, valp, cr));
3639 	}
3640 }
3641 
3642 /*ARGSUSED*/
3643 static int
3644 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
3645 {
3646 	znode_t *zp = VTOZ(vp);
3647 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3648 	int error;
3649 
3650 	ZFS_ENTER(zfsvfs);
3651 	error = zfs_getacl(zp, vsecp, cr);
3652 	ZFS_EXIT(zfsvfs);
3653 
3654 	return (error);
3655 }
3656 
3657 /*ARGSUSED*/
3658 static int
3659 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
3660 {
3661 	znode_t *zp = VTOZ(vp);
3662 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3663 	int error;
3664 
3665 	ZFS_ENTER(zfsvfs);
3666 	error = zfs_setacl(zp, vsecp, cr);
3667 	ZFS_EXIT(zfsvfs);
3668 	return (error);
3669 }
3670 
3671 /*
3672  * Predeclare these here so that the compiler assumes that
3673  * this is an "old style" function declaration that does
3674  * not include arguments => we won't get type mismatch errors
3675  * in the initializations that follow.
3676  */
3677 static int zfs_inval();
3678 static int zfs_isdir();
3679 
3680 static int
3681 zfs_inval()
3682 {
3683 	return (EINVAL);
3684 }
3685 
3686 static int
3687 zfs_isdir()
3688 {
3689 	return (EISDIR);
3690 }
3691 /*
3692  * Directory vnode operations template
3693  */
3694 vnodeops_t *zfs_dvnodeops;
3695 const fs_operation_def_t zfs_dvnodeops_template[] = {
3696 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
3697 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
3698 	VOPNAME_READ,		{ .error = zfs_isdir },
3699 	VOPNAME_WRITE,		{ .error = zfs_isdir },
3700 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
3701 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
3702 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
3703 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
3704 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
3705 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
3706 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
3707 	VOPNAME_LINK,		{ .vop_link = zfs_link },
3708 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
3709 	VOPNAME_MKDIR,		{ .vop_mkdir = zfs_mkdir },
3710 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
3711 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
3712 	VOPNAME_SYMLINK,	{ .vop_symlink = zfs_symlink },
3713 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
3714 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
3715 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
3716 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
3717 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
3718 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
3719 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
3720 	VOPNAME_VNEVENT, 	{ .vop_vnevent = fs_vnevent_support },
3721 	NULL,			NULL
3722 };
3723 
3724 /*
3725  * Regular file vnode operations template
3726  */
3727 vnodeops_t *zfs_fvnodeops;
3728 const fs_operation_def_t zfs_fvnodeops_template[] = {
3729 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
3730 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
3731 	VOPNAME_READ,		{ .vop_read = zfs_read },
3732 	VOPNAME_WRITE,		{ .vop_write = zfs_write },
3733 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
3734 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
3735 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
3736 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
3737 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
3738 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
3739 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
3740 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
3741 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
3742 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
3743 	VOPNAME_FRLOCK,		{ .vop_frlock = zfs_frlock },
3744 	VOPNAME_SPACE,		{ .vop_space = zfs_space },
3745 	VOPNAME_GETPAGE,	{ .vop_getpage = zfs_getpage },
3746 	VOPNAME_PUTPAGE,	{ .vop_putpage = zfs_putpage },
3747 	VOPNAME_MAP,		{ .vop_map = zfs_map },
3748 	VOPNAME_ADDMAP,		{ .vop_addmap = zfs_addmap },
3749 	VOPNAME_DELMAP,		{ .vop_delmap = zfs_delmap },
3750 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
3751 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
3752 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
3753 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
3754 	NULL,			NULL
3755 };
3756 
3757 /*
3758  * Symbolic link vnode operations template
3759  */
3760 vnodeops_t *zfs_symvnodeops;
3761 const fs_operation_def_t zfs_symvnodeops_template[] = {
3762 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
3763 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
3764 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
3765 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
3766 	VOPNAME_READLINK,	{ .vop_readlink = zfs_readlink },
3767 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
3768 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
3769 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
3770 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
3771 	NULL,			NULL
3772 };
3773 
3774 /*
3775  * Extended attribute directory vnode operations template
3776  *	This template is identical to the directory vnodes
3777  *	operation template except for restricted operations:
3778  *		VOP_MKDIR()
3779  *		VOP_SYMLINK()
3780  * Note that there are other restrictions embedded in:
3781  *	zfs_create()	- restrict type to VREG
3782  *	zfs_link()	- no links into/out of attribute space
3783  *	zfs_rename()	- no moves into/out of attribute space
3784  */
3785 vnodeops_t *zfs_xdvnodeops;
3786 const fs_operation_def_t zfs_xdvnodeops_template[] = {
3787 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
3788 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
3789 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
3790 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
3791 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
3792 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
3793 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
3794 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
3795 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
3796 	VOPNAME_LINK,		{ .vop_link = zfs_link },
3797 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
3798 	VOPNAME_MKDIR,		{ .error = zfs_inval },
3799 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
3800 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
3801 	VOPNAME_SYMLINK,	{ .error = zfs_inval },
3802 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
3803 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
3804 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
3805 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
3806 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
3807 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
3808 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
3809 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
3810 	NULL,			NULL
3811 };
3812 
3813 /*
3814  * Error vnode operations template
3815  */
3816 vnodeops_t *zfs_evnodeops;
3817 const fs_operation_def_t zfs_evnodeops_template[] = {
3818 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
3819 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
3820 	NULL,			NULL
3821 };
3822