xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 8fd04b8338ed5093ec2d1e668fa620b7de44c177)
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 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /* Portions Copyright 2007 Jeremy Teo */
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/time.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/resource.h>
34 #include <sys/vfs.h>
35 #include <sys/vfs_opreg.h>
36 #include <sys/vnode.h>
37 #include <sys/file.h>
38 #include <sys/stat.h>
39 #include <sys/kmem.h>
40 #include <sys/taskq.h>
41 #include <sys/uio.h>
42 #include <sys/vmsystm.h>
43 #include <sys/atomic.h>
44 #include <sys/vm.h>
45 #include <vm/seg_vn.h>
46 #include <vm/pvn.h>
47 #include <vm/as.h>
48 #include <vm/kpm.h>
49 #include <vm/seg_kpm.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_dir.h>
56 #include <sys/zfs_acl.h>
57 #include <sys/zfs_ioctl.h>
58 #include <sys/fs/zfs.h>
59 #include <sys/dmu.h>
60 #include <sys/spa.h>
61 #include <sys/txg.h>
62 #include <sys/dbuf.h>
63 #include <sys/zap.h>
64 #include <sys/dirent.h>
65 #include <sys/policy.h>
66 #include <sys/sunddi.h>
67 #include <sys/filio.h>
68 #include <sys/sid.h>
69 #include "fs/fs_subr.h"
70 #include <sys/zfs_ctldir.h>
71 #include <sys/zfs_fuid.h>
72 #include <sys/dnlc.h>
73 #include <sys/zfs_rlock.h>
74 #include <sys/extdirent.h>
75 #include <sys/kidmap.h>
76 #include <sys/cred.h>
77 #include <sys/attr.h>
78 
79 /*
80  * Programming rules.
81  *
82  * Each vnode op performs some logical unit of work.  To do this, the ZPL must
83  * properly lock its in-core state, create a DMU transaction, do the work,
84  * record this work in the intent log (ZIL), commit the DMU transaction,
85  * and wait for the intent log to commit if it is a synchronous operation.
86  * Moreover, the vnode ops must work in both normal and log replay context.
87  * The ordering of events is important to avoid deadlocks and references
88  * to freed memory.  The example below illustrates the following Big Rules:
89  *
90  *  (1) A check must be made in each zfs thread for a mounted file system.
91  *	This is done avoiding races using ZFS_ENTER(zfsvfs).
92  *      A ZFS_EXIT(zfsvfs) is needed before all returns.  Any znodes
93  *      must be checked with ZFS_VERIFY_ZP(zp).  Both of these macros
94  *      can return EIO from the calling function.
95  *
96  *  (2)	VN_RELE() should always be the last thing except for zil_commit()
97  *	(if necessary) and ZFS_EXIT(). This is for 3 reasons:
98  *	First, if it's the last reference, the vnode/znode
99  *	can be freed, so the zp may point to freed memory.  Second, the last
100  *	reference will call zfs_zinactive(), which may induce a lot of work --
101  *	pushing cached pages (which acquires range locks) and syncing out
102  *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
103  *	which could deadlock the system if you were already holding one.
104  *	If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
105  *
106  *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
107  *	as they can span dmu_tx_assign() calls.
108  *
109  *  (4)	Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
110  *	This is critical because we don't want to block while holding locks.
111  *	Note, in particular, that if a lock is sometimes acquired before
112  *	the tx assigns, and sometimes after (e.g. z_lock), then failing to
113  *	use a non-blocking assign can deadlock the system.  The scenario:
114  *
115  *	Thread A has grabbed a lock before calling dmu_tx_assign().
116  *	Thread B is in an already-assigned tx, and blocks for this lock.
117  *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
118  *	forever, because the previous txg can't quiesce until B's tx commits.
119  *
120  *	If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
121  *	then drop all locks, call dmu_tx_wait(), and try again.
122  *
123  *  (5)	If the operation succeeded, generate the intent log entry for it
124  *	before dropping locks.  This ensures that the ordering of events
125  *	in the intent log matches the order in which they actually occurred.
126  *      During ZIL replay the zfs_log_* functions will update the sequence
127  *	number to indicate the zil transaction has replayed.
128  *
129  *  (6)	At the end of each vnode op, the DMU tx must always commit,
130  *	regardless of whether there were any errors.
131  *
132  *  (7)	After dropping all locks, invoke zil_commit(zilog, seq, foid)
133  *	to ensure that synchronous semantics are provided when necessary.
134  *
135  * In general, this is how things should be ordered in each vnode op:
136  *
137  *	ZFS_ENTER(zfsvfs);		// exit if unmounted
138  * top:
139  *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
140  *	rw_enter(...);			// grab any other locks you need
141  *	tx = dmu_tx_create(...);	// get DMU tx
142  *	dmu_tx_hold_*();		// hold each object you might modify
143  *	error = dmu_tx_assign(tx, TXG_NOWAIT);	// try to assign
144  *	if (error) {
145  *		rw_exit(...);		// drop locks
146  *		zfs_dirent_unlock(dl);	// unlock directory entry
147  *		VN_RELE(...);		// release held vnodes
148  *		if (error == ERESTART) {
149  *			dmu_tx_wait(tx);
150  *			dmu_tx_abort(tx);
151  *			goto top;
152  *		}
153  *		dmu_tx_abort(tx);	// abort DMU tx
154  *		ZFS_EXIT(zfsvfs);	// finished in zfs
155  *		return (error);		// really out of space
156  *	}
157  *	error = do_real_work();		// do whatever this VOP does
158  *	if (error == 0)
159  *		zfs_log_*(...);		// on success, make ZIL entry
160  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
161  *	rw_exit(...);			// drop locks
162  *	zfs_dirent_unlock(dl);		// unlock directory entry
163  *	VN_RELE(...);			// release held vnodes
164  *	zil_commit(zilog, seq, foid);	// synchronous when necessary
165  *	ZFS_EXIT(zfsvfs);		// finished in zfs
166  *	return (error);			// done, report error
167  */
168 
169 /* ARGSUSED */
170 static int
171 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
172 {
173 	znode_t	*zp = VTOZ(*vpp);
174 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
175 
176 	ZFS_ENTER(zfsvfs);
177 	ZFS_VERIFY_ZP(zp);
178 
179 	if ((flag & FWRITE) && (zp->z_phys->zp_flags & ZFS_APPENDONLY) &&
180 	    ((flag & FAPPEND) == 0)) {
181 		ZFS_EXIT(zfsvfs);
182 		return (EPERM);
183 	}
184 
185 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
186 	    ZTOV(zp)->v_type == VREG &&
187 	    !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
188 	    zp->z_phys->zp_size > 0) {
189 		if (fs_vscan(*vpp, cr, 0) != 0) {
190 			ZFS_EXIT(zfsvfs);
191 			return (EACCES);
192 		}
193 	}
194 
195 	/* Keep a count of the synchronous opens in the znode */
196 	if (flag & (FSYNC | FDSYNC))
197 		atomic_inc_32(&zp->z_sync_cnt);
198 
199 	ZFS_EXIT(zfsvfs);
200 	return (0);
201 }
202 
203 /* ARGSUSED */
204 static int
205 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
206     caller_context_t *ct)
207 {
208 	znode_t	*zp = VTOZ(vp);
209 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
210 
211 	/*
212 	 * Clean up any locks held by this process on the vp.
213 	 */
214 	cleanlocks(vp, ddi_get_pid(), 0);
215 	cleanshares(vp, ddi_get_pid());
216 
217 	ZFS_ENTER(zfsvfs);
218 	ZFS_VERIFY_ZP(zp);
219 
220 	/* Decrement the synchronous opens in the znode */
221 	if ((flag & (FSYNC | FDSYNC)) && (count == 1))
222 		atomic_dec_32(&zp->z_sync_cnt);
223 
224 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
225 	    ZTOV(zp)->v_type == VREG &&
226 	    !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
227 	    zp->z_phys->zp_size > 0)
228 		VERIFY(fs_vscan(vp, cr, 1) == 0);
229 
230 	ZFS_EXIT(zfsvfs);
231 	return (0);
232 }
233 
234 /*
235  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
236  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
237  */
238 static int
239 zfs_holey(vnode_t *vp, int cmd, offset_t *off)
240 {
241 	znode_t	*zp = VTOZ(vp);
242 	uint64_t noff = (uint64_t)*off; /* new offset */
243 	uint64_t file_sz;
244 	int error;
245 	boolean_t hole;
246 
247 	file_sz = zp->z_phys->zp_size;
248 	if (noff >= file_sz)  {
249 		return (ENXIO);
250 	}
251 
252 	if (cmd == _FIO_SEEK_HOLE)
253 		hole = B_TRUE;
254 	else
255 		hole = B_FALSE;
256 
257 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
258 
259 	/* end of file? */
260 	if ((error == ESRCH) || (noff > file_sz)) {
261 		/*
262 		 * Handle the virtual hole at the end of file.
263 		 */
264 		if (hole) {
265 			*off = file_sz;
266 			return (0);
267 		}
268 		return (ENXIO);
269 	}
270 
271 	if (noff < *off)
272 		return (error);
273 	*off = noff;
274 	return (error);
275 }
276 
277 /* ARGSUSED */
278 static int
279 zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
280     int *rvalp, caller_context_t *ct)
281 {
282 	offset_t off;
283 	int error;
284 	zfsvfs_t *zfsvfs;
285 	znode_t *zp;
286 
287 	switch (com) {
288 	case _FIOFFS:
289 		return (zfs_sync(vp->v_vfsp, 0, cred));
290 
291 		/*
292 		 * The following two ioctls are used by bfu.  Faking out,
293 		 * necessary to avoid bfu errors.
294 		 */
295 	case _FIOGDIO:
296 	case _FIOSDIO:
297 		return (0);
298 
299 	case _FIO_SEEK_DATA:
300 	case _FIO_SEEK_HOLE:
301 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
302 			return (EFAULT);
303 
304 		zp = VTOZ(vp);
305 		zfsvfs = zp->z_zfsvfs;
306 		ZFS_ENTER(zfsvfs);
307 		ZFS_VERIFY_ZP(zp);
308 
309 		/* offset parameter is in/out */
310 		error = zfs_holey(vp, com, &off);
311 		ZFS_EXIT(zfsvfs);
312 		if (error)
313 			return (error);
314 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
315 			return (EFAULT);
316 		return (0);
317 	}
318 	return (ENOTTY);
319 }
320 
321 /*
322  * Utility functions to map and unmap a single physical page.  These
323  * are used to manage the mappable copies of ZFS file data, and therefore
324  * do not update ref/mod bits.
325  */
326 caddr_t
327 zfs_map_page(page_t *pp, enum seg_rw rw)
328 {
329 	if (kpm_enable)
330 		return (hat_kpm_mapin(pp, 0));
331 	ASSERT(rw == S_READ || rw == S_WRITE);
332 	return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0),
333 	    (caddr_t)-1));
334 }
335 
336 void
337 zfs_unmap_page(page_t *pp, caddr_t addr)
338 {
339 	if (kpm_enable) {
340 		hat_kpm_mapout(pp, 0, addr);
341 	} else {
342 		ppmapout(addr);
343 	}
344 }
345 
346 /*
347  * When a file is memory mapped, we must keep the IO data synchronized
348  * between the DMU cache and the memory mapped pages.  What this means:
349  *
350  * On Write:	If we find a memory mapped page, we write to *both*
351  *		the page and the dmu buffer.
352  */
353 static void
354 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid)
355 {
356 	int64_t	off;
357 
358 	off = start & PAGEOFFSET;
359 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
360 		page_t *pp;
361 		uint64_t nbytes = MIN(PAGESIZE - off, len);
362 
363 		if (pp = page_lookup(vp, start, SE_SHARED)) {
364 			caddr_t va;
365 
366 			va = zfs_map_page(pp, S_WRITE);
367 			(void) dmu_read(os, oid, start+off, nbytes, va+off,
368 			    DMU_READ_PREFETCH);
369 			zfs_unmap_page(pp, va);
370 			page_unlock(pp);
371 		}
372 		len -= nbytes;
373 		off = 0;
374 	}
375 }
376 
377 /*
378  * When a file is memory mapped, we must keep the IO data synchronized
379  * between the DMU cache and the memory mapped pages.  What this means:
380  *
381  * On Read:	We "read" preferentially from memory mapped pages,
382  *		else we default from the dmu buffer.
383  *
384  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
385  *	the file is memory mapped.
386  */
387 static int
388 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
389 {
390 	znode_t *zp = VTOZ(vp);
391 	objset_t *os = zp->z_zfsvfs->z_os;
392 	int64_t	start, off;
393 	int len = nbytes;
394 	int error = 0;
395 
396 	start = uio->uio_loffset;
397 	off = start & PAGEOFFSET;
398 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
399 		page_t *pp;
400 		uint64_t bytes = MIN(PAGESIZE - off, len);
401 
402 		if (pp = page_lookup(vp, start, SE_SHARED)) {
403 			caddr_t va;
404 
405 			va = zfs_map_page(pp, S_READ);
406 			error = uiomove(va + off, bytes, UIO_READ, uio);
407 			zfs_unmap_page(pp, va);
408 			page_unlock(pp);
409 		} else {
410 			error = dmu_read_uio(os, zp->z_id, uio, bytes);
411 		}
412 		len -= bytes;
413 		off = 0;
414 		if (error)
415 			break;
416 	}
417 	return (error);
418 }
419 
420 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
421 
422 /*
423  * Read bytes from specified file into supplied buffer.
424  *
425  *	IN:	vp	- vnode of file to be read from.
426  *		uio	- structure supplying read location, range info,
427  *			  and return buffer.
428  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
429  *		cr	- credentials of caller.
430  *		ct	- caller context
431  *
432  *	OUT:	uio	- updated offset and range, buffer filled.
433  *
434  *	RETURN:	0 if success
435  *		error code if failure
436  *
437  * Side Effects:
438  *	vp - atime updated if byte count > 0
439  */
440 /* ARGSUSED */
441 static int
442 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
443 {
444 	znode_t		*zp = VTOZ(vp);
445 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
446 	objset_t	*os;
447 	ssize_t		n, nbytes;
448 	int		error;
449 	rl_t		*rl;
450 	xuio_t		*xuio = NULL;
451 
452 	ZFS_ENTER(zfsvfs);
453 	ZFS_VERIFY_ZP(zp);
454 	os = zfsvfs->z_os;
455 
456 	if (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) {
457 		ZFS_EXIT(zfsvfs);
458 		return (EACCES);
459 	}
460 
461 	/*
462 	 * Validate file offset
463 	 */
464 	if (uio->uio_loffset < (offset_t)0) {
465 		ZFS_EXIT(zfsvfs);
466 		return (EINVAL);
467 	}
468 
469 	/*
470 	 * Fasttrack empty reads
471 	 */
472 	if (uio->uio_resid == 0) {
473 		ZFS_EXIT(zfsvfs);
474 		return (0);
475 	}
476 
477 	/*
478 	 * Check for mandatory locks
479 	 */
480 	if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
481 		if (error = chklock(vp, FREAD,
482 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
483 			ZFS_EXIT(zfsvfs);
484 			return (error);
485 		}
486 	}
487 
488 	/*
489 	 * If we're in FRSYNC mode, sync out this znode before reading it.
490 	 */
491 	if (ioflag & FRSYNC)
492 		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
493 
494 	/*
495 	 * Lock the range against changes.
496 	 */
497 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
498 
499 	/*
500 	 * If we are reading past end-of-file we can skip
501 	 * to the end; but we might still need to set atime.
502 	 */
503 	if (uio->uio_loffset >= zp->z_phys->zp_size) {
504 		error = 0;
505 		goto out;
506 	}
507 
508 	ASSERT(uio->uio_loffset < zp->z_phys->zp_size);
509 	n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset);
510 
511 	if ((uio->uio_extflg == UIO_XUIO) &&
512 	    (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
513 		int nblk;
514 		int blksz = zp->z_blksz;
515 		uint64_t offset = uio->uio_loffset;
516 
517 		xuio = (xuio_t *)uio;
518 		if ((ISP2(blksz))) {
519 			nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
520 			    blksz)) / blksz;
521 		} else {
522 			ASSERT(offset + n <= blksz);
523 			nblk = 1;
524 		}
525 		(void) dmu_xuio_init(xuio, nblk);
526 
527 		if (vn_has_cached_data(vp)) {
528 			/*
529 			 * For simplicity, we always allocate a full buffer
530 			 * even if we only expect to read a portion of a block.
531 			 */
532 			while (--nblk >= 0) {
533 				(void) dmu_xuio_add(xuio,
534 				    dmu_request_arcbuf(zp->z_dbuf, blksz),
535 				    0, blksz);
536 			}
537 		}
538 	}
539 
540 	while (n > 0) {
541 		nbytes = MIN(n, zfs_read_chunk_size -
542 		    P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
543 
544 		if (vn_has_cached_data(vp))
545 			error = mappedread(vp, nbytes, uio);
546 		else
547 			error = dmu_read_uio(os, zp->z_id, uio, nbytes);
548 		if (error) {
549 			/* convert checksum errors into IO errors */
550 			if (error == ECKSUM)
551 				error = EIO;
552 			break;
553 		}
554 
555 		n -= nbytes;
556 	}
557 out:
558 	zfs_range_unlock(rl);
559 
560 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
561 	ZFS_EXIT(zfsvfs);
562 	return (error);
563 }
564 
565 /*
566  * Write the bytes to a file.
567  *
568  *	IN:	vp	- vnode of file to be written to.
569  *		uio	- structure supplying write location, range info,
570  *			  and data buffer.
571  *		ioflag	- FAPPEND flag set if in append mode.
572  *		cr	- credentials of caller.
573  *		ct	- caller context (NFS/CIFS fem monitor only)
574  *
575  *	OUT:	uio	- updated offset and range.
576  *
577  *	RETURN:	0 if success
578  *		error code if failure
579  *
580  * Timestamps:
581  *	vp - ctime|mtime updated if byte count > 0
582  */
583 /* ARGSUSED */
584 static int
585 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
586 {
587 	znode_t		*zp = VTOZ(vp);
588 	rlim64_t	limit = uio->uio_llimit;
589 	ssize_t		start_resid = uio->uio_resid;
590 	ssize_t		tx_bytes;
591 	uint64_t	end_size;
592 	dmu_tx_t	*tx;
593 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
594 	zilog_t		*zilog;
595 	offset_t	woff;
596 	ssize_t		n, nbytes;
597 	rl_t		*rl;
598 	int		max_blksz = zfsvfs->z_max_blksz;
599 	uint64_t	pflags;
600 	int		error;
601 	arc_buf_t	*abuf;
602 	iovec_t		*aiov;
603 	xuio_t		*xuio = NULL;
604 	int		i_iov = 0;
605 	int		iovcnt = uio->uio_iovcnt;
606 	iovec_t		*iovp = uio->uio_iov;
607 	int		write_eof;
608 
609 	/*
610 	 * Fasttrack empty write
611 	 */
612 	n = start_resid;
613 	if (n == 0)
614 		return (0);
615 
616 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
617 		limit = MAXOFFSET_T;
618 
619 	ZFS_ENTER(zfsvfs);
620 	ZFS_VERIFY_ZP(zp);
621 
622 	/*
623 	 * If immutable or not appending then return EPERM
624 	 */
625 	pflags = zp->z_phys->zp_flags;
626 	if ((pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
627 	    ((pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
628 	    (uio->uio_loffset < zp->z_phys->zp_size))) {
629 		ZFS_EXIT(zfsvfs);
630 		return (EPERM);
631 	}
632 
633 	zilog = zfsvfs->z_log;
634 
635 	/*
636 	 * Validate file offset
637 	 */
638 	woff = ioflag & FAPPEND ? zp->z_phys->zp_size : uio->uio_loffset;
639 	if (woff < 0) {
640 		ZFS_EXIT(zfsvfs);
641 		return (EINVAL);
642 	}
643 
644 	/*
645 	 * Check for mandatory locks before calling zfs_range_lock()
646 	 * in order to prevent a deadlock with locks set via fcntl().
647 	 */
648 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) &&
649 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
650 		ZFS_EXIT(zfsvfs);
651 		return (error);
652 	}
653 
654 	/*
655 	 * Pre-fault the pages to ensure slow (eg NFS) pages
656 	 * don't hold up txg.
657 	 * Skip this if uio contains loaned arc_buf.
658 	 */
659 	if ((uio->uio_extflg == UIO_XUIO) &&
660 	    (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
661 		xuio = (xuio_t *)uio;
662 	else
663 		uio_prefaultpages(n, uio);
664 
665 	/*
666 	 * If in append mode, set the io offset pointer to eof.
667 	 */
668 	if (ioflag & FAPPEND) {
669 		/*
670 		 * Obtain an appending range lock to guarantee file append
671 		 * semantics.  We reset the write offset once we have the lock.
672 		 */
673 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
674 		woff = rl->r_off;
675 		if (rl->r_len == UINT64_MAX) {
676 			/*
677 			 * We overlocked the file because this write will cause
678 			 * the file block size to increase.
679 			 * Note that zp_size cannot change with this lock held.
680 			 */
681 			woff = zp->z_phys->zp_size;
682 		}
683 		uio->uio_loffset = woff;
684 	} else {
685 		/*
686 		 * Note that if the file block size will change as a result of
687 		 * this write, then this range lock will lock the entire file
688 		 * so that we can re-write the block safely.
689 		 */
690 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
691 	}
692 
693 	if (woff >= limit) {
694 		zfs_range_unlock(rl);
695 		ZFS_EXIT(zfsvfs);
696 		return (EFBIG);
697 	}
698 
699 	if ((woff + n) > limit || woff > (limit - n))
700 		n = limit - woff;
701 
702 	/* Will this write extend the file length? */
703 	write_eof = (woff + n > zp->z_phys->zp_size);
704 
705 	end_size = MAX(zp->z_phys->zp_size, woff + n);
706 
707 	/*
708 	 * Write the file in reasonable size chunks.  Each chunk is written
709 	 * in a separate transaction; this keeps the intent log records small
710 	 * and allows us to do more fine-grained space accounting.
711 	 */
712 	while (n > 0) {
713 		abuf = NULL;
714 		woff = uio->uio_loffset;
715 again:
716 		if (zfs_usergroup_overquota(zfsvfs,
717 		    B_FALSE, zp->z_phys->zp_uid) ||
718 		    zfs_usergroup_overquota(zfsvfs,
719 		    B_TRUE, zp->z_phys->zp_gid)) {
720 			if (abuf != NULL)
721 				dmu_return_arcbuf(abuf);
722 			error = EDQUOT;
723 			break;
724 		}
725 
726 		if (xuio && abuf == NULL) {
727 			ASSERT(i_iov < iovcnt);
728 			aiov = &iovp[i_iov];
729 			abuf = dmu_xuio_arcbuf(xuio, i_iov);
730 			dmu_xuio_clear(xuio, i_iov);
731 			DTRACE_PROBE3(zfs_cp_write, int, i_iov,
732 			    iovec_t *, aiov, arc_buf_t *, abuf);
733 			ASSERT((aiov->iov_base == abuf->b_data) ||
734 			    ((char *)aiov->iov_base - (char *)abuf->b_data +
735 			    aiov->iov_len == arc_buf_size(abuf)));
736 			i_iov++;
737 		} else if (abuf == NULL && n >= max_blksz &&
738 		    woff >= zp->z_phys->zp_size &&
739 		    P2PHASE(woff, max_blksz) == 0 &&
740 		    zp->z_blksz == max_blksz) {
741 			/*
742 			 * This write covers a full block.  "Borrow" a buffer
743 			 * from the dmu so that we can fill it before we enter
744 			 * a transaction.  This avoids the possibility of
745 			 * holding up the transaction if the data copy hangs
746 			 * up on a pagefault (e.g., from an NFS server mapping).
747 			 */
748 			size_t cbytes;
749 
750 			abuf = dmu_request_arcbuf(zp->z_dbuf, max_blksz);
751 			ASSERT(abuf != NULL);
752 			ASSERT(arc_buf_size(abuf) == max_blksz);
753 			if (error = uiocopy(abuf->b_data, max_blksz,
754 			    UIO_WRITE, uio, &cbytes)) {
755 				dmu_return_arcbuf(abuf);
756 				break;
757 			}
758 			ASSERT(cbytes == max_blksz);
759 		}
760 
761 		/*
762 		 * Start a transaction.
763 		 */
764 		tx = dmu_tx_create(zfsvfs->z_os);
765 		dmu_tx_hold_bonus(tx, zp->z_id);
766 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
767 		error = dmu_tx_assign(tx, TXG_NOWAIT);
768 		if (error) {
769 			if (error == ERESTART) {
770 				dmu_tx_wait(tx);
771 				dmu_tx_abort(tx);
772 				goto again;
773 			}
774 			dmu_tx_abort(tx);
775 			if (abuf != NULL)
776 				dmu_return_arcbuf(abuf);
777 			break;
778 		}
779 
780 		/*
781 		 * If zfs_range_lock() over-locked we grow the blocksize
782 		 * and then reduce the lock range.  This will only happen
783 		 * on the first iteration since zfs_range_reduce() will
784 		 * shrink down r_len to the appropriate size.
785 		 */
786 		if (rl->r_len == UINT64_MAX) {
787 			uint64_t new_blksz;
788 
789 			if (zp->z_blksz > max_blksz) {
790 				ASSERT(!ISP2(zp->z_blksz));
791 				new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
792 			} else {
793 				new_blksz = MIN(end_size, max_blksz);
794 			}
795 			zfs_grow_blocksize(zp, new_blksz, tx);
796 			zfs_range_reduce(rl, woff, n);
797 		}
798 
799 		/*
800 		 * XXX - should we really limit each write to z_max_blksz?
801 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
802 		 */
803 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
804 
805 		if (abuf == NULL) {
806 			tx_bytes = uio->uio_resid;
807 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id, uio,
808 			    nbytes, tx);
809 			tx_bytes -= uio->uio_resid;
810 		} else {
811 			tx_bytes = nbytes;
812 			ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
813 			/*
814 			 * If this is not a full block write, but we are
815 			 * extending the file past EOF and this data starts
816 			 * block-aligned, use assign_arcbuf().  Otherwise,
817 			 * write via dmu_write().
818 			 */
819 			if (tx_bytes < max_blksz && (!write_eof ||
820 			    aiov->iov_base != abuf->b_data)) {
821 				ASSERT(xuio);
822 				dmu_write(zfsvfs->z_os, zp->z_id, woff,
823 				    aiov->iov_len, aiov->iov_base, tx);
824 				dmu_return_arcbuf(abuf);
825 				xuio_stat_wbuf_copied();
826 			} else {
827 				ASSERT(xuio || tx_bytes == max_blksz);
828 				dmu_assign_arcbuf(zp->z_dbuf, woff, abuf, tx);
829 			}
830 			ASSERT(tx_bytes <= uio->uio_resid);
831 			uioskip(uio, tx_bytes);
832 		}
833 		if (tx_bytes && vn_has_cached_data(vp)) {
834 			update_pages(vp, woff,
835 			    tx_bytes, zfsvfs->z_os, zp->z_id);
836 		}
837 
838 		/*
839 		 * If we made no progress, we're done.  If we made even
840 		 * partial progress, update the znode and ZIL accordingly.
841 		 */
842 		if (tx_bytes == 0) {
843 			dmu_tx_commit(tx);
844 			ASSERT(error != 0);
845 			break;
846 		}
847 
848 		/*
849 		 * Clear Set-UID/Set-GID bits on successful write if not
850 		 * privileged and at least one of the excute bits is set.
851 		 *
852 		 * It would be nice to to this after all writes have
853 		 * been done, but that would still expose the ISUID/ISGID
854 		 * to another app after the partial write is committed.
855 		 *
856 		 * Note: we don't call zfs_fuid_map_id() here because
857 		 * user 0 is not an ephemeral uid.
858 		 */
859 		mutex_enter(&zp->z_acl_lock);
860 		if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) |
861 		    (S_IXUSR >> 6))) != 0 &&
862 		    (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 &&
863 		    secpolicy_vnode_setid_retain(cr,
864 		    (zp->z_phys->zp_mode & S_ISUID) != 0 &&
865 		    zp->z_phys->zp_uid == 0) != 0) {
866 			zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID);
867 		}
868 		mutex_exit(&zp->z_acl_lock);
869 
870 		/*
871 		 * Update time stamp.  NOTE: This marks the bonus buffer as
872 		 * dirty, so we don't have to do it again for zp_size.
873 		 */
874 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
875 
876 		/*
877 		 * Update the file size (zp_size) if it has changed;
878 		 * account for possible concurrent updates.
879 		 */
880 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset)
881 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
882 			    uio->uio_loffset);
883 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
884 		dmu_tx_commit(tx);
885 
886 		if (error != 0)
887 			break;
888 		ASSERT(tx_bytes == nbytes);
889 		n -= nbytes;
890 	}
891 
892 	zfs_range_unlock(rl);
893 
894 	/*
895 	 * If we're in replay mode, or we made no progress, return error.
896 	 * Otherwise, it's at least a partial write, so it's successful.
897 	 */
898 	if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
899 		ZFS_EXIT(zfsvfs);
900 		return (error);
901 	}
902 
903 	if (ioflag & (FSYNC | FDSYNC))
904 		zil_commit(zilog, zp->z_last_itx, zp->z_id);
905 
906 	ZFS_EXIT(zfsvfs);
907 	return (0);
908 }
909 
910 void
911 zfs_get_done(zgd_t *zgd, int error)
912 {
913 	znode_t *zp = zgd->zgd_private;
914 	objset_t *os = zp->z_zfsvfs->z_os;
915 
916 	if (zgd->zgd_db)
917 		dmu_buf_rele(zgd->zgd_db, zgd);
918 
919 	zfs_range_unlock(zgd->zgd_rl);
920 
921 	/*
922 	 * Release the vnode asynchronously as we currently have the
923 	 * txg stopped from syncing.
924 	 */
925 	VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
926 
927 	if (error == 0 && zgd->zgd_bp)
928 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
929 
930 	kmem_free(zgd, sizeof (zgd_t));
931 }
932 
933 #ifdef DEBUG
934 static int zil_fault_io = 0;
935 #endif
936 
937 /*
938  * Get data to generate a TX_WRITE intent log record.
939  */
940 int
941 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
942 {
943 	zfsvfs_t *zfsvfs = arg;
944 	objset_t *os = zfsvfs->z_os;
945 	znode_t *zp;
946 	uint64_t object = lr->lr_foid;
947 	uint64_t offset = lr->lr_offset;
948 	uint64_t size = lr->lr_length;
949 	blkptr_t *bp = &lr->lr_blkptr;
950 	dmu_buf_t *db;
951 	zgd_t *zgd;
952 	int error = 0;
953 
954 	ASSERT(zio != NULL);
955 	ASSERT(size != 0);
956 
957 	/*
958 	 * Nothing to do if the file has been removed
959 	 */
960 	if (zfs_zget(zfsvfs, object, &zp) != 0)
961 		return (ENOENT);
962 	if (zp->z_unlinked) {
963 		/*
964 		 * Release the vnode asynchronously as we currently have the
965 		 * txg stopped from syncing.
966 		 */
967 		VN_RELE_ASYNC(ZTOV(zp),
968 		    dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
969 		return (ENOENT);
970 	}
971 
972 	zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
973 	zgd->zgd_zilog = zfsvfs->z_log;
974 	zgd->zgd_private = zp;
975 
976 	/*
977 	 * Write records come in two flavors: immediate and indirect.
978 	 * For small writes it's cheaper to store the data with the
979 	 * log record (immediate); for large writes it's cheaper to
980 	 * sync the data and get a pointer to it (indirect) so that
981 	 * we don't have to write the data twice.
982 	 */
983 	if (buf != NULL) { /* immediate write */
984 		zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
985 		/* test for truncation needs to be done while range locked */
986 		if (offset >= zp->z_phys->zp_size) {
987 			error = ENOENT;
988 		} else {
989 			error = dmu_read(os, object, offset, size, buf,
990 			    DMU_READ_NO_PREFETCH);
991 		}
992 		ASSERT(error == 0 || error == ENOENT);
993 	} else { /* indirect write */
994 		/*
995 		 * Have to lock the whole block to ensure when it's
996 		 * written out and it's checksum is being calculated
997 		 * that no one can change the data. We need to re-check
998 		 * blocksize after we get the lock in case it's changed!
999 		 */
1000 		for (;;) {
1001 			uint64_t blkoff;
1002 			size = zp->z_blksz;
1003 			blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1004 			offset -= blkoff;
1005 			zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1006 			    RL_READER);
1007 			if (zp->z_blksz == size)
1008 				break;
1009 			offset += blkoff;
1010 			zfs_range_unlock(zgd->zgd_rl);
1011 		}
1012 		/* test for truncation needs to be done while range locked */
1013 		if (lr->lr_offset >= zp->z_phys->zp_size)
1014 			error = ENOENT;
1015 #ifdef DEBUG
1016 		if (zil_fault_io) {
1017 			error = EIO;
1018 			zil_fault_io = 0;
1019 		}
1020 #endif
1021 		if (error == 0)
1022 			error = dmu_buf_hold(os, object, offset, zgd, &db);
1023 
1024 		if (error == 0) {
1025 			zgd->zgd_db = db;
1026 			zgd->zgd_bp = bp;
1027 
1028 			ASSERT(db->db_offset == offset);
1029 			ASSERT(db->db_size == size);
1030 
1031 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
1032 			    zfs_get_done, zgd);
1033 			ASSERT(error || lr->lr_length <= zp->z_blksz);
1034 
1035 			/*
1036 			 * On success, we need to wait for the write I/O
1037 			 * initiated by dmu_sync() to complete before we can
1038 			 * release this dbuf.  We will finish everything up
1039 			 * in the zfs_get_done() callback.
1040 			 */
1041 			if (error == 0)
1042 				return (0);
1043 
1044 			if (error == EALREADY) {
1045 				lr->lr_common.lrc_txtype = TX_WRITE2;
1046 				error = 0;
1047 			}
1048 		}
1049 	}
1050 
1051 	zfs_get_done(zgd, error);
1052 
1053 	return (error);
1054 }
1055 
1056 /*ARGSUSED*/
1057 static int
1058 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1059     caller_context_t *ct)
1060 {
1061 	znode_t *zp = VTOZ(vp);
1062 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1063 	int error;
1064 
1065 	ZFS_ENTER(zfsvfs);
1066 	ZFS_VERIFY_ZP(zp);
1067 
1068 	if (flag & V_ACE_MASK)
1069 		error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1070 	else
1071 		error = zfs_zaccess_rwx(zp, mode, flag, cr);
1072 
1073 	ZFS_EXIT(zfsvfs);
1074 	return (error);
1075 }
1076 
1077 /*
1078  * If vnode is for a device return a specfs vnode instead.
1079  */
1080 static int
1081 specvp_check(vnode_t **vpp, cred_t *cr)
1082 {
1083 	int error = 0;
1084 
1085 	if (IS_DEVVP(*vpp)) {
1086 		struct vnode *svp;
1087 
1088 		svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1089 		VN_RELE(*vpp);
1090 		if (svp == NULL)
1091 			error = ENOSYS;
1092 		*vpp = svp;
1093 	}
1094 	return (error);
1095 }
1096 
1097 
1098 /*
1099  * Lookup an entry in a directory, or an extended attribute directory.
1100  * If it exists, return a held vnode reference for it.
1101  *
1102  *	IN:	dvp	- vnode of directory to search.
1103  *		nm	- name of entry to lookup.
1104  *		pnp	- full pathname to lookup [UNUSED].
1105  *		flags	- LOOKUP_XATTR set if looking for an attribute.
1106  *		rdir	- root directory vnode [UNUSED].
1107  *		cr	- credentials of caller.
1108  *		ct	- caller context
1109  *		direntflags - directory lookup flags
1110  *		realpnp - returned pathname.
1111  *
1112  *	OUT:	vpp	- vnode of located entry, NULL if not found.
1113  *
1114  *	RETURN:	0 if success
1115  *		error code if failure
1116  *
1117  * Timestamps:
1118  *	NA
1119  */
1120 /* ARGSUSED */
1121 static int
1122 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1123     int flags, vnode_t *rdir, cred_t *cr,  caller_context_t *ct,
1124     int *direntflags, pathname_t *realpnp)
1125 {
1126 	znode_t *zdp = VTOZ(dvp);
1127 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1128 	int	error = 0;
1129 
1130 	/* fast path */
1131 	if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1132 
1133 		if (dvp->v_type != VDIR) {
1134 			return (ENOTDIR);
1135 		} else if (zdp->z_dbuf == NULL) {
1136 			return (EIO);
1137 		}
1138 
1139 		if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1140 			error = zfs_fastaccesschk_execute(zdp, cr);
1141 			if (!error) {
1142 				*vpp = dvp;
1143 				VN_HOLD(*vpp);
1144 				return (0);
1145 			}
1146 			return (error);
1147 		} else {
1148 			vnode_t *tvp = dnlc_lookup(dvp, nm);
1149 
1150 			if (tvp) {
1151 				error = zfs_fastaccesschk_execute(zdp, cr);
1152 				if (error) {
1153 					VN_RELE(tvp);
1154 					return (error);
1155 				}
1156 				if (tvp == DNLC_NO_VNODE) {
1157 					VN_RELE(tvp);
1158 					return (ENOENT);
1159 				} else {
1160 					*vpp = tvp;
1161 					return (specvp_check(vpp, cr));
1162 				}
1163 			}
1164 		}
1165 	}
1166 
1167 	DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1168 
1169 	ZFS_ENTER(zfsvfs);
1170 	ZFS_VERIFY_ZP(zdp);
1171 
1172 	*vpp = NULL;
1173 
1174 	if (flags & LOOKUP_XATTR) {
1175 		/*
1176 		 * If the xattr property is off, refuse the lookup request.
1177 		 */
1178 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1179 			ZFS_EXIT(zfsvfs);
1180 			return (EINVAL);
1181 		}
1182 
1183 		/*
1184 		 * We don't allow recursive attributes..
1185 		 * Maybe someday we will.
1186 		 */
1187 		if (zdp->z_phys->zp_flags & ZFS_XATTR) {
1188 			ZFS_EXIT(zfsvfs);
1189 			return (EINVAL);
1190 		}
1191 
1192 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1193 			ZFS_EXIT(zfsvfs);
1194 			return (error);
1195 		}
1196 
1197 		/*
1198 		 * Do we have permission to get into attribute directory?
1199 		 */
1200 
1201 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1202 		    B_FALSE, cr)) {
1203 			VN_RELE(*vpp);
1204 			*vpp = NULL;
1205 		}
1206 
1207 		ZFS_EXIT(zfsvfs);
1208 		return (error);
1209 	}
1210 
1211 	if (dvp->v_type != VDIR) {
1212 		ZFS_EXIT(zfsvfs);
1213 		return (ENOTDIR);
1214 	}
1215 
1216 	/*
1217 	 * Check accessibility of directory.
1218 	 */
1219 
1220 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1221 		ZFS_EXIT(zfsvfs);
1222 		return (error);
1223 	}
1224 
1225 	if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1226 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1227 		ZFS_EXIT(zfsvfs);
1228 		return (EILSEQ);
1229 	}
1230 
1231 	error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1232 	if (error == 0)
1233 		error = specvp_check(vpp, cr);
1234 
1235 	ZFS_EXIT(zfsvfs);
1236 	return (error);
1237 }
1238 
1239 /*
1240  * Attempt to create a new entry in a directory.  If the entry
1241  * already exists, truncate the file if permissible, else return
1242  * an error.  Return the vp of the created or trunc'd file.
1243  *
1244  *	IN:	dvp	- vnode of directory to put new file entry in.
1245  *		name	- name of new file entry.
1246  *		vap	- attributes of new file.
1247  *		excl	- flag indicating exclusive or non-exclusive mode.
1248  *		mode	- mode to open file with.
1249  *		cr	- credentials of caller.
1250  *		flag	- large file flag [UNUSED].
1251  *		ct	- caller context
1252  *		vsecp 	- ACL to be set
1253  *
1254  *	OUT:	vpp	- vnode of created or trunc'd entry.
1255  *
1256  *	RETURN:	0 if success
1257  *		error code if failure
1258  *
1259  * Timestamps:
1260  *	dvp - ctime|mtime updated if new entry created
1261  *	 vp - ctime|mtime always, atime if new
1262  */
1263 
1264 /* ARGSUSED */
1265 static int
1266 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1267     int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
1268     vsecattr_t *vsecp)
1269 {
1270 	znode_t		*zp, *dzp = VTOZ(dvp);
1271 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1272 	zilog_t		*zilog;
1273 	objset_t	*os;
1274 	zfs_dirlock_t	*dl;
1275 	dmu_tx_t	*tx;
1276 	int		error;
1277 	ksid_t		*ksid;
1278 	uid_t		uid;
1279 	gid_t		gid = crgetgid(cr);
1280 	zfs_acl_ids_t	acl_ids;
1281 	boolean_t	fuid_dirtied;
1282 
1283 	/*
1284 	 * If we have an ephemeral id, ACL, or XVATTR then
1285 	 * make sure file system is at proper version
1286 	 */
1287 
1288 	ksid = crgetsid(cr, KSID_OWNER);
1289 	if (ksid)
1290 		uid = ksid_getid(ksid);
1291 	else
1292 		uid = crgetuid(cr);
1293 
1294 	if (zfsvfs->z_use_fuids == B_FALSE &&
1295 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
1296 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1297 		return (EINVAL);
1298 
1299 	ZFS_ENTER(zfsvfs);
1300 	ZFS_VERIFY_ZP(dzp);
1301 	os = zfsvfs->z_os;
1302 	zilog = zfsvfs->z_log;
1303 
1304 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1305 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1306 		ZFS_EXIT(zfsvfs);
1307 		return (EILSEQ);
1308 	}
1309 
1310 	if (vap->va_mask & AT_XVATTR) {
1311 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
1312 		    crgetuid(cr), cr, vap->va_type)) != 0) {
1313 			ZFS_EXIT(zfsvfs);
1314 			return (error);
1315 		}
1316 	}
1317 top:
1318 	*vpp = NULL;
1319 
1320 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1321 		vap->va_mode &= ~VSVTX;
1322 
1323 	if (*name == '\0') {
1324 		/*
1325 		 * Null component name refers to the directory itself.
1326 		 */
1327 		VN_HOLD(dvp);
1328 		zp = dzp;
1329 		dl = NULL;
1330 		error = 0;
1331 	} else {
1332 		/* possible VN_HOLD(zp) */
1333 		int zflg = 0;
1334 
1335 		if (flag & FIGNORECASE)
1336 			zflg |= ZCILOOK;
1337 
1338 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1339 		    NULL, NULL);
1340 		if (error) {
1341 			if (strcmp(name, "..") == 0)
1342 				error = EISDIR;
1343 			ZFS_EXIT(zfsvfs);
1344 			return (error);
1345 		}
1346 	}
1347 	if (zp == NULL) {
1348 		uint64_t txtype;
1349 
1350 		/*
1351 		 * Create a new file object and update the directory
1352 		 * to reference it.
1353 		 */
1354 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1355 			goto out;
1356 		}
1357 
1358 		/*
1359 		 * We only support the creation of regular files in
1360 		 * extended attribute directories.
1361 		 */
1362 		if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1363 		    (vap->va_type != VREG)) {
1364 			error = EINVAL;
1365 			goto out;
1366 		}
1367 
1368 		if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
1369 		    &acl_ids)) != 0)
1370 			goto out;
1371 		if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1372 			zfs_acl_ids_free(&acl_ids);
1373 			error = EDQUOT;
1374 			goto out;
1375 		}
1376 
1377 		tx = dmu_tx_create(os);
1378 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1379 		fuid_dirtied = zfsvfs->z_fuid_dirty;
1380 		if (fuid_dirtied)
1381 			zfs_fuid_txhold(zfsvfs, tx);
1382 		dmu_tx_hold_bonus(tx, dzp->z_id);
1383 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1384 		if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1385 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1386 			    0, SPA_MAXBLOCKSIZE);
1387 		}
1388 		error = dmu_tx_assign(tx, TXG_NOWAIT);
1389 		if (error) {
1390 			zfs_acl_ids_free(&acl_ids);
1391 			zfs_dirent_unlock(dl);
1392 			if (error == ERESTART) {
1393 				dmu_tx_wait(tx);
1394 				dmu_tx_abort(tx);
1395 				goto top;
1396 			}
1397 			dmu_tx_abort(tx);
1398 			ZFS_EXIT(zfsvfs);
1399 			return (error);
1400 		}
1401 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids);
1402 
1403 		if (fuid_dirtied)
1404 			zfs_fuid_sync(zfsvfs, tx);
1405 
1406 		(void) zfs_link_create(dl, zp, tx, ZNEW);
1407 
1408 		txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1409 		if (flag & FIGNORECASE)
1410 			txtype |= TX_CI;
1411 		zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1412 		    vsecp, acl_ids.z_fuidp, vap);
1413 		zfs_acl_ids_free(&acl_ids);
1414 		dmu_tx_commit(tx);
1415 	} else {
1416 		int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1417 
1418 		/*
1419 		 * A directory entry already exists for this name.
1420 		 */
1421 		/*
1422 		 * Can't truncate an existing file if in exclusive mode.
1423 		 */
1424 		if (excl == EXCL) {
1425 			error = EEXIST;
1426 			goto out;
1427 		}
1428 		/*
1429 		 * Can't open a directory for writing.
1430 		 */
1431 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1432 			error = EISDIR;
1433 			goto out;
1434 		}
1435 		/*
1436 		 * Verify requested access to file.
1437 		 */
1438 		if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1439 			goto out;
1440 		}
1441 
1442 		mutex_enter(&dzp->z_lock);
1443 		dzp->z_seq++;
1444 		mutex_exit(&dzp->z_lock);
1445 
1446 		/*
1447 		 * Truncate regular files if requested.
1448 		 */
1449 		if ((ZTOV(zp)->v_type == VREG) &&
1450 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1451 			/* we can't hold any locks when calling zfs_freesp() */
1452 			zfs_dirent_unlock(dl);
1453 			dl = NULL;
1454 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
1455 			if (error == 0) {
1456 				vnevent_create(ZTOV(zp), ct);
1457 			}
1458 		}
1459 	}
1460 out:
1461 
1462 	if (dl)
1463 		zfs_dirent_unlock(dl);
1464 
1465 	if (error) {
1466 		if (zp)
1467 			VN_RELE(ZTOV(zp));
1468 	} else {
1469 		*vpp = ZTOV(zp);
1470 		error = specvp_check(vpp, cr);
1471 	}
1472 
1473 	ZFS_EXIT(zfsvfs);
1474 	return (error);
1475 }
1476 
1477 /*
1478  * Remove an entry from a directory.
1479  *
1480  *	IN:	dvp	- vnode of directory to remove entry from.
1481  *		name	- name of entry to remove.
1482  *		cr	- credentials of caller.
1483  *		ct	- caller context
1484  *		flags	- case flags
1485  *
1486  *	RETURN:	0 if success
1487  *		error code if failure
1488  *
1489  * Timestamps:
1490  *	dvp - ctime|mtime
1491  *	 vp - ctime (if nlink > 0)
1492  */
1493 /*ARGSUSED*/
1494 static int
1495 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1496     int flags)
1497 {
1498 	znode_t		*zp, *dzp = VTOZ(dvp);
1499 	znode_t		*xzp = NULL;
1500 	vnode_t		*vp;
1501 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1502 	zilog_t		*zilog;
1503 	uint64_t	acl_obj, xattr_obj;
1504 	zfs_dirlock_t	*dl;
1505 	dmu_tx_t	*tx;
1506 	boolean_t	may_delete_now, delete_now = FALSE;
1507 	boolean_t	unlinked, toobig = FALSE;
1508 	uint64_t	txtype;
1509 	pathname_t	*realnmp = NULL;
1510 	pathname_t	realnm;
1511 	int		error;
1512 	int		zflg = ZEXISTS;
1513 
1514 	ZFS_ENTER(zfsvfs);
1515 	ZFS_VERIFY_ZP(dzp);
1516 	zilog = zfsvfs->z_log;
1517 
1518 	if (flags & FIGNORECASE) {
1519 		zflg |= ZCILOOK;
1520 		pn_alloc(&realnm);
1521 		realnmp = &realnm;
1522 	}
1523 
1524 top:
1525 	/*
1526 	 * Attempt to lock directory; fail if entry doesn't exist.
1527 	 */
1528 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1529 	    NULL, realnmp)) {
1530 		if (realnmp)
1531 			pn_free(realnmp);
1532 		ZFS_EXIT(zfsvfs);
1533 		return (error);
1534 	}
1535 
1536 	vp = ZTOV(zp);
1537 
1538 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1539 		goto out;
1540 	}
1541 
1542 	/*
1543 	 * Need to use rmdir for removing directories.
1544 	 */
1545 	if (vp->v_type == VDIR) {
1546 		error = EPERM;
1547 		goto out;
1548 	}
1549 
1550 	vnevent_remove(vp, dvp, name, ct);
1551 
1552 	if (realnmp)
1553 		dnlc_remove(dvp, realnmp->pn_buf);
1554 	else
1555 		dnlc_remove(dvp, name);
1556 
1557 	mutex_enter(&vp->v_lock);
1558 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1559 	mutex_exit(&vp->v_lock);
1560 
1561 	/*
1562 	 * We may delete the znode now, or we may put it in the unlinked set;
1563 	 * it depends on whether we're the last link, and on whether there are
1564 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1565 	 * allow for either case.
1566 	 */
1567 	tx = dmu_tx_create(zfsvfs->z_os);
1568 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1569 	dmu_tx_hold_bonus(tx, zp->z_id);
1570 	if (may_delete_now) {
1571 		toobig =
1572 		    zp->z_phys->zp_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1573 		/* if the file is too big, only hold_free a token amount */
1574 		dmu_tx_hold_free(tx, zp->z_id, 0,
1575 		    (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1576 	}
1577 
1578 	/* are there any extended attributes? */
1579 	if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1580 		/* XXX - do we need this if we are deleting? */
1581 		dmu_tx_hold_bonus(tx, xattr_obj);
1582 	}
1583 
1584 	/* are there any additional acls */
1585 	if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1586 	    may_delete_now)
1587 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1588 
1589 	/* charge as an update -- would be nice not to charge at all */
1590 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1591 
1592 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1593 	if (error) {
1594 		zfs_dirent_unlock(dl);
1595 		VN_RELE(vp);
1596 		if (error == ERESTART) {
1597 			dmu_tx_wait(tx);
1598 			dmu_tx_abort(tx);
1599 			goto top;
1600 		}
1601 		if (realnmp)
1602 			pn_free(realnmp);
1603 		dmu_tx_abort(tx);
1604 		ZFS_EXIT(zfsvfs);
1605 		return (error);
1606 	}
1607 
1608 	/*
1609 	 * Remove the directory entry.
1610 	 */
1611 	error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1612 
1613 	if (error) {
1614 		dmu_tx_commit(tx);
1615 		goto out;
1616 	}
1617 
1618 	if (unlinked) {
1619 		mutex_enter(&vp->v_lock);
1620 		delete_now = may_delete_now && !toobig &&
1621 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1622 		    zp->z_phys->zp_xattr == xattr_obj &&
1623 		    zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1624 		mutex_exit(&vp->v_lock);
1625 	}
1626 
1627 	if (delete_now) {
1628 		if (zp->z_phys->zp_xattr) {
1629 			error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1630 			ASSERT3U(error, ==, 0);
1631 			ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1632 			dmu_buf_will_dirty(xzp->z_dbuf, tx);
1633 			mutex_enter(&xzp->z_lock);
1634 			xzp->z_unlinked = 1;
1635 			xzp->z_phys->zp_links = 0;
1636 			mutex_exit(&xzp->z_lock);
1637 			zfs_unlinked_add(xzp, tx);
1638 			zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1639 		}
1640 		mutex_enter(&zp->z_lock);
1641 		mutex_enter(&vp->v_lock);
1642 		vp->v_count--;
1643 		ASSERT3U(vp->v_count, ==, 0);
1644 		mutex_exit(&vp->v_lock);
1645 		mutex_exit(&zp->z_lock);
1646 		zfs_znode_delete(zp, tx);
1647 	} else if (unlinked) {
1648 		zfs_unlinked_add(zp, tx);
1649 	}
1650 
1651 	txtype = TX_REMOVE;
1652 	if (flags & FIGNORECASE)
1653 		txtype |= TX_CI;
1654 	zfs_log_remove(zilog, tx, txtype, dzp, name);
1655 
1656 	dmu_tx_commit(tx);
1657 out:
1658 	if (realnmp)
1659 		pn_free(realnmp);
1660 
1661 	zfs_dirent_unlock(dl);
1662 
1663 	if (!delete_now) {
1664 		VN_RELE(vp);
1665 	} else if (xzp) {
1666 		/* this rele is delayed to prevent nesting transactions */
1667 		VN_RELE(ZTOV(xzp));
1668 	}
1669 
1670 	ZFS_EXIT(zfsvfs);
1671 	return (error);
1672 }
1673 
1674 /*
1675  * Create a new directory and insert it into dvp using the name
1676  * provided.  Return a pointer to the inserted directory.
1677  *
1678  *	IN:	dvp	- vnode of directory to add subdir to.
1679  *		dirname	- name of new directory.
1680  *		vap	- attributes of new directory.
1681  *		cr	- credentials of caller.
1682  *		ct	- caller context
1683  *		vsecp	- ACL to be set
1684  *
1685  *	OUT:	vpp	- vnode of created directory.
1686  *
1687  *	RETURN:	0 if success
1688  *		error code if failure
1689  *
1690  * Timestamps:
1691  *	dvp - ctime|mtime updated
1692  *	 vp - ctime|mtime|atime updated
1693  */
1694 /*ARGSUSED*/
1695 static int
1696 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
1697     caller_context_t *ct, int flags, vsecattr_t *vsecp)
1698 {
1699 	znode_t		*zp, *dzp = VTOZ(dvp);
1700 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1701 	zilog_t		*zilog;
1702 	zfs_dirlock_t	*dl;
1703 	uint64_t	txtype;
1704 	dmu_tx_t	*tx;
1705 	int		error;
1706 	int		zf = ZNEW;
1707 	ksid_t		*ksid;
1708 	uid_t		uid;
1709 	gid_t		gid = crgetgid(cr);
1710 	zfs_acl_ids_t	acl_ids;
1711 	boolean_t	fuid_dirtied;
1712 
1713 	ASSERT(vap->va_type == VDIR);
1714 
1715 	/*
1716 	 * If we have an ephemeral id, ACL, or XVATTR then
1717 	 * make sure file system is at proper version
1718 	 */
1719 
1720 	ksid = crgetsid(cr, KSID_OWNER);
1721 	if (ksid)
1722 		uid = ksid_getid(ksid);
1723 	else
1724 		uid = crgetuid(cr);
1725 	if (zfsvfs->z_use_fuids == B_FALSE &&
1726 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
1727 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1728 		return (EINVAL);
1729 
1730 	ZFS_ENTER(zfsvfs);
1731 	ZFS_VERIFY_ZP(dzp);
1732 	zilog = zfsvfs->z_log;
1733 
1734 	if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1735 		ZFS_EXIT(zfsvfs);
1736 		return (EINVAL);
1737 	}
1738 
1739 	if (zfsvfs->z_utf8 && u8_validate(dirname,
1740 	    strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1741 		ZFS_EXIT(zfsvfs);
1742 		return (EILSEQ);
1743 	}
1744 	if (flags & FIGNORECASE)
1745 		zf |= ZCILOOK;
1746 
1747 	if (vap->va_mask & AT_XVATTR)
1748 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
1749 		    crgetuid(cr), cr, vap->va_type)) != 0) {
1750 			ZFS_EXIT(zfsvfs);
1751 			return (error);
1752 		}
1753 
1754 	/*
1755 	 * First make sure the new directory doesn't exist.
1756 	 */
1757 top:
1758 	*vpp = NULL;
1759 
1760 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1761 	    NULL, NULL)) {
1762 		ZFS_EXIT(zfsvfs);
1763 		return (error);
1764 	}
1765 
1766 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
1767 		zfs_dirent_unlock(dl);
1768 		ZFS_EXIT(zfsvfs);
1769 		return (error);
1770 	}
1771 
1772 	if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
1773 	    &acl_ids)) != 0) {
1774 		zfs_dirent_unlock(dl);
1775 		ZFS_EXIT(zfsvfs);
1776 		return (error);
1777 	}
1778 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1779 		zfs_acl_ids_free(&acl_ids);
1780 		zfs_dirent_unlock(dl);
1781 		ZFS_EXIT(zfsvfs);
1782 		return (EDQUOT);
1783 	}
1784 
1785 	/*
1786 	 * Add a new entry to the directory.
1787 	 */
1788 	tx = dmu_tx_create(zfsvfs->z_os);
1789 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1790 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1791 	fuid_dirtied = zfsvfs->z_fuid_dirty;
1792 	if (fuid_dirtied)
1793 		zfs_fuid_txhold(zfsvfs, tx);
1794 	if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE)
1795 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1796 		    0, SPA_MAXBLOCKSIZE);
1797 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1798 	if (error) {
1799 		zfs_acl_ids_free(&acl_ids);
1800 		zfs_dirent_unlock(dl);
1801 		if (error == ERESTART) {
1802 			dmu_tx_wait(tx);
1803 			dmu_tx_abort(tx);
1804 			goto top;
1805 		}
1806 		dmu_tx_abort(tx);
1807 		ZFS_EXIT(zfsvfs);
1808 		return (error);
1809 	}
1810 
1811 	/*
1812 	 * Create new node.
1813 	 */
1814 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids);
1815 
1816 	if (fuid_dirtied)
1817 		zfs_fuid_sync(zfsvfs, tx);
1818 	/*
1819 	 * Now put new name in parent dir.
1820 	 */
1821 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1822 
1823 	*vpp = ZTOV(zp);
1824 
1825 	txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
1826 	if (flags & FIGNORECASE)
1827 		txtype |= TX_CI;
1828 	zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
1829 	    acl_ids.z_fuidp, vap);
1830 
1831 	zfs_acl_ids_free(&acl_ids);
1832 	dmu_tx_commit(tx);
1833 
1834 	zfs_dirent_unlock(dl);
1835 
1836 	ZFS_EXIT(zfsvfs);
1837 	return (0);
1838 }
1839 
1840 /*
1841  * Remove a directory subdir entry.  If the current working
1842  * directory is the same as the subdir to be removed, the
1843  * remove will fail.
1844  *
1845  *	IN:	dvp	- vnode of directory to remove from.
1846  *		name	- name of directory to be removed.
1847  *		cwd	- vnode of current working directory.
1848  *		cr	- credentials of caller.
1849  *		ct	- caller context
1850  *		flags	- case flags
1851  *
1852  *	RETURN:	0 if success
1853  *		error code if failure
1854  *
1855  * Timestamps:
1856  *	dvp - ctime|mtime updated
1857  */
1858 /*ARGSUSED*/
1859 static int
1860 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
1861     caller_context_t *ct, int flags)
1862 {
1863 	znode_t		*dzp = VTOZ(dvp);
1864 	znode_t		*zp;
1865 	vnode_t		*vp;
1866 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1867 	zilog_t		*zilog;
1868 	zfs_dirlock_t	*dl;
1869 	dmu_tx_t	*tx;
1870 	int		error;
1871 	int		zflg = ZEXISTS;
1872 
1873 	ZFS_ENTER(zfsvfs);
1874 	ZFS_VERIFY_ZP(dzp);
1875 	zilog = zfsvfs->z_log;
1876 
1877 	if (flags & FIGNORECASE)
1878 		zflg |= ZCILOOK;
1879 top:
1880 	zp = NULL;
1881 
1882 	/*
1883 	 * Attempt to lock directory; fail if entry doesn't exist.
1884 	 */
1885 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1886 	    NULL, NULL)) {
1887 		ZFS_EXIT(zfsvfs);
1888 		return (error);
1889 	}
1890 
1891 	vp = ZTOV(zp);
1892 
1893 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1894 		goto out;
1895 	}
1896 
1897 	if (vp->v_type != VDIR) {
1898 		error = ENOTDIR;
1899 		goto out;
1900 	}
1901 
1902 	if (vp == cwd) {
1903 		error = EINVAL;
1904 		goto out;
1905 	}
1906 
1907 	vnevent_rmdir(vp, dvp, name, ct);
1908 
1909 	/*
1910 	 * Grab a lock on the directory to make sure that noone is
1911 	 * trying to add (or lookup) entries while we are removing it.
1912 	 */
1913 	rw_enter(&zp->z_name_lock, RW_WRITER);
1914 
1915 	/*
1916 	 * Grab a lock on the parent pointer to make sure we play well
1917 	 * with the treewalk and directory rename code.
1918 	 */
1919 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1920 
1921 	tx = dmu_tx_create(zfsvfs->z_os);
1922 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1923 	dmu_tx_hold_bonus(tx, zp->z_id);
1924 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1925 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1926 	if (error) {
1927 		rw_exit(&zp->z_parent_lock);
1928 		rw_exit(&zp->z_name_lock);
1929 		zfs_dirent_unlock(dl);
1930 		VN_RELE(vp);
1931 		if (error == ERESTART) {
1932 			dmu_tx_wait(tx);
1933 			dmu_tx_abort(tx);
1934 			goto top;
1935 		}
1936 		dmu_tx_abort(tx);
1937 		ZFS_EXIT(zfsvfs);
1938 		return (error);
1939 	}
1940 
1941 	error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
1942 
1943 	if (error == 0) {
1944 		uint64_t txtype = TX_RMDIR;
1945 		if (flags & FIGNORECASE)
1946 			txtype |= TX_CI;
1947 		zfs_log_remove(zilog, tx, txtype, dzp, name);
1948 	}
1949 
1950 	dmu_tx_commit(tx);
1951 
1952 	rw_exit(&zp->z_parent_lock);
1953 	rw_exit(&zp->z_name_lock);
1954 out:
1955 	zfs_dirent_unlock(dl);
1956 
1957 	VN_RELE(vp);
1958 
1959 	ZFS_EXIT(zfsvfs);
1960 	return (error);
1961 }
1962 
1963 /*
1964  * Read as many directory entries as will fit into the provided
1965  * buffer from the given directory cursor position (specified in
1966  * the uio structure.
1967  *
1968  *	IN:	vp	- vnode of directory to read.
1969  *		uio	- structure supplying read location, range info,
1970  *			  and return buffer.
1971  *		cr	- credentials of caller.
1972  *		ct	- caller context
1973  *		flags	- case flags
1974  *
1975  *	OUT:	uio	- updated offset and range, buffer filled.
1976  *		eofp	- set to true if end-of-file detected.
1977  *
1978  *	RETURN:	0 if success
1979  *		error code if failure
1980  *
1981  * Timestamps:
1982  *	vp - atime updated
1983  *
1984  * Note that the low 4 bits of the cookie returned by zap is always zero.
1985  * This allows us to use the low range for "special" directory entries:
1986  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
1987  * we use the offset 2 for the '.zfs' directory.
1988  */
1989 /* ARGSUSED */
1990 static int
1991 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
1992     caller_context_t *ct, int flags)
1993 {
1994 	znode_t		*zp = VTOZ(vp);
1995 	iovec_t		*iovp;
1996 	edirent_t	*eodp;
1997 	dirent64_t	*odp;
1998 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1999 	objset_t	*os;
2000 	caddr_t		outbuf;
2001 	size_t		bufsize;
2002 	zap_cursor_t	zc;
2003 	zap_attribute_t	zap;
2004 	uint_t		bytes_wanted;
2005 	uint64_t	offset; /* must be unsigned; checks for < 1 */
2006 	int		local_eof;
2007 	int		outcount;
2008 	int		error;
2009 	uint8_t		prefetch;
2010 	boolean_t	check_sysattrs;
2011 
2012 	ZFS_ENTER(zfsvfs);
2013 	ZFS_VERIFY_ZP(zp);
2014 
2015 	/*
2016 	 * If we are not given an eof variable,
2017 	 * use a local one.
2018 	 */
2019 	if (eofp == NULL)
2020 		eofp = &local_eof;
2021 
2022 	/*
2023 	 * Check for valid iov_len.
2024 	 */
2025 	if (uio->uio_iov->iov_len <= 0) {
2026 		ZFS_EXIT(zfsvfs);
2027 		return (EINVAL);
2028 	}
2029 
2030 	/*
2031 	 * Quit if directory has been removed (posix)
2032 	 */
2033 	if ((*eofp = zp->z_unlinked) != 0) {
2034 		ZFS_EXIT(zfsvfs);
2035 		return (0);
2036 	}
2037 
2038 	error = 0;
2039 	os = zfsvfs->z_os;
2040 	offset = uio->uio_loffset;
2041 	prefetch = zp->z_zn_prefetch;
2042 
2043 	/*
2044 	 * Initialize the iterator cursor.
2045 	 */
2046 	if (offset <= 3) {
2047 		/*
2048 		 * Start iteration from the beginning of the directory.
2049 		 */
2050 		zap_cursor_init(&zc, os, zp->z_id);
2051 	} else {
2052 		/*
2053 		 * The offset is a serialized cursor.
2054 		 */
2055 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2056 	}
2057 
2058 	/*
2059 	 * Get space to change directory entries into fs independent format.
2060 	 */
2061 	iovp = uio->uio_iov;
2062 	bytes_wanted = iovp->iov_len;
2063 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2064 		bufsize = bytes_wanted;
2065 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
2066 		odp = (struct dirent64 *)outbuf;
2067 	} else {
2068 		bufsize = bytes_wanted;
2069 		odp = (struct dirent64 *)iovp->iov_base;
2070 	}
2071 	eodp = (struct edirent *)odp;
2072 
2073 	/*
2074 	 * If this VFS supports the system attribute view interface; and
2075 	 * we're looking at an extended attribute directory; and we care
2076 	 * about normalization conflicts on this vfs; then we must check
2077 	 * for normalization conflicts with the sysattr name space.
2078 	 */
2079 	check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2080 	    (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2081 	    (flags & V_RDDIR_ENTFLAGS);
2082 
2083 	/*
2084 	 * Transform to file-system independent format
2085 	 */
2086 	outcount = 0;
2087 	while (outcount < bytes_wanted) {
2088 		ino64_t objnum;
2089 		ushort_t reclen;
2090 		off64_t *next;
2091 
2092 		/*
2093 		 * Special case `.', `..', and `.zfs'.
2094 		 */
2095 		if (offset == 0) {
2096 			(void) strcpy(zap.za_name, ".");
2097 			zap.za_normalization_conflict = 0;
2098 			objnum = zp->z_id;
2099 		} else if (offset == 1) {
2100 			(void) strcpy(zap.za_name, "..");
2101 			zap.za_normalization_conflict = 0;
2102 			objnum = zp->z_phys->zp_parent;
2103 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
2104 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2105 			zap.za_normalization_conflict = 0;
2106 			objnum = ZFSCTL_INO_ROOT;
2107 		} else {
2108 			/*
2109 			 * Grab next entry.
2110 			 */
2111 			if (error = zap_cursor_retrieve(&zc, &zap)) {
2112 				if ((*eofp = (error == ENOENT)) != 0)
2113 					break;
2114 				else
2115 					goto update;
2116 			}
2117 
2118 			if (zap.za_integer_length != 8 ||
2119 			    zap.za_num_integers != 1) {
2120 				cmn_err(CE_WARN, "zap_readdir: bad directory "
2121 				    "entry, obj = %lld, offset = %lld\n",
2122 				    (u_longlong_t)zp->z_id,
2123 				    (u_longlong_t)offset);
2124 				error = ENXIO;
2125 				goto update;
2126 			}
2127 
2128 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2129 			/*
2130 			 * MacOS X can extract the object type here such as:
2131 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2132 			 */
2133 
2134 			if (check_sysattrs && !zap.za_normalization_conflict) {
2135 				zap.za_normalization_conflict =
2136 				    xattr_sysattr_casechk(zap.za_name);
2137 			}
2138 		}
2139 
2140 		if (flags & V_RDDIR_ACCFILTER) {
2141 			/*
2142 			 * If we have no access at all, don't include
2143 			 * this entry in the returned information
2144 			 */
2145 			znode_t	*ezp;
2146 			if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2147 				goto skip_entry;
2148 			if (!zfs_has_access(ezp, cr)) {
2149 				VN_RELE(ZTOV(ezp));
2150 				goto skip_entry;
2151 			}
2152 			VN_RELE(ZTOV(ezp));
2153 		}
2154 
2155 		if (flags & V_RDDIR_ENTFLAGS)
2156 			reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2157 		else
2158 			reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2159 
2160 		/*
2161 		 * Will this entry fit in the buffer?
2162 		 */
2163 		if (outcount + reclen > bufsize) {
2164 			/*
2165 			 * Did we manage to fit anything in the buffer?
2166 			 */
2167 			if (!outcount) {
2168 				error = EINVAL;
2169 				goto update;
2170 			}
2171 			break;
2172 		}
2173 		if (flags & V_RDDIR_ENTFLAGS) {
2174 			/*
2175 			 * Add extended flag entry:
2176 			 */
2177 			eodp->ed_ino = objnum;
2178 			eodp->ed_reclen = reclen;
2179 			/* NOTE: ed_off is the offset for the *next* entry */
2180 			next = &(eodp->ed_off);
2181 			eodp->ed_eflags = zap.za_normalization_conflict ?
2182 			    ED_CASE_CONFLICT : 0;
2183 			(void) strncpy(eodp->ed_name, zap.za_name,
2184 			    EDIRENT_NAMELEN(reclen));
2185 			eodp = (edirent_t *)((intptr_t)eodp + reclen);
2186 		} else {
2187 			/*
2188 			 * Add normal entry:
2189 			 */
2190 			odp->d_ino = objnum;
2191 			odp->d_reclen = reclen;
2192 			/* NOTE: d_off is the offset for the *next* entry */
2193 			next = &(odp->d_off);
2194 			(void) strncpy(odp->d_name, zap.za_name,
2195 			    DIRENT64_NAMELEN(reclen));
2196 			odp = (dirent64_t *)((intptr_t)odp + reclen);
2197 		}
2198 		outcount += reclen;
2199 
2200 		ASSERT(outcount <= bufsize);
2201 
2202 		/* Prefetch znode */
2203 		if (prefetch)
2204 			dmu_prefetch(os, objnum, 0, 0);
2205 
2206 	skip_entry:
2207 		/*
2208 		 * Move to the next entry, fill in the previous offset.
2209 		 */
2210 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2211 			zap_cursor_advance(&zc);
2212 			offset = zap_cursor_serialize(&zc);
2213 		} else {
2214 			offset += 1;
2215 		}
2216 		*next = offset;
2217 	}
2218 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2219 
2220 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2221 		iovp->iov_base += outcount;
2222 		iovp->iov_len -= outcount;
2223 		uio->uio_resid -= outcount;
2224 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2225 		/*
2226 		 * Reset the pointer.
2227 		 */
2228 		offset = uio->uio_loffset;
2229 	}
2230 
2231 update:
2232 	zap_cursor_fini(&zc);
2233 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2234 		kmem_free(outbuf, bufsize);
2235 
2236 	if (error == ENOENT)
2237 		error = 0;
2238 
2239 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2240 
2241 	uio->uio_loffset = offset;
2242 	ZFS_EXIT(zfsvfs);
2243 	return (error);
2244 }
2245 
2246 ulong_t zfs_fsync_sync_cnt = 4;
2247 
2248 static int
2249 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2250 {
2251 	znode_t	*zp = VTOZ(vp);
2252 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2253 
2254 	/*
2255 	 * Regardless of whether this is required for standards conformance,
2256 	 * this is the logical behavior when fsync() is called on a file with
2257 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
2258 	 * going to be pushed out as part of the zil_commit().
2259 	 */
2260 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
2261 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
2262 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
2263 
2264 	(void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2265 
2266 	ZFS_ENTER(zfsvfs);
2267 	ZFS_VERIFY_ZP(zp);
2268 	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
2269 	ZFS_EXIT(zfsvfs);
2270 	return (0);
2271 }
2272 
2273 
2274 /*
2275  * Get the requested file attributes and place them in the provided
2276  * vattr structure.
2277  *
2278  *	IN:	vp	- vnode of file.
2279  *		vap	- va_mask identifies requested attributes.
2280  *			  If AT_XVATTR set, then optional attrs are requested
2281  *		flags	- ATTR_NOACLCHECK (CIFS server context)
2282  *		cr	- credentials of caller.
2283  *		ct	- caller context
2284  *
2285  *	OUT:	vap	- attribute values.
2286  *
2287  *	RETURN:	0 (always succeeds)
2288  */
2289 /* ARGSUSED */
2290 static int
2291 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2292     caller_context_t *ct)
2293 {
2294 	znode_t *zp = VTOZ(vp);
2295 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2296 	znode_phys_t *pzp;
2297 	int	error = 0;
2298 	uint64_t links;
2299 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
2300 	xoptattr_t *xoap = NULL;
2301 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2302 
2303 	ZFS_ENTER(zfsvfs);
2304 	ZFS_VERIFY_ZP(zp);
2305 	pzp = zp->z_phys;
2306 
2307 	/*
2308 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2309 	 * Also, if we are the owner don't bother, since owner should
2310 	 * always be allowed to read basic attributes of file.
2311 	 */
2312 	if (!(pzp->zp_flags & ZFS_ACL_TRIVIAL) &&
2313 	    (pzp->zp_uid != crgetuid(cr))) {
2314 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2315 		    skipaclchk, cr)) {
2316 			ZFS_EXIT(zfsvfs);
2317 			return (error);
2318 		}
2319 	}
2320 
2321 	/*
2322 	 * Return all attributes.  It's cheaper to provide the answer
2323 	 * than to determine whether we were asked the question.
2324 	 */
2325 
2326 	mutex_enter(&zp->z_lock);
2327 	vap->va_type = vp->v_type;
2328 	vap->va_mode = pzp->zp_mode & MODEMASK;
2329 	zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2330 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2331 	vap->va_nodeid = zp->z_id;
2332 	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2333 		links = pzp->zp_links + 1;
2334 	else
2335 		links = pzp->zp_links;
2336 	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
2337 	vap->va_size = pzp->zp_size;
2338 	vap->va_rdev = vp->v_rdev;
2339 	vap->va_seq = zp->z_seq;
2340 
2341 	/*
2342 	 * Add in any requested optional attributes and the create time.
2343 	 * Also set the corresponding bits in the returned attribute bitmap.
2344 	 */
2345 	if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2346 		if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2347 			xoap->xoa_archive =
2348 			    ((pzp->zp_flags & ZFS_ARCHIVE) != 0);
2349 			XVA_SET_RTN(xvap, XAT_ARCHIVE);
2350 		}
2351 
2352 		if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2353 			xoap->xoa_readonly =
2354 			    ((pzp->zp_flags & ZFS_READONLY) != 0);
2355 			XVA_SET_RTN(xvap, XAT_READONLY);
2356 		}
2357 
2358 		if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2359 			xoap->xoa_system =
2360 			    ((pzp->zp_flags & ZFS_SYSTEM) != 0);
2361 			XVA_SET_RTN(xvap, XAT_SYSTEM);
2362 		}
2363 
2364 		if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2365 			xoap->xoa_hidden =
2366 			    ((pzp->zp_flags & ZFS_HIDDEN) != 0);
2367 			XVA_SET_RTN(xvap, XAT_HIDDEN);
2368 		}
2369 
2370 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2371 			xoap->xoa_nounlink =
2372 			    ((pzp->zp_flags & ZFS_NOUNLINK) != 0);
2373 			XVA_SET_RTN(xvap, XAT_NOUNLINK);
2374 		}
2375 
2376 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2377 			xoap->xoa_immutable =
2378 			    ((pzp->zp_flags & ZFS_IMMUTABLE) != 0);
2379 			XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2380 		}
2381 
2382 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2383 			xoap->xoa_appendonly =
2384 			    ((pzp->zp_flags & ZFS_APPENDONLY) != 0);
2385 			XVA_SET_RTN(xvap, XAT_APPENDONLY);
2386 		}
2387 
2388 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2389 			xoap->xoa_nodump =
2390 			    ((pzp->zp_flags & ZFS_NODUMP) != 0);
2391 			XVA_SET_RTN(xvap, XAT_NODUMP);
2392 		}
2393 
2394 		if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2395 			xoap->xoa_opaque =
2396 			    ((pzp->zp_flags & ZFS_OPAQUE) != 0);
2397 			XVA_SET_RTN(xvap, XAT_OPAQUE);
2398 		}
2399 
2400 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2401 			xoap->xoa_av_quarantined =
2402 			    ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0);
2403 			XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2404 		}
2405 
2406 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2407 			xoap->xoa_av_modified =
2408 			    ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0);
2409 			XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2410 		}
2411 
2412 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2413 		    vp->v_type == VREG &&
2414 		    (pzp->zp_flags & ZFS_BONUS_SCANSTAMP)) {
2415 			size_t len;
2416 			dmu_object_info_t doi;
2417 
2418 			/*
2419 			 * Only VREG files have anti-virus scanstamps, so we
2420 			 * won't conflict with symlinks in the bonus buffer.
2421 			 */
2422 			dmu_object_info_from_db(zp->z_dbuf, &doi);
2423 			len = sizeof (xoap->xoa_av_scanstamp) +
2424 			    sizeof (znode_phys_t);
2425 			if (len <= doi.doi_bonus_size) {
2426 				/*
2427 				 * pzp points to the start of the
2428 				 * znode_phys_t. pzp + 1 points to the
2429 				 * first byte after the znode_phys_t.
2430 				 */
2431 				(void) memcpy(xoap->xoa_av_scanstamp,
2432 				    pzp + 1,
2433 				    sizeof (xoap->xoa_av_scanstamp));
2434 				XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
2435 			}
2436 		}
2437 
2438 		if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2439 			ZFS_TIME_DECODE(&xoap->xoa_createtime, pzp->zp_crtime);
2440 			XVA_SET_RTN(xvap, XAT_CREATETIME);
2441 		}
2442 
2443 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2444 			xoap->xoa_reparse =
2445 			    ((pzp->zp_flags & ZFS_REPARSE) != 0);
2446 			XVA_SET_RTN(xvap, XAT_REPARSE);
2447 		}
2448 	}
2449 
2450 	ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
2451 	ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
2452 	ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
2453 
2454 	mutex_exit(&zp->z_lock);
2455 
2456 	dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks);
2457 
2458 	if (zp->z_blksz == 0) {
2459 		/*
2460 		 * Block size hasn't been set; suggest maximal I/O transfers.
2461 		 */
2462 		vap->va_blksize = zfsvfs->z_max_blksz;
2463 	}
2464 
2465 	ZFS_EXIT(zfsvfs);
2466 	return (0);
2467 }
2468 
2469 /*
2470  * Set the file attributes to the values contained in the
2471  * vattr structure.
2472  *
2473  *	IN:	vp	- vnode of file to be modified.
2474  *		vap	- new attribute values.
2475  *			  If AT_XVATTR set, then optional attrs are being set
2476  *		flags	- ATTR_UTIME set if non-default time values provided.
2477  *			- ATTR_NOACLCHECK (CIFS context only).
2478  *		cr	- credentials of caller.
2479  *		ct	- caller context
2480  *
2481  *	RETURN:	0 if success
2482  *		error code if failure
2483  *
2484  * Timestamps:
2485  *	vp - ctime updated, mtime updated if size changed.
2486  */
2487 /* ARGSUSED */
2488 static int
2489 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2490 	caller_context_t *ct)
2491 {
2492 	znode_t		*zp = VTOZ(vp);
2493 	znode_phys_t	*pzp;
2494 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2495 	zilog_t		*zilog;
2496 	dmu_tx_t	*tx;
2497 	vattr_t		oldva;
2498 	xvattr_t	tmpxvattr;
2499 	uint_t		mask = vap->va_mask;
2500 	uint_t		saved_mask;
2501 	int		trim_mask = 0;
2502 	uint64_t	new_mode;
2503 	uint64_t	new_uid, new_gid;
2504 	znode_t		*attrzp;
2505 	int		need_policy = FALSE;
2506 	int		err;
2507 	zfs_fuid_info_t *fuidp = NULL;
2508 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
2509 	xoptattr_t	*xoap;
2510 	zfs_acl_t	*aclp = NULL;
2511 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2512 	boolean_t fuid_dirtied = B_FALSE;
2513 
2514 	if (mask == 0)
2515 		return (0);
2516 
2517 	if (mask & AT_NOSET)
2518 		return (EINVAL);
2519 
2520 	ZFS_ENTER(zfsvfs);
2521 	ZFS_VERIFY_ZP(zp);
2522 
2523 	pzp = zp->z_phys;
2524 	zilog = zfsvfs->z_log;
2525 
2526 	/*
2527 	 * Make sure that if we have ephemeral uid/gid or xvattr specified
2528 	 * that file system is at proper version level
2529 	 */
2530 
2531 	if (zfsvfs->z_use_fuids == B_FALSE &&
2532 	    (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2533 	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2534 	    (mask & AT_XVATTR))) {
2535 		ZFS_EXIT(zfsvfs);
2536 		return (EINVAL);
2537 	}
2538 
2539 	if (mask & AT_SIZE && vp->v_type == VDIR) {
2540 		ZFS_EXIT(zfsvfs);
2541 		return (EISDIR);
2542 	}
2543 
2544 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2545 		ZFS_EXIT(zfsvfs);
2546 		return (EINVAL);
2547 	}
2548 
2549 	/*
2550 	 * If this is an xvattr_t, then get a pointer to the structure of
2551 	 * optional attributes.  If this is NULL, then we have a vattr_t.
2552 	 */
2553 	xoap = xva_getxoptattr(xvap);
2554 
2555 	xva_init(&tmpxvattr);
2556 
2557 	/*
2558 	 * Immutable files can only alter immutable bit and atime
2559 	 */
2560 	if ((pzp->zp_flags & ZFS_IMMUTABLE) &&
2561 	    ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2562 	    ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2563 		ZFS_EXIT(zfsvfs);
2564 		return (EPERM);
2565 	}
2566 
2567 	if ((mask & AT_SIZE) && (pzp->zp_flags & ZFS_READONLY)) {
2568 		ZFS_EXIT(zfsvfs);
2569 		return (EPERM);
2570 	}
2571 
2572 	/*
2573 	 * Verify timestamps doesn't overflow 32 bits.
2574 	 * ZFS can handle large timestamps, but 32bit syscalls can't
2575 	 * handle times greater than 2039.  This check should be removed
2576 	 * once large timestamps are fully supported.
2577 	 */
2578 	if (mask & (AT_ATIME | AT_MTIME)) {
2579 		if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2580 		    ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2581 			ZFS_EXIT(zfsvfs);
2582 			return (EOVERFLOW);
2583 		}
2584 	}
2585 
2586 top:
2587 	attrzp = NULL;
2588 
2589 	/* Can this be moved to before the top label? */
2590 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2591 		ZFS_EXIT(zfsvfs);
2592 		return (EROFS);
2593 	}
2594 
2595 	/*
2596 	 * First validate permissions
2597 	 */
2598 
2599 	if (mask & AT_SIZE) {
2600 		err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2601 		if (err) {
2602 			ZFS_EXIT(zfsvfs);
2603 			return (err);
2604 		}
2605 		/*
2606 		 * XXX - Note, we are not providing any open
2607 		 * mode flags here (like FNDELAY), so we may
2608 		 * block if there are locks present... this
2609 		 * should be addressed in openat().
2610 		 */
2611 		/* XXX - would it be OK to generate a log record here? */
2612 		err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2613 		if (err) {
2614 			ZFS_EXIT(zfsvfs);
2615 			return (err);
2616 		}
2617 	}
2618 
2619 	if (mask & (AT_ATIME|AT_MTIME) ||
2620 	    ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2621 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2622 	    XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2623 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2624 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM))))
2625 		need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2626 		    skipaclchk, cr);
2627 
2628 	if (mask & (AT_UID|AT_GID)) {
2629 		int	idmask = (mask & (AT_UID|AT_GID));
2630 		int	take_owner;
2631 		int	take_group;
2632 
2633 		/*
2634 		 * NOTE: even if a new mode is being set,
2635 		 * we may clear S_ISUID/S_ISGID bits.
2636 		 */
2637 
2638 		if (!(mask & AT_MODE))
2639 			vap->va_mode = pzp->zp_mode;
2640 
2641 		/*
2642 		 * Take ownership or chgrp to group we are a member of
2643 		 */
2644 
2645 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2646 		take_group = (mask & AT_GID) &&
2647 		    zfs_groupmember(zfsvfs, vap->va_gid, cr);
2648 
2649 		/*
2650 		 * If both AT_UID and AT_GID are set then take_owner and
2651 		 * take_group must both be set in order to allow taking
2652 		 * ownership.
2653 		 *
2654 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2655 		 *
2656 		 */
2657 
2658 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2659 		    ((idmask == AT_UID) && take_owner) ||
2660 		    ((idmask == AT_GID) && take_group)) {
2661 			if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2662 			    skipaclchk, cr) == 0) {
2663 				/*
2664 				 * Remove setuid/setgid for non-privileged users
2665 				 */
2666 				secpolicy_setid_clear(vap, cr);
2667 				trim_mask = (mask & (AT_UID|AT_GID));
2668 			} else {
2669 				need_policy =  TRUE;
2670 			}
2671 		} else {
2672 			need_policy =  TRUE;
2673 		}
2674 	}
2675 
2676 	mutex_enter(&zp->z_lock);
2677 	oldva.va_mode = pzp->zp_mode;
2678 	zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2679 	if (mask & AT_XVATTR) {
2680 		/*
2681 		 * Update xvattr mask to include only those attributes
2682 		 * that are actually changing.
2683 		 *
2684 		 * the bits will be restored prior to actually setting
2685 		 * the attributes so the caller thinks they were set.
2686 		 */
2687 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2688 			if (xoap->xoa_appendonly !=
2689 			    ((pzp->zp_flags & ZFS_APPENDONLY) != 0)) {
2690 				need_policy = TRUE;
2691 			} else {
2692 				XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2693 				XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
2694 			}
2695 		}
2696 
2697 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2698 			if (xoap->xoa_nounlink !=
2699 			    ((pzp->zp_flags & ZFS_NOUNLINK) != 0)) {
2700 				need_policy = TRUE;
2701 			} else {
2702 				XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2703 				XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
2704 			}
2705 		}
2706 
2707 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2708 			if (xoap->xoa_immutable !=
2709 			    ((pzp->zp_flags & ZFS_IMMUTABLE) != 0)) {
2710 				need_policy = TRUE;
2711 			} else {
2712 				XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2713 				XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
2714 			}
2715 		}
2716 
2717 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2718 			if (xoap->xoa_nodump !=
2719 			    ((pzp->zp_flags & ZFS_NODUMP) != 0)) {
2720 				need_policy = TRUE;
2721 			} else {
2722 				XVA_CLR_REQ(xvap, XAT_NODUMP);
2723 				XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
2724 			}
2725 		}
2726 
2727 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2728 			if (xoap->xoa_av_modified !=
2729 			    ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0)) {
2730 				need_policy = TRUE;
2731 			} else {
2732 				XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2733 				XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
2734 			}
2735 		}
2736 
2737 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2738 			if ((vp->v_type != VREG &&
2739 			    xoap->xoa_av_quarantined) ||
2740 			    xoap->xoa_av_quarantined !=
2741 			    ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0)) {
2742 				need_policy = TRUE;
2743 			} else {
2744 				XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2745 				XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
2746 			}
2747 		}
2748 
2749 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2750 			mutex_exit(&zp->z_lock);
2751 			ZFS_EXIT(zfsvfs);
2752 			return (EPERM);
2753 		}
2754 
2755 		if (need_policy == FALSE &&
2756 		    (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2757 		    XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2758 			need_policy = TRUE;
2759 		}
2760 	}
2761 
2762 	mutex_exit(&zp->z_lock);
2763 
2764 	if (mask & AT_MODE) {
2765 		if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2766 			err = secpolicy_setid_setsticky_clear(vp, vap,
2767 			    &oldva, cr);
2768 			if (err) {
2769 				ZFS_EXIT(zfsvfs);
2770 				return (err);
2771 			}
2772 			trim_mask |= AT_MODE;
2773 		} else {
2774 			need_policy = TRUE;
2775 		}
2776 	}
2777 
2778 	if (need_policy) {
2779 		/*
2780 		 * If trim_mask is set then take ownership
2781 		 * has been granted or write_acl is present and user
2782 		 * has the ability to modify mode.  In that case remove
2783 		 * UID|GID and or MODE from mask so that
2784 		 * secpolicy_vnode_setattr() doesn't revoke it.
2785 		 */
2786 
2787 		if (trim_mask) {
2788 			saved_mask = vap->va_mask;
2789 			vap->va_mask &= ~trim_mask;
2790 		}
2791 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2792 		    (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2793 		if (err) {
2794 			ZFS_EXIT(zfsvfs);
2795 			return (err);
2796 		}
2797 
2798 		if (trim_mask)
2799 			vap->va_mask |= saved_mask;
2800 	}
2801 
2802 	/*
2803 	 * secpolicy_vnode_setattr, or take ownership may have
2804 	 * changed va_mask
2805 	 */
2806 	mask = vap->va_mask;
2807 
2808 	tx = dmu_tx_create(zfsvfs->z_os);
2809 	dmu_tx_hold_bonus(tx, zp->z_id);
2810 
2811 	if (mask & AT_MODE) {
2812 		uint64_t pmode = pzp->zp_mode;
2813 
2814 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2815 
2816 		if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
2817 			goto out;
2818 		if (pzp->zp_acl.z_acl_extern_obj) {
2819 			/* Are we upgrading ACL from old V0 format to new V1 */
2820 			if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
2821 			    pzp->zp_acl.z_acl_version ==
2822 			    ZFS_ACL_VERSION_INITIAL) {
2823 				dmu_tx_hold_free(tx,
2824 				    pzp->zp_acl.z_acl_extern_obj, 0,
2825 				    DMU_OBJECT_END);
2826 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2827 				    0, aclp->z_acl_bytes);
2828 			} else {
2829 				dmu_tx_hold_write(tx,
2830 				    pzp->zp_acl.z_acl_extern_obj, 0,
2831 				    aclp->z_acl_bytes);
2832 			}
2833 		} else if (aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2834 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2835 			    0, aclp->z_acl_bytes);
2836 		}
2837 	}
2838 
2839 	if (mask & (AT_UID | AT_GID)) {
2840 		if (pzp->zp_xattr) {
2841 			err = zfs_zget(zp->z_zfsvfs, pzp->zp_xattr, &attrzp);
2842 			if (err)
2843 				goto out;
2844 			dmu_tx_hold_bonus(tx, attrzp->z_id);
2845 		}
2846 		if (mask & AT_UID) {
2847 			new_uid = zfs_fuid_create(zfsvfs,
2848 			    (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2849 			if (new_uid != pzp->zp_uid &&
2850 			    zfs_usergroup_overquota(zfsvfs, B_FALSE, new_uid)) {
2851 				err = EDQUOT;
2852 				goto out;
2853 			}
2854 		}
2855 
2856 		if (mask & AT_GID) {
2857 			new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
2858 			    cr, ZFS_GROUP, &fuidp);
2859 			if (new_gid != pzp->zp_gid &&
2860 			    zfs_usergroup_overquota(zfsvfs, B_TRUE, new_gid)) {
2861 				err = EDQUOT;
2862 				goto out;
2863 			}
2864 		}
2865 		fuid_dirtied = zfsvfs->z_fuid_dirty;
2866 		if (fuid_dirtied) {
2867 			if (zfsvfs->z_fuid_obj == 0) {
2868 				dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
2869 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2870 				    FUID_SIZE_ESTIMATE(zfsvfs));
2871 				dmu_tx_hold_zap(tx, MASTER_NODE_OBJ,
2872 				    FALSE, NULL);
2873 			} else {
2874 				dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
2875 				dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
2876 				    FUID_SIZE_ESTIMATE(zfsvfs));
2877 			}
2878 		}
2879 	}
2880 
2881 	err = dmu_tx_assign(tx, TXG_NOWAIT);
2882 	if (err) {
2883 		if (err == ERESTART)
2884 			dmu_tx_wait(tx);
2885 		goto out;
2886 	}
2887 
2888 	dmu_buf_will_dirty(zp->z_dbuf, tx);
2889 
2890 	/*
2891 	 * Set each attribute requested.
2892 	 * We group settings according to the locks they need to acquire.
2893 	 *
2894 	 * Note: you cannot set ctime directly, although it will be
2895 	 * updated as a side-effect of calling this function.
2896 	 */
2897 
2898 	mutex_enter(&zp->z_lock);
2899 
2900 	if (mask & AT_MODE) {
2901 		mutex_enter(&zp->z_acl_lock);
2902 		zp->z_phys->zp_mode = new_mode;
2903 		err = zfs_aclset_common(zp, aclp, cr, tx);
2904 		ASSERT3U(err, ==, 0);
2905 		zp->z_acl_cached = aclp;
2906 		aclp = NULL;
2907 		mutex_exit(&zp->z_acl_lock);
2908 	}
2909 
2910 	if (attrzp)
2911 		mutex_enter(&attrzp->z_lock);
2912 
2913 	if (mask & AT_UID) {
2914 		pzp->zp_uid = new_uid;
2915 		if (attrzp)
2916 			attrzp->z_phys->zp_uid = new_uid;
2917 	}
2918 
2919 	if (mask & AT_GID) {
2920 		pzp->zp_gid = new_gid;
2921 		if (attrzp)
2922 			attrzp->z_phys->zp_gid = new_gid;
2923 	}
2924 
2925 	if (attrzp)
2926 		mutex_exit(&attrzp->z_lock);
2927 
2928 	if (mask & AT_ATIME)
2929 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
2930 
2931 	if (mask & AT_MTIME)
2932 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
2933 
2934 	/* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2935 	if (mask & AT_SIZE)
2936 		zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
2937 	else if (mask != 0)
2938 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
2939 	/*
2940 	 * Do this after setting timestamps to prevent timestamp
2941 	 * update from toggling bit
2942 	 */
2943 
2944 	if (xoap && (mask & AT_XVATTR)) {
2945 
2946 		/*
2947 		 * restore trimmed off masks
2948 		 * so that return masks can be set for caller.
2949 		 */
2950 
2951 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
2952 			XVA_SET_REQ(xvap, XAT_APPENDONLY);
2953 		}
2954 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
2955 			XVA_SET_REQ(xvap, XAT_NOUNLINK);
2956 		}
2957 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
2958 			XVA_SET_REQ(xvap, XAT_IMMUTABLE);
2959 		}
2960 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
2961 			XVA_SET_REQ(xvap, XAT_NODUMP);
2962 		}
2963 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
2964 			XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
2965 		}
2966 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
2967 			XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
2968 		}
2969 
2970 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
2971 			size_t len;
2972 			dmu_object_info_t doi;
2973 
2974 			ASSERT(vp->v_type == VREG);
2975 
2976 			/* Grow the bonus buffer if necessary. */
2977 			dmu_object_info_from_db(zp->z_dbuf, &doi);
2978 			len = sizeof (xoap->xoa_av_scanstamp) +
2979 			    sizeof (znode_phys_t);
2980 			if (len > doi.doi_bonus_size)
2981 				VERIFY(dmu_set_bonus(zp->z_dbuf, len, tx) == 0);
2982 		}
2983 		zfs_xvattr_set(zp, xvap);
2984 	}
2985 
2986 	if (fuid_dirtied)
2987 		zfs_fuid_sync(zfsvfs, tx);
2988 
2989 	if (mask != 0)
2990 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
2991 
2992 	mutex_exit(&zp->z_lock);
2993 
2994 out:
2995 	if (attrzp)
2996 		VN_RELE(ZTOV(attrzp));
2997 
2998 	if (aclp)
2999 		zfs_acl_free(aclp);
3000 
3001 	if (fuidp) {
3002 		zfs_fuid_info_free(fuidp);
3003 		fuidp = NULL;
3004 	}
3005 
3006 	if (err)
3007 		dmu_tx_abort(tx);
3008 	else
3009 		dmu_tx_commit(tx);
3010 
3011 	if (err == ERESTART)
3012 		goto top;
3013 
3014 	ZFS_EXIT(zfsvfs);
3015 	return (err);
3016 }
3017 
3018 typedef struct zfs_zlock {
3019 	krwlock_t	*zl_rwlock;	/* lock we acquired */
3020 	znode_t		*zl_znode;	/* znode we held */
3021 	struct zfs_zlock *zl_next;	/* next in list */
3022 } zfs_zlock_t;
3023 
3024 /*
3025  * Drop locks and release vnodes that were held by zfs_rename_lock().
3026  */
3027 static void
3028 zfs_rename_unlock(zfs_zlock_t **zlpp)
3029 {
3030 	zfs_zlock_t *zl;
3031 
3032 	while ((zl = *zlpp) != NULL) {
3033 		if (zl->zl_znode != NULL)
3034 			VN_RELE(ZTOV(zl->zl_znode));
3035 		rw_exit(zl->zl_rwlock);
3036 		*zlpp = zl->zl_next;
3037 		kmem_free(zl, sizeof (*zl));
3038 	}
3039 }
3040 
3041 /*
3042  * Search back through the directory tree, using the ".." entries.
3043  * Lock each directory in the chain to prevent concurrent renames.
3044  * Fail any attempt to move a directory into one of its own descendants.
3045  * XXX - z_parent_lock can overlap with map or grow locks
3046  */
3047 static int
3048 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3049 {
3050 	zfs_zlock_t	*zl;
3051 	znode_t		*zp = tdzp;
3052 	uint64_t	rootid = zp->z_zfsvfs->z_root;
3053 	uint64_t	*oidp = &zp->z_id;
3054 	krwlock_t	*rwlp = &szp->z_parent_lock;
3055 	krw_t		rw = RW_WRITER;
3056 
3057 	/*
3058 	 * First pass write-locks szp and compares to zp->z_id.
3059 	 * Later passes read-lock zp and compare to zp->z_parent.
3060 	 */
3061 	do {
3062 		if (!rw_tryenter(rwlp, rw)) {
3063 			/*
3064 			 * Another thread is renaming in this path.
3065 			 * Note that if we are a WRITER, we don't have any
3066 			 * parent_locks held yet.
3067 			 */
3068 			if (rw == RW_READER && zp->z_id > szp->z_id) {
3069 				/*
3070 				 * Drop our locks and restart
3071 				 */
3072 				zfs_rename_unlock(&zl);
3073 				*zlpp = NULL;
3074 				zp = tdzp;
3075 				oidp = &zp->z_id;
3076 				rwlp = &szp->z_parent_lock;
3077 				rw = RW_WRITER;
3078 				continue;
3079 			} else {
3080 				/*
3081 				 * Wait for other thread to drop its locks
3082 				 */
3083 				rw_enter(rwlp, rw);
3084 			}
3085 		}
3086 
3087 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3088 		zl->zl_rwlock = rwlp;
3089 		zl->zl_znode = NULL;
3090 		zl->zl_next = *zlpp;
3091 		*zlpp = zl;
3092 
3093 		if (*oidp == szp->z_id)		/* We're a descendant of szp */
3094 			return (EINVAL);
3095 
3096 		if (*oidp == rootid)		/* We've hit the top */
3097 			return (0);
3098 
3099 		if (rw == RW_READER) {		/* i.e. not the first pass */
3100 			int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
3101 			if (error)
3102 				return (error);
3103 			zl->zl_znode = zp;
3104 		}
3105 		oidp = &zp->z_phys->zp_parent;
3106 		rwlp = &zp->z_parent_lock;
3107 		rw = RW_READER;
3108 
3109 	} while (zp->z_id != sdzp->z_id);
3110 
3111 	return (0);
3112 }
3113 
3114 /*
3115  * Move an entry from the provided source directory to the target
3116  * directory.  Change the entry name as indicated.
3117  *
3118  *	IN:	sdvp	- Source directory containing the "old entry".
3119  *		snm	- Old entry name.
3120  *		tdvp	- Target directory to contain the "new entry".
3121  *		tnm	- New entry name.
3122  *		cr	- credentials of caller.
3123  *		ct	- caller context
3124  *		flags	- case flags
3125  *
3126  *	RETURN:	0 if success
3127  *		error code if failure
3128  *
3129  * Timestamps:
3130  *	sdvp,tdvp - ctime|mtime updated
3131  */
3132 /*ARGSUSED*/
3133 static int
3134 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3135     caller_context_t *ct, int flags)
3136 {
3137 	znode_t		*tdzp, *szp, *tzp;
3138 	znode_t		*sdzp = VTOZ(sdvp);
3139 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
3140 	zilog_t		*zilog;
3141 	vnode_t		*realvp;
3142 	zfs_dirlock_t	*sdl, *tdl;
3143 	dmu_tx_t	*tx;
3144 	zfs_zlock_t	*zl;
3145 	int		cmp, serr, terr;
3146 	int		error = 0;
3147 	int		zflg = 0;
3148 
3149 	ZFS_ENTER(zfsvfs);
3150 	ZFS_VERIFY_ZP(sdzp);
3151 	zilog = zfsvfs->z_log;
3152 
3153 	/*
3154 	 * Make sure we have the real vp for the target directory.
3155 	 */
3156 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3157 		tdvp = realvp;
3158 
3159 	if (tdvp->v_vfsp != sdvp->v_vfsp) {
3160 		ZFS_EXIT(zfsvfs);
3161 		return (EXDEV);
3162 	}
3163 
3164 	tdzp = VTOZ(tdvp);
3165 	ZFS_VERIFY_ZP(tdzp);
3166 	if (zfsvfs->z_utf8 && u8_validate(tnm,
3167 	    strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3168 		ZFS_EXIT(zfsvfs);
3169 		return (EILSEQ);
3170 	}
3171 
3172 	if (flags & FIGNORECASE)
3173 		zflg |= ZCILOOK;
3174 
3175 top:
3176 	szp = NULL;
3177 	tzp = NULL;
3178 	zl = NULL;
3179 
3180 	/*
3181 	 * This is to prevent the creation of links into attribute space
3182 	 * by renaming a linked file into/outof an attribute directory.
3183 	 * See the comment in zfs_link() for why this is considered bad.
3184 	 */
3185 	if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
3186 	    (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
3187 		ZFS_EXIT(zfsvfs);
3188 		return (EINVAL);
3189 	}
3190 
3191 	/*
3192 	 * Lock source and target directory entries.  To prevent deadlock,
3193 	 * a lock ordering must be defined.  We lock the directory with
3194 	 * the smallest object id first, or if it's a tie, the one with
3195 	 * the lexically first name.
3196 	 */
3197 	if (sdzp->z_id < tdzp->z_id) {
3198 		cmp = -1;
3199 	} else if (sdzp->z_id > tdzp->z_id) {
3200 		cmp = 1;
3201 	} else {
3202 		/*
3203 		 * First compare the two name arguments without
3204 		 * considering any case folding.
3205 		 */
3206 		int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3207 
3208 		cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3209 		ASSERT(error == 0 || !zfsvfs->z_utf8);
3210 		if (cmp == 0) {
3211 			/*
3212 			 * POSIX: "If the old argument and the new argument
3213 			 * both refer to links to the same existing file,
3214 			 * the rename() function shall return successfully
3215 			 * and perform no other action."
3216 			 */
3217 			ZFS_EXIT(zfsvfs);
3218 			return (0);
3219 		}
3220 		/*
3221 		 * If the file system is case-folding, then we may
3222 		 * have some more checking to do.  A case-folding file
3223 		 * system is either supporting mixed case sensitivity
3224 		 * access or is completely case-insensitive.  Note
3225 		 * that the file system is always case preserving.
3226 		 *
3227 		 * In mixed sensitivity mode case sensitive behavior
3228 		 * is the default.  FIGNORECASE must be used to
3229 		 * explicitly request case insensitive behavior.
3230 		 *
3231 		 * If the source and target names provided differ only
3232 		 * by case (e.g., a request to rename 'tim' to 'Tim'),
3233 		 * we will treat this as a special case in the
3234 		 * case-insensitive mode: as long as the source name
3235 		 * is an exact match, we will allow this to proceed as
3236 		 * a name-change request.
3237 		 */
3238 		if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3239 		    (zfsvfs->z_case == ZFS_CASE_MIXED &&
3240 		    flags & FIGNORECASE)) &&
3241 		    u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3242 		    &error) == 0) {
3243 			/*
3244 			 * case preserving rename request, require exact
3245 			 * name matches
3246 			 */
3247 			zflg |= ZCIEXACT;
3248 			zflg &= ~ZCILOOK;
3249 		}
3250 	}
3251 
3252 	/*
3253 	 * If the source and destination directories are the same, we should
3254 	 * grab the z_name_lock of that directory only once.
3255 	 */
3256 	if (sdzp == tdzp) {
3257 		zflg |= ZHAVELOCK;
3258 		rw_enter(&sdzp->z_name_lock, RW_READER);
3259 	}
3260 
3261 	if (cmp < 0) {
3262 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3263 		    ZEXISTS | zflg, NULL, NULL);
3264 		terr = zfs_dirent_lock(&tdl,
3265 		    tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3266 	} else {
3267 		terr = zfs_dirent_lock(&tdl,
3268 		    tdzp, tnm, &tzp, zflg, NULL, NULL);
3269 		serr = zfs_dirent_lock(&sdl,
3270 		    sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3271 		    NULL, NULL);
3272 	}
3273 
3274 	if (serr) {
3275 		/*
3276 		 * Source entry invalid or not there.
3277 		 */
3278 		if (!terr) {
3279 			zfs_dirent_unlock(tdl);
3280 			if (tzp)
3281 				VN_RELE(ZTOV(tzp));
3282 		}
3283 
3284 		if (sdzp == tdzp)
3285 			rw_exit(&sdzp->z_name_lock);
3286 
3287 		if (strcmp(snm, "..") == 0)
3288 			serr = EINVAL;
3289 		ZFS_EXIT(zfsvfs);
3290 		return (serr);
3291 	}
3292 	if (terr) {
3293 		zfs_dirent_unlock(sdl);
3294 		VN_RELE(ZTOV(szp));
3295 
3296 		if (sdzp == tdzp)
3297 			rw_exit(&sdzp->z_name_lock);
3298 
3299 		if (strcmp(tnm, "..") == 0)
3300 			terr = EINVAL;
3301 		ZFS_EXIT(zfsvfs);
3302 		return (terr);
3303 	}
3304 
3305 	/*
3306 	 * Must have write access at the source to remove the old entry
3307 	 * and write access at the target to create the new entry.
3308 	 * Note that if target and source are the same, this can be
3309 	 * done in a single check.
3310 	 */
3311 
3312 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3313 		goto out;
3314 
3315 	if (ZTOV(szp)->v_type == VDIR) {
3316 		/*
3317 		 * Check to make sure rename is valid.
3318 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3319 		 */
3320 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3321 			goto out;
3322 	}
3323 
3324 	/*
3325 	 * Does target exist?
3326 	 */
3327 	if (tzp) {
3328 		/*
3329 		 * Source and target must be the same type.
3330 		 */
3331 		if (ZTOV(szp)->v_type == VDIR) {
3332 			if (ZTOV(tzp)->v_type != VDIR) {
3333 				error = ENOTDIR;
3334 				goto out;
3335 			}
3336 		} else {
3337 			if (ZTOV(tzp)->v_type == VDIR) {
3338 				error = EISDIR;
3339 				goto out;
3340 			}
3341 		}
3342 		/*
3343 		 * POSIX dictates that when the source and target
3344 		 * entries refer to the same file object, rename
3345 		 * must do nothing and exit without error.
3346 		 */
3347 		if (szp->z_id == tzp->z_id) {
3348 			error = 0;
3349 			goto out;
3350 		}
3351 	}
3352 
3353 	vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3354 	if (tzp)
3355 		vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3356 
3357 	/*
3358 	 * notify the target directory if it is not the same
3359 	 * as source directory.
3360 	 */
3361 	if (tdvp != sdvp) {
3362 		vnevent_rename_dest_dir(tdvp, ct);
3363 	}
3364 
3365 	tx = dmu_tx_create(zfsvfs->z_os);
3366 	dmu_tx_hold_bonus(tx, szp->z_id);	/* nlink changes */
3367 	dmu_tx_hold_bonus(tx, sdzp->z_id);	/* nlink changes */
3368 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3369 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3370 	if (sdzp != tdzp)
3371 		dmu_tx_hold_bonus(tx, tdzp->z_id);	/* nlink changes */
3372 	if (tzp)
3373 		dmu_tx_hold_bonus(tx, tzp->z_id);	/* parent changes */
3374 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3375 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3376 	if (error) {
3377 		if (zl != NULL)
3378 			zfs_rename_unlock(&zl);
3379 		zfs_dirent_unlock(sdl);
3380 		zfs_dirent_unlock(tdl);
3381 
3382 		if (sdzp == tdzp)
3383 			rw_exit(&sdzp->z_name_lock);
3384 
3385 		VN_RELE(ZTOV(szp));
3386 		if (tzp)
3387 			VN_RELE(ZTOV(tzp));
3388 		if (error == ERESTART) {
3389 			dmu_tx_wait(tx);
3390 			dmu_tx_abort(tx);
3391 			goto top;
3392 		}
3393 		dmu_tx_abort(tx);
3394 		ZFS_EXIT(zfsvfs);
3395 		return (error);
3396 	}
3397 
3398 	if (tzp)	/* Attempt to remove the existing target */
3399 		error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3400 
3401 	if (error == 0) {
3402 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3403 		if (error == 0) {
3404 			szp->z_phys->zp_flags |= ZFS_AV_MODIFIED;
3405 
3406 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3407 			ASSERT(error == 0);
3408 
3409 			zfs_log_rename(zilog, tx,
3410 			    TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0),
3411 			    sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
3412 
3413 			/* Update path information for the target vnode */
3414 			vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm));
3415 		}
3416 	}
3417 
3418 	dmu_tx_commit(tx);
3419 out:
3420 	if (zl != NULL)
3421 		zfs_rename_unlock(&zl);
3422 
3423 	zfs_dirent_unlock(sdl);
3424 	zfs_dirent_unlock(tdl);
3425 
3426 	if (sdzp == tdzp)
3427 		rw_exit(&sdzp->z_name_lock);
3428 
3429 
3430 	VN_RELE(ZTOV(szp));
3431 	if (tzp)
3432 		VN_RELE(ZTOV(tzp));
3433 
3434 	ZFS_EXIT(zfsvfs);
3435 	return (error);
3436 }
3437 
3438 /*
3439  * Insert the indicated symbolic reference entry into the directory.
3440  *
3441  *	IN:	dvp	- Directory to contain new symbolic link.
3442  *		link	- Name for new symlink entry.
3443  *		vap	- Attributes of new entry.
3444  *		target	- Target path of new symlink.
3445  *		cr	- credentials of caller.
3446  *		ct	- caller context
3447  *		flags	- case flags
3448  *
3449  *	RETURN:	0 if success
3450  *		error code if failure
3451  *
3452  * Timestamps:
3453  *	dvp - ctime|mtime updated
3454  */
3455 /*ARGSUSED*/
3456 static int
3457 zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
3458     caller_context_t *ct, int flags)
3459 {
3460 	znode_t		*zp, *dzp = VTOZ(dvp);
3461 	zfs_dirlock_t	*dl;
3462 	dmu_tx_t	*tx;
3463 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
3464 	zilog_t		*zilog;
3465 	int		len = strlen(link);
3466 	int		error;
3467 	int		zflg = ZNEW;
3468 	zfs_acl_ids_t	acl_ids;
3469 	boolean_t	fuid_dirtied;
3470 
3471 	ASSERT(vap->va_type == VLNK);
3472 
3473 	ZFS_ENTER(zfsvfs);
3474 	ZFS_VERIFY_ZP(dzp);
3475 	zilog = zfsvfs->z_log;
3476 
3477 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3478 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3479 		ZFS_EXIT(zfsvfs);
3480 		return (EILSEQ);
3481 	}
3482 	if (flags & FIGNORECASE)
3483 		zflg |= ZCILOOK;
3484 top:
3485 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3486 		ZFS_EXIT(zfsvfs);
3487 		return (error);
3488 	}
3489 
3490 	if (len > MAXPATHLEN) {
3491 		ZFS_EXIT(zfsvfs);
3492 		return (ENAMETOOLONG);
3493 	}
3494 
3495 	/*
3496 	 * Attempt to lock directory; fail if entry already exists.
3497 	 */
3498 	error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3499 	if (error) {
3500 		ZFS_EXIT(zfsvfs);
3501 		return (error);
3502 	}
3503 
3504 	VERIFY(0 == zfs_acl_ids_create(dzp, 0, vap, cr, NULL, &acl_ids));
3505 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
3506 		zfs_acl_ids_free(&acl_ids);
3507 		zfs_dirent_unlock(dl);
3508 		ZFS_EXIT(zfsvfs);
3509 		return (EDQUOT);
3510 	}
3511 	tx = dmu_tx_create(zfsvfs->z_os);
3512 	fuid_dirtied = zfsvfs->z_fuid_dirty;
3513 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3514 	dmu_tx_hold_bonus(tx, dzp->z_id);
3515 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3516 	if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE)
3517 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
3518 	if (fuid_dirtied)
3519 		zfs_fuid_txhold(zfsvfs, tx);
3520 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3521 	if (error) {
3522 		zfs_acl_ids_free(&acl_ids);
3523 		zfs_dirent_unlock(dl);
3524 		if (error == ERESTART) {
3525 			dmu_tx_wait(tx);
3526 			dmu_tx_abort(tx);
3527 			goto top;
3528 		}
3529 		dmu_tx_abort(tx);
3530 		ZFS_EXIT(zfsvfs);
3531 		return (error);
3532 	}
3533 
3534 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
3535 
3536 	/*
3537 	 * Create a new object for the symlink.
3538 	 * Put the link content into bonus buffer if it will fit;
3539 	 * otherwise, store it just like any other file data.
3540 	 */
3541 	if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
3542 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, len, &acl_ids);
3543 		if (len != 0)
3544 			bcopy(link, zp->z_phys + 1, len);
3545 	} else {
3546 		dmu_buf_t *dbp;
3547 
3548 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids);
3549 
3550 		if (fuid_dirtied)
3551 			zfs_fuid_sync(zfsvfs, tx);
3552 		/*
3553 		 * Nothing can access the znode yet so no locking needed
3554 		 * for growing the znode's blocksize.
3555 		 */
3556 		zfs_grow_blocksize(zp, len, tx);
3557 
3558 		VERIFY(0 == dmu_buf_hold(zfsvfs->z_os,
3559 		    zp->z_id, 0, FTAG, &dbp));
3560 		dmu_buf_will_dirty(dbp, tx);
3561 
3562 		ASSERT3U(len, <=, dbp->db_size);
3563 		bcopy(link, dbp->db_data, len);
3564 		dmu_buf_rele(dbp, FTAG);
3565 	}
3566 	zp->z_phys->zp_size = len;
3567 
3568 	/*
3569 	 * Insert the new object into the directory.
3570 	 */
3571 	(void) zfs_link_create(dl, zp, tx, ZNEW);
3572 	if (error == 0) {
3573 		uint64_t txtype = TX_SYMLINK;
3574 		if (flags & FIGNORECASE)
3575 			txtype |= TX_CI;
3576 		zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3577 	}
3578 
3579 	zfs_acl_ids_free(&acl_ids);
3580 
3581 	dmu_tx_commit(tx);
3582 
3583 	zfs_dirent_unlock(dl);
3584 
3585 	VN_RELE(ZTOV(zp));
3586 
3587 	ZFS_EXIT(zfsvfs);
3588 	return (error);
3589 }
3590 
3591 /*
3592  * Return, in the buffer contained in the provided uio structure,
3593  * the symbolic path referred to by vp.
3594  *
3595  *	IN:	vp	- vnode of symbolic link.
3596  *		uoip	- structure to contain the link path.
3597  *		cr	- credentials of caller.
3598  *		ct	- caller context
3599  *
3600  *	OUT:	uio	- structure to contain the link path.
3601  *
3602  *	RETURN:	0 if success
3603  *		error code if failure
3604  *
3605  * Timestamps:
3606  *	vp - atime updated
3607  */
3608 /* ARGSUSED */
3609 static int
3610 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3611 {
3612 	znode_t		*zp = VTOZ(vp);
3613 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3614 	size_t		bufsz;
3615 	int		error;
3616 
3617 	ZFS_ENTER(zfsvfs);
3618 	ZFS_VERIFY_ZP(zp);
3619 
3620 	bufsz = (size_t)zp->z_phys->zp_size;
3621 	if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
3622 		error = uiomove(zp->z_phys + 1,
3623 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
3624 	} else {
3625 		dmu_buf_t *dbp;
3626 		error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
3627 		if (error) {
3628 			ZFS_EXIT(zfsvfs);
3629 			return (error);
3630 		}
3631 		error = uiomove(dbp->db_data,
3632 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
3633 		dmu_buf_rele(dbp, FTAG);
3634 	}
3635 
3636 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3637 	ZFS_EXIT(zfsvfs);
3638 	return (error);
3639 }
3640 
3641 /*
3642  * Insert a new entry into directory tdvp referencing svp.
3643  *
3644  *	IN:	tdvp	- Directory to contain new entry.
3645  *		svp	- vnode of new entry.
3646  *		name	- name of new entry.
3647  *		cr	- credentials of caller.
3648  *		ct	- caller context
3649  *
3650  *	RETURN:	0 if success
3651  *		error code if failure
3652  *
3653  * Timestamps:
3654  *	tdvp - ctime|mtime updated
3655  *	 svp - ctime updated
3656  */
3657 /* ARGSUSED */
3658 static int
3659 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
3660     caller_context_t *ct, int flags)
3661 {
3662 	znode_t		*dzp = VTOZ(tdvp);
3663 	znode_t		*tzp, *szp;
3664 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
3665 	zilog_t		*zilog;
3666 	zfs_dirlock_t	*dl;
3667 	dmu_tx_t	*tx;
3668 	vnode_t		*realvp;
3669 	int		error;
3670 	int		zf = ZNEW;
3671 	uid_t		owner;
3672 
3673 	ASSERT(tdvp->v_type == VDIR);
3674 
3675 	ZFS_ENTER(zfsvfs);
3676 	ZFS_VERIFY_ZP(dzp);
3677 	zilog = zfsvfs->z_log;
3678 
3679 	if (VOP_REALVP(svp, &realvp, ct) == 0)
3680 		svp = realvp;
3681 
3682 	if (svp->v_vfsp != tdvp->v_vfsp) {
3683 		ZFS_EXIT(zfsvfs);
3684 		return (EXDEV);
3685 	}
3686 	szp = VTOZ(svp);
3687 	ZFS_VERIFY_ZP(szp);
3688 
3689 	if (zfsvfs->z_utf8 && u8_validate(name,
3690 	    strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3691 		ZFS_EXIT(zfsvfs);
3692 		return (EILSEQ);
3693 	}
3694 	if (flags & FIGNORECASE)
3695 		zf |= ZCILOOK;
3696 
3697 top:
3698 	/*
3699 	 * We do not support links between attributes and non-attributes
3700 	 * because of the potential security risk of creating links
3701 	 * into "normal" file space in order to circumvent restrictions
3702 	 * imposed in attribute space.
3703 	 */
3704 	if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
3705 	    (dzp->z_phys->zp_flags & ZFS_XATTR)) {
3706 		ZFS_EXIT(zfsvfs);
3707 		return (EINVAL);
3708 	}
3709 
3710 	/*
3711 	 * POSIX dictates that we return EPERM here.
3712 	 * Better choices include ENOTSUP or EISDIR.
3713 	 */
3714 	if (svp->v_type == VDIR) {
3715 		ZFS_EXIT(zfsvfs);
3716 		return (EPERM);
3717 	}
3718 
3719 	owner = zfs_fuid_map_id(zfsvfs, szp->z_phys->zp_uid, cr, ZFS_OWNER);
3720 	if (owner != crgetuid(cr) &&
3721 	    secpolicy_basic_link(cr) != 0) {
3722 		ZFS_EXIT(zfsvfs);
3723 		return (EPERM);
3724 	}
3725 
3726 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3727 		ZFS_EXIT(zfsvfs);
3728 		return (error);
3729 	}
3730 
3731 	/*
3732 	 * Attempt to lock directory; fail if entry already exists.
3733 	 */
3734 	error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
3735 	if (error) {
3736 		ZFS_EXIT(zfsvfs);
3737 		return (error);
3738 	}
3739 
3740 	tx = dmu_tx_create(zfsvfs->z_os);
3741 	dmu_tx_hold_bonus(tx, szp->z_id);
3742 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3743 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3744 	if (error) {
3745 		zfs_dirent_unlock(dl);
3746 		if (error == ERESTART) {
3747 			dmu_tx_wait(tx);
3748 			dmu_tx_abort(tx);
3749 			goto top;
3750 		}
3751 		dmu_tx_abort(tx);
3752 		ZFS_EXIT(zfsvfs);
3753 		return (error);
3754 	}
3755 
3756 	error = zfs_link_create(dl, szp, tx, 0);
3757 
3758 	if (error == 0) {
3759 		uint64_t txtype = TX_LINK;
3760 		if (flags & FIGNORECASE)
3761 			txtype |= TX_CI;
3762 		zfs_log_link(zilog, tx, txtype, dzp, szp, name);
3763 	}
3764 
3765 	dmu_tx_commit(tx);
3766 
3767 	zfs_dirent_unlock(dl);
3768 
3769 	if (error == 0) {
3770 		vnevent_link(svp, ct);
3771 	}
3772 
3773 	ZFS_EXIT(zfsvfs);
3774 	return (error);
3775 }
3776 
3777 /*
3778  * zfs_null_putapage() is used when the file system has been force
3779  * unmounted. It just drops the pages.
3780  */
3781 /* ARGSUSED */
3782 static int
3783 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3784 		size_t *lenp, int flags, cred_t *cr)
3785 {
3786 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
3787 	return (0);
3788 }
3789 
3790 /*
3791  * Push a page out to disk, klustering if possible.
3792  *
3793  *	IN:	vp	- file to push page to.
3794  *		pp	- page to push.
3795  *		flags	- additional flags.
3796  *		cr	- credentials of caller.
3797  *
3798  *	OUT:	offp	- start of range pushed.
3799  *		lenp	- len of range pushed.
3800  *
3801  *	RETURN:	0 if success
3802  *		error code if failure
3803  *
3804  * NOTE: callers must have locked the page to be pushed.  On
3805  * exit, the page (and all other pages in the kluster) must be
3806  * unlocked.
3807  */
3808 /* ARGSUSED */
3809 static int
3810 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3811 		size_t *lenp, int flags, cred_t *cr)
3812 {
3813 	znode_t		*zp = VTOZ(vp);
3814 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3815 	dmu_tx_t	*tx;
3816 	u_offset_t	off, koff;
3817 	size_t		len, klen;
3818 	uint64_t	filesz;
3819 	int		err;
3820 
3821 	filesz = zp->z_phys->zp_size;
3822 	off = pp->p_offset;
3823 	len = PAGESIZE;
3824 	/*
3825 	 * If our blocksize is bigger than the page size, try to kluster
3826 	 * multiple pages so that we write a full block (thus avoiding
3827 	 * a read-modify-write).
3828 	 */
3829 	if (off < filesz && zp->z_blksz > PAGESIZE) {
3830 		klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
3831 		koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
3832 		ASSERT(koff <= filesz);
3833 		if (koff + klen > filesz)
3834 			klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE);
3835 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
3836 	}
3837 	ASSERT3U(btop(len), ==, btopr(len));
3838 
3839 	/*
3840 	 * Can't push pages past end-of-file.
3841 	 */
3842 	if (off >= filesz) {
3843 		/* ignore all pages */
3844 		err = 0;
3845 		goto out;
3846 	} else if (off + len > filesz) {
3847 		int npages = btopr(filesz - off);
3848 		page_t *trunc;
3849 
3850 		page_list_break(&pp, &trunc, npages);
3851 		/* ignore pages past end of file */
3852 		if (trunc)
3853 			pvn_write_done(trunc, flags);
3854 		len = filesz - off;
3855 	}
3856 
3857 	if (zfs_usergroup_overquota(zfsvfs, B_FALSE, zp->z_phys->zp_uid) ||
3858 	    zfs_usergroup_overquota(zfsvfs, B_TRUE, zp->z_phys->zp_gid)) {
3859 		err = EDQUOT;
3860 		goto out;
3861 	}
3862 top:
3863 	tx = dmu_tx_create(zfsvfs->z_os);
3864 	dmu_tx_hold_write(tx, zp->z_id, off, len);
3865 	dmu_tx_hold_bonus(tx, zp->z_id);
3866 	err = dmu_tx_assign(tx, TXG_NOWAIT);
3867 	if (err != 0) {
3868 		if (err == ERESTART) {
3869 			dmu_tx_wait(tx);
3870 			dmu_tx_abort(tx);
3871 			goto top;
3872 		}
3873 		dmu_tx_abort(tx);
3874 		goto out;
3875 	}
3876 
3877 	if (zp->z_blksz <= PAGESIZE) {
3878 		caddr_t va = zfs_map_page(pp, S_READ);
3879 		ASSERT3U(len, <=, PAGESIZE);
3880 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
3881 		zfs_unmap_page(pp, va);
3882 	} else {
3883 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
3884 	}
3885 
3886 	if (err == 0) {
3887 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
3888 		zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
3889 	}
3890 	dmu_tx_commit(tx);
3891 
3892 out:
3893 	pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
3894 	if (offp)
3895 		*offp = off;
3896 	if (lenp)
3897 		*lenp = len;
3898 
3899 	return (err);
3900 }
3901 
3902 /*
3903  * Copy the portion of the file indicated from pages into the file.
3904  * The pages are stored in a page list attached to the files vnode.
3905  *
3906  *	IN:	vp	- vnode of file to push page data to.
3907  *		off	- position in file to put data.
3908  *		len	- amount of data to write.
3909  *		flags	- flags to control the operation.
3910  *		cr	- credentials of caller.
3911  *		ct	- caller context.
3912  *
3913  *	RETURN:	0 if success
3914  *		error code if failure
3915  *
3916  * Timestamps:
3917  *	vp - ctime|mtime updated
3918  */
3919 /*ARGSUSED*/
3920 static int
3921 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
3922     caller_context_t *ct)
3923 {
3924 	znode_t		*zp = VTOZ(vp);
3925 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3926 	page_t		*pp;
3927 	size_t		io_len;
3928 	u_offset_t	io_off;
3929 	uint_t		blksz;
3930 	rl_t		*rl;
3931 	int		error = 0;
3932 
3933 	ZFS_ENTER(zfsvfs);
3934 	ZFS_VERIFY_ZP(zp);
3935 
3936 	/*
3937 	 * Align this request to the file block size in case we kluster.
3938 	 * XXX - this can result in pretty aggresive locking, which can
3939 	 * impact simultanious read/write access.  One option might be
3940 	 * to break up long requests (len == 0) into block-by-block
3941 	 * operations to get narrower locking.
3942 	 */
3943 	blksz = zp->z_blksz;
3944 	if (ISP2(blksz))
3945 		io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
3946 	else
3947 		io_off = 0;
3948 	if (len > 0 && ISP2(blksz))
3949 		io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
3950 	else
3951 		io_len = 0;
3952 
3953 	if (io_len == 0) {
3954 		/*
3955 		 * Search the entire vp list for pages >= io_off.
3956 		 */
3957 		rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
3958 		error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
3959 		goto out;
3960 	}
3961 	rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
3962 
3963 	if (off > zp->z_phys->zp_size) {
3964 		/* past end of file */
3965 		zfs_range_unlock(rl);
3966 		ZFS_EXIT(zfsvfs);
3967 		return (0);
3968 	}
3969 
3970 	len = MIN(io_len, P2ROUNDUP(zp->z_phys->zp_size, PAGESIZE) - io_off);
3971 
3972 	for (off = io_off; io_off < off + len; io_off += io_len) {
3973 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
3974 			pp = page_lookup(vp, io_off,
3975 			    (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
3976 		} else {
3977 			pp = page_lookup_nowait(vp, io_off,
3978 			    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
3979 		}
3980 
3981 		if (pp != NULL && pvn_getdirty(pp, flags)) {
3982 			int err;
3983 
3984 			/*
3985 			 * Found a dirty page to push
3986 			 */
3987 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
3988 			if (err)
3989 				error = err;
3990 		} else {
3991 			io_len = PAGESIZE;
3992 		}
3993 	}
3994 out:
3995 	zfs_range_unlock(rl);
3996 	if ((flags & B_ASYNC) == 0)
3997 		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
3998 	ZFS_EXIT(zfsvfs);
3999 	return (error);
4000 }
4001 
4002 /*ARGSUSED*/
4003 void
4004 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4005 {
4006 	znode_t	*zp = VTOZ(vp);
4007 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4008 	int error;
4009 
4010 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4011 	if (zp->z_dbuf == NULL) {
4012 		/*
4013 		 * The fs has been unmounted, or we did a
4014 		 * suspend/resume and this file no longer exists.
4015 		 */
4016 		if (vn_has_cached_data(vp)) {
4017 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
4018 			    B_INVAL, cr);
4019 		}
4020 
4021 		mutex_enter(&zp->z_lock);
4022 		mutex_enter(&vp->v_lock);
4023 		ASSERT(vp->v_count == 1);
4024 		vp->v_count = 0;
4025 		mutex_exit(&vp->v_lock);
4026 		mutex_exit(&zp->z_lock);
4027 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
4028 		zfs_znode_free(zp);
4029 		return;
4030 	}
4031 
4032 	/*
4033 	 * Attempt to push any data in the page cache.  If this fails
4034 	 * we will get kicked out later in zfs_zinactive().
4035 	 */
4036 	if (vn_has_cached_data(vp)) {
4037 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
4038 		    cr);
4039 	}
4040 
4041 	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4042 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4043 
4044 		dmu_tx_hold_bonus(tx, zp->z_id);
4045 		error = dmu_tx_assign(tx, TXG_WAIT);
4046 		if (error) {
4047 			dmu_tx_abort(tx);
4048 		} else {
4049 			dmu_buf_will_dirty(zp->z_dbuf, tx);
4050 			mutex_enter(&zp->z_lock);
4051 			zp->z_atime_dirty = 0;
4052 			mutex_exit(&zp->z_lock);
4053 			dmu_tx_commit(tx);
4054 		}
4055 	}
4056 
4057 	zfs_zinactive(zp);
4058 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
4059 }
4060 
4061 /*
4062  * Bounds-check the seek operation.
4063  *
4064  *	IN:	vp	- vnode seeking within
4065  *		ooff	- old file offset
4066  *		noffp	- pointer to new file offset
4067  *		ct	- caller context
4068  *
4069  *	RETURN:	0 if success
4070  *		EINVAL if new offset invalid
4071  */
4072 /* ARGSUSED */
4073 static int
4074 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
4075     caller_context_t *ct)
4076 {
4077 	if (vp->v_type == VDIR)
4078 		return (0);
4079 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4080 }
4081 
4082 /*
4083  * Pre-filter the generic locking function to trap attempts to place
4084  * a mandatory lock on a memory mapped file.
4085  */
4086 static int
4087 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
4088     flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4089 {
4090 	znode_t *zp = VTOZ(vp);
4091 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4092 
4093 	ZFS_ENTER(zfsvfs);
4094 	ZFS_VERIFY_ZP(zp);
4095 
4096 	/*
4097 	 * We are following the UFS semantics with respect to mapcnt
4098 	 * here: If we see that the file is mapped already, then we will
4099 	 * return an error, but we don't worry about races between this
4100 	 * function and zfs_map().
4101 	 */
4102 	if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) {
4103 		ZFS_EXIT(zfsvfs);
4104 		return (EAGAIN);
4105 	}
4106 	ZFS_EXIT(zfsvfs);
4107 	return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4108 }
4109 
4110 /*
4111  * If we can't find a page in the cache, we will create a new page
4112  * and fill it with file data.  For efficiency, we may try to fill
4113  * multiple pages at once (klustering) to fill up the supplied page
4114  * list.  Note that the pages to be filled are held with an exclusive
4115  * lock to prevent access by other threads while they are being filled.
4116  */
4117 static int
4118 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4119     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4120 {
4121 	znode_t *zp = VTOZ(vp);
4122 	page_t *pp, *cur_pp;
4123 	objset_t *os = zp->z_zfsvfs->z_os;
4124 	u_offset_t io_off, total;
4125 	size_t io_len;
4126 	int err;
4127 
4128 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
4129 		/*
4130 		 * We only have a single page, don't bother klustering
4131 		 */
4132 		io_off = off;
4133 		io_len = PAGESIZE;
4134 		pp = page_create_va(vp, io_off, io_len,
4135 		    PG_EXCL | PG_WAIT, seg, addr);
4136 	} else {
4137 		/*
4138 		 * Try to find enough pages to fill the page list
4139 		 */
4140 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4141 		    &io_len, off, plsz, 0);
4142 	}
4143 	if (pp == NULL) {
4144 		/*
4145 		 * The page already exists, nothing to do here.
4146 		 */
4147 		*pl = NULL;
4148 		return (0);
4149 	}
4150 
4151 	/*
4152 	 * Fill the pages in the kluster.
4153 	 */
4154 	cur_pp = pp;
4155 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4156 		caddr_t va;
4157 
4158 		ASSERT3U(io_off, ==, cur_pp->p_offset);
4159 		va = zfs_map_page(cur_pp, S_WRITE);
4160 		err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4161 		    DMU_READ_PREFETCH);
4162 		zfs_unmap_page(cur_pp, va);
4163 		if (err) {
4164 			/* On error, toss the entire kluster */
4165 			pvn_read_done(pp, B_ERROR);
4166 			/* convert checksum errors into IO errors */
4167 			if (err == ECKSUM)
4168 				err = EIO;
4169 			return (err);
4170 		}
4171 		cur_pp = cur_pp->p_next;
4172 	}
4173 
4174 	/*
4175 	 * Fill in the page list array from the kluster starting
4176 	 * from the desired offset `off'.
4177 	 * NOTE: the page list will always be null terminated.
4178 	 */
4179 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4180 	ASSERT(pl == NULL || (*pl)->p_offset == off);
4181 
4182 	return (0);
4183 }
4184 
4185 /*
4186  * Return pointers to the pages for the file region [off, off + len]
4187  * in the pl array.  If plsz is greater than len, this function may
4188  * also return page pointers from after the specified region
4189  * (i.e. the region [off, off + plsz]).  These additional pages are
4190  * only returned if they are already in the cache, or were created as
4191  * part of a klustered read.
4192  *
4193  *	IN:	vp	- vnode of file to get data from.
4194  *		off	- position in file to get data from.
4195  *		len	- amount of data to retrieve.
4196  *		plsz	- length of provided page list.
4197  *		seg	- segment to obtain pages for.
4198  *		addr	- virtual address of fault.
4199  *		rw	- mode of created pages.
4200  *		cr	- credentials of caller.
4201  *		ct	- caller context.
4202  *
4203  *	OUT:	protp	- protection mode of created pages.
4204  *		pl	- list of pages created.
4205  *
4206  *	RETURN:	0 if success
4207  *		error code if failure
4208  *
4209  * Timestamps:
4210  *	vp - atime updated
4211  */
4212 /* ARGSUSED */
4213 static int
4214 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4215 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4216 	enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4217 {
4218 	znode_t		*zp = VTOZ(vp);
4219 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4220 	page_t		**pl0 = pl;
4221 	int		err = 0;
4222 
4223 	/* we do our own caching, faultahead is unnecessary */
4224 	if (pl == NULL)
4225 		return (0);
4226 	else if (len > plsz)
4227 		len = plsz;
4228 	else
4229 		len = P2ROUNDUP(len, PAGESIZE);
4230 	ASSERT(plsz >= len);
4231 
4232 	ZFS_ENTER(zfsvfs);
4233 	ZFS_VERIFY_ZP(zp);
4234 
4235 	if (protp)
4236 		*protp = PROT_ALL;
4237 
4238 	/*
4239 	 * Loop through the requested range [off, off + len) looking
4240 	 * for pages.  If we don't find a page, we will need to create
4241 	 * a new page and fill it with data from the file.
4242 	 */
4243 	while (len > 0) {
4244 		if (*pl = page_lookup(vp, off, SE_SHARED))
4245 			*(pl+1) = NULL;
4246 		else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
4247 			goto out;
4248 		while (*pl) {
4249 			ASSERT3U((*pl)->p_offset, ==, off);
4250 			off += PAGESIZE;
4251 			addr += PAGESIZE;
4252 			if (len > 0) {
4253 				ASSERT3U(len, >=, PAGESIZE);
4254 				len -= PAGESIZE;
4255 			}
4256 			ASSERT3U(plsz, >=, PAGESIZE);
4257 			plsz -= PAGESIZE;
4258 			pl++;
4259 		}
4260 	}
4261 
4262 	/*
4263 	 * Fill out the page array with any pages already in the cache.
4264 	 */
4265 	while (plsz > 0 &&
4266 	    (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
4267 			off += PAGESIZE;
4268 			plsz -= PAGESIZE;
4269 	}
4270 out:
4271 	if (err) {
4272 		/*
4273 		 * Release any pages we have previously locked.
4274 		 */
4275 		while (pl > pl0)
4276 			page_unlock(*--pl);
4277 	} else {
4278 		ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4279 	}
4280 
4281 	*pl = NULL;
4282 
4283 	ZFS_EXIT(zfsvfs);
4284 	return (err);
4285 }
4286 
4287 /*
4288  * Request a memory map for a section of a file.  This code interacts
4289  * with common code and the VM system as follows:
4290  *
4291  *	common code calls mmap(), which ends up in smmap_common()
4292  *
4293  *	this calls VOP_MAP(), which takes you into (say) zfs
4294  *
4295  *	zfs_map() calls as_map(), passing segvn_create() as the callback
4296  *
4297  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
4298  *
4299  *	zfs_addmap() updates z_mapcnt
4300  */
4301 /*ARGSUSED*/
4302 static int
4303 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4304     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4305     caller_context_t *ct)
4306 {
4307 	znode_t *zp = VTOZ(vp);
4308 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4309 	segvn_crargs_t	vn_a;
4310 	int		error;
4311 
4312 	ZFS_ENTER(zfsvfs);
4313 	ZFS_VERIFY_ZP(zp);
4314 
4315 	if ((prot & PROT_WRITE) &&
4316 	    (zp->z_phys->zp_flags & (ZFS_IMMUTABLE | ZFS_READONLY |
4317 	    ZFS_APPENDONLY))) {
4318 		ZFS_EXIT(zfsvfs);
4319 		return (EPERM);
4320 	}
4321 
4322 	if ((prot & (PROT_READ | PROT_EXEC)) &&
4323 	    (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED)) {
4324 		ZFS_EXIT(zfsvfs);
4325 		return (EACCES);
4326 	}
4327 
4328 	if (vp->v_flag & VNOMAP) {
4329 		ZFS_EXIT(zfsvfs);
4330 		return (ENOSYS);
4331 	}
4332 
4333 	if (off < 0 || len > MAXOFFSET_T - off) {
4334 		ZFS_EXIT(zfsvfs);
4335 		return (ENXIO);
4336 	}
4337 
4338 	if (vp->v_type != VREG) {
4339 		ZFS_EXIT(zfsvfs);
4340 		return (ENODEV);
4341 	}
4342 
4343 	/*
4344 	 * If file is locked, disallow mapping.
4345 	 */
4346 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) {
4347 		ZFS_EXIT(zfsvfs);
4348 		return (EAGAIN);
4349 	}
4350 
4351 	as_rangelock(as);
4352 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
4353 	if (error != 0) {
4354 		as_rangeunlock(as);
4355 		ZFS_EXIT(zfsvfs);
4356 		return (error);
4357 	}
4358 
4359 	vn_a.vp = vp;
4360 	vn_a.offset = (u_offset_t)off;
4361 	vn_a.type = flags & MAP_TYPE;
4362 	vn_a.prot = prot;
4363 	vn_a.maxprot = maxprot;
4364 	vn_a.cred = cr;
4365 	vn_a.amp = NULL;
4366 	vn_a.flags = flags & ~MAP_TYPE;
4367 	vn_a.szc = 0;
4368 	vn_a.lgrp_mem_policy_flags = 0;
4369 
4370 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
4371 
4372 	as_rangeunlock(as);
4373 	ZFS_EXIT(zfsvfs);
4374 	return (error);
4375 }
4376 
4377 /* ARGSUSED */
4378 static int
4379 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4380     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4381     caller_context_t *ct)
4382 {
4383 	uint64_t pages = btopr(len);
4384 
4385 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4386 	return (0);
4387 }
4388 
4389 /*
4390  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
4391  * more accurate mtime for the associated file.  Since we don't have a way of
4392  * detecting when the data was actually modified, we have to resort to
4393  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
4394  * last page is pushed.  The problem occurs when the msync() call is omitted,
4395  * which by far the most common case:
4396  *
4397  * 	open()
4398  * 	mmap()
4399  * 	<modify memory>
4400  * 	munmap()
4401  * 	close()
4402  * 	<time lapse>
4403  * 	putpage() via fsflush
4404  *
4405  * If we wait until fsflush to come along, we can have a modification time that
4406  * is some arbitrary point in the future.  In order to prevent this in the
4407  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
4408  * torn down.
4409  */
4410 /* ARGSUSED */
4411 static int
4412 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4413     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
4414     caller_context_t *ct)
4415 {
4416 	uint64_t pages = btopr(len);
4417 
4418 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
4419 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
4420 
4421 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
4422 	    vn_has_cached_data(vp))
4423 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
4424 
4425 	return (0);
4426 }
4427 
4428 /*
4429  * Free or allocate space in a file.  Currently, this function only
4430  * supports the `F_FREESP' command.  However, this command is somewhat
4431  * misnamed, as its functionality includes the ability to allocate as
4432  * well as free space.
4433  *
4434  *	IN:	vp	- vnode of file to free data in.
4435  *		cmd	- action to take (only F_FREESP supported).
4436  *		bfp	- section of file to free/alloc.
4437  *		flag	- current file open mode flags.
4438  *		offset	- current file offset.
4439  *		cr	- credentials of caller [UNUSED].
4440  *		ct	- caller context.
4441  *
4442  *	RETURN:	0 if success
4443  *		error code if failure
4444  *
4445  * Timestamps:
4446  *	vp - ctime|mtime updated
4447  */
4448 /* ARGSUSED */
4449 static int
4450 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4451     offset_t offset, cred_t *cr, caller_context_t *ct)
4452 {
4453 	znode_t		*zp = VTOZ(vp);
4454 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4455 	uint64_t	off, len;
4456 	int		error;
4457 
4458 	ZFS_ENTER(zfsvfs);
4459 	ZFS_VERIFY_ZP(zp);
4460 
4461 	if (cmd != F_FREESP) {
4462 		ZFS_EXIT(zfsvfs);
4463 		return (EINVAL);
4464 	}
4465 
4466 	if (error = convoff(vp, bfp, 0, offset)) {
4467 		ZFS_EXIT(zfsvfs);
4468 		return (error);
4469 	}
4470 
4471 	if (bfp->l_len < 0) {
4472 		ZFS_EXIT(zfsvfs);
4473 		return (EINVAL);
4474 	}
4475 
4476 	off = bfp->l_start;
4477 	len = bfp->l_len; /* 0 means from off to end of file */
4478 
4479 	error = zfs_freesp(zp, off, len, flag, TRUE);
4480 
4481 	ZFS_EXIT(zfsvfs);
4482 	return (error);
4483 }
4484 
4485 /*ARGSUSED*/
4486 static int
4487 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4488 {
4489 	znode_t		*zp = VTOZ(vp);
4490 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4491 	uint32_t	gen;
4492 	uint64_t	object = zp->z_id;
4493 	zfid_short_t	*zfid;
4494 	int		size, i;
4495 
4496 	ZFS_ENTER(zfsvfs);
4497 	ZFS_VERIFY_ZP(zp);
4498 	gen = (uint32_t)zp->z_gen;
4499 
4500 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4501 	if (fidp->fid_len < size) {
4502 		fidp->fid_len = size;
4503 		ZFS_EXIT(zfsvfs);
4504 		return (ENOSPC);
4505 	}
4506 
4507 	zfid = (zfid_short_t *)fidp;
4508 
4509 	zfid->zf_len = size;
4510 
4511 	for (i = 0; i < sizeof (zfid->zf_object); i++)
4512 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4513 
4514 	/* Must have a non-zero generation number to distinguish from .zfs */
4515 	if (gen == 0)
4516 		gen = 1;
4517 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
4518 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4519 
4520 	if (size == LONG_FID_LEN) {
4521 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
4522 		zfid_long_t	*zlfid;
4523 
4524 		zlfid = (zfid_long_t *)fidp;
4525 
4526 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4527 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4528 
4529 		/* XXX - this should be the generation number for the objset */
4530 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4531 			zlfid->zf_setgen[i] = 0;
4532 	}
4533 
4534 	ZFS_EXIT(zfsvfs);
4535 	return (0);
4536 }
4537 
4538 static int
4539 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4540     caller_context_t *ct)
4541 {
4542 	znode_t		*zp, *xzp;
4543 	zfsvfs_t	*zfsvfs;
4544 	zfs_dirlock_t	*dl;
4545 	int		error;
4546 
4547 	switch (cmd) {
4548 	case _PC_LINK_MAX:
4549 		*valp = ULONG_MAX;
4550 		return (0);
4551 
4552 	case _PC_FILESIZEBITS:
4553 		*valp = 64;
4554 		return (0);
4555 
4556 	case _PC_XATTR_EXISTS:
4557 		zp = VTOZ(vp);
4558 		zfsvfs = zp->z_zfsvfs;
4559 		ZFS_ENTER(zfsvfs);
4560 		ZFS_VERIFY_ZP(zp);
4561 		*valp = 0;
4562 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
4563 		    ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4564 		if (error == 0) {
4565 			zfs_dirent_unlock(dl);
4566 			if (!zfs_dirempty(xzp))
4567 				*valp = 1;
4568 			VN_RELE(ZTOV(xzp));
4569 		} else if (error == ENOENT) {
4570 			/*
4571 			 * If there aren't extended attributes, it's the
4572 			 * same as having zero of them.
4573 			 */
4574 			error = 0;
4575 		}
4576 		ZFS_EXIT(zfsvfs);
4577 		return (error);
4578 
4579 	case _PC_SATTR_ENABLED:
4580 	case _PC_SATTR_EXISTS:
4581 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
4582 		    (vp->v_type == VREG || vp->v_type == VDIR);
4583 		return (0);
4584 
4585 	case _PC_ACCESS_FILTERING:
4586 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
4587 		    vp->v_type == VDIR;
4588 		return (0);
4589 
4590 	case _PC_ACL_ENABLED:
4591 		*valp = _ACL_ACE_ENABLED;
4592 		return (0);
4593 
4594 	case _PC_MIN_HOLE_SIZE:
4595 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
4596 		return (0);
4597 
4598 	case _PC_TIMESTAMP_RESOLUTION:
4599 		/* nanosecond timestamp resolution */
4600 		*valp = 1L;
4601 		return (0);
4602 
4603 	default:
4604 		return (fs_pathconf(vp, cmd, valp, cr, ct));
4605 	}
4606 }
4607 
4608 /*ARGSUSED*/
4609 static int
4610 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4611     caller_context_t *ct)
4612 {
4613 	znode_t *zp = VTOZ(vp);
4614 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4615 	int error;
4616 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4617 
4618 	ZFS_ENTER(zfsvfs);
4619 	ZFS_VERIFY_ZP(zp);
4620 	error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4621 	ZFS_EXIT(zfsvfs);
4622 
4623 	return (error);
4624 }
4625 
4626 /*ARGSUSED*/
4627 static int
4628 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4629     caller_context_t *ct)
4630 {
4631 	znode_t *zp = VTOZ(vp);
4632 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4633 	int error;
4634 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4635 
4636 	ZFS_ENTER(zfsvfs);
4637 	ZFS_VERIFY_ZP(zp);
4638 	error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4639 	ZFS_EXIT(zfsvfs);
4640 	return (error);
4641 }
4642 
4643 /*
4644  * Tunable, both must be a power of 2.
4645  *
4646  * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4647  * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4648  *                an arcbuf for a partial block read
4649  */
4650 int zcr_blksz_min = (1 << 10);	/* 1K */
4651 int zcr_blksz_max = (1 << 17);	/* 128K */
4652 
4653 /*ARGSUSED*/
4654 static int
4655 zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
4656     caller_context_t *ct)
4657 {
4658 	znode_t	*zp = VTOZ(vp);
4659 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4660 	int max_blksz = zfsvfs->z_max_blksz;
4661 	uio_t *uio = &xuio->xu_uio;
4662 	ssize_t size = uio->uio_resid;
4663 	offset_t offset = uio->uio_loffset;
4664 	int blksz;
4665 	int fullblk, i;
4666 	arc_buf_t *abuf;
4667 	ssize_t maxsize;
4668 	int preamble, postamble;
4669 
4670 	if (xuio->xu_type != UIOTYPE_ZEROCOPY)
4671 		return (EINVAL);
4672 
4673 	ZFS_ENTER(zfsvfs);
4674 	ZFS_VERIFY_ZP(zp);
4675 	switch (ioflag) {
4676 	case UIO_WRITE:
4677 		/*
4678 		 * Loan out an arc_buf for write if write size is bigger than
4679 		 * max_blksz, and the file's block size is also max_blksz.
4680 		 */
4681 		blksz = max_blksz;
4682 		if (size < blksz || zp->z_blksz != blksz) {
4683 			ZFS_EXIT(zfsvfs);
4684 			return (EINVAL);
4685 		}
4686 		/*
4687 		 * Caller requests buffers for write before knowing where the
4688 		 * write offset might be (e.g. NFS TCP write).
4689 		 */
4690 		if (offset == -1) {
4691 			preamble = 0;
4692 		} else {
4693 			preamble = P2PHASE(offset, blksz);
4694 			if (preamble) {
4695 				preamble = blksz - preamble;
4696 				size -= preamble;
4697 			}
4698 		}
4699 
4700 		postamble = P2PHASE(size, blksz);
4701 		size -= postamble;
4702 
4703 		fullblk = size / blksz;
4704 		(void) dmu_xuio_init(xuio,
4705 		    (preamble != 0) + fullblk + (postamble != 0));
4706 		DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
4707 		    int, postamble, int,
4708 		    (preamble != 0) + fullblk + (postamble != 0));
4709 
4710 		/*
4711 		 * Have to fix iov base/len for partial buffers.  They
4712 		 * currently represent full arc_buf's.
4713 		 */
4714 		if (preamble) {
4715 			/* data begins in the middle of the arc_buf */
4716 			abuf = dmu_request_arcbuf(zp->z_dbuf, blksz);
4717 			ASSERT(abuf);
4718 			(void) dmu_xuio_add(xuio, abuf,
4719 			    blksz - preamble, preamble);
4720 		}
4721 
4722 		for (i = 0; i < fullblk; i++) {
4723 			abuf = dmu_request_arcbuf(zp->z_dbuf, blksz);
4724 			ASSERT(abuf);
4725 			(void) dmu_xuio_add(xuio, abuf, 0, blksz);
4726 		}
4727 
4728 		if (postamble) {
4729 			/* data ends in the middle of the arc_buf */
4730 			abuf = dmu_request_arcbuf(zp->z_dbuf, blksz);
4731 			ASSERT(abuf);
4732 			(void) dmu_xuio_add(xuio, abuf, 0, postamble);
4733 		}
4734 		break;
4735 	case UIO_READ:
4736 		/*
4737 		 * Loan out an arc_buf for read if the read size is larger than
4738 		 * the current file block size.  Block alignment is not
4739 		 * considered.  Partial arc_buf will be loaned out for read.
4740 		 */
4741 		blksz = zp->z_blksz;
4742 		if (blksz < zcr_blksz_min)
4743 			blksz = zcr_blksz_min;
4744 		if (blksz > zcr_blksz_max)
4745 			blksz = zcr_blksz_max;
4746 		/* avoid potential complexity of dealing with it */
4747 		if (blksz > max_blksz) {
4748 			ZFS_EXIT(zfsvfs);
4749 			return (EINVAL);
4750 		}
4751 
4752 		maxsize = zp->z_phys->zp_size - uio->uio_loffset;
4753 		if (size > maxsize)
4754 			size = maxsize;
4755 
4756 		if (size < blksz || vn_has_cached_data(vp)) {
4757 			ZFS_EXIT(zfsvfs);
4758 			return (EINVAL);
4759 		}
4760 		break;
4761 	default:
4762 		ZFS_EXIT(zfsvfs);
4763 		return (EINVAL);
4764 	}
4765 
4766 	uio->uio_extflg = UIO_XUIO;
4767 	XUIO_XUZC_RW(xuio) = ioflag;
4768 	ZFS_EXIT(zfsvfs);
4769 	return (0);
4770 }
4771 
4772 /*ARGSUSED*/
4773 static int
4774 zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
4775 {
4776 	int i;
4777 	arc_buf_t *abuf;
4778 	int ioflag = XUIO_XUZC_RW(xuio);
4779 
4780 	ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
4781 
4782 	i = dmu_xuio_cnt(xuio);
4783 	while (i-- > 0) {
4784 		abuf = dmu_xuio_arcbuf(xuio, i);
4785 		/*
4786 		 * if abuf == NULL, it must be a write buffer
4787 		 * that has been returned in zfs_write().
4788 		 */
4789 		if (abuf)
4790 			dmu_return_arcbuf(abuf);
4791 		ASSERT(abuf || ioflag == UIO_WRITE);
4792 	}
4793 
4794 	dmu_xuio_fini(xuio);
4795 	return (0);
4796 }
4797 
4798 /*
4799  * Predeclare these here so that the compiler assumes that
4800  * this is an "old style" function declaration that does
4801  * not include arguments => we won't get type mismatch errors
4802  * in the initializations that follow.
4803  */
4804 static int zfs_inval();
4805 static int zfs_isdir();
4806 
4807 static int
4808 zfs_inval()
4809 {
4810 	return (EINVAL);
4811 }
4812 
4813 static int
4814 zfs_isdir()
4815 {
4816 	return (EISDIR);
4817 }
4818 /*
4819  * Directory vnode operations template
4820  */
4821 vnodeops_t *zfs_dvnodeops;
4822 const fs_operation_def_t zfs_dvnodeops_template[] = {
4823 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
4824 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
4825 	VOPNAME_READ,		{ .error = zfs_isdir },
4826 	VOPNAME_WRITE,		{ .error = zfs_isdir },
4827 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
4828 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
4829 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
4830 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
4831 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
4832 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
4833 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
4834 	VOPNAME_LINK,		{ .vop_link = zfs_link },
4835 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
4836 	VOPNAME_MKDIR,		{ .vop_mkdir = zfs_mkdir },
4837 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
4838 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
4839 	VOPNAME_SYMLINK,	{ .vop_symlink = zfs_symlink },
4840 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
4841 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
4842 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
4843 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
4844 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
4845 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
4846 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
4847 	VOPNAME_VNEVENT, 	{ .vop_vnevent = fs_vnevent_support },
4848 	NULL,			NULL
4849 };
4850 
4851 /*
4852  * Regular file vnode operations template
4853  */
4854 vnodeops_t *zfs_fvnodeops;
4855 const fs_operation_def_t zfs_fvnodeops_template[] = {
4856 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
4857 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
4858 	VOPNAME_READ,		{ .vop_read = zfs_read },
4859 	VOPNAME_WRITE,		{ .vop_write = zfs_write },
4860 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
4861 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
4862 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
4863 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
4864 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
4865 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
4866 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
4867 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
4868 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
4869 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
4870 	VOPNAME_FRLOCK,		{ .vop_frlock = zfs_frlock },
4871 	VOPNAME_SPACE,		{ .vop_space = zfs_space },
4872 	VOPNAME_GETPAGE,	{ .vop_getpage = zfs_getpage },
4873 	VOPNAME_PUTPAGE,	{ .vop_putpage = zfs_putpage },
4874 	VOPNAME_MAP,		{ .vop_map = zfs_map },
4875 	VOPNAME_ADDMAP,		{ .vop_addmap = zfs_addmap },
4876 	VOPNAME_DELMAP,		{ .vop_delmap = zfs_delmap },
4877 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
4878 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
4879 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
4880 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
4881 	VOPNAME_REQZCBUF, 	{ .vop_reqzcbuf = zfs_reqzcbuf },
4882 	VOPNAME_RETZCBUF, 	{ .vop_retzcbuf = zfs_retzcbuf },
4883 	NULL,			NULL
4884 };
4885 
4886 /*
4887  * Symbolic link vnode operations template
4888  */
4889 vnodeops_t *zfs_symvnodeops;
4890 const fs_operation_def_t zfs_symvnodeops_template[] = {
4891 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
4892 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
4893 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
4894 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
4895 	VOPNAME_READLINK,	{ .vop_readlink = zfs_readlink },
4896 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
4897 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
4898 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
4899 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
4900 	NULL,			NULL
4901 };
4902 
4903 /*
4904  * special share hidden files vnode operations template
4905  */
4906 vnodeops_t *zfs_sharevnodeops;
4907 const fs_operation_def_t zfs_sharevnodeops_template[] = {
4908 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
4909 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
4910 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
4911 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
4912 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
4913 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
4914 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
4915 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
4916 	NULL,			NULL
4917 };
4918 
4919 /*
4920  * Extended attribute directory vnode operations template
4921  *	This template is identical to the directory vnodes
4922  *	operation template except for restricted operations:
4923  *		VOP_MKDIR()
4924  *		VOP_SYMLINK()
4925  * Note that there are other restrictions embedded in:
4926  *	zfs_create()	- restrict type to VREG
4927  *	zfs_link()	- no links into/out of attribute space
4928  *	zfs_rename()	- no moves into/out of attribute space
4929  */
4930 vnodeops_t *zfs_xdvnodeops;
4931 const fs_operation_def_t zfs_xdvnodeops_template[] = {
4932 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
4933 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
4934 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
4935 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
4936 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
4937 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
4938 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
4939 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
4940 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
4941 	VOPNAME_LINK,		{ .vop_link = zfs_link },
4942 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
4943 	VOPNAME_MKDIR,		{ .error = zfs_inval },
4944 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
4945 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
4946 	VOPNAME_SYMLINK,	{ .error = zfs_inval },
4947 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
4948 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
4949 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
4950 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
4951 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
4952 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
4953 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
4954 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
4955 	NULL,			NULL
4956 };
4957 
4958 /*
4959  * Error vnode operations template
4960  */
4961 vnodeops_t *zfs_evnodeops;
4962 const fs_operation_def_t zfs_evnodeops_template[] = {
4963 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
4964 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
4965 	NULL,			NULL
4966 };
4967