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