blob: db372230848ed54aacc7841bd4ce289e464c9686 [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>
12
13/*
14 * The default fd array needs to be at least BITS_PER_LONG,
15 * as this is the granularity returned by copy_fdset().
16 */
17#define NR_OPEN_DEFAULT BITS_PER_LONG
18
Dipankar Sarmabadf1662005-09-09 13:04:10 -070019struct fdtable {
20 unsigned int max_fds;
21 int max_fdset;
22 int next_fd;
23 struct file ** fd; /* current fd array */
24 fd_set *close_on_exec;
25 fd_set *open_fds;
26};
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/*
29 * Open file table structure
30 */
31struct files_struct {
32 atomic_t count;
33 spinlock_t file_lock; /* Protects all the below members. Nests inside tsk->alloc_lock */
Dipankar Sarmabadf1662005-09-09 13:04:10 -070034 struct fdtable fdtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 fd_set close_on_exec_init;
36 fd_set open_fds_init;
37 struct file * fd_array[NR_OPEN_DEFAULT];
38};
39
Dipankar Sarmabadf1662005-09-09 13:04:10 -070040#define files_fdtable(files) (&(files)->fdtab)
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042extern void FASTCALL(__fput(struct file *));
43extern void FASTCALL(fput(struct file *));
44
45static inline void fput_light(struct file *file, int fput_needed)
46{
47 if (unlikely(fput_needed))
48 fput(file);
49}
50
51extern struct file * FASTCALL(fget(unsigned int fd));
52extern struct file * FASTCALL(fget_light(unsigned int fd, int *fput_needed));
53extern void FASTCALL(set_close_on_exec(unsigned int fd, int flag));
54extern void put_filp(struct file *);
55extern int get_unused_fd(void);
56extern void FASTCALL(put_unused_fd(unsigned int fd));
57struct kmem_cache_s;
58extern void filp_ctor(void * objp, struct kmem_cache_s *cachep, unsigned long cflags);
59extern void filp_dtor(void * objp, struct kmem_cache_s *cachep, unsigned long dflags);
60
61extern struct file ** alloc_fd_array(int);
62extern void free_fd_array(struct file **, int);
63
64extern fd_set *alloc_fdset(int);
65extern void free_fdset(fd_set *, int);
66
67extern int expand_files(struct files_struct *, int nr);
68
69static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd)
70{
71 struct file * file = NULL;
Dipankar Sarmabadf1662005-09-09 13:04:10 -070072 struct fdtable *fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Dipankar Sarmabadf1662005-09-09 13:04:10 -070074 if (fd < fdt->max_fds)
75 file = fdt->fd[fd];
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return file;
77}
78
79/*
80 * Check whether the specified fd has an open file.
81 */
82#define fcheck(fd) fcheck_files(current->files, fd)
83
84extern void FASTCALL(fd_install(unsigned int fd, struct file * file));
85
86struct task_struct;
87
88struct files_struct *get_files_struct(struct task_struct *);
89void FASTCALL(put_files_struct(struct files_struct *fs));
90
91#endif /* __LINUX_FILE_H */