ALSA: hda - iMac9,1 sound fixes
[safe/jmp/linux-2.6] / fs / nfs / pagelist.c
index 5281285..a3654e5 100644 (file)
 
 #include <linux/slab.h>
 #include <linux/file.h>
+#include <linux/sched.h>
 #include <linux/sunrpc/clnt.h>
 #include <linux/nfs3.h>
 #include <linux/nfs4.h>
 #include <linux/nfs_page.h>
 #include <linux/nfs_fs.h>
 #include <linux/nfs_mount.h>
-#include <linux/writeback.h>
 
-#define NFS_PARANOIA 1
+#include "internal.h"
 
 static struct kmem_cache *nfs_page_cachep;
 
@@ -50,9 +50,7 @@ nfs_page_free(struct nfs_page *p)
  * @count: number of bytes to read/write
  *
  * The page must be locked by the caller. This makes sure we never
- * create two different requests for the same page, and avoids
- * a possible deadlock when we reach the hard limit on the number
- * of dirty pages.
+ * create two different requests for the same page.
  * User should ensure it is safe to sleep in this function.
  */
 struct nfs_page *
@@ -60,23 +58,12 @@ nfs_create_request(struct nfs_open_context *ctx, struct inode *inode,
                   struct page *page,
                   unsigned int offset, unsigned int count)
 {
-       struct nfs_server *server = NFS_SERVER(inode);
        struct nfs_page         *req;
 
-       /* Deal with hard limits.  */
-       for (;;) {
-               /* try to allocate the request struct */
-               req = nfs_page_alloc();
-               if (req != NULL)
-                       break;
-
-               /* Try to free up at least one request in order to stay
-                * below the hard limit
-                */
-               if (signalled() && (server->flags & NFS_MOUNT_INTR))
-                       return ERR_PTR(-ERESTARTSYS);
-               yield();
-       }
+       /* try to allocate the request struct */
+       req = nfs_page_alloc();
+       if (req == NULL)
+               return ERR_PTR(-ENOMEM);
 
        /* Initialize the request struct. Initially, we assume a
         * long write-back delay. This will be adjusted in
@@ -91,9 +78,8 @@ nfs_create_request(struct nfs_open_context *ctx, struct inode *inode,
        req->wb_offset  = offset;
        req->wb_pgbase  = offset;
        req->wb_bytes   = count;
-       atomic_set(&req->wb_count, 1);
        req->wb_context = get_nfs_open_context(ctx);
-
+       kref_init(&req->wb_kref);
        return req;
 }
 
@@ -115,48 +101,55 @@ void nfs_unlock_request(struct nfs_page *req)
 }
 
 /**
- * nfs_set_page_writeback_locked - Lock a request for writeback
+ * nfs_set_page_tag_locked - Tag a request as locked
  * @req:
  */
-int nfs_set_page_writeback_locked(struct nfs_page *req)
+int nfs_set_page_tag_locked(struct nfs_page *req)
 {
-       struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
-
-       if (!nfs_lock_request(req))
+       if (!nfs_lock_request_dontget(req))
                return 0;
-       radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
+       if (req->wb_page != NULL)
+               radix_tree_tag_set(&NFS_I(req->wb_context->path.dentry->d_inode)->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_LOCKED);
        return 1;
 }
 
 /**
- * nfs_clear_page_writeback - Unlock request and wake up sleepers
+ * nfs_clear_page_tag_locked - Clear request tag and wake up sleepers
  */
-void nfs_clear_page_writeback(struct nfs_page *req)
+void nfs_clear_page_tag_locked(struct nfs_page *req)
 {
-       struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
-
        if (req->wb_page != NULL) {
-               spin_lock(&nfsi->req_lock);
-               radix_tree_tag_clear(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
-               spin_unlock(&nfsi->req_lock);
-       }
-       nfs_unlock_request(req);
+               struct inode *inode = req->wb_context->path.dentry->d_inode;
+               struct nfs_inode *nfsi = NFS_I(inode);
+
+               spin_lock(&inode->i_lock);
+               radix_tree_tag_clear(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_LOCKED);
+               nfs_unlock_request(req);
+               spin_unlock(&inode->i_lock);
+       } else
+               nfs_unlock_request(req);
 }
 
 /**
  * nfs_clear_request - Free up all resources allocated to the request
  * @req:
  *
- * Release page resources associated with a write request after it
- * has completed.
+ * Release page and open context resources associated with a read/write
+ * request after it has completed.
  */
 void nfs_clear_request(struct nfs_page *req)
 {
        struct page *page = req->wb_page;
+       struct nfs_open_context *ctx = req->wb_context;
+
        if (page != NULL) {
                page_cache_release(page);
                req->wb_page = NULL;
        }
+       if (ctx != NULL) {
+               put_nfs_open_context(ctx);
+               req->wb_context = NULL;
+       }
 }
 
 
@@ -166,60 +159,39 @@ void nfs_clear_request(struct nfs_page *req)
  *
  * Note: Should never be called with the spinlock held!
  */
-void
-nfs_release_request(struct nfs_page *req)
+static void nfs_free_request(struct kref *kref)
 {
-       if (!atomic_dec_and_test(&req->wb_count))
-               return;
-
-#ifdef NFS_PARANOIA
-       BUG_ON (!list_empty(&req->wb_list));
-       BUG_ON (NFS_WBACK_BUSY(req));
-#endif
+       struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
 
-       /* Release struct file or cached credential */
+       /* Release struct file and open context */
        nfs_clear_request(req);
-       put_nfs_open_context(req->wb_context);
        nfs_page_free(req);
 }
 
-static int nfs_wait_bit_interruptible(void *word)
+void nfs_release_request(struct nfs_page *req)
 {
-       int ret = 0;
+       kref_put(&req->wb_kref, nfs_free_request);
+}
 
-       if (signal_pending(current))
-               ret = -ERESTARTSYS;
-       else
-               schedule();
-       return ret;
+static int nfs_wait_bit_uninterruptible(void *word)
+{
+       io_schedule();
+       return 0;
 }
 
 /**
  * nfs_wait_on_request - Wait for a request to complete.
  * @req: request to wait upon.
  *
- * Interruptible by signals only if mounted with intr flag.
+ * Interruptible by fatal signals only.
  * The user is responsible for holding a count on the request.
  */
 int
 nfs_wait_on_request(struct nfs_page *req)
 {
-        struct rpc_clnt        *clnt = NFS_CLIENT(req->wb_context->dentry->d_inode);
-       sigset_t oldmask;
-       int ret = 0;
-
-       if (!test_bit(PG_BUSY, &req->wb_flags))
-               goto out;
-       /*
-        * Note: the call to rpc_clnt_sigmask() suffices to ensure that we
-        *       are not interrupted if intr flag is not set
-        */
-       rpc_clnt_sigmask(clnt, &oldmask);
-       ret = out_of_line_wait_on_bit(&req->wb_flags, PG_BUSY,
-                       nfs_wait_bit_interruptible, TASK_INTERRUPTIBLE);
-       rpc_clnt_sigunmask(clnt, &oldmask);
-out:
-       return ret;
+       return wait_on_bit(&req->wb_flags, PG_BUSY,
+                       nfs_wait_bit_uninterruptible,
+                       TASK_UNINTERRUPTIBLE);
 }
 
 /**
@@ -232,8 +204,8 @@ out:
  */
 void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
                     struct inode *inode,
-                    int (*doio)(struct inode *, struct list_head *, size_t, int),
-                    unsigned int bsize,
+                    int (*doio)(struct inode *, struct list_head *, unsigned int, size_t, int),
+                    size_t bsize,
                     int io_flags)
 {
        INIT_LIST_HEAD(&desc->pg_list);
@@ -299,8 +271,10 @@ static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
                 * since nfs_flush_multi and nfs_pagein_multi assume you
                 * can have only one struct nfs_page.
                 */
+               if (desc->pg_bsize < PAGE_SIZE)
+                       return 0;
                newlen += desc->pg_count;
-               if (desc->pg_base + newlen > desc->pg_bsize)
+               if (newlen > desc->pg_bsize)
                        return 0;
                prev = nfs_list_entry(desc->pg_list.prev);
                if (!nfs_can_coalesce_requests(prev, req))
@@ -321,6 +295,8 @@ static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
        if (!list_empty(&desc->pg_list)) {
                int error = desc->pg_doio(desc->pg_inode,
                                          &desc->pg_list,
+                                         nfs_page_array_len(desc->pg_base,
+                                                            desc->pg_count),
                                          desc->pg_count,
                                          desc->pg_ioflags);
                if (error < 0)
@@ -342,8 +318,8 @@ static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
  * Returns true if the request 'req' was successfully coalesced into the
  * existing list of pages 'desc'.
  */
-static int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
-                                 struct nfs_page *req)
+int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
+                          struct nfs_page *req)
 {
        while (!nfs_pageio_do_add_request(desc, req)) {
                nfs_pageio_doio(desc);
@@ -354,25 +330,6 @@ static int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
 }
 
 /**
- * nfs_pageio_add_list - Split coalesced requests out from a list.
- * @desc: destination io descriptor
- * @head: source list
- *
- * Moves a maximum of 'nmax' elements from one list to another.
- * The elements are checked to ensure that they form a contiguous set
- * of pages, and that the RPC credentials are the same.
- */
-void nfs_pageio_add_list(struct nfs_pageio_descriptor *desc,
-                        struct list_head *head)
-{
-       while (!list_empty(head)) {
-               struct nfs_page *req = nfs_list_entry(head->next);
-               if (!nfs_pageio_add_request(desc, req))
-                       break;
-       }
-}
-
-/**
  * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
  * @desc: pointer to io descriptor
  */
@@ -381,100 +338,48 @@ void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
        nfs_pageio_doio(desc);
 }
 
