file.h revision 255240
1219820Sjeff/*- 2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc. 3219820Sjeff * Copyright (c) 2010 iX Systems, Inc. 4219820Sjeff * Copyright (c) 2010 Panasas, Inc. 5219820Sjeff * All rights reserved. 6219820Sjeff * 7219820Sjeff * Redistribution and use in source and binary forms, with or without 8219820Sjeff * modification, are permitted provided that the following conditions 9219820Sjeff * are met: 10219820Sjeff * 1. Redistributions of source code must retain the above copyright 11219820Sjeff * notice unmodified, this list of conditions, and the following 12219820Sjeff * disclaimer. 13219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright 14219820Sjeff * notice, this list of conditions and the following disclaimer in the 15219820Sjeff * documentation and/or other materials provided with the distribution. 16219820Sjeff * 17219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27219820Sjeff */ 28219820Sjeff#ifndef _LINUX_FILE_H_ 29219820Sjeff#define _LINUX_FILE_H_ 30219820Sjeff 31219820Sjeff#include <sys/param.h> 32219820Sjeff#include <sys/file.h> 33219820Sjeff#include <sys/filedesc.h> 34219820Sjeff#include <sys/refcount.h> 35219820Sjeff#include <sys/proc.h> 36219820Sjeff 37219820Sjeff#include <linux/fs.h> 38219820Sjeff 39219820Sjeffstruct linux_file; 40219820Sjeff 41219820Sjeff#undef file 42219820Sjeff 43219820Sjeffextern struct fileops linuxfileops; 44219820Sjeff 45219820Sjeffstatic inline struct linux_file * 46219820Sjefflinux_fget(unsigned int fd) 47219820Sjeff{ 48219820Sjeff struct file *file; 49219820Sjeff 50255240Spjd if (fget_unlocked(curthread->td_proc->p_fd, fd, NULL, 0, &file, 51255240Spjd NULL) != 0) { 52247602Spjd return (NULL); 53255240Spjd } 54219820Sjeff return (struct linux_file *)file->f_data; 55219820Sjeff} 56219820Sjeff 57219820Sjeffstatic inline void 58219820Sjefffput(struct linux_file *filp) 59219820Sjeff{ 60219820Sjeff if (filp->_file == NULL) { 61219820Sjeff kfree(filp); 62219820Sjeff return; 63219820Sjeff } 64219820Sjeff if (refcount_release(&filp->_file->f_count)) { 65219820Sjeff _fdrop(filp->_file, curthread); 66219820Sjeff kfree(filp); 67219820Sjeff } 68219820Sjeff} 69219820Sjeff 70219820Sjeffstatic inline void 71219820Sjeffput_unused_fd(unsigned int fd) 72219820Sjeff{ 73219820Sjeff struct file *file; 74219820Sjeff 75255240Spjd if (fget_unlocked(curthread->td_proc->p_fd, fd, NULL, 0, &file, 76255240Spjd NULL) != 0) { 77219820Sjeff return; 78255240Spjd } 79219820Sjeff fdclose(curthread->td_proc->p_fd, file, fd, curthread); 80219820Sjeff} 81219820Sjeff 82219820Sjeffstatic inline void 83219820Sjefffd_install(unsigned int fd, struct linux_file *filp) 84219820Sjeff{ 85219820Sjeff struct file *file; 86219820Sjeff 87255240Spjd if (fget_unlocked(curthread->td_proc->p_fd, fd, NULL, 0, &file, 88255240Spjd NULL) != 0) { 89247602Spjd file = NULL; 90255240Spjd } 91219820Sjeff filp->_file = file; 92219820Sjeff finit(file, filp->f_mode, DTYPE_DEV, filp, &linuxfileops); 93219820Sjeff} 94219820Sjeff 95219820Sjeffstatic inline int 96219820Sjeffget_unused_fd(void) 97219820Sjeff{ 98219820Sjeff struct file *file; 99219820Sjeff int error; 100219820Sjeff int fd; 101219820Sjeff 102221055Sjeff error = falloc(curthread, &file, &fd, 0); 103219820Sjeff if (error) 104219820Sjeff return -error; 105219820Sjeff return fd; 106219820Sjeff} 107219820Sjeff 108219820Sjeffstatic inline struct linux_file * 109219820Sjeff_alloc_file(int mode, const struct file_operations *fops) 110219820Sjeff{ 111219820Sjeff struct linux_file *filp; 112219820Sjeff 113219820Sjeff filp = kzalloc(sizeof(*filp), GFP_KERNEL); 114219820Sjeff if (filp == NULL) 115219820Sjeff return (NULL); 116219820Sjeff filp->f_op = fops; 117219820Sjeff filp->f_mode = mode; 118219820Sjeff 119219820Sjeff return filp; 120219820Sjeff} 121219820Sjeff 122219820Sjeff#define alloc_file(mnt, root, mode, fops) _alloc_file((mode), (fops)) 123219820Sjeff 124219820Sjeff#define file linux_file 125219820Sjeff#define fget linux_fget 126219820Sjeff 127219820Sjeff#endif /* _LINUX_FILE_H_ */ 128