1 // SPDX-License-Identifier: GPL-2.0-only
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
13 #include <linux/slab.h>
14 #include <linux/file.h>
15 #include <linux/sched.h>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/nfs.h>
18 #include <linux/nfs3.h>
19 #include <linux/nfs4.h>
20 #include <linux/nfs_fs.h>
21 #include <linux/nfs_page.h>
22 #include <linux/nfs_mount.h>
23 #include <linux/export.h>
24
25 #include "internal.h"
26 #include "pnfs.h"
27
28 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
29
30 static struct kmem_cache *nfs_page_cachep;
31 static const struct rpc_call_ops nfs_pgio_common_ops;
32
33 struct nfs_pgio_mirror *
nfs_pgio_current_mirror(struct nfs_pageio_descriptor * desc)34 nfs_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 }
40 EXPORT_SYMBOL_GPL(nfs_pgio_current_mirror);
41
nfs_pgheader_init(struct nfs_pageio_descriptor * desc,struct nfs_pgio_header * hdr,void (* release)(struct nfs_pgio_header * hdr))42 void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
43 struct nfs_pgio_header *hdr,
44 void (*release)(struct nfs_pgio_header *hdr))
45 {
46 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
47
48
49 hdr->req = nfs_list_entry(mirror->pg_list.next);
50 hdr->inode = desc->pg_inode;
51 hdr->cred = nfs_req_openctx(hdr->req)->cred;
52 hdr->io_start = req_offset(hdr->req);
53 hdr->good_bytes = mirror->pg_count;
54 hdr->io_completion = desc->pg_io_completion;
55 hdr->dreq = desc->pg_dreq;
56 hdr->release = release;
57 hdr->completion_ops = desc->pg_completion_ops;
58 if (hdr->completion_ops->init_hdr)
59 hdr->completion_ops->init_hdr(hdr);
60
61 hdr->pgio_mirror_idx = desc->pg_mirror_idx;
62 }
63 EXPORT_SYMBOL_GPL(nfs_pgheader_init);
64
nfs_set_pgio_error(struct nfs_pgio_header * hdr,int error,loff_t pos)65 void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos)
66 {
67 unsigned int new = pos - hdr->io_start;
68
69 if (hdr->good_bytes > new) {
70 hdr->good_bytes = new;
71 clear_bit(NFS_IOHDR_EOF, &hdr->flags);
72 if (!test_and_set_bit(NFS_IOHDR_ERROR, &hdr->flags))
73 hdr->error = error;
74 }
75 }
76
77 static inline struct nfs_page *
nfs_page_alloc(void)78 nfs_page_alloc(void)
79 {
80 struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_KERNEL);
81 if (p)
82 INIT_LIST_HEAD(&p->wb_list);
83 return p;
84 }
85
86 static inline void
nfs_page_free(struct nfs_page * p)87 nfs_page_free(struct nfs_page *p)
88 {
89 kmem_cache_free(nfs_page_cachep, p);
90 }
91
92 /**
93 * nfs_iocounter_wait - wait for i/o to complete
94 * @l_ctx: nfs_lock_context with io_counter to use
95 *
96 * returns -ERESTARTSYS if interrupted by a fatal signal.
97 * Otherwise returns 0 once the io_count hits 0.
98 */
99 int
nfs_iocounter_wait(struct nfs_lock_context * l_ctx)100 nfs_iocounter_wait(struct nfs_lock_context *l_ctx)
101 {
102 return wait_var_event_killable(&l_ctx->io_count,
103 !atomic_read(&l_ctx->io_count));
104 }
105
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 */
115 bool
nfs_async_iocounter_wait(struct rpc_task * task,struct nfs_lock_context * l_ctx)116 nfs_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 }
133 EXPORT_SYMBOL_GPL(nfs_async_iocounter_wait);
134
135 /*
136 * nfs_page_set_headlock - set the request PG_HEADLOCK
137 * @req: request that is to be locked
138 *
139 * this lock must be held when modifying req->wb_head
140 *
141 * return 0 on success, < 0 on error
142 */
143 int
nfs_page_set_headlock(struct nfs_page * req)144 nfs_page_set_headlock(struct nfs_page *req)
145 {
146 if (!test_and_set_bit(PG_HEADLOCK, &req->wb_flags))
147 return 0;
148
149 set_bit(PG_CONTENDED1, &req->wb_flags);
150 smp_mb__after_atomic();
151 return wait_on_bit_lock(&req->wb_flags, PG_HEADLOCK,
152 TASK_UNINTERRUPTIBLE);
153 }
154
155 /*
156 * nfs_page_clear_headlock - clear the request PG_HEADLOCK
157 * @req: request that is to be locked
158 */
159 void
nfs_page_clear_headlock(struct nfs_page * req)160 nfs_page_clear_headlock(struct nfs_page *req)
161 {
162 smp_mb__before_atomic();
163 clear_bit(PG_HEADLOCK, &req->wb_flags);
164 smp_mb__after_atomic();
165 if (!test_bit(PG_CONTENDED1, &req->wb_flags))
166 return;
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 */
179 int
nfs_page_group_lock(struct nfs_page * req)180 nfs_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 */
194 void
nfs_page_group_unlock(struct nfs_page * req)195 nfs_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);
200 }
201
202 /*
203 * nfs_page_group_sync_on_bit_locked
204 *
205 * must be called with page group lock held
206 */
207 static bool
nfs_page_group_sync_on_bit_locked(struct nfs_page * req,unsigned int bit)208 nfs_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 */
nfs_page_group_sync_on_bit(struct nfs_page * req,unsigned int bit)239 bool nfs_page_group_sync_on_bit(struct nfs_page *req, unsigned int bit)
240 {
241 bool ret;
242
243 nfs_page_group_lock(req);
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 */
256 static inline void
nfs_page_group_init(struct nfs_page * req,struct nfs_page * prev)257 nfs_page_group_init(struct nfs_page *req, struct nfs_page *prev)
258 {
259 struct inode *inode;
260 WARN_ON_ONCE(prev == req);
261
262 if (!prev) {
263 /* a head request */
264 req->wb_head = req;
265 req->wb_this_page = req;
266 } else {
267 /* a subrequest */
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
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
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. */
281 if (test_bit(PG_INODE_REF, &prev->wb_head->wb_flags)) {
282 inode = page_file_mapping(req->wb_page)->host;
283 set_bit(PG_INODE_REF, &req->wb_flags);
284 kref_get(&req->wb_kref);
285 atomic_long_inc(&NFS_I(inode)->nrequests);
286 }
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 */
297 static void
nfs_page_group_destroy(struct kref * kref)298 nfs_page_group_destroy(struct kref *kref)
299 {
300 struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
301 struct nfs_page *head = req->wb_head;
302 struct nfs_page *tmp, *next;
303
304 if (!nfs_page_group_sync_on_bit(req, PG_TEARDOWN))
305 goto out;
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);
316 out:
317 /* subrequests must release the ref on the head request */
318 if (head != req)
319 nfs_release_request(head);
320 }
321
322 static struct nfs_page *
__nfs_create_request(struct nfs_lock_context * l_ctx,struct page * page,unsigned int pgbase,unsigned int offset,unsigned int count)323 __nfs_create_request(struct nfs_lock_context *l_ctx, struct page *page,
324 unsigned int pgbase, unsigned int offset,
325 unsigned int count)
326 {
327 struct nfs_page *req;
328 struct nfs_open_context *ctx = l_ctx->open_context;
329
330 if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
331 return ERR_PTR(-EBADF);
332 /* try to allocate the request struct */
333 req = nfs_page_alloc();
334 if (req == NULL)
335 return ERR_PTR(-ENOMEM);
336
337 req->wb_lock_context = l_ctx;
338 refcount_inc(&l_ctx->count);
339 atomic_inc(&l_ctx->io_count);
340
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;
345 if (page) {
346 req->wb_index = page_index(page);
347 get_page(page);
348 }
349 req->wb_offset = offset;
350 req->wb_pgbase = pgbase;
351 req->wb_bytes = count;
352 kref_init(&req->wb_kref);
353 req->wb_nio = 0;
354 return req;
355 }
356
357 /**
358 * nfs_create_request - Create an NFS read/write request.
359 * @ctx: open context to use
360 * @page: page to write
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 */
368 struct nfs_page *
nfs_create_request(struct nfs_open_context * ctx,struct page * page,unsigned int offset,unsigned int count)369 nfs_create_request(struct nfs_open_context *ctx, struct page *page,
370 unsigned int offset, unsigned int count)
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);
377 ret = __nfs_create_request(l_ctx, page, offset, offset, count);
378 if (!IS_ERR(ret))
379 nfs_page_group_init(ret, NULL);
380 nfs_put_lock_context(l_ctx);
381 return ret;
382 }
383
384 static struct nfs_page *
nfs_create_subreq(struct nfs_page * req,struct nfs_page * last,unsigned int pgbase,unsigned int offset,unsigned int count)385 nfs_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
391 ret = __nfs_create_request(req->wb_lock_context, req->wb_page,
392 pgbase, offset, count);
393 if (!IS_ERR(ret)) {
394 nfs_lock_request(ret);
395 ret->wb_index = req->wb_index;
396 nfs_page_group_init(ret, last);
397 ret->wb_nio = req->wb_nio;
398 }
399 return ret;
400 }
401
402 /**
403 * nfs_unlock_request - Unlock request and wake up sleepers.
404 * @req: pointer to request
405 */
nfs_unlock_request(struct nfs_page * req)406 void nfs_unlock_request(struct nfs_page *req)
407 {
408 if (!NFS_WBACK_BUSY(req)) {
409 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
410 BUG();
411 }
412 smp_mb__before_atomic();
413 clear_bit(PG_BUSY, &req->wb_flags);
414 smp_mb__after_atomic();
415 if (!test_bit(PG_CONTENDED2, &req->wb_flags))
416 return;
417 wake_up_bit(&req->wb_flags, PG_BUSY);
418 }
419
420 /**
421 * nfs_unlock_and_release_request - Unlock request and release the nfs_page
422 * @req: pointer to request
423 */
nfs_unlock_and_release_request(struct nfs_page * req)424 void nfs_unlock_and_release_request(struct nfs_page *req)
425 {
426 nfs_unlock_request(req);
427 nfs_release_request(req);
428 }
429
430 /*
431 * nfs_clear_request - Free up all resources allocated to the request
432 * @req:
433 *
434 * Release page and open context resources associated with a read/write
435 * request after it has completed.
436 */
nfs_clear_request(struct nfs_page * req)437 static void nfs_clear_request(struct nfs_page *req)
438 {
439 struct page *page = req->wb_page;
440 struct nfs_lock_context *l_ctx = req->wb_lock_context;
441 struct nfs_open_context *ctx;
442
443 if (page != NULL) {
444 put_page(page);
445 req->wb_page = NULL;
446 }
447 if (l_ctx != NULL) {
448 if (atomic_dec_and_test(&l_ctx->io_count)) {
449 wake_up_var(&l_ctx->io_count);
450 ctx = l_ctx->open_context;
451 if (test_bit(NFS_CONTEXT_UNLOCK, &ctx->flags))
452 rpc_wake_up(&NFS_SERVER(d_inode(ctx->dentry))->uoc_rpcwaitq);
453 }
454 nfs_put_lock_context(l_ctx);
455 req->wb_lock_context = NULL;
456 }
457 }
458
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 */
nfs_free_request(struct nfs_page * req)465 void nfs_free_request(struct nfs_page *req)
466 {
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));
471 WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags));
472 WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags));
473 WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags));
474 WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags));
475
476 /* Release struct file and open context */
477 nfs_clear_request(req);
478 nfs_page_free(req);
479 }
480
nfs_release_request(struct nfs_page * req)481 void nfs_release_request(struct nfs_page *req)
482 {
483 kref_put(&req->wb_kref, nfs_page_group_destroy);
484 }
485 EXPORT_SYMBOL_GPL(nfs_release_request);
486
487 /**
488 * nfs_wait_on_request - Wait for a request to complete.
489 * @req: request to wait upon.
490 *
491 * Interruptible by fatal signals only.
492 * The user is responsible for holding a count on the request.
493 */
494 int
nfs_wait_on_request(struct nfs_page * req)495 nfs_wait_on_request(struct nfs_page *req)
496 {
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();
501 return wait_on_bit_io(&req->wb_flags, PG_BUSY,
502 TASK_UNINTERRUPTIBLE);
503 }
504 EXPORT_SYMBOL_GPL(nfs_wait_on_request);
505
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 *
512 * Returns zero if @req cannot be coalesced into @desc, otherwise it returns
513 * the size of the request.
514 */
nfs_generic_pg_test(struct nfs_pageio_descriptor * desc,struct nfs_page * prev,struct nfs_page * req)515 size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc,
516 struct nfs_page *prev, struct nfs_page *req)
517 {
518 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
519
520
521 if (mirror->pg_count > mirror->pg_bsize) {
522 /* should never happen */
523 WARN_ON_ONCE(1);
524 return 0;
525 }
526
527 /*
528 * Limit the request size so that we can still allocate a page array
529 * for it without upsetting the slab allocator.
530 */
531 if (((mirror->pg_count + req->wb_bytes) >> PAGE_SHIFT) *
532 sizeof(struct page *) > PAGE_SIZE)
533 return 0;
534
535 return min(mirror->pg_bsize - mirror->pg_count, (size_t)req->wb_bytes);
536 }
537 EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
538
nfs_pgio_header_alloc(const struct nfs_rw_ops * ops)539 struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *ops)
540 {
541 struct nfs_pgio_header *hdr = ops->rw_alloc_header();
542
543 if (hdr) {
544 INIT_LIST_HEAD(&hdr->pages);
545 hdr->rw_ops = ops;
546 }
547 return hdr;
548 }
549 EXPORT_SYMBOL_GPL(nfs_pgio_header_alloc);
550
551 /**
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
558 */
nfs_pgio_data_destroy(struct nfs_pgio_header * hdr)559 static void nfs_pgio_data_destroy(struct nfs_pgio_header *hdr)
560 {
561 if (hdr->args.context)
562 put_nfs_open_context(hdr->args.context);
563 if (hdr->page_array.pagevec != hdr->page_array.page_array)
564 kfree(hdr->page_array.pagevec);
565 }
566
567 /*
568 * nfs_pgio_header_free - Free a read or write header
569 * @hdr: The header to free
570 */
nfs_pgio_header_free(struct nfs_pgio_header * hdr)571 void 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 }
576 EXPORT_SYMBOL_GPL(nfs_pgio_header_free);
577
578 /**
579 * nfs_pgio_rpcsetup - Set up arguments for a pageio call
580 * @hdr: The pageio hdr
581 * @count: Number of bytes to read
582 * @how: How to commit data (writes only)
583 * @cinfo: Commit information for the call (writes only)
584 */
nfs_pgio_rpcsetup(struct nfs_pgio_header * hdr,unsigned int count,int how,struct nfs_commit_info * cinfo)585 static void nfs_pgio_rpcsetup(struct nfs_pgio_header *hdr,
586 unsigned int count,
587 int how, struct nfs_commit_info *cinfo)
588 {
589 struct nfs_page *req = hdr->req;
590
591 /* Set up the RPC argument and reply structs
592 * NB: take care not to mess about with hdr->commit et al. */
593
594 hdr->args.fh = NFS_FH(hdr->inode);
595 hdr->args.offset = req_offset(req);
596 /* pnfs_set_layoutcommit needs this */
597 hdr->mds_offset = hdr->args.offset;
598 hdr->args.pgbase = req->wb_pgbase;
599 hdr->args.pages = hdr->page_array.pagevec;
600 hdr->args.count = count;
601 hdr->args.context = get_nfs_open_context(nfs_req_openctx(req));
602 hdr->args.lock_context = req->wb_lock_context;
603 hdr->args.stable = NFS_UNSTABLE;
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;
610 /* fall through */
611 default:
612 hdr->args.stable = NFS_FILE_SYNC;
613 }
614
615 hdr->res.fattr = &hdr->fattr;
616 hdr->res.count = 0;
617 hdr->res.eof = 0;
618 hdr->res.verf = &hdr->verf;
619 nfs_fattr_init(&hdr->fattr);
620 }
621
622 /**
623 * nfs_pgio_prepare - Prepare pageio hdr to go over the wire
624 * @task: The current task
625 * @calldata: pageio header to prepare
626 */
nfs_pgio_prepare(struct rpc_task * task,void * calldata)627 static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
628 {
629 struct nfs_pgio_header *hdr = calldata;
630 int err;
631 err = NFS_PROTO(hdr->inode)->pgio_rpc_prepare(task, hdr);
632 if (err)
633 rpc_exit(task, err);
634 }
635
nfs_initiate_pgio(struct rpc_clnt * clnt,struct nfs_pgio_header * hdr,const struct cred * cred,const struct nfs_rpc_ops * rpc_ops,const struct rpc_call_ops * call_ops,int how,int flags)636 int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr,
637 const struct cred *cred, const struct nfs_rpc_ops *rpc_ops,
638 const struct rpc_call_ops *call_ops, int how, int flags)
639 {
640 struct rpc_task *task;
641 struct rpc_message msg = {
642 .rpc_argp = &hdr->args,
643 .rpc_resp = &hdr->res,
644 .rpc_cred = cred,
645 };
646 struct rpc_task_setup task_setup_data = {
647 .rpc_client = clnt,
648 .task = &hdr->task,
649 .rpc_message = &msg,
650 .callback_ops = call_ops,
651 .callback_data = hdr,
652 .workqueue = nfsiod_workqueue,
653 .flags = RPC_TASK_ASYNC | flags,
654 };
655 int ret = 0;
656
657 hdr->rw_ops->rw_initiate(hdr, &msg, rpc_ops, &task_setup_data, how);
658
659 dprintk("NFS: initiated pgio call "
660 "(req %s/%llu, %u bytes @ offset %llu)\n",
661 hdr->inode->i_sb->s_id,
662 (unsigned long long)NFS_FILEID(hdr->inode),
663 hdr->args.count,
664 (unsigned long long)hdr->args.offset);
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);
677 out:
678 return ret;
679 }
680 EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
681
682 /**
683 * nfs_pgio_error - Clean up from a pageio error
684 * @hdr: pageio header
685 */
nfs_pgio_error(struct nfs_pgio_header * hdr)686 static void nfs_pgio_error(struct nfs_pgio_header *hdr)
687 {
688 set_bit(NFS_IOHDR_REDO, &hdr->flags);
689 hdr->completion_ops->completion(hdr);
690 }
691
692 /**
693 * nfs_pgio_release - Release pageio data
694 * @calldata: The pageio header to release
695 */
nfs_pgio_release(void * calldata)696 static void nfs_pgio_release(void *calldata)
697 {
698 struct nfs_pgio_header *hdr = calldata;
699 hdr->completion_ops->completion(hdr);
700 }
701
nfs_pageio_mirror_init(struct nfs_pgio_mirror * mirror,unsigned int bsize)702 static 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
713 /**
714 * nfs_pageio_init - initialise a page io descriptor
715 * @desc: pointer to descriptor
716 * @inode: pointer to inode
717 * @pg_ops: pointer to pageio operations
718 * @compl_ops: pointer to pageio completion operations
719 * @rw_ops: pointer to nfs read/write operations
720 * @bsize: io block size
721 * @io_flags: extra parameters for the io function
722 */
nfs_pageio_init(struct nfs_pageio_descriptor * desc,struct inode * inode,const struct nfs_pageio_ops * pg_ops,const struct nfs_pgio_completion_ops * compl_ops,const struct nfs_rw_ops * rw_ops,size_t bsize,int io_flags)723 void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
724 struct inode *inode,
725 const struct nfs_pageio_ops *pg_ops,
726 const struct nfs_pgio_completion_ops *compl_ops,
727 const struct nfs_rw_ops *rw_ops,
728 size_t bsize,
729 int io_flags)
730 {
731 desc->pg_moreio = 0;
732 desc->pg_inode = inode;
733 desc->pg_ops = pg_ops;
734 desc->pg_completion_ops = compl_ops;
735 desc->pg_rw_ops = rw_ops;
736 desc->pg_ioflags = io_flags;
737 desc->pg_error = 0;
738 desc->pg_lseg = NULL;
739 desc->pg_io_completion = NULL;
740 desc->pg_dreq = NULL;
741 desc->pg_bsize = bsize;
742
743 desc->pg_mirror_count = 1;
744 desc->pg_mirror_idx = 0;
745
746 desc->pg_mirrors_dynamic = NULL;
747 desc->pg_mirrors = desc->pg_mirrors_static;
748 nfs_pageio_mirror_init(&desc->pg_mirrors[0], bsize);
749 desc->pg_maxretrans = 0;
750 }
751
752 /**
753 * nfs_pgio_result - Basic pageio error handling
754 * @task: The task that ran
755 * @calldata: Pageio header to check
756 */
nfs_pgio_result(struct rpc_task * task,void * calldata)757 static void nfs_pgio_result(struct rpc_task *task, void *calldata)
758 {
759 struct nfs_pgio_header *hdr = calldata;
760 struct inode *inode = hdr->inode;
761
762 dprintk("NFS: %s: %5u, (status %d)\n", __func__,
763 task->tk_pid, task->tk_status);
764
765 if (hdr->rw_ops->rw_done(task, hdr, inode) != 0)
766 return;
767 if (task->tk_status < 0)
768 nfs_set_pgio_error(hdr, task->tk_status, hdr->args.offset);
769 else
770 hdr->rw_ops->rw_result(task, hdr);
771 }
772
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 */
nfs_generic_pgio(struct nfs_pageio_descriptor * desc,struct nfs_pgio_header * hdr)781 int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
782 struct nfs_pgio_header *hdr)
783 {
784 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
785
786 struct nfs_page *req;
787 struct page **pages,
788 *last_page;
789 struct list_head *head = &mirror->pg_list;
790 struct nfs_commit_info cinfo;
791 struct nfs_page_array *pg_array = &hdr->page_array;
792 unsigned int pagecount, pageused;
793 gfp_t gfp_flags = GFP_KERNEL;
794
795 pagecount = nfs_page_array_len(mirror->pg_base, mirror->pg_count);
796 pg_array->npages = pagecount;
797
798 if (pagecount <= ARRAY_SIZE(pg_array->page_array))
799 pg_array->pagevec = pg_array->page_array;
800 else {
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 }
808 }
809
810 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
811 pages = hdr->page_array.pagevec;
812 last_page = NULL;
813 pageused = 0;
814 while (!list_empty(head)) {
815 req = nfs_list_entry(head->next);
816 nfs_list_move_request(req, &hdr->pages);
817
818 if (!last_page || last_page != req->wb_page) {
819 pageused++;
820 if (pageused > pagecount)
821 break;
822 *pages++ = last_page = req->wb_page;
823 }
824 }
825 if (WARN_ON_ONCE(pageused != pagecount)) {
826 nfs_pgio_error(hdr);
827 desc->pg_error = -EINVAL;
828 return desc->pg_error;
829 }
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 */
836 nfs_pgio_rpcsetup(hdr, mirror->pg_count, desc->pg_ioflags, &cinfo);
837 desc->pg_rpc_callops = &nfs_pgio_common_ops;
838 return 0;
839 }
840 EXPORT_SYMBOL_GPL(nfs_generic_pgio);
841
nfs_generic_pg_pgios(struct nfs_pageio_descriptor * desc)842 static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
843 {
844 struct nfs_pgio_header *hdr;
845 int ret;
846
847 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
848 if (!hdr) {
849 desc->pg_error = -ENOMEM;
850 return desc->pg_error;
851 }
852 nfs_pgheader_init(desc, hdr, nfs_pgio_header_free);
853 ret = nfs_generic_pgio(desc, hdr);
854 if (ret == 0)
855 ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
856 hdr,
857 hdr->cred,
858 NFS_PROTO(hdr->inode),
859 desc->pg_rpc_callops,
860 desc->pg_ioflags, 0);
861 return ret;
862 }
863
864 static struct nfs_pgio_mirror *
nfs_pageio_alloc_mirrors(struct nfs_pageio_descriptor * desc,unsigned int mirror_count)865 nfs_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;
875 ret = kmalloc_array(mirror_count, sizeof(*ret), GFP_KERNEL);
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
884 /*
885 * nfs_pageio_setup_mirroring - determine if mirroring is to be used
886 * by calling the pg_get_mirror_count op
887 */
nfs_pageio_setup_mirroring(struct nfs_pageio_descriptor * pgio,struct nfs_page * req)888 static void nfs_pageio_setup_mirroring(struct nfs_pageio_descriptor *pgio,
889 struct nfs_page *req)
890 {
891 unsigned int mirror_count = 1;
892
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;
897
898 if (!mirror_count || mirror_count > NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX) {
899 pgio->pg_error = -EINVAL;
900 return;
901 }
902
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 }
909 pgio->pg_mirror_count = mirror_count;
910 }
911
nfs_pageio_cleanup_mirroring(struct nfs_pageio_descriptor * pgio)912 static 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
nfs_match_lock_context(const struct nfs_lock_context * l1,const struct nfs_lock_context * l2)921 static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
922 const struct nfs_lock_context *l2)
923 {
924 return l1->lockowner == l2->lockowner;
925 }
926
927 /**
928 * nfs_can_coalesce_requests - test two requests for compatibility
929 * @prev: pointer to nfs_page
930 * @req: pointer to nfs_page
931 * @pgio: pointer to nfs_pagio_descriptor
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 */
nfs_can_coalesce_requests(struct nfs_page * prev,struct nfs_page * req,struct nfs_pageio_descriptor * pgio)939 static bool nfs_can_coalesce_requests(struct nfs_page *prev,
940 struct nfs_page *req,
941 struct nfs_pageio_descriptor *pgio)
942 {
943 size_t size;
944 struct file_lock_context *flctx;
945
946 if (prev) {
947 if (!nfs_match_open_context(nfs_req_openctx(req), nfs_req_openctx(prev)))
948 return false;
949 flctx = d_inode(nfs_req_openctx(req)->dentry)->i_flctx;
950 if (flctx != NULL &&
951 !(list_empty_careful(&flctx->flc_posix) &&
952 list_empty_careful(&flctx->flc_flock)) &&
953 !nfs_match_lock_context(req->wb_lock_context,
954 prev->wb_lock_context))
955 return false;
956 if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
957 return false;
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 ||
963 prev->wb_pgbase + prev->wb_bytes != PAGE_SIZE)
964 return false;
965 }
966 }
967 size = pgio->pg_ops->pg_test(pgio, prev, req);
968 WARN_ON_ONCE(size > req->wb_bytes);
969 if (size && size < req->wb_bytes)
970 req->wb_bytes = size;
971 return size > 0;
972 }
973
974 /**
975 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
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 */
nfs_pageio_do_add_request(struct nfs_pageio_descriptor * desc,struct nfs_page * req)982 static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
983 struct nfs_page *req)
984 {
985 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
986
987 struct nfs_page *prev = NULL;
988
989 if (list_empty(&mirror->pg_list)) {
990 if (desc->pg_ops->pg_init)
991 desc->pg_ops->pg_init(desc, req);
992 if (desc->pg_error < 0)
993 return 0;
994 mirror->pg_base = req->wb_pgbase;
995 mirror->pg_count = 0;
996 mirror->pg_recoalesce = 0;
997 } else
998 prev = nfs_list_entry(mirror->pg_list.prev);
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
1008 if (!nfs_can_coalesce_requests(prev, req, desc))
1009 return 0;
1010 nfs_list_move_request(req, &mirror->pg_list);
1011 mirror->pg_count += req->wb_bytes;
1012 return 1;
1013 }
1014
1015 /*
1016 * Helper for nfs_pageio_add_request and nfs_pageio_complete
1017 */
nfs_pageio_doio(struct nfs_pageio_descriptor * desc)1018 static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
1019 {
1020 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1021
1022 if (!list_empty(&mirror->pg_list)) {
1023 int error = desc->pg_ops->pg_doio(desc);
1024 if (error < 0)
1025 desc->pg_error = error;
1026 if (list_empty(&mirror->pg_list)) {
1027 mirror->pg_bytes_written += mirror->pg_count;
1028 mirror->pg_count = 0;
1029 mirror->pg_base = 0;
1030 mirror->pg_recoalesce = 0;
1031 }
1032 }
1033 }
1034
1035 static void
nfs_pageio_cleanup_request(struct nfs_pageio_descriptor * desc,struct nfs_page * req)1036 nfs_pageio_cleanup_request(struct nfs_pageio_descriptor *desc,
1037 struct nfs_page *req)
1038 {
1039 LIST_HEAD(head);
1040
1041 nfs_list_move_request(req, &head);
1042 desc->pg_completion_ops->error_cleanup(&head, desc->pg_error);
1043 }
1044
1045 /**
1046 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
1047 * @desc: destination io descriptor
1048 * @req: request
1049 *
1050 * This may split a request into subrequests which are all part of the
1051 * same page group.
1052 *
1053 * Returns true if the request 'req' was successfully coalesced into the
1054 * existing list of pages 'desc'.
1055 */
__nfs_pageio_add_request(struct nfs_pageio_descriptor * desc,struct nfs_page * req)1056 static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
1057 struct nfs_page *req)
1058 {
1059 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1060
1061 struct nfs_page *subreq;
1062 unsigned int bytes_left = 0;
1063 unsigned int offset, pgbase;
1064
1065 nfs_page_group_lock(req);
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);
1082 if (desc->pg_error < 0 || mirror->pg_recoalesce)
1083 goto out_cleanup_subreq;
1084 /* retry add_request for this subreq */
1085 nfs_page_group_lock(req);
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) {
1099 subreq = nfs_create_subreq(req, subreq, pgbase,
1100 offset, bytes_left);
1101 if (IS_ERR(subreq))
1102 goto err_ptr;
1103 }
1104 } while (bytes_left > 0);
1105
1106 nfs_page_group_unlock(req);
1107 return 1;
1108 err_ptr:
1109 desc->pg_error = PTR_ERR(subreq);
1110 nfs_page_group_unlock(req);
1111 return 0;
1112 out_cleanup_subreq:
1113 if (req != subreq)
1114 nfs_pageio_cleanup_request(desc, subreq);
1115 return 0;
1116 }
1117
nfs_do_recoalesce(struct nfs_pageio_descriptor * desc)1118 static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
1119 {
1120 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1121 LIST_HEAD(head);
1122
1123 do {
1124 list_splice_init(&mirror->pg_list, &head);
1125 mirror->pg_count = 0;
1126 mirror->pg_base = 0;
1127 mirror->pg_recoalesce = 0;
1128
1129 while (!list_empty(&head)) {
1130 struct nfs_page *req;
1131
1132 req = list_first_entry(&head, struct nfs_page, wb_list);
1133 if (__nfs_pageio_add_request(desc, req))
1134 continue;
1135 if (desc->pg_error < 0) {
1136 list_splice_tail(&head, &mirror->pg_list);
1137 mirror->pg_recoalesce = 1;
1138 return 0;
1139 }
1140 break;
1141 }
1142 } while (mirror->pg_recoalesce);
1143 return 1;
1144 }
1145
nfs_pageio_add_request_mirror(struct nfs_pageio_descriptor * desc,struct nfs_page * req)1146 static int nfs_pageio_add_request_mirror(struct nfs_pageio_descriptor *desc,
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);
1159
1160 return ret;
1161 }
1162
nfs_pageio_error_cleanup(struct nfs_pageio_descriptor * desc)1163 static 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];
1173 desc->pg_completion_ops->error_cleanup(&mirror->pg_list,
1174 desc->pg_error);
1175 }
1176 }
1177
nfs_pageio_add_request(struct nfs_pageio_descriptor * desc,struct nfs_page * req)1178 int 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);
1190 if (desc->pg_error < 0)
1191 goto out_failed;
1192
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 }
1211
1212 desc->pg_mirror_idx = midx;
1213 if (!nfs_pageio_add_request_mirror(desc, dupreq))
1214 goto out_cleanup_subreq;
1215 }
1216
1217 desc->pg_mirror_idx = 0;
1218 if (!nfs_pageio_add_request_mirror(desc, req))
1219 goto out_failed;
1220
1221 return 1;
1222
1223 out_cleanup_subreq:
1224 nfs_pageio_cleanup_request(desc, dupreq);
1225 out_failed:
1226 nfs_pageio_error_cleanup(desc);
1227 return 0;
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
1234 * @mirror_idx: pointer to mirror index
1235 */
nfs_pageio_complete_mirror(struct nfs_pageio_descriptor * desc,u32 mirror_idx)1236 static 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
1242 if (nfs_pgio_has_mirroring(desc))
1243 desc->pg_mirror_idx = mirror_idx;
1244 for (;;) {
1245 nfs_pageio_doio(desc);
1246 if (desc->pg_error < 0 || !mirror->pg_recoalesce)
1247 break;
1248 if (!nfs_do_recoalesce(desc))
1249 break;
1250 }
1251 desc->pg_mirror_idx = restore_idx;
1252 }
1253
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 */
nfs_pageio_resend(struct nfs_pageio_descriptor * desc,struct nfs_pgio_header * hdr)1264 int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
1265 struct nfs_pgio_header *hdr)
1266 {
1267 LIST_HEAD(pages);
1268
1269 desc->pg_io_completion = hdr->io_completion;
1270 desc->pg_dreq = hdr->dreq;
1271 list_splice_init(&hdr->pages, &pages);
1272 while (!list_empty(&pages)) {
1273 struct nfs_page *req = nfs_list_entry(pages.next);
1274
1275 if (!nfs_pageio_add_request(desc, req))
1276 break;
1277 }
1278 nfs_pageio_complete(desc);
1279 if (!list_empty(&pages)) {
1280 int err = desc->pg_error < 0 ? desc->pg_error : -EIO;
1281 hdr->completion_ops->error_cleanup(&pages, err);
1282 nfs_set_pgio_error(hdr, err, hdr->io_start);
1283 return err;
1284 }
1285 return 0;
1286 }
1287 EXPORT_SYMBOL_GPL(nfs_pageio_resend);
1288
1289 /**
1290 * nfs_pageio_complete - Complete I/O then cleanup an nfs_pageio_descriptor
1291 * @desc: pointer to io descriptor
1292 */
nfs_pageio_complete(struct nfs_pageio_descriptor * desc)1293 void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
1294 {
1295 u32 midx;
1296
1297 for (midx = 0; midx < desc->pg_mirror_count; midx++)
1298 nfs_pageio_complete_mirror(desc, midx);
1299
1300 if (desc->pg_error < 0)
1301 nfs_pageio_error_cleanup(desc);
1302 if (desc->pg_ops->pg_cleanup)
1303 desc->pg_ops->pg_cleanup(desc);
1304 nfs_pageio_cleanup_mirroring(desc);
1305 }
1306
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 */
nfs_pageio_cond_complete(struct nfs_pageio_descriptor * desc,pgoff_t index)1318 void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
1319 {
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);
1328 if (index != prev->wb_index + 1) {
1329 nfs_pageio_complete(desc);
1330 break;
1331 }
1332 }
1333 }
1334 }
1335
1336 /*
1337 * nfs_pageio_stop_mirroring - stop using mirroring (set mirror count to 1)
1338 */
nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor * pgio)1339 void nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor *pgio)
1340 {
1341 nfs_pageio_complete(pgio);
1342 }
1343
nfs_init_nfspagecache(void)1344 int __init nfs_init_nfspagecache(void)
1345 {
1346 nfs_page_cachep = kmem_cache_create("nfs_page",
1347 sizeof(struct nfs_page),
1348 0, SLAB_HWCACHE_ALIGN,
1349 NULL);
1350 if (nfs_page_cachep == NULL)
1351 return -ENOMEM;
1352
1353 return 0;
1354 }
1355
nfs_destroy_nfspagecache(void)1356 void nfs_destroy_nfspagecache(void)
1357 {
1358 kmem_cache_destroy(nfs_page_cachep);
1359 }
1360
1361 static const struct rpc_call_ops nfs_pgio_common_ops = {
1362 .rpc_call_prepare = nfs_pgio_prepare,
1363 .rpc_call_done = nfs_pgio_result,
1364 .rpc_release = nfs_pgio_release,
1365 };
1366
1367 const struct nfs_pageio_ops nfs_pgio_rw_ops = {
1368 .pg_test = nfs_generic_pg_test,
1369 .pg_doio = nfs_generic_pg_pgios,
1370 };
1371