-#define NFS_SCAN_MAXENTRIES 16
 /**
- * nfs_scan_dirty - Scan the radix tree for dirty requests
- * @mapping: pointer to address space
- * @wbc: writeback_control structure
- * @dst: Destination list
+ * nfs_pageio_cond_complete - Conditional I/O completion
+ * @desc: pointer to io descriptor
+ * @index: page index
  *
- * Moves elements from one of the inode request lists.
- * If the number of requests is set to 0, the entire address_space
- * starting at index idx_start, is scanned.
- * The requests are *not* checked to ensure that they form a contiguous set.
- * You must be holding the inode's req_lock when calling this function
+ * It is important to ensure that processes don't try to take locks
+ * on non-contiguous ranges of pages as that might deadlock. This
+ * function should be called before attempting to wait on a locked
+ * nfs_page. It will complete the I/O if the page index 'index'
+ * is not contiguous with the existing list of pages in 'desc'.
  */
-long nfs_scan_dirty(struct address_space *mapping,
-                       struct writeback_control *wbc,
-                       struct list_head *dst)
+void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
 {
-       struct nfs_inode *nfsi = NFS_I(mapping->host);
-       struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
-       struct nfs_page *req;
-       pgoff_t idx_start, idx_end;
-       long res = 0;
-       int found, i;
-
-       if (nfsi->ndirty == 0)
-               return 0;
-       if (wbc->range_cyclic) {
-               idx_start = 0;
-               idx_end = ULONG_MAX;
-       } else if (wbc->range_end == 0) {
-               idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
-               idx_end = ULONG_MAX;
-       } else {
-               idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
-               idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
-       }
-
-       for (;;) {
-               unsigned int toscan = NFS_SCAN_MAXENTRIES;
-
-               found = radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree,
-                               (void **)&pgvec[0], idx_start, toscan,
-                               NFS_PAGE_TAG_DIRTY);
-
-               /* Did we make progress? */
-               if (found <= 0)
-                       break;
-
-               for (i = 0; i < found; i++) {
-                       req = pgvec[i];
-                       if (!wbc->range_cyclic && req->wb_index > idx_end)
-                               goto out;
-
-                       /* Try to lock request and mark it for writeback */
-                       if (!nfs_set_page_writeback_locked(req))
-                               goto next;
-                       radix_tree_tag_clear(&nfsi->nfs_page_tree,
-                                       req->wb_index, NFS_PAGE_TAG_DIRTY);
-                       nfsi->ndirty--;
-                       nfs_list_remove_request(req);
-                       nfs_list_add_request(req, dst);
-                       res++;
-                       if (res == LONG_MAX)
-                               goto out;
-next:
-                       idx_start = req->wb_index + 1;
-               }
+       if (!list_empty(&desc->pg_list)) {
+               struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
+               if (index != prev->wb_index + 1)
+                       nfs_pageio_doio(desc);
        }
-out:
-       WARN_ON ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty));
-       return res;
 }
 
