2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2018 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>
45 #include <linux/dcache.h>
53 struct pipe_inode_info;
54 struct vm_area_struct;
55 struct poll_table_struct;
62 #define i_private v_data
64 #define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
65 #define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
68 typedef struct files_struct *fl_owner_t;
70 struct file_operations;
72 struct linux_file_wait_queue {
74 struct wait_queue_head *wqh;
76 #define LINUX_FWQ_STATE_INIT 0
77 #define LINUX_FWQ_STATE_NOT_READY 1
78 #define LINUX_FWQ_STATE_QUEUED 2
79 #define LINUX_FWQ_STATE_READY 3
80 #define LINUX_FWQ_STATE_MAX 4
85 const struct file_operations *f_op;
88 int f_mode; /* Just starting mode. */
89 struct dentry *f_dentry;
90 struct dentry f_dentry_store;
91 struct selinfo f_selinfo;
92 struct sigio *f_sigio;
93 struct vnode *f_vnode;
94 #define f_inode f_vnode
95 volatile u_int f_count;
97 /* anonymous shmem object */
100 /* kqfilter support */
102 #define LINUX_KQ_FLAG_HAS_READ (1 << 0)
103 #define LINUX_KQ_FLAG_HAS_WRITE (1 << 1)
104 #define LINUX_KQ_FLAG_NEED_READ (1 << 2)
105 #define LINUX_KQ_FLAG_NEED_WRITE (1 << 3)
106 /* protects f_selinfo.si_note */
108 struct linux_file_wait_queue f_wait_queue;
110 /* pointer to associated character device, if any */
111 struct linux_cdev *f_cdev;
114 #define file linux_file
115 #define fasync_struct sigio *
117 #define fasync_helper(fd, filp, on, queue) \
120 *(queue) = &(filp)->f_sigio; \
126 #define kill_fasync(queue, sig, pollstat) \
128 if (*(queue) != NULL) \
129 pgsigio(*(queue), (sig), 0); \
132 typedef int (*filldir_t)(void *, const char *, int, off_t, u64, unsigned);
134 struct file_operations {
135 struct module *owner;
136 ssize_t (*read)(struct linux_file *, char __user *, size_t, off_t *);
137 ssize_t (*write)(struct linux_file *, const char __user *, size_t, off_t *);
138 unsigned int (*poll) (struct linux_file *, struct poll_table_struct *);
139 long (*unlocked_ioctl)(struct linux_file *, unsigned int, unsigned long);
140 long (*compat_ioctl)(struct linux_file *, unsigned int, unsigned long);
141 int (*mmap)(struct linux_file *, struct vm_area_struct *);
142 int (*open)(struct inode *, struct file *);
143 int (*release)(struct inode *, struct linux_file *);
144 int (*fasync)(int, struct linux_file *, int);
146 /* Although not supported in FreeBSD, to align with Linux code
147 * we are adding llseek() only when it is mapped to no_llseek which returns
148 * an illegal seek error
150 off_t (*llseek)(struct linux_file *, off_t, int);
152 /* We do not support these methods. Don't permit them to compile. */
153 loff_t (*llseek)(struct file *, loff_t, int);
154 ssize_t (*aio_read)(struct kiocb *, const struct iovec *,
155 unsigned long, loff_t);
156 ssize_t (*aio_write)(struct kiocb *, const struct iovec *,
157 unsigned long, loff_t);
158 int (*readdir)(struct file *, void *, filldir_t);
159 int (*ioctl)(struct inode *, struct file *, unsigned int,
161 int (*flush)(struct file *, fl_owner_t id);
162 int (*fsync)(struct file *, struct dentry *, int datasync);
163 int (*aio_fsync)(struct kiocb *, int datasync);
164 int (*lock)(struct file *, int, struct file_lock *);
165 ssize_t (*sendpage)(struct file *, struct page *, int, size_t,
167 unsigned long (*get_unmapped_area)(struct file *, unsigned long,
168 unsigned long, unsigned long, unsigned long);
169 int (*check_flags)(int);
170 int (*flock)(struct file *, int, struct file_lock *);
171 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
172 loff_t *, size_t, unsigned int);
173 ssize_t (*splice_read)(struct file *, loff_t *,
174 struct pipe_inode_info *, size_t, unsigned int);
175 int (*setlease)(struct file *, long, struct file_lock **);
178 #define fops_get(fops) (fops)
179 #define replace_fops(f, fops) ((f)->f_op = (fops))
181 #define FMODE_READ FREAD
182 #define FMODE_WRITE FWRITE
183 #define FMODE_EXEC FEXEC
185 int __register_chrdev(unsigned int major, unsigned int baseminor,
186 unsigned int count, const char *name,
187 const struct file_operations *fops);
188 int __register_chrdev_p(unsigned int major, unsigned int baseminor,
189 unsigned int count, const char *name,
190 const struct file_operations *fops, uid_t uid,
191 gid_t gid, int mode);
192 void __unregister_chrdev(unsigned int major, unsigned int baseminor,
193 unsigned int count, const char *name);
196 unregister_chrdev(unsigned int major, const char *name)
199 __unregister_chrdev(major, 0, 256, name);
203 register_chrdev(unsigned int major, const char *name,
204 const struct file_operations *fops)
207 return (__register_chrdev(major, 0, 256, name, fops));
211 register_chrdev_p(unsigned int major, const char *name,
212 const struct file_operations *fops, uid_t uid, gid_t gid, int mode)
215 return (__register_chrdev_p(major, 0, 256, name, fops, uid, gid, mode));
219 register_chrdev_region(dev_t dev, unsigned range, const char *name)
226 unregister_chrdev_region(dev_t dev, unsigned range)
233 alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
240 /* No current support for seek op in FreeBSD */
242 nonseekable_open(struct inode *inode, struct file *filp)
247 extern unsigned int linux_iminor(struct inode *);
248 #define iminor(...) linux_iminor(__VA_ARGS__)
250 static inline struct linux_file *
251 get_file(struct linux_file *f)
254 refcount_acquire(f->_file == NULL ? &f->f_count : &f->_file->f_count);
258 static inline struct inode *
259 igrab(struct inode *inode)
263 error = vget(inode, 0, curthread);
271 iput(struct inode *inode)
278 no_llseek(struct file *file, loff_t offset, int whence)
285 noop_llseek(struct linux_file *file, loff_t offset, int whence)
288 return (file->_file->f_offset);
291 static inline struct vnode *
292 file_inode(const struct linux_file *file)
295 return (file->f_vnode);
299 call_mmap(struct linux_file *file, struct vm_area_struct *vma)
302 return (file->f_op->mmap(file, vma));
305 /* Shared memory support */
306 unsigned long linux_invalidate_mapping_pages(vm_object_t, pgoff_t, pgoff_t);
307 struct page *linux_shmem_read_mapping_page_gfp(vm_object_t, int, gfp_t);
308 struct linux_file *linux_shmem_file_setup(const char *, loff_t, unsigned long);
309 void linux_shmem_truncate_range(vm_object_t, loff_t, loff_t);
311 #define invalidate_mapping_pages(...) \
312 linux_invalidate_mapping_pages(__VA_ARGS__)
314 #define shmem_read_mapping_page(...) \
315 linux_shmem_read_mapping_page_gfp(__VA_ARGS__, 0)
317 #define shmem_read_mapping_page_gfp(...) \
318 linux_shmem_read_mapping_page_gfp(__VA_ARGS__)
320 #define shmem_file_setup(...) \
321 linux_shmem_file_setup(__VA_ARGS__)
323 #define shmem_truncate_range(...) \
324 linux_shmem_truncate_range(__VA_ARGS__)
326 #endif /* _LINUX_FS_H_ */