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" 3194ad1c80SFred Isaman #include "pnfs.h" 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_PAGECACHE 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds #define MIN_POOL_WRITE (32) 361da177e4SLinus Torvalds #define MIN_POOL_COMMIT (4) 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds /* 391da177e4SLinus Torvalds * Local function declarations 401da177e4SLinus Torvalds */ 41c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc, 42c63c7b05STrond Myklebust struct inode *inode, int ioflags); 43f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req); 44788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops; 45788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops; 46788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops; 471da177e4SLinus Torvalds 48e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep; 493feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool; 501da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool; 511da177e4SLinus Torvalds 52c9d8f89dSTrond Myklebust struct nfs_write_data *nfs_commitdata_alloc(void) 531da177e4SLinus Torvalds { 54e6b4f8daSChristoph Lameter struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS); 5540859d7eSChuck Lever 561da177e4SLinus Torvalds if (p) { 571da177e4SLinus Torvalds memset(p, 0, sizeof(*p)); 581da177e4SLinus Torvalds INIT_LIST_HEAD(&p->pages); 591da177e4SLinus Torvalds } 601da177e4SLinus Torvalds return p; 611da177e4SLinus Torvalds } 621da177e4SLinus Torvalds 635e4424afSTrond Myklebust void nfs_commit_free(struct nfs_write_data *p) 641da177e4SLinus Torvalds { 6540859d7eSChuck Lever if (p && (p->pagevec != &p->page_array[0])) 6640859d7eSChuck Lever kfree(p->pagevec); 671da177e4SLinus Torvalds mempool_free(p, nfs_commit_mempool); 681da177e4SLinus Torvalds } 691da177e4SLinus Torvalds 708d5658c9STrond Myklebust struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount) 713feb2d49STrond Myklebust { 72e6b4f8daSChristoph Lameter struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS); 733feb2d49STrond Myklebust 743feb2d49STrond Myklebust if (p) { 753feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 763feb2d49STrond Myklebust INIT_LIST_HEAD(&p->pages); 77e9f7bee1STrond Myklebust p->npages = pagecount; 780d0b5cb3SChuck Lever if (pagecount <= ARRAY_SIZE(p->page_array)) 790d0b5cb3SChuck Lever p->pagevec = p->page_array; 803feb2d49STrond Myklebust else { 810d0b5cb3SChuck Lever p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS); 820d0b5cb3SChuck Lever if (!p->pagevec) { 833feb2d49STrond Myklebust mempool_free(p, nfs_wdata_mempool); 843feb2d49STrond Myklebust p = NULL; 853feb2d49STrond Myklebust } 863feb2d49STrond Myklebust } 873feb2d49STrond Myklebust } 883feb2d49STrond Myklebust return p; 893feb2d49STrond Myklebust } 903feb2d49STrond Myklebust 911ae88b2eSTrond Myklebust void nfs_writedata_free(struct nfs_write_data *p) 923feb2d49STrond Myklebust { 933feb2d49STrond Myklebust if (p && (p->pagevec != &p->page_array[0])) 943feb2d49STrond Myklebust kfree(p->pagevec); 953feb2d49STrond Myklebust mempool_free(p, nfs_wdata_mempool); 963feb2d49STrond Myklebust } 973feb2d49STrond Myklebust 981ae88b2eSTrond Myklebust static void nfs_writedata_release(struct nfs_write_data *wdata) 991da177e4SLinus Torvalds { 100383ba719STrond Myklebust put_nfs_open_context(wdata->args.context); 1011da177e4SLinus Torvalds nfs_writedata_free(wdata); 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds 1047b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error) 1057b159fc1STrond Myklebust { 1067b159fc1STrond Myklebust ctx->error = error; 1077b159fc1STrond Myklebust smp_wmb(); 1087b159fc1STrond Myklebust set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 1097b159fc1STrond Myklebust } 1107b159fc1STrond Myklebust 111277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page) 112277459d2STrond Myklebust { 113277459d2STrond Myklebust struct nfs_page *req = NULL; 114277459d2STrond Myklebust 115277459d2STrond Myklebust if (PagePrivate(page)) { 116277459d2STrond Myklebust req = (struct nfs_page *)page_private(page); 117277459d2STrond Myklebust if (req != NULL) 118c03b4024STrond Myklebust kref_get(&req->wb_kref); 119277459d2STrond Myklebust } 120277459d2STrond Myklebust return req; 121277459d2STrond Myklebust } 122277459d2STrond Myklebust 123277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page) 124277459d2STrond Myklebust { 125587142f8STrond Myklebust struct inode *inode = page->mapping->host; 126277459d2STrond Myklebust struct nfs_page *req = NULL; 127277459d2STrond Myklebust 128587142f8STrond Myklebust spin_lock(&inode->i_lock); 129277459d2STrond Myklebust req = nfs_page_find_request_locked(page); 130587142f8STrond Myklebust spin_unlock(&inode->i_lock); 131277459d2STrond Myklebust return req; 132277459d2STrond Myklebust } 133277459d2STrond Myklebust 1341da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */ 1351da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) 1361da177e4SLinus Torvalds { 1371da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 138a3d01454STrond Myklebust loff_t end, i_size; 139a3d01454STrond Myklebust pgoff_t end_index; 1401da177e4SLinus Torvalds 141a3d01454STrond Myklebust spin_lock(&inode->i_lock); 142a3d01454STrond Myklebust i_size = i_size_read(inode); 143a3d01454STrond Myklebust end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; 1441da177e4SLinus Torvalds if (i_size > 0 && page->index < end_index) 145a3d01454STrond Myklebust goto out; 1461da177e4SLinus Torvalds end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count); 1471da177e4SLinus Torvalds if (i_size >= end) 148a3d01454STrond Myklebust goto out; 1491da177e4SLinus Torvalds i_size_write(inode, end); 150a3d01454STrond Myklebust nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); 151a3d01454STrond Myklebust out: 152a3d01454STrond Myklebust spin_unlock(&inode->i_lock); 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 155a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */ 156a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page) 157a301b777STrond Myklebust { 158a301b777STrond Myklebust SetPageError(page); 159a301b777STrond Myklebust nfs_zap_mapping(page->mapping->host, page->mapping); 160a301b777STrond Myklebust } 161a301b777STrond Myklebust 1621da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request 1631da177e4SLinus Torvalds * covers the full page. 1641da177e4SLinus Torvalds */ 1651da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count) 1661da177e4SLinus Torvalds { 1671da177e4SLinus Torvalds if (PageUptodate(page)) 1681da177e4SLinus Torvalds return; 1691da177e4SLinus Torvalds if (base != 0) 1701da177e4SLinus Torvalds return; 17149a70f27STrond Myklebust if (count != nfs_page_length(page)) 1721da177e4SLinus Torvalds return; 1731da177e4SLinus Torvalds SetPageUptodate(page); 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc) 1771da177e4SLinus Torvalds { 1781da177e4SLinus Torvalds if (wbc->for_reclaim) 179c63c7b05STrond Myklebust return FLUSH_HIGHPRI | FLUSH_STABLE; 180b17621feSWu Fengguang if (wbc->for_kupdate || wbc->for_background) 1811da177e4SLinus Torvalds return FLUSH_LOWPRI; 1821da177e4SLinus Torvalds return 0; 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds /* 18689a09141SPeter Zijlstra * NFS congestion control 18789a09141SPeter Zijlstra */ 18889a09141SPeter Zijlstra 18989a09141SPeter Zijlstra int nfs_congestion_kb; 19089a09141SPeter Zijlstra 19189a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) 19289a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH \ 19389a09141SPeter Zijlstra (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) 19489a09141SPeter Zijlstra 1955a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page) 19689a09141SPeter Zijlstra { 1975a6d41b3STrond Myklebust int ret = test_set_page_writeback(page); 1985a6d41b3STrond Myklebust 1995a6d41b3STrond Myklebust if (!ret) { 20089a09141SPeter Zijlstra struct inode *inode = page->mapping->host; 20189a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 20289a09141SPeter Zijlstra 203a6305ddbSTrond Myklebust page_cache_get(page); 204277866a0SPeter Zijlstra if (atomic_long_inc_return(&nfss->writeback) > 2058aa7e847SJens Axboe NFS_CONGESTION_ON_THRESH) { 2068aa7e847SJens Axboe set_bdi_congested(&nfss->backing_dev_info, 2078aa7e847SJens Axboe BLK_RW_ASYNC); 2088aa7e847SJens 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); 219a6305ddbSTrond Myklebust page_cache_release(page); 220c4dc4beeSPeter Zijlstra if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) 2218aa7e847SJens Axboe clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC); 22289a09141SPeter Zijlstra } 22389a09141SPeter Zijlstra 224cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock) 225e261f51fSTrond Myklebust { 226587142f8STrond Myklebust struct inode *inode = page->mapping->host; 227e261f51fSTrond Myklebust struct nfs_page *req; 228e261f51fSTrond Myklebust int ret; 229e261f51fSTrond Myklebust 230587142f8STrond Myklebust spin_lock(&inode->i_lock); 231e261f51fSTrond Myklebust for (;;) { 232e261f51fSTrond Myklebust req = nfs_page_find_request_locked(page); 233074cc1deSTrond Myklebust if (req == NULL) 234074cc1deSTrond Myklebust break; 235acee478aSTrond Myklebust if (nfs_set_page_tag_locked(req)) 236e261f51fSTrond Myklebust break; 237e261f51fSTrond Myklebust /* Note: If we hold the page lock, as is the case in nfs_writepage, 238acee478aSTrond Myklebust * then the call to nfs_set_page_tag_locked() will always 239e261f51fSTrond Myklebust * succeed provided that someone hasn't already marked the 240e261f51fSTrond Myklebust * request as dirty (in which case we don't care). 241e261f51fSTrond Myklebust */ 242587142f8STrond Myklebust spin_unlock(&inode->i_lock); 243cfb506e1STrond Myklebust if (!nonblock) 244e261f51fSTrond Myklebust ret = nfs_wait_on_request(req); 245cfb506e1STrond Myklebust else 246cfb506e1STrond Myklebust ret = -EAGAIN; 247e261f51fSTrond Myklebust nfs_release_request(req); 248e261f51fSTrond Myklebust if (ret != 0) 249074cc1deSTrond Myklebust return ERR_PTR(ret); 250587142f8STrond Myklebust spin_lock(&inode->i_lock); 251e261f51fSTrond Myklebust } 252587142f8STrond Myklebust spin_unlock(&inode->i_lock); 253074cc1deSTrond Myklebust return req; 254612c9384STrond Myklebust } 255074cc1deSTrond Myklebust 256074cc1deSTrond Myklebust /* 257074cc1deSTrond Myklebust * Find an associated nfs write request, and prepare to flush it out 258074cc1deSTrond Myklebust * May return an error if the user signalled nfs_wait_on_request(). 259074cc1deSTrond Myklebust */ 260074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, 261cfb506e1STrond Myklebust struct page *page, bool nonblock) 262074cc1deSTrond Myklebust { 263074cc1deSTrond Myklebust struct nfs_page *req; 264074cc1deSTrond Myklebust int ret = 0; 265074cc1deSTrond Myklebust 266cfb506e1STrond Myklebust req = nfs_find_and_lock_request(page, nonblock); 267074cc1deSTrond Myklebust if (!req) 268074cc1deSTrond Myklebust goto out; 269074cc1deSTrond Myklebust ret = PTR_ERR(req); 270074cc1deSTrond Myklebust if (IS_ERR(req)) 271074cc1deSTrond Myklebust goto out; 272074cc1deSTrond Myklebust 273074cc1deSTrond Myklebust ret = nfs_set_page_writeback(page); 274074cc1deSTrond Myklebust BUG_ON(ret != 0); 275074cc1deSTrond Myklebust BUG_ON(test_bit(PG_CLEAN, &req->wb_flags)); 276074cc1deSTrond Myklebust 277f8512ad0SFred Isaman if (!nfs_pageio_add_request(pgio, req)) { 278f8512ad0SFred Isaman nfs_redirty_request(req); 279074cc1deSTrond Myklebust ret = pgio->pg_error; 280f8512ad0SFred Isaman } 281074cc1deSTrond Myklebust out: 282074cc1deSTrond Myklebust return ret; 283e261f51fSTrond Myklebust } 284e261f51fSTrond Myklebust 285f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio) 286f758c885STrond Myklebust { 287f758c885STrond Myklebust struct inode *inode = page->mapping->host; 288cfb506e1STrond Myklebust int ret; 289f758c885STrond Myklebust 290f758c885STrond Myklebust nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); 291f758c885STrond Myklebust nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); 292f758c885STrond Myklebust 293f758c885STrond Myklebust nfs_pageio_cond_complete(pgio, page->index); 2941b430beeSWu Fengguang ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE); 295cfb506e1STrond Myklebust if (ret == -EAGAIN) { 296cfb506e1STrond Myklebust redirty_page_for_writepage(wbc, page); 297cfb506e1STrond Myklebust ret = 0; 298cfb506e1STrond Myklebust } 299cfb506e1STrond Myklebust return ret; 300f758c885STrond Myklebust } 301f758c885STrond Myklebust 302e261f51fSTrond Myklebust /* 3031da177e4SLinus Torvalds * Write an mmapped page to the server. 3041da177e4SLinus Torvalds */ 3054d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc) 3061da177e4SLinus Torvalds { 307f758c885STrond Myklebust struct nfs_pageio_descriptor pgio; 308e261f51fSTrond Myklebust int err; 3091da177e4SLinus Torvalds 310f758c885STrond Myklebust nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc)); 311f758c885STrond Myklebust err = nfs_do_writepage(page, wbc, &pgio); 312f758c885STrond Myklebust nfs_pageio_complete(&pgio); 313f758c885STrond Myklebust if (err < 0) 3144d770ccfSTrond Myklebust return err; 315f758c885STrond Myklebust if (pgio.pg_error < 0) 316f758c885STrond Myklebust return pgio.pg_error; 317f758c885STrond Myklebust return 0; 3184d770ccfSTrond Myklebust } 3194d770ccfSTrond Myklebust 3204d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc) 3214d770ccfSTrond Myklebust { 322f758c885STrond Myklebust int ret; 3234d770ccfSTrond Myklebust 324f758c885STrond Myklebust ret = nfs_writepage_locked(page, wbc); 3251da177e4SLinus Torvalds unlock_page(page); 326f758c885STrond Myklebust return ret; 327f758c885STrond Myklebust } 328f758c885STrond Myklebust 329f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) 330f758c885STrond Myklebust { 331f758c885STrond Myklebust int ret; 332f758c885STrond Myklebust 333f758c885STrond Myklebust ret = nfs_do_writepage(page, wbc, data); 334f758c885STrond Myklebust unlock_page(page); 335f758c885STrond Myklebust return ret; 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) 3391da177e4SLinus Torvalds { 3401da177e4SLinus Torvalds struct inode *inode = mapping->host; 34172cb77f4STrond Myklebust unsigned long *bitlock = &NFS_I(inode)->flags; 342c63c7b05STrond Myklebust struct nfs_pageio_descriptor pgio; 3431da177e4SLinus Torvalds int err; 3441da177e4SLinus Torvalds 34572cb77f4STrond Myklebust /* Stop dirtying of new pages while we sync */ 34672cb77f4STrond Myklebust err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING, 34772cb77f4STrond Myklebust nfs_wait_bit_killable, TASK_KILLABLE); 34872cb77f4STrond Myklebust if (err) 34972cb77f4STrond Myklebust goto out_err; 35072cb77f4STrond Myklebust 35191d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); 35291d5b470SChuck Lever 353c63c7b05STrond Myklebust nfs_pageio_init_write(&pgio, inode, wb_priority(wbc)); 354f758c885STrond Myklebust err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); 355c63c7b05STrond Myklebust nfs_pageio_complete(&pgio); 35672cb77f4STrond Myklebust 35772cb77f4STrond Myklebust clear_bit_unlock(NFS_INO_FLUSHING, bitlock); 35872cb77f4STrond Myklebust smp_mb__after_clear_bit(); 35972cb77f4STrond Myklebust wake_up_bit(bitlock, NFS_INO_FLUSHING); 36072cb77f4STrond Myklebust 361f758c885STrond Myklebust if (err < 0) 36272cb77f4STrond Myklebust goto out_err; 36372cb77f4STrond Myklebust err = pgio.pg_error; 36472cb77f4STrond Myklebust if (err < 0) 36572cb77f4STrond Myklebust goto out_err; 366c63c7b05STrond Myklebust return 0; 36772cb77f4STrond Myklebust out_err: 36872cb77f4STrond Myklebust return err; 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds /* 3721da177e4SLinus Torvalds * Insert a write request into an inode 3731da177e4SLinus Torvalds */ 374e7d39069STrond Myklebust static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req) 3751da177e4SLinus Torvalds { 3761da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 3771da177e4SLinus Torvalds int error; 3781da177e4SLinus Torvalds 379e7d39069STrond Myklebust error = radix_tree_preload(GFP_NOFS); 380e7d39069STrond Myklebust if (error != 0) 381e7d39069STrond Myklebust goto out; 382e7d39069STrond Myklebust 383e7d39069STrond Myklebust /* Lock the request! */ 384e7d39069STrond Myklebust nfs_lock_request_dontget(req); 385e7d39069STrond Myklebust 386e7d39069STrond Myklebust spin_lock(&inode->i_lock); 3871da177e4SLinus Torvalds error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req); 38827852596SNick Piggin BUG_ON(error); 3891da177e4SLinus Torvalds if (!nfsi->npages) { 3901da177e4SLinus Torvalds igrab(inode); 3911da177e4SLinus Torvalds if (nfs_have_delegation(inode, FMODE_WRITE)) 3921da177e4SLinus Torvalds nfsi->change_attr++; 3931da177e4SLinus Torvalds } 3942df485a7STrond Myklebust set_bit(PG_MAPPED, &req->wb_flags); 395deb7d638STrond Myklebust SetPagePrivate(req->wb_page); 396277459d2STrond Myklebust set_page_private(req->wb_page, (unsigned long)req); 3971da177e4SLinus Torvalds nfsi->npages++; 398c03b4024STrond Myklebust kref_get(&req->wb_kref); 39927852596SNick Piggin radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, 40027852596SNick Piggin NFS_PAGE_TAG_LOCKED); 401e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 402e7d39069STrond Myklebust radix_tree_preload_end(); 403e7d39069STrond Myklebust out: 404e7d39069STrond Myklebust return error; 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds /* 40889a09141SPeter Zijlstra * Remove a write request from an inode 4091da177e4SLinus Torvalds */ 4101da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req) 4111da177e4SLinus Torvalds { 41288be9f99STrond Myklebust struct inode *inode = req->wb_context->path.dentry->d_inode; 4131da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds BUG_ON (!NFS_WBACK_BUSY(req)); 4161da177e4SLinus Torvalds 417587142f8STrond Myklebust spin_lock(&inode->i_lock); 418277459d2STrond Myklebust set_page_private(req->wb_page, 0); 419deb7d638STrond Myklebust ClearPagePrivate(req->wb_page); 4202df485a7STrond Myklebust clear_bit(PG_MAPPED, &req->wb_flags); 4211da177e4SLinus Torvalds radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index); 4221da177e4SLinus Torvalds nfsi->npages--; 4231da177e4SLinus Torvalds if (!nfsi->npages) { 424587142f8STrond Myklebust spin_unlock(&inode->i_lock); 4251da177e4SLinus Torvalds iput(inode); 4261da177e4SLinus Torvalds } else 427587142f8STrond Myklebust spin_unlock(&inode->i_lock); 4281da177e4SLinus Torvalds nfs_release_request(req); 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 43161822ab5STrond Myklebust static void 4326d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req) 43361822ab5STrond Myklebust { 43461822ab5STrond Myklebust __set_page_dirty_nobuffers(req->wb_page); 435b80c3cb6STrond Myklebust __mark_inode_dirty(req->wb_page->mapping->host, I_DIRTY_DATASYNC); 43661822ab5STrond Myklebust } 43761822ab5STrond Myklebust 4381da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 4391da177e4SLinus Torvalds /* 4401da177e4SLinus Torvalds * Add a request to the inode's commit list. 4411da177e4SLinus Torvalds */ 4421da177e4SLinus Torvalds static void 4431da177e4SLinus Torvalds nfs_mark_request_commit(struct nfs_page *req) 4441da177e4SLinus Torvalds { 44588be9f99STrond Myklebust struct inode *inode = req->wb_context->path.dentry->d_inode; 4461da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 4471da177e4SLinus Torvalds 448587142f8STrond Myklebust spin_lock(&inode->i_lock); 449e468bae9STrond Myklebust set_bit(PG_CLEAN, &(req)->wb_flags); 4505c369683STrond Myklebust radix_tree_tag_set(&nfsi->nfs_page_tree, 4515c369683STrond Myklebust req->wb_index, 4525c369683STrond Myklebust NFS_PAGE_TAG_COMMIT); 453ff778d02STrond Myklebust nfsi->ncommit++; 454587142f8STrond Myklebust spin_unlock(&inode->i_lock); 455fd39fc85SChristoph Lameter inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 456c9e51e41SPeter Zijlstra inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE); 457a1803044STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 4581da177e4SLinus Torvalds } 4598e821cadSTrond Myklebust 460e468bae9STrond Myklebust static int 461e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req) 462e468bae9STrond Myklebust { 463e468bae9STrond Myklebust struct page *page = req->wb_page; 464e468bae9STrond Myklebust 465e468bae9STrond Myklebust if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) { 466e468bae9STrond Myklebust dec_zone_page_state(page, NR_UNSTABLE_NFS); 467e468bae9STrond Myklebust dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE); 468e468bae9STrond Myklebust return 1; 469e468bae9STrond Myklebust } 470e468bae9STrond Myklebust return 0; 471e468bae9STrond Myklebust } 472e468bae9STrond Myklebust 4738e821cadSTrond Myklebust static inline 4748e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 4758e821cadSTrond Myklebust { 4768e821cadSTrond Myklebust return data->verf.committed != NFS_FILE_SYNC; 4778e821cadSTrond Myklebust } 4788e821cadSTrond Myklebust 4798e821cadSTrond Myklebust static inline 4808e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req) 4818e821cadSTrond Myklebust { 482e468bae9STrond Myklebust if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) { 4838e821cadSTrond Myklebust nfs_mark_request_commit(req); 4848e821cadSTrond Myklebust return 1; 4858e821cadSTrond Myklebust } 4868e821cadSTrond Myklebust if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) { 4876d884e8fSFred nfs_mark_request_dirty(req); 4888e821cadSTrond Myklebust return 1; 4898e821cadSTrond Myklebust } 4908e821cadSTrond Myklebust return 0; 4918e821cadSTrond Myklebust } 4928e821cadSTrond Myklebust #else 4938e821cadSTrond Myklebust static inline void 4948e821cadSTrond Myklebust nfs_mark_request_commit(struct nfs_page *req) 4958e821cadSTrond Myklebust { 4968e821cadSTrond Myklebust } 4978e821cadSTrond Myklebust 498e468bae9STrond Myklebust static inline int 499e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req) 500e468bae9STrond Myklebust { 501e468bae9STrond Myklebust return 0; 502e468bae9STrond Myklebust } 503e468bae9STrond Myklebust 5048e821cadSTrond Myklebust static inline 5058e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 5068e821cadSTrond Myklebust { 5078e821cadSTrond Myklebust return 0; 5088e821cadSTrond Myklebust } 5098e821cadSTrond Myklebust 5108e821cadSTrond Myklebust static inline 5118e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req) 5128e821cadSTrond Myklebust { 5138e821cadSTrond Myklebust return 0; 5148e821cadSTrond Myklebust } 5151da177e4SLinus Torvalds #endif 5161da177e4SLinus Torvalds 51747c62564STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 518fb8a1f11STrond Myklebust static int 519fb8a1f11STrond Myklebust nfs_need_commit(struct nfs_inode *nfsi) 520fb8a1f11STrond Myklebust { 521fb8a1f11STrond Myklebust return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT); 522fb8a1f11STrond Myklebust } 523fb8a1f11STrond Myklebust 5241da177e4SLinus Torvalds /* 5251da177e4SLinus Torvalds * nfs_scan_commit - Scan an inode for commit requests 5261da177e4SLinus Torvalds * @inode: NFS inode to scan 5271da177e4SLinus Torvalds * @dst: destination list 5281da177e4SLinus Torvalds * @idx_start: lower bound of page->index to scan. 5291da177e4SLinus Torvalds * @npages: idx_start + npages sets the upper bound to scan. 5301da177e4SLinus Torvalds * 5311da177e4SLinus Torvalds * Moves requests from the inode's 'commit' request list. 5321da177e4SLinus Torvalds * The requests are *not* checked to ensure that they form a contiguous set. 5331da177e4SLinus Torvalds */ 5341da177e4SLinus Torvalds static int 535ca52fec1STrond Myklebust nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages) 5361da177e4SLinus Torvalds { 5371da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 538ff778d02STrond Myklebust int ret; 5393da28eb1STrond Myklebust 540fb8a1f11STrond Myklebust if (!nfs_need_commit(nfsi)) 541fb8a1f11STrond Myklebust return 0; 542fb8a1f11STrond Myklebust 543ff778d02STrond Myklebust ret = nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT); 544ff778d02STrond Myklebust if (ret > 0) 545ff778d02STrond Myklebust nfsi->ncommit -= ret; 5462928db1fSTrond Myklebust if (nfs_need_commit(NFS_I(inode))) 5472928db1fSTrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 548ff778d02STrond Myklebust return ret; 5491da177e4SLinus Torvalds } 550c42de9ddSTrond Myklebust #else 551fb8a1f11STrond Myklebust static inline int nfs_need_commit(struct nfs_inode *nfsi) 552fb8a1f11STrond Myklebust { 553fb8a1f11STrond Myklebust return 0; 554fb8a1f11STrond Myklebust } 555fb8a1f11STrond Myklebust 556ca52fec1STrond Myklebust static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages) 557c42de9ddSTrond Myklebust { 558c42de9ddSTrond Myklebust return 0; 559c42de9ddSTrond Myklebust } 5601da177e4SLinus Torvalds #endif 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds /* 563e7d39069STrond Myklebust * Search for an existing write request, and attempt to update 564e7d39069STrond Myklebust * it to reflect a new dirty region on a given page. 5651da177e4SLinus Torvalds * 566e7d39069STrond Myklebust * If the attempt fails, then the existing request is flushed out 567e7d39069STrond Myklebust * to disk. 5681da177e4SLinus Torvalds */ 569e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode, 570e7d39069STrond Myklebust struct page *page, 571e7d39069STrond Myklebust unsigned int offset, 572e7d39069STrond Myklebust unsigned int bytes) 5731da177e4SLinus Torvalds { 574e7d39069STrond Myklebust struct nfs_page *req; 575e7d39069STrond Myklebust unsigned int rqend; 576e7d39069STrond Myklebust unsigned int end; 5771da177e4SLinus Torvalds int error; 578277459d2STrond Myklebust 579e7d39069STrond Myklebust if (!PagePrivate(page)) 580e7d39069STrond Myklebust return NULL; 581e7d39069STrond Myklebust 582e7d39069STrond Myklebust end = offset + bytes; 583e7d39069STrond Myklebust spin_lock(&inode->i_lock); 584e7d39069STrond Myklebust 585e7d39069STrond Myklebust for (;;) { 586e7d39069STrond Myklebust req = nfs_page_find_request_locked(page); 587e7d39069STrond Myklebust if (req == NULL) 588e7d39069STrond Myklebust goto out_unlock; 589e7d39069STrond Myklebust 590e7d39069STrond Myklebust rqend = req->wb_offset + req->wb_bytes; 591e7d39069STrond Myklebust /* 592e7d39069STrond Myklebust * Tell the caller to flush out the request if 593e7d39069STrond Myklebust * the offsets are non-contiguous. 594e7d39069STrond Myklebust * Note: nfs_flush_incompatible() will already 595e7d39069STrond Myklebust * have flushed out requests having wrong owners. 596e7d39069STrond Myklebust */ 597e468bae9STrond Myklebust if (offset > rqend 598e7d39069STrond Myklebust || end < req->wb_offset) 599e7d39069STrond Myklebust goto out_flushme; 600e7d39069STrond Myklebust 601e7d39069STrond Myklebust if (nfs_set_page_tag_locked(req)) 602e7d39069STrond Myklebust break; 603e7d39069STrond Myklebust 604e7d39069STrond Myklebust /* The request is locked, so wait and then retry */ 605587142f8STrond Myklebust spin_unlock(&inode->i_lock); 6061da177e4SLinus Torvalds error = nfs_wait_on_request(req); 6071da177e4SLinus Torvalds nfs_release_request(req); 608e7d39069STrond Myklebust if (error != 0) 609e7d39069STrond Myklebust goto out_err; 610e7d39069STrond Myklebust spin_lock(&inode->i_lock); 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds 613ff778d02STrond Myklebust if (nfs_clear_request_commit(req) && 614e468bae9STrond Myklebust radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree, 615ff778d02STrond Myklebust req->wb_index, NFS_PAGE_TAG_COMMIT) != NULL) 616ff778d02STrond Myklebust NFS_I(inode)->ncommit--; 617e468bae9STrond Myklebust 6181da177e4SLinus Torvalds /* Okay, the request matches. Update the region */ 6191da177e4SLinus Torvalds if (offset < req->wb_offset) { 6201da177e4SLinus Torvalds req->wb_offset = offset; 6211da177e4SLinus Torvalds req->wb_pgbase = offset; 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds if (end > rqend) 6241da177e4SLinus Torvalds req->wb_bytes = end - req->wb_offset; 625e7d39069STrond Myklebust else 626e7d39069STrond Myklebust req->wb_bytes = rqend - req->wb_offset; 627e7d39069STrond Myklebust out_unlock: 628e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 629e7d39069STrond Myklebust return req; 630e7d39069STrond Myklebust out_flushme: 631e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 632e7d39069STrond Myklebust nfs_release_request(req); 633e7d39069STrond Myklebust error = nfs_wb_page(inode, page); 634e7d39069STrond Myklebust out_err: 635e7d39069STrond Myklebust return ERR_PTR(error); 636e7d39069STrond Myklebust } 6371da177e4SLinus Torvalds 638e7d39069STrond Myklebust /* 639e7d39069STrond Myklebust * Try to update an existing write request, or create one if there is none. 640e7d39069STrond Myklebust * 641e7d39069STrond Myklebust * Note: Should always be called with the Page Lock held to prevent races 642e7d39069STrond Myklebust * if we have to add a new request. Also assumes that the caller has 643e7d39069STrond Myklebust * already called nfs_flush_incompatible() if necessary. 644e7d39069STrond Myklebust */ 645e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, 646e7d39069STrond Myklebust struct page *page, unsigned int offset, unsigned int bytes) 647e7d39069STrond Myklebust { 648e7d39069STrond Myklebust struct inode *inode = page->mapping->host; 649e7d39069STrond Myklebust struct nfs_page *req; 650e7d39069STrond Myklebust int error; 651e7d39069STrond Myklebust 652e7d39069STrond Myklebust req = nfs_try_to_update_request(inode, page, offset, bytes); 653e7d39069STrond Myklebust if (req != NULL) 654e7d39069STrond Myklebust goto out; 655e7d39069STrond Myklebust req = nfs_create_request(ctx, inode, page, offset, bytes); 656e7d39069STrond Myklebust if (IS_ERR(req)) 657e7d39069STrond Myklebust goto out; 658e7d39069STrond Myklebust error = nfs_inode_add_request(inode, req); 659e7d39069STrond Myklebust if (error != 0) { 660e7d39069STrond Myklebust nfs_release_request(req); 661e7d39069STrond Myklebust req = ERR_PTR(error); 662e7d39069STrond Myklebust } 663efc91ed0STrond Myklebust out: 66461e930a9STrond Myklebust return req; 6651da177e4SLinus Torvalds } 6661da177e4SLinus Torvalds 667e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, 668e7d39069STrond Myklebust unsigned int offset, unsigned int count) 669e7d39069STrond Myklebust { 670e7d39069STrond Myklebust struct nfs_page *req; 671e7d39069STrond Myklebust 672e7d39069STrond Myklebust req = nfs_setup_write_request(ctx, page, offset, count); 673e7d39069STrond Myklebust if (IS_ERR(req)) 674e7d39069STrond Myklebust return PTR_ERR(req); 675b80c3cb6STrond Myklebust nfs_mark_request_dirty(req); 676e7d39069STrond Myklebust /* Update file length */ 677e7d39069STrond Myklebust nfs_grow_file(page, offset, count); 678e7d39069STrond Myklebust nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); 679a6305ddbSTrond Myklebust nfs_mark_request_dirty(req); 680e7d39069STrond Myklebust nfs_clear_page_tag_locked(req); 681e7d39069STrond Myklebust return 0; 682e7d39069STrond Myklebust } 683e7d39069STrond Myklebust 6841da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page) 6851da177e4SLinus Torvalds { 686cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 6871da177e4SLinus Torvalds struct nfs_page *req; 6881a54533eSTrond Myklebust int do_flush, status; 6891da177e4SLinus Torvalds /* 6901da177e4SLinus Torvalds * Look for a request corresponding to this page. If there 6911da177e4SLinus Torvalds * is one, and it belongs to another file, we flush it out 6921da177e4SLinus Torvalds * before we try to copy anything into the page. Do this 6931da177e4SLinus Torvalds * due to the lack of an ACCESS-type call in NFSv2. 6941da177e4SLinus Torvalds * Also do the same if we find a request from an existing 6951da177e4SLinus Torvalds * dropped page. 6961da177e4SLinus Torvalds */ 6971a54533eSTrond Myklebust do { 698277459d2STrond Myklebust req = nfs_page_find_request(page); 6991a54533eSTrond Myklebust if (req == NULL) 7001a54533eSTrond Myklebust return 0; 701f11ac8dbSTrond Myklebust do_flush = req->wb_page != page || req->wb_context != ctx || 702f11ac8dbSTrond Myklebust req->wb_lock_context->lockowner != current->files || 703f11ac8dbSTrond Myklebust req->wb_lock_context->pid != current->tgid; 7041da177e4SLinus Torvalds nfs_release_request(req); 7051a54533eSTrond Myklebust if (!do_flush) 7061a54533eSTrond Myklebust return 0; 707277459d2STrond Myklebust status = nfs_wb_page(page->mapping->host, page); 7081a54533eSTrond Myklebust } while (status == 0); 7091a54533eSTrond Myklebust return status; 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds /* 7135d47a356STrond Myklebust * If the page cache is marked as unsafe or invalid, then we can't rely on 7145d47a356STrond Myklebust * the PageUptodate() flag. In this case, we will need to turn off 7155d47a356STrond Myklebust * write optimisations that depend on the page contents being correct. 7165d47a356STrond Myklebust */ 7175d47a356STrond Myklebust static int nfs_write_pageuptodate(struct page *page, struct inode *inode) 7185d47a356STrond Myklebust { 7195d47a356STrond Myklebust return PageUptodate(page) && 7205d47a356STrond Myklebust !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA)); 7215d47a356STrond Myklebust } 7225d47a356STrond Myklebust 7235d47a356STrond Myklebust /* 7241da177e4SLinus Torvalds * Update and possibly write a cached page of an NFS file. 7251da177e4SLinus Torvalds * 7261da177e4SLinus Torvalds * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad 7271da177e4SLinus Torvalds * things with a page scheduled for an RPC call (e.g. invalidate it). 7281da177e4SLinus Torvalds */ 7291da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page, 7301da177e4SLinus Torvalds unsigned int offset, unsigned int count) 7311da177e4SLinus Torvalds { 732cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 7331da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 7341da177e4SLinus Torvalds int status = 0; 7351da177e4SLinus Torvalds 73691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 73791d5b470SChuck Lever 73848186c7dSChuck Lever dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n", 73901cce933SJosef "Jeff" Sipek file->f_path.dentry->d_parent->d_name.name, 74001cce933SJosef "Jeff" Sipek file->f_path.dentry->d_name.name, count, 7410bbacc40SChuck Lever (long long)(page_offset(page) + offset)); 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds /* If we're not using byte range locks, and we know the page 7445d47a356STrond Myklebust * is up to date, it may be more efficient to extend the write 7455d47a356STrond Myklebust * to cover the entire page in order to avoid fragmentation 7465d47a356STrond Myklebust * inefficiencies. 7471da177e4SLinus Torvalds */ 7485d47a356STrond Myklebust if (nfs_write_pageuptodate(page, inode) && 7495d47a356STrond Myklebust inode->i_flock == NULL && 7506b2f3d1fSChristoph Hellwig !(file->f_flags & O_DSYNC)) { 75149a70f27STrond Myklebust count = max(count + offset, nfs_page_length(page)); 7521da177e4SLinus Torvalds offset = 0; 7531da177e4SLinus Torvalds } 7541da177e4SLinus Torvalds 755e21195a7STrond Myklebust status = nfs_writepage_setup(ctx, page, offset, count); 75603fa9e84STrond Myklebust if (status < 0) 75703fa9e84STrond Myklebust nfs_set_pageerror(page); 7581da177e4SLinus Torvalds 75948186c7dSChuck Lever dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", 7601da177e4SLinus Torvalds status, (long long)i_size_read(inode)); 7611da177e4SLinus Torvalds return status; 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds static void nfs_writepage_release(struct nfs_page *req) 7651da177e4SLinus Torvalds { 766a6305ddbSTrond Myklebust struct page *page = req->wb_page; 7678e821cadSTrond Myklebust 768a6305ddbSTrond Myklebust if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) 7691da177e4SLinus Torvalds nfs_inode_remove_request(req); 7709fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 771a6305ddbSTrond Myklebust nfs_end_page_writeback(page); 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 7743ff7576dSTrond Myklebust static int flush_task_priority(int how) 7751da177e4SLinus Torvalds { 7761da177e4SLinus Torvalds switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { 7771da177e4SLinus Torvalds case FLUSH_HIGHPRI: 7781da177e4SLinus Torvalds return RPC_PRIORITY_HIGH; 7791da177e4SLinus Torvalds case FLUSH_LOWPRI: 7801da177e4SLinus Torvalds return RPC_PRIORITY_LOW; 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds return RPC_PRIORITY_NORMAL; 7831da177e4SLinus Torvalds } 7841da177e4SLinus Torvalds 785*d138d5d1SAndy Adamson static int nfs_initiate_write(struct nfs_write_data *data, 786*d138d5d1SAndy Adamson struct rpc_clnt *clnt, 787788e7a89STrond Myklebust const struct rpc_call_ops *call_ops, 7881da177e4SLinus Torvalds int how) 7891da177e4SLinus Torvalds { 790*d138d5d1SAndy Adamson struct inode *inode = data->inode; 7913ff7576dSTrond Myklebust int priority = flush_task_priority(how); 79207737691STrond Myklebust struct rpc_task *task; 793bdc7f021STrond Myklebust struct rpc_message msg = { 794bdc7f021STrond Myklebust .rpc_argp = &data->args, 795bdc7f021STrond Myklebust .rpc_resp = &data->res, 796*d138d5d1SAndy Adamson .rpc_cred = data->cred, 797bdc7f021STrond Myklebust }; 79884115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 799*d138d5d1SAndy Adamson .rpc_client = clnt, 80007737691STrond Myklebust .task = &data->task, 801bdc7f021STrond Myklebust .rpc_message = &msg, 80284115e1cSTrond Myklebust .callback_ops = call_ops, 80384115e1cSTrond Myklebust .callback_data = data, 804101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 8052c61be0aSTrond Myklebust .flags = RPC_TASK_ASYNC, 8063ff7576dSTrond Myklebust .priority = priority, 80784115e1cSTrond Myklebust }; 8082c61be0aSTrond Myklebust int ret = 0; 8091da177e4SLinus Torvalds 810*d138d5d1SAndy Adamson /* Set up the initial task struct. */ 811*d138d5d1SAndy Adamson NFS_PROTO(inode)->write_setup(data, &msg); 812*d138d5d1SAndy Adamson 813*d138d5d1SAndy Adamson dprintk("NFS: %5u initiated write call " 814*d138d5d1SAndy Adamson "(req %s/%lld, %u bytes @ offset %llu)\n", 815*d138d5d1SAndy Adamson data->task.tk_pid, 816*d138d5d1SAndy Adamson inode->i_sb->s_id, 817*d138d5d1SAndy Adamson (long long)NFS_FILEID(inode), 818*d138d5d1SAndy Adamson data->args.count, 819*d138d5d1SAndy Adamson (unsigned long long)data->args.offset); 820*d138d5d1SAndy Adamson 821*d138d5d1SAndy Adamson task = rpc_run_task(&task_setup_data); 822*d138d5d1SAndy Adamson if (IS_ERR(task)) { 823*d138d5d1SAndy Adamson ret = PTR_ERR(task); 824*d138d5d1SAndy Adamson goto out; 825*d138d5d1SAndy Adamson } 826*d138d5d1SAndy Adamson if (how & FLUSH_SYNC) { 827*d138d5d1SAndy Adamson ret = rpc_wait_for_completion_task(task); 828*d138d5d1SAndy Adamson if (ret == 0) 829*d138d5d1SAndy Adamson ret = task->tk_status; 830*d138d5d1SAndy Adamson } 831*d138d5d1SAndy Adamson rpc_put_task(task); 832*d138d5d1SAndy Adamson out: 833*d138d5d1SAndy Adamson return ret; 834*d138d5d1SAndy Adamson } 835*d138d5d1SAndy Adamson 836*d138d5d1SAndy Adamson /* 837*d138d5d1SAndy Adamson * Set up the argument/result storage required for the RPC call. 838*d138d5d1SAndy Adamson */ 839*d138d5d1SAndy Adamson static int nfs_write_rpcsetup(struct nfs_page *req, 840*d138d5d1SAndy Adamson struct nfs_write_data *data, 841*d138d5d1SAndy Adamson const struct rpc_call_ops *call_ops, 842*d138d5d1SAndy Adamson unsigned int count, unsigned int offset, 843*d138d5d1SAndy Adamson int how) 844*d138d5d1SAndy Adamson { 845*d138d5d1SAndy Adamson struct inode *inode = req->wb_context->path.dentry->d_inode; 846*d138d5d1SAndy Adamson 8471da177e4SLinus Torvalds /* Set up the RPC argument and reply structs 8481da177e4SLinus Torvalds * NB: take care not to mess about with data->commit et al. */ 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds data->req = req; 85188be9f99STrond Myklebust data->inode = inode = req->wb_context->path.dentry->d_inode; 852*d138d5d1SAndy Adamson data->cred = req->wb_context->cred; 8531da177e4SLinus Torvalds 8541da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 8551da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 8561da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 8571da177e4SLinus Torvalds data->args.pages = data->pagevec; 8581da177e4SLinus Torvalds data->args.count = count; 859383ba719STrond Myklebust data->args.context = get_nfs_open_context(req->wb_context); 860f11ac8dbSTrond Myklebust data->args.lock_context = req->wb_lock_context; 861bdc7f021STrond Myklebust data->args.stable = NFS_UNSTABLE; 862bdc7f021STrond Myklebust if (how & FLUSH_STABLE) { 863bdc7f021STrond Myklebust data->args.stable = NFS_DATA_SYNC; 864fb8a1f11STrond Myklebust if (!nfs_need_commit(NFS_I(inode))) 865bdc7f021STrond Myklebust data->args.stable = NFS_FILE_SYNC; 866bdc7f021STrond Myklebust } 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds data->res.fattr = &data->fattr; 8691da177e4SLinus Torvalds data->res.count = count; 8701da177e4SLinus Torvalds data->res.verf = &data->verf; 8710e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 8721da177e4SLinus Torvalds 873*d138d5d1SAndy Adamson return nfs_initiate_write(data, NFS_CLIENT(inode), call_ops, how); 8741da177e4SLinus Torvalds } 8751da177e4SLinus Torvalds 8766d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and 8776d884e8fSFred * call this on each, which will prepare them to be retried on next 8786d884e8fSFred * writeback using standard nfs. 8796d884e8fSFred */ 8806d884e8fSFred static void nfs_redirty_request(struct nfs_page *req) 8816d884e8fSFred { 882a6305ddbSTrond Myklebust struct page *page = req->wb_page; 883a6305ddbSTrond Myklebust 8846d884e8fSFred nfs_mark_request_dirty(req); 8856d884e8fSFred nfs_clear_page_tag_locked(req); 886a6305ddbSTrond Myklebust nfs_end_page_writeback(page); 8876d884e8fSFred } 8886d884e8fSFred 8891da177e4SLinus Torvalds /* 8901da177e4SLinus Torvalds * Generate multiple small requests to write out a single 8911da177e4SLinus Torvalds * contiguous dirty area on one page. 8921da177e4SLinus Torvalds */ 893bae724efSFred Isaman static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how, struct pnfs_layout_segment *lseg) 8941da177e4SLinus Torvalds { 8951da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(head->next); 8961da177e4SLinus Torvalds struct page *page = req->wb_page; 8971da177e4SLinus Torvalds struct nfs_write_data *data; 898e9f7bee1STrond Myklebust size_t wsize = NFS_SERVER(inode)->wsize, nbytes; 899e9f7bee1STrond Myklebust unsigned int offset; 9001da177e4SLinus Torvalds int requests = 0; 901dbae4c73STrond Myklebust int ret = 0; 9021da177e4SLinus Torvalds LIST_HEAD(list); 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds nfs_list_remove_request(req); 9051da177e4SLinus Torvalds 906bcb71bbaSTrond Myklebust nbytes = count; 907e9f7bee1STrond Myklebust do { 908e9f7bee1STrond Myklebust size_t len = min(nbytes, wsize); 909e9f7bee1STrond Myklebust 9108d5658c9STrond Myklebust data = nfs_writedata_alloc(1); 9111da177e4SLinus Torvalds if (!data) 9121da177e4SLinus Torvalds goto out_bad; 9131da177e4SLinus Torvalds list_add(&data->pages, &list); 9141da177e4SLinus Torvalds requests++; 915e9f7bee1STrond Myklebust nbytes -= len; 916e9f7bee1STrond Myklebust } while (nbytes != 0); 9171da177e4SLinus Torvalds atomic_set(&req->wb_complete, requests); 9181da177e4SLinus Torvalds 9191da177e4SLinus Torvalds ClearPageError(page); 9201da177e4SLinus Torvalds offset = 0; 921bcb71bbaSTrond Myklebust nbytes = count; 9221da177e4SLinus Torvalds do { 923dbae4c73STrond Myklebust int ret2; 924dbae4c73STrond Myklebust 9251da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_write_data, pages); 9261da177e4SLinus Torvalds list_del_init(&data->pages); 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds data->pagevec[0] = page; 9291da177e4SLinus Torvalds 930bcb71bbaSTrond Myklebust if (nbytes < wsize) 931bcb71bbaSTrond Myklebust wsize = nbytes; 932dbae4c73STrond Myklebust ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops, 933788e7a89STrond Myklebust wsize, offset, how); 934dbae4c73STrond Myklebust if (ret == 0) 935dbae4c73STrond Myklebust ret = ret2; 9361da177e4SLinus Torvalds offset += wsize; 9371da177e4SLinus Torvalds nbytes -= wsize; 9381da177e4SLinus Torvalds } while (nbytes != 0); 9391da177e4SLinus Torvalds 940dbae4c73STrond Myklebust return ret; 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds out_bad: 9431da177e4SLinus Torvalds while (!list_empty(&list)) { 9441da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_write_data, pages); 9451da177e4SLinus Torvalds list_del(&data->pages); 9460da2a4acSFred Isaman nfs_writedata_free(data); 9471da177e4SLinus Torvalds } 94861822ab5STrond Myklebust nfs_redirty_request(req); 9491da177e4SLinus Torvalds return -ENOMEM; 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds /* 9531da177e4SLinus Torvalds * Create an RPC task for the given write request and kick it. 9541da177e4SLinus Torvalds * The page must have been locked by the caller. 9551da177e4SLinus Torvalds * 9561da177e4SLinus Torvalds * It may happen that the page we're passed is not marked dirty. 9571da177e4SLinus Torvalds * This is the case if nfs_updatepage detects a conflicting request 9581da177e4SLinus Torvalds * that has been written but not committed. 9591da177e4SLinus Torvalds */ 960bae724efSFred Isaman static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how, struct pnfs_layout_segment *lseg) 9611da177e4SLinus Torvalds { 9621da177e4SLinus Torvalds struct nfs_page *req; 9631da177e4SLinus Torvalds struct page **pages; 9641da177e4SLinus Torvalds struct nfs_write_data *data; 9651da177e4SLinus Torvalds 9668d5658c9STrond Myklebust data = nfs_writedata_alloc(npages); 9671da177e4SLinus Torvalds if (!data) 9681da177e4SLinus Torvalds goto out_bad; 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds pages = data->pagevec; 9711da177e4SLinus Torvalds while (!list_empty(head)) { 9721da177e4SLinus Torvalds req = nfs_list_entry(head->next); 9731da177e4SLinus Torvalds nfs_list_remove_request(req); 9741da177e4SLinus Torvalds nfs_list_add_request(req, &data->pages); 9751da177e4SLinus Torvalds ClearPageError(req->wb_page); 9761da177e4SLinus Torvalds *pages++ = req->wb_page; 9771da177e4SLinus Torvalds } 9781da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds /* Set up the argument struct */ 981dbae4c73STrond Myklebust return nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how); 9821da177e4SLinus Torvalds out_bad: 9831da177e4SLinus Torvalds while (!list_empty(head)) { 98410afec90STrond Myklebust req = nfs_list_entry(head->next); 9851da177e4SLinus Torvalds nfs_list_remove_request(req); 98661822ab5STrond Myklebust nfs_redirty_request(req); 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds return -ENOMEM; 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds 991c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, 992c63c7b05STrond Myklebust struct inode *inode, int ioflags) 9931da177e4SLinus Torvalds { 994bf4285e7SChuck Lever size_t wsize = NFS_SERVER(inode)->wsize; 9951da177e4SLinus Torvalds 99694ad1c80SFred Isaman pgio->pg_test = NULL; 99794ad1c80SFred Isaman 998bcb71bbaSTrond Myklebust if (wsize < PAGE_CACHE_SIZE) 999c63c7b05STrond Myklebust nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags); 1000bcb71bbaSTrond Myklebust else 1001c63c7b05STrond Myklebust nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags); 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds 10041da177e4SLinus Torvalds /* 10051da177e4SLinus Torvalds * Handle a write reply that flushed part of a page. 10061da177e4SLinus Torvalds */ 1007788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata) 10081da177e4SLinus Torvalds { 1009788e7a89STrond Myklebust struct nfs_write_data *data = calldata; 10101da177e4SLinus Torvalds 101148186c7dSChuck Lever dprintk("NFS: %5u write(%s/%lld %d@%lld)", 101248186c7dSChuck Lever task->tk_pid, 101348186c7dSChuck Lever data->req->wb_context->path.dentry->d_inode->i_sb->s_id, 101448186c7dSChuck Lever (long long) 101548186c7dSChuck Lever NFS_FILEID(data->req->wb_context->path.dentry->d_inode), 101648186c7dSChuck Lever data->req->wb_bytes, (long long)req_offset(data->req)); 10171da177e4SLinus Torvalds 1018c9d8f89dSTrond Myklebust nfs_writeback_done(task, data); 1019c9d8f89dSTrond Myklebust } 1020788e7a89STrond Myklebust 1021c9d8f89dSTrond Myklebust static void nfs_writeback_release_partial(void *calldata) 1022c9d8f89dSTrond Myklebust { 1023c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1024c9d8f89dSTrond Myklebust struct nfs_page *req = data->req; 1025c9d8f89dSTrond Myklebust struct page *page = req->wb_page; 1026c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1027c9d8f89dSTrond Myklebust 1028c9d8f89dSTrond Myklebust if (status < 0) { 1029a301b777STrond Myklebust nfs_set_pageerror(page); 1030c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 1031c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 10328e821cadSTrond Myklebust goto out; 10338e821cadSTrond Myklebust } 10348e821cadSTrond Myklebust 10358e821cadSTrond Myklebust if (nfs_write_need_commit(data)) { 1036587142f8STrond Myklebust struct inode *inode = page->mapping->host; 10378e821cadSTrond Myklebust 1038587142f8STrond Myklebust spin_lock(&inode->i_lock); 10398e821cadSTrond Myklebust if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) { 10408e821cadSTrond Myklebust /* Do nothing we need to resend the writes */ 10418e821cadSTrond Myklebust } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) { 10421da177e4SLinus Torvalds memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); 10431da177e4SLinus Torvalds dprintk(" defer commit\n"); 10441da177e4SLinus Torvalds } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) { 10458e821cadSTrond Myklebust set_bit(PG_NEED_RESCHED, &req->wb_flags); 10468e821cadSTrond Myklebust clear_bit(PG_NEED_COMMIT, &req->wb_flags); 10471da177e4SLinus Torvalds dprintk(" server reboot detected\n"); 10481da177e4SLinus Torvalds } 1049587142f8STrond Myklebust spin_unlock(&inode->i_lock); 10501da177e4SLinus Torvalds } else 10511da177e4SLinus Torvalds dprintk(" OK\n"); 10521da177e4SLinus Torvalds 10538e821cadSTrond Myklebust out: 10541da177e4SLinus Torvalds if (atomic_dec_and_test(&req->wb_complete)) 10551da177e4SLinus Torvalds nfs_writepage_release(req); 1056c9d8f89dSTrond Myklebust nfs_writedata_release(calldata); 10571da177e4SLinus Torvalds } 10581da177e4SLinus Torvalds 1059def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1) 1060def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata) 1061def6ed7eSAndy Adamson { 1062def6ed7eSAndy Adamson struct nfs_write_data *data = calldata; 1063def6ed7eSAndy Adamson 1064035168abSTrond Myklebust if (nfs4_setup_sequence(NFS_SERVER(data->inode), 1065035168abSTrond Myklebust &data->args.seq_args, 1066def6ed7eSAndy Adamson &data->res.seq_res, 1, task)) 1067def6ed7eSAndy Adamson return; 1068def6ed7eSAndy Adamson rpc_call_start(task); 1069def6ed7eSAndy Adamson } 1070def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1071def6ed7eSAndy Adamson 1072788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = { 1073def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1) 1074def6ed7eSAndy Adamson .rpc_call_prepare = nfs_write_prepare, 1075def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1076788e7a89STrond Myklebust .rpc_call_done = nfs_writeback_done_partial, 1077c9d8f89dSTrond Myklebust .rpc_release = nfs_writeback_release_partial, 1078788e7a89STrond Myklebust }; 1079788e7a89STrond Myklebust 10801da177e4SLinus Torvalds /* 10811da177e4SLinus Torvalds * Handle a write reply that flushes a whole page. 10821da177e4SLinus Torvalds * 10831da177e4SLinus Torvalds * FIXME: There is an inherent race with invalidate_inode_pages and 10841da177e4SLinus Torvalds * writebacks since the page->count is kept > 1 for as long 10851da177e4SLinus Torvalds * as the page has a write request pending. 10861da177e4SLinus Torvalds */ 1087788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata) 10881da177e4SLinus Torvalds { 1089788e7a89STrond Myklebust struct nfs_write_data *data = calldata; 10901da177e4SLinus Torvalds 1091c9d8f89dSTrond Myklebust nfs_writeback_done(task, data); 1092c9d8f89dSTrond Myklebust } 1093c9d8f89dSTrond Myklebust 1094c9d8f89dSTrond Myklebust static void nfs_writeback_release_full(void *calldata) 1095c9d8f89dSTrond Myklebust { 1096c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1097c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1098788e7a89STrond Myklebust 10991da177e4SLinus Torvalds /* Update attributes as result of writeback. */ 11001da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 1101c9d8f89dSTrond Myklebust struct nfs_page *req = nfs_list_entry(data->pages.next); 1102c9d8f89dSTrond Myklebust struct page *page = req->wb_page; 1103c9d8f89dSTrond Myklebust 11041da177e4SLinus Torvalds nfs_list_remove_request(req); 11051da177e4SLinus Torvalds 110648186c7dSChuck Lever dprintk("NFS: %5u write (%s/%lld %d@%lld)", 110748186c7dSChuck Lever data->task.tk_pid, 110888be9f99STrond Myklebust req->wb_context->path.dentry->d_inode->i_sb->s_id, 110988be9f99STrond Myklebust (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 11101da177e4SLinus Torvalds req->wb_bytes, 11111da177e4SLinus Torvalds (long long)req_offset(req)); 11121da177e4SLinus Torvalds 1113c9d8f89dSTrond Myklebust if (status < 0) { 1114a301b777STrond Myklebust nfs_set_pageerror(page); 1115c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 1116c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 11178e821cadSTrond Myklebust goto remove_request; 11181da177e4SLinus Torvalds } 11191da177e4SLinus Torvalds 11208e821cadSTrond Myklebust if (nfs_write_need_commit(data)) { 11211da177e4SLinus Torvalds memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); 11221da177e4SLinus Torvalds nfs_mark_request_commit(req); 11231da177e4SLinus Torvalds dprintk(" marked for commit\n"); 11248e821cadSTrond Myklebust goto next; 11258e821cadSTrond Myklebust } 11268e821cadSTrond Myklebust dprintk(" OK\n"); 11278e821cadSTrond Myklebust remove_request: 11281da177e4SLinus Torvalds nfs_inode_remove_request(req); 11291da177e4SLinus Torvalds next: 11309fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 1131a6305ddbSTrond Myklebust nfs_end_page_writeback(page); 11321da177e4SLinus Torvalds } 1133c9d8f89dSTrond Myklebust nfs_writedata_release(calldata); 11341da177e4SLinus Torvalds } 11351da177e4SLinus Torvalds 1136788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = { 1137def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1) 1138def6ed7eSAndy Adamson .rpc_call_prepare = nfs_write_prepare, 1139def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1140788e7a89STrond Myklebust .rpc_call_done = nfs_writeback_done_full, 1141c9d8f89dSTrond Myklebust .rpc_release = nfs_writeback_release_full, 1142788e7a89STrond Myklebust }; 1143788e7a89STrond Myklebust 1144788e7a89STrond Myklebust 11451da177e4SLinus Torvalds /* 11461da177e4SLinus Torvalds * This function is called when the WRITE call is complete. 11471da177e4SLinus Torvalds */ 114813602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data) 11491da177e4SLinus Torvalds { 11501da177e4SLinus Torvalds struct nfs_writeargs *argp = &data->args; 11511da177e4SLinus Torvalds struct nfs_writeres *resp = &data->res; 1152eedc020eSAndy Adamson struct nfs_server *server = NFS_SERVER(data->inode); 1153788e7a89STrond Myklebust int status; 11541da177e4SLinus Torvalds 1155a3f565b1SChuck Lever dprintk("NFS: %5u nfs_writeback_done (status %d)\n", 11561da177e4SLinus Torvalds task->tk_pid, task->tk_status); 11571da177e4SLinus Torvalds 1158f551e44fSChuck Lever /* 1159f551e44fSChuck Lever * ->write_done will attempt to use post-op attributes to detect 1160f551e44fSChuck Lever * conflicting writes by other clients. A strict interpretation 1161f551e44fSChuck Lever * of close-to-open would allow us to continue caching even if 1162f551e44fSChuck Lever * another writer had changed the file, but some applications 1163f551e44fSChuck Lever * depend on tighter cache coherency when writing. 1164f551e44fSChuck Lever */ 1165788e7a89STrond Myklebust status = NFS_PROTO(data->inode)->write_done(task, data); 1166788e7a89STrond Myklebust if (status != 0) 116713602896SFred Isaman return; 116891d5b470SChuck Lever nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count); 116991d5b470SChuck Lever 11701da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 11711da177e4SLinus Torvalds if (resp->verf->committed < argp->stable && task->tk_status >= 0) { 11721da177e4SLinus Torvalds /* We tried a write call, but the server did not 11731da177e4SLinus Torvalds * commit data to stable storage even though we 11741da177e4SLinus Torvalds * requested it. 11751da177e4SLinus Torvalds * Note: There is a known bug in Tru64 < 5.0 in which 11761da177e4SLinus Torvalds * the server reports NFS_DATA_SYNC, but performs 11771da177e4SLinus Torvalds * NFS_FILE_SYNC. We therefore implement this checking 11781da177e4SLinus Torvalds * as a dprintk() in order to avoid filling syslog. 11791da177e4SLinus Torvalds */ 11801da177e4SLinus Torvalds static unsigned long complain; 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 11831da177e4SLinus Torvalds dprintk("NFS: faulty NFS server %s:" 11841da177e4SLinus Torvalds " (committed = %d) != (stable = %d)\n", 1185eedc020eSAndy Adamson server->nfs_client->cl_hostname, 11861da177e4SLinus Torvalds resp->verf->committed, argp->stable); 11871da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 11881da177e4SLinus Torvalds } 11891da177e4SLinus Torvalds } 11901da177e4SLinus Torvalds #endif 11911da177e4SLinus Torvalds /* Is this a short write? */ 11921da177e4SLinus Torvalds if (task->tk_status >= 0 && resp->count < argp->count) { 11931da177e4SLinus Torvalds static unsigned long complain; 11941da177e4SLinus Torvalds 119591d5b470SChuck Lever nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE); 119691d5b470SChuck Lever 11971da177e4SLinus Torvalds /* Has the server at least made some progress? */ 11981da177e4SLinus Torvalds if (resp->count != 0) { 11991da177e4SLinus Torvalds /* Was this an NFSv2 write or an NFSv3 stable write? */ 12001da177e4SLinus Torvalds if (resp->verf->committed != NFS_UNSTABLE) { 12011da177e4SLinus Torvalds /* Resend from where the server left off */ 12021da177e4SLinus Torvalds argp->offset += resp->count; 12031da177e4SLinus Torvalds argp->pgbase += resp->count; 12041da177e4SLinus Torvalds argp->count -= resp->count; 12051da177e4SLinus Torvalds } else { 12061da177e4SLinus Torvalds /* Resend as a stable write in order to avoid 12071da177e4SLinus Torvalds * headaches in the case of a server crash. 12081da177e4SLinus Torvalds */ 12091da177e4SLinus Torvalds argp->stable = NFS_FILE_SYNC; 12101da177e4SLinus Torvalds } 12110110ee15STrond Myklebust nfs_restart_rpc(task, server->nfs_client); 121213602896SFred Isaman return; 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 12151da177e4SLinus Torvalds printk(KERN_WARNING 12161da177e4SLinus Torvalds "NFS: Server wrote zero bytes, expected %u.\n", 12171da177e4SLinus Torvalds argp->count); 12181da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 12191da177e4SLinus Torvalds } 12201da177e4SLinus Torvalds /* Can't do anything about it except throw an error. */ 12211da177e4SLinus Torvalds task->tk_status = -EIO; 12221da177e4SLinus Torvalds } 122313602896SFred Isaman return; 12241da177e4SLinus Torvalds } 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 122871d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait) 122971d0a611STrond Myklebust { 123071d0a611STrond Myklebust if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags)) 123171d0a611STrond Myklebust return 1; 123271d0a611STrond Myklebust if (may_wait && !out_of_line_wait_on_bit_lock(&nfsi->flags, 123371d0a611STrond Myklebust NFS_INO_COMMIT, nfs_wait_bit_killable, 123471d0a611STrond Myklebust TASK_KILLABLE)) 123571d0a611STrond Myklebust return 1; 123671d0a611STrond Myklebust return 0; 123771d0a611STrond Myklebust } 123871d0a611STrond Myklebust 123971d0a611STrond Myklebust static void nfs_commit_clear_lock(struct nfs_inode *nfsi) 124071d0a611STrond Myklebust { 124171d0a611STrond Myklebust clear_bit(NFS_INO_COMMIT, &nfsi->flags); 124271d0a611STrond Myklebust smp_mb__after_clear_bit(); 124371d0a611STrond Myklebust wake_up_bit(&nfsi->flags, NFS_INO_COMMIT); 124471d0a611STrond Myklebust } 124571d0a611STrond Myklebust 124671d0a611STrond Myklebust 12470aa05887SH Hartley Sweeten static void nfs_commitdata_release(void *data) 12481da177e4SLinus Torvalds { 1249383ba719STrond Myklebust struct nfs_write_data *wdata = data; 1250383ba719STrond Myklebust 1251383ba719STrond Myklebust put_nfs_open_context(wdata->args.context); 12521da177e4SLinus Torvalds nfs_commit_free(wdata); 12531da177e4SLinus Torvalds } 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds /* 12561da177e4SLinus Torvalds * Set up the argument/result storage required for the RPC call. 12571da177e4SLinus Torvalds */ 1258dbae4c73STrond Myklebust static int nfs_commit_rpcsetup(struct list_head *head, 1259788e7a89STrond Myklebust struct nfs_write_data *data, 1260788e7a89STrond Myklebust int how) 12611da177e4SLinus Torvalds { 126284115e1cSTrond Myklebust struct nfs_page *first = nfs_list_entry(head->next); 126384115e1cSTrond Myklebust struct inode *inode = first->wb_context->path.dentry->d_inode; 12643ff7576dSTrond Myklebust int priority = flush_task_priority(how); 126507737691STrond Myklebust struct rpc_task *task; 1266bdc7f021STrond Myklebust struct rpc_message msg = { 1267bdc7f021STrond Myklebust .rpc_argp = &data->args, 1268bdc7f021STrond Myklebust .rpc_resp = &data->res, 1269bdc7f021STrond Myklebust .rpc_cred = first->wb_context->cred, 1270bdc7f021STrond Myklebust }; 127184115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 127207737691STrond Myklebust .task = &data->task, 127384115e1cSTrond Myklebust .rpc_client = NFS_CLIENT(inode), 1274bdc7f021STrond Myklebust .rpc_message = &msg, 127584115e1cSTrond Myklebust .callback_ops = &nfs_commit_ops, 127684115e1cSTrond Myklebust .callback_data = data, 1277101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 12782c61be0aSTrond Myklebust .flags = RPC_TASK_ASYNC, 12793ff7576dSTrond Myklebust .priority = priority, 128084115e1cSTrond Myklebust }; 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds /* Set up the RPC argument and reply structs 12831da177e4SLinus Torvalds * NB: take care not to mess about with data->commit et al. */ 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds list_splice_init(head, &data->pages); 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds data->inode = inode; 1288bdc7f021STrond Myklebust data->cred = msg.rpc_cred; 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds data->args.fh = NFS_FH(data->inode); 12913da28eb1STrond Myklebust /* Note: we always request a commit of the entire inode */ 12923da28eb1STrond Myklebust data->args.offset = 0; 12933da28eb1STrond Myklebust data->args.count = 0; 1294383ba719STrond Myklebust data->args.context = get_nfs_open_context(first->wb_context); 12953da28eb1STrond Myklebust data->res.count = 0; 12961da177e4SLinus Torvalds data->res.fattr = &data->fattr; 12971da177e4SLinus Torvalds data->res.verf = &data->verf; 12980e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 12991da177e4SLinus Torvalds 1300788e7a89STrond Myklebust /* Set up the initial task struct. */ 1301bdc7f021STrond Myklebust NFS_PROTO(inode)->commit_setup(data, &msg); 13021da177e4SLinus Torvalds 1303a3f565b1SChuck Lever dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); 1304bdc7f021STrond Myklebust 130507737691STrond Myklebust task = rpc_run_task(&task_setup_data); 1306dbae4c73STrond Myklebust if (IS_ERR(task)) 1307dbae4c73STrond Myklebust return PTR_ERR(task); 1308d2224e7aSJeff Layton if (how & FLUSH_SYNC) 1309d2224e7aSJeff Layton rpc_wait_for_completion_task(task); 131007737691STrond Myklebust rpc_put_task(task); 1311dbae4c73STrond Myklebust return 0; 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds /* 13151da177e4SLinus Torvalds * Commit dirty pages 13161da177e4SLinus Torvalds */ 13171da177e4SLinus Torvalds static int 131840859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how) 13191da177e4SLinus Torvalds { 13201da177e4SLinus Torvalds struct nfs_write_data *data; 13211da177e4SLinus Torvalds struct nfs_page *req; 13221da177e4SLinus Torvalds 1323c9d8f89dSTrond Myklebust data = nfs_commitdata_alloc(); 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds if (!data) 13261da177e4SLinus Torvalds goto out_bad; 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds /* Set up the argument struct */ 1329dbae4c73STrond Myklebust return nfs_commit_rpcsetup(head, data, how); 13301da177e4SLinus Torvalds out_bad: 13311da177e4SLinus Torvalds while (!list_empty(head)) { 13321da177e4SLinus Torvalds req = nfs_list_entry(head->next); 13331da177e4SLinus Torvalds nfs_list_remove_request(req); 13341da177e4SLinus Torvalds nfs_mark_request_commit(req); 133583715ad5STrond Myklebust dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 1336c9e51e41SPeter Zijlstra dec_bdi_stat(req->wb_page->mapping->backing_dev_info, 1337c9e51e41SPeter Zijlstra BDI_RECLAIMABLE); 13389fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 13391da177e4SLinus Torvalds } 134071d0a611STrond Myklebust nfs_commit_clear_lock(NFS_I(inode)); 13411da177e4SLinus Torvalds return -ENOMEM; 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds /* 13451da177e4SLinus Torvalds * COMMIT call returned 13461da177e4SLinus Torvalds */ 1347788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata) 13481da177e4SLinus Torvalds { 1349963d8fe5STrond Myklebust struct nfs_write_data *data = calldata; 13501da177e4SLinus Torvalds 1351a3f565b1SChuck Lever dprintk("NFS: %5u nfs_commit_done (status %d)\n", 13521da177e4SLinus Torvalds task->tk_pid, task->tk_status); 13531da177e4SLinus Torvalds 1354788e7a89STrond Myklebust /* Call the NFS version-specific code */ 1355788e7a89STrond Myklebust if (NFS_PROTO(data->inode)->commit_done(task, data) != 0) 1356788e7a89STrond Myklebust return; 1357c9d8f89dSTrond Myklebust } 1358c9d8f89dSTrond Myklebust 1359c9d8f89dSTrond Myklebust static void nfs_commit_release(void *calldata) 1360c9d8f89dSTrond Myklebust { 1361c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1362c9d8f89dSTrond Myklebust struct nfs_page *req; 1363c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1364788e7a89STrond Myklebust 13651da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 13661da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 13671da177e4SLinus Torvalds nfs_list_remove_request(req); 1368e468bae9STrond Myklebust nfs_clear_request_commit(req); 13691da177e4SLinus Torvalds 137048186c7dSChuck Lever dprintk("NFS: commit (%s/%lld %d@%lld)", 137188be9f99STrond Myklebust req->wb_context->path.dentry->d_inode->i_sb->s_id, 137288be9f99STrond Myklebust (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 13731da177e4SLinus Torvalds req->wb_bytes, 13741da177e4SLinus Torvalds (long long)req_offset(req)); 1375c9d8f89dSTrond Myklebust if (status < 0) { 1376c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 13771da177e4SLinus Torvalds nfs_inode_remove_request(req); 1378c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 13791da177e4SLinus Torvalds goto next; 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds /* Okay, COMMIT succeeded, apparently. Check the verifier 13831da177e4SLinus Torvalds * returned by the server against all stored verfs. */ 13841da177e4SLinus Torvalds if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) { 13851da177e4SLinus Torvalds /* We have a match */ 13861da177e4SLinus Torvalds nfs_inode_remove_request(req); 13871da177e4SLinus Torvalds dprintk(" OK\n"); 13881da177e4SLinus Torvalds goto next; 13891da177e4SLinus Torvalds } 13901da177e4SLinus Torvalds /* We have a mismatch. Write the page again */ 13911da177e4SLinus Torvalds dprintk(" mismatch\n"); 13926d884e8fSFred nfs_mark_request_dirty(req); 13931da177e4SLinus Torvalds next: 13949fd367f0STrond Myklebust nfs_clear_page_tag_locked(req); 13951da177e4SLinus Torvalds } 139671d0a611STrond Myklebust nfs_commit_clear_lock(NFS_I(data->inode)); 1397c9d8f89dSTrond Myklebust nfs_commitdata_release(calldata); 13981da177e4SLinus Torvalds } 1399788e7a89STrond Myklebust 1400788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = { 140121d9a851SAndy Adamson #if defined(CONFIG_NFS_V4_1) 140221d9a851SAndy Adamson .rpc_call_prepare = nfs_write_prepare, 140321d9a851SAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1404788e7a89STrond Myklebust .rpc_call_done = nfs_commit_done, 1405788e7a89STrond Myklebust .rpc_release = nfs_commit_release, 1406788e7a89STrond Myklebust }; 14071da177e4SLinus Torvalds 1408b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how) 14091da177e4SLinus Torvalds { 14101da177e4SLinus Torvalds LIST_HEAD(head); 141171d0a611STrond Myklebust int may_wait = how & FLUSH_SYNC; 141271d0a611STrond Myklebust int res = 0; 14131da177e4SLinus Torvalds 141471d0a611STrond Myklebust if (!nfs_commit_set_lock(NFS_I(inode), may_wait)) 1415c5efa5fcSTrond Myklebust goto out_mark_dirty; 1416587142f8STrond Myklebust spin_lock(&inode->i_lock); 14173da28eb1STrond Myklebust res = nfs_scan_commit(inode, &head, 0, 0); 1418587142f8STrond Myklebust spin_unlock(&inode->i_lock); 14191da177e4SLinus Torvalds if (res) { 14207d46a49fSTrond Myklebust int error = nfs_commit_list(inode, &head, how); 14211da177e4SLinus Torvalds if (error < 0) 14221da177e4SLinus Torvalds return error; 142371d0a611STrond Myklebust if (may_wait) 142471d0a611STrond Myklebust wait_on_bit(&NFS_I(inode)->flags, NFS_INO_COMMIT, 142571d0a611STrond Myklebust nfs_wait_bit_killable, 142671d0a611STrond Myklebust TASK_KILLABLE); 1427c5efa5fcSTrond Myklebust else 1428c5efa5fcSTrond Myklebust goto out_mark_dirty; 142971d0a611STrond Myklebust } else 143071d0a611STrond Myklebust nfs_commit_clear_lock(NFS_I(inode)); 1431c5efa5fcSTrond Myklebust return res; 1432c5efa5fcSTrond Myklebust /* Note: If we exit without ensuring that the commit is complete, 1433c5efa5fcSTrond Myklebust * we must mark the inode as dirty. Otherwise, future calls to 1434c5efa5fcSTrond Myklebust * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure 1435c5efa5fcSTrond Myklebust * that the data is on the disk. 1436c5efa5fcSTrond Myklebust */ 1437c5efa5fcSTrond Myklebust out_mark_dirty: 1438c5efa5fcSTrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 14391da177e4SLinus Torvalds return res; 14401da177e4SLinus Torvalds } 14418fc795f7STrond Myklebust 14428fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 14438fc795f7STrond Myklebust { 1444420e3646STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 1445420e3646STrond Myklebust int flags = FLUSH_SYNC; 1446420e3646STrond Myklebust int ret = 0; 14478fc795f7STrond Myklebust 1448a00dd6c0SJeff Layton if (wbc->sync_mode == WB_SYNC_NONE) { 1449a00dd6c0SJeff Layton /* Don't commit yet if this is a non-blocking flush and there 1450a00dd6c0SJeff Layton * are a lot of outstanding writes for this mapping. 1451420e3646STrond Myklebust */ 1452a00dd6c0SJeff Layton if (nfsi->ncommit <= (nfsi->npages >> 1)) 1453420e3646STrond Myklebust goto out_mark_dirty; 1454420e3646STrond Myklebust 1455a00dd6c0SJeff Layton /* don't wait for the COMMIT response */ 1456420e3646STrond Myklebust flags = 0; 1457a00dd6c0SJeff Layton } 1458a00dd6c0SJeff Layton 1459420e3646STrond Myklebust ret = nfs_commit_inode(inode, flags); 1460420e3646STrond Myklebust if (ret >= 0) { 1461420e3646STrond Myklebust if (wbc->sync_mode == WB_SYNC_NONE) { 1462420e3646STrond Myklebust if (ret < wbc->nr_to_write) 1463420e3646STrond Myklebust wbc->nr_to_write -= ret; 1464420e3646STrond Myklebust else 1465420e3646STrond Myklebust wbc->nr_to_write = 0; 1466420e3646STrond Myklebust } 14678fc795f7STrond Myklebust return 0; 1468420e3646STrond Myklebust } 1469420e3646STrond Myklebust out_mark_dirty: 14708fc795f7STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 14718fc795f7STrond Myklebust return ret; 14728fc795f7STrond Myklebust } 1473c63c7b05STrond Myklebust #else 14748fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 14758fc795f7STrond Myklebust { 14768fc795f7STrond Myklebust return 0; 14778fc795f7STrond Myklebust } 14781da177e4SLinus Torvalds #endif 14791da177e4SLinus Torvalds 14808fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc) 14818fc795f7STrond Myklebust { 14828fc795f7STrond Myklebust return nfs_commit_unstable_pages(inode, wbc); 14838fc795f7STrond Myklebust } 14848fc795f7STrond Myklebust 1485acdc53b2STrond Myklebust /* 1486acdc53b2STrond Myklebust * flush the inode to disk. 1487acdc53b2STrond Myklebust */ 1488acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode) 148934901f70STrond Myklebust { 149034901f70STrond Myklebust struct writeback_control wbc = { 149172cb77f4STrond Myklebust .sync_mode = WB_SYNC_ALL, 149234901f70STrond Myklebust .nr_to_write = LONG_MAX, 1493d7fb1207STrond Myklebust .range_start = 0, 1494d7fb1207STrond Myklebust .range_end = LLONG_MAX, 149534901f70STrond Myklebust }; 149634901f70STrond Myklebust 1497acdc53b2STrond Myklebust return sync_inode(inode, &wbc); 14981c75950bSTrond Myklebust } 14991c75950bSTrond Myklebust 15001b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page) 15011b3b4a1aSTrond Myklebust { 15021b3b4a1aSTrond Myklebust struct nfs_page *req; 15031b3b4a1aSTrond Myklebust int ret = 0; 15041b3b4a1aSTrond Myklebust 15051b3b4a1aSTrond Myklebust BUG_ON(!PageLocked(page)); 15061b3b4a1aSTrond Myklebust for (;;) { 1507ba8b06e6STrond Myklebust wait_on_page_writeback(page); 15081b3b4a1aSTrond Myklebust req = nfs_page_find_request(page); 15091b3b4a1aSTrond Myklebust if (req == NULL) 15101b3b4a1aSTrond Myklebust break; 15111b3b4a1aSTrond Myklebust if (nfs_lock_request_dontget(req)) { 15121b3b4a1aSTrond Myklebust nfs_inode_remove_request(req); 15131b3b4a1aSTrond Myklebust /* 15141b3b4a1aSTrond Myklebust * In case nfs_inode_remove_request has marked the 15151b3b4a1aSTrond Myklebust * page as being dirty 15161b3b4a1aSTrond Myklebust */ 15171b3b4a1aSTrond Myklebust cancel_dirty_page(page, PAGE_CACHE_SIZE); 15181b3b4a1aSTrond Myklebust nfs_unlock_request(req); 15191b3b4a1aSTrond Myklebust break; 15201b3b4a1aSTrond Myklebust } 15211b3b4a1aSTrond Myklebust ret = nfs_wait_on_request(req); 1522c9edda71STrond Myklebust nfs_release_request(req); 15231b3b4a1aSTrond Myklebust if (ret < 0) 1524c988950eSTrond Myklebust break; 15251b3b4a1aSTrond Myklebust } 15261b3b4a1aSTrond Myklebust return ret; 15271b3b4a1aSTrond Myklebust } 15281b3b4a1aSTrond Myklebust 15291c75950bSTrond Myklebust /* 15301c75950bSTrond Myklebust * Write back all requests on one page - we do this before reading it. 15311c75950bSTrond Myklebust */ 15321c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page) 15331c75950bSTrond Myklebust { 15347f2f12d9STrond Myklebust loff_t range_start = page_offset(page); 15357f2f12d9STrond Myklebust loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); 15367f2f12d9STrond Myklebust struct writeback_control wbc = { 15377f2f12d9STrond Myklebust .sync_mode = WB_SYNC_ALL, 15387f2f12d9STrond Myklebust .nr_to_write = 0, 15397f2f12d9STrond Myklebust .range_start = range_start, 15407f2f12d9STrond Myklebust .range_end = range_end, 15417f2f12d9STrond Myklebust }; 15427f2f12d9STrond Myklebust int ret; 15437f2f12d9STrond Myklebust 15440522f6adSTrond Myklebust for (;;) { 1545ba8b06e6STrond Myklebust wait_on_page_writeback(page); 15467f2f12d9STrond Myklebust if (clear_page_dirty_for_io(page)) { 15477f2f12d9STrond Myklebust ret = nfs_writepage_locked(page, &wbc); 15487f2f12d9STrond Myklebust if (ret < 0) 15497f2f12d9STrond Myklebust goto out_error; 15500522f6adSTrond Myklebust continue; 15517f2f12d9STrond Myklebust } 15520522f6adSTrond Myklebust if (!PagePrivate(page)) 15530522f6adSTrond Myklebust break; 15540522f6adSTrond Myklebust ret = nfs_commit_inode(inode, FLUSH_SYNC); 15557f2f12d9STrond Myklebust if (ret < 0) 15567f2f12d9STrond Myklebust goto out_error; 15577f2f12d9STrond Myklebust } 15587f2f12d9STrond Myklebust return 0; 15597f2f12d9STrond Myklebust out_error: 15607f2f12d9STrond Myklebust return ret; 15611c75950bSTrond Myklebust } 15621c75950bSTrond Myklebust 1563074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION 1564074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage, 1565074cc1deSTrond Myklebust struct page *page) 1566074cc1deSTrond Myklebust { 1567074cc1deSTrond Myklebust struct nfs_page *req; 1568074cc1deSTrond Myklebust int ret; 1569074cc1deSTrond Myklebust 1570074cc1deSTrond Myklebust nfs_fscache_release_page(page, GFP_KERNEL); 1571074cc1deSTrond Myklebust 1572cfb506e1STrond Myklebust req = nfs_find_and_lock_request(page, false); 1573074cc1deSTrond Myklebust ret = PTR_ERR(req); 1574074cc1deSTrond Myklebust if (IS_ERR(req)) 1575074cc1deSTrond Myklebust goto out; 1576074cc1deSTrond Myklebust 1577074cc1deSTrond Myklebust ret = migrate_page(mapping, newpage, page); 1578074cc1deSTrond Myklebust if (!req) 1579074cc1deSTrond Myklebust goto out; 1580074cc1deSTrond Myklebust if (ret) 1581074cc1deSTrond Myklebust goto out_unlock; 1582074cc1deSTrond Myklebust page_cache_get(newpage); 1583190f38e5STrond Myklebust spin_lock(&mapping->host->i_lock); 1584074cc1deSTrond Myklebust req->wb_page = newpage; 1585074cc1deSTrond Myklebust SetPagePrivate(newpage); 1586190f38e5STrond Myklebust set_page_private(newpage, (unsigned long)req); 1587074cc1deSTrond Myklebust ClearPagePrivate(page); 1588074cc1deSTrond Myklebust set_page_private(page, 0); 1589190f38e5STrond Myklebust spin_unlock(&mapping->host->i_lock); 1590074cc1deSTrond Myklebust page_cache_release(page); 1591074cc1deSTrond Myklebust out_unlock: 1592074cc1deSTrond Myklebust nfs_clear_page_tag_locked(req); 1593074cc1deSTrond Myklebust out: 1594074cc1deSTrond Myklebust return ret; 1595074cc1deSTrond Myklebust } 1596074cc1deSTrond Myklebust #endif 1597074cc1deSTrond Myklebust 1598f7b422b1SDavid Howells int __init nfs_init_writepagecache(void) 15991da177e4SLinus Torvalds { 16001da177e4SLinus Torvalds nfs_wdata_cachep = kmem_cache_create("nfs_write_data", 16011da177e4SLinus Torvalds sizeof(struct nfs_write_data), 16021da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 160320c2df83SPaul Mundt NULL); 16041da177e4SLinus Torvalds if (nfs_wdata_cachep == NULL) 16051da177e4SLinus Torvalds return -ENOMEM; 16061da177e4SLinus Torvalds 160793d2341cSMatthew Dobson nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, 16081da177e4SLinus Torvalds nfs_wdata_cachep); 16091da177e4SLinus Torvalds if (nfs_wdata_mempool == NULL) 16101da177e4SLinus Torvalds return -ENOMEM; 16111da177e4SLinus Torvalds 161293d2341cSMatthew Dobson nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, 16131da177e4SLinus Torvalds nfs_wdata_cachep); 16141da177e4SLinus Torvalds if (nfs_commit_mempool == NULL) 16151da177e4SLinus Torvalds return -ENOMEM; 16161da177e4SLinus Torvalds 161789a09141SPeter Zijlstra /* 161889a09141SPeter Zijlstra * NFS congestion size, scale with available memory. 161989a09141SPeter Zijlstra * 162089a09141SPeter Zijlstra * 64MB: 8192k 162189a09141SPeter Zijlstra * 128MB: 11585k 162289a09141SPeter Zijlstra * 256MB: 16384k 162389a09141SPeter Zijlstra * 512MB: 23170k 162489a09141SPeter Zijlstra * 1GB: 32768k 162589a09141SPeter Zijlstra * 2GB: 46340k 162689a09141SPeter Zijlstra * 4GB: 65536k 162789a09141SPeter Zijlstra * 8GB: 92681k 162889a09141SPeter Zijlstra * 16GB: 131072k 162989a09141SPeter Zijlstra * 163089a09141SPeter Zijlstra * This allows larger machines to have larger/more transfers. 163189a09141SPeter Zijlstra * Limit the default to 256M 163289a09141SPeter Zijlstra */ 163389a09141SPeter Zijlstra nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); 163489a09141SPeter Zijlstra if (nfs_congestion_kb > 256*1024) 163589a09141SPeter Zijlstra nfs_congestion_kb = 256*1024; 163689a09141SPeter Zijlstra 16371da177e4SLinus Torvalds return 0; 16381da177e4SLinus Torvalds } 16391da177e4SLinus Torvalds 1640266bee88SDavid Brownell void nfs_destroy_writepagecache(void) 16411da177e4SLinus Torvalds { 16421da177e4SLinus Torvalds mempool_destroy(nfs_commit_mempool); 16431da177e4SLinus Torvalds mempool_destroy(nfs_wdata_mempool); 16441a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_wdata_cachep); 16451da177e4SLinus Torvalds } 16461da177e4SLinus Torvalds 1647