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