4 * Copyright (C) 1995 Linus Torvalds
7 #include <linux/stddef.h>
8 #include <linux/kernel.h>
9 #include <linux/export.h>
10 #include <linux/time.h>
12 #include <linux/errno.h>
13 #include <linux/stat.h>
14 #include <linux/file.h>
16 #include <linux/fsnotify.h>
17 #include <linux/dirent.h>
18 #include <linux/security.h>
19 #include <linux/syscalls.h>
20 #include <linux/unistd.h>
22 #include <asm/uaccess.h>
24 int iterate_dir(struct file
*file
, struct dir_context
*ctx
)
26 struct inode
*inode
= file_inode(file
);
28 if (!file
->f_op
->iterate
)
31 res
= security_file_permission(file
, MAY_READ
);
35 res
= mutex_lock_killable(&inode
->i_mutex
);
40 if (!IS_DEADDIR(inode
)) {
41 ctx
->pos
= file
->f_pos
;
42 res
= file
->f_op
->iterate(file
, ctx
);
43 file
->f_pos
= ctx
->pos
;
44 fsnotify_access(file
);
47 mutex_unlock(&inode
->i_mutex
);
51 EXPORT_SYMBOL(iterate_dir
);
54 * Traditional linux readdir() handling..
56 * "count=1" is a special case, meaning that the buffer is one
57 * dirent-structure in size and that the code can't handle more
58 * anyway. Thus the special "fillonedir()" function for that
59 * case (the low-level handlers don't need to care about this).
62 #ifdef __ARCH_WANT_OLD_READDIR
64 struct old_linux_dirent
{
66 unsigned long d_offset
;
67 unsigned short d_namlen
;
71 struct readdir_callback
{
72 struct dir_context ctx
;
73 struct old_linux_dirent __user
* dirent
;
77 static int fillonedir(struct dir_context
*ctx
, const char *name
, int namlen
,
78 loff_t offset
, u64 ino
, unsigned int d_type
)
80 struct readdir_callback
*buf
=
81 container_of(ctx
, struct readdir_callback
, ctx
);
82 struct old_linux_dirent __user
* dirent
;
88 if (sizeof(d_ino
) < sizeof(ino
) && d_ino
!= ino
) {
89 buf
->result
= -EOVERFLOW
;
94 if (!access_ok(VERIFY_WRITE
, dirent
,
95 (unsigned long)(dirent
->d_name
+ namlen
+ 1) -
96 (unsigned long)dirent
))
98 if ( __put_user(d_ino
, &dirent
->d_ino
) ||
99 __put_user(offset
, &dirent
->d_offset
) ||
100 __put_user(namlen
, &dirent
->d_namlen
) ||
101 __copy_to_user(dirent
->d_name
, name
, namlen
) ||
102 __put_user(0, dirent
->d_name
+ namlen
))
106 buf
->result
= -EFAULT
;
110 SYSCALL_DEFINE3(old_readdir
, unsigned int, fd
,
111 struct old_linux_dirent __user
*, dirent
, unsigned int, count
)
114 struct fd f
= fdget(fd
);
115 struct readdir_callback buf
= {
116 .ctx
.actor
= fillonedir
,
123 error
= iterate_dir(f
.file
, &buf
.ctx
);
131 #endif /* __ARCH_WANT_OLD_READDIR */
134 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
137 struct linux_dirent
{
140 unsigned short d_reclen
;
144 struct getdents_callback
{
145 struct dir_context ctx
;
146 struct linux_dirent __user
* current_dir
;
147 struct linux_dirent __user
* previous
;
152 static int filldir(struct dir_context
*ctx
, const char *name
, int namlen
,
153 loff_t offset
, u64 ino
, unsigned int d_type
)
155 struct linux_dirent __user
* dirent
;
156 struct getdents_callback
*buf
=
157 container_of(ctx
, struct getdents_callback
, ctx
);
159 int reclen
= ALIGN(offsetof(struct linux_dirent
, d_name
) + namlen
+ 2,
162 buf
->error
= -EINVAL
; /* only used if we fail.. */
163 if (reclen
> buf
->count
)
166 if (sizeof(d_ino
) < sizeof(ino
) && d_ino
!= ino
) {
167 buf
->error
= -EOVERFLOW
;
170 dirent
= buf
->previous
;
172 if (__put_user(offset
, &dirent
->d_off
))
175 dirent
= buf
->current_dir
;
176 if (__put_user(d_ino
, &dirent
->d_ino
))
178 if (__put_user(reclen
, &dirent
->d_reclen
))
180 if (copy_to_user(dirent
->d_name
, name
, namlen
))
182 if (__put_user(0, dirent
->d_name
+ namlen
))
184 if (__put_user(d_type
, (char __user
*) dirent
+ reclen
- 1))
186 buf
->previous
= dirent
;
187 dirent
= (void __user
*)dirent
+ reclen
;
188 buf
->current_dir
= dirent
;
189 buf
->count
-= reclen
;
192 buf
->error
= -EFAULT
;
196 SYSCALL_DEFINE3(getdents
, unsigned int, fd
,
197 struct linux_dirent __user
*, dirent
, unsigned int, count
)
200 struct linux_dirent __user
* lastdirent
;
201 struct getdents_callback buf
= {
202 .ctx
.actor
= filldir
,
204 .current_dir
= dirent
208 if (!access_ok(VERIFY_WRITE
, dirent
, count
))
215 error
= iterate_dir(f
.file
, &buf
.ctx
);
218 lastdirent
= buf
.previous
;
220 if (put_user(buf
.ctx
.pos
, &lastdirent
->d_off
))
223 error
= count
- buf
.count
;
229 struct getdents_callback64
{
230 struct dir_context ctx
;
231 struct linux_dirent64 __user
* current_dir
;
232 struct linux_dirent64 __user
* previous
;
237 static int filldir64(struct dir_context
*ctx
, const char *name
, int namlen
,
238 loff_t offset
, u64 ino
, unsigned int d_type
)
240 struct linux_dirent64 __user
*dirent
;
241 struct getdents_callback64
*buf
=
242 container_of(ctx
, struct getdents_callback64
, ctx
);
243 int reclen
= ALIGN(offsetof(struct linux_dirent64
, d_name
) + namlen
+ 1,
246 buf
->error
= -EINVAL
; /* only used if we fail.. */
247 if (reclen
> buf
->count
)
249 dirent
= buf
->previous
;
251 if (__put_user(offset
, &dirent
->d_off
))
254 dirent
= buf
->current_dir
;
255 if (__put_user(ino
, &dirent
->d_ino
))
257 if (__put_user(0, &dirent
->d_off
))
259 if (__put_user(reclen
, &dirent
->d_reclen
))
261 if (__put_user(d_type
, &dirent
->d_type
))
263 if (copy_to_user(dirent
->d_name
, name
, namlen
))
265 if (__put_user(0, dirent
->d_name
+ namlen
))
267 buf
->previous
= dirent
;
268 dirent
= (void __user
*)dirent
+ reclen
;
269 buf
->current_dir
= dirent
;
270 buf
->count
-= reclen
;
273 buf
->error
= -EFAULT
;
277 SYSCALL_DEFINE3(getdents64
, unsigned int, fd
,
278 struct linux_dirent64 __user
*, dirent
, unsigned int, count
)
281 struct linux_dirent64 __user
* lastdirent
;
282 struct getdents_callback64 buf
= {
283 .ctx
.actor
= filldir64
,
285 .current_dir
= dirent
289 if (!access_ok(VERIFY_WRITE
, dirent
, count
))
296 error
= iterate_dir(f
.file
, &buf
.ctx
);
299 lastdirent
= buf
.previous
;
301 typeof(lastdirent
->d_off
) d_off
= buf
.ctx
.pos
;
302 if (__put_user(d_off
, &lastdirent
->d_off
))
305 error
= count
- buf
.count
;