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