xref: /titanic_52/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision 1a561c7696fb9440160a24ca56e8dd264a4cd8ab)
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 
1037 		if (error == 0) {
1038 			zgd->zgd_db = db;
1039 			zgd->zgd_bp = bp;
1040 
1041 			ASSERT(db->db_offset == offset);
1042 			ASSERT(db->db_size == size);
1043 
1044 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
1045 			    zfs_get_done, zgd);
1046 			ASSERT(error || lr->lr_length <= zp->z_blksz);
1047 
1048 			/*
1049 			 * On success, we need to wait for the write I/O
1050 			 * initiated by dmu_sync() to complete before we can
1051 			 * release this dbuf.  We will finish everything up
1052 			 * in the zfs_get_done() callback.
1053 			 */
1054 			if (error == 0)
1055 				return (0);
1056 
1057 			if (error == EALREADY) {
1058 				lr->lr_common.lrc_txtype = TX_WRITE2;
1059 				error = 0;
1060 			}
1061 		}
1062 	}
1063 
1064 	zfs_get_done(zgd, error);
1065 
1066 	return (error);
1067 }
1068 
1069 /*ARGSUSED*/
1070 static int
1071 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1072     caller_context_t *ct)
1073 {
1074 	znode_t *zp = VTOZ(vp);
1075 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1076 	int error;
1077 
1078 	ZFS_ENTER(zfsvfs);
1079 	ZFS_VERIFY_ZP(zp);
1080 
1081 	if (flag & V_ACE_MASK)
1082 		error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1083 	else
1084 		error = zfs_zaccess_rwx(zp, mode, flag, cr);
1085 
1086 	ZFS_EXIT(zfsvfs);
1087 	return (error);
1088 }
1089 
1090 /*
1091  * If vnode is for a device return a specfs vnode instead.
1092  */
1093 static int
1094 specvp_check(vnode_t **vpp, cred_t *cr)
1095 {
1096 	int error = 0;
1097 
1098 	if (IS_DEVVP(*vpp)) {
1099 		struct vnode *svp;
1100 
1101 		svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1102 		VN_RELE(*vpp);
1103 		if (svp == NULL)
1104 			error = ENOSYS;
1105 		*vpp = svp;
1106 	}
1107 	return (error);
1108 }
1109 
1110 
1111 /*
1112  * Lookup an entry in a directory, or an extended attribute directory.
1113  * If it exists, return a held vnode reference for it.
1114  *
1115  *	IN:	dvp	- vnode of directory to search.
1116  *		nm	- name of entry to lookup.
1117  *		pnp	- full pathname to lookup [UNUSED].
1118  *		flags	- LOOKUP_XATTR set if looking for an attribute.
1119  *		rdir	- root directory vnode [UNUSED].
1120  *		cr	- credentials of caller.
1121  *		ct	- caller context
1122  *		direntflags - directory lookup flags
1123  *		realpnp - returned pathname.
1124  *
1125  *	OUT:	vpp	- vnode of located entry, NULL if not found.
1126  *
1127  *	RETURN:	0 if success
1128  *		error code if failure
1129  *
1130  * Timestamps:
1131  *	NA
1132  */
1133 /* ARGSUSED */
1134 static int
1135 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1136     int flags, vnode_t *rdir, cred_t *cr,  caller_context_t *ct,
1137     int *direntflags, pathname_t *realpnp)
1138 {
1139 	znode_t *zdp = VTOZ(dvp);
1140 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1141 	int	error = 0;
1142 
1143 	/* fast path */
1144 	if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1145 
1146 		if (dvp->v_type != VDIR) {
1147 			return (ENOTDIR);
1148 		} else if (zdp->z_sa_hdl == NULL) {
1149 			return (EIO);
1150 		}
1151 
1152 		if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1153 			error = zfs_fastaccesschk_execute(zdp, cr);
1154 			if (!error) {
1155 				*vpp = dvp;
1156 				VN_HOLD(*vpp);
1157 				return (0);
1158 			}
1159 			return (error);
1160 		} else {
1161 			vnode_t *tvp = dnlc_lookup(dvp, nm);
1162 
1163 			if (tvp) {
1164 				error = zfs_fastaccesschk_execute(zdp, cr);
1165 				if (error) {
1166 					VN_RELE(tvp);
1167 					return (error);
1168 				}
1169 				if (tvp == DNLC_NO_VNODE) {
1170 					VN_RELE(tvp);
1171 					return (ENOENT);
1172 				} else {
1173 					*vpp = tvp;
1174 					return (specvp_check(vpp, cr));
1175 				}
1176 			}
1177 		}
1178 	}
1179 
1180 	DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1181 
1182 	ZFS_ENTER(zfsvfs);
1183 	ZFS_VERIFY_ZP(zdp);
1184 
1185 	*vpp = NULL;
1186 
1187 	if (flags & LOOKUP_XATTR) {
1188 		/*
1189 		 * If the xattr property is off, refuse the lookup request.
1190 		 */
1191 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1192 			ZFS_EXIT(zfsvfs);
1193 			return (EINVAL);
1194 		}
1195 
1196 		/*
1197 		 * We don't allow recursive attributes..
1198 		 * Maybe someday we will.
1199 		 */
1200 		if (zdp->z_pflags & ZFS_XATTR) {
1201 			ZFS_EXIT(zfsvfs);
1202 			return (EINVAL);
1203 		}
1204 
1205 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1206 			ZFS_EXIT(zfsvfs);
1207 			return (error);
1208 		}
1209 
1210 		/*
1211 		 * Do we have permission to get into attribute directory?
1212 		 */
1213 
1214 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1215 		    B_FALSE, cr)) {
1216 			VN_RELE(*vpp);
1217 			*vpp = NULL;
1218 		}
1219 
1220 		ZFS_EXIT(zfsvfs);
1221 		return (error);
1222 	}
1223 
1224 	if (dvp->v_type != VDIR) {
1225 		ZFS_EXIT(zfsvfs);
1226 		return (ENOTDIR);
1227 	}
1228 
1229 	/*
1230 	 * Check accessibility of directory.
1231 	 */
1232 
1233 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1234 		ZFS_EXIT(zfsvfs);
1235 		return (error);
1236 	}
1237 
1238 	if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1239 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1240 		ZFS_EXIT(zfsvfs);
1241 		return (EILSEQ);
1242 	}
1243 
1244 	error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1245 	if (error == 0)
1246 		error = specvp_check(vpp, cr);
1247 
1248 	ZFS_EXIT(zfsvfs);
1249 	return (error);
1250 }
1251 
1252 /*
1253  * Attempt to create a new entry in a directory.  If the entry
1254  * already exists, truncate the file if permissible, else return
1255  * an error.  Return the vp of the created or trunc'd file.
1256  *
1257  *	IN:	dvp	- vnode of directory to put new file entry in.
1258  *		name	- name of new file entry.
1259  *		vap	- attributes of new file.
1260  *		excl	- flag indicating exclusive or non-exclusive mode.
1261  *		mode	- mode to open file with.
1262  *		cr	- credentials of caller.
1263  *		flag	- large file flag [UNUSED].
1264  *		ct	- caller context
1265  *		vsecp 	- ACL to be set
1266  *
1267  *	OUT:	vpp	- vnode of created or trunc'd entry.
1268  *
1269  *	RETURN:	0 if success
1270  *		error code if failure
1271  *
1272  * Timestamps:
1273  *	dvp - ctime|mtime updated if new entry created
1274  *	 vp - ctime|mtime always, atime if new
1275  */
1276 
1277 /* ARGSUSED */
1278 static int
1279 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1280     int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
1281     vsecattr_t *vsecp)
1282 {
1283 	znode_t		*zp, *dzp = VTOZ(dvp);
1284 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1285 	zilog_t		*zilog;
1286 	objset_t	*os;
1287 	zfs_dirlock_t	*dl;
1288 	dmu_tx_t	*tx;
1289 	int		error;
1290 	ksid_t		*ksid;
1291 	uid_t		uid;
1292 	gid_t		gid = crgetgid(cr);
1293 	zfs_acl_ids_t   acl_ids;
1294 	boolean_t	fuid_dirtied;
1295 
1296 	/*
1297 	 * If we have an ephemeral id, ACL, or XVATTR then
1298 	 * make sure file system is at proper version
1299 	 */
1300 
1301 	ksid = crgetsid(cr, KSID_OWNER);
1302 	if (ksid)
1303 		uid = ksid_getid(ksid);
1304 	else
1305 		uid = crgetuid(cr);
1306 
1307 	if (zfsvfs->z_use_fuids == B_FALSE &&
1308 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
1309 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1310 		return (EINVAL);
1311 
1312 	ZFS_ENTER(zfsvfs);
1313 	ZFS_VERIFY_ZP(dzp);
1314 	os = zfsvfs->z_os;
1315 	zilog = zfsvfs->z_log;
1316 
1317 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1318 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1319 		ZFS_EXIT(zfsvfs);
1320 		return (EILSEQ);
1321 	}
1322 
1323 	if (vap->va_mask & AT_XVATTR) {
1324 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
1325 		    crgetuid(cr), cr, vap->va_type)) != 0) {
1326 			ZFS_EXIT(zfsvfs);
1327 			return (error);
1328 		}
1329 	}
1330 top:
1331 	*vpp = NULL;
1332 
1333 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1334 		vap->va_mode &= ~VSVTX;
1335 
1336 	if (*name == '\0') {
1337 		/*
1338 		 * Null component name refers to the directory itself.
1339 		 */
1340 		VN_HOLD(dvp);
1341 		zp = dzp;
1342 		dl = NULL;
1343 		error = 0;
1344 	} else {
1345 		/* possible VN_HOLD(zp) */
1346 		int zflg = 0;
1347 
1348 		if (flag & FIGNORECASE)
1349 			zflg |= ZCILOOK;
1350 
1351 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1352 		    NULL, NULL);
1353 		if (error) {
1354 			if (strcmp(name, "..") == 0)
1355 				error = EISDIR;
1356 			ZFS_EXIT(zfsvfs);
1357 			return (error);
1358 		}
1359 	}
1360 
1361 	if (zp == NULL) {
1362 		uint64_t txtype;
1363 
1364 		/*
1365 		 * Create a new file object and update the directory
1366 		 * to reference it.
1367 		 */
1368 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1369 			goto out;
1370 		}
1371 
1372 		/*
1373 		 * We only support the creation of regular files in
1374 		 * extended attribute directories.
1375 		 */
1376 
1377 		if ((dzp->z_pflags & ZFS_XATTR) &&
1378 		    (vap->va_type != VREG)) {
1379 			error = EINVAL;
1380 			goto out;
1381 		}
1382 
1383 		if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
1384 		    &acl_ids)) != 0)
1385 			goto out;
1386 		if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1387 			zfs_acl_ids_free(&acl_ids);
1388 			error = EDQUOT;
1389 			goto out;
1390 		}
1391 
1392 		tx = dmu_tx_create(os);
1393 
1394 		dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1395 		    ZFS_SA_BASE_ATTR_SIZE);
1396 
1397 		fuid_dirtied = zfsvfs->z_fuid_dirty;
1398 		if (fuid_dirtied)
1399 			zfs_fuid_txhold(zfsvfs, tx);
1400 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1401 		dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1402 		if (!zfsvfs->z_use_sa &&
1403 		    acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1404 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1405 			    0, acl_ids.z_aclp->z_acl_bytes);
1406 		}
1407 		error = dmu_tx_assign(tx, TXG_NOWAIT);
1408 		if (error) {
1409 			zfs_acl_ids_free(&acl_ids);
1410 			zfs_dirent_unlock(dl);
1411 			if (error == ERESTART) {
1412 				dmu_tx_wait(tx);
1413 				dmu_tx_abort(tx);
1414 				goto top;
1415 			}
1416 			dmu_tx_abort(tx);
1417 			ZFS_EXIT(zfsvfs);
1418 			return (error);
1419 		}
1420 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1421 
1422 		if (fuid_dirtied)
1423 			zfs_fuid_sync(zfsvfs, tx);
1424 
1425 		(void) zfs_link_create(dl, zp, tx, ZNEW);
1426 		txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1427 		if (flag & FIGNORECASE)
1428 			txtype |= TX_CI;
1429 		zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1430 		    vsecp, acl_ids.z_fuidp, vap);
1431 		zfs_acl_ids_free(&acl_ids);
1432 		dmu_tx_commit(tx);
1433 	} else {
1434 		int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1435 
1436 		/*
1437 		 * A directory entry already exists for this name.
1438 		 */
1439 		/*
1440 		 * Can't truncate an existing file if in exclusive mode.
1441 		 */
1442 		if (excl == EXCL) {
1443 			error = EEXIST;
1444 			goto out;
1445 		}
1446 		/*
1447 		 * Can't open a directory for writing.
1448 		 */
1449 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1450 			error = EISDIR;
1451 			goto out;
1452 		}
1453 		/*
1454 		 * Verify requested access to file.
1455 		 */
1456 		if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1457 			goto out;
1458 		}
1459 
1460 		mutex_enter(&dzp->z_lock);
1461 		dzp->z_seq++;
1462 		mutex_exit(&dzp->z_lock);
1463 
1464 		/*
1465 		 * Truncate regular files if requested.
1466 		 */
1467 		if ((ZTOV(zp)->v_type == VREG) &&
1468 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1469 			/* we can't hold any locks when calling zfs_freesp() */
1470 			zfs_dirent_unlock(dl);
1471 			dl = NULL;
1472 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
1473 			if (error == 0) {
1474 				vnevent_create(ZTOV(zp), ct);
1475 			}
1476 		}
1477 	}
1478 out:
1479 
1480 	if (dl)
1481 		zfs_dirent_unlock(dl);
1482 
1483 	if (error) {
1484 		if (zp)
1485 			VN_RELE(ZTOV(zp));
1486 	} else {
1487 		*vpp = ZTOV(zp);
1488 		error = specvp_check(vpp, cr);
1489 	}
1490 
1491 	ZFS_EXIT(zfsvfs);
1492 	return (error);
1493 }
1494 
1495 /*
1496  * Remove an entry from a directory.
1497  *
1498  *	IN:	dvp	- vnode of directory to remove entry from.
1499  *		name	- name of entry to remove.
1500  *		cr	- credentials of caller.
1501  *		ct	- caller context
1502  *		flags	- case flags
1503  *
1504  *	RETURN:	0 if success
1505  *		error code if failure
1506  *
1507  * Timestamps:
1508  *	dvp - ctime|mtime
1509  *	 vp - ctime (if nlink > 0)
1510  */
1511 
1512 uint64_t null_xattr = 0;
1513 
1514 /*ARGSUSED*/
1515 static int
1516 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1517     int flags)
1518 {
1519 	znode_t		*zp, *dzp = VTOZ(dvp);
1520 	znode_t		*xzp = NULL;
1521 	vnode_t		*vp;
1522 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1523 	zilog_t		*zilog;
1524 	uint64_t	acl_obj, xattr_obj = 0;
1525 	uint64_t 	xattr_obj_unlinked = 0;
1526 	zfs_dirlock_t	*dl;
1527 	dmu_tx_t	*tx;
1528 	boolean_t	may_delete_now, delete_now = FALSE;
1529 	boolean_t	unlinked, toobig = FALSE;
1530 	uint64_t	txtype;
1531 	pathname_t	*realnmp = NULL;
1532 	pathname_t	realnm;
1533 	int		error;
1534 	int		zflg = ZEXISTS;
1535 
1536 	ZFS_ENTER(zfsvfs);
1537 	ZFS_VERIFY_ZP(dzp);
1538 	zilog = zfsvfs->z_log;
1539 
1540 	if (flags & FIGNORECASE) {
1541 		zflg |= ZCILOOK;
1542 		pn_alloc(&realnm);
1543 		realnmp = &realnm;
1544 	}
1545 
1546 top:
1547 	/*
1548 	 * Attempt to lock directory; fail if entry doesn't exist.
1549 	 */
1550 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1551 	    NULL, realnmp)) {
1552 		if (realnmp)
1553 			pn_free(realnmp);
1554 		ZFS_EXIT(zfsvfs);
1555 		return (error);
1556 	}
1557 
1558 	vp = ZTOV(zp);
1559 
1560 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1561 		goto out;
1562 	}
1563 
1564 	/*
1565 	 * Need to use rmdir for removing directories.
1566 	 */
1567 	if (vp->v_type == VDIR) {
1568 		error = EPERM;
1569 		goto out;
1570 	}
1571 
1572 	vnevent_remove(vp, dvp, name, ct);
1573 
1574 	if (realnmp)
1575 		dnlc_remove(dvp, realnmp->pn_buf);
1576 	else
1577 		dnlc_remove(dvp, name);
1578 
1579 	mutex_enter(&vp->v_lock);
1580 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1581 	mutex_exit(&vp->v_lock);
1582 
1583 	/*
1584 	 * We may delete the znode now, or we may put it in the unlinked set;
1585 	 * it depends on whether we're the last link, and on whether there are
1586 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1587 	 * allow for either case.
1588 	 */
1589 	tx = dmu_tx_create(zfsvfs->z_os);
1590 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1591 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1592 	zfs_sa_upgrade_txholds(tx, zp);
1593 	zfs_sa_upgrade_txholds(tx, dzp);
1594 	if (may_delete_now) {
1595 		toobig =
1596 		    zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1597 		/* if the file is too big, only hold_free a token amount */
1598 		dmu_tx_hold_free(tx, zp->z_id, 0,
1599 		    (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1600 	}
1601 
1602 	/* are there any extended attributes? */
1603 	error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1604 	    &xattr_obj, sizeof (xattr_obj));
1605 	if (xattr_obj) {
1606 		error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1607 		ASSERT3U(error, ==, 0);
1608 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1609 		dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1610 	}
1611 
1612 	/* are there any additional acls */
1613 	if ((acl_obj = ZFS_EXTERNAL_ACL(zp)) != 0 && may_delete_now)
1614 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1615 
1616 	/* charge as an update -- would be nice not to charge at all */
1617 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1618 
1619 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1620 	if (error) {
1621 		zfs_dirent_unlock(dl);
1622 		VN_RELE(vp);
1623 		if (error == ERESTART) {
1624 			dmu_tx_wait(tx);
1625 			dmu_tx_abort(tx);
1626 			goto top;
1627 		}
1628 		if (realnmp)
1629 			pn_free(realnmp);
1630 		dmu_tx_abort(tx);
1631 		ZFS_EXIT(zfsvfs);
1632 		return (error);
1633 	}
1634 
1635 	/*
1636 	 * Remove the directory entry.
1637 	 */
1638 	error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1639 
1640 	if (error) {
1641 		dmu_tx_commit(tx);
1642 		goto out;
1643 	}
1644 
1645 	if (unlinked) {
1646 
1647 		mutex_enter(&vp->v_lock);
1648 
1649 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1650 		    &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1651 		delete_now = may_delete_now && !toobig &&
1652 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1653 		    xattr_obj == xattr_obj_unlinked && ZFS_EXTERNAL_ACL(zp) ==
1654 		    acl_obj;
1655 		mutex_exit(&vp->v_lock);
1656 	}
1657 
1658 	if (delete_now) {
1659 		if (xattr_obj_unlinked) {
1660 			ASSERT3U(xzp->z_links, ==, 2);
1661 			mutex_enter(&xzp->z_lock);
1662 			xzp->z_unlinked = 1;
1663 			xzp->z_links = 0;
1664 			error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
1665 			    &xzp->z_links, sizeof (xzp->z_links), tx);
1666 			ASSERT3U(error,  ==,  0);
1667 			mutex_exit(&xzp->z_lock);
1668 			zfs_unlinked_add(xzp, tx);
1669 			if (zp->z_is_sa)
1670 				error = sa_remove(zp->z_sa_hdl,
1671 				    SA_ZPL_XATTR(zfsvfs), tx);
1672 			else
1673 				error = sa_update(zp->z_sa_hdl,
1674 				    SA_ZPL_XATTR(zfsvfs), &null_xattr,
1675 				    sizeof (uint64_t), tx);
1676 			ASSERT3U(error, ==, 0);
1677 		}
1678 		mutex_enter(&zp->z_lock);
1679 		mutex_enter(&vp->v_lock);
1680 		vp->v_count--;
1681 		ASSERT3U(vp->v_count, ==, 0);
1682 		mutex_exit(&vp->v_lock);
1683 		mutex_exit(&zp->z_lock);
1684 		zfs_znode_delete(zp, tx);
1685 	} else if (unlinked) {
1686 		zfs_unlinked_add(zp, tx);
1687 	}
1688 
1689 	txtype = TX_REMOVE;
1690 	if (flags & FIGNORECASE)
1691 		txtype |= TX_CI;
1692 	zfs_log_remove(zilog, tx, txtype, dzp, name);
1693 
1694 	dmu_tx_commit(tx);
1695 out:
1696 	if (realnmp)
1697 		pn_free(realnmp);
1698 
1699 	zfs_dirent_unlock(dl);
1700 
1701 	if (!delete_now)
1702 		VN_RELE(vp);
1703 	if (xzp)
1704 		VN_RELE(ZTOV(xzp));
1705 
1706 	ZFS_EXIT(zfsvfs);
1707 	return (error);
1708 }
1709 
1710 /*
1711  * Create a new directory and insert it into dvp using the name
1712  * provided.  Return a pointer to the inserted directory.
1713  *
1714  *	IN:	dvp	- vnode of directory to add subdir to.
1715  *		dirname	- name of new directory.
1716  *		vap	- attributes of new directory.
1717  *		cr	- credentials of caller.
1718  *		ct	- caller context
1719  *		vsecp	- ACL to be set
1720  *
1721  *	OUT:	vpp	- vnode of created directory.
1722  *
1723  *	RETURN:	0 if success
1724  *		error code if failure
1725  *
1726  * Timestamps:
1727  *	dvp - ctime|mtime updated
1728  *	 vp - ctime|mtime|atime updated
1729  */
1730 /*ARGSUSED*/
1731 static int
1732 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
1733     caller_context_t *ct, int flags, vsecattr_t *vsecp)
1734 {
1735 	znode_t		*zp, *dzp = VTOZ(dvp);
1736 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1737 	zilog_t		*zilog;
1738 	zfs_dirlock_t	*dl;
1739 	uint64_t	txtype;
1740 	dmu_tx_t	*tx;
1741 	int		error;
1742 	int		zf = ZNEW;
1743 	ksid_t		*ksid;
1744 	uid_t		uid;
1745 	gid_t		gid = crgetgid(cr);
1746 	zfs_acl_ids_t   acl_ids;
1747 	boolean_t	fuid_dirtied;
1748 
1749 	ASSERT(vap->va_type == VDIR);
1750 
1751 	/*
1752 	 * If we have an ephemeral id, ACL, or XVATTR then
1753 	 * make sure file system is at proper version
1754 	 */
1755 
1756 	ksid = crgetsid(cr, KSID_OWNER);
1757 	if (ksid)
1758 		uid = ksid_getid(ksid);
1759 	else
1760 		uid = crgetuid(cr);
1761 	if (zfsvfs->z_use_fuids == B_FALSE &&
1762 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
1763 	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1764 		return (EINVAL);
1765 
1766 	ZFS_ENTER(zfsvfs);
1767 	ZFS_VERIFY_ZP(dzp);
1768 	zilog = zfsvfs->z_log;
1769 
1770 	if (dzp->z_pflags & ZFS_XATTR) {
1771 		ZFS_EXIT(zfsvfs);
1772 		return (EINVAL);
1773 	}
1774 
1775 	if (zfsvfs->z_utf8 && u8_validate(dirname,
1776 	    strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1777 		ZFS_EXIT(zfsvfs);
1778 		return (EILSEQ);
1779 	}
1780 	if (flags & FIGNORECASE)
1781 		zf |= ZCILOOK;
1782 
1783 	if (vap->va_mask & AT_XVATTR)
1784 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
1785 		    crgetuid(cr), cr, vap->va_type)) != 0) {
1786 			ZFS_EXIT(zfsvfs);
1787 			return (error);
1788 		}
1789 
1790 	/*
1791 	 * First make sure the new directory doesn't exist.
1792 	 */
1793 top:
1794 	*vpp = NULL;
1795 
1796 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1797 	    NULL, NULL)) {
1798 		ZFS_EXIT(zfsvfs);
1799 		return (error);
1800 	}
1801 
1802 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
1803 		zfs_dirent_unlock(dl);
1804 		ZFS_EXIT(zfsvfs);
1805 		return (error);
1806 	}
1807 
1808 	if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp,
1809 	    &acl_ids)) != 0) {
1810 		zfs_dirent_unlock(dl);
1811 		ZFS_EXIT(zfsvfs);
1812 		return (error);
1813 	}
1814 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1815 		zfs_acl_ids_free(&acl_ids);
1816 		zfs_dirent_unlock(dl);
1817 		ZFS_EXIT(zfsvfs);
1818 		return (EDQUOT);
1819 	}
1820 
1821 	/*
1822 	 * Add a new entry to the directory.
1823 	 */
1824 	tx = dmu_tx_create(zfsvfs->z_os);
1825 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1826 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1827 	fuid_dirtied = zfsvfs->z_fuid_dirty;
1828 	if (fuid_dirtied)
1829 		zfs_fuid_txhold(zfsvfs, tx);
1830 	if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1831 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1832 		    acl_ids.z_aclp->z_acl_bytes);
1833 	}
1834 
1835 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1836 	    ZFS_SA_BASE_ATTR_SIZE);
1837 
1838 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1839 	if (error) {
1840 		zfs_acl_ids_free(&acl_ids);
1841 		zfs_dirent_unlock(dl);
1842 		if (error == ERESTART) {
1843 			dmu_tx_wait(tx);
1844 			dmu_tx_abort(tx);
1845 			goto top;
1846 		}
1847 		dmu_tx_abort(tx);
1848 		ZFS_EXIT(zfsvfs);
1849 		return (error);
1850 	}
1851 
1852 	/*
1853 	 * Create new node.
1854 	 */
1855 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1856 
1857 	if (fuid_dirtied)
1858 		zfs_fuid_sync(zfsvfs, tx);
1859 
1860 	/*
1861 	 * Now put new name in parent dir.
1862 	 */
1863 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1864 
1865 	*vpp = ZTOV(zp);
1866 
1867 	txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
1868 	if (flags & FIGNORECASE)
1869 		txtype |= TX_CI;
1870 	zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
1871 	    acl_ids.z_fuidp, vap);
1872 
1873 	zfs_acl_ids_free(&acl_ids);
1874 
1875 	dmu_tx_commit(tx);
1876 
1877 	zfs_dirent_unlock(dl);
1878 
1879 	ZFS_EXIT(zfsvfs);
1880 	return (0);
1881 }
1882 
1883 /*
1884  * Remove a directory subdir entry.  If the current working
1885  * directory is the same as the subdir to be removed, the
1886  * remove will fail.
1887  *
1888  *	IN:	dvp	- vnode of directory to remove from.
1889  *		name	- name of directory to be removed.
1890  *		cwd	- vnode of current working directory.
1891  *		cr	- credentials of caller.
1892  *		ct	- caller context
1893  *		flags	- case flags
1894  *
1895  *	RETURN:	0 if success
1896  *		error code if failure
1897  *
1898  * Timestamps:
1899  *	dvp - ctime|mtime updated
1900  */
1901 /*ARGSUSED*/
1902 static int
1903 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
1904     caller_context_t *ct, int flags)
1905 {
1906 	znode_t		*dzp = VTOZ(dvp);
1907 	znode_t		*zp;
1908 	vnode_t		*vp;
1909 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1910 	zilog_t		*zilog;
1911 	zfs_dirlock_t	*dl;
1912 	dmu_tx_t	*tx;
1913 	int		error;
1914 	int		zflg = ZEXISTS;
1915 
1916 	ZFS_ENTER(zfsvfs);
1917 	ZFS_VERIFY_ZP(dzp);
1918 	zilog = zfsvfs->z_log;
1919 
1920 	if (flags & FIGNORECASE)
1921 		zflg |= ZCILOOK;
1922 top:
1923 	zp = NULL;
1924 
1925 	/*
1926 	 * Attempt to lock directory; fail if entry doesn't exist.
1927 	 */
1928 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1929 	    NULL, NULL)) {
1930 		ZFS_EXIT(zfsvfs);
1931 		return (error);
1932 	}
1933 
1934 	vp = ZTOV(zp);
1935 
1936 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1937 		goto out;
1938 	}
1939 
1940 	if (vp->v_type != VDIR) {
1941 		error = ENOTDIR;
1942 		goto out;
1943 	}
1944 
1945 	if (vp == cwd) {
1946 		error = EINVAL;
1947 		goto out;
1948 	}
1949 
1950 	vnevent_rmdir(vp, dvp, name, ct);
1951 
1952 	/*
1953 	 * Grab a lock on the directory to make sure that noone is
1954 	 * trying to add (or lookup) entries while we are removing it.
1955 	 */
1956 	rw_enter(&zp->z_name_lock, RW_WRITER);
1957 
1958 	/*
1959 	 * Grab a lock on the parent pointer to make sure we play well
1960 	 * with the treewalk and directory rename code.
1961 	 */
1962 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1963 
1964 	tx = dmu_tx_create(zfsvfs->z_os);
1965 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1966 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1967 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1968 	zfs_sa_upgrade_txholds(tx, zp);
1969 	zfs_sa_upgrade_txholds(tx, dzp);
1970 	error = dmu_tx_assign(tx, TXG_NOWAIT);
1971 	if (error) {
1972 		rw_exit(&zp->z_parent_lock);
1973 		rw_exit(&zp->z_name_lock);
1974 		zfs_dirent_unlock(dl);
1975 		VN_RELE(vp);
1976 		if (error == ERESTART) {
1977 			dmu_tx_wait(tx);
1978 			dmu_tx_abort(tx);
1979 			goto top;
1980 		}
1981 		dmu_tx_abort(tx);
1982 		ZFS_EXIT(zfsvfs);
1983 		return (error);
1984 	}
1985 
1986 	error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
1987 
1988 	if (error == 0) {
1989 		uint64_t txtype = TX_RMDIR;
1990 		if (flags & FIGNORECASE)
1991 			txtype |= TX_CI;
1992 		zfs_log_remove(zilog, tx, txtype, dzp, name);
1993 	}
1994 
1995 	dmu_tx_commit(tx);
1996 
1997 	rw_exit(&zp->z_parent_lock);
1998 	rw_exit(&zp->z_name_lock);
1999 out:
2000 	zfs_dirent_unlock(dl);
2001 
2002 	VN_RELE(vp);
2003 
2004 	ZFS_EXIT(zfsvfs);
2005 	return (error);
2006 }
2007 
2008 /*
2009  * Read as many directory entries as will fit into the provided
2010  * buffer from the given directory cursor position (specified in
2011  * the uio structure.
2012  *
2013  *	IN:	vp	- vnode of directory to read.
2014  *		uio	- structure supplying read location, range info,
2015  *			  and return buffer.
2016  *		cr	- credentials of caller.
2017  *		ct	- caller context
2018  *		flags	- case flags
2019  *
2020  *	OUT:	uio	- updated offset and range, buffer filled.
2021  *		eofp	- set to true if end-of-file detected.
2022  *
2023  *	RETURN:	0 if success
2024  *		error code if failure
2025  *
2026  * Timestamps:
2027  *	vp - atime updated
2028  *
2029  * Note that the low 4 bits of the cookie returned by zap is always zero.
2030  * This allows us to use the low range for "special" directory entries:
2031  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
2032  * we use the offset 2 for the '.zfs' directory.
2033  */
2034 /* ARGSUSED */
2035 static int
2036 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
2037     caller_context_t *ct, int flags)
2038 {
2039 	znode_t		*zp = VTOZ(vp);
2040 	iovec_t		*iovp;
2041 	edirent_t	*eodp;
2042 	dirent64_t	*odp;
2043 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2044 	objset_t	*os;
2045 	caddr_t		outbuf;
2046 	size_t		bufsize;
2047 	zap_cursor_t	zc;
2048 	zap_attribute_t	zap;
2049 	uint_t		bytes_wanted;
2050 	uint64_t	offset; /* must be unsigned; checks for < 1 */
2051 	uint64_t	parent;
2052 	int		local_eof;
2053 	int		outcount;
2054 	int		error;
2055 	uint8_t		prefetch;
2056 	boolean_t	check_sysattrs;
2057 
2058 	ZFS_ENTER(zfsvfs);
2059 	ZFS_VERIFY_ZP(zp);
2060 
2061 	if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2062 	    &parent, sizeof (parent))) != 0) {
2063 		ZFS_EXIT(zfsvfs);
2064 		return (error);
2065 	}
2066 
2067 	/*
2068 	 * If we are not given an eof variable,
2069 	 * use a local one.
2070 	 */
2071 	if (eofp == NULL)
2072 		eofp = &local_eof;
2073 
2074 	/*
2075 	 * Check for valid iov_len.
2076 	 */
2077 	if (uio->uio_iov->iov_len <= 0) {
2078 		ZFS_EXIT(zfsvfs);
2079 		return (EINVAL);
2080 	}
2081 
2082 	/*
2083 	 * Quit if directory has been removed (posix)
2084 	 */
2085 	if ((*eofp = zp->z_unlinked) != 0) {
2086 		ZFS_EXIT(zfsvfs);
2087 		return (0);
2088 	}
2089 
2090 	error = 0;
2091 	os = zfsvfs->z_os;
2092 	offset = uio->uio_loffset;
2093 	prefetch = zp->z_zn_prefetch;
2094 
2095 	/*
2096 	 * Initialize the iterator cursor.
2097 	 */
2098 	if (offset <= 3) {
2099 		/*
2100 		 * Start iteration from the beginning of the directory.
2101 		 */
2102 		zap_cursor_init(&zc, os, zp->z_id);
2103 	} else {
2104 		/*
2105 		 * The offset is a serialized cursor.
2106 		 */
2107 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2108 	}
2109 
2110 	/*
2111 	 * Get space to change directory entries into fs independent format.
2112 	 */
2113 	iovp = uio->uio_iov;
2114 	bytes_wanted = iovp->iov_len;
2115 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2116 		bufsize = bytes_wanted;
2117 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
2118 		odp = (struct dirent64 *)outbuf;
2119 	} else {
2120 		bufsize = bytes_wanted;
2121 		odp = (struct dirent64 *)iovp->iov_base;
2122 	}
2123 	eodp = (struct edirent *)odp;
2124 
2125 	/*
2126 	 * If this VFS supports the system attribute view interface; and
2127 	 * we're looking at an extended attribute directory; and we care
2128 	 * about normalization conflicts on this vfs; then we must check
2129 	 * for normalization conflicts with the sysattr name space.
2130 	 */
2131 	check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2132 	    (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2133 	    (flags & V_RDDIR_ENTFLAGS);
2134 
2135 	/*
2136 	 * Transform to file-system independent format
2137 	 */
2138 	outcount = 0;
2139 	while (outcount < bytes_wanted) {
2140 		ino64_t objnum;
2141 		ushort_t reclen;
2142 		off64_t *next;
2143 
2144 		/*
2145 		 * Special case `.', `..', and `.zfs'.
2146 		 */
2147 		if (offset == 0) {
2148 			(void) strcpy(zap.za_name, ".");
2149 			zap.za_normalization_conflict = 0;
2150 			objnum = zp->z_id;
2151 		} else if (offset == 1) {
2152 			(void) strcpy(zap.za_name, "..");
2153 			zap.za_normalization_conflict = 0;
2154 			objnum = parent;
2155 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
2156 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2157 			zap.za_normalization_conflict = 0;
2158 			objnum = ZFSCTL_INO_ROOT;
2159 		} else {
2160 			/*
2161 			 * Grab next entry.
2162 			 */
2163 			if (error = zap_cursor_retrieve(&zc, &zap)) {
2164 				if ((*eofp = (error == ENOENT)) != 0)
2165 					break;
2166 				else
2167 					goto update;
2168 			}
2169 
2170 			if (zap.za_integer_length != 8 ||
2171 			    zap.za_num_integers != 1) {
2172 				cmn_err(CE_WARN, "zap_readdir: bad directory "
2173 				    "entry, obj = %lld, offset = %lld\n",
2174 				    (u_longlong_t)zp->z_id,
2175 				    (u_longlong_t)offset);
2176 				error = ENXIO;
2177 				goto update;
2178 			}
2179 
2180 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2181 			/*
2182 			 * MacOS X can extract the object type here such as:
2183 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2184 			 */
2185 
2186 			if (check_sysattrs && !zap.za_normalization_conflict) {
2187 				zap.za_normalization_conflict =
2188 				    xattr_sysattr_casechk(zap.za_name);
2189 			}
2190 		}
2191 
2192 		if (flags & V_RDDIR_ACCFILTER) {
2193 			/*
2194 			 * If we have no access at all, don't include
2195 			 * this entry in the returned information
2196 			 */
2197 			znode_t	*ezp;
2198 			if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2199 				goto skip_entry;
2200 			if (!zfs_has_access(ezp, cr)) {
2201 				VN_RELE(ZTOV(ezp));
2202 				goto skip_entry;
2203 			}
2204 			VN_RELE(ZTOV(ezp));
2205 		}
2206 
2207 		if (flags & V_RDDIR_ENTFLAGS)
2208 			reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2209 		else
2210 			reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2211 
2212 		/*
2213 		 * Will this entry fit in the buffer?
2214 		 */
2215 		if (outcount + reclen > bufsize) {
2216 			/*
2217 			 * Did we manage to fit anything in the buffer?
2218 			 */
2219 			if (!outcount) {
2220 				error = EINVAL;
2221 				goto update;
2222 			}
2223 			break;
2224 		}
2225 		if (flags & V_RDDIR_ENTFLAGS) {
2226 			/*
2227 			 * Add extended flag entry:
2228 			 */
2229 			eodp->ed_ino = objnum;
2230 			eodp->ed_reclen = reclen;
2231 			/* NOTE: ed_off is the offset for the *next* entry */
2232 			next = &(eodp->ed_off);
2233 			eodp->ed_eflags = zap.za_normalization_conflict ?
2234 			    ED_CASE_CONFLICT : 0;
2235 			(void) strncpy(eodp->ed_name, zap.za_name,
2236 			    EDIRENT_NAMELEN(reclen));
2237 			eodp = (edirent_t *)((intptr_t)eodp + reclen);
2238 		} else {
2239 			/*
2240 			 * Add normal entry:
2241 			 */
2242 			odp->d_ino = objnum;
2243 			odp->d_reclen = reclen;
2244 			/* NOTE: d_off is the offset for the *next* entry */
2245 			next = &(odp->d_off);
2246 			(void) strncpy(odp->d_name, zap.za_name,
2247 			    DIRENT64_NAMELEN(reclen));
2248 			odp = (dirent64_t *)((intptr_t)odp + reclen);
2249 		}
2250 		outcount += reclen;
2251 
2252 		ASSERT(outcount <= bufsize);
2253 
2254 		/* Prefetch znode */
2255 		if (prefetch)
2256 			dmu_prefetch(os, objnum, 0, 0);
2257 
2258 	skip_entry:
2259 		/*
2260 		 * Move to the next entry, fill in the previous offset.
2261 		 */
2262 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2263 			zap_cursor_advance(&zc);
2264 			offset = zap_cursor_serialize(&zc);
2265 		} else {
2266 			offset += 1;
2267 		}
2268 		*next = offset;
2269 	}
2270 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2271 
2272 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2273 		iovp->iov_base += outcount;
2274 		iovp->iov_len -= outcount;
2275 		uio->uio_resid -= outcount;
2276 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2277 		/*
2278 		 * Reset the pointer.
2279 		 */
2280 		offset = uio->uio_loffset;
2281 	}
2282 
2283 update:
2284 	zap_cursor_fini(&zc);
2285 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2286 		kmem_free(outbuf, bufsize);
2287 
2288 	if (error == ENOENT)
2289 		error = 0;
2290 
2291 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2292 
2293 	uio->uio_loffset = offset;
2294 	ZFS_EXIT(zfsvfs);
2295 	return (error);
2296 }
2297 
2298 ulong_t zfs_fsync_sync_cnt = 4;
2299 
2300 static int
2301 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2302 {
2303 	znode_t	*zp = VTOZ(vp);
2304 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2305 
2306 	/*
2307 	 * Regardless of whether this is required for standards conformance,
2308 	 * this is the logical behavior when fsync() is called on a file with
2309 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
2310 	 * going to be pushed out as part of the zil_commit().
2311 	 */
2312 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
2313 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
2314 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
2315 
2316 	(void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2317 
2318 	ZFS_ENTER(zfsvfs);
2319 	ZFS_VERIFY_ZP(zp);
2320 	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
2321 	ZFS_EXIT(zfsvfs);
2322 	return (0);
2323 }
2324 
2325 
2326 /*
2327  * Get the requested file attributes and place them in the provided
2328  * vattr structure.
2329  *
2330  *	IN:	vp	- vnode of file.
2331  *		vap	- va_mask identifies requested attributes.
2332  *			  If AT_XVATTR set, then optional attrs are requested
2333  *		flags	- ATTR_NOACLCHECK (CIFS server context)
2334  *		cr	- credentials of caller.
2335  *		ct	- caller context
2336  *
2337  *	OUT:	vap	- attribute values.
2338  *
2339  *	RETURN:	0 (always succeeds)
2340  */
2341 /* ARGSUSED */
2342 static int
2343 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2344     caller_context_t *ct)
2345 {
2346 	znode_t *zp = VTOZ(vp);
2347 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2348 	int	error = 0;
2349 	uint64_t links;
2350 	uint64_t mtime[2], ctime[2];
2351 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
2352 	xoptattr_t *xoap = NULL;
2353 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2354 	sa_bulk_attr_t bulk[2];
2355 	int count = 0;
2356 
2357 	ZFS_ENTER(zfsvfs);
2358 	ZFS_VERIFY_ZP(zp);
2359 
2360 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2361 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2362 
2363 	if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2364 		ZFS_EXIT(zfsvfs);
2365 		return (error);
2366 	}
2367 
2368 	/*
2369 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2370 	 * Also, if we are the owner don't bother, since owner should
2371 	 * always be allowed to read basic attributes of file.
2372 	 */
2373 	if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) && (zp->z_uid != crgetuid(cr))) {
2374 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2375 		    skipaclchk, cr)) {
2376 			ZFS_EXIT(zfsvfs);
2377 			return (error);
2378 		}
2379 	}
2380 
2381 	/*
2382 	 * Return all attributes.  It's cheaper to provide the answer
2383 	 * than to determine whether we were asked the question.
2384 	 */
2385 
2386 	mutex_enter(&zp->z_lock);
2387 	vap->va_type = vp->v_type;
2388 	vap->va_mode = zp->z_mode & MODEMASK;
2389 	vap->va_uid = zp->z_uid;
2390 	vap->va_gid = zp->z_gid;
2391 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2392 	vap->va_nodeid = zp->z_id;
2393 	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2394 		links = zp->z_links + 1;
2395 	else
2396 		links = zp->z_links;
2397 	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
2398 	vap->va_size = zp->z_size;
2399 	vap->va_rdev = vp->v_rdev;
2400 	vap->va_seq = zp->z_seq;
2401 
2402 	/*
2403 	 * Add in any requested optional attributes and the create time.
2404 	 * Also set the corresponding bits in the returned attribute bitmap.
2405 	 */
2406 	if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2407 		if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2408 			xoap->xoa_archive =
2409 			    ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2410 			XVA_SET_RTN(xvap, XAT_ARCHIVE);
2411 		}
2412 
2413 		if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2414 			xoap->xoa_readonly =
2415 			    ((zp->z_pflags & ZFS_READONLY) != 0);
2416 			XVA_SET_RTN(xvap, XAT_READONLY);
2417 		}
2418 
2419 		if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2420 			xoap->xoa_system =
2421 			    ((zp->z_pflags & ZFS_SYSTEM) != 0);
2422 			XVA_SET_RTN(xvap, XAT_SYSTEM);
2423 		}
2424 
2425 		if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2426 			xoap->xoa_hidden =
2427 			    ((zp->z_pflags & ZFS_HIDDEN) != 0);
2428 			XVA_SET_RTN(xvap, XAT_HIDDEN);
2429 		}
2430 
2431 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2432 			xoap->xoa_nounlink =
2433 			    ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2434 			XVA_SET_RTN(xvap, XAT_NOUNLINK);
2435 		}
2436 
2437 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2438 			xoap->xoa_immutable =
2439 			    ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2440 			XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2441 		}
2442 
2443 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2444 			xoap->xoa_appendonly =
2445 			    ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2446 			XVA_SET_RTN(xvap, XAT_APPENDONLY);
2447 		}
2448 
2449 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2450 			xoap->xoa_nodump =
2451 			    ((zp->z_pflags & ZFS_NODUMP) != 0);
2452 			XVA_SET_RTN(xvap, XAT_NODUMP);
2453 		}
2454 
2455 		if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2456 			xoap->xoa_opaque =
2457 			    ((zp->z_pflags & ZFS_OPAQUE) != 0);
2458 			XVA_SET_RTN(xvap, XAT_OPAQUE);
2459 		}
2460 
2461 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2462 			xoap->xoa_av_quarantined =
2463 			    ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2464 			XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2465 		}
2466 
2467 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2468 			xoap->xoa_av_modified =
2469 			    ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2470 			XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2471 		}
2472 
2473 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2474 		    vp->v_type == VREG) {
2475 			zfs_sa_get_scanstamp(zp, xvap);
2476 		}
2477 
2478 		if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2479 			uint64_t times[2];
2480 
2481 			(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2482 			    times, sizeof (times));
2483 			ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2484 			XVA_SET_RTN(xvap, XAT_CREATETIME);
2485 		}
2486 
2487 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2488 			xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2489 			XVA_SET_RTN(xvap, XAT_REPARSE);
2490 		}
2491 	}
2492 
2493 	ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2494 	ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2495 	ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2496 
2497 	mutex_exit(&zp->z_lock);
2498 
2499 	sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2500 
2501 	if (zp->z_blksz == 0) {
2502 		/*
2503 		 * Block size hasn't been set; suggest maximal I/O transfers.
2504 		 */
2505 		vap->va_blksize = zfsvfs->z_max_blksz;
2506 	}
2507 
2508 	ZFS_EXIT(zfsvfs);
2509 	return (0);
2510 }
2511 
2512 /*
2513  * Set the file attributes to the values contained in the
2514  * vattr structure.
2515  *
2516  *	IN:	vp	- vnode of file to be modified.
2517  *		vap	- new attribute values.
2518  *			  If AT_XVATTR set, then optional attrs are being set
2519  *		flags	- ATTR_UTIME set if non-default time values provided.
2520  *			- ATTR_NOACLCHECK (CIFS context only).
2521  *		cr	- credentials of caller.
2522  *		ct	- caller context
2523  *
2524  *	RETURN:	0 if success
2525  *		error code if failure
2526  *
2527  * Timestamps:
2528  *	vp - ctime updated, mtime updated if size changed.
2529  */
2530 /* ARGSUSED */
2531 static int
2532 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2533 	caller_context_t *ct)
2534 {
2535 	znode_t		*zp = VTOZ(vp);
2536 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2537 	zilog_t		*zilog;
2538 	dmu_tx_t	*tx;
2539 	vattr_t		oldva;
2540 	xvattr_t	tmpxvattr;
2541 	uint_t		mask = vap->va_mask;
2542 	uint_t		saved_mask;
2543 	int		trim_mask = 0;
2544 	uint64_t	new_mode;
2545 	uint64_t	new_uid, new_gid;
2546 	uint64_t	xattr_obj = 0;
2547 	uint64_t	mtime[2], ctime[2];
2548 	znode_t		*attrzp;
2549 	int		need_policy = FALSE;
2550 	int		err, err2;
2551 	zfs_fuid_info_t *fuidp = NULL;
2552 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
2553 	xoptattr_t	*xoap;
2554 	zfs_acl_t	*aclp = NULL;
2555 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2556 	boolean_t	fuid_dirtied = B_FALSE;
2557 	sa_bulk_attr_t	bulk[7], xattr_bulk[7];
2558 	int		count = 0, xattr_count = 0;
2559 
2560 	if (mask == 0)
2561 		return (0);
2562 
2563 	if (mask & AT_NOSET)
2564 		return (EINVAL);
2565 
2566 	ZFS_ENTER(zfsvfs);
2567 	ZFS_VERIFY_ZP(zp);
2568 
2569 	zilog = zfsvfs->z_log;
2570 
2571 	/*
2572 	 * Make sure that if we have ephemeral uid/gid or xvattr specified
2573 	 * that file system is at proper version level
2574 	 */
2575 
2576 	if (zfsvfs->z_use_fuids == B_FALSE &&
2577 	    (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2578 	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2579 	    (mask & AT_XVATTR))) {
2580 		ZFS_EXIT(zfsvfs);
2581 		return (EINVAL);
2582 	}
2583 
2584 	if (mask & AT_SIZE && vp->v_type == VDIR) {
2585 		ZFS_EXIT(zfsvfs);
2586 		return (EISDIR);
2587 	}
2588 
2589 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2590 		ZFS_EXIT(zfsvfs);
2591 		return (EINVAL);
2592 	}
2593 
2594 	/*
2595 	 * If this is an xvattr_t, then get a pointer to the structure of
2596 	 * optional attributes.  If this is NULL, then we have a vattr_t.
2597 	 */
2598 	xoap = xva_getxoptattr(xvap);
2599 
2600 	xva_init(&tmpxvattr);
2601 
2602 	/*
2603 	 * Immutable files can only alter immutable bit and atime
2604 	 */
2605 	if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2606 	    ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2607 	    ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2608 		ZFS_EXIT(zfsvfs);
2609 		return (EPERM);
2610 	}
2611 
2612 	if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2613 		ZFS_EXIT(zfsvfs);
2614 		return (EPERM);
2615 	}
2616 
2617 	/*
2618 	 * Verify timestamps doesn't overflow 32 bits.
2619 	 * ZFS can handle large timestamps, but 32bit syscalls can't
2620 	 * handle times greater than 2039.  This check should be removed
2621 	 * once large timestamps are fully supported.
2622 	 */
2623 	if (mask & (AT_ATIME | AT_MTIME)) {
2624 		if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2625 		    ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2626 			ZFS_EXIT(zfsvfs);
2627 			return (EOVERFLOW);
2628 		}
2629 	}
2630 
2631 top:
2632 	attrzp = NULL;
2633 
2634 	/* Can this be moved to before the top label? */
2635 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2636 		ZFS_EXIT(zfsvfs);
2637 		return (EROFS);
2638 	}
2639 
2640 	/*
2641 	 * First validate permissions
2642 	 */
2643 
2644 	if (mask & AT_SIZE) {
2645 		err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2646 		if (err) {
2647 			ZFS_EXIT(zfsvfs);
2648 			return (err);
2649 		}
2650 		/*
2651 		 * XXX - Note, we are not providing any open
2652 		 * mode flags here (like FNDELAY), so we may
2653 		 * block if there are locks present... this
2654 		 * should be addressed in openat().
2655 		 */
2656 		/* XXX - would it be OK to generate a log record here? */
2657 		err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2658 		if (err) {
2659 			ZFS_EXIT(zfsvfs);
2660 			return (err);
2661 		}
2662 	}
2663 
2664 	if (mask & (AT_ATIME|AT_MTIME) ||
2665 	    ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2666 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2667 	    XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2668 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2669 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2670 		need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2671 		    skipaclchk, cr);
2672 	}
2673 
2674 	if (mask & (AT_UID|AT_GID)) {
2675 		int	idmask = (mask & (AT_UID|AT_GID));
2676 		int	take_owner;
2677 		int	take_group;
2678 
2679 		/*
2680 		 * NOTE: even if a new mode is being set,
2681 		 * we may clear S_ISUID/S_ISGID bits.
2682 		 */
2683 
2684 		if (!(mask & AT_MODE))
2685 			vap->va_mode = zp->z_mode;
2686 
2687 		/*
2688 		 * Take ownership or chgrp to group we are a member of
2689 		 */
2690 
2691 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2692 		take_group = (mask & AT_GID) &&
2693 		    zfs_groupmember(zfsvfs, vap->va_gid, cr);
2694 
2695 		/*
2696 		 * If both AT_UID and AT_GID are set then take_owner and
2697 		 * take_group must both be set in order to allow taking
2698 		 * ownership.
2699 		 *
2700 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2701 		 *
2702 		 */
2703 
2704 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2705 		    ((idmask == AT_UID) && take_owner) ||
2706 		    ((idmask == AT_GID) && take_group)) {
2707 			if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2708 			    skipaclchk, cr) == 0) {
2709 				/*
2710 				 * Remove setuid/setgid for non-privileged users
2711 				 */
2712 				secpolicy_setid_clear(vap, cr);
2713 				trim_mask = (mask & (AT_UID|AT_GID));
2714 			} else {
2715 				need_policy =  TRUE;
2716 			}
2717 		} else {
2718 			need_policy =  TRUE;
2719 		}
2720 	}
2721 
2722 	mutex_enter(&zp->z_lock);
2723 	oldva.va_mode = zp->z_mode;
2724 	oldva.va_uid = zp->z_uid;
2725 	oldva.va_gid = zp->z_gid;
2726 	if (mask & AT_XVATTR) {
2727 		/*
2728 		 * Update xvattr mask to include only those attributes
2729 		 * that are actually changing.
2730 		 *
2731 		 * the bits will be restored prior to actually setting
2732 		 * the attributes so the caller thinks they were set.
2733 		 */
2734 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2735 			if (xoap->xoa_appendonly !=
2736 			    ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2737 				need_policy = TRUE;
2738 			} else {
2739 				XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2740 				XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
2741 			}
2742 		}
2743 
2744 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2745 			if (xoap->xoa_nounlink !=
2746 			    ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2747 				need_policy = TRUE;
2748 			} else {
2749 				XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2750 				XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
2751 			}
2752 		}
2753 
2754 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2755 			if (xoap->xoa_immutable !=
2756 			    ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2757 				need_policy = TRUE;
2758 			} else {
2759 				XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2760 				XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
2761 			}
2762 		}
2763 
2764 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2765 			if (xoap->xoa_nodump !=
2766 			    ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2767 				need_policy = TRUE;
2768 			} else {
2769 				XVA_CLR_REQ(xvap, XAT_NODUMP);
2770 				XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
2771 			}
2772 		}
2773 
2774 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2775 			if (xoap->xoa_av_modified !=
2776 			    ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2777 				need_policy = TRUE;
2778 			} else {
2779 				XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2780 				XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
2781 			}
2782 		}
2783 
2784 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2785 			if ((vp->v_type != VREG &&
2786 			    xoap->xoa_av_quarantined) ||
2787 			    xoap->xoa_av_quarantined !=
2788 			    ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2789 				need_policy = TRUE;
2790 			} else {
2791 				XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2792 				XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
2793 			}
2794 		}
2795 
2796 		if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2797 			mutex_exit(&zp->z_lock);
2798 			ZFS_EXIT(zfsvfs);
2799 			return (EPERM);
2800 		}
2801 
2802 		if (need_policy == FALSE &&
2803 		    (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2804 		    XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2805 			need_policy = TRUE;
2806 		}
2807 	}
2808 
2809 	mutex_exit(&zp->z_lock);
2810 
2811 	if (mask & AT_MODE) {
2812 		if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2813 			err = secpolicy_setid_setsticky_clear(vp, vap,
2814 			    &oldva, cr);
2815 			if (err) {
2816 				ZFS_EXIT(zfsvfs);
2817 				return (err);
2818 			}
2819 			trim_mask |= AT_MODE;
2820 		} else {
2821 			need_policy = TRUE;
2822 		}
2823 	}
2824 
2825 	if (need_policy) {
2826 		/*
2827 		 * If trim_mask is set then take ownership
2828 		 * has been granted or write_acl is present and user
2829 		 * has the ability to modify mode.  In that case remove
2830 		 * UID|GID and or MODE from mask so that
2831 		 * secpolicy_vnode_setattr() doesn't revoke it.
2832 		 */
2833 
2834 		if (trim_mask) {
2835 			saved_mask = vap->va_mask;
2836 			vap->va_mask &= ~trim_mask;
2837 		}
2838 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2839 		    (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2840 		if (err) {
2841 			ZFS_EXIT(zfsvfs);
2842 			return (err);
2843 		}
2844 
2845 		if (trim_mask)
2846 			vap->va_mask |= saved_mask;
2847 	}
2848 
2849 	/*
2850 	 * secpolicy_vnode_setattr, or take ownership may have
2851 	 * changed va_mask
2852 	 */
2853 	mask = vap->va_mask;
2854 
2855 	if ((mask & (AT_UID | AT_GID))) {
2856 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xattr_obj,
2857 		    sizeof (xattr_obj));
2858 
2859 		if (xattr_obj) {
2860 			err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
2861 			if (err)
2862 				goto out2;
2863 		}
2864 		if (mask & AT_UID) {
2865 			new_uid = zfs_fuid_create(zfsvfs,
2866 			    (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2867 			if (vap->va_uid != zp->z_uid &&
2868 			    zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
2869 				err = EDQUOT;
2870 				goto out2;
2871 			}
2872 		}
2873 
2874 		if (mask & AT_GID) {
2875 			new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
2876 			    cr, ZFS_GROUP, &fuidp);
2877 			if (new_gid != zp->z_gid &&
2878 			    zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
2879 				err = EDQUOT;
2880 				goto out2;
2881 			}
2882 		}
2883 	}
2884 	tx = dmu_tx_create(zfsvfs->z_os);
2885 
2886 	if (mask & AT_MODE) {
2887 		uint64_t pmode = zp->z_mode;
2888 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2889 
2890 		if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
2891 			goto out;
2892 
2893 		if (!zp->z_is_sa && ZFS_EXTERNAL_ACL(zp)) {
2894 			/*
2895 			 * Are we upgrading ACL from old V0 format
2896 			 * to V1 format?
2897 			 */
2898 			if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
2899 			    ZNODE_ACL_VERSION(zp) ==
2900 			    ZFS_ACL_VERSION_INITIAL) {
2901 				dmu_tx_hold_free(tx,
2902 				    ZFS_EXTERNAL_ACL(zp), 0,
2903 				    DMU_OBJECT_END);
2904 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2905 				    0, aclp->z_acl_bytes);
2906 			} else {
2907 				dmu_tx_hold_write(tx, ZFS_EXTERNAL_ACL(zp), 0,
2908 				    aclp->z_acl_bytes);
2909 			}
2910 		} else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2911 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2912 			    0, aclp->z_acl_bytes);
2913 		}
2914 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2915 	} else {
2916 		if ((mask & AT_XVATTR) &&
2917 		    XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2918 			dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2919 		else
2920 			dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2921 	}
2922 
2923 	if (attrzp) {
2924 		dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
2925 	}
2926 
2927 	fuid_dirtied = zfsvfs->z_fuid_dirty;
2928 	if (fuid_dirtied)
2929 		zfs_fuid_txhold(zfsvfs, tx);
2930 
2931 	zfs_sa_upgrade_txholds(tx, zp);
2932 
2933 	err = dmu_tx_assign(tx, TXG_NOWAIT);
2934 	if (err) {
2935 		if (err == ERESTART)
2936 			dmu_tx_wait(tx);
2937 		goto out;
2938 	}
2939 
2940 	count = 0;
2941 	/*
2942 	 * Set each attribute requested.
2943 	 * We group settings according to the locks they need to acquire.
2944 	 *
2945 	 * Note: you cannot set ctime directly, although it will be
2946 	 * updated as a side-effect of calling this function.
2947 	 */
2948 
2949 	mutex_enter(&zp->z_lock);
2950 
2951 	if (attrzp)
2952 		mutex_enter(&attrzp->z_lock);
2953 
2954 	if (mask & (AT_UID|AT_GID)) {
2955 
2956 		if (mask & AT_UID) {
2957 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
2958 			    &new_uid, sizeof (new_uid));
2959 			zp->z_uid = zfs_fuid_map_id(zfsvfs, new_uid,
2960 			    cr, ZFS_OWNER);
2961 			if (attrzp) {
2962 				SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2963 				    SA_ZPL_UID(zfsvfs), NULL, &new_uid,
2964 				    sizeof (new_uid));
2965 				attrzp->z_gid = zp->z_uid;
2966 			}
2967 		}
2968 
2969 		if (mask & AT_GID) {
2970 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
2971 			    NULL, &new_gid, sizeof (new_gid));
2972 			zp->z_gid = zfs_fuid_map_id(zfsvfs, new_gid, cr,
2973 			    ZFS_GROUP);
2974 			if (attrzp) {
2975 				SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2976 				    SA_ZPL_GID(zfsvfs), NULL, &new_gid,
2977 				    sizeof (new_gid));
2978 				attrzp->z_gid = zp->z_gid;
2979 			}
2980 		}
2981 		if (!(mask & AT_MODE)) {
2982 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
2983 			    NULL, &new_mode, sizeof (new_mode));
2984 			new_mode = zp->z_mode;
2985 		}
2986 		err = zfs_acl_chown_setattr(zp);
2987 		ASSERT(err == 0);
2988 		if (attrzp) {
2989 			err = zfs_acl_chown_setattr(attrzp);
2990 			ASSERT(err == 0);
2991 		}
2992 	}
2993 
2994 	if (mask & AT_MODE) {
2995 		mutex_enter(&zp->z_acl_lock);
2996 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
2997 		    &new_mode, sizeof (new_mode));
2998 		zp->z_mode = new_mode;
2999 		ASSERT3U((uintptr_t)aclp, !=, NULL);
3000 		err = zfs_aclset_common(zp, aclp, cr, tx);
3001 		ASSERT3U(err, ==, 0);
3002 		zp->z_acl_cached = aclp;
3003 		aclp = NULL;
3004 		mutex_exit(&zp->z_acl_lock);
3005 	}
3006 
3007 	if (attrzp)
3008 		mutex_exit(&attrzp->z_lock);
3009 
3010 	if (mask & AT_ATIME) {
3011 		ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3012 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3013 		    &zp->z_atime, sizeof (zp->z_atime));
3014 	}
3015 
3016 	if (mask & AT_MTIME) {
3017 		ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3018 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3019 		    mtime, sizeof (mtime));
3020 	}
3021 
3022 	/* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3023 	if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3024 		if (!(mask & AT_MTIME))
3025 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3026 			    NULL, mtime, sizeof (mtime));
3027 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3028 		    &ctime, sizeof (ctime));
3029 		zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3030 		    B_TRUE);
3031 	} else if (mask != 0) {
3032 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3033 		    &ctime, sizeof (ctime));
3034 		zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3035 		    B_TRUE);
3036 		if (attrzp) {
3037 			SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3038 			    SA_ZPL_CTIME(zfsvfs), NULL,
3039 			    &ctime, sizeof (ctime));
3040 			zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3041 			    mtime, ctime, B_TRUE);
3042 		}
3043 	}
3044 	/*
3045 	 * Do this after setting timestamps to prevent timestamp
3046 	 * update from toggling bit
3047 	 */
3048 
3049 	if (xoap && (mask & AT_XVATTR)) {
3050 
3051 		/*
3052 		 * restore trimmed off masks
3053 		 * so that return masks can be set for caller.
3054 		 */
3055 
3056 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3057 			XVA_SET_REQ(xvap, XAT_APPENDONLY);
3058 		}
3059 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3060 			XVA_SET_REQ(xvap, XAT_NOUNLINK);
3061 		}
3062 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3063 			XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3064 		}
3065 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3066 			XVA_SET_REQ(xvap, XAT_NODUMP);
3067 		}
3068 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3069 			XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3070 		}
3071 		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3072 			XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3073 		}
3074 
3075 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3076 			ASSERT(vp->v_type == VREG);
3077 
3078 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3079 		    &zp->z_pflags, sizeof (zp->z_pflags));
3080 		zfs_xvattr_set(zp, xvap, tx);
3081 	}
3082 
3083 	if (fuid_dirtied)
3084 		zfs_fuid_sync(zfsvfs, tx);
3085 
3086 	if (mask != 0)
3087 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3088 
3089 	mutex_exit(&zp->z_lock);
3090 
3091 out:
3092 	if (err == 0 && attrzp) {
3093 		err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3094 		    xattr_count, tx);
3095 		ASSERT(err2 == 0);
3096 	}
3097 
3098 	if (attrzp)
3099 		VN_RELE(ZTOV(attrzp));
3100 	if (aclp)
3101 		zfs_acl_free(aclp);
3102 
3103 	if (fuidp) {
3104 		zfs_fuid_info_free(fuidp);
3105 		fuidp = NULL;
3106 	}
3107 
3108 	if (err) {
3109 		dmu_tx_abort(tx);
3110 		if (err == ERESTART)
3111 			goto top;
3112 	} else {
3113 		err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3114 		dmu_tx_commit(tx);
3115 	}
3116 
3117 
3118 out2:
3119 	ZFS_EXIT(zfsvfs);
3120 	return (err);
3121 }
3122 
3123 typedef struct zfs_zlock {
3124 	krwlock_t	*zl_rwlock;	/* lock we acquired */
3125 	znode_t		*zl_znode;	/* znode we held */
3126 	struct zfs_zlock *zl_next;	/* next in list */
3127 } zfs_zlock_t;
3128 
3129 /*
3130  * Drop locks and release vnodes that were held by zfs_rename_lock().
3131  */
3132 static void
3133 zfs_rename_unlock(zfs_zlock_t **zlpp)
3134 {
3135 	zfs_zlock_t *zl;
3136 
3137 	while ((zl = *zlpp) != NULL) {
3138 		if (zl->zl_znode != NULL)
3139 			VN_RELE(ZTOV(zl->zl_znode));
3140 		rw_exit(zl->zl_rwlock);
3141 		*zlpp = zl->zl_next;
3142 		kmem_free(zl, sizeof (*zl));
3143 	}
3144 }
3145 
3146 /*
3147  * Search back through the directory tree, using the ".." entries.
3148  * Lock each directory in the chain to prevent concurrent renames.
3149  * Fail any attempt to move a directory into one of its own descendants.
3150  * XXX - z_parent_lock can overlap with map or grow locks
3151  */
3152 static int
3153 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3154 {
3155 	zfs_zlock_t	*zl;
3156 	znode_t		*zp = tdzp;
3157 	uint64_t	rootid = zp->z_zfsvfs->z_root;
3158 	uint64_t	oidp = zp->z_id;
3159 	krwlock_t	*rwlp = &szp->z_parent_lock;
3160 	krw_t		rw = RW_WRITER;
3161 
3162 	/*
3163 	 * First pass write-locks szp and compares to zp->z_id.
3164 	 * Later passes read-lock zp and compare to zp->z_parent.
3165 	 */
3166 	do {
3167 		if (!rw_tryenter(rwlp, rw)) {
3168 			/*
3169 			 * Another thread is renaming in this path.
3170 			 * Note that if we are a WRITER, we don't have any
3171 			 * parent_locks held yet.
3172 			 */
3173 			if (rw == RW_READER && zp->z_id > szp->z_id) {
3174 				/*
3175 				 * Drop our locks and restart
3176 				 */
3177 				zfs_rename_unlock(&zl);
3178 				*zlpp = NULL;
3179 				zp = tdzp;
3180 				oidp = zp->z_id;
3181 				rwlp = &szp->z_parent_lock;
3182 				rw = RW_WRITER;
3183 				continue;
3184 			} else {
3185 				/*
3186 				 * Wait for other thread to drop its locks
3187 				 */
3188 				rw_enter(rwlp, rw);
3189 			}
3190 		}
3191 
3192 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3193 		zl->zl_rwlock = rwlp;
3194 		zl->zl_znode = NULL;
3195 		zl->zl_next = *zlpp;
3196 		*zlpp = zl;
3197 
3198 		if (oidp == szp->z_id)		/* We're a descendant of szp */
3199 			return (EINVAL);
3200 
3201 		if (oidp == rootid)		/* We've hit the top */
3202 			return (0);
3203 
3204 		if (rw == RW_READER) {		/* i.e. not the first pass */
3205 			int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3206 			if (error)
3207 				return (error);
3208 			zl->zl_znode = zp;
3209 		}
3210 		(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
3211 		    &oidp, sizeof (oidp));
3212 		rwlp = &zp->z_parent_lock;
3213 		rw = RW_READER;
3214 
3215 	} while (zp->z_id != sdzp->z_id);
3216 
3217 	return (0);
3218 }
3219 
3220 /*
3221  * Move an entry from the provided source directory to the target
3222  * directory.  Change the entry name as indicated.
3223  *
3224  *	IN:	sdvp	- Source directory containing the "old entry".
3225  *		snm	- Old entry name.
3226  *		tdvp	- Target directory to contain the "new entry".
3227  *		tnm	- New entry name.
3228  *		cr	- credentials of caller.
3229  *		ct	- caller context
3230  *		flags	- case flags
3231  *
3232  *	RETURN:	0 if success
3233  *		error code if failure
3234  *
3235  * Timestamps:
3236  *	sdvp,tdvp - ctime|mtime updated
3237  */
3238 /*ARGSUSED*/
3239 static int
3240 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3241     caller_context_t *ct, int flags)
3242 {
3243 	znode_t		*tdzp, *szp, *tzp;
3244 	znode_t		*sdzp = VTOZ(sdvp);
3245 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
3246 	zilog_t		*zilog;
3247 	vnode_t		*realvp;
3248 	zfs_dirlock_t	*sdl, *tdl;
3249 	dmu_tx_t	*tx;
3250 	zfs_zlock_t	*zl;
3251 	int		cmp, serr, terr;
3252 	int		error = 0;
3253 	int		zflg = 0;
3254 
3255 	ZFS_ENTER(zfsvfs);
3256 	ZFS_VERIFY_ZP(sdzp);
3257 	zilog = zfsvfs->z_log;
3258 
3259 	/*
3260 	 * Make sure we have the real vp for the target directory.
3261 	 */
3262 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3263 		tdvp = realvp;
3264 
3265 	if (tdvp->v_vfsp != sdvp->v_vfsp || zfsctl_is_node(tdvp)) {
3266 		ZFS_EXIT(zfsvfs);
3267 		return (EXDEV);
3268 	}
3269 
3270 	tdzp = VTOZ(tdvp);
3271 	ZFS_VERIFY_ZP(tdzp);
3272 	if (zfsvfs->z_utf8 && u8_validate(tnm,
3273 	    strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3274 		ZFS_EXIT(zfsvfs);
3275 		return (EILSEQ);
3276 	}
3277 
3278 	if (flags & FIGNORECASE)
3279 		zflg |= ZCILOOK;
3280 
3281 top:
3282 	szp = NULL;
3283 	tzp = NULL;
3284 	zl = NULL;
3285 
3286 	/*
3287 	 * This is to prevent the creation of links into attribute space
3288 	 * by renaming a linked file into/outof an attribute directory.
3289 	 * See the comment in zfs_link() for why this is considered bad.
3290 	 */
3291 	if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3292 		ZFS_EXIT(zfsvfs);
3293 		return (EINVAL);
3294 	}
3295 
3296 	/*
3297 	 * Lock source and target directory entries.  To prevent deadlock,
3298 	 * a lock ordering must be defined.  We lock the directory with
3299 	 * the smallest object id first, or if it's a tie, the one with
3300 	 * the lexically first name.
3301 	 */
3302 	if (sdzp->z_id < tdzp->z_id) {
3303 		cmp = -1;
3304 	} else if (sdzp->z_id > tdzp->z_id) {
3305 		cmp = 1;
3306 	} else {
3307 		/*
3308 		 * First compare the two name arguments without
3309 		 * considering any case folding.
3310 		 */
3311 		int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3312 
3313 		cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3314 		ASSERT(error == 0 || !zfsvfs->z_utf8);
3315 		if (cmp == 0) {
3316 			/*
3317 			 * POSIX: "If the old argument and the new argument
3318 			 * both refer to links to the same existing file,
3319 			 * the rename() function shall return successfully
3320 			 * and perform no other action."
3321 			 */
3322 			ZFS_EXIT(zfsvfs);
3323 			return (0);
3324 		}
3325 		/*
3326 		 * If the file system is case-folding, then we may
3327 		 * have some more checking to do.  A case-folding file
3328 		 * system is either supporting mixed case sensitivity
3329 		 * access or is completely case-insensitive.  Note
3330 		 * that the file system is always case preserving.
3331 		 *
3332 		 * In mixed sensitivity mode case sensitive behavior
3333 		 * is the default.  FIGNORECASE must be used to
3334 		 * explicitly request case insensitive behavior.
3335 		 *
3336 		 * If the source and target names provided differ only
3337 		 * by case (e.g., a request to rename 'tim' to 'Tim'),
3338 		 * we will treat this as a special case in the
3339 		 * case-insensitive mode: as long as the source name
3340 		 * is an exact match, we will allow this to proceed as
3341 		 * a name-change request.
3342 		 */
3343 		if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3344 		    (zfsvfs->z_case == ZFS_CASE_MIXED &&
3345 		    flags & FIGNORECASE)) &&
3346 		    u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3347 		    &error) == 0) {
3348 			/*
3349 			 * case preserving rename request, require exact
3350 			 * name matches
3351 			 */
3352 			zflg |= ZCIEXACT;
3353 			zflg &= ~ZCILOOK;
3354 		}
3355 	}
3356 
3357 	/*
3358 	 * If the source and destination directories are the same, we should
3359 	 * grab the z_name_lock of that directory only once.
3360 	 */
3361 	if (sdzp == tdzp) {
3362 		zflg |= ZHAVELOCK;
3363 		rw_enter(&sdzp->z_name_lock, RW_READER);
3364 	}
3365 
3366 	if (cmp < 0) {
3367 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3368 		    ZEXISTS | zflg, NULL, NULL);
3369 		terr = zfs_dirent_lock(&tdl,
3370 		    tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3371 	} else {
3372 		terr = zfs_dirent_lock(&tdl,
3373 		    tdzp, tnm, &tzp, zflg, NULL, NULL);
3374 		serr = zfs_dirent_lock(&sdl,
3375 		    sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3376 		    NULL, NULL);
3377 	}
3378 
3379 	if (serr) {
3380 		/*
3381 		 * Source entry invalid or not there.
3382 		 */
3383 		if (!terr) {
3384 			zfs_dirent_unlock(tdl);
3385 			if (tzp)
3386 				VN_RELE(ZTOV(tzp));
3387 		}
3388 
3389 		if (sdzp == tdzp)
3390 			rw_exit(&sdzp->z_name_lock);
3391 
3392 		if (strcmp(snm, "..") == 0)
3393 			serr = EINVAL;
3394 		ZFS_EXIT(zfsvfs);
3395 		return (serr);
3396 	}
3397 	if (terr) {
3398 		zfs_dirent_unlock(sdl);
3399 		VN_RELE(ZTOV(szp));
3400 
3401 		if (sdzp == tdzp)
3402 			rw_exit(&sdzp->z_name_lock);
3403 
3404 		if (strcmp(tnm, "..") == 0)
3405 			terr = EINVAL;
3406 		ZFS_EXIT(zfsvfs);
3407 		return (terr);
3408 	}
3409 
3410 	/*
3411 	 * Must have write access at the source to remove the old entry
3412 	 * and write access at the target to create the new entry.
3413 	 * Note that if target and source are the same, this can be
3414 	 * done in a single check.
3415 	 */
3416 
3417 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3418 		goto out;
3419 
3420 	if (ZTOV(szp)->v_type == VDIR) {
3421 		/*
3422 		 * Check to make sure rename is valid.
3423 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3424 		 */
3425 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3426 			goto out;
3427 	}
3428 
3429 	/*
3430 	 * Does target exist?
3431 	 */
3432 	if (tzp) {
3433 		/*
3434 		 * Source and target must be the same type.
3435 		 */
3436 		if (ZTOV(szp)->v_type == VDIR) {
3437 			if (ZTOV(tzp)->v_type != VDIR) {
3438 				error = ENOTDIR;
3439 				goto out;
3440 			}
3441 		} else {
3442 			if (ZTOV(tzp)->v_type == VDIR) {
3443 				error = EISDIR;
3444 				goto out;
3445 			}
3446 		}
3447 		/*
3448 		 * POSIX dictates that when the source and target
3449 		 * entries refer to the same file object, rename
3450 		 * must do nothing and exit without error.
3451 		 */
3452 		if (szp->z_id == tzp->z_id) {
3453 			error = 0;
3454 			goto out;
3455 		}
3456 	}
3457 
3458 	vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3459 	if (tzp)
3460 		vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3461 
3462 	/*
3463 	 * notify the target directory if it is not the same
3464 	 * as source directory.
3465 	 */
3466 	if (tdvp != sdvp) {
3467 		vnevent_rename_dest_dir(tdvp, ct);
3468 	}
3469 
3470 	tx = dmu_tx_create(zfsvfs->z_os);
3471 	dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3472 	dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3473 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3474 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3475 	if (sdzp != tdzp) {
3476 		dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3477 		zfs_sa_upgrade_txholds(tx, tdzp);
3478 	}
3479 	if (tzp) {
3480 		dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3481 		zfs_sa_upgrade_txholds(tx, tzp);
3482 	}
3483 
3484 	zfs_sa_upgrade_txholds(tx, szp);
3485 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3486 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3487 	if (error) {
3488 		if (zl != NULL)
3489 			zfs_rename_unlock(&zl);
3490 		zfs_dirent_unlock(sdl);
3491 		zfs_dirent_unlock(tdl);
3492 
3493 		if (sdzp == tdzp)
3494 			rw_exit(&sdzp->z_name_lock);
3495 
3496 		VN_RELE(ZTOV(szp));
3497 		if (tzp)
3498 			VN_RELE(ZTOV(tzp));
3499 		if (error == ERESTART) {
3500 			dmu_tx_wait(tx);
3501 			dmu_tx_abort(tx);
3502 			goto top;
3503 		}
3504 		dmu_tx_abort(tx);
3505 		ZFS_EXIT(zfsvfs);
3506 		return (error);
3507 	}
3508 
3509 	if (tzp)	/* Attempt to remove the existing target */
3510 		error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3511 
3512 	if (error == 0) {
3513 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3514 		if (error == 0) {
3515 			szp->z_pflags |= ZFS_AV_MODIFIED;
3516 
3517 			error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3518 			    (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3519 			ASSERT3U(error, ==, 0);
3520 
3521 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3522 			ASSERT3U(error, ==, 0);
3523 
3524 			zfs_log_rename(zilog, tx,
3525 			    TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0),
3526 			    sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
3527 
3528 			/* Update path information for the target vnode */
3529 			vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm));
3530 		}
3531 	}
3532 
3533 	dmu_tx_commit(tx);
3534 out:
3535 	if (zl != NULL)
3536 		zfs_rename_unlock(&zl);
3537 
3538 	zfs_dirent_unlock(sdl);
3539 	zfs_dirent_unlock(tdl);
3540 
3541 	if (sdzp == tdzp)
3542 		rw_exit(&sdzp->z_name_lock);
3543 
3544 
3545 	VN_RELE(ZTOV(szp));
3546 	if (tzp)
3547 		VN_RELE(ZTOV(tzp));
3548 
3549 	ZFS_EXIT(zfsvfs);
3550 	return (error);
3551 }
3552 
3553 /*
3554  * Insert the indicated symbolic reference entry into the directory.
3555  *
3556  *	IN:	dvp	- Directory to contain new symbolic link.
3557  *		link	- Name for new symlink entry.
3558  *		vap	- Attributes of new entry.
3559  *		target	- Target path of new symlink.
3560  *		cr	- credentials of caller.
3561  *		ct	- caller context
3562  *		flags	- case flags
3563  *
3564  *	RETURN:	0 if success
3565  *		error code if failure
3566  *
3567  * Timestamps:
3568  *	dvp - ctime|mtime updated
3569  */
3570 /*ARGSUSED*/
3571 static int
3572 zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
3573     caller_context_t *ct, int flags)
3574 {
3575 	znode_t		*zp, *dzp = VTOZ(dvp);
3576 	zfs_dirlock_t	*dl;
3577 	dmu_tx_t	*tx;
3578 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
3579 	zilog_t		*zilog;
3580 	uint64_t	len = strlen(link);
3581 	int		error;
3582 	int		zflg = ZNEW;
3583 	zfs_acl_ids_t	acl_ids;
3584 	boolean_t	fuid_dirtied;
3585 	uint64_t	txtype = TX_SYMLINK;
3586 
3587 	ASSERT(vap->va_type == VLNK);
3588 
3589 	ZFS_ENTER(zfsvfs);
3590 	ZFS_VERIFY_ZP(dzp);
3591 	zilog = zfsvfs->z_log;
3592 
3593 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3594 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3595 		ZFS_EXIT(zfsvfs);
3596 		return (EILSEQ);
3597 	}
3598 	if (flags & FIGNORECASE)
3599 		zflg |= ZCILOOK;
3600 top:
3601 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3602 		ZFS_EXIT(zfsvfs);
3603 		return (error);
3604 	}
3605 
3606 	if (len > MAXPATHLEN) {
3607 		ZFS_EXIT(zfsvfs);
3608 		return (ENAMETOOLONG);
3609 	}
3610 
3611 	/*
3612 	 * Attempt to lock directory; fail if entry already exists.
3613 	 */
3614 	error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3615 	if (error) {
3616 		ZFS_EXIT(zfsvfs);
3617 		return (error);
3618 	}
3619 
3620 	VERIFY(0 == zfs_acl_ids_create(dzp, 0, vap, cr, NULL, &acl_ids));
3621 	if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
3622 		zfs_acl_ids_free(&acl_ids);
3623 		zfs_dirent_unlock(dl);
3624 		ZFS_EXIT(zfsvfs);
3625 		return (EDQUOT);
3626 	}
3627 	tx = dmu_tx_create(zfsvfs->z_os);
3628 	fuid_dirtied = zfsvfs->z_fuid_dirty;
3629 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3630 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3631 	dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3632 	    ZFS_SA_BASE_ATTR_SIZE + len);
3633 	dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3634 	if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3635 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3636 		    acl_ids.z_aclp->z_acl_bytes);
3637 	}
3638 	if (fuid_dirtied)
3639 		zfs_fuid_txhold(zfsvfs, tx);
3640 	error = dmu_tx_assign(tx, TXG_NOWAIT);
3641 	if (error) {
3642 		zfs_acl_ids_free(&acl_ids);
3643 		zfs_dirent_unlock(dl);
3644 		if (error == ERESTART) {
3645 			dmu_tx_wait(tx);
3646 			dmu_tx_abort(tx);
3647 			goto top;
3648 		}
3649 		dmu_tx_abort(tx);
3650 		ZFS_EXIT(zfsvfs);
3651 		return (error);
3652 	}
3653 
3654 	/*
3655 	 * Create a new object for the symlink.
3656 	 * for version 4 ZPL datsets the symlink will be an SA attribute
3657 	 */
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