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
,
17 int (*filler
)(void *,const char *,int,off_t
,ino_t
),
20 struct inode
*inode
= file
->f_dentry
->d_inode
;
22 if (!file
->f_op
|| !file
->f_op
->readdir
)
25 down(&inode
->i_zombie
);
27 if (!IS_DEADDIR(inode
))
28 res
= file
->f_op
->readdir(file
, buf
, filler
);
35 int dcache_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
38 struct dentry
*dentry
= filp
->f_dentry
;
43 if (filldir(dirent
, ".", 1, i
, dentry
->d_inode
->i_ino
) < 0)
49 if (filldir(dirent
, "..", 2, i
, dentry
->d_parent
->d_inode
->i_ino
) < 0)
55 struct list_head
*list
= dentry
->d_subdirs
.next
;
59 if (list
== &dentry
->d_subdirs
)
68 struct dentry
*de
= list_entry(list
, struct dentry
, d_child
);
70 if (!d_unhashed(de
) && de
->d_inode
) {
71 if (filldir(dirent
, de
->d_name
.name
, de
->d_name
.len
, filp
->f_pos
, de
->d_inode
->i_ino
) < 0)
76 } while (list
!= &dentry
->d_subdirs
);
83 * Traditional linux readdir() handling..
85 * "count=1" is a special case, meaning that the buffer is one
86 * dirent-structure in size and that the code can't handle more
87 * anyway. Thus the special "fillonedir()" function for that
88 * case (the low-level handlers don't need to care about this).
90 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char *) (de)))
91 #define ROUND_UP(x) (((x)+sizeof(long)-1) & ~(sizeof(long)-1))
93 struct old_linux_dirent
{
95 unsigned long d_offset
;
96 unsigned short d_namlen
;
100 struct readdir_callback
{
101 struct old_linux_dirent
* dirent
;
105 static int fillonedir(void * __buf
, const char * name
, int namlen
, off_t offset
, ino_t ino
)
107 struct readdir_callback
* buf
= (struct readdir_callback
*) __buf
;
108 struct old_linux_dirent
* dirent
;
113 dirent
= buf
->dirent
;
114 put_user(ino
, &dirent
->d_ino
);
115 put_user(offset
, &dirent
->d_offset
);
116 put_user(namlen
, &dirent
->d_namlen
);
117 copy_to_user(dirent
->d_name
, name
, namlen
);
118 put_user(0, dirent
->d_name
+ namlen
);
122 asmlinkage
int old_readdir(unsigned int fd
, void * dirent
, unsigned int count
)
126 struct readdir_callback buf
;
137 error
= vfs_readdir(file
, fillonedir
, &buf
);
148 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
151 struct linux_dirent
{
154 unsigned short d_reclen
;
158 struct getdents_callback
{
159 struct linux_dirent
* current_dir
;
160 struct linux_dirent
* previous
;
165 static int filldir(void * __buf
, const char * name
, int namlen
, off_t offset
, ino_t ino
)
167 struct linux_dirent
* dirent
;
168 struct getdents_callback
* buf
= (struct getdents_callback
*) __buf
;
169 int reclen
= ROUND_UP(NAME_OFFSET(dirent
) + namlen
+ 1);
171 buf
->error
= -EINVAL
; /* only used if we fail.. */
172 if (reclen
> buf
->count
)
174 dirent
= buf
->previous
;
176 put_user(offset
, &dirent
->d_off
);
177 dirent
= buf
->current_dir
;
178 buf
->previous
= dirent
;
179 put_user(ino
, &dirent
->d_ino
);
180 put_user(reclen
, &dirent
->d_reclen
);
181 copy_to_user(dirent
->d_name
, name
, namlen
);
182 put_user(0, dirent
->d_name
+ namlen
);
183 ((char *) dirent
) += reclen
;
184 buf
->current_dir
= dirent
;
185 buf
->count
-= reclen
;
189 asmlinkage
long sys_getdents(unsigned int fd
, void * dirent
, unsigned int count
)
192 struct linux_dirent
* lastdirent
;
193 struct getdents_callback buf
;
201 buf
.current_dir
= (struct linux_dirent
*) dirent
;
207 error
= vfs_readdir(file
, filldir
, &buf
);
211 lastdirent
= buf
.previous
;
213 put_user(file
->f_pos
, &lastdirent
->d_off
);
214 error
= count
- buf
.count
;