fs.h revision 328653
1219820Sjeff/*- 2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc. 3219820Sjeff * Copyright (c) 2010 iX Systems, Inc. 4219820Sjeff * Copyright (c) 2010 Panasas, Inc. 5328653Shselasky * Copyright (c) 2013-2017 Mellanox Technologies, Ltd. 6219820Sjeff * All rights reserved. 7219820Sjeff * 8219820Sjeff * Redistribution and use in source and binary forms, with or without 9219820Sjeff * modification, are permitted provided that the following conditions 10219820Sjeff * are met: 11219820Sjeff * 1. Redistributions of source code must retain the above copyright 12219820Sjeff * notice unmodified, this list of conditions, and the following 13219820Sjeff * disclaimer. 14219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright 15219820Sjeff * notice, this list of conditions and the following disclaimer in the 16219820Sjeff * documentation and/or other materials provided with the distribution. 17219820Sjeff * 18219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28289644Shselasky * 29289644Shselasky * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/fs.h 328653 2018-02-01 13:01:44Z hselasky $ 30219820Sjeff */ 31219820Sjeff#ifndef _LINUX_FS_H_ 32219820Sjeff#define _LINUX_FS_H_ 33219820Sjeff 34280764Shselasky#include <sys/cdefs.h> 35280764Shselasky#include <sys/param.h> 36219820Sjeff#include <sys/systm.h> 37219820Sjeff#include <sys/conf.h> 38219820Sjeff#include <sys/vnode.h> 39219820Sjeff#include <sys/file.h> 40219820Sjeff#include <sys/filedesc.h> 41219820Sjeff#include <linux/types.h> 42219820Sjeff#include <linux/wait.h> 43219820Sjeff#include <linux/semaphore.h> 44328653Shselasky#include <linux/spinlock.h> 45219820Sjeff 46219820Sjeffstruct module; 47219820Sjeffstruct kiocb; 48219820Sjeffstruct iovec; 49219820Sjeffstruct dentry; 50219820Sjeffstruct page; 51219820Sjeffstruct file_lock; 52219820Sjeffstruct pipe_inode_info; 53219820Sjeffstruct vm_area_struct; 54219820Sjeffstruct poll_table_struct; 55219820Sjeffstruct files_struct; 56328653Shselaskystruct pfs_node; 57219820Sjeff 58219820Sjeff#define inode vnode 59219820Sjeff#define i_cdev v_rdev 60328653Shselasky#define i_private v_data 61219820Sjeff 62219820Sjeff#define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH) 63219820Sjeff#define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH) 64219820Sjeff 65219820Sjeff 66219820Sjefftypedef struct files_struct *fl_owner_t; 67219820Sjeff 68219820Sjeffstruct dentry { 69219820Sjeff struct inode *d_inode; 70328653Shselasky struct pfs_node *d_pfs_node; 71219820Sjeff}; 72219820Sjeff 73219820Sjeffstruct file_operations; 74219820Sjeff 75328653Shselaskystruct linux_file_wait_queue { 76328653Shselasky struct wait_queue wq; 77328653Shselasky struct wait_queue_head *wqh; 78328653Shselasky atomic_t state; 79328653Shselasky#define LINUX_FWQ_STATE_INIT 0 80328653Shselasky#define LINUX_FWQ_STATE_NOT_READY 1 81328653Shselasky#define LINUX_FWQ_STATE_QUEUED 2 82328653Shselasky#define LINUX_FWQ_STATE_READY 3 83328653Shselasky#define LINUX_FWQ_STATE_MAX 4 84328653Shselasky}; 85328653Shselasky 86219820Sjeffstruct linux_file { 87219820Sjeff struct file *_file; 88219820Sjeff const struct file_operations *f_op; 89219820Sjeff void *private_data; 90219820Sjeff int f_flags; 91219820Sjeff int f_mode; /* Just starting mode. */ 92219820Sjeff struct dentry *f_dentry; 93219820Sjeff struct dentry f_dentry_store; 94219820Sjeff struct selinfo f_selinfo; 95219820Sjeff struct sigio *f_sigio; 96251617Sjhb struct vnode *f_vnode; 97328653Shselasky#define f_inode f_vnode 98328653Shselasky volatile u_int f_count; 99328653Shselasky 100328653Shselasky /* anonymous shmem object */ 101328653Shselasky vm_object_t f_shmem; 102328653Shselasky 103328653Shselasky /* kqfilter support */ 104328653Shselasky int f_kqflags; 105328653Shselasky#define LINUX_KQ_FLAG_HAS_READ (1 << 0) 106328653Shselasky#define LINUX_KQ_FLAG_HAS_WRITE (1 << 1) 107328653Shselasky#define LINUX_KQ_FLAG_NEED_READ (1 << 2) 108328653Shselasky#define LINUX_KQ_FLAG_NEED_WRITE (1 << 3) 109328653Shselasky /* protects f_selinfo.si_note */ 110328653Shselasky spinlock_t f_kqlock; 111328653Shselasky struct linux_file_wait_queue f_wait_queue; 112219820Sjeff}; 113219820Sjeff 114219820Sjeff#define file linux_file 115219820Sjeff#define fasync_struct sigio * 116219820Sjeff 117219820Sjeff#define fasync_helper(fd, filp, on, queue) \ 118219820Sjeff({ \ 119219820Sjeff if ((on)) \ 120219820Sjeff *(queue) = &(filp)->f_sigio; \ 121219820Sjeff else \ 122219820Sjeff *(queue) = NULL; \ 123219820Sjeff 0; \ 124219820Sjeff}) 125219820Sjeff 126219820Sjeff#define kill_fasync(queue, sig, pollstat) \ 127219820Sjeffdo { \ 128219820Sjeff if (*(queue) != NULL) \ 129219820Sjeff pgsigio(*(queue), (sig), 0); \ 130219820Sjeff} while (0) 131219820Sjeff 132219820Sjefftypedef int (*filldir_t)(void *, const char *, int, loff_t, u64, unsigned); 133219820Sjeff 134219820Sjeffstruct file_operations { 135219820Sjeff struct module *owner; 136219820Sjeff ssize_t (*read)(struct file *, char __user *, size_t, loff_t *); 137219820Sjeff ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *); 138219820Sjeff unsigned int (*poll) (struct file *, struct poll_table_struct *); 139219820Sjeff long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long); 140328653Shselasky long (*compat_ioctl)(struct file *, unsigned int, unsigned long); 141219820Sjeff int (*mmap)(struct file *, struct vm_area_struct *); 142219820Sjeff int (*open)(struct inode *, struct file *); 143219820Sjeff int (*release)(struct inode *, struct file *); 144219820Sjeff int (*fasync)(int, struct file *, int); 145270710Shselasky 146270710Shselasky/* Although not supported in FreeBSD, to align with Linux code 147270710Shselasky * we are adding llseek() only when it is mapped to no_llseek which returns 148270710Shselasky * an illegal seek error 149270710Shselasky */ 150270710Shselasky loff_t (*llseek)(struct file *, loff_t, int); 151219820Sjeff#if 0 152219820Sjeff /* We do not support these methods. Don't permit them to compile. */ 153219820Sjeff loff_t (*llseek)(struct file *, loff_t, int); 154219820Sjeff ssize_t (*aio_read)(struct kiocb *, const struct iovec *, 155219820Sjeff unsigned long, loff_t); 156219820Sjeff ssize_t (*aio_write)(struct kiocb *, const struct iovec *, 157219820Sjeff unsigned long, loff_t); 158219820Sjeff int (*readdir)(struct file *, void *, filldir_t); 159219820Sjeff int (*ioctl)(struct inode *, struct file *, unsigned int, 160219820Sjeff unsigned long); 161219820Sjeff int (*flush)(struct file *, fl_owner_t id); 162219820Sjeff int (*fsync)(struct file *, struct dentry *, int datasync); 163219820Sjeff int (*aio_fsync)(struct kiocb *, int datasync); 164219820Sjeff int (*lock)(struct file *, int, struct file_lock *); 165219820Sjeff ssize_t (*sendpage)(struct file *, struct page *, int, size_t, 166219820Sjeff loff_t *, int); 167219820Sjeff unsigned long (*get_unmapped_area)(struct file *, unsigned long, 168219820Sjeff unsigned long, unsigned long, unsigned long); 169219820Sjeff int (*check_flags)(int); 170219820Sjeff int (*flock)(struct file *, int, struct file_lock *); 171219820Sjeff ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, 172219820Sjeff loff_t *, size_t, unsigned int); 173219820Sjeff ssize_t (*splice_read)(struct file *, loff_t *, 174219820Sjeff struct pipe_inode_info *, size_t, unsigned int); 175219820Sjeff int (*setlease)(struct file *, long, struct file_lock **); 176219820Sjeff#endif 177219820Sjeff}; 178328653Shselasky#define fops_get(fops) (fops) 179328653Shselasky#define replace_fops(f, fops) ((f)->f_op = (fops)) 180219820Sjeff 181219820Sjeff#define FMODE_READ FREAD 182219820Sjeff#define FMODE_WRITE FWRITE 183219820Sjeff#define FMODE_EXEC FEXEC 184219820Sjeff 185311803Shselaskyint __register_chrdev(unsigned int major, unsigned int baseminor, 186311803Shselasky unsigned int count, const char *name, 187311803Shselasky const struct file_operations *fops); 188311803Shselaskyint __register_chrdev_p(unsigned int major, unsigned int baseminor, 189311803Shselasky unsigned int count, const char *name, 190311803Shselasky const struct file_operations *fops, uid_t uid, 191311803Shselasky gid_t gid, int mode); 192311803Shselaskyvoid __unregister_chrdev(unsigned int major, unsigned int baseminor, 193311803Shselasky unsigned int count, const char *name); 194311803Shselasky 195311803Shselaskystatic inline void 196311803Shselaskyunregister_chrdev(unsigned int major, const char *name) 197311803Shselasky{ 198311803Shselasky 199311803Shselasky __unregister_chrdev(major, 0, 256, name); 200311803Shselasky} 201311803Shselasky 202219820Sjeffstatic inline int 203311803Shselaskyregister_chrdev(unsigned int major, const char *name, 204311803Shselasky const struct file_operations *fops) 205311803Shselasky{ 206311803Shselasky 207311803Shselasky return (__register_chrdev(major, 0, 256, name, fops)); 208311803Shselasky} 209311803Shselasky 210311803Shselaskystatic inline int 211311803Shselaskyregister_chrdev_p(unsigned int major, const char *name, 212311803Shselasky const struct file_operations *fops, uid_t uid, gid_t gid, int mode) 213311803Shselasky{ 214311803Shselasky 215311803Shselasky return (__register_chrdev_p(major, 0, 256, name, fops, uid, gid, mode)); 216311803Shselasky} 217311803Shselasky 218311803Shselaskystatic inline int 219219820Sjeffregister_chrdev_region(dev_t dev, unsigned range, const char *name) 220219820Sjeff{ 221219820Sjeff 222219820Sjeff return 0; 223219820Sjeff} 224219820Sjeff 225219820Sjeffstatic inline void 226219820Sjeffunregister_chrdev_region(dev_t dev, unsigned range) 227219820Sjeff{ 228219820Sjeff 229219820Sjeff return; 230219820Sjeff} 231219820Sjeff 232270710Shselaskystatic inline int 233270710Shselaskyalloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, 234270710Shselasky const char *name) 235270710Shselasky{ 236270710Shselasky 237270710Shselasky return 0; 238270710Shselasky} 239270710Shselasky 240270710Shselasky/* No current support for seek op in FreeBSD */ 241270710Shselaskystatic inline int 242270710Shselaskynonseekable_open(struct inode *inode, struct file *filp) 243270710Shselasky{ 244270710Shselasky return 0; 245270710Shselasky} 246270710Shselasky 247328653Shselaskyextern unsigned int linux_iminor(struct inode *); 248328653Shselasky#define iminor(...) linux_iminor(__VA_ARGS__) 249328653Shselasky 250328653Shselaskystatic inline struct linux_file * 251328653Shselaskyget_file(struct linux_file *f) 252219820Sjeff{ 253219820Sjeff 254328653Shselasky refcount_acquire(f->_file == NULL ? &f->f_count : &f->_file->f_count); 255328653Shselasky return (f); 256219820Sjeff} 257219820Sjeff 258219820Sjeffstatic inline struct inode * 259219820Sjeffigrab(struct inode *inode) 260219820Sjeff{ 261219820Sjeff int error; 262219820Sjeff 263219820Sjeff error = vget(inode, 0, curthread); 264219820Sjeff if (error) 265219820Sjeff return (NULL); 266219820Sjeff 267219820Sjeff return (inode); 268219820Sjeff} 269219820Sjeff 270219820Sjeffstatic inline void 271219820Sjeffiput(struct inode *inode) 272219820Sjeff{ 273219820Sjeff 274219820Sjeff vrele(inode); 275219820Sjeff} 276219820Sjeff 277270710Shselaskystatic inline loff_t 278270710Shselaskyno_llseek(struct file *file, loff_t offset, int whence) 279270710Shselasky{ 280328653Shselasky 281328653Shselasky return (-ESPIPE); 282270710Shselasky} 283270710Shselasky 284328653Shselaskystatic inline loff_t 285328653Shselaskynoop_llseek(struct linux_file *file, loff_t offset, int whence) 286328653Shselasky{ 287328653Shselasky 288328653Shselasky return (file->_file->f_offset); 289328653Shselasky} 290328653Shselasky 291328653Shselasky/* Shared memory support */ 292328653Shselaskyunsigned long linux_invalidate_mapping_pages(vm_object_t, pgoff_t, pgoff_t); 293328653Shselaskystruct page *linux_shmem_read_mapping_page_gfp(vm_object_t, int, gfp_t); 294328653Shselaskystruct linux_file *linux_shmem_file_setup(const char *, loff_t, unsigned long); 295328653Shselaskyvoid linux_shmem_truncate_range(vm_object_t, loff_t, loff_t); 296328653Shselasky 297328653Shselasky#define invalidate_mapping_pages(...) \ 298328653Shselasky linux_invalidate_mapping_pages(__VA_ARGS__) 299328653Shselasky 300328653Shselasky#define shmem_read_mapping_page(...) \ 301328653Shselasky linux_shmem_read_mapping_page_gfp(__VA_ARGS__, 0) 302328653Shselasky 303328653Shselasky#define shmem_read_mapping_page_gfp(...) \ 304328653Shselasky linux_shmem_read_mapping_page_gfp(__VA_ARGS__) 305328653Shselasky 306328653Shselasky#define shmem_file_setup(...) \ 307328653Shselasky linux_shmem_file_setup(__VA_ARGS__) 308328653Shselasky 309328653Shselasky#define shmem_truncate_range(...) \ 310328653Shselasky linux_shmem_truncate_range(__VA_ARGS__) 311328653Shselasky 312270710Shselasky#endif /* _LINUX_FS_H_ */ 313