Unleashed v1.4
[unleashed.git] / kernel / os / fio.c
blob0cb46c128ca7d1caa83fa6e0305d20133af8cae4
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2015, Joyent Inc.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
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>
61 #include <sys/nbmlock.h>
63 #ifdef DEBUG
65 static uint32_t afd_maxfd; /* # of entries in maximum allocated array */
66 static uint32_t afd_alloc; /* count of kmem_alloc()s */
67 static uint32_t afd_free; /* count of kmem_free()s */
68 static uint32_t afd_wait; /* count of waits on non-zero ref count */
69 #define MAXFD(x) (afd_maxfd = ((afd_maxfd >= (x))? afd_maxfd : (x)))
70 #define COUNT(x) atomic_inc_32(&x)
72 #else /* DEBUG */
74 #define MAXFD(x)
75 #define COUNT(x)
77 #endif /* DEBUG */
79 kmem_cache_t *file_cache;
81 static void port_close_fd(portfd_t *);
84 * File descriptor allocation.
86 * fd_find(fip, minfd) finds the first available descriptor >= minfd.
87 * The most common case is open(2), in which minfd = 0, but we must also
88 * support fcntl(fd, F_DUPFD, minfd).
90 * The algorithm is as follows: we keep all file descriptors in an infix
91 * binary tree in which each node records the number of descriptors
92 * allocated in its right subtree, including itself. Starting at minfd,
93 * we ascend the tree until we find a non-fully allocated right subtree.
94 * We then descend that subtree in a binary search for the smallest fd.
95 * Finally, we ascend the tree again to increment the allocation count
96 * of every subtree containing the newly-allocated fd. Freeing an fd
97 * requires only the last step: we ascend the tree to decrement allocation
98 * counts. Each of these three steps (ascent to find non-full subtree,
99 * descent to find lowest fd, ascent to update allocation counts) is
100 * O(log n), thus the algorithm as a whole is O(log n).
102 * We don't implement the fd tree using the customary left/right/parent
103 * pointers, but instead take advantage of the glorious mathematics of
104 * full infix binary trees. For reference, here's an illustration of the
105 * logical structure of such a tree, rooted at 4 (binary 100), covering
106 * the range 1-7 (binary 001-111). Our canonical trees do not include
107 * fd 0; we'll deal with that later.
109 * 100
110 * / \
111 * / \
112 * 010 110
113 * / \ / \
114 * 001 011 101 111
116 * We make the following observations, all of which are easily proven by
117 * induction on the depth of the tree:
119 * (T1) The least-significant bit (LSB) of any node is equal to its level
120 * in the tree. In our example, nodes 001, 011, 101 and 111 are at
121 * level 0; nodes 010 and 110 are at level 1; and node 100 is at level 2.
123 * (T2) The child size (CSIZE) of node N -- that is, the total number of
124 * right-branch descendants in a child of node N, including itself -- is
125 * given by clearing all but the least significant bit of N. This
126 * follows immediately from (T1). Applying this rule to our example, we
127 * see that CSIZE(100) = 100, CSIZE(x10) = 10, and CSIZE(xx1) = 1.
129 * (T3) The nearest left ancestor (LPARENT) of node N -- that is, the nearest
130 * ancestor containing node N in its right child -- is given by clearing
131 * the LSB of N. For example, LPARENT(111) = 110 and LPARENT(110) = 100.
132 * Clearing the LSB of nodes 001, 010 or 100 yields zero, reflecting
133 * the fact that these are leftmost nodes. Note that this algorithm
134 * automatically skips generations as necessary. For example, the parent
135 * of node 101 is 110, which is a *right* ancestor (not what we want);
136 * but its grandparent is 100, which is a left ancestor. Clearing the LSB
137 * of 101 gets us to 100 directly, skipping right past the uninteresting
138 * generation (110).
140 * Note that since LPARENT clears the LSB, whereas CSIZE clears all *but*
141 * the LSB, we can express LPARENT() nicely in terms of CSIZE():
143 * LPARENT(N) = N - CSIZE(N)
145 * (T4) The nearest right ancestor (RPARENT) of node N is given by:
147 * RPARENT(N) = N + CSIZE(N)
149 * (T5) For every interior node, the children differ from their parent by
150 * CSIZE(parent) / 2. In our example, CSIZE(100) / 2 = 2 = 10 binary,
151 * and indeed, the children of 100 are 100 +/- 10 = 010 and 110.
153 * Next, we'll need a few two's-complement math tricks. Suppose a number,
154 * N, has the following form:
156 * N = xxxx10...0
158 * That is, the binary representation of N consists of some string of bits,
159 * then a 1, then all zeroes. This amounts to nothing more than saying that
160 * N has a least-significant bit, which is true for any N != 0. If we look
161 * at N and N - 1 together, we see that we can combine them in useful ways:
163 * N = xxxx10...0
164 * N - 1 = xxxx01...1
165 * ------------------------
166 * N & (N - 1) = xxxx000000
167 * N | (N - 1) = xxxx111111
168 * N ^ (N - 1) = 111111
170 * In particular, this suggests several easy ways to clear all but the LSB,
171 * which by (T2) is exactly what we need to determine CSIZE(N) = 10...0.
172 * We'll opt for this formulation:
174 * (C1) CSIZE(N) = (N - 1) ^ (N | (N - 1))
176 * Similarly, we have an easy way to determine LPARENT(N), which requires
177 * that we clear the LSB of N:
179 * (L1) LPARENT(N) = N & (N - 1)
181 * We note in the above relations that (N | (N - 1)) - N = CSIZE(N) - 1.
182 * When combined with (T4), this yields an easy way to compute RPARENT(N):
184 * (R1) RPARENT(N) = (N | (N - 1)) + 1
186 * Finally, to accommodate fd 0 we must adjust all of our results by +/-1 to
187 * move the fd range from [1, 2^n) to [0, 2^n - 1). This is straightforward,
188 * so there's no need to belabor the algebra; the revised relations become:
190 * (C1a) CSIZE(N) = N ^ (N | (N + 1))
192 * (L1a) LPARENT(N) = (N & (N + 1)) - 1
194 * (R1a) RPARENT(N) = N | (N + 1)
196 * This completes the mathematical framework. We now have all the tools
197 * we need to implement fd_find() and fd_reserve().
199 * fd_find(fip, minfd) finds the smallest available file descriptor >= minfd.
200 * It does not actually allocate the descriptor; that's done by fd_reserve().
201 * fd_find() proceeds in two steps:
203 * (1) Find the leftmost subtree that contains a descriptor >= minfd.
204 * We start at the right subtree rooted at minfd. If this subtree is
205 * not full -- if fip->fi_list[minfd].uf_alloc != CSIZE(minfd) -- then
206 * step 1 is done. Otherwise, we know that all fds in this subtree
207 * are taken, so we ascend to RPARENT(minfd) using (R1a). We repeat
208 * this process until we either find a candidate subtree or exceed
209 * fip->fi_nfiles. We use (C1a) to compute CSIZE().
211 * (2) Find the smallest fd in the subtree discovered by step 1.
212 * Starting at the root of this subtree, we descend to find the
213 * smallest available fd. Since the left children have the smaller
214 * fds, we will descend rightward only when the left child is full.
216 * We begin by comparing the number of allocated fds in the root
217 * to the number of allocated fds in its right child; if they differ
218 * by exactly CSIZE(child), we know the left subtree is full, so we
219 * descend right; that is, the right child becomes the search root.
220 * Otherwise we leave the root alone and start following the right
221 * child's left children. As fortune would have it, this is very
222 * simple computationally: by (T5), the right child of fd is just
223 * fd + size, where size = CSIZE(fd) / 2. Applying (T5) again,
224 * we find that the right child's left child is fd + size - (size / 2) =
225 * fd + (size / 2); *its* left child is fd + (size / 2) - (size / 4) =
226 * fd + (size / 4), and so on. In general, fd's right child's
227 * leftmost nth descendant is fd + (size >> n). Thus, to follow
228 * the right child's left descendants, we just halve the size in
229 * each iteration of the search.
231 * When we descend leftward, we must keep track of the number of fds
232 * that were allocated in all the right subtrees we rejected, so we
233 * know how many of the root fd's allocations are in the remaining
234 * (as yet unexplored) leftmost part of its right subtree. When we
235 * encounter a fully-allocated left child -- that is, when we find
236 * that fip->fi_list[fd].uf_alloc == ralloc + size -- we descend right
237 * (as described earlier), resetting ralloc to zero.
239 * fd_reserve(fip, fd, incr) either allocates or frees fd, depending
240 * on whether incr is 1 or -1. Starting at fd, fd_reserve() ascends
241 * the leftmost ancestors (see (T3)) and updates the allocation counts.
242 * At each step we use (L1a) to compute LPARENT(), the next left ancestor.
244 * flist_minsize() finds the minimal tree that still covers all
245 * used fds; as long as the allocation count of a root node is zero, we
246 * don't need that node or its right subtree.
248 * flist_nalloc() counts the number of allocated fds in the tree, by starting
249 * at the top of the tree and summing the right-subtree allocation counts as
250 * it descends leftwards.
252 * Note: we assume that flist_grow() will keep fip->fi_nfiles of the form
253 * 2^n - 1. This ensures that the fd trees are always full, which saves
254 * quite a bit of boundary checking.
256 static int
257 fd_find(uf_info_t *fip, int minfd)
259 int size, ralloc, fd;
261 ASSERT(MUTEX_HELD(&fip->fi_lock));
262 ASSERT((fip->fi_nfiles & (fip->fi_nfiles + 1)) == 0);
264 for (fd = minfd; (uint_t)fd < fip->fi_nfiles; fd |= fd + 1) {
265 size = fd ^ (fd | (fd + 1));
266 if (fip->fi_list[fd].uf_alloc == size)
267 continue;
268 for (ralloc = 0, size >>= 1; size != 0; size >>= 1) {
269 ralloc += fip->fi_list[fd + size].uf_alloc;
270 if (fip->fi_list[fd].uf_alloc == ralloc + size) {
271 fd += size;
272 ralloc = 0;
275 return (fd);
277 return (-1);
280 static void
281 fd_reserve(uf_info_t *fip, int fd, int incr)
283 int pfd;
284 uf_entry_t *ufp = &fip->fi_list[fd];
286 ASSERT((uint_t)fd < fip->fi_nfiles);
287 ASSERT((ufp->uf_busy == 0 && incr == 1) ||
288 (ufp->uf_busy == 1 && incr == -1));
289 ASSERT(MUTEX_HELD(&ufp->uf_lock));
290 ASSERT(MUTEX_HELD(&fip->fi_lock));
292 for (pfd = fd; pfd >= 0; pfd = (pfd & (pfd + 1)) - 1)
293 fip->fi_list[pfd].uf_alloc += incr;
295 ufp->uf_busy += incr;
298 static int
299 flist_minsize(uf_info_t *fip)
301 int fd;
304 * We'd like to ASSERT(MUTEX_HELD(&fip->fi_lock)), but we're called
305 * by flist_fork(), which relies on other mechanisms for mutual
306 * exclusion.
308 ASSERT((fip->fi_nfiles & (fip->fi_nfiles + 1)) == 0);
310 for (fd = fip->fi_nfiles; fd != 0; fd >>= 1)
311 if (fip->fi_list[fd >> 1].uf_alloc != 0)
312 break;
314 return (fd);
317 static int
318 flist_nalloc(uf_info_t *fip)
320 int fd;
321 int nalloc = 0;
323 ASSERT(MUTEX_HELD(&fip->fi_lock));
324 ASSERT((fip->fi_nfiles & (fip->fi_nfiles + 1)) == 0);
326 for (fd = fip->fi_nfiles; fd != 0; fd >>= 1)
327 nalloc += fip->fi_list[fd >> 1].uf_alloc;
329 return (nalloc);
333 * Increase size of the fi_list array to accommodate at least maxfd.
334 * We keep the size of the form 2^n - 1 for benefit of fd_find().
336 static void
337 flist_grow(int maxfd)
339 uf_info_t *fip = P_FINFO(curproc);
340 int newcnt, oldcnt;
341 uf_entry_t *src, *dst, *newlist, *oldlist, *newend, *oldend;
342 uf_rlist_t *urp;
344 for (newcnt = 1; newcnt <= maxfd; newcnt = (newcnt << 1) | 1)
345 continue;
347 newlist = kmem_zalloc(newcnt * sizeof (uf_entry_t), KM_SLEEP);
349 mutex_enter(&fip->fi_lock);
350 oldcnt = fip->fi_nfiles;
351 if (newcnt <= oldcnt) {
352 mutex_exit(&fip->fi_lock);
353 kmem_free(newlist, newcnt * sizeof (uf_entry_t));
354 return;
356 ASSERT((newcnt & (newcnt + 1)) == 0);
357 oldlist = fip->fi_list;
358 oldend = oldlist + oldcnt;
359 newend = newlist + oldcnt; /* no need to lock beyond old end */
362 * fi_list and fi_nfiles cannot change while any uf_lock is held,
363 * so we must grab all the old locks *and* the new locks up to oldcnt.
364 * (Locks beyond the end of oldcnt aren't visible until we store
365 * the new fi_nfiles, which is the last thing we do before dropping
366 * all the locks, so there's no need to acquire these locks).
367 * Holding the new locks is necessary because when fi_list changes
368 * to point to the new list, fi_nfiles won't have been stored yet.
369 * If we *didn't* hold the new locks, someone doing a UF_ENTER()
370 * could see the new fi_list, grab the new uf_lock, and then see
371 * fi_nfiles change while the lock is held -- in violation of
372 * UF_ENTER() semantics.
374 for (src = oldlist; src < oldend; src++)
375 mutex_enter(&src->uf_lock);
377 for (dst = newlist; dst < newend; dst++)
378 mutex_enter(&dst->uf_lock);
380 for (src = oldlist, dst = newlist; src < oldend; src++, dst++) {
381 dst->uf_file = src->uf_file;
382 dst->uf_fpollinfo = src->uf_fpollinfo;
383 dst->uf_refcnt = src->uf_refcnt;
384 dst->uf_alloc = src->uf_alloc;
385 dst->uf_flag = src->uf_flag;
386 dst->uf_busy = src->uf_busy;
387 dst->uf_portfd = src->uf_portfd;
391 * As soon as we store the new flist, future locking operations
392 * will use it. Therefore, we must ensure that all the state
393 * we've just established reaches global visibility before the
394 * new flist does.
396 membar_producer();
397 fip->fi_list = newlist;
400 * Routines like getf() make an optimistic check on the validity
401 * of the supplied file descriptor: if it's less than the current
402 * value of fi_nfiles -- examined without any locks -- then it's
403 * safe to attempt a UF_ENTER() on that fd (which is a valid
404 * assumption because fi_nfiles only increases). Therefore, it
405 * is critical that the new value of fi_nfiles not reach global
406 * visibility until after the new fi_list: if it happened the
407 * other way around, getf() could see the new fi_nfiles and attempt
408 * a UF_ENTER() on the old fi_list, which would write beyond its
409 * end if the fd exceeded the old fi_nfiles.
411 membar_producer();
412 fip->fi_nfiles = newcnt;
415 * The new state is consistent now, so we can drop all the locks.
417 for (dst = newlist; dst < newend; dst++)
418 mutex_exit(&dst->uf_lock);
420 for (src = oldlist; src < oldend; src++) {
422 * If any threads are blocked on the old cvs, wake them.
423 * This will force them to wake up, discover that fi_list
424 * has changed, and go back to sleep on the new cvs.
426 cv_broadcast(&src->uf_wanted_cv);
427 cv_broadcast(&src->uf_closing_cv);
428 mutex_exit(&src->uf_lock);
431 mutex_exit(&fip->fi_lock);
434 * Retire the old flist. We can't actually kmem_free() it now
435 * because someone may still have a pointer to it. Instead,
436 * we link it onto a list of retired flists. The new flist
437 * is at least double the size of the previous flist, so the
438 * total size of all retired flists will be less than the size
439 * of the current one (to prove, consider the sum of a geometric
440 * series in powers of 2). exit() frees the retired flists.
442 urp = kmem_zalloc(sizeof (uf_rlist_t), KM_SLEEP);
443 urp->ur_list = oldlist;
444 urp->ur_nfiles = oldcnt;
446 mutex_enter(&fip->fi_lock);
447 urp->ur_next = fip->fi_rlist;
448 fip->fi_rlist = urp;
449 mutex_exit(&fip->fi_lock);
453 * Utility functions for keeping track of the active file descriptors.
455 void
456 clear_stale_fd() /* called from post_syscall() */
458 afd_t *afd = &curthread->t_activefd;
459 int i;
461 /* uninitialized is ok here, a_nfd is then zero */
462 for (i = 0; i < afd->a_nfd; i++) {
463 /* assert that this should not be necessary */
464 ASSERT(afd->a_fd[i] == -1);
465 afd->a_fd[i] = -1;
467 afd->a_stale = 0;
470 void
471 free_afd(afd_t *afd) /* called below and from thread_free() */
473 int i;
475 /* free the buffer if it was kmem_alloc()ed */
476 if (afd->a_nfd > sizeof (afd->a_buf) / sizeof (afd->a_buf[0])) {
477 COUNT(afd_free);
478 kmem_free(afd->a_fd, afd->a_nfd * sizeof (afd->a_fd[0]));
481 /* (re)initialize the structure */
482 afd->a_fd = &afd->a_buf[0];
483 afd->a_nfd = sizeof (afd->a_buf) / sizeof (afd->a_buf[0]);
484 afd->a_stale = 0;
485 for (i = 0; i < afd->a_nfd; i++)
486 afd->a_fd[i] = -1;
489 static void
490 set_active_fd(int fd)
492 afd_t *afd = &curthread->t_activefd;
493 int i;
494 int *old_fd;
495 int old_nfd;
496 int *new_fd;
497 int new_nfd;
499 if (afd->a_nfd == 0) { /* first time initialization */
500 ASSERT(fd == -1);
501 mutex_enter(&afd->a_fdlock);
502 free_afd(afd);
503 mutex_exit(&afd->a_fdlock);
506 /* insert fd into vacant slot, if any */
507 for (i = 0; i < afd->a_nfd; i++) {
508 if (afd->a_fd[i] == -1) {
509 afd->a_fd[i] = fd;
510 return;
515 * Reallocate the a_fd[] array to add one more slot.
517 ASSERT(fd == -1);
518 old_nfd = afd->a_nfd;
519 old_fd = afd->a_fd;
520 new_nfd = old_nfd + 1;
521 new_fd = kmem_alloc(new_nfd * sizeof (afd->a_fd[0]), KM_SLEEP);
522 MAXFD(new_nfd);
523 COUNT(afd_alloc);
525 mutex_enter(&afd->a_fdlock);
526 afd->a_fd = new_fd;
527 afd->a_nfd = new_nfd;
528 for (i = 0; i < old_nfd; i++)
529 afd->a_fd[i] = old_fd[i];
530 afd->a_fd[i] = fd;
531 mutex_exit(&afd->a_fdlock);
533 if (old_nfd > sizeof (afd->a_buf) / sizeof (afd->a_buf[0])) {
534 COUNT(afd_free);
535 kmem_free(old_fd, old_nfd * sizeof (afd->a_fd[0]));
539 void
540 clear_active_fd(int fd) /* called below and from aio.c */
542 afd_t *afd = &curthread->t_activefd;
543 int i;
545 for (i = 0; i < afd->a_nfd; i++) {
546 if (afd->a_fd[i] == fd) {
547 afd->a_fd[i] = -1;
548 break;
551 ASSERT(i < afd->a_nfd); /* not found is not ok */
555 * Does this thread have this fd active?
557 static int
558 is_active_fd(kthread_t *t, int fd)
560 afd_t *afd = &t->t_activefd;
561 int i;
563 ASSERT(t != curthread);
564 mutex_enter(&afd->a_fdlock);
565 /* uninitialized is ok here, a_nfd is then zero */
566 for (i = 0; i < afd->a_nfd; i++) {
567 if (afd->a_fd[i] == fd) {
568 mutex_exit(&afd->a_fdlock);
569 return (1);
572 mutex_exit(&afd->a_fdlock);
573 return (0);
577 * Convert a user supplied file descriptor into a pointer to a file
578 * structure. Only task is to check range of the descriptor (soft
579 * resource limit was enforced at open time and shouldn't be checked
580 * here).
582 file_t *
583 getf(int fd)
585 uf_info_t *fip = P_FINFO(curproc);
586 uf_entry_t *ufp;
587 file_t *fp;
589 if ((uint_t)fd >= fip->fi_nfiles)
590 return (NULL);
593 * Reserve a slot in the active fd array now so we can call
594 * set_active_fd(fd) for real below, while still inside UF_ENTER().
596 set_active_fd(-1);
598 UF_ENTER(ufp, fip, fd);
600 if ((fp = ufp->uf_file) == NULL) {
601 UF_EXIT(ufp);
603 if (fd == fip->fi_badfd && fip->fi_action > 0)
604 tsignal(curthread, fip->fi_action);
606 return (NULL);
608 ufp->uf_refcnt++;
610 set_active_fd(fd); /* record the active file descriptor */
612 UF_EXIT(ufp);
614 return (fp);
618 * Close whatever file currently occupies the file descriptor slot
619 * and install the new file, usually NULL, in the file descriptor slot.
620 * The close must complete before we release the file descriptor slot.
621 * If newfp != NULL we only return an error if we can't allocate the
622 * slot so the caller knows that it needs to free the filep;
623 * in the other cases we return the error number from closef().
626 closeandsetf(int fd, file_t *newfp)
628 proc_t *p = curproc;
629 uf_info_t *fip = P_FINFO(p);
630 uf_entry_t *ufp;
631 file_t *fp;
632 fpollinfo_t *fpip;
633 portfd_t *pfd;
634 int error;
636 if ((uint_t)fd >= fip->fi_nfiles) {
637 if (newfp == NULL)
638 return (EBADF);
639 flist_grow(fd);
642 if (newfp != NULL) {
644 * If ufp is reserved but has no file pointer, it's in the
645 * transition between ufalloc() and setf(). We must wait
646 * for this transition to complete before assigning the
647 * new non-NULL file pointer.
649 mutex_enter(&fip->fi_lock);
650 if (fd == fip->fi_badfd) {
651 mutex_exit(&fip->fi_lock);
652 if (fip->fi_action > 0)
653 tsignal(curthread, fip->fi_action);
654 return (EBADF);
656 UF_ENTER(ufp, fip, fd);
657 while (ufp->uf_busy && ufp->uf_file == NULL) {
658 mutex_exit(&fip->fi_lock);
659 cv_wait_stop(&ufp->uf_wanted_cv, &ufp->uf_lock, 250);
660 UF_EXIT(ufp);
661 mutex_enter(&fip->fi_lock);
662 UF_ENTER(ufp, fip, fd);
664 if ((fp = ufp->uf_file) == NULL) {
665 ASSERT(ufp->uf_fpollinfo == NULL);
666 ASSERT(ufp->uf_flag == 0);
667 fd_reserve(fip, fd, 1);
668 ufp->uf_file = newfp;
669 UF_EXIT(ufp);
670 mutex_exit(&fip->fi_lock);
671 return (0);
673 mutex_exit(&fip->fi_lock);
674 } else {
675 UF_ENTER(ufp, fip, fd);
676 if ((fp = ufp->uf_file) == NULL) {
677 UF_EXIT(ufp);
678 return (EBADF);
682 ASSERT(ufp->uf_busy);
683 ufp->uf_file = NULL;
684 ufp->uf_flag = 0;
687 * If the file descriptor reference count is non-zero, then
688 * some other lwp in the process is performing system call
689 * activity on the file. To avoid blocking here for a long
690 * time (the other lwp might be in a long term sleep in its
691 * system call), we scan all other lwps in the process to
692 * find the ones with this fd as one of their active fds,
693 * set their a_stale flag, and set them running if they
694 * are in an interruptible sleep so they will emerge from
695 * their system calls immediately. post_syscall() will
696 * test the a_stale flag and set errno to EBADF.
698 ASSERT(ufp->uf_refcnt == 0 || p->p_lwpcnt > 1);
699 if (ufp->uf_refcnt > 0) {
700 kthread_t *t;
703 * We call sprlock_proc(p) to ensure that the thread
704 * list will not change while we are scanning it.
705 * To do this, we must drop ufp->uf_lock and then
706 * reacquire it (so we are not holding both p->p_lock
707 * and ufp->uf_lock at the same time). ufp->uf_lock
708 * must be held for is_active_fd() to be correct
709 * (set_active_fd() is called while holding ufp->uf_lock).
711 * This is a convoluted dance, but it is better than
712 * the old brute-force method of stopping every thread
713 * in the process by calling holdlwps(SHOLDFORK1).
716 UF_EXIT(ufp);
717 COUNT(afd_wait);
719 mutex_enter(&p->p_lock);
720 sprlock_proc(p);
721 mutex_exit(&p->p_lock);
723 UF_ENTER(ufp, fip, fd);
724 ASSERT(ufp->uf_file == NULL);
726 if (ufp->uf_refcnt > 0) {
727 for (t = curthread->t_forw;
728 t != curthread;
729 t = t->t_forw) {
730 if (is_active_fd(t, fd)) {
731 thread_lock(t);
732 t->t_activefd.a_stale = 1;
733 t->t_post_sys = 1;
734 if (ISWAKEABLE(t))
735 setrun_locked(t);
736 thread_unlock(t);
741 UF_EXIT(ufp);
743 mutex_enter(&p->p_lock);
744 sprunlock(p);
746 UF_ENTER(ufp, fip, fd);
747 ASSERT(ufp->uf_file == NULL);
751 * Wait for other lwps to stop using this file descriptor.
753 while (ufp->uf_refcnt > 0) {
754 cv_wait_stop(&ufp->uf_closing_cv, &ufp->uf_lock, 250);
756 * cv_wait_stop() drops ufp->uf_lock, so the file list
757 * can change. Drop the lock on our (possibly) stale
758 * ufp and let UF_ENTER() find and lock the current ufp.
760 UF_EXIT(ufp);
761 UF_ENTER(ufp, fip, fd);
764 #ifdef DEBUG
766 * catch a watchfd on device's pollhead list but not on fpollinfo list
768 if (ufp->uf_fpollinfo != NULL)
769 checkwfdlist(fp->f_vnode, ufp->uf_fpollinfo);
770 #endif /* DEBUG */
773 * We may need to cleanup some cached poll states in t_pollstate
774 * before the fd can be reused. It is important that we don't
775 * access a stale thread structure. We will do the cleanup in two
776 * phases to avoid deadlock and holding uf_lock for too long.
777 * In phase 1, hold the uf_lock and call pollblockexit() to set
778 * state in t_pollstate struct so that a thread does not exit on
779 * us. In phase 2, we drop the uf_lock and call pollcacheclean().
781 pfd = ufp->uf_portfd;
782 ufp->uf_portfd = NULL;
783 fpip = ufp->uf_fpollinfo;
784 ufp->uf_fpollinfo = NULL;
785 if (fpip != NULL)
786 pollblockexit(fpip);
787 UF_EXIT(ufp);
788 if (fpip != NULL)
789 pollcacheclean(fpip, fd);
790 if (pfd)
791 port_close_fd(pfd);
794 * Keep the file descriptor entry reserved across the closef().
796 error = closef(fp);
798 setf(fd, newfp);
800 /* Only return closef() error when closing is all we do */
801 return (newfp == NULL ? error : 0);
805 * Decrement uf_refcnt; wakeup anyone waiting to close the file.
807 void
808 releasef(int fd)
810 uf_info_t *fip = P_FINFO(curproc);
811 uf_entry_t *ufp;
813 UF_ENTER(ufp, fip, fd);
814 ASSERT(ufp->uf_refcnt > 0);
815 clear_active_fd(fd); /* clear the active file descriptor */
816 if (--ufp->uf_refcnt == 0)
817 cv_broadcast(&ufp->uf_closing_cv);
818 UF_EXIT(ufp);
822 * Identical to releasef() but can be called from another process.
824 void
825 areleasef(int fd, uf_info_t *fip)
827 uf_entry_t *ufp;
829 UF_ENTER(ufp, fip, fd);
830 ASSERT(ufp->uf_refcnt > 0);
831 if (--ufp->uf_refcnt == 0)
832 cv_broadcast(&ufp->uf_closing_cv);
833 UF_EXIT(ufp);
837 * Duplicate all file descriptors across a fork.
839 void
840 flist_fork(uf_info_t *pfip, uf_info_t *cfip)
842 int fd, nfiles;
843 uf_entry_t *pufp, *cufp;
845 mutex_init(&cfip->fi_lock, NULL, MUTEX_DEFAULT, NULL);
846 cfip->fi_rlist = NULL;
849 * We don't need to hold fi_lock because all other lwp's in the
850 * parent have been held.
852 cfip->fi_nfiles = nfiles = flist_minsize(pfip);
854 cfip->fi_list = kmem_zalloc(nfiles * sizeof (uf_entry_t), KM_SLEEP);
856 for (fd = 0, pufp = pfip->fi_list, cufp = cfip->fi_list; fd < nfiles;
857 fd++, pufp++, cufp++) {
858 cufp->uf_file = pufp->uf_file;
859 cufp->uf_alloc = pufp->uf_alloc;
860 cufp->uf_flag = pufp->uf_flag;
861 cufp->uf_busy = pufp->uf_busy;
862 if (pufp->uf_file == NULL) {
863 ASSERT(pufp->uf_flag == 0);
864 if (pufp->uf_busy) {
866 * Grab locks to appease ASSERTs in fd_reserve
868 mutex_enter(&cfip->fi_lock);
869 mutex_enter(&cufp->uf_lock);
870 fd_reserve(cfip, fd, -1);
871 mutex_exit(&cufp->uf_lock);
872 mutex_exit(&cfip->fi_lock);
879 * Close all open file descriptors for the current process.
880 * This is only called from exit(), which is single-threaded,
881 * so we don't need any locking.
883 void
884 closeall(uf_info_t *fip)
886 int fd;
887 file_t *fp;
888 uf_entry_t *ufp;
890 ufp = fip->fi_list;
891 for (fd = 0; fd < fip->fi_nfiles; fd++, ufp++) {
892 if ((fp = ufp->uf_file) != NULL) {
893 ufp->uf_file = NULL;
894 if (ufp->uf_portfd != NULL) {
895 portfd_t *pfd;
896 /* remove event port association */
897 pfd = ufp->uf_portfd;
898 ufp->uf_portfd = NULL;
899 port_close_fd(pfd);
901 ASSERT(ufp->uf_fpollinfo == NULL);
902 (void) closef(fp);
906 kmem_free(fip->fi_list, fip->fi_nfiles * sizeof (uf_entry_t));
907 fip->fi_list = NULL;
908 fip->fi_nfiles = 0;
909 while (fip->fi_rlist != NULL) {
910 uf_rlist_t *urp = fip->fi_rlist;
911 fip->fi_rlist = urp->ur_next;
912 kmem_free(urp->ur_list, urp->ur_nfiles * sizeof (uf_entry_t));
913 kmem_free(urp, sizeof (uf_rlist_t));
918 * Internal form of close. Decrement reference count on file
919 * structure. Decrement reference count on the vnode following
920 * removal of the referencing file structure.
923 closef(file_t *fp)
925 vnode_t *vp;
926 int error;
927 int count;
928 int flag;
929 offset_t offset;
931 ASSERT(MUTEX_NOT_HELD(&P_FINFO(curproc)->fi_lock));
933 mutex_enter(&fp->f_tlock);
935 ASSERT(fp->f_count > 0);
937 count = fp->f_count--;
938 flag = fp->f_flag;
939 offset = fp->f_offset;
941 vp = fp->f_vnode;
943 error = fop_close(vp, flag, count, offset, fp->f_cred, NULL);
945 if (count > 1) {
946 mutex_exit(&fp->f_tlock);
947 return (error);
949 ASSERT(fp->f_count == 0);
950 /* Last reference, remove any OFD style lock for the file_t */
951 ofdcleanlock(fp);
952 mutex_exit(&fp->f_tlock);
955 * If DTrace has getf() subroutines active, it will set dtrace_closef
956 * to point to code that implements a barrier with respect to probe
957 * context. This must be called before the file_t is freed (and the
958 * vnode that it refers to is released) -- but it must be after the
959 * file_t has been removed from the uf_entry_t. That is, there must
960 * be no way for a racing getf() in probe context to yield the fp that
961 * we're operating upon.
963 if (dtrace_closef != NULL)
964 (*dtrace_closef)();
966 VN_RELE(vp);
967 crfree(fp->f_cred);
968 kmem_cache_free(file_cache, fp);
969 return (error);
973 * This is a combination of ufalloc() and setf().
976 ufalloc_file(int start, file_t *fp)
978 proc_t *p = curproc;
979 uf_info_t *fip = P_FINFO(p);
980 int filelimit;
981 uf_entry_t *ufp;
982 int nfiles;
983 int fd;
986 * Assertion is to convince the correctness of the following
987 * assignment for filelimit after casting to int.
989 ASSERT(p->p_fno_ctl <= INT_MAX);
990 filelimit = (int)p->p_fno_ctl;
992 for (;;) {
993 mutex_enter(&fip->fi_lock);
994 fd = fd_find(fip, start);
995 if (fd >= 0 && fd == fip->fi_badfd) {
996 start = fd + 1;
997 mutex_exit(&fip->fi_lock);
998 continue;
1000 if ((uint_t)fd < filelimit)
1001 break;
1002 if (fd >= filelimit) {
1003 mutex_exit(&fip->fi_lock);
1004 mutex_enter(&p->p_lock);
1005 (void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
1006 p->p_rctls, p, RCA_SAFE);
1007 mutex_exit(&p->p_lock);
1008 return (-1);
1010 /* fd_find() returned -1 */
1011 nfiles = fip->fi_nfiles;
1012 mutex_exit(&fip->fi_lock);
1013 flist_grow(MAX(start, nfiles));
1016 UF_ENTER(ufp, fip, fd);
1017 fd_reserve(fip, fd, 1);
1018 ASSERT(ufp->uf_file == NULL);
1019 ufp->uf_file = fp;
1020 UF_EXIT(ufp);
1021 mutex_exit(&fip->fi_lock);
1022 return (fd);
1026 * Allocate a user file descriptor greater than or equal to "start".
1029 ufalloc(int start)
1031 return (ufalloc_file(start, NULL));
1035 * Check that a future allocation of count fds on proc p has a good
1036 * chance of succeeding. If not, do rctl processing as if we'd failed
1037 * the allocation.
1039 * Our caller must guarantee that p cannot disappear underneath us.
1042 ufcanalloc(proc_t *p, uint_t count)
1044 uf_info_t *fip = P_FINFO(p);
1045 int filelimit;
1046 int current;
1048 if (count == 0)
1049 return (1);
1051 ASSERT(p->p_fno_ctl <= INT_MAX);
1052 filelimit = (int)p->p_fno_ctl;
1054 mutex_enter(&fip->fi_lock);
1055 current = flist_nalloc(fip); /* # of in-use descriptors */
1056 mutex_exit(&fip->fi_lock);
1059 * If count is a positive integer, the worst that can happen is
1060 * an overflow to a negative value, which is caught by the >= 0 check.
1062 current += count;
1063 if (count <= INT_MAX && current >= 0 && current <= filelimit)
1064 return (1);
1066 mutex_enter(&p->p_lock);
1067 (void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
1068 p->p_rctls, p, RCA_SAFE);
1069 mutex_exit(&p->p_lock);
1070 return (0);
1074 * Allocate a user file descriptor and a file structure.
1075 * Initialize the descriptor to point at the file structure.
1076 * If fdp is NULL, the user file descriptor will not be allocated.
1079 falloc(vnode_t *vp, int flag, file_t **fpp, int *fdp)
1081 file_t *fp;
1082 int fd;
1084 if (fdp) {
1085 if ((fd = ufalloc(0)) == -1)
1086 return (EMFILE);
1088 fp = kmem_cache_alloc(file_cache, KM_SLEEP);
1090 * Note: falloc returns the fp locked
1092 mutex_enter(&fp->f_tlock);
1093 fp->f_count = 1;
1094 fp->f_flag = (uint32_t)flag;
1095 fp->f_vnode = vp;
1096 fp->f_offset = 0;
1097 crhold(fp->f_cred = CRED());
1098 *fpp = fp;
1099 if (fdp)
1100 *fdp = fd;
1101 return (0);
1104 /*ARGSUSED*/
1105 static int
1106 file_cache_constructor(void *buf, void *cdrarg, int kmflags)
1108 file_t *fp = buf;
1110 mutex_init(&fp->f_tlock, NULL, MUTEX_DEFAULT, NULL);
1111 return (0);
1114 /*ARGSUSED*/
1115 static void
1116 file_cache_destructor(void *buf, void *cdrarg)
1118 file_t *fp = buf;
1120 mutex_destroy(&fp->f_tlock);
1123 void
1124 finit()
1126 file_cache = kmem_cache_create("file_cache", sizeof (file_t), 0,
1127 file_cache_constructor, file_cache_destructor, NULL, NULL, NULL, 0);
1130 void
1131 unfalloc(file_t *fp)
1133 ASSERT(MUTEX_HELD(&fp->f_tlock));
1134 if (--fp->f_count <= 0) {
1135 crfree(fp->f_cred);
1136 mutex_exit(&fp->f_tlock);
1137 kmem_cache_free(file_cache, fp);
1138 } else
1139 mutex_exit(&fp->f_tlock);
1143 * Given a file descriptor, set the user's
1144 * file pointer to the given parameter.
1146 void
1147 setf(int fd, file_t *fp)
1149 uf_info_t *fip = P_FINFO(curproc);
1150 uf_entry_t *ufp;
1152 if (fp == NULL) {
1153 mutex_enter(&fip->fi_lock);
1154 UF_ENTER(ufp, fip, fd);
1155 fd_reserve(fip, fd, -1);
1156 mutex_exit(&fip->fi_lock);
1157 } else {
1158 UF_ENTER(ufp, fip, fd);
1159 ASSERT(ufp->uf_busy);
1161 ASSERT(ufp->uf_fpollinfo == NULL);
1162 ASSERT(ufp->uf_flag == 0);
1163 ufp->uf_file = fp;
1164 cv_broadcast(&ufp->uf_wanted_cv);
1165 UF_EXIT(ufp);
1169 * Given a file descriptor, return the file table flags, plus,
1170 * if this is a socket in asynchronous mode, the FASYNC flag.
1171 * getf() may or may not have been called before calling f_getfl().
1174 f_getfl(int fd, int *flagp)
1176 uf_info_t *fip = P_FINFO(curproc);
1177 uf_entry_t *ufp;
1178 file_t *fp;
1179 int error;
1181 if ((uint_t)fd >= fip->fi_nfiles)
1182 error = EBADF;
1183 else {
1184 UF_ENTER(ufp, fip, fd);
1185 if ((fp = ufp->uf_file) == NULL)
1186 error = EBADF;
1187 else {
1188 vnode_t *vp = fp->f_vnode;
1189 int flag = (fp->f_flag & ~FEPOLLED);
1190 ASSERT((flag & (FREAD|FWRITE|FSEARCH|FEXEC)) != 0);
1193 * BSD fcntl() FASYNC compatibility.
1195 if (vp->v_type == VSOCK)
1196 flag |= sock_getfasync(vp);
1197 *flagp = flag;
1198 error = 0;
1200 UF_EXIT(ufp);
1203 return (error);
1207 * Given a file descriptor, return the user's file flags.
1208 * Force the FD_CLOEXEC flag for writable self-open /proc files.
1209 * getf() may or may not have been called before calling f_getfd_error().
1212 f_getfd_error(int fd, int *flagp)
1214 uf_info_t *fip = P_FINFO(curproc);
1215 uf_entry_t *ufp;
1216 file_t *fp;
1217 int flag;
1218 int error;
1220 if ((uint_t)fd >= fip->fi_nfiles)
1221 error = EBADF;
1222 else {
1223 UF_ENTER(ufp, fip, fd);
1224 if ((fp = ufp->uf_file) == NULL)
1225 error = EBADF;
1226 else {
1227 flag = ufp->uf_flag;
1228 if ((fp->f_flag & FWRITE) && pr_isself(fp->f_vnode))
1229 flag |= FD_CLOEXEC;
1230 *flagp = flag;
1231 error = 0;
1233 UF_EXIT(ufp);
1236 return (error);
1240 * getf() must have been called before calling f_getfd().
1242 char
1243 f_getfd(int fd)
1245 int flag = 0;
1246 (void) f_getfd_error(fd, &flag);
1247 return ((char)flag);
1251 * Given a file descriptor and file flags, set the user's file flags.
1252 * At present, the only valid flag is FD_CLOEXEC.
1253 * getf() may or may not have been called before calling f_setfd_error().
1256 f_setfd_error(int fd, int flags)
1258 uf_info_t *fip = P_FINFO(curproc);
1259 uf_entry_t *ufp;
1260 int error;
1262 if ((uint_t)fd >= fip->fi_nfiles)
1263 error = EBADF;
1264 else {
1265 UF_ENTER(ufp, fip, fd);
1266 if (ufp->uf_file == NULL)
1267 error = EBADF;
1268 else {
1269 ufp->uf_flag = flags & FD_CLOEXEC;
1270 error = 0;
1272 UF_EXIT(ufp);
1274 return (error);
1277 void
1278 f_setfd(int fd, char flags)
1280 (void) f_setfd_error(fd, flags);
1283 #define BADFD_MIN 3
1284 #define BADFD_MAX 255
1287 * Attempt to allocate a file descriptor which is bad and which
1288 * is "poison" to the application. It cannot be closed (except
1289 * on exec), allocated for a different use, etc.
1292 f_badfd(int start, int *fdp, int action)
1294 int fdr;
1295 int badfd;
1296 uf_info_t *fip = P_FINFO(curproc);
1298 #ifdef _LP64
1299 /* No restrictions on 64 bit _file */
1300 if (get_udatamodel() != DATAMODEL_ILP32)
1301 return (EINVAL);
1302 #endif
1304 if (start > BADFD_MAX || start < BADFD_MIN)
1305 return (EINVAL);
1307 if (action >= NSIG || action < 0)
1308 return (EINVAL);
1310 mutex_enter(&fip->fi_lock);
1311 badfd = fip->fi_badfd;
1312 mutex_exit(&fip->fi_lock);
1314 if (badfd != -1)
1315 return (EAGAIN);
1317 fdr = ufalloc(start);
1319 if (fdr > BADFD_MAX) {
1320 setf(fdr, NULL);
1321 return (EMFILE);
1323 if (fdr < 0)
1324 return (EMFILE);
1326 mutex_enter(&fip->fi_lock);
1327 if (fip->fi_badfd != -1) {
1328 /* Lost race */
1329 mutex_exit(&fip->fi_lock);
1330 setf(fdr, NULL);
1331 return (EAGAIN);
1333 fip->fi_action = action;
1334 fip->fi_badfd = fdr;
1335 mutex_exit(&fip->fi_lock);
1336 setf(fdr, NULL);
1338 *fdp = fdr;
1340 return (0);
1344 * Allocate a file descriptor and assign it to the vnode "*vpp",
1345 * performing the usual open protocol upon it and returning the
1346 * file descriptor allocated. It is the responsibility of the
1347 * caller to dispose of "*vpp" if any error occurs.
1350 fassign(vnode_t **vpp, int mode, int *fdp)
1352 file_t *fp;
1353 int error;
1354 int fd;
1356 if (error = falloc(NULL, mode, &fp, &fd))
1357 return (error);
1358 if (error = fop_open(vpp, mode, fp->f_cred, NULL)) {
1359 setf(fd, NULL);
1360 unfalloc(fp);
1361 return (error);
1363 fp->f_vnode = *vpp;
1364 mutex_exit(&fp->f_tlock);
1366 * Fill in the slot falloc reserved.
1368 setf(fd, fp);
1369 *fdp = fd;
1370 return (0);
1374 * When a process forks it must increment the f_count of all file pointers
1375 * since there is a new process pointing at them. fcnt_add(fip, 1) does this.
1376 * Since we are called when there is only 1 active lwp we don't need to
1377 * hold fi_lock or any uf_lock. If the fork fails, fork_fail() calls
1378 * fcnt_add(fip, -1) to restore the counts.
1380 void
1381 fcnt_add(uf_info_t *fip, int incr)
1383 int i;
1384 uf_entry_t *ufp;
1385 file_t *fp;
1387 ufp = fip->fi_list;
1388 for (i = 0; i < fip->fi_nfiles; i++, ufp++) {
1389 if ((fp = ufp->uf_file) != NULL) {
1390 mutex_enter(&fp->f_tlock);
1391 ASSERT((incr == 1 && fp->f_count >= 1) ||
1392 (incr == -1 && fp->f_count >= 2));
1393 fp->f_count += incr;
1394 mutex_exit(&fp->f_tlock);
1400 * This is called from exec to close all fd's that have the FD_CLOEXEC flag
1401 * set and also to close all self-open for write /proc file descriptors.
1403 void
1404 close_exec(uf_info_t *fip)
1406 int fd;
1407 file_t *fp;
1408 fpollinfo_t *fpip;
1409 uf_entry_t *ufp;
1410 portfd_t *pfd;
1412 ufp = fip->fi_list;
1413 for (fd = 0; fd < fip->fi_nfiles; fd++, ufp++) {
1414 if ((fp = ufp->uf_file) != NULL &&
1415 ((ufp->uf_flag & FD_CLOEXEC) ||
1416 ((fp->f_flag & FWRITE) && pr_isself(fp->f_vnode)))) {
1417 fpip = ufp->uf_fpollinfo;
1418 mutex_enter(&fip->fi_lock);
1419 mutex_enter(&ufp->uf_lock);
1420 fd_reserve(fip, fd, -1);
1421 mutex_exit(&fip->fi_lock);
1422 ufp->uf_file = NULL;
1423 ufp->uf_fpollinfo = NULL;
1424 ufp->uf_flag = 0;
1426 * We may need to cleanup some cached poll states
1427 * in t_pollstate before the fd can be reused. It
1428 * is important that we don't access a stale thread
1429 * structure. We will do the cleanup in two
1430 * phases to avoid deadlock and holding uf_lock for
1431 * too long. In phase 1, hold the uf_lock and call
1432 * pollblockexit() to set state in t_pollstate struct
1433 * so that a thread does not exit on us. In phase 2,
1434 * we drop the uf_lock and call pollcacheclean().
1436 pfd = ufp->uf_portfd;
1437 ufp->uf_portfd = NULL;
1438 if (fpip != NULL)
1439 pollblockexit(fpip);
1440 mutex_exit(&ufp->uf_lock);
1441 if (fpip != NULL)
1442 pollcacheclean(fpip, fd);
1443 if (pfd)
1444 port_close_fd(pfd);
1445 (void) closef(fp);
1449 /* Reset bad fd */
1450 fip->fi_badfd = -1;
1451 fip->fi_action = -1;
1455 * Utility function called by most of the *at() system call interfaces.
1457 * Generate a starting vnode pointer for an (fd, path) pair where 'fd'
1458 * is an open file descriptor for a directory to be used as the starting
1459 * point for the lookup of the relative pathname 'path' (or, if path is
1460 * NULL, generate a vnode pointer for the direct target of the operation).
1462 * If we successfully return a non-NULL startvp, it has been the target
1463 * of VN_HOLD() and the caller must call VN_RELE() on it.
1466 fgetstartvp(int fd, char *path, vnode_t **startvpp)
1468 vnode_t *startvp;
1469 file_t *startfp;
1470 char startchar;
1472 if (fd == AT_FDCWD && path == NULL)
1473 return (EFAULT);
1475 if (fd == AT_FDCWD) {
1477 * Start from the current working directory.
1479 startvp = NULL;
1480 } else {
1481 if (path == NULL)
1482 startchar = '\0';
1483 else if (copyin(path, &startchar, sizeof (char)))
1484 return (EFAULT);
1486 if (startchar == '/') {
1488 * 'path' is an absolute pathname.
1490 startvp = NULL;
1491 } else {
1493 * 'path' is a relative pathname or we will
1494 * be applying the operation to 'fd' itself.
1496 if ((startfp = getf(fd)) == NULL)
1497 return (EBADF);
1498 startvp = startfp->f_vnode;
1499 VN_HOLD(startvp);
1500 releasef(fd);
1503 *startvpp = startvp;
1504 return (0);
1508 * Called from fchownat() and fchmodat() to set ownership and mode.
1509 * The contents of *vap must be set before calling here.
1512 fsetattrat(int fd, char *path, int flags, struct vattr *vap)
1514 vnode_t *startvp;
1515 vnode_t *vp;
1516 int error;
1519 * Since we are never called to set the size of a file, we don't
1520 * need to check for non-blocking locks (via nbl_need_check(vp)).
1522 ASSERT(!(vap->va_mask & VATTR_SIZE));
1524 if ((error = fgetstartvp(fd, path, &startvp)) != 0)
1525 return (error);
1528 * Do lookup for fchownat/fchmodat when path not NULL
1530 if (path != NULL) {
1531 if (error = lookupnameat(path, UIO_USERSPACE,
1532 (flags == AT_SYMLINK_NOFOLLOW) ?
1533 NO_FOLLOW : FOLLOW,
1534 NULLVPP, &vp, startvp)) {
1535 if (startvp != NULL)
1536 VN_RELE(startvp);
1537 return (error);
1539 } else {
1540 vp = startvp;
1541 ASSERT(vp);
1542 VN_HOLD(vp);
1545 if (vn_is_readonly(vp)) {
1546 error = EROFS;
1547 } else {
1548 error = fop_setattr(vp, vap, 0, CRED(), NULL);
1551 if (startvp != NULL)
1552 VN_RELE(startvp);
1553 VN_RELE(vp);
1555 return (error);
1559 * Return true if the given vnode is referenced by any
1560 * entry in the current process's file descriptor table.
1563 fisopen(vnode_t *vp)
1565 int fd;
1566 file_t *fp;
1567 vnode_t *ovp;
1568 uf_info_t *fip = P_FINFO(curproc);
1569 uf_entry_t *ufp;
1571 mutex_enter(&fip->fi_lock);
1572 for (fd = 0; fd < fip->fi_nfiles; fd++) {
1573 UF_ENTER(ufp, fip, fd);
1574 if ((fp = ufp->uf_file) != NULL &&
1575 (ovp = fp->f_vnode) != NULL && VN_CMP(vp, ovp)) {
1576 UF_EXIT(ufp);
1577 mutex_exit(&fip->fi_lock);
1578 return (1);
1580 UF_EXIT(ufp);
1582 mutex_exit(&fip->fi_lock);
1583 return (0);
1587 * Return zero if at least one file currently open (by curproc) shouldn't be
1588 * allowed to change zones.
1591 files_can_change_zones(void)
1593 int fd;
1594 file_t *fp;
1595 uf_info_t *fip = P_FINFO(curproc);
1596 uf_entry_t *ufp;
1598 mutex_enter(&fip->fi_lock);
1599 for (fd = 0; fd < fip->fi_nfiles; fd++) {
1600 UF_ENTER(ufp, fip, fd);
1601 if ((fp = ufp->uf_file) != NULL &&
1602 !vn_can_change_zones(fp->f_vnode)) {
1603 UF_EXIT(ufp);
1604 mutex_exit(&fip->fi_lock);
1605 return (0);
1607 UF_EXIT(ufp);
1609 mutex_exit(&fip->fi_lock);
1610 return (1);
1613 #ifdef DEBUG
1616 * The following functions are only used in ASSERT()s elsewhere.
1617 * They do not modify the state of the system.
1621 * Return true (1) if the current thread is in the fpollinfo
1622 * list for this file descriptor, else false (0).
1624 static int
1625 curthread_in_plist(uf_entry_t *ufp)
1627 fpollinfo_t *fpip;
1629 ASSERT(MUTEX_HELD(&ufp->uf_lock));
1630 for (fpip = ufp->uf_fpollinfo; fpip; fpip = fpip->fp_next)
1631 if (fpip->fp_thread == curthread)
1632 return (1);
1633 return (0);
1637 * Sanity check to make sure that after lwp_exit(),
1638 * curthread does not appear on any fd's fpollinfo list.
1640 void
1641 checkfpollinfo(void)
1643 int fd;
1644 uf_info_t *fip = P_FINFO(curproc);
1645 uf_entry_t *ufp;
1647 mutex_enter(&fip->fi_lock);
1648 for (fd = 0; fd < fip->fi_nfiles; fd++) {
1649 UF_ENTER(ufp, fip, fd);
1650 ASSERT(!curthread_in_plist(ufp));
1651 UF_EXIT(ufp);
1653 mutex_exit(&fip->fi_lock);
1657 * Return true (1) if the current thread is in the fpollinfo
1658 * list for this file descriptor, else false (0).
1659 * This is the same as curthread_in_plist(),
1660 * but is called w/o holding uf_lock.
1663 infpollinfo(int fd)
1665 uf_info_t *fip = P_FINFO(curproc);
1666 uf_entry_t *ufp;
1667 int rc;
1669 UF_ENTER(ufp, fip, fd);
1670 rc = curthread_in_plist(ufp);
1671 UF_EXIT(ufp);
1672 return (rc);
1675 #endif /* DEBUG */
1678 * Add the curthread to fpollinfo list, meaning this fd is currently in the
1679 * thread's poll cache. Each lwp polling this file descriptor should call
1680 * this routine once.
1682 void
1683 addfpollinfo(int fd)
1685 struct uf_entry *ufp;
1686 fpollinfo_t *fpip;
1687 uf_info_t *fip = P_FINFO(curproc);
1689 fpip = kmem_zalloc(sizeof (fpollinfo_t), KM_SLEEP);
1690 fpip->fp_thread = curthread;
1691 UF_ENTER(ufp, fip, fd);
1693 * Assert we are not already on the list, that is, that
1694 * this lwp did not call addfpollinfo twice for the same fd.
1696 ASSERT(!curthread_in_plist(ufp));
1698 * addfpollinfo is always done inside the getf/releasef pair.
1700 ASSERT(ufp->uf_refcnt >= 1);
1701 fpip->fp_next = ufp->uf_fpollinfo;
1702 ufp->uf_fpollinfo = fpip;
1703 UF_EXIT(ufp);
1707 * Delete curthread from fpollinfo list if it is there.
1709 void
1710 delfpollinfo(int fd)
1712 struct uf_entry *ufp;
1713 struct fpollinfo *fpip;
1714 struct fpollinfo **fpipp;
1715 uf_info_t *fip = P_FINFO(curproc);
1717 UF_ENTER(ufp, fip, fd);
1718 for (fpipp = &ufp->uf_fpollinfo;
1719 (fpip = *fpipp) != NULL;
1720 fpipp = &fpip->fp_next) {
1721 if (fpip->fp_thread == curthread) {
1722 *fpipp = fpip->fp_next;
1723 kmem_free(fpip, sizeof (fpollinfo_t));
1724 break;
1728 * Assert that we are not still on the list, that is, that
1729 * this lwp did not call addfpollinfo twice for the same fd.
1731 ASSERT(!curthread_in_plist(ufp));
1732 UF_EXIT(ufp);
1736 * fd is associated with a port. pfd is a pointer to the fd entry in the
1737 * cache of the port.
1740 void
1741 addfd_port(int fd, portfd_t *pfd)
1743 struct uf_entry *ufp;
1744 uf_info_t *fip = P_FINFO(curproc);
1746 UF_ENTER(ufp, fip, fd);
1748 * addfd_port is always done inside the getf/releasef pair.
1750 ASSERT(ufp->uf_refcnt >= 1);
1751 if (ufp->uf_portfd == NULL) {
1752 /* first entry */
1753 ufp->uf_portfd = pfd;
1754 pfd->pfd_next = NULL;
1755 } else {
1756 pfd->pfd_next = ufp->uf_portfd;
1757 ufp->uf_portfd = pfd;
1758 pfd->pfd_next->pfd_prev = pfd;
1760 UF_EXIT(ufp);
1763 void
1764 delfd_port(int fd, portfd_t *pfd)
1766 struct uf_entry *ufp;
1767 uf_info_t *fip = P_FINFO(curproc);
1769 UF_ENTER(ufp, fip, fd);
1771 * delfd_port is always done inside the getf/releasef pair.
1773 ASSERT(ufp->uf_refcnt >= 1);
1774 if (ufp->uf_portfd == pfd) {
1775 /* remove first entry */
1776 ufp->uf_portfd = pfd->pfd_next;
1777 } else {
1778 pfd->pfd_prev->pfd_next = pfd->pfd_next;
1779 if (pfd->pfd_next != NULL)
1780 pfd->pfd_next->pfd_prev = pfd->pfd_prev;
1782 UF_EXIT(ufp);
1785 static void
1786 port_close_fd(portfd_t *pfd)
1788 portfd_t *pfdn;
1791 * At this point, no other thread should access
1792 * the portfd_t list for this fd. The uf_file, uf_portfd
1793 * pointers in the uf_entry_t struct for this fd would
1794 * be set to NULL.
1796 for (; pfd != NULL; pfd = pfdn) {
1797 pfdn = pfd->pfd_next;
1798 port_close_pfd(pfd);