xref: /openbmc/linux/fs/nfs/write.c (revision 20633f04)
1 /*
2  * linux/fs/nfs/write.c
3  *
4  * Write file data over NFS.
5  *
6  * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
7  */
8 
9 #include <linux/types.h>
10 #include <linux/slab.h>
11 #include <linux/mm.h>
12 #include <linux/pagemap.h>
13 #include <linux/file.h>
14 #include <linux/writeback.h>
15 #include <linux/swap.h>
16 #include <linux/migrate.h>
17 
18 #include <linux/sunrpc/clnt.h>
19 #include <linux/nfs_fs.h>
20 #include <linux/nfs_mount.h>
21 #include <linux/nfs_page.h>
22 #include <linux/backing-dev.h>
23 #include <linux/export.h>
24 
25 #include <asm/uaccess.h>
26 
27 #include "delegation.h"
28 #include "internal.h"
29 #include "iostat.h"
30 #include "nfs4_fs.h"
31 #include "fscache.h"
32 #include "pnfs.h"
33 
34 #include "nfstrace.h"
35 
36 #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
37 
38 #define MIN_POOL_WRITE		(32)
39 #define MIN_POOL_COMMIT		(4)
40 
41 /*
42  * Local function declarations
43  */
44 static void nfs_redirty_request(struct nfs_page *req);
45 static const struct rpc_call_ops nfs_commit_ops;
46 static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
47 static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
48 static const struct nfs_rw_ops nfs_rw_write_ops;
49 
50 static struct kmem_cache *nfs_wdata_cachep;
51 static mempool_t *nfs_wdata_mempool;
52 static struct kmem_cache *nfs_cdata_cachep;
53 static mempool_t *nfs_commit_mempool;
54 
55 struct nfs_commit_data *nfs_commitdata_alloc(void)
56 {
57 	struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOIO);
58 
59 	if (p) {
60 		memset(p, 0, sizeof(*p));
61 		INIT_LIST_HEAD(&p->pages);
62 	}
63 	return p;
64 }
65 EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
66 
67 void nfs_commit_free(struct nfs_commit_data *p)
68 {
69 	mempool_free(p, nfs_commit_mempool);
70 }
71 EXPORT_SYMBOL_GPL(nfs_commit_free);
72 
73 static struct nfs_rw_header *nfs_writehdr_alloc(void)
74 {
75 	struct nfs_rw_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO);
76 
77 	if (p)
78 		memset(p, 0, sizeof(*p));
79 	return p;
80 }
81 
82 static void nfs_writehdr_free(struct nfs_rw_header *whdr)
83 {
84 	mempool_free(whdr, nfs_wdata_mempool);
85 }
86 
87 static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
88 {
89 	ctx->error = error;
90 	smp_wmb();
91 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
92 }
93 
94 static struct nfs_page *
95 nfs_page_find_request_locked(struct nfs_inode *nfsi, struct page *page)
96 {
97 	struct nfs_page *req = NULL;
98 
99 	if (PagePrivate(page))
100 		req = (struct nfs_page *)page_private(page);
101 	else if (unlikely(PageSwapCache(page))) {
102 		struct nfs_page *freq, *t;
103 
104 		/* Linearly search the commit list for the correct req */
105 		list_for_each_entry_safe(freq, t, &nfsi->commit_info.list, wb_list) {
106 			if (freq->wb_page == page) {
107 				req = freq;
108 				break;
109 			}
110 		}
111 	}
112 
113 	if (req)
114 		kref_get(&req->wb_kref);
115 
116 	return req;
117 }
118 
119 static struct nfs_page *nfs_page_find_request(struct page *page)
120 {
121 	struct inode *inode = page_file_mapping(page)->host;
122 	struct nfs_page *req = NULL;
123 
124 	spin_lock(&inode->i_lock);
125 	req = nfs_page_find_request_locked(NFS_I(inode), page);
126 	spin_unlock(&inode->i_lock);
127 	return req;
128 }
129 
130 /* Adjust the file length if we're writing beyond the end */
131 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
132 {
133 	struct inode *inode = page_file_mapping(page)->host;
134 	loff_t end, i_size;
135 	pgoff_t end_index;
136 
137 	spin_lock(&inode->i_lock);
138 	i_size = i_size_read(inode);
139 	end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
140 	if (i_size > 0 && page_file_index(page) < end_index)
141 		goto out;
142 	end = page_file_offset(page) + ((loff_t)offset+count);
143 	if (i_size >= end)
144 		goto out;
145 	i_size_write(inode, end);
146 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
147 out:
148 	spin_unlock(&inode->i_lock);
149 }
150 
151 /* A writeback failed: mark the page as bad, and invalidate the page cache */
152 static void nfs_set_pageerror(struct page *page)
153 {
154 	nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page));
155 }
156 
157 /* We can set the PG_uptodate flag if we see that a write request
158  * covers the full page.
159  */
160 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
161 {
162 	if (PageUptodate(page))
163 		return;
164 	if (base != 0)
165 		return;
166 	if (count != nfs_page_length(page))
167 		return;
168 	SetPageUptodate(page);
169 }
170 
171 static int wb_priority(struct writeback_control *wbc)
172 {
173 	if (wbc->for_reclaim)
174 		return FLUSH_HIGHPRI | FLUSH_STABLE;
175 	if (wbc->for_kupdate || wbc->for_background)
176 		return FLUSH_LOWPRI | FLUSH_COND_STABLE;
177 	return FLUSH_COND_STABLE;
178 }
179 
180 /*
181  * NFS congestion control
182  */
183 
184 int nfs_congestion_kb;
185 
186 #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
187 #define NFS_CONGESTION_OFF_THRESH	\
188 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
189 
190 static void nfs_set_page_writeback(struct page *page)
191 {
192 	struct nfs_server *nfss = NFS_SERVER(page_file_mapping(page)->host);
193 	int ret = test_set_page_writeback(page);
194 
195 	WARN_ON_ONCE(ret != 0);
196 
197 	if (atomic_long_inc_return(&nfss->writeback) >
198 			NFS_CONGESTION_ON_THRESH) {
199 		set_bdi_congested(&nfss->backing_dev_info,
200 					BLK_RW_ASYNC);
201 	}
202 }
203 
204 static void nfs_end_page_writeback(struct nfs_page *req)
205 {
206 	struct inode *inode = page_file_mapping(req->wb_page)->host;
207 	struct nfs_server *nfss = NFS_SERVER(inode);
208 
209 	if (!nfs_page_group_sync_on_bit(req, PG_WB_END))
210 		return;
211 
212 	end_page_writeback(req->wb_page);
213 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
214 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
215 }
216 
217 static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
218 {
219 	struct inode *inode = page_file_mapping(page)->host;
220 	struct nfs_page *req;
221 	int ret;
222 
223 	spin_lock(&inode->i_lock);
224 	for (;;) {
225 		req = nfs_page_find_request_locked(NFS_I(inode), page);
226 		if (req == NULL)
227 			break;
228 		if (nfs_lock_request(req))
229 			break;
230 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
231 		 *	 then the call to nfs_lock_request() will always
232 		 *	 succeed provided that someone hasn't already marked the
233 		 *	 request as dirty (in which case we don't care).
234 		 */
235 		spin_unlock(&inode->i_lock);
236 		if (!nonblock)
237 			ret = nfs_wait_on_request(req);
238 		else
239 			ret = -EAGAIN;
240 		nfs_release_request(req);
241 		if (ret != 0)
242 			return ERR_PTR(ret);
243 		spin_lock(&inode->i_lock);
244 	}
245 	spin_unlock(&inode->i_lock);
246 	return req;
247 }
248 
249 /*
250  * Find an associated nfs write request, and prepare to flush it out
251  * May return an error if the user signalled nfs_wait_on_request().
252  */
253 static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
254 				struct page *page, bool nonblock)
255 {
256 	struct nfs_page *req;
257 	int ret = 0;
258 
259 	req = nfs_find_and_lock_request(page, nonblock);
260 	if (!req)
261 		goto out;
262 	ret = PTR_ERR(req);
263 	if (IS_ERR(req))
264 		goto out;
265 
266 	nfs_set_page_writeback(page);
267 	WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
268 
269 	ret = 0;
270 	if (!nfs_pageio_add_request(pgio, req)) {
271 		nfs_redirty_request(req);
272 		ret = pgio->pg_error;
273 	}
274 out:
275 	return ret;
276 }
277 
278 static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
279 {
280 	struct inode *inode = page_file_mapping(page)->host;
281 	int ret;
282 
283 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
284 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
285 
286 	nfs_pageio_cond_complete(pgio, page_file_index(page));
287 	ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
288 	if (ret == -EAGAIN) {
289 		redirty_page_for_writepage(wbc, page);
290 		ret = 0;
291 	}
292 	return ret;
293 }
294 
295 /*
296  * Write an mmapped page to the server.
297  */
298 static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
299 {
300 	struct nfs_pageio_descriptor pgio;
301 	int err;
302 
303 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc),
304 				false, &nfs_async_write_completion_ops);
305 	err = nfs_do_writepage(page, wbc, &pgio);
306 	nfs_pageio_complete(&pgio);
307 	if (err < 0)
308 		return err;
309 	if (pgio.pg_error < 0)
310 		return pgio.pg_error;
311 	return 0;
312 }
313 
314 int nfs_writepage(struct page *page, struct writeback_control *wbc)
315 {
316 	int ret;
317 
318 	ret = nfs_writepage_locked(page, wbc);
319 	unlock_page(page);
320 	return ret;
321 }
322 
323 static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
324 {
325 	int ret;
326 
327 	ret = nfs_do_writepage(page, wbc, data);
328 	unlock_page(page);
329 	return ret;
330 }
331 
332 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
333 {
334 	struct inode *inode = mapping->host;
335 	unsigned long *bitlock = &NFS_I(inode)->flags;
336 	struct nfs_pageio_descriptor pgio;
337 	int err;
338 
339 	/* Stop dirtying of new pages while we sync */
340 	err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
341 			nfs_wait_bit_killable, TASK_KILLABLE);
342 	if (err)
343 		goto out_err;
344 
345 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
346 
347 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
348 				&nfs_async_write_completion_ops);
349 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
350 	nfs_pageio_complete(&pgio);
351 
352 	clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
353 	smp_mb__after_clear_bit();
354 	wake_up_bit(bitlock, NFS_INO_FLUSHING);
355 
356 	if (err < 0)
357 		goto out_err;
358 	err = pgio.pg_error;
359 	if (err < 0)
360 		goto out_err;
361 	return 0;
362 out_err:
363 	return err;
364 }
365 
366 /*
367  * Insert a write request into an inode
368  */
369 static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
370 {
371 	struct nfs_inode *nfsi = NFS_I(inode);
372 
373 	WARN_ON_ONCE(req->wb_this_page != req);
374 
375 	/* Lock the request! */
376 	nfs_lock_request(req);
377 
378 	spin_lock(&inode->i_lock);
379 	if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
380 		inode->i_version++;
381 	/*
382 	 * Swap-space should not get truncated. Hence no need to plug the race
383 	 * with invalidate/truncate.
384 	 */
385 	if (likely(!PageSwapCache(req->wb_page))) {
386 		set_bit(PG_MAPPED, &req->wb_flags);
387 		SetPagePrivate(req->wb_page);
388 		set_page_private(req->wb_page, (unsigned long)req);
389 	}
390 	nfsi->npages++;
391 	set_bit(PG_INODE_REF, &req->wb_flags);
392 	kref_get(&req->wb_kref);
393 	spin_unlock(&inode->i_lock);
394 }
395 
396 /*
397  * Remove a write request from an inode
398  */
399 static void nfs_inode_remove_request(struct nfs_page *req)
400 {
401 	struct inode *inode = req->wb_context->dentry->d_inode;
402 	struct nfs_inode *nfsi = NFS_I(inode);
403 	struct nfs_page *head;
404 
405 	if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
406 		head = req->wb_head;
407 
408 		spin_lock(&inode->i_lock);
409 		if (likely(!PageSwapCache(head->wb_page))) {
410 			set_page_private(head->wb_page, 0);
411 			ClearPagePrivate(head->wb_page);
412 			clear_bit(PG_MAPPED, &head->wb_flags);
413 		}
414 		nfsi->npages--;
415 		spin_unlock(&inode->i_lock);
416 	}
417 	nfs_release_request(req);
418 }
419 
420 static void
421 nfs_mark_request_dirty(struct nfs_page *req)
422 {
423 	__set_page_dirty_nobuffers(req->wb_page);
424 }
425 
426 #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
427 /**
428  * nfs_request_add_commit_list - add request to a commit list
429  * @req: pointer to a struct nfs_page
430  * @dst: commit list head
431  * @cinfo: holds list lock and accounting info
432  *
433  * This sets the PG_CLEAN bit, updates the cinfo count of
434  * number of outstanding requests requiring a commit as well as
435  * the MM page stats.
436  *
437  * The caller must _not_ hold the cinfo->lock, but must be
438  * holding the nfs_page lock.
439  */
440 void
441 nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst,
442 			    struct nfs_commit_info *cinfo)
443 {
444 	set_bit(PG_CLEAN, &(req)->wb_flags);
445 	spin_lock(cinfo->lock);
446 	nfs_list_add_request(req, dst);
447 	cinfo->mds->ncommit++;
448 	spin_unlock(cinfo->lock);
449 	if (!cinfo->dreq) {
450 		inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
451 		inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
452 			     BDI_RECLAIMABLE);
453 		__mark_inode_dirty(req->wb_context->dentry->d_inode,
454 				   I_DIRTY_DATASYNC);
455 	}
456 }
457 EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
458 
459 /**
460  * nfs_request_remove_commit_list - Remove request from a commit list
461  * @req: pointer to a nfs_page
462  * @cinfo: holds list lock and accounting info
463  *
464  * This clears the PG_CLEAN bit, and updates the cinfo's count of
465  * number of outstanding requests requiring a commit
466  * It does not update the MM page stats.
467  *
468  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
469  */
470 void
471 nfs_request_remove_commit_list(struct nfs_page *req,
472 			       struct nfs_commit_info *cinfo)
473 {
474 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
475 		return;
476 	nfs_list_remove_request(req);
477 	cinfo->mds->ncommit--;
478 }
479 EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
480 
481 static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
482 				      struct inode *inode)
483 {
484 	cinfo->lock = &inode->i_lock;
485 	cinfo->mds = &NFS_I(inode)->commit_info;
486 	cinfo->ds = pnfs_get_ds_info(inode);
487 	cinfo->dreq = NULL;
488 	cinfo->completion_ops = &nfs_commit_completion_ops;
489 }
490 
491 void nfs_init_cinfo(struct nfs_commit_info *cinfo,
492 		    struct inode *inode,
493 		    struct nfs_direct_req *dreq)
494 {
495 	if (dreq)
496 		nfs_init_cinfo_from_dreq(cinfo, dreq);
497 	else
498 		nfs_init_cinfo_from_inode(cinfo, inode);
499 }
500 EXPORT_SYMBOL_GPL(nfs_init_cinfo);
501 
502 /*
503  * Add a request to the inode's commit list.
504  */
505 void
506 nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
507 			struct nfs_commit_info *cinfo)
508 {
509 	if (pnfs_mark_request_commit(req, lseg, cinfo))
510 		return;
511 	nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo);
512 }
513 
514 static void
515 nfs_clear_page_commit(struct page *page)
516 {
517 	dec_zone_page_state(page, NR_UNSTABLE_NFS);
518 	dec_bdi_stat(page_file_mapping(page)->backing_dev_info, BDI_RECLAIMABLE);
519 }
520 
521 static void
522 nfs_clear_request_commit(struct nfs_page *req)
523 {
524 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
525 		struct inode *inode = req->wb_context->dentry->d_inode;
526 		struct nfs_commit_info cinfo;
527 
528 		nfs_init_cinfo_from_inode(&cinfo, inode);
529 		if (!pnfs_clear_request_commit(req, &cinfo)) {
530 			spin_lock(cinfo.lock);
531 			nfs_request_remove_commit_list(req, &cinfo);
532 			spin_unlock(cinfo.lock);
533 		}
534 		nfs_clear_page_commit(req->wb_page);
535 	}
536 }
537 
538 static inline
539 int nfs_write_need_commit(struct nfs_pgio_data *data)
540 {
541 	if (data->verf.committed == NFS_DATA_SYNC)
542 		return data->header->lseg == NULL;
543 	return data->verf.committed != NFS_FILE_SYNC;
544 }
545 
546 #else
547 static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
548 				      struct inode *inode)
549 {
550 }
551 
552 void nfs_init_cinfo(struct nfs_commit_info *cinfo,
553 		    struct inode *inode,
554 		    struct nfs_direct_req *dreq)
555 {
556 }
557 
558 void
559 nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
560 			struct nfs_commit_info *cinfo)
561 {
562 }
563 
564 static void
565 nfs_clear_request_commit(struct nfs_page *req)
566 {
567 }
568 
569 static inline
570 int nfs_write_need_commit(struct nfs_pgio_data *data)
571 {
572 	return 0;
573 }
574 
575 #endif
576 
577 static void nfs_write_completion(struct nfs_pgio_header *hdr)
578 {
579 	struct nfs_commit_info cinfo;
580 	unsigned long bytes = 0;
581 	bool do_destroy;
582 
583 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
584 		goto out;
585 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
586 	while (!list_empty(&hdr->pages)) {
587 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
588 
589 		bytes += req->wb_bytes;
590 		nfs_list_remove_request(req);
591 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
592 		    (hdr->good_bytes < bytes)) {
593 			nfs_set_pageerror(req->wb_page);
594 			nfs_context_set_write_error(req->wb_context, hdr->error);
595 			goto remove_req;
596 		}
597 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
598 			nfs_mark_request_dirty(req);
599 			goto next;
600 		}
601 		if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
602 			memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
603 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
604 			goto next;
605 		}
606 remove_req:
607 		nfs_inode_remove_request(req);
608 next:
609 		nfs_unlock_request(req);
610 		nfs_end_page_writeback(req);
611 		do_destroy = !test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags);
612 		nfs_release_request(req);
613 	}
614 out:
615 	hdr->release(hdr);
616 }
617 
618 #if  IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
619 unsigned long
620 nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
621 {
622 	return cinfo->mds->ncommit;
623 }
624 
625 /* cinfo->lock held by caller */
626 int
627 nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
628 		     struct nfs_commit_info *cinfo, int max)
629 {
630 	struct nfs_page *req, *tmp;
631 	int ret = 0;
632 
633 	list_for_each_entry_safe(req, tmp, src, wb_list) {
634 		if (!nfs_lock_request(req))
635 			continue;
636 		kref_get(&req->wb_kref);
637 		if (cond_resched_lock(cinfo->lock))
638 			list_safe_reset_next(req, tmp, wb_list);
639 		nfs_request_remove_commit_list(req, cinfo);
640 		nfs_list_add_request(req, dst);
641 		ret++;
642 		if ((ret == max) && !cinfo->dreq)
643 			break;
644 	}
645 	return ret;
646 }
647 
648 /*
649  * nfs_scan_commit - Scan an inode for commit requests
650  * @inode: NFS inode to scan
651  * @dst: mds destination list
652  * @cinfo: mds and ds lists of reqs ready to commit
653  *
654  * Moves requests from the inode's 'commit' request list.
655  * The requests are *not* checked to ensure that they form a contiguous set.
656  */
657 int
658 nfs_scan_commit(struct inode *inode, struct list_head *dst,
659 		struct nfs_commit_info *cinfo)
660 {
661 	int ret = 0;
662 
663 	spin_lock(cinfo->lock);
664 	if (cinfo->mds->ncommit > 0) {
665 		const int max = INT_MAX;
666 
667 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
668 					   cinfo, max);
669 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
670 	}
671 	spin_unlock(cinfo->lock);
672 	return ret;
673 }
674 
675 #else
676 unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
677 {
678 	return 0;
679 }
680 
681 int nfs_scan_commit(struct inode *inode, struct list_head *dst,
682 		    struct nfs_commit_info *cinfo)
683 {
684 	return 0;
685 }
686 #endif
687 
688 /*
689  * Search for an existing write request, and attempt to update
690  * it to reflect a new dirty region on a given page.
691  *
692  * If the attempt fails, then the existing request is flushed out
693  * to disk.
694  */
695 static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
696 		struct page *page,
697 		unsigned int offset,
698 		unsigned int bytes)
699 {
700 	struct nfs_page *req;
701 	unsigned int rqend;
702 	unsigned int end;
703 	int error;
704 
705 	if (!PagePrivate(page))
706 		return NULL;
707 
708 	end = offset + bytes;
709 	spin_lock(&inode->i_lock);
710 
711 	for (;;) {
712 		req = nfs_page_find_request_locked(NFS_I(inode), page);
713 		if (req == NULL)
714 			goto out_unlock;
715 
716 		/* should be handled by nfs_flush_incompatible */
717 		WARN_ON_ONCE(req->wb_head != req);
718 		WARN_ON_ONCE(req->wb_this_page != req);
719 
720 		rqend = req->wb_offset + req->wb_bytes;
721 		/*
722 		 * Tell the caller to flush out the request if
723 		 * the offsets are non-contiguous.
724 		 * Note: nfs_flush_incompatible() will already
725 		 * have flushed out requests having wrong owners.
726 		 */
727 		if (offset > rqend
728 		    || end < req->wb_offset)
729 			goto out_flushme;
730 
731 		if (nfs_lock_request(req))
732 			break;
733 
734 		/* The request is locked, so wait and then retry */
735 		spin_unlock(&inode->i_lock);
736 		error = nfs_wait_on_request(req);
737 		nfs_release_request(req);
738 		if (error != 0)
739 			goto out_err;
740 		spin_lock(&inode->i_lock);
741 	}
742 
743 	/* Okay, the request matches. Update the region */
744 	if (offset < req->wb_offset) {
745 		req->wb_offset = offset;
746 		req->wb_pgbase = offset;
747 	}
748 	if (end > rqend)
749 		req->wb_bytes = end - req->wb_offset;
750 	else
751 		req->wb_bytes = rqend - req->wb_offset;
752 out_unlock:
753 	spin_unlock(&inode->i_lock);
754 	if (req)
755 		nfs_clear_request_commit(req);
756 	return req;
757 out_flushme:
758 	spin_unlock(&inode->i_lock);
759 	nfs_release_request(req);
760 	error = nfs_wb_page(inode, page);
761 out_err:
762 	return ERR_PTR(error);
763 }
764 
765 /*
766  * Try to update an existing write request, or create one if there is none.
767  *
768  * Note: Should always be called with the Page Lock held to prevent races
769  * if we have to add a new request. Also assumes that the caller has
770  * already called nfs_flush_incompatible() if necessary.
771  */
772 static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
773 		struct page *page, unsigned int offset, unsigned int bytes)
774 {
775 	struct inode *inode = page_file_mapping(page)->host;
776 	struct nfs_page	*req;
777 
778 	req = nfs_try_to_update_request(inode, page, offset, bytes);
779 	if (req != NULL)
780 		goto out;
781 	req = nfs_create_request(ctx, page, NULL, offset, bytes);
782 	if (IS_ERR(req))
783 		goto out;
784 	nfs_inode_add_request(inode, req);
785 out:
786 	return req;
787 }
788 
789 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
790 		unsigned int offset, unsigned int count)
791 {
792 	struct nfs_page	*req;
793 
794 	req = nfs_setup_write_request(ctx, page, offset, count);
795 	if (IS_ERR(req))
796 		return PTR_ERR(req);
797 	/* Update file length */
798 	nfs_grow_file(page, offset, count);
799 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
800 	nfs_mark_request_dirty(req);
801 	nfs_unlock_and_release_request(req);
802 	return 0;
803 }
804 
805 int nfs_flush_incompatible(struct file *file, struct page *page)
806 {
807 	struct nfs_open_context *ctx = nfs_file_open_context(file);
808 	struct nfs_lock_context *l_ctx;
809 	struct nfs_page	*req;
810 	int do_flush, status;
811 	/*
812 	 * Look for a request corresponding to this page. If there
813 	 * is one, and it belongs to another file, we flush it out
814 	 * before we try to copy anything into the page. Do this
815 	 * due to the lack of an ACCESS-type call in NFSv2.
816 	 * Also do the same if we find a request from an existing
817 	 * dropped page.
818 	 */
819 	do {
820 		req = nfs_page_find_request(page);
821 		if (req == NULL)
822 			return 0;
823 		l_ctx = req->wb_lock_context;
824 		do_flush = req->wb_page != page || req->wb_context != ctx;
825 		/* for now, flush if more than 1 request in page_group */
826 		do_flush |= req->wb_this_page != req;
827 		if (l_ctx && ctx->dentry->d_inode->i_flock != NULL) {
828 			do_flush |= l_ctx->lockowner.l_owner != current->files
829 				|| l_ctx->lockowner.l_pid != current->tgid;
830 		}
831 		nfs_release_request(req);
832 		if (!do_flush)
833 			return 0;
834 		status = nfs_wb_page(page_file_mapping(page)->host, page);
835 	} while (status == 0);
836 	return status;
837 }
838 
839 /*
840  * Avoid buffered writes when a open context credential's key would
841  * expire soon.
842  *
843  * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
844  *
845  * Return 0 and set a credential flag which triggers the inode to flush
846  * and performs  NFS_FILE_SYNC writes if the key will expired within
847  * RPC_KEY_EXPIRE_TIMEO.
848  */
849 int
850 nfs_key_timeout_notify(struct file *filp, struct inode *inode)
851 {
852 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
853 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
854 
855 	return rpcauth_key_timeout_notify(auth, ctx->cred);
856 }
857 
858 /*
859  * Test if the open context credential key is marked to expire soon.
860  */
861 bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx)
862 {
863 	return rpcauth_cred_key_to_expire(ctx->cred);
864 }
865 
866 /*
867  * If the page cache is marked as unsafe or invalid, then we can't rely on
868  * the PageUptodate() flag. In this case, we will need to turn off
869  * write optimisations that depend on the page contents being correct.
870  */
871 static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
872 {
873 	struct nfs_inode *nfsi = NFS_I(inode);
874 
875 	if (nfs_have_delegated_attributes(inode))
876 		goto out;
877 	if (nfsi->cache_validity & (NFS_INO_INVALID_DATA|NFS_INO_REVAL_PAGECACHE))
878 		return false;
879 	smp_rmb();
880 	if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
881 		return false;
882 out:
883 	return PageUptodate(page) != 0;
884 }
885 
886 /* If we know the page is up to date, and we're not using byte range locks (or
887  * if we have the whole file locked for writing), it may be more efficient to
888  * extend the write to cover the entire page in order to avoid fragmentation
889  * inefficiencies.
890  *
891  * If the file is opened for synchronous writes then we can just skip the rest
892  * of the checks.
893  */
894 static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
895 {
896 	if (file->f_flags & O_DSYNC)
897 		return 0;
898 	if (!nfs_write_pageuptodate(page, inode))
899 		return 0;
900 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
901 		return 1;
902 	if (inode->i_flock == NULL || (inode->i_flock->fl_start == 0 &&
903 			inode->i_flock->fl_end == OFFSET_MAX &&
904 			inode->i_flock->fl_type != F_RDLCK))
905 		return 1;
906 	return 0;
907 }
908 
909 /*
910  * Update and possibly write a cached page of an NFS file.
911  *
912  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
913  * things with a page scheduled for an RPC call (e.g. invalidate it).
914  */
915 int nfs_updatepage(struct file *file, struct page *page,
916 		unsigned int offset, unsigned int count)
917 {
918 	struct nfs_open_context *ctx = nfs_file_open_context(file);
919 	struct inode	*inode = page_file_mapping(page)->host;
920 	int		status = 0;
921 
922 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
923 
924 	dprintk("NFS:       nfs_updatepage(%pD2 %d@%lld)\n",
925 		file, count, (long long)(page_file_offset(page) + offset));
926 
927 	if (nfs_can_extend_write(file, page, inode)) {
928 		count = max(count + offset, nfs_page_length(page));
929 		offset = 0;
930 	}
931 
932 	status = nfs_writepage_setup(ctx, page, offset, count);
933 	if (status < 0)
934 		nfs_set_pageerror(page);
935 	else
936 		__set_page_dirty_nobuffers(page);
937 
938 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
939 			status, (long long)i_size_read(inode));
940 	return status;
941 }
942 
943 static int flush_task_priority(int how)
944 {
945 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
946 		case FLUSH_HIGHPRI:
947 			return RPC_PRIORITY_HIGH;
948 		case FLUSH_LOWPRI:
949 			return RPC_PRIORITY_LOW;
950 	}
951 	return RPC_PRIORITY_NORMAL;
952 }
953 
954 static void nfs_initiate_write(struct nfs_pgio_data *data, struct rpc_message *msg,
955 			       struct rpc_task_setup *task_setup_data, int how)
956 {
957 	struct inode *inode = data->header->inode;
958 	int priority = flush_task_priority(how);
959 
960 	task_setup_data->priority = priority;
961 	NFS_PROTO(inode)->write_setup(data, msg);
962 
963 	nfs4_state_protect_write(NFS_SERVER(inode)->nfs_client,
964 				 &task_setup_data->rpc_client, msg, data);
965 }
966 
967 /* If a nfs_flush_* function fails, it should remove reqs from @head and
968  * call this on each, which will prepare them to be retried on next
969  * writeback using standard nfs.
970  */
971 static void nfs_redirty_request(struct nfs_page *req)
972 {
973 	nfs_mark_request_dirty(req);
974 	nfs_unlock_request(req);
975 	nfs_end_page_writeback(req);
976 	nfs_release_request(req);
977 }
978 
979 static void nfs_async_write_error(struct list_head *head)
980 {
981 	struct nfs_page	*req;
982 
983 	while (!list_empty(head)) {
984 		req = nfs_list_entry(head->next);
985 		nfs_list_remove_request(req);
986 		nfs_redirty_request(req);
987 	}
988 }
989 
990 static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
991 	.error_cleanup = nfs_async_write_error,
992 	.completion = nfs_write_completion,
993 };
994 
995 void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
996 			       struct inode *inode, int ioflags, bool force_mds,
997 			       const struct nfs_pgio_completion_ops *compl_ops)
998 {
999 	struct nfs_server *server = NFS_SERVER(inode);
1000 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
1001 
1002 #ifdef CONFIG_NFS_V4_1
1003 	if (server->pnfs_curr_ld && !force_mds)
1004 		pg_ops = server->pnfs_curr_ld->pg_write_ops;
1005 #endif
1006 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
1007 			server->wsize, ioflags);
1008 }
1009 EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
1010 
1011 void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1012 {
1013 	pgio->pg_ops = &nfs_pgio_rw_ops;
1014 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1015 }
1016 EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1017 
1018 
1019 void nfs_commit_prepare(struct rpc_task *task, void *calldata)
1020 {
1021 	struct nfs_commit_data *data = calldata;
1022 
1023 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
1024 }
1025 
1026 static void nfs_writeback_release_common(struct nfs_pgio_data *data)
1027 {
1028 	struct nfs_pgio_header *hdr = data->header;
1029 	int status = data->task.tk_status;
1030 
1031 	if ((status >= 0) && nfs_write_need_commit(data)) {
1032 		spin_lock(&hdr->lock);
1033 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags))
1034 			; /* Do nothing */
1035 		else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags))
1036 			memcpy(&hdr->verf, &data->verf, sizeof(hdr->verf));
1037 		else if (memcmp(&hdr->verf, &data->verf, sizeof(hdr->verf)))
1038 			set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags);
1039 		spin_unlock(&hdr->lock);
1040 	}
1041 }
1042 
1043 /*
1044  * Special version of should_remove_suid() that ignores capabilities.
1045  */
1046 static int nfs_should_remove_suid(const struct inode *inode)
1047 {
1048 	umode_t mode = inode->i_mode;
1049 	int kill = 0;
1050 
1051 	/* suid always must be killed */
1052 	if (unlikely(mode & S_ISUID))
1053 		kill = ATTR_KILL_SUID;
1054 
1055 	/*
1056 	 * sgid without any exec bits is just a mandatory locking mark; leave
1057 	 * it alone.  If some exec bits are set, it's a real sgid; kill it.
1058 	 */
1059 	if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
1060 		kill |= ATTR_KILL_SGID;
1061 
1062 	if (unlikely(kill && S_ISREG(mode)))
1063 		return kill;
1064 
1065 	return 0;
1066 }
1067 
1068 /*
1069  * This function is called when the WRITE call is complete.
1070  */
1071 static int nfs_writeback_done(struct rpc_task *task, struct nfs_pgio_data *data,
1072 			      struct inode *inode)
1073 {
1074 	int status;
1075 
1076 	/*
1077 	 * ->write_done will attempt to use post-op attributes to detect
1078 	 * conflicting writes by other clients.  A strict interpretation
1079 	 * of close-to-open would allow us to continue caching even if
1080 	 * another writer had changed the file, but some applications
1081 	 * depend on tighter cache coherency when writing.
1082 	 */
1083 	status = NFS_PROTO(inode)->write_done(task, data);
1084 	if (status != 0)
1085 		return status;
1086 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, data->res.count);
1087 
1088 #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
1089 	if (data->res.verf->committed < data->args.stable && task->tk_status >= 0) {
1090 		/* We tried a write call, but the server did not
1091 		 * commit data to stable storage even though we
1092 		 * requested it.
1093 		 * Note: There is a known bug in Tru64 < 5.0 in which
1094 		 *	 the server reports NFS_DATA_SYNC, but performs
1095 		 *	 NFS_FILE_SYNC. We therefore implement this checking
1096 		 *	 as a dprintk() in order to avoid filling syslog.
1097 		 */
1098 		static unsigned long    complain;
1099 
1100 		/* Note this will print the MDS for a DS write */
1101 		if (time_before(complain, jiffies)) {
1102 			dprintk("NFS:       faulty NFS server %s:"
1103 				" (committed = %d) != (stable = %d)\n",
1104 				NFS_SERVER(inode)->nfs_client->cl_hostname,
1105 				data->res.verf->committed, data->args.stable);
1106 			complain = jiffies + 300 * HZ;
1107 		}
1108 	}
1109 #endif
1110 
1111 	/* Deal with the suid/sgid bit corner case */
1112 	if (nfs_should_remove_suid(inode))
1113 		nfs_mark_for_revalidate(inode);
1114 	return 0;
1115 }
1116 
1117 /*
1118  * This function is called when the WRITE call is complete.
1119  */
1120 static void nfs_writeback_result(struct rpc_task *task, struct nfs_pgio_data *data)
1121 {
1122 	struct nfs_pgio_args	*argp = &data->args;
1123 	struct nfs_pgio_res	*resp = &data->res;
1124 
1125 	if (resp->count < argp->count) {
1126 		static unsigned long    complain;
1127 
1128 		/* This a short write! */
1129 		nfs_inc_stats(data->header->inode, NFSIOS_SHORTWRITE);
1130 
1131 		/* Has the server at least made some progress? */
1132 		if (resp->count == 0) {
1133 			if (time_before(complain, jiffies)) {
1134 				printk(KERN_WARNING
1135 				       "NFS: Server wrote zero bytes, expected %u.\n",
1136 				       argp->count);
1137 				complain = jiffies + 300 * HZ;
1138 			}
1139 			nfs_set_pgio_error(data->header, -EIO, argp->offset);
1140 			task->tk_status = -EIO;
1141 			return;
1142 		}
1143 		/* Was this an NFSv2 write or an NFSv3 stable write? */
1144 		if (resp->verf->committed != NFS_UNSTABLE) {
1145 			/* Resend from where the server left off */
1146 			data->mds_offset += resp->count;
1147 			argp->offset += resp->count;
1148 			argp->pgbase += resp->count;
1149 			argp->count -= resp->count;
1150 		} else {
1151 			/* Resend as a stable write in order to avoid
1152 			 * headaches in the case of a server crash.
1153 			 */
1154 			argp->stable = NFS_FILE_SYNC;
1155 		}
1156 		rpc_restart_call_prepare(task);
1157 	}
1158 }
1159 
1160 
1161 #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
1162 static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
1163 {
1164 	int ret;
1165 
1166 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
1167 		return 1;
1168 	if (!may_wait)
1169 		return 0;
1170 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1171 				NFS_INO_COMMIT,
1172 				nfs_wait_bit_killable,
1173 				TASK_KILLABLE);
1174 	return (ret < 0) ? ret : 1;
1175 }
1176 
1177 static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
1178 {
1179 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
1180 	smp_mb__after_clear_bit();
1181 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
1182 }
1183 
1184 void nfs_commitdata_release(struct nfs_commit_data *data)
1185 {
1186 	put_nfs_open_context(data->context);
1187 	nfs_commit_free(data);
1188 }
1189 EXPORT_SYMBOL_GPL(nfs_commitdata_release);
1190 
1191 int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
1192 			const struct rpc_call_ops *call_ops,
1193 			int how, int flags)
1194 {
1195 	struct rpc_task *task;
1196 	int priority = flush_task_priority(how);
1197 	struct rpc_message msg = {
1198 		.rpc_argp = &data->args,
1199 		.rpc_resp = &data->res,
1200 		.rpc_cred = data->cred,
1201 	};
1202 	struct rpc_task_setup task_setup_data = {
1203 		.task = &data->task,
1204 		.rpc_client = clnt,
1205 		.rpc_message = &msg,
1206 		.callback_ops = call_ops,
1207 		.callback_data = data,
1208 		.workqueue = nfsiod_workqueue,
1209 		.flags = RPC_TASK_ASYNC | flags,
1210 		.priority = priority,
1211 	};
1212 	/* Set up the initial task struct.  */
1213 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
1214 
1215 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1216 
1217 	nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
1218 		NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
1219 
1220 	task = rpc_run_task(&task_setup_data);
1221 	if (IS_ERR(task))
1222 		return PTR_ERR(task);
1223 	if (how & FLUSH_SYNC)
1224 		rpc_wait_for_completion_task(task);
1225 	rpc_put_task(task);
1226 	return 0;
1227 }
1228 EXPORT_SYMBOL_GPL(nfs_initiate_commit);
1229 
1230 /*
1231  * Set up the argument/result storage required for the RPC call.
1232  */
1233 void nfs_init_commit(struct nfs_commit_data *data,
1234 		     struct list_head *head,
1235 		     struct pnfs_layout_segment *lseg,
1236 		     struct nfs_commit_info *cinfo)
1237 {
1238 	struct nfs_page *first = nfs_list_entry(head->next);
1239 	struct inode *inode = first->wb_context->dentry->d_inode;
1240 
1241 	/* Set up the RPC argument and reply structs
1242 	 * NB: take care not to mess about with data->commit et al. */
1243 
1244 	list_splice_init(head, &data->pages);
1245 
1246 	data->inode	  = inode;
1247 	data->cred	  = first->wb_context->cred;
1248 	data->lseg	  = lseg; /* reference transferred */
1249 	data->mds_ops     = &nfs_commit_ops;
1250 	data->completion_ops = cinfo->completion_ops;
1251 	data->dreq	  = cinfo->dreq;
1252 
1253 	data->args.fh     = NFS_FH(data->inode);
1254 	/* Note: we always request a commit of the entire inode */
1255 	data->args.offset = 0;
1256 	data->args.count  = 0;
1257 	data->context     = get_nfs_open_context(first->wb_context);
1258 	data->res.fattr   = &data->fattr;
1259 	data->res.verf    = &data->verf;
1260 	nfs_fattr_init(&data->fattr);
1261 }
1262 EXPORT_SYMBOL_GPL(nfs_init_commit);
1263 
1264 void nfs_retry_commit(struct list_head *page_list,
1265 		      struct pnfs_layout_segment *lseg,
1266 		      struct nfs_commit_info *cinfo)
1267 {
1268 	struct nfs_page *req;
1269 
1270 	while (!list_empty(page_list)) {
1271 		req = nfs_list_entry(page_list->next);
1272 		nfs_list_remove_request(req);
1273 		nfs_mark_request_commit(req, lseg, cinfo);
1274 		if (!cinfo->dreq) {
1275 			dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1276 			dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
1277 				     BDI_RECLAIMABLE);
1278 		}
1279 		nfs_unlock_and_release_request(req);
1280 	}
1281 }
1282 EXPORT_SYMBOL_GPL(nfs_retry_commit);
1283 
1284 /*
1285  * Commit dirty pages
1286  */
1287 static int
1288 nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1289 		struct nfs_commit_info *cinfo)
1290 {
1291 	struct nfs_commit_data	*data;
1292 
1293 	data = nfs_commitdata_alloc();
1294 
1295 	if (!data)
1296 		goto out_bad;
1297 
1298 	/* Set up the argument struct */
1299 	nfs_init_commit(data, head, NULL, cinfo);
1300 	atomic_inc(&cinfo->mds->rpcs_out);
1301 	return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops,
1302 				   how, 0);
1303  out_bad:
1304 	nfs_retry_commit(head, NULL, cinfo);
1305 	cinfo->completion_ops->error_cleanup(NFS_I(inode));
1306 	return -ENOMEM;
1307 }
1308 
1309 /*
1310  * COMMIT call returned
1311  */
1312 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1313 {
1314 	struct nfs_commit_data	*data = calldata;
1315 
1316         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1317                                 task->tk_pid, task->tk_status);
1318 
1319 	/* Call the NFS version-specific code */
1320 	NFS_PROTO(data->inode)->commit_done(task, data);
1321 }
1322 
1323 static void nfs_commit_release_pages(struct nfs_commit_data *data)
1324 {
1325 	struct nfs_page	*req;
1326 	int status = data->task.tk_status;
1327 	struct nfs_commit_info cinfo;
1328 
1329 	while (!list_empty(&data->pages)) {
1330 		req = nfs_list_entry(data->pages.next);
1331 		nfs_list_remove_request(req);
1332 		nfs_clear_page_commit(req->wb_page);
1333 
1334 		dprintk("NFS:       commit (%s/%llu %d@%lld)",
1335 			req->wb_context->dentry->d_sb->s_id,
1336 			(unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1337 			req->wb_bytes,
1338 			(long long)req_offset(req));
1339 		if (status < 0) {
1340 			nfs_context_set_write_error(req->wb_context, status);
1341 			nfs_inode_remove_request(req);
1342 			dprintk(", error = %d\n", status);
1343 			goto next;
1344 		}
1345 
1346 		/* Okay, COMMIT succeeded, apparently. Check the verifier
1347 		 * returned by the server against all stored verfs. */
1348 		if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) {
1349 			/* We have a match */
1350 			nfs_inode_remove_request(req);
1351 			dprintk(" OK\n");
1352 			goto next;
1353 		}
1354 		/* We have a mismatch. Write the page again */
1355 		dprintk(" mismatch\n");
1356 		nfs_mark_request_dirty(req);
1357 		set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
1358 	next:
1359 		nfs_unlock_and_release_request(req);
1360 	}
1361 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1362 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
1363 		nfs_commit_clear_lock(NFS_I(data->inode));
1364 }
1365 
1366 static void nfs_commit_release(void *calldata)
1367 {
1368 	struct nfs_commit_data *data = calldata;
1369 
1370 	data->completion_ops->completion(data);
1371 	nfs_commitdata_release(calldata);
1372 }
1373 
1374 static const struct rpc_call_ops nfs_commit_ops = {
1375 	.rpc_call_prepare = nfs_commit_prepare,
1376 	.rpc_call_done = nfs_commit_done,
1377 	.rpc_release = nfs_commit_release,
1378 };
1379 
1380 static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1381 	.completion = nfs_commit_release_pages,
1382 	.error_cleanup = nfs_commit_clear_lock,
1383 };
1384 
1385 int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1386 			    int how, struct nfs_commit_info *cinfo)
1387 {
1388 	int status;
1389 
1390 	status = pnfs_commit_list(inode, head, how, cinfo);
1391 	if (status == PNFS_NOT_ATTEMPTED)
1392 		status = nfs_commit_list(inode, head, how, cinfo);
1393 	return status;
1394 }
1395 
1396 int nfs_commit_inode(struct inode *inode, int how)
1397 {
1398 	LIST_HEAD(head);
1399 	struct nfs_commit_info cinfo;
1400 	int may_wait = how & FLUSH_SYNC;
1401 	int res;
1402 
1403 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1404 	if (res <= 0)
1405 		goto out_mark_dirty;
1406 	nfs_init_cinfo_from_inode(&cinfo, inode);
1407 	res = nfs_scan_commit(inode, &head, &cinfo);
1408 	if (res) {
1409 		int error;
1410 
1411 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
1412 		if (error < 0)
1413 			return error;
1414 		if (!may_wait)
1415 			goto out_mark_dirty;
1416 		error = wait_on_bit(&NFS_I(inode)->flags,
1417 				NFS_INO_COMMIT,
1418 				nfs_wait_bit_killable,
1419 				TASK_KILLABLE);
1420 		if (error < 0)
1421 			return error;
1422 	} else
1423 		nfs_commit_clear_lock(NFS_I(inode));
1424 	return res;
1425 	/* Note: If we exit without ensuring that the commit is complete,
1426 	 * we must mark the inode as dirty. Otherwise, future calls to
1427 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1428 	 * that the data is on the disk.
1429 	 */
1430 out_mark_dirty:
1431 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1432 	return res;
1433 }
1434 
1435 static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
1436 {
1437 	struct nfs_inode *nfsi = NFS_I(inode);
1438 	int flags = FLUSH_SYNC;
1439 	int ret = 0;
1440 
1441 	/* no commits means nothing needs to be done */
1442 	if (!nfsi->commit_info.ncommit)
1443 		return ret;
1444 
1445 	if (wbc->sync_mode == WB_SYNC_NONE) {
1446 		/* Don't commit yet if this is a non-blocking flush and there
1447 		 * are a lot of outstanding writes for this mapping.
1448 		 */
1449 		if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
1450 			goto out_mark_dirty;
1451 
1452 		/* don't wait for the COMMIT response */
1453 		flags = 0;
1454 	}
1455 
1456 	ret = nfs_commit_inode(inode, flags);
1457 	if (ret >= 0) {
1458 		if (wbc->sync_mode == WB_SYNC_NONE) {
1459 			if (ret < wbc->nr_to_write)
1460 				wbc->nr_to_write -= ret;
1461 			else
1462 				wbc->nr_to_write = 0;
1463 		}
1464 		return 0;
1465 	}
1466 out_mark_dirty:
1467 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1468 	return ret;
1469 }
1470 #else
1471 static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
1472 {
1473 	return 0;
1474 }
1475 #endif
1476 
1477 int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
1478 {
1479 	return nfs_commit_unstable_pages(inode, wbc);
1480 }
1481 EXPORT_SYMBOL_GPL(nfs_write_inode);
1482 
1483 /*
1484  * flush the inode to disk.
1485  */
1486 int nfs_wb_all(struct inode *inode)
1487 {
1488 	struct writeback_control wbc = {
1489 		.sync_mode = WB_SYNC_ALL,
1490 		.nr_to_write = LONG_MAX,
1491 		.range_start = 0,
1492 		.range_end = LLONG_MAX,
1493 	};
1494 	int ret;
1495 
1496 	trace_nfs_writeback_inode_enter(inode);
1497 
1498 	ret = sync_inode(inode, &wbc);
1499 
1500 	trace_nfs_writeback_inode_exit(inode, ret);
1501 	return ret;
1502 }
1503 EXPORT_SYMBOL_GPL(nfs_wb_all);
1504 
1505 int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1506 {
1507 	struct nfs_page *req;
1508 	int ret = 0;
1509 
1510 	for (;;) {
1511 		wait_on_page_writeback(page);
1512 		req = nfs_page_find_request(page);
1513 		if (req == NULL)
1514 			break;
1515 		if (nfs_lock_request(req)) {
1516 			nfs_clear_request_commit(req);
1517 			nfs_inode_remove_request(req);
1518 			/*
1519 			 * In case nfs_inode_remove_request has marked the
1520 			 * page as being dirty
1521 			 */
1522 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
1523 			nfs_unlock_and_release_request(req);
1524 			break;
1525 		}
1526 		ret = nfs_wait_on_request(req);
1527 		nfs_release_request(req);
1528 		if (ret < 0)
1529 			break;
1530 	}
1531 	return ret;
1532 }
1533 
1534 /*
1535  * Write back all requests on one page - we do this before reading it.
1536  */
1537 int nfs_wb_page(struct inode *inode, struct page *page)
1538 {
1539 	loff_t range_start = page_file_offset(page);
1540 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1541 	struct writeback_control wbc = {
1542 		.sync_mode = WB_SYNC_ALL,
1543 		.nr_to_write = 0,
1544 		.range_start = range_start,
1545 		.range_end = range_end,
1546 	};
1547 	int ret;
1548 
1549 	trace_nfs_writeback_page_enter(inode);
1550 
1551 	for (;;) {
1552 		wait_on_page_writeback(page);
1553 		if (clear_page_dirty_for_io(page)) {
1554 			ret = nfs_writepage_locked(page, &wbc);
1555 			if (ret < 0)
1556 				goto out_error;
1557 			continue;
1558 		}
1559 		ret = 0;
1560 		if (!PagePrivate(page))
1561 			break;
1562 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
1563 		if (ret < 0)
1564 			goto out_error;
1565 	}
1566 out_error:
1567 	trace_nfs_writeback_page_exit(inode, ret);
1568 	return ret;
1569 }
1570 
1571 #ifdef CONFIG_MIGRATION
1572 int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1573 		struct page *page, enum migrate_mode mode)
1574 {
1575 	/*
1576 	 * If PagePrivate is set, then the page is currently associated with
1577 	 * an in-progress read or write request. Don't try to migrate it.
1578 	 *
1579 	 * FIXME: we could do this in principle, but we'll need a way to ensure
1580 	 *        that we can safely release the inode reference while holding
1581 	 *        the page lock.
1582 	 */
1583 	if (PagePrivate(page))
1584 		return -EBUSY;
1585 
1586 	if (!nfs_fscache_release_page(page, GFP_KERNEL))
1587 		return -EBUSY;
1588 
1589 	return migrate_page(mapping, newpage, page, mode);
1590 }
1591 #endif
1592 
1593 int __init nfs_init_writepagecache(void)
1594 {
1595 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1596 					     sizeof(struct nfs_rw_header),
1597 					     0, SLAB_HWCACHE_ALIGN,
1598 					     NULL);
1599 	if (nfs_wdata_cachep == NULL)
1600 		return -ENOMEM;
1601 
1602 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1603 						     nfs_wdata_cachep);
1604 	if (nfs_wdata_mempool == NULL)
1605 		goto out_destroy_write_cache;
1606 
1607 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
1608 					     sizeof(struct nfs_commit_data),
1609 					     0, SLAB_HWCACHE_ALIGN,
1610 					     NULL);
1611 	if (nfs_cdata_cachep == NULL)
1612 		goto out_destroy_write_mempool;
1613 
1614 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1615 						      nfs_cdata_cachep);
1616 	if (nfs_commit_mempool == NULL)
1617 		goto out_destroy_commit_cache;
1618 
1619 	/*
1620 	 * NFS congestion size, scale with available memory.
1621 	 *
1622 	 *  64MB:    8192k
1623 	 * 128MB:   11585k
1624 	 * 256MB:   16384k
1625 	 * 512MB:   23170k
1626 	 *   1GB:   32768k
1627 	 *   2GB:   46340k
1628 	 *   4GB:   65536k
1629 	 *   8GB:   92681k
1630 	 *  16GB:  131072k
1631 	 *
1632 	 * This allows larger machines to have larger/more transfers.
1633 	 * Limit the default to 256M
1634 	 */
1635 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1636 	if (nfs_congestion_kb > 256*1024)
1637 		nfs_congestion_kb = 256*1024;
1638 
1639 	return 0;
1640 
1641 out_destroy_commit_cache:
1642 	kmem_cache_destroy(nfs_cdata_cachep);
1643 out_destroy_write_mempool:
1644 	mempool_destroy(nfs_wdata_mempool);
1645 out_destroy_write_cache:
1646 	kmem_cache_destroy(nfs_wdata_cachep);
1647 	return -ENOMEM;
1648 }
1649 
1650 void nfs_destroy_writepagecache(void)
1651 {
1652 	mempool_destroy(nfs_commit_mempool);
1653 	kmem_cache_destroy(nfs_cdata_cachep);
1654 	mempool_destroy(nfs_wdata_mempool);
1655 	kmem_cache_destroy(nfs_wdata_cachep);
1656 }
1657 
1658 static const struct nfs_rw_ops nfs_rw_write_ops = {
1659 	.rw_mode		= FMODE_WRITE,
1660 	.rw_alloc_header	= nfs_writehdr_alloc,
1661 	.rw_free_header		= nfs_writehdr_free,
1662 	.rw_release		= nfs_writeback_release_common,
1663 	.rw_done		= nfs_writeback_done,
1664 	.rw_result		= nfs_writeback_result,
1665 	.rw_initiate		= nfs_initiate_write,
1666 };
1667