Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Wrapper functions for accessing the file_struct fd array. |
| 3 | */ |
| 4 | |
| 5 | #ifndef __LINUX_FILE_H |
| 6 | #define __LINUX_FILE_H |
| 7 | |
| 8 | #include <asm/atomic.h> |
| 9 | #include <linux/posix_types.h> |
| 10 | #include <linux/compiler.h> |
| 11 | #include <linux/spinlock.h> |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 12 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
| 14 | /* |
| 15 | * The default fd array needs to be at least BITS_PER_LONG, |
| 16 | * as this is the granularity returned by copy_fdset(). |
| 17 | */ |
| 18 | #define NR_OPEN_DEFAULT BITS_PER_LONG |
| 19 | |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 20 | struct fdtable { |
| 21 | unsigned int max_fds; |
| 22 | int max_fdset; |
| 23 | int next_fd; |
| 24 | struct file ** fd; /* current fd array */ |
| 25 | fd_set *close_on_exec; |
| 26 | fd_set *open_fds; |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 27 | struct rcu_head rcu; |
| 28 | struct files_struct *free_files; |
| 29 | struct fdtable *next; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | /* |
| 33 | * Open file table structure |
| 34 | */ |
| 35 | struct files_struct { |
| 36 | atomic_t count; |
| 37 | spinlock_t file_lock; /* Protects all the below members. Nests inside tsk->alloc_lock */ |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 38 | struct fdtable *fdt; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 39 | struct fdtable fdtab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | fd_set close_on_exec_init; |
| 41 | fd_set open_fds_init; |
| 42 | struct file * fd_array[NR_OPEN_DEFAULT]; |
| 43 | }; |
| 44 | |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 45 | #define files_fdtable(files) (rcu_dereference((files)->fdt)) |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | extern void FASTCALL(__fput(struct file *)); |
| 48 | extern void FASTCALL(fput(struct file *)); |
| 49 | |
| 50 | static inline void fput_light(struct file *file, int fput_needed) |
| 51 | { |
| 52 | if (unlikely(fput_needed)) |
| 53 | fput(file); |
| 54 | } |
| 55 | |
| 56 | extern struct file * FASTCALL(fget(unsigned int fd)); |
| 57 | extern struct file * FASTCALL(fget_light(unsigned int fd, int *fput_needed)); |
| 58 | extern void FASTCALL(set_close_on_exec(unsigned int fd, int flag)); |
| 59 | extern void put_filp(struct file *); |
| 60 | extern int get_unused_fd(void); |
| 61 | extern void FASTCALL(put_unused_fd(unsigned int fd)); |
Pekka J Enberg | 2109a2d | 2005-11-07 00:58:01 -0800 | [diff] [blame] | 62 | struct kmem_cache; |
| 63 | extern void filp_ctor(void * objp, struct kmem_cache *cachep, unsigned long cflags); |
| 64 | extern void filp_dtor(void * objp, struct kmem_cache *cachep, unsigned long dflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | extern struct file ** alloc_fd_array(int); |
| 67 | extern void free_fd_array(struct file **, int); |
| 68 | |
| 69 | extern fd_set *alloc_fdset(int); |
| 70 | extern void free_fdset(fd_set *, int); |
| 71 | |
| 72 | extern int expand_files(struct files_struct *, int nr); |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 73 | extern void free_fdtable(struct fdtable *fdt); |
| 74 | extern void __init files_defer_init(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd) |
| 77 | { |
| 78 | struct file * file = NULL; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 79 | struct fdtable *fdt = files_fdtable(files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 81 | if (fd < fdt->max_fds) |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 82 | file = rcu_dereference(fdt->fd[fd]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | return file; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Check whether the specified fd has an open file. |
| 88 | */ |
| 89 | #define fcheck(fd) fcheck_files(current->files, fd) |
| 90 | |
| 91 | extern void FASTCALL(fd_install(unsigned int fd, struct file * file)); |
| 92 | |
| 93 | struct task_struct; |
| 94 | |
| 95 | struct files_struct *get_files_struct(struct task_struct *); |
| 96 | void FASTCALL(put_files_struct(struct files_struct *fs)); |
| 97 | |
| 98 | #endif /* __LINUX_FILE_H */ |