1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2017 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #ifndef	_LINUXKPI_LINUX_FILE_H_
32 #define	_LINUXKPI_LINUX_FILE_H_
33 
34 #include <sys/param.h>
35 #include <sys/file.h>
36 #include <sys/filedesc.h>
37 #include <sys/refcount.h>
38 #include <sys/capsicum.h>
39 #include <sys/proc.h>
40 
41 #include <linux/fs.h>
42 #include <linux/slab.h>
43 
44 struct linux_file;
45 
46 #undef file
47 
48 extern struct fileops linuxfileops;
49 
50 static inline struct linux_file *
51 linux_fget(unsigned int fd)
52 {
53 	struct file *file;
54 
55 	/* lookup file pointer by file descriptor index */
56 	if (fget_unlocked(curthread, fd, &cap_no_rights, &file) != 0)
57 		return (NULL);
58 
59 	/* check if file handle really belongs to us */
60 	if (file->f_data == NULL ||
61 	    file->f_ops != &linuxfileops) {
62 		fdrop(file, curthread);
63 		return (NULL);
64 	}
65 	return ((struct linux_file *)file->f_data);
66 }
67 
68 extern void linux_file_free(struct linux_file *filp);
69 
70 static inline void
71 fput(struct linux_file *filp)
72 {
73 	if (refcount_release(filp->_file == NULL ?
74 	    &filp->f_count : &filp->_file->f_count)) {
75 		linux_file_free(filp);
76 	}
77 }
78 
79 static inline unsigned int
80 file_count(struct linux_file *filp)
81 {
82 	return (filp->_file == NULL ?
83 	    filp->f_count : filp->_file->f_count);
84 }
85 
86 static inline void
87 put_unused_fd(unsigned int fd)
88 {
89 	struct file *file;
90 
91 	if (fget_unlocked(curthread, fd, &cap_no_rights, &file) != 0) {
92 		return;
93 	}
94 	/*
95 	 * NOTE: We should only get here when the "fd" has not been
96 	 * installed, so no need to free the associated Linux file
97 	 * structure.
98 	 */
99 	fdclose(curthread, file, fd);
100 
101 	/* drop extra reference */
102 	fdrop(file, curthread);
103 }
104 
105 static inline void
106 fd_install(unsigned int fd, struct linux_file *filp)
107 {
108 	struct file *file;
109 
110 	if (fget_unlocked(curthread, fd, &cap_no_rights, &file) != 0) {
111 		filp->_file = NULL;
112 	} else {
113 		filp->_file = file;
114 		finit(file, filp->f_mode, DTYPE_DEV, filp, &linuxfileops);
115 
116 		/* transfer reference count from "filp" to "file" */
117 		while (refcount_release(&filp->f_count) == 0)
118 			refcount_acquire(&file->f_count);
119 	}
120 
121 	/* drop the extra reference */
122 	fput(filp);
123 }
124 
125 static inline int
126 get_unused_fd(void)
127 {
128 	struct file *file;
129 	int error;
130 	int fd;
131 
132 	error = falloc(curthread, &file, &fd, 0);
133 	if (error)
134 		return -error;
135 	/* drop the extra reference */
136 	fdrop(file, curthread);
137 	return fd;
138 }
139 
140 static inline int
141 get_unused_fd_flags(int flags)
142 {
143 	struct file *file;
144 	int error;
145 	int fd;
146 
147 	error = falloc(curthread, &file, &fd, flags);
148 	if (error)
149 		return -error;
150 	/* drop the extra reference */
151 	fdrop(file, curthread);
152 	return fd;
153 }
154 
155 extern struct linux_file *linux_file_alloc(void);
156 
157 static inline struct linux_file *
158 alloc_file(int mode, const struct file_operations *fops)
159 {
160 	struct linux_file *filp;
161 
162 	filp = linux_file_alloc();
163 	filp->f_op = fops;
164 	filp->f_mode = mode;
165 
166 	return (filp);
167 }
168 
169 struct fd {
170 	struct linux_file *linux_file;
171 };
172 
173 static inline void fdput(struct fd fd)
174 {
175 	fput(fd.linux_file);
176 }
177 
178 static inline struct fd fdget(unsigned int fd)
179 {
180 	struct linux_file *f = linux_fget(fd);
181 	return (struct fd){f};
182 }
183 
184 #define	file		linux_file
185 #define	fget(...)	linux_fget(__VA_ARGS__)
186 
187 #endif	/* _LINUXKPI_LINUX_FILE_H_ */
188