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 #ifndef _LINUXKPI_LINUX_FS_H_ 30 #define _LINUXKPI_LINUX_FS_H_ 31 32 #include <sys/cdefs.h> 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/conf.h> 36 #include <sys/vnode.h> 37 #include <sys/file.h> 38 #include <sys/filedesc.h> 39 #include <linux/types.h> 40 #include <linux/wait.h> 41 #include <linux/semaphore.h> 42 #include <linux/spinlock.h> 43 #include <linux/dcache.h> 44 #include <linux/capability.h> 45 #include <linux/wait_bit.h> 46 #include <linux/kernel.h> 47 #include <linux/mutex.h> 48 49 struct module; 50 struct kiocb; 51 struct iovec; 52 struct dentry; 53 struct page; 54 struct file_lock; 55 struct pipe_inode_info; 56 struct vm_area_struct; 57 struct poll_table_struct; 58 struct files_struct; 59 struct pfs_node; 60 struct linux_cdev; 61 62 #define inode vnode 63 #define i_cdev v_rdev 64 #define i_private v_data 65 66 #define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH) 67 #define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH) 68 69 typedef struct files_struct *fl_owner_t; 70 71 struct file_operations; 72 73 struct linux_file_wait_queue { 74 struct wait_queue wq; 75 struct wait_queue_head *wqh; 76 atomic_t state; 77 #define LINUX_FWQ_STATE_INIT 0 78 #define LINUX_FWQ_STATE_NOT_READY 1 79 #define LINUX_FWQ_STATE_QUEUED 2 80 #define LINUX_FWQ_STATE_READY 3 81 #define LINUX_FWQ_STATE_MAX 4 82 }; 83 84 struct linux_file { 85 struct file *_file; 86 const struct file_operations *f_op; 87 void *private_data; 88 int f_flags; 89 int f_mode; /* Just starting mode. */ 90 struct dentry *f_dentry; 91 struct dentry f_dentry_store; 92 struct selinfo f_selinfo; 93 struct sigio *f_sigio; 94 struct vnode *f_vnode; 95 #define f_inode f_vnode 96 volatile u_int f_count; 97 98 /* anonymous shmem object */ 99 vm_object_t f_shmem; 100 101 /* kqfilter support */ 102 int f_kqflags; 103 #define LINUX_KQ_FLAG_HAS_READ (1 << 0) 104 #define LINUX_KQ_FLAG_HAS_WRITE (1 << 1) 105 #define LINUX_KQ_FLAG_NEED_READ (1 << 2) 106 #define LINUX_KQ_FLAG_NEED_WRITE (1 << 3) 107 /* protects f_selinfo.si_note */ 108 spinlock_t f_kqlock; 109 struct linux_file_wait_queue f_wait_queue; 110 111 /* pointer to associated character device, if any */ 112 struct linux_cdev *f_cdev; 113 114 struct rcu_head rcu; 115 }; 116 117 #define file linux_file 118 #define fasync_struct sigio * 119 120 #define fasync_helper(fd, filp, on, queue) \ 121 ({ \ 122 if ((on)) \ 123 *(queue) = &(filp)->f_sigio; \ 124 else \ 125 *(queue) = NULL; \ 126 0; \ 127 }) 128 129 #define kill_fasync(queue, sig, pollstat) \ 130 do { \ 131 if (*(queue) != NULL) \ 132 pgsigio(*(queue), (sig), 0); \ 133 } while (0) 134 135 typedef int (*filldir_t)(void *, const char *, int, off_t, u64, unsigned); 136 137 struct file_operations { 138 struct module *owner; 139 ssize_t (*read)(struct linux_file *, char __user *, size_t, off_t *); 140 ssize_t (*write)(struct linux_file *, const char __user *, size_t, off_t *); 141 unsigned int (*poll) (struct linux_file *, struct poll_table_struct *); 142 long (*unlocked_ioctl)(struct linux_file *, unsigned int, unsigned long); 143 long (*compat_ioctl)(struct linux_file *, unsigned int, unsigned long); 144 int (*mmap)(struct linux_file *, struct vm_area_struct *); 145 int (*open)(struct inode *, struct file *); 146 int (*release)(struct inode *, struct linux_file *); 147 int (*fasync)(int, struct linux_file *, int); 148 149 /* Although not supported in FreeBSD, to align with Linux code 150 * we are adding llseek() only when it is mapped to no_llseek which returns 151 * an illegal seek error 152 */ 153 off_t (*llseek)(struct linux_file *, off_t, int); 154 #if 0 155 /* We do not support these methods. Don't permit them to compile. */ 156 loff_t (*llseek)(struct file *, loff_t, int); 157 ssize_t (*aio_read)(struct kiocb *, const struct iovec *, 158 unsigned long, loff_t); 159 ssize_t (*aio_write)(struct kiocb *, const struct iovec *, 160 unsigned long, loff_t); 161 int (*readdir)(struct file *, void *, filldir_t); 162 int (*ioctl)(struct inode *, struct file *, unsigned int, 163 unsigned long); 164 int (*flush)(struct file *, fl_owner_t id); 165 int (*fsync)(struct file *, struct dentry *, int datasync); 166 int (*aio_fsync)(struct kiocb *, int datasync); 167 int (*lock)(struct file *, int, struct file_lock *); 168 ssize_t (*sendpage)(struct file *, struct page *, int, size_t, 169 loff_t *, int); 170 unsigned long (*get_unmapped_area)(struct file *, unsigned long, 171 unsigned long, unsigned long, unsigned long); 172 int (*check_flags)(int); 173 int (*flock)(struct file *, int, struct file_lock *); 174 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, 175 loff_t *, size_t, unsigned int); 176 ssize_t (*splice_read)(struct file *, loff_t *, 177 struct pipe_inode_info *, size_t, unsigned int); 178 int (*setlease)(struct file *, long, struct file_lock **); 179 #endif 180 }; 181 #define fops_get(fops) (fops) 182 #define replace_fops(f, fops) ((f)->f_op = (fops)) 183 184 #define FMODE_READ FREAD 185 #define FMODE_WRITE FWRITE 186 #define FMODE_EXEC FEXEC 187 #define FMODE_UNSIGNED_OFFSET 0x2000 188 int __register_chrdev(unsigned int major, unsigned int baseminor, 189 unsigned int count, const char *name, 190 const struct file_operations *fops); 191 int __register_chrdev_p(unsigned int major, unsigned int baseminor, 192 unsigned int count, const char *name, 193 const struct file_operations *fops, uid_t uid, 194 gid_t gid, int mode); 195 void __unregister_chrdev(unsigned int major, unsigned int baseminor, 196 unsigned int count, const char *name); 197 198 static inline void 199 unregister_chrdev(unsigned int major, const char *name) 200 { 201 202 __unregister_chrdev(major, 0, 256, name); 203 } 204 205 static inline int 206 register_chrdev(unsigned int major, const char *name, 207 const struct file_operations *fops) 208 { 209 210 return (__register_chrdev(major, 0, 256, name, fops)); 211 } 212 213 static inline int 214 register_chrdev_p(unsigned int major, const char *name, 215 const struct file_operations *fops, uid_t uid, gid_t gid, int mode) 216 { 217 218 return (__register_chrdev_p(major, 0, 256, name, fops, uid, gid, mode)); 219 } 220 221 static inline int 222 register_chrdev_region(dev_t dev, unsigned range, const char *name) 223 { 224 225 return 0; 226 } 227 228 static inline void 229 unregister_chrdev_region(dev_t dev, unsigned range) 230 { 231 232 return; 233 } 234 235 static inline int 236 alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, 237 const char *name) 238 { 239 240 return 0; 241 } 242 243 /* No current support for seek op in FreeBSD */ 244 static inline int 245 nonseekable_open(struct inode *inode, struct file *filp) 246 { 247 return 0; 248 } 249 250 static inline int 251 simple_open(struct inode *inode, struct file *filp) 252 { 253 filp->private_data = inode->i_private; 254 return 0; 255 } 256 257 extern unsigned int linux_iminor(struct inode *); 258 #define iminor(...) linux_iminor(__VA_ARGS__) 259 260 static inline struct linux_file * 261 get_file(struct linux_file *f) 262 { 263 264 refcount_acquire(f->_file == NULL ? &f->f_count : &f->_file->f_count); 265 return (f); 266 } 267 268 static inline bool 269 get_file_rcu(struct linux_file *f) 270 { 271 return (refcount_acquire_if_not_zero( 272 f->_file == NULL ? &f->f_count : &f->_file->f_count)); 273 } 274 275 static inline struct inode * 276 igrab(struct inode *inode) 277 { 278 int error; 279 280 error = vget(inode, 0); 281 if (error) 282 return (NULL); 283 284 return (inode); 285 } 286 287 static inline void 288 iput(struct inode *inode) 289 { 290 291 vrele(inode); 292 } 293 294 static inline loff_t 295 no_llseek(struct file *file, loff_t offset, int whence) 296 { 297 298 return (-ESPIPE); 299 } 300 301 static inline loff_t 302 default_llseek(struct file *file, loff_t offset, int whence) 303 { 304 return (no_llseek(file, offset, whence)); 305 } 306 307 static inline loff_t 308 generic_file_llseek(struct file *file, loff_t offset, int whence) 309 { 310 return (no_llseek(file, offset, whence)); 311 } 312 313 static inline loff_t 314 noop_llseek(struct linux_file *file, loff_t offset, int whence) 315 { 316 317 return (file->_file->f_offset); 318 } 319 320 static inline struct vnode * 321 file_inode(const struct linux_file *file) 322 { 323 324 return (file->f_vnode); 325 } 326 327 static inline int 328 call_mmap(struct linux_file *file, struct vm_area_struct *vma) 329 { 330 331 return (file->f_op->mmap(file, vma)); 332 } 333 334 static inline void 335 i_size_write(struct inode *inode, loff_t i_size) 336 { 337 } 338 339 /* 340 * simple_read_from_buffer: copy data from kernel-space origin 341 * buffer into user-space destination buffer 342 * 343 * @dest: destination buffer 344 * @read_size: number of bytes to be transferred 345 * @ppos: starting transfer position pointer 346 * @orig: origin buffer 347 * @buf_size: size of destination and origin buffers 348 * 349 * Return value: 350 * On success, total bytes copied with *ppos incremented accordingly. 351 * On failure, negative value. 352 */ 353 static inline ssize_t 354 simple_read_from_buffer(void __user *dest, size_t read_size, loff_t *ppos, 355 void *orig, size_t buf_size) 356 { 357 void *read_pos = ((char *) orig) + *ppos; 358 size_t buf_remain = buf_size - *ppos; 359 ssize_t num_read; 360 361 if (buf_remain < 0 || buf_remain > buf_size) 362 return -EINVAL; 363 364 if (read_size > buf_remain) 365 read_size = buf_remain; 366 367 /* copy_to_user returns number of bytes NOT read */ 368 num_read = read_size - copy_to_user(dest, read_pos, read_size); 369 if (num_read == 0) 370 return -EFAULT; 371 *ppos += num_read; 372 373 return (num_read); 374 } 375 376 MALLOC_DECLARE(M_LSATTR); 377 378 #define DEFINE_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \ 379 static inline int \ 380 __fops ## _open(struct inode *inode, struct file *filp) \ 381 { \ 382 return (simple_attr_open(inode, filp, __get, __set, __fmt)); \ 383 } \ 384 static const struct file_operations __fops = { \ 385 .owner = THIS_MODULE, \ 386 .open = __fops ## _open, \ 387 .release = simple_attr_release, \ 388 .read = simple_attr_read, \ 389 .write = simple_attr_write, \ 390 .llseek = no_llseek \ 391 } 392 393 int simple_attr_open(struct inode *inode, struct file *filp, 394 int (*get)(void *, uint64_t *), int (*set)(void *, uint64_t), 395 const char *fmt); 396 397 int simple_attr_release(struct inode *inode, struct file *filp); 398 399 ssize_t simple_attr_read(struct file *filp, char *buf, size_t read_size, loff_t *ppos); 400 401 ssize_t simple_attr_write(struct file *filp, const char *buf, size_t write_size, loff_t *ppos); 402 403 #endif /* _LINUXKPI_LINUX_FS_H_ */ 404