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
);
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))
56 struct old_linux_dirent
{
58 unsigned long d_offset
;
59 unsigned short d_namlen
;
63 struct readdir_callback
{
64 struct old_linux_dirent __user
* dirent
;
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
;
78 if (!access_ok(VERIFY_WRITE
, (unsigned long)dirent
,
79 (unsigned long)(dirent
->d_name
+ namlen
+ 1) -
80 (unsigned long)dirent
))
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
))
90 buf
->result
= -EFAULT
;
94 asmlinkage
long old_readdir(unsigned int fd
, struct old_linux_dirent __user
* dirent
, unsigned int count
)
98 struct readdir_callback buf
;
108 error
= vfs_readdir(file
, fillonedir
, &buf
);
117 #endif /* !__ia64__ */
120 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
123 struct linux_dirent
{
126 unsigned short d_reclen
;
130 struct getdents_callback
{
131 struct linux_dirent __user
* current_dir
;
132 struct linux_dirent __user
* previous
;
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
)
147 dirent
= buf
->previous
;
149 if (__put_user(offset
, &dirent
->d_off
))
152 dirent
= buf
->current_dir
;
153 if (__put_user(ino
, &dirent
->d_ino
))
155 if (__put_user(reclen
, &dirent
->d_reclen
))
157 if (copy_to_user(dirent
->d_name
, name
, namlen
))
159 if (__put_user(0, dirent
->d_name
+ namlen
))
161 buf
->previous
= dirent
;
162 ((char *) dirent
) += reclen
;
163 buf
->current_dir
= dirent
;
164 buf
->count
-= reclen
;
167 buf
->error
= -EFAULT
;
171 asmlinkage
long sys_getdents(unsigned int fd
, struct linux_dirent __user
* dirent
, unsigned int count
)
174 struct linux_dirent __user
* lastdirent
;
175 struct getdents_callback buf
;
179 if (!access_ok(VERIFY_WRITE
, dirent
, count
))
187 buf
.current_dir
= dirent
;
192 error
= vfs_readdir(file
, filldir
, &buf
);
196 lastdirent
= buf
.previous
;
198 if (put_user(file
->f_pos
, &lastdirent
->d_off
))
201 error
= count
- buf
.count
;
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
;
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
)
229 dirent
= buf
->previous
;
231 if (__put_user(offset
, &dirent
->d_off
))
234 dirent
= buf
->current_dir
;
235 if (__put_user(ino
, &dirent
->d_ino
))
237 if (__put_user(0, &dirent
->d_off
))
239 if (__put_user(reclen
, &dirent
->d_reclen
))
241 if (__put_user(d_type
, &dirent
->d_type
))
243 if (copy_to_user(dirent
->d_name
, name
, namlen
))
245 if (__put_user(0, dirent
->d_name
+ namlen
))
247 buf
->previous
= dirent
;
248 ((char *) dirent
) += reclen
;
249 buf
->current_dir
= dirent
;
250 buf
->count
-= reclen
;
253 buf
->error
= -EFAULT
;
257 asmlinkage
long sys_getdents64(unsigned int fd
, struct linux_dirent64 __user
* dirent
, unsigned int count
)
260 struct linux_dirent64 __user
* lastdirent
;
261 struct getdents_callback64 buf
;
265 if (!access_ok(VERIFY_WRITE
, dirent
, count
))
273 buf
.current_dir
= dirent
;
278 error
= vfs_readdir(file
, filldir64
, &buf
);
282 lastdirent
= buf
.previous
;
284 typeof(lastdirent
->d_off
) d_off
= file
->f_pos
;
285 __put_user(d_off
, &lastdirent
->d_off
);
286 error
= count
- buf
.count
;