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