file.h revision 219820
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 file = fget_unlocked(curthread->td_proc->p_fd, fd); 51 return (struct linux_file *)file->f_data; 52} 53 54static inline void 55fput(struct linux_file *filp) 56{ 57 if (filp->_file == NULL) { 58 kfree(filp); 59 return; 60 } 61 if (refcount_release(&filp->_file->f_count)) { 62 _fdrop(filp->_file, curthread); 63 kfree(filp); 64 } 65} 66 67static inline void 68put_unused_fd(unsigned int fd) 69{ 70 struct file *file; 71 72 file = fget_unlocked(curthread->td_proc->p_fd, fd); 73 if (file == NULL) 74 return; 75 fdclose(curthread->td_proc->p_fd, file, fd, curthread); 76} 77 78static inline void 79fd_install(unsigned int fd, struct linux_file *filp) 80{ 81 struct file *file; 82 83 file = fget_unlocked(curthread->td_proc->p_fd, fd); 84 filp->_file = file; 85 finit(file, filp->f_mode, DTYPE_DEV, filp, &linuxfileops); 86} 87 88static inline int 89get_unused_fd(void) 90{ 91 struct file *file; 92 int error; 93 int fd; 94 95 error = falloc(curthread, &file, &fd); 96 if (error) 97 return -error; 98 return fd; 99} 100 101static inline struct linux_file * 102_alloc_file(int mode, const struct file_operations *fops) 103{ 104 struct linux_file *filp; 105 106 filp = kzalloc(sizeof(*filp), GFP_KERNEL); 107 if (filp == NULL) 108 return (NULL); 109 filp->f_op = fops; 110 filp->f_mode = mode; 111 112 return filp; 113} 114 115#define alloc_file(mnt, root, mode, fops) _alloc_file((mode), (fops)) 116 117#define file linux_file 118#define fget linux_fget 119 120#endif /* _LINUX_FILE_H_ */ 121