]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/readdir.c
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 res
= file
->f_op
->iterate(file
, ctx
);
43 file
->f_pos
= ctx
->pos
;
45 res
= file
->f_op
->readdir(file
, ctx
, ctx
->actor
);
46 ctx
->pos
= file
->f_pos
;
50 mutex_unlock(&inode
->i_mutex
);
54 EXPORT_SYMBOL(iterate_dir
);
57 * Traditional linux readdir() handling..
59 * "count=1" is a special case, meaning that the buffer is one
60 * dirent-structure in size and that the code can't handle more
61 * anyway. Thus the special "fillonedir()" function for that
62 * case (the low-level handlers don't need to care about this).
65 #ifdef __ARCH_WANT_OLD_READDIR
67 struct old_linux_dirent
{
69 unsigned long d_offset
;
70 unsigned short d_namlen
;
74 struct readdir_callback
{
75 struct dir_context ctx
;
76 struct old_linux_dirent __user
* dirent
;
80 static int fillonedir(void * __buf
, const char * name
, int namlen
, loff_t offset
,
81 u64 ino
, unsigned int d_type
)
83 struct readdir_callback
*buf
= (struct readdir_callback
*) __buf
;
84 struct old_linux_dirent __user
* dirent
;
90 if (sizeof(d_ino
) < sizeof(ino
) && d_ino
!= ino
) {
91 buf
->result
= -EOVERFLOW
;
96 if (!access_ok(VERIFY_WRITE
, dirent
,
97 (unsigned long)(dirent
->d_name
+ namlen
+ 1) -
98 (unsigned long)dirent
))
100 if ( __put_user(d_ino
, &dirent
->d_ino
) ||
101 __put_user(offset
, &dirent
->d_offset
) ||
102 __put_user(namlen
, &dirent
->d_namlen
) ||
103 __copy_to_user(dirent
->d_name
, name
, namlen
) ||
104 __put_user(0, dirent
->d_name
+ namlen
))
108 buf
->result
= -EFAULT
;
112 SYSCALL_DEFINE3(old_readdir
, unsigned int, fd
,
113 struct old_linux_dirent __user
*, dirent
, unsigned int, count
)
116 struct fd f
= fdget(fd
);
117 struct readdir_callback buf
;
122 buf
.ctx
.actor
= fillonedir
;
126 error
= iterate_dir(f
.file
, &buf
.ctx
);
134 #endif /* __ARCH_WANT_OLD_READDIR */
137 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
140 struct linux_dirent
{
143 unsigned short d_reclen
;
147 struct getdents_callback
{
148 struct dir_context ctx
;
149 struct linux_dirent __user
* current_dir
;
150 struct linux_dirent __user
* previous
;
155 static int filldir(void * __buf
, const char * name
, int namlen
, loff_t offset
,
156 u64 ino
, unsigned int d_type
)
158 struct linux_dirent __user
* dirent
;
159 struct getdents_callback
* buf
= (struct getdents_callback
*) __buf
;
161 int reclen
= ALIGN(offsetof(struct linux_dirent
, d_name
) + namlen
+ 2,
164 buf
->error
= -EINVAL
; /* only used if we fail.. */
165 if (reclen
> buf
->count
)
168 if (sizeof(d_ino
) < sizeof(ino
) && d_ino
!= ino
) {
169 buf
->error
= -EOVERFLOW
;
172 dirent
= buf
->previous
;
174 if (__put_user(offset
, &dirent
->d_off
))
177 dirent
= buf
->current_dir
;
178 if (__put_user(d_ino
, &dirent
->d_ino
))
180 if (__put_user(reclen
, &dirent
->d_reclen
))
182 if (copy_to_user(dirent
->d_name
, name
, namlen
))
184 if (__put_user(0, dirent
->d_name
+ namlen
))
186 if (__put_user(d_type
, (char __user
*) dirent
+ reclen
- 1))
188 buf
->previous
= dirent
;
189 dirent
= (void __user
*)dirent
+ reclen
;
190 buf
->current_dir
= dirent
;
191 buf
->count
-= reclen
;
194 buf
->error
= -EFAULT
;
198 SYSCALL_DEFINE3(getdents
, unsigned int, fd
,
199 struct linux_dirent __user
*, dirent
, unsigned int, count
)
202 struct linux_dirent __user
* lastdirent
;
203 struct getdents_callback buf
;
206 if (!access_ok(VERIFY_WRITE
, dirent
, count
))
213 buf
.current_dir
= dirent
;
217 buf
.ctx
.actor
= filldir
;
219 error
= iterate_dir(f
.file
, &buf
.ctx
);
222 lastdirent
= buf
.previous
;
224 if (put_user(buf
.ctx
.pos
, &lastdirent
->d_off
))
227 error
= count
- buf
.count
;
233 struct getdents_callback64
{
234 struct dir_context ctx
;
235 struct linux_dirent64 __user
* current_dir
;
236 struct linux_dirent64 __user
* previous
;
241 static int filldir64(void * __buf
, const char * name
, int namlen
, loff_t offset
,
242 u64 ino
, unsigned int d_type
)
244 struct linux_dirent64 __user
*dirent
;
245 struct getdents_callback64
* buf
= (struct getdents_callback64
*) __buf
;
246 int reclen
= ALIGN(offsetof(struct linux_dirent64
, d_name
) + namlen
+ 1,
249 buf
->error
= -EINVAL
; /* only used if we fail.. */
250 if (reclen
> buf
->count
)
252 dirent
= buf
->previous
;
254 if (__put_user(offset
, &dirent
->d_off
))
257 dirent
= buf
->current_dir
;
258 if (__put_user(ino
, &dirent
->d_ino
))
260 if (__put_user(0, &dirent
->d_off
))
262 if (__put_user(reclen
, &dirent
->d_reclen
))
264 if (__put_user(d_type
, &dirent
->d_type
))
266 if (copy_to_user(dirent
->d_name
, name
, namlen
))
268 if (__put_user(0, dirent
->d_name
+ namlen
))
270 buf
->previous
= dirent
;
271 dirent
= (void __user
*)dirent
+ reclen
;
272 buf
->current_dir
= dirent
;
273 buf
->count
-= reclen
;
276 buf
->error
= -EFAULT
;
280 SYSCALL_DEFINE3(getdents64
, unsigned int, fd
,
281 struct linux_dirent64 __user
*, dirent
, unsigned int, count
)
284 struct linux_dirent64 __user
* lastdirent
;
285 struct getdents_callback64 buf
;
288 if (!access_ok(VERIFY_WRITE
, dirent
, count
))
295 buf
.current_dir
= dirent
;
299 buf
.ctx
.actor
= filldir64
;
301 error
= iterate_dir(f
.file
, &buf
.ctx
);
304 lastdirent
= buf
.previous
;
306 typeof(lastdirent
->d_off
) d_off
= buf
.ctx
.pos
;
307 if (__put_user(d_off
, &lastdirent
->d_off
))
310 error
= count
- buf
.count
;