4 * Copyright (C) 1995 Linus Torvalds
7 #include <linux/time.h>
9 #include <linux/errno.h>
10 #include <linux/stat.h>
11 #include <linux/file.h>
12 #include <linux/smp_lock.h>
14 #include <linux/dirent.h>
15 #include <linux/security.h>
17 #include <asm/uaccess.h>
19 int vfs_readdir(struct file
*file
, filldir_t filler
, void *buf
)
21 struct inode
*inode
= file
->f_dentry
->d_inode
;
23 if (!file
->f_op
|| !file
->f_op
->readdir
)
26 res
= security_file_permission(file
, MAY_READ
);
32 if (!IS_DEADDIR(inode
)) {
33 res
= file
->f_op
->readdir(file
, buf
, filler
);
41 * Traditional linux readdir() handling..
43 * "count=1" is a special case, meaning that the buffer is one
44 * dirent-structure in size and that the code can't handle more
45 * anyway. Thus the special "fillonedir()" function for that
46 * case (the low-level handlers don't need to care about this).
48 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char *) (de)))
49 #define ROUND_UP(x) (((x)+sizeof(long)-1) & ~(sizeof(long)-1))
53 struct old_linux_dirent
{
55 unsigned long d_offset
;
56 unsigned short d_namlen
;
60 struct readdir_callback
{
61 struct old_linux_dirent __user
* dirent
;
65 static int fillonedir(void * __buf
, const char * name
, int namlen
, loff_t offset
,
66 ino_t ino
, unsigned int d_type
)
68 struct readdir_callback
* buf
= (struct readdir_callback
*) __buf
;
69 struct old_linux_dirent __user
* dirent
;
75 if (!access_ok(VERIFY_WRITE
, (unsigned long)dirent
,
76 (unsigned long)(dirent
->d_name
+ namlen
+ 1) -
77 (unsigned long)dirent
))
79 if ( __put_user(ino
, &dirent
->d_ino
) ||
80 __put_user(offset
, &dirent
->d_offset
) ||
81 __put_user(namlen
, &dirent
->d_namlen
) ||
82 __copy_to_user(dirent
->d_name
, name
, namlen
) ||
83 __put_user(0, dirent
->d_name
+ namlen
))
87 buf
->result
= -EFAULT
;
91 asmlinkage
long old_readdir(unsigned int fd
, struct old_linux_dirent __user
* dirent
, unsigned int count
)
95 struct readdir_callback buf
;
105 error
= vfs_readdir(file
, fillonedir
, &buf
);
114 #endif /* !__ia64__ */
117 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
120 struct linux_dirent
{
123 unsigned short d_reclen
;
127 struct getdents_callback
{
128 struct linux_dirent __user
* current_dir
;
129 struct linux_dirent __user
* previous
;
134 static int filldir(void * __buf
, const char * name
, int namlen
, loff_t offset
,
135 ino_t ino
, unsigned int d_type
)
137 struct linux_dirent __user
* dirent
;
138 struct getdents_callback
* buf
= (struct getdents_callback
*) __buf
;
139 int reclen
= ROUND_UP(NAME_OFFSET(dirent
) + namlen
+ 1);
141 buf
->error
= -EINVAL
; /* only used if we fail.. */
142 if (reclen
> buf
->count
)
144 dirent
= buf
->previous
;
146 if (__put_user(offset
, &dirent
->d_off
))
149 dirent
= buf
->current_dir
;
150 if (__put_user(ino
, &dirent
->d_ino
))
152 if (__put_user(reclen
, &dirent
->d_reclen
))
154 if (copy_to_user(dirent
->d_name
, name
, namlen
))
156 if (__put_user(0, dirent
->d_name
+ namlen
))
158 buf
->previous
= dirent
;
159 ((char *) dirent
) += reclen
;
160 buf
->current_dir
= dirent
;
161 buf
->count
-= reclen
;
164 buf
->error
= -EFAULT
;
168 asmlinkage
long sys_getdents(unsigned int fd
, struct linux_dirent __user
* dirent
, unsigned int count
)
171 struct linux_dirent __user
* lastdirent
;
172 struct getdents_callback buf
;
176 if (!access_ok(VERIFY_WRITE
, dirent
, count
))
184 buf
.current_dir
= dirent
;
189 error
= vfs_readdir(file
, filldir
, &buf
);
193 lastdirent
= buf
.previous
;
195 if (put_user(file
->f_pos
, &lastdirent
->d_off
))
198 error
= count
- buf
.count
;
207 #define ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
209 struct getdents_callback64
{
210 struct linux_dirent64 __user
* current_dir
;
211 struct linux_dirent64 __user
* previous
;
216 static int filldir64(void * __buf
, const char * name
, int namlen
, loff_t offset
,
217 ino_t ino
, unsigned int d_type
)
219 struct linux_dirent64 __user
*dirent
;
220 struct getdents_callback64
* buf
= (struct getdents_callback64
*) __buf
;
221 int reclen
= ROUND_UP64(NAME_OFFSET(dirent
) + namlen
+ 1);
223 buf
->error
= -EINVAL
; /* only used if we fail.. */
224 if (reclen
> buf
->count
)
226 dirent
= buf
->previous
;
228 if (__put_user(offset
, &dirent
->d_off
))
231 dirent
= buf
->current_dir
;
232 if (__put_user(ino
, &dirent
->d_ino
))
234 if (__put_user(0, &dirent
->d_off
))
236 if (__put_user(reclen
, &dirent
->d_reclen
))
238 if (__put_user(d_type
, &dirent
->d_type
))
240 if (copy_to_user(dirent
->d_name
, name
, namlen
))
242 if (__put_user(0, dirent
->d_name
+ namlen
))
244 buf
->previous
= dirent
;
245 ((char *) dirent
) += reclen
;
246 buf
->current_dir
= dirent
;
247 buf
->count
-= reclen
;
250 buf
->error
= -EFAULT
;
254 asmlinkage
long sys_getdents64(unsigned int fd
, struct linux_dirent64 __user
* dirent
, unsigned int count
)
257 struct linux_dirent64 __user
* lastdirent
;
258 struct getdents_callback64 buf
;
262 if (!access_ok(VERIFY_WRITE
, dirent
, count
))
270 buf
.current_dir
= dirent
;
275 error
= vfs_readdir(file
, filldir64
, &buf
);
279 lastdirent
= buf
.previous
;
281 typeof(lastdirent
->d_off
) d_off
= file
->f_pos
;
282 __put_user(d_off
, &lastdirent
->d_off
);
283 error
= count
- buf
.count
;