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