ext4: don't save the error information if the block device is read-only
[firefly-linux-kernel-4.4.55.git] / fs / readdir.c
index 5b620a2b45e62e2c533eb897d8c58acb91a569aa..d46eca8567a413a8f081b4c8a5272a60a2039437 100644 (file)
@@ -24,7 +24,7 @@ int iterate_dir(struct file *file, struct dir_context *ctx)
 {
        struct inode *inode = file_inode(file);
        int res = -ENOTDIR;
-       if (!file->f_op || !file->f_op->readdir)
+       if (!file->f_op || (!file->f_op->readdir && !file->f_op->iterate))
                goto out;
 
        res = security_file_permission(file, MAY_READ);
@@ -37,7 +37,14 @@ int iterate_dir(struct file *file, struct dir_context *ctx)
 
        res = -ENOENT;
        if (!IS_DEADDIR(inode)) {
-               res = file->f_op->readdir(file, ctx, ctx->actor);
+               if (file->f_op->iterate) {
+                       ctx->pos = file->f_pos;
+                       res = file->f_op->iterate(file, ctx);
+                       file->f_pos = ctx->pos;
+               } else {
+                       res = file->f_op->readdir(file, ctx, ctx->actor);
+                       ctx->pos = file->f_pos;
+               }
                file_accessed(file);
        }
        mutex_unlock(&inode->i_mutex);
@@ -107,15 +114,14 @@ SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
 {
        int error;
        struct fd f = fdget(fd);
-       struct readdir_callback buf;
+       struct readdir_callback buf = {
+               .ctx.actor = fillonedir,
+               .dirent = dirent
+       };
 
        if (!f.file)
                return -EBADF;
 
-       buf.ctx.actor = fillonedir;
-       buf.result = 0;
-       buf.dirent = dirent;
-
        error = iterate_dir(f.file, &buf.ctx);
        if (buf.result)
                error = buf.result;
@@ -193,7 +199,11 @@ SYSCALL_DEFINE3(getdents, unsigned int, fd,
 {
        struct fd f;
        struct linux_dirent __user * lastdirent;
-       struct getdents_callback buf;
+       struct getdents_callback buf = {
+               .ctx.actor = filldir,
+               .count = count,
+               .current_dir = dirent
+       };
        int error;
 
        if (!access_ok(VERIFY_WRITE, dirent, count))
@@ -203,18 +213,12 @@ SYSCALL_DEFINE3(getdents, unsigned int, fd,
        if (!f.file)
                return -EBADF;
 
-       buf.current_dir = dirent;
-       buf.previous = NULL;
-       buf.count = count;
-       buf.error = 0;
-       buf.ctx.actor = filldir;
-
        error = iterate_dir(f.file, &buf.ctx);
        if (error >= 0)
                error = buf.error;
        lastdirent = buf.previous;
        if (lastdirent) {
-               if (put_user(f.file->f_pos, &lastdirent->d_off))
+               if (put_user(buf.ctx.pos, &lastdirent->d_off))
                        error = -EFAULT;
                else
                        error = count - buf.count;
@@ -275,7 +279,11 @@ SYSCALL_DEFINE3(getdents64, unsigned int, fd,
 {
        struct fd f;
        struct linux_dirent64 __user * lastdirent;
-       struct getdents_callback64 buf;
+       struct getdents_callback64 buf = {
+               .ctx.actor = filldir64,
+               .count = count,
+               .current_dir = dirent
+       };
        int error;
 
        if (!access_ok(VERIFY_WRITE, dirent, count))
@@ -285,18 +293,12 @@ SYSCALL_DEFINE3(getdents64, unsigned int, fd,
        if (!f.file)
                return -EBADF;
 
-       buf.current_dir = dirent;
-       buf.previous = NULL;
-       buf.count = count;
-       buf.error = 0;
-       buf.ctx.actor = filldir64;
-
        error = iterate_dir(f.file, &buf.ctx);
        if (error >= 0)
                error = buf.error;
        lastdirent = buf.previous;
        if (lastdirent) {
-               typeof(lastdirent->d_off) d_off = f.file->f_pos;
+               typeof(lastdirent->d_off) d_off = buf.ctx.pos;
                if (__put_user(d_off, &lastdirent->d_off))
                        error = -EFAULT;
                else