1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc.
3219820Sjeff * Copyright (c) 2010 iX Systems, Inc.
4219820Sjeff * Copyright (c) 2010 Panasas, Inc.
5341850Shselasky * Copyright (c) 2013-2018 Mellanox Technologies, Ltd.
6219820Sjeff * All rights reserved.
7219820Sjeff *
8219820Sjeff * Redistribution and use in source and binary forms, with or without
9219820Sjeff * modification, are permitted provided that the following conditions
10219820Sjeff * are met:
11219820Sjeff * 1. Redistributions of source code must retain the above copyright
12219820Sjeff *    notice unmodified, this list of conditions, and the following
13219820Sjeff *    disclaimer.
14219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
15219820Sjeff *    notice, this list of conditions and the following disclaimer in the
16219820Sjeff *    documentation and/or other materials provided with the distribution.
17219820Sjeff *
18219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28289644Shselasky *
29289644Shselasky * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/fs.h 341850 2018-12-12 10:09:23Z hselasky $
30219820Sjeff */
31219820Sjeff#ifndef	_LINUX_FS_H_
32219820Sjeff#define	_LINUX_FS_H_
33219820Sjeff
34280764Shselasky#include <sys/cdefs.h>
35280764Shselasky#include <sys/param.h>
36219820Sjeff#include <sys/systm.h>
37219820Sjeff#include <sys/conf.h>
38219820Sjeff#include <sys/vnode.h>
39219820Sjeff#include <sys/file.h>
40219820Sjeff#include <sys/filedesc.h>
41219820Sjeff#include <linux/types.h>
42219820Sjeff#include <linux/wait.h>
43219820Sjeff#include <linux/semaphore.h>
44328653Shselasky#include <linux/spinlock.h>
45329966Shselasky#include <linux/dcache.h>
46219820Sjeff
47219820Sjeffstruct module;
48219820Sjeffstruct kiocb;
49219820Sjeffstruct iovec;
50219820Sjeffstruct dentry;
51219820Sjeffstruct page;
52219820Sjeffstruct file_lock;
53219820Sjeffstruct pipe_inode_info;
54219820Sjeffstruct vm_area_struct;
55219820Sjeffstruct poll_table_struct;
56219820Sjeffstruct files_struct;
57328653Shselaskystruct pfs_node;
58341850Shselaskystruct linux_cdev;
59219820Sjeff
60219820Sjeff#define	inode	vnode
61219820Sjeff#define	i_cdev	v_rdev
62328653Shselasky#define	i_private v_data
63219820Sjeff
64219820Sjeff#define	S_IRUGO	(S_IRUSR | S_IRGRP | S_IROTH)
65219820Sjeff#define	S_IWUGO	(S_IWUSR | S_IWGRP | S_IWOTH)
66219820Sjeff
67219820Sjeff
68219820Sjefftypedef struct files_struct *fl_owner_t;
69219820Sjeff
70219820Sjeffstruct file_operations;
71219820Sjeff
72328653Shselaskystruct linux_file_wait_queue {
73328653Shselasky	struct wait_queue wq;
74328653Shselasky	struct wait_queue_head *wqh;
75328653Shselasky	atomic_t state;
76328653Shselasky#define	LINUX_FWQ_STATE_INIT 0
77328653Shselasky#define	LINUX_FWQ_STATE_NOT_READY 1
78328653Shselasky#define	LINUX_FWQ_STATE_QUEUED 2
79328653Shselasky#define	LINUX_FWQ_STATE_READY 3
80328653Shselasky#define	LINUX_FWQ_STATE_MAX 4
81328653Shselasky};
82328653Shselasky
83219820Sjeffstruct linux_file {
84219820Sjeff	struct file	*_file;
85219820Sjeff	const struct file_operations	*f_op;
86331756Semaste	void		*private_data;
87219820Sjeff	int		f_flags;
88219820Sjeff	int		f_mode;	/* Just starting mode. */
89219820Sjeff	struct dentry	*f_dentry;
90219820Sjeff	struct dentry	f_dentry_store;
91219820Sjeff	struct selinfo	f_selinfo;
92219820Sjeff	struct sigio	*f_sigio;
93251617Sjhb	struct vnode	*f_vnode;
94328653Shselasky#define	f_inode	f_vnode
95328653Shselasky	volatile u_int	f_count;
96328653Shselasky
97328653Shselasky	/* anonymous shmem object */
98328653Shselasky	vm_object_t	f_shmem;
99328653Shselasky
100328653Shselasky	/* kqfilter support */
101328653Shselasky	int		f_kqflags;
102328653Shselasky#define	LINUX_KQ_FLAG_HAS_READ (1 << 0)
103328653Shselasky#define	LINUX_KQ_FLAG_HAS_WRITE (1 << 1)
104328653Shselasky#define	LINUX_KQ_FLAG_NEED_READ (1 << 2)
105328653Shselasky#define	LINUX_KQ_FLAG_NEED_WRITE (1 << 3)
106328653Shselasky	/* protects f_selinfo.si_note */
107328653Shselasky	spinlock_t	f_kqlock;
108328653Shselasky	struct linux_file_wait_queue f_wait_queue;
109341850Shselasky
110341850Shselasky	/* pointer to associated character device, if any */
111341850Shselasky	struct linux_cdev *f_cdev;
112219820Sjeff};
113219820Sjeff
114219820Sjeff#define	file		linux_file
115219820Sjeff#define	fasync_struct	sigio *
116219820Sjeff
117219820Sjeff#define	fasync_helper(fd, filp, on, queue)				\
118219820Sjeff({									\
119219820Sjeff	if ((on))							\
120219820Sjeff		*(queue) = &(filp)->f_sigio;				\
121219820Sjeff	else								\
122219820Sjeff		*(queue) = NULL;					\
123219820Sjeff	0;								\
124219820Sjeff})
125219820Sjeff
126219820Sjeff#define	kill_fasync(queue, sig, pollstat)				\
127219820Sjeffdo {									\
128219820Sjeff	if (*(queue) != NULL)						\
129219820Sjeff		pgsigio(*(queue), (sig), 0);				\
130219820Sjeff} while (0)
131219820Sjeff
132219820Sjefftypedef int (*filldir_t)(void *, const char *, int, loff_t, u64, unsigned);
133219820Sjeff
134219820Sjeffstruct file_operations {
135219820Sjeff	struct module *owner;
136219820Sjeff	ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
137219820Sjeff	ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
138219820Sjeff	unsigned int (*poll) (struct file *, struct poll_table_struct *);
139219820Sjeff	long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
140328653Shselasky	long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
141219820Sjeff	int (*mmap)(struct file *, struct vm_area_struct *);
142219820Sjeff	int (*open)(struct inode *, struct file *);
143219820Sjeff	int (*release)(struct inode *, struct file *);
144219820Sjeff	int (*fasync)(int, struct file *, int);
145270710Shselasky
146270710Shselasky/* Although not supported in FreeBSD, to align with Linux code
147331756Semaste * we are adding llseek() only when it is mapped to no_llseek which returns
148270710Shselasky * an illegal seek error
149270710Shselasky */
150270710Shselasky	loff_t (*llseek)(struct file *, loff_t, int);
151219820Sjeff#if 0
152219820Sjeff	/* We do not support these methods.  Don't permit them to compile. */
153219820Sjeff	loff_t (*llseek)(struct file *, loff_t, int);
154219820Sjeff	ssize_t (*aio_read)(struct kiocb *, const struct iovec *,
155219820Sjeff	    unsigned long, loff_t);
156219820Sjeff	ssize_t (*aio_write)(struct kiocb *, const struct iovec *,
157219820Sjeff	    unsigned long, loff_t);
158219820Sjeff	int (*readdir)(struct file *, void *, filldir_t);
159219820Sjeff	int (*ioctl)(struct inode *, struct file *, unsigned int,
160219820Sjeff	    unsigned long);
161219820Sjeff	int (*flush)(struct file *, fl_owner_t id);
162219820Sjeff	int (*fsync)(struct file *, struct dentry *, int datasync);
163219820Sjeff	int (*aio_fsync)(struct kiocb *, int datasync);
164219820Sjeff	int (*lock)(struct file *, int, struct file_lock *);
165219820Sjeff	ssize_t (*sendpage)(struct file *, struct page *, int, size_t,
166219820Sjeff	    loff_t *, int);
167219820Sjeff	unsigned long (*get_unmapped_area)(struct file *, unsigned long,
168219820Sjeff	    unsigned long, unsigned long, unsigned long);
169219820Sjeff	int (*check_flags)(int);
170219820Sjeff	int (*flock)(struct file *, int, struct file_lock *);
171219820Sjeff	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
172219820Sjeff	    loff_t *, size_t, unsigned int);
173219820Sjeff	ssize_t (*splice_read)(struct file *, loff_t *,
174219820Sjeff	    struct pipe_inode_info *, size_t, unsigned int);
175219820Sjeff	int (*setlease)(struct file *, long, struct file_lock **);
176219820Sjeff#endif
177219820Sjeff};
178328653Shselasky#define	fops_get(fops)		(fops)
179328653Shselasky#define	replace_fops(f, fops)	((f)->f_op = (fops))
180219820Sjeff
181219820Sjeff#define	FMODE_READ	FREAD
182219820Sjeff#define	FMODE_WRITE	FWRITE
183219820Sjeff#define	FMODE_EXEC	FEXEC
184219820Sjeff
185311803Shselaskyint __register_chrdev(unsigned int major, unsigned int baseminor,
186311803Shselasky    unsigned int count, const char *name,
187311803Shselasky    const struct file_operations *fops);
188311803Shselaskyint __register_chrdev_p(unsigned int major, unsigned int baseminor,
189311803Shselasky    unsigned int count, const char *name,
190311803Shselasky    const struct file_operations *fops, uid_t uid,
191311803Shselasky    gid_t gid, int mode);
192311803Shselaskyvoid __unregister_chrdev(unsigned int major, unsigned int baseminor,
193311803Shselasky    unsigned int count, const char *name);
194311803Shselasky
195311803Shselaskystatic inline void
196311803Shselaskyunregister_chrdev(unsigned int major, const char *name)
197311803Shselasky{
198311803Shselasky
199311803Shselasky	__unregister_chrdev(major, 0, 256, name);
200311803Shselasky}
201311803Shselasky
202219820Sjeffstatic inline int
203311803Shselaskyregister_chrdev(unsigned int major, const char *name,
204311803Shselasky    const struct file_operations *fops)
205311803Shselasky{
206311803Shselasky
207311803Shselasky	return (__register_chrdev(major, 0, 256, name, fops));
208311803Shselasky}
209311803Shselasky
210311803Shselaskystatic inline int
211311803Shselaskyregister_chrdev_p(unsigned int major, const char *name,
212311803Shselasky    const struct file_operations *fops, uid_t uid, gid_t gid, int mode)
213311803Shselasky{
214311803Shselasky
215311803Shselasky	return (__register_chrdev_p(major, 0, 256, name, fops, uid, gid, mode));
216311803Shselasky}
217311803Shselasky
218311803Shselaskystatic inline int
219219820Sjeffregister_chrdev_region(dev_t dev, unsigned range, const char *name)
220219820Sjeff{
221219820Sjeff
222219820Sjeff	return 0;
223219820Sjeff}
224219820Sjeff
225219820Sjeffstatic inline void
226219820Sjeffunregister_chrdev_region(dev_t dev, unsigned range)
227219820Sjeff{
228219820Sjeff
229219820Sjeff	return;
230219820Sjeff}
231219820Sjeff
232270710Shselaskystatic inline int
233270710Shselaskyalloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
234270710Shselasky			const char *name)
235270710Shselasky{
236270710Shselasky
237270710Shselasky	return 0;
238270710Shselasky}
239270710Shselasky
240270710Shselasky/* No current support for seek op in FreeBSD */
241270710Shselaskystatic inline int
242270710Shselaskynonseekable_open(struct inode *inode, struct file *filp)
243270710Shselasky{
244270710Shselasky	return 0;
245270710Shselasky}
246270710Shselasky
247328653Shselaskyextern unsigned int linux_iminor(struct inode *);
248328653Shselasky#define	iminor(...) linux_iminor(__VA_ARGS__)
249328653Shselasky
250328653Shselaskystatic inline struct linux_file *
251328653Shselaskyget_file(struct linux_file *f)
252219820Sjeff{
253219820Sjeff
254328653Shselasky	refcount_acquire(f->_file == NULL ? &f->f_count : &f->_file->f_count);
255328653Shselasky	return (f);
256219820Sjeff}
257219820Sjeff
258219820Sjeffstatic inline struct inode *
259219820Sjeffigrab(struct inode *inode)
260219820Sjeff{
261219820Sjeff	int error;
262219820Sjeff
263219820Sjeff	error = vget(inode, 0, curthread);
264219820Sjeff	if (error)
265219820Sjeff		return (NULL);
266219820Sjeff
267219820Sjeff	return (inode);
268219820Sjeff}
269219820Sjeff
270219820Sjeffstatic inline void
271219820Sjeffiput(struct inode *inode)
272219820Sjeff{
273219820Sjeff
274219820Sjeff	vrele(inode);
275219820Sjeff}
276219820Sjeff
277331756Semastestatic inline loff_t
278270710Shselaskyno_llseek(struct file *file, loff_t offset, int whence)
279270710Shselasky{
280328653Shselasky
281328653Shselasky	return (-ESPIPE);
282270710Shselasky}
283270710Shselasky
284328653Shselaskystatic inline loff_t
285328653Shselaskynoop_llseek(struct linux_file *file, loff_t offset, int whence)
286328653Shselasky{
287328653Shselasky
288328653Shselasky	return (file->_file->f_offset);
289328653Shselasky}
290328653Shselasky
291329967Shselaskystatic inline struct vnode *
292329967Shselaskyfile_inode(const struct linux_file *file)
293329967Shselasky{
294329967Shselasky
295329967Shselasky	return (file->f_vnode);
296329967Shselasky}
297329967Shselasky
298329967Shselaskystatic inline int
299329967Shselaskycall_mmap(struct linux_file *file, struct vm_area_struct *vma)
300329967Shselasky{
301329967Shselasky
302329967Shselasky	return (file->f_op->mmap(file, vma));
303329967Shselasky}
304329967Shselasky
305328653Shselasky/* Shared memory support */
306328653Shselaskyunsigned long linux_invalidate_mapping_pages(vm_object_t, pgoff_t, pgoff_t);
307328653Shselaskystruct page *linux_shmem_read_mapping_page_gfp(vm_object_t, int, gfp_t);
308328653Shselaskystruct linux_file *linux_shmem_file_setup(const char *, loff_t, unsigned long);
309328653Shselaskyvoid linux_shmem_truncate_range(vm_object_t, loff_t, loff_t);
310328653Shselasky
311328653Shselasky#define	invalidate_mapping_pages(...) \
312328653Shselasky  linux_invalidate_mapping_pages(__VA_ARGS__)
313328653Shselasky
314328653Shselasky#define	shmem_read_mapping_page(...) \
315328653Shselasky  linux_shmem_read_mapping_page_gfp(__VA_ARGS__, 0)
316328653Shselasky
317328653Shselasky#define	shmem_read_mapping_page_gfp(...) \
318328653Shselasky  linux_shmem_read_mapping_page_gfp(__VA_ARGS__)
319328653Shselasky
320328653Shselasky#define	shmem_file_setup(...) \
321328653Shselasky  linux_shmem_file_setup(__VA_ARGS__)
322328653Shselasky
323328653Shselasky#define	shmem_truncate_range(...) \
324328653Shselasky  linux_shmem_truncate_range(__VA_ARGS__)
325328653Shselasky
326270710Shselasky#endif /* _LINUX_FS_H_ */
327