xref: /onnv-gate/usr/src/uts/common/fs/tmpfs/tmp_vnops.c (revision 10440:ba48e0ae8d55)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51841Spraks  * Common Development and Distribution License (the "License").
61841Spraks  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*10440SRoger.Faulkner@Sun.COM 
220Sstevel@tonic-gate /*
23*10440SRoger.Faulkner@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/t_lock.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/sysmacros.h>
320Sstevel@tonic-gate #include <sys/user.h>
330Sstevel@tonic-gate #include <sys/time.h>
340Sstevel@tonic-gate #include <sys/vfs.h>
353898Srsb #include <sys/vfs_opreg.h>
360Sstevel@tonic-gate #include <sys/vnode.h>
370Sstevel@tonic-gate #include <sys/file.h>
380Sstevel@tonic-gate #include <sys/fcntl.h>
390Sstevel@tonic-gate #include <sys/flock.h>
400Sstevel@tonic-gate #include <sys/kmem.h>
410Sstevel@tonic-gate #include <sys/uio.h>
420Sstevel@tonic-gate #include <sys/errno.h>
430Sstevel@tonic-gate #include <sys/stat.h>
440Sstevel@tonic-gate #include <sys/cred.h>
450Sstevel@tonic-gate #include <sys/dirent.h>
460Sstevel@tonic-gate #include <sys/pathname.h>
470Sstevel@tonic-gate #include <sys/vmsystm.h>
480Sstevel@tonic-gate #include <sys/fs/tmp.h>
490Sstevel@tonic-gate #include <sys/fs/tmpnode.h>
500Sstevel@tonic-gate #include <sys/mman.h>
510Sstevel@tonic-gate #include <vm/hat.h>
520Sstevel@tonic-gate #include <vm/seg_vn.h>
530Sstevel@tonic-gate #include <vm/seg_map.h>
540Sstevel@tonic-gate #include <vm/seg.h>
550Sstevel@tonic-gate #include <vm/anon.h>
560Sstevel@tonic-gate #include <vm/as.h>
570Sstevel@tonic-gate #include <vm/page.h>
580Sstevel@tonic-gate #include <vm/pvn.h>
590Sstevel@tonic-gate #include <sys/cmn_err.h>
600Sstevel@tonic-gate #include <sys/debug.h>
610Sstevel@tonic-gate #include <sys/swap.h>
620Sstevel@tonic-gate #include <sys/buf.h>
630Sstevel@tonic-gate #include <sys/vm.h>
640Sstevel@tonic-gate #include <sys/vtrace.h>
650Sstevel@tonic-gate #include <sys/policy.h>
660Sstevel@tonic-gate #include <fs/fs_subr.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate static int	tmp_getapage(struct vnode *, u_offset_t, size_t, uint_t *,
690Sstevel@tonic-gate 	page_t **, size_t, struct seg *, caddr_t, enum seg_rw, struct cred *);
700Sstevel@tonic-gate static int 	tmp_putapage(struct vnode *, page_t *, u_offset_t *, size_t *,
710Sstevel@tonic-gate 	int, struct cred *);
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /* ARGSUSED1 */
740Sstevel@tonic-gate static int
tmp_open(struct vnode ** vpp,int flag,struct cred * cred,caller_context_t * ct)755331Samw tmp_open(struct vnode **vpp, int flag, struct cred *cred, caller_context_t *ct)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	/*
780Sstevel@tonic-gate 	 * swapon to a tmpfs file is not supported so access
790Sstevel@tonic-gate 	 * is denied on open if VISSWAP is set.
800Sstevel@tonic-gate 	 */
810Sstevel@tonic-gate 	if ((*vpp)->v_flag & VISSWAP)
820Sstevel@tonic-gate 		return (EINVAL);
830Sstevel@tonic-gate 	return (0);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /* ARGSUSED1 */
870Sstevel@tonic-gate static int
tmp_close(struct vnode * vp,int flag,int count,offset_t offset,struct cred * cred,caller_context_t * ct)885331Samw tmp_close(
895331Samw 	struct vnode *vp,
905331Samw 	int flag,
915331Samw 	int count,
925331Samw 	offset_t offset,
935331Samw 	struct cred *cred,
945331Samw 	caller_context_t *ct)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
970Sstevel@tonic-gate 	cleanshares(vp, ttoproc(curthread)->p_pid);
980Sstevel@tonic-gate 	return (0);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate  * wrtmp does the real work of write requests for tmpfs.
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate static int
wrtmp(struct tmount * tm,struct tmpnode * tp,struct uio * uio,struct cred * cr,struct caller_context * ct)1050Sstevel@tonic-gate wrtmp(
1060Sstevel@tonic-gate 	struct tmount *tm,
1070Sstevel@tonic-gate 	struct tmpnode *tp,
1080Sstevel@tonic-gate 	struct uio *uio,
1090Sstevel@tonic-gate 	struct cred *cr,
1100Sstevel@tonic-gate 	struct caller_context *ct)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	pgcnt_t pageoffset;	/* offset in pages */
1130Sstevel@tonic-gate 	ulong_t segmap_offset;	/* pagesize byte offset into segmap */
1140Sstevel@tonic-gate 	caddr_t base;		/* base of segmap */
1150Sstevel@tonic-gate 	ssize_t bytes;		/* bytes to uiomove */
1160Sstevel@tonic-gate 	pfn_t pagenumber;	/* offset in pages into tmp file */
1170Sstevel@tonic-gate 	struct vnode *vp;
1180Sstevel@tonic-gate 	int error = 0;
1190Sstevel@tonic-gate 	int	pagecreate;	/* == 1 if we allocated a page */
1200Sstevel@tonic-gate 	int	newpage;
1210Sstevel@tonic-gate 	rlim64_t limit = uio->uio_llimit;
1220Sstevel@tonic-gate 	long oresid = uio->uio_resid;
1230Sstevel@tonic-gate 	timestruc_t now;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	long tn_size_changed = 0;
1260Sstevel@tonic-gate 	long old_tn_size;
1275928Sjj204856 	long new_tn_size;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	vp = TNTOV(tp);
1300Sstevel@tonic-gate 	ASSERT(vp->v_type == VREG);
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	TRACE_1(TR_FAC_TMPFS, TR_TMPFS_RWTMP_START,
1335928Sjj204856 	    "tmp_wrtmp_start:vp %p", vp);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&tp->tn_contents));
1360Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&tp->tn_rwlock));
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	if (MANDLOCK(vp, tp->tn_mode)) {
1390Sstevel@tonic-gate 		rw_exit(&tp->tn_contents);
1400Sstevel@tonic-gate 		/*
1410Sstevel@tonic-gate 		 * tmp_getattr ends up being called by chklock
1420Sstevel@tonic-gate 		 */
1435928Sjj204856 		error = chklock(vp, FWRITE, uio->uio_loffset, uio->uio_resid,
1445928Sjj204856 		    uio->uio_fmode, ct);
1450Sstevel@tonic-gate 		rw_enter(&tp->tn_contents, RW_WRITER);
1460Sstevel@tonic-gate 		if (error != 0) {
1470Sstevel@tonic-gate 			TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
1485928Sjj204856 			    "tmp_wrtmp_end:vp %p error %d", vp, error);
1490Sstevel@tonic-gate 			return (error);
1500Sstevel@tonic-gate 		}
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	if (uio->uio_loffset < 0)
1540Sstevel@tonic-gate 		return (EINVAL);
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
1570Sstevel@tonic-gate 		limit = MAXOFFSET_T;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	if (uio->uio_loffset >= limit) {
1600Sstevel@tonic-gate 		proc_t *p = ttoproc(curthread);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
1630Sstevel@tonic-gate 		(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], p->p_rctls,
1640Sstevel@tonic-gate 		    p, RCA_UNSAFE_SIGINFO);
1650Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1660Sstevel@tonic-gate 		return (EFBIG);
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	if (uio->uio_loffset >= MAXOFF_T) {
1700Sstevel@tonic-gate 		TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
1715928Sjj204856 		    "tmp_wrtmp_end:vp %p error %d", vp, EINVAL);
1720Sstevel@tonic-gate 		return (EFBIG);
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	if (uio->uio_resid == 0) {
1760Sstevel@tonic-gate 		TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
1775928Sjj204856 		    "tmp_wrtmp_end:vp %p error %d", vp, 0);
1780Sstevel@tonic-gate 		return (0);
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (limit > MAXOFF_T)
1820Sstevel@tonic-gate 		limit = MAXOFF_T;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	do {
1850Sstevel@tonic-gate 		long	offset;
1860Sstevel@tonic-gate 		long	delta;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 		offset = (long)uio->uio_offset;
1890Sstevel@tonic-gate 		pageoffset = offset & PAGEOFFSET;
1900Sstevel@tonic-gate 		/*
1910Sstevel@tonic-gate 		 * A maximum of PAGESIZE bytes of data is transferred
1920Sstevel@tonic-gate 		 * each pass through this loop
1930Sstevel@tonic-gate 		 */
1940Sstevel@tonic-gate 		bytes = MIN(PAGESIZE - pageoffset, uio->uio_resid);
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 		if (offset + bytes >= limit) {
1970Sstevel@tonic-gate 			if (offset >= limit) {
1980Sstevel@tonic-gate 				error = EFBIG;
1990Sstevel@tonic-gate 				goto out;
2000Sstevel@tonic-gate 			}
2010Sstevel@tonic-gate 			bytes = limit - offset;
2020Sstevel@tonic-gate 		}
2030Sstevel@tonic-gate 		pagenumber = btop(offset);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 		/*
2060Sstevel@tonic-gate 		 * delta is the amount of anonymous memory
2070Sstevel@tonic-gate 		 * to reserve for the file.
2080Sstevel@tonic-gate 		 * We always reserve in pagesize increments so
2090Sstevel@tonic-gate 		 * unless we're extending the file into a new page,
2100Sstevel@tonic-gate 		 * we don't need to call tmp_resv.
2110Sstevel@tonic-gate 		 */
2120Sstevel@tonic-gate 		delta = offset + bytes -
2130Sstevel@tonic-gate 		    P2ROUNDUP_TYPED(tp->tn_size, PAGESIZE, u_offset_t);
2140Sstevel@tonic-gate 		if (delta > 0) {
2150Sstevel@tonic-gate 			pagecreate = 1;
2160Sstevel@tonic-gate 			if (tmp_resv(tm, tp, delta, pagecreate)) {
2173247Sgjelinek 				/*
2183247Sgjelinek 				 * Log file system full in the zone that owns
2193247Sgjelinek 				 * the tmpfs mount, as well as in the global
2203247Sgjelinek 				 * zone if necessary.
2213247Sgjelinek 				 */
2223247Sgjelinek 				zcmn_err(tm->tm_vfsp->vfs_zone->zone_id,
2233247Sgjelinek 				    CE_WARN, "%s: File system full, "
2243247Sgjelinek 				    "swap space limit exceeded",
2250Sstevel@tonic-gate 				    tm->tm_mntpath);
2263247Sgjelinek 
2273247Sgjelinek 				if (tm->tm_vfsp->vfs_zone->zone_id !=
2283247Sgjelinek 				    GLOBAL_ZONEID) {
2293247Sgjelinek 
2303247Sgjelinek 					vfs_t *vfs = tm->tm_vfsp;
2313247Sgjelinek 
2323247Sgjelinek 					zcmn_err(GLOBAL_ZONEID,
2333247Sgjelinek 					    CE_WARN, "%s: File system full, "
2343247Sgjelinek 					    "swap space limit exceeded",
2353247Sgjelinek 					    vfs->vfs_vnodecovered->v_path);
2363247Sgjelinek 				}
2370Sstevel@tonic-gate 				error = ENOSPC;
2380Sstevel@tonic-gate 				break;
2390Sstevel@tonic-gate 			}
2400Sstevel@tonic-gate 			tmpnode_growmap(tp, (ulong_t)offset + bytes);
2410Sstevel@tonic-gate 		}
2420Sstevel@tonic-gate 		/* grow the file to the new length */
2430Sstevel@tonic-gate 		if (offset + bytes > tp->tn_size) {
2440Sstevel@tonic-gate 			tn_size_changed = 1;
2450Sstevel@tonic-gate 			old_tn_size = tp->tn_size;
2465928Sjj204856 			/*
2475928Sjj204856 			 * Postpone updating tp->tn_size until uiomove() is
2485928Sjj204856 			 * done.
2495928Sjj204856 			 */
2505928Sjj204856 			new_tn_size = offset + bytes;
2510Sstevel@tonic-gate 		}
2520Sstevel@tonic-gate 		if (bytes == PAGESIZE) {
2530Sstevel@tonic-gate 			/*
2540Sstevel@tonic-gate 			 * Writing whole page so reading from disk
2550Sstevel@tonic-gate 			 * is a waste
2560Sstevel@tonic-gate 			 */
2570Sstevel@tonic-gate 			pagecreate = 1;
2580Sstevel@tonic-gate 		} else {
2590Sstevel@tonic-gate 			pagecreate = 0;
2600Sstevel@tonic-gate 		}
2610Sstevel@tonic-gate 		/*
2620Sstevel@tonic-gate 		 * If writing past EOF or filling in a hole
2630Sstevel@tonic-gate 		 * we need to allocate an anon slot.
2640Sstevel@tonic-gate 		 */
2650Sstevel@tonic-gate 		if (anon_get_ptr(tp->tn_anon, pagenumber) == NULL) {
2660Sstevel@tonic-gate 			(void) anon_set_ptr(tp->tn_anon, pagenumber,
2675928Sjj204856 			    anon_alloc(vp, ptob(pagenumber)), ANON_SLEEP);
2680Sstevel@tonic-gate 			pagecreate = 1;
2690Sstevel@tonic-gate 			tp->tn_nblocks++;
2700Sstevel@tonic-gate 		}
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 		/*
2734232Spraks 		 * We have to drop the contents lock to allow the VM
2745331Samw 		 * system to reacquire it in tmp_getpage()
2750Sstevel@tonic-gate 		 */
2764232Spraks 		rw_exit(&tp->tn_contents);
2770Sstevel@tonic-gate 
2788059SDonghai.Qiao@Sun.COM 		/*
2798059SDonghai.Qiao@Sun.COM 		 * Touch the page and fault it in if it is not in core
2808059SDonghai.Qiao@Sun.COM 		 * before segmap_getmapflt or vpm_data_copy can lock it.
2818059SDonghai.Qiao@Sun.COM 		 * This is to avoid the deadlock if the buffer is mapped
2828059SDonghai.Qiao@Sun.COM 		 * to the same file through mmap which we want to write.
2838059SDonghai.Qiao@Sun.COM 		 */
2848059SDonghai.Qiao@Sun.COM 		uio_prefaultpages((long)bytes, uio);
2858059SDonghai.Qiao@Sun.COM 
2861841Spraks 		newpage = 0;
2871841Spraks 		if (vpm_enable) {
2881841Spraks 			/*
2891841Spraks 			 * Copy data. If new pages are created, part of
2901841Spraks 			 * the page that is not written will be initizliazed
2911841Spraks 			 * with zeros.
2921841Spraks 			 */
2931841Spraks 			error = vpm_data_copy(vp, offset, bytes, uio,
2945928Sjj204856 			    !pagecreate, &newpage, 1, S_WRITE);
2951841Spraks 		} else {
2961841Spraks 			/* Get offset within the segmap mapping */
2971841Spraks 			segmap_offset = (offset & PAGEMASK) & MAXBOFFSET;
2981841Spraks 			base = segmap_getmapflt(segkmap, vp,
2995928Sjj204856 			    (offset &  MAXBMASK), PAGESIZE, !pagecreate,
3005928Sjj204856 			    S_WRITE);
3011841Spraks 		}
3020Sstevel@tonic-gate 
3031841Spraks 
3041841Spraks 		if (!vpm_enable && pagecreate) {
3050Sstevel@tonic-gate 			/*
3060Sstevel@tonic-gate 			 * segmap_pagecreate() returns 1 if it calls
3070Sstevel@tonic-gate 			 * page_create_va() to allocate any pages.
3080Sstevel@tonic-gate 			 */
3090Sstevel@tonic-gate 			newpage = segmap_pagecreate(segkmap,
3100Sstevel@tonic-gate 			    base + segmap_offset, (size_t)PAGESIZE, 0);
3110Sstevel@tonic-gate 			/*
3120Sstevel@tonic-gate 			 * Clear from the beginning of the page to the starting
3130Sstevel@tonic-gate 			 * offset of the data.
3140Sstevel@tonic-gate 			 */
3150Sstevel@tonic-gate 			if (pageoffset != 0)
3160Sstevel@tonic-gate 				(void) kzero(base + segmap_offset,
3170Sstevel@tonic-gate 				    (size_t)pageoffset);
3180Sstevel@tonic-gate 		}
3190Sstevel@tonic-gate 
3201841Spraks 		if (!vpm_enable) {
3211841Spraks 			error = uiomove(base + segmap_offset + pageoffset,
3225928Sjj204856 			    (long)bytes, UIO_WRITE, uio);
3231841Spraks 		}
3240Sstevel@tonic-gate 
3251841Spraks 		if (!vpm_enable && pagecreate &&
3260Sstevel@tonic-gate 		    uio->uio_offset < P2ROUNDUP(offset + bytes, PAGESIZE)) {
3270Sstevel@tonic-gate 			long	zoffset; /* zero from offset into page */
3280Sstevel@tonic-gate 			/*
3290Sstevel@tonic-gate 			 * We created pages w/o initializing them completely,
3300Sstevel@tonic-gate 			 * thus we need to zero the part that wasn't set up.
3310Sstevel@tonic-gate 			 * This happens on most EOF write cases and if
3320Sstevel@tonic-gate 			 * we had some sort of error during the uiomove.
3330Sstevel@tonic-gate 			 */
3340Sstevel@tonic-gate 			long nmoved;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 			nmoved = uio->uio_offset - offset;
3370Sstevel@tonic-gate 			ASSERT((nmoved + pageoffset) <= PAGESIZE);
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 			/*
3400Sstevel@tonic-gate 			 * Zero from the end of data in the page to the
3410Sstevel@tonic-gate 			 * end of the page.
3420Sstevel@tonic-gate 			 */
3430Sstevel@tonic-gate 			if ((zoffset = pageoffset + nmoved) < PAGESIZE)
3440Sstevel@tonic-gate 				(void) kzero(base + segmap_offset + zoffset,
3455928Sjj204856 				    (size_t)PAGESIZE - zoffset);
3460Sstevel@tonic-gate 		}
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 		/*
3490Sstevel@tonic-gate 		 * Unlock the pages which have been allocated by
3500Sstevel@tonic-gate 		 * page_create_va() in segmap_pagecreate()
3510Sstevel@tonic-gate 		 */
3521841Spraks 		if (!vpm_enable && newpage) {
3530Sstevel@tonic-gate 			segmap_pageunlock(segkmap, base + segmap_offset,
3540Sstevel@tonic-gate 			    (size_t)PAGESIZE, S_WRITE);
3551841Spraks 		}
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 		if (error) {
3580Sstevel@tonic-gate 			/*
3590Sstevel@tonic-gate 			 * If we failed on a write, we must
3600Sstevel@tonic-gate 			 * be sure to invalidate any pages that may have
3610Sstevel@tonic-gate 			 * been allocated.
3620Sstevel@tonic-gate 			 */
3631841Spraks 			if (vpm_enable) {
3645928Sjj204856 				(void) vpm_sync_pages(vp, offset, PAGESIZE,
3655928Sjj204856 				    SM_INVAL);
3661841Spraks 			} else {
3671841Spraks 				(void) segmap_release(segkmap, base, SM_INVAL);
3681841Spraks 			}
3690Sstevel@tonic-gate 		} else {
3701841Spraks 			if (vpm_enable) {
3715928Sjj204856 				error = vpm_sync_pages(vp, offset, PAGESIZE,
3725928Sjj204856 				    0);
3731841Spraks 			} else {
3741841Spraks 				error = segmap_release(segkmap, base, 0);
3751841Spraks 			}
3760Sstevel@tonic-gate 		}
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 		/*
3790Sstevel@tonic-gate 		 * Re-acquire contents lock.
3800Sstevel@tonic-gate 		 */
3810Sstevel@tonic-gate 		rw_enter(&tp->tn_contents, RW_WRITER);
3825928Sjj204856 
3835928Sjj204856 		/*
3845928Sjj204856 		 * Update tn_size.
3855928Sjj204856 		 */
3865928Sjj204856 		if (tn_size_changed)
3875928Sjj204856 			tp->tn_size = new_tn_size;
3885928Sjj204856 
3890Sstevel@tonic-gate 		/*
3900Sstevel@tonic-gate 		 * If the uiomove failed, fix up tn_size.
3910Sstevel@tonic-gate 		 */
3920Sstevel@tonic-gate 		if (error) {
3930Sstevel@tonic-gate 			if (tn_size_changed) {
3940Sstevel@tonic-gate 				/*
3950Sstevel@tonic-gate 				 * The uiomove failed, and we
3960Sstevel@tonic-gate 				 * allocated blocks,so get rid
3970Sstevel@tonic-gate 				 * of them.
3980Sstevel@tonic-gate 				 */
3990Sstevel@tonic-gate 				(void) tmpnode_trunc(tm, tp,
4000Sstevel@tonic-gate 				    (ulong_t)old_tn_size);
4010Sstevel@tonic-gate 			}
4020Sstevel@tonic-gate 		} else {
4030Sstevel@tonic-gate 			/*
4040Sstevel@tonic-gate 			 * XXX - Can this be out of the loop?
4050Sstevel@tonic-gate 			 */
4060Sstevel@tonic-gate 			if ((tp->tn_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) &&
4070Sstevel@tonic-gate 			    (tp->tn_mode & (S_ISUID | S_ISGID)) &&
4080Sstevel@tonic-gate 			    secpolicy_vnode_setid_retain(cr,
4090Sstevel@tonic-gate 			    (tp->tn_mode & S_ISUID) != 0 && tp->tn_uid == 0)) {
4100Sstevel@tonic-gate 				/*
4110Sstevel@tonic-gate 				 * Clear Set-UID & Set-GID bits on
4120Sstevel@tonic-gate 				 * successful write if not privileged
4130Sstevel@tonic-gate 				 * and at least one of the execute bits
4140Sstevel@tonic-gate 				 * is set.  If we always clear Set-GID,
4150Sstevel@tonic-gate 				 * mandatory file and record locking is
4160Sstevel@tonic-gate 				 * unuseable.
4170Sstevel@tonic-gate 				 */
4180Sstevel@tonic-gate 				tp->tn_mode &= ~(S_ISUID | S_ISGID);
4190Sstevel@tonic-gate 			}
4200Sstevel@tonic-gate 			gethrestime(&now);
4210Sstevel@tonic-gate 			tp->tn_mtime = now;
4220Sstevel@tonic-gate 			tp->tn_ctime = now;
4230Sstevel@tonic-gate 		}
4240Sstevel@tonic-gate 	} while (error == 0 && uio->uio_resid > 0 && bytes != 0);
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate out:
4270Sstevel@tonic-gate 	/*
4280Sstevel@tonic-gate 	 * If we've already done a partial-write, terminate
4290Sstevel@tonic-gate 	 * the write but return no error.
4300Sstevel@tonic-gate 	 */
4310Sstevel@tonic-gate 	if (oresid != uio->uio_resid)
4320Sstevel@tonic-gate 		error = 0;
4330Sstevel@tonic-gate 	TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
4345928Sjj204856 	    "tmp_wrtmp_end:vp %p error %d", vp, error);
4350Sstevel@tonic-gate 	return (error);
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate /*
4390Sstevel@tonic-gate  * rdtmp does the real work of read requests for tmpfs.
4400Sstevel@tonic-gate  */
4410Sstevel@tonic-gate static int
rdtmp(struct tmount * tm,struct tmpnode * tp,struct uio * uio,struct caller_context * ct)4420Sstevel@tonic-gate rdtmp(
4430Sstevel@tonic-gate 	struct tmount *tm,
4440Sstevel@tonic-gate 	struct tmpnode *tp,
4450Sstevel@tonic-gate 	struct uio *uio,
4460Sstevel@tonic-gate 	struct caller_context *ct)
4470Sstevel@tonic-gate {
4480Sstevel@tonic-gate 	ulong_t pageoffset;	/* offset in tmpfs file (uio_offset) */
4490Sstevel@tonic-gate 	ulong_t segmap_offset;	/* pagesize byte offset into segmap */
4500Sstevel@tonic-gate 	caddr_t base;		/* base of segmap */
4510Sstevel@tonic-gate 	ssize_t bytes;		/* bytes to uiomove */
4520Sstevel@tonic-gate 	struct vnode *vp;
4530Sstevel@tonic-gate 	int error;
4540Sstevel@tonic-gate 	long oresid = uio->uio_resid;
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate #if defined(lint)
4570Sstevel@tonic-gate 	tm = tm;
4580Sstevel@tonic-gate #endif
4590Sstevel@tonic-gate 	vp = TNTOV(tp);
4600Sstevel@tonic-gate 
4615928Sjj204856 	TRACE_1(TR_FAC_TMPFS, TR_TMPFS_RWTMP_START, "tmp_rdtmp_start:vp %p",
4625928Sjj204856 	    vp);
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&tp->tn_contents));
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	if (MANDLOCK(vp, tp->tn_mode)) {
4670Sstevel@tonic-gate 		rw_exit(&tp->tn_contents);
4680Sstevel@tonic-gate 		/*
4690Sstevel@tonic-gate 		 * tmp_getattr ends up being called by chklock
4700Sstevel@tonic-gate 		 */
4715928Sjj204856 		error = chklock(vp, FREAD, uio->uio_loffset, uio->uio_resid,
4725928Sjj204856 		    uio->uio_fmode, ct);
4730Sstevel@tonic-gate 		rw_enter(&tp->tn_contents, RW_READER);
4740Sstevel@tonic-gate 		if (error != 0) {
4750Sstevel@tonic-gate 			TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
4765928Sjj204856 			    "tmp_rdtmp_end:vp %p error %d", vp, error);
4770Sstevel@tonic-gate 			return (error);
4780Sstevel@tonic-gate 		}
4790Sstevel@tonic-gate 	}
4800Sstevel@tonic-gate 	ASSERT(tp->tn_type == VREG);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	if (uio->uio_loffset >= MAXOFF_T) {
4830Sstevel@tonic-gate 		TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
4845928Sjj204856 		    "tmp_rdtmp_end:vp %p error %d", vp, EINVAL);
4850Sstevel@tonic-gate 		return (0);
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate 	if (uio->uio_loffset < 0)
4880Sstevel@tonic-gate 		return (EINVAL);
4890Sstevel@tonic-gate 	if (uio->uio_resid == 0) {
4900Sstevel@tonic-gate 		TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
4915928Sjj204856 		    "tmp_rdtmp_end:vp %p error %d", vp, 0);
4920Sstevel@tonic-gate 		return (0);
4930Sstevel@tonic-gate 	}
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	vp = TNTOV(tp);
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	do {
4980Sstevel@tonic-gate 		long diff;
4990Sstevel@tonic-gate 		long offset;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 		offset = uio->uio_offset;
5020Sstevel@tonic-gate 		pageoffset = offset & PAGEOFFSET;
5030Sstevel@tonic-gate 		bytes = MIN(PAGESIZE - pageoffset, uio->uio_resid);
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 		diff = tp->tn_size - offset;
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 		if (diff <= 0) {
5080Sstevel@tonic-gate 			error = 0;
5090Sstevel@tonic-gate 			goto out;
5100Sstevel@tonic-gate 		}
5110Sstevel@tonic-gate 		if (diff < bytes)
5120Sstevel@tonic-gate 			bytes = diff;
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 		/*
5155928Sjj204856 		 * We have to drop the contents lock to allow the VM system
5165928Sjj204856 		 * to reacquire it in tmp_getpage() should the uiomove cause a
5175928Sjj204856 		 * pagefault.
5180Sstevel@tonic-gate 		 */
5190Sstevel@tonic-gate 		rw_exit(&tp->tn_contents);
5200Sstevel@tonic-gate 
5211841Spraks 		if (vpm_enable) {
5221841Spraks 			/*
5231841Spraks 			 * Copy data.
5241841Spraks 			 */
5255928Sjj204856 			error = vpm_data_copy(vp, offset, bytes, uio, 1, NULL,
5265928Sjj204856 			    0, S_READ);
5271841Spraks 		} else {
5281841Spraks 			segmap_offset = (offset & PAGEMASK) & MAXBOFFSET;
5291841Spraks 			base = segmap_getmapflt(segkmap, vp, offset & MAXBMASK,
5301841Spraks 			    bytes, 1, S_READ);
5311841Spraks 
5321841Spraks 			error = uiomove(base + segmap_offset + pageoffset,
5331841Spraks 			    (long)bytes, UIO_READ, uio);
5341841Spraks 		}
5350Sstevel@tonic-gate 
5361841Spraks 		if (error) {
5371841Spraks 			if (vpm_enable) {
5385928Sjj204856 				(void) vpm_sync_pages(vp, offset, PAGESIZE, 0);
5391841Spraks 			} else {
5401841Spraks 				(void) segmap_release(segkmap, base, 0);
5411841Spraks 			}
5421841Spraks 		} else {
5431841Spraks 			if (vpm_enable) {
5445928Sjj204856 				error = vpm_sync_pages(vp, offset, PAGESIZE,
5455928Sjj204856 				    0);
5461841Spraks 			} else {
5471841Spraks 				error = segmap_release(segkmap, base, 0);
5481841Spraks 			}
5491841Spraks 		}
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 		/*
5520Sstevel@tonic-gate 		 * Re-acquire contents lock.
5530Sstevel@tonic-gate 		 */
5540Sstevel@tonic-gate 		rw_enter(&tp->tn_contents, RW_READER);
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	} while (error == 0 && uio->uio_resid > 0);
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate out:
5590Sstevel@tonic-gate 	gethrestime(&tp->tn_atime);
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	/*
5620Sstevel@tonic-gate 	 * If we've already done a partial read, terminate
5630Sstevel@tonic-gate 	 * the read but return no error.
5640Sstevel@tonic-gate 	 */
5650Sstevel@tonic-gate 	if (oresid != uio->uio_resid)
5660Sstevel@tonic-gate 		error = 0;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
5695928Sjj204856 	    "tmp_rdtmp_end:vp %x error %d", vp, error);
5700Sstevel@tonic-gate 	return (error);
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate /* ARGSUSED2 */
5740Sstevel@tonic-gate static int
tmp_read(struct vnode * vp,struct uio * uiop,int ioflag,cred_t * cred,struct caller_context * ct)5750Sstevel@tonic-gate tmp_read(struct vnode *vp, struct uio *uiop, int ioflag, cred_t *cred,
5765928Sjj204856     struct caller_context *ct)
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
5790Sstevel@tonic-gate 	struct tmount *tm = (struct tmount *)VTOTM(vp);
5800Sstevel@tonic-gate 	int error;
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	/*
5830Sstevel@tonic-gate 	 * We don't currently support reading non-regular files
5840Sstevel@tonic-gate 	 */
5850Sstevel@tonic-gate 	if (vp->v_type == VDIR)
5860Sstevel@tonic-gate 		return (EISDIR);
5870Sstevel@tonic-gate 	if (vp->v_type != VREG)
5880Sstevel@tonic-gate 		return (EINVAL);
5890Sstevel@tonic-gate 	/*
5900Sstevel@tonic-gate 	 * tmp_rwlock should have already been called from layers above
5910Sstevel@tonic-gate 	 */
5920Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&tp->tn_rwlock));
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	rw_enter(&tp->tn_contents, RW_READER);
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	error = rdtmp(tm, tp, uiop, ct);
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	rw_exit(&tp->tn_contents);
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	return (error);
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate static int
tmp_write(struct vnode * vp,struct uio * uiop,int ioflag,struct cred * cred,struct caller_context * ct)6040Sstevel@tonic-gate tmp_write(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cred,
6055928Sjj204856     struct caller_context *ct)
6060Sstevel@tonic-gate {
6070Sstevel@tonic-gate 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
6080Sstevel@tonic-gate 	struct tmount *tm = (struct tmount *)VTOTM(vp);
6090Sstevel@tonic-gate 	int error;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	/*
6120Sstevel@tonic-gate 	 * We don't currently support writing to non-regular files
6130Sstevel@tonic-gate 	 */
6140Sstevel@tonic-gate 	if (vp->v_type != VREG)
6150Sstevel@tonic-gate 		return (EINVAL);	/* XXX EISDIR? */
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	/*
6180Sstevel@tonic-gate 	 * tmp_rwlock should have already been called from layers above
6190Sstevel@tonic-gate 	 */
6200Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&tp->tn_rwlock));
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	rw_enter(&tp->tn_contents, RW_WRITER);
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	if (ioflag & FAPPEND) {
6250Sstevel@tonic-gate 		/*
6260Sstevel@tonic-gate 		 * In append mode start at end of file.
6270Sstevel@tonic-gate 		 */
6280Sstevel@tonic-gate 		uiop->uio_loffset = tp->tn_size;
6290Sstevel@tonic-gate 	}
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	error = wrtmp(tm, tp, uiop, cred, ct);
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	rw_exit(&tp->tn_contents);
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	return (error);
6360Sstevel@tonic-gate }
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate /* ARGSUSED */
6390Sstevel@tonic-gate static int
tmp_ioctl(struct vnode * vp,int com,intptr_t data,int flag,struct cred * cred,int * rvalp,caller_context_t * ct)6405331Samw tmp_ioctl(
6415331Samw 	struct vnode *vp,
6425331Samw 	int com,
6435331Samw 	intptr_t data,
6445331Samw 	int flag,
6455331Samw 	struct cred *cred,
6465331Samw 	int *rvalp,
6475331Samw 	caller_context_t *ct)
6480Sstevel@tonic-gate {
6490Sstevel@tonic-gate 	return (ENOTTY);
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate /* ARGSUSED2 */
6530Sstevel@tonic-gate static int
tmp_getattr(struct vnode * vp,struct vattr * vap,int flags,struct cred * cred,caller_context_t * ct)6545331Samw tmp_getattr(
6555331Samw 	struct vnode *vp,
6565331Samw 	struct vattr *vap,
6575331Samw 	int flags,
6585331Samw 	struct cred *cred,
6595331Samw 	caller_context_t *ct)
6600Sstevel@tonic-gate {
6610Sstevel@tonic-gate 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
6620Sstevel@tonic-gate 	struct vnode *mvp;
6630Sstevel@tonic-gate 	struct vattr va;
6640Sstevel@tonic-gate 	int attrs = 1;
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	/*
6670Sstevel@tonic-gate 	 * A special case to handle the root tnode on a diskless nfs
6680Sstevel@tonic-gate 	 * client who may have had its uid and gid inherited
6690Sstevel@tonic-gate 	 * from an nfs vnode with nobody ownership.  Likely the
6700Sstevel@tonic-gate 	 * root filesystem. After nfs is fully functional the uid/gid
6710Sstevel@tonic-gate 	 * may be mapable so ask again.
6720Sstevel@tonic-gate 	 * vfsp can't get unmounted because we hold vp.
6730Sstevel@tonic-gate 	 */
6740Sstevel@tonic-gate 	if (vp->v_flag & VROOT &&
6750Sstevel@tonic-gate 	    (mvp = vp->v_vfsp->vfs_vnodecovered) != NULL) {
6760Sstevel@tonic-gate 		mutex_enter(&tp->tn_tlock);
6770Sstevel@tonic-gate 		if (tp->tn_uid == UID_NOBODY || tp->tn_gid == GID_NOBODY) {
6780Sstevel@tonic-gate 			mutex_exit(&tp->tn_tlock);
6790Sstevel@tonic-gate 			bzero(&va, sizeof (struct vattr));
6800Sstevel@tonic-gate 			va.va_mask = AT_UID|AT_GID;
6815331Samw 			attrs = VOP_GETATTR(mvp, &va, 0, cred, ct);
6820Sstevel@tonic-gate 		} else {
6830Sstevel@tonic-gate 			mutex_exit(&tp->tn_tlock);
6840Sstevel@tonic-gate 		}
6850Sstevel@tonic-gate 	}
6860Sstevel@tonic-gate 	mutex_enter(&tp->tn_tlock);
6870Sstevel@tonic-gate 	if (attrs == 0) {
6880Sstevel@tonic-gate 		tp->tn_uid = va.va_uid;
6890Sstevel@tonic-gate 		tp->tn_gid = va.va_gid;
6900Sstevel@tonic-gate 	}
6910Sstevel@tonic-gate 	vap->va_type = vp->v_type;
6920Sstevel@tonic-gate 	vap->va_mode = tp->tn_mode & MODEMASK;
6930Sstevel@tonic-gate 	vap->va_uid = tp->tn_uid;
6940Sstevel@tonic-gate 	vap->va_gid = tp->tn_gid;
6950Sstevel@tonic-gate 	vap->va_fsid = tp->tn_fsid;
6960Sstevel@tonic-gate 	vap->va_nodeid = (ino64_t)tp->tn_nodeid;
6970Sstevel@tonic-gate 	vap->va_nlink = tp->tn_nlink;
6980Sstevel@tonic-gate 	vap->va_size = (u_offset_t)tp->tn_size;
6990Sstevel@tonic-gate 	vap->va_atime = tp->tn_atime;
7000Sstevel@tonic-gate 	vap->va_mtime = tp->tn_mtime;
7010Sstevel@tonic-gate 	vap->va_ctime = tp->tn_ctime;
7020Sstevel@tonic-gate 	vap->va_blksize = PAGESIZE;
7030Sstevel@tonic-gate 	vap->va_rdev = tp->tn_rdev;
7040Sstevel@tonic-gate 	vap->va_seq = tp->tn_seq;
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	/*
7070Sstevel@tonic-gate 	 * XXX Holes are not taken into account.  We could take the time to
7080Sstevel@tonic-gate 	 * run through the anon array looking for allocated slots...
7090Sstevel@tonic-gate 	 */
7100Sstevel@tonic-gate 	vap->va_nblocks = (fsblkcnt64_t)btodb(ptob(btopr(vap->va_size)));
7110Sstevel@tonic-gate 	mutex_exit(&tp->tn_tlock);
7120Sstevel@tonic-gate 	return (0);
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate /*ARGSUSED4*/
7160Sstevel@tonic-gate static int
tmp_setattr(struct vnode * vp,struct vattr * vap,int flags,struct cred * cred,caller_context_t * ct)7170Sstevel@tonic-gate tmp_setattr(
7180Sstevel@tonic-gate 	struct vnode *vp,
7190Sstevel@tonic-gate 	struct vattr *vap,
7200Sstevel@tonic-gate 	int flags,
7210Sstevel@tonic-gate 	struct cred *cred,
7220Sstevel@tonic-gate 	caller_context_t *ct)
7230Sstevel@tonic-gate {
7240Sstevel@tonic-gate 	struct tmount *tm = (struct tmount *)VTOTM(vp);
7250Sstevel@tonic-gate 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
7260Sstevel@tonic-gate 	int error = 0;
7270Sstevel@tonic-gate 	struct vattr *get;
7280Sstevel@tonic-gate 	long mask;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	/*
7310Sstevel@tonic-gate 	 * Cannot set these attributes
7320Sstevel@tonic-gate 	 */
7335331Samw 	if ((vap->va_mask & AT_NOSET) || (vap->va_mask & AT_XVATTR))
7340Sstevel@tonic-gate 		return (EINVAL);
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	mutex_enter(&tp->tn_tlock);
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	get = &tp->tn_attr;
7390Sstevel@tonic-gate 	/*
7400Sstevel@tonic-gate 	 * Change file access modes. Must be owner or have sufficient
7410Sstevel@tonic-gate 	 * privileges.
7420Sstevel@tonic-gate 	 */
7435928Sjj204856 	error = secpolicy_vnode_setattr(cred, vp, vap, get, flags, tmp_taccess,
7445928Sjj204856 	    tp);
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	if (error)
7470Sstevel@tonic-gate 		goto out;
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	mask = vap->va_mask;
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	if (mask & AT_MODE) {
7520Sstevel@tonic-gate 		get->va_mode &= S_IFMT;
7530Sstevel@tonic-gate 		get->va_mode |= vap->va_mode & ~S_IFMT;
7540Sstevel@tonic-gate 	}
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	if (mask & AT_UID)
7570Sstevel@tonic-gate 		get->va_uid = vap->va_uid;
7580Sstevel@tonic-gate 	if (mask & AT_GID)
7590Sstevel@tonic-gate 		get->va_gid = vap->va_gid;
7600Sstevel@tonic-gate 	if (mask & AT_ATIME)
7610Sstevel@tonic-gate 		get->va_atime = vap->va_atime;
7620Sstevel@tonic-gate 	if (mask & AT_MTIME)
7630Sstevel@tonic-gate 		get->va_mtime = vap->va_mtime;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	if (mask & (AT_UID | AT_GID | AT_MODE | AT_MTIME))
7660Sstevel@tonic-gate 		gethrestime(&tp->tn_ctime);
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	if (mask & AT_SIZE) {
7690Sstevel@tonic-gate 		ASSERT(vp->v_type != VDIR);
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 		/* Don't support large files. */
7720Sstevel@tonic-gate 		if (vap->va_size > MAXOFF_T) {
7730Sstevel@tonic-gate 			error = EFBIG;
7740Sstevel@tonic-gate 			goto out;
7750Sstevel@tonic-gate 		}
7760Sstevel@tonic-gate 		mutex_exit(&tp->tn_tlock);
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 		rw_enter(&tp->tn_rwlock, RW_WRITER);
7790Sstevel@tonic-gate 		rw_enter(&tp->tn_contents, RW_WRITER);
7800Sstevel@tonic-gate 		error = tmpnode_trunc(tm, tp, (ulong_t)vap->va_size);
7810Sstevel@tonic-gate 		rw_exit(&tp->tn_contents);
7820Sstevel@tonic-gate 		rw_exit(&tp->tn_rwlock);
7830Sstevel@tonic-gate 		goto out1;
7840Sstevel@tonic-gate 	}
7850Sstevel@tonic-gate out:
7860Sstevel@tonic-gate 	mutex_exit(&tp->tn_tlock);
7870Sstevel@tonic-gate out1:
7880Sstevel@tonic-gate 	return (error);
7890Sstevel@tonic-gate }
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate /* ARGSUSED2 */
7920Sstevel@tonic-gate static int
tmp_access(struct vnode * vp,int mode,int flags,struct cred * cred,caller_context_t * ct)7935331Samw tmp_access(
7945331Samw 	struct vnode *vp,
7955331Samw 	int mode,
7965331Samw 	int flags,
7975331Samw 	struct cred *cred,
7985331Samw 	caller_context_t *ct)
7990Sstevel@tonic-gate {
8000Sstevel@tonic-gate 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
8010Sstevel@tonic-gate 	int error;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	mutex_enter(&tp->tn_tlock);
8040Sstevel@tonic-gate 	error = tmp_taccess(tp, mode, cred);
8050Sstevel@tonic-gate 	mutex_exit(&tp->tn_tlock);
8060Sstevel@tonic-gate 	return (error);
8070Sstevel@tonic-gate }
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate /* ARGSUSED3 */
8100Sstevel@tonic-gate static int
tmp_lookup(struct vnode * dvp,char * nm,struct vnode ** vpp,struct pathname * pnp,int flags,struct vnode * rdir,struct cred * cred,caller_context_t * ct,int * direntflags,pathname_t * realpnp)8110Sstevel@tonic-gate tmp_lookup(
8120Sstevel@tonic-gate 	struct vnode *dvp,
8130Sstevel@tonic-gate 	char *nm,
8140Sstevel@tonic-gate 	struct vnode **vpp,
8150Sstevel@tonic-gate 	struct pathname *pnp,
8160Sstevel@tonic-gate 	int flags,
8170Sstevel@tonic-gate 	struct vnode *rdir,
8185331Samw 	struct cred *cred,
8195331Samw 	caller_context_t *ct,
8205331Samw 	int *direntflags,
8215331Samw 	pathname_t *realpnp)
8220Sstevel@tonic-gate {
8230Sstevel@tonic-gate 	struct tmpnode *tp = (struct tmpnode *)VTOTN(dvp);
8240Sstevel@tonic-gate 	struct tmpnode *ntp = NULL;
8250Sstevel@tonic-gate 	int error;
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	/* allow cd into @ dir */
8290Sstevel@tonic-gate 	if (flags & LOOKUP_XATTR) {
8300Sstevel@tonic-gate 		struct tmpnode *xdp;
8310Sstevel@tonic-gate 		struct tmount *tm;
8320Sstevel@tonic-gate 
8335331Samw 		/*
8345331Samw 		 * don't allow attributes if not mounted XATTR support
8355331Samw 		 */
8365331Samw 		if (!(dvp->v_vfsp->vfs_flag & VFS_XATTR))
8375331Samw 			return (EINVAL);
8385331Samw 
8390Sstevel@tonic-gate 		if (tp->tn_flags & ISXATTR)
8400Sstevel@tonic-gate 			/* No attributes on attributes */
8410Sstevel@tonic-gate 			return (EINVAL);
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 		rw_enter(&tp->tn_rwlock, RW_WRITER);
8440Sstevel@tonic-gate 		if (tp->tn_xattrdp == NULL) {
8450Sstevel@tonic-gate 			if (!(flags & CREATE_XATTR_DIR)) {
8460Sstevel@tonic-gate 				rw_exit(&tp->tn_rwlock);
8470Sstevel@tonic-gate 				return (ENOENT);
8480Sstevel@tonic-gate 			}
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 			/*
8510Sstevel@tonic-gate 			 * No attribute directory exists for this
8520Sstevel@tonic-gate 			 * node - create the attr dir as a side effect
8530Sstevel@tonic-gate 			 * of this lookup.
8540Sstevel@tonic-gate 			 */
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 			/*
8570Sstevel@tonic-gate 			 * Make sure we have adequate permission...
8580Sstevel@tonic-gate 			 */
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 			if ((error = tmp_taccess(tp, VWRITE, cred)) != 0) {
8610Sstevel@tonic-gate 				rw_exit(&tp->tn_rwlock);
8620Sstevel@tonic-gate 				return (error);
8630Sstevel@tonic-gate 			}
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 			xdp = tmp_memalloc(sizeof (struct tmpnode),
8665928Sjj204856 			    TMP_MUSTHAVE);
8670Sstevel@tonic-gate 			tm = VTOTM(dvp);
8680Sstevel@tonic-gate 			tmpnode_init(tm, xdp, &tp->tn_attr, NULL);
8690Sstevel@tonic-gate 			/*
8700Sstevel@tonic-gate 			 * Fix-up fields unique to attribute directories.
8710Sstevel@tonic-gate 			 */
8720Sstevel@tonic-gate 			xdp->tn_flags = ISXATTR;
8730Sstevel@tonic-gate 			xdp->tn_type = VDIR;
8740Sstevel@tonic-gate 			if (tp->tn_type == VDIR) {
8750Sstevel@tonic-gate 				xdp->tn_mode = tp->tn_attr.va_mode;
8760Sstevel@tonic-gate 			} else {
8770Sstevel@tonic-gate 				xdp->tn_mode = 0700;
8780Sstevel@tonic-gate 				if (tp->tn_attr.va_mode & 0040)
8790Sstevel@tonic-gate 					xdp->tn_mode |= 0750;
8800Sstevel@tonic-gate 				if (tp->tn_attr.va_mode & 0004)
8810Sstevel@tonic-gate 					xdp->tn_mode |= 0705;
8820Sstevel@tonic-gate 			}
8830Sstevel@tonic-gate 			xdp->tn_vnode->v_type = VDIR;
8840Sstevel@tonic-gate 			xdp->tn_vnode->v_flag |= V_XATTRDIR;
8850Sstevel@tonic-gate 			tdirinit(tp, xdp);
8860Sstevel@tonic-gate 			tp->tn_xattrdp = xdp;
8870Sstevel@tonic-gate 		} else {
8880Sstevel@tonic-gate 			VN_HOLD(tp->tn_xattrdp->tn_vnode);
8890Sstevel@tonic-gate 		}
8900Sstevel@tonic-gate 		*vpp = TNTOV(tp->tn_xattrdp);
8910Sstevel@tonic-gate 		rw_exit(&tp->tn_rwlock);
8920Sstevel@tonic-gate 		return (0);
8930Sstevel@tonic-gate 	}
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	/*
8960Sstevel@tonic-gate 	 * Null component name is a synonym for directory being searched.
8970Sstevel@tonic-gate 	 */
8980Sstevel@tonic-gate 	if (*nm == '\0') {
8990Sstevel@tonic-gate 		VN_HOLD(dvp);
9000Sstevel@tonic-gate 		*vpp = dvp;
9010Sstevel@tonic-gate 		return (0);
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate 	ASSERT(tp);
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	error = tdirlookup(tp, nm, &ntp, cred);
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	if (error == 0) {
9080Sstevel@tonic-gate 		ASSERT(ntp);
9090Sstevel@tonic-gate 		*vpp = TNTOV(ntp);
9100Sstevel@tonic-gate 		/*
9110Sstevel@tonic-gate 		 * If vnode is a device return special vnode instead
9120Sstevel@tonic-gate 		 */
9130Sstevel@tonic-gate 		if (IS_DEVVP(*vpp)) {
9140Sstevel@tonic-gate 			struct vnode *newvp;
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 			newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type,
9170Sstevel@tonic-gate 			    cred);
9180Sstevel@tonic-gate 			VN_RELE(*vpp);
9190Sstevel@tonic-gate 			*vpp = newvp;
9200Sstevel@tonic-gate 		}
9210Sstevel@tonic-gate 	}
9220Sstevel@tonic-gate 	TRACE_4(TR_FAC_TMPFS, TR_TMPFS_LOOKUP,
9230Sstevel@tonic-gate 	    "tmpfs lookup:vp %p name %s vpp %p error %d",
9240Sstevel@tonic-gate 	    dvp, nm, vpp, error);
9250Sstevel@tonic-gate 	return (error);
9260Sstevel@tonic-gate }
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate /*ARGSUSED7*/
9290Sstevel@tonic-gate static int
tmp_create(struct vnode * dvp,char * nm,struct vattr * vap,enum vcexcl exclusive,int mode,struct vnode ** vpp,struct cred * cred,int flag,caller_context_t * ct,vsecattr_t * vsecp)9300Sstevel@tonic-gate tmp_create(
9310Sstevel@tonic-gate 	struct vnode *dvp,
9320Sstevel@tonic-gate 	char *nm,
9330Sstevel@tonic-gate 	struct vattr *vap,
9340Sstevel@tonic-gate 	enum vcexcl exclusive,
9350Sstevel@tonic-gate 	int mode,
9360Sstevel@tonic-gate 	struct vnode **vpp,
9370Sstevel@tonic-gate 	struct cred *cred,
9385331Samw 	int flag,
9395331Samw 	caller_context_t *ct,
9405331Samw 	vsecattr_t *vsecp)
9410Sstevel@tonic-gate {
9420Sstevel@tonic-gate 	struct tmpnode *parent;
9430Sstevel@tonic-gate 	struct tmount *tm;
9440Sstevel@tonic-gate 	struct tmpnode *self;
9450Sstevel@tonic-gate 	int error;
9460Sstevel@tonic-gate 	struct tmpnode *oldtp;
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate again:
9490Sstevel@tonic-gate 	parent = (struct tmpnode *)VTOTN(dvp);
9500Sstevel@tonic-gate 	tm = (struct tmount *)VTOTM(dvp);
9510Sstevel@tonic-gate 	self = NULL;
9520Sstevel@tonic-gate 	error = 0;
9530Sstevel@tonic-gate 	oldtp = NULL;
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	/* device files not allowed in ext. attr dirs */
9560Sstevel@tonic-gate 	if ((parent->tn_flags & ISXATTR) &&
9575928Sjj204856 	    (vap->va_type == VBLK || vap->va_type == VCHR ||
9585928Sjj204856 	    vap->va_type == VFIFO || vap->va_type == VDOOR ||
9595928Sjj204856 	    vap->va_type == VSOCK || vap->va_type == VPORT))
9600Sstevel@tonic-gate 			return (EINVAL);
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	if (vap->va_type == VREG && (vap->va_mode & VSVTX)) {
9630Sstevel@tonic-gate 		/* Must be privileged to set sticky bit */
9640Sstevel@tonic-gate 		if (secpolicy_vnode_stky_modify(cred))
9650Sstevel@tonic-gate 			vap->va_mode &= ~VSVTX;
9660Sstevel@tonic-gate 	} else if (vap->va_type == VNON) {
9670Sstevel@tonic-gate 		return (EINVAL);
9680Sstevel@tonic-gate 	}
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	/*
9710Sstevel@tonic-gate 	 * Null component name is a synonym for directory being searched.
9720Sstevel@tonic-gate 	 */
9730Sstevel@tonic-gate 	if (*nm == '\0') {
9740Sstevel@tonic-gate 		VN_HOLD(dvp);
9750Sstevel@tonic-gate 		oldtp = parent;
9760Sstevel@tonic-gate 	} else {
9770Sstevel@tonic-gate 		error = tdirlookup(parent, nm, &oldtp, cred);
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	if (error == 0) {	/* name found */
9810Sstevel@tonic-gate 		ASSERT(oldtp);
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 		rw_enter(&oldtp->tn_rwlock, RW_WRITER);
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 		/*
9860Sstevel@tonic-gate 		 * if create/read-only an existing
9870Sstevel@tonic-gate 		 * directory, allow it
9880Sstevel@tonic-gate 		 */
9890Sstevel@tonic-gate 		if (exclusive == EXCL)
9900Sstevel@tonic-gate 			error = EEXIST;
9910Sstevel@tonic-gate 		else if ((oldtp->tn_type == VDIR) && (mode & VWRITE))
9920Sstevel@tonic-gate 			error = EISDIR;
9930Sstevel@tonic-gate 		else {
9940Sstevel@tonic-gate 			error = tmp_taccess(oldtp, mode, cred);
9950Sstevel@tonic-gate 		}
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 		if (error) {
9980Sstevel@tonic-gate 			rw_exit(&oldtp->tn_rwlock);
9990Sstevel@tonic-gate 			tmpnode_rele(oldtp);
10000Sstevel@tonic-gate 			return (error);
10010Sstevel@tonic-gate 		}
10020Sstevel@tonic-gate 		*vpp = TNTOV(oldtp);
10030Sstevel@tonic-gate 		if ((*vpp)->v_type == VREG && (vap->va_mask & AT_SIZE) &&
10040Sstevel@tonic-gate 		    vap->va_size == 0) {
10050Sstevel@tonic-gate 			rw_enter(&oldtp->tn_contents, RW_WRITER);
10060Sstevel@tonic-gate 			(void) tmpnode_trunc(tm, oldtp, 0);
10070Sstevel@tonic-gate 			rw_exit(&oldtp->tn_contents);
10080Sstevel@tonic-gate 		}
10090Sstevel@tonic-gate 		rw_exit(&oldtp->tn_rwlock);
10100Sstevel@tonic-gate 		if (IS_DEVVP(*vpp)) {
10110Sstevel@tonic-gate 			struct vnode *newvp;
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 			newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type,
10140Sstevel@tonic-gate 			    cred);
10150Sstevel@tonic-gate 			VN_RELE(*vpp);
10160Sstevel@tonic-gate 			if (newvp == NULL) {
10170Sstevel@tonic-gate 				return (ENOSYS);
10180Sstevel@tonic-gate 			}
10190Sstevel@tonic-gate 			*vpp = newvp;
10200Sstevel@tonic-gate 		}
10214863Spraks 
10224863Spraks 		if (error == 0) {
10235331Samw 			vnevent_create(*vpp, ct);
10244863Spraks 		}
10250Sstevel@tonic-gate 		return (0);
10260Sstevel@tonic-gate 	}
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	if (error != ENOENT)
10290Sstevel@tonic-gate 		return (error);
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 	rw_enter(&parent->tn_rwlock, RW_WRITER);
10320Sstevel@tonic-gate 	error = tdirenter(tm, parent, nm, DE_CREATE,
10330Sstevel@tonic-gate 	    (struct tmpnode *)NULL, (struct tmpnode *)NULL,
10345331Samw 	    vap, &self, cred, ct);
10350Sstevel@tonic-gate 	rw_exit(&parent->tn_rwlock);
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	if (error) {
10380Sstevel@tonic-gate 		if (self)
10390Sstevel@tonic-gate 			tmpnode_rele(self);
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 		if (error == EEXIST) {
10420Sstevel@tonic-gate 			/*
10430Sstevel@tonic-gate 			 * This means that the file was created sometime
10440Sstevel@tonic-gate 			 * after we checked and did not find it and when
10450Sstevel@tonic-gate 			 * we went to create it.
10460Sstevel@tonic-gate 			 * Since creat() is supposed to truncate a file
10470Sstevel@tonic-gate 			 * that already exits go back to the begining
10480Sstevel@tonic-gate 			 * of the function. This time we will find it
10490Sstevel@tonic-gate 			 * and go down the tmp_trunc() path
10500Sstevel@tonic-gate 			 */
10510Sstevel@tonic-gate 			goto again;
10520Sstevel@tonic-gate 		}
10530Sstevel@tonic-gate 		return (error);
10540Sstevel@tonic-gate 	}
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	*vpp = TNTOV(self);
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	if (!error && IS_DEVVP(*vpp)) {
10590Sstevel@tonic-gate 		struct vnode *newvp;
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 		newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cred);
10620Sstevel@tonic-gate 		VN_RELE(*vpp);
10630Sstevel@tonic-gate 		if (newvp == NULL)
10640Sstevel@tonic-gate 			return (ENOSYS);
10650Sstevel@tonic-gate 		*vpp = newvp;
10660Sstevel@tonic-gate 	}
10670Sstevel@tonic-gate 	TRACE_3(TR_FAC_TMPFS, TR_TMPFS_CREATE,
10685928Sjj204856 	    "tmpfs create:dvp %p nm %s vpp %p", dvp, nm, vpp);
10690Sstevel@tonic-gate 	return (0);
10700Sstevel@tonic-gate }
10710Sstevel@tonic-gate 
10725331Samw /* ARGSUSED3 */
10730Sstevel@tonic-gate static int
tmp_remove(struct vnode * dvp,char * nm,struct cred * cred,caller_context_t * ct,int flags)10745331Samw tmp_remove(
10755331Samw 	struct vnode *dvp,
10765331Samw 	char *nm,
10775331Samw 	struct cred *cred,
10785331Samw 	caller_context_t *ct,
10795331Samw 	int flags)
10800Sstevel@tonic-gate {
10810Sstevel@tonic-gate 	struct tmpnode *parent = (struct tmpnode *)VTOTN(dvp);
10820Sstevel@tonic-gate 	int error;
10830Sstevel@tonic-gate 	struct tmpnode *tp = NULL;
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	error = tdirlookup(parent, nm, &tp, cred);
10860Sstevel@tonic-gate 	if (error)
10870Sstevel@tonic-gate 		return (error);
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 	ASSERT(tp);
10900Sstevel@tonic-gate 	rw_enter(&parent->tn_rwlock, RW_WRITER);
10910Sstevel@tonic-gate 	rw_enter(&tp->tn_rwlock, RW_WRITER);
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	if (tp->tn_type != VDIR ||
10940Sstevel@tonic-gate 	    (error = secpolicy_fs_linkdir(cred, dvp->v_vfsp)) == 0)
10950Sstevel@tonic-gate 		error = tdirdelete(parent, tp, nm, DR_REMOVE, cred);
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	rw_exit(&tp->tn_rwlock);
10980Sstevel@tonic-gate 	rw_exit(&parent->tn_rwlock);
10995331Samw 	vnevent_remove(TNTOV(tp), dvp, nm, ct);
11000Sstevel@tonic-gate 	tmpnode_rele(tp);
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	TRACE_3(TR_FAC_TMPFS, TR_TMPFS_REMOVE,
11035928Sjj204856 	    "tmpfs remove:dvp %p nm %s error %d", dvp, nm, error);
11040Sstevel@tonic-gate 	return (error);
11050Sstevel@tonic-gate }
11060Sstevel@tonic-gate 
11075331Samw /* ARGSUSED4 */
11080Sstevel@tonic-gate static int
tmp_link(struct vnode * dvp,struct vnode * srcvp,char * tnm,struct cred * cred,caller_context_t * ct,int flags)11095331Samw tmp_link(
11105331Samw 	struct vnode *dvp,
11115331Samw 	struct vnode *srcvp,
11125331Samw 	char *tnm,
11135331Samw 	struct cred *cred,
11145331Samw 	caller_context_t *ct,
11155331Samw 	int flags)
11160Sstevel@tonic-gate {
11170Sstevel@tonic-gate 	struct tmpnode *parent;
11180Sstevel@tonic-gate 	struct tmpnode *from;
11190Sstevel@tonic-gate 	struct tmount *tm = (struct tmount *)VTOTM(dvp);
11200Sstevel@tonic-gate 	int error;
11210Sstevel@tonic-gate 	struct tmpnode *found = NULL;
11220Sstevel@tonic-gate 	struct vnode *realvp;
11230Sstevel@tonic-gate 
11245331Samw 	if (VOP_REALVP(srcvp, &realvp, ct) == 0)
11250Sstevel@tonic-gate 		srcvp = realvp;
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	parent = (struct tmpnode *)VTOTN(dvp);
11280Sstevel@tonic-gate 	from = (struct tmpnode *)VTOTN(srcvp);
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 	if ((srcvp->v_type == VDIR &&
11310Sstevel@tonic-gate 	    secpolicy_fs_linkdir(cred, dvp->v_vfsp)) ||
11320Sstevel@tonic-gate 	    (from->tn_uid != crgetuid(cred) && secpolicy_basic_link(cred)))
11330Sstevel@tonic-gate 		return (EPERM);
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	/*
11360Sstevel@tonic-gate 	 * Make sure link for extended attributes is valid
11370Sstevel@tonic-gate 	 * We only support hard linking of xattr's in xattrdir to an xattrdir
11380Sstevel@tonic-gate 	 */
11390Sstevel@tonic-gate 	if ((from->tn_flags & ISXATTR) != (parent->tn_flags & ISXATTR))
11400Sstevel@tonic-gate 		return (EINVAL);
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	error = tdirlookup(parent, tnm, &found, cred);
11430Sstevel@tonic-gate 	if (error == 0) {
11440Sstevel@tonic-gate 		ASSERT(found);
11450Sstevel@tonic-gate 		tmpnode_rele(found);
11460Sstevel@tonic-gate 		return (EEXIST);
11470Sstevel@tonic-gate 	}
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	if (error != ENOENT)
11500Sstevel@tonic-gate 		return (error);
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	rw_enter(&parent->tn_rwlock, RW_WRITER);
11530Sstevel@tonic-gate 	error = tdirenter(tm, parent, tnm, DE_LINK, (struct tmpnode *)NULL,
11545928Sjj204856 	    from, NULL, (struct tmpnode **)NULL, cred, ct);
11550Sstevel@tonic-gate 	rw_exit(&parent->tn_rwlock);
11564863Spraks 	if (error == 0) {
11575331Samw 		vnevent_link(srcvp, ct);
11584863Spraks 	}
11590Sstevel@tonic-gate 	return (error);
11600Sstevel@tonic-gate }
11610Sstevel@tonic-gate 
11625331Samw /* ARGSUSED5 */
11630Sstevel@tonic-gate static int
tmp_rename(struct vnode * odvp,char * onm,struct vnode * ndvp,char * nnm,struct cred * cred,caller_context_t * ct,int flags)11640Sstevel@tonic-gate tmp_rename(
11650Sstevel@tonic-gate 	struct vnode *odvp,	/* source parent vnode */
11660Sstevel@tonic-gate 	char *onm,		/* source name */
11670Sstevel@tonic-gate 	struct vnode *ndvp,	/* destination parent vnode */
11680Sstevel@tonic-gate 	char *nnm,		/* destination name */
11695331Samw 	struct cred *cred,
11705331Samw 	caller_context_t *ct,
11715331Samw 	int flags)
11720Sstevel@tonic-gate {
11730Sstevel@tonic-gate 	struct tmpnode *fromparent;
11740Sstevel@tonic-gate 	struct tmpnode *toparent;
11750Sstevel@tonic-gate 	struct tmpnode *fromtp = NULL;	/* source tmpnode */
11760Sstevel@tonic-gate 	struct tmount *tm = (struct tmount *)VTOTM(odvp);
11770Sstevel@tonic-gate 	int error;
11780Sstevel@tonic-gate 	int samedir = 0;	/* set if odvp == ndvp */
11790Sstevel@tonic-gate 	struct vnode *realvp;
11800Sstevel@tonic-gate 
11815331Samw 	if (VOP_REALVP(ndvp, &realvp, ct) == 0)
11820Sstevel@tonic-gate 		ndvp = realvp;
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	fromparent = (struct tmpnode *)VTOTN(odvp);
11850Sstevel@tonic-gate 	toparent = (struct tmpnode *)VTOTN(ndvp);
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	if ((fromparent->tn_flags & ISXATTR) != (toparent->tn_flags & ISXATTR))
11880Sstevel@tonic-gate 		return (EINVAL);
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	mutex_enter(&tm->tm_renamelck);
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	/*
11930Sstevel@tonic-gate 	 * Look up tmpnode of file we're supposed to rename.
11940Sstevel@tonic-gate 	 */
11950Sstevel@tonic-gate 	error = tdirlookup(fromparent, onm, &fromtp, cred);
11960Sstevel@tonic-gate 	if (error) {
11970Sstevel@tonic-gate 		mutex_exit(&tm->tm_renamelck);
11980Sstevel@tonic-gate 		return (error);
11990Sstevel@tonic-gate 	}
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	/*
12020Sstevel@tonic-gate 	 * Make sure we can delete the old (source) entry.  This
12030Sstevel@tonic-gate 	 * requires write permission on the containing directory.  If
12040Sstevel@tonic-gate 	 * that directory is "sticky" it requires further checks.
12050Sstevel@tonic-gate 	 */
12060Sstevel@tonic-gate 	if (((error = tmp_taccess(fromparent, VWRITE, cred)) != 0) ||
12070Sstevel@tonic-gate 	    (error = tmp_sticky_remove_access(fromparent, fromtp, cred)) != 0)
12080Sstevel@tonic-gate 		goto done;
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	/*
12110Sstevel@tonic-gate 	 * Check for renaming to or from '.' or '..' or that
12120Sstevel@tonic-gate 	 * fromtp == fromparent
12130Sstevel@tonic-gate 	 */
12140Sstevel@tonic-gate 	if ((onm[0] == '.' &&
12150Sstevel@tonic-gate 	    (onm[1] == '\0' || (onm[1] == '.' && onm[2] == '\0'))) ||
12160Sstevel@tonic-gate 	    (nnm[0] == '.' &&
12170Sstevel@tonic-gate 	    (nnm[1] == '\0' || (nnm[1] == '.' && nnm[2] == '\0'))) ||
12180Sstevel@tonic-gate 	    (fromparent == fromtp)) {
12190Sstevel@tonic-gate 		error = EINVAL;
12200Sstevel@tonic-gate 		goto done;
12210Sstevel@tonic-gate 	}
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	samedir = (fromparent == toparent);
12240Sstevel@tonic-gate 	/*
12250Sstevel@tonic-gate 	 * Make sure we can search and rename into the new
12260Sstevel@tonic-gate 	 * (destination) directory.
12270Sstevel@tonic-gate 	 */
12280Sstevel@tonic-gate 	if (!samedir) {
12290Sstevel@tonic-gate 		error = tmp_taccess(toparent, VEXEC|VWRITE, cred);
12300Sstevel@tonic-gate 		if (error)
12310Sstevel@tonic-gate 			goto done;
12320Sstevel@tonic-gate 	}
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	/*
12350Sstevel@tonic-gate 	 * Link source to new target
12360Sstevel@tonic-gate 	 */
12370Sstevel@tonic-gate 	rw_enter(&toparent->tn_rwlock, RW_WRITER);
12380Sstevel@tonic-gate 	error = tdirenter(tm, toparent, nnm, DE_RENAME,
12390Sstevel@tonic-gate 	    fromparent, fromtp, (struct vattr *)NULL,
12405331Samw 	    (struct tmpnode **)NULL, cred, ct);
12410Sstevel@tonic-gate 	rw_exit(&toparent->tn_rwlock);
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	if (error) {
12440Sstevel@tonic-gate 		/*
12450Sstevel@tonic-gate 		 * ESAME isn't really an error; it indicates that the
12460Sstevel@tonic-gate 		 * operation should not be done because the source and target
12470Sstevel@tonic-gate 		 * are the same file, but that no error should be reported.
12480Sstevel@tonic-gate 		 */
12490Sstevel@tonic-gate 		if (error == ESAME)
12500Sstevel@tonic-gate 			error = 0;
12510Sstevel@tonic-gate 		goto done;
12520Sstevel@tonic-gate 	}
12535331Samw 	vnevent_rename_src(TNTOV(fromtp), odvp, onm, ct);
12544863Spraks 
12554863Spraks 	/*
12564863Spraks 	 * Notify the target directory if not same as
12574863Spraks 	 * source directory.
12584863Spraks 	 */
12594863Spraks 	if (ndvp != odvp) {
12605331Samw 		vnevent_rename_dest_dir(ndvp, ct);
12614863Spraks 	}
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 	/*
12640Sstevel@tonic-gate 	 * Unlink from source.
12650Sstevel@tonic-gate 	 */
12660Sstevel@tonic-gate 	rw_enter(&fromparent->tn_rwlock, RW_WRITER);
12670Sstevel@tonic-gate 	rw_enter(&fromtp->tn_rwlock, RW_WRITER);
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 	error = tdirdelete(fromparent, fromtp, onm, DR_RENAME, cred);
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	/*
12720Sstevel@tonic-gate 	 * The following handles the case where our source tmpnode was
12730Sstevel@tonic-gate 	 * removed before we got to it.
12740Sstevel@tonic-gate 	 *
12750Sstevel@tonic-gate 	 * XXX We should also cleanup properly in the case where tdirdelete
12760Sstevel@tonic-gate 	 * fails for some other reason.  Currently this case shouldn't happen.
12770Sstevel@tonic-gate 	 * (see 1184991).
12780Sstevel@tonic-gate 	 */
12790Sstevel@tonic-gate 	if (error == ENOENT)
12800Sstevel@tonic-gate 		error = 0;
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	rw_exit(&fromtp->tn_rwlock);
12830Sstevel@tonic-gate 	rw_exit(&fromparent->tn_rwlock);
12840Sstevel@tonic-gate done:
12850Sstevel@tonic-gate 	tmpnode_rele(fromtp);
12860Sstevel@tonic-gate 	mutex_exit(&tm->tm_renamelck);
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	TRACE_5(TR_FAC_TMPFS, TR_TMPFS_RENAME,
12895928Sjj204856 	    "tmpfs rename:ovp %p onm %s nvp %p nnm %s error %d", odvp, onm,
12905928Sjj204856 	    ndvp, nnm, error);
12910Sstevel@tonic-gate 	return (error);
12920Sstevel@tonic-gate }
12930Sstevel@tonic-gate 
12945331Samw /* ARGSUSED5 */
12950Sstevel@tonic-gate static int
tmp_mkdir(struct vnode * dvp,char * nm,struct vattr * va,struct vnode ** vpp,struct cred * cred,caller_context_t * ct,int flags,vsecattr_t * vsecp)12960Sstevel@tonic-gate tmp_mkdir(
12970Sstevel@tonic-gate 	struct vnode *dvp,
12980Sstevel@tonic-gate 	char *nm,
12990Sstevel@tonic-gate 	struct vattr *va,
13000Sstevel@tonic-gate 	struct vnode **vpp,
13015331Samw 	struct cred *cred,
13025331Samw 	caller_context_t *ct,
13035331Samw 	int flags,
13045331Samw 	vsecattr_t *vsecp)
13050Sstevel@tonic-gate {
13060Sstevel@tonic-gate 	struct tmpnode *parent = (struct tmpnode *)VTOTN(dvp);
13070Sstevel@tonic-gate 	struct tmpnode *self = NULL;
13080Sstevel@tonic-gate 	struct tmount *tm = (struct tmount *)VTOTM(dvp);
13090Sstevel@tonic-gate 	int error;
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	/* no new dirs allowed in xattr dirs */
13120Sstevel@tonic-gate 	if (parent->tn_flags & ISXATTR)
13130Sstevel@tonic-gate 		return (EINVAL);
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	/*
13160Sstevel@tonic-gate 	 * Might be dangling directory.  Catch it here,
13170Sstevel@tonic-gate 	 * because a ENOENT return from tdirlookup() is
13180Sstevel@tonic-gate 	 * an "o.k. return".
13190Sstevel@tonic-gate 	 */
13200Sstevel@tonic-gate 	if (parent->tn_nlink == 0)
13210Sstevel@tonic-gate 		return (ENOENT);
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	error = tdirlookup(parent, nm, &self, cred);
13240Sstevel@tonic-gate 	if (error == 0) {
13250Sstevel@tonic-gate 		ASSERT(self);
13260Sstevel@tonic-gate 		tmpnode_rele(self);
13270Sstevel@tonic-gate 		return (EEXIST);
13280Sstevel@tonic-gate 	}
13290Sstevel@tonic-gate 	if (error != ENOENT)
13300Sstevel@tonic-gate 		return (error);
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	rw_enter(&parent->tn_rwlock, RW_WRITER);
13335928Sjj204856 	error = tdirenter(tm, parent, nm, DE_MKDIR, (struct tmpnode *)NULL,
13345928Sjj204856 	    (struct tmpnode *)NULL, va, &self, cred, ct);
13350Sstevel@tonic-gate 	if (error) {
13360Sstevel@tonic-gate 		rw_exit(&parent->tn_rwlock);
13370Sstevel@tonic-gate 		if (self)
13380Sstevel@tonic-gate 			tmpnode_rele(self);
13390Sstevel@tonic-gate 		return (error);
13400Sstevel@tonic-gate 	}
13410Sstevel@tonic-gate 	rw_exit(&parent->tn_rwlock);
13420Sstevel@tonic-gate 	*vpp = TNTOV(self);
13430Sstevel@tonic-gate 	return (0);
13440Sstevel@tonic-gate }
13450Sstevel@tonic-gate 
13465331Samw /* ARGSUSED4 */
13470Sstevel@tonic-gate static int
tmp_rmdir(struct vnode * dvp,char * nm,struct vnode * cdir,struct cred * cred,caller_context_t * ct,int flags)13480Sstevel@tonic-gate tmp_rmdir(
13490Sstevel@tonic-gate 	struct vnode *dvp,
13500Sstevel@tonic-gate 	char *nm,
13510Sstevel@tonic-gate 	struct vnode *cdir,
13525331Samw 	struct cred *cred,
13535331Samw 	caller_context_t *ct,
13545331Samw 	int flags)
13550Sstevel@tonic-gate {
13560Sstevel@tonic-gate 	struct tmpnode *parent = (struct tmpnode *)VTOTN(dvp);
13570Sstevel@tonic-gate 	struct tmpnode *self = NULL;
13580Sstevel@tonic-gate 	struct vnode *vp;
13590Sstevel@tonic-gate 	int error = 0;
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 	/*
13620Sstevel@tonic-gate 	 * Return error when removing . and ..
13630Sstevel@tonic-gate 	 */
13640Sstevel@tonic-gate 	if (strcmp(nm, ".") == 0)
13650Sstevel@tonic-gate 		return (EINVAL);
13660Sstevel@tonic-gate 	if (strcmp(nm, "..") == 0)
13670Sstevel@tonic-gate 		return (EEXIST); /* Should be ENOTEMPTY */
13680Sstevel@tonic-gate 	error = tdirlookup(parent, nm, &self, cred);
13690Sstevel@tonic-gate 	if (error)
13700Sstevel@tonic-gate 		return (error);
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	rw_enter(&parent->tn_rwlock, RW_WRITER);
13730Sstevel@tonic-gate 	rw_enter(&self->tn_rwlock, RW_WRITER);
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	vp = TNTOV(self);
13760Sstevel@tonic-gate 	if (vp == dvp || vp == cdir) {
13770Sstevel@tonic-gate 		error = EINVAL;
13780Sstevel@tonic-gate 		goto done1;
13790Sstevel@tonic-gate 	}
13800Sstevel@tonic-gate 	if (self->tn_type != VDIR) {
13810Sstevel@tonic-gate 		error = ENOTDIR;
13820Sstevel@tonic-gate 		goto done1;
13830Sstevel@tonic-gate 	}
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 	mutex_enter(&self->tn_tlock);
13860Sstevel@tonic-gate 	if (self->tn_nlink > 2) {
13870Sstevel@tonic-gate 		mutex_exit(&self->tn_tlock);
13880Sstevel@tonic-gate 		error = EEXIST;
13890Sstevel@tonic-gate 		goto done1;
13900Sstevel@tonic-gate 	}
13910Sstevel@tonic-gate 	mutex_exit(&self->tn_tlock);
13920Sstevel@tonic-gate 
1393569Sbatschul 	if (vn_vfswlock(vp)) {
13940Sstevel@tonic-gate 		error = EBUSY;
13950Sstevel@tonic-gate 		goto done1;
13960Sstevel@tonic-gate 	}
13970Sstevel@tonic-gate 	if (vn_mountedvfs(vp) != NULL) {
13980Sstevel@tonic-gate 		error = EBUSY;
13990Sstevel@tonic-gate 		goto done;
14000Sstevel@tonic-gate 	}
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 	/*
14030Sstevel@tonic-gate 	 * Check for an empty directory
14040Sstevel@tonic-gate 	 * i.e. only includes entries for "." and ".."
14050Sstevel@tonic-gate 	 */
14060Sstevel@tonic-gate 	if (self->tn_dirents > 2) {
14070Sstevel@tonic-gate 		error = EEXIST;		/* SIGH should be ENOTEMPTY */
14080Sstevel@tonic-gate 		/*
14090Sstevel@tonic-gate 		 * Update atime because checking tn_dirents is logically
14100Sstevel@tonic-gate 		 * equivalent to reading the directory
14110Sstevel@tonic-gate 		 */
14120Sstevel@tonic-gate 		gethrestime(&self->tn_atime);
14130Sstevel@tonic-gate 		goto done;
14140Sstevel@tonic-gate 	}
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	error = tdirdelete(parent, self, nm, DR_RMDIR, cred);
14170Sstevel@tonic-gate done:
14180Sstevel@tonic-gate 	vn_vfsunlock(vp);
14190Sstevel@tonic-gate done1:
14200Sstevel@tonic-gate 	rw_exit(&self->tn_rwlock);
14210Sstevel@tonic-gate 	rw_exit(&parent->tn_rwlock);
14225331Samw 	vnevent_rmdir(TNTOV(self), dvp, nm, ct);
14230Sstevel@tonic-gate 	tmpnode_rele(self);
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 	return (error);
14260Sstevel@tonic-gate }
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate /* ARGSUSED2 */
14290Sstevel@tonic-gate static int
tmp_readdir(struct vnode * vp,struct uio * uiop,struct cred * cred,int * eofp,caller_context_t * ct,int flags)14305331Samw tmp_readdir(
14315331Samw 	struct vnode *vp,
14325331Samw 	struct uio *uiop,
14335331Samw 	struct cred *cred,
14345331Samw 	int *eofp,
14355331Samw 	caller_context_t *ct,
14365331Samw 	int flags)
14370Sstevel@tonic-gate {
14380Sstevel@tonic-gate 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
14390Sstevel@tonic-gate 	struct tdirent *tdp;
14400Sstevel@tonic-gate 	int error = 0;
14410Sstevel@tonic-gate 	size_t namelen;
14420Sstevel@tonic-gate 	struct dirent64 *dp;
14430Sstevel@tonic-gate 	ulong_t offset;
14440Sstevel@tonic-gate 	ulong_t total_bytes_wanted;
14450Sstevel@tonic-gate 	long outcount = 0;
14460Sstevel@tonic-gate 	long bufsize;
14470Sstevel@tonic-gate 	int reclen;
14480Sstevel@tonic-gate 	caddr_t outbuf;
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 	if (uiop->uio_loffset >= MAXOFF_T) {
14510Sstevel@tonic-gate 		if (eofp)
14520Sstevel@tonic-gate 			*eofp = 1;
14530Sstevel@tonic-gate 		return (0);
14540Sstevel@tonic-gate 	}
14550Sstevel@tonic-gate 	/*
14560Sstevel@tonic-gate 	 * assuming system call has already called tmp_rwlock
14570Sstevel@tonic-gate 	 */
14580Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&tp->tn_rwlock));
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	if (uiop->uio_iovcnt != 1)
14610Sstevel@tonic-gate 		return (EINVAL);
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 	if (vp->v_type != VDIR)
14640Sstevel@tonic-gate 		return (ENOTDIR);
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate 	/*
14670Sstevel@tonic-gate 	 * There's a window here where someone could have removed
14680Sstevel@tonic-gate 	 * all the entries in the directory after we put a hold on the
14690Sstevel@tonic-gate 	 * vnode but before we grabbed the rwlock.  Just return.
14700Sstevel@tonic-gate 	 */
14710Sstevel@tonic-gate 	if (tp->tn_dir == NULL) {
14720Sstevel@tonic-gate 		if (tp->tn_nlink) {
14730Sstevel@tonic-gate 			panic("empty directory 0x%p", (void *)tp);
14740Sstevel@tonic-gate 			/*NOTREACHED*/
14750Sstevel@tonic-gate 		}
14760Sstevel@tonic-gate 		return (0);
14770Sstevel@tonic-gate 	}
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 	/*
14800Sstevel@tonic-gate 	 * Get space for multiple directory entries
14810Sstevel@tonic-gate 	 */
14820Sstevel@tonic-gate 	total_bytes_wanted = uiop->uio_iov->iov_len;
14830Sstevel@tonic-gate 	bufsize = total_bytes_wanted + sizeof (struct dirent64);
14840Sstevel@tonic-gate 	outbuf = kmem_alloc(bufsize, KM_SLEEP);
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate 	dp = (struct dirent64 *)outbuf;
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 	offset = 0;
14900Sstevel@tonic-gate 	tdp = tp->tn_dir;
14910Sstevel@tonic-gate 	while (tdp) {
14920Sstevel@tonic-gate 		namelen = strlen(tdp->td_name);	/* no +1 needed */
14930Sstevel@tonic-gate 		offset = tdp->td_offset;
14940Sstevel@tonic-gate 		if (offset >= uiop->uio_offset) {
14950Sstevel@tonic-gate 			reclen = (int)DIRENT64_RECLEN(namelen);
14960Sstevel@tonic-gate 			if (outcount + reclen > total_bytes_wanted) {
14970Sstevel@tonic-gate 				if (!outcount)
14980Sstevel@tonic-gate 					/*
14990Sstevel@tonic-gate 					 * Buffer too small for any entries.
15000Sstevel@tonic-gate 					 */
15010Sstevel@tonic-gate 					error = EINVAL;
15020Sstevel@tonic-gate 				break;
15030Sstevel@tonic-gate 			}
15040Sstevel@tonic-gate 			ASSERT(tdp->td_tmpnode != NULL);
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 			/* use strncpy(9f) to zero out uninitialized bytes */
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 			(void) strncpy(dp->d_name, tdp->td_name,
15090Sstevel@tonic-gate 			    DIRENT64_NAMELEN(reclen));
15100Sstevel@tonic-gate 			dp->d_reclen = (ushort_t)reclen;
15110Sstevel@tonic-gate 			dp->d_ino = (ino64_t)tdp->td_tmpnode->tn_nodeid;
15120Sstevel@tonic-gate 			dp->d_off = (offset_t)tdp->td_offset + 1;
15130Sstevel@tonic-gate 			dp = (struct dirent64 *)
15140Sstevel@tonic-gate 			    ((uintptr_t)dp + dp->d_reclen);
15150Sstevel@tonic-gate 			outcount += reclen;
15160Sstevel@tonic-gate 			ASSERT(outcount <= bufsize);
15170Sstevel@tonic-gate 		}
15180Sstevel@tonic-gate 		tdp = tdp->td_next;
15190Sstevel@tonic-gate 	}
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate 	if (!error)
15220Sstevel@tonic-gate 		error = uiomove(outbuf, outcount, UIO_READ, uiop);
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 	if (!error) {
15250Sstevel@tonic-gate 		/* If we reached the end of the list our offset */
15260Sstevel@tonic-gate 		/* should now be just past the end. */
15270Sstevel@tonic-gate 		if (!tdp) {
15280Sstevel@tonic-gate 			offset += 1;
15290Sstevel@tonic-gate 			if (eofp)
15300Sstevel@tonic-gate 				*eofp = 1;
15310Sstevel@tonic-gate 		} else if (eofp)
15320Sstevel@tonic-gate 			*eofp = 0;
15330Sstevel@tonic-gate 		uiop->uio_offset = offset;
15340Sstevel@tonic-gate 	}
15350Sstevel@tonic-gate 	gethrestime(&tp->tn_atime);
15360Sstevel@tonic-gate 	kmem_free(outbuf, bufsize);
15370Sstevel@tonic-gate 	return (error);
15380Sstevel@tonic-gate }
15390Sstevel@tonic-gate 
15405331Samw /* ARGSUSED5 */
15410Sstevel@tonic-gate static int
tmp_symlink(struct vnode * dvp,char * lnm,struct vattr * tva,char * tnm,struct cred * cred,caller_context_t * ct,int flags)15420Sstevel@tonic-gate tmp_symlink(
15430Sstevel@tonic-gate 	struct vnode *dvp,
15440Sstevel@tonic-gate 	char *lnm,
15450Sstevel@tonic-gate 	struct vattr *tva,
15460Sstevel@tonic-gate 	char *tnm,
15475331Samw 	struct cred *cred,
15485331Samw 	caller_context_t *ct,
15495331Samw 	int flags)
15500Sstevel@tonic-gate {
15510Sstevel@tonic-gate 	struct tmpnode *parent = (struct tmpnode *)VTOTN(dvp);
15520Sstevel@tonic-gate 	struct tmpnode *self = (struct tmpnode *)NULL;
15530Sstevel@tonic-gate 	struct tmount *tm = (struct tmount *)VTOTM(dvp);
15540Sstevel@tonic-gate 	char *cp = NULL;
15550Sstevel@tonic-gate 	int error;
15560Sstevel@tonic-gate 	size_t len;
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	/* no symlinks allowed to files in xattr dirs */
15590Sstevel@tonic-gate 	if (parent->tn_flags & ISXATTR)
15600Sstevel@tonic-gate 		return (EINVAL);
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	error = tdirlookup(parent, lnm, &self, cred);
15630Sstevel@tonic-gate 	if (error == 0) {
15640Sstevel@tonic-gate 		/*
15650Sstevel@tonic-gate 		 * The entry already exists
15660Sstevel@tonic-gate 		 */
15670Sstevel@tonic-gate 		tmpnode_rele(self);
15680Sstevel@tonic-gate 		return (EEXIST);	/* was 0 */
15690Sstevel@tonic-gate 	}
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 	if (error != ENOENT) {
15720Sstevel@tonic-gate 		if (self != NULL)
15730Sstevel@tonic-gate 			tmpnode_rele(self);
15740Sstevel@tonic-gate 		return (error);
15750Sstevel@tonic-gate 	}
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	rw_enter(&parent->tn_rwlock, RW_WRITER);
15780Sstevel@tonic-gate 	error = tdirenter(tm, parent, lnm, DE_CREATE, (struct tmpnode *)NULL,
15795331Samw 	    (struct tmpnode *)NULL, tva, &self, cred, ct);
15800Sstevel@tonic-gate 	rw_exit(&parent->tn_rwlock);
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 	if (error) {
15830Sstevel@tonic-gate 		if (self)
15840Sstevel@tonic-gate 			tmpnode_rele(self);
15850Sstevel@tonic-gate 		return (error);
15860Sstevel@tonic-gate 	}
15870Sstevel@tonic-gate 	len = strlen(tnm) + 1;
15880Sstevel@tonic-gate 	cp = tmp_memalloc(len, 0);
15890Sstevel@tonic-gate 	if (cp == NULL) {
15900Sstevel@tonic-gate 		tmpnode_rele(self);
15910Sstevel@tonic-gate 		return (ENOSPC);
15920Sstevel@tonic-gate 	}
15930Sstevel@tonic-gate 	(void) strcpy(cp, tnm);
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 	self->tn_symlink = cp;
15960Sstevel@tonic-gate 	self->tn_size = len - 1;
15970Sstevel@tonic-gate 	tmpnode_rele(self);
15980Sstevel@tonic-gate 	return (error);
15990Sstevel@tonic-gate }
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate /* ARGSUSED2 */
16020Sstevel@tonic-gate static int
tmp_readlink(struct vnode * vp,struct uio * uiop,struct cred * cred,caller_context_t * ct)16035331Samw tmp_readlink(
16045331Samw 	struct vnode *vp,
16055331Samw 	struct uio *uiop,
16065331Samw 	struct cred *cred,
16075331Samw 	caller_context_t *ct)
16080Sstevel@tonic-gate {
16090Sstevel@tonic-gate 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
16100Sstevel@tonic-gate 	int error = 0;
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 	if (vp->v_type != VLNK)
16130Sstevel@tonic-gate 		return (EINVAL);
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 	rw_enter(&tp->tn_rwlock, RW_READER);
16160Sstevel@tonic-gate 	rw_enter(&tp->tn_contents, RW_READER);
16170Sstevel@tonic-gate 	error = uiomove(tp->tn_symlink, tp->tn_size, UIO_READ, uiop);
16180Sstevel@tonic-gate 	gethrestime(&tp->tn_atime);
16190Sstevel@tonic-gate 	rw_exit(&tp->tn_contents);
16200Sstevel@tonic-gate 	rw_exit(&tp->tn_rwlock);
16210Sstevel@tonic-gate 	return (error);
16220Sstevel@tonic-gate }
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate /* ARGSUSED */
16250Sstevel@tonic-gate static int
tmp_fsync(struct vnode * vp,int syncflag,struct cred * cred,caller_context_t * ct)16265331Samw tmp_fsync(
16275331Samw 	struct vnode *vp,
16285331Samw 	int syncflag,
16295331Samw 	struct cred *cred,
16305331Samw 	caller_context_t *ct)
16310Sstevel@tonic-gate {
16320Sstevel@tonic-gate 	return (0);
16330Sstevel@tonic-gate }
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate /* ARGSUSED */
16360Sstevel@tonic-gate static void
tmp_inactive(struct vnode * vp,struct cred * cred,caller_context_t * ct)16375331Samw tmp_inactive(struct vnode *vp, struct cred *cred, caller_context_t *ct)
16380Sstevel@tonic-gate {
16390Sstevel@tonic-gate 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
16400Sstevel@tonic-gate 	struct tmount *tm = (struct tmount *)VFSTOTM(vp->v_vfsp);
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 	rw_enter(&tp->tn_rwlock, RW_WRITER);
16430Sstevel@tonic-gate top:
16440Sstevel@tonic-gate 	mutex_enter(&tp->tn_tlock);
16450Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
16460Sstevel@tonic-gate 	ASSERT(vp->v_count >= 1);
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 	/*
16490Sstevel@tonic-gate 	 * If we don't have the last hold or the link count is non-zero,
16500Sstevel@tonic-gate 	 * there's little to do -- just drop our hold.
16510Sstevel@tonic-gate 	 */
16520Sstevel@tonic-gate 	if (vp->v_count > 1 || tp->tn_nlink != 0) {
16530Sstevel@tonic-gate 		vp->v_count--;
16540Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
16550Sstevel@tonic-gate 		mutex_exit(&tp->tn_tlock);
16560Sstevel@tonic-gate 		rw_exit(&tp->tn_rwlock);
16570Sstevel@tonic-gate 		return;
16580Sstevel@tonic-gate 	}
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 	/*
16610Sstevel@tonic-gate 	 * We have the last hold *and* the link count is zero, so this
16620Sstevel@tonic-gate 	 * tmpnode is dead from the filesystem's viewpoint.  However,
16630Sstevel@tonic-gate 	 * if the tmpnode has any pages associated with it (i.e. if it's
16640Sstevel@tonic-gate 	 * a normal file with non-zero size), the tmpnode can still be
16650Sstevel@tonic-gate 	 * discovered by pageout or fsflush via the page vnode pointers.
16660Sstevel@tonic-gate 	 * In this case we must drop all our locks, truncate the tmpnode,
16670Sstevel@tonic-gate 	 * and try the whole dance again.
16680Sstevel@tonic-gate 	 */
16690Sstevel@tonic-gate 	if (tp->tn_size != 0) {
16700Sstevel@tonic-gate 		if (tp->tn_type == VREG) {
16710Sstevel@tonic-gate 			mutex_exit(&vp->v_lock);
16720Sstevel@tonic-gate 			mutex_exit(&tp->tn_tlock);
16730Sstevel@tonic-gate 			rw_enter(&tp->tn_contents, RW_WRITER);
16740Sstevel@tonic-gate 			(void) tmpnode_trunc(tm, tp, 0);
16750Sstevel@tonic-gate 			rw_exit(&tp->tn_contents);
16760Sstevel@tonic-gate 			ASSERT(tp->tn_size == 0);
16770Sstevel@tonic-gate 			ASSERT(tp->tn_nblocks == 0);
16780Sstevel@tonic-gate 			goto top;
16790Sstevel@tonic-gate 		}
16800Sstevel@tonic-gate 		if (tp->tn_type == VLNK)
16810Sstevel@tonic-gate 			tmp_memfree(tp->tn_symlink, tp->tn_size + 1);
16820Sstevel@tonic-gate 	}
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	/*
16850Sstevel@tonic-gate 	 * Remove normal file/dir's xattr dir and xattrs.
16860Sstevel@tonic-gate 	 */
16870Sstevel@tonic-gate 	if (tp->tn_xattrdp) {
16880Sstevel@tonic-gate 		struct tmpnode *xtp = tp->tn_xattrdp;
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 		ASSERT(xtp->tn_flags & ISXATTR);
16910Sstevel@tonic-gate 		tmpnode_hold(xtp);
16920Sstevel@tonic-gate 		rw_enter(&xtp->tn_rwlock, RW_WRITER);
16930Sstevel@tonic-gate 		tdirtrunc(xtp);
16940Sstevel@tonic-gate 		DECR_COUNT(&xtp->tn_nlink, &xtp->tn_tlock);
16950Sstevel@tonic-gate 		tp->tn_xattrdp = NULL;
16960Sstevel@tonic-gate 		rw_exit(&xtp->tn_rwlock);
16970Sstevel@tonic-gate 		tmpnode_rele(xtp);
16980Sstevel@tonic-gate 	}
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
17010Sstevel@tonic-gate 	mutex_exit(&tp->tn_tlock);
17020Sstevel@tonic-gate 	/* Here's our chance to send invalid event while we're between locks */
17030Sstevel@tonic-gate 	vn_invalid(TNTOV(tp));
17040Sstevel@tonic-gate 	mutex_enter(&tm->tm_contents);
17050Sstevel@tonic-gate 	if (tp->tn_forw == NULL)
17060Sstevel@tonic-gate 		tm->tm_rootnode->tn_back = tp->tn_back;
17070Sstevel@tonic-gate 	else
17080Sstevel@tonic-gate 		tp->tn_forw->tn_back = tp->tn_back;
17090Sstevel@tonic-gate 	tp->tn_back->tn_forw = tp->tn_forw;
17100Sstevel@tonic-gate 	mutex_exit(&tm->tm_contents);
17110Sstevel@tonic-gate 	rw_exit(&tp->tn_rwlock);
17120Sstevel@tonic-gate 	rw_destroy(&tp->tn_rwlock);
17130Sstevel@tonic-gate 	mutex_destroy(&tp->tn_tlock);
17140Sstevel@tonic-gate 	vn_free(TNTOV(tp));
17150Sstevel@tonic-gate 	tmp_memfree(tp, sizeof (struct tmpnode));
17160Sstevel@tonic-gate }
17170Sstevel@tonic-gate 
17185331Samw /* ARGSUSED2 */
17190Sstevel@tonic-gate static int
tmp_fid(struct vnode * vp,struct fid * fidp,caller_context_t * ct)17205331Samw tmp_fid(struct vnode *vp, struct fid *fidp, caller_context_t *ct)
17210Sstevel@tonic-gate {
17220Sstevel@tonic-gate 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
17230Sstevel@tonic-gate 	struct tfid *tfid;
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 	if (fidp->fid_len < (sizeof (struct tfid) - sizeof (ushort_t))) {
17260Sstevel@tonic-gate 		fidp->fid_len = sizeof (struct tfid) - sizeof (ushort_t);
17270Sstevel@tonic-gate 		return (ENOSPC);
17280Sstevel@tonic-gate 	}
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 	tfid = (struct tfid *)fidp;
17310Sstevel@tonic-gate 	bzero(tfid, sizeof (struct tfid));
17320Sstevel@tonic-gate 	tfid->tfid_len = (int)sizeof (struct tfid) - sizeof (ushort_t);
17330Sstevel@tonic-gate 
17340Sstevel@tonic-gate 	tfid->tfid_ino = tp->tn_nodeid;
17350Sstevel@tonic-gate 	tfid->tfid_gen = tp->tn_gen;
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 	return (0);
17380Sstevel@tonic-gate }
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate /*
17420Sstevel@tonic-gate  * Return all the pages from [off..off+len] in given file
17430Sstevel@tonic-gate  */
17445331Samw /* ARGSUSED */
17450Sstevel@tonic-gate static int
tmp_getpage(struct vnode * vp,offset_t off,size_t len,uint_t * protp,page_t * pl[],size_t plsz,struct seg * seg,caddr_t addr,enum seg_rw rw,struct cred * cr,caller_context_t * ct)17460Sstevel@tonic-gate tmp_getpage(
17470Sstevel@tonic-gate 	struct vnode *vp,
17480Sstevel@tonic-gate 	offset_t off,
17490Sstevel@tonic-gate 	size_t len,
17500Sstevel@tonic-gate 	uint_t *protp,
17510Sstevel@tonic-gate 	page_t *pl[],
17520Sstevel@tonic-gate 	size_t plsz,
17530Sstevel@tonic-gate 	struct seg *seg,
17540Sstevel@tonic-gate 	caddr_t addr,
17550Sstevel@tonic-gate 	enum seg_rw rw,
17565331Samw 	struct cred *cr,
17575331Samw 	caller_context_t *ct)
17580Sstevel@tonic-gate {
17590Sstevel@tonic-gate 	int err = 0;
17600Sstevel@tonic-gate 	struct tmpnode *tp = VTOTN(vp);
17610Sstevel@tonic-gate 	anoff_t toff = (anoff_t)off;
17620Sstevel@tonic-gate 	size_t tlen = len;
17630Sstevel@tonic-gate 	u_offset_t tmpoff;
17640Sstevel@tonic-gate 	timestruc_t now;
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate 	rw_enter(&tp->tn_contents, RW_READER);
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate 	if (off + len  > tp->tn_size + PAGEOFFSET) {
17690Sstevel@tonic-gate 		err = EFAULT;
17700Sstevel@tonic-gate 		goto out;
17710Sstevel@tonic-gate 	}
17720Sstevel@tonic-gate 	/*
17730Sstevel@tonic-gate 	 * Look for holes (no anon slot) in faulting range. If there are
17740Sstevel@tonic-gate 	 * holes we have to switch to a write lock and fill them in. Swap
17750Sstevel@tonic-gate 	 * space for holes was already reserved when the file was grown.
17760Sstevel@tonic-gate 	 */
17770Sstevel@tonic-gate 	tmpoff = toff;
17780Sstevel@tonic-gate 	if (non_anon(tp->tn_anon, btop(off), &tmpoff, &tlen)) {
17790Sstevel@tonic-gate 		if (!rw_tryupgrade(&tp->tn_contents)) {
17800Sstevel@tonic-gate 			rw_exit(&tp->tn_contents);
17810Sstevel@tonic-gate 			rw_enter(&tp->tn_contents, RW_WRITER);
17820Sstevel@tonic-gate 			/* Size may have changed when lock was dropped */
17830Sstevel@tonic-gate 			if (off + len  > tp->tn_size + PAGEOFFSET) {
17840Sstevel@tonic-gate 				err = EFAULT;
17850Sstevel@tonic-gate 				goto out;
17860Sstevel@tonic-gate 			}
17870Sstevel@tonic-gate 		}
17880Sstevel@tonic-gate 		for (toff = (anoff_t)off; toff < (anoff_t)off + len;
17890Sstevel@tonic-gate 		    toff += PAGESIZE) {
17900Sstevel@tonic-gate 			if (anon_get_ptr(tp->tn_anon, btop(toff)) == NULL) {
17910Sstevel@tonic-gate 				/* XXX - may allocate mem w. write lock held */
17920Sstevel@tonic-gate 				(void) anon_set_ptr(tp->tn_anon, btop(toff),
17935928Sjj204856 				    anon_alloc(vp, toff), ANON_SLEEP);
17940Sstevel@tonic-gate 				tp->tn_nblocks++;
17950Sstevel@tonic-gate 			}
17960Sstevel@tonic-gate 		}
17970Sstevel@tonic-gate 		rw_downgrade(&tp->tn_contents);
17980Sstevel@tonic-gate 	}
17990Sstevel@tonic-gate 
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 	if (len <= PAGESIZE)
18020Sstevel@tonic-gate 		err = tmp_getapage(vp, (u_offset_t)off, len, protp, pl, plsz,
18030Sstevel@tonic-gate 		    seg, addr, rw, cr);
18040Sstevel@tonic-gate 	else
18050Sstevel@tonic-gate 		err = pvn_getpages(tmp_getapage, vp, (u_offset_t)off, len,
18060Sstevel@tonic-gate 		    protp, pl, plsz, seg, addr, rw, cr);
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate 	gethrestime(&now);
18090Sstevel@tonic-gate 	tp->tn_atime = now;
18100Sstevel@tonic-gate 	if (rw == S_WRITE)
18110Sstevel@tonic-gate 		tp->tn_mtime = now;
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate out:
18140Sstevel@tonic-gate 	rw_exit(&tp->tn_contents);
18150Sstevel@tonic-gate 	return (err);
18160Sstevel@tonic-gate }
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate /*
18190Sstevel@tonic-gate  * Called from pvn_getpages or swap_getpage to get a particular page.
18200Sstevel@tonic-gate  */
18210Sstevel@tonic-gate /*ARGSUSED*/
18220Sstevel@tonic-gate static int
tmp_getapage(struct vnode * vp,u_offset_t off,size_t len,uint_t * protp,page_t * pl[],size_t plsz,struct seg * seg,caddr_t addr,enum seg_rw rw,struct cred * cr)18230Sstevel@tonic-gate tmp_getapage(
18240Sstevel@tonic-gate 	struct vnode *vp,
18250Sstevel@tonic-gate 	u_offset_t off,
18260Sstevel@tonic-gate 	size_t len,
18270Sstevel@tonic-gate 	uint_t *protp,
18280Sstevel@tonic-gate 	page_t *pl[],
18290Sstevel@tonic-gate 	size_t plsz,
18300Sstevel@tonic-gate 	struct seg *seg,
18310Sstevel@tonic-gate 	caddr_t addr,
18320Sstevel@tonic-gate 	enum seg_rw rw,
18330Sstevel@tonic-gate 	struct cred *cr)
18340Sstevel@tonic-gate {
18350Sstevel@tonic-gate 	struct page *pp;
18360Sstevel@tonic-gate 	int flags;
18370Sstevel@tonic-gate 	int err = 0;
18380Sstevel@tonic-gate 	struct vnode *pvp;
18390Sstevel@tonic-gate 	u_offset_t poff;
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate 	if (protp != NULL)
18420Sstevel@tonic-gate 		*protp = PROT_ALL;
18430Sstevel@tonic-gate again:
18440Sstevel@tonic-gate 	if (pp = page_lookup(vp, off, rw == S_CREATE ? SE_EXCL : SE_SHARED)) {
18450Sstevel@tonic-gate 		if (pl) {
18460Sstevel@tonic-gate 			pl[0] = pp;
18470Sstevel@tonic-gate 			pl[1] = NULL;
18480Sstevel@tonic-gate 		} else {
18490Sstevel@tonic-gate 			page_unlock(pp);
18500Sstevel@tonic-gate 		}
18510Sstevel@tonic-gate 	} else {
18520Sstevel@tonic-gate 		pp = page_create_va(vp, off, PAGESIZE,
18530Sstevel@tonic-gate 		    PG_WAIT | PG_EXCL, seg, addr);
18540Sstevel@tonic-gate 		/*
18550Sstevel@tonic-gate 		 * Someone raced in and created the page after we did the
18560Sstevel@tonic-gate 		 * lookup but before we did the create, so go back and
18570Sstevel@tonic-gate 		 * try to look it up again.
18580Sstevel@tonic-gate 		 */
18590Sstevel@tonic-gate 		if (pp == NULL)
18600Sstevel@tonic-gate 			goto again;
18610Sstevel@tonic-gate 		/*
18620Sstevel@tonic-gate 		 * Fill page from backing store, if any. If none, then
18630Sstevel@tonic-gate 		 * either this is a newly filled hole or page must have
18640Sstevel@tonic-gate 		 * been unmodified and freed so just zero it out.
18650Sstevel@tonic-gate 		 */
18660Sstevel@tonic-gate 		err = swap_getphysname(vp, off, &pvp, &poff);
18670Sstevel@tonic-gate 		if (err) {
18680Sstevel@tonic-gate 			panic("tmp_getapage: no anon slot vp %p "
18690Sstevel@tonic-gate 			    "off %llx pp %p\n", (void *)vp, off, (void *)pp);
18700Sstevel@tonic-gate 		}
18710Sstevel@tonic-gate 		if (pvp) {
18720Sstevel@tonic-gate 			flags = (pl == NULL ? B_ASYNC|B_READ : B_READ);
18730Sstevel@tonic-gate 			err = VOP_PAGEIO(pvp, pp, (u_offset_t)poff, PAGESIZE,
18745331Samw 			    flags, cr, NULL);
18750Sstevel@tonic-gate 			if (flags & B_ASYNC)
18760Sstevel@tonic-gate 				pp = NULL;
18770Sstevel@tonic-gate 		} else if (rw != S_CREATE) {
18780Sstevel@tonic-gate 			pagezero(pp, 0, PAGESIZE);
18790Sstevel@tonic-gate 		}
18800Sstevel@tonic-gate 		if (err && pp)
18810Sstevel@tonic-gate 			pvn_read_done(pp, B_ERROR);
18820Sstevel@tonic-gate 		if (err == 0) {
18830Sstevel@tonic-gate 			if (pl)
18840Sstevel@tonic-gate 				pvn_plist_init(pp, pl, plsz, off, PAGESIZE, rw);
18850Sstevel@tonic-gate 			else
18860Sstevel@tonic-gate 				pvn_io_done(pp);
18870Sstevel@tonic-gate 		}
18880Sstevel@tonic-gate 	}
18890Sstevel@tonic-gate 	return (err);
18900Sstevel@tonic-gate }
18910Sstevel@tonic-gate 
18920Sstevel@tonic-gate 
18930Sstevel@tonic-gate /*
18940Sstevel@tonic-gate  * Flags are composed of {B_INVAL, B_DIRTY B_FREE, B_DONTNEED}.
18950Sstevel@tonic-gate  * If len == 0, do from off to EOF.
18960Sstevel@tonic-gate  */
18970Sstevel@tonic-gate static int tmp_nopage = 0;	/* Don't do tmp_putpage's if set */
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate /* ARGSUSED */
19000Sstevel@tonic-gate int
tmp_putpage(register struct vnode * vp,offset_t off,size_t len,int flags,struct cred * cr,caller_context_t * ct)19010Sstevel@tonic-gate tmp_putpage(
19020Sstevel@tonic-gate 	register struct vnode *vp,
19030Sstevel@tonic-gate 	offset_t off,
19040Sstevel@tonic-gate 	size_t len,
19050Sstevel@tonic-gate 	int flags,
19065331Samw 	struct cred *cr,
19075331Samw 	caller_context_t *ct)
19080Sstevel@tonic-gate {
19090Sstevel@tonic-gate 	register page_t *pp;
19100Sstevel@tonic-gate 	u_offset_t io_off;
19110Sstevel@tonic-gate 	size_t io_len = 0;
19120Sstevel@tonic-gate 	int err = 0;
19130Sstevel@tonic-gate 	struct tmpnode *tp = VTOTN(vp);
19140Sstevel@tonic-gate 	int dolock;
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate 	if (tmp_nopage)
19170Sstevel@tonic-gate 		return (0);
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate 	ASSERT(vp->v_count != 0);
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
19220Sstevel@tonic-gate 		return (ENOSYS);
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 	/*
19250Sstevel@tonic-gate 	 * This being tmpfs, we don't ever do i/o unless we really
19260Sstevel@tonic-gate 	 * have to (when we're low on memory and pageout calls us
19270Sstevel@tonic-gate 	 * with B_ASYNC | B_FREE or the user explicitly asks for it with
19280Sstevel@tonic-gate 	 * B_DONTNEED).
19290Sstevel@tonic-gate 	 * XXX to approximately track the mod time like ufs we should
19300Sstevel@tonic-gate 	 * update the times here. The problem is, once someone does a
19310Sstevel@tonic-gate 	 * store we never clear the mod bit and do i/o, thus fsflush
19320Sstevel@tonic-gate 	 * will keep calling us every 30 seconds to do the i/o and we'll
19330Sstevel@tonic-gate 	 * continually update the mod time. At least we update the mod
19340Sstevel@tonic-gate 	 * time on the first store because this results in a call to getpage.
19350Sstevel@tonic-gate 	 */
19360Sstevel@tonic-gate 	if (flags != (B_ASYNC | B_FREE) && (flags & B_INVAL) == 0 &&
19375928Sjj204856 	    (flags & B_DONTNEED) == 0)
19380Sstevel@tonic-gate 		return (0);
19390Sstevel@tonic-gate 	/*
19400Sstevel@tonic-gate 	 * If this thread owns the lock, i.e., this thread grabbed it
19410Sstevel@tonic-gate 	 * as writer somewhere above, then we don't need to grab the
19420Sstevel@tonic-gate 	 * lock as reader in this routine.
19430Sstevel@tonic-gate 	 */
19440Sstevel@tonic-gate 	dolock = (rw_owner(&tp->tn_contents) != curthread);
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 	/*
19470Sstevel@tonic-gate 	 * If this is pageout don't block on the lock as you could deadlock
19480Sstevel@tonic-gate 	 * when freemem == 0 (another thread has the read lock and is blocked
19490Sstevel@tonic-gate 	 * creating a page, and a third thread is waiting to get the writers
19500Sstevel@tonic-gate 	 * lock - waiting writers priority blocks us from getting the read
19510Sstevel@tonic-gate 	 * lock). Of course, if the only freeable pages are on this tmpnode
19520Sstevel@tonic-gate 	 * we're hosed anyways. A better solution might be a new lock type.
19530Sstevel@tonic-gate 	 * Note: ufs has the same problem.
19540Sstevel@tonic-gate 	 */
19550Sstevel@tonic-gate 	if (curproc == proc_pageout) {
19560Sstevel@tonic-gate 		if (!rw_tryenter(&tp->tn_contents, RW_READER))
19570Sstevel@tonic-gate 			return (ENOMEM);
19580Sstevel@tonic-gate 	} else if (dolock)
19590Sstevel@tonic-gate 		rw_enter(&tp->tn_contents, RW_READER);
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate 	if (!vn_has_cached_data(vp))
19620Sstevel@tonic-gate 		goto out;
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate 	if (len == 0) {
19650Sstevel@tonic-gate 		if (curproc == proc_pageout) {
19660Sstevel@tonic-gate 			panic("tmp: pageout can't block");
19670Sstevel@tonic-gate 			/*NOTREACHED*/
19680Sstevel@tonic-gate 		}
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate 		/* Search the entire vp list for pages >= off. */
19710Sstevel@tonic-gate 		err = pvn_vplist_dirty(vp, (u_offset_t)off, tmp_putapage,
19720Sstevel@tonic-gate 		    flags, cr);
19730Sstevel@tonic-gate 	} else {
19740Sstevel@tonic-gate 		u_offset_t eoff;
19750Sstevel@tonic-gate 
19760Sstevel@tonic-gate 		/*
19770Sstevel@tonic-gate 		 * Loop over all offsets in the range [off...off + len]
19780Sstevel@tonic-gate 		 * looking for pages to deal with.
19790Sstevel@tonic-gate 		 */
19800Sstevel@tonic-gate 		eoff = MIN(off + len, tp->tn_size);
19810Sstevel@tonic-gate 		for (io_off = off; io_off < eoff; io_off += io_len) {
19820Sstevel@tonic-gate 			/*
19830Sstevel@tonic-gate 			 * If we are not invalidating, synchronously
19840Sstevel@tonic-gate 			 * freeing or writing pages use the routine
19850Sstevel@tonic-gate 			 * page_lookup_nowait() to prevent reclaiming
19860Sstevel@tonic-gate 			 * them from the free list.
19870Sstevel@tonic-gate 			 */
19880Sstevel@tonic-gate 			if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
19890Sstevel@tonic-gate 				pp = page_lookup(vp, io_off,
19900Sstevel@tonic-gate 				    (flags & (B_INVAL | B_FREE)) ?
19910Sstevel@tonic-gate 				    SE_EXCL : SE_SHARED);
19920Sstevel@tonic-gate 			} else {
19930Sstevel@tonic-gate 				pp = page_lookup_nowait(vp, io_off,
19940Sstevel@tonic-gate 				    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
19950Sstevel@tonic-gate 			}
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 			if (pp == NULL || pvn_getdirty(pp, flags) == 0)
19980Sstevel@tonic-gate 				io_len = PAGESIZE;
19990Sstevel@tonic-gate 			else {
20000Sstevel@tonic-gate 				err = tmp_putapage(vp, pp, &io_off, &io_len,
20010Sstevel@tonic-gate 				    flags, cr);
20020Sstevel@tonic-gate 				if (err != 0)
20030Sstevel@tonic-gate 					break;
20040Sstevel@tonic-gate 			}
20050Sstevel@tonic-gate 		}
20060Sstevel@tonic-gate 	}
20070Sstevel@tonic-gate 	/* If invalidating, verify all pages on vnode list are gone. */
20080Sstevel@tonic-gate 	if (err == 0 && off == 0 && len == 0 &&
20090Sstevel@tonic-gate 	    (flags & B_INVAL) && vn_has_cached_data(vp)) {
20100Sstevel@tonic-gate 		panic("tmp_putpage: B_INVAL, pages not gone");
20110Sstevel@tonic-gate 		/*NOTREACHED*/
20120Sstevel@tonic-gate 	}
20130Sstevel@tonic-gate out:
20140Sstevel@tonic-gate 	if ((curproc == proc_pageout) || dolock)
20150Sstevel@tonic-gate 		rw_exit(&tp->tn_contents);
20160Sstevel@tonic-gate 	/*
20170Sstevel@tonic-gate 	 * Only reason putapage is going to give us SE_NOSWAP as error
20180Sstevel@tonic-gate 	 * is when we ask a page to be written to physical backing store
20190Sstevel@tonic-gate 	 * and there is none. Ignore this because we might be dealing
20200Sstevel@tonic-gate 	 * with a swap page which does not have any backing store
20210Sstevel@tonic-gate 	 * on disk. In any other case we won't get this error over here.
20220Sstevel@tonic-gate 	 */
20230Sstevel@tonic-gate 	if (err == SE_NOSWAP)
20240Sstevel@tonic-gate 		err = 0;
20250Sstevel@tonic-gate 	return (err);
20260Sstevel@tonic-gate }
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate long tmp_putpagecnt, tmp_pagespushed;
20290Sstevel@tonic-gate 
20300Sstevel@tonic-gate /*
20310Sstevel@tonic-gate  * Write out a single page.
20320Sstevel@tonic-gate  * For tmpfs this means choose a physical swap slot and write the page
20330Sstevel@tonic-gate  * out using VOP_PAGEIO. For performance, we attempt to kluster; i.e.,
20340Sstevel@tonic-gate  * we try to find a bunch of other dirty pages adjacent in the file
20350Sstevel@tonic-gate  * and a bunch of contiguous swap slots, and then write all the pages
20360Sstevel@tonic-gate  * out in a single i/o.
20370Sstevel@tonic-gate  */
20380Sstevel@tonic-gate /*ARGSUSED*/
20390Sstevel@tonic-gate static int
tmp_putapage(struct vnode * vp,page_t * pp,u_offset_t * offp,size_t * lenp,int flags,struct cred * cr)20400Sstevel@tonic-gate tmp_putapage(
20410Sstevel@tonic-gate 	struct vnode *vp,
20420Sstevel@tonic-gate 	page_t *pp,
20430Sstevel@tonic-gate 	u_offset_t *offp,
20440Sstevel@tonic-gate 	size_t *lenp,
20450Sstevel@tonic-gate 	int flags,
20460Sstevel@tonic-gate 	struct cred *cr)
20470Sstevel@tonic-gate {
20480Sstevel@tonic-gate 	int err;
20490Sstevel@tonic-gate 	ulong_t klstart, kllen;
20500Sstevel@tonic-gate 	page_t *pplist, *npplist;
20510Sstevel@tonic-gate 	extern int klustsize;
20520Sstevel@tonic-gate 	long tmp_klustsize;
20530Sstevel@tonic-gate 	struct tmpnode *tp;
20540Sstevel@tonic-gate 	size_t pp_off, pp_len;
20550Sstevel@tonic-gate 	u_offset_t io_off;
20560Sstevel@tonic-gate 	size_t io_len;
20570Sstevel@tonic-gate 	struct vnode *pvp;
20580Sstevel@tonic-gate 	u_offset_t pstart;
20590Sstevel@tonic-gate 	u_offset_t offset;
20600Sstevel@tonic-gate 	u_offset_t tmpoff;
20610Sstevel@tonic-gate 
20620Sstevel@tonic-gate 	ASSERT(PAGE_LOCKED(pp));
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate 	/* Kluster in tmp_klustsize chunks */
20650Sstevel@tonic-gate 	tp = VTOTN(vp);
20660Sstevel@tonic-gate 	tmp_klustsize = klustsize;
20670Sstevel@tonic-gate 	offset = pp->p_offset;
20680Sstevel@tonic-gate 	klstart = (offset / tmp_klustsize) * tmp_klustsize;
20690Sstevel@tonic-gate 	kllen = MIN(tmp_klustsize, tp->tn_size - klstart);
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 	/* Get a kluster of pages */
20720Sstevel@tonic-gate 	pplist =
20730Sstevel@tonic-gate 	    pvn_write_kluster(vp, pp, &tmpoff, &pp_len, klstart, kllen, flags);
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 	pp_off = (size_t)tmpoff;
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 	/*
20780Sstevel@tonic-gate 	 * Get a cluster of physical offsets for the pages; the amount we
20790Sstevel@tonic-gate 	 * get may be some subrange of what we ask for (io_off, io_len).
20800Sstevel@tonic-gate 	 */
20810Sstevel@tonic-gate 	io_off = pp_off;
20820Sstevel@tonic-gate 	io_len = pp_len;
20830Sstevel@tonic-gate 	err = swap_newphysname(vp, offset, &io_off, &io_len, &pvp, &pstart);
20840Sstevel@tonic-gate 	ASSERT(err != SE_NOANON); /* anon slot must have been filled */
20850Sstevel@tonic-gate 	if (err) {
20860Sstevel@tonic-gate 		pvn_write_done(pplist, B_ERROR | B_WRITE | flags);
20870Sstevel@tonic-gate 		/*
20880Sstevel@tonic-gate 		 * If this routine is called as a result of segvn_sync
20890Sstevel@tonic-gate 		 * operation and we have no physical swap then we can get an
20900Sstevel@tonic-gate 		 * error here. In such case we would return SE_NOSWAP as error.
20910Sstevel@tonic-gate 		 * At this point, we expect only SE_NOSWAP.
20920Sstevel@tonic-gate 		 */
20930Sstevel@tonic-gate 		ASSERT(err == SE_NOSWAP);
20940Sstevel@tonic-gate 		if (flags & B_INVAL)
20950Sstevel@tonic-gate 			err = ENOMEM;
20960Sstevel@tonic-gate 		goto out;
20970Sstevel@tonic-gate 	}
20980Sstevel@tonic-gate 	ASSERT(pp_off <= io_off && io_off + io_len <= pp_off + pp_len);
20990Sstevel@tonic-gate 	ASSERT(io_off <= offset && offset < io_off + io_len);
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate 	/* Toss pages at front/rear that we couldn't get physical backing for */
21020Sstevel@tonic-gate 	if (io_off != pp_off) {
21030Sstevel@tonic-gate 		npplist = NULL;
21040Sstevel@tonic-gate 		page_list_break(&pplist, &npplist, btop(io_off - pp_off));
21050Sstevel@tonic-gate 		ASSERT(pplist->p_offset == pp_off);
21060Sstevel@tonic-gate 		ASSERT(pplist->p_prev->p_offset == io_off - PAGESIZE);
21070Sstevel@tonic-gate 		pvn_write_done(pplist, B_ERROR | B_WRITE | flags);
21080Sstevel@tonic-gate 		pplist = npplist;
21090Sstevel@tonic-gate 	}
21100Sstevel@tonic-gate 	if (io_off + io_len < pp_off + pp_len) {
21110Sstevel@tonic-gate 		npplist = NULL;
21120Sstevel@tonic-gate 		page_list_break(&pplist, &npplist, btop(io_len));
21130Sstevel@tonic-gate 		ASSERT(npplist->p_offset == io_off + io_len);
21140Sstevel@tonic-gate 		ASSERT(npplist->p_prev->p_offset == pp_off + pp_len - PAGESIZE);
21150Sstevel@tonic-gate 		pvn_write_done(npplist, B_ERROR | B_WRITE | flags);
21160Sstevel@tonic-gate 	}
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 	ASSERT(pplist->p_offset == io_off);
21190Sstevel@tonic-gate 	ASSERT(pplist->p_prev->p_offset == io_off + io_len - PAGESIZE);
21200Sstevel@tonic-gate 	ASSERT(btopr(io_len) <= btopr(kllen));
21210Sstevel@tonic-gate 
21220Sstevel@tonic-gate 	/* Do i/o on the remaining kluster */
21230Sstevel@tonic-gate 	err = VOP_PAGEIO(pvp, pplist, (u_offset_t)pstart, io_len,
21245331Samw 	    B_WRITE | flags, cr, NULL);
21250Sstevel@tonic-gate 
21260Sstevel@tonic-gate 	if ((flags & B_ASYNC) == 0) {
21270Sstevel@tonic-gate 		pvn_write_done(pplist, ((err) ? B_ERROR : 0) | B_WRITE | flags);
21280Sstevel@tonic-gate 	}
21290Sstevel@tonic-gate out:
21300Sstevel@tonic-gate 	if (!err) {
21310Sstevel@tonic-gate 		if (offp)
21320Sstevel@tonic-gate 			*offp = io_off;
21330Sstevel@tonic-gate 		if (lenp)
21340Sstevel@tonic-gate 			*lenp = io_len;
21350Sstevel@tonic-gate 		tmp_putpagecnt++;
21360Sstevel@tonic-gate 		tmp_pagespushed += btop(io_len);
21370Sstevel@tonic-gate 	}
21380Sstevel@tonic-gate 	if (err && err != ENOMEM && err != SE_NOSWAP)
21390Sstevel@tonic-gate 		cmn_err(CE_WARN, "tmp_putapage: err %d\n", err);
21400Sstevel@tonic-gate 	return (err);
21410Sstevel@tonic-gate }
21420Sstevel@tonic-gate 
21435331Samw /* ARGSUSED */
21440Sstevel@tonic-gate static int
tmp_map(struct vnode * vp,offset_t off,struct as * as,caddr_t * addrp,size_t len,uchar_t prot,uchar_t maxprot,uint_t flags,struct cred * cred,caller_context_t * ct)21450Sstevel@tonic-gate tmp_map(
21460Sstevel@tonic-gate 	struct vnode *vp,
21470Sstevel@tonic-gate 	offset_t off,
21480Sstevel@tonic-gate 	struct as *as,
21490Sstevel@tonic-gate 	caddr_t *addrp,
21500Sstevel@tonic-gate 	size_t len,
21510Sstevel@tonic-gate 	uchar_t prot,
21520Sstevel@tonic-gate 	uchar_t maxprot,
21530Sstevel@tonic-gate 	uint_t flags,
21545331Samw 	struct cred *cred,
21555331Samw 	caller_context_t *ct)
21560Sstevel@tonic-gate {
21570Sstevel@tonic-gate 	struct segvn_crargs vn_a;
21580Sstevel@tonic-gate 	struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
21590Sstevel@tonic-gate 	int error;
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate #ifdef _ILP32
21620Sstevel@tonic-gate 	if (len > MAXOFF_T)
21630Sstevel@tonic-gate 		return (ENOMEM);
21640Sstevel@tonic-gate #endif
21650Sstevel@tonic-gate 
21660Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
21670Sstevel@tonic-gate 		return (ENOSYS);
21680Sstevel@tonic-gate 
21693446Smrj 	if (off < 0 || (offset_t)(off + len) < 0 ||
2170145Speterte 	    off > MAXOFF_T || (off + len) > MAXOFF_T)
2171145Speterte 		return (ENXIO);
21720Sstevel@tonic-gate 
21730Sstevel@tonic-gate 	if (vp->v_type != VREG)
21740Sstevel@tonic-gate 		return (ENODEV);
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate 	/*
21770Sstevel@tonic-gate 	 * Don't allow mapping to locked file
21780Sstevel@tonic-gate 	 */
21790Sstevel@tonic-gate 	if (vn_has_mandatory_locks(vp, tp->tn_mode)) {
21800Sstevel@tonic-gate 		return (EAGAIN);
21810Sstevel@tonic-gate 	}
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate 	as_rangelock(as);
21846036Smec 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
21856036Smec 	if (error != 0) {
21866036Smec 		as_rangeunlock(as);
21876036Smec 		return (error);
21880Sstevel@tonic-gate 	}
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 	vn_a.vp = vp;
21910Sstevel@tonic-gate 	vn_a.offset = (u_offset_t)off;
21920Sstevel@tonic-gate 	vn_a.type = flags & MAP_TYPE;
21930Sstevel@tonic-gate 	vn_a.prot = prot;
21940Sstevel@tonic-gate 	vn_a.maxprot = maxprot;
21950Sstevel@tonic-gate 	vn_a.flags = flags & ~MAP_TYPE;
21960Sstevel@tonic-gate 	vn_a.cred = cred;
21970Sstevel@tonic-gate 	vn_a.amp = NULL;
21980Sstevel@tonic-gate 	vn_a.szc = 0;
21990Sstevel@tonic-gate 	vn_a.lgrp_mem_policy_flags = 0;
22000Sstevel@tonic-gate 
22010Sstevel@tonic-gate 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
22020Sstevel@tonic-gate 	as_rangeunlock(as);
22030Sstevel@tonic-gate 	return (error);
22040Sstevel@tonic-gate }
22050Sstevel@tonic-gate 
22060Sstevel@tonic-gate /*
22070Sstevel@tonic-gate  * tmp_addmap and tmp_delmap can't be called since the vp
22080Sstevel@tonic-gate  * maintained in the segvn mapping is NULL.
22090Sstevel@tonic-gate  */
22100Sstevel@tonic-gate /* ARGSUSED */
22110Sstevel@tonic-gate static int
tmp_addmap(struct vnode * vp,offset_t off,struct as * as,caddr_t addr,size_t len,uchar_t prot,uchar_t maxprot,uint_t flags,struct cred * cred,caller_context_t * ct)22120Sstevel@tonic-gate tmp_addmap(
22130Sstevel@tonic-gate 	struct vnode *vp,
22140Sstevel@tonic-gate 	offset_t off,
22150Sstevel@tonic-gate 	struct as *as,
22160Sstevel@tonic-gate 	caddr_t addr,
22170Sstevel@tonic-gate 	size_t len,
22180Sstevel@tonic-gate 	uchar_t prot,
22190Sstevel@tonic-gate 	uchar_t maxprot,
22200Sstevel@tonic-gate 	uint_t flags,
22215331Samw 	struct cred *cred,
22225331Samw 	caller_context_t *ct)
22230Sstevel@tonic-gate {
22240Sstevel@tonic-gate 	return (0);
22250Sstevel@tonic-gate }
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate /* ARGSUSED */
22280Sstevel@tonic-gate static int
tmp_delmap(struct vnode * vp,offset_t off,struct as * as,caddr_t addr,size_t len,uint_t prot,uint_t maxprot,uint_t flags,struct cred * cred,caller_context_t * ct)22290Sstevel@tonic-gate tmp_delmap(
22300Sstevel@tonic-gate 	struct vnode *vp,
22310Sstevel@tonic-gate 	offset_t off,
22320Sstevel@tonic-gate 	struct as *as,
22330Sstevel@tonic-gate 	caddr_t addr,
22340Sstevel@tonic-gate 	size_t len,
22350Sstevel@tonic-gate 	uint_t prot,
22360Sstevel@tonic-gate 	uint_t maxprot,
22370Sstevel@tonic-gate 	uint_t flags,
22385331Samw 	struct cred *cred,
22395331Samw 	caller_context_t *ct)
22400Sstevel@tonic-gate {
22410Sstevel@tonic-gate 	return (0);
22420Sstevel@tonic-gate }
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate static int
tmp_freesp(struct vnode * vp,struct flock64 * lp,int flag)22450Sstevel@tonic-gate tmp_freesp(struct vnode *vp, struct flock64 *lp, int flag)
22460Sstevel@tonic-gate {
22470Sstevel@tonic-gate 	register int i;
22480Sstevel@tonic-gate 	register struct tmpnode *tp = VTOTN(vp);
22490Sstevel@tonic-gate 	int error;
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate 	ASSERT(vp->v_type == VREG);
22520Sstevel@tonic-gate 	ASSERT(lp->l_start >= 0);
22530Sstevel@tonic-gate 
22540Sstevel@tonic-gate 	if (lp->l_len != 0)
22550Sstevel@tonic-gate 		return (EINVAL);
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate 	rw_enter(&tp->tn_rwlock, RW_WRITER);
22580Sstevel@tonic-gate 	if (tp->tn_size == lp->l_start) {
22590Sstevel@tonic-gate 		rw_exit(&tp->tn_rwlock);
22600Sstevel@tonic-gate 		return (0);
22610Sstevel@tonic-gate 	}
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate 	/*
22640Sstevel@tonic-gate 	 * Check for any mandatory locks on the range
22650Sstevel@tonic-gate 	 */
22660Sstevel@tonic-gate 	if (MANDLOCK(vp, tp->tn_mode)) {
22670Sstevel@tonic-gate 		long save_start;
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate 		save_start = lp->l_start;
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 		if (tp->tn_size < lp->l_start) {
22720Sstevel@tonic-gate 			/*
22730Sstevel@tonic-gate 			 * "Truncate up" case: need to make sure there
22740Sstevel@tonic-gate 			 * is no lock beyond current end-of-file. To
22750Sstevel@tonic-gate 			 * do so, we need to set l_start to the size
22760Sstevel@tonic-gate 			 * of the file temporarily.
22770Sstevel@tonic-gate 			 */
22780Sstevel@tonic-gate 			lp->l_start = tp->tn_size;
22790Sstevel@tonic-gate 		}
22800Sstevel@tonic-gate 		lp->l_type = F_WRLCK;
22810Sstevel@tonic-gate 		lp->l_sysid = 0;
22820Sstevel@tonic-gate 		lp->l_pid = ttoproc(curthread)->p_pid;
22830Sstevel@tonic-gate 		i = (flag & (FNDELAY|FNONBLOCK)) ? 0 : SLPFLCK;
22840Sstevel@tonic-gate 		if ((i = reclock(vp, lp, i, 0, lp->l_start, NULL)) != 0 ||
22850Sstevel@tonic-gate 		    lp->l_type != F_UNLCK) {
22860Sstevel@tonic-gate 			rw_exit(&tp->tn_rwlock);
22870Sstevel@tonic-gate 			return (i ? i : EAGAIN);
22880Sstevel@tonic-gate 		}
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate 		lp->l_start = save_start;
22910Sstevel@tonic-gate 	}
22920Sstevel@tonic-gate 	VFSTOTM(vp->v_vfsp);
22930Sstevel@tonic-gate 
22940Sstevel@tonic-gate 	rw_enter(&tp->tn_contents, RW_WRITER);
22950Sstevel@tonic-gate 	error = tmpnode_trunc((struct tmount *)VFSTOTM(vp->v_vfsp),
22960Sstevel@tonic-gate 	    tp, (ulong_t)lp->l_start);
22970Sstevel@tonic-gate 	rw_exit(&tp->tn_contents);
22980Sstevel@tonic-gate 	rw_exit(&tp->tn_rwlock);
22990Sstevel@tonic-gate 	return (error);
23000Sstevel@tonic-gate }
23010Sstevel@tonic-gate 
23020Sstevel@tonic-gate /* ARGSUSED */
23030Sstevel@tonic-gate static int
tmp_space(struct vnode * vp,int cmd,struct flock64 * bfp,int flag,offset_t offset,cred_t * cred,caller_context_t * ct)23040Sstevel@tonic-gate tmp_space(
23050Sstevel@tonic-gate 	struct vnode *vp,
23060Sstevel@tonic-gate 	int cmd,
23070Sstevel@tonic-gate 	struct flock64 *bfp,
23080Sstevel@tonic-gate 	int flag,
23090Sstevel@tonic-gate 	offset_t offset,
23100Sstevel@tonic-gate 	cred_t *cred,
23110Sstevel@tonic-gate 	caller_context_t *ct)
23120Sstevel@tonic-gate {
23130Sstevel@tonic-gate 	int error;
23140Sstevel@tonic-gate 
23150Sstevel@tonic-gate 	if (cmd != F_FREESP)
23160Sstevel@tonic-gate 		return (EINVAL);
23170Sstevel@tonic-gate 	if ((error = convoff(vp, bfp, 0, (offset_t)offset)) == 0) {
23180Sstevel@tonic-gate 		if ((bfp->l_start > MAXOFF_T) || (bfp->l_len > MAXOFF_T))
23190Sstevel@tonic-gate 			return (EFBIG);
23200Sstevel@tonic-gate 		error = tmp_freesp(vp, bfp, flag);
23210Sstevel@tonic-gate 	}
23220Sstevel@tonic-gate 	return (error);
23230Sstevel@tonic-gate }
23240Sstevel@tonic-gate 
23250Sstevel@tonic-gate /* ARGSUSED */
23260Sstevel@tonic-gate static int
tmp_seek(struct vnode * vp,offset_t ooff,offset_t * noffp,caller_context_t * ct)23275331Samw tmp_seek(
23285331Samw 	struct vnode *vp,
23295331Samw 	offset_t ooff,
23305331Samw 	offset_t *noffp,
23315331Samw 	caller_context_t *ct)
23320Sstevel@tonic-gate {
23330Sstevel@tonic-gate 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
23340Sstevel@tonic-gate }
23350Sstevel@tonic-gate 
23360Sstevel@tonic-gate /* ARGSUSED2 */
23370Sstevel@tonic-gate static int
tmp_rwlock(struct vnode * vp,int write_lock,caller_context_t * ctp)23380Sstevel@tonic-gate tmp_rwlock(struct vnode *vp, int write_lock, caller_context_t *ctp)
23390Sstevel@tonic-gate {
23400Sstevel@tonic-gate 	struct tmpnode *tp = VTOTN(vp);
23410Sstevel@tonic-gate 
23420Sstevel@tonic-gate 	if (write_lock) {
23430Sstevel@tonic-gate 		rw_enter(&tp->tn_rwlock, RW_WRITER);
23440Sstevel@tonic-gate 	} else {
23450Sstevel@tonic-gate 		rw_enter(&tp->tn_rwlock, RW_READER);
23460Sstevel@tonic-gate 	}
23470Sstevel@tonic-gate 	return (write_lock);
23480Sstevel@tonic-gate }
23490Sstevel@tonic-gate 
23500Sstevel@tonic-gate /* ARGSUSED1 */
23510Sstevel@tonic-gate static void
tmp_rwunlock(struct vnode * vp,int write_lock,caller_context_t * ctp)23520Sstevel@tonic-gate tmp_rwunlock(struct vnode *vp, int write_lock, caller_context_t *ctp)
23530Sstevel@tonic-gate {
23540Sstevel@tonic-gate 	struct tmpnode *tp = VTOTN(vp);
23550Sstevel@tonic-gate 
23560Sstevel@tonic-gate 	rw_exit(&tp->tn_rwlock);
23570Sstevel@tonic-gate }
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate static int
tmp_pathconf(struct vnode * vp,int cmd,ulong_t * valp,cred_t * cr,caller_context_t * ct)23605331Samw tmp_pathconf(
23615331Samw 	struct vnode *vp,
23625331Samw 	int cmd,
23635331Samw 	ulong_t *valp,
23645331Samw 	cred_t *cr,
23655331Samw 	caller_context_t *ct)
23660Sstevel@tonic-gate {
23670Sstevel@tonic-gate 	struct tmpnode *tp = NULL;
23680Sstevel@tonic-gate 	int error;
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate 	switch (cmd) {
23710Sstevel@tonic-gate 	case _PC_XATTR_EXISTS:
23720Sstevel@tonic-gate 		if (vp->v_vfsp->vfs_flag & VFS_XATTR) {
23730Sstevel@tonic-gate 			*valp = 0;	/* assume no attributes */
23740Sstevel@tonic-gate 			error = 0;	/* okay to ask */
23750Sstevel@tonic-gate 			tp = VTOTN(vp);
23760Sstevel@tonic-gate 			rw_enter(&tp->tn_rwlock, RW_READER);
23770Sstevel@tonic-gate 			if (tp->tn_xattrdp) {
23780Sstevel@tonic-gate 				rw_enter(&tp->tn_xattrdp->tn_rwlock, RW_READER);
23790Sstevel@tonic-gate 				/* do not count "." and ".." */
23800Sstevel@tonic-gate 				if (tp->tn_xattrdp->tn_dirents > 2)
23810Sstevel@tonic-gate 					*valp = 1;
23820Sstevel@tonic-gate 				rw_exit(&tp->tn_xattrdp->tn_rwlock);
23830Sstevel@tonic-gate 			}
23840Sstevel@tonic-gate 			rw_exit(&tp->tn_rwlock);
23850Sstevel@tonic-gate 		} else {
23860Sstevel@tonic-gate 			error = EINVAL;
23870Sstevel@tonic-gate 		}
23880Sstevel@tonic-gate 		break;
23895331Samw 	case _PC_SATTR_ENABLED:
23905331Samw 	case _PC_SATTR_EXISTS:
23917757SJanice.Chang@Sun.COM 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
23925331Samw 		    (vp->v_type == VREG || vp->v_type == VDIR);
23935331Samw 		error = 0;
23945331Samw 		break;
2395*10440SRoger.Faulkner@Sun.COM 	case _PC_TIMESTAMP_RESOLUTION:
2396*10440SRoger.Faulkner@Sun.COM 		/* nanosecond timestamp resolution */
2397*10440SRoger.Faulkner@Sun.COM 		*valp = 1L;
2398*10440SRoger.Faulkner@Sun.COM 		error = 0;
2399*10440SRoger.Faulkner@Sun.COM 		break;
24000Sstevel@tonic-gate 	default:
24015331Samw 		error = fs_pathconf(vp, cmd, valp, cr, ct);
24020Sstevel@tonic-gate 	}
24030Sstevel@tonic-gate 	return (error);
24040Sstevel@tonic-gate }
24050Sstevel@tonic-gate 
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate struct vnodeops *tmp_vnodeops;
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate const fs_operation_def_t tmp_vnodeops_template[] = {
24103898Srsb 	VOPNAME_OPEN,		{ .vop_open = tmp_open },
24113898Srsb 	VOPNAME_CLOSE,		{ .vop_close = tmp_close },
24123898Srsb 	VOPNAME_READ,		{ .vop_read = tmp_read },
24133898Srsb 	VOPNAME_WRITE,		{ .vop_write = tmp_write },
24143898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = tmp_ioctl },
24153898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = tmp_getattr },
24163898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = tmp_setattr },
24173898Srsb 	VOPNAME_ACCESS,		{ .vop_access = tmp_access },
24183898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = tmp_lookup },
24193898Srsb 	VOPNAME_CREATE,		{ .vop_create = tmp_create },
24203898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = tmp_remove },
24213898Srsb 	VOPNAME_LINK,		{ .vop_link = tmp_link },
24223898Srsb 	VOPNAME_RENAME,		{ .vop_rename = tmp_rename },
24233898Srsb 	VOPNAME_MKDIR,		{ .vop_mkdir = tmp_mkdir },
24243898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = tmp_rmdir },
24253898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = tmp_readdir },
24263898Srsb 	VOPNAME_SYMLINK,	{ .vop_symlink = tmp_symlink },
24273898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = tmp_readlink },
24283898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = tmp_fsync },
24293898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = tmp_inactive },
24303898Srsb 	VOPNAME_FID,		{ .vop_fid = tmp_fid },
24313898Srsb 	VOPNAME_RWLOCK,		{ .vop_rwlock = tmp_rwlock },
24323898Srsb 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = tmp_rwunlock },
24333898Srsb 	VOPNAME_SEEK,		{ .vop_seek = tmp_seek },
24343898Srsb 	VOPNAME_SPACE,		{ .vop_space = tmp_space },
24353898Srsb 	VOPNAME_GETPAGE,	{ .vop_getpage = tmp_getpage },
24363898Srsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = tmp_putpage },
24373898Srsb 	VOPNAME_MAP,		{ .vop_map = tmp_map },
24383898Srsb 	VOPNAME_ADDMAP,		{ .vop_addmap = tmp_addmap },
24393898Srsb 	VOPNAME_DELMAP,		{ .vop_delmap = tmp_delmap },
24403898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = tmp_pathconf },
24413898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
24423898Srsb 	NULL,			NULL
24430Sstevel@tonic-gate };
2444