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-2018 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$ 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 #include <linux/spinlock.h> 45 #include <linux/dcache.h> 46 47 struct module; 48 struct kiocb; 49 struct iovec; 50 struct dentry; 51 struct page; 52 struct file_lock; 53 struct pipe_inode_info; 54 struct vm_area_struct; 55 struct poll_table_struct; 56 struct files_struct; 57 struct pfs_node; 58 struct linux_cdev; 59 60 #define inode vnode 61 #define i_cdev v_rdev 62 #define i_private v_data 63 64 #define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH) 65 #define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH) 66 67 typedef struct files_struct *fl_owner_t; 68 69 struct file_operations; 70 71 struct linux_file_wait_queue { 72 struct wait_queue wq; 73 struct wait_queue_head *wqh; 74 atomic_t state; 75 #define LINUX_FWQ_STATE_INIT 0 76 #define LINUX_FWQ_STATE_NOT_READY 1 77 #define LINUX_FWQ_STATE_QUEUED 2 78 #define LINUX_FWQ_STATE_READY 3 79 #define LINUX_FWQ_STATE_MAX 4 80 }; 81 82 struct linux_file { 83 struct file *_file; 84 const struct file_operations *f_op; 85 void *private_data; 86 int f_flags; 87 int f_mode; /* Just starting mode. */ 88 struct dentry *f_dentry; 89 struct dentry f_dentry_store; 90 struct selinfo f_selinfo; 91 struct sigio *f_sigio; 92 struct vnode *f_vnode; 93 #define f_inode f_vnode 94 volatile u_int f_count; 95 96 /* anonymous shmem object */ 97 vm_object_t f_shmem; 98 99 /* kqfilter support */ 100 int f_kqflags; 101 #define LINUX_KQ_FLAG_HAS_READ (1 << 0) 102 #define LINUX_KQ_FLAG_HAS_WRITE (1 << 1) 103 #define LINUX_KQ_FLAG_NEED_READ (1 << 2) 104 #define LINUX_KQ_FLAG_NEED_WRITE (1 << 3) 105 /* protects f_selinfo.si_note */ 106 spinlock_t f_kqlock; 107 struct linux_file_wait_queue f_wait_queue; 108 109 /* pointer to associated character device, if any */ 110 struct linux_cdev *f_cdev; 111 112 struct rcu_head rcu; 113 }; 114 115 #define file linux_file 116 #define fasync_struct sigio * 117 118 #define fasync_helper(fd, filp, on, queue) \ 119 ({ \ 120 if ((on)) \ 121 *(queue) = &(filp)->f_sigio; \ 122 else \ 123 *(queue) = NULL; \ 124 0; \ 125 }) 126 127 #define kill_fasync(queue, sig, pollstat) \ 128 do { \ 129 if (*(queue) != NULL) \ 130 pgsigio(*(queue), (sig), 0); \ 131 } while (0) 132 133 typedef int (*filldir_t)(void *, const char *, int, off_t, u64, unsigned); 134 135 struct file_operations { 136 struct module *owner; 137 ssize_t (*read)(struct linux_file *, char __user *, size_t, off_t *); 138 ssize_t (*write)(struct linux_file *, const char __user *, size_t, off_t *); 139 unsigned int (*poll) (struct linux_file *, struct poll_table_struct *); 140 long (*unlocked_ioctl)(struct linux_file *, unsigned int, unsigned long); 141 long (*compat_ioctl)(struct linux_file *, unsigned int, unsigned long); 142 int (*mmap)(struct linux_file *, struct vm_area_struct *); 143 int (*open)(struct inode *, struct file *); 144 int (*release)(struct inode *, struct linux_file *); 145 int (*fasync)(int, struct linux_file *, int); 146 147 /* Although not supported in FreeBSD, to align with Linux code 148 * we are adding llseek() only when it is mapped to no_llseek which returns 149 * an illegal seek error 150 */ 151 off_t (*llseek)(struct linux_file *, off_t, int); 152 #if 0 153 /* We do not support these methods. Don't permit them to compile. */ 154 loff_t (*llseek)(struct file *, loff_t, int); 155 ssize_t (*aio_read)(struct kiocb *, const struct iovec *, 156 unsigned long, loff_t); 157 ssize_t (*aio_write)(struct kiocb *, const struct iovec *, 158 unsigned long, loff_t); 159 int (*readdir)(struct file *, void *, filldir_t); 160 int (*ioctl)(struct inode *, struct file *, unsigned int, 161 unsigned long); 162 int (*flush)(struct file *, fl_owner_t id); 163 int (*fsync)(struct file *, struct dentry *, int datasync); 164 int (*aio_fsync)(struct kiocb *, int datasync); 165 int (*lock)(struct file *, int, struct file_lock *); 166 ssize_t (*sendpage)(struct file *, struct page *, int, size_t, 167 loff_t *, int); 168 unsigned long (*get_unmapped_area)(struct file *, unsigned long, 169 unsigned long, unsigned long, unsigned long); 170 int (*check_flags)(int); 171 int (*flock)(struct file *, int, struct file_lock *); 172 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, 173 loff_t *, size_t, unsigned int); 174 ssize_t (*splice_read)(struct file *, loff_t *, 175 struct pipe_inode_info *, size_t, unsigned int); 176 int (*setlease)(struct file *, long, struct file_lock **); 177 #endif 178 }; 179 #define fops_get(fops) (fops) 180 #define replace_fops(f, fops) ((f)->f_op = (fops)) 181 182 #define FMODE_READ FREAD 183 #define FMODE_WRITE FWRITE 184 #define FMODE_EXEC FEXEC 185 #define FMODE_UNSIGNED_OFFSET 0x2000 186 int __register_chrdev(unsigned int major, unsigned int baseminor, 187 unsigned int count, const char *name, 188 const struct file_operations *fops); 189 int __register_chrdev_p(unsigned int major, unsigned int baseminor, 190 unsigned int count, const char *name, 191 const struct file_operations *fops, uid_t uid, 192 gid_t gid, int mode); 193 void __unregister_chrdev(unsigned int major, unsigned int baseminor, 194 unsigned int count, const char *name); 195 196 static inline void 197 unregister_chrdev(unsigned int major, const char *name) 198 { 199 200 __unregister_chrdev(major, 0, 256, name); 201 } 202 203 static inline int 204 register_chrdev(unsigned int major, const char *name, 205 const struct file_operations *fops) 206 { 207 208 return (__register_chrdev(major, 0, 256, name, fops)); 209 } 210 211 static inline int 212 register_chrdev_p(unsigned int major, const char *name, 213 const struct file_operations *fops, uid_t uid, gid_t gid, int mode) 214 { 215 216 return (__register_chrdev_p(major, 0, 256, name, fops, uid, gid, mode)); 217 } 218 219 static inline int 220 register_chrdev_region(dev_t dev, unsigned range, const char *name) 221 { 222 223 return 0; 224 } 225 226 static inline void 227 unregister_chrdev_region(dev_t dev, unsigned range) 228 { 229 230 return; 231 } 232 233 static inline int 234 alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, 235 const char *name) 236 { 237 238 return 0; 239 } 240 241 /* No current support for seek op in FreeBSD */ 242 static inline int 243 nonseekable_open(struct inode *inode, struct file *filp) 244 { 245 return 0; 246 } 247 248 static inline int 249 simple_open(struct inode *inode, struct file *filp) 250 { 251 return 0; 252 } 253 254 extern unsigned int linux_iminor(struct inode *); 255 #define iminor(...) linux_iminor(__VA_ARGS__) 256 257 static inline struct linux_file * 258 get_file(struct linux_file *f) 259 { 260 261 refcount_acquire(f->_file == NULL ? &f->f_count : &f->_file->f_count); 262 return (f); 263 } 264 265 static inline bool 266 get_file_rcu(struct linux_file *f) 267 { 268 return (refcount_acquire_if_not_zero( 269 f->_file == NULL ? &f->f_count : &f->_file->f_count)); 270 } 271 272 static inline struct inode * 273 igrab(struct inode *inode) 274 { 275 int error; 276 277 error = vget(inode, 0); 278 if (error) 279 return (NULL); 280 281 return (inode); 282 } 283 284 static inline void 285 iput(struct inode *inode) 286 { 287 288 vrele(inode); 289 } 290 291 static inline loff_t 292 no_llseek(struct file *file, loff_t offset, int whence) 293 { 294 295 return (-ESPIPE); 296 } 297 298 static inline loff_t 299 noop_llseek(struct linux_file *file, loff_t offset, int whence) 300 { 301 302 return (file->_file->f_offset); 303 } 304 305 static inline struct vnode * 306 file_inode(const struct linux_file *file) 307 { 308 309 return (file->f_vnode); 310 } 311 312 static inline int 313 call_mmap(struct linux_file *file, struct vm_area_struct *vma) 314 { 315 316 return (file->f_op->mmap(file, vma)); 317 } 318 319 #endif /* _LINUX_FS_H_ */ 320