file.h revision 293151
1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc.
3219820Sjeff * Copyright (c) 2010 iX Systems, Inc.
4219820Sjeff * Copyright (c) 2010 Panasas, Inc.
5293151Shselasky * Copyright (c) 2013-2015 Mellanox Technologies, Ltd.
6219820Sjeff * All rights reserved.
7219820Sjeff *
8219820Sjeff * Redistribution and use in source and binary forms, with or without
9219820Sjeff * modification, are permitted provided that the following conditions
10219820Sjeff * are met:
11219820Sjeff * 1. Redistributions of source code must retain the above copyright
12219820Sjeff *    notice unmodified, this list of conditions, and the following
13219820Sjeff *    disclaimer.
14219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
15219820Sjeff *    notice, this list of conditions and the following disclaimer in the
16219820Sjeff *    documentation and/or other materials provided with the distribution.
17219820Sjeff *
18219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28219820Sjeff */
29219820Sjeff#ifndef	_LINUX_FILE_H_
30219820Sjeff#define	_LINUX_FILE_H_
31219820Sjeff
32219820Sjeff#include <sys/param.h>
33219820Sjeff#include <sys/file.h>
34219820Sjeff#include <sys/filedesc.h>
35219820Sjeff#include <sys/refcount.h>
36219820Sjeff#include <sys/proc.h>
37219820Sjeff
38219820Sjeff#include <linux/fs.h>
39219820Sjeff
40219820Sjeffstruct linux_file;
41219820Sjeff
42219820Sjeff#undef file
43219820Sjeff
44219820Sjeffextern struct fileops linuxfileops;
45219820Sjeff
46219820Sjeffstatic inline struct linux_file *
47219820Sjefflinux_fget(unsigned int fd)
48219820Sjeff{
49219820Sjeff	struct file *file;
50219820Sjeff
51255240Spjd	if (fget_unlocked(curthread->td_proc->p_fd, fd, NULL, 0, &file,
52255240Spjd	    NULL) != 0) {
53247602Spjd		return (NULL);
54255240Spjd	}
55219820Sjeff	return (struct linux_file *)file->f_data;
56219820Sjeff}
57219820Sjeff
58219820Sjeffstatic inline void
59219820Sjefffput(struct linux_file *filp)
60219820Sjeff{
61219820Sjeff	if (filp->_file == NULL) {
62219820Sjeff		kfree(filp);
63219820Sjeff		return;
64219820Sjeff	}
65219820Sjeff	if (refcount_release(&filp->_file->f_count)) {
66219820Sjeff		_fdrop(filp->_file, curthread);
67219820Sjeff		kfree(filp);
68219820Sjeff	}
69219820Sjeff}
70219820Sjeff
71219820Sjeffstatic inline void
72219820Sjeffput_unused_fd(unsigned int fd)
73219820Sjeff{
74219820Sjeff	struct file *file;
75219820Sjeff
76255240Spjd	if (fget_unlocked(curthread->td_proc->p_fd, fd, NULL, 0, &file,
77255240Spjd	    NULL) != 0) {
78219820Sjeff		return;
79255240Spjd	}
80277139Shselasky	/*
81277139Shselasky	 * NOTE: We should only get here when the "fd" has not been
82277139Shselasky	 * installed, so no need to free the associated Linux file
83277139Shselasky	 * structure.
84277139Shselasky	 */
85219820Sjeff	fdclose(curthread->td_proc->p_fd, file, fd, curthread);
86277139Shselasky
87277139Shselasky	/* drop extra reference */
88277139Shselasky	fdrop(file, curthread);
89219820Sjeff}
90219820Sjeff
91219820Sjeffstatic inline void
92219820Sjefffd_install(unsigned int fd, struct linux_file *filp)
93219820Sjeff{
94219820Sjeff	struct file *file;
95219820Sjeff
96255240Spjd	if (fget_unlocked(curthread->td_proc->p_fd, fd, NULL, 0, &file,
97255240Spjd	    NULL) != 0) {
98247602Spjd		file = NULL;
99255240Spjd	}
100219820Sjeff	filp->_file = file;
101277139Shselasky	finit(file, filp->f_mode, DTYPE_DEV, filp, &linuxfileops);
102277139Shselasky
103277139Shselasky	/* drop the extra reference */
104277139Shselasky	fput(filp);
105219820Sjeff}
106219820Sjeff
107219820Sjeffstatic inline int
108219820Sjeffget_unused_fd(void)
109219820Sjeff{
110219820Sjeff	struct file *file;
111219820Sjeff	int error;
112219820Sjeff	int fd;
113219820Sjeff
114221055Sjeff	error = falloc(curthread, &file, &fd, 0);
115219820Sjeff	if (error)
116219820Sjeff		return -error;
117277139Shselasky	/* drop the extra reference */
118277139Shselasky	fdrop(file, curthread);
119219820Sjeff	return fd;
120219820Sjeff}
121219820Sjeff
122293151Shselaskystatic inline int
123293151Shselaskyget_unused_fd_flags(int flags)
124293151Shselasky{
125293151Shselasky	struct file *file;
126293151Shselasky	int error;
127293151Shselasky	int fd;
128293151Shselasky
129293151Shselasky	error = falloc(curthread, &file, &fd, flags);
130293151Shselasky	if (error)
131293151Shselasky		return -error;
132293151Shselasky	/* drop the extra reference */
133293151Shselasky	fdrop(file, curthread);
134293151Shselasky	return fd;
135293151Shselasky}
136293151Shselasky
137219820Sjeffstatic inline struct linux_file *
138271127Shselaskyalloc_file(int mode, const struct file_operations *fops)
139219820Sjeff{
140219820Sjeff	struct linux_file *filp;
141219820Sjeff
142219820Sjeff	filp = kzalloc(sizeof(*filp), GFP_KERNEL);
143271127Shselasky	if (filp == NULL)
144219820Sjeff		return (NULL);
145219820Sjeff	filp->f_op = fops;
146219820Sjeff	filp->f_mode = mode;
147219820Sjeff
148219820Sjeff	return filp;
149219820Sjeff}
150219820Sjeff
151271127Shselaskystruct fd {
152271127Shselasky	struct linux_file *linux_file;
153271127Shselasky};
154219820Sjeff
155271127Shselaskystatic inline void fdput(struct fd fd)
156271127Shselasky{
157271127Shselasky	fput(fd.linux_file);
158271127Shselasky}
159271127Shselasky
160271127Shselaskystatic inline struct fd fdget(unsigned int fd)
161271127Shselasky{
162271127Shselasky	struct linux_file *f = linux_fget(fd);
163271127Shselasky	return (struct fd){f};
164271127Shselasky}
165271127Shselasky
166219820Sjeff#define	file	linux_file
167219820Sjeff#define	fget	linux_fget
168219820Sjeff
169219820Sjeff#endif	/* _LINUX_FILE_H_ */
170