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>
15 #include <asm/uaccess.h>
17 int vfs_readdir(struct file
*file
, filldir_t filler
, void *buf
)
19 struct inode
*inode
= file
->f_dentry
->d_inode
;
21 if (!file
->f_op
|| !file
->f_op
->readdir
)
24 res
= security_ops
->file_permission(file
, MAY_READ
);
30 if (!IS_DEADDIR(inode
)) {
31 res
= file
->f_op
->readdir(file
, buf
, filler
);
39 * Traditional linux readdir() handling..
41 * "count=1" is a special case, meaning that the buffer is one
42 * dirent-structure in size and that the code can't handle more
43 * anyway. Thus the special "fillonedir()" function for that
44 * case (the low-level handlers don't need to care about this).
46 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char *) (de)))
47 #define ROUND_UP(x) (((x)+sizeof(long)-1) & ~(sizeof(long)-1))
51 struct old_linux_dirent
{
53 unsigned long d_offset
;
54 unsigned short d_namlen
;
58 struct readdir_callback
{
59 struct old_linux_dirent
* dirent
;
63 static int fillonedir(void * __buf
, const char * name
, int namlen
, loff_t offset
,
64 ino_t ino
, unsigned int d_type
)
66 struct readdir_callback
* buf
= (struct readdir_callback
*) __buf
;
67 struct old_linux_dirent
* dirent
;
73 put_user(ino
, &dirent
->d_ino
);
74 put_user(offset
, &dirent
->d_offset
);
75 put_user(namlen
, &dirent
->d_namlen
);
76 copy_to_user(dirent
->d_name
, name
, namlen
);
77 put_user(0, dirent
->d_name
+ namlen
);
81 asmlinkage
int old_readdir(unsigned int fd
, void * dirent
, unsigned int count
)
85 struct readdir_callback buf
;
95 error
= vfs_readdir(file
, fillonedir
, &buf
);
104 #endif /* !__ia64__ */
107 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
110 struct linux_dirent
{
113 unsigned short d_reclen
;
117 struct getdents_callback
{
118 struct linux_dirent
* current_dir
;
119 struct linux_dirent
* previous
;
124 static int filldir(void * __buf
, const char * name
, int namlen
, loff_t offset
,
125 ino_t ino
, unsigned int d_type
)
127 struct linux_dirent
* dirent
;
128 struct getdents_callback
* buf
= (struct getdents_callback
*) __buf
;
129 int reclen
= ROUND_UP(NAME_OFFSET(dirent
) + namlen
+ 1);
131 buf
->error
= -EINVAL
; /* only used if we fail.. */
132 if (reclen
> buf
->count
)
134 dirent
= buf
->previous
;
136 put_user(offset
, &dirent
->d_off
);
137 dirent
= buf
->current_dir
;
138 buf
->previous
= dirent
;
139 put_user(ino
, &dirent
->d_ino
);
140 put_user(reclen
, &dirent
->d_reclen
);
141 copy_to_user(dirent
->d_name
, name
, namlen
);
142 put_user(0, dirent
->d_name
+ namlen
);
143 ((char *) dirent
) += reclen
;
144 buf
->current_dir
= dirent
;
145 buf
->count
-= reclen
;
149 asmlinkage
long sys_getdents(unsigned int fd
, void * dirent
, unsigned int count
)
152 struct linux_dirent
* lastdirent
;
153 struct getdents_callback buf
;
161 buf
.current_dir
= (struct linux_dirent
*) dirent
;
166 error
= vfs_readdir(file
, filldir
, &buf
);
170 lastdirent
= buf
.previous
;
172 put_user(file
->f_pos
, &lastdirent
->d_off
);
173 error
= count
- buf
.count
;
183 * And even better one including d_type field and 64bit d_ino and d_off.
185 struct linux_dirent64
{
188 unsigned short d_reclen
;
189 unsigned char d_type
;
193 #define ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
195 struct getdents_callback64
{
196 struct linux_dirent64
* current_dir
;
197 struct linux_dirent64
* previous
;
202 static int filldir64(void * __buf
, const char * name
, int namlen
, loff_t offset
,
203 ino_t ino
, unsigned int d_type
)
205 struct linux_dirent64
* dirent
, d
;
206 struct getdents_callback64
* buf
= (struct getdents_callback64
*) __buf
;
207 int reclen
= ROUND_UP64(NAME_OFFSET(dirent
) + namlen
+ 1);
209 buf
->error
= -EINVAL
; /* only used if we fail.. */
210 if (reclen
> buf
->count
)
212 dirent
= buf
->previous
;
215 copy_to_user(&dirent
->d_off
, &d
.d_off
, sizeof(d
.d_off
));
217 dirent
= buf
->current_dir
;
218 buf
->previous
= dirent
;
219 memset(&d
, 0, NAME_OFFSET(&d
));
223 copy_to_user(dirent
, &d
, NAME_OFFSET(&d
));
224 copy_to_user(dirent
->d_name
, name
, namlen
);
225 put_user(0, dirent
->d_name
+ namlen
);
226 ((char *) dirent
) += reclen
;
227 buf
->current_dir
= dirent
;
228 buf
->count
-= reclen
;
232 asmlinkage
long sys_getdents64(unsigned int fd
, void * dirent
, unsigned int count
)
235 struct linux_dirent64
* lastdirent
;
236 struct getdents_callback64 buf
;
244 buf
.current_dir
= (struct linux_dirent64
*) dirent
;
249 error
= vfs_readdir(file
, filldir64
, &buf
);
253 lastdirent
= buf
.previous
;
255 struct linux_dirent64 d
;
256 d
.d_off
= file
->f_pos
;
257 copy_to_user(&lastdirent
->d_off
, &d
.d_off
, sizeof(d
.d_off
));
258 error
= count
- buf
.count
;