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