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