fs.h revision 311803
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-2016 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 * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/fs.h 311803 2017-01-09 17:25:23Z hselasky $
30 */
31#ifndef	_LINUX_FS_H_
32#define	_LINUX_FS_H_
33
34#include <sys/cdefs.h>
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/conf.h>
38#include <sys/vnode.h>
39#include <sys/file.h>
40#include <sys/filedesc.h>
41#include <linux/types.h>
42#include <linux/wait.h>
43#include <linux/semaphore.h>
44
45struct module;
46struct kiocb;
47struct iovec;
48struct dentry;
49struct page;
50struct file_lock;
51struct pipe_inode_info;
52struct vm_area_struct;
53struct poll_table_struct;
54struct files_struct;
55
56#define	inode	vnode
57#define	i_cdev	v_rdev
58
59#define	S_IRUGO	(S_IRUSR | S_IRGRP | S_IROTH)
60#define	S_IWUGO	(S_IWUSR | S_IWGRP | S_IWOTH)
61
62
63typedef struct files_struct *fl_owner_t;
64
65struct dentry {
66	struct inode	*d_inode;
67};
68
69struct file_operations;
70
71struct linux_file {
72	struct file	*_file;
73	const struct file_operations	*f_op;
74	void 		*private_data;
75	int		f_flags;
76	int		f_mode;	/* Just starting mode. */
77	struct dentry	*f_dentry;
78	struct dentry	f_dentry_store;
79	struct selinfo	f_selinfo;
80	struct sigio	*f_sigio;
81	struct vnode	*f_vnode;
82};
83
84#define	file		linux_file
85#define	fasync_struct	sigio *
86
87#define	fasync_helper(fd, filp, on, queue)				\
88({									\
89	if ((on))							\
90		*(queue) = &(filp)->f_sigio;				\
91	else								\
92		*(queue) = NULL;					\
93	0;								\
94})
95
96#define	kill_fasync(queue, sig, pollstat)				\
97do {									\
98	if (*(queue) != NULL)						\
99		pgsigio(*(queue), (sig), 0);				\
100} while (0)
101
102typedef int (*filldir_t)(void *, const char *, int, loff_t, u64, unsigned);
103
104struct file_operations {
105	struct module *owner;
106	ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
107	ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
108	unsigned int (*poll) (struct file *, struct poll_table_struct *);
109	long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
110	int (*mmap)(struct file *, struct vm_area_struct *);
111	int (*open)(struct inode *, struct file *);
112	int (*release)(struct inode *, struct file *);
113	int (*fasync)(int, struct file *, int);
114
115/* Although not supported in FreeBSD, to align with Linux code
116 * we are adding llseek() only when it is mapped to no_llseek which returns
117 * an illegal seek error
118 */
119	loff_t (*llseek)(struct file *, loff_t, int);
120#if 0
121	/* We do not support these methods.  Don't permit them to compile. */
122	loff_t (*llseek)(struct file *, loff_t, int);
123	ssize_t (*aio_read)(struct kiocb *, const struct iovec *,
124	    unsigned long, loff_t);
125	ssize_t (*aio_write)(struct kiocb *, const struct iovec *,
126	    unsigned long, loff_t);
127	int (*readdir)(struct file *, void *, filldir_t);
128	int (*ioctl)(struct inode *, struct file *, unsigned int,
129	    unsigned long);
130	long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
131	int (*flush)(struct file *, fl_owner_t id);
132	int (*fsync)(struct file *, struct dentry *, int datasync);
133	int (*aio_fsync)(struct kiocb *, int datasync);
134	int (*lock)(struct file *, int, struct file_lock *);
135	ssize_t (*sendpage)(struct file *, struct page *, int, size_t,
136	    loff_t *, int);
137	unsigned long (*get_unmapped_area)(struct file *, unsigned long,
138	    unsigned long, unsigned long, unsigned long);
139	int (*check_flags)(int);
140	int (*flock)(struct file *, int, struct file_lock *);
141	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
142	    loff_t *, size_t, unsigned int);
143	ssize_t (*splice_read)(struct file *, loff_t *,
144	    struct pipe_inode_info *, size_t, unsigned int);
145	int (*setlease)(struct file *, long, struct file_lock **);
146#endif
147};
148#define	fops_get(fops)	(fops)
149
150#define	FMODE_READ	FREAD
151#define	FMODE_WRITE	FWRITE
152#define	FMODE_EXEC	FEXEC
153
154int __register_chrdev(unsigned int major, unsigned int baseminor,
155    unsigned int count, const char *name,
156    const struct file_operations *fops);
157int __register_chrdev_p(unsigned int major, unsigned int baseminor,
158    unsigned int count, const char *name,
159    const struct file_operations *fops, uid_t uid,
160    gid_t gid, int mode);
161void __unregister_chrdev(unsigned int major, unsigned int baseminor,
162    unsigned int count, const char *name);
163
164static inline void
165unregister_chrdev(unsigned int major, const char *name)
166{
167
168	__unregister_chrdev(major, 0, 256, name);
169}
170
171static inline int
172register_chrdev(unsigned int major, const char *name,
173    const struct file_operations *fops)
174{
175
176	return (__register_chrdev(major, 0, 256, name, fops));
177}
178
179static inline int
180register_chrdev_p(unsigned int major, const char *name,
181    const struct file_operations *fops, uid_t uid, gid_t gid, int mode)
182{
183
184	return (__register_chrdev_p(major, 0, 256, name, fops, uid, gid, mode));
185}
186
187static inline int
188register_chrdev_region(dev_t dev, unsigned range, const char *name)
189{
190
191	return 0;
192}
193
194static inline void
195unregister_chrdev_region(dev_t dev, unsigned range)
196{
197
198	return;
199}
200
201static inline int
202alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
203			const char *name)
204{
205
206	return 0;
207}
208
209/* No current support for seek op in FreeBSD */
210static inline int
211nonseekable_open(struct inode *inode, struct file *filp)
212{
213	return 0;
214}
215
216static inline dev_t
217iminor(struct inode *inode)
218{
219
220	return (minor(dev2unit(inode->v_rdev)));
221}
222
223static inline struct inode *
224igrab(struct inode *inode)
225{
226	int error;
227
228	error = vget(inode, 0, curthread);
229	if (error)
230		return (NULL);
231
232	return (inode);
233}
234
235static inline void
236iput(struct inode *inode)
237{
238
239	vrele(inode);
240}
241
242static inline loff_t
243no_llseek(struct file *file, loff_t offset, int whence)
244{
245        return -ESPIPE;
246}
247
248#endif /* _LINUX_FS_H_ */
249