xref: /titanic_53/usr/src/uts/common/os/fio.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	All Rights Reserved */
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/signal.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/user.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/var.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/open.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/procset.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/poll.h>
58*7c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
59*7c478bd9Sstevel@tonic-gate #include <sys/port_impl.h>
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate #include <c2/audit.h>
62*7c478bd9Sstevel@tonic-gate #include <sys/nbmlock.h>
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate static uint32_t afd_maxfd;	/* # of entries in maximum allocated array */
67*7c478bd9Sstevel@tonic-gate static uint32_t afd_alloc;	/* count of kmem_alloc()s */
68*7c478bd9Sstevel@tonic-gate static uint32_t afd_free;	/* count of kmem_free()s */
69*7c478bd9Sstevel@tonic-gate static uint32_t afd_wait;	/* count of waits on non-zero ref count */
70*7c478bd9Sstevel@tonic-gate #define	MAXFD(x)	(afd_maxfd = ((afd_maxfd >= (x))? afd_maxfd : (x)))
71*7c478bd9Sstevel@tonic-gate #define	COUNT(x)	atomic_add_32(&x, 1)
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate #else	/* DEBUG */
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate #define	MAXFD(x)
76*7c478bd9Sstevel@tonic-gate #define	COUNT(x)
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate kmem_cache_t *file_cache;
81*7c478bd9Sstevel@tonic-gate static int vpsetattr(vnode_t *, vattr_t *, int);
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate static void port_close_fd(portfd_t *, int);
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate /*
86*7c478bd9Sstevel@tonic-gate  * File descriptor allocation.
87*7c478bd9Sstevel@tonic-gate  *
88*7c478bd9Sstevel@tonic-gate  * fd_find(fip, minfd) finds the first available descriptor >= minfd.
89*7c478bd9Sstevel@tonic-gate  * The most common case is open(2), in which minfd = 0, but we must also
90*7c478bd9Sstevel@tonic-gate  * support fcntl(fd, F_DUPFD, minfd).
91*7c478bd9Sstevel@tonic-gate  *
92*7c478bd9Sstevel@tonic-gate  * The algorithm is as follows: we keep all file descriptors in an infix
93*7c478bd9Sstevel@tonic-gate  * binary tree in which each node records the number of descriptors
94*7c478bd9Sstevel@tonic-gate  * allocated in its right subtree, including itself.  Starting at minfd,
95*7c478bd9Sstevel@tonic-gate  * we ascend the tree until we find a non-fully allocated right subtree.
96*7c478bd9Sstevel@tonic-gate  * We then descend that subtree in a binary search for the smallest fd.
97*7c478bd9Sstevel@tonic-gate  * Finally, we ascend the tree again to increment the allocation count
98*7c478bd9Sstevel@tonic-gate  * of every subtree containing the newly-allocated fd.  Freeing an fd
99*7c478bd9Sstevel@tonic-gate  * requires only the last step: we ascend the tree to decrement allocation
100*7c478bd9Sstevel@tonic-gate  * counts.  Each of these three steps (ascent to find non-full subtree,
101*7c478bd9Sstevel@tonic-gate  * descent to find lowest fd, ascent to update allocation counts) is
102*7c478bd9Sstevel@tonic-gate  * O(log n), thus the algorithm as a whole is O(log n).
103*7c478bd9Sstevel@tonic-gate  *
104*7c478bd9Sstevel@tonic-gate  * We don't implement the fd tree using the customary left/right/parent
105*7c478bd9Sstevel@tonic-gate  * pointers, but instead take advantage of the glorious mathematics of
106*7c478bd9Sstevel@tonic-gate  * full infix binary trees.  For reference, here's an illustration of the
107*7c478bd9Sstevel@tonic-gate  * logical structure of such a tree, rooted at 4 (binary 100), covering
108*7c478bd9Sstevel@tonic-gate  * the range 1-7 (binary 001-111).  Our canonical trees do not include
109*7c478bd9Sstevel@tonic-gate  * fd 0; we'll deal with that later.
110*7c478bd9Sstevel@tonic-gate  *
111*7c478bd9Sstevel@tonic-gate  *	      100
112*7c478bd9Sstevel@tonic-gate  *	     /	 \
113*7c478bd9Sstevel@tonic-gate  *	    /	  \
114*7c478bd9Sstevel@tonic-gate  *	  010	  110
115*7c478bd9Sstevel@tonic-gate  *	  / \	  / \
116*7c478bd9Sstevel@tonic-gate  *	001 011 101 111
117*7c478bd9Sstevel@tonic-gate  *
118*7c478bd9Sstevel@tonic-gate  * We make the following observations, all of which are easily proven by
119*7c478bd9Sstevel@tonic-gate  * induction on the depth of the tree:
120*7c478bd9Sstevel@tonic-gate  *
121*7c478bd9Sstevel@tonic-gate  * (T1) The least-significant bit (LSB) of any node is equal to its level
122*7c478bd9Sstevel@tonic-gate  *      in the tree.  In our example, nodes 001, 011, 101 and 111 are at
123*7c478bd9Sstevel@tonic-gate  *      level 0; nodes 010 and 110 are at level 1; and node 100 is at level 2.
124*7c478bd9Sstevel@tonic-gate  *
125*7c478bd9Sstevel@tonic-gate  * (T2) The child size (CSIZE) of node N -- that is, the total number of
126*7c478bd9Sstevel@tonic-gate  *	right-branch descendants in a child of node N, including itself -- is
127*7c478bd9Sstevel@tonic-gate  *	given by clearing all but the least significant bit of N.  This
128*7c478bd9Sstevel@tonic-gate  *	follows immediately from (T1).  Applying this rule to our example, we
129*7c478bd9Sstevel@tonic-gate  *	see that CSIZE(100) = 100, CSIZE(x10) = 10, and CSIZE(xx1) = 1.
130*7c478bd9Sstevel@tonic-gate  *
131*7c478bd9Sstevel@tonic-gate  * (T3) The nearest left ancestor (LPARENT) of node N -- that is, the nearest
132*7c478bd9Sstevel@tonic-gate  *	ancestor containing node N in its right child -- is given by clearing
133*7c478bd9Sstevel@tonic-gate  *	the LSB of N.  For example, LPARENT(111) = 110 and LPARENT(110) = 100.
134*7c478bd9Sstevel@tonic-gate  *	Clearing the LSB of nodes 001, 010 or 100 yields zero, reflecting
135*7c478bd9Sstevel@tonic-gate  *	the fact that these are leftmost nodes.  Note that this algorithm
136*7c478bd9Sstevel@tonic-gate  *	automatically skips generations as necessary.  For example, the parent
137*7c478bd9Sstevel@tonic-gate  *      of node 101 is 110, which is a *right* ancestor (not what we want);
138*7c478bd9Sstevel@tonic-gate  *      but its grandparent is 100, which is a left ancestor. Clearing the LSB
139*7c478bd9Sstevel@tonic-gate  *      of 101 gets us to 100 directly, skipping right past the uninteresting
140*7c478bd9Sstevel@tonic-gate  *      generation (110).
141*7c478bd9Sstevel@tonic-gate  *
142*7c478bd9Sstevel@tonic-gate  *      Note that since LPARENT clears the LSB, whereas CSIZE clears all *but*
143*7c478bd9Sstevel@tonic-gate  *	the LSB, we can express LPARENT() nicely in terms of CSIZE():
144*7c478bd9Sstevel@tonic-gate  *
145*7c478bd9Sstevel@tonic-gate  *	LPARENT(N) = N - CSIZE(N)
146*7c478bd9Sstevel@tonic-gate  *
147*7c478bd9Sstevel@tonic-gate  * (T4) The nearest right ancestor (RPARENT) of node N is given by:
148*7c478bd9Sstevel@tonic-gate  *
149*7c478bd9Sstevel@tonic-gate  *	RPARENT(N) = N + CSIZE(N)
150*7c478bd9Sstevel@tonic-gate  *
151*7c478bd9Sstevel@tonic-gate  * (T5) For every interior node, the children differ from their parent by
152*7c478bd9Sstevel@tonic-gate  *	CSIZE(parent) / 2.  In our example, CSIZE(100) / 2 = 2 = 10 binary,
153*7c478bd9Sstevel@tonic-gate  *      and indeed, the children of 100 are 100 +/- 10 = 010 and 110.
154*7c478bd9Sstevel@tonic-gate  *
155*7c478bd9Sstevel@tonic-gate  * Next, we'll need a few two's-complement math tricks.  Suppose a number,
156*7c478bd9Sstevel@tonic-gate  * N, has the following form:
157*7c478bd9Sstevel@tonic-gate  *
158*7c478bd9Sstevel@tonic-gate  *		N = xxxx10...0
159*7c478bd9Sstevel@tonic-gate  *
160*7c478bd9Sstevel@tonic-gate  * That is, the binary representation of N consists of some string of bits,
161*7c478bd9Sstevel@tonic-gate  * then a 1, then all zeroes.  This amounts to nothing more than saying that
162*7c478bd9Sstevel@tonic-gate  * N has a least-significant bit, which is true for any N != 0.  If we look
163*7c478bd9Sstevel@tonic-gate  * at N and N - 1 together, we see that we can combine them in useful ways:
164*7c478bd9Sstevel@tonic-gate  *
165*7c478bd9Sstevel@tonic-gate  *		  N = xxxx10...0
166*7c478bd9Sstevel@tonic-gate  *	      N - 1 = xxxx01...1
167*7c478bd9Sstevel@tonic-gate  *	------------------------
168*7c478bd9Sstevel@tonic-gate  *	N & (N - 1) = xxxx000000
169*7c478bd9Sstevel@tonic-gate  *	N | (N - 1) = xxxx111111
170*7c478bd9Sstevel@tonic-gate  *	N ^ (N - 1) =     111111
171*7c478bd9Sstevel@tonic-gate  *
172*7c478bd9Sstevel@tonic-gate  * In particular, this suggests several easy ways to clear all but the LSB,
173*7c478bd9Sstevel@tonic-gate  * which by (T2) is exactly what we need to determine CSIZE(N) = 10...0.
174*7c478bd9Sstevel@tonic-gate  * We'll opt for this formulation:
175*7c478bd9Sstevel@tonic-gate  *
176*7c478bd9Sstevel@tonic-gate  *	(C1) CSIZE(N) = (N - 1) ^ (N | (N - 1))
177*7c478bd9Sstevel@tonic-gate  *
178*7c478bd9Sstevel@tonic-gate  * Similarly, we have an easy way to determine LPARENT(N), which requires
179*7c478bd9Sstevel@tonic-gate  * that we clear the LSB of N:
180*7c478bd9Sstevel@tonic-gate  *
181*7c478bd9Sstevel@tonic-gate  *	(L1) LPARENT(N) = N & (N - 1)
182*7c478bd9Sstevel@tonic-gate  *
183*7c478bd9Sstevel@tonic-gate  * We note in the above relations that (N | (N - 1)) - N = CSIZE(N) - 1.
184*7c478bd9Sstevel@tonic-gate  * When combined with (T4), this yields an easy way to compute RPARENT(N):
185*7c478bd9Sstevel@tonic-gate  *
186*7c478bd9Sstevel@tonic-gate  *	(R1) RPARENT(N) = (N | (N - 1)) + 1
187*7c478bd9Sstevel@tonic-gate  *
188*7c478bd9Sstevel@tonic-gate  * Finally, to accommodate fd 0 we must adjust all of our results by +/-1 to
189*7c478bd9Sstevel@tonic-gate  * move the fd range from [1, 2^n) to [0, 2^n - 1).  This is straightforward,
190*7c478bd9Sstevel@tonic-gate  * so there's no need to belabor the algebra; the revised relations become:
191*7c478bd9Sstevel@tonic-gate  *
192*7c478bd9Sstevel@tonic-gate  *	(C1a) CSIZE(N) = N ^ (N | (N + 1))
193*7c478bd9Sstevel@tonic-gate  *
194*7c478bd9Sstevel@tonic-gate  *	(L1a) LPARENT(N) = (N & (N + 1)) - 1
195*7c478bd9Sstevel@tonic-gate  *
196*7c478bd9Sstevel@tonic-gate  *	(R1a) RPARENT(N) = N | (N + 1)
197*7c478bd9Sstevel@tonic-gate  *
198*7c478bd9Sstevel@tonic-gate  * This completes the mathematical framework.  We now have all the tools
199*7c478bd9Sstevel@tonic-gate  * we need to implement fd_find() and fd_reserve().
200*7c478bd9Sstevel@tonic-gate  *
201*7c478bd9Sstevel@tonic-gate  * fd_find(fip, minfd) finds the smallest available file descriptor >= minfd.
202*7c478bd9Sstevel@tonic-gate  * It does not actually allocate the descriptor; that's done by fd_reserve().
203*7c478bd9Sstevel@tonic-gate  * fd_find() proceeds in two steps:
204*7c478bd9Sstevel@tonic-gate  *
205*7c478bd9Sstevel@tonic-gate  * (1) Find the leftmost subtree that contains a descriptor >= minfd.
206*7c478bd9Sstevel@tonic-gate  *     We start at the right subtree rooted at minfd.  If this subtree is
207*7c478bd9Sstevel@tonic-gate  *     not full -- if fip->fi_list[minfd].uf_alloc != CSIZE(minfd) -- then
208*7c478bd9Sstevel@tonic-gate  *     step 1 is done.  Otherwise, we know that all fds in this subtree
209*7c478bd9Sstevel@tonic-gate  *     are taken, so we ascend to RPARENT(minfd) using (R1a).  We repeat
210*7c478bd9Sstevel@tonic-gate  *     this process until we either find a candidate subtree or exceed
211*7c478bd9Sstevel@tonic-gate  *     fip->fi_nfiles.  We use (C1a) to compute CSIZE().
212*7c478bd9Sstevel@tonic-gate  *
213*7c478bd9Sstevel@tonic-gate  * (2) Find the smallest fd in the subtree discovered by step 1.
214*7c478bd9Sstevel@tonic-gate  *     Starting at the root of this subtree, we descend to find the
215*7c478bd9Sstevel@tonic-gate  *     smallest available fd.  Since the left children have the smaller
216*7c478bd9Sstevel@tonic-gate  *     fds, we will descend rightward only when the left child is full.
217*7c478bd9Sstevel@tonic-gate  *
218*7c478bd9Sstevel@tonic-gate  *     We begin by comparing the number of allocated fds in the root
219*7c478bd9Sstevel@tonic-gate  *     to the number of allocated fds in its right child; if they differ
220*7c478bd9Sstevel@tonic-gate  *     by exactly CSIZE(child), we know the left subtree is full, so we
221*7c478bd9Sstevel@tonic-gate  *     descend right; that is, the right child becomes the search root.
222*7c478bd9Sstevel@tonic-gate  *     Otherwise we leave the root alone and start following the right
223*7c478bd9Sstevel@tonic-gate  *     child's left children.  As fortune would have it, this is very
224*7c478bd9Sstevel@tonic-gate  *     simple computationally: by (T5), the right child of fd is just
225*7c478bd9Sstevel@tonic-gate  *     fd + size, where size = CSIZE(fd) / 2.  Applying (T5) again,
226*7c478bd9Sstevel@tonic-gate  *     we find that the right child's left child is fd + size - (size / 2) =
227*7c478bd9Sstevel@tonic-gate  *     fd + (size / 2); *its* left child is fd + (size / 2) - (size / 4) =
228*7c478bd9Sstevel@tonic-gate  *     fd + (size / 4), and so on.  In general, fd's right child's
229*7c478bd9Sstevel@tonic-gate  *     leftmost nth descendant is fd + (size >> n).  Thus, to follow
230*7c478bd9Sstevel@tonic-gate  *     the right child's left descendants, we just halve the size in
231*7c478bd9Sstevel@tonic-gate  *     each iteration of the search.
232*7c478bd9Sstevel@tonic-gate  *
233*7c478bd9Sstevel@tonic-gate  *     When we descend leftward, we must keep track of the number of fds
234*7c478bd9Sstevel@tonic-gate  *     that were allocated in all the right subtrees we rejected, so we
235*7c478bd9Sstevel@tonic-gate  *     know how many of the root fd's allocations are in the remaining
236*7c478bd9Sstevel@tonic-gate  *     (as yet unexplored) leftmost part of its right subtree.  When we
237*7c478bd9Sstevel@tonic-gate  *     encounter a fully-allocated left child -- that is, when we find
238*7c478bd9Sstevel@tonic-gate  *     that fip->fi_list[fd].uf_alloc == ralloc + size -- we descend right
239*7c478bd9Sstevel@tonic-gate  *     (as described earlier), resetting ralloc to zero.
240*7c478bd9Sstevel@tonic-gate  *
241*7c478bd9Sstevel@tonic-gate  * fd_reserve(fip, fd, incr) either allocates or frees fd, depending
242*7c478bd9Sstevel@tonic-gate  * on whether incr is 1 or -1.  Starting at fd, fd_reserve() ascends
243*7c478bd9Sstevel@tonic-gate  * the leftmost ancestors (see (T3)) and updates the allocation counts.
244*7c478bd9Sstevel@tonic-gate  * At each step we use (L1a) to compute LPARENT(), the next left ancestor.
245*7c478bd9Sstevel@tonic-gate  *
246*7c478bd9Sstevel@tonic-gate  * flist_minsize() finds the minimal tree that still covers all
247*7c478bd9Sstevel@tonic-gate  * used fds; as long as the allocation count of a root node is zero, we
248*7c478bd9Sstevel@tonic-gate  * don't need that node or its right subtree.
249*7c478bd9Sstevel@tonic-gate  *
250*7c478bd9Sstevel@tonic-gate  * flist_nalloc() counts the number of allocated fds in the tree, by starting
251*7c478bd9Sstevel@tonic-gate  * at the top of the tree and summing the right-subtree allocation counts as
252*7c478bd9Sstevel@tonic-gate  * it descends leftwards.
253*7c478bd9Sstevel@tonic-gate  *
254*7c478bd9Sstevel@tonic-gate  * Note: we assume that flist_grow() will keep fip->fi_nfiles of the form
255*7c478bd9Sstevel@tonic-gate  * 2^n - 1.  This ensures that the fd trees are always full, which saves
256*7c478bd9Sstevel@tonic-gate  * quite a bit of boundary checking.
257*7c478bd9Sstevel@tonic-gate  */
258*7c478bd9Sstevel@tonic-gate static int
259*7c478bd9Sstevel@tonic-gate fd_find(uf_info_t *fip, int minfd)
260*7c478bd9Sstevel@tonic-gate {
261*7c478bd9Sstevel@tonic-gate 	int size, ralloc, fd;
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fip->fi_lock));
264*7c478bd9Sstevel@tonic-gate 	ASSERT((fip->fi_nfiles & (fip->fi_nfiles + 1)) == 0);
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 	for (fd = minfd; (uint_t)fd < fip->fi_nfiles; fd |= fd + 1) {
267*7c478bd9Sstevel@tonic-gate 		size = fd ^ (fd | (fd + 1));
268*7c478bd9Sstevel@tonic-gate 		if (fip->fi_list[fd].uf_alloc == size)
269*7c478bd9Sstevel@tonic-gate 			continue;
270*7c478bd9Sstevel@tonic-gate 		for (ralloc = 0, size >>= 1; size != 0; size >>= 1) {
271*7c478bd9Sstevel@tonic-gate 			ralloc += fip->fi_list[fd + size].uf_alloc;
272*7c478bd9Sstevel@tonic-gate 			if (fip->fi_list[fd].uf_alloc == ralloc + size) {
273*7c478bd9Sstevel@tonic-gate 				fd += size;
274*7c478bd9Sstevel@tonic-gate 				ralloc = 0;
275*7c478bd9Sstevel@tonic-gate 			}
276*7c478bd9Sstevel@tonic-gate 		}
277*7c478bd9Sstevel@tonic-gate 		return (fd);
278*7c478bd9Sstevel@tonic-gate 	}
279*7c478bd9Sstevel@tonic-gate 	return (-1);
280*7c478bd9Sstevel@tonic-gate }
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate static void
283*7c478bd9Sstevel@tonic-gate fd_reserve(uf_info_t *fip, int fd, int incr)
284*7c478bd9Sstevel@tonic-gate {
285*7c478bd9Sstevel@tonic-gate 	int pfd;
286*7c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp = &fip->fi_list[fd];
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 	ASSERT((uint_t)fd < fip->fi_nfiles);
289*7c478bd9Sstevel@tonic-gate 	ASSERT((ufp->uf_busy == 0 && incr == 1) ||
290*7c478bd9Sstevel@tonic-gate 	    (ufp->uf_busy == 1 && incr == -1));
291*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ufp->uf_lock));
292*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fip->fi_lock));
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	for (pfd = fd; pfd >= 0; pfd = (pfd & (pfd + 1)) - 1)
295*7c478bd9Sstevel@tonic-gate 		fip->fi_list[pfd].uf_alloc += incr;
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate 	ufp->uf_busy += incr;
298*7c478bd9Sstevel@tonic-gate }
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate static int
301*7c478bd9Sstevel@tonic-gate flist_minsize(uf_info_t *fip)
302*7c478bd9Sstevel@tonic-gate {
303*7c478bd9Sstevel@tonic-gate 	int fd;
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate 	/*
306*7c478bd9Sstevel@tonic-gate 	 * We'd like to ASSERT(MUTEX_HELD(&fip->fi_lock)), but we're called
307*7c478bd9Sstevel@tonic-gate 	 * by flist_fork(), which relies on other mechanisms for mutual
308*7c478bd9Sstevel@tonic-gate 	 * exclusion.
309*7c478bd9Sstevel@tonic-gate 	 */
310*7c478bd9Sstevel@tonic-gate 	ASSERT((fip->fi_nfiles & (fip->fi_nfiles + 1)) == 0);
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 	for (fd = fip->fi_nfiles; fd != 0; fd >>= 1)
313*7c478bd9Sstevel@tonic-gate 		if (fip->fi_list[fd >> 1].uf_alloc != 0)
314*7c478bd9Sstevel@tonic-gate 			break;
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 	return (fd);
317*7c478bd9Sstevel@tonic-gate }
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate static int
320*7c478bd9Sstevel@tonic-gate flist_nalloc(uf_info_t *fip)
321*7c478bd9Sstevel@tonic-gate {
322*7c478bd9Sstevel@tonic-gate 	int fd;
323*7c478bd9Sstevel@tonic-gate 	int nalloc = 0;
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fip->fi_lock));
326*7c478bd9Sstevel@tonic-gate 	ASSERT((fip->fi_nfiles & (fip->fi_nfiles + 1)) == 0);
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate 	for (fd = fip->fi_nfiles; fd != 0; fd >>= 1)
329*7c478bd9Sstevel@tonic-gate 		nalloc += fip->fi_list[fd >> 1].uf_alloc;
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 	return (nalloc);
332*7c478bd9Sstevel@tonic-gate }
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate /*
335*7c478bd9Sstevel@tonic-gate  * Increase size of the fi_list array to accommodate at least maxfd.
336*7c478bd9Sstevel@tonic-gate  * We keep the size of the form 2^n - 1 for benefit of fd_find().
337*7c478bd9Sstevel@tonic-gate  */
338*7c478bd9Sstevel@tonic-gate static void
339*7c478bd9Sstevel@tonic-gate flist_grow(int maxfd)
340*7c478bd9Sstevel@tonic-gate {
341*7c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
342*7c478bd9Sstevel@tonic-gate 	int newcnt, oldcnt;
343*7c478bd9Sstevel@tonic-gate 	uf_entry_t *src, *dst, *newlist, *oldlist, *newend, *oldend;
344*7c478bd9Sstevel@tonic-gate 	uf_rlist_t *urp;
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate 	for (newcnt = 1; newcnt <= maxfd; newcnt = (newcnt << 1) | 1)
347*7c478bd9Sstevel@tonic-gate 		continue;
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate 	newlist = kmem_zalloc(newcnt * sizeof (uf_entry_t), KM_SLEEP);
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fip->fi_lock);
352*7c478bd9Sstevel@tonic-gate 	oldcnt = fip->fi_nfiles;
353*7c478bd9Sstevel@tonic-gate 	if (newcnt <= oldcnt) {
354*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fip->fi_lock);
355*7c478bd9Sstevel@tonic-gate 		kmem_free(newlist, newcnt * sizeof (uf_entry_t));
356*7c478bd9Sstevel@tonic-gate 		return;
357*7c478bd9Sstevel@tonic-gate 	}
358*7c478bd9Sstevel@tonic-gate 	ASSERT((newcnt & (newcnt + 1)) == 0);
359*7c478bd9Sstevel@tonic-gate 	oldlist = fip->fi_list;
360*7c478bd9Sstevel@tonic-gate 	oldend = oldlist + oldcnt;
361*7c478bd9Sstevel@tonic-gate 	newend = newlist + oldcnt;	/* no need to lock beyond old end */
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate 	/*
364*7c478bd9Sstevel@tonic-gate 	 * fi_list and fi_nfiles cannot change while any uf_lock is held,
365*7c478bd9Sstevel@tonic-gate 	 * so we must grab all the old locks *and* the new locks up to oldcnt.
366*7c478bd9Sstevel@tonic-gate 	 * (Locks beyond the end of oldcnt aren't visible until we store
367*7c478bd9Sstevel@tonic-gate 	 * the new fi_nfiles, which is the last thing we do before dropping
368*7c478bd9Sstevel@tonic-gate 	 * all the locks, so there's no need to acquire these locks).
369*7c478bd9Sstevel@tonic-gate 	 * Holding the new locks is necessary because when fi_list changes
370*7c478bd9Sstevel@tonic-gate 	 * to point to the new list, fi_nfiles won't have been stored yet.
371*7c478bd9Sstevel@tonic-gate 	 * If we *didn't* hold the new locks, someone doing a UF_ENTER()
372*7c478bd9Sstevel@tonic-gate 	 * could see the new fi_list, grab the new uf_lock, and then see
373*7c478bd9Sstevel@tonic-gate 	 * fi_nfiles change while the lock is held -- in violation of
374*7c478bd9Sstevel@tonic-gate 	 * UF_ENTER() semantics.
375*7c478bd9Sstevel@tonic-gate 	 */
376*7c478bd9Sstevel@tonic-gate 	for (src = oldlist; src < oldend; src++)
377*7c478bd9Sstevel@tonic-gate 		mutex_enter(&src->uf_lock);
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 	for (dst = newlist; dst < newend; dst++)
380*7c478bd9Sstevel@tonic-gate 		mutex_enter(&dst->uf_lock);
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate 	for (src = oldlist, dst = newlist; src < oldend; src++, dst++) {
383*7c478bd9Sstevel@tonic-gate 		dst->uf_file = src->uf_file;
384*7c478bd9Sstevel@tonic-gate 		dst->uf_fpollinfo = src->uf_fpollinfo;
385*7c478bd9Sstevel@tonic-gate 		dst->uf_refcnt = src->uf_refcnt;
386*7c478bd9Sstevel@tonic-gate 		dst->uf_alloc = src->uf_alloc;
387*7c478bd9Sstevel@tonic-gate 		dst->uf_flag = src->uf_flag;
388*7c478bd9Sstevel@tonic-gate 		dst->uf_busy = src->uf_busy;
389*7c478bd9Sstevel@tonic-gate 		dst->uf_portfd = src->uf_portfd;
390*7c478bd9Sstevel@tonic-gate 	}
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 	/*
393*7c478bd9Sstevel@tonic-gate 	 * As soon as we store the new flist, future locking operations
394*7c478bd9Sstevel@tonic-gate 	 * will use it.  Therefore, we must ensure that all the state
395*7c478bd9Sstevel@tonic-gate 	 * we've just established reaches global visibility before the
396*7c478bd9Sstevel@tonic-gate 	 * new flist does.
397*7c478bd9Sstevel@tonic-gate 	 */
398*7c478bd9Sstevel@tonic-gate 	membar_producer();
399*7c478bd9Sstevel@tonic-gate 	fip->fi_list = newlist;
400*7c478bd9Sstevel@tonic-gate 
401*7c478bd9Sstevel@tonic-gate 	/*
402*7c478bd9Sstevel@tonic-gate 	 * Routines like getf() make an optimistic check on the validity
403*7c478bd9Sstevel@tonic-gate 	 * of the supplied file descriptor: if it's less than the current
404*7c478bd9Sstevel@tonic-gate 	 * value of fi_nfiles -- examined without any locks -- then it's
405*7c478bd9Sstevel@tonic-gate 	 * safe to attempt a UF_ENTER() on that fd (which is a valid
406*7c478bd9Sstevel@tonic-gate 	 * assumption because fi_nfiles only increases).  Therefore, it
407*7c478bd9Sstevel@tonic-gate 	 * is critical that the new value of fi_nfiles not reach global
408*7c478bd9Sstevel@tonic-gate 	 * visibility until after the new fi_list: if it happened the
409*7c478bd9Sstevel@tonic-gate 	 * other way around, getf() could see the new fi_nfiles and attempt
410*7c478bd9Sstevel@tonic-gate 	 * a UF_ENTER() on the old fi_list, which would write beyond its
411*7c478bd9Sstevel@tonic-gate 	 * end if the fd exceeded the old fi_nfiles.
412*7c478bd9Sstevel@tonic-gate 	 */
413*7c478bd9Sstevel@tonic-gate 	membar_producer();
414*7c478bd9Sstevel@tonic-gate 	fip->fi_nfiles = newcnt;
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate 	/*
417*7c478bd9Sstevel@tonic-gate 	 * The new state is consistent now, so we can drop all the locks.
418*7c478bd9Sstevel@tonic-gate 	 */
419*7c478bd9Sstevel@tonic-gate 	for (dst = newlist; dst < newend; dst++)
420*7c478bd9Sstevel@tonic-gate 		mutex_exit(&dst->uf_lock);
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate 	for (src = oldlist; src < oldend; src++) {
423*7c478bd9Sstevel@tonic-gate 		/*
424*7c478bd9Sstevel@tonic-gate 		 * If any threads are blocked on the old cvs, wake them.
425*7c478bd9Sstevel@tonic-gate 		 * This will force them to wake up, discover that fi_list
426*7c478bd9Sstevel@tonic-gate 		 * has changed, and go back to sleep on the new cvs.
427*7c478bd9Sstevel@tonic-gate 		 */
428*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&src->uf_wanted_cv);
429*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&src->uf_closing_cv);
430*7c478bd9Sstevel@tonic-gate 		mutex_exit(&src->uf_lock);
431*7c478bd9Sstevel@tonic-gate 	}
432*7c478bd9Sstevel@tonic-gate 
433*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate 	/*
436*7c478bd9Sstevel@tonic-gate 	 * Retire the old flist.  We can't actually kmem_free() it now
437*7c478bd9Sstevel@tonic-gate 	 * because someone may still have a pointer to it.  Instead,
438*7c478bd9Sstevel@tonic-gate 	 * we link it onto a list of retired flists.  The new flist
439*7c478bd9Sstevel@tonic-gate 	 * is at least double the size of the previous flist, so the
440*7c478bd9Sstevel@tonic-gate 	 * total size of all retired flists will be less than the size
441*7c478bd9Sstevel@tonic-gate 	 * of the current one (to prove, consider the sum of a geometric
442*7c478bd9Sstevel@tonic-gate 	 * series in powers of 2).  exit() frees the retired flists.
443*7c478bd9Sstevel@tonic-gate 	 */
444*7c478bd9Sstevel@tonic-gate 	urp = kmem_zalloc(sizeof (uf_rlist_t), KM_SLEEP);
445*7c478bd9Sstevel@tonic-gate 	urp->ur_list = oldlist;
446*7c478bd9Sstevel@tonic-gate 	urp->ur_nfiles = oldcnt;
447*7c478bd9Sstevel@tonic-gate 
448*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fip->fi_lock);
449*7c478bd9Sstevel@tonic-gate 	urp->ur_next = fip->fi_rlist;
450*7c478bd9Sstevel@tonic-gate 	fip->fi_rlist = urp;
451*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
452*7c478bd9Sstevel@tonic-gate }
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate /*
455*7c478bd9Sstevel@tonic-gate  * Utility functions for keeping track of the active file descriptors.
456*7c478bd9Sstevel@tonic-gate  */
457*7c478bd9Sstevel@tonic-gate void
458*7c478bd9Sstevel@tonic-gate clear_stale_fd()		/* called from post_syscall() */
459*7c478bd9Sstevel@tonic-gate {
460*7c478bd9Sstevel@tonic-gate 	afd_t *afd = &curthread->t_activefd;
461*7c478bd9Sstevel@tonic-gate 	int i;
462*7c478bd9Sstevel@tonic-gate 
463*7c478bd9Sstevel@tonic-gate 	/* uninitialized is ok here, a_nfd is then zero */
464*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < afd->a_nfd; i++) {
465*7c478bd9Sstevel@tonic-gate 		/* assert that this should not be necessary */
466*7c478bd9Sstevel@tonic-gate 		ASSERT(afd->a_fd[i] == -1);
467*7c478bd9Sstevel@tonic-gate 		afd->a_fd[i] = -1;
468*7c478bd9Sstevel@tonic-gate 	}
469*7c478bd9Sstevel@tonic-gate 	afd->a_stale = 0;
470*7c478bd9Sstevel@tonic-gate }
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate void
473*7c478bd9Sstevel@tonic-gate free_afd(afd_t *afd)		/* called below and from thread_free() */
474*7c478bd9Sstevel@tonic-gate {
475*7c478bd9Sstevel@tonic-gate 	int i;
476*7c478bd9Sstevel@tonic-gate 
477*7c478bd9Sstevel@tonic-gate 	/* free the buffer if it was kmem_alloc()ed */
478*7c478bd9Sstevel@tonic-gate 	if (afd->a_nfd > sizeof (afd->a_buf) / sizeof (afd->a_buf[0])) {
479*7c478bd9Sstevel@tonic-gate 		COUNT(afd_free);
480*7c478bd9Sstevel@tonic-gate 		kmem_free(afd->a_fd, afd->a_nfd * sizeof (afd->a_fd[0]));
481*7c478bd9Sstevel@tonic-gate 	}
482*7c478bd9Sstevel@tonic-gate 
483*7c478bd9Sstevel@tonic-gate 	/* (re)initialize the structure */
484*7c478bd9Sstevel@tonic-gate 	afd->a_fd = &afd->a_buf[0];
485*7c478bd9Sstevel@tonic-gate 	afd->a_nfd = sizeof (afd->a_buf) / sizeof (afd->a_buf[0]);
486*7c478bd9Sstevel@tonic-gate 	afd->a_stale = 0;
487*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < afd->a_nfd; i++)
488*7c478bd9Sstevel@tonic-gate 		afd->a_fd[i] = -1;
489*7c478bd9Sstevel@tonic-gate }
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate static void
492*7c478bd9Sstevel@tonic-gate set_active_fd(int fd)
493*7c478bd9Sstevel@tonic-gate {
494*7c478bd9Sstevel@tonic-gate 	afd_t *afd = &curthread->t_activefd;
495*7c478bd9Sstevel@tonic-gate 	int i;
496*7c478bd9Sstevel@tonic-gate 	int *old_fd;
497*7c478bd9Sstevel@tonic-gate 	int old_nfd;
498*7c478bd9Sstevel@tonic-gate 
499*7c478bd9Sstevel@tonic-gate 	if (afd->a_nfd == 0)	/* first time initialization */
500*7c478bd9Sstevel@tonic-gate 		free_afd(afd);
501*7c478bd9Sstevel@tonic-gate 
502*7c478bd9Sstevel@tonic-gate 	/* insert fd into vacant slot, if any */
503*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < afd->a_nfd; i++) {
504*7c478bd9Sstevel@tonic-gate 		if (afd->a_fd[i] == -1) {
505*7c478bd9Sstevel@tonic-gate 			afd->a_fd[i] = fd;
506*7c478bd9Sstevel@tonic-gate 			return;
507*7c478bd9Sstevel@tonic-gate 		}
508*7c478bd9Sstevel@tonic-gate 	}
509*7c478bd9Sstevel@tonic-gate 
510*7c478bd9Sstevel@tonic-gate 	/*
511*7c478bd9Sstevel@tonic-gate 	 * Reallocate the a_fd[] array to add one more slot.
512*7c478bd9Sstevel@tonic-gate 	 */
513*7c478bd9Sstevel@tonic-gate 	old_fd = afd->a_fd;
514*7c478bd9Sstevel@tonic-gate 	old_nfd = afd->a_nfd;
515*7c478bd9Sstevel@tonic-gate 	afd->a_nfd = old_nfd + 1;
516*7c478bd9Sstevel@tonic-gate 	MAXFD(afd->a_nfd);
517*7c478bd9Sstevel@tonic-gate 	COUNT(afd_alloc);
518*7c478bd9Sstevel@tonic-gate 	afd->a_fd = kmem_alloc(afd->a_nfd * sizeof (afd->a_fd[0]), KM_SLEEP);
519*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < old_nfd; i++)
520*7c478bd9Sstevel@tonic-gate 		afd->a_fd[i] = old_fd[i];
521*7c478bd9Sstevel@tonic-gate 	afd->a_fd[i] = fd;
522*7c478bd9Sstevel@tonic-gate 
523*7c478bd9Sstevel@tonic-gate 	if (old_nfd > sizeof (afd->a_buf) / sizeof (afd->a_buf[0])) {
524*7c478bd9Sstevel@tonic-gate 		COUNT(afd_free);
525*7c478bd9Sstevel@tonic-gate 		kmem_free(old_fd, old_nfd * sizeof (afd->a_fd[0]));
526*7c478bd9Sstevel@tonic-gate 	}
527*7c478bd9Sstevel@tonic-gate }
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate void
530*7c478bd9Sstevel@tonic-gate clear_active_fd(int fd)		/* called below and from aio.c */
531*7c478bd9Sstevel@tonic-gate {
532*7c478bd9Sstevel@tonic-gate 	afd_t *afd = &curthread->t_activefd;
533*7c478bd9Sstevel@tonic-gate 	int i;
534*7c478bd9Sstevel@tonic-gate 
535*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < afd->a_nfd; i++) {
536*7c478bd9Sstevel@tonic-gate 		if (afd->a_fd[i] == fd) {
537*7c478bd9Sstevel@tonic-gate 			afd->a_fd[i] = -1;
538*7c478bd9Sstevel@tonic-gate 			break;
539*7c478bd9Sstevel@tonic-gate 		}
540*7c478bd9Sstevel@tonic-gate 	}
541*7c478bd9Sstevel@tonic-gate 	ASSERT(i < afd->a_nfd);		/* not found is not ok */
542*7c478bd9Sstevel@tonic-gate }
543*7c478bd9Sstevel@tonic-gate 
544*7c478bd9Sstevel@tonic-gate /*
545*7c478bd9Sstevel@tonic-gate  * Does this thread have this fd active?
546*7c478bd9Sstevel@tonic-gate  */
547*7c478bd9Sstevel@tonic-gate static int
548*7c478bd9Sstevel@tonic-gate is_active_fd(kthread_t *t, int fd)
549*7c478bd9Sstevel@tonic-gate {
550*7c478bd9Sstevel@tonic-gate 	afd_t *afd = &t->t_activefd;
551*7c478bd9Sstevel@tonic-gate 	int i;
552*7c478bd9Sstevel@tonic-gate 
553*7c478bd9Sstevel@tonic-gate 	/* uninitialized is ok here, a_nfd is then zero */
554*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < afd->a_nfd; i++) {
555*7c478bd9Sstevel@tonic-gate 		if (afd->a_fd[i] == fd)
556*7c478bd9Sstevel@tonic-gate 			return (1);
557*7c478bd9Sstevel@tonic-gate 	}
558*7c478bd9Sstevel@tonic-gate 	return (0);
559*7c478bd9Sstevel@tonic-gate }
560*7c478bd9Sstevel@tonic-gate 
561*7c478bd9Sstevel@tonic-gate /*
562*7c478bd9Sstevel@tonic-gate  * Convert a user supplied file descriptor into a pointer to a file
563*7c478bd9Sstevel@tonic-gate  * structure.  Only task is to check range of the descriptor (soft
564*7c478bd9Sstevel@tonic-gate  * resource limit was enforced at open time and shouldn't be checked
565*7c478bd9Sstevel@tonic-gate  * here).
566*7c478bd9Sstevel@tonic-gate  */
567*7c478bd9Sstevel@tonic-gate file_t *
568*7c478bd9Sstevel@tonic-gate getf(int fd)
569*7c478bd9Sstevel@tonic-gate {
570*7c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
571*7c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
572*7c478bd9Sstevel@tonic-gate 	file_t *fp;
573*7c478bd9Sstevel@tonic-gate 
574*7c478bd9Sstevel@tonic-gate 	if ((uint_t)fd >= fip->fi_nfiles)
575*7c478bd9Sstevel@tonic-gate 		return (NULL);
576*7c478bd9Sstevel@tonic-gate 
577*7c478bd9Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
578*7c478bd9Sstevel@tonic-gate 	if ((fp = ufp->uf_file) == NULL) {
579*7c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
580*7c478bd9Sstevel@tonic-gate 		return (NULL);
581*7c478bd9Sstevel@tonic-gate 	}
582*7c478bd9Sstevel@tonic-gate 	ufp->uf_refcnt++;
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
585*7c478bd9Sstevel@tonic-gate 	/*
586*7c478bd9Sstevel@tonic-gate 	 * archive per file audit data
587*7c478bd9Sstevel@tonic-gate 	 */
588*7c478bd9Sstevel@tonic-gate 	if (audit_active)
589*7c478bd9Sstevel@tonic-gate 		(void) audit_getf(fd);
590*7c478bd9Sstevel@tonic-gate #endif
591*7c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate 	set_active_fd(fd);	/* record the active file descriptor */
594*7c478bd9Sstevel@tonic-gate 
595*7c478bd9Sstevel@tonic-gate 	return (fp);
596*7c478bd9Sstevel@tonic-gate }
597*7c478bd9Sstevel@tonic-gate 
598*7c478bd9Sstevel@tonic-gate /*
599*7c478bd9Sstevel@tonic-gate  * Close whatever file currently occupies the file descriptor slot
600*7c478bd9Sstevel@tonic-gate  * and install the new file, usually NULL, in the file descriptor slot.
601*7c478bd9Sstevel@tonic-gate  * The close must complete before we release the file descriptor slot.
602*7c478bd9Sstevel@tonic-gate  * We return the error number from closef().
603*7c478bd9Sstevel@tonic-gate  */
604*7c478bd9Sstevel@tonic-gate int
605*7c478bd9Sstevel@tonic-gate closeandsetf(int fd, file_t *newfp)
606*7c478bd9Sstevel@tonic-gate {
607*7c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
608*7c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(p);
609*7c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
610*7c478bd9Sstevel@tonic-gate 	file_t *fp;
611*7c478bd9Sstevel@tonic-gate 	fpollinfo_t *fpip;
612*7c478bd9Sstevel@tonic-gate 	portfd_t *pfd;
613*7c478bd9Sstevel@tonic-gate 	int error;
614*7c478bd9Sstevel@tonic-gate 
615*7c478bd9Sstevel@tonic-gate 	if ((uint_t)fd >= fip->fi_nfiles) {
616*7c478bd9Sstevel@tonic-gate 		if (newfp == NULL)
617*7c478bd9Sstevel@tonic-gate 			return (EBADF);
618*7c478bd9Sstevel@tonic-gate 		flist_grow(fd);
619*7c478bd9Sstevel@tonic-gate 	}
620*7c478bd9Sstevel@tonic-gate 
621*7c478bd9Sstevel@tonic-gate 	if (newfp != NULL) {
622*7c478bd9Sstevel@tonic-gate 		/*
623*7c478bd9Sstevel@tonic-gate 		 * If ufp is reserved but has no file pointer, it's in the
624*7c478bd9Sstevel@tonic-gate 		 * transition between ufalloc() and setf().  We must wait
625*7c478bd9Sstevel@tonic-gate 		 * for this transition to complete before assigning the
626*7c478bd9Sstevel@tonic-gate 		 * new non-NULL file pointer.
627*7c478bd9Sstevel@tonic-gate 		 */
628*7c478bd9Sstevel@tonic-gate 		mutex_enter(&fip->fi_lock);
629*7c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
630*7c478bd9Sstevel@tonic-gate 		while (ufp->uf_busy && ufp->uf_file == NULL) {
631*7c478bd9Sstevel@tonic-gate 			mutex_exit(&fip->fi_lock);
632*7c478bd9Sstevel@tonic-gate 			cv_wait_stop(&ufp->uf_wanted_cv, &ufp->uf_lock, 250);
633*7c478bd9Sstevel@tonic-gate 			UF_EXIT(ufp);
634*7c478bd9Sstevel@tonic-gate 			mutex_enter(&fip->fi_lock);
635*7c478bd9Sstevel@tonic-gate 			UF_ENTER(ufp, fip, fd);
636*7c478bd9Sstevel@tonic-gate 		}
637*7c478bd9Sstevel@tonic-gate 		if ((fp = ufp->uf_file) == NULL) {
638*7c478bd9Sstevel@tonic-gate 			ASSERT(ufp->uf_fpollinfo == NULL);
639*7c478bd9Sstevel@tonic-gate 			ASSERT(ufp->uf_flag == 0);
640*7c478bd9Sstevel@tonic-gate 			fd_reserve(fip, fd, 1);
641*7c478bd9Sstevel@tonic-gate 			ufp->uf_file = newfp;
642*7c478bd9Sstevel@tonic-gate 			UF_EXIT(ufp);
643*7c478bd9Sstevel@tonic-gate 			mutex_exit(&fip->fi_lock);
644*7c478bd9Sstevel@tonic-gate 			return (0);
645*7c478bd9Sstevel@tonic-gate 		}
646*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fip->fi_lock);
647*7c478bd9Sstevel@tonic-gate 	} else {
648*7c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
649*7c478bd9Sstevel@tonic-gate 		if ((fp = ufp->uf_file) == NULL) {
650*7c478bd9Sstevel@tonic-gate 			UF_EXIT(ufp);
651*7c478bd9Sstevel@tonic-gate 			return (EBADF);
652*7c478bd9Sstevel@tonic-gate 		}
653*7c478bd9Sstevel@tonic-gate 	}
654*7c478bd9Sstevel@tonic-gate 
655*7c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
656*7c478bd9Sstevel@tonic-gate 	/*
657*7c478bd9Sstevel@tonic-gate 	 * archive per file audit data
658*7c478bd9Sstevel@tonic-gate 	 */
659*7c478bd9Sstevel@tonic-gate 	if (audit_active)
660*7c478bd9Sstevel@tonic-gate 		(void) audit_getf(fd);
661*7c478bd9Sstevel@tonic-gate #endif
662*7c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_busy);
663*7c478bd9Sstevel@tonic-gate 	ufp->uf_file = NULL;
664*7c478bd9Sstevel@tonic-gate 	ufp->uf_flag = 0;
665*7c478bd9Sstevel@tonic-gate 
666*7c478bd9Sstevel@tonic-gate 	/*
667*7c478bd9Sstevel@tonic-gate 	 * If the file descriptor reference count is non-zero, then
668*7c478bd9Sstevel@tonic-gate 	 * some other lwp in the process is performing system call
669*7c478bd9Sstevel@tonic-gate 	 * activity on the file.  To avoid blocking here for a long
670*7c478bd9Sstevel@tonic-gate 	 * time (the other lwp might be in a long term sleep in its
671*7c478bd9Sstevel@tonic-gate 	 * system call), we stop all other lwps in the process and
672*7c478bd9Sstevel@tonic-gate 	 * scan them to find the ones with this fd as one of their
673*7c478bd9Sstevel@tonic-gate 	 * active fds and set their a_stale flag so they will emerge
674*7c478bd9Sstevel@tonic-gate 	 * from their system calls immediately.  post_syscall() will
675*7c478bd9Sstevel@tonic-gate 	 * test the a_stale flag and set errno to EBADF.
676*7c478bd9Sstevel@tonic-gate 	 */
677*7c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_refcnt == 0 || p->p_lwpcnt > 1);
678*7c478bd9Sstevel@tonic-gate 	if (ufp->uf_refcnt > 0) {
679*7c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
680*7c478bd9Sstevel@tonic-gate 		COUNT(afd_wait);
681*7c478bd9Sstevel@tonic-gate 
682*7c478bd9Sstevel@tonic-gate 		/*
683*7c478bd9Sstevel@tonic-gate 		 * Make all other lwps hold in place, as if doing fork1().
684*7c478bd9Sstevel@tonic-gate 		 * holdlwps(SHOLDFORK1) fails only if another lwp wants to
685*7c478bd9Sstevel@tonic-gate 		 * perform a forkall() or the process is exiting.  In either
686*7c478bd9Sstevel@tonic-gate 		 * case, all other lwps are either returning from their
687*7c478bd9Sstevel@tonic-gate 		 * system calls (because of SHOLDFORK) or calling lwp_exit()
688*7c478bd9Sstevel@tonic-gate 		 * (because of SEXITLWPS) so we don't need to scan them.
689*7c478bd9Sstevel@tonic-gate 		 */
690*7c478bd9Sstevel@tonic-gate 		if (holdlwps(SHOLDFORK1)) {
691*7c478bd9Sstevel@tonic-gate 			kthread_t *t;
692*7c478bd9Sstevel@tonic-gate 
693*7c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
694*7c478bd9Sstevel@tonic-gate 			for (t = curthread->t_forw; t != curthread;
695*7c478bd9Sstevel@tonic-gate 			    t = t->t_forw) {
696*7c478bd9Sstevel@tonic-gate 				if (is_active_fd(t, fd)) {
697*7c478bd9Sstevel@tonic-gate 					t->t_activefd.a_stale = 1;
698*7c478bd9Sstevel@tonic-gate 					t->t_post_sys = 1;
699*7c478bd9Sstevel@tonic-gate 				}
700*7c478bd9Sstevel@tonic-gate 			}
701*7c478bd9Sstevel@tonic-gate 			continuelwps(p);
702*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
703*7c478bd9Sstevel@tonic-gate 		}
704*7c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
705*7c478bd9Sstevel@tonic-gate 		ASSERT(ufp->uf_file == NULL);
706*7c478bd9Sstevel@tonic-gate 	}
707*7c478bd9Sstevel@tonic-gate 
708*7c478bd9Sstevel@tonic-gate 	/*
709*7c478bd9Sstevel@tonic-gate 	 * Wait for other lwps to stop using this file descriptor.
710*7c478bd9Sstevel@tonic-gate 	 */
711*7c478bd9Sstevel@tonic-gate 	while (ufp->uf_refcnt > 0) {
712*7c478bd9Sstevel@tonic-gate 		cv_wait_stop(&ufp->uf_closing_cv, &ufp->uf_lock, 250);
713*7c478bd9Sstevel@tonic-gate 		/*
714*7c478bd9Sstevel@tonic-gate 		 * cv_wait_stop() drops ufp->uf_lock, so the file list
715*7c478bd9Sstevel@tonic-gate 		 * can change.  Drop the lock on our (possibly) stale
716*7c478bd9Sstevel@tonic-gate 		 * ufp and let UF_ENTER() find and lock the current ufp.
717*7c478bd9Sstevel@tonic-gate 		 */
718*7c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
719*7c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
720*7c478bd9Sstevel@tonic-gate 	}
721*7c478bd9Sstevel@tonic-gate 
722*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
723*7c478bd9Sstevel@tonic-gate 	/*
724*7c478bd9Sstevel@tonic-gate 	 * catch a watchfd on device's pollhead list but not on fpollinfo list
725*7c478bd9Sstevel@tonic-gate 	 */
726*7c478bd9Sstevel@tonic-gate 	if (ufp->uf_fpollinfo != NULL)
727*7c478bd9Sstevel@tonic-gate 		checkwfdlist(fp->f_vnode, ufp->uf_fpollinfo);
728*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
729*7c478bd9Sstevel@tonic-gate 
730*7c478bd9Sstevel@tonic-gate 	/*
731*7c478bd9Sstevel@tonic-gate 	 * We may need to cleanup some cached poll states in t_pollstate
732*7c478bd9Sstevel@tonic-gate 	 * before the fd can be reused. It is important that we don't
733*7c478bd9Sstevel@tonic-gate 	 * access a stale thread structure. We will do the cleanup in two
734*7c478bd9Sstevel@tonic-gate 	 * phases to avoid deadlock and holding uf_lock for too long.
735*7c478bd9Sstevel@tonic-gate 	 * In phase 1, hold the uf_lock and call pollblockexit() to set
736*7c478bd9Sstevel@tonic-gate 	 * state in t_pollstate struct so that a thread does not exit on
737*7c478bd9Sstevel@tonic-gate 	 * us. In phase 2, we drop the uf_lock and call pollcacheclean().
738*7c478bd9Sstevel@tonic-gate 	 */
739*7c478bd9Sstevel@tonic-gate 	pfd = ufp->uf_portfd;
740*7c478bd9Sstevel@tonic-gate 	ufp->uf_portfd = NULL;
741*7c478bd9Sstevel@tonic-gate 	fpip = ufp->uf_fpollinfo;
742*7c478bd9Sstevel@tonic-gate 	ufp->uf_fpollinfo = NULL;
743*7c478bd9Sstevel@tonic-gate 	if (fpip != NULL)
744*7c478bd9Sstevel@tonic-gate 		pollblockexit(fpip);
745*7c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
746*7c478bd9Sstevel@tonic-gate 	if (fpip != NULL)
747*7c478bd9Sstevel@tonic-gate 		pollcacheclean(fpip, fd);
748*7c478bd9Sstevel@tonic-gate 	if (pfd)
749*7c478bd9Sstevel@tonic-gate 		port_close_fd(pfd, fd);
750*7c478bd9Sstevel@tonic-gate 
751*7c478bd9Sstevel@tonic-gate 	/*
752*7c478bd9Sstevel@tonic-gate 	 * Keep the file descriptor entry reserved across the closef().
753*7c478bd9Sstevel@tonic-gate 	 */
754*7c478bd9Sstevel@tonic-gate 	error = closef(fp);
755*7c478bd9Sstevel@tonic-gate 
756*7c478bd9Sstevel@tonic-gate 	setf(fd, newfp);
757*7c478bd9Sstevel@tonic-gate 
758*7c478bd9Sstevel@tonic-gate 	return (error);
759*7c478bd9Sstevel@tonic-gate }
760*7c478bd9Sstevel@tonic-gate 
761*7c478bd9Sstevel@tonic-gate /*
762*7c478bd9Sstevel@tonic-gate  * Decrement uf_refcnt; wakeup anyone waiting to close the file.
763*7c478bd9Sstevel@tonic-gate  */
764*7c478bd9Sstevel@tonic-gate void
765*7c478bd9Sstevel@tonic-gate releasef(int fd)
766*7c478bd9Sstevel@tonic-gate {
767*7c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
768*7c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
769*7c478bd9Sstevel@tonic-gate 
770*7c478bd9Sstevel@tonic-gate 	clear_active_fd(fd);	/* clear the active file descriptor */
771*7c478bd9Sstevel@tonic-gate 
772*7c478bd9Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
773*7c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_refcnt > 0);
774*7c478bd9Sstevel@tonic-gate 	if (--ufp->uf_refcnt == 0)
775*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&ufp->uf_closing_cv);
776*7c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
777*7c478bd9Sstevel@tonic-gate }
778*7c478bd9Sstevel@tonic-gate 
779*7c478bd9Sstevel@tonic-gate /*
780*7c478bd9Sstevel@tonic-gate  * Identical to releasef() but can be called from another process.
781*7c478bd9Sstevel@tonic-gate  */
782*7c478bd9Sstevel@tonic-gate void
783*7c478bd9Sstevel@tonic-gate areleasef(int fd, uf_info_t *fip)
784*7c478bd9Sstevel@tonic-gate {
785*7c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
786*7c478bd9Sstevel@tonic-gate 
787*7c478bd9Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
788*7c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_refcnt > 0);
789*7c478bd9Sstevel@tonic-gate 	if (--ufp->uf_refcnt == 0)
790*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&ufp->uf_closing_cv);
791*7c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
792*7c478bd9Sstevel@tonic-gate }
793*7c478bd9Sstevel@tonic-gate 
794*7c478bd9Sstevel@tonic-gate /*
795*7c478bd9Sstevel@tonic-gate  * Duplicate all file descriptors across a fork.
796*7c478bd9Sstevel@tonic-gate  */
797*7c478bd9Sstevel@tonic-gate void
798*7c478bd9Sstevel@tonic-gate flist_fork(uf_info_t *pfip, uf_info_t *cfip)
799*7c478bd9Sstevel@tonic-gate {
800*7c478bd9Sstevel@tonic-gate 	int fd, nfiles;
801*7c478bd9Sstevel@tonic-gate 	uf_entry_t *pufp, *cufp;
802*7c478bd9Sstevel@tonic-gate 
803*7c478bd9Sstevel@tonic-gate 	mutex_init(&cfip->fi_lock, NULL, MUTEX_DEFAULT, NULL);
804*7c478bd9Sstevel@tonic-gate 	cfip->fi_rlist = NULL;
805*7c478bd9Sstevel@tonic-gate 
806*7c478bd9Sstevel@tonic-gate 	/*
807*7c478bd9Sstevel@tonic-gate 	 * We don't need to hold fi_lock because all other lwp's in the
808*7c478bd9Sstevel@tonic-gate 	 * parent have been held.
809*7c478bd9Sstevel@tonic-gate 	 */
810*7c478bd9Sstevel@tonic-gate 	cfip->fi_nfiles = nfiles = flist_minsize(pfip);
811*7c478bd9Sstevel@tonic-gate 
812*7c478bd9Sstevel@tonic-gate 	cfip->fi_list = kmem_zalloc(nfiles * sizeof (uf_entry_t), KM_SLEEP);
813*7c478bd9Sstevel@tonic-gate 
814*7c478bd9Sstevel@tonic-gate 	for (fd = 0, pufp = pfip->fi_list, cufp = cfip->fi_list; fd < nfiles;
815*7c478bd9Sstevel@tonic-gate 	    fd++, pufp++, cufp++) {
816*7c478bd9Sstevel@tonic-gate 		cufp->uf_file = pufp->uf_file;
817*7c478bd9Sstevel@tonic-gate 		cufp->uf_alloc = pufp->uf_alloc;
818*7c478bd9Sstevel@tonic-gate 		cufp->uf_flag = pufp->uf_flag;
819*7c478bd9Sstevel@tonic-gate 		cufp->uf_busy = pufp->uf_busy;
820*7c478bd9Sstevel@tonic-gate 		if (pufp->uf_file == NULL) {
821*7c478bd9Sstevel@tonic-gate 			ASSERT(pufp->uf_flag == 0);
822*7c478bd9Sstevel@tonic-gate 			if (pufp->uf_busy) {
823*7c478bd9Sstevel@tonic-gate 				/*
824*7c478bd9Sstevel@tonic-gate 				 * Grab locks to appease ASSERTs in fd_reserve
825*7c478bd9Sstevel@tonic-gate 				 */
826*7c478bd9Sstevel@tonic-gate 				mutex_enter(&cfip->fi_lock);
827*7c478bd9Sstevel@tonic-gate 				mutex_enter(&cufp->uf_lock);
828*7c478bd9Sstevel@tonic-gate 				fd_reserve(cfip, fd, -1);
829*7c478bd9Sstevel@tonic-gate 				mutex_exit(&cufp->uf_lock);
830*7c478bd9Sstevel@tonic-gate 				mutex_exit(&cfip->fi_lock);
831*7c478bd9Sstevel@tonic-gate 			}
832*7c478bd9Sstevel@tonic-gate 		}
833*7c478bd9Sstevel@tonic-gate 	}
834*7c478bd9Sstevel@tonic-gate }
835*7c478bd9Sstevel@tonic-gate 
836*7c478bd9Sstevel@tonic-gate /*
837*7c478bd9Sstevel@tonic-gate  * Close all open file descriptors for the current process.
838*7c478bd9Sstevel@tonic-gate  * This is only called from exit(), which is single-threaded,
839*7c478bd9Sstevel@tonic-gate  * so we don't need any locking.
840*7c478bd9Sstevel@tonic-gate  */
841*7c478bd9Sstevel@tonic-gate void
842*7c478bd9Sstevel@tonic-gate closeall(uf_info_t *fip)
843*7c478bd9Sstevel@tonic-gate {
844*7c478bd9Sstevel@tonic-gate 	int fd;
845*7c478bd9Sstevel@tonic-gate 	file_t *fp;
846*7c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
847*7c478bd9Sstevel@tonic-gate 
848*7c478bd9Sstevel@tonic-gate 	ufp = fip->fi_list;
849*7c478bd9Sstevel@tonic-gate 	for (fd = 0; fd < fip->fi_nfiles; fd++, ufp++) {
850*7c478bd9Sstevel@tonic-gate 		if ((fp = ufp->uf_file) != NULL) {
851*7c478bd9Sstevel@tonic-gate 			ufp->uf_file = NULL;
852*7c478bd9Sstevel@tonic-gate 			if (ufp->uf_portfd != NULL) {
853*7c478bd9Sstevel@tonic-gate 				/* remove event port association */
854*7c478bd9Sstevel@tonic-gate 				port_close_fd(ufp->uf_portfd, fd);
855*7c478bd9Sstevel@tonic-gate 				ufp->uf_portfd = NULL;
856*7c478bd9Sstevel@tonic-gate 			}
857*7c478bd9Sstevel@tonic-gate 			ASSERT(ufp->uf_fpollinfo == NULL);
858*7c478bd9Sstevel@tonic-gate 			(void) closef(fp);
859*7c478bd9Sstevel@tonic-gate 		}
860*7c478bd9Sstevel@tonic-gate 	}
861*7c478bd9Sstevel@tonic-gate 
862*7c478bd9Sstevel@tonic-gate 	kmem_free(fip->fi_list, fip->fi_nfiles * sizeof (uf_entry_t));
863*7c478bd9Sstevel@tonic-gate 	fip->fi_list = NULL;
864*7c478bd9Sstevel@tonic-gate 	fip->fi_nfiles = 0;
865*7c478bd9Sstevel@tonic-gate 	while (fip->fi_rlist != NULL) {
866*7c478bd9Sstevel@tonic-gate 		uf_rlist_t *urp = fip->fi_rlist;
867*7c478bd9Sstevel@tonic-gate 		fip->fi_rlist = urp->ur_next;
868*7c478bd9Sstevel@tonic-gate 		kmem_free(urp->ur_list, urp->ur_nfiles * sizeof (uf_entry_t));
869*7c478bd9Sstevel@tonic-gate 		kmem_free(urp, sizeof (uf_rlist_t));
870*7c478bd9Sstevel@tonic-gate 	}
871*7c478bd9Sstevel@tonic-gate }
872*7c478bd9Sstevel@tonic-gate 
873*7c478bd9Sstevel@tonic-gate /*
874*7c478bd9Sstevel@tonic-gate  * Internal form of close.  Decrement reference count on file
875*7c478bd9Sstevel@tonic-gate  * structure.  Decrement reference count on the vnode following
876*7c478bd9Sstevel@tonic-gate  * removal of the referencing file structure.
877*7c478bd9Sstevel@tonic-gate  */
878*7c478bd9Sstevel@tonic-gate int
879*7c478bd9Sstevel@tonic-gate closef(file_t *fp)
880*7c478bd9Sstevel@tonic-gate {
881*7c478bd9Sstevel@tonic-gate 	vnode_t *vp;
882*7c478bd9Sstevel@tonic-gate 	int error;
883*7c478bd9Sstevel@tonic-gate 	int count;
884*7c478bd9Sstevel@tonic-gate 	int flag;
885*7c478bd9Sstevel@tonic-gate 	offset_t offset;
886*7c478bd9Sstevel@tonic-gate 
887*7c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
888*7c478bd9Sstevel@tonic-gate 	/*
889*7c478bd9Sstevel@tonic-gate 	 * audit close of file (may be exit)
890*7c478bd9Sstevel@tonic-gate 	 */
891*7c478bd9Sstevel@tonic-gate 	if (audit_active)
892*7c478bd9Sstevel@tonic-gate 		audit_closef(fp);
893*7c478bd9Sstevel@tonic-gate #endif
894*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&P_FINFO(curproc)->fi_lock));
895*7c478bd9Sstevel@tonic-gate 
896*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fp->f_tlock);
897*7c478bd9Sstevel@tonic-gate 
898*7c478bd9Sstevel@tonic-gate 	ASSERT(fp->f_count > 0);
899*7c478bd9Sstevel@tonic-gate 
900*7c478bd9Sstevel@tonic-gate 	count = fp->f_count--;
901*7c478bd9Sstevel@tonic-gate 	flag = fp->f_flag;
902*7c478bd9Sstevel@tonic-gate 	offset = fp->f_offset;
903*7c478bd9Sstevel@tonic-gate 
904*7c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
905*7c478bd9Sstevel@tonic-gate 
906*7c478bd9Sstevel@tonic-gate 	error = VOP_CLOSE(vp, flag, count, offset, fp->f_cred);
907*7c478bd9Sstevel@tonic-gate 
908*7c478bd9Sstevel@tonic-gate 	if (count > 1) {
909*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
910*7c478bd9Sstevel@tonic-gate 		return (error);
911*7c478bd9Sstevel@tonic-gate 	}
912*7c478bd9Sstevel@tonic-gate 	ASSERT(fp->f_count == 0);
913*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fp->f_tlock);
914*7c478bd9Sstevel@tonic-gate 
915*7c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
916*7c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
917*7c478bd9Sstevel@tonic-gate 	/*
918*7c478bd9Sstevel@tonic-gate 	 * deallocate resources to audit_data
919*7c478bd9Sstevel@tonic-gate 	 */
920*7c478bd9Sstevel@tonic-gate 	if (audit_active)
921*7c478bd9Sstevel@tonic-gate 		audit_unfalloc(fp);
922*7c478bd9Sstevel@tonic-gate #endif
923*7c478bd9Sstevel@tonic-gate 	crfree(fp->f_cred);
924*7c478bd9Sstevel@tonic-gate 	kmem_cache_free(file_cache, fp);
925*7c478bd9Sstevel@tonic-gate 	return (error);
926*7c478bd9Sstevel@tonic-gate }
927*7c478bd9Sstevel@tonic-gate 
928*7c478bd9Sstevel@tonic-gate /*
929*7c478bd9Sstevel@tonic-gate  * This is a combination of ufalloc() and setf().
930*7c478bd9Sstevel@tonic-gate  */
931*7c478bd9Sstevel@tonic-gate int
932*7c478bd9Sstevel@tonic-gate ufalloc_file(int start, file_t *fp)
933*7c478bd9Sstevel@tonic-gate {
934*7c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
935*7c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(p);
936*7c478bd9Sstevel@tonic-gate 	int filelimit;
937*7c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
938*7c478bd9Sstevel@tonic-gate 	int nfiles;
939*7c478bd9Sstevel@tonic-gate 	int fd;
940*7c478bd9Sstevel@tonic-gate 
941*7c478bd9Sstevel@tonic-gate 	/*
942*7c478bd9Sstevel@tonic-gate 	 * Assertion is to convince the correctness of the following
943*7c478bd9Sstevel@tonic-gate 	 * assignment for filelimit after casting to int.
944*7c478bd9Sstevel@tonic-gate 	 */
945*7c478bd9Sstevel@tonic-gate 	ASSERT(p->p_fno_ctl <= INT_MAX);
946*7c478bd9Sstevel@tonic-gate 	filelimit = (int)p->p_fno_ctl;
947*7c478bd9Sstevel@tonic-gate 
948*7c478bd9Sstevel@tonic-gate 	for (;;) {
949*7c478bd9Sstevel@tonic-gate 		mutex_enter(&fip->fi_lock);
950*7c478bd9Sstevel@tonic-gate 		fd = fd_find(fip, start);
951*7c478bd9Sstevel@tonic-gate 		if ((uint_t)fd < filelimit)
952*7c478bd9Sstevel@tonic-gate 			break;
953*7c478bd9Sstevel@tonic-gate 		if (fd >= filelimit) {
954*7c478bd9Sstevel@tonic-gate 			mutex_exit(&fip->fi_lock);
955*7c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
956*7c478bd9Sstevel@tonic-gate 			(void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
957*7c478bd9Sstevel@tonic-gate 			    p->p_rctls, p, RCA_SAFE);
958*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
959*7c478bd9Sstevel@tonic-gate 			return (-1);
960*7c478bd9Sstevel@tonic-gate 		}
961*7c478bd9Sstevel@tonic-gate 		/* fd_find() returned -1 */
962*7c478bd9Sstevel@tonic-gate 		nfiles = fip->fi_nfiles;
963*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fip->fi_lock);
964*7c478bd9Sstevel@tonic-gate 		flist_grow(MAX(start, nfiles));
965*7c478bd9Sstevel@tonic-gate 	}
966*7c478bd9Sstevel@tonic-gate 
967*7c478bd9Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
968*7c478bd9Sstevel@tonic-gate 	fd_reserve(fip, fd, 1);
969*7c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_file == NULL);
970*7c478bd9Sstevel@tonic-gate 	ufp->uf_file = fp;
971*7c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
972*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
973*7c478bd9Sstevel@tonic-gate 	return (fd);
974*7c478bd9Sstevel@tonic-gate }
975*7c478bd9Sstevel@tonic-gate 
976*7c478bd9Sstevel@tonic-gate /*
977*7c478bd9Sstevel@tonic-gate  * Allocate a user file descriptor greater than or equal to "start".
978*7c478bd9Sstevel@tonic-gate  */
979*7c478bd9Sstevel@tonic-gate int
980*7c478bd9Sstevel@tonic-gate ufalloc(int start)
981*7c478bd9Sstevel@tonic-gate {
982*7c478bd9Sstevel@tonic-gate 	return (ufalloc_file(start, NULL));
983*7c478bd9Sstevel@tonic-gate }
984*7c478bd9Sstevel@tonic-gate 
985*7c478bd9Sstevel@tonic-gate /*
986*7c478bd9Sstevel@tonic-gate  * Check that a future allocation of count fds on proc p has a good
987*7c478bd9Sstevel@tonic-gate  * chance of succeeding.  If not, do rctl processing as if we'd failed
988*7c478bd9Sstevel@tonic-gate  * the allocation.
989*7c478bd9Sstevel@tonic-gate  *
990*7c478bd9Sstevel@tonic-gate  * Our caller must guarantee that p cannot disappear underneath us.
991*7c478bd9Sstevel@tonic-gate  */
992*7c478bd9Sstevel@tonic-gate int
993*7c478bd9Sstevel@tonic-gate ufcanalloc(proc_t *p, uint_t count)
994*7c478bd9Sstevel@tonic-gate {
995*7c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(p);
996*7c478bd9Sstevel@tonic-gate 	int filelimit;
997*7c478bd9Sstevel@tonic-gate 	int current;
998*7c478bd9Sstevel@tonic-gate 
999*7c478bd9Sstevel@tonic-gate 	if (count == 0)
1000*7c478bd9Sstevel@tonic-gate 		return (1);
1001*7c478bd9Sstevel@tonic-gate 
1002*7c478bd9Sstevel@tonic-gate 	ASSERT(p->p_fno_ctl <= INT_MAX);
1003*7c478bd9Sstevel@tonic-gate 	filelimit = (int)p->p_fno_ctl;
1004*7c478bd9Sstevel@tonic-gate 
1005*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fip->fi_lock);
1006*7c478bd9Sstevel@tonic-gate 	current = flist_nalloc(fip);		/* # of in-use descriptors */
1007*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
1008*7c478bd9Sstevel@tonic-gate 
1009*7c478bd9Sstevel@tonic-gate 	/*
1010*7c478bd9Sstevel@tonic-gate 	 * If count is a positive integer, the worst that can happen is
1011*7c478bd9Sstevel@tonic-gate 	 * an overflow to a negative value, which is caught by the >= 0 check.
1012*7c478bd9Sstevel@tonic-gate 	 */
1013*7c478bd9Sstevel@tonic-gate 	current += count;
1014*7c478bd9Sstevel@tonic-gate 	if (count <= INT_MAX && current >= 0 && current <= filelimit)
1015*7c478bd9Sstevel@tonic-gate 		return (1);
1016*7c478bd9Sstevel@tonic-gate 
1017*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1018*7c478bd9Sstevel@tonic-gate 	(void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
1019*7c478bd9Sstevel@tonic-gate 	    p->p_rctls, p, RCA_SAFE);
1020*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1021*7c478bd9Sstevel@tonic-gate 	return (0);
1022*7c478bd9Sstevel@tonic-gate }
1023*7c478bd9Sstevel@tonic-gate 
1024*7c478bd9Sstevel@tonic-gate /*
1025*7c478bd9Sstevel@tonic-gate  * Allocate a user file descriptor and a file structure.
1026*7c478bd9Sstevel@tonic-gate  * Initialize the descriptor to point at the file structure.
1027*7c478bd9Sstevel@tonic-gate  * If fdp is NULL, the user file descriptor will not be allocated.
1028*7c478bd9Sstevel@tonic-gate  */
1029*7c478bd9Sstevel@tonic-gate int
1030*7c478bd9Sstevel@tonic-gate falloc(vnode_t *vp, int flag, file_t **fpp, int *fdp)
1031*7c478bd9Sstevel@tonic-gate {
1032*7c478bd9Sstevel@tonic-gate 	file_t *fp;
1033*7c478bd9Sstevel@tonic-gate 	int fd;
1034*7c478bd9Sstevel@tonic-gate 
1035*7c478bd9Sstevel@tonic-gate 	if (fdp) {
1036*7c478bd9Sstevel@tonic-gate 		if ((fd = ufalloc(0)) == -1)
1037*7c478bd9Sstevel@tonic-gate 			return (EMFILE);
1038*7c478bd9Sstevel@tonic-gate 	}
1039*7c478bd9Sstevel@tonic-gate 	fp = kmem_cache_alloc(file_cache, KM_SLEEP);
1040*7c478bd9Sstevel@tonic-gate 	/*
1041*7c478bd9Sstevel@tonic-gate 	 * Note: falloc returns the fp locked
1042*7c478bd9Sstevel@tonic-gate 	 */
1043*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fp->f_tlock);
1044*7c478bd9Sstevel@tonic-gate 	fp->f_count = 1;
1045*7c478bd9Sstevel@tonic-gate 	fp->f_flag = (ushort_t)flag;
1046*7c478bd9Sstevel@tonic-gate 	fp->f_vnode = vp;
1047*7c478bd9Sstevel@tonic-gate 	fp->f_offset = 0;
1048*7c478bd9Sstevel@tonic-gate 	fp->f_audit_data = 0;
1049*7c478bd9Sstevel@tonic-gate 	crhold(fp->f_cred = CRED());
1050*7c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
1051*7c478bd9Sstevel@tonic-gate 	/*
1052*7c478bd9Sstevel@tonic-gate 	 * allocate resources to audit_data
1053*7c478bd9Sstevel@tonic-gate 	 */
1054*7c478bd9Sstevel@tonic-gate 	if (audit_active)
1055*7c478bd9Sstevel@tonic-gate 		audit_falloc(fp);
1056*7c478bd9Sstevel@tonic-gate #endif
1057*7c478bd9Sstevel@tonic-gate 	*fpp = fp;
1058*7c478bd9Sstevel@tonic-gate 	if (fdp)
1059*7c478bd9Sstevel@tonic-gate 		*fdp = fd;
1060*7c478bd9Sstevel@tonic-gate 	return (0);
1061*7c478bd9Sstevel@tonic-gate }
1062*7c478bd9Sstevel@tonic-gate 
1063*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1064*7c478bd9Sstevel@tonic-gate static int
1065*7c478bd9Sstevel@tonic-gate file_cache_constructor(void *buf, void *cdrarg, int kmflags)
1066*7c478bd9Sstevel@tonic-gate {
1067*7c478bd9Sstevel@tonic-gate 	file_t *fp = buf;
1068*7c478bd9Sstevel@tonic-gate 
1069*7c478bd9Sstevel@tonic-gate 	mutex_init(&fp->f_tlock, NULL, MUTEX_DEFAULT, NULL);
1070*7c478bd9Sstevel@tonic-gate 	return (0);
1071*7c478bd9Sstevel@tonic-gate }
1072*7c478bd9Sstevel@tonic-gate 
1073*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1074*7c478bd9Sstevel@tonic-gate static void
1075*7c478bd9Sstevel@tonic-gate file_cache_destructor(void *buf, void *cdrarg)
1076*7c478bd9Sstevel@tonic-gate {
1077*7c478bd9Sstevel@tonic-gate 	file_t *fp = buf;
1078*7c478bd9Sstevel@tonic-gate 
1079*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&fp->f_tlock);
1080*7c478bd9Sstevel@tonic-gate }
1081*7c478bd9Sstevel@tonic-gate 
1082*7c478bd9Sstevel@tonic-gate void
1083*7c478bd9Sstevel@tonic-gate finit()
1084*7c478bd9Sstevel@tonic-gate {
1085*7c478bd9Sstevel@tonic-gate 	file_cache = kmem_cache_create("file_cache", sizeof (file_t), 0,
1086*7c478bd9Sstevel@tonic-gate 	    file_cache_constructor, file_cache_destructor, NULL, NULL, NULL, 0);
1087*7c478bd9Sstevel@tonic-gate }
1088*7c478bd9Sstevel@tonic-gate 
1089*7c478bd9Sstevel@tonic-gate void
1090*7c478bd9Sstevel@tonic-gate unfalloc(file_t *fp)
1091*7c478bd9Sstevel@tonic-gate {
1092*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fp->f_tlock));
1093*7c478bd9Sstevel@tonic-gate 	if (--fp->f_count <= 0) {
1094*7c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
1095*7c478bd9Sstevel@tonic-gate 		/*
1096*7c478bd9Sstevel@tonic-gate 		 * deallocate resources to audit_data
1097*7c478bd9Sstevel@tonic-gate 		 */
1098*7c478bd9Sstevel@tonic-gate 		if (audit_active)
1099*7c478bd9Sstevel@tonic-gate 			audit_unfalloc(fp);
1100*7c478bd9Sstevel@tonic-gate #endif
1101*7c478bd9Sstevel@tonic-gate 		crfree(fp->f_cred);
1102*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
1103*7c478bd9Sstevel@tonic-gate 		kmem_cache_free(file_cache, fp);
1104*7c478bd9Sstevel@tonic-gate 	} else
1105*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
1106*7c478bd9Sstevel@tonic-gate }
1107*7c478bd9Sstevel@tonic-gate 
1108*7c478bd9Sstevel@tonic-gate /*
1109*7c478bd9Sstevel@tonic-gate  * Given a file descriptor, set the user's
1110*7c478bd9Sstevel@tonic-gate  * file pointer to the given parameter.
1111*7c478bd9Sstevel@tonic-gate  */
1112*7c478bd9Sstevel@tonic-gate void
1113*7c478bd9Sstevel@tonic-gate setf(int fd, file_t *fp)
1114*7c478bd9Sstevel@tonic-gate {
1115*7c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
1116*7c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
1117*7c478bd9Sstevel@tonic-gate 
1118*7c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
1119*7c478bd9Sstevel@tonic-gate 	if (audit_active)
1120*7c478bd9Sstevel@tonic-gate 		audit_setf(fp, fd);
1121*7c478bd9Sstevel@tonic-gate #endif /* C2_AUDIT */
1122*7c478bd9Sstevel@tonic-gate 
1123*7c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
1124*7c478bd9Sstevel@tonic-gate 		mutex_enter(&fip->fi_lock);
1125*7c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
1126*7c478bd9Sstevel@tonic-gate 		fd_reserve(fip, fd, -1);
1127*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fip->fi_lock);
1128*7c478bd9Sstevel@tonic-gate 	} else {
1129*7c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
1130*7c478bd9Sstevel@tonic-gate 		ASSERT(ufp->uf_busy);
1131*7c478bd9Sstevel@tonic-gate 	}
1132*7c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_fpollinfo == NULL);
1133*7c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_flag == 0);
1134*7c478bd9Sstevel@tonic-gate 	ufp->uf_file = fp;
1135*7c478bd9Sstevel@tonic-gate 	cv_broadcast(&ufp->uf_wanted_cv);
1136*7c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
1137*7c478bd9Sstevel@tonic-gate }
1138*7c478bd9Sstevel@tonic-gate 
1139*7c478bd9Sstevel@tonic-gate /*
1140*7c478bd9Sstevel@tonic-gate  * Given a file descriptor, return the file table flags, plus,
1141*7c478bd9Sstevel@tonic-gate  * if this is a socket in asynchronous mode, the FASYNC flag.
1142*7c478bd9Sstevel@tonic-gate  * getf() may or may not have been called before calling f_getfl().
1143*7c478bd9Sstevel@tonic-gate  */
1144*7c478bd9Sstevel@tonic-gate int
1145*7c478bd9Sstevel@tonic-gate f_getfl(int fd, int *flagp)
1146*7c478bd9Sstevel@tonic-gate {
1147*7c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
1148*7c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
1149*7c478bd9Sstevel@tonic-gate 	file_t *fp;
1150*7c478bd9Sstevel@tonic-gate 	int error;
1151*7c478bd9Sstevel@tonic-gate 
1152*7c478bd9Sstevel@tonic-gate 	if ((uint_t)fd >= fip->fi_nfiles)
1153*7c478bd9Sstevel@tonic-gate 		error = EBADF;
1154*7c478bd9Sstevel@tonic-gate 	else {
1155*7c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
1156*7c478bd9Sstevel@tonic-gate 		if ((fp = ufp->uf_file) == NULL)
1157*7c478bd9Sstevel@tonic-gate 			error = EBADF;
1158*7c478bd9Sstevel@tonic-gate 		else {
1159*7c478bd9Sstevel@tonic-gate 			vnode_t *vp = fp->f_vnode;
1160*7c478bd9Sstevel@tonic-gate 			int flag = fp->f_flag;
1161*7c478bd9Sstevel@tonic-gate 
1162*7c478bd9Sstevel@tonic-gate 			/*
1163*7c478bd9Sstevel@tonic-gate 			 * BSD fcntl() FASYNC compatibility.
1164*7c478bd9Sstevel@tonic-gate 			 *
1165*7c478bd9Sstevel@tonic-gate 			 * SCTP doesn't have an associated stream and thus
1166*7c478bd9Sstevel@tonic-gate 			 * doesn't store flags on it.
1167*7c478bd9Sstevel@tonic-gate 			 */
1168*7c478bd9Sstevel@tonic-gate 			if ((vp->v_type == VSOCK) && (vp->v_stream != NULL))
1169*7c478bd9Sstevel@tonic-gate 				flag |= sock_getfasync(vp);
1170*7c478bd9Sstevel@tonic-gate 			*flagp = flag;
1171*7c478bd9Sstevel@tonic-gate 			error = 0;
1172*7c478bd9Sstevel@tonic-gate 		}
1173*7c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
1174*7c478bd9Sstevel@tonic-gate 	}
1175*7c478bd9Sstevel@tonic-gate 
1176*7c478bd9Sstevel@tonic-gate 	return (error);
1177*7c478bd9Sstevel@tonic-gate }
1178*7c478bd9Sstevel@tonic-gate 
1179*7c478bd9Sstevel@tonic-gate /*
1180*7c478bd9Sstevel@tonic-gate  * Given a file descriptor, return the user's file flags.
1181*7c478bd9Sstevel@tonic-gate  * Force the FD_CLOEXEC flag for writable self-open /proc files.
1182*7c478bd9Sstevel@tonic-gate  * getf() may or may not have been called before calling f_getfd_error().
1183*7c478bd9Sstevel@tonic-gate  */
1184*7c478bd9Sstevel@tonic-gate int
1185*7c478bd9Sstevel@tonic-gate f_getfd_error(int fd, int *flagp)
1186*7c478bd9Sstevel@tonic-gate {
1187*7c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
1188*7c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
1189*7c478bd9Sstevel@tonic-gate 	file_t *fp;
1190*7c478bd9Sstevel@tonic-gate 	int flag;
1191*7c478bd9Sstevel@tonic-gate 	int error;
1192*7c478bd9Sstevel@tonic-gate 
1193*7c478bd9Sstevel@tonic-gate 	if ((uint_t)fd >= fip->fi_nfiles)
1194*7c478bd9Sstevel@tonic-gate 		error = EBADF;
1195*7c478bd9Sstevel@tonic-gate 	else {
1196*7c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
1197*7c478bd9Sstevel@tonic-gate 		if ((fp = ufp->uf_file) == NULL)
1198*7c478bd9Sstevel@tonic-gate 			error = EBADF;
1199*7c478bd9Sstevel@tonic-gate 		else {
1200*7c478bd9Sstevel@tonic-gate 			flag = ufp->uf_flag;
1201*7c478bd9Sstevel@tonic-gate 			if ((fp->f_flag & FWRITE) && pr_isself(fp->f_vnode))
1202*7c478bd9Sstevel@tonic-gate 				flag |= FD_CLOEXEC;
1203*7c478bd9Sstevel@tonic-gate 			*flagp = flag;
1204*7c478bd9Sstevel@tonic-gate 			error = 0;
1205*7c478bd9Sstevel@tonic-gate 		}
1206*7c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
1207*7c478bd9Sstevel@tonic-gate 	}
1208*7c478bd9Sstevel@tonic-gate 
1209*7c478bd9Sstevel@tonic-gate 	return (error);
1210*7c478bd9Sstevel@tonic-gate }
1211*7c478bd9Sstevel@tonic-gate 
1212*7c478bd9Sstevel@tonic-gate /*
1213*7c478bd9Sstevel@tonic-gate  * getf() must have been called before calling f_getfd().
1214*7c478bd9Sstevel@tonic-gate  */
1215*7c478bd9Sstevel@tonic-gate char
1216*7c478bd9Sstevel@tonic-gate f_getfd(int fd)
1217*7c478bd9Sstevel@tonic-gate {
1218*7c478bd9Sstevel@tonic-gate 	int flag = 0;
1219*7c478bd9Sstevel@tonic-gate 	(void) f_getfd_error(fd, &flag);
1220*7c478bd9Sstevel@tonic-gate 	return ((char)flag);
1221*7c478bd9Sstevel@tonic-gate }
1222*7c478bd9Sstevel@tonic-gate 
1223*7c478bd9Sstevel@tonic-gate /*
1224*7c478bd9Sstevel@tonic-gate  * Given a file descriptor and file flags, set the user's file flags.
1225*7c478bd9Sstevel@tonic-gate  * At present, the only valid flag is FD_CLOEXEC.
1226*7c478bd9Sstevel@tonic-gate  * getf() may or may not have been called before calling f_setfd_error().
1227*7c478bd9Sstevel@tonic-gate  */
1228*7c478bd9Sstevel@tonic-gate int
1229*7c478bd9Sstevel@tonic-gate f_setfd_error(int fd, int flags)
1230*7c478bd9Sstevel@tonic-gate {
1231*7c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
1232*7c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
1233*7c478bd9Sstevel@tonic-gate 	int error;
1234*7c478bd9Sstevel@tonic-gate 
1235*7c478bd9Sstevel@tonic-gate 	if ((uint_t)fd >= fip->fi_nfiles)
1236*7c478bd9Sstevel@tonic-gate 		error = EBADF;
1237*7c478bd9Sstevel@tonic-gate 	else {
1238*7c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
1239*7c478bd9Sstevel@tonic-gate 		if (ufp->uf_file == NULL)
1240*7c478bd9Sstevel@tonic-gate 			error = EBADF;
1241*7c478bd9Sstevel@tonic-gate 		else {
1242*7c478bd9Sstevel@tonic-gate 			ufp->uf_flag = flags & FD_CLOEXEC;
1243*7c478bd9Sstevel@tonic-gate 			error = 0;
1244*7c478bd9Sstevel@tonic-gate 		}
1245*7c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
1246*7c478bd9Sstevel@tonic-gate 	}
1247*7c478bd9Sstevel@tonic-gate 	return (error);
1248*7c478bd9Sstevel@tonic-gate }
1249*7c478bd9Sstevel@tonic-gate 
1250*7c478bd9Sstevel@tonic-gate void
1251*7c478bd9Sstevel@tonic-gate f_setfd(int fd, char flags)
1252*7c478bd9Sstevel@tonic-gate {
1253*7c478bd9Sstevel@tonic-gate 	(void) f_setfd_error(fd, flags);
1254*7c478bd9Sstevel@tonic-gate }
1255*7c478bd9Sstevel@tonic-gate 
1256*7c478bd9Sstevel@tonic-gate /*
1257*7c478bd9Sstevel@tonic-gate  * Allocate a file descriptor and assign it to the vnode "*vpp",
1258*7c478bd9Sstevel@tonic-gate  * performing the usual open protocol upon it and returning the
1259*7c478bd9Sstevel@tonic-gate  * file descriptor allocated.  It is the responsibility of the
1260*7c478bd9Sstevel@tonic-gate  * caller to dispose of "*vpp" if any error occurs.
1261*7c478bd9Sstevel@tonic-gate  */
1262*7c478bd9Sstevel@tonic-gate int
1263*7c478bd9Sstevel@tonic-gate fassign(vnode_t **vpp, int mode, int *fdp)
1264*7c478bd9Sstevel@tonic-gate {
1265*7c478bd9Sstevel@tonic-gate 	file_t *fp;
1266*7c478bd9Sstevel@tonic-gate 	int error;
1267*7c478bd9Sstevel@tonic-gate 	int fd;
1268*7c478bd9Sstevel@tonic-gate 
1269*7c478bd9Sstevel@tonic-gate 	if (error = falloc((vnode_t *)NULL, mode, &fp, &fd))
1270*7c478bd9Sstevel@tonic-gate 		return (error);
1271*7c478bd9Sstevel@tonic-gate 	if (error = VOP_OPEN(vpp, mode, fp->f_cred)) {
1272*7c478bd9Sstevel@tonic-gate 		setf(fd, NULL);
1273*7c478bd9Sstevel@tonic-gate 		unfalloc(fp);
1274*7c478bd9Sstevel@tonic-gate 		return (error);
1275*7c478bd9Sstevel@tonic-gate 	}
1276*7c478bd9Sstevel@tonic-gate 	fp->f_vnode = *vpp;
1277*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fp->f_tlock);
1278*7c478bd9Sstevel@tonic-gate 	/*
1279*7c478bd9Sstevel@tonic-gate 	 * Fill in the slot falloc reserved.
1280*7c478bd9Sstevel@tonic-gate 	 */
1281*7c478bd9Sstevel@tonic-gate 	setf(fd, fp);
1282*7c478bd9Sstevel@tonic-gate 	*fdp = fd;
1283*7c478bd9Sstevel@tonic-gate 	return (0);
1284*7c478bd9Sstevel@tonic-gate }
1285*7c478bd9Sstevel@tonic-gate 
1286*7c478bd9Sstevel@tonic-gate /*
1287*7c478bd9Sstevel@tonic-gate  * When a process forks it must increment the f_count of all file pointers
1288*7c478bd9Sstevel@tonic-gate  * since there is a new process pointing at them.  fcnt_add(fip, 1) does this.
1289*7c478bd9Sstevel@tonic-gate  * Since we are called when there is only 1 active lwp we don't need to
1290*7c478bd9Sstevel@tonic-gate  * hold fi_lock or any uf_lock.  If the fork fails, fork_fail() calls
1291*7c478bd9Sstevel@tonic-gate  * fcnt_add(fip, -1) to restore the counts.
1292*7c478bd9Sstevel@tonic-gate  */
1293*7c478bd9Sstevel@tonic-gate void
1294*7c478bd9Sstevel@tonic-gate fcnt_add(uf_info_t *fip, int incr)
1295*7c478bd9Sstevel@tonic-gate {
1296*7c478bd9Sstevel@tonic-gate 	int i;
1297*7c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
1298*7c478bd9Sstevel@tonic-gate 	file_t *fp;
1299*7c478bd9Sstevel@tonic-gate 
1300*7c478bd9Sstevel@tonic-gate 	ufp = fip->fi_list;
1301*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < fip->fi_nfiles; i++, ufp++) {
1302*7c478bd9Sstevel@tonic-gate 		if ((fp = ufp->uf_file) != NULL) {
1303*7c478bd9Sstevel@tonic-gate 			mutex_enter(&fp->f_tlock);
1304*7c478bd9Sstevel@tonic-gate 			ASSERT((incr == 1 && fp->f_count >= 1) ||
1305*7c478bd9Sstevel@tonic-gate 			    (incr == -1 && fp->f_count >= 2));
1306*7c478bd9Sstevel@tonic-gate 			fp->f_count += incr;
1307*7c478bd9Sstevel@tonic-gate 			mutex_exit(&fp->f_tlock);
1308*7c478bd9Sstevel@tonic-gate 		}
1309*7c478bd9Sstevel@tonic-gate 	}
1310*7c478bd9Sstevel@tonic-gate }
1311*7c478bd9Sstevel@tonic-gate 
1312*7c478bd9Sstevel@tonic-gate /*
1313*7c478bd9Sstevel@tonic-gate  * This is called from exec to close all fd's that have the FD_CLOEXEC flag
1314*7c478bd9Sstevel@tonic-gate  * set and also to close all self-open for write /proc file descriptors.
1315*7c478bd9Sstevel@tonic-gate  */
1316*7c478bd9Sstevel@tonic-gate void
1317*7c478bd9Sstevel@tonic-gate close_exec(uf_info_t *fip)
1318*7c478bd9Sstevel@tonic-gate {
1319*7c478bd9Sstevel@tonic-gate 	int fd;
1320*7c478bd9Sstevel@tonic-gate 	file_t *fp;
1321*7c478bd9Sstevel@tonic-gate 	fpollinfo_t *fpip;
1322*7c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
1323*7c478bd9Sstevel@tonic-gate 	portfd_t *pfd;
1324*7c478bd9Sstevel@tonic-gate 
1325*7c478bd9Sstevel@tonic-gate 	ufp = fip->fi_list;
1326*7c478bd9Sstevel@tonic-gate 	for (fd = 0; fd < fip->fi_nfiles; fd++, ufp++) {
1327*7c478bd9Sstevel@tonic-gate 		if ((fp = ufp->uf_file) != NULL &&
1328*7c478bd9Sstevel@tonic-gate 		    ((ufp->uf_flag & FD_CLOEXEC) ||
1329*7c478bd9Sstevel@tonic-gate 		    ((fp->f_flag & FWRITE) && pr_isself(fp->f_vnode)))) {
1330*7c478bd9Sstevel@tonic-gate 			fpip = ufp->uf_fpollinfo;
1331*7c478bd9Sstevel@tonic-gate 			mutex_enter(&fip->fi_lock);
1332*7c478bd9Sstevel@tonic-gate 			mutex_enter(&ufp->uf_lock);
1333*7c478bd9Sstevel@tonic-gate 			fd_reserve(fip, fd, -1);
1334*7c478bd9Sstevel@tonic-gate 			mutex_exit(&fip->fi_lock);
1335*7c478bd9Sstevel@tonic-gate 			ufp->uf_file = NULL;
1336*7c478bd9Sstevel@tonic-gate 			ufp->uf_fpollinfo = NULL;
1337*7c478bd9Sstevel@tonic-gate 			ufp->uf_flag = 0;
1338*7c478bd9Sstevel@tonic-gate 			/*
1339*7c478bd9Sstevel@tonic-gate 			 * We may need to cleanup some cached poll states
1340*7c478bd9Sstevel@tonic-gate 			 * in t_pollstate before the fd can be reused. It
1341*7c478bd9Sstevel@tonic-gate 			 * is important that we don't access a stale thread
1342*7c478bd9Sstevel@tonic-gate 			 * structure. We will do the cleanup in two
1343*7c478bd9Sstevel@tonic-gate 			 * phases to avoid deadlock and holding uf_lock for
1344*7c478bd9Sstevel@tonic-gate 			 * too long. In phase 1, hold the uf_lock and call
1345*7c478bd9Sstevel@tonic-gate 			 * pollblockexit() to set state in t_pollstate struct
1346*7c478bd9Sstevel@tonic-gate 			 * so that a thread does not exit on us. In phase 2,
1347*7c478bd9Sstevel@tonic-gate 			 * we drop the uf_lock and call pollcacheclean().
1348*7c478bd9Sstevel@tonic-gate 			 */
1349*7c478bd9Sstevel@tonic-gate 			pfd = ufp->uf_portfd;
1350*7c478bd9Sstevel@tonic-gate 			ufp->uf_portfd = NULL;
1351*7c478bd9Sstevel@tonic-gate 			if (fpip != NULL)
1352*7c478bd9Sstevel@tonic-gate 				pollblockexit(fpip);
1353*7c478bd9Sstevel@tonic-gate 			mutex_exit(&ufp->uf_lock);
1354*7c478bd9Sstevel@tonic-gate 			if (fpip != NULL)
1355*7c478bd9Sstevel@tonic-gate 				pollcacheclean(fpip, fd);
1356*7c478bd9Sstevel@tonic-gate 			if (pfd)
1357*7c478bd9Sstevel@tonic-gate 				port_close_fd(pfd, fd);
1358*7c478bd9Sstevel@tonic-gate 			(void) closef(fp);
1359*7c478bd9Sstevel@tonic-gate 		}
1360*7c478bd9Sstevel@tonic-gate 	}
1361*7c478bd9Sstevel@tonic-gate }
1362*7c478bd9Sstevel@tonic-gate 
1363*7c478bd9Sstevel@tonic-gate /*
1364*7c478bd9Sstevel@tonic-gate  * Common routine for modifying attributes of named files.
1365*7c478bd9Sstevel@tonic-gate  */
1366*7c478bd9Sstevel@tonic-gate int
1367*7c478bd9Sstevel@tonic-gate namesetattr(char *fnamep, enum symfollow followlink, vattr_t *vap, int flags)
1368*7c478bd9Sstevel@tonic-gate {
1369*7c478bd9Sstevel@tonic-gate 	vnode_t *vp;
1370*7c478bd9Sstevel@tonic-gate 	int error = 0;
1371*7c478bd9Sstevel@tonic-gate 
1372*7c478bd9Sstevel@tonic-gate 	if (error = lookupname(fnamep, UIO_USERSPACE, followlink, NULLVPP, &vp))
1373*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1374*7c478bd9Sstevel@tonic-gate 	if (error = vpsetattr(vp, vap, flags))
1375*7c478bd9Sstevel@tonic-gate 		(void) set_errno(error);
1376*7c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
1377*7c478bd9Sstevel@tonic-gate 	return (error);
1378*7c478bd9Sstevel@tonic-gate }
1379*7c478bd9Sstevel@tonic-gate 
1380*7c478bd9Sstevel@tonic-gate /*
1381*7c478bd9Sstevel@tonic-gate  * Common routine for modifying attributes of files referenced
1382*7c478bd9Sstevel@tonic-gate  * by descriptor.
1383*7c478bd9Sstevel@tonic-gate  */
1384*7c478bd9Sstevel@tonic-gate int
1385*7c478bd9Sstevel@tonic-gate fdsetattr(int fd, vattr_t *vap)
1386*7c478bd9Sstevel@tonic-gate {
1387*7c478bd9Sstevel@tonic-gate 	file_t *fp;
1388*7c478bd9Sstevel@tonic-gate 	vnode_t *vp;
1389*7c478bd9Sstevel@tonic-gate 	int error = 0;
1390*7c478bd9Sstevel@tonic-gate 
1391*7c478bd9Sstevel@tonic-gate 	if ((fp = getf(fd)) != NULL) {
1392*7c478bd9Sstevel@tonic-gate 		vp = fp->f_vnode;
1393*7c478bd9Sstevel@tonic-gate 		if (error = vpsetattr(vp, vap, 0)) {
1394*7c478bd9Sstevel@tonic-gate 			(void) set_errno(error);
1395*7c478bd9Sstevel@tonic-gate 		}
1396*7c478bd9Sstevel@tonic-gate 		releasef(fd);
1397*7c478bd9Sstevel@tonic-gate 	} else
1398*7c478bd9Sstevel@tonic-gate 		error = set_errno(EBADF);
1399*7c478bd9Sstevel@tonic-gate 	return (error);
1400*7c478bd9Sstevel@tonic-gate }
1401*7c478bd9Sstevel@tonic-gate 
1402*7c478bd9Sstevel@tonic-gate /*
1403*7c478bd9Sstevel@tonic-gate  * Common routine to set the attributes for the given vnode.
1404*7c478bd9Sstevel@tonic-gate  * If the vnode is a file and the filesize is being manipulated,
1405*7c478bd9Sstevel@tonic-gate  * this makes sure that there are no conflicting non-blocking
1406*7c478bd9Sstevel@tonic-gate  * mandatory locks in that region.
1407*7c478bd9Sstevel@tonic-gate  */
1408*7c478bd9Sstevel@tonic-gate static int
1409*7c478bd9Sstevel@tonic-gate vpsetattr(vnode_t *vp, vattr_t *vap, int flags)
1410*7c478bd9Sstevel@tonic-gate {
1411*7c478bd9Sstevel@tonic-gate 	int error = 0;
1412*7c478bd9Sstevel@tonic-gate 	int in_crit = 0;
1413*7c478bd9Sstevel@tonic-gate 	u_offset_t	begin;
1414*7c478bd9Sstevel@tonic-gate 	vattr_t	vattr;
1415*7c478bd9Sstevel@tonic-gate 	ssize_t	length;
1416*7c478bd9Sstevel@tonic-gate 
1417*7c478bd9Sstevel@tonic-gate 	if (vn_is_readonly(vp)) {
1418*7c478bd9Sstevel@tonic-gate 		error = EROFS;
1419*7c478bd9Sstevel@tonic-gate 	}
1420*7c478bd9Sstevel@tonic-gate 	if (!error && (vap->va_mask & AT_SIZE) &&
1421*7c478bd9Sstevel@tonic-gate 	    nbl_need_check(vp)) {
1422*7c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
1423*7c478bd9Sstevel@tonic-gate 		in_crit = 1;
1424*7c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_SIZE;
1425*7c478bd9Sstevel@tonic-gate 		if (!(error = VOP_GETATTR(vp, &vattr, 0, CRED()))) {
1426*7c478bd9Sstevel@tonic-gate 			begin = vap->va_size > vattr.va_size ?
1427*7c478bd9Sstevel@tonic-gate 					vattr.va_size : vap->va_size;
1428*7c478bd9Sstevel@tonic-gate 			length = vattr.va_size > vap->va_size ?
1429*7c478bd9Sstevel@tonic-gate 					vattr.va_size - vap->va_size :
1430*7c478bd9Sstevel@tonic-gate 					vap->va_size - vattr.va_size;
1431*7c478bd9Sstevel@tonic-gate 
1432*7c478bd9Sstevel@tonic-gate 			if (nbl_conflict(vp, NBL_WRITE, begin, length, 0)) {
1433*7c478bd9Sstevel@tonic-gate 				error = EACCES;
1434*7c478bd9Sstevel@tonic-gate 			}
1435*7c478bd9Sstevel@tonic-gate 		}
1436*7c478bd9Sstevel@tonic-gate 	}
1437*7c478bd9Sstevel@tonic-gate 	if (!error)
1438*7c478bd9Sstevel@tonic-gate 		error = VOP_SETATTR(vp, vap, flags, CRED(), NULL);
1439*7c478bd9Sstevel@tonic-gate 
1440*7c478bd9Sstevel@tonic-gate 	if (in_crit)
1441*7c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
1442*7c478bd9Sstevel@tonic-gate 
1443*7c478bd9Sstevel@tonic-gate 	return (error);
1444*7c478bd9Sstevel@tonic-gate }
1445*7c478bd9Sstevel@tonic-gate 
1446*7c478bd9Sstevel@tonic-gate /*
1447*7c478bd9Sstevel@tonic-gate  * Return true if the given vnode is referenced by any
1448*7c478bd9Sstevel@tonic-gate  * entry in the current process's file descriptor table.
1449*7c478bd9Sstevel@tonic-gate  */
1450*7c478bd9Sstevel@tonic-gate int
1451*7c478bd9Sstevel@tonic-gate fisopen(vnode_t *vp)
1452*7c478bd9Sstevel@tonic-gate {
1453*7c478bd9Sstevel@tonic-gate 	int fd;
1454*7c478bd9Sstevel@tonic-gate 	file_t *fp;
1455*7c478bd9Sstevel@tonic-gate 	vnode_t *ovp;
1456*7c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
1457*7c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
1458*7c478bd9Sstevel@tonic-gate 
1459*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fip->fi_lock);
1460*7c478bd9Sstevel@tonic-gate 	for (fd = 0; fd < fip->fi_nfiles; fd++) {
1461*7c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
1462*7c478bd9Sstevel@tonic-gate 		if ((fp = ufp->uf_file) != NULL &&
1463*7c478bd9Sstevel@tonic-gate 		    (ovp = fp->f_vnode) != NULL && VN_CMP(vp, ovp)) {
1464*7c478bd9Sstevel@tonic-gate 			UF_EXIT(ufp);
1465*7c478bd9Sstevel@tonic-gate 			mutex_exit(&fip->fi_lock);
1466*7c478bd9Sstevel@tonic-gate 			return (1);
1467*7c478bd9Sstevel@tonic-gate 		}
1468*7c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
1469*7c478bd9Sstevel@tonic-gate 	}
1470*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
1471*7c478bd9Sstevel@tonic-gate 	return (0);
1472*7c478bd9Sstevel@tonic-gate }
1473*7c478bd9Sstevel@tonic-gate 
1474*7c478bd9Sstevel@tonic-gate /*
1475*7c478bd9Sstevel@tonic-gate  * Return zero if at least one file currently open (by curproc) shouldn't be
1476*7c478bd9Sstevel@tonic-gate  * allowed to change zones.
1477*7c478bd9Sstevel@tonic-gate  */
1478*7c478bd9Sstevel@tonic-gate int
1479*7c478bd9Sstevel@tonic-gate files_can_change_zones(void)
1480*7c478bd9Sstevel@tonic-gate {
1481*7c478bd9Sstevel@tonic-gate 	int fd;
1482*7c478bd9Sstevel@tonic-gate 	file_t *fp;
1483*7c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
1484*7c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
1485*7c478bd9Sstevel@tonic-gate 
1486*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fip->fi_lock);
1487*7c478bd9Sstevel@tonic-gate 	for (fd = 0; fd < fip->fi_nfiles; fd++) {
1488*7c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
1489*7c478bd9Sstevel@tonic-gate 		if ((fp = ufp->uf_file) != NULL &&
1490*7c478bd9Sstevel@tonic-gate 		    !vn_can_change_zones(fp->f_vnode)) {
1491*7c478bd9Sstevel@tonic-gate 			UF_EXIT(ufp);
1492*7c478bd9Sstevel@tonic-gate 			mutex_exit(&fip->fi_lock);
1493*7c478bd9Sstevel@tonic-gate 			return (0);
1494*7c478bd9Sstevel@tonic-gate 		}
1495*7c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
1496*7c478bd9Sstevel@tonic-gate 	}
1497*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
1498*7c478bd9Sstevel@tonic-gate 	return (1);
1499*7c478bd9Sstevel@tonic-gate }
1500*7c478bd9Sstevel@tonic-gate 
1501*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
1502*7c478bd9Sstevel@tonic-gate 
1503*7c478bd9Sstevel@tonic-gate /*
1504*7c478bd9Sstevel@tonic-gate  * The following functions are only used in ASSERT()s elsewhere.
1505*7c478bd9Sstevel@tonic-gate  * They do not modify the state of the system.
1506*7c478bd9Sstevel@tonic-gate  */
1507*7c478bd9Sstevel@tonic-gate 
1508*7c478bd9Sstevel@tonic-gate /*
1509*7c478bd9Sstevel@tonic-gate  * Return true (1) if the current thread is in the fpollinfo
1510*7c478bd9Sstevel@tonic-gate  * list for this file descriptor, else false (0).
1511*7c478bd9Sstevel@tonic-gate  */
1512*7c478bd9Sstevel@tonic-gate static int
1513*7c478bd9Sstevel@tonic-gate curthread_in_plist(uf_entry_t *ufp)
1514*7c478bd9Sstevel@tonic-gate {
1515*7c478bd9Sstevel@tonic-gate 	fpollinfo_t *fpip;
1516*7c478bd9Sstevel@tonic-gate 
1517*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ufp->uf_lock));
1518*7c478bd9Sstevel@tonic-gate 	for (fpip = ufp->uf_fpollinfo; fpip; fpip = fpip->fp_next)
1519*7c478bd9Sstevel@tonic-gate 		if (fpip->fp_thread == curthread)
1520*7c478bd9Sstevel@tonic-gate 			return (1);
1521*7c478bd9Sstevel@tonic-gate 	return (0);
1522*7c478bd9Sstevel@tonic-gate }
1523*7c478bd9Sstevel@tonic-gate 
1524*7c478bd9Sstevel@tonic-gate /*
1525*7c478bd9Sstevel@tonic-gate  * Sanity check to make sure that after lwp_exit(),
1526*7c478bd9Sstevel@tonic-gate  * curthread does not appear on any fd's fpollinfo list.
1527*7c478bd9Sstevel@tonic-gate  */
1528*7c478bd9Sstevel@tonic-gate void
1529*7c478bd9Sstevel@tonic-gate checkfpollinfo(void)
1530*7c478bd9Sstevel@tonic-gate {
1531*7c478bd9Sstevel@tonic-gate 	int fd;
1532*7c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
1533*7c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
1534*7c478bd9Sstevel@tonic-gate 
1535*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fip->fi_lock);
1536*7c478bd9Sstevel@tonic-gate 	for (fd = 0; fd < fip->fi_nfiles; fd++) {
1537*7c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
1538*7c478bd9Sstevel@tonic-gate 		ASSERT(!curthread_in_plist(ufp));
1539*7c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
1540*7c478bd9Sstevel@tonic-gate 	}
1541*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
1542*7c478bd9Sstevel@tonic-gate }
1543*7c478bd9Sstevel@tonic-gate 
1544*7c478bd9Sstevel@tonic-gate /*
1545*7c478bd9Sstevel@tonic-gate  * Return true (1) if the current thread is in the fpollinfo
1546*7c478bd9Sstevel@tonic-gate  * list for this file descriptor, else false (0).
1547*7c478bd9Sstevel@tonic-gate  * This is the same as curthread_in_plist(),
1548*7c478bd9Sstevel@tonic-gate  * but is called w/o holding uf_lock.
1549*7c478bd9Sstevel@tonic-gate  */
1550*7c478bd9Sstevel@tonic-gate int
1551*7c478bd9Sstevel@tonic-gate infpollinfo(int fd)
1552*7c478bd9Sstevel@tonic-gate {
1553*7c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
1554*7c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
1555*7c478bd9Sstevel@tonic-gate 	int rc;
1556*7c478bd9Sstevel@tonic-gate 
1557*7c478bd9Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
1558*7c478bd9Sstevel@tonic-gate 	rc = curthread_in_plist(ufp);
1559*7c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
1560*7c478bd9Sstevel@tonic-gate 	return (rc);
1561*7c478bd9Sstevel@tonic-gate }
1562*7c478bd9Sstevel@tonic-gate 
1563*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
1564*7c478bd9Sstevel@tonic-gate 
1565*7c478bd9Sstevel@tonic-gate /*
1566*7c478bd9Sstevel@tonic-gate  * Add the curthread to fpollinfo list, meaning this fd is currently in the
1567*7c478bd9Sstevel@tonic-gate  * thread's poll cache. Each lwp polling this file descriptor should call
1568*7c478bd9Sstevel@tonic-gate  * this routine once.
1569*7c478bd9Sstevel@tonic-gate  */
1570*7c478bd9Sstevel@tonic-gate void
1571*7c478bd9Sstevel@tonic-gate addfpollinfo(int fd)
1572*7c478bd9Sstevel@tonic-gate {
1573*7c478bd9Sstevel@tonic-gate 	struct uf_entry *ufp;
1574*7c478bd9Sstevel@tonic-gate 	fpollinfo_t *fpip;
1575*7c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
1576*7c478bd9Sstevel@tonic-gate 
1577*7c478bd9Sstevel@tonic-gate 	fpip = kmem_zalloc(sizeof (fpollinfo_t), KM_SLEEP);
1578*7c478bd9Sstevel@tonic-gate 	fpip->fp_thread = curthread;
1579*7c478bd9Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
1580*7c478bd9Sstevel@tonic-gate 	/*
1581*7c478bd9Sstevel@tonic-gate 	 * Assert we are not already on the list, that is, that
1582*7c478bd9Sstevel@tonic-gate 	 * this lwp did not call addfpollinfo twice for the same fd.
1583*7c478bd9Sstevel@tonic-gate 	 */
1584*7c478bd9Sstevel@tonic-gate 	ASSERT(!curthread_in_plist(ufp));
1585*7c478bd9Sstevel@tonic-gate 	/*
1586*7c478bd9Sstevel@tonic-gate 	 * addfpollinfo is always done inside the getf/releasef pair.
1587*7c478bd9Sstevel@tonic-gate 	 */
1588*7c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_refcnt >= 1);
1589*7c478bd9Sstevel@tonic-gate 	fpip->fp_next = ufp->uf_fpollinfo;
1590*7c478bd9Sstevel@tonic-gate 	ufp->uf_fpollinfo = fpip;
1591*7c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
1592*7c478bd9Sstevel@tonic-gate }
1593*7c478bd9Sstevel@tonic-gate 
1594*7c478bd9Sstevel@tonic-gate /*
1595*7c478bd9Sstevel@tonic-gate  * delete curthread from fpollinfo list.
1596*7c478bd9Sstevel@tonic-gate  */
1597*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1598*7c478bd9Sstevel@tonic-gate void
1599*7c478bd9Sstevel@tonic-gate delfpollinfo(int fd)
1600*7c478bd9Sstevel@tonic-gate {
1601*7c478bd9Sstevel@tonic-gate 	struct uf_entry *ufp;
1602*7c478bd9Sstevel@tonic-gate 	struct fpollinfo *fpip;
1603*7c478bd9Sstevel@tonic-gate 	struct fpollinfo **fpipp;
1604*7c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
1605*7c478bd9Sstevel@tonic-gate 
1606*7c478bd9Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
1607*7c478bd9Sstevel@tonic-gate 	if (ufp->uf_fpollinfo == NULL) {
1608*7c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
1609*7c478bd9Sstevel@tonic-gate 		return;
1610*7c478bd9Sstevel@tonic-gate 	}
1611*7c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_busy);
1612*7c478bd9Sstevel@tonic-gate 	/*
1613*7c478bd9Sstevel@tonic-gate 	 * Find and delete curthread from the list.
1614*7c478bd9Sstevel@tonic-gate 	 */
1615*7c478bd9Sstevel@tonic-gate 	fpipp = &ufp->uf_fpollinfo;
1616*7c478bd9Sstevel@tonic-gate 	while ((fpip = *fpipp)->fp_thread != curthread)
1617*7c478bd9Sstevel@tonic-gate 		fpipp = &fpip->fp_next;
1618*7c478bd9Sstevel@tonic-gate 	*fpipp = fpip->fp_next;
1619*7c478bd9Sstevel@tonic-gate 	kmem_free(fpip, sizeof (fpollinfo_t));
1620*7c478bd9Sstevel@tonic-gate 	/*
1621*7c478bd9Sstevel@tonic-gate 	 * Assert that we are not still on the list, that is, that
1622*7c478bd9Sstevel@tonic-gate 	 * this lwp did not call addfpollinfo twice for the same fd.
1623*7c478bd9Sstevel@tonic-gate 	 */
1624*7c478bd9Sstevel@tonic-gate 	ASSERT(!curthread_in_plist(ufp));
1625*7c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
1626*7c478bd9Sstevel@tonic-gate }
1627*7c478bd9Sstevel@tonic-gate 
1628*7c478bd9Sstevel@tonic-gate /*
1629*7c478bd9Sstevel@tonic-gate  * fd is associated with a port. pfd is a pointer to the fd entry in the
1630*7c478bd9Sstevel@tonic-gate  * cache of the port.
1631*7c478bd9Sstevel@tonic-gate  */
1632*7c478bd9Sstevel@tonic-gate 
1633*7c478bd9Sstevel@tonic-gate void
1634*7c478bd9Sstevel@tonic-gate addfd_port(int fd, portfd_t *pfd)
1635*7c478bd9Sstevel@tonic-gate {
1636*7c478bd9Sstevel@tonic-gate 	struct uf_entry *ufp;
1637*7c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
1638*7c478bd9Sstevel@tonic-gate 
1639*7c478bd9Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
1640*7c478bd9Sstevel@tonic-gate 	/*
1641*7c478bd9Sstevel@tonic-gate 	 * addfd_port is always done inside the getf/releasef pair.
1642*7c478bd9Sstevel@tonic-gate 	 */
1643*7c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_refcnt >= 1);
1644*7c478bd9Sstevel@tonic-gate 	if (ufp->uf_portfd == NULL) {
1645*7c478bd9Sstevel@tonic-gate 		/* first entry */
1646*7c478bd9Sstevel@tonic-gate 		ufp->uf_portfd = pfd;
1647*7c478bd9Sstevel@tonic-gate 		pfd->pfd_next = NULL;
1648*7c478bd9Sstevel@tonic-gate 	} else {
1649*7c478bd9Sstevel@tonic-gate 		pfd->pfd_next = ufp->uf_portfd;
1650*7c478bd9Sstevel@tonic-gate 		ufp->uf_portfd = pfd;
1651*7c478bd9Sstevel@tonic-gate 		pfd->pfd_next->pfd_prev = pfd;
1652*7c478bd9Sstevel@tonic-gate 	}
1653*7c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
1654*7c478bd9Sstevel@tonic-gate }
1655*7c478bd9Sstevel@tonic-gate 
1656*7c478bd9Sstevel@tonic-gate void
1657*7c478bd9Sstevel@tonic-gate delfd_port(int fd, portfd_t *pfd)
1658*7c478bd9Sstevel@tonic-gate {
1659*7c478bd9Sstevel@tonic-gate 	struct uf_entry *ufp;
1660*7c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
1661*7c478bd9Sstevel@tonic-gate 
1662*7c478bd9Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
1663*7c478bd9Sstevel@tonic-gate 	/*
1664*7c478bd9Sstevel@tonic-gate 	 * delfd_port is always done inside the getf/releasef pair.
1665*7c478bd9Sstevel@tonic-gate 	 */
1666*7c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_refcnt >= 1);
1667*7c478bd9Sstevel@tonic-gate 	if (ufp->uf_portfd == pfd) {
1668*7c478bd9Sstevel@tonic-gate 		/* remove first entry */
1669*7c478bd9Sstevel@tonic-gate 		ufp->uf_portfd = pfd->pfd_next;
1670*7c478bd9Sstevel@tonic-gate 	} else {
1671*7c478bd9Sstevel@tonic-gate 		pfd->pfd_prev->pfd_next = pfd->pfd_next;
1672*7c478bd9Sstevel@tonic-gate 		if (pfd->pfd_next != NULL)
1673*7c478bd9Sstevel@tonic-gate 			pfd->pfd_next->pfd_prev = pfd->pfd_prev;
1674*7c478bd9Sstevel@tonic-gate 	}
1675*7c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
1676*7c478bd9Sstevel@tonic-gate }
1677*7c478bd9Sstevel@tonic-gate 
1678*7c478bd9Sstevel@tonic-gate static void
1679*7c478bd9Sstevel@tonic-gate port_close_fd(portfd_t *pfd, int fd)
1680*7c478bd9Sstevel@tonic-gate {
1681*7c478bd9Sstevel@tonic-gate 	portfd_t	*pfdn;
1682*7c478bd9Sstevel@tonic-gate 	struct uf_entry *ufp;
1683*7c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
1684*7c478bd9Sstevel@tonic-gate 
1685*7c478bd9Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
1686*7c478bd9Sstevel@tonic-gate 	for (; pfd != NULL; pfd = pfdn) {
1687*7c478bd9Sstevel@tonic-gate 		pfdn = pfd->pfd_next;
1688*7c478bd9Sstevel@tonic-gate 		port_close_pfd(pfd);
1689*7c478bd9Sstevel@tonic-gate 	}
1690*7c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
1691*7c478bd9Sstevel@tonic-gate }
1692