blob: 9c6c5535939495f545016624db53f4ca3bf17c43 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static 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);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100103 smp_mb__after_atomic();
Trond Myklebust577b4232013-04-08 21:38:12 -0400104 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
Trond Myklebustf8680892014-05-29 11:45:57 -0400141static int nfs_wait_bit_uninterruptible(void *word)
142{
143 io_schedule();
144 return 0;
145}
146
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400147/*
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 */
153void
154nfs_page_group_lock(struct nfs_page *req)
155{
156 struct nfs_page *head = req->wb_head;
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400157
158 WARN_ON_ONCE(head != head->wb_head);
159
Trond Myklebustf8680892014-05-29 11:45:57 -0400160 wait_on_bit_lock(&head->wb_flags, PG_HEADLOCK,
161 nfs_wait_bit_uninterruptible,
162 TASK_UNINTERRUPTIBLE);
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400163}
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 */
169void
170nfs_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 Torvaldsd1e1cda2014-06-10 15:02:42 -0700176 smp_mb__before_atomic();
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400177 clear_bit(PG_HEADLOCK, &head->wb_flags);
Linus Torvaldsd1e1cda2014-06-10 15:02:42 -0700178 smp_mb__after_atomic();
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400179 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 */
187static bool
188nfs_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 */
219bool 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 */
236static inline void
237nfs_page_group_init(struct nfs_page *req, struct nfs_page *prev)
238{
239 WARN_ON_ONCE(prev == req);
240
241 if (!prev) {
242 req->wb_head = req;
243 req->wb_this_page = req;
244 } else {
245 WARN_ON_ONCE(prev->wb_this_page != prev->wb_head);
246 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &prev->wb_head->wb_flags));
247 req->wb_head = prev->wb_head;
248 req->wb_this_page = prev->wb_this_page;
249 prev->wb_this_page = req;
250
251 /* grab extra ref if head request has extra ref from
252 * the write/commit path to handle handoff between write
253 * and commit lists */
254 if (test_bit(PG_INODE_REF, &prev->wb_head->wb_flags))
255 kref_get(&req->wb_kref);
256 }
257}
258
259/*
260 * nfs_page_group_destroy - sync the destruction of page groups
261 * @req - request that no longer needs the page group
262 *
263 * releases the page group reference from each member once all
264 * members have called this function.
265 */
266static void
267nfs_page_group_destroy(struct kref *kref)
268{
269 struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
270 struct nfs_page *tmp, *next;
271
272 if (!nfs_page_group_sync_on_bit(req, PG_TEARDOWN))
273 return;
274
275 tmp = req;
276 do {
277 next = tmp->wb_this_page;
278 /* unlink and free */
279 tmp->wb_this_page = tmp;
280 tmp->wb_head = tmp;
281 nfs_free_request(tmp);
282 tmp = next;
283 } while (tmp != req);
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/**
287 * nfs_create_request - Create an NFS read/write request.
Chuck Leverc02f5572011-10-25 12:17:43 -0400288 * @ctx: open context to use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 * @page: page to write
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400290 * @last: last nfs request created for this page group or NULL if head
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * @offset: starting offset within the page for the write
292 * @count: number of bytes to read/write
293 *
294 * The page must be locked by the caller. This makes sure we never
Jason Uhlenkotta19b89c2007-04-26 17:25:51 -0700295 * create two different requests for the same page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * User should ensure it is safe to sleep in this function.
297 */
298struct nfs_page *
Weston Andros Adamson8c8f1ac12014-05-15 11:56:42 -0400299nfs_create_request(struct nfs_open_context *ctx, struct page *page,
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400300 struct nfs_page *last, unsigned int offset,
301 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 struct nfs_page *req;
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400304 struct nfs_lock_context *l_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Trond Myklebustc58c8442013-03-18 19:45:14 -0400306 if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
307 return ERR_PTR(-EBADF);
Trond Myklebust18eb8842010-05-13 12:51:02 -0400308 /* try to allocate the request struct */
309 req = nfs_page_alloc();
310 if (req == NULL)
311 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Jeff Layton015f0212010-10-28 10:10:37 -0400313 /* get lock context early so we can deal with alloc failures */
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400314 l_ctx = nfs_get_lock_context(ctx);
315 if (IS_ERR(l_ctx)) {
Jeff Layton015f0212010-10-28 10:10:37 -0400316 nfs_page_free(req);
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400317 return ERR_CAST(l_ctx);
Jeff Layton015f0212010-10-28 10:10:37 -0400318 }
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400319 req->wb_lock_context = l_ctx;
Trond Myklebust577b4232013-04-08 21:38:12 -0400320 nfs_iocounter_inc(&l_ctx->io_count);
Jeff Layton015f0212010-10-28 10:10:37 -0400321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 /* Initialize the request struct. Initially, we assume a
323 * long write-back delay. This will be adjusted in
324 * update_nfs_request below if the region is not locked. */
325 req->wb_page = page;
Mel Gormand56b4dd2012-07-31 16:45:06 -0700326 req->wb_index = page_file_index(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 page_cache_get(page);
328 req->wb_offset = offset;
329 req->wb_pgbase = offset;
330 req->wb_bytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 req->wb_context = get_nfs_open_context(ctx);
Trond Myklebustc03b4022007-06-17 13:26:38 -0400332 kref_init(&req->wb_kref);
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400333 nfs_page_group_init(req, last);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return req;
335}
336
337/**
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400338 * nfs_unlock_request - Unlock request and wake up sleepers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * @req:
340 */
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400341void nfs_unlock_request(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 if (!NFS_WBACK_BUSY(req)) {
344 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
345 BUG();
346 }
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100347 smp_mb__before_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 clear_bit(PG_BUSY, &req->wb_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100349 smp_mb__after_atomic();
Trond Myklebust464a98b2005-06-22 17:16:21 +0000350 wake_up_bit(&req->wb_flags, PG_BUSY);
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400351}
352
353/**
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400354 * nfs_unlock_and_release_request - Unlock request and release the nfs_page
355 * @req:
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400356 */
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400357void nfs_unlock_and_release_request(struct nfs_page *req)
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400358{
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400359 nfs_unlock_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 nfs_release_request(req);
361}
362
Trond Myklebust4d65c522011-03-25 14:15:11 -0400363/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * nfs_clear_request - Free up all resources allocated to the request
365 * @req:
366 *
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500367 * Release page and open context resources associated with a read/write
368 * request after it has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 */
Trond Myklebust4d65c522011-03-25 14:15:11 -0400370static void nfs_clear_request(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500372 struct page *page = req->wb_page;
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500373 struct nfs_open_context *ctx = req->wb_context;
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400374 struct nfs_lock_context *l_ctx = req->wb_lock_context;
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500375
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500376 if (page != NULL) {
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500377 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 req->wb_page = NULL;
379 }
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400380 if (l_ctx != NULL) {
Trond Myklebust577b4232013-04-08 21:38:12 -0400381 nfs_iocounter_dec(&l_ctx->io_count);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400382 nfs_put_lock_context(l_ctx);
383 req->wb_lock_context = NULL;
384 }
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500385 if (ctx != NULL) {
386 put_nfs_open_context(ctx);
387 req->wb_context = NULL;
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/**
392 * nfs_release_request - Release the count on an NFS read/write request
393 * @req: request to release
394 *
395 * Note: Should never be called with the spinlock held!
396 */
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400397static void nfs_free_request(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400399 WARN_ON_ONCE(req->wb_this_page != req);
400
401 /* extra debug: make sure no sync bits are still set */
402 WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
Weston Andros Adamson67d03382014-05-15 11:56:46 -0400403 WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags));
404 WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags));
Weston Andros Adamson20633f02014-05-15 11:56:47 -0400405 WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags));
406 WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500408 /* Release struct file and open context */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 nfs_clear_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 nfs_page_free(req);
411}
412
Trond Myklebustc03b4022007-06-17 13:26:38 -0400413void nfs_release_request(struct nfs_page *req)
414{
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400415 kref_put(&req->wb_kref, nfs_page_group_destroy);
Trond Myklebust9f557cd2010-02-03 08:27:22 -0500416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/**
419 * nfs_wait_on_request - Wait for a request to complete.
420 * @req: request to wait upon.
421 *
Matthew Wilcox150030b2007-12-06 16:24:39 -0500422 * Interruptible by fatal signals only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * The user is responsible for holding a count on the request.
424 */
425int
426nfs_wait_on_request(struct nfs_page *req)
427{
Trond Myklebust9f557cd2010-02-03 08:27:22 -0500428 return wait_on_bit(&req->wb_flags, PG_BUSY,
429 nfs_wait_bit_uninterruptible,
430 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400433/*
434 * nfs_generic_pg_test - determine if requests can be coalesced
435 * @desc: pointer to descriptor
436 * @prev: previous request in desc, or NULL
437 * @req: this request
438 *
439 * Returns zero if @req can be coalesced into @desc, otherwise it returns
440 * the size of the request.
441 */
442size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc,
443 struct nfs_page *prev, struct nfs_page *req)
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300444{
Weston Andros Adamsonf0cb9ab2014-05-15 11:56:52 -0400445 if (desc->pg_count > desc->pg_bsize) {
446 /* should never happen */
447 WARN_ON_ONCE(1);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300448 return 0;
Weston Andros Adamsonf0cb9ab2014-05-15 11:56:52 -0400449 }
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300450
Weston Andros Adamsonf0cb9ab2014-05-15 11:56:52 -0400451 return min(desc->pg_bsize - desc->pg_count, (size_t)req->wb_bytes);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300452}
Benny Halevy19345cb2011-06-19 18:33:46 -0400453EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300454
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400455struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *ops)
Anna Schumaker00bfa302014-05-06 09:12:29 -0400456{
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400457 struct nfs_pgio_header *hdr = ops->rw_alloc_header();
Anna Schumaker00bfa302014-05-06 09:12:29 -0400458
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400459 if (hdr) {
Anna Schumaker4a0de552014-05-06 09:12:30 -0400460 INIT_LIST_HEAD(&hdr->pages);
Anna Schumaker4a0de552014-05-06 09:12:30 -0400461 spin_lock_init(&hdr->lock);
Anna Schumaker4a0de552014-05-06 09:12:30 -0400462 hdr->rw_ops = ops;
463 }
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400464 return hdr;
Anna Schumaker4a0de552014-05-06 09:12:30 -0400465}
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400466EXPORT_SYMBOL_GPL(nfs_pgio_header_alloc);
Anna Schumaker4a0de552014-05-06 09:12:30 -0400467
468/*
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400469 * nfs_pgio_header_free - Free a read or write header
Anna Schumaker4a0de552014-05-06 09:12:30 -0400470 * @hdr: The header to free
471 */
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400472void nfs_pgio_header_free(struct nfs_pgio_header *hdr)
Anna Schumaker4a0de552014-05-06 09:12:30 -0400473{
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400474 hdr->rw_ops->rw_free_header(hdr);
Anna Schumaker4a0de552014-05-06 09:12:30 -0400475}
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400476EXPORT_SYMBOL_GPL(nfs_pgio_header_free);
Anna Schumaker4a0de552014-05-06 09:12:30 -0400477
478/**
Weston Andros Adamson4714fb52014-06-09 11:48:37 -0400479 * nfs_pgio_data_destroy - make @hdr suitable for reuse
480 *
481 * Frees memory and releases refs from nfs_generic_pgio, so that it may
482 * be called again.
483 *
484 * @hdr: A header that has had nfs_generic_pgio called
Anna Schumaker00bfa302014-05-06 09:12:29 -0400485 */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400486void nfs_pgio_data_destroy(struct nfs_pgio_header *hdr)
Anna Schumaker00bfa302014-05-06 09:12:29 -0400487{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400488 put_nfs_open_context(hdr->args.context);
489 if (hdr->page_array.pagevec != hdr->page_array.page_array)
490 kfree(hdr->page_array.pagevec);
Anna Schumaker00bfa302014-05-06 09:12:29 -0400491}
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400492EXPORT_SYMBOL_GPL(nfs_pgio_data_destroy);
Anna Schumaker00bfa302014-05-06 09:12:29 -0400493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/**
Anna Schumakerce595152014-05-06 09:12:34 -0400495 * nfs_pgio_rpcsetup - Set up arguments for a pageio call
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400496 * @hdr: The pageio hdr
Anna Schumakerce595152014-05-06 09:12:34 -0400497 * @count: Number of bytes to read
498 * @offset: Initial offset
499 * @how: How to commit data (writes only)
500 * @cinfo: Commit information for the call (writes only)
501 */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400502static void nfs_pgio_rpcsetup(struct nfs_pgio_header *hdr,
Anna Schumakerce595152014-05-06 09:12:34 -0400503 unsigned int count, unsigned int offset,
504 int how, struct nfs_commit_info *cinfo)
505{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400506 struct nfs_page *req = hdr->req;
Anna Schumakerce595152014-05-06 09:12:34 -0400507
508 /* Set up the RPC argument and reply structs
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400509 * NB: take care not to mess about with hdr->commit et al. */
Anna Schumakerce595152014-05-06 09:12:34 -0400510
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400511 hdr->args.fh = NFS_FH(hdr->inode);
512 hdr->args.offset = req_offset(req) + offset;
Anna Schumakerce595152014-05-06 09:12:34 -0400513 /* pnfs_set_layoutcommit needs this */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400514 hdr->mds_offset = hdr->args.offset;
515 hdr->args.pgbase = req->wb_pgbase + offset;
516 hdr->args.pages = hdr->page_array.pagevec;
517 hdr->args.count = count;
518 hdr->args.context = get_nfs_open_context(req->wb_context);
519 hdr->args.lock_context = req->wb_lock_context;
520 hdr->args.stable = NFS_UNSTABLE;
Anna Schumakerce595152014-05-06 09:12:34 -0400521 switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
522 case 0:
523 break;
524 case FLUSH_COND_STABLE:
525 if (nfs_reqs_to_commit(cinfo))
526 break;
527 default:
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400528 hdr->args.stable = NFS_FILE_SYNC;
Anna Schumakerce595152014-05-06 09:12:34 -0400529 }
530
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400531 hdr->res.fattr = &hdr->fattr;
532 hdr->res.count = count;
533 hdr->res.eof = 0;
Weston Andros Adamsonc65e6252014-06-09 11:48:36 -0400534 hdr->res.verf = &hdr->verf;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400535 nfs_fattr_init(&hdr->fattr);
Anna Schumakerce595152014-05-06 09:12:34 -0400536}
537
538/**
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400539 * nfs_pgio_prepare - Prepare pageio hdr to go over the wire
Anna Schumakera4cdda52014-05-06 09:12:31 -0400540 * @task: The current task
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400541 * @calldata: pageio header to prepare
Anna Schumakera4cdda52014-05-06 09:12:31 -0400542 */
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400543static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
Anna Schumakera4cdda52014-05-06 09:12:31 -0400544{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400545 struct nfs_pgio_header *hdr = calldata;
Anna Schumakera4cdda52014-05-06 09:12:31 -0400546 int err;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400547 err = NFS_PROTO(hdr->inode)->pgio_rpc_prepare(task, hdr);
Anna Schumakera4cdda52014-05-06 09:12:31 -0400548 if (err)
549 rpc_exit(task, err);
550}
551
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400552int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr,
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400553 const struct rpc_call_ops *call_ops, int how, int flags)
554{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400555 struct inode *inode = hdr->inode;
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400556 struct rpc_task *task;
557 struct rpc_message msg = {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400558 .rpc_argp = &hdr->args,
559 .rpc_resp = &hdr->res,
560 .rpc_cred = hdr->cred,
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400561 };
562 struct rpc_task_setup task_setup_data = {
563 .rpc_client = clnt,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400564 .task = &hdr->task,
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400565 .rpc_message = &msg,
566 .callback_ops = call_ops,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400567 .callback_data = hdr,
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400568 .workqueue = nfsiod_workqueue,
569 .flags = RPC_TASK_ASYNC | flags,
570 };
571 int ret = 0;
572
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400573 hdr->rw_ops->rw_initiate(hdr, &msg, &task_setup_data, how);
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400574
575 dprintk("NFS: %5u initiated pgio call "
576 "(req %s/%llu, %u bytes @ offset %llu)\n",
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400577 hdr->task.tk_pid,
578 inode->i_sb->s_id,
579 (unsigned long long)NFS_FILEID(inode),
580 hdr->args.count,
581 (unsigned long long)hdr->args.offset);
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400582
583 task = rpc_run_task(&task_setup_data);
584 if (IS_ERR(task)) {
585 ret = PTR_ERR(task);
586 goto out;
587 }
588 if (how & FLUSH_SYNC) {
589 ret = rpc_wait_for_completion_task(task);
590 if (ret == 0)
591 ret = task->tk_status;
592 }
593 rpc_put_task(task);
594out:
595 return ret;
596}
597EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
598
Anna Schumakera4cdda52014-05-06 09:12:31 -0400599/**
Anna Schumaker844c9e62014-05-06 09:12:35 -0400600 * nfs_pgio_error - Clean up from a pageio error
601 * @desc: IO descriptor
602 * @hdr: pageio header
603 */
Anna Schumakeref2c4882014-05-06 09:12:36 -0400604static int nfs_pgio_error(struct nfs_pageio_descriptor *desc,
Anna Schumaker844c9e62014-05-06 09:12:35 -0400605 struct nfs_pgio_header *hdr)
606{
Anna Schumaker844c9e62014-05-06 09:12:35 -0400607 set_bit(NFS_IOHDR_REDO, &hdr->flags);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400608 nfs_pgio_data_destroy(hdr);
Weston Andros Adamson4714fb52014-06-09 11:48:37 -0400609 hdr->completion_ops->completion(hdr);
Anna Schumaker844c9e62014-05-06 09:12:35 -0400610 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
611 return -ENOMEM;
612}
613
614/**
Anna Schumakera4cdda52014-05-06 09:12:31 -0400615 * nfs_pgio_release - Release pageio data
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400616 * @calldata: The pageio header to release
Anna Schumakera4cdda52014-05-06 09:12:31 -0400617 */
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400618static void nfs_pgio_release(void *calldata)
Anna Schumakera4cdda52014-05-06 09:12:31 -0400619{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400620 struct nfs_pgio_header *hdr = calldata;
621 if (hdr->rw_ops->rw_release)
622 hdr->rw_ops->rw_release(hdr);
623 nfs_pgio_data_destroy(hdr);
Weston Andros Adamson4714fb52014-06-09 11:48:37 -0400624 hdr->completion_ops->completion(hdr);
Anna Schumakera4cdda52014-05-06 09:12:31 -0400625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627/**
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400628 * nfs_pageio_init - initialise a page io descriptor
629 * @desc: pointer to descriptor
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400630 * @inode: pointer to inode
631 * @doio: pointer to io function
632 * @bsize: io block size
633 * @io_flags: extra parameters for the io function
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400634 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400635void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
636 struct inode *inode,
Trond Myklebust1751c362011-06-10 13:30:23 -0400637 const struct nfs_pageio_ops *pg_ops,
Fred Isaman061ae2e2012-04-20 14:47:48 -0400638 const struct nfs_pgio_completion_ops *compl_ops,
Anna Schumaker4a0de552014-05-06 09:12:30 -0400639 const struct nfs_rw_ops *rw_ops,
Trond Myklebust84dde762007-05-04 14:44:06 -0400640 size_t bsize,
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400641 int io_flags)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400642{
643 INIT_LIST_HEAD(&desc->pg_list);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400644 desc->pg_bytes_written = 0;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400645 desc->pg_count = 0;
646 desc->pg_bsize = bsize;
647 desc->pg_base = 0;
Trond Myklebustb31268a2011-03-21 17:02:00 -0400648 desc->pg_moreio = 0;
Trond Myklebustd9156f92011-07-12 13:42:02 -0400649 desc->pg_recoalesce = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400650 desc->pg_inode = inode;
Trond Myklebust1751c362011-06-10 13:30:23 -0400651 desc->pg_ops = pg_ops;
Fred Isaman061ae2e2012-04-20 14:47:48 -0400652 desc->pg_completion_ops = compl_ops;
Anna Schumaker4a0de552014-05-06 09:12:30 -0400653 desc->pg_rw_ops = rw_ops;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400654 desc->pg_ioflags = io_flags;
655 desc->pg_error = 0;
Fred Isaman94ad1c82011-03-01 01:34:14 +0000656 desc->pg_lseg = NULL;
Fred Isaman584aa812012-04-20 14:47:51 -0400657 desc->pg_dreq = NULL;
Peng Taof6166382012-08-02 15:36:09 +0300658 desc->pg_layout_private = NULL;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400659}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400660EXPORT_SYMBOL_GPL(nfs_pageio_init);
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400661
Anna Schumaker0eecb212014-05-06 09:12:32 -0400662/**
663 * nfs_pgio_result - Basic pageio error handling
664 * @task: The task that ran
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400665 * @calldata: Pageio header to check
Anna Schumaker0eecb212014-05-06 09:12:32 -0400666 */
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400667static void nfs_pgio_result(struct rpc_task *task, void *calldata)
Anna Schumaker0eecb212014-05-06 09:12:32 -0400668{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400669 struct nfs_pgio_header *hdr = calldata;
670 struct inode *inode = hdr->inode;
Anna Schumaker0eecb212014-05-06 09:12:32 -0400671
672 dprintk("NFS: %s: %5u, (status %d)\n", __func__,
673 task->tk_pid, task->tk_status);
674
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400675 if (hdr->rw_ops->rw_done(task, hdr, inode) != 0)
Anna Schumaker0eecb212014-05-06 09:12:32 -0400676 return;
677 if (task->tk_status < 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400678 nfs_set_pgio_error(hdr, task->tk_status, hdr->args.offset);
Anna Schumaker0eecb212014-05-06 09:12:32 -0400679 else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400680 hdr->rw_ops->rw_result(task, hdr);
Anna Schumaker0eecb212014-05-06 09:12:32 -0400681}
682
Anna Schumakeref2c4882014-05-06 09:12:36 -0400683/*
Anna Schumakeref2c4882014-05-06 09:12:36 -0400684 * Create an RPC task for the given read or write request and kick it.
685 * The page must have been locked by the caller.
686 *
687 * It may happen that the page we're passed is not marked dirty.
688 * This is the case if nfs_updatepage detects a conflicting request
689 * that has been written but not committed.
690 */
Weston Andros Adamsonf0cb9ab2014-05-15 11:56:52 -0400691int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
692 struct nfs_pgio_header *hdr)
Anna Schumakeref2c4882014-05-06 09:12:36 -0400693{
694 struct nfs_page *req;
695 struct page **pages;
Anna Schumakeref2c4882014-05-06 09:12:36 -0400696 struct list_head *head = &desc->pg_list;
697 struct nfs_commit_info cinfo;
Weston Andros Adamson4714fb52014-06-09 11:48:37 -0400698 unsigned int pagecount;
Anna Schumakeref2c4882014-05-06 09:12:36 -0400699
Weston Andros Adamson4714fb52014-06-09 11:48:37 -0400700 pagecount = nfs_page_array_len(desc->pg_base, desc->pg_count);
701 if (!nfs_pgarray_set(&hdr->page_array, pagecount))
Anna Schumakeref2c4882014-05-06 09:12:36 -0400702 return nfs_pgio_error(desc, hdr);
703
704 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400705 pages = hdr->page_array.pagevec;
Anna Schumakeref2c4882014-05-06 09:12:36 -0400706 while (!list_empty(head)) {
707 req = nfs_list_entry(head->next);
708 nfs_list_remove_request(req);
709 nfs_list_add_request(req, &hdr->pages);
710 *pages++ = req->wb_page;
711 }
712
713 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
714 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
715 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
716
717 /* Set up the argument struct */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400718 nfs_pgio_rpcsetup(hdr, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
Anna Schumakeref2c4882014-05-06 09:12:36 -0400719 desc->pg_rpc_callops = &nfs_pgio_common_ops;
720 return 0;
721}
Weston Andros Adamsonf0cb9ab2014-05-15 11:56:52 -0400722EXPORT_SYMBOL_GPL(nfs_generic_pgio);
Anna Schumakeref2c4882014-05-06 09:12:36 -0400723
Anna Schumaker41d8d5b2014-05-06 09:12:40 -0400724static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
Anna Schumakercf485fc2014-05-06 09:12:39 -0400725{
Anna Schumakercf485fc2014-05-06 09:12:39 -0400726 struct nfs_pgio_header *hdr;
727 int ret;
728
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400729 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
730 if (!hdr) {
Anna Schumakercf485fc2014-05-06 09:12:39 -0400731 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
732 return -ENOMEM;
733 }
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400734 nfs_pgheader_init(desc, hdr, nfs_pgio_header_free);
Anna Schumakercf485fc2014-05-06 09:12:39 -0400735 ret = nfs_generic_pgio(desc, hdr);
736 if (ret == 0)
Weston Andros Adamson7f714722014-05-15 11:56:53 -0400737 ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400738 hdr, desc->pg_rpc_callops,
Weston Andros Adamson7f714722014-05-15 11:56:53 -0400739 desc->pg_ioflags, 0);
Anna Schumakercf485fc2014-05-06 09:12:39 -0400740 return ret;
741}
742
Trond Myklebust4109bb72013-09-06 11:09:38 -0400743static bool nfs_match_open_context(const struct nfs_open_context *ctx1,
744 const struct nfs_open_context *ctx2)
745{
746 return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state;
747}
748
749static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
750 const struct nfs_lock_context *l2)
751{
752 return l1->lockowner.l_owner == l2->lockowner.l_owner
753 && l1->lockowner.l_pid == l2->lockowner.l_pid;
754}
755
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400756/**
757 * nfs_can_coalesce_requests - test two requests for compatibility
758 * @prev: pointer to nfs_page
759 * @req: pointer to nfs_page
760 *
761 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
762 * page data area they describe is contiguous, and that their RPC
763 * credentials, NFSv4 open state, and lockowners are the same.
764 *
765 * Return 'true' if this is the case, else return 'false'.
766 */
Benny Halevy18ad0a92011-05-25 21:03:56 +0300767static bool nfs_can_coalesce_requests(struct nfs_page *prev,
768 struct nfs_page *req,
769 struct nfs_pageio_descriptor *pgio)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400770{
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400771 size_t size;
772
Weston Andros Adamsonab75e412014-05-15 11:56:44 -0400773 if (prev) {
774 if (!nfs_match_open_context(req->wb_context, prev->wb_context))
775 return false;
776 if (req->wb_context->dentry->d_inode->i_flock != NULL &&
777 !nfs_match_lock_context(req->wb_lock_context,
778 prev->wb_lock_context))
779 return false;
Weston Andros Adamsonab75e412014-05-15 11:56:44 -0400780 if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
781 return false;
782 }
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400783 size = pgio->pg_ops->pg_test(pgio, prev, req);
Weston Andros Adamsonf0cb9ab2014-05-15 11:56:52 -0400784 WARN_ON_ONCE(size > req->wb_bytes);
785 if (size && size < req->wb_bytes)
786 req->wb_bytes = size;
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400787 return size > 0;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400788}
789
790/**
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400791 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400792 * @desc: destination io descriptor
793 * @req: request
794 *
795 * Returns true if the request 'req' was successfully coalesced into the
796 * existing list of pages 'desc'.
797 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400798static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
799 struct nfs_page *req)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400800{
Weston Andros Adamsonab75e412014-05-15 11:56:44 -0400801 struct nfs_page *prev = NULL;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400802 if (desc->pg_count != 0) {
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400803 prev = nfs_list_entry(desc->pg_list.prev);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300804 } else {
Trond Myklebustd8007d42011-06-10 13:30:23 -0400805 if (desc->pg_ops->pg_init)
806 desc->pg_ops->pg_init(desc, req);
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400807 desc->pg_base = req->wb_pgbase;
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300808 }
Weston Andros Adamsonab75e412014-05-15 11:56:44 -0400809 if (!nfs_can_coalesce_requests(prev, req, desc))
810 return 0;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400811 nfs_list_remove_request(req);
812 nfs_list_add_request(req, &desc->pg_list);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300813 desc->pg_count += req->wb_bytes;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400814 return 1;
815}
816
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400817/*
818 * Helper for nfs_pageio_add_request and nfs_pageio_complete
819 */
820static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
821{
822 if (!list_empty(&desc->pg_list)) {
Trond Myklebust1751c362011-06-10 13:30:23 -0400823 int error = desc->pg_ops->pg_doio(desc);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400824 if (error < 0)
825 desc->pg_error = error;
826 else
827 desc->pg_bytes_written += desc->pg_count;
828 }
829 if (list_empty(&desc->pg_list)) {
830 desc->pg_count = 0;
831 desc->pg_base = 0;
832 }
833}
834
835/**
836 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
837 * @desc: destination io descriptor
838 * @req: request
839 *
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400840 * This may split a request into subrequests which are all part of the
841 * same page group.
842 *
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400843 * Returns true if the request 'req' was successfully coalesced into the
844 * existing list of pages 'desc'.
845 */
Trond Myklebustd9156f92011-07-12 13:42:02 -0400846static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400847 struct nfs_page *req)
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400848{
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400849 struct nfs_page *subreq;
850 unsigned int bytes_left = 0;
851 unsigned int offset, pgbase;
852
853 nfs_page_group_lock(req);
854
855 subreq = req;
856 bytes_left = subreq->wb_bytes;
857 offset = subreq->wb_offset;
858 pgbase = subreq->wb_pgbase;
859
860 do {
861 if (!nfs_pageio_do_add_request(desc, subreq)) {
862 /* make sure pg_test call(s) did nothing */
863 WARN_ON_ONCE(subreq->wb_bytes != bytes_left);
864 WARN_ON_ONCE(subreq->wb_offset != offset);
865 WARN_ON_ONCE(subreq->wb_pgbase != pgbase);
866
867 nfs_page_group_unlock(req);
868 desc->pg_moreio = 1;
869 nfs_pageio_doio(desc);
870 if (desc->pg_error < 0)
871 return 0;
872 desc->pg_moreio = 0;
873 if (desc->pg_recoalesce)
874 return 0;
875 /* retry add_request for this subreq */
876 nfs_page_group_lock(req);
877 continue;
878 }
879
880 /* check for buggy pg_test call(s) */
881 WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE);
882 WARN_ON_ONCE(subreq->wb_bytes > bytes_left);
883 WARN_ON_ONCE(subreq->wb_bytes == 0);
884
885 bytes_left -= subreq->wb_bytes;
886 offset += subreq->wb_bytes;
887 pgbase += subreq->wb_bytes;
888
889 if (bytes_left) {
890 subreq = nfs_create_request(req->wb_context,
891 req->wb_page,
892 subreq, pgbase, bytes_left);
Trond Myklebustc1109552014-05-29 11:38:15 -0400893 if (IS_ERR(subreq))
894 goto err_ptr;
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400895 nfs_lock_request(subreq);
896 subreq->wb_offset = offset;
897 subreq->wb_index = req->wb_index;
898 }
899 } while (bytes_left > 0);
900
901 nfs_page_group_unlock(req);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400902 return 1;
Trond Myklebustc1109552014-05-29 11:38:15 -0400903err_ptr:
904 desc->pg_error = PTR_ERR(subreq);
905 nfs_page_group_unlock(req);
906 return 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400907}
908
Trond Myklebustd9156f92011-07-12 13:42:02 -0400909static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
910{
911 LIST_HEAD(head);
912
913 do {
914 list_splice_init(&desc->pg_list, &head);
915 desc->pg_bytes_written -= desc->pg_count;
916 desc->pg_count = 0;
917 desc->pg_base = 0;
918 desc->pg_recoalesce = 0;
919
920 while (!list_empty(&head)) {
921 struct nfs_page *req;
922
923 req = list_first_entry(&head, struct nfs_page, wb_list);
924 nfs_list_remove_request(req);
925 if (__nfs_pageio_add_request(desc, req))
926 continue;
927 if (desc->pg_error < 0)
928 return 0;
929 break;
930 }
931 } while (desc->pg_recoalesce);
932 return 1;
933}
934
935int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
936 struct nfs_page *req)
937{
938 int ret;
939
940 do {
941 ret = __nfs_pageio_add_request(desc, req);
942 if (ret)
943 break;
944 if (desc->pg_error < 0)
945 break;
946 ret = nfs_do_recoalesce(desc);
947 } while (ret);
948 return ret;
949}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400950EXPORT_SYMBOL_GPL(nfs_pageio_add_request);
Trond Myklebustd9156f92011-07-12 13:42:02 -0400951
Weston Andros Adamson53113ad2014-06-09 11:48:38 -0400952/*
953 * nfs_pageio_resend - Transfer requests to new descriptor and resend
954 * @hdr - the pgio header to move request from
955 * @desc - the pageio descriptor to add requests to
956 *
957 * Try to move each request (nfs_page) from @hdr to @desc then attempt
958 * to send them.
959 *
960 * Returns 0 on success and < 0 on error.
961 */
962int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
963 struct nfs_pgio_header *hdr)
964{
965 LIST_HEAD(failed);
966
967 desc->pg_dreq = hdr->dreq;
968 while (!list_empty(&hdr->pages)) {
969 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
970
971 nfs_list_remove_request(req);
972 if (!nfs_pageio_add_request(desc, req))
973 nfs_list_add_request(req, &failed);
974 }
975 nfs_pageio_complete(desc);
976 if (!list_empty(&failed)) {
977 list_move(&failed, &hdr->pages);
978 return -EIO;
979 }
980 return 0;
981}
982EXPORT_SYMBOL_GPL(nfs_pageio_resend);
983
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400984/**
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400985 * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
986 * @desc: pointer to io descriptor
987 */
988void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
989{
Trond Myklebustd9156f92011-07-12 13:42:02 -0400990 for (;;) {
991 nfs_pageio_doio(desc);
992 if (!desc->pg_recoalesce)
993 break;
994 if (!nfs_do_recoalesce(desc))
995 break;
996 }
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400997}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400998EXPORT_SYMBOL_GPL(nfs_pageio_complete);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400999
Trond Myklebust7fe7f842007-05-20 10:18:27 -04001000/**
1001 * nfs_pageio_cond_complete - Conditional I/O completion
1002 * @desc: pointer to io descriptor
1003 * @index: page index
1004 *
1005 * It is important to ensure that processes don't try to take locks
1006 * on non-contiguous ranges of pages as that might deadlock. This
1007 * function should be called before attempting to wait on a locked
1008 * nfs_page. It will complete the I/O if the page index 'index'
1009 * is not contiguous with the existing list of pages in 'desc'.
1010 */
1011void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
1012{
1013 if (!list_empty(&desc->pg_list)) {
1014 struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
1015 if (index != prev->wb_index + 1)
Trond Myklebustd9156f92011-07-12 13:42:02 -04001016 nfs_pageio_complete(desc);
Trond Myklebust7fe7f842007-05-20 10:18:27 -04001017 }
1018}
1019
David Howellsf7b422b2006-06-09 09:34:33 -04001020int __init nfs_init_nfspagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 nfs_page_cachep = kmem_cache_create("nfs_page",
1023 sizeof(struct nfs_page),
1024 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001025 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (nfs_page_cachep == NULL)
1027 return -ENOMEM;
1028
1029 return 0;
1030}
1031
David Brownell266bee82006-06-27 12:59:15 -07001032void nfs_destroy_nfspagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001034 kmem_cache_destroy(nfs_page_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
Anna Schumakeref2c4882014-05-06 09:12:36 -04001037static const struct rpc_call_ops nfs_pgio_common_ops = {
Anna Schumaker6f92fa42014-05-06 09:12:33 -04001038 .rpc_call_prepare = nfs_pgio_prepare,
1039 .rpc_call_done = nfs_pgio_result,
1040 .rpc_release = nfs_pgio_release,
1041};
Anna Schumaker41d8d5b2014-05-06 09:12:40 -04001042
1043const struct nfs_pageio_ops nfs_pgio_rw_ops = {
1044 .pg_test = nfs_generic_pg_test,
1045 .pg_doio = nfs_generic_pg_pgios,
1046};