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