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> 23afeacc8cSPaul Gortmaker #include <linux/export.h> 243fcfab16SAndrew Morton 251da177e4SLinus Torvalds #include <asm/uaccess.h> 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds #include "delegation.h" 2849a70f27STrond Myklebust #include "internal.h" 2991d5b470SChuck Lever #include "iostat.h" 30def6ed7eSAndy Adamson #include "nfs4_fs.h" 31074cc1deSTrond Myklebust #include "fscache.h" 3294ad1c80SFred Isaman #include "pnfs.h" 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_PAGECACHE 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds #define MIN_POOL_WRITE (32) 371da177e4SLinus Torvalds #define MIN_POOL_COMMIT (4) 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds /* 401da177e4SLinus Torvalds * Local function declarations 411da177e4SLinus Torvalds */ 42c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc, 43c63c7b05STrond Myklebust struct inode *inode, int ioflags); 44f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req); 45788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops; 46788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops; 47788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops; 481da177e4SLinus Torvalds 49e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep; 503feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool; 510b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep; 521da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool; 531da177e4SLinus Torvalds 540b7c0153SFred Isaman struct nfs_commit_data *nfs_commitdata_alloc(void) 551da177e4SLinus Torvalds { 560b7c0153SFred Isaman struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS); 5740859d7eSChuck Lever 581da177e4SLinus Torvalds if (p) { 591da177e4SLinus Torvalds memset(p, 0, sizeof(*p)); 601da177e4SLinus Torvalds INIT_LIST_HEAD(&p->pages); 611da177e4SLinus Torvalds } 621da177e4SLinus Torvalds return p; 631da177e4SLinus Torvalds } 64e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc); 651da177e4SLinus Torvalds 660b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p) 671da177e4SLinus Torvalds { 681da177e4SLinus Torvalds mempool_free(p, nfs_commit_mempool); 691da177e4SLinus Torvalds } 70e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free); 711da177e4SLinus Torvalds 72cd841605SFred Isaman struct nfs_write_header *nfs_writehdr_alloc(unsigned int pagecount) 733feb2d49STrond Myklebust { 74cd841605SFred Isaman struct nfs_write_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS); 753feb2d49STrond Myklebust 763feb2d49STrond Myklebust if (p) { 77cd841605SFred Isaman struct nfs_pgio_header *hdr = &p->header; 78cd841605SFred Isaman struct nfs_write_data *data = &p->rpc_data; 79cd841605SFred Isaman 803feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 81cd841605SFred Isaman INIT_LIST_HEAD(&hdr->pages); 82cd841605SFred Isaman INIT_LIST_HEAD(&data->list); 83cd841605SFred Isaman data->header = hdr; 84*30dd374fSFred Isaman if (!nfs_pgarray_set(&data->pages, pagecount)) { 853feb2d49STrond Myklebust mempool_free(p, nfs_wdata_mempool); 863feb2d49STrond Myklebust p = NULL; 873feb2d49STrond Myklebust } 883feb2d49STrond Myklebust } 893feb2d49STrond Myklebust return p; 903feb2d49STrond Myklebust } 913feb2d49STrond Myklebust 92cd841605SFred Isaman void nfs_writehdr_free(struct nfs_pgio_header *hdr) 933feb2d49STrond Myklebust { 94cd841605SFred Isaman struct nfs_write_header *whdr = container_of(hdr, struct nfs_write_header, header); 95cd841605SFred Isaman mempool_free(whdr, nfs_wdata_mempool); 963feb2d49STrond Myklebust } 973feb2d49STrond Myklebust 98dce81290STrond Myklebust void nfs_writedata_release(struct nfs_write_data *wdata) 991da177e4SLinus Torvalds { 100383ba719STrond Myklebust put_nfs_open_context(wdata->args.context); 101*30dd374fSFred Isaman if (wdata->pages.pagevec != wdata->pages.page_array) 102*30dd374fSFred Isaman kfree(wdata->pages.pagevec); 103cd841605SFred Isaman nfs_writehdr_free(wdata->header); 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds 1067b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error) 1077b159fc1STrond Myklebust { 1087b159fc1STrond Myklebust ctx->error = error; 1097b159fc1STrond Myklebust smp_wmb(); 1107b159fc1STrond Myklebust set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 1117b159fc1STrond Myklebust } 1127b159fc1STrond Myklebust 113277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page) 114277459d2STrond Myklebust { 115277459d2STrond Myklebust struct nfs_page *req = NULL; 116277459d2STrond Myklebust 117277459d2STrond Myklebust if (PagePrivate(page)) { 118277459d2STrond Myklebust req = (struct nfs_page *)page_private(page); 119277459d2STrond Myklebust if (req != NULL) 120c03b4024STrond Myklebust kref_get(&req->wb_kref); 121277459d2STrond Myklebust } 122277459d2STrond Myklebust return req; 123277459d2STrond Myklebust } 124277459d2STrond Myklebust 125277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page) 126277459d2STrond Myklebust { 127587142f8STrond Myklebust struct inode *inode = page->mapping->host; 128277459d2STrond Myklebust struct nfs_page *req = NULL; 129277459d2STrond Myklebust 130587142f8STrond Myklebust spin_lock(&inode->i_lock); 131277459d2STrond Myklebust req = nfs_page_find_request_locked(page); 132587142f8STrond Myklebust spin_unlock(&inode->i_lock); 133277459d2STrond Myklebust return req; 134277459d2STrond Myklebust } 135277459d2STrond Myklebust 1361da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */ 1371da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) 1381da177e4SLinus Torvalds { 1391da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 140a3d01454STrond Myklebust loff_t end, i_size; 141a3d01454STrond Myklebust pgoff_t end_index; 1421da177e4SLinus Torvalds 143a3d01454STrond Myklebust spin_lock(&inode->i_lock); 144a3d01454STrond Myklebust i_size = i_size_read(inode); 145a3d01454STrond Myklebust end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; 1461da177e4SLinus Torvalds if (i_size > 0 && page->index < end_index) 147a3d01454STrond Myklebust goto out; 1481da177e4SLinus Torvalds end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count); 1491da177e4SLinus Torvalds if (i_size >= end) 150a3d01454STrond Myklebust goto out; 1511da177e4SLinus Torvalds i_size_write(inode, end); 152a3d01454STrond Myklebust nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); 153a3d01454STrond Myklebust out: 154a3d01454STrond Myklebust spin_unlock(&inode->i_lock); 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds 157a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */ 158a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page) 159a301b777STrond Myklebust { 160a301b777STrond Myklebust SetPageError(page); 161a301b777STrond Myklebust nfs_zap_mapping(page->mapping->host, page->mapping); 162a301b777STrond Myklebust } 163a301b777STrond Myklebust 1641da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request 1651da177e4SLinus Torvalds * covers the full page. 1661da177e4SLinus Torvalds */ 1671da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count) 1681da177e4SLinus Torvalds { 1691da177e4SLinus Torvalds if (PageUptodate(page)) 1701da177e4SLinus Torvalds return; 1711da177e4SLinus Torvalds if (base != 0) 1721da177e4SLinus Torvalds return; 17349a70f27STrond Myklebust if (count != nfs_page_length(page)) 1741da177e4SLinus Torvalds return; 1751da177e4SLinus Torvalds SetPageUptodate(page); 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc) 1791da177e4SLinus Torvalds { 1801da177e4SLinus Torvalds if (wbc->for_reclaim) 181c63c7b05STrond Myklebust return FLUSH_HIGHPRI | FLUSH_STABLE; 182b17621feSWu Fengguang if (wbc->for_kupdate || wbc->for_background) 183b31268acSTrond Myklebust return FLUSH_LOWPRI | FLUSH_COND_STABLE; 184b31268acSTrond Myklebust return FLUSH_COND_STABLE; 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds /* 18889a09141SPeter Zijlstra * NFS congestion control 18989a09141SPeter Zijlstra */ 19089a09141SPeter Zijlstra 19189a09141SPeter Zijlstra int nfs_congestion_kb; 19289a09141SPeter Zijlstra 19389a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) 19489a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH \ 19589a09141SPeter Zijlstra (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) 19689a09141SPeter Zijlstra 1975a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page) 19889a09141SPeter Zijlstra { 1995a6d41b3STrond Myklebust int ret = test_set_page_writeback(page); 2005a6d41b3STrond Myklebust 2015a6d41b3STrond Myklebust if (!ret) { 20289a09141SPeter Zijlstra struct inode *inode = page->mapping->host; 20389a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 20489a09141SPeter Zijlstra 205a6305ddbSTrond Myklebust page_cache_get(page); 206277866a0SPeter Zijlstra if (atomic_long_inc_return(&nfss->writeback) > 2078aa7e847SJens Axboe NFS_CONGESTION_ON_THRESH) { 2088aa7e847SJens Axboe set_bdi_congested(&nfss->backing_dev_info, 2098aa7e847SJens Axboe BLK_RW_ASYNC); 2108aa7e847SJens Axboe } 21189a09141SPeter Zijlstra } 2125a6d41b3STrond Myklebust return ret; 21389a09141SPeter Zijlstra } 21489a09141SPeter Zijlstra 21589a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page) 21689a09141SPeter Zijlstra { 21789a09141SPeter Zijlstra struct inode *inode = page->mapping->host; 21889a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 21989a09141SPeter Zijlstra 22089a09141SPeter Zijlstra end_page_writeback(page); 221a6305ddbSTrond Myklebust page_cache_release(page); 222c4dc4beeSPeter Zijlstra if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) 2238aa7e847SJens Axboe clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC); 22489a09141SPeter Zijlstra } 22589a09141SPeter Zijlstra 226cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock) 227e261f51fSTrond Myklebust { 228587142f8STrond Myklebust struct inode *inode = page->mapping->host; 229e261f51fSTrond Myklebust struct nfs_page *req; 230e261f51fSTrond Myklebust int ret; 231e261f51fSTrond Myklebust 232587142f8STrond Myklebust spin_lock(&inode->i_lock); 233e261f51fSTrond Myklebust for (;;) { 234e261f51fSTrond Myklebust req = nfs_page_find_request_locked(page); 235074cc1deSTrond Myklebust if (req == NULL) 236074cc1deSTrond Myklebust break; 2379994b62bSFred Isaman if (nfs_lock_request_dontget(req)) 238e261f51fSTrond Myklebust break; 239e261f51fSTrond Myklebust /* Note: If we hold the page lock, as is the case in nfs_writepage, 2409994b62bSFred Isaman * then the call to nfs_lock_request_dontget() will always 241e261f51fSTrond Myklebust * succeed provided that someone hasn't already marked the 242e261f51fSTrond Myklebust * request as dirty (in which case we don't care). 243e261f51fSTrond Myklebust */ 244587142f8STrond Myklebust spin_unlock(&inode->i_lock); 245cfb506e1STrond Myklebust if (!nonblock) 246e261f51fSTrond Myklebust ret = nfs_wait_on_request(req); 247cfb506e1STrond Myklebust else 248cfb506e1STrond Myklebust ret = -EAGAIN; 249e261f51fSTrond Myklebust nfs_release_request(req); 250e261f51fSTrond Myklebust if (ret != 0) 251074cc1deSTrond Myklebust return ERR_PTR(ret); 252587142f8STrond Myklebust spin_lock(&inode->i_lock); 253e261f51fSTrond Myklebust } 254587142f8STrond Myklebust spin_unlock(&inode->i_lock); 255074cc1deSTrond Myklebust return req; 256612c9384STrond Myklebust } 257074cc1deSTrond Myklebust 258074cc1deSTrond Myklebust /* 259074cc1deSTrond Myklebust * Find an associated nfs write request, and prepare to flush it out 260074cc1deSTrond Myklebust * May return an error if the user signalled nfs_wait_on_request(). 261074cc1deSTrond Myklebust */ 262074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, 263cfb506e1STrond Myklebust struct page *page, bool nonblock) 264074cc1deSTrond Myklebust { 265074cc1deSTrond Myklebust struct nfs_page *req; 266074cc1deSTrond Myklebust int ret = 0; 267074cc1deSTrond Myklebust 268cfb506e1STrond Myklebust req = nfs_find_and_lock_request(page, nonblock); 269074cc1deSTrond Myklebust if (!req) 270074cc1deSTrond Myklebust goto out; 271074cc1deSTrond Myklebust ret = PTR_ERR(req); 272074cc1deSTrond Myklebust if (IS_ERR(req)) 273074cc1deSTrond Myklebust goto out; 274074cc1deSTrond Myklebust 275074cc1deSTrond Myklebust ret = nfs_set_page_writeback(page); 276074cc1deSTrond Myklebust BUG_ON(ret != 0); 277074cc1deSTrond Myklebust BUG_ON(test_bit(PG_CLEAN, &req->wb_flags)); 278074cc1deSTrond Myklebust 279f8512ad0SFred Isaman if (!nfs_pageio_add_request(pgio, req)) { 280f8512ad0SFred Isaman nfs_redirty_request(req); 281074cc1deSTrond Myklebust ret = pgio->pg_error; 282f8512ad0SFred Isaman } 283074cc1deSTrond Myklebust out: 284074cc1deSTrond Myklebust return ret; 285e261f51fSTrond Myklebust } 286e261f51fSTrond Myklebust 287f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio) 288f758c885STrond Myklebust { 289f758c885STrond Myklebust struct inode *inode = page->mapping->host; 290cfb506e1STrond Myklebust int ret; 291f758c885STrond Myklebust 292f758c885STrond Myklebust nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); 293f758c885STrond Myklebust nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); 294f758c885STrond Myklebust 295f758c885STrond Myklebust nfs_pageio_cond_complete(pgio, page->index); 2961b430beeSWu Fengguang ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE); 297cfb506e1STrond Myklebust if (ret == -EAGAIN) { 298cfb506e1STrond Myklebust redirty_page_for_writepage(wbc, page); 299cfb506e1STrond Myklebust ret = 0; 300cfb506e1STrond Myklebust } 301cfb506e1STrond Myklebust return ret; 302f758c885STrond Myklebust } 303f758c885STrond Myklebust 304e261f51fSTrond Myklebust /* 3051da177e4SLinus Torvalds * Write an mmapped page to the server. 3061da177e4SLinus Torvalds */ 3074d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc) 3081da177e4SLinus Torvalds { 309f758c885STrond Myklebust struct nfs_pageio_descriptor pgio; 310e261f51fSTrond Myklebust int err; 3111da177e4SLinus Torvalds 312f758c885STrond Myklebust nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc)); 313f758c885STrond Myklebust err = nfs_do_writepage(page, wbc, &pgio); 314f758c885STrond Myklebust nfs_pageio_complete(&pgio); 315f758c885STrond Myklebust if (err < 0) 3164d770ccfSTrond Myklebust return err; 317f758c885STrond Myklebust if (pgio.pg_error < 0) 318f758c885STrond Myklebust return pgio.pg_error; 319f758c885STrond Myklebust return 0; 3204d770ccfSTrond Myklebust } 3214d770ccfSTrond Myklebust 3224d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc) 3234d770ccfSTrond Myklebust { 324f758c885STrond Myklebust int ret; 3254d770ccfSTrond Myklebust 326f758c885STrond Myklebust ret = nfs_writepage_locked(page, wbc); 3271da177e4SLinus Torvalds unlock_page(page); 328f758c885STrond Myklebust return ret; 329f758c885STrond Myklebust } 330f758c885STrond Myklebust 331f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) 332f758c885STrond Myklebust { 333f758c885STrond Myklebust int ret; 334f758c885STrond Myklebust 335f758c885STrond Myklebust ret = nfs_do_writepage(page, wbc, data); 336f758c885STrond Myklebust unlock_page(page); 337f758c885STrond Myklebust return ret; 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) 3411da177e4SLinus Torvalds { 3421da177e4SLinus Torvalds struct inode *inode = mapping->host; 34372cb77f4STrond Myklebust unsigned long *bitlock = &NFS_I(inode)->flags; 344c63c7b05STrond Myklebust struct nfs_pageio_descriptor pgio; 3451da177e4SLinus Torvalds int err; 3461da177e4SLinus Torvalds 34772cb77f4STrond Myklebust /* Stop dirtying of new pages while we sync */ 34872cb77f4STrond Myklebust err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING, 34972cb77f4STrond Myklebust nfs_wait_bit_killable, TASK_KILLABLE); 35072cb77f4STrond Myklebust if (err) 35172cb77f4STrond Myklebust goto out_err; 35272cb77f4STrond Myklebust 35391d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); 35491d5b470SChuck Lever 355c63c7b05STrond Myklebust nfs_pageio_init_write(&pgio, inode, wb_priority(wbc)); 356f758c885STrond Myklebust err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); 357c63c7b05STrond Myklebust nfs_pageio_complete(&pgio); 35872cb77f4STrond Myklebust 35972cb77f4STrond Myklebust clear_bit_unlock(NFS_INO_FLUSHING, bitlock); 36072cb77f4STrond Myklebust smp_mb__after_clear_bit(); 36172cb77f4STrond Myklebust wake_up_bit(bitlock, NFS_INO_FLUSHING); 36272cb77f4STrond Myklebust 363f758c885STrond Myklebust if (err < 0) 36472cb77f4STrond Myklebust goto out_err; 36572cb77f4STrond Myklebust err = pgio.pg_error; 36672cb77f4STrond Myklebust if (err < 0) 36772cb77f4STrond Myklebust goto out_err; 368c63c7b05STrond Myklebust return 0; 36972cb77f4STrond Myklebust out_err: 37072cb77f4STrond Myklebust return err; 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds /* 3741da177e4SLinus Torvalds * Insert a write request into an inode 3751da177e4SLinus Torvalds */ 376d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req) 3771da177e4SLinus Torvalds { 3781da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 379e7d39069STrond Myklebust 380e7d39069STrond Myklebust /* Lock the request! */ 381e7d39069STrond Myklebust nfs_lock_request_dontget(req); 382e7d39069STrond Myklebust 383e7d39069STrond Myklebust spin_lock(&inode->i_lock); 3844d65c520STrond Myklebust if (!nfsi->npages && nfs_have_delegation(inode, FMODE_WRITE)) 385a9a4a87aSTrond Myklebust inode->i_version++; 3862df485a7STrond Myklebust set_bit(PG_MAPPED, &req->wb_flags); 387deb7d638STrond Myklebust SetPagePrivate(req->wb_page); 388277459d2STrond Myklebust set_page_private(req->wb_page, (unsigned long)req); 3891da177e4SLinus Torvalds nfsi->npages++; 390c03b4024STrond Myklebust kref_get(&req->wb_kref); 391e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds /* 39589a09141SPeter Zijlstra * Remove a write request from an inode 3961da177e4SLinus Torvalds */ 3971da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req) 3981da177e4SLinus Torvalds { 3993d4ff43dSAl Viro struct inode *inode = req->wb_context->dentry->d_inode; 4001da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds BUG_ON (!NFS_WBACK_BUSY(req)); 4031da177e4SLinus Torvalds 404587142f8STrond Myklebust spin_lock(&inode->i_lock); 405277459d2STrond Myklebust set_page_private(req->wb_page, 0); 406deb7d638STrond Myklebust ClearPagePrivate(req->wb_page); 4072df485a7STrond Myklebust clear_bit(PG_MAPPED, &req->wb_flags); 4081da177e4SLinus Torvalds nfsi->npages--; 409587142f8STrond Myklebust spin_unlock(&inode->i_lock); 4101da177e4SLinus Torvalds nfs_release_request(req); 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 41361822ab5STrond Myklebust static void 4146d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req) 41561822ab5STrond Myklebust { 41661822ab5STrond Myklebust __set_page_dirty_nobuffers(req->wb_page); 41761822ab5STrond Myklebust } 41861822ab5STrond Myklebust 4191da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 4208dd37758STrond Myklebust /** 4218dd37758STrond Myklebust * nfs_request_add_commit_list - add request to a commit list 4228dd37758STrond Myklebust * @req: pointer to a struct nfs_page 4238dd37758STrond Myklebust * @head: commit list head 4248dd37758STrond Myklebust * 4258dd37758STrond Myklebust * This sets the PG_CLEAN bit, updates the inode global count of 4268dd37758STrond Myklebust * number of outstanding requests requiring a commit as well as 4278dd37758STrond Myklebust * the MM page stats. 4288dd37758STrond Myklebust * 4298dd37758STrond Myklebust * The caller must _not_ hold the inode->i_lock, but must be 4308dd37758STrond Myklebust * holding the nfs_page lock. 4318dd37758STrond Myklebust */ 4328dd37758STrond Myklebust void 4338dd37758STrond Myklebust nfs_request_add_commit_list(struct nfs_page *req, struct list_head *head) 4348dd37758STrond Myklebust { 4358dd37758STrond Myklebust struct inode *inode = req->wb_context->dentry->d_inode; 4368dd37758STrond Myklebust 4378dd37758STrond Myklebust set_bit(PG_CLEAN, &(req)->wb_flags); 4388dd37758STrond Myklebust spin_lock(&inode->i_lock); 4398dd37758STrond Myklebust nfs_list_add_request(req, head); 4408dd37758STrond Myklebust NFS_I(inode)->ncommit++; 4418dd37758STrond Myklebust spin_unlock(&inode->i_lock); 4428dd37758STrond Myklebust inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 4438dd37758STrond Myklebust inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE); 4448dd37758STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 4458dd37758STrond Myklebust } 4468dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list); 4478dd37758STrond Myklebust 4488dd37758STrond Myklebust /** 4498dd37758STrond Myklebust * nfs_request_remove_commit_list - Remove request from a commit list 4508dd37758STrond Myklebust * @req: pointer to a nfs_page 4518dd37758STrond Myklebust * 4528dd37758STrond Myklebust * This clears the PG_CLEAN bit, and updates the inode global count of 4538dd37758STrond Myklebust * number of outstanding requests requiring a commit 4548dd37758STrond Myklebust * It does not update the MM page stats. 4558dd37758STrond Myklebust * 4568dd37758STrond Myklebust * The caller _must_ hold the inode->i_lock and the nfs_page lock. 4578dd37758STrond Myklebust */ 4588dd37758STrond Myklebust void 4598dd37758STrond Myklebust nfs_request_remove_commit_list(struct nfs_page *req) 4608dd37758STrond Myklebust { 4618dd37758STrond Myklebust struct inode *inode = req->wb_context->dentry->d_inode; 4628dd37758STrond Myklebust 4638dd37758STrond Myklebust if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) 4648dd37758STrond Myklebust return; 4658dd37758STrond Myklebust nfs_list_remove_request(req); 4668dd37758STrond Myklebust NFS_I(inode)->ncommit--; 4678dd37758STrond Myklebust } 4688dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list); 4698dd37758STrond Myklebust 4708dd37758STrond Myklebust 4711da177e4SLinus Torvalds /* 4721da177e4SLinus Torvalds * Add a request to the inode's commit list. 4731da177e4SLinus Torvalds */ 4741da177e4SLinus Torvalds static void 475a861a1e1SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg) 4761da177e4SLinus Torvalds { 4773d4ff43dSAl Viro struct inode *inode = req->wb_context->dentry->d_inode; 4781da177e4SLinus Torvalds 4798dd37758STrond Myklebust if (pnfs_mark_request_commit(req, lseg)) 4808dd37758STrond Myklebust return; 4818dd37758STrond Myklebust nfs_request_add_commit_list(req, &NFS_I(inode)->commit_list); 4821da177e4SLinus Torvalds } 4838e821cadSTrond Myklebust 484d6d6dc7cSFred Isaman static void 485d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page) 486e468bae9STrond Myklebust { 487e468bae9STrond Myklebust dec_zone_page_state(page, NR_UNSTABLE_NFS); 488e468bae9STrond Myklebust dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE); 489e468bae9STrond Myklebust } 490d6d6dc7cSFred Isaman 4918dd37758STrond Myklebust static void 492d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req) 493d6d6dc7cSFred Isaman { 4948dd37758STrond Myklebust if (test_bit(PG_CLEAN, &req->wb_flags)) { 4958dd37758STrond Myklebust struct inode *inode = req->wb_context->dentry->d_inode; 496d6d6dc7cSFred Isaman 4978dd37758STrond Myklebust if (!pnfs_clear_request_commit(req)) { 4988dd37758STrond Myklebust spin_lock(&inode->i_lock); 4998dd37758STrond Myklebust nfs_request_remove_commit_list(req); 5008dd37758STrond Myklebust spin_unlock(&inode->i_lock); 501d6d6dc7cSFred Isaman } 5028dd37758STrond Myklebust nfs_clear_page_commit(req->wb_page); 5038dd37758STrond Myklebust } 504e468bae9STrond Myklebust } 505e468bae9STrond Myklebust 5068e821cadSTrond Myklebust static inline 5078e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 5088e821cadSTrond Myklebust { 509465d5243SFred Isaman if (data->verf.committed == NFS_DATA_SYNC) 510cd841605SFred Isaman return data->header->lseg == NULL; 5118e821cadSTrond Myklebust return data->verf.committed != NFS_FILE_SYNC; 5128e821cadSTrond Myklebust } 5138e821cadSTrond Myklebust 5148e821cadSTrond Myklebust static inline 515a861a1e1SFred Isaman int nfs_reschedule_unstable_write(struct nfs_page *req, 516a861a1e1SFred Isaman struct nfs_write_data *data) 5178e821cadSTrond Myklebust { 518e468bae9STrond Myklebust if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) { 519cd841605SFred Isaman nfs_mark_request_commit(req, data->header->lseg); 5208e821cadSTrond Myklebust return 1; 5218e821cadSTrond Myklebust } 5228e821cadSTrond Myklebust if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) { 5236d884e8fSFred nfs_mark_request_dirty(req); 5248e821cadSTrond Myklebust return 1; 5258e821cadSTrond Myklebust } 5268e821cadSTrond Myklebust return 0; 5278e821cadSTrond Myklebust } 5288e821cadSTrond Myklebust #else 5298dd37758STrond Myklebust static void 530a861a1e1SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg) 5318e821cadSTrond Myklebust { 5328e821cadSTrond Myklebust } 5338e821cadSTrond Myklebust 5348dd37758STrond Myklebust static void 535e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req) 536e468bae9STrond Myklebust { 537e468bae9STrond Myklebust } 538e468bae9STrond Myklebust 5398e821cadSTrond Myklebust static inline 5408e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 5418e821cadSTrond Myklebust { 5428e821cadSTrond Myklebust return 0; 5438e821cadSTrond Myklebust } 5448e821cadSTrond Myklebust 5458e821cadSTrond Myklebust static inline 546a861a1e1SFred Isaman int nfs_reschedule_unstable_write(struct nfs_page *req, 547a861a1e1SFred Isaman struct nfs_write_data *data) 5488e821cadSTrond Myklebust { 5498e821cadSTrond Myklebust return 0; 5508e821cadSTrond Myklebust } 5511da177e4SLinus Torvalds #endif 5521da177e4SLinus Torvalds 55347c62564STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 554fb8a1f11STrond Myklebust static int 555fb8a1f11STrond Myklebust nfs_need_commit(struct nfs_inode *nfsi) 556fb8a1f11STrond Myklebust { 557d6d6dc7cSFred Isaman return nfsi->ncommit > 0; 558fb8a1f11STrond Myklebust } 559fb8a1f11STrond Myklebust 560d6d6dc7cSFred Isaman /* i_lock held by caller */ 5618dd37758STrond Myklebust static int 5623b3be88dSTrond Myklebust nfs_scan_commit_list(struct list_head *src, struct list_head *dst, int max, 5633b3be88dSTrond Myklebust spinlock_t *lock) 564d6d6dc7cSFred Isaman { 565d6d6dc7cSFred Isaman struct nfs_page *req, *tmp; 566d6d6dc7cSFred Isaman int ret = 0; 567d6d6dc7cSFred Isaman 568d6d6dc7cSFred Isaman list_for_each_entry_safe(req, tmp, src, wb_list) { 5698dd37758STrond Myklebust if (!nfs_lock_request(req)) 5708dd37758STrond Myklebust continue; 5713b3be88dSTrond Myklebust if (cond_resched_lock(lock)) 5723b3be88dSTrond Myklebust list_safe_reset_next(req, tmp, wb_list); 5738dd37758STrond Myklebust nfs_request_remove_commit_list(req); 5748dd37758STrond Myklebust nfs_list_add_request(req, dst); 575d6d6dc7cSFred Isaman ret++; 576d6d6dc7cSFred Isaman if (ret == max) 577d6d6dc7cSFred Isaman break; 578d6d6dc7cSFred Isaman } 579d6d6dc7cSFred Isaman return ret; 580d6d6dc7cSFred Isaman } 581d6d6dc7cSFred Isaman 5821da177e4SLinus Torvalds /* 5831da177e4SLinus Torvalds * nfs_scan_commit - Scan an inode for commit requests 5841da177e4SLinus Torvalds * @inode: NFS inode to scan 5851da177e4SLinus Torvalds * @dst: destination list 5861da177e4SLinus Torvalds * 5871da177e4SLinus Torvalds * Moves requests from the inode's 'commit' request list. 5881da177e4SLinus Torvalds * The requests are *not* checked to ensure that they form a contiguous set. 5891da177e4SLinus Torvalds */ 5901da177e4SLinus Torvalds static int 591d6d6dc7cSFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst) 5921da177e4SLinus Torvalds { 5931da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 594d6d6dc7cSFred Isaman int ret = 0; 595fb8a1f11STrond Myklebust 5960d88f6e8SDave Chinner spin_lock(&inode->i_lock); 597d6d6dc7cSFred Isaman if (nfsi->ncommit > 0) { 5988dd37758STrond Myklebust const int max = INT_MAX; 599d6d6dc7cSFred Isaman 6003b3be88dSTrond Myklebust ret = nfs_scan_commit_list(&nfsi->commit_list, dst, max, 6013b3be88dSTrond Myklebust &inode->i_lock); 6023b3be88dSTrond Myklebust ret += pnfs_scan_commit_lists(inode, max - ret, 6033b3be88dSTrond Myklebust &inode->i_lock); 604d6d6dc7cSFred Isaman } 6050d88f6e8SDave Chinner spin_unlock(&inode->i_lock); 606ff778d02STrond Myklebust return ret; 6071da177e4SLinus Torvalds } 608d6d6dc7cSFred Isaman 609c42de9ddSTrond Myklebust #else 610fb8a1f11STrond Myklebust static inline int nfs_need_commit(struct nfs_inode *nfsi) 611fb8a1f11STrond Myklebust { 612fb8a1f11STrond Myklebust return 0; 613fb8a1f11STrond Myklebust } 614fb8a1f11STrond Myklebust 615d6d6dc7cSFred Isaman static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst) 616c42de9ddSTrond Myklebust { 617c42de9ddSTrond Myklebust return 0; 618c42de9ddSTrond Myklebust } 6191da177e4SLinus Torvalds #endif 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds /* 622e7d39069STrond Myklebust * Search for an existing write request, and attempt to update 623e7d39069STrond Myklebust * it to reflect a new dirty region on a given page. 6241da177e4SLinus Torvalds * 625e7d39069STrond Myklebust * If the attempt fails, then the existing request is flushed out 626e7d39069STrond Myklebust * to disk. 6271da177e4SLinus Torvalds */ 628e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode, 629e7d39069STrond Myklebust struct page *page, 630e7d39069STrond Myklebust unsigned int offset, 631e7d39069STrond Myklebust unsigned int bytes) 6321da177e4SLinus Torvalds { 633e7d39069STrond Myklebust struct nfs_page *req; 634e7d39069STrond Myklebust unsigned int rqend; 635e7d39069STrond Myklebust unsigned int end; 6361da177e4SLinus Torvalds int error; 637277459d2STrond Myklebust 638e7d39069STrond Myklebust if (!PagePrivate(page)) 639e7d39069STrond Myklebust return NULL; 640e7d39069STrond Myklebust 641e7d39069STrond Myklebust end = offset + bytes; 642e7d39069STrond Myklebust spin_lock(&inode->i_lock); 643e7d39069STrond Myklebust 644e7d39069STrond Myklebust for (;;) { 645e7d39069STrond Myklebust req = nfs_page_find_request_locked(page); 646e7d39069STrond Myklebust if (req == NULL) 647e7d39069STrond Myklebust goto out_unlock; 648e7d39069STrond Myklebust 649e7d39069STrond Myklebust rqend = req->wb_offset + req->wb_bytes; 650e7d39069STrond Myklebust /* 651e7d39069STrond Myklebust * Tell the caller to flush out the request if 652e7d39069STrond Myklebust * the offsets are non-contiguous. 653e7d39069STrond Myklebust * Note: nfs_flush_incompatible() will already 654e7d39069STrond Myklebust * have flushed out requests having wrong owners. 655e7d39069STrond Myklebust */ 656e468bae9STrond Myklebust if (offset > rqend 657e7d39069STrond Myklebust || end < req->wb_offset) 658e7d39069STrond Myklebust goto out_flushme; 659e7d39069STrond Myklebust 6609994b62bSFred Isaman if (nfs_lock_request_dontget(req)) 661e7d39069STrond Myklebust break; 662e7d39069STrond Myklebust 663e7d39069STrond Myklebust /* The request is locked, so wait and then retry */ 664587142f8STrond Myklebust spin_unlock(&inode->i_lock); 6651da177e4SLinus Torvalds error = nfs_wait_on_request(req); 6661da177e4SLinus Torvalds nfs_release_request(req); 667e7d39069STrond Myklebust if (error != 0) 668e7d39069STrond Myklebust goto out_err; 669e7d39069STrond Myklebust spin_lock(&inode->i_lock); 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds /* Okay, the request matches. Update the region */ 6731da177e4SLinus Torvalds if (offset < req->wb_offset) { 6741da177e4SLinus Torvalds req->wb_offset = offset; 6751da177e4SLinus Torvalds req->wb_pgbase = offset; 6761da177e4SLinus Torvalds } 6771da177e4SLinus Torvalds if (end > rqend) 6781da177e4SLinus Torvalds req->wb_bytes = end - req->wb_offset; 679e7d39069STrond Myklebust else 680e7d39069STrond Myklebust req->wb_bytes = rqend - req->wb_offset; 681e7d39069STrond Myklebust out_unlock: 682e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 683ca138f36SFred Isaman if (req) 6848dd37758STrond Myklebust nfs_clear_request_commit(req); 685e7d39069STrond Myklebust return req; 686e7d39069STrond Myklebust out_flushme: 687e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 688e7d39069STrond Myklebust nfs_release_request(req); 689e7d39069STrond Myklebust error = nfs_wb_page(inode, page); 690e7d39069STrond Myklebust out_err: 691e7d39069STrond Myklebust return ERR_PTR(error); 692e7d39069STrond Myklebust } 6931da177e4SLinus Torvalds 694e7d39069STrond Myklebust /* 695e7d39069STrond Myklebust * Try to update an existing write request, or create one if there is none. 696e7d39069STrond Myklebust * 697e7d39069STrond Myklebust * Note: Should always be called with the Page Lock held to prevent races 698e7d39069STrond Myklebust * if we have to add a new request. Also assumes that the caller has 699e7d39069STrond Myklebust * already called nfs_flush_incompatible() if necessary. 700e7d39069STrond Myklebust */ 701e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, 702e7d39069STrond Myklebust struct page *page, unsigned int offset, unsigned int bytes) 703e7d39069STrond Myklebust { 704e7d39069STrond Myklebust struct inode *inode = page->mapping->host; 705e7d39069STrond Myklebust struct nfs_page *req; 706e7d39069STrond Myklebust 707e7d39069STrond Myklebust req = nfs_try_to_update_request(inode, page, offset, bytes); 708e7d39069STrond Myklebust if (req != NULL) 709e7d39069STrond Myklebust goto out; 710e7d39069STrond Myklebust req = nfs_create_request(ctx, inode, page, offset, bytes); 711e7d39069STrond Myklebust if (IS_ERR(req)) 712e7d39069STrond Myklebust goto out; 713d6d6dc7cSFred Isaman nfs_inode_add_request(inode, req); 714efc91ed0STrond Myklebust out: 71561e930a9STrond Myklebust return req; 7161da177e4SLinus Torvalds } 7171da177e4SLinus Torvalds 718e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, 719e7d39069STrond Myklebust unsigned int offset, unsigned int count) 720e7d39069STrond Myklebust { 721e7d39069STrond Myklebust struct nfs_page *req; 722e7d39069STrond Myklebust 723e7d39069STrond Myklebust req = nfs_setup_write_request(ctx, page, offset, count); 724e7d39069STrond Myklebust if (IS_ERR(req)) 725e7d39069STrond Myklebust return PTR_ERR(req); 726e7d39069STrond Myklebust /* Update file length */ 727e7d39069STrond Myklebust nfs_grow_file(page, offset, count); 728e7d39069STrond Myklebust nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); 729a6305ddbSTrond Myklebust nfs_mark_request_dirty(req); 7309994b62bSFred Isaman nfs_unlock_request(req); 731e7d39069STrond Myklebust return 0; 732e7d39069STrond Myklebust } 733e7d39069STrond Myklebust 7341da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page) 7351da177e4SLinus Torvalds { 736cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 7371da177e4SLinus Torvalds struct nfs_page *req; 7381a54533eSTrond Myklebust int do_flush, status; 7391da177e4SLinus Torvalds /* 7401da177e4SLinus Torvalds * Look for a request corresponding to this page. If there 7411da177e4SLinus Torvalds * is one, and it belongs to another file, we flush it out 7421da177e4SLinus Torvalds * before we try to copy anything into the page. Do this 7431da177e4SLinus Torvalds * due to the lack of an ACCESS-type call in NFSv2. 7441da177e4SLinus Torvalds * Also do the same if we find a request from an existing 7451da177e4SLinus Torvalds * dropped page. 7461da177e4SLinus Torvalds */ 7471a54533eSTrond Myklebust do { 748277459d2STrond Myklebust req = nfs_page_find_request(page); 7491a54533eSTrond Myklebust if (req == NULL) 7501a54533eSTrond Myklebust return 0; 751f11ac8dbSTrond Myklebust do_flush = req->wb_page != page || req->wb_context != ctx || 752f11ac8dbSTrond Myklebust req->wb_lock_context->lockowner != current->files || 753f11ac8dbSTrond Myklebust req->wb_lock_context->pid != current->tgid; 7541da177e4SLinus Torvalds nfs_release_request(req); 7551a54533eSTrond Myklebust if (!do_flush) 7561a54533eSTrond Myklebust return 0; 757277459d2STrond Myklebust status = nfs_wb_page(page->mapping->host, page); 7581a54533eSTrond Myklebust } while (status == 0); 7591a54533eSTrond Myklebust return status; 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds /* 7635d47a356STrond Myklebust * If the page cache is marked as unsafe or invalid, then we can't rely on 7645d47a356STrond Myklebust * the PageUptodate() flag. In this case, we will need to turn off 7655d47a356STrond Myklebust * write optimisations that depend on the page contents being correct. 7665d47a356STrond Myklebust */ 7675d47a356STrond Myklebust static int nfs_write_pageuptodate(struct page *page, struct inode *inode) 7685d47a356STrond Myklebust { 7695d47a356STrond Myklebust return PageUptodate(page) && 7705d47a356STrond Myklebust !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA)); 7715d47a356STrond Myklebust } 7725d47a356STrond Myklebust 7735d47a356STrond Myklebust /* 7741da177e4SLinus Torvalds * Update and possibly write a cached page of an NFS file. 7751da177e4SLinus Torvalds * 7761da177e4SLinus Torvalds * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad 7771da177e4SLinus Torvalds * things with a page scheduled for an RPC call (e.g. invalidate it). 7781da177e4SLinus Torvalds */ 7791da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page, 7801da177e4SLinus Torvalds unsigned int offset, unsigned int count) 7811da177e4SLinus Torvalds { 782cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 7831da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 7841da177e4SLinus Torvalds int status = 0; 7851da177e4SLinus Torvalds 78691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 78791d5b470SChuck Lever 78848186c7dSChuck Lever dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n", 78901cce933SJosef "Jeff" Sipek file->f_path.dentry->d_parent->d_name.name, 79001cce933SJosef "Jeff" Sipek file->f_path.dentry->d_name.name, count, 7910bbacc40SChuck Lever (long long)(page_offset(page) + offset)); 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds /* If we're not using byte range locks, and we know the page 7945d47a356STrond Myklebust * is up to date, it may be more efficient to extend the write 7955d47a356STrond Myklebust * to cover the entire page in order to avoid fragmentation 7965d47a356STrond Myklebust * inefficiencies. 7971da177e4SLinus Torvalds */ 7985d47a356STrond Myklebust if (nfs_write_pageuptodate(page, inode) && 7995d47a356STrond Myklebust inode->i_flock == NULL && 8006b2f3d1fSChristoph Hellwig !(file->f_flags & O_DSYNC)) { 80149a70f27STrond Myklebust count = max(count + offset, nfs_page_length(page)); 8021da177e4SLinus Torvalds offset = 0; 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds 805e21195a7STrond Myklebust status = nfs_writepage_setup(ctx, page, offset, count); 80603fa9e84STrond Myklebust if (status < 0) 80703fa9e84STrond Myklebust nfs_set_pageerror(page); 80859b7c05fSTrond Myklebust else 80959b7c05fSTrond Myklebust __set_page_dirty_nobuffers(page); 8101da177e4SLinus Torvalds 81148186c7dSChuck Lever dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", 8121da177e4SLinus Torvalds status, (long long)i_size_read(inode)); 8131da177e4SLinus Torvalds return status; 8141da177e4SLinus Torvalds } 8151da177e4SLinus Torvalds 816a861a1e1SFred Isaman static void nfs_writepage_release(struct nfs_page *req, 817a861a1e1SFred Isaman struct nfs_write_data *data) 8181da177e4SLinus Torvalds { 819a6305ddbSTrond Myklebust struct page *page = req->wb_page; 8208e821cadSTrond Myklebust 821a861a1e1SFred Isaman if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req, data)) 8221da177e4SLinus Torvalds nfs_inode_remove_request(req); 8239994b62bSFred Isaman nfs_unlock_request(req); 824a6305ddbSTrond Myklebust nfs_end_page_writeback(page); 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds 8273ff7576dSTrond Myklebust static int flush_task_priority(int how) 8281da177e4SLinus Torvalds { 8291da177e4SLinus Torvalds switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { 8301da177e4SLinus Torvalds case FLUSH_HIGHPRI: 8311da177e4SLinus Torvalds return RPC_PRIORITY_HIGH; 8321da177e4SLinus Torvalds case FLUSH_LOWPRI: 8331da177e4SLinus Torvalds return RPC_PRIORITY_LOW; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds return RPC_PRIORITY_NORMAL; 8361da177e4SLinus Torvalds } 8371da177e4SLinus Torvalds 838c5996c4eSFred Isaman int nfs_initiate_write(struct rpc_clnt *clnt, 839c5996c4eSFred Isaman struct nfs_write_data *data, 840788e7a89STrond Myklebust const struct rpc_call_ops *call_ops, 8411da177e4SLinus Torvalds int how) 8421da177e4SLinus Torvalds { 843cd841605SFred Isaman struct inode *inode = data->header->inode; 8443ff7576dSTrond Myklebust int priority = flush_task_priority(how); 84507737691STrond Myklebust struct rpc_task *task; 846bdc7f021STrond Myklebust struct rpc_message msg = { 847bdc7f021STrond Myklebust .rpc_argp = &data->args, 848bdc7f021STrond Myklebust .rpc_resp = &data->res, 849cd841605SFred Isaman .rpc_cred = data->header->cred, 850bdc7f021STrond Myklebust }; 85184115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 852d138d5d1SAndy Adamson .rpc_client = clnt, 85307737691STrond Myklebust .task = &data->task, 854bdc7f021STrond Myklebust .rpc_message = &msg, 85584115e1cSTrond Myklebust .callback_ops = call_ops, 85684115e1cSTrond Myklebust .callback_data = data, 857101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 8582c61be0aSTrond Myklebust .flags = RPC_TASK_ASYNC, 8593ff7576dSTrond Myklebust .priority = priority, 86084115e1cSTrond Myklebust }; 8612c61be0aSTrond Myklebust int ret = 0; 8621da177e4SLinus Torvalds 863d138d5d1SAndy Adamson /* Set up the initial task struct. */ 864d138d5d1SAndy Adamson NFS_PROTO(inode)->write_setup(data, &msg); 865d138d5d1SAndy Adamson 866d138d5d1SAndy Adamson dprintk("NFS: %5u initiated write call " 867d138d5d1SAndy Adamson "(req %s/%lld, %u bytes @ offset %llu)\n", 868d138d5d1SAndy Adamson data->task.tk_pid, 869d138d5d1SAndy Adamson inode->i_sb->s_id, 870d138d5d1SAndy Adamson (long long)NFS_FILEID(inode), 871d138d5d1SAndy Adamson data->args.count, 872d138d5d1SAndy Adamson (unsigned long long)data->args.offset); 873d138d5d1SAndy Adamson 874d138d5d1SAndy Adamson task = rpc_run_task(&task_setup_data); 875d138d5d1SAndy Adamson if (IS_ERR(task)) { 876d138d5d1SAndy Adamson ret = PTR_ERR(task); 877d138d5d1SAndy Adamson goto out; 878d138d5d1SAndy Adamson } 879d138d5d1SAndy Adamson if (how & FLUSH_SYNC) { 880d138d5d1SAndy Adamson ret = rpc_wait_for_completion_task(task); 881d138d5d1SAndy Adamson if (ret == 0) 882d138d5d1SAndy Adamson ret = task->tk_status; 883d138d5d1SAndy Adamson } 884d138d5d1SAndy Adamson rpc_put_task(task); 885d138d5d1SAndy Adamson out: 886d138d5d1SAndy Adamson return ret; 887d138d5d1SAndy Adamson } 888a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write); 889d138d5d1SAndy Adamson 890d138d5d1SAndy Adamson /* 891d138d5d1SAndy Adamson * Set up the argument/result storage required for the RPC call. 892d138d5d1SAndy Adamson */ 8936e4efd56STrond Myklebust static void nfs_write_rpcsetup(struct nfs_page *req, 894d138d5d1SAndy Adamson struct nfs_write_data *data, 895d138d5d1SAndy Adamson unsigned int count, unsigned int offset, 896d138d5d1SAndy Adamson int how) 897d138d5d1SAndy Adamson { 898cd841605SFred Isaman struct nfs_pgio_header *hdr = data->header; 8993d4ff43dSAl Viro struct inode *inode = req->wb_context->dentry->d_inode; 900d138d5d1SAndy Adamson 9011da177e4SLinus Torvalds /* Set up the RPC argument and reply structs 9021da177e4SLinus Torvalds * NB: take care not to mess about with data->commit et al. */ 9031da177e4SLinus Torvalds 904cd841605SFred Isaman hdr->req = req; 905cd841605SFred Isaman hdr->inode = inode = req->wb_context->dentry->d_inode; 906cd841605SFred Isaman hdr->cred = req->wb_context->cred; 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 9091da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 9102bea038cSBoaz Harrosh /* pnfs_set_layoutcommit needs this */ 9112bea038cSBoaz Harrosh data->mds_offset = data->args.offset; 9121da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 913*30dd374fSFred Isaman data->args.pages = data->pages.pagevec; 9141da177e4SLinus Torvalds data->args.count = count; 915383ba719STrond Myklebust data->args.context = get_nfs_open_context(req->wb_context); 916f11ac8dbSTrond Myklebust data->args.lock_context = req->wb_lock_context; 917bdc7f021STrond Myklebust data->args.stable = NFS_UNSTABLE; 91887ed5eb4STrond Myklebust switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) { 91987ed5eb4STrond Myklebust case 0: 92087ed5eb4STrond Myklebust break; 92187ed5eb4STrond Myklebust case FLUSH_COND_STABLE: 92287ed5eb4STrond Myklebust if (nfs_need_commit(NFS_I(inode))) 92387ed5eb4STrond Myklebust break; 92487ed5eb4STrond Myklebust default: 925bdc7f021STrond Myklebust data->args.stable = NFS_FILE_SYNC; 926bdc7f021STrond Myklebust } 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds data->res.fattr = &data->fattr; 9291da177e4SLinus Torvalds data->res.count = count; 9301da177e4SLinus Torvalds data->res.verf = &data->verf; 9310e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 9326e4efd56STrond Myklebust } 9331da177e4SLinus Torvalds 9346e4efd56STrond Myklebust static int nfs_do_write(struct nfs_write_data *data, 9356e4efd56STrond Myklebust const struct rpc_call_ops *call_ops, 9366e4efd56STrond Myklebust int how) 9376e4efd56STrond Myklebust { 938cd841605SFred Isaman struct inode *inode = data->header->inode; 9390382b744SAndy Adamson 940c5996c4eSFred Isaman return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how); 9411da177e4SLinus Torvalds } 9421da177e4SLinus Torvalds 943275acaafSTrond Myklebust static int nfs_do_multiple_writes(struct list_head *head, 944275acaafSTrond Myklebust const struct rpc_call_ops *call_ops, 945275acaafSTrond Myklebust int how) 946275acaafSTrond Myklebust { 947275acaafSTrond Myklebust struct nfs_write_data *data; 948275acaafSTrond Myklebust int ret = 0; 949275acaafSTrond Myklebust 950275acaafSTrond Myklebust while (!list_empty(head)) { 951275acaafSTrond Myklebust int ret2; 952275acaafSTrond Myklebust 953275acaafSTrond Myklebust data = list_entry(head->next, struct nfs_write_data, list); 954275acaafSTrond Myklebust list_del_init(&data->list); 955275acaafSTrond Myklebust 956dce81290STrond Myklebust ret2 = nfs_do_write(data, call_ops, how); 957275acaafSTrond Myklebust if (ret == 0) 958275acaafSTrond Myklebust ret = ret2; 959275acaafSTrond Myklebust } 960275acaafSTrond Myklebust return ret; 961275acaafSTrond Myklebust } 962275acaafSTrond Myklebust 9636d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and 9646d884e8fSFred * call this on each, which will prepare them to be retried on next 9656d884e8fSFred * writeback using standard nfs. 9666d884e8fSFred */ 9676d884e8fSFred static void nfs_redirty_request(struct nfs_page *req) 9686d884e8fSFred { 969a6305ddbSTrond Myklebust struct page *page = req->wb_page; 970a6305ddbSTrond Myklebust 9716d884e8fSFred nfs_mark_request_dirty(req); 9729994b62bSFred Isaman nfs_unlock_request(req); 973a6305ddbSTrond Myklebust nfs_end_page_writeback(page); 9746d884e8fSFred } 9756d884e8fSFred 9761da177e4SLinus Torvalds /* 9771da177e4SLinus Torvalds * Generate multiple small requests to write out a single 9781da177e4SLinus Torvalds * contiguous dirty area on one page. 9791da177e4SLinus Torvalds */ 980275acaafSTrond Myklebust static int nfs_flush_multi(struct nfs_pageio_descriptor *desc, struct list_head *res) 9811da177e4SLinus Torvalds { 982c76069bdSFred Isaman struct nfs_page *req = nfs_list_entry(desc->pg_list.next); 9831da177e4SLinus Torvalds struct page *page = req->wb_page; 984cd841605SFred Isaman struct nfs_write_header *whdr; 9851da177e4SLinus Torvalds struct nfs_write_data *data; 986d097971dSTrond Myklebust size_t wsize = desc->pg_bsize, nbytes; 987e9f7bee1STrond Myklebust unsigned int offset; 9881da177e4SLinus Torvalds int requests = 0; 989dbae4c73STrond Myklebust int ret = 0; 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds nfs_list_remove_request(req); 9921da177e4SLinus Torvalds 993b31268acSTrond Myklebust if ((desc->pg_ioflags & FLUSH_COND_STABLE) && 994b31268acSTrond Myklebust (desc->pg_moreio || NFS_I(desc->pg_inode)->ncommit || 995b31268acSTrond Myklebust desc->pg_count > wsize)) 996b31268acSTrond Myklebust desc->pg_ioflags &= ~FLUSH_COND_STABLE; 997b31268acSTrond Myklebust 998b31268acSTrond Myklebust 999275acaafSTrond Myklebust offset = 0; 1000c76069bdSFred Isaman nbytes = desc->pg_count; 1001e9f7bee1STrond Myklebust do { 1002e9f7bee1STrond Myklebust size_t len = min(nbytes, wsize); 1003e9f7bee1STrond Myklebust 1004cd841605SFred Isaman whdr = nfs_writehdr_alloc(1); 1005cd841605SFred Isaman if (!whdr) 10061da177e4SLinus Torvalds goto out_bad; 1007cd841605SFred Isaman data = &whdr->rpc_data; 1008*30dd374fSFred Isaman data->pages.pagevec[0] = page; 1009f13c3620STrond Myklebust nfs_write_rpcsetup(req, data, len, offset, desc->pg_ioflags); 1010275acaafSTrond Myklebust list_add(&data->list, res); 10111da177e4SLinus Torvalds requests++; 1012e9f7bee1STrond Myklebust nbytes -= len; 1013275acaafSTrond Myklebust offset += len; 1014e9f7bee1STrond Myklebust } while (nbytes != 0); 10151da177e4SLinus Torvalds atomic_set(&req->wb_complete, requests); 101650828d7eSTrond Myklebust desc->pg_rpc_callops = &nfs_write_partial_ops; 1017dbae4c73STrond Myklebust return ret; 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds out_bad: 1020275acaafSTrond Myklebust while (!list_empty(res)) { 1021275acaafSTrond Myklebust data = list_entry(res->next, struct nfs_write_data, list); 10226e4efd56STrond Myklebust list_del(&data->list); 10238ccd271fSFred Isaman nfs_writedata_release(data); 10241da177e4SLinus Torvalds } 102561822ab5STrond Myklebust nfs_redirty_request(req); 10261da177e4SLinus Torvalds return -ENOMEM; 10271da177e4SLinus Torvalds } 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds /* 10301da177e4SLinus Torvalds * Create an RPC task for the given write request and kick it. 10311da177e4SLinus Torvalds * The page must have been locked by the caller. 10321da177e4SLinus Torvalds * 10331da177e4SLinus Torvalds * It may happen that the page we're passed is not marked dirty. 10341da177e4SLinus Torvalds * This is the case if nfs_updatepage detects a conflicting request 10351da177e4SLinus Torvalds * that has been written but not committed. 10361da177e4SLinus Torvalds */ 1037275acaafSTrond Myklebust static int nfs_flush_one(struct nfs_pageio_descriptor *desc, struct list_head *res) 10381da177e4SLinus Torvalds { 10391da177e4SLinus Torvalds struct nfs_page *req; 10401da177e4SLinus Torvalds struct page **pages; 1041cd841605SFred Isaman struct nfs_write_header *whdr; 10421da177e4SLinus Torvalds struct nfs_write_data *data; 1043c76069bdSFred Isaman struct list_head *head = &desc->pg_list; 10443b609184SPeng Tao int ret = 0; 10451da177e4SLinus Torvalds 1046cd841605SFred Isaman whdr = nfs_writehdr_alloc(nfs_page_array_len(desc->pg_base, 1047c76069bdSFred Isaman desc->pg_count)); 1048cd841605SFred Isaman if (!whdr) { 104944b83799SFred Isaman while (!list_empty(head)) { 105044b83799SFred Isaman req = nfs_list_entry(head->next); 105144b83799SFred Isaman nfs_list_remove_request(req); 105244b83799SFred Isaman nfs_redirty_request(req); 105344b83799SFred Isaman } 105444b83799SFred Isaman ret = -ENOMEM; 105544b83799SFred Isaman goto out; 105644b83799SFred Isaman } 1057cd841605SFred Isaman data = &whdr->rpc_data; 1058*30dd374fSFred Isaman pages = data->pages.pagevec; 10591da177e4SLinus Torvalds while (!list_empty(head)) { 10601da177e4SLinus Torvalds req = nfs_list_entry(head->next); 10611da177e4SLinus Torvalds nfs_list_remove_request(req); 1062cd841605SFred Isaman nfs_list_add_request(req, &whdr->header.pages); 10631da177e4SLinus Torvalds *pages++ = req->wb_page; 10641da177e4SLinus Torvalds } 1065cd841605SFred Isaman req = nfs_list_entry(whdr->header.pages.next); 10661da177e4SLinus Torvalds 1067b31268acSTrond Myklebust if ((desc->pg_ioflags & FLUSH_COND_STABLE) && 1068b31268acSTrond Myklebust (desc->pg_moreio || NFS_I(desc->pg_inode)->ncommit)) 1069b31268acSTrond Myklebust desc->pg_ioflags &= ~FLUSH_COND_STABLE; 1070b31268acSTrond Myklebust 10711da177e4SLinus Torvalds /* Set up the argument struct */ 10726e4efd56STrond Myklebust nfs_write_rpcsetup(req, data, desc->pg_count, 0, desc->pg_ioflags); 1073275acaafSTrond Myklebust list_add(&data->list, res); 107450828d7eSTrond Myklebust desc->pg_rpc_callops = &nfs_write_full_ops; 107544b83799SFred Isaman out: 107644b83799SFred Isaman return ret; 10771da177e4SLinus Torvalds } 10781da177e4SLinus Torvalds 1079dce81290STrond Myklebust int nfs_generic_flush(struct nfs_pageio_descriptor *desc, struct list_head *head) 1080dce81290STrond Myklebust { 1081dce81290STrond Myklebust if (desc->pg_bsize < PAGE_CACHE_SIZE) 1082dce81290STrond Myklebust return nfs_flush_multi(desc, head); 1083dce81290STrond Myklebust return nfs_flush_one(desc, head); 1084dce81290STrond Myklebust } 1085dce81290STrond Myklebust 1086dce81290STrond Myklebust static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc) 10871751c363STrond Myklebust { 1088275acaafSTrond Myklebust LIST_HEAD(head); 1089275acaafSTrond Myklebust int ret; 1090275acaafSTrond Myklebust 1091dce81290STrond Myklebust ret = nfs_generic_flush(desc, &head); 1092275acaafSTrond Myklebust if (ret == 0) 109350828d7eSTrond Myklebust ret = nfs_do_multiple_writes(&head, desc->pg_rpc_callops, 1094dce81290STrond Myklebust desc->pg_ioflags); 1095275acaafSTrond Myklebust return ret; 10961751c363STrond Myklebust } 10971751c363STrond Myklebust 10981751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_write_ops = { 10991751c363STrond Myklebust .pg_test = nfs_generic_pg_test, 11001751c363STrond Myklebust .pg_doio = nfs_generic_pg_writepages, 11011751c363STrond Myklebust }; 11021751c363STrond Myklebust 1103e2fecb21STrond Myklebust void nfs_pageio_init_write_mds(struct nfs_pageio_descriptor *pgio, 11041751c363STrond Myklebust struct inode *inode, int ioflags) 11051751c363STrond Myklebust { 11061751c363STrond Myklebust nfs_pageio_init(pgio, inode, &nfs_pageio_write_ops, 11071751c363STrond Myklebust NFS_SERVER(inode)->wsize, ioflags); 11081751c363STrond Myklebust } 11091751c363STrond Myklebust 1110dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio) 1111dce81290STrond Myklebust { 1112dce81290STrond Myklebust pgio->pg_ops = &nfs_pageio_write_ops; 1113dce81290STrond Myklebust pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize; 1114dce81290STrond Myklebust } 11151f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds); 1116dce81290STrond Myklebust 1117c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, 1118c63c7b05STrond Myklebust struct inode *inode, int ioflags) 11191da177e4SLinus Torvalds { 11201751c363STrond Myklebust if (!pnfs_pageio_init_write(pgio, inode, ioflags)) 11211751c363STrond Myklebust nfs_pageio_init_write_mds(pgio, inode, ioflags); 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds /* 11251da177e4SLinus Torvalds * Handle a write reply that flushed part of a page. 11261da177e4SLinus Torvalds */ 1127788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata) 11281da177e4SLinus Torvalds { 1129788e7a89STrond Myklebust struct nfs_write_data *data = calldata; 11301da177e4SLinus Torvalds 113148186c7dSChuck Lever dprintk("NFS: %5u write(%s/%lld %d@%lld)", 113248186c7dSChuck Lever task->tk_pid, 1133cd841605SFred Isaman data->header->inode->i_sb->s_id, 113448186c7dSChuck Lever (long long) 1135cd841605SFred Isaman NFS_FILEID(data->header->inode), 1136cd841605SFred Isaman data->header->req->wb_bytes, 1137cd841605SFred Isaman (long long)req_offset(data->header->req)); 11381da177e4SLinus Torvalds 1139c9d8f89dSTrond Myklebust nfs_writeback_done(task, data); 1140c9d8f89dSTrond Myklebust } 1141788e7a89STrond Myklebust 1142c9d8f89dSTrond Myklebust static void nfs_writeback_release_partial(void *calldata) 1143c9d8f89dSTrond Myklebust { 1144c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1145cd841605SFred Isaman struct nfs_page *req = data->header->req; 1146c9d8f89dSTrond Myklebust struct page *page = req->wb_page; 1147c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1148c9d8f89dSTrond Myklebust 1149c9d8f89dSTrond Myklebust if (status < 0) { 1150a301b777STrond Myklebust nfs_set_pageerror(page); 1151c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 1152c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 11538e821cadSTrond Myklebust goto out; 11548e821cadSTrond Myklebust } 11558e821cadSTrond Myklebust 11568e821cadSTrond Myklebust if (nfs_write_need_commit(data)) { 1157587142f8STrond Myklebust struct inode *inode = page->mapping->host; 11588e821cadSTrond Myklebust 1159587142f8STrond Myklebust spin_lock(&inode->i_lock); 11608e821cadSTrond Myklebust if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) { 11618e821cadSTrond Myklebust /* Do nothing we need to resend the writes */ 11628e821cadSTrond Myklebust } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) { 11631da177e4SLinus Torvalds memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); 11641da177e4SLinus Torvalds dprintk(" defer commit\n"); 11651da177e4SLinus Torvalds } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) { 11668e821cadSTrond Myklebust set_bit(PG_NEED_RESCHED, &req->wb_flags); 11678e821cadSTrond Myklebust clear_bit(PG_NEED_COMMIT, &req->wb_flags); 11681da177e4SLinus Torvalds dprintk(" server reboot detected\n"); 11691da177e4SLinus Torvalds } 1170587142f8STrond Myklebust spin_unlock(&inode->i_lock); 11711da177e4SLinus Torvalds } else 11721da177e4SLinus Torvalds dprintk(" OK\n"); 11731da177e4SLinus Torvalds 11748e821cadSTrond Myklebust out: 11751da177e4SLinus Torvalds if (atomic_dec_and_test(&req->wb_complete)) 1176a861a1e1SFred Isaman nfs_writepage_release(req, data); 1177cd841605SFred Isaman nfs_writedata_release(data); 11781da177e4SLinus Torvalds } 11791da177e4SLinus Torvalds 1180def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata) 1181def6ed7eSAndy Adamson { 1182def6ed7eSAndy Adamson struct nfs_write_data *data = calldata; 1183cd841605SFred Isaman NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data); 1184def6ed7eSAndy Adamson } 1185def6ed7eSAndy Adamson 11860b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata) 11870b7c0153SFred Isaman { 11880b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 11890b7c0153SFred Isaman 11900b7c0153SFred Isaman NFS_PROTO(data->inode)->commit_rpc_prepare(task, data); 11910b7c0153SFred Isaman } 11920b7c0153SFred Isaman 1193788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = { 1194def6ed7eSAndy Adamson .rpc_call_prepare = nfs_write_prepare, 1195788e7a89STrond Myklebust .rpc_call_done = nfs_writeback_done_partial, 1196c9d8f89dSTrond Myklebust .rpc_release = nfs_writeback_release_partial, 1197788e7a89STrond Myklebust }; 1198788e7a89STrond Myklebust 11991da177e4SLinus Torvalds /* 12001da177e4SLinus Torvalds * Handle a write reply that flushes a whole page. 12011da177e4SLinus Torvalds * 12021da177e4SLinus Torvalds * FIXME: There is an inherent race with invalidate_inode_pages and 12031da177e4SLinus Torvalds * writebacks since the page->count is kept > 1 for as long 12041da177e4SLinus Torvalds * as the page has a write request pending. 12051da177e4SLinus Torvalds */ 1206788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata) 12071da177e4SLinus Torvalds { 1208788e7a89STrond Myklebust struct nfs_write_data *data = calldata; 12091da177e4SLinus Torvalds 1210c9d8f89dSTrond Myklebust nfs_writeback_done(task, data); 1211c9d8f89dSTrond Myklebust } 1212c9d8f89dSTrond Myklebust 1213c9d8f89dSTrond Myklebust static void nfs_writeback_release_full(void *calldata) 1214c9d8f89dSTrond Myklebust { 1215c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1216cd841605SFred Isaman struct nfs_pgio_header *hdr = data->header; 1217e2fecb21STrond Myklebust int status = data->task.tk_status; 1218788e7a89STrond Myklebust 12191da177e4SLinus Torvalds /* Update attributes as result of writeback. */ 1220cd841605SFred Isaman while (!list_empty(&hdr->pages)) { 1221cd841605SFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 1222c9d8f89dSTrond Myklebust struct page *page = req->wb_page; 1223c9d8f89dSTrond Myklebust 12241da177e4SLinus Torvalds nfs_list_remove_request(req); 12251da177e4SLinus Torvalds 122648186c7dSChuck Lever dprintk("NFS: %5u write (%s/%lld %d@%lld)", 122748186c7dSChuck Lever data->task.tk_pid, 12283d4ff43dSAl Viro req->wb_context->dentry->d_inode->i_sb->s_id, 12293d4ff43dSAl Viro (long long)NFS_FILEID(req->wb_context->dentry->d_inode), 12301da177e4SLinus Torvalds req->wb_bytes, 12311da177e4SLinus Torvalds (long long)req_offset(req)); 12321da177e4SLinus Torvalds 1233c9d8f89dSTrond Myklebust if (status < 0) { 1234a301b777STrond Myklebust nfs_set_pageerror(page); 1235c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 1236c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 12378e821cadSTrond Myklebust goto remove_request; 12381da177e4SLinus Torvalds } 12391da177e4SLinus Torvalds 12408e821cadSTrond Myklebust if (nfs_write_need_commit(data)) { 12411da177e4SLinus Torvalds memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); 1242cd841605SFred Isaman nfs_mark_request_commit(req, hdr->lseg); 12431da177e4SLinus Torvalds dprintk(" marked for commit\n"); 12448e821cadSTrond Myklebust goto next; 12458e821cadSTrond Myklebust } 12468e821cadSTrond Myklebust dprintk(" OK\n"); 12478e821cadSTrond Myklebust remove_request: 12481da177e4SLinus Torvalds nfs_inode_remove_request(req); 12491da177e4SLinus Torvalds next: 12509994b62bSFred Isaman nfs_unlock_request(req); 1251a6305ddbSTrond Myklebust nfs_end_page_writeback(page); 12521da177e4SLinus Torvalds } 1253cd841605SFred Isaman nfs_writedata_release(data); 12541da177e4SLinus Torvalds } 12551da177e4SLinus Torvalds 1256788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = { 1257def6ed7eSAndy Adamson .rpc_call_prepare = nfs_write_prepare, 1258788e7a89STrond Myklebust .rpc_call_done = nfs_writeback_done_full, 1259c9d8f89dSTrond Myklebust .rpc_release = nfs_writeback_release_full, 1260788e7a89STrond Myklebust }; 1261788e7a89STrond Myklebust 1262788e7a89STrond Myklebust 12631da177e4SLinus Torvalds /* 12641da177e4SLinus Torvalds * This function is called when the WRITE call is complete. 12651da177e4SLinus Torvalds */ 126613602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data) 12671da177e4SLinus Torvalds { 12681da177e4SLinus Torvalds struct nfs_writeargs *argp = &data->args; 12691da177e4SLinus Torvalds struct nfs_writeres *resp = &data->res; 1270cd841605SFred Isaman struct inode *inode = data->header->inode; 1271788e7a89STrond Myklebust int status; 12721da177e4SLinus Torvalds 1273a3f565b1SChuck Lever dprintk("NFS: %5u nfs_writeback_done (status %d)\n", 12741da177e4SLinus Torvalds task->tk_pid, task->tk_status); 12751da177e4SLinus Torvalds 1276f551e44fSChuck Lever /* 1277f551e44fSChuck Lever * ->write_done will attempt to use post-op attributes to detect 1278f551e44fSChuck Lever * conflicting writes by other clients. A strict interpretation 1279f551e44fSChuck Lever * of close-to-open would allow us to continue caching even if 1280f551e44fSChuck Lever * another writer had changed the file, but some applications 1281f551e44fSChuck Lever * depend on tighter cache coherency when writing. 1282f551e44fSChuck Lever */ 1283cd841605SFred Isaman status = NFS_PROTO(inode)->write_done(task, data); 1284788e7a89STrond Myklebust if (status != 0) 128513602896SFred Isaman return; 1286cd841605SFred Isaman nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count); 128791d5b470SChuck Lever 12881da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 12891da177e4SLinus Torvalds if (resp->verf->committed < argp->stable && task->tk_status >= 0) { 12901da177e4SLinus Torvalds /* We tried a write call, but the server did not 12911da177e4SLinus Torvalds * commit data to stable storage even though we 12921da177e4SLinus Torvalds * requested it. 12931da177e4SLinus Torvalds * Note: There is a known bug in Tru64 < 5.0 in which 12941da177e4SLinus Torvalds * the server reports NFS_DATA_SYNC, but performs 12951da177e4SLinus Torvalds * NFS_FILE_SYNC. We therefore implement this checking 12961da177e4SLinus Torvalds * as a dprintk() in order to avoid filling syslog. 12971da177e4SLinus Torvalds */ 12981da177e4SLinus Torvalds static unsigned long complain; 12991da177e4SLinus Torvalds 1300a69aef14SFred Isaman /* Note this will print the MDS for a DS write */ 13011da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 13021da177e4SLinus Torvalds dprintk("NFS: faulty NFS server %s:" 13031da177e4SLinus Torvalds " (committed = %d) != (stable = %d)\n", 1304cd841605SFred Isaman NFS_SERVER(inode)->nfs_client->cl_hostname, 13051da177e4SLinus Torvalds resp->verf->committed, argp->stable); 13061da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 13071da177e4SLinus Torvalds } 13081da177e4SLinus Torvalds } 13091da177e4SLinus Torvalds #endif 13101da177e4SLinus Torvalds /* Is this a short write? */ 13111da177e4SLinus Torvalds if (task->tk_status >= 0 && resp->count < argp->count) { 13121da177e4SLinus Torvalds static unsigned long complain; 13131da177e4SLinus Torvalds 1314cd841605SFred Isaman nfs_inc_stats(inode, NFSIOS_SHORTWRITE); 131591d5b470SChuck Lever 13161da177e4SLinus Torvalds /* Has the server at least made some progress? */ 13171da177e4SLinus Torvalds if (resp->count != 0) { 13181da177e4SLinus Torvalds /* Was this an NFSv2 write or an NFSv3 stable write? */ 13191da177e4SLinus Torvalds if (resp->verf->committed != NFS_UNSTABLE) { 13201da177e4SLinus Torvalds /* Resend from where the server left off */ 1321a69aef14SFred Isaman data->mds_offset += resp->count; 13221da177e4SLinus Torvalds argp->offset += resp->count; 13231da177e4SLinus Torvalds argp->pgbase += resp->count; 13241da177e4SLinus Torvalds argp->count -= resp->count; 13251da177e4SLinus Torvalds } else { 13261da177e4SLinus Torvalds /* Resend as a stable write in order to avoid 13271da177e4SLinus Torvalds * headaches in the case of a server crash. 13281da177e4SLinus Torvalds */ 13291da177e4SLinus Torvalds argp->stable = NFS_FILE_SYNC; 13301da177e4SLinus Torvalds } 1331d00c5d43STrond Myklebust rpc_restart_call_prepare(task); 133213602896SFred Isaman return; 13331da177e4SLinus Torvalds } 13341da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 13351da177e4SLinus Torvalds printk(KERN_WARNING 13361da177e4SLinus Torvalds "NFS: Server wrote zero bytes, expected %u.\n", 13371da177e4SLinus Torvalds argp->count); 13381da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 13391da177e4SLinus Torvalds } 13401da177e4SLinus Torvalds /* Can't do anything about it except throw an error. */ 13411da177e4SLinus Torvalds task->tk_status = -EIO; 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 134771d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait) 134871d0a611STrond Myklebust { 1349b8413f98STrond Myklebust int ret; 1350b8413f98STrond Myklebust 135171d0a611STrond Myklebust if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags)) 135271d0a611STrond Myklebust return 1; 1353b8413f98STrond Myklebust if (!may_wait) 135471d0a611STrond Myklebust return 0; 1355b8413f98STrond Myklebust ret = out_of_line_wait_on_bit_lock(&nfsi->flags, 1356b8413f98STrond Myklebust NFS_INO_COMMIT, 1357b8413f98STrond Myklebust nfs_wait_bit_killable, 1358b8413f98STrond Myklebust TASK_KILLABLE); 1359b8413f98STrond Myklebust return (ret < 0) ? ret : 1; 136071d0a611STrond Myklebust } 136171d0a611STrond Myklebust 1362e0c2b380SFred Isaman void nfs_commit_clear_lock(struct nfs_inode *nfsi) 136371d0a611STrond Myklebust { 136471d0a611STrond Myklebust clear_bit(NFS_INO_COMMIT, &nfsi->flags); 136571d0a611STrond Myklebust smp_mb__after_clear_bit(); 136671d0a611STrond Myklebust wake_up_bit(&nfsi->flags, NFS_INO_COMMIT); 136771d0a611STrond Myklebust } 1368e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_clear_lock); 136971d0a611STrond Myklebust 13700b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data) 13711da177e4SLinus Torvalds { 13720b7c0153SFred Isaman put_nfs_open_context(data->context); 13730b7c0153SFred Isaman nfs_commit_free(data); 13741da177e4SLinus Torvalds } 1375e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release); 13761da177e4SLinus Torvalds 13770b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data, 13789ace33cdSFred Isaman const struct rpc_call_ops *call_ops, 1379788e7a89STrond Myklebust int how) 13801da177e4SLinus Torvalds { 138107737691STrond Myklebust struct rpc_task *task; 13829ace33cdSFred Isaman int priority = flush_task_priority(how); 1383bdc7f021STrond Myklebust struct rpc_message msg = { 1384bdc7f021STrond Myklebust .rpc_argp = &data->args, 1385bdc7f021STrond Myklebust .rpc_resp = &data->res, 13869ace33cdSFred Isaman .rpc_cred = data->cred, 1387bdc7f021STrond Myklebust }; 138884115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 138907737691STrond Myklebust .task = &data->task, 13909ace33cdSFred Isaman .rpc_client = clnt, 1391bdc7f021STrond Myklebust .rpc_message = &msg, 13929ace33cdSFred Isaman .callback_ops = call_ops, 139384115e1cSTrond Myklebust .callback_data = data, 1394101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 13952c61be0aSTrond Myklebust .flags = RPC_TASK_ASYNC, 13963ff7576dSTrond Myklebust .priority = priority, 139784115e1cSTrond Myklebust }; 1398788e7a89STrond Myklebust /* Set up the initial task struct. */ 13999ace33cdSFred Isaman NFS_PROTO(data->inode)->commit_setup(data, &msg); 14001da177e4SLinus Torvalds 1401a3f565b1SChuck Lever dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); 1402bdc7f021STrond Myklebust 140307737691STrond Myklebust task = rpc_run_task(&task_setup_data); 1404dbae4c73STrond Myklebust if (IS_ERR(task)) 1405dbae4c73STrond Myklebust return PTR_ERR(task); 1406d2224e7aSJeff Layton if (how & FLUSH_SYNC) 1407d2224e7aSJeff Layton rpc_wait_for_completion_task(task); 140807737691STrond Myklebust rpc_put_task(task); 1409dbae4c73STrond Myklebust return 0; 14101da177e4SLinus Torvalds } 1411e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit); 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds /* 14149ace33cdSFred Isaman * Set up the argument/result storage required for the RPC call. 14159ace33cdSFred Isaman */ 14160b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data, 1417988b6dceSFred Isaman struct list_head *head, 1418988b6dceSFred Isaman struct pnfs_layout_segment *lseg) 14199ace33cdSFred Isaman { 14209ace33cdSFred Isaman struct nfs_page *first = nfs_list_entry(head->next); 14213d4ff43dSAl Viro struct inode *inode = first->wb_context->dentry->d_inode; 14229ace33cdSFred Isaman 14239ace33cdSFred Isaman /* Set up the RPC argument and reply structs 14249ace33cdSFred Isaman * NB: take care not to mess about with data->commit et al. */ 14259ace33cdSFred Isaman 14269ace33cdSFred Isaman list_splice_init(head, &data->pages); 14279ace33cdSFred Isaman 14289ace33cdSFred Isaman data->inode = inode; 14299ace33cdSFred Isaman data->cred = first->wb_context->cred; 1430988b6dceSFred Isaman data->lseg = lseg; /* reference transferred */ 14319ace33cdSFred Isaman data->mds_ops = &nfs_commit_ops; 14329ace33cdSFred Isaman 14339ace33cdSFred Isaman data->args.fh = NFS_FH(data->inode); 14349ace33cdSFred Isaman /* Note: we always request a commit of the entire inode */ 14359ace33cdSFred Isaman data->args.offset = 0; 14369ace33cdSFred Isaman data->args.count = 0; 14370b7c0153SFred Isaman data->context = get_nfs_open_context(first->wb_context); 14389ace33cdSFred Isaman data->res.fattr = &data->fattr; 14399ace33cdSFred Isaman data->res.verf = &data->verf; 14409ace33cdSFred Isaman nfs_fattr_init(&data->fattr); 14419ace33cdSFred Isaman } 1442e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit); 14439ace33cdSFred Isaman 1444e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list, 1445a861a1e1SFred Isaman struct pnfs_layout_segment *lseg) 144664bfeb49SFred Isaman { 144764bfeb49SFred Isaman struct nfs_page *req; 144864bfeb49SFred Isaman 144964bfeb49SFred Isaman while (!list_empty(page_list)) { 145064bfeb49SFred Isaman req = nfs_list_entry(page_list->next); 145164bfeb49SFred Isaman nfs_list_remove_request(req); 1452a861a1e1SFred Isaman nfs_mark_request_commit(req, lseg); 145364bfeb49SFred Isaman dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 145464bfeb49SFred Isaman dec_bdi_stat(req->wb_page->mapping->backing_dev_info, 145564bfeb49SFred Isaman BDI_RECLAIMABLE); 14569994b62bSFred Isaman nfs_unlock_request(req); 145764bfeb49SFred Isaman } 145864bfeb49SFred Isaman } 1459e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit); 146064bfeb49SFred Isaman 14619ace33cdSFred Isaman /* 14621da177e4SLinus Torvalds * Commit dirty pages 14631da177e4SLinus Torvalds */ 14641da177e4SLinus Torvalds static int 146540859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how) 14661da177e4SLinus Torvalds { 14670b7c0153SFred Isaman struct nfs_commit_data *data; 14681da177e4SLinus Torvalds 1469c9d8f89dSTrond Myklebust data = nfs_commitdata_alloc(); 14701da177e4SLinus Torvalds 14711da177e4SLinus Torvalds if (!data) 14721da177e4SLinus Torvalds goto out_bad; 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds /* Set up the argument struct */ 1475988b6dceSFred Isaman nfs_init_commit(data, head, NULL); 14760b7c0153SFred Isaman return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops, how); 14771da177e4SLinus Torvalds out_bad: 1478a861a1e1SFred Isaman nfs_retry_commit(head, NULL); 147971d0a611STrond Myklebust nfs_commit_clear_lock(NFS_I(inode)); 14801da177e4SLinus Torvalds return -ENOMEM; 14811da177e4SLinus Torvalds } 14821da177e4SLinus Torvalds 14831da177e4SLinus Torvalds /* 14841da177e4SLinus Torvalds * COMMIT call returned 14851da177e4SLinus Torvalds */ 1486788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata) 14871da177e4SLinus Torvalds { 14880b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 14891da177e4SLinus Torvalds 1490a3f565b1SChuck Lever dprintk("NFS: %5u nfs_commit_done (status %d)\n", 14911da177e4SLinus Torvalds task->tk_pid, task->tk_status); 14921da177e4SLinus Torvalds 1493788e7a89STrond Myklebust /* Call the NFS version-specific code */ 1494c0d0e96bSTrond Myklebust NFS_PROTO(data->inode)->commit_done(task, data); 1495c9d8f89dSTrond Myklebust } 1496c9d8f89dSTrond Myklebust 14970b7c0153SFred Isaman void nfs_commit_release_pages(struct nfs_commit_data *data) 1498c9d8f89dSTrond Myklebust { 1499c9d8f89dSTrond Myklebust struct nfs_page *req; 1500c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1501788e7a89STrond Myklebust 15021da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 15031da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 15041da177e4SLinus Torvalds nfs_list_remove_request(req); 1505d6d6dc7cSFred Isaman nfs_clear_page_commit(req->wb_page); 15061da177e4SLinus Torvalds 150748186c7dSChuck Lever dprintk("NFS: commit (%s/%lld %d@%lld)", 15083d4ff43dSAl Viro req->wb_context->dentry->d_sb->s_id, 15093d4ff43dSAl Viro (long long)NFS_FILEID(req->wb_context->dentry->d_inode), 15101da177e4SLinus Torvalds req->wb_bytes, 15111da177e4SLinus Torvalds (long long)req_offset(req)); 1512c9d8f89dSTrond Myklebust if (status < 0) { 1513c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 15141da177e4SLinus Torvalds nfs_inode_remove_request(req); 1515c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 15161da177e4SLinus Torvalds goto next; 15171da177e4SLinus Torvalds } 15181da177e4SLinus Torvalds 15191da177e4SLinus Torvalds /* Okay, COMMIT succeeded, apparently. Check the verifier 15201da177e4SLinus Torvalds * returned by the server against all stored verfs. */ 15211da177e4SLinus Torvalds if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) { 15221da177e4SLinus Torvalds /* We have a match */ 15231da177e4SLinus Torvalds nfs_inode_remove_request(req); 15241da177e4SLinus Torvalds dprintk(" OK\n"); 15251da177e4SLinus Torvalds goto next; 15261da177e4SLinus Torvalds } 15271da177e4SLinus Torvalds /* We have a mismatch. Write the page again */ 15281da177e4SLinus Torvalds dprintk(" mismatch\n"); 15296d884e8fSFred nfs_mark_request_dirty(req); 15301da177e4SLinus Torvalds next: 15319994b62bSFred Isaman nfs_unlock_request(req); 15321da177e4SLinus Torvalds } 15335917ce84SFred Isaman } 1534e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_release_pages); 15355917ce84SFred Isaman 15365917ce84SFred Isaman static void nfs_commit_release(void *calldata) 15375917ce84SFred Isaman { 15380b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 15395917ce84SFred Isaman 15405917ce84SFred Isaman nfs_commit_release_pages(data); 154171d0a611STrond Myklebust nfs_commit_clear_lock(NFS_I(data->inode)); 1542c9d8f89dSTrond Myklebust nfs_commitdata_release(calldata); 15431da177e4SLinus Torvalds } 1544788e7a89STrond Myklebust 1545788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = { 15460b7c0153SFred Isaman .rpc_call_prepare = nfs_commit_prepare, 1547788e7a89STrond Myklebust .rpc_call_done = nfs_commit_done, 1548788e7a89STrond Myklebust .rpc_release = nfs_commit_release, 1549788e7a89STrond Myklebust }; 15501da177e4SLinus Torvalds 1551b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how) 15521da177e4SLinus Torvalds { 15531da177e4SLinus Torvalds LIST_HEAD(head); 155471d0a611STrond Myklebust int may_wait = how & FLUSH_SYNC; 1555b8413f98STrond Myklebust int res; 15561da177e4SLinus Torvalds 1557b8413f98STrond Myklebust res = nfs_commit_set_lock(NFS_I(inode), may_wait); 1558b8413f98STrond Myklebust if (res <= 0) 1559c5efa5fcSTrond Myklebust goto out_mark_dirty; 1560d6d6dc7cSFred Isaman res = nfs_scan_commit(inode, &head); 15611da177e4SLinus Torvalds if (res) { 1562a861a1e1SFred Isaman int error; 1563a861a1e1SFred Isaman 1564a861a1e1SFred Isaman error = pnfs_commit_list(inode, &head, how); 1565a861a1e1SFred Isaman if (error == PNFS_NOT_ATTEMPTED) 1566a861a1e1SFred Isaman error = nfs_commit_list(inode, &head, how); 15671da177e4SLinus Torvalds if (error < 0) 15681da177e4SLinus Torvalds return error; 1569b8413f98STrond Myklebust if (!may_wait) 1570b8413f98STrond Myklebust goto out_mark_dirty; 1571b8413f98STrond Myklebust error = wait_on_bit(&NFS_I(inode)->flags, 1572b8413f98STrond Myklebust NFS_INO_COMMIT, 157371d0a611STrond Myklebust nfs_wait_bit_killable, 157471d0a611STrond Myklebust TASK_KILLABLE); 1575b8413f98STrond Myklebust if (error < 0) 1576b8413f98STrond Myklebust return error; 157771d0a611STrond Myklebust } else 157871d0a611STrond Myklebust nfs_commit_clear_lock(NFS_I(inode)); 1579c5efa5fcSTrond Myklebust return res; 1580c5efa5fcSTrond Myklebust /* Note: If we exit without ensuring that the commit is complete, 1581c5efa5fcSTrond Myklebust * we must mark the inode as dirty. Otherwise, future calls to 1582c5efa5fcSTrond Myklebust * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure 1583c5efa5fcSTrond Myklebust * that the data is on the disk. 1584c5efa5fcSTrond Myklebust */ 1585c5efa5fcSTrond Myklebust out_mark_dirty: 1586c5efa5fcSTrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 15871da177e4SLinus Torvalds return res; 15881da177e4SLinus Torvalds } 15898fc795f7STrond Myklebust 15908fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 15918fc795f7STrond Myklebust { 1592420e3646STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 1593420e3646STrond Myklebust int flags = FLUSH_SYNC; 1594420e3646STrond Myklebust int ret = 0; 15958fc795f7STrond Myklebust 15963236c3e1SJeff Layton /* no commits means nothing needs to be done */ 15973236c3e1SJeff Layton if (!nfsi->ncommit) 15983236c3e1SJeff Layton return ret; 15993236c3e1SJeff Layton 1600a00dd6c0SJeff Layton if (wbc->sync_mode == WB_SYNC_NONE) { 1601a00dd6c0SJeff Layton /* Don't commit yet if this is a non-blocking flush and there 1602a00dd6c0SJeff Layton * are a lot of outstanding writes for this mapping. 1603420e3646STrond Myklebust */ 1604a00dd6c0SJeff Layton if (nfsi->ncommit <= (nfsi->npages >> 1)) 1605420e3646STrond Myklebust goto out_mark_dirty; 1606420e3646STrond Myklebust 1607a00dd6c0SJeff Layton /* don't wait for the COMMIT response */ 1608420e3646STrond Myklebust flags = 0; 1609a00dd6c0SJeff Layton } 1610a00dd6c0SJeff Layton 1611420e3646STrond Myklebust ret = nfs_commit_inode(inode, flags); 1612420e3646STrond Myklebust if (ret >= 0) { 1613420e3646STrond Myklebust if (wbc->sync_mode == WB_SYNC_NONE) { 1614420e3646STrond Myklebust if (ret < wbc->nr_to_write) 1615420e3646STrond Myklebust wbc->nr_to_write -= ret; 1616420e3646STrond Myklebust else 1617420e3646STrond Myklebust wbc->nr_to_write = 0; 1618420e3646STrond Myklebust } 16198fc795f7STrond Myklebust return 0; 1620420e3646STrond Myklebust } 1621420e3646STrond Myklebust out_mark_dirty: 16228fc795f7STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 16238fc795f7STrond Myklebust return ret; 16248fc795f7STrond Myklebust } 1625c63c7b05STrond Myklebust #else 16268fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 16278fc795f7STrond Myklebust { 16288fc795f7STrond Myklebust return 0; 16298fc795f7STrond Myklebust } 16301da177e4SLinus Torvalds #endif 16311da177e4SLinus Torvalds 16328fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc) 16338fc795f7STrond Myklebust { 1634863a3c6cSAndy Adamson int ret; 1635863a3c6cSAndy Adamson 1636863a3c6cSAndy Adamson ret = nfs_commit_unstable_pages(inode, wbc); 1637863a3c6cSAndy Adamson if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) { 1638ef311537SAndy Adamson int status; 1639ef311537SAndy Adamson bool sync = true; 1640863a3c6cSAndy Adamson 1641846d5a09SWu Fengguang if (wbc->sync_mode == WB_SYNC_NONE) 1642ef311537SAndy Adamson sync = false; 1643863a3c6cSAndy Adamson 1644863a3c6cSAndy Adamson status = pnfs_layoutcommit_inode(inode, sync); 1645863a3c6cSAndy Adamson if (status < 0) 1646863a3c6cSAndy Adamson return status; 1647863a3c6cSAndy Adamson } 1648863a3c6cSAndy Adamson return ret; 16498fc795f7STrond Myklebust } 16508fc795f7STrond Myklebust 1651acdc53b2STrond Myklebust /* 1652acdc53b2STrond Myklebust * flush the inode to disk. 1653acdc53b2STrond Myklebust */ 1654acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode) 165534901f70STrond Myklebust { 165634901f70STrond Myklebust struct writeback_control wbc = { 165772cb77f4STrond Myklebust .sync_mode = WB_SYNC_ALL, 165834901f70STrond Myklebust .nr_to_write = LONG_MAX, 1659d7fb1207STrond Myklebust .range_start = 0, 1660d7fb1207STrond Myklebust .range_end = LLONG_MAX, 166134901f70STrond Myklebust }; 166234901f70STrond Myklebust 1663acdc53b2STrond Myklebust return sync_inode(inode, &wbc); 16641c75950bSTrond Myklebust } 16651c75950bSTrond Myklebust 16661b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page) 16671b3b4a1aSTrond Myklebust { 16681b3b4a1aSTrond Myklebust struct nfs_page *req; 16691b3b4a1aSTrond Myklebust int ret = 0; 16701b3b4a1aSTrond Myklebust 16711b3b4a1aSTrond Myklebust BUG_ON(!PageLocked(page)); 16721b3b4a1aSTrond Myklebust for (;;) { 1673ba8b06e6STrond Myklebust wait_on_page_writeback(page); 16741b3b4a1aSTrond Myklebust req = nfs_page_find_request(page); 16751b3b4a1aSTrond Myklebust if (req == NULL) 16761b3b4a1aSTrond Myklebust break; 16771b3b4a1aSTrond Myklebust if (nfs_lock_request_dontget(req)) { 16788dd37758STrond Myklebust nfs_clear_request_commit(req); 16791b3b4a1aSTrond Myklebust nfs_inode_remove_request(req); 16801b3b4a1aSTrond Myklebust /* 16811b3b4a1aSTrond Myklebust * In case nfs_inode_remove_request has marked the 16821b3b4a1aSTrond Myklebust * page as being dirty 16831b3b4a1aSTrond Myklebust */ 16841b3b4a1aSTrond Myklebust cancel_dirty_page(page, PAGE_CACHE_SIZE); 16851b3b4a1aSTrond Myklebust nfs_unlock_request(req); 16861b3b4a1aSTrond Myklebust break; 16871b3b4a1aSTrond Myklebust } 16881b3b4a1aSTrond Myklebust ret = nfs_wait_on_request(req); 1689c9edda71STrond Myklebust nfs_release_request(req); 16901b3b4a1aSTrond Myklebust if (ret < 0) 1691c988950eSTrond Myklebust break; 16921b3b4a1aSTrond Myklebust } 16931b3b4a1aSTrond Myklebust return ret; 16941b3b4a1aSTrond Myklebust } 16951b3b4a1aSTrond Myklebust 16961c75950bSTrond Myklebust /* 16971c75950bSTrond Myklebust * Write back all requests on one page - we do this before reading it. 16981c75950bSTrond Myklebust */ 16991c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page) 17001c75950bSTrond Myklebust { 17017f2f12d9STrond Myklebust loff_t range_start = page_offset(page); 17027f2f12d9STrond Myklebust loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); 17037f2f12d9STrond Myklebust struct writeback_control wbc = { 17047f2f12d9STrond Myklebust .sync_mode = WB_SYNC_ALL, 17057f2f12d9STrond Myklebust .nr_to_write = 0, 17067f2f12d9STrond Myklebust .range_start = range_start, 17077f2f12d9STrond Myklebust .range_end = range_end, 17087f2f12d9STrond Myklebust }; 17097f2f12d9STrond Myklebust int ret; 17107f2f12d9STrond Myklebust 17110522f6adSTrond Myklebust for (;;) { 1712ba8b06e6STrond Myklebust wait_on_page_writeback(page); 17137f2f12d9STrond Myklebust if (clear_page_dirty_for_io(page)) { 17147f2f12d9STrond Myklebust ret = nfs_writepage_locked(page, &wbc); 17157f2f12d9STrond Myklebust if (ret < 0) 17167f2f12d9STrond Myklebust goto out_error; 17170522f6adSTrond Myklebust continue; 17187f2f12d9STrond Myklebust } 17190522f6adSTrond Myklebust if (!PagePrivate(page)) 17200522f6adSTrond Myklebust break; 17210522f6adSTrond Myklebust ret = nfs_commit_inode(inode, FLUSH_SYNC); 17227f2f12d9STrond Myklebust if (ret < 0) 17237f2f12d9STrond Myklebust goto out_error; 17247f2f12d9STrond Myklebust } 17257f2f12d9STrond Myklebust return 0; 17267f2f12d9STrond Myklebust out_error: 17277f2f12d9STrond Myklebust return ret; 17281c75950bSTrond Myklebust } 17291c75950bSTrond Myklebust 1730074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION 1731074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage, 1732a6bc32b8SMel Gorman struct page *page, enum migrate_mode mode) 1733074cc1deSTrond Myklebust { 17342da95652SJeff Layton /* 17352da95652SJeff Layton * If PagePrivate is set, then the page is currently associated with 17362da95652SJeff Layton * an in-progress read or write request. Don't try to migrate it. 17372da95652SJeff Layton * 17382da95652SJeff Layton * FIXME: we could do this in principle, but we'll need a way to ensure 17392da95652SJeff Layton * that we can safely release the inode reference while holding 17402da95652SJeff Layton * the page lock. 17412da95652SJeff Layton */ 17422da95652SJeff Layton if (PagePrivate(page)) 17432da95652SJeff Layton return -EBUSY; 1744074cc1deSTrond Myklebust 1745074cc1deSTrond Myklebust nfs_fscache_release_page(page, GFP_KERNEL); 1746074cc1deSTrond Myklebust 1747a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 1748074cc1deSTrond Myklebust } 1749074cc1deSTrond Myklebust #endif 1750074cc1deSTrond Myklebust 1751f7b422b1SDavid Howells int __init nfs_init_writepagecache(void) 17521da177e4SLinus Torvalds { 17531da177e4SLinus Torvalds nfs_wdata_cachep = kmem_cache_create("nfs_write_data", 1754cd841605SFred Isaman sizeof(struct nfs_write_header), 17551da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 175620c2df83SPaul Mundt NULL); 17571da177e4SLinus Torvalds if (nfs_wdata_cachep == NULL) 17581da177e4SLinus Torvalds return -ENOMEM; 17591da177e4SLinus Torvalds 176093d2341cSMatthew Dobson nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, 17611da177e4SLinus Torvalds nfs_wdata_cachep); 17621da177e4SLinus Torvalds if (nfs_wdata_mempool == NULL) 17631da177e4SLinus Torvalds return -ENOMEM; 17641da177e4SLinus Torvalds 17650b7c0153SFred Isaman nfs_cdata_cachep = kmem_cache_create("nfs_commit_data", 17660b7c0153SFred Isaman sizeof(struct nfs_commit_data), 17670b7c0153SFred Isaman 0, SLAB_HWCACHE_ALIGN, 17680b7c0153SFred Isaman NULL); 17690b7c0153SFred Isaman if (nfs_cdata_cachep == NULL) 17700b7c0153SFred Isaman return -ENOMEM; 17710b7c0153SFred Isaman 177293d2341cSMatthew Dobson nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, 17731da177e4SLinus Torvalds nfs_wdata_cachep); 17741da177e4SLinus Torvalds if (nfs_commit_mempool == NULL) 17751da177e4SLinus Torvalds return -ENOMEM; 17761da177e4SLinus Torvalds 177789a09141SPeter Zijlstra /* 177889a09141SPeter Zijlstra * NFS congestion size, scale with available memory. 177989a09141SPeter Zijlstra * 178089a09141SPeter Zijlstra * 64MB: 8192k 178189a09141SPeter Zijlstra * 128MB: 11585k 178289a09141SPeter Zijlstra * 256MB: 16384k 178389a09141SPeter Zijlstra * 512MB: 23170k 178489a09141SPeter Zijlstra * 1GB: 32768k 178589a09141SPeter Zijlstra * 2GB: 46340k 178689a09141SPeter Zijlstra * 4GB: 65536k 178789a09141SPeter Zijlstra * 8GB: 92681k 178889a09141SPeter Zijlstra * 16GB: 131072k 178989a09141SPeter Zijlstra * 179089a09141SPeter Zijlstra * This allows larger machines to have larger/more transfers. 179189a09141SPeter Zijlstra * Limit the default to 256M 179289a09141SPeter Zijlstra */ 179389a09141SPeter Zijlstra nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); 179489a09141SPeter Zijlstra if (nfs_congestion_kb > 256*1024) 179589a09141SPeter Zijlstra nfs_congestion_kb = 256*1024; 179689a09141SPeter Zijlstra 17971da177e4SLinus Torvalds return 0; 17981da177e4SLinus Torvalds } 17991da177e4SLinus Torvalds 1800266bee88SDavid Brownell void nfs_destroy_writepagecache(void) 18011da177e4SLinus Torvalds { 18021da177e4SLinus Torvalds mempool_destroy(nfs_commit_mempool); 18031da177e4SLinus Torvalds mempool_destroy(nfs_wdata_mempool); 18041a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_wdata_cachep); 18051da177e4SLinus Torvalds } 18061da177e4SLinus Torvalds 1807