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