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> 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 181da177e4SLinus Torvalds #include <linux/nfs_fs.h> 191da177e4SLinus Torvalds #include <linux/nfs_mount.h> 201da177e4SLinus Torvalds #include <linux/nfs_page.h> 213fcfab16SAndrew Morton #include <linux/backing-dev.h> 223fcfab16SAndrew Morton 231da177e4SLinus Torvalds #include <asm/uaccess.h> 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #include "delegation.h" 2649a70f27STrond Myklebust #include "internal.h" 2791d5b470SChuck Lever #include "iostat.h" 28def6ed7eSAndy Adamson #include "nfs4_fs.h" 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_PAGECACHE 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds #define MIN_POOL_WRITE (32) 331da177e4SLinus Torvalds #define MIN_POOL_COMMIT (4) 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds /* 361da177e4SLinus Torvalds * Local function declarations 371da177e4SLinus Torvalds */ 38c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc, 39c63c7b05STrond Myklebust struct inode *inode, int ioflags); 40f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req); 41788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops; 42788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops; 43788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops; 441da177e4SLinus Torvalds 45e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep; 463feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool; 471da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool; 481da177e4SLinus Torvalds 49c9d8f89dSTrond Myklebust struct nfs_write_data *nfs_commitdata_alloc(void) 501da177e4SLinus Torvalds { 51e6b4f8daSChristoph Lameter struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS); 5240859d7eSChuck Lever 531da177e4SLinus Torvalds if (p) { 541da177e4SLinus Torvalds memset(p, 0, sizeof(*p)); 551da177e4SLinus Torvalds INIT_LIST_HEAD(&p->pages); 565f7dbd5cSAndy Adamson p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE; 571da177e4SLinus Torvalds } 581da177e4SLinus Torvalds return p; 591da177e4SLinus Torvalds } 601da177e4SLinus Torvalds 615e4424afSTrond Myklebust void nfs_commit_free(struct nfs_write_data *p) 621da177e4SLinus Torvalds { 6340859d7eSChuck Lever if (p && (p->pagevec != &p->page_array[0])) 6440859d7eSChuck Lever kfree(p->pagevec); 651da177e4SLinus Torvalds mempool_free(p, nfs_commit_mempool); 661da177e4SLinus Torvalds } 671da177e4SLinus Torvalds 688d5658c9STrond Myklebust struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount) 693feb2d49STrond Myklebust { 70e6b4f8daSChristoph Lameter struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS); 713feb2d49STrond Myklebust 723feb2d49STrond Myklebust if (p) { 733feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 743feb2d49STrond Myklebust INIT_LIST_HEAD(&p->pages); 75e9f7bee1STrond Myklebust p->npages = pagecount; 765f7dbd5cSAndy Adamson p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE; 770d0b5cb3SChuck Lever if (pagecount <= ARRAY_SIZE(p->page_array)) 780d0b5cb3SChuck Lever p->pagevec = p->page_array; 793feb2d49STrond Myklebust else { 800d0b5cb3SChuck Lever p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS); 810d0b5cb3SChuck Lever if (!p->pagevec) { 823feb2d49STrond Myklebust mempool_free(p, nfs_wdata_mempool); 833feb2d49STrond Myklebust p = NULL; 843feb2d49STrond Myklebust } 853feb2d49STrond Myklebust } 863feb2d49STrond Myklebust } 873feb2d49STrond Myklebust return p; 883feb2d49STrond Myklebust } 893feb2d49STrond Myklebust 905e4424afSTrond Myklebust static void nfs_writedata_free(struct nfs_write_data *p) 913feb2d49STrond Myklebust { 923feb2d49STrond Myklebust if (p && (p->pagevec != &p->page_array[0])) 933feb2d49STrond Myklebust kfree(p->pagevec); 943feb2d49STrond Myklebust mempool_free(p, nfs_wdata_mempool); 953feb2d49STrond Myklebust } 963feb2d49STrond Myklebust 97383ba719STrond Myklebust void nfs_writedata_release(void *data) 981da177e4SLinus Torvalds { 99383ba719STrond Myklebust struct nfs_write_data *wdata = data; 100383ba719STrond Myklebust 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; 1811da177e4SLinus Torvalds if (wbc->for_kupdate) 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 204277866a0SPeter Zijlstra if (atomic_long_inc_return(&nfss->writeback) > 20589a09141SPeter Zijlstra NFS_CONGESTION_ON_THRESH) 20689a09141SPeter Zijlstra set_bdi_congested(&nfss->backing_dev_info, WRITE); 20789a09141SPeter Zijlstra } 2085a6d41b3STrond Myklebust return ret; 20989a09141SPeter Zijlstra } 21089a09141SPeter Zijlstra 21189a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page) 21289a09141SPeter Zijlstra { 21389a09141SPeter Zijlstra struct inode *inode = page->mapping->host; 21489a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 21589a09141SPeter Zijlstra 21689a09141SPeter Zijlstra end_page_writeback(page); 217c4dc4beeSPeter Zijlstra if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) 21889a09141SPeter Zijlstra clear_bdi_congested(&nfss->backing_dev_info, WRITE); 21989a09141SPeter Zijlstra } 22089a09141SPeter Zijlstra 22189a09141SPeter Zijlstra /* 222e261f51fSTrond Myklebust * Find an associated nfs write request, and prepare to flush it out 2239cccef95STrond Myklebust * May return an error if the user signalled nfs_wait_on_request(). 224e261f51fSTrond Myklebust */ 225c63c7b05STrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, 226c63c7b05STrond Myklebust struct page *page) 227e261f51fSTrond Myklebust { 228587142f8STrond Myklebust struct inode *inode = page->mapping->host; 229e261f51fSTrond Myklebust struct nfs_page *req; 230e261f51fSTrond Myklebust int ret; 231e261f51fSTrond Myklebust 232587142f8STrond Myklebust spin_lock(&inode->i_lock); 233e261f51fSTrond Myklebust for(;;) { 234e261f51fSTrond Myklebust req = nfs_page_find_request_locked(page); 235e261f51fSTrond Myklebust if (req == NULL) { 236587142f8STrond Myklebust spin_unlock(&inode->i_lock); 2379cccef95STrond Myklebust return 0; 238e261f51fSTrond Myklebust } 239acee478aSTrond Myklebust if (nfs_set_page_tag_locked(req)) 240e261f51fSTrond Myklebust break; 241e261f51fSTrond Myklebust /* Note: If we hold the page lock, as is the case in nfs_writepage, 242acee478aSTrond Myklebust * then the call to nfs_set_page_tag_locked() will always 243e261f51fSTrond Myklebust * succeed provided that someone hasn't already marked the 244e261f51fSTrond Myklebust * request as dirty (in which case we don't care). 245e261f51fSTrond Myklebust */ 246587142f8STrond Myklebust spin_unlock(&inode->i_lock); 247e261f51fSTrond Myklebust ret = nfs_wait_on_request(req); 248e261f51fSTrond Myklebust nfs_release_request(req); 249e261f51fSTrond Myklebust if (ret != 0) 250e261f51fSTrond Myklebust return ret; 251587142f8STrond Myklebust spin_lock(&inode->i_lock); 252e261f51fSTrond Myklebust } 253e468bae9STrond Myklebust if (test_bit(PG_CLEAN, &req->wb_flags)) { 254587142f8STrond Myklebust spin_unlock(&inode->i_lock); 255e468bae9STrond Myklebust BUG(); 256612c9384STrond Myklebust } 257c63c7b05STrond Myklebust if (nfs_set_page_writeback(page) != 0) { 258587142f8STrond Myklebust spin_unlock(&inode->i_lock); 259c63c7b05STrond Myklebust BUG(); 260c63c7b05STrond Myklebust } 261587142f8STrond Myklebust spin_unlock(&inode->i_lock); 262f8512ad0SFred Isaman if (!nfs_pageio_add_request(pgio, req)) { 263f8512ad0SFred Isaman nfs_redirty_request(req); 264f8512ad0SFred Isaman return pgio->pg_error; 265f8512ad0SFred Isaman } 2669cccef95STrond Myklebust return 0; 267e261f51fSTrond Myklebust } 268e261f51fSTrond Myklebust 269f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio) 270f758c885STrond Myklebust { 271f758c885STrond Myklebust struct inode *inode = page->mapping->host; 272f758c885STrond Myklebust 273f758c885STrond Myklebust nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); 274f758c885STrond Myklebust nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); 275f758c885STrond Myklebust 276f758c885STrond Myklebust nfs_pageio_cond_complete(pgio, page->index); 277f758c885STrond Myklebust return nfs_page_async_flush(pgio, page); 278f758c885STrond Myklebust } 279f758c885STrond Myklebust 280e261f51fSTrond Myklebust /* 2811da177e4SLinus Torvalds * Write an mmapped page to the server. 2821da177e4SLinus Torvalds */ 2834d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc) 2841da177e4SLinus Torvalds { 285f758c885STrond Myklebust struct nfs_pageio_descriptor pgio; 286e261f51fSTrond Myklebust int err; 2871da177e4SLinus Torvalds 288f758c885STrond Myklebust nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc)); 289f758c885STrond Myklebust err = nfs_do_writepage(page, wbc, &pgio); 290f758c885STrond Myklebust nfs_pageio_complete(&pgio); 291f758c885STrond Myklebust if (err < 0) 2924d770ccfSTrond Myklebust return err; 293f758c885STrond Myklebust if (pgio.pg_error < 0) 294f758c885STrond Myklebust return pgio.pg_error; 295f758c885STrond Myklebust return 0; 2964d770ccfSTrond Myklebust } 2974d770ccfSTrond Myklebust 2984d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc) 2994d770ccfSTrond Myklebust { 300f758c885STrond Myklebust int ret; 3014d770ccfSTrond Myklebust 302f758c885STrond Myklebust ret = nfs_writepage_locked(page, wbc); 3031da177e4SLinus Torvalds unlock_page(page); 304f758c885STrond Myklebust return ret; 305f758c885STrond Myklebust } 306f758c885STrond Myklebust 307f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) 308f758c885STrond Myklebust { 309f758c885STrond Myklebust int ret; 310f758c885STrond Myklebust 311f758c885STrond Myklebust ret = nfs_do_writepage(page, wbc, data); 312f758c885STrond Myklebust unlock_page(page); 313f758c885STrond Myklebust return ret; 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) 3171da177e4SLinus Torvalds { 3181da177e4SLinus Torvalds struct inode *inode = mapping->host; 31972cb77f4STrond Myklebust unsigned long *bitlock = &NFS_I(inode)->flags; 320c63c7b05STrond Myklebust struct nfs_pageio_descriptor pgio; 3211da177e4SLinus Torvalds int err; 3221da177e4SLinus Torvalds 32372cb77f4STrond Myklebust /* Stop dirtying of new pages while we sync */ 32472cb77f4STrond Myklebust err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING, 32572cb77f4STrond Myklebust nfs_wait_bit_killable, TASK_KILLABLE); 32672cb77f4STrond Myklebust if (err) 32772cb77f4STrond Myklebust goto out_err; 32872cb77f4STrond Myklebust 32991d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); 33091d5b470SChuck Lever 331c63c7b05STrond Myklebust nfs_pageio_init_write(&pgio, inode, wb_priority(wbc)); 332f758c885STrond Myklebust err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); 333c63c7b05STrond Myklebust nfs_pageio_complete(&pgio); 33472cb77f4STrond Myklebust 33572cb77f4STrond Myklebust clear_bit_unlock(NFS_INO_FLUSHING, bitlock); 33672cb77f4STrond Myklebust smp_mb__after_clear_bit(); 33772cb77f4STrond Myklebust wake_up_bit(bitlock, NFS_INO_FLUSHING); 33872cb77f4STrond Myklebust 339f758c885STrond Myklebust if (err < 0) 34072cb77f4STrond Myklebust goto out_err; 34172cb77f4STrond Myklebust err = pgio.pg_error; 34272cb77f4STrond Myklebust if (err < 0) 34372cb77f4STrond Myklebust goto out_err; 344c63c7b05STrond Myklebust return 0; 34572cb77f4STrond Myklebust out_err: 34672cb77f4STrond Myklebust return err; 3471da177e4SLinus Torvalds } 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds /* 3501da177e4SLinus Torvalds * Insert a write request into an inode 3511da177e4SLinus Torvalds */ 352e7d39069STrond Myklebust static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req) 3531da177e4SLinus Torvalds { 3541da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 3551da177e4SLinus Torvalds int error; 3561da177e4SLinus Torvalds 357e7d39069STrond Myklebust error = radix_tree_preload(GFP_NOFS); 358e7d39069STrond Myklebust if (error != 0) 359e7d39069STrond Myklebust goto out; 360e7d39069STrond Myklebust 361e7d39069STrond Myklebust /* Lock the request! */ 362e7d39069STrond Myklebust nfs_lock_request_dontget(req); 363e7d39069STrond Myklebust 364e7d39069STrond Myklebust spin_lock(&inode->i_lock); 3651da177e4SLinus Torvalds error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req); 36627852596SNick Piggin BUG_ON(error); 3671da177e4SLinus Torvalds if (!nfsi->npages) { 3681da177e4SLinus Torvalds igrab(inode); 3691da177e4SLinus Torvalds if (nfs_have_delegation(inode, FMODE_WRITE)) 3701da177e4SLinus Torvalds nfsi->change_attr++; 3711da177e4SLinus Torvalds } 372deb7d638STrond Myklebust SetPagePrivate(req->wb_page); 373277459d2STrond Myklebust set_page_private(req->wb_page, (unsigned long)req); 3741da177e4SLinus Torvalds nfsi->npages++; 375c03b4024STrond Myklebust kref_get(&req->wb_kref); 37627852596SNick Piggin radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, 37727852596SNick Piggin NFS_PAGE_TAG_LOCKED); 378e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 379e7d39069STrond Myklebust radix_tree_preload_end(); 380e7d39069STrond Myklebust out: 381e7d39069STrond Myklebust return error; 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds /* 38589a09141SPeter Zijlstra * Remove a write request from an inode 3861da177e4SLinus Torvalds */ 3871da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req) 3881da177e4SLinus Torvalds { 38988be9f99STrond Myklebust struct inode *inode = req->wb_context->path.dentry->d_inode; 3901da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds BUG_ON (!NFS_WBACK_BUSY(req)); 3931da177e4SLinus Torvalds 394587142f8STrond Myklebust spin_lock(&inode->i_lock); 395277459d2STrond Myklebust set_page_private(req->wb_page, 0); 396deb7d638STrond Myklebust ClearPagePrivate(req->wb_page); 3971da177e4SLinus Torvalds radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index); 3981da177e4SLinus Torvalds nfsi->npages--; 3991da177e4SLinus Torvalds if (!nfsi->npages) { 400587142f8STrond Myklebust spin_unlock(&inode->i_lock); 4011da177e4SLinus Torvalds iput(inode); 4021da177e4SLinus Torvalds } else 403587142f8STrond Myklebust spin_unlock(&inode->i_lock); 4041da177e4SLinus Torvalds nfs_clear_request(req); 4051da177e4SLinus Torvalds nfs_release_request(req); 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds 40861822ab5STrond Myklebust static void 4096d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req) 41061822ab5STrond Myklebust { 41161822ab5STrond Myklebust __set_page_dirty_nobuffers(req->wb_page); 41261822ab5STrond Myklebust } 41361822ab5STrond Myklebust 4141da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 4151da177e4SLinus Torvalds /* 4161da177e4SLinus Torvalds * Add a request to the inode's commit list. 4171da177e4SLinus Torvalds */ 4181da177e4SLinus Torvalds static void 4191da177e4SLinus Torvalds nfs_mark_request_commit(struct nfs_page *req) 4201da177e4SLinus Torvalds { 42188be9f99STrond Myklebust struct inode *inode = req->wb_context->path.dentry->d_inode; 4221da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 4231da177e4SLinus Torvalds 424587142f8STrond Myklebust spin_lock(&inode->i_lock); 425e468bae9STrond Myklebust set_bit(PG_CLEAN, &(req)->wb_flags); 4265c369683STrond Myklebust radix_tree_tag_set(&nfsi->nfs_page_tree, 4275c369683STrond Myklebust req->wb_index, 4285c369683STrond Myklebust NFS_PAGE_TAG_COMMIT); 429587142f8STrond Myklebust spin_unlock(&inode->i_lock); 430fd39fc85SChristoph Lameter inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 431c9e51e41SPeter Zijlstra inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE); 432a1803044STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 4331da177e4SLinus Torvalds } 4348e821cadSTrond Myklebust 435e468bae9STrond Myklebust static int 436e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req) 437e468bae9STrond Myklebust { 438e468bae9STrond Myklebust struct page *page = req->wb_page; 439e468bae9STrond Myklebust 440e468bae9STrond Myklebust if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) { 441e468bae9STrond Myklebust dec_zone_page_state(page, NR_UNSTABLE_NFS); 442e468bae9STrond Myklebust dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE); 443e468bae9STrond Myklebust return 1; 444e468bae9STrond Myklebust } 445e468bae9STrond Myklebust return 0; 446e468bae9STrond Myklebust } 447e468bae9STrond Myklebust 4488e821cadSTrond Myklebust static inline 4498e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 4508e821cadSTrond Myklebust { 4518e821cadSTrond Myklebust return data->verf.committed != NFS_FILE_SYNC; 4528e821cadSTrond Myklebust } 4538e821cadSTrond Myklebust 4548e821cadSTrond Myklebust static inline 4558e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req) 4568e821cadSTrond Myklebust { 457e468bae9STrond Myklebust if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) { 4588e821cadSTrond Myklebust nfs_mark_request_commit(req); 4598e821cadSTrond Myklebust return 1; 4608e821cadSTrond Myklebust } 4618e821cadSTrond Myklebust if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) { 4626d884e8fSFred nfs_mark_request_dirty(req); 4638e821cadSTrond Myklebust return 1; 4648e821cadSTrond Myklebust } 4658e821cadSTrond Myklebust return 0; 4668e821cadSTrond Myklebust } 4678e821cadSTrond Myklebust #else 4688e821cadSTrond Myklebust static inline void 4698e821cadSTrond Myklebust nfs_mark_request_commit(struct nfs_page *req) 4708e821cadSTrond Myklebust { 4718e821cadSTrond Myklebust } 4728e821cadSTrond Myklebust 473e468bae9STrond Myklebust static inline int 474e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req) 475e468bae9STrond Myklebust { 476e468bae9STrond Myklebust return 0; 477e468bae9STrond Myklebust } 478e468bae9STrond Myklebust 4798e821cadSTrond Myklebust static inline 4808e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 4818e821cadSTrond Myklebust { 4828e821cadSTrond Myklebust return 0; 4838e821cadSTrond Myklebust } 4848e821cadSTrond Myklebust 4858e821cadSTrond Myklebust static inline 4868e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req) 4878e821cadSTrond Myklebust { 4888e821cadSTrond Myklebust return 0; 4898e821cadSTrond Myklebust } 4901da177e4SLinus Torvalds #endif 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds /* 4931da177e4SLinus Torvalds * Wait for a request to complete. 4941da177e4SLinus Torvalds * 495150030b7SMatthew Wilcox * Interruptible by fatal signals only. 4961da177e4SLinus Torvalds */ 497ca52fec1STrond Myklebust static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages) 4981da177e4SLinus Torvalds { 4991da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 5001da177e4SLinus Torvalds struct nfs_page *req; 501ca52fec1STrond Myklebust pgoff_t idx_end, next; 5021da177e4SLinus Torvalds unsigned int res = 0; 5031da177e4SLinus Torvalds int error; 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds if (npages == 0) 5061da177e4SLinus Torvalds idx_end = ~0; 5071da177e4SLinus Torvalds else 5081da177e4SLinus Torvalds idx_end = idx_start + npages - 1; 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds next = idx_start; 5119fd367f0STrond Myklebust while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) { 5121da177e4SLinus Torvalds if (req->wb_index > idx_end) 5131da177e4SLinus Torvalds break; 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds next = req->wb_index + 1; 516c6a556b8STrond Myklebust BUG_ON(!NFS_WBACK_BUSY(req)); 5171da177e4SLinus Torvalds 518c03b4024STrond Myklebust kref_get(&req->wb_kref); 519587142f8STrond Myklebust spin_unlock(&inode->i_lock); 5201da177e4SLinus Torvalds error = nfs_wait_on_request(req); 5211da177e4SLinus Torvalds nfs_release_request(req); 522587142f8STrond Myklebust spin_lock(&inode->i_lock); 5231da177e4SLinus Torvalds if (error < 0) 5241da177e4SLinus Torvalds return error; 5251da177e4SLinus Torvalds res++; 5261da177e4SLinus Torvalds } 5271da177e4SLinus Torvalds return res; 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 53083715ad5STrond Myklebust static void nfs_cancel_commit_list(struct list_head *head) 53183715ad5STrond Myklebust { 53283715ad5STrond Myklebust struct nfs_page *req; 53383715ad5STrond Myklebust 53483715ad5STrond Myklebust while(!list_empty(head)) { 53583715ad5STrond Myklebust req = nfs_list_entry(head->next); 53683715ad5STrond Myklebust nfs_list_remove_request(req); 537e468bae9STrond Myklebust nfs_clear_request_commit(req); 53883715ad5STrond Myklebust nfs_inode_remove_request(req); 539b6dff26aSTrond Myklebust nfs_unlock_request(req); 54083715ad5STrond Myklebust } 54183715ad5STrond Myklebust } 54283715ad5STrond Myklebust 54347c62564STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 544fb8a1f11STrond Myklebust static int 545fb8a1f11STrond Myklebust nfs_need_commit(struct nfs_inode *nfsi) 546fb8a1f11STrond Myklebust { 547fb8a1f11STrond Myklebust return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT); 548fb8a1f11STrond Myklebust } 549fb8a1f11STrond Myklebust 5501da177e4SLinus Torvalds /* 5511da177e4SLinus Torvalds * nfs_scan_commit - Scan an inode for commit requests 5521da177e4SLinus Torvalds * @inode: NFS inode to scan 5531da177e4SLinus Torvalds * @dst: destination list 5541da177e4SLinus Torvalds * @idx_start: lower bound of page->index to scan. 5551da177e4SLinus Torvalds * @npages: idx_start + npages sets the upper bound to scan. 5561da177e4SLinus Torvalds * 5571da177e4SLinus Torvalds * Moves requests from the inode's 'commit' request list. 5581da177e4SLinus Torvalds * The requests are *not* checked to ensure that they form a contiguous set. 5591da177e4SLinus Torvalds */ 5601da177e4SLinus Torvalds static int 561ca52fec1STrond Myklebust nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages) 5621da177e4SLinus Torvalds { 5631da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 5643da28eb1STrond Myklebust 565fb8a1f11STrond Myklebust if (!nfs_need_commit(nfsi)) 566fb8a1f11STrond Myklebust return 0; 567fb8a1f11STrond Myklebust 568fb8a1f11STrond Myklebust return nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT); 5691da177e4SLinus Torvalds } 570c42de9ddSTrond Myklebust #else 571fb8a1f11STrond Myklebust static inline int nfs_need_commit(struct nfs_inode *nfsi) 572fb8a1f11STrond Myklebust { 573fb8a1f11STrond Myklebust return 0; 574fb8a1f11STrond Myklebust } 575fb8a1f11STrond Myklebust 576ca52fec1STrond Myklebust static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages) 577c42de9ddSTrond Myklebust { 578c42de9ddSTrond Myklebust return 0; 579c42de9ddSTrond Myklebust } 5801da177e4SLinus Torvalds #endif 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds /* 583e7d39069STrond Myklebust * Search for an existing write request, and attempt to update 584e7d39069STrond Myklebust * it to reflect a new dirty region on a given page. 5851da177e4SLinus Torvalds * 586e7d39069STrond Myklebust * If the attempt fails, then the existing request is flushed out 587e7d39069STrond Myklebust * to disk. 5881da177e4SLinus Torvalds */ 589e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode, 590e7d39069STrond Myklebust struct page *page, 591e7d39069STrond Myklebust unsigned int offset, 592e7d39069STrond Myklebust unsigned int bytes) 5931da177e4SLinus Torvalds { 594e7d39069STrond Myklebust struct nfs_page *req; 595e7d39069STrond Myklebust unsigned int rqend; 596e7d39069STrond Myklebust unsigned int end; 5971da177e4SLinus Torvalds int error; 598277459d2STrond Myklebust 599e7d39069STrond Myklebust if (!PagePrivate(page)) 600e7d39069STrond Myklebust return NULL; 601e7d39069STrond Myklebust 602e7d39069STrond Myklebust end = offset + bytes; 603e7d39069STrond Myklebust spin_lock(&inode->i_lock); 604e7d39069STrond Myklebust 605e7d39069STrond Myklebust for (;;) { 606e7d39069STrond Myklebust req = nfs_page_find_request_locked(page); 607e7d39069STrond Myklebust if (req == NULL) 608e7d39069STrond Myklebust goto out_unlock; 609e7d39069STrond Myklebust 610e7d39069STrond Myklebust rqend = req->wb_offset + req->wb_bytes; 611e7d39069STrond Myklebust /* 612e7d39069STrond Myklebust * Tell the caller to flush out the request if 613e7d39069STrond Myklebust * the offsets are non-contiguous. 614e7d39069STrond Myklebust * Note: nfs_flush_incompatible() will already 615e7d39069STrond Myklebust * have flushed out requests having wrong owners. 616e7d39069STrond Myklebust */ 617e468bae9STrond Myklebust if (offset > rqend 618e7d39069STrond Myklebust || end < req->wb_offset) 619e7d39069STrond Myklebust goto out_flushme; 620e7d39069STrond Myklebust 621e7d39069STrond Myklebust if (nfs_set_page_tag_locked(req)) 622e7d39069STrond Myklebust break; 623e7d39069STrond Myklebust 624e7d39069STrond Myklebust /* The request is locked, so wait and then retry */ 625587142f8STrond Myklebust spin_unlock(&inode->i_lock); 6261da177e4SLinus Torvalds error = nfs_wait_on_request(req); 6271da177e4SLinus Torvalds nfs_release_request(req); 628e7d39069STrond Myklebust if (error != 0) 629e7d39069STrond Myklebust goto out_err; 630e7d39069STrond Myklebust spin_lock(&inode->i_lock); 6311da177e4SLinus Torvalds } 6321da177e4SLinus Torvalds 633e468bae9STrond Myklebust if (nfs_clear_request_commit(req)) 634e468bae9STrond Myklebust radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree, 635e468bae9STrond Myklebust req->wb_index, NFS_PAGE_TAG_COMMIT); 636e468bae9STrond Myklebust 6371da177e4SLinus Torvalds /* Okay, the request matches. Update the region */ 6381da177e4SLinus Torvalds if (offset < req->wb_offset) { 6391da177e4SLinus Torvalds req->wb_offset = offset; 6401da177e4SLinus Torvalds req->wb_pgbase = offset; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds if (end > rqend) 6431da177e4SLinus Torvalds req->wb_bytes = end - req->wb_offset; 644e7d39069STrond Myklebust else 645e7d39069STrond Myklebust req->wb_bytes = rqend - req->wb_offset; 646e7d39069STrond Myklebust out_unlock: 647e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 648e7d39069STrond Myklebust return req; 649e7d39069STrond Myklebust out_flushme: 650e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 651e7d39069STrond Myklebust nfs_release_request(req); 652e7d39069STrond Myklebust error = nfs_wb_page(inode, page); 653e7d39069STrond Myklebust out_err: 654e7d39069STrond Myklebust return ERR_PTR(error); 655e7d39069STrond Myklebust } 6561da177e4SLinus Torvalds 657e7d39069STrond Myklebust /* 658e7d39069STrond Myklebust * Try to update an existing write request, or create one if there is none. 659e7d39069STrond Myklebust * 660e7d39069STrond Myklebust * Note: Should always be called with the Page Lock held to prevent races 661e7d39069STrond Myklebust * if we have to add a new request. Also assumes that the caller has 662e7d39069STrond Myklebust * already called nfs_flush_incompatible() if necessary. 663e7d39069STrond Myklebust */ 664e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, 665e7d39069STrond Myklebust struct page *page, unsigned int offset, unsigned int bytes) 666e7d39069STrond Myklebust { 667e7d39069STrond Myklebust struct inode *inode = page->mapping->host; 668e7d39069STrond Myklebust struct nfs_page *req; 669e7d39069STrond Myklebust int error; 670e7d39069STrond Myklebust 671e7d39069STrond Myklebust req = nfs_try_to_update_request(inode, page, offset, bytes); 672e7d39069STrond Myklebust if (req != NULL) 673e7d39069STrond Myklebust goto out; 674e7d39069STrond Myklebust req = nfs_create_request(ctx, inode, page, offset, bytes); 675e7d39069STrond Myklebust if (IS_ERR(req)) 676e7d39069STrond Myklebust goto out; 677e7d39069STrond Myklebust error = nfs_inode_add_request(inode, req); 678e7d39069STrond Myklebust if (error != 0) { 679e7d39069STrond Myklebust nfs_release_request(req); 680e7d39069STrond Myklebust req = ERR_PTR(error); 681e7d39069STrond Myklebust } 682efc91ed0STrond Myklebust out: 68361e930a9STrond Myklebust return req; 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds 686e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, 687e7d39069STrond Myklebust unsigned int offset, unsigned int count) 688e7d39069STrond Myklebust { 689e7d39069STrond Myklebust struct nfs_page *req; 690e7d39069STrond Myklebust 691e7d39069STrond Myklebust req = nfs_setup_write_request(ctx, page, offset, count); 692e7d39069STrond Myklebust if (IS_ERR(req)) 693e7d39069STrond Myklebust return PTR_ERR(req); 694e7d39069STrond Myklebust /* Update file length */ 695e7d39069STrond Myklebust nfs_grow_file(page, offset, count); 696e7d39069STrond Myklebust nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); 697e7d39069STrond Myklebust nfs_clear_page_tag_locked(req); 698e7d39069STrond Myklebust return 0; 699e7d39069STrond Myklebust } 700e7d39069STrond Myklebust 7011da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page) 7021da177e4SLinus Torvalds { 703cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 7041da177e4SLinus Torvalds struct nfs_page *req; 7051a54533eSTrond Myklebust int do_flush, status; 7061da177e4SLinus Torvalds /* 7071da177e4SLinus Torvalds * Look for a request corresponding to this page. If there 7081da177e4SLinus Torvalds * is one, and it belongs to another file, we flush it out 7091da177e4SLinus Torvalds * before we try to copy anything into the page. Do this 7101da177e4SLinus Torvalds * due to the lack of an ACCESS-type call in NFSv2. 7111da177e4SLinus Torvalds * Also do the same if we find a request from an existing 7121da177e4SLinus Torvalds * dropped page. 7131da177e4SLinus Torvalds */ 7141a54533eSTrond Myklebust do { 715277459d2STrond Myklebust req = nfs_page_find_request(page); 7161a54533eSTrond Myklebust if (req == NULL) 7171a54533eSTrond Myklebust return 0; 718e468bae9STrond Myklebust do_flush = req->wb_page != page || req->wb_context != ctx; 7191da177e4SLinus Torvalds nfs_release_request(req); 7201a54533eSTrond Myklebust if (!do_flush) 7211a54533eSTrond Myklebust return 0; 722277459d2STrond Myklebust status = nfs_wb_page(page->mapping->host, page); 7231a54533eSTrond Myklebust } while (status == 0); 7241a54533eSTrond Myklebust return status; 7251da177e4SLinus Torvalds } 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds /* 7285d47a356STrond Myklebust * If the page cache is marked as unsafe or invalid, then we can't rely on 7295d47a356STrond Myklebust * the PageUptodate() flag. In this case, we will need to turn off 7305d47a356STrond Myklebust * write optimisations that depend on the page contents being correct. 7315d47a356STrond Myklebust */ 7325d47a356STrond Myklebust static int nfs_write_pageuptodate(struct page *page, struct inode *inode) 7335d47a356STrond Myklebust { 7345d47a356STrond Myklebust return PageUptodate(page) && 7355d47a356STrond Myklebust !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA)); 7365d47a356STrond Myklebust } 7375d47a356STrond Myklebust 7385d47a356STrond Myklebust /* 7391da177e4SLinus Torvalds * Update and possibly write a cached page of an NFS file. 7401da177e4SLinus Torvalds * 7411da177e4SLinus Torvalds * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad 7421da177e4SLinus Torvalds * things with a page scheduled for an RPC call (e.g. invalidate it). 7431da177e4SLinus Torvalds */ 7441da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page, 7451da177e4SLinus Torvalds unsigned int offset, unsigned int count) 7461da177e4SLinus Torvalds { 747cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 7481da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 7491da177e4SLinus Torvalds int status = 0; 7501da177e4SLinus Torvalds 75191d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 75291d5b470SChuck Lever 75348186c7dSChuck Lever dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n", 75401cce933SJosef "Jeff" Sipek file->f_path.dentry->d_parent->d_name.name, 75501cce933SJosef "Jeff" Sipek file->f_path.dentry->d_name.name, count, 7560bbacc40SChuck Lever (long long)(page_offset(page) + offset)); 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds /* If we're not using byte range locks, and we know the page 7595d47a356STrond Myklebust * is up to date, it may be more efficient to extend the write 7605d47a356STrond Myklebust * to cover the entire page in order to avoid fragmentation 7615d47a356STrond Myklebust * inefficiencies. 7621da177e4SLinus Torvalds */ 7635d47a356STrond Myklebust if (nfs_write_pageuptodate(page, inode) && 7645d47a356STrond Myklebust inode->i_flock == NULL && 7654b5621f6STrond Myklebust !(file->f_flags & O_SYNC)) { 76649a70f27STrond Myklebust count = max(count + offset, nfs_page_length(page)); 7671da177e4SLinus Torvalds offset = 0; 7681da177e4SLinus Torvalds } 7691da177e4SLinus Torvalds 770e21195a7STrond Myklebust status = nfs_writepage_setup(ctx, page, offset, count); 77103fa9e84STrond Myklebust if (status < 0) 77203fa9e84STrond Myklebust nfs_set_pageerror(page); 77303fa9e84STrond Myklebust else 774e261f51fSTrond Myklebust __set_page_dirty_nobuffers(page); 7751da177e4SLinus Torvalds 77648186c7dSChuck Lever dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", 7771da177e4SLinus Torvalds status, (long long)i_size_read(inode)); 7781da177e4SLinus Torvalds return status; 7791da177e4SLinus Torvalds } 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds static void nfs_writepage_release(struct nfs_page *req) 7821da177e4SLinus Torvalds { 7838e821cadSTrond Myklebust 7847e5f6146STrond Myklebust if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) { 78589a09141SPeter Zijlstra nfs_end_page_writeback(req->wb_page); 7861da177e4SLinus Torvalds nfs_inode_remove_request(req); 7878e821cadSTrond Myklebust } else 7888e821cadSTrond Myklebust nfs_end_page_writeback(req->wb_page); 7899fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 7901da177e4SLinus Torvalds } 7911da177e4SLinus Torvalds 7923ff7576dSTrond Myklebust static int flush_task_priority(int how) 7931da177e4SLinus Torvalds { 7941da177e4SLinus Torvalds switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { 7951da177e4SLinus Torvalds case FLUSH_HIGHPRI: 7961da177e4SLinus Torvalds return RPC_PRIORITY_HIGH; 7971da177e4SLinus Torvalds case FLUSH_LOWPRI: 7981da177e4SLinus Torvalds return RPC_PRIORITY_LOW; 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds return RPC_PRIORITY_NORMAL; 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds /* 8041da177e4SLinus Torvalds * Set up the argument/result storage required for the RPC call. 8051da177e4SLinus Torvalds */ 806dbae4c73STrond Myklebust static int nfs_write_rpcsetup(struct nfs_page *req, 8071da177e4SLinus Torvalds struct nfs_write_data *data, 808788e7a89STrond Myklebust const struct rpc_call_ops *call_ops, 8091da177e4SLinus Torvalds unsigned int count, unsigned int offset, 8101da177e4SLinus Torvalds int how) 8111da177e4SLinus Torvalds { 81284115e1cSTrond Myklebust struct inode *inode = req->wb_context->path.dentry->d_inode; 81384115e1cSTrond Myklebust int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC; 8143ff7576dSTrond Myklebust int priority = flush_task_priority(how); 81507737691STrond Myklebust struct rpc_task *task; 816bdc7f021STrond Myklebust struct rpc_message msg = { 817bdc7f021STrond Myklebust .rpc_argp = &data->args, 818bdc7f021STrond Myklebust .rpc_resp = &data->res, 819bdc7f021STrond Myklebust .rpc_cred = req->wb_context->cred, 820bdc7f021STrond Myklebust }; 82184115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 82284115e1cSTrond Myklebust .rpc_client = NFS_CLIENT(inode), 82307737691STrond Myklebust .task = &data->task, 824bdc7f021STrond Myklebust .rpc_message = &msg, 82584115e1cSTrond Myklebust .callback_ops = call_ops, 82684115e1cSTrond Myklebust .callback_data = data, 827101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 82884115e1cSTrond Myklebust .flags = flags, 8293ff7576dSTrond Myklebust .priority = priority, 83084115e1cSTrond Myklebust }; 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds /* Set up the RPC argument and reply structs 8331da177e4SLinus Torvalds * NB: take care not to mess about with data->commit et al. */ 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds data->req = req; 83688be9f99STrond Myklebust data->inode = inode = req->wb_context->path.dentry->d_inode; 837bdc7f021STrond Myklebust data->cred = msg.rpc_cred; 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 8401da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 8411da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 8421da177e4SLinus Torvalds data->args.pages = data->pagevec; 8431da177e4SLinus Torvalds data->args.count = count; 844383ba719STrond Myklebust data->args.context = get_nfs_open_context(req->wb_context); 845bdc7f021STrond Myklebust data->args.stable = NFS_UNSTABLE; 846bdc7f021STrond Myklebust if (how & FLUSH_STABLE) { 847bdc7f021STrond Myklebust data->args.stable = NFS_DATA_SYNC; 848fb8a1f11STrond Myklebust if (!nfs_need_commit(NFS_I(inode))) 849bdc7f021STrond Myklebust data->args.stable = NFS_FILE_SYNC; 850bdc7f021STrond Myklebust } 8511da177e4SLinus Torvalds 8521da177e4SLinus Torvalds data->res.fattr = &data->fattr; 8531da177e4SLinus Torvalds data->res.count = count; 8541da177e4SLinus Torvalds data->res.verf = &data->verf; 8550e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 8561da177e4SLinus Torvalds 857788e7a89STrond Myklebust /* Set up the initial task struct. */ 858bdc7f021STrond Myklebust NFS_PROTO(inode)->write_setup(data, &msg); 8591da177e4SLinus Torvalds 860a3f565b1SChuck Lever dprintk("NFS: %5u initiated write call " 86148186c7dSChuck Lever "(req %s/%lld, %u bytes @ offset %llu)\n", 8620bbacc40SChuck Lever data->task.tk_pid, 8631da177e4SLinus Torvalds inode->i_sb->s_id, 8641da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 8651da177e4SLinus Torvalds count, 8661da177e4SLinus Torvalds (unsigned long long)data->args.offset); 8671da177e4SLinus Torvalds 86807737691STrond Myklebust task = rpc_run_task(&task_setup_data); 869dbae4c73STrond Myklebust if (IS_ERR(task)) 870dbae4c73STrond Myklebust return PTR_ERR(task); 87107737691STrond Myklebust rpc_put_task(task); 872dbae4c73STrond Myklebust return 0; 8731da177e4SLinus Torvalds } 8741da177e4SLinus Torvalds 8756d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and 8766d884e8fSFred * call this on each, which will prepare them to be retried on next 8776d884e8fSFred * writeback using standard nfs. 8786d884e8fSFred */ 8796d884e8fSFred static void nfs_redirty_request(struct nfs_page *req) 8806d884e8fSFred { 8816d884e8fSFred nfs_mark_request_dirty(req); 8826d884e8fSFred nfs_end_page_writeback(req->wb_page); 8836d884e8fSFred nfs_clear_page_tag_locked(req); 8846d884e8fSFred } 8856d884e8fSFred 8861da177e4SLinus Torvalds /* 8871da177e4SLinus Torvalds * Generate multiple small requests to write out a single 8881da177e4SLinus Torvalds * contiguous dirty area on one page. 8891da177e4SLinus Torvalds */ 8908d5658c9STrond Myklebust static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how) 8911da177e4SLinus Torvalds { 8921da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(head->next); 8931da177e4SLinus Torvalds struct page *page = req->wb_page; 8941da177e4SLinus Torvalds struct nfs_write_data *data; 895e9f7bee1STrond Myklebust size_t wsize = NFS_SERVER(inode)->wsize, nbytes; 896e9f7bee1STrond Myklebust unsigned int offset; 8971da177e4SLinus Torvalds int requests = 0; 898dbae4c73STrond Myklebust int ret = 0; 8991da177e4SLinus Torvalds LIST_HEAD(list); 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds nfs_list_remove_request(req); 9021da177e4SLinus Torvalds 903bcb71bbaSTrond Myklebust nbytes = count; 904e9f7bee1STrond Myklebust do { 905e9f7bee1STrond Myklebust size_t len = min(nbytes, wsize); 906e9f7bee1STrond Myklebust 9078d5658c9STrond Myklebust data = nfs_writedata_alloc(1); 9081da177e4SLinus Torvalds if (!data) 9091da177e4SLinus Torvalds goto out_bad; 9101da177e4SLinus Torvalds list_add(&data->pages, &list); 9111da177e4SLinus Torvalds requests++; 912e9f7bee1STrond Myklebust nbytes -= len; 913e9f7bee1STrond Myklebust } while (nbytes != 0); 9141da177e4SLinus Torvalds atomic_set(&req->wb_complete, requests); 9151da177e4SLinus Torvalds 9161da177e4SLinus Torvalds ClearPageError(page); 9171da177e4SLinus Torvalds offset = 0; 918bcb71bbaSTrond Myklebust nbytes = count; 9191da177e4SLinus Torvalds do { 920dbae4c73STrond Myklebust int ret2; 921dbae4c73STrond Myklebust 9221da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_write_data, pages); 9231da177e4SLinus Torvalds list_del_init(&data->pages); 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds data->pagevec[0] = page; 9261da177e4SLinus Torvalds 927bcb71bbaSTrond Myklebust if (nbytes < wsize) 928bcb71bbaSTrond Myklebust wsize = nbytes; 929dbae4c73STrond Myklebust ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops, 930788e7a89STrond Myklebust wsize, offset, how); 931dbae4c73STrond Myklebust if (ret == 0) 932dbae4c73STrond Myklebust ret = ret2; 9331da177e4SLinus Torvalds offset += wsize; 9341da177e4SLinus Torvalds nbytes -= wsize; 9351da177e4SLinus Torvalds } while (nbytes != 0); 9361da177e4SLinus Torvalds 937dbae4c73STrond Myklebust return ret; 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds out_bad: 9401da177e4SLinus Torvalds while (!list_empty(&list)) { 9411da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_write_data, pages); 9421da177e4SLinus Torvalds list_del(&data->pages); 9438aca67f0STrond Myklebust nfs_writedata_release(data); 9441da177e4SLinus Torvalds } 94561822ab5STrond Myklebust nfs_redirty_request(req); 9461da177e4SLinus Torvalds return -ENOMEM; 9471da177e4SLinus Torvalds } 9481da177e4SLinus Torvalds 9491da177e4SLinus Torvalds /* 9501da177e4SLinus Torvalds * Create an RPC task for the given write request and kick it. 9511da177e4SLinus Torvalds * The page must have been locked by the caller. 9521da177e4SLinus Torvalds * 9531da177e4SLinus Torvalds * It may happen that the page we're passed is not marked dirty. 9541da177e4SLinus Torvalds * This is the case if nfs_updatepage detects a conflicting request 9551da177e4SLinus Torvalds * that has been written but not committed. 9561da177e4SLinus Torvalds */ 9578d5658c9STrond Myklebust static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how) 9581da177e4SLinus Torvalds { 9591da177e4SLinus Torvalds struct nfs_page *req; 9601da177e4SLinus Torvalds struct page **pages; 9611da177e4SLinus Torvalds struct nfs_write_data *data; 9621da177e4SLinus Torvalds 9638d5658c9STrond Myklebust data = nfs_writedata_alloc(npages); 9641da177e4SLinus Torvalds if (!data) 9651da177e4SLinus Torvalds goto out_bad; 9661da177e4SLinus Torvalds 9671da177e4SLinus Torvalds pages = data->pagevec; 9681da177e4SLinus Torvalds while (!list_empty(head)) { 9691da177e4SLinus Torvalds req = nfs_list_entry(head->next); 9701da177e4SLinus Torvalds nfs_list_remove_request(req); 9711da177e4SLinus Torvalds nfs_list_add_request(req, &data->pages); 9721da177e4SLinus Torvalds ClearPageError(req->wb_page); 9731da177e4SLinus Torvalds *pages++ = req->wb_page; 9741da177e4SLinus Torvalds } 9751da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds /* Set up the argument struct */ 978dbae4c73STrond Myklebust return nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how); 9791da177e4SLinus Torvalds out_bad: 9801da177e4SLinus Torvalds while (!list_empty(head)) { 98110afec90STrond Myklebust req = nfs_list_entry(head->next); 9821da177e4SLinus Torvalds nfs_list_remove_request(req); 98361822ab5STrond Myklebust nfs_redirty_request(req); 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds return -ENOMEM; 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 988c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, 989c63c7b05STrond Myklebust struct inode *inode, int ioflags) 9901da177e4SLinus Torvalds { 991bf4285e7SChuck Lever size_t wsize = NFS_SERVER(inode)->wsize; 9921da177e4SLinus Torvalds 993bcb71bbaSTrond Myklebust if (wsize < PAGE_CACHE_SIZE) 994c63c7b05STrond Myklebust nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags); 995bcb71bbaSTrond Myklebust else 996c63c7b05STrond Myklebust nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags); 9971da177e4SLinus Torvalds } 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds /* 10001da177e4SLinus Torvalds * Handle a write reply that flushed part of a page. 10011da177e4SLinus Torvalds */ 1002788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata) 10031da177e4SLinus Torvalds { 1004788e7a89STrond Myklebust struct nfs_write_data *data = calldata; 10051da177e4SLinus Torvalds 100648186c7dSChuck Lever dprintk("NFS: %5u write(%s/%lld %d@%lld)", 100748186c7dSChuck Lever task->tk_pid, 100848186c7dSChuck Lever data->req->wb_context->path.dentry->d_inode->i_sb->s_id, 100948186c7dSChuck Lever (long long) 101048186c7dSChuck Lever NFS_FILEID(data->req->wb_context->path.dentry->d_inode), 101148186c7dSChuck Lever data->req->wb_bytes, (long long)req_offset(data->req)); 10121da177e4SLinus Torvalds 1013c9d8f89dSTrond Myklebust nfs_writeback_done(task, data); 1014c9d8f89dSTrond Myklebust } 1015788e7a89STrond Myklebust 1016c9d8f89dSTrond Myklebust static void nfs_writeback_release_partial(void *calldata) 1017c9d8f89dSTrond Myklebust { 1018c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1019c9d8f89dSTrond Myklebust struct nfs_page *req = data->req; 1020c9d8f89dSTrond Myklebust struct page *page = req->wb_page; 1021c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1022c9d8f89dSTrond Myklebust 1023c9d8f89dSTrond Myklebust if (status < 0) { 1024a301b777STrond Myklebust nfs_set_pageerror(page); 1025c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 1026c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 10278e821cadSTrond Myklebust goto out; 10288e821cadSTrond Myklebust } 10298e821cadSTrond Myklebust 10308e821cadSTrond Myklebust if (nfs_write_need_commit(data)) { 1031587142f8STrond Myklebust struct inode *inode = page->mapping->host; 10328e821cadSTrond Myklebust 1033587142f8STrond Myklebust spin_lock(&inode->i_lock); 10348e821cadSTrond Myklebust if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) { 10358e821cadSTrond Myklebust /* Do nothing we need to resend the writes */ 10368e821cadSTrond Myklebust } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) { 10371da177e4SLinus Torvalds memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); 10381da177e4SLinus Torvalds dprintk(" defer commit\n"); 10391da177e4SLinus Torvalds } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) { 10408e821cadSTrond Myklebust set_bit(PG_NEED_RESCHED, &req->wb_flags); 10418e821cadSTrond Myklebust clear_bit(PG_NEED_COMMIT, &req->wb_flags); 10421da177e4SLinus Torvalds dprintk(" server reboot detected\n"); 10431da177e4SLinus Torvalds } 1044587142f8STrond Myklebust spin_unlock(&inode->i_lock); 10451da177e4SLinus Torvalds } else 10461da177e4SLinus Torvalds dprintk(" OK\n"); 10471da177e4SLinus Torvalds 10488e821cadSTrond Myklebust out: 10491da177e4SLinus Torvalds if (atomic_dec_and_test(&req->wb_complete)) 10501da177e4SLinus Torvalds nfs_writepage_release(req); 1051c9d8f89dSTrond Myklebust nfs_writedata_release(calldata); 10521da177e4SLinus Torvalds } 10531da177e4SLinus Torvalds 1054def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1) 1055def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata) 1056def6ed7eSAndy Adamson { 1057def6ed7eSAndy Adamson struct nfs_write_data *data = calldata; 1058def6ed7eSAndy Adamson struct nfs_client *clp = (NFS_SERVER(data->inode))->nfs_client; 1059def6ed7eSAndy Adamson 1060def6ed7eSAndy Adamson if (nfs4_setup_sequence(clp, &data->args.seq_args, 1061def6ed7eSAndy Adamson &data->res.seq_res, 1, task)) 1062def6ed7eSAndy Adamson return; 1063def6ed7eSAndy Adamson rpc_call_start(task); 1064def6ed7eSAndy Adamson } 1065def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1066def6ed7eSAndy Adamson 1067788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = { 1068def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1) 1069def6ed7eSAndy Adamson .rpc_call_prepare = nfs_write_prepare, 1070def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1071788e7a89STrond Myklebust .rpc_call_done = nfs_writeback_done_partial, 1072c9d8f89dSTrond Myklebust .rpc_release = nfs_writeback_release_partial, 1073788e7a89STrond Myklebust }; 1074788e7a89STrond Myklebust 10751da177e4SLinus Torvalds /* 10761da177e4SLinus Torvalds * Handle a write reply that flushes a whole page. 10771da177e4SLinus Torvalds * 10781da177e4SLinus Torvalds * FIXME: There is an inherent race with invalidate_inode_pages and 10791da177e4SLinus Torvalds * writebacks since the page->count is kept > 1 for as long 10801da177e4SLinus Torvalds * as the page has a write request pending. 10811da177e4SLinus Torvalds */ 1082788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata) 10831da177e4SLinus Torvalds { 1084788e7a89STrond Myklebust struct nfs_write_data *data = calldata; 10851da177e4SLinus Torvalds 1086c9d8f89dSTrond Myklebust nfs_writeback_done(task, data); 1087c9d8f89dSTrond Myklebust } 1088c9d8f89dSTrond Myklebust 1089c9d8f89dSTrond Myklebust static void nfs_writeback_release_full(void *calldata) 1090c9d8f89dSTrond Myklebust { 1091c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1092c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1093788e7a89STrond Myklebust 10941da177e4SLinus Torvalds /* Update attributes as result of writeback. */ 10951da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 1096c9d8f89dSTrond Myklebust struct nfs_page *req = nfs_list_entry(data->pages.next); 1097c9d8f89dSTrond Myklebust struct page *page = req->wb_page; 1098c9d8f89dSTrond Myklebust 10991da177e4SLinus Torvalds nfs_list_remove_request(req); 11001da177e4SLinus Torvalds 110148186c7dSChuck Lever dprintk("NFS: %5u write (%s/%lld %d@%lld)", 110248186c7dSChuck Lever data->task.tk_pid, 110388be9f99STrond Myklebust req->wb_context->path.dentry->d_inode->i_sb->s_id, 110488be9f99STrond Myklebust (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 11051da177e4SLinus Torvalds req->wb_bytes, 11061da177e4SLinus Torvalds (long long)req_offset(req)); 11071da177e4SLinus Torvalds 1108c9d8f89dSTrond Myklebust if (status < 0) { 1109a301b777STrond Myklebust nfs_set_pageerror(page); 1110c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 1111c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 11128e821cadSTrond Myklebust goto remove_request; 11131da177e4SLinus Torvalds } 11141da177e4SLinus Torvalds 11158e821cadSTrond Myklebust if (nfs_write_need_commit(data)) { 11161da177e4SLinus Torvalds memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); 11171da177e4SLinus Torvalds nfs_mark_request_commit(req); 11188e821cadSTrond Myklebust nfs_end_page_writeback(page); 11191da177e4SLinus Torvalds dprintk(" marked for commit\n"); 11208e821cadSTrond Myklebust goto next; 11218e821cadSTrond Myklebust } 11228e821cadSTrond Myklebust dprintk(" OK\n"); 11238e821cadSTrond Myklebust remove_request: 11248e821cadSTrond Myklebust nfs_end_page_writeback(page); 11251da177e4SLinus Torvalds nfs_inode_remove_request(req); 11261da177e4SLinus Torvalds next: 11279fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 11281da177e4SLinus Torvalds } 1129c9d8f89dSTrond Myklebust nfs_writedata_release(calldata); 11301da177e4SLinus Torvalds } 11311da177e4SLinus Torvalds 1132788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = { 1133def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1) 1134def6ed7eSAndy Adamson .rpc_call_prepare = nfs_write_prepare, 1135def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1136788e7a89STrond Myklebust .rpc_call_done = nfs_writeback_done_full, 1137c9d8f89dSTrond Myklebust .rpc_release = nfs_writeback_release_full, 1138788e7a89STrond Myklebust }; 1139788e7a89STrond Myklebust 1140788e7a89STrond Myklebust 11411da177e4SLinus Torvalds /* 11421da177e4SLinus Torvalds * This function is called when the WRITE call is complete. 11431da177e4SLinus Torvalds */ 1144462d5b32SChuck Lever int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data) 11451da177e4SLinus Torvalds { 11461da177e4SLinus Torvalds struct nfs_writeargs *argp = &data->args; 11471da177e4SLinus Torvalds struct nfs_writeres *resp = &data->res; 1148788e7a89STrond Myklebust int status; 11491da177e4SLinus Torvalds 1150a3f565b1SChuck Lever dprintk("NFS: %5u nfs_writeback_done (status %d)\n", 11511da177e4SLinus Torvalds task->tk_pid, task->tk_status); 11521da177e4SLinus Torvalds 1153f551e44fSChuck Lever /* 1154f551e44fSChuck Lever * ->write_done will attempt to use post-op attributes to detect 1155f551e44fSChuck Lever * conflicting writes by other clients. A strict interpretation 1156f551e44fSChuck Lever * of close-to-open would allow us to continue caching even if 1157f551e44fSChuck Lever * another writer had changed the file, but some applications 1158f551e44fSChuck Lever * depend on tighter cache coherency when writing. 1159f551e44fSChuck Lever */ 1160788e7a89STrond Myklebust status = NFS_PROTO(data->inode)->write_done(task, data); 1161788e7a89STrond Myklebust if (status != 0) 1162788e7a89STrond Myklebust return status; 116391d5b470SChuck Lever nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count); 116491d5b470SChuck Lever 11651da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 11661da177e4SLinus Torvalds if (resp->verf->committed < argp->stable && task->tk_status >= 0) { 11671da177e4SLinus Torvalds /* We tried a write call, but the server did not 11681da177e4SLinus Torvalds * commit data to stable storage even though we 11691da177e4SLinus Torvalds * requested it. 11701da177e4SLinus Torvalds * Note: There is a known bug in Tru64 < 5.0 in which 11711da177e4SLinus Torvalds * the server reports NFS_DATA_SYNC, but performs 11721da177e4SLinus Torvalds * NFS_FILE_SYNC. We therefore implement this checking 11731da177e4SLinus Torvalds * as a dprintk() in order to avoid filling syslog. 11741da177e4SLinus Torvalds */ 11751da177e4SLinus Torvalds static unsigned long complain; 11761da177e4SLinus Torvalds 11771da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 11781da177e4SLinus Torvalds dprintk("NFS: faulty NFS server %s:" 11791da177e4SLinus Torvalds " (committed = %d) != (stable = %d)\n", 118054ceac45SDavid Howells NFS_SERVER(data->inode)->nfs_client->cl_hostname, 11811da177e4SLinus Torvalds resp->verf->committed, argp->stable); 11821da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 11831da177e4SLinus Torvalds } 11841da177e4SLinus Torvalds } 11851da177e4SLinus Torvalds #endif 11861da177e4SLinus Torvalds /* Is this a short write? */ 11871da177e4SLinus Torvalds if (task->tk_status >= 0 && resp->count < argp->count) { 11881da177e4SLinus Torvalds static unsigned long complain; 11891da177e4SLinus Torvalds 119091d5b470SChuck Lever nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE); 119191d5b470SChuck Lever 11921da177e4SLinus Torvalds /* Has the server at least made some progress? */ 11931da177e4SLinus Torvalds if (resp->count != 0) { 11941da177e4SLinus Torvalds /* Was this an NFSv2 write or an NFSv3 stable write? */ 11951da177e4SLinus Torvalds if (resp->verf->committed != NFS_UNSTABLE) { 11961da177e4SLinus Torvalds /* Resend from where the server left off */ 11971da177e4SLinus Torvalds argp->offset += resp->count; 11981da177e4SLinus Torvalds argp->pgbase += resp->count; 11991da177e4SLinus Torvalds argp->count -= resp->count; 12001da177e4SLinus Torvalds } else { 12011da177e4SLinus Torvalds /* Resend as a stable write in order to avoid 12021da177e4SLinus Torvalds * headaches in the case of a server crash. 12031da177e4SLinus Torvalds */ 12041da177e4SLinus Torvalds argp->stable = NFS_FILE_SYNC; 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds rpc_restart_call(task); 1207788e7a89STrond Myklebust return -EAGAIN; 12081da177e4SLinus Torvalds } 12091da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 12101da177e4SLinus Torvalds printk(KERN_WARNING 12111da177e4SLinus Torvalds "NFS: Server wrote zero bytes, expected %u.\n", 12121da177e4SLinus Torvalds argp->count); 12131da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds /* Can't do anything about it except throw an error. */ 12161da177e4SLinus Torvalds task->tk_status = -EIO; 12171da177e4SLinus Torvalds } 1218def6ed7eSAndy Adamson nfs4_sequence_free_slot(NFS_SERVER(data->inode)->nfs_client, 1219def6ed7eSAndy Adamson &data->res.seq_res); 1220788e7a89STrond Myklebust return 0; 12211da177e4SLinus Torvalds } 12221da177e4SLinus Torvalds 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 1225c9d8f89dSTrond Myklebust void nfs_commitdata_release(void *data) 12261da177e4SLinus Torvalds { 1227383ba719STrond Myklebust struct nfs_write_data *wdata = data; 1228383ba719STrond Myklebust 1229383ba719STrond Myklebust put_nfs_open_context(wdata->args.context); 12301da177e4SLinus Torvalds nfs_commit_free(wdata); 12311da177e4SLinus Torvalds } 12321da177e4SLinus Torvalds 12331da177e4SLinus Torvalds /* 12341da177e4SLinus Torvalds * Set up the argument/result storage required for the RPC call. 12351da177e4SLinus Torvalds */ 1236dbae4c73STrond Myklebust static int nfs_commit_rpcsetup(struct list_head *head, 1237788e7a89STrond Myklebust struct nfs_write_data *data, 1238788e7a89STrond Myklebust int how) 12391da177e4SLinus Torvalds { 124084115e1cSTrond Myklebust struct nfs_page *first = nfs_list_entry(head->next); 124184115e1cSTrond Myklebust struct inode *inode = first->wb_context->path.dentry->d_inode; 124284115e1cSTrond Myklebust int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC; 12433ff7576dSTrond Myklebust int priority = flush_task_priority(how); 124407737691STrond Myklebust struct rpc_task *task; 1245bdc7f021STrond Myklebust struct rpc_message msg = { 1246bdc7f021STrond Myklebust .rpc_argp = &data->args, 1247bdc7f021STrond Myklebust .rpc_resp = &data->res, 1248bdc7f021STrond Myklebust .rpc_cred = first->wb_context->cred, 1249bdc7f021STrond Myklebust }; 125084115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 125107737691STrond Myklebust .task = &data->task, 125284115e1cSTrond Myklebust .rpc_client = NFS_CLIENT(inode), 1253bdc7f021STrond Myklebust .rpc_message = &msg, 125484115e1cSTrond Myklebust .callback_ops = &nfs_commit_ops, 125584115e1cSTrond Myklebust .callback_data = data, 1256101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 125784115e1cSTrond Myklebust .flags = flags, 12583ff7576dSTrond Myklebust .priority = priority, 125984115e1cSTrond Myklebust }; 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds /* Set up the RPC argument and reply structs 12621da177e4SLinus Torvalds * NB: take care not to mess about with data->commit et al. */ 12631da177e4SLinus Torvalds 12641da177e4SLinus Torvalds list_splice_init(head, &data->pages); 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds data->inode = inode; 1267bdc7f021STrond Myklebust data->cred = msg.rpc_cred; 12681da177e4SLinus Torvalds 12691da177e4SLinus Torvalds data->args.fh = NFS_FH(data->inode); 12703da28eb1STrond Myklebust /* Note: we always request a commit of the entire inode */ 12713da28eb1STrond Myklebust data->args.offset = 0; 12723da28eb1STrond Myklebust data->args.count = 0; 1273383ba719STrond Myklebust data->args.context = get_nfs_open_context(first->wb_context); 12743da28eb1STrond Myklebust data->res.count = 0; 12751da177e4SLinus Torvalds data->res.fattr = &data->fattr; 12761da177e4SLinus Torvalds data->res.verf = &data->verf; 12770e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 12781da177e4SLinus Torvalds 1279788e7a89STrond Myklebust /* Set up the initial task struct. */ 1280bdc7f021STrond Myklebust NFS_PROTO(inode)->commit_setup(data, &msg); 12811da177e4SLinus Torvalds 1282a3f565b1SChuck Lever dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); 1283bdc7f021STrond Myklebust 128407737691STrond Myklebust task = rpc_run_task(&task_setup_data); 1285dbae4c73STrond Myklebust if (IS_ERR(task)) 1286dbae4c73STrond Myklebust return PTR_ERR(task); 128707737691STrond Myklebust rpc_put_task(task); 1288dbae4c73STrond Myklebust return 0; 12891da177e4SLinus Torvalds } 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds /* 12921da177e4SLinus Torvalds * Commit dirty pages 12931da177e4SLinus Torvalds */ 12941da177e4SLinus Torvalds static int 129540859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how) 12961da177e4SLinus Torvalds { 12971da177e4SLinus Torvalds struct nfs_write_data *data; 12981da177e4SLinus Torvalds struct nfs_page *req; 12991da177e4SLinus Torvalds 1300c9d8f89dSTrond Myklebust data = nfs_commitdata_alloc(); 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds if (!data) 13031da177e4SLinus Torvalds goto out_bad; 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds /* Set up the argument struct */ 1306dbae4c73STrond Myklebust return nfs_commit_rpcsetup(head, data, how); 13071da177e4SLinus Torvalds out_bad: 13081da177e4SLinus Torvalds while (!list_empty(head)) { 13091da177e4SLinus Torvalds req = nfs_list_entry(head->next); 13101da177e4SLinus Torvalds nfs_list_remove_request(req); 13111da177e4SLinus Torvalds nfs_mark_request_commit(req); 131283715ad5STrond Myklebust dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 1313c9e51e41SPeter Zijlstra dec_bdi_stat(req->wb_page->mapping->backing_dev_info, 1314c9e51e41SPeter Zijlstra BDI_RECLAIMABLE); 13159fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 13161da177e4SLinus Torvalds } 13171da177e4SLinus Torvalds return -ENOMEM; 13181da177e4SLinus Torvalds } 13191da177e4SLinus Torvalds 13201da177e4SLinus Torvalds /* 13211da177e4SLinus Torvalds * COMMIT call returned 13221da177e4SLinus Torvalds */ 1323788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata) 13241da177e4SLinus Torvalds { 1325963d8fe5STrond Myklebust struct nfs_write_data *data = calldata; 13261da177e4SLinus Torvalds 1327a3f565b1SChuck Lever dprintk("NFS: %5u nfs_commit_done (status %d)\n", 13281da177e4SLinus Torvalds task->tk_pid, task->tk_status); 13291da177e4SLinus Torvalds 1330788e7a89STrond Myklebust /* Call the NFS version-specific code */ 1331788e7a89STrond Myklebust if (NFS_PROTO(data->inode)->commit_done(task, data) != 0) 1332788e7a89STrond Myklebust return; 1333c9d8f89dSTrond Myklebust } 1334c9d8f89dSTrond Myklebust 1335c9d8f89dSTrond Myklebust static void nfs_commit_release(void *calldata) 1336c9d8f89dSTrond Myklebust { 1337c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1338c9d8f89dSTrond Myklebust struct nfs_page *req; 1339c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1340788e7a89STrond Myklebust 13411da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 13421da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 13431da177e4SLinus Torvalds nfs_list_remove_request(req); 1344e468bae9STrond Myklebust nfs_clear_request_commit(req); 13451da177e4SLinus Torvalds 134648186c7dSChuck Lever dprintk("NFS: commit (%s/%lld %d@%lld)", 134788be9f99STrond Myklebust req->wb_context->path.dentry->d_inode->i_sb->s_id, 134888be9f99STrond Myklebust (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 13491da177e4SLinus Torvalds req->wb_bytes, 13501da177e4SLinus Torvalds (long long)req_offset(req)); 1351c9d8f89dSTrond Myklebust if (status < 0) { 1352c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 13531da177e4SLinus Torvalds nfs_inode_remove_request(req); 1354c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 13551da177e4SLinus Torvalds goto next; 13561da177e4SLinus Torvalds } 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds /* Okay, COMMIT succeeded, apparently. Check the verifier 13591da177e4SLinus Torvalds * returned by the server against all stored verfs. */ 13601da177e4SLinus Torvalds if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) { 13611da177e4SLinus Torvalds /* We have a match */ 13621da177e4SLinus Torvalds nfs_inode_remove_request(req); 13631da177e4SLinus Torvalds dprintk(" OK\n"); 13641da177e4SLinus Torvalds goto next; 13651da177e4SLinus Torvalds } 13661da177e4SLinus Torvalds /* We have a mismatch. Write the page again */ 13671da177e4SLinus Torvalds dprintk(" mismatch\n"); 13686d884e8fSFred nfs_mark_request_dirty(req); 13691da177e4SLinus Torvalds next: 13709fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 13711da177e4SLinus Torvalds } 1372c9d8f89dSTrond Myklebust nfs_commitdata_release(calldata); 13731da177e4SLinus Torvalds } 1374788e7a89STrond Myklebust 1375788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = { 1376*21d9a851SAndy Adamson #if defined(CONFIG_NFS_V4_1) 1377*21d9a851SAndy Adamson .rpc_call_prepare = nfs_write_prepare, 1378*21d9a851SAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1379788e7a89STrond Myklebust .rpc_call_done = nfs_commit_done, 1380788e7a89STrond Myklebust .rpc_release = nfs_commit_release, 1381788e7a89STrond Myklebust }; 13821da177e4SLinus Torvalds 13833da28eb1STrond Myklebust int nfs_commit_inode(struct inode *inode, int how) 13841da177e4SLinus Torvalds { 13851da177e4SLinus Torvalds LIST_HEAD(head); 13867d46a49fSTrond Myklebust int res; 13871da177e4SLinus Torvalds 1388587142f8STrond Myklebust spin_lock(&inode->i_lock); 13893da28eb1STrond Myklebust res = nfs_scan_commit(inode, &head, 0, 0); 1390587142f8STrond Myklebust spin_unlock(&inode->i_lock); 13911da177e4SLinus Torvalds if (res) { 13927d46a49fSTrond Myklebust int error = nfs_commit_list(inode, &head, how); 13931da177e4SLinus Torvalds if (error < 0) 13941da177e4SLinus Torvalds return error; 13953da28eb1STrond Myklebust } 13961da177e4SLinus Torvalds return res; 13971da177e4SLinus Torvalds } 1398c63c7b05STrond Myklebust #else 1399c63c7b05STrond Myklebust static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how) 1400c63c7b05STrond Myklebust { 1401c63c7b05STrond Myklebust return 0; 1402c63c7b05STrond Myklebust } 14031da177e4SLinus Torvalds #endif 14041da177e4SLinus Torvalds 14051c75950bSTrond Myklebust long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how) 14061da177e4SLinus Torvalds { 14071c75950bSTrond Myklebust struct inode *inode = mapping->host; 1408ca52fec1STrond Myklebust pgoff_t idx_start, idx_end; 14091c75950bSTrond Myklebust unsigned int npages = 0; 1410c42de9ddSTrond Myklebust LIST_HEAD(head); 141170b9ecbdSTrond Myklebust int nocommit = how & FLUSH_NOCOMMIT; 14123f442547STrond Myklebust long pages, ret; 14131da177e4SLinus Torvalds 14141c75950bSTrond Myklebust /* FIXME */ 14151c75950bSTrond Myklebust if (wbc->range_cyclic) 14161c75950bSTrond Myklebust idx_start = 0; 14171c75950bSTrond Myklebust else { 14181c75950bSTrond Myklebust idx_start = wbc->range_start >> PAGE_CACHE_SHIFT; 14191c75950bSTrond Myklebust idx_end = wbc->range_end >> PAGE_CACHE_SHIFT; 14201c75950bSTrond Myklebust if (idx_end > idx_start) { 1421ca52fec1STrond Myklebust pgoff_t l_npages = 1 + idx_end - idx_start; 14221c75950bSTrond Myklebust npages = l_npages; 14231c75950bSTrond Myklebust if (sizeof(npages) != sizeof(l_npages) && 1424ca52fec1STrond Myklebust (pgoff_t)npages != l_npages) 14251c75950bSTrond Myklebust npages = 0; 14261c75950bSTrond Myklebust } 14271c75950bSTrond Myklebust } 1428c42de9ddSTrond Myklebust how &= ~FLUSH_NOCOMMIT; 1429587142f8STrond Myklebust spin_lock(&inode->i_lock); 14301da177e4SLinus Torvalds do { 1431c42de9ddSTrond Myklebust ret = nfs_wait_on_requests_locked(inode, idx_start, npages); 1432c42de9ddSTrond Myklebust if (ret != 0) 1433c42de9ddSTrond Myklebust continue; 1434c42de9ddSTrond Myklebust if (nocommit) 1435c42de9ddSTrond Myklebust break; 1436d2ccddf0STrond Myklebust pages = nfs_scan_commit(inode, &head, idx_start, npages); 1437724c439cSTrond Myklebust if (pages == 0) 1438c42de9ddSTrond Myklebust break; 1439d2ccddf0STrond Myklebust if (how & FLUSH_INVALIDATE) { 1440587142f8STrond Myklebust spin_unlock(&inode->i_lock); 144183715ad5STrond Myklebust nfs_cancel_commit_list(&head); 1442e8e058e8STrond Myklebust ret = pages; 1443587142f8STrond Myklebust spin_lock(&inode->i_lock); 1444d2ccddf0STrond Myklebust continue; 1445d2ccddf0STrond Myklebust } 1446d2ccddf0STrond Myklebust pages += nfs_scan_commit(inode, &head, 0, 0); 1447587142f8STrond Myklebust spin_unlock(&inode->i_lock); 1448c42de9ddSTrond Myklebust ret = nfs_commit_list(inode, &head, how); 1449587142f8STrond Myklebust spin_lock(&inode->i_lock); 1450587142f8STrond Myklebust 1451c42de9ddSTrond Myklebust } while (ret >= 0); 1452587142f8STrond Myklebust spin_unlock(&inode->i_lock); 1453c42de9ddSTrond Myklebust return ret; 14541da177e4SLinus Torvalds } 14551da177e4SLinus Torvalds 145634901f70STrond Myklebust static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how) 14571c75950bSTrond Myklebust { 14581c75950bSTrond Myklebust int ret; 14591c75950bSTrond Myklebust 146034901f70STrond Myklebust ret = nfs_writepages(mapping, wbc); 146161822ab5STrond Myklebust if (ret < 0) 146261822ab5STrond Myklebust goto out; 146334901f70STrond Myklebust ret = nfs_sync_mapping_wait(mapping, wbc, how); 1464ed90ef51STrond Myklebust if (ret < 0) 1465ed90ef51STrond Myklebust goto out; 14661c75950bSTrond Myklebust return 0; 146761822ab5STrond Myklebust out: 1468e507d9ebSTrond Myklebust __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); 14691c75950bSTrond Myklebust return ret; 14701c75950bSTrond Myklebust } 14711c75950bSTrond Myklebust 147234901f70STrond Myklebust /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */ 147334901f70STrond Myklebust static int nfs_write_mapping(struct address_space *mapping, int how) 147434901f70STrond Myklebust { 147534901f70STrond Myklebust struct writeback_control wbc = { 147634901f70STrond Myklebust .bdi = mapping->backing_dev_info, 147772cb77f4STrond Myklebust .sync_mode = WB_SYNC_ALL, 147834901f70STrond Myklebust .nr_to_write = LONG_MAX, 1479d7fb1207STrond Myklebust .range_start = 0, 1480d7fb1207STrond Myklebust .range_end = LLONG_MAX, 148134901f70STrond Myklebust .for_writepages = 1, 148234901f70STrond Myklebust }; 148334901f70STrond Myklebust 148434901f70STrond Myklebust return __nfs_write_mapping(mapping, &wbc, how); 148534901f70STrond Myklebust } 148634901f70STrond Myklebust 1487ed90ef51STrond Myklebust /* 1488ed90ef51STrond Myklebust * flush the inode to disk. 1489ed90ef51STrond Myklebust */ 1490ed90ef51STrond Myklebust int nfs_wb_all(struct inode *inode) 14911c75950bSTrond Myklebust { 1492ed90ef51STrond Myklebust return nfs_write_mapping(inode->i_mapping, 0); 1493ed90ef51STrond Myklebust } 14941c75950bSTrond Myklebust 1495ed90ef51STrond Myklebust int nfs_wb_nocommit(struct inode *inode) 1496ed90ef51STrond Myklebust { 1497ed90ef51STrond Myklebust return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT); 14981c75950bSTrond Myklebust } 14991c75950bSTrond Myklebust 15001b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page) 15011b3b4a1aSTrond Myklebust { 15021b3b4a1aSTrond Myklebust struct nfs_page *req; 15031b3b4a1aSTrond Myklebust loff_t range_start = page_offset(page); 15041b3b4a1aSTrond Myklebust loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); 15051b3b4a1aSTrond Myklebust struct writeback_control wbc = { 15061b3b4a1aSTrond Myklebust .bdi = page->mapping->backing_dev_info, 15071b3b4a1aSTrond Myklebust .sync_mode = WB_SYNC_ALL, 15081b3b4a1aSTrond Myklebust .nr_to_write = LONG_MAX, 15091b3b4a1aSTrond Myklebust .range_start = range_start, 15101b3b4a1aSTrond Myklebust .range_end = range_end, 15111b3b4a1aSTrond Myklebust }; 15121b3b4a1aSTrond Myklebust int ret = 0; 15131b3b4a1aSTrond Myklebust 15141b3b4a1aSTrond Myklebust BUG_ON(!PageLocked(page)); 15151b3b4a1aSTrond Myklebust for (;;) { 15161b3b4a1aSTrond Myklebust req = nfs_page_find_request(page); 15171b3b4a1aSTrond Myklebust if (req == NULL) 15181b3b4a1aSTrond Myklebust goto out; 1519e468bae9STrond Myklebust if (test_bit(PG_CLEAN, &req->wb_flags)) { 15201b3b4a1aSTrond Myklebust nfs_release_request(req); 15211b3b4a1aSTrond Myklebust break; 15221b3b4a1aSTrond Myklebust } 15231b3b4a1aSTrond Myklebust if (nfs_lock_request_dontget(req)) { 15241b3b4a1aSTrond Myklebust nfs_inode_remove_request(req); 15251b3b4a1aSTrond Myklebust /* 15261b3b4a1aSTrond Myklebust * In case nfs_inode_remove_request has marked the 15271b3b4a1aSTrond Myklebust * page as being dirty 15281b3b4a1aSTrond Myklebust */ 15291b3b4a1aSTrond Myklebust cancel_dirty_page(page, PAGE_CACHE_SIZE); 15301b3b4a1aSTrond Myklebust nfs_unlock_request(req); 15311b3b4a1aSTrond Myklebust break; 15321b3b4a1aSTrond Myklebust } 15331b3b4a1aSTrond Myklebust ret = nfs_wait_on_request(req); 15341b3b4a1aSTrond Myklebust if (ret < 0) 15351b3b4a1aSTrond Myklebust goto out; 15361b3b4a1aSTrond Myklebust } 15371b3b4a1aSTrond Myklebust if (!PagePrivate(page)) 15381b3b4a1aSTrond Myklebust return 0; 15391b3b4a1aSTrond Myklebust ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE); 15401b3b4a1aSTrond Myklebust out: 15411b3b4a1aSTrond Myklebust return ret; 15421b3b4a1aSTrond Myklebust } 15431b3b4a1aSTrond Myklebust 15445334eb13SAdrian Bunk static int nfs_wb_page_priority(struct inode *inode, struct page *page, 15455334eb13SAdrian Bunk int how) 15461c75950bSTrond Myklebust { 15471c75950bSTrond Myklebust loff_t range_start = page_offset(page); 15481c75950bSTrond Myklebust loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); 15494d770ccfSTrond Myklebust struct writeback_control wbc = { 15504d770ccfSTrond Myklebust .bdi = page->mapping->backing_dev_info, 15514d770ccfSTrond Myklebust .sync_mode = WB_SYNC_ALL, 15524d770ccfSTrond Myklebust .nr_to_write = LONG_MAX, 15534d770ccfSTrond Myklebust .range_start = range_start, 15544d770ccfSTrond Myklebust .range_end = range_end, 15554d770ccfSTrond Myklebust }; 15564d770ccfSTrond Myklebust int ret; 15571c75950bSTrond Myklebust 155873e3302fSTrond Myklebust do { 1559c63c7b05STrond Myklebust if (clear_page_dirty_for_io(page)) { 15604d770ccfSTrond Myklebust ret = nfs_writepage_locked(page, &wbc); 15614d770ccfSTrond Myklebust if (ret < 0) 156273e3302fSTrond Myklebust goto out_error; 156373e3302fSTrond Myklebust } else if (!PagePrivate(page)) 156473e3302fSTrond Myklebust break; 15654d770ccfSTrond Myklebust ret = nfs_sync_mapping_wait(page->mapping, &wbc, how); 156673e3302fSTrond Myklebust if (ret < 0) 156773e3302fSTrond Myklebust goto out_error; 156873e3302fSTrond Myklebust } while (PagePrivate(page)); 15694d770ccfSTrond Myklebust return 0; 157073e3302fSTrond Myklebust out_error: 1571e507d9ebSTrond Myklebust __mark_inode_dirty(inode, I_DIRTY_PAGES); 15724d770ccfSTrond Myklebust return ret; 15731c75950bSTrond Myklebust } 15741c75950bSTrond Myklebust 15751c75950bSTrond Myklebust /* 15761c75950bSTrond Myklebust * Write back all requests on one page - we do this before reading it. 15771c75950bSTrond Myklebust */ 15781c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page* page) 15791c75950bSTrond Myklebust { 15804d770ccfSTrond Myklebust return nfs_wb_page_priority(inode, page, FLUSH_STABLE); 15811c75950bSTrond Myklebust } 15821c75950bSTrond Myklebust 1583f7b422b1SDavid Howells int __init nfs_init_writepagecache(void) 15841da177e4SLinus Torvalds { 15851da177e4SLinus Torvalds nfs_wdata_cachep = kmem_cache_create("nfs_write_data", 15861da177e4SLinus Torvalds sizeof(struct nfs_write_data), 15871da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 158820c2df83SPaul Mundt NULL); 15891da177e4SLinus Torvalds if (nfs_wdata_cachep == NULL) 15901da177e4SLinus Torvalds return -ENOMEM; 15911da177e4SLinus Torvalds 159293d2341cSMatthew Dobson nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, 15931da177e4SLinus Torvalds nfs_wdata_cachep); 15941da177e4SLinus Torvalds if (nfs_wdata_mempool == NULL) 15951da177e4SLinus Torvalds return -ENOMEM; 15961da177e4SLinus Torvalds 159793d2341cSMatthew Dobson nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, 15981da177e4SLinus Torvalds nfs_wdata_cachep); 15991da177e4SLinus Torvalds if (nfs_commit_mempool == NULL) 16001da177e4SLinus Torvalds return -ENOMEM; 16011da177e4SLinus Torvalds 160289a09141SPeter Zijlstra /* 160389a09141SPeter Zijlstra * NFS congestion size, scale with available memory. 160489a09141SPeter Zijlstra * 160589a09141SPeter Zijlstra * 64MB: 8192k 160689a09141SPeter Zijlstra * 128MB: 11585k 160789a09141SPeter Zijlstra * 256MB: 16384k 160889a09141SPeter Zijlstra * 512MB: 23170k 160989a09141SPeter Zijlstra * 1GB: 32768k 161089a09141SPeter Zijlstra * 2GB: 46340k 161189a09141SPeter Zijlstra * 4GB: 65536k 161289a09141SPeter Zijlstra * 8GB: 92681k 161389a09141SPeter Zijlstra * 16GB: 131072k 161489a09141SPeter Zijlstra * 161589a09141SPeter Zijlstra * This allows larger machines to have larger/more transfers. 161689a09141SPeter Zijlstra * Limit the default to 256M 161789a09141SPeter Zijlstra */ 161889a09141SPeter Zijlstra nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); 161989a09141SPeter Zijlstra if (nfs_congestion_kb > 256*1024) 162089a09141SPeter Zijlstra nfs_congestion_kb = 256*1024; 162189a09141SPeter Zijlstra 16221da177e4SLinus Torvalds return 0; 16231da177e4SLinus Torvalds } 16241da177e4SLinus Torvalds 1625266bee88SDavid Brownell void nfs_destroy_writepagecache(void) 16261da177e4SLinus Torvalds { 16271da177e4SLinus Torvalds mempool_destroy(nfs_commit_mempool); 16281da177e4SLinus Torvalds mempool_destroy(nfs_wdata_mempool); 16291a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_wdata_cachep); 16301da177e4SLinus Torvalds } 16311da177e4SLinus Torvalds 1632