4 * Copyright (C) 1995 Linus Torvalds
7 #include <linux/sched.h>
9 #include <linux/types.h>
10 #include <linux/errno.h>
11 #include <linux/stat.h>
12 #include <linux/file.h>
13 #include <linux/kernel.h>
14 #include <linux/smp.h>
15 #include <linux/smp_lock.h>
17 #include <asm/uaccess.h>
20 * Traditional linux readdir() handling..
22 * "count=1" is a special case, meaning that the buffer is one
23 * dirent-structure in size and that the code can't handle more
24 * anyway. Thus the special "fillonedir()" function for that
25 * case (the low-level handlers don't need to care about this).
27 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char *) (de)))
28 #define ROUND_UP(x) (((x)+sizeof(long)-1) & ~(sizeof(long)-1))
30 struct old_linux_dirent
{
32 unsigned long d_offset
;
33 unsigned short d_namlen
;
37 struct readdir_callback
{
38 struct old_linux_dirent
* dirent
;
42 static int fillonedir(void * __buf
, const char * name
, int namlen
, off_t offset
, ino_t ino
)
44 struct readdir_callback
* buf
= (struct readdir_callback
*) __buf
;
45 struct old_linux_dirent
* dirent
;
51 put_user(ino
, &dirent
->d_ino
);
52 put_user(offset
, &dirent
->d_offset
);
53 put_user(namlen
, &dirent
->d_namlen
);
54 copy_to_user(dirent
->d_name
, name
, namlen
);
55 put_user(0, dirent
->d_name
+ namlen
);
59 asmlinkage
int old_readdir(unsigned int fd
, void * dirent
, unsigned int count
)
63 struct dentry
* dentry
;
65 struct readdir_callback buf
;
73 dentry
= file
->f_dentry
;
77 inode
= dentry
->d_inode
;
85 if (!file
->f_op
|| !file
->f_op
->readdir
)
89 * Get the inode's semaphore to prevent changes
90 * to the directory while we read it.
93 error
= file
->f_op
->readdir(file
, &buf
, fillonedir
);
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
, off_t offset
, ino_t ino
)
126 struct linux_dirent
* dirent
;
127 struct getdents_callback
* buf
= (struct getdents_callback
*) __buf
;
128 int reclen
= ROUND_UP(NAME_OFFSET(dirent
) + namlen
+ 1);
130 buf
->error
= -EINVAL
; /* only used if we fail.. */
131 if (reclen
> buf
->count
)
133 dirent
= buf
->previous
;
135 put_user(offset
, &dirent
->d_off
);
136 dirent
= buf
->current_dir
;
137 buf
->previous
= dirent
;
138 put_user(ino
, &dirent
->d_ino
);
139 put_user(reclen
, &dirent
->d_reclen
);
140 copy_to_user(dirent
->d_name
, name
, namlen
);
141 put_user(0, dirent
->d_name
+ namlen
);
142 ((char *) dirent
) += reclen
;
143 buf
->current_dir
= dirent
;
144 buf
->count
-= reclen
;
148 asmlinkage
int sys_getdents(unsigned int fd
, void * dirent
, unsigned int count
)
151 struct dentry
* dentry
;
152 struct inode
* inode
;
153 struct linux_dirent
* lastdirent
;
154 struct getdents_callback buf
;
163 dentry
= file
->f_dentry
;
167 inode
= dentry
->d_inode
;
171 buf
.current_dir
= (struct linux_dirent
*) dirent
;
177 if (!file
->f_op
|| !file
->f_op
->readdir
)
181 * Get the inode's semaphore to prevent changes
182 * to the directory while we read it.
185 error
= file
->f_op
->readdir(file
, &buf
, filldir
);
190 lastdirent
= buf
.previous
;
192 put_user(file
->f_pos
, &lastdirent
->d_off
);
193 error
= count
- buf
.count
;