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