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