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);
36 // res = mutex_lock_killable(&inode->i_mutex);
41 if (!IS_DEADDIR(inode)) {
42 ctx->pos = file->f_pos;
43 res = file->f_op->iterate(file, ctx);
44 file->f_pos = ctx->pos;
45 fsnotify_access(file);
52 EXPORT_SYMBOL(iterate_dir);
55 * Traditional linux readdir() handling..
57 * "count=1" is a special case, meaning that the buffer is one
58 * dirent-structure in size and that the code can't handle more
59 * anyway. Thus the special "fillonedir()" function for that
60 * case (the low-level handlers don't need to care about this).
63 #ifdef __ARCH_WANT_OLD_READDIR
65 struct old_linux_dirent {
67 unsigned long d_offset;
68 unsigned short d_namlen;
72 struct readdir_callback {
73 struct dir_context ctx;
74 struct old_linux_dirent __user * dirent;
78 static int fillonedir(struct dir_context *ctx, const char *name, int namlen,
79 loff_t offset, u64 ino, unsigned int d_type)
81 struct readdir_callback *buf =
82 container_of(ctx, struct readdir_callback, ctx);
83 struct old_linux_dirent __user * dirent;
89 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
90 buf->result = -EOVERFLOW;
95 if (!access_ok(VERIFY_WRITE, dirent,
96 (unsigned long)(dirent->d_name + namlen + 1) -
97 (unsigned long)dirent))
99 if ( __put_user(d_ino, &dirent->d_ino) ||
100 __put_user(offset, &dirent->d_offset) ||
101 __put_user(namlen, &dirent->d_namlen) ||
102 __copy_to_user(dirent->d_name, name, namlen) ||
103 __put_user(0, dirent->d_name + namlen))
107 buf->result = -EFAULT;
111 SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
112 struct old_linux_dirent __user *, dirent, unsigned int, count)
115 struct fd f = fdget(fd);
116 struct readdir_callback buf = {
117 .ctx.actor = fillonedir,
124 error = iterate_dir(f.file, &buf.ctx);
132 #endif /* __ARCH_WANT_OLD_READDIR */
135 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
138 struct linux_dirent {
141 unsigned short d_reclen;
145 struct getdents_callback {
146 struct dir_context ctx;
147 struct linux_dirent __user * current_dir;
148 struct linux_dirent __user * previous;
153 static int filldir(struct dir_context *ctx, const char *name, int namlen,
154 loff_t offset, u64 ino, unsigned int d_type)
156 struct linux_dirent __user * dirent;
157 struct getdents_callback *buf =
158 container_of(ctx, struct getdents_callback, ctx);
160 int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2,
163 buf->error = -EINVAL; /* only used if we fail.. */
164 if (reclen > buf->count)
167 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
168 buf->error = -EOVERFLOW;
171 dirent = buf->previous;
173 if (__put_user(offset, &dirent->d_off))
176 dirent = buf->current_dir;
177 if (__put_user(d_ino, &dirent->d_ino))
179 if (__put_user(reclen, &dirent->d_reclen))
181 if (copy_to_user(dirent->d_name, name, namlen))
183 if (__put_user(0, dirent->d_name + namlen))
185 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
187 buf->previous = dirent;
188 dirent = (void __user *)dirent + reclen;
189 buf->current_dir = dirent;
190 buf->count -= reclen;
193 buf->error = -EFAULT;
197 SYSCALL_DEFINE3(getdents, unsigned int, fd,
198 struct linux_dirent __user *, dirent, unsigned int, count)
201 struct linux_dirent __user * lastdirent;
202 struct getdents_callback buf = {
203 .ctx.actor = filldir,
205 .current_dir = dirent
209 if (!access_ok(VERIFY_WRITE, dirent, count))
216 error = iterate_dir(f.file, &buf.ctx);
219 lastdirent = buf.previous;
221 if (put_user(buf.ctx.pos, &lastdirent->d_off))
224 error = count - buf.count;
230 struct getdents_callback64 {
231 struct dir_context ctx;
232 struct linux_dirent64 __user * current_dir;
233 struct linux_dirent64 __user * previous;
238 static int filldir64(struct dir_context *ctx, const char *name, int namlen,
239 loff_t offset, u64 ino, unsigned int d_type)
241 struct linux_dirent64 __user *dirent;
242 struct getdents_callback64 *buf =
243 container_of(ctx, struct getdents_callback64, ctx);
244 int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
247 buf->error = -EINVAL; /* only used if we fail.. */
248 if (reclen > buf->count)
250 dirent = buf->previous;
252 if (__put_user(offset, &dirent->d_off))
255 dirent = buf->current_dir;
256 if (__put_user(ino, &dirent->d_ino))
258 if (__put_user(0, &dirent->d_off))
260 if (__put_user(reclen, &dirent->d_reclen))
262 if (__put_user(d_type, &dirent->d_type))
264 if (copy_to_user(dirent->d_name, name, namlen))
266 if (__put_user(0, dirent->d_name + namlen))
268 buf->previous = dirent;
269 dirent = (void __user *)dirent + reclen;
270 buf->current_dir = dirent;
271 buf->count -= reclen;
274 buf->error = -EFAULT;
278 SYSCALL_DEFINE3(getdents64, unsigned int, fd,
279 struct linux_dirent64 __user *, dirent, unsigned int, count)
282 struct linux_dirent64 __user * lastdirent;
283 struct getdents_callback64 buf = {
284 .ctx.actor = filldir64,
286 .current_dir = dirent
290 if (!access_ok(VERIFY_WRITE, dirent, count))
297 error = iterate_dir(f.file, &buf.ctx);
300 lastdirent = buf.previous;
302 typeof(lastdirent->d_off) d_off = buf.ctx.pos;
303 if (__put_user(d_off, &lastdirent->d_off))
306 error = count - buf.count;