blob: e69ef3b79787b9fb4f02ab65b35c28571c9df1a0 [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>
Heinrich Schuchardtd4c7cf62014-06-04 16:05:41 -070016#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/dirent.h>
18#include <linux/security.h>
19#include <linux/syscalls.h>
20#include <linux/unistd.h>
21
22#include <asm/uaccess.h>
23
Al Viro5c0ba4e2013-05-15 13:52:59 -040024int iterate_dir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Al Viro496ad9a2013-01-23 17:07:38 -050026 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 int res = -ENOTDIR;
Al Viro72c2d532013-09-22 16:27:52 -040028 if (!file->f_op->iterate)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 goto out;
30
31 res = security_file_permission(file, MAY_READ);
32 if (res)
33 goto out;
34
Liam R. Howlettda784512007-12-06 17:39:54 -050035 res = mutex_lock_killable(&inode->i_mutex);
36 if (res)
37 goto out;
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 res = -ENOENT;
40 if (!IS_DEADDIR(inode)) {
Al Viro2233f312013-05-22 21:44:23 -040041 ctx->pos = file->f_pos;
42 res = file->f_op->iterate(file, ctx);
43 file->f_pos = ctx->pos;
Heinrich Schuchardtd4c7cf62014-06-04 16:05:41 -070044 fsnotify_access(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 file_accessed(file);
46 }
Al Viro59551022016-01-22 15:40:57 -050047 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048out:
49 return res;
50}
Al Viro5c0ba4e2013-05-15 13:52:59 -040051EXPORT_SYMBOL(iterate_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/*
54 * Traditional linux readdir() handling..
55 *
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).
60 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#ifdef __ARCH_WANT_OLD_READDIR
63
64struct old_linux_dirent {
65 unsigned long d_ino;
66 unsigned long d_offset;
67 unsigned short d_namlen;
68 char d_name[1];
69};
70
71struct readdir_callback {
Al Viro5c0ba4e2013-05-15 13:52:59 -040072 struct dir_context ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct old_linux_dirent __user * dirent;
74 int result;
75};
76
Miklos Szerediac7576f2014-10-30 17:37:34 +010077static int fillonedir(struct dir_context *ctx, const char *name, int namlen,
78 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Miklos Szerediac7576f2014-10-30 17:37:34 +010080 struct readdir_callback *buf =
81 container_of(ctx, struct readdir_callback, ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 struct old_linux_dirent __user * dirent;
David Howellsafefdbb2006-10-03 01:13:46 -070083 unsigned long d_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 if (buf->result)
86 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -070087 d_ino = ino;
Al Viro8f3f6552008-08-12 00:28:24 -040088 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
89 buf->result = -EOVERFLOW;
David Howellsafefdbb2006-10-03 01:13:46 -070090 return -EOVERFLOW;
Al Viro8f3f6552008-08-12 00:28:24 -040091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 buf->result++;
93 dirent = buf->dirent;
94 if (!access_ok(VERIFY_WRITE, dirent,
95 (unsigned long)(dirent->d_name + namlen + 1) -
96 (unsigned long)dirent))
97 goto efault;
David Howellsafefdbb2006-10-03 01:13:46 -070098 if ( __put_user(d_ino, &dirent->d_ino) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 __put_user(offset, &dirent->d_offset) ||
100 __put_user(namlen, &dirent->d_namlen) ||
101 __copy_to_user(dirent->d_name, name, namlen) ||
102 __put_user(0, dirent->d_name + namlen))
103 goto efault;
104 return 0;
105efault:
106 buf->result = -EFAULT;
107 return -EFAULT;
108}
109
Heiko Carstensd4e82042009-01-14 14:14:34 +0100110SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
111 struct old_linux_dirent __user *, dirent, unsigned int, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 int error;
Al Viro2903ff02012-08-28 12:52:22 -0400114 struct fd f = fdget(fd);
Al Viroac6614b2013-05-22 22:22:04 -0400115 struct readdir_callback buf = {
116 .ctx.actor = fillonedir,
117 .dirent = dirent
118 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Al Viro2903ff02012-08-28 12:52:22 -0400120 if (!f.file)
Al Viro863ced72012-04-21 18:40:32 -0400121 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Al Viro5c0ba4e2013-05-15 13:52:59 -0400123 error = iterate_dir(f.file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -0400124 if (buf.result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 error = buf.result;
126
Al Viro2903ff02012-08-28 12:52:22 -0400127 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return error;
129}
130
131#endif /* __ARCH_WANT_OLD_READDIR */
132
133/*
134 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
135 * interface.
136 */
137struct linux_dirent {
138 unsigned long d_ino;
139 unsigned long d_off;
140 unsigned short d_reclen;
141 char d_name[1];
142};
143
144struct getdents_callback {
Al Viro5c0ba4e2013-05-15 13:52:59 -0400145 struct dir_context ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct linux_dirent __user * current_dir;
147 struct linux_dirent __user * previous;
148 int count;
149 int error;
150};
151
Miklos Szerediac7576f2014-10-30 17:37:34 +0100152static int filldir(struct dir_context *ctx, const char *name, int namlen,
153 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 struct linux_dirent __user * dirent;
Miklos Szerediac7576f2014-10-30 17:37:34 +0100156 struct getdents_callback *buf =
157 container_of(ctx, struct getdents_callback, ctx);
David Howellsafefdbb2006-10-03 01:13:46 -0700158 unsigned long d_ino;
Kevin Winchester85c9fe82010-08-09 17:20:22 -0700159 int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2,
160 sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 buf->error = -EINVAL; /* only used if we fail.. */
163 if (reclen > buf->count)
164 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -0700165 d_ino = ino;
Al Viro8f3f6552008-08-12 00:28:24 -0400166 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
167 buf->error = -EOVERFLOW;
David Howellsafefdbb2006-10-03 01:13:46 -0700168 return -EOVERFLOW;
Al Viro8f3f6552008-08-12 00:28:24 -0400169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 dirent = buf->previous;
171 if (dirent) {
172 if (__put_user(offset, &dirent->d_off))
173 goto efault;
174 }
175 dirent = buf->current_dir;
David Howellsafefdbb2006-10-03 01:13:46 -0700176 if (__put_user(d_ino, &dirent->d_ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 goto efault;
178 if (__put_user(reclen, &dirent->d_reclen))
179 goto efault;
180 if (copy_to_user(dirent->d_name, name, namlen))
181 goto efault;
182 if (__put_user(0, dirent->d_name + namlen))
183 goto efault;
184 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
185 goto efault;
186 buf->previous = dirent;
187 dirent = (void __user *)dirent + reclen;
188 buf->current_dir = dirent;
189 buf->count -= reclen;
190 return 0;
191efault:
192 buf->error = -EFAULT;
193 return -EFAULT;
194}
195
Heiko Carstens20f37032009-01-14 14:14:23 +0100196SYSCALL_DEFINE3(getdents, unsigned int, fd,
197 struct linux_dirent __user *, dirent, unsigned int, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Al Viro2903ff02012-08-28 12:52:22 -0400199 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 struct linux_dirent __user * lastdirent;
Al Viroac6614b2013-05-22 22:22:04 -0400201 struct getdents_callback buf = {
202 .ctx.actor = filldir,
203 .count = count,
204 .current_dir = dirent
205 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 int error;
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (!access_ok(VERIFY_WRITE, dirent, count))
Al Viro863ced72012-04-21 18:40:32 -0400209 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Al Viro2903ff02012-08-28 12:52:22 -0400211 f = fdget(fd);
212 if (!f.file)
Al Viro863ced72012-04-21 18:40:32 -0400213 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Al Viro5c0ba4e2013-05-15 13:52:59 -0400215 error = iterate_dir(f.file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -0400216 if (error >= 0)
217 error = buf.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 lastdirent = buf.previous;
219 if (lastdirent) {
Al Virobb6f6192013-05-15 18:49:12 -0400220 if (put_user(buf.ctx.pos, &lastdirent->d_off))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 error = -EFAULT;
222 else
223 error = count - buf.count;
224 }
Al Viro2903ff02012-08-28 12:52:22 -0400225 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return error;
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229struct getdents_callback64 {
Al Viro5c0ba4e2013-05-15 13:52:59 -0400230 struct dir_context ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct linux_dirent64 __user * current_dir;
232 struct linux_dirent64 __user * previous;
233 int count;
234 int error;
235};
236
Miklos Szerediac7576f2014-10-30 17:37:34 +0100237static int filldir64(struct dir_context *ctx, const char *name, int namlen,
238 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct linux_dirent64 __user *dirent;
Miklos Szerediac7576f2014-10-30 17:37:34 +0100241 struct getdents_callback64 *buf =
242 container_of(ctx, struct getdents_callback64, ctx);
Kevin Winchester85c9fe82010-08-09 17:20:22 -0700243 int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
244 sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 buf->error = -EINVAL; /* only used if we fail.. */
247 if (reclen > buf->count)
248 return -EINVAL;
249 dirent = buf->previous;
250 if (dirent) {
251 if (__put_user(offset, &dirent->d_off))
252 goto efault;
253 }
254 dirent = buf->current_dir;
255 if (__put_user(ino, &dirent->d_ino))
256 goto efault;
257 if (__put_user(0, &dirent->d_off))
258 goto efault;
259 if (__put_user(reclen, &dirent->d_reclen))
260 goto efault;
261 if (__put_user(d_type, &dirent->d_type))
262 goto efault;
263 if (copy_to_user(dirent->d_name, name, namlen))
264 goto efault;
265 if (__put_user(0, dirent->d_name + namlen))
266 goto efault;
267 buf->previous = dirent;
268 dirent = (void __user *)dirent + reclen;
269 buf->current_dir = dirent;
270 buf->count -= reclen;
271 return 0;
272efault:
273 buf->error = -EFAULT;
274 return -EFAULT;
275}
276
Heiko Carstens20f37032009-01-14 14:14:23 +0100277SYSCALL_DEFINE3(getdents64, unsigned int, fd,
278 struct linux_dirent64 __user *, dirent, unsigned int, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Al Viro2903ff02012-08-28 12:52:22 -0400280 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 struct linux_dirent64 __user * lastdirent;
Al Viroac6614b2013-05-22 22:22:04 -0400282 struct getdents_callback64 buf = {
283 .ctx.actor = filldir64,
284 .count = count,
285 .current_dir = dirent
286 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 int error;
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (!access_ok(VERIFY_WRITE, dirent, count))
Al Viro863ced72012-04-21 18:40:32 -0400290 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Al Viro2903ff02012-08-28 12:52:22 -0400292 f = fdget(fd);
293 if (!f.file)
Al Viro863ced72012-04-21 18:40:32 -0400294 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Al Viro5c0ba4e2013-05-15 13:52:59 -0400296 error = iterate_dir(f.file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -0400297 if (error >= 0)
298 error = buf.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 lastdirent = buf.previous;
300 if (lastdirent) {
Al Virobb6f6192013-05-15 18:49:12 -0400301 typeof(lastdirent->d_off) d_off = buf.ctx.pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (__put_user(d_off, &lastdirent->d_off))
Al Viro53c9c5c2008-08-24 07:29:52 -0400303 error = -EFAULT;
304 else
305 error = count - buf.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
Al Viro2903ff02012-08-28 12:52:22 -0400307 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return error;
309}