]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - fs/nfs/pagelist.c
UBUNTU: Ubuntu-5.4.0-117.132
[mirror_ubuntu-focal-kernel.git] / fs / nfs / pagelist.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * linux/fs/nfs/pagelist.c
4 *
5 * A set of helper functions for managing NFS read and write requests.
6 * The main purpose of these routines is to provide support for the
7 * coalescing of several requests into a single RPC call.
8 *
9 * Copyright 2000, 2001 (c) Trond Myklebust <trond.myklebust@fys.uio.no>
10 *
11 */
12
1da177e4
LT
13#include <linux/slab.h>
14#include <linux/file.h>
e8edc6e0 15#include <linux/sched.h>
1da177e4 16#include <linux/sunrpc/clnt.h>
1313e603 17#include <linux/nfs.h>
1da177e4
LT
18#include <linux/nfs3.h>
19#include <linux/nfs4.h>
1da177e4 20#include <linux/nfs_fs.h>
33344e0f 21#include <linux/nfs_page.h>
1da177e4 22#include <linux/nfs_mount.h>
afeacc8c 23#include <linux/export.h>
1da177e4 24
8d5658c9 25#include "internal.h"
bae724ef 26#include "pnfs.h"
8d5658c9 27
0eecb214
AS
28#define NFSDBG_FACILITY NFSDBG_PAGECACHE
29
e18b890b 30static struct kmem_cache *nfs_page_cachep;
ef2c488c 31static const struct rpc_call_ops nfs_pgio_common_ops;
1da177e4 32
48d635f1
PT
33struct nfs_pgio_mirror *
34nfs_pgio_current_mirror(struct nfs_pageio_descriptor *desc)
35{
36 return nfs_pgio_has_mirroring(desc) ?
37 &desc->pg_mirrors[desc->pg_mirror_idx] :
38 &desc->pg_mirrors[0];
39}
40EXPORT_SYMBOL_GPL(nfs_pgio_current_mirror);
41
4db6e0b7
FI
42void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
43 struct nfs_pgio_header *hdr,
44 void (*release)(struct nfs_pgio_header *hdr))
45{
48d635f1 46 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
a7d42ddb
WAA
47
48
49 hdr->req = nfs_list_entry(mirror->pg_list.next);
4db6e0b7 50 hdr->inode = desc->pg_inode;
9fcd5960 51 hdr->cred = nfs_req_openctx(hdr->req)->cred;
4db6e0b7 52 hdr->io_start = req_offset(hdr->req);
a7d42ddb 53 hdr->good_bytes = mirror->pg_count;
919e3bd9 54 hdr->io_completion = desc->pg_io_completion;
584aa810 55 hdr->dreq = desc->pg_dreq;
4db6e0b7 56 hdr->release = release;
061ae2ed 57 hdr->completion_ops = desc->pg_completion_ops;
584aa810
FI
58 if (hdr->completion_ops->init_hdr)
59 hdr->completion_ops->init_hdr(hdr);
a7d42ddb
WAA
60
61 hdr->pgio_mirror_idx = desc->pg_mirror_idx;
4db6e0b7 62}
89d77c8f 63EXPORT_SYMBOL_GPL(nfs_pgheader_init);
4db6e0b7
FI
64
65void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos)
66{
1c6c4b74
TM
67 unsigned int new = pos - hdr->io_start;
68
69 if (hdr->good_bytes > new) {
70 hdr->good_bytes = new;
4db6e0b7 71 clear_bit(NFS_IOHDR_EOF, &hdr->flags);
1c6c4b74
TM
72 if (!test_and_set_bit(NFS_IOHDR_ERROR, &hdr->flags))
73 hdr->error = error;
4db6e0b7 74 }
4db6e0b7
FI
75}
76
1da177e4
LT
77static inline struct nfs_page *
78nfs_page_alloc(void)
79{
2b17d725 80 struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_KERNEL);
72895b1a 81 if (p)
1da177e4 82 INIT_LIST_HEAD(&p->wb_list);
1da177e4
LT
83 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
577b4232
TM
92/**
93 * nfs_iocounter_wait - wait for i/o to complete
210c7c17 94 * @l_ctx: nfs_lock_context with io_counter to use
577b4232
TM
95 *
96 * returns -ERESTARTSYS if interrupted by a fatal signal.
97 * Otherwise returns 0 once the io_count hits 0.
98 */
99int
210c7c17 100nfs_iocounter_wait(struct nfs_lock_context *l_ctx)
577b4232 101{
723c921e
PZ
102 return wait_var_event_killable(&l_ctx->io_count,
103 !atomic_read(&l_ctx->io_count));
577b4232
TM
104}
105
7d6ddf88
BC
106/**
107 * nfs_async_iocounter_wait - wait on a rpc_waitqueue for I/O
108 * to complete
109 * @task: the rpc_task that should wait
110 * @l_ctx: nfs_lock_context with io_counter to check
111 *
112 * Returns true if there is outstanding I/O to wait on and the
113 * task has been put to sleep.
114 */
115bool
116nfs_async_iocounter_wait(struct rpc_task *task, struct nfs_lock_context *l_ctx)
117{
118 struct inode *inode = d_inode(l_ctx->open_context->dentry);
119 bool ret = false;
120
121 if (atomic_read(&l_ctx->io_count) > 0) {
122 rpc_sleep_on(&NFS_SERVER(inode)->uoc_rpcwaitq, task, NULL);
123 ret = true;
124 }
125
126 if (atomic_read(&l_ctx->io_count) == 0) {
127 rpc_wake_up_queued_task(&NFS_SERVER(inode)->uoc_rpcwaitq, task);
128 ret = false;
129 }
130
131 return ret;
132}
133EXPORT_SYMBOL_GPL(nfs_async_iocounter_wait);
134
2bfc6e56 135/*
aea90c4e
TM
136 * nfs_page_set_headlock - set the request PG_HEADLOCK
137 * @req: request that is to be locked
2bfc6e56 138 *
aea90c4e 139 * this lock must be held when modifying req->wb_head
e7029206 140 *
1344b7ea 141 * return 0 on success, < 0 on error
2bfc6e56 142 */
e7029206 143int
aea90c4e 144nfs_page_set_headlock(struct nfs_page *req)
2bfc6e56 145{
aea90c4e 146 if (!test_and_set_bit(PG_HEADLOCK, &req->wb_flags))
bc8a309e 147 return 0;
e7029206 148
aea90c4e 149 set_bit(PG_CONTENDED1, &req->wb_flags);
1344b7ea 150 smp_mb__after_atomic();
aea90c4e 151 return wait_on_bit_lock(&req->wb_flags, PG_HEADLOCK,
bc8a309e 152 TASK_UNINTERRUPTIBLE);
2bfc6e56
WAA
153}
154
155/*
aea90c4e
TM
156 * nfs_page_clear_headlock - clear the request PG_HEADLOCK
157 * @req: request that is to be locked
2bfc6e56
WAA
158 */
159void
aea90c4e 160nfs_page_clear_headlock(struct nfs_page *req)
2bfc6e56 161{
d1e1cda8 162 smp_mb__before_atomic();
aea90c4e 163 clear_bit(PG_HEADLOCK, &req->wb_flags);
d1e1cda8 164 smp_mb__after_atomic();
aea90c4e 165 if (!test_bit(PG_CONTENDED1, &req->wb_flags))
b4f937cf 166 return;
aea90c4e
TM
167 wake_up_bit(&req->wb_flags, PG_HEADLOCK);
168}
169
170/*
171 * nfs_page_group_lock - lock the head of the page group
172 * @req: request in group that is to be locked
173 *
174 * this lock must be held when traversing or modifying the page
175 * group list
176 *
177 * return 0 on success, < 0 on error
178 */
179int
180nfs_page_group_lock(struct nfs_page *req)
181{
182 int ret;
183
184 ret = nfs_page_set_headlock(req);
185 if (ret || req->wb_head == req)
186 return ret;
187 return nfs_page_set_headlock(req->wb_head);
188}
189
190/*
191 * nfs_page_group_unlock - unlock the head of the page group
192 * @req: request in group that is to be unlocked
193 */
194void
195nfs_page_group_unlock(struct nfs_page *req)
196{
197 if (req != req->wb_head)
198 nfs_page_clear_headlock(req->wb_head);
199 nfs_page_clear_headlock(req);
2bfc6e56
WAA
200}
201
202/*
203 * nfs_page_group_sync_on_bit_locked
204 *
205 * must be called with page group lock held
206 */
207static bool
208nfs_page_group_sync_on_bit_locked(struct nfs_page *req, unsigned int bit)
209{
210 struct nfs_page *head = req->wb_head;
211 struct nfs_page *tmp;
212
213 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_flags));
214 WARN_ON_ONCE(test_and_set_bit(bit, &req->wb_flags));
215
216 tmp = req->wb_this_page;
217 while (tmp != req) {
218 if (!test_bit(bit, &tmp->wb_flags))
219 return false;
220 tmp = tmp->wb_this_page;
221 }
222
223 /* true! reset all bits */
224 tmp = req;
225 do {
226 clear_bit(bit, &tmp->wb_flags);
227 tmp = tmp->wb_this_page;
228 } while (tmp != req);
229
230 return true;
231}
232
233/*
234 * nfs_page_group_sync_on_bit - set bit on current request, but only
235 * return true if the bit is set for all requests in page group
236 * @req - request in page group
237 * @bit - PG_* bit that is used to sync page group
238 */
239bool nfs_page_group_sync_on_bit(struct nfs_page *req, unsigned int bit)
240{
241 bool ret;
242
1344b7ea 243 nfs_page_group_lock(req);
2bfc6e56
WAA
244 ret = nfs_page_group_sync_on_bit_locked(req, bit);
245 nfs_page_group_unlock(req);
246
247 return ret;
248}
249
250/*
251 * nfs_page_group_init - Initialize the page group linkage for @req
252 * @req - a new nfs request
253 * @prev - the previous request in page group, or NULL if @req is the first
254 * or only request in the group (the head).
255 */
256static inline void
257nfs_page_group_init(struct nfs_page *req, struct nfs_page *prev)
258{
cb1410c7 259 struct inode *inode;
2bfc6e56
WAA
260 WARN_ON_ONCE(prev == req);
261
262 if (!prev) {
85710a83 263 /* a head request */
2bfc6e56
WAA
264 req->wb_head = req;
265 req->wb_this_page = req;
266 } else {
85710a83 267 /* a subrequest */
2bfc6e56
WAA
268 WARN_ON_ONCE(prev->wb_this_page != prev->wb_head);
269 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &prev->wb_head->wb_flags));
270 req->wb_head = prev->wb_head;
271 req->wb_this_page = prev->wb_this_page;
272 prev->wb_this_page = req;
273
85710a83
WAA
274 /* All subrequests take a ref on the head request until
275 * nfs_page_group_destroy is called */
276 kref_get(&req->wb_head->wb_kref);
277
cb1410c7
WAA
278 /* grab extra ref and bump the request count if head request
279 * has extra ref from the write/commit path to handle handoff
280 * between write and commit lists. */
17089a29 281 if (test_bit(PG_INODE_REF, &prev->wb_head->wb_flags)) {
cb1410c7 282 inode = page_file_mapping(req->wb_page)->host;
17089a29 283 set_bit(PG_INODE_REF, &req->wb_flags);
2bfc6e56 284 kref_get(&req->wb_kref);
a6b6d5b8 285 atomic_long_inc(&NFS_I(inode)->nrequests);
17089a29 286 }
2bfc6e56
WAA
287 }
288}
289
290/*
291 * nfs_page_group_destroy - sync the destruction of page groups
292 * @req - request that no longer needs the page group
293 *
294 * releases the page group reference from each member once all
295 * members have called this function.
296 */
297static void
298nfs_page_group_destroy(struct kref *kref)
299{
300 struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
08fead2a 301 struct nfs_page *head = req->wb_head;
2bfc6e56
WAA
302 struct nfs_page *tmp, *next;
303
304 if (!nfs_page_group_sync_on_bit(req, PG_TEARDOWN))
08fead2a 305 goto out;
2bfc6e56
WAA
306
307 tmp = req;
308 do {
309 next = tmp->wb_this_page;
310 /* unlink and free */
311 tmp->wb_this_page = tmp;
312 tmp->wb_head = tmp;
313 nfs_free_request(tmp);
314 tmp = next;
315 } while (tmp != req);
08fead2a
TM
316out:
317 /* subrequests must release the ref on the head request */
318 if (head != req)
319 nfs_release_request(head);
2bfc6e56
WAA
320}
321
c917cfaf
TM
322static struct nfs_page *
323__nfs_create_request(struct nfs_lock_context *l_ctx, struct page *page,
28b1d3f5
TM
324 unsigned int pgbase, unsigned int offset,
325 unsigned int count)
1da177e4 326{
1da177e4 327 struct nfs_page *req;
c917cfaf 328 struct nfs_open_context *ctx = l_ctx->open_context;
1da177e4 329
c58c8441
TM
330 if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
331 return ERR_PTR(-EBADF);
18eb8842
TM
332 /* try to allocate the request struct */
333 req = nfs_page_alloc();
334 if (req == NULL)
335 return ERR_PTR(-ENOMEM);
1da177e4 336
b3c54de6 337 req->wb_lock_context = l_ctx;
c917cfaf 338 refcount_inc(&l_ctx->count);
210c7c17 339 atomic_inc(&l_ctx->io_count);
015f0212 340
1da177e4
LT
341 /* Initialize the request struct. Initially, we assume a
342 * long write-back delay. This will be adjusted in
343 * update_nfs_request below if the region is not locked. */
344 req->wb_page = page;
67911c8f 345 if (page) {
8cd79788 346 req->wb_index = page_index(page);
67911c8f
AS
347 get_page(page);
348 }
1da177e4 349 req->wb_offset = offset;
c917cfaf 350 req->wb_pgbase = pgbase;
1da177e4 351 req->wb_bytes = count;
c03b4024 352 kref_init(&req->wb_kref);
33344e0f 353 req->wb_nio = 0;
1da177e4
LT
354 return req;
355}
356
c917cfaf
TM
357/**
358 * nfs_create_request - Create an NFS read/write request.
359 * @ctx: open context to use
360 * @page: page to write
c917cfaf
TM
361 * @offset: starting offset within the page for the write
362 * @count: number of bytes to read/write
363 *
364 * The page must be locked by the caller. This makes sure we never
365 * create two different requests for the same page.
366 * User should ensure it is safe to sleep in this function.
367 */
368struct nfs_page *
369nfs_create_request(struct nfs_open_context *ctx, struct page *page,
28b1d3f5 370 unsigned int offset, unsigned int count)
c917cfaf
TM
371{
372 struct nfs_lock_context *l_ctx = nfs_get_lock_context(ctx);
373 struct nfs_page *ret;
374
375 if (IS_ERR(l_ctx))
376 return ERR_CAST(l_ctx);
28b1d3f5
TM
377 ret = __nfs_create_request(l_ctx, page, offset, offset, count);
378 if (!IS_ERR(ret))
379 nfs_page_group_init(ret, NULL);
c917cfaf
TM
380 nfs_put_lock_context(l_ctx);
381 return ret;
382}
383
384static struct nfs_page *
385nfs_create_subreq(struct nfs_page *req, struct nfs_page *last,
386 unsigned int pgbase, unsigned int offset,
387 unsigned int count)
388{
389 struct nfs_page *ret;
390
28b1d3f5 391 ret = __nfs_create_request(req->wb_lock_context, req->wb_page,
c917cfaf
TM
392 pgbase, offset, count);
393 if (!IS_ERR(ret)) {
394 nfs_lock_request(ret);
395 ret->wb_index = req->wb_index;
28b1d3f5 396 nfs_page_group_init(ret, last);
33344e0f 397 ret->wb_nio = req->wb_nio;
c917cfaf
TM
398 }
399 return ret;
400}
401
1da177e4 402/**
1d1afcbc 403 * nfs_unlock_request - Unlock request and wake up sleepers.
302fad7b 404 * @req: pointer to request
1da177e4 405 */
1d1afcbc 406void nfs_unlock_request(struct nfs_page *req)
1da177e4
LT
407{
408 if (!NFS_WBACK_BUSY(req)) {
409 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
410 BUG();
411 }
4e857c58 412 smp_mb__before_atomic();
1da177e4 413 clear_bit(PG_BUSY, &req->wb_flags);
4e857c58 414 smp_mb__after_atomic();
b4f937cf
TM
415 if (!test_bit(PG_CONTENDED2, &req->wb_flags))
416 return;
464a98bd 417 wake_up_bit(&req->wb_flags, PG_BUSY);
3aff4ebb
TM
418}
419
420/**
1d1afcbc 421 * nfs_unlock_and_release_request - Unlock request and release the nfs_page
302fad7b 422 * @req: pointer to request
3aff4ebb 423 */
1d1afcbc 424void nfs_unlock_and_release_request(struct nfs_page *req)
3aff4ebb 425{
1d1afcbc 426 nfs_unlock_request(req);
1da177e4
LT
427 nfs_release_request(req);
428}
429
4d65c520 430/*
1da177e4
LT
431 * nfs_clear_request - Free up all resources allocated to the request
432 * @req:
433 *
bb6fbc45
TM
434 * Release page and open context resources associated with a read/write
435 * request after it has completed.
1da177e4 436 */
4d65c520 437static void nfs_clear_request(struct nfs_page *req)
1da177e4 438{
cd52ed35 439 struct page *page = req->wb_page;
f11ac8db 440 struct nfs_lock_context *l_ctx = req->wb_lock_context;
c79d183e 441 struct nfs_open_context *ctx;
bb6fbc45 442
cd52ed35 443 if (page != NULL) {
09cbfeaf 444 put_page(page);
1da177e4
LT
445 req->wb_page = NULL;
446 }
f11ac8db 447 if (l_ctx != NULL) {
7d6ddf88 448 if (atomic_dec_and_test(&l_ctx->io_count)) {
723c921e 449 wake_up_var(&l_ctx->io_count);
c79d183e 450 ctx = l_ctx->open_context;
7d6ddf88
BC
451 if (test_bit(NFS_CONTEXT_UNLOCK, &ctx->flags))
452 rpc_wake_up(&NFS_SERVER(d_inode(ctx->dentry))->uoc_rpcwaitq);
453 }
f11ac8db
TM
454 nfs_put_lock_context(l_ctx);
455 req->wb_lock_context = NULL;
456 }
1da177e4
LT
457}
458
1da177e4
LT
459/**
460 * nfs_release_request - Release the count on an NFS read/write request
461 * @req: request to release
462 *
463 * Note: Should never be called with the spinlock held!
464 */
d4581383 465void nfs_free_request(struct nfs_page *req)
1da177e4 466{
2bfc6e56
WAA
467 WARN_ON_ONCE(req->wb_this_page != req);
468
469 /* extra debug: make sure no sync bits are still set */
470 WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
67d0338e
WAA
471 WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags));
472 WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags));
20633f04
WAA
473 WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags));
474 WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags));
1da177e4 475
bb6fbc45 476 /* Release struct file and open context */
1da177e4 477 nfs_clear_request(req);
1da177e4
LT
478 nfs_page_free(req);
479}
480
c03b4024
TM
481void nfs_release_request(struct nfs_page *req)
482{
2bfc6e56 483 kref_put(&req->wb_kref, nfs_page_group_destroy);
9f557cd8 484}
2ce209c4 485EXPORT_SYMBOL_GPL(nfs_release_request);
9f557cd8 486
1da177e4
LT
487/**
488 * nfs_wait_on_request - Wait for a request to complete.
489 * @req: request to wait upon.
490 *
150030b7 491 * Interruptible by fatal signals only.
1da177e4
LT
492 * The user is responsible for holding a count on the request.
493 */
494int
495nfs_wait_on_request(struct nfs_page *req)
496{
b4f937cf
TM
497 if (!test_bit(PG_BUSY, &req->wb_flags))
498 return 0;
499 set_bit(PG_CONTENDED2, &req->wb_flags);
500 smp_mb__after_atomic();
74316201
N
501 return wait_on_bit_io(&req->wb_flags, PG_BUSY,
502 TASK_UNINTERRUPTIBLE);
1da177e4 503}
2ce209c4 504EXPORT_SYMBOL_GPL(nfs_wait_on_request);
1da177e4 505
b4fdac1a
WAA
506/*
507 * nfs_generic_pg_test - determine if requests can be coalesced
508 * @desc: pointer to descriptor
509 * @prev: previous request in desc, or NULL
510 * @req: this request
511 *
ac0aa5e8 512 * Returns zero if @req cannot be coalesced into @desc, otherwise it returns
b4fdac1a
WAA
513 * the size of the request.
514 */
515size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc,
516 struct nfs_page *prev, struct nfs_page *req)
5b36c7dc 517{
48d635f1 518 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
a7d42ddb
WAA
519
520
521 if (mirror->pg_count > mirror->pg_bsize) {
f0cb9ab8
WAA
522 /* should never happen */
523 WARN_ON_ONCE(1);
5b36c7dc 524 return 0;
f0cb9ab8 525 }
5b36c7dc 526
2e11f829
CH
527 /*
528 * Limit the request size so that we can still allocate a page array
529 * for it without upsetting the slab allocator.
530 */
a7d42ddb 531 if (((mirror->pg_count + req->wb_bytes) >> PAGE_SHIFT) *
048883e0 532 sizeof(struct page *) > PAGE_SIZE)
2e11f829
CH
533 return 0;
534
a7d42ddb 535 return min(mirror->pg_bsize - mirror->pg_count, (size_t)req->wb_bytes);
5b36c7dc 536}
19345cb2 537EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
5b36c7dc 538
1e7f3a48 539struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *ops)
4a0de55c 540{
1e7f3a48 541 struct nfs_pgio_header *hdr = ops->rw_alloc_header();
4a0de55c 542
1e7f3a48 543 if (hdr) {
4a0de55c 544 INIT_LIST_HEAD(&hdr->pages);
4a0de55c
AS
545 hdr->rw_ops = ops;
546 }
1e7f3a48 547 return hdr;
4a0de55c 548}
1e7f3a48 549EXPORT_SYMBOL_GPL(nfs_pgio_header_alloc);
4a0de55c 550
00bfa30a 551/**
4714fb51
WAA
552 * nfs_pgio_data_destroy - make @hdr suitable for reuse
553 *
554 * Frees memory and releases refs from nfs_generic_pgio, so that it may
555 * be called again.
556 *
557 * @hdr: A header that has had nfs_generic_pgio called
00bfa30a 558 */
196639eb 559static void nfs_pgio_data_destroy(struct nfs_pgio_header *hdr)
00bfa30a 560{
3caa0c6e
TM
561 if (hdr->args.context)
562 put_nfs_open_context(hdr->args.context);
d45f60c6
WAA
563 if (hdr->page_array.pagevec != hdr->page_array.page_array)
564 kfree(hdr->page_array.pagevec);
00bfa30a 565}
196639eb
TM
566
567/*
568 * nfs_pgio_header_free - Free a read or write header
569 * @hdr: The header to free
570 */
571void nfs_pgio_header_free(struct nfs_pgio_header *hdr)
572{
573 nfs_pgio_data_destroy(hdr);
574 hdr->rw_ops->rw_free_header(hdr);
575}
576EXPORT_SYMBOL_GPL(nfs_pgio_header_free);
00bfa30a 577
ce59515c
AS
578/**
579 * nfs_pgio_rpcsetup - Set up arguments for a pageio call
d45f60c6 580 * @hdr: The pageio hdr
ce59515c 581 * @count: Number of bytes to read
ce59515c
AS
582 * @how: How to commit data (writes only)
583 * @cinfo: Commit information for the call (writes only)
584 */
d45f60c6 585static void nfs_pgio_rpcsetup(struct nfs_pgio_header *hdr,
e545735a 586 unsigned int count,
ce59515c
AS
587 int how, struct nfs_commit_info *cinfo)
588{
d45f60c6 589 struct nfs_page *req = hdr->req;
ce59515c
AS
590
591 /* Set up the RPC argument and reply structs
d45f60c6 592 * NB: take care not to mess about with hdr->commit et al. */
ce59515c 593
d45f60c6 594 hdr->args.fh = NFS_FH(hdr->inode);
e545735a 595 hdr->args.offset = req_offset(req);
ce59515c 596 /* pnfs_set_layoutcommit needs this */
d45f60c6 597 hdr->mds_offset = hdr->args.offset;
e545735a 598 hdr->args.pgbase = req->wb_pgbase;
d45f60c6
WAA
599 hdr->args.pages = hdr->page_array.pagevec;
600 hdr->args.count = count;
9fcd5960 601 hdr->args.context = get_nfs_open_context(nfs_req_openctx(req));
d45f60c6
WAA
602 hdr->args.lock_context = req->wb_lock_context;
603 hdr->args.stable = NFS_UNSTABLE;
ce59515c
AS
604 switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
605 case 0:
606 break;
607 case FLUSH_COND_STABLE:
608 if (nfs_reqs_to_commit(cinfo))
609 break;
01e03bdc 610 /* fall through */
ce59515c 611 default:
d45f60c6 612 hdr->args.stable = NFS_FILE_SYNC;
ce59515c
AS
613 }
614
d45f60c6 615 hdr->res.fattr = &hdr->fattr;
17d8c5d1 616 hdr->res.count = 0;
d45f60c6 617 hdr->res.eof = 0;
c65e6254 618 hdr->res.verf = &hdr->verf;
d45f60c6 619 nfs_fattr_init(&hdr->fattr);
ce59515c
AS
620}
621
a4cdda59 622/**
d45f60c6 623 * nfs_pgio_prepare - Prepare pageio hdr to go over the wire
a4cdda59 624 * @task: The current task
d45f60c6 625 * @calldata: pageio header to prepare
a4cdda59 626 */
6f92fa45 627static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
a4cdda59 628{
d45f60c6 629 struct nfs_pgio_header *hdr = calldata;
a4cdda59 630 int err;
d45f60c6 631 err = NFS_PROTO(hdr->inode)->pgio_rpc_prepare(task, hdr);
a4cdda59
AS
632 if (err)
633 rpc_exit(task, err);
634}
635
d45f60c6 636int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr,
a52458b4 637 const struct cred *cred, const struct nfs_rpc_ops *rpc_ops,
1ed26f33
AS
638 const struct rpc_call_ops *call_ops, int how, int flags)
639{
640 struct rpc_task *task;
641 struct rpc_message msg = {
d45f60c6
WAA
642 .rpc_argp = &hdr->args,
643 .rpc_resp = &hdr->res,
46a5ab47 644 .rpc_cred = cred,
1ed26f33
AS
645 };
646 struct rpc_task_setup task_setup_data = {
647 .rpc_client = clnt,
d45f60c6 648 .task = &hdr->task,
1ed26f33
AS
649 .rpc_message = &msg,
650 .callback_ops = call_ops,
d45f60c6 651 .callback_data = hdr,
1ed26f33
AS
652 .workqueue = nfsiod_workqueue,
653 .flags = RPC_TASK_ASYNC | flags,
654 };
655 int ret = 0;
656
abde71f4 657 hdr->rw_ops->rw_initiate(hdr, &msg, rpc_ops, &task_setup_data, how);
1ed26f33 658
b4839ebe 659 dprintk("NFS: initiated pgio call "
1ed26f33 660 "(req %s/%llu, %u bytes @ offset %llu)\n",
343ae531
AS
661 hdr->inode->i_sb->s_id,
662 (unsigned long long)NFS_FILEID(hdr->inode),
d45f60c6
WAA
663 hdr->args.count,
664 (unsigned long long)hdr->args.offset);
1ed26f33
AS
665
666 task = rpc_run_task(&task_setup_data);
667 if (IS_ERR(task)) {
668 ret = PTR_ERR(task);
669 goto out;
670 }
671 if (how & FLUSH_SYNC) {
672 ret = rpc_wait_for_completion_task(task);
673 if (ret == 0)
674 ret = task->tk_status;
675 }
676 rpc_put_task(task);
677out:
678 return ret;
679}
680EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
681
844c9e69
AS
682/**
683 * nfs_pgio_error - Clean up from a pageio error
844c9e69
AS
684 * @hdr: pageio header
685 */
2bff2288 686static void nfs_pgio_error(struct nfs_pgio_header *hdr)
844c9e69 687{
844c9e69 688 set_bit(NFS_IOHDR_REDO, &hdr->flags);
4714fb51 689 hdr->completion_ops->completion(hdr);
844c9e69
AS
690}
691
a4cdda59
AS
692/**
693 * nfs_pgio_release - Release pageio data
d45f60c6 694 * @calldata: The pageio header to release
a4cdda59 695 */
6f92fa45 696static void nfs_pgio_release(void *calldata)
a4cdda59 697{
d45f60c6 698 struct nfs_pgio_header *hdr = calldata;
4714fb51 699 hdr->completion_ops->completion(hdr);
a4cdda59
AS
700}
701
a7d42ddb
WAA
702static void nfs_pageio_mirror_init(struct nfs_pgio_mirror *mirror,
703 unsigned int bsize)
704{
705 INIT_LIST_HEAD(&mirror->pg_list);
706 mirror->pg_bytes_written = 0;
707 mirror->pg_count = 0;
708 mirror->pg_bsize = bsize;
709 mirror->pg_base = 0;
710 mirror->pg_recoalesce = 0;
711}
712
1da177e4 713/**
d8a5ad75
TM
714 * nfs_pageio_init - initialise a page io descriptor
715 * @desc: pointer to descriptor
bcb71bba 716 * @inode: pointer to inode
dfad7000
YW
717 * @pg_ops: pointer to pageio operations
718 * @compl_ops: pointer to pageio completion operations
719 * @rw_ops: pointer to nfs read/write operations
bcb71bba
TM
720 * @bsize: io block size
721 * @io_flags: extra parameters for the io function
d8a5ad75 722 */
bcb71bba
TM
723void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
724 struct inode *inode,
1751c363 725 const struct nfs_pageio_ops *pg_ops,
061ae2ed 726 const struct nfs_pgio_completion_ops *compl_ops,
4a0de55c 727 const struct nfs_rw_ops *rw_ops,
84dde76c 728 size_t bsize,
3bde7afd 729 int io_flags)
d8a5ad75 730{
b31268ac 731 desc->pg_moreio = 0;
bcb71bba 732 desc->pg_inode = inode;
1751c363 733 desc->pg_ops = pg_ops;
061ae2ed 734 desc->pg_completion_ops = compl_ops;
4a0de55c 735 desc->pg_rw_ops = rw_ops;
bcb71bba
TM
736 desc->pg_ioflags = io_flags;
737 desc->pg_error = 0;
94ad1c80 738 desc->pg_lseg = NULL;
919e3bd9 739 desc->pg_io_completion = NULL;
584aa810 740 desc->pg_dreq = NULL;
a7d42ddb
WAA
741 desc->pg_bsize = bsize;
742
743 desc->pg_mirror_count = 1;
744 desc->pg_mirror_idx = 0;
745
14abcb0b
TM
746 desc->pg_mirrors_dynamic = NULL;
747 desc->pg_mirrors = desc->pg_mirrors_static;
748 nfs_pageio_mirror_init(&desc->pg_mirrors[0], bsize);
33344e0f 749 desc->pg_maxretrans = 0;
d8a5ad75
TM
750}
751
0eecb214
AS
752/**
753 * nfs_pgio_result - Basic pageio error handling
754 * @task: The task that ran
d45f60c6 755 * @calldata: Pageio header to check
0eecb214 756 */
6f92fa45 757static void nfs_pgio_result(struct rpc_task *task, void *calldata)
0eecb214 758{
d45f60c6
WAA
759 struct nfs_pgio_header *hdr = calldata;
760 struct inode *inode = hdr->inode;
0eecb214
AS
761
762 dprintk("NFS: %s: %5u, (status %d)\n", __func__,
763 task->tk_pid, task->tk_status);
764
d45f60c6 765 if (hdr->rw_ops->rw_done(task, hdr, inode) != 0)
0eecb214
AS
766 return;
767 if (task->tk_status < 0)
d45f60c6 768 nfs_set_pgio_error(hdr, task->tk_status, hdr->args.offset);
0eecb214 769 else
d45f60c6 770 hdr->rw_ops->rw_result(task, hdr);
0eecb214
AS
771}
772
ef2c488c
AS
773/*
774 * Create an RPC task for the given read or write request and kick it.
775 * The page must have been locked by the caller.
776 *
777 * It may happen that the page we're passed is not marked dirty.
778 * This is the case if nfs_updatepage detects a conflicting request
779 * that has been written but not committed.
780 */
f0cb9ab8
WAA
781int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
782 struct nfs_pgio_header *hdr)
ef2c488c 783{
48d635f1 784 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
a7d42ddb 785
ef2c488c 786 struct nfs_page *req;
bba5c188
WAA
787 struct page **pages,
788 *last_page;
a7d42ddb 789 struct list_head *head = &mirror->pg_list;
ef2c488c 790 struct nfs_commit_info cinfo;
8ef9b0b9 791 struct nfs_page_array *pg_array = &hdr->page_array;
bba5c188 792 unsigned int pagecount, pageused;
ae97aa52 793 gfp_t gfp_flags = GFP_KERNEL;
ef2c488c 794
a7d42ddb 795 pagecount = nfs_page_array_len(mirror->pg_base, mirror->pg_count);
2eb3aea7 796 pg_array->npages = pagecount;
8ef9b0b9
BC
797
798 if (pagecount <= ARRAY_SIZE(pg_array->page_array))
799 pg_array->pagevec = pg_array->page_array;
800 else {
8ef9b0b9
BC
801 pg_array->pagevec = kcalloc(pagecount, sizeof(struct page *), gfp_flags);
802 if (!pg_array->pagevec) {
803 pg_array->npages = 0;
804 nfs_pgio_error(hdr);
805 desc->pg_error = -ENOMEM;
806 return desc->pg_error;
807 }
2bff2288 808 }
ef2c488c
AS
809
810 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
d45f60c6 811 pages = hdr->page_array.pagevec;
bba5c188
WAA
812 last_page = NULL;
813 pageused = 0;
ef2c488c
AS
814 while (!list_empty(head)) {
815 req = nfs_list_entry(head->next);
078b5fd9 816 nfs_list_move_request(req, &hdr->pages);
bba5c188 817
bba5c188 818 if (!last_page || last_page != req->wb_page) {
bba5c188 819 pageused++;
b8fb9c30
TM
820 if (pageused > pagecount)
821 break;
822 *pages++ = last_page = req->wb_page;
bba5c188 823 }
ef2c488c 824 }
2bff2288
PT
825 if (WARN_ON_ONCE(pageused != pagecount)) {
826 nfs_pgio_error(hdr);
827 desc->pg_error = -EINVAL;
828 return desc->pg_error;
829 }
ef2c488c
AS
830
831 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
832 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
833 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
834
835 /* Set up the argument struct */
e545735a 836 nfs_pgio_rpcsetup(hdr, mirror->pg_count, desc->pg_ioflags, &cinfo);
ef2c488c
AS
837 desc->pg_rpc_callops = &nfs_pgio_common_ops;
838 return 0;
839}
f0cb9ab8 840EXPORT_SYMBOL_GPL(nfs_generic_pgio);
ef2c488c 841
41d8d5b7 842static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
cf485fcd 843{
cf485fcd
AS
844 struct nfs_pgio_header *hdr;
845 int ret;
846
1e7f3a48
WAA
847 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
848 if (!hdr) {
2bff2288
PT
849 desc->pg_error = -ENOMEM;
850 return desc->pg_error;
cf485fcd 851 }
1e7f3a48 852 nfs_pgheader_init(desc, hdr, nfs_pgio_header_free);
cf485fcd
AS
853 ret = nfs_generic_pgio(desc, hdr);
854 if (ret == 0)
7f714720 855 ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
46a5ab47
PT
856 hdr,
857 hdr->cred,
858 NFS_PROTO(hdr->inode),
abde71f4 859 desc->pg_rpc_callops,
7f714720 860 desc->pg_ioflags, 0);
cf485fcd
AS
861 return ret;
862}
863
14abcb0b
TM
864static struct nfs_pgio_mirror *
865nfs_pageio_alloc_mirrors(struct nfs_pageio_descriptor *desc,
866 unsigned int mirror_count)
867{
868 struct nfs_pgio_mirror *ret;
869 unsigned int i;
870
871 kfree(desc->pg_mirrors_dynamic);
872 desc->pg_mirrors_dynamic = NULL;
873 if (mirror_count == 1)
874 return desc->pg_mirrors_static;
2b17d725 875 ret = kmalloc_array(mirror_count, sizeof(*ret), GFP_KERNEL);
14abcb0b
TM
876 if (ret != NULL) {
877 for (i = 0; i < mirror_count; i++)
878 nfs_pageio_mirror_init(&ret[i], desc->pg_bsize);
879 desc->pg_mirrors_dynamic = ret;
880 }
881 return ret;
882}
883
a7d42ddb
WAA
884/*
885 * nfs_pageio_setup_mirroring - determine if mirroring is to be used
886 * by calling the pg_get_mirror_count op
887 */
14abcb0b 888static void nfs_pageio_setup_mirroring(struct nfs_pageio_descriptor *pgio,
a7d42ddb
WAA
889 struct nfs_page *req)
890{
14abcb0b 891 unsigned int mirror_count = 1;
a7d42ddb 892
14abcb0b
TM
893 if (pgio->pg_ops->pg_get_mirror_count)
894 mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req);
895 if (mirror_count == pgio->pg_mirror_count || pgio->pg_error < 0)
896 return;
a7d42ddb 897
14abcb0b
TM
898 if (!mirror_count || mirror_count > NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX) {
899 pgio->pg_error = -EINVAL;
900 return;
901 }
a7d42ddb 902
14abcb0b
TM
903 pgio->pg_mirrors = nfs_pageio_alloc_mirrors(pgio, mirror_count);
904 if (pgio->pg_mirrors == NULL) {
905 pgio->pg_error = -ENOMEM;
906 pgio->pg_mirrors = pgio->pg_mirrors_static;
907 mirror_count = 1;
908 }
a7d42ddb 909 pgio->pg_mirror_count = mirror_count;
a7d42ddb
WAA
910}
911
a7d42ddb
WAA
912static void nfs_pageio_cleanup_mirroring(struct nfs_pageio_descriptor *pgio)
913{
914 pgio->pg_mirror_count = 1;
915 pgio->pg_mirror_idx = 0;
916 pgio->pg_mirrors = pgio->pg_mirrors_static;
917 kfree(pgio->pg_mirrors_dynamic);
918 pgio->pg_mirrors_dynamic = NULL;
919}
920
4109bb74
TM
921static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
922 const struct nfs_lock_context *l2)
923{
d51fdb87 924 return l1->lockowner == l2->lockowner;
4109bb74
TM
925}
926
d8a5ad75
TM
927/**
928 * nfs_can_coalesce_requests - test two requests for compatibility
929 * @prev: pointer to nfs_page
930 * @req: pointer to nfs_page
302fad7b 931 * @pgio: pointer to nfs_pagio_descriptor
d8a5ad75
TM
932 *
933 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
934 * page data area they describe is contiguous, and that their RPC
935 * credentials, NFSv4 open state, and lockowners are the same.
936 *
937 * Return 'true' if this is the case, else return 'false'.
938 */
18ad0a9f
BH
939static bool nfs_can_coalesce_requests(struct nfs_page *prev,
940 struct nfs_page *req,
941 struct nfs_pageio_descriptor *pgio)
d8a5ad75 942{
b4fdac1a 943 size_t size;
5263e31e 944 struct file_lock_context *flctx;
b4fdac1a 945
ab75e417 946 if (prev) {
9fcd5960 947 if (!nfs_match_open_context(nfs_req_openctx(req), nfs_req_openctx(prev)))
ab75e417 948 return false;
9fcd5960 949 flctx = d_inode(nfs_req_openctx(req)->dentry)->i_flctx;
bd61e0a9
JL
950 if (flctx != NULL &&
951 !(list_empty_careful(&flctx->flc_posix) &&
952 list_empty_careful(&flctx->flc_flock)) &&
5263e31e
JL
953 !nfs_match_lock_context(req->wb_lock_context,
954 prev->wb_lock_context))
955 return false;
ab75e417
WAA
956 if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
957 return false;
78270e8f
WAA
958 if (req->wb_page == prev->wb_page) {
959 if (req->wb_pgbase != prev->wb_pgbase + prev->wb_bytes)
960 return false;
961 } else {
962 if (req->wb_pgbase != 0 ||
09cbfeaf 963 prev->wb_pgbase + prev->wb_bytes != PAGE_SIZE)
78270e8f
WAA
964 return false;
965 }
ab75e417 966 }
b4fdac1a 967 size = pgio->pg_ops->pg_test(pgio, prev, req);
f0cb9ab8
WAA
968 WARN_ON_ONCE(size > req->wb_bytes);
969 if (size && size < req->wb_bytes)
970 req->wb_bytes = size;
b4fdac1a 971 return size > 0;
d8a5ad75
TM
972}
973
974/**
bcb71bba 975 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
d8a5ad75
TM
976 * @desc: destination io descriptor
977 * @req: request
978 *
979 * Returns true if the request 'req' was successfully coalesced into the
980 * existing list of pages 'desc'.
981 */
bcb71bba
TM
982static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
983 struct nfs_page *req)
d8a5ad75 984{
48d635f1 985 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
a7d42ddb 986
ab75e417 987 struct nfs_page *prev = NULL;
a7d42ddb 988
a1f5ab56 989 if (list_empty(&mirror->pg_list)) {
d8007d4d
TM
990 if (desc->pg_ops->pg_init)
991 desc->pg_ops->pg_init(desc, req);
d600ad1f
PT
992 if (desc->pg_error < 0)
993 return 0;
a7d42ddb 994 mirror->pg_base = req->wb_pgbase;
a1f5ab56
TM
995 mirror->pg_count = 0;
996 mirror->pg_recoalesce = 0;
997 } else
998 prev = nfs_list_entry(mirror->pg_list.prev);
33344e0f
TM
999
1000 if (desc->pg_maxretrans && req->wb_nio > desc->pg_maxretrans) {
1001 if (NFS_SERVER(desc->pg_inode)->flags & NFS_MOUNT_SOFTERR)
1002 desc->pg_error = -ETIMEDOUT;
1003 else
1004 desc->pg_error = -EIO;
1005 return 0;
1006 }
1007
ab75e417
WAA
1008 if (!nfs_can_coalesce_requests(prev, req, desc))
1009 return 0;
078b5fd9 1010 nfs_list_move_request(req, &mirror->pg_list);
a7d42ddb 1011 mirror->pg_count += req->wb_bytes;
d8a5ad75
TM
1012 return 1;
1013}
1014
bcb71bba
TM
1015/*
1016 * Helper for nfs_pageio_add_request and nfs_pageio_complete
1017 */
1018static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
1019{
48d635f1 1020 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
a7d42ddb 1021
a7d42ddb 1022 if (!list_empty(&mirror->pg_list)) {
1751c363 1023 int error = desc->pg_ops->pg_doio(desc);
bcb71bba
TM
1024 if (error < 0)
1025 desc->pg_error = error;
cd2ce7fb 1026 if (list_empty(&mirror->pg_list)) {
a7d42ddb 1027 mirror->pg_bytes_written += mirror->pg_count;
cd2ce7fb
TM
1028 mirror->pg_count = 0;
1029 mirror->pg_base = 0;
1030 mirror->pg_recoalesce = 0;
1031 }
bcb71bba
TM
1032 }
1033}
1034
f57dcf4c
TM
1035static void
1036nfs_pageio_cleanup_request(struct nfs_pageio_descriptor *desc,
1037 struct nfs_page *req)
1038{
1039 LIST_HEAD(head);
1040
078b5fd9 1041 nfs_list_move_request(req, &head);
df3accb8 1042 desc->pg_completion_ops->error_cleanup(&head, desc->pg_error);
f57dcf4c
TM
1043}
1044
bcb71bba
TM
1045/**
1046 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
1047 * @desc: destination io descriptor
1048 * @req: request
1049 *
2bfc6e56
WAA
1050 * This may split a request into subrequests which are all part of the
1051 * same page group.
1052 *
bcb71bba
TM
1053 * Returns true if the request 'req' was successfully coalesced into the
1054 * existing list of pages 'desc'.
1055 */
d9156f9f 1056static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
8b09bee3 1057 struct nfs_page *req)
bcb71bba 1058{
48d635f1 1059 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
a7d42ddb 1060
2bfc6e56
WAA
1061 struct nfs_page *subreq;
1062 unsigned int bytes_left = 0;
1063 unsigned int offset, pgbase;
1064
1344b7ea 1065 nfs_page_group_lock(req);
2bfc6e56
WAA
1066
1067 subreq = req;
1068 bytes_left = subreq->wb_bytes;
1069 offset = subreq->wb_offset;
1070 pgbase = subreq->wb_pgbase;
1071
1072 do {
1073 if (!nfs_pageio_do_add_request(desc, subreq)) {
1074 /* make sure pg_test call(s) did nothing */
1075 WARN_ON_ONCE(subreq->wb_bytes != bytes_left);
1076 WARN_ON_ONCE(subreq->wb_offset != offset);
1077 WARN_ON_ONCE(subreq->wb_pgbase != pgbase);
1078
1079 nfs_page_group_unlock(req);
1080 desc->pg_moreio = 1;
1081 nfs_pageio_doio(desc);
f57dcf4c
TM
1082 if (desc->pg_error < 0 || mirror->pg_recoalesce)
1083 goto out_cleanup_subreq;
2bfc6e56 1084 /* retry add_request for this subreq */
1344b7ea 1085 nfs_page_group_lock(req);
2bfc6e56
WAA
1086 continue;
1087 }
1088
1089 /* check for buggy pg_test call(s) */
1090 WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE);
1091 WARN_ON_ONCE(subreq->wb_bytes > bytes_left);
1092 WARN_ON_ONCE(subreq->wb_bytes == 0);
1093
1094 bytes_left -= subreq->wb_bytes;
1095 offset += subreq->wb_bytes;
1096 pgbase += subreq->wb_bytes;
1097
1098 if (bytes_left) {
c917cfaf
TM
1099 subreq = nfs_create_subreq(req, subreq, pgbase,
1100 offset, bytes_left);
c1109558
TM
1101 if (IS_ERR(subreq))
1102 goto err_ptr;
2bfc6e56
WAA
1103 }
1104 } while (bytes_left > 0);
1105
1106 nfs_page_group_unlock(req);
bcb71bba 1107 return 1;
c1109558
TM
1108err_ptr:
1109 desc->pg_error = PTR_ERR(subreq);
1110 nfs_page_group_unlock(req);
1111 return 0;
f57dcf4c
TM
1112out_cleanup_subreq:
1113 if (req != subreq)
1114 nfs_pageio_cleanup_request(desc, subreq);
1115 return 0;
bcb71bba
TM
1116}
1117
d9156f9f
TM
1118static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
1119{
48d635f1 1120 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
d9156f9f
TM
1121 LIST_HEAD(head);
1122
1123 do {
a7d42ddb 1124 list_splice_init(&mirror->pg_list, &head);
a7d42ddb
WAA
1125 mirror->pg_count = 0;
1126 mirror->pg_base = 0;
1127 mirror->pg_recoalesce = 0;
1128
d9156f9f
TM
1129 while (!list_empty(&head)) {
1130 struct nfs_page *req;
1131
1132 req = list_first_entry(&head, struct nfs_page, wb_list);
d9156f9f
TM
1133 if (__nfs_pageio_add_request(desc, req))
1134 continue;
03d5eb65
TM
1135 if (desc->pg_error < 0) {
1136 list_splice_tail(&head, &mirror->pg_list);
1137 mirror->pg_recoalesce = 1;
d9156f9f 1138 return 0;
03d5eb65 1139 }
d9156f9f
TM
1140 break;
1141 }
a7d42ddb 1142 } while (mirror->pg_recoalesce);
d9156f9f
TM
1143 return 1;
1144}
1145
a7d42ddb 1146static int nfs_pageio_add_request_mirror(struct nfs_pageio_descriptor *desc,
d9156f9f
TM
1147 struct nfs_page *req)
1148{
1149 int ret;
1150
1151 do {
1152 ret = __nfs_pageio_add_request(desc, req);
1153 if (ret)
1154 break;
1155 if (desc->pg_error < 0)
1156 break;
1157 ret = nfs_do_recoalesce(desc);
1158 } while (ret);
a7d42ddb 1159
d9156f9f
TM
1160 return ret;
1161}
1162
fdbd1a2e
BC
1163static void nfs_pageio_error_cleanup(struct nfs_pageio_descriptor *desc)
1164{
1165 u32 midx;
1166 struct nfs_pgio_mirror *mirror;
1167
1168 if (!desc->pg_error)
1169 return;
1170
1171 for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1172 mirror = &desc->pg_mirrors[midx];
df3accb8
TM
1173 desc->pg_completion_ops->error_cleanup(&mirror->pg_list,
1174 desc->pg_error);
fdbd1a2e
BC
1175 }
1176}
1177
a7d42ddb
WAA
1178int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
1179 struct nfs_page *req)
1180{
1181 u32 midx;
1182 unsigned int pgbase, offset, bytes;
1183 struct nfs_page *dupreq, *lastreq;
1184
1185 pgbase = req->wb_pgbase;
1186 offset = req->wb_offset;
1187 bytes = req->wb_bytes;
1188
1189 nfs_pageio_setup_mirroring(desc, req);
d600ad1f 1190 if (desc->pg_error < 0)
c18b96a1 1191 goto out_failed;
a7d42ddb 1192
ae96b56f
TM
1193 /* Create the mirror instances first, and fire them off */
1194 for (midx = 1; midx < desc->pg_mirror_count; midx++) {
1195 nfs_page_group_lock(req);
1196
1197 /* find the last request */
1198 for (lastreq = req->wb_head;
1199 lastreq->wb_this_page != req->wb_head;
1200 lastreq = lastreq->wb_this_page)
1201 ;
1202
1203 dupreq = nfs_create_subreq(req, lastreq,
1204 pgbase, offset, bytes);
1205
1206 nfs_page_group_unlock(req);
1207 if (IS_ERR(dupreq)) {
1208 desc->pg_error = PTR_ERR(dupreq);
1209 goto out_failed;
1210 }
a7d42ddb 1211
ae96b56f 1212 desc->pg_mirror_idx = midx;
a7d42ddb 1213 if (!nfs_pageio_add_request_mirror(desc, dupreq))
f57dcf4c 1214 goto out_cleanup_subreq;
a7d42ddb
WAA
1215 }
1216
ae96b56f
TM
1217 desc->pg_mirror_idx = 0;
1218 if (!nfs_pageio_add_request_mirror(desc, req))
1219 goto out_failed;
1220
a7d42ddb 1221 return 1;
c18b96a1 1222
f57dcf4c 1223out_cleanup_subreq:
ae96b56f 1224 nfs_pageio_cleanup_request(desc, dupreq);
c18b96a1 1225out_failed:
fdbd1a2e 1226 nfs_pageio_error_cleanup(desc);
c18b96a1 1227 return 0;
a7d42ddb
WAA
1228}
1229
1230/*
1231 * nfs_pageio_complete_mirror - Complete I/O on the current mirror of an
1232 * nfs_pageio_descriptor
1233 * @desc: pointer to io descriptor
dfad7000 1234 * @mirror_idx: pointer to mirror index
a7d42ddb
WAA
1235 */
1236static void nfs_pageio_complete_mirror(struct nfs_pageio_descriptor *desc,
1237 u32 mirror_idx)
1238{
1239 struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[mirror_idx];
1240 u32 restore_idx = desc->pg_mirror_idx;
1241
47af81f2
PT
1242 if (nfs_pgio_has_mirroring(desc))
1243 desc->pg_mirror_idx = mirror_idx;
a7d42ddb
WAA
1244 for (;;) {
1245 nfs_pageio_doio(desc);
8127d827 1246 if (desc->pg_error < 0 || !mirror->pg_recoalesce)
a7d42ddb
WAA
1247 break;
1248 if (!nfs_do_recoalesce(desc))
1249 break;
1250 }
1251 desc->pg_mirror_idx = restore_idx;
1252}
1253
53113ad3
WAA
1254/*
1255 * nfs_pageio_resend - Transfer requests to new descriptor and resend
1256 * @hdr - the pgio header to move request from
1257 * @desc - the pageio descriptor to add requests to
1258 *
1259 * Try to move each request (nfs_page) from @hdr to @desc then attempt
1260 * to send them.
1261 *
1262 * Returns 0 on success and < 0 on error.
1263 */
1264int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
1265 struct nfs_pgio_header *hdr)
1266{
f4340e93 1267 LIST_HEAD(pages);
53113ad3 1268
919e3bd9 1269 desc->pg_io_completion = hdr->io_completion;
53113ad3 1270 desc->pg_dreq = hdr->dreq;
f4340e93
TM
1271 list_splice_init(&hdr->pages, &pages);
1272 while (!list_empty(&pages)) {
1273 struct nfs_page *req = nfs_list_entry(pages.next);
53113ad3 1274
53113ad3 1275 if (!nfs_pageio_add_request(desc, req))
f4340e93 1276 break;
53113ad3
WAA
1277 }
1278 nfs_pageio_complete(desc);
f4340e93
TM
1279 if (!list_empty(&pages)) {
1280 int err = desc->pg_error < 0 ? desc->pg_error : -EIO;
1281 hdr->completion_ops->error_cleanup(&pages, err);
eb2c50da 1282 nfs_set_pgio_error(hdr, err, hdr->io_start);
f4340e93 1283 return err;
53113ad3
WAA
1284 }
1285 return 0;
1286}
1287EXPORT_SYMBOL_GPL(nfs_pageio_resend);
d9156f9f 1288
bcb71bba 1289/**
2176bf42 1290 * nfs_pageio_complete - Complete I/O then cleanup an nfs_pageio_descriptor
bcb71bba
TM
1291 * @desc: pointer to io descriptor
1292 */
1293void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
1294{
a7d42ddb
WAA
1295 u32 midx;
1296
1297 for (midx = 0; midx < desc->pg_mirror_count; midx++)
1298 nfs_pageio_complete_mirror(desc, midx);
2176bf42 1299
fdbd1a2e
BC
1300 if (desc->pg_error < 0)
1301 nfs_pageio_error_cleanup(desc);
2176bf42
WAA
1302 if (desc->pg_ops->pg_cleanup)
1303 desc->pg_ops->pg_cleanup(desc);
a7d42ddb 1304 nfs_pageio_cleanup_mirroring(desc);
bcb71bba
TM
1305}
1306
7fe7f848
TM
1307/**
1308 * nfs_pageio_cond_complete - Conditional I/O completion
1309 * @desc: pointer to io descriptor
1310 * @index: page index
1311 *
1312 * It is important to ensure that processes don't try to take locks
1313 * on non-contiguous ranges of pages as that might deadlock. This
1314 * function should be called before attempting to wait on a locked
1315 * nfs_page. It will complete the I/O if the page index 'index'
1316 * is not contiguous with the existing list of pages in 'desc'.
1317 */
1318void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
1319{
a7d42ddb
WAA
1320 struct nfs_pgio_mirror *mirror;
1321 struct nfs_page *prev;
1322 u32 midx;
1323
1324 for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1325 mirror = &desc->pg_mirrors[midx];
1326 if (!list_empty(&mirror->pg_list)) {
1327 prev = nfs_list_entry(mirror->pg_list.prev);
43b7d964
BC
1328 if (index != prev->wb_index + 1) {
1329 nfs_pageio_complete(desc);
1330 break;
1331 }
a7d42ddb 1332 }
7fe7f848
TM
1333 }
1334}
1335
e3e8cd13
TM
1336/*
1337 * nfs_pageio_stop_mirroring - stop using mirroring (set mirror count to 1)
1338 */
1339void nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor *pgio)
1340{
1341 nfs_pageio_complete(pgio);
1342}
1343
f7b422b1 1344int __init nfs_init_nfspagecache(void)
1da177e4
LT
1345{
1346 nfs_page_cachep = kmem_cache_create("nfs_page",
1347 sizeof(struct nfs_page),
1348 0, SLAB_HWCACHE_ALIGN,
20c2df83 1349 NULL);
1da177e4
LT
1350 if (nfs_page_cachep == NULL)
1351 return -ENOMEM;
1352
1353 return 0;
1354}
1355
266bee88 1356void nfs_destroy_nfspagecache(void)
1da177e4 1357{
1a1d92c1 1358 kmem_cache_destroy(nfs_page_cachep);
1da177e4
LT
1359}
1360
ef2c488c 1361static const struct rpc_call_ops nfs_pgio_common_ops = {
6f92fa45
AS
1362 .rpc_call_prepare = nfs_pgio_prepare,
1363 .rpc_call_done = nfs_pgio_result,
1364 .rpc_release = nfs_pgio_release,
1365};
41d8d5b7
AS
1366
1367const struct nfs_pageio_ops nfs_pgio_rw_ops = {
1368 .pg_test = nfs_generic_pg_test,
1369 .pg_doio = nfs_generic_pg_pgios,
1370};