fs.h revision 329967
1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc.
3219820Sjeff * Copyright (c) 2010 iX Systems, Inc.
4219820Sjeff * Copyright (c) 2010 Panasas, Inc.
5328653Shselasky * Copyright (c) 2013-2017 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 329967 2018-02-25 10:31:27Z 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;
58219820Sjeff
59219820Sjeff#define	inode	vnode
60219820Sjeff#define	i_cdev	v_rdev
61328653Shselasky#define	i_private v_data
62219820Sjeff
63219820Sjeff#define	S_IRUGO	(S_IRUSR | S_IRGRP | S_IROTH)
64219820Sjeff#define	S_IWUGO	(S_IWUSR | S_IWGRP | S_IWOTH)
65219820Sjeff
66219820Sjeff
67219820Sjefftypedef struct files_struct *fl_owner_t;
68219820Sjeff
69219820Sjeffstruct file_operations;
70219820Sjeff
71328653Shselaskystruct linux_file_wait_queue {
72328653Shselasky	struct wait_queue wq;
73328653Shselasky	struct wait_queue_head *wqh;
74328653Shselasky	atomic_t state;
75328653Shselasky#define	LINUX_FWQ_STATE_INIT 0
76328653Shselasky#define	LINUX_FWQ_STATE_NOT_READY 1
77328653Shselasky#define	LINUX_FWQ_STATE_QUEUED 2
78328653Shselasky#define	LINUX_FWQ_STATE_READY 3
79328653Shselasky#define	LINUX_FWQ_STATE_MAX 4
80328653Shselasky};
81328653Shselasky
82219820Sjeffstruct linux_file {
83219820Sjeff	struct file	*_file;
84219820Sjeff	const struct file_operations	*f_op;
85219820Sjeff	void 		*private_data;
86219820Sjeff	int		f_flags;
87219820Sjeff	int		f_mode;	/* Just starting mode. */
88219820Sjeff	struct dentry	*f_dentry;
89219820Sjeff	struct dentry	f_dentry_store;
90219820Sjeff	struct selinfo	f_selinfo;
91219820Sjeff	struct sigio	*f_sigio;
92251617Sjhb	struct vnode	*f_vnode;
93328653Shselasky#define	f_inode	f_vnode
94328653Shselasky	volatile u_int	f_count;
95328653Shselasky
96328653Shselasky	/* anonymous shmem object */
97328653Shselasky	vm_object_t	f_shmem;
98328653Shselasky
99328653Shselasky	/* kqfilter support */
100328653Shselasky	int		f_kqflags;
101328653Shselasky#define	LINUX_KQ_FLAG_HAS_READ (1 << 0)
102328653Shselasky#define	LINUX_KQ_FLAG_HAS_WRITE (1 << 1)
103328653Shselasky#define	LINUX_KQ_FLAG_NEED_READ (1 << 2)
104328653Shselasky#define	LINUX_KQ_FLAG_NEED_WRITE (1 << 3)
105328653Shselasky	/* protects f_selinfo.si_note */
106328653Shselasky	spinlock_t	f_kqlock;
107328653Shselasky	struct linux_file_wait_queue f_wait_queue;
108219820Sjeff};
109219820Sjeff
110219820Sjeff#define	file		linux_file
111219820Sjeff#define	fasync_struct	sigio *
112219820Sjeff
113219820Sjeff#define	fasync_helper(fd, filp, on, queue)				\
114219820Sjeff({									\
115219820Sjeff	if ((on))							\
116219820Sjeff		*(queue) = &(filp)->f_sigio;				\
117219820Sjeff	else								\
118219820Sjeff		*(queue) = NULL;					\
119219820Sjeff	0;								\
120219820Sjeff})
121219820Sjeff
122219820Sjeff#define	kill_fasync(queue, sig, pollstat)				\
123219820Sjeffdo {									\
124219820Sjeff	if (*(queue) != NULL)						\
125219820Sjeff		pgsigio(*(queue), (sig), 0);				\
126219820Sjeff} while (0)
127219820Sjeff
128219820Sjefftypedef int (*filldir_t)(void *, const char *, int, loff_t, u64, unsigned);
129219820Sjeff
130219820Sjeffstruct file_operations {
131219820Sjeff	struct module *owner;
132219820Sjeff	ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
133219820Sjeff	ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
134219820Sjeff	unsigned int (*poll) (struct file *, struct poll_table_struct *);
135219820Sjeff	long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
136328653Shselasky	long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
137219820Sjeff	int (*mmap)(struct file *, struct vm_area_struct *);
138219820Sjeff	int (*open)(struct inode *, struct file *);
139219820Sjeff	int (*release)(struct inode *, struct file *);
140219820Sjeff	int (*fasync)(int, struct file *, int);
141270710Shselasky
142270710Shselasky/* Although not supported in FreeBSD, to align with Linux code
143270710Shselasky * we are adding llseek() only when it is mapped to no_llseek which returns
144270710Shselasky * an illegal seek error
145270710Shselasky */
146270710Shselasky	loff_t (*llseek)(struct file *, loff_t, int);
147219820Sjeff#if 0
148219820Sjeff	/* We do not support these methods.  Don't permit them to compile. */
149219820Sjeff	loff_t (*llseek)(struct file *, loff_t, int);
150219820Sjeff	ssize_t (*aio_read)(struct kiocb *, const struct iovec *,
151219820Sjeff	    unsigned long, loff_t);
152219820Sjeff	ssize_t (*aio_write)(struct kiocb *, const struct iovec *,
153219820Sjeff	    unsigned long, loff_t);
154219820Sjeff	int (*readdir)(struct file *, void *, filldir_t);
155219820Sjeff	int (*ioctl)(struct inode *, struct file *, unsigned int,
156219820Sjeff	    unsigned long);
157219820Sjeff	int (*flush)(struct file *, fl_owner_t id);
158219820Sjeff	int (*fsync)(struct file *, struct dentry *, int datasync);
159219820Sjeff	int (*aio_fsync)(struct kiocb *, int datasync);
160219820Sjeff	int (*lock)(struct file *, int, struct file_lock *);
161219820Sjeff	ssize_t (*sendpage)(struct file *, struct page *, int, size_t,
162219820Sjeff	    loff_t *, int);
163219820Sjeff	unsigned long (*get_unmapped_area)(struct file *, unsigned long,
164219820Sjeff	    unsigned long, unsigned long, unsigned long);
165219820Sjeff	int (*check_flags)(int);
166219820Sjeff	int (*flock)(struct file *, int, struct file_lock *);
167219820Sjeff	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
168219820Sjeff	    loff_t *, size_t, unsigned int);
169219820Sjeff	ssize_t (*splice_read)(struct file *, loff_t *,
170219820Sjeff	    struct pipe_inode_info *, size_t, unsigned int);
171219820Sjeff	int (*setlease)(struct file *, long, struct file_lock **);
172219820Sjeff#endif
173219820Sjeff};
174328653Shselasky#define	fops_get(fops)		(fops)
175328653Shselasky#define	replace_fops(f, fops)	((f)->f_op = (fops))
176219820Sjeff
177219820Sjeff#define	FMODE_READ	FREAD
178219820Sjeff#define	FMODE_WRITE	FWRITE
179219820Sjeff#define	FMODE_EXEC	FEXEC
180219820Sjeff
181311803Shselaskyint __register_chrdev(unsigned int major, unsigned int baseminor,
182311803Shselasky    unsigned int count, const char *name,
183311803Shselasky    const struct file_operations *fops);
184311803Shselaskyint __register_chrdev_p(unsigned int major, unsigned int baseminor,
185311803Shselasky    unsigned int count, const char *name,
186311803Shselasky    const struct file_operations *fops, uid_t uid,
187311803Shselasky    gid_t gid, int mode);
188311803Shselaskyvoid __unregister_chrdev(unsigned int major, unsigned int baseminor,
189311803Shselasky    unsigned int count, const char *name);
190311803Shselasky
191311803Shselaskystatic inline void
192311803Shselaskyunregister_chrdev(unsigned int major, const char *name)
193311803Shselasky{
194311803Shselasky
195311803Shselasky	__unregister_chrdev(major, 0, 256, name);
196311803Shselasky}
197311803Shselasky
198219820Sjeffstatic inline int
199311803Shselaskyregister_chrdev(unsigned int major, const char *name,
200311803Shselasky    const struct file_operations *fops)
201311803Shselasky{
202311803Shselasky
203311803Shselasky	return (__register_chrdev(major, 0, 256, name, fops));
204311803Shselasky}
205311803Shselasky
206311803Shselaskystatic inline int
207311803Shselaskyregister_chrdev_p(unsigned int major, const char *name,
208311803Shselasky    const struct file_operations *fops, uid_t uid, gid_t gid, int mode)
209311803Shselasky{
210311803Shselasky
211311803Shselasky	return (__register_chrdev_p(major, 0, 256, name, fops, uid, gid, mode));
212311803Shselasky}
213311803Shselasky
214311803Shselaskystatic inline int
215219820Sjeffregister_chrdev_region(dev_t dev, unsigned range, const char *name)
216219820Sjeff{
217219820Sjeff
218219820Sjeff	return 0;
219219820Sjeff}
220219820Sjeff
221219820Sjeffstatic inline void
222219820Sjeffunregister_chrdev_region(dev_t dev, unsigned range)
223219820Sjeff{
224219820Sjeff
225219820Sjeff	return;
226219820Sjeff}
227219820Sjeff
228270710Shselaskystatic inline int
229270710Shselaskyalloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
230270710Shselasky			const char *name)
231270710Shselasky{
232270710Shselasky
233270710Shselasky	return 0;
234270710Shselasky}
235270710Shselasky
236270710Shselasky/* No current support for seek op in FreeBSD */
237270710Shselaskystatic inline int
238270710Shselaskynonseekable_open(struct inode *inode, struct file *filp)
239270710Shselasky{
240270710Shselasky	return 0;
241270710Shselasky}
242270710Shselasky
243328653Shselaskyextern unsigned int linux_iminor(struct inode *);
244328653Shselasky#define	iminor(...) linux_iminor(__VA_ARGS__)
245328653Shselasky
246328653Shselaskystatic inline struct linux_file *
247328653Shselaskyget_file(struct linux_file *f)
248219820Sjeff{
249219820Sjeff
250328653Shselasky	refcount_acquire(f->_file == NULL ? &f->f_count : &f->_file->f_count);
251328653Shselasky	return (f);
252219820Sjeff}
253219820Sjeff
254219820Sjeffstatic inline struct inode *
255219820Sjeffigrab(struct inode *inode)
256219820Sjeff{
257219820Sjeff	int error;
258219820Sjeff
259219820Sjeff	error = vget(inode, 0, curthread);
260219820Sjeff	if (error)
261219820Sjeff		return (NULL);
262219820Sjeff
263219820Sjeff	return (inode);
264219820Sjeff}
265219820Sjeff
266219820Sjeffstatic inline void
267219820Sjeffiput(struct inode *inode)
268219820Sjeff{
269219820Sjeff
270219820Sjeff	vrele(inode);
271219820Sjeff}
272219820Sjeff
273270710Shselaskystatic inline loff_t
274270710Shselaskyno_llseek(struct file *file, loff_t offset, int whence)
275270710Shselasky{
276328653Shselasky
277328653Shselasky	return (-ESPIPE);
278270710Shselasky}
279270710Shselasky
280328653Shselaskystatic inline loff_t
281328653Shselaskynoop_llseek(struct linux_file *file, loff_t offset, int whence)
282328653Shselasky{
283328653Shselasky
284328653Shselasky	return (file->_file->f_offset);
285328653Shselasky}
286328653Shselasky
287329967Shselaskystatic inline struct vnode *
288329967Shselaskyfile_inode(const struct linux_file *file)
289329967Shselasky{
290329967Shselasky
291329967Shselasky	return (file->f_vnode);
292329967Shselasky}
293329967Shselasky
294329967Shselaskystatic inline int
295329967Shselaskycall_mmap(struct linux_file *file, struct vm_area_struct *vma)
296329967Shselasky{
297329967Shselasky
298329967Shselasky	return (file->f_op->mmap(file, vma));
299329967Shselasky}
300329967Shselasky
301328653Shselasky/* Shared memory support */
302328653Shselaskyunsigned long linux_invalidate_mapping_pages(vm_object_t, pgoff_t, pgoff_t);
303328653Shselaskystruct page *linux_shmem_read_mapping_page_gfp(vm_object_t, int, gfp_t);
304328653Shselaskystruct linux_file *linux_shmem_file_setup(const char *, loff_t, unsigned long);
305328653Shselaskyvoid linux_shmem_truncate_range(vm_object_t, loff_t, loff_t);
306328653Shselasky
307328653Shselasky#define	invalidate_mapping_pages(...) \
308328653Shselasky  linux_invalidate_mapping_pages(__VA_ARGS__)
309328653Shselasky
310328653Shselasky#define	shmem_read_mapping_page(...) \
311328653Shselasky  linux_shmem_read_mapping_page_gfp(__VA_ARGS__, 0)
312328653Shselasky
313328653Shselasky#define	shmem_read_mapping_page_gfp(...) \
314328653Shselasky  linux_shmem_read_mapping_page_gfp(__VA_ARGS__)
315328653Shselasky
316328653Shselasky#define	shmem_file_setup(...) \
317328653Shselasky  linux_shmem_file_setup(__VA_ARGS__)
318328653Shselasky
319328653Shselasky#define	shmem_truncate_range(...) \
320328653Shselasky  linux_shmem_truncate_range(__VA_ARGS__)
321328653Shselasky
322270710Shselasky#endif /* _LINUX_FS_H_ */
323