NFS: Allow redirtying of a completed unstable write.
[linux-2.6.git] / fs / nfs / read.c
index 4affb53..40d1798 100644 (file)
@@ -27,7 +27,8 @@
 
 #define NFSDBG_FACILITY                NFSDBG_PAGECACHE
 
-static int nfs_pagein_one(struct list_head *, struct inode *);
+static int nfs_pagein_multi(struct inode *, struct list_head *, unsigned int, size_t, int);
+static int nfs_pagein_one(struct inode *, struct list_head *, unsigned int, size_t, int);
 static const struct rpc_call_ops nfs_read_partial_ops;
 static const struct rpc_call_ops nfs_read_full_ops;
 
@@ -36,9 +37,8 @@ static mempool_t *nfs_rdata_mempool;
 
 #define MIN_POOL_READ  (32)
 
-struct nfs_read_data *nfs_readdata_alloc(size_t len)
+struct nfs_read_data *nfs_readdata_alloc(unsigned int pagecount)
 {
-       unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
        struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, GFP_NOFS);
 
        if (p) {
@@ -58,28 +58,25 @@ struct nfs_read_data *nfs_readdata_alloc(size_t len)
        return p;
 }
 
-static void nfs_readdata_rcu_free(struct rcu_head *head)
+static void nfs_readdata_free(struct nfs_read_data *p)
 {
-       struct nfs_read_data *p = container_of(head, struct nfs_read_data, task.u.tk_rcu);
        if (p && (p->pagevec != &p->page_array[0]))
                kfree(p->pagevec);
        mempool_free(p, nfs_rdata_mempool);
 }
 
-static void nfs_readdata_free(struct nfs_read_data *rdata)
-{
-       call_rcu_bh(&rdata->task.u.tk_rcu, nfs_readdata_rcu_free);
-}
-
 void nfs_readdata_release(void *data)
 {
-        nfs_readdata_free(data);
+       struct nfs_read_data *rdata = data;
+
+       put_nfs_open_context(rdata->args.context);
+       nfs_readdata_free(rdata);
 }
 
 static
 int nfs_return_empty_page(struct page *page)
 {
-       memclear_highpage_flush(page, 0, PAGE_CACHE_SIZE);
+       zero_user(page, 0, PAGE_CACHE_SIZE);
        SetPageUptodate(page);
        unlock_page(page);
        return 0;
@@ -103,10 +100,10 @@ static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
        pglen = PAGE_CACHE_SIZE - base;
        for (;;) {
                if (remainder <= pglen) {
-                       memclear_highpage_flush(*pages, base, remainder);
+                       zero_user(*pages, base, remainder);
                        break;
                }
-               memclear_highpage_flush(*pages, base, pglen);
+               zero_user(*pages, base, pglen);
                pages++;
                remainder -= pglen;
                pglen = PAGE_CACHE_SIZE;
@@ -130,10 +127,13 @@ static int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
                return PTR_ERR(new);
        }
        if (len < PAGE_CACHE_SIZE)
-               memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
+               zero_user_segment(page, len, PAGE_CACHE_SIZE);
 
        nfs_list_add_request(new, &one_request);
-       nfs_pagein_one(&one_request, inode);
+       if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
+               nfs_pagein_multi(inode, &one_request, 1, len, 0);
+       else
+               nfs_pagein_one(inode, &one_request, 1, len, 0);
        return 0;
 }
 
@@ -142,8 +142,8 @@ static void nfs_readpage_release(struct nfs_page *req)
        unlock_page(req->wb_page);
 
        dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
