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