Import 2.4.0-test2pre7
[davej-history.git] / include / linux / file.h
blob268fd27a18b44c72ea1814ed6054556f3f26fd59
1 /*
2 * Wrapper functions for accessing the file_struct fd array.
3 */
5 #ifndef __LINUX_FILE_H
6 #define __LINUX_FILE_H
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)
15 file = files->fd[fd];
16 return file;
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)
28 file = files->fd[fd];
29 return file;
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);
38 return file;
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);
47 file = fcheck(fd);
48 if (file)
49 get_file(file);
50 read_unlock(&files->file_lock);
51 return file;
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:
62 * 3756 (i386 arch)
64 * precious bytes from my kernel, even without counting all the code compiled
65 * as module!
67 * I suspect there are many other similar "optimizations" across the
68 * kernel...
70 static inline void fput(struct file * file)
72 if (atomic_dec_and_test(&file->f_count))
73 _fput(file);
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)
83 files->next_fd = 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);
113 if (result)
114 fput(result);
117 void put_files_struct(struct files_struct *fs);
119 #endif /* __LINUX_FILE_H */