NFS: Cleanup file handle allocations in fs/nfs/super.c
[safe/jmp/linux-2.6] / fs / reiserfs / dir.c
index 973c819..0793044 100644 (file)
@@ -2,17 +2,16 @@
  * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
  */
 
-#include <linux/config.h>
 #include <linux/string.h>
 #include <linux/errno.h>
 #include <linux/fs.h>
 #include <linux/reiserfs_fs.h>
 #include <linux/stat.h>
-#include <linux/smp_lock.h>
 #include <linux/buffer_head.h>
+#include <linux/slab.h>
 #include <asm/uaccess.h>
 
-extern struct reiserfs_key MIN_KEY;
+extern const struct reiserfs_key MIN_KEY;
 
 static int reiserfs_readdir(struct file *, void *, filldir_t);
 static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry,
@@ -22,7 +21,10 @@ const struct file_operations reiserfs_dir_operations = {
        .read = generic_read_dir,
        .readdir = reiserfs_readdir,
        .fsync = reiserfs_dir_fsync,
-       .ioctl = reiserfs_ioctl,
+       .unlocked_ioctl = reiserfs_ioctl,
+#ifdef CONFIG_COMPAT
+       .compat_ioctl = reiserfs_compat_ioctl,
+#endif
 };
 
 static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry,
@@ -40,10 +42,18 @@ static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry,
 
 #define store_ih(where,what) copy_item_head (where, what)
 
-//
-static int reiserfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
+static inline bool is_privroot_deh(struct dentry *dir,
+                                  struct reiserfs_de_head *deh)
 {
-       struct inode *inode = filp->f_dentry->d_inode;
+       struct dentry *privroot = REISERFS_SB(dir->d_sb)->priv_root;
+       return (dir == dir->d_parent && privroot->d_inode &&
+               deh->deh_objectid == INODE_PKEY(privroot->d_inode)->k_objectid);
+}
+
+int reiserfs_readdir_dentry(struct dentry *dentry, void *dirent,
+                          filldir_t filldir, loff_t *pos)
+{
+       struct inode *inode = dentry->d_inode;
        struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
        INITIALIZE_PATH(path_to_entry);
        struct buffer_head *bh;
@@ -63,13 +73,9 @@ static int reiserfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
 
        /* form key for search the next directory entry using f_pos field of
           file structure */
-       make_cpu_key(&pos_key, inode,
-                    (filp->f_pos) ? (filp->f_pos) : DOT_OFFSET, TYPE_DIRENTRY,
-                    3);
+       make_cpu_key(&pos_key, inode, *pos ?: DOT_OFFSET, TYPE_DIRENTRY, 3);
        next_pos = cpu_key_k_offset(&pos_key);
 
-       /*  reiserfs_warning (inode->i_sb, "reiserfs_readdir 1: f_pos = %Ld", filp->f_pos); */
-
        path_to_entry.reada = PATH_READA;
        while (1) {
              research:
@@ -120,6 +126,16 @@ static int reiserfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
                                        continue;
                                d_reclen = entry_length(bh, ih, entry_num);
                                d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
+
+                               if (d_reclen <= 0 ||
+                                   d_name + d_reclen > bh->b_data + bh->b_size) {
+                                       /* There is corrupted data in entry,
+                                        * We'd better stop here */
+                                       pathrelse(&path_to_entry);
+                                       ret = -EIO;
+                                       goto out;
+                               }
+
                                if (!d_name[d_reclen - 1])
                                        d_reclen = strlen(d_name);
 
@@ -131,21 +147,11 @@ static int reiserfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
                                }
 
                                /* Ignore the .reiserfs_priv entry */
-                               if (reiserfs_xattrs(inode->i_sb) &&
-                                   !old_format_only(inode->i_sb) &&
-                                   filp->f_dentry == inode->i_sb->s_root &&
-                                   REISERFS_SB(inode->i_sb)->priv_root &&
-                                   REISERFS_SB(inode->i_sb)->priv_root->d_inode
-                                   && deh_objectid(deh) ==
-                                   le32_to_cpu(INODE_PKEY
-                                               (REISERFS_SB(inode->i_sb)->
-                                                priv_root->d_inode)->
-                                               k_objectid)) {
+                               if (is_privroot_deh(dentry, deh))
                                        continue;
-                               }
 
                                d_off = deh_offset(deh);
-                               filp->f_pos = d_off;
+                               *pos = d_off;
                                d_ino = deh_objectid(deh);
                                if (d_reclen <= 32) {
                                        local_buf = small_buf;
@@ -167,14 +173,22 @@ static int reiserfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
                                // user space buffer is swapped out. At that time
                                // entry can move to somewhere else
                                memcpy(local_buf, d_name, d_reclen);
+
+                               /*
+                                * Since filldir might sleep, we can release
+                                * the write lock here for other waiters
+                                */
+                               reiserfs_write_unlock(inode->i_sb);
                                if (filldir
                                    (dirent, local_buf, d_reclen, d_off, d_ino,
                                     DT_UNKNOWN) < 0) {
+                                       reiserfs_write_lock(inode->i_sb);
                                        if (local_buf != small_buf) {
                                                kfree(local_buf);
                                        }
                                        goto end;
                                }
+                               reiserfs_write_lock(inode->i_sb);
                                if (local_buf != small_buf) {
                                        kfree(local_buf);
                                }
@@ -212,15 +226,21 @@ static int reiserfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
 
        }                       /* while */
 
-      end:
-       filp->f_pos = next_pos;
+end:
+       *pos = next_pos;
        pathrelse(&path_to_entry);
        reiserfs_check_path(&path_to_entry);
-      out:
+out:
        reiserfs_write_unlock(inode->i_sb);
        return ret;
 }
 
+static int reiserfs_readdir(struct file *file, void *dirent, filldir_t filldir)
+{
+       struct dentry *dentry = file->f_path.dentry;
+       return reiserfs_readdir_dentry(dentry, dirent, filldir, &file->f_pos);
+}
+
 /* compose directory item containing "." and ".." entries (entries are
    not aligned to 4 byte boundary) */
 /* the last four params are LE */