blob: e21af53ec24ea8505b8c3eb8317d35de56ddbdfb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/readdir.c
3 *
4 * Copyright (C) 1995 Linus Torvalds
5 */
6
Kevin Winchester85c9fe82010-08-09 17:20:22 -07007#include <linux/stddef.h>
Milind Arun Choudhary022a1692007-05-08 00:29:02 -07008#include <linux/kernel.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -05009#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/time.h>
11#include <linux/mm.h>
12#include <linux/errno.h>
13#include <linux/stat.h>
14#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/fs.h>
16#include <linux/dirent.h>
17#include <linux/security.h>
18#include <linux/syscalls.h>
19#include <linux/unistd.h>
20
21#include <asm/uaccess.h>
22
Al Viro5c0ba4e2013-05-15 13:52:59 -040023int iterate_dir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
Al Viro496ad9a2013-01-23 17:07:38 -050025 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 int res = -ENOTDIR;
Al Viro72c2d532013-09-22 16:27:52 -040027 if (!file->f_op->iterate)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 goto out;
29
30 res = security_file_permission(file, MAY_READ);
31 if (res)
32 goto out;
33
Liam R. Howlettda784512007-12-06 17:39:54 -050034 res = mutex_lock_killable(&inode->i_mutex);
35 if (res)
36 goto out;
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 res = -ENOENT;
39 if (!IS_DEADDIR(inode)) {
Al Viro2233f312013-05-22 21:44:23 -040040 ctx->pos = file->f_pos;
41 res = file->f_op->iterate(file, ctx);
42 file->f_pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 file_accessed(file);
44 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080045 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046out:
47 return res;
48}
Al Viro5c0ba4e2013-05-15 13:52:59 -040049EXPORT_SYMBOL(iterate_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/*
52 * Traditional linux readdir() handling..
53 *
54 * "count=1" is a special case, meaning that the buffer is one
55 * dirent-structure in size and that the code can't handle more
56 * anyway. Thus the special "fillonedir()" function for that
57 * case (the low-level handlers don't need to care about this).
58 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#ifdef __ARCH_WANT_OLD_READDIR
61
62struct old_linux_dirent {
63 unsigned long d_ino;
64 unsigned long d_offset;
65 unsigned short d_namlen;
66 char d_name[1];
67};
68
69struct readdir_callback {
Al Viro5c0ba4e2013-05-15 13:52:59 -040070 struct dir_context ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 struct old_linux_dirent __user * dirent;
72 int result;
73};
74
Miklos Szeredi30d1df12014-10-30 17:37:34 +010075static int fillonedir(struct dir_context *ctx, const char *name, int namlen,
76 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Miklos Szeredi30d1df12014-10-30 17:37:34 +010078 struct readdir_callback *buf =
79 container_of(ctx, struct readdir_callback, ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 struct old_linux_dirent __user * dirent;
David Howellsafefdbb2006-10-03 01:13:46 -070081 unsigned long d_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 if (buf->result)
84 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -070085 d_ino = ino;
Al Viro8f3f6552008-08-12 00:28:24 -040086 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
87 buf->result = -EOVERFLOW;
David Howellsafefdbb2006-10-03 01:13:46 -070088 return -EOVERFLOW;
Al Viro8f3f6552008-08-12 00:28:24 -040089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 buf->result++;
91 dirent = buf->dirent;
92 if (!access_ok(VERIFY_WRITE, dirent,
93 (unsigned long)(dirent->d_name + namlen + 1) -
94 (unsigned long)dirent))
95 goto efault;
David Howellsafefdbb2006-10-03 01:13:46 -070096 if ( __put_user(d_ino, &dirent->d_ino) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 __put_user(offset, &dirent->d_offset) ||
98 __put_user(namlen, &dirent->d_namlen) ||
99 __copy_to_user(dirent->d_name, name, namlen) ||
100 __put_user(0, dirent->d_name + namlen))
101 goto efault;
102 return 0;
103efault:
104 buf->result = -EFAULT;
105 return -EFAULT;
106}
107
Heiko Carstensd4e82042009-01-14 14:14:34 +0100108SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
109 struct old_linux_dirent __user *, dirent, unsigned int, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 int error;
Al Viro2903ff02012-08-28 12:52:22 -0400112 struct fd f = fdget(fd);
Al Viroac6614b2013-05-22 22:22:04 -0400113 struct readdir_callback buf = {
114 .ctx.actor = fillonedir,
115 .dirent = dirent
116 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Al Viro2903ff02012-08-28 12:52:22 -0400118 if (!f.file)
Al Viro863ced72012-04-21 18:40:32 -0400119 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Al Viro5c0ba4e2013-05-15 13:52:59 -0400121 error = iterate_dir(f.file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -0400122 if (buf.result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 error = buf.result;
124
Al Viro2903ff02012-08-28 12:52:22 -0400125 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return error;
127}
128
129#endif /* __ARCH_WANT_OLD_READDIR */
130
131/*
132 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
133 * interface.
134 */
135struct linux_dirent {
136 unsigned long d_ino;
137 unsigned long d_off;
138 unsigned short d_reclen;
139 char d_name[1];
140};
141
142struct getdents_callback {
Al Viro5c0ba4e2013-05-15 13:52:59 -0400143 struct dir_context ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 struct linux_dirent __user * current_dir;
145 struct linux_dirent __user * previous;
146 int count;
147 int error;
148};
149
Miklos Szeredi30d1df12014-10-30 17:37:34 +0100150static int filldir(struct dir_context *ctx, const char *name, int namlen,
151 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 struct linux_dirent __user * dirent;
Miklos Szeredi30d1df12014-10-30 17:37:34 +0100154 struct getdents_callback *buf =
155 container_of(ctx, struct getdents_callback, ctx);
David Howellsafefdbb2006-10-03 01:13:46 -0700156 unsigned long d_ino;
Kevin Winchester85c9fe82010-08-09 17:20:22 -0700157 int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2,
158 sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 buf->error = -EINVAL; /* only used if we fail.. */
161 if (reclen > buf->count)
162 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -0700163 d_ino = ino;
Al Viro8f3f6552008-08-12 00:28:24 -0400164 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
165 buf->error = -EOVERFLOW;
David Howellsafefdbb2006-10-03 01:13:46 -0700166 return -EOVERFLOW;
Al Viro8f3f6552008-08-12 00:28:24 -0400167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 dirent = buf->previous;
169 if (dirent) {
170 if (__put_user(offset, &dirent->d_off))
171 goto efault;
172 }
173 dirent = buf->current_dir;
David Howellsafefdbb2006-10-03 01:13:46 -0700174 if (__put_user(d_ino, &dirent->d_ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 goto efault;
176 if (__put_user(reclen, &dirent->d_reclen))
177 goto efault;
178 if (copy_to_user(dirent->d_name, name, namlen))
179 goto efault;
180 if (__put_user(0, dirent->d_name + namlen))
181 goto efault;
182 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
183 goto efault;
184 buf->previous = dirent;
185 dirent = (void __user *)dirent + reclen;
186 buf->current_dir = dirent;
187 buf->count -= reclen;
188 return 0;
189efault:
190 buf->error = -EFAULT;
191 return -EFAULT;
192}
193
Heiko Carstens20f37032009-01-14 14:14:23 +0100194SYSCALL_DEFINE3(getdents, unsigned int, fd,
195 struct linux_dirent __user *, dirent, unsigned int, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Al Viro2903ff02012-08-28 12:52:22 -0400197 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct linux_dirent __user * lastdirent;
Al Viroac6614b2013-05-22 22:22:04 -0400199 struct getdents_callback buf = {
200 .ctx.actor = filldir,
201 .count = count,
202 .current_dir = dirent
203 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 int error;
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (!access_ok(VERIFY_WRITE, dirent, count))
Al Viro863ced72012-04-21 18:40:32 -0400207 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Al Viro2903ff02012-08-28 12:52:22 -0400209 f = fdget(fd);
210 if (!f.file)
Al Viro863ced72012-04-21 18:40:32 -0400211 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Al Viro5c0ba4e2013-05-15 13:52:59 -0400213 error = iterate_dir(f.file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -0400214 if (error >= 0)
215 error = buf.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 lastdirent = buf.previous;
217 if (lastdirent) {
Al Virobb6f6192013-05-15 18:49:12 -0400218 if (put_user(buf.ctx.pos, &lastdirent->d_off))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 error = -EFAULT;
220 else
221 error = count - buf.count;
222 }
Al Viro2903ff02012-08-28 12:52:22 -0400223 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return error;
225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227struct getdents_callback64 {
Al Viro5c0ba4e2013-05-15 13:52:59 -0400228 struct dir_context ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 struct linux_dirent64 __user * current_dir;
230 struct linux_dirent64 __user * previous;
231 int count;
232 int error;
233};
234
Miklos Szeredi30d1df12014-10-30 17:37:34 +0100235static int filldir64(struct dir_context *ctx, const char *name, int namlen,
236 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 struct linux_dirent64 __user *dirent;
Miklos Szeredi30d1df12014-10-30 17:37:34 +0100239 struct getdents_callback64 *buf =
240 container_of(ctx, struct getdents_callback64, ctx);
Kevin Winchester85c9fe82010-08-09 17:20:22 -0700241 int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
242 sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 buf->error = -EINVAL; /* only used if we fail.. */
245 if (reclen > buf->count)
246 return -EINVAL;
247 dirent = buf->previous;
248 if (dirent) {
249 if (__put_user(offset, &dirent->d_off))
250 goto efault;
251 }
252 dirent = buf->current_dir;
253 if (__put_user(ino, &dirent->d_ino))
254 goto efault;
255 if (__put_user(0, &dirent->d_off))
256 goto efault;
257 if (__put_user(reclen, &dirent->d_reclen))
258 goto efault;
259 if (__put_user(d_type, &dirent->d_type))
260 goto efault;
261 if (copy_to_user(dirent->d_name, name, namlen))
262 goto efault;
263 if (__put_user(0, dirent->d_name + namlen))
264 goto efault;
265 buf->previous = dirent;
266 dirent = (void __user *)dirent + reclen;
267 buf->current_dir = dirent;
268 buf->count -= reclen;
269 return 0;
270efault:
271 buf->error = -EFAULT;
272 return -EFAULT;
273}
274
Heiko Carstens20f37032009-01-14 14:14:23 +0100275SYSCALL_DEFINE3(getdents64, unsigned int, fd,
276 struct linux_dirent64 __user *, dirent, unsigned int, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Al Viro2903ff02012-08-28 12:52:22 -0400278 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 struct linux_dirent64 __user * lastdirent;
Al Viroac6614b2013-05-22 22:22:04 -0400280 struct getdents_callback64 buf = {
281 .ctx.actor = filldir64,
282 .count = count,
283 .current_dir = dirent
284 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 int error;
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (!access_ok(VERIFY_WRITE, dirent, count))
Al Viro863ced72012-04-21 18:40:32 -0400288 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Al Viro2903ff02012-08-28 12:52:22 -0400290 f = fdget(fd);
291 if (!f.file)
Al Viro863ced72012-04-21 18:40:32 -0400292 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Al Viro5c0ba4e2013-05-15 13:52:59 -0400294 error = iterate_dir(f.file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -0400295 if (error >= 0)
296 error = buf.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 lastdirent = buf.previous;
298 if (lastdirent) {
Al Virobb6f6192013-05-15 18:49:12 -0400299 typeof(lastdirent->d_off) d_off = buf.ctx.pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (__put_user(d_off, &lastdirent->d_off))
Al Viro53c9c5c2008-08-24 07:29:52 -0400301 error = -EFAULT;
302 else
303 error = count - buf.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Al Viro2903ff02012-08-28 12:52:22 -0400305 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return error;
307}