NFS: Fix a readdirplus bug
[pandora-kernel.git] / fs / nfs / dir.c
index 353f47c..f0a384e 100644 (file)
@@ -162,6 +162,7 @@ struct nfs_cache_array_entry {
        u64 cookie;
        u64 ino;
        struct qstr string;
+       unsigned char d_type;
 };
 
 struct nfs_cache_array {
@@ -171,8 +172,6 @@ struct nfs_cache_array {
        struct nfs_cache_array_entry array[0];
 };
 
-#define MAX_READDIR_ARRAY ((PAGE_SIZE - sizeof(struct nfs_cache_array)) / sizeof(struct nfs_cache_array_entry))
-
 typedef __be32 * (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, struct nfs_server *, int);
 typedef struct {
        struct file     *file;
@@ -257,13 +256,17 @@ int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
 
        if (IS_ERR(array))
                return PTR_ERR(array);
+
+       cache_entry = &array->array[array->size];
+
+       /* Check that this entry lies within the page bounds */
        ret = -ENOSPC;
-       if (array->size >= MAX_READDIR_ARRAY)
+       if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
                goto out;
 
-       cache_entry = &array->array[array->size];
        cache_entry->cookie = entry->prev_cookie;
        cache_entry->ino = entry->ino;
+       cache_entry->d_type = entry->d_type;
        ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
        if (ret)
                goto out;
@@ -392,13 +395,9 @@ int xdr_decode(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry, struct x
 static
 int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
 {
-       struct nfs_inode *node;
        if (dentry->d_inode == NULL)
                goto different;
-       node = NFS_I(dentry->d_inode);
-       if (node->fh.size != entry->fh->size)
-               goto different;
-       if (strncmp(node->fh.data, entry->fh->data, node->fh.size) != 0)
+       if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0)
                goto different;
        return 1;
 different:
@@ -700,7 +699,6 @@ int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
        int i = 0;
        int res = 0;
        struct nfs_cache_array *array = NULL;
-       unsigned int d_type = DT_UNKNOWN;
 
        array = nfs_readdir_get_array(desc->page);
        if (IS_ERR(array)) {
@@ -709,13 +707,15 @@ int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
        }
 
        for (i = desc->cache_entry_index; i < array->size; i++) {
-               d_type = DT_UNKNOWN;
+               struct nfs_cache_array_entry *ent;
 
-               res = filldir(dirent, array->array[i].string.name,
-                       array->array[i].string.len, file->f_pos,
-                       nfs_compat_user_ino64(array->array[i].ino), d_type);
-               if (res < 0)
+               ent = &array->array[i];
+               if (filldir(dirent, ent->string.name, ent->string.len,
+                   file->f_pos, nfs_compat_user_ino64(ent->ino),
+                   ent->d_type) < 0) {
+                       desc->eof = 1;
                        break;
+               }
                file->f_pos++;
                desc->cache_entry_index = i;
                if (i < (array->size-1))
@@ -766,10 +766,9 @@ int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
        desc->page_index = 0;
        desc->page = page;
 
-       if (nfs_readdir_xdr_to_array(desc, page, inode) == -1) {
-               status = -EIO;
+       status = nfs_readdir_xdr_to_array(desc, page, inode);
+       if (status < 0)
                goto out_release;
-       }
 
        status = nfs_do_filldir(desc, dirent, filldir);
 
@@ -821,14 +820,14 @@ static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
                res = readdir_search_pagecache(desc);
 
                if (res == -EBADCOOKIE) {
+                       res = 0;
                        /* This means either end of directory */
                        if (*desc->dir_cookie && desc->eof == 0) {
                                /* Or that the server has 'lost' a cookie */
                                res = uncached_readdir(desc, dirent, filldir);
-                               if (res >= 0)
+                               if (res == 0)
                                        continue;
                        }
-                       res = 0;
                        break;
                }
                if (res == -ETOOSMALL && desc->plus) {
@@ -843,10 +842,8 @@ static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
                        break;
 
                res = nfs_do_filldir(desc, dirent, filldir);
-               if (res < 0) {
-                       res = 0;
+               if (res < 0)
                        break;
-               }
        }
 out:
        nfs_unblock_sillyrename(dentry);