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