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