+#define NFS_SCAN_MAXENTRIES 16
 /**
  * nfs_scan_list - Scan a list for matching requests
  * @nfsi: NFS inode
- * @head: One of the NFS inode request lists
  * @dst: Destination list
  * @idx_start: lower bound of page->index to scan
  * @npages: idx_start + npages sets the upper bound to scan.
+ * @tag: tag to scan for
  *
  * Moves elements from one of the inode request lists.
  * If the number of requests is set to 0, the entire address_space
  * starting at index idx_start, is scanned.
  * The requests are *not* checked to ensure that they form a contiguous set.
- * You must be holding the inode's req_lock when calling this function
+ * You must be holding the inode's i_lock when calling this function
  */
-int nfs_scan_list(struct nfs_inode *nfsi, struct list_head *head,
-               struct list_head *dst, unsigned long idx_start,
-               unsigned int npages)
+int nfs_scan_list(struct nfs_inode *nfsi,
+               struct list_head *dst, pgoff_t idx_start,
+               unsigned int npages, int tag)
 {
        struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
        struct nfs_page *req;
-       unsigned long idx_end;
+       pgoff_t idx_end;
        int found, i;
        int res;
 
@@ -485,9 +390,9 @@ int nfs_scan_list(struct nfs_inode *nfsi, struct list_head *head,
                idx_end = idx_start + npages - 1;
 
        for (;;) {
-               found = radix_tree_gang_lookup(&nfsi->nfs_page_tree,
+               found = radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree,
                                (void **)&pgvec[0], idx_start,
-                               NFS_SCAN_MAXENTRIES);
+                               NFS_SCAN_MAXENTRIES, tag);
                if (found <= 0)
                        break;
                for (i = 0; i < found; i++) {
@@ -495,15 +400,19 @@ int nfs_scan_list(struct nfs_inode *nfsi, struct list_head *head,
                        if (req->wb_index > idx_end)
                                goto out;
                        idx_start = req->wb_index + 1;
-                       if (req->wb_list_head != head)
-                               continue;
-                       if (nfs_set_page_writeback_locked(req)) {
+                       if (nfs_set_page_tag_locked(req)) {
+                               kref_get(&req->wb_kref);
                                nfs_list_remove_request(req);
+                               radix_tree_tag_clear(&nfsi->nfs_page_tree,
+                                               req->wb_index, tag);
                                nfs_list_add_request(req, dst);
                                res++;
+                               if (res == INT_MAX)
+                                       goto out;
                        }
                }
-
+               /* for latency reduction */
+               cond_resched_lock(&nfsi->vfs_inode.i_lock);
        }
 out:
        return res;
@@ -514,7 +423,7 @@ int __init nfs_init_nfspagecache(void)
        nfs_page_cachep = kmem_cache_create("nfs_page",
                                            sizeof(struct nfs_page),
                                            0, SLAB_HWCACHE_ALIGN,
-                                           NULL, NULL);
+                                           NULL);
        if (nfs_page_cachep == NULL)
                return -ENOMEM;