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