rk312x: add psci support
[firefly-linux-kernel-4.4.55.git] / fs / readdir.c
1 /*
2  *  linux/fs/readdir.c
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  */
6
7 #include <linux/stddef.h>
8 #include <linux/kernel.h>
9 #include <linux/export.h>
10 #include <linux/time.h>
11 #include <linux/mm.h>
12 #include <linux/errno.h>
13 #include <linux/stat.h>
14 #include <linux/file.h>
15 #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
23 int iterate_dir(struct file *file, struct dir_context *ctx)
24 {
25         struct inode *inode = file_inode(file);
26         int res = -ENOTDIR;
27         if (!file->f_op || (!file->f_op->readdir && !file->f_op->iterate))
28                 goto out;
29
30         res = security_file_permission(file, MAY_READ);
31         if (res)
32                 goto out;
33
34         res = mutex_lock_killable(&inode->i_mutex);
35         if (res)
36                 goto out;
37
38         res = -ENOENT;
39         if (!IS_DEADDIR(inode)) {
40                 if (file->f_op->iterate) {
41                         ctx->pos = file->f_pos;
42                         res = file->f_op->iterate(file, ctx);
43                         file->f_pos = ctx->pos;
44                 } else {
45                         res = file->f_op->readdir(file, ctx, ctx->actor);
46                         ctx->pos = file->f_pos;
47                 }
48                 file_accessed(file);
49         }
50         mutex_unlock(&inode->i_mutex);
51 out:
52         return res;
53 }
54 EXPORT_SYMBOL(iterate_dir);
55
56 /*
57  * Traditional linux readdir() handling..
58  *
59  * "count=1" is a special case, meaning that the buffer is one
60  * dirent-structure in size and that the code can't handle more
61  * anyway. Thus the special "fillonedir()" function for that
62  * case (the low-level handlers don't need to care about this).
63  */
64
65 #ifdef __ARCH_WANT_OLD_READDIR
66
67 struct old_linux_dirent {
68         unsigned long   d_ino;
69         unsigned long   d_offset;
70         unsigned short  d_namlen;
71         char            d_name[1];
72 };
73
74 struct readdir_callback {
75         struct dir_context ctx;
76         struct old_linux_dirent __user * dirent;
77         int result;
78 };
79
80 static int fillonedir(void * __buf, const char * name, int namlen, loff_t offset,
81                       u64 ino, unsigned int d_type)
82 {
83         struct readdir_callback *buf = (struct readdir_callback *) __buf;
84         struct old_linux_dirent __user * dirent;
85         unsigned long d_ino;
86
87         if (buf->result)
88                 return -EINVAL;
89         d_ino = ino;
90         if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
91                 buf->result = -EOVERFLOW;
92                 return -EOVERFLOW;
93         }
94         buf->result++;
95         dirent = buf->dirent;
96         if (!access_ok(VERIFY_WRITE, dirent,
97                         (unsigned long)(dirent->d_name + namlen + 1) -
98                                 (unsigned long)dirent))
99                 goto efault;
100         if (    __put_user(d_ino, &dirent->d_ino) ||
101                 __put_user(offset, &dirent->d_offset) ||
102                 __put_user(namlen, &dirent->d_namlen) ||
103                 __copy_to_user(dirent->d_name, name, namlen) ||
104                 __put_user(0, dirent->d_name + namlen))
105                 goto efault;
106         return 0;
107 efault:
108         buf->result = -EFAULT;
109         return -EFAULT;
110 }
111
112 SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
113                 struct old_linux_dirent __user *, dirent, unsigned int, count)
114 {
115         int error;
116         struct fd f = fdget(fd);
117         struct readdir_callback buf = {
118                 .ctx.actor = fillonedir,
119                 .dirent = dirent
120         };
121
122         if (!f.file)
123                 return -EBADF;
124
125         error = iterate_dir(f.file, &buf.ctx);
126         if (buf.result)
127                 error = buf.result;
128
129         fdput(f);
130         return error;
131 }
132
133 #endif /* __ARCH_WANT_OLD_READDIR */
134
135 /*
136  * New, all-improved, singing, dancing, iBCS2-compliant getdents()
137  * interface. 
138  */
139 struct linux_dirent {
140         unsigned long   d_ino;
141         unsigned long   d_off;
142         unsigned short  d_reclen;
143         char            d_name[1];
144 };
145
146 struct getdents_callback {
147         struct dir_context ctx;
148         struct linux_dirent __user * current_dir;
149         struct linux_dirent __user * previous;
150         int count;
151         int error;
152 };
153
154 static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
155                    u64 ino, unsigned int d_type)
156 {
157         struct linux_dirent __user * dirent;
158         struct getdents_callback * buf = (struct getdents_callback *) __buf;
159         unsigned long d_ino;
160         int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2,
161                 sizeof(long));
162
163         buf->error = -EINVAL;   /* only used if we fail.. */
164         if (reclen > buf->count)
165                 return -EINVAL;
166         d_ino = ino;
167         if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
168                 buf->error = -EOVERFLOW;
169                 return -EOVERFLOW;
170         }
171         dirent = buf->previous;
172         if (dirent) {
173                 if (__put_user(offset, &dirent->d_off))
174                         goto efault;
175         }
176         dirent = buf->current_dir;
177         if (__put_user(d_ino, &dirent->d_ino))
178                 goto efault;
179         if (__put_user(reclen, &dirent->d_reclen))
180                 goto efault;
181         if (copy_to_user(dirent->d_name, name, namlen))
182                 goto efault;
183         if (__put_user(0, dirent->d_name + namlen))
184                 goto efault;
185         if (__put_user(d_type, (char __user *) dirent + reclen - 1))
186                 goto efault;
187         buf->previous = dirent;
188         dirent = (void __user *)dirent + reclen;
189         buf->current_dir = dirent;
190         buf->count -= reclen;
191         return 0;
192 efault:
193         buf->error = -EFAULT;
194         return -EFAULT;
195 }
196
197 SYSCALL_DEFINE3(getdents, unsigned int, fd,
198                 struct linux_dirent __user *, dirent, unsigned int, count)
199 {
200         struct fd f;
201         struct linux_dirent __user * lastdirent;
202         struct getdents_callback buf = {
203                 .ctx.actor = filldir,
204                 .count = count,
205                 .current_dir = dirent
206         };
207         int error;
208
209         if (!access_ok(VERIFY_WRITE, dirent, count))
210                 return -EFAULT;
211
212         f = fdget(fd);
213         if (!f.file)
214                 return -EBADF;
215
216         error = iterate_dir(f.file, &buf.ctx);
217         if (error >= 0)
218                 error = buf.error;
219         lastdirent = buf.previous;
220         if (lastdirent) {
221                 if (put_user(buf.ctx.pos, &lastdirent->d_off))
222                         error = -EFAULT;
223                 else
224                         error = count - buf.count;
225         }
226         fdput(f);
227         return error;
228 }
229
230 struct getdents_callback64 {
231         struct dir_context ctx;
232         struct linux_dirent64 __user * current_dir;
233         struct linux_dirent64 __user * previous;
234         int count;
235         int error;
236 };
237
238 static int filldir64(void * __buf, const char * name, int namlen, loff_t offset,
239                      u64 ino, unsigned int d_type)
240 {
241         struct linux_dirent64 __user *dirent;
242         struct getdents_callback64 * buf = (struct getdents_callback64 *) __buf;
243         int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
244                 sizeof(u64));
245
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;
272 efault:
273         buf->error = -EFAULT;
274         return -EFAULT;
275 }
276
277 SYSCALL_DEFINE3(getdents64, unsigned int, fd,
278                 struct linux_dirent64 __user *, dirent, unsigned int, count)
279 {
280         struct fd f;
281         struct linux_dirent64 __user * lastdirent;
282         struct getdents_callback64 buf = {
283                 .ctx.actor = filldir64,
284                 .count = count,
285                 .current_dir = dirent
286         };
287         int error;
288
289         if (!access_ok(VERIFY_WRITE, dirent, count))
290                 return -EFAULT;
291
292         f = fdget(fd);
293         if (!f.file)
294                 return -EBADF;
295
296         error = iterate_dir(f.file, &buf.ctx);
297         if (error >= 0)
298                 error = buf.error;
299         lastdirent = buf.previous;
300         if (lastdirent) {
301                 typeof(lastdirent->d_off) d_off = buf.ctx.pos;
302                 if (__put_user(d_off, &lastdirent->d_off))
303                         error = -EFAULT;
304                 else
305                         error = count - buf.count;
306         }
307         fdput(f);
308         return error;
309 }