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(void * __buf, const char * name, int namlen, loff_t offset,
78 u64 ino, unsigned int d_type)
80 struct readdir_callback *buf = (struct readdir_callback *) __buf;
81 struct old_linux_dirent __user * dirent;
87 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
88 buf->result = -EOVERFLOW;
93 if (!access_ok(VERIFY_WRITE, dirent,
94 (unsigned long)(dirent->d_name + namlen + 1) -
95 (unsigned long)dirent))
97 if ( __put_user(d_ino, &dirent->d_ino) ||
98 __put_user(offset, &dirent->d_offset) ||
99 __put_user(namlen, &dirent->d_namlen) ||
100 __copy_to_user(dirent->d_name, name, namlen) ||
101 __put_user(0, dirent->d_name + namlen))
105 buf->result = -EFAULT;
109 SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
110 struct old_linux_dirent __user *, dirent, unsigned int, count)
113 struct fd f = fdget(fd);
114 struct readdir_callback buf = {
115 .ctx.actor = fillonedir,
122 error = iterate_dir(f.file, &buf.ctx);
130 #endif /* __ARCH_WANT_OLD_READDIR */
133 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
136 struct linux_dirent {
139 unsigned short d_reclen;
143 struct getdents_callback {
144 struct dir_context ctx;
145 struct linux_dirent __user * current_dir;
146 struct linux_dirent __user * previous;
151 static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
152 u64 ino, unsigned int d_type)
154 struct linux_dirent __user * dirent;
155 struct getdents_callback * buf = (struct getdents_callback *) __buf;
157 int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2,
160 buf->error = -EINVAL; /* only used if we fail.. */
161 if (reclen > buf->count)
164 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
165 buf->error = -EOVERFLOW;
168 dirent = buf->previous;
170 if (__put_user(offset, &dirent->d_off))
173 dirent = buf->current_dir;
174 if (__put_user(d_ino, &dirent->d_ino))
176 if (__put_user(reclen, &dirent->d_reclen))
178 if (copy_to_user(dirent->d_name, name, namlen))
180 if (__put_user(0, dirent->d_name + namlen))
182 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
184 buf->previous = dirent;
185 dirent = (void __user *)dirent + reclen;
186 buf->current_dir = dirent;
187 buf->count -= reclen;
190 buf->error = -EFAULT;
194 SYSCALL_DEFINE3(getdents, unsigned int, fd,
195 struct linux_dirent __user *, dirent, unsigned int, count)
198 struct linux_dirent __user * lastdirent;
199 struct getdents_callback buf = {
200 .ctx.actor = filldir,
202 .current_dir = dirent
206 if (!access_ok(VERIFY_WRITE, dirent, count))
213 error = iterate_dir(f.file, &buf.ctx);
216 lastdirent = buf.previous;
218 if (put_user(buf.ctx.pos, &lastdirent->d_off))
221 error = count - buf.count;
227 struct getdents_callback64 {
228 struct dir_context ctx;
229 struct linux_dirent64 __user * current_dir;
230 struct linux_dirent64 __user * previous;
235 static int filldir64(void * __buf, const char * name, int namlen, loff_t offset,
236 u64 ino, unsigned int d_type)
238 struct linux_dirent64 __user *dirent;
239 struct getdents_callback64 * buf = (struct getdents_callback64 *) __buf;
240 int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
243 buf->error = -EINVAL; /* only used if we fail.. */
244 if (reclen > buf->count)
246 dirent = buf->previous;
248 if (__put_user(offset, &dirent->d_off))
251 dirent = buf->current_dir;
252 if (__put_user(ino, &dirent->d_ino))
254 if (__put_user(0, &dirent->d_off))
256 if (__put_user(reclen, &dirent->d_reclen))
258 if (__put_user(d_type, &dirent->d_type))
260 if (copy_to_user(dirent->d_name, name, namlen))
262 if (__put_user(0, dirent->d_name + namlen))
264 buf->previous = dirent;
265 dirent = (void __user *)dirent + reclen;
266 buf->current_dir = dirent;
267 buf->count -= reclen;
270 buf->error = -EFAULT;
274 SYSCALL_DEFINE3(getdents64, unsigned int, fd,
275 struct linux_dirent64 __user *, dirent, unsigned int, count)
278 struct linux_dirent64 __user * lastdirent;
279 struct getdents_callback64 buf = {
280 .ctx.actor = filldir64,
282 .current_dir = dirent
286 if (!access_ok(VERIFY_WRITE, dirent, count))
293 error = iterate_dir(f.file, &buf.ctx);
296 lastdirent = buf.previous;
298 typeof(lastdirent->d_off) d_off = buf.ctx.pos;
299 if (__put_user(d_off, &lastdirent->d_off))
302 error = count - buf.count;