11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/nfs/write.c 31da177e4SLinus Torvalds * 47c85d900STrond Myklebust * Write file data over NFS. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de> 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds #include <linux/types.h> 101da177e4SLinus Torvalds #include <linux/slab.h> 111da177e4SLinus Torvalds #include <linux/mm.h> 121da177e4SLinus Torvalds #include <linux/pagemap.h> 131da177e4SLinus Torvalds #include <linux/file.h> 141da177e4SLinus Torvalds #include <linux/writeback.h> 1589a09141SPeter Zijlstra #include <linux/swap.h> 16074cc1deSTrond Myklebust #include <linux/migrate.h> 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 191da177e4SLinus Torvalds #include <linux/nfs_fs.h> 201da177e4SLinus Torvalds #include <linux/nfs_mount.h> 211da177e4SLinus Torvalds #include <linux/nfs_page.h> 223fcfab16SAndrew Morton #include <linux/backing-dev.h> 233fcfab16SAndrew Morton 241da177e4SLinus Torvalds #include <asm/uaccess.h> 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds #include "delegation.h" 2749a70f27STrond Myklebust #include "internal.h" 2891d5b470SChuck Lever #include "iostat.h" 29def6ed7eSAndy Adamson #include "nfs4_fs.h" 30074cc1deSTrond Myklebust #include "fscache.h" 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_PAGECACHE 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds #define MIN_POOL_WRITE (32) 351da177e4SLinus Torvalds #define MIN_POOL_COMMIT (4) 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds /* 381da177e4SLinus Torvalds * Local function declarations 391da177e4SLinus Torvalds */ 40c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc, 41c63c7b05STrond Myklebust struct inode *inode, int ioflags); 42f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req); 43788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops; 44788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops; 45788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops; 461da177e4SLinus Torvalds 47e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep; 483feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool; 491da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool; 501da177e4SLinus Torvalds 51c9d8f89dSTrond Myklebust struct nfs_write_data *nfs_commitdata_alloc(void) 521da177e4SLinus Torvalds { 53e6b4f8daSChristoph Lameter struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS); 5440859d7eSChuck Lever 551da177e4SLinus Torvalds if (p) { 561da177e4SLinus Torvalds memset(p, 0, sizeof(*p)); 571da177e4SLinus Torvalds INIT_LIST_HEAD(&p->pages); 581da177e4SLinus Torvalds } 591da177e4SLinus Torvalds return p; 601da177e4SLinus Torvalds } 611da177e4SLinus Torvalds 625e4424afSTrond Myklebust void nfs_commit_free(struct nfs_write_data *p) 631da177e4SLinus Torvalds { 6440859d7eSChuck Lever if (p && (p->pagevec != &p->page_array[0])) 6540859d7eSChuck Lever kfree(p->pagevec); 661da177e4SLinus Torvalds mempool_free(p, nfs_commit_mempool); 671da177e4SLinus Torvalds } 681da177e4SLinus Torvalds 698d5658c9STrond Myklebust struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount) 703feb2d49STrond Myklebust { 71e6b4f8daSChristoph Lameter struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS); 723feb2d49STrond Myklebust 733feb2d49STrond Myklebust if (p) { 743feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 753feb2d49STrond Myklebust INIT_LIST_HEAD(&p->pages); 76e9f7bee1STrond Myklebust p->npages = pagecount; 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 901ae88b2eSTrond Myklebust 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 971ae88b2eSTrond Myklebust static void nfs_writedata_release(struct nfs_write_data *wdata) 981da177e4SLinus Torvalds { 99383ba719STrond Myklebust put_nfs_open_context(wdata->args.context); 1001da177e4SLinus Torvalds nfs_writedata_free(wdata); 1011da177e4SLinus Torvalds } 1021da177e4SLinus Torvalds 1037b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error) 1047b159fc1STrond Myklebust { 1057b159fc1STrond Myklebust ctx->error = error; 1067b159fc1STrond Myklebust smp_wmb(); 1077b159fc1STrond Myklebust set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 1087b159fc1STrond Myklebust } 1097b159fc1STrond Myklebust 110277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page) 111277459d2STrond Myklebust { 112277459d2STrond Myklebust struct nfs_page *req = NULL; 113277459d2STrond Myklebust 114277459d2STrond Myklebust if (PagePrivate(page)) { 115277459d2STrond Myklebust req = (struct nfs_page *)page_private(page); 116277459d2STrond Myklebust if (req != NULL) 117c03b4024STrond Myklebust kref_get(&req->wb_kref); 118277459d2STrond Myklebust } 119277459d2STrond Myklebust return req; 120277459d2STrond Myklebust } 121277459d2STrond Myklebust 122277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page) 123277459d2STrond Myklebust { 124587142f8STrond Myklebust struct inode *inode = page->mapping->host; 125277459d2STrond Myklebust struct nfs_page *req = NULL; 126277459d2STrond Myklebust 127587142f8STrond Myklebust spin_lock(&inode->i_lock); 128277459d2STrond Myklebust req = nfs_page_find_request_locked(page); 129587142f8STrond Myklebust spin_unlock(&inode->i_lock); 130277459d2STrond Myklebust return req; 131277459d2STrond Myklebust } 132277459d2STrond Myklebust 1331da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */ 1341da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) 1351da177e4SLinus Torvalds { 1361da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 137a3d01454STrond Myklebust loff_t end, i_size; 138a3d01454STrond Myklebust pgoff_t end_index; 1391da177e4SLinus Torvalds 140a3d01454STrond Myklebust spin_lock(&inode->i_lock); 141a3d01454STrond Myklebust i_size = i_size_read(inode); 142a3d01454STrond Myklebust end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; 1431da177e4SLinus Torvalds if (i_size > 0 && page->index < end_index) 144a3d01454STrond Myklebust goto out; 1451da177e4SLinus Torvalds end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count); 1461da177e4SLinus Torvalds if (i_size >= end) 147a3d01454STrond Myklebust goto out; 1481da177e4SLinus Torvalds i_size_write(inode, end); 149a3d01454STrond Myklebust nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); 150a3d01454STrond Myklebust out: 151a3d01454STrond Myklebust spin_unlock(&inode->i_lock); 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds 154a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */ 155a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page) 156a301b777STrond Myklebust { 157a301b777STrond Myklebust SetPageError(page); 158a301b777STrond Myklebust nfs_zap_mapping(page->mapping->host, page->mapping); 159a301b777STrond Myklebust } 160a301b777STrond Myklebust 1611da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request 1621da177e4SLinus Torvalds * covers the full page. 1631da177e4SLinus Torvalds */ 1641da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count) 1651da177e4SLinus Torvalds { 1661da177e4SLinus Torvalds if (PageUptodate(page)) 1671da177e4SLinus Torvalds return; 1681da177e4SLinus Torvalds if (base != 0) 1691da177e4SLinus Torvalds return; 17049a70f27STrond Myklebust if (count != nfs_page_length(page)) 1711da177e4SLinus Torvalds return; 1721da177e4SLinus Torvalds SetPageUptodate(page); 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc) 1761da177e4SLinus Torvalds { 1771da177e4SLinus Torvalds if (wbc->for_reclaim) 178c63c7b05STrond Myklebust return FLUSH_HIGHPRI | FLUSH_STABLE; 179b17621feSWu Fengguang if (wbc->for_kupdate || wbc->for_background) 1801da177e4SLinus Torvalds return FLUSH_LOWPRI; 1811da177e4SLinus Torvalds return 0; 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds /* 18589a09141SPeter Zijlstra * NFS congestion control 18689a09141SPeter Zijlstra */ 18789a09141SPeter Zijlstra 18889a09141SPeter Zijlstra int nfs_congestion_kb; 18989a09141SPeter Zijlstra 19089a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) 19189a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH \ 19289a09141SPeter Zijlstra (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) 19389a09141SPeter Zijlstra 1945a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page) 19589a09141SPeter Zijlstra { 1965a6d41b3STrond Myklebust int ret = test_set_page_writeback(page); 1975a6d41b3STrond Myklebust 1985a6d41b3STrond Myklebust if (!ret) { 19989a09141SPeter Zijlstra struct inode *inode = page->mapping->host; 20089a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 20189a09141SPeter Zijlstra 202a6305ddbSTrond Myklebust page_cache_get(page); 203277866a0SPeter Zijlstra if (atomic_long_inc_return(&nfss->writeback) > 2048aa7e847SJens Axboe NFS_CONGESTION_ON_THRESH) { 2058aa7e847SJens Axboe set_bdi_congested(&nfss->backing_dev_info, 2068aa7e847SJens Axboe BLK_RW_ASYNC); 2078aa7e847SJens Axboe } 20889a09141SPeter Zijlstra } 2095a6d41b3STrond Myklebust return ret; 21089a09141SPeter Zijlstra } 21189a09141SPeter Zijlstra 21289a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page) 21389a09141SPeter Zijlstra { 21489a09141SPeter Zijlstra struct inode *inode = page->mapping->host; 21589a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 21689a09141SPeter Zijlstra 21789a09141SPeter Zijlstra end_page_writeback(page); 218a6305ddbSTrond Myklebust page_cache_release(page); 219c4dc4beeSPeter Zijlstra if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) 2208aa7e847SJens Axboe clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC); 22189a09141SPeter Zijlstra } 22289a09141SPeter Zijlstra 223cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock) 224e261f51fSTrond Myklebust { 225587142f8STrond Myklebust struct inode *inode = page->mapping->host; 226e261f51fSTrond Myklebust struct nfs_page *req; 227e261f51fSTrond Myklebust int ret; 228e261f51fSTrond Myklebust 229587142f8STrond Myklebust spin_lock(&inode->i_lock); 230e261f51fSTrond Myklebust for (;;) { 231e261f51fSTrond Myklebust req = nfs_page_find_request_locked(page); 232074cc1deSTrond Myklebust if (req == NULL) 233074cc1deSTrond Myklebust break; 234acee478aSTrond Myklebust if (nfs_set_page_tag_locked(req)) 235e261f51fSTrond Myklebust break; 236e261f51fSTrond Myklebust /* Note: If we hold the page lock, as is the case in nfs_writepage, 237acee478aSTrond Myklebust * then the call to nfs_set_page_tag_locked() will always 238e261f51fSTrond Myklebust * succeed provided that someone hasn't already marked the 239e261f51fSTrond Myklebust * request as dirty (in which case we don't care). 240e261f51fSTrond Myklebust */ 241587142f8STrond Myklebust spin_unlock(&inode->i_lock); 242cfb506e1STrond Myklebust if (!nonblock) 243e261f51fSTrond Myklebust ret = nfs_wait_on_request(req); 244cfb506e1STrond Myklebust else 245cfb506e1STrond Myklebust ret = -EAGAIN; 246e261f51fSTrond Myklebust nfs_release_request(req); 247e261f51fSTrond Myklebust if (ret != 0) 248074cc1deSTrond Myklebust return ERR_PTR(ret); 249587142f8STrond Myklebust spin_lock(&inode->i_lock); 250e261f51fSTrond Myklebust } 251587142f8STrond Myklebust spin_unlock(&inode->i_lock); 252074cc1deSTrond Myklebust return req; 253612c9384STrond Myklebust } 254074cc1deSTrond Myklebust 255074cc1deSTrond Myklebust /* 256074cc1deSTrond Myklebust * Find an associated nfs write request, and prepare to flush it out 257074cc1deSTrond Myklebust * May return an error if the user signalled nfs_wait_on_request(). 258074cc1deSTrond Myklebust */ 259074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, 260cfb506e1STrond Myklebust struct page *page, bool nonblock) 261074cc1deSTrond Myklebust { 262074cc1deSTrond Myklebust struct nfs_page *req; 263074cc1deSTrond Myklebust int ret = 0; 264074cc1deSTrond Myklebust 265cfb506e1STrond Myklebust req = nfs_find_and_lock_request(page, nonblock); 266074cc1deSTrond Myklebust if (!req) 267074cc1deSTrond Myklebust goto out; 268074cc1deSTrond Myklebust ret = PTR_ERR(req); 269074cc1deSTrond Myklebust if (IS_ERR(req)) 270074cc1deSTrond Myklebust goto out; 271074cc1deSTrond Myklebust 272074cc1deSTrond Myklebust ret = nfs_set_page_writeback(page); 273074cc1deSTrond Myklebust BUG_ON(ret != 0); 274074cc1deSTrond Myklebust BUG_ON(test_bit(PG_CLEAN, &req->wb_flags)); 275074cc1deSTrond Myklebust 276f8512ad0SFred Isaman if (!nfs_pageio_add_request(pgio, req)) { 277f8512ad0SFred Isaman nfs_redirty_request(req); 278074cc1deSTrond Myklebust ret = pgio->pg_error; 279f8512ad0SFred Isaman } 280074cc1deSTrond Myklebust out: 281074cc1deSTrond Myklebust return ret; 282e261f51fSTrond Myklebust } 283e261f51fSTrond Myklebust 284f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio) 285f758c885STrond Myklebust { 286f758c885STrond Myklebust struct inode *inode = page->mapping->host; 287cfb506e1STrond Myklebust int ret; 288f758c885STrond Myklebust 289f758c885STrond Myklebust nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); 290f758c885STrond Myklebust nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); 291f758c885STrond Myklebust 292f758c885STrond Myklebust nfs_pageio_cond_complete(pgio, page->index); 2931b430beeSWu Fengguang ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE); 294cfb506e1STrond Myklebust if (ret == -EAGAIN) { 295cfb506e1STrond Myklebust redirty_page_for_writepage(wbc, page); 296cfb506e1STrond Myklebust ret = 0; 297cfb506e1STrond Myklebust } 298cfb506e1STrond Myklebust return ret; 299f758c885STrond Myklebust } 300f758c885STrond Myklebust 301e261f51fSTrond Myklebust /* 3021da177e4SLinus Torvalds * Write an mmapped page to the server. 3031da177e4SLinus Torvalds */ 3044d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc) 3051da177e4SLinus Torvalds { 306f758c885STrond Myklebust struct nfs_pageio_descriptor pgio; 307e261f51fSTrond Myklebust int err; 3081da177e4SLinus Torvalds 309f758c885STrond Myklebust nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc)); 310f758c885STrond Myklebust err = nfs_do_writepage(page, wbc, &pgio); 311f758c885STrond Myklebust nfs_pageio_complete(&pgio); 312f758c885STrond Myklebust if (err < 0) 3134d770ccfSTrond Myklebust return err; 314f758c885STrond Myklebust if (pgio.pg_error < 0) 315f758c885STrond Myklebust return pgio.pg_error; 316f758c885STrond Myklebust return 0; 3174d770ccfSTrond Myklebust } 3184d770ccfSTrond Myklebust 3194d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc) 3204d770ccfSTrond Myklebust { 321f758c885STrond Myklebust int ret; 3224d770ccfSTrond Myklebust 323f758c885STrond Myklebust ret = nfs_writepage_locked(page, wbc); 3241da177e4SLinus Torvalds unlock_page(page); 325f758c885STrond Myklebust return ret; 326f758c885STrond Myklebust } 327f758c885STrond Myklebust 328f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) 329f758c885STrond Myklebust { 330f758c885STrond Myklebust int ret; 331f758c885STrond Myklebust 332f758c885STrond Myklebust ret = nfs_do_writepage(page, wbc, data); 333f758c885STrond Myklebust unlock_page(page); 334f758c885STrond Myklebust return ret; 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) 3381da177e4SLinus Torvalds { 3391da177e4SLinus Torvalds struct inode *inode = mapping->host; 34072cb77f4STrond Myklebust unsigned long *bitlock = &NFS_I(inode)->flags; 341c63c7b05STrond Myklebust struct nfs_pageio_descriptor pgio; 3421da177e4SLinus Torvalds int err; 3431da177e4SLinus Torvalds 34472cb77f4STrond Myklebust /* Stop dirtying of new pages while we sync */ 34572cb77f4STrond Myklebust err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING, 34672cb77f4STrond Myklebust nfs_wait_bit_killable, TASK_KILLABLE); 34772cb77f4STrond Myklebust if (err) 34872cb77f4STrond Myklebust goto out_err; 34972cb77f4STrond Myklebust 35091d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); 35191d5b470SChuck Lever 352c63c7b05STrond Myklebust nfs_pageio_init_write(&pgio, inode, wb_priority(wbc)); 353f758c885STrond Myklebust err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); 354c63c7b05STrond Myklebust nfs_pageio_complete(&pgio); 35572cb77f4STrond Myklebust 35672cb77f4STrond Myklebust clear_bit_unlock(NFS_INO_FLUSHING, bitlock); 35772cb77f4STrond Myklebust smp_mb__after_clear_bit(); 35872cb77f4STrond Myklebust wake_up_bit(bitlock, NFS_INO_FLUSHING); 35972cb77f4STrond Myklebust 360f758c885STrond Myklebust if (err < 0) 36172cb77f4STrond Myklebust goto out_err; 36272cb77f4STrond Myklebust err = pgio.pg_error; 36372cb77f4STrond Myklebust if (err < 0) 36472cb77f4STrond Myklebust goto out_err; 365c63c7b05STrond Myklebust return 0; 36672cb77f4STrond Myklebust out_err: 36772cb77f4STrond Myklebust return err; 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds 3701da177e4SLinus Torvalds /* 3711da177e4SLinus Torvalds * Insert a write request into an inode 3721da177e4SLinus Torvalds */ 373e7d39069STrond Myklebust static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req) 3741da177e4SLinus Torvalds { 3751da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 3761da177e4SLinus Torvalds int error; 3771da177e4SLinus Torvalds 378e7d39069STrond Myklebust error = radix_tree_preload(GFP_NOFS); 379e7d39069STrond Myklebust if (error != 0) 380e7d39069STrond Myklebust goto out; 381e7d39069STrond Myklebust 382e7d39069STrond Myklebust /* Lock the request! */ 383e7d39069STrond Myklebust nfs_lock_request_dontget(req); 384e7d39069STrond Myklebust 385e7d39069STrond Myklebust spin_lock(&inode->i_lock); 3861da177e4SLinus Torvalds error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req); 38727852596SNick Piggin BUG_ON(error); 3881da177e4SLinus Torvalds if (!nfsi->npages) { 3891da177e4SLinus Torvalds igrab(inode); 3901da177e4SLinus Torvalds if (nfs_have_delegation(inode, FMODE_WRITE)) 3911da177e4SLinus Torvalds nfsi->change_attr++; 3921da177e4SLinus Torvalds } 3932df485a7STrond Myklebust set_bit(PG_MAPPED, &req->wb_flags); 394deb7d638STrond Myklebust SetPagePrivate(req->wb_page); 395277459d2STrond Myklebust set_page_private(req->wb_page, (unsigned long)req); 3961da177e4SLinus Torvalds nfsi->npages++; 397c03b4024STrond Myklebust kref_get(&req->wb_kref); 39827852596SNick Piggin radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, 39927852596SNick Piggin NFS_PAGE_TAG_LOCKED); 400e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 401e7d39069STrond Myklebust radix_tree_preload_end(); 402e7d39069STrond Myklebust out: 403e7d39069STrond Myklebust return error; 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds /* 40789a09141SPeter Zijlstra * Remove a write request from an inode 4081da177e4SLinus Torvalds */ 4091da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req) 4101da177e4SLinus Torvalds { 41188be9f99STrond Myklebust struct inode *inode = req->wb_context->path.dentry->d_inode; 4121da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds BUG_ON (!NFS_WBACK_BUSY(req)); 4151da177e4SLinus Torvalds 416587142f8STrond Myklebust spin_lock(&inode->i_lock); 417277459d2STrond Myklebust set_page_private(req->wb_page, 0); 418deb7d638STrond Myklebust ClearPagePrivate(req->wb_page); 4192df485a7STrond Myklebust clear_bit(PG_MAPPED, &req->wb_flags); 4201da177e4SLinus Torvalds radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index); 4211da177e4SLinus Torvalds nfsi->npages--; 4221da177e4SLinus Torvalds if (!nfsi->npages) { 423587142f8STrond Myklebust spin_unlock(&inode->i_lock); 4241da177e4SLinus Torvalds iput(inode); 4251da177e4SLinus Torvalds } else 426587142f8STrond Myklebust spin_unlock(&inode->i_lock); 4271da177e4SLinus Torvalds nfs_release_request(req); 4281da177e4SLinus Torvalds } 4291da177e4SLinus Torvalds 43061822ab5STrond Myklebust static void 4316d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req) 43261822ab5STrond Myklebust { 43361822ab5STrond Myklebust __set_page_dirty_nobuffers(req->wb_page); 434b80c3cb6STrond Myklebust __mark_inode_dirty(req->wb_page->mapping->host, I_DIRTY_DATASYNC); 43561822ab5STrond Myklebust } 43661822ab5STrond Myklebust 4371da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 4381da177e4SLinus Torvalds /* 4391da177e4SLinus Torvalds * Add a request to the inode's commit list. 4401da177e4SLinus Torvalds */ 4411da177e4SLinus Torvalds static void 4421da177e4SLinus Torvalds nfs_mark_request_commit(struct nfs_page *req) 4431da177e4SLinus Torvalds { 44488be9f99STrond Myklebust struct inode *inode = req->wb_context->path.dentry->d_inode; 4451da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 4461da177e4SLinus Torvalds 447587142f8STrond Myklebust spin_lock(&inode->i_lock); 448e468bae9STrond Myklebust set_bit(PG_CLEAN, &(req)->wb_flags); 4495c369683STrond Myklebust radix_tree_tag_set(&nfsi->nfs_page_tree, 4505c369683STrond Myklebust req->wb_index, 4515c369683STrond Myklebust NFS_PAGE_TAG_COMMIT); 452ff778d02STrond Myklebust nfsi->ncommit++; 453587142f8STrond Myklebust spin_unlock(&inode->i_lock); 454fd39fc85SChristoph Lameter inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 455c9e51e41SPeter Zijlstra inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE); 456a1803044STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 4571da177e4SLinus Torvalds } 4588e821cadSTrond Myklebust 459e468bae9STrond Myklebust static int 460e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req) 461e468bae9STrond Myklebust { 462e468bae9STrond Myklebust struct page *page = req->wb_page; 463e468bae9STrond Myklebust 464e468bae9STrond Myklebust if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) { 465e468bae9STrond Myklebust dec_zone_page_state(page, NR_UNSTABLE_NFS); 466e468bae9STrond Myklebust dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE); 467e468bae9STrond Myklebust return 1; 468e468bae9STrond Myklebust } 469e468bae9STrond Myklebust return 0; 470e468bae9STrond Myklebust } 471e468bae9STrond Myklebust 4728e821cadSTrond Myklebust static inline 4738e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 4748e821cadSTrond Myklebust { 4758e821cadSTrond Myklebust return data->verf.committed != NFS_FILE_SYNC; 4768e821cadSTrond Myklebust } 4778e821cadSTrond Myklebust 4788e821cadSTrond Myklebust static inline 4798e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req) 4808e821cadSTrond Myklebust { 481e468bae9STrond Myklebust if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) { 4828e821cadSTrond Myklebust nfs_mark_request_commit(req); 4838e821cadSTrond Myklebust return 1; 4848e821cadSTrond Myklebust } 4858e821cadSTrond Myklebust if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) { 4866d884e8fSFred nfs_mark_request_dirty(req); 4878e821cadSTrond Myklebust return 1; 4888e821cadSTrond Myklebust } 4898e821cadSTrond Myklebust return 0; 4908e821cadSTrond Myklebust } 4918e821cadSTrond Myklebust #else 4928e821cadSTrond Myklebust static inline void 4938e821cadSTrond Myklebust nfs_mark_request_commit(struct nfs_page *req) 4948e821cadSTrond Myklebust { 4958e821cadSTrond Myklebust } 4968e821cadSTrond Myklebust 497e468bae9STrond Myklebust static inline int 498e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req) 499e468bae9STrond Myklebust { 500e468bae9STrond Myklebust return 0; 501e468bae9STrond Myklebust } 502e468bae9STrond Myklebust 5038e821cadSTrond Myklebust static inline 5048e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 5058e821cadSTrond Myklebust { 5068e821cadSTrond Myklebust return 0; 5078e821cadSTrond Myklebust } 5088e821cadSTrond Myklebust 5098e821cadSTrond Myklebust static inline 5108e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req) 5118e821cadSTrond Myklebust { 5128e821cadSTrond Myklebust return 0; 5138e821cadSTrond Myklebust } 5141da177e4SLinus Torvalds #endif 5151da177e4SLinus Torvalds 51647c62564STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 517fb8a1f11STrond Myklebust static int 518fb8a1f11STrond Myklebust nfs_need_commit(struct nfs_inode *nfsi) 519fb8a1f11STrond Myklebust { 520fb8a1f11STrond Myklebust return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT); 521fb8a1f11STrond Myklebust } 522fb8a1f11STrond Myklebust 5231da177e4SLinus Torvalds /* 5241da177e4SLinus Torvalds * nfs_scan_commit - Scan an inode for commit requests 5251da177e4SLinus Torvalds * @inode: NFS inode to scan 5261da177e4SLinus Torvalds * @dst: destination list 5271da177e4SLinus Torvalds * @idx_start: lower bound of page->index to scan. 5281da177e4SLinus Torvalds * @npages: idx_start + npages sets the upper bound to scan. 5291da177e4SLinus Torvalds * 5301da177e4SLinus Torvalds * Moves requests from the inode's 'commit' request list. 5311da177e4SLinus Torvalds * The requests are *not* checked to ensure that they form a contiguous set. 5321da177e4SLinus Torvalds */ 5331da177e4SLinus Torvalds static int 534ca52fec1STrond Myklebust nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages) 5351da177e4SLinus Torvalds { 5361da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 537ff778d02STrond Myklebust int ret; 5383da28eb1STrond Myklebust 539fb8a1f11STrond Myklebust if (!nfs_need_commit(nfsi)) 540fb8a1f11STrond Myklebust return 0; 541fb8a1f11STrond Myklebust 542ff778d02STrond Myklebust ret = nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT); 543ff778d02STrond Myklebust if (ret > 0) 544ff778d02STrond Myklebust nfsi->ncommit -= ret; 5452928db1fSTrond Myklebust if (nfs_need_commit(NFS_I(inode))) 5462928db1fSTrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 547ff778d02STrond Myklebust return ret; 5481da177e4SLinus Torvalds } 549c42de9ddSTrond Myklebust #else 550fb8a1f11STrond Myklebust static inline int nfs_need_commit(struct nfs_inode *nfsi) 551fb8a1f11STrond Myklebust { 552fb8a1f11STrond Myklebust return 0; 553fb8a1f11STrond Myklebust } 554fb8a1f11STrond Myklebust 555ca52fec1STrond Myklebust static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages) 556c42de9ddSTrond Myklebust { 557c42de9ddSTrond Myklebust return 0; 558c42de9ddSTrond Myklebust } 5591da177e4SLinus Torvalds #endif 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds /* 562e7d39069STrond Myklebust * Search for an existing write request, and attempt to update 563e7d39069STrond Myklebust * it to reflect a new dirty region on a given page. 5641da177e4SLinus Torvalds * 565e7d39069STrond Myklebust * If the attempt fails, then the existing request is flushed out 566e7d39069STrond Myklebust * to disk. 5671da177e4SLinus Torvalds */ 568e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode, 569e7d39069STrond Myklebust struct page *page, 570e7d39069STrond Myklebust unsigned int offset, 571e7d39069STrond Myklebust unsigned int bytes) 5721da177e4SLinus Torvalds { 573e7d39069STrond Myklebust struct nfs_page *req; 574e7d39069STrond Myklebust unsigned int rqend; 575e7d39069STrond Myklebust unsigned int end; 5761da177e4SLinus Torvalds int error; 577277459d2STrond Myklebust 578e7d39069STrond Myklebust if (!PagePrivate(page)) 579e7d39069STrond Myklebust return NULL; 580e7d39069STrond Myklebust 581e7d39069STrond Myklebust end = offset + bytes; 582e7d39069STrond Myklebust spin_lock(&inode->i_lock); 583e7d39069STrond Myklebust 584e7d39069STrond Myklebust for (;;) { 585e7d39069STrond Myklebust req = nfs_page_find_request_locked(page); 586e7d39069STrond Myklebust if (req == NULL) 587e7d39069STrond Myklebust goto out_unlock; 588e7d39069STrond Myklebust 589e7d39069STrond Myklebust rqend = req->wb_offset + req->wb_bytes; 590e7d39069STrond Myklebust /* 591e7d39069STrond Myklebust * Tell the caller to flush out the request if 592e7d39069STrond Myklebust * the offsets are non-contiguous. 593e7d39069STrond Myklebust * Note: nfs_flush_incompatible() will already 594e7d39069STrond Myklebust * have flushed out requests having wrong owners. 595e7d39069STrond Myklebust */ 596e468bae9STrond Myklebust if (offset > rqend 597e7d39069STrond Myklebust || end < req->wb_offset) 598e7d39069STrond Myklebust goto out_flushme; 599e7d39069STrond Myklebust 600e7d39069STrond Myklebust if (nfs_set_page_tag_locked(req)) 601e7d39069STrond Myklebust break; 602e7d39069STrond Myklebust 603e7d39069STrond Myklebust /* The request is locked, so wait and then retry */ 604587142f8STrond Myklebust spin_unlock(&inode->i_lock); 6051da177e4SLinus Torvalds error = nfs_wait_on_request(req); 6061da177e4SLinus Torvalds nfs_release_request(req); 607e7d39069STrond Myklebust if (error != 0) 608e7d39069STrond Myklebust goto out_err; 609e7d39069STrond Myklebust spin_lock(&inode->i_lock); 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds 612ff778d02STrond Myklebust if (nfs_clear_request_commit(req) && 613e468bae9STrond Myklebust radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree, 614ff778d02STrond Myklebust req->wb_index, NFS_PAGE_TAG_COMMIT) != NULL) 615ff778d02STrond Myklebust NFS_I(inode)->ncommit--; 616e468bae9STrond Myklebust 6171da177e4SLinus Torvalds /* Okay, the request matches. Update the region */ 6181da177e4SLinus Torvalds if (offset < req->wb_offset) { 6191da177e4SLinus Torvalds req->wb_offset = offset; 6201da177e4SLinus Torvalds req->wb_pgbase = offset; 6211da177e4SLinus Torvalds } 6221da177e4SLinus Torvalds if (end > rqend) 6231da177e4SLinus Torvalds req->wb_bytes = end - req->wb_offset; 624e7d39069STrond Myklebust else 625e7d39069STrond Myklebust req->wb_bytes = rqend - req->wb_offset; 626e7d39069STrond Myklebust out_unlock: 627e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 628e7d39069STrond Myklebust return req; 629e7d39069STrond Myklebust out_flushme: 630e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 631e7d39069STrond Myklebust nfs_release_request(req); 632e7d39069STrond Myklebust error = nfs_wb_page(inode, page); 633e7d39069STrond Myklebust out_err: 634e7d39069STrond Myklebust return ERR_PTR(error); 635e7d39069STrond Myklebust } 6361da177e4SLinus Torvalds 637e7d39069STrond Myklebust /* 638e7d39069STrond Myklebust * Try to update an existing write request, or create one if there is none. 639e7d39069STrond Myklebust * 640e7d39069STrond Myklebust * Note: Should always be called with the Page Lock held to prevent races 641e7d39069STrond Myklebust * if we have to add a new request. Also assumes that the caller has 642e7d39069STrond Myklebust * already called nfs_flush_incompatible() if necessary. 643e7d39069STrond Myklebust */ 644e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, 645e7d39069STrond Myklebust struct page *page, unsigned int offset, unsigned int bytes) 646e7d39069STrond Myklebust { 647e7d39069STrond Myklebust struct inode *inode = page->mapping->host; 648e7d39069STrond Myklebust struct nfs_page *req; 649e7d39069STrond Myklebust int error; 650e7d39069STrond Myklebust 651e7d39069STrond Myklebust req = nfs_try_to_update_request(inode, page, offset, bytes); 652e7d39069STrond Myklebust if (req != NULL) 653e7d39069STrond Myklebust goto out; 654e7d39069STrond Myklebust req = nfs_create_request(ctx, inode, page, offset, bytes); 655e7d39069STrond Myklebust if (IS_ERR(req)) 656e7d39069STrond Myklebust goto out; 657e7d39069STrond Myklebust error = nfs_inode_add_request(inode, req); 658e7d39069STrond Myklebust if (error != 0) { 659e7d39069STrond Myklebust nfs_release_request(req); 660e7d39069STrond Myklebust req = ERR_PTR(error); 661e7d39069STrond Myklebust } 662efc91ed0STrond Myklebust out: 66361e930a9STrond Myklebust return req; 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds 666e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, 667e7d39069STrond Myklebust unsigned int offset, unsigned int count) 668e7d39069STrond Myklebust { 669e7d39069STrond Myklebust struct nfs_page *req; 670e7d39069STrond Myklebust 671e7d39069STrond Myklebust req = nfs_setup_write_request(ctx, page, offset, count); 672e7d39069STrond Myklebust if (IS_ERR(req)) 673e7d39069STrond Myklebust return PTR_ERR(req); 674b80c3cb6STrond Myklebust nfs_mark_request_dirty(req); 675e7d39069STrond Myklebust /* Update file length */ 676e7d39069STrond Myklebust nfs_grow_file(page, offset, count); 677e7d39069STrond Myklebust nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); 678a6305ddbSTrond Myklebust nfs_mark_request_dirty(req); 679e7d39069STrond Myklebust nfs_clear_page_tag_locked(req); 680e7d39069STrond Myklebust return 0; 681e7d39069STrond Myklebust } 682e7d39069STrond Myklebust 6831da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page) 6841da177e4SLinus Torvalds { 685cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 6861da177e4SLinus Torvalds struct nfs_page *req; 6871a54533eSTrond Myklebust int do_flush, status; 6881da177e4SLinus Torvalds /* 6891da177e4SLinus Torvalds * Look for a request corresponding to this page. If there 6901da177e4SLinus Torvalds * is one, and it belongs to another file, we flush it out 6911da177e4SLinus Torvalds * before we try to copy anything into the page. Do this 6921da177e4SLinus Torvalds * due to the lack of an ACCESS-type call in NFSv2. 6931da177e4SLinus Torvalds * Also do the same if we find a request from an existing 6941da177e4SLinus Torvalds * dropped page. 6951da177e4SLinus Torvalds */ 6961a54533eSTrond Myklebust do { 697277459d2STrond Myklebust req = nfs_page_find_request(page); 6981a54533eSTrond Myklebust if (req == NULL) 6991a54533eSTrond Myklebust return 0; 700f11ac8dbSTrond Myklebust do_flush = req->wb_page != page || req->wb_context != ctx || 701f11ac8dbSTrond Myklebust req->wb_lock_context->lockowner != current->files || 702f11ac8dbSTrond Myklebust req->wb_lock_context->pid != current->tgid; 7031da177e4SLinus Torvalds nfs_release_request(req); 7041a54533eSTrond Myklebust if (!do_flush) 7051a54533eSTrond Myklebust return 0; 706277459d2STrond Myklebust status = nfs_wb_page(page->mapping->host, page); 7071a54533eSTrond Myklebust } while (status == 0); 7081a54533eSTrond Myklebust return status; 7091da177e4SLinus Torvalds } 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds /* 7125d47a356STrond Myklebust * If the page cache is marked as unsafe or invalid, then we can't rely on 7135d47a356STrond Myklebust * the PageUptodate() flag. In this case, we will need to turn off 7145d47a356STrond Myklebust * write optimisations that depend on the page contents being correct. 7155d47a356STrond Myklebust */ 7165d47a356STrond Myklebust static int nfs_write_pageuptodate(struct page *page, struct inode *inode) 7175d47a356STrond Myklebust { 7185d47a356STrond Myklebust return PageUptodate(page) && 7195d47a356STrond Myklebust !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA)); 7205d47a356STrond Myklebust } 7215d47a356STrond Myklebust 7225d47a356STrond Myklebust /* 7231da177e4SLinus Torvalds * Update and possibly write a cached page of an NFS file. 7241da177e4SLinus Torvalds * 7251da177e4SLinus Torvalds * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad 7261da177e4SLinus Torvalds * things with a page scheduled for an RPC call (e.g. invalidate it). 7271da177e4SLinus Torvalds */ 7281da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page, 7291da177e4SLinus Torvalds unsigned int offset, unsigned int count) 7301da177e4SLinus Torvalds { 731cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 7321da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 7331da177e4SLinus Torvalds int status = 0; 7341da177e4SLinus Torvalds 73591d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 73691d5b470SChuck Lever 73748186c7dSChuck Lever dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n", 73801cce933SJosef "Jeff" Sipek file->f_path.dentry->d_parent->d_name.name, 73901cce933SJosef "Jeff" Sipek file->f_path.dentry->d_name.name, count, 7400bbacc40SChuck Lever (long long)(page_offset(page) + offset)); 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds /* If we're not using byte range locks, and we know the page 7435d47a356STrond Myklebust * is up to date, it may be more efficient to extend the write 7445d47a356STrond Myklebust * to cover the entire page in order to avoid fragmentation 7455d47a356STrond Myklebust * inefficiencies. 7461da177e4SLinus Torvalds */ 7475d47a356STrond Myklebust if (nfs_write_pageuptodate(page, inode) && 7485d47a356STrond Myklebust inode->i_flock == NULL && 7496b2f3d1fSChristoph Hellwig !(file->f_flags & O_DSYNC)) { 75049a70f27STrond Myklebust count = max(count + offset, nfs_page_length(page)); 7511da177e4SLinus Torvalds offset = 0; 7521da177e4SLinus Torvalds } 7531da177e4SLinus Torvalds 754e21195a7STrond Myklebust status = nfs_writepage_setup(ctx, page, offset, count); 75503fa9e84STrond Myklebust if (status < 0) 75603fa9e84STrond Myklebust nfs_set_pageerror(page); 7571da177e4SLinus Torvalds 75848186c7dSChuck Lever dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", 7591da177e4SLinus Torvalds status, (long long)i_size_read(inode)); 7601da177e4SLinus Torvalds return status; 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds static void nfs_writepage_release(struct nfs_page *req) 7641da177e4SLinus Torvalds { 765a6305ddbSTrond Myklebust struct page *page = req->wb_page; 7668e821cadSTrond Myklebust 767a6305ddbSTrond Myklebust if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) 7681da177e4SLinus Torvalds nfs_inode_remove_request(req); 7699fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 770a6305ddbSTrond Myklebust nfs_end_page_writeback(page); 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds 7733ff7576dSTrond Myklebust static int flush_task_priority(int how) 7741da177e4SLinus Torvalds { 7751da177e4SLinus Torvalds switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { 7761da177e4SLinus Torvalds case FLUSH_HIGHPRI: 7771da177e4SLinus Torvalds return RPC_PRIORITY_HIGH; 7781da177e4SLinus Torvalds case FLUSH_LOWPRI: 7791da177e4SLinus Torvalds return RPC_PRIORITY_LOW; 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds return RPC_PRIORITY_NORMAL; 7821da177e4SLinus Torvalds } 7831da177e4SLinus Torvalds 7841da177e4SLinus Torvalds /* 7851da177e4SLinus Torvalds * Set up the argument/result storage required for the RPC call. 7861da177e4SLinus Torvalds */ 787dbae4c73STrond Myklebust static int nfs_write_rpcsetup(struct nfs_page *req, 7881da177e4SLinus Torvalds struct nfs_write_data *data, 789788e7a89STrond Myklebust const struct rpc_call_ops *call_ops, 7901da177e4SLinus Torvalds unsigned int count, unsigned int offset, 7911da177e4SLinus Torvalds int how) 7921da177e4SLinus Torvalds { 79384115e1cSTrond Myklebust struct inode *inode = req->wb_context->path.dentry->d_inode; 7943ff7576dSTrond Myklebust int priority = flush_task_priority(how); 79507737691STrond Myklebust struct rpc_task *task; 796bdc7f021STrond Myklebust struct rpc_message msg = { 797bdc7f021STrond Myklebust .rpc_argp = &data->args, 798bdc7f021STrond Myklebust .rpc_resp = &data->res, 799bdc7f021STrond Myklebust .rpc_cred = req->wb_context->cred, 800bdc7f021STrond Myklebust }; 80184115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 80284115e1cSTrond Myklebust .rpc_client = NFS_CLIENT(inode), 80307737691STrond Myklebust .task = &data->task, 804bdc7f021STrond Myklebust .rpc_message = &msg, 80584115e1cSTrond Myklebust .callback_ops = call_ops, 80684115e1cSTrond Myklebust .callback_data = data, 807101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 8082c61be0aSTrond Myklebust .flags = RPC_TASK_ASYNC, 8093ff7576dSTrond Myklebust .priority = priority, 81084115e1cSTrond Myklebust }; 8112c61be0aSTrond Myklebust int ret = 0; 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds /* Set up the RPC argument and reply structs 8141da177e4SLinus Torvalds * NB: take care not to mess about with data->commit et al. */ 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds data->req = req; 81788be9f99STrond Myklebust data->inode = inode = req->wb_context->path.dentry->d_inode; 818bdc7f021STrond Myklebust data->cred = msg.rpc_cred; 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 8211da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 8221da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 8231da177e4SLinus Torvalds data->args.pages = data->pagevec; 8241da177e4SLinus Torvalds data->args.count = count; 825383ba719STrond Myklebust data->args.context = get_nfs_open_context(req->wb_context); 826f11ac8dbSTrond Myklebust data->args.lock_context = req->wb_lock_context; 827bdc7f021STrond Myklebust data->args.stable = NFS_UNSTABLE; 828bdc7f021STrond Myklebust if (how & FLUSH_STABLE) { 829bdc7f021STrond Myklebust data->args.stable = NFS_DATA_SYNC; 830fb8a1f11STrond Myklebust if (!nfs_need_commit(NFS_I(inode))) 831bdc7f021STrond Myklebust data->args.stable = NFS_FILE_SYNC; 832bdc7f021STrond Myklebust } 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds data->res.fattr = &data->fattr; 8351da177e4SLinus Torvalds data->res.count = count; 8361da177e4SLinus Torvalds data->res.verf = &data->verf; 8370e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 8381da177e4SLinus Torvalds 839788e7a89STrond Myklebust /* Set up the initial task struct. */ 840bdc7f021STrond Myklebust NFS_PROTO(inode)->write_setup(data, &msg); 8411da177e4SLinus Torvalds 842a3f565b1SChuck Lever dprintk("NFS: %5u initiated write call " 84348186c7dSChuck Lever "(req %s/%lld, %u bytes @ offset %llu)\n", 8440bbacc40SChuck Lever data->task.tk_pid, 8451da177e4SLinus Torvalds inode->i_sb->s_id, 8461da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 8471da177e4SLinus Torvalds count, 8481da177e4SLinus Torvalds (unsigned long long)data->args.offset); 8491da177e4SLinus Torvalds 85007737691STrond Myklebust task = rpc_run_task(&task_setup_data); 8512c61be0aSTrond Myklebust if (IS_ERR(task)) { 8522c61be0aSTrond Myklebust ret = PTR_ERR(task); 8532c61be0aSTrond Myklebust goto out; 8542c61be0aSTrond Myklebust } 8552c61be0aSTrond Myklebust if (how & FLUSH_SYNC) { 8562c61be0aSTrond Myklebust ret = rpc_wait_for_completion_task(task); 8572c61be0aSTrond Myklebust if (ret == 0) 8582c61be0aSTrond Myklebust ret = task->tk_status; 8592c61be0aSTrond Myklebust } 86007737691STrond Myklebust rpc_put_task(task); 8612c61be0aSTrond Myklebust out: 8622c61be0aSTrond Myklebust return ret; 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds 8656d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and 8666d884e8fSFred * call this on each, which will prepare them to be retried on next 8676d884e8fSFred * writeback using standard nfs. 8686d884e8fSFred */ 8696d884e8fSFred static void nfs_redirty_request(struct nfs_page *req) 8706d884e8fSFred { 871a6305ddbSTrond Myklebust struct page *page = req->wb_page; 872a6305ddbSTrond Myklebust 8736d884e8fSFred nfs_mark_request_dirty(req); 8746d884e8fSFred nfs_clear_page_tag_locked(req); 875a6305ddbSTrond Myklebust nfs_end_page_writeback(page); 8766d884e8fSFred } 8776d884e8fSFred 8781da177e4SLinus Torvalds /* 8791da177e4SLinus Torvalds * Generate multiple small requests to write out a single 8801da177e4SLinus Torvalds * contiguous dirty area on one page. 8811da177e4SLinus Torvalds */ 8828d5658c9STrond Myklebust static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how) 8831da177e4SLinus Torvalds { 8841da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(head->next); 8851da177e4SLinus Torvalds struct page *page = req->wb_page; 8861da177e4SLinus Torvalds struct nfs_write_data *data; 887e9f7bee1STrond Myklebust size_t wsize = NFS_SERVER(inode)->wsize, nbytes; 888e9f7bee1STrond Myklebust unsigned int offset; 8891da177e4SLinus Torvalds int requests = 0; 890dbae4c73STrond Myklebust int ret = 0; 8911da177e4SLinus Torvalds LIST_HEAD(list); 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds nfs_list_remove_request(req); 8941da177e4SLinus Torvalds 895bcb71bbaSTrond Myklebust nbytes = count; 896e9f7bee1STrond Myklebust do { 897e9f7bee1STrond Myklebust size_t len = min(nbytes, wsize); 898e9f7bee1STrond Myklebust 8998d5658c9STrond Myklebust data = nfs_writedata_alloc(1); 9001da177e4SLinus Torvalds if (!data) 9011da177e4SLinus Torvalds goto out_bad; 9021da177e4SLinus Torvalds list_add(&data->pages, &list); 9031da177e4SLinus Torvalds requests++; 904e9f7bee1STrond Myklebust nbytes -= len; 905e9f7bee1STrond Myklebust } while (nbytes != 0); 9061da177e4SLinus Torvalds atomic_set(&req->wb_complete, requests); 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds ClearPageError(page); 9091da177e4SLinus Torvalds offset = 0; 910bcb71bbaSTrond Myklebust nbytes = count; 9111da177e4SLinus Torvalds do { 912dbae4c73STrond Myklebust int ret2; 913dbae4c73STrond Myklebust 9141da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_write_data, pages); 9151da177e4SLinus Torvalds list_del_init(&data->pages); 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds data->pagevec[0] = page; 9181da177e4SLinus Torvalds 919bcb71bbaSTrond Myklebust if (nbytes < wsize) 920bcb71bbaSTrond Myklebust wsize = nbytes; 921dbae4c73STrond Myklebust ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops, 922788e7a89STrond Myklebust wsize, offset, how); 923dbae4c73STrond Myklebust if (ret == 0) 924dbae4c73STrond Myklebust ret = ret2; 9251da177e4SLinus Torvalds offset += wsize; 9261da177e4SLinus Torvalds nbytes -= wsize; 9271da177e4SLinus Torvalds } while (nbytes != 0); 9281da177e4SLinus Torvalds 929dbae4c73STrond Myklebust return ret; 9301da177e4SLinus Torvalds 9311da177e4SLinus Torvalds out_bad: 9321da177e4SLinus Torvalds while (!list_empty(&list)) { 9331da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_write_data, pages); 9341da177e4SLinus Torvalds list_del(&data->pages); 9350da2a4acSFred Isaman nfs_writedata_free(data); 9361da177e4SLinus Torvalds } 93761822ab5STrond Myklebust nfs_redirty_request(req); 9381da177e4SLinus Torvalds return -ENOMEM; 9391da177e4SLinus Torvalds } 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds /* 9421da177e4SLinus Torvalds * Create an RPC task for the given write request and kick it. 9431da177e4SLinus Torvalds * The page must have been locked by the caller. 9441da177e4SLinus Torvalds * 9451da177e4SLinus Torvalds * It may happen that the page we're passed is not marked dirty. 9461da177e4SLinus Torvalds * This is the case if nfs_updatepage detects a conflicting request 9471da177e4SLinus Torvalds * that has been written but not committed. 9481da177e4SLinus Torvalds */ 9498d5658c9STrond Myklebust static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how) 9501da177e4SLinus Torvalds { 9511da177e4SLinus Torvalds struct nfs_page *req; 9521da177e4SLinus Torvalds struct page **pages; 9531da177e4SLinus Torvalds struct nfs_write_data *data; 9541da177e4SLinus Torvalds 9558d5658c9STrond Myklebust data = nfs_writedata_alloc(npages); 9561da177e4SLinus Torvalds if (!data) 9571da177e4SLinus Torvalds goto out_bad; 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds pages = data->pagevec; 9601da177e4SLinus Torvalds while (!list_empty(head)) { 9611da177e4SLinus Torvalds req = nfs_list_entry(head->next); 9621da177e4SLinus Torvalds nfs_list_remove_request(req); 9631da177e4SLinus Torvalds nfs_list_add_request(req, &data->pages); 9641da177e4SLinus Torvalds ClearPageError(req->wb_page); 9651da177e4SLinus Torvalds *pages++ = req->wb_page; 9661da177e4SLinus Torvalds } 9671da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds /* Set up the argument struct */ 970dbae4c73STrond Myklebust return nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how); 9711da177e4SLinus Torvalds out_bad: 9721da177e4SLinus Torvalds while (!list_empty(head)) { 97310afec90STrond Myklebust req = nfs_list_entry(head->next); 9741da177e4SLinus Torvalds nfs_list_remove_request(req); 97561822ab5STrond Myklebust nfs_redirty_request(req); 9761da177e4SLinus Torvalds } 9771da177e4SLinus Torvalds return -ENOMEM; 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds 980c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, 981c63c7b05STrond Myklebust struct inode *inode, int ioflags) 9821da177e4SLinus Torvalds { 983bf4285e7SChuck Lever size_t wsize = NFS_SERVER(inode)->wsize; 9841da177e4SLinus Torvalds 985bcb71bbaSTrond Myklebust if (wsize < PAGE_CACHE_SIZE) 986c63c7b05STrond Myklebust nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags); 987bcb71bbaSTrond Myklebust else 988c63c7b05STrond Myklebust nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags); 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds /* 9921da177e4SLinus Torvalds * Handle a write reply that flushed part of a page. 9931da177e4SLinus Torvalds */ 994788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata) 9951da177e4SLinus Torvalds { 996788e7a89STrond Myklebust struct nfs_write_data *data = calldata; 9971da177e4SLinus Torvalds 99848186c7dSChuck Lever dprintk("NFS: %5u write(%s/%lld %d@%lld)", 99948186c7dSChuck Lever task->tk_pid, 100048186c7dSChuck Lever data->req->wb_context->path.dentry->d_inode->i_sb->s_id, 100148186c7dSChuck Lever (long long) 100248186c7dSChuck Lever NFS_FILEID(data->req->wb_context->path.dentry->d_inode), 100348186c7dSChuck Lever data->req->wb_bytes, (long long)req_offset(data->req)); 10041da177e4SLinus Torvalds 1005c9d8f89dSTrond Myklebust nfs_writeback_done(task, data); 1006c9d8f89dSTrond Myklebust } 1007788e7a89STrond Myklebust 1008c9d8f89dSTrond Myklebust static void nfs_writeback_release_partial(void *calldata) 1009c9d8f89dSTrond Myklebust { 1010c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1011c9d8f89dSTrond Myklebust struct nfs_page *req = data->req; 1012c9d8f89dSTrond Myklebust struct page *page = req->wb_page; 1013c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1014c9d8f89dSTrond Myklebust 1015c9d8f89dSTrond Myklebust if (status < 0) { 1016a301b777STrond Myklebust nfs_set_pageerror(page); 1017c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 1018c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 10198e821cadSTrond Myklebust goto out; 10208e821cadSTrond Myklebust } 10218e821cadSTrond Myklebust 10228e821cadSTrond Myklebust if (nfs_write_need_commit(data)) { 1023587142f8STrond Myklebust struct inode *inode = page->mapping->host; 10248e821cadSTrond Myklebust 1025587142f8STrond Myklebust spin_lock(&inode->i_lock); 10268e821cadSTrond Myklebust if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) { 10278e821cadSTrond Myklebust /* Do nothing we need to resend the writes */ 10288e821cadSTrond Myklebust } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) { 10291da177e4SLinus Torvalds memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); 10301da177e4SLinus Torvalds dprintk(" defer commit\n"); 10311da177e4SLinus Torvalds } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) { 10328e821cadSTrond Myklebust set_bit(PG_NEED_RESCHED, &req->wb_flags); 10338e821cadSTrond Myklebust clear_bit(PG_NEED_COMMIT, &req->wb_flags); 10341da177e4SLinus Torvalds dprintk(" server reboot detected\n"); 10351da177e4SLinus Torvalds } 1036587142f8STrond Myklebust spin_unlock(&inode->i_lock); 10371da177e4SLinus Torvalds } else 10381da177e4SLinus Torvalds dprintk(" OK\n"); 10391da177e4SLinus Torvalds 10408e821cadSTrond Myklebust out: 10411da177e4SLinus Torvalds if (atomic_dec_and_test(&req->wb_complete)) 10421da177e4SLinus Torvalds nfs_writepage_release(req); 1043c9d8f89dSTrond Myklebust nfs_writedata_release(calldata); 10441da177e4SLinus Torvalds } 10451da177e4SLinus Torvalds 1046def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1) 1047def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata) 1048def6ed7eSAndy Adamson { 1049def6ed7eSAndy Adamson struct nfs_write_data *data = calldata; 1050def6ed7eSAndy Adamson 1051035168abSTrond Myklebust if (nfs4_setup_sequence(NFS_SERVER(data->inode), 1052035168abSTrond Myklebust &data->args.seq_args, 1053def6ed7eSAndy Adamson &data->res.seq_res, 1, task)) 1054def6ed7eSAndy Adamson return; 1055def6ed7eSAndy Adamson rpc_call_start(task); 1056def6ed7eSAndy Adamson } 1057def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1058def6ed7eSAndy Adamson 1059788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = { 1060def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1) 1061def6ed7eSAndy Adamson .rpc_call_prepare = nfs_write_prepare, 1062def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1063788e7a89STrond Myklebust .rpc_call_done = nfs_writeback_done_partial, 1064c9d8f89dSTrond Myklebust .rpc_release = nfs_writeback_release_partial, 1065788e7a89STrond Myklebust }; 1066788e7a89STrond Myklebust 10671da177e4SLinus Torvalds /* 10681da177e4SLinus Torvalds * Handle a write reply that flushes a whole page. 10691da177e4SLinus Torvalds * 10701da177e4SLinus Torvalds * FIXME: There is an inherent race with invalidate_inode_pages and 10711da177e4SLinus Torvalds * writebacks since the page->count is kept > 1 for as long 10721da177e4SLinus Torvalds * as the page has a write request pending. 10731da177e4SLinus Torvalds */ 1074788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata) 10751da177e4SLinus Torvalds { 1076788e7a89STrond Myklebust struct nfs_write_data *data = calldata; 10771da177e4SLinus Torvalds 1078c9d8f89dSTrond Myklebust nfs_writeback_done(task, data); 1079c9d8f89dSTrond Myklebust } 1080c9d8f89dSTrond Myklebust 1081c9d8f89dSTrond Myklebust static void nfs_writeback_release_full(void *calldata) 1082c9d8f89dSTrond Myklebust { 1083c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1084c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1085788e7a89STrond Myklebust 10861da177e4SLinus Torvalds /* Update attributes as result of writeback. */ 10871da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 1088c9d8f89dSTrond Myklebust struct nfs_page *req = nfs_list_entry(data->pages.next); 1089c9d8f89dSTrond Myklebust struct page *page = req->wb_page; 1090c9d8f89dSTrond Myklebust 10911da177e4SLinus Torvalds nfs_list_remove_request(req); 10921da177e4SLinus Torvalds 109348186c7dSChuck Lever dprintk("NFS: %5u write (%s/%lld %d@%lld)", 109448186c7dSChuck Lever data->task.tk_pid, 109588be9f99STrond Myklebust req->wb_context->path.dentry->d_inode->i_sb->s_id, 109688be9f99STrond Myklebust (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 10971da177e4SLinus Torvalds req->wb_bytes, 10981da177e4SLinus Torvalds (long long)req_offset(req)); 10991da177e4SLinus Torvalds 1100c9d8f89dSTrond Myklebust if (status < 0) { 1101a301b777STrond Myklebust nfs_set_pageerror(page); 1102c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 1103c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 11048e821cadSTrond Myklebust goto remove_request; 11051da177e4SLinus Torvalds } 11061da177e4SLinus Torvalds 11078e821cadSTrond Myklebust if (nfs_write_need_commit(data)) { 11081da177e4SLinus Torvalds memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); 11091da177e4SLinus Torvalds nfs_mark_request_commit(req); 11101da177e4SLinus Torvalds dprintk(" marked for commit\n"); 11118e821cadSTrond Myklebust goto next; 11128e821cadSTrond Myklebust } 11138e821cadSTrond Myklebust dprintk(" OK\n"); 11148e821cadSTrond Myklebust remove_request: 11151da177e4SLinus Torvalds nfs_inode_remove_request(req); 11161da177e4SLinus Torvalds next: 11179fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 1118a6305ddbSTrond Myklebust nfs_end_page_writeback(page); 11191da177e4SLinus Torvalds } 1120c9d8f89dSTrond Myklebust nfs_writedata_release(calldata); 11211da177e4SLinus Torvalds } 11221da177e4SLinus Torvalds 1123788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = { 1124def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1) 1125def6ed7eSAndy Adamson .rpc_call_prepare = nfs_write_prepare, 1126def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1127788e7a89STrond Myklebust .rpc_call_done = nfs_writeback_done_full, 1128c9d8f89dSTrond Myklebust .rpc_release = nfs_writeback_release_full, 1129788e7a89STrond Myklebust }; 1130788e7a89STrond Myklebust 1131788e7a89STrond Myklebust 11321da177e4SLinus Torvalds /* 11331da177e4SLinus Torvalds * This function is called when the WRITE call is complete. 11341da177e4SLinus Torvalds */ 1135*13602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data) 11361da177e4SLinus Torvalds { 11371da177e4SLinus Torvalds struct nfs_writeargs *argp = &data->args; 11381da177e4SLinus Torvalds struct nfs_writeres *resp = &data->res; 1139eedc020eSAndy Adamson struct nfs_server *server = NFS_SERVER(data->inode); 1140788e7a89STrond Myklebust int status; 11411da177e4SLinus Torvalds 1142a3f565b1SChuck Lever dprintk("NFS: %5u nfs_writeback_done (status %d)\n", 11431da177e4SLinus Torvalds task->tk_pid, task->tk_status); 11441da177e4SLinus Torvalds 1145f551e44fSChuck Lever /* 1146f551e44fSChuck Lever * ->write_done will attempt to use post-op attributes to detect 1147f551e44fSChuck Lever * conflicting writes by other clients. A strict interpretation 1148f551e44fSChuck Lever * of close-to-open would allow us to continue caching even if 1149f551e44fSChuck Lever * another writer had changed the file, but some applications 1150f551e44fSChuck Lever * depend on tighter cache coherency when writing. 1151f551e44fSChuck Lever */ 1152788e7a89STrond Myklebust status = NFS_PROTO(data->inode)->write_done(task, data); 1153788e7a89STrond Myklebust if (status != 0) 1154*13602896SFred Isaman return; 115591d5b470SChuck Lever nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count); 115691d5b470SChuck Lever 11571da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 11581da177e4SLinus Torvalds if (resp->verf->committed < argp->stable && task->tk_status >= 0) { 11591da177e4SLinus Torvalds /* We tried a write call, but the server did not 11601da177e4SLinus Torvalds * commit data to stable storage even though we 11611da177e4SLinus Torvalds * requested it. 11621da177e4SLinus Torvalds * Note: There is a known bug in Tru64 < 5.0 in which 11631da177e4SLinus Torvalds * the server reports NFS_DATA_SYNC, but performs 11641da177e4SLinus Torvalds * NFS_FILE_SYNC. We therefore implement this checking 11651da177e4SLinus Torvalds * as a dprintk() in order to avoid filling syslog. 11661da177e4SLinus Torvalds */ 11671da177e4SLinus Torvalds static unsigned long complain; 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 11701da177e4SLinus Torvalds dprintk("NFS: faulty NFS server %s:" 11711da177e4SLinus Torvalds " (committed = %d) != (stable = %d)\n", 1172eedc020eSAndy Adamson server->nfs_client->cl_hostname, 11731da177e4SLinus Torvalds resp->verf->committed, argp->stable); 11741da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 11751da177e4SLinus Torvalds } 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds #endif 11781da177e4SLinus Torvalds /* Is this a short write? */ 11791da177e4SLinus Torvalds if (task->tk_status >= 0 && resp->count < argp->count) { 11801da177e4SLinus Torvalds static unsigned long complain; 11811da177e4SLinus Torvalds 118291d5b470SChuck Lever nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE); 118391d5b470SChuck Lever 11841da177e4SLinus Torvalds /* Has the server at least made some progress? */ 11851da177e4SLinus Torvalds if (resp->count != 0) { 11861da177e4SLinus Torvalds /* Was this an NFSv2 write or an NFSv3 stable write? */ 11871da177e4SLinus Torvalds if (resp->verf->committed != NFS_UNSTABLE) { 11881da177e4SLinus Torvalds /* Resend from where the server left off */ 11891da177e4SLinus Torvalds argp->offset += resp->count; 11901da177e4SLinus Torvalds argp->pgbase += resp->count; 11911da177e4SLinus Torvalds argp->count -= resp->count; 11921da177e4SLinus Torvalds } else { 11931da177e4SLinus Torvalds /* Resend as a stable write in order to avoid 11941da177e4SLinus Torvalds * headaches in the case of a server crash. 11951da177e4SLinus Torvalds */ 11961da177e4SLinus Torvalds argp->stable = NFS_FILE_SYNC; 11971da177e4SLinus Torvalds } 11980110ee15STrond Myklebust nfs_restart_rpc(task, server->nfs_client); 1199*13602896SFred Isaman return; 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 12021da177e4SLinus Torvalds printk(KERN_WARNING 12031da177e4SLinus Torvalds "NFS: Server wrote zero bytes, expected %u.\n", 12041da177e4SLinus Torvalds argp->count); 12051da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 12061da177e4SLinus Torvalds } 12071da177e4SLinus Torvalds /* Can't do anything about it except throw an error. */ 12081da177e4SLinus Torvalds task->tk_status = -EIO; 12091da177e4SLinus Torvalds } 1210*13602896SFred Isaman return; 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds 12131da177e4SLinus Torvalds 12141da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 121571d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait) 121671d0a611STrond Myklebust { 121771d0a611STrond Myklebust if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags)) 121871d0a611STrond Myklebust return 1; 121971d0a611STrond Myklebust if (may_wait && !out_of_line_wait_on_bit_lock(&nfsi->flags, 122071d0a611STrond Myklebust NFS_INO_COMMIT, nfs_wait_bit_killable, 122171d0a611STrond Myklebust TASK_KILLABLE)) 122271d0a611STrond Myklebust return 1; 122371d0a611STrond Myklebust return 0; 122471d0a611STrond Myklebust } 122571d0a611STrond Myklebust 122671d0a611STrond Myklebust static void nfs_commit_clear_lock(struct nfs_inode *nfsi) 122771d0a611STrond Myklebust { 122871d0a611STrond Myklebust clear_bit(NFS_INO_COMMIT, &nfsi->flags); 122971d0a611STrond Myklebust smp_mb__after_clear_bit(); 123071d0a611STrond Myklebust wake_up_bit(&nfsi->flags, NFS_INO_COMMIT); 123171d0a611STrond Myklebust } 123271d0a611STrond Myklebust 123371d0a611STrond Myklebust 12340aa05887SH Hartley Sweeten static void nfs_commitdata_release(void *data) 12351da177e4SLinus Torvalds { 1236383ba719STrond Myklebust struct nfs_write_data *wdata = data; 1237383ba719STrond Myklebust 1238383ba719STrond Myklebust put_nfs_open_context(wdata->args.context); 12391da177e4SLinus Torvalds nfs_commit_free(wdata); 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds 12421da177e4SLinus Torvalds /* 12431da177e4SLinus Torvalds * Set up the argument/result storage required for the RPC call. 12441da177e4SLinus Torvalds */ 1245dbae4c73STrond Myklebust static int nfs_commit_rpcsetup(struct list_head *head, 1246788e7a89STrond Myklebust struct nfs_write_data *data, 1247788e7a89STrond Myklebust int how) 12481da177e4SLinus Torvalds { 124984115e1cSTrond Myklebust struct nfs_page *first = nfs_list_entry(head->next); 125084115e1cSTrond Myklebust struct inode *inode = first->wb_context->path.dentry->d_inode; 12513ff7576dSTrond Myklebust int priority = flush_task_priority(how); 125207737691STrond Myklebust struct rpc_task *task; 1253bdc7f021STrond Myklebust struct rpc_message msg = { 1254bdc7f021STrond Myklebust .rpc_argp = &data->args, 1255bdc7f021STrond Myklebust .rpc_resp = &data->res, 1256bdc7f021STrond Myklebust .rpc_cred = first->wb_context->cred, 1257bdc7f021STrond Myklebust }; 125884115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 125907737691STrond Myklebust .task = &data->task, 126084115e1cSTrond Myklebust .rpc_client = NFS_CLIENT(inode), 1261bdc7f021STrond Myklebust .rpc_message = &msg, 126284115e1cSTrond Myklebust .callback_ops = &nfs_commit_ops, 126384115e1cSTrond Myklebust .callback_data = data, 1264101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 12652c61be0aSTrond Myklebust .flags = RPC_TASK_ASYNC, 12663ff7576dSTrond Myklebust .priority = priority, 126784115e1cSTrond Myklebust }; 12681da177e4SLinus Torvalds 12691da177e4SLinus Torvalds /* Set up the RPC argument and reply structs 12701da177e4SLinus Torvalds * NB: take care not to mess about with data->commit et al. */ 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds list_splice_init(head, &data->pages); 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds data->inode = inode; 1275bdc7f021STrond Myklebust data->cred = msg.rpc_cred; 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds data->args.fh = NFS_FH(data->inode); 12783da28eb1STrond Myklebust /* Note: we always request a commit of the entire inode */ 12793da28eb1STrond Myklebust data->args.offset = 0; 12803da28eb1STrond Myklebust data->args.count = 0; 1281383ba719STrond Myklebust data->args.context = get_nfs_open_context(first->wb_context); 12823da28eb1STrond Myklebust data->res.count = 0; 12831da177e4SLinus Torvalds data->res.fattr = &data->fattr; 12841da177e4SLinus Torvalds data->res.verf = &data->verf; 12850e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 12861da177e4SLinus Torvalds 1287788e7a89STrond Myklebust /* Set up the initial task struct. */ 1288bdc7f021STrond Myklebust NFS_PROTO(inode)->commit_setup(data, &msg); 12891da177e4SLinus Torvalds 1290a3f565b1SChuck Lever dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); 1291bdc7f021STrond Myklebust 129207737691STrond Myklebust task = rpc_run_task(&task_setup_data); 1293dbae4c73STrond Myklebust if (IS_ERR(task)) 1294dbae4c73STrond Myklebust return PTR_ERR(task); 1295d2224e7aSJeff Layton if (how & FLUSH_SYNC) 1296d2224e7aSJeff Layton rpc_wait_for_completion_task(task); 129707737691STrond Myklebust rpc_put_task(task); 1298dbae4c73STrond Myklebust return 0; 12991da177e4SLinus Torvalds } 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds /* 13021da177e4SLinus Torvalds * Commit dirty pages 13031da177e4SLinus Torvalds */ 13041da177e4SLinus Torvalds static int 130540859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how) 13061da177e4SLinus Torvalds { 13071da177e4SLinus Torvalds struct nfs_write_data *data; 13081da177e4SLinus Torvalds struct nfs_page *req; 13091da177e4SLinus Torvalds 1310c9d8f89dSTrond Myklebust data = nfs_commitdata_alloc(); 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds if (!data) 13131da177e4SLinus Torvalds goto out_bad; 13141da177e4SLinus Torvalds 13151da177e4SLinus Torvalds /* Set up the argument struct */ 1316dbae4c73STrond Myklebust return nfs_commit_rpcsetup(head, data, how); 13171da177e4SLinus Torvalds out_bad: 13181da177e4SLinus Torvalds while (!list_empty(head)) { 13191da177e4SLinus Torvalds req = nfs_list_entry(head->next); 13201da177e4SLinus Torvalds nfs_list_remove_request(req); 13211da177e4SLinus Torvalds nfs_mark_request_commit(req); 132283715ad5STrond Myklebust dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 1323c9e51e41SPeter Zijlstra dec_bdi_stat(req->wb_page->mapping->backing_dev_info, 1324c9e51e41SPeter Zijlstra BDI_RECLAIMABLE); 13259fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 13261da177e4SLinus Torvalds } 132771d0a611STrond Myklebust nfs_commit_clear_lock(NFS_I(inode)); 13281da177e4SLinus Torvalds return -ENOMEM; 13291da177e4SLinus Torvalds } 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds /* 13321da177e4SLinus Torvalds * COMMIT call returned 13331da177e4SLinus Torvalds */ 1334788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata) 13351da177e4SLinus Torvalds { 1336963d8fe5STrond Myklebust struct nfs_write_data *data = calldata; 13371da177e4SLinus Torvalds 1338a3f565b1SChuck Lever dprintk("NFS: %5u nfs_commit_done (status %d)\n", 13391da177e4SLinus Torvalds task->tk_pid, task->tk_status); 13401da177e4SLinus Torvalds 1341788e7a89STrond Myklebust /* Call the NFS version-specific code */ 1342788e7a89STrond Myklebust if (NFS_PROTO(data->inode)->commit_done(task, data) != 0) 1343788e7a89STrond Myklebust return; 1344c9d8f89dSTrond Myklebust } 1345c9d8f89dSTrond Myklebust 1346c9d8f89dSTrond Myklebust static void nfs_commit_release(void *calldata) 1347c9d8f89dSTrond Myklebust { 1348c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1349c9d8f89dSTrond Myklebust struct nfs_page *req; 1350c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1351788e7a89STrond Myklebust 13521da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 13531da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 13541da177e4SLinus Torvalds nfs_list_remove_request(req); 1355e468bae9STrond Myklebust nfs_clear_request_commit(req); 13561da177e4SLinus Torvalds 135748186c7dSChuck Lever dprintk("NFS: commit (%s/%lld %d@%lld)", 135888be9f99STrond Myklebust req->wb_context->path.dentry->d_inode->i_sb->s_id, 135988be9f99STrond Myklebust (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 13601da177e4SLinus Torvalds req->wb_bytes, 13611da177e4SLinus Torvalds (long long)req_offset(req)); 1362c9d8f89dSTrond Myklebust if (status < 0) { 1363c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 13641da177e4SLinus Torvalds nfs_inode_remove_request(req); 1365c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 13661da177e4SLinus Torvalds goto next; 13671da177e4SLinus Torvalds } 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds /* Okay, COMMIT succeeded, apparently. Check the verifier 13701da177e4SLinus Torvalds * returned by the server against all stored verfs. */ 13711da177e4SLinus Torvalds if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) { 13721da177e4SLinus Torvalds /* We have a match */ 13731da177e4SLinus Torvalds nfs_inode_remove_request(req); 13741da177e4SLinus Torvalds dprintk(" OK\n"); 13751da177e4SLinus Torvalds goto next; 13761da177e4SLinus Torvalds } 13771da177e4SLinus Torvalds /* We have a mismatch. Write the page again */ 13781da177e4SLinus Torvalds dprintk(" mismatch\n"); 13796d884e8fSFred nfs_mark_request_dirty(req); 13801da177e4SLinus Torvalds next: 13819fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 13821da177e4SLinus Torvalds } 138371d0a611STrond Myklebust nfs_commit_clear_lock(NFS_I(data->inode)); 1384c9d8f89dSTrond Myklebust nfs_commitdata_release(calldata); 13851da177e4SLinus Torvalds } 1386788e7a89STrond Myklebust 1387788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = { 138821d9a851SAndy Adamson #if defined(CONFIG_NFS_V4_1) 138921d9a851SAndy Adamson .rpc_call_prepare = nfs_write_prepare, 139021d9a851SAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1391788e7a89STrond Myklebust .rpc_call_done = nfs_commit_done, 1392788e7a89STrond Myklebust .rpc_release = nfs_commit_release, 1393788e7a89STrond Myklebust }; 13941da177e4SLinus Torvalds 1395b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how) 13961da177e4SLinus Torvalds { 13971da177e4SLinus Torvalds LIST_HEAD(head); 139871d0a611STrond Myklebust int may_wait = how & FLUSH_SYNC; 139971d0a611STrond Myklebust int res = 0; 14001da177e4SLinus Torvalds 140171d0a611STrond Myklebust if (!nfs_commit_set_lock(NFS_I(inode), may_wait)) 1402c5efa5fcSTrond Myklebust goto out_mark_dirty; 1403587142f8STrond Myklebust spin_lock(&inode->i_lock); 14043da28eb1STrond Myklebust res = nfs_scan_commit(inode, &head, 0, 0); 1405587142f8STrond Myklebust spin_unlock(&inode->i_lock); 14061da177e4SLinus Torvalds if (res) { 14077d46a49fSTrond Myklebust int error = nfs_commit_list(inode, &head, how); 14081da177e4SLinus Torvalds if (error < 0) 14091da177e4SLinus Torvalds return error; 141071d0a611STrond Myklebust if (may_wait) 141171d0a611STrond Myklebust wait_on_bit(&NFS_I(inode)->flags, NFS_INO_COMMIT, 141271d0a611STrond Myklebust nfs_wait_bit_killable, 141371d0a611STrond Myklebust TASK_KILLABLE); 1414c5efa5fcSTrond Myklebust else 1415c5efa5fcSTrond Myklebust goto out_mark_dirty; 141671d0a611STrond Myklebust } else 141771d0a611STrond Myklebust nfs_commit_clear_lock(NFS_I(inode)); 1418c5efa5fcSTrond Myklebust return res; 1419c5efa5fcSTrond Myklebust /* Note: If we exit without ensuring that the commit is complete, 1420c5efa5fcSTrond Myklebust * we must mark the inode as dirty. Otherwise, future calls to 1421c5efa5fcSTrond Myklebust * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure 1422c5efa5fcSTrond Myklebust * that the data is on the disk. 1423c5efa5fcSTrond Myklebust */ 1424c5efa5fcSTrond Myklebust out_mark_dirty: 1425c5efa5fcSTrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 14261da177e4SLinus Torvalds return res; 14271da177e4SLinus Torvalds } 14288fc795f7STrond Myklebust 14298fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 14308fc795f7STrond Myklebust { 1431420e3646STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 1432420e3646STrond Myklebust int flags = FLUSH_SYNC; 1433420e3646STrond Myklebust int ret = 0; 14348fc795f7STrond Myklebust 1435a00dd6c0SJeff Layton if (wbc->sync_mode == WB_SYNC_NONE) { 1436a00dd6c0SJeff Layton /* Don't commit yet if this is a non-blocking flush and there 1437a00dd6c0SJeff Layton * are a lot of outstanding writes for this mapping. 1438420e3646STrond Myklebust */ 1439a00dd6c0SJeff Layton if (nfsi->ncommit <= (nfsi->npages >> 1)) 1440420e3646STrond Myklebust goto out_mark_dirty; 1441420e3646STrond Myklebust 1442a00dd6c0SJeff Layton /* don't wait for the COMMIT response */ 1443420e3646STrond Myklebust flags = 0; 1444a00dd6c0SJeff Layton } 1445a00dd6c0SJeff Layton 1446420e3646STrond Myklebust ret = nfs_commit_inode(inode, flags); 1447420e3646STrond Myklebust if (ret >= 0) { 1448420e3646STrond Myklebust if (wbc->sync_mode == WB_SYNC_NONE) { 1449420e3646STrond Myklebust if (ret < wbc->nr_to_write) 1450420e3646STrond Myklebust wbc->nr_to_write -= ret; 1451420e3646STrond Myklebust else 1452420e3646STrond Myklebust wbc->nr_to_write = 0; 1453420e3646STrond Myklebust } 14548fc795f7STrond Myklebust return 0; 1455420e3646STrond Myklebust } 1456420e3646STrond Myklebust out_mark_dirty: 14578fc795f7STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 14588fc795f7STrond Myklebust return ret; 14598fc795f7STrond Myklebust } 1460c63c7b05STrond Myklebust #else 14618fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 14628fc795f7STrond Myklebust { 14638fc795f7STrond Myklebust return 0; 14648fc795f7STrond Myklebust } 14651da177e4SLinus Torvalds #endif 14661da177e4SLinus Torvalds 14678fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc) 14688fc795f7STrond Myklebust { 14698fc795f7STrond Myklebust return nfs_commit_unstable_pages(inode, wbc); 14708fc795f7STrond Myklebust } 14718fc795f7STrond Myklebust 1472acdc53b2STrond Myklebust /* 1473acdc53b2STrond Myklebust * flush the inode to disk. 1474acdc53b2STrond Myklebust */ 1475acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode) 147634901f70STrond Myklebust { 147734901f70STrond Myklebust struct writeback_control wbc = { 147872cb77f4STrond Myklebust .sync_mode = WB_SYNC_ALL, 147934901f70STrond Myklebust .nr_to_write = LONG_MAX, 1480d7fb1207STrond Myklebust .range_start = 0, 1481d7fb1207STrond Myklebust .range_end = LLONG_MAX, 148234901f70STrond Myklebust }; 148334901f70STrond Myklebust 1484acdc53b2STrond Myklebust return sync_inode(inode, &wbc); 14851c75950bSTrond Myklebust } 14861c75950bSTrond Myklebust 14871b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page) 14881b3b4a1aSTrond Myklebust { 14891b3b4a1aSTrond Myklebust struct nfs_page *req; 14901b3b4a1aSTrond Myklebust int ret = 0; 14911b3b4a1aSTrond Myklebust 14921b3b4a1aSTrond Myklebust BUG_ON(!PageLocked(page)); 14931b3b4a1aSTrond Myklebust for (;;) { 1494ba8b06e6STrond Myklebust wait_on_page_writeback(page); 14951b3b4a1aSTrond Myklebust req = nfs_page_find_request(page); 14961b3b4a1aSTrond Myklebust if (req == NULL) 14971b3b4a1aSTrond Myklebust break; 14981b3b4a1aSTrond Myklebust if (nfs_lock_request_dontget(req)) { 14991b3b4a1aSTrond Myklebust nfs_inode_remove_request(req); 15001b3b4a1aSTrond Myklebust /* 15011b3b4a1aSTrond Myklebust * In case nfs_inode_remove_request has marked the 15021b3b4a1aSTrond Myklebust * page as being dirty 15031b3b4a1aSTrond Myklebust */ 15041b3b4a1aSTrond Myklebust cancel_dirty_page(page, PAGE_CACHE_SIZE); 15051b3b4a1aSTrond Myklebust nfs_unlock_request(req); 15061b3b4a1aSTrond Myklebust break; 15071b3b4a1aSTrond Myklebust } 15081b3b4a1aSTrond Myklebust ret = nfs_wait_on_request(req); 1509c9edda71STrond Myklebust nfs_release_request(req); 15101b3b4a1aSTrond Myklebust if (ret < 0) 1511c988950eSTrond Myklebust break; 15121b3b4a1aSTrond Myklebust } 15131b3b4a1aSTrond Myklebust return ret; 15141b3b4a1aSTrond Myklebust } 15151b3b4a1aSTrond Myklebust 15161c75950bSTrond Myklebust /* 15171c75950bSTrond Myklebust * Write back all requests on one page - we do this before reading it. 15181c75950bSTrond Myklebust */ 15191c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page) 15201c75950bSTrond Myklebust { 15217f2f12d9STrond Myklebust loff_t range_start = page_offset(page); 15227f2f12d9STrond Myklebust loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); 15237f2f12d9STrond Myklebust struct writeback_control wbc = { 15247f2f12d9STrond Myklebust .sync_mode = WB_SYNC_ALL, 15257f2f12d9STrond Myklebust .nr_to_write = 0, 15267f2f12d9STrond Myklebust .range_start = range_start, 15277f2f12d9STrond Myklebust .range_end = range_end, 15287f2f12d9STrond Myklebust }; 15297f2f12d9STrond Myklebust int ret; 15307f2f12d9STrond Myklebust 15310522f6adSTrond Myklebust for (;;) { 1532ba8b06e6STrond Myklebust wait_on_page_writeback(page); 15337f2f12d9STrond Myklebust if (clear_page_dirty_for_io(page)) { 15347f2f12d9STrond Myklebust ret = nfs_writepage_locked(page, &wbc); 15357f2f12d9STrond Myklebust if (ret < 0) 15367f2f12d9STrond Myklebust goto out_error; 15370522f6adSTrond Myklebust continue; 15387f2f12d9STrond Myklebust } 15390522f6adSTrond Myklebust if (!PagePrivate(page)) 15400522f6adSTrond Myklebust break; 15410522f6adSTrond Myklebust ret = nfs_commit_inode(inode, FLUSH_SYNC); 15427f2f12d9STrond Myklebust if (ret < 0) 15437f2f12d9STrond Myklebust goto out_error; 15447f2f12d9STrond Myklebust } 15457f2f12d9STrond Myklebust return 0; 15467f2f12d9STrond Myklebust out_error: 15477f2f12d9STrond Myklebust return ret; 15481c75950bSTrond Myklebust } 15491c75950bSTrond Myklebust 1550074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION 1551074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage, 1552074cc1deSTrond Myklebust struct page *page) 1553074cc1deSTrond Myklebust { 1554074cc1deSTrond Myklebust struct nfs_page *req; 1555074cc1deSTrond Myklebust int ret; 1556074cc1deSTrond Myklebust 1557074cc1deSTrond Myklebust nfs_fscache_release_page(page, GFP_KERNEL); 1558074cc1deSTrond Myklebust 1559cfb506e1STrond Myklebust req = nfs_find_and_lock_request(page, false); 1560074cc1deSTrond Myklebust ret = PTR_ERR(req); 1561074cc1deSTrond Myklebust if (IS_ERR(req)) 1562074cc1deSTrond Myklebust goto out; 1563074cc1deSTrond Myklebust 1564074cc1deSTrond Myklebust ret = migrate_page(mapping, newpage, page); 1565074cc1deSTrond Myklebust if (!req) 1566074cc1deSTrond Myklebust goto out; 1567074cc1deSTrond Myklebust if (ret) 1568074cc1deSTrond Myklebust goto out_unlock; 1569074cc1deSTrond Myklebust page_cache_get(newpage); 1570190f38e5STrond Myklebust spin_lock(&mapping->host->i_lock); 1571074cc1deSTrond Myklebust req->wb_page = newpage; 1572074cc1deSTrond Myklebust SetPagePrivate(newpage); 1573190f38e5STrond Myklebust set_page_private(newpage, (unsigned long)req); 1574074cc1deSTrond Myklebust ClearPagePrivate(page); 1575074cc1deSTrond Myklebust set_page_private(page, 0); 1576190f38e5STrond Myklebust spin_unlock(&mapping->host->i_lock); 1577074cc1deSTrond Myklebust page_cache_release(page); 1578074cc1deSTrond Myklebust out_unlock: 1579074cc1deSTrond Myklebust nfs_clear_page_tag_locked(req); 1580074cc1deSTrond Myklebust out: 1581074cc1deSTrond Myklebust return ret; 1582074cc1deSTrond Myklebust } 1583074cc1deSTrond Myklebust #endif 1584074cc1deSTrond 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