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);
29 if (file->f_op->iterate_shared)
31 else if (!file->f_op->iterate)
34 res = security_file_permission(file, MAY_READ);
39 inode_lock_shared(inode);
41 res = down_write_killable(&inode->i_rwsem);
47 if (!IS_DEADDIR(inode)) {
48 ctx->pos = file->f_pos;
50 res = file->f_op->iterate_shared(file, ctx);
52 res = file->f_op->iterate(file, ctx);
53 file->f_pos = ctx->pos;
54 fsnotify_access(file);
58 inode_unlock_shared(inode);
64 EXPORT_SYMBOL(iterate_dir);
67 * POSIX says that a dirent name cannot contain NULL or a '/'.
69 * It's not 100% clear what we should really do in this case.
70 * The filesystem is clearly corrupted, but returning a hard
71 * error means that you now don't see any of the other names
72 * either, so that isn't a perfect alternative.
74 * And if you return an error, what error do you use? Several
75 * filesystems seem to have decided on EUCLEAN being the error
76 * code for EFSCORRUPTED, and that may be the error to use. Or
77 * just EIO, which is perhaps more obvious to users.
79 * In order to see the other file names in the directory, the
80 * caller might want to make this a "soft" error: skip the
81 * entry, and return the error at the end instead.
83 * Note that this should likely do a "memchr(name, 0, len)"
84 * check too, since that would be filesystem corruption as
85 * well. However, that case can't actually confuse user space,
86 * which has to do a strlen() on the name anyway to find the
87 * filename length, and the above "soft error" worry means
88 * that it's probably better left alone until we have that
91 static int verify_dirent_name(const char *name, int len)
95 if (memchr(name, '/', len))
101 * Traditional linux readdir() handling..
103 * "count=1" is a special case, meaning that the buffer is one
104 * dirent-structure in size and that the code can't handle more
105 * anyway. Thus the special "fillonedir()" function for that
106 * case (the low-level handlers don't need to care about this).
109 #ifdef __ARCH_WANT_OLD_READDIR
111 struct old_linux_dirent {
113 unsigned long d_offset;
114 unsigned short d_namlen;
118 struct readdir_callback {
119 struct dir_context ctx;
120 struct old_linux_dirent __user * dirent;
124 static int fillonedir(struct dir_context *ctx, const char *name, int namlen,
125 loff_t offset, u64 ino, unsigned int d_type)
127 struct readdir_callback *buf =
128 container_of(ctx, struct readdir_callback, ctx);
129 struct old_linux_dirent __user * dirent;
135 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
136 buf->result = -EOVERFLOW;
140 dirent = buf->dirent;
141 if (!access_ok(VERIFY_WRITE, dirent,
142 (unsigned long)(dirent->d_name + namlen + 1) -
143 (unsigned long)dirent))
145 if ( __put_user(d_ino, &dirent->d_ino) ||
146 __put_user(offset, &dirent->d_offset) ||
147 __put_user(namlen, &dirent->d_namlen) ||
148 __copy_to_user(dirent->d_name, name, namlen) ||
149 __put_user(0, dirent->d_name + namlen))
153 buf->result = -EFAULT;
157 SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
158 struct old_linux_dirent __user *, dirent, unsigned int, count)
161 struct fd f = fdget_pos(fd);
162 struct readdir_callback buf = {
163 .ctx.actor = fillonedir,
170 error = iterate_dir(f.file, &buf.ctx);
178 #endif /* __ARCH_WANT_OLD_READDIR */
181 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
184 struct linux_dirent {
187 unsigned short d_reclen;
191 struct getdents_callback {
192 struct dir_context ctx;
193 struct linux_dirent __user * current_dir;
194 struct linux_dirent __user * previous;
199 static int filldir(struct dir_context *ctx, const char *name, int namlen,
200 loff_t offset, u64 ino, unsigned int d_type)
202 struct linux_dirent __user * dirent;
203 struct getdents_callback *buf =
204 container_of(ctx, struct getdents_callback, ctx);
206 int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2,
209 buf->error = verify_dirent_name(name, namlen);
210 if (unlikely(buf->error))
212 buf->error = -EINVAL; /* only used if we fail.. */
213 if (reclen > buf->count)
216 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
217 buf->error = -EOVERFLOW;
220 dirent = buf->previous;
222 if (signal_pending(current))
224 if (__put_user(offset, &dirent->d_off))
227 dirent = buf->current_dir;
228 if (__put_user(d_ino, &dirent->d_ino))
230 if (__put_user(reclen, &dirent->d_reclen))
232 if (copy_to_user(dirent->d_name, name, namlen))
234 if (__put_user(0, dirent->d_name + namlen))
236 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
238 buf->previous = dirent;
239 dirent = (void __user *)dirent + reclen;
240 buf->current_dir = dirent;
241 buf->count -= reclen;
244 buf->error = -EFAULT;
248 SYSCALL_DEFINE3(getdents, unsigned int, fd,
249 struct linux_dirent __user *, dirent, unsigned int, count)
252 struct linux_dirent __user * lastdirent;
253 struct getdents_callback buf = {
254 .ctx.actor = filldir,
256 .current_dir = dirent
260 if (!access_ok(VERIFY_WRITE, dirent, count))
267 error = iterate_dir(f.file, &buf.ctx);
270 lastdirent = buf.previous;
272 if (put_user(buf.ctx.pos, &lastdirent->d_off))
275 error = count - buf.count;
281 struct getdents_callback64 {
282 struct dir_context ctx;
283 struct linux_dirent64 __user * current_dir;
284 struct linux_dirent64 __user * previous;
289 static int filldir64(struct dir_context *ctx, const char *name, int namlen,
290 loff_t offset, u64 ino, unsigned int d_type)
292 struct linux_dirent64 __user *dirent;
293 struct getdents_callback64 *buf =
294 container_of(ctx, struct getdents_callback64, ctx);
295 int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
298 buf->error = verify_dirent_name(name, namlen);
299 if (unlikely(buf->error))
301 buf->error = -EINVAL; /* only used if we fail.. */
302 if (reclen > buf->count)
304 dirent = buf->previous;
306 if (signal_pending(current))
308 if (__put_user(offset, &dirent->d_off))
311 dirent = buf->current_dir;
312 if (__put_user(ino, &dirent->d_ino))
314 if (__put_user(0, &dirent->d_off))
316 if (__put_user(reclen, &dirent->d_reclen))
318 if (__put_user(d_type, &dirent->d_type))
320 if (copy_to_user(dirent->d_name, name, namlen))
322 if (__put_user(0, dirent->d_name + namlen))
324 buf->previous = dirent;
325 dirent = (void __user *)dirent + reclen;
326 buf->current_dir = dirent;
327 buf->count -= reclen;
330 buf->error = -EFAULT;
334 SYSCALL_DEFINE3(getdents64, unsigned int, fd,
335 struct linux_dirent64 __user *, dirent, unsigned int, count)
338 struct linux_dirent64 __user * lastdirent;
339 struct getdents_callback64 buf = {
340 .ctx.actor = filldir64,
342 .current_dir = dirent
346 if (!access_ok(VERIFY_WRITE, dirent, count))
353 error = iterate_dir(f.file, &buf.ctx);
356 lastdirent = buf.previous;
358 typeof(lastdirent->d_off) d_off = buf.ctx.pos;
359 if (__put_user(d_off, &lastdirent->d_off))
362 error = count - buf.count;