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>
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
;
24 if (!file
->f_op
|| !file
->f_op
->readdir
)
27 res
= security_file_permission(file
, MAY_READ
);
33 if (!IS_DEADDIR(inode
)) {
34 res
= file
->f_op
->readdir(file
, buf
, filler
);
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))
57 struct old_linux_dirent
{
59 unsigned long d_offset
;
60 unsigned short d_namlen
;
64 struct readdir_callback
{
65 struct old_linux_dirent __user
* dirent
;
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
;
79 if (!access_ok(VERIFY_WRITE
, (unsigned long)dirent
,
80 (unsigned long)(dirent
->d_name
+ namlen
+ 1) -
81 (unsigned long)dirent
))
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
))
91 buf
->result
= -EFAULT
;
95 asmlinkage
long old_readdir(unsigned int fd
, struct old_linux_dirent __user
* dirent
, unsigned int count
)
99 struct readdir_callback buf
;
109 error
= vfs_readdir(file
, fillonedir
, &buf
);
118 #endif /* !__ia64__ */
121 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
124 struct linux_dirent
{
127 unsigned short d_reclen
;
131 struct getdents_callback
{
132 struct linux_dirent __user
* current_dir
;
133 struct linux_dirent __user
* previous
;
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
)
148 dirent
= buf
->previous
;
150 if (__put_user(offset
, &dirent
->d_off
))
153 dirent
= buf
->current_dir
;
154 if (__put_user(ino
, &dirent
->d_ino
))
156 if (__put_user(reclen
, &dirent
->d_reclen
))
158 if (copy_to_user(dirent
->d_name
, name
, namlen
))
160 if (__put_user(0, dirent
->d_name
+ namlen
))
162 if (__put_user(d_type
, (char *) dirent
+ reclen
- 1))
164 buf
->previous
= dirent
;
165 dirent
= (void __user
*)dirent
+ reclen
;
166 buf
->current_dir
= dirent
;
167 buf
->count
-= reclen
;
170 buf
->error
= -EFAULT
;
174 asmlinkage
long sys_getdents(unsigned int fd
, struct linux_dirent __user
* dirent
, unsigned int count
)
177 struct linux_dirent __user
* lastdirent
;
178 struct getdents_callback buf
;
182 if (!access_ok(VERIFY_WRITE
, dirent
, count
))
190 buf
.current_dir
= dirent
;
195 error
= vfs_readdir(file
, filldir
, &buf
);
199 lastdirent
= buf
.previous
;
201 if (put_user(file
->f_pos
, &lastdirent
->d_off
))
204 error
= count
- buf
.count
;
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
;
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
)
232 dirent
= buf
->previous
;
234 if (__put_user(offset
, &dirent
->d_off
))
237 dirent
= buf
->current_dir
;
238 if (__put_user(ino
, &dirent
->d_ino
))
240 if (__put_user(0, &dirent
->d_off
))
242 if (__put_user(reclen
, &dirent
->d_reclen
))
244 if (__put_user(d_type
, &dirent
->d_type
))
246 if (copy_to_user(dirent
->d_name
, name
, namlen
))
248 if (__put_user(0, dirent
->d_name
+ namlen
))
250 buf
->previous
= dirent
;
251 dirent
= (void __user
*)dirent
+ reclen
;
252 buf
->current_dir
= dirent
;
253 buf
->count
-= reclen
;
256 buf
->error
= -EFAULT
;
260 asmlinkage
long sys_getdents64(unsigned int fd
, struct linux_dirent64 __user
* dirent
, unsigned int count
)
263 struct linux_dirent64 __user
* lastdirent
;
264 struct getdents_callback64 buf
;
268 if (!access_ok(VERIFY_WRITE
, dirent
, count
))
276 buf
.current_dir
= dirent
;
281 error
= vfs_readdir(file
, filldir64
, &buf
);
285 lastdirent
= buf
.previous
;
287 typeof(lastdirent
->d_off
) d_off
= file
->f_pos
;
288 __put_user(d_off
, &lastdirent
->d_off
);
289 error
= count
- buf
.count
;