1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions * Copyright (c) 2000-2021 Apple Inc. All rights reserved.
3*43a90889SApple OSS Distributions *
4*43a90889SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*43a90889SApple OSS Distributions *
6*43a90889SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*43a90889SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*43a90889SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*43a90889SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*43a90889SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*43a90889SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*43a90889SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*43a90889SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*43a90889SApple OSS Distributions *
15*43a90889SApple OSS Distributions * Please obtain a copy of the License at
16*43a90889SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*43a90889SApple OSS Distributions *
18*43a90889SApple OSS Distributions * The Original Code and all software distributed under the License are
19*43a90889SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*43a90889SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*43a90889SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*43a90889SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*43a90889SApple OSS Distributions * Please see the License for the specific language governing rights and
24*43a90889SApple OSS Distributions * limitations under the License.
25*43a90889SApple OSS Distributions *
26*43a90889SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*43a90889SApple OSS Distributions */
28*43a90889SApple OSS Distributions /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
29*43a90889SApple OSS Distributions /*
30*43a90889SApple OSS Distributions * Copyright (c) 1982, 1986, 1989, 1993
31*43a90889SApple OSS Distributions * The Regents of the University of California. All rights reserved.
32*43a90889SApple OSS Distributions *
33*43a90889SApple OSS Distributions * Redistribution and use in source and binary forms, with or without
34*43a90889SApple OSS Distributions * modification, are permitted provided that the following conditions
35*43a90889SApple OSS Distributions * are met:
36*43a90889SApple OSS Distributions * 1. Redistributions of source code must retain the above copyright
37*43a90889SApple OSS Distributions * notice, this list of conditions and the following disclaimer.
38*43a90889SApple OSS Distributions * 2. Redistributions in binary form must reproduce the above copyright
39*43a90889SApple OSS Distributions * notice, this list of conditions and the following disclaimer in the
40*43a90889SApple OSS Distributions * documentation and/or other materials provided with the distribution.
41*43a90889SApple OSS Distributions * 3. All advertising materials mentioning features or use of this software
42*43a90889SApple OSS Distributions * must display the following acknowledgement:
43*43a90889SApple OSS Distributions * This product includes software developed by the University of
44*43a90889SApple OSS Distributions * California, Berkeley and its contributors.
45*43a90889SApple OSS Distributions * 4. Neither the name of the University nor the names of its contributors
46*43a90889SApple OSS Distributions * may be used to endorse or promote products derived from this software
47*43a90889SApple OSS Distributions * without specific prior written permission.
48*43a90889SApple OSS Distributions *
49*43a90889SApple OSS Distributions * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50*43a90889SApple OSS Distributions * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51*43a90889SApple OSS Distributions * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52*43a90889SApple OSS Distributions * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53*43a90889SApple OSS Distributions * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54*43a90889SApple OSS Distributions * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55*43a90889SApple OSS Distributions * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56*43a90889SApple OSS Distributions * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57*43a90889SApple OSS Distributions * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58*43a90889SApple OSS Distributions * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59*43a90889SApple OSS Distributions * SUCH DAMAGE.
60*43a90889SApple OSS Distributions *
61*43a90889SApple OSS Distributions * @(#)file.h 8.3 (Berkeley) 1/9/95
62*43a90889SApple OSS Distributions */
63*43a90889SApple OSS Distributions
64*43a90889SApple OSS Distributions #ifndef _SYS_FILE_INTERNAL_H_
65*43a90889SApple OSS Distributions #define _SYS_FILE_INTERNAL_H_
66*43a90889SApple OSS Distributions
67*43a90889SApple OSS Distributions #include <sys/appleapiopts.h>
68*43a90889SApple OSS Distributions #include <sys/fcntl.h>
69*43a90889SApple OSS Distributions #include <sys/unistd.h>
70*43a90889SApple OSS Distributions
71*43a90889SApple OSS Distributions #ifdef XNU_KERNEL_PRIVATE
72*43a90889SApple OSS Distributions #include <sys/errno.h>
73*43a90889SApple OSS Distributions #include <sys/queue.h>
74*43a90889SApple OSS Distributions #include <sys/cdefs.h>
75*43a90889SApple OSS Distributions #include <sys/constrained_ctypes.h>
76*43a90889SApple OSS Distributions #include <sys/lock.h>
77*43a90889SApple OSS Distributions #include <sys/file.h>
78*43a90889SApple OSS Distributions #include <sys/filedesc.h>
79*43a90889SApple OSS Distributions #include <sys/guarded.h>
80*43a90889SApple OSS Distributions #include <os/refcnt.h>
81*43a90889SApple OSS Distributions
82*43a90889SApple OSS Distributions __BEGIN_DECLS
83*43a90889SApple OSS Distributions
84*43a90889SApple OSS Distributions #pragma GCC visibility push(hidden)
85*43a90889SApple OSS Distributions
86*43a90889SApple OSS Distributions struct proc;
87*43a90889SApple OSS Distributions struct uio;
88*43a90889SApple OSS Distributions struct knote;
89*43a90889SApple OSS Distributions struct kevent_qos_s;
90*43a90889SApple OSS Distributions struct file;
91*43a90889SApple OSS Distributions #ifndef _KAUTH_CRED_T
92*43a90889SApple OSS Distributions #define _KAUTH_CRED_T
93*43a90889SApple OSS Distributions typedef struct ucred *kauth_cred_t;
94*43a90889SApple OSS Distributions typedef struct posix_cred *posix_cred_t;
95*43a90889SApple OSS Distributions #endif /* !_KAUTH_CRED_T */
96*43a90889SApple OSS Distributions
97*43a90889SApple OSS Distributions __options_decl(fileproc_vflags_t, unsigned int, {
98*43a90889SApple OSS Distributions FPV_NONE = 0,
99*43a90889SApple OSS Distributions FPV_DRAIN = 0x01,
100*43a90889SApple OSS Distributions });
101*43a90889SApple OSS Distributions
102*43a90889SApple OSS Distributions __options_decl(fileproc_flags_t, uint16_t, {
103*43a90889SApple OSS Distributions FP_NONE = 0,
104*43a90889SApple OSS Distributions FP_CLOEXEC = 0x01,
105*43a90889SApple OSS Distributions FP_CLOFORK = 0x02,
106*43a90889SApple OSS Distributions FP_INSELECT = 0x04,
107*43a90889SApple OSS Distributions FP_AIOISSUED = 0x08,
108*43a90889SApple OSS Distributions FP_SELCONFLICT = 0x10, /* select conflict on an individual fp */
109*43a90889SApple OSS Distributions });
110*43a90889SApple OSS Distributions
111*43a90889SApple OSS Distributions struct fileproc_guard {
112*43a90889SApple OSS Distributions struct select_set *fpg_wset;
113*43a90889SApple OSS Distributions guardid_t fpg_guard;
114*43a90889SApple OSS Distributions };
115*43a90889SApple OSS Distributions
116*43a90889SApple OSS Distributions /*
117*43a90889SApple OSS Distributions * Kernel descriptor table.
118*43a90889SApple OSS Distributions * One entry for each open kernel vnode and socket.
119*43a90889SApple OSS Distributions */
120*43a90889SApple OSS Distributions struct fileproc {
121*43a90889SApple OSS Distributions os_refcnt_t fp_iocount;
122*43a90889SApple OSS Distributions _Atomic fileproc_vflags_t fp_vflags;
123*43a90889SApple OSS Distributions fileproc_flags_t fp_flags;
124*43a90889SApple OSS Distributions uint16_t fp_guard_attrs;
125*43a90889SApple OSS Distributions struct fileglob *XNU_PTRAUTH_SIGNED_PTR("fileproc.fp_glob") fp_glob;
126*43a90889SApple OSS Distributions union {
127*43a90889SApple OSS Distributions struct select_set *fp_wset; /* fp_guard_attrs == 0 */
128*43a90889SApple OSS Distributions struct fileproc_guard *XNU_PTRAUTH_SIGNED_PTR("fileproc.fp_guard") fp_guard; /* fp_guard_attrs != 0 */
129*43a90889SApple OSS Distributions };
130*43a90889SApple OSS Distributions };
131*43a90889SApple OSS Distributions __CCT_DECLARE_CONSTRAINED_PTR_TYPES(struct fileproc, fileproc);
132*43a90889SApple OSS Distributions #define FILEPROC_NULL ((struct fileproc *)0)
133*43a90889SApple OSS Distributions
134*43a90889SApple OSS Distributions /* file types */
135*43a90889SApple OSS Distributions typedef enum {
136*43a90889SApple OSS Distributions DTYPE_VNODE = 1, /* file */
137*43a90889SApple OSS Distributions DTYPE_SOCKET, /* communications endpoint */
138*43a90889SApple OSS Distributions DTYPE_PSXSHM, /* POSIX Shared memory */
139*43a90889SApple OSS Distributions DTYPE_PSXSEM, /* POSIX Semaphores */
140*43a90889SApple OSS Distributions DTYPE_KQUEUE, /* kqueue */
141*43a90889SApple OSS Distributions DTYPE_PIPE, /* pipe */
142*43a90889SApple OSS Distributions DTYPE_FSEVENTS, /* fsevents */
143*43a90889SApple OSS Distributions DTYPE_ATALK, /* (obsolete) */
144*43a90889SApple OSS Distributions DTYPE_NETPOLICY, /* networking policy */
145*43a90889SApple OSS Distributions DTYPE_CHANNEL, /* Skywalk Channel */
146*43a90889SApple OSS Distributions DTYPE_NEXUS /* Skywalk Nexus */
147*43a90889SApple OSS Distributions } file_type_t;
148*43a90889SApple OSS Distributions
149*43a90889SApple OSS Distributions /* defines for fg_lflags */
150*43a90889SApple OSS Distributions // was FG_TERM 0x01
151*43a90889SApple OSS Distributions #define FG_INSMSGQ 0x02 /* insert to msgqueue pending .. */
152*43a90889SApple OSS Distributions #define FG_WINSMSGQ 0x04 /* wait for the fielglob is in msgque */
153*43a90889SApple OSS Distributions #define FG_RMMSGQ 0x08 /* the fileglob is being removed from msgqueue */
154*43a90889SApple OSS Distributions #define FG_WRMMSGQ 0x10 /* wait for the fileglob to be removed from msgqueue */
155*43a90889SApple OSS Distributions #define FG_PORTMADE 0x20 /* a port was at some point created for this fileglob */
156*43a90889SApple OSS Distributions #define FG_NOSIGPIPE 0x40 /* don't deliver SIGPIPE with EPIPE return */
157*43a90889SApple OSS Distributions #define FG_OFF_LOCKED 0x80 /* Used as a mutex for offset changes (for vnodes) */
158*43a90889SApple OSS Distributions #define FG_OFF_LOCKWANT 0x100 /* Somebody's wating for the lock */
159*43a90889SApple OSS Distributions #define FG_CONFINED 0x200 /* fileglob confined to process, immutably */
160*43a90889SApple OSS Distributions #define FG_HAS_OFDLOCK 0x400 /* Has or has had an OFD lock */
161*43a90889SApple OSS Distributions
162*43a90889SApple OSS Distributions struct fileops {
163*43a90889SApple OSS Distributions file_type_t fo_type; /* descriptor type */
164*43a90889SApple OSS Distributions int (*fo_read) (struct fileproc *fp, struct uio *uio,
165*43a90889SApple OSS Distributions int flags, vfs_context_t ctx);
166*43a90889SApple OSS Distributions int (*fo_write) (struct fileproc *fp, struct uio *uio,
167*43a90889SApple OSS Distributions int flags, vfs_context_t ctx);
168*43a90889SApple OSS Distributions #define FOF_OFFSET 0x00000001 /* offset supplied to vn_write */
169*43a90889SApple OSS Distributions int (*fo_ioctl)(struct fileproc *fp, u_long com,
170*43a90889SApple OSS Distributions caddr_t data, vfs_context_t ctx);
171*43a90889SApple OSS Distributions int (*fo_select) (struct fileproc *fp, int which,
172*43a90889SApple OSS Distributions void *wql, vfs_context_t ctx);
173*43a90889SApple OSS Distributions int (*fo_close) (struct fileglob *fg, vfs_context_t ctx);
174*43a90889SApple OSS Distributions int (*fo_kqfilter) (struct fileproc *fp, struct knote *, struct kevent_qos_s *);
175*43a90889SApple OSS Distributions int (*fo_drain) (struct fileproc *fp, vfs_context_t ctx);
176*43a90889SApple OSS Distributions };
177*43a90889SApple OSS Distributions
178*43a90889SApple OSS Distributions struct fileglob {
179*43a90889SApple OSS Distributions LIST_ENTRY(fileglob) f_msglist; /* list of files in unix messages */
180*43a90889SApple OSS Distributions uint32_t fg_flag; /* (atomic) see fcntl.h */
181*43a90889SApple OSS Distributions os_ref_atomic_t fg_count; /* reference count */
182*43a90889SApple OSS Distributions uint32_t fg_msgcount; /* references from message queue */
183*43a90889SApple OSS Distributions int32_t fg_lflags; /* file global flags */
184*43a90889SApple OSS Distributions kauth_cred_t XNU_PTRAUTH_SIGNED_PTR("fileglob.fg_cred") fg_cred; /* credentials associated with descriptor */
185*43a90889SApple OSS Distributions const struct fileops *XNU_PTRAUTH_SIGNED_PTR("fileglob.fg_ops") fg_ops;
186*43a90889SApple OSS Distributions off_t fg_offset;
187*43a90889SApple OSS Distributions uintptr_t fg_data; /* vnode or socket or SHM or semaphore */
188*43a90889SApple OSS Distributions struct fd_vn_data *XNU_PTRAUTH_SIGNED_PTR("fileglob.fg_vn_data") fg_vn_data; /* Per fd vnode data, used for directories */
189*43a90889SApple OSS Distributions lck_mtx_t fg_lock;
190*43a90889SApple OSS Distributions #if CONFIG_MACF && CONFIG_VNGUARD
191*43a90889SApple OSS Distributions struct vng_owner *fg_vgo; /* Used by the vnode guard MAC hook */
192*43a90889SApple OSS Distributions #endif
193*43a90889SApple OSS Distributions };
194*43a90889SApple OSS Distributions
195*43a90889SApple OSS Distributions /* Disambiguate OFD ids from flock ids (fileglobs) */
196*43a90889SApple OSS Distributions __pure2
197*43a90889SApple OSS Distributions static inline caddr_t __unsafe_indexable
ofd_to_id(const struct fileglob * fg)198*43a90889SApple OSS Distributions ofd_to_id(const struct fileglob *fg)
199*43a90889SApple OSS Distributions {
200*43a90889SApple OSS Distributions return (caddr_t __unsafe_indexable)~(uintptr_t)fg;
201*43a90889SApple OSS Distributions }
202*43a90889SApple OSS Distributions
203*43a90889SApple OSS Distributions extern int maxfiles; /* kernel limit on number of open files */
204*43a90889SApple OSS Distributions extern int nfiles; /* actual number of open files */
205*43a90889SApple OSS Distributions extern int maxfilesperproc;
206*43a90889SApple OSS Distributions os_refgrp_decl_extern(f_refgrp); /* os_refgrp_t for file refcounts */
207*43a90889SApple OSS Distributions
208*43a90889SApple OSS Distributions #define FILEGLOB_DTYPE(fg) ((const file_type_t)((fg)->fg_ops->fo_type))
209*43a90889SApple OSS Distributions
210*43a90889SApple OSS Distributions /* Special value to indicate "no process association". */
211*43a90889SApple OSS Distributions #define FG_NOPROC ((struct proc *)~0UL)
212*43a90889SApple OSS Distributions
213*43a90889SApple OSS Distributions #pragma mark files (struct fileglob)
214*43a90889SApple OSS Distributions
215*43a90889SApple OSS Distributions /*!
216*43a90889SApple OSS Distributions * @function fg_alloc_init
217*43a90889SApple OSS Distributions *
218*43a90889SApple OSS Distributions * @brief
219*43a90889SApple OSS Distributions * Allocate and minimally initialize a file structure.
220*43a90889SApple OSS Distributions *
221*43a90889SApple OSS Distributions * @description
222*43a90889SApple OSS Distributions * The fileglob is allocated as if with falloc_withinit(), but is not
223*43a90889SApple OSS Distributions * assocated with any fileproc.
224*43a90889SApple OSS Distributions */
225*43a90889SApple OSS Distributions struct fileglob *
226*43a90889SApple OSS Distributions fg_alloc_init(vfs_context_t ctx);
227*43a90889SApple OSS Distributions
228*43a90889SApple OSS Distributions /*!
229*43a90889SApple OSS Distributions * @function fg_ref
230*43a90889SApple OSS Distributions *
231*43a90889SApple OSS Distributions * @brief
232*43a90889SApple OSS Distributions * Acquire a file reference on the specified file.
233*43a90889SApple OSS Distributions *
234*43a90889SApple OSS Distributions * @description
235*43a90889SApple OSS Distributions * The @c proc must be locked while this operation is being performed
236*43a90889SApple OSS Distributions * to avoid races with setting the FG_CONFINED flag.
237*43a90889SApple OSS Distributions *
238*43a90889SApple OSS Distributions * @param proc
239*43a90889SApple OSS Distributions * The proc this file reference is taken on behalf of, or FG_NOPROC
240*43a90889SApple OSS Distributions * if the file is guaranteed to not be associated with any fileproc.
241*43a90889SApple OSS Distributions *
242*43a90889SApple OSS Distributions * @param fg
243*43a90889SApple OSS Distributions * The specified file
244*43a90889SApple OSS Distributions */
245*43a90889SApple OSS Distributions void
246*43a90889SApple OSS Distributions fg_ref(proc_t proc, struct fileglob *fg);
247*43a90889SApple OSS Distributions
248*43a90889SApple OSS Distributions /*!
249*43a90889SApple OSS Distributions * @function fg_drop_live
250*43a90889SApple OSS Distributions *
251*43a90889SApple OSS Distributions * @brief
252*43a90889SApple OSS Distributions * Drops a file reference on the specified file that isn't the last one.
253*43a90889SApple OSS Distributions *
254*43a90889SApple OSS Distributions * @param fg
255*43a90889SApple OSS Distributions * The file whose reference is being dropped.
256*43a90889SApple OSS Distributions */
257*43a90889SApple OSS Distributions void
258*43a90889SApple OSS Distributions fg_drop_live(struct fileglob *fg);
259*43a90889SApple OSS Distributions
260*43a90889SApple OSS Distributions /*!
261*43a90889SApple OSS Distributions * @function fg_drop
262*43a90889SApple OSS Distributions *
263*43a90889SApple OSS Distributions * @brief
264*43a90889SApple OSS Distributions * Drops a file reference on the specified file.
265*43a90889SApple OSS Distributions *
266*43a90889SApple OSS Distributions * @discussion
267*43a90889SApple OSS Distributions * No locks should be held when calling this function.
268*43a90889SApple OSS Distributions *
269*43a90889SApple OSS Distributions * @param p
270*43a90889SApple OSS Distributions * The process making the request,
271*43a90889SApple OSS Distributions * or FG_NOPROC if the file belongs to a message/fileport.
272*43a90889SApple OSS Distributions *
273*43a90889SApple OSS Distributions * @param fg
274*43a90889SApple OSS Distributions * The file being closed.
275*43a90889SApple OSS Distributions *
276*43a90889SApple OSS Distributions * @returns
277*43a90889SApple OSS Distributions * 0 Success
278*43a90889SApple OSS Distributions * ??? Any error that @c fileops::fo_close can return
279*43a90889SApple OSS Distributions */
280*43a90889SApple OSS Distributions int
281*43a90889SApple OSS Distributions fg_drop(proc_t p, struct fileglob *fg);
282*43a90889SApple OSS Distributions
283*43a90889SApple OSS Distributions /*!
284*43a90889SApple OSS Distributions * @function fg_sendable
285*43a90889SApple OSS Distributions *
286*43a90889SApple OSS Distributions * @brief
287*43a90889SApple OSS Distributions * Returns whether a particular file can be sent over IPC.
288*43a90889SApple OSS Distributions */
289*43a90889SApple OSS Distributions bool
290*43a90889SApple OSS Distributions fg_sendable(struct fileglob *fg);
291*43a90889SApple OSS Distributions
292*43a90889SApple OSS Distributions /*!
293*43a90889SApple OSS Distributions * @function fg_get_data_volatile
294*43a90889SApple OSS Distributions *
295*43a90889SApple OSS Distributions * @brief
296*43a90889SApple OSS Distributions * Returns the fileglob opaque data pointer.
297*43a90889SApple OSS Distributions *
298*43a90889SApple OSS Distributions * @discussion
299*43a90889SApple OSS Distributions * Unlike @c fg_get_data() this variant will
300*43a90889SApple OSS Distributions * not be hoisted by the compiler.
301*43a90889SApple OSS Distributions *
302*43a90889SApple OSS Distributions * @param fg
303*43a90889SApple OSS Distributions * The file whose data is being requested.
304*43a90889SApple OSS Distributions */
305*43a90889SApple OSS Distributions void *
306*43a90889SApple OSS Distributions fg_get_data_volatile(struct fileglob *fg);
307*43a90889SApple OSS Distributions
308*43a90889SApple OSS Distributions /*!
309*43a90889SApple OSS Distributions * @function fg_get_data
310*43a90889SApple OSS Distributions *
311*43a90889SApple OSS Distributions * @brief
312*43a90889SApple OSS Distributions * Returns the fileglob opaque data pointer.
313*43a90889SApple OSS Distributions *
314*43a90889SApple OSS Distributions * @discussion
315*43a90889SApple OSS Distributions * Unlike @c fg_get_data_volatile() this variant
316*43a90889SApple OSS Distributions * will be hoisted by the compiler if it is called
317*43a90889SApple OSS Distributions * repeatedly.
318*43a90889SApple OSS Distributions *
319*43a90889SApple OSS Distributions * @param fg
320*43a90889SApple OSS Distributions * The file whose data is being requested.
321*43a90889SApple OSS Distributions */
322*43a90889SApple OSS Distributions __pure2
323*43a90889SApple OSS Distributions static inline void *
fg_get_data(struct fileglob * fg)324*43a90889SApple OSS Distributions fg_get_data(struct fileglob *fg)
325*43a90889SApple OSS Distributions {
326*43a90889SApple OSS Distributions return fg_get_data_volatile(fg);
327*43a90889SApple OSS Distributions }
328*43a90889SApple OSS Distributions
329*43a90889SApple OSS Distributions /*!
330*43a90889SApple OSS Distributions * @function fg_set_data
331*43a90889SApple OSS Distributions *
332*43a90889SApple OSS Distributions * @brief
333*43a90889SApple OSS Distributions * Sets the fileglob opaque data pointer.
334*43a90889SApple OSS Distributions *
335*43a90889SApple OSS Distributions * @param fg
336*43a90889SApple OSS Distributions * The file whose data is being set.
337*43a90889SApple OSS Distributions *
338*43a90889SApple OSS Distributions * @param fg_data
339*43a90889SApple OSS Distributions * Opaque file data value
340*43a90889SApple OSS Distributions */
341*43a90889SApple OSS Distributions void
342*43a90889SApple OSS Distributions fg_set_data(struct fileglob *fg, void *fg_data);
343*43a90889SApple OSS Distributions
344*43a90889SApple OSS Distributions #pragma mark file descriptor entries (struct fileproc)
345*43a90889SApple OSS Distributions
346*43a90889SApple OSS Distributions /*!
347*43a90889SApple OSS Distributions * @function fg_get_data
348*43a90889SApple OSS Distributions *
349*43a90889SApple OSS Distributions * @brief
350*43a90889SApple OSS Distributions * Returns the fileproc fileglob opaque data pointer.
351*43a90889SApple OSS Distributions *
352*43a90889SApple OSS Distributions * @discussion
353*43a90889SApple OSS Distributions * Unlike @c fp_get_data_volatile() this variant
354*43a90889SApple OSS Distributions * will be hoisted by the compiler if it is called
355*43a90889SApple OSS Distributions * repeatedly.
356*43a90889SApple OSS Distributions *
357*43a90889SApple OSS Distributions * @param fp
358*43a90889SApple OSS Distributions * The fileproc whose data is being requested.
359*43a90889SApple OSS Distributions */
360*43a90889SApple OSS Distributions __pure2
361*43a90889SApple OSS Distributions static inline void *
fp_get_data(struct fileproc * fp)362*43a90889SApple OSS Distributions fp_get_data(struct fileproc *fp)
363*43a90889SApple OSS Distributions {
364*43a90889SApple OSS Distributions return fg_get_data(fp->fp_glob);
365*43a90889SApple OSS Distributions }
366*43a90889SApple OSS Distributions
367*43a90889SApple OSS Distributions /*!
368*43a90889SApple OSS Distributions * @function fp_get_data
369*43a90889SApple OSS Distributions *
370*43a90889SApple OSS Distributions * @brief
371*43a90889SApple OSS Distributions * Returns the fileproc fileglob opaque data pointer.
372*43a90889SApple OSS Distributions *
373*43a90889SApple OSS Distributions * @discussion
374*43a90889SApple OSS Distributions * Unlike @c fp_get_data() this variant will
375*43a90889SApple OSS Distributions * not be hoisted by the compiler.
376*43a90889SApple OSS Distributions *
377*43a90889SApple OSS Distributions * @param fp
378*43a90889SApple OSS Distributions * The fileproc whose data is being requested.
379*43a90889SApple OSS Distributions */
380*43a90889SApple OSS Distributions static inline void *
fp_get_data_volatile(struct fileproc * fp)381*43a90889SApple OSS Distributions fp_get_data_volatile(struct fileproc *fp)
382*43a90889SApple OSS Distributions {
383*43a90889SApple OSS Distributions return fg_get_data_volatile(fp->fp_glob);
384*43a90889SApple OSS Distributions }
385*43a90889SApple OSS Distributions
386*43a90889SApple OSS Distributions /*!
387*43a90889SApple OSS Distributions * @function fp_set_data
388*43a90889SApple OSS Distributions *
389*43a90889SApple OSS Distributions * @brief
390*43a90889SApple OSS Distributions * Sets the fileproc opaque data pointer.
391*43a90889SApple OSS Distributions *
392*43a90889SApple OSS Distributions * @param fp
393*43a90889SApple OSS Distributions * The fileproc whose data is being set.
394*43a90889SApple OSS Distributions *
395*43a90889SApple OSS Distributions * @param fg_data
396*43a90889SApple OSS Distributions * Opaque file data value
397*43a90889SApple OSS Distributions */
398*43a90889SApple OSS Distributions static inline void
fp_set_data(struct fileproc * fp,void * fg_data)399*43a90889SApple OSS Distributions fp_set_data(struct fileproc *fp, void *fg_data)
400*43a90889SApple OSS Distributions {
401*43a90889SApple OSS Distributions fg_set_data(fp->fp_glob, fg_data);
402*43a90889SApple OSS Distributions }
403*43a90889SApple OSS Distributions
404*43a90889SApple OSS Distributions /*!
405*43a90889SApple OSS Distributions * @function fp_get_ftype
406*43a90889SApple OSS Distributions *
407*43a90889SApple OSS Distributions * @brief
408*43a90889SApple OSS Distributions * Get the fileproc pointer for the given fd from the per process, with the
409*43a90889SApple OSS Distributions * specified file type, and with an I/O reference.
410*43a90889SApple OSS Distributions *
411*43a90889SApple OSS Distributions * @param p
412*43a90889SApple OSS Distributions * The process in which fd lives.
413*43a90889SApple OSS Distributions *
414*43a90889SApple OSS Distributions * @param fd
415*43a90889SApple OSS Distributions * The file descriptor index to lookup.
416*43a90889SApple OSS Distributions *
417*43a90889SApple OSS Distributions * @param ftype
418*43a90889SApple OSS Distributions * The required file type.
419*43a90889SApple OSS Distributions *
420*43a90889SApple OSS Distributions * @param err
421*43a90889SApple OSS Distributions * The error to return if the file exists but isn't of the specified type.
422*43a90889SApple OSS Distributions *
423*43a90889SApple OSS Distributions * @param fpp
424*43a90889SApple OSS Distributions * The returned fileproc when the call returns 0.
425*43a90889SApple OSS Distributions *
426*43a90889SApple OSS Distributions * @returns
427*43a90889SApple OSS Distributions * 0 Success (@c fpp is set)
428*43a90889SApple OSS Distributions * EBADF Bad file descriptor
429*43a90889SApple OSS Distributions * @c err There is an entry, but it isn't of the specified type.
430*43a90889SApple OSS Distributions */
431*43a90889SApple OSS Distributions extern int
432*43a90889SApple OSS Distributions fp_get_ftype(proc_t p, int fd, file_type_t ftype, int err, struct fileproc **fpp);
433*43a90889SApple OSS Distributions
434*43a90889SApple OSS Distributions /*!
435*43a90889SApple OSS Distributions * @function fp_get_noref_locked
436*43a90889SApple OSS Distributions *
437*43a90889SApple OSS Distributions * @brief
438*43a90889SApple OSS Distributions * Get the fileproc pointer for the given fd from the per process
439*43a90889SApple OSS Distributions * open file table without taking an explicit reference on it.
440*43a90889SApple OSS Distributions *
441*43a90889SApple OSS Distributions * @description
442*43a90889SApple OSS Distributions * This function assumes that the @c proc_fdlock is held, as the caller
443*43a90889SApple OSS Distributions * doesn't hold an I/O reference for the returned fileproc.
444*43a90889SApple OSS Distributions *
445*43a90889SApple OSS Distributions * Because there is no reference explicitly taken, the returned
446*43a90889SApple OSS Distributions * fileproc pointer is only valid so long as the @c proc_fdlock
447*43a90889SApple OSS Distributions * remains held by the caller.
448*43a90889SApple OSS Distributions *
449*43a90889SApple OSS Distributions * @param p
450*43a90889SApple OSS Distributions * The process in which fd lives.
451*43a90889SApple OSS Distributions *
452*43a90889SApple OSS Distributions * @param fd
453*43a90889SApple OSS Distributions * The file descriptor index to lookup.
454*43a90889SApple OSS Distributions *
455*43a90889SApple OSS Distributions * @returns
456*43a90889SApple OSS Distributions * - the fileproc on success
457*43a90889SApple OSS Distributions * - FILEPROC_NULL on error
458*43a90889SApple OSS Distributions */
459*43a90889SApple OSS Distributions extern struct fileproc *
460*43a90889SApple OSS Distributions fp_get_noref_locked(proc_t p, int fd);
461*43a90889SApple OSS Distributions
462*43a90889SApple OSS Distributions /*!
463*43a90889SApple OSS Distributions * @function fp_get_noref_locked_with_iocount
464*43a90889SApple OSS Distributions *
465*43a90889SApple OSS Distributions * @brief
466*43a90889SApple OSS Distributions * Similar to fp_get_noref_locked(), but allows returning files that are
467*43a90889SApple OSS Distributions * closing.
468*43a90889SApple OSS Distributions *
469*43a90889SApple OSS Distributions * @discussion
470*43a90889SApple OSS Distributions * Some parts of the kernel take I/O references on fileprocs but only remember
471*43a90889SApple OSS Distributions * the file descriptor for which they did that.
472*43a90889SApple OSS Distributions *
473*43a90889SApple OSS Distributions * These interfaces later need to drop that reference, but if the file is
474*43a90889SApple OSS Distributions * already closing, then fp_get_noref_locked() will refuse to resolve
475*43a90889SApple OSS Distributions * the file descriptor.
476*43a90889SApple OSS Distributions *
477*43a90889SApple OSS Distributions * This interface allows the lookup (but will assert that the fileproc has
478*43a90889SApple OSS Distributions * enouhg I/O references).
479*43a90889SApple OSS Distributions *
480*43a90889SApple OSS Distributions * @warning
481*43a90889SApple OSS Distributions * New code should NOT use this function, it is required for interfaces
482*43a90889SApple OSS Distributions * that acquire iocounts without remembering the fileproc pointer,
483*43a90889SApple OSS Distributions * which is bad practice.
484*43a90889SApple OSS Distributions */
485*43a90889SApple OSS Distributions extern struct fileproc *
486*43a90889SApple OSS Distributions fp_get_noref_locked_with_iocount(proc_t p, int fd);
487*43a90889SApple OSS Distributions
488*43a90889SApple OSS Distributions /*!
489*43a90889SApple OSS Distributions * @function fp_close_and_unlock
490*43a90889SApple OSS Distributions *
491*43a90889SApple OSS Distributions * @brief
492*43a90889SApple OSS Distributions * Close the given file descriptor entry.
493*43a90889SApple OSS Distributions *
494*43a90889SApple OSS Distributions * @description
495*43a90889SApple OSS Distributions * This function assumes that the @c proc_fdlock is held,
496*43a90889SApple OSS Distributions * and that the caller holds no additional I/O reference
497*43a90889SApple OSS Distributions * on the specified file descriptor entry.
498*43a90889SApple OSS Distributions *
499*43a90889SApple OSS Distributions * The @c proc_fdlock is unlocked upon return.
500*43a90889SApple OSS Distributions *
501*43a90889SApple OSS Distributions * @param p The process in which fd lives.
502*43a90889SApple OSS Distributions * @param p_cred The proc's cred for this operation.
503*43a90889SApple OSS Distributions * @param fd The file descriptor index being closed.
504*43a90889SApple OSS Distributions * @param fp The fileproc entry associated with @c fd.
505*43a90889SApple OSS Distributions * @param flags FD_DUP2RESV or 0.
506*43a90889SApple OSS Distributions *
507*43a90889SApple OSS Distributions * @returns
508*43a90889SApple OSS Distributions * 0 Success
509*43a90889SApple OSS Distributions * EBADF Bad file descriptor
510*43a90889SApple OSS Distributions * ??? Any error that @c fileops::fo_close can return.
511*43a90889SApple OSS Distributions */
512*43a90889SApple OSS Distributions extern int
513*43a90889SApple OSS Distributions fp_close_and_unlock(proc_t p, kauth_cred_t p_cred, int fd, struct fileproc *fp, int flags);
514*43a90889SApple OSS Distributions
515*43a90889SApple OSS Distributions /* wrappers for fp->f_ops->fo_... */
516*43a90889SApple OSS Distributions int fo_read(struct fileproc *fp, struct uio *uio, int flags, vfs_context_t ctx);
517*43a90889SApple OSS Distributions int fo_write(struct fileproc *fp, struct uio *uio, int flags,
518*43a90889SApple OSS Distributions vfs_context_t ctx);
519*43a90889SApple OSS Distributions int fo_ioctl(struct fileproc *fp, u_long com, caddr_t data, vfs_context_t ctx);
520*43a90889SApple OSS Distributions int fo_select(struct fileproc *fp, int which, void *wql, vfs_context_t ctx);
521*43a90889SApple OSS Distributions int fo_close(struct fileglob *fg, vfs_context_t ctx);
522*43a90889SApple OSS Distributions int fo_drain(struct fileproc *fp, vfs_context_t ctx);
523*43a90889SApple OSS Distributions int fo_kqfilter(struct fileproc *fp, struct knote *kn, struct kevent_qos_s *kev);
524*43a90889SApple OSS Distributions
525*43a90889SApple OSS Distributions /* Functions to use for unsupported fileops */
526*43a90889SApple OSS Distributions int fo_no_read(struct fileproc *fp, struct uio *uio, int flags, vfs_context_t ctx);
527*43a90889SApple OSS Distributions int fo_no_write(struct fileproc *fp, struct uio *uio, int flags,
528*43a90889SApple OSS Distributions vfs_context_t ctx);
529*43a90889SApple OSS Distributions int fo_no_ioctl(struct fileproc *fp, u_long com, caddr_t data, vfs_context_t ctx);
530*43a90889SApple OSS Distributions int fo_no_select(struct fileproc *fp, int which, void *wql, vfs_context_t ctx);
531*43a90889SApple OSS Distributions int fo_no_drain(struct fileproc *fp, vfs_context_t ctx);
532*43a90889SApple OSS Distributions int fo_no_kqfilter(struct fileproc *, struct knote *, struct kevent_qos_s *kev);
533*43a90889SApple OSS Distributions
534*43a90889SApple OSS Distributions int fp_tryswap(proc_t, int fd, struct fileproc *nfp);
535*43a90889SApple OSS Distributions int fp_drop(struct proc *p, int fd, struct fileproc *fp, int locked);
536*43a90889SApple OSS Distributions void fp_free(struct proc * p, int fd, struct fileproc * fp);
537*43a90889SApple OSS Distributions int fp_lookup(struct proc *p, int fd, struct fileproc **resultfp, int locked);
538*43a90889SApple OSS Distributions int fp_lookup_guarded(struct proc *p, int fd, guardid_t guard, struct fileproc **resultfp, int locked);
539*43a90889SApple OSS Distributions int fp_isguarded(struct fileproc *fp, u_int attribs);
540*43a90889SApple OSS Distributions int fp_guard_exception(proc_t p, int fd, struct fileproc *fp, u_int attribs);
541*43a90889SApple OSS Distributions struct nameidata;
542*43a90889SApple OSS Distributions struct vnode_attr;
543*43a90889SApple OSS Distributions int open1(vfs_context_t ctx, struct nameidata *ndp, int uflags,
544*43a90889SApple OSS Distributions struct vnode_attr *vap, fp_initfn_t fp_init, void *initarg,
545*43a90889SApple OSS Distributions int32_t *retval, int authfd);
546*43a90889SApple OSS Distributions int chdir_internal(proc_t p, vfs_context_t ctx, struct nameidata *ndp, int per_thread);
547*43a90889SApple OSS Distributions int kqueue_internal(struct proc *p, fp_initfn_t, void *initarg, int32_t *retval);
548*43a90889SApple OSS Distributions void procfdtbl_releasefd(struct proc * p, int fd, struct fileproc * fp);
549*43a90889SApple OSS Distributions extern struct fileproc *fileproc_alloc_init(void);
550*43a90889SApple OSS Distributions extern void fileproc_free(struct fileproc *fp);
551*43a90889SApple OSS Distributions extern void guarded_fileproc_copy_guard(struct fileproc *ofp, struct fileproc *nfp);
552*43a90889SApple OSS Distributions extern void guarded_fileproc_unguard(struct fileproc *fp);
553*43a90889SApple OSS Distributions extern void fg_vn_data_free(void *fgvndata);
554*43a90889SApple OSS Distributions extern int nameiat(struct nameidata *ndp, int dirfd);
555*43a90889SApple OSS Distributions extern void vn_offset_lock(struct fileglob *fg);
556*43a90889SApple OSS Distributions extern void vn_offset_unlock(struct fileglob *fg);
557*43a90889SApple OSS Distributions extern int falloc_guarded(struct proc *p, struct fileproc **fp, int *fd,
558*43a90889SApple OSS Distributions vfs_context_t ctx, const guardid_t *guard, u_int attrs);
559*43a90889SApple OSS Distributions extern void fileproc_modify_vflags(struct fileproc *fp, fileproc_vflags_t vflags, boolean_t clearflags);
560*43a90889SApple OSS Distributions fileproc_vflags_t fileproc_get_vflags(struct fileproc *fp);
561*43a90889SApple OSS Distributions
562*43a90889SApple OSS Distributions #pragma mark internal version of syscalls
563*43a90889SApple OSS Distributions
564*43a90889SApple OSS Distributions int fileport_makefd(proc_t p, ipc_port_t port, fileproc_flags_t fp_flags, int *fd);
565*43a90889SApple OSS Distributions int dup2(proc_t p, kauth_cred_t p_cred, int from, int to, int *fd);
566*43a90889SApple OSS Distributions int close_nocancel(proc_t p, kauth_cred_t p_cred, int fd);
567*43a90889SApple OSS Distributions int fchdir(proc_t p, vfs_context_t ctx, int fd, bool per_thread);
568*43a90889SApple OSS Distributions
569*43a90889SApple OSS Distributions #pragma GCC visibility pop
570*43a90889SApple OSS Distributions
571*43a90889SApple OSS Distributions __END_DECLS
572*43a90889SApple OSS Distributions
573*43a90889SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
574*43a90889SApple OSS Distributions
575*43a90889SApple OSS Distributions #endif /* !_SYS_FILE_INTERNAL_H_ */
576