xref: /freebsd-src/sys/compat/linuxkpi/common/include/linux/fs.h (revision a4a801688c909ef39cbcbc3488bc4fdbabd69d66)
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-2016 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 
46 struct module;
47 struct kiocb;
48 struct iovec;
49 struct dentry;
50 struct page;
51 struct file_lock;
52 struct pipe_inode_info;
53 struct vm_area_struct;
54 struct poll_table_struct;
55 struct files_struct;
56 
57 #define	inode	vnode
58 #define	i_cdev	v_rdev
59 
60 #define	S_IRUGO	(S_IRUSR | S_IRGRP | S_IROTH)
61 #define	S_IWUGO	(S_IWUSR | S_IWGRP | S_IWOTH)
62 
63 
64 typedef struct files_struct *fl_owner_t;
65 
66 struct dentry {
67 	struct inode	*d_inode;
68 };
69 
70 struct file_operations;
71 
72 struct linux_file {
73 	struct file	*_file;
74 	const struct file_operations	*f_op;
75 	void 		*private_data;
76 	int		f_flags;
77 	int		f_mode;	/* Just starting mode. */
78 	struct dentry	*f_dentry;
79 	struct dentry	f_dentry_store;
80 	struct selinfo	f_selinfo;
81 	struct sigio	*f_sigio;
82 	struct vnode	*f_vnode;
83 #define	f_inode	f_vnode
84 	volatile u_int	f_count;
85 
86 	/* anonymous shmem object */
87 	vm_object_t	f_shmem;
88 
89 	/* kqfilter support */
90 	int		f_kqflags;
91 #define	LINUX_KQ_FLAG_HAS_READ (1 << 0)
92 #define	LINUX_KQ_FLAG_HAS_WRITE (1 << 1)
93 #define	LINUX_KQ_FLAG_NEED_READ (1 << 2)
94 #define	LINUX_KQ_FLAG_NEED_WRITE (1 << 3)
95 	/* protects f_selinfo.si_note */
96 	spinlock_t	f_kqlock;
97 };
98 
99 #define	file		linux_file
100 #define	fasync_struct	sigio *
101 
102 #define	fasync_helper(fd, filp, on, queue)				\
103 ({									\
104 	if ((on))							\
105 		*(queue) = &(filp)->f_sigio;				\
106 	else								\
107 		*(queue) = NULL;					\
108 	0;								\
109 })
110 
111 #define	kill_fasync(queue, sig, pollstat)				\
112 do {									\
113 	if (*(queue) != NULL)						\
114 		pgsigio(*(queue), (sig), 0);				\
115 } while (0)
116 
117 typedef int (*filldir_t)(void *, const char *, int, loff_t, u64, unsigned);
118 
119 struct file_operations {
120 	struct module *owner;
121 	ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
122 	ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
123 	unsigned int (*poll) (struct file *, struct poll_table_struct *);
124 	long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
125 	int (*mmap)(struct file *, struct vm_area_struct *);
126 	int (*open)(struct inode *, struct file *);
127 	int (*release)(struct inode *, struct file *);
128 	int (*fasync)(int, struct file *, int);
129 
130 /* Although not supported in FreeBSD, to align with Linux code
131  * we are adding llseek() only when it is mapped to no_llseek which returns
132  * an illegal seek error
133  */
134 	loff_t (*llseek)(struct file *, loff_t, int);
135 #if 0
136 	/* We do not support these methods.  Don't permit them to compile. */
137 	loff_t (*llseek)(struct file *, loff_t, int);
138 	ssize_t (*aio_read)(struct kiocb *, const struct iovec *,
139 	    unsigned long, loff_t);
140 	ssize_t (*aio_write)(struct kiocb *, const struct iovec *,
141 	    unsigned long, loff_t);
142 	int (*readdir)(struct file *, void *, filldir_t);
143 	int (*ioctl)(struct inode *, struct file *, unsigned int,
144 	    unsigned long);
145 	long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
146 	int (*flush)(struct file *, fl_owner_t id);
147 	int (*fsync)(struct file *, struct dentry *, int datasync);
148 	int (*aio_fsync)(struct kiocb *, int datasync);
149 	int (*lock)(struct file *, int, struct file_lock *);
150 	ssize_t (*sendpage)(struct file *, struct page *, int, size_t,
151 	    loff_t *, int);
152 	unsigned long (*get_unmapped_area)(struct file *, unsigned long,
153 	    unsigned long, unsigned long, unsigned long);
154 	int (*check_flags)(int);
155 	int (*flock)(struct file *, int, struct file_lock *);
156 	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
157 	    loff_t *, size_t, unsigned int);
158 	ssize_t (*splice_read)(struct file *, loff_t *,
159 	    struct pipe_inode_info *, size_t, unsigned int);
160 	int (*setlease)(struct file *, long, struct file_lock **);
161 #endif
162 };
163 #define	fops_get(fops)		(fops)
164 #define	replace_fops(f, fops)	((f)->f_op = (fops))
165 
166 #define	FMODE_READ	FREAD
167 #define	FMODE_WRITE	FWRITE
168 #define	FMODE_EXEC	FEXEC
169 
170 int __register_chrdev(unsigned int major, unsigned int baseminor,
171     unsigned int count, const char *name,
172     const struct file_operations *fops);
173 int __register_chrdev_p(unsigned int major, unsigned int baseminor,
174     unsigned int count, const char *name,
175     const struct file_operations *fops, uid_t uid,
176     gid_t gid, int mode);
177 void __unregister_chrdev(unsigned int major, unsigned int baseminor,
178     unsigned int count, const char *name);
179 
180 static inline void
181 unregister_chrdev(unsigned int major, const char *name)
182 {
183 
184 	__unregister_chrdev(major, 0, 256, name);
185 }
186 
187 static inline int
188 register_chrdev(unsigned int major, const char *name,
189     const struct file_operations *fops)
190 {
191 
192 	return (__register_chrdev(major, 0, 256, name, fops));
193 }
194 
195 static inline int
196 register_chrdev_p(unsigned int major, const char *name,
197     const struct file_operations *fops, uid_t uid, gid_t gid, int mode)
198 {
199 
200 	return (__register_chrdev_p(major, 0, 256, name, fops, uid, gid, mode));
201 }
202 
203 static inline int
204 register_chrdev_region(dev_t dev, unsigned range, const char *name)
205 {
206 
207 	return 0;
208 }
209 
210 static inline void
211 unregister_chrdev_region(dev_t dev, unsigned range)
212 {
213 
214 	return;
215 }
216 
217 static inline int
218 alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
219 			const char *name)
220 {
221 
222 	return 0;
223 }
224 
225 /* No current support for seek op in FreeBSD */
226 static inline int
227 nonseekable_open(struct inode *inode, struct file *filp)
228 {
229 	return 0;
230 }
231 
232 extern unsigned int linux_iminor(struct inode *);
233 #define	iminor(...) linux_iminor(__VA_ARGS__)
234 
235 static inline struct linux_file *
236 get_file(struct linux_file *f)
237 {
238 
239 	refcount_acquire(f->_file == NULL ? &f->f_count : &f->_file->f_count);
240 	return (f);
241 }
242 
243 static inline struct inode *
244 igrab(struct inode *inode)
245 {
246 	int error;
247 
248 	error = vget(inode, 0, curthread);
249 	if (error)
250 		return (NULL);
251 
252 	return (inode);
253 }
254 
255 static inline void
256 iput(struct inode *inode)
257 {
258 
259 	vrele(inode);
260 }
261 
262 static inline loff_t
263 no_llseek(struct file *file, loff_t offset, int whence)
264 {
265 
266 	return (-ESPIPE);
267 }
268 
269 static inline loff_t
270 noop_llseek(struct linux_file *file, loff_t offset, int whence)
271 {
272 
273 	return (file->_file->f_offset);
274 }
275 
276 #endif /* _LINUX_FS_H_ */
277