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