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