18d59ecb2SHans Petter Selasky /*-
28d59ecb2SHans Petter Selasky  * Copyright (c) 2010 Isilon Systems, Inc.
38d59ecb2SHans Petter Selasky  * Copyright (c) 2010 iX Systems, Inc.
48d59ecb2SHans Petter Selasky  * Copyright (c) 2010 Panasas, Inc.
51125dbc0SHans Petter Selasky  * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
68d59ecb2SHans Petter Selasky  * All rights reserved.
78d59ecb2SHans Petter Selasky  *
88d59ecb2SHans Petter Selasky  * Redistribution and use in source and binary forms, with or without
98d59ecb2SHans Petter Selasky  * modification, are permitted provided that the following conditions
108d59ecb2SHans Petter Selasky  * are met:
118d59ecb2SHans Petter Selasky  * 1. Redistributions of source code must retain the above copyright
128d59ecb2SHans Petter Selasky  *    notice unmodified, this list of conditions, and the following
138d59ecb2SHans Petter Selasky  *    disclaimer.
148d59ecb2SHans Petter Selasky  * 2. Redistributions in binary form must reproduce the above copyright
158d59ecb2SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer in the
168d59ecb2SHans Petter Selasky  *    documentation and/or other materials provided with the distribution.
178d59ecb2SHans Petter Selasky  *
188d59ecb2SHans Petter Selasky  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
198d59ecb2SHans Petter Selasky  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
208d59ecb2SHans Petter Selasky  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
218d59ecb2SHans Petter Selasky  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
228d59ecb2SHans Petter Selasky  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
238d59ecb2SHans Petter Selasky  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
248d59ecb2SHans Petter Selasky  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
258d59ecb2SHans Petter Selasky  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
268d59ecb2SHans Petter Selasky  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
278d59ecb2SHans Petter Selasky  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
288d59ecb2SHans Petter Selasky  *
298d59ecb2SHans Petter Selasky  * $FreeBSD$
308d59ecb2SHans Petter Selasky  */
318d59ecb2SHans Petter Selasky #ifndef	_LINUX_FS_H_
328d59ecb2SHans Petter Selasky #define	_LINUX_FS_H_
338d59ecb2SHans Petter Selasky 
348d59ecb2SHans Petter Selasky #include <sys/cdefs.h>
358d59ecb2SHans Petter Selasky #include <sys/param.h>
368d59ecb2SHans Petter Selasky #include <sys/systm.h>
378d59ecb2SHans Petter Selasky #include <sys/conf.h>
388d59ecb2SHans Petter Selasky #include <sys/vnode.h>
398d59ecb2SHans Petter Selasky #include <sys/file.h>
408d59ecb2SHans Petter Selasky #include <sys/filedesc.h>
418d59ecb2SHans Petter Selasky #include <linux/types.h>
428d59ecb2SHans Petter Selasky #include <linux/wait.h>
438d59ecb2SHans Petter Selasky #include <linux/semaphore.h>
44a6b28ee0SHans Petter Selasky #include <linux/spinlock.h>
458d59ecb2SHans Petter Selasky 
468d59ecb2SHans Petter Selasky struct module;
478d59ecb2SHans Petter Selasky struct kiocb;
488d59ecb2SHans Petter Selasky struct iovec;
498d59ecb2SHans Petter Selasky struct dentry;
508d59ecb2SHans Petter Selasky struct page;
518d59ecb2SHans Petter Selasky struct file_lock;
528d59ecb2SHans Petter Selasky struct pipe_inode_info;
538d59ecb2SHans Petter Selasky struct vm_area_struct;
548d59ecb2SHans Petter Selasky struct poll_table_struct;
558d59ecb2SHans Petter Selasky struct files_struct;
567e1a02baSMark Johnston struct pfs_node;
578d59ecb2SHans Petter Selasky 
588d59ecb2SHans Petter Selasky #define	inode	vnode
598d59ecb2SHans Petter Selasky #define	i_cdev	v_rdev
607e1a02baSMark Johnston #define	i_private v_data
618d59ecb2SHans Petter Selasky 
628d59ecb2SHans Petter Selasky #define	S_IRUGO	(S_IRUSR | S_IRGRP | S_IROTH)
638d59ecb2SHans Petter Selasky #define	S_IWUGO	(S_IWUSR | S_IWGRP | S_IWOTH)
648d59ecb2SHans Petter Selasky 
658d59ecb2SHans Petter Selasky 
668d59ecb2SHans Petter Selasky typedef struct files_struct *fl_owner_t;
678d59ecb2SHans Petter Selasky 
688d59ecb2SHans Petter Selasky struct dentry {
698d59ecb2SHans Petter Selasky 	struct inode	*d_inode;
707e1a02baSMark Johnston 	struct pfs_node	*d_pfs_node;
718d59ecb2SHans Petter Selasky };
728d59ecb2SHans Petter Selasky 
738d59ecb2SHans Petter Selasky struct file_operations;
748d59ecb2SHans Petter Selasky 
756dec7efaSHans Petter Selasky struct linux_file_wait_queue {
766dec7efaSHans Petter Selasky 	struct wait_queue wq;
776dec7efaSHans Petter Selasky 	struct wait_queue_head *wqh;
786dec7efaSHans Petter Selasky 	atomic_t state;
796dec7efaSHans Petter Selasky #define	LINUX_FWQ_STATE_INIT 0
806dec7efaSHans Petter Selasky #define	LINUX_FWQ_STATE_NOT_READY 1
816dec7efaSHans Petter Selasky #define	LINUX_FWQ_STATE_QUEUED 2
826dec7efaSHans Petter Selasky #define	LINUX_FWQ_STATE_READY 3
836dec7efaSHans Petter Selasky #define	LINUX_FWQ_STATE_MAX 4
846dec7efaSHans Petter Selasky };
856dec7efaSHans Petter Selasky 
868d59ecb2SHans Petter Selasky struct linux_file {
878d59ecb2SHans Petter Selasky 	struct file	*_file;
888d59ecb2SHans Petter Selasky 	const struct file_operations	*f_op;
898d59ecb2SHans Petter Selasky 	void 		*private_data;
908d59ecb2SHans Petter Selasky 	int		f_flags;
918d59ecb2SHans Petter Selasky 	int		f_mode;	/* Just starting mode. */
928d59ecb2SHans Petter Selasky 	struct dentry	*f_dentry;
938d59ecb2SHans Petter Selasky 	struct dentry	f_dentry_store;
948d59ecb2SHans Petter Selasky 	struct selinfo	f_selinfo;
958d59ecb2SHans Petter Selasky 	struct sigio	*f_sigio;
968d59ecb2SHans Petter Selasky 	struct vnode	*f_vnode;
97dac6b88aSMark Johnston #define	f_inode	f_vnode
98f5a9867bSHans Petter Selasky 	volatile u_int	f_count;
99a6b28ee0SHans Petter Selasky 
100dac6b88aSMark Johnston 	/* anonymous shmem object */
101dac6b88aSMark Johnston 	vm_object_t	f_shmem;
102dac6b88aSMark Johnston 
103a6b28ee0SHans Petter Selasky 	/* kqfilter support */
104a6b28ee0SHans Petter Selasky 	int		f_kqflags;
105a6b28ee0SHans Petter Selasky #define	LINUX_KQ_FLAG_HAS_READ (1 << 0)
106a6b28ee0SHans Petter Selasky #define	LINUX_KQ_FLAG_HAS_WRITE (1 << 1)
107a6b28ee0SHans Petter Selasky #define	LINUX_KQ_FLAG_NEED_READ (1 << 2)
108a6b28ee0SHans Petter Selasky #define	LINUX_KQ_FLAG_NEED_WRITE (1 << 3)
109a6b28ee0SHans Petter Selasky 	/* protects f_selinfo.si_note */
110a6b28ee0SHans Petter Selasky 	spinlock_t	f_kqlock;
1116dec7efaSHans Petter Selasky 	struct linux_file_wait_queue f_wait_queue;
1128d59ecb2SHans Petter Selasky };
1138d59ecb2SHans Petter Selasky 
1148d59ecb2SHans Petter Selasky #define	file		linux_file
1158d59ecb2SHans Petter Selasky #define	fasync_struct	sigio *
1168d59ecb2SHans Petter Selasky 
1178d59ecb2SHans Petter Selasky #define	fasync_helper(fd, filp, on, queue)				\
1188d59ecb2SHans Petter Selasky ({									\
1198d59ecb2SHans Petter Selasky 	if ((on))							\
1208d59ecb2SHans Petter Selasky 		*(queue) = &(filp)->f_sigio;				\
1218d59ecb2SHans Petter Selasky 	else								\
1228d59ecb2SHans Petter Selasky 		*(queue) = NULL;					\
1238d59ecb2SHans Petter Selasky 	0;								\
1248d59ecb2SHans Petter Selasky })
1258d59ecb2SHans Petter Selasky 
1268d59ecb2SHans Petter Selasky #define	kill_fasync(queue, sig, pollstat)				\
1278d59ecb2SHans Petter Selasky do {									\
1288d59ecb2SHans Petter Selasky 	if (*(queue) != NULL)						\
1298d59ecb2SHans Petter Selasky 		pgsigio(*(queue), (sig), 0);				\
1308d59ecb2SHans Petter Selasky } while (0)
1318d59ecb2SHans Petter Selasky 
1328d59ecb2SHans Petter Selasky typedef int (*filldir_t)(void *, const char *, int, loff_t, u64, unsigned);
1338d59ecb2SHans Petter Selasky 
1348d59ecb2SHans Petter Selasky struct file_operations {
1358d59ecb2SHans Petter Selasky 	struct module *owner;
1368d59ecb2SHans Petter Selasky 	ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
1378d59ecb2SHans Petter Selasky 	ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
1388d59ecb2SHans Petter Selasky 	unsigned int (*poll) (struct file *, struct poll_table_struct *);
1398d59ecb2SHans Petter Selasky 	long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
1408d59ecb2SHans Petter Selasky 	int (*mmap)(struct file *, struct vm_area_struct *);
1418d59ecb2SHans Petter Selasky 	int (*open)(struct inode *, struct file *);
1428d59ecb2SHans Petter Selasky 	int (*release)(struct inode *, struct file *);
1438d59ecb2SHans Petter Selasky 	int (*fasync)(int, struct file *, int);
1448d59ecb2SHans Petter Selasky 
1458d59ecb2SHans Petter Selasky /* Although not supported in FreeBSD, to align with Linux code
1468d59ecb2SHans Petter Selasky  * we are adding llseek() only when it is mapped to no_llseek which returns
1478d59ecb2SHans Petter Selasky  * an illegal seek error
1488d59ecb2SHans Petter Selasky  */
1498d59ecb2SHans Petter Selasky 	loff_t (*llseek)(struct file *, loff_t, int);
1508d59ecb2SHans Petter Selasky #if 0
1518d59ecb2SHans Petter Selasky 	/* We do not support these methods.  Don't permit them to compile. */
1528d59ecb2SHans Petter Selasky 	loff_t (*llseek)(struct file *, loff_t, int);
1538d59ecb2SHans Petter Selasky 	ssize_t (*aio_read)(struct kiocb *, const struct iovec *,
1548d59ecb2SHans Petter Selasky 	    unsigned long, loff_t);
1558d59ecb2SHans Petter Selasky 	ssize_t (*aio_write)(struct kiocb *, const struct iovec *,
1568d59ecb2SHans Petter Selasky 	    unsigned long, loff_t);
1578d59ecb2SHans Petter Selasky 	int (*readdir)(struct file *, void *, filldir_t);
1588d59ecb2SHans Petter Selasky 	int (*ioctl)(struct inode *, struct file *, unsigned int,
1598d59ecb2SHans Petter Selasky 	    unsigned long);
1608d59ecb2SHans Petter Selasky 	long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
1618d59ecb2SHans Petter Selasky 	int (*flush)(struct file *, fl_owner_t id);
1628d59ecb2SHans Petter Selasky 	int (*fsync)(struct file *, struct dentry *, int datasync);
1638d59ecb2SHans Petter Selasky 	int (*aio_fsync)(struct kiocb *, int datasync);
1648d59ecb2SHans Petter Selasky 	int (*lock)(struct file *, int, struct file_lock *);
1658d59ecb2SHans Petter Selasky 	ssize_t (*sendpage)(struct file *, struct page *, int, size_t,
1668d59ecb2SHans Petter Selasky 	    loff_t *, int);
1678d59ecb2SHans Petter Selasky 	unsigned long (*get_unmapped_area)(struct file *, unsigned long,
1688d59ecb2SHans Petter Selasky 	    unsigned long, unsigned long, unsigned long);
1698d59ecb2SHans Petter Selasky 	int (*check_flags)(int);
1708d59ecb2SHans Petter Selasky 	int (*flock)(struct file *, int, struct file_lock *);
1718d59ecb2SHans Petter Selasky 	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
1728d59ecb2SHans Petter Selasky 	    loff_t *, size_t, unsigned int);
1738d59ecb2SHans Petter Selasky 	ssize_t (*splice_read)(struct file *, loff_t *,
1748d59ecb2SHans Petter Selasky 	    struct pipe_inode_info *, size_t, unsigned int);
1758d59ecb2SHans Petter Selasky 	int (*setlease)(struct file *, long, struct file_lock **);
1768d59ecb2SHans Petter Selasky #endif
1778d59ecb2SHans Petter Selasky };
1788d59ecb2SHans Petter Selasky #define	fops_get(fops)		(fops)
179dac6b88aSMark Johnston #define	replace_fops(f, fops)	((f)->f_op = (fops))
1808d59ecb2SHans Petter Selasky 
1818d59ecb2SHans Petter Selasky #define	FMODE_READ	FREAD
1828d59ecb2SHans Petter Selasky #define	FMODE_WRITE	FWRITE
1838d59ecb2SHans Petter Selasky #define	FMODE_EXEC	FEXEC
1848d59ecb2SHans Petter Selasky 
1851125dbc0SHans Petter Selasky int __register_chrdev(unsigned int major, unsigned int baseminor,
1861125dbc0SHans Petter Selasky     unsigned int count, const char *name,
1871125dbc0SHans Petter Selasky     const struct file_operations *fops);
1881125dbc0SHans Petter Selasky int __register_chrdev_p(unsigned int major, unsigned int baseminor,
1891125dbc0SHans Petter Selasky     unsigned int count, const char *name,
1901125dbc0SHans Petter Selasky     const struct file_operations *fops, uid_t uid,
1911125dbc0SHans Petter Selasky     gid_t gid, int mode);
1921125dbc0SHans Petter Selasky void __unregister_chrdev(unsigned int major, unsigned int baseminor,
1931125dbc0SHans Petter Selasky     unsigned int count, const char *name);
1941125dbc0SHans Petter Selasky 
1951125dbc0SHans Petter Selasky static inline void
1961125dbc0SHans Petter Selasky unregister_chrdev(unsigned int major, const char *name)
1971125dbc0SHans Petter Selasky {
1981125dbc0SHans Petter Selasky 
1991125dbc0SHans Petter Selasky 	__unregister_chrdev(major, 0, 256, name);
2001125dbc0SHans Petter Selasky }
2011125dbc0SHans Petter Selasky 
2021125dbc0SHans Petter Selasky static inline int
2031125dbc0SHans Petter Selasky register_chrdev(unsigned int major, const char *name,
2041125dbc0SHans Petter Selasky     const struct file_operations *fops)
2051125dbc0SHans Petter Selasky {
2061125dbc0SHans Petter Selasky 
2071125dbc0SHans Petter Selasky 	return (__register_chrdev(major, 0, 256, name, fops));
2081125dbc0SHans Petter Selasky }
2091125dbc0SHans Petter Selasky 
2101125dbc0SHans Petter Selasky static inline int
2111125dbc0SHans Petter Selasky register_chrdev_p(unsigned int major, const char *name,
2121125dbc0SHans Petter Selasky     const struct file_operations *fops, uid_t uid, gid_t gid, int mode)
2131125dbc0SHans Petter Selasky {
2141125dbc0SHans Petter Selasky 
2151125dbc0SHans Petter Selasky 	return (__register_chrdev_p(major, 0, 256, name, fops, uid, gid, mode));
2161125dbc0SHans Petter Selasky }
2171125dbc0SHans Petter Selasky 
2188d59ecb2SHans Petter Selasky static inline int
2198d59ecb2SHans Petter Selasky register_chrdev_region(dev_t dev, unsigned range, const char *name)
2208d59ecb2SHans Petter Selasky {
2218d59ecb2SHans Petter Selasky 
2228d59ecb2SHans Petter Selasky 	return 0;
2238d59ecb2SHans Petter Selasky }
2248d59ecb2SHans Petter Selasky 
2258d59ecb2SHans Petter Selasky static inline void
2268d59ecb2SHans Petter Selasky unregister_chrdev_region(dev_t dev, unsigned range)
2278d59ecb2SHans Petter Selasky {
2288d59ecb2SHans Petter Selasky 
2298d59ecb2SHans Petter Selasky 	return;
2308d59ecb2SHans Petter Selasky }
2318d59ecb2SHans Petter Selasky 
2328d59ecb2SHans Petter Selasky static inline int
2338d59ecb2SHans Petter Selasky alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
2348d59ecb2SHans Petter Selasky 			const char *name)
2358d59ecb2SHans Petter Selasky {
2368d59ecb2SHans Petter Selasky 
2378d59ecb2SHans Petter Selasky 	return 0;
2388d59ecb2SHans Petter Selasky }
2398d59ecb2SHans Petter Selasky 
2408d59ecb2SHans Petter Selasky /* No current support for seek op in FreeBSD */
2418d59ecb2SHans Petter Selasky static inline int
2428d59ecb2SHans Petter Selasky nonseekable_open(struct inode *inode, struct file *filp)
2438d59ecb2SHans Petter Selasky {
2448d59ecb2SHans Petter Selasky 	return 0;
2458d59ecb2SHans Petter Selasky }
2468d59ecb2SHans Petter Selasky 
2472b79a966SHans Petter Selasky extern unsigned int linux_iminor(struct inode *);
2482b79a966SHans Petter Selasky #define	iminor(...) linux_iminor(__VA_ARGS__)
2498d59ecb2SHans Petter Selasky 
250f5a9867bSHans Petter Selasky static inline struct linux_file *
251f5a9867bSHans Petter Selasky get_file(struct linux_file *f)
252f5a9867bSHans Petter Selasky {
253f5a9867bSHans Petter Selasky 
254f5a9867bSHans Petter Selasky 	refcount_acquire(f->_file == NULL ? &f->f_count : &f->_file->f_count);
255f5a9867bSHans Petter Selasky 	return (f);
256f5a9867bSHans Petter Selasky }
257f5a9867bSHans Petter Selasky 
2588d59ecb2SHans Petter Selasky static inline struct inode *
2598d59ecb2SHans Petter Selasky igrab(struct inode *inode)
2608d59ecb2SHans Petter Selasky {
2618d59ecb2SHans Petter Selasky 	int error;
2628d59ecb2SHans Petter Selasky 
2638d59ecb2SHans Petter Selasky 	error = vget(inode, 0, curthread);
2648d59ecb2SHans Petter Selasky 	if (error)
2658d59ecb2SHans Petter Selasky 		return (NULL);
2668d59ecb2SHans Petter Selasky 
2678d59ecb2SHans Petter Selasky 	return (inode);
2688d59ecb2SHans Petter Selasky }
2698d59ecb2SHans Petter Selasky 
2708d59ecb2SHans Petter Selasky static inline void
2718d59ecb2SHans Petter Selasky iput(struct inode *inode)
2728d59ecb2SHans Petter Selasky {
2738d59ecb2SHans Petter Selasky 
2748d59ecb2SHans Petter Selasky 	vrele(inode);
2758d59ecb2SHans Petter Selasky }
2768d59ecb2SHans Petter Selasky 
2778d59ecb2SHans Petter Selasky static inline loff_t
2788d59ecb2SHans Petter Selasky no_llseek(struct file *file, loff_t offset, int whence)
2798d59ecb2SHans Petter Selasky {
2804eb1bcfcSMark Johnston 
2814eb1bcfcSMark Johnston 	return (-ESPIPE);
2824eb1bcfcSMark Johnston }
2834eb1bcfcSMark Johnston 
2844eb1bcfcSMark Johnston static inline loff_t
2854eb1bcfcSMark Johnston noop_llseek(struct linux_file *file, loff_t offset, int whence)
2864eb1bcfcSMark Johnston {
2874eb1bcfcSMark Johnston 
2884eb1bcfcSMark Johnston 	return (file->_file->f_offset);
2898d59ecb2SHans Petter Selasky }
2908d59ecb2SHans Petter Selasky 
2918d59ecb2SHans Petter Selasky #endif /* _LINUX_FS_H_ */
292