xref: /onnv-gate/usr/src/uts/common/os/fio.c (revision 12789:82cffaae72d5)
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
51846Scraigm  * Common Development and Distribution License (the "License").
61846Scraigm  * 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  */
211846Scraigm 
2210470SRoger.Faulkner@Sun.COM /*
2312290SRoger.Faulkner@Oracle.COM  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
2410470SRoger.Faulkner@Sun.COM  */
2510470SRoger.Faulkner@Sun.COM 
261846Scraigm /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
271846Scraigm /*	All Rights Reserved */
281846Scraigm 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/systm.h>
330Sstevel@tonic-gate #include <sys/errno.h>
340Sstevel@tonic-gate #include <sys/signal.h>
350Sstevel@tonic-gate #include <sys/cred.h>
360Sstevel@tonic-gate #include <sys/user.h>
370Sstevel@tonic-gate #include <sys/conf.h>
380Sstevel@tonic-gate #include <sys/vfs.h>
390Sstevel@tonic-gate #include <sys/vnode.h>
400Sstevel@tonic-gate #include <sys/pathname.h>
410Sstevel@tonic-gate #include <sys/file.h>
420Sstevel@tonic-gate #include <sys/proc.h>
430Sstevel@tonic-gate #include <sys/var.h>
440Sstevel@tonic-gate #include <sys/cpuvar.h>
450Sstevel@tonic-gate #include <sys/open.h>
460Sstevel@tonic-gate #include <sys/cmn_err.h>
470Sstevel@tonic-gate #include <sys/priocntl.h>
480Sstevel@tonic-gate #include <sys/procset.h>
490Sstevel@tonic-gate #include <sys/prsystm.h>
500Sstevel@tonic-gate #include <sys/debug.h>
510Sstevel@tonic-gate #include <sys/kmem.h>
520Sstevel@tonic-gate #include <sys/atomic.h>
530Sstevel@tonic-gate #include <sys/fcntl.h>
540Sstevel@tonic-gate #include <sys/poll.h>
550Sstevel@tonic-gate #include <sys/rctl.h>
560Sstevel@tonic-gate #include <sys/port_impl.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #include <c2/audit.h>
590Sstevel@tonic-gate #include <sys/nbmlock.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #ifdef DEBUG
620Sstevel@tonic-gate 
630Sstevel@tonic-gate static uint32_t afd_maxfd;	/* # of entries in maximum allocated array */
640Sstevel@tonic-gate static uint32_t afd_alloc;	/* count of kmem_alloc()s */
650Sstevel@tonic-gate static uint32_t afd_free;	/* count of kmem_free()s */
660Sstevel@tonic-gate static uint32_t afd_wait;	/* count of waits on non-zero ref count */
670Sstevel@tonic-gate #define	MAXFD(x)	(afd_maxfd = ((afd_maxfd >= (x))? afd_maxfd : (x)))
680Sstevel@tonic-gate #define	COUNT(x)	atomic_add_32(&x, 1)
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #else	/* DEBUG */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #define	MAXFD(x)
730Sstevel@tonic-gate #define	COUNT(x)
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #endif	/* DEBUG */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate kmem_cache_t *file_cache;
780Sstevel@tonic-gate 
791425Spraks static void port_close_fd(portfd_t *);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * File descriptor allocation.
830Sstevel@tonic-gate  *
840Sstevel@tonic-gate  * fd_find(fip, minfd) finds the first available descriptor >= minfd.
850Sstevel@tonic-gate  * The most common case is open(2), in which minfd = 0, but we must also
860Sstevel@tonic-gate  * support fcntl(fd, F_DUPFD, minfd).
870Sstevel@tonic-gate  *
880Sstevel@tonic-gate  * The algorithm is as follows: we keep all file descriptors in an infix
890Sstevel@tonic-gate  * binary tree in which each node records the number of descriptors
900Sstevel@tonic-gate  * allocated in its right subtree, including itself.  Starting at minfd,
910Sstevel@tonic-gate  * we ascend the tree until we find a non-fully allocated right subtree.
920Sstevel@tonic-gate  * We then descend that subtree in a binary search for the smallest fd.
930Sstevel@tonic-gate  * Finally, we ascend the tree again to increment the allocation count
940Sstevel@tonic-gate  * of every subtree containing the newly-allocated fd.  Freeing an fd
950Sstevel@tonic-gate  * requires only the last step: we ascend the tree to decrement allocation
960Sstevel@tonic-gate  * counts.  Each of these three steps (ascent to find non-full subtree,
970Sstevel@tonic-gate  * descent to find lowest fd, ascent to update allocation counts) is
980Sstevel@tonic-gate  * O(log n), thus the algorithm as a whole is O(log n).
990Sstevel@tonic-gate  *
1000Sstevel@tonic-gate  * We don't implement the fd tree using the customary left/right/parent
1010Sstevel@tonic-gate  * pointers, but instead take advantage of the glorious mathematics of
1020Sstevel@tonic-gate  * full infix binary trees.  For reference, here's an illustration of the
1030Sstevel@tonic-gate  * logical structure of such a tree, rooted at 4 (binary 100), covering
1040Sstevel@tonic-gate  * the range 1-7 (binary 001-111).  Our canonical trees do not include
1050Sstevel@tonic-gate  * fd 0; we'll deal with that later.
1060Sstevel@tonic-gate  *
1070Sstevel@tonic-gate  *	      100
1080Sstevel@tonic-gate  *	     /	 \
1090Sstevel@tonic-gate  *	    /	  \
1100Sstevel@tonic-gate  *	  010	  110
1110Sstevel@tonic-gate  *	  / \	  / \
1120Sstevel@tonic-gate  *	001 011 101 111
1130Sstevel@tonic-gate  *
1140Sstevel@tonic-gate  * We make the following observations, all of which are easily proven by
1150Sstevel@tonic-gate  * induction on the depth of the tree:
1160Sstevel@tonic-gate  *
1170Sstevel@tonic-gate  * (T1) The least-significant bit (LSB) of any node is equal to its level
1180Sstevel@tonic-gate  *      in the tree.  In our example, nodes 001, 011, 101 and 111 are at
1190Sstevel@tonic-gate  *      level 0; nodes 010 and 110 are at level 1; and node 100 is at level 2.
1200Sstevel@tonic-gate  *
1210Sstevel@tonic-gate  * (T2) The child size (CSIZE) of node N -- that is, the total number of
1220Sstevel@tonic-gate  *	right-branch descendants in a child of node N, including itself -- is
1230Sstevel@tonic-gate  *	given by clearing all but the least significant bit of N.  This
1240Sstevel@tonic-gate  *	follows immediately from (T1).  Applying this rule to our example, we
1250Sstevel@tonic-gate  *	see that CSIZE(100) = 100, CSIZE(x10) = 10, and CSIZE(xx1) = 1.
1260Sstevel@tonic-gate  *
1270Sstevel@tonic-gate  * (T3) The nearest left ancestor (LPARENT) of node N -- that is, the nearest
1280Sstevel@tonic-gate  *	ancestor containing node N in its right child -- is given by clearing
1290Sstevel@tonic-gate  *	the LSB of N.  For example, LPARENT(111) = 110 and LPARENT(110) = 100.
1300Sstevel@tonic-gate  *	Clearing the LSB of nodes 001, 010 or 100 yields zero, reflecting
1310Sstevel@tonic-gate  *	the fact that these are leftmost nodes.  Note that this algorithm
1320Sstevel@tonic-gate  *	automatically skips generations as necessary.  For example, the parent
1330Sstevel@tonic-gate  *      of node 101 is 110, which is a *right* ancestor (not what we want);
1340Sstevel@tonic-gate  *      but its grandparent is 100, which is a left ancestor. Clearing the LSB
1350Sstevel@tonic-gate  *      of 101 gets us to 100 directly, skipping right past the uninteresting
1360Sstevel@tonic-gate  *      generation (110).
1370Sstevel@tonic-gate  *
1380Sstevel@tonic-gate  *      Note that since LPARENT clears the LSB, whereas CSIZE clears all *but*
1390Sstevel@tonic-gate  *	the LSB, we can express LPARENT() nicely in terms of CSIZE():
1400Sstevel@tonic-gate  *
1410Sstevel@tonic-gate  *	LPARENT(N) = N - CSIZE(N)
1420Sstevel@tonic-gate  *
1430Sstevel@tonic-gate  * (T4) The nearest right ancestor (RPARENT) of node N is given by:
1440Sstevel@tonic-gate  *
1450Sstevel@tonic-gate  *	RPARENT(N) = N + CSIZE(N)
1460Sstevel@tonic-gate  *
1470Sstevel@tonic-gate  * (T5) For every interior node, the children differ from their parent by
1480Sstevel@tonic-gate  *	CSIZE(parent) / 2.  In our example, CSIZE(100) / 2 = 2 = 10 binary,
1490Sstevel@tonic-gate  *      and indeed, the children of 100 are 100 +/- 10 = 010 and 110.
1500Sstevel@tonic-gate  *
1510Sstevel@tonic-gate  * Next, we'll need a few two's-complement math tricks.  Suppose a number,
1520Sstevel@tonic-gate  * N, has the following form:
1530Sstevel@tonic-gate  *
1540Sstevel@tonic-gate  *		N = xxxx10...0
1550Sstevel@tonic-gate  *
1560Sstevel@tonic-gate  * That is, the binary representation of N consists of some string of bits,
1570Sstevel@tonic-gate  * then a 1, then all zeroes.  This amounts to nothing more than saying that
1580Sstevel@tonic-gate  * N has a least-significant bit, which is true for any N != 0.  If we look
1590Sstevel@tonic-gate  * at N and N - 1 together, we see that we can combine them in useful ways:
1600Sstevel@tonic-gate  *
1610Sstevel@tonic-gate  *		  N = xxxx10...0
1620Sstevel@tonic-gate  *	      N - 1 = xxxx01...1
1630Sstevel@tonic-gate  *	------------------------
1640Sstevel@tonic-gate  *	N & (N - 1) = xxxx000000
1650Sstevel@tonic-gate  *	N | (N - 1) = xxxx111111
1660Sstevel@tonic-gate  *	N ^ (N - 1) =     111111
1670Sstevel@tonic-gate  *
1680Sstevel@tonic-gate  * In particular, this suggests several easy ways to clear all but the LSB,
1690Sstevel@tonic-gate  * which by (T2) is exactly what we need to determine CSIZE(N) = 10...0.
1700Sstevel@tonic-gate  * We'll opt for this formulation:
1710Sstevel@tonic-gate  *
1720Sstevel@tonic-gate  *	(C1) CSIZE(N) = (N - 1) ^ (N | (N - 1))
1730Sstevel@tonic-gate  *
1740Sstevel@tonic-gate  * Similarly, we have an easy way to determine LPARENT(N), which requires
1750Sstevel@tonic-gate  * that we clear the LSB of N:
1760Sstevel@tonic-gate  *
1770Sstevel@tonic-gate  *	(L1) LPARENT(N) = N & (N - 1)
1780Sstevel@tonic-gate  *
1790Sstevel@tonic-gate  * We note in the above relations that (N | (N - 1)) - N = CSIZE(N) - 1.
1800Sstevel@tonic-gate  * When combined with (T4), this yields an easy way to compute RPARENT(N):
1810Sstevel@tonic-gate  *
1820Sstevel@tonic-gate  *	(R1) RPARENT(N) = (N | (N - 1)) + 1
1830Sstevel@tonic-gate  *
1840Sstevel@tonic-gate  * Finally, to accommodate fd 0 we must adjust all of our results by +/-1 to
1850Sstevel@tonic-gate  * move the fd range from [1, 2^n) to [0, 2^n - 1).  This is straightforward,
1860Sstevel@tonic-gate  * so there's no need to belabor the algebra; the revised relations become:
1870Sstevel@tonic-gate  *
1880Sstevel@tonic-gate  *	(C1a) CSIZE(N) = N ^ (N | (N + 1))
1890Sstevel@tonic-gate  *
1900Sstevel@tonic-gate  *	(L1a) LPARENT(N) = (N & (N + 1)) - 1
1910Sstevel@tonic-gate  *
1920Sstevel@tonic-gate  *	(R1a) RPARENT(N) = N | (N + 1)
1930Sstevel@tonic-gate  *
1940Sstevel@tonic-gate  * This completes the mathematical framework.  We now have all the tools
1950Sstevel@tonic-gate  * we need to implement fd_find() and fd_reserve().
1960Sstevel@tonic-gate  *
1970Sstevel@tonic-gate  * fd_find(fip, minfd) finds the smallest available file descriptor >= minfd.
1980Sstevel@tonic-gate  * It does not actually allocate the descriptor; that's done by fd_reserve().
1990Sstevel@tonic-gate  * fd_find() proceeds in two steps:
2000Sstevel@tonic-gate  *
2010Sstevel@tonic-gate  * (1) Find the leftmost subtree that contains a descriptor >= minfd.
2020Sstevel@tonic-gate  *     We start at the right subtree rooted at minfd.  If this subtree is
2030Sstevel@tonic-gate  *     not full -- if fip->fi_list[minfd].uf_alloc != CSIZE(minfd) -- then
2040Sstevel@tonic-gate  *     step 1 is done.  Otherwise, we know that all fds in this subtree
2050Sstevel@tonic-gate  *     are taken, so we ascend to RPARENT(minfd) using (R1a).  We repeat
2060Sstevel@tonic-gate  *     this process until we either find a candidate subtree or exceed
2070Sstevel@tonic-gate  *     fip->fi_nfiles.  We use (C1a) to compute CSIZE().
2080Sstevel@tonic-gate  *
2090Sstevel@tonic-gate  * (2) Find the smallest fd in the subtree discovered by step 1.
2100Sstevel@tonic-gate  *     Starting at the root of this subtree, we descend to find the
2110Sstevel@tonic-gate  *     smallest available fd.  Since the left children have the smaller
2120Sstevel@tonic-gate  *     fds, we will descend rightward only when the left child is full.
2130Sstevel@tonic-gate  *
2140Sstevel@tonic-gate  *     We begin by comparing the number of allocated fds in the root
2150Sstevel@tonic-gate  *     to the number of allocated fds in its right child; if they differ
2160Sstevel@tonic-gate  *     by exactly CSIZE(child), we know the left subtree is full, so we
2170Sstevel@tonic-gate  *     descend right; that is, the right child becomes the search root.
2180Sstevel@tonic-gate  *     Otherwise we leave the root alone and start following the right
2190Sstevel@tonic-gate  *     child's left children.  As fortune would have it, this is very
2200Sstevel@tonic-gate  *     simple computationally: by (T5), the right child of fd is just
2210Sstevel@tonic-gate  *     fd + size, where size = CSIZE(fd) / 2.  Applying (T5) again,
2220Sstevel@tonic-gate  *     we find that the right child's left child is fd + size - (size / 2) =
2230Sstevel@tonic-gate  *     fd + (size / 2); *its* left child is fd + (size / 2) - (size / 4) =
2240Sstevel@tonic-gate  *     fd + (size / 4), and so on.  In general, fd's right child's
2250Sstevel@tonic-gate  *     leftmost nth descendant is fd + (size >> n).  Thus, to follow
2260Sstevel@tonic-gate  *     the right child's left descendants, we just halve the size in
2270Sstevel@tonic-gate  *     each iteration of the search.
2280Sstevel@tonic-gate  *
2290Sstevel@tonic-gate  *     When we descend leftward, we must keep track of the number of fds
2300Sstevel@tonic-gate  *     that were allocated in all the right subtrees we rejected, so we
2310Sstevel@tonic-gate  *     know how many of the root fd's allocations are in the remaining
2320Sstevel@tonic-gate  *     (as yet unexplored) leftmost part of its right subtree.  When we
2330Sstevel@tonic-gate  *     encounter a fully-allocated left child -- that is, when we find
2340Sstevel@tonic-gate  *     that fip->fi_list[fd].uf_alloc == ralloc + size -- we descend right
2350Sstevel@tonic-gate  *     (as described earlier), resetting ralloc to zero.
2360Sstevel@tonic-gate  *
2370Sstevel@tonic-gate  * fd_reserve(fip, fd, incr) either allocates or frees fd, depending
2380Sstevel@tonic-gate  * on whether incr is 1 or -1.  Starting at fd, fd_reserve() ascends
2390Sstevel@tonic-gate  * the leftmost ancestors (see (T3)) and updates the allocation counts.
2400Sstevel@tonic-gate  * At each step we use (L1a) to compute LPARENT(), the next left ancestor.
2410Sstevel@tonic-gate  *
2420Sstevel@tonic-gate  * flist_minsize() finds the minimal tree that still covers all
2430Sstevel@tonic-gate  * used fds; as long as the allocation count of a root node is zero, we
2440Sstevel@tonic-gate  * don't need that node or its right subtree.
2450Sstevel@tonic-gate  *
2460Sstevel@tonic-gate  * flist_nalloc() counts the number of allocated fds in the tree, by starting
2470Sstevel@tonic-gate  * at the top of the tree and summing the right-subtree allocation counts as
2480Sstevel@tonic-gate  * it descends leftwards.
2490Sstevel@tonic-gate  *
2500Sstevel@tonic-gate  * Note: we assume that flist_grow() will keep fip->fi_nfiles of the form
2510Sstevel@tonic-gate  * 2^n - 1.  This ensures that the fd trees are always full, which saves
2520Sstevel@tonic-gate  * quite a bit of boundary checking.
2530Sstevel@tonic-gate  */
2540Sstevel@tonic-gate static int
fd_find(uf_info_t * fip,int minfd)2550Sstevel@tonic-gate fd_find(uf_info_t *fip, int minfd)
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate 	int size, ralloc, fd;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fip->fi_lock));
2600Sstevel@tonic-gate 	ASSERT((fip->fi_nfiles & (fip->fi_nfiles + 1)) == 0);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	for (fd = minfd; (uint_t)fd < fip->fi_nfiles; fd |= fd + 1) {
2630Sstevel@tonic-gate 		size = fd ^ (fd | (fd + 1));
2640Sstevel@tonic-gate 		if (fip->fi_list[fd].uf_alloc == size)
2650Sstevel@tonic-gate 			continue;
2660Sstevel@tonic-gate 		for (ralloc = 0, size >>= 1; size != 0; size >>= 1) {
2670Sstevel@tonic-gate 			ralloc += fip->fi_list[fd + size].uf_alloc;
2680Sstevel@tonic-gate 			if (fip->fi_list[fd].uf_alloc == ralloc + size) {
2690Sstevel@tonic-gate 				fd += size;
2700Sstevel@tonic-gate 				ralloc = 0;
2710Sstevel@tonic-gate 			}
2720Sstevel@tonic-gate 		}
2730Sstevel@tonic-gate 		return (fd);
2740Sstevel@tonic-gate 	}
2750Sstevel@tonic-gate 	return (-1);
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate static void
fd_reserve(uf_info_t * fip,int fd,int incr)2790Sstevel@tonic-gate fd_reserve(uf_info_t *fip, int fd, int incr)
2800Sstevel@tonic-gate {
2810Sstevel@tonic-gate 	int pfd;
2820Sstevel@tonic-gate 	uf_entry_t *ufp = &fip->fi_list[fd];
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	ASSERT((uint_t)fd < fip->fi_nfiles);
2850Sstevel@tonic-gate 	ASSERT((ufp->uf_busy == 0 && incr == 1) ||
2860Sstevel@tonic-gate 	    (ufp->uf_busy == 1 && incr == -1));
2870Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ufp->uf_lock));
2880Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fip->fi_lock));
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	for (pfd = fd; pfd >= 0; pfd = (pfd & (pfd + 1)) - 1)
2910Sstevel@tonic-gate 		fip->fi_list[pfd].uf_alloc += incr;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	ufp->uf_busy += incr;
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate static int
flist_minsize(uf_info_t * fip)2970Sstevel@tonic-gate flist_minsize(uf_info_t *fip)
2980Sstevel@tonic-gate {
2990Sstevel@tonic-gate 	int fd;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	/*
3020Sstevel@tonic-gate 	 * We'd like to ASSERT(MUTEX_HELD(&fip->fi_lock)), but we're called
3030Sstevel@tonic-gate 	 * by flist_fork(), which relies on other mechanisms for mutual
3040Sstevel@tonic-gate 	 * exclusion.
3050Sstevel@tonic-gate 	 */
3060Sstevel@tonic-gate 	ASSERT((fip->fi_nfiles & (fip->fi_nfiles + 1)) == 0);
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	for (fd = fip->fi_nfiles; fd != 0; fd >>= 1)
3090Sstevel@tonic-gate 		if (fip->fi_list[fd >> 1].uf_alloc != 0)
3100Sstevel@tonic-gate 			break;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	return (fd);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate static int
flist_nalloc(uf_info_t * fip)3160Sstevel@tonic-gate flist_nalloc(uf_info_t *fip)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate 	int fd;
3190Sstevel@tonic-gate 	int nalloc = 0;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fip->fi_lock));
3220Sstevel@tonic-gate 	ASSERT((fip->fi_nfiles & (fip->fi_nfiles + 1)) == 0);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	for (fd = fip->fi_nfiles; fd != 0; fd >>= 1)
3250Sstevel@tonic-gate 		nalloc += fip->fi_list[fd >> 1].uf_alloc;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	return (nalloc);
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate /*
3310Sstevel@tonic-gate  * Increase size of the fi_list array to accommodate at least maxfd.
3320Sstevel@tonic-gate  * We keep the size of the form 2^n - 1 for benefit of fd_find().
3330Sstevel@tonic-gate  */
3340Sstevel@tonic-gate static void
flist_grow(int maxfd)3350Sstevel@tonic-gate flist_grow(int maxfd)
3360Sstevel@tonic-gate {
3370Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
3380Sstevel@tonic-gate 	int newcnt, oldcnt;
3390Sstevel@tonic-gate 	uf_entry_t *src, *dst, *newlist, *oldlist, *newend, *oldend;
3400Sstevel@tonic-gate 	uf_rlist_t *urp;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	for (newcnt = 1; newcnt <= maxfd; newcnt = (newcnt << 1) | 1)
3430Sstevel@tonic-gate 		continue;
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	newlist = kmem_zalloc(newcnt * sizeof (uf_entry_t), KM_SLEEP);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	mutex_enter(&fip->fi_lock);
3480Sstevel@tonic-gate 	oldcnt = fip->fi_nfiles;
3490Sstevel@tonic-gate 	if (newcnt <= oldcnt) {
3500Sstevel@tonic-gate 		mutex_exit(&fip->fi_lock);
3510Sstevel@tonic-gate 		kmem_free(newlist, newcnt * sizeof (uf_entry_t));
3520Sstevel@tonic-gate 		return;
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate 	ASSERT((newcnt & (newcnt + 1)) == 0);
3550Sstevel@tonic-gate 	oldlist = fip->fi_list;
3560Sstevel@tonic-gate 	oldend = oldlist + oldcnt;
3570Sstevel@tonic-gate 	newend = newlist + oldcnt;	/* no need to lock beyond old end */
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	/*
3600Sstevel@tonic-gate 	 * fi_list and fi_nfiles cannot change while any uf_lock is held,
3610Sstevel@tonic-gate 	 * so we must grab all the old locks *and* the new locks up to oldcnt.
3620Sstevel@tonic-gate 	 * (Locks beyond the end of oldcnt aren't visible until we store
3630Sstevel@tonic-gate 	 * the new fi_nfiles, which is the last thing we do before dropping
3640Sstevel@tonic-gate 	 * all the locks, so there's no need to acquire these locks).
3650Sstevel@tonic-gate 	 * Holding the new locks is necessary because when fi_list changes
3660Sstevel@tonic-gate 	 * to point to the new list, fi_nfiles won't have been stored yet.
3670Sstevel@tonic-gate 	 * If we *didn't* hold the new locks, someone doing a UF_ENTER()
3680Sstevel@tonic-gate 	 * could see the new fi_list, grab the new uf_lock, and then see
3690Sstevel@tonic-gate 	 * fi_nfiles change while the lock is held -- in violation of
3700Sstevel@tonic-gate 	 * UF_ENTER() semantics.
3710Sstevel@tonic-gate 	 */
3720Sstevel@tonic-gate 	for (src = oldlist; src < oldend; src++)
3730Sstevel@tonic-gate 		mutex_enter(&src->uf_lock);
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	for (dst = newlist; dst < newend; dst++)
3760Sstevel@tonic-gate 		mutex_enter(&dst->uf_lock);
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	for (src = oldlist, dst = newlist; src < oldend; src++, dst++) {
3790Sstevel@tonic-gate 		dst->uf_file = src->uf_file;
3800Sstevel@tonic-gate 		dst->uf_fpollinfo = src->uf_fpollinfo;
3810Sstevel@tonic-gate 		dst->uf_refcnt = src->uf_refcnt;
3820Sstevel@tonic-gate 		dst->uf_alloc = src->uf_alloc;
3830Sstevel@tonic-gate 		dst->uf_flag = src->uf_flag;
3840Sstevel@tonic-gate 		dst->uf_busy = src->uf_busy;
3850Sstevel@tonic-gate 		dst->uf_portfd = src->uf_portfd;
3860Sstevel@tonic-gate 	}
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	/*
3890Sstevel@tonic-gate 	 * As soon as we store the new flist, future locking operations
3900Sstevel@tonic-gate 	 * will use it.  Therefore, we must ensure that all the state
3910Sstevel@tonic-gate 	 * we've just established reaches global visibility before the
3920Sstevel@tonic-gate 	 * new flist does.
3930Sstevel@tonic-gate 	 */
3940Sstevel@tonic-gate 	membar_producer();
3950Sstevel@tonic-gate 	fip->fi_list = newlist;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	/*
3980Sstevel@tonic-gate 	 * Routines like getf() make an optimistic check on the validity
3990Sstevel@tonic-gate 	 * of the supplied file descriptor: if it's less than the current
4000Sstevel@tonic-gate 	 * value of fi_nfiles -- examined without any locks -- then it's
4010Sstevel@tonic-gate 	 * safe to attempt a UF_ENTER() on that fd (which is a valid
4020Sstevel@tonic-gate 	 * assumption because fi_nfiles only increases).  Therefore, it
4030Sstevel@tonic-gate 	 * is critical that the new value of fi_nfiles not reach global
4040Sstevel@tonic-gate 	 * visibility until after the new fi_list: if it happened the
4050Sstevel@tonic-gate 	 * other way around, getf() could see the new fi_nfiles and attempt
4060Sstevel@tonic-gate 	 * a UF_ENTER() on the old fi_list, which would write beyond its
4070Sstevel@tonic-gate 	 * end if the fd exceeded the old fi_nfiles.
4080Sstevel@tonic-gate 	 */
4090Sstevel@tonic-gate 	membar_producer();
4100Sstevel@tonic-gate 	fip->fi_nfiles = newcnt;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	/*
4130Sstevel@tonic-gate 	 * The new state is consistent now, so we can drop all the locks.
4140Sstevel@tonic-gate 	 */
4150Sstevel@tonic-gate 	for (dst = newlist; dst < newend; dst++)
4160Sstevel@tonic-gate 		mutex_exit(&dst->uf_lock);
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	for (src = oldlist; src < oldend; src++) {
4190Sstevel@tonic-gate 		/*
4200Sstevel@tonic-gate 		 * If any threads are blocked on the old cvs, wake them.
4210Sstevel@tonic-gate 		 * This will force them to wake up, discover that fi_list
4220Sstevel@tonic-gate 		 * has changed, and go back to sleep on the new cvs.
4230Sstevel@tonic-gate 		 */
4240Sstevel@tonic-gate 		cv_broadcast(&src->uf_wanted_cv);
4250Sstevel@tonic-gate 		cv_broadcast(&src->uf_closing_cv);
4260Sstevel@tonic-gate 		mutex_exit(&src->uf_lock);
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	/*
4320Sstevel@tonic-gate 	 * Retire the old flist.  We can't actually kmem_free() it now
4330Sstevel@tonic-gate 	 * because someone may still have a pointer to it.  Instead,
4340Sstevel@tonic-gate 	 * we link it onto a list of retired flists.  The new flist
4350Sstevel@tonic-gate 	 * is at least double the size of the previous flist, so the
4360Sstevel@tonic-gate 	 * total size of all retired flists will be less than the size
4370Sstevel@tonic-gate 	 * of the current one (to prove, consider the sum of a geometric
4380Sstevel@tonic-gate 	 * series in powers of 2).  exit() frees the retired flists.
4390Sstevel@tonic-gate 	 */
4400Sstevel@tonic-gate 	urp = kmem_zalloc(sizeof (uf_rlist_t), KM_SLEEP);
4410Sstevel@tonic-gate 	urp->ur_list = oldlist;
4420Sstevel@tonic-gate 	urp->ur_nfiles = oldcnt;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	mutex_enter(&fip->fi_lock);
4450Sstevel@tonic-gate 	urp->ur_next = fip->fi_rlist;
4460Sstevel@tonic-gate 	fip->fi_rlist = urp;
4470Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate /*
4510Sstevel@tonic-gate  * Utility functions for keeping track of the active file descriptors.
4520Sstevel@tonic-gate  */
4530Sstevel@tonic-gate void
clear_stale_fd()4540Sstevel@tonic-gate clear_stale_fd()		/* called from post_syscall() */
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate 	afd_t *afd = &curthread->t_activefd;
4570Sstevel@tonic-gate 	int i;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	/* uninitialized is ok here, a_nfd is then zero */
4600Sstevel@tonic-gate 	for (i = 0; i < afd->a_nfd; i++) {
4610Sstevel@tonic-gate 		/* assert that this should not be necessary */
4620Sstevel@tonic-gate 		ASSERT(afd->a_fd[i] == -1);
4630Sstevel@tonic-gate 		afd->a_fd[i] = -1;
4640Sstevel@tonic-gate 	}
4650Sstevel@tonic-gate 	afd->a_stale = 0;
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate void
free_afd(afd_t * afd)4690Sstevel@tonic-gate free_afd(afd_t *afd)		/* called below and from thread_free() */
4700Sstevel@tonic-gate {
4710Sstevel@tonic-gate 	int i;
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	/* free the buffer if it was kmem_alloc()ed */
4740Sstevel@tonic-gate 	if (afd->a_nfd > sizeof (afd->a_buf) / sizeof (afd->a_buf[0])) {
4750Sstevel@tonic-gate 		COUNT(afd_free);
4760Sstevel@tonic-gate 		kmem_free(afd->a_fd, afd->a_nfd * sizeof (afd->a_fd[0]));
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/* (re)initialize the structure */
4800Sstevel@tonic-gate 	afd->a_fd = &afd->a_buf[0];
4810Sstevel@tonic-gate 	afd->a_nfd = sizeof (afd->a_buf) / sizeof (afd->a_buf[0]);
4820Sstevel@tonic-gate 	afd->a_stale = 0;
4830Sstevel@tonic-gate 	for (i = 0; i < afd->a_nfd; i++)
4840Sstevel@tonic-gate 		afd->a_fd[i] = -1;
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate static void
set_active_fd(int fd)4880Sstevel@tonic-gate set_active_fd(int fd)
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate 	afd_t *afd = &curthread->t_activefd;
4910Sstevel@tonic-gate 	int i;
4920Sstevel@tonic-gate 	int *old_fd;
4930Sstevel@tonic-gate 	int old_nfd;
49410470SRoger.Faulkner@Sun.COM 	int *new_fd;
49510470SRoger.Faulkner@Sun.COM 	int new_nfd;
4960Sstevel@tonic-gate 
49710470SRoger.Faulkner@Sun.COM 	if (afd->a_nfd == 0) {	/* first time initialization */
49810470SRoger.Faulkner@Sun.COM 		ASSERT(fd == -1);
49910470SRoger.Faulkner@Sun.COM 		mutex_enter(&afd->a_fdlock);
5000Sstevel@tonic-gate 		free_afd(afd);
50110470SRoger.Faulkner@Sun.COM 		mutex_exit(&afd->a_fdlock);
50210470SRoger.Faulkner@Sun.COM 	}
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	/* insert fd into vacant slot, if any */
5050Sstevel@tonic-gate 	for (i = 0; i < afd->a_nfd; i++) {
5060Sstevel@tonic-gate 		if (afd->a_fd[i] == -1) {
5070Sstevel@tonic-gate 			afd->a_fd[i] = fd;
5080Sstevel@tonic-gate 			return;
5090Sstevel@tonic-gate 		}
5100Sstevel@tonic-gate 	}
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	/*
5130Sstevel@tonic-gate 	 * Reallocate the a_fd[] array to add one more slot.
5140Sstevel@tonic-gate 	 */
51510470SRoger.Faulkner@Sun.COM 	ASSERT(fd == -1);
5160Sstevel@tonic-gate 	old_nfd = afd->a_nfd;
51710470SRoger.Faulkner@Sun.COM 	old_fd = afd->a_fd;
51810470SRoger.Faulkner@Sun.COM 	new_nfd = old_nfd + 1;
51910470SRoger.Faulkner@Sun.COM 	new_fd = kmem_alloc(new_nfd * sizeof (afd->a_fd[0]), KM_SLEEP);
52010470SRoger.Faulkner@Sun.COM 	MAXFD(new_nfd);
5210Sstevel@tonic-gate 	COUNT(afd_alloc);
52210470SRoger.Faulkner@Sun.COM 
52310470SRoger.Faulkner@Sun.COM 	mutex_enter(&afd->a_fdlock);
52410470SRoger.Faulkner@Sun.COM 	afd->a_fd = new_fd;
52510470SRoger.Faulkner@Sun.COM 	afd->a_nfd = new_nfd;
5260Sstevel@tonic-gate 	for (i = 0; i < old_nfd; i++)
5270Sstevel@tonic-gate 		afd->a_fd[i] = old_fd[i];
5280Sstevel@tonic-gate 	afd->a_fd[i] = fd;
52910470SRoger.Faulkner@Sun.COM 	mutex_exit(&afd->a_fdlock);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	if (old_nfd > sizeof (afd->a_buf) / sizeof (afd->a_buf[0])) {
5320Sstevel@tonic-gate 		COUNT(afd_free);
5330Sstevel@tonic-gate 		kmem_free(old_fd, old_nfd * sizeof (afd->a_fd[0]));
5340Sstevel@tonic-gate 	}
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate void
clear_active_fd(int fd)5380Sstevel@tonic-gate clear_active_fd(int fd)		/* called below and from aio.c */
5390Sstevel@tonic-gate {
5400Sstevel@tonic-gate 	afd_t *afd = &curthread->t_activefd;
5410Sstevel@tonic-gate 	int i;
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	for (i = 0; i < afd->a_nfd; i++) {
5440Sstevel@tonic-gate 		if (afd->a_fd[i] == fd) {
5450Sstevel@tonic-gate 			afd->a_fd[i] = -1;
5460Sstevel@tonic-gate 			break;
5470Sstevel@tonic-gate 		}
5480Sstevel@tonic-gate 	}
5490Sstevel@tonic-gate 	ASSERT(i < afd->a_nfd);		/* not found is not ok */
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate /*
5530Sstevel@tonic-gate  * Does this thread have this fd active?
5540Sstevel@tonic-gate  */
5550Sstevel@tonic-gate static int
is_active_fd(kthread_t * t,int fd)5560Sstevel@tonic-gate is_active_fd(kthread_t *t, int fd)
5570Sstevel@tonic-gate {
5580Sstevel@tonic-gate 	afd_t *afd = &t->t_activefd;
5590Sstevel@tonic-gate 	int i;
5600Sstevel@tonic-gate 
56110470SRoger.Faulkner@Sun.COM 	ASSERT(t != curthread);
56210470SRoger.Faulkner@Sun.COM 	mutex_enter(&afd->a_fdlock);
5630Sstevel@tonic-gate 	/* uninitialized is ok here, a_nfd is then zero */
5640Sstevel@tonic-gate 	for (i = 0; i < afd->a_nfd; i++) {
56510470SRoger.Faulkner@Sun.COM 		if (afd->a_fd[i] == fd) {
56610470SRoger.Faulkner@Sun.COM 			mutex_exit(&afd->a_fdlock);
5670Sstevel@tonic-gate 			return (1);
56810470SRoger.Faulkner@Sun.COM 		}
5690Sstevel@tonic-gate 	}
57010470SRoger.Faulkner@Sun.COM 	mutex_exit(&afd->a_fdlock);
5710Sstevel@tonic-gate 	return (0);
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate /*
5750Sstevel@tonic-gate  * Convert a user supplied file descriptor into a pointer to a file
5760Sstevel@tonic-gate  * structure.  Only task is to check range of the descriptor (soft
5770Sstevel@tonic-gate  * resource limit was enforced at open time and shouldn't be checked
5780Sstevel@tonic-gate  * here).
5790Sstevel@tonic-gate  */
5800Sstevel@tonic-gate file_t *
getf(int fd)5810Sstevel@tonic-gate getf(int fd)
5820Sstevel@tonic-gate {
5830Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
5840Sstevel@tonic-gate 	uf_entry_t *ufp;
5850Sstevel@tonic-gate 	file_t *fp;
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	if ((uint_t)fd >= fip->fi_nfiles)
5880Sstevel@tonic-gate 		return (NULL);
5890Sstevel@tonic-gate 
59010470SRoger.Faulkner@Sun.COM 	/*
59110470SRoger.Faulkner@Sun.COM 	 * Reserve a slot in the active fd array now so we can call
59210470SRoger.Faulkner@Sun.COM 	 * set_active_fd(fd) for real below, while still inside UF_ENTER().
59310470SRoger.Faulkner@Sun.COM 	 */
59410470SRoger.Faulkner@Sun.COM 	set_active_fd(-1);
59510470SRoger.Faulkner@Sun.COM 
5960Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
59710470SRoger.Faulkner@Sun.COM 
5980Sstevel@tonic-gate 	if ((fp = ufp->uf_file) == NULL) {
5990Sstevel@tonic-gate 		UF_EXIT(ufp);
6001846Scraigm 
6011846Scraigm 		if (fd == fip->fi_badfd && fip->fi_action > 0)
6021846Scraigm 			tsignal(curthread, fip->fi_action);
6031846Scraigm 
6040Sstevel@tonic-gate 		return (NULL);
6050Sstevel@tonic-gate 	}
6060Sstevel@tonic-gate 	ufp->uf_refcnt++;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	set_active_fd(fd);	/* record the active file descriptor */
6090Sstevel@tonic-gate 
61010470SRoger.Faulkner@Sun.COM 	UF_EXIT(ufp);
61110470SRoger.Faulkner@Sun.COM 
6120Sstevel@tonic-gate 	return (fp);
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate /*
6160Sstevel@tonic-gate  * Close whatever file currently occupies the file descriptor slot
6170Sstevel@tonic-gate  * and install the new file, usually NULL, in the file descriptor slot.
6180Sstevel@tonic-gate  * The close must complete before we release the file descriptor slot.
6191846Scraigm  * If newfp != NULL we only return an error if we can't allocate the
6201846Scraigm  * slot so the caller knows that it needs to free the filep;
6211846Scraigm  * in the other cases we return the error number from closef().
6220Sstevel@tonic-gate  */
6230Sstevel@tonic-gate int
closeandsetf(int fd,file_t * newfp)6240Sstevel@tonic-gate closeandsetf(int fd, file_t *newfp)
6250Sstevel@tonic-gate {
6260Sstevel@tonic-gate 	proc_t *p = curproc;
6270Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(p);
6280Sstevel@tonic-gate 	uf_entry_t *ufp;
6290Sstevel@tonic-gate 	file_t *fp;
6300Sstevel@tonic-gate 	fpollinfo_t *fpip;
6310Sstevel@tonic-gate 	portfd_t *pfd;
6320Sstevel@tonic-gate 	int error;
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	if ((uint_t)fd >= fip->fi_nfiles) {
6350Sstevel@tonic-gate 		if (newfp == NULL)
6360Sstevel@tonic-gate 			return (EBADF);
6370Sstevel@tonic-gate 		flist_grow(fd);
6380Sstevel@tonic-gate 	}
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	if (newfp != NULL) {
6410Sstevel@tonic-gate 		/*
6420Sstevel@tonic-gate 		 * If ufp is reserved but has no file pointer, it's in the
6430Sstevel@tonic-gate 		 * transition between ufalloc() and setf().  We must wait
6440Sstevel@tonic-gate 		 * for this transition to complete before assigning the
6450Sstevel@tonic-gate 		 * new non-NULL file pointer.
6460Sstevel@tonic-gate 		 */
6470Sstevel@tonic-gate 		mutex_enter(&fip->fi_lock);
6481846Scraigm 		if (fd == fip->fi_badfd) {
6491846Scraigm 			mutex_exit(&fip->fi_lock);
6501846Scraigm 			if (fip->fi_action > 0)
6511846Scraigm 				tsignal(curthread, fip->fi_action);
6521846Scraigm 			return (EBADF);
6531846Scraigm 		}
6540Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
6550Sstevel@tonic-gate 		while (ufp->uf_busy && ufp->uf_file == NULL) {
6560Sstevel@tonic-gate 			mutex_exit(&fip->fi_lock);
6570Sstevel@tonic-gate 			cv_wait_stop(&ufp->uf_wanted_cv, &ufp->uf_lock, 250);
6580Sstevel@tonic-gate 			UF_EXIT(ufp);
6590Sstevel@tonic-gate 			mutex_enter(&fip->fi_lock);
6600Sstevel@tonic-gate 			UF_ENTER(ufp, fip, fd);
6610Sstevel@tonic-gate 		}
6620Sstevel@tonic-gate 		if ((fp = ufp->uf_file) == NULL) {
6630Sstevel@tonic-gate 			ASSERT(ufp->uf_fpollinfo == NULL);
6640Sstevel@tonic-gate 			ASSERT(ufp->uf_flag == 0);
6650Sstevel@tonic-gate 			fd_reserve(fip, fd, 1);
6660Sstevel@tonic-gate 			ufp->uf_file = newfp;
6670Sstevel@tonic-gate 			UF_EXIT(ufp);
6680Sstevel@tonic-gate 			mutex_exit(&fip->fi_lock);
6690Sstevel@tonic-gate 			return (0);
6700Sstevel@tonic-gate 		}
6710Sstevel@tonic-gate 		mutex_exit(&fip->fi_lock);
6720Sstevel@tonic-gate 	} else {
6730Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
6740Sstevel@tonic-gate 		if ((fp = ufp->uf_file) == NULL) {
6750Sstevel@tonic-gate 			UF_EXIT(ufp);
6760Sstevel@tonic-gate 			return (EBADF);
6770Sstevel@tonic-gate 		}
6780Sstevel@tonic-gate 	}
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	ASSERT(ufp->uf_busy);
6810Sstevel@tonic-gate 	ufp->uf_file = NULL;
6820Sstevel@tonic-gate 	ufp->uf_flag = 0;
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	/*
6850Sstevel@tonic-gate 	 * If the file descriptor reference count is non-zero, then
6860Sstevel@tonic-gate 	 * some other lwp in the process is performing system call
6870Sstevel@tonic-gate 	 * activity on the file.  To avoid blocking here for a long
6880Sstevel@tonic-gate 	 * time (the other lwp might be in a long term sleep in its
68910470SRoger.Faulkner@Sun.COM 	 * system call), we scan all other lwps in the process to
69010470SRoger.Faulkner@Sun.COM 	 * find the ones with this fd as one of their active fds,
69110470SRoger.Faulkner@Sun.COM 	 * set their a_stale flag, and set them running if they
69210470SRoger.Faulkner@Sun.COM 	 * are in an interruptible sleep so they will emerge from
69310470SRoger.Faulkner@Sun.COM 	 * their system calls immediately.  post_syscall() will
6940Sstevel@tonic-gate 	 * test the a_stale flag and set errno to EBADF.
6950Sstevel@tonic-gate 	 */
6960Sstevel@tonic-gate 	ASSERT(ufp->uf_refcnt == 0 || p->p_lwpcnt > 1);
6970Sstevel@tonic-gate 	if (ufp->uf_refcnt > 0) {
69810470SRoger.Faulkner@Sun.COM 		kthread_t *t;
69910470SRoger.Faulkner@Sun.COM 
70010470SRoger.Faulkner@Sun.COM 		/*
70110470SRoger.Faulkner@Sun.COM 		 * We call sprlock_proc(p) to ensure that the thread
70210470SRoger.Faulkner@Sun.COM 		 * list will not change while we are scanning it.
70310470SRoger.Faulkner@Sun.COM 		 * To do this, we must drop ufp->uf_lock and then
70410470SRoger.Faulkner@Sun.COM 		 * reacquire it (so we are not holding both p->p_lock
70510470SRoger.Faulkner@Sun.COM 		 * and ufp->uf_lock at the same time).  ufp->uf_lock
70610470SRoger.Faulkner@Sun.COM 		 * must be held for is_active_fd() to be correct
70710470SRoger.Faulkner@Sun.COM 		 * (set_active_fd() is called while holding ufp->uf_lock).
70810470SRoger.Faulkner@Sun.COM 		 *
70910470SRoger.Faulkner@Sun.COM 		 * This is a convoluted dance, but it is better than
71010470SRoger.Faulkner@Sun.COM 		 * the old brute-force method of stopping every thread
71110470SRoger.Faulkner@Sun.COM 		 * in the process by calling holdlwps(SHOLDFORK1).
71210470SRoger.Faulkner@Sun.COM 		 */
71310470SRoger.Faulkner@Sun.COM 
7140Sstevel@tonic-gate 		UF_EXIT(ufp);
7150Sstevel@tonic-gate 		COUNT(afd_wait);
7160Sstevel@tonic-gate 
71710470SRoger.Faulkner@Sun.COM 		mutex_enter(&p->p_lock);
71810470SRoger.Faulkner@Sun.COM 		sprlock_proc(p);
71910470SRoger.Faulkner@Sun.COM 		mutex_exit(&p->p_lock);
7200Sstevel@tonic-gate 
72110470SRoger.Faulkner@Sun.COM 		UF_ENTER(ufp, fip, fd);
72210470SRoger.Faulkner@Sun.COM 		ASSERT(ufp->uf_file == NULL);
72310470SRoger.Faulkner@Sun.COM 
72410470SRoger.Faulkner@Sun.COM 		if (ufp->uf_refcnt > 0) {
72510470SRoger.Faulkner@Sun.COM 			for (t = curthread->t_forw;
72610470SRoger.Faulkner@Sun.COM 			    t != curthread;
7270Sstevel@tonic-gate 			    t = t->t_forw) {
7280Sstevel@tonic-gate 				if (is_active_fd(t, fd)) {
72910470SRoger.Faulkner@Sun.COM 					thread_lock(t);
7300Sstevel@tonic-gate 					t->t_activefd.a_stale = 1;
7310Sstevel@tonic-gate 					t->t_post_sys = 1;
73210470SRoger.Faulkner@Sun.COM 					if (ISWAKEABLE(t))
73310470SRoger.Faulkner@Sun.COM 						setrun_locked(t);
73410470SRoger.Faulkner@Sun.COM 					thread_unlock(t);
7350Sstevel@tonic-gate 				}
7360Sstevel@tonic-gate 			}
7370Sstevel@tonic-gate 		}
73810470SRoger.Faulkner@Sun.COM 
73910470SRoger.Faulkner@Sun.COM 		UF_EXIT(ufp);
74010470SRoger.Faulkner@Sun.COM 
74110470SRoger.Faulkner@Sun.COM 		mutex_enter(&p->p_lock);
74210470SRoger.Faulkner@Sun.COM 		sprunlock(p);
74310470SRoger.Faulkner@Sun.COM 
7440Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
7450Sstevel@tonic-gate 		ASSERT(ufp->uf_file == NULL);
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	/*
7490Sstevel@tonic-gate 	 * Wait for other lwps to stop using this file descriptor.
7500Sstevel@tonic-gate 	 */
7510Sstevel@tonic-gate 	while (ufp->uf_refcnt > 0) {
7520Sstevel@tonic-gate 		cv_wait_stop(&ufp->uf_closing_cv, &ufp->uf_lock, 250);
7530Sstevel@tonic-gate 		/*
7540Sstevel@tonic-gate 		 * cv_wait_stop() drops ufp->uf_lock, so the file list
7550Sstevel@tonic-gate 		 * can change.  Drop the lock on our (possibly) stale
7560Sstevel@tonic-gate 		 * ufp and let UF_ENTER() find and lock the current ufp.
7570Sstevel@tonic-gate 		 */
7580Sstevel@tonic-gate 		UF_EXIT(ufp);
7590Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
7600Sstevel@tonic-gate 	}
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate #ifdef DEBUG
7630Sstevel@tonic-gate 	/*
7640Sstevel@tonic-gate 	 * catch a watchfd on device's pollhead list but not on fpollinfo list
7650Sstevel@tonic-gate 	 */
7660Sstevel@tonic-gate 	if (ufp->uf_fpollinfo != NULL)
7670Sstevel@tonic-gate 		checkwfdlist(fp->f_vnode, ufp->uf_fpollinfo);
7680Sstevel@tonic-gate #endif	/* DEBUG */
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	/*
7710Sstevel@tonic-gate 	 * We may need to cleanup some cached poll states in t_pollstate
7720Sstevel@tonic-gate 	 * before the fd can be reused. It is important that we don't
7730Sstevel@tonic-gate 	 * access a stale thread structure. We will do the cleanup in two
7740Sstevel@tonic-gate 	 * phases to avoid deadlock and holding uf_lock for too long.
7750Sstevel@tonic-gate 	 * In phase 1, hold the uf_lock and call pollblockexit() to set
7760Sstevel@tonic-gate 	 * state in t_pollstate struct so that a thread does not exit on
7770Sstevel@tonic-gate 	 * us. In phase 2, we drop the uf_lock and call pollcacheclean().
7780Sstevel@tonic-gate 	 */
7790Sstevel@tonic-gate 	pfd = ufp->uf_portfd;
7800Sstevel@tonic-gate 	ufp->uf_portfd = NULL;
7810Sstevel@tonic-gate 	fpip = ufp->uf_fpollinfo;
7820Sstevel@tonic-gate 	ufp->uf_fpollinfo = NULL;
7830Sstevel@tonic-gate 	if (fpip != NULL)
7840Sstevel@tonic-gate 		pollblockexit(fpip);
7850Sstevel@tonic-gate 	UF_EXIT(ufp);
7860Sstevel@tonic-gate 	if (fpip != NULL)
7870Sstevel@tonic-gate 		pollcacheclean(fpip, fd);
7880Sstevel@tonic-gate 	if (pfd)
7891425Spraks 		port_close_fd(pfd);
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	/*
7920Sstevel@tonic-gate 	 * Keep the file descriptor entry reserved across the closef().
7930Sstevel@tonic-gate 	 */
7940Sstevel@tonic-gate 	error = closef(fp);
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	setf(fd, newfp);
7970Sstevel@tonic-gate 
7981846Scraigm 	/* Only return closef() error when closing is all we do */
7991846Scraigm 	return (newfp == NULL ? error : 0);
8000Sstevel@tonic-gate }
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate /*
8030Sstevel@tonic-gate  * Decrement uf_refcnt; wakeup anyone waiting to close the file.
8040Sstevel@tonic-gate  */
8050Sstevel@tonic-gate void
releasef(int fd)8060Sstevel@tonic-gate releasef(int fd)
8070Sstevel@tonic-gate {
8080Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
8090Sstevel@tonic-gate 	uf_entry_t *ufp;
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
8120Sstevel@tonic-gate 	ASSERT(ufp->uf_refcnt > 0);
81310470SRoger.Faulkner@Sun.COM 	clear_active_fd(fd);	/* clear the active file descriptor */
8140Sstevel@tonic-gate 	if (--ufp->uf_refcnt == 0)
8150Sstevel@tonic-gate 		cv_broadcast(&ufp->uf_closing_cv);
8160Sstevel@tonic-gate 	UF_EXIT(ufp);
8170Sstevel@tonic-gate }
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate /*
8200Sstevel@tonic-gate  * Identical to releasef() but can be called from another process.
8210Sstevel@tonic-gate  */
8220Sstevel@tonic-gate void
areleasef(int fd,uf_info_t * fip)8230Sstevel@tonic-gate areleasef(int fd, uf_info_t *fip)
8240Sstevel@tonic-gate {
8250Sstevel@tonic-gate 	uf_entry_t *ufp;
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
8280Sstevel@tonic-gate 	ASSERT(ufp->uf_refcnt > 0);
8290Sstevel@tonic-gate 	if (--ufp->uf_refcnt == 0)
8300Sstevel@tonic-gate 		cv_broadcast(&ufp->uf_closing_cv);
8310Sstevel@tonic-gate 	UF_EXIT(ufp);
8320Sstevel@tonic-gate }
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate /*
8350Sstevel@tonic-gate  * Duplicate all file descriptors across a fork.
8360Sstevel@tonic-gate  */
8370Sstevel@tonic-gate void
flist_fork(uf_info_t * pfip,uf_info_t * cfip)8380Sstevel@tonic-gate flist_fork(uf_info_t *pfip, uf_info_t *cfip)
8390Sstevel@tonic-gate {
8400Sstevel@tonic-gate 	int fd, nfiles;
8410Sstevel@tonic-gate 	uf_entry_t *pufp, *cufp;
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	mutex_init(&cfip->fi_lock, NULL, MUTEX_DEFAULT, NULL);
8440Sstevel@tonic-gate 	cfip->fi_rlist = NULL;
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	/*
8470Sstevel@tonic-gate 	 * We don't need to hold fi_lock because all other lwp's in the
8480Sstevel@tonic-gate 	 * parent have been held.
8490Sstevel@tonic-gate 	 */
8500Sstevel@tonic-gate 	cfip->fi_nfiles = nfiles = flist_minsize(pfip);
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	cfip->fi_list = kmem_zalloc(nfiles * sizeof (uf_entry_t), KM_SLEEP);
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	for (fd = 0, pufp = pfip->fi_list, cufp = cfip->fi_list; fd < nfiles;
8550Sstevel@tonic-gate 	    fd++, pufp++, cufp++) {
8560Sstevel@tonic-gate 		cufp->uf_file = pufp->uf_file;
8570Sstevel@tonic-gate 		cufp->uf_alloc = pufp->uf_alloc;
8580Sstevel@tonic-gate 		cufp->uf_flag = pufp->uf_flag;
8590Sstevel@tonic-gate 		cufp->uf_busy = pufp->uf_busy;
8600Sstevel@tonic-gate 		if (pufp->uf_file == NULL) {
8610Sstevel@tonic-gate 			ASSERT(pufp->uf_flag == 0);
8620Sstevel@tonic-gate 			if (pufp->uf_busy) {
8630Sstevel@tonic-gate 				/*
8640Sstevel@tonic-gate 				 * Grab locks to appease ASSERTs in fd_reserve
8650Sstevel@tonic-gate 				 */
8660Sstevel@tonic-gate 				mutex_enter(&cfip->fi_lock);
8670Sstevel@tonic-gate 				mutex_enter(&cufp->uf_lock);
8680Sstevel@tonic-gate 				fd_reserve(cfip, fd, -1);
8690Sstevel@tonic-gate 				mutex_exit(&cufp->uf_lock);
8700Sstevel@tonic-gate 				mutex_exit(&cfip->fi_lock);
8710Sstevel@tonic-gate 			}
8720Sstevel@tonic-gate 		}
8730Sstevel@tonic-gate 	}
8740Sstevel@tonic-gate }
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate /*
8770Sstevel@tonic-gate  * Close all open file descriptors for the current process.
8780Sstevel@tonic-gate  * This is only called from exit(), which is single-threaded,
8790Sstevel@tonic-gate  * so we don't need any locking.
8800Sstevel@tonic-gate  */
8810Sstevel@tonic-gate void
closeall(uf_info_t * fip)8820Sstevel@tonic-gate closeall(uf_info_t *fip)
8830Sstevel@tonic-gate {
8840Sstevel@tonic-gate 	int fd;
8850Sstevel@tonic-gate 	file_t *fp;
8860Sstevel@tonic-gate 	uf_entry_t *ufp;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	ufp = fip->fi_list;
8890Sstevel@tonic-gate 	for (fd = 0; fd < fip->fi_nfiles; fd++, ufp++) {
8900Sstevel@tonic-gate 		if ((fp = ufp->uf_file) != NULL) {
8910Sstevel@tonic-gate 			ufp->uf_file = NULL;
8920Sstevel@tonic-gate 			if (ufp->uf_portfd != NULL) {
8931425Spraks 				portfd_t *pfd;
8940Sstevel@tonic-gate 				/* remove event port association */
8951425Spraks 				pfd = ufp->uf_portfd;
8960Sstevel@tonic-gate 				ufp->uf_portfd = NULL;
8971425Spraks 				port_close_fd(pfd);
8980Sstevel@tonic-gate 			}
8990Sstevel@tonic-gate 			ASSERT(ufp->uf_fpollinfo == NULL);
9000Sstevel@tonic-gate 			(void) closef(fp);
9010Sstevel@tonic-gate 		}
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	kmem_free(fip->fi_list, fip->fi_nfiles * sizeof (uf_entry_t));
9050Sstevel@tonic-gate 	fip->fi_list = NULL;
9060Sstevel@tonic-gate 	fip->fi_nfiles = 0;
9070Sstevel@tonic-gate 	while (fip->fi_rlist != NULL) {
9080Sstevel@tonic-gate 		uf_rlist_t *urp = fip->fi_rlist;
9090Sstevel@tonic-gate 		fip->fi_rlist = urp->ur_next;
9100Sstevel@tonic-gate 		kmem_free(urp->ur_list, urp->ur_nfiles * sizeof (uf_entry_t));
9110Sstevel@tonic-gate 		kmem_free(urp, sizeof (uf_rlist_t));
9120Sstevel@tonic-gate 	}
9130Sstevel@tonic-gate }
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate /*
9160Sstevel@tonic-gate  * Internal form of close.  Decrement reference count on file
9170Sstevel@tonic-gate  * structure.  Decrement reference count on the vnode following
9180Sstevel@tonic-gate  * removal of the referencing file structure.
9190Sstevel@tonic-gate  */
9200Sstevel@tonic-gate int
closef(file_t * fp)9210Sstevel@tonic-gate closef(file_t *fp)
9220Sstevel@tonic-gate {
9230Sstevel@tonic-gate 	vnode_t *vp;
9240Sstevel@tonic-gate 	int error;
9250Sstevel@tonic-gate 	int count;
9260Sstevel@tonic-gate 	int flag;
9270Sstevel@tonic-gate 	offset_t offset;
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	/*
9300Sstevel@tonic-gate 	 * audit close of file (may be exit)
9310Sstevel@tonic-gate 	 */
93211861SMarek.Pospisil@Sun.COM 	if (AU_AUDITING())
9330Sstevel@tonic-gate 		audit_closef(fp);
9340Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&P_FINFO(curproc)->fi_lock));
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	mutex_enter(&fp->f_tlock);
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	ASSERT(fp->f_count > 0);
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 	count = fp->f_count--;
9410Sstevel@tonic-gate 	flag = fp->f_flag;
9420Sstevel@tonic-gate 	offset = fp->f_offset;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	vp = fp->f_vnode;
9450Sstevel@tonic-gate 
9465331Samw 	error = VOP_CLOSE(vp, flag, count, offset, fp->f_cred, NULL);
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	if (count > 1) {
9490Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
9500Sstevel@tonic-gate 		return (error);
9510Sstevel@tonic-gate 	}
9520Sstevel@tonic-gate 	ASSERT(fp->f_count == 0);
9530Sstevel@tonic-gate 	mutex_exit(&fp->f_tlock);
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	VN_RELE(vp);
9560Sstevel@tonic-gate 	/*
9570Sstevel@tonic-gate 	 * deallocate resources to audit_data
9580Sstevel@tonic-gate 	 */
9590Sstevel@tonic-gate 	if (audit_active)
9600Sstevel@tonic-gate 		audit_unfalloc(fp);
9610Sstevel@tonic-gate 	crfree(fp->f_cred);
9620Sstevel@tonic-gate 	kmem_cache_free(file_cache, fp);
9630Sstevel@tonic-gate 	return (error);
9640Sstevel@tonic-gate }
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate /*
9670Sstevel@tonic-gate  * This is a combination of ufalloc() and setf().
9680Sstevel@tonic-gate  */
9690Sstevel@tonic-gate int
ufalloc_file(int start,file_t * fp)9700Sstevel@tonic-gate ufalloc_file(int start, file_t *fp)
9710Sstevel@tonic-gate {
9720Sstevel@tonic-gate 	proc_t *p = curproc;
9730Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(p);
9740Sstevel@tonic-gate 	int filelimit;
9750Sstevel@tonic-gate 	uf_entry_t *ufp;
9760Sstevel@tonic-gate 	int nfiles;
9770Sstevel@tonic-gate 	int fd;
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 	/*
9800Sstevel@tonic-gate 	 * Assertion is to convince the correctness of the following
9810Sstevel@tonic-gate 	 * assignment for filelimit after casting to int.
9820Sstevel@tonic-gate 	 */
9830Sstevel@tonic-gate 	ASSERT(p->p_fno_ctl <= INT_MAX);
9840Sstevel@tonic-gate 	filelimit = (int)p->p_fno_ctl;
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	for (;;) {
9870Sstevel@tonic-gate 		mutex_enter(&fip->fi_lock);
9880Sstevel@tonic-gate 		fd = fd_find(fip, start);
9891846Scraigm 		if (fd >= 0 && fd == fip->fi_badfd) {
9901846Scraigm 			start = fd + 1;
9911846Scraigm 			mutex_exit(&fip->fi_lock);
9921846Scraigm 			continue;
9931846Scraigm 		}
9940Sstevel@tonic-gate 		if ((uint_t)fd < filelimit)
9950Sstevel@tonic-gate 			break;
9960Sstevel@tonic-gate 		if (fd >= filelimit) {
9970Sstevel@tonic-gate 			mutex_exit(&fip->fi_lock);
9980Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
9990Sstevel@tonic-gate 			(void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
10000Sstevel@tonic-gate 			    p->p_rctls, p, RCA_SAFE);
10010Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
10020Sstevel@tonic-gate 			return (-1);
10030Sstevel@tonic-gate 		}
10040Sstevel@tonic-gate 		/* fd_find() returned -1 */
10050Sstevel@tonic-gate 		nfiles = fip->fi_nfiles;
10060Sstevel@tonic-gate 		mutex_exit(&fip->fi_lock);
10070Sstevel@tonic-gate 		flist_grow(MAX(start, nfiles));
10080Sstevel@tonic-gate 	}
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
10110Sstevel@tonic-gate 	fd_reserve(fip, fd, 1);
10120Sstevel@tonic-gate 	ASSERT(ufp->uf_file == NULL);
10130Sstevel@tonic-gate 	ufp->uf_file = fp;
10140Sstevel@tonic-gate 	UF_EXIT(ufp);
10150Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
10160Sstevel@tonic-gate 	return (fd);
10170Sstevel@tonic-gate }
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate /*
10200Sstevel@tonic-gate  * Allocate a user file descriptor greater than or equal to "start".
10210Sstevel@tonic-gate  */
10220Sstevel@tonic-gate int
ufalloc(int start)10230Sstevel@tonic-gate ufalloc(int start)
10240Sstevel@tonic-gate {
10250Sstevel@tonic-gate 	return (ufalloc_file(start, NULL));
10260Sstevel@tonic-gate }
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate /*
10290Sstevel@tonic-gate  * Check that a future allocation of count fds on proc p has a good
10300Sstevel@tonic-gate  * chance of succeeding.  If not, do rctl processing as if we'd failed
10310Sstevel@tonic-gate  * the allocation.
10320Sstevel@tonic-gate  *
10330Sstevel@tonic-gate  * Our caller must guarantee that p cannot disappear underneath us.
10340Sstevel@tonic-gate  */
10350Sstevel@tonic-gate int
ufcanalloc(proc_t * p,uint_t count)10360Sstevel@tonic-gate ufcanalloc(proc_t *p, uint_t count)
10370Sstevel@tonic-gate {
10380Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(p);
10390Sstevel@tonic-gate 	int filelimit;
10400Sstevel@tonic-gate 	int current;
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	if (count == 0)
10430Sstevel@tonic-gate 		return (1);
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 	ASSERT(p->p_fno_ctl <= INT_MAX);
10460Sstevel@tonic-gate 	filelimit = (int)p->p_fno_ctl;
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	mutex_enter(&fip->fi_lock);
10490Sstevel@tonic-gate 	current = flist_nalloc(fip);		/* # of in-use descriptors */
10500Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	/*
10530Sstevel@tonic-gate 	 * If count is a positive integer, the worst that can happen is
10540Sstevel@tonic-gate 	 * an overflow to a negative value, which is caught by the >= 0 check.
10550Sstevel@tonic-gate 	 */
10560Sstevel@tonic-gate 	current += count;
10570Sstevel@tonic-gate 	if (count <= INT_MAX && current >= 0 && current <= filelimit)
10580Sstevel@tonic-gate 		return (1);
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
10610Sstevel@tonic-gate 	(void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
10620Sstevel@tonic-gate 	    p->p_rctls, p, RCA_SAFE);
10630Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
10640Sstevel@tonic-gate 	return (0);
10650Sstevel@tonic-gate }
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate /*
10680Sstevel@tonic-gate  * Allocate a user file descriptor and a file structure.
10690Sstevel@tonic-gate  * Initialize the descriptor to point at the file structure.
10700Sstevel@tonic-gate  * If fdp is NULL, the user file descriptor will not be allocated.
10710Sstevel@tonic-gate  */
10720Sstevel@tonic-gate int
falloc(vnode_t * vp,int flag,file_t ** fpp,int * fdp)10730Sstevel@tonic-gate falloc(vnode_t *vp, int flag, file_t **fpp, int *fdp)
10740Sstevel@tonic-gate {
10750Sstevel@tonic-gate 	file_t *fp;
10760Sstevel@tonic-gate 	int fd;
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 	if (fdp) {
10790Sstevel@tonic-gate 		if ((fd = ufalloc(0)) == -1)
10800Sstevel@tonic-gate 			return (EMFILE);
10810Sstevel@tonic-gate 	}
10820Sstevel@tonic-gate 	fp = kmem_cache_alloc(file_cache, KM_SLEEP);
10830Sstevel@tonic-gate 	/*
10840Sstevel@tonic-gate 	 * Note: falloc returns the fp locked
10850Sstevel@tonic-gate 	 */
10860Sstevel@tonic-gate 	mutex_enter(&fp->f_tlock);
10870Sstevel@tonic-gate 	fp->f_count = 1;
10880Sstevel@tonic-gate 	fp->f_flag = (ushort_t)flag;
1089*12789SRoger.Faulkner@Oracle.COM 	fp->f_flag2 = (flag & (FSEARCH|FEXEC)) >> 16;
10900Sstevel@tonic-gate 	fp->f_vnode = vp;
10910Sstevel@tonic-gate 	fp->f_offset = 0;
10920Sstevel@tonic-gate 	fp->f_audit_data = 0;
10930Sstevel@tonic-gate 	crhold(fp->f_cred = CRED());
10940Sstevel@tonic-gate 	/*
10950Sstevel@tonic-gate 	 * allocate resources to audit_data
10960Sstevel@tonic-gate 	 */
10970Sstevel@tonic-gate 	if (audit_active)
10980Sstevel@tonic-gate 		audit_falloc(fp);
10990Sstevel@tonic-gate 	*fpp = fp;
11000Sstevel@tonic-gate 	if (fdp)
11010Sstevel@tonic-gate 		*fdp = fd;
11020Sstevel@tonic-gate 	return (0);
11030Sstevel@tonic-gate }
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate /*ARGSUSED*/
11060Sstevel@tonic-gate static int
file_cache_constructor(void * buf,void * cdrarg,int kmflags)11070Sstevel@tonic-gate file_cache_constructor(void *buf, void *cdrarg, int kmflags)
11080Sstevel@tonic-gate {
11090Sstevel@tonic-gate 	file_t *fp = buf;
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	mutex_init(&fp->f_tlock, NULL, MUTEX_DEFAULT, NULL);
11120Sstevel@tonic-gate 	return (0);
11130Sstevel@tonic-gate }
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate /*ARGSUSED*/
11160Sstevel@tonic-gate static void
file_cache_destructor(void * buf,void * cdrarg)11170Sstevel@tonic-gate file_cache_destructor(void *buf, void *cdrarg)
11180Sstevel@tonic-gate {
11190Sstevel@tonic-gate 	file_t *fp = buf;
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	mutex_destroy(&fp->f_tlock);
11220Sstevel@tonic-gate }
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate void
finit()11250Sstevel@tonic-gate finit()
11260Sstevel@tonic-gate {
11270Sstevel@tonic-gate 	file_cache = kmem_cache_create("file_cache", sizeof (file_t), 0,
11280Sstevel@tonic-gate 	    file_cache_constructor, file_cache_destructor, NULL, NULL, NULL, 0);
11290Sstevel@tonic-gate }
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate void
unfalloc(file_t * fp)11320Sstevel@tonic-gate unfalloc(file_t *fp)
11330Sstevel@tonic-gate {
11340Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fp->f_tlock));
11350Sstevel@tonic-gate 	if (--fp->f_count <= 0) {
11360Sstevel@tonic-gate 		/*
11370Sstevel@tonic-gate 		 * deallocate resources to audit_data
11380Sstevel@tonic-gate 		 */
11390Sstevel@tonic-gate 		if (audit_active)
11400Sstevel@tonic-gate 			audit_unfalloc(fp);
11410Sstevel@tonic-gate 		crfree(fp->f_cred);
11420Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
11430Sstevel@tonic-gate 		kmem_cache_free(file_cache, fp);
11440Sstevel@tonic-gate 	} else
11450Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
11460Sstevel@tonic-gate }
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate /*
11490Sstevel@tonic-gate  * Given a file descriptor, set the user's
11500Sstevel@tonic-gate  * file pointer to the given parameter.
11510Sstevel@tonic-gate  */
11520Sstevel@tonic-gate void
setf(int fd,file_t * fp)11530Sstevel@tonic-gate setf(int fd, file_t *fp)
11540Sstevel@tonic-gate {
11550Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
11560Sstevel@tonic-gate 	uf_entry_t *ufp;
11570Sstevel@tonic-gate 
115811861SMarek.Pospisil@Sun.COM 	if (AU_AUDITING())
11590Sstevel@tonic-gate 		audit_setf(fp, fd);
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	if (fp == NULL) {
11620Sstevel@tonic-gate 		mutex_enter(&fip->fi_lock);
11630Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
11640Sstevel@tonic-gate 		fd_reserve(fip, fd, -1);
11650Sstevel@tonic-gate 		mutex_exit(&fip->fi_lock);
11660Sstevel@tonic-gate 	} else {
11670Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
11680Sstevel@tonic-gate 		ASSERT(ufp->uf_busy);
11690Sstevel@tonic-gate 	}
11700Sstevel@tonic-gate 	ASSERT(ufp->uf_fpollinfo == NULL);
11710Sstevel@tonic-gate 	ASSERT(ufp->uf_flag == 0);
11720Sstevel@tonic-gate 	ufp->uf_file = fp;
11730Sstevel@tonic-gate 	cv_broadcast(&ufp->uf_wanted_cv);
11740Sstevel@tonic-gate 	UF_EXIT(ufp);
11750Sstevel@tonic-gate }
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate /*
11780Sstevel@tonic-gate  * Given a file descriptor, return the file table flags, plus,
11790Sstevel@tonic-gate  * if this is a socket in asynchronous mode, the FASYNC flag.
11800Sstevel@tonic-gate  * getf() may or may not have been called before calling f_getfl().
11810Sstevel@tonic-gate  */
11820Sstevel@tonic-gate int
f_getfl(int fd,int * flagp)11830Sstevel@tonic-gate f_getfl(int fd, int *flagp)
11840Sstevel@tonic-gate {
11850Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
11860Sstevel@tonic-gate 	uf_entry_t *ufp;
11870Sstevel@tonic-gate 	file_t *fp;
11880Sstevel@tonic-gate 	int error;
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	if ((uint_t)fd >= fip->fi_nfiles)
11910Sstevel@tonic-gate 		error = EBADF;
11920Sstevel@tonic-gate 	else {
11930Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
11940Sstevel@tonic-gate 		if ((fp = ufp->uf_file) == NULL)
11950Sstevel@tonic-gate 			error = EBADF;
11960Sstevel@tonic-gate 		else {
11970Sstevel@tonic-gate 			vnode_t *vp = fp->f_vnode;
1198*12789SRoger.Faulkner@Oracle.COM 			int flag = fp->f_flag | (fp->f_flag2 << 16);
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 			/*
12010Sstevel@tonic-gate 			 * BSD fcntl() FASYNC compatibility.
12020Sstevel@tonic-gate 			 */
12038348SEric.Yu@Sun.COM 			if (vp->v_type == VSOCK)
12040Sstevel@tonic-gate 				flag |= sock_getfasync(vp);
12050Sstevel@tonic-gate 			*flagp = flag;
12060Sstevel@tonic-gate 			error = 0;
12070Sstevel@tonic-gate 		}
12080Sstevel@tonic-gate 		UF_EXIT(ufp);
12090Sstevel@tonic-gate 	}
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	return (error);
12120Sstevel@tonic-gate }
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate /*
12150Sstevel@tonic-gate  * Given a file descriptor, return the user's file flags.
12160Sstevel@tonic-gate  * Force the FD_CLOEXEC flag for writable self-open /proc files.
12170Sstevel@tonic-gate  * getf() may or may not have been called before calling f_getfd_error().
12180Sstevel@tonic-gate  */
12190Sstevel@tonic-gate int
f_getfd_error(int fd,int * flagp)12200Sstevel@tonic-gate f_getfd_error(int fd, int *flagp)
12210Sstevel@tonic-gate {
12220Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
12230Sstevel@tonic-gate 	uf_entry_t *ufp;
12240Sstevel@tonic-gate 	file_t *fp;
12250Sstevel@tonic-gate 	int flag;
12260Sstevel@tonic-gate 	int error;
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	if ((uint_t)fd >= fip->fi_nfiles)
12290Sstevel@tonic-gate 		error = EBADF;
12300Sstevel@tonic-gate 	else {
12310Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
12320Sstevel@tonic-gate 		if ((fp = ufp->uf_file) == NULL)
12330Sstevel@tonic-gate 			error = EBADF;
12340Sstevel@tonic-gate 		else {
12350Sstevel@tonic-gate 			flag = ufp->uf_flag;
12360Sstevel@tonic-gate 			if ((fp->f_flag & FWRITE) && pr_isself(fp->f_vnode))
12370Sstevel@tonic-gate 				flag |= FD_CLOEXEC;
12380Sstevel@tonic-gate 			*flagp = flag;
12390Sstevel@tonic-gate 			error = 0;
12400Sstevel@tonic-gate 		}
12410Sstevel@tonic-gate 		UF_EXIT(ufp);
12420Sstevel@tonic-gate 	}
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	return (error);
12450Sstevel@tonic-gate }
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate /*
12480Sstevel@tonic-gate  * getf() must have been called before calling f_getfd().
12490Sstevel@tonic-gate  */
12500Sstevel@tonic-gate char
f_getfd(int fd)12510Sstevel@tonic-gate f_getfd(int fd)
12520Sstevel@tonic-gate {
12530Sstevel@tonic-gate 	int flag = 0;
12540Sstevel@tonic-gate 	(void) f_getfd_error(fd, &flag);
12550Sstevel@tonic-gate 	return ((char)flag);
12560Sstevel@tonic-gate }
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate /*
12590Sstevel@tonic-gate  * Given a file descriptor and file flags, set the user's file flags.
12600Sstevel@tonic-gate  * At present, the only valid flag is FD_CLOEXEC.
12610Sstevel@tonic-gate  * getf() may or may not have been called before calling f_setfd_error().
12620Sstevel@tonic-gate  */
12630Sstevel@tonic-gate int
f_setfd_error(int fd,int flags)12640Sstevel@tonic-gate f_setfd_error(int fd, int flags)
12650Sstevel@tonic-gate {
12660Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
12670Sstevel@tonic-gate 	uf_entry_t *ufp;
12680Sstevel@tonic-gate 	int error;
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	if ((uint_t)fd >= fip->fi_nfiles)
12710Sstevel@tonic-gate 		error = EBADF;
12720Sstevel@tonic-gate 	else {
12730Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
12740Sstevel@tonic-gate 		if (ufp->uf_file == NULL)
12750Sstevel@tonic-gate 			error = EBADF;
12760Sstevel@tonic-gate 		else {
12770Sstevel@tonic-gate 			ufp->uf_flag = flags & FD_CLOEXEC;
12780Sstevel@tonic-gate 			error = 0;
12790Sstevel@tonic-gate 		}
12800Sstevel@tonic-gate 		UF_EXIT(ufp);
12810Sstevel@tonic-gate 	}
12820Sstevel@tonic-gate 	return (error);
12830Sstevel@tonic-gate }
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate void
f_setfd(int fd,char flags)12860Sstevel@tonic-gate f_setfd(int fd, char flags)
12870Sstevel@tonic-gate {
12880Sstevel@tonic-gate 	(void) f_setfd_error(fd, flags);
12890Sstevel@tonic-gate }
12900Sstevel@tonic-gate 
12911846Scraigm #define	BADFD_MIN	3
12921846Scraigm #define	BADFD_MAX	255
12931846Scraigm 
12941846Scraigm /*
12951846Scraigm  * Attempt to allocate a file descriptor which is bad and which
12961846Scraigm  * is "poison" to the application.  It cannot be closed (except
12971846Scraigm  * on exec), allocated for a different use, etc.
12981846Scraigm  */
12991846Scraigm int
f_badfd(int start,int * fdp,int action)13001846Scraigm f_badfd(int start, int *fdp, int action)
13011846Scraigm {
13021846Scraigm 	int fdr;
13031846Scraigm 	int badfd;
13041846Scraigm 	uf_info_t *fip = P_FINFO(curproc);
13051846Scraigm 
13061846Scraigm #ifdef _LP64
13071846Scraigm 	/* No restrictions on 64 bit _file */
13081846Scraigm 	if (get_udatamodel() != DATAMODEL_ILP32)
13091846Scraigm 		return (EINVAL);
13101846Scraigm #endif
13111846Scraigm 
13121846Scraigm 	if (start > BADFD_MAX || start < BADFD_MIN)
13131846Scraigm 		return (EINVAL);
13141846Scraigm 
13151846Scraigm 	if (action >= NSIG || action < 0)
13161846Scraigm 		return (EINVAL);
13171846Scraigm 
13181846Scraigm 	mutex_enter(&fip->fi_lock);
13191846Scraigm 	badfd = fip->fi_badfd;
13201846Scraigm 	mutex_exit(&fip->fi_lock);
13211846Scraigm 
13221846Scraigm 	if (badfd != -1)
13231846Scraigm 		return (EAGAIN);
13241846Scraigm 
13251846Scraigm 	fdr = ufalloc(start);
13261846Scraigm 
13271846Scraigm 	if (fdr > BADFD_MAX) {
13281846Scraigm 		setf(fdr, NULL);
13291846Scraigm 		return (EMFILE);
13301846Scraigm 	}
13311846Scraigm 	if (fdr < 0)
13321846Scraigm 		return (EMFILE);
13331846Scraigm 
13341846Scraigm 	mutex_enter(&fip->fi_lock);
13351846Scraigm 	if (fip->fi_badfd != -1) {
13361846Scraigm 		/* Lost race */
13371846Scraigm 		mutex_exit(&fip->fi_lock);
13381846Scraigm 		setf(fdr, NULL);
13391846Scraigm 		return (EAGAIN);
13401846Scraigm 	}
13411846Scraigm 	fip->fi_action = action;
13421846Scraigm 	fip->fi_badfd = fdr;
13431846Scraigm 	mutex_exit(&fip->fi_lock);
13441846Scraigm 	setf(fdr, NULL);
13451846Scraigm 
13461846Scraigm 	*fdp = fdr;
13471846Scraigm 
13481846Scraigm 	return (0);
13491846Scraigm }
13501846Scraigm 
13510Sstevel@tonic-gate /*
13520Sstevel@tonic-gate  * Allocate a file descriptor and assign it to the vnode "*vpp",
13530Sstevel@tonic-gate  * performing the usual open protocol upon it and returning the
13540Sstevel@tonic-gate  * file descriptor allocated.  It is the responsibility of the
13550Sstevel@tonic-gate  * caller to dispose of "*vpp" if any error occurs.
13560Sstevel@tonic-gate  */
13570Sstevel@tonic-gate int
fassign(vnode_t ** vpp,int mode,int * fdp)13580Sstevel@tonic-gate fassign(vnode_t **vpp, int mode, int *fdp)
13590Sstevel@tonic-gate {
13600Sstevel@tonic-gate 	file_t *fp;
13610Sstevel@tonic-gate 	int error;
13620Sstevel@tonic-gate 	int fd;
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 	if (error = falloc((vnode_t *)NULL, mode, &fp, &fd))
13650Sstevel@tonic-gate 		return (error);
13665331Samw 	if (error = VOP_OPEN(vpp, mode, fp->f_cred, NULL)) {
13670Sstevel@tonic-gate 		setf(fd, NULL);
13680Sstevel@tonic-gate 		unfalloc(fp);
13690Sstevel@tonic-gate 		return (error);
13700Sstevel@tonic-gate 	}
13710Sstevel@tonic-gate 	fp->f_vnode = *vpp;
13720Sstevel@tonic-gate 	mutex_exit(&fp->f_tlock);
13730Sstevel@tonic-gate 	/*
13740Sstevel@tonic-gate 	 * Fill in the slot falloc reserved.
13750Sstevel@tonic-gate 	 */
13760Sstevel@tonic-gate 	setf(fd, fp);
13770Sstevel@tonic-gate 	*fdp = fd;
13780Sstevel@tonic-gate 	return (0);
13790Sstevel@tonic-gate }
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate /*
13820Sstevel@tonic-gate  * When a process forks it must increment the f_count of all file pointers
13830Sstevel@tonic-gate  * since there is a new process pointing at them.  fcnt_add(fip, 1) does this.
13840Sstevel@tonic-gate  * Since we are called when there is only 1 active lwp we don't need to
13850Sstevel@tonic-gate  * hold fi_lock or any uf_lock.  If the fork fails, fork_fail() calls
13860Sstevel@tonic-gate  * fcnt_add(fip, -1) to restore the counts.
13870Sstevel@tonic-gate  */
13880Sstevel@tonic-gate void
fcnt_add(uf_info_t * fip,int incr)13890Sstevel@tonic-gate fcnt_add(uf_info_t *fip, int incr)
13900Sstevel@tonic-gate {
13910Sstevel@tonic-gate 	int i;
13920Sstevel@tonic-gate 	uf_entry_t *ufp;
13930Sstevel@tonic-gate 	file_t *fp;
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 	ufp = fip->fi_list;
13960Sstevel@tonic-gate 	for (i = 0; i < fip->fi_nfiles; i++, ufp++) {
13970Sstevel@tonic-gate 		if ((fp = ufp->uf_file) != NULL) {
13980Sstevel@tonic-gate 			mutex_enter(&fp->f_tlock);
13990Sstevel@tonic-gate 			ASSERT((incr == 1 && fp->f_count >= 1) ||
14000Sstevel@tonic-gate 			    (incr == -1 && fp->f_count >= 2));
14010Sstevel@tonic-gate 			fp->f_count += incr;
14020Sstevel@tonic-gate 			mutex_exit(&fp->f_tlock);
14030Sstevel@tonic-gate 		}
14040Sstevel@tonic-gate 	}
14050Sstevel@tonic-gate }
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate /*
14080Sstevel@tonic-gate  * This is called from exec to close all fd's that have the FD_CLOEXEC flag
14090Sstevel@tonic-gate  * set and also to close all self-open for write /proc file descriptors.
14100Sstevel@tonic-gate  */
14110Sstevel@tonic-gate void
close_exec(uf_info_t * fip)14120Sstevel@tonic-gate close_exec(uf_info_t *fip)
14130Sstevel@tonic-gate {
14140Sstevel@tonic-gate 	int fd;
14150Sstevel@tonic-gate 	file_t *fp;
14160Sstevel@tonic-gate 	fpollinfo_t *fpip;
14170Sstevel@tonic-gate 	uf_entry_t *ufp;
14180Sstevel@tonic-gate 	portfd_t *pfd;
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	ufp = fip->fi_list;
14210Sstevel@tonic-gate 	for (fd = 0; fd < fip->fi_nfiles; fd++, ufp++) {
14220Sstevel@tonic-gate 		if ((fp = ufp->uf_file) != NULL &&
14230Sstevel@tonic-gate 		    ((ufp->uf_flag & FD_CLOEXEC) ||
14240Sstevel@tonic-gate 		    ((fp->f_flag & FWRITE) && pr_isself(fp->f_vnode)))) {
14250Sstevel@tonic-gate 			fpip = ufp->uf_fpollinfo;
14260Sstevel@tonic-gate 			mutex_enter(&fip->fi_lock);
14270Sstevel@tonic-gate 			mutex_enter(&ufp->uf_lock);
14280Sstevel@tonic-gate 			fd_reserve(fip, fd, -1);
14290Sstevel@tonic-gate 			mutex_exit(&fip->fi_lock);
14300Sstevel@tonic-gate 			ufp->uf_file = NULL;
14310Sstevel@tonic-gate 			ufp->uf_fpollinfo = NULL;
14320Sstevel@tonic-gate 			ufp->uf_flag = 0;
14330Sstevel@tonic-gate 			/*
14340Sstevel@tonic-gate 			 * We may need to cleanup some cached poll states
14350Sstevel@tonic-gate 			 * in t_pollstate before the fd can be reused. It
14360Sstevel@tonic-gate 			 * is important that we don't access a stale thread
14370Sstevel@tonic-gate 			 * structure. We will do the cleanup in two
14380Sstevel@tonic-gate 			 * phases to avoid deadlock and holding uf_lock for
14390Sstevel@tonic-gate 			 * too long. In phase 1, hold the uf_lock and call
14400Sstevel@tonic-gate 			 * pollblockexit() to set state in t_pollstate struct
14410Sstevel@tonic-gate 			 * so that a thread does not exit on us. In phase 2,
14420Sstevel@tonic-gate 			 * we drop the uf_lock and call pollcacheclean().
14430Sstevel@tonic-gate 			 */
14440Sstevel@tonic-gate 			pfd = ufp->uf_portfd;
14450Sstevel@tonic-gate 			ufp->uf_portfd = NULL;
14460Sstevel@tonic-gate 			if (fpip != NULL)
14470Sstevel@tonic-gate 				pollblockexit(fpip);
14480Sstevel@tonic-gate 			mutex_exit(&ufp->uf_lock);
14490Sstevel@tonic-gate 			if (fpip != NULL)
14500Sstevel@tonic-gate 				pollcacheclean(fpip, fd);
14510Sstevel@tonic-gate 			if (pfd)
14521425Spraks 				port_close_fd(pfd);
14530Sstevel@tonic-gate 			(void) closef(fp);
14540Sstevel@tonic-gate 		}
14550Sstevel@tonic-gate 	}
14561846Scraigm 
14571846Scraigm 	/* Reset bad fd */
14581846Scraigm 	fip->fi_badfd = -1;
14591846Scraigm 	fip->fi_action = -1;
14600Sstevel@tonic-gate }
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate /*
1463*12789SRoger.Faulkner@Oracle.COM  * Utility function called by most of the *at() system call interfaces.
1464*12789SRoger.Faulkner@Oracle.COM  *
1465*12789SRoger.Faulkner@Oracle.COM  * Generate a starting vnode pointer for an (fd, path) pair where 'fd'
1466*12789SRoger.Faulkner@Oracle.COM  * is an open file descriptor for a directory to be used as the starting
1467*12789SRoger.Faulkner@Oracle.COM  * point for the lookup of the relative pathname 'path' (or, if path is
1468*12789SRoger.Faulkner@Oracle.COM  * NULL, generate a vnode pointer for the direct target of the operation).
1469*12789SRoger.Faulkner@Oracle.COM  *
1470*12789SRoger.Faulkner@Oracle.COM  * If we successfully return a non-NULL startvp, it has been the target
1471*12789SRoger.Faulkner@Oracle.COM  * of VN_HOLD() and the caller must call VN_RELE() on it.
14720Sstevel@tonic-gate  */
14730Sstevel@tonic-gate int
fgetstartvp(int fd,char * path,vnode_t ** startvpp)1474*12789SRoger.Faulkner@Oracle.COM fgetstartvp(int fd, char *path, vnode_t **startvpp)
14750Sstevel@tonic-gate {
1476*12789SRoger.Faulkner@Oracle.COM 	vnode_t		*startvp;
1477*12789SRoger.Faulkner@Oracle.COM 	file_t 		*startfp;
1478*12789SRoger.Faulkner@Oracle.COM 	char 		startchar;
1479*12789SRoger.Faulkner@Oracle.COM 
1480*12789SRoger.Faulkner@Oracle.COM 	if (fd == AT_FDCWD && path == NULL)
1481*12789SRoger.Faulkner@Oracle.COM 		return (EFAULT);
14820Sstevel@tonic-gate 
1483*12789SRoger.Faulkner@Oracle.COM 	if (fd == AT_FDCWD) {
1484*12789SRoger.Faulkner@Oracle.COM 		/*
1485*12789SRoger.Faulkner@Oracle.COM 		 * Start from the current working directory.
1486*12789SRoger.Faulkner@Oracle.COM 		 */
1487*12789SRoger.Faulkner@Oracle.COM 		startvp = NULL;
1488*12789SRoger.Faulkner@Oracle.COM 	} else {
1489*12789SRoger.Faulkner@Oracle.COM 		if (path == NULL)
1490*12789SRoger.Faulkner@Oracle.COM 			startchar = '\0';
1491*12789SRoger.Faulkner@Oracle.COM 		else if (copyin(path, &startchar, sizeof (char)))
1492*12789SRoger.Faulkner@Oracle.COM 			return (EFAULT);
14930Sstevel@tonic-gate 
1494*12789SRoger.Faulkner@Oracle.COM 		if (startchar == '/') {
1495*12789SRoger.Faulkner@Oracle.COM 			/*
1496*12789SRoger.Faulkner@Oracle.COM 			 * 'path' is an absolute pathname.
1497*12789SRoger.Faulkner@Oracle.COM 			 */
1498*12789SRoger.Faulkner@Oracle.COM 			startvp = NULL;
1499*12789SRoger.Faulkner@Oracle.COM 		} else {
1500*12789SRoger.Faulkner@Oracle.COM 			/*
1501*12789SRoger.Faulkner@Oracle.COM 			 * 'path' is a relative pathname or we will
1502*12789SRoger.Faulkner@Oracle.COM 			 * be applying the operation to 'fd' itself.
1503*12789SRoger.Faulkner@Oracle.COM 			 */
1504*12789SRoger.Faulkner@Oracle.COM 			if ((startfp = getf(fd)) == NULL)
1505*12789SRoger.Faulkner@Oracle.COM 				return (EBADF);
1506*12789SRoger.Faulkner@Oracle.COM 			startvp = startfp->f_vnode;
1507*12789SRoger.Faulkner@Oracle.COM 			VN_HOLD(startvp);
1508*12789SRoger.Faulkner@Oracle.COM 			releasef(fd);
15090Sstevel@tonic-gate 		}
1510*12789SRoger.Faulkner@Oracle.COM 	}
1511*12789SRoger.Faulkner@Oracle.COM 	*startvpp = startvp;
1512*12789SRoger.Faulkner@Oracle.COM 	return (0);
15130Sstevel@tonic-gate }
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate /*
1516*12789SRoger.Faulkner@Oracle.COM  * Called from fchownat() and fchmodat() to set ownership and mode.
1517*12789SRoger.Faulkner@Oracle.COM  * The contents of *vap must be set before calling here.
15180Sstevel@tonic-gate  */
1519*12789SRoger.Faulkner@Oracle.COM int
fsetattrat(int fd,char * path,int flags,struct vattr * vap)1520*12789SRoger.Faulkner@Oracle.COM fsetattrat(int fd, char *path, int flags, struct vattr *vap)
15210Sstevel@tonic-gate {
1522*12789SRoger.Faulkner@Oracle.COM 	vnode_t		*startvp;
1523*12789SRoger.Faulkner@Oracle.COM 	vnode_t		*vp;
1524*12789SRoger.Faulkner@Oracle.COM 	int 		error;
1525*12789SRoger.Faulkner@Oracle.COM 
1526*12789SRoger.Faulkner@Oracle.COM 	/*
1527*12789SRoger.Faulkner@Oracle.COM 	 * Since we are never called to set the size of a file, we don't
1528*12789SRoger.Faulkner@Oracle.COM 	 * need to check for non-blocking locks (via nbl_need_check(vp)).
1529*12789SRoger.Faulkner@Oracle.COM 	 */
1530*12789SRoger.Faulkner@Oracle.COM 	ASSERT(!(vap->va_mask & AT_SIZE));
1531*12789SRoger.Faulkner@Oracle.COM 
1532*12789SRoger.Faulkner@Oracle.COM 	if ((error = fgetstartvp(fd, path, &startvp)) != 0)
1533*12789SRoger.Faulkner@Oracle.COM 		return (error);
1534*12789SRoger.Faulkner@Oracle.COM 	if (AU_AUDITING() && startvp != NULL)
1535*12789SRoger.Faulkner@Oracle.COM 		audit_setfsat_path(1);
1536*12789SRoger.Faulkner@Oracle.COM 
1537*12789SRoger.Faulkner@Oracle.COM 	/*
1538*12789SRoger.Faulkner@Oracle.COM 	 * Do lookup for fchownat/fchmodat when path not NULL
1539*12789SRoger.Faulkner@Oracle.COM 	 */
1540*12789SRoger.Faulkner@Oracle.COM 	if (path != NULL) {
1541*12789SRoger.Faulkner@Oracle.COM 		if (error = lookupnameat(path, UIO_USERSPACE,
1542*12789SRoger.Faulkner@Oracle.COM 		    (flags == AT_SYMLINK_NOFOLLOW) ?
1543*12789SRoger.Faulkner@Oracle.COM 		    NO_FOLLOW : FOLLOW,
1544*12789SRoger.Faulkner@Oracle.COM 		    NULLVPP, &vp, startvp)) {
1545*12789SRoger.Faulkner@Oracle.COM 			if (startvp != NULL)
1546*12789SRoger.Faulkner@Oracle.COM 				VN_RELE(startvp);
1547*12789SRoger.Faulkner@Oracle.COM 			return (error);
1548*12789SRoger.Faulkner@Oracle.COM 		}
1549*12789SRoger.Faulkner@Oracle.COM 	} else {
1550*12789SRoger.Faulkner@Oracle.COM 		vp = startvp;
1551*12789SRoger.Faulkner@Oracle.COM 		ASSERT(vp);
1552*12789SRoger.Faulkner@Oracle.COM 		VN_HOLD(vp);
1553*12789SRoger.Faulkner@Oracle.COM 	}
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	if (vn_is_readonly(vp)) {
15560Sstevel@tonic-gate 		error = EROFS;
1557*12789SRoger.Faulkner@Oracle.COM 	} else {
1558*12789SRoger.Faulkner@Oracle.COM 		error = VOP_SETATTR(vp, vap, 0, CRED(), NULL);
15590Sstevel@tonic-gate 	}
15600Sstevel@tonic-gate 
1561*12789SRoger.Faulkner@Oracle.COM 	if (startvp != NULL)
1562*12789SRoger.Faulkner@Oracle.COM 		VN_RELE(startvp);
1563*12789SRoger.Faulkner@Oracle.COM 	VN_RELE(vp);
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	return (error);
15660Sstevel@tonic-gate }
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate /*
15690Sstevel@tonic-gate  * Return true if the given vnode is referenced by any
15700Sstevel@tonic-gate  * entry in the current process's file descriptor table.
15710Sstevel@tonic-gate  */
15720Sstevel@tonic-gate int
fisopen(vnode_t * vp)15730Sstevel@tonic-gate fisopen(vnode_t *vp)
15740Sstevel@tonic-gate {
15750Sstevel@tonic-gate 	int fd;
15760Sstevel@tonic-gate 	file_t *fp;
15770Sstevel@tonic-gate 	vnode_t *ovp;
15780Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
15790Sstevel@tonic-gate 	uf_entry_t *ufp;
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate 	mutex_enter(&fip->fi_lock);
15820Sstevel@tonic-gate 	for (fd = 0; fd < fip->fi_nfiles; fd++) {
15830Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
15840Sstevel@tonic-gate 		if ((fp = ufp->uf_file) != NULL &&
15850Sstevel@tonic-gate 		    (ovp = fp->f_vnode) != NULL && VN_CMP(vp, ovp)) {
15860Sstevel@tonic-gate 			UF_EXIT(ufp);
15870Sstevel@tonic-gate 			mutex_exit(&fip->fi_lock);
15880Sstevel@tonic-gate 			return (1);
15890Sstevel@tonic-gate 		}
15900Sstevel@tonic-gate 		UF_EXIT(ufp);
15910Sstevel@tonic-gate 	}
15920Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
15930Sstevel@tonic-gate 	return (0);
15940Sstevel@tonic-gate }
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate /*
15970Sstevel@tonic-gate  * Return zero if at least one file currently open (by curproc) shouldn't be
15980Sstevel@tonic-gate  * allowed to change zones.
15990Sstevel@tonic-gate  */
16000Sstevel@tonic-gate int
files_can_change_zones(void)16010Sstevel@tonic-gate files_can_change_zones(void)
16020Sstevel@tonic-gate {
16030Sstevel@tonic-gate 	int fd;
16040Sstevel@tonic-gate 	file_t *fp;
16050Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
16060Sstevel@tonic-gate 	uf_entry_t *ufp;
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 	mutex_enter(&fip->fi_lock);
16090Sstevel@tonic-gate 	for (fd = 0; fd < fip->fi_nfiles; fd++) {
16100Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
16110Sstevel@tonic-gate 		if ((fp = ufp->uf_file) != NULL &&
16120Sstevel@tonic-gate 		    !vn_can_change_zones(fp->f_vnode)) {
16130Sstevel@tonic-gate 			UF_EXIT(ufp);
16140Sstevel@tonic-gate 			mutex_exit(&fip->fi_lock);
16150Sstevel@tonic-gate 			return (0);
16160Sstevel@tonic-gate 		}
16170Sstevel@tonic-gate 		UF_EXIT(ufp);
16180Sstevel@tonic-gate 	}
16190Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
16200Sstevel@tonic-gate 	return (1);
16210Sstevel@tonic-gate }
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate #ifdef DEBUG
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate /*
16260Sstevel@tonic-gate  * The following functions are only used in ASSERT()s elsewhere.
16270Sstevel@tonic-gate  * They do not modify the state of the system.
16280Sstevel@tonic-gate  */
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate /*
16310Sstevel@tonic-gate  * Return true (1) if the current thread is in the fpollinfo
16320Sstevel@tonic-gate  * list for this file descriptor, else false (0).
16330Sstevel@tonic-gate  */
16340Sstevel@tonic-gate static int
curthread_in_plist(uf_entry_t * ufp)16350Sstevel@tonic-gate curthread_in_plist(uf_entry_t *ufp)
16360Sstevel@tonic-gate {
16370Sstevel@tonic-gate 	fpollinfo_t *fpip;
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ufp->uf_lock));
16400Sstevel@tonic-gate 	for (fpip = ufp->uf_fpollinfo; fpip; fpip = fpip->fp_next)
16410Sstevel@tonic-gate 		if (fpip->fp_thread == curthread)
16420Sstevel@tonic-gate 			return (1);
16430Sstevel@tonic-gate 	return (0);
16440Sstevel@tonic-gate }
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate /*
16470Sstevel@tonic-gate  * Sanity check to make sure that after lwp_exit(),
16480Sstevel@tonic-gate  * curthread does not appear on any fd's fpollinfo list.
16490Sstevel@tonic-gate  */
16500Sstevel@tonic-gate void
checkfpollinfo(void)16510Sstevel@tonic-gate checkfpollinfo(void)
16520Sstevel@tonic-gate {
16530Sstevel@tonic-gate 	int fd;
16540Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
16550Sstevel@tonic-gate 	uf_entry_t *ufp;
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 	mutex_enter(&fip->fi_lock);
16580Sstevel@tonic-gate 	for (fd = 0; fd < fip->fi_nfiles; fd++) {
16590Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
16600Sstevel@tonic-gate 		ASSERT(!curthread_in_plist(ufp));
16610Sstevel@tonic-gate 		UF_EXIT(ufp);
16620Sstevel@tonic-gate 	}
16630Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
16640Sstevel@tonic-gate }
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate /*
16670Sstevel@tonic-gate  * Return true (1) if the current thread is in the fpollinfo
16680Sstevel@tonic-gate  * list for this file descriptor, else false (0).
16690Sstevel@tonic-gate  * This is the same as curthread_in_plist(),
16700Sstevel@tonic-gate  * but is called w/o holding uf_lock.
16710Sstevel@tonic-gate  */
16720Sstevel@tonic-gate int
infpollinfo(int fd)16730Sstevel@tonic-gate infpollinfo(int fd)
16740Sstevel@tonic-gate {
16750Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
16760Sstevel@tonic-gate 	uf_entry_t *ufp;
16770Sstevel@tonic-gate 	int rc;
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
16800Sstevel@tonic-gate 	rc = curthread_in_plist(ufp);
16810Sstevel@tonic-gate 	UF_EXIT(ufp);
16820Sstevel@tonic-gate 	return (rc);
16830Sstevel@tonic-gate }
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate #endif	/* DEBUG */
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate /*
16880Sstevel@tonic-gate  * Add the curthread to fpollinfo list, meaning this fd is currently in the
16890Sstevel@tonic-gate  * thread's poll cache. Each lwp polling this file descriptor should call
16900Sstevel@tonic-gate  * this routine once.
16910Sstevel@tonic-gate  */
16920Sstevel@tonic-gate void
addfpollinfo(int fd)16930Sstevel@tonic-gate addfpollinfo(int fd)
16940Sstevel@tonic-gate {
16950Sstevel@tonic-gate 	struct uf_entry *ufp;
16960Sstevel@tonic-gate 	fpollinfo_t *fpip;
16970Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 	fpip = kmem_zalloc(sizeof (fpollinfo_t), KM_SLEEP);
17000Sstevel@tonic-gate 	fpip->fp_thread = curthread;
17010Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
17020Sstevel@tonic-gate 	/*
17030Sstevel@tonic-gate 	 * Assert we are not already on the list, that is, that
17040Sstevel@tonic-gate 	 * this lwp did not call addfpollinfo twice for the same fd.
17050Sstevel@tonic-gate 	 */
17060Sstevel@tonic-gate 	ASSERT(!curthread_in_plist(ufp));
17070Sstevel@tonic-gate 	/*
17080Sstevel@tonic-gate 	 * addfpollinfo is always done inside the getf/releasef pair.
17090Sstevel@tonic-gate 	 */
17100Sstevel@tonic-gate 	ASSERT(ufp->uf_refcnt >= 1);
17110Sstevel@tonic-gate 	fpip->fp_next = ufp->uf_fpollinfo;
17120Sstevel@tonic-gate 	ufp->uf_fpollinfo = fpip;
17130Sstevel@tonic-gate 	UF_EXIT(ufp);
17140Sstevel@tonic-gate }
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate /*
171712290SRoger.Faulkner@Oracle.COM  * Delete curthread from fpollinfo list if it is there.
17180Sstevel@tonic-gate  */
17190Sstevel@tonic-gate void
delfpollinfo(int fd)17200Sstevel@tonic-gate delfpollinfo(int fd)
17210Sstevel@tonic-gate {
17220Sstevel@tonic-gate 	struct uf_entry *ufp;
17230Sstevel@tonic-gate 	struct fpollinfo *fpip;
17240Sstevel@tonic-gate 	struct fpollinfo **fpipp;
17250Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
172812290SRoger.Faulkner@Oracle.COM 	for (fpipp = &ufp->uf_fpollinfo;
172912290SRoger.Faulkner@Oracle.COM 	    (fpip = *fpipp) != NULL;
173012290SRoger.Faulkner@Oracle.COM 	    fpipp = &fpip->fp_next) {
173112290SRoger.Faulkner@Oracle.COM 		if (fpip->fp_thread == curthread) {
173212290SRoger.Faulkner@Oracle.COM 			*fpipp = fpip->fp_next;
173312290SRoger.Faulkner@Oracle.COM 			kmem_free(fpip, sizeof (fpollinfo_t));
173412290SRoger.Faulkner@Oracle.COM 			break;
173512290SRoger.Faulkner@Oracle.COM 		}
17360Sstevel@tonic-gate 	}
17370Sstevel@tonic-gate 	/*
17380Sstevel@tonic-gate 	 * Assert that we are not still on the list, that is, that
17390Sstevel@tonic-gate 	 * this lwp did not call addfpollinfo twice for the same fd.
17400Sstevel@tonic-gate 	 */
17410Sstevel@tonic-gate 	ASSERT(!curthread_in_plist(ufp));
17420Sstevel@tonic-gate 	UF_EXIT(ufp);
17430Sstevel@tonic-gate }
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate /*
17460Sstevel@tonic-gate  * fd is associated with a port. pfd is a pointer to the fd entry in the
17470Sstevel@tonic-gate  * cache of the port.
17480Sstevel@tonic-gate  */
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate void
addfd_port(int fd,portfd_t * pfd)17510Sstevel@tonic-gate addfd_port(int fd, portfd_t *pfd)
17520Sstevel@tonic-gate {
17530Sstevel@tonic-gate 	struct uf_entry *ufp;
17540Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
17570Sstevel@tonic-gate 	/*
17580Sstevel@tonic-gate 	 * addfd_port is always done inside the getf/releasef pair.
17590Sstevel@tonic-gate 	 */
17600Sstevel@tonic-gate 	ASSERT(ufp->uf_refcnt >= 1);
17610Sstevel@tonic-gate 	if (ufp->uf_portfd == NULL) {
17620Sstevel@tonic-gate 		/* first entry */
17630Sstevel@tonic-gate 		ufp->uf_portfd = pfd;
17640Sstevel@tonic-gate 		pfd->pfd_next = NULL;
17650Sstevel@tonic-gate 	} else {
17660Sstevel@tonic-gate 		pfd->pfd_next = ufp->uf_portfd;
17670Sstevel@tonic-gate 		ufp->uf_portfd = pfd;
17680Sstevel@tonic-gate 		pfd->pfd_next->pfd_prev = pfd;
17690Sstevel@tonic-gate 	}
17700Sstevel@tonic-gate 	UF_EXIT(ufp);
17710Sstevel@tonic-gate }
17720Sstevel@tonic-gate 
17730Sstevel@tonic-gate void
delfd_port(int fd,portfd_t * pfd)17740Sstevel@tonic-gate delfd_port(int fd, portfd_t *pfd)
17750Sstevel@tonic-gate {
17760Sstevel@tonic-gate 	struct uf_entry *ufp;
17770Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
17800Sstevel@tonic-gate 	/*
17810Sstevel@tonic-gate 	 * delfd_port is always done inside the getf/releasef pair.
17820Sstevel@tonic-gate 	 */
17830Sstevel@tonic-gate 	ASSERT(ufp->uf_refcnt >= 1);
17840Sstevel@tonic-gate 	if (ufp->uf_portfd == pfd) {
17850Sstevel@tonic-gate 		/* remove first entry */
17860Sstevel@tonic-gate 		ufp->uf_portfd = pfd->pfd_next;
17870Sstevel@tonic-gate 	} else {
17880Sstevel@tonic-gate 		pfd->pfd_prev->pfd_next = pfd->pfd_next;
17890Sstevel@tonic-gate 		if (pfd->pfd_next != NULL)
17900Sstevel@tonic-gate 			pfd->pfd_next->pfd_prev = pfd->pfd_prev;
17910Sstevel@tonic-gate 	}
17920Sstevel@tonic-gate 	UF_EXIT(ufp);
17930Sstevel@tonic-gate }
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate static void
port_close_fd(portfd_t * pfd)17961425Spraks port_close_fd(portfd_t *pfd)
17970Sstevel@tonic-gate {
17980Sstevel@tonic-gate 	portfd_t	*pfdn;
17990Sstevel@tonic-gate 
18001425Spraks 	/*
18011425Spraks 	 * At this point, no other thread should access
18021425Spraks 	 * the portfd_t list for this fd. The uf_file, uf_portfd
18031425Spraks 	 * pointers in the uf_entry_t struct for this fd would
18041425Spraks 	 * be set to NULL.
18051425Spraks 	 */
18060Sstevel@tonic-gate 	for (; pfd != NULL; pfd = pfdn) {
18070Sstevel@tonic-gate 		pfdn = pfd->pfd_next;
18080Sstevel@tonic-gate 		port_close_pfd(pfd);
18090Sstevel@tonic-gate 	}
18100Sstevel@tonic-gate }
1811