[PATCH] DVB: Update documentation and credits
[linux-2.6/history.git] / fs / readdir.c
blobf9cd0419bf5e89a674f9ef8287c73f35e094def6
1 /*
2 * linux/fs/readdir.c
4 * Copyright (C) 1995 Linus Torvalds
5 */
7 #include <linux/module.h>
8 #include <linux/time.h>
9 #include <linux/mm.h>
10 #include <linux/errno.h>
11 #include <linux/stat.h>
12 #include <linux/file.h>
13 #include <linux/smp_lock.h>
14 #include <linux/fs.h>
15 #include <linux/dirent.h>
16 #include <linux/security.h>
18 #include <asm/uaccess.h>
20 int vfs_readdir(struct file *file, filldir_t filler, void *buf)
22 struct inode *inode = file->f_dentry->d_inode;
23 int res = -ENOTDIR;
24 if (!file->f_op || !file->f_op->readdir)
25 goto out;
27 res = security_file_permission(file, MAY_READ);
28 if (res)
29 goto out;
31 down(&inode->i_sem);
32 res = -ENOENT;
33 if (!IS_DEADDIR(inode)) {
34 res = file->f_op->readdir(file, buf, filler);
36 up(&inode->i_sem);
37 out:
38 return res;
41 EXPORT_SYMBOL(vfs_readdir);
44 * Traditional linux readdir() handling..
46 * "count=1" is a special case, meaning that the buffer is one
47 * dirent-structure in size and that the code can't handle more
48 * anyway. Thus the special "fillonedir()" function for that
49 * case (the low-level handlers don't need to care about this).
51 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char *) (de)))
52 #define ROUND_UP(x) (((x)+sizeof(long)-1) & ~(sizeof(long)-1))
54 #ifndef __ia64__
56 struct old_linux_dirent {
57 unsigned long d_ino;
58 unsigned long d_offset;
59 unsigned short d_namlen;
60 char d_name[1];
63 struct readdir_callback {
64 struct old_linux_dirent __user * dirent;
65 int result;
68 static int fillonedir(void * __buf, const char * name, int namlen, loff_t offset,
69 ino_t ino, unsigned int d_type)
71 struct readdir_callback * buf = (struct readdir_callback *) __buf;
72 struct old_linux_dirent __user * dirent;
74 if (buf->result)
75 return -EINVAL;
76 buf->result++;
77 dirent = buf->dirent;
78 if (!access_ok(VERIFY_WRITE, (unsigned long)dirent,
79 (unsigned long)(dirent->d_name + namlen + 1) -
80 (unsigned long)dirent))
81 goto efault;
82 if ( __put_user(ino, &dirent->d_ino) ||
83 __put_user(offset, &dirent->d_offset) ||
84 __put_user(namlen, &dirent->d_namlen) ||
85 __copy_to_user(dirent->d_name, name, namlen) ||
86 __put_user(0, dirent->d_name + namlen))
87 goto efault;
88 return 0;
89 efault:
90 buf->result = -EFAULT;
91 return -EFAULT;
94 asmlinkage long old_readdir(unsigned int fd, struct old_linux_dirent __user * dirent, unsigned int count)
96 int error;
97 struct file * file;
98 struct readdir_callback buf;
100 error = -EBADF;
101 file = fget(fd);
102 if (!file)
103 goto out;
105 buf.result = 0;
106 buf.dirent = dirent;
108 error = vfs_readdir(file, fillonedir, &buf);
109 if (error >= 0)
110 error = buf.result;
112 fput(file);
113 out:
114 return error;
117 #endif /* !__ia64__ */
120 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
121 * interface.
123 struct linux_dirent {
124 unsigned long d_ino;
125 unsigned long d_off;
126 unsigned short d_reclen;
127 char d_name[1];
130 struct getdents_callback {
131 struct linux_dirent __user * current_dir;
132 struct linux_dirent __user * previous;
133 int count;
134 int error;
137 static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
138 ino_t ino, unsigned int d_type)
140 struct linux_dirent __user * dirent;
141 struct getdents_callback * buf = (struct getdents_callback *) __buf;
142 int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 1);
144 buf->error = -EINVAL; /* only used if we fail.. */
145 if (reclen > buf->count)
146 return -EINVAL;
147 dirent = buf->previous;
148 if (dirent) {
149 if (__put_user(offset, &dirent->d_off))
150 goto efault;
152 dirent = buf->current_dir;
153 if (__put_user(ino, &dirent->d_ino))
154 goto efault;
155 if (__put_user(reclen, &dirent->d_reclen))
156 goto efault;
157 if (copy_to_user(dirent->d_name, name, namlen))
158 goto efault;
159 if (__put_user(0, dirent->d_name + namlen))
160 goto efault;
161 buf->previous = dirent;
162 ((char *) dirent) += reclen;
163 buf->current_dir = dirent;
164 buf->count -= reclen;
165 return 0;
166 efault:
167 buf->error = -EFAULT;
168 return -EFAULT;
171 asmlinkage long sys_getdents(unsigned int fd, struct linux_dirent __user * dirent, unsigned int count)
173 struct file * file;
174 struct linux_dirent __user * lastdirent;
175 struct getdents_callback buf;
176 int error;
178 error = -EFAULT;
179 if (!access_ok(VERIFY_WRITE, dirent, count))
180 goto out;
182 error = -EBADF;
183 file = fget(fd);
184 if (!file)
185 goto out;
187 buf.current_dir = dirent;
188 buf.previous = NULL;
189 buf.count = count;
190 buf.error = 0;
192 error = vfs_readdir(file, filldir, &buf);
193 if (error < 0)
194 goto out_putf;
195 error = buf.error;
196 lastdirent = buf.previous;
197 if (lastdirent) {
198 if (put_user(file->f_pos, &lastdirent->d_off))
199 error = -EFAULT;
200 else
201 error = count - buf.count;
204 out_putf:
205 fput(file);
206 out:
207 return error;
210 #define ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
212 struct getdents_callback64 {
213 struct linux_dirent64 __user * current_dir;
214 struct linux_dirent64 __user * previous;
215 int count;
216 int error;
219 static int filldir64(void * __buf, const char * name, int namlen, loff_t offset,
220 ino_t ino, unsigned int d_type)
222 struct linux_dirent64 __user *dirent;
223 struct getdents_callback64 * buf = (struct getdents_callback64 *) __buf;
224 int reclen = ROUND_UP64(NAME_OFFSET(dirent) + namlen + 1);
226 buf->error = -EINVAL; /* only used if we fail.. */
227 if (reclen > buf->count)
228 return -EINVAL;
229 dirent = buf->previous;
230 if (dirent) {
231 if (__put_user(offset, &dirent->d_off))
232 goto efault;
234 dirent = buf->current_dir;
235 if (__put_user(ino, &dirent->d_ino))
236 goto efault;
237 if (__put_user(0, &dirent->d_off))
238 goto efault;
239 if (__put_user(reclen, &dirent->d_reclen))
240 goto efault;
241 if (__put_user(d_type, &dirent->d_type))
242 goto efault;
243 if (copy_to_user(dirent->d_name, name, namlen))
244 goto efault;
245 if (__put_user(0, dirent->d_name + namlen))
246 goto efault;
247 buf->previous = dirent;
248 ((char *) dirent) += reclen;
249 buf->current_dir = dirent;
250 buf->count -= reclen;
251 return 0;
252 efault:
253 buf->error = -EFAULT;
254 return -EFAULT;
257 asmlinkage long sys_getdents64(unsigned int fd, struct linux_dirent64 __user * dirent, unsigned int count)
259 struct file * file;
260 struct linux_dirent64 __user * lastdirent;
261 struct getdents_callback64 buf;
262 int error;
264 error = -EFAULT;
265 if (!access_ok(VERIFY_WRITE, dirent, count))
266 goto out;
268 error = -EBADF;
269 file = fget(fd);
270 if (!file)
271 goto out;
273 buf.current_dir = dirent;
274 buf.previous = NULL;
275 buf.count = count;
276 buf.error = 0;
278 error = vfs_readdir(file, filldir64, &buf);
279 if (error < 0)
280 goto out_putf;
281 error = buf.error;
282 lastdirent = buf.previous;
283 if (lastdirent) {
284 typeof(lastdirent->d_off) d_off = file->f_pos;
285 __put_user(d_off, &lastdirent->d_off);
286 error = count - buf.count;
289 out_putf:
290 fput(file);
291 out:
292 return error;