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-2018 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 #ifndef _LINUXKPI_LINUX_FS_H_
30 #define _LINUXKPI_LINUX_FS_H_
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/vnode.h>
36 #include <sys/file.h>
37 #include <sys/filedesc.h>
38 #include <linux/types.h>
39 #include <linux/wait.h>
40 #include <linux/semaphore.h>
41 #include <linux/spinlock.h>
42 #include <linux/dcache.h>
43 #include <linux/capability.h>
44 #include <linux/wait_bit.h>
45 #include <linux/kernel.h>
46 #include <linux/mount.h>
47 #include <linux/mutex.h>
48
49 struct module;
50 struct kiocb;
51 struct iovec;
52 struct dentry;
53 struct page;
54 struct file_lock;
55 struct pipe_inode_info;
56 struct vm_area_struct;
57 struct poll_table_struct;
58 struct files_struct;
59 struct pfs_node;
60 struct linux_cdev;
61
62 #define inode vnode
63 #define i_cdev v_rdev
64 #define i_private v_data
65
66 #define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
67 #define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
68
69 typedef struct files_struct *fl_owner_t;
70
71 struct file_operations;
72
73 struct linux_file_wait_queue {
74 struct wait_queue wq;
75 struct wait_queue_head *wqh;
76 atomic_t state;
77 #define LINUX_FWQ_STATE_INIT 0
78 #define LINUX_FWQ_STATE_NOT_READY 1
79 #define LINUX_FWQ_STATE_QUEUED 2
80 #define LINUX_FWQ_STATE_READY 3
81 #define LINUX_FWQ_STATE_MAX 4
82 };
83
84 struct linux_file {
85 struct file *_file;
86 const struct file_operations *f_op;
87 void *private_data;
88 int f_flags;
89 int f_mode; /* Just starting mode. */
90 struct dentry *f_dentry;
91 struct dentry f_dentry_store;
92 struct selinfo f_selinfo;
93 struct sigio *f_sigio;
94 struct vnode *f_vnode;
95 #define f_inode f_vnode
96 volatile u_int f_count;
97
98 /* anonymous shmem object */
99 vm_object_t f_shmem;
100
101 /* kqfilter support */
102 int f_kqflags;
103 #define LINUX_KQ_FLAG_HAS_READ (1 << 0)
104 #define LINUX_KQ_FLAG_HAS_WRITE (1 << 1)
105 #define LINUX_KQ_FLAG_NEED_READ (1 << 2)
106 #define LINUX_KQ_FLAG_NEED_WRITE (1 << 3)
107 /* protects f_selinfo.si_note */
108 spinlock_t f_kqlock;
109 struct linux_file_wait_queue f_wait_queue;
110
111 /* pointer to associated character device, if any */
112 struct linux_cdev *f_cdev;
113
114 struct rcu_head rcu;
115 };
116
117 #define file linux_file
118 #define fasync_struct sigio *
119
120 #define fasync_helper(fd, filp, on, queue) \
121 ({ \
122 if ((on)) \
123 *(queue) = &(filp)->f_sigio; \
124 else \
125 *(queue) = NULL; \
126 0; \
127 })
128
129 #define kill_fasync(queue, sig, pollstat) \
130 do { \
131 if (*(queue) != NULL) \
132 pgsigio(*(queue), (sig), 0); \
133 } while (0)
134
135 typedef int (*filldir_t)(void *, const char *, int, off_t, u64, unsigned);
136
137 typedef unsigned int fop_flags_t;
138
139 struct file_operations {
140 struct module *owner;
141 fop_flags_t fop_flags; /* Unused on FreeBSD. */
142 ssize_t (*read)(struct linux_file *, char __user *, size_t, off_t *);
143 ssize_t (*write)(struct linux_file *, const char __user *, size_t, off_t *);
144 unsigned int (*poll) (struct linux_file *, struct poll_table_struct *);
145 long (*unlocked_ioctl)(struct linux_file *, unsigned int, unsigned long);
146 long (*compat_ioctl)(struct linux_file *, unsigned int, unsigned long);
147 int (*mmap)(struct linux_file *, struct vm_area_struct *);
148 int (*open)(struct inode *, struct file *);
149 int (*release)(struct inode *, struct linux_file *);
150 int (*fasync)(int, struct linux_file *, int);
151
152 /* Although not supported in FreeBSD, to align with Linux code
153 * we are adding llseek() only when it is mapped to no_llseek which returns
154 * an illegal seek error
155 */
156 off_t (*llseek)(struct linux_file *, off_t, int);
157 /*
158 * Not supported in FreeBSD. That's ok, we never call it and it allows some
159 * drivers like DRM drivers to compile without changes.
160 */
161 void (*show_fdinfo)(struct seq_file *, struct file *);
162 #if 0
163 /* We do not support these methods. Don't permit them to compile. */
164 loff_t (*llseek)(struct file *, loff_t, int);
165 ssize_t (*aio_read)(struct kiocb *, const struct iovec *,
166 unsigned long, loff_t);
167 ssize_t (*aio_write)(struct kiocb *, const struct iovec *,
168 unsigned long, loff_t);
169 int (*readdir)(struct file *, void *, filldir_t);
170 int (*ioctl)(struct inode *, struct file *, unsigned int,
171 unsigned long);
172 int (*flush)(struct file *, fl_owner_t id);
173 int (*fsync)(struct file *, struct dentry *, int datasync);
174 int (*aio_fsync)(struct kiocb *, int datasync);
175 int (*lock)(struct file *, int, struct file_lock *);
176 ssize_t (*sendpage)(struct file *, struct page *, int, size_t,
177 loff_t *, int);
178 unsigned long (*get_unmapped_area)(struct file *, unsigned long,
179 unsigned long, unsigned long, unsigned long);
180 int (*check_flags)(int);
181 int (*flock)(struct file *, int, struct file_lock *);
182 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
183 loff_t *, size_t, unsigned int);
184 ssize_t (*splice_read)(struct file *, loff_t *,
185 struct pipe_inode_info *, size_t, unsigned int);
186 int (*setlease)(struct file *, long, struct file_lock **);
187 #endif
188 };
189
190 #define FOP_BUFFER_RASYNC (1 << 0)
191 #define FOP_BUFFER_WASYNC (1 << 1)
192 #define FOP_MMAP_SYNC (1 << 2)
193 #define FOP_DIO_PARALLEL_WRITE (1 << 3)
194 #define FOP_HUGE_PAGES (1 << 4)
195 #define FOP_UNSIGNED_OFFSET (1 << 5)
196
197 #define fops_get(fops) (fops)
198 #define replace_fops(f, fops) ((f)->f_op = (fops))
199
200 #define FMODE_READ FREAD
201 #define FMODE_WRITE FWRITE
202 #define FMODE_EXEC FEXEC
203 #define FMODE_UNSIGNED_OFFSET 0x2000
204 int __register_chrdev(unsigned int major, unsigned int baseminor,
205 unsigned int count, const char *name,
206 const struct file_operations *fops);
207 int __register_chrdev_p(unsigned int major, unsigned int baseminor,
208 unsigned int count, const char *name,
209 const struct file_operations *fops, uid_t uid,
210 gid_t gid, int mode);
211 void __unregister_chrdev(unsigned int major, unsigned int baseminor,
212 unsigned int count, const char *name);
213
214 static inline void
unregister_chrdev(unsigned int major,const char * name)215 unregister_chrdev(unsigned int major, const char *name)
216 {
217
218 __unregister_chrdev(major, 0, 256, name);
219 }
220
221 static inline int
register_chrdev(unsigned int major,const char * name,const struct file_operations * fops)222 register_chrdev(unsigned int major, const char *name,
223 const struct file_operations *fops)
224 {
225
226 return (__register_chrdev(major, 0, 256, name, fops));
227 }
228
229 static inline int
register_chrdev_p(unsigned int major,const char * name,const struct file_operations * fops,uid_t uid,gid_t gid,int mode)230 register_chrdev_p(unsigned int major, const char *name,
231 const struct file_operations *fops, uid_t uid, gid_t gid, int mode)
232 {
233
234 return (__register_chrdev_p(major, 0, 256, name, fops, uid, gid, mode));
235 }
236
237 static inline int
register_chrdev_region(dev_t dev,unsigned range,const char * name)238 register_chrdev_region(dev_t dev, unsigned range, const char *name)
239 {
240
241 return 0;
242 }
243
244 static inline void
unregister_chrdev_region(dev_t dev,unsigned range)245 unregister_chrdev_region(dev_t dev, unsigned range)
246 {
247
248 return;
249 }
250
251 static inline int
alloc_chrdev_region(dev_t * dev,unsigned baseminor,unsigned count,const char * name)252 alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
253 const char *name)
254 {
255
256 return 0;
257 }
258
259 /* No current support for seek op in FreeBSD */
260 static inline int
nonseekable_open(struct inode * inode,struct file * filp)261 nonseekable_open(struct inode *inode, struct file *filp)
262 {
263 return 0;
264 }
265
266 static inline int
simple_open(struct inode * inode,struct file * filp)267 simple_open(struct inode *inode, struct file *filp)
268 {
269 filp->private_data = inode->i_private;
270 return 0;
271 }
272
273 extern unsigned int linux_iminor(struct inode *);
274 #define iminor(...) linux_iminor(__VA_ARGS__)
275
276 static inline struct linux_file *
get_file(struct linux_file * f)277 get_file(struct linux_file *f)
278 {
279
280 refcount_acquire(f->_file == NULL ? &f->f_count : &f->_file->f_count);
281 return (f);
282 }
283
284 struct linux_file * linux_get_file_rcu(struct linux_file **f);
285 struct linux_file * get_file_active(struct linux_file **f);
286 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION < 60700
287 static inline bool
get_file_rcu(struct linux_file * f)288 get_file_rcu(struct linux_file *f)
289 {
290 return (refcount_acquire_if_not_zero(
291 f->_file == NULL ? &f->f_count : &f->_file->f_count));
292 }
293 #else
294 #define get_file_rcu(f) linux_get_file_rcu(f)
295 #endif
296
297 static inline struct inode *
igrab(struct inode * inode)298 igrab(struct inode *inode)
299 {
300 int error;
301
302 error = vget(inode, 0);
303 if (error)
304 return (NULL);
305
306 return (inode);
307 }
308
309 static inline void
iput(struct inode * inode)310 iput(struct inode *inode)
311 {
312
313 vrele(inode);
314 }
315
316 static inline loff_t
no_llseek(struct file * file,loff_t offset,int whence)317 no_llseek(struct file *file, loff_t offset, int whence)
318 {
319
320 return (-ESPIPE);
321 }
322
323 static inline loff_t
default_llseek(struct file * file,loff_t offset,int whence)324 default_llseek(struct file *file, loff_t offset, int whence)
325 {
326 return (no_llseek(file, offset, whence));
327 }
328
329 static inline loff_t
generic_file_llseek(struct file * file,loff_t offset,int whence)330 generic_file_llseek(struct file *file, loff_t offset, int whence)
331 {
332 return (no_llseek(file, offset, whence));
333 }
334
335 static inline loff_t
noop_llseek(struct linux_file * file,loff_t offset,int whence)336 noop_llseek(struct linux_file *file, loff_t offset, int whence)
337 {
338
339 return (file->_file->f_offset);
340 }
341
342 static inline struct vnode *
file_inode(const struct linux_file * file)343 file_inode(const struct linux_file *file)
344 {
345
346 return (file->f_vnode);
347 }
348
349 static inline int
call_mmap(struct linux_file * file,struct vm_area_struct * vma)350 call_mmap(struct linux_file *file, struct vm_area_struct *vma)
351 {
352
353 return (file->f_op->mmap(file, vma));
354 }
355
356 static inline void
i_size_write(struct inode * inode,loff_t i_size)357 i_size_write(struct inode *inode, loff_t i_size)
358 {
359 }
360
361 /*
362 * simple_read_from_buffer: copy data from kernel-space origin
363 * buffer into user-space destination buffer
364 *
365 * @dest: destination buffer
366 * @read_size: number of bytes to be transferred
367 * @ppos: starting transfer position pointer
368 * @orig: origin buffer
369 * @buf_size: size of destination and origin buffers
370 *
371 * Return value:
372 * On success, total bytes copied with *ppos incremented accordingly.
373 * On failure, negative value.
374 */
375 static inline ssize_t
simple_read_from_buffer(void __user * dest,size_t read_size,loff_t * ppos,void * orig,size_t buf_size)376 simple_read_from_buffer(void __user *dest, size_t read_size, loff_t *ppos,
377 void *orig, size_t buf_size)
378 {
379 void *read_pos = ((char *) orig) + *ppos;
380 size_t buf_remain = buf_size - *ppos;
381 ssize_t num_read;
382
383 if (*ppos >= buf_size || read_size == 0)
384 return (0);
385
386 if (read_size > buf_remain)
387 read_size = buf_remain;
388
389 /* copy_to_user returns number of bytes NOT read */
390 num_read = read_size - copy_to_user(dest, read_pos, read_size);
391 if (num_read == 0)
392 return -EFAULT;
393 *ppos += num_read;
394
395 return (num_read);
396 }
397
398 MALLOC_DECLARE(M_LSATTR);
399
400 #define __DEFINE_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt, __wrfunc)\
401 static inline int \
402 __fops ## _open(struct inode *inode, struct file *filp) \
403 { \
404 return (simple_attr_open(inode, filp, __get, __set, __fmt)); \
405 } \
406 static const struct file_operations __fops = { \
407 .owner = THIS_MODULE, \
408 .open = __fops ## _open, \
409 .release = simple_attr_release, \
410 .read = simple_attr_read, \
411 .write = __wrfunc, \
412 .llseek = no_llseek \
413 }
414
415 #define DEFINE_SIMPLE_ATTRIBUTE(fops, get, set, fmt) \
416 __DEFINE_SIMPLE_ATTRIBUTE(fops, get, set, fmt, simple_attr_write)
417 #define DEFINE_SIMPLE_ATTRIBUTE_SIGNED(fops, get, set, fmt) \
418 __DEFINE_SIMPLE_ATTRIBUTE(fops, get, set, fmt, simple_attr_write_signed)
419
420 int simple_attr_open(struct inode *inode, struct file *filp,
421 int (*get)(void *, uint64_t *), int (*set)(void *, uint64_t),
422 const char *fmt);
423
424 int simple_attr_release(struct inode *inode, struct file *filp);
425
426 ssize_t simple_attr_read(struct file *filp, char __user *buf, size_t read_size,
427 loff_t *ppos);
428
429 ssize_t simple_attr_write(struct file *filp, const char __user *buf,
430 size_t write_size, loff_t *ppos);
431
432 ssize_t simple_attr_write_signed(struct file *filp, const char __user *buf,
433 size_t write_size, loff_t *ppos);
434
435 #endif /* _LINUXKPI_LINUX_FS_H_ */
436