[PATCH] DVB: Documentation and Kconfig updazes
[linux-2.6/history.git] / fs / readdir.c
blobf02cae6b6067f9043f4f52d33ae5b9c8171b27b2
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);
35 file_accessed(file);
37 up(&inode->i_sem);
38 out:
39 return res;
42 EXPORT_SYMBOL(vfs_readdir);
45 * Traditional linux readdir() handling..
47 * "count=1" is a special case, meaning that the buffer is one
48 * dirent-structure in size and that the code can't handle more
49 * anyway. Thus the special "fillonedir()" function for that
50 * case (the low-level handlers don't need to care about this).
52 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char *) (de)))
53 #define ROUND_UP(x) (((x)+sizeof(long)-1) & ~(sizeof(long)-1))
55 #ifndef __ia64__
57 struct old_linux_dirent {
58 unsigned long d_ino;
59 unsigned long d_offset;
60 unsigned short d_namlen;
61 char d_name[1];
64 struct readdir_callback {
65 struct old_linux_dirent __user * dirent;
66 int result;
69 static int fillonedir(void * __buf, const char * name, int namlen, loff_t offset,
70 ino_t ino, unsigned int d_type)
72 struct readdir_callback * buf = (struct readdir_callback *) __buf;
73 struct old_linux_dirent __user * dirent;
75 if (buf->result)
76 return -EINVAL;
77 buf->result++;
78 dirent = buf->dirent;
79 if (!access_ok(VERIFY_WRITE, (unsigned long)dirent,
80 (unsigned long)(dirent->d_name + namlen + 1) -
81 (unsigned long)dirent))
82 goto efault;
83 if ( __put_user(ino, &dirent->d_ino) ||
84 __put_user(offset, &dirent->d_offset) ||
85 __put_user(namlen, &dirent->d_namlen) ||
86 __copy_to_user(dirent->d_name, name, namlen) ||
87 __put_user(0, dirent->d_name + namlen))
88 goto efault;
89 return 0;
90 efault:
91 buf->result = -EFAULT;
92 return -EFAULT;
95 asmlinkage long old_readdir(unsigned int fd, struct old_linux_dirent __user * dirent, unsigned int count)
97 int error;
98 struct file * file;
99 struct readdir_callback buf;
101 error = -EBADF;
102 file = fget(fd);
103 if (!file)
104 goto out;
106 buf.result = 0;
107 buf.dirent = dirent;
109 error = vfs_readdir(file, fillonedir, &buf);
110 if (error >= 0)
111 error = buf.result;
113 fput(file);
114 out:
115 return error;
118 #endif /* !__ia64__ */
121 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
122 * interface.
124 struct linux_dirent {
125 unsigned long d_ino;
126 unsigned long d_off;
127 unsigned short d_reclen;
128 char d_name[1];
131 struct getdents_callback {
132 struct linux_dirent __user * current_dir;
133 struct linux_dirent __user * previous;
134 int count;
135 int error;
138 static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
139 ino_t ino, unsigned int d_type)
141 struct linux_dirent __user * dirent;
142 struct getdents_callback * buf = (struct getdents_callback *) __buf;
143 int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 2);
145 buf->error = -EINVAL; /* only used if we fail.. */
146 if (reclen > buf->count)
147 return -EINVAL;
148 dirent = buf->previous;
149 if (dirent) {
150 if (__put_user(offset, &dirent->d_off))
151 goto efault;
153 dirent = buf->current_dir;
154 if (__put_user(ino, &dirent->d_ino))
155 goto efault;
156 if (__put_user(reclen, &dirent->d_reclen))
157 goto efault;
158 if (copy_to_user(dirent->d_name, name, namlen))
159 goto efault;
160 if (__put_user(0, dirent->d_name + namlen))
161 goto efault;
162 if (__put_user(d_type, (char *) dirent + reclen - 1))
163 goto efault;
164 buf->previous = dirent;
165 dirent = (void __user *)dirent + reclen;
166 buf->current_dir = dirent;
167 buf->count -= reclen;
168 return 0;
169 efault:
170 buf->error = -EFAULT;
171 return -EFAULT;
174 asmlinkage long sys_getdents(unsigned int fd, struct linux_dirent __user * dirent, unsigned int count)
176 struct file * file;
177 struct linux_dirent __user * lastdirent;
178 struct getdents_callback buf;
179 int error;
181 error = -EFAULT;
182 if (!access_ok(VERIFY_WRITE, dirent, count))
183 goto out;
185 error = -EBADF;
186 file = fget(fd);
187 if (!file)
188 goto out;
190 buf.current_dir = dirent;
191 buf.previous = NULL;
192 buf.count = count;
193 buf.error = 0;
195 error = vfs_readdir(file, filldir, &buf);
196 if (error < 0)
197 goto out_putf;
198 error = buf.error;
199 lastdirent = buf.previous;
200 if (lastdirent) {
201 if (put_user(file->f_pos, &lastdirent->d_off))
202 error = -EFAULT;
203 else
204 error = count - buf.count;
207 out_putf:
208 fput(file);
209 out:
210 return error;
213 #define ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
215 struct getdents_callback64 {
216 struct linux_dirent64 __user * current_dir;
217 struct linux_dirent64 __user * previous;
218 int count;
219 int error;
222 static int filldir64(void * __buf, const char * name, int namlen, loff_t offset,
223 ino_t ino, unsigned int d_type)
225 struct linux_dirent64 __user *dirent;
226 struct getdents_callback64 * buf = (struct getdents_callback64 *) __buf;
227 int reclen = ROUND_UP64(NAME_OFFSET(dirent) + namlen + 1);
229 buf->error = -EINVAL; /* only used if we fail.. */
230 if (reclen > buf->count)
231 return -EINVAL;
232 dirent = buf->previous;
233 if (dirent) {
234 if (__put_user(offset, &dirent->d_off))
235 goto efault;
237 dirent = buf->current_dir;
238 if (__put_user(ino, &dirent->d_ino))
239 goto efault;
240 if (__put_user(0, &dirent->d_off))
241 goto efault;
242 if (__put_user(reclen, &dirent->d_reclen))
243 goto efault;
244 if (__put_user(d_type, &dirent->d_type))
245 goto efault;
246 if (copy_to_user(dirent->d_name, name, namlen))
247 goto efault;
248 if (__put_user(0, dirent->d_name + namlen))
249 goto efault;
250 buf->previous = dirent;
251 dirent = (void __user *)dirent + reclen;
252 buf->current_dir = dirent;
253 buf->count -= reclen;
254 return 0;
255 efault:
256 buf->error = -EFAULT;
257 return -EFAULT;
260 asmlinkage long sys_getdents64(unsigned int fd, struct linux_dirent64 __user * dirent, unsigned int count)
262 struct file * file;
263 struct linux_dirent64 __user * lastdirent;
264 struct getdents_callback64 buf;
265 int error;
267 error = -EFAULT;
268 if (!access_ok(VERIFY_WRITE, dirent, count))
269 goto out;
271 error = -EBADF;
272 file = fget(fd);
273 if (!file)
274 goto out;
276 buf.current_dir = dirent;
277 buf.previous = NULL;
278 buf.count = count;
279 buf.error = 0;
281 error = vfs_readdir(file, filldir64, &buf);
282 if (error < 0)
283 goto out_putf;
284 error = buf.error;
285 lastdirent = buf.previous;
286 if (lastdirent) {
287 typeof(lastdirent->d_off) d_off = file->f_pos;
288 __put_user(d_off, &lastdirent->d_off);
289 error = count - buf.count;
292 out_putf:
293 fput(file);
294 out:
295 return error;