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