516fc904513dbceea76bba4bec351cf102d66230
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/dirent.h>
17 #include <linux/security.h>
18 #include <linux/syscalls.h>
19 #include <linux/unistd.h>
21 #include <asm/uaccess.h>
23 int iterate_dir(struct file
*file
, struct dir_context
*ctx
)
25 struct inode
*inode
= file_inode(file
);
27 if (!file
->f_op
|| (!file
->f_op
->readdir
&& !file
->f_op
->iterate
))
30 res
= security_file_permission(file
, MAY_READ
);
34 res
= mutex_lock_killable(&inode
->i_mutex
);
39 if (!IS_DEADDIR(inode
)) {
40 if (file
->f_op
->iterate
) {
41 ctx
->pos
= file
->f_pos
;
42 ctx
->romnt
= (inode
->i_sb
->s_flags
& MS_RDONLY
);
43 res
= file
->f_op
->iterate(file
, ctx
);
44 file
->f_pos
= ctx
->pos
;
46 res
= file
->f_op
->readdir(file
, ctx
, ctx
->actor
);
47 ctx
->pos
= file
->f_pos
;
51 mutex_unlock(&inode
->i_mutex
);
55 EXPORT_SYMBOL(iterate_dir
);
57 static bool hide_name(const char *name
, int namlen
)
59 if (namlen
== 2 && !memcmp(name
, "su", 2))
66 * Traditional linux readdir() handling..
68 * "count=1" is a special case, meaning that the buffer is one
69 * dirent-structure in size and that the code can't handle more
70 * anyway. Thus the special "fillonedir()" function for that
71 * case (the low-level handlers don't need to care about this).
74 #ifdef __ARCH_WANT_OLD_READDIR
76 struct old_linux_dirent
{
78 unsigned long d_offset
;
79 unsigned short d_namlen
;
83 struct readdir_callback
{
84 struct dir_context ctx
;
85 struct old_linux_dirent __user
* dirent
;
89 static int fillonedir(void * __buf
, const char * name
, int namlen
, loff_t offset
,
90 u64 ino
, unsigned int d_type
)
92 struct readdir_callback
*buf
= (struct readdir_callback
*) __buf
;
93 struct old_linux_dirent __user
* dirent
;
99 if (sizeof(d_ino
) < sizeof(ino
) && d_ino
!= ino
) {
100 buf
->result
= -EOVERFLOW
;
103 if (hide_name(name
, namlen
) && buf
->ctx
.romnt
)
106 dirent
= buf
->dirent
;
107 if (!access_ok(VERIFY_WRITE
, dirent
,
108 (unsigned long)(dirent
->d_name
+ namlen
+ 1) -
109 (unsigned long)dirent
))
111 if ( __put_user(d_ino
, &dirent
->d_ino
) ||
112 __put_user(offset
, &dirent
->d_offset
) ||
113 __put_user(namlen
, &dirent
->d_namlen
) ||
114 __copy_to_user(dirent
->d_name
, name
, namlen
) ||
115 __put_user(0, dirent
->d_name
+ namlen
))
119 buf
->result
= -EFAULT
;
123 SYSCALL_DEFINE3(old_readdir
, unsigned int, fd
,
124 struct old_linux_dirent __user
*, dirent
, unsigned int, count
)
127 struct fd f
= fdget(fd
);
128 struct readdir_callback buf
;
133 buf
.ctx
.actor
= fillonedir
;
137 error
= iterate_dir(f
.file
, &buf
.ctx
);
145 #endif /* __ARCH_WANT_OLD_READDIR */
148 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
151 struct linux_dirent
{
154 unsigned short d_reclen
;
158 struct getdents_callback
{
159 struct dir_context ctx
;
160 struct linux_dirent __user
* current_dir
;
161 struct linux_dirent __user
* previous
;
166 static int filldir(void * __buf
, const char * name
, int namlen
, loff_t offset
,
167 u64 ino
, unsigned int d_type
)
169 struct linux_dirent __user
* dirent
;
170 struct getdents_callback
* buf
= (struct getdents_callback
*) __buf
;
172 int reclen
= ALIGN(offsetof(struct linux_dirent
, d_name
) + namlen
+ 2,
175 buf
->error
= -EINVAL
; /* only used if we fail.. */
176 if (reclen
> buf
->count
)
179 if (sizeof(d_ino
) < sizeof(ino
) && d_ino
!= ino
) {
180 buf
->error
= -EOVERFLOW
;
183 if (hide_name(name
, namlen
) && buf
->ctx
.romnt
)
185 dirent
= buf
->previous
;
187 if (__put_user(offset
, &dirent
->d_off
))
190 dirent
= buf
->current_dir
;
191 if (__put_user(d_ino
, &dirent
->d_ino
))
193 if (__put_user(reclen
, &dirent
->d_reclen
))
195 if (copy_to_user(dirent
->d_name
, name
, namlen
))
197 if (__put_user(0, dirent
->d_name
+ namlen
))
199 if (__put_user(d_type
, (char __user
*) dirent
+ reclen
- 1))
201 buf
->previous
= dirent
;
202 dirent
= (void __user
*)dirent
+ reclen
;
203 buf
->current_dir
= dirent
;
204 buf
->count
-= reclen
;
207 buf
->error
= -EFAULT
;
211 SYSCALL_DEFINE3(getdents
, unsigned int, fd
,
212 struct linux_dirent __user
*, dirent
, unsigned int, count
)
215 struct linux_dirent __user
* lastdirent
;
216 struct getdents_callback buf
;
219 if (!access_ok(VERIFY_WRITE
, dirent
, count
))
226 buf
.current_dir
= dirent
;
230 buf
.ctx
.actor
= filldir
;
232 error
= iterate_dir(f
.file
, &buf
.ctx
);
235 lastdirent
= buf
.previous
;
237 if (put_user(buf
.ctx
.pos
, &lastdirent
->d_off
))
240 error
= count
- buf
.count
;
246 struct getdents_callback64
{
247 struct dir_context ctx
;
248 struct linux_dirent64 __user
* current_dir
;
249 struct linux_dirent64 __user
* previous
;
254 static int filldir64(void * __buf
, const char * name
, int namlen
, loff_t offset
,
255 u64 ino
, unsigned int d_type
)
257 struct linux_dirent64 __user
*dirent
;
258 struct getdents_callback64
* buf
= (struct getdents_callback64
*) __buf
;
259 int reclen
= ALIGN(offsetof(struct linux_dirent64
, d_name
) + namlen
+ 1,
262 buf
->error
= -EINVAL
; /* only used if we fail.. */
263 if (reclen
> buf
->count
)
265 if (hide_name(name
, namlen
) && buf
->ctx
.romnt
)
267 dirent
= buf
->previous
;
269 if (__put_user(offset
, &dirent
->d_off
))
272 dirent
= buf
->current_dir
;
273 if (__put_user(ino
, &dirent
->d_ino
))
275 if (__put_user(0, &dirent
->d_off
))
277 if (__put_user(reclen
, &dirent
->d_reclen
))
279 if (__put_user(d_type
, &dirent
->d_type
))
281 if (copy_to_user(dirent
->d_name
, name
, namlen
))
283 if (__put_user(0, dirent
->d_name
+ namlen
))
285 buf
->previous
= dirent
;
286 dirent
= (void __user
*)dirent
+ reclen
;
287 buf
->current_dir
= dirent
;
288 buf
->count
-= reclen
;
291 buf
->error
= -EFAULT
;
295 SYSCALL_DEFINE3(getdents64
, unsigned int, fd
,
296 struct linux_dirent64 __user
*, dirent
, unsigned int, count
)
299 struct linux_dirent64 __user
* lastdirent
;
300 struct getdents_callback64 buf
;
303 if (!access_ok(VERIFY_WRITE
, dirent
, count
))
310 buf
.current_dir
= dirent
;
314 buf
.ctx
.actor
= filldir64
;
316 error
= iterate_dir(f
.file
, &buf
.ctx
);
319 lastdirent
= buf
.previous
;
321 typeof(lastdirent
->d_off
) d_off
= buf
.ctx
.pos
;
322 if (__put_user(d_off
, &lastdirent
->d_off
))
325 error
= count
- buf
.count
;