Home | History | Annotate | Line # | Download | only in sys
      1 /*	$NetBSD: file.h,v 1.93 2023/07/10 02:31:55 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1982, 1986, 1989, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  *	@(#)file.h	8.3 (Berkeley) 1/9/95
     61  */
     62 
     63 #ifndef _SYS_FILE_H_
     64 #define	_SYS_FILE_H_
     65 
     66 #include <sys/types.h>
     67 
     68 #include <sys/fcntl.h>
     69 #include <sys/unistd.h>
     70 
     71 #if defined(_KERNEL) || defined(_KMEMUSER)
     72 #include <sys/queue.h>
     73 #include <sys/mutex.h>
     74 #include <sys/condvar.h>
     75 
     76 struct flock;
     77 struct iovec;
     78 struct knote;
     79 struct lwp;
     80 struct proc;
     81 struct stat;
     82 struct uio;
     83 struct uvm_object;
     84 
     85 struct fileops {
     86 	const char *fo_name;
     87 	int	(*fo_read)	(struct file *, off_t *, struct uio *,
     88 				    kauth_cred_t, int);
     89 	int	(*fo_write)	(struct file *, off_t *, struct uio *,
     90 				    kauth_cred_t, int);
     91 	int	(*fo_ioctl)	(struct file *, u_long, void *);
     92 	int	(*fo_fcntl)	(struct file *, u_int, void *);
     93 	int	(*fo_poll)	(struct file *, int);
     94 	int	(*fo_stat)	(struct file *, struct stat *);
     95 	int	(*fo_close)	(struct file *);
     96 	int	(*fo_kqfilter)	(struct file *, struct knote *);
     97 	void	(*fo_restart)	(struct file *);
     98 	int	(*fo_mmap)	(struct file *, off_t *, size_t, int, int *,
     99 				 int *, struct uvm_object **, int *);
    100 	int	(*fo_seek)	(struct file *, off_t, int, off_t *, int);
    101 	int	(*fo_advlock)	(struct file *, void *, int, struct flock *,
    102 				 int);
    103 	int	(*fo_fpathconf)	(struct file *, int, register_t *);
    104 	int	(*fo_posix_fadvise)
    105 				(struct file *, off_t, off_t, int);
    106 	int	(*fo_truncate)	(struct file *, off_t);
    107 };
    108 
    109 union file_data {
    110 	struct vnode *fd_vp;		// DTYPE_VNODE
    111 	struct socket *fd_so;		// DTYPE_SOCKET
    112 	struct pipe *fd_pipe;		// DTYPE_PIPE
    113 	struct kqueue *fd_kq;		// DTYPE_KQUEUE
    114 	struct eventfd *fd_eventfd;	// DTYPE_EVENTFD
    115 	struct timerfd *fd_timerfd;	// DTYPE_TIMERFD
    116 	void *fd_data;			// DTYPE_MISC
    117 	struct audio_file *fd_audioctx;	// DTYPE_MISC (audio)
    118 	struct pad_softc *fd_pad;	// DTYPE_MISC (pad)
    119 	int fd_devunit;			// DTYPE_MISC (tap)
    120 	struct bpf_d *fd_bpf;		// DTYPE_MISC (bpf)
    121 	struct fcrypt *fd_fcrypt;	// DTYPE_CRYPTO is not used
    122 	struct mqueue *fd_mq;		// DTYPE_MQUEUE
    123 	struct ksem *fd_ks;		// DTYPE_SEM
    124 	struct iscsifd *fd_iscsi;	// DTYPE_MISC (iscsi)
    125 	struct memfd *fd_memfd;		// DTYPE_MEMFD
    126 };
    127 
    128 /*
    129  * Kernel file descriptor.  One entry for each open kernel vnode and
    130  * socket.
    131  *
    132  * This structure is exported via the KERN_FILE sysctl.
    133  * Only add members to the end, do not delete them.
    134  *
    135  * Note: new code should not use KERN_FILE; use KERN_FILE2 instead,
    136  * which exports struct kinfo_file instead; struct kinfo_file is
    137  * declared in sys/sysctl.h and is meant to be ABI-stable.
    138  */
    139 struct file {
    140 	off_t		f_offset;	/* first, is 64-bit */
    141 	kauth_cred_t 	f_cred;		/* creds associated with descriptor */
    142 	const struct fileops *f_ops;
    143 	union file_data	f_undata;	/* descriptor data, e.g. vnode/socket */
    144 	LIST_ENTRY(file) f_list;	/* list of active files */
    145 	kmutex_t	f_lock;		/* lock on structure */
    146 	int		f_flag;		/* see fcntl.h */
    147 	u_int		f_marker;	/* traversal marker (sysctl) */
    148 	u_int		f_type;		/* descriptor type */
    149 	u_int		f_advice;	/* access pattern hint; UVM_ADV_* */
    150 	u_int		f_count;	/* reference count */
    151 	u_int		f_msgcount;	/* references from message queue */
    152 	u_int		f_unpcount;	/* deferred close: see uipc_usrreq.c */
    153 	SLIST_ENTRY(file) f_unplist;	/* deferred close: see uipc_usrreq.c */
    154 };
    155 
    156 #define f_vnode		f_undata.fd_vp
    157 #define f_socket	f_undata.fd_so
    158 #define f_pipe		f_undata.fd_pipe
    159 #define f_kqueue	f_undata.fd_kq
    160 #define f_data		f_undata.fd_data
    161 #define f_mqueue	f_undata.fd_mq
    162 #define f_ksem		f_undata.fd_ks
    163 #define f_eventfd	f_undata.fd_eventfd
    164 #define f_timerfd	f_undata.fd_timerfd
    165 #define f_memfd		f_undata.fd_memfd
    166 
    167 #define f_rndctx	f_undata.fd_rndctx
    168 #define f_audioctx	f_undata.fd_audioctx
    169 #define f_pad		f_undata.fd_pad
    170 #define f_devunit	f_undata.fd_devunit
    171 #define f_bpf		f_undata.fd_bpf
    172 #define f_fcrypt	f_undata.fd_fcrypt
    173 #define f_iscsi		f_undata.fd_iscsi
    174 #endif /* _KERNEL || _KMEMUSER */
    175 
    176 /*
    177  * Descriptor types.
    178  */
    179 
    180 #define	DTYPE_VNODE	1		/* file */
    181 #define	DTYPE_SOCKET	2		/* communications endpoint */
    182 #define	DTYPE_PIPE	3		/* pipe */
    183 #define	DTYPE_KQUEUE	4		/* event queue */
    184 #define	DTYPE_MISC	5		/* misc file descriptor type */
    185 #define	DTYPE_CRYPTO	6		/* crypto */
    186 #define	DTYPE_MQUEUE	7		/* message queue */
    187 #define	DTYPE_SEM	8		/* semaphore */
    188 #define	DTYPE_EVENTFD	9		/* eventfd */
    189 #define	DTYPE_TIMERFD	10		/* timerfd */
    190 #define	DTYPE_MEMFD	11		/* memfd */
    191 
    192 #define DTYPE_NAMES	\
    193     "0", "file", "socket", "pipe", "kqueue", "misc", "crypto", "mqueue", \
    194     "semaphore", "eventfd", "timerfd", "memfd"
    195 
    196 #ifdef _KERNEL
    197 
    198 /*
    199  * Flags for fo_read and fo_write and do_fileread/write/v
    200  */
    201 #define	FOF_UPDATE_OFFSET	0x0001	/* update the file offset */
    202 #define	FOF_IOV_SYSSPACE	0x0100	/* iov structure in kernel memory */
    203 
    204 LIST_HEAD(filelist, file);
    205 extern struct filelist	filehead;	/* head of list of open files */
    206 extern u_int		maxfiles;	/* kernel limit on # of open files */
    207 
    208 extern const struct fileops vnops;	/* vnode operations for files */
    209 
    210 int	dofileread(int, struct file *, void *, size_t,
    211 	    off_t *, int, register_t *);
    212 int	dofilewrite(int, struct file *, const void *,
    213 	    size_t, off_t *, int, register_t *);
    214 
    215 int	do_filereadv(int, const struct iovec *, int, off_t *,
    216 	    int, register_t *);
    217 int	do_filewritev(int, const struct iovec *, int, off_t *,
    218 	    int, register_t *);
    219 
    220 int	fsetown(pid_t *, u_long, const void *);
    221 int	fgetown(pid_t, u_long, void *);
    222 void	fownsignal(pid_t, int, int, int, void *);
    223 
    224 /* Commonly used fileops */
    225 int	fnullop_fcntl(struct file *, u_int, void *);
    226 int	fnullop_poll(struct file *, int);
    227 int	fnullop_kqfilter(struct file *, struct knote *);
    228 int	fbadop_read(struct file *, off_t *, struct uio *, kauth_cred_t, int);
    229 int	fbadop_write(struct file *, off_t *, struct uio *, kauth_cred_t, int);
    230 int	fbadop_ioctl(struct file *, u_long, void *);
    231 int	fbadop_close(struct file *);
    232 int	fbadop_stat(struct file *, struct stat *);
    233 void	fnullop_restart(struct file *);
    234 
    235 #endif /* _KERNEL */
    236 
    237 #endif /* _SYS_FILE_H_ */
    238