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