file.h revision 255240
1/*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28#ifndef _LINUX_FILE_H_ 29#define _LINUX_FILE_H_ 30 31#include <sys/param.h> 32#include <sys/file.h> 33#include <sys/filedesc.h> 34#include <sys/refcount.h> 35#include <sys/proc.h> 36 37#include <linux/fs.h> 38 39struct linux_file; 40 41#undef file 42 43extern struct fileops linuxfileops; 44 45static inline struct linux_file * 46linux_fget(unsigned int fd) 47{ 48 struct file *file; 49 50 if (fget_unlocked(curthread->td_proc->p_fd, fd, NULL, 0, &file, 51 NULL) != 0) { 52 return (NULL); 53 } 54 return (struct linux_file *)file->f_data; 55} 56 57static inline void 58fput(struct linux_file *filp) 59{ 60 if (filp->_file == NULL) { 61 kfree(filp); 62 return; 63 } 64 if (refcount_release(&filp->_file->f_count)) { 65 _fdrop(filp->_file, curthread); 66 kfree(filp); 67 } 68} 69 70static inline void 71put_unused_fd(unsigned int fd) 72{ 73 struct file *file; 74 75 if (fget_unlocked(curthread->td_proc->p_fd, fd, NULL, 0, &file, 76 NULL) != 0) { 77 return; 78 } 79 fdclose(curthread->td_proc->p_fd, file, fd, curthread); 80} 81 82static inline void 83fd_install(unsigned int fd, struct linux_file *filp) 84{ 85 struct file *file; 86 87 if (fget_unlocked(curthread->td_proc->p_fd, fd, NULL, 0, &file, 88 NULL) != 0) { 89 file = NULL; 90 } 91 filp->_file = file; 92 finit(file, filp->f_mode, DTYPE_DEV, filp, &linuxfileops); 93} 94 95static inline int 96get_unused_fd(void) 97{ 98 struct file *file; 99 int error; 100 int fd; 101 102 error = falloc(curthread, &file, &fd, 0); 103 if (error) 104 return -error; 105 return fd; 106} 107 108static inline struct linux_file * 109_alloc_file(int mode, const struct file_operations *fops) 110{ 111 struct linux_file *filp; 112 113 filp = kzalloc(sizeof(*filp), GFP_KERNEL); 114 if (filp == NULL) 115 return (NULL); 116 filp->f_op = fops; 117 filp->f_mode = mode; 118 119 return filp; 120} 121 122#define alloc_file(mnt, root, mode, fops) _alloc_file((mode), (fops)) 123 124#define file linux_file 125#define fget linux_fget 126 127#endif /* _LINUX_FILE_H_ */ 128