4 * Copyright (C) 1995 Linus Torvalds
7 #include <linux/sched.h>
9 #include <linux/errno.h>
10 #include <linux/stat.h>
11 #include <linux/file.h>
12 #include <linux/smp_lock.h>
14 #include <asm/uaccess.h>
16 int vfs_readdir(struct file
*file
, filldir_t filler
, void *buf
)
18 struct inode
*inode
= file
->f_dentry
->d_inode
;
20 if (!file
->f_op
|| !file
->f_op
->readdir
)
23 down(&inode
->i_zombie
);
25 if (!IS_DEADDIR(inode
)) {
27 res
= file
->f_op
->readdir(file
, buf
, filler
);
37 * Directory is locked and all positive dentries in it are safe, since
38 * for ramfs-type trees they can't go away without unlink() or rmdir(),
39 * both impossible due to the lock on directory.
42 int dcache_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
45 struct dentry
*dentry
= filp
->f_dentry
;
50 if (filldir(dirent
, ".", 1, i
, dentry
->d_inode
->i_ino
, DT_DIR
) < 0)
56 if (filldir(dirent
, "..", 2, i
, dentry
->d_parent
->d_inode
->i_ino
, DT_DIR
) < 0)
62 struct list_head
*list
;
65 spin_lock(&dcache_lock
);
66 list
= dentry
->d_subdirs
.next
;
69 if (list
== &dentry
->d_subdirs
) {
70 spin_unlock(&dcache_lock
);
80 struct dentry
*de
= list_entry(list
, struct dentry
, d_child
);
82 if (!list_empty(&de
->d_hash
) && de
->d_inode
) {
83 spin_unlock(&dcache_lock
);
84 if (filldir(dirent
, de
->d_name
.name
, de
->d_name
.len
, filp
->f_pos
, de
->d_inode
->i_ino
, DT_UNKNOWN
) < 0)
86 spin_lock(&dcache_lock
);
90 if (list
!= &dentry
->d_subdirs
)
92 spin_unlock(&dcache_lock
);
101 * Traditional linux readdir() handling..
103 * "count=1" is a special case, meaning that the buffer is one
104 * dirent-structure in size and that the code can't handle more
105 * anyway. Thus the special "fillonedir()" function for that
106 * case (the low-level handlers don't need to care about this).
108 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char *) (de)))
109 #define ROUND_UP(x) (((x)+sizeof(long)-1) & ~(sizeof(long)-1))
113 struct old_linux_dirent
{
115 unsigned long d_offset
;
116 unsigned short d_namlen
;
120 struct readdir_callback
{
121 struct old_linux_dirent
* dirent
;
125 static int fillonedir(void * __buf
, const char * name
, int namlen
, off_t offset
,
126 ino_t ino
, unsigned int d_type
)
128 struct readdir_callback
* buf
= (struct readdir_callback
*) __buf
;
129 struct old_linux_dirent
* dirent
;
134 dirent
= buf
->dirent
;
135 put_user(ino
, &dirent
->d_ino
);
136 put_user(offset
, &dirent
->d_offset
);
137 put_user(namlen
, &dirent
->d_namlen
);
138 copy_to_user(dirent
->d_name
, name
, namlen
);
139 put_user(0, dirent
->d_name
+ namlen
);
143 asmlinkage
int old_readdir(unsigned int fd
, void * dirent
, unsigned int count
)
147 struct readdir_callback buf
;
157 error
= vfs_readdir(file
, fillonedir
, &buf
);
166 #endif /* !__ia64__ */
169 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
172 struct linux_dirent
{
175 unsigned short d_reclen
;
179 struct getdents_callback
{
180 struct linux_dirent
* current_dir
;
181 struct linux_dirent
* previous
;
186 static int filldir(void * __buf
, const char * name
, int namlen
, off_t offset
,
187 ino_t ino
, unsigned int d_type
)
189 struct linux_dirent
* dirent
;
190 struct getdents_callback
* buf
= (struct getdents_callback
*) __buf
;
191 int reclen
= ROUND_UP(NAME_OFFSET(dirent
) + namlen
+ 1);
193 buf
->error
= -EINVAL
; /* only used if we fail.. */
194 if (reclen
> buf
->count
)
196 dirent
= buf
->previous
;
198 put_user(offset
, &dirent
->d_off
);
199 dirent
= buf
->current_dir
;
200 buf
->previous
= dirent
;
201 put_user(ino
, &dirent
->d_ino
);
202 put_user(reclen
, &dirent
->d_reclen
);
203 copy_to_user(dirent
->d_name
, name
, namlen
);
204 put_user(0, dirent
->d_name
+ namlen
);
205 ((char *) dirent
) += reclen
;
206 buf
->current_dir
= dirent
;
207 buf
->count
-= reclen
;
211 asmlinkage
long sys_getdents(unsigned int fd
, void * dirent
, unsigned int count
)
214 struct linux_dirent
* lastdirent
;
215 struct getdents_callback buf
;
223 buf
.current_dir
= (struct linux_dirent
*) dirent
;
228 error
= vfs_readdir(file
, filldir
, &buf
);
232 lastdirent
= buf
.previous
;
234 put_user(file
->f_pos
, &lastdirent
->d_off
);
235 error
= count
- buf
.count
;
245 * And even better one including d_type field and 64bit d_ino and d_off.
247 struct linux_dirent64
{
250 unsigned short d_reclen
;
251 unsigned char d_type
;
255 #define ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
257 struct getdents_callback64
{
258 struct linux_dirent64
* current_dir
;
259 struct linux_dirent64
* previous
;
264 static int filldir64(void * __buf
, const char * name
, int namlen
, off_t offset
,
265 ino_t ino
, unsigned int d_type
)
267 struct linux_dirent64
* dirent
, d
;
268 struct getdents_callback64
* buf
= (struct getdents_callback64
*) __buf
;
269 int reclen
= ROUND_UP64(NAME_OFFSET(dirent
) + namlen
+ 1);
271 buf
->error
= -EINVAL
; /* only used if we fail.. */
272 if (reclen
> buf
->count
)
274 dirent
= buf
->previous
;
277 copy_to_user(&dirent
->d_off
, &d
.d_off
, sizeof(d
.d_off
));
279 dirent
= buf
->current_dir
;
280 buf
->previous
= dirent
;
281 memset(&d
, 0, NAME_OFFSET(&d
));
285 copy_to_user(dirent
, &d
, NAME_OFFSET(&d
));
286 copy_to_user(dirent
->d_name
, name
, namlen
);
287 put_user(0, dirent
->d_name
+ namlen
);
288 ((char *) dirent
) += reclen
;
289 buf
->current_dir
= dirent
;
290 buf
->count
-= reclen
;
294 asmlinkage
long sys_getdents64(unsigned int fd
, void * dirent
, unsigned int count
)
297 struct linux_dirent64
* lastdirent
;
298 struct getdents_callback64 buf
;
306 buf
.current_dir
= (struct linux_dirent64
*) dirent
;
311 error
= vfs_readdir(file
, filldir64
, &buf
);
315 lastdirent
= buf
.previous
;
317 struct linux_dirent64 d
;
318 d
.d_off
= file
->f_pos
;
319 copy_to_user(&lastdirent
->d_off
, &d
.d_off
, sizeof(d
.d_off
));
320 error
= count
- buf
.count
;