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) {
98293736Shselasky		filp->_file = NULL;
99293736Shselasky	} else {
100293736Shselasky		filp->_file = file;
101293736Shselasky		finit(file, filp->f_mode, DTYPE_DEV, filp, &linuxfileops);
102255240Spjd	}
103277139Shselasky
104277139Shselasky	/* drop the extra reference */
105277139Shselasky	fput(filp);
106219820Sjeff}
107219820Sjeff
108219820Sjeffstatic inline int
109219820Sjeffget_unused_fd(void)
110219820Sjeff{
111219820Sjeff	struct file *file;
112219820Sjeff	int error;
113219820Sjeff	int fd;
114219820Sjeff
115221055Sjeff	error = falloc(curthread, &file, &fd, 0);
116219820Sjeff	if (error)
117219820Sjeff		return -error;
118277139Shselasky	/* drop the extra reference */
119277139Shselasky	fdrop(file, curthread);
120219820Sjeff	return fd;
121219820Sjeff}
122219820Sjeff
123293151Shselaskystatic inline int
124293151Shselaskyget_unused_fd_flags(int flags)
125293151Shselasky{
126293151Shselasky	struct file *file;
127293151Shselasky	int error;
128293151Shselasky	int fd;
129293151Shselasky
130293151Shselasky	error = falloc(curthread, &file, &fd, flags);
131293151Shselasky	if (error)
132293151Shselasky		return -error;
133293151Shselasky	/* drop the extra reference */
134293151Shselasky	fdrop(file, curthread);
135293151Shselasky	return fd;
136293151Shselasky}
137293151Shselasky
138219820Sjeffstatic inline struct linux_file *
139271127Shselaskyalloc_file(int mode, const struct file_operations *fops)
140219820Sjeff{
141219820Sjeff	struct linux_file *filp;
142219820Sjeff
143219820Sjeff	filp = kzalloc(sizeof(*filp), GFP_KERNEL);
144271127Shselasky	if (filp == NULL)
145219820Sjeff		return (NULL);
146219820Sjeff	filp->f_op = fops;
147219820Sjeff	filp->f_mode = mode;
148219820Sjeff
149219820Sjeff	return filp;
150219820Sjeff}
151219820Sjeff
152271127Shselaskystruct fd {
153271127Shselasky	struct linux_file *linux_file;
154271127Shselasky};
155219820Sjeff
156271127Shselaskystatic inline void fdput(struct fd fd)
157271127Shselasky{
158271127Shselasky	fput(fd.linux_file);
159271127Shselasky}
160271127Shselasky
161271127Shselaskystatic inline struct fd fdget(unsigned int fd)
162271127Shselasky{
163271127Shselasky	struct linux_file *f = linux_fget(fd);
164271127Shselasky	return (struct fd){f};
165271127Shselasky}
166271127Shselasky
167219820Sjeff#define	file	linux_file
168219820Sjeff#define	fget	linux_fget
169219820Sjeff
170219820Sjeff#endif	/* _LINUX_FILE_H_ */
171