xref: /freebsd/sys/compat/linuxkpi/common/include/linux/fs.h (revision e9ac41698b2f322d55ccf9da50a3596edb2c1800)
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 #ifndef	_LINUXKPI_LINUX_FS_H_
30 #define	_LINUXKPI_LINUX_FS_H_
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/vnode.h>
36 #include <sys/file.h>
37 #include <sys/filedesc.h>
38 #include <linux/types.h>
39 #include <linux/wait.h>
40 #include <linux/semaphore.h>
41 #include <linux/spinlock.h>
42 #include <linux/dcache.h>
43 #include <linux/capability.h>
44 #include <linux/wait_bit.h>
45 #include <linux/kernel.h>
46 #include <linux/mutex.h>
47 
48 struct module;
49 struct kiocb;
50 struct iovec;
51 struct dentry;
52 struct page;
53 struct file_lock;
54 struct pipe_inode_info;
55 struct vm_area_struct;
56 struct poll_table_struct;
57 struct files_struct;
58 struct pfs_node;
59 struct linux_cdev;
60 
61 #define	inode	vnode
62 #define	i_cdev	v_rdev
63 #define	i_private v_data
64 
65 #define	S_IRUGO	(S_IRUSR | S_IRGRP | S_IROTH)
66 #define	S_IWUGO	(S_IWUSR | S_IWGRP | S_IWOTH)
67 
68 typedef struct files_struct *fl_owner_t;
69 
70 struct file_operations;
71 
72 struct linux_file_wait_queue {
73 	struct wait_queue wq;
74 	struct wait_queue_head *wqh;
75 	atomic_t state;
76 #define	LINUX_FWQ_STATE_INIT 0
77 #define	LINUX_FWQ_STATE_NOT_READY 1
78 #define	LINUX_FWQ_STATE_QUEUED 2
79 #define	LINUX_FWQ_STATE_READY 3
80 #define	LINUX_FWQ_STATE_MAX 4
81 };
82 
83 struct linux_file {
84 	struct file	*_file;
85 	const struct file_operations	*f_op;
86 	void		*private_data;
87 	int		f_flags;
88 	int		f_mode;	/* Just starting mode. */
89 	struct dentry	*f_dentry;
90 	struct dentry	f_dentry_store;
91 	struct selinfo	f_selinfo;
92 	struct sigio	*f_sigio;
93 	struct vnode	*f_vnode;
94 #define	f_inode	f_vnode
95 	volatile u_int	f_count;
96 
97 	/* anonymous shmem object */
98 	vm_object_t	f_shmem;
99 
100 	/* kqfilter support */
101 	int		f_kqflags;
102 #define	LINUX_KQ_FLAG_HAS_READ (1 << 0)
103 #define	LINUX_KQ_FLAG_HAS_WRITE (1 << 1)
104 #define	LINUX_KQ_FLAG_NEED_READ (1 << 2)
105 #define	LINUX_KQ_FLAG_NEED_WRITE (1 << 3)
106 	/* protects f_selinfo.si_note */
107 	spinlock_t	f_kqlock;
108 	struct linux_file_wait_queue f_wait_queue;
109 
110 	/* pointer to associated character device, if any */
111 	struct linux_cdev *f_cdev;
112 
113 	struct rcu_head	rcu;
114 };
115 
116 #define	file		linux_file
117 #define	fasync_struct	sigio *
118 
119 #define	fasync_helper(fd, filp, on, queue)				\
120 ({									\
121 	if ((on))							\
122 		*(queue) = &(filp)->f_sigio;				\
123 	else								\
124 		*(queue) = NULL;					\
125 	0;								\
126 })
127 
128 #define	kill_fasync(queue, sig, pollstat)				\
129 do {									\
130 	if (*(queue) != NULL)						\
131 		pgsigio(*(queue), (sig), 0);				\
132 } while (0)
133 
134 typedef int (*filldir_t)(void *, const char *, int, off_t, u64, unsigned);
135 
136 struct file_operations {
137 	struct module *owner;
138 	ssize_t (*read)(struct linux_file *, char __user *, size_t, off_t *);
139 	ssize_t (*write)(struct linux_file *, const char __user *, size_t, off_t *);
140 	unsigned int (*poll) (struct linux_file *, struct poll_table_struct *);
141 	long (*unlocked_ioctl)(struct linux_file *, unsigned int, unsigned long);
142 	long (*compat_ioctl)(struct linux_file *, unsigned int, unsigned long);
143 	int (*mmap)(struct linux_file *, struct vm_area_struct *);
144 	int (*open)(struct inode *, struct file *);
145 	int (*release)(struct inode *, struct linux_file *);
146 	int (*fasync)(int, struct linux_file *, int);
147 
148 /* Although not supported in FreeBSD, to align with Linux code
149  * we are adding llseek() only when it is mapped to no_llseek which returns
150  * an illegal seek error
151  */
152 	off_t (*llseek)(struct linux_file *, off_t, int);
153 #if 0
154 	/* We do not support these methods.  Don't permit them to compile. */
155 	loff_t (*llseek)(struct file *, loff_t, int);
156 	ssize_t (*aio_read)(struct kiocb *, const struct iovec *,
157 	    unsigned long, loff_t);
158 	ssize_t (*aio_write)(struct kiocb *, const struct iovec *,
159 	    unsigned long, loff_t);
160 	int (*readdir)(struct file *, void *, filldir_t);
161 	int (*ioctl)(struct inode *, struct file *, unsigned int,
162 	    unsigned long);
163 	int (*flush)(struct file *, fl_owner_t id);
164 	int (*fsync)(struct file *, struct dentry *, int datasync);
165 	int (*aio_fsync)(struct kiocb *, int datasync);
166 	int (*lock)(struct file *, int, struct file_lock *);
167 	ssize_t (*sendpage)(struct file *, struct page *, int, size_t,
168 	    loff_t *, int);
169 	unsigned long (*get_unmapped_area)(struct file *, unsigned long,
170 	    unsigned long, unsigned long, unsigned long);
171 	int (*check_flags)(int);
172 	int (*flock)(struct file *, int, struct file_lock *);
173 	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
174 	    loff_t *, size_t, unsigned int);
175 	ssize_t (*splice_read)(struct file *, loff_t *,
176 	    struct pipe_inode_info *, size_t, unsigned int);
177 	int (*setlease)(struct file *, long, struct file_lock **);
178 #endif
179 };
180 #define	fops_get(fops)		(fops)
181 #define	replace_fops(f, fops)	((f)->f_op = (fops))
182 
183 #define	FMODE_READ	FREAD
184 #define	FMODE_WRITE	FWRITE
185 #define	FMODE_EXEC	FEXEC
186 #define	FMODE_UNSIGNED_OFFSET	0x2000
187 int __register_chrdev(unsigned int major, unsigned int baseminor,
188     unsigned int count, const char *name,
189     const struct file_operations *fops);
190 int __register_chrdev_p(unsigned int major, unsigned int baseminor,
191     unsigned int count, const char *name,
192     const struct file_operations *fops, uid_t uid,
193     gid_t gid, int mode);
194 void __unregister_chrdev(unsigned int major, unsigned int baseminor,
195     unsigned int count, const char *name);
196 
197 static inline void
198 unregister_chrdev(unsigned int major, const char *name)
199 {
200 
201 	__unregister_chrdev(major, 0, 256, name);
202 }
203 
204 static inline int
205 register_chrdev(unsigned int major, const char *name,
206     const struct file_operations *fops)
207 {
208 
209 	return (__register_chrdev(major, 0, 256, name, fops));
210 }
211 
212 static inline int
213 register_chrdev_p(unsigned int major, const char *name,
214     const struct file_operations *fops, uid_t uid, gid_t gid, int mode)
215 {
216 
217 	return (__register_chrdev_p(major, 0, 256, name, fops, uid, gid, mode));
218 }
219 
220 static inline int
221 register_chrdev_region(dev_t dev, unsigned range, const char *name)
222 {
223 
224 	return 0;
225 }
226 
227 static inline void
228 unregister_chrdev_region(dev_t dev, unsigned range)
229 {
230 
231 	return;
232 }
233 
234 static inline int
235 alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
236 			const char *name)
237 {
238 
239 	return 0;
240 }
241 
242 /* No current support for seek op in FreeBSD */
243 static inline int
244 nonseekable_open(struct inode *inode, struct file *filp)
245 {
246 	return 0;
247 }
248 
249 static inline int
250 simple_open(struct inode *inode, struct file *filp)
251 {
252 	filp->private_data = inode->i_private;
253 	return 0;
254 }
255 
256 extern unsigned int linux_iminor(struct inode *);
257 #define	iminor(...) linux_iminor(__VA_ARGS__)
258 
259 static inline struct linux_file *
260 get_file(struct linux_file *f)
261 {
262 
263 	refcount_acquire(f->_file == NULL ? &f->f_count : &f->_file->f_count);
264 	return (f);
265 }
266 
267 static inline bool
268 get_file_rcu(struct linux_file *f)
269 {
270 	return (refcount_acquire_if_not_zero(
271 	    f->_file == NULL ? &f->f_count : &f->_file->f_count));
272 }
273 
274 static inline struct inode *
275 igrab(struct inode *inode)
276 {
277 	int error;
278 
279 	error = vget(inode, 0);
280 	if (error)
281 		return (NULL);
282 
283 	return (inode);
284 }
285 
286 static inline void
287 iput(struct inode *inode)
288 {
289 
290 	vrele(inode);
291 }
292 
293 static inline loff_t
294 no_llseek(struct file *file, loff_t offset, int whence)
295 {
296 
297 	return (-ESPIPE);
298 }
299 
300 static inline loff_t
301 default_llseek(struct file *file, loff_t offset, int whence)
302 {
303 	return (no_llseek(file, offset, whence));
304 }
305 
306 static inline loff_t
307 generic_file_llseek(struct file *file, loff_t offset, int whence)
308 {
309 	return (no_llseek(file, offset, whence));
310 }
311 
312 static inline loff_t
313 noop_llseek(struct linux_file *file, loff_t offset, int whence)
314 {
315 
316 	return (file->_file->f_offset);
317 }
318 
319 static inline struct vnode *
320 file_inode(const struct linux_file *file)
321 {
322 
323 	return (file->f_vnode);
324 }
325 
326 static inline int
327 call_mmap(struct linux_file *file, struct vm_area_struct *vma)
328 {
329 
330 	return (file->f_op->mmap(file, vma));
331 }
332 
333 static inline void
334 i_size_write(struct inode *inode, loff_t i_size)
335 {
336 }
337 
338 /*
339  * simple_read_from_buffer: copy data from kernel-space origin
340  * buffer into user-space destination buffer
341  *
342  * @dest: destination buffer
343  * @read_size: number of bytes to be transferred
344  * @ppos: starting transfer position pointer
345  * @orig: origin buffer
346  * @buf_size: size of destination and origin buffers
347  *
348  * Return value:
349  * On success, total bytes copied with *ppos incremented accordingly.
350  * On failure, negative value.
351  */
352 static inline ssize_t
353 simple_read_from_buffer(void __user *dest, size_t read_size, loff_t *ppos,
354     void *orig, size_t buf_size)
355 {
356 	void *read_pos = ((char *) orig) + *ppos;
357 	size_t buf_remain = buf_size - *ppos;
358 	ssize_t num_read;
359 
360 	if (buf_remain < 0 || buf_remain > buf_size)
361 		return -EINVAL;
362 
363 	if (read_size > buf_remain)
364 		read_size = buf_remain;
365 
366 	/* copy_to_user returns number of bytes NOT read */
367 	num_read = read_size - copy_to_user(dest, read_pos, read_size);
368 	if (num_read == 0)
369 		return -EFAULT;
370 	*ppos += num_read;
371 
372 	return (num_read);
373 }
374 
375 MALLOC_DECLARE(M_LSATTR);
376 
377 #define	__DEFINE_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt, __wrfunc)\
378 static inline int							\
379 __fops ## _open(struct inode *inode, struct file *filp)			\
380 {									\
381 	return (simple_attr_open(inode, filp, __get, __set, __fmt));	\
382 }									\
383 static const struct file_operations __fops = {				\
384 	.owner	 = THIS_MODULE,						\
385 	.open	 = __fops ## _open,					\
386 	.release = simple_attr_release,					\
387 	.read	 = simple_attr_read,					\
388 	.write	 = __wrfunc,						\
389 	.llseek	 = no_llseek						\
390 }
391 
392 #define	DEFINE_SIMPLE_ATTRIBUTE(fops, get, set, fmt)			\
393 	__DEFINE_SIMPLE_ATTRIBUTE(fops, get, set, fmt, simple_attr_write)
394 #define	DEFINE_SIMPLE_ATTRIBUTE_SIGNED(fops, get, set, fmt)		\
395 	__DEFINE_SIMPLE_ATTRIBUTE(fops, get, set, fmt, simple_attr_write_signed)
396 
397 int simple_attr_open(struct inode *inode, struct file *filp,
398     int (*get)(void *, uint64_t *), int (*set)(void *, uint64_t),
399     const char *fmt);
400 
401 int simple_attr_release(struct inode *inode, struct file *filp);
402 
403 ssize_t simple_attr_read(struct file *filp, char *buf, size_t read_size, loff_t *ppos);
404 
405 ssize_t simple_attr_write(struct file *filp, const char *buf, size_t write_size, loff_t *ppos);
406 
407 ssize_t simple_attr_write_signed(struct file *filp, const char *buf,
408 	    size_t write_size, loff_t *ppos);
409 
410 #endif /* _LINUXKPI_LINUX_FS_H_ */
411