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