4 * Copyright (C) 1995 Linus Torvalds
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/time.h>
11 #include <linux/errno.h>
12 #include <linux/stat.h>
13 #include <linux/file.h>
15 #include <linux/dirent.h>
16 #include <linux/security.h>
17 #include <linux/syscalls.h>
18 #include <linux/unistd.h>
20 #include <asm/uaccess.h>
22 int vfs_readdir(struct file
*file
, filldir_t filler
, void *buf
)
24 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
26 if (!file
->f_op
|| !file
->f_op
->readdir
)
29 res
= security_file_permission(file
, MAY_READ
);
33 mutex_lock(&inode
->i_mutex
);
35 if (!IS_DEADDIR(inode
)) {
36 res
= file
->f_op
->readdir(file
, buf
, filler
);
39 mutex_unlock(&inode
->i_mutex
);
44 EXPORT_SYMBOL(vfs_readdir
);
47 * Traditional linux readdir() handling..
49 * "count=1" is a special case, meaning that the buffer is one
50 * dirent-structure in size and that the code can't handle more
51 * anyway. Thus the special "fillonedir()" function for that
52 * case (the low-level handlers don't need to care about this).
54 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
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 u64 ino
, unsigned int d_type
)
73 struct readdir_callback
* buf
= (struct readdir_callback
*) __buf
;
74 struct old_linux_dirent __user
* dirent
;
80 if (sizeof(d_ino
) < sizeof(ino
) && d_ino
!= ino
)
84 if (!access_ok(VERIFY_WRITE
, dirent
,
85 (unsigned long)(dirent
->d_name
+ namlen
+ 1) -
86 (unsigned long)dirent
))
88 if ( __put_user(d_ino
, &dirent
->d_ino
) ||
89 __put_user(offset
, &dirent
->d_offset
) ||
90 __put_user(namlen
, &dirent
->d_namlen
) ||
91 __copy_to_user(dirent
->d_name
, name
, namlen
) ||
92 __put_user(0, dirent
->d_name
+ namlen
))
96 buf
->result
= -EFAULT
;
100 asmlinkage
long old_readdir(unsigned int fd
, struct old_linux_dirent __user
* dirent
, unsigned int count
)
104 struct readdir_callback buf
;
114 error
= vfs_readdir(file
, fillonedir
, &buf
);
123 #endif /* __ARCH_WANT_OLD_READDIR */
126 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
129 struct linux_dirent
{
132 unsigned short d_reclen
;
136 struct getdents_callback
{
137 struct linux_dirent __user
* current_dir
;
138 struct linux_dirent __user
* previous
;
143 static int filldir(void * __buf
, const char * name
, int namlen
, loff_t offset
,
144 u64 ino
, unsigned int d_type
)
146 struct linux_dirent __user
* dirent
;
147 struct getdents_callback
* buf
= (struct getdents_callback
*) __buf
;
149 int reclen
= ALIGN(NAME_OFFSET(dirent
) + namlen
+ 2, sizeof(long));
151 buf
->error
= -EINVAL
; /* only used if we fail.. */
152 if (reclen
> buf
->count
)
155 if (sizeof(d_ino
) < sizeof(ino
) && d_ino
!= ino
)
157 dirent
= buf
->previous
;
159 if (__put_user(offset
, &dirent
->d_off
))
162 dirent
= buf
->current_dir
;
163 if (__put_user(d_ino
, &dirent
->d_ino
))
165 if (__put_user(reclen
, &dirent
->d_reclen
))
167 if (copy_to_user(dirent
->d_name
, name
, namlen
))
169 if (__put_user(0, dirent
->d_name
+ namlen
))
171 if (__put_user(d_type
, (char __user
*) dirent
+ reclen
- 1))
173 buf
->previous
= dirent
;
174 dirent
= (void __user
*)dirent
+ reclen
;
175 buf
->current_dir
= dirent
;
176 buf
->count
-= reclen
;
179 buf
->error
= -EFAULT
;
183 asmlinkage
long sys_getdents(unsigned int fd
, struct linux_dirent __user
* dirent
, unsigned int count
)
186 struct linux_dirent __user
* lastdirent
;
187 struct getdents_callback buf
;
191 if (!access_ok(VERIFY_WRITE
, dirent
, count
))
199 buf
.current_dir
= dirent
;
204 error
= vfs_readdir(file
, filldir
, &buf
);
208 lastdirent
= buf
.previous
;
210 if (put_user(file
->f_pos
, &lastdirent
->d_off
))
213 error
= count
- buf
.count
;
222 struct getdents_callback64
{
223 struct linux_dirent64 __user
* current_dir
;
224 struct linux_dirent64 __user
* previous
;
229 static int filldir64(void * __buf
, const char * name
, int namlen
, loff_t offset
,
230 u64 ino
, unsigned int d_type
)
232 struct linux_dirent64 __user
*dirent
;
233 struct getdents_callback64
* buf
= (struct getdents_callback64
*) __buf
;
234 int reclen
= ALIGN(NAME_OFFSET(dirent
) + namlen
+ 1, sizeof(u64
));
236 buf
->error
= -EINVAL
; /* only used if we fail.. */
237 if (reclen
> buf
->count
)
239 dirent
= buf
->previous
;
241 if (__put_user(offset
, &dirent
->d_off
))
244 dirent
= buf
->current_dir
;
245 if (__put_user(ino
, &dirent
->d_ino
))
247 if (__put_user(0, &dirent
->d_off
))
249 if (__put_user(reclen
, &dirent
->d_reclen
))
251 if (__put_user(d_type
, &dirent
->d_type
))
253 if (copy_to_user(dirent
->d_name
, name
, namlen
))
255 if (__put_user(0, dirent
->d_name
+ namlen
))
257 buf
->previous
= dirent
;
258 dirent
= (void __user
*)dirent
+ reclen
;
259 buf
->current_dir
= dirent
;
260 buf
->count
-= reclen
;
263 buf
->error
= -EFAULT
;
267 asmlinkage
long sys_getdents64(unsigned int fd
, struct linux_dirent64 __user
* dirent
, unsigned int count
)
270 struct linux_dirent64 __user
* lastdirent
;
271 struct getdents_callback64 buf
;
275 if (!access_ok(VERIFY_WRITE
, dirent
, count
))
283 buf
.current_dir
= dirent
;
288 error
= vfs_readdir(file
, filldir64
, &buf
);
292 lastdirent
= buf
.previous
;
294 typeof(lastdirent
->d_off
) d_off
= file
->f_pos
;
296 if (__put_user(d_off
, &lastdirent
->d_off
))
298 error
= count
- buf
.count
;