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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
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.
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.
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/vnode.h>
40 #include <sys/filedesc.h>
41 #include <linux/types.h>
42 #include <linux/wait.h>
43 #include <linux/semaphore.h>
44 #include <linux/spinlock.h>
52 struct pipe_inode_info;
53 struct vm_area_struct;
54 struct poll_table_struct;
60 #define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
61 #define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
64 typedef struct files_struct *fl_owner_t;
67 struct inode *d_inode;
70 struct file_operations;
74 const struct file_operations *f_op;
77 int f_mode; /* Just starting mode. */
78 struct dentry *f_dentry;
79 struct dentry f_dentry_store;
80 struct selinfo f_selinfo;
81 struct sigio *f_sigio;
82 struct vnode *f_vnode;
83 volatile u_int f_count;
85 /* kqfilter support */
87 #define LINUX_KQ_FLAG_HAS_READ (1 << 0)
88 #define LINUX_KQ_FLAG_HAS_WRITE (1 << 1)
89 #define LINUX_KQ_FLAG_NEED_READ (1 << 2)
90 #define LINUX_KQ_FLAG_NEED_WRITE (1 << 3)
91 /* protects f_selinfo.si_note */
95 #define file linux_file
96 #define fasync_struct sigio *
98 #define fasync_helper(fd, filp, on, queue) \
101 *(queue) = &(filp)->f_sigio; \
107 #define kill_fasync(queue, sig, pollstat) \
109 if (*(queue) != NULL) \
110 pgsigio(*(queue), (sig), 0); \
113 typedef int (*filldir_t)(void *, const char *, int, loff_t, u64, unsigned);
115 struct file_operations {
116 struct module *owner;
117 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
118 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
119 unsigned int (*poll) (struct file *, struct poll_table_struct *);
120 long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
121 int (*mmap)(struct file *, struct vm_area_struct *);
122 int (*open)(struct inode *, struct file *);
123 int (*release)(struct inode *, struct file *);
124 int (*fasync)(int, struct file *, int);
126 /* Although not supported in FreeBSD, to align with Linux code
127 * we are adding llseek() only when it is mapped to no_llseek which returns
128 * an illegal seek error
130 loff_t (*llseek)(struct file *, loff_t, int);
132 /* We do not support these methods. Don't permit them to compile. */
133 loff_t (*llseek)(struct file *, loff_t, int);
134 ssize_t (*aio_read)(struct kiocb *, const struct iovec *,
135 unsigned long, loff_t);
136 ssize_t (*aio_write)(struct kiocb *, const struct iovec *,
137 unsigned long, loff_t);
138 int (*readdir)(struct file *, void *, filldir_t);
139 int (*ioctl)(struct inode *, struct file *, unsigned int,
141 long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
142 int (*flush)(struct file *, fl_owner_t id);
143 int (*fsync)(struct file *, struct dentry *, int datasync);
144 int (*aio_fsync)(struct kiocb *, int datasync);
145 int (*lock)(struct file *, int, struct file_lock *);
146 ssize_t (*sendpage)(struct file *, struct page *, int, size_t,
148 unsigned long (*get_unmapped_area)(struct file *, unsigned long,
149 unsigned long, unsigned long, unsigned long);
150 int (*check_flags)(int);
151 int (*flock)(struct file *, int, struct file_lock *);
152 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
153 loff_t *, size_t, unsigned int);
154 ssize_t (*splice_read)(struct file *, loff_t *,
155 struct pipe_inode_info *, size_t, unsigned int);
156 int (*setlease)(struct file *, long, struct file_lock **);
159 #define fops_get(fops) (fops)
161 #define FMODE_READ FREAD
162 #define FMODE_WRITE FWRITE
163 #define FMODE_EXEC FEXEC
165 int __register_chrdev(unsigned int major, unsigned int baseminor,
166 unsigned int count, const char *name,
167 const struct file_operations *fops);
168 int __register_chrdev_p(unsigned int major, unsigned int baseminor,
169 unsigned int count, const char *name,
170 const struct file_operations *fops, uid_t uid,
171 gid_t gid, int mode);
172 void __unregister_chrdev(unsigned int major, unsigned int baseminor,
173 unsigned int count, const char *name);
176 unregister_chrdev(unsigned int major, const char *name)
179 __unregister_chrdev(major, 0, 256, name);
183 register_chrdev(unsigned int major, const char *name,
184 const struct file_operations *fops)
187 return (__register_chrdev(major, 0, 256, name, fops));
191 register_chrdev_p(unsigned int major, const char *name,
192 const struct file_operations *fops, uid_t uid, gid_t gid, int mode)
195 return (__register_chrdev_p(major, 0, 256, name, fops, uid, gid, mode));
199 register_chrdev_region(dev_t dev, unsigned range, const char *name)
206 unregister_chrdev_region(dev_t dev, unsigned range)
213 alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
220 /* No current support for seek op in FreeBSD */
222 nonseekable_open(struct inode *inode, struct file *filp)
228 iminor(struct inode *inode)
231 return (minor(dev2unit(inode->v_rdev)));
234 static inline struct linux_file *
235 get_file(struct linux_file *f)
238 refcount_acquire(f->_file == NULL ? &f->f_count : &f->_file->f_count);
242 static inline struct inode *
243 igrab(struct inode *inode)
247 error = vget(inode, 0, curthread);
255 iput(struct inode *inode)
262 no_llseek(struct file *file, loff_t offset, int whence)
269 noop_llseek(struct linux_file *file, loff_t offset, int whence)
272 return (file->_file->f_offset);
275 #endif /* _LINUX_FS_H_ */