-                       req->wb_context->dentry->d_inode->i_sb->s_id,
-                       (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
+                       req->wb_context->path.dentry->d_inode->i_sb->s_id,
+                       (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
                        req->wb_bytes,
                        (long long)req_offset(req));
        nfs_clear_request(req);
@@ -153,23 +153,38 @@ static void nfs_readpage_release(struct nfs_page *req)
 /*
  * Set up the NFS read request struct
  */
-static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
+static int nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
                const struct rpc_call_ops *call_ops,
                unsigned int count, unsigned int offset)
 {
-       struct inode            *inode;
-       int flags;
+       struct inode *inode = req->wb_context->path.dentry->d_inode;
+       int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
+       struct rpc_task *task;
+       struct rpc_message msg = {
+               .rpc_argp = &data->args,
+               .rpc_resp = &data->res,
+               .rpc_cred = req->wb_context->cred,
+       };
+       struct rpc_task_setup task_setup_data = {
+               .task = &data->task,
+               .rpc_client = NFS_CLIENT(inode),
+               .rpc_message = &msg,
+               .callback_ops = call_ops,
+               .callback_data = data,
+               .workqueue = nfsiod_workqueue,
+               .flags = RPC_TASK_ASYNC | swap_flags,
+       };
 
        data->req         = req;
-       data->inode       = inode = req->wb_context->dentry->d_inode;
-       data->cred        = req->wb_context->cred;
+       data->inode       = inode;
+       data->cred        = msg.rpc_cred;
 
        data->args.fh     = NFS_FH(inode);
        data->args.offset = req_offset(req) + offset;
        data->args.pgbase = req->wb_pgbase + offset;
        data->args.pages  = data->pagevec;
        data->args.count  = count;
-       data->args.context = req->wb_context;
+       data->args.context = get_nfs_open_context(req->wb_context);
 
        data->res.fattr   = &data->fattr;
        data->res.count   = count;
@@ -177,18 +192,20 @@ static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
        nfs_fattr_init(&data->fattr);
 
        /* Set up the initial task struct. */
-       flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
-       rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
-       NFS_PROTO(inode)->read_setup(data);
-
-       data->task.tk_cookie = (unsigned long)inode;
+       NFS_PROTO(inode)->read_setup(data, &msg);
 
-       dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
+       dprintk("NFS: %5u initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
                        data->task.tk_pid,
                        inode->i_sb->s_id,
                        (long long)NFS_FILEID(inode),
                        count,
                        (unsigned long long)data->args.offset);
+
+       task = rpc_run_task(&task_setup_data);
+       if (IS_ERR(task))
+               return PTR_ERR(task);
+       rpc_put_task(task);
+       return 0;
 }
 
 static void
@@ -205,19 +222,6 @@ nfs_async_read_error(struct list_head *head)
 }
 
 /*
- * Start an async read operation
- */
-static void nfs_execute_read(struct nfs_read_data *data)
-{
-       struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
-       sigset_t oldset;
-
-       rpc_clnt_sigmask(clnt, &oldset);
-       rpc_execute(&data->task);
-       rpc_clnt_sigunmask(clnt, &oldset);
-}
-
-/*
  * Generate multiple requests to fill a single page.
  *
  * We optimize to reduce the number of read operations on the wire.  If we
@@ -230,7 +234,7 @@ static void nfs_execute_read(struct nfs_read_data *data)
  * won't see the new data until our attribute cache is updated.  This is more
  * or less conventional NFS client behavior.
  */
