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