xref: /freebsd/sys/compat/linuxkpi/common/include/linux/file.h (revision 47606b869eb149ebb7135d6594c3b9b9f05b9aed)
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	_LINUX_FILE_H_
32 #define	_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 	cap_rights_t rights;
54 	struct file *file;
55 
56 	if (fget_unlocked(curthread->td_proc->p_fd, fd,
57 	    cap_rights_init(&rights), &file, NULL) != 0) {
58 		return (NULL);
59 	}
60 	return (struct linux_file *)file->f_data;
61 }
62 
63 extern void linux_file_free(struct linux_file *filp);
64 
65 static inline void
66 fput(struct linux_file *filp)
67 {
68 	if (refcount_release(filp->_file == NULL ?
69 	    &filp->f_count : &filp->_file->f_count)) {
70 		linux_file_free(filp);
71 	}
72 }
73 
74 static inline unsigned int
75 file_count(struct linux_file *filp)
76 {
77 	return (filp->_file == NULL ?
78 	    filp->f_count : filp->_file->f_count);
79 }
80 
81 static inline void
82 put_unused_fd(unsigned int fd)
83 {
84 	cap_rights_t rights;
85 	struct file *file;
86 
87 	if (fget_unlocked(curthread->td_proc->p_fd, fd,
88 	    cap_rights_init(&rights), &file, NULL) != 0) {
89 		return;
90 	}
91 	/*
92 	 * NOTE: We should only get here when the "fd" has not been
93 	 * installed, so no need to free the associated Linux file
94 	 * structure.
95 	 */
96 	fdclose(curthread, file, fd);
97 
98 	/* drop extra reference */
99 	fdrop(file, curthread);
100 }
101 
102 static inline void
103 fd_install(unsigned int fd, struct linux_file *filp)
104 {
105 	cap_rights_t rights;
106 	struct file *file;
107 
108 	if (fget_unlocked(curthread->td_proc->p_fd, fd,
109 	    cap_rights_init(&rights), &file, NULL) != 0) {
110 		filp->_file = NULL;
111 	} else {
112 		filp->_file = file;
113 		finit(file, filp->f_mode, DTYPE_DEV, filp, &linuxfileops);
114 
115 		/* transfer reference count from "filp" to "file" */
116 		while (refcount_release(&filp->f_count) == 0)
117 			refcount_acquire(&file->f_count);
118 	}
119 
120 	/* drop the extra reference */
121 	fput(filp);
122 }
123 
124 static inline int
125 get_unused_fd(void)
126 {
127 	struct file *file;
128 	int error;
129 	int fd;
130 
131 	error = falloc(curthread, &file, &fd, 0);
132 	if (error)
133 		return -error;
134 	/* drop the extra reference */
135 	fdrop(file, curthread);
136 	return fd;
137 }
138 
139 static inline int
140 get_unused_fd_flags(int flags)
141 {
142 	struct file *file;
143 	int error;
144 	int fd;
145 
146 	error = falloc(curthread, &file, &fd, flags);
147 	if (error)
148 		return -error;
149 	/* drop the extra reference */
150 	fdrop(file, curthread);
151 	return fd;
152 }
153 
154 extern struct linux_file *linux_file_alloc(void);
155 
156 static inline struct linux_file *
157 alloc_file(int mode, const struct file_operations *fops)
158 {
159 	struct linux_file *filp;
160 
161 	filp = linux_file_alloc();
162 	filp->f_op = fops;
163 	filp->f_mode = mode;
164 
165 	return (filp);
166 }
167 
168 struct fd {
169 	struct linux_file *linux_file;
170 };
171 
172 static inline void fdput(struct fd fd)
173 {
174 	fput(fd.linux_file);
175 }
176 
177 static inline struct fd fdget(unsigned int fd)
178 {
179 	struct linux_file *f = linux_fget(fd);
180 	return (struct fd){f};
181 }
182 
183 #define	file		linux_file
184 #define	fget(...)	linux_fget(__VA_ARGS__)
185 
186 #endif	/* _LINUX_FILE_H_ */
187