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>
17 * Traditional linux readdir() handling..
19 * "count=1" is a special case, meaning that the buffer is one
20 * dirent-structure in size and that the code can't handle more
21 * anyway. Thus the special "fillonedir()" function for that
22 * case (the low-level handlers don't need to care about this).
24 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char *) (de)))
25 #define ROUND_UP(x) (((x)+sizeof(long)-1) & ~(sizeof(long)-1))
27 struct old_linux_dirent
{
29 unsigned long d_offset
;
30 unsigned short d_namlen
;
34 struct readdir_callback
{
35 struct old_linux_dirent
* dirent
;
39 static int fillonedir(void * __buf
, const char * name
, int namlen
, off_t offset
, ino_t ino
)
41 struct readdir_callback
* buf
= (struct readdir_callback
*) __buf
;
42 struct old_linux_dirent
* dirent
;
48 put_user(ino
, &dirent
->d_ino
);
49 put_user(offset
, &dirent
->d_offset
);
50 put_user(namlen
, &dirent
->d_namlen
);
51 copy_to_user(dirent
->d_name
, name
, namlen
);
52 put_user(0, dirent
->d_name
+ namlen
);
56 asmlinkage
int old_readdir(unsigned int fd
, void * dirent
, unsigned int count
)
60 struct dentry
* dentry
;
62 struct readdir_callback buf
;
70 dentry
= file
->f_dentry
;
74 inode
= dentry
->d_inode
;
82 if (!file
->f_op
|| !file
->f_op
->readdir
)
86 * Get the inode's semaphore to prevent changes
87 * to the directory while we read it.
90 error
= file
->f_op
->readdir(file
, &buf
, fillonedir
);
104 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
107 struct linux_dirent
{
110 unsigned short d_reclen
;
114 struct getdents_callback
{
115 struct linux_dirent
* current_dir
;
116 struct linux_dirent
* previous
;
121 static int filldir(void * __buf
, const char * name
, int namlen
, off_t offset
, ino_t ino
)
123 struct linux_dirent
* dirent
;
124 struct getdents_callback
* buf
= (struct getdents_callback
*) __buf
;
125 int reclen
= ROUND_UP(NAME_OFFSET(dirent
) + namlen
+ 1);
127 buf
->error
= -EINVAL
; /* only used if we fail.. */
128 if (reclen
> buf
->count
)
130 dirent
= buf
->previous
;
132 put_user(offset
, &dirent
->d_off
);
133 dirent
= buf
->current_dir
;
134 buf
->previous
= dirent
;
135 put_user(ino
, &dirent
->d_ino
);
136 put_user(reclen
, &dirent
->d_reclen
);
137 copy_to_user(dirent
->d_name
, name
, namlen
);
138 put_user(0, dirent
->d_name
+ namlen
);
139 ((char *) dirent
) += reclen
;
140 buf
->current_dir
= dirent
;
141 buf
->count
-= reclen
;
145 asmlinkage
long sys_getdents(unsigned int fd
, void * dirent
, unsigned int count
)
148 struct dentry
* dentry
;
149 struct inode
* inode
;
150 struct linux_dirent
* lastdirent
;
151 struct getdents_callback buf
;
160 dentry
= file
->f_dentry
;
164 inode
= dentry
->d_inode
;
168 buf
.current_dir
= (struct linux_dirent
*) dirent
;
174 if (!file
->f_op
|| !file
->f_op
->readdir
)
178 * Get the inode's semaphore to prevent changes
179 * to the directory while we read it.
182 error
= file
->f_op
->readdir(file
, &buf
, filldir
);
187 lastdirent
= buf
.previous
;
189 put_user(file
->f_pos
, &lastdirent
->d_off
);
190 error
= count
- buf
.count
;