xref: /titanic_44/usr/src/uts/common/fs/tmpfs/tmp_vnops.c (revision 62a24de03df1f2399ceda704cb3874dabc98bbbd)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/t_lock.h>
29 #include <sys/systm.h>
30 #include <sys/sysmacros.h>
31 #include <sys/user.h>
32 #include <sys/time.h>
33 #include <sys/vfs.h>
34 #include <sys/vfs_opreg.h>
35 #include <sys/vnode.h>
36 #include <sys/file.h>
37 #include <sys/fcntl.h>
38 #include <sys/flock.h>
39 #include <sys/kmem.h>
40 #include <sys/uio.h>
41 #include <sys/errno.h>
42 #include <sys/stat.h>
43 #include <sys/cred.h>
44 #include <sys/dirent.h>
45 #include <sys/pathname.h>
46 #include <sys/vmsystm.h>
47 #include <sys/fs/tmp.h>
48 #include <sys/fs/tmpnode.h>
49 #include <sys/mman.h>
50 #include <vm/hat.h>
51 #include <vm/seg_vn.h>
52 #include <vm/seg_map.h>
53 #include <vm/seg.h>
54 #include <vm/anon.h>
55 #include <vm/as.h>
56 #include <vm/page.h>
57 #include <vm/pvn.h>
58 #include <sys/cmn_err.h>
59 #include <sys/debug.h>
60 #include <sys/swap.h>
61 #include <sys/buf.h>
62 #include <sys/vm.h>
63 #include <sys/vtrace.h>
64 #include <sys/policy.h>
65 #include <fs/fs_subr.h>
66 
67 static int	tmp_getapage(struct vnode *, u_offset_t, size_t, uint_t *,
68 	page_t **, size_t, struct seg *, caddr_t, enum seg_rw, struct cred *);
69 static int 	tmp_putapage(struct vnode *, page_t *, u_offset_t *, size_t *,
70 	int, struct cred *);
71 
72 /* ARGSUSED1 */
73 static int
74 tmp_open(struct vnode **vpp, int flag, struct cred *cred, caller_context_t *ct)
75 {
76 	/*
77 	 * swapon to a tmpfs file is not supported so access
78 	 * is denied on open if VISSWAP is set.
79 	 */
80 	if ((*vpp)->v_flag & VISSWAP)
81 		return (EINVAL);
82 	return (0);
83 }
84 
85 /* ARGSUSED1 */
86 static int
87 tmp_close(
88 	struct vnode *vp,
89 	int flag,
90 	int count,
91 	offset_t offset,
92 	struct cred *cred,
93 	caller_context_t *ct)
94 {
95 	cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
96 	cleanshares(vp, ttoproc(curthread)->p_pid);
97 	return (0);
98 }
99 
100 /*
101  * wrtmp does the real work of write requests for tmpfs.
102  */
103 static int
104 wrtmp(
105 	struct tmount *tm,
106 	struct tmpnode *tp,
107 	struct uio *uio,
108 	struct cred *cr,
109 	struct caller_context *ct)
110 {
111 	pgcnt_t pageoffset;	/* offset in pages */
112 	ulong_t segmap_offset;	/* pagesize byte offset into segmap */
113 	caddr_t base;		/* base of segmap */
114 	ssize_t bytes;		/* bytes to uiomove */
115 	pfn_t pagenumber;	/* offset in pages into tmp file */
116 	struct vnode *vp;
117 	int error = 0;
118 	int	pagecreate;	/* == 1 if we allocated a page */
119 	int	newpage;
120 	rlim64_t limit = uio->uio_llimit;
121 	long oresid = uio->uio_resid;
122 	timestruc_t now;
123 
124 	long tn_size_changed = 0;
125 	long old_tn_size;
126 	long new_tn_size;
127 
128 	vp = TNTOV(tp);
129 	ASSERT(vp->v_type == VREG);
130 
131 	TRACE_1(TR_FAC_TMPFS, TR_TMPFS_RWTMP_START,
132 	    "tmp_wrtmp_start:vp %p", vp);
133 
134 	ASSERT(RW_WRITE_HELD(&tp->tn_contents));
135 	ASSERT(RW_WRITE_HELD(&tp->tn_rwlock));
136 
137 	if (MANDLOCK(vp, tp->tn_mode)) {
138 		rw_exit(&tp->tn_contents);
139 		/*
140 		 * tmp_getattr ends up being called by chklock
141 		 */
142 		error = chklock(vp, FWRITE, uio->uio_loffset, uio->uio_resid,
143 		    uio->uio_fmode, ct);
144 		rw_enter(&tp->tn_contents, RW_WRITER);
145 		if (error != 0) {
146 			TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
147 			    "tmp_wrtmp_end:vp %p error %d", vp, error);
148 			return (error);
149 		}
150 	}
151 
152 	if (uio->uio_loffset < 0)
153 		return (EINVAL);
154 
155 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
156 		limit = MAXOFFSET_T;
157 
158 	if (uio->uio_loffset >= limit) {
159 		proc_t *p = ttoproc(curthread);
160 
161 		mutex_enter(&p->p_lock);
162 		(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], p->p_rctls,
163 		    p, RCA_UNSAFE_SIGINFO);
164 		mutex_exit(&p->p_lock);
165 		return (EFBIG);
166 	}
167 
168 	if (uio->uio_loffset >= MAXOFF_T) {
169 		TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
170 		    "tmp_wrtmp_end:vp %p error %d", vp, EINVAL);
171 		return (EFBIG);
172 	}
173 
174 	if (uio->uio_resid == 0) {
175 		TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
176 		    "tmp_wrtmp_end:vp %p error %d", vp, 0);
177 		return (0);
178 	}
179 
180 	if (limit > MAXOFF_T)
181 		limit = MAXOFF_T;
182 
183 	do {
184 		long	offset;
185 		long	delta;
186 
187 		offset = (long)uio->uio_offset;
188 		pageoffset = offset & PAGEOFFSET;
189 		/*
190 		 * A maximum of PAGESIZE bytes of data is transferred
191 		 * each pass through this loop
192 		 */
193 		bytes = MIN(PAGESIZE - pageoffset, uio->uio_resid);
194 
195 		if (offset + bytes >= limit) {
196 			if (offset >= limit) {
197 				error = EFBIG;
198 				goto out;
199 			}
200 			bytes = limit - offset;
201 		}
202 		pagenumber = btop(offset);
203 
204 		/*
205 		 * delta is the amount of anonymous memory
206 		 * to reserve for the file.
207 		 * We always reserve in pagesize increments so
208 		 * unless we're extending the file into a new page,
209 		 * we don't need to call tmp_resv.
210 		 */
211 		delta = offset + bytes -
212 		    P2ROUNDUP_TYPED(tp->tn_size, PAGESIZE, u_offset_t);
213 		if (delta > 0) {
214 			pagecreate = 1;
215 			if (tmp_resv(tm, tp, delta, pagecreate)) {
216 				/*
217 				 * Log file system full in the zone that owns
218 				 * the tmpfs mount, as well as in the global
219 				 * zone if necessary.
220 				 */
221 				zcmn_err(tm->tm_vfsp->vfs_zone->zone_id,
222 				    CE_WARN, "%s: File system full, "
223 				    "swap space limit exceeded",
224 				    tm->tm_mntpath);
225 
226 				if (tm->tm_vfsp->vfs_zone->zone_id !=
227 				    GLOBAL_ZONEID) {
228 
229 					vfs_t *vfs = tm->tm_vfsp;
230 
231 					zcmn_err(GLOBAL_ZONEID,
232 					    CE_WARN, "%s: File system full, "
233 					    "swap space limit exceeded",
234 					    vfs->vfs_vnodecovered->v_path);
235 				}
236 				error = ENOSPC;
237 				break;
238 			}
239 			tmpnode_growmap(tp, (ulong_t)offset + bytes);
240 		}
241 		/* grow the file to the new length */
242 		if (offset + bytes > tp->tn_size) {
243 			tn_size_changed = 1;
244 			old_tn_size = tp->tn_size;
245 			/*
246 			 * Postpone updating tp->tn_size until uiomove() is
247 			 * done.
248 			 */
249 			new_tn_size = offset + bytes;
250 		}
251 		if (bytes == PAGESIZE) {
252 			/*
253 			 * Writing whole page so reading from disk
254 			 * is a waste
255 			 */
256 			pagecreate = 1;
257 		} else {
258 			pagecreate = 0;
259 		}
260 		/*
261 		 * If writing past EOF or filling in a hole
262 		 * we need to allocate an anon slot.
263 		 */
264 		if (anon_get_ptr(tp->tn_anon, pagenumber) == NULL) {
265 			(void) anon_set_ptr(tp->tn_anon, pagenumber,
266 			    anon_alloc(vp, ptob(pagenumber)), ANON_SLEEP);
267 			pagecreate = 1;
268 			tp->tn_nblocks++;
269 		}
270 
271 		/*
272 		 * We have to drop the contents lock to allow the VM
273 		 * system to reacquire it in tmp_getpage()
274 		 */
275 		rw_exit(&tp->tn_contents);
276 
277 		newpage = 0;
278 		if (vpm_enable) {
279 			/*
280 			 * Copy data. If new pages are created, part of
281 			 * the page that is not written will be initizliazed
282 			 * with zeros.
283 			 */
284 			error = vpm_data_copy(vp, offset, bytes, uio,
285 			    !pagecreate, &newpage, 1, S_WRITE);
286 		} else {
287 			/* Get offset within the segmap mapping */
288 			segmap_offset = (offset & PAGEMASK) & MAXBOFFSET;
289 			base = segmap_getmapflt(segkmap, vp,
290 			    (offset &  MAXBMASK), PAGESIZE, !pagecreate,
291 			    S_WRITE);
292 		}
293 
294 
295 		if (!vpm_enable && pagecreate) {
296 			/*
297 			 * segmap_pagecreate() returns 1 if it calls
298 			 * page_create_va() to allocate any pages.
299 			 */
300 			newpage = segmap_pagecreate(segkmap,
301 			    base + segmap_offset, (size_t)PAGESIZE, 0);
302 			/*
303 			 * Clear from the beginning of the page to the starting
304 			 * offset of the data.
305 			 */
306 			if (pageoffset != 0)
307 				(void) kzero(base + segmap_offset,
308 				    (size_t)pageoffset);
309 		}
310 
311 		if (!vpm_enable) {
312 			error = uiomove(base + segmap_offset + pageoffset,
313 			    (long)bytes, UIO_WRITE, uio);
314 		}
315 
316 		if (!vpm_enable && pagecreate &&
317 		    uio->uio_offset < P2ROUNDUP(offset + bytes, PAGESIZE)) {
318 			long	zoffset; /* zero from offset into page */
319 			/*
320 			 * We created pages w/o initializing them completely,
321 			 * thus we need to zero the part that wasn't set up.
322 			 * This happens on most EOF write cases and if
323 			 * we had some sort of error during the uiomove.
324 			 */
325 			long nmoved;
326 
327 			nmoved = uio->uio_offset - offset;
328 			ASSERT((nmoved + pageoffset) <= PAGESIZE);
329 
330 			/*
331 			 * Zero from the end of data in the page to the
332 			 * end of the page.
333 			 */
334 			if ((zoffset = pageoffset + nmoved) < PAGESIZE)
335 				(void) kzero(base + segmap_offset + zoffset,
336 				    (size_t)PAGESIZE - zoffset);
337 		}
338 
339 		/*
340 		 * Unlock the pages which have been allocated by
341 		 * page_create_va() in segmap_pagecreate()
342 		 */
343 		if (!vpm_enable && newpage) {
344 			segmap_pageunlock(segkmap, base + segmap_offset,
345 			    (size_t)PAGESIZE, S_WRITE);
346 		}
347 
348 		if (error) {
349 			/*
350 			 * If we failed on a write, we must
351 			 * be sure to invalidate any pages that may have
352 			 * been allocated.
353 			 */
354 			if (vpm_enable) {
355 				(void) vpm_sync_pages(vp, offset, PAGESIZE,
356 				    SM_INVAL);
357 			} else {
358 				(void) segmap_release(segkmap, base, SM_INVAL);
359 			}
360 		} else {
361 			if (vpm_enable) {
362 				error = vpm_sync_pages(vp, offset, PAGESIZE,
363 				    0);
364 			} else {
365 				error = segmap_release(segkmap, base, 0);
366 			}
367 		}
368 
369 		/*
370 		 * Re-acquire contents lock.
371 		 */
372 		rw_enter(&tp->tn_contents, RW_WRITER);
373 
374 		/*
375 		 * Update tn_size.
376 		 */
377 		if (tn_size_changed)
378 			tp->tn_size = new_tn_size;
379 
380 		/*
381 		 * If the uiomove failed, fix up tn_size.
382 		 */
383 		if (error) {
384 			if (tn_size_changed) {
385 				/*
386 				 * The uiomove failed, and we
387 				 * allocated blocks,so get rid
388 				 * of them.
389 				 */
390 				(void) tmpnode_trunc(tm, tp,
391 				    (ulong_t)old_tn_size);
392 			}
393 		} else {
394 			/*
395 			 * XXX - Can this be out of the loop?
396 			 */
397 			if ((tp->tn_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) &&
398 			    (tp->tn_mode & (S_ISUID | S_ISGID)) &&
399 			    secpolicy_vnode_setid_retain(cr,
400 			    (tp->tn_mode & S_ISUID) != 0 && tp->tn_uid == 0)) {
401 				/*
402 				 * Clear Set-UID & Set-GID bits on
403 				 * successful write if not privileged
404 				 * and at least one of the execute bits
405 				 * is set.  If we always clear Set-GID,
406 				 * mandatory file and record locking is
407 				 * unuseable.
408 				 */
409 				tp->tn_mode &= ~(S_ISUID | S_ISGID);
410 			}
411 			gethrestime(&now);
412 			tp->tn_mtime = now;
413 			tp->tn_ctime = now;
414 		}
415 	} while (error == 0 && uio->uio_resid > 0 && bytes != 0);
416 
417 out:
418 	/*
419 	 * If we've already done a partial-write, terminate
420 	 * the write but return no error.
421 	 */
422 	if (oresid != uio->uio_resid)
423 		error = 0;
424 	TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
425 	    "tmp_wrtmp_end:vp %p error %d", vp, error);
426 	return (error);
427 }
428 
429 /*
430  * rdtmp does the real work of read requests for tmpfs.
431  */
432 static int
433 rdtmp(
434 	struct tmount *tm,
435 	struct tmpnode *tp,
436 	struct uio *uio,
437 	struct caller_context *ct)
438 {
439 	ulong_t pageoffset;	/* offset in tmpfs file (uio_offset) */
440 	ulong_t segmap_offset;	/* pagesize byte offset into segmap */
441 	caddr_t base;		/* base of segmap */
442 	ssize_t bytes;		/* bytes to uiomove */
443 	struct vnode *vp;
444 	int error;
445 	long oresid = uio->uio_resid;
446 
447 #if defined(lint)
448 	tm = tm;
449 #endif
450 	vp = TNTOV(tp);
451 
452 	TRACE_1(TR_FAC_TMPFS, TR_TMPFS_RWTMP_START, "tmp_rdtmp_start:vp %p",
453 	    vp);
454 
455 	ASSERT(RW_LOCK_HELD(&tp->tn_contents));
456 
457 	if (MANDLOCK(vp, tp->tn_mode)) {
458 		rw_exit(&tp->tn_contents);
459 		/*
460 		 * tmp_getattr ends up being called by chklock
461 		 */
462 		error = chklock(vp, FREAD, uio->uio_loffset, uio->uio_resid,
463 		    uio->uio_fmode, ct);
464 		rw_enter(&tp->tn_contents, RW_READER);
465 		if (error != 0) {
466 			TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
467 			    "tmp_rdtmp_end:vp %p error %d", vp, error);
468 			return (error);
469 		}
470 	}
471 	ASSERT(tp->tn_type == VREG);
472 
473 	if (uio->uio_loffset >= MAXOFF_T) {
474 		TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
475 		    "tmp_rdtmp_end:vp %p error %d", vp, EINVAL);
476 		return (0);
477 	}
478 	if (uio->uio_loffset < 0)
479 		return (EINVAL);
480 	if (uio->uio_resid == 0) {
481 		TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
482 		    "tmp_rdtmp_end:vp %p error %d", vp, 0);
483 		return (0);
484 	}
485 
486 	vp = TNTOV(tp);
487 
488 	do {
489 		long diff;
490 		long offset;
491 
492 		offset = uio->uio_offset;
493 		pageoffset = offset & PAGEOFFSET;
494 		bytes = MIN(PAGESIZE - pageoffset, uio->uio_resid);
495 
496 		diff = tp->tn_size - offset;
497 
498 		if (diff <= 0) {
499 			error = 0;
500 			goto out;
501 		}
502 		if (diff < bytes)
503 			bytes = diff;
504 
505 		/*
506 		 * We have to drop the contents lock to allow the VM system
507 		 * to reacquire it in tmp_getpage() should the uiomove cause a
508 		 * pagefault.
509 		 */
510 		rw_exit(&tp->tn_contents);
511 
512 		if (vpm_enable) {
513 			/*
514 			 * Copy data.
515 			 */
516 			error = vpm_data_copy(vp, offset, bytes, uio, 1, NULL,
517 			    0, S_READ);
518 		} else {
519 			segmap_offset = (offset & PAGEMASK) & MAXBOFFSET;
520 			base = segmap_getmapflt(segkmap, vp, offset & MAXBMASK,
521 			    bytes, 1, S_READ);
522 
523 			error = uiomove(base + segmap_offset + pageoffset,
524 			    (long)bytes, UIO_READ, uio);
525 		}
526 
527 		if (error) {
528 			if (vpm_enable) {
529 				(void) vpm_sync_pages(vp, offset, PAGESIZE, 0);
530 			} else {
531 				(void) segmap_release(segkmap, base, 0);
532 			}
533 		} else {
534 			if (vpm_enable) {
535 				error = vpm_sync_pages(vp, offset, PAGESIZE,
536 				    0);
537 			} else {
538 				error = segmap_release(segkmap, base, 0);
539 			}
540 		}
541 
542 		/*
543 		 * Re-acquire contents lock.
544 		 */
545 		rw_enter(&tp->tn_contents, RW_READER);
546 
547 	} while (error == 0 && uio->uio_resid > 0);
548 
549 out:
550 	gethrestime(&tp->tn_atime);
551 
552 	/*
553 	 * If we've already done a partial read, terminate
554 	 * the read but return no error.
555 	 */
556 	if (oresid != uio->uio_resid)
557 		error = 0;
558 
559 	TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
560 	    "tmp_rdtmp_end:vp %x error %d", vp, error);
561 	return (error);
562 }
563 
564 /* ARGSUSED2 */
565 static int
566 tmp_read(struct vnode *vp, struct uio *uiop, int ioflag, cred_t *cred,
567     struct caller_context *ct)
568 {
569 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
570 	struct tmount *tm = (struct tmount *)VTOTM(vp);
571 	int error;
572 
573 	/*
574 	 * We don't currently support reading non-regular files
575 	 */
576 	if (vp->v_type == VDIR)
577 		return (EISDIR);
578 	if (vp->v_type != VREG)
579 		return (EINVAL);
580 	/*
581 	 * tmp_rwlock should have already been called from layers above
582 	 */
583 	ASSERT(RW_READ_HELD(&tp->tn_rwlock));
584 
585 	rw_enter(&tp->tn_contents, RW_READER);
586 
587 	error = rdtmp(tm, tp, uiop, ct);
588 
589 	rw_exit(&tp->tn_contents);
590 
591 	return (error);
592 }
593 
594 static int
595 tmp_write(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cred,
596     struct caller_context *ct)
597 {
598 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
599 	struct tmount *tm = (struct tmount *)VTOTM(vp);
600 	int error;
601 
602 	/*
603 	 * We don't currently support writing to non-regular files
604 	 */
605 	if (vp->v_type != VREG)
606 		return (EINVAL);	/* XXX EISDIR? */
607 
608 	/*
609 	 * tmp_rwlock should have already been called from layers above
610 	 */
611 	ASSERT(RW_WRITE_HELD(&tp->tn_rwlock));
612 
613 	rw_enter(&tp->tn_contents, RW_WRITER);
614 
615 	if (ioflag & FAPPEND) {
616 		/*
617 		 * In append mode start at end of file.
618 		 */
619 		uiop->uio_loffset = tp->tn_size;
620 	}
621 
622 	error = wrtmp(tm, tp, uiop, cred, ct);
623 
624 	rw_exit(&tp->tn_contents);
625 
626 	return (error);
627 }
628 
629 /* ARGSUSED */
630 static int
631 tmp_ioctl(
632 	struct vnode *vp,
633 	int com,
634 	intptr_t data,
635 	int flag,
636 	struct cred *cred,
637 	int *rvalp,
638 	caller_context_t *ct)
639 {
640 	return (ENOTTY);
641 }
642 
643 /* ARGSUSED2 */
644 static int
645 tmp_getattr(
646 	struct vnode *vp,
647 	struct vattr *vap,
648 	int flags,
649 	struct cred *cred,
650 	caller_context_t *ct)
651 {
652 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
653 	struct vnode *mvp;
654 	struct vattr va;
655 	int attrs = 1;
656 
657 	/*
658 	 * A special case to handle the root tnode on a diskless nfs
659 	 * client who may have had its uid and gid inherited
660 	 * from an nfs vnode with nobody ownership.  Likely the
661 	 * root filesystem. After nfs is fully functional the uid/gid
662 	 * may be mapable so ask again.
663 	 * vfsp can't get unmounted because we hold vp.
664 	 */
665 	if (vp->v_flag & VROOT &&
666 	    (mvp = vp->v_vfsp->vfs_vnodecovered) != NULL) {
667 		mutex_enter(&tp->tn_tlock);
668 		if (tp->tn_uid == UID_NOBODY || tp->tn_gid == GID_NOBODY) {
669 			mutex_exit(&tp->tn_tlock);
670 			bzero(&va, sizeof (struct vattr));
671 			va.va_mask = AT_UID|AT_GID;
672 			attrs = VOP_GETATTR(mvp, &va, 0, cred, ct);
673 		} else {
674 			mutex_exit(&tp->tn_tlock);
675 		}
676 	}
677 	mutex_enter(&tp->tn_tlock);
678 	if (attrs == 0) {
679 		tp->tn_uid = va.va_uid;
680 		tp->tn_gid = va.va_gid;
681 	}
682 	vap->va_type = vp->v_type;
683 	vap->va_mode = tp->tn_mode & MODEMASK;
684 	vap->va_uid = tp->tn_uid;
685 	vap->va_gid = tp->tn_gid;
686 	vap->va_fsid = tp->tn_fsid;
687 	vap->va_nodeid = (ino64_t)tp->tn_nodeid;
688 	vap->va_nlink = tp->tn_nlink;
689 	vap->va_size = (u_offset_t)tp->tn_size;
690 	vap->va_atime = tp->tn_atime;
691 	vap->va_mtime = tp->tn_mtime;
692 	vap->va_ctime = tp->tn_ctime;
693 	vap->va_blksize = PAGESIZE;
694 	vap->va_rdev = tp->tn_rdev;
695 	vap->va_seq = tp->tn_seq;
696 
697 	/*
698 	 * XXX Holes are not taken into account.  We could take the time to
699 	 * run through the anon array looking for allocated slots...
700 	 */
701 	vap->va_nblocks = (fsblkcnt64_t)btodb(ptob(btopr(vap->va_size)));
702 	mutex_exit(&tp->tn_tlock);
703 	return (0);
704 }
705 
706 /*ARGSUSED4*/
707 static int
708 tmp_setattr(
709 	struct vnode *vp,
710 	struct vattr *vap,
711 	int flags,
712 	struct cred *cred,
713 	caller_context_t *ct)
714 {
715 	struct tmount *tm = (struct tmount *)VTOTM(vp);
716 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
717 	int error = 0;
718 	struct vattr *get;
719 	long mask;
720 
721 	/*
722 	 * Cannot set these attributes
723 	 */
724 	if ((vap->va_mask & AT_NOSET) || (vap->va_mask & AT_XVATTR))
725 		return (EINVAL);
726 
727 	mutex_enter(&tp->tn_tlock);
728 
729 	get = &tp->tn_attr;
730 	/*
731 	 * Change file access modes. Must be owner or have sufficient
732 	 * privileges.
733 	 */
734 	error = secpolicy_vnode_setattr(cred, vp, vap, get, flags, tmp_taccess,
735 	    tp);
736 
737 	if (error)
738 		goto out;
739 
740 	mask = vap->va_mask;
741 
742 	if (mask & AT_MODE) {
743 		get->va_mode &= S_IFMT;
744 		get->va_mode |= vap->va_mode & ~S_IFMT;
745 	}
746 
747 	if (mask & AT_UID)
748 		get->va_uid = vap->va_uid;
749 	if (mask & AT_GID)
750 		get->va_gid = vap->va_gid;
751 	if (mask & AT_ATIME)
752 		get->va_atime = vap->va_atime;
753 	if (mask & AT_MTIME)
754 		get->va_mtime = vap->va_mtime;
755 
756 	if (mask & (AT_UID | AT_GID | AT_MODE | AT_MTIME))
757 		gethrestime(&tp->tn_ctime);
758 
759 	if (mask & AT_SIZE) {
760 		ASSERT(vp->v_type != VDIR);
761 
762 		/* Don't support large files. */
763 		if (vap->va_size > MAXOFF_T) {
764 			error = EFBIG;
765 			goto out;
766 		}
767 		mutex_exit(&tp->tn_tlock);
768 
769 		rw_enter(&tp->tn_rwlock, RW_WRITER);
770 		rw_enter(&tp->tn_contents, RW_WRITER);
771 		error = tmpnode_trunc(tm, tp, (ulong_t)vap->va_size);
772 		rw_exit(&tp->tn_contents);
773 		rw_exit(&tp->tn_rwlock);
774 		goto out1;
775 	}
776 out:
777 	mutex_exit(&tp->tn_tlock);
778 out1:
779 	return (error);
780 }
781 
782 /* ARGSUSED2 */
783 static int
784 tmp_access(
785 	struct vnode *vp,
786 	int mode,
787 	int flags,
788 	struct cred *cred,
789 	caller_context_t *ct)
790 {
791 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
792 	int error;
793 
794 	mutex_enter(&tp->tn_tlock);
795 	error = tmp_taccess(tp, mode, cred);
796 	mutex_exit(&tp->tn_tlock);
797 	return (error);
798 }
799 
800 /* ARGSUSED3 */
801 static int
802 tmp_lookup(
803 	struct vnode *dvp,
804 	char *nm,
805 	struct vnode **vpp,
806 	struct pathname *pnp,
807 	int flags,
808 	struct vnode *rdir,
809 	struct cred *cred,
810 	caller_context_t *ct,
811 	int *direntflags,
812 	pathname_t *realpnp)
813 {
814 	struct tmpnode *tp = (struct tmpnode *)VTOTN(dvp);
815 	struct tmpnode *ntp = NULL;
816 	int error;
817 
818 
819 	/* allow cd into @ dir */
820 	if (flags & LOOKUP_XATTR) {
821 		struct tmpnode *xdp;
822 		struct tmount *tm;
823 
824 		/*
825 		 * don't allow attributes if not mounted XATTR support
826 		 */
827 		if (!(dvp->v_vfsp->vfs_flag & VFS_XATTR))
828 			return (EINVAL);
829 
830 		if (tp->tn_flags & ISXATTR)
831 			/* No attributes on attributes */
832 			return (EINVAL);
833 
834 		rw_enter(&tp->tn_rwlock, RW_WRITER);
835 		if (tp->tn_xattrdp == NULL) {
836 			if (!(flags & CREATE_XATTR_DIR)) {
837 				rw_exit(&tp->tn_rwlock);
838 				return (ENOENT);
839 			}
840 
841 			/*
842 			 * No attribute directory exists for this
843 			 * node - create the attr dir as a side effect
844 			 * of this lookup.
845 			 */
846 
847 			/*
848 			 * Make sure we have adequate permission...
849 			 */
850 
851 			if ((error = tmp_taccess(tp, VWRITE, cred)) != 0) {
852 				rw_exit(&tp->tn_rwlock);
853 				return (error);
854 			}
855 
856 			xdp = tmp_memalloc(sizeof (struct tmpnode),
857 			    TMP_MUSTHAVE);
858 			tm = VTOTM(dvp);
859 			tmpnode_init(tm, xdp, &tp->tn_attr, NULL);
860 			/*
861 			 * Fix-up fields unique to attribute directories.
862 			 */
863 			xdp->tn_flags = ISXATTR;
864 			xdp->tn_type = VDIR;
865 			if (tp->tn_type == VDIR) {
866 				xdp->tn_mode = tp->tn_attr.va_mode;
867 			} else {
868 				xdp->tn_mode = 0700;
869 				if (tp->tn_attr.va_mode & 0040)
870 					xdp->tn_mode |= 0750;
871 				if (tp->tn_attr.va_mode & 0004)
872 					xdp->tn_mode |= 0705;
873 			}
874 			xdp->tn_vnode->v_type = VDIR;
875 			xdp->tn_vnode->v_flag |= V_XATTRDIR;
876 			tdirinit(tp, xdp);
877 			tp->tn_xattrdp = xdp;
878 		} else {
879 			VN_HOLD(tp->tn_xattrdp->tn_vnode);
880 		}
881 		*vpp = TNTOV(tp->tn_xattrdp);
882 		rw_exit(&tp->tn_rwlock);
883 		return (0);
884 	}
885 
886 	/*
887 	 * Null component name is a synonym for directory being searched.
888 	 */
889 	if (*nm == '\0') {
890 		VN_HOLD(dvp);
891 		*vpp = dvp;
892 		return (0);
893 	}
894 	ASSERT(tp);
895 
896 	error = tdirlookup(tp, nm, &ntp, cred);
897 
898 	if (error == 0) {
899 		ASSERT(ntp);
900 		*vpp = TNTOV(ntp);
901 		/*
902 		 * If vnode is a device return special vnode instead
903 		 */
904 		if (IS_DEVVP(*vpp)) {
905 			struct vnode *newvp;
906 
907 			newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type,
908 			    cred);
909 			VN_RELE(*vpp);
910 			*vpp = newvp;
911 		}
912 	}
913 	TRACE_4(TR_FAC_TMPFS, TR_TMPFS_LOOKUP,
914 	    "tmpfs lookup:vp %p name %s vpp %p error %d",
915 	    dvp, nm, vpp, error);
916 	return (error);
917 }
918 
919 /*ARGSUSED7*/
920 static int
921 tmp_create(
922 	struct vnode *dvp,
923 	char *nm,
924 	struct vattr *vap,
925 	enum vcexcl exclusive,
926 	int mode,
927 	struct vnode **vpp,
928 	struct cred *cred,
929 	int flag,
930 	caller_context_t *ct,
931 	vsecattr_t *vsecp)
932 {
933 	struct tmpnode *parent;
934 	struct tmount *tm;
935 	struct tmpnode *self;
936 	int error;
937 	struct tmpnode *oldtp;
938 
939 again:
940 	parent = (struct tmpnode *)VTOTN(dvp);
941 	tm = (struct tmount *)VTOTM(dvp);
942 	self = NULL;
943 	error = 0;
944 	oldtp = NULL;
945 
946 	/* device files not allowed in ext. attr dirs */
947 	if ((parent->tn_flags & ISXATTR) &&
948 	    (vap->va_type == VBLK || vap->va_type == VCHR ||
949 	    vap->va_type == VFIFO || vap->va_type == VDOOR ||
950 	    vap->va_type == VSOCK || vap->va_type == VPORT))
951 			return (EINVAL);
952 
953 	if (vap->va_type == VREG && (vap->va_mode & VSVTX)) {
954 		/* Must be privileged to set sticky bit */
955 		if (secpolicy_vnode_stky_modify(cred))
956 			vap->va_mode &= ~VSVTX;
957 	} else if (vap->va_type == VNON) {
958 		return (EINVAL);
959 	}
960 
961 	/*
962 	 * Null component name is a synonym for directory being searched.
963 	 */
964 	if (*nm == '\0') {
965 		VN_HOLD(dvp);
966 		oldtp = parent;
967 	} else {
968 		error = tdirlookup(parent, nm, &oldtp, cred);
969 	}
970 
971 	if (error == 0) {	/* name found */
972 		ASSERT(oldtp);
973 
974 		rw_enter(&oldtp->tn_rwlock, RW_WRITER);
975 
976 		/*
977 		 * if create/read-only an existing
978 		 * directory, allow it
979 		 */
980 		if (exclusive == EXCL)
981 			error = EEXIST;
982 		else if ((oldtp->tn_type == VDIR) && (mode & VWRITE))
983 			error = EISDIR;
984 		else {
985 			error = tmp_taccess(oldtp, mode, cred);
986 		}
987 
988 		if (error) {
989 			rw_exit(&oldtp->tn_rwlock);
990 			tmpnode_rele(oldtp);
991 			return (error);
992 		}
993 		*vpp = TNTOV(oldtp);
994 		if ((*vpp)->v_type == VREG && (vap->va_mask & AT_SIZE) &&
995 		    vap->va_size == 0) {
996 			rw_enter(&oldtp->tn_contents, RW_WRITER);
997 			(void) tmpnode_trunc(tm, oldtp, 0);
998 			rw_exit(&oldtp->tn_contents);
999 		}
1000 		rw_exit(&oldtp->tn_rwlock);
1001 		if (IS_DEVVP(*vpp)) {
1002 			struct vnode *newvp;
1003 
1004 			newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type,
1005 			    cred);
1006 			VN_RELE(*vpp);
1007 			if (newvp == NULL) {
1008 				return (ENOSYS);
1009 			}
1010 			*vpp = newvp;
1011 		}
1012 
1013 		if (error == 0) {
1014 			vnevent_create(*vpp, ct);
1015 		}
1016 		return (0);
1017 	}
1018 
1019 	if (error != ENOENT)
1020 		return (error);
1021 
1022 	rw_enter(&parent->tn_rwlock, RW_WRITER);
1023 	error = tdirenter(tm, parent, nm, DE_CREATE,
1024 	    (struct tmpnode *)NULL, (struct tmpnode *)NULL,
1025 	    vap, &self, cred, ct);
1026 	rw_exit(&parent->tn_rwlock);
1027 
1028 	if (error) {
1029 		if (self)
1030 			tmpnode_rele(self);
1031 
1032 		if (error == EEXIST) {
1033 			/*
1034 			 * This means that the file was created sometime
1035 			 * after we checked and did not find it and when
1036 			 * we went to create it.
1037 			 * Since creat() is supposed to truncate a file
1038 			 * that already exits go back to the begining
1039 			 * of the function. This time we will find it
1040 			 * and go down the tmp_trunc() path
1041 			 */
1042 			goto again;
1043 		}
1044 		return (error);
1045 	}
1046 
1047 	*vpp = TNTOV(self);
1048 
1049 	if (!error && IS_DEVVP(*vpp)) {
1050 		struct vnode *newvp;
1051 
1052 		newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cred);
1053 		VN_RELE(*vpp);
1054 		if (newvp == NULL)
1055 			return (ENOSYS);
1056 		*vpp = newvp;
1057 	}
1058 	TRACE_3(TR_FAC_TMPFS, TR_TMPFS_CREATE,
1059 	    "tmpfs create:dvp %p nm %s vpp %p", dvp, nm, vpp);
1060 	return (0);
1061 }
1062 
1063 /* ARGSUSED3 */
1064 static int
1065 tmp_remove(
1066 	struct vnode *dvp,
1067 	char *nm,
1068 	struct cred *cred,
1069 	caller_context_t *ct,
1070 	int flags)
1071 {
1072 	struct tmpnode *parent = (struct tmpnode *)VTOTN(dvp);
1073 	int error;
1074 	struct tmpnode *tp = NULL;
1075 
1076 	error = tdirlookup(parent, nm, &tp, cred);
1077 	if (error)
1078 		return (error);
1079 
1080 	ASSERT(tp);
1081 	rw_enter(&parent->tn_rwlock, RW_WRITER);
1082 	rw_enter(&tp->tn_rwlock, RW_WRITER);
1083 
1084 	if (tp->tn_type != VDIR ||
1085 	    (error = secpolicy_fs_linkdir(cred, dvp->v_vfsp)) == 0)
1086 		error = tdirdelete(parent, tp, nm, DR_REMOVE, cred);
1087 
1088 	rw_exit(&tp->tn_rwlock);
1089 	rw_exit(&parent->tn_rwlock);
1090 	vnevent_remove(TNTOV(tp), dvp, nm, ct);
1091 	tmpnode_rele(tp);
1092 
1093 	TRACE_3(TR_FAC_TMPFS, TR_TMPFS_REMOVE,
1094 	    "tmpfs remove:dvp %p nm %s error %d", dvp, nm, error);
1095 	return (error);
1096 }
1097 
1098 /* ARGSUSED4 */
1099 static int
1100 tmp_link(
1101 	struct vnode *dvp,
1102 	struct vnode *srcvp,
1103 	char *tnm,
1104 	struct cred *cred,
1105 	caller_context_t *ct,
1106 	int flags)
1107 {
1108 	struct tmpnode *parent;
1109 	struct tmpnode *from;
1110 	struct tmount *tm = (struct tmount *)VTOTM(dvp);
1111 	int error;
1112 	struct tmpnode *found = NULL;
1113 	struct vnode *realvp;
1114 
1115 	if (VOP_REALVP(srcvp, &realvp, ct) == 0)
1116 		srcvp = realvp;
1117 
1118 	parent = (struct tmpnode *)VTOTN(dvp);
1119 	from = (struct tmpnode *)VTOTN(srcvp);
1120 
1121 	if ((srcvp->v_type == VDIR &&
1122 	    secpolicy_fs_linkdir(cred, dvp->v_vfsp)) ||
1123 	    (from->tn_uid != crgetuid(cred) && secpolicy_basic_link(cred)))
1124 		return (EPERM);
1125 
1126 	/*
1127 	 * Make sure link for extended attributes is valid
1128 	 * We only support hard linking of xattr's in xattrdir to an xattrdir
1129 	 */
1130 	if ((from->tn_flags & ISXATTR) != (parent->tn_flags & ISXATTR))
1131 		return (EINVAL);
1132 
1133 	error = tdirlookup(parent, tnm, &found, cred);
1134 	if (error == 0) {
1135 		ASSERT(found);
1136 		tmpnode_rele(found);
1137 		return (EEXIST);
1138 	}
1139 
1140 	if (error != ENOENT)
1141 		return (error);
1142 
1143 	rw_enter(&parent->tn_rwlock, RW_WRITER);
1144 	error = tdirenter(tm, parent, tnm, DE_LINK, (struct tmpnode *)NULL,
1145 	    from, NULL, (struct tmpnode **)NULL, cred, ct);
1146 	rw_exit(&parent->tn_rwlock);
1147 	if (error == 0) {
1148 		vnevent_link(srcvp, ct);
1149 	}
1150 	return (error);
1151 }
1152 
1153 /* ARGSUSED5 */
1154 static int
1155 tmp_rename(
1156 	struct vnode *odvp,	/* source parent vnode */
1157 	char *onm,		/* source name */
1158 	struct vnode *ndvp,	/* destination parent vnode */
1159 	char *nnm,		/* destination name */
1160 	struct cred *cred,
1161 	caller_context_t *ct,
1162 	int flags)
1163 {
1164 	struct tmpnode *fromparent;
1165 	struct tmpnode *toparent;
1166 	struct tmpnode *fromtp = NULL;	/* source tmpnode */
1167 	struct tmount *tm = (struct tmount *)VTOTM(odvp);
1168 	int error;
1169 	int samedir = 0;	/* set if odvp == ndvp */
1170 	struct vnode *realvp;
1171 
1172 	if (VOP_REALVP(ndvp, &realvp, ct) == 0)
1173 		ndvp = realvp;
1174 
1175 	fromparent = (struct tmpnode *)VTOTN(odvp);
1176 	toparent = (struct tmpnode *)VTOTN(ndvp);
1177 
1178 	if ((fromparent->tn_flags & ISXATTR) != (toparent->tn_flags & ISXATTR))
1179 		return (EINVAL);
1180 
1181 	mutex_enter(&tm->tm_renamelck);
1182 
1183 	/*
1184 	 * Look up tmpnode of file we're supposed to rename.
1185 	 */
1186 	error = tdirlookup(fromparent, onm, &fromtp, cred);
1187 	if (error) {
1188 		mutex_exit(&tm->tm_renamelck);
1189 		return (error);
1190 	}
1191 
1192 	/*
1193 	 * Make sure we can delete the old (source) entry.  This
1194 	 * requires write permission on the containing directory.  If
1195 	 * that directory is "sticky" it requires further checks.
1196 	 */
1197 	if (((error = tmp_taccess(fromparent, VWRITE, cred)) != 0) ||
1198 	    (error = tmp_sticky_remove_access(fromparent, fromtp, cred)) != 0)
1199 		goto done;
1200 
1201 	/*
1202 	 * Check for renaming to or from '.' or '..' or that
1203 	 * fromtp == fromparent
1204 	 */
1205 	if ((onm[0] == '.' &&
1206 	    (onm[1] == '\0' || (onm[1] == '.' && onm[2] == '\0'))) ||
1207 	    (nnm[0] == '.' &&
1208 	    (nnm[1] == '\0' || (nnm[1] == '.' && nnm[2] == '\0'))) ||
1209 	    (fromparent == fromtp)) {
1210 		error = EINVAL;
1211 		goto done;
1212 	}
1213 
1214 	samedir = (fromparent == toparent);
1215 	/*
1216 	 * Make sure we can search and rename into the new
1217 	 * (destination) directory.
1218 	 */
1219 	if (!samedir) {
1220 		error = tmp_taccess(toparent, VEXEC|VWRITE, cred);
1221 		if (error)
1222 			goto done;
1223 	}
1224 
1225 	/*
1226 	 * Link source to new target
1227 	 */
1228 	rw_enter(&toparent->tn_rwlock, RW_WRITER);
1229 	error = tdirenter(tm, toparent, nnm, DE_RENAME,
1230 	    fromparent, fromtp, (struct vattr *)NULL,
1231 	    (struct tmpnode **)NULL, cred, ct);
1232 	rw_exit(&toparent->tn_rwlock);
1233 
1234 	if (error) {
1235 		/*
1236 		 * ESAME isn't really an error; it indicates that the
1237 		 * operation should not be done because the source and target
1238 		 * are the same file, but that no error should be reported.
1239 		 */
1240 		if (error == ESAME)
1241 			error = 0;
1242 		goto done;
1243 	}
1244 	vnevent_rename_src(TNTOV(fromtp), odvp, onm, ct);
1245 
1246 	/*
1247 	 * Notify the target directory if not same as
1248 	 * source directory.
1249 	 */
1250 	if (ndvp != odvp) {
1251 		vnevent_rename_dest_dir(ndvp, ct);
1252 	}
1253 
1254 	/*
1255 	 * Unlink from source.
1256 	 */
1257 	rw_enter(&fromparent->tn_rwlock, RW_WRITER);
1258 	rw_enter(&fromtp->tn_rwlock, RW_WRITER);
1259 
1260 	error = tdirdelete(fromparent, fromtp, onm, DR_RENAME, cred);
1261 
1262 	/*
1263 	 * The following handles the case where our source tmpnode was
1264 	 * removed before we got to it.
1265 	 *
1266 	 * XXX We should also cleanup properly in the case where tdirdelete
1267 	 * fails for some other reason.  Currently this case shouldn't happen.
1268 	 * (see 1184991).
1269 	 */
1270 	if (error == ENOENT)
1271 		error = 0;
1272 
1273 	rw_exit(&fromtp->tn_rwlock);
1274 	rw_exit(&fromparent->tn_rwlock);
1275 done:
1276 	tmpnode_rele(fromtp);
1277 	mutex_exit(&tm->tm_renamelck);
1278 
1279 	TRACE_5(TR_FAC_TMPFS, TR_TMPFS_RENAME,
1280 	    "tmpfs rename:ovp %p onm %s nvp %p nnm %s error %d", odvp, onm,
1281 	    ndvp, nnm, error);
1282 	return (error);
1283 }
1284 
1285 /* ARGSUSED5 */
1286 static int
1287 tmp_mkdir(
1288 	struct vnode *dvp,
1289 	char *nm,
1290 	struct vattr *va,
1291 	struct vnode **vpp,
1292 	struct cred *cred,
1293 	caller_context_t *ct,
1294 	int flags,
1295 	vsecattr_t *vsecp)
1296 {
1297 	struct tmpnode *parent = (struct tmpnode *)VTOTN(dvp);
1298 	struct tmpnode *self = NULL;
1299 	struct tmount *tm = (struct tmount *)VTOTM(dvp);
1300 	int error;
1301 
1302 	/* no new dirs allowed in xattr dirs */
1303 	if (parent->tn_flags & ISXATTR)
1304 		return (EINVAL);
1305 
1306 	/*
1307 	 * Might be dangling directory.  Catch it here,
1308 	 * because a ENOENT return from tdirlookup() is
1309 	 * an "o.k. return".
1310 	 */
1311 	if (parent->tn_nlink == 0)
1312 		return (ENOENT);
1313 
1314 	error = tdirlookup(parent, nm, &self, cred);
1315 	if (error == 0) {
1316 		ASSERT(self);
1317 		tmpnode_rele(self);
1318 		return (EEXIST);
1319 	}
1320 	if (error != ENOENT)
1321 		return (error);
1322 
1323 	rw_enter(&parent->tn_rwlock, RW_WRITER);
1324 	error = tdirenter(tm, parent, nm, DE_MKDIR, (struct tmpnode *)NULL,
1325 	    (struct tmpnode *)NULL, va, &self, cred, ct);
1326 	if (error) {
1327 		rw_exit(&parent->tn_rwlock);
1328 		if (self)
1329 			tmpnode_rele(self);
1330 		return (error);
1331 	}
1332 	rw_exit(&parent->tn_rwlock);
1333 	*vpp = TNTOV(self);
1334 	return (0);
1335 }
1336 
1337 /* ARGSUSED4 */
1338 static int
1339 tmp_rmdir(
1340 	struct vnode *dvp,
1341 	char *nm,
1342 	struct vnode *cdir,
1343 	struct cred *cred,
1344 	caller_context_t *ct,
1345 	int flags)
1346 {
1347 	struct tmpnode *parent = (struct tmpnode *)VTOTN(dvp);
1348 	struct tmpnode *self = NULL;
1349 	struct vnode *vp;
1350 	int error = 0;
1351 
1352 	/*
1353 	 * Return error when removing . and ..
1354 	 */
1355 	if (strcmp(nm, ".") == 0)
1356 		return (EINVAL);
1357 	if (strcmp(nm, "..") == 0)
1358 		return (EEXIST); /* Should be ENOTEMPTY */
1359 	error = tdirlookup(parent, nm, &self, cred);
1360 	if (error)
1361 		return (error);
1362 
1363 	rw_enter(&parent->tn_rwlock, RW_WRITER);
1364 	rw_enter(&self->tn_rwlock, RW_WRITER);
1365 
1366 	vp = TNTOV(self);
1367 	if (vp == dvp || vp == cdir) {
1368 		error = EINVAL;
1369 		goto done1;
1370 	}
1371 	if (self->tn_type != VDIR) {
1372 		error = ENOTDIR;
1373 		goto done1;
1374 	}
1375 
1376 	mutex_enter(&self->tn_tlock);
1377 	if (self->tn_nlink > 2) {
1378 		mutex_exit(&self->tn_tlock);
1379 		error = EEXIST;
1380 		goto done1;
1381 	}
1382 	mutex_exit(&self->tn_tlock);
1383 
1384 	if (vn_vfswlock(vp)) {
1385 		error = EBUSY;
1386 		goto done1;
1387 	}
1388 	if (vn_mountedvfs(vp) != NULL) {
1389 		error = EBUSY;
1390 		goto done;
1391 	}
1392 
1393 	/*
1394 	 * Check for an empty directory
1395 	 * i.e. only includes entries for "." and ".."
1396 	 */
1397 	if (self->tn_dirents > 2) {
1398 		error = EEXIST;		/* SIGH should be ENOTEMPTY */
1399 		/*
1400 		 * Update atime because checking tn_dirents is logically
1401 		 * equivalent to reading the directory
1402 		 */
1403 		gethrestime(&self->tn_atime);
1404 		goto done;
1405 	}
1406 
1407 	error = tdirdelete(parent, self, nm, DR_RMDIR, cred);
1408 done:
1409 	vn_vfsunlock(vp);
1410 done1:
1411 	rw_exit(&self->tn_rwlock);
1412 	rw_exit(&parent->tn_rwlock);
1413 	vnevent_rmdir(TNTOV(self), dvp, nm, ct);
1414 	tmpnode_rele(self);
1415 
1416 	return (error);
1417 }
1418 
1419 /* ARGSUSED2 */
1420 static int
1421 tmp_readdir(
1422 	struct vnode *vp,
1423 	struct uio *uiop,
1424 	struct cred *cred,
1425 	int *eofp,
1426 	caller_context_t *ct,
1427 	int flags)
1428 {
1429 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
1430 	struct tdirent *tdp;
1431 	int error = 0;
1432 	size_t namelen;
1433 	struct dirent64 *dp;
1434 	ulong_t offset;
1435 	ulong_t total_bytes_wanted;
1436 	long outcount = 0;
1437 	long bufsize;
1438 	int reclen;
1439 	caddr_t outbuf;
1440 
1441 	if (uiop->uio_loffset >= MAXOFF_T) {
1442 		if (eofp)
1443 			*eofp = 1;
1444 		return (0);
1445 	}
1446 	/*
1447 	 * assuming system call has already called tmp_rwlock
1448 	 */
1449 	ASSERT(RW_READ_HELD(&tp->tn_rwlock));
1450 
1451 	if (uiop->uio_iovcnt != 1)
1452 		return (EINVAL);
1453 
1454 	if (vp->v_type != VDIR)
1455 		return (ENOTDIR);
1456 
1457 	/*
1458 	 * There's a window here where someone could have removed
1459 	 * all the entries in the directory after we put a hold on the
1460 	 * vnode but before we grabbed the rwlock.  Just return.
1461 	 */
1462 	if (tp->tn_dir == NULL) {
1463 		if (tp->tn_nlink) {
1464 			panic("empty directory 0x%p", (void *)tp);
1465 			/*NOTREACHED*/
1466 		}
1467 		return (0);
1468 	}
1469 
1470 	/*
1471 	 * Get space for multiple directory entries
1472 	 */
1473 	total_bytes_wanted = uiop->uio_iov->iov_len;
1474 	bufsize = total_bytes_wanted + sizeof (struct dirent64);
1475 	outbuf = kmem_alloc(bufsize, KM_SLEEP);
1476 
1477 	dp = (struct dirent64 *)outbuf;
1478 
1479 
1480 	offset = 0;
1481 	tdp = tp->tn_dir;
1482 	while (tdp) {
1483 		namelen = strlen(tdp->td_name);	/* no +1 needed */
1484 		offset = tdp->td_offset;
1485 		if (offset >= uiop->uio_offset) {
1486 			reclen = (int)DIRENT64_RECLEN(namelen);
1487 			if (outcount + reclen > total_bytes_wanted) {
1488 				if (!outcount)
1489 					/*
1490 					 * Buffer too small for any entries.
1491 					 */
1492 					error = EINVAL;
1493 				break;
1494 			}
1495 			ASSERT(tdp->td_tmpnode != NULL);
1496 
1497 			/* use strncpy(9f) to zero out uninitialized bytes */
1498 
1499 			(void) strncpy(dp->d_name, tdp->td_name,
1500 			    DIRENT64_NAMELEN(reclen));
1501 			dp->d_reclen = (ushort_t)reclen;
1502 			dp->d_ino = (ino64_t)tdp->td_tmpnode->tn_nodeid;
1503 			dp->d_off = (offset_t)tdp->td_offset + 1;
1504 			dp = (struct dirent64 *)
1505 			    ((uintptr_t)dp + dp->d_reclen);
1506 			outcount += reclen;
1507 			ASSERT(outcount <= bufsize);
1508 		}
1509 		tdp = tdp->td_next;
1510 	}
1511 
1512 	if (!error)
1513 		error = uiomove(outbuf, outcount, UIO_READ, uiop);
1514 
1515 	if (!error) {
1516 		/* If we reached the end of the list our offset */
1517 		/* should now be just past the end. */
1518 		if (!tdp) {
1519 			offset += 1;
1520 			if (eofp)
1521 				*eofp = 1;
1522 		} else if (eofp)
1523 			*eofp = 0;
1524 		uiop->uio_offset = offset;
1525 	}
1526 	gethrestime(&tp->tn_atime);
1527 	kmem_free(outbuf, bufsize);
1528 	return (error);
1529 }
1530 
1531 /* ARGSUSED5 */
1532 static int
1533 tmp_symlink(
1534 	struct vnode *dvp,
1535 	char *lnm,
1536 	struct vattr *tva,
1537 	char *tnm,
1538 	struct cred *cred,
1539 	caller_context_t *ct,
1540 	int flags)
1541 {
1542 	struct tmpnode *parent = (struct tmpnode *)VTOTN(dvp);
1543 	struct tmpnode *self = (struct tmpnode *)NULL;
1544 	struct tmount *tm = (struct tmount *)VTOTM(dvp);
1545 	char *cp = NULL;
1546 	int error;
1547 	size_t len;
1548 
1549 	/* no symlinks allowed to files in xattr dirs */
1550 	if (parent->tn_flags & ISXATTR)
1551 		return (EINVAL);
1552 
1553 	error = tdirlookup(parent, lnm, &self, cred);
1554 	if (error == 0) {
1555 		/*
1556 		 * The entry already exists
1557 		 */
1558 		tmpnode_rele(self);
1559 		return (EEXIST);	/* was 0 */
1560 	}
1561 
1562 	if (error != ENOENT) {
1563 		if (self != NULL)
1564 			tmpnode_rele(self);
1565 		return (error);
1566 	}
1567 
1568 	rw_enter(&parent->tn_rwlock, RW_WRITER);
1569 	error = tdirenter(tm, parent, lnm, DE_CREATE, (struct tmpnode *)NULL,
1570 	    (struct tmpnode *)NULL, tva, &self, cred, ct);
1571 	rw_exit(&parent->tn_rwlock);
1572 
1573 	if (error) {
1574 		if (self)
1575 			tmpnode_rele(self);
1576 		return (error);
1577 	}
1578 	len = strlen(tnm) + 1;
1579 	cp = tmp_memalloc(len, 0);
1580 	if (cp == NULL) {
1581 		tmpnode_rele(self);
1582 		return (ENOSPC);
1583 	}
1584 	(void) strcpy(cp, tnm);
1585 
1586 	self->tn_symlink = cp;
1587 	self->tn_size = len - 1;
1588 	tmpnode_rele(self);
1589 	return (error);
1590 }
1591 
1592 /* ARGSUSED2 */
1593 static int
1594 tmp_readlink(
1595 	struct vnode *vp,
1596 	struct uio *uiop,
1597 	struct cred *cred,
1598 	caller_context_t *ct)
1599 {
1600 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
1601 	int error = 0;
1602 
1603 	if (vp->v_type != VLNK)
1604 		return (EINVAL);
1605 
1606 	rw_enter(&tp->tn_rwlock, RW_READER);
1607 	rw_enter(&tp->tn_contents, RW_READER);
1608 	error = uiomove(tp->tn_symlink, tp->tn_size, UIO_READ, uiop);
1609 	gethrestime(&tp->tn_atime);
1610 	rw_exit(&tp->tn_contents);
1611 	rw_exit(&tp->tn_rwlock);
1612 	return (error);
1613 }
1614 
1615 /* ARGSUSED */
1616 static int
1617 tmp_fsync(
1618 	struct vnode *vp,
1619 	int syncflag,
1620 	struct cred *cred,
1621 	caller_context_t *ct)
1622 {
1623 	return (0);
1624 }
1625 
1626 /* ARGSUSED */
1627 static void
1628 tmp_inactive(struct vnode *vp, struct cred *cred, caller_context_t *ct)
1629 {
1630 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
1631 	struct tmount *tm = (struct tmount *)VFSTOTM(vp->v_vfsp);
1632 
1633 	rw_enter(&tp->tn_rwlock, RW_WRITER);
1634 top:
1635 	mutex_enter(&tp->tn_tlock);
1636 	mutex_enter(&vp->v_lock);
1637 	ASSERT(vp->v_count >= 1);
1638 
1639 	/*
1640 	 * If we don't have the last hold or the link count is non-zero,
1641 	 * there's little to do -- just drop our hold.
1642 	 */
1643 	if (vp->v_count > 1 || tp->tn_nlink != 0) {
1644 		vp->v_count--;
1645 		mutex_exit(&vp->v_lock);
1646 		mutex_exit(&tp->tn_tlock);
1647 		rw_exit(&tp->tn_rwlock);
1648 		return;
1649 	}
1650 
1651 	/*
1652 	 * We have the last hold *and* the link count is zero, so this
1653 	 * tmpnode is dead from the filesystem's viewpoint.  However,
1654 	 * if the tmpnode has any pages associated with it (i.e. if it's
1655 	 * a normal file with non-zero size), the tmpnode can still be
1656 	 * discovered by pageout or fsflush via the page vnode pointers.
1657 	 * In this case we must drop all our locks, truncate the tmpnode,
1658 	 * and try the whole dance again.
1659 	 */
1660 	if (tp->tn_size != 0) {
1661 		if (tp->tn_type == VREG) {
1662 			mutex_exit(&vp->v_lock);
1663 			mutex_exit(&tp->tn_tlock);
1664 			rw_enter(&tp->tn_contents, RW_WRITER);
1665 			(void) tmpnode_trunc(tm, tp, 0);
1666 			rw_exit(&tp->tn_contents);
1667 			ASSERT(tp->tn_size == 0);
1668 			ASSERT(tp->tn_nblocks == 0);
1669 			goto top;
1670 		}
1671 		if (tp->tn_type == VLNK)
1672 			tmp_memfree(tp->tn_symlink, tp->tn_size + 1);
1673 	}
1674 
1675 	/*
1676 	 * Remove normal file/dir's xattr dir and xattrs.
1677 	 */
1678 	if (tp->tn_xattrdp) {
1679 		struct tmpnode *xtp = tp->tn_xattrdp;
1680 
1681 		ASSERT(xtp->tn_flags & ISXATTR);
1682 		tmpnode_hold(xtp);
1683 		rw_enter(&xtp->tn_rwlock, RW_WRITER);
1684 		tdirtrunc(xtp);
1685 		DECR_COUNT(&xtp->tn_nlink, &xtp->tn_tlock);
1686 		tp->tn_xattrdp = NULL;
1687 		rw_exit(&xtp->tn_rwlock);
1688 		tmpnode_rele(xtp);
1689 	}
1690 
1691 	mutex_exit(&vp->v_lock);
1692 	mutex_exit(&tp->tn_tlock);
1693 	/* Here's our chance to send invalid event while we're between locks */
1694 	vn_invalid(TNTOV(tp));
1695 	mutex_enter(&tm->tm_contents);
1696 	if (tp->tn_forw == NULL)
1697 		tm->tm_rootnode->tn_back = tp->tn_back;
1698 	else
1699 		tp->tn_forw->tn_back = tp->tn_back;
1700 	tp->tn_back->tn_forw = tp->tn_forw;
1701 	mutex_exit(&tm->tm_contents);
1702 	rw_exit(&tp->tn_rwlock);
1703 	rw_destroy(&tp->tn_rwlock);
1704 	mutex_destroy(&tp->tn_tlock);
1705 	vn_free(TNTOV(tp));
1706 	tmp_memfree(tp, sizeof (struct tmpnode));
1707 }
1708 
1709 /* ARGSUSED2 */
1710 static int
1711 tmp_fid(struct vnode *vp, struct fid *fidp, caller_context_t *ct)
1712 {
1713 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
1714 	struct tfid *tfid;
1715 
1716 	if (fidp->fid_len < (sizeof (struct tfid) - sizeof (ushort_t))) {
1717 		fidp->fid_len = sizeof (struct tfid) - sizeof (ushort_t);
1718 		return (ENOSPC);
1719 	}
1720 
1721 	tfid = (struct tfid *)fidp;
1722 	bzero(tfid, sizeof (struct tfid));
1723 	tfid->tfid_len = (int)sizeof (struct tfid) - sizeof (ushort_t);
1724 
1725 	tfid->tfid_ino = tp->tn_nodeid;
1726 	tfid->tfid_gen = tp->tn_gen;
1727 
1728 	return (0);
1729 }
1730 
1731 
1732 /*
1733  * Return all the pages from [off..off+len] in given file
1734  */
1735 /* ARGSUSED */
1736 static int
1737 tmp_getpage(
1738 	struct vnode *vp,
1739 	offset_t off,
1740 	size_t len,
1741 	uint_t *protp,
1742 	page_t *pl[],
1743 	size_t plsz,
1744 	struct seg *seg,
1745 	caddr_t addr,
1746 	enum seg_rw rw,
1747 	struct cred *cr,
1748 	caller_context_t *ct)
1749 {
1750 	int err = 0;
1751 	struct tmpnode *tp = VTOTN(vp);
1752 	anoff_t toff = (anoff_t)off;
1753 	size_t tlen = len;
1754 	u_offset_t tmpoff;
1755 	timestruc_t now;
1756 
1757 	rw_enter(&tp->tn_contents, RW_READER);
1758 
1759 	if (off + len  > tp->tn_size + PAGEOFFSET) {
1760 		err = EFAULT;
1761 		goto out;
1762 	}
1763 	/*
1764 	 * Look for holes (no anon slot) in faulting range. If there are
1765 	 * holes we have to switch to a write lock and fill them in. Swap
1766 	 * space for holes was already reserved when the file was grown.
1767 	 */
1768 	tmpoff = toff;
1769 	if (non_anon(tp->tn_anon, btop(off), &tmpoff, &tlen)) {
1770 		if (!rw_tryupgrade(&tp->tn_contents)) {
1771 			rw_exit(&tp->tn_contents);
1772 			rw_enter(&tp->tn_contents, RW_WRITER);
1773 			/* Size may have changed when lock was dropped */
1774 			if (off + len  > tp->tn_size + PAGEOFFSET) {
1775 				err = EFAULT;
1776 				goto out;
1777 			}
1778 		}
1779 		for (toff = (anoff_t)off; toff < (anoff_t)off + len;
1780 		    toff += PAGESIZE) {
1781 			if (anon_get_ptr(tp->tn_anon, btop(toff)) == NULL) {
1782 				/* XXX - may allocate mem w. write lock held */
1783 				(void) anon_set_ptr(tp->tn_anon, btop(toff),
1784 				    anon_alloc(vp, toff), ANON_SLEEP);
1785 				tp->tn_nblocks++;
1786 			}
1787 		}
1788 		rw_downgrade(&tp->tn_contents);
1789 	}
1790 
1791 
1792 	if (len <= PAGESIZE)
1793 		err = tmp_getapage(vp, (u_offset_t)off, len, protp, pl, plsz,
1794 		    seg, addr, rw, cr);
1795 	else
1796 		err = pvn_getpages(tmp_getapage, vp, (u_offset_t)off, len,
1797 		    protp, pl, plsz, seg, addr, rw, cr);
1798 
1799 	gethrestime(&now);
1800 	tp->tn_atime = now;
1801 	if (rw == S_WRITE)
1802 		tp->tn_mtime = now;
1803 
1804 out:
1805 	rw_exit(&tp->tn_contents);
1806 	return (err);
1807 }
1808 
1809 /*
1810  * Called from pvn_getpages or swap_getpage to get a particular page.
1811  */
1812 /*ARGSUSED*/
1813 static int
1814 tmp_getapage(
1815 	struct vnode *vp,
1816 	u_offset_t off,
1817 	size_t len,
1818 	uint_t *protp,
1819 	page_t *pl[],
1820 	size_t plsz,
1821 	struct seg *seg,
1822 	caddr_t addr,
1823 	enum seg_rw rw,
1824 	struct cred *cr)
1825 {
1826 	struct page *pp;
1827 	int flags;
1828 	int err = 0;
1829 	struct vnode *pvp;
1830 	u_offset_t poff;
1831 
1832 	if (protp != NULL)
1833 		*protp = PROT_ALL;
1834 again:
1835 	if (pp = page_lookup(vp, off, rw == S_CREATE ? SE_EXCL : SE_SHARED)) {
1836 		if (pl) {
1837 			pl[0] = pp;
1838 			pl[1] = NULL;
1839 		} else {
1840 			page_unlock(pp);
1841 		}
1842 	} else {
1843 		pp = page_create_va(vp, off, PAGESIZE,
1844 		    PG_WAIT | PG_EXCL, seg, addr);
1845 		/*
1846 		 * Someone raced in and created the page after we did the
1847 		 * lookup but before we did the create, so go back and
1848 		 * try to look it up again.
1849 		 */
1850 		if (pp == NULL)
1851 			goto again;
1852 		/*
1853 		 * Fill page from backing store, if any. If none, then
1854 		 * either this is a newly filled hole or page must have
1855 		 * been unmodified and freed so just zero it out.
1856 		 */
1857 		err = swap_getphysname(vp, off, &pvp, &poff);
1858 		if (err) {
1859 			panic("tmp_getapage: no anon slot vp %p "
1860 			    "off %llx pp %p\n", (void *)vp, off, (void *)pp);
1861 		}
1862 		if (pvp) {
1863 			flags = (pl == NULL ? B_ASYNC|B_READ : B_READ);
1864 			err = VOP_PAGEIO(pvp, pp, (u_offset_t)poff, PAGESIZE,
1865 			    flags, cr, NULL);
1866 			if (flags & B_ASYNC)
1867 				pp = NULL;
1868 		} else if (rw != S_CREATE) {
1869 			pagezero(pp, 0, PAGESIZE);
1870 		}
1871 		if (err && pp)
1872 			pvn_read_done(pp, B_ERROR);
1873 		if (err == 0) {
1874 			if (pl)
1875 				pvn_plist_init(pp, pl, plsz, off, PAGESIZE, rw);
1876 			else
1877 				pvn_io_done(pp);
1878 		}
1879 	}
1880 	return (err);
1881 }
1882 
1883 
1884 /*
1885  * Flags are composed of {B_INVAL, B_DIRTY B_FREE, B_DONTNEED}.
1886  * If len == 0, do from off to EOF.
1887  */
1888 static int tmp_nopage = 0;	/* Don't do tmp_putpage's if set */
1889 
1890 /* ARGSUSED */
1891 int
1892 tmp_putpage(
1893 	register struct vnode *vp,
1894 	offset_t off,
1895 	size_t len,
1896 	int flags,
1897 	struct cred *cr,
1898 	caller_context_t *ct)
1899 {
1900 	register page_t *pp;
1901 	u_offset_t io_off;
1902 	size_t io_len = 0;
1903 	int err = 0;
1904 	struct tmpnode *tp = VTOTN(vp);
1905 	int dolock;
1906 
1907 	if (tmp_nopage)
1908 		return (0);
1909 
1910 	ASSERT(vp->v_count != 0);
1911 
1912 	if (vp->v_flag & VNOMAP)
1913 		return (ENOSYS);
1914 
1915 	/*
1916 	 * This being tmpfs, we don't ever do i/o unless we really
1917 	 * have to (when we're low on memory and pageout calls us
1918 	 * with B_ASYNC | B_FREE or the user explicitly asks for it with
1919 	 * B_DONTNEED).
1920 	 * XXX to approximately track the mod time like ufs we should
1921 	 * update the times here. The problem is, once someone does a
1922 	 * store we never clear the mod bit and do i/o, thus fsflush
1923 	 * will keep calling us every 30 seconds to do the i/o and we'll
1924 	 * continually update the mod time. At least we update the mod
1925 	 * time on the first store because this results in a call to getpage.
1926 	 */
1927 	if (flags != (B_ASYNC | B_FREE) && (flags & B_INVAL) == 0 &&
1928 	    (flags & B_DONTNEED) == 0)
1929 		return (0);
1930 	/*
1931 	 * If this thread owns the lock, i.e., this thread grabbed it
1932 	 * as writer somewhere above, then we don't need to grab the
1933 	 * lock as reader in this routine.
1934 	 */
1935 	dolock = (rw_owner(&tp->tn_contents) != curthread);
1936 
1937 	/*
1938 	 * If this is pageout don't block on the lock as you could deadlock
1939 	 * when freemem == 0 (another thread has the read lock and is blocked
1940 	 * creating a page, and a third thread is waiting to get the writers
1941 	 * lock - waiting writers priority blocks us from getting the read
1942 	 * lock). Of course, if the only freeable pages are on this tmpnode
1943 	 * we're hosed anyways. A better solution might be a new lock type.
1944 	 * Note: ufs has the same problem.
1945 	 */
1946 	if (curproc == proc_pageout) {
1947 		if (!rw_tryenter(&tp->tn_contents, RW_READER))
1948 			return (ENOMEM);
1949 	} else if (dolock)
1950 		rw_enter(&tp->tn_contents, RW_READER);
1951 
1952 	if (!vn_has_cached_data(vp))
1953 		goto out;
1954 
1955 	if (len == 0) {
1956 		if (curproc == proc_pageout) {
1957 			panic("tmp: pageout can't block");
1958 			/*NOTREACHED*/
1959 		}
1960 
1961 		/* Search the entire vp list for pages >= off. */
1962 		err = pvn_vplist_dirty(vp, (u_offset_t)off, tmp_putapage,
1963 		    flags, cr);
1964 	} else {
1965 		u_offset_t eoff;
1966 
1967 		/*
1968 		 * Loop over all offsets in the range [off...off + len]
1969 		 * looking for pages to deal with.
1970 		 */
1971 		eoff = MIN(off + len, tp->tn_size);
1972 		for (io_off = off; io_off < eoff; io_off += io_len) {
1973 			/*
1974 			 * If we are not invalidating, synchronously
1975 			 * freeing or writing pages use the routine
1976 			 * page_lookup_nowait() to prevent reclaiming
1977 			 * them from the free list.
1978 			 */
1979 			if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
1980 				pp = page_lookup(vp, io_off,
1981 				    (flags & (B_INVAL | B_FREE)) ?
1982 				    SE_EXCL : SE_SHARED);
1983 			} else {
1984 				pp = page_lookup_nowait(vp, io_off,
1985 				    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
1986 			}
1987 
1988 			if (pp == NULL || pvn_getdirty(pp, flags) == 0)
1989 				io_len = PAGESIZE;
1990 			else {
1991 				err = tmp_putapage(vp, pp, &io_off, &io_len,
1992 				    flags, cr);
1993 				if (err != 0)
1994 					break;
1995 			}
1996 		}
1997 	}
1998 	/* If invalidating, verify all pages on vnode list are gone. */
1999 	if (err == 0 && off == 0 && len == 0 &&
2000 	    (flags & B_INVAL) && vn_has_cached_data(vp)) {
2001 		panic("tmp_putpage: B_INVAL, pages not gone");
2002 		/*NOTREACHED*/
2003 	}
2004 out:
2005 	if ((curproc == proc_pageout) || dolock)
2006 		rw_exit(&tp->tn_contents);
2007 	/*
2008 	 * Only reason putapage is going to give us SE_NOSWAP as error
2009 	 * is when we ask a page to be written to physical backing store
2010 	 * and there is none. Ignore this because we might be dealing
2011 	 * with a swap page which does not have any backing store
2012 	 * on disk. In any other case we won't get this error over here.
2013 	 */
2014 	if (err == SE_NOSWAP)
2015 		err = 0;
2016 	return (err);
2017 }
2018 
2019 long tmp_putpagecnt, tmp_pagespushed;
2020 
2021 /*
2022  * Write out a single page.
2023  * For tmpfs this means choose a physical swap slot and write the page
2024  * out using VOP_PAGEIO. For performance, we attempt to kluster; i.e.,
2025  * we try to find a bunch of other dirty pages adjacent in the file
2026  * and a bunch of contiguous swap slots, and then write all the pages
2027  * out in a single i/o.
2028  */
2029 /*ARGSUSED*/
2030 static int
2031 tmp_putapage(
2032 	struct vnode *vp,
2033 	page_t *pp,
2034 	u_offset_t *offp,
2035 	size_t *lenp,
2036 	int flags,
2037 	struct cred *cr)
2038 {
2039 	int err;
2040 	ulong_t klstart, kllen;
2041 	page_t *pplist, *npplist;
2042 	extern int klustsize;
2043 	long tmp_klustsize;
2044 	struct tmpnode *tp;
2045 	size_t pp_off, pp_len;
2046 	u_offset_t io_off;
2047 	size_t io_len;
2048 	struct vnode *pvp;
2049 	u_offset_t pstart;
2050 	u_offset_t offset;
2051 	u_offset_t tmpoff;
2052 
2053 	ASSERT(PAGE_LOCKED(pp));
2054 
2055 	/* Kluster in tmp_klustsize chunks */
2056 	tp = VTOTN(vp);
2057 	tmp_klustsize = klustsize;
2058 	offset = pp->p_offset;
2059 	klstart = (offset / tmp_klustsize) * tmp_klustsize;
2060 	kllen = MIN(tmp_klustsize, tp->tn_size - klstart);
2061 
2062 	/* Get a kluster of pages */
2063 	pplist =
2064 	    pvn_write_kluster(vp, pp, &tmpoff, &pp_len, klstart, kllen, flags);
2065 
2066 	pp_off = (size_t)tmpoff;
2067 
2068 	/*
2069 	 * Get a cluster of physical offsets for the pages; the amount we
2070 	 * get may be some subrange of what we ask for (io_off, io_len).
2071 	 */
2072 	io_off = pp_off;
2073 	io_len = pp_len;
2074 	err = swap_newphysname(vp, offset, &io_off, &io_len, &pvp, &pstart);
2075 	ASSERT(err != SE_NOANON); /* anon slot must have been filled */
2076 	if (err) {
2077 		pvn_write_done(pplist, B_ERROR | B_WRITE | flags);
2078 		/*
2079 		 * If this routine is called as a result of segvn_sync
2080 		 * operation and we have no physical swap then we can get an
2081 		 * error here. In such case we would return SE_NOSWAP as error.
2082 		 * At this point, we expect only SE_NOSWAP.
2083 		 */
2084 		ASSERT(err == SE_NOSWAP);
2085 		if (flags & B_INVAL)
2086 			err = ENOMEM;
2087 		goto out;
2088 	}
2089 	ASSERT(pp_off <= io_off && io_off + io_len <= pp_off + pp_len);
2090 	ASSERT(io_off <= offset && offset < io_off + io_len);
2091 
2092 	/* Toss pages at front/rear that we couldn't get physical backing for */
2093 	if (io_off != pp_off) {
2094 		npplist = NULL;
2095 		page_list_break(&pplist, &npplist, btop(io_off - pp_off));
2096 		ASSERT(pplist->p_offset == pp_off);
2097 		ASSERT(pplist->p_prev->p_offset == io_off - PAGESIZE);
2098 		pvn_write_done(pplist, B_ERROR | B_WRITE | flags);
2099 		pplist = npplist;
2100 	}
2101 	if (io_off + io_len < pp_off + pp_len) {
2102 		npplist = NULL;
2103 		page_list_break(&pplist, &npplist, btop(io_len));
2104 		ASSERT(npplist->p_offset == io_off + io_len);
2105 		ASSERT(npplist->p_prev->p_offset == pp_off + pp_len - PAGESIZE);
2106 		pvn_write_done(npplist, B_ERROR | B_WRITE | flags);
2107 	}
2108 
2109 	ASSERT(pplist->p_offset == io_off);
2110 	ASSERT(pplist->p_prev->p_offset == io_off + io_len - PAGESIZE);
2111 	ASSERT(btopr(io_len) <= btopr(kllen));
2112 
2113 	/* Do i/o on the remaining kluster */
2114 	err = VOP_PAGEIO(pvp, pplist, (u_offset_t)pstart, io_len,
2115 	    B_WRITE | flags, cr, NULL);
2116 
2117 	if ((flags & B_ASYNC) == 0) {
2118 		pvn_write_done(pplist, ((err) ? B_ERROR : 0) | B_WRITE | flags);
2119 	}
2120 out:
2121 	if (!err) {
2122 		if (offp)
2123 			*offp = io_off;
2124 		if (lenp)
2125 			*lenp = io_len;
2126 		tmp_putpagecnt++;
2127 		tmp_pagespushed += btop(io_len);
2128 	}
2129 	if (err && err != ENOMEM && err != SE_NOSWAP)
2130 		cmn_err(CE_WARN, "tmp_putapage: err %d\n", err);
2131 	return (err);
2132 }
2133 
2134 /* ARGSUSED */
2135 static int
2136 tmp_map(
2137 	struct vnode *vp,
2138 	offset_t off,
2139 	struct as *as,
2140 	caddr_t *addrp,
2141 	size_t len,
2142 	uchar_t prot,
2143 	uchar_t maxprot,
2144 	uint_t flags,
2145 	struct cred *cred,
2146 	caller_context_t *ct)
2147 {
2148 	struct segvn_crargs vn_a;
2149 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
2150 	int error;
2151 
2152 #ifdef _ILP32
2153 	if (len > MAXOFF_T)
2154 		return (ENOMEM);
2155 #endif
2156 
2157 	if (vp->v_flag & VNOMAP)
2158 		return (ENOSYS);
2159 
2160 	if (off < 0 || (offset_t)(off + len) < 0 ||
2161 	    off > MAXOFF_T || (off + len) > MAXOFF_T)
2162 		return (ENXIO);
2163 
2164 	if (vp->v_type != VREG)
2165 		return (ENODEV);
2166 
2167 	/*
2168 	 * Don't allow mapping to locked file
2169 	 */
2170 	if (vn_has_mandatory_locks(vp, tp->tn_mode)) {
2171 		return (EAGAIN);
2172 	}
2173 
2174 	as_rangelock(as);
2175 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
2176 	if (error != 0) {
2177 		as_rangeunlock(as);
2178 		return (error);
2179 	}
2180 
2181 	vn_a.vp = vp;
2182 	vn_a.offset = (u_offset_t)off;
2183 	vn_a.type = flags & MAP_TYPE;
2184 	vn_a.prot = prot;
2185 	vn_a.maxprot = maxprot;
2186 	vn_a.flags = flags & ~MAP_TYPE;
2187 	vn_a.cred = cred;
2188 	vn_a.amp = NULL;
2189 	vn_a.szc = 0;
2190 	vn_a.lgrp_mem_policy_flags = 0;
2191 
2192 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
2193 	as_rangeunlock(as);
2194 	return (error);
2195 }
2196 
2197 /*
2198  * tmp_addmap and tmp_delmap can't be called since the vp
2199  * maintained in the segvn mapping is NULL.
2200  */
2201 /* ARGSUSED */
2202 static int
2203 tmp_addmap(
2204 	struct vnode *vp,
2205 	offset_t off,
2206 	struct as *as,
2207 	caddr_t addr,
2208 	size_t len,
2209 	uchar_t prot,
2210 	uchar_t maxprot,
2211 	uint_t flags,
2212 	struct cred *cred,
2213 	caller_context_t *ct)
2214 {
2215 	return (0);
2216 }
2217 
2218 /* ARGSUSED */
2219 static int
2220 tmp_delmap(
2221 	struct vnode *vp,
2222 	offset_t off,
2223 	struct as *as,
2224 	caddr_t addr,
2225 	size_t len,
2226 	uint_t prot,
2227 	uint_t maxprot,
2228 	uint_t flags,
2229 	struct cred *cred,
2230 	caller_context_t *ct)
2231 {
2232 	return (0);
2233 }
2234 
2235 static int
2236 tmp_freesp(struct vnode *vp, struct flock64 *lp, int flag)
2237 {
2238 	register int i;
2239 	register struct tmpnode *tp = VTOTN(vp);
2240 	int error;
2241 
2242 	ASSERT(vp->v_type == VREG);
2243 	ASSERT(lp->l_start >= 0);
2244 
2245 	if (lp->l_len != 0)
2246 		return (EINVAL);
2247 
2248 	rw_enter(&tp->tn_rwlock, RW_WRITER);
2249 	if (tp->tn_size == lp->l_start) {
2250 		rw_exit(&tp->tn_rwlock);
2251 		return (0);
2252 	}
2253 
2254 	/*
2255 	 * Check for any mandatory locks on the range
2256 	 */
2257 	if (MANDLOCK(vp, tp->tn_mode)) {
2258 		long save_start;
2259 
2260 		save_start = lp->l_start;
2261 
2262 		if (tp->tn_size < lp->l_start) {
2263 			/*
2264 			 * "Truncate up" case: need to make sure there
2265 			 * is no lock beyond current end-of-file. To
2266 			 * do so, we need to set l_start to the size
2267 			 * of the file temporarily.
2268 			 */
2269 			lp->l_start = tp->tn_size;
2270 		}
2271 		lp->l_type = F_WRLCK;
2272 		lp->l_sysid = 0;
2273 		lp->l_pid = ttoproc(curthread)->p_pid;
2274 		i = (flag & (FNDELAY|FNONBLOCK)) ? 0 : SLPFLCK;
2275 		if ((i = reclock(vp, lp, i, 0, lp->l_start, NULL)) != 0 ||
2276 		    lp->l_type != F_UNLCK) {
2277 			rw_exit(&tp->tn_rwlock);
2278 			return (i ? i : EAGAIN);
2279 		}
2280 
2281 		lp->l_start = save_start;
2282 	}
2283 	VFSTOTM(vp->v_vfsp);
2284 
2285 	rw_enter(&tp->tn_contents, RW_WRITER);
2286 	error = tmpnode_trunc((struct tmount *)VFSTOTM(vp->v_vfsp),
2287 	    tp, (ulong_t)lp->l_start);
2288 	rw_exit(&tp->tn_contents);
2289 	rw_exit(&tp->tn_rwlock);
2290 	return (error);
2291 }
2292 
2293 /* ARGSUSED */
2294 static int
2295 tmp_space(
2296 	struct vnode *vp,
2297 	int cmd,
2298 	struct flock64 *bfp,
2299 	int flag,
2300 	offset_t offset,
2301 	cred_t *cred,
2302 	caller_context_t *ct)
2303 {
2304 	int error;
2305 
2306 	if (cmd != F_FREESP)
2307 		return (EINVAL);
2308 	if ((error = convoff(vp, bfp, 0, (offset_t)offset)) == 0) {
2309 		if ((bfp->l_start > MAXOFF_T) || (bfp->l_len > MAXOFF_T))
2310 			return (EFBIG);
2311 		error = tmp_freesp(vp, bfp, flag);
2312 	}
2313 	return (error);
2314 }
2315 
2316 /* ARGSUSED */
2317 static int
2318 tmp_seek(
2319 	struct vnode *vp,
2320 	offset_t ooff,
2321 	offset_t *noffp,
2322 	caller_context_t *ct)
2323 {
2324 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
2325 }
2326 
2327 /* ARGSUSED2 */
2328 static int
2329 tmp_rwlock(struct vnode *vp, int write_lock, caller_context_t *ctp)
2330 {
2331 	struct tmpnode *tp = VTOTN(vp);
2332 
2333 	if (write_lock) {
2334 		rw_enter(&tp->tn_rwlock, RW_WRITER);
2335 	} else {
2336 		rw_enter(&tp->tn_rwlock, RW_READER);
2337 	}
2338 	return (write_lock);
2339 }
2340 
2341 /* ARGSUSED1 */
2342 static void
2343 tmp_rwunlock(struct vnode *vp, int write_lock, caller_context_t *ctp)
2344 {
2345 	struct tmpnode *tp = VTOTN(vp);
2346 
2347 	rw_exit(&tp->tn_rwlock);
2348 }
2349 
2350 static int
2351 tmp_pathconf(
2352 	struct vnode *vp,
2353 	int cmd,
2354 	ulong_t *valp,
2355 	cred_t *cr,
2356 	caller_context_t *ct)
2357 {
2358 	struct tmpnode *tp = NULL;
2359 	int error;
2360 
2361 	switch (cmd) {
2362 	case _PC_XATTR_EXISTS:
2363 		if (vp->v_vfsp->vfs_flag & VFS_XATTR) {
2364 			*valp = 0;	/* assume no attributes */
2365 			error = 0;	/* okay to ask */
2366 			tp = VTOTN(vp);
2367 			rw_enter(&tp->tn_rwlock, RW_READER);
2368 			if (tp->tn_xattrdp) {
2369 				rw_enter(&tp->tn_xattrdp->tn_rwlock, RW_READER);
2370 				/* do not count "." and ".." */
2371 				if (tp->tn_xattrdp->tn_dirents > 2)
2372 					*valp = 1;
2373 				rw_exit(&tp->tn_xattrdp->tn_rwlock);
2374 			}
2375 			rw_exit(&tp->tn_rwlock);
2376 		} else {
2377 			error = EINVAL;
2378 		}
2379 		break;
2380 	case _PC_SATTR_ENABLED:
2381 	case _PC_SATTR_EXISTS:
2382 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2383 		    (vp->v_type == VREG || vp->v_type == VDIR);
2384 		error = 0;
2385 		break;
2386 	default:
2387 		error = fs_pathconf(vp, cmd, valp, cr, ct);
2388 	}
2389 	return (error);
2390 }
2391 
2392 
2393 struct vnodeops *tmp_vnodeops;
2394 
2395 const fs_operation_def_t tmp_vnodeops_template[] = {
2396 	VOPNAME_OPEN,		{ .vop_open = tmp_open },
2397 	VOPNAME_CLOSE,		{ .vop_close = tmp_close },
2398 	VOPNAME_READ,		{ .vop_read = tmp_read },
2399 	VOPNAME_WRITE,		{ .vop_write = tmp_write },
2400 	VOPNAME_IOCTL,		{ .vop_ioctl = tmp_ioctl },
2401 	VOPNAME_GETATTR,	{ .vop_getattr = tmp_getattr },
2402 	VOPNAME_SETATTR,	{ .vop_setattr = tmp_setattr },
2403 	VOPNAME_ACCESS,		{ .vop_access = tmp_access },
2404 	VOPNAME_LOOKUP,		{ .vop_lookup = tmp_lookup },
2405 	VOPNAME_CREATE,		{ .vop_create = tmp_create },
2406 	VOPNAME_REMOVE,		{ .vop_remove = tmp_remove },
2407 	VOPNAME_LINK,		{ .vop_link = tmp_link },
2408 	VOPNAME_RENAME,		{ .vop_rename = tmp_rename },
2409 	VOPNAME_MKDIR,		{ .vop_mkdir = tmp_mkdir },
2410 	VOPNAME_RMDIR,		{ .vop_rmdir = tmp_rmdir },
2411 	VOPNAME_READDIR,	{ .vop_readdir = tmp_readdir },
2412 	VOPNAME_SYMLINK,	{ .vop_symlink = tmp_symlink },
2413 	VOPNAME_READLINK,	{ .vop_readlink = tmp_readlink },
2414 	VOPNAME_FSYNC,		{ .vop_fsync = tmp_fsync },
2415 	VOPNAME_INACTIVE,	{ .vop_inactive = tmp_inactive },
2416 	VOPNAME_FID,		{ .vop_fid = tmp_fid },
2417 	VOPNAME_RWLOCK,		{ .vop_rwlock = tmp_rwlock },
2418 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = tmp_rwunlock },
2419 	VOPNAME_SEEK,		{ .vop_seek = tmp_seek },
2420 	VOPNAME_SPACE,		{ .vop_space = tmp_space },
2421 	VOPNAME_GETPAGE,	{ .vop_getpage = tmp_getpage },
2422 	VOPNAME_PUTPAGE,	{ .vop_putpage = tmp_putpage },
2423 	VOPNAME_MAP,		{ .vop_map = tmp_map },
2424 	VOPNAME_ADDMAP,		{ .vop_addmap = tmp_addmap },
2425 	VOPNAME_DELMAP,		{ .vop_delmap = tmp_delmap },
2426 	VOPNAME_PATHCONF,	{ .vop_pathconf = tmp_pathconf },
2427 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
2428 	NULL,			NULL
2429 };
2430