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$ 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 45 struct module; 46 struct kiocb; 47 struct iovec; 48 struct dentry; 49 struct page; 50 struct file_lock; 51 struct pipe_inode_info; 52 struct vm_area_struct; 53 struct poll_table_struct; 54 struct 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 63 typedef struct files_struct *fl_owner_t; 64 65 struct dentry { 66 struct inode *d_inode; 67 }; 68 69 struct file_operations; 70 71 struct 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) \ 97 do { \ 98 if (*(queue) != NULL) \ 99 pgsigio(*(queue), (sig), 0); \ 100 } while (0) 101 102 typedef int (*filldir_t)(void *, const char *, int, loff_t, u64, unsigned); 103 104 struct 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 154 int __register_chrdev(unsigned int major, unsigned int baseminor, 155 unsigned int count, const char *name, 156 const struct file_operations *fops); 157 int __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); 161 void __unregister_chrdev(unsigned int major, unsigned int baseminor, 162 unsigned int count, const char *name); 163 164 static inline void 165 unregister_chrdev(unsigned int major, const char *name) 166 { 167 168 __unregister_chrdev(major, 0, 256, name); 169 } 170 171 static inline int 172 register_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 179 static inline int 180 register_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 187 static inline int 188 register_chrdev_region(dev_t dev, unsigned range, const char *name) 189 { 190 191 return 0; 192 } 193 194 static inline void 195 unregister_chrdev_region(dev_t dev, unsigned range) 196 { 197 198 return; 199 } 200 201 static inline int 202 alloc_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 */ 210 static inline int 211 nonseekable_open(struct inode *inode, struct file *filp) 212 { 213 return 0; 214 } 215 216 static inline dev_t 217 iminor(struct inode *inode) 218 { 219 220 return (minor(dev2unit(inode->v_rdev))); 221 } 222 223 static inline struct inode * 224 igrab(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 235 static inline void 236 iput(struct inode *inode) 237 { 238 239 vrele(inode); 240 } 241 242 static inline loff_t 243 no_llseek(struct file *file, loff_t offset, int whence) 244 { 245 return -ESPIPE; 246 } 247 248 #endif /* _LINUX_FS_H_ */ 249