Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfs/pagelist.c |
| 3 | * |
| 4 | * A set of helper functions for managing NFS read and write requests. |
| 5 | * The main purpose of these routines is to provide support for the |
| 6 | * coalescing of several requests into a single RPC call. |
| 7 | * |
| 8 | * Copyright 2000, 2001 (c) Trond Myklebust <trond.myklebust@fys.uio.no> |
| 9 | * |
| 10 | */ |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/slab.h> |
| 13 | #include <linux/file.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 14 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/sunrpc/clnt.h> |
Trond Myklebust | 1313e60 | 2012-01-17 22:04:24 -0500 | [diff] [blame] | 16 | #include <linux/nfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/nfs3.h> |
| 18 | #include <linux/nfs4.h> |
| 19 | #include <linux/nfs_page.h> |
| 20 | #include <linux/nfs_fs.h> |
| 21 | #include <linux/nfs_mount.h> |
Paul Gortmaker | afeacc8 | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 22 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 24 | #include "internal.h" |
Fred Isaman | bae724e | 2011-03-01 01:34:15 +0000 | [diff] [blame] | 25 | #include "pnfs.h" |
Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 26 | |
Anna Schumaker | 0eecb21 | 2014-05-06 09:12:32 -0400 | [diff] [blame] | 27 | #define NFSDBG_FACILITY NFSDBG_PAGECACHE |
| 28 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 29 | static struct kmem_cache *nfs_page_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Anna Schumaker | 00bfa30 | 2014-05-06 09:12:29 -0400 | [diff] [blame] | 31 | static bool nfs_pgarray_set(struct nfs_page_array *p, unsigned int pagecount) |
Fred Isaman | 30dd374 | 2012-04-20 14:47:45 -0400 | [diff] [blame] | 32 | { |
| 33 | p->npages = pagecount; |
| 34 | if (pagecount <= ARRAY_SIZE(p->page_array)) |
| 35 | p->pagevec = p->page_array; |
| 36 | else { |
| 37 | p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL); |
| 38 | if (!p->pagevec) |
| 39 | p->npages = 0; |
| 40 | } |
| 41 | return p->pagevec != NULL; |
| 42 | } |
| 43 | |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 44 | void nfs_pgheader_init(struct nfs_pageio_descriptor *desc, |
| 45 | struct nfs_pgio_header *hdr, |
| 46 | void (*release)(struct nfs_pgio_header *hdr)) |
| 47 | { |
| 48 | hdr->req = nfs_list_entry(desc->pg_list.next); |
| 49 | hdr->inode = desc->pg_inode; |
| 50 | hdr->cred = hdr->req->wb_context->cred; |
| 51 | hdr->io_start = req_offset(hdr->req); |
| 52 | hdr->good_bytes = desc->pg_count; |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 53 | hdr->dreq = desc->pg_dreq; |
Peng Tao | f616638 | 2012-08-02 15:36:09 +0300 | [diff] [blame] | 54 | hdr->layout_private = desc->pg_layout_private; |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 55 | hdr->release = release; |
Fred Isaman | 061ae2e | 2012-04-20 14:47:48 -0400 | [diff] [blame] | 56 | hdr->completion_ops = desc->pg_completion_ops; |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 57 | if (hdr->completion_ops->init_hdr) |
| 58 | hdr->completion_ops->init_hdr(hdr); |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 59 | } |
Bryan Schumaker | 89d77c8 | 2012-07-30 16:05:25 -0400 | [diff] [blame] | 60 | EXPORT_SYMBOL_GPL(nfs_pgheader_init); |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 61 | |
| 62 | void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos) |
| 63 | { |
| 64 | spin_lock(&hdr->lock); |
| 65 | if (pos < hdr->io_start + hdr->good_bytes) { |
| 66 | set_bit(NFS_IOHDR_ERROR, &hdr->flags); |
| 67 | clear_bit(NFS_IOHDR_EOF, &hdr->flags); |
| 68 | hdr->good_bytes = pos - hdr->io_start; |
| 69 | hdr->error = error; |
| 70 | } |
| 71 | spin_unlock(&hdr->lock); |
| 72 | } |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | static inline struct nfs_page * |
| 75 | nfs_page_alloc(void) |
| 76 | { |
Mel Gorman | 192e501 | 2012-07-31 16:45:16 -0700 | [diff] [blame] | 77 | struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_NOIO); |
Jesper Juhl | 72895b1 | 2010-12-09 23:17:15 +0100 | [diff] [blame] | 78 | if (p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | INIT_LIST_HEAD(&p->wb_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | return p; |
| 81 | } |
| 82 | |
| 83 | static inline void |
| 84 | nfs_page_free(struct nfs_page *p) |
| 85 | { |
| 86 | kmem_cache_free(nfs_page_cachep, p); |
| 87 | } |
| 88 | |
Trond Myklebust | 577b423 | 2013-04-08 21:38:12 -0400 | [diff] [blame] | 89 | static void |
| 90 | nfs_iocounter_inc(struct nfs_io_counter *c) |
| 91 | { |
| 92 | atomic_inc(&c->io_count); |
| 93 | } |
| 94 | |
| 95 | static void |
| 96 | nfs_iocounter_dec(struct nfs_io_counter *c) |
| 97 | { |
| 98 | if (atomic_dec_and_test(&c->io_count)) { |
| 99 | clear_bit(NFS_IO_INPROGRESS, &c->flags); |
| 100 | smp_mb__after_clear_bit(); |
| 101 | wake_up_bit(&c->flags, NFS_IO_INPROGRESS); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | static int |
| 106 | __nfs_iocounter_wait(struct nfs_io_counter *c) |
| 107 | { |
| 108 | wait_queue_head_t *wq = bit_waitqueue(&c->flags, NFS_IO_INPROGRESS); |
| 109 | DEFINE_WAIT_BIT(q, &c->flags, NFS_IO_INPROGRESS); |
| 110 | int ret = 0; |
| 111 | |
| 112 | do { |
| 113 | prepare_to_wait(wq, &q.wait, TASK_KILLABLE); |
| 114 | set_bit(NFS_IO_INPROGRESS, &c->flags); |
| 115 | if (atomic_read(&c->io_count) == 0) |
| 116 | break; |
| 117 | ret = nfs_wait_bit_killable(&c->flags); |
| 118 | } while (atomic_read(&c->io_count) != 0); |
| 119 | finish_wait(wq, &q.wait); |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * nfs_iocounter_wait - wait for i/o to complete |
| 125 | * @c: nfs_io_counter to use |
| 126 | * |
| 127 | * returns -ERESTARTSYS if interrupted by a fatal signal. |
| 128 | * Otherwise returns 0 once the io_count hits 0. |
| 129 | */ |
| 130 | int |
| 131 | nfs_iocounter_wait(struct nfs_io_counter *c) |
| 132 | { |
| 133 | if (atomic_read(&c->io_count) == 0) |
| 134 | return 0; |
| 135 | return __nfs_iocounter_wait(c); |
| 136 | } |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | /** |
| 139 | * nfs_create_request - Create an NFS read/write request. |
Chuck Lever | c02f557 | 2011-10-25 12:17:43 -0400 | [diff] [blame] | 140 | * @ctx: open context to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | * @inode: inode to which the request is attached |
| 142 | * @page: page to write |
| 143 | * @offset: starting offset within the page for the write |
| 144 | * @count: number of bytes to read/write |
| 145 | * |
| 146 | * The page must be locked by the caller. This makes sure we never |
Jason Uhlenkott | a19b89c | 2007-04-26 17:25:51 -0700 | [diff] [blame] | 147 | * create two different requests for the same page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | * User should ensure it is safe to sleep in this function. |
| 149 | */ |
| 150 | struct nfs_page * |
| 151 | nfs_create_request(struct nfs_open_context *ctx, struct inode *inode, |
| 152 | struct page *page, |
| 153 | unsigned int offset, unsigned int count) |
| 154 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | struct nfs_page *req; |
Trond Myklebust | b3c54de | 2012-08-13 17:15:50 -0400 | [diff] [blame] | 156 | struct nfs_lock_context *l_ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Trond Myklebust | c58c844 | 2013-03-18 19:45:14 -0400 | [diff] [blame] | 158 | if (test_bit(NFS_CONTEXT_BAD, &ctx->flags)) |
| 159 | return ERR_PTR(-EBADF); |
Trond Myklebust | 18eb884 | 2010-05-13 12:51:02 -0400 | [diff] [blame] | 160 | /* try to allocate the request struct */ |
| 161 | req = nfs_page_alloc(); |
| 162 | if (req == NULL) |
| 163 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Jeff Layton | 015f021 | 2010-10-28 10:10:37 -0400 | [diff] [blame] | 165 | /* get lock context early so we can deal with alloc failures */ |
Trond Myklebust | b3c54de | 2012-08-13 17:15:50 -0400 | [diff] [blame] | 166 | l_ctx = nfs_get_lock_context(ctx); |
| 167 | if (IS_ERR(l_ctx)) { |
Jeff Layton | 015f021 | 2010-10-28 10:10:37 -0400 | [diff] [blame] | 168 | nfs_page_free(req); |
Trond Myklebust | b3c54de | 2012-08-13 17:15:50 -0400 | [diff] [blame] | 169 | return ERR_CAST(l_ctx); |
Jeff Layton | 015f021 | 2010-10-28 10:10:37 -0400 | [diff] [blame] | 170 | } |
Trond Myklebust | b3c54de | 2012-08-13 17:15:50 -0400 | [diff] [blame] | 171 | req->wb_lock_context = l_ctx; |
Trond Myklebust | 577b423 | 2013-04-08 21:38:12 -0400 | [diff] [blame] | 172 | nfs_iocounter_inc(&l_ctx->io_count); |
Jeff Layton | 015f021 | 2010-10-28 10:10:37 -0400 | [diff] [blame] | 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | /* Initialize the request struct. Initially, we assume a |
| 175 | * long write-back delay. This will be adjusted in |
| 176 | * update_nfs_request below if the region is not locked. */ |
| 177 | req->wb_page = page; |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 178 | req->wb_index = page_file_index(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | page_cache_get(page); |
| 180 | req->wb_offset = offset; |
| 181 | req->wb_pgbase = offset; |
| 182 | req->wb_bytes = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | req->wb_context = get_nfs_open_context(ctx); |
Trond Myklebust | c03b402 | 2007-06-17 13:26:38 -0400 | [diff] [blame] | 184 | kref_init(&req->wb_kref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | return req; |
| 186 | } |
| 187 | |
| 188 | /** |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 189 | * nfs_unlock_request - Unlock request and wake up sleepers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | * @req: |
| 191 | */ |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 192 | void nfs_unlock_request(struct nfs_page *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { |
| 194 | if (!NFS_WBACK_BUSY(req)) { |
| 195 | printk(KERN_ERR "NFS: Invalid unlock attempted\n"); |
| 196 | BUG(); |
| 197 | } |
| 198 | smp_mb__before_clear_bit(); |
| 199 | clear_bit(PG_BUSY, &req->wb_flags); |
| 200 | smp_mb__after_clear_bit(); |
Trond Myklebust | 464a98b | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 201 | wake_up_bit(&req->wb_flags, PG_BUSY); |
Trond Myklebust | 3aff4eb | 2012-05-09 14:30:35 -0400 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 205 | * nfs_unlock_and_release_request - Unlock request and release the nfs_page |
| 206 | * @req: |
Trond Myklebust | 3aff4eb | 2012-05-09 14:30:35 -0400 | [diff] [blame] | 207 | */ |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 208 | void nfs_unlock_and_release_request(struct nfs_page *req) |
Trond Myklebust | 3aff4eb | 2012-05-09 14:30:35 -0400 | [diff] [blame] | 209 | { |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 210 | nfs_unlock_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | nfs_release_request(req); |
| 212 | } |
| 213 | |
Trond Myklebust | 4d65c52 | 2011-03-25 14:15:11 -0400 | [diff] [blame] | 214 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | * nfs_clear_request - Free up all resources allocated to the request |
| 216 | * @req: |
| 217 | * |
Trond Myklebust | bb6fbc4 | 2010-03-11 09:19:35 -0500 | [diff] [blame] | 218 | * Release page and open context resources associated with a read/write |
| 219 | * request after it has completed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | */ |
Trond Myklebust | 4d65c52 | 2011-03-25 14:15:11 -0400 | [diff] [blame] | 221 | static void nfs_clear_request(struct nfs_page *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
Trond Myklebust | cd52ed3 | 2006-03-20 13:44:04 -0500 | [diff] [blame] | 223 | struct page *page = req->wb_page; |
Trond Myklebust | bb6fbc4 | 2010-03-11 09:19:35 -0500 | [diff] [blame] | 224 | struct nfs_open_context *ctx = req->wb_context; |
Trond Myklebust | f11ac8d | 2010-06-25 16:35:53 -0400 | [diff] [blame] | 225 | struct nfs_lock_context *l_ctx = req->wb_lock_context; |
Trond Myklebust | bb6fbc4 | 2010-03-11 09:19:35 -0500 | [diff] [blame] | 226 | |
Trond Myklebust | cd52ed3 | 2006-03-20 13:44:04 -0500 | [diff] [blame] | 227 | if (page != NULL) { |
Trond Myklebust | cd52ed3 | 2006-03-20 13:44:04 -0500 | [diff] [blame] | 228 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | req->wb_page = NULL; |
| 230 | } |
Trond Myklebust | f11ac8d | 2010-06-25 16:35:53 -0400 | [diff] [blame] | 231 | if (l_ctx != NULL) { |
Trond Myklebust | 577b423 | 2013-04-08 21:38:12 -0400 | [diff] [blame] | 232 | nfs_iocounter_dec(&l_ctx->io_count); |
Trond Myklebust | f11ac8d | 2010-06-25 16:35:53 -0400 | [diff] [blame] | 233 | nfs_put_lock_context(l_ctx); |
| 234 | req->wb_lock_context = NULL; |
| 235 | } |
Trond Myklebust | bb6fbc4 | 2010-03-11 09:19:35 -0500 | [diff] [blame] | 236 | if (ctx != NULL) { |
| 237 | put_nfs_open_context(ctx); |
| 238 | req->wb_context = NULL; |
| 239 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | |
| 243 | /** |
| 244 | * nfs_release_request - Release the count on an NFS read/write request |
| 245 | * @req: request to release |
| 246 | * |
| 247 | * Note: Should never be called with the spinlock held! |
| 248 | */ |
Trond Myklebust | c03b402 | 2007-06-17 13:26:38 -0400 | [diff] [blame] | 249 | static void nfs_free_request(struct kref *kref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
Trond Myklebust | c03b402 | 2007-06-17 13:26:38 -0400 | [diff] [blame] | 251 | struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Trond Myklebust | bb6fbc4 | 2010-03-11 09:19:35 -0500 | [diff] [blame] | 253 | /* Release struct file and open context */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | nfs_clear_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | nfs_page_free(req); |
| 256 | } |
| 257 | |
Trond Myklebust | c03b402 | 2007-06-17 13:26:38 -0400 | [diff] [blame] | 258 | void nfs_release_request(struct nfs_page *req) |
| 259 | { |
| 260 | kref_put(&req->wb_kref, nfs_free_request); |
| 261 | } |
| 262 | |
Trond Myklebust | 9f557cd | 2010-02-03 08:27:22 -0500 | [diff] [blame] | 263 | static int nfs_wait_bit_uninterruptible(void *word) |
| 264 | { |
| 265 | io_schedule(); |
| 266 | return 0; |
| 267 | } |
| 268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | /** |
| 270 | * nfs_wait_on_request - Wait for a request to complete. |
| 271 | * @req: request to wait upon. |
| 272 | * |
Matthew Wilcox | 150030b | 2007-12-06 16:24:39 -0500 | [diff] [blame] | 273 | * Interruptible by fatal signals only. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | * The user is responsible for holding a count on the request. |
| 275 | */ |
| 276 | int |
| 277 | nfs_wait_on_request(struct nfs_page *req) |
| 278 | { |
Trond Myklebust | 9f557cd | 2010-02-03 08:27:22 -0500 | [diff] [blame] | 279 | return wait_on_bit(&req->wb_flags, PG_BUSY, |
| 280 | nfs_wait_bit_uninterruptible, |
| 281 | TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Benny Halevy | 19345cb | 2011-06-19 18:33:46 -0400 | [diff] [blame] | 284 | bool nfs_generic_pg_test(struct nfs_pageio_descriptor *desc, struct nfs_page *prev, struct nfs_page *req) |
Boaz Harrosh | 5b36c7d | 2011-05-29 11:45:39 +0300 | [diff] [blame] | 285 | { |
| 286 | /* |
| 287 | * FIXME: ideally we should be able to coalesce all requests |
| 288 | * that are not block boundary aligned, but currently this |
| 289 | * is problematic for the case of bsize < PAGE_CACHE_SIZE, |
| 290 | * since nfs_flush_multi and nfs_pagein_multi assume you |
| 291 | * can have only one struct nfs_page. |
| 292 | */ |
| 293 | if (desc->pg_bsize < PAGE_SIZE) |
| 294 | return 0; |
| 295 | |
| 296 | return desc->pg_count + req->wb_bytes <= desc->pg_bsize; |
| 297 | } |
Benny Halevy | 19345cb | 2011-06-19 18:33:46 -0400 | [diff] [blame] | 298 | EXPORT_SYMBOL_GPL(nfs_generic_pg_test); |
Boaz Harrosh | 5b36c7d | 2011-05-29 11:45:39 +0300 | [diff] [blame] | 299 | |
Anna Schumaker | 00bfa30 | 2014-05-06 09:12:29 -0400 | [diff] [blame] | 300 | static inline struct nfs_rw_header *NFS_RW_HEADER(struct nfs_pgio_header *hdr) |
| 301 | { |
| 302 | return container_of(hdr, struct nfs_rw_header, header); |
| 303 | } |
| 304 | |
| 305 | /** |
Anna Schumaker | 4a0de55 | 2014-05-06 09:12:30 -0400 | [diff] [blame] | 306 | * nfs_rw_header_alloc - Allocate a header for a read or write |
| 307 | * @ops: Read or write function vector |
| 308 | */ |
| 309 | struct nfs_rw_header *nfs_rw_header_alloc(const struct nfs_rw_ops *ops) |
| 310 | { |
| 311 | struct nfs_rw_header *header = ops->rw_alloc_header(); |
| 312 | |
| 313 | if (header) { |
| 314 | struct nfs_pgio_header *hdr = &header->header; |
| 315 | |
| 316 | INIT_LIST_HEAD(&hdr->pages); |
| 317 | INIT_LIST_HEAD(&hdr->rpc_list); |
| 318 | spin_lock_init(&hdr->lock); |
| 319 | atomic_set(&hdr->refcnt, 0); |
| 320 | hdr->rw_ops = ops; |
| 321 | } |
| 322 | return header; |
| 323 | } |
| 324 | EXPORT_SYMBOL_GPL(nfs_rw_header_alloc); |
| 325 | |
| 326 | /* |
| 327 | * nfs_rw_header_free - Free a read or write header |
| 328 | * @hdr: The header to free |
| 329 | */ |
| 330 | void nfs_rw_header_free(struct nfs_pgio_header *hdr) |
| 331 | { |
| 332 | hdr->rw_ops->rw_free_header(NFS_RW_HEADER(hdr)); |
| 333 | } |
| 334 | EXPORT_SYMBOL_GPL(nfs_rw_header_free); |
| 335 | |
| 336 | /** |
Anna Schumaker | 00bfa30 | 2014-05-06 09:12:29 -0400 | [diff] [blame] | 337 | * nfs_pgio_data_alloc - Allocate pageio data |
| 338 | * @hdr: The header making a request |
| 339 | * @pagecount: Number of pages to create |
| 340 | */ |
| 341 | struct nfs_pgio_data *nfs_pgio_data_alloc(struct nfs_pgio_header *hdr, |
| 342 | unsigned int pagecount) |
| 343 | { |
| 344 | struct nfs_pgio_data *data, *prealloc; |
| 345 | |
| 346 | prealloc = &NFS_RW_HEADER(hdr)->rpc_data; |
| 347 | if (prealloc->header == NULL) |
| 348 | data = prealloc; |
| 349 | else |
| 350 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 351 | if (!data) |
| 352 | goto out; |
| 353 | |
| 354 | if (nfs_pgarray_set(&data->pages, pagecount)) { |
| 355 | data->header = hdr; |
| 356 | atomic_inc(&hdr->refcnt); |
| 357 | } else { |
| 358 | if (data != prealloc) |
| 359 | kfree(data); |
| 360 | data = NULL; |
| 361 | } |
| 362 | out: |
| 363 | return data; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * nfs_pgio_data_release - Properly free pageio data |
| 368 | * @data: The data to release |
| 369 | */ |
| 370 | void nfs_pgio_data_release(struct nfs_pgio_data *data) |
| 371 | { |
| 372 | struct nfs_pgio_header *hdr = data->header; |
| 373 | struct nfs_rw_header *pageio_header = NFS_RW_HEADER(hdr); |
| 374 | |
| 375 | put_nfs_open_context(data->args.context); |
| 376 | if (data->pages.pagevec != data->pages.page_array) |
| 377 | kfree(data->pages.pagevec); |
| 378 | if (data == &pageio_header->rpc_data) { |
| 379 | data->header = NULL; |
| 380 | data = NULL; |
| 381 | } |
| 382 | if (atomic_dec_and_test(&hdr->refcnt)) |
| 383 | hdr->completion_ops->completion(hdr); |
| 384 | /* Note: we only free the rpc_task after callbacks are done. |
| 385 | * See the comment in rpc_free_task() for why |
| 386 | */ |
| 387 | kfree(data); |
| 388 | } |
| 389 | EXPORT_SYMBOL_GPL(nfs_pgio_data_release); |
| 390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | /** |
Anna Schumaker | ce59515 | 2014-05-06 09:12:34 -0400 | [diff] [blame] | 392 | * nfs_pgio_rpcsetup - Set up arguments for a pageio call |
| 393 | * @data: The pageio data |
| 394 | * @count: Number of bytes to read |
| 395 | * @offset: Initial offset |
| 396 | * @how: How to commit data (writes only) |
| 397 | * @cinfo: Commit information for the call (writes only) |
| 398 | */ |
| 399 | void nfs_pgio_rpcsetup(struct nfs_pgio_data *data, |
| 400 | unsigned int count, unsigned int offset, |
| 401 | int how, struct nfs_commit_info *cinfo) |
| 402 | { |
| 403 | struct nfs_page *req = data->header->req; |
| 404 | |
| 405 | /* Set up the RPC argument and reply structs |
| 406 | * NB: take care not to mess about with data->commit et al. */ |
| 407 | |
| 408 | data->args.fh = NFS_FH(data->header->inode); |
| 409 | data->args.offset = req_offset(req) + offset; |
| 410 | /* pnfs_set_layoutcommit needs this */ |
| 411 | data->mds_offset = data->args.offset; |
| 412 | data->args.pgbase = req->wb_pgbase + offset; |
| 413 | data->args.pages = data->pages.pagevec; |
| 414 | data->args.count = count; |
| 415 | data->args.context = get_nfs_open_context(req->wb_context); |
| 416 | data->args.lock_context = req->wb_lock_context; |
| 417 | data->args.stable = NFS_UNSTABLE; |
| 418 | switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) { |
| 419 | case 0: |
| 420 | break; |
| 421 | case FLUSH_COND_STABLE: |
| 422 | if (nfs_reqs_to_commit(cinfo)) |
| 423 | break; |
| 424 | default: |
| 425 | data->args.stable = NFS_FILE_SYNC; |
| 426 | } |
| 427 | |
| 428 | data->res.fattr = &data->fattr; |
| 429 | data->res.count = count; |
| 430 | data->res.eof = 0; |
| 431 | data->res.verf = &data->verf; |
| 432 | nfs_fattr_init(&data->fattr); |
| 433 | } |
| 434 | |
| 435 | /** |
Anna Schumaker | a4cdda5 | 2014-05-06 09:12:31 -0400 | [diff] [blame] | 436 | * nfs_pgio_prepare - Prepare pageio data to go over the wire |
| 437 | * @task: The current task |
| 438 | * @calldata: pageio data to prepare |
| 439 | */ |
Anna Schumaker | 6f92fa4 | 2014-05-06 09:12:33 -0400 | [diff] [blame] | 440 | static void nfs_pgio_prepare(struct rpc_task *task, void *calldata) |
Anna Schumaker | a4cdda5 | 2014-05-06 09:12:31 -0400 | [diff] [blame] | 441 | { |
| 442 | struct nfs_pgio_data *data = calldata; |
| 443 | int err; |
| 444 | err = NFS_PROTO(data->header->inode)->pgio_rpc_prepare(task, data); |
| 445 | if (err) |
| 446 | rpc_exit(task, err); |
| 447 | } |
| 448 | |
| 449 | /** |
Anna Schumaker | 844c9e6 | 2014-05-06 09:12:35 -0400 | [diff] [blame^] | 450 | * nfs_pgio_error - Clean up from a pageio error |
| 451 | * @desc: IO descriptor |
| 452 | * @hdr: pageio header |
| 453 | */ |
| 454 | int nfs_pgio_error(struct nfs_pageio_descriptor *desc, |
| 455 | struct nfs_pgio_header *hdr) |
| 456 | { |
| 457 | struct nfs_pgio_data *data; |
| 458 | |
| 459 | set_bit(NFS_IOHDR_REDO, &hdr->flags); |
| 460 | while (!list_empty(&hdr->rpc_list)) { |
| 461 | data = list_first_entry(&hdr->rpc_list, struct nfs_pgio_data, list); |
| 462 | list_del(&data->list); |
| 463 | nfs_pgio_data_release(data); |
| 464 | } |
| 465 | desc->pg_completion_ops->error_cleanup(&desc->pg_list); |
| 466 | return -ENOMEM; |
| 467 | } |
| 468 | |
| 469 | /** |
Anna Schumaker | a4cdda5 | 2014-05-06 09:12:31 -0400 | [diff] [blame] | 470 | * nfs_pgio_release - Release pageio data |
| 471 | * @calldata: The pageio data to release |
| 472 | */ |
Anna Schumaker | 6f92fa4 | 2014-05-06 09:12:33 -0400 | [diff] [blame] | 473 | static void nfs_pgio_release(void *calldata) |
Anna Schumaker | a4cdda5 | 2014-05-06 09:12:31 -0400 | [diff] [blame] | 474 | { |
| 475 | struct nfs_pgio_data *data = calldata; |
| 476 | if (data->header->rw_ops->rw_release) |
| 477 | data->header->rw_ops->rw_release(data); |
| 478 | nfs_pgio_data_release(data); |
| 479 | } |
| 480 | |
| 481 | /** |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 482 | * nfs_pageio_init - initialise a page io descriptor |
| 483 | * @desc: pointer to descriptor |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 484 | * @inode: pointer to inode |
| 485 | * @doio: pointer to io function |
| 486 | * @bsize: io block size |
| 487 | * @io_flags: extra parameters for the io function |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 488 | */ |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 489 | void nfs_pageio_init(struct nfs_pageio_descriptor *desc, |
| 490 | struct inode *inode, |
Trond Myklebust | 1751c36 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 491 | const struct nfs_pageio_ops *pg_ops, |
Fred Isaman | 061ae2e | 2012-04-20 14:47:48 -0400 | [diff] [blame] | 492 | const struct nfs_pgio_completion_ops *compl_ops, |
Anna Schumaker | 4a0de55 | 2014-05-06 09:12:30 -0400 | [diff] [blame] | 493 | const struct nfs_rw_ops *rw_ops, |
Trond Myklebust | 84dde76 | 2007-05-04 14:44:06 -0400 | [diff] [blame] | 494 | size_t bsize, |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 495 | int io_flags) |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 496 | { |
| 497 | INIT_LIST_HEAD(&desc->pg_list); |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 498 | desc->pg_bytes_written = 0; |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 499 | desc->pg_count = 0; |
| 500 | desc->pg_bsize = bsize; |
| 501 | desc->pg_base = 0; |
Trond Myklebust | b31268a | 2011-03-21 17:02:00 -0400 | [diff] [blame] | 502 | desc->pg_moreio = 0; |
Trond Myklebust | d9156f9 | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 503 | desc->pg_recoalesce = 0; |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 504 | desc->pg_inode = inode; |
Trond Myklebust | 1751c36 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 505 | desc->pg_ops = pg_ops; |
Fred Isaman | 061ae2e | 2012-04-20 14:47:48 -0400 | [diff] [blame] | 506 | desc->pg_completion_ops = compl_ops; |
Anna Schumaker | 4a0de55 | 2014-05-06 09:12:30 -0400 | [diff] [blame] | 507 | desc->pg_rw_ops = rw_ops; |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 508 | desc->pg_ioflags = io_flags; |
| 509 | desc->pg_error = 0; |
Fred Isaman | 94ad1c8 | 2011-03-01 01:34:14 +0000 | [diff] [blame] | 510 | desc->pg_lseg = NULL; |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 511 | desc->pg_dreq = NULL; |
Peng Tao | f616638 | 2012-08-02 15:36:09 +0300 | [diff] [blame] | 512 | desc->pg_layout_private = NULL; |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 513 | } |
Bryan Schumaker | 89d77c8 | 2012-07-30 16:05:25 -0400 | [diff] [blame] | 514 | EXPORT_SYMBOL_GPL(nfs_pageio_init); |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 515 | |
Anna Schumaker | 0eecb21 | 2014-05-06 09:12:32 -0400 | [diff] [blame] | 516 | /** |
| 517 | * nfs_pgio_result - Basic pageio error handling |
| 518 | * @task: The task that ran |
| 519 | * @calldata: Pageio data to check |
| 520 | */ |
Anna Schumaker | 6f92fa4 | 2014-05-06 09:12:33 -0400 | [diff] [blame] | 521 | static void nfs_pgio_result(struct rpc_task *task, void *calldata) |
Anna Schumaker | 0eecb21 | 2014-05-06 09:12:32 -0400 | [diff] [blame] | 522 | { |
| 523 | struct nfs_pgio_data *data = calldata; |
| 524 | struct inode *inode = data->header->inode; |
| 525 | |
| 526 | dprintk("NFS: %s: %5u, (status %d)\n", __func__, |
| 527 | task->tk_pid, task->tk_status); |
| 528 | |
| 529 | if (data->header->rw_ops->rw_done(task, data, inode) != 0) |
| 530 | return; |
| 531 | if (task->tk_status < 0) |
| 532 | nfs_set_pgio_error(data->header, task->tk_status, data->args.offset); |
| 533 | else |
| 534 | data->header->rw_ops->rw_result(task, data); |
| 535 | } |
| 536 | |
Trond Myklebust | 4109bb7 | 2013-09-06 11:09:38 -0400 | [diff] [blame] | 537 | static bool nfs_match_open_context(const struct nfs_open_context *ctx1, |
| 538 | const struct nfs_open_context *ctx2) |
| 539 | { |
| 540 | return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state; |
| 541 | } |
| 542 | |
| 543 | static bool nfs_match_lock_context(const struct nfs_lock_context *l1, |
| 544 | const struct nfs_lock_context *l2) |
| 545 | { |
| 546 | return l1->lockowner.l_owner == l2->lockowner.l_owner |
| 547 | && l1->lockowner.l_pid == l2->lockowner.l_pid; |
| 548 | } |
| 549 | |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 550 | /** |
| 551 | * nfs_can_coalesce_requests - test two requests for compatibility |
| 552 | * @prev: pointer to nfs_page |
| 553 | * @req: pointer to nfs_page |
| 554 | * |
| 555 | * The nfs_page structures 'prev' and 'req' are compared to ensure that the |
| 556 | * page data area they describe is contiguous, and that their RPC |
| 557 | * credentials, NFSv4 open state, and lockowners are the same. |
| 558 | * |
| 559 | * Return 'true' if this is the case, else return 'false'. |
| 560 | */ |
Benny Halevy | 18ad0a9 | 2011-05-25 21:03:56 +0300 | [diff] [blame] | 561 | static bool nfs_can_coalesce_requests(struct nfs_page *prev, |
| 562 | struct nfs_page *req, |
| 563 | struct nfs_pageio_descriptor *pgio) |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 564 | { |
Trond Myklebust | 4109bb7 | 2013-09-06 11:09:38 -0400 | [diff] [blame] | 565 | if (!nfs_match_open_context(req->wb_context, prev->wb_context)) |
Benny Halevy | 18ad0a9 | 2011-05-25 21:03:56 +0300 | [diff] [blame] | 566 | return false; |
Trond Myklebust | 4109bb7 | 2013-09-06 11:09:38 -0400 | [diff] [blame] | 567 | if (req->wb_context->dentry->d_inode->i_flock != NULL && |
| 568 | !nfs_match_lock_context(req->wb_lock_context, prev->wb_lock_context)) |
Benny Halevy | 18ad0a9 | 2011-05-25 21:03:56 +0300 | [diff] [blame] | 569 | return false; |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 570 | if (req->wb_pgbase != 0) |
Benny Halevy | 18ad0a9 | 2011-05-25 21:03:56 +0300 | [diff] [blame] | 571 | return false; |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 572 | if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE) |
Benny Halevy | 18ad0a9 | 2011-05-25 21:03:56 +0300 | [diff] [blame] | 573 | return false; |
Fred Isaman | 1825a0d | 2012-04-20 19:55:31 -0400 | [diff] [blame] | 574 | if (req_offset(req) != req_offset(prev) + prev->wb_bytes) |
| 575 | return false; |
Trond Myklebust | 1751c36 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 576 | return pgio->pg_ops->pg_test(pgio, prev, req); |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /** |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 580 | * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list. |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 581 | * @desc: destination io descriptor |
| 582 | * @req: request |
| 583 | * |
| 584 | * Returns true if the request 'req' was successfully coalesced into the |
| 585 | * existing list of pages 'desc'. |
| 586 | */ |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 587 | static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc, |
| 588 | struct nfs_page *req) |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 589 | { |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 590 | if (desc->pg_count != 0) { |
| 591 | struct nfs_page *prev; |
| 592 | |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 593 | prev = nfs_list_entry(desc->pg_list.prev); |
Fred Isaman | 94ad1c8 | 2011-03-01 01:34:14 +0000 | [diff] [blame] | 594 | if (!nfs_can_coalesce_requests(prev, req, desc)) |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 595 | return 0; |
Boaz Harrosh | 5b36c7d | 2011-05-29 11:45:39 +0300 | [diff] [blame] | 596 | } else { |
Trond Myklebust | d8007d4 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 597 | if (desc->pg_ops->pg_init) |
| 598 | desc->pg_ops->pg_init(desc, req); |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 599 | desc->pg_base = req->wb_pgbase; |
Boaz Harrosh | 5b36c7d | 2011-05-29 11:45:39 +0300 | [diff] [blame] | 600 | } |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 601 | nfs_list_remove_request(req); |
| 602 | nfs_list_add_request(req, &desc->pg_list); |
Boaz Harrosh | 5b36c7d | 2011-05-29 11:45:39 +0300 | [diff] [blame] | 603 | desc->pg_count += req->wb_bytes; |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 604 | return 1; |
| 605 | } |
| 606 | |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 607 | /* |
| 608 | * Helper for nfs_pageio_add_request and nfs_pageio_complete |
| 609 | */ |
| 610 | static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc) |
| 611 | { |
| 612 | if (!list_empty(&desc->pg_list)) { |
Trond Myklebust | 1751c36 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 613 | int error = desc->pg_ops->pg_doio(desc); |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 614 | if (error < 0) |
| 615 | desc->pg_error = error; |
| 616 | else |
| 617 | desc->pg_bytes_written += desc->pg_count; |
| 618 | } |
| 619 | if (list_empty(&desc->pg_list)) { |
| 620 | desc->pg_count = 0; |
| 621 | desc->pg_base = 0; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * nfs_pageio_add_request - Attempt to coalesce a request into a page list. |
| 627 | * @desc: destination io descriptor |
| 628 | * @req: request |
| 629 | * |
| 630 | * Returns true if the request 'req' was successfully coalesced into the |
| 631 | * existing list of pages 'desc'. |
| 632 | */ |
Trond Myklebust | d9156f9 | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 633 | static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc, |
Trond Myklebust | 8b09bee | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 634 | struct nfs_page *req) |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 635 | { |
| 636 | while (!nfs_pageio_do_add_request(desc, req)) { |
Trond Myklebust | b31268a | 2011-03-21 17:02:00 -0400 | [diff] [blame] | 637 | desc->pg_moreio = 1; |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 638 | nfs_pageio_doio(desc); |
| 639 | if (desc->pg_error < 0) |
| 640 | return 0; |
Trond Myklebust | b31268a | 2011-03-21 17:02:00 -0400 | [diff] [blame] | 641 | desc->pg_moreio = 0; |
Trond Myklebust | d9156f9 | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 642 | if (desc->pg_recoalesce) |
| 643 | return 0; |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 644 | } |
| 645 | return 1; |
| 646 | } |
| 647 | |
Trond Myklebust | d9156f9 | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 648 | static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc) |
| 649 | { |
| 650 | LIST_HEAD(head); |
| 651 | |
| 652 | do { |
| 653 | list_splice_init(&desc->pg_list, &head); |
| 654 | desc->pg_bytes_written -= desc->pg_count; |
| 655 | desc->pg_count = 0; |
| 656 | desc->pg_base = 0; |
| 657 | desc->pg_recoalesce = 0; |
| 658 | |
| 659 | while (!list_empty(&head)) { |
| 660 | struct nfs_page *req; |
| 661 | |
| 662 | req = list_first_entry(&head, struct nfs_page, wb_list); |
| 663 | nfs_list_remove_request(req); |
| 664 | if (__nfs_pageio_add_request(desc, req)) |
| 665 | continue; |
| 666 | if (desc->pg_error < 0) |
| 667 | return 0; |
| 668 | break; |
| 669 | } |
| 670 | } while (desc->pg_recoalesce); |
| 671 | return 1; |
| 672 | } |
| 673 | |
| 674 | int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc, |
| 675 | struct nfs_page *req) |
| 676 | { |
| 677 | int ret; |
| 678 | |
| 679 | do { |
| 680 | ret = __nfs_pageio_add_request(desc, req); |
| 681 | if (ret) |
| 682 | break; |
| 683 | if (desc->pg_error < 0) |
| 684 | break; |
| 685 | ret = nfs_do_recoalesce(desc); |
| 686 | } while (ret); |
| 687 | return ret; |
| 688 | } |
Bryan Schumaker | 89d77c8 | 2012-07-30 16:05:25 -0400 | [diff] [blame] | 689 | EXPORT_SYMBOL_GPL(nfs_pageio_add_request); |
Trond Myklebust | d9156f9 | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 690 | |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 691 | /** |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 692 | * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor |
| 693 | * @desc: pointer to io descriptor |
| 694 | */ |
| 695 | void nfs_pageio_complete(struct nfs_pageio_descriptor *desc) |
| 696 | { |
Trond Myklebust | d9156f9 | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 697 | for (;;) { |
| 698 | nfs_pageio_doio(desc); |
| 699 | if (!desc->pg_recoalesce) |
| 700 | break; |
| 701 | if (!nfs_do_recoalesce(desc)) |
| 702 | break; |
| 703 | } |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 704 | } |
Bryan Schumaker | 89d77c8 | 2012-07-30 16:05:25 -0400 | [diff] [blame] | 705 | EXPORT_SYMBOL_GPL(nfs_pageio_complete); |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 706 | |
Trond Myklebust | 7fe7f84 | 2007-05-20 10:18:27 -0400 | [diff] [blame] | 707 | /** |
| 708 | * nfs_pageio_cond_complete - Conditional I/O completion |
| 709 | * @desc: pointer to io descriptor |
| 710 | * @index: page index |
| 711 | * |
| 712 | * It is important to ensure that processes don't try to take locks |
| 713 | * on non-contiguous ranges of pages as that might deadlock. This |
| 714 | * function should be called before attempting to wait on a locked |
| 715 | * nfs_page. It will complete the I/O if the page index 'index' |
| 716 | * is not contiguous with the existing list of pages in 'desc'. |
| 717 | */ |
| 718 | void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index) |
| 719 | { |
| 720 | if (!list_empty(&desc->pg_list)) { |
| 721 | struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev); |
| 722 | if (index != prev->wb_index + 1) |
Trond Myklebust | d9156f9 | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 723 | nfs_pageio_complete(desc); |
Trond Myklebust | 7fe7f84 | 2007-05-20 10:18:27 -0400 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 727 | int __init nfs_init_nfspagecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | { |
| 729 | nfs_page_cachep = kmem_cache_create("nfs_page", |
| 730 | sizeof(struct nfs_page), |
| 731 | 0, SLAB_HWCACHE_ALIGN, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 732 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | if (nfs_page_cachep == NULL) |
| 734 | return -ENOMEM; |
| 735 | |
| 736 | return 0; |
| 737 | } |
| 738 | |
David Brownell | 266bee8 | 2006-06-27 12:59:15 -0700 | [diff] [blame] | 739 | void nfs_destroy_nfspagecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | { |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 741 | kmem_cache_destroy(nfs_page_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | } |
| 743 | |
Anna Schumaker | 6f92fa4 | 2014-05-06 09:12:33 -0400 | [diff] [blame] | 744 | const struct rpc_call_ops nfs_pgio_common_ops = { |
| 745 | .rpc_call_prepare = nfs_pgio_prepare, |
| 746 | .rpc_call_done = nfs_pgio_result, |
| 747 | .rpc_release = nfs_pgio_release, |
| 748 | }; |