xref: /freebsd/sys/compat/linuxkpi/common/include/linux/fs.h (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2018 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #ifndef	_LINUX_FS_H_
32 #define	_LINUX_FS_H_
33 
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/conf.h>
38 #include <sys/vnode.h>
39 #include <sys/file.h>
40 #include <sys/filedesc.h>
41 #include <linux/types.h>
42 #include <linux/wait.h>
43 #include <linux/semaphore.h>
44 #include <linux/spinlock.h>
45 #include <linux/dcache.h>
46 
47 struct module;
48 struct kiocb;
49 struct iovec;
50 struct dentry;
51 struct page;
52 struct file_lock;
53 struct pipe_inode_info;
54 struct vm_area_struct;
55 struct poll_table_struct;
56 struct files_struct;
57 struct pfs_node;
58 struct linux_cdev;
59 
60 #define	inode	vnode
61 #define	i_cdev	v_rdev
62 #define	i_private v_data
63 
64 #define	S_IRUGO	(S_IRUSR | S_IRGRP | S_IROTH)
65 #define	S_IWUGO	(S_IWUSR | S_IWGRP | S_IWOTH)
66 
67 typedef struct files_struct *fl_owner_t;
68 
69 struct file_operations;
70 
71 struct linux_file_wait_queue {
72 	struct wait_queue wq;
73 	struct wait_queue_head *wqh;
74 	atomic_t state;
75 #define	LINUX_FWQ_STATE_INIT 0
76 #define	LINUX_FWQ_STATE_NOT_READY 1
77 #define	LINUX_FWQ_STATE_QUEUED 2
78 #define	LINUX_FWQ_STATE_READY 3
79 #define	LINUX_FWQ_STATE_MAX 4
80 };
81 
82 struct linux_file {
83 	struct file	*_file;
84 	const struct file_operations	*f_op;
85 	void		*private_data;
86 	int		f_flags;
87 	int		f_mode;	/* Just starting mode. */
88 	struct dentry	*f_dentry;
89 	struct dentry	f_dentry_store;
90 	struct selinfo	f_selinfo;
91 	struct sigio	*f_sigio;
92 	struct vnode	*f_vnode;
93 #define	f_inode	f_vnode
94 	volatile u_int	f_count;
95 
96 	/* anonymous shmem object */
97 	vm_object_t	f_shmem;
98 
99 	/* kqfilter support */
100 	int		f_kqflags;
101 #define	LINUX_KQ_FLAG_HAS_READ (1 << 0)
102 #define	LINUX_KQ_FLAG_HAS_WRITE (1 << 1)
103 #define	LINUX_KQ_FLAG_NEED_READ (1 << 2)
104 #define	LINUX_KQ_FLAG_NEED_WRITE (1 << 3)
105 	/* protects f_selinfo.si_note */
106 	spinlock_t	f_kqlock;
107 	struct linux_file_wait_queue f_wait_queue;
108 
109 	/* pointer to associated character device, if any */
110 	struct linux_cdev *f_cdev;
111 };
112 
113 #define	file		linux_file
114 #define	fasync_struct	sigio *
115 
116 #define	fasync_helper(fd, filp, on, queue)				\
117 ({									\
118 	if ((on))							\
119 		*(queue) = &(filp)->f_sigio;				\
120 	else								\
121 		*(queue) = NULL;					\
122 	0;								\
123 })
124 
125 #define	kill_fasync(queue, sig, pollstat)				\
126 do {									\
127 	if (*(queue) != NULL)						\
128 		pgsigio(*(queue), (sig), 0);				\
129 } while (0)
130 
131 typedef int (*filldir_t)(void *, const char *, int, off_t, u64, unsigned);
132 
133 struct file_operations {
134 	struct module *owner;
135 	ssize_t (*read)(struct linux_file *, char __user *, size_t, off_t *);
136 	ssize_t (*write)(struct linux_file *, const char __user *, size_t, off_t *);
137 	unsigned int (*poll) (struct linux_file *, struct poll_table_struct *);
138 	long (*unlocked_ioctl)(struct linux_file *, unsigned int, unsigned long);
139 	long (*compat_ioctl)(struct linux_file *, unsigned int, unsigned long);
140 	int (*mmap)(struct linux_file *, struct vm_area_struct *);
141 	int (*open)(struct inode *, struct file *);
142 	int (*release)(struct inode *, struct linux_file *);
143 	int (*fasync)(int, struct linux_file *, int);
144 
145 /* Although not supported in FreeBSD, to align with Linux code
146  * we are adding llseek() only when it is mapped to no_llseek which returns
147  * an illegal seek error
148  */
149 	off_t (*llseek)(struct linux_file *, off_t, int);
150 #if 0
151 	/* We do not support these methods.  Don't permit them to compile. */
152 	loff_t (*llseek)(struct file *, loff_t, int);
153 	ssize_t (*aio_read)(struct kiocb *, const struct iovec *,
154 	    unsigned long, loff_t);
155 	ssize_t (*aio_write)(struct kiocb *, const struct iovec *,
156 	    unsigned long, loff_t);
157 	int (*readdir)(struct file *, void *, filldir_t);
158 	int (*ioctl)(struct inode *, struct file *, unsigned int,
159 	    unsigned long);
160 	int (*flush)(struct file *, fl_owner_t id);
161 	int (*fsync)(struct file *, struct dentry *, int datasync);
162 	int (*aio_fsync)(struct kiocb *, int datasync);
163 	int (*lock)(struct file *, int, struct file_lock *);
164 	ssize_t (*sendpage)(struct file *, struct page *, int, size_t,
165 	    loff_t *, int);
166 	unsigned long (*get_unmapped_area)(struct file *, unsigned long,
167 	    unsigned long, unsigned long, unsigned long);
168 	int (*check_flags)(int);
169 	int (*flock)(struct file *, int, struct file_lock *);
170 	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
171 	    loff_t *, size_t, unsigned int);
172 	ssize_t (*splice_read)(struct file *, loff_t *,
173 	    struct pipe_inode_info *, size_t, unsigned int);
174 	int (*setlease)(struct file *, long, struct file_lock **);
175 #endif
176 };
177 #define	fops_get(fops)		(fops)
178 #define	replace_fops(f, fops)	((f)->f_op = (fops))
179 
180 #define	FMODE_READ	FREAD
181 #define	FMODE_WRITE	FWRITE
182 #define	FMODE_EXEC	FEXEC
183 #define	FMODE_UNSIGNED_OFFSET	0x2000
184 int __register_chrdev(unsigned int major, unsigned int baseminor,
185     unsigned int count, const char *name,
186     const struct file_operations *fops);
187 int __register_chrdev_p(unsigned int major, unsigned int baseminor,
188     unsigned int count, const char *name,
189     const struct file_operations *fops, uid_t uid,
190     gid_t gid, int mode);
191 void __unregister_chrdev(unsigned int major, unsigned int baseminor,
192     unsigned int count, const char *name);
193 
194 static inline void
195 unregister_chrdev(unsigned int major, const char *name)
196 {
197 
198 	__unregister_chrdev(major, 0, 256, name);
199 }
200 
201 static inline int
202 register_chrdev(unsigned int major, const char *name,
203     const struct file_operations *fops)
204 {
205 
206 	return (__register_chrdev(major, 0, 256, name, fops));
207 }
208 
209 static inline int
210 register_chrdev_p(unsigned int major, const char *name,
211     const struct file_operations *fops, uid_t uid, gid_t gid, int mode)
212 {
213 
214 	return (__register_chrdev_p(major, 0, 256, name, fops, uid, gid, mode));
215 }
216 
217 static inline int
218 register_chrdev_region(dev_t dev, unsigned range, const char *name)
219 {
220 
221 	return 0;
222 }
223 
224 static inline void
225 unregister_chrdev_region(dev_t dev, unsigned range)
226 {
227 
228 	return;
229 }
230 
231 static inline int
232 alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
233 			const char *name)
234 {
235 
236 	return 0;
237 }
238 
239 /* No current support for seek op in FreeBSD */
240 static inline int
241 nonseekable_open(struct inode *inode, struct file *filp)
242 {
243 	return 0;
244 }
245 
246 extern unsigned int linux_iminor(struct inode *);
247 #define	iminor(...) linux_iminor(__VA_ARGS__)
248 
249 static inline struct linux_file *
250 get_file(struct linux_file *f)
251 {
252 
253 	refcount_acquire(f->_file == NULL ? &f->f_count : &f->_file->f_count);
254 	return (f);
255 }
256 
257 static inline struct inode *
258 igrab(struct inode *inode)
259 {
260 	int error;
261 
262 	error = vget(inode, 0);
263 	if (error)
264 		return (NULL);
265 
266 	return (inode);
267 }
268 
269 static inline void
270 iput(struct inode *inode)
271 {
272 
273 	vrele(inode);
274 }
275 
276 static inline loff_t
277 no_llseek(struct file *file, loff_t offset, int whence)
278 {
279 
280 	return (-ESPIPE);
281 }
282 
283 static inline loff_t
284 noop_llseek(struct linux_file *file, loff_t offset, int whence)
285 {
286 
287 	return (file->_file->f_offset);
288 }
289 
290 static inline struct vnode *
291 file_inode(const struct linux_file *file)
292 {
293 
294 	return (file->f_vnode);
295 }
296 
297 static inline int
298 call_mmap(struct linux_file *file, struct vm_area_struct *vma)
299 {
300 
301 	return (file->f_op->mmap(file, vma));
302 }
303 
304 #endif /* _LINUX_FS_H_ */
305