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-2017 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_FILE_H_ 30 #define _LINUXKPI_LINUX_FILE_H_ 31 32 #include <sys/param.h> 33 #include <sys/file.h> 34 #include <sys/filedesc.h> 35 #include <sys/refcount.h> 36 #include <sys/capsicum.h> 37 #include <sys/proc.h> 38 39 #include <linux/fs.h> 40 #include <linux/slab.h> 41 42 struct linux_file; 43 44 #undef file 45 46 extern struct fileops linuxfileops; 47 48 static inline struct linux_file * 49 linux_fget(unsigned int fd) 50 { 51 struct file *file; 52 53 /* lookup file pointer by file descriptor index */ 54 if (fget_unlocked(curthread, fd, &cap_no_rights, &file) != 0) 55 return (NULL); 56 57 /* check if file handle really belongs to us */ 58 if (file->f_data == NULL || 59 file->f_ops != &linuxfileops) { 60 fdrop(file, curthread); 61 return (NULL); 62 } 63 return ((struct linux_file *)file->f_data); 64 } 65 66 extern void linux_file_free(struct linux_file *filp); 67 68 static inline void 69 fput(struct linux_file *filp) 70 { 71 if (refcount_release(filp->_file == NULL ? 72 &filp->f_count : &filp->_file->f_count)) { 73 linux_file_free(filp); 74 } 75 } 76 77 static inline unsigned int 78 file_count(struct linux_file *filp) 79 { 80 return (filp->_file == NULL ? 81 filp->f_count : filp->_file->f_count); 82 } 83 84 static inline void 85 put_unused_fd(unsigned int fd) 86 { 87 struct file *file; 88 89 if (fget_unlocked(curthread, fd, &cap_no_rights, &file) != 0) { 90 return; 91 } 92 /* 93 * NOTE: We should only get here when the "fd" has not been 94 * installed, so no need to free the associated Linux file 95 * structure. 96 */ 97 fdclose(curthread, file, fd); 98 99 /* drop extra reference */ 100 fdrop(file, curthread); 101 } 102 103 static inline void 104 fd_install(unsigned int fd, struct linux_file *filp) 105 { 106 struct file *file; 107 108 if (fget_unlocked(curthread, fd, &cap_no_rights, &file) != 0) { 109 filp->_file = NULL; 110 } else { 111 filp->_file = file; 112 finit(file, filp->f_mode, DTYPE_DEV, filp, &linuxfileops); 113 114 /* transfer reference count from "filp" to "file" */ 115 while (refcount_release(&filp->f_count) == 0) 116 refcount_acquire(&file->f_count); 117 } 118 119 /* drop the extra reference */ 120 fput(filp); 121 } 122 123 static inline int 124 get_unused_fd(void) 125 { 126 struct file *file; 127 int error; 128 int fd; 129 130 error = falloc(curthread, &file, &fd, 0); 131 if (error) 132 return -error; 133 /* drop the extra reference */ 134 fdrop(file, curthread); 135 return fd; 136 } 137 138 static inline int 139 get_unused_fd_flags(int flags) 140 { 141 struct file *file; 142 int error; 143 int fd; 144 145 error = falloc(curthread, &file, &fd, flags); 146 if (error) 147 return -error; 148 /* drop the extra reference */ 149 fdrop(file, curthread); 150 return fd; 151 } 152 153 extern struct linux_file *linux_file_alloc(void); 154 155 static inline struct linux_file * 156 alloc_file(int mode, const struct file_operations *fops) 157 { 158 struct linux_file *filp; 159 160 filp = linux_file_alloc(); 161 filp->f_op = fops; 162 filp->f_mode = mode; 163 164 return (filp); 165 } 166 167 struct fd { 168 struct linux_file *linux_file; 169 }; 170 171 static inline void fdput(struct fd fd) 172 { 173 fput(fd.linux_file); 174 } 175 176 static inline struct fd fdget(unsigned int fd) 177 { 178 struct linux_file *f = linux_fget(fd); 179 return (struct fd){f}; 180 } 181 182 #define file linux_file 183 #define fget(...) linux_fget(__VA_ARGS__) 184 185 #endif /* _LINUXKPI_LINUX_FILE_H_ */ 186