xref: /freebsd/sys/compat/linuxkpi/common/include/linux/fs.h (revision a3cefe7f2b4df0f70ff92d4570ce18e517af43ec)
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 typedef unsigned int fop_flags_t;
137 
138 struct file_operations {
139 	struct module *owner;
140 	fop_flags_t fop_flags; /* Unused on FreeBSD. */
141 	ssize_t (*read)(struct linux_file *, char __user *, size_t, off_t *);
142 	ssize_t (*write)(struct linux_file *, const char __user *, size_t, off_t *);
143 	unsigned int (*poll) (struct linux_file *, struct poll_table_struct *);
144 	long (*unlocked_ioctl)(struct linux_file *, unsigned int, unsigned long);
145 	long (*compat_ioctl)(struct linux_file *, unsigned int, unsigned long);
146 	int (*mmap)(struct linux_file *, struct vm_area_struct *);
147 	int (*open)(struct inode *, struct file *);
148 	int (*release)(struct inode *, struct linux_file *);
149 	int (*fasync)(int, struct linux_file *, int);
150 
151 /* Although not supported in FreeBSD, to align with Linux code
152  * we are adding llseek() only when it is mapped to no_llseek which returns
153  * an illegal seek error
154  */
155 	off_t (*llseek)(struct linux_file *, off_t, int);
156 /*
157  * Not supported in FreeBSD. That's ok, we never call it and it allows some
158  * drivers like DRM drivers to compile without changes.
159  */
160 	void (*show_fdinfo)(struct seq_file *, struct file *);
161 #if 0
162 	/* We do not support these methods.  Don't permit them to compile. */
163 	loff_t (*llseek)(struct file *, loff_t, int);
164 	ssize_t (*aio_read)(struct kiocb *, const struct iovec *,
165 	    unsigned long, loff_t);
166 	ssize_t (*aio_write)(struct kiocb *, const struct iovec *,
167 	    unsigned long, loff_t);
168 	int (*readdir)(struct file *, void *, filldir_t);
169 	int (*ioctl)(struct inode *, struct file *, unsigned int,
170 	    unsigned long);
171 	int (*flush)(struct file *, fl_owner_t id);
172 	int (*fsync)(struct file *, struct dentry *, int datasync);
173 	int (*aio_fsync)(struct kiocb *, int datasync);
174 	int (*lock)(struct file *, int, struct file_lock *);
175 	ssize_t (*sendpage)(struct file *, struct page *, int, size_t,
176 	    loff_t *, int);
177 	unsigned long (*get_unmapped_area)(struct file *, unsigned long,
178 	    unsigned long, unsigned long, unsigned long);
179 	int (*check_flags)(int);
180 	int (*flock)(struct file *, int, struct file_lock *);
181 	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
182 	    loff_t *, size_t, unsigned int);
183 	ssize_t (*splice_read)(struct file *, loff_t *,
184 	    struct pipe_inode_info *, size_t, unsigned int);
185 	int (*setlease)(struct file *, long, struct file_lock **);
186 #endif
187 };
188 
189 #define	FOP_BUFFER_RASYNC	(1 << 0)
190 #define	FOP_BUFFER_WASYNC	(1 << 1)
191 #define	FOP_MMAP_SYNC		(1 << 2)
192 #define	FOP_DIO_PARALLEL_WRITE	(1 << 3)
193 #define	FOP_HUGE_PAGES		(1 << 4)
194 #define	FOP_UNSIGNED_OFFSET	(1 << 5)
195 
196 #define	fops_get(fops)		(fops)
197 #define	replace_fops(f, fops)	((f)->f_op = (fops))
198 
199 #define	FMODE_READ	FREAD
200 #define	FMODE_WRITE	FWRITE
201 #define	FMODE_EXEC	FEXEC
202 #define	FMODE_UNSIGNED_OFFSET	0x2000
203 int __register_chrdev(unsigned int major, unsigned int baseminor,
204     unsigned int count, const char *name,
205     const struct file_operations *fops);
206 int __register_chrdev_p(unsigned int major, unsigned int baseminor,
207     unsigned int count, const char *name,
208     const struct file_operations *fops, uid_t uid,
209     gid_t gid, int mode);
210 void __unregister_chrdev(unsigned int major, unsigned int baseminor,
211     unsigned int count, const char *name);
212 
213 static inline void
214 unregister_chrdev(unsigned int major, const char *name)
215 {
216 
217 	__unregister_chrdev(major, 0, 256, name);
218 }
219 
220 static inline int
221 register_chrdev(unsigned int major, const char *name,
222     const struct file_operations *fops)
223 {
224 
225 	return (__register_chrdev(major, 0, 256, name, fops));
226 }
227 
228 static inline int
229 register_chrdev_p(unsigned int major, const char *name,
230     const struct file_operations *fops, uid_t uid, gid_t gid, int mode)
231 {
232 
233 	return (__register_chrdev_p(major, 0, 256, name, fops, uid, gid, mode));
234 }
235 
236 static inline int
237 register_chrdev_region(dev_t dev, unsigned range, const char *name)
238 {
239 
240 	return 0;
241 }
242 
243 static inline void
244 unregister_chrdev_region(dev_t dev, unsigned range)
245 {
246 
247 	return;
248 }
249 
250 static inline int
251 alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
252 			const char *name)
253 {
254 
255 	return 0;
256 }
257 
258 /* No current support for seek op in FreeBSD */
259 static inline int
260 nonseekable_open(struct inode *inode, struct file *filp)
261 {
262 	return 0;
263 }
264 
265 static inline int
266 simple_open(struct inode *inode, struct file *filp)
267 {
268 	filp->private_data = inode->i_private;
269 	return 0;
270 }
271 
272 extern unsigned int linux_iminor(struct inode *);
273 #define	iminor(...) linux_iminor(__VA_ARGS__)
274 
275 static inline struct linux_file *
276 get_file(struct linux_file *f)
277 {
278 
279 	refcount_acquire(f->_file == NULL ? &f->f_count : &f->_file->f_count);
280 	return (f);
281 }
282 
283 struct linux_file * linux_get_file_rcu(struct linux_file **f);
284 struct linux_file * get_file_active(struct linux_file **f);
285 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION < 60700
286 static inline bool
287 get_file_rcu(struct linux_file *f)
288 {
289 	return (refcount_acquire_if_not_zero(
290 	    f->_file == NULL ? &f->f_count : &f->_file->f_count));
291 }
292 #else
293 #define	get_file_rcu(f)	linux_get_file_rcu(f)
294 #endif
295 
296 static inline struct inode *
297 igrab(struct inode *inode)
298 {
299 	int error;
300 
301 	error = vget(inode, 0);
302 	if (error)
303 		return (NULL);
304 
305 	return (inode);
306 }
307 
308 static inline void
309 iput(struct inode *inode)
310 {
311 
312 	vrele(inode);
313 }
314 
315 static inline loff_t
316 no_llseek(struct file *file, loff_t offset, int whence)
317 {
318 
319 	return (-ESPIPE);
320 }
321 
322 static inline loff_t
323 default_llseek(struct file *file, loff_t offset, int whence)
324 {
325 	return (no_llseek(file, offset, whence));
326 }
327 
328 static inline loff_t
329 generic_file_llseek(struct file *file, loff_t offset, int whence)
330 {
331 	return (no_llseek(file, offset, whence));
332 }
333 
334 static inline loff_t
335 noop_llseek(struct linux_file *file, loff_t offset, int whence)
336 {
337 
338 	return (file->_file->f_offset);
339 }
340 
341 static inline struct vnode *
342 file_inode(const struct linux_file *file)
343 {
344 
345 	return (file->f_vnode);
346 }
347 
348 static inline int
349 call_mmap(struct linux_file *file, struct vm_area_struct *vma)
350 {
351 
352 	return (file->f_op->mmap(file, vma));
353 }
354 
355 static inline void
356 i_size_write(struct inode *inode, loff_t i_size)
357 {
358 }
359 
360 /*
361  * simple_read_from_buffer: copy data from kernel-space origin
362  * buffer into user-space destination buffer
363  *
364  * @dest: destination buffer
365  * @read_size: number of bytes to be transferred
366  * @ppos: starting transfer position pointer
367  * @orig: origin buffer
368  * @buf_size: size of destination and origin buffers
369  *
370  * Return value:
371  * On success, total bytes copied with *ppos incremented accordingly.
372  * On failure, negative value.
373  */
374 static inline ssize_t
375 simple_read_from_buffer(void __user *dest, size_t read_size, loff_t *ppos,
376     void *orig, size_t buf_size)
377 {
378 	void *read_pos = ((char *) orig) + *ppos;
379 	size_t buf_remain = buf_size - *ppos;
380 	ssize_t num_read;
381 
382 	if (*ppos >= buf_size || read_size == 0)
383 		return (0);
384 
385 	if (read_size > buf_remain)
386 		read_size = buf_remain;
387 
388 	/* copy_to_user returns number of bytes NOT read */
389 	num_read = read_size - copy_to_user(dest, read_pos, read_size);
390 	if (num_read == 0)
391 		return -EFAULT;
392 	*ppos += num_read;
393 
394 	return (num_read);
395 }
396 
397 MALLOC_DECLARE(M_LSATTR);
398 
399 #define	__DEFINE_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt, __wrfunc)\
400 static inline int							\
401 __fops ## _open(struct inode *inode, struct file *filp)			\
402 {									\
403 	return (simple_attr_open(inode, filp, __get, __set, __fmt));	\
404 }									\
405 static const struct file_operations __fops = {				\
406 	.owner	 = THIS_MODULE,						\
407 	.open	 = __fops ## _open,					\
408 	.release = simple_attr_release,					\
409 	.read	 = simple_attr_read,					\
410 	.write	 = __wrfunc,						\
411 	.llseek	 = no_llseek						\
412 }
413 
414 #define	DEFINE_SIMPLE_ATTRIBUTE(fops, get, set, fmt)			\
415 	__DEFINE_SIMPLE_ATTRIBUTE(fops, get, set, fmt, simple_attr_write)
416 #define	DEFINE_SIMPLE_ATTRIBUTE_SIGNED(fops, get, set, fmt)		\
417 	__DEFINE_SIMPLE_ATTRIBUTE(fops, get, set, fmt, simple_attr_write_signed)
418 
419 int simple_attr_open(struct inode *inode, struct file *filp,
420     int (*get)(void *, uint64_t *), int (*set)(void *, uint64_t),
421     const char *fmt);
422 
423 int simple_attr_release(struct inode *inode, struct file *filp);
424 
425 ssize_t simple_attr_read(struct file *filp, char __user *buf, size_t read_size,
426     loff_t *ppos);
427 
428 ssize_t simple_attr_write(struct file *filp, const char __user *buf,
429     size_t write_size, loff_t *ppos);
430 
431 ssize_t simple_attr_write_signed(struct file *filp, const char __user *buf,
432 	    size_t write_size, loff_t *ppos);
433 
434 #endif /* _LINUXKPI_LINUX_FS_H_ */
435