blob: 9901b850f2e466158556711303829d6d114686ce [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Sarmaab2af1f2005-09-09 13:04:13 -070012#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
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 Sarmabadf1662005-09-09 13:04:10 -070020struct 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 Sarmaab2af1f2005-09-09 13:04:13 -070027 struct rcu_head rcu;
28 struct files_struct *free_files;
29 struct fdtable *next;
Dipankar Sarmabadf1662005-09-09 13:04:10 -070030};
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/*
33 * Open file table structure
34 */
35struct files_struct {
Eric Dumazet95e861d2005-11-13 16:06:24 -080036 atomic_t count;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070037 struct fdtable *fdt;
Dipankar Sarmabadf1662005-09-09 13:04:10 -070038 struct fdtable fdtab;
Eric Dumazet95e861d2005-11-13 16:06:24 -080039 fd_set close_on_exec_init;
40 fd_set open_fds_init;
41 struct file * fd_array[NR_OPEN_DEFAULT];
42 spinlock_t file_lock; /* Protects concurrent writers. Nests inside tsk->alloc_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043};
44
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070045#define files_fdtable(files) (rcu_dereference((files)->fdt))
Dipankar Sarmabadf1662005-09-09 13:04:10 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047extern void FASTCALL(__fput(struct file *));
48extern void FASTCALL(fput(struct file *));
49
50static inline void fput_light(struct file *file, int fput_needed)
51{
52 if (unlikely(fput_needed))
53 fput(file);
54}
55
56extern struct file * FASTCALL(fget(unsigned int fd));
57extern struct file * FASTCALL(fget_light(unsigned int fd, int *fput_needed));
58extern void FASTCALL(set_close_on_exec(unsigned int fd, int flag));
59extern void put_filp(struct file *);
60extern int get_unused_fd(void);
61extern void FASTCALL(put_unused_fd(unsigned int fd));
Pekka J Enberg2109a2d2005-11-07 00:58:01 -080062struct kmem_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64extern struct file ** alloc_fd_array(int);
65extern void free_fd_array(struct file **, int);
66
67extern fd_set *alloc_fdset(int);
68extern void free_fdset(fd_set *, int);
69
70extern int expand_files(struct files_struct *, int nr);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070071extern void free_fdtable(struct fdtable *fdt);
72extern void __init files_defer_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd)
75{
76 struct file * file = NULL;
Dipankar Sarmabadf1662005-09-09 13:04:10 -070077 struct fdtable *fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Dipankar Sarmabadf1662005-09-09 13:04:10 -070079 if (fd < fdt->max_fds)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070080 file = rcu_dereference(fdt->fd[fd]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return file;
82}
83
84/*
85 * Check whether the specified fd has an open file.
86 */
87#define fcheck(fd) fcheck_files(current->files, fd)
88
89extern void FASTCALL(fd_install(unsigned int fd, struct file * file));
90
91struct task_struct;
92
93struct files_struct *get_files_struct(struct task_struct *);
94void FASTCALL(put_files_struct(struct files_struct *fs));
95
96#endif /* __LINUX_FILE_H */