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