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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | static struct kmem_cache *nfs_page_cachep; |
Anna Schumaker | ef2c488 | 2014-05-06 09:12:36 -0400 | [diff] [blame] | 30 | static const struct rpc_call_ops nfs_pgio_common_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 32 | static void nfs_free_request(struct nfs_page *); |
| 33 | |
Anna Schumaker | 00bfa30 | 2014-05-06 09:12:29 -0400 | [diff] [blame] | 34 | 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] | 35 | { |
| 36 | p->npages = pagecount; |
| 37 | if (pagecount <= ARRAY_SIZE(p->page_array)) |
| 38 | p->pagevec = p->page_array; |
| 39 | else { |
| 40 | p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL); |
| 41 | if (!p->pagevec) |
| 42 | p->npages = 0; |
| 43 | } |
| 44 | return p->pagevec != NULL; |
| 45 | } |
| 46 | |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 47 | void nfs_pgheader_init(struct nfs_pageio_descriptor *desc, |
| 48 | struct nfs_pgio_header *hdr, |
| 49 | void (*release)(struct nfs_pgio_header *hdr)) |
| 50 | { |
| 51 | hdr->req = nfs_list_entry(desc->pg_list.next); |
| 52 | hdr->inode = desc->pg_inode; |
| 53 | hdr->cred = hdr->req->wb_context->cred; |
| 54 | hdr->io_start = req_offset(hdr->req); |
| 55 | hdr->good_bytes = desc->pg_count; |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 56 | hdr->dreq = desc->pg_dreq; |
Peng Tao | f616638 | 2012-08-02 15:36:09 +0300 | [diff] [blame] | 57 | hdr->layout_private = desc->pg_layout_private; |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 58 | hdr->release = release; |
Fred Isaman | 061ae2e | 2012-04-20 14:47:48 -0400 | [diff] [blame] | 59 | hdr->completion_ops = desc->pg_completion_ops; |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 60 | if (hdr->completion_ops->init_hdr) |
| 61 | hdr->completion_ops->init_hdr(hdr); |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 62 | } |
Bryan Schumaker | 89d77c8 | 2012-07-30 16:05:25 -0400 | [diff] [blame] | 63 | EXPORT_SYMBOL_GPL(nfs_pgheader_init); |
Fred Isaman | 4db6e0b | 2012-04-20 14:47:46 -0400 | [diff] [blame] | 64 | |
| 65 | void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos) |
| 66 | { |
| 67 | spin_lock(&hdr->lock); |
| 68 | if (pos < hdr->io_start + hdr->good_bytes) { |
| 69 | set_bit(NFS_IOHDR_ERROR, &hdr->flags); |
| 70 | clear_bit(NFS_IOHDR_EOF, &hdr->flags); |
| 71 | hdr->good_bytes = pos - hdr->io_start; |
| 72 | hdr->error = error; |
| 73 | } |
| 74 | spin_unlock(&hdr->lock); |
| 75 | } |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | static inline struct nfs_page * |
| 78 | nfs_page_alloc(void) |
| 79 | { |
Mel Gorman | 192e501 | 2012-07-31 16:45:16 -0700 | [diff] [blame] | 80 | struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_NOIO); |
Jesper Juhl | 72895b1 | 2010-12-09 23:17:15 +0100 | [diff] [blame] | 81 | if (p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | INIT_LIST_HEAD(&p->wb_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | return p; |
| 84 | } |
| 85 | |
| 86 | static inline void |
| 87 | nfs_page_free(struct nfs_page *p) |
| 88 | { |
| 89 | kmem_cache_free(nfs_page_cachep, p); |
| 90 | } |
| 91 | |
Trond Myklebust | 577b423 | 2013-04-08 21:38:12 -0400 | [diff] [blame] | 92 | static void |
| 93 | nfs_iocounter_inc(struct nfs_io_counter *c) |
| 94 | { |
| 95 | atomic_inc(&c->io_count); |
| 96 | } |
| 97 | |
| 98 | static void |
| 99 | nfs_iocounter_dec(struct nfs_io_counter *c) |
| 100 | { |
| 101 | if (atomic_dec_and_test(&c->io_count)) { |
| 102 | clear_bit(NFS_IO_INPROGRESS, &c->flags); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 103 | smp_mb__after_atomic(); |
Trond Myklebust | 577b423 | 2013-04-08 21:38:12 -0400 | [diff] [blame] | 104 | wake_up_bit(&c->flags, NFS_IO_INPROGRESS); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static int |
| 109 | __nfs_iocounter_wait(struct nfs_io_counter *c) |
| 110 | { |
| 111 | wait_queue_head_t *wq = bit_waitqueue(&c->flags, NFS_IO_INPROGRESS); |
| 112 | DEFINE_WAIT_BIT(q, &c->flags, NFS_IO_INPROGRESS); |
| 113 | int ret = 0; |
| 114 | |
| 115 | do { |
| 116 | prepare_to_wait(wq, &q.wait, TASK_KILLABLE); |
| 117 | set_bit(NFS_IO_INPROGRESS, &c->flags); |
| 118 | if (atomic_read(&c->io_count) == 0) |
| 119 | break; |
| 120 | ret = nfs_wait_bit_killable(&c->flags); |
| 121 | } while (atomic_read(&c->io_count) != 0); |
| 122 | finish_wait(wq, &q.wait); |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * nfs_iocounter_wait - wait for i/o to complete |
| 128 | * @c: nfs_io_counter to use |
| 129 | * |
| 130 | * returns -ERESTARTSYS if interrupted by a fatal signal. |
| 131 | * Otherwise returns 0 once the io_count hits 0. |
| 132 | */ |
| 133 | int |
| 134 | nfs_iocounter_wait(struct nfs_io_counter *c) |
| 135 | { |
| 136 | if (atomic_read(&c->io_count) == 0) |
| 137 | return 0; |
| 138 | return __nfs_iocounter_wait(c); |
| 139 | } |
| 140 | |
Trond Myklebust | f868089 | 2014-05-29 11:45:57 -0400 | [diff] [blame] | 141 | static int nfs_wait_bit_uninterruptible(void *word) |
| 142 | { |
| 143 | io_schedule(); |
| 144 | return 0; |
| 145 | } |
| 146 | |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 147 | /* |
| 148 | * nfs_page_group_lock - lock the head of the page group |
| 149 | * @req - request in group that is to be locked |
| 150 | * |
| 151 | * this lock must be held if modifying the page group list |
| 152 | */ |
| 153 | void |
| 154 | nfs_page_group_lock(struct nfs_page *req) |
| 155 | { |
| 156 | struct nfs_page *head = req->wb_head; |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 157 | |
| 158 | WARN_ON_ONCE(head != head->wb_head); |
| 159 | |
Trond Myklebust | f868089 | 2014-05-29 11:45:57 -0400 | [diff] [blame] | 160 | wait_on_bit_lock(&head->wb_flags, PG_HEADLOCK, |
| 161 | nfs_wait_bit_uninterruptible, |
| 162 | TASK_UNINTERRUPTIBLE); |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* |
| 166 | * nfs_page_group_unlock - unlock the head of the page group |
| 167 | * @req - request in group that is to be unlocked |
| 168 | */ |
| 169 | void |
| 170 | nfs_page_group_unlock(struct nfs_page *req) |
| 171 | { |
| 172 | struct nfs_page *head = req->wb_head; |
| 173 | |
| 174 | WARN_ON_ONCE(head != head->wb_head); |
| 175 | |
Linus Torvalds | d1e1cda | 2014-06-10 15:02:42 -0700 | [diff] [blame] | 176 | smp_mb__before_atomic(); |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 177 | clear_bit(PG_HEADLOCK, &head->wb_flags); |
Linus Torvalds | d1e1cda | 2014-06-10 15:02:42 -0700 | [diff] [blame] | 178 | smp_mb__after_atomic(); |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 179 | wake_up_bit(&head->wb_flags, PG_HEADLOCK); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * nfs_page_group_sync_on_bit_locked |
| 184 | * |
| 185 | * must be called with page group lock held |
| 186 | */ |
| 187 | static bool |
| 188 | nfs_page_group_sync_on_bit_locked(struct nfs_page *req, unsigned int bit) |
| 189 | { |
| 190 | struct nfs_page *head = req->wb_head; |
| 191 | struct nfs_page *tmp; |
| 192 | |
| 193 | WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_flags)); |
| 194 | WARN_ON_ONCE(test_and_set_bit(bit, &req->wb_flags)); |
| 195 | |
| 196 | tmp = req->wb_this_page; |
| 197 | while (tmp != req) { |
| 198 | if (!test_bit(bit, &tmp->wb_flags)) |
| 199 | return false; |
| 200 | tmp = tmp->wb_this_page; |
| 201 | } |
| 202 | |
| 203 | /* true! reset all bits */ |
| 204 | tmp = req; |
| 205 | do { |
| 206 | clear_bit(bit, &tmp->wb_flags); |
| 207 | tmp = tmp->wb_this_page; |
| 208 | } while (tmp != req); |
| 209 | |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * nfs_page_group_sync_on_bit - set bit on current request, but only |
| 215 | * return true if the bit is set for all requests in page group |
| 216 | * @req - request in page group |
| 217 | * @bit - PG_* bit that is used to sync page group |
| 218 | */ |
| 219 | bool nfs_page_group_sync_on_bit(struct nfs_page *req, unsigned int bit) |
| 220 | { |
| 221 | bool ret; |
| 222 | |
| 223 | nfs_page_group_lock(req); |
| 224 | ret = nfs_page_group_sync_on_bit_locked(req, bit); |
| 225 | nfs_page_group_unlock(req); |
| 226 | |
| 227 | return ret; |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * nfs_page_group_init - Initialize the page group linkage for @req |
| 232 | * @req - a new nfs request |
| 233 | * @prev - the previous request in page group, or NULL if @req is the first |
| 234 | * or only request in the group (the head). |
| 235 | */ |
| 236 | static inline void |
| 237 | nfs_page_group_init(struct nfs_page *req, struct nfs_page *prev) |
| 238 | { |
| 239 | WARN_ON_ONCE(prev == req); |
| 240 | |
| 241 | if (!prev) { |
Weston Andros Adamson | 85710a8 | 2014-07-11 10:20:46 -0400 | [diff] [blame^] | 242 | /* a head request */ |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 243 | req->wb_head = req; |
| 244 | req->wb_this_page = req; |
| 245 | } else { |
Weston Andros Adamson | 85710a8 | 2014-07-11 10:20:46 -0400 | [diff] [blame^] | 246 | /* a subrequest */ |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 247 | WARN_ON_ONCE(prev->wb_this_page != prev->wb_head); |
| 248 | WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &prev->wb_head->wb_flags)); |
| 249 | req->wb_head = prev->wb_head; |
| 250 | req->wb_this_page = prev->wb_this_page; |
| 251 | prev->wb_this_page = req; |
| 252 | |
Weston Andros Adamson | 85710a8 | 2014-07-11 10:20:46 -0400 | [diff] [blame^] | 253 | /* All subrequests take a ref on the head request until |
| 254 | * nfs_page_group_destroy is called */ |
| 255 | kref_get(&req->wb_head->wb_kref); |
| 256 | |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 257 | /* grab extra ref if head request has extra ref from |
| 258 | * the write/commit path to handle handoff between write |
| 259 | * and commit lists */ |
Weston Andros Adamson | 17089a2 | 2014-07-11 10:20:45 -0400 | [diff] [blame] | 260 | if (test_bit(PG_INODE_REF, &prev->wb_head->wb_flags)) { |
| 261 | set_bit(PG_INODE_REF, &req->wb_flags); |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 262 | kref_get(&req->wb_kref); |
Weston Andros Adamson | 17089a2 | 2014-07-11 10:20:45 -0400 | [diff] [blame] | 263 | } |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * nfs_page_group_destroy - sync the destruction of page groups |
| 269 | * @req - request that no longer needs the page group |
| 270 | * |
| 271 | * releases the page group reference from each member once all |
| 272 | * members have called this function. |
| 273 | */ |
| 274 | static void |
| 275 | nfs_page_group_destroy(struct kref *kref) |
| 276 | { |
| 277 | struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref); |
| 278 | struct nfs_page *tmp, *next; |
| 279 | |
Weston Andros Adamson | 85710a8 | 2014-07-11 10:20:46 -0400 | [diff] [blame^] | 280 | /* subrequests must release the ref on the head request */ |
| 281 | if (req->wb_head != req) |
| 282 | nfs_release_request(req->wb_head); |
| 283 | |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 284 | if (!nfs_page_group_sync_on_bit(req, PG_TEARDOWN)) |
| 285 | return; |
| 286 | |
| 287 | tmp = req; |
| 288 | do { |
| 289 | next = tmp->wb_this_page; |
| 290 | /* unlink and free */ |
| 291 | tmp->wb_this_page = tmp; |
| 292 | tmp->wb_head = tmp; |
| 293 | nfs_free_request(tmp); |
| 294 | tmp = next; |
| 295 | } while (tmp != req); |
| 296 | } |
| 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | /** |
| 299 | * nfs_create_request - Create an NFS read/write request. |
Chuck Lever | c02f557 | 2011-10-25 12:17:43 -0400 | [diff] [blame] | 300 | * @ctx: open context to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | * @page: page to write |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 302 | * @last: last nfs request created for this page group or NULL if head |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | * @offset: starting offset within the page for the write |
| 304 | * @count: number of bytes to read/write |
| 305 | * |
| 306 | * 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] | 307 | * create two different requests for the same page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | * User should ensure it is safe to sleep in this function. |
| 309 | */ |
| 310 | struct nfs_page * |
Weston Andros Adamson | 8c8f1ac1 | 2014-05-15 11:56:42 -0400 | [diff] [blame] | 311 | nfs_create_request(struct nfs_open_context *ctx, struct page *page, |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 312 | struct nfs_page *last, unsigned int offset, |
| 313 | unsigned int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | struct nfs_page *req; |
Trond Myklebust | b3c54de | 2012-08-13 17:15:50 -0400 | [diff] [blame] | 316 | struct nfs_lock_context *l_ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
Trond Myklebust | c58c844 | 2013-03-18 19:45:14 -0400 | [diff] [blame] | 318 | if (test_bit(NFS_CONTEXT_BAD, &ctx->flags)) |
| 319 | return ERR_PTR(-EBADF); |
Trond Myklebust | 18eb884 | 2010-05-13 12:51:02 -0400 | [diff] [blame] | 320 | /* try to allocate the request struct */ |
| 321 | req = nfs_page_alloc(); |
| 322 | if (req == NULL) |
| 323 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Jeff Layton | 015f021 | 2010-10-28 10:10:37 -0400 | [diff] [blame] | 325 | /* get lock context early so we can deal with alloc failures */ |
Trond Myklebust | b3c54de | 2012-08-13 17:15:50 -0400 | [diff] [blame] | 326 | l_ctx = nfs_get_lock_context(ctx); |
| 327 | if (IS_ERR(l_ctx)) { |
Jeff Layton | 015f021 | 2010-10-28 10:10:37 -0400 | [diff] [blame] | 328 | nfs_page_free(req); |
Trond Myklebust | b3c54de | 2012-08-13 17:15:50 -0400 | [diff] [blame] | 329 | return ERR_CAST(l_ctx); |
Jeff Layton | 015f021 | 2010-10-28 10:10:37 -0400 | [diff] [blame] | 330 | } |
Trond Myklebust | b3c54de | 2012-08-13 17:15:50 -0400 | [diff] [blame] | 331 | req->wb_lock_context = l_ctx; |
Trond Myklebust | 577b423 | 2013-04-08 21:38:12 -0400 | [diff] [blame] | 332 | nfs_iocounter_inc(&l_ctx->io_count); |
Jeff Layton | 015f021 | 2010-10-28 10:10:37 -0400 | [diff] [blame] | 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | /* Initialize the request struct. Initially, we assume a |
| 335 | * long write-back delay. This will be adjusted in |
| 336 | * update_nfs_request below if the region is not locked. */ |
| 337 | req->wb_page = page; |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 338 | req->wb_index = page_file_index(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | page_cache_get(page); |
| 340 | req->wb_offset = offset; |
| 341 | req->wb_pgbase = offset; |
| 342 | req->wb_bytes = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | req->wb_context = get_nfs_open_context(ctx); |
Trond Myklebust | c03b402 | 2007-06-17 13:26:38 -0400 | [diff] [blame] | 344 | kref_init(&req->wb_kref); |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 345 | nfs_page_group_init(req, last); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | return req; |
| 347 | } |
| 348 | |
| 349 | /** |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 350 | * nfs_unlock_request - Unlock request and wake up sleepers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | * @req: |
| 352 | */ |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 353 | void nfs_unlock_request(struct nfs_page *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | { |
| 355 | if (!NFS_WBACK_BUSY(req)) { |
| 356 | printk(KERN_ERR "NFS: Invalid unlock attempted\n"); |
| 357 | BUG(); |
| 358 | } |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 359 | smp_mb__before_atomic(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | clear_bit(PG_BUSY, &req->wb_flags); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 361 | smp_mb__after_atomic(); |
Trond Myklebust | 464a98b | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 362 | wake_up_bit(&req->wb_flags, PG_BUSY); |
Trond Myklebust | 3aff4eb | 2012-05-09 14:30:35 -0400 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /** |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 366 | * nfs_unlock_and_release_request - Unlock request and release the nfs_page |
| 367 | * @req: |
Trond Myklebust | 3aff4eb | 2012-05-09 14:30:35 -0400 | [diff] [blame] | 368 | */ |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 369 | void nfs_unlock_and_release_request(struct nfs_page *req) |
Trond Myklebust | 3aff4eb | 2012-05-09 14:30:35 -0400 | [diff] [blame] | 370 | { |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 371 | nfs_unlock_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | nfs_release_request(req); |
| 373 | } |
| 374 | |
Trond Myklebust | 4d65c52 | 2011-03-25 14:15:11 -0400 | [diff] [blame] | 375 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | * nfs_clear_request - Free up all resources allocated to the request |
| 377 | * @req: |
| 378 | * |
Trond Myklebust | bb6fbc4 | 2010-03-11 09:19:35 -0500 | [diff] [blame] | 379 | * Release page and open context resources associated with a read/write |
| 380 | * request after it has completed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | */ |
Trond Myklebust | 4d65c52 | 2011-03-25 14:15:11 -0400 | [diff] [blame] | 382 | static void nfs_clear_request(struct nfs_page *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | { |
Trond Myklebust | cd52ed3 | 2006-03-20 13:44:04 -0500 | [diff] [blame] | 384 | struct page *page = req->wb_page; |
Trond Myklebust | bb6fbc4 | 2010-03-11 09:19:35 -0500 | [diff] [blame] | 385 | struct nfs_open_context *ctx = req->wb_context; |
Trond Myklebust | f11ac8d | 2010-06-25 16:35:53 -0400 | [diff] [blame] | 386 | struct nfs_lock_context *l_ctx = req->wb_lock_context; |
Trond Myklebust | bb6fbc4 | 2010-03-11 09:19:35 -0500 | [diff] [blame] | 387 | |
Trond Myklebust | cd52ed3 | 2006-03-20 13:44:04 -0500 | [diff] [blame] | 388 | if (page != NULL) { |
Trond Myklebust | cd52ed3 | 2006-03-20 13:44:04 -0500 | [diff] [blame] | 389 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | req->wb_page = NULL; |
| 391 | } |
Trond Myklebust | f11ac8d | 2010-06-25 16:35:53 -0400 | [diff] [blame] | 392 | if (l_ctx != NULL) { |
Trond Myklebust | 577b423 | 2013-04-08 21:38:12 -0400 | [diff] [blame] | 393 | nfs_iocounter_dec(&l_ctx->io_count); |
Trond Myklebust | f11ac8d | 2010-06-25 16:35:53 -0400 | [diff] [blame] | 394 | nfs_put_lock_context(l_ctx); |
| 395 | req->wb_lock_context = NULL; |
| 396 | } |
Trond Myklebust | bb6fbc4 | 2010-03-11 09:19:35 -0500 | [diff] [blame] | 397 | if (ctx != NULL) { |
| 398 | put_nfs_open_context(ctx); |
| 399 | req->wb_context = NULL; |
| 400 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | /** |
| 404 | * nfs_release_request - Release the count on an NFS read/write request |
| 405 | * @req: request to release |
| 406 | * |
| 407 | * Note: Should never be called with the spinlock held! |
| 408 | */ |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 409 | static void nfs_free_request(struct nfs_page *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | { |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 411 | WARN_ON_ONCE(req->wb_this_page != req); |
| 412 | |
| 413 | /* extra debug: make sure no sync bits are still set */ |
| 414 | WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags)); |
Weston Andros Adamson | 67d0338 | 2014-05-15 11:56:46 -0400 | [diff] [blame] | 415 | WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags)); |
| 416 | WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags)); |
Weston Andros Adamson | 20633f0 | 2014-05-15 11:56:47 -0400 | [diff] [blame] | 417 | WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags)); |
| 418 | WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Trond Myklebust | bb6fbc4 | 2010-03-11 09:19:35 -0500 | [diff] [blame] | 420 | /* Release struct file and open context */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | nfs_clear_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | nfs_page_free(req); |
| 423 | } |
| 424 | |
Trond Myklebust | c03b402 | 2007-06-17 13:26:38 -0400 | [diff] [blame] | 425 | void nfs_release_request(struct nfs_page *req) |
| 426 | { |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 427 | kref_put(&req->wb_kref, nfs_page_group_destroy); |
Trond Myklebust | 9f557cd | 2010-02-03 08:27:22 -0500 | [diff] [blame] | 428 | } |
| 429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | /** |
| 431 | * nfs_wait_on_request - Wait for a request to complete. |
| 432 | * @req: request to wait upon. |
| 433 | * |
Matthew Wilcox | 150030b | 2007-12-06 16:24:39 -0500 | [diff] [blame] | 434 | * Interruptible by fatal signals only. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | * The user is responsible for holding a count on the request. |
| 436 | */ |
| 437 | int |
| 438 | nfs_wait_on_request(struct nfs_page *req) |
| 439 | { |
Trond Myklebust | 9f557cd | 2010-02-03 08:27:22 -0500 | [diff] [blame] | 440 | return wait_on_bit(&req->wb_flags, PG_BUSY, |
| 441 | nfs_wait_bit_uninterruptible, |
| 442 | TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Weston Andros Adamson | b4fdac1 | 2014-05-15 11:56:43 -0400 | [diff] [blame] | 445 | /* |
| 446 | * nfs_generic_pg_test - determine if requests can be coalesced |
| 447 | * @desc: pointer to descriptor |
| 448 | * @prev: previous request in desc, or NULL |
| 449 | * @req: this request |
| 450 | * |
| 451 | * Returns zero if @req can be coalesced into @desc, otherwise it returns |
| 452 | * the size of the request. |
| 453 | */ |
| 454 | size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc, |
| 455 | struct nfs_page *prev, struct nfs_page *req) |
Boaz Harrosh | 5b36c7d | 2011-05-29 11:45:39 +0300 | [diff] [blame] | 456 | { |
Weston Andros Adamson | f0cb9ab | 2014-05-15 11:56:52 -0400 | [diff] [blame] | 457 | if (desc->pg_count > desc->pg_bsize) { |
| 458 | /* should never happen */ |
| 459 | WARN_ON_ONCE(1); |
Boaz Harrosh | 5b36c7d | 2011-05-29 11:45:39 +0300 | [diff] [blame] | 460 | return 0; |
Weston Andros Adamson | f0cb9ab | 2014-05-15 11:56:52 -0400 | [diff] [blame] | 461 | } |
Boaz Harrosh | 5b36c7d | 2011-05-29 11:45:39 +0300 | [diff] [blame] | 462 | |
Weston Andros Adamson | f0cb9ab | 2014-05-15 11:56:52 -0400 | [diff] [blame] | 463 | return min(desc->pg_bsize - desc->pg_count, (size_t)req->wb_bytes); |
Boaz Harrosh | 5b36c7d | 2011-05-29 11:45:39 +0300 | [diff] [blame] | 464 | } |
Benny Halevy | 19345cb | 2011-06-19 18:33:46 -0400 | [diff] [blame] | 465 | EXPORT_SYMBOL_GPL(nfs_generic_pg_test); |
Boaz Harrosh | 5b36c7d | 2011-05-29 11:45:39 +0300 | [diff] [blame] | 466 | |
Anna Schumaker | 00bfa30 | 2014-05-06 09:12:29 -0400 | [diff] [blame] | 467 | static inline struct nfs_rw_header *NFS_RW_HEADER(struct nfs_pgio_header *hdr) |
| 468 | { |
| 469 | return container_of(hdr, struct nfs_rw_header, header); |
| 470 | } |
| 471 | |
| 472 | /** |
Anna Schumaker | 4a0de55 | 2014-05-06 09:12:30 -0400 | [diff] [blame] | 473 | * nfs_rw_header_alloc - Allocate a header for a read or write |
| 474 | * @ops: Read or write function vector |
| 475 | */ |
| 476 | struct nfs_rw_header *nfs_rw_header_alloc(const struct nfs_rw_ops *ops) |
| 477 | { |
| 478 | struct nfs_rw_header *header = ops->rw_alloc_header(); |
| 479 | |
| 480 | if (header) { |
| 481 | struct nfs_pgio_header *hdr = &header->header; |
| 482 | |
| 483 | INIT_LIST_HEAD(&hdr->pages); |
Anna Schumaker | 4a0de55 | 2014-05-06 09:12:30 -0400 | [diff] [blame] | 484 | spin_lock_init(&hdr->lock); |
| 485 | atomic_set(&hdr->refcnt, 0); |
| 486 | hdr->rw_ops = ops; |
| 487 | } |
| 488 | return header; |
| 489 | } |
| 490 | EXPORT_SYMBOL_GPL(nfs_rw_header_alloc); |
| 491 | |
| 492 | /* |
| 493 | * nfs_rw_header_free - Free a read or write header |
| 494 | * @hdr: The header to free |
| 495 | */ |
| 496 | void nfs_rw_header_free(struct nfs_pgio_header *hdr) |
| 497 | { |
| 498 | hdr->rw_ops->rw_free_header(NFS_RW_HEADER(hdr)); |
| 499 | } |
| 500 | EXPORT_SYMBOL_GPL(nfs_rw_header_free); |
| 501 | |
| 502 | /** |
Anna Schumaker | 00bfa30 | 2014-05-06 09:12:29 -0400 | [diff] [blame] | 503 | * nfs_pgio_data_alloc - Allocate pageio data |
| 504 | * @hdr: The header making a request |
| 505 | * @pagecount: Number of pages to create |
| 506 | */ |
Anna Schumaker | ef2c488 | 2014-05-06 09:12:36 -0400 | [diff] [blame] | 507 | static struct nfs_pgio_data *nfs_pgio_data_alloc(struct nfs_pgio_header *hdr, |
| 508 | unsigned int pagecount) |
Anna Schumaker | 00bfa30 | 2014-05-06 09:12:29 -0400 | [diff] [blame] | 509 | { |
| 510 | struct nfs_pgio_data *data, *prealloc; |
| 511 | |
| 512 | prealloc = &NFS_RW_HEADER(hdr)->rpc_data; |
| 513 | if (prealloc->header == NULL) |
| 514 | data = prealloc; |
| 515 | else |
| 516 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 517 | if (!data) |
| 518 | goto out; |
| 519 | |
| 520 | if (nfs_pgarray_set(&data->pages, pagecount)) { |
| 521 | data->header = hdr; |
| 522 | atomic_inc(&hdr->refcnt); |
| 523 | } else { |
| 524 | if (data != prealloc) |
| 525 | kfree(data); |
| 526 | data = NULL; |
| 527 | } |
| 528 | out: |
| 529 | return data; |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * nfs_pgio_data_release - Properly free pageio data |
| 534 | * @data: The data to release |
| 535 | */ |
| 536 | void nfs_pgio_data_release(struct nfs_pgio_data *data) |
| 537 | { |
| 538 | struct nfs_pgio_header *hdr = data->header; |
| 539 | struct nfs_rw_header *pageio_header = NFS_RW_HEADER(hdr); |
| 540 | |
| 541 | put_nfs_open_context(data->args.context); |
| 542 | if (data->pages.pagevec != data->pages.page_array) |
| 543 | kfree(data->pages.pagevec); |
| 544 | if (data == &pageio_header->rpc_data) { |
| 545 | data->header = NULL; |
| 546 | data = NULL; |
| 547 | } |
| 548 | if (atomic_dec_and_test(&hdr->refcnt)) |
| 549 | hdr->completion_ops->completion(hdr); |
| 550 | /* Note: we only free the rpc_task after callbacks are done. |
| 551 | * See the comment in rpc_free_task() for why |
| 552 | */ |
| 553 | kfree(data); |
| 554 | } |
| 555 | EXPORT_SYMBOL_GPL(nfs_pgio_data_release); |
| 556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | /** |
Anna Schumaker | ce59515 | 2014-05-06 09:12:34 -0400 | [diff] [blame] | 558 | * nfs_pgio_rpcsetup - Set up arguments for a pageio call |
| 559 | * @data: The pageio data |
| 560 | * @count: Number of bytes to read |
| 561 | * @offset: Initial offset |
| 562 | * @how: How to commit data (writes only) |
| 563 | * @cinfo: Commit information for the call (writes only) |
| 564 | */ |
Anna Schumaker | ef2c488 | 2014-05-06 09:12:36 -0400 | [diff] [blame] | 565 | static void nfs_pgio_rpcsetup(struct nfs_pgio_data *data, |
Anna Schumaker | ce59515 | 2014-05-06 09:12:34 -0400 | [diff] [blame] | 566 | unsigned int count, unsigned int offset, |
| 567 | int how, struct nfs_commit_info *cinfo) |
| 568 | { |
| 569 | struct nfs_page *req = data->header->req; |
| 570 | |
| 571 | /* Set up the RPC argument and reply structs |
| 572 | * NB: take care not to mess about with data->commit et al. */ |
| 573 | |
| 574 | data->args.fh = NFS_FH(data->header->inode); |
| 575 | data->args.offset = req_offset(req) + offset; |
| 576 | /* pnfs_set_layoutcommit needs this */ |
| 577 | data->mds_offset = data->args.offset; |
| 578 | data->args.pgbase = req->wb_pgbase + offset; |
| 579 | data->args.pages = data->pages.pagevec; |
| 580 | data->args.count = count; |
| 581 | data->args.context = get_nfs_open_context(req->wb_context); |
| 582 | data->args.lock_context = req->wb_lock_context; |
| 583 | data->args.stable = NFS_UNSTABLE; |
| 584 | switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) { |
| 585 | case 0: |
| 586 | break; |
| 587 | case FLUSH_COND_STABLE: |
| 588 | if (nfs_reqs_to_commit(cinfo)) |
| 589 | break; |
| 590 | default: |
| 591 | data->args.stable = NFS_FILE_SYNC; |
| 592 | } |
| 593 | |
| 594 | data->res.fattr = &data->fattr; |
| 595 | data->res.count = count; |
| 596 | data->res.eof = 0; |
| 597 | data->res.verf = &data->verf; |
| 598 | nfs_fattr_init(&data->fattr); |
| 599 | } |
| 600 | |
| 601 | /** |
Anna Schumaker | a4cdda5 | 2014-05-06 09:12:31 -0400 | [diff] [blame] | 602 | * nfs_pgio_prepare - Prepare pageio data to go over the wire |
| 603 | * @task: The current task |
| 604 | * @calldata: pageio data to prepare |
| 605 | */ |
Anna Schumaker | 6f92fa4 | 2014-05-06 09:12:33 -0400 | [diff] [blame] | 606 | static void nfs_pgio_prepare(struct rpc_task *task, void *calldata) |
Anna Schumaker | a4cdda5 | 2014-05-06 09:12:31 -0400 | [diff] [blame] | 607 | { |
| 608 | struct nfs_pgio_data *data = calldata; |
| 609 | int err; |
| 610 | err = NFS_PROTO(data->header->inode)->pgio_rpc_prepare(task, data); |
| 611 | if (err) |
| 612 | rpc_exit(task, err); |
| 613 | } |
| 614 | |
Anna Schumaker | 1ed26f3 | 2014-05-06 09:12:37 -0400 | [diff] [blame] | 615 | int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_data *data, |
| 616 | const struct rpc_call_ops *call_ops, int how, int flags) |
| 617 | { |
| 618 | struct rpc_task *task; |
| 619 | struct rpc_message msg = { |
| 620 | .rpc_argp = &data->args, |
| 621 | .rpc_resp = &data->res, |
| 622 | .rpc_cred = data->header->cred, |
| 623 | }; |
| 624 | struct rpc_task_setup task_setup_data = { |
| 625 | .rpc_client = clnt, |
| 626 | .task = &data->task, |
| 627 | .rpc_message = &msg, |
| 628 | .callback_ops = call_ops, |
| 629 | .callback_data = data, |
| 630 | .workqueue = nfsiod_workqueue, |
| 631 | .flags = RPC_TASK_ASYNC | flags, |
| 632 | }; |
| 633 | int ret = 0; |
| 634 | |
| 635 | data->header->rw_ops->rw_initiate(data, &msg, &task_setup_data, how); |
| 636 | |
| 637 | dprintk("NFS: %5u initiated pgio call " |
| 638 | "(req %s/%llu, %u bytes @ offset %llu)\n", |
| 639 | data->task.tk_pid, |
| 640 | data->header->inode->i_sb->s_id, |
| 641 | (unsigned long long)NFS_FILEID(data->header->inode), |
| 642 | data->args.count, |
| 643 | (unsigned long long)data->args.offset); |
| 644 | |
| 645 | task = rpc_run_task(&task_setup_data); |
| 646 | if (IS_ERR(task)) { |
| 647 | ret = PTR_ERR(task); |
| 648 | goto out; |
| 649 | } |
| 650 | if (how & FLUSH_SYNC) { |
| 651 | ret = rpc_wait_for_completion_task(task); |
| 652 | if (ret == 0) |
| 653 | ret = task->tk_status; |
| 654 | } |
| 655 | rpc_put_task(task); |
| 656 | out: |
| 657 | return ret; |
| 658 | } |
| 659 | EXPORT_SYMBOL_GPL(nfs_initiate_pgio); |
| 660 | |
Anna Schumaker | a4cdda5 | 2014-05-06 09:12:31 -0400 | [diff] [blame] | 661 | /** |
Anna Schumaker | 844c9e6 | 2014-05-06 09:12:35 -0400 | [diff] [blame] | 662 | * nfs_pgio_error - Clean up from a pageio error |
| 663 | * @desc: IO descriptor |
| 664 | * @hdr: pageio header |
| 665 | */ |
Anna Schumaker | ef2c488 | 2014-05-06 09:12:36 -0400 | [diff] [blame] | 666 | static int nfs_pgio_error(struct nfs_pageio_descriptor *desc, |
Anna Schumaker | 844c9e6 | 2014-05-06 09:12:35 -0400 | [diff] [blame] | 667 | struct nfs_pgio_header *hdr) |
| 668 | { |
Anna Schumaker | 844c9e6 | 2014-05-06 09:12:35 -0400 | [diff] [blame] | 669 | set_bit(NFS_IOHDR_REDO, &hdr->flags); |
Weston Andros Adamson | 7f71472 | 2014-05-15 11:56:53 -0400 | [diff] [blame] | 670 | nfs_pgio_data_release(hdr->data); |
| 671 | hdr->data = NULL; |
Anna Schumaker | 844c9e6 | 2014-05-06 09:12:35 -0400 | [diff] [blame] | 672 | desc->pg_completion_ops->error_cleanup(&desc->pg_list); |
| 673 | return -ENOMEM; |
| 674 | } |
| 675 | |
| 676 | /** |
Anna Schumaker | a4cdda5 | 2014-05-06 09:12:31 -0400 | [diff] [blame] | 677 | * nfs_pgio_release - Release pageio data |
| 678 | * @calldata: The pageio data to release |
| 679 | */ |
Anna Schumaker | 6f92fa4 | 2014-05-06 09:12:33 -0400 | [diff] [blame] | 680 | static void nfs_pgio_release(void *calldata) |
Anna Schumaker | a4cdda5 | 2014-05-06 09:12:31 -0400 | [diff] [blame] | 681 | { |
| 682 | struct nfs_pgio_data *data = calldata; |
| 683 | if (data->header->rw_ops->rw_release) |
| 684 | data->header->rw_ops->rw_release(data); |
| 685 | nfs_pgio_data_release(data); |
| 686 | } |
| 687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | /** |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 689 | * nfs_pageio_init - initialise a page io descriptor |
| 690 | * @desc: pointer to descriptor |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 691 | * @inode: pointer to inode |
| 692 | * @doio: pointer to io function |
| 693 | * @bsize: io block size |
| 694 | * @io_flags: extra parameters for the io function |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 695 | */ |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 696 | void nfs_pageio_init(struct nfs_pageio_descriptor *desc, |
| 697 | struct inode *inode, |
Trond Myklebust | 1751c36 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 698 | const struct nfs_pageio_ops *pg_ops, |
Fred Isaman | 061ae2e | 2012-04-20 14:47:48 -0400 | [diff] [blame] | 699 | const struct nfs_pgio_completion_ops *compl_ops, |
Anna Schumaker | 4a0de55 | 2014-05-06 09:12:30 -0400 | [diff] [blame] | 700 | const struct nfs_rw_ops *rw_ops, |
Trond Myklebust | 84dde76 | 2007-05-04 14:44:06 -0400 | [diff] [blame] | 701 | size_t bsize, |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 702 | int io_flags) |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 703 | { |
| 704 | INIT_LIST_HEAD(&desc->pg_list); |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 705 | desc->pg_bytes_written = 0; |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 706 | desc->pg_count = 0; |
| 707 | desc->pg_bsize = bsize; |
| 708 | desc->pg_base = 0; |
Trond Myklebust | b31268a | 2011-03-21 17:02:00 -0400 | [diff] [blame] | 709 | desc->pg_moreio = 0; |
Trond Myklebust | d9156f9 | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 710 | desc->pg_recoalesce = 0; |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 711 | desc->pg_inode = inode; |
Trond Myklebust | 1751c36 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 712 | desc->pg_ops = pg_ops; |
Fred Isaman | 061ae2e | 2012-04-20 14:47:48 -0400 | [diff] [blame] | 713 | desc->pg_completion_ops = compl_ops; |
Anna Schumaker | 4a0de55 | 2014-05-06 09:12:30 -0400 | [diff] [blame] | 714 | desc->pg_rw_ops = rw_ops; |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 715 | desc->pg_ioflags = io_flags; |
| 716 | desc->pg_error = 0; |
Fred Isaman | 94ad1c8 | 2011-03-01 01:34:14 +0000 | [diff] [blame] | 717 | desc->pg_lseg = NULL; |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 718 | desc->pg_dreq = NULL; |
Peng Tao | f616638 | 2012-08-02 15:36:09 +0300 | [diff] [blame] | 719 | desc->pg_layout_private = NULL; |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 720 | } |
Bryan Schumaker | 89d77c8 | 2012-07-30 16:05:25 -0400 | [diff] [blame] | 721 | EXPORT_SYMBOL_GPL(nfs_pageio_init); |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 722 | |
Anna Schumaker | 0eecb21 | 2014-05-06 09:12:32 -0400 | [diff] [blame] | 723 | /** |
| 724 | * nfs_pgio_result - Basic pageio error handling |
| 725 | * @task: The task that ran |
| 726 | * @calldata: Pageio data to check |
| 727 | */ |
Anna Schumaker | 6f92fa4 | 2014-05-06 09:12:33 -0400 | [diff] [blame] | 728 | static void nfs_pgio_result(struct rpc_task *task, void *calldata) |
Anna Schumaker | 0eecb21 | 2014-05-06 09:12:32 -0400 | [diff] [blame] | 729 | { |
| 730 | struct nfs_pgio_data *data = calldata; |
| 731 | struct inode *inode = data->header->inode; |
| 732 | |
| 733 | dprintk("NFS: %s: %5u, (status %d)\n", __func__, |
| 734 | task->tk_pid, task->tk_status); |
| 735 | |
| 736 | if (data->header->rw_ops->rw_done(task, data, inode) != 0) |
| 737 | return; |
| 738 | if (task->tk_status < 0) |
| 739 | nfs_set_pgio_error(data->header, task->tk_status, data->args.offset); |
| 740 | else |
| 741 | data->header->rw_ops->rw_result(task, data); |
| 742 | } |
| 743 | |
Anna Schumaker | ef2c488 | 2014-05-06 09:12:36 -0400 | [diff] [blame] | 744 | /* |
Anna Schumaker | ef2c488 | 2014-05-06 09:12:36 -0400 | [diff] [blame] | 745 | * Create an RPC task for the given read or write request and kick it. |
| 746 | * The page must have been locked by the caller. |
| 747 | * |
| 748 | * It may happen that the page we're passed is not marked dirty. |
| 749 | * This is the case if nfs_updatepage detects a conflicting request |
| 750 | * that has been written but not committed. |
| 751 | */ |
Weston Andros Adamson | f0cb9ab | 2014-05-15 11:56:52 -0400 | [diff] [blame] | 752 | int nfs_generic_pgio(struct nfs_pageio_descriptor *desc, |
| 753 | struct nfs_pgio_header *hdr) |
Anna Schumaker | ef2c488 | 2014-05-06 09:12:36 -0400 | [diff] [blame] | 754 | { |
| 755 | struct nfs_page *req; |
| 756 | struct page **pages; |
| 757 | struct nfs_pgio_data *data; |
| 758 | struct list_head *head = &desc->pg_list; |
| 759 | struct nfs_commit_info cinfo; |
| 760 | |
| 761 | data = nfs_pgio_data_alloc(hdr, nfs_page_array_len(desc->pg_base, |
| 762 | desc->pg_count)); |
| 763 | if (!data) |
| 764 | return nfs_pgio_error(desc, hdr); |
| 765 | |
| 766 | nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq); |
| 767 | pages = data->pages.pagevec; |
| 768 | while (!list_empty(head)) { |
| 769 | req = nfs_list_entry(head->next); |
| 770 | nfs_list_remove_request(req); |
| 771 | nfs_list_add_request(req, &hdr->pages); |
| 772 | *pages++ = req->wb_page; |
| 773 | } |
| 774 | |
| 775 | if ((desc->pg_ioflags & FLUSH_COND_STABLE) && |
| 776 | (desc->pg_moreio || nfs_reqs_to_commit(&cinfo))) |
| 777 | desc->pg_ioflags &= ~FLUSH_COND_STABLE; |
| 778 | |
| 779 | /* Set up the argument struct */ |
| 780 | nfs_pgio_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo); |
Weston Andros Adamson | 7f71472 | 2014-05-15 11:56:53 -0400 | [diff] [blame] | 781 | hdr->data = data; |
Anna Schumaker | ef2c488 | 2014-05-06 09:12:36 -0400 | [diff] [blame] | 782 | desc->pg_rpc_callops = &nfs_pgio_common_ops; |
| 783 | return 0; |
| 784 | } |
Weston Andros Adamson | f0cb9ab | 2014-05-15 11:56:52 -0400 | [diff] [blame] | 785 | EXPORT_SYMBOL_GPL(nfs_generic_pgio); |
Anna Schumaker | ef2c488 | 2014-05-06 09:12:36 -0400 | [diff] [blame] | 786 | |
Anna Schumaker | 41d8d5b | 2014-05-06 09:12:40 -0400 | [diff] [blame] | 787 | static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc) |
Anna Schumaker | cf485fc | 2014-05-06 09:12:39 -0400 | [diff] [blame] | 788 | { |
| 789 | struct nfs_rw_header *rw_hdr; |
| 790 | struct nfs_pgio_header *hdr; |
| 791 | int ret; |
| 792 | |
| 793 | rw_hdr = nfs_rw_header_alloc(desc->pg_rw_ops); |
| 794 | if (!rw_hdr) { |
| 795 | desc->pg_completion_ops->error_cleanup(&desc->pg_list); |
| 796 | return -ENOMEM; |
| 797 | } |
| 798 | hdr = &rw_hdr->header; |
| 799 | nfs_pgheader_init(desc, hdr, nfs_rw_header_free); |
| 800 | atomic_inc(&hdr->refcnt); |
| 801 | ret = nfs_generic_pgio(desc, hdr); |
| 802 | if (ret == 0) |
Weston Andros Adamson | 7f71472 | 2014-05-15 11:56:53 -0400 | [diff] [blame] | 803 | ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode), |
| 804 | hdr->data, desc->pg_rpc_callops, |
| 805 | desc->pg_ioflags, 0); |
Anna Schumaker | cf485fc | 2014-05-06 09:12:39 -0400 | [diff] [blame] | 806 | if (atomic_dec_and_test(&hdr->refcnt)) |
| 807 | hdr->completion_ops->completion(hdr); |
| 808 | return ret; |
| 809 | } |
| 810 | |
Trond Myklebust | 4109bb7 | 2013-09-06 11:09:38 -0400 | [diff] [blame] | 811 | static bool nfs_match_open_context(const struct nfs_open_context *ctx1, |
| 812 | const struct nfs_open_context *ctx2) |
| 813 | { |
| 814 | return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state; |
| 815 | } |
| 816 | |
| 817 | static bool nfs_match_lock_context(const struct nfs_lock_context *l1, |
| 818 | const struct nfs_lock_context *l2) |
| 819 | { |
| 820 | return l1->lockowner.l_owner == l2->lockowner.l_owner |
| 821 | && l1->lockowner.l_pid == l2->lockowner.l_pid; |
| 822 | } |
| 823 | |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 824 | /** |
| 825 | * nfs_can_coalesce_requests - test two requests for compatibility |
| 826 | * @prev: pointer to nfs_page |
| 827 | * @req: pointer to nfs_page |
| 828 | * |
| 829 | * The nfs_page structures 'prev' and 'req' are compared to ensure that the |
| 830 | * page data area they describe is contiguous, and that their RPC |
| 831 | * credentials, NFSv4 open state, and lockowners are the same. |
| 832 | * |
| 833 | * Return 'true' if this is the case, else return 'false'. |
| 834 | */ |
Benny Halevy | 18ad0a9 | 2011-05-25 21:03:56 +0300 | [diff] [blame] | 835 | static bool nfs_can_coalesce_requests(struct nfs_page *prev, |
| 836 | struct nfs_page *req, |
| 837 | struct nfs_pageio_descriptor *pgio) |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 838 | { |
Weston Andros Adamson | b4fdac1 | 2014-05-15 11:56:43 -0400 | [diff] [blame] | 839 | size_t size; |
| 840 | |
Weston Andros Adamson | ab75e41 | 2014-05-15 11:56:44 -0400 | [diff] [blame] | 841 | if (prev) { |
| 842 | if (!nfs_match_open_context(req->wb_context, prev->wb_context)) |
| 843 | return false; |
| 844 | if (req->wb_context->dentry->d_inode->i_flock != NULL && |
| 845 | !nfs_match_lock_context(req->wb_lock_context, |
| 846 | prev->wb_lock_context)) |
| 847 | return false; |
Weston Andros Adamson | ab75e41 | 2014-05-15 11:56:44 -0400 | [diff] [blame] | 848 | if (req_offset(req) != req_offset(prev) + prev->wb_bytes) |
| 849 | return false; |
| 850 | } |
Weston Andros Adamson | b4fdac1 | 2014-05-15 11:56:43 -0400 | [diff] [blame] | 851 | size = pgio->pg_ops->pg_test(pgio, prev, req); |
Weston Andros Adamson | f0cb9ab | 2014-05-15 11:56:52 -0400 | [diff] [blame] | 852 | WARN_ON_ONCE(size > req->wb_bytes); |
| 853 | if (size && size < req->wb_bytes) |
| 854 | req->wb_bytes = size; |
Weston Andros Adamson | b4fdac1 | 2014-05-15 11:56:43 -0400 | [diff] [blame] | 855 | return size > 0; |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | /** |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 859 | * 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] | 860 | * @desc: destination io descriptor |
| 861 | * @req: request |
| 862 | * |
| 863 | * Returns true if the request 'req' was successfully coalesced into the |
| 864 | * existing list of pages 'desc'. |
| 865 | */ |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 866 | static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc, |
| 867 | struct nfs_page *req) |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 868 | { |
Weston Andros Adamson | ab75e41 | 2014-05-15 11:56:44 -0400 | [diff] [blame] | 869 | struct nfs_page *prev = NULL; |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 870 | if (desc->pg_count != 0) { |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 871 | prev = nfs_list_entry(desc->pg_list.prev); |
Boaz Harrosh | 5b36c7d | 2011-05-29 11:45:39 +0300 | [diff] [blame] | 872 | } else { |
Trond Myklebust | d8007d4 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 873 | if (desc->pg_ops->pg_init) |
| 874 | desc->pg_ops->pg_init(desc, req); |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 875 | desc->pg_base = req->wb_pgbase; |
Boaz Harrosh | 5b36c7d | 2011-05-29 11:45:39 +0300 | [diff] [blame] | 876 | } |
Weston Andros Adamson | ab75e41 | 2014-05-15 11:56:44 -0400 | [diff] [blame] | 877 | if (!nfs_can_coalesce_requests(prev, req, desc)) |
| 878 | return 0; |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 879 | nfs_list_remove_request(req); |
| 880 | nfs_list_add_request(req, &desc->pg_list); |
Boaz Harrosh | 5b36c7d | 2011-05-29 11:45:39 +0300 | [diff] [blame] | 881 | desc->pg_count += req->wb_bytes; |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 882 | return 1; |
| 883 | } |
| 884 | |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 885 | /* |
| 886 | * Helper for nfs_pageio_add_request and nfs_pageio_complete |
| 887 | */ |
| 888 | static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc) |
| 889 | { |
| 890 | if (!list_empty(&desc->pg_list)) { |
Trond Myklebust | 1751c36 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 891 | int error = desc->pg_ops->pg_doio(desc); |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 892 | if (error < 0) |
| 893 | desc->pg_error = error; |
| 894 | else |
| 895 | desc->pg_bytes_written += desc->pg_count; |
| 896 | } |
| 897 | if (list_empty(&desc->pg_list)) { |
| 898 | desc->pg_count = 0; |
| 899 | desc->pg_base = 0; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | /** |
| 904 | * nfs_pageio_add_request - Attempt to coalesce a request into a page list. |
| 905 | * @desc: destination io descriptor |
| 906 | * @req: request |
| 907 | * |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 908 | * This may split a request into subrequests which are all part of the |
| 909 | * same page group. |
| 910 | * |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 911 | * Returns true if the request 'req' was successfully coalesced into the |
| 912 | * existing list of pages 'desc'. |
| 913 | */ |
Trond Myklebust | d9156f9 | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 914 | static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc, |
Trond Myklebust | 8b09bee | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 915 | struct nfs_page *req) |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 916 | { |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 917 | struct nfs_page *subreq; |
| 918 | unsigned int bytes_left = 0; |
| 919 | unsigned int offset, pgbase; |
| 920 | |
| 921 | nfs_page_group_lock(req); |
| 922 | |
| 923 | subreq = req; |
| 924 | bytes_left = subreq->wb_bytes; |
| 925 | offset = subreq->wb_offset; |
| 926 | pgbase = subreq->wb_pgbase; |
| 927 | |
| 928 | do { |
| 929 | if (!nfs_pageio_do_add_request(desc, subreq)) { |
| 930 | /* make sure pg_test call(s) did nothing */ |
| 931 | WARN_ON_ONCE(subreq->wb_bytes != bytes_left); |
| 932 | WARN_ON_ONCE(subreq->wb_offset != offset); |
| 933 | WARN_ON_ONCE(subreq->wb_pgbase != pgbase); |
| 934 | |
| 935 | nfs_page_group_unlock(req); |
| 936 | desc->pg_moreio = 1; |
| 937 | nfs_pageio_doio(desc); |
| 938 | if (desc->pg_error < 0) |
| 939 | return 0; |
| 940 | desc->pg_moreio = 0; |
| 941 | if (desc->pg_recoalesce) |
| 942 | return 0; |
| 943 | /* retry add_request for this subreq */ |
| 944 | nfs_page_group_lock(req); |
| 945 | continue; |
| 946 | } |
| 947 | |
| 948 | /* check for buggy pg_test call(s) */ |
| 949 | WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE); |
| 950 | WARN_ON_ONCE(subreq->wb_bytes > bytes_left); |
| 951 | WARN_ON_ONCE(subreq->wb_bytes == 0); |
| 952 | |
| 953 | bytes_left -= subreq->wb_bytes; |
| 954 | offset += subreq->wb_bytes; |
| 955 | pgbase += subreq->wb_bytes; |
| 956 | |
| 957 | if (bytes_left) { |
| 958 | subreq = nfs_create_request(req->wb_context, |
| 959 | req->wb_page, |
| 960 | subreq, pgbase, bytes_left); |
Trond Myklebust | c110955 | 2014-05-29 11:38:15 -0400 | [diff] [blame] | 961 | if (IS_ERR(subreq)) |
| 962 | goto err_ptr; |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 963 | nfs_lock_request(subreq); |
| 964 | subreq->wb_offset = offset; |
| 965 | subreq->wb_index = req->wb_index; |
| 966 | } |
| 967 | } while (bytes_left > 0); |
| 968 | |
| 969 | nfs_page_group_unlock(req); |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 970 | return 1; |
Trond Myklebust | c110955 | 2014-05-29 11:38:15 -0400 | [diff] [blame] | 971 | err_ptr: |
| 972 | desc->pg_error = PTR_ERR(subreq); |
| 973 | nfs_page_group_unlock(req); |
| 974 | return 0; |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 975 | } |
| 976 | |
Trond Myklebust | d9156f9 | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 977 | static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc) |
| 978 | { |
| 979 | LIST_HEAD(head); |
| 980 | |
| 981 | do { |
| 982 | list_splice_init(&desc->pg_list, &head); |
| 983 | desc->pg_bytes_written -= desc->pg_count; |
| 984 | desc->pg_count = 0; |
| 985 | desc->pg_base = 0; |
| 986 | desc->pg_recoalesce = 0; |
| 987 | |
| 988 | while (!list_empty(&head)) { |
| 989 | struct nfs_page *req; |
| 990 | |
| 991 | req = list_first_entry(&head, struct nfs_page, wb_list); |
| 992 | nfs_list_remove_request(req); |
| 993 | if (__nfs_pageio_add_request(desc, req)) |
| 994 | continue; |
| 995 | if (desc->pg_error < 0) |
| 996 | return 0; |
| 997 | break; |
| 998 | } |
| 999 | } while (desc->pg_recoalesce); |
| 1000 | return 1; |
| 1001 | } |
| 1002 | |
| 1003 | int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc, |
| 1004 | struct nfs_page *req) |
| 1005 | { |
| 1006 | int ret; |
| 1007 | |
| 1008 | do { |
| 1009 | ret = __nfs_pageio_add_request(desc, req); |
| 1010 | if (ret) |
| 1011 | break; |
| 1012 | if (desc->pg_error < 0) |
| 1013 | break; |
| 1014 | ret = nfs_do_recoalesce(desc); |
| 1015 | } while (ret); |
| 1016 | return ret; |
| 1017 | } |
Bryan Schumaker | 89d77c8 | 2012-07-30 16:05:25 -0400 | [diff] [blame] | 1018 | EXPORT_SYMBOL_GPL(nfs_pageio_add_request); |
Trond Myklebust | d9156f9 | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 1019 | |
Trond Myklebust | d8a5ad7 | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 1020 | /** |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 1021 | * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor |
| 1022 | * @desc: pointer to io descriptor |
| 1023 | */ |
| 1024 | void nfs_pageio_complete(struct nfs_pageio_descriptor *desc) |
| 1025 | { |
Trond Myklebust | d9156f9 | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 1026 | for (;;) { |
| 1027 | nfs_pageio_doio(desc); |
| 1028 | if (!desc->pg_recoalesce) |
| 1029 | break; |
| 1030 | if (!nfs_do_recoalesce(desc)) |
| 1031 | break; |
| 1032 | } |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 1033 | } |
Bryan Schumaker | 89d77c8 | 2012-07-30 16:05:25 -0400 | [diff] [blame] | 1034 | EXPORT_SYMBOL_GPL(nfs_pageio_complete); |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 1035 | |
Trond Myklebust | 7fe7f84 | 2007-05-20 10:18:27 -0400 | [diff] [blame] | 1036 | /** |
| 1037 | * nfs_pageio_cond_complete - Conditional I/O completion |
| 1038 | * @desc: pointer to io descriptor |
| 1039 | * @index: page index |
| 1040 | * |
| 1041 | * It is important to ensure that processes don't try to take locks |
| 1042 | * on non-contiguous ranges of pages as that might deadlock. This |
| 1043 | * function should be called before attempting to wait on a locked |
| 1044 | * nfs_page. It will complete the I/O if the page index 'index' |
| 1045 | * is not contiguous with the existing list of pages in 'desc'. |
| 1046 | */ |
| 1047 | void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index) |
| 1048 | { |
| 1049 | if (!list_empty(&desc->pg_list)) { |
| 1050 | struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev); |
| 1051 | if (index != prev->wb_index + 1) |
Trond Myklebust | d9156f9 | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 1052 | nfs_pageio_complete(desc); |
Trond Myklebust | 7fe7f84 | 2007-05-20 10:18:27 -0400 | [diff] [blame] | 1053 | } |
| 1054 | } |
| 1055 | |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 1056 | int __init nfs_init_nfspagecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | { |
| 1058 | nfs_page_cachep = kmem_cache_create("nfs_page", |
| 1059 | sizeof(struct nfs_page), |
| 1060 | 0, SLAB_HWCACHE_ALIGN, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1061 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | if (nfs_page_cachep == NULL) |
| 1063 | return -ENOMEM; |
| 1064 | |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
David Brownell | 266bee8 | 2006-06-27 12:59:15 -0700 | [diff] [blame] | 1068 | void nfs_destroy_nfspagecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | { |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 1070 | kmem_cache_destroy(nfs_page_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
Anna Schumaker | ef2c488 | 2014-05-06 09:12:36 -0400 | [diff] [blame] | 1073 | static const struct rpc_call_ops nfs_pgio_common_ops = { |
Anna Schumaker | 6f92fa4 | 2014-05-06 09:12:33 -0400 | [diff] [blame] | 1074 | .rpc_call_prepare = nfs_pgio_prepare, |
| 1075 | .rpc_call_done = nfs_pgio_result, |
| 1076 | .rpc_release = nfs_pgio_release, |
| 1077 | }; |
Anna Schumaker | 41d8d5b | 2014-05-06 09:12:40 -0400 | [diff] [blame] | 1078 | |
| 1079 | const struct nfs_pageio_ops nfs_pgio_rw_ops = { |
| 1080 | .pg_test = nfs_generic_pg_test, |
| 1081 | .pg_doio = nfs_generic_pg_pgios, |
| 1082 | }; |