2 * Wrapper functions for accessing the file_struct fd array.
8 extern void _fput(struct file
*);
10 static inline struct file
* fcheck_files(struct files_struct
*files
, unsigned int fd
)
12 struct file
* file
= NULL
;
14 if (fd
< files
->max_fds
)
20 * Check whether the specified fd has an open file.
22 static inline struct file
* fcheck(unsigned int fd
)
24 struct file
* file
= NULL
;
25 struct files_struct
*files
= current
->files
;
27 if (fd
< files
->max_fds
)
32 static inline struct file
* frip(struct files_struct
*files
, unsigned int fd
)
34 struct file
* file
= NULL
;
36 if (fd
< files
->max_fds
)
37 file
= xchg(&files
->fd
[fd
], NULL
);
41 static inline struct file
* fget(unsigned int fd
)
43 struct file
* file
= NULL
;
44 struct files_struct
*files
= current
->files
;
46 read_lock(&files
->file_lock
);
50 read_unlock(&files
->file_lock
);
55 * 23/12/1998 Marcin Dalecki <dalecki@cs.net.pl>:
57 * Since those functions where calling other functions, it was completely
58 * bogus to make them all "extern inline".
60 * The removal of this pseudo optimization saved me scandalous:
64 * precious bytes from my kernel, even without counting all the code compiled
67 * I suspect there are many other similar "optimizations" across the
70 static inline void fput(struct file
* file
)
72 if (atomic_dec_and_test(&file
->f_count
))
75 extern void put_filp(struct file
*);
77 extern int get_unused_fd(void);
79 static inline void __put_unused_fd(struct files_struct
*files
, unsigned int fd
)
81 FD_CLR(fd
, files
->open_fds
);
82 if (fd
< files
->next_fd
)
86 static inline void put_unused_fd(unsigned int fd
)
88 struct files_struct
*files
= current
->files
;
90 write_lock(&files
->file_lock
);
91 __put_unused_fd(files
, fd
);
92 write_unlock(&files
->file_lock
);
96 * Install a file pointer in the fd array.
98 * The VFS is full of places where we drop the files lock between
99 * setting the open_fds bitmap and installing the file in the file
100 * array. At any such point, we are vulnerable to a dup2() race
101 * installing a file in the array before us. We need to detect this and
102 * fput() the struct file we are about to overwrite in this case.
105 static inline void fd_install(unsigned int fd
, struct file
* file
)
107 struct files_struct
*files
= current
->files
;
108 struct file
* result
;
110 write_lock(&files
->file_lock
);
111 result
= xchg(&files
->fd
[fd
], file
);
112 write_unlock(&files
->file_lock
);
117 void put_files_struct(struct files_struct
*fs
);
119 #endif /* __LINUX_FILE_H */