file.h revision 247602
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
50247602Spjd	if (fget_unlocked(curthread->td_proc->p_fd, fd, 0, 0, &file, NULL) != 0)
51247602Spjd		return (NULL);
52219820Sjeff	return (struct linux_file *)file->f_data;
53219820Sjeff}
54219820Sjeff
55219820Sjeffstatic inline void
56219820Sjefffput(struct linux_file *filp)
57219820Sjeff{
58219820Sjeff	if (filp->_file == NULL) {
59219820Sjeff		kfree(filp);
60219820Sjeff		return;
61219820Sjeff	}
62219820Sjeff	if (refcount_release(&filp->_file->f_count)) {
63219820Sjeff		_fdrop(filp->_file, curthread);
64219820Sjeff		kfree(filp);
65219820Sjeff	}
66219820Sjeff}
67219820Sjeff
68219820Sjeffstatic inline void
69219820Sjeffput_unused_fd(unsigned int fd)
70219820Sjeff{
71219820Sjeff	struct file *file;
72219820Sjeff
73247602Spjd	if (fget_unlocked(curthread->td_proc->p_fd, fd, 0, 0, &file, NULL) != 0)
74219820Sjeff		return;
75219820Sjeff	fdclose(curthread->td_proc->p_fd, file, fd, curthread);
76219820Sjeff}
77219820Sjeff
78219820Sjeffstatic inline void
79219820Sjefffd_install(unsigned int fd, struct linux_file *filp)
80219820Sjeff{
81219820Sjeff	struct file *file;
82219820Sjeff
83247602Spjd	if (fget_unlocked(curthread->td_proc->p_fd, fd, 0, 0, &file, NULL) != 0)
84247602Spjd		file = NULL;
85219820Sjeff	filp->_file = file;
86219820Sjeff        finit(file, filp->f_mode, DTYPE_DEV, filp, &linuxfileops);
87219820Sjeff}
88219820Sjeff
89219820Sjeffstatic inline int
90219820Sjeffget_unused_fd(void)
91219820Sjeff{
92219820Sjeff	struct file *file;
93219820Sjeff	int error;
94219820Sjeff	int fd;
95219820Sjeff
96221055Sjeff	error = falloc(curthread, &file, &fd, 0);
97219820Sjeff	if (error)
98219820Sjeff		return -error;
99219820Sjeff	return fd;
100219820Sjeff}
101219820Sjeff
102219820Sjeffstatic inline struct linux_file *
103219820Sjeff_alloc_file(int mode, const struct file_operations *fops)
104219820Sjeff{
105219820Sjeff	struct linux_file *filp;
106219820Sjeff
107219820Sjeff	filp = kzalloc(sizeof(*filp), GFP_KERNEL);
108219820Sjeff	if (filp == NULL)
109219820Sjeff		return (NULL);
110219820Sjeff	filp->f_op = fops;
111219820Sjeff	filp->f_mode = mode;
112219820Sjeff
113219820Sjeff	return filp;
114219820Sjeff}
115219820Sjeff
116219820Sjeff#define	alloc_file(mnt, root, mode, fops)	_alloc_file((mode), (fops))
117219820Sjeff
118219820Sjeff#define	file	linux_file
119219820Sjeff#define	fget	linux_fget
120219820Sjeff
121219820Sjeff#endif	/* _LINUX_FILE_H_ */
122