Commit 00a92642 authored by Olivier Galibert's avatar Olivier Galibert Committed by Trond Myklebust

[PATCH] NFS: Hide NFS server-generated readdir cookies from userland

 NFSv3 currently returns the unsigned 64-bit cookie directly to
 userspace. The following patch causes the kernel to generate
 loff_t offsets for the benefit of userland.
 The current server-generated READDIR cookie is cached in the
 nfs_open_context instead of in filp->f_pos, so we still end up work
 correctly under directory insertions/deletion.
Signed-off-by: default avatarOlivier Galibert <galibert@pobox.com>
Signed-off-by: default avatarTrond Myklebust <Trond.Myklebust@netapp.com>
parent ae388462
...@@ -141,7 +141,9 @@ typedef struct { ...@@ -141,7 +141,9 @@ typedef struct {
struct page *page; struct page *page;
unsigned long page_index; unsigned long page_index;
u32 *ptr; u32 *ptr;
u64 target; u64 target_cookie;
int target_index;
int current_index;
struct nfs_entry *entry; struct nfs_entry *entry;
decode_dirent_t decode; decode_dirent_t decode;
int plus; int plus;
...@@ -225,14 +227,14 @@ void dir_page_release(nfs_readdir_descriptor_t *desc) ...@@ -225,14 +227,14 @@ void dir_page_release(nfs_readdir_descriptor_t *desc)
/* /*
* Given a pointer to a buffer that has already been filled by a call * Given a pointer to a buffer that has already been filled by a call
* to readdir, find the next entry. * to readdir, find the next entry with cookie 'desc->target_cookie'.
* *
* If the end of the buffer has been reached, return -EAGAIN, if not, * If the end of the buffer has been reached, return -EAGAIN, if not,
* return the offset within the buffer of the next entry to be * return the offset within the buffer of the next entry to be
* read. * read.
*/ */
static inline static inline
int find_dirent(nfs_readdir_descriptor_t *desc, struct page *page) int find_dirent(nfs_readdir_descriptor_t *desc)
{ {
struct nfs_entry *entry = desc->entry; struct nfs_entry *entry = desc->entry;
int loop_count = 0, int loop_count = 0,
...@@ -240,7 +242,7 @@ int find_dirent(nfs_readdir_descriptor_t *desc, struct page *page) ...@@ -240,7 +242,7 @@ int find_dirent(nfs_readdir_descriptor_t *desc, struct page *page)
while((status = dir_decode(desc)) == 0) { while((status = dir_decode(desc)) == 0) {
dfprintk(VFS, "NFS: found cookie %Lu\n", (long long)entry->cookie); dfprintk(VFS, "NFS: found cookie %Lu\n", (long long)entry->cookie);
if (entry->prev_cookie == desc->target) if (entry->prev_cookie == desc->target_cookie)
break; break;
if (loop_count++ > 200) { if (loop_count++ > 200) {
loop_count = 0; loop_count = 0;
...@@ -252,8 +254,44 @@ int find_dirent(nfs_readdir_descriptor_t *desc, struct page *page) ...@@ -252,8 +254,44 @@ int find_dirent(nfs_readdir_descriptor_t *desc, struct page *page)
} }
/* /*
* Find the given page, and call find_dirent() in order to try to * Given a pointer to a buffer that has already been filled by a call
* return the next entry. * to readdir, find the entry at offset 'desc->target_index'.
*
* If the end of the buffer has been reached, return -EAGAIN, if not,
* return the offset within the buffer of the next entry to be
* read.
*/
static inline
int find_dirent_index(nfs_readdir_descriptor_t *desc)
{
struct nfs_entry *entry = desc->entry;
int loop_count = 0,
status;
for(;;) {
status = dir_decode(desc);
if (status)
break;
dfprintk(VFS, "NFS: found cookie %Lu at index %d\n", (long long)entry->cookie, desc->current_index);
if (desc->target_index == desc->current_index) {
desc->target_cookie = entry->cookie;
break;
}
desc->current_index++;
if (loop_count++ > 200) {
loop_count = 0;
schedule();
}
}
dfprintk(VFS, "NFS: find_dirent_index() returns %d\n", status);
return status;
}
/*
* Find the given page, and call find_dirent() or find_dirent_index in
* order to try to return the next entry.
*/ */
static inline static inline
int find_dirent_page(nfs_readdir_descriptor_t *desc) int find_dirent_page(nfs_readdir_descriptor_t *desc)
...@@ -276,7 +314,10 @@ int find_dirent_page(nfs_readdir_descriptor_t *desc) ...@@ -276,7 +314,10 @@ int find_dirent_page(nfs_readdir_descriptor_t *desc)
/* NOTE: Someone else may have changed the READDIRPLUS flag */ /* NOTE: Someone else may have changed the READDIRPLUS flag */
desc->page = page; desc->page = page;
desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */ desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
status = find_dirent(desc, page); if (desc->target_cookie)
status = find_dirent(desc);
else
status = find_dirent_index(desc);
if (status < 0) if (status < 0)
dir_page_release(desc); dir_page_release(desc);
out: out:
...@@ -291,7 +332,8 @@ int find_dirent_page(nfs_readdir_descriptor_t *desc) ...@@ -291,7 +332,8 @@ int find_dirent_page(nfs_readdir_descriptor_t *desc)
* Recurse through the page cache pages, and return a * Recurse through the page cache pages, and return a
* filled nfs_entry structure of the next directory entry if possible. * filled nfs_entry structure of the next directory entry if possible.
* *
* The target for the search is 'desc->target'. * The target for the search is 'desc->target_cookie' if non-0,
* 'desc->target_index' otherwise
*/ */
static inline static inline
int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
...@@ -299,7 +341,19 @@ int readdir_search_pagecache(nfs_readdir_descriptor_t *desc) ...@@ -299,7 +341,19 @@ int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
int loop_count = 0; int loop_count = 0;
int res; int res;
dfprintk(VFS, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", (long long)desc->target); if (desc->target_cookie)
dfprintk(VFS, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", (long long)desc->target_cookie);
else
dfprintk(VFS, "NFS: readdir_search_pagecache() searching for cookie number %d\n", desc->target_index);
/* Always search-by-index from the beginning of the cache */
if (!(desc->target_cookie)) {
desc->page_index = 0;
desc->entry->cookie = desc->entry->prev_cookie = 0;
desc->entry->eof = 0;
desc->current_index = 0;
}
for (;;) { for (;;) {
res = find_dirent_page(desc); res = find_dirent_page(desc);
if (res != -EAGAIN) if (res != -EAGAIN)
...@@ -332,11 +386,12 @@ int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent, ...@@ -332,11 +386,12 @@ int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
struct file *file = desc->file; struct file *file = desc->file;
struct nfs_entry *entry = desc->entry; struct nfs_entry *entry = desc->entry;
struct dentry *dentry = NULL; struct dentry *dentry = NULL;
struct nfs_open_context *ctx = file->private_data;
unsigned long fileid; unsigned long fileid;
int loop_count = 0, int loop_count = 0,
res; res;
dfprintk(VFS, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", (long long)desc->target); dfprintk(VFS, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", (long long)entry->cookie);
for(;;) { for(;;) {
unsigned d_type = DT_UNKNOWN; unsigned d_type = DT_UNKNOWN;
...@@ -356,10 +411,11 @@ int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent, ...@@ -356,10 +411,11 @@ int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
} }
res = filldir(dirent, entry->name, entry->len, res = filldir(dirent, entry->name, entry->len,
entry->prev_cookie, fileid, d_type); file->f_pos, fileid, d_type);
if (res < 0) if (res < 0)
break; break;
file->f_pos = desc->target = entry->cookie; file->f_pos++;
desc->target_cookie = entry->cookie;
if (dir_decode(desc) != 0) { if (dir_decode(desc) != 0) {
desc->page_index ++; desc->page_index ++;
break; break;
...@@ -369,10 +425,12 @@ int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent, ...@@ -369,10 +425,12 @@ int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
schedule(); schedule();
} }
} }
ctx->dir_pos = file->f_pos;
ctx->dir_cookie = desc->target_cookie;
dir_page_release(desc); dir_page_release(desc);
if (dentry != NULL) if (dentry != NULL)
dput(dentry); dput(dentry);
dfprintk(VFS, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", (long long)desc->target, res); dfprintk(VFS, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", (long long)desc->target_cookie, res);
return res; return res;
} }
...@@ -398,14 +456,14 @@ int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent, ...@@ -398,14 +456,14 @@ int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
struct page *page = NULL; struct page *page = NULL;
int status; int status;
dfprintk(VFS, "NFS: uncached_readdir() searching for cookie %Lu\n", (long long)desc->target); dfprintk(VFS, "NFS: uncached_readdir() searching for cookie %Lu\n", (long long)desc->target_cookie);
page = alloc_page(GFP_HIGHUSER); page = alloc_page(GFP_HIGHUSER);
if (!page) { if (!page) {
status = -ENOMEM; status = -ENOMEM;
goto out; goto out;
} }
desc->error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, desc->target, desc->error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, desc->target_cookie,
page, page,
NFS_SERVER(inode)->dtsize, NFS_SERVER(inode)->dtsize,
desc->plus); desc->plus);
...@@ -414,7 +472,7 @@ int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent, ...@@ -414,7 +472,7 @@ int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */ desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
if (desc->error >= 0) { if (desc->error >= 0) {
if ((status = dir_decode(desc)) == 0) if ((status = dir_decode(desc)) == 0)
desc->entry->prev_cookie = desc->target; desc->entry->prev_cookie = desc->target_cookie;
} else } else
status = -EIO; status = -EIO;
if (status < 0) if (status < 0)
...@@ -435,13 +493,15 @@ int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent, ...@@ -435,13 +493,15 @@ int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
goto out; goto out;
} }
/* The file offset position is now represented as a true offset into the /* The file offset position represents the dirent entry number. A
* page cache as is the case in most of the other filesystems. last cookie cache takes care of the common case of reading the
whole directory.
*/ */
static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir) static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
{ {
struct dentry *dentry = filp->f_dentry; struct dentry *dentry = filp->f_dentry;
struct inode *inode = dentry->d_inode; struct inode *inode = dentry->d_inode;
struct nfs_open_context *ctx = filp->private_data;
nfs_readdir_descriptor_t my_desc, nfs_readdir_descriptor_t my_desc,
*desc = &my_desc; *desc = &my_desc;
struct nfs_entry my_entry; struct nfs_entry my_entry;
...@@ -458,17 +518,22 @@ static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir) ...@@ -458,17 +518,22 @@ static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
} }
/* /*
* filp->f_pos points to the file offset in the page cache. * filp->f_pos points to the dirent entry number.
* but if the cache has meanwhile been zapped, we need to * ctx->dir_pos has the number of the cached cookie. We have
* read from the last dirent to revalidate f_pos * to either find the entry with the appropriate number or
* itself. * revalidate the cookie.
*/ */
memset(desc, 0, sizeof(*desc)); memset(desc, 0, sizeof(*desc));
desc->file = filp; desc->file = filp;
desc->target = filp->f_pos;
desc->decode = NFS_PROTO(inode)->decode_dirent; desc->decode = NFS_PROTO(inode)->decode_dirent;
desc->plus = NFS_USE_READDIRPLUS(inode); desc->plus = NFS_USE_READDIRPLUS(inode);
desc->target_index = filp->f_pos;
if (filp->f_pos == ctx->dir_pos)
desc->target_cookie = ctx->dir_cookie;
else
desc->target_cookie = 0;
my_entry.cookie = my_entry.prev_cookie = 0; my_entry.cookie = my_entry.prev_cookie = 0;
my_entry.eof = 0; my_entry.eof = 0;
...@@ -478,9 +543,10 @@ static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir) ...@@ -478,9 +543,10 @@ static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
while(!desc->entry->eof) { while(!desc->entry->eof) {
res = readdir_search_pagecache(desc); res = readdir_search_pagecache(desc);
if (res == -EBADCOOKIE) { if (res == -EBADCOOKIE) {
/* This means either end of directory */ /* This means either end of directory */
if (desc->entry->cookie != desc->target) { if (desc->target_cookie && desc->entry->cookie != desc->target_cookie) {
/* Or that the server has 'lost' a cookie */ /* Or that the server has 'lost' a cookie */
res = uncached_readdir(desc, dirent, filldir); res = uncached_readdir(desc, dirent, filldir);
if (res >= 0) if (res >= 0)
......
...@@ -891,6 +891,8 @@ struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry, struct rp ...@@ -891,6 +891,8 @@ struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry, struct rp
ctx->state = NULL; ctx->state = NULL;
ctx->lockowner = current->files; ctx->lockowner = current->files;
ctx->error = 0; ctx->error = 0;
ctx->dir_pos = 0;
ctx->dir_cookie = 0;
} }
return ctx; return ctx;
} }
......
...@@ -84,6 +84,9 @@ struct nfs_open_context { ...@@ -84,6 +84,9 @@ struct nfs_open_context {
int error; int error;
struct list_head list; struct list_head list;
int dir_pos; /* Directory cookie cache */
__u64 dir_cookie;
}; };
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment