NFS: Convert v2 into a module
[deliverable/linux.git] / fs / nfs / write.c
1 /*
2 * linux/fs/nfs/write.c
3 *
4 * Write file data over NFS.
5 *
6 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
7 */
8
9 #include <linux/types.h>
10 #include <linux/slab.h>
11 #include <linux/mm.h>
12 #include <linux/pagemap.h>
13 #include <linux/file.h>
14 #include <linux/writeback.h>
15 #include <linux/swap.h>
16 #include <linux/migrate.h>
17
18 #include <linux/sunrpc/clnt.h>
19 #include <linux/nfs_fs.h>
20 #include <linux/nfs_mount.h>
21 #include <linux/nfs_page.h>
22 #include <linux/backing-dev.h>
23 #include <linux/export.h>
24
25 #include <asm/uaccess.h>
26
27 #include "delegation.h"
28 #include "internal.h"
29 #include "iostat.h"
30 #include "nfs4_fs.h"
31 #include "fscache.h"
32 #include "pnfs.h"
33
34 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
35
36 #define MIN_POOL_WRITE (32)
37 #define MIN_POOL_COMMIT (4)
38
39 /*
40 * Local function declarations
41 */
42 static void nfs_redirty_request(struct nfs_page *req);
43 static const struct rpc_call_ops nfs_write_common_ops;
44 static const struct rpc_call_ops nfs_commit_ops;
45 static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
46 static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
47
48 static struct kmem_cache *nfs_wdata_cachep;
49 static mempool_t *nfs_wdata_mempool;
50 static struct kmem_cache *nfs_cdata_cachep;
51 static mempool_t *nfs_commit_mempool;
52
53 struct nfs_commit_data *nfs_commitdata_alloc(void)
54 {
55 struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
56
57 if (p) {
58 memset(p, 0, sizeof(*p));
59 INIT_LIST_HEAD(&p->pages);
60 }
61 return p;
62 }
63 EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
64
65 void nfs_commit_free(struct nfs_commit_data *p)
66 {
67 mempool_free(p, nfs_commit_mempool);
68 }
69 EXPORT_SYMBOL_GPL(nfs_commit_free);
70
71 struct nfs_write_header *nfs_writehdr_alloc(void)
72 {
73 struct nfs_write_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
74
75 if (p) {
76 struct nfs_pgio_header *hdr = &p->header;
77
78 memset(p, 0, sizeof(*p));
79 INIT_LIST_HEAD(&hdr->pages);
80 INIT_LIST_HEAD(&hdr->rpc_list);
81 spin_lock_init(&hdr->lock);
82 atomic_set(&hdr->refcnt, 0);
83 hdr->verf = &p->verf;
84 }
85 return p;
86 }
87
88 static struct nfs_write_data *nfs_writedata_alloc(struct nfs_pgio_header *hdr,
89 unsigned int pagecount)
90 {
91 struct nfs_write_data *data, *prealloc;
92
93 prealloc = &container_of(hdr, struct nfs_write_header, header)->rpc_data;
94 if (prealloc->header == NULL)
95 data = prealloc;
96 else
97 data = kzalloc(sizeof(*data), GFP_KERNEL);
98 if (!data)
99 goto out;
100
101 if (nfs_pgarray_set(&data->pages, pagecount)) {
102 data->header = hdr;
103 atomic_inc(&hdr->refcnt);
104 } else {
105 if (data != prealloc)
106 kfree(data);
107 data = NULL;
108 }
109 out:
110 return data;
111 }
112
113 void nfs_writehdr_free(struct nfs_pgio_header *hdr)
114 {
115 struct nfs_write_header *whdr = container_of(hdr, struct nfs_write_header, header);
116 mempool_free(whdr, nfs_wdata_mempool);
117 }
118
119 void nfs_writedata_release(struct nfs_write_data *wdata)
120 {
121 struct nfs_pgio_header *hdr = wdata->header;
122 struct nfs_write_header *write_header = container_of(hdr, struct nfs_write_header, header);
123
124 put_nfs_open_context(wdata->args.context);
125 if (wdata->pages.pagevec != wdata->pages.page_array)
126 kfree(wdata->pages.pagevec);
127 if (wdata != &write_header->rpc_data)
128 kfree(wdata);
129 else
130 wdata->header = NULL;
131 if (atomic_dec_and_test(&hdr->refcnt))
132 hdr->completion_ops->completion(hdr);
133 }
134
135 static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
136 {
137 ctx->error = error;
138 smp_wmb();
139 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
140 }
141
142 static struct nfs_page *nfs_page_find_request_locked(struct page *page)
143 {
144 struct nfs_page *req = NULL;
145
146 if (PagePrivate(page)) {
147 req = (struct nfs_page *)page_private(page);
148 if (req != NULL)
149 kref_get(&req->wb_kref);
150 }
151 return req;
152 }
153
154 static struct nfs_page *nfs_page_find_request(struct page *page)
155 {
156 struct inode *inode = page->mapping->host;
157 struct nfs_page *req = NULL;
158
159 spin_lock(&inode->i_lock);
160 req = nfs_page_find_request_locked(page);
161 spin_unlock(&inode->i_lock);
162 return req;
163 }
164
165 /* Adjust the file length if we're writing beyond the end */
166 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
167 {
168 struct inode *inode = page->mapping->host;
169 loff_t end, i_size;
170 pgoff_t end_index;
171
172 spin_lock(&inode->i_lock);
173 i_size = i_size_read(inode);
174 end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
175 if (i_size > 0 && page->index < end_index)
176 goto out;
177 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
178 if (i_size >= end)
179 goto out;
180 i_size_write(inode, end);
181 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
182 out:
183 spin_unlock(&inode->i_lock);
184 }
185
186 /* A writeback failed: mark the page as bad, and invalidate the page cache */
187 static void nfs_set_pageerror(struct page *page)
188 {
189 SetPageError(page);
190 nfs_zap_mapping(page->mapping->host, page->mapping);
191 }
192
193 /* We can set the PG_uptodate flag if we see that a write request
194 * covers the full page.
195 */
196 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
197 {
198 if (PageUptodate(page))
199 return;
200 if (base != 0)
201 return;
202 if (count != nfs_page_length(page))
203 return;
204 SetPageUptodate(page);
205 }
206
207 static int wb_priority(struct writeback_control *wbc)
208 {
209 if (wbc->for_reclaim)
210 return FLUSH_HIGHPRI | FLUSH_STABLE;
211 if (wbc->for_kupdate || wbc->for_background)
212 return FLUSH_LOWPRI | FLUSH_COND_STABLE;
213 return FLUSH_COND_STABLE;
214 }
215
216 /*
217 * NFS congestion control
218 */
219
220 int nfs_congestion_kb;
221
222 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
223 #define NFS_CONGESTION_OFF_THRESH \
224 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
225
226 static int nfs_set_page_writeback(struct page *page)
227 {
228 int ret = test_set_page_writeback(page);
229
230 if (!ret) {
231 struct inode *inode = page->mapping->host;
232 struct nfs_server *nfss = NFS_SERVER(inode);
233
234 if (atomic_long_inc_return(&nfss->writeback) >
235 NFS_CONGESTION_ON_THRESH) {
236 set_bdi_congested(&nfss->backing_dev_info,
237 BLK_RW_ASYNC);
238 }
239 }
240 return ret;
241 }
242
243 static void nfs_end_page_writeback(struct page *page)
244 {
245 struct inode *inode = page->mapping->host;
246 struct nfs_server *nfss = NFS_SERVER(inode);
247
248 end_page_writeback(page);
249 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
250 clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
251 }
252
253 static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
254 {
255 struct inode *inode = page->mapping->host;
256 struct nfs_page *req;
257 int ret;
258
259 spin_lock(&inode->i_lock);
260 for (;;) {
261 req = nfs_page_find_request_locked(page);
262 if (req == NULL)
263 break;
264 if (nfs_lock_request(req))
265 break;
266 /* Note: If we hold the page lock, as is the case in nfs_writepage,
267 * then the call to nfs_lock_request() will always
268 * succeed provided that someone hasn't already marked the
269 * request as dirty (in which case we don't care).
270 */
271 spin_unlock(&inode->i_lock);
272 if (!nonblock)
273 ret = nfs_wait_on_request(req);
274 else
275 ret = -EAGAIN;
276 nfs_release_request(req);
277 if (ret != 0)
278 return ERR_PTR(ret);
279 spin_lock(&inode->i_lock);
280 }
281 spin_unlock(&inode->i_lock);
282 return req;
283 }
284
285 /*
286 * Find an associated nfs write request, and prepare to flush it out
287 * May return an error if the user signalled nfs_wait_on_request().
288 */
289 static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
290 struct page *page, bool nonblock)
291 {
292 struct nfs_page *req;
293 int ret = 0;
294
295 req = nfs_find_and_lock_request(page, nonblock);
296 if (!req)
297 goto out;
298 ret = PTR_ERR(req);
299 if (IS_ERR(req))
300 goto out;
301
302 ret = nfs_set_page_writeback(page);
303 BUG_ON(ret != 0);
304 BUG_ON(test_bit(PG_CLEAN, &req->wb_flags));
305
306 if (!nfs_pageio_add_request(pgio, req)) {
307 nfs_redirty_request(req);
308 ret = pgio->pg_error;
309 }
310 out:
311 return ret;
312 }
313
314 static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
315 {
316 struct inode *inode = page->mapping->host;
317 int ret;
318
319 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
320 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
321
322 nfs_pageio_cond_complete(pgio, page->index);
323 ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
324 if (ret == -EAGAIN) {
325 redirty_page_for_writepage(wbc, page);
326 ret = 0;
327 }
328 return ret;
329 }
330
331 /*
332 * Write an mmapped page to the server.
333 */
334 static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
335 {
336 struct nfs_pageio_descriptor pgio;
337 int err;
338
339 NFS_PROTO(page->mapping->host)->write_pageio_init(&pgio,
340 page->mapping->host,
341 wb_priority(wbc),
342 &nfs_async_write_completion_ops);
343 err = nfs_do_writepage(page, wbc, &pgio);
344 nfs_pageio_complete(&pgio);
345 if (err < 0)
346 return err;
347 if (pgio.pg_error < 0)
348 return pgio.pg_error;
349 return 0;
350 }
351
352 int nfs_writepage(struct page *page, struct writeback_control *wbc)
353 {
354 int ret;
355
356 ret = nfs_writepage_locked(page, wbc);
357 unlock_page(page);
358 return ret;
359 }
360
361 static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
362 {
363 int ret;
364
365 ret = nfs_do_writepage(page, wbc, data);
366 unlock_page(page);
367 return ret;
368 }
369
370 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
371 {
372 struct inode *inode = mapping->host;
373 unsigned long *bitlock = &NFS_I(inode)->flags;
374 struct nfs_pageio_descriptor pgio;
375 int err;
376
377 /* Stop dirtying of new pages while we sync */
378 err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
379 nfs_wait_bit_killable, TASK_KILLABLE);
380 if (err)
381 goto out_err;
382
383 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
384
385 NFS_PROTO(inode)->write_pageio_init(&pgio, inode, wb_priority(wbc), &nfs_async_write_completion_ops);
386 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
387 nfs_pageio_complete(&pgio);
388
389 clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
390 smp_mb__after_clear_bit();
391 wake_up_bit(bitlock, NFS_INO_FLUSHING);
392
393 if (err < 0)
394 goto out_err;
395 err = pgio.pg_error;
396 if (err < 0)
397 goto out_err;
398 return 0;
399 out_err:
400 return err;
401 }
402
403 /*
404 * Insert a write request into an inode
405 */
406 static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
407 {
408 struct nfs_inode *nfsi = NFS_I(inode);
409
410 /* Lock the request! */
411 nfs_lock_request(req);
412
413 spin_lock(&inode->i_lock);
414 if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
415 inode->i_version++;
416 set_bit(PG_MAPPED, &req->wb_flags);
417 SetPagePrivate(req->wb_page);
418 set_page_private(req->wb_page, (unsigned long)req);
419 nfsi->npages++;
420 kref_get(&req->wb_kref);
421 spin_unlock(&inode->i_lock);
422 }
423
424 /*
425 * Remove a write request from an inode
426 */
427 static void nfs_inode_remove_request(struct nfs_page *req)
428 {
429 struct inode *inode = req->wb_context->dentry->d_inode;
430 struct nfs_inode *nfsi = NFS_I(inode);
431
432 BUG_ON (!NFS_WBACK_BUSY(req));
433
434 spin_lock(&inode->i_lock);
435 set_page_private(req->wb_page, 0);
436 ClearPagePrivate(req->wb_page);
437 clear_bit(PG_MAPPED, &req->wb_flags);
438 nfsi->npages--;
439 spin_unlock(&inode->i_lock);
440 nfs_release_request(req);
441 }
442
443 static void
444 nfs_mark_request_dirty(struct nfs_page *req)
445 {
446 __set_page_dirty_nobuffers(req->wb_page);
447 }
448
449 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
450 /**
451 * nfs_request_add_commit_list - add request to a commit list
452 * @req: pointer to a struct nfs_page
453 * @dst: commit list head
454 * @cinfo: holds list lock and accounting info
455 *
456 * This sets the PG_CLEAN bit, updates the cinfo count of
457 * number of outstanding requests requiring a commit as well as
458 * the MM page stats.
459 *
460 * The caller must _not_ hold the cinfo->lock, but must be
461 * holding the nfs_page lock.
462 */
463 void
464 nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst,
465 struct nfs_commit_info *cinfo)
466 {
467 set_bit(PG_CLEAN, &(req)->wb_flags);
468 spin_lock(cinfo->lock);
469 nfs_list_add_request(req, dst);
470 cinfo->mds->ncommit++;
471 spin_unlock(cinfo->lock);
472 if (!cinfo->dreq) {
473 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
474 inc_bdi_stat(req->wb_page->mapping->backing_dev_info,
475 BDI_RECLAIMABLE);
476 __mark_inode_dirty(req->wb_context->dentry->d_inode,
477 I_DIRTY_DATASYNC);
478 }
479 }
480 EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
481
482 /**
483 * nfs_request_remove_commit_list - Remove request from a commit list
484 * @req: pointer to a nfs_page
485 * @cinfo: holds list lock and accounting info
486 *
487 * This clears the PG_CLEAN bit, and updates the cinfo's count of
488 * number of outstanding requests requiring a commit
489 * It does not update the MM page stats.
490 *
491 * The caller _must_ hold the cinfo->lock and the nfs_page lock.
492 */
493 void
494 nfs_request_remove_commit_list(struct nfs_page *req,
495 struct nfs_commit_info *cinfo)
496 {
497 if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
498 return;
499 nfs_list_remove_request(req);
500 cinfo->mds->ncommit--;
501 }
502 EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
503
504 static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
505 struct inode *inode)
506 {
507 cinfo->lock = &inode->i_lock;
508 cinfo->mds = &NFS_I(inode)->commit_info;
509 cinfo->ds = pnfs_get_ds_info(inode);
510 cinfo->dreq = NULL;
511 cinfo->completion_ops = &nfs_commit_completion_ops;
512 }
513
514 void nfs_init_cinfo(struct nfs_commit_info *cinfo,
515 struct inode *inode,
516 struct nfs_direct_req *dreq)
517 {
518 if (dreq)
519 nfs_init_cinfo_from_dreq(cinfo, dreq);
520 else
521 nfs_init_cinfo_from_inode(cinfo, inode);
522 }
523 EXPORT_SYMBOL_GPL(nfs_init_cinfo);
524
525 /*
526 * Add a request to the inode's commit list.
527 */
528 void
529 nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
530 struct nfs_commit_info *cinfo)
531 {
532 if (pnfs_mark_request_commit(req, lseg, cinfo))
533 return;
534 nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo);
535 }
536
537 static void
538 nfs_clear_page_commit(struct page *page)
539 {
540 dec_zone_page_state(page, NR_UNSTABLE_NFS);
541 dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
542 }
543
544 static void
545 nfs_clear_request_commit(struct nfs_page *req)
546 {
547 if (test_bit(PG_CLEAN, &req->wb_flags)) {
548 struct inode *inode = req->wb_context->dentry->d_inode;
549 struct nfs_commit_info cinfo;
550
551 nfs_init_cinfo_from_inode(&cinfo, inode);
552 if (!pnfs_clear_request_commit(req, &cinfo)) {
553 spin_lock(cinfo.lock);
554 nfs_request_remove_commit_list(req, &cinfo);
555 spin_unlock(cinfo.lock);
556 }
557 nfs_clear_page_commit(req->wb_page);
558 }
559 }
560
561 static inline
562 int nfs_write_need_commit(struct nfs_write_data *data)
563 {
564 if (data->verf.committed == NFS_DATA_SYNC)
565 return data->header->lseg == NULL;
566 return data->verf.committed != NFS_FILE_SYNC;
567 }
568
569 #else
570 static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
571 struct inode *inode)
572 {
573 }
574
575 void nfs_init_cinfo(struct nfs_commit_info *cinfo,
576 struct inode *inode,
577 struct nfs_direct_req *dreq)
578 {
579 }
580
581 void
582 nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
583 struct nfs_commit_info *cinfo)
584 {
585 }
586
587 static void
588 nfs_clear_request_commit(struct nfs_page *req)
589 {
590 }
591
592 static inline
593 int nfs_write_need_commit(struct nfs_write_data *data)
594 {
595 return 0;
596 }
597
598 #endif
599
600 static void nfs_write_completion(struct nfs_pgio_header *hdr)
601 {
602 struct nfs_commit_info cinfo;
603 unsigned long bytes = 0;
604
605 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
606 goto out;
607 nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
608 while (!list_empty(&hdr->pages)) {
609 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
610
611 bytes += req->wb_bytes;
612 nfs_list_remove_request(req);
613 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
614 (hdr->good_bytes < bytes)) {
615 nfs_set_pageerror(req->wb_page);
616 nfs_context_set_write_error(req->wb_context, hdr->error);
617 goto remove_req;
618 }
619 if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
620 nfs_mark_request_dirty(req);
621 goto next;
622 }
623 if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
624 memcpy(&req->wb_verf, &hdr->verf->verifier, sizeof(req->wb_verf));
625 nfs_mark_request_commit(req, hdr->lseg, &cinfo);
626 goto next;
627 }
628 remove_req:
629 nfs_inode_remove_request(req);
630 next:
631 nfs_unlock_request(req);
632 nfs_end_page_writeback(req->wb_page);
633 nfs_release_request(req);
634 }
635 out:
636 hdr->release(hdr);
637 }
638
639 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
640 static unsigned long
641 nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
642 {
643 return cinfo->mds->ncommit;
644 }
645
646 /* cinfo->lock held by caller */
647 int
648 nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
649 struct nfs_commit_info *cinfo, int max)
650 {
651 struct nfs_page *req, *tmp;
652 int ret = 0;
653
654 list_for_each_entry_safe(req, tmp, src, wb_list) {
655 if (!nfs_lock_request(req))
656 continue;
657 kref_get(&req->wb_kref);
658 if (cond_resched_lock(cinfo->lock))
659 list_safe_reset_next(req, tmp, wb_list);
660 nfs_request_remove_commit_list(req, cinfo);
661 nfs_list_add_request(req, dst);
662 ret++;
663 if ((ret == max) && !cinfo->dreq)
664 break;
665 }
666 return ret;
667 }
668
669 /*
670 * nfs_scan_commit - Scan an inode for commit requests
671 * @inode: NFS inode to scan
672 * @dst: mds destination list
673 * @cinfo: mds and ds lists of reqs ready to commit
674 *
675 * Moves requests from the inode's 'commit' request list.
676 * The requests are *not* checked to ensure that they form a contiguous set.
677 */
678 int
679 nfs_scan_commit(struct inode *inode, struct list_head *dst,
680 struct nfs_commit_info *cinfo)
681 {
682 int ret = 0;
683
684 spin_lock(cinfo->lock);
685 if (cinfo->mds->ncommit > 0) {
686 const int max = INT_MAX;
687
688 ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
689 cinfo, max);
690 ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
691 }
692 spin_unlock(cinfo->lock);
693 return ret;
694 }
695
696 #else
697 static unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
698 {
699 return 0;
700 }
701
702 int nfs_scan_commit(struct inode *inode, struct list_head *dst,
703 struct nfs_commit_info *cinfo)
704 {
705 return 0;
706 }
707 #endif
708
709 /*
710 * Search for an existing write request, and attempt to update
711 * it to reflect a new dirty region on a given page.
712 *
713 * If the attempt fails, then the existing request is flushed out
714 * to disk.
715 */
716 static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
717 struct page *page,
718 unsigned int offset,
719 unsigned int bytes)
720 {
721 struct nfs_page *req;
722 unsigned int rqend;
723 unsigned int end;
724 int error;
725
726 if (!PagePrivate(page))
727 return NULL;
728
729 end = offset + bytes;
730 spin_lock(&inode->i_lock);
731
732 for (;;) {
733 req = nfs_page_find_request_locked(page);
734 if (req == NULL)
735 goto out_unlock;
736
737 rqend = req->wb_offset + req->wb_bytes;
738 /*
739 * Tell the caller to flush out the request if
740 * the offsets are non-contiguous.
741 * Note: nfs_flush_incompatible() will already
742 * have flushed out requests having wrong owners.
743 */
744 if (offset > rqend
745 || end < req->wb_offset)
746 goto out_flushme;
747
748 if (nfs_lock_request(req))
749 break;
750
751 /* The request is locked, so wait and then retry */
752 spin_unlock(&inode->i_lock);
753 error = nfs_wait_on_request(req);
754 nfs_release_request(req);
755 if (error != 0)
756 goto out_err;
757 spin_lock(&inode->i_lock);
758 }
759
760 /* Okay, the request matches. Update the region */
761 if (offset < req->wb_offset) {
762 req->wb_offset = offset;
763 req->wb_pgbase = offset;
764 }
765 if (end > rqend)
766 req->wb_bytes = end - req->wb_offset;
767 else
768 req->wb_bytes = rqend - req->wb_offset;
769 out_unlock:
770 spin_unlock(&inode->i_lock);
771 if (req)
772 nfs_clear_request_commit(req);
773 return req;
774 out_flushme:
775 spin_unlock(&inode->i_lock);
776 nfs_release_request(req);
777 error = nfs_wb_page(inode, page);
778 out_err:
779 return ERR_PTR(error);
780 }
781
782 /*
783 * Try to update an existing write request, or create one if there is none.
784 *
785 * Note: Should always be called with the Page Lock held to prevent races
786 * if we have to add a new request. Also assumes that the caller has
787 * already called nfs_flush_incompatible() if necessary.
788 */
789 static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
790 struct page *page, unsigned int offset, unsigned int bytes)
791 {
792 struct inode *inode = page->mapping->host;
793 struct nfs_page *req;
794
795 req = nfs_try_to_update_request(inode, page, offset, bytes);
796 if (req != NULL)
797 goto out;
798 req = nfs_create_request(ctx, inode, page, offset, bytes);
799 if (IS_ERR(req))
800 goto out;
801 nfs_inode_add_request(inode, req);
802 out:
803 return req;
804 }
805
806 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
807 unsigned int offset, unsigned int count)
808 {
809 struct nfs_page *req;
810
811 req = nfs_setup_write_request(ctx, page, offset, count);
812 if (IS_ERR(req))
813 return PTR_ERR(req);
814 /* Update file length */
815 nfs_grow_file(page, offset, count);
816 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
817 nfs_mark_request_dirty(req);
818 nfs_unlock_and_release_request(req);
819 return 0;
820 }
821
822 int nfs_flush_incompatible(struct file *file, struct page *page)
823 {
824 struct nfs_open_context *ctx = nfs_file_open_context(file);
825 struct nfs_page *req;
826 int do_flush, status;
827 /*
828 * Look for a request corresponding to this page. If there
829 * is one, and it belongs to another file, we flush it out
830 * before we try to copy anything into the page. Do this
831 * due to the lack of an ACCESS-type call in NFSv2.
832 * Also do the same if we find a request from an existing
833 * dropped page.
834 */
835 do {
836 req = nfs_page_find_request(page);
837 if (req == NULL)
838 return 0;
839 do_flush = req->wb_page != page || req->wb_context != ctx ||
840 req->wb_lock_context->lockowner != current->files ||
841 req->wb_lock_context->pid != current->tgid;
842 nfs_release_request(req);
843 if (!do_flush)
844 return 0;
845 status = nfs_wb_page(page->mapping->host, page);
846 } while (status == 0);
847 return status;
848 }
849
850 /*
851 * If the page cache is marked as unsafe or invalid, then we can't rely on
852 * the PageUptodate() flag. In this case, we will need to turn off
853 * write optimisations that depend on the page contents being correct.
854 */
855 static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
856 {
857 if (nfs_have_delegated_attributes(inode))
858 goto out;
859 if (NFS_I(inode)->cache_validity & NFS_INO_REVAL_PAGECACHE)
860 return false;
861 out:
862 return PageUptodate(page) != 0;
863 }
864
865 /*
866 * Update and possibly write a cached page of an NFS file.
867 *
868 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
869 * things with a page scheduled for an RPC call (e.g. invalidate it).
870 */
871 int nfs_updatepage(struct file *file, struct page *page,
872 unsigned int offset, unsigned int count)
873 {
874 struct nfs_open_context *ctx = nfs_file_open_context(file);
875 struct inode *inode = page->mapping->host;
876 int status = 0;
877
878 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
879
880 dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n",
881 file->f_path.dentry->d_parent->d_name.name,
882 file->f_path.dentry->d_name.name, count,
883 (long long)(page_offset(page) + offset));
884
885 /* If we're not using byte range locks, and we know the page
886 * is up to date, it may be more efficient to extend the write
887 * to cover the entire page in order to avoid fragmentation
888 * inefficiencies.
889 */
890 if (nfs_write_pageuptodate(page, inode) &&
891 inode->i_flock == NULL &&
892 !(file->f_flags & O_DSYNC)) {
893 count = max(count + offset, nfs_page_length(page));
894 offset = 0;
895 }
896
897 status = nfs_writepage_setup(ctx, page, offset, count);
898 if (status < 0)
899 nfs_set_pageerror(page);
900 else
901 __set_page_dirty_nobuffers(page);
902
903 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
904 status, (long long)i_size_read(inode));
905 return status;
906 }
907
908 static int flush_task_priority(int how)
909 {
910 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
911 case FLUSH_HIGHPRI:
912 return RPC_PRIORITY_HIGH;
913 case FLUSH_LOWPRI:
914 return RPC_PRIORITY_LOW;
915 }
916 return RPC_PRIORITY_NORMAL;
917 }
918
919 int nfs_initiate_write(struct rpc_clnt *clnt,
920 struct nfs_write_data *data,
921 const struct rpc_call_ops *call_ops,
922 int how, int flags)
923 {
924 struct inode *inode = data->header->inode;
925 int priority = flush_task_priority(how);
926 struct rpc_task *task;
927 struct rpc_message msg = {
928 .rpc_argp = &data->args,
929 .rpc_resp = &data->res,
930 .rpc_cred = data->header->cred,
931 };
932 struct rpc_task_setup task_setup_data = {
933 .rpc_client = clnt,
934 .task = &data->task,
935 .rpc_message = &msg,
936 .callback_ops = call_ops,
937 .callback_data = data,
938 .workqueue = nfsiod_workqueue,
939 .flags = RPC_TASK_ASYNC | flags,
940 .priority = priority,
941 };
942 int ret = 0;
943
944 /* Set up the initial task struct. */
945 NFS_PROTO(inode)->write_setup(data, &msg);
946
947 dprintk("NFS: %5u initiated write call "
948 "(req %s/%lld, %u bytes @ offset %llu)\n",
949 data->task.tk_pid,
950 inode->i_sb->s_id,
951 (long long)NFS_FILEID(inode),
952 data->args.count,
953 (unsigned long long)data->args.offset);
954
955 task = rpc_run_task(&task_setup_data);
956 if (IS_ERR(task)) {
957 ret = PTR_ERR(task);
958 goto out;
959 }
960 if (how & FLUSH_SYNC) {
961 ret = rpc_wait_for_completion_task(task);
962 if (ret == 0)
963 ret = task->tk_status;
964 }
965 rpc_put_task(task);
966 out:
967 return ret;
968 }
969 EXPORT_SYMBOL_GPL(nfs_initiate_write);
970
971 /*
972 * Set up the argument/result storage required for the RPC call.
973 */
974 static void nfs_write_rpcsetup(struct nfs_write_data *data,
975 unsigned int count, unsigned int offset,
976 int how, struct nfs_commit_info *cinfo)
977 {
978 struct nfs_page *req = data->header->req;
979
980 /* Set up the RPC argument and reply structs
981 * NB: take care not to mess about with data->commit et al. */
982
983 data->args.fh = NFS_FH(data->header->inode);
984 data->args.offset = req_offset(req) + offset;
985 /* pnfs_set_layoutcommit needs this */
986 data->mds_offset = data->args.offset;
987 data->args.pgbase = req->wb_pgbase + offset;
988 data->args.pages = data->pages.pagevec;
989 data->args.count = count;
990 data->args.context = get_nfs_open_context(req->wb_context);
991 data->args.lock_context = req->wb_lock_context;
992 data->args.stable = NFS_UNSTABLE;
993 switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
994 case 0:
995 break;
996 case FLUSH_COND_STABLE:
997 if (nfs_reqs_to_commit(cinfo))
998 break;
999 default:
1000 data->args.stable = NFS_FILE_SYNC;
1001 }
1002
1003 data->res.fattr = &data->fattr;
1004 data->res.count = count;
1005 data->res.verf = &data->verf;
1006 nfs_fattr_init(&data->fattr);
1007 }
1008
1009 static int nfs_do_write(struct nfs_write_data *data,
1010 const struct rpc_call_ops *call_ops,
1011 int how)
1012 {
1013 struct inode *inode = data->header->inode;
1014
1015 return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how, 0);
1016 }
1017
1018 static int nfs_do_multiple_writes(struct list_head *head,
1019 const struct rpc_call_ops *call_ops,
1020 int how)
1021 {
1022 struct nfs_write_data *data;
1023 int ret = 0;
1024
1025 while (!list_empty(head)) {
1026 int ret2;
1027
1028 data = list_first_entry(head, struct nfs_write_data, list);
1029 list_del_init(&data->list);
1030
1031 ret2 = nfs_do_write(data, call_ops, how);
1032 if (ret == 0)
1033 ret = ret2;
1034 }
1035 return ret;
1036 }
1037
1038 /* If a nfs_flush_* function fails, it should remove reqs from @head and
1039 * call this on each, which will prepare them to be retried on next
1040 * writeback using standard nfs.
1041 */
1042 static void nfs_redirty_request(struct nfs_page *req)
1043 {
1044 nfs_mark_request_dirty(req);
1045 nfs_unlock_request(req);
1046 nfs_end_page_writeback(req->wb_page);
1047 nfs_release_request(req);
1048 }
1049
1050 static void nfs_async_write_error(struct list_head *head)
1051 {
1052 struct nfs_page *req;
1053
1054 while (!list_empty(head)) {
1055 req = nfs_list_entry(head->next);
1056 nfs_list_remove_request(req);
1057 nfs_redirty_request(req);
1058 }
1059 }
1060
1061 static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1062 .error_cleanup = nfs_async_write_error,
1063 .completion = nfs_write_completion,
1064 };
1065
1066 static void nfs_flush_error(struct nfs_pageio_descriptor *desc,
1067 struct nfs_pgio_header *hdr)
1068 {
1069 set_bit(NFS_IOHDR_REDO, &hdr->flags);
1070 while (!list_empty(&hdr->rpc_list)) {
1071 struct nfs_write_data *data = list_first_entry(&hdr->rpc_list,
1072 struct nfs_write_data, list);
1073 list_del(&data->list);
1074 nfs_writedata_release(data);
1075 }
1076 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
1077 }
1078
1079 /*
1080 * Generate multiple small requests to write out a single
1081 * contiguous dirty area on one page.
1082 */
1083 static int nfs_flush_multi(struct nfs_pageio_descriptor *desc,
1084 struct nfs_pgio_header *hdr)
1085 {
1086 struct nfs_page *req = hdr->req;
1087 struct page *page = req->wb_page;
1088 struct nfs_write_data *data;
1089 size_t wsize = desc->pg_bsize, nbytes;
1090 unsigned int offset;
1091 int requests = 0;
1092 struct nfs_commit_info cinfo;
1093
1094 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
1095
1096 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1097 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) ||
1098 desc->pg_count > wsize))
1099 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1100
1101
1102 offset = 0;
1103 nbytes = desc->pg_count;
1104 do {
1105 size_t len = min(nbytes, wsize);
1106
1107 data = nfs_writedata_alloc(hdr, 1);
1108 if (!data) {
1109 nfs_flush_error(desc, hdr);
1110 return -ENOMEM;
1111 }
1112 data->pages.pagevec[0] = page;
1113 nfs_write_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo);
1114 list_add(&data->list, &hdr->rpc_list);
1115 requests++;
1116 nbytes -= len;
1117 offset += len;
1118 } while (nbytes != 0);
1119 nfs_list_remove_request(req);
1120 nfs_list_add_request(req, &hdr->pages);
1121 desc->pg_rpc_callops = &nfs_write_common_ops;
1122 return 0;
1123 }
1124
1125 /*
1126 * Create an RPC task for the given write request and kick it.
1127 * The page must have been locked by the caller.
1128 *
1129 * It may happen that the page we're passed is not marked dirty.
1130 * This is the case if nfs_updatepage detects a conflicting request
1131 * that has been written but not committed.
1132 */
1133 static int nfs_flush_one(struct nfs_pageio_descriptor *desc,
1134 struct nfs_pgio_header *hdr)
1135 {
1136 struct nfs_page *req;
1137 struct page **pages;
1138 struct nfs_write_data *data;
1139 struct list_head *head = &desc->pg_list;
1140 struct nfs_commit_info cinfo;
1141
1142 data = nfs_writedata_alloc(hdr, nfs_page_array_len(desc->pg_base,
1143 desc->pg_count));
1144 if (!data) {
1145 nfs_flush_error(desc, hdr);
1146 return -ENOMEM;
1147 }
1148
1149 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
1150 pages = data->pages.pagevec;
1151 while (!list_empty(head)) {
1152 req = nfs_list_entry(head->next);
1153 nfs_list_remove_request(req);
1154 nfs_list_add_request(req, &hdr->pages);
1155 *pages++ = req->wb_page;
1156 }
1157
1158 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1159 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
1160 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1161
1162 /* Set up the argument struct */
1163 nfs_write_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
1164 list_add(&data->list, &hdr->rpc_list);
1165 desc->pg_rpc_callops = &nfs_write_common_ops;
1166 return 0;
1167 }
1168
1169 int nfs_generic_flush(struct nfs_pageio_descriptor *desc,
1170 struct nfs_pgio_header *hdr)
1171 {
1172 if (desc->pg_bsize < PAGE_CACHE_SIZE)
1173 return nfs_flush_multi(desc, hdr);
1174 return nfs_flush_one(desc, hdr);
1175 }
1176
1177 static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
1178 {
1179 struct nfs_write_header *whdr;
1180 struct nfs_pgio_header *hdr;
1181 int ret;
1182
1183 whdr = nfs_writehdr_alloc();
1184 if (!whdr) {
1185 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
1186 return -ENOMEM;
1187 }
1188 hdr = &whdr->header;
1189 nfs_pgheader_init(desc, hdr, nfs_writehdr_free);
1190 atomic_inc(&hdr->refcnt);
1191 ret = nfs_generic_flush(desc, hdr);
1192 if (ret == 0)
1193 ret = nfs_do_multiple_writes(&hdr->rpc_list,
1194 desc->pg_rpc_callops,
1195 desc->pg_ioflags);
1196 if (atomic_dec_and_test(&hdr->refcnt))
1197 hdr->completion_ops->completion(hdr);
1198 return ret;
1199 }
1200
1201 static const struct nfs_pageio_ops nfs_pageio_write_ops = {
1202 .pg_test = nfs_generic_pg_test,
1203 .pg_doio = nfs_generic_pg_writepages,
1204 };
1205
1206 void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1207 struct inode *inode, int ioflags,
1208 const struct nfs_pgio_completion_ops *compl_ops)
1209 {
1210 nfs_pageio_init(pgio, inode, &nfs_pageio_write_ops, compl_ops,
1211 NFS_SERVER(inode)->wsize, ioflags);
1212 }
1213 EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
1214
1215 void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1216 {
1217 pgio->pg_ops = &nfs_pageio_write_ops;
1218 pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1219 }
1220 EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1221
1222
1223 void nfs_write_prepare(struct rpc_task *task, void *calldata)
1224 {
1225 struct nfs_write_data *data = calldata;
1226 NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data);
1227 }
1228
1229 void nfs_commit_prepare(struct rpc_task *task, void *calldata)
1230 {
1231 struct nfs_commit_data *data = calldata;
1232
1233 NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
1234 }
1235
1236 /*
1237 * Handle a write reply that flushes a whole page.
1238 *
1239 * FIXME: There is an inherent race with invalidate_inode_pages and
1240 * writebacks since the page->count is kept > 1 for as long
1241 * as the page has a write request pending.
1242 */
1243 static void nfs_writeback_done_common(struct rpc_task *task, void *calldata)
1244 {
1245 struct nfs_write_data *data = calldata;
1246
1247 nfs_writeback_done(task, data);
1248 }
1249
1250 static void nfs_writeback_release_common(void *calldata)
1251 {
1252 struct nfs_write_data *data = calldata;
1253 struct nfs_pgio_header *hdr = data->header;
1254 int status = data->task.tk_status;
1255
1256 if ((status >= 0) && nfs_write_need_commit(data)) {
1257 spin_lock(&hdr->lock);
1258 if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags))
1259 ; /* Do nothing */
1260 else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags))
1261 memcpy(hdr->verf, &data->verf, sizeof(*hdr->verf));
1262 else if (memcmp(hdr->verf, &data->verf, sizeof(*hdr->verf)))
1263 set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags);
1264 spin_unlock(&hdr->lock);
1265 }
1266 nfs_writedata_release(data);
1267 }
1268
1269 static const struct rpc_call_ops nfs_write_common_ops = {
1270 .rpc_call_prepare = nfs_write_prepare,
1271 .rpc_call_done = nfs_writeback_done_common,
1272 .rpc_release = nfs_writeback_release_common,
1273 };
1274
1275
1276 /*
1277 * This function is called when the WRITE call is complete.
1278 */
1279 void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1280 {
1281 struct nfs_writeargs *argp = &data->args;
1282 struct nfs_writeres *resp = &data->res;
1283 struct inode *inode = data->header->inode;
1284 int status;
1285
1286 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1287 task->tk_pid, task->tk_status);
1288
1289 /*
1290 * ->write_done will attempt to use post-op attributes to detect
1291 * conflicting writes by other clients. A strict interpretation
1292 * of close-to-open would allow us to continue caching even if
1293 * another writer had changed the file, but some applications
1294 * depend on tighter cache coherency when writing.
1295 */
1296 status = NFS_PROTO(inode)->write_done(task, data);
1297 if (status != 0)
1298 return;
1299 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1300
1301 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1302 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1303 /* We tried a write call, but the server did not
1304 * commit data to stable storage even though we
1305 * requested it.
1306 * Note: There is a known bug in Tru64 < 5.0 in which
1307 * the server reports NFS_DATA_SYNC, but performs
1308 * NFS_FILE_SYNC. We therefore implement this checking
1309 * as a dprintk() in order to avoid filling syslog.
1310 */
1311 static unsigned long complain;
1312
1313 /* Note this will print the MDS for a DS write */
1314 if (time_before(complain, jiffies)) {
1315 dprintk("NFS: faulty NFS server %s:"
1316 " (committed = %d) != (stable = %d)\n",
1317 NFS_SERVER(inode)->nfs_client->cl_hostname,
1318 resp->verf->committed, argp->stable);
1319 complain = jiffies + 300 * HZ;
1320 }
1321 }
1322 #endif
1323 if (task->tk_status < 0)
1324 nfs_set_pgio_error(data->header, task->tk_status, argp->offset);
1325 else if (resp->count < argp->count) {
1326 static unsigned long complain;
1327
1328 /* This a short write! */
1329 nfs_inc_stats(inode, NFSIOS_SHORTWRITE);
1330
1331 /* Has the server at least made some progress? */
1332 if (resp->count == 0) {
1333 if (time_before(complain, jiffies)) {
1334 printk(KERN_WARNING
1335 "NFS: Server wrote zero bytes, expected %u.\n",
1336 argp->count);
1337 complain = jiffies + 300 * HZ;
1338 }
1339 nfs_set_pgio_error(data->header, -EIO, argp->offset);
1340 task->tk_status = -EIO;
1341 return;
1342 }
1343 /* Was this an NFSv2 write or an NFSv3 stable write? */
1344 if (resp->verf->committed != NFS_UNSTABLE) {
1345 /* Resend from where the server left off */
1346 data->mds_offset += resp->count;
1347 argp->offset += resp->count;
1348 argp->pgbase += resp->count;
1349 argp->count -= resp->count;
1350 } else {
1351 /* Resend as a stable write in order to avoid
1352 * headaches in the case of a server crash.
1353 */
1354 argp->stable = NFS_FILE_SYNC;
1355 }
1356 rpc_restart_call_prepare(task);
1357 }
1358 }
1359
1360
1361 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1362 static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
1363 {
1364 int ret;
1365
1366 if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
1367 return 1;
1368 if (!may_wait)
1369 return 0;
1370 ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1371 NFS_INO_COMMIT,
1372 nfs_wait_bit_killable,
1373 TASK_KILLABLE);
1374 return (ret < 0) ? ret : 1;
1375 }
1376
1377 static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
1378 {
1379 clear_bit(NFS_INO_COMMIT, &nfsi->flags);
1380 smp_mb__after_clear_bit();
1381 wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
1382 }
1383
1384 void nfs_commitdata_release(struct nfs_commit_data *data)
1385 {
1386 put_nfs_open_context(data->context);
1387 nfs_commit_free(data);
1388 }
1389 EXPORT_SYMBOL_GPL(nfs_commitdata_release);
1390
1391 int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
1392 const struct rpc_call_ops *call_ops,
1393 int how, int flags)
1394 {
1395 struct rpc_task *task;
1396 int priority = flush_task_priority(how);
1397 struct rpc_message msg = {
1398 .rpc_argp = &data->args,
1399 .rpc_resp = &data->res,
1400 .rpc_cred = data->cred,
1401 };
1402 struct rpc_task_setup task_setup_data = {
1403 .task = &data->task,
1404 .rpc_client = clnt,
1405 .rpc_message = &msg,
1406 .callback_ops = call_ops,
1407 .callback_data = data,
1408 .workqueue = nfsiod_workqueue,
1409 .flags = RPC_TASK_ASYNC | flags,
1410 .priority = priority,
1411 };
1412 /* Set up the initial task struct. */
1413 NFS_PROTO(data->inode)->commit_setup(data, &msg);
1414
1415 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1416
1417 task = rpc_run_task(&task_setup_data);
1418 if (IS_ERR(task))
1419 return PTR_ERR(task);
1420 if (how & FLUSH_SYNC)
1421 rpc_wait_for_completion_task(task);
1422 rpc_put_task(task);
1423 return 0;
1424 }
1425 EXPORT_SYMBOL_GPL(nfs_initiate_commit);
1426
1427 /*
1428 * Set up the argument/result storage required for the RPC call.
1429 */
1430 void nfs_init_commit(struct nfs_commit_data *data,
1431 struct list_head *head,
1432 struct pnfs_layout_segment *lseg,
1433 struct nfs_commit_info *cinfo)
1434 {
1435 struct nfs_page *first = nfs_list_entry(head->next);
1436 struct inode *inode = first->wb_context->dentry->d_inode;
1437
1438 /* Set up the RPC argument and reply structs
1439 * NB: take care not to mess about with data->commit et al. */
1440
1441 list_splice_init(head, &data->pages);
1442
1443 data->inode = inode;
1444 data->cred = first->wb_context->cred;
1445 data->lseg = lseg; /* reference transferred */
1446 data->mds_ops = &nfs_commit_ops;
1447 data->completion_ops = cinfo->completion_ops;
1448 data->dreq = cinfo->dreq;
1449
1450 data->args.fh = NFS_FH(data->inode);
1451 /* Note: we always request a commit of the entire inode */
1452 data->args.offset = 0;
1453 data->args.count = 0;
1454 data->context = get_nfs_open_context(first->wb_context);
1455 data->res.fattr = &data->fattr;
1456 data->res.verf = &data->verf;
1457 nfs_fattr_init(&data->fattr);
1458 }
1459 EXPORT_SYMBOL_GPL(nfs_init_commit);
1460
1461 void nfs_retry_commit(struct list_head *page_list,
1462 struct pnfs_layout_segment *lseg,
1463 struct nfs_commit_info *cinfo)
1464 {
1465 struct nfs_page *req;
1466
1467 while (!list_empty(page_list)) {
1468 req = nfs_list_entry(page_list->next);
1469 nfs_list_remove_request(req);
1470 nfs_mark_request_commit(req, lseg, cinfo);
1471 if (!cinfo->dreq) {
1472 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1473 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1474 BDI_RECLAIMABLE);
1475 }
1476 nfs_unlock_and_release_request(req);
1477 }
1478 }
1479 EXPORT_SYMBOL_GPL(nfs_retry_commit);
1480
1481 /*
1482 * Commit dirty pages
1483 */
1484 static int
1485 nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1486 struct nfs_commit_info *cinfo)
1487 {
1488 struct nfs_commit_data *data;
1489
1490 data = nfs_commitdata_alloc();
1491
1492 if (!data)
1493 goto out_bad;
1494
1495 /* Set up the argument struct */
1496 nfs_init_commit(data, head, NULL, cinfo);
1497 atomic_inc(&cinfo->mds->rpcs_out);
1498 return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops,
1499 how, 0);
1500 out_bad:
1501 nfs_retry_commit(head, NULL, cinfo);
1502 cinfo->completion_ops->error_cleanup(NFS_I(inode));
1503 return -ENOMEM;
1504 }
1505
1506 /*
1507 * COMMIT call returned
1508 */
1509 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1510 {
1511 struct nfs_commit_data *data = calldata;
1512
1513 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1514 task->tk_pid, task->tk_status);
1515
1516 /* Call the NFS version-specific code */
1517 NFS_PROTO(data->inode)->commit_done(task, data);
1518 }
1519
1520 static void nfs_commit_release_pages(struct nfs_commit_data *data)
1521 {
1522 struct nfs_page *req;
1523 int status = data->task.tk_status;
1524 struct nfs_commit_info cinfo;
1525
1526 while (!list_empty(&data->pages)) {
1527 req = nfs_list_entry(data->pages.next);
1528 nfs_list_remove_request(req);
1529 nfs_clear_page_commit(req->wb_page);
1530
1531 dprintk("NFS: commit (%s/%lld %d@%lld)",
1532 req->wb_context->dentry->d_sb->s_id,
1533 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1534 req->wb_bytes,
1535 (long long)req_offset(req));
1536 if (status < 0) {
1537 nfs_context_set_write_error(req->wb_context, status);
1538 nfs_inode_remove_request(req);
1539 dprintk(", error = %d\n", status);
1540 goto next;
1541 }
1542
1543 /* Okay, COMMIT succeeded, apparently. Check the verifier
1544 * returned by the server against all stored verfs. */
1545 if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) {
1546 /* We have a match */
1547 nfs_inode_remove_request(req);
1548 dprintk(" OK\n");
1549 goto next;
1550 }
1551 /* We have a mismatch. Write the page again */
1552 dprintk(" mismatch\n");
1553 nfs_mark_request_dirty(req);
1554 next:
1555 nfs_unlock_and_release_request(req);
1556 }
1557 nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1558 if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
1559 nfs_commit_clear_lock(NFS_I(data->inode));
1560 }
1561
1562 static void nfs_commit_release(void *calldata)
1563 {
1564 struct nfs_commit_data *data = calldata;
1565
1566 data->completion_ops->completion(data);
1567 nfs_commitdata_release(calldata);
1568 }
1569
1570 static const struct rpc_call_ops nfs_commit_ops = {
1571 .rpc_call_prepare = nfs_commit_prepare,
1572 .rpc_call_done = nfs_commit_done,
1573 .rpc_release = nfs_commit_release,
1574 };
1575
1576 static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1577 .completion = nfs_commit_release_pages,
1578 .error_cleanup = nfs_commit_clear_lock,
1579 };
1580
1581 int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1582 int how, struct nfs_commit_info *cinfo)
1583 {
1584 int status;
1585
1586 status = pnfs_commit_list(inode, head, how, cinfo);
1587 if (status == PNFS_NOT_ATTEMPTED)
1588 status = nfs_commit_list(inode, head, how, cinfo);
1589 return status;
1590 }
1591
1592 int nfs_commit_inode(struct inode *inode, int how)
1593 {
1594 LIST_HEAD(head);
1595 struct nfs_commit_info cinfo;
1596 int may_wait = how & FLUSH_SYNC;
1597 int res;
1598
1599 res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1600 if (res <= 0)
1601 goto out_mark_dirty;
1602 nfs_init_cinfo_from_inode(&cinfo, inode);
1603 res = nfs_scan_commit(inode, &head, &cinfo);
1604 if (res) {
1605 int error;
1606
1607 error = nfs_generic_commit_list(inode, &head, how, &cinfo);
1608 if (error < 0)
1609 return error;
1610 if (!may_wait)
1611 goto out_mark_dirty;
1612 error = wait_on_bit(&NFS_I(inode)->flags,
1613 NFS_INO_COMMIT,
1614 nfs_wait_bit_killable,
1615 TASK_KILLABLE);
1616 if (error < 0)
1617 return error;
1618 } else
1619 nfs_commit_clear_lock(NFS_I(inode));
1620 return res;
1621 /* Note: If we exit without ensuring that the commit is complete,
1622 * we must mark the inode as dirty. Otherwise, future calls to
1623 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1624 * that the data is on the disk.
1625 */
1626 out_mark_dirty:
1627 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1628 return res;
1629 }
1630
1631 static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
1632 {
1633 struct nfs_inode *nfsi = NFS_I(inode);
1634 int flags = FLUSH_SYNC;
1635 int ret = 0;
1636
1637 /* no commits means nothing needs to be done */
1638 if (!nfsi->commit_info.ncommit)
1639 return ret;
1640
1641 if (wbc->sync_mode == WB_SYNC_NONE) {
1642 /* Don't commit yet if this is a non-blocking flush and there
1643 * are a lot of outstanding writes for this mapping.
1644 */
1645 if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
1646 goto out_mark_dirty;
1647
1648 /* don't wait for the COMMIT response */
1649 flags = 0;
1650 }
1651
1652 ret = nfs_commit_inode(inode, flags);
1653 if (ret >= 0) {
1654 if (wbc->sync_mode == WB_SYNC_NONE) {
1655 if (ret < wbc->nr_to_write)
1656 wbc->nr_to_write -= ret;
1657 else
1658 wbc->nr_to_write = 0;
1659 }
1660 return 0;
1661 }
1662 out_mark_dirty:
1663 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1664 return ret;
1665 }
1666 #else
1667 static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
1668 {
1669 return 0;
1670 }
1671 #endif
1672
1673 int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
1674 {
1675 return nfs_commit_unstable_pages(inode, wbc);
1676 }
1677
1678 /*
1679 * flush the inode to disk.
1680 */
1681 int nfs_wb_all(struct inode *inode)
1682 {
1683 struct writeback_control wbc = {
1684 .sync_mode = WB_SYNC_ALL,
1685 .nr_to_write = LONG_MAX,
1686 .range_start = 0,
1687 .range_end = LLONG_MAX,
1688 };
1689
1690 return sync_inode(inode, &wbc);
1691 }
1692 EXPORT_SYMBOL_GPL(nfs_wb_all);
1693
1694 int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1695 {
1696 struct nfs_page *req;
1697 int ret = 0;
1698
1699 BUG_ON(!PageLocked(page));
1700 for (;;) {
1701 wait_on_page_writeback(page);
1702 req = nfs_page_find_request(page);
1703 if (req == NULL)
1704 break;
1705 if (nfs_lock_request(req)) {
1706 nfs_clear_request_commit(req);
1707 nfs_inode_remove_request(req);
1708 /*
1709 * In case nfs_inode_remove_request has marked the
1710 * page as being dirty
1711 */
1712 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1713 nfs_unlock_and_release_request(req);
1714 break;
1715 }
1716 ret = nfs_wait_on_request(req);
1717 nfs_release_request(req);
1718 if (ret < 0)
1719 break;
1720 }
1721 return ret;
1722 }
1723
1724 /*
1725 * Write back all requests on one page - we do this before reading it.
1726 */
1727 int nfs_wb_page(struct inode *inode, struct page *page)
1728 {
1729 loff_t range_start = page_offset(page);
1730 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1731 struct writeback_control wbc = {
1732 .sync_mode = WB_SYNC_ALL,
1733 .nr_to_write = 0,
1734 .range_start = range_start,
1735 .range_end = range_end,
1736 };
1737 int ret;
1738
1739 for (;;) {
1740 wait_on_page_writeback(page);
1741 if (clear_page_dirty_for_io(page)) {
1742 ret = nfs_writepage_locked(page, &wbc);
1743 if (ret < 0)
1744 goto out_error;
1745 continue;
1746 }
1747 if (!PagePrivate(page))
1748 break;
1749 ret = nfs_commit_inode(inode, FLUSH_SYNC);
1750 if (ret < 0)
1751 goto out_error;
1752 }
1753 return 0;
1754 out_error:
1755 return ret;
1756 }
1757
1758 #ifdef CONFIG_MIGRATION
1759 int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1760 struct page *page, enum migrate_mode mode)
1761 {
1762 /*
1763 * If PagePrivate is set, then the page is currently associated with
1764 * an in-progress read or write request. Don't try to migrate it.
1765 *
1766 * FIXME: we could do this in principle, but we'll need a way to ensure
1767 * that we can safely release the inode reference while holding
1768 * the page lock.
1769 */
1770 if (PagePrivate(page))
1771 return -EBUSY;
1772
1773 nfs_fscache_release_page(page, GFP_KERNEL);
1774
1775 return migrate_page(mapping, newpage, page, mode);
1776 }
1777 #endif
1778
1779 int __init nfs_init_writepagecache(void)
1780 {
1781 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1782 sizeof(struct nfs_write_header),
1783 0, SLAB_HWCACHE_ALIGN,
1784 NULL);
1785 if (nfs_wdata_cachep == NULL)
1786 return -ENOMEM;
1787
1788 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1789 nfs_wdata_cachep);
1790 if (nfs_wdata_mempool == NULL)
1791 return -ENOMEM;
1792
1793 nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
1794 sizeof(struct nfs_commit_data),
1795 0, SLAB_HWCACHE_ALIGN,
1796 NULL);
1797 if (nfs_cdata_cachep == NULL)
1798 return -ENOMEM;
1799
1800 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1801 nfs_wdata_cachep);
1802 if (nfs_commit_mempool == NULL)
1803 return -ENOMEM;
1804
1805 /*
1806 * NFS congestion size, scale with available memory.
1807 *
1808 * 64MB: 8192k
1809 * 128MB: 11585k
1810 * 256MB: 16384k
1811 * 512MB: 23170k
1812 * 1GB: 32768k
1813 * 2GB: 46340k
1814 * 4GB: 65536k
1815 * 8GB: 92681k
1816 * 16GB: 131072k
1817 *
1818 * This allows larger machines to have larger/more transfers.
1819 * Limit the default to 256M
1820 */
1821 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1822 if (nfs_congestion_kb > 256*1024)
1823 nfs_congestion_kb = 256*1024;
1824
1825 return 0;
1826 }
1827
1828 void nfs_destroy_writepagecache(void)
1829 {
1830 mempool_destroy(nfs_commit_mempool);
1831 mempool_destroy(nfs_wdata_mempool);
1832 kmem_cache_destroy(nfs_wdata_cachep);
1833 }
1834
This page took 0.09294 seconds and 5 git commands to generate.