xref: /f-stack/freebsd/sys/filedesc.h (revision 22ce4aff)
1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-3-Clause
3*22ce4affSfengbojiang  *
4a9643ea8Slogwang  * Copyright (c) 1990, 1993
5a9643ea8Slogwang  *	The Regents of the University of California.  All rights reserved.
6a9643ea8Slogwang  *
7a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
8a9643ea8Slogwang  * modification, are permitted provided that the following conditions
9a9643ea8Slogwang  * are met:
10a9643ea8Slogwang  * 1. Redistributions of source code must retain the above copyright
11a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer.
12a9643ea8Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
13a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer in the
14a9643ea8Slogwang  *    documentation and/or other materials provided with the distribution.
15*22ce4affSfengbojiang  * 3. Neither the name of the University nor the names of its contributors
16a9643ea8Slogwang  *    may be used to endorse or promote products derived from this software
17a9643ea8Slogwang  *    without specific prior written permission.
18a9643ea8Slogwang  *
19a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20a9643ea8Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21a9643ea8Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22a9643ea8Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23a9643ea8Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24a9643ea8Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25a9643ea8Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26a9643ea8Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27a9643ea8Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28a9643ea8Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29a9643ea8Slogwang  * SUCH DAMAGE.
30a9643ea8Slogwang  *
31a9643ea8Slogwang  *	@(#)filedesc.h	8.1 (Berkeley) 6/2/93
32a9643ea8Slogwang  * $FreeBSD$
33a9643ea8Slogwang  */
34a9643ea8Slogwang 
35a9643ea8Slogwang #ifndef _SYS_FILEDESC_H_
36a9643ea8Slogwang #define	_SYS_FILEDESC_H_
37a9643ea8Slogwang 
38a9643ea8Slogwang #include <sys/caprights.h>
39a9643ea8Slogwang #include <sys/queue.h>
40a9643ea8Slogwang #include <sys/event.h>
41a9643ea8Slogwang #include <sys/lock.h>
42*22ce4affSfengbojiang #include <sys/mutex.h>
43a9643ea8Slogwang #include <sys/priority.h>
44*22ce4affSfengbojiang #include <sys/seqc.h>
45a9643ea8Slogwang #include <sys/sx.h>
46*22ce4affSfengbojiang #include <sys/_smr.h>
47*22ce4affSfengbojiang #include <sys/smr_types.h>
48a9643ea8Slogwang 
49a9643ea8Slogwang #include <machine/_limits.h>
50a9643ea8Slogwang 
51a9643ea8Slogwang struct filecaps {
52a9643ea8Slogwang 	cap_rights_t	 fc_rights;	/* per-descriptor capability rights */
53a9643ea8Slogwang 	u_long		*fc_ioctls;	/* per-descriptor allowed ioctls */
54a9643ea8Slogwang 	int16_t		 fc_nioctls;	/* fc_ioctls array size */
55a9643ea8Slogwang 	uint32_t	 fc_fcntls;	/* per-descriptor allowed fcntls */
56a9643ea8Slogwang };
57a9643ea8Slogwang 
58a9643ea8Slogwang struct filedescent {
59a9643ea8Slogwang 	struct file	*fde_file;	/* file structure for open file */
60a9643ea8Slogwang 	struct filecaps	 fde_caps;	/* per-descriptor rights */
61a9643ea8Slogwang 	uint8_t		 fde_flags;	/* per-process open file flags */
62*22ce4affSfengbojiang 	seqc_t		 fde_seqc;	/* keep file and caps in sync */
63a9643ea8Slogwang };
64a9643ea8Slogwang #define	fde_rights	fde_caps.fc_rights
65a9643ea8Slogwang #define	fde_fcntls	fde_caps.fc_fcntls
66a9643ea8Slogwang #define	fde_ioctls	fde_caps.fc_ioctls
67a9643ea8Slogwang #define	fde_nioctls	fde_caps.fc_nioctls
68*22ce4affSfengbojiang #define	fde_change_size	(offsetof(struct filedescent, fde_seqc))
69a9643ea8Slogwang 
70a9643ea8Slogwang struct fdescenttbl {
71a9643ea8Slogwang 	int	fdt_nfiles;		/* number of open files allocated */
72a9643ea8Slogwang 	struct	filedescent fdt_ofiles[0];	/* open files */
73a9643ea8Slogwang };
74*22ce4affSfengbojiang #define	fd_seqc(fdt, fd)	(&(fdt)->fdt_ofiles[(fd)].fde_seqc)
75a9643ea8Slogwang 
76a9643ea8Slogwang /*
77a9643ea8Slogwang  * This structure is used for the management of descriptors.  It may be
78a9643ea8Slogwang  * shared by multiple processes.
79a9643ea8Slogwang  */
80a9643ea8Slogwang #define NDSLOTTYPE	u_long
81a9643ea8Slogwang 
82*22ce4affSfengbojiang /*
83*22ce4affSfengbojiang  * This struct is copy-on-write and allocated from an SMR zone.
84*22ce4affSfengbojiang  * All fields are constant after initialization apart from the reference count.
85*22ce4affSfengbojiang  *
86*22ce4affSfengbojiang  * Check pwd_* routines for usage.
87*22ce4affSfengbojiang  */
88*22ce4affSfengbojiang struct pwd {
89*22ce4affSfengbojiang 	volatile u_int pwd_refcount;
90*22ce4affSfengbojiang 	struct	vnode *pwd_cdir;		/* current directory */
91*22ce4affSfengbojiang 	struct	vnode *pwd_rdir;		/* root directory */
92*22ce4affSfengbojiang 	struct	vnode *pwd_jdir;		/* jail root directory */
93*22ce4affSfengbojiang };
94*22ce4affSfengbojiang typedef SMR_POINTER(struct pwd *) smrpwd_t;
95*22ce4affSfengbojiang 
96*22ce4affSfengbojiang struct pwddesc {
97*22ce4affSfengbojiang 	struct mtx	pd_lock;	/* protects members of this struct */
98*22ce4affSfengbojiang 	smrpwd_t	pd_pwd;		/* directories */
99*22ce4affSfengbojiang 	volatile u_int	pd_refcount;
100*22ce4affSfengbojiang 	u_short		pd_cmask;	/* mask for file creation */
101*22ce4affSfengbojiang };
102*22ce4affSfengbojiang 
103a9643ea8Slogwang struct filedesc {
104a9643ea8Slogwang 	struct	fdescenttbl *fd_files;	/* open files table */
105a9643ea8Slogwang 	NDSLOTTYPE *fd_map;		/* bitmap of free fds */
106a9643ea8Slogwang 	int	fd_freefile;		/* approx. next free file */
107a9643ea8Slogwang 	int	fd_refcnt;		/* thread reference count */
108a9643ea8Slogwang 	int	fd_holdcnt;		/* hold count on structure + mutex */
109a9643ea8Slogwang 	struct	sx fd_sx;		/* protects members of this struct */
110a9643ea8Slogwang 	struct	kqlist fd_kqlist;	/* list of kqueues on this filedesc */
111a9643ea8Slogwang 	int	fd_holdleaderscount;	/* block fdfree() for shared close() */
112a9643ea8Slogwang 	int	fd_holdleaderswakeup;	/* fdfree() needs wakeup */
113a9643ea8Slogwang };
114a9643ea8Slogwang 
115a9643ea8Slogwang /*
116a9643ea8Slogwang  * Structure to keep track of (process leader, struct fildedesc) tuples.
117a9643ea8Slogwang  * Each process has a pointer to such a structure when detailed tracking
118a9643ea8Slogwang  * is needed, e.g., when rfork(RFPROC | RFMEM) causes a file descriptor
119a9643ea8Slogwang  * table to be shared by processes having different "p_leader" pointers
120a9643ea8Slogwang  * and thus distinct POSIX style locks.
121a9643ea8Slogwang  *
122a9643ea8Slogwang  * fdl_refcount and fdl_holdcount are protected by struct filedesc mtx.
123a9643ea8Slogwang  */
124a9643ea8Slogwang struct filedesc_to_leader {
125a9643ea8Slogwang 	int		fdl_refcount;	/* references from struct proc */
126a9643ea8Slogwang 	int		fdl_holdcount;	/* temporary hold during closef */
127a9643ea8Slogwang 	int		fdl_wakeup;	/* fdfree() waits on closef() */
128a9643ea8Slogwang 	struct proc	*fdl_leader;	/* owner of POSIX locks */
129a9643ea8Slogwang 	/* Circular list: */
130a9643ea8Slogwang 	struct filedesc_to_leader *fdl_prev;
131a9643ea8Slogwang 	struct filedesc_to_leader *fdl_next;
132a9643ea8Slogwang };
133a9643ea8Slogwang #define	fd_nfiles	fd_files->fdt_nfiles
134a9643ea8Slogwang #define	fd_ofiles	fd_files->fdt_ofiles
135a9643ea8Slogwang 
136a9643ea8Slogwang /*
137a9643ea8Slogwang  * Per-process open flags.
138a9643ea8Slogwang  */
139a9643ea8Slogwang #define	UF_EXCLOSE	0x01		/* auto-close on exec */
140a9643ea8Slogwang 
141a9643ea8Slogwang #ifdef _KERNEL
142a9643ea8Slogwang 
143*22ce4affSfengbojiang /* Lock a paths descriptor table. */
144*22ce4affSfengbojiang #define	PWDDESC_LOCK(pdp)	(&(pdp)->pd_lock)
145*22ce4affSfengbojiang #define	PWDDESC_LOCK_INIT(pdp) \
146*22ce4affSfengbojiang     mtx_init(PWDDESC_LOCK(pdp), "pwddesc", NULL, MTX_DEF)
147*22ce4affSfengbojiang #define	PWDDESC_LOCK_DESTROY(pdp)	mtx_destroy(PWDDESC_LOCK(pdp))
148*22ce4affSfengbojiang #define	PWDDESC_XLOCK(pdp)	mtx_lock(PWDDESC_LOCK(pdp))
149*22ce4affSfengbojiang #define	PWDDESC_XUNLOCK(pdp)	mtx_unlock(PWDDESC_LOCK(pdp))
150*22ce4affSfengbojiang #define	PWDDESC_LOCK_ASSERT(pdp, what) \
151*22ce4affSfengbojiang     mtx_assert(PWDDESC_LOCK(pdp), (what))
152*22ce4affSfengbojiang #define	PWDDESC_ASSERT_XLOCKED(pdp) \
153*22ce4affSfengbojiang     PWDDESC_LOCK_ASSERT((pdp), MA_OWNED)
154*22ce4affSfengbojiang #define	PWDDESC_ASSERT_UNLOCKED(pdp) \
155*22ce4affSfengbojiang     PWDDESC_LOCK_ASSERT((pdp), MA_NOTOWNED)
156*22ce4affSfengbojiang 
157*22ce4affSfengbojiang #define	PWDDESC_XLOCKED_LOAD_PWD(pdp)	({					\
158*22ce4affSfengbojiang 	struct pwddesc *_pdp = (pdp);						\
159*22ce4affSfengbojiang 	struct pwd *_pwd;							\
160*22ce4affSfengbojiang 	_pwd = smr_serialized_load(&(_pdp)->pd_pwd,				\
161*22ce4affSfengbojiang 	    (PWDDESC_ASSERT_XLOCKED(_pdp), true));				\
162*22ce4affSfengbojiang 	_pwd;									\
163*22ce4affSfengbojiang })
164*22ce4affSfengbojiang 
165a9643ea8Slogwang /* Lock a file descriptor table. */
166a9643ea8Slogwang #define	FILEDESC_LOCK_INIT(fdp)	sx_init(&(fdp)->fd_sx, "filedesc structure")
167a9643ea8Slogwang #define	FILEDESC_LOCK_DESTROY(fdp)	sx_destroy(&(fdp)->fd_sx)
168a9643ea8Slogwang #define	FILEDESC_LOCK(fdp)	(&(fdp)->fd_sx)
169a9643ea8Slogwang #define	FILEDESC_XLOCK(fdp)	sx_xlock(&(fdp)->fd_sx)
170a9643ea8Slogwang #define	FILEDESC_XUNLOCK(fdp)	sx_xunlock(&(fdp)->fd_sx)
171a9643ea8Slogwang #define	FILEDESC_SLOCK(fdp)	sx_slock(&(fdp)->fd_sx)
172a9643ea8Slogwang #define	FILEDESC_SUNLOCK(fdp)	sx_sunlock(&(fdp)->fd_sx)
173a9643ea8Slogwang 
174a9643ea8Slogwang #define	FILEDESC_LOCK_ASSERT(fdp)	sx_assert(&(fdp)->fd_sx, SX_LOCKED | \
175a9643ea8Slogwang 					    SX_NOTRECURSED)
176a9643ea8Slogwang #define	FILEDESC_XLOCK_ASSERT(fdp)	sx_assert(&(fdp)->fd_sx, SX_XLOCKED | \
177a9643ea8Slogwang 					    SX_NOTRECURSED)
178a9643ea8Slogwang #define	FILEDESC_UNLOCK_ASSERT(fdp)	sx_assert(&(fdp)->fd_sx, SX_UNLOCKED)
179a9643ea8Slogwang 
180*22ce4affSfengbojiang #define	FILEDESC_IS_ONLY_USER(fdp)	({					\
181*22ce4affSfengbojiang 	struct filedesc *_fdp = (fdp);						\
182*22ce4affSfengbojiang 	MPASS(curproc->p_fd == _fdp);						\
183*22ce4affSfengbojiang 	(curproc->p_numthreads == 1 && refcount_load(&_fdp->fd_refcnt) == 1);	\
184*22ce4affSfengbojiang })
185*22ce4affSfengbojiang #else
186*22ce4affSfengbojiang 
187*22ce4affSfengbojiang /*
188*22ce4affSfengbojiang  * Accessor for libkvm et al.
189*22ce4affSfengbojiang  */
190*22ce4affSfengbojiang #define	PWDDESC_KVM_LOAD_PWD(pdp)	({					\
191*22ce4affSfengbojiang 	struct pwddesc *_pdp = (pdp);						\
192*22ce4affSfengbojiang 	struct pwd *_pwd;							\
193*22ce4affSfengbojiang 	_pwd = smr_kvm_load(&(_pdp)->pd_pwd);					\
194*22ce4affSfengbojiang 	_pwd;									\
195*22ce4affSfengbojiang })
196*22ce4affSfengbojiang 
197*22ce4affSfengbojiang #endif
198*22ce4affSfengbojiang 
199*22ce4affSfengbojiang #ifdef _KERNEL
200*22ce4affSfengbojiang 
201a9643ea8Slogwang /* Operation types for kern_dup(). */
202a9643ea8Slogwang enum {
203a9643ea8Slogwang 	FDDUP_NORMAL,		/* dup() behavior. */
204a9643ea8Slogwang 	FDDUP_FCNTL,		/* fcntl()-style errors. */
205a9643ea8Slogwang 	FDDUP_FIXED,		/* Force fixed allocation. */
206a9643ea8Slogwang 	FDDUP_MUSTREPLACE,	/* Target must exist. */
207a9643ea8Slogwang 	FDDUP_LASTMODE,
208a9643ea8Slogwang };
209a9643ea8Slogwang 
210a9643ea8Slogwang /* Flags for kern_dup(). */
211a9643ea8Slogwang #define	FDDUP_FLAG_CLOEXEC	0x1	/* Atomically set UF_EXCLOSE. */
212a9643ea8Slogwang 
213a9643ea8Slogwang /* For backward compatibility. */
214a9643ea8Slogwang #define	falloc(td, resultfp, resultfd, flags) \
215a9643ea8Slogwang 	falloc_caps(td, resultfp, resultfd, flags, NULL)
216a9643ea8Slogwang 
217a9643ea8Slogwang struct thread;
218a9643ea8Slogwang 
219*22ce4affSfengbojiang static __inline void
filecaps_init(struct filecaps * fcaps)220*22ce4affSfengbojiang filecaps_init(struct filecaps *fcaps)
221*22ce4affSfengbojiang {
222*22ce4affSfengbojiang 
223*22ce4affSfengbojiang         bzero(fcaps, sizeof(*fcaps));
224*22ce4affSfengbojiang         fcaps->fc_nioctls = -1;
225*22ce4affSfengbojiang }
226*22ce4affSfengbojiang bool	filecaps_copy(const struct filecaps *src, struct filecaps *dst,
227a9643ea8Slogwang 	    bool locked);
228a9643ea8Slogwang void	filecaps_move(struct filecaps *src, struct filecaps *dst);
229a9643ea8Slogwang void	filecaps_free(struct filecaps *fcaps);
230a9643ea8Slogwang 
231a9643ea8Slogwang int	closef(struct file *fp, struct thread *td);
232*22ce4affSfengbojiang void	closef_nothread(struct file *fp);
233a9643ea8Slogwang int	dupfdopen(struct thread *td, struct filedesc *fdp, int dfd, int mode,
234a9643ea8Slogwang 	    int openerror, int *indxp);
235a9643ea8Slogwang int	falloc_caps(struct thread *td, struct file **resultfp, int *resultfd,
236a9643ea8Slogwang 	    int flags, struct filecaps *fcaps);
237*22ce4affSfengbojiang void	falloc_abort(struct thread *td, struct file *fp);
238*22ce4affSfengbojiang int	_falloc_noinstall(struct thread *td, struct file **resultfp, u_int n);
239*22ce4affSfengbojiang #define	falloc_noinstall(td, resultfp) _falloc_noinstall(td, resultfp, 1)
240a9643ea8Slogwang void	_finstall(struct filedesc *fdp, struct file *fp, int fd, int flags,
241a9643ea8Slogwang 	    struct filecaps *fcaps);
242a9643ea8Slogwang int	finstall(struct thread *td, struct file *fp, int *resultfd, int flags,
243a9643ea8Slogwang 	    struct filecaps *fcaps);
244*22ce4affSfengbojiang int	finstall_refed(struct thread *td, struct file *fp, int *resultfd, int flags,
245*22ce4affSfengbojiang 	    struct filecaps *fcaps);
246a9643ea8Slogwang int	fdalloc(struct thread *td, int minfd, int *result);
247a9643ea8Slogwang int	fdallocn(struct thread *td, int minfd, int *fds, int n);
248a9643ea8Slogwang int	fdcheckstd(struct thread *td);
249a9643ea8Slogwang void	fdclose(struct thread *td, struct file *fp, int idx);
250a9643ea8Slogwang void	fdcloseexec(struct thread *td);
251a9643ea8Slogwang void	fdsetugidsafety(struct thread *td);
252a9643ea8Slogwang struct	filedesc *fdcopy(struct filedesc *fdp);
253a9643ea8Slogwang int	fdcopy_remapped(struct filedesc *fdp, const int *fds, size_t nfds,
254a9643ea8Slogwang 	    struct filedesc **newfdp);
255a9643ea8Slogwang void	fdinstall_remapped(struct thread *td, struct filedesc *fdp);
256a9643ea8Slogwang void	fdunshare(struct thread *td);
257a9643ea8Slogwang void	fdescfree(struct thread *td);
258a9643ea8Slogwang void	fdescfree_remapped(struct filedesc *fdp);
259*22ce4affSfengbojiang int	fdlastfile(struct filedesc *fdp);
260*22ce4affSfengbojiang int	fdlastfile_single(struct filedesc *fdp);
261*22ce4affSfengbojiang struct	filedesc *fdinit(struct filedesc *fdp, bool prepfiles, int *lastfile);
262a9643ea8Slogwang struct	filedesc *fdshare(struct filedesc *fdp);
263a9643ea8Slogwang struct filedesc_to_leader *
264a9643ea8Slogwang 	filedesc_to_leader_alloc(struct filedesc_to_leader *old,
265a9643ea8Slogwang 	    struct filedesc *fdp, struct proc *leader);
266a9643ea8Slogwang int	getvnode(struct thread *td, int fd, cap_rights_t *rightsp,
267a9643ea8Slogwang 	    struct file **fpp);
268a9643ea8Slogwang void	mountcheckdirs(struct vnode *olddp, struct vnode *newdp);
269a9643ea8Slogwang 
270*22ce4affSfengbojiang int	fget_cap_locked(struct filedesc *fdp, int fd, cap_rights_t *needrightsp,
271*22ce4affSfengbojiang 	    struct file **fpp, struct filecaps *havecapsp);
272*22ce4affSfengbojiang int	fget_cap(struct thread *td, int fd, cap_rights_t *needrightsp,
273*22ce4affSfengbojiang 	    struct file **fpp, struct filecaps *havecapsp);
274*22ce4affSfengbojiang 
275a9643ea8Slogwang /* Return a referenced file from an unlocked descriptor. */
276*22ce4affSfengbojiang int	fget_unlocked_seq(struct filedesc *fdp, int fd, cap_rights_t *needrightsp,
277*22ce4affSfengbojiang 	    struct file **fpp, seqc_t *seqp);
278a9643ea8Slogwang int	fget_unlocked(struct filedesc *fdp, int fd, cap_rights_t *needrightsp,
279*22ce4affSfengbojiang 	    struct file **fpp);
280*22ce4affSfengbojiang /* Return a file pointer without a ref. FILEDESC_IS_ONLY_USER must be true.  */
281*22ce4affSfengbojiang int	fget_only_user(struct filedesc *fdp, int fd, cap_rights_t *needrightsp,
282*22ce4affSfengbojiang 	    struct file **fpp);
283*22ce4affSfengbojiang #define	fput_only_user(fdp, fp)	({					\
284*22ce4affSfengbojiang 	MPASS(FILEDESC_IS_ONLY_USER(fdp));				\
285*22ce4affSfengbojiang 	MPASS(refcount_load(&fp->f_count) > 0);				\
286*22ce4affSfengbojiang })
287a9643ea8Slogwang 
288a9643ea8Slogwang /* Requires a FILEDESC_{S,X}LOCK held and returns without a ref. */
289a9643ea8Slogwang static __inline struct file *
fget_locked(struct filedesc * fdp,int fd)290a9643ea8Slogwang fget_locked(struct filedesc *fdp, int fd)
291a9643ea8Slogwang {
292a9643ea8Slogwang 
293a9643ea8Slogwang 	FILEDESC_LOCK_ASSERT(fdp);
294a9643ea8Slogwang 
295*22ce4affSfengbojiang 	if (__predict_false((u_int)fd >= fdp->fd_nfiles))
296a9643ea8Slogwang 		return (NULL);
297a9643ea8Slogwang 
298a9643ea8Slogwang 	return (fdp->fd_ofiles[fd].fde_file);
299a9643ea8Slogwang }
300a9643ea8Slogwang 
301*22ce4affSfengbojiang static __inline struct filedescent *
fdeget_locked(struct filedesc * fdp,int fd)302*22ce4affSfengbojiang fdeget_locked(struct filedesc *fdp, int fd)
303a9643ea8Slogwang {
304*22ce4affSfengbojiang 	struct filedescent *fde;
305a9643ea8Slogwang 
306*22ce4affSfengbojiang 	FILEDESC_LOCK_ASSERT(fdp);
307*22ce4affSfengbojiang 
308*22ce4affSfengbojiang 	if (__predict_false((u_int)fd >= fdp->fd_nfiles))
309*22ce4affSfengbojiang 		return (NULL);
310*22ce4affSfengbojiang 
311*22ce4affSfengbojiang 	fde = &fdp->fd_ofiles[fd];
312*22ce4affSfengbojiang 	if (__predict_false(fde->fde_file == NULL))
313*22ce4affSfengbojiang 		return (NULL);
314*22ce4affSfengbojiang 
315*22ce4affSfengbojiang 	return (fde);
316a9643ea8Slogwang }
317a9643ea8Slogwang 
318*22ce4affSfengbojiang #ifdef CAPABILITIES
319*22ce4affSfengbojiang static __inline bool
fd_modified(struct filedesc * fdp,int fd,seqc_t seqc)320*22ce4affSfengbojiang fd_modified(struct filedesc *fdp, int fd, seqc_t seqc)
321*22ce4affSfengbojiang {
322*22ce4affSfengbojiang 
323*22ce4affSfengbojiang 	return (!seqc_consistent(fd_seqc(fdp->fd_files, fd), seqc));
324*22ce4affSfengbojiang }
325*22ce4affSfengbojiang #endif
326*22ce4affSfengbojiang 
327a9643ea8Slogwang /* cdir/rdir/jdir manipulation functions. */
328*22ce4affSfengbojiang struct pwddesc *pdcopy(struct pwddesc *pdp);
329*22ce4affSfengbojiang void	pdescfree(struct thread *td);
330*22ce4affSfengbojiang struct pwddesc *pdinit(struct pwddesc *pdp, bool keeplock);
331*22ce4affSfengbojiang struct pwddesc *pdshare(struct pwddesc *pdp);
332*22ce4affSfengbojiang void	pdunshare(struct thread *td);
333*22ce4affSfengbojiang 
334a9643ea8Slogwang void	pwd_chdir(struct thread *td, struct vnode *vp);
335a9643ea8Slogwang int	pwd_chroot(struct thread *td, struct vnode *vp);
336*22ce4affSfengbojiang int	pwd_chroot_chdir(struct thread *td, struct vnode *vp);
337a9643ea8Slogwang void	pwd_ensure_dirs(void);
338*22ce4affSfengbojiang void	pwd_set_rootvnode(void);
339*22ce4affSfengbojiang 
340*22ce4affSfengbojiang struct pwd *pwd_hold_pwddesc(struct pwddesc *pdp);
341*22ce4affSfengbojiang bool	pwd_hold_smr(struct pwd *pwd);
342*22ce4affSfengbojiang struct pwd *pwd_hold(struct thread *td);
343*22ce4affSfengbojiang void	pwd_drop(struct pwd *pwd);
344*22ce4affSfengbojiang static inline void
pwd_set(struct pwddesc * pdp,struct pwd * newpwd)345*22ce4affSfengbojiang pwd_set(struct pwddesc *pdp, struct pwd *newpwd)
346*22ce4affSfengbojiang {
347*22ce4affSfengbojiang 	smr_serialized_store(&pdp->pd_pwd, newpwd,
348*22ce4affSfengbojiang 	    (PWDDESC_ASSERT_XLOCKED(pdp), true));
349*22ce4affSfengbojiang }
350*22ce4affSfengbojiang #define	pwd_get_smr()	vfs_smr_entered_load(&curproc->p_pd->pd_pwd)
351a9643ea8Slogwang 
352a9643ea8Slogwang #endif /* _KERNEL */
353a9643ea8Slogwang 
354a9643ea8Slogwang #endif /* !_SYS_FILEDESC_H_ */
355