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) > 205*8aa7e847SJens Axboe NFS_CONGESTION_ON_THRESH) { 206*8aa7e847SJens Axboe set_bdi_congested(&nfss->backing_dev_info, 207*8aa7e847SJens Axboe BLK_RW_ASYNC); 208*8aa7e847SJens Axboe } 20989a09141SPeter Zijlstra } 2105a6d41b3STrond Myklebust return ret; 21189a09141SPeter Zijlstra } 21289a09141SPeter Zijlstra 21389a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page) 21489a09141SPeter Zijlstra { 21589a09141SPeter Zijlstra struct inode *inode = page->mapping->host; 21689a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 21789a09141SPeter Zijlstra 21889a09141SPeter Zijlstra end_page_writeback(page); 219c4dc4beeSPeter Zijlstra if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) 220*8aa7e847SJens Axboe clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC); 22189a09141SPeter Zijlstra } 22289a09141SPeter Zijlstra 22389a09141SPeter Zijlstra /* 224e261f51fSTrond Myklebust * Find an associated nfs write request, and prepare to flush it out 2259cccef95STrond Myklebust * May return an error if the user signalled nfs_wait_on_request(). 226e261f51fSTrond Myklebust */ 227c63c7b05STrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, 228c63c7b05STrond Myklebust struct page *page) 229e261f51fSTrond Myklebust { 230587142f8STrond Myklebust struct inode *inode = page->mapping->host; 231e261f51fSTrond Myklebust struct nfs_page *req; 232e261f51fSTrond Myklebust int ret; 233e261f51fSTrond Myklebust 234587142f8STrond Myklebust spin_lock(&inode->i_lock); 235e261f51fSTrond Myklebust for(;;) { 236e261f51fSTrond Myklebust req = nfs_page_find_request_locked(page); 237e261f51fSTrond Myklebust if (req == NULL) { 238587142f8STrond Myklebust spin_unlock(&inode->i_lock); 2399cccef95STrond Myklebust return 0; 240e261f51fSTrond Myklebust } 241acee478aSTrond Myklebust if (nfs_set_page_tag_locked(req)) 242e261f51fSTrond Myklebust break; 243e261f51fSTrond Myklebust /* Note: If we hold the page lock, as is the case in nfs_writepage, 244acee478aSTrond Myklebust * then the call to nfs_set_page_tag_locked() will always 245e261f51fSTrond Myklebust * succeed provided that someone hasn't already marked the 246e261f51fSTrond Myklebust * request as dirty (in which case we don't care). 247e261f51fSTrond Myklebust */ 248587142f8STrond Myklebust spin_unlock(&inode->i_lock); 249e261f51fSTrond Myklebust ret = nfs_wait_on_request(req); 250e261f51fSTrond Myklebust nfs_release_request(req); 251e261f51fSTrond Myklebust if (ret != 0) 252e261f51fSTrond Myklebust return ret; 253587142f8STrond Myklebust spin_lock(&inode->i_lock); 254e261f51fSTrond Myklebust } 255e468bae9STrond Myklebust if (test_bit(PG_CLEAN, &req->wb_flags)) { 256587142f8STrond Myklebust spin_unlock(&inode->i_lock); 257e468bae9STrond Myklebust BUG(); 258612c9384STrond Myklebust } 259c63c7b05STrond Myklebust if (nfs_set_page_writeback(page) != 0) { 260587142f8STrond Myklebust spin_unlock(&inode->i_lock); 261c63c7b05STrond Myklebust BUG(); 262c63c7b05STrond Myklebust } 263587142f8STrond Myklebust spin_unlock(&inode->i_lock); 264f8512ad0SFred Isaman if (!nfs_pageio_add_request(pgio, req)) { 265f8512ad0SFred Isaman nfs_redirty_request(req); 266f8512ad0SFred Isaman return pgio->pg_error; 267f8512ad0SFred Isaman } 2689cccef95STrond Myklebust return 0; 269e261f51fSTrond Myklebust } 270e261f51fSTrond Myklebust 271f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio) 272f758c885STrond Myklebust { 273f758c885STrond Myklebust struct inode *inode = page->mapping->host; 274f758c885STrond Myklebust 275f758c885STrond Myklebust nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); 276f758c885STrond Myklebust nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); 277f758c885STrond Myklebust 278f758c885STrond Myklebust nfs_pageio_cond_complete(pgio, page->index); 279f758c885STrond Myklebust return nfs_page_async_flush(pgio, page); 280f758c885STrond Myklebust } 281f758c885STrond Myklebust 282e261f51fSTrond Myklebust /* 2831da177e4SLinus Torvalds * Write an mmapped page to the server. 2841da177e4SLinus Torvalds */ 2854d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc) 2861da177e4SLinus Torvalds { 287f758c885STrond Myklebust struct nfs_pageio_descriptor pgio; 288e261f51fSTrond Myklebust int err; 2891da177e4SLinus Torvalds 290f758c885STrond Myklebust nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc)); 291f758c885STrond Myklebust err = nfs_do_writepage(page, wbc, &pgio); 292f758c885STrond Myklebust nfs_pageio_complete(&pgio); 293f758c885STrond Myklebust if (err < 0) 2944d770ccfSTrond Myklebust return err; 295f758c885STrond Myklebust if (pgio.pg_error < 0) 296f758c885STrond Myklebust return pgio.pg_error; 297f758c885STrond Myklebust return 0; 2984d770ccfSTrond Myklebust } 2994d770ccfSTrond Myklebust 3004d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc) 3014d770ccfSTrond Myklebust { 302f758c885STrond Myklebust int ret; 3034d770ccfSTrond Myklebust 304f758c885STrond Myklebust ret = nfs_writepage_locked(page, wbc); 3051da177e4SLinus Torvalds unlock_page(page); 306f758c885STrond Myklebust return ret; 307f758c885STrond Myklebust } 308f758c885STrond Myklebust 309f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) 310f758c885STrond Myklebust { 311f758c885STrond Myklebust int ret; 312f758c885STrond Myklebust 313f758c885STrond Myklebust ret = nfs_do_writepage(page, wbc, data); 314f758c885STrond Myklebust unlock_page(page); 315f758c885STrond Myklebust return ret; 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) 3191da177e4SLinus Torvalds { 3201da177e4SLinus Torvalds struct inode *inode = mapping->host; 32172cb77f4STrond Myklebust unsigned long *bitlock = &NFS_I(inode)->flags; 322c63c7b05STrond Myklebust struct nfs_pageio_descriptor pgio; 3231da177e4SLinus Torvalds int err; 3241da177e4SLinus Torvalds 32572cb77f4STrond Myklebust /* Stop dirtying of new pages while we sync */ 32672cb77f4STrond Myklebust err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING, 32772cb77f4STrond Myklebust nfs_wait_bit_killable, TASK_KILLABLE); 32872cb77f4STrond Myklebust if (err) 32972cb77f4STrond Myklebust goto out_err; 33072cb77f4STrond Myklebust 33191d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); 33291d5b470SChuck Lever 333c63c7b05STrond Myklebust nfs_pageio_init_write(&pgio, inode, wb_priority(wbc)); 334f758c885STrond Myklebust err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); 335c63c7b05STrond Myklebust nfs_pageio_complete(&pgio); 33672cb77f4STrond Myklebust 33772cb77f4STrond Myklebust clear_bit_unlock(NFS_INO_FLUSHING, bitlock); 33872cb77f4STrond Myklebust smp_mb__after_clear_bit(); 33972cb77f4STrond Myklebust wake_up_bit(bitlock, NFS_INO_FLUSHING); 34072cb77f4STrond Myklebust 341f758c885STrond Myklebust if (err < 0) 34272cb77f4STrond Myklebust goto out_err; 34372cb77f4STrond Myklebust err = pgio.pg_error; 34472cb77f4STrond Myklebust if (err < 0) 34572cb77f4STrond Myklebust goto out_err; 346c63c7b05STrond Myklebust return 0; 34772cb77f4STrond Myklebust out_err: 34872cb77f4STrond Myklebust return err; 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds /* 3521da177e4SLinus Torvalds * Insert a write request into an inode 3531da177e4SLinus Torvalds */ 354e7d39069STrond Myklebust static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req) 3551da177e4SLinus Torvalds { 3561da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 3571da177e4SLinus Torvalds int error; 3581da177e4SLinus Torvalds 359e7d39069STrond Myklebust error = radix_tree_preload(GFP_NOFS); 360e7d39069STrond Myklebust if (error != 0) 361e7d39069STrond Myklebust goto out; 362e7d39069STrond Myklebust 363e7d39069STrond Myklebust /* Lock the request! */ 364e7d39069STrond Myklebust nfs_lock_request_dontget(req); 365e7d39069STrond Myklebust 366e7d39069STrond Myklebust spin_lock(&inode->i_lock); 3671da177e4SLinus Torvalds error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req); 36827852596SNick Piggin BUG_ON(error); 3691da177e4SLinus Torvalds if (!nfsi->npages) { 3701da177e4SLinus Torvalds igrab(inode); 3711da177e4SLinus Torvalds if (nfs_have_delegation(inode, FMODE_WRITE)) 3721da177e4SLinus Torvalds nfsi->change_attr++; 3731da177e4SLinus Torvalds } 374deb7d638STrond Myklebust SetPagePrivate(req->wb_page); 375277459d2STrond Myklebust set_page_private(req->wb_page, (unsigned long)req); 3761da177e4SLinus Torvalds nfsi->npages++; 377c03b4024STrond Myklebust kref_get(&req->wb_kref); 37827852596SNick Piggin radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, 37927852596SNick Piggin NFS_PAGE_TAG_LOCKED); 380e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 381e7d39069STrond Myklebust radix_tree_preload_end(); 382e7d39069STrond Myklebust out: 383e7d39069STrond Myklebust return error; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds /* 38789a09141SPeter Zijlstra * Remove a write request from an inode 3881da177e4SLinus Torvalds */ 3891da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req) 3901da177e4SLinus Torvalds { 39188be9f99STrond Myklebust struct inode *inode = req->wb_context->path.dentry->d_inode; 3921da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds BUG_ON (!NFS_WBACK_BUSY(req)); 3951da177e4SLinus Torvalds 396587142f8STrond Myklebust spin_lock(&inode->i_lock); 397277459d2STrond Myklebust set_page_private(req->wb_page, 0); 398deb7d638STrond Myklebust ClearPagePrivate(req->wb_page); 3991da177e4SLinus Torvalds radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index); 4001da177e4SLinus Torvalds nfsi->npages--; 4011da177e4SLinus Torvalds if (!nfsi->npages) { 402587142f8STrond Myklebust spin_unlock(&inode->i_lock); 4031da177e4SLinus Torvalds iput(inode); 4041da177e4SLinus Torvalds } else 405587142f8STrond Myklebust spin_unlock(&inode->i_lock); 4061da177e4SLinus Torvalds nfs_clear_request(req); 4071da177e4SLinus Torvalds nfs_release_request(req); 4081da177e4SLinus Torvalds } 4091da177e4SLinus Torvalds 41061822ab5STrond Myklebust static void 4116d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req) 41261822ab5STrond Myklebust { 41361822ab5STrond Myklebust __set_page_dirty_nobuffers(req->wb_page); 41461822ab5STrond Myklebust } 41561822ab5STrond Myklebust 4161da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 4171da177e4SLinus Torvalds /* 4181da177e4SLinus Torvalds * Add a request to the inode's commit list. 4191da177e4SLinus Torvalds */ 4201da177e4SLinus Torvalds static void 4211da177e4SLinus Torvalds nfs_mark_request_commit(struct nfs_page *req) 4221da177e4SLinus Torvalds { 42388be9f99STrond Myklebust struct inode *inode = req->wb_context->path.dentry->d_inode; 4241da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 4251da177e4SLinus Torvalds 426587142f8STrond Myklebust spin_lock(&inode->i_lock); 427e468bae9STrond Myklebust set_bit(PG_CLEAN, &(req)->wb_flags); 4285c369683STrond Myklebust radix_tree_tag_set(&nfsi->nfs_page_tree, 4295c369683STrond Myklebust req->wb_index, 4305c369683STrond Myklebust NFS_PAGE_TAG_COMMIT); 431587142f8STrond Myklebust spin_unlock(&inode->i_lock); 432fd39fc85SChristoph Lameter inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 433c9e51e41SPeter Zijlstra inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE); 434a1803044STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 4351da177e4SLinus Torvalds } 4368e821cadSTrond Myklebust 437e468bae9STrond Myklebust static int 438e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req) 439e468bae9STrond Myklebust { 440e468bae9STrond Myklebust struct page *page = req->wb_page; 441e468bae9STrond Myklebust 442e468bae9STrond Myklebust if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) { 443e468bae9STrond Myklebust dec_zone_page_state(page, NR_UNSTABLE_NFS); 444e468bae9STrond Myklebust dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE); 445e468bae9STrond Myklebust return 1; 446e468bae9STrond Myklebust } 447e468bae9STrond Myklebust return 0; 448e468bae9STrond Myklebust } 449e468bae9STrond Myklebust 4508e821cadSTrond Myklebust static inline 4518e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 4528e821cadSTrond Myklebust { 4538e821cadSTrond Myklebust return data->verf.committed != NFS_FILE_SYNC; 4548e821cadSTrond Myklebust } 4558e821cadSTrond Myklebust 4568e821cadSTrond Myklebust static inline 4578e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req) 4588e821cadSTrond Myklebust { 459e468bae9STrond Myklebust if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) { 4608e821cadSTrond Myklebust nfs_mark_request_commit(req); 4618e821cadSTrond Myklebust return 1; 4628e821cadSTrond Myklebust } 4638e821cadSTrond Myklebust if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) { 4646d884e8fSFred nfs_mark_request_dirty(req); 4658e821cadSTrond Myklebust return 1; 4668e821cadSTrond Myklebust } 4678e821cadSTrond Myklebust return 0; 4688e821cadSTrond Myklebust } 4698e821cadSTrond Myklebust #else 4708e821cadSTrond Myklebust static inline void 4718e821cadSTrond Myklebust nfs_mark_request_commit(struct nfs_page *req) 4728e821cadSTrond Myklebust { 4738e821cadSTrond Myklebust } 4748e821cadSTrond Myklebust 475e468bae9STrond Myklebust static inline int 476e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req) 477e468bae9STrond Myklebust { 478e468bae9STrond Myklebust return 0; 479e468bae9STrond Myklebust } 480e468bae9STrond Myklebust 4818e821cadSTrond Myklebust static inline 4828e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 4838e821cadSTrond Myklebust { 4848e821cadSTrond Myklebust return 0; 4858e821cadSTrond Myklebust } 4868e821cadSTrond Myklebust 4878e821cadSTrond Myklebust static inline 4888e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req) 4898e821cadSTrond Myklebust { 4908e821cadSTrond Myklebust return 0; 4918e821cadSTrond Myklebust } 4921da177e4SLinus Torvalds #endif 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds /* 4951da177e4SLinus Torvalds * Wait for a request to complete. 4961da177e4SLinus Torvalds * 497150030b7SMatthew Wilcox * Interruptible by fatal signals only. 4981da177e4SLinus Torvalds */ 499ca52fec1STrond Myklebust static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages) 5001da177e4SLinus Torvalds { 5011da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 5021da177e4SLinus Torvalds struct nfs_page *req; 503ca52fec1STrond Myklebust pgoff_t idx_end, next; 5041da177e4SLinus Torvalds unsigned int res = 0; 5051da177e4SLinus Torvalds int error; 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds if (npages == 0) 5081da177e4SLinus Torvalds idx_end = ~0; 5091da177e4SLinus Torvalds else 5101da177e4SLinus Torvalds idx_end = idx_start + npages - 1; 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds next = idx_start; 5139fd367f0STrond Myklebust while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) { 5141da177e4SLinus Torvalds if (req->wb_index > idx_end) 5151da177e4SLinus Torvalds break; 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds next = req->wb_index + 1; 518c6a556b8STrond Myklebust BUG_ON(!NFS_WBACK_BUSY(req)); 5191da177e4SLinus Torvalds 520c03b4024STrond Myklebust kref_get(&req->wb_kref); 521587142f8STrond Myklebust spin_unlock(&inode->i_lock); 5221da177e4SLinus Torvalds error = nfs_wait_on_request(req); 5231da177e4SLinus Torvalds nfs_release_request(req); 524587142f8STrond Myklebust spin_lock(&inode->i_lock); 5251da177e4SLinus Torvalds if (error < 0) 5261da177e4SLinus Torvalds return error; 5271da177e4SLinus Torvalds res++; 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds return res; 5301da177e4SLinus Torvalds } 5311da177e4SLinus Torvalds 53283715ad5STrond Myklebust static void nfs_cancel_commit_list(struct list_head *head) 53383715ad5STrond Myklebust { 53483715ad5STrond Myklebust struct nfs_page *req; 53583715ad5STrond Myklebust 53683715ad5STrond Myklebust while(!list_empty(head)) { 53783715ad5STrond Myklebust req = nfs_list_entry(head->next); 53883715ad5STrond Myklebust nfs_list_remove_request(req); 539e468bae9STrond Myklebust nfs_clear_request_commit(req); 54083715ad5STrond Myklebust nfs_inode_remove_request(req); 541b6dff26aSTrond Myklebust nfs_unlock_request(req); 54283715ad5STrond Myklebust } 54383715ad5STrond Myklebust } 54483715ad5STrond Myklebust 54547c62564STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 546fb8a1f11STrond Myklebust static int 547fb8a1f11STrond Myklebust nfs_need_commit(struct nfs_inode *nfsi) 548fb8a1f11STrond Myklebust { 549fb8a1f11STrond Myklebust return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT); 550fb8a1f11STrond Myklebust } 551fb8a1f11STrond Myklebust 5521da177e4SLinus Torvalds /* 5531da177e4SLinus Torvalds * nfs_scan_commit - Scan an inode for commit requests 5541da177e4SLinus Torvalds * @inode: NFS inode to scan 5551da177e4SLinus Torvalds * @dst: destination list 5561da177e4SLinus Torvalds * @idx_start: lower bound of page->index to scan. 5571da177e4SLinus Torvalds * @npages: idx_start + npages sets the upper bound to scan. 5581da177e4SLinus Torvalds * 5591da177e4SLinus Torvalds * Moves requests from the inode's 'commit' request list. 5601da177e4SLinus Torvalds * The requests are *not* checked to ensure that they form a contiguous set. 5611da177e4SLinus Torvalds */ 5621da177e4SLinus Torvalds static int 563ca52fec1STrond Myklebust nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages) 5641da177e4SLinus Torvalds { 5651da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 5663da28eb1STrond Myklebust 567fb8a1f11STrond Myklebust if (!nfs_need_commit(nfsi)) 568fb8a1f11STrond Myklebust return 0; 569fb8a1f11STrond Myklebust 570fb8a1f11STrond Myklebust return nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT); 5711da177e4SLinus Torvalds } 572c42de9ddSTrond Myklebust #else 573fb8a1f11STrond Myklebust static inline int nfs_need_commit(struct nfs_inode *nfsi) 574fb8a1f11STrond Myklebust { 575fb8a1f11STrond Myklebust return 0; 576fb8a1f11STrond Myklebust } 577fb8a1f11STrond Myklebust 578ca52fec1STrond Myklebust static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages) 579c42de9ddSTrond Myklebust { 580c42de9ddSTrond Myklebust return 0; 581c42de9ddSTrond Myklebust } 5821da177e4SLinus Torvalds #endif 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds /* 585e7d39069STrond Myklebust * Search for an existing write request, and attempt to update 586e7d39069STrond Myklebust * it to reflect a new dirty region on a given page. 5871da177e4SLinus Torvalds * 588e7d39069STrond Myklebust * If the attempt fails, then the existing request is flushed out 589e7d39069STrond Myklebust * to disk. 5901da177e4SLinus Torvalds */ 591e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode, 592e7d39069STrond Myklebust struct page *page, 593e7d39069STrond Myklebust unsigned int offset, 594e7d39069STrond Myklebust unsigned int bytes) 5951da177e4SLinus Torvalds { 596e7d39069STrond Myklebust struct nfs_page *req; 597e7d39069STrond Myklebust unsigned int rqend; 598e7d39069STrond Myklebust unsigned int end; 5991da177e4SLinus Torvalds int error; 600277459d2STrond Myklebust 601e7d39069STrond Myklebust if (!PagePrivate(page)) 602e7d39069STrond Myklebust return NULL; 603e7d39069STrond Myklebust 604e7d39069STrond Myklebust end = offset + bytes; 605e7d39069STrond Myklebust spin_lock(&inode->i_lock); 606e7d39069STrond Myklebust 607e7d39069STrond Myklebust for (;;) { 608e7d39069STrond Myklebust req = nfs_page_find_request_locked(page); 609e7d39069STrond Myklebust if (req == NULL) 610e7d39069STrond Myklebust goto out_unlock; 611e7d39069STrond Myklebust 612e7d39069STrond Myklebust rqend = req->wb_offset + req->wb_bytes; 613e7d39069STrond Myklebust /* 614e7d39069STrond Myklebust * Tell the caller to flush out the request if 615e7d39069STrond Myklebust * the offsets are non-contiguous. 616e7d39069STrond Myklebust * Note: nfs_flush_incompatible() will already 617e7d39069STrond Myklebust * have flushed out requests having wrong owners. 618e7d39069STrond Myklebust */ 619e468bae9STrond Myklebust if (offset > rqend 620e7d39069STrond Myklebust || end < req->wb_offset) 621e7d39069STrond Myklebust goto out_flushme; 622e7d39069STrond Myklebust 623e7d39069STrond Myklebust if (nfs_set_page_tag_locked(req)) 624e7d39069STrond Myklebust break; 625e7d39069STrond Myklebust 626e7d39069STrond Myklebust /* The request is locked, so wait and then retry */ 627587142f8STrond Myklebust spin_unlock(&inode->i_lock); 6281da177e4SLinus Torvalds error = nfs_wait_on_request(req); 6291da177e4SLinus Torvalds nfs_release_request(req); 630e7d39069STrond Myklebust if (error != 0) 631e7d39069STrond Myklebust goto out_err; 632e7d39069STrond Myklebust spin_lock(&inode->i_lock); 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds 635e468bae9STrond Myklebust if (nfs_clear_request_commit(req)) 636e468bae9STrond Myklebust radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree, 637e468bae9STrond Myklebust req->wb_index, NFS_PAGE_TAG_COMMIT); 638e468bae9STrond Myklebust 6391da177e4SLinus Torvalds /* Okay, the request matches. Update the region */ 6401da177e4SLinus Torvalds if (offset < req->wb_offset) { 6411da177e4SLinus Torvalds req->wb_offset = offset; 6421da177e4SLinus Torvalds req->wb_pgbase = offset; 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds if (end > rqend) 6451da177e4SLinus Torvalds req->wb_bytes = end - req->wb_offset; 646e7d39069STrond Myklebust else 647e7d39069STrond Myklebust req->wb_bytes = rqend - req->wb_offset; 648e7d39069STrond Myklebust out_unlock: 649e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 650e7d39069STrond Myklebust return req; 651e7d39069STrond Myklebust out_flushme: 652e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 653e7d39069STrond Myklebust nfs_release_request(req); 654e7d39069STrond Myklebust error = nfs_wb_page(inode, page); 655e7d39069STrond Myklebust out_err: 656e7d39069STrond Myklebust return ERR_PTR(error); 657e7d39069STrond Myklebust } 6581da177e4SLinus Torvalds 659e7d39069STrond Myklebust /* 660e7d39069STrond Myklebust * Try to update an existing write request, or create one if there is none. 661e7d39069STrond Myklebust * 662e7d39069STrond Myklebust * Note: Should always be called with the Page Lock held to prevent races 663e7d39069STrond Myklebust * if we have to add a new request. Also assumes that the caller has 664e7d39069STrond Myklebust * already called nfs_flush_incompatible() if necessary. 665e7d39069STrond Myklebust */ 666e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, 667e7d39069STrond Myklebust struct page *page, unsigned int offset, unsigned int bytes) 668e7d39069STrond Myklebust { 669e7d39069STrond Myklebust struct inode *inode = page->mapping->host; 670e7d39069STrond Myklebust struct nfs_page *req; 671e7d39069STrond Myklebust int error; 672e7d39069STrond Myklebust 673e7d39069STrond Myklebust req = nfs_try_to_update_request(inode, page, offset, bytes); 674e7d39069STrond Myklebust if (req != NULL) 675e7d39069STrond Myklebust goto out; 676e7d39069STrond Myklebust req = nfs_create_request(ctx, inode, page, offset, bytes); 677e7d39069STrond Myklebust if (IS_ERR(req)) 678e7d39069STrond Myklebust goto out; 679e7d39069STrond Myklebust error = nfs_inode_add_request(inode, req); 680e7d39069STrond Myklebust if (error != 0) { 681e7d39069STrond Myklebust nfs_release_request(req); 682e7d39069STrond Myklebust req = ERR_PTR(error); 683e7d39069STrond Myklebust } 684efc91ed0STrond Myklebust out: 68561e930a9STrond Myklebust return req; 6861da177e4SLinus Torvalds } 6871da177e4SLinus Torvalds 688e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, 689e7d39069STrond Myklebust unsigned int offset, unsigned int count) 690e7d39069STrond Myklebust { 691e7d39069STrond Myklebust struct nfs_page *req; 692e7d39069STrond Myklebust 693e7d39069STrond Myklebust req = nfs_setup_write_request(ctx, page, offset, count); 694e7d39069STrond Myklebust if (IS_ERR(req)) 695e7d39069STrond Myklebust return PTR_ERR(req); 696e7d39069STrond Myklebust /* Update file length */ 697e7d39069STrond Myklebust nfs_grow_file(page, offset, count); 698e7d39069STrond Myklebust nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); 699e7d39069STrond Myklebust nfs_clear_page_tag_locked(req); 700e7d39069STrond Myklebust return 0; 701e7d39069STrond Myklebust } 702e7d39069STrond Myklebust 7031da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page) 7041da177e4SLinus Torvalds { 705cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 7061da177e4SLinus Torvalds struct nfs_page *req; 7071a54533eSTrond Myklebust int do_flush, status; 7081da177e4SLinus Torvalds /* 7091da177e4SLinus Torvalds * Look for a request corresponding to this page. If there 7101da177e4SLinus Torvalds * is one, and it belongs to another file, we flush it out 7111da177e4SLinus Torvalds * before we try to copy anything into the page. Do this 7121da177e4SLinus Torvalds * due to the lack of an ACCESS-type call in NFSv2. 7131da177e4SLinus Torvalds * Also do the same if we find a request from an existing 7141da177e4SLinus Torvalds * dropped page. 7151da177e4SLinus Torvalds */ 7161a54533eSTrond Myklebust do { 717277459d2STrond Myklebust req = nfs_page_find_request(page); 7181a54533eSTrond Myklebust if (req == NULL) 7191a54533eSTrond Myklebust return 0; 720e468bae9STrond Myklebust do_flush = req->wb_page != page || req->wb_context != ctx; 7211da177e4SLinus Torvalds nfs_release_request(req); 7221a54533eSTrond Myklebust if (!do_flush) 7231a54533eSTrond Myklebust return 0; 724277459d2STrond Myklebust status = nfs_wb_page(page->mapping->host, page); 7251a54533eSTrond Myklebust } while (status == 0); 7261a54533eSTrond Myklebust return status; 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds /* 7305d47a356STrond Myklebust * If the page cache is marked as unsafe or invalid, then we can't rely on 7315d47a356STrond Myklebust * the PageUptodate() flag. In this case, we will need to turn off 7325d47a356STrond Myklebust * write optimisations that depend on the page contents being correct. 7335d47a356STrond Myklebust */ 7345d47a356STrond Myklebust static int nfs_write_pageuptodate(struct page *page, struct inode *inode) 7355d47a356STrond Myklebust { 7365d47a356STrond Myklebust return PageUptodate(page) && 7375d47a356STrond Myklebust !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA)); 7385d47a356STrond Myklebust } 7395d47a356STrond Myklebust 7405d47a356STrond Myklebust /* 7411da177e4SLinus Torvalds * Update and possibly write a cached page of an NFS file. 7421da177e4SLinus Torvalds * 7431da177e4SLinus Torvalds * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad 7441da177e4SLinus Torvalds * things with a page scheduled for an RPC call (e.g. invalidate it). 7451da177e4SLinus Torvalds */ 7461da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page, 7471da177e4SLinus Torvalds unsigned int offset, unsigned int count) 7481da177e4SLinus Torvalds { 749cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 7501da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 7511da177e4SLinus Torvalds int status = 0; 7521da177e4SLinus Torvalds 75391d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 75491d5b470SChuck Lever 75548186c7dSChuck Lever dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n", 75601cce933SJosef "Jeff" Sipek file->f_path.dentry->d_parent->d_name.name, 75701cce933SJosef "Jeff" Sipek file->f_path.dentry->d_name.name, count, 7580bbacc40SChuck Lever (long long)(page_offset(page) + offset)); 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds /* If we're not using byte range locks, and we know the page 7615d47a356STrond Myklebust * is up to date, it may be more efficient to extend the write 7625d47a356STrond Myklebust * to cover the entire page in order to avoid fragmentation 7635d47a356STrond Myklebust * inefficiencies. 7641da177e4SLinus Torvalds */ 7655d47a356STrond Myklebust if (nfs_write_pageuptodate(page, inode) && 7665d47a356STrond Myklebust inode->i_flock == NULL && 7674b5621f6STrond Myklebust !(file->f_flags & O_SYNC)) { 76849a70f27STrond Myklebust count = max(count + offset, nfs_page_length(page)); 7691da177e4SLinus Torvalds offset = 0; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds 772e21195a7STrond Myklebust status = nfs_writepage_setup(ctx, page, offset, count); 77303fa9e84STrond Myklebust if (status < 0) 77403fa9e84STrond Myklebust nfs_set_pageerror(page); 77503fa9e84STrond Myklebust else 776e261f51fSTrond Myklebust __set_page_dirty_nobuffers(page); 7771da177e4SLinus Torvalds 77848186c7dSChuck Lever dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", 7791da177e4SLinus Torvalds status, (long long)i_size_read(inode)); 7801da177e4SLinus Torvalds return status; 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds static void nfs_writepage_release(struct nfs_page *req) 7841da177e4SLinus Torvalds { 7858e821cadSTrond Myklebust 7867e5f6146STrond Myklebust if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) { 78789a09141SPeter Zijlstra nfs_end_page_writeback(req->wb_page); 7881da177e4SLinus Torvalds nfs_inode_remove_request(req); 7898e821cadSTrond Myklebust } else 7908e821cadSTrond Myklebust nfs_end_page_writeback(req->wb_page); 7919fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 7921da177e4SLinus Torvalds } 7931da177e4SLinus Torvalds 7943ff7576dSTrond Myklebust static int flush_task_priority(int how) 7951da177e4SLinus Torvalds { 7961da177e4SLinus Torvalds switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { 7971da177e4SLinus Torvalds case FLUSH_HIGHPRI: 7981da177e4SLinus Torvalds return RPC_PRIORITY_HIGH; 7991da177e4SLinus Torvalds case FLUSH_LOWPRI: 8001da177e4SLinus Torvalds return RPC_PRIORITY_LOW; 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds return RPC_PRIORITY_NORMAL; 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds /* 8061da177e4SLinus Torvalds * Set up the argument/result storage required for the RPC call. 8071da177e4SLinus Torvalds */ 808dbae4c73STrond Myklebust static int nfs_write_rpcsetup(struct nfs_page *req, 8091da177e4SLinus Torvalds struct nfs_write_data *data, 810788e7a89STrond Myklebust const struct rpc_call_ops *call_ops, 8111da177e4SLinus Torvalds unsigned int count, unsigned int offset, 8121da177e4SLinus Torvalds int how) 8131da177e4SLinus Torvalds { 81484115e1cSTrond Myklebust struct inode *inode = req->wb_context->path.dentry->d_inode; 81584115e1cSTrond Myklebust int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC; 8163ff7576dSTrond Myklebust int priority = flush_task_priority(how); 81707737691STrond Myklebust struct rpc_task *task; 818bdc7f021STrond Myklebust struct rpc_message msg = { 819bdc7f021STrond Myklebust .rpc_argp = &data->args, 820bdc7f021STrond Myklebust .rpc_resp = &data->res, 821bdc7f021STrond Myklebust .rpc_cred = req->wb_context->cred, 822bdc7f021STrond Myklebust }; 82384115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 82484115e1cSTrond Myklebust .rpc_client = NFS_CLIENT(inode), 82507737691STrond Myklebust .task = &data->task, 826bdc7f021STrond Myklebust .rpc_message = &msg, 82784115e1cSTrond Myklebust .callback_ops = call_ops, 82884115e1cSTrond Myklebust .callback_data = data, 829101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 83084115e1cSTrond Myklebust .flags = flags, 8313ff7576dSTrond Myklebust .priority = priority, 83284115e1cSTrond Myklebust }; 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds /* Set up the RPC argument and reply structs 8351da177e4SLinus Torvalds * NB: take care not to mess about with data->commit et al. */ 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds data->req = req; 83888be9f99STrond Myklebust data->inode = inode = req->wb_context->path.dentry->d_inode; 839bdc7f021STrond Myklebust data->cred = msg.rpc_cred; 8401da177e4SLinus Torvalds 8411da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 8421da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 8431da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 8441da177e4SLinus Torvalds data->args.pages = data->pagevec; 8451da177e4SLinus Torvalds data->args.count = count; 846383ba719STrond Myklebust data->args.context = get_nfs_open_context(req->wb_context); 847bdc7f021STrond Myklebust data->args.stable = NFS_UNSTABLE; 848bdc7f021STrond Myklebust if (how & FLUSH_STABLE) { 849bdc7f021STrond Myklebust data->args.stable = NFS_DATA_SYNC; 850fb8a1f11STrond Myklebust if (!nfs_need_commit(NFS_I(inode))) 851bdc7f021STrond Myklebust data->args.stable = NFS_FILE_SYNC; 852bdc7f021STrond Myklebust } 8531da177e4SLinus Torvalds 8541da177e4SLinus Torvalds data->res.fattr = &data->fattr; 8551da177e4SLinus Torvalds data->res.count = count; 8561da177e4SLinus Torvalds data->res.verf = &data->verf; 8570e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 8581da177e4SLinus Torvalds 859788e7a89STrond Myklebust /* Set up the initial task struct. */ 860bdc7f021STrond Myklebust NFS_PROTO(inode)->write_setup(data, &msg); 8611da177e4SLinus Torvalds 862a3f565b1SChuck Lever dprintk("NFS: %5u initiated write call " 86348186c7dSChuck Lever "(req %s/%lld, %u bytes @ offset %llu)\n", 8640bbacc40SChuck Lever data->task.tk_pid, 8651da177e4SLinus Torvalds inode->i_sb->s_id, 8661da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 8671da177e4SLinus Torvalds count, 8681da177e4SLinus Torvalds (unsigned long long)data->args.offset); 8691da177e4SLinus Torvalds 87007737691STrond Myklebust task = rpc_run_task(&task_setup_data); 871dbae4c73STrond Myklebust if (IS_ERR(task)) 872dbae4c73STrond Myklebust return PTR_ERR(task); 87307737691STrond Myklebust rpc_put_task(task); 874dbae4c73STrond Myklebust return 0; 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds 8776d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and 8786d884e8fSFred * call this on each, which will prepare them to be retried on next 8796d884e8fSFred * writeback using standard nfs. 8806d884e8fSFred */ 8816d884e8fSFred static void nfs_redirty_request(struct nfs_page *req) 8826d884e8fSFred { 8836d884e8fSFred nfs_mark_request_dirty(req); 8846d884e8fSFred nfs_end_page_writeback(req->wb_page); 8856d884e8fSFred nfs_clear_page_tag_locked(req); 8866d884e8fSFred } 8876d884e8fSFred 8881da177e4SLinus Torvalds /* 8891da177e4SLinus Torvalds * Generate multiple small requests to write out a single 8901da177e4SLinus Torvalds * contiguous dirty area on one page. 8911da177e4SLinus Torvalds */ 8928d5658c9STrond Myklebust static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how) 8931da177e4SLinus Torvalds { 8941da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(head->next); 8951da177e4SLinus Torvalds struct page *page = req->wb_page; 8961da177e4SLinus Torvalds struct nfs_write_data *data; 897e9f7bee1STrond Myklebust size_t wsize = NFS_SERVER(inode)->wsize, nbytes; 898e9f7bee1STrond Myklebust unsigned int offset; 8991da177e4SLinus Torvalds int requests = 0; 900dbae4c73STrond Myklebust int ret = 0; 9011da177e4SLinus Torvalds LIST_HEAD(list); 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds nfs_list_remove_request(req); 9041da177e4SLinus Torvalds 905bcb71bbaSTrond Myklebust nbytes = count; 906e9f7bee1STrond Myklebust do { 907e9f7bee1STrond Myklebust size_t len = min(nbytes, wsize); 908e9f7bee1STrond Myklebust 9098d5658c9STrond Myklebust data = nfs_writedata_alloc(1); 9101da177e4SLinus Torvalds if (!data) 9111da177e4SLinus Torvalds goto out_bad; 9121da177e4SLinus Torvalds list_add(&data->pages, &list); 9131da177e4SLinus Torvalds requests++; 914e9f7bee1STrond Myklebust nbytes -= len; 915e9f7bee1STrond Myklebust } while (nbytes != 0); 9161da177e4SLinus Torvalds atomic_set(&req->wb_complete, requests); 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds ClearPageError(page); 9191da177e4SLinus Torvalds offset = 0; 920bcb71bbaSTrond Myklebust nbytes = count; 9211da177e4SLinus Torvalds do { 922dbae4c73STrond Myklebust int ret2; 923dbae4c73STrond Myklebust 9241da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_write_data, pages); 9251da177e4SLinus Torvalds list_del_init(&data->pages); 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds data->pagevec[0] = page; 9281da177e4SLinus Torvalds 929bcb71bbaSTrond Myklebust if (nbytes < wsize) 930bcb71bbaSTrond Myklebust wsize = nbytes; 931dbae4c73STrond Myklebust ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops, 932788e7a89STrond Myklebust wsize, offset, how); 933dbae4c73STrond Myklebust if (ret == 0) 934dbae4c73STrond Myklebust ret = ret2; 9351da177e4SLinus Torvalds offset += wsize; 9361da177e4SLinus Torvalds nbytes -= wsize; 9371da177e4SLinus Torvalds } while (nbytes != 0); 9381da177e4SLinus Torvalds 939dbae4c73STrond Myklebust return ret; 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds out_bad: 9421da177e4SLinus Torvalds while (!list_empty(&list)) { 9431da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_write_data, pages); 9441da177e4SLinus Torvalds list_del(&data->pages); 9458aca67f0STrond Myklebust nfs_writedata_release(data); 9461da177e4SLinus Torvalds } 94761822ab5STrond Myklebust nfs_redirty_request(req); 9481da177e4SLinus Torvalds return -ENOMEM; 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds 9511da177e4SLinus Torvalds /* 9521da177e4SLinus Torvalds * Create an RPC task for the given write request and kick it. 9531da177e4SLinus Torvalds * The page must have been locked by the caller. 9541da177e4SLinus Torvalds * 9551da177e4SLinus Torvalds * It may happen that the page we're passed is not marked dirty. 9561da177e4SLinus Torvalds * This is the case if nfs_updatepage detects a conflicting request 9571da177e4SLinus Torvalds * that has been written but not committed. 9581da177e4SLinus Torvalds */ 9598d5658c9STrond Myklebust static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how) 9601da177e4SLinus Torvalds { 9611da177e4SLinus Torvalds struct nfs_page *req; 9621da177e4SLinus Torvalds struct page **pages; 9631da177e4SLinus Torvalds struct nfs_write_data *data; 9641da177e4SLinus Torvalds 9658d5658c9STrond Myklebust data = nfs_writedata_alloc(npages); 9661da177e4SLinus Torvalds if (!data) 9671da177e4SLinus Torvalds goto out_bad; 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds pages = data->pagevec; 9701da177e4SLinus Torvalds while (!list_empty(head)) { 9711da177e4SLinus Torvalds req = nfs_list_entry(head->next); 9721da177e4SLinus Torvalds nfs_list_remove_request(req); 9731da177e4SLinus Torvalds nfs_list_add_request(req, &data->pages); 9741da177e4SLinus Torvalds ClearPageError(req->wb_page); 9751da177e4SLinus Torvalds *pages++ = req->wb_page; 9761da177e4SLinus Torvalds } 9771da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds /* Set up the argument struct */ 980dbae4c73STrond Myklebust return nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how); 9811da177e4SLinus Torvalds out_bad: 9821da177e4SLinus Torvalds while (!list_empty(head)) { 98310afec90STrond Myklebust req = nfs_list_entry(head->next); 9841da177e4SLinus Torvalds nfs_list_remove_request(req); 98561822ab5STrond Myklebust nfs_redirty_request(req); 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds return -ENOMEM; 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds 990c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, 991c63c7b05STrond Myklebust struct inode *inode, int ioflags) 9921da177e4SLinus Torvalds { 993bf4285e7SChuck Lever size_t wsize = NFS_SERVER(inode)->wsize; 9941da177e4SLinus Torvalds 995bcb71bbaSTrond Myklebust if (wsize < PAGE_CACHE_SIZE) 996c63c7b05STrond Myklebust nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags); 997bcb71bbaSTrond Myklebust else 998c63c7b05STrond Myklebust nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags); 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds /* 10021da177e4SLinus Torvalds * Handle a write reply that flushed part of a page. 10031da177e4SLinus Torvalds */ 1004788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata) 10051da177e4SLinus Torvalds { 1006788e7a89STrond Myklebust struct nfs_write_data *data = calldata; 10071da177e4SLinus Torvalds 100848186c7dSChuck Lever dprintk("NFS: %5u write(%s/%lld %d@%lld)", 100948186c7dSChuck Lever task->tk_pid, 101048186c7dSChuck Lever data->req->wb_context->path.dentry->d_inode->i_sb->s_id, 101148186c7dSChuck Lever (long long) 101248186c7dSChuck Lever NFS_FILEID(data->req->wb_context->path.dentry->d_inode), 101348186c7dSChuck Lever data->req->wb_bytes, (long long)req_offset(data->req)); 10141da177e4SLinus Torvalds 1015c9d8f89dSTrond Myklebust nfs_writeback_done(task, data); 1016c9d8f89dSTrond Myklebust } 1017788e7a89STrond Myklebust 1018c9d8f89dSTrond Myklebust static void nfs_writeback_release_partial(void *calldata) 1019c9d8f89dSTrond Myklebust { 1020c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1021c9d8f89dSTrond Myklebust struct nfs_page *req = data->req; 1022c9d8f89dSTrond Myklebust struct page *page = req->wb_page; 1023c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1024c9d8f89dSTrond Myklebust 1025c9d8f89dSTrond Myklebust if (status < 0) { 1026a301b777STrond Myklebust nfs_set_pageerror(page); 1027c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 1028c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 10298e821cadSTrond Myklebust goto out; 10308e821cadSTrond Myklebust } 10318e821cadSTrond Myklebust 10328e821cadSTrond Myklebust if (nfs_write_need_commit(data)) { 1033587142f8STrond Myklebust struct inode *inode = page->mapping->host; 10348e821cadSTrond Myklebust 1035587142f8STrond Myklebust spin_lock(&inode->i_lock); 10368e821cadSTrond Myklebust if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) { 10378e821cadSTrond Myklebust /* Do nothing we need to resend the writes */ 10388e821cadSTrond Myklebust } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) { 10391da177e4SLinus Torvalds memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); 10401da177e4SLinus Torvalds dprintk(" defer commit\n"); 10411da177e4SLinus Torvalds } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) { 10428e821cadSTrond Myklebust set_bit(PG_NEED_RESCHED, &req->wb_flags); 10438e821cadSTrond Myklebust clear_bit(PG_NEED_COMMIT, &req->wb_flags); 10441da177e4SLinus Torvalds dprintk(" server reboot detected\n"); 10451da177e4SLinus Torvalds } 1046587142f8STrond Myklebust spin_unlock(&inode->i_lock); 10471da177e4SLinus Torvalds } else 10481da177e4SLinus Torvalds dprintk(" OK\n"); 10491da177e4SLinus Torvalds 10508e821cadSTrond Myklebust out: 10511da177e4SLinus Torvalds if (atomic_dec_and_test(&req->wb_complete)) 10521da177e4SLinus Torvalds nfs_writepage_release(req); 1053c9d8f89dSTrond Myklebust nfs_writedata_release(calldata); 10541da177e4SLinus Torvalds } 10551da177e4SLinus Torvalds 1056def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1) 1057def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata) 1058def6ed7eSAndy Adamson { 1059def6ed7eSAndy Adamson struct nfs_write_data *data = calldata; 1060def6ed7eSAndy Adamson struct nfs_client *clp = (NFS_SERVER(data->inode))->nfs_client; 1061def6ed7eSAndy Adamson 1062def6ed7eSAndy Adamson if (nfs4_setup_sequence(clp, &data->args.seq_args, 1063def6ed7eSAndy Adamson &data->res.seq_res, 1, task)) 1064def6ed7eSAndy Adamson return; 1065def6ed7eSAndy Adamson rpc_call_start(task); 1066def6ed7eSAndy Adamson } 1067def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1068def6ed7eSAndy Adamson 1069788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = { 1070def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1) 1071def6ed7eSAndy Adamson .rpc_call_prepare = nfs_write_prepare, 1072def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1073788e7a89STrond Myklebust .rpc_call_done = nfs_writeback_done_partial, 1074c9d8f89dSTrond Myklebust .rpc_release = nfs_writeback_release_partial, 1075788e7a89STrond Myklebust }; 1076788e7a89STrond Myklebust 10771da177e4SLinus Torvalds /* 10781da177e4SLinus Torvalds * Handle a write reply that flushes a whole page. 10791da177e4SLinus Torvalds * 10801da177e4SLinus Torvalds * FIXME: There is an inherent race with invalidate_inode_pages and 10811da177e4SLinus Torvalds * writebacks since the page->count is kept > 1 for as long 10821da177e4SLinus Torvalds * as the page has a write request pending. 10831da177e4SLinus Torvalds */ 1084788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata) 10851da177e4SLinus Torvalds { 1086788e7a89STrond Myklebust struct nfs_write_data *data = calldata; 10871da177e4SLinus Torvalds 1088c9d8f89dSTrond Myklebust nfs_writeback_done(task, data); 1089c9d8f89dSTrond Myklebust } 1090c9d8f89dSTrond Myklebust 1091c9d8f89dSTrond Myklebust static void nfs_writeback_release_full(void *calldata) 1092c9d8f89dSTrond Myklebust { 1093c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1094c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1095788e7a89STrond Myklebust 10961da177e4SLinus Torvalds /* Update attributes as result of writeback. */ 10971da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 1098c9d8f89dSTrond Myklebust struct nfs_page *req = nfs_list_entry(data->pages.next); 1099c9d8f89dSTrond Myklebust struct page *page = req->wb_page; 1100c9d8f89dSTrond Myklebust 11011da177e4SLinus Torvalds nfs_list_remove_request(req); 11021da177e4SLinus Torvalds 110348186c7dSChuck Lever dprintk("NFS: %5u write (%s/%lld %d@%lld)", 110448186c7dSChuck Lever data->task.tk_pid, 110588be9f99STrond Myklebust req->wb_context->path.dentry->d_inode->i_sb->s_id, 110688be9f99STrond Myklebust (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 11071da177e4SLinus Torvalds req->wb_bytes, 11081da177e4SLinus Torvalds (long long)req_offset(req)); 11091da177e4SLinus Torvalds 1110c9d8f89dSTrond Myklebust if (status < 0) { 1111a301b777STrond Myklebust nfs_set_pageerror(page); 1112c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 1113c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 11148e821cadSTrond Myklebust goto remove_request; 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds 11178e821cadSTrond Myklebust if (nfs_write_need_commit(data)) { 11181da177e4SLinus Torvalds memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); 11191da177e4SLinus Torvalds nfs_mark_request_commit(req); 11208e821cadSTrond Myklebust nfs_end_page_writeback(page); 11211da177e4SLinus Torvalds dprintk(" marked for commit\n"); 11228e821cadSTrond Myklebust goto next; 11238e821cadSTrond Myklebust } 11248e821cadSTrond Myklebust dprintk(" OK\n"); 11258e821cadSTrond Myklebust remove_request: 11268e821cadSTrond Myklebust nfs_end_page_writeback(page); 11271da177e4SLinus Torvalds nfs_inode_remove_request(req); 11281da177e4SLinus Torvalds next: 11299fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 11301da177e4SLinus Torvalds } 1131c9d8f89dSTrond Myklebust nfs_writedata_release(calldata); 11321da177e4SLinus Torvalds } 11331da177e4SLinus Torvalds 1134788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = { 1135def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1) 1136def6ed7eSAndy Adamson .rpc_call_prepare = nfs_write_prepare, 1137def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1138788e7a89STrond Myklebust .rpc_call_done = nfs_writeback_done_full, 1139c9d8f89dSTrond Myklebust .rpc_release = nfs_writeback_release_full, 1140788e7a89STrond Myklebust }; 1141788e7a89STrond Myklebust 1142788e7a89STrond Myklebust 11431da177e4SLinus Torvalds /* 11441da177e4SLinus Torvalds * This function is called when the WRITE call is complete. 11451da177e4SLinus Torvalds */ 1146462d5b32SChuck Lever int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data) 11471da177e4SLinus Torvalds { 11481da177e4SLinus Torvalds struct nfs_writeargs *argp = &data->args; 11491da177e4SLinus Torvalds struct nfs_writeres *resp = &data->res; 1150eedc020eSAndy Adamson struct nfs_server *server = NFS_SERVER(data->inode); 1151788e7a89STrond Myklebust int status; 11521da177e4SLinus Torvalds 1153a3f565b1SChuck Lever dprintk("NFS: %5u nfs_writeback_done (status %d)\n", 11541da177e4SLinus Torvalds task->tk_pid, task->tk_status); 11551da177e4SLinus Torvalds 1156f551e44fSChuck Lever /* 1157f551e44fSChuck Lever * ->write_done will attempt to use post-op attributes to detect 1158f551e44fSChuck Lever * conflicting writes by other clients. A strict interpretation 1159f551e44fSChuck Lever * of close-to-open would allow us to continue caching even if 1160f551e44fSChuck Lever * another writer had changed the file, but some applications 1161f551e44fSChuck Lever * depend on tighter cache coherency when writing. 1162f551e44fSChuck Lever */ 1163788e7a89STrond Myklebust status = NFS_PROTO(data->inode)->write_done(task, data); 1164788e7a89STrond Myklebust if (status != 0) 1165788e7a89STrond Myklebust return status; 116691d5b470SChuck Lever nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count); 116791d5b470SChuck Lever 11681da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 11691da177e4SLinus Torvalds if (resp->verf->committed < argp->stable && task->tk_status >= 0) { 11701da177e4SLinus Torvalds /* We tried a write call, but the server did not 11711da177e4SLinus Torvalds * commit data to stable storage even though we 11721da177e4SLinus Torvalds * requested it. 11731da177e4SLinus Torvalds * Note: There is a known bug in Tru64 < 5.0 in which 11741da177e4SLinus Torvalds * the server reports NFS_DATA_SYNC, but performs 11751da177e4SLinus Torvalds * NFS_FILE_SYNC. We therefore implement this checking 11761da177e4SLinus Torvalds * as a dprintk() in order to avoid filling syslog. 11771da177e4SLinus Torvalds */ 11781da177e4SLinus Torvalds static unsigned long complain; 11791da177e4SLinus Torvalds 11801da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 11811da177e4SLinus Torvalds dprintk("NFS: faulty NFS server %s:" 11821da177e4SLinus Torvalds " (committed = %d) != (stable = %d)\n", 1183eedc020eSAndy Adamson server->nfs_client->cl_hostname, 11841da177e4SLinus Torvalds resp->verf->committed, argp->stable); 11851da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 11861da177e4SLinus Torvalds } 11871da177e4SLinus Torvalds } 11881da177e4SLinus Torvalds #endif 11891da177e4SLinus Torvalds /* Is this a short write? */ 11901da177e4SLinus Torvalds if (task->tk_status >= 0 && resp->count < argp->count) { 11911da177e4SLinus Torvalds static unsigned long complain; 11921da177e4SLinus Torvalds 119391d5b470SChuck Lever nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE); 119491d5b470SChuck Lever 11951da177e4SLinus Torvalds /* Has the server at least made some progress? */ 11961da177e4SLinus Torvalds if (resp->count != 0) { 11971da177e4SLinus Torvalds /* Was this an NFSv2 write or an NFSv3 stable write? */ 11981da177e4SLinus Torvalds if (resp->verf->committed != NFS_UNSTABLE) { 11991da177e4SLinus Torvalds /* Resend from where the server left off */ 12001da177e4SLinus Torvalds argp->offset += resp->count; 12011da177e4SLinus Torvalds argp->pgbase += resp->count; 12021da177e4SLinus Torvalds argp->count -= resp->count; 12031da177e4SLinus Torvalds } else { 12041da177e4SLinus Torvalds /* Resend as a stable write in order to avoid 12051da177e4SLinus Torvalds * headaches in the case of a server crash. 12061da177e4SLinus Torvalds */ 12071da177e4SLinus Torvalds argp->stable = NFS_FILE_SYNC; 12081da177e4SLinus Torvalds } 1209eedc020eSAndy Adamson nfs4_restart_rpc(task, server->nfs_client); 1210788e7a89STrond Myklebust return -EAGAIN; 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 12131da177e4SLinus Torvalds printk(KERN_WARNING 12141da177e4SLinus Torvalds "NFS: Server wrote zero bytes, expected %u.\n", 12151da177e4SLinus Torvalds argp->count); 12161da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds /* Can't do anything about it except throw an error. */ 12191da177e4SLinus Torvalds task->tk_status = -EIO; 12201da177e4SLinus Torvalds } 1221eedc020eSAndy Adamson nfs4_sequence_free_slot(server->nfs_client, &data->res.seq_res); 1222788e7a89STrond Myklebust return 0; 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 1227c9d8f89dSTrond Myklebust void nfs_commitdata_release(void *data) 12281da177e4SLinus Torvalds { 1229383ba719STrond Myklebust struct nfs_write_data *wdata = data; 1230383ba719STrond Myklebust 1231383ba719STrond Myklebust put_nfs_open_context(wdata->args.context); 12321da177e4SLinus Torvalds nfs_commit_free(wdata); 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds /* 12361da177e4SLinus Torvalds * Set up the argument/result storage required for the RPC call. 12371da177e4SLinus Torvalds */ 1238dbae4c73STrond Myklebust static int nfs_commit_rpcsetup(struct list_head *head, 1239788e7a89STrond Myklebust struct nfs_write_data *data, 1240788e7a89STrond Myklebust int how) 12411da177e4SLinus Torvalds { 124284115e1cSTrond Myklebust struct nfs_page *first = nfs_list_entry(head->next); 124384115e1cSTrond Myklebust struct inode *inode = first->wb_context->path.dentry->d_inode; 124484115e1cSTrond Myklebust int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC; 12453ff7576dSTrond Myklebust int priority = flush_task_priority(how); 124607737691STrond Myklebust struct rpc_task *task; 1247bdc7f021STrond Myklebust struct rpc_message msg = { 1248bdc7f021STrond Myklebust .rpc_argp = &data->args, 1249bdc7f021STrond Myklebust .rpc_resp = &data->res, 1250bdc7f021STrond Myklebust .rpc_cred = first->wb_context->cred, 1251bdc7f021STrond Myklebust }; 125284115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 125307737691STrond Myklebust .task = &data->task, 125484115e1cSTrond Myklebust .rpc_client = NFS_CLIENT(inode), 1255bdc7f021STrond Myklebust .rpc_message = &msg, 125684115e1cSTrond Myklebust .callback_ops = &nfs_commit_ops, 125784115e1cSTrond Myklebust .callback_data = data, 1258101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 125984115e1cSTrond Myklebust .flags = flags, 12603ff7576dSTrond Myklebust .priority = priority, 126184115e1cSTrond Myklebust }; 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds /* Set up the RPC argument and reply structs 12641da177e4SLinus Torvalds * NB: take care not to mess about with data->commit et al. */ 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds list_splice_init(head, &data->pages); 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds data->inode = inode; 1269bdc7f021STrond Myklebust data->cred = msg.rpc_cred; 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds data->args.fh = NFS_FH(data->inode); 12723da28eb1STrond Myklebust /* Note: we always request a commit of the entire inode */ 12733da28eb1STrond Myklebust data->args.offset = 0; 12743da28eb1STrond Myklebust data->args.count = 0; 1275383ba719STrond Myklebust data->args.context = get_nfs_open_context(first->wb_context); 12763da28eb1STrond Myklebust data->res.count = 0; 12771da177e4SLinus Torvalds data->res.fattr = &data->fattr; 12781da177e4SLinus Torvalds data->res.verf = &data->verf; 12790e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 12801da177e4SLinus Torvalds 1281788e7a89STrond Myklebust /* Set up the initial task struct. */ 1282bdc7f021STrond Myklebust NFS_PROTO(inode)->commit_setup(data, &msg); 12831da177e4SLinus Torvalds 1284a3f565b1SChuck Lever dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); 1285bdc7f021STrond Myklebust 128607737691STrond Myklebust task = rpc_run_task(&task_setup_data); 1287dbae4c73STrond Myklebust if (IS_ERR(task)) 1288dbae4c73STrond Myklebust return PTR_ERR(task); 128907737691STrond Myklebust rpc_put_task(task); 1290dbae4c73STrond Myklebust return 0; 12911da177e4SLinus Torvalds } 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds /* 12941da177e4SLinus Torvalds * Commit dirty pages 12951da177e4SLinus Torvalds */ 12961da177e4SLinus Torvalds static int 129740859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how) 12981da177e4SLinus Torvalds { 12991da177e4SLinus Torvalds struct nfs_write_data *data; 13001da177e4SLinus Torvalds struct nfs_page *req; 13011da177e4SLinus Torvalds 1302c9d8f89dSTrond Myklebust data = nfs_commitdata_alloc(); 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds if (!data) 13051da177e4SLinus Torvalds goto out_bad; 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds /* Set up the argument struct */ 1308dbae4c73STrond Myklebust return nfs_commit_rpcsetup(head, data, how); 13091da177e4SLinus Torvalds out_bad: 13101da177e4SLinus Torvalds while (!list_empty(head)) { 13111da177e4SLinus Torvalds req = nfs_list_entry(head->next); 13121da177e4SLinus Torvalds nfs_list_remove_request(req); 13131da177e4SLinus Torvalds nfs_mark_request_commit(req); 131483715ad5STrond Myklebust dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 1315c9e51e41SPeter Zijlstra dec_bdi_stat(req->wb_page->mapping->backing_dev_info, 1316c9e51e41SPeter Zijlstra BDI_RECLAIMABLE); 13179fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 13181da177e4SLinus Torvalds } 13191da177e4SLinus Torvalds return -ENOMEM; 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds /* 13231da177e4SLinus Torvalds * COMMIT call returned 13241da177e4SLinus Torvalds */ 1325788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata) 13261da177e4SLinus Torvalds { 1327963d8fe5STrond Myklebust struct nfs_write_data *data = calldata; 13281da177e4SLinus Torvalds 1329a3f565b1SChuck Lever dprintk("NFS: %5u nfs_commit_done (status %d)\n", 13301da177e4SLinus Torvalds task->tk_pid, task->tk_status); 13311da177e4SLinus Torvalds 1332788e7a89STrond Myklebust /* Call the NFS version-specific code */ 1333788e7a89STrond Myklebust if (NFS_PROTO(data->inode)->commit_done(task, data) != 0) 1334788e7a89STrond Myklebust return; 1335c9d8f89dSTrond Myklebust } 1336c9d8f89dSTrond Myklebust 1337c9d8f89dSTrond Myklebust static void nfs_commit_release(void *calldata) 1338c9d8f89dSTrond Myklebust { 1339c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1340c9d8f89dSTrond Myklebust struct nfs_page *req; 1341c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1342788e7a89STrond Myklebust 13431da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 13441da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 13451da177e4SLinus Torvalds nfs_list_remove_request(req); 1346e468bae9STrond Myklebust nfs_clear_request_commit(req); 13471da177e4SLinus Torvalds 134848186c7dSChuck Lever dprintk("NFS: commit (%s/%lld %d@%lld)", 134988be9f99STrond Myklebust req->wb_context->path.dentry->d_inode->i_sb->s_id, 135088be9f99STrond Myklebust (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 13511da177e4SLinus Torvalds req->wb_bytes, 13521da177e4SLinus Torvalds (long long)req_offset(req)); 1353c9d8f89dSTrond Myklebust if (status < 0) { 1354c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 13551da177e4SLinus Torvalds nfs_inode_remove_request(req); 1356c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 13571da177e4SLinus Torvalds goto next; 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds /* Okay, COMMIT succeeded, apparently. Check the verifier 13611da177e4SLinus Torvalds * returned by the server against all stored verfs. */ 13621da177e4SLinus Torvalds if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) { 13631da177e4SLinus Torvalds /* We have a match */ 13641da177e4SLinus Torvalds nfs_inode_remove_request(req); 13651da177e4SLinus Torvalds dprintk(" OK\n"); 13661da177e4SLinus Torvalds goto next; 13671da177e4SLinus Torvalds } 13681da177e4SLinus Torvalds /* We have a mismatch. Write the page again */ 13691da177e4SLinus Torvalds dprintk(" mismatch\n"); 13706d884e8fSFred nfs_mark_request_dirty(req); 13711da177e4SLinus Torvalds next: 13729fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 13731da177e4SLinus Torvalds } 1374c9d8f89dSTrond Myklebust nfs_commitdata_release(calldata); 13751da177e4SLinus Torvalds } 1376788e7a89STrond Myklebust 1377788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = { 137821d9a851SAndy Adamson #if defined(CONFIG_NFS_V4_1) 137921d9a851SAndy Adamson .rpc_call_prepare = nfs_write_prepare, 138021d9a851SAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1381788e7a89STrond Myklebust .rpc_call_done = nfs_commit_done, 1382788e7a89STrond Myklebust .rpc_release = nfs_commit_release, 1383788e7a89STrond Myklebust }; 13841da177e4SLinus Torvalds 13853da28eb1STrond Myklebust int nfs_commit_inode(struct inode *inode, int how) 13861da177e4SLinus Torvalds { 13871da177e4SLinus Torvalds LIST_HEAD(head); 13887d46a49fSTrond Myklebust int res; 13891da177e4SLinus Torvalds 1390587142f8STrond Myklebust spin_lock(&inode->i_lock); 13913da28eb1STrond Myklebust res = nfs_scan_commit(inode, &head, 0, 0); 1392587142f8STrond Myklebust spin_unlock(&inode->i_lock); 13931da177e4SLinus Torvalds if (res) { 13947d46a49fSTrond Myklebust int error = nfs_commit_list(inode, &head, how); 13951da177e4SLinus Torvalds if (error < 0) 13961da177e4SLinus Torvalds return error; 13973da28eb1STrond Myklebust } 13981da177e4SLinus Torvalds return res; 13991da177e4SLinus Torvalds } 1400c63c7b05STrond Myklebust #else 1401c63c7b05STrond Myklebust static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how) 1402c63c7b05STrond Myklebust { 1403c63c7b05STrond Myklebust return 0; 1404c63c7b05STrond Myklebust } 14051da177e4SLinus Torvalds #endif 14061da177e4SLinus Torvalds 14071c75950bSTrond Myklebust long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how) 14081da177e4SLinus Torvalds { 14091c75950bSTrond Myklebust struct inode *inode = mapping->host; 1410ca52fec1STrond Myklebust pgoff_t idx_start, idx_end; 14111c75950bSTrond Myklebust unsigned int npages = 0; 1412c42de9ddSTrond Myklebust LIST_HEAD(head); 141370b9ecbdSTrond Myklebust int nocommit = how & FLUSH_NOCOMMIT; 14143f442547STrond Myklebust long pages, ret; 14151da177e4SLinus Torvalds 14161c75950bSTrond Myklebust /* FIXME */ 14171c75950bSTrond Myklebust if (wbc->range_cyclic) 14181c75950bSTrond Myklebust idx_start = 0; 14191c75950bSTrond Myklebust else { 14201c75950bSTrond Myklebust idx_start = wbc->range_start >> PAGE_CACHE_SHIFT; 14211c75950bSTrond Myklebust idx_end = wbc->range_end >> PAGE_CACHE_SHIFT; 14221c75950bSTrond Myklebust if (idx_end > idx_start) { 1423ca52fec1STrond Myklebust pgoff_t l_npages = 1 + idx_end - idx_start; 14241c75950bSTrond Myklebust npages = l_npages; 14251c75950bSTrond Myklebust if (sizeof(npages) != sizeof(l_npages) && 1426ca52fec1STrond Myklebust (pgoff_t)npages != l_npages) 14271c75950bSTrond Myklebust npages = 0; 14281c75950bSTrond Myklebust } 14291c75950bSTrond Myklebust } 1430c42de9ddSTrond Myklebust how &= ~FLUSH_NOCOMMIT; 1431587142f8STrond Myklebust spin_lock(&inode->i_lock); 14321da177e4SLinus Torvalds do { 1433c42de9ddSTrond Myklebust ret = nfs_wait_on_requests_locked(inode, idx_start, npages); 1434c42de9ddSTrond Myklebust if (ret != 0) 1435c42de9ddSTrond Myklebust continue; 1436c42de9ddSTrond Myklebust if (nocommit) 1437c42de9ddSTrond Myklebust break; 1438d2ccddf0STrond Myklebust pages = nfs_scan_commit(inode, &head, idx_start, npages); 1439724c439cSTrond Myklebust if (pages == 0) 1440c42de9ddSTrond Myklebust break; 1441d2ccddf0STrond Myklebust if (how & FLUSH_INVALIDATE) { 1442587142f8STrond Myklebust spin_unlock(&inode->i_lock); 144383715ad5STrond Myklebust nfs_cancel_commit_list(&head); 1444e8e058e8STrond Myklebust ret = pages; 1445587142f8STrond Myklebust spin_lock(&inode->i_lock); 1446d2ccddf0STrond Myklebust continue; 1447d2ccddf0STrond Myklebust } 1448d2ccddf0STrond Myklebust pages += nfs_scan_commit(inode, &head, 0, 0); 1449587142f8STrond Myklebust spin_unlock(&inode->i_lock); 1450c42de9ddSTrond Myklebust ret = nfs_commit_list(inode, &head, how); 1451587142f8STrond Myklebust spin_lock(&inode->i_lock); 1452587142f8STrond Myklebust 1453c42de9ddSTrond Myklebust } while (ret >= 0); 1454587142f8STrond Myklebust spin_unlock(&inode->i_lock); 1455c42de9ddSTrond Myklebust return ret; 14561da177e4SLinus Torvalds } 14571da177e4SLinus Torvalds 145834901f70STrond Myklebust static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how) 14591c75950bSTrond Myklebust { 14601c75950bSTrond Myklebust int ret; 14611c75950bSTrond Myklebust 146234901f70STrond Myklebust ret = nfs_writepages(mapping, wbc); 146361822ab5STrond Myklebust if (ret < 0) 146461822ab5STrond Myklebust goto out; 146534901f70STrond Myklebust ret = nfs_sync_mapping_wait(mapping, wbc, how); 1466ed90ef51STrond Myklebust if (ret < 0) 1467ed90ef51STrond Myklebust goto out; 14681c75950bSTrond Myklebust return 0; 146961822ab5STrond Myklebust out: 1470e507d9ebSTrond Myklebust __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); 14711c75950bSTrond Myklebust return ret; 14721c75950bSTrond Myklebust } 14731c75950bSTrond Myklebust 147434901f70STrond Myklebust /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */ 147534901f70STrond Myklebust static int nfs_write_mapping(struct address_space *mapping, int how) 147634901f70STrond Myklebust { 147734901f70STrond Myklebust struct writeback_control wbc = { 147834901f70STrond Myklebust .bdi = mapping->backing_dev_info, 147972cb77f4STrond Myklebust .sync_mode = WB_SYNC_ALL, 148034901f70STrond Myklebust .nr_to_write = LONG_MAX, 1481d7fb1207STrond Myklebust .range_start = 0, 1482d7fb1207STrond Myklebust .range_end = LLONG_MAX, 148334901f70STrond Myklebust .for_writepages = 1, 148434901f70STrond Myklebust }; 148534901f70STrond Myklebust 148634901f70STrond Myklebust return __nfs_write_mapping(mapping, &wbc, how); 148734901f70STrond Myklebust } 148834901f70STrond Myklebust 1489ed90ef51STrond Myklebust /* 1490ed90ef51STrond Myklebust * flush the inode to disk. 1491ed90ef51STrond Myklebust */ 1492ed90ef51STrond Myklebust int nfs_wb_all(struct inode *inode) 14931c75950bSTrond Myklebust { 1494ed90ef51STrond Myklebust return nfs_write_mapping(inode->i_mapping, 0); 1495ed90ef51STrond Myklebust } 14961c75950bSTrond Myklebust 1497ed90ef51STrond Myklebust int nfs_wb_nocommit(struct inode *inode) 1498ed90ef51STrond Myklebust { 1499ed90ef51STrond Myklebust return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT); 15001c75950bSTrond Myklebust } 15011c75950bSTrond Myklebust 15021b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page) 15031b3b4a1aSTrond Myklebust { 15041b3b4a1aSTrond Myklebust struct nfs_page *req; 15051b3b4a1aSTrond Myklebust loff_t range_start = page_offset(page); 15061b3b4a1aSTrond Myklebust loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); 15071b3b4a1aSTrond Myklebust struct writeback_control wbc = { 15081b3b4a1aSTrond Myklebust .bdi = page->mapping->backing_dev_info, 15091b3b4a1aSTrond Myklebust .sync_mode = WB_SYNC_ALL, 15101b3b4a1aSTrond Myklebust .nr_to_write = LONG_MAX, 15111b3b4a1aSTrond Myklebust .range_start = range_start, 15121b3b4a1aSTrond Myklebust .range_end = range_end, 15131b3b4a1aSTrond Myklebust }; 15141b3b4a1aSTrond Myklebust int ret = 0; 15151b3b4a1aSTrond Myklebust 15161b3b4a1aSTrond Myklebust BUG_ON(!PageLocked(page)); 15171b3b4a1aSTrond Myklebust for (;;) { 15181b3b4a1aSTrond Myklebust req = nfs_page_find_request(page); 15191b3b4a1aSTrond Myklebust if (req == NULL) 15201b3b4a1aSTrond Myklebust goto out; 1521e468bae9STrond Myklebust if (test_bit(PG_CLEAN, &req->wb_flags)) { 15221b3b4a1aSTrond Myklebust nfs_release_request(req); 15231b3b4a1aSTrond Myklebust break; 15241b3b4a1aSTrond Myklebust } 15251b3b4a1aSTrond Myklebust if (nfs_lock_request_dontget(req)) { 15261b3b4a1aSTrond Myklebust nfs_inode_remove_request(req); 15271b3b4a1aSTrond Myklebust /* 15281b3b4a1aSTrond Myklebust * In case nfs_inode_remove_request has marked the 15291b3b4a1aSTrond Myklebust * page as being dirty 15301b3b4a1aSTrond Myklebust */ 15311b3b4a1aSTrond Myklebust cancel_dirty_page(page, PAGE_CACHE_SIZE); 15321b3b4a1aSTrond Myklebust nfs_unlock_request(req); 15331b3b4a1aSTrond Myklebust break; 15341b3b4a1aSTrond Myklebust } 15351b3b4a1aSTrond Myklebust ret = nfs_wait_on_request(req); 15361b3b4a1aSTrond Myklebust if (ret < 0) 15371b3b4a1aSTrond Myklebust goto out; 15381b3b4a1aSTrond Myklebust } 15391b3b4a1aSTrond Myklebust if (!PagePrivate(page)) 15401b3b4a1aSTrond Myklebust return 0; 15411b3b4a1aSTrond Myklebust ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE); 15421b3b4a1aSTrond Myklebust out: 15431b3b4a1aSTrond Myklebust return ret; 15441b3b4a1aSTrond Myklebust } 15451b3b4a1aSTrond Myklebust 15465334eb13SAdrian Bunk static int nfs_wb_page_priority(struct inode *inode, struct page *page, 15475334eb13SAdrian Bunk int how) 15481c75950bSTrond Myklebust { 15491c75950bSTrond Myklebust loff_t range_start = page_offset(page); 15501c75950bSTrond Myklebust loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); 15514d770ccfSTrond Myklebust struct writeback_control wbc = { 15524d770ccfSTrond Myklebust .bdi = page->mapping->backing_dev_info, 15534d770ccfSTrond Myklebust .sync_mode = WB_SYNC_ALL, 15544d770ccfSTrond Myklebust .nr_to_write = LONG_MAX, 15554d770ccfSTrond Myklebust .range_start = range_start, 15564d770ccfSTrond Myklebust .range_end = range_end, 15574d770ccfSTrond Myklebust }; 15584d770ccfSTrond Myklebust int ret; 15591c75950bSTrond Myklebust 156073e3302fSTrond Myklebust do { 1561c63c7b05STrond Myklebust if (clear_page_dirty_for_io(page)) { 15624d770ccfSTrond Myklebust ret = nfs_writepage_locked(page, &wbc); 15634d770ccfSTrond Myklebust if (ret < 0) 156473e3302fSTrond Myklebust goto out_error; 156573e3302fSTrond Myklebust } else if (!PagePrivate(page)) 156673e3302fSTrond Myklebust break; 15674d770ccfSTrond Myklebust ret = nfs_sync_mapping_wait(page->mapping, &wbc, how); 156873e3302fSTrond Myklebust if (ret < 0) 156973e3302fSTrond Myklebust goto out_error; 157073e3302fSTrond Myklebust } while (PagePrivate(page)); 15714d770ccfSTrond Myklebust return 0; 157273e3302fSTrond Myklebust out_error: 1573e507d9ebSTrond Myklebust __mark_inode_dirty(inode, I_DIRTY_PAGES); 15744d770ccfSTrond Myklebust return ret; 15751c75950bSTrond Myklebust } 15761c75950bSTrond Myklebust 15771c75950bSTrond Myklebust /* 15781c75950bSTrond Myklebust * Write back all requests on one page - we do this before reading it. 15791c75950bSTrond Myklebust */ 15801c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page* page) 15811c75950bSTrond Myklebust { 15824d770ccfSTrond Myklebust return nfs_wb_page_priority(inode, page, FLUSH_STABLE); 15831c75950bSTrond Myklebust } 15841c75950bSTrond Myklebust 1585f7b422b1SDavid Howells int __init nfs_init_writepagecache(void) 15861da177e4SLinus Torvalds { 15871da177e4SLinus Torvalds nfs_wdata_cachep = kmem_cache_create("nfs_write_data", 15881da177e4SLinus Torvalds sizeof(struct nfs_write_data), 15891da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 159020c2df83SPaul Mundt NULL); 15911da177e4SLinus Torvalds if (nfs_wdata_cachep == NULL) 15921da177e4SLinus Torvalds return -ENOMEM; 15931da177e4SLinus Torvalds 159493d2341cSMatthew Dobson nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, 15951da177e4SLinus Torvalds nfs_wdata_cachep); 15961da177e4SLinus Torvalds if (nfs_wdata_mempool == NULL) 15971da177e4SLinus Torvalds return -ENOMEM; 15981da177e4SLinus Torvalds 159993d2341cSMatthew Dobson nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, 16001da177e4SLinus Torvalds nfs_wdata_cachep); 16011da177e4SLinus Torvalds if (nfs_commit_mempool == NULL) 16021da177e4SLinus Torvalds return -ENOMEM; 16031da177e4SLinus Torvalds 160489a09141SPeter Zijlstra /* 160589a09141SPeter Zijlstra * NFS congestion size, scale with available memory. 160689a09141SPeter Zijlstra * 160789a09141SPeter Zijlstra * 64MB: 8192k 160889a09141SPeter Zijlstra * 128MB: 11585k 160989a09141SPeter Zijlstra * 256MB: 16384k 161089a09141SPeter Zijlstra * 512MB: 23170k 161189a09141SPeter Zijlstra * 1GB: 32768k 161289a09141SPeter Zijlstra * 2GB: 46340k 161389a09141SPeter Zijlstra * 4GB: 65536k 161489a09141SPeter Zijlstra * 8GB: 92681k 161589a09141SPeter Zijlstra * 16GB: 131072k 161689a09141SPeter Zijlstra * 161789a09141SPeter Zijlstra * This allows larger machines to have larger/more transfers. 161889a09141SPeter Zijlstra * Limit the default to 256M 161989a09141SPeter Zijlstra */ 162089a09141SPeter Zijlstra nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); 162189a09141SPeter Zijlstra if (nfs_congestion_kb > 256*1024) 162289a09141SPeter Zijlstra nfs_congestion_kb = 256*1024; 162389a09141SPeter Zijlstra 16241da177e4SLinus Torvalds return 0; 16251da177e4SLinus Torvalds } 16261da177e4SLinus Torvalds 1627266bee88SDavid Brownell void nfs_destroy_writepagecache(void) 16281da177e4SLinus Torvalds { 16291da177e4SLinus Torvalds mempool_destroy(nfs_commit_mempool); 16301da177e4SLinus Torvalds mempool_destroy(nfs_wdata_mempool); 16311a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_wdata_cachep); 16321da177e4SLinus Torvalds } 16331da177e4SLinus Torvalds 1634