-static int nfs_pagein_multi(struct list_head *head, struct inode *inode)
+static int nfs_pagein_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int flags)
 {
        struct nfs_page *req = nfs_list_entry(head->next);
        struct page *page = req->wb_page;
@@ -238,18 +242,18 @@ static int nfs_pagein_multi(struct list_head *head, struct inode *inode)
        size_t rsize = NFS_SERVER(inode)->rsize, nbytes;
        unsigned int offset;
        int requests = 0;
+       int ret = 0;
        LIST_HEAD(list);
 
        nfs_list_remove_request(req);
 
-       nbytes = req->wb_bytes;
+       nbytes = count;
        do {
                size_t len = min(nbytes,rsize);
 
-               data = nfs_readdata_alloc(len);
+               data = nfs_readdata_alloc(1);
                if (!data)
                        goto out_bad;
-               INIT_LIST_HEAD(&data->pages);
                list_add(&data->pages, &list);
                requests++;
                nbytes -= len;
@@ -258,27 +262,26 @@ static int nfs_pagein_multi(struct list_head *head, struct inode *inode)
 
        ClearPageError(page);
        offset = 0;
-       nbytes = req->wb_bytes;
+       nbytes = count;
        do {
+               int ret2;
+
                data = list_entry(list.next, struct nfs_read_data, pages);
                list_del_init(&data->pages);
 
                data->pagevec[0] = page;
 
-               if (nbytes > rsize) {
-                       nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
-                                       rsize, offset);
-                       offset += rsize;
-                       nbytes -= rsize;
-               } else {
-                       nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
-                                       nbytes, offset);
-                       nbytes = 0;
-               }
-               nfs_execute_read(data);
+               if (nbytes < rsize)
+                       rsize = nbytes;
+               ret2 = nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
+                                 rsize, offset);
+               if (ret == 0)
+                       ret = ret2;
+               offset += rsize;
+               nbytes -= rsize;
        } while (nbytes != 0);
 
-       return 0;
+       return ret;
 
 out_bad:
        while (!list_empty(&list)) {
@@ -291,62 +294,31 @@ out_bad:
        return -ENOMEM;
 }
 
-static int nfs_pagein_one(struct list_head *head, struct inode *inode)
+static int nfs_pagein_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int flags)
 {
        struct nfs_page         *req;
        struct page             **pages;
        struct nfs_read_data    *data;
-       unsigned int            count;
+       int ret = -ENOMEM;
 
-       if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
-               return nfs_pagein_multi(head, inode);
-
-       data = nfs_readdata_alloc(NFS_SERVER(inode)->rsize);
+       data = nfs_readdata_alloc(npages);
        if (!data)
                goto out_bad;
 
-       INIT_LIST_HEAD(&data->pages);
        pages = data->pagevec;
-       count = 0;
        while (!list_empty(head)) {
                req = nfs_list_entry(head->next);
                nfs_list_remove_request(req);
                nfs_list_add_request(req, &data->pages);
                ClearPageError(req->wb_page);
                *pages++ = req->wb_page;
-               count += req->wb_bytes;
        }
        req = nfs_list_entry(data->pages.next);
 
-       nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0);
-
-       nfs_execute_read(data);
-       return 0;
+       return nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0);
 out_bad:
        nfs_async_read_error(head);
-       return -ENOMEM;
-}
-
-static int
-nfs_pagein_list(struct list_head *head, int rpages)
-{
-       LIST_HEAD(one_request);
-       struct nfs_page         *req;
-       int                     error = 0;
-       unsigned int            pages = 0;
-
-       while (!list_empty(head)) {
-               pages += nfs_coalesce_requests(head, &one_request, rpages);
-               req = nfs_list_entry(one_request.next);
-               error = nfs_pagein_one(&one_request, req->wb_context->dentry->d_inode);
-               if (error < 0)
-                       break;
-       }
-       if (error >= 0)
-               return pages;
-
-       nfs_async_read_error(head);
-       return error;
+       return ret;
 }
 
 /*
@@ -357,7 +329,7 @@ int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
 {
        int status;
 
-       dprintk("%s: %4d, (status %d)\n", __FUNCTION__, task->tk_pid,
+       dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid,
                        task->tk_status);
 
        status = NFS_PROTO(data->inode)->read_done(task, data);
@@ -367,35 +339,31 @@ int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
        nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count);
 
        if (task->tk_status == -ESTALE) {
-               set_bit(NFS_INO_STALE, &NFS_FLAGS(data->inode));
+               set_bit(NFS_INO_STALE, &NFS_I(data->inode)->flags);
                nfs_mark_for_revalidate(data->inode);
        }
-       spin_lock(&data->inode->i_lock);
-       NFS_I(data->inode)->cache_validity |= NFS_INO_INVALID_ATIME;
-       spin_unlock(&data->inode->i_lock);
        return 0;
 }
 
-static int nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
+static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
 {
        struct nfs_readargs *argp = &data->args;
        struct nfs_readres *resp = &data->res;
 
        if (resp->eof || resp->count == argp->count)
-               return 0;
+               return;
 
        /* This is a short read! */
        nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
        /* Has the server at least made some progress? */
        if (resp->count == 0)
