11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/nfs/write.c 31da177e4SLinus Torvalds * 47c85d900STrond Myklebust * Write file data over NFS. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de> 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds #include <linux/types.h> 101da177e4SLinus Torvalds #include <linux/slab.h> 111da177e4SLinus Torvalds #include <linux/mm.h> 121da177e4SLinus Torvalds #include <linux/pagemap.h> 131da177e4SLinus Torvalds #include <linux/file.h> 141da177e4SLinus Torvalds #include <linux/writeback.h> 1589a09141SPeter Zijlstra #include <linux/swap.h> 16074cc1deSTrond Myklebust #include <linux/migrate.h> 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 191da177e4SLinus Torvalds #include <linux/nfs_fs.h> 201da177e4SLinus Torvalds #include <linux/nfs_mount.h> 211da177e4SLinus Torvalds #include <linux/nfs_page.h> 223fcfab16SAndrew Morton #include <linux/backing-dev.h> 233fcfab16SAndrew Morton 241da177e4SLinus Torvalds #include <asm/uaccess.h> 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds #include "delegation.h" 2749a70f27STrond Myklebust #include "internal.h" 2891d5b470SChuck Lever #include "iostat.h" 29def6ed7eSAndy Adamson #include "nfs4_fs.h" 30074cc1deSTrond Myklebust #include "fscache.h" 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_PAGECACHE 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds #define MIN_POOL_WRITE (32) 351da177e4SLinus Torvalds #define MIN_POOL_COMMIT (4) 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds /* 381da177e4SLinus Torvalds * Local function declarations 391da177e4SLinus Torvalds */ 40c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc, 41c63c7b05STrond Myklebust struct inode *inode, int ioflags); 42f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req); 43788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops; 44788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops; 45788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops; 461da177e4SLinus Torvalds 47e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep; 483feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool; 491da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool; 501da177e4SLinus Torvalds 51c9d8f89dSTrond Myklebust struct nfs_write_data *nfs_commitdata_alloc(void) 521da177e4SLinus Torvalds { 53e6b4f8daSChristoph Lameter struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS); 5440859d7eSChuck Lever 551da177e4SLinus Torvalds if (p) { 561da177e4SLinus Torvalds memset(p, 0, sizeof(*p)); 571da177e4SLinus Torvalds INIT_LIST_HEAD(&p->pages); 585f7dbd5cSAndy Adamson p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE; 591da177e4SLinus Torvalds } 601da177e4SLinus Torvalds return p; 611da177e4SLinus Torvalds } 621da177e4SLinus Torvalds 635e4424afSTrond Myklebust void nfs_commit_free(struct nfs_write_data *p) 641da177e4SLinus Torvalds { 6540859d7eSChuck Lever if (p && (p->pagevec != &p->page_array[0])) 6640859d7eSChuck Lever kfree(p->pagevec); 671da177e4SLinus Torvalds mempool_free(p, nfs_commit_mempool); 681da177e4SLinus Torvalds } 691da177e4SLinus Torvalds 708d5658c9STrond Myklebust struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount) 713feb2d49STrond Myklebust { 72e6b4f8daSChristoph Lameter struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS); 733feb2d49STrond Myklebust 743feb2d49STrond Myklebust if (p) { 753feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 763feb2d49STrond Myklebust INIT_LIST_HEAD(&p->pages); 77e9f7bee1STrond Myklebust p->npages = pagecount; 785f7dbd5cSAndy Adamson p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE; 790d0b5cb3SChuck Lever if (pagecount <= ARRAY_SIZE(p->page_array)) 800d0b5cb3SChuck Lever p->pagevec = p->page_array; 813feb2d49STrond Myklebust else { 820d0b5cb3SChuck Lever p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS); 830d0b5cb3SChuck Lever if (!p->pagevec) { 843feb2d49STrond Myklebust mempool_free(p, nfs_wdata_mempool); 853feb2d49STrond Myklebust p = NULL; 863feb2d49STrond Myklebust } 873feb2d49STrond Myklebust } 883feb2d49STrond Myklebust } 893feb2d49STrond Myklebust return p; 903feb2d49STrond Myklebust } 913feb2d49STrond Myklebust 921ae88b2eSTrond Myklebust void nfs_writedata_free(struct nfs_write_data *p) 933feb2d49STrond Myklebust { 943feb2d49STrond Myklebust if (p && (p->pagevec != &p->page_array[0])) 953feb2d49STrond Myklebust kfree(p->pagevec); 963feb2d49STrond Myklebust mempool_free(p, nfs_wdata_mempool); 973feb2d49STrond Myklebust } 983feb2d49STrond Myklebust 991ae88b2eSTrond Myklebust static void nfs_writedata_release(struct nfs_write_data *wdata) 1001da177e4SLinus Torvalds { 101383ba719STrond Myklebust put_nfs_open_context(wdata->args.context); 1021da177e4SLinus Torvalds nfs_writedata_free(wdata); 1031da177e4SLinus Torvalds } 1041da177e4SLinus Torvalds 1057b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error) 1067b159fc1STrond Myklebust { 1077b159fc1STrond Myklebust ctx->error = error; 1087b159fc1STrond Myklebust smp_wmb(); 1097b159fc1STrond Myklebust set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 1107b159fc1STrond Myklebust } 1117b159fc1STrond Myklebust 112277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page) 113277459d2STrond Myklebust { 114277459d2STrond Myklebust struct nfs_page *req = NULL; 115277459d2STrond Myklebust 116277459d2STrond Myklebust if (PagePrivate(page)) { 117277459d2STrond Myklebust req = (struct nfs_page *)page_private(page); 118277459d2STrond Myklebust if (req != NULL) 119c03b4024STrond Myklebust kref_get(&req->wb_kref); 120277459d2STrond Myklebust } 121277459d2STrond Myklebust return req; 122277459d2STrond Myklebust } 123277459d2STrond Myklebust 124277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page) 125277459d2STrond Myklebust { 126587142f8STrond Myklebust struct inode *inode = page->mapping->host; 127277459d2STrond Myklebust struct nfs_page *req = NULL; 128277459d2STrond Myklebust 129587142f8STrond Myklebust spin_lock(&inode->i_lock); 130277459d2STrond Myklebust req = nfs_page_find_request_locked(page); 131587142f8STrond Myklebust spin_unlock(&inode->i_lock); 132277459d2STrond Myklebust return req; 133277459d2STrond Myklebust } 134277459d2STrond Myklebust 1351da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */ 1361da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) 1371da177e4SLinus Torvalds { 1381da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 139a3d01454STrond Myklebust loff_t end, i_size; 140a3d01454STrond Myklebust pgoff_t end_index; 1411da177e4SLinus Torvalds 142a3d01454STrond Myklebust spin_lock(&inode->i_lock); 143a3d01454STrond Myklebust i_size = i_size_read(inode); 144a3d01454STrond Myklebust end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; 1451da177e4SLinus Torvalds if (i_size > 0 && page->index < end_index) 146a3d01454STrond Myklebust goto out; 1471da177e4SLinus Torvalds end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count); 1481da177e4SLinus Torvalds if (i_size >= end) 149a3d01454STrond Myklebust goto out; 1501da177e4SLinus Torvalds i_size_write(inode, end); 151a3d01454STrond Myklebust nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); 152a3d01454STrond Myklebust out: 153a3d01454STrond Myklebust spin_unlock(&inode->i_lock); 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds 156a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */ 157a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page) 158a301b777STrond Myklebust { 159a301b777STrond Myklebust SetPageError(page); 160a301b777STrond Myklebust nfs_zap_mapping(page->mapping->host, page->mapping); 161a301b777STrond Myklebust } 162a301b777STrond Myklebust 1631da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request 1641da177e4SLinus Torvalds * covers the full page. 1651da177e4SLinus Torvalds */ 1661da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count) 1671da177e4SLinus Torvalds { 1681da177e4SLinus Torvalds if (PageUptodate(page)) 1691da177e4SLinus Torvalds return; 1701da177e4SLinus Torvalds if (base != 0) 1711da177e4SLinus Torvalds return; 17249a70f27STrond Myklebust if (count != nfs_page_length(page)) 1731da177e4SLinus Torvalds return; 1741da177e4SLinus Torvalds SetPageUptodate(page); 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc) 1781da177e4SLinus Torvalds { 1791da177e4SLinus Torvalds if (wbc->for_reclaim) 180c63c7b05STrond Myklebust return FLUSH_HIGHPRI | FLUSH_STABLE; 181b17621feSWu Fengguang if (wbc->for_kupdate || wbc->for_background) 1821da177e4SLinus Torvalds return FLUSH_LOWPRI; 1831da177e4SLinus Torvalds return 0; 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds /* 18789a09141SPeter Zijlstra * NFS congestion control 18889a09141SPeter Zijlstra */ 18989a09141SPeter Zijlstra 19089a09141SPeter Zijlstra int nfs_congestion_kb; 19189a09141SPeter Zijlstra 19289a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) 19389a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH \ 19489a09141SPeter Zijlstra (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) 19589a09141SPeter Zijlstra 1965a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page) 19789a09141SPeter Zijlstra { 1985a6d41b3STrond Myklebust int ret = test_set_page_writeback(page); 1995a6d41b3STrond Myklebust 2005a6d41b3STrond Myklebust if (!ret) { 20189a09141SPeter Zijlstra struct inode *inode = page->mapping->host; 20289a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 20389a09141SPeter Zijlstra 204a6305ddbSTrond Myklebust page_cache_get(page); 205277866a0SPeter Zijlstra if (atomic_long_inc_return(&nfss->writeback) > 2068aa7e847SJens Axboe NFS_CONGESTION_ON_THRESH) { 2078aa7e847SJens Axboe set_bdi_congested(&nfss->backing_dev_info, 2088aa7e847SJens Axboe BLK_RW_ASYNC); 2098aa7e847SJens Axboe } 21089a09141SPeter Zijlstra } 2115a6d41b3STrond Myklebust return ret; 21289a09141SPeter Zijlstra } 21389a09141SPeter Zijlstra 21489a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page) 21589a09141SPeter Zijlstra { 21689a09141SPeter Zijlstra struct inode *inode = page->mapping->host; 21789a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 21889a09141SPeter Zijlstra 21989a09141SPeter Zijlstra end_page_writeback(page); 220a6305ddbSTrond Myklebust page_cache_release(page); 221c4dc4beeSPeter Zijlstra if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) 2228aa7e847SJens Axboe clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC); 22389a09141SPeter Zijlstra } 22489a09141SPeter Zijlstra 225074cc1deSTrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page) 226e261f51fSTrond Myklebust { 227587142f8STrond Myklebust struct inode *inode = page->mapping->host; 228e261f51fSTrond Myklebust struct nfs_page *req; 229e261f51fSTrond Myklebust int ret; 230e261f51fSTrond Myklebust 231587142f8STrond Myklebust spin_lock(&inode->i_lock); 232e261f51fSTrond Myklebust for (;;) { 233e261f51fSTrond Myklebust req = nfs_page_find_request_locked(page); 234074cc1deSTrond Myklebust if (req == NULL) 235074cc1deSTrond Myklebust break; 236acee478aSTrond Myklebust if (nfs_set_page_tag_locked(req)) 237e261f51fSTrond Myklebust break; 238e261f51fSTrond Myklebust /* Note: If we hold the page lock, as is the case in nfs_writepage, 239acee478aSTrond Myklebust * then the call to nfs_set_page_tag_locked() will always 240e261f51fSTrond Myklebust * succeed provided that someone hasn't already marked the 241e261f51fSTrond Myklebust * request as dirty (in which case we don't care). 242e261f51fSTrond Myklebust */ 243587142f8STrond Myklebust spin_unlock(&inode->i_lock); 244e261f51fSTrond Myklebust ret = nfs_wait_on_request(req); 245e261f51fSTrond Myklebust nfs_release_request(req); 246e261f51fSTrond Myklebust if (ret != 0) 247074cc1deSTrond Myklebust return ERR_PTR(ret); 248587142f8STrond Myklebust spin_lock(&inode->i_lock); 249e261f51fSTrond Myklebust } 250587142f8STrond Myklebust spin_unlock(&inode->i_lock); 251074cc1deSTrond Myklebust return req; 252612c9384STrond Myklebust } 253074cc1deSTrond Myklebust 254074cc1deSTrond Myklebust /* 255074cc1deSTrond Myklebust * Find an associated nfs write request, and prepare to flush it out 256074cc1deSTrond Myklebust * May return an error if the user signalled nfs_wait_on_request(). 257074cc1deSTrond Myklebust */ 258074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, 259074cc1deSTrond Myklebust struct page *page) 260074cc1deSTrond Myklebust { 261074cc1deSTrond Myklebust struct nfs_page *req; 262074cc1deSTrond Myklebust int ret = 0; 263074cc1deSTrond Myklebust 264074cc1deSTrond Myklebust req = nfs_find_and_lock_request(page); 265074cc1deSTrond Myklebust if (!req) 266074cc1deSTrond Myklebust goto out; 267074cc1deSTrond Myklebust ret = PTR_ERR(req); 268074cc1deSTrond Myklebust if (IS_ERR(req)) 269074cc1deSTrond Myklebust goto out; 270074cc1deSTrond Myklebust 271074cc1deSTrond Myklebust ret = nfs_set_page_writeback(page); 272074cc1deSTrond Myklebust BUG_ON(ret != 0); 273074cc1deSTrond Myklebust BUG_ON(test_bit(PG_CLEAN, &req->wb_flags)); 274074cc1deSTrond Myklebust 275f8512ad0SFred Isaman if (!nfs_pageio_add_request(pgio, req)) { 276f8512ad0SFred Isaman nfs_redirty_request(req); 277074cc1deSTrond Myklebust ret = pgio->pg_error; 278f8512ad0SFred Isaman } 279074cc1deSTrond Myklebust out: 280074cc1deSTrond Myklebust return ret; 281e261f51fSTrond Myklebust } 282e261f51fSTrond Myklebust 283f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio) 284f758c885STrond Myklebust { 285f758c885STrond Myklebust struct inode *inode = page->mapping->host; 286f758c885STrond Myklebust 287f758c885STrond Myklebust nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); 288f758c885STrond Myklebust nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); 289f758c885STrond Myklebust 290f758c885STrond Myklebust nfs_pageio_cond_complete(pgio, page->index); 291f758c885STrond Myklebust return nfs_page_async_flush(pgio, page); 292f758c885STrond Myklebust } 293f758c885STrond Myklebust 294e261f51fSTrond Myklebust /* 2951da177e4SLinus Torvalds * Write an mmapped page to the server. 2961da177e4SLinus Torvalds */ 2974d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc) 2981da177e4SLinus Torvalds { 299f758c885STrond Myklebust struct nfs_pageio_descriptor pgio; 300e261f51fSTrond Myklebust int err; 3011da177e4SLinus Torvalds 302f758c885STrond Myklebust nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc)); 303f758c885STrond Myklebust err = nfs_do_writepage(page, wbc, &pgio); 304f758c885STrond Myklebust nfs_pageio_complete(&pgio); 305f758c885STrond Myklebust if (err < 0) 3064d770ccfSTrond Myklebust return err; 307f758c885STrond Myklebust if (pgio.pg_error < 0) 308f758c885STrond Myklebust return pgio.pg_error; 309f758c885STrond Myklebust return 0; 3104d770ccfSTrond Myklebust } 3114d770ccfSTrond Myklebust 3124d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc) 3134d770ccfSTrond Myklebust { 314f758c885STrond Myklebust int ret; 3154d770ccfSTrond Myklebust 316f758c885STrond Myklebust ret = nfs_writepage_locked(page, wbc); 3171da177e4SLinus Torvalds unlock_page(page); 318f758c885STrond Myklebust return ret; 319f758c885STrond Myklebust } 320f758c885STrond Myklebust 321f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) 322f758c885STrond Myklebust { 323f758c885STrond Myklebust int ret; 324f758c885STrond Myklebust 325f758c885STrond Myklebust ret = nfs_do_writepage(page, wbc, data); 326f758c885STrond Myklebust unlock_page(page); 327f758c885STrond Myklebust return ret; 3281da177e4SLinus Torvalds } 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) 3311da177e4SLinus Torvalds { 3321da177e4SLinus Torvalds struct inode *inode = mapping->host; 33372cb77f4STrond Myklebust unsigned long *bitlock = &NFS_I(inode)->flags; 334c63c7b05STrond Myklebust struct nfs_pageio_descriptor pgio; 3351da177e4SLinus Torvalds int err; 3361da177e4SLinus Torvalds 33772cb77f4STrond Myklebust /* Stop dirtying of new pages while we sync */ 33872cb77f4STrond Myklebust err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING, 33972cb77f4STrond Myklebust nfs_wait_bit_killable, TASK_KILLABLE); 34072cb77f4STrond Myklebust if (err) 34172cb77f4STrond Myklebust goto out_err; 34272cb77f4STrond Myklebust 34391d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); 34491d5b470SChuck Lever 345c63c7b05STrond Myklebust nfs_pageio_init_write(&pgio, inode, wb_priority(wbc)); 346f758c885STrond Myklebust err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); 347c63c7b05STrond Myklebust nfs_pageio_complete(&pgio); 34872cb77f4STrond Myklebust 34972cb77f4STrond Myklebust clear_bit_unlock(NFS_INO_FLUSHING, bitlock); 35072cb77f4STrond Myklebust smp_mb__after_clear_bit(); 35172cb77f4STrond Myklebust wake_up_bit(bitlock, NFS_INO_FLUSHING); 35272cb77f4STrond Myklebust 353f758c885STrond Myklebust if (err < 0) 35472cb77f4STrond Myklebust goto out_err; 35572cb77f4STrond Myklebust err = pgio.pg_error; 35672cb77f4STrond Myklebust if (err < 0) 35772cb77f4STrond Myklebust goto out_err; 358c63c7b05STrond Myklebust return 0; 35972cb77f4STrond Myklebust out_err: 36072cb77f4STrond Myklebust return err; 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds /* 3641da177e4SLinus Torvalds * Insert a write request into an inode 3651da177e4SLinus Torvalds */ 366e7d39069STrond Myklebust static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req) 3671da177e4SLinus Torvalds { 3681da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 3691da177e4SLinus Torvalds int error; 3701da177e4SLinus Torvalds 371e7d39069STrond Myklebust error = radix_tree_preload(GFP_NOFS); 372e7d39069STrond Myklebust if (error != 0) 373e7d39069STrond Myklebust goto out; 374e7d39069STrond Myklebust 375e7d39069STrond Myklebust /* Lock the request! */ 376e7d39069STrond Myklebust nfs_lock_request_dontget(req); 377e7d39069STrond Myklebust 378e7d39069STrond Myklebust spin_lock(&inode->i_lock); 3791da177e4SLinus Torvalds error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req); 38027852596SNick Piggin BUG_ON(error); 3811da177e4SLinus Torvalds if (!nfsi->npages) { 3821da177e4SLinus Torvalds igrab(inode); 3831da177e4SLinus Torvalds if (nfs_have_delegation(inode, FMODE_WRITE)) 3841da177e4SLinus Torvalds nfsi->change_attr++; 3851da177e4SLinus Torvalds } 386deb7d638STrond Myklebust SetPagePrivate(req->wb_page); 387277459d2STrond Myklebust set_page_private(req->wb_page, (unsigned long)req); 3881da177e4SLinus Torvalds nfsi->npages++; 389c03b4024STrond Myklebust kref_get(&req->wb_kref); 39027852596SNick Piggin radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, 39127852596SNick Piggin NFS_PAGE_TAG_LOCKED); 392e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 393e7d39069STrond Myklebust radix_tree_preload_end(); 394e7d39069STrond Myklebust out: 395e7d39069STrond Myklebust return error; 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds /* 39989a09141SPeter Zijlstra * Remove a write request from an inode 4001da177e4SLinus Torvalds */ 4011da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req) 4021da177e4SLinus Torvalds { 40388be9f99STrond Myklebust struct inode *inode = req->wb_context->path.dentry->d_inode; 4041da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds BUG_ON (!NFS_WBACK_BUSY(req)); 4071da177e4SLinus Torvalds 408587142f8STrond Myklebust spin_lock(&inode->i_lock); 409277459d2STrond Myklebust set_page_private(req->wb_page, 0); 410deb7d638STrond Myklebust ClearPagePrivate(req->wb_page); 4111da177e4SLinus Torvalds radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index); 4121da177e4SLinus Torvalds nfsi->npages--; 4131da177e4SLinus Torvalds if (!nfsi->npages) { 414587142f8STrond Myklebust spin_unlock(&inode->i_lock); 4151da177e4SLinus Torvalds iput(inode); 4161da177e4SLinus Torvalds } else 417587142f8STrond Myklebust spin_unlock(&inode->i_lock); 4181da177e4SLinus Torvalds nfs_clear_request(req); 4191da177e4SLinus Torvalds nfs_release_request(req); 4201da177e4SLinus Torvalds } 4211da177e4SLinus Torvalds 42261822ab5STrond Myklebust static void 4236d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req) 42461822ab5STrond Myklebust { 42561822ab5STrond Myklebust __set_page_dirty_nobuffers(req->wb_page); 426b80c3cb6STrond Myklebust __mark_inode_dirty(req->wb_page->mapping->host, I_DIRTY_DATASYNC); 42761822ab5STrond Myklebust } 42861822ab5STrond Myklebust 4291da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 4301da177e4SLinus Torvalds /* 4311da177e4SLinus Torvalds * Add a request to the inode's commit list. 4321da177e4SLinus Torvalds */ 4331da177e4SLinus Torvalds static void 4341da177e4SLinus Torvalds nfs_mark_request_commit(struct nfs_page *req) 4351da177e4SLinus Torvalds { 43688be9f99STrond Myklebust struct inode *inode = req->wb_context->path.dentry->d_inode; 4371da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 4381da177e4SLinus Torvalds 439587142f8STrond Myklebust spin_lock(&inode->i_lock); 440e468bae9STrond Myklebust set_bit(PG_CLEAN, &(req)->wb_flags); 4415c369683STrond Myklebust radix_tree_tag_set(&nfsi->nfs_page_tree, 4425c369683STrond Myklebust req->wb_index, 4435c369683STrond Myklebust NFS_PAGE_TAG_COMMIT); 444ff778d02STrond Myklebust nfsi->ncommit++; 445587142f8STrond Myklebust spin_unlock(&inode->i_lock); 446fd39fc85SChristoph Lameter inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 447c9e51e41SPeter Zijlstra inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE); 448a1803044STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 4491da177e4SLinus Torvalds } 4508e821cadSTrond Myklebust 451e468bae9STrond Myklebust static int 452e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req) 453e468bae9STrond Myklebust { 454e468bae9STrond Myklebust struct page *page = req->wb_page; 455e468bae9STrond Myklebust 456e468bae9STrond Myklebust if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) { 457e468bae9STrond Myklebust dec_zone_page_state(page, NR_UNSTABLE_NFS); 458e468bae9STrond Myklebust dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE); 459e468bae9STrond Myklebust return 1; 460e468bae9STrond Myklebust } 461e468bae9STrond Myklebust return 0; 462e468bae9STrond Myklebust } 463e468bae9STrond Myklebust 4648e821cadSTrond Myklebust static inline 4658e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 4668e821cadSTrond Myklebust { 4678e821cadSTrond Myklebust return data->verf.committed != NFS_FILE_SYNC; 4688e821cadSTrond Myklebust } 4698e821cadSTrond Myklebust 4708e821cadSTrond Myklebust static inline 4718e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req) 4728e821cadSTrond Myklebust { 473e468bae9STrond Myklebust if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) { 4748e821cadSTrond Myklebust nfs_mark_request_commit(req); 4758e821cadSTrond Myklebust return 1; 4768e821cadSTrond Myklebust } 4778e821cadSTrond Myklebust if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) { 4786d884e8fSFred nfs_mark_request_dirty(req); 4798e821cadSTrond Myklebust return 1; 4808e821cadSTrond Myklebust } 4818e821cadSTrond Myklebust return 0; 4828e821cadSTrond Myklebust } 4838e821cadSTrond Myklebust #else 4848e821cadSTrond Myklebust static inline void 4858e821cadSTrond Myklebust nfs_mark_request_commit(struct nfs_page *req) 4868e821cadSTrond Myklebust { 4878e821cadSTrond Myklebust } 4888e821cadSTrond Myklebust 489e468bae9STrond Myklebust static inline int 490e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req) 491e468bae9STrond Myklebust { 492e468bae9STrond Myklebust return 0; 493e468bae9STrond Myklebust } 494e468bae9STrond Myklebust 4958e821cadSTrond Myklebust static inline 4968e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 4978e821cadSTrond Myklebust { 4988e821cadSTrond Myklebust return 0; 4998e821cadSTrond Myklebust } 5008e821cadSTrond Myklebust 5018e821cadSTrond Myklebust static inline 5028e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req) 5038e821cadSTrond Myklebust { 5048e821cadSTrond Myklebust return 0; 5058e821cadSTrond Myklebust } 5061da177e4SLinus Torvalds #endif 5071da177e4SLinus Torvalds 50847c62564STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 509fb8a1f11STrond Myklebust static int 510fb8a1f11STrond Myklebust nfs_need_commit(struct nfs_inode *nfsi) 511fb8a1f11STrond Myklebust { 512fb8a1f11STrond Myklebust return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT); 513fb8a1f11STrond Myklebust } 514fb8a1f11STrond Myklebust 5151da177e4SLinus Torvalds /* 5161da177e4SLinus Torvalds * nfs_scan_commit - Scan an inode for commit requests 5171da177e4SLinus Torvalds * @inode: NFS inode to scan 5181da177e4SLinus Torvalds * @dst: destination list 5191da177e4SLinus Torvalds * @idx_start: lower bound of page->index to scan. 5201da177e4SLinus Torvalds * @npages: idx_start + npages sets the upper bound to scan. 5211da177e4SLinus Torvalds * 5221da177e4SLinus Torvalds * Moves requests from the inode's 'commit' request list. 5231da177e4SLinus Torvalds * The requests are *not* checked to ensure that they form a contiguous set. 5241da177e4SLinus Torvalds */ 5251da177e4SLinus Torvalds static int 526ca52fec1STrond Myklebust nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages) 5271da177e4SLinus Torvalds { 5281da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 529ff778d02STrond Myklebust int ret; 5303da28eb1STrond Myklebust 531fb8a1f11STrond Myklebust if (!nfs_need_commit(nfsi)) 532fb8a1f11STrond Myklebust return 0; 533fb8a1f11STrond Myklebust 534ff778d02STrond Myklebust ret = nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT); 535ff778d02STrond Myklebust if (ret > 0) 536ff778d02STrond Myklebust nfsi->ncommit -= ret; 5372928db1fSTrond Myklebust if (nfs_need_commit(NFS_I(inode))) 5382928db1fSTrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 539ff778d02STrond Myklebust return ret; 5401da177e4SLinus Torvalds } 541c42de9ddSTrond Myklebust #else 542fb8a1f11STrond Myklebust static inline int nfs_need_commit(struct nfs_inode *nfsi) 543fb8a1f11STrond Myklebust { 544fb8a1f11STrond Myklebust return 0; 545fb8a1f11STrond Myklebust } 546fb8a1f11STrond Myklebust 547ca52fec1STrond Myklebust static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages) 548c42de9ddSTrond Myklebust { 549c42de9ddSTrond Myklebust return 0; 550c42de9ddSTrond Myklebust } 5511da177e4SLinus Torvalds #endif 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds /* 554e7d39069STrond Myklebust * Search for an existing write request, and attempt to update 555e7d39069STrond Myklebust * it to reflect a new dirty region on a given page. 5561da177e4SLinus Torvalds * 557e7d39069STrond Myklebust * If the attempt fails, then the existing request is flushed out 558e7d39069STrond Myklebust * to disk. 5591da177e4SLinus Torvalds */ 560e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode, 561e7d39069STrond Myklebust struct page *page, 562e7d39069STrond Myklebust unsigned int offset, 563e7d39069STrond Myklebust unsigned int bytes) 5641da177e4SLinus Torvalds { 565e7d39069STrond Myklebust struct nfs_page *req; 566e7d39069STrond Myklebust unsigned int rqend; 567e7d39069STrond Myklebust unsigned int end; 5681da177e4SLinus Torvalds int error; 569277459d2STrond Myklebust 570e7d39069STrond Myklebust if (!PagePrivate(page)) 571e7d39069STrond Myklebust return NULL; 572e7d39069STrond Myklebust 573e7d39069STrond Myklebust end = offset + bytes; 574e7d39069STrond Myklebust spin_lock(&inode->i_lock); 575e7d39069STrond Myklebust 576e7d39069STrond Myklebust for (;;) { 577e7d39069STrond Myklebust req = nfs_page_find_request_locked(page); 578e7d39069STrond Myklebust if (req == NULL) 579e7d39069STrond Myklebust goto out_unlock; 580e7d39069STrond Myklebust 581e7d39069STrond Myklebust rqend = req->wb_offset + req->wb_bytes; 582e7d39069STrond Myklebust /* 583e7d39069STrond Myklebust * Tell the caller to flush out the request if 584e7d39069STrond Myklebust * the offsets are non-contiguous. 585e7d39069STrond Myklebust * Note: nfs_flush_incompatible() will already 586e7d39069STrond Myklebust * have flushed out requests having wrong owners. 587e7d39069STrond Myklebust */ 588e468bae9STrond Myklebust if (offset > rqend 589e7d39069STrond Myklebust || end < req->wb_offset) 590e7d39069STrond Myklebust goto out_flushme; 591e7d39069STrond Myklebust 592e7d39069STrond Myklebust if (nfs_set_page_tag_locked(req)) 593e7d39069STrond Myklebust break; 594e7d39069STrond Myklebust 595e7d39069STrond Myklebust /* The request is locked, so wait and then retry */ 596587142f8STrond Myklebust spin_unlock(&inode->i_lock); 5971da177e4SLinus Torvalds error = nfs_wait_on_request(req); 5981da177e4SLinus Torvalds nfs_release_request(req); 599e7d39069STrond Myklebust if (error != 0) 600e7d39069STrond Myklebust goto out_err; 601e7d39069STrond Myklebust spin_lock(&inode->i_lock); 6021da177e4SLinus Torvalds } 6031da177e4SLinus Torvalds 604ff778d02STrond Myklebust if (nfs_clear_request_commit(req) && 605e468bae9STrond Myklebust radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree, 606ff778d02STrond Myklebust req->wb_index, NFS_PAGE_TAG_COMMIT) != NULL) 607ff778d02STrond Myklebust NFS_I(inode)->ncommit--; 608e468bae9STrond Myklebust 6091da177e4SLinus Torvalds /* Okay, the request matches. Update the region */ 6101da177e4SLinus Torvalds if (offset < req->wb_offset) { 6111da177e4SLinus Torvalds req->wb_offset = offset; 6121da177e4SLinus Torvalds req->wb_pgbase = offset; 6131da177e4SLinus Torvalds } 6141da177e4SLinus Torvalds if (end > rqend) 6151da177e4SLinus Torvalds req->wb_bytes = end - req->wb_offset; 616e7d39069STrond Myklebust else 617e7d39069STrond Myklebust req->wb_bytes = rqend - req->wb_offset; 618e7d39069STrond Myklebust out_unlock: 619e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 620e7d39069STrond Myklebust return req; 621e7d39069STrond Myklebust out_flushme: 622e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 623e7d39069STrond Myklebust nfs_release_request(req); 624e7d39069STrond Myklebust error = nfs_wb_page(inode, page); 625e7d39069STrond Myklebust out_err: 626e7d39069STrond Myklebust return ERR_PTR(error); 627e7d39069STrond Myklebust } 6281da177e4SLinus Torvalds 629e7d39069STrond Myklebust /* 630e7d39069STrond Myklebust * Try to update an existing write request, or create one if there is none. 631e7d39069STrond Myklebust * 632e7d39069STrond Myklebust * Note: Should always be called with the Page Lock held to prevent races 633e7d39069STrond Myklebust * if we have to add a new request. Also assumes that the caller has 634e7d39069STrond Myklebust * already called nfs_flush_incompatible() if necessary. 635e7d39069STrond Myklebust */ 636e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, 637e7d39069STrond Myklebust struct page *page, unsigned int offset, unsigned int bytes) 638e7d39069STrond Myklebust { 639e7d39069STrond Myklebust struct inode *inode = page->mapping->host; 640e7d39069STrond Myklebust struct nfs_page *req; 641e7d39069STrond Myklebust int error; 642e7d39069STrond Myklebust 643e7d39069STrond Myklebust req = nfs_try_to_update_request(inode, page, offset, bytes); 644e7d39069STrond Myklebust if (req != NULL) 645e7d39069STrond Myklebust goto out; 646e7d39069STrond Myklebust req = nfs_create_request(ctx, inode, page, offset, bytes); 647e7d39069STrond Myklebust if (IS_ERR(req)) 648e7d39069STrond Myklebust goto out; 649e7d39069STrond Myklebust error = nfs_inode_add_request(inode, req); 650e7d39069STrond Myklebust if (error != 0) { 651e7d39069STrond Myklebust nfs_release_request(req); 652e7d39069STrond Myklebust req = ERR_PTR(error); 653e7d39069STrond Myklebust } 654efc91ed0STrond Myklebust out: 65561e930a9STrond Myklebust return req; 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds 658e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, 659e7d39069STrond Myklebust unsigned int offset, unsigned int count) 660e7d39069STrond Myklebust { 661e7d39069STrond Myklebust struct nfs_page *req; 662e7d39069STrond Myklebust 663e7d39069STrond Myklebust req = nfs_setup_write_request(ctx, page, offset, count); 664e7d39069STrond Myklebust if (IS_ERR(req)) 665e7d39069STrond Myklebust return PTR_ERR(req); 666b80c3cb6STrond Myklebust nfs_mark_request_dirty(req); 667e7d39069STrond Myklebust /* Update file length */ 668e7d39069STrond Myklebust nfs_grow_file(page, offset, count); 669e7d39069STrond Myklebust nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); 670a6305ddbSTrond Myklebust nfs_mark_request_dirty(req); 671e7d39069STrond Myklebust nfs_clear_page_tag_locked(req); 672e7d39069STrond Myklebust return 0; 673e7d39069STrond Myklebust } 674e7d39069STrond Myklebust 6751da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page) 6761da177e4SLinus Torvalds { 677cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 6781da177e4SLinus Torvalds struct nfs_page *req; 6791a54533eSTrond Myklebust int do_flush, status; 6801da177e4SLinus Torvalds /* 6811da177e4SLinus Torvalds * Look for a request corresponding to this page. If there 6821da177e4SLinus Torvalds * is one, and it belongs to another file, we flush it out 6831da177e4SLinus Torvalds * before we try to copy anything into the page. Do this 6841da177e4SLinus Torvalds * due to the lack of an ACCESS-type call in NFSv2. 6851da177e4SLinus Torvalds * Also do the same if we find a request from an existing 6861da177e4SLinus Torvalds * dropped page. 6871da177e4SLinus Torvalds */ 6881a54533eSTrond Myklebust do { 689277459d2STrond Myklebust req = nfs_page_find_request(page); 6901a54533eSTrond Myklebust if (req == NULL) 6911a54533eSTrond Myklebust return 0; 692e468bae9STrond Myklebust do_flush = req->wb_page != page || req->wb_context != ctx; 6931da177e4SLinus Torvalds nfs_release_request(req); 6941a54533eSTrond Myklebust if (!do_flush) 6951a54533eSTrond Myklebust return 0; 696277459d2STrond Myklebust status = nfs_wb_page(page->mapping->host, page); 6971a54533eSTrond Myklebust } while (status == 0); 6981a54533eSTrond Myklebust return status; 6991da177e4SLinus Torvalds } 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds /* 7025d47a356STrond Myklebust * If the page cache is marked as unsafe or invalid, then we can't rely on 7035d47a356STrond Myklebust * the PageUptodate() flag. In this case, we will need to turn off 7045d47a356STrond Myklebust * write optimisations that depend on the page contents being correct. 7055d47a356STrond Myklebust */ 7065d47a356STrond Myklebust static int nfs_write_pageuptodate(struct page *page, struct inode *inode) 7075d47a356STrond Myklebust { 7085d47a356STrond Myklebust return PageUptodate(page) && 7095d47a356STrond Myklebust !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA)); 7105d47a356STrond Myklebust } 7115d47a356STrond Myklebust 7125d47a356STrond Myklebust /* 7131da177e4SLinus Torvalds * Update and possibly write a cached page of an NFS file. 7141da177e4SLinus Torvalds * 7151da177e4SLinus Torvalds * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad 7161da177e4SLinus Torvalds * things with a page scheduled for an RPC call (e.g. invalidate it). 7171da177e4SLinus Torvalds */ 7181da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page, 7191da177e4SLinus Torvalds unsigned int offset, unsigned int count) 7201da177e4SLinus Torvalds { 721cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 7221da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 7231da177e4SLinus Torvalds int status = 0; 7241da177e4SLinus Torvalds 72591d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 72691d5b470SChuck Lever 72748186c7dSChuck Lever dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n", 72801cce933SJosef "Jeff" Sipek file->f_path.dentry->d_parent->d_name.name, 72901cce933SJosef "Jeff" Sipek file->f_path.dentry->d_name.name, count, 7300bbacc40SChuck Lever (long long)(page_offset(page) + offset)); 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds /* If we're not using byte range locks, and we know the page 7335d47a356STrond Myklebust * is up to date, it may be more efficient to extend the write 7345d47a356STrond Myklebust * to cover the entire page in order to avoid fragmentation 7355d47a356STrond Myklebust * inefficiencies. 7361da177e4SLinus Torvalds */ 7375d47a356STrond Myklebust if (nfs_write_pageuptodate(page, inode) && 7385d47a356STrond Myklebust inode->i_flock == NULL && 7396b2f3d1fSChristoph Hellwig !(file->f_flags & O_DSYNC)) { 74049a70f27STrond Myklebust count = max(count + offset, nfs_page_length(page)); 7411da177e4SLinus Torvalds offset = 0; 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds 744e21195a7STrond Myklebust status = nfs_writepage_setup(ctx, page, offset, count); 74503fa9e84STrond Myklebust if (status < 0) 74603fa9e84STrond Myklebust nfs_set_pageerror(page); 7471da177e4SLinus Torvalds 74848186c7dSChuck Lever dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", 7491da177e4SLinus Torvalds status, (long long)i_size_read(inode)); 7501da177e4SLinus Torvalds return status; 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds static void nfs_writepage_release(struct nfs_page *req) 7541da177e4SLinus Torvalds { 755a6305ddbSTrond Myklebust struct page *page = req->wb_page; 7568e821cadSTrond Myklebust 757a6305ddbSTrond Myklebust if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) 7581da177e4SLinus Torvalds nfs_inode_remove_request(req); 7599fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 760a6305ddbSTrond Myklebust nfs_end_page_writeback(page); 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds 7633ff7576dSTrond Myklebust static int flush_task_priority(int how) 7641da177e4SLinus Torvalds { 7651da177e4SLinus Torvalds switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { 7661da177e4SLinus Torvalds case FLUSH_HIGHPRI: 7671da177e4SLinus Torvalds return RPC_PRIORITY_HIGH; 7681da177e4SLinus Torvalds case FLUSH_LOWPRI: 7691da177e4SLinus Torvalds return RPC_PRIORITY_LOW; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds return RPC_PRIORITY_NORMAL; 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds /* 7751da177e4SLinus Torvalds * Set up the argument/result storage required for the RPC call. 7761da177e4SLinus Torvalds */ 777dbae4c73STrond Myklebust static int nfs_write_rpcsetup(struct nfs_page *req, 7781da177e4SLinus Torvalds struct nfs_write_data *data, 779788e7a89STrond Myklebust const struct rpc_call_ops *call_ops, 7801da177e4SLinus Torvalds unsigned int count, unsigned int offset, 7811da177e4SLinus Torvalds int how) 7821da177e4SLinus Torvalds { 78384115e1cSTrond Myklebust struct inode *inode = req->wb_context->path.dentry->d_inode; 7843ff7576dSTrond Myklebust int priority = flush_task_priority(how); 78507737691STrond Myklebust struct rpc_task *task; 786bdc7f021STrond Myklebust struct rpc_message msg = { 787bdc7f021STrond Myklebust .rpc_argp = &data->args, 788bdc7f021STrond Myklebust .rpc_resp = &data->res, 789bdc7f021STrond Myklebust .rpc_cred = req->wb_context->cred, 790bdc7f021STrond Myklebust }; 79184115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 79284115e1cSTrond Myklebust .rpc_client = NFS_CLIENT(inode), 79307737691STrond Myklebust .task = &data->task, 794bdc7f021STrond Myklebust .rpc_message = &msg, 79584115e1cSTrond Myklebust .callback_ops = call_ops, 79684115e1cSTrond Myklebust .callback_data = data, 797101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 798*2c61be0aSTrond Myklebust .flags = RPC_TASK_ASYNC, 7993ff7576dSTrond Myklebust .priority = priority, 80084115e1cSTrond Myklebust }; 801*2c61be0aSTrond Myklebust int ret = 0; 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds /* Set up the RPC argument and reply structs 8041da177e4SLinus Torvalds * NB: take care not to mess about with data->commit et al. */ 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds data->req = req; 80788be9f99STrond Myklebust data->inode = inode = req->wb_context->path.dentry->d_inode; 808bdc7f021STrond Myklebust data->cred = msg.rpc_cred; 8091da177e4SLinus Torvalds 8101da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 8111da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 8121da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 8131da177e4SLinus Torvalds data->args.pages = data->pagevec; 8141da177e4SLinus Torvalds data->args.count = count; 815383ba719STrond Myklebust data->args.context = get_nfs_open_context(req->wb_context); 816bdc7f021STrond Myklebust data->args.stable = NFS_UNSTABLE; 817bdc7f021STrond Myklebust if (how & FLUSH_STABLE) { 818bdc7f021STrond Myklebust data->args.stable = NFS_DATA_SYNC; 819fb8a1f11STrond Myklebust if (!nfs_need_commit(NFS_I(inode))) 820bdc7f021STrond Myklebust data->args.stable = NFS_FILE_SYNC; 821bdc7f021STrond Myklebust } 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds data->res.fattr = &data->fattr; 8241da177e4SLinus Torvalds data->res.count = count; 8251da177e4SLinus Torvalds data->res.verf = &data->verf; 8260e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 8271da177e4SLinus Torvalds 828788e7a89STrond Myklebust /* Set up the initial task struct. */ 829bdc7f021STrond Myklebust NFS_PROTO(inode)->write_setup(data, &msg); 8301da177e4SLinus Torvalds 831a3f565b1SChuck Lever dprintk("NFS: %5u initiated write call " 83248186c7dSChuck Lever "(req %s/%lld, %u bytes @ offset %llu)\n", 8330bbacc40SChuck Lever data->task.tk_pid, 8341da177e4SLinus Torvalds inode->i_sb->s_id, 8351da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 8361da177e4SLinus Torvalds count, 8371da177e4SLinus Torvalds (unsigned long long)data->args.offset); 8381da177e4SLinus Torvalds 83907737691STrond Myklebust task = rpc_run_task(&task_setup_data); 840*2c61be0aSTrond Myklebust if (IS_ERR(task)) { 841*2c61be0aSTrond Myklebust ret = PTR_ERR(task); 842*2c61be0aSTrond Myklebust goto out; 843*2c61be0aSTrond Myklebust } 844*2c61be0aSTrond Myklebust if (how & FLUSH_SYNC) { 845*2c61be0aSTrond Myklebust ret = rpc_wait_for_completion_task(task); 846*2c61be0aSTrond Myklebust if (ret == 0) 847*2c61be0aSTrond Myklebust ret = task->tk_status; 848*2c61be0aSTrond Myklebust } 84907737691STrond Myklebust rpc_put_task(task); 850*2c61be0aSTrond Myklebust out: 851*2c61be0aSTrond Myklebust return ret; 8521da177e4SLinus Torvalds } 8531da177e4SLinus Torvalds 8546d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and 8556d884e8fSFred * call this on each, which will prepare them to be retried on next 8566d884e8fSFred * writeback using standard nfs. 8576d884e8fSFred */ 8586d884e8fSFred static void nfs_redirty_request(struct nfs_page *req) 8596d884e8fSFred { 860a6305ddbSTrond Myklebust struct page *page = req->wb_page; 861a6305ddbSTrond Myklebust 8626d884e8fSFred nfs_mark_request_dirty(req); 8636d884e8fSFred nfs_clear_page_tag_locked(req); 864a6305ddbSTrond Myklebust nfs_end_page_writeback(page); 8656d884e8fSFred } 8666d884e8fSFred 8671da177e4SLinus Torvalds /* 8681da177e4SLinus Torvalds * Generate multiple small requests to write out a single 8691da177e4SLinus Torvalds * contiguous dirty area on one page. 8701da177e4SLinus Torvalds */ 8718d5658c9STrond Myklebust static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how) 8721da177e4SLinus Torvalds { 8731da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(head->next); 8741da177e4SLinus Torvalds struct page *page = req->wb_page; 8751da177e4SLinus Torvalds struct nfs_write_data *data; 876e9f7bee1STrond Myklebust size_t wsize = NFS_SERVER(inode)->wsize, nbytes; 877e9f7bee1STrond Myklebust unsigned int offset; 8781da177e4SLinus Torvalds int requests = 0; 879dbae4c73STrond Myklebust int ret = 0; 8801da177e4SLinus Torvalds LIST_HEAD(list); 8811da177e4SLinus Torvalds 8821da177e4SLinus Torvalds nfs_list_remove_request(req); 8831da177e4SLinus Torvalds 884bcb71bbaSTrond Myklebust nbytes = count; 885e9f7bee1STrond Myklebust do { 886e9f7bee1STrond Myklebust size_t len = min(nbytes, wsize); 887e9f7bee1STrond Myklebust 8888d5658c9STrond Myklebust data = nfs_writedata_alloc(1); 8891da177e4SLinus Torvalds if (!data) 8901da177e4SLinus Torvalds goto out_bad; 8911da177e4SLinus Torvalds list_add(&data->pages, &list); 8921da177e4SLinus Torvalds requests++; 893e9f7bee1STrond Myklebust nbytes -= len; 894e9f7bee1STrond Myklebust } while (nbytes != 0); 8951da177e4SLinus Torvalds atomic_set(&req->wb_complete, requests); 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds ClearPageError(page); 8981da177e4SLinus Torvalds offset = 0; 899bcb71bbaSTrond Myklebust nbytes = count; 9001da177e4SLinus Torvalds do { 901dbae4c73STrond Myklebust int ret2; 902dbae4c73STrond Myklebust 9031da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_write_data, pages); 9041da177e4SLinus Torvalds list_del_init(&data->pages); 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds data->pagevec[0] = page; 9071da177e4SLinus Torvalds 908bcb71bbaSTrond Myklebust if (nbytes < wsize) 909bcb71bbaSTrond Myklebust wsize = nbytes; 910dbae4c73STrond Myklebust ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops, 911788e7a89STrond Myklebust wsize, offset, how); 912dbae4c73STrond Myklebust if (ret == 0) 913dbae4c73STrond Myklebust ret = ret2; 9141da177e4SLinus Torvalds offset += wsize; 9151da177e4SLinus Torvalds nbytes -= wsize; 9161da177e4SLinus Torvalds } while (nbytes != 0); 9171da177e4SLinus Torvalds 918dbae4c73STrond Myklebust return ret; 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds out_bad: 9211da177e4SLinus Torvalds while (!list_empty(&list)) { 9221da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_write_data, pages); 9231da177e4SLinus Torvalds list_del(&data->pages); 9248aca67f0STrond Myklebust nfs_writedata_release(data); 9251da177e4SLinus Torvalds } 92661822ab5STrond Myklebust nfs_redirty_request(req); 9271da177e4SLinus Torvalds return -ENOMEM; 9281da177e4SLinus Torvalds } 9291da177e4SLinus Torvalds 9301da177e4SLinus Torvalds /* 9311da177e4SLinus Torvalds * Create an RPC task for the given write request and kick it. 9321da177e4SLinus Torvalds * The page must have been locked by the caller. 9331da177e4SLinus Torvalds * 9341da177e4SLinus Torvalds * It may happen that the page we're passed is not marked dirty. 9351da177e4SLinus Torvalds * This is the case if nfs_updatepage detects a conflicting request 9361da177e4SLinus Torvalds * that has been written but not committed. 9371da177e4SLinus Torvalds */ 9388d5658c9STrond Myklebust static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how) 9391da177e4SLinus Torvalds { 9401da177e4SLinus Torvalds struct nfs_page *req; 9411da177e4SLinus Torvalds struct page **pages; 9421da177e4SLinus Torvalds struct nfs_write_data *data; 9431da177e4SLinus Torvalds 9448d5658c9STrond Myklebust data = nfs_writedata_alloc(npages); 9451da177e4SLinus Torvalds if (!data) 9461da177e4SLinus Torvalds goto out_bad; 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds pages = data->pagevec; 9491da177e4SLinus Torvalds while (!list_empty(head)) { 9501da177e4SLinus Torvalds req = nfs_list_entry(head->next); 9511da177e4SLinus Torvalds nfs_list_remove_request(req); 9521da177e4SLinus Torvalds nfs_list_add_request(req, &data->pages); 9531da177e4SLinus Torvalds ClearPageError(req->wb_page); 9541da177e4SLinus Torvalds *pages++ = req->wb_page; 9551da177e4SLinus Torvalds } 9561da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 9571da177e4SLinus Torvalds 9581da177e4SLinus Torvalds /* Set up the argument struct */ 959dbae4c73STrond Myklebust return nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how); 9601da177e4SLinus Torvalds out_bad: 9611da177e4SLinus Torvalds while (!list_empty(head)) { 96210afec90STrond Myklebust req = nfs_list_entry(head->next); 9631da177e4SLinus Torvalds nfs_list_remove_request(req); 96461822ab5STrond Myklebust nfs_redirty_request(req); 9651da177e4SLinus Torvalds } 9661da177e4SLinus Torvalds return -ENOMEM; 9671da177e4SLinus Torvalds } 9681da177e4SLinus Torvalds 969c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, 970c63c7b05STrond Myklebust struct inode *inode, int ioflags) 9711da177e4SLinus Torvalds { 972bf4285e7SChuck Lever size_t wsize = NFS_SERVER(inode)->wsize; 9731da177e4SLinus Torvalds 974bcb71bbaSTrond Myklebust if (wsize < PAGE_CACHE_SIZE) 975c63c7b05STrond Myklebust nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags); 976bcb71bbaSTrond Myklebust else 977c63c7b05STrond Myklebust nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags); 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds /* 9811da177e4SLinus Torvalds * Handle a write reply that flushed part of a page. 9821da177e4SLinus Torvalds */ 983788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata) 9841da177e4SLinus Torvalds { 985788e7a89STrond Myklebust struct nfs_write_data *data = calldata; 9861da177e4SLinus Torvalds 98748186c7dSChuck Lever dprintk("NFS: %5u write(%s/%lld %d@%lld)", 98848186c7dSChuck Lever task->tk_pid, 98948186c7dSChuck Lever data->req->wb_context->path.dentry->d_inode->i_sb->s_id, 99048186c7dSChuck Lever (long long) 99148186c7dSChuck Lever NFS_FILEID(data->req->wb_context->path.dentry->d_inode), 99248186c7dSChuck Lever data->req->wb_bytes, (long long)req_offset(data->req)); 9931da177e4SLinus Torvalds 994c9d8f89dSTrond Myklebust nfs_writeback_done(task, data); 995c9d8f89dSTrond Myklebust } 996788e7a89STrond Myklebust 997c9d8f89dSTrond Myklebust static void nfs_writeback_release_partial(void *calldata) 998c9d8f89dSTrond Myklebust { 999c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1000c9d8f89dSTrond Myklebust struct nfs_page *req = data->req; 1001c9d8f89dSTrond Myklebust struct page *page = req->wb_page; 1002c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1003c9d8f89dSTrond Myklebust 1004c9d8f89dSTrond Myklebust if (status < 0) { 1005a301b777STrond Myklebust nfs_set_pageerror(page); 1006c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 1007c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 10088e821cadSTrond Myklebust goto out; 10098e821cadSTrond Myklebust } 10108e821cadSTrond Myklebust 10118e821cadSTrond Myklebust if (nfs_write_need_commit(data)) { 1012587142f8STrond Myklebust struct inode *inode = page->mapping->host; 10138e821cadSTrond Myklebust 1014587142f8STrond Myklebust spin_lock(&inode->i_lock); 10158e821cadSTrond Myklebust if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) { 10168e821cadSTrond Myklebust /* Do nothing we need to resend the writes */ 10178e821cadSTrond Myklebust } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) { 10181da177e4SLinus Torvalds memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); 10191da177e4SLinus Torvalds dprintk(" defer commit\n"); 10201da177e4SLinus Torvalds } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) { 10218e821cadSTrond Myklebust set_bit(PG_NEED_RESCHED, &req->wb_flags); 10228e821cadSTrond Myklebust clear_bit(PG_NEED_COMMIT, &req->wb_flags); 10231da177e4SLinus Torvalds dprintk(" server reboot detected\n"); 10241da177e4SLinus Torvalds } 1025587142f8STrond Myklebust spin_unlock(&inode->i_lock); 10261da177e4SLinus Torvalds } else 10271da177e4SLinus Torvalds dprintk(" OK\n"); 10281da177e4SLinus Torvalds 10298e821cadSTrond Myklebust out: 10301da177e4SLinus Torvalds if (atomic_dec_and_test(&req->wb_complete)) 10311da177e4SLinus Torvalds nfs_writepage_release(req); 1032c9d8f89dSTrond Myklebust nfs_writedata_release(calldata); 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds 1035def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1) 1036def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata) 1037def6ed7eSAndy Adamson { 1038def6ed7eSAndy Adamson struct nfs_write_data *data = calldata; 1039def6ed7eSAndy Adamson struct nfs_client *clp = (NFS_SERVER(data->inode))->nfs_client; 1040def6ed7eSAndy Adamson 1041def6ed7eSAndy Adamson if (nfs4_setup_sequence(clp, &data->args.seq_args, 1042def6ed7eSAndy Adamson &data->res.seq_res, 1, task)) 1043def6ed7eSAndy Adamson return; 1044def6ed7eSAndy Adamson rpc_call_start(task); 1045def6ed7eSAndy Adamson } 1046def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1047def6ed7eSAndy Adamson 1048788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = { 1049def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1) 1050def6ed7eSAndy Adamson .rpc_call_prepare = nfs_write_prepare, 1051def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1052788e7a89STrond Myklebust .rpc_call_done = nfs_writeback_done_partial, 1053c9d8f89dSTrond Myklebust .rpc_release = nfs_writeback_release_partial, 1054788e7a89STrond Myklebust }; 1055788e7a89STrond Myklebust 10561da177e4SLinus Torvalds /* 10571da177e4SLinus Torvalds * Handle a write reply that flushes a whole page. 10581da177e4SLinus Torvalds * 10591da177e4SLinus Torvalds * FIXME: There is an inherent race with invalidate_inode_pages and 10601da177e4SLinus Torvalds * writebacks since the page->count is kept > 1 for as long 10611da177e4SLinus Torvalds * as the page has a write request pending. 10621da177e4SLinus Torvalds */ 1063788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata) 10641da177e4SLinus Torvalds { 1065788e7a89STrond Myklebust struct nfs_write_data *data = calldata; 10661da177e4SLinus Torvalds 1067c9d8f89dSTrond Myklebust nfs_writeback_done(task, data); 1068c9d8f89dSTrond Myklebust } 1069c9d8f89dSTrond Myklebust 1070c9d8f89dSTrond Myklebust static void nfs_writeback_release_full(void *calldata) 1071c9d8f89dSTrond Myklebust { 1072c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1073c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1074788e7a89STrond Myklebust 10751da177e4SLinus Torvalds /* Update attributes as result of writeback. */ 10761da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 1077c9d8f89dSTrond Myklebust struct nfs_page *req = nfs_list_entry(data->pages.next); 1078c9d8f89dSTrond Myklebust struct page *page = req->wb_page; 1079c9d8f89dSTrond Myklebust 10801da177e4SLinus Torvalds nfs_list_remove_request(req); 10811da177e4SLinus Torvalds 108248186c7dSChuck Lever dprintk("NFS: %5u write (%s/%lld %d@%lld)", 108348186c7dSChuck Lever data->task.tk_pid, 108488be9f99STrond Myklebust req->wb_context->path.dentry->d_inode->i_sb->s_id, 108588be9f99STrond Myklebust (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 10861da177e4SLinus Torvalds req->wb_bytes, 10871da177e4SLinus Torvalds (long long)req_offset(req)); 10881da177e4SLinus Torvalds 1089c9d8f89dSTrond Myklebust if (status < 0) { 1090a301b777STrond Myklebust nfs_set_pageerror(page); 1091c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 1092c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 10938e821cadSTrond Myklebust goto remove_request; 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds 10968e821cadSTrond Myklebust if (nfs_write_need_commit(data)) { 10971da177e4SLinus Torvalds memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); 10981da177e4SLinus Torvalds nfs_mark_request_commit(req); 10991da177e4SLinus Torvalds dprintk(" marked for commit\n"); 11008e821cadSTrond Myklebust goto next; 11018e821cadSTrond Myklebust } 11028e821cadSTrond Myklebust dprintk(" OK\n"); 11038e821cadSTrond Myklebust remove_request: 11041da177e4SLinus Torvalds nfs_inode_remove_request(req); 11051da177e4SLinus Torvalds next: 11069fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 1107a6305ddbSTrond Myklebust nfs_end_page_writeback(page); 11081da177e4SLinus Torvalds } 1109c9d8f89dSTrond Myklebust nfs_writedata_release(calldata); 11101da177e4SLinus Torvalds } 11111da177e4SLinus Torvalds 1112788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = { 1113def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1) 1114def6ed7eSAndy Adamson .rpc_call_prepare = nfs_write_prepare, 1115def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1116788e7a89STrond Myklebust .rpc_call_done = nfs_writeback_done_full, 1117c9d8f89dSTrond Myklebust .rpc_release = nfs_writeback_release_full, 1118788e7a89STrond Myklebust }; 1119788e7a89STrond Myklebust 1120788e7a89STrond Myklebust 11211da177e4SLinus Torvalds /* 11221da177e4SLinus Torvalds * This function is called when the WRITE call is complete. 11231da177e4SLinus Torvalds */ 1124462d5b32SChuck Lever int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data) 11251da177e4SLinus Torvalds { 11261da177e4SLinus Torvalds struct nfs_writeargs *argp = &data->args; 11271da177e4SLinus Torvalds struct nfs_writeres *resp = &data->res; 1128eedc020eSAndy Adamson struct nfs_server *server = NFS_SERVER(data->inode); 1129788e7a89STrond Myklebust int status; 11301da177e4SLinus Torvalds 1131a3f565b1SChuck Lever dprintk("NFS: %5u nfs_writeback_done (status %d)\n", 11321da177e4SLinus Torvalds task->tk_pid, task->tk_status); 11331da177e4SLinus Torvalds 1134f551e44fSChuck Lever /* 1135f551e44fSChuck Lever * ->write_done will attempt to use post-op attributes to detect 1136f551e44fSChuck Lever * conflicting writes by other clients. A strict interpretation 1137f551e44fSChuck Lever * of close-to-open would allow us to continue caching even if 1138f551e44fSChuck Lever * another writer had changed the file, but some applications 1139f551e44fSChuck Lever * depend on tighter cache coherency when writing. 1140f551e44fSChuck Lever */ 1141788e7a89STrond Myklebust status = NFS_PROTO(data->inode)->write_done(task, data); 1142788e7a89STrond Myklebust if (status != 0) 1143788e7a89STrond Myklebust return status; 114491d5b470SChuck Lever nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count); 114591d5b470SChuck Lever 11461da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 11471da177e4SLinus Torvalds if (resp->verf->committed < argp->stable && task->tk_status >= 0) { 11481da177e4SLinus Torvalds /* We tried a write call, but the server did not 11491da177e4SLinus Torvalds * commit data to stable storage even though we 11501da177e4SLinus Torvalds * requested it. 11511da177e4SLinus Torvalds * Note: There is a known bug in Tru64 < 5.0 in which 11521da177e4SLinus Torvalds * the server reports NFS_DATA_SYNC, but performs 11531da177e4SLinus Torvalds * NFS_FILE_SYNC. We therefore implement this checking 11541da177e4SLinus Torvalds * as a dprintk() in order to avoid filling syslog. 11551da177e4SLinus Torvalds */ 11561da177e4SLinus Torvalds static unsigned long complain; 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 11591da177e4SLinus Torvalds dprintk("NFS: faulty NFS server %s:" 11601da177e4SLinus Torvalds " (committed = %d) != (stable = %d)\n", 1161eedc020eSAndy Adamson server->nfs_client->cl_hostname, 11621da177e4SLinus Torvalds resp->verf->committed, argp->stable); 11631da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds #endif 11671da177e4SLinus Torvalds /* Is this a short write? */ 11681da177e4SLinus Torvalds if (task->tk_status >= 0 && resp->count < argp->count) { 11691da177e4SLinus Torvalds static unsigned long complain; 11701da177e4SLinus Torvalds 117191d5b470SChuck Lever nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE); 117291d5b470SChuck Lever 11731da177e4SLinus Torvalds /* Has the server at least made some progress? */ 11741da177e4SLinus Torvalds if (resp->count != 0) { 11751da177e4SLinus Torvalds /* Was this an NFSv2 write or an NFSv3 stable write? */ 11761da177e4SLinus Torvalds if (resp->verf->committed != NFS_UNSTABLE) { 11771da177e4SLinus Torvalds /* Resend from where the server left off */ 11781da177e4SLinus Torvalds argp->offset += resp->count; 11791da177e4SLinus Torvalds argp->pgbase += resp->count; 11801da177e4SLinus Torvalds argp->count -= resp->count; 11811da177e4SLinus Torvalds } else { 11821da177e4SLinus Torvalds /* Resend as a stable write in order to avoid 11831da177e4SLinus Torvalds * headaches in the case of a server crash. 11841da177e4SLinus Torvalds */ 11851da177e4SLinus Torvalds argp->stable = NFS_FILE_SYNC; 11861da177e4SLinus Torvalds } 11870110ee15STrond Myklebust nfs_restart_rpc(task, server->nfs_client); 1188788e7a89STrond Myklebust return -EAGAIN; 11891da177e4SLinus Torvalds } 11901da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 11911da177e4SLinus Torvalds printk(KERN_WARNING 11921da177e4SLinus Torvalds "NFS: Server wrote zero bytes, expected %u.\n", 11931da177e4SLinus Torvalds argp->count); 11941da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 11951da177e4SLinus Torvalds } 11961da177e4SLinus Torvalds /* Can't do anything about it except throw an error. */ 11971da177e4SLinus Torvalds task->tk_status = -EIO; 11981da177e4SLinus Torvalds } 1199788e7a89STrond Myklebust return 0; 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 12040aa05887SH Hartley Sweeten static void nfs_commitdata_release(void *data) 12051da177e4SLinus Torvalds { 1206383ba719STrond Myklebust struct nfs_write_data *wdata = data; 1207383ba719STrond Myklebust 1208383ba719STrond Myklebust put_nfs_open_context(wdata->args.context); 12091da177e4SLinus Torvalds nfs_commit_free(wdata); 12101da177e4SLinus Torvalds } 12111da177e4SLinus Torvalds 12121da177e4SLinus Torvalds /* 12131da177e4SLinus Torvalds * Set up the argument/result storage required for the RPC call. 12141da177e4SLinus Torvalds */ 1215dbae4c73STrond Myklebust static int nfs_commit_rpcsetup(struct list_head *head, 1216788e7a89STrond Myklebust struct nfs_write_data *data, 1217788e7a89STrond Myklebust int how) 12181da177e4SLinus Torvalds { 121984115e1cSTrond Myklebust struct nfs_page *first = nfs_list_entry(head->next); 122084115e1cSTrond Myklebust struct inode *inode = first->wb_context->path.dentry->d_inode; 12213ff7576dSTrond Myklebust int priority = flush_task_priority(how); 122207737691STrond Myklebust struct rpc_task *task; 1223bdc7f021STrond Myklebust struct rpc_message msg = { 1224bdc7f021STrond Myklebust .rpc_argp = &data->args, 1225bdc7f021STrond Myklebust .rpc_resp = &data->res, 1226bdc7f021STrond Myklebust .rpc_cred = first->wb_context->cred, 1227bdc7f021STrond Myklebust }; 122884115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 122907737691STrond Myklebust .task = &data->task, 123084115e1cSTrond Myklebust .rpc_client = NFS_CLIENT(inode), 1231bdc7f021STrond Myklebust .rpc_message = &msg, 123284115e1cSTrond Myklebust .callback_ops = &nfs_commit_ops, 123384115e1cSTrond Myklebust .callback_data = data, 1234101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 1235*2c61be0aSTrond Myklebust .flags = RPC_TASK_ASYNC, 12363ff7576dSTrond Myklebust .priority = priority, 123784115e1cSTrond Myklebust }; 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds /* Set up the RPC argument and reply structs 12401da177e4SLinus Torvalds * NB: take care not to mess about with data->commit et al. */ 12411da177e4SLinus Torvalds 12421da177e4SLinus Torvalds list_splice_init(head, &data->pages); 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds data->inode = inode; 1245bdc7f021STrond Myklebust data->cred = msg.rpc_cred; 12461da177e4SLinus Torvalds 12471da177e4SLinus Torvalds data->args.fh = NFS_FH(data->inode); 12483da28eb1STrond Myklebust /* Note: we always request a commit of the entire inode */ 12493da28eb1STrond Myklebust data->args.offset = 0; 12503da28eb1STrond Myklebust data->args.count = 0; 1251383ba719STrond Myklebust data->args.context = get_nfs_open_context(first->wb_context); 12523da28eb1STrond Myklebust data->res.count = 0; 12531da177e4SLinus Torvalds data->res.fattr = &data->fattr; 12541da177e4SLinus Torvalds data->res.verf = &data->verf; 12550e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 12561da177e4SLinus Torvalds 1257788e7a89STrond Myklebust /* Set up the initial task struct. */ 1258bdc7f021STrond Myklebust NFS_PROTO(inode)->commit_setup(data, &msg); 12591da177e4SLinus Torvalds 1260a3f565b1SChuck Lever dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); 1261bdc7f021STrond Myklebust 126207737691STrond Myklebust task = rpc_run_task(&task_setup_data); 1263dbae4c73STrond Myklebust if (IS_ERR(task)) 1264dbae4c73STrond Myklebust return PTR_ERR(task); 1265*2c61be0aSTrond Myklebust if (how & FLUSH_SYNC) 1266*2c61be0aSTrond Myklebust rpc_wait_for_completion_task(task); 126707737691STrond Myklebust rpc_put_task(task); 1268dbae4c73STrond Myklebust return 0; 12691da177e4SLinus Torvalds } 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds /* 12721da177e4SLinus Torvalds * Commit dirty pages 12731da177e4SLinus Torvalds */ 12741da177e4SLinus Torvalds static int 127540859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how) 12761da177e4SLinus Torvalds { 12771da177e4SLinus Torvalds struct nfs_write_data *data; 12781da177e4SLinus Torvalds struct nfs_page *req; 12791da177e4SLinus Torvalds 1280c9d8f89dSTrond Myklebust data = nfs_commitdata_alloc(); 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds if (!data) 12831da177e4SLinus Torvalds goto out_bad; 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds /* Set up the argument struct */ 1286dbae4c73STrond Myklebust return nfs_commit_rpcsetup(head, data, how); 12871da177e4SLinus Torvalds out_bad: 12881da177e4SLinus Torvalds while (!list_empty(head)) { 12891da177e4SLinus Torvalds req = nfs_list_entry(head->next); 12901da177e4SLinus Torvalds nfs_list_remove_request(req); 12911da177e4SLinus Torvalds nfs_mark_request_commit(req); 129283715ad5STrond Myklebust dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 1293c9e51e41SPeter Zijlstra dec_bdi_stat(req->wb_page->mapping->backing_dev_info, 1294c9e51e41SPeter Zijlstra BDI_RECLAIMABLE); 12959fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 12961da177e4SLinus Torvalds } 12971da177e4SLinus Torvalds return -ENOMEM; 12981da177e4SLinus Torvalds } 12991da177e4SLinus Torvalds 13001da177e4SLinus Torvalds /* 13011da177e4SLinus Torvalds * COMMIT call returned 13021da177e4SLinus Torvalds */ 1303788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata) 13041da177e4SLinus Torvalds { 1305963d8fe5STrond Myklebust struct nfs_write_data *data = calldata; 13061da177e4SLinus Torvalds 1307a3f565b1SChuck Lever dprintk("NFS: %5u nfs_commit_done (status %d)\n", 13081da177e4SLinus Torvalds task->tk_pid, task->tk_status); 13091da177e4SLinus Torvalds 1310788e7a89STrond Myklebust /* Call the NFS version-specific code */ 1311788e7a89STrond Myklebust if (NFS_PROTO(data->inode)->commit_done(task, data) != 0) 1312788e7a89STrond Myklebust return; 1313c9d8f89dSTrond Myklebust } 1314c9d8f89dSTrond Myklebust 1315c9d8f89dSTrond Myklebust static void nfs_commit_release(void *calldata) 1316c9d8f89dSTrond Myklebust { 1317c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1318c9d8f89dSTrond Myklebust struct nfs_page *req; 1319c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1320788e7a89STrond Myklebust 13211da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 13221da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 13231da177e4SLinus Torvalds nfs_list_remove_request(req); 1324e468bae9STrond Myklebust nfs_clear_request_commit(req); 13251da177e4SLinus Torvalds 132648186c7dSChuck Lever dprintk("NFS: commit (%s/%lld %d@%lld)", 132788be9f99STrond Myklebust req->wb_context->path.dentry->d_inode->i_sb->s_id, 132888be9f99STrond Myklebust (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 13291da177e4SLinus Torvalds req->wb_bytes, 13301da177e4SLinus Torvalds (long long)req_offset(req)); 1331c9d8f89dSTrond Myklebust if (status < 0) { 1332c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 13331da177e4SLinus Torvalds nfs_inode_remove_request(req); 1334c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 13351da177e4SLinus Torvalds goto next; 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds /* Okay, COMMIT succeeded, apparently. Check the verifier 13391da177e4SLinus Torvalds * returned by the server against all stored verfs. */ 13401da177e4SLinus Torvalds if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) { 13411da177e4SLinus Torvalds /* We have a match */ 13421da177e4SLinus Torvalds nfs_inode_remove_request(req); 13431da177e4SLinus Torvalds dprintk(" OK\n"); 13441da177e4SLinus Torvalds goto next; 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds /* We have a mismatch. Write the page again */ 13471da177e4SLinus Torvalds dprintk(" mismatch\n"); 13486d884e8fSFred nfs_mark_request_dirty(req); 13491da177e4SLinus Torvalds next: 13509fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 13511da177e4SLinus Torvalds } 1352c9d8f89dSTrond Myklebust nfs_commitdata_release(calldata); 13531da177e4SLinus Torvalds } 1354788e7a89STrond Myklebust 1355788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = { 135621d9a851SAndy Adamson #if defined(CONFIG_NFS_V4_1) 135721d9a851SAndy Adamson .rpc_call_prepare = nfs_write_prepare, 135821d9a851SAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1359788e7a89STrond Myklebust .rpc_call_done = nfs_commit_done, 1360788e7a89STrond Myklebust .rpc_release = nfs_commit_release, 1361788e7a89STrond Myklebust }; 13621da177e4SLinus Torvalds 13638fc795f7STrond Myklebust static int nfs_commit_inode(struct inode *inode, int how) 13641da177e4SLinus Torvalds { 13651da177e4SLinus Torvalds LIST_HEAD(head); 13667d46a49fSTrond Myklebust int res; 13671da177e4SLinus Torvalds 1368587142f8STrond Myklebust spin_lock(&inode->i_lock); 13693da28eb1STrond Myklebust res = nfs_scan_commit(inode, &head, 0, 0); 1370587142f8STrond Myklebust spin_unlock(&inode->i_lock); 13711da177e4SLinus Torvalds if (res) { 13727d46a49fSTrond Myklebust int error = nfs_commit_list(inode, &head, how); 13731da177e4SLinus Torvalds if (error < 0) 13741da177e4SLinus Torvalds return error; 13753da28eb1STrond Myklebust } 13761da177e4SLinus Torvalds return res; 13771da177e4SLinus Torvalds } 13788fc795f7STrond Myklebust 13798fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 13808fc795f7STrond Myklebust { 1381420e3646STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 1382420e3646STrond Myklebust int flags = FLUSH_SYNC; 1383420e3646STrond Myklebust int ret = 0; 13848fc795f7STrond Myklebust 1385420e3646STrond Myklebust /* Don't commit yet if this is a non-blocking flush and there are 1386420e3646STrond Myklebust * lots of outstanding writes for this mapping. 1387420e3646STrond Myklebust */ 1388420e3646STrond Myklebust if (wbc->sync_mode == WB_SYNC_NONE && 1389420e3646STrond Myklebust nfsi->ncommit <= (nfsi->npages >> 1)) 1390420e3646STrond Myklebust goto out_mark_dirty; 1391420e3646STrond Myklebust 13925bad5abeSTrond Myklebust if (wbc->nonblocking || wbc->for_background) 1393420e3646STrond Myklebust flags = 0; 1394420e3646STrond Myklebust ret = nfs_commit_inode(inode, flags); 1395420e3646STrond Myklebust if (ret >= 0) { 1396420e3646STrond Myklebust if (wbc->sync_mode == WB_SYNC_NONE) { 1397420e3646STrond Myklebust if (ret < wbc->nr_to_write) 1398420e3646STrond Myklebust wbc->nr_to_write -= ret; 1399420e3646STrond Myklebust else 1400420e3646STrond Myklebust wbc->nr_to_write = 0; 1401420e3646STrond Myklebust } 14028fc795f7STrond Myklebust return 0; 1403420e3646STrond Myklebust } 1404420e3646STrond Myklebust out_mark_dirty: 14058fc795f7STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 14068fc795f7STrond Myklebust return ret; 14078fc795f7STrond Myklebust } 1408c63c7b05STrond Myklebust #else 14097f2f12d9STrond Myklebust static int nfs_commit_inode(struct inode *inode, int how) 1410c63c7b05STrond Myklebust { 1411c63c7b05STrond Myklebust return 0; 1412c63c7b05STrond Myklebust } 14138fc795f7STrond Myklebust 14148fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 14158fc795f7STrond Myklebust { 14168fc795f7STrond Myklebust return 0; 14178fc795f7STrond Myklebust } 14181da177e4SLinus Torvalds #endif 14191da177e4SLinus Torvalds 14208fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc) 14218fc795f7STrond Myklebust { 14228fc795f7STrond Myklebust return nfs_commit_unstable_pages(inode, wbc); 14238fc795f7STrond Myklebust } 14248fc795f7STrond Myklebust 1425acdc53b2STrond Myklebust /* 1426acdc53b2STrond Myklebust * flush the inode to disk. 1427acdc53b2STrond Myklebust */ 1428acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode) 142934901f70STrond Myklebust { 143034901f70STrond Myklebust struct writeback_control wbc = { 143172cb77f4STrond Myklebust .sync_mode = WB_SYNC_ALL, 143234901f70STrond Myklebust .nr_to_write = LONG_MAX, 1433d7fb1207STrond Myklebust .range_start = 0, 1434d7fb1207STrond Myklebust .range_end = LLONG_MAX, 143534901f70STrond Myklebust }; 143634901f70STrond Myklebust 1437acdc53b2STrond Myklebust return sync_inode(inode, &wbc); 14381c75950bSTrond Myklebust } 14391c75950bSTrond Myklebust 14401b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page) 14411b3b4a1aSTrond Myklebust { 14421b3b4a1aSTrond Myklebust struct nfs_page *req; 14431b3b4a1aSTrond Myklebust int ret = 0; 14441b3b4a1aSTrond Myklebust 14451b3b4a1aSTrond Myklebust BUG_ON(!PageLocked(page)); 14461b3b4a1aSTrond Myklebust for (;;) { 14471b3b4a1aSTrond Myklebust req = nfs_page_find_request(page); 14481b3b4a1aSTrond Myklebust if (req == NULL) 14491b3b4a1aSTrond Myklebust break; 14501b3b4a1aSTrond Myklebust if (nfs_lock_request_dontget(req)) { 14511b3b4a1aSTrond Myklebust nfs_inode_remove_request(req); 14521b3b4a1aSTrond Myklebust /* 14531b3b4a1aSTrond Myklebust * In case nfs_inode_remove_request has marked the 14541b3b4a1aSTrond Myklebust * page as being dirty 14551b3b4a1aSTrond Myklebust */ 14561b3b4a1aSTrond Myklebust cancel_dirty_page(page, PAGE_CACHE_SIZE); 14571b3b4a1aSTrond Myklebust nfs_unlock_request(req); 14581b3b4a1aSTrond Myklebust break; 14591b3b4a1aSTrond Myklebust } 14601b3b4a1aSTrond Myklebust ret = nfs_wait_on_request(req); 1461c9edda71STrond Myklebust nfs_release_request(req); 14621b3b4a1aSTrond Myklebust if (ret < 0) 1463c988950eSTrond Myklebust break; 14641b3b4a1aSTrond Myklebust } 14651b3b4a1aSTrond Myklebust return ret; 14661b3b4a1aSTrond Myklebust } 14671b3b4a1aSTrond Myklebust 14681c75950bSTrond Myklebust /* 14691c75950bSTrond Myklebust * Write back all requests on one page - we do this before reading it. 14701c75950bSTrond Myklebust */ 14711c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page) 14721c75950bSTrond Myklebust { 14737f2f12d9STrond Myklebust loff_t range_start = page_offset(page); 14747f2f12d9STrond Myklebust loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); 14757f2f12d9STrond Myklebust struct writeback_control wbc = { 14767f2f12d9STrond Myklebust .sync_mode = WB_SYNC_ALL, 14777f2f12d9STrond Myklebust .nr_to_write = 0, 14787f2f12d9STrond Myklebust .range_start = range_start, 14797f2f12d9STrond Myklebust .range_end = range_end, 14807f2f12d9STrond Myklebust }; 14817f2f12d9STrond Myklebust struct nfs_page *req; 14827f2f12d9STrond Myklebust int need_commit; 14837f2f12d9STrond Myklebust int ret; 14847f2f12d9STrond Myklebust 14857f2f12d9STrond Myklebust while(PagePrivate(page)) { 14867f2f12d9STrond Myklebust if (clear_page_dirty_for_io(page)) { 14877f2f12d9STrond Myklebust ret = nfs_writepage_locked(page, &wbc); 14887f2f12d9STrond Myklebust if (ret < 0) 14897f2f12d9STrond Myklebust goto out_error; 14907f2f12d9STrond Myklebust } 14917f2f12d9STrond Myklebust req = nfs_find_and_lock_request(page); 14927f2f12d9STrond Myklebust if (!req) 14937f2f12d9STrond Myklebust break; 14947f2f12d9STrond Myklebust if (IS_ERR(req)) { 14957f2f12d9STrond Myklebust ret = PTR_ERR(req); 14967f2f12d9STrond Myklebust goto out_error; 14977f2f12d9STrond Myklebust } 14987f2f12d9STrond Myklebust need_commit = test_bit(PG_CLEAN, &req->wb_flags); 14997f2f12d9STrond Myklebust nfs_clear_page_tag_locked(req); 15007f2f12d9STrond Myklebust if (need_commit) { 15017f2f12d9STrond Myklebust ret = nfs_commit_inode(inode, FLUSH_SYNC); 15027f2f12d9STrond Myklebust if (ret < 0) 15037f2f12d9STrond Myklebust goto out_error; 15047f2f12d9STrond Myklebust } 15057f2f12d9STrond Myklebust } 15067f2f12d9STrond Myklebust return 0; 15077f2f12d9STrond Myklebust out_error: 15087f2f12d9STrond Myklebust return ret; 15091c75950bSTrond Myklebust } 15101c75950bSTrond Myklebust 1511074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION 1512074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage, 1513074cc1deSTrond Myklebust struct page *page) 1514074cc1deSTrond Myklebust { 1515074cc1deSTrond Myklebust struct nfs_page *req; 1516074cc1deSTrond Myklebust int ret; 1517074cc1deSTrond Myklebust 1518074cc1deSTrond Myklebust nfs_fscache_release_page(page, GFP_KERNEL); 1519074cc1deSTrond Myklebust 1520074cc1deSTrond Myklebust req = nfs_find_and_lock_request(page); 1521074cc1deSTrond Myklebust ret = PTR_ERR(req); 1522074cc1deSTrond Myklebust if (IS_ERR(req)) 1523074cc1deSTrond Myklebust goto out; 1524074cc1deSTrond Myklebust 1525074cc1deSTrond Myklebust ret = migrate_page(mapping, newpage, page); 1526074cc1deSTrond Myklebust if (!req) 1527074cc1deSTrond Myklebust goto out; 1528074cc1deSTrond Myklebust if (ret) 1529074cc1deSTrond Myklebust goto out_unlock; 1530074cc1deSTrond Myklebust page_cache_get(newpage); 1531190f38e5STrond Myklebust spin_lock(&mapping->host->i_lock); 1532074cc1deSTrond Myklebust req->wb_page = newpage; 1533074cc1deSTrond Myklebust SetPagePrivate(newpage); 1534190f38e5STrond Myklebust set_page_private(newpage, (unsigned long)req); 1535074cc1deSTrond Myklebust ClearPagePrivate(page); 1536074cc1deSTrond Myklebust set_page_private(page, 0); 1537190f38e5STrond Myklebust spin_unlock(&mapping->host->i_lock); 1538074cc1deSTrond Myklebust page_cache_release(page); 1539074cc1deSTrond Myklebust out_unlock: 1540074cc1deSTrond Myklebust nfs_clear_page_tag_locked(req); 1541074cc1deSTrond Myklebust out: 1542074cc1deSTrond Myklebust return ret; 1543074cc1deSTrond Myklebust } 1544074cc1deSTrond Myklebust #endif 1545074cc1deSTrond Myklebust 1546f7b422b1SDavid Howells int __init nfs_init_writepagecache(void) 15471da177e4SLinus Torvalds { 15481da177e4SLinus Torvalds nfs_wdata_cachep = kmem_cache_create("nfs_write_data", 15491da177e4SLinus Torvalds sizeof(struct nfs_write_data), 15501da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 155120c2df83SPaul Mundt NULL); 15521da177e4SLinus Torvalds if (nfs_wdata_cachep == NULL) 15531da177e4SLinus Torvalds return -ENOMEM; 15541da177e4SLinus Torvalds 155593d2341cSMatthew Dobson nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, 15561da177e4SLinus Torvalds nfs_wdata_cachep); 15571da177e4SLinus Torvalds if (nfs_wdata_mempool == NULL) 15581da177e4SLinus Torvalds return -ENOMEM; 15591da177e4SLinus Torvalds 156093d2341cSMatthew Dobson nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, 15611da177e4SLinus Torvalds nfs_wdata_cachep); 15621da177e4SLinus Torvalds if (nfs_commit_mempool == NULL) 15631da177e4SLinus Torvalds return -ENOMEM; 15641da177e4SLinus Torvalds 156589a09141SPeter Zijlstra /* 156689a09141SPeter Zijlstra * NFS congestion size, scale with available memory. 156789a09141SPeter Zijlstra * 156889a09141SPeter Zijlstra * 64MB: 8192k 156989a09141SPeter Zijlstra * 128MB: 11585k 157089a09141SPeter Zijlstra * 256MB: 16384k 157189a09141SPeter Zijlstra * 512MB: 23170k 157289a09141SPeter Zijlstra * 1GB: 32768k 157389a09141SPeter Zijlstra * 2GB: 46340k 157489a09141SPeter Zijlstra * 4GB: 65536k 157589a09141SPeter Zijlstra * 8GB: 92681k 157689a09141SPeter Zijlstra * 16GB: 131072k 157789a09141SPeter Zijlstra * 157889a09141SPeter Zijlstra * This allows larger machines to have larger/more transfers. 157989a09141SPeter Zijlstra * Limit the default to 256M 158089a09141SPeter Zijlstra */ 158189a09141SPeter Zijlstra nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); 158289a09141SPeter Zijlstra if (nfs_congestion_kb > 256*1024) 158389a09141SPeter Zijlstra nfs_congestion_kb = 256*1024; 158489a09141SPeter Zijlstra 15851da177e4SLinus Torvalds return 0; 15861da177e4SLinus Torvalds } 15871da177e4SLinus Torvalds 1588266bee88SDavid Brownell void nfs_destroy_writepagecache(void) 15891da177e4SLinus Torvalds { 15901da177e4SLinus Torvalds mempool_destroy(nfs_commit_mempool); 15911da177e4SLinus Torvalds mempool_destroy(nfs_wdata_mempool); 15921a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_wdata_cachep); 15931da177e4SLinus Torvalds } 15941da177e4SLinus Torvalds 1595