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))
95 struct old_linux_dirent
{
97 unsigned long d_offset
;
98 unsigned short d_namlen
;
102 struct readdir_callback
{
103 struct old_linux_dirent
* dirent
;
107 static int fillonedir(void * __buf
, const char * name
, int namlen
, off_t offset
, ino_t ino
)
109 struct readdir_callback
* buf
= (struct readdir_callback
*) __buf
;
110 struct old_linux_dirent
* dirent
;
115 dirent
= buf
->dirent
;
116 put_user(ino
, &dirent
->d_ino
);
117 put_user(offset
, &dirent
->d_offset
);
118 put_user(namlen
, &dirent
->d_namlen
);
119 copy_to_user(dirent
->d_name
, name
, namlen
);
120 put_user(0, dirent
->d_name
+ namlen
);
124 asmlinkage
int old_readdir(unsigned int fd
, void * dirent
, unsigned int count
)
128 struct readdir_callback buf
;
139 error
= vfs_readdir(file
, fillonedir
, &buf
);
149 #endif /* !__ia64__ */
152 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
155 struct linux_dirent
{
158 unsigned short d_reclen
;
162 struct getdents_callback
{
163 struct linux_dirent
* current_dir
;
164 struct linux_dirent
* previous
;
169 static int filldir(void * __buf
, const char * name
, int namlen
, off_t offset
, ino_t ino
)
171 struct linux_dirent
* dirent
;
172 struct getdents_callback
* buf
= (struct getdents_callback
*) __buf
;
173 int reclen
= ROUND_UP(NAME_OFFSET(dirent
) + namlen
+ 1);
175 buf
->error
= -EINVAL
; /* only used if we fail.. */
176 if (reclen
> buf
->count
)
178 dirent
= buf
->previous
;
180 put_user(offset
, &dirent
->d_off
);
181 dirent
= buf
->current_dir
;
182 buf
->previous
= dirent
;
183 put_user(ino
, &dirent
->d_ino
);
184 put_user(reclen
, &dirent
->d_reclen
);
185 copy_to_user(dirent
->d_name
, name
, namlen
);
186 put_user(0, dirent
->d_name
+ namlen
);
187 ((char *) dirent
) += reclen
;
188 buf
->current_dir
= dirent
;
189 buf
->count
-= reclen
;
193 asmlinkage
long sys_getdents(unsigned int fd
, void * dirent
, unsigned int count
)
196 struct linux_dirent
* lastdirent
;
197 struct getdents_callback buf
;
205 buf
.current_dir
= (struct linux_dirent
*) dirent
;
211 error
= vfs_readdir(file
, filldir
, &buf
);
215 lastdirent
= buf
.previous
;
217 put_user(file
->f_pos
, &lastdirent
->d_off
);
218 error
= count
- buf
.count
;