xref: /freebsd/sys/sys/file.h (revision 4b69f1fab66db4fd3f874e78a457e317cd498d36)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
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:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef _SYS_FILE_H_
33 #define	_SYS_FILE_H_
34 
35 #ifndef _KERNEL
36 #include <sys/types.h> /* XXX */
37 #include <sys/fcntl.h>
38 #include <sys/unistd.h>
39 #else
40 #include <sys/errno.h>
41 #include <sys/_lock.h>
42 #include <sys/_mutex.h>
43 #include <sys/_null.h>
44 #include <sys/queue.h>
45 #include <sys/refcount.h>
46 #include <vm/vm.h>
47 
48 struct filedesc;
49 struct proc;
50 struct stat;
51 struct thread;
52 struct uio;
53 struct knote;
54 struct vnode;
55 struct nameidata;
56 
57 #endif /* _KERNEL */
58 
59 #define	DTYPE_NONE	0	/* not yet initialized */
60 #define	DTYPE_VNODE	1	/* file */
61 #define	DTYPE_SOCKET	2	/* communications endpoint */
62 #define	DTYPE_PIPE	3	/* pipe */
63 #define	DTYPE_FIFO	4	/* fifo (named pipe) */
64 #define	DTYPE_KQUEUE	5	/* event queue */
65 #define	DTYPE_CRYPTO	6	/* crypto */
66 #define	DTYPE_MQUEUE	7	/* posix message queue */
67 #define	DTYPE_SHM	8	/* swap-backed shared memory */
68 #define	DTYPE_SEM	9	/* posix semaphore */
69 #define	DTYPE_PTS	10	/* pseudo teletype master device */
70 #define	DTYPE_DEV	11	/* Device specific fd type */
71 #define	DTYPE_PROCDESC	12	/* process descriptor */
72 #define	DTYPE_EVENTFD	13	/* eventfd */
73 #define	DTYPE_TIMERFD	14	/* timerfd */
74 
75 #ifdef _KERNEL
76 
77 struct file;
78 struct filecaps;
79 struct kaiocb;
80 struct kinfo_file;
81 struct ucred;
82 
83 #define	FOF_OFFSET	0x01	/* Use the offset in uio argument */
84 #define	FOF_NOLOCK	0x02	/* Do not take FOFFSET_LOCK */
85 #define	FOF_NEXTOFF_R	0x04	/* Also update f_nextoff[UIO_READ] */
86 #define	FOF_NEXTOFF_W	0x08	/* Also update f_nextoff[UIO_WRITE] */
87 #define	FOF_NOUPDATE	0x10	/* Do not update f_offset */
88 off_t foffset_lock(struct file *fp, int flags);
89 void foffset_lock_uio(struct file *fp, struct uio *uio, int flags);
90 void foffset_unlock(struct file *fp, off_t val, int flags);
91 void foffset_unlock_uio(struct file *fp, struct uio *uio, int flags);
92 
93 static inline off_t
foffset_get(struct file * fp)94 foffset_get(struct file *fp)
95 {
96 
97 	return (foffset_lock(fp, FOF_NOLOCK));
98 }
99 
100 typedef int fo_rdwr_t(struct file *fp, struct uio *uio,
101 		    struct ucred *active_cred, int flags,
102 		    struct thread *td);
103 typedef	int fo_truncate_t(struct file *fp, off_t length,
104 		    struct ucred *active_cred, struct thread *td);
105 typedef	int fo_ioctl_t(struct file *fp, u_long com, void *data,
106 		    struct ucred *active_cred, struct thread *td);
107 typedef	int fo_poll_t(struct file *fp, int events,
108 		    struct ucred *active_cred, struct thread *td);
109 typedef	int fo_kqfilter_t(struct file *fp, struct knote *kn);
110 typedef	int fo_stat_t(struct file *fp, struct stat *sb,
111 		    struct ucred *active_cred);
112 typedef	int fo_close_t(struct file *fp, struct thread *td);
113 typedef	int fo_chmod_t(struct file *fp, mode_t mode,
114 		    struct ucred *active_cred, struct thread *td);
115 typedef	int fo_chown_t(struct file *fp, uid_t uid, gid_t gid,
116 		    struct ucred *active_cred, struct thread *td);
117 typedef int fo_sendfile_t(struct file *fp, int sockfd, struct uio *hdr_uio,
118 		    struct uio *trl_uio, off_t offset, size_t nbytes,
119 		    off_t *sent, int flags, struct thread *td);
120 typedef int fo_seek_t(struct file *fp, off_t offset, int whence,
121 		    struct thread *td);
122 typedef int fo_fill_kinfo_t(struct file *fp, struct kinfo_file *kif,
123 		    struct filedesc *fdp);
124 typedef int fo_mmap_t(struct file *fp, vm_map_t map, vm_offset_t *addr,
125 		    vm_size_t size, vm_prot_t prot, vm_prot_t cap_maxprot,
126 		    int flags, vm_ooffset_t foff, struct thread *td);
127 typedef int fo_aio_queue_t(struct file *fp, struct kaiocb *job);
128 typedef int fo_add_seals_t(struct file *fp, int flags);
129 typedef int fo_get_seals_t(struct file *fp, int *flags);
130 typedef int fo_fallocate_t(struct file *fp, off_t offset, off_t len,
131 		    struct thread *td);
132 typedef int fo_fspacectl_t(struct file *fp, int cmd,
133 		    off_t *offset, off_t *length, int flags,
134 		    struct ucred *active_cred, struct thread *td);
135 typedef int fo_cmp_t(struct file *fp, struct file *fp1, struct thread *td);
136 typedef int fo_spare_t(struct file *fp);
137 typedef	int fo_flags_t;
138 
139 struct fileops {
140 	fo_rdwr_t	*fo_read;
141 	fo_rdwr_t	*fo_write;
142 	fo_truncate_t	*fo_truncate;
143 	fo_ioctl_t	*fo_ioctl;
144 	fo_poll_t	*fo_poll;
145 	fo_kqfilter_t	*fo_kqfilter;
146 	fo_stat_t	*fo_stat;
147 	fo_close_t	*fo_close;
148 	fo_chmod_t	*fo_chmod;
149 	fo_chown_t	*fo_chown;
150 	fo_sendfile_t	*fo_sendfile;
151 	fo_seek_t	*fo_seek;
152 	fo_fill_kinfo_t	*fo_fill_kinfo;
153 	fo_mmap_t	*fo_mmap;
154 	fo_aio_queue_t	*fo_aio_queue;
155 	fo_add_seals_t	*fo_add_seals;
156 	fo_get_seals_t	*fo_get_seals;
157 	fo_fallocate_t	*fo_fallocate;
158 	fo_fspacectl_t	*fo_fspacectl;
159 	fo_cmp_t	*fo_cmp;
160 	fo_spare_t	*fo_spares[8];	/* Spare slots */
161 	fo_flags_t	fo_flags;	/* DFLAG_* below */
162 };
163 
164 #define DFLAG_PASSABLE	0x01	/* may be passed via unix sockets. */
165 #define DFLAG_SEEKABLE	0x02	/* seekable / nonsequential */
166 #endif /* _KERNEL */
167 
168 #if defined(_KERNEL) || defined(_WANT_FILE)
169 /*
170  * Kernel descriptor table.
171  * One entry for each open kernel vnode and socket.
172  *
173  * Below is the list of locks that protects members in struct file.
174  *
175  * (a) f_vnode lock required (shared allows both reads and writes)
176  * (f) updated with atomics and blocking on sleepq
177  * (d) cdevpriv_mtx
178  * none	not locked
179  */
180 
181 #if __BSD_VISIBLE
182 struct fadvise_info {
183 	int		fa_advice;	/* (f) FADV_* type. */
184 	off_t		fa_start;	/* (f) Region start. */
185 	off_t		fa_end;		/* (f) Region end. */
186 };
187 
188 struct file {
189 	volatile u_int	f_flag;		/* see fcntl.h */
190 	volatile u_int 	f_count;	/* reference count */
191 	void		*f_data;	/* file descriptor specific data */
192 	const struct fileops *f_ops;	/* File operations */
193 	struct vnode 	*f_vnode;	/* NULL or applicable vnode */
194 	struct ucred	*f_cred;	/* associated credentials. */
195 	short		f_type;		/* descriptor type */
196 	short		f_vnread_flags; /* (f) Sleep lock for f_offset */
197 	/*
198 	 *  DTYPE_VNODE specific fields.
199 	 */
200 	union {
201 		int16_t	f_seqcount[2];	/* (a) Count of seq. reads and writes. */
202 		int	f_pipegen;
203 	};
204 	off_t		f_nextoff[2];	/* next expected read/write offset. */
205 	union {
206 		struct cdev_privdata *fvn_cdevpriv;
207 					/* (d) Private data for the cdev. */
208 		struct fadvise_info *fvn_advice;
209 	} f_vnun;
210 	/*
211 	 *  DFLAG_SEEKABLE specific fields
212 	 */
213 	off_t		f_offset;
214 };
215 
216 #define	f_cdevpriv	f_vnun.fvn_cdevpriv
217 #define	f_advice	f_vnun.fvn_advice
218 
219 #define	FOFFSET_LOCKED       0x1
220 #define	FOFFSET_LOCK_WAITING 0x2
221 #endif /* __BSD_VISIBLE */
222 
223 #endif /* _KERNEL || _WANT_FILE */
224 
225 /*
226  * Userland version of struct file, for sysctl
227  */
228 #if __BSD_VISIBLE
229 struct xfile {
230 	ksize_t	xf_size;	/* size of struct xfile */
231 	pid_t	xf_pid;		/* owning process */
232 	uid_t	xf_uid;		/* effective uid of owning process */
233 	int	xf_fd;		/* descriptor number */
234 	int	_xf_int_pad1;
235 	kvaddr_t xf_file;	/* address of struct file */
236 	short	xf_type;	/* descriptor type */
237 	short	_xf_short_pad1;
238 	int	xf_count;	/* reference count */
239 	int	xf_msgcount;	/* references from message queue */
240 	int	_xf_int_pad2;
241 	off_t	xf_offset;	/* file offset */
242 	kvaddr_t xf_data;	/* file descriptor specific data */
243 	kvaddr_t xf_vnode;	/* vnode pointer */
244 	u_int	xf_flag;	/* flags (see fcntl.h) */
245 	int	_xf_int_pad3;
246 	int64_t	_xf_int64_pad[6];
247 };
248 #endif /* __BSD_VISIBLE */
249 
250 #ifdef _KERNEL
251 
252 extern const struct fileops vnops;
253 extern const struct fileops badfileops;
254 extern const struct fileops path_fileops;
255 extern const struct fileops socketops;
256 extern int maxfiles;		/* kernel limit on number of open files */
257 extern int maxfilesperproc;	/* per process limit on number of open files */
258 
259 int fget(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp);
260 int fget_mmap(struct thread *td, int fd, cap_rights_t *rightsp,
261     vm_prot_t *maxprotp, struct file **fpp);
262 int fget_read(struct thread *td, int fd, cap_rights_t *rightsp,
263     struct file **fpp);
264 int fget_write(struct thread *td, int fd, cap_rights_t *rightsp,
265     struct file **fpp);
266 int fget_fcntl(struct thread *td, int fd, cap_rights_t *rightsp,
267     int needfcntl, struct file **fpp);
268 int _fdrop(struct file *fp, struct thread *td);
269 int fget_remote(struct thread *td, struct proc *p, int fd, struct file **fpp);
270 int fget_remote_foreach(struct thread *td, struct proc *p,
271     int (*fn)(struct proc *, int, struct file *, void *), void *arg);
272 
273 fo_rdwr_t	invfo_rdwr;
274 fo_truncate_t	invfo_truncate;
275 fo_ioctl_t	invfo_ioctl;
276 fo_poll_t	invfo_poll;
277 fo_kqfilter_t	invfo_kqfilter;
278 fo_chmod_t	invfo_chmod;
279 fo_chown_t	invfo_chown;
280 fo_sendfile_t	invfo_sendfile;
281 fo_stat_t	vn_statfile;
282 fo_sendfile_t	vn_sendfile;
283 fo_seek_t	vn_seek;
284 fo_fill_kinfo_t	vn_fill_kinfo;
285 fo_kqfilter_t	vn_kqfilter_opath;
286 int vn_fill_kinfo_vnode(struct vnode *vp, struct kinfo_file *kif);
287 int file_kcmp_generic(struct file *fp1, struct file *fp2, struct thread *td);
288 
289 void finit(struct file *, u_int, short, void *, const struct fileops *);
290 void finit_vnode(struct file *, u_int, void *, const struct fileops *);
291 int fgetvp(struct thread *td, int fd, cap_rights_t *rightsp,
292     struct vnode **vpp);
293 int fgetvp_exec(struct thread *td, int fd, cap_rights_t *rightsp,
294     struct vnode **vpp);
295 int fgetvp_rights(struct thread *td, int fd, cap_rights_t *needrightsp,
296     struct filecaps *havecaps, struct vnode **vpp);
297 int fgetvp_read(struct thread *td, int fd, cap_rights_t *rightsp,
298     struct vnode **vpp);
299 int fgetvp_write(struct thread *td, int fd, cap_rights_t *rightsp,
300     struct vnode **vpp);
301 int fgetvp_lookup_smr(struct nameidata *ndp, struct vnode **vpp, bool *fsearch);
302 int fgetvp_lookup(struct nameidata *ndp, struct vnode **vpp);
303 
304 static __inline __result_use_check bool
fhold(struct file * fp)305 fhold(struct file *fp)
306 {
307 	return (refcount_acquire_checked(&fp->f_count));
308 }
309 
310 #define	fdrop(fp, td)		({				\
311 	struct file *_fp;					\
312 	int _error;						\
313 								\
314 	_error = 0;						\
315 	_fp = (fp);						\
316 	if (__predict_false(refcount_release(&_fp->f_count)))	\
317 		_error = _fdrop(_fp, td);			\
318 	_error;							\
319 })
320 
321 #define	fdrop_close(fp, td)		({			\
322 	struct file *_fp;					\
323 	int _error;						\
324 								\
325 	_error = 0;						\
326 	_fp = (fp);						\
327 	if (__predict_true(refcount_release(&_fp->f_count)))	\
328 		_error = _fdrop(_fp, td);			\
329 	_error;							\
330 })
331 
332 static __inline fo_rdwr_t	fo_read;
333 static __inline fo_rdwr_t	fo_write;
334 static __inline fo_truncate_t	fo_truncate;
335 static __inline fo_ioctl_t	fo_ioctl;
336 static __inline fo_poll_t	fo_poll;
337 static __inline fo_kqfilter_t	fo_kqfilter;
338 static __inline fo_stat_t	fo_stat;
339 static __inline fo_close_t	fo_close;
340 static __inline fo_chmod_t	fo_chmod;
341 static __inline fo_chown_t	fo_chown;
342 static __inline fo_sendfile_t	fo_sendfile;
343 
344 static __inline int
fo_read(struct file * fp,struct uio * uio,struct ucred * active_cred,int flags,struct thread * td)345 fo_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
346     int flags, struct thread *td)
347 {
348 
349 	return ((*fp->f_ops->fo_read)(fp, uio, active_cred, flags, td));
350 }
351 
352 static __inline int
fo_write(struct file * fp,struct uio * uio,struct ucred * active_cred,int flags,struct thread * td)353 fo_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
354     int flags, struct thread *td)
355 {
356 
357 	return ((*fp->f_ops->fo_write)(fp, uio, active_cred, flags, td));
358 }
359 
360 static __inline int
fo_truncate(struct file * fp,off_t length,struct ucred * active_cred,struct thread * td)361 fo_truncate(struct file *fp, off_t length, struct ucred *active_cred,
362     struct thread *td)
363 {
364 
365 	return ((*fp->f_ops->fo_truncate)(fp, length, active_cred, td));
366 }
367 
368 static __inline int
fo_ioctl(struct file * fp,u_long com,void * data,struct ucred * active_cred,struct thread * td)369 fo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred,
370     struct thread *td)
371 {
372 
373 	return ((*fp->f_ops->fo_ioctl)(fp, com, data, active_cred, td));
374 }
375 
376 static __inline int
fo_poll(struct file * fp,int events,struct ucred * active_cred,struct thread * td)377 fo_poll(struct file *fp, int events, struct ucred *active_cred,
378     struct thread *td)
379 {
380 
381 	return ((*fp->f_ops->fo_poll)(fp, events, active_cred, td));
382 }
383 
384 static __inline int
fo_stat(struct file * fp,struct stat * sb,struct ucred * active_cred)385 fo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred)
386 {
387 
388 	return ((*fp->f_ops->fo_stat)(fp, sb, active_cred));
389 }
390 
391 static __inline int
fo_close(struct file * fp,struct thread * td)392 fo_close(struct file *fp, struct thread *td)
393 {
394 
395 	return ((*fp->f_ops->fo_close)(fp, td));
396 }
397 
398 static __inline int
fo_kqfilter(struct file * fp,struct knote * kn)399 fo_kqfilter(struct file *fp, struct knote *kn)
400 {
401 
402 	return ((*fp->f_ops->fo_kqfilter)(fp, kn));
403 }
404 
405 static __inline int
fo_chmod(struct file * fp,mode_t mode,struct ucred * active_cred,struct thread * td)406 fo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
407     struct thread *td)
408 {
409 
410 	return ((*fp->f_ops->fo_chmod)(fp, mode, active_cred, td));
411 }
412 
413 static __inline int
fo_chown(struct file * fp,uid_t uid,gid_t gid,struct ucred * active_cred,struct thread * td)414 fo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
415     struct thread *td)
416 {
417 
418 	return ((*fp->f_ops->fo_chown)(fp, uid, gid, active_cred, td));
419 }
420 
421 static __inline int
fo_sendfile(struct file * fp,int sockfd,struct uio * hdr_uio,struct uio * trl_uio,off_t offset,size_t nbytes,off_t * sent,int flags,struct thread * td)422 fo_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio,
423     struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags,
424     struct thread *td)
425 {
426 
427 	return ((*fp->f_ops->fo_sendfile)(fp, sockfd, hdr_uio, trl_uio, offset,
428 	    nbytes, sent, flags, td));
429 }
430 
431 static __inline int
fo_seek(struct file * fp,off_t offset,int whence,struct thread * td)432 fo_seek(struct file *fp, off_t offset, int whence, struct thread *td)
433 {
434 
435 	return ((*fp->f_ops->fo_seek)(fp, offset, whence, td));
436 }
437 
438 static __inline int
fo_fill_kinfo(struct file * fp,struct kinfo_file * kif,struct filedesc * fdp)439 fo_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
440 {
441 
442 	return ((*fp->f_ops->fo_fill_kinfo)(fp, kif, fdp));
443 }
444 
445 static __inline int
fo_mmap(struct file * fp,vm_map_t map,vm_offset_t * addr,vm_size_t size,vm_prot_t prot,vm_prot_t cap_maxprot,int flags,vm_ooffset_t foff,struct thread * td)446 fo_mmap(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
447     vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
448     struct thread *td)
449 {
450 
451 	if (fp->f_ops->fo_mmap == NULL)
452 		return (ENODEV);
453 	return ((*fp->f_ops->fo_mmap)(fp, map, addr, size, prot, cap_maxprot,
454 	    flags, foff, td));
455 }
456 
457 static __inline int
fo_aio_queue(struct file * fp,struct kaiocb * job)458 fo_aio_queue(struct file *fp, struct kaiocb *job)
459 {
460 
461 	return ((*fp->f_ops->fo_aio_queue)(fp, job));
462 }
463 
464 static __inline int
fo_add_seals(struct file * fp,int seals)465 fo_add_seals(struct file *fp, int seals)
466 {
467 
468 	if (fp->f_ops->fo_add_seals == NULL)
469 		return (EINVAL);
470 	return ((*fp->f_ops->fo_add_seals)(fp, seals));
471 }
472 
473 static __inline int
fo_get_seals(struct file * fp,int * seals)474 fo_get_seals(struct file *fp, int *seals)
475 {
476 
477 	if (fp->f_ops->fo_get_seals == NULL)
478 		return (EINVAL);
479 	return ((*fp->f_ops->fo_get_seals)(fp, seals));
480 }
481 
482 static __inline int
fo_fallocate(struct file * fp,off_t offset,off_t len,struct thread * td)483 fo_fallocate(struct file *fp, off_t offset, off_t len, struct thread *td)
484 {
485 
486 	if (fp->f_ops->fo_fallocate == NULL)
487 		return (ENODEV);
488 	return ((*fp->f_ops->fo_fallocate)(fp, offset, len, td));
489 }
490 
491 static __inline int
fo_fspacectl(struct file * fp,int cmd,off_t * offset,off_t * length,int flags,struct ucred * active_cred,struct thread * td)492 fo_fspacectl(struct file *fp, int cmd, off_t *offset, off_t *length,
493     int flags, struct ucred *active_cred, struct thread *td)
494 {
495 
496 	if (fp->f_ops->fo_fspacectl == NULL)
497 		return (ENODEV);
498 	return ((*fp->f_ops->fo_fspacectl)(fp, cmd, offset, length, flags,
499 	    active_cred, td));
500 }
501 
502 static __inline int
fo_cmp(struct file * fp1,struct file * fp2,struct thread * td)503 fo_cmp(struct file *fp1, struct file *fp2, struct thread *td)
504 {
505 
506 	if (fp1->f_ops->fo_cmp == NULL)
507 		return (ENODEV);
508 	return ((*fp1->f_ops->fo_cmp)(fp1, fp2, td));
509 }
510 
511 #endif /* _KERNEL */
512 
513 #endif /* !SYS_FILE_H */
514