4 * Copyright (C) 1995 Linus Torvalds
7 #include <linux/module.h>
8 #include <linux/time.h>
10 #include <linux/errno.h>
11 #include <linux/stat.h>
12 #include <linux/file.h>
13 #include <linux/smp_lock.h>
15 #include <linux/dirent.h>
16 #include <linux/security.h>
17 #include <linux/unistd.h>
19 #include <asm/uaccess.h>
21 int vfs_readdir(struct file *file, filldir_t filler, void *buf)
23 struct inode *inode = file->f_dentry->d_inode;
25 if (!file->f_op || !file->f_op->readdir)
28 res = security_file_permission(file, MAY_READ);
34 if (!IS_DEADDIR(inode)) {
35 res = file->f_op->readdir(file, buf, filler);
43 EXPORT_SYMBOL(vfs_readdir);
46 * Traditional linux readdir() handling..
48 * "count=1" is a special case, meaning that the buffer is one
49 * dirent-structure in size and that the code can't handle more
50 * anyway. Thus the special "fillonedir()" function for that
51 * case (the low-level handlers don't need to care about this).
53 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
54 #define ROUND_UP(x) (((x)+sizeof(long)-1) & ~(sizeof(long)-1))
56 #ifdef __ARCH_WANT_OLD_READDIR
58 struct old_linux_dirent {
60 unsigned long d_offset;
61 unsigned short d_namlen;
65 struct readdir_callback {
66 struct old_linux_dirent __user * dirent;
70 static int fillonedir(void * __buf, const char * name, int namlen, loff_t offset,
71 ino_t ino, unsigned int d_type)
73 struct readdir_callback * buf = (struct readdir_callback *) __buf;
74 struct old_linux_dirent __user * dirent;
80 if (!access_ok(VERIFY_WRITE, dirent,
81 (unsigned long)(dirent->d_name + namlen + 1) -
82 (unsigned long)dirent))
84 if ( __put_user(ino, &dirent->d_ino) ||
85 __put_user(offset, &dirent->d_offset) ||
86 __put_user(namlen, &dirent->d_namlen) ||
87 __copy_to_user(dirent->d_name, name, namlen) ||
88 __put_user(0, dirent->d_name + namlen))
92 buf->result = -EFAULT;
96 asmlinkage long old_readdir(unsigned int fd, struct old_linux_dirent __user * dirent, unsigned int count)
100 struct readdir_callback buf;
110 error = vfs_readdir(file, fillonedir, &buf);
119 #endif /* __ARCH_WANT_OLD_READDIR */
122 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
125 struct linux_dirent {
128 unsigned short d_reclen;
132 struct getdents_callback {
133 struct linux_dirent __user * current_dir;
134 struct linux_dirent __user * previous;
139 static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
140 ino_t ino, unsigned int d_type)
142 struct linux_dirent __user * dirent;
143 struct getdents_callback * buf = (struct getdents_callback *) __buf;
144 int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 2);
146 buf->error = -EINVAL; /* only used if we fail.. */
147 if (reclen > buf->count)
149 dirent = buf->previous;
151 if (__put_user(offset, &dirent->d_off))
154 dirent = buf->current_dir;
155 if (__put_user(ino, &dirent->d_ino))
157 if (__put_user(reclen, &dirent->d_reclen))
159 if (copy_to_user(dirent->d_name, name, namlen))
161 if (__put_user(0, dirent->d_name + namlen))
163 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
165 buf->previous = dirent;
166 dirent = (void __user *)dirent + reclen;
167 buf->current_dir = dirent;
168 buf->count -= reclen;
171 buf->error = -EFAULT;
175 asmlinkage long sys_getdents(unsigned int fd, struct linux_dirent __user * dirent, unsigned int count)
178 struct linux_dirent __user * lastdirent;
179 struct getdents_callback buf;
183 if (!access_ok(VERIFY_WRITE, dirent, count))
191 buf.current_dir = dirent;
196 error = vfs_readdir(file, filldir, &buf);
200 lastdirent = buf.previous;
202 if (put_user(file->f_pos, &lastdirent->d_off))
205 error = count - buf.count;
214 #define ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
216 struct getdents_callback64 {
217 struct linux_dirent64 __user * current_dir;
218 struct linux_dirent64 __user * previous;
223 static int filldir64(void * __buf, const char * name, int namlen, loff_t offset,
224 ino_t ino, unsigned int d_type)
226 struct linux_dirent64 __user *dirent;
227 struct getdents_callback64 * buf = (struct getdents_callback64 *) __buf;
228 int reclen = ROUND_UP64(NAME_OFFSET(dirent) + namlen + 1);
230 buf->error = -EINVAL; /* only used if we fail.. */
231 if (reclen > buf->count)
233 dirent = buf->previous;
235 if (__put_user(offset, &dirent->d_off))
238 dirent = buf->current_dir;
239 if (__put_user(ino, &dirent->d_ino))
241 if (__put_user(0, &dirent->d_off))
243 if (__put_user(reclen, &dirent->d_reclen))
245 if (__put_user(d_type, &dirent->d_type))
247 if (copy_to_user(dirent->d_name, name, namlen))
249 if (__put_user(0, dirent->d_name + namlen))
251 buf->previous = dirent;
252 dirent = (void __user *)dirent + reclen;
253 buf->current_dir = dirent;
254 buf->count -= reclen;
257 buf->error = -EFAULT;
261 asmlinkage long sys_getdents64(unsigned int fd, struct linux_dirent64 __user * dirent, unsigned int count)
264 struct linux_dirent64 __user * lastdirent;
265 struct getdents_callback64 buf;
269 if (!access_ok(VERIFY_WRITE, dirent, count))
277 buf.current_dir = dirent;
282 error = vfs_readdir(file, filldir64, &buf);
286 lastdirent = buf.previous;
288 typeof(lastdirent->d_off) d_off = file->f_pos;
289 __put_user(d_off, &lastdirent->d_off);
290 error = count - buf.count;