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