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