kernel - Remove SMP bottlenecks on uidinfo, descriptors, and lockf
[dragonfly.git] / sys / kern / kern_descrip.c
blobc69696ffe7904724a6fae2fcb967456c7ea5143b
1 /*
2 * Copyright (c) 2005-2018 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Jeffrey Hsu and Matthew Dillon.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 * Copyright (c) 1982, 1986, 1989, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
37 * (c) UNIX System Laboratories, Inc.
38 * All or some portions of this file are derived from material licensed
39 * to the University of California by American Telephone and Telegraph
40 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
41 * the permission of UNIX System Laboratories, Inc.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
67 * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94
68 * $FreeBSD: src/sys/kern/kern_descrip.c,v 1.81.2.19 2004/02/28 00:43:31 tegge Exp $
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/malloc.h>
74 #include <sys/sysproto.h>
75 #include <sys/conf.h>
76 #include <sys/device.h>
77 #include <sys/file.h>
78 #include <sys/filedesc.h>
79 #include <sys/kernel.h>
80 #include <sys/sysctl.h>
81 #include <sys/vnode.h>
82 #include <sys/proc.h>
83 #include <sys/nlookup.h>
84 #include <sys/stat.h>
85 #include <sys/filio.h>
86 #include <sys/fcntl.h>
87 #include <sys/unistd.h>
88 #include <sys/resourcevar.h>
89 #include <sys/event.h>
90 #include <sys/kern_syscall.h>
91 #include <sys/kcore.h>
92 #include <sys/kinfo.h>
93 #include <sys/un.h>
94 #include <sys/objcache.h>
96 #include <vm/vm.h>
97 #include <vm/vm_extern.h>
99 #include <sys/thread2.h>
100 #include <sys/file2.h>
101 #include <sys/spinlock2.h>
103 static void fsetfd_locked(struct filedesc *fdp, struct file *fp, int fd);
104 static void fdreserve_locked (struct filedesc *fdp, int fd0, int incr);
105 static struct file *funsetfd_locked (struct filedesc *fdp, int fd);
106 static void ffree(struct file *fp);
108 static MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
109 static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "file desc to leader",
110 "file desc to leader structures");
111 MALLOC_DEFINE(M_FILE, "file", "Open file structure");
112 static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
114 static struct krate krate_uidinfo = { .freq = 1 };
116 static d_open_t fdopen;
117 #define NUMFDESC 64
119 #define CDEV_MAJOR 22
120 static struct dev_ops fildesc_ops = {
121 { "FD", 0, 0 },
122 .d_open = fdopen,
126 * Descriptor management.
128 #ifndef NFILELIST_HEADS
129 #define NFILELIST_HEADS 257 /* primary number */
130 #endif
132 struct filelist_head {
133 struct spinlock spin;
134 struct filelist list;
135 } __cachealign;
137 static struct filelist_head filelist_heads[NFILELIST_HEADS];
139 static int nfiles; /* actual number of open files */
140 extern int cmask;
142 struct lwkt_token revoke_token = LWKT_TOKEN_INITIALIZER(revoke_token);
144 static struct objcache *file_objcache;
146 static struct objcache_malloc_args file_malloc_args = {
147 .objsize = sizeof(struct file),
148 .mtype = M_FILE
152 * Fixup fd_freefile and fd_lastfile after a descriptor has been cleared.
154 * must be called with fdp->fd_spin exclusively held
156 static __inline
157 void
158 fdfixup_locked(struct filedesc *fdp, int fd)
160 if (fd < fdp->fd_freefile) {
161 fdp->fd_freefile = fd;
163 while (fdp->fd_lastfile >= 0 &&
164 fdp->fd_files[fdp->fd_lastfile].fp == NULL &&
165 fdp->fd_files[fdp->fd_lastfile].reserved == 0
167 --fdp->fd_lastfile;
172 * Clear the fd thread caches for this fdnode.
174 * If match_fdc is NULL, all thread caches of fdn will be cleared.
175 * The caller must hold fdp->fd_spin exclusively. The threads caching
176 * the descriptor do not have to be the current thread. The (status)
177 * argument is ignored.
179 * If match_fdc is not NULL, only the match_fdc's cache will be cleared.
180 * The caller must hold fdp->fd_spin shared and match_fdc must match a
181 * fdcache entry in curthread. match_fdc has been locked by the caller
182 * and had the specified (status).
184 * Since we are matching against a fp in the fdp (which must still be present
185 * at this time), fp will have at least two refs on any match and we can
186 * decrement the count trivially.
188 static
189 void
190 fclearcache(struct fdnode *fdn, struct fdcache *match_fdc, int status)
192 struct fdcache *fdc;
193 struct file *fp;
194 int i;
197 * match_fdc == NULL We are cleaning out all tdcache entries
198 * for the fdn and hold fdp->fd_spin exclusively.
199 * This can race against the target threads
200 * cleaning out specific entries.
202 * match_fdc != NULL We are cleaning out a specific tdcache
203 * entry on behalf of the owning thread
204 * and hold fdp->fd_spin shared. The thread
205 * has already locked the entry. This cannot
206 * race.
208 fp = fdn->fp;
209 for (i = 0; i < NTDCACHEFD; ++i) {
210 if ((fdc = fdn->tdcache[i]) == NULL)
211 continue;
214 * If match_fdc is non-NULL we are being asked to
215 * clear a specific fdc owned by curthread. There must
216 * be exactly one match. The caller has already locked
217 * the cache entry and will dispose of the lock after
218 * we return.
220 * Since we also have a shared lock on fdp, we
221 * can do this without atomic ops.
223 if (match_fdc) {
224 if (fdc != match_fdc)
225 continue;
226 fdn->tdcache[i] = NULL;
227 KASSERT(fp == fdc->fp,
228 ("fclearcache(1): fp mismatch %p/%p\n",
229 fp, fdc->fp));
230 fdc->fp = NULL;
231 fdc->fd = -1;
234 * status can be 0 or 2. If 2 the ref is borrowed,
235 * if 0 the ref is not borrowed and we have to drop
236 * it.
238 if (status == 0)
239 atomic_add_int(&fp->f_count, -1);
240 fdn->isfull = 0; /* heuristic */
241 return;
245 * Otherwise we hold an exclusive spin-lock and can only
246 * race thread consumers borrowing cache entries.
248 * Acquire the lock and dispose of the entry. We have to
249 * spin until we get the lock.
251 for (;;) {
252 status = atomic_swap_int(&fdc->locked, 1);
253 if (status == 1) { /* foreign lock, retry */
254 cpu_pause();
255 continue;
257 fdn->tdcache[i] = NULL;
258 KASSERT(fp == fdc->fp,
259 ("fclearcache(2): fp mismatch %p/%p\n",
260 fp, fdc->fp));
261 fdc->fp = NULL;
262 fdc->fd = -1;
263 if (status == 0)
264 atomic_add_int(&fp->f_count, -1);
265 fdn->isfull = 0; /* heuristic */
266 atomic_swap_int(&fdc->locked, 0);
267 break;
270 KKASSERT(match_fdc == NULL);
274 * Retrieve the fp for the specified fd given the specified file descriptor
275 * table. The fdp does not have to be owned by the current process.
276 * If flags != -1, fp->f_flag must contain at least one of the flags.
278 * This function is not able to cache the fp.
280 struct file *
281 holdfp_fdp(struct filedesc *fdp, int fd, int flag)
283 struct file *fp;
285 spin_lock_shared(&fdp->fd_spin);
286 if (((u_int)fd) < fdp->fd_nfiles) {
287 fp = fdp->fd_files[fd].fp; /* can be NULL */
288 if (fp) {
289 if ((fp->f_flag & flag) == 0 && flag != -1) {
290 fp = NULL;
291 } else {
292 fhold(fp);
295 } else {
296 fp = NULL;
298 spin_unlock_shared(&fdp->fd_spin);
300 return fp;
303 struct file *
304 holdfp_fdp_locked(struct filedesc *fdp, int fd, int flag)
306 struct file *fp;
308 if (((u_int)fd) < fdp->fd_nfiles) {
309 fp = fdp->fd_files[fd].fp; /* can be NULL */
310 if (fp) {
311 if ((fp->f_flag & flag) == 0 && flag != -1) {
312 fp = NULL;
313 } else {
314 fhold(fp);
317 } else {
318 fp = NULL;
320 return fp;
324 * Acquire the fp for the specified file descriptor, using the thread
325 * cache if possible and caching it if possible.
327 * td must be the curren thread.
329 static
330 struct file *
331 _holdfp_cache(thread_t td, int fd)
333 struct filedesc *fdp;
334 struct fdcache *fdc;
335 struct fdcache *best;
336 struct fdnode *fdn;
337 struct file *fp;
338 int status;
339 int delta;
340 int i;
343 * Fast
345 for (fdc = &td->td_fdcache[0]; fdc < &td->td_fdcache[NFDCACHE]; ++fdc) {
346 if (fdc->fd != fd || fdc->fp == NULL)
347 continue;
348 status = atomic_swap_int(&fdc->locked, 1);
351 * If someone else has locked our cache entry they are in
352 * the middle of clearing it, skip the entry.
354 if (status == 1)
355 continue;
358 * We have locked the entry, but if it no longer matches
359 * restore the previous state (0 or 2) and skip the entry.
361 if (fdc->fd != fd || fdc->fp == NULL) {
362 atomic_swap_int(&fdc->locked, status);
363 continue;
367 * We have locked a valid entry. We can borrow the ref
368 * for a mode 0 entry. We can get a valid fp for a mode
369 * 2 entry but not borrow the ref.
371 if (status == 0) {
372 fp = fdc->fp;
373 fdc->lru = ++td->td_fdcache_lru;
374 atomic_swap_int(&fdc->locked, 2);
376 return fp;
378 if (status == 2) {
379 fp = fdc->fp;
380 fhold(fp);
381 fdc->lru = ++td->td_fdcache_lru;
382 atomic_swap_int(&fdc->locked, 2);
384 return fp;
386 KKASSERT(0);
390 * Lookup the descriptor the slow way. This can contend against
391 * modifying operations in a multi-threaded environment and cause
392 * cache line ping ponging otherwise.
394 fdp = td->td_proc->p_fd;
395 spin_lock_shared(&fdp->fd_spin);
397 if (((u_int)fd) < fdp->fd_nfiles) {
398 fp = fdp->fd_files[fd].fp; /* can be NULL */
399 if (fp) {
400 fhold(fp);
401 if (fdp->fd_files[fd].isfull == 0)
402 goto enter;
404 } else {
405 fp = NULL;
407 spin_unlock_shared(&fdp->fd_spin);
409 return fp;
412 * We found a valid fp and held it, fdp is still shared locked.
413 * Enter the fp into the per-thread cache. Find the oldest entry
414 * via lru, or an empty entry.
416 * Because fdp's spinlock is held (shared is fine), no other
417 * thread should be in the middle of clearing our selected entry.
419 enter:
420 best = &td->td_fdcache[0];
421 for (fdc = &td->td_fdcache[0]; fdc < &td->td_fdcache[NFDCACHE]; ++fdc) {
422 if (fdc->fp == NULL) {
423 best = fdc;
424 break;
426 delta = fdc->lru - best->lru;
427 if (delta < 0)
428 best = fdc;
432 * Replace best
434 * Don't enter into the cache if we cannot get the lock.
436 status = atomic_swap_int(&best->locked, 1);
437 if (status == 1)
438 goto done;
441 * Clear the previous cache entry if present
443 if (best->fp) {
444 KKASSERT(best->fd >= 0);
445 fclearcache(&fdp->fd_files[best->fd], best, status);
449 * Create our new cache entry. This entry is 'safe' until we tie
450 * into the fdnode. If we cannot tie in, we will clear the entry.
452 best->fd = fd;
453 best->fp = fp;
454 best->lru = ++td->td_fdcache_lru;
455 best->locked = 2; /* borrowed ref */
457 fdn = &fdp->fd_files[fd];
458 for (i = 0; i < NTDCACHEFD; ++i) {
459 if (fdn->tdcache[i] == NULL &&
460 atomic_cmpset_ptr((void **)&fdn->tdcache[i], NULL, best)) {
461 goto done;
464 fdn->isfull = 1; /* no space */
465 best->fd = -1;
466 best->fp = NULL;
467 best->locked = 0;
468 done:
469 spin_unlock_shared(&fdp->fd_spin);
471 return fp;
475 * Drop the file pointer and return to the thread cache if possible.
477 * Caller must not hold fdp's spin lock.
478 * td must be the current thread.
480 void
481 dropfp(thread_t td, int fd, struct file *fp)
483 struct filedesc *fdp;
484 struct fdcache *fdc;
485 int status;
487 fdp = td->td_proc->p_fd;
490 * If our placeholder is still present we can re-cache the ref.
492 * Note that we can race an fclearcache().
494 for (fdc = &td->td_fdcache[0]; fdc < &td->td_fdcache[NFDCACHE]; ++fdc) {
495 if (fdc->fp != fp || fdc->fd != fd)
496 continue;
497 status = atomic_swap_int(&fdc->locked, 1);
498 switch(status) {
499 case 0:
501 * Not in mode 2, fdrop fp without caching.
503 atomic_swap_int(&fdc->locked, 0);
504 break;
505 case 1:
507 * Not in mode 2, locked by someone else.
508 * fdrop fp without caching.
510 break;
511 case 2:
513 * Intact borrowed ref, return to mode 0
514 * indicating that we have returned the ref.
516 * Return the borrowed ref (2->1->0)
518 if (fdc->fp == fp && fdc->fd == fd) {
519 atomic_swap_int(&fdc->locked, 0);
520 return;
522 atomic_swap_int(&fdc->locked, 2);
523 break;
528 * Failed to re-cache, drop the fp without caching.
530 fdrop(fp);
534 * Clear all descriptors cached in the per-thread fd cache for
535 * the specified thread.
537 * Caller must not hold p_fd->spin. This function will temporarily
538 * obtain a shared spin lock.
540 void
541 fexitcache(thread_t td)
543 struct filedesc *fdp;
544 struct fdcache *fdc;
545 int status;
546 int i;
548 if (td->td_proc == NULL)
549 return;
550 fdp = td->td_proc->p_fd;
551 if (fdp == NULL)
552 return;
555 * A shared lock is sufficient as the caller controls td and we
556 * are only clearing td's cache.
558 spin_lock_shared(&fdp->fd_spin);
559 for (i = 0; i < NFDCACHE; ++i) {
560 fdc = &td->td_fdcache[i];
561 if (fdc->fp) {
562 status = atomic_swap_int(&fdc->locked, 1);
563 if (status == 1) {
564 cpu_pause();
565 --i;
566 continue;
568 if (fdc->fp) {
569 KKASSERT(fdc->fd >= 0);
570 fclearcache(&fdp->fd_files[fdc->fd], fdc,
571 status);
573 atomic_swap_int(&fdc->locked, 0);
576 spin_unlock_shared(&fdp->fd_spin);
579 static __inline struct filelist_head *
580 fp2filelist(const struct file *fp)
582 u_int i;
584 i = (u_int)(uintptr_t)fp % NFILELIST_HEADS;
585 return &filelist_heads[i];
588 static __inline
589 struct plimit *
590 readplimits(struct proc *p)
592 thread_t td = curthread;
593 struct plimit *limit;
595 limit = td->td_limit;
596 if (limit != p->p_limit) {
597 spin_lock_shared(&p->p_spin);
598 limit = p->p_limit;
599 atomic_add_int(&limit->p_refcnt, 1);
600 spin_unlock_shared(&p->p_spin);
601 if (td->td_limit)
602 plimit_free(td->td_limit);
603 td->td_limit = limit;
605 return limit;
609 * System calls on descriptors.
612 sys_getdtablesize(struct getdtablesize_args *uap)
614 struct proc *p = curproc;
615 struct plimit *limit = readplimits(p);
616 int dtsize;
618 if (limit->pl_rlimit[RLIMIT_NOFILE].rlim_cur > INT_MAX)
619 dtsize = INT_MAX;
620 else
621 dtsize = (int)limit->pl_rlimit[RLIMIT_NOFILE].rlim_cur;
623 if (dtsize > maxfilesperproc)
624 dtsize = maxfilesperproc;
625 if (dtsize < minfilesperproc)
626 dtsize = minfilesperproc;
627 if (p->p_ucred->cr_uid && dtsize > maxfilesperuser)
628 dtsize = maxfilesperuser;
629 uap->sysmsg_result = dtsize;
630 return (0);
634 * Duplicate a file descriptor to a particular value.
636 * note: keep in mind that a potential race condition exists when closing
637 * descriptors from a shared descriptor table (via rfork).
640 sys_dup2(struct dup2_args *uap)
642 int error;
643 int fd = 0;
645 error = kern_dup(DUP_FIXED, uap->from, uap->to, &fd);
646 uap->sysmsg_fds[0] = fd;
648 return (error);
652 * Duplicate a file descriptor.
655 sys_dup(struct dup_args *uap)
657 int error;
658 int fd = 0;
660 error = kern_dup(DUP_VARIABLE, uap->fd, 0, &fd);
661 uap->sysmsg_fds[0] = fd;
663 return (error);
667 * MPALMOSTSAFE - acquires mplock for fp operations
670 kern_fcntl(int fd, int cmd, union fcntl_dat *dat, struct ucred *cred)
672 struct thread *td = curthread;
673 struct proc *p = td->td_proc;
674 struct file *fp;
675 struct vnode *vp;
676 u_int newmin;
677 u_int oflags;
678 u_int nflags;
679 int closedcounter;
680 int tmp, error, flg = F_POSIX;
682 KKASSERT(p);
685 * Operations on file descriptors that do not require a file pointer.
687 switch (cmd) {
688 case F_GETFD:
689 error = fgetfdflags(p->p_fd, fd, &tmp);
690 if (error == 0)
691 dat->fc_cloexec = (tmp & UF_EXCLOSE) ? FD_CLOEXEC : 0;
692 return (error);
694 case F_SETFD:
695 if (dat->fc_cloexec & FD_CLOEXEC)
696 error = fsetfdflags(p->p_fd, fd, UF_EXCLOSE);
697 else
698 error = fclrfdflags(p->p_fd, fd, UF_EXCLOSE);
699 return (error);
700 case F_DUPFD:
701 newmin = dat->fc_fd;
702 error = kern_dup(DUP_VARIABLE | DUP_FCNTL, fd, newmin,
703 &dat->fc_fd);
704 return (error);
705 case F_DUPFD_CLOEXEC:
706 newmin = dat->fc_fd;
707 error = kern_dup(DUP_VARIABLE | DUP_CLOEXEC | DUP_FCNTL,
708 fd, newmin, &dat->fc_fd);
709 return (error);
710 case F_DUP2FD:
711 newmin = dat->fc_fd;
712 error = kern_dup(DUP_FIXED, fd, newmin, &dat->fc_fd);
713 return (error);
714 case F_DUP2FD_CLOEXEC:
715 newmin = dat->fc_fd;
716 error = kern_dup(DUP_FIXED | DUP_CLOEXEC, fd, newmin,
717 &dat->fc_fd);
718 return (error);
719 default:
720 break;
724 * Operations on file pointers
726 closedcounter = p->p_fd->fd_closedcounter;
727 if ((fp = holdfp(td, fd, -1)) == NULL)
728 return (EBADF);
730 switch (cmd) {
731 case F_GETFL:
732 dat->fc_flags = OFLAGS(fp->f_flag);
733 error = 0;
734 break;
736 case F_SETFL:
737 oflags = fp->f_flag;
738 nflags = FFLAGS(dat->fc_flags & ~O_ACCMODE) & FCNTLFLAGS;
739 nflags |= oflags & ~FCNTLFLAGS;
741 error = 0;
742 if (((nflags ^ oflags) & O_APPEND) && (oflags & FAPPENDONLY))
743 error = EINVAL;
744 if (error == 0 && ((nflags ^ oflags) & FASYNC)) {
745 tmp = nflags & FASYNC;
746 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp,
747 cred, NULL);
751 * If no error, must be atomically set.
753 while (error == 0) {
754 oflags = fp->f_flag;
755 cpu_ccfence();
756 nflags = (oflags & ~FCNTLFLAGS) | (nflags & FCNTLFLAGS);
757 if (atomic_cmpset_int(&fp->f_flag, oflags, nflags))
758 break;
759 cpu_pause();
761 break;
763 case F_GETOWN:
764 error = fo_ioctl(fp, FIOGETOWN, (caddr_t)&dat->fc_owner,
765 cred, NULL);
766 break;
768 case F_SETOWN:
769 error = fo_ioctl(fp, FIOSETOWN, (caddr_t)&dat->fc_owner,
770 cred, NULL);
771 break;
773 case F_SETLKW:
774 flg |= F_WAIT;
775 /* Fall into F_SETLK */
777 case F_SETLK:
778 if (fp->f_type != DTYPE_VNODE) {
779 error = EBADF;
780 break;
782 vp = (struct vnode *)fp->f_data;
785 * copyin/lockop may block
787 if (dat->fc_flock.l_whence == SEEK_CUR)
788 dat->fc_flock.l_start += fp->f_offset;
790 switch (dat->fc_flock.l_type) {
791 case F_RDLCK:
792 if ((fp->f_flag & FREAD) == 0) {
793 error = EBADF;
794 break;
796 if (p->p_leader->p_advlock_flag == 0)
797 p->p_leader->p_advlock_flag = 1;
798 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
799 &dat->fc_flock, flg);
800 break;
801 case F_WRLCK:
802 if ((fp->f_flag & FWRITE) == 0) {
803 error = EBADF;
804 break;
806 if (p->p_leader->p_advlock_flag == 0)
807 p->p_leader->p_advlock_flag = 1;
808 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
809 &dat->fc_flock, flg);
810 break;
811 case F_UNLCK:
812 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
813 &dat->fc_flock, F_POSIX);
814 break;
815 default:
816 error = EINVAL;
817 break;
821 * It is possible to race a close() on the descriptor while
822 * we were blocked getting the lock. If this occurs the
823 * close might not have caught the lock.
825 if (checkfdclosed(td, p->p_fd, fd, fp, closedcounter)) {
826 dat->fc_flock.l_whence = SEEK_SET;
827 dat->fc_flock.l_start = 0;
828 dat->fc_flock.l_len = 0;
829 dat->fc_flock.l_type = F_UNLCK;
830 VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
831 F_UNLCK, &dat->fc_flock, F_POSIX);
833 break;
835 case F_GETLK:
836 if (fp->f_type != DTYPE_VNODE) {
837 error = EBADF;
838 break;
840 vp = (struct vnode *)fp->f_data;
842 * copyin/lockop may block
844 if (dat->fc_flock.l_type != F_RDLCK &&
845 dat->fc_flock.l_type != F_WRLCK &&
846 dat->fc_flock.l_type != F_UNLCK) {
847 error = EINVAL;
848 break;
850 if (dat->fc_flock.l_whence == SEEK_CUR)
851 dat->fc_flock.l_start += fp->f_offset;
852 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK,
853 &dat->fc_flock, F_POSIX);
854 break;
855 default:
856 error = EINVAL;
857 break;
860 fdrop(fp);
861 return (error);
865 * The file control system call.
868 sys_fcntl(struct fcntl_args *uap)
870 union fcntl_dat dat;
871 int error;
873 switch (uap->cmd) {
874 case F_DUPFD:
875 case F_DUP2FD:
876 case F_DUPFD_CLOEXEC:
877 case F_DUP2FD_CLOEXEC:
878 dat.fc_fd = uap->arg;
879 break;
880 case F_SETFD:
881 dat.fc_cloexec = uap->arg;
882 break;
883 case F_SETFL:
884 dat.fc_flags = uap->arg;
885 break;
886 case F_SETOWN:
887 dat.fc_owner = uap->arg;
888 break;
889 case F_SETLKW:
890 case F_SETLK:
891 case F_GETLK:
892 error = copyin((caddr_t)uap->arg, &dat.fc_flock,
893 sizeof(struct flock));
894 if (error)
895 return (error);
896 break;
899 error = kern_fcntl(uap->fd, uap->cmd, &dat, curthread->td_ucred);
901 if (error == 0) {
902 switch (uap->cmd) {
903 case F_DUPFD:
904 case F_DUP2FD:
905 case F_DUPFD_CLOEXEC:
906 case F_DUP2FD_CLOEXEC:
907 uap->sysmsg_result = dat.fc_fd;
908 break;
909 case F_GETFD:
910 uap->sysmsg_result = dat.fc_cloexec;
911 break;
912 case F_GETFL:
913 uap->sysmsg_result = dat.fc_flags;
914 break;
915 case F_GETOWN:
916 uap->sysmsg_result = dat.fc_owner;
917 break;
918 case F_GETLK:
919 error = copyout(&dat.fc_flock, (caddr_t)uap->arg,
920 sizeof(struct flock));
921 break;
925 return (error);
929 * Common code for dup, dup2, and fcntl(F_DUPFD).
931 * There are four type flags: DUP_FCNTL, DUP_FIXED, DUP_VARIABLE, and
932 * DUP_CLOEXEC.
934 * DUP_FCNTL is for handling EINVAL vs. EBADF differences between
935 * fcntl()'s F_DUPFD and F_DUPFD_CLOEXEC and dup2() (per POSIX).
936 * The next two flags are mutually exclusive, and the fourth is optional.
937 * DUP_FIXED tells kern_dup() to destructively dup over an existing file
938 * descriptor if "new" is already open. DUP_VARIABLE tells kern_dup()
939 * to find the lowest unused file descriptor that is greater than or
940 * equal to "new". DUP_CLOEXEC, which works with either of the first
941 * two flags, sets the close-on-exec flag on the "new" file descriptor.
944 kern_dup(int flags, int old, int new, int *res)
946 struct thread *td = curthread;
947 struct proc *p = td->td_proc;
948 struct plimit *limit = readplimits(p);
949 struct filedesc *fdp = p->p_fd;
950 struct file *fp;
951 struct file *delfp;
952 int oldflags;
953 int holdleaders;
954 int dtsize;
955 int error, newfd;
958 * Verify that we have a valid descriptor to dup from and
959 * possibly to dup to. When the new descriptor is out of
960 * bounds, fcntl()'s F_DUPFD and F_DUPFD_CLOEXEC must
961 * return EINVAL, while dup2() returns EBADF in
962 * this case.
964 * NOTE: maxfilesperuser is not applicable to dup()
966 retry:
967 if (limit->pl_rlimit[RLIMIT_NOFILE].rlim_cur > INT_MAX)
968 dtsize = INT_MAX;
969 else
970 dtsize = (int)limit->pl_rlimit[RLIMIT_NOFILE].rlim_cur;
971 if (dtsize > maxfilesperproc)
972 dtsize = maxfilesperproc;
973 if (dtsize < minfilesperproc)
974 dtsize = minfilesperproc;
976 if (new < 0 || new > dtsize)
977 return (flags & DUP_FCNTL ? EINVAL : EBADF);
979 spin_lock(&fdp->fd_spin);
980 if ((unsigned)old >= fdp->fd_nfiles || fdp->fd_files[old].fp == NULL) {
981 spin_unlock(&fdp->fd_spin);
982 return (EBADF);
984 if ((flags & DUP_FIXED) && old == new) {
985 *res = new;
986 if (flags & DUP_CLOEXEC)
987 fdp->fd_files[new].fileflags |= UF_EXCLOSE;
988 spin_unlock(&fdp->fd_spin);
989 return (0);
991 fp = fdp->fd_files[old].fp;
992 oldflags = fdp->fd_files[old].fileflags;
993 fhold(fp);
996 * Allocate a new descriptor if DUP_VARIABLE, or expand the table
997 * if the requested descriptor is beyond the current table size.
999 * This can block. Retry if the source descriptor no longer matches
1000 * or if our expectation in the expansion case races.
1002 * If we are not expanding or allocating a new decriptor, then reset
1003 * the target descriptor to a reserved state so we have a uniform
1004 * setup for the next code block.
1006 if ((flags & DUP_VARIABLE) || new >= fdp->fd_nfiles) {
1007 spin_unlock(&fdp->fd_spin);
1008 error = fdalloc(p, new, &newfd);
1009 spin_lock(&fdp->fd_spin);
1010 if (error) {
1011 spin_unlock(&fdp->fd_spin);
1012 fdrop(fp);
1013 return (error);
1016 * Check for ripout
1018 if (old >= fdp->fd_nfiles || fdp->fd_files[old].fp != fp) {
1019 fsetfd_locked(fdp, NULL, newfd);
1020 spin_unlock(&fdp->fd_spin);
1021 fdrop(fp);
1022 goto retry;
1025 * Check for expansion race
1027 if ((flags & DUP_VARIABLE) == 0 && new != newfd) {
1028 fsetfd_locked(fdp, NULL, newfd);
1029 spin_unlock(&fdp->fd_spin);
1030 fdrop(fp);
1031 goto retry;
1034 * Check for ripout, newfd reused old (this case probably
1035 * can't occur).
1037 if (old == newfd) {
1038 fsetfd_locked(fdp, NULL, newfd);
1039 spin_unlock(&fdp->fd_spin);
1040 fdrop(fp);
1041 goto retry;
1043 new = newfd;
1044 delfp = NULL;
1045 } else {
1046 if (fdp->fd_files[new].reserved) {
1047 spin_unlock(&fdp->fd_spin);
1048 fdrop(fp);
1049 kprintf("Warning: dup(): target descriptor %d is reserved, waiting for it to be resolved\n", new);
1050 tsleep(fdp, 0, "fdres", hz);
1051 goto retry;
1055 * If the target descriptor was never allocated we have
1056 * to allocate it. If it was we have to clean out the
1057 * old descriptor. delfp inherits the ref from the
1058 * descriptor table.
1060 ++fdp->fd_closedcounter;
1061 fclearcache(&fdp->fd_files[new], NULL, 0);
1062 ++fdp->fd_closedcounter;
1063 delfp = fdp->fd_files[new].fp;
1064 fdp->fd_files[new].fp = NULL;
1065 fdp->fd_files[new].reserved = 1;
1066 if (delfp == NULL) {
1067 fdreserve_locked(fdp, new, 1);
1068 if (new > fdp->fd_lastfile)
1069 fdp->fd_lastfile = new;
1075 * NOTE: still holding an exclusive spinlock
1079 * If a descriptor is being overwritten we may hve to tell
1080 * fdfree() to sleep to ensure that all relevant process
1081 * leaders can be traversed in closef().
1083 if (delfp != NULL && p->p_fdtol != NULL) {
1084 fdp->fd_holdleaderscount++;
1085 holdleaders = 1;
1086 } else {
1087 holdleaders = 0;
1089 KASSERT(delfp == NULL || (flags & DUP_FIXED),
1090 ("dup() picked an open file"));
1093 * Duplicate the source descriptor, update lastfile. If the new
1094 * descriptor was not allocated and we aren't replacing an existing
1095 * descriptor we have to mark the descriptor as being in use.
1097 * The fd_files[] array inherits fp's hold reference.
1099 fsetfd_locked(fdp, fp, new);
1100 if ((flags & DUP_CLOEXEC) != 0)
1101 fdp->fd_files[new].fileflags = oldflags | UF_EXCLOSE;
1102 else
1103 fdp->fd_files[new].fileflags = oldflags & ~UF_EXCLOSE;
1104 spin_unlock(&fdp->fd_spin);
1105 fdrop(fp);
1106 *res = new;
1109 * If we dup'd over a valid file, we now own the reference to it
1110 * and must dispose of it using closef() semantics (as if a
1111 * close() were performed on it).
1113 if (delfp) {
1114 if (SLIST_FIRST(&delfp->f_klist))
1115 knote_fdclose(delfp, fdp, new);
1116 closef(delfp, p);
1117 if (holdleaders) {
1118 spin_lock(&fdp->fd_spin);
1119 fdp->fd_holdleaderscount--;
1120 if (fdp->fd_holdleaderscount == 0 &&
1121 fdp->fd_holdleaderswakeup != 0) {
1122 fdp->fd_holdleaderswakeup = 0;
1123 spin_unlock(&fdp->fd_spin);
1124 wakeup(&fdp->fd_holdleaderscount);
1125 } else {
1126 spin_unlock(&fdp->fd_spin);
1130 return (0);
1134 * If sigio is on the list associated with a process or process group,
1135 * disable signalling from the device, remove sigio from the list and
1136 * free sigio.
1138 void
1139 funsetown(struct sigio **sigiop)
1141 struct pgrp *pgrp;
1142 struct proc *p;
1143 struct sigio *sigio;
1145 if ((sigio = *sigiop) != NULL) {
1146 lwkt_gettoken(&sigio_token); /* protect sigio */
1147 KKASSERT(sigiop == sigio->sio_myref);
1148 sigio = *sigiop;
1149 *sigiop = NULL;
1150 lwkt_reltoken(&sigio_token);
1152 if (sigio == NULL)
1153 return;
1155 if (sigio->sio_pgid < 0) {
1156 pgrp = sigio->sio_pgrp;
1157 sigio->sio_pgrp = NULL;
1158 lwkt_gettoken(&pgrp->pg_token);
1159 SLIST_REMOVE(&pgrp->pg_sigiolst, sigio, sigio, sio_pgsigio);
1160 lwkt_reltoken(&pgrp->pg_token);
1161 pgrel(pgrp);
1162 } else /* if ((*sigiop)->sio_pgid > 0) */ {
1163 p = sigio->sio_proc;
1164 sigio->sio_proc = NULL;
1165 PHOLD(p);
1166 lwkt_gettoken(&p->p_token);
1167 SLIST_REMOVE(&p->p_sigiolst, sigio, sigio, sio_pgsigio);
1168 lwkt_reltoken(&p->p_token);
1169 PRELE(p);
1171 crfree(sigio->sio_ucred);
1172 sigio->sio_ucred = NULL;
1173 kfree(sigio, M_SIGIO);
1177 * Free a list of sigio structures. Caller is responsible for ensuring
1178 * that the list is MPSAFE.
1180 void
1181 funsetownlst(struct sigiolst *sigiolst)
1183 struct sigio *sigio;
1185 while ((sigio = SLIST_FIRST(sigiolst)) != NULL)
1186 funsetown(sigio->sio_myref);
1190 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
1192 * After permission checking, add a sigio structure to the sigio list for
1193 * the process or process group.
1196 fsetown(pid_t pgid, struct sigio **sigiop)
1198 struct proc *proc = NULL;
1199 struct pgrp *pgrp = NULL;
1200 struct sigio *sigio;
1201 int error;
1203 if (pgid == 0) {
1204 funsetown(sigiop);
1205 return (0);
1208 if (pgid > 0) {
1209 proc = pfind(pgid);
1210 if (proc == NULL) {
1211 error = ESRCH;
1212 goto done;
1216 * Policy - Don't allow a process to FSETOWN a process
1217 * in another session.
1219 * Remove this test to allow maximum flexibility or
1220 * restrict FSETOWN to the current process or process
1221 * group for maximum safety.
1223 if (proc->p_session != curproc->p_session) {
1224 error = EPERM;
1225 goto done;
1227 } else /* if (pgid < 0) */ {
1228 pgrp = pgfind(-pgid);
1229 if (pgrp == NULL) {
1230 error = ESRCH;
1231 goto done;
1235 * Policy - Don't allow a process to FSETOWN a process
1236 * in another session.
1238 * Remove this test to allow maximum flexibility or
1239 * restrict FSETOWN to the current process or process
1240 * group for maximum safety.
1242 if (pgrp->pg_session != curproc->p_session) {
1243 error = EPERM;
1244 goto done;
1247 sigio = kmalloc(sizeof(struct sigio), M_SIGIO, M_WAITOK | M_ZERO);
1248 if (pgid > 0) {
1249 KKASSERT(pgrp == NULL);
1250 lwkt_gettoken(&proc->p_token);
1251 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
1252 sigio->sio_proc = proc;
1253 lwkt_reltoken(&proc->p_token);
1254 } else {
1255 KKASSERT(proc == NULL);
1256 lwkt_gettoken(&pgrp->pg_token);
1257 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
1258 sigio->sio_pgrp = pgrp;
1259 lwkt_reltoken(&pgrp->pg_token);
1260 pgrp = NULL;
1262 sigio->sio_pgid = pgid;
1263 sigio->sio_ucred = crhold(curthread->td_ucred);
1264 /* It would be convenient if p_ruid was in ucred. */
1265 sigio->sio_ruid = sigio->sio_ucred->cr_ruid;
1266 sigio->sio_myref = sigiop;
1268 lwkt_gettoken(&sigio_token);
1269 while (*sigiop)
1270 funsetown(sigiop);
1271 *sigiop = sigio;
1272 lwkt_reltoken(&sigio_token);
1273 error = 0;
1274 done:
1275 if (pgrp)
1276 pgrel(pgrp);
1277 if (proc)
1278 PRELE(proc);
1279 return (error);
1283 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
1285 pid_t
1286 fgetown(struct sigio **sigiop)
1288 struct sigio *sigio;
1289 pid_t own;
1291 lwkt_gettoken_shared(&sigio_token);
1292 sigio = *sigiop;
1293 own = (sigio != NULL ? sigio->sio_pgid : 0);
1294 lwkt_reltoken(&sigio_token);
1296 return (own);
1300 * Close many file descriptors.
1303 sys_closefrom(struct closefrom_args *uap)
1305 return(kern_closefrom(uap->fd));
1309 * Close all file descriptors greater then or equal to fd
1312 kern_closefrom(int fd)
1314 struct thread *td = curthread;
1315 struct proc *p = td->td_proc;
1316 struct filedesc *fdp;
1318 KKASSERT(p);
1319 fdp = p->p_fd;
1321 if (fd < 0)
1322 return (EINVAL);
1325 * NOTE: This function will skip unassociated descriptors and
1326 * reserved descriptors that have not yet been assigned.
1327 * fd_lastfile can change as a side effect of kern_close().
1329 spin_lock(&fdp->fd_spin);
1330 while (fd <= fdp->fd_lastfile) {
1331 if (fdp->fd_files[fd].fp != NULL) {
1332 spin_unlock(&fdp->fd_spin);
1333 /* ok if this races another close */
1334 if (kern_close(fd) == EINTR)
1335 return (EINTR);
1336 spin_lock(&fdp->fd_spin);
1338 ++fd;
1340 spin_unlock(&fdp->fd_spin);
1341 return (0);
1345 * Close a file descriptor.
1348 sys_close(struct close_args *uap)
1350 return(kern_close(uap->fd));
1354 * close() helper
1357 kern_close(int fd)
1359 struct thread *td = curthread;
1360 struct proc *p = td->td_proc;
1361 struct filedesc *fdp;
1362 struct file *fp;
1363 int error;
1364 int holdleaders;
1366 KKASSERT(p);
1367 fdp = p->p_fd;
1370 * funsetfd*() also clears the fd cache
1372 spin_lock(&fdp->fd_spin);
1373 if ((fp = funsetfd_locked(fdp, fd)) == NULL) {
1374 spin_unlock(&fdp->fd_spin);
1375 return (EBADF);
1377 holdleaders = 0;
1378 if (p->p_fdtol != NULL) {
1380 * Ask fdfree() to sleep to ensure that all relevant
1381 * process leaders can be traversed in closef().
1383 fdp->fd_holdleaderscount++;
1384 holdleaders = 1;
1388 * we now hold the fp reference that used to be owned by the descriptor
1389 * array.
1391 spin_unlock(&fdp->fd_spin);
1392 if (SLIST_FIRST(&fp->f_klist))
1393 knote_fdclose(fp, fdp, fd);
1394 error = closef(fp, p);
1395 if (holdleaders) {
1396 spin_lock(&fdp->fd_spin);
1397 fdp->fd_holdleaderscount--;
1398 if (fdp->fd_holdleaderscount == 0 &&
1399 fdp->fd_holdleaderswakeup != 0) {
1400 fdp->fd_holdleaderswakeup = 0;
1401 spin_unlock(&fdp->fd_spin);
1402 wakeup(&fdp->fd_holdleaderscount);
1403 } else {
1404 spin_unlock(&fdp->fd_spin);
1407 return (error);
1411 * shutdown_args(int fd, int how)
1414 kern_shutdown(int fd, int how)
1416 struct thread *td = curthread;
1417 struct file *fp;
1418 int error;
1420 if ((fp = holdfp(td, fd, -1)) == NULL)
1421 return (EBADF);
1422 error = fo_shutdown(fp, how);
1423 fdrop(fp);
1425 return (error);
1429 * MPALMOSTSAFE
1432 sys_shutdown(struct shutdown_args *uap)
1434 int error;
1436 error = kern_shutdown(uap->s, uap->how);
1438 return (error);
1442 * fstat() helper
1445 kern_fstat(int fd, struct stat *ub)
1447 struct thread *td = curthread;
1448 struct file *fp;
1449 int error;
1451 if ((fp = holdfp(td, fd, -1)) == NULL)
1452 return (EBADF);
1453 error = fo_stat(fp, ub, td->td_ucred);
1454 fdrop(fp);
1456 return (error);
1460 * Return status information about a file descriptor.
1463 sys_fstat(struct fstat_args *uap)
1465 struct stat st;
1466 int error;
1468 error = kern_fstat(uap->fd, &st);
1470 if (error == 0)
1471 error = copyout(&st, uap->sb, sizeof(st));
1472 return (error);
1476 * Return pathconf information about a file descriptor.
1478 * MPALMOSTSAFE
1481 sys_fpathconf(struct fpathconf_args *uap)
1483 struct thread *td = curthread;
1484 struct file *fp;
1485 struct vnode *vp;
1486 int error = 0;
1488 if ((fp = holdfp(td, uap->fd, -1)) == NULL)
1489 return (EBADF);
1491 switch (fp->f_type) {
1492 case DTYPE_PIPE:
1493 case DTYPE_SOCKET:
1494 if (uap->name != _PC_PIPE_BUF) {
1495 error = EINVAL;
1496 } else {
1497 uap->sysmsg_result = PIPE_BUF;
1498 error = 0;
1500 break;
1501 case DTYPE_FIFO:
1502 case DTYPE_VNODE:
1503 vp = (struct vnode *)fp->f_data;
1504 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
1505 break;
1506 default:
1507 error = EOPNOTSUPP;
1508 break;
1510 fdrop(fp);
1511 return(error);
1515 * Grow the file table so it can hold through descriptor (want).
1517 * The fdp's spinlock must be held exclusively on entry and may be held
1518 * exclusively on return. The spinlock may be cycled by the routine.
1520 static void
1521 fdgrow_locked(struct filedesc *fdp, int want)
1523 struct fdnode *newfiles;
1524 struct fdnode *oldfiles;
1525 int nf, extra;
1527 nf = fdp->fd_nfiles;
1528 do {
1529 /* nf has to be of the form 2^n - 1 */
1530 nf = 2 * nf + 1;
1531 } while (nf <= want);
1533 spin_unlock(&fdp->fd_spin);
1534 newfiles = kmalloc(nf * sizeof(struct fdnode), M_FILEDESC, M_WAITOK);
1535 spin_lock(&fdp->fd_spin);
1538 * We could have raced another extend while we were not holding
1539 * the spinlock.
1541 if (fdp->fd_nfiles >= nf) {
1542 spin_unlock(&fdp->fd_spin);
1543 kfree(newfiles, M_FILEDESC);
1544 spin_lock(&fdp->fd_spin);
1545 return;
1548 * Copy the existing ofile and ofileflags arrays
1549 * and zero the new portion of each array.
1551 extra = nf - fdp->fd_nfiles;
1552 bcopy(fdp->fd_files, newfiles, fdp->fd_nfiles * sizeof(struct fdnode));
1553 bzero(&newfiles[fdp->fd_nfiles], extra * sizeof(struct fdnode));
1555 oldfiles = fdp->fd_files;
1556 fdp->fd_files = newfiles;
1557 fdp->fd_nfiles = nf;
1559 if (oldfiles != fdp->fd_builtin_files) {
1560 spin_unlock(&fdp->fd_spin);
1561 kfree(oldfiles, M_FILEDESC);
1562 spin_lock(&fdp->fd_spin);
1567 * Number of nodes in right subtree, including the root.
1569 static __inline int
1570 right_subtree_size(int n)
1572 return (n ^ (n | (n + 1)));
1576 * Bigger ancestor.
1578 static __inline int
1579 right_ancestor(int n)
1581 return (n | (n + 1));
1585 * Smaller ancestor.
1587 static __inline int
1588 left_ancestor(int n)
1590 return ((n & (n + 1)) - 1);
1594 * Traverse the in-place binary tree buttom-up adjusting the allocation
1595 * count so scans can determine where free descriptors are located.
1597 * caller must be holding an exclusive spinlock on fdp
1599 static
1600 void
1601 fdreserve_locked(struct filedesc *fdp, int fd, int incr)
1603 while (fd >= 0) {
1604 fdp->fd_files[fd].allocated += incr;
1605 KKASSERT(fdp->fd_files[fd].allocated >= 0);
1606 fd = left_ancestor(fd);
1611 * Reserve a file descriptor for the process. If no error occurs, the
1612 * caller MUST at some point call fsetfd() or assign a file pointer
1613 * or dispose of the reservation.
1616 fdalloc(struct proc *p, int want, int *result)
1618 struct plimit *limit = readplimits(p);
1619 struct filedesc *fdp = p->p_fd;
1620 struct uidinfo *uip;
1621 int fd, rsize, rsum, node, lim;
1624 * Check dtable size limit
1626 *result = -1; /* avoid gcc warnings */
1627 if (limit->pl_rlimit[RLIMIT_NOFILE].rlim_cur > INT_MAX)
1628 lim = INT_MAX;
1629 else
1630 lim = (int)limit->pl_rlimit[RLIMIT_NOFILE].rlim_cur;
1632 if (lim > maxfilesperproc)
1633 lim = maxfilesperproc;
1634 if (lim < minfilesperproc)
1635 lim = minfilesperproc;
1636 if (want >= lim)
1637 return (EMFILE);
1640 * Check that the user has not run out of descriptors (non-root only).
1641 * As a safety measure the dtable is allowed to have at least
1642 * minfilesperproc open fds regardless of the maxfilesperuser limit.
1644 * This isn't as loose a spec as ui_posixlocks, so we use atomic
1645 * ops to force synchronize and recheck if we would otherwise
1646 * error.
1648 if (p->p_ucred->cr_uid && fdp->fd_nfiles >= minfilesperproc) {
1649 uip = p->p_ucred->cr_uidinfo;
1650 if (uip->ui_openfiles > maxfilesperuser) {
1651 int n;
1652 int count;
1654 for (n = 0; n < ncpus; ++n) {
1655 count = atomic_swap_int(
1656 &uip->ui_pcpu[n].pu_openfiles, 0);
1657 atomic_add_int(&uip->ui_openfiles, count);
1659 if (uip->ui_openfiles > maxfilesperuser) {
1660 krateprintf(&krate_uidinfo,
1661 "Warning: user %d pid %d (%s) "
1662 "ran out of file descriptors "
1663 "(%d/%d)\n",
1664 p->p_ucred->cr_uid, (int)p->p_pid,
1665 p->p_comm,
1666 uip->ui_openfiles, maxfilesperuser);
1667 return(ENFILE);
1673 * Grow the dtable if necessary
1675 spin_lock(&fdp->fd_spin);
1676 if (want >= fdp->fd_nfiles)
1677 fdgrow_locked(fdp, want);
1680 * Search for a free descriptor starting at the higher
1681 * of want or fd_freefile. If that fails, consider
1682 * expanding the ofile array.
1684 * NOTE! the 'allocated' field is a cumulative recursive allocation
1685 * count. If we happen to see a value of 0 then we can shortcut
1686 * our search. Otherwise we run through through the tree going
1687 * down branches we know have free descriptor(s) until we hit a
1688 * leaf node. The leaf node will be free but will not necessarily
1689 * have an allocated field of 0.
1691 retry:
1692 /* move up the tree looking for a subtree with a free node */
1693 for (fd = max(want, fdp->fd_freefile); fd < min(fdp->fd_nfiles, lim);
1694 fd = right_ancestor(fd)) {
1695 if (fdp->fd_files[fd].allocated == 0)
1696 goto found;
1698 rsize = right_subtree_size(fd);
1699 if (fdp->fd_files[fd].allocated == rsize)
1700 continue; /* right subtree full */
1703 * Free fd is in the right subtree of the tree rooted at fd.
1704 * Call that subtree R. Look for the smallest (leftmost)
1705 * subtree of R with an unallocated fd: continue moving
1706 * down the left branch until encountering a full left
1707 * subtree, then move to the right.
1709 for (rsum = 0, rsize /= 2; rsize > 0; rsize /= 2) {
1710 node = fd + rsize;
1711 rsum += fdp->fd_files[node].allocated;
1712 if (fdp->fd_files[fd].allocated == rsum + rsize) {
1713 fd = node; /* move to the right */
1714 if (fdp->fd_files[node].allocated == 0)
1715 goto found;
1716 rsum = 0;
1719 goto found;
1723 * No space in current array. Expand?
1725 if (fdp->fd_nfiles >= lim) {
1726 spin_unlock(&fdp->fd_spin);
1727 return (EMFILE);
1729 fdgrow_locked(fdp, want);
1730 goto retry;
1732 found:
1733 KKASSERT(fd < fdp->fd_nfiles);
1734 if (fd > fdp->fd_lastfile)
1735 fdp->fd_lastfile = fd;
1736 if (want <= fdp->fd_freefile)
1737 fdp->fd_freefile = fd;
1738 *result = fd;
1739 KKASSERT(fdp->fd_files[fd].fp == NULL);
1740 KKASSERT(fdp->fd_files[fd].reserved == 0);
1741 fdp->fd_files[fd].fileflags = 0;
1742 fdp->fd_files[fd].reserved = 1;
1743 fdreserve_locked(fdp, fd, 1);
1744 spin_unlock(&fdp->fd_spin);
1745 return (0);
1749 * Check to see whether n user file descriptors
1750 * are available to the process p.
1753 fdavail(struct proc *p, int n)
1755 struct plimit *limit = readplimits(p);
1756 struct filedesc *fdp = p->p_fd;
1757 struct fdnode *fdnode;
1758 int i, lim, last;
1760 if (limit->pl_rlimit[RLIMIT_NOFILE].rlim_cur > INT_MAX)
1761 lim = INT_MAX;
1762 else
1763 lim = (int)limit->pl_rlimit[RLIMIT_NOFILE].rlim_cur;
1765 if (lim > maxfilesperproc)
1766 lim = maxfilesperproc;
1767 if (lim < minfilesperproc)
1768 lim = minfilesperproc;
1770 spin_lock(&fdp->fd_spin);
1771 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0) {
1772 spin_unlock(&fdp->fd_spin);
1773 return (1);
1775 last = min(fdp->fd_nfiles, lim);
1776 fdnode = &fdp->fd_files[fdp->fd_freefile];
1777 for (i = last - fdp->fd_freefile; --i >= 0; ++fdnode) {
1778 if (fdnode->fp == NULL && --n <= 0) {
1779 spin_unlock(&fdp->fd_spin);
1780 return (1);
1783 spin_unlock(&fdp->fd_spin);
1784 return (0);
1788 * Revoke open descriptors referencing (f_data, f_type)
1790 * Any revoke executed within a prison is only able to
1791 * revoke descriptors for processes within that prison.
1793 * Returns 0 on success or an error code.
1795 struct fdrevoke_info {
1796 void *data;
1797 short type;
1798 short unused;
1799 int found;
1800 struct ucred *cred;
1801 struct file *nfp;
1804 static int fdrevoke_check_callback(struct file *fp, void *vinfo);
1805 static int fdrevoke_proc_callback(struct proc *p, void *vinfo);
1808 fdrevoke(void *f_data, short f_type, struct ucred *cred)
1810 struct fdrevoke_info info;
1811 int error;
1813 bzero(&info, sizeof(info));
1814 info.data = f_data;
1815 info.type = f_type;
1816 info.cred = cred;
1817 error = falloc(NULL, &info.nfp, NULL);
1818 if (error)
1819 return (error);
1822 * Scan the file pointer table once. dups do not dup file pointers,
1823 * only descriptors, so there is no leak. Set FREVOKED on the fps
1824 * being revoked.
1826 * Any fps sent over unix-domain sockets will be revoked by the
1827 * socket code checking for FREVOKED when the fps are externialized.
1828 * revoke_token is used to make sure that fps marked FREVOKED and
1829 * externalized will be picked up by the following allproc_scan().
1831 lwkt_gettoken(&revoke_token);
1832 allfiles_scan_exclusive(fdrevoke_check_callback, &info);
1833 lwkt_reltoken(&revoke_token);
1836 * If any fps were marked track down the related descriptors
1837 * and close them. Any dup()s at this point will notice
1838 * the FREVOKED already set in the fp and do the right thing.
1840 if (info.found)
1841 allproc_scan(fdrevoke_proc_callback, &info, 0);
1842 fdrop(info.nfp);
1843 return(0);
1847 * Locate matching file pointers directly.
1849 * WARNING: allfiles_scan_exclusive() holds a spinlock through these calls!
1851 static int
1852 fdrevoke_check_callback(struct file *fp, void *vinfo)
1854 struct fdrevoke_info *info = vinfo;
1857 * File pointers already flagged for revokation are skipped.
1859 if (fp->f_flag & FREVOKED)
1860 return(0);
1863 * If revoking from a prison file pointers created outside of
1864 * that prison, or file pointers without creds, cannot be revoked.
1866 if (info->cred->cr_prison &&
1867 (fp->f_cred == NULL ||
1868 info->cred->cr_prison != fp->f_cred->cr_prison)) {
1869 return(0);
1873 * If the file pointer matches then mark it for revocation. The
1874 * flag is currently only used by unp_revoke_gc().
1876 * info->found is a heuristic and can race in a SMP environment.
1878 if (info->data == fp->f_data && info->type == fp->f_type) {
1879 atomic_set_int(&fp->f_flag, FREVOKED);
1880 info->found = 1;
1882 return(0);
1886 * Locate matching file pointers via process descriptor tables.
1888 static int
1889 fdrevoke_proc_callback(struct proc *p, void *vinfo)
1891 struct fdrevoke_info *info = vinfo;
1892 struct filedesc *fdp;
1893 struct file *fp;
1894 int n;
1896 if (p->p_stat == SIDL || p->p_stat == SZOMB)
1897 return(0);
1898 if (info->cred->cr_prison &&
1899 info->cred->cr_prison != p->p_ucred->cr_prison) {
1900 return(0);
1904 * If the controlling terminal of the process matches the
1905 * vnode being revoked we clear the controlling terminal.
1907 * The normal spec_close() may not catch this because it
1908 * uses curproc instead of p.
1910 if (p->p_session && info->type == DTYPE_VNODE &&
1911 info->data == p->p_session->s_ttyvp) {
1912 p->p_session->s_ttyvp = NULL;
1913 vrele(info->data);
1917 * Softref the fdp to prevent it from being destroyed
1919 spin_lock(&p->p_spin);
1920 if ((fdp = p->p_fd) == NULL) {
1921 spin_unlock(&p->p_spin);
1922 return(0);
1924 atomic_add_int(&fdp->fd_softrefs, 1);
1925 spin_unlock(&p->p_spin);
1928 * Locate and close any matching file descriptors, replacing
1929 * them with info->nfp.
1931 spin_lock(&fdp->fd_spin);
1932 for (n = 0; n < fdp->fd_nfiles; ++n) {
1933 if ((fp = fdp->fd_files[n].fp) == NULL)
1934 continue;
1935 if (fp->f_flag & FREVOKED) {
1936 ++fdp->fd_closedcounter;
1937 fclearcache(&fdp->fd_files[n], NULL, 0);
1938 ++fdp->fd_closedcounter;
1939 fhold(info->nfp);
1940 fdp->fd_files[n].fp = info->nfp;
1941 spin_unlock(&fdp->fd_spin);
1942 knote_fdclose(fp, fdp, n); /* XXX */
1943 closef(fp, p);
1944 spin_lock(&fdp->fd_spin);
1947 spin_unlock(&fdp->fd_spin);
1948 atomic_subtract_int(&fdp->fd_softrefs, 1);
1949 return(0);
1953 * falloc:
1954 * Create a new open file structure and reserve a file decriptor
1955 * for the process that refers to it.
1957 * Root creds are checked using lp, or assumed if lp is NULL. If
1958 * resultfd is non-NULL then lp must also be non-NULL. No file
1959 * descriptor is reserved (and no process context is needed) if
1960 * resultfd is NULL.
1962 * A file pointer with a refcount of 1 is returned. Note that the
1963 * file pointer is NOT associated with the descriptor. If falloc
1964 * returns success, fsetfd() MUST be called to either associate the
1965 * file pointer or clear the reservation.
1968 falloc(struct lwp *lp, struct file **resultfp, int *resultfd)
1970 static struct timeval lastfail;
1971 static int curfail;
1972 struct filelist_head *head;
1973 struct file *fp;
1974 struct ucred *cred = lp ? lp->lwp_thread->td_ucred : proc0.p_ucred;
1975 int error;
1977 fp = NULL;
1980 * Handle filetable full issues and root overfill.
1982 if (nfiles >= maxfiles - maxfilesrootres &&
1983 (cred->cr_ruid != 0 || nfiles >= maxfiles)) {
1984 if (ppsratecheck(&lastfail, &curfail, 1)) {
1985 kprintf("kern.maxfiles limit exceeded by uid %d, "
1986 "please see tuning(7).\n",
1987 cred->cr_ruid);
1989 error = ENFILE;
1990 goto done;
1994 * Allocate a new file descriptor.
1996 fp = objcache_get(file_objcache, M_WAITOK);
1997 bzero(fp, sizeof(*fp));
1998 spin_init(&fp->f_spin, "falloc");
1999 SLIST_INIT(&fp->f_klist);
2000 fp->f_count = 1;
2001 fp->f_ops = &badfileops;
2002 fp->f_seqcount = 1;
2003 fsetcred(fp, cred);
2004 atomic_add_int(&nfiles, 1);
2006 head = fp2filelist(fp);
2007 spin_lock(&head->spin);
2008 LIST_INSERT_HEAD(&head->list, fp, f_list);
2009 spin_unlock(&head->spin);
2011 if (resultfd) {
2012 if ((error = fdalloc(lp->lwp_proc, 0, resultfd)) != 0) {
2013 fdrop(fp);
2014 fp = NULL;
2016 } else {
2017 error = 0;
2019 done:
2020 *resultfp = fp;
2021 return (error);
2025 * Check for races against a file descriptor by determining that the
2026 * file pointer is still associated with the specified file descriptor,
2027 * and a close is not currently in progress.
2030 checkfdclosed(thread_t td, struct filedesc *fdp, int fd, struct file *fp,
2031 int closedcounter)
2033 struct fdcache *fdc;
2034 int error;
2036 cpu_lfence();
2037 if (fdp->fd_closedcounter == closedcounter)
2038 return 0;
2040 if (td->td_proc && td->td_proc->p_fd == fdp) {
2041 for (fdc = &td->td_fdcache[0];
2042 fdc < &td->td_fdcache[NFDCACHE]; ++fdc) {
2043 if (fdc->fd == fd && fdc->fp == fp)
2044 return 0;
2048 spin_lock_shared(&fdp->fd_spin);
2049 if ((unsigned)fd >= fdp->fd_nfiles || fp != fdp->fd_files[fd].fp)
2050 error = EBADF;
2051 else
2052 error = 0;
2053 spin_unlock_shared(&fdp->fd_spin);
2054 return (error);
2058 * Associate a file pointer with a previously reserved file descriptor.
2059 * This function always succeeds.
2061 * If fp is NULL, the file descriptor is returned to the pool.
2063 * Caller must hold an exclusive spinlock on fdp->fd_spin.
2065 static void
2066 fsetfd_locked(struct filedesc *fdp, struct file *fp, int fd)
2068 KKASSERT((unsigned)fd < fdp->fd_nfiles);
2069 KKASSERT(fdp->fd_files[fd].reserved != 0);
2070 if (fp) {
2071 fhold(fp);
2072 fclearcache(&fdp->fd_files[fd], NULL, 0);
2073 fdp->fd_files[fd].fp = fp;
2074 fdp->fd_files[fd].reserved = 0;
2075 } else {
2076 fdp->fd_files[fd].reserved = 0;
2077 fdreserve_locked(fdp, fd, -1);
2078 fdfixup_locked(fdp, fd);
2083 * Caller must hold an exclusive spinlock on fdp->fd_spin.
2085 void
2086 fsetfd(struct filedesc *fdp, struct file *fp, int fd)
2088 spin_lock(&fdp->fd_spin);
2089 fsetfd_locked(fdp, fp, fd);
2090 spin_unlock(&fdp->fd_spin);
2094 * Caller must hold an exclusive spinlock on fdp->fd_spin.
2096 static
2097 struct file *
2098 funsetfd_locked(struct filedesc *fdp, int fd)
2100 struct file *fp;
2102 if ((unsigned)fd >= fdp->fd_nfiles)
2103 return (NULL);
2104 if ((fp = fdp->fd_files[fd].fp) == NULL)
2105 return (NULL);
2106 ++fdp->fd_closedcounter;
2107 fclearcache(&fdp->fd_files[fd], NULL, 0);
2108 fdp->fd_files[fd].fp = NULL;
2109 fdp->fd_files[fd].fileflags = 0;
2110 ++fdp->fd_closedcounter;
2112 fdreserve_locked(fdp, fd, -1);
2113 fdfixup_locked(fdp, fd);
2115 return(fp);
2119 * WARNING: May not be called before initial fsetfd().
2122 fgetfdflags(struct filedesc *fdp, int fd, int *flagsp)
2124 int error;
2126 spin_lock(&fdp->fd_spin);
2127 if (((u_int)fd) >= fdp->fd_nfiles) {
2128 error = EBADF;
2129 } else if (fdp->fd_files[fd].fp == NULL) {
2130 error = EBADF;
2131 } else {
2132 *flagsp = fdp->fd_files[fd].fileflags;
2133 error = 0;
2135 spin_unlock(&fdp->fd_spin);
2136 return (error);
2140 * WARNING: May not be called before initial fsetfd().
2143 fsetfdflags(struct filedesc *fdp, int fd, int add_flags)
2145 int error;
2147 spin_lock(&fdp->fd_spin);
2148 if (((u_int)fd) >= fdp->fd_nfiles) {
2149 error = EBADF;
2150 } else if (fdp->fd_files[fd].fp == NULL) {
2151 error = EBADF;
2152 } else {
2153 fdp->fd_files[fd].fileflags |= add_flags;
2154 error = 0;
2156 spin_unlock(&fdp->fd_spin);
2157 return (error);
2161 * WARNING: May not be called before initial fsetfd().
2164 fclrfdflags(struct filedesc *fdp, int fd, int rem_flags)
2166 int error;
2168 spin_lock(&fdp->fd_spin);
2169 if (((u_int)fd) >= fdp->fd_nfiles) {
2170 error = EBADF;
2171 } else if (fdp->fd_files[fd].fp == NULL) {
2172 error = EBADF;
2173 } else {
2174 fdp->fd_files[fd].fileflags &= ~rem_flags;
2175 error = 0;
2177 spin_unlock(&fdp->fd_spin);
2178 return (error);
2182 * Set/Change/Clear the creds for a fp and synchronize the uidinfo.
2184 void
2185 fsetcred(struct file *fp, struct ucred *ncr)
2187 struct ucred *ocr;
2188 struct uidinfo *uip;
2189 struct uidcount *pup;
2190 int cpu = mycpuid;
2191 int count;
2193 ocr = fp->f_cred;
2194 if (ocr == NULL || ncr == NULL || ocr->cr_uidinfo != ncr->cr_uidinfo) {
2195 if (ocr) {
2196 uip = ocr->cr_uidinfo;
2197 pup = &uip->ui_pcpu[cpu];
2198 atomic_add_int(&pup->pu_openfiles, -1);
2199 if (pup->pu_openfiles < -PUP_LIMIT ||
2200 pup->pu_openfiles > PUP_LIMIT) {
2201 count = atomic_swap_int(&pup->pu_openfiles, 0);
2202 atomic_add_int(&uip->ui_openfiles, count);
2205 if (ncr) {
2206 uip = ncr->cr_uidinfo;
2207 pup = &uip->ui_pcpu[cpu];
2208 atomic_add_int(&pup->pu_openfiles, 1);
2209 if (pup->pu_openfiles < -PUP_LIMIT ||
2210 pup->pu_openfiles > PUP_LIMIT) {
2211 count = atomic_swap_int(&pup->pu_openfiles, 0);
2212 atomic_add_int(&uip->ui_openfiles, count);
2216 if (ncr)
2217 crhold(ncr);
2218 fp->f_cred = ncr;
2219 if (ocr)
2220 crfree(ocr);
2224 * Free a file descriptor.
2226 static
2227 void
2228 ffree(struct file *fp)
2230 KASSERT((fp->f_count == 0), ("ffree: fp_fcount not 0!"));
2231 fsetcred(fp, NULL);
2232 if (fp->f_nchandle.ncp)
2233 cache_drop(&fp->f_nchandle);
2234 objcache_put(file_objcache, fp);
2238 * called from init_main, initialize filedesc0 for proc0.
2240 void
2241 fdinit_bootstrap(struct proc *p0, struct filedesc *fdp0, int cmask)
2243 p0->p_fd = fdp0;
2244 p0->p_fdtol = NULL;
2245 fdp0->fd_refcnt = 1;
2246 fdp0->fd_cmask = cmask;
2247 fdp0->fd_files = fdp0->fd_builtin_files;
2248 fdp0->fd_nfiles = NDFILE;
2249 fdp0->fd_lastfile = -1;
2250 spin_init(&fdp0->fd_spin, "fdinitbootstrap");
2254 * Build a new filedesc structure.
2256 struct filedesc *
2257 fdinit(struct proc *p)
2259 struct filedesc *newfdp;
2260 struct filedesc *fdp = p->p_fd;
2262 newfdp = kmalloc(sizeof(struct filedesc), M_FILEDESC, M_WAITOK|M_ZERO);
2263 spin_lock(&fdp->fd_spin);
2264 if (fdp->fd_cdir) {
2265 newfdp->fd_cdir = fdp->fd_cdir;
2266 vref(newfdp->fd_cdir);
2267 cache_copy(&fdp->fd_ncdir, &newfdp->fd_ncdir);
2271 * rdir may not be set in e.g. proc0 or anything vm_fork'd off of
2272 * proc0, but should unconditionally exist in other processes.
2274 if (fdp->fd_rdir) {
2275 newfdp->fd_rdir = fdp->fd_rdir;
2276 vref(newfdp->fd_rdir);
2277 cache_copy(&fdp->fd_nrdir, &newfdp->fd_nrdir);
2279 if (fdp->fd_jdir) {
2280 newfdp->fd_jdir = fdp->fd_jdir;
2281 vref(newfdp->fd_jdir);
2282 cache_copy(&fdp->fd_njdir, &newfdp->fd_njdir);
2284 spin_unlock(&fdp->fd_spin);
2286 /* Create the file descriptor table. */
2287 newfdp->fd_refcnt = 1;
2288 newfdp->fd_cmask = cmask;
2289 newfdp->fd_files = newfdp->fd_builtin_files;
2290 newfdp->fd_nfiles = NDFILE;
2291 newfdp->fd_lastfile = -1;
2292 spin_init(&newfdp->fd_spin, "fdinit");
2294 return (newfdp);
2298 * Share a filedesc structure.
2300 struct filedesc *
2301 fdshare(struct proc *p)
2303 struct filedesc *fdp;
2305 fdp = p->p_fd;
2306 spin_lock(&fdp->fd_spin);
2307 fdp->fd_refcnt++;
2308 spin_unlock(&fdp->fd_spin);
2309 return (fdp);
2313 * Copy a filedesc structure.
2316 fdcopy(struct proc *p, struct filedesc **fpp)
2318 struct filedesc *fdp = p->p_fd;
2319 struct filedesc *newfdp;
2320 struct fdnode *fdnode;
2321 int i;
2322 int ni;
2325 * Certain daemons might not have file descriptors.
2327 if (fdp == NULL)
2328 return (0);
2331 * Allocate the new filedesc and fd_files[] array. This can race
2332 * with operations by other threads on the fdp so we have to be
2333 * careful.
2335 newfdp = kmalloc(sizeof(struct filedesc),
2336 M_FILEDESC, M_WAITOK | M_ZERO | M_NULLOK);
2337 if (newfdp == NULL) {
2338 *fpp = NULL;
2339 return (-1);
2341 again:
2342 spin_lock(&fdp->fd_spin);
2343 if (fdp->fd_lastfile < NDFILE) {
2344 newfdp->fd_files = newfdp->fd_builtin_files;
2345 i = NDFILE;
2346 } else {
2348 * We have to allocate (N^2-1) entries for our in-place
2349 * binary tree. Allow the table to shrink.
2351 i = fdp->fd_nfiles;
2352 ni = (i - 1) / 2;
2353 while (ni > fdp->fd_lastfile && ni > NDFILE) {
2354 i = ni;
2355 ni = (i - 1) / 2;
2357 spin_unlock(&fdp->fd_spin);
2358 newfdp->fd_files = kmalloc(i * sizeof(struct fdnode),
2359 M_FILEDESC, M_WAITOK | M_ZERO);
2362 * Check for race, retry
2364 spin_lock(&fdp->fd_spin);
2365 if (i <= fdp->fd_lastfile) {
2366 spin_unlock(&fdp->fd_spin);
2367 kfree(newfdp->fd_files, M_FILEDESC);
2368 goto again;
2373 * Dup the remaining fields. vref() and cache_hold() can be
2374 * safely called while holding the read spinlock on fdp.
2376 * The read spinlock on fdp is still being held.
2378 * NOTE: vref and cache_hold calls for the case where the vnode
2379 * or cache entry already has at least one ref may be called
2380 * while holding spin locks.
2382 if ((newfdp->fd_cdir = fdp->fd_cdir) != NULL) {
2383 vref(newfdp->fd_cdir);
2384 cache_copy(&fdp->fd_ncdir, &newfdp->fd_ncdir);
2387 * We must check for fd_rdir here, at least for now because
2388 * the init process is created before we have access to the
2389 * rootvode to take a reference to it.
2391 if ((newfdp->fd_rdir = fdp->fd_rdir) != NULL) {
2392 vref(newfdp->fd_rdir);
2393 cache_copy(&fdp->fd_nrdir, &newfdp->fd_nrdir);
2395 if ((newfdp->fd_jdir = fdp->fd_jdir) != NULL) {
2396 vref(newfdp->fd_jdir);
2397 cache_copy(&fdp->fd_njdir, &newfdp->fd_njdir);
2399 newfdp->fd_refcnt = 1;
2400 newfdp->fd_nfiles = i;
2401 newfdp->fd_lastfile = fdp->fd_lastfile;
2402 newfdp->fd_freefile = fdp->fd_freefile;
2403 newfdp->fd_cmask = fdp->fd_cmask;
2404 spin_init(&newfdp->fd_spin, "fdcopy");
2407 * Copy the descriptor table through (i). This also copies the
2408 * allocation state. Then go through and ref the file pointers
2409 * and clean up any KQ descriptors.
2411 * kq descriptors cannot be copied. Since we haven't ref'd the
2412 * copied files yet we can ignore the return value from funsetfd().
2414 * The read spinlock on fdp is still being held.
2416 * Be sure to clean out fdnode->tdcache, otherwise bad things will
2417 * happen.
2419 bcopy(fdp->fd_files, newfdp->fd_files, i * sizeof(struct fdnode));
2420 for (i = 0 ; i < newfdp->fd_nfiles; ++i) {
2421 fdnode = &newfdp->fd_files[i];
2422 if (fdnode->reserved) {
2423 fdreserve_locked(newfdp, i, -1);
2424 fdnode->reserved = 0;
2425 fdfixup_locked(newfdp, i);
2426 } else if (fdnode->fp) {
2427 bzero(&fdnode->tdcache, sizeof(fdnode->tdcache));
2428 if (fdnode->fp->f_type == DTYPE_KQUEUE) {
2429 (void)funsetfd_locked(newfdp, i);
2430 } else {
2431 fhold(fdnode->fp);
2435 spin_unlock(&fdp->fd_spin);
2436 *fpp = newfdp;
2437 return (0);
2441 * Release a filedesc structure.
2443 * NOT MPSAFE (MPSAFE for refs > 1, but the final cleanup code is not MPSAFE)
2445 void
2446 fdfree(struct proc *p, struct filedesc *repl)
2448 struct filedesc *fdp;
2449 struct fdnode *fdnode;
2450 int i;
2451 struct filedesc_to_leader *fdtol;
2452 struct file *fp;
2453 struct vnode *vp;
2454 struct flock lf;
2457 * Before destroying or replacing p->p_fd we must be sure to
2458 * clean out the cache of the last thread, which should be
2459 * curthread.
2461 fexitcache(curthread);
2464 * Certain daemons might not have file descriptors.
2466 fdp = p->p_fd;
2467 if (fdp == NULL) {
2468 p->p_fd = repl;
2469 return;
2473 * Severe messing around to follow.
2475 spin_lock(&fdp->fd_spin);
2477 /* Check for special need to clear POSIX style locks */
2478 fdtol = p->p_fdtol;
2479 if (fdtol != NULL) {
2480 KASSERT(fdtol->fdl_refcount > 0,
2481 ("filedesc_to_refcount botch: fdl_refcount=%d",
2482 fdtol->fdl_refcount));
2483 if (fdtol->fdl_refcount == 1 && p->p_leader->p_advlock_flag) {
2484 for (i = 0; i <= fdp->fd_lastfile; ++i) {
2485 fdnode = &fdp->fd_files[i];
2486 if (fdnode->fp == NULL ||
2487 fdnode->fp->f_type != DTYPE_VNODE) {
2488 continue;
2490 fp = fdnode->fp;
2491 fhold(fp);
2492 spin_unlock(&fdp->fd_spin);
2494 lf.l_whence = SEEK_SET;
2495 lf.l_start = 0;
2496 lf.l_len = 0;
2497 lf.l_type = F_UNLCK;
2498 vp = (struct vnode *)fp->f_data;
2499 VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
2500 F_UNLCK, &lf, F_POSIX);
2501 fdrop(fp);
2502 spin_lock(&fdp->fd_spin);
2505 retry:
2506 if (fdtol->fdl_refcount == 1) {
2507 if (fdp->fd_holdleaderscount > 0 &&
2508 p->p_leader->p_advlock_flag) {
2510 * close() or do_dup() has cleared a reference
2511 * in a shared file descriptor table.
2513 fdp->fd_holdleaderswakeup = 1;
2514 ssleep(&fdp->fd_holdleaderscount,
2515 &fdp->fd_spin, 0, "fdlhold", 0);
2516 goto retry;
2518 if (fdtol->fdl_holdcount > 0) {
2520 * Ensure that fdtol->fdl_leader
2521 * remains valid in closef().
2523 fdtol->fdl_wakeup = 1;
2524 ssleep(fdtol, &fdp->fd_spin, 0, "fdlhold", 0);
2525 goto retry;
2528 fdtol->fdl_refcount--;
2529 if (fdtol->fdl_refcount == 0 &&
2530 fdtol->fdl_holdcount == 0) {
2531 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
2532 fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
2533 } else {
2534 fdtol = NULL;
2536 p->p_fdtol = NULL;
2537 if (fdtol != NULL) {
2538 spin_unlock(&fdp->fd_spin);
2539 kfree(fdtol, M_FILEDESC_TO_LEADER);
2540 spin_lock(&fdp->fd_spin);
2543 if (--fdp->fd_refcnt > 0) {
2544 spin_unlock(&fdp->fd_spin);
2545 spin_lock(&p->p_spin);
2546 p->p_fd = repl;
2547 spin_unlock(&p->p_spin);
2548 return;
2552 * Even though we are the last reference to the structure allproc
2553 * scans may still reference the structure. Maintain proper
2554 * locks until we can replace p->p_fd.
2556 * Also note that kqueue's closef still needs to reference the
2557 * fdp via p->p_fd, so we have to close the descriptors before
2558 * we replace p->p_fd.
2560 for (i = 0; i <= fdp->fd_lastfile; ++i) {
2561 if (fdp->fd_files[i].fp) {
2562 fp = funsetfd_locked(fdp, i);
2563 if (fp) {
2564 spin_unlock(&fdp->fd_spin);
2565 if (SLIST_FIRST(&fp->f_klist))
2566 knote_fdclose(fp, fdp, i);
2567 closef(fp, p);
2568 spin_lock(&fdp->fd_spin);
2572 spin_unlock(&fdp->fd_spin);
2575 * Interlock against an allproc scan operations (typically frevoke).
2577 spin_lock(&p->p_spin);
2578 p->p_fd = repl;
2579 spin_unlock(&p->p_spin);
2582 * Wait for any softrefs to go away. This race rarely occurs so
2583 * we can use a non-critical-path style poll/sleep loop. The
2584 * race only occurs against allproc scans.
2586 * No new softrefs can occur with the fdp disconnected from the
2587 * process.
2589 if (fdp->fd_softrefs) {
2590 kprintf("pid %d: Warning, fdp race avoided\n", p->p_pid);
2591 while (fdp->fd_softrefs)
2592 tsleep(&fdp->fd_softrefs, 0, "fdsoft", 1);
2595 if (fdp->fd_files != fdp->fd_builtin_files)
2596 kfree(fdp->fd_files, M_FILEDESC);
2597 if (fdp->fd_cdir) {
2598 cache_drop(&fdp->fd_ncdir);
2599 vrele(fdp->fd_cdir);
2601 if (fdp->fd_rdir) {
2602 cache_drop(&fdp->fd_nrdir);
2603 vrele(fdp->fd_rdir);
2605 if (fdp->fd_jdir) {
2606 cache_drop(&fdp->fd_njdir);
2607 vrele(fdp->fd_jdir);
2609 kfree(fdp, M_FILEDESC);
2613 * Retrieve and reference the file pointer associated with a descriptor.
2615 * td must be the current thread.
2617 struct file *
2618 holdfp(thread_t td, int fd, int flag)
2620 struct file *fp;
2622 fp = _holdfp_cache(td, fd);
2623 if (fp) {
2624 if ((fp->f_flag & flag) == 0 && flag != -1) {
2625 fdrop(fp);
2626 fp = NULL;
2629 return fp;
2633 * holdsock() - load the struct file pointer associated
2634 * with a socket into *fpp. If an error occurs, non-zero
2635 * will be returned and *fpp will be set to NULL.
2637 * td must be the current thread.
2640 holdsock(thread_t td, int fd, struct file **fpp)
2642 struct file *fp;
2643 int error;
2646 * Lockless shortcut
2648 fp = _holdfp_cache(td, fd);
2649 if (fp) {
2650 if (fp->f_type != DTYPE_SOCKET) {
2651 fdrop(fp);
2652 fp = NULL;
2653 error = ENOTSOCK;
2654 } else {
2655 error = 0;
2657 } else {
2658 error = EBADF;
2660 *fpp = fp;
2662 return (error);
2666 * Convert a user file descriptor to a held file pointer.
2668 * td must be the current thread.
2671 holdvnode(thread_t td, int fd, struct file **fpp)
2673 struct file *fp;
2674 int error;
2676 fp = _holdfp_cache(td, fd);
2677 if (fp) {
2678 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO) {
2679 fdrop(fp);
2680 fp = NULL;
2681 error = EINVAL;
2682 } else {
2683 error = 0;
2685 } else {
2686 error = EBADF;
2688 *fpp = fp;
2690 return (error);
2694 * For setugid programs, we don't want to people to use that setugidness
2695 * to generate error messages which write to a file which otherwise would
2696 * otherwise be off-limits to the process.
2698 * This is a gross hack to plug the hole. A better solution would involve
2699 * a special vop or other form of generalized access control mechanism. We
2700 * go ahead and just reject all procfs file systems accesses as dangerous.
2702 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
2703 * sufficient. We also don't for check setugidness since we know we are.
2705 static int
2706 is_unsafe(struct file *fp)
2708 if (fp->f_type == DTYPE_VNODE &&
2709 ((struct vnode *)(fp->f_data))->v_tag == VT_PROCFS)
2710 return (1);
2711 return (0);
2715 * Make this setguid thing safe, if at all possible.
2717 * NOT MPSAFE - scans fdp without spinlocks, calls knote_fdclose()
2719 void
2720 setugidsafety(struct proc *p)
2722 struct filedesc *fdp = p->p_fd;
2723 int i;
2725 /* Certain daemons might not have file descriptors. */
2726 if (fdp == NULL)
2727 return;
2730 * note: fdp->fd_files may be reallocated out from under us while
2731 * we are blocked in a close. Be careful!
2733 for (i = 0; i <= fdp->fd_lastfile; i++) {
2734 if (i > 2)
2735 break;
2736 if (fdp->fd_files[i].fp && is_unsafe(fdp->fd_files[i].fp)) {
2737 struct file *fp;
2740 * NULL-out descriptor prior to close to avoid
2741 * a race while close blocks.
2743 if ((fp = funsetfd_locked(fdp, i)) != NULL) {
2744 knote_fdclose(fp, fdp, i);
2745 closef(fp, p);
2752 * Close all CLOEXEC files on exec.
2754 * Only a single thread remains for the current process.
2756 * NOT MPSAFE - scans fdp without spinlocks, calls knote_fdclose()
2758 void
2759 fdcloseexec(struct proc *p)
2761 struct filedesc *fdp = p->p_fd;
2762 int i;
2764 /* Certain daemons might not have file descriptors. */
2765 if (fdp == NULL)
2766 return;
2769 * We cannot cache fd_files since operations may block and rip
2770 * them out from under us.
2772 for (i = 0; i <= fdp->fd_lastfile; i++) {
2773 if (fdp->fd_files[i].fp != NULL &&
2774 (fdp->fd_files[i].fileflags & UF_EXCLOSE)) {
2775 struct file *fp;
2778 * NULL-out descriptor prior to close to avoid
2779 * a race while close blocks.
2781 * (funsetfd*() also clears the fd cache)
2783 if ((fp = funsetfd_locked(fdp, i)) != NULL) {
2784 knote_fdclose(fp, fdp, i);
2785 closef(fp, p);
2792 * It is unsafe for set[ug]id processes to be started with file
2793 * descriptors 0..2 closed, as these descriptors are given implicit
2794 * significance in the Standard C library. fdcheckstd() will create a
2795 * descriptor referencing /dev/null for each of stdin, stdout, and
2796 * stderr that is not already open.
2798 * NOT MPSAFE - calls falloc, vn_open, etc
2801 fdcheckstd(struct lwp *lp)
2803 struct nlookupdata nd;
2804 struct filedesc *fdp;
2805 struct file *fp;
2806 int retval;
2807 int i, error, flags, devnull;
2809 fdp = lp->lwp_proc->p_fd;
2810 if (fdp == NULL)
2811 return (0);
2812 devnull = -1;
2813 error = 0;
2814 for (i = 0; i < 3; i++) {
2815 if (fdp->fd_files[i].fp != NULL)
2816 continue;
2817 if (devnull < 0) {
2818 if ((error = falloc(lp, &fp, &devnull)) != 0)
2819 break;
2821 error = nlookup_init(&nd, "/dev/null", UIO_SYSSPACE,
2822 NLC_FOLLOW|NLC_LOCKVP);
2823 flags = FREAD | FWRITE;
2824 if (error == 0)
2825 error = vn_open(&nd, fp, flags, 0);
2826 if (error == 0)
2827 fsetfd(fdp, fp, devnull);
2828 else
2829 fsetfd(fdp, NULL, devnull);
2830 fdrop(fp);
2831 nlookup_done(&nd);
2832 if (error)
2833 break;
2834 KKASSERT(i == devnull);
2835 } else {
2836 error = kern_dup(DUP_FIXED, devnull, i, &retval);
2837 if (error != 0)
2838 break;
2841 return (error);
2845 * Internal form of close.
2846 * Decrement reference count on file structure.
2847 * Note: td and/or p may be NULL when closing a file
2848 * that was being passed in a message.
2850 * MPALMOSTSAFE - acquires mplock for VOP operations
2853 closef(struct file *fp, struct proc *p)
2855 struct vnode *vp;
2856 struct flock lf;
2857 struct filedesc_to_leader *fdtol;
2859 if (fp == NULL)
2860 return (0);
2863 * POSIX record locking dictates that any close releases ALL
2864 * locks owned by this process. This is handled by setting
2865 * a flag in the unlock to free ONLY locks obeying POSIX
2866 * semantics, and not to free BSD-style file locks.
2867 * If the descriptor was in a message, POSIX-style locks
2868 * aren't passed with the descriptor.
2870 if (p != NULL && fp->f_type == DTYPE_VNODE &&
2871 (((struct vnode *)fp->f_data)->v_flag & VMAYHAVELOCKS)
2873 if (p->p_leader->p_advlock_flag) {
2874 lf.l_whence = SEEK_SET;
2875 lf.l_start = 0;
2876 lf.l_len = 0;
2877 lf.l_type = F_UNLCK;
2878 vp = (struct vnode *)fp->f_data;
2879 VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
2880 &lf, F_POSIX);
2882 fdtol = p->p_fdtol;
2883 if (fdtol != NULL) {
2884 lwkt_gettoken(&p->p_token);
2887 * Handle special case where file descriptor table
2888 * is shared between multiple process leaders.
2890 for (fdtol = fdtol->fdl_next;
2891 fdtol != p->p_fdtol;
2892 fdtol = fdtol->fdl_next) {
2893 if (fdtol->fdl_leader->p_advlock_flag == 0)
2894 continue;
2895 fdtol->fdl_holdcount++;
2896 lf.l_whence = SEEK_SET;
2897 lf.l_start = 0;
2898 lf.l_len = 0;
2899 lf.l_type = F_UNLCK;
2900 vp = (struct vnode *)fp->f_data;
2901 VOP_ADVLOCK(vp, (caddr_t)fdtol->fdl_leader,
2902 F_UNLCK, &lf, F_POSIX);
2903 fdtol->fdl_holdcount--;
2904 if (fdtol->fdl_holdcount == 0 &&
2905 fdtol->fdl_wakeup != 0) {
2906 fdtol->fdl_wakeup = 0;
2907 wakeup(fdtol);
2910 lwkt_reltoken(&p->p_token);
2913 return (fdrop(fp));
2917 * fhold() can only be called if f_count is already at least 1 (i.e. the
2918 * caller of fhold() already has a reference to the file pointer in some
2919 * manner or other).
2921 * Atomic ops are used for incrementing and decrementing f_count before
2922 * the 1->0 transition. f_count 1->0 transition is special, see the
2923 * comment in fdrop().
2925 void
2926 fhold(struct file *fp)
2928 /* 0->1 transition will never work */
2929 KASSERT(fp->f_count > 0, ("fhold: invalid f_count %d", fp->f_count));
2930 atomic_add_int(&fp->f_count, 1);
2934 * fdrop() - drop a reference to a descriptor
2937 fdrop(struct file *fp)
2939 struct flock lf;
2940 struct vnode *vp;
2941 int error, do_free = 0;
2944 * NOTE:
2945 * Simple atomic_fetchadd_int(f_count, -1) here will cause use-
2946 * after-free or double free (due to f_count 0->1 transition), if
2947 * fhold() is called on the fps found through filehead iteration.
2949 for (;;) {
2950 int count = fp->f_count;
2952 cpu_ccfence();
2953 KASSERT(count > 0, ("fdrop: invalid f_count %d", count));
2954 if (count == 1) {
2955 struct filelist_head *head = fp2filelist(fp);
2958 * About to drop the last reference, hold the
2959 * filehead spin lock and drop it, so that no
2960 * one could see this fp through filehead anymore,
2961 * let alone fhold() this fp.
2963 spin_lock(&head->spin);
2964 if (atomic_cmpset_int(&fp->f_count, count, 0)) {
2965 LIST_REMOVE(fp, f_list);
2966 spin_unlock(&head->spin);
2967 atomic_subtract_int(&nfiles, 1);
2968 do_free = 1; /* free this fp */
2969 break;
2971 spin_unlock(&head->spin);
2972 /* retry */
2973 } else if (atomic_cmpset_int(&fp->f_count, count, count - 1)) {
2974 break;
2976 /* retry */
2978 if (!do_free)
2979 return (0);
2981 KKASSERT(SLIST_FIRST(&fp->f_klist) == NULL);
2984 * The last reference has gone away, we own the fp structure free
2985 * and clear.
2987 if (fp->f_count < 0)
2988 panic("fdrop: count < 0");
2989 if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE &&
2990 (((struct vnode *)fp->f_data)->v_flag & VMAYHAVELOCKS)
2992 lf.l_whence = SEEK_SET;
2993 lf.l_start = 0;
2994 lf.l_len = 0;
2995 lf.l_type = F_UNLCK;
2996 vp = (struct vnode *)fp->f_data;
2997 VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, 0);
2999 if (fp->f_ops != &badfileops)
3000 error = fo_close(fp);
3001 else
3002 error = 0;
3003 ffree(fp);
3004 return (error);
3008 * Apply an advisory lock on a file descriptor.
3010 * Just attempt to get a record lock of the requested type on
3011 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
3013 * MPALMOSTSAFE
3016 sys_flock(struct flock_args *uap)
3018 thread_t td = curthread;
3019 struct file *fp;
3020 struct vnode *vp;
3021 struct flock lf;
3022 int error;
3024 if ((fp = holdfp(td, uap->fd, -1)) == NULL)
3025 return (EBADF);
3026 if (fp->f_type != DTYPE_VNODE) {
3027 error = EOPNOTSUPP;
3028 goto done;
3030 vp = (struct vnode *)fp->f_data;
3031 lf.l_whence = SEEK_SET;
3032 lf.l_start = 0;
3033 lf.l_len = 0;
3034 if (uap->how & LOCK_UN) {
3035 lf.l_type = F_UNLCK;
3036 atomic_clear_int(&fp->f_flag, FHASLOCK); /* race ok */
3037 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, 0);
3038 goto done;
3040 if (uap->how & LOCK_EX)
3041 lf.l_type = F_WRLCK;
3042 else if (uap->how & LOCK_SH)
3043 lf.l_type = F_RDLCK;
3044 else {
3045 error = EBADF;
3046 goto done;
3048 if (uap->how & LOCK_NB)
3049 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, 0);
3050 else
3051 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_WAIT);
3052 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
3053 done:
3054 fdrop(fp);
3055 return (error);
3059 * File Descriptor pseudo-device driver (/dev/fd/).
3061 * Opening minor device N dup()s the file (if any) connected to file
3062 * descriptor N belonging to the calling process. Note that this driver
3063 * consists of only the ``open()'' routine, because all subsequent
3064 * references to this file will be direct to the other driver.
3066 static int
3067 fdopen(struct dev_open_args *ap)
3069 thread_t td = curthread;
3071 KKASSERT(td->td_lwp != NULL);
3074 * XXX Kludge: set curlwp->lwp_dupfd to contain the value of the
3075 * the file descriptor being sought for duplication. The error
3076 * return ensures that the vnode for this device will be released
3077 * by vn_open. Open will detect this special error and take the
3078 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
3079 * will simply report the error.
3081 td->td_lwp->lwp_dupfd = minor(ap->a_head.a_dev);
3082 return (ENODEV);
3086 * The caller has reserved the file descriptor dfd for us. On success we
3087 * must fsetfd() it. On failure the caller will clean it up.
3090 dupfdopen(thread_t td, int dfd, int sfd, int mode, int error)
3092 struct filedesc *fdp;
3093 struct file *wfp;
3094 struct file *xfp;
3095 int werror;
3097 if ((wfp = holdfp(td, sfd, -1)) == NULL)
3098 return (EBADF);
3101 * Close a revoke/dup race. Duping a descriptor marked as revoked
3102 * will dup a dummy descriptor instead of the real one.
3104 if (wfp->f_flag & FREVOKED) {
3105 kprintf("Warning: attempt to dup() a revoked descriptor\n");
3106 fdrop(wfp);
3107 wfp = NULL;
3108 werror = falloc(NULL, &wfp, NULL);
3109 if (werror)
3110 return (werror);
3113 fdp = td->td_proc->p_fd;
3116 * There are two cases of interest here.
3118 * For ENODEV simply dup sfd to file descriptor dfd and return.
3120 * For ENXIO steal away the file structure from sfd and store it
3121 * dfd. sfd is effectively closed by this operation.
3123 * Any other error code is just returned.
3125 switch (error) {
3126 case ENODEV:
3128 * Check that the mode the file is being opened for is a
3129 * subset of the mode of the existing descriptor.
3131 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
3132 error = EACCES;
3133 break;
3135 spin_lock(&fdp->fd_spin);
3136 fdp->fd_files[dfd].fileflags = fdp->fd_files[sfd].fileflags;
3137 fsetfd_locked(fdp, wfp, dfd);
3138 spin_unlock(&fdp->fd_spin);
3139 error = 0;
3140 break;
3141 case ENXIO:
3143 * Steal away the file pointer from dfd, and stuff it into indx.
3145 spin_lock(&fdp->fd_spin);
3146 fdp->fd_files[dfd].fileflags = fdp->fd_files[sfd].fileflags;
3147 fsetfd(fdp, wfp, dfd);
3148 if ((xfp = funsetfd_locked(fdp, sfd)) != NULL) {
3149 spin_unlock(&fdp->fd_spin);
3150 fdrop(xfp);
3151 } else {
3152 spin_unlock(&fdp->fd_spin);
3154 error = 0;
3155 break;
3156 default:
3157 break;
3159 fdrop(wfp);
3160 return (error);
3164 * NOT MPSAFE - I think these refer to a common file descriptor table
3165 * and we need to spinlock that to link fdtol in.
3167 struct filedesc_to_leader *
3168 filedesc_to_leader_alloc(struct filedesc_to_leader *old,
3169 struct proc *leader)
3171 struct filedesc_to_leader *fdtol;
3173 fdtol = kmalloc(sizeof(struct filedesc_to_leader),
3174 M_FILEDESC_TO_LEADER, M_WAITOK | M_ZERO);
3175 fdtol->fdl_refcount = 1;
3176 fdtol->fdl_holdcount = 0;
3177 fdtol->fdl_wakeup = 0;
3178 fdtol->fdl_leader = leader;
3179 if (old != NULL) {
3180 fdtol->fdl_next = old->fdl_next;
3181 fdtol->fdl_prev = old;
3182 old->fdl_next = fdtol;
3183 fdtol->fdl_next->fdl_prev = fdtol;
3184 } else {
3185 fdtol->fdl_next = fdtol;
3186 fdtol->fdl_prev = fdtol;
3188 return fdtol;
3192 * Scan all file pointers in the system. The callback is made with
3193 * the master list spinlock held exclusively.
3195 void
3196 allfiles_scan_exclusive(int (*callback)(struct file *, void *), void *data)
3198 int i;
3200 for (i = 0; i < NFILELIST_HEADS; ++i) {
3201 struct filelist_head *head = &filelist_heads[i];
3202 struct file *fp;
3204 spin_lock(&head->spin);
3205 LIST_FOREACH(fp, &head->list, f_list) {
3206 int res;
3208 res = callback(fp, data);
3209 if (res < 0)
3210 break;
3212 spin_unlock(&head->spin);
3217 * Get file structures.
3219 * NOT MPSAFE - process list scan, SYSCTL_OUT (probably not mpsafe)
3222 struct sysctl_kern_file_info {
3223 int count;
3224 int error;
3225 struct sysctl_req *req;
3228 static int sysctl_kern_file_callback(struct proc *p, void *data);
3230 static int
3231 sysctl_kern_file(SYSCTL_HANDLER_ARGS)
3233 struct sysctl_kern_file_info info;
3236 * Note: because the number of file descriptors is calculated
3237 * in different ways for sizing vs returning the data,
3238 * there is information leakage from the first loop. However,
3239 * it is of a similar order of magnitude to the leakage from
3240 * global system statistics such as kern.openfiles.
3242 * When just doing a count, note that we cannot just count
3243 * the elements and add f_count via the filehead list because
3244 * threaded processes share their descriptor table and f_count might
3245 * still be '1' in that case.
3247 * Since the SYSCTL op can block, we must hold the process to
3248 * prevent it being ripped out from under us either in the
3249 * file descriptor loop or in the greater LIST_FOREACH. The
3250 * process may be in varying states of disrepair. If the process
3251 * is in SZOMB we may have caught it just as it is being removed
3252 * from the allproc list, we must skip it in that case to maintain
3253 * an unbroken chain through the allproc list.
3255 info.count = 0;
3256 info.error = 0;
3257 info.req = req;
3258 allproc_scan(sysctl_kern_file_callback, &info, 0);
3261 * When just calculating the size, overestimate a bit to try to
3262 * prevent system activity from causing the buffer-fill call
3263 * to fail later on.
3265 if (req->oldptr == NULL) {
3266 info.count = (info.count + 16) + (info.count / 10);
3267 info.error = SYSCTL_OUT(req, NULL,
3268 info.count * sizeof(struct kinfo_file));
3270 return (info.error);
3273 static int
3274 sysctl_kern_file_callback(struct proc *p, void *data)
3276 struct sysctl_kern_file_info *info = data;
3277 struct kinfo_file kf;
3278 struct filedesc *fdp;
3279 struct file *fp;
3280 uid_t uid;
3281 int n;
3283 if (p->p_stat == SIDL || p->p_stat == SZOMB)
3284 return(0);
3285 if (!(PRISON_CHECK(info->req->td->td_ucred, p->p_ucred) != 0))
3286 return(0);
3289 * Softref the fdp to prevent it from being destroyed
3291 spin_lock(&p->p_spin);
3292 if ((fdp = p->p_fd) == NULL) {
3293 spin_unlock(&p->p_spin);
3294 return(0);
3296 atomic_add_int(&fdp->fd_softrefs, 1);
3297 spin_unlock(&p->p_spin);
3300 * The fdp's own spinlock prevents the contents from being
3301 * modified.
3303 spin_lock_shared(&fdp->fd_spin);
3304 for (n = 0; n < fdp->fd_nfiles; ++n) {
3305 if ((fp = fdp->fd_files[n].fp) == NULL)
3306 continue;
3307 if (info->req->oldptr == NULL) {
3308 ++info->count;
3309 } else {
3310 uid = p->p_ucred ? p->p_ucred->cr_uid : -1;
3311 kcore_make_file(&kf, fp, p->p_pid, uid, n);
3312 spin_unlock_shared(&fdp->fd_spin);
3313 info->error = SYSCTL_OUT(info->req, &kf, sizeof(kf));
3314 spin_lock_shared(&fdp->fd_spin);
3315 if (info->error)
3316 break;
3319 spin_unlock_shared(&fdp->fd_spin);
3320 atomic_subtract_int(&fdp->fd_softrefs, 1);
3321 if (info->error)
3322 return(-1);
3323 return(0);
3326 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
3327 0, 0, sysctl_kern_file, "S,file", "Entire file table");
3329 SYSCTL_INT(_kern, OID_AUTO, minfilesperproc, CTLFLAG_RW,
3330 &minfilesperproc, 0, "Minimum files allowed open per process");
3331 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
3332 &maxfilesperproc, 0, "Maximum files allowed open per process");
3333 SYSCTL_INT(_kern, OID_AUTO, maxfilesperuser, CTLFLAG_RW,
3334 &maxfilesperuser, 0, "Maximum files allowed open per user");
3336 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
3337 &maxfiles, 0, "Maximum number of files");
3339 SYSCTL_INT(_kern, OID_AUTO, maxfilesrootres, CTLFLAG_RW,
3340 &maxfilesrootres, 0, "Descriptors reserved for root use");
3342 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
3343 &nfiles, 0, "System-wide number of open files");
3345 static void
3346 fildesc_drvinit(void *unused)
3348 int fd;
3350 for (fd = 0; fd < NUMFDESC; fd++) {
3351 make_dev(&fildesc_ops, fd,
3352 UID_BIN, GID_BIN, 0666, "fd/%d", fd);
3355 make_dev(&fildesc_ops, 0, UID_ROOT, GID_WHEEL, 0666, "stdin");
3356 make_dev(&fildesc_ops, 1, UID_ROOT, GID_WHEEL, 0666, "stdout");
3357 make_dev(&fildesc_ops, 2, UID_ROOT, GID_WHEEL, 0666, "stderr");
3360 struct fileops badfileops = {
3361 .fo_read = badfo_readwrite,
3362 .fo_write = badfo_readwrite,
3363 .fo_ioctl = badfo_ioctl,
3364 .fo_kqfilter = badfo_kqfilter,
3365 .fo_stat = badfo_stat,
3366 .fo_close = badfo_close,
3367 .fo_shutdown = badfo_shutdown
3371 badfo_readwrite(
3372 struct file *fp,
3373 struct uio *uio,
3374 struct ucred *cred,
3375 int flags
3377 return (EBADF);
3381 badfo_ioctl(struct file *fp, u_long com, caddr_t data,
3382 struct ucred *cred, struct sysmsg *msgv)
3384 return (EBADF);
3388 * Must return an error to prevent registration, typically
3389 * due to a revoked descriptor (file_filtops assigned).
3392 badfo_kqfilter(struct file *fp, struct knote *kn)
3394 return (EOPNOTSUPP);
3398 badfo_stat(struct file *fp, struct stat *sb, struct ucred *cred)
3400 return (EBADF);
3404 badfo_close(struct file *fp)
3406 return (EBADF);
3410 badfo_shutdown(struct file *fp, int how)
3412 return (EBADF);
3416 nofo_shutdown(struct file *fp, int how)
3418 return (EOPNOTSUPP);
3421 SYSINIT(fildescdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR,
3422 fildesc_drvinit,NULL);
3424 static void
3425 filelist_heads_init(void *arg __unused)
3427 int i;
3429 for (i = 0; i < NFILELIST_HEADS; ++i) {
3430 struct filelist_head *head = &filelist_heads[i];
3432 spin_init(&head->spin, "filehead_spin");
3433 LIST_INIT(&head->list);
3437 SYSINIT(filelistheads, SI_BOOT1_LOCK, SI_ORDER_ANY,
3438 filelist_heads_init, NULL);
3440 static void
3441 file_objcache_init(void *dummy __unused)
3443 file_objcache = objcache_create("file", maxfiles, maxfiles / 8,
3444 NULL, NULL, NULL, /* TODO: ctor/dtor */
3445 objcache_malloc_alloc, objcache_malloc_free, &file_malloc_args);
3447 SYSINIT(fpobjcache, SI_BOOT2_POST_SMP, SI_ORDER_ANY, file_objcache_init, NULL);