blob: ceb4424614aae7ecc50155ab8e6340df7c9a796f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/slab.h>
13#include <linux/file.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040014#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sunrpc/clnt.h>
Trond Myklebust1313e602012-01-17 22:04:24 -050016#include <linux/nfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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 Gortmakerafeacc82011-05-26 16:00:52 -040022#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Trond Myklebust8d5658c2007-04-10 09:26:35 -040024#include "internal.h"
Fred Isamanbae724e2011-03-01 01:34:15 +000025#include "pnfs.h"
Trond Myklebust8d5658c2007-04-10 09:26:35 -040026
Anna Schumaker0eecb212014-05-06 09:12:32 -040027#define NFSDBG_FACILITY NFSDBG_PAGECACHE
28
Christoph Lametere18b8902006-12-06 20:33:20 -080029static struct kmem_cache *nfs_page_cachep;
Anna Schumakeref2c4882014-05-06 09:12:36 -040030static const struct rpc_call_ops nfs_pgio_common_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -040032static void nfs_free_request(struct nfs_page *);
33
Anna Schumaker00bfa302014-05-06 09:12:29 -040034static bool nfs_pgarray_set(struct nfs_page_array *p, unsigned int pagecount)
Fred Isaman30dd3742012-04-20 14:47:45 -040035{
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 Isaman4db6e0b2012-04-20 14:47:46 -040047void 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 Isaman584aa812012-04-20 14:47:51 -040056 hdr->dreq = desc->pg_dreq;
Peng Taof6166382012-08-02 15:36:09 +030057 hdr->layout_private = desc->pg_layout_private;
Fred Isaman4db6e0b2012-04-20 14:47:46 -040058 hdr->release = release;
Fred Isaman061ae2e2012-04-20 14:47:48 -040059 hdr->completion_ops = desc->pg_completion_ops;
Fred Isaman584aa812012-04-20 14:47:51 -040060 if (hdr->completion_ops->init_hdr)
61 hdr->completion_ops->init_hdr(hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -040062}
Bryan Schumaker89d77c82012-07-30 16:05:25 -040063EXPORT_SYMBOL_GPL(nfs_pgheader_init);
Fred Isaman4db6e0b2012-04-20 14:47:46 -040064
65void 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 Torvalds1da177e2005-04-16 15:20:36 -070077static inline struct nfs_page *
78nfs_page_alloc(void)
79{
Mel Gorman192e5012012-07-31 16:45:16 -070080 struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_NOIO);
Jesper Juhl72895b12010-12-09 23:17:15 +010081 if (p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 INIT_LIST_HEAD(&p->wb_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return p;
84}
85
86static inline void
87nfs_page_free(struct nfs_page *p)
88{
89 kmem_cache_free(nfs_page_cachep, p);
90}
91
Trond Myklebust577b4232013-04-08 21:38:12 -040092static void
93nfs_iocounter_inc(struct nfs_io_counter *c)
94{
95 atomic_inc(&c->io_count);
96}
97
98static void
99nfs_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);
103 smp_mb__after_clear_bit();
104 wake_up_bit(&c->flags, NFS_IO_INPROGRESS);
105 }
106}
107
108static 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 */
133int
134nfs_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
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400141/*
142 * nfs_page_group_lock - lock the head of the page group
143 * @req - request in group that is to be locked
144 *
145 * this lock must be held if modifying the page group list
146 */
147void
148nfs_page_group_lock(struct nfs_page *req)
149{
150 struct nfs_page *head = req->wb_head;
151 int err = -EAGAIN;
152
153 WARN_ON_ONCE(head != head->wb_head);
154
155 while (err)
156 err = wait_on_bit_lock(&head->wb_flags, PG_HEADLOCK,
157 nfs_wait_bit_killable, TASK_KILLABLE);
158}
159
160/*
161 * nfs_page_group_unlock - unlock the head of the page group
162 * @req - request in group that is to be unlocked
163 */
164void
165nfs_page_group_unlock(struct nfs_page *req)
166{
167 struct nfs_page *head = req->wb_head;
168
169 WARN_ON_ONCE(head != head->wb_head);
170
171 smp_mb__before_clear_bit();
172 clear_bit(PG_HEADLOCK, &head->wb_flags);
173 smp_mb__after_clear_bit();
174 wake_up_bit(&head->wb_flags, PG_HEADLOCK);
175}
176
177/*
178 * nfs_page_group_sync_on_bit_locked
179 *
180 * must be called with page group lock held
181 */
182static bool
183nfs_page_group_sync_on_bit_locked(struct nfs_page *req, unsigned int bit)
184{
185 struct nfs_page *head = req->wb_head;
186 struct nfs_page *tmp;
187
188 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_flags));
189 WARN_ON_ONCE(test_and_set_bit(bit, &req->wb_flags));
190
191 tmp = req->wb_this_page;
192 while (tmp != req) {
193 if (!test_bit(bit, &tmp->wb_flags))
194 return false;
195 tmp = tmp->wb_this_page;
196 }
197
198 /* true! reset all bits */
199 tmp = req;
200 do {
201 clear_bit(bit, &tmp->wb_flags);
202 tmp = tmp->wb_this_page;
203 } while (tmp != req);
204
205 return true;
206}
207
208/*
209 * nfs_page_group_sync_on_bit - set bit on current request, but only
210 * return true if the bit is set for all requests in page group
211 * @req - request in page group
212 * @bit - PG_* bit that is used to sync page group
213 */
214bool nfs_page_group_sync_on_bit(struct nfs_page *req, unsigned int bit)
215{
216 bool ret;
217
218 nfs_page_group_lock(req);
219 ret = nfs_page_group_sync_on_bit_locked(req, bit);
220 nfs_page_group_unlock(req);
221
222 return ret;
223}
224
225/*
226 * nfs_page_group_init - Initialize the page group linkage for @req
227 * @req - a new nfs request
228 * @prev - the previous request in page group, or NULL if @req is the first
229 * or only request in the group (the head).
230 */
231static inline void
232nfs_page_group_init(struct nfs_page *req, struct nfs_page *prev)
233{
234 WARN_ON_ONCE(prev == req);
235
236 if (!prev) {
237 req->wb_head = req;
238 req->wb_this_page = req;
239 } else {
240 WARN_ON_ONCE(prev->wb_this_page != prev->wb_head);
241 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &prev->wb_head->wb_flags));
242 req->wb_head = prev->wb_head;
243 req->wb_this_page = prev->wb_this_page;
244 prev->wb_this_page = req;
245
246 /* grab extra ref if head request has extra ref from
247 * the write/commit path to handle handoff between write
248 * and commit lists */
249 if (test_bit(PG_INODE_REF, &prev->wb_head->wb_flags))
250 kref_get(&req->wb_kref);
251 }
252}
253
254/*
255 * nfs_page_group_destroy - sync the destruction of page groups
256 * @req - request that no longer needs the page group
257 *
258 * releases the page group reference from each member once all
259 * members have called this function.
260 */
261static void
262nfs_page_group_destroy(struct kref *kref)
263{
264 struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
265 struct nfs_page *tmp, *next;
266
267 if (!nfs_page_group_sync_on_bit(req, PG_TEARDOWN))
268 return;
269
270 tmp = req;
271 do {
272 next = tmp->wb_this_page;
273 /* unlink and free */
274 tmp->wb_this_page = tmp;
275 tmp->wb_head = tmp;
276 nfs_free_request(tmp);
277 tmp = next;
278 } while (tmp != req);
279}
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281/**
282 * nfs_create_request - Create an NFS read/write request.
Chuck Leverc02f5572011-10-25 12:17:43 -0400283 * @ctx: open context to use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * @page: page to write
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400285 * @last: last nfs request created for this page group or NULL if head
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * @offset: starting offset within the page for the write
287 * @count: number of bytes to read/write
288 *
289 * The page must be locked by the caller. This makes sure we never
Jason Uhlenkotta19b89c2007-04-26 17:25:51 -0700290 * create two different requests for the same page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * User should ensure it is safe to sleep in this function.
292 */
293struct nfs_page *
Weston Andros Adamson8c8f1ac12014-05-15 11:56:42 -0400294nfs_create_request(struct nfs_open_context *ctx, struct page *page,
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400295 struct nfs_page *last, unsigned int offset,
296 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 struct nfs_page *req;
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400299 struct nfs_lock_context *l_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Trond Myklebustc58c8442013-03-18 19:45:14 -0400301 if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
302 return ERR_PTR(-EBADF);
Trond Myklebust18eb8842010-05-13 12:51:02 -0400303 /* try to allocate the request struct */
304 req = nfs_page_alloc();
305 if (req == NULL)
306 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Jeff Layton015f0212010-10-28 10:10:37 -0400308 /* get lock context early so we can deal with alloc failures */
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400309 l_ctx = nfs_get_lock_context(ctx);
310 if (IS_ERR(l_ctx)) {
Jeff Layton015f0212010-10-28 10:10:37 -0400311 nfs_page_free(req);
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400312 return ERR_CAST(l_ctx);
Jeff Layton015f0212010-10-28 10:10:37 -0400313 }
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400314 req->wb_lock_context = l_ctx;
Trond Myklebust577b4232013-04-08 21:38:12 -0400315 nfs_iocounter_inc(&l_ctx->io_count);
Jeff Layton015f0212010-10-28 10:10:37 -0400316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /* Initialize the request struct. Initially, we assume a
318 * long write-back delay. This will be adjusted in
319 * update_nfs_request below if the region is not locked. */
320 req->wb_page = page;
Mel Gormand56b4dd2012-07-31 16:45:06 -0700321 req->wb_index = page_file_index(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 page_cache_get(page);
323 req->wb_offset = offset;
324 req->wb_pgbase = offset;
325 req->wb_bytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 req->wb_context = get_nfs_open_context(ctx);
Trond Myklebustc03b4022007-06-17 13:26:38 -0400327 kref_init(&req->wb_kref);
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400328 nfs_page_group_init(req, last);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return req;
330}
331
332/**
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400333 * nfs_unlock_request - Unlock request and wake up sleepers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * @req:
335 */
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400336void nfs_unlock_request(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 if (!NFS_WBACK_BUSY(req)) {
339 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
340 BUG();
341 }
342 smp_mb__before_clear_bit();
343 clear_bit(PG_BUSY, &req->wb_flags);
344 smp_mb__after_clear_bit();
Trond Myklebust464a98b2005-06-22 17:16:21 +0000345 wake_up_bit(&req->wb_flags, PG_BUSY);
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400346}
347
348/**
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400349 * nfs_unlock_and_release_request - Unlock request and release the nfs_page
350 * @req:
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400351 */
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400352void nfs_unlock_and_release_request(struct nfs_page *req)
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400353{
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400354 nfs_unlock_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 nfs_release_request(req);
356}
357
Trond Myklebust4d65c522011-03-25 14:15:11 -0400358/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * nfs_clear_request - Free up all resources allocated to the request
360 * @req:
361 *
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500362 * Release page and open context resources associated with a read/write
363 * request after it has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
Trond Myklebust4d65c522011-03-25 14:15:11 -0400365static void nfs_clear_request(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500367 struct page *page = req->wb_page;
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500368 struct nfs_open_context *ctx = req->wb_context;
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400369 struct nfs_lock_context *l_ctx = req->wb_lock_context;
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500370
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500371 if (page != NULL) {
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500372 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 req->wb_page = NULL;
374 }
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400375 if (l_ctx != NULL) {
Trond Myklebust577b4232013-04-08 21:38:12 -0400376 nfs_iocounter_dec(&l_ctx->io_count);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400377 nfs_put_lock_context(l_ctx);
378 req->wb_lock_context = NULL;
379 }
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500380 if (ctx != NULL) {
381 put_nfs_open_context(ctx);
382 req->wb_context = NULL;
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/**
387 * nfs_release_request - Release the count on an NFS read/write request
388 * @req: request to release
389 *
390 * Note: Should never be called with the spinlock held!
391 */
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400392static void nfs_free_request(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400394 WARN_ON_ONCE(req->wb_this_page != req);
395
396 /* extra debug: make sure no sync bits are still set */
397 WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
Weston Andros Adamson67d03382014-05-15 11:56:46 -0400398 WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags));
399 WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags));
Weston Andros Adamson20633f02014-05-15 11:56:47 -0400400 WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags));
401 WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500403 /* Release struct file and open context */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 nfs_clear_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 nfs_page_free(req);
406}
407
Trond Myklebustc03b4022007-06-17 13:26:38 -0400408void nfs_release_request(struct nfs_page *req)
409{
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400410 kref_put(&req->wb_kref, nfs_page_group_destroy);
Trond Myklebustc03b4022007-06-17 13:26:38 -0400411}
412
Trond Myklebust9f557cd2010-02-03 08:27:22 -0500413static int nfs_wait_bit_uninterruptible(void *word)
414{
415 io_schedule();
416 return 0;
417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/**
420 * nfs_wait_on_request - Wait for a request to complete.
421 * @req: request to wait upon.
422 *
Matthew Wilcox150030b2007-12-06 16:24:39 -0500423 * Interruptible by fatal signals only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * The user is responsible for holding a count on the request.
425 */
426int
427nfs_wait_on_request(struct nfs_page *req)
428{
Trond Myklebust9f557cd2010-02-03 08:27:22 -0500429 return wait_on_bit(&req->wb_flags, PG_BUSY,
430 nfs_wait_bit_uninterruptible,
431 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400434/*
435 * nfs_generic_pg_test - determine if requests can be coalesced
436 * @desc: pointer to descriptor
437 * @prev: previous request in desc, or NULL
438 * @req: this request
439 *
440 * Returns zero if @req can be coalesced into @desc, otherwise it returns
441 * the size of the request.
442 */
443size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc,
444 struct nfs_page *prev, struct nfs_page *req)
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300445{
Weston Andros Adamsonab75e412014-05-15 11:56:44 -0400446 if (!prev)
447 return req->wb_bytes;
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300448 /*
449 * FIXME: ideally we should be able to coalesce all requests
450 * that are not block boundary aligned, but currently this
451 * is problematic for the case of bsize < PAGE_CACHE_SIZE,
452 * since nfs_flush_multi and nfs_pagein_multi assume you
453 * can have only one struct nfs_page.
454 */
455 if (desc->pg_bsize < PAGE_SIZE)
456 return 0;
457
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400458 if (desc->pg_count + req->wb_bytes <= desc->pg_bsize)
459 return req->wb_bytes;
460 return 0;
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300461}
Benny Halevy19345cb2011-06-19 18:33:46 -0400462EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300463
Anna Schumaker00bfa302014-05-06 09:12:29 -0400464static inline struct nfs_rw_header *NFS_RW_HEADER(struct nfs_pgio_header *hdr)
465{
466 return container_of(hdr, struct nfs_rw_header, header);
467}
468
469/**
Anna Schumaker4a0de552014-05-06 09:12:30 -0400470 * nfs_rw_header_alloc - Allocate a header for a read or write
471 * @ops: Read or write function vector
472 */
473struct nfs_rw_header *nfs_rw_header_alloc(const struct nfs_rw_ops *ops)
474{
475 struct nfs_rw_header *header = ops->rw_alloc_header();
476
477 if (header) {
478 struct nfs_pgio_header *hdr = &header->header;
479
480 INIT_LIST_HEAD(&hdr->pages);
481 INIT_LIST_HEAD(&hdr->rpc_list);
482 spin_lock_init(&hdr->lock);
483 atomic_set(&hdr->refcnt, 0);
484 hdr->rw_ops = ops;
485 }
486 return header;
487}
488EXPORT_SYMBOL_GPL(nfs_rw_header_alloc);
489
490/*
491 * nfs_rw_header_free - Free a read or write header
492 * @hdr: The header to free
493 */
494void nfs_rw_header_free(struct nfs_pgio_header *hdr)
495{
496 hdr->rw_ops->rw_free_header(NFS_RW_HEADER(hdr));
497}
498EXPORT_SYMBOL_GPL(nfs_rw_header_free);
499
500/**
Anna Schumaker00bfa302014-05-06 09:12:29 -0400501 * nfs_pgio_data_alloc - Allocate pageio data
502 * @hdr: The header making a request
503 * @pagecount: Number of pages to create
504 */
Anna Schumakeref2c4882014-05-06 09:12:36 -0400505static struct nfs_pgio_data *nfs_pgio_data_alloc(struct nfs_pgio_header *hdr,
506 unsigned int pagecount)
Anna Schumaker00bfa302014-05-06 09:12:29 -0400507{
508 struct nfs_pgio_data *data, *prealloc;
509
510 prealloc = &NFS_RW_HEADER(hdr)->rpc_data;
511 if (prealloc->header == NULL)
512 data = prealloc;
513 else
514 data = kzalloc(sizeof(*data), GFP_KERNEL);
515 if (!data)
516 goto out;
517
518 if (nfs_pgarray_set(&data->pages, pagecount)) {
519 data->header = hdr;
520 atomic_inc(&hdr->refcnt);
521 } else {
522 if (data != prealloc)
523 kfree(data);
524 data = NULL;
525 }
526out:
527 return data;
528}
529
530/**
531 * nfs_pgio_data_release - Properly free pageio data
532 * @data: The data to release
533 */
534void nfs_pgio_data_release(struct nfs_pgio_data *data)
535{
536 struct nfs_pgio_header *hdr = data->header;
537 struct nfs_rw_header *pageio_header = NFS_RW_HEADER(hdr);
538
539 put_nfs_open_context(data->args.context);
540 if (data->pages.pagevec != data->pages.page_array)
541 kfree(data->pages.pagevec);
542 if (data == &pageio_header->rpc_data) {
543 data->header = NULL;
544 data = NULL;
545 }
546 if (atomic_dec_and_test(&hdr->refcnt))
547 hdr->completion_ops->completion(hdr);
548 /* Note: we only free the rpc_task after callbacks are done.
549 * See the comment in rpc_free_task() for why
550 */
551 kfree(data);
552}
553EXPORT_SYMBOL_GPL(nfs_pgio_data_release);
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555/**
Anna Schumakerce595152014-05-06 09:12:34 -0400556 * nfs_pgio_rpcsetup - Set up arguments for a pageio call
557 * @data: The pageio data
558 * @count: Number of bytes to read
559 * @offset: Initial offset
560 * @how: How to commit data (writes only)
561 * @cinfo: Commit information for the call (writes only)
562 */
Anna Schumakeref2c4882014-05-06 09:12:36 -0400563static void nfs_pgio_rpcsetup(struct nfs_pgio_data *data,
Anna Schumakerce595152014-05-06 09:12:34 -0400564 unsigned int count, unsigned int offset,
565 int how, struct nfs_commit_info *cinfo)
566{
567 struct nfs_page *req = data->header->req;
568
569 /* Set up the RPC argument and reply structs
570 * NB: take care not to mess about with data->commit et al. */
571
572 data->args.fh = NFS_FH(data->header->inode);
573 data->args.offset = req_offset(req) + offset;
574 /* pnfs_set_layoutcommit needs this */
575 data->mds_offset = data->args.offset;
576 data->args.pgbase = req->wb_pgbase + offset;
577 data->args.pages = data->pages.pagevec;
578 data->args.count = count;
579 data->args.context = get_nfs_open_context(req->wb_context);
580 data->args.lock_context = req->wb_lock_context;
581 data->args.stable = NFS_UNSTABLE;
582 switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
583 case 0:
584 break;
585 case FLUSH_COND_STABLE:
586 if (nfs_reqs_to_commit(cinfo))
587 break;
588 default:
589 data->args.stable = NFS_FILE_SYNC;
590 }
591
592 data->res.fattr = &data->fattr;
593 data->res.count = count;
594 data->res.eof = 0;
595 data->res.verf = &data->verf;
596 nfs_fattr_init(&data->fattr);
597}
598
599/**
Anna Schumakera4cdda52014-05-06 09:12:31 -0400600 * nfs_pgio_prepare - Prepare pageio data to go over the wire
601 * @task: The current task
602 * @calldata: pageio data to prepare
603 */
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400604static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
Anna Schumakera4cdda52014-05-06 09:12:31 -0400605{
606 struct nfs_pgio_data *data = calldata;
607 int err;
608 err = NFS_PROTO(data->header->inode)->pgio_rpc_prepare(task, data);
609 if (err)
610 rpc_exit(task, err);
611}
612
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400613int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_data *data,
614 const struct rpc_call_ops *call_ops, int how, int flags)
615{
616 struct rpc_task *task;
617 struct rpc_message msg = {
618 .rpc_argp = &data->args,
619 .rpc_resp = &data->res,
620 .rpc_cred = data->header->cred,
621 };
622 struct rpc_task_setup task_setup_data = {
623 .rpc_client = clnt,
624 .task = &data->task,
625 .rpc_message = &msg,
626 .callback_ops = call_ops,
627 .callback_data = data,
628 .workqueue = nfsiod_workqueue,
629 .flags = RPC_TASK_ASYNC | flags,
630 };
631 int ret = 0;
632
633 data->header->rw_ops->rw_initiate(data, &msg, &task_setup_data, how);
634
635 dprintk("NFS: %5u initiated pgio call "
636 "(req %s/%llu, %u bytes @ offset %llu)\n",
637 data->task.tk_pid,
638 data->header->inode->i_sb->s_id,
639 (unsigned long long)NFS_FILEID(data->header->inode),
640 data->args.count,
641 (unsigned long long)data->args.offset);
642
643 task = rpc_run_task(&task_setup_data);
644 if (IS_ERR(task)) {
645 ret = PTR_ERR(task);
646 goto out;
647 }
648 if (how & FLUSH_SYNC) {
649 ret = rpc_wait_for_completion_task(task);
650 if (ret == 0)
651 ret = task->tk_status;
652 }
653 rpc_put_task(task);
654out:
655 return ret;
656}
657EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
658
Anna Schumakercf485fc2014-05-06 09:12:39 -0400659static int nfs_do_multiple_pgios(struct list_head *head,
Anna Schumakerc3766272014-05-06 09:12:38 -0400660 const struct rpc_call_ops *call_ops,
661 int how)
662{
663 struct nfs_pgio_data *data;
664 int ret = 0;
665
666 while (!list_empty(head)) {
667 int ret2;
668
669 data = list_first_entry(head, struct nfs_pgio_data, list);
670 list_del_init(&data->list);
671
672 ret2 = nfs_initiate_pgio(NFS_CLIENT(data->header->inode),
673 data, call_ops, how, 0);
674 if (ret == 0)
675 ret = ret2;
676 }
677 return ret;
678}
679
Anna Schumakera4cdda52014-05-06 09:12:31 -0400680/**
Anna Schumaker844c9e62014-05-06 09:12:35 -0400681 * nfs_pgio_error - Clean up from a pageio error
682 * @desc: IO descriptor
683 * @hdr: pageio header
684 */
Anna Schumakeref2c4882014-05-06 09:12:36 -0400685static int nfs_pgio_error(struct nfs_pageio_descriptor *desc,
Anna Schumaker844c9e62014-05-06 09:12:35 -0400686 struct nfs_pgio_header *hdr)
687{
688 struct nfs_pgio_data *data;
689
690 set_bit(NFS_IOHDR_REDO, &hdr->flags);
691 while (!list_empty(&hdr->rpc_list)) {
692 data = list_first_entry(&hdr->rpc_list, struct nfs_pgio_data, list);
693 list_del(&data->list);
694 nfs_pgio_data_release(data);
695 }
696 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
697 return -ENOMEM;
698}
699
700/**
Anna Schumakera4cdda52014-05-06 09:12:31 -0400701 * nfs_pgio_release - Release pageio data
702 * @calldata: The pageio data to release
703 */
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400704static void nfs_pgio_release(void *calldata)
Anna Schumakera4cdda52014-05-06 09:12:31 -0400705{
706 struct nfs_pgio_data *data = calldata;
707 if (data->header->rw_ops->rw_release)
708 data->header->rw_ops->rw_release(data);
709 nfs_pgio_data_release(data);
710}
711
712/**
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400713 * nfs_pageio_init - initialise a page io descriptor
714 * @desc: pointer to descriptor
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400715 * @inode: pointer to inode
716 * @doio: pointer to io function
717 * @bsize: io block size
718 * @io_flags: extra parameters for the io function
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400719 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400720void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
721 struct inode *inode,
Trond Myklebust1751c362011-06-10 13:30:23 -0400722 const struct nfs_pageio_ops *pg_ops,
Fred Isaman061ae2e2012-04-20 14:47:48 -0400723 const struct nfs_pgio_completion_ops *compl_ops,
Anna Schumaker4a0de552014-05-06 09:12:30 -0400724 const struct nfs_rw_ops *rw_ops,
Trond Myklebust84dde762007-05-04 14:44:06 -0400725 size_t bsize,
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400726 int io_flags)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400727{
728 INIT_LIST_HEAD(&desc->pg_list);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400729 desc->pg_bytes_written = 0;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400730 desc->pg_count = 0;
731 desc->pg_bsize = bsize;
732 desc->pg_base = 0;
Trond Myklebustb31268a2011-03-21 17:02:00 -0400733 desc->pg_moreio = 0;
Trond Myklebustd9156f92011-07-12 13:42:02 -0400734 desc->pg_recoalesce = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400735 desc->pg_inode = inode;
Trond Myklebust1751c362011-06-10 13:30:23 -0400736 desc->pg_ops = pg_ops;
Fred Isaman061ae2e2012-04-20 14:47:48 -0400737 desc->pg_completion_ops = compl_ops;
Anna Schumaker4a0de552014-05-06 09:12:30 -0400738 desc->pg_rw_ops = rw_ops;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400739 desc->pg_ioflags = io_flags;
740 desc->pg_error = 0;
Fred Isaman94ad1c82011-03-01 01:34:14 +0000741 desc->pg_lseg = NULL;
Fred Isaman584aa812012-04-20 14:47:51 -0400742 desc->pg_dreq = NULL;
Peng Taof6166382012-08-02 15:36:09 +0300743 desc->pg_layout_private = NULL;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400744}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400745EXPORT_SYMBOL_GPL(nfs_pageio_init);
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400746
Anna Schumaker0eecb212014-05-06 09:12:32 -0400747/**
748 * nfs_pgio_result - Basic pageio error handling
749 * @task: The task that ran
750 * @calldata: Pageio data to check
751 */
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400752static void nfs_pgio_result(struct rpc_task *task, void *calldata)
Anna Schumaker0eecb212014-05-06 09:12:32 -0400753{
754 struct nfs_pgio_data *data = calldata;
755 struct inode *inode = data->header->inode;
756
757 dprintk("NFS: %s: %5u, (status %d)\n", __func__,
758 task->tk_pid, task->tk_status);
759
760 if (data->header->rw_ops->rw_done(task, data, inode) != 0)
761 return;
762 if (task->tk_status < 0)
763 nfs_set_pgio_error(data->header, task->tk_status, data->args.offset);
764 else
765 data->header->rw_ops->rw_result(task, data);
766}
767
Anna Schumakeref2c4882014-05-06 09:12:36 -0400768/*
769 * Generate multiple small requests to read or write a single
770 * contiguous dirty on one page.
771 */
772static int nfs_pgio_multi(struct nfs_pageio_descriptor *desc,
773 struct nfs_pgio_header *hdr)
774{
775 struct nfs_page *req = hdr->req;
776 struct page *page = req->wb_page;
777 struct nfs_pgio_data *data;
778 size_t wsize = desc->pg_bsize, nbytes;
779 unsigned int offset;
780 int requests = 0;
781 struct nfs_commit_info cinfo;
782
783 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
784
785 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
786 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) ||
787 desc->pg_count > wsize))
788 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
789
790 offset = 0;
791 nbytes = desc->pg_count;
792 do {
793 size_t len = min(nbytes, wsize);
794
795 data = nfs_pgio_data_alloc(hdr, 1);
796 if (!data)
797 return nfs_pgio_error(desc, hdr);
798 data->pages.pagevec[0] = page;
799 nfs_pgio_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo);
800 list_add(&data->list, &hdr->rpc_list);
801 requests++;
802 nbytes -= len;
803 offset += len;
804 } while (nbytes != 0);
805
806 nfs_list_remove_request(req);
807 nfs_list_add_request(req, &hdr->pages);
808 desc->pg_rpc_callops = &nfs_pgio_common_ops;
809 return 0;
810}
811
812/*
813 * Create an RPC task for the given read or write request and kick it.
814 * The page must have been locked by the caller.
815 *
816 * It may happen that the page we're passed is not marked dirty.
817 * This is the case if nfs_updatepage detects a conflicting request
818 * that has been written but not committed.
819 */
820static int nfs_pgio_one(struct nfs_pageio_descriptor *desc,
821 struct nfs_pgio_header *hdr)
822{
823 struct nfs_page *req;
824 struct page **pages;
825 struct nfs_pgio_data *data;
826 struct list_head *head = &desc->pg_list;
827 struct nfs_commit_info cinfo;
828
829 data = nfs_pgio_data_alloc(hdr, nfs_page_array_len(desc->pg_base,
830 desc->pg_count));
831 if (!data)
832 return nfs_pgio_error(desc, hdr);
833
834 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
835 pages = data->pages.pagevec;
836 while (!list_empty(head)) {
837 req = nfs_list_entry(head->next);
838 nfs_list_remove_request(req);
839 nfs_list_add_request(req, &hdr->pages);
840 *pages++ = req->wb_page;
841 }
842
843 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
844 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
845 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
846
847 /* Set up the argument struct */
848 nfs_pgio_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
849 list_add(&data->list, &hdr->rpc_list);
850 desc->pg_rpc_callops = &nfs_pgio_common_ops;
851 return 0;
852}
853
Anna Schumaker41d8d5b2014-05-06 09:12:40 -0400854static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
Anna Schumakercf485fc2014-05-06 09:12:39 -0400855{
856 struct nfs_rw_header *rw_hdr;
857 struct nfs_pgio_header *hdr;
858 int ret;
859
860 rw_hdr = nfs_rw_header_alloc(desc->pg_rw_ops);
861 if (!rw_hdr) {
862 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
863 return -ENOMEM;
864 }
865 hdr = &rw_hdr->header;
866 nfs_pgheader_init(desc, hdr, nfs_rw_header_free);
867 atomic_inc(&hdr->refcnt);
868 ret = nfs_generic_pgio(desc, hdr);
869 if (ret == 0)
870 ret = nfs_do_multiple_pgios(&hdr->rpc_list,
871 desc->pg_rpc_callops,
872 desc->pg_ioflags);
873 if (atomic_dec_and_test(&hdr->refcnt))
874 hdr->completion_ops->completion(hdr);
875 return ret;
876}
877
Anna Schumakeref2c4882014-05-06 09:12:36 -0400878int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
879 struct nfs_pgio_header *hdr)
880{
881 if (desc->pg_bsize < PAGE_CACHE_SIZE)
882 return nfs_pgio_multi(desc, hdr);
883 return nfs_pgio_one(desc, hdr);
884}
885EXPORT_SYMBOL_GPL(nfs_generic_pgio);
886
Trond Myklebust4109bb72013-09-06 11:09:38 -0400887static bool nfs_match_open_context(const struct nfs_open_context *ctx1,
888 const struct nfs_open_context *ctx2)
889{
890 return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state;
891}
892
893static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
894 const struct nfs_lock_context *l2)
895{
896 return l1->lockowner.l_owner == l2->lockowner.l_owner
897 && l1->lockowner.l_pid == l2->lockowner.l_pid;
898}
899
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400900/**
901 * nfs_can_coalesce_requests - test two requests for compatibility
902 * @prev: pointer to nfs_page
903 * @req: pointer to nfs_page
904 *
905 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
906 * page data area they describe is contiguous, and that their RPC
907 * credentials, NFSv4 open state, and lockowners are the same.
908 *
909 * Return 'true' if this is the case, else return 'false'.
910 */
Benny Halevy18ad0a92011-05-25 21:03:56 +0300911static bool nfs_can_coalesce_requests(struct nfs_page *prev,
912 struct nfs_page *req,
913 struct nfs_pageio_descriptor *pgio)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400914{
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400915 size_t size;
916
Weston Andros Adamsonab75e412014-05-15 11:56:44 -0400917 if (prev) {
918 if (!nfs_match_open_context(req->wb_context, prev->wb_context))
919 return false;
920 if (req->wb_context->dentry->d_inode->i_flock != NULL &&
921 !nfs_match_lock_context(req->wb_lock_context,
922 prev->wb_lock_context))
923 return false;
924 if (req->wb_pgbase != 0)
925 return false;
926 if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
927 return false;
928 if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
929 return false;
930 }
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400931 size = pgio->pg_ops->pg_test(pgio, prev, req);
932 WARN_ON_ONCE(size && size != req->wb_bytes);
933 return size > 0;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400934}
935
936/**
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400937 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400938 * @desc: destination io descriptor
939 * @req: request
940 *
941 * Returns true if the request 'req' was successfully coalesced into the
942 * existing list of pages 'desc'.
943 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400944static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
945 struct nfs_page *req)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400946{
Weston Andros Adamsonab75e412014-05-15 11:56:44 -0400947 struct nfs_page *prev = NULL;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400948 if (desc->pg_count != 0) {
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400949 prev = nfs_list_entry(desc->pg_list.prev);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300950 } else {
Trond Myklebustd8007d42011-06-10 13:30:23 -0400951 if (desc->pg_ops->pg_init)
952 desc->pg_ops->pg_init(desc, req);
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400953 desc->pg_base = req->wb_pgbase;
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300954 }
Weston Andros Adamsonab75e412014-05-15 11:56:44 -0400955 if (!nfs_can_coalesce_requests(prev, req, desc))
956 return 0;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400957 nfs_list_remove_request(req);
958 nfs_list_add_request(req, &desc->pg_list);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300959 desc->pg_count += req->wb_bytes;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400960 return 1;
961}
962
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400963/*
964 * Helper for nfs_pageio_add_request and nfs_pageio_complete
965 */
966static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
967{
968 if (!list_empty(&desc->pg_list)) {
Trond Myklebust1751c362011-06-10 13:30:23 -0400969 int error = desc->pg_ops->pg_doio(desc);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400970 if (error < 0)
971 desc->pg_error = error;
972 else
973 desc->pg_bytes_written += desc->pg_count;
974 }
975 if (list_empty(&desc->pg_list)) {
976 desc->pg_count = 0;
977 desc->pg_base = 0;
978 }
979}
980
981/**
982 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
983 * @desc: destination io descriptor
984 * @req: request
985 *
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400986 * This may split a request into subrequests which are all part of the
987 * same page group.
988 *
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400989 * Returns true if the request 'req' was successfully coalesced into the
990 * existing list of pages 'desc'.
991 */
Trond Myklebustd9156f92011-07-12 13:42:02 -0400992static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400993 struct nfs_page *req)
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400994{
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400995 struct nfs_page *subreq;
996 unsigned int bytes_left = 0;
997 unsigned int offset, pgbase;
998
999 nfs_page_group_lock(req);
1000
1001 subreq = req;
1002 bytes_left = subreq->wb_bytes;
1003 offset = subreq->wb_offset;
1004 pgbase = subreq->wb_pgbase;
1005
1006 do {
1007 if (!nfs_pageio_do_add_request(desc, subreq)) {
1008 /* make sure pg_test call(s) did nothing */
1009 WARN_ON_ONCE(subreq->wb_bytes != bytes_left);
1010 WARN_ON_ONCE(subreq->wb_offset != offset);
1011 WARN_ON_ONCE(subreq->wb_pgbase != pgbase);
1012
1013 nfs_page_group_unlock(req);
1014 desc->pg_moreio = 1;
1015 nfs_pageio_doio(desc);
1016 if (desc->pg_error < 0)
1017 return 0;
1018 desc->pg_moreio = 0;
1019 if (desc->pg_recoalesce)
1020 return 0;
1021 /* retry add_request for this subreq */
1022 nfs_page_group_lock(req);
1023 continue;
1024 }
1025
1026 /* check for buggy pg_test call(s) */
1027 WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE);
1028 WARN_ON_ONCE(subreq->wb_bytes > bytes_left);
1029 WARN_ON_ONCE(subreq->wb_bytes == 0);
1030
1031 bytes_left -= subreq->wb_bytes;
1032 offset += subreq->wb_bytes;
1033 pgbase += subreq->wb_bytes;
1034
1035 if (bytes_left) {
1036 subreq = nfs_create_request(req->wb_context,
1037 req->wb_page,
1038 subreq, pgbase, bytes_left);
1039 nfs_lock_request(subreq);
1040 subreq->wb_offset = offset;
1041 subreq->wb_index = req->wb_index;
1042 }
1043 } while (bytes_left > 0);
1044
1045 nfs_page_group_unlock(req);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -04001046 return 1;
1047}
1048
Trond Myklebustd9156f92011-07-12 13:42:02 -04001049static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
1050{
1051 LIST_HEAD(head);
1052
1053 do {
1054 list_splice_init(&desc->pg_list, &head);
1055 desc->pg_bytes_written -= desc->pg_count;
1056 desc->pg_count = 0;
1057 desc->pg_base = 0;
1058 desc->pg_recoalesce = 0;
1059
1060 while (!list_empty(&head)) {
1061 struct nfs_page *req;
1062
1063 req = list_first_entry(&head, struct nfs_page, wb_list);
1064 nfs_list_remove_request(req);
1065 if (__nfs_pageio_add_request(desc, req))
1066 continue;
1067 if (desc->pg_error < 0)
1068 return 0;
1069 break;
1070 }
1071 } while (desc->pg_recoalesce);
1072 return 1;
1073}
1074
1075int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
1076 struct nfs_page *req)
1077{
1078 int ret;
1079
1080 do {
1081 ret = __nfs_pageio_add_request(desc, req);
1082 if (ret)
1083 break;
1084 if (desc->pg_error < 0)
1085 break;
1086 ret = nfs_do_recoalesce(desc);
1087 } while (ret);
1088 return ret;
1089}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001090EXPORT_SYMBOL_GPL(nfs_pageio_add_request);
Trond Myklebustd9156f92011-07-12 13:42:02 -04001091
Trond Myklebustd8a5ad72007-04-02 18:48:28 -04001092/**
Trond Myklebustbcb71bb2007-04-02 18:48:28 -04001093 * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
1094 * @desc: pointer to io descriptor
1095 */
1096void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
1097{
Trond Myklebustd9156f92011-07-12 13:42:02 -04001098 for (;;) {
1099 nfs_pageio_doio(desc);
1100 if (!desc->pg_recoalesce)
1101 break;
1102 if (!nfs_do_recoalesce(desc))
1103 break;
1104 }
Trond Myklebustbcb71bb2007-04-02 18:48:28 -04001105}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001106EXPORT_SYMBOL_GPL(nfs_pageio_complete);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -04001107
Trond Myklebust7fe7f842007-05-20 10:18:27 -04001108/**
1109 * nfs_pageio_cond_complete - Conditional I/O completion
1110 * @desc: pointer to io descriptor
1111 * @index: page index
1112 *
1113 * It is important to ensure that processes don't try to take locks
1114 * on non-contiguous ranges of pages as that might deadlock. This
1115 * function should be called before attempting to wait on a locked
1116 * nfs_page. It will complete the I/O if the page index 'index'
1117 * is not contiguous with the existing list of pages in 'desc'.
1118 */
1119void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
1120{
1121 if (!list_empty(&desc->pg_list)) {
1122 struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
1123 if (index != prev->wb_index + 1)
Trond Myklebustd9156f92011-07-12 13:42:02 -04001124 nfs_pageio_complete(desc);
Trond Myklebust7fe7f842007-05-20 10:18:27 -04001125 }
1126}
1127
David Howellsf7b422b2006-06-09 09:34:33 -04001128int __init nfs_init_nfspagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
1130 nfs_page_cachep = kmem_cache_create("nfs_page",
1131 sizeof(struct nfs_page),
1132 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001133 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (nfs_page_cachep == NULL)
1135 return -ENOMEM;
1136
1137 return 0;
1138}
1139
David Brownell266bee82006-06-27 12:59:15 -07001140void nfs_destroy_nfspagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001142 kmem_cache_destroy(nfs_page_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
Anna Schumakeref2c4882014-05-06 09:12:36 -04001145static const struct rpc_call_ops nfs_pgio_common_ops = {
Anna Schumaker6f92fa42014-05-06 09:12:33 -04001146 .rpc_call_prepare = nfs_pgio_prepare,
1147 .rpc_call_done = nfs_pgio_result,
1148 .rpc_release = nfs_pgio_release,
1149};
Anna Schumaker41d8d5b2014-05-06 09:12:40 -04001150
1151const struct nfs_pageio_ops nfs_pgio_rw_ops = {
1152 .pg_test = nfs_generic_pg_test,
1153 .pg_doio = nfs_generic_pg_pgios,
1154};