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