-               return 0;
+               return;
 
        /* Yes, so retry the read at the end of the data */
        argp->offset += resp->count;
        argp->pgbase += resp->count;
        argp->count -= resp->count;
        rpc_restart_call(task);
-       return -EAGAIN;
 }
 
 /*
@@ -404,29 +372,37 @@ static int nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
 static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
 {
        struct nfs_read_data *data = calldata;
-       struct nfs_page *req = data->req;
-       struct page *page = req->wb_page;
  
        if (nfs_readpage_result(task, data) != 0)
                return;
+       if (task->tk_status < 0)
+               return;
 
-       if (likely(task->tk_status >= 0)) {
-               nfs_readpage_truncate_uninitialised_page(data);
-               if (nfs_readpage_retry(task, data) != 0)
-                       return;
-       }
-       if (unlikely(task->tk_status < 0))
+       nfs_readpage_truncate_uninitialised_page(data);
+       nfs_readpage_retry(task, data);
+}
+
+static void nfs_readpage_release_partial(void *calldata)
+{
+       struct nfs_read_data *data = calldata;
+       struct nfs_page *req = data->req;
+       struct page *page = req->wb_page;
+       int status = data->task.tk_status;
+
+       if (status < 0)
                SetPageError(page);
+
        if (atomic_dec_and_test(&req->wb_complete)) {
                if (!PageError(page))
                        SetPageUptodate(page);
                nfs_readpage_release(req);
        }
+       nfs_readdata_release(calldata);
 }
 
 static const struct rpc_call_ops nfs_read_partial_ops = {
        .rpc_call_done = nfs_readpage_result_partial,
-       .rpc_release = nfs_readdata_release,
+       .rpc_release = nfs_readpage_release_partial,
 };
 
 static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
@@ -461,29 +437,35 @@ static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
 
        if (nfs_readpage_result(task, data) != 0)
                return;
+       if (task->tk_status < 0)
+               return;
        /*
         * Note: nfs_readpage_retry may change the values of
         * data->args. In the multi-page case, we therefore need
         * to ensure that we call nfs_readpage_set_pages_uptodate()
         * first.
         */
-       if (likely(task->tk_status >= 0)) {
-               nfs_readpage_truncate_uninitialised_page(data);
-               nfs_readpage_set_pages_uptodate(data);
-               if (nfs_readpage_retry(task, data) != 0)
-                       return;
-       }
+       nfs_readpage_truncate_uninitialised_page(data);
+       nfs_readpage_set_pages_uptodate(data);
+       nfs_readpage_retry(task, data);
+}
+
+static void nfs_readpage_release_full(void *calldata)
+{
+       struct nfs_read_data *data = calldata;
+
        while (!list_empty(&data->pages)) {
                struct nfs_page *req = nfs_list_entry(data->pages.next);
 
                nfs_list_remove_request(req);
                nfs_readpage_release(req);
        }
+       nfs_readdata_release(calldata);
 }
 
 static const struct rpc_call_ops nfs_read_full_ops = {
        .rpc_call_done = nfs_readpage_result_full,
-       .rpc_release = nfs_readdata_release,
+       .rpc_release = nfs_readpage_release_full,
 };
 
 /*
@@ -512,33 +494,33 @@ int nfs_readpage(struct file *file, struct page *page)
         */
        error = nfs_wb_page(inode, page);
        if (error)
-               goto out_error;
+               goto out_unlock;
+       if (PageUptodate(page))
+               goto out_unlock;
 
        error = -ESTALE;
        if (NFS_STALE(inode))
