xref: /illumos-gate/usr/src/uts/common/os/fio.c (revision cf96e8bf1ffaa8e64318435100a5e29e9d7971c7)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a5f69788Scraigm  * Common Development and Distribution License (the "License").
6a5f69788Scraigm  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21a5f69788Scraigm 
227c478bd9Sstevel@tonic-gate /*
23f57e3e4cSRoger A. Faulkner  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
247a5aac98SJerry Jelinek  * Copyright 2015, Joyent Inc.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27236317e1SRoger A. Faulkner /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28236317e1SRoger A. Faulkner /*	All Rights Reserved */
29236317e1SRoger A. Faulkner 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/systm.h>
347c478bd9Sstevel@tonic-gate #include <sys/errno.h>
357c478bd9Sstevel@tonic-gate #include <sys/signal.h>
367c478bd9Sstevel@tonic-gate #include <sys/cred.h>
377c478bd9Sstevel@tonic-gate #include <sys/user.h>
387c478bd9Sstevel@tonic-gate #include <sys/conf.h>
397c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
407c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
417c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
427c478bd9Sstevel@tonic-gate #include <sys/file.h>
437a5aac98SJerry Jelinek #include <sys/flock.h>
447c478bd9Sstevel@tonic-gate #include <sys/proc.h>
457c478bd9Sstevel@tonic-gate #include <sys/var.h>
467c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
477c478bd9Sstevel@tonic-gate #include <sys/open.h>
487c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
497c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
507c478bd9Sstevel@tonic-gate #include <sys/procset.h>
517c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
527c478bd9Sstevel@tonic-gate #include <sys/debug.h>
537c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
547c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
557c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
567c478bd9Sstevel@tonic-gate #include <sys/poll.h>
577c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
587c478bd9Sstevel@tonic-gate #include <sys/port_impl.h>
59b0f673c4SBryan Cantrill #include <sys/dtrace.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #include <c2/audit.h>
627c478bd9Sstevel@tonic-gate #include <sys/nbmlock.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #ifdef DEBUG
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static uint32_t afd_maxfd;	/* # of entries in maximum allocated array */
677c478bd9Sstevel@tonic-gate static uint32_t afd_alloc;	/* count of kmem_alloc()s */
687c478bd9Sstevel@tonic-gate static uint32_t afd_free;	/* count of kmem_free()s */
697c478bd9Sstevel@tonic-gate static uint32_t afd_wait;	/* count of waits on non-zero ref count */
707c478bd9Sstevel@tonic-gate #define	MAXFD(x)	(afd_maxfd = ((afd_maxfd >= (x))? afd_maxfd : (x)))
711a5e258fSJosef 'Jeff' Sipek #define	COUNT(x)	atomic_inc_32(&x)
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #else	/* DEBUG */
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #define	MAXFD(x)
767c478bd9Sstevel@tonic-gate #define	COUNT(x)
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate kmem_cache_t *file_cache;
817c478bd9Sstevel@tonic-gate 
8261b4b1efSpraks static void port_close_fd(portfd_t *);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * File descriptor allocation.
867c478bd9Sstevel@tonic-gate  *
877c478bd9Sstevel@tonic-gate  * fd_find(fip, minfd) finds the first available descriptor >= minfd.
887c478bd9Sstevel@tonic-gate  * The most common case is open(2), in which minfd = 0, but we must also
897c478bd9Sstevel@tonic-gate  * support fcntl(fd, F_DUPFD, minfd).
907c478bd9Sstevel@tonic-gate  *
917c478bd9Sstevel@tonic-gate  * The algorithm is as follows: we keep all file descriptors in an infix
927c478bd9Sstevel@tonic-gate  * binary tree in which each node records the number of descriptors
937c478bd9Sstevel@tonic-gate  * allocated in its right subtree, including itself.  Starting at minfd,
947c478bd9Sstevel@tonic-gate  * we ascend the tree until we find a non-fully allocated right subtree.
957c478bd9Sstevel@tonic-gate  * We then descend that subtree in a binary search for the smallest fd.
967c478bd9Sstevel@tonic-gate  * Finally, we ascend the tree again to increment the allocation count
977c478bd9Sstevel@tonic-gate  * of every subtree containing the newly-allocated fd.  Freeing an fd
987c478bd9Sstevel@tonic-gate  * requires only the last step: we ascend the tree to decrement allocation
997c478bd9Sstevel@tonic-gate  * counts.  Each of these three steps (ascent to find non-full subtree,
1007c478bd9Sstevel@tonic-gate  * descent to find lowest fd, ascent to update allocation counts) is
1017c478bd9Sstevel@tonic-gate  * O(log n), thus the algorithm as a whole is O(log n).
1027c478bd9Sstevel@tonic-gate  *
1037c478bd9Sstevel@tonic-gate  * We don't implement the fd tree using the customary left/right/parent
1047c478bd9Sstevel@tonic-gate  * pointers, but instead take advantage of the glorious mathematics of
1057c478bd9Sstevel@tonic-gate  * full infix binary trees.  For reference, here's an illustration of the
1067c478bd9Sstevel@tonic-gate  * logical structure of such a tree, rooted at 4 (binary 100), covering
1077c478bd9Sstevel@tonic-gate  * the range 1-7 (binary 001-111).  Our canonical trees do not include
1087c478bd9Sstevel@tonic-gate  * fd 0; we'll deal with that later.
1097c478bd9Sstevel@tonic-gate  *
1107c478bd9Sstevel@tonic-gate  *	      100
1117c478bd9Sstevel@tonic-gate  *	     /	 \
1127c478bd9Sstevel@tonic-gate  *	    /	  \
1137c478bd9Sstevel@tonic-gate  *	  010	  110
1147c478bd9Sstevel@tonic-gate  *	  / \	  / \
1157c478bd9Sstevel@tonic-gate  *	001 011 101 111
1167c478bd9Sstevel@tonic-gate  *
1177c478bd9Sstevel@tonic-gate  * We make the following observations, all of which are easily proven by
1187c478bd9Sstevel@tonic-gate  * induction on the depth of the tree:
1197c478bd9Sstevel@tonic-gate  *
1207c478bd9Sstevel@tonic-gate  * (T1) The least-significant bit (LSB) of any node is equal to its level
1217c478bd9Sstevel@tonic-gate  *      in the tree.  In our example, nodes 001, 011, 101 and 111 are at
1227c478bd9Sstevel@tonic-gate  *      level 0; nodes 010 and 110 are at level 1; and node 100 is at level 2.
1237c478bd9Sstevel@tonic-gate  *
1247c478bd9Sstevel@tonic-gate  * (T2) The child size (CSIZE) of node N -- that is, the total number of
1257c478bd9Sstevel@tonic-gate  *	right-branch descendants in a child of node N, including itself -- is
1267c478bd9Sstevel@tonic-gate  *	given by clearing all but the least significant bit of N.  This
1277c478bd9Sstevel@tonic-gate  *	follows immediately from (T1).  Applying this rule to our example, we
1287c478bd9Sstevel@tonic-gate  *	see that CSIZE(100) = 100, CSIZE(x10) = 10, and CSIZE(xx1) = 1.
1297c478bd9Sstevel@tonic-gate  *
1307c478bd9Sstevel@tonic-gate  * (T3) The nearest left ancestor (LPARENT) of node N -- that is, the nearest
1317c478bd9Sstevel@tonic-gate  *	ancestor containing node N in its right child -- is given by clearing
1327c478bd9Sstevel@tonic-gate  *	the LSB of N.  For example, LPARENT(111) = 110 and LPARENT(110) = 100.
1337c478bd9Sstevel@tonic-gate  *	Clearing the LSB of nodes 001, 010 or 100 yields zero, reflecting
1347c478bd9Sstevel@tonic-gate  *	the fact that these are leftmost nodes.  Note that this algorithm
1357c478bd9Sstevel@tonic-gate  *	automatically skips generations as necessary.  For example, the parent
1367c478bd9Sstevel@tonic-gate  *      of node 101 is 110, which is a *right* ancestor (not what we want);
1377c478bd9Sstevel@tonic-gate  *      but its grandparent is 100, which is a left ancestor. Clearing the LSB
1387c478bd9Sstevel@tonic-gate  *      of 101 gets us to 100 directly, skipping right past the uninteresting
1397c478bd9Sstevel@tonic-gate  *      generation (110).
1407c478bd9Sstevel@tonic-gate  *
1417c478bd9Sstevel@tonic-gate  *      Note that since LPARENT clears the LSB, whereas CSIZE clears all *but*
1427c478bd9Sstevel@tonic-gate  *	the LSB, we can express LPARENT() nicely in terms of CSIZE():
1437c478bd9Sstevel@tonic-gate  *
1447c478bd9Sstevel@tonic-gate  *	LPARENT(N) = N - CSIZE(N)
1457c478bd9Sstevel@tonic-gate  *
1467c478bd9Sstevel@tonic-gate  * (T4) The nearest right ancestor (RPARENT) of node N is given by:
1477c478bd9Sstevel@tonic-gate  *
1487c478bd9Sstevel@tonic-gate  *	RPARENT(N) = N + CSIZE(N)
1497c478bd9Sstevel@tonic-gate  *
1507c478bd9Sstevel@tonic-gate  * (T5) For every interior node, the children differ from their parent by
1517c478bd9Sstevel@tonic-gate  *	CSIZE(parent) / 2.  In our example, CSIZE(100) / 2 = 2 = 10 binary,
1527c478bd9Sstevel@tonic-gate  *      and indeed, the children of 100 are 100 +/- 10 = 010 and 110.
1537c478bd9Sstevel@tonic-gate  *
1547c478bd9Sstevel@tonic-gate  * Next, we'll need a few two's-complement math tricks.  Suppose a number,
1557c478bd9Sstevel@tonic-gate  * N, has the following form:
1567c478bd9Sstevel@tonic-gate  *
1577c478bd9Sstevel@tonic-gate  *		N = xxxx10...0
1587c478bd9Sstevel@tonic-gate  *
1597c478bd9Sstevel@tonic-gate  * That is, the binary representation of N consists of some string of bits,
1607c478bd9Sstevel@tonic-gate  * then a 1, then all zeroes.  This amounts to nothing more than saying that
1617c478bd9Sstevel@tonic-gate  * N has a least-significant bit, which is true for any N != 0.  If we look
1627c478bd9Sstevel@tonic-gate  * at N and N - 1 together, we see that we can combine them in useful ways:
1637c478bd9Sstevel@tonic-gate  *
1647c478bd9Sstevel@tonic-gate  *		  N = xxxx10...0
1657c478bd9Sstevel@tonic-gate  *	      N - 1 = xxxx01...1
1667c478bd9Sstevel@tonic-gate  *	------------------------
1677c478bd9Sstevel@tonic-gate  *	N & (N - 1) = xxxx000000
1687c478bd9Sstevel@tonic-gate  *	N | (N - 1) = xxxx111111
1697c478bd9Sstevel@tonic-gate  *	N ^ (N - 1) =     111111
1707c478bd9Sstevel@tonic-gate  *
1717c478bd9Sstevel@tonic-gate  * In particular, this suggests several easy ways to clear all but the LSB,
1727c478bd9Sstevel@tonic-gate  * which by (T2) is exactly what we need to determine CSIZE(N) = 10...0.
1737c478bd9Sstevel@tonic-gate  * We'll opt for this formulation:
1747c478bd9Sstevel@tonic-gate  *
1757c478bd9Sstevel@tonic-gate  *	(C1) CSIZE(N) = (N - 1) ^ (N | (N - 1))
1767c478bd9Sstevel@tonic-gate  *
1777c478bd9Sstevel@tonic-gate  * Similarly, we have an easy way to determine LPARENT(N), which requires
1787c478bd9Sstevel@tonic-gate  * that we clear the LSB of N:
1797c478bd9Sstevel@tonic-gate  *
1807c478bd9Sstevel@tonic-gate  *	(L1) LPARENT(N) = N & (N - 1)
1817c478bd9Sstevel@tonic-gate  *
1827c478bd9Sstevel@tonic-gate  * We note in the above relations that (N | (N - 1)) - N = CSIZE(N) - 1.
1837c478bd9Sstevel@tonic-gate  * When combined with (T4), this yields an easy way to compute RPARENT(N):
1847c478bd9Sstevel@tonic-gate  *
1857c478bd9Sstevel@tonic-gate  *	(R1) RPARENT(N) = (N | (N - 1)) + 1
1867c478bd9Sstevel@tonic-gate  *
1877c478bd9Sstevel@tonic-gate  * Finally, to accommodate fd 0 we must adjust all of our results by +/-1 to
1887c478bd9Sstevel@tonic-gate  * move the fd range from [1, 2^n) to [0, 2^n - 1).  This is straightforward,
1897c478bd9Sstevel@tonic-gate  * so there's no need to belabor the algebra; the revised relations become:
1907c478bd9Sstevel@tonic-gate  *
1917c478bd9Sstevel@tonic-gate  *	(C1a) CSIZE(N) = N ^ (N | (N + 1))
1927c478bd9Sstevel@tonic-gate  *
1937c478bd9Sstevel@tonic-gate  *	(L1a) LPARENT(N) = (N & (N + 1)) - 1
1947c478bd9Sstevel@tonic-gate  *
1957c478bd9Sstevel@tonic-gate  *	(R1a) RPARENT(N) = N | (N + 1)
1967c478bd9Sstevel@tonic-gate  *
1977c478bd9Sstevel@tonic-gate  * This completes the mathematical framework.  We now have all the tools
1987c478bd9Sstevel@tonic-gate  * we need to implement fd_find() and fd_reserve().
1997c478bd9Sstevel@tonic-gate  *
2007c478bd9Sstevel@tonic-gate  * fd_find(fip, minfd) finds the smallest available file descriptor >= minfd.
2017c478bd9Sstevel@tonic-gate  * It does not actually allocate the descriptor; that's done by fd_reserve().
2027c478bd9Sstevel@tonic-gate  * fd_find() proceeds in two steps:
2037c478bd9Sstevel@tonic-gate  *
2047c478bd9Sstevel@tonic-gate  * (1) Find the leftmost subtree that contains a descriptor >= minfd.
2057c478bd9Sstevel@tonic-gate  *     We start at the right subtree rooted at minfd.  If this subtree is
2067c478bd9Sstevel@tonic-gate  *     not full -- if fip->fi_list[minfd].uf_alloc != CSIZE(minfd) -- then
2077c478bd9Sstevel@tonic-gate  *     step 1 is done.  Otherwise, we know that all fds in this subtree
2087c478bd9Sstevel@tonic-gate  *     are taken, so we ascend to RPARENT(minfd) using (R1a).  We repeat
2097c478bd9Sstevel@tonic-gate  *     this process until we either find a candidate subtree or exceed
2107c478bd9Sstevel@tonic-gate  *     fip->fi_nfiles.  We use (C1a) to compute CSIZE().
2117c478bd9Sstevel@tonic-gate  *
2127c478bd9Sstevel@tonic-gate  * (2) Find the smallest fd in the subtree discovered by step 1.
2137c478bd9Sstevel@tonic-gate  *     Starting at the root of this subtree, we descend to find the
2147c478bd9Sstevel@tonic-gate  *     smallest available fd.  Since the left children have the smaller
2157c478bd9Sstevel@tonic-gate  *     fds, we will descend rightward only when the left child is full.
2167c478bd9Sstevel@tonic-gate  *
2177c478bd9Sstevel@tonic-gate  *     We begin by comparing the number of allocated fds in the root
2187c478bd9Sstevel@tonic-gate  *     to the number of allocated fds in its right child; if they differ
2197c478bd9Sstevel@tonic-gate  *     by exactly CSIZE(child), we know the left subtree is full, so we
2207c478bd9Sstevel@tonic-gate  *     descend right; that is, the right child becomes the search root.
2217c478bd9Sstevel@tonic-gate  *     Otherwise we leave the root alone and start following the right
2227c478bd9Sstevel@tonic-gate  *     child's left children.  As fortune would have it, this is very
2237c478bd9Sstevel@tonic-gate  *     simple computationally: by (T5), the right child of fd is just
2247c478bd9Sstevel@tonic-gate  *     fd + size, where size = CSIZE(fd) / 2.  Applying (T5) again,
2257c478bd9Sstevel@tonic-gate  *     we find that the right child's left child is fd + size - (size / 2) =
2267c478bd9Sstevel@tonic-gate  *     fd + (size / 2); *its* left child is fd + (size / 2) - (size / 4) =
2277c478bd9Sstevel@tonic-gate  *     fd + (size / 4), and so on.  In general, fd's right child's
2287c478bd9Sstevel@tonic-gate  *     leftmost nth descendant is fd + (size >> n).  Thus, to follow
2297c478bd9Sstevel@tonic-gate  *     the right child's left descendants, we just halve the size in
2307c478bd9Sstevel@tonic-gate  *     each iteration of the search.
2317c478bd9Sstevel@tonic-gate  *
2327c478bd9Sstevel@tonic-gate  *     When we descend leftward, we must keep track of the number of fds
2337c478bd9Sstevel@tonic-gate  *     that were allocated in all the right subtrees we rejected, so we
2347c478bd9Sstevel@tonic-gate  *     know how many of the root fd's allocations are in the remaining
2357c478bd9Sstevel@tonic-gate  *     (as yet unexplored) leftmost part of its right subtree.  When we
2367c478bd9Sstevel@tonic-gate  *     encounter a fully-allocated left child -- that is, when we find
2377c478bd9Sstevel@tonic-gate  *     that fip->fi_list[fd].uf_alloc == ralloc + size -- we descend right
2387c478bd9Sstevel@tonic-gate  *     (as described earlier), resetting ralloc to zero.
2397c478bd9Sstevel@tonic-gate  *
2407c478bd9Sstevel@tonic-gate  * fd_reserve(fip, fd, incr) either allocates or frees fd, depending
2417c478bd9Sstevel@tonic-gate  * on whether incr is 1 or -1.  Starting at fd, fd_reserve() ascends
2427c478bd9Sstevel@tonic-gate  * the leftmost ancestors (see (T3)) and updates the allocation counts.
2437c478bd9Sstevel@tonic-gate  * At each step we use (L1a) to compute LPARENT(), the next left ancestor.
2447c478bd9Sstevel@tonic-gate  *
2457c478bd9Sstevel@tonic-gate  * flist_minsize() finds the minimal tree that still covers all
2467c478bd9Sstevel@tonic-gate  * used fds; as long as the allocation count of a root node is zero, we
2477c478bd9Sstevel@tonic-gate  * don't need that node or its right subtree.
2487c478bd9Sstevel@tonic-gate  *
2497c478bd9Sstevel@tonic-gate  * flist_nalloc() counts the number of allocated fds in the tree, by starting
2507c478bd9Sstevel@tonic-gate  * at the top of the tree and summing the right-subtree allocation counts as
2517c478bd9Sstevel@tonic-gate  * it descends leftwards.
2527c478bd9Sstevel@tonic-gate  *
2537c478bd9Sstevel@tonic-gate  * Note: we assume that flist_grow() will keep fip->fi_nfiles of the form
2547c478bd9Sstevel@tonic-gate  * 2^n - 1.  This ensures that the fd trees are always full, which saves
2557c478bd9Sstevel@tonic-gate  * quite a bit of boundary checking.
2567c478bd9Sstevel@tonic-gate  */
2577c478bd9Sstevel@tonic-gate static int
fd_find(uf_info_t * fip,int minfd)2587c478bd9Sstevel@tonic-gate fd_find(uf_info_t *fip, int minfd)
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate 	int size, ralloc, fd;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fip->fi_lock));
2637c478bd9Sstevel@tonic-gate 	ASSERT((fip->fi_nfiles & (fip->fi_nfiles + 1)) == 0);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	for (fd = minfd; (uint_t)fd < fip->fi_nfiles; fd |= fd + 1) {
2667c478bd9Sstevel@tonic-gate 		size = fd ^ (fd | (fd + 1));
2677c478bd9Sstevel@tonic-gate 		if (fip->fi_list[fd].uf_alloc == size)
2687c478bd9Sstevel@tonic-gate 			continue;
2697c478bd9Sstevel@tonic-gate 		for (ralloc = 0, size >>= 1; size != 0; size >>= 1) {
2707c478bd9Sstevel@tonic-gate 			ralloc += fip->fi_list[fd + size].uf_alloc;
2717c478bd9Sstevel@tonic-gate 			if (fip->fi_list[fd].uf_alloc == ralloc + size) {
2727c478bd9Sstevel@tonic-gate 				fd += size;
2737c478bd9Sstevel@tonic-gate 				ralloc = 0;
2747c478bd9Sstevel@tonic-gate 			}
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate 		return (fd);
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 	return (-1);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate static void
fd_reserve(uf_info_t * fip,int fd,int incr)2827c478bd9Sstevel@tonic-gate fd_reserve(uf_info_t *fip, int fd, int incr)
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate 	int pfd;
2857c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp = &fip->fi_list[fd];
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	ASSERT((uint_t)fd < fip->fi_nfiles);
2887c478bd9Sstevel@tonic-gate 	ASSERT((ufp->uf_busy == 0 && incr == 1) ||
2897c478bd9Sstevel@tonic-gate 	    (ufp->uf_busy == 1 && incr == -1));
2907c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ufp->uf_lock));
2917c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fip->fi_lock));
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	for (pfd = fd; pfd >= 0; pfd = (pfd & (pfd + 1)) - 1)
2947c478bd9Sstevel@tonic-gate 		fip->fi_list[pfd].uf_alloc += incr;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	ufp->uf_busy += incr;
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate static int
flist_minsize(uf_info_t * fip)3007c478bd9Sstevel@tonic-gate flist_minsize(uf_info_t *fip)
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate 	int fd;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/*
3057c478bd9Sstevel@tonic-gate 	 * We'd like to ASSERT(MUTEX_HELD(&fip->fi_lock)), but we're called
3067c478bd9Sstevel@tonic-gate 	 * by flist_fork(), which relies on other mechanisms for mutual
3077c478bd9Sstevel@tonic-gate 	 * exclusion.
3087c478bd9Sstevel@tonic-gate 	 */
3097c478bd9Sstevel@tonic-gate 	ASSERT((fip->fi_nfiles & (fip->fi_nfiles + 1)) == 0);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	for (fd = fip->fi_nfiles; fd != 0; fd >>= 1)
3127c478bd9Sstevel@tonic-gate 		if (fip->fi_list[fd >> 1].uf_alloc != 0)
3137c478bd9Sstevel@tonic-gate 			break;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	return (fd);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate static int
flist_nalloc(uf_info_t * fip)3197c478bd9Sstevel@tonic-gate flist_nalloc(uf_info_t *fip)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	int fd;
3227c478bd9Sstevel@tonic-gate 	int nalloc = 0;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fip->fi_lock));
3257c478bd9Sstevel@tonic-gate 	ASSERT((fip->fi_nfiles & (fip->fi_nfiles + 1)) == 0);
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	for (fd = fip->fi_nfiles; fd != 0; fd >>= 1)
3287c478bd9Sstevel@tonic-gate 		nalloc += fip->fi_list[fd >> 1].uf_alloc;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	return (nalloc);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate  * Increase size of the fi_list array to accommodate at least maxfd.
3357c478bd9Sstevel@tonic-gate  * We keep the size of the form 2^n - 1 for benefit of fd_find().
3367c478bd9Sstevel@tonic-gate  */
3377c478bd9Sstevel@tonic-gate static void
flist_grow(int maxfd)3387c478bd9Sstevel@tonic-gate flist_grow(int maxfd)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
3417c478bd9Sstevel@tonic-gate 	int newcnt, oldcnt;
3427c478bd9Sstevel@tonic-gate 	uf_entry_t *src, *dst, *newlist, *oldlist, *newend, *oldend;
3437c478bd9Sstevel@tonic-gate 	uf_rlist_t *urp;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	for (newcnt = 1; newcnt <= maxfd; newcnt = (newcnt << 1) | 1)
3467c478bd9Sstevel@tonic-gate 		continue;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	newlist = kmem_zalloc(newcnt * sizeof (uf_entry_t), KM_SLEEP);
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	mutex_enter(&fip->fi_lock);
3517c478bd9Sstevel@tonic-gate 	oldcnt = fip->fi_nfiles;
3527c478bd9Sstevel@tonic-gate 	if (newcnt <= oldcnt) {
3537c478bd9Sstevel@tonic-gate 		mutex_exit(&fip->fi_lock);
3547c478bd9Sstevel@tonic-gate 		kmem_free(newlist, newcnt * sizeof (uf_entry_t));
3557c478bd9Sstevel@tonic-gate 		return;
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 	ASSERT((newcnt & (newcnt + 1)) == 0);
3587c478bd9Sstevel@tonic-gate 	oldlist = fip->fi_list;
3597c478bd9Sstevel@tonic-gate 	oldend = oldlist + oldcnt;
3607c478bd9Sstevel@tonic-gate 	newend = newlist + oldcnt;	/* no need to lock beyond old end */
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	/*
3637c478bd9Sstevel@tonic-gate 	 * fi_list and fi_nfiles cannot change while any uf_lock is held,
3647c478bd9Sstevel@tonic-gate 	 * so we must grab all the old locks *and* the new locks up to oldcnt.
3657c478bd9Sstevel@tonic-gate 	 * (Locks beyond the end of oldcnt aren't visible until we store
3667c478bd9Sstevel@tonic-gate 	 * the new fi_nfiles, which is the last thing we do before dropping
3677c478bd9Sstevel@tonic-gate 	 * all the locks, so there's no need to acquire these locks).
3687c478bd9Sstevel@tonic-gate 	 * Holding the new locks is necessary because when fi_list changes
3697c478bd9Sstevel@tonic-gate 	 * to point to the new list, fi_nfiles won't have been stored yet.
3707c478bd9Sstevel@tonic-gate 	 * If we *didn't* hold the new locks, someone doing a UF_ENTER()
3717c478bd9Sstevel@tonic-gate 	 * could see the new fi_list, grab the new uf_lock, and then see
3727c478bd9Sstevel@tonic-gate 	 * fi_nfiles change while the lock is held -- in violation of
3737c478bd9Sstevel@tonic-gate 	 * UF_ENTER() semantics.
3747c478bd9Sstevel@tonic-gate 	 */
3757c478bd9Sstevel@tonic-gate 	for (src = oldlist; src < oldend; src++)
3767c478bd9Sstevel@tonic-gate 		mutex_enter(&src->uf_lock);
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	for (dst = newlist; dst < newend; dst++)
3797c478bd9Sstevel@tonic-gate 		mutex_enter(&dst->uf_lock);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	for (src = oldlist, dst = newlist; src < oldend; src++, dst++) {
3827c478bd9Sstevel@tonic-gate 		dst->uf_file = src->uf_file;
3837c478bd9Sstevel@tonic-gate 		dst->uf_fpollinfo = src->uf_fpollinfo;
3847c478bd9Sstevel@tonic-gate 		dst->uf_refcnt = src->uf_refcnt;
3857c478bd9Sstevel@tonic-gate 		dst->uf_alloc = src->uf_alloc;
3867c478bd9Sstevel@tonic-gate 		dst->uf_flag = src->uf_flag;
3877c478bd9Sstevel@tonic-gate 		dst->uf_busy = src->uf_busy;
3887c478bd9Sstevel@tonic-gate 		dst->uf_portfd = src->uf_portfd;
389086d9687SPatrick Mooney 		dst->uf_gen = src->uf_gen;
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	/*
3937c478bd9Sstevel@tonic-gate 	 * As soon as we store the new flist, future locking operations
3947c478bd9Sstevel@tonic-gate 	 * will use it.  Therefore, we must ensure that all the state
3957c478bd9Sstevel@tonic-gate 	 * we've just established reaches global visibility before the
3967c478bd9Sstevel@tonic-gate 	 * new flist does.
3977c478bd9Sstevel@tonic-gate 	 */
3987c478bd9Sstevel@tonic-gate 	membar_producer();
3997c478bd9Sstevel@tonic-gate 	fip->fi_list = newlist;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	/*
4027c478bd9Sstevel@tonic-gate 	 * Routines like getf() make an optimistic check on the validity
4037c478bd9Sstevel@tonic-gate 	 * of the supplied file descriptor: if it's less than the current
4047c478bd9Sstevel@tonic-gate 	 * value of fi_nfiles -- examined without any locks -- then it's
4057c478bd9Sstevel@tonic-gate 	 * safe to attempt a UF_ENTER() on that fd (which is a valid
4067c478bd9Sstevel@tonic-gate 	 * assumption because fi_nfiles only increases).  Therefore, it
4077c478bd9Sstevel@tonic-gate 	 * is critical that the new value of fi_nfiles not reach global
4087c478bd9Sstevel@tonic-gate 	 * visibility until after the new fi_list: if it happened the
4097c478bd9Sstevel@tonic-gate 	 * other way around, getf() could see the new fi_nfiles and attempt
4107c478bd9Sstevel@tonic-gate 	 * a UF_ENTER() on the old fi_list, which would write beyond its
4117c478bd9Sstevel@tonic-gate 	 * end if the fd exceeded the old fi_nfiles.
4127c478bd9Sstevel@tonic-gate 	 */
4137c478bd9Sstevel@tonic-gate 	membar_producer();
4147c478bd9Sstevel@tonic-gate 	fip->fi_nfiles = newcnt;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	/*
4177c478bd9Sstevel@tonic-gate 	 * The new state is consistent now, so we can drop all the locks.
4187c478bd9Sstevel@tonic-gate 	 */
4197c478bd9Sstevel@tonic-gate 	for (dst = newlist; dst < newend; dst++)
4207c478bd9Sstevel@tonic-gate 		mutex_exit(&dst->uf_lock);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	for (src = oldlist; src < oldend; src++) {
4237c478bd9Sstevel@tonic-gate 		/*
4247c478bd9Sstevel@tonic-gate 		 * If any threads are blocked on the old cvs, wake them.
4257c478bd9Sstevel@tonic-gate 		 * This will force them to wake up, discover that fi_list
4267c478bd9Sstevel@tonic-gate 		 * has changed, and go back to sleep on the new cvs.
4277c478bd9Sstevel@tonic-gate 		 */
4287c478bd9Sstevel@tonic-gate 		cv_broadcast(&src->uf_wanted_cv);
4297c478bd9Sstevel@tonic-gate 		cv_broadcast(&src->uf_closing_cv);
4307c478bd9Sstevel@tonic-gate 		mutex_exit(&src->uf_lock);
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	/*
4367c478bd9Sstevel@tonic-gate 	 * Retire the old flist.  We can't actually kmem_free() it now
4377c478bd9Sstevel@tonic-gate 	 * because someone may still have a pointer to it.  Instead,
4387c478bd9Sstevel@tonic-gate 	 * we link it onto a list of retired flists.  The new flist
4397c478bd9Sstevel@tonic-gate 	 * is at least double the size of the previous flist, so the
4407c478bd9Sstevel@tonic-gate 	 * total size of all retired flists will be less than the size
4417c478bd9Sstevel@tonic-gate 	 * of the current one (to prove, consider the sum of a geometric
4427c478bd9Sstevel@tonic-gate 	 * series in powers of 2).  exit() frees the retired flists.
4437c478bd9Sstevel@tonic-gate 	 */
4447c478bd9Sstevel@tonic-gate 	urp = kmem_zalloc(sizeof (uf_rlist_t), KM_SLEEP);
4457c478bd9Sstevel@tonic-gate 	urp->ur_list = oldlist;
4467c478bd9Sstevel@tonic-gate 	urp->ur_nfiles = oldcnt;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	mutex_enter(&fip->fi_lock);
4497c478bd9Sstevel@tonic-gate 	urp->ur_next = fip->fi_rlist;
4507c478bd9Sstevel@tonic-gate 	fip->fi_rlist = urp;
4517c478bd9Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate /*
4557c478bd9Sstevel@tonic-gate  * Utility functions for keeping track of the active file descriptors.
4567c478bd9Sstevel@tonic-gate  */
4577c478bd9Sstevel@tonic-gate void
clear_stale_fd()4587c478bd9Sstevel@tonic-gate clear_stale_fd()		/* called from post_syscall() */
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate 	afd_t *afd = &curthread->t_activefd;
4617c478bd9Sstevel@tonic-gate 	int i;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	/* uninitialized is ok here, a_nfd is then zero */
4647c478bd9Sstevel@tonic-gate 	for (i = 0; i < afd->a_nfd; i++) {
4657c478bd9Sstevel@tonic-gate 		/* assert that this should not be necessary */
4667c478bd9Sstevel@tonic-gate 		ASSERT(afd->a_fd[i] == -1);
4677c478bd9Sstevel@tonic-gate 		afd->a_fd[i] = -1;
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 	afd->a_stale = 0;
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate void
free_afd(afd_t * afd)4737c478bd9Sstevel@tonic-gate free_afd(afd_t *afd)		/* called below and from thread_free() */
4747c478bd9Sstevel@tonic-gate {
4757c478bd9Sstevel@tonic-gate 	int i;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/* free the buffer if it was kmem_alloc()ed */
4787c478bd9Sstevel@tonic-gate 	if (afd->a_nfd > sizeof (afd->a_buf) / sizeof (afd->a_buf[0])) {
4797c478bd9Sstevel@tonic-gate 		COUNT(afd_free);
4807c478bd9Sstevel@tonic-gate 		kmem_free(afd->a_fd, afd->a_nfd * sizeof (afd->a_fd[0]));
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	/* (re)initialize the structure */
4847c478bd9Sstevel@tonic-gate 	afd->a_fd = &afd->a_buf[0];
4857c478bd9Sstevel@tonic-gate 	afd->a_nfd = sizeof (afd->a_buf) / sizeof (afd->a_buf[0]);
4867c478bd9Sstevel@tonic-gate 	afd->a_stale = 0;
4877c478bd9Sstevel@tonic-gate 	for (i = 0; i < afd->a_nfd; i++)
4887c478bd9Sstevel@tonic-gate 		afd->a_fd[i] = -1;
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate static void
set_active_fd(int fd)4927c478bd9Sstevel@tonic-gate set_active_fd(int fd)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 	afd_t *afd = &curthread->t_activefd;
4957c478bd9Sstevel@tonic-gate 	int i;
4967c478bd9Sstevel@tonic-gate 	int *old_fd;
4977c478bd9Sstevel@tonic-gate 	int old_nfd;
498236317e1SRoger A. Faulkner 	int *new_fd;
499236317e1SRoger A. Faulkner 	int new_nfd;
5007c478bd9Sstevel@tonic-gate 
501236317e1SRoger A. Faulkner 	if (afd->a_nfd == 0) {	/* first time initialization */
502236317e1SRoger A. Faulkner 		ASSERT(fd == -1);
503236317e1SRoger A. Faulkner 		mutex_enter(&afd->a_fdlock);
5047c478bd9Sstevel@tonic-gate 		free_afd(afd);
505236317e1SRoger A. Faulkner 		mutex_exit(&afd->a_fdlock);
506236317e1SRoger A. Faulkner 	}
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	/* insert fd into vacant slot, if any */
5097c478bd9Sstevel@tonic-gate 	for (i = 0; i < afd->a_nfd; i++) {
5107c478bd9Sstevel@tonic-gate 		if (afd->a_fd[i] == -1) {
5117c478bd9Sstevel@tonic-gate 			afd->a_fd[i] = fd;
5127c478bd9Sstevel@tonic-gate 			return;
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	/*
5177c478bd9Sstevel@tonic-gate 	 * Reallocate the a_fd[] array to add one more slot.
5187c478bd9Sstevel@tonic-gate 	 */
519236317e1SRoger A. Faulkner 	ASSERT(fd == -1);
5207c478bd9Sstevel@tonic-gate 	old_nfd = afd->a_nfd;
521236317e1SRoger A. Faulkner 	old_fd = afd->a_fd;
522236317e1SRoger A. Faulkner 	new_nfd = old_nfd + 1;
523236317e1SRoger A. Faulkner 	new_fd = kmem_alloc(new_nfd * sizeof (afd->a_fd[0]), KM_SLEEP);
524236317e1SRoger A. Faulkner 	MAXFD(new_nfd);
5257c478bd9Sstevel@tonic-gate 	COUNT(afd_alloc);
526236317e1SRoger A. Faulkner 
527236317e1SRoger A. Faulkner 	mutex_enter(&afd->a_fdlock);
528236317e1SRoger A. Faulkner 	afd->a_fd = new_fd;
529236317e1SRoger A. Faulkner 	afd->a_nfd = new_nfd;
5307c478bd9Sstevel@tonic-gate 	for (i = 0; i < old_nfd; i++)
5317c478bd9Sstevel@tonic-gate 		afd->a_fd[i] = old_fd[i];
5327c478bd9Sstevel@tonic-gate 	afd->a_fd[i] = fd;
533236317e1SRoger A. Faulkner 	mutex_exit(&afd->a_fdlock);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	if (old_nfd > sizeof (afd->a_buf) / sizeof (afd->a_buf[0])) {
5367c478bd9Sstevel@tonic-gate 		COUNT(afd_free);
5377c478bd9Sstevel@tonic-gate 		kmem_free(old_fd, old_nfd * sizeof (afd->a_fd[0]));
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate void
clear_active_fd(int fd)5427c478bd9Sstevel@tonic-gate clear_active_fd(int fd)		/* called below and from aio.c */
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate 	afd_t *afd = &curthread->t_activefd;
5457c478bd9Sstevel@tonic-gate 	int i;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	for (i = 0; i < afd->a_nfd; i++) {
5487c478bd9Sstevel@tonic-gate 		if (afd->a_fd[i] == fd) {
5497c478bd9Sstevel@tonic-gate 			afd->a_fd[i] = -1;
5507c478bd9Sstevel@tonic-gate 			break;
5517c478bd9Sstevel@tonic-gate 		}
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 	ASSERT(i < afd->a_nfd);		/* not found is not ok */
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate /*
5577c478bd9Sstevel@tonic-gate  * Does this thread have this fd active?
5587c478bd9Sstevel@tonic-gate  */
5597c478bd9Sstevel@tonic-gate static int
is_active_fd(kthread_t * t,int fd)5607c478bd9Sstevel@tonic-gate is_active_fd(kthread_t *t, int fd)
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate 	afd_t *afd = &t->t_activefd;
5637c478bd9Sstevel@tonic-gate 	int i;
5647c478bd9Sstevel@tonic-gate 
565236317e1SRoger A. Faulkner 	ASSERT(t != curthread);
566236317e1SRoger A. Faulkner 	mutex_enter(&afd->a_fdlock);
5677c478bd9Sstevel@tonic-gate 	/* uninitialized is ok here, a_nfd is then zero */
5687c478bd9Sstevel@tonic-gate 	for (i = 0; i < afd->a_nfd; i++) {
569236317e1SRoger A. Faulkner 		if (afd->a_fd[i] == fd) {
570236317e1SRoger A. Faulkner 			mutex_exit(&afd->a_fdlock);
5717c478bd9Sstevel@tonic-gate 			return (1);
5727c478bd9Sstevel@tonic-gate 		}
573236317e1SRoger A. Faulkner 	}
574236317e1SRoger A. Faulkner 	mutex_exit(&afd->a_fdlock);
5757c478bd9Sstevel@tonic-gate 	return (0);
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate /*
579086d9687SPatrick Mooney  * Convert a user supplied file descriptor into a pointer to a file structure.
580086d9687SPatrick Mooney  * Only task is to check range of the descriptor (soft resource limit was
581086d9687SPatrick Mooney  * enforced at open time and shouldn't be checked here).
5827c478bd9Sstevel@tonic-gate  */
5837c478bd9Sstevel@tonic-gate file_t *
getf_gen(int fd,uf_entry_gen_t * genp)584086d9687SPatrick Mooney getf_gen(int fd, uf_entry_gen_t *genp)
5857c478bd9Sstevel@tonic-gate {
5867c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
5877c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
5887c478bd9Sstevel@tonic-gate 	file_t *fp;
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	if ((uint_t)fd >= fip->fi_nfiles)
5917c478bd9Sstevel@tonic-gate 		return (NULL);
5927c478bd9Sstevel@tonic-gate 
593236317e1SRoger A. Faulkner 	/*
594236317e1SRoger A. Faulkner 	 * Reserve a slot in the active fd array now so we can call
595236317e1SRoger A. Faulkner 	 * set_active_fd(fd) for real below, while still inside UF_ENTER().
596236317e1SRoger A. Faulkner 	 */
597236317e1SRoger A. Faulkner 	set_active_fd(-1);
598236317e1SRoger A. Faulkner 
5997c478bd9Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
600236317e1SRoger A. Faulkner 
6017c478bd9Sstevel@tonic-gate 	if ((fp = ufp->uf_file) == NULL) {
6027c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
603a5f69788Scraigm 
604a5f69788Scraigm 		if (fd == fip->fi_badfd && fip->fi_action > 0)
605a5f69788Scraigm 			tsignal(curthread, fip->fi_action);
606a5f69788Scraigm 
6077c478bd9Sstevel@tonic-gate 		return (NULL);
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 	ufp->uf_refcnt++;
610086d9687SPatrick Mooney 	if (genp != NULL) {
611086d9687SPatrick Mooney 		*genp = ufp->uf_gen;
612086d9687SPatrick Mooney 	}
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	set_active_fd(fd);	/* record the active file descriptor */
6157c478bd9Sstevel@tonic-gate 
616236317e1SRoger A. Faulkner 	UF_EXIT(ufp);
617236317e1SRoger A. Faulkner 
6187c478bd9Sstevel@tonic-gate 	return (fp);
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate 
621086d9687SPatrick Mooney file_t *
getf(int fd)622086d9687SPatrick Mooney getf(int fd)
623086d9687SPatrick Mooney {
624086d9687SPatrick Mooney 	return (getf_gen(fd, NULL));
625086d9687SPatrick Mooney }
626086d9687SPatrick Mooney 
6277c478bd9Sstevel@tonic-gate /*
6287c478bd9Sstevel@tonic-gate  * Close whatever file currently occupies the file descriptor slot
6297c478bd9Sstevel@tonic-gate  * and install the new file, usually NULL, in the file descriptor slot.
6307c478bd9Sstevel@tonic-gate  * The close must complete before we release the file descriptor slot.
631a5f69788Scraigm  * If newfp != NULL we only return an error if we can't allocate the
632a5f69788Scraigm  * slot so the caller knows that it needs to free the filep;
633a5f69788Scraigm  * in the other cases we return the error number from closef().
6347c478bd9Sstevel@tonic-gate  */
6357c478bd9Sstevel@tonic-gate int
closeandsetf(int fd,file_t * newfp)6367c478bd9Sstevel@tonic-gate closeandsetf(int fd, file_t *newfp)
6377c478bd9Sstevel@tonic-gate {
6387c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
6397c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(p);
6407c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
6417c478bd9Sstevel@tonic-gate 	file_t *fp;
6427c478bd9Sstevel@tonic-gate 	fpollinfo_t *fpip;
6437c478bd9Sstevel@tonic-gate 	portfd_t *pfd;
6447c478bd9Sstevel@tonic-gate 	int error;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	if ((uint_t)fd >= fip->fi_nfiles) {
6477c478bd9Sstevel@tonic-gate 		if (newfp == NULL)
6487c478bd9Sstevel@tonic-gate 			return (EBADF);
6497c478bd9Sstevel@tonic-gate 		flist_grow(fd);
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	if (newfp != NULL) {
6537c478bd9Sstevel@tonic-gate 		/*
6547c478bd9Sstevel@tonic-gate 		 * If ufp is reserved but has no file pointer, it's in the
6557c478bd9Sstevel@tonic-gate 		 * transition between ufalloc() and setf().  We must wait
6567c478bd9Sstevel@tonic-gate 		 * for this transition to complete before assigning the
6577c478bd9Sstevel@tonic-gate 		 * new non-NULL file pointer.
6587c478bd9Sstevel@tonic-gate 		 */
6597c478bd9Sstevel@tonic-gate 		mutex_enter(&fip->fi_lock);
660a5f69788Scraigm 		if (fd == fip->fi_badfd) {
661a5f69788Scraigm 			mutex_exit(&fip->fi_lock);
662a5f69788Scraigm 			if (fip->fi_action > 0)
663a5f69788Scraigm 				tsignal(curthread, fip->fi_action);
664a5f69788Scraigm 			return (EBADF);
665a5f69788Scraigm 		}
6667c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
6677c478bd9Sstevel@tonic-gate 		while (ufp->uf_busy && ufp->uf_file == NULL) {
6687c478bd9Sstevel@tonic-gate 			mutex_exit(&fip->fi_lock);
6697c478bd9Sstevel@tonic-gate 			cv_wait_stop(&ufp->uf_wanted_cv, &ufp->uf_lock, 250);
6707c478bd9Sstevel@tonic-gate 			UF_EXIT(ufp);
6717c478bd9Sstevel@tonic-gate 			mutex_enter(&fip->fi_lock);
6727c478bd9Sstevel@tonic-gate 			UF_ENTER(ufp, fip, fd);
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate 		if ((fp = ufp->uf_file) == NULL) {
6757c478bd9Sstevel@tonic-gate 			ASSERT(ufp->uf_fpollinfo == NULL);
6767c478bd9Sstevel@tonic-gate 			ASSERT(ufp->uf_flag == 0);
6777c478bd9Sstevel@tonic-gate 			fd_reserve(fip, fd, 1);
6787c478bd9Sstevel@tonic-gate 			ufp->uf_file = newfp;
679086d9687SPatrick Mooney 			ufp->uf_gen++;
6807c478bd9Sstevel@tonic-gate 			UF_EXIT(ufp);
6817c478bd9Sstevel@tonic-gate 			mutex_exit(&fip->fi_lock);
6827c478bd9Sstevel@tonic-gate 			return (0);
6837c478bd9Sstevel@tonic-gate 		}
6847c478bd9Sstevel@tonic-gate 		mutex_exit(&fip->fi_lock);
6857c478bd9Sstevel@tonic-gate 	} else {
6867c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
6877c478bd9Sstevel@tonic-gate 		if ((fp = ufp->uf_file) == NULL) {
6887c478bd9Sstevel@tonic-gate 			UF_EXIT(ufp);
6897c478bd9Sstevel@tonic-gate 			return (EBADF);
6907c478bd9Sstevel@tonic-gate 		}
6917c478bd9Sstevel@tonic-gate 	}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_busy);
6947c478bd9Sstevel@tonic-gate 	ufp->uf_file = NULL;
6957c478bd9Sstevel@tonic-gate 	ufp->uf_flag = 0;
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	/*
6987c478bd9Sstevel@tonic-gate 	 * If the file descriptor reference count is non-zero, then
6997c478bd9Sstevel@tonic-gate 	 * some other lwp in the process is performing system call
7007c478bd9Sstevel@tonic-gate 	 * activity on the file.  To avoid blocking here for a long
7017c478bd9Sstevel@tonic-gate 	 * time (the other lwp might be in a long term sleep in its
702236317e1SRoger A. Faulkner 	 * system call), we scan all other lwps in the process to
703236317e1SRoger A. Faulkner 	 * find the ones with this fd as one of their active fds,
704236317e1SRoger A. Faulkner 	 * set their a_stale flag, and set them running if they
705236317e1SRoger A. Faulkner 	 * are in an interruptible sleep so they will emerge from
706236317e1SRoger A. Faulkner 	 * their system calls immediately.  post_syscall() will
7077c478bd9Sstevel@tonic-gate 	 * test the a_stale flag and set errno to EBADF.
7087c478bd9Sstevel@tonic-gate 	 */
7097c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_refcnt == 0 || p->p_lwpcnt > 1);
7107c478bd9Sstevel@tonic-gate 	if (ufp->uf_refcnt > 0) {
711236317e1SRoger A. Faulkner 		kthread_t *t;
712236317e1SRoger A. Faulkner 
713236317e1SRoger A. Faulkner 		/*
714236317e1SRoger A. Faulkner 		 * We call sprlock_proc(p) to ensure that the thread
715236317e1SRoger A. Faulkner 		 * list will not change while we are scanning it.
716236317e1SRoger A. Faulkner 		 * To do this, we must drop ufp->uf_lock and then
717236317e1SRoger A. Faulkner 		 * reacquire it (so we are not holding both p->p_lock
718236317e1SRoger A. Faulkner 		 * and ufp->uf_lock at the same time).  ufp->uf_lock
719236317e1SRoger A. Faulkner 		 * must be held for is_active_fd() to be correct
720236317e1SRoger A. Faulkner 		 * (set_active_fd() is called while holding ufp->uf_lock).
721236317e1SRoger A. Faulkner 		 *
722236317e1SRoger A. Faulkner 		 * This is a convoluted dance, but it is better than
723236317e1SRoger A. Faulkner 		 * the old brute-force method of stopping every thread
724236317e1SRoger A. Faulkner 		 * in the process by calling holdlwps(SHOLDFORK1).
725236317e1SRoger A. Faulkner 		 */
726236317e1SRoger A. Faulkner 
7277c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
7287c478bd9Sstevel@tonic-gate 		COUNT(afd_wait);
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
731236317e1SRoger A. Faulkner 		sprlock_proc(p);
732236317e1SRoger A. Faulkner 		mutex_exit(&p->p_lock);
733236317e1SRoger A. Faulkner 
734236317e1SRoger A. Faulkner 		UF_ENTER(ufp, fip, fd);
735236317e1SRoger A. Faulkner 		ASSERT(ufp->uf_file == NULL);
736236317e1SRoger A. Faulkner 
737236317e1SRoger A. Faulkner 		if (ufp->uf_refcnt > 0) {
738236317e1SRoger A. Faulkner 			for (t = curthread->t_forw;
739236317e1SRoger A. Faulkner 			    t != curthread;
7407c478bd9Sstevel@tonic-gate 			    t = t->t_forw) {
7417c478bd9Sstevel@tonic-gate 				if (is_active_fd(t, fd)) {
742236317e1SRoger A. Faulkner 					thread_lock(t);
7437c478bd9Sstevel@tonic-gate 					t->t_activefd.a_stale = 1;
7447c478bd9Sstevel@tonic-gate 					t->t_post_sys = 1;
745236317e1SRoger A. Faulkner 					if (ISWAKEABLE(t))
746236317e1SRoger A. Faulkner 						setrun_locked(t);
747236317e1SRoger A. Faulkner 					thread_unlock(t);
7487c478bd9Sstevel@tonic-gate 				}
7497c478bd9Sstevel@tonic-gate 			}
7507c478bd9Sstevel@tonic-gate 		}
751236317e1SRoger A. Faulkner 
752236317e1SRoger A. Faulkner 		UF_EXIT(ufp);
753236317e1SRoger A. Faulkner 
754236317e1SRoger A. Faulkner 		mutex_enter(&p->p_lock);
755236317e1SRoger A. Faulkner 		sprunlock(p);
756236317e1SRoger A. Faulkner 
7577c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
7587c478bd9Sstevel@tonic-gate 		ASSERT(ufp->uf_file == NULL);
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	/*
7627c478bd9Sstevel@tonic-gate 	 * Wait for other lwps to stop using this file descriptor.
7637c478bd9Sstevel@tonic-gate 	 */
7647c478bd9Sstevel@tonic-gate 	while (ufp->uf_refcnt > 0) {
7657c478bd9Sstevel@tonic-gate 		cv_wait_stop(&ufp->uf_closing_cv, &ufp->uf_lock, 250);
7667c478bd9Sstevel@tonic-gate 		/*
7677c478bd9Sstevel@tonic-gate 		 * cv_wait_stop() drops ufp->uf_lock, so the file list
7687c478bd9Sstevel@tonic-gate 		 * can change.  Drop the lock on our (possibly) stale
7697c478bd9Sstevel@tonic-gate 		 * ufp and let UF_ENTER() find and lock the current ufp.
7707c478bd9Sstevel@tonic-gate 		 */
7717c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
7727c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate #ifdef DEBUG
7767c478bd9Sstevel@tonic-gate 	/*
7777c478bd9Sstevel@tonic-gate 	 * catch a watchfd on device's pollhead list but not on fpollinfo list
7787c478bd9Sstevel@tonic-gate 	 */
7797c478bd9Sstevel@tonic-gate 	if (ufp->uf_fpollinfo != NULL)
7807c478bd9Sstevel@tonic-gate 		checkwfdlist(fp->f_vnode, ufp->uf_fpollinfo);
7817c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	/*
7847c478bd9Sstevel@tonic-gate 	 * We may need to cleanup some cached poll states in t_pollstate
7857c478bd9Sstevel@tonic-gate 	 * before the fd can be reused. It is important that we don't
7867c478bd9Sstevel@tonic-gate 	 * access a stale thread structure. We will do the cleanup in two
7877c478bd9Sstevel@tonic-gate 	 * phases to avoid deadlock and holding uf_lock for too long.
7887c478bd9Sstevel@tonic-gate 	 * In phase 1, hold the uf_lock and call pollblockexit() to set
7897c478bd9Sstevel@tonic-gate 	 * state in t_pollstate struct so that a thread does not exit on
7907c478bd9Sstevel@tonic-gate 	 * us. In phase 2, we drop the uf_lock and call pollcacheclean().
7917c478bd9Sstevel@tonic-gate 	 */
7927c478bd9Sstevel@tonic-gate 	pfd = ufp->uf_portfd;
7937c478bd9Sstevel@tonic-gate 	ufp->uf_portfd = NULL;
7947c478bd9Sstevel@tonic-gate 	fpip = ufp->uf_fpollinfo;
7957c478bd9Sstevel@tonic-gate 	ufp->uf_fpollinfo = NULL;
7967c478bd9Sstevel@tonic-gate 	if (fpip != NULL)
7977c478bd9Sstevel@tonic-gate 		pollblockexit(fpip);
7987c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
7997c478bd9Sstevel@tonic-gate 	if (fpip != NULL)
8007c478bd9Sstevel@tonic-gate 		pollcacheclean(fpip, fd);
8017c478bd9Sstevel@tonic-gate 	if (pfd)
80261b4b1efSpraks 		port_close_fd(pfd);
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	/*
8057c478bd9Sstevel@tonic-gate 	 * Keep the file descriptor entry reserved across the closef().
8067c478bd9Sstevel@tonic-gate 	 */
8077c478bd9Sstevel@tonic-gate 	error = closef(fp);
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	setf(fd, newfp);
8107c478bd9Sstevel@tonic-gate 
811a5f69788Scraigm 	/* Only return closef() error when closing is all we do */
812a5f69788Scraigm 	return (newfp == NULL ? error : 0);
8137c478bd9Sstevel@tonic-gate }
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate /*
8167c478bd9Sstevel@tonic-gate  * Decrement uf_refcnt; wakeup anyone waiting to close the file.
8177c478bd9Sstevel@tonic-gate  */
8187c478bd9Sstevel@tonic-gate void
releasef(int fd)8197c478bd9Sstevel@tonic-gate releasef(int fd)
8207c478bd9Sstevel@tonic-gate {
8217c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
8227c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
8257c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_refcnt > 0);
826236317e1SRoger A. Faulkner 	clear_active_fd(fd);	/* clear the active file descriptor */
8277c478bd9Sstevel@tonic-gate 	if (--ufp->uf_refcnt == 0)
8287c478bd9Sstevel@tonic-gate 		cv_broadcast(&ufp->uf_closing_cv);
8297c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate /*
8337c478bd9Sstevel@tonic-gate  * Identical to releasef() but can be called from another process.
8347c478bd9Sstevel@tonic-gate  */
8357c478bd9Sstevel@tonic-gate void
areleasef(int fd,uf_info_t * fip)8367c478bd9Sstevel@tonic-gate areleasef(int fd, uf_info_t *fip)
8377c478bd9Sstevel@tonic-gate {
8387c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
8417c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_refcnt > 0);
8427c478bd9Sstevel@tonic-gate 	if (--ufp->uf_refcnt == 0)
8437c478bd9Sstevel@tonic-gate 		cv_broadcast(&ufp->uf_closing_cv);
8447c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate /*
8487c478bd9Sstevel@tonic-gate  * Duplicate all file descriptors across a fork.
8497c478bd9Sstevel@tonic-gate  */
8507c478bd9Sstevel@tonic-gate void
flist_fork(uf_info_t * pfip,uf_info_t * cfip)8517c478bd9Sstevel@tonic-gate flist_fork(uf_info_t *pfip, uf_info_t *cfip)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	int fd, nfiles;
8547c478bd9Sstevel@tonic-gate 	uf_entry_t *pufp, *cufp;
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	mutex_init(&cfip->fi_lock, NULL, MUTEX_DEFAULT, NULL);
8577c478bd9Sstevel@tonic-gate 	cfip->fi_rlist = NULL;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	/*
8607c478bd9Sstevel@tonic-gate 	 * We don't need to hold fi_lock because all other lwp's in the
8617c478bd9Sstevel@tonic-gate 	 * parent have been held.
8627c478bd9Sstevel@tonic-gate 	 */
8637c478bd9Sstevel@tonic-gate 	cfip->fi_nfiles = nfiles = flist_minsize(pfip);
8647c478bd9Sstevel@tonic-gate 
865d1580181SBryan Cantrill 	cfip->fi_list = nfiles == 0 ? NULL :
866d1580181SBryan Cantrill 	    kmem_zalloc(nfiles * sizeof (uf_entry_t), KM_SLEEP);
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	for (fd = 0, pufp = pfip->fi_list, cufp = cfip->fi_list; fd < nfiles;
8697c478bd9Sstevel@tonic-gate 	    fd++, pufp++, cufp++) {
8707c478bd9Sstevel@tonic-gate 		cufp->uf_file = pufp->uf_file;
8717c478bd9Sstevel@tonic-gate 		cufp->uf_alloc = pufp->uf_alloc;
8727c478bd9Sstevel@tonic-gate 		cufp->uf_flag = pufp->uf_flag;
8737c478bd9Sstevel@tonic-gate 		cufp->uf_busy = pufp->uf_busy;
874086d9687SPatrick Mooney 		cufp->uf_gen = pufp->uf_gen;
8757c478bd9Sstevel@tonic-gate 		if (pufp->uf_file == NULL) {
8767c478bd9Sstevel@tonic-gate 			ASSERT(pufp->uf_flag == 0);
8777c478bd9Sstevel@tonic-gate 			if (pufp->uf_busy) {
8787c478bd9Sstevel@tonic-gate 				/*
8797c478bd9Sstevel@tonic-gate 				 * Grab locks to appease ASSERTs in fd_reserve
8807c478bd9Sstevel@tonic-gate 				 */
8817c478bd9Sstevel@tonic-gate 				mutex_enter(&cfip->fi_lock);
8827c478bd9Sstevel@tonic-gate 				mutex_enter(&cufp->uf_lock);
8837c478bd9Sstevel@tonic-gate 				fd_reserve(cfip, fd, -1);
8847c478bd9Sstevel@tonic-gate 				mutex_exit(&cufp->uf_lock);
8857c478bd9Sstevel@tonic-gate 				mutex_exit(&cfip->fi_lock);
8867c478bd9Sstevel@tonic-gate 			}
8877c478bd9Sstevel@tonic-gate 		}
8887c478bd9Sstevel@tonic-gate 	}
8897c478bd9Sstevel@tonic-gate }
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate /*
8927c478bd9Sstevel@tonic-gate  * Close all open file descriptors for the current process.
8937c478bd9Sstevel@tonic-gate  * This is only called from exit(), which is single-threaded,
8947c478bd9Sstevel@tonic-gate  * so we don't need any locking.
8957c478bd9Sstevel@tonic-gate  */
8967c478bd9Sstevel@tonic-gate void
closeall(uf_info_t * fip)8977c478bd9Sstevel@tonic-gate closeall(uf_info_t *fip)
8987c478bd9Sstevel@tonic-gate {
8997c478bd9Sstevel@tonic-gate 	int fd;
9007c478bd9Sstevel@tonic-gate 	file_t *fp;
9017c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	ufp = fip->fi_list;
9047c478bd9Sstevel@tonic-gate 	for (fd = 0; fd < fip->fi_nfiles; fd++, ufp++) {
9057c478bd9Sstevel@tonic-gate 		if ((fp = ufp->uf_file) != NULL) {
9067c478bd9Sstevel@tonic-gate 			ufp->uf_file = NULL;
9077c478bd9Sstevel@tonic-gate 			if (ufp->uf_portfd != NULL) {
90861b4b1efSpraks 				portfd_t *pfd;
9097c478bd9Sstevel@tonic-gate 				/* remove event port association */
91061b4b1efSpraks 				pfd = ufp->uf_portfd;
9117c478bd9Sstevel@tonic-gate 				ufp->uf_portfd = NULL;
91261b4b1efSpraks 				port_close_fd(pfd);
9137c478bd9Sstevel@tonic-gate 			}
9147c478bd9Sstevel@tonic-gate 			ASSERT(ufp->uf_fpollinfo == NULL);
9157c478bd9Sstevel@tonic-gate 			(void) closef(fp);
9167c478bd9Sstevel@tonic-gate 		}
9177c478bd9Sstevel@tonic-gate 	}
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	kmem_free(fip->fi_list, fip->fi_nfiles * sizeof (uf_entry_t));
9207c478bd9Sstevel@tonic-gate 	fip->fi_list = NULL;
9217c478bd9Sstevel@tonic-gate 	fip->fi_nfiles = 0;
9227c478bd9Sstevel@tonic-gate 	while (fip->fi_rlist != NULL) {
9237c478bd9Sstevel@tonic-gate 		uf_rlist_t *urp = fip->fi_rlist;
9247c478bd9Sstevel@tonic-gate 		fip->fi_rlist = urp->ur_next;
9257c478bd9Sstevel@tonic-gate 		kmem_free(urp->ur_list, urp->ur_nfiles * sizeof (uf_entry_t));
9267c478bd9Sstevel@tonic-gate 		kmem_free(urp, sizeof (uf_rlist_t));
9277c478bd9Sstevel@tonic-gate 	}
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate /*
9317c478bd9Sstevel@tonic-gate  * Internal form of close.  Decrement reference count on file
9327c478bd9Sstevel@tonic-gate  * structure.  Decrement reference count on the vnode following
9337c478bd9Sstevel@tonic-gate  * removal of the referencing file structure.
9347c478bd9Sstevel@tonic-gate  */
9357c478bd9Sstevel@tonic-gate int
closef(file_t * fp)9367c478bd9Sstevel@tonic-gate closef(file_t *fp)
9377c478bd9Sstevel@tonic-gate {
9387c478bd9Sstevel@tonic-gate 	vnode_t *vp;
9397c478bd9Sstevel@tonic-gate 	int error;
9407c478bd9Sstevel@tonic-gate 	int count;
9417c478bd9Sstevel@tonic-gate 	int flag;
9427c478bd9Sstevel@tonic-gate 	offset_t offset;
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	/*
9457c478bd9Sstevel@tonic-gate 	 * audit close of file (may be exit)
9467c478bd9Sstevel@tonic-gate 	 */
947005d3febSMarek Pospisil 	if (AU_AUDITING())
9487c478bd9Sstevel@tonic-gate 		audit_closef(fp);
9497c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&P_FINFO(curproc)->fi_lock));
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	mutex_enter(&fp->f_tlock);
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	ASSERT(fp->f_count > 0);
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	count = fp->f_count--;
9567c478bd9Sstevel@tonic-gate 	flag = fp->f_flag;
9577c478bd9Sstevel@tonic-gate 	offset = fp->f_offset;
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
9607c478bd9Sstevel@tonic-gate 
961da6c28aaSamw 	error = VOP_CLOSE(vp, flag, count, offset, fp->f_cred, NULL);
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	if (count > 1) {
9647c478bd9Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
9657c478bd9Sstevel@tonic-gate 		return (error);
9667c478bd9Sstevel@tonic-gate 	}
9677c478bd9Sstevel@tonic-gate 	ASSERT(fp->f_count == 0);
9687a5aac98SJerry Jelinek 	/* Last reference, remove any OFD style lock for the file_t */
9697a5aac98SJerry Jelinek 	ofdcleanlock(fp);
9707c478bd9Sstevel@tonic-gate 	mutex_exit(&fp->f_tlock);
9717c478bd9Sstevel@tonic-gate 
972b0f673c4SBryan Cantrill 	/*
973b0f673c4SBryan Cantrill 	 * If DTrace has getf() subroutines active, it will set dtrace_closef
974b0f673c4SBryan Cantrill 	 * to point to code that implements a barrier with respect to probe
975b0f673c4SBryan Cantrill 	 * context.  This must be called before the file_t is freed (and the
976b0f673c4SBryan Cantrill 	 * vnode that it refers to is released) -- but it must be after the
977b0f673c4SBryan Cantrill 	 * file_t has been removed from the uf_entry_t.  That is, there must
978b0f673c4SBryan Cantrill 	 * be no way for a racing getf() in probe context to yield the fp that
979b0f673c4SBryan Cantrill 	 * we're operating upon.
980b0f673c4SBryan Cantrill 	 */
981b0f673c4SBryan Cantrill 	if (dtrace_closef != NULL)
982b0f673c4SBryan Cantrill 		(*dtrace_closef)();
983b0f673c4SBryan Cantrill 
9847c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
9857c478bd9Sstevel@tonic-gate 	/*
9867c478bd9Sstevel@tonic-gate 	 * deallocate resources to audit_data
9877c478bd9Sstevel@tonic-gate 	 */
9887c478bd9Sstevel@tonic-gate 	if (audit_active)
9897c478bd9Sstevel@tonic-gate 		audit_unfalloc(fp);
9907c478bd9Sstevel@tonic-gate 	crfree(fp->f_cred);
9917c478bd9Sstevel@tonic-gate 	kmem_cache_free(file_cache, fp);
9927c478bd9Sstevel@tonic-gate 	return (error);
9937c478bd9Sstevel@tonic-gate }
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate /*
9967c478bd9Sstevel@tonic-gate  * This is a combination of ufalloc() and setf().
9977c478bd9Sstevel@tonic-gate  */
9987c478bd9Sstevel@tonic-gate int
ufalloc_file(int start,file_t * fp)9997c478bd9Sstevel@tonic-gate ufalloc_file(int start, file_t *fp)
10007c478bd9Sstevel@tonic-gate {
10017c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
10027c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(p);
10037c478bd9Sstevel@tonic-gate 	int filelimit;
10047c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
10057c478bd9Sstevel@tonic-gate 	int nfiles;
10067c478bd9Sstevel@tonic-gate 	int fd;
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	/*
10097c478bd9Sstevel@tonic-gate 	 * Assertion is to convince the correctness of the following
10107c478bd9Sstevel@tonic-gate 	 * assignment for filelimit after casting to int.
10117c478bd9Sstevel@tonic-gate 	 */
10127c478bd9Sstevel@tonic-gate 	ASSERT(p->p_fno_ctl <= INT_MAX);
10137c478bd9Sstevel@tonic-gate 	filelimit = (int)p->p_fno_ctl;
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	for (;;) {
10167c478bd9Sstevel@tonic-gate 		mutex_enter(&fip->fi_lock);
10177c478bd9Sstevel@tonic-gate 		fd = fd_find(fip, start);
1018a5f69788Scraigm 		if (fd >= 0 && fd == fip->fi_badfd) {
1019a5f69788Scraigm 			start = fd + 1;
1020a5f69788Scraigm 			mutex_exit(&fip->fi_lock);
1021a5f69788Scraigm 			continue;
1022a5f69788Scraigm 		}
10237c478bd9Sstevel@tonic-gate 		if ((uint_t)fd < filelimit)
10247c478bd9Sstevel@tonic-gate 			break;
10257c478bd9Sstevel@tonic-gate 		if (fd >= filelimit) {
10267c478bd9Sstevel@tonic-gate 			mutex_exit(&fip->fi_lock);
10277c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
10287c478bd9Sstevel@tonic-gate 			(void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
10297c478bd9Sstevel@tonic-gate 			    p->p_rctls, p, RCA_SAFE);
10307c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
10317c478bd9Sstevel@tonic-gate 			return (-1);
10327c478bd9Sstevel@tonic-gate 		}
10337c478bd9Sstevel@tonic-gate 		/* fd_find() returned -1 */
10347c478bd9Sstevel@tonic-gate 		nfiles = fip->fi_nfiles;
10357c478bd9Sstevel@tonic-gate 		mutex_exit(&fip->fi_lock);
10367c478bd9Sstevel@tonic-gate 		flist_grow(MAX(start, nfiles));
10377c478bd9Sstevel@tonic-gate 	}
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
10407c478bd9Sstevel@tonic-gate 	fd_reserve(fip, fd, 1);
10417c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_file == NULL);
10427c478bd9Sstevel@tonic-gate 	ufp->uf_file = fp;
1043086d9687SPatrick Mooney 	if (fp != NULL) {
1044086d9687SPatrick Mooney 		ufp->uf_gen++;
1045086d9687SPatrick Mooney 	}
10467c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
10477c478bd9Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
10487c478bd9Sstevel@tonic-gate 	return (fd);
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate /*
10527c478bd9Sstevel@tonic-gate  * Allocate a user file descriptor greater than or equal to "start".
10537c478bd9Sstevel@tonic-gate  */
10547c478bd9Sstevel@tonic-gate int
ufalloc(int start)10557c478bd9Sstevel@tonic-gate ufalloc(int start)
10567c478bd9Sstevel@tonic-gate {
10577c478bd9Sstevel@tonic-gate 	return (ufalloc_file(start, NULL));
10587c478bd9Sstevel@tonic-gate }
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate /*
10617c478bd9Sstevel@tonic-gate  * Check that a future allocation of count fds on proc p has a good
10627c478bd9Sstevel@tonic-gate  * chance of succeeding.  If not, do rctl processing as if we'd failed
10637c478bd9Sstevel@tonic-gate  * the allocation.
10647c478bd9Sstevel@tonic-gate  *
10657c478bd9Sstevel@tonic-gate  * Our caller must guarantee that p cannot disappear underneath us.
10667c478bd9Sstevel@tonic-gate  */
10677c478bd9Sstevel@tonic-gate int
ufcanalloc(proc_t * p,uint_t count)10687c478bd9Sstevel@tonic-gate ufcanalloc(proc_t *p, uint_t count)
10697c478bd9Sstevel@tonic-gate {
10707c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(p);
10717c478bd9Sstevel@tonic-gate 	int filelimit;
10727c478bd9Sstevel@tonic-gate 	int current;
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	if (count == 0)
10757c478bd9Sstevel@tonic-gate 		return (1);
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	ASSERT(p->p_fno_ctl <= INT_MAX);
10787c478bd9Sstevel@tonic-gate 	filelimit = (int)p->p_fno_ctl;
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	mutex_enter(&fip->fi_lock);
10817c478bd9Sstevel@tonic-gate 	current = flist_nalloc(fip);		/* # of in-use descriptors */
10827c478bd9Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	/*
10857c478bd9Sstevel@tonic-gate 	 * If count is a positive integer, the worst that can happen is
10867c478bd9Sstevel@tonic-gate 	 * an overflow to a negative value, which is caught by the >= 0 check.
10877c478bd9Sstevel@tonic-gate 	 */
10887c478bd9Sstevel@tonic-gate 	current += count;
10897c478bd9Sstevel@tonic-gate 	if (count <= INT_MAX && current >= 0 && current <= filelimit)
10907c478bd9Sstevel@tonic-gate 		return (1);
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
10937c478bd9Sstevel@tonic-gate 	(void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
10947c478bd9Sstevel@tonic-gate 	    p->p_rctls, p, RCA_SAFE);
10957c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
10967c478bd9Sstevel@tonic-gate 	return (0);
10977c478bd9Sstevel@tonic-gate }
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate /*
11007c478bd9Sstevel@tonic-gate  * Allocate a user file descriptor and a file structure.
11017c478bd9Sstevel@tonic-gate  * Initialize the descriptor to point at the file structure.
11027c478bd9Sstevel@tonic-gate  * If fdp is NULL, the user file descriptor will not be allocated.
11037c478bd9Sstevel@tonic-gate  */
11047c478bd9Sstevel@tonic-gate int
falloc(vnode_t * vp,int flag,file_t ** fpp,int * fdp)11057c478bd9Sstevel@tonic-gate falloc(vnode_t *vp, int flag, file_t **fpp, int *fdp)
11067c478bd9Sstevel@tonic-gate {
11077c478bd9Sstevel@tonic-gate 	file_t *fp;
11087c478bd9Sstevel@tonic-gate 	int fd;
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	if (fdp) {
11117c478bd9Sstevel@tonic-gate 		if ((fd = ufalloc(0)) == -1)
11127c478bd9Sstevel@tonic-gate 			return (EMFILE);
11137c478bd9Sstevel@tonic-gate 	}
11147c478bd9Sstevel@tonic-gate 	fp = kmem_cache_alloc(file_cache, KM_SLEEP);
11157c478bd9Sstevel@tonic-gate 	/*
11167c478bd9Sstevel@tonic-gate 	 * Note: falloc returns the fp locked
11177c478bd9Sstevel@tonic-gate 	 */
11187c478bd9Sstevel@tonic-gate 	mutex_enter(&fp->f_tlock);
11197c478bd9Sstevel@tonic-gate 	fp->f_count = 1;
11207c478bd9Sstevel@tonic-gate 	fp->f_flag = (ushort_t)flag;
1121794f0adbSRoger A. Faulkner 	fp->f_flag2 = (flag & (FSEARCH|FEXEC)) >> 16;
11227c478bd9Sstevel@tonic-gate 	fp->f_vnode = vp;
11237c478bd9Sstevel@tonic-gate 	fp->f_offset = 0;
11247c478bd9Sstevel@tonic-gate 	fp->f_audit_data = 0;
11257c478bd9Sstevel@tonic-gate 	crhold(fp->f_cred = CRED());
11267c478bd9Sstevel@tonic-gate 	/*
11277c478bd9Sstevel@tonic-gate 	 * allocate resources to audit_data
11287c478bd9Sstevel@tonic-gate 	 */
11297c478bd9Sstevel@tonic-gate 	if (audit_active)
11307c478bd9Sstevel@tonic-gate 		audit_falloc(fp);
11317c478bd9Sstevel@tonic-gate 	*fpp = fp;
11327c478bd9Sstevel@tonic-gate 	if (fdp)
11337c478bd9Sstevel@tonic-gate 		*fdp = fd;
11347c478bd9Sstevel@tonic-gate 	return (0);
11357c478bd9Sstevel@tonic-gate }
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11387c478bd9Sstevel@tonic-gate static int
file_cache_constructor(void * buf,void * cdrarg,int kmflags)11397c478bd9Sstevel@tonic-gate file_cache_constructor(void *buf, void *cdrarg, int kmflags)
11407c478bd9Sstevel@tonic-gate {
11417c478bd9Sstevel@tonic-gate 	file_t *fp = buf;
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	mutex_init(&fp->f_tlock, NULL, MUTEX_DEFAULT, NULL);
11447c478bd9Sstevel@tonic-gate 	return (0);
11457c478bd9Sstevel@tonic-gate }
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11487c478bd9Sstevel@tonic-gate static void
file_cache_destructor(void * buf,void * cdrarg)11497c478bd9Sstevel@tonic-gate file_cache_destructor(void *buf, void *cdrarg)
11507c478bd9Sstevel@tonic-gate {
11517c478bd9Sstevel@tonic-gate 	file_t *fp = buf;
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	mutex_destroy(&fp->f_tlock);
11547c478bd9Sstevel@tonic-gate }
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate void
finit()11577c478bd9Sstevel@tonic-gate finit()
11587c478bd9Sstevel@tonic-gate {
11597c478bd9Sstevel@tonic-gate 	file_cache = kmem_cache_create("file_cache", sizeof (file_t), 0,
11607c478bd9Sstevel@tonic-gate 	    file_cache_constructor, file_cache_destructor, NULL, NULL, NULL, 0);
11617c478bd9Sstevel@tonic-gate }
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate void
unfalloc(file_t * fp)11647c478bd9Sstevel@tonic-gate unfalloc(file_t *fp)
11657c478bd9Sstevel@tonic-gate {
11667c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fp->f_tlock));
11677c478bd9Sstevel@tonic-gate 	if (--fp->f_count <= 0) {
11687c478bd9Sstevel@tonic-gate 		/*
11697c478bd9Sstevel@tonic-gate 		 * deallocate resources to audit_data
11707c478bd9Sstevel@tonic-gate 		 */
11717c478bd9Sstevel@tonic-gate 		if (audit_active)
11727c478bd9Sstevel@tonic-gate 			audit_unfalloc(fp);
11737c478bd9Sstevel@tonic-gate 		crfree(fp->f_cred);
11747c478bd9Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
11757c478bd9Sstevel@tonic-gate 		kmem_cache_free(file_cache, fp);
11767c478bd9Sstevel@tonic-gate 	} else
11777c478bd9Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
11787c478bd9Sstevel@tonic-gate }
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate /*
11817c478bd9Sstevel@tonic-gate  * Given a file descriptor, set the user's
11827c478bd9Sstevel@tonic-gate  * file pointer to the given parameter.
11837c478bd9Sstevel@tonic-gate  */
11847c478bd9Sstevel@tonic-gate void
setf(int fd,file_t * fp)11857c478bd9Sstevel@tonic-gate setf(int fd, file_t *fp)
11867c478bd9Sstevel@tonic-gate {
11877c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
11887c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
11897c478bd9Sstevel@tonic-gate 
1190005d3febSMarek Pospisil 	if (AU_AUDITING())
11917c478bd9Sstevel@tonic-gate 		audit_setf(fp, fd);
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
11947c478bd9Sstevel@tonic-gate 		mutex_enter(&fip->fi_lock);
11957c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
11967c478bd9Sstevel@tonic-gate 		fd_reserve(fip, fd, -1);
11977c478bd9Sstevel@tonic-gate 		mutex_exit(&fip->fi_lock);
11987c478bd9Sstevel@tonic-gate 	} else {
11997c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
12007c478bd9Sstevel@tonic-gate 		ASSERT(ufp->uf_busy);
1201086d9687SPatrick Mooney 		ufp->uf_gen++;
12027c478bd9Sstevel@tonic-gate 	}
12037c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_fpollinfo == NULL);
12047c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_flag == 0);
12057c478bd9Sstevel@tonic-gate 	ufp->uf_file = fp;
12067c478bd9Sstevel@tonic-gate 	cv_broadcast(&ufp->uf_wanted_cv);
12077c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
12087c478bd9Sstevel@tonic-gate }
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate /*
12117c478bd9Sstevel@tonic-gate  * Given a file descriptor, return the file table flags, plus,
12127c478bd9Sstevel@tonic-gate  * if this is a socket in asynchronous mode, the FASYNC flag.
12137c478bd9Sstevel@tonic-gate  * getf() may or may not have been called before calling f_getfl().
12147c478bd9Sstevel@tonic-gate  */
12157c478bd9Sstevel@tonic-gate int
f_getfl(int fd,int * flagp)12167c478bd9Sstevel@tonic-gate f_getfl(int fd, int *flagp)
12177c478bd9Sstevel@tonic-gate {
12187c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
12197c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
12207c478bd9Sstevel@tonic-gate 	file_t *fp;
12217c478bd9Sstevel@tonic-gate 	int error;
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	if ((uint_t)fd >= fip->fi_nfiles)
12247c478bd9Sstevel@tonic-gate 		error = EBADF;
12257c478bd9Sstevel@tonic-gate 	else {
12267c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
12277c478bd9Sstevel@tonic-gate 		if ((fp = ufp->uf_file) == NULL)
12287c478bd9Sstevel@tonic-gate 			error = EBADF;
12297c478bd9Sstevel@tonic-gate 		else {
12307c478bd9Sstevel@tonic-gate 			vnode_t *vp = fp->f_vnode;
1231086d9687SPatrick Mooney 			int flag = fp->f_flag | (fp->f_flag2 << 16);
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 			/*
12347c478bd9Sstevel@tonic-gate 			 * BSD fcntl() FASYNC compatibility.
12357c478bd9Sstevel@tonic-gate 			 */
12360f1702c5SYu Xiangning 			if (vp->v_type == VSOCK)
12377c478bd9Sstevel@tonic-gate 				flag |= sock_getfasync(vp);
12387c478bd9Sstevel@tonic-gate 			*flagp = flag;
12397c478bd9Sstevel@tonic-gate 			error = 0;
12407c478bd9Sstevel@tonic-gate 		}
12417c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
12427c478bd9Sstevel@tonic-gate 	}
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	return (error);
12457c478bd9Sstevel@tonic-gate }
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate /*
12487c478bd9Sstevel@tonic-gate  * Given a file descriptor, return the user's file flags.
12497c478bd9Sstevel@tonic-gate  * Force the FD_CLOEXEC flag for writable self-open /proc files.
12507c478bd9Sstevel@tonic-gate  * getf() may or may not have been called before calling f_getfd_error().
12517c478bd9Sstevel@tonic-gate  */
12527c478bd9Sstevel@tonic-gate int
f_getfd_error(int fd,int * flagp)12537c478bd9Sstevel@tonic-gate f_getfd_error(int fd, int *flagp)
12547c478bd9Sstevel@tonic-gate {
12557c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
12567c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
12577c478bd9Sstevel@tonic-gate 	file_t *fp;
12587c478bd9Sstevel@tonic-gate 	int flag;
12597c478bd9Sstevel@tonic-gate 	int error;
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	if ((uint_t)fd >= fip->fi_nfiles)
12627c478bd9Sstevel@tonic-gate 		error = EBADF;
12637c478bd9Sstevel@tonic-gate 	else {
12647c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
12657c478bd9Sstevel@tonic-gate 		if ((fp = ufp->uf_file) == NULL)
12667c478bd9Sstevel@tonic-gate 			error = EBADF;
12677c478bd9Sstevel@tonic-gate 		else {
12687c478bd9Sstevel@tonic-gate 			flag = ufp->uf_flag;
12697c478bd9Sstevel@tonic-gate 			if ((fp->f_flag & FWRITE) && pr_isself(fp->f_vnode))
12707c478bd9Sstevel@tonic-gate 				flag |= FD_CLOEXEC;
12717c478bd9Sstevel@tonic-gate 			*flagp = flag;
12727c478bd9Sstevel@tonic-gate 			error = 0;
12737c478bd9Sstevel@tonic-gate 		}
12747c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
12757c478bd9Sstevel@tonic-gate 	}
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 	return (error);
12787c478bd9Sstevel@tonic-gate }
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate /*
12817c478bd9Sstevel@tonic-gate  * getf() must have been called before calling f_getfd().
12827c478bd9Sstevel@tonic-gate  */
12837c478bd9Sstevel@tonic-gate char
f_getfd(int fd)12847c478bd9Sstevel@tonic-gate f_getfd(int fd)
12857c478bd9Sstevel@tonic-gate {
12867c478bd9Sstevel@tonic-gate 	int flag = 0;
12877c478bd9Sstevel@tonic-gate 	(void) f_getfd_error(fd, &flag);
12887c478bd9Sstevel@tonic-gate 	return ((char)flag);
12897c478bd9Sstevel@tonic-gate }
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate /*
12927c478bd9Sstevel@tonic-gate  * Given a file descriptor and file flags, set the user's file flags.
12937c478bd9Sstevel@tonic-gate  * At present, the only valid flag is FD_CLOEXEC.
12947c478bd9Sstevel@tonic-gate  * getf() may or may not have been called before calling f_setfd_error().
12957c478bd9Sstevel@tonic-gate  */
12967c478bd9Sstevel@tonic-gate int
f_setfd_error(int fd,int flags)12977c478bd9Sstevel@tonic-gate f_setfd_error(int fd, int flags)
12987c478bd9Sstevel@tonic-gate {
12997c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
13007c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
13017c478bd9Sstevel@tonic-gate 	int error;
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	if ((uint_t)fd >= fip->fi_nfiles)
13047c478bd9Sstevel@tonic-gate 		error = EBADF;
13057c478bd9Sstevel@tonic-gate 	else {
13067c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
13077c478bd9Sstevel@tonic-gate 		if (ufp->uf_file == NULL)
13087c478bd9Sstevel@tonic-gate 			error = EBADF;
13097c478bd9Sstevel@tonic-gate 		else {
13107c478bd9Sstevel@tonic-gate 			ufp->uf_flag = flags & FD_CLOEXEC;
13117c478bd9Sstevel@tonic-gate 			error = 0;
13127c478bd9Sstevel@tonic-gate 		}
13137c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
13147c478bd9Sstevel@tonic-gate 	}
13157c478bd9Sstevel@tonic-gate 	return (error);
13167c478bd9Sstevel@tonic-gate }
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate void
f_setfd(int fd,char flags)13197c478bd9Sstevel@tonic-gate f_setfd(int fd, char flags)
13207c478bd9Sstevel@tonic-gate {
13217c478bd9Sstevel@tonic-gate 	(void) f_setfd_error(fd, flags);
13227c478bd9Sstevel@tonic-gate }
13237c478bd9Sstevel@tonic-gate 
1324a5f69788Scraigm #define	BADFD_MIN	3
1325a5f69788Scraigm #define	BADFD_MAX	255
1326a5f69788Scraigm 
1327a5f69788Scraigm /*
1328a5f69788Scraigm  * Attempt to allocate a file descriptor which is bad and which
1329a5f69788Scraigm  * is "poison" to the application.  It cannot be closed (except
1330a5f69788Scraigm  * on exec), allocated for a different use, etc.
1331a5f69788Scraigm  */
1332a5f69788Scraigm int
f_badfd(int start,int * fdp,int action)1333a5f69788Scraigm f_badfd(int start, int *fdp, int action)
1334a5f69788Scraigm {
1335a5f69788Scraigm 	int fdr;
1336a5f69788Scraigm 	int badfd;
1337a5f69788Scraigm 	uf_info_t *fip = P_FINFO(curproc);
1338a5f69788Scraigm 
1339a5f69788Scraigm #ifdef _LP64
1340a5f69788Scraigm 	/* No restrictions on 64 bit _file */
1341a5f69788Scraigm 	if (get_udatamodel() != DATAMODEL_ILP32)
1342a5f69788Scraigm 		return (EINVAL);
1343a5f69788Scraigm #endif
1344a5f69788Scraigm 
1345a5f69788Scraigm 	if (start > BADFD_MAX || start < BADFD_MIN)
1346a5f69788Scraigm 		return (EINVAL);
1347a5f69788Scraigm 
1348a5f69788Scraigm 	if (action >= NSIG || action < 0)
1349a5f69788Scraigm 		return (EINVAL);
1350a5f69788Scraigm 
1351a5f69788Scraigm 	mutex_enter(&fip->fi_lock);
1352a5f69788Scraigm 	badfd = fip->fi_badfd;
1353a5f69788Scraigm 	mutex_exit(&fip->fi_lock);
1354a5f69788Scraigm 
1355a5f69788Scraigm 	if (badfd != -1)
1356a5f69788Scraigm 		return (EAGAIN);
1357a5f69788Scraigm 
1358a5f69788Scraigm 	fdr = ufalloc(start);
1359a5f69788Scraigm 
1360a5f69788Scraigm 	if (fdr > BADFD_MAX) {
1361a5f69788Scraigm 		setf(fdr, NULL);
1362a5f69788Scraigm 		return (EMFILE);
1363a5f69788Scraigm 	}
1364a5f69788Scraigm 	if (fdr < 0)
1365a5f69788Scraigm 		return (EMFILE);
1366a5f69788Scraigm 
1367a5f69788Scraigm 	mutex_enter(&fip->fi_lock);
1368a5f69788Scraigm 	if (fip->fi_badfd != -1) {
1369a5f69788Scraigm 		/* Lost race */
1370a5f69788Scraigm 		mutex_exit(&fip->fi_lock);
1371a5f69788Scraigm 		setf(fdr, NULL);
1372a5f69788Scraigm 		return (EAGAIN);
1373a5f69788Scraigm 	}
1374a5f69788Scraigm 	fip->fi_action = action;
1375a5f69788Scraigm 	fip->fi_badfd = fdr;
1376a5f69788Scraigm 	mutex_exit(&fip->fi_lock);
1377a5f69788Scraigm 	setf(fdr, NULL);
1378a5f69788Scraigm 
1379a5f69788Scraigm 	*fdp = fdr;
1380a5f69788Scraigm 
1381a5f69788Scraigm 	return (0);
1382a5f69788Scraigm }
1383a5f69788Scraigm 
13847c478bd9Sstevel@tonic-gate /*
13857c478bd9Sstevel@tonic-gate  * Allocate a file descriptor and assign it to the vnode "*vpp",
13867c478bd9Sstevel@tonic-gate  * performing the usual open protocol upon it and returning the
13877c478bd9Sstevel@tonic-gate  * file descriptor allocated.  It is the responsibility of the
13887c478bd9Sstevel@tonic-gate  * caller to dispose of "*vpp" if any error occurs.
13897c478bd9Sstevel@tonic-gate  */
13907c478bd9Sstevel@tonic-gate int
fassign(vnode_t ** vpp,int mode,int * fdp)13917c478bd9Sstevel@tonic-gate fassign(vnode_t **vpp, int mode, int *fdp)
13927c478bd9Sstevel@tonic-gate {
13937c478bd9Sstevel@tonic-gate 	file_t *fp;
13947c478bd9Sstevel@tonic-gate 	int error;
13957c478bd9Sstevel@tonic-gate 	int fd;
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	if (error = falloc((vnode_t *)NULL, mode, &fp, &fd))
13987c478bd9Sstevel@tonic-gate 		return (error);
1399da6c28aaSamw 	if (error = VOP_OPEN(vpp, mode, fp->f_cred, NULL)) {
14007c478bd9Sstevel@tonic-gate 		setf(fd, NULL);
14017c478bd9Sstevel@tonic-gate 		unfalloc(fp);
14027c478bd9Sstevel@tonic-gate 		return (error);
14037c478bd9Sstevel@tonic-gate 	}
14047c478bd9Sstevel@tonic-gate 	fp->f_vnode = *vpp;
14057c478bd9Sstevel@tonic-gate 	mutex_exit(&fp->f_tlock);
14067c478bd9Sstevel@tonic-gate 	/*
14077c478bd9Sstevel@tonic-gate 	 * Fill in the slot falloc reserved.
14087c478bd9Sstevel@tonic-gate 	 */
14097c478bd9Sstevel@tonic-gate 	setf(fd, fp);
14107c478bd9Sstevel@tonic-gate 	*fdp = fd;
14117c478bd9Sstevel@tonic-gate 	return (0);
14127c478bd9Sstevel@tonic-gate }
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate /*
14157c478bd9Sstevel@tonic-gate  * When a process forks it must increment the f_count of all file pointers
14167c478bd9Sstevel@tonic-gate  * since there is a new process pointing at them.  fcnt_add(fip, 1) does this.
14177c478bd9Sstevel@tonic-gate  * Since we are called when there is only 1 active lwp we don't need to
14187c478bd9Sstevel@tonic-gate  * hold fi_lock or any uf_lock.  If the fork fails, fork_fail() calls
14197c478bd9Sstevel@tonic-gate  * fcnt_add(fip, -1) to restore the counts.
14207c478bd9Sstevel@tonic-gate  */
14217c478bd9Sstevel@tonic-gate void
fcnt_add(uf_info_t * fip,int incr)14227c478bd9Sstevel@tonic-gate fcnt_add(uf_info_t *fip, int incr)
14237c478bd9Sstevel@tonic-gate {
14247c478bd9Sstevel@tonic-gate 	int i;
14257c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
14267c478bd9Sstevel@tonic-gate 	file_t *fp;
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	ufp = fip->fi_list;
14297c478bd9Sstevel@tonic-gate 	for (i = 0; i < fip->fi_nfiles; i++, ufp++) {
14307c478bd9Sstevel@tonic-gate 		if ((fp = ufp->uf_file) != NULL) {
14317c478bd9Sstevel@tonic-gate 			mutex_enter(&fp->f_tlock);
14327c478bd9Sstevel@tonic-gate 			ASSERT((incr == 1 && fp->f_count >= 1) ||
14337c478bd9Sstevel@tonic-gate 			    (incr == -1 && fp->f_count >= 2));
14347c478bd9Sstevel@tonic-gate 			fp->f_count += incr;
14357c478bd9Sstevel@tonic-gate 			mutex_exit(&fp->f_tlock);
14367c478bd9Sstevel@tonic-gate 		}
14377c478bd9Sstevel@tonic-gate 	}
14387c478bd9Sstevel@tonic-gate }
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate /*
14417c478bd9Sstevel@tonic-gate  * This is called from exec to close all fd's that have the FD_CLOEXEC flag
14427c478bd9Sstevel@tonic-gate  * set and also to close all self-open for write /proc file descriptors.
14437c478bd9Sstevel@tonic-gate  */
14447c478bd9Sstevel@tonic-gate void
close_exec(uf_info_t * fip)14457c478bd9Sstevel@tonic-gate close_exec(uf_info_t *fip)
14467c478bd9Sstevel@tonic-gate {
14477c478bd9Sstevel@tonic-gate 	int fd;
14487c478bd9Sstevel@tonic-gate 	file_t *fp;
14497c478bd9Sstevel@tonic-gate 	fpollinfo_t *fpip;
14507c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
14517c478bd9Sstevel@tonic-gate 	portfd_t *pfd;
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	ufp = fip->fi_list;
14547c478bd9Sstevel@tonic-gate 	for (fd = 0; fd < fip->fi_nfiles; fd++, ufp++) {
14557c478bd9Sstevel@tonic-gate 		if ((fp = ufp->uf_file) != NULL &&
14567c478bd9Sstevel@tonic-gate 		    ((ufp->uf_flag & FD_CLOEXEC) ||
14577c478bd9Sstevel@tonic-gate 		    ((fp->f_flag & FWRITE) && pr_isself(fp->f_vnode)))) {
14587c478bd9Sstevel@tonic-gate 			fpip = ufp->uf_fpollinfo;
14597c478bd9Sstevel@tonic-gate 			mutex_enter(&fip->fi_lock);
14607c478bd9Sstevel@tonic-gate 			mutex_enter(&ufp->uf_lock);
14617c478bd9Sstevel@tonic-gate 			fd_reserve(fip, fd, -1);
14627c478bd9Sstevel@tonic-gate 			mutex_exit(&fip->fi_lock);
14637c478bd9Sstevel@tonic-gate 			ufp->uf_file = NULL;
14647c478bd9Sstevel@tonic-gate 			ufp->uf_fpollinfo = NULL;
14657c478bd9Sstevel@tonic-gate 			ufp->uf_flag = 0;
14667c478bd9Sstevel@tonic-gate 			/*
14677c478bd9Sstevel@tonic-gate 			 * We may need to cleanup some cached poll states
14687c478bd9Sstevel@tonic-gate 			 * in t_pollstate before the fd can be reused. It
14697c478bd9Sstevel@tonic-gate 			 * is important that we don't access a stale thread
14707c478bd9Sstevel@tonic-gate 			 * structure. We will do the cleanup in two
14717c478bd9Sstevel@tonic-gate 			 * phases to avoid deadlock and holding uf_lock for
14727c478bd9Sstevel@tonic-gate 			 * too long. In phase 1, hold the uf_lock and call
14737c478bd9Sstevel@tonic-gate 			 * pollblockexit() to set state in t_pollstate struct
14747c478bd9Sstevel@tonic-gate 			 * so that a thread does not exit on us. In phase 2,
14757c478bd9Sstevel@tonic-gate 			 * we drop the uf_lock and call pollcacheclean().
14767c478bd9Sstevel@tonic-gate 			 */
14777c478bd9Sstevel@tonic-gate 			pfd = ufp->uf_portfd;
14787c478bd9Sstevel@tonic-gate 			ufp->uf_portfd = NULL;
14797c478bd9Sstevel@tonic-gate 			if (fpip != NULL)
14807c478bd9Sstevel@tonic-gate 				pollblockexit(fpip);
14817c478bd9Sstevel@tonic-gate 			mutex_exit(&ufp->uf_lock);
14827c478bd9Sstevel@tonic-gate 			if (fpip != NULL)
14837c478bd9Sstevel@tonic-gate 				pollcacheclean(fpip, fd);
14847c478bd9Sstevel@tonic-gate 			if (pfd)
148561b4b1efSpraks 				port_close_fd(pfd);
14867c478bd9Sstevel@tonic-gate 			(void) closef(fp);
14877c478bd9Sstevel@tonic-gate 		}
14887c478bd9Sstevel@tonic-gate 	}
1489a5f69788Scraigm 
1490a5f69788Scraigm 	/* Reset bad fd */
1491a5f69788Scraigm 	fip->fi_badfd = -1;
1492a5f69788Scraigm 	fip->fi_action = -1;
14937c478bd9Sstevel@tonic-gate }
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate /*
1496794f0adbSRoger A. Faulkner  * Utility function called by most of the *at() system call interfaces.
1497794f0adbSRoger A. Faulkner  *
1498794f0adbSRoger A. Faulkner  * Generate a starting vnode pointer for an (fd, path) pair where 'fd'
1499794f0adbSRoger A. Faulkner  * is an open file descriptor for a directory to be used as the starting
1500794f0adbSRoger A. Faulkner  * point for the lookup of the relative pathname 'path' (or, if path is
1501794f0adbSRoger A. Faulkner  * NULL, generate a vnode pointer for the direct target of the operation).
1502794f0adbSRoger A. Faulkner  *
1503794f0adbSRoger A. Faulkner  * If we successfully return a non-NULL startvp, it has been the target
1504794f0adbSRoger A. Faulkner  * of VN_HOLD() and the caller must call VN_RELE() on it.
15057c478bd9Sstevel@tonic-gate  */
15067c478bd9Sstevel@tonic-gate int
fgetstartvp(int fd,char * path,vnode_t ** startvpp)1507794f0adbSRoger A. Faulkner fgetstartvp(int fd, char *path, vnode_t **startvpp)
15087c478bd9Sstevel@tonic-gate {
1509794f0adbSRoger A. Faulkner 	vnode_t		*startvp;
1510794f0adbSRoger A. Faulkner 	file_t		*startfp;
1511794f0adbSRoger A. Faulkner 	char		startchar;
15127c478bd9Sstevel@tonic-gate 
1513794f0adbSRoger A. Faulkner 	if (fd == AT_FDCWD && path == NULL)
1514794f0adbSRoger A. Faulkner 		return (EFAULT);
15157c478bd9Sstevel@tonic-gate 
1516794f0adbSRoger A. Faulkner 	if (fd == AT_FDCWD) {
15177c478bd9Sstevel@tonic-gate 		/*
1518794f0adbSRoger A. Faulkner 		 * Start from the current working directory.
15197c478bd9Sstevel@tonic-gate 		 */
1520794f0adbSRoger A. Faulkner 		startvp = NULL;
1521794f0adbSRoger A. Faulkner 	} else {
1522794f0adbSRoger A. Faulkner 		if (path == NULL)
1523794f0adbSRoger A. Faulkner 			startchar = '\0';
1524794f0adbSRoger A. Faulkner 		else if (copyin(path, &startchar, sizeof (char)))
1525794f0adbSRoger A. Faulkner 			return (EFAULT);
15267c478bd9Sstevel@tonic-gate 
1527794f0adbSRoger A. Faulkner 		if (startchar == '/') {
1528794f0adbSRoger A. Faulkner 			/*
1529794f0adbSRoger A. Faulkner 			 * 'path' is an absolute pathname.
1530794f0adbSRoger A. Faulkner 			 */
1531794f0adbSRoger A. Faulkner 			startvp = NULL;
1532794f0adbSRoger A. Faulkner 		} else {
1533794f0adbSRoger A. Faulkner 			/*
1534794f0adbSRoger A. Faulkner 			 * 'path' is a relative pathname or we will
1535794f0adbSRoger A. Faulkner 			 * be applying the operation to 'fd' itself.
1536794f0adbSRoger A. Faulkner 			 */
1537794f0adbSRoger A. Faulkner 			if ((startfp = getf(fd)) == NULL)
1538794f0adbSRoger A. Faulkner 				return (EBADF);
1539794f0adbSRoger A. Faulkner 			startvp = startfp->f_vnode;
1540794f0adbSRoger A. Faulkner 			VN_HOLD(startvp);
15417c478bd9Sstevel@tonic-gate 			releasef(fd);
1542794f0adbSRoger A. Faulkner 		}
1543794f0adbSRoger A. Faulkner 	}
1544794f0adbSRoger A. Faulkner 	*startvpp = startvp;
1545794f0adbSRoger A. Faulkner 	return (0);
15467c478bd9Sstevel@tonic-gate }
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate /*
1549794f0adbSRoger A. Faulkner  * Called from fchownat() and fchmodat() to set ownership and mode.
1550794f0adbSRoger A. Faulkner  * The contents of *vap must be set before calling here.
15517c478bd9Sstevel@tonic-gate  */
1552794f0adbSRoger A. Faulkner int
fsetattrat(int fd,char * path,int flags,struct vattr * vap)1553794f0adbSRoger A. Faulkner fsetattrat(int fd, char *path, int flags, struct vattr *vap)
15547c478bd9Sstevel@tonic-gate {
1555794f0adbSRoger A. Faulkner 	vnode_t		*startvp;
1556794f0adbSRoger A. Faulkner 	vnode_t		*vp;
1557794f0adbSRoger A. Faulkner 	int		error;
1558794f0adbSRoger A. Faulkner 
1559794f0adbSRoger A. Faulkner 	/*
1560794f0adbSRoger A. Faulkner 	 * Since we are never called to set the size of a file, we don't
1561794f0adbSRoger A. Faulkner 	 * need to check for non-blocking locks (via nbl_need_check(vp)).
1562794f0adbSRoger A. Faulkner 	 */
1563794f0adbSRoger A. Faulkner 	ASSERT(!(vap->va_mask & AT_SIZE));
1564794f0adbSRoger A. Faulkner 
1565794f0adbSRoger A. Faulkner 	if ((error = fgetstartvp(fd, path, &startvp)) != 0)
1566794f0adbSRoger A. Faulkner 		return (error);
1567794f0adbSRoger A. Faulkner 	if (AU_AUDITING() && startvp != NULL)
1568794f0adbSRoger A. Faulkner 		audit_setfsat_path(1);
1569794f0adbSRoger A. Faulkner 
1570794f0adbSRoger A. Faulkner 	/*
1571794f0adbSRoger A. Faulkner 	 * Do lookup for fchownat/fchmodat when path not NULL
1572794f0adbSRoger A. Faulkner 	 */
1573794f0adbSRoger A. Faulkner 	if (path != NULL) {
1574794f0adbSRoger A. Faulkner 		if (error = lookupnameat(path, UIO_USERSPACE,
1575794f0adbSRoger A. Faulkner 		    (flags == AT_SYMLINK_NOFOLLOW) ?
1576794f0adbSRoger A. Faulkner 		    NO_FOLLOW : FOLLOW,
1577794f0adbSRoger A. Faulkner 		    NULLVPP, &vp, startvp)) {
1578794f0adbSRoger A. Faulkner 			if (startvp != NULL)
1579794f0adbSRoger A. Faulkner 				VN_RELE(startvp);
1580794f0adbSRoger A. Faulkner 			return (error);
1581794f0adbSRoger A. Faulkner 		}
1582794f0adbSRoger A. Faulkner 	} else {
1583794f0adbSRoger A. Faulkner 		vp = startvp;
1584794f0adbSRoger A. Faulkner 		ASSERT(vp);
1585794f0adbSRoger A. Faulkner 		VN_HOLD(vp);
1586794f0adbSRoger A. Faulkner 	}
15877c478bd9Sstevel@tonic-gate 
1588*cf96e8bfSMarcel Telka 	if (vp->v_type == VLNK && (vap->va_mask & AT_MODE) != 0) {
1589*cf96e8bfSMarcel Telka 		error = EOPNOTSUPP;
1590*cf96e8bfSMarcel Telka 	} else if (vn_is_readonly(vp)) {
15917c478bd9Sstevel@tonic-gate 		error = EROFS;
1592794f0adbSRoger A. Faulkner 	} else {
1593794f0adbSRoger A. Faulkner 		error = VOP_SETATTR(vp, vap, 0, CRED(), NULL);
15947c478bd9Sstevel@tonic-gate 	}
15957c478bd9Sstevel@tonic-gate 
1596794f0adbSRoger A. Faulkner 	if (startvp != NULL)
1597794f0adbSRoger A. Faulkner 		VN_RELE(startvp);
1598794f0adbSRoger A. Faulkner 	VN_RELE(vp);
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	return (error);
16017c478bd9Sstevel@tonic-gate }
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate /*
16047c478bd9Sstevel@tonic-gate  * Return true if the given vnode is referenced by any
16057c478bd9Sstevel@tonic-gate  * entry in the current process's file descriptor table.
16067c478bd9Sstevel@tonic-gate  */
16077c478bd9Sstevel@tonic-gate int
fisopen(vnode_t * vp)16087c478bd9Sstevel@tonic-gate fisopen(vnode_t *vp)
16097c478bd9Sstevel@tonic-gate {
16107c478bd9Sstevel@tonic-gate 	int fd;
16117c478bd9Sstevel@tonic-gate 	file_t *fp;
16127c478bd9Sstevel@tonic-gate 	vnode_t *ovp;
16137c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
16147c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 	mutex_enter(&fip->fi_lock);
16177c478bd9Sstevel@tonic-gate 	for (fd = 0; fd < fip->fi_nfiles; fd++) {
16187c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
16197c478bd9Sstevel@tonic-gate 		if ((fp = ufp->uf_file) != NULL &&
16207c478bd9Sstevel@tonic-gate 		    (ovp = fp->f_vnode) != NULL && VN_CMP(vp, ovp)) {
16217c478bd9Sstevel@tonic-gate 			UF_EXIT(ufp);
16227c478bd9Sstevel@tonic-gate 			mutex_exit(&fip->fi_lock);
16237c478bd9Sstevel@tonic-gate 			return (1);
16247c478bd9Sstevel@tonic-gate 		}
16257c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
16267c478bd9Sstevel@tonic-gate 	}
16277c478bd9Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
16287c478bd9Sstevel@tonic-gate 	return (0);
16297c478bd9Sstevel@tonic-gate }
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate /*
16327c478bd9Sstevel@tonic-gate  * Return zero if at least one file currently open (by curproc) shouldn't be
16337c478bd9Sstevel@tonic-gate  * allowed to change zones.
16347c478bd9Sstevel@tonic-gate  */
16357c478bd9Sstevel@tonic-gate int
files_can_change_zones(void)16367c478bd9Sstevel@tonic-gate files_can_change_zones(void)
16377c478bd9Sstevel@tonic-gate {
16387c478bd9Sstevel@tonic-gate 	int fd;
16397c478bd9Sstevel@tonic-gate 	file_t *fp;
16407c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
16417c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	mutex_enter(&fip->fi_lock);
16447c478bd9Sstevel@tonic-gate 	for (fd = 0; fd < fip->fi_nfiles; fd++) {
16457c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
16467c478bd9Sstevel@tonic-gate 		if ((fp = ufp->uf_file) != NULL &&
16477c478bd9Sstevel@tonic-gate 		    !vn_can_change_zones(fp->f_vnode)) {
16487c478bd9Sstevel@tonic-gate 			UF_EXIT(ufp);
16497c478bd9Sstevel@tonic-gate 			mutex_exit(&fip->fi_lock);
16507c478bd9Sstevel@tonic-gate 			return (0);
16517c478bd9Sstevel@tonic-gate 		}
16527c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
16537c478bd9Sstevel@tonic-gate 	}
16547c478bd9Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
16557c478bd9Sstevel@tonic-gate 	return (1);
16567c478bd9Sstevel@tonic-gate }
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate #ifdef DEBUG
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate /*
16617c478bd9Sstevel@tonic-gate  * The following functions are only used in ASSERT()s elsewhere.
16627c478bd9Sstevel@tonic-gate  * They do not modify the state of the system.
16637c478bd9Sstevel@tonic-gate  */
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate /*
16667c478bd9Sstevel@tonic-gate  * Return true (1) if the current thread is in the fpollinfo
16677c478bd9Sstevel@tonic-gate  * list for this file descriptor, else false (0).
16687c478bd9Sstevel@tonic-gate  */
16697c478bd9Sstevel@tonic-gate static int
curthread_in_plist(uf_entry_t * ufp)16707c478bd9Sstevel@tonic-gate curthread_in_plist(uf_entry_t *ufp)
16717c478bd9Sstevel@tonic-gate {
16727c478bd9Sstevel@tonic-gate 	fpollinfo_t *fpip;
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ufp->uf_lock));
16757c478bd9Sstevel@tonic-gate 	for (fpip = ufp->uf_fpollinfo; fpip; fpip = fpip->fp_next)
16767c478bd9Sstevel@tonic-gate 		if (fpip->fp_thread == curthread)
16777c478bd9Sstevel@tonic-gate 			return (1);
16787c478bd9Sstevel@tonic-gate 	return (0);
16797c478bd9Sstevel@tonic-gate }
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate /*
16827c478bd9Sstevel@tonic-gate  * Sanity check to make sure that after lwp_exit(),
16837c478bd9Sstevel@tonic-gate  * curthread does not appear on any fd's fpollinfo list.
16847c478bd9Sstevel@tonic-gate  */
16857c478bd9Sstevel@tonic-gate void
checkfpollinfo(void)16867c478bd9Sstevel@tonic-gate checkfpollinfo(void)
16877c478bd9Sstevel@tonic-gate {
16887c478bd9Sstevel@tonic-gate 	int fd;
16897c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
16907c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 	mutex_enter(&fip->fi_lock);
16937c478bd9Sstevel@tonic-gate 	for (fd = 0; fd < fip->fi_nfiles; fd++) {
16947c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, fd);
16957c478bd9Sstevel@tonic-gate 		ASSERT(!curthread_in_plist(ufp));
16967c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
16977c478bd9Sstevel@tonic-gate 	}
16987c478bd9Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
16997c478bd9Sstevel@tonic-gate }
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate /*
17027c478bd9Sstevel@tonic-gate  * Return true (1) if the current thread is in the fpollinfo
17037c478bd9Sstevel@tonic-gate  * list for this file descriptor, else false (0).
17047c478bd9Sstevel@tonic-gate  * This is the same as curthread_in_plist(),
17057c478bd9Sstevel@tonic-gate  * but is called w/o holding uf_lock.
17067c478bd9Sstevel@tonic-gate  */
17077c478bd9Sstevel@tonic-gate int
infpollinfo(int fd)17087c478bd9Sstevel@tonic-gate infpollinfo(int fd)
17097c478bd9Sstevel@tonic-gate {
17107c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
17117c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
17127c478bd9Sstevel@tonic-gate 	int rc;
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
17157c478bd9Sstevel@tonic-gate 	rc = curthread_in_plist(ufp);
17167c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
17177c478bd9Sstevel@tonic-gate 	return (rc);
17187c478bd9Sstevel@tonic-gate }
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate /*
17237c478bd9Sstevel@tonic-gate  * Add the curthread to fpollinfo list, meaning this fd is currently in the
17247c478bd9Sstevel@tonic-gate  * thread's poll cache. Each lwp polling this file descriptor should call
17257c478bd9Sstevel@tonic-gate  * this routine once.
17267c478bd9Sstevel@tonic-gate  */
17277c478bd9Sstevel@tonic-gate void
addfpollinfo(int fd)17287c478bd9Sstevel@tonic-gate addfpollinfo(int fd)
17297c478bd9Sstevel@tonic-gate {
17307c478bd9Sstevel@tonic-gate 	struct uf_entry *ufp;
17317c478bd9Sstevel@tonic-gate 	fpollinfo_t *fpip;
17327c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 	fpip = kmem_zalloc(sizeof (fpollinfo_t), KM_SLEEP);
17357c478bd9Sstevel@tonic-gate 	fpip->fp_thread = curthread;
17367c478bd9Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
17377c478bd9Sstevel@tonic-gate 	/*
17387c478bd9Sstevel@tonic-gate 	 * Assert we are not already on the list, that is, that
17397c478bd9Sstevel@tonic-gate 	 * this lwp did not call addfpollinfo twice for the same fd.
17407c478bd9Sstevel@tonic-gate 	 */
17417c478bd9Sstevel@tonic-gate 	ASSERT(!curthread_in_plist(ufp));
17427c478bd9Sstevel@tonic-gate 	/*
17437c478bd9Sstevel@tonic-gate 	 * addfpollinfo is always done inside the getf/releasef pair.
17447c478bd9Sstevel@tonic-gate 	 */
17457c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_refcnt >= 1);
17467c478bd9Sstevel@tonic-gate 	fpip->fp_next = ufp->uf_fpollinfo;
17477c478bd9Sstevel@tonic-gate 	ufp->uf_fpollinfo = fpip;
17487c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
17497c478bd9Sstevel@tonic-gate }
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate /*
1752f57e3e4cSRoger A. Faulkner  * Delete curthread from fpollinfo list if it is there.
17537c478bd9Sstevel@tonic-gate  */
17547c478bd9Sstevel@tonic-gate void
delfpollinfo(int fd)17557c478bd9Sstevel@tonic-gate delfpollinfo(int fd)
17567c478bd9Sstevel@tonic-gate {
17577c478bd9Sstevel@tonic-gate 	struct uf_entry *ufp;
17587c478bd9Sstevel@tonic-gate 	struct fpollinfo *fpip;
17597c478bd9Sstevel@tonic-gate 	struct fpollinfo **fpipp;
17607c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
1763f57e3e4cSRoger A. Faulkner 	for (fpipp = &ufp->uf_fpollinfo;
1764f57e3e4cSRoger A. Faulkner 	    (fpip = *fpipp) != NULL;
1765f57e3e4cSRoger A. Faulkner 	    fpipp = &fpip->fp_next) {
1766f57e3e4cSRoger A. Faulkner 		if (fpip->fp_thread == curthread) {
17677c478bd9Sstevel@tonic-gate 			*fpipp = fpip->fp_next;
17687c478bd9Sstevel@tonic-gate 			kmem_free(fpip, sizeof (fpollinfo_t));
1769f57e3e4cSRoger A. Faulkner 			break;
1770f57e3e4cSRoger A. Faulkner 		}
1771f57e3e4cSRoger A. Faulkner 	}
17727c478bd9Sstevel@tonic-gate 	/*
17737c478bd9Sstevel@tonic-gate 	 * Assert that we are not still on the list, that is, that
17747c478bd9Sstevel@tonic-gate 	 * this lwp did not call addfpollinfo twice for the same fd.
17757c478bd9Sstevel@tonic-gate 	 */
17767c478bd9Sstevel@tonic-gate 	ASSERT(!curthread_in_plist(ufp));
17777c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
17787c478bd9Sstevel@tonic-gate }
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate /*
17817c478bd9Sstevel@tonic-gate  * fd is associated with a port. pfd is a pointer to the fd entry in the
17827c478bd9Sstevel@tonic-gate  * cache of the port.
17837c478bd9Sstevel@tonic-gate  */
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate void
addfd_port(int fd,portfd_t * pfd)17867c478bd9Sstevel@tonic-gate addfd_port(int fd, portfd_t *pfd)
17877c478bd9Sstevel@tonic-gate {
17887c478bd9Sstevel@tonic-gate 	struct uf_entry *ufp;
17897c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
17927c478bd9Sstevel@tonic-gate 	/*
17937c478bd9Sstevel@tonic-gate 	 * addfd_port is always done inside the getf/releasef pair.
17947c478bd9Sstevel@tonic-gate 	 */
17957c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_refcnt >= 1);
17967c478bd9Sstevel@tonic-gate 	if (ufp->uf_portfd == NULL) {
17977c478bd9Sstevel@tonic-gate 		/* first entry */
17987c478bd9Sstevel@tonic-gate 		ufp->uf_portfd = pfd;
17997c478bd9Sstevel@tonic-gate 		pfd->pfd_next = NULL;
18007c478bd9Sstevel@tonic-gate 	} else {
18017c478bd9Sstevel@tonic-gate 		pfd->pfd_next = ufp->uf_portfd;
18027c478bd9Sstevel@tonic-gate 		ufp->uf_portfd = pfd;
18037c478bd9Sstevel@tonic-gate 		pfd->pfd_next->pfd_prev = pfd;
18047c478bd9Sstevel@tonic-gate 	}
18057c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
18067c478bd9Sstevel@tonic-gate }
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate void
delfd_port(int fd,portfd_t * pfd)18097c478bd9Sstevel@tonic-gate delfd_port(int fd, portfd_t *pfd)
18107c478bd9Sstevel@tonic-gate {
18117c478bd9Sstevel@tonic-gate 	struct uf_entry *ufp;
18127c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	UF_ENTER(ufp, fip, fd);
18157c478bd9Sstevel@tonic-gate 	/*
18167c478bd9Sstevel@tonic-gate 	 * delfd_port is always done inside the getf/releasef pair.
18177c478bd9Sstevel@tonic-gate 	 */
18187c478bd9Sstevel@tonic-gate 	ASSERT(ufp->uf_refcnt >= 1);
18197c478bd9Sstevel@tonic-gate 	if (ufp->uf_portfd == pfd) {
18207c478bd9Sstevel@tonic-gate 		/* remove first entry */
18217c478bd9Sstevel@tonic-gate 		ufp->uf_portfd = pfd->pfd_next;
18227c478bd9Sstevel@tonic-gate 	} else {
18237c478bd9Sstevel@tonic-gate 		pfd->pfd_prev->pfd_next = pfd->pfd_next;
18247c478bd9Sstevel@tonic-gate 		if (pfd->pfd_next != NULL)
18257c478bd9Sstevel@tonic-gate 			pfd->pfd_next->pfd_prev = pfd->pfd_prev;
18267c478bd9Sstevel@tonic-gate 	}
18277c478bd9Sstevel@tonic-gate 	UF_EXIT(ufp);
18287c478bd9Sstevel@tonic-gate }
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate static void
port_close_fd(portfd_t * pfd)183161b4b1efSpraks port_close_fd(portfd_t *pfd)
18327c478bd9Sstevel@tonic-gate {
18337c478bd9Sstevel@tonic-gate 	portfd_t	*pfdn;
18347c478bd9Sstevel@tonic-gate 
183561b4b1efSpraks 	/*
183661b4b1efSpraks 	 * At this point, no other thread should access
183761b4b1efSpraks 	 * the portfd_t list for this fd. The uf_file, uf_portfd
183861b4b1efSpraks 	 * pointers in the uf_entry_t struct for this fd would
183961b4b1efSpraks 	 * be set to NULL.
184061b4b1efSpraks 	 */
18417c478bd9Sstevel@tonic-gate 	for (; pfd != NULL; pfd = pfdn) {
18427c478bd9Sstevel@tonic-gate 		pfdn = pfd->pfd_next;
18437c478bd9Sstevel@tonic-gate 		port_close_pfd(pfd);
18447c478bd9Sstevel@tonic-gate 	}
18457c478bd9Sstevel@tonic-gate }
1846