Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/readdir.c |
| 3 | * |
| 4 | * Copyright (C) 1995 Linus Torvalds |
| 5 | */ |
| 6 | |
Kevin Winchester | 85c9fe8 | 2010-08-09 17:20:22 -0700 | [diff] [blame] | 7 | #include <linux/stddef.h> |
Milind Arun Choudhary | 022a169 | 2007-05-08 00:29:02 -0700 | [diff] [blame] | 8 | #include <linux/kernel.h> |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 9 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 23 | int iterate_dir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 25 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | int res = -ENOTDIR; |
Al Viro | bb6f619 | 2013-05-15 18:49:12 -0400 | [diff] [blame^] | 27 | if (!file->f_op || (!file->f_op->readdir && !file->f_op->iterate)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | goto out; |
| 29 | |
| 30 | res = security_file_permission(file, MAY_READ); |
| 31 | if (res) |
| 32 | goto out; |
| 33 | |
Liam R. Howlett | da78451 | 2007-12-06 17:39:54 -0500 | [diff] [blame] | 34 | res = mutex_lock_killable(&inode->i_mutex); |
| 35 | if (res) |
| 36 | goto out; |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | res = -ENOENT; |
| 39 | if (!IS_DEADDIR(inode)) { |
Al Viro | bb6f619 | 2013-05-15 18:49:12 -0400 | [diff] [blame^] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | file_accessed(file); |
| 49 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 50 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | out: |
| 52 | return res; |
| 53 | } |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 54 | EXPORT_SYMBOL(iterate_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 { |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 75 | struct dir_context ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 81 | u64 ino, unsigned int d_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 83 | struct readdir_callback *buf = (struct readdir_callback *) __buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | struct old_linux_dirent __user * dirent; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 85 | unsigned long d_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | if (buf->result) |
| 88 | return -EINVAL; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 89 | d_ino = ino; |
Al Viro | 8f3f655 | 2008-08-12 00:28:24 -0400 | [diff] [blame] | 90 | if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) { |
| 91 | buf->result = -EOVERFLOW; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 92 | return -EOVERFLOW; |
Al Viro | 8f3f655 | 2008-08-12 00:28:24 -0400 | [diff] [blame] | 93 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 100 | if ( __put_user(d_ino, &dirent->d_ino) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Heiko Carstens | d4e8204 | 2009-01-14 14:14:34 +0100 | [diff] [blame] | 112 | SYSCALL_DEFINE3(old_readdir, unsigned int, fd, |
| 113 | struct old_linux_dirent __user *, dirent, unsigned int, count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
| 115 | int error; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 116 | struct fd f = fdget(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | struct readdir_callback buf; |
| 118 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 119 | if (!f.file) |
Al Viro | 863ced7 | 2012-04-21 18:40:32 -0400 | [diff] [blame] | 120 | return -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 122 | buf.ctx.actor = fillonedir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | buf.result = 0; |
| 124 | buf.dirent = dirent; |
| 125 | |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 126 | error = iterate_dir(f.file, &buf.ctx); |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 127 | if (buf.result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | error = buf.result; |
| 129 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 130 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | return error; |
| 132 | } |
| 133 | |
| 134 | #endif /* __ARCH_WANT_OLD_READDIR */ |
| 135 | |
| 136 | /* |
| 137 | * New, all-improved, singing, dancing, iBCS2-compliant getdents() |
| 138 | * interface. |
| 139 | */ |
| 140 | struct linux_dirent { |
| 141 | unsigned long d_ino; |
| 142 | unsigned long d_off; |
| 143 | unsigned short d_reclen; |
| 144 | char d_name[1]; |
| 145 | }; |
| 146 | |
| 147 | struct getdents_callback { |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 148 | struct dir_context ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | struct linux_dirent __user * current_dir; |
| 150 | struct linux_dirent __user * previous; |
| 151 | int count; |
| 152 | int error; |
| 153 | }; |
| 154 | |
| 155 | static int filldir(void * __buf, const char * name, int namlen, loff_t offset, |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 156 | u64 ino, unsigned int d_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
| 158 | struct linux_dirent __user * dirent; |
| 159 | struct getdents_callback * buf = (struct getdents_callback *) __buf; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 160 | unsigned long d_ino; |
Kevin Winchester | 85c9fe8 | 2010-08-09 17:20:22 -0700 | [diff] [blame] | 161 | int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2, |
| 162 | sizeof(long)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | buf->error = -EINVAL; /* only used if we fail.. */ |
| 165 | if (reclen > buf->count) |
| 166 | return -EINVAL; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 167 | d_ino = ino; |
Al Viro | 8f3f655 | 2008-08-12 00:28:24 -0400 | [diff] [blame] | 168 | if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) { |
| 169 | buf->error = -EOVERFLOW; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 170 | return -EOVERFLOW; |
Al Viro | 8f3f655 | 2008-08-12 00:28:24 -0400 | [diff] [blame] | 171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | dirent = buf->previous; |
| 173 | if (dirent) { |
| 174 | if (__put_user(offset, &dirent->d_off)) |
| 175 | goto efault; |
| 176 | } |
| 177 | dirent = buf->current_dir; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 178 | if (__put_user(d_ino, &dirent->d_ino)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | goto efault; |
| 180 | if (__put_user(reclen, &dirent->d_reclen)) |
| 181 | goto efault; |
| 182 | if (copy_to_user(dirent->d_name, name, namlen)) |
| 183 | goto efault; |
| 184 | if (__put_user(0, dirent->d_name + namlen)) |
| 185 | goto efault; |
| 186 | if (__put_user(d_type, (char __user *) dirent + reclen - 1)) |
| 187 | goto efault; |
| 188 | buf->previous = dirent; |
| 189 | dirent = (void __user *)dirent + reclen; |
| 190 | buf->current_dir = dirent; |
| 191 | buf->count -= reclen; |
| 192 | return 0; |
| 193 | efault: |
| 194 | buf->error = -EFAULT; |
| 195 | return -EFAULT; |
| 196 | } |
| 197 | |
Heiko Carstens | 20f3703 | 2009-01-14 14:14:23 +0100 | [diff] [blame] | 198 | SYSCALL_DEFINE3(getdents, unsigned int, fd, |
| 199 | struct linux_dirent __user *, dirent, unsigned int, count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 201 | struct fd f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | struct linux_dirent __user * lastdirent; |
| 203 | struct getdents_callback buf; |
| 204 | int error; |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | if (!access_ok(VERIFY_WRITE, dirent, count)) |
Al Viro | 863ced7 | 2012-04-21 18:40:32 -0400 | [diff] [blame] | 207 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 209 | f = fdget(fd); |
| 210 | if (!f.file) |
Al Viro | 863ced7 | 2012-04-21 18:40:32 -0400 | [diff] [blame] | 211 | return -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | buf.current_dir = dirent; |
| 214 | buf.previous = NULL; |
| 215 | buf.count = count; |
| 216 | buf.error = 0; |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 217 | buf.ctx.actor = filldir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 219 | error = iterate_dir(f.file, &buf.ctx); |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 220 | if (error >= 0) |
| 221 | error = buf.error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | lastdirent = buf.previous; |
| 223 | if (lastdirent) { |
Al Viro | bb6f619 | 2013-05-15 18:49:12 -0400 | [diff] [blame^] | 224 | if (put_user(buf.ctx.pos, &lastdirent->d_off)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | error = -EFAULT; |
| 226 | else |
| 227 | error = count - buf.count; |
| 228 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 229 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | return error; |
| 231 | } |
| 232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | struct getdents_callback64 { |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 234 | struct dir_context ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | struct linux_dirent64 __user * current_dir; |
| 236 | struct linux_dirent64 __user * previous; |
| 237 | int count; |
| 238 | int error; |
| 239 | }; |
| 240 | |
| 241 | static int filldir64(void * __buf, const char * name, int namlen, loff_t offset, |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 242 | u64 ino, unsigned int d_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | { |
| 244 | struct linux_dirent64 __user *dirent; |
| 245 | struct getdents_callback64 * buf = (struct getdents_callback64 *) __buf; |
Kevin Winchester | 85c9fe8 | 2010-08-09 17:20:22 -0700 | [diff] [blame] | 246 | int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1, |
| 247 | sizeof(u64)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 249 | buf->error = -EINVAL; /* only used if we fail.. */ |
| 250 | if (reclen > buf->count) |
| 251 | return -EINVAL; |
| 252 | dirent = buf->previous; |
| 253 | if (dirent) { |
| 254 | if (__put_user(offset, &dirent->d_off)) |
| 255 | goto efault; |
| 256 | } |
| 257 | dirent = buf->current_dir; |
| 258 | if (__put_user(ino, &dirent->d_ino)) |
| 259 | goto efault; |
| 260 | if (__put_user(0, &dirent->d_off)) |
| 261 | goto efault; |
| 262 | if (__put_user(reclen, &dirent->d_reclen)) |
| 263 | goto efault; |
| 264 | if (__put_user(d_type, &dirent->d_type)) |
| 265 | goto efault; |
| 266 | if (copy_to_user(dirent->d_name, name, namlen)) |
| 267 | goto efault; |
| 268 | if (__put_user(0, dirent->d_name + namlen)) |
| 269 | goto efault; |
| 270 | buf->previous = dirent; |
| 271 | dirent = (void __user *)dirent + reclen; |
| 272 | buf->current_dir = dirent; |
| 273 | buf->count -= reclen; |
| 274 | return 0; |
| 275 | efault: |
| 276 | buf->error = -EFAULT; |
| 277 | return -EFAULT; |
| 278 | } |
| 279 | |
Heiko Carstens | 20f3703 | 2009-01-14 14:14:23 +0100 | [diff] [blame] | 280 | SYSCALL_DEFINE3(getdents64, unsigned int, fd, |
| 281 | struct linux_dirent64 __user *, dirent, unsigned int, count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 283 | struct fd f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | struct linux_dirent64 __user * lastdirent; |
| 285 | struct getdents_callback64 buf; |
| 286 | int error; |
| 287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | if (!access_ok(VERIFY_WRITE, dirent, count)) |
Al Viro | 863ced7 | 2012-04-21 18:40:32 -0400 | [diff] [blame] | 289 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 291 | f = fdget(fd); |
| 292 | if (!f.file) |
Al Viro | 863ced7 | 2012-04-21 18:40:32 -0400 | [diff] [blame] | 293 | return -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
| 295 | buf.current_dir = dirent; |
| 296 | buf.previous = NULL; |
| 297 | buf.count = count; |
| 298 | buf.error = 0; |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 299 | buf.ctx.actor = filldir64; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 301 | error = iterate_dir(f.file, &buf.ctx); |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 302 | if (error >= 0) |
| 303 | error = buf.error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | lastdirent = buf.previous; |
| 305 | if (lastdirent) { |
Al Viro | bb6f619 | 2013-05-15 18:49:12 -0400 | [diff] [blame^] | 306 | typeof(lastdirent->d_off) d_off = buf.ctx.pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | if (__put_user(d_off, &lastdirent->d_off)) |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 308 | error = -EFAULT; |
| 309 | else |
| 310 | error = count - buf.count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 312 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | return error; |
| 314 | } |