-               goto out_error;
+               goto out_unlock;
 
        if (file == NULL) {
                error = -EBADF;
                ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
                if (ctx == NULL)
-                       goto out_error;
+                       goto out_unlock;
        } else
-               ctx = get_nfs_open_context((struct nfs_open_context *)
-                               file->private_data);
+               ctx = get_nfs_open_context(nfs_file_open_context(file));
 
        error = nfs_readpage_async(ctx, inode, page);
 
        put_nfs_open_context(ctx);
        return error;
-
-out_error:
+out_unlock:
        unlock_page(page);
        return error;
 }
 
 struct nfs_readdesc {
-       struct list_head *head;
+       struct nfs_pageio_descriptor *pgio;
        struct nfs_open_context *ctx;
 };
 
@@ -549,32 +531,48 @@ readpage_async_filler(void *data, struct page *page)
        struct inode *inode = page->mapping->host;
        struct nfs_page *new;
        unsigned int len;
+       int error;
+
+       error = nfs_wb_page(inode, page);
+       if (error)
+               goto out_unlock;
+       if (PageUptodate(page))
+               goto out_unlock;
 
-       nfs_wb_page(inode, page);
        len = nfs_page_length(page);
        if (len == 0)
                return nfs_return_empty_page(page);
+
        new = nfs_create_request(desc->ctx, inode, page, 0, len);
-       if (IS_ERR(new)) {
-                       SetPageError(page);
-                       unlock_page(page);
-                       return PTR_ERR(new);
-       }
+       if (IS_ERR(new))
+               goto out_error;
+
        if (len < PAGE_CACHE_SIZE)
-               memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
-       nfs_list_add_request(new, desc->head);
+               zero_user_segment(page, len, PAGE_CACHE_SIZE);
+       if (!nfs_pageio_add_request(desc->pgio, new)) {
+               error = desc->pgio->pg_error;
+               goto out_unlock;
+       }
        return 0;
+out_error:
+       error = PTR_ERR(new);
+       SetPageError(page);
+out_unlock:
+       unlock_page(page);
+       return error;
 }
 
 int nfs_readpages(struct file *filp, struct address_space *mapping,
                struct list_head *pages, unsigned nr_pages)
 {
-       LIST_HEAD(head);
+       struct nfs_pageio_descriptor pgio;
        struct nfs_readdesc desc = {
-               .head           = &head,
+               .pgio = &pgio,
        };
        struct inode *inode = mapping->host;
        struct nfs_server *server = NFS_SERVER(inode);
+       size_t rsize = server->rsize;
+       unsigned long npages;
        int ret = -ESTALE;
 
        dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
@@ -591,15 +589,17 @@ int nfs_readpages(struct file *filp, struct address_space *mapping,
                if (desc.ctx == NULL)
                        return -EBADF;
        } else
-               desc.ctx = get_nfs_open_context((struct nfs_open_context *)
-                               filp->private_data);
+               desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
+       if (rsize < PAGE_CACHE_SIZE)
+               nfs_pageio_init(&pgio, inode, nfs_pagein_multi, rsize, 0);
+       else
+               nfs_pageio_init(&pgio, inode, nfs_pagein_one, rsize, 0);
+
        ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
-       if (!list_empty(&head)) {
-               int err = nfs_pagein_list(&head, server->rpages);
-               if (!ret)
-                       nfs_add_stats(inode, NFSIOS_READPAGES, err);
-                       ret = err;
-       }
+
+       nfs_pageio_complete(&pgio);
+       npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
+       nfs_add_stats(inode, NFSIOS_READPAGES, npages);
        put_nfs_open_context(desc.ctx);
 out:
        return ret;
@@ -610,7 +610,7 @@ int __init nfs_init_readpagecache(void)
        nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
                                             sizeof(struct nfs_read_data),
                                             0, SLAB_HWCACHE_ALIGN,
-                                            NULL, NULL);
+                                            NULL);
        if (nfs_rdata_cachep == NULL)
                return -ENOMEM;