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