]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/filedesc.h
Merge from vmcontention
[FreeBSD/FreeBSD.git] / sys / sys / filedesc.h
1 /*-
2  * Copyright (c) 1990, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)filedesc.h  8.1 (Berkeley) 6/2/93
30  * $FreeBSD$
31  */
32
33 #ifndef _SYS_FILEDESC_H_
34 #define _SYS_FILEDESC_H_
35
36 #include <sys/queue.h>
37 #include <sys/event.h>
38 #include <sys/lock.h>
39 #include <sys/priority.h>
40 #include <sys/sx.h>
41
42 #include <machine/_limits.h>
43
44 struct filecaps {
45         cap_rights_t     fc_rights;     /* per-descriptor capability rights */
46         uint32_t         fc_fcntls;     /* per-descriptor allowed fcntls */
47         u_long          *fc_ioctls;     /* per-descriptor allowed ioctls */
48         int16_t          fc_nioctls;    /* fc_ioctls array size */
49 };
50
51 struct filedescent {
52         struct file     *fde_file;              /* file structure for open file */
53         struct filecaps  fde_caps;              /* per-descriptor rights */
54         uint8_t          fde_flags;             /* per-process open file flags */
55 };
56 #define fde_rights      fde_caps.fc_rights
57 #define fde_fcntls      fde_caps.fc_fcntls
58 #define fde_ioctls      fde_caps.fc_ioctls
59 #define fde_nioctls     fde_caps.fc_nioctls
60
61 /*
62  * This structure is used for the management of descriptors.  It may be
63  * shared by multiple processes.
64  */
65 #define NDSLOTTYPE      u_long
66
67 struct filedesc {
68         struct  filedescent *fd_ofiles; /* open files */
69         struct  vnode *fd_cdir;         /* current directory */
70         struct  vnode *fd_rdir;         /* root directory */
71         struct  vnode *fd_jdir;         /* jail root directory */
72         int     fd_nfiles;              /* number of open files allocated */
73         NDSLOTTYPE *fd_map;             /* bitmap of free fds */
74         int     fd_lastfile;            /* high-water mark of fd_ofiles */
75         int     fd_freefile;            /* approx. next free file */
76         u_short fd_cmask;               /* mask for file creation */
77         u_short fd_refcnt;              /* thread reference count */
78         u_short fd_holdcnt;             /* hold count on structure + mutex */
79         struct  sx fd_sx;               /* protects members of this struct */
80         struct  kqlist fd_kqlist;       /* list of kqueues on this filedesc */
81         int     fd_holdleaderscount;    /* block fdfree() for shared close() */
82         int     fd_holdleaderswakeup;   /* fdfree() needs wakeup */
83 };
84
85 /*
86  * Structure to keep track of (process leader, struct fildedesc) tuples.
87  * Each process has a pointer to such a structure when detailed tracking
88  * is needed, e.g., when rfork(RFPROC | RFMEM) causes a file descriptor
89  * table to be shared by processes having different "p_leader" pointers
90  * and thus distinct POSIX style locks.
91  *
92  * fdl_refcount and fdl_holdcount are protected by struct filedesc mtx.
93  */
94 struct filedesc_to_leader {
95         int             fdl_refcount;   /* references from struct proc */
96         int             fdl_holdcount;  /* temporary hold during closef */
97         int             fdl_wakeup;     /* fdfree() waits on closef() */
98         struct proc     *fdl_leader;    /* owner of POSIX locks */
99         /* Circular list: */
100         struct filedesc_to_leader *fdl_prev;
101         struct filedesc_to_leader *fdl_next;
102 };
103
104 /*
105  * Per-process open flags.
106  */
107 #define UF_EXCLOSE      0x01            /* auto-close on exec */
108
109 #ifdef _KERNEL
110
111 #include <sys/systm.h>  /* CTASSERT() */
112
113 CTASSERT(sizeof(cap_rights_t) == sizeof(uint64_t));
114
115 /* Flags for do_dup() */
116 #define DUP_FIXED       0x1     /* Force fixed allocation. */
117 #define DUP_FCNTL       0x2     /* fcntl()-style errors. */
118 #define DUP_CLOEXEC     0x4     /* Atomically set FD_CLOEXEC. */
119
120 /* Lock a file descriptor table. */
121 #define FILEDESC_LOCK_INIT(fdp) sx_init(&(fdp)->fd_sx, "filedesc structure")
122 #define FILEDESC_LOCK_DESTROY(fdp)      sx_destroy(&(fdp)->fd_sx)
123 #define FILEDESC_LOCK(fdp)      (&(fdp)->fd_sx)
124 #define FILEDESC_XLOCK(fdp)     sx_xlock(&(fdp)->fd_sx)
125 #define FILEDESC_XUNLOCK(fdp)   sx_xunlock(&(fdp)->fd_sx)
126 #define FILEDESC_SLOCK(fdp)     sx_slock(&(fdp)->fd_sx)
127 #define FILEDESC_SUNLOCK(fdp)   sx_sunlock(&(fdp)->fd_sx)
128
129 #define FILEDESC_LOCK_ASSERT(fdp)       sx_assert(&(fdp)->fd_sx, SX_LOCKED | \
130                                             SX_NOTRECURSED)
131 #define FILEDESC_XLOCK_ASSERT(fdp)      sx_assert(&(fdp)->fd_sx, SX_XLOCKED | \
132                                             SX_NOTRECURSED)
133 #define FILEDESC_UNLOCK_ASSERT(fdp)     sx_assert(&(fdp)->fd_sx, SX_UNLOCKED)
134
135 struct thread;
136
137 void    filecaps_init(struct filecaps *fcaps);
138 void    filecaps_copy(const struct filecaps *src, struct filecaps *dst);
139 void    filecaps_free(struct filecaps *fcaps);
140
141 int     closef(struct file *fp, struct thread *td);
142 int     do_dup(struct thread *td, int flags, int old, int new,
143             register_t *retval);
144 int     dupfdopen(struct thread *td, struct filedesc *fdp, int dfd, int mode,
145             int openerror, int *indxp);
146 int     falloc(struct thread *td, struct file **resultfp, int *resultfd,
147             int flags);
148 int     falloc_noinstall(struct thread *td, struct file **resultfp);
149 int     finstall(struct thread *td, struct file *fp, int *resultfp, int flags,
150             struct filecaps *fcaps);
151 int     fdalloc(struct thread *td, int minfd, int *result);
152 int     fdavail(struct thread *td, int n);
153 int     fdcheckstd(struct thread *td);
154 void    fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td);
155 void    fdcloseexec(struct thread *td);
156 struct  filedesc *fdcopy(struct filedesc *fdp);
157 void    fdunshare(struct proc *p, struct thread *td);
158 void    fdescfree(struct thread *td);
159 struct  filedesc *fdinit(struct filedesc *fdp);
160 struct  filedesc *fdshare(struct filedesc *fdp);
161 struct filedesc_to_leader *
162         filedesc_to_leader_alloc(struct filedesc_to_leader *old,
163             struct filedesc *fdp, struct proc *leader);
164 int     getvnode(struct filedesc *fdp, int fd, cap_rights_t rights,
165             struct file **fpp);
166 void    mountcheckdirs(struct vnode *olddp, struct vnode *newdp);
167 void    setugidsafety(struct thread *td);
168
169 /* Return a referenced file from an unlocked descriptor. */
170 int     fget_unlocked(struct filedesc *fdp, int fd, cap_rights_t needrights,
171             int needfcntl, struct file **fpp, cap_rights_t *haverightsp);
172
173 /* Requires a FILEDESC_{S,X}LOCK held and returns without a ref. */
174 static __inline struct file *
175 fget_locked(struct filedesc *fdp, int fd)
176 {
177
178         FILEDESC_LOCK_ASSERT(fdp);
179
180         if (fd < 0 || fd >= fdp->fd_nfiles)
181                 return (NULL);
182
183         return (fdp->fd_ofiles[fd].fde_file);
184 }
185
186 #endif /* _KERNEL */
187
188 #endif /* !_SYS_FILEDESC_H_ */