blob: 613c3ef23e07b2617eee2973c2a0561fd275ab48 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * linux/fs/nfs/write.c
4 *
5 * Write file data over NFS.
6 *
7 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
8 */
9
10#include <linux/types.h>
11#include <linux/slab.h>
12#include <linux/mm.h>
13#include <linux/pagemap.h>
14#include <linux/file.h>
15#include <linux/writeback.h>
16#include <linux/swap.h>
17#include <linux/migrate.h>
18
19#include <linux/sunrpc/clnt.h>
20#include <linux/nfs_fs.h>
21#include <linux/nfs_mount.h>
22#include <linux/nfs_page.h>
23#include <linux/backing-dev.h>
24#include <linux/export.h>
25#include <linux/freezer.h>
26#include <linux/wait.h>
27#include <linux/iversion.h>
28
29#include <linux/uaccess.h>
David Brazdil0f672f62019-12-10 10:32:29 +000030#include <linux/sched/mm.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000031
32#include "delegation.h"
33#include "internal.h"
34#include "iostat.h"
35#include "nfs4_fs.h"
36#include "fscache.h"
37#include "pnfs.h"
38
39#include "nfstrace.h"
40
41#define NFSDBG_FACILITY NFSDBG_PAGECACHE
42
43#define MIN_POOL_WRITE (32)
44#define MIN_POOL_COMMIT (4)
45
46struct nfs_io_completion {
47 void (*complete)(void *data);
48 void *data;
49 struct kref refcount;
50};
51
52/*
53 * Local function declarations
54 */
55static void nfs_redirty_request(struct nfs_page *req);
56static const struct rpc_call_ops nfs_commit_ops;
57static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
58static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
59static const struct nfs_rw_ops nfs_rw_write_ops;
David Brazdil0f672f62019-12-10 10:32:29 +000060static void nfs_inode_remove_request(struct nfs_page *req);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000061static void nfs_clear_request_commit(struct nfs_page *req);
62static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
63 struct inode *inode);
64static struct nfs_page *
65nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
66 struct page *page);
67
68static struct kmem_cache *nfs_wdata_cachep;
69static mempool_t *nfs_wdata_mempool;
70static struct kmem_cache *nfs_cdata_cachep;
71static mempool_t *nfs_commit_mempool;
72
73struct nfs_commit_data *nfs_commitdata_alloc(bool never_fail)
74{
75 struct nfs_commit_data *p;
76
77 if (never_fail)
78 p = mempool_alloc(nfs_commit_mempool, GFP_NOIO);
79 else {
80 /* It is OK to do some reclaim, not no safe to wait
81 * for anything to be returned to the pool.
82 * mempool_alloc() cannot handle that particular combination,
83 * so we need two separate attempts.
84 */
85 p = mempool_alloc(nfs_commit_mempool, GFP_NOWAIT);
86 if (!p)
87 p = kmem_cache_alloc(nfs_cdata_cachep, GFP_NOIO |
88 __GFP_NOWARN | __GFP_NORETRY);
89 if (!p)
90 return NULL;
91 }
92
93 memset(p, 0, sizeof(*p));
94 INIT_LIST_HEAD(&p->pages);
95 return p;
96}
97EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
98
99void nfs_commit_free(struct nfs_commit_data *p)
100{
101 mempool_free(p, nfs_commit_mempool);
102}
103EXPORT_SYMBOL_GPL(nfs_commit_free);
104
105static struct nfs_pgio_header *nfs_writehdr_alloc(void)
106{
David Brazdil0f672f62019-12-10 10:32:29 +0000107 struct nfs_pgio_header *p = mempool_alloc(nfs_wdata_mempool, GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000108
109 memset(p, 0, sizeof(*p));
110 p->rw_mode = FMODE_WRITE;
111 return p;
112}
113
114static void nfs_writehdr_free(struct nfs_pgio_header *hdr)
115{
116 mempool_free(hdr, nfs_wdata_mempool);
117}
118
119static struct nfs_io_completion *nfs_io_completion_alloc(gfp_t gfp_flags)
120{
121 return kmalloc(sizeof(struct nfs_io_completion), gfp_flags);
122}
123
124static void nfs_io_completion_init(struct nfs_io_completion *ioc,
125 void (*complete)(void *), void *data)
126{
127 ioc->complete = complete;
128 ioc->data = data;
129 kref_init(&ioc->refcount);
130}
131
132static void nfs_io_completion_release(struct kref *kref)
133{
134 struct nfs_io_completion *ioc = container_of(kref,
135 struct nfs_io_completion, refcount);
136 ioc->complete(ioc->data);
137 kfree(ioc);
138}
139
140static void nfs_io_completion_get(struct nfs_io_completion *ioc)
141{
142 if (ioc != NULL)
143 kref_get(&ioc->refcount);
144}
145
146static void nfs_io_completion_put(struct nfs_io_completion *ioc)
147{
148 if (ioc != NULL)
149 kref_put(&ioc->refcount, nfs_io_completion_release);
150}
151
152static struct nfs_page *
153nfs_page_private_request(struct page *page)
154{
155 if (!PagePrivate(page))
156 return NULL;
157 return (struct nfs_page *)page_private(page);
158}
159
160/*
161 * nfs_page_find_head_request_locked - find head request associated with @page
162 *
163 * must be called while holding the inode lock.
164 *
165 * returns matching head request with reference held, or NULL if not found.
166 */
167static struct nfs_page *
168nfs_page_find_private_request(struct page *page)
169{
170 struct address_space *mapping = page_file_mapping(page);
171 struct nfs_page *req;
172
173 if (!PagePrivate(page))
174 return NULL;
175 spin_lock(&mapping->private_lock);
176 req = nfs_page_private_request(page);
177 if (req) {
178 WARN_ON_ONCE(req->wb_head != req);
179 kref_get(&req->wb_kref);
180 }
181 spin_unlock(&mapping->private_lock);
182 return req;
183}
184
185static struct nfs_page *
186nfs_page_find_swap_request(struct page *page)
187{
188 struct inode *inode = page_file_mapping(page)->host;
189 struct nfs_inode *nfsi = NFS_I(inode);
190 struct nfs_page *req = NULL;
191 if (!PageSwapCache(page))
192 return NULL;
193 mutex_lock(&nfsi->commit_mutex);
194 if (PageSwapCache(page)) {
195 req = nfs_page_search_commits_for_head_request_locked(nfsi,
196 page);
197 if (req) {
198 WARN_ON_ONCE(req->wb_head != req);
199 kref_get(&req->wb_kref);
200 }
201 }
202 mutex_unlock(&nfsi->commit_mutex);
203 return req;
204}
205
206/*
207 * nfs_page_find_head_request - find head request associated with @page
208 *
209 * returns matching head request with reference held, or NULL if not found.
210 */
211static struct nfs_page *nfs_page_find_head_request(struct page *page)
212{
213 struct nfs_page *req;
214
215 req = nfs_page_find_private_request(page);
216 if (!req)
217 req = nfs_page_find_swap_request(page);
218 return req;
219}
220
221/* Adjust the file length if we're writing beyond the end */
222static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
223{
224 struct inode *inode = page_file_mapping(page)->host;
225 loff_t end, i_size;
226 pgoff_t end_index;
227
228 spin_lock(&inode->i_lock);
229 i_size = i_size_read(inode);
230 end_index = (i_size - 1) >> PAGE_SHIFT;
231 if (i_size > 0 && page_index(page) < end_index)
232 goto out;
233 end = page_file_offset(page) + ((loff_t)offset+count);
234 if (i_size >= end)
235 goto out;
236 i_size_write(inode, end);
237 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
238 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
239out:
240 spin_unlock(&inode->i_lock);
241}
242
243/* A writeback failed: mark the page as bad, and invalidate the page cache */
David Brazdil0f672f62019-12-10 10:32:29 +0000244static void nfs_set_pageerror(struct address_space *mapping)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000245{
Olivier Deprez0e641232021-09-23 10:07:05 +0200246 struct inode *inode = mapping->host;
247
David Brazdil0f672f62019-12-10 10:32:29 +0000248 nfs_zap_mapping(mapping->host, mapping);
Olivier Deprez0e641232021-09-23 10:07:05 +0200249 /* Force file size revalidation */
250 spin_lock(&inode->i_lock);
251 NFS_I(inode)->cache_validity |= NFS_INO_REVAL_FORCED |
252 NFS_INO_REVAL_PAGECACHE |
253 NFS_INO_INVALID_SIZE;
254 spin_unlock(&inode->i_lock);
David Brazdil0f672f62019-12-10 10:32:29 +0000255}
256
257static void nfs_mapping_set_error(struct page *page, int error)
258{
259 SetPageError(page);
260 mapping_set_error(page_file_mapping(page), error);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000261}
262
263/*
264 * nfs_page_group_search_locked
265 * @head - head request of page group
266 * @page_offset - offset into page
267 *
268 * Search page group with head @head to find a request that contains the
269 * page offset @page_offset.
270 *
271 * Returns a pointer to the first matching nfs request, or NULL if no
272 * match is found.
273 *
274 * Must be called with the page group lock held
275 */
276static struct nfs_page *
277nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset)
278{
279 struct nfs_page *req;
280
281 req = head;
282 do {
283 if (page_offset >= req->wb_pgbase &&
284 page_offset < (req->wb_pgbase + req->wb_bytes))
285 return req;
286
287 req = req->wb_this_page;
288 } while (req != head);
289
290 return NULL;
291}
292
293/*
294 * nfs_page_group_covers_page
295 * @head - head request of page group
296 *
297 * Return true if the page group with head @head covers the whole page,
298 * returns false otherwise
299 */
300static bool nfs_page_group_covers_page(struct nfs_page *req)
301{
302 struct nfs_page *tmp;
303 unsigned int pos = 0;
304 unsigned int len = nfs_page_length(req->wb_page);
305
306 nfs_page_group_lock(req);
307
308 for (;;) {
309 tmp = nfs_page_group_search_locked(req->wb_head, pos);
310 if (!tmp)
311 break;
312 pos = tmp->wb_pgbase + tmp->wb_bytes;
313 }
314
315 nfs_page_group_unlock(req);
316 return pos >= len;
317}
318
319/* We can set the PG_uptodate flag if we see that a write request
320 * covers the full page.
321 */
322static void nfs_mark_uptodate(struct nfs_page *req)
323{
324 if (PageUptodate(req->wb_page))
325 return;
326 if (!nfs_page_group_covers_page(req))
327 return;
328 SetPageUptodate(req->wb_page);
329}
330
331static int wb_priority(struct writeback_control *wbc)
332{
333 int ret = 0;
334
335 if (wbc->sync_mode == WB_SYNC_ALL)
336 ret = FLUSH_COND_STABLE;
337 return ret;
338}
339
340/*
341 * NFS congestion control
342 */
343
344int nfs_congestion_kb;
345
346#define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
347#define NFS_CONGESTION_OFF_THRESH \
348 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
349
350static void nfs_set_page_writeback(struct page *page)
351{
352 struct inode *inode = page_file_mapping(page)->host;
353 struct nfs_server *nfss = NFS_SERVER(inode);
354 int ret = test_set_page_writeback(page);
355
356 WARN_ON_ONCE(ret != 0);
357
358 if (atomic_long_inc_return(&nfss->writeback) >
359 NFS_CONGESTION_ON_THRESH)
360 set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
361}
362
363static void nfs_end_page_writeback(struct nfs_page *req)
364{
365 struct inode *inode = page_file_mapping(req->wb_page)->host;
366 struct nfs_server *nfss = NFS_SERVER(inode);
367 bool is_done;
368
369 is_done = nfs_page_group_sync_on_bit(req, PG_WB_END);
370 nfs_unlock_request(req);
371 if (!is_done)
372 return;
373
374 end_page_writeback(req->wb_page);
375 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
376 clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
377}
378
379/*
380 * nfs_unroll_locks_and_wait - unlock all newly locked reqs and wait on @req
381 *
382 * this is a helper function for nfs_lock_and_join_requests
383 *
384 * @inode - inode associated with request page group, must be holding inode lock
385 * @head - head request of page group, must be holding head lock
386 * @req - request that couldn't lock and needs to wait on the req bit lock
387 *
388 * NOTE: this must be called holding page_group bit lock
389 * which will be released before returning.
390 *
391 * returns 0 on success, < 0 on error.
392 */
393static void
394nfs_unroll_locks(struct inode *inode, struct nfs_page *head,
395 struct nfs_page *req)
396{
397 struct nfs_page *tmp;
398
399 /* relinquish all the locks successfully grabbed this run */
400 for (tmp = head->wb_this_page ; tmp != req; tmp = tmp->wb_this_page) {
401 if (!kref_read(&tmp->wb_kref))
402 continue;
403 nfs_unlock_and_release_request(tmp);
404 }
405}
406
407/*
408 * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
409 *
410 * @destroy_list - request list (using wb_this_page) terminated by @old_head
411 * @old_head - the old head of the list
412 *
413 * All subrequests must be locked and removed from all lists, so at this point
414 * they are only "active" in this function, and possibly in nfs_wait_on_request
415 * with a reference held by some other context.
416 */
417static void
418nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
419 struct nfs_page *old_head,
420 struct inode *inode)
421{
422 while (destroy_list) {
423 struct nfs_page *subreq = destroy_list;
424
425 destroy_list = (subreq->wb_this_page == old_head) ?
426 NULL : subreq->wb_this_page;
427
Olivier Deprez0e641232021-09-23 10:07:05 +0200428 /* Note: lock subreq in order to change subreq->wb_head */
429 nfs_page_set_headlock(subreq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000430 WARN_ON_ONCE(old_head != subreq->wb_head);
431
432 /* make sure old group is not used */
433 subreq->wb_this_page = subreq;
Olivier Deprez0e641232021-09-23 10:07:05 +0200434 subreq->wb_head = subreq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000435
436 clear_bit(PG_REMOVE, &subreq->wb_flags);
437
438 /* Note: races with nfs_page_group_destroy() */
439 if (!kref_read(&subreq->wb_kref)) {
440 /* Check if we raced with nfs_page_group_destroy() */
Olivier Deprez0e641232021-09-23 10:07:05 +0200441 if (test_and_clear_bit(PG_TEARDOWN, &subreq->wb_flags)) {
442 nfs_page_clear_headlock(subreq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000443 nfs_free_request(subreq);
Olivier Deprez0e641232021-09-23 10:07:05 +0200444 } else
445 nfs_page_clear_headlock(subreq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000446 continue;
447 }
Olivier Deprez0e641232021-09-23 10:07:05 +0200448 nfs_page_clear_headlock(subreq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000449
Olivier Deprez0e641232021-09-23 10:07:05 +0200450 nfs_release_request(old_head);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000451
452 if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags)) {
453 nfs_release_request(subreq);
454 atomic_long_dec(&NFS_I(inode)->nrequests);
455 }
456
457 /* subreq is now totally disconnected from page group or any
458 * write / commit lists. last chance to wake any waiters */
459 nfs_unlock_and_release_request(subreq);
460 }
461}
462
463/*
464 * nfs_lock_and_join_requests - join all subreqs to the head req and return
465 * a locked reference, cancelling any pending
466 * operations for this page.
467 *
468 * @page - the page used to lookup the "page group" of nfs_page structures
469 *
470 * This function joins all sub requests to the head request by first
471 * locking all requests in the group, cancelling any pending operations
472 * and finally updating the head request to cover the whole range covered by
473 * the (former) group. All subrequests are removed from any write or commit
474 * lists, unlinked from the group and destroyed.
475 *
476 * Returns a locked, referenced pointer to the head request - which after
477 * this call is guaranteed to be the only request associated with the page.
478 * Returns NULL if no requests are found for @page, or a ERR_PTR if an
479 * error was encountered.
480 */
481static struct nfs_page *
482nfs_lock_and_join_requests(struct page *page)
483{
484 struct inode *inode = page_file_mapping(page)->host;
485 struct nfs_page *head, *subreq;
486 struct nfs_page *destroy_list = NULL;
487 unsigned int total_bytes;
488 int ret;
489
490try_again:
491 /*
492 * A reference is taken only on the head request which acts as a
493 * reference to the whole page group - the group will not be destroyed
494 * until the head reference is released.
495 */
496 head = nfs_page_find_head_request(page);
497 if (!head)
498 return NULL;
499
500 /* lock the page head first in order to avoid an ABBA inefficiency */
501 if (!nfs_lock_request(head)) {
502 ret = nfs_wait_on_request(head);
503 nfs_release_request(head);
504 if (ret < 0)
505 return ERR_PTR(ret);
506 goto try_again;
507 }
508
509 /* Ensure that nobody removed the request before we locked it */
510 if (head != nfs_page_private_request(page) && !PageSwapCache(page)) {
511 nfs_unlock_and_release_request(head);
512 goto try_again;
513 }
514
515 ret = nfs_page_group_lock(head);
516 if (ret < 0)
517 goto release_request;
518
519 /* lock each request in the page group */
520 total_bytes = head->wb_bytes;
521 for (subreq = head->wb_this_page; subreq != head;
522 subreq = subreq->wb_this_page) {
523
524 if (!kref_get_unless_zero(&subreq->wb_kref)) {
525 if (subreq->wb_offset == head->wb_offset + total_bytes)
526 total_bytes += subreq->wb_bytes;
527 continue;
528 }
529
530 while (!nfs_lock_request(subreq)) {
531 /*
532 * Unlock page to allow nfs_page_group_sync_on_bit()
533 * to succeed
534 */
535 nfs_page_group_unlock(head);
536 ret = nfs_wait_on_request(subreq);
537 if (!ret)
538 ret = nfs_page_group_lock(head);
539 if (ret < 0) {
540 nfs_unroll_locks(inode, head, subreq);
541 nfs_release_request(subreq);
542 goto release_request;
543 }
544 }
545 /*
546 * Subrequests are always contiguous, non overlapping
547 * and in order - but may be repeated (mirrored writes).
548 */
549 if (subreq->wb_offset == (head->wb_offset + total_bytes)) {
550 /* keep track of how many bytes this group covers */
551 total_bytes += subreq->wb_bytes;
552 } else if (WARN_ON_ONCE(subreq->wb_offset < head->wb_offset ||
553 ((subreq->wb_offset + subreq->wb_bytes) >
554 (head->wb_offset + total_bytes)))) {
555 nfs_page_group_unlock(head);
556 nfs_unroll_locks(inode, head, subreq);
557 nfs_unlock_and_release_request(subreq);
558 ret = -EIO;
559 goto release_request;
560 }
561 }
562
563 /* Now that all requests are locked, make sure they aren't on any list.
564 * Commit list removal accounting is done after locks are dropped */
565 subreq = head;
566 do {
567 nfs_clear_request_commit(subreq);
568 subreq = subreq->wb_this_page;
569 } while (subreq != head);
570
571 /* unlink subrequests from head, destroy them later */
572 if (head->wb_this_page != head) {
573 /* destroy list will be terminated by head */
574 destroy_list = head->wb_this_page;
575 head->wb_this_page = head;
576
577 /* change head request to cover whole range that
578 * the former page group covered */
579 head->wb_bytes = total_bytes;
580 }
581
582 /* Postpone destruction of this request */
583 if (test_and_clear_bit(PG_REMOVE, &head->wb_flags)) {
584 set_bit(PG_INODE_REF, &head->wb_flags);
585 kref_get(&head->wb_kref);
586 atomic_long_inc(&NFS_I(inode)->nrequests);
587 }
588
589 nfs_page_group_unlock(head);
590
591 nfs_destroy_unlinked_subrequests(destroy_list, head, inode);
592
593 /* Did we lose a race with nfs_inode_remove_request()? */
594 if (!(PagePrivate(page) || PageSwapCache(page))) {
595 nfs_unlock_and_release_request(head);
596 return NULL;
597 }
598
599 /* still holds ref on head from nfs_page_find_head_request
600 * and still has lock on head from lock loop */
601 return head;
602
603release_request:
604 nfs_unlock_and_release_request(head);
605 return ERR_PTR(ret);
606}
607
David Brazdil0f672f62019-12-10 10:32:29 +0000608static void nfs_write_error(struct nfs_page *req, int error)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000609{
David Brazdil0f672f62019-12-10 10:32:29 +0000610 nfs_set_pageerror(page_file_mapping(req->wb_page));
611 nfs_mapping_set_error(req->wb_page, error);
612 nfs_inode_remove_request(req);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000613 nfs_end_page_writeback(req);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000614 nfs_release_request(req);
615}
616
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000617/*
618 * Find an associated nfs write request, and prepare to flush it out
619 * May return an error if the user signalled nfs_wait_on_request().
620 */
621static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
622 struct page *page)
623{
624 struct nfs_page *req;
625 int ret = 0;
626
627 req = nfs_lock_and_join_requests(page);
628 if (!req)
629 goto out;
630 ret = PTR_ERR(req);
631 if (IS_ERR(req))
632 goto out;
633
634 nfs_set_page_writeback(page);
635 WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
636
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000637 /* If there is a fatal error that covers this write, just exit */
David Brazdil0f672f62019-12-10 10:32:29 +0000638 ret = pgio->pg_error;
639 if (nfs_error_is_fatal_on_server(ret))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000640 goto out_launder;
641
David Brazdil0f672f62019-12-10 10:32:29 +0000642 ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000643 if (!nfs_pageio_add_request(pgio, req)) {
644 ret = pgio->pg_error;
645 /*
646 * Remove the problematic req upon fatal errors on the server
647 */
648 if (nfs_error_is_fatal(ret)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000649 if (nfs_error_is_fatal_on_server(ret))
650 goto out_launder;
David Brazdil0f672f62019-12-10 10:32:29 +0000651 } else
652 ret = -EAGAIN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000653 nfs_redirty_request(req);
David Brazdil0f672f62019-12-10 10:32:29 +0000654 pgio->pg_error = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000655 } else
656 nfs_add_stats(page_file_mapping(page)->host,
657 NFSIOS_WRITEPAGES, 1);
658out:
659 return ret;
660out_launder:
David Brazdil0f672f62019-12-10 10:32:29 +0000661 nfs_write_error(req, ret);
662 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000663}
664
665static int nfs_do_writepage(struct page *page, struct writeback_control *wbc,
666 struct nfs_pageio_descriptor *pgio)
667{
668 int ret;
669
670 nfs_pageio_cond_complete(pgio, page_index(page));
671 ret = nfs_page_async_flush(pgio, page);
672 if (ret == -EAGAIN) {
673 redirty_page_for_writepage(wbc, page);
David Brazdil0f672f62019-12-10 10:32:29 +0000674 ret = AOP_WRITEPAGE_ACTIVATE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000675 }
676 return ret;
677}
678
679/*
680 * Write an mmapped page to the server.
681 */
682static int nfs_writepage_locked(struct page *page,
683 struct writeback_control *wbc)
684{
685 struct nfs_pageio_descriptor pgio;
686 struct inode *inode = page_file_mapping(page)->host;
687 int err;
688
689 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
690 nfs_pageio_init_write(&pgio, inode, 0,
691 false, &nfs_async_write_completion_ops);
692 err = nfs_do_writepage(page, wbc, &pgio);
David Brazdil0f672f62019-12-10 10:32:29 +0000693 pgio.pg_error = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000694 nfs_pageio_complete(&pgio);
695 if (err < 0)
696 return err;
David Brazdil0f672f62019-12-10 10:32:29 +0000697 if (nfs_error_is_fatal(pgio.pg_error))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000698 return pgio.pg_error;
699 return 0;
700}
701
702int nfs_writepage(struct page *page, struct writeback_control *wbc)
703{
704 int ret;
705
706 ret = nfs_writepage_locked(page, wbc);
David Brazdil0f672f62019-12-10 10:32:29 +0000707 if (ret != AOP_WRITEPAGE_ACTIVATE)
708 unlock_page(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000709 return ret;
710}
711
712static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
713{
714 int ret;
715
716 ret = nfs_do_writepage(page, wbc, data);
David Brazdil0f672f62019-12-10 10:32:29 +0000717 if (ret != AOP_WRITEPAGE_ACTIVATE)
718 unlock_page(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000719 return ret;
720}
721
722static void nfs_io_completion_commit(void *inode)
723{
724 nfs_commit_inode(inode, 0);
725}
726
727int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
728{
729 struct inode *inode = mapping->host;
730 struct nfs_pageio_descriptor pgio;
David Brazdil0f672f62019-12-10 10:32:29 +0000731 struct nfs_io_completion *ioc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000732 int err;
733
734 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
735
David Brazdil0f672f62019-12-10 10:32:29 +0000736 ioc = nfs_io_completion_alloc(GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000737 if (ioc)
738 nfs_io_completion_init(ioc, nfs_io_completion_commit, inode);
739
740 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
741 &nfs_async_write_completion_ops);
742 pgio.pg_io_completion = ioc;
743 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
David Brazdil0f672f62019-12-10 10:32:29 +0000744 pgio.pg_error = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000745 nfs_pageio_complete(&pgio);
746 nfs_io_completion_put(ioc);
747
748 if (err < 0)
749 goto out_err;
750 err = pgio.pg_error;
David Brazdil0f672f62019-12-10 10:32:29 +0000751 if (nfs_error_is_fatal(err))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000752 goto out_err;
753 return 0;
754out_err:
755 return err;
756}
757
758/*
759 * Insert a write request into an inode
760 */
761static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
762{
763 struct address_space *mapping = page_file_mapping(req->wb_page);
764 struct nfs_inode *nfsi = NFS_I(inode);
765
766 WARN_ON_ONCE(req->wb_this_page != req);
767
768 /* Lock the request! */
769 nfs_lock_request(req);
770
771 /*
772 * Swap-space should not get truncated. Hence no need to plug the race
773 * with invalidate/truncate.
774 */
775 spin_lock(&mapping->private_lock);
776 if (!nfs_have_writebacks(inode) &&
777 NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
778 inode_inc_iversion_raw(inode);
779 if (likely(!PageSwapCache(req->wb_page))) {
780 set_bit(PG_MAPPED, &req->wb_flags);
781 SetPagePrivate(req->wb_page);
782 set_page_private(req->wb_page, (unsigned long)req);
783 }
784 spin_unlock(&mapping->private_lock);
785 atomic_long_inc(&nfsi->nrequests);
786 /* this a head request for a page group - mark it as having an
787 * extra reference so sub groups can follow suit.
788 * This flag also informs pgio layer when to bump nrequests when
789 * adding subrequests. */
790 WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
791 kref_get(&req->wb_kref);
792}
793
794/*
795 * Remove a write request from an inode
796 */
797static void nfs_inode_remove_request(struct nfs_page *req)
798{
799 struct address_space *mapping = page_file_mapping(req->wb_page);
800 struct inode *inode = mapping->host;
801 struct nfs_inode *nfsi = NFS_I(inode);
802 struct nfs_page *head;
803
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000804 if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
805 head = req->wb_head;
806
807 spin_lock(&mapping->private_lock);
808 if (likely(head->wb_page && !PageSwapCache(head->wb_page))) {
809 set_page_private(head->wb_page, 0);
810 ClearPagePrivate(head->wb_page);
811 clear_bit(PG_MAPPED, &head->wb_flags);
812 }
813 spin_unlock(&mapping->private_lock);
814 }
815
David Brazdil0f672f62019-12-10 10:32:29 +0000816 if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000817 nfs_release_request(req);
David Brazdil0f672f62019-12-10 10:32:29 +0000818 atomic_long_dec(&nfsi->nrequests);
819 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000820}
821
822static void
823nfs_mark_request_dirty(struct nfs_page *req)
824{
825 if (req->wb_page)
826 __set_page_dirty_nobuffers(req->wb_page);
827}
828
829/*
830 * nfs_page_search_commits_for_head_request_locked
831 *
832 * Search through commit lists on @inode for the head request for @page.
833 * Must be called while holding the inode (which is cinfo) lock.
834 *
835 * Returns the head request if found, or NULL if not found.
836 */
837static struct nfs_page *
838nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
839 struct page *page)
840{
841 struct nfs_page *freq, *t;
842 struct nfs_commit_info cinfo;
843 struct inode *inode = &nfsi->vfs_inode;
844
845 nfs_init_cinfo_from_inode(&cinfo, inode);
846
847 /* search through pnfs commit lists */
848 freq = pnfs_search_commit_reqs(inode, &cinfo, page);
849 if (freq)
850 return freq->wb_head;
851
852 /* Linearly search the commit list for the correct request */
853 list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) {
854 if (freq->wb_page == page)
855 return freq->wb_head;
856 }
857
858 return NULL;
859}
860
861/**
862 * nfs_request_add_commit_list_locked - add request to a commit list
863 * @req: pointer to a struct nfs_page
864 * @dst: commit list head
865 * @cinfo: holds list lock and accounting info
866 *
867 * This sets the PG_CLEAN bit, updates the cinfo count of
868 * number of outstanding requests requiring a commit as well as
869 * the MM page stats.
870 *
871 * The caller must hold NFS_I(cinfo->inode)->commit_mutex, and the
872 * nfs_page lock.
873 */
874void
875nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst,
876 struct nfs_commit_info *cinfo)
877{
878 set_bit(PG_CLEAN, &req->wb_flags);
879 nfs_list_add_request(req, dst);
880 atomic_long_inc(&cinfo->mds->ncommit);
881}
882EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked);
883
884/**
885 * nfs_request_add_commit_list - add request to a commit list
886 * @req: pointer to a struct nfs_page
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000887 * @cinfo: holds list lock and accounting info
888 *
889 * This sets the PG_CLEAN bit, updates the cinfo count of
890 * number of outstanding requests requiring a commit as well as
891 * the MM page stats.
892 *
893 * The caller must _not_ hold the cinfo->lock, but must be
894 * holding the nfs_page lock.
895 */
896void
897nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo)
898{
899 mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
900 nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo);
901 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
902 if (req->wb_page)
903 nfs_mark_page_unstable(req->wb_page, cinfo);
904}
905EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
906
907/**
908 * nfs_request_remove_commit_list - Remove request from a commit list
909 * @req: pointer to a nfs_page
910 * @cinfo: holds list lock and accounting info
911 *
912 * This clears the PG_CLEAN bit, and updates the cinfo's count of
913 * number of outstanding requests requiring a commit
914 * It does not update the MM page stats.
915 *
916 * The caller _must_ hold the cinfo->lock and the nfs_page lock.
917 */
918void
919nfs_request_remove_commit_list(struct nfs_page *req,
920 struct nfs_commit_info *cinfo)
921{
922 if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
923 return;
924 nfs_list_remove_request(req);
925 atomic_long_dec(&cinfo->mds->ncommit);
926}
927EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
928
929static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
930 struct inode *inode)
931{
932 cinfo->inode = inode;
933 cinfo->mds = &NFS_I(inode)->commit_info;
934 cinfo->ds = pnfs_get_ds_info(inode);
935 cinfo->dreq = NULL;
936 cinfo->completion_ops = &nfs_commit_completion_ops;
937}
938
939void nfs_init_cinfo(struct nfs_commit_info *cinfo,
940 struct inode *inode,
941 struct nfs_direct_req *dreq)
942{
943 if (dreq)
944 nfs_init_cinfo_from_dreq(cinfo, dreq);
945 else
946 nfs_init_cinfo_from_inode(cinfo, inode);
947}
948EXPORT_SYMBOL_GPL(nfs_init_cinfo);
949
950/*
951 * Add a request to the inode's commit list.
952 */
953void
954nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
955 struct nfs_commit_info *cinfo, u32 ds_commit_idx)
956{
957 if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx))
958 return;
959 nfs_request_add_commit_list(req, cinfo);
960}
961
962static void
963nfs_clear_page_commit(struct page *page)
964{
965 dec_node_page_state(page, NR_UNSTABLE_NFS);
966 dec_wb_stat(&inode_to_bdi(page_file_mapping(page)->host)->wb,
967 WB_RECLAIMABLE);
968}
969
970/* Called holding the request lock on @req */
971static void
972nfs_clear_request_commit(struct nfs_page *req)
973{
974 if (test_bit(PG_CLEAN, &req->wb_flags)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000975 struct nfs_open_context *ctx = nfs_req_openctx(req);
976 struct inode *inode = d_inode(ctx->dentry);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000977 struct nfs_commit_info cinfo;
978
979 nfs_init_cinfo_from_inode(&cinfo, inode);
980 mutex_lock(&NFS_I(inode)->commit_mutex);
981 if (!pnfs_clear_request_commit(req, &cinfo)) {
982 nfs_request_remove_commit_list(req, &cinfo);
983 }
984 mutex_unlock(&NFS_I(inode)->commit_mutex);
985 nfs_clear_page_commit(req->wb_page);
986 }
987}
988
989int nfs_write_need_commit(struct nfs_pgio_header *hdr)
990{
991 if (hdr->verf.committed == NFS_DATA_SYNC)
992 return hdr->lseg == NULL;
993 return hdr->verf.committed != NFS_FILE_SYNC;
994}
995
996static void nfs_async_write_init(struct nfs_pgio_header *hdr)
997{
998 nfs_io_completion_get(hdr->io_completion);
999}
1000
1001static void nfs_write_completion(struct nfs_pgio_header *hdr)
1002{
1003 struct nfs_commit_info cinfo;
1004 unsigned long bytes = 0;
1005
1006 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
1007 goto out;
1008 nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
1009 while (!list_empty(&hdr->pages)) {
1010 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
1011
1012 bytes += req->wb_bytes;
1013 nfs_list_remove_request(req);
1014 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
1015 (hdr->good_bytes < bytes)) {
David Brazdil0f672f62019-12-10 10:32:29 +00001016 nfs_set_pageerror(page_file_mapping(req->wb_page));
1017 nfs_mapping_set_error(req->wb_page, hdr->error);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001018 goto remove_req;
1019 }
1020 if (nfs_write_need_commit(hdr)) {
David Brazdil0f672f62019-12-10 10:32:29 +00001021 /* Reset wb_nio, since the write was successful. */
1022 req->wb_nio = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001023 memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
1024 nfs_mark_request_commit(req, hdr->lseg, &cinfo,
1025 hdr->pgio_mirror_idx);
1026 goto next;
1027 }
1028remove_req:
1029 nfs_inode_remove_request(req);
1030next:
1031 nfs_end_page_writeback(req);
1032 nfs_release_request(req);
1033 }
1034out:
1035 nfs_io_completion_put(hdr->io_completion);
1036 hdr->release(hdr);
1037}
1038
1039unsigned long
1040nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
1041{
1042 return atomic_long_read(&cinfo->mds->ncommit);
1043}
1044
1045/* NFS_I(cinfo->inode)->commit_mutex held by caller */
1046int
1047nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
1048 struct nfs_commit_info *cinfo, int max)
1049{
1050 struct nfs_page *req, *tmp;
1051 int ret = 0;
1052
1053restart:
1054 list_for_each_entry_safe(req, tmp, src, wb_list) {
1055 kref_get(&req->wb_kref);
1056 if (!nfs_lock_request(req)) {
1057 int status;
1058
1059 /* Prevent deadlock with nfs_lock_and_join_requests */
1060 if (!list_empty(dst)) {
1061 nfs_release_request(req);
1062 continue;
1063 }
1064 /* Ensure we make progress to prevent livelock */
1065 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
1066 status = nfs_wait_on_request(req);
1067 nfs_release_request(req);
1068 mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
1069 if (status < 0)
1070 break;
1071 goto restart;
1072 }
1073 nfs_request_remove_commit_list(req, cinfo);
1074 clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
1075 nfs_list_add_request(req, dst);
1076 ret++;
1077 if ((ret == max) && !cinfo->dreq)
1078 break;
1079 cond_resched();
1080 }
1081 return ret;
1082}
1083EXPORT_SYMBOL_GPL(nfs_scan_commit_list);
1084
1085/*
1086 * nfs_scan_commit - Scan an inode for commit requests
1087 * @inode: NFS inode to scan
1088 * @dst: mds destination list
1089 * @cinfo: mds and ds lists of reqs ready to commit
1090 *
1091 * Moves requests from the inode's 'commit' request list.
1092 * The requests are *not* checked to ensure that they form a contiguous set.
1093 */
1094int
1095nfs_scan_commit(struct inode *inode, struct list_head *dst,
1096 struct nfs_commit_info *cinfo)
1097{
1098 int ret = 0;
1099
1100 if (!atomic_long_read(&cinfo->mds->ncommit))
1101 return 0;
1102 mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
1103 if (atomic_long_read(&cinfo->mds->ncommit) > 0) {
1104 const int max = INT_MAX;
1105
1106 ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
1107 cinfo, max);
1108 ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
1109 }
1110 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
1111 return ret;
1112}
1113
1114/*
1115 * Search for an existing write request, and attempt to update
1116 * it to reflect a new dirty region on a given page.
1117 *
1118 * If the attempt fails, then the existing request is flushed out
1119 * to disk.
1120 */
1121static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
1122 struct page *page,
1123 unsigned int offset,
1124 unsigned int bytes)
1125{
1126 struct nfs_page *req;
1127 unsigned int rqend;
1128 unsigned int end;
1129 int error;
1130
1131 end = offset + bytes;
1132
1133 req = nfs_lock_and_join_requests(page);
1134 if (IS_ERR_OR_NULL(req))
1135 return req;
1136
1137 rqend = req->wb_offset + req->wb_bytes;
1138 /*
1139 * Tell the caller to flush out the request if
1140 * the offsets are non-contiguous.
1141 * Note: nfs_flush_incompatible() will already
1142 * have flushed out requests having wrong owners.
1143 */
1144 if (offset > rqend || end < req->wb_offset)
1145 goto out_flushme;
1146
1147 /* Okay, the request matches. Update the region */
1148 if (offset < req->wb_offset) {
1149 req->wb_offset = offset;
1150 req->wb_pgbase = offset;
1151 }
1152 if (end > rqend)
1153 req->wb_bytes = end - req->wb_offset;
1154 else
1155 req->wb_bytes = rqend - req->wb_offset;
David Brazdil0f672f62019-12-10 10:32:29 +00001156 req->wb_nio = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001157 return req;
1158out_flushme:
1159 /*
1160 * Note: we mark the request dirty here because
1161 * nfs_lock_and_join_requests() cannot preserve
1162 * commit flags, so we have to replay the write.
1163 */
1164 nfs_mark_request_dirty(req);
1165 nfs_unlock_and_release_request(req);
1166 error = nfs_wb_page(inode, page);
1167 return (error < 0) ? ERR_PTR(error) : NULL;
1168}
1169
1170/*
1171 * Try to update an existing write request, or create one if there is none.
1172 *
1173 * Note: Should always be called with the Page Lock held to prevent races
1174 * if we have to add a new request. Also assumes that the caller has
1175 * already called nfs_flush_incompatible() if necessary.
1176 */
1177static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
1178 struct page *page, unsigned int offset, unsigned int bytes)
1179{
1180 struct inode *inode = page_file_mapping(page)->host;
1181 struct nfs_page *req;
1182
1183 req = nfs_try_to_update_request(inode, page, offset, bytes);
1184 if (req != NULL)
1185 goto out;
David Brazdil0f672f62019-12-10 10:32:29 +00001186 req = nfs_create_request(ctx, page, offset, bytes);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001187 if (IS_ERR(req))
1188 goto out;
1189 nfs_inode_add_request(inode, req);
1190out:
1191 return req;
1192}
1193
1194static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1195 unsigned int offset, unsigned int count)
1196{
1197 struct nfs_page *req;
1198
1199 req = nfs_setup_write_request(ctx, page, offset, count);
1200 if (IS_ERR(req))
1201 return PTR_ERR(req);
1202 /* Update file length */
1203 nfs_grow_file(page, offset, count);
1204 nfs_mark_uptodate(req);
1205 nfs_mark_request_dirty(req);
1206 nfs_unlock_and_release_request(req);
1207 return 0;
1208}
1209
1210int nfs_flush_incompatible(struct file *file, struct page *page)
1211{
1212 struct nfs_open_context *ctx = nfs_file_open_context(file);
1213 struct nfs_lock_context *l_ctx;
1214 struct file_lock_context *flctx = file_inode(file)->i_flctx;
1215 struct nfs_page *req;
1216 int do_flush, status;
1217 /*
1218 * Look for a request corresponding to this page. If there
1219 * is one, and it belongs to another file, we flush it out
1220 * before we try to copy anything into the page. Do this
1221 * due to the lack of an ACCESS-type call in NFSv2.
1222 * Also do the same if we find a request from an existing
1223 * dropped page.
1224 */
1225 do {
1226 req = nfs_page_find_head_request(page);
1227 if (req == NULL)
1228 return 0;
1229 l_ctx = req->wb_lock_context;
1230 do_flush = req->wb_page != page ||
David Brazdil0f672f62019-12-10 10:32:29 +00001231 !nfs_match_open_context(nfs_req_openctx(req), ctx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001232 if (l_ctx && flctx &&
1233 !(list_empty_careful(&flctx->flc_posix) &&
1234 list_empty_careful(&flctx->flc_flock))) {
1235 do_flush |= l_ctx->lockowner != current->files;
1236 }
1237 nfs_release_request(req);
1238 if (!do_flush)
1239 return 0;
1240 status = nfs_wb_page(page_file_mapping(page)->host, page);
1241 } while (status == 0);
1242 return status;
1243}
1244
1245/*
1246 * Avoid buffered writes when a open context credential's key would
1247 * expire soon.
1248 *
1249 * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
1250 *
1251 * Return 0 and set a credential flag which triggers the inode to flush
1252 * and performs NFS_FILE_SYNC writes if the key will expired within
1253 * RPC_KEY_EXPIRE_TIMEO.
1254 */
1255int
1256nfs_key_timeout_notify(struct file *filp, struct inode *inode)
1257{
1258 struct nfs_open_context *ctx = nfs_file_open_context(filp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001259
David Brazdil0f672f62019-12-10 10:32:29 +00001260 if (nfs_ctx_key_to_expire(ctx, inode) &&
1261 !ctx->ll_cred)
1262 /* Already expired! */
1263 return -EACCES;
1264 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001265}
1266
1267/*
1268 * Test if the open context credential key is marked to expire soon.
1269 */
1270bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode)
1271{
1272 struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
David Brazdil0f672f62019-12-10 10:32:29 +00001273 struct rpc_cred *cred = ctx->ll_cred;
1274 struct auth_cred acred = {
1275 .cred = ctx->cred,
1276 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001277
David Brazdil0f672f62019-12-10 10:32:29 +00001278 if (cred && !cred->cr_ops->crmatch(&acred, cred, 0)) {
1279 put_rpccred(cred);
1280 ctx->ll_cred = NULL;
1281 cred = NULL;
1282 }
1283 if (!cred)
1284 cred = auth->au_ops->lookup_cred(auth, &acred, 0);
1285 if (!cred || IS_ERR(cred))
1286 return true;
1287 ctx->ll_cred = cred;
1288 return !!(cred->cr_ops->crkey_timeout &&
1289 cred->cr_ops->crkey_timeout(cred));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001290}
1291
1292/*
1293 * If the page cache is marked as unsafe or invalid, then we can't rely on
1294 * the PageUptodate() flag. In this case, we will need to turn off
1295 * write optimisations that depend on the page contents being correct.
1296 */
1297static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
1298{
1299 struct nfs_inode *nfsi = NFS_I(inode);
1300
1301 if (nfs_have_delegated_attributes(inode))
1302 goto out;
1303 if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
1304 return false;
1305 smp_rmb();
1306 if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
1307 return false;
1308out:
1309 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1310 return false;
1311 return PageUptodate(page) != 0;
1312}
1313
1314static bool
1315is_whole_file_wrlock(struct file_lock *fl)
1316{
1317 return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX &&
1318 fl->fl_type == F_WRLCK;
1319}
1320
1321/* If we know the page is up to date, and we're not using byte range locks (or
1322 * if we have the whole file locked for writing), it may be more efficient to
1323 * extend the write to cover the entire page in order to avoid fragmentation
1324 * inefficiencies.
1325 *
1326 * If the file is opened for synchronous writes then we can just skip the rest
1327 * of the checks.
1328 */
1329static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
1330{
1331 int ret;
1332 struct file_lock_context *flctx = inode->i_flctx;
1333 struct file_lock *fl;
1334
1335 if (file->f_flags & O_DSYNC)
1336 return 0;
1337 if (!nfs_write_pageuptodate(page, inode))
1338 return 0;
1339 if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
1340 return 1;
1341 if (!flctx || (list_empty_careful(&flctx->flc_flock) &&
1342 list_empty_careful(&flctx->flc_posix)))
1343 return 1;
1344
1345 /* Check to see if there are whole file write locks */
1346 ret = 0;
1347 spin_lock(&flctx->flc_lock);
1348 if (!list_empty(&flctx->flc_posix)) {
1349 fl = list_first_entry(&flctx->flc_posix, struct file_lock,
1350 fl_list);
1351 if (is_whole_file_wrlock(fl))
1352 ret = 1;
1353 } else if (!list_empty(&flctx->flc_flock)) {
1354 fl = list_first_entry(&flctx->flc_flock, struct file_lock,
1355 fl_list);
1356 if (fl->fl_type == F_WRLCK)
1357 ret = 1;
1358 }
1359 spin_unlock(&flctx->flc_lock);
1360 return ret;
1361}
1362
1363/*
1364 * Update and possibly write a cached page of an NFS file.
1365 *
1366 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
1367 * things with a page scheduled for an RPC call (e.g. invalidate it).
1368 */
1369int nfs_updatepage(struct file *file, struct page *page,
1370 unsigned int offset, unsigned int count)
1371{
1372 struct nfs_open_context *ctx = nfs_file_open_context(file);
David Brazdil0f672f62019-12-10 10:32:29 +00001373 struct address_space *mapping = page_file_mapping(page);
1374 struct inode *inode = mapping->host;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001375 int status = 0;
1376
1377 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
1378
1379 dprintk("NFS: nfs_updatepage(%pD2 %d@%lld)\n",
1380 file, count, (long long)(page_file_offset(page) + offset));
1381
1382 if (!count)
1383 goto out;
1384
1385 if (nfs_can_extend_write(file, page, inode)) {
1386 count = max(count + offset, nfs_page_length(page));
1387 offset = 0;
1388 }
1389
1390 status = nfs_writepage_setup(ctx, page, offset, count);
1391 if (status < 0)
David Brazdil0f672f62019-12-10 10:32:29 +00001392 nfs_set_pageerror(mapping);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001393 else
1394 __set_page_dirty_nobuffers(page);
1395out:
1396 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
1397 status, (long long)i_size_read(inode));
1398 return status;
1399}
1400
1401static int flush_task_priority(int how)
1402{
1403 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
1404 case FLUSH_HIGHPRI:
1405 return RPC_PRIORITY_HIGH;
1406 case FLUSH_LOWPRI:
1407 return RPC_PRIORITY_LOW;
1408 }
1409 return RPC_PRIORITY_NORMAL;
1410}
1411
1412static void nfs_initiate_write(struct nfs_pgio_header *hdr,
1413 struct rpc_message *msg,
1414 const struct nfs_rpc_ops *rpc_ops,
1415 struct rpc_task_setup *task_setup_data, int how)
1416{
1417 int priority = flush_task_priority(how);
1418
1419 task_setup_data->priority = priority;
1420 rpc_ops->write_setup(hdr, msg, &task_setup_data->rpc_client);
1421 trace_nfs_initiate_write(hdr->inode, hdr->io_start, hdr->good_bytes,
1422 hdr->args.stable);
1423}
1424
1425/* If a nfs_flush_* function fails, it should remove reqs from @head and
1426 * call this on each, which will prepare them to be retried on next
1427 * writeback using standard nfs.
1428 */
1429static void nfs_redirty_request(struct nfs_page *req)
1430{
David Brazdil0f672f62019-12-10 10:32:29 +00001431 /* Bump the transmission count */
1432 req->wb_nio++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001433 nfs_mark_request_dirty(req);
David Brazdil0f672f62019-12-10 10:32:29 +00001434 set_bit(NFS_CONTEXT_RESEND_WRITES, &nfs_req_openctx(req)->flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001435 nfs_end_page_writeback(req);
1436 nfs_release_request(req);
1437}
1438
David Brazdil0f672f62019-12-10 10:32:29 +00001439static void nfs_async_write_error(struct list_head *head, int error)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001440{
1441 struct nfs_page *req;
1442
1443 while (!list_empty(head)) {
1444 req = nfs_list_entry(head->next);
1445 nfs_list_remove_request(req);
David Brazdil0f672f62019-12-10 10:32:29 +00001446 if (nfs_error_is_fatal(error))
1447 nfs_write_error(req, error);
1448 else
1449 nfs_redirty_request(req);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001450 }
1451}
1452
1453static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr)
1454{
David Brazdil0f672f62019-12-10 10:32:29 +00001455 nfs_async_write_error(&hdr->pages, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001456 filemap_fdatawrite_range(hdr->inode->i_mapping, hdr->args.offset,
1457 hdr->args.offset + hdr->args.count - 1);
1458}
1459
1460static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1461 .init_hdr = nfs_async_write_init,
1462 .error_cleanup = nfs_async_write_error,
1463 .completion = nfs_write_completion,
1464 .reschedule_io = nfs_async_write_reschedule_io,
1465};
1466
1467void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1468 struct inode *inode, int ioflags, bool force_mds,
1469 const struct nfs_pgio_completion_ops *compl_ops)
1470{
1471 struct nfs_server *server = NFS_SERVER(inode);
1472 const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
1473
1474#ifdef CONFIG_NFS_V4_1
1475 if (server->pnfs_curr_ld && !force_mds)
1476 pg_ops = server->pnfs_curr_ld->pg_write_ops;
1477#endif
1478 nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
1479 server->wsize, ioflags);
1480}
1481EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
1482
1483void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1484{
1485 struct nfs_pgio_mirror *mirror;
1486
1487 if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
1488 pgio->pg_ops->pg_cleanup(pgio);
1489
1490 pgio->pg_ops = &nfs_pgio_rw_ops;
1491
1492 nfs_pageio_stop_mirroring(pgio);
1493
1494 mirror = &pgio->pg_mirrors[0];
1495 mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1496}
1497EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1498
1499
1500void nfs_commit_prepare(struct rpc_task *task, void *calldata)
1501{
1502 struct nfs_commit_data *data = calldata;
1503
1504 NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
1505}
1506
1507/*
1508 * Special version of should_remove_suid() that ignores capabilities.
1509 */
1510static int nfs_should_remove_suid(const struct inode *inode)
1511{
1512 umode_t mode = inode->i_mode;
1513 int kill = 0;
1514
1515 /* suid always must be killed */
1516 if (unlikely(mode & S_ISUID))
1517 kill = ATTR_KILL_SUID;
1518
1519 /*
1520 * sgid without any exec bits is just a mandatory locking mark; leave
1521 * it alone. If some exec bits are set, it's a real sgid; kill it.
1522 */
1523 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
1524 kill |= ATTR_KILL_SGID;
1525
1526 if (unlikely(kill && S_ISREG(mode)))
1527 return kill;
1528
1529 return 0;
1530}
1531
1532static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr,
1533 struct nfs_fattr *fattr)
1534{
1535 struct nfs_pgio_args *argp = &hdr->args;
1536 struct nfs_pgio_res *resp = &hdr->res;
1537 u64 size = argp->offset + resp->count;
1538
1539 if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
1540 fattr->size = size;
1541 if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) {
1542 fattr->valid &= ~NFS_ATTR_FATTR_SIZE;
1543 return;
1544 }
1545 if (size != fattr->size)
1546 return;
1547 /* Set attribute barrier */
1548 nfs_fattr_set_barrier(fattr);
1549 /* ...and update size */
1550 fattr->valid |= NFS_ATTR_FATTR_SIZE;
1551}
1552
1553void nfs_writeback_update_inode(struct nfs_pgio_header *hdr)
1554{
1555 struct nfs_fattr *fattr = &hdr->fattr;
1556 struct inode *inode = hdr->inode;
1557
1558 spin_lock(&inode->i_lock);
1559 nfs_writeback_check_extend(hdr, fattr);
1560 nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
1561 spin_unlock(&inode->i_lock);
1562}
1563EXPORT_SYMBOL_GPL(nfs_writeback_update_inode);
1564
1565/*
1566 * This function is called when the WRITE call is complete.
1567 */
1568static int nfs_writeback_done(struct rpc_task *task,
1569 struct nfs_pgio_header *hdr,
1570 struct inode *inode)
1571{
1572 int status;
1573
1574 /*
1575 * ->write_done will attempt to use post-op attributes to detect
1576 * conflicting writes by other clients. A strict interpretation
1577 * of close-to-open would allow us to continue caching even if
1578 * another writer had changed the file, but some applications
1579 * depend on tighter cache coherency when writing.
1580 */
1581 status = NFS_PROTO(inode)->write_done(task, hdr);
1582 if (status != 0)
1583 return status;
1584
1585 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
1586 trace_nfs_writeback_done(inode, task->tk_status,
1587 hdr->args.offset, hdr->res.verf);
1588
1589 if (hdr->res.verf->committed < hdr->args.stable &&
1590 task->tk_status >= 0) {
1591 /* We tried a write call, but the server did not
1592 * commit data to stable storage even though we
1593 * requested it.
1594 * Note: There is a known bug in Tru64 < 5.0 in which
1595 * the server reports NFS_DATA_SYNC, but performs
1596 * NFS_FILE_SYNC. We therefore implement this checking
1597 * as a dprintk() in order to avoid filling syslog.
1598 */
1599 static unsigned long complain;
1600
1601 /* Note this will print the MDS for a DS write */
1602 if (time_before(complain, jiffies)) {
1603 dprintk("NFS: faulty NFS server %s:"
1604 " (committed = %d) != (stable = %d)\n",
1605 NFS_SERVER(inode)->nfs_client->cl_hostname,
1606 hdr->res.verf->committed, hdr->args.stable);
1607 complain = jiffies + 300 * HZ;
1608 }
1609 }
1610
1611 /* Deal with the suid/sgid bit corner case */
1612 if (nfs_should_remove_suid(inode)) {
1613 spin_lock(&inode->i_lock);
1614 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1615 spin_unlock(&inode->i_lock);
1616 }
1617 return 0;
1618}
1619
1620/*
1621 * This function is called when the WRITE call is complete.
1622 */
1623static void nfs_writeback_result(struct rpc_task *task,
1624 struct nfs_pgio_header *hdr)
1625{
1626 struct nfs_pgio_args *argp = &hdr->args;
1627 struct nfs_pgio_res *resp = &hdr->res;
1628
1629 if (resp->count < argp->count) {
1630 static unsigned long complain;
1631
1632 /* This a short write! */
1633 nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
1634
1635 /* Has the server at least made some progress? */
1636 if (resp->count == 0) {
1637 if (time_before(complain, jiffies)) {
1638 printk(KERN_WARNING
1639 "NFS: Server wrote zero bytes, expected %u.\n",
1640 argp->count);
1641 complain = jiffies + 300 * HZ;
1642 }
1643 nfs_set_pgio_error(hdr, -EIO, argp->offset);
1644 task->tk_status = -EIO;
1645 return;
1646 }
1647
1648 /* For non rpc-based layout drivers, retry-through-MDS */
1649 if (!task->tk_ops) {
1650 hdr->pnfs_error = -EAGAIN;
1651 return;
1652 }
1653
1654 /* Was this an NFSv2 write or an NFSv3 stable write? */
1655 if (resp->verf->committed != NFS_UNSTABLE) {
1656 /* Resend from where the server left off */
1657 hdr->mds_offset += resp->count;
1658 argp->offset += resp->count;
1659 argp->pgbase += resp->count;
1660 argp->count -= resp->count;
1661 } else {
1662 /* Resend as a stable write in order to avoid
1663 * headaches in the case of a server crash.
1664 */
1665 argp->stable = NFS_FILE_SYNC;
1666 }
1667 rpc_restart_call_prepare(task);
1668 }
1669}
1670
1671static int wait_on_commit(struct nfs_mds_commit_info *cinfo)
1672{
1673 return wait_var_event_killable(&cinfo->rpcs_out,
1674 !atomic_read(&cinfo->rpcs_out));
1675}
1676
1677static void nfs_commit_begin(struct nfs_mds_commit_info *cinfo)
1678{
1679 atomic_inc(&cinfo->rpcs_out);
1680}
1681
1682static void nfs_commit_end(struct nfs_mds_commit_info *cinfo)
1683{
1684 if (atomic_dec_and_test(&cinfo->rpcs_out))
1685 wake_up_var(&cinfo->rpcs_out);
1686}
1687
1688void nfs_commitdata_release(struct nfs_commit_data *data)
1689{
1690 put_nfs_open_context(data->context);
1691 nfs_commit_free(data);
1692}
1693EXPORT_SYMBOL_GPL(nfs_commitdata_release);
1694
1695int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
1696 const struct nfs_rpc_ops *nfs_ops,
1697 const struct rpc_call_ops *call_ops,
1698 int how, int flags)
1699{
1700 struct rpc_task *task;
1701 int priority = flush_task_priority(how);
1702 struct rpc_message msg = {
1703 .rpc_argp = &data->args,
1704 .rpc_resp = &data->res,
1705 .rpc_cred = data->cred,
1706 };
1707 struct rpc_task_setup task_setup_data = {
1708 .task = &data->task,
1709 .rpc_client = clnt,
1710 .rpc_message = &msg,
1711 .callback_ops = call_ops,
1712 .callback_data = data,
1713 .workqueue = nfsiod_workqueue,
1714 .flags = RPC_TASK_ASYNC | flags,
1715 .priority = priority,
1716 };
1717 /* Set up the initial task struct. */
1718 nfs_ops->commit_setup(data, &msg, &task_setup_data.rpc_client);
1719 trace_nfs_initiate_commit(data);
1720
1721 dprintk("NFS: initiated commit call\n");
1722
1723 task = rpc_run_task(&task_setup_data);
1724 if (IS_ERR(task))
1725 return PTR_ERR(task);
1726 if (how & FLUSH_SYNC)
1727 rpc_wait_for_completion_task(task);
1728 rpc_put_task(task);
1729 return 0;
1730}
1731EXPORT_SYMBOL_GPL(nfs_initiate_commit);
1732
1733static loff_t nfs_get_lwb(struct list_head *head)
1734{
1735 loff_t lwb = 0;
1736 struct nfs_page *req;
1737
1738 list_for_each_entry(req, head, wb_list)
1739 if (lwb < (req_offset(req) + req->wb_bytes))
1740 lwb = req_offset(req) + req->wb_bytes;
1741
1742 return lwb;
1743}
1744
1745/*
1746 * Set up the argument/result storage required for the RPC call.
1747 */
1748void nfs_init_commit(struct nfs_commit_data *data,
1749 struct list_head *head,
1750 struct pnfs_layout_segment *lseg,
1751 struct nfs_commit_info *cinfo)
1752{
1753 struct nfs_page *first = nfs_list_entry(head->next);
David Brazdil0f672f62019-12-10 10:32:29 +00001754 struct nfs_open_context *ctx = nfs_req_openctx(first);
1755 struct inode *inode = d_inode(ctx->dentry);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001756
1757 /* Set up the RPC argument and reply structs
1758 * NB: take care not to mess about with data->commit et al. */
1759
1760 list_splice_init(head, &data->pages);
1761
1762 data->inode = inode;
David Brazdil0f672f62019-12-10 10:32:29 +00001763 data->cred = ctx->cred;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001764 data->lseg = lseg; /* reference transferred */
1765 /* only set lwb for pnfs commit */
1766 if (lseg)
1767 data->lwb = nfs_get_lwb(&data->pages);
1768 data->mds_ops = &nfs_commit_ops;
1769 data->completion_ops = cinfo->completion_ops;
1770 data->dreq = cinfo->dreq;
1771
1772 data->args.fh = NFS_FH(data->inode);
1773 /* Note: we always request a commit of the entire inode */
1774 data->args.offset = 0;
1775 data->args.count = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001776 data->context = get_nfs_open_context(ctx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001777 data->res.fattr = &data->fattr;
1778 data->res.verf = &data->verf;
1779 nfs_fattr_init(&data->fattr);
1780}
1781EXPORT_SYMBOL_GPL(nfs_init_commit);
1782
1783void nfs_retry_commit(struct list_head *page_list,
1784 struct pnfs_layout_segment *lseg,
1785 struct nfs_commit_info *cinfo,
1786 u32 ds_commit_idx)
1787{
1788 struct nfs_page *req;
1789
1790 while (!list_empty(page_list)) {
1791 req = nfs_list_entry(page_list->next);
1792 nfs_list_remove_request(req);
1793 nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx);
1794 if (!cinfo->dreq)
1795 nfs_clear_page_commit(req->wb_page);
1796 nfs_unlock_and_release_request(req);
1797 }
1798}
1799EXPORT_SYMBOL_GPL(nfs_retry_commit);
1800
1801static void
1802nfs_commit_resched_write(struct nfs_commit_info *cinfo,
1803 struct nfs_page *req)
1804{
1805 __set_page_dirty_nobuffers(req->wb_page);
1806}
1807
1808/*
1809 * Commit dirty pages
1810 */
1811static int
1812nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1813 struct nfs_commit_info *cinfo)
1814{
1815 struct nfs_commit_data *data;
1816
1817 /* another commit raced with us */
1818 if (list_empty(head))
1819 return 0;
1820
1821 data = nfs_commitdata_alloc(true);
1822
1823 /* Set up the argument struct */
1824 nfs_init_commit(data, head, NULL, cinfo);
1825 atomic_inc(&cinfo->mds->rpcs_out);
1826 return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode),
1827 data->mds_ops, how, 0);
1828}
1829
1830/*
1831 * COMMIT call returned
1832 */
1833static void nfs_commit_done(struct rpc_task *task, void *calldata)
1834{
1835 struct nfs_commit_data *data = calldata;
1836
1837 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1838 task->tk_pid, task->tk_status);
1839
1840 /* Call the NFS version-specific code */
1841 NFS_PROTO(data->inode)->commit_done(task, data);
1842 trace_nfs_commit_done(data);
1843}
1844
1845static void nfs_commit_release_pages(struct nfs_commit_data *data)
1846{
Olivier Deprez0e641232021-09-23 10:07:05 +02001847 const struct nfs_writeverf *verf = data->res.verf;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001848 struct nfs_page *req;
1849 int status = data->task.tk_status;
1850 struct nfs_commit_info cinfo;
1851 struct nfs_server *nfss;
1852
1853 while (!list_empty(&data->pages)) {
1854 req = nfs_list_entry(data->pages.next);
1855 nfs_list_remove_request(req);
1856 if (req->wb_page)
1857 nfs_clear_page_commit(req->wb_page);
1858
1859 dprintk("NFS: commit (%s/%llu %d@%lld)",
David Brazdil0f672f62019-12-10 10:32:29 +00001860 nfs_req_openctx(req)->dentry->d_sb->s_id,
1861 (unsigned long long)NFS_FILEID(d_inode(nfs_req_openctx(req)->dentry)),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001862 req->wb_bytes,
1863 (long long)req_offset(req));
1864 if (status < 0) {
David Brazdil0f672f62019-12-10 10:32:29 +00001865 if (req->wb_page) {
1866 nfs_mapping_set_error(req->wb_page, status);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001867 nfs_inode_remove_request(req);
David Brazdil0f672f62019-12-10 10:32:29 +00001868 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001869 dprintk_cont(", error = %d\n", status);
1870 goto next;
1871 }
1872
1873 /* Okay, COMMIT succeeded, apparently. Check the verifier
1874 * returned by the server against all stored verfs. */
Olivier Deprez0e641232021-09-23 10:07:05 +02001875 if (verf->committed > NFS_UNSTABLE &&
1876 !nfs_write_verifier_cmp(&req->wb_verf, &verf->verifier)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001877 /* We have a match */
1878 if (req->wb_page)
1879 nfs_inode_remove_request(req);
1880 dprintk_cont(" OK\n");
1881 goto next;
1882 }
1883 /* We have a mismatch. Write the page again */
1884 dprintk_cont(" mismatch\n");
1885 nfs_mark_request_dirty(req);
David Brazdil0f672f62019-12-10 10:32:29 +00001886 set_bit(NFS_CONTEXT_RESEND_WRITES, &nfs_req_openctx(req)->flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001887 next:
1888 nfs_unlock_and_release_request(req);
1889 /* Latency breaker */
1890 cond_resched();
1891 }
1892 nfss = NFS_SERVER(data->inode);
1893 if (atomic_long_read(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
1894 clear_bdi_congested(inode_to_bdi(data->inode), BLK_RW_ASYNC);
1895
1896 nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1897 nfs_commit_end(cinfo.mds);
1898}
1899
1900static void nfs_commit_release(void *calldata)
1901{
1902 struct nfs_commit_data *data = calldata;
1903
1904 data->completion_ops->completion(data);
1905 nfs_commitdata_release(calldata);
1906}
1907
1908static const struct rpc_call_ops nfs_commit_ops = {
1909 .rpc_call_prepare = nfs_commit_prepare,
1910 .rpc_call_done = nfs_commit_done,
1911 .rpc_release = nfs_commit_release,
1912};
1913
1914static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1915 .completion = nfs_commit_release_pages,
1916 .resched_write = nfs_commit_resched_write,
1917};
1918
1919int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1920 int how, struct nfs_commit_info *cinfo)
1921{
1922 int status;
1923
1924 status = pnfs_commit_list(inode, head, how, cinfo);
1925 if (status == PNFS_NOT_ATTEMPTED)
1926 status = nfs_commit_list(inode, head, how, cinfo);
1927 return status;
1928}
1929
1930static int __nfs_commit_inode(struct inode *inode, int how,
1931 struct writeback_control *wbc)
1932{
1933 LIST_HEAD(head);
1934 struct nfs_commit_info cinfo;
1935 int may_wait = how & FLUSH_SYNC;
1936 int ret, nscan;
1937
1938 nfs_init_cinfo_from_inode(&cinfo, inode);
1939 nfs_commit_begin(cinfo.mds);
1940 for (;;) {
1941 ret = nscan = nfs_scan_commit(inode, &head, &cinfo);
1942 if (ret <= 0)
1943 break;
1944 ret = nfs_generic_commit_list(inode, &head, how, &cinfo);
1945 if (ret < 0)
1946 break;
1947 ret = 0;
1948 if (wbc && wbc->sync_mode == WB_SYNC_NONE) {
1949 if (nscan < wbc->nr_to_write)
1950 wbc->nr_to_write -= nscan;
1951 else
1952 wbc->nr_to_write = 0;
1953 }
1954 if (nscan < INT_MAX)
1955 break;
1956 cond_resched();
1957 }
1958 nfs_commit_end(cinfo.mds);
1959 if (ret || !may_wait)
1960 return ret;
1961 return wait_on_commit(cinfo.mds);
1962}
1963
1964int nfs_commit_inode(struct inode *inode, int how)
1965{
1966 return __nfs_commit_inode(inode, how, NULL);
1967}
1968EXPORT_SYMBOL_GPL(nfs_commit_inode);
1969
1970int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
1971{
1972 struct nfs_inode *nfsi = NFS_I(inode);
1973 int flags = FLUSH_SYNC;
1974 int ret = 0;
1975
1976 if (wbc->sync_mode == WB_SYNC_NONE) {
1977 /* no commits means nothing needs to be done */
1978 if (!atomic_long_read(&nfsi->commit_info.ncommit))
1979 goto check_requests_outstanding;
1980
1981 /* Don't commit yet if this is a non-blocking flush and there
1982 * are a lot of outstanding writes for this mapping.
1983 */
1984 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1985 goto out_mark_dirty;
1986
1987 /* don't wait for the COMMIT response */
1988 flags = 0;
1989 }
1990
1991 ret = __nfs_commit_inode(inode, flags, wbc);
1992 if (!ret) {
1993 if (flags & FLUSH_SYNC)
1994 return 0;
1995 } else if (atomic_long_read(&nfsi->commit_info.ncommit))
1996 goto out_mark_dirty;
1997
1998check_requests_outstanding:
1999 if (!atomic_read(&nfsi->commit_info.rpcs_out))
2000 return ret;
2001out_mark_dirty:
2002 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
2003 return ret;
2004}
2005EXPORT_SYMBOL_GPL(nfs_write_inode);
2006
2007/*
2008 * Wrapper for filemap_write_and_wait_range()
2009 *
2010 * Needed for pNFS in order to ensure data becomes visible to the
2011 * client.
2012 */
2013int nfs_filemap_write_and_wait_range(struct address_space *mapping,
2014 loff_t lstart, loff_t lend)
2015{
2016 int ret;
2017
2018 ret = filemap_write_and_wait_range(mapping, lstart, lend);
2019 if (ret == 0)
2020 ret = pnfs_sync_inode(mapping->host, true);
2021 return ret;
2022}
2023EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range);
2024
2025/*
2026 * flush the inode to disk.
2027 */
2028int nfs_wb_all(struct inode *inode)
2029{
2030 int ret;
2031
2032 trace_nfs_writeback_inode_enter(inode);
2033
2034 ret = filemap_write_and_wait(inode->i_mapping);
2035 if (ret)
2036 goto out;
2037 ret = nfs_commit_inode(inode, FLUSH_SYNC);
2038 if (ret < 0)
2039 goto out;
2040 pnfs_sync_inode(inode, true);
2041 ret = 0;
2042
2043out:
2044 trace_nfs_writeback_inode_exit(inode, ret);
2045 return ret;
2046}
2047EXPORT_SYMBOL_GPL(nfs_wb_all);
2048
2049int nfs_wb_page_cancel(struct inode *inode, struct page *page)
2050{
2051 struct nfs_page *req;
2052 int ret = 0;
2053
2054 wait_on_page_writeback(page);
2055
2056 /* blocking call to cancel all requests and join to a single (head)
2057 * request */
2058 req = nfs_lock_and_join_requests(page);
2059
2060 if (IS_ERR(req)) {
2061 ret = PTR_ERR(req);
2062 } else if (req) {
2063 /* all requests from this page have been cancelled by
2064 * nfs_lock_and_join_requests, so just remove the head
2065 * request from the inode / page_private pointer and
2066 * release it */
2067 nfs_inode_remove_request(req);
2068 nfs_unlock_and_release_request(req);
2069 }
2070
2071 return ret;
2072}
2073
2074/*
2075 * Write back all requests on one page - we do this before reading it.
2076 */
2077int nfs_wb_page(struct inode *inode, struct page *page)
2078{
2079 loff_t range_start = page_file_offset(page);
2080 loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
2081 struct writeback_control wbc = {
2082 .sync_mode = WB_SYNC_ALL,
2083 .nr_to_write = 0,
2084 .range_start = range_start,
2085 .range_end = range_end,
2086 };
2087 int ret;
2088
2089 trace_nfs_writeback_page_enter(inode);
2090
2091 for (;;) {
2092 wait_on_page_writeback(page);
2093 if (clear_page_dirty_for_io(page)) {
2094 ret = nfs_writepage_locked(page, &wbc);
2095 if (ret < 0)
2096 goto out_error;
2097 continue;
2098 }
2099 ret = 0;
2100 if (!PagePrivate(page))
2101 break;
2102 ret = nfs_commit_inode(inode, FLUSH_SYNC);
2103 if (ret < 0)
2104 goto out_error;
2105 }
2106out_error:
2107 trace_nfs_writeback_page_exit(inode, ret);
2108 return ret;
2109}
2110
2111#ifdef CONFIG_MIGRATION
2112int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
2113 struct page *page, enum migrate_mode mode)
2114{
2115 /*
2116 * If PagePrivate is set, then the page is currently associated with
2117 * an in-progress read or write request. Don't try to migrate it.
2118 *
2119 * FIXME: we could do this in principle, but we'll need a way to ensure
2120 * that we can safely release the inode reference while holding
2121 * the page lock.
2122 */
2123 if (PagePrivate(page))
2124 return -EBUSY;
2125
2126 if (!nfs_fscache_release_page(page, GFP_KERNEL))
2127 return -EBUSY;
2128
2129 return migrate_page(mapping, newpage, page, mode);
2130}
2131#endif
2132
2133int __init nfs_init_writepagecache(void)
2134{
2135 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
2136 sizeof(struct nfs_pgio_header),
2137 0, SLAB_HWCACHE_ALIGN,
2138 NULL);
2139 if (nfs_wdata_cachep == NULL)
2140 return -ENOMEM;
2141
2142 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
2143 nfs_wdata_cachep);
2144 if (nfs_wdata_mempool == NULL)
2145 goto out_destroy_write_cache;
2146
2147 nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
2148 sizeof(struct nfs_commit_data),
2149 0, SLAB_HWCACHE_ALIGN,
2150 NULL);
2151 if (nfs_cdata_cachep == NULL)
2152 goto out_destroy_write_mempool;
2153
2154 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
2155 nfs_cdata_cachep);
2156 if (nfs_commit_mempool == NULL)
2157 goto out_destroy_commit_cache;
2158
2159 /*
2160 * NFS congestion size, scale with available memory.
2161 *
2162 * 64MB: 8192k
2163 * 128MB: 11585k
2164 * 256MB: 16384k
2165 * 512MB: 23170k
2166 * 1GB: 32768k
2167 * 2GB: 46340k
2168 * 4GB: 65536k
2169 * 8GB: 92681k
2170 * 16GB: 131072k
2171 *
2172 * This allows larger machines to have larger/more transfers.
2173 * Limit the default to 256M
2174 */
David Brazdil0f672f62019-12-10 10:32:29 +00002175 nfs_congestion_kb = (16*int_sqrt(totalram_pages())) << (PAGE_SHIFT-10);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002176 if (nfs_congestion_kb > 256*1024)
2177 nfs_congestion_kb = 256*1024;
2178
2179 return 0;
2180
2181out_destroy_commit_cache:
2182 kmem_cache_destroy(nfs_cdata_cachep);
2183out_destroy_write_mempool:
2184 mempool_destroy(nfs_wdata_mempool);
2185out_destroy_write_cache:
2186 kmem_cache_destroy(nfs_wdata_cachep);
2187 return -ENOMEM;
2188}
2189
2190void nfs_destroy_writepagecache(void)
2191{
2192 mempool_destroy(nfs_commit_mempool);
2193 kmem_cache_destroy(nfs_cdata_cachep);
2194 mempool_destroy(nfs_wdata_mempool);
2195 kmem_cache_destroy(nfs_wdata_cachep);
2196}
2197
2198static const struct nfs_rw_ops nfs_rw_write_ops = {
2199 .rw_alloc_header = nfs_writehdr_alloc,
2200 .rw_free_header = nfs_writehdr_free,
2201 .rw_done = nfs_writeback_done,
2202 .rw_result = nfs_writeback_result,
2203 .rw_initiate = nfs_initiate_write,
2204};