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 */ 42f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req); 436c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops; 44788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops; 45061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops; 46f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops; 471da177e4SLinus Torvalds 48e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep; 493feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool; 500b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep; 511da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool; 521da177e4SLinus Torvalds 530b7c0153SFred Isaman struct nfs_commit_data *nfs_commitdata_alloc(void) 541da177e4SLinus Torvalds { 550b7c0153SFred Isaman struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS); 5640859d7eSChuck Lever 571da177e4SLinus Torvalds if (p) { 581da177e4SLinus Torvalds memset(p, 0, sizeof(*p)); 591da177e4SLinus Torvalds INIT_LIST_HEAD(&p->pages); 601da177e4SLinus Torvalds } 611da177e4SLinus Torvalds return p; 621da177e4SLinus Torvalds } 63e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc); 641da177e4SLinus Torvalds 650b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p) 661da177e4SLinus Torvalds { 671da177e4SLinus Torvalds mempool_free(p, nfs_commit_mempool); 681da177e4SLinus Torvalds } 69e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free); 701da177e4SLinus Torvalds 716c75dc0dSFred Isaman struct nfs_write_header *nfs_writehdr_alloc(void) 723feb2d49STrond Myklebust { 73cd841605SFred Isaman struct nfs_write_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS); 743feb2d49STrond Myklebust 753feb2d49STrond Myklebust if (p) { 76cd841605SFred Isaman struct nfs_pgio_header *hdr = &p->header; 77cd841605SFred Isaman 783feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 79cd841605SFred Isaman INIT_LIST_HEAD(&hdr->pages); 806c75dc0dSFred Isaman INIT_LIST_HEAD(&hdr->rpc_list); 816c75dc0dSFred Isaman spin_lock_init(&hdr->lock); 826c75dc0dSFred Isaman atomic_set(&hdr->refcnt, 0); 839bce008bSTrond Myklebust hdr->verf = &p->verf; 843feb2d49STrond Myklebust } 853feb2d49STrond Myklebust return p; 863feb2d49STrond Myklebust } 873feb2d49STrond Myklebust 881763da12SFred Isaman static struct nfs_write_data *nfs_writedata_alloc(struct nfs_pgio_header *hdr, 896c75dc0dSFred Isaman unsigned int pagecount) 906c75dc0dSFred Isaman { 916c75dc0dSFred Isaman struct nfs_write_data *data, *prealloc; 926c75dc0dSFred Isaman 936c75dc0dSFred Isaman prealloc = &container_of(hdr, struct nfs_write_header, header)->rpc_data; 946c75dc0dSFred Isaman if (prealloc->header == NULL) 956c75dc0dSFred Isaman data = prealloc; 966c75dc0dSFred Isaman else 976c75dc0dSFred Isaman data = kzalloc(sizeof(*data), GFP_KERNEL); 986c75dc0dSFred Isaman if (!data) 996c75dc0dSFred Isaman goto out; 1006c75dc0dSFred Isaman 1016c75dc0dSFred Isaman if (nfs_pgarray_set(&data->pages, pagecount)) { 1026c75dc0dSFred Isaman data->header = hdr; 1036c75dc0dSFred Isaman atomic_inc(&hdr->refcnt); 1046c75dc0dSFred Isaman } else { 1056c75dc0dSFred Isaman if (data != prealloc) 1066c75dc0dSFred Isaman kfree(data); 1076c75dc0dSFred Isaman data = NULL; 1086c75dc0dSFred Isaman } 1096c75dc0dSFred Isaman out: 1106c75dc0dSFred Isaman return data; 1116c75dc0dSFred Isaman } 1126c75dc0dSFred Isaman 113cd841605SFred Isaman void nfs_writehdr_free(struct nfs_pgio_header *hdr) 1143feb2d49STrond Myklebust { 115cd841605SFred Isaman struct nfs_write_header *whdr = container_of(hdr, struct nfs_write_header, header); 116cd841605SFred Isaman mempool_free(whdr, nfs_wdata_mempool); 1173feb2d49STrond Myklebust } 1183feb2d49STrond Myklebust 119dce81290STrond Myklebust void nfs_writedata_release(struct nfs_write_data *wdata) 1201da177e4SLinus Torvalds { 1216c75dc0dSFred Isaman struct nfs_pgio_header *hdr = wdata->header; 1226c75dc0dSFred Isaman struct nfs_write_header *write_header = container_of(hdr, struct nfs_write_header, header); 1236c75dc0dSFred Isaman 124383ba719STrond Myklebust put_nfs_open_context(wdata->args.context); 12530dd374fSFred Isaman if (wdata->pages.pagevec != wdata->pages.page_array) 12630dd374fSFred Isaman kfree(wdata->pages.pagevec); 1276c75dc0dSFred Isaman if (wdata != &write_header->rpc_data) 1286c75dc0dSFred Isaman kfree(wdata); 1296c75dc0dSFred Isaman else 1306c75dc0dSFred Isaman wdata->header = NULL; 1316c75dc0dSFred Isaman if (atomic_dec_and_test(&hdr->refcnt)) 132061ae2edSFred Isaman hdr->completion_ops->completion(hdr); 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 1357b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error) 1367b159fc1STrond Myklebust { 1377b159fc1STrond Myklebust ctx->error = error; 1387b159fc1STrond Myklebust smp_wmb(); 1397b159fc1STrond Myklebust set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 1407b159fc1STrond Myklebust } 1417b159fc1STrond Myklebust 142277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page) 143277459d2STrond Myklebust { 144277459d2STrond Myklebust struct nfs_page *req = NULL; 145277459d2STrond Myklebust 146277459d2STrond Myklebust if (PagePrivate(page)) { 147277459d2STrond Myklebust req = (struct nfs_page *)page_private(page); 148277459d2STrond Myklebust if (req != NULL) 149c03b4024STrond Myklebust kref_get(&req->wb_kref); 150277459d2STrond Myklebust } 151277459d2STrond Myklebust return req; 152277459d2STrond Myklebust } 153277459d2STrond Myklebust 154277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page) 155277459d2STrond Myklebust { 156587142f8STrond Myklebust struct inode *inode = page->mapping->host; 157277459d2STrond Myklebust struct nfs_page *req = NULL; 158277459d2STrond Myklebust 159587142f8STrond Myklebust spin_lock(&inode->i_lock); 160277459d2STrond Myklebust req = nfs_page_find_request_locked(page); 161587142f8STrond Myklebust spin_unlock(&inode->i_lock); 162277459d2STrond Myklebust return req; 163277459d2STrond Myklebust } 164277459d2STrond Myklebust 1651da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */ 1661da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) 1671da177e4SLinus Torvalds { 1681da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 169a3d01454STrond Myklebust loff_t end, i_size; 170a3d01454STrond Myklebust pgoff_t end_index; 1711da177e4SLinus Torvalds 172a3d01454STrond Myklebust spin_lock(&inode->i_lock); 173a3d01454STrond Myklebust i_size = i_size_read(inode); 174a3d01454STrond Myklebust end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; 1751da177e4SLinus Torvalds if (i_size > 0 && page->index < end_index) 176a3d01454STrond Myklebust goto out; 1771da177e4SLinus Torvalds end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count); 1781da177e4SLinus Torvalds if (i_size >= end) 179a3d01454STrond Myklebust goto out; 1801da177e4SLinus Torvalds i_size_write(inode, end); 181a3d01454STrond Myklebust nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); 182a3d01454STrond Myklebust out: 183a3d01454STrond Myklebust spin_unlock(&inode->i_lock); 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds 186a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */ 187a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page) 188a301b777STrond Myklebust { 189a301b777STrond Myklebust SetPageError(page); 190a301b777STrond Myklebust nfs_zap_mapping(page->mapping->host, page->mapping); 191a301b777STrond Myklebust } 192a301b777STrond Myklebust 1931da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request 1941da177e4SLinus Torvalds * covers the full page. 1951da177e4SLinus Torvalds */ 1961da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count) 1971da177e4SLinus Torvalds { 1981da177e4SLinus Torvalds if (PageUptodate(page)) 1991da177e4SLinus Torvalds return; 2001da177e4SLinus Torvalds if (base != 0) 2011da177e4SLinus Torvalds return; 20249a70f27STrond Myklebust if (count != nfs_page_length(page)) 2031da177e4SLinus Torvalds return; 2041da177e4SLinus Torvalds SetPageUptodate(page); 2051da177e4SLinus Torvalds } 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc) 2081da177e4SLinus Torvalds { 2091da177e4SLinus Torvalds if (wbc->for_reclaim) 210c63c7b05STrond Myklebust return FLUSH_HIGHPRI | FLUSH_STABLE; 211b17621feSWu Fengguang if (wbc->for_kupdate || wbc->for_background) 212b31268acSTrond Myklebust return FLUSH_LOWPRI | FLUSH_COND_STABLE; 213b31268acSTrond Myklebust return FLUSH_COND_STABLE; 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds /* 21789a09141SPeter Zijlstra * NFS congestion control 21889a09141SPeter Zijlstra */ 21989a09141SPeter Zijlstra 22089a09141SPeter Zijlstra int nfs_congestion_kb; 22189a09141SPeter Zijlstra 22289a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) 22389a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH \ 22489a09141SPeter Zijlstra (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) 22589a09141SPeter Zijlstra 2265a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page) 22789a09141SPeter Zijlstra { 2285a6d41b3STrond Myklebust int ret = test_set_page_writeback(page); 2295a6d41b3STrond Myklebust 2305a6d41b3STrond Myklebust if (!ret) { 23189a09141SPeter Zijlstra struct inode *inode = page->mapping->host; 23289a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 23389a09141SPeter Zijlstra 234277866a0SPeter Zijlstra if (atomic_long_inc_return(&nfss->writeback) > 2358aa7e847SJens Axboe NFS_CONGESTION_ON_THRESH) { 2368aa7e847SJens Axboe set_bdi_congested(&nfss->backing_dev_info, 2378aa7e847SJens Axboe BLK_RW_ASYNC); 2388aa7e847SJens Axboe } 23989a09141SPeter Zijlstra } 2405a6d41b3STrond Myklebust return ret; 24189a09141SPeter Zijlstra } 24289a09141SPeter Zijlstra 24389a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page) 24489a09141SPeter Zijlstra { 24589a09141SPeter Zijlstra struct inode *inode = page->mapping->host; 24689a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 24789a09141SPeter Zijlstra 24889a09141SPeter Zijlstra end_page_writeback(page); 249c4dc4beeSPeter Zijlstra if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) 2508aa7e847SJens Axboe clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC); 25189a09141SPeter Zijlstra } 25289a09141SPeter Zijlstra 253cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock) 254e261f51fSTrond Myklebust { 255587142f8STrond Myklebust struct inode *inode = page->mapping->host; 256e261f51fSTrond Myklebust struct nfs_page *req; 257e261f51fSTrond Myklebust int ret; 258e261f51fSTrond Myklebust 259587142f8STrond Myklebust spin_lock(&inode->i_lock); 260e261f51fSTrond Myklebust for (;;) { 261e261f51fSTrond Myklebust req = nfs_page_find_request_locked(page); 262074cc1deSTrond Myklebust if (req == NULL) 263074cc1deSTrond Myklebust break; 2647ad84aa9STrond Myklebust if (nfs_lock_request(req)) 265e261f51fSTrond Myklebust break; 266e261f51fSTrond Myklebust /* Note: If we hold the page lock, as is the case in nfs_writepage, 2677ad84aa9STrond Myklebust * then the call to nfs_lock_request() will always 268e261f51fSTrond Myklebust * succeed provided that someone hasn't already marked the 269e261f51fSTrond Myklebust * request as dirty (in which case we don't care). 270e261f51fSTrond Myklebust */ 271587142f8STrond Myklebust spin_unlock(&inode->i_lock); 272cfb506e1STrond Myklebust if (!nonblock) 273e261f51fSTrond Myklebust ret = nfs_wait_on_request(req); 274cfb506e1STrond Myklebust else 275cfb506e1STrond Myklebust ret = -EAGAIN; 276e261f51fSTrond Myklebust nfs_release_request(req); 277e261f51fSTrond Myklebust if (ret != 0) 278074cc1deSTrond Myklebust return ERR_PTR(ret); 279587142f8STrond Myklebust spin_lock(&inode->i_lock); 280e261f51fSTrond Myklebust } 281587142f8STrond Myklebust spin_unlock(&inode->i_lock); 282074cc1deSTrond Myklebust return req; 283612c9384STrond Myklebust } 284074cc1deSTrond Myklebust 285074cc1deSTrond Myklebust /* 286074cc1deSTrond Myklebust * Find an associated nfs write request, and prepare to flush it out 287074cc1deSTrond Myklebust * May return an error if the user signalled nfs_wait_on_request(). 288074cc1deSTrond Myklebust */ 289074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, 290cfb506e1STrond Myklebust struct page *page, bool nonblock) 291074cc1deSTrond Myklebust { 292074cc1deSTrond Myklebust struct nfs_page *req; 293074cc1deSTrond Myklebust int ret = 0; 294074cc1deSTrond Myklebust 295cfb506e1STrond Myklebust req = nfs_find_and_lock_request(page, nonblock); 296074cc1deSTrond Myklebust if (!req) 297074cc1deSTrond Myklebust goto out; 298074cc1deSTrond Myklebust ret = PTR_ERR(req); 299074cc1deSTrond Myklebust if (IS_ERR(req)) 300074cc1deSTrond Myklebust goto out; 301074cc1deSTrond Myklebust 302074cc1deSTrond Myklebust ret = nfs_set_page_writeback(page); 303074cc1deSTrond Myklebust BUG_ON(ret != 0); 304074cc1deSTrond Myklebust BUG_ON(test_bit(PG_CLEAN, &req->wb_flags)); 305074cc1deSTrond Myklebust 306f8512ad0SFred Isaman if (!nfs_pageio_add_request(pgio, req)) { 307f8512ad0SFred Isaman nfs_redirty_request(req); 308074cc1deSTrond Myklebust ret = pgio->pg_error; 309f8512ad0SFred Isaman } 310074cc1deSTrond Myklebust out: 311074cc1deSTrond Myklebust return ret; 312e261f51fSTrond Myklebust } 313e261f51fSTrond Myklebust 314f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio) 315f758c885STrond Myklebust { 316f758c885STrond Myklebust struct inode *inode = page->mapping->host; 317cfb506e1STrond Myklebust int ret; 318f758c885STrond Myklebust 319f758c885STrond Myklebust nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); 320f758c885STrond Myklebust nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); 321f758c885STrond Myklebust 322f758c885STrond Myklebust nfs_pageio_cond_complete(pgio, page->index); 3231b430beeSWu Fengguang ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE); 324cfb506e1STrond Myklebust if (ret == -EAGAIN) { 325cfb506e1STrond Myklebust redirty_page_for_writepage(wbc, page); 326cfb506e1STrond Myklebust ret = 0; 327cfb506e1STrond Myklebust } 328cfb506e1STrond Myklebust return ret; 329f758c885STrond Myklebust } 330f758c885STrond Myklebust 331e261f51fSTrond Myklebust /* 3321da177e4SLinus Torvalds * Write an mmapped page to the server. 3331da177e4SLinus Torvalds */ 3344d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc) 3351da177e4SLinus Torvalds { 336f758c885STrond Myklebust struct nfs_pageio_descriptor pgio; 337e261f51fSTrond Myklebust int err; 3381da177e4SLinus Torvalds 33957208fa7SBryan Schumaker NFS_PROTO(page->mapping->host)->write_pageio_init(&pgio, 34057208fa7SBryan Schumaker page->mapping->host, 34157208fa7SBryan Schumaker wb_priority(wbc), 342061ae2edSFred Isaman &nfs_async_write_completion_ops); 343f758c885STrond Myklebust err = nfs_do_writepage(page, wbc, &pgio); 344f758c885STrond Myklebust nfs_pageio_complete(&pgio); 345f758c885STrond Myklebust if (err < 0) 3464d770ccfSTrond Myklebust return err; 347f758c885STrond Myklebust if (pgio.pg_error < 0) 348f758c885STrond Myklebust return pgio.pg_error; 349f758c885STrond Myklebust return 0; 3504d770ccfSTrond Myklebust } 3514d770ccfSTrond Myklebust 3524d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc) 3534d770ccfSTrond Myklebust { 354f758c885STrond Myklebust int ret; 3554d770ccfSTrond Myklebust 356f758c885STrond Myklebust ret = nfs_writepage_locked(page, wbc); 3571da177e4SLinus Torvalds unlock_page(page); 358f758c885STrond Myklebust return ret; 359f758c885STrond Myklebust } 360f758c885STrond Myklebust 361f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) 362f758c885STrond Myklebust { 363f758c885STrond Myklebust int ret; 364f758c885STrond Myklebust 365f758c885STrond Myklebust ret = nfs_do_writepage(page, wbc, data); 366f758c885STrond Myklebust unlock_page(page); 367f758c885STrond Myklebust return ret; 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds 3701da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) 3711da177e4SLinus Torvalds { 3721da177e4SLinus Torvalds struct inode *inode = mapping->host; 37372cb77f4STrond Myklebust unsigned long *bitlock = &NFS_I(inode)->flags; 374c63c7b05STrond Myklebust struct nfs_pageio_descriptor pgio; 3751da177e4SLinus Torvalds int err; 3761da177e4SLinus Torvalds 37772cb77f4STrond Myklebust /* Stop dirtying of new pages while we sync */ 37872cb77f4STrond Myklebust err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING, 37972cb77f4STrond Myklebust nfs_wait_bit_killable, TASK_KILLABLE); 38072cb77f4STrond Myklebust if (err) 38172cb77f4STrond Myklebust goto out_err; 38272cb77f4STrond Myklebust 38391d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); 38491d5b470SChuck Lever 38557208fa7SBryan Schumaker NFS_PROTO(inode)->write_pageio_init(&pgio, inode, wb_priority(wbc), &nfs_async_write_completion_ops); 386f758c885STrond Myklebust err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); 387c63c7b05STrond Myklebust nfs_pageio_complete(&pgio); 38872cb77f4STrond Myklebust 38972cb77f4STrond Myklebust clear_bit_unlock(NFS_INO_FLUSHING, bitlock); 39072cb77f4STrond Myklebust smp_mb__after_clear_bit(); 39172cb77f4STrond Myklebust wake_up_bit(bitlock, NFS_INO_FLUSHING); 39272cb77f4STrond Myklebust 393f758c885STrond Myklebust if (err < 0) 39472cb77f4STrond Myklebust goto out_err; 39572cb77f4STrond Myklebust err = pgio.pg_error; 39672cb77f4STrond Myklebust if (err < 0) 39772cb77f4STrond Myklebust goto out_err; 398c63c7b05STrond Myklebust return 0; 39972cb77f4STrond Myklebust out_err: 40072cb77f4STrond Myklebust return err; 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds /* 4041da177e4SLinus Torvalds * Insert a write request into an inode 4051da177e4SLinus Torvalds */ 406d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req) 4071da177e4SLinus Torvalds { 4081da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 409e7d39069STrond Myklebust 410e7d39069STrond Myklebust /* Lock the request! */ 4117ad84aa9STrond Myklebust nfs_lock_request(req); 412e7d39069STrond Myklebust 413e7d39069STrond Myklebust spin_lock(&inode->i_lock); 414011e2a7fSBryan Schumaker if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE)) 415a9a4a87aSTrond Myklebust inode->i_version++; 4162df485a7STrond Myklebust set_bit(PG_MAPPED, &req->wb_flags); 417deb7d638STrond Myklebust SetPagePrivate(req->wb_page); 418277459d2STrond Myklebust set_page_private(req->wb_page, (unsigned long)req); 4191da177e4SLinus Torvalds nfsi->npages++; 420c03b4024STrond Myklebust kref_get(&req->wb_kref); 421e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds /* 42589a09141SPeter Zijlstra * Remove a write request from an inode 4261da177e4SLinus Torvalds */ 4271da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req) 4281da177e4SLinus Torvalds { 4293d4ff43dSAl Viro struct inode *inode = req->wb_context->dentry->d_inode; 4301da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds BUG_ON (!NFS_WBACK_BUSY(req)); 4331da177e4SLinus Torvalds 434587142f8STrond Myklebust spin_lock(&inode->i_lock); 435277459d2STrond Myklebust set_page_private(req->wb_page, 0); 436deb7d638STrond Myklebust ClearPagePrivate(req->wb_page); 4372df485a7STrond Myklebust clear_bit(PG_MAPPED, &req->wb_flags); 4381da177e4SLinus Torvalds nfsi->npages--; 439587142f8STrond Myklebust spin_unlock(&inode->i_lock); 4401da177e4SLinus Torvalds nfs_release_request(req); 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds 44361822ab5STrond Myklebust static void 4446d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req) 44561822ab5STrond Myklebust { 44661822ab5STrond Myklebust __set_page_dirty_nobuffers(req->wb_page); 44761822ab5STrond Myklebust } 44861822ab5STrond Myklebust 4491da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 4508dd37758STrond Myklebust /** 4518dd37758STrond Myklebust * nfs_request_add_commit_list - add request to a commit list 4528dd37758STrond Myklebust * @req: pointer to a struct nfs_page 453ea2cf228SFred Isaman * @dst: commit list head 454ea2cf228SFred Isaman * @cinfo: holds list lock and accounting info 4558dd37758STrond Myklebust * 456ea2cf228SFred Isaman * This sets the PG_CLEAN bit, updates the cinfo count of 4578dd37758STrond Myklebust * number of outstanding requests requiring a commit as well as 4588dd37758STrond Myklebust * the MM page stats. 4598dd37758STrond Myklebust * 460ea2cf228SFred Isaman * The caller must _not_ hold the cinfo->lock, but must be 4618dd37758STrond Myklebust * holding the nfs_page lock. 4628dd37758STrond Myklebust */ 4638dd37758STrond Myklebust void 464ea2cf228SFred Isaman nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst, 465ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 4668dd37758STrond Myklebust { 4678dd37758STrond Myklebust set_bit(PG_CLEAN, &(req)->wb_flags); 468ea2cf228SFred Isaman spin_lock(cinfo->lock); 469ea2cf228SFred Isaman nfs_list_add_request(req, dst); 470ea2cf228SFred Isaman cinfo->mds->ncommit++; 471ea2cf228SFred Isaman spin_unlock(cinfo->lock); 47256f9cd68SFred Isaman if (!cinfo->dreq) { 4738dd37758STrond Myklebust inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 47456f9cd68SFred Isaman inc_bdi_stat(req->wb_page->mapping->backing_dev_info, 47556f9cd68SFred Isaman BDI_RECLAIMABLE); 47656f9cd68SFred Isaman __mark_inode_dirty(req->wb_context->dentry->d_inode, 47756f9cd68SFred Isaman I_DIRTY_DATASYNC); 47856f9cd68SFred Isaman } 4798dd37758STrond Myklebust } 4808dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list); 4818dd37758STrond Myklebust 4828dd37758STrond Myklebust /** 4838dd37758STrond Myklebust * nfs_request_remove_commit_list - Remove request from a commit list 4848dd37758STrond Myklebust * @req: pointer to a nfs_page 485ea2cf228SFred Isaman * @cinfo: holds list lock and accounting info 4868dd37758STrond Myklebust * 487ea2cf228SFred Isaman * This clears the PG_CLEAN bit, and updates the cinfo's count of 4888dd37758STrond Myklebust * number of outstanding requests requiring a commit 4898dd37758STrond Myklebust * It does not update the MM page stats. 4908dd37758STrond Myklebust * 491ea2cf228SFred Isaman * The caller _must_ hold the cinfo->lock and the nfs_page lock. 4928dd37758STrond Myklebust */ 4938dd37758STrond Myklebust void 494ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req, 495ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 4968dd37758STrond Myklebust { 4978dd37758STrond Myklebust if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) 4988dd37758STrond Myklebust return; 4998dd37758STrond Myklebust nfs_list_remove_request(req); 500ea2cf228SFred Isaman cinfo->mds->ncommit--; 5018dd37758STrond Myklebust } 5028dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list); 5038dd37758STrond Myklebust 504ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, 505ea2cf228SFred Isaman struct inode *inode) 506ea2cf228SFred Isaman { 507ea2cf228SFred Isaman cinfo->lock = &inode->i_lock; 508ea2cf228SFred Isaman cinfo->mds = &NFS_I(inode)->commit_info; 509ea2cf228SFred Isaman cinfo->ds = pnfs_get_ds_info(inode); 510b359f9d0SFred Isaman cinfo->dreq = NULL; 511f453a54aSFred Isaman cinfo->completion_ops = &nfs_commit_completion_ops; 512ea2cf228SFred Isaman } 513ea2cf228SFred Isaman 514ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo, 515ea2cf228SFred Isaman struct inode *inode, 516ea2cf228SFred Isaman struct nfs_direct_req *dreq) 517ea2cf228SFred Isaman { 5181763da12SFred Isaman if (dreq) 5191763da12SFred Isaman nfs_init_cinfo_from_dreq(cinfo, dreq); 5201763da12SFred Isaman else 521ea2cf228SFred Isaman nfs_init_cinfo_from_inode(cinfo, inode); 522ea2cf228SFred Isaman } 523ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo); 5248dd37758STrond Myklebust 5251da177e4SLinus Torvalds /* 5261da177e4SLinus Torvalds * Add a request to the inode's commit list. 5271da177e4SLinus Torvalds */ 5281763da12SFred Isaman void 529ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, 530ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 5311da177e4SLinus Torvalds { 532ea2cf228SFred Isaman if (pnfs_mark_request_commit(req, lseg, cinfo)) 5338dd37758STrond Myklebust return; 534ea2cf228SFred Isaman nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo); 5351da177e4SLinus Torvalds } 5368e821cadSTrond Myklebust 537d6d6dc7cSFred Isaman static void 538d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page) 539e468bae9STrond Myklebust { 540e468bae9STrond Myklebust dec_zone_page_state(page, NR_UNSTABLE_NFS); 541e468bae9STrond Myklebust dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE); 542e468bae9STrond Myklebust } 543d6d6dc7cSFred Isaman 5448dd37758STrond Myklebust static void 545d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req) 546d6d6dc7cSFred Isaman { 5478dd37758STrond Myklebust if (test_bit(PG_CLEAN, &req->wb_flags)) { 5488dd37758STrond Myklebust struct inode *inode = req->wb_context->dentry->d_inode; 549ea2cf228SFred Isaman struct nfs_commit_info cinfo; 550d6d6dc7cSFred Isaman 551ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, inode); 552ea2cf228SFred Isaman if (!pnfs_clear_request_commit(req, &cinfo)) { 553ea2cf228SFred Isaman spin_lock(cinfo.lock); 554ea2cf228SFred Isaman nfs_request_remove_commit_list(req, &cinfo); 555ea2cf228SFred Isaman spin_unlock(cinfo.lock); 556d6d6dc7cSFred Isaman } 5578dd37758STrond Myklebust nfs_clear_page_commit(req->wb_page); 5588dd37758STrond Myklebust } 559e468bae9STrond Myklebust } 560e468bae9STrond Myklebust 5618e821cadSTrond Myklebust static inline 5628e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 5638e821cadSTrond Myklebust { 564465d5243SFred Isaman if (data->verf.committed == NFS_DATA_SYNC) 565cd841605SFred Isaman return data->header->lseg == NULL; 5668e821cadSTrond Myklebust return data->verf.committed != NFS_FILE_SYNC; 5678e821cadSTrond Myklebust } 5688e821cadSTrond Myklebust 5698e821cadSTrond Myklebust #else 57068cd6fa4SBryan Schumaker static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, 57168cd6fa4SBryan Schumaker struct inode *inode) 57268cd6fa4SBryan Schumaker { 57368cd6fa4SBryan Schumaker } 57468cd6fa4SBryan Schumaker 57568cd6fa4SBryan Schumaker void nfs_init_cinfo(struct nfs_commit_info *cinfo, 57668cd6fa4SBryan Schumaker struct inode *inode, 57768cd6fa4SBryan Schumaker struct nfs_direct_req *dreq) 57868cd6fa4SBryan Schumaker { 57968cd6fa4SBryan Schumaker } 58068cd6fa4SBryan Schumaker 5811763da12SFred Isaman void 582ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, 583ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 5848e821cadSTrond Myklebust { 5858e821cadSTrond Myklebust } 5868e821cadSTrond Myklebust 5878dd37758STrond Myklebust static void 588e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req) 589e468bae9STrond Myklebust { 590e468bae9STrond Myklebust } 591e468bae9STrond Myklebust 5928e821cadSTrond Myklebust static inline 5938e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 5948e821cadSTrond Myklebust { 5958e821cadSTrond Myklebust return 0; 5968e821cadSTrond Myklebust } 5978e821cadSTrond Myklebust 5981da177e4SLinus Torvalds #endif 5991da177e4SLinus Torvalds 600061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr) 6016c75dc0dSFred Isaman { 602ea2cf228SFred Isaman struct nfs_commit_info cinfo; 6036c75dc0dSFred Isaman unsigned long bytes = 0; 6046c75dc0dSFred Isaman 6056c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 6066c75dc0dSFred Isaman goto out; 607ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, hdr->inode); 6086c75dc0dSFred Isaman while (!list_empty(&hdr->pages)) { 6096c75dc0dSFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 6106c75dc0dSFred Isaman 6116c75dc0dSFred Isaman bytes += req->wb_bytes; 6126c75dc0dSFred Isaman nfs_list_remove_request(req); 6136c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && 6146c75dc0dSFred Isaman (hdr->good_bytes < bytes)) { 615d1182b33STrond Myklebust nfs_set_pageerror(req->wb_page); 6166c75dc0dSFred Isaman nfs_context_set_write_error(req->wb_context, hdr->error); 6176c75dc0dSFred Isaman goto remove_req; 6186c75dc0dSFred Isaman } 6196c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) { 6206c75dc0dSFred Isaman nfs_mark_request_dirty(req); 6216c75dc0dSFred Isaman goto next; 6226c75dc0dSFred Isaman } 6236c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) { 6242f2c63bcSTrond Myklebust memcpy(&req->wb_verf, &hdr->verf->verifier, sizeof(req->wb_verf)); 625ea2cf228SFred Isaman nfs_mark_request_commit(req, hdr->lseg, &cinfo); 6266c75dc0dSFred Isaman goto next; 6276c75dc0dSFred Isaman } 6286c75dc0dSFred Isaman remove_req: 6296c75dc0dSFred Isaman nfs_inode_remove_request(req); 6306c75dc0dSFred Isaman next: 6311d1afcbcSTrond Myklebust nfs_unlock_request(req); 632d1182b33STrond Myklebust nfs_end_page_writeback(req->wb_page); 6333aff4ebbSTrond Myklebust nfs_release_request(req); 6346c75dc0dSFred Isaman } 6356c75dc0dSFred Isaman out: 6366c75dc0dSFred Isaman hdr->release(hdr); 6376c75dc0dSFred Isaman } 6386c75dc0dSFred Isaman 63947c62564STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 640ea2cf228SFred Isaman static unsigned long 641ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo) 642fb8a1f11STrond Myklebust { 643ea2cf228SFred Isaman return cinfo->mds->ncommit; 644fb8a1f11STrond Myklebust } 645fb8a1f11STrond Myklebust 646ea2cf228SFred Isaman /* cinfo->lock held by caller */ 6471763da12SFred Isaman int 648ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst, 649ea2cf228SFred Isaman struct nfs_commit_info *cinfo, int max) 650d6d6dc7cSFred Isaman { 651d6d6dc7cSFred Isaman struct nfs_page *req, *tmp; 652d6d6dc7cSFred Isaman int ret = 0; 653d6d6dc7cSFred Isaman 654d6d6dc7cSFred Isaman list_for_each_entry_safe(req, tmp, src, wb_list) { 6558dd37758STrond Myklebust if (!nfs_lock_request(req)) 6568dd37758STrond Myklebust continue; 6577ad84aa9STrond Myklebust kref_get(&req->wb_kref); 658ea2cf228SFred Isaman if (cond_resched_lock(cinfo->lock)) 6593b3be88dSTrond Myklebust list_safe_reset_next(req, tmp, wb_list); 660ea2cf228SFred Isaman nfs_request_remove_commit_list(req, cinfo); 6618dd37758STrond Myklebust nfs_list_add_request(req, dst); 662d6d6dc7cSFred Isaman ret++; 6631763da12SFred Isaman if ((ret == max) && !cinfo->dreq) 664d6d6dc7cSFred Isaman break; 665d6d6dc7cSFred Isaman } 666d6d6dc7cSFred Isaman return ret; 667d6d6dc7cSFred Isaman } 668d6d6dc7cSFred Isaman 6691da177e4SLinus Torvalds /* 6701da177e4SLinus Torvalds * nfs_scan_commit - Scan an inode for commit requests 6711da177e4SLinus Torvalds * @inode: NFS inode to scan 672ea2cf228SFred Isaman * @dst: mds destination list 673ea2cf228SFred Isaman * @cinfo: mds and ds lists of reqs ready to commit 6741da177e4SLinus Torvalds * 6751da177e4SLinus Torvalds * Moves requests from the inode's 'commit' request list. 6761da177e4SLinus Torvalds * The requests are *not* checked to ensure that they form a contiguous set. 6771da177e4SLinus Torvalds */ 6781763da12SFred Isaman int 679ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst, 680ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 6811da177e4SLinus Torvalds { 682d6d6dc7cSFred Isaman int ret = 0; 683fb8a1f11STrond Myklebust 684ea2cf228SFred Isaman spin_lock(cinfo->lock); 685ea2cf228SFred Isaman if (cinfo->mds->ncommit > 0) { 6868dd37758STrond Myklebust const int max = INT_MAX; 687d6d6dc7cSFred Isaman 688ea2cf228SFred Isaman ret = nfs_scan_commit_list(&cinfo->mds->list, dst, 689ea2cf228SFred Isaman cinfo, max); 690ea2cf228SFred Isaman ret += pnfs_scan_commit_lists(inode, cinfo, max - ret); 691d6d6dc7cSFred Isaman } 692ea2cf228SFred Isaman spin_unlock(cinfo->lock); 693ff778d02STrond Myklebust return ret; 6941da177e4SLinus Torvalds } 695d6d6dc7cSFred Isaman 696c42de9ddSTrond Myklebust #else 697ea2cf228SFred Isaman static unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo) 698fb8a1f11STrond Myklebust { 699fb8a1f11STrond Myklebust return 0; 700fb8a1f11STrond Myklebust } 701fb8a1f11STrond Myklebust 7021763da12SFred Isaman int nfs_scan_commit(struct inode *inode, struct list_head *dst, 703ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 704c42de9ddSTrond Myklebust { 705c42de9ddSTrond Myklebust return 0; 706c42de9ddSTrond Myklebust } 7071da177e4SLinus Torvalds #endif 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds /* 710e7d39069STrond Myklebust * Search for an existing write request, and attempt to update 711e7d39069STrond Myklebust * it to reflect a new dirty region on a given page. 7121da177e4SLinus Torvalds * 713e7d39069STrond Myklebust * If the attempt fails, then the existing request is flushed out 714e7d39069STrond Myklebust * to disk. 7151da177e4SLinus Torvalds */ 716e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode, 717e7d39069STrond Myklebust struct page *page, 718e7d39069STrond Myklebust unsigned int offset, 719e7d39069STrond Myklebust unsigned int bytes) 7201da177e4SLinus Torvalds { 721e7d39069STrond Myklebust struct nfs_page *req; 722e7d39069STrond Myklebust unsigned int rqend; 723e7d39069STrond Myklebust unsigned int end; 7241da177e4SLinus Torvalds int error; 725277459d2STrond Myklebust 726e7d39069STrond Myklebust if (!PagePrivate(page)) 727e7d39069STrond Myklebust return NULL; 728e7d39069STrond Myklebust 729e7d39069STrond Myklebust end = offset + bytes; 730e7d39069STrond Myklebust spin_lock(&inode->i_lock); 731e7d39069STrond Myklebust 732e7d39069STrond Myklebust for (;;) { 733e7d39069STrond Myklebust req = nfs_page_find_request_locked(page); 734e7d39069STrond Myklebust if (req == NULL) 735e7d39069STrond Myklebust goto out_unlock; 736e7d39069STrond Myklebust 737e7d39069STrond Myklebust rqend = req->wb_offset + req->wb_bytes; 738e7d39069STrond Myklebust /* 739e7d39069STrond Myklebust * Tell the caller to flush out the request if 740e7d39069STrond Myklebust * the offsets are non-contiguous. 741e7d39069STrond Myklebust * Note: nfs_flush_incompatible() will already 742e7d39069STrond Myklebust * have flushed out requests having wrong owners. 743e7d39069STrond Myklebust */ 744e468bae9STrond Myklebust if (offset > rqend 745e7d39069STrond Myklebust || end < req->wb_offset) 746e7d39069STrond Myklebust goto out_flushme; 747e7d39069STrond Myklebust 7487ad84aa9STrond Myklebust if (nfs_lock_request(req)) 749e7d39069STrond Myklebust break; 750e7d39069STrond Myklebust 751e7d39069STrond Myklebust /* The request is locked, so wait and then retry */ 752587142f8STrond Myklebust spin_unlock(&inode->i_lock); 7531da177e4SLinus Torvalds error = nfs_wait_on_request(req); 7541da177e4SLinus Torvalds nfs_release_request(req); 755e7d39069STrond Myklebust if (error != 0) 756e7d39069STrond Myklebust goto out_err; 757e7d39069STrond Myklebust spin_lock(&inode->i_lock); 7581da177e4SLinus Torvalds } 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds /* Okay, the request matches. Update the region */ 7611da177e4SLinus Torvalds if (offset < req->wb_offset) { 7621da177e4SLinus Torvalds req->wb_offset = offset; 7631da177e4SLinus Torvalds req->wb_pgbase = offset; 7641da177e4SLinus Torvalds } 7651da177e4SLinus Torvalds if (end > rqend) 7661da177e4SLinus Torvalds req->wb_bytes = end - req->wb_offset; 767e7d39069STrond Myklebust else 768e7d39069STrond Myklebust req->wb_bytes = rqend - req->wb_offset; 769e7d39069STrond Myklebust out_unlock: 770e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 771ca138f36SFred Isaman if (req) 7728dd37758STrond Myklebust nfs_clear_request_commit(req); 773e7d39069STrond Myklebust return req; 774e7d39069STrond Myklebust out_flushme: 775e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 776e7d39069STrond Myklebust nfs_release_request(req); 777e7d39069STrond Myklebust error = nfs_wb_page(inode, page); 778e7d39069STrond Myklebust out_err: 779e7d39069STrond Myklebust return ERR_PTR(error); 780e7d39069STrond Myklebust } 7811da177e4SLinus Torvalds 782e7d39069STrond Myklebust /* 783e7d39069STrond Myklebust * Try to update an existing write request, or create one if there is none. 784e7d39069STrond Myklebust * 785e7d39069STrond Myklebust * Note: Should always be called with the Page Lock held to prevent races 786e7d39069STrond Myklebust * if we have to add a new request. Also assumes that the caller has 787e7d39069STrond Myklebust * already called nfs_flush_incompatible() if necessary. 788e7d39069STrond Myklebust */ 789e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, 790e7d39069STrond Myklebust struct page *page, unsigned int offset, unsigned int bytes) 791e7d39069STrond Myklebust { 792e7d39069STrond Myklebust struct inode *inode = page->mapping->host; 793e7d39069STrond Myklebust struct nfs_page *req; 794e7d39069STrond Myklebust 795e7d39069STrond Myklebust req = nfs_try_to_update_request(inode, page, offset, bytes); 796e7d39069STrond Myklebust if (req != NULL) 797e7d39069STrond Myklebust goto out; 798e7d39069STrond Myklebust req = nfs_create_request(ctx, inode, page, offset, bytes); 799e7d39069STrond Myklebust if (IS_ERR(req)) 800e7d39069STrond Myklebust goto out; 801d6d6dc7cSFred Isaman nfs_inode_add_request(inode, req); 802efc91ed0STrond Myklebust out: 80361e930a9STrond Myklebust return req; 8041da177e4SLinus Torvalds } 8051da177e4SLinus Torvalds 806e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, 807e7d39069STrond Myklebust unsigned int offset, unsigned int count) 808e7d39069STrond Myklebust { 809e7d39069STrond Myklebust struct nfs_page *req; 810e7d39069STrond Myklebust 811e7d39069STrond Myklebust req = nfs_setup_write_request(ctx, page, offset, count); 812e7d39069STrond Myklebust if (IS_ERR(req)) 813e7d39069STrond Myklebust return PTR_ERR(req); 814e7d39069STrond Myklebust /* Update file length */ 815e7d39069STrond Myklebust nfs_grow_file(page, offset, count); 816e7d39069STrond Myklebust nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); 817a6305ddbSTrond Myklebust nfs_mark_request_dirty(req); 8181d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 819e7d39069STrond Myklebust return 0; 820e7d39069STrond Myklebust } 821e7d39069STrond Myklebust 8221da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page) 8231da177e4SLinus Torvalds { 824cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 8251da177e4SLinus Torvalds struct nfs_page *req; 8261a54533eSTrond Myklebust int do_flush, status; 8271da177e4SLinus Torvalds /* 8281da177e4SLinus Torvalds * Look for a request corresponding to this page. If there 8291da177e4SLinus Torvalds * is one, and it belongs to another file, we flush it out 8301da177e4SLinus Torvalds * before we try to copy anything into the page. Do this 8311da177e4SLinus Torvalds * due to the lack of an ACCESS-type call in NFSv2. 8321da177e4SLinus Torvalds * Also do the same if we find a request from an existing 8331da177e4SLinus Torvalds * dropped page. 8341da177e4SLinus Torvalds */ 8351a54533eSTrond Myklebust do { 836277459d2STrond Myklebust req = nfs_page_find_request(page); 8371a54533eSTrond Myklebust if (req == NULL) 8381a54533eSTrond Myklebust return 0; 839f11ac8dbSTrond Myklebust do_flush = req->wb_page != page || req->wb_context != ctx || 840f11ac8dbSTrond Myklebust req->wb_lock_context->lockowner != current->files || 841f11ac8dbSTrond Myklebust req->wb_lock_context->pid != current->tgid; 8421da177e4SLinus Torvalds nfs_release_request(req); 8431a54533eSTrond Myklebust if (!do_flush) 8441a54533eSTrond Myklebust return 0; 845277459d2STrond Myklebust status = nfs_wb_page(page->mapping->host, page); 8461a54533eSTrond Myklebust } while (status == 0); 8471a54533eSTrond Myklebust return status; 8481da177e4SLinus Torvalds } 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds /* 8515d47a356STrond Myklebust * If the page cache is marked as unsafe or invalid, then we can't rely on 8525d47a356STrond Myklebust * the PageUptodate() flag. In this case, we will need to turn off 8535d47a356STrond Myklebust * write optimisations that depend on the page contents being correct. 8545d47a356STrond Myklebust */ 8558d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode) 8565d47a356STrond Myklebust { 8578d197a56STrond Myklebust if (nfs_have_delegated_attributes(inode)) 8588d197a56STrond Myklebust goto out; 8598d197a56STrond Myklebust if (NFS_I(inode)->cache_validity & NFS_INO_REVAL_PAGECACHE) 8608d197a56STrond Myklebust return false; 8618d197a56STrond Myklebust out: 8628d197a56STrond Myklebust return PageUptodate(page) != 0; 8635d47a356STrond Myklebust } 8645d47a356STrond Myklebust 8655d47a356STrond Myklebust /* 8661da177e4SLinus Torvalds * Update and possibly write a cached page of an NFS file. 8671da177e4SLinus Torvalds * 8681da177e4SLinus Torvalds * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad 8691da177e4SLinus Torvalds * things with a page scheduled for an RPC call (e.g. invalidate it). 8701da177e4SLinus Torvalds */ 8711da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page, 8721da177e4SLinus Torvalds unsigned int offset, unsigned int count) 8731da177e4SLinus Torvalds { 874cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 8751da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 8761da177e4SLinus Torvalds int status = 0; 8771da177e4SLinus Torvalds 87891d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 87991d5b470SChuck Lever 88048186c7dSChuck Lever dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n", 88101cce933SJosef "Jeff" Sipek file->f_path.dentry->d_parent->d_name.name, 88201cce933SJosef "Jeff" Sipek file->f_path.dentry->d_name.name, count, 8830bbacc40SChuck Lever (long long)(page_offset(page) + offset)); 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds /* If we're not using byte range locks, and we know the page 8865d47a356STrond Myklebust * is up to date, it may be more efficient to extend the write 8875d47a356STrond Myklebust * to cover the entire page in order to avoid fragmentation 8885d47a356STrond Myklebust * inefficiencies. 8891da177e4SLinus Torvalds */ 8905d47a356STrond Myklebust if (nfs_write_pageuptodate(page, inode) && 8915d47a356STrond Myklebust inode->i_flock == NULL && 8926b2f3d1fSChristoph Hellwig !(file->f_flags & O_DSYNC)) { 89349a70f27STrond Myklebust count = max(count + offset, nfs_page_length(page)); 8941da177e4SLinus Torvalds offset = 0; 8951da177e4SLinus Torvalds } 8961da177e4SLinus Torvalds 897e21195a7STrond Myklebust status = nfs_writepage_setup(ctx, page, offset, count); 89803fa9e84STrond Myklebust if (status < 0) 89903fa9e84STrond Myklebust nfs_set_pageerror(page); 90059b7c05fSTrond Myklebust else 90159b7c05fSTrond Myklebust __set_page_dirty_nobuffers(page); 9021da177e4SLinus Torvalds 90348186c7dSChuck Lever dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", 9041da177e4SLinus Torvalds status, (long long)i_size_read(inode)); 9051da177e4SLinus Torvalds return status; 9061da177e4SLinus Torvalds } 9071da177e4SLinus Torvalds 9083ff7576dSTrond Myklebust static int flush_task_priority(int how) 9091da177e4SLinus Torvalds { 9101da177e4SLinus Torvalds switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { 9111da177e4SLinus Torvalds case FLUSH_HIGHPRI: 9121da177e4SLinus Torvalds return RPC_PRIORITY_HIGH; 9131da177e4SLinus Torvalds case FLUSH_LOWPRI: 9141da177e4SLinus Torvalds return RPC_PRIORITY_LOW; 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds return RPC_PRIORITY_NORMAL; 9171da177e4SLinus Torvalds } 9181da177e4SLinus Torvalds 919c5996c4eSFred Isaman int nfs_initiate_write(struct rpc_clnt *clnt, 920c5996c4eSFred Isaman struct nfs_write_data *data, 921788e7a89STrond Myklebust const struct rpc_call_ops *call_ops, 9229f0ec176SAndy Adamson int how, int flags) 9231da177e4SLinus Torvalds { 924cd841605SFred Isaman struct inode *inode = data->header->inode; 9253ff7576dSTrond Myklebust int priority = flush_task_priority(how); 92607737691STrond Myklebust struct rpc_task *task; 927bdc7f021STrond Myklebust struct rpc_message msg = { 928bdc7f021STrond Myklebust .rpc_argp = &data->args, 929bdc7f021STrond Myklebust .rpc_resp = &data->res, 930cd841605SFred Isaman .rpc_cred = data->header->cred, 931bdc7f021STrond Myklebust }; 93284115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 933d138d5d1SAndy Adamson .rpc_client = clnt, 93407737691STrond Myklebust .task = &data->task, 935bdc7f021STrond Myklebust .rpc_message = &msg, 93684115e1cSTrond Myklebust .callback_ops = call_ops, 93784115e1cSTrond Myklebust .callback_data = data, 938101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 9399f0ec176SAndy Adamson .flags = RPC_TASK_ASYNC | flags, 9403ff7576dSTrond Myklebust .priority = priority, 94184115e1cSTrond Myklebust }; 9422c61be0aSTrond Myklebust int ret = 0; 9431da177e4SLinus Torvalds 944d138d5d1SAndy Adamson /* Set up the initial task struct. */ 945d138d5d1SAndy Adamson NFS_PROTO(inode)->write_setup(data, &msg); 946d138d5d1SAndy Adamson 947d138d5d1SAndy Adamson dprintk("NFS: %5u initiated write call " 948d138d5d1SAndy Adamson "(req %s/%lld, %u bytes @ offset %llu)\n", 949d138d5d1SAndy Adamson data->task.tk_pid, 950d138d5d1SAndy Adamson inode->i_sb->s_id, 951d138d5d1SAndy Adamson (long long)NFS_FILEID(inode), 952d138d5d1SAndy Adamson data->args.count, 953d138d5d1SAndy Adamson (unsigned long long)data->args.offset); 954d138d5d1SAndy Adamson 955d138d5d1SAndy Adamson task = rpc_run_task(&task_setup_data); 956d138d5d1SAndy Adamson if (IS_ERR(task)) { 957d138d5d1SAndy Adamson ret = PTR_ERR(task); 958d138d5d1SAndy Adamson goto out; 959d138d5d1SAndy Adamson } 960d138d5d1SAndy Adamson if (how & FLUSH_SYNC) { 961d138d5d1SAndy Adamson ret = rpc_wait_for_completion_task(task); 962d138d5d1SAndy Adamson if (ret == 0) 963d138d5d1SAndy Adamson ret = task->tk_status; 964d138d5d1SAndy Adamson } 965d138d5d1SAndy Adamson rpc_put_task(task); 966d138d5d1SAndy Adamson out: 967d138d5d1SAndy Adamson return ret; 968d138d5d1SAndy Adamson } 969a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write); 970d138d5d1SAndy Adamson 971d138d5d1SAndy Adamson /* 972d138d5d1SAndy Adamson * Set up the argument/result storage required for the RPC call. 973d138d5d1SAndy Adamson */ 9746c75dc0dSFred Isaman static void nfs_write_rpcsetup(struct nfs_write_data *data, 975d138d5d1SAndy Adamson unsigned int count, unsigned int offset, 976ea2cf228SFred Isaman int how, struct nfs_commit_info *cinfo) 977d138d5d1SAndy Adamson { 9786c75dc0dSFred Isaman struct nfs_page *req = data->header->req; 979d138d5d1SAndy Adamson 9801da177e4SLinus Torvalds /* Set up the RPC argument and reply structs 9811da177e4SLinus Torvalds * NB: take care not to mess about with data->commit et al. */ 9821da177e4SLinus Torvalds 9836c75dc0dSFred Isaman data->args.fh = NFS_FH(data->header->inode); 9841da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 9852bea038cSBoaz Harrosh /* pnfs_set_layoutcommit needs this */ 9862bea038cSBoaz Harrosh data->mds_offset = data->args.offset; 9871da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 98830dd374fSFred Isaman data->args.pages = data->pages.pagevec; 9891da177e4SLinus Torvalds data->args.count = count; 990383ba719STrond Myklebust data->args.context = get_nfs_open_context(req->wb_context); 991f11ac8dbSTrond Myklebust data->args.lock_context = req->wb_lock_context; 992bdc7f021STrond Myklebust data->args.stable = NFS_UNSTABLE; 99387ed5eb4STrond Myklebust switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) { 99487ed5eb4STrond Myklebust case 0: 99587ed5eb4STrond Myklebust break; 99687ed5eb4STrond Myklebust case FLUSH_COND_STABLE: 997ea2cf228SFred Isaman if (nfs_reqs_to_commit(cinfo)) 99887ed5eb4STrond Myklebust break; 99987ed5eb4STrond Myklebust default: 1000bdc7f021STrond Myklebust data->args.stable = NFS_FILE_SYNC; 1001bdc7f021STrond Myklebust } 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds data->res.fattr = &data->fattr; 10041da177e4SLinus Torvalds data->res.count = count; 10051da177e4SLinus Torvalds data->res.verf = &data->verf; 10060e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 10076e4efd56STrond Myklebust } 10081da177e4SLinus Torvalds 10096e4efd56STrond Myklebust static int nfs_do_write(struct nfs_write_data *data, 10106e4efd56STrond Myklebust const struct rpc_call_ops *call_ops, 10116e4efd56STrond Myklebust int how) 10126e4efd56STrond Myklebust { 1013cd841605SFred Isaman struct inode *inode = data->header->inode; 10140382b744SAndy Adamson 10159f0ec176SAndy Adamson return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how, 0); 10161da177e4SLinus Torvalds } 10171da177e4SLinus Torvalds 1018275acaafSTrond Myklebust static int nfs_do_multiple_writes(struct list_head *head, 1019275acaafSTrond Myklebust const struct rpc_call_ops *call_ops, 1020275acaafSTrond Myklebust int how) 1021275acaafSTrond Myklebust { 1022275acaafSTrond Myklebust struct nfs_write_data *data; 1023275acaafSTrond Myklebust int ret = 0; 1024275acaafSTrond Myklebust 1025275acaafSTrond Myklebust while (!list_empty(head)) { 1026275acaafSTrond Myklebust int ret2; 1027275acaafSTrond Myklebust 10286c75dc0dSFred Isaman data = list_first_entry(head, struct nfs_write_data, list); 1029275acaafSTrond Myklebust list_del_init(&data->list); 1030275acaafSTrond Myklebust 1031dce81290STrond Myklebust ret2 = nfs_do_write(data, call_ops, how); 1032275acaafSTrond Myklebust if (ret == 0) 1033275acaafSTrond Myklebust ret = ret2; 1034275acaafSTrond Myklebust } 1035275acaafSTrond Myklebust return ret; 1036275acaafSTrond Myklebust } 1037275acaafSTrond Myklebust 10386d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and 10396d884e8fSFred * call this on each, which will prepare them to be retried on next 10406d884e8fSFred * writeback using standard nfs. 10416d884e8fSFred */ 10426d884e8fSFred static void nfs_redirty_request(struct nfs_page *req) 10436d884e8fSFred { 10446d884e8fSFred nfs_mark_request_dirty(req); 10451d1afcbcSTrond Myklebust nfs_unlock_request(req); 1046d1182b33STrond Myklebust nfs_end_page_writeback(req->wb_page); 10473aff4ebbSTrond Myklebust nfs_release_request(req); 10486d884e8fSFred } 10496d884e8fSFred 1050061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head) 10516c75dc0dSFred Isaman { 10526c75dc0dSFred Isaman struct nfs_page *req; 10536c75dc0dSFred Isaman 10546c75dc0dSFred Isaman while (!list_empty(head)) { 10556c75dc0dSFred Isaman req = nfs_list_entry(head->next); 10566c75dc0dSFred Isaman nfs_list_remove_request(req); 10576c75dc0dSFred Isaman nfs_redirty_request(req); 10586c75dc0dSFred Isaman } 10596c75dc0dSFred Isaman } 10606c75dc0dSFred Isaman 1061061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = { 1062061ae2edSFred Isaman .error_cleanup = nfs_async_write_error, 1063061ae2edSFred Isaman .completion = nfs_write_completion, 1064061ae2edSFred Isaman }; 1065061ae2edSFred Isaman 106625b11dcdSTrond Myklebust static void nfs_flush_error(struct nfs_pageio_descriptor *desc, 106725b11dcdSTrond Myklebust struct nfs_pgio_header *hdr) 106825b11dcdSTrond Myklebust { 106925b11dcdSTrond Myklebust set_bit(NFS_IOHDR_REDO, &hdr->flags); 107025b11dcdSTrond Myklebust while (!list_empty(&hdr->rpc_list)) { 107125b11dcdSTrond Myklebust struct nfs_write_data *data = list_first_entry(&hdr->rpc_list, 107225b11dcdSTrond Myklebust struct nfs_write_data, list); 107325b11dcdSTrond Myklebust list_del(&data->list); 107425b11dcdSTrond Myklebust nfs_writedata_release(data); 107525b11dcdSTrond Myklebust } 107625b11dcdSTrond Myklebust desc->pg_completion_ops->error_cleanup(&desc->pg_list); 107725b11dcdSTrond Myklebust } 107825b11dcdSTrond Myklebust 10791da177e4SLinus Torvalds /* 10801da177e4SLinus Torvalds * Generate multiple small requests to write out a single 10811da177e4SLinus Torvalds * contiguous dirty area on one page. 10821da177e4SLinus Torvalds */ 10836c75dc0dSFred Isaman static int nfs_flush_multi(struct nfs_pageio_descriptor *desc, 10846c75dc0dSFred Isaman struct nfs_pgio_header *hdr) 10851da177e4SLinus Torvalds { 10866c75dc0dSFred Isaman struct nfs_page *req = hdr->req; 10871da177e4SLinus Torvalds struct page *page = req->wb_page; 10881da177e4SLinus Torvalds struct nfs_write_data *data; 1089d097971dSTrond Myklebust size_t wsize = desc->pg_bsize, nbytes; 1090e9f7bee1STrond Myklebust unsigned int offset; 10911da177e4SLinus Torvalds int requests = 0; 1092ea2cf228SFred Isaman struct nfs_commit_info cinfo; 10931da177e4SLinus Torvalds 1094ea2cf228SFred Isaman nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq); 10951da177e4SLinus Torvalds 1096b31268acSTrond Myklebust if ((desc->pg_ioflags & FLUSH_COND_STABLE) && 1097ea2cf228SFred Isaman (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) || 1098b31268acSTrond Myklebust desc->pg_count > wsize)) 1099b31268acSTrond Myklebust desc->pg_ioflags &= ~FLUSH_COND_STABLE; 1100b31268acSTrond Myklebust 1101b31268acSTrond Myklebust 1102275acaafSTrond Myklebust offset = 0; 1103c76069bdSFred Isaman nbytes = desc->pg_count; 1104e9f7bee1STrond Myklebust do { 1105e9f7bee1STrond Myklebust size_t len = min(nbytes, wsize); 1106e9f7bee1STrond Myklebust 11076c75dc0dSFred Isaman data = nfs_writedata_alloc(hdr, 1); 110825b11dcdSTrond Myklebust if (!data) { 110925b11dcdSTrond Myklebust nfs_flush_error(desc, hdr); 111025b11dcdSTrond Myklebust return -ENOMEM; 111125b11dcdSTrond Myklebust } 111230dd374fSFred Isaman data->pages.pagevec[0] = page; 1113ea2cf228SFred Isaman nfs_write_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo); 11146c75dc0dSFred Isaman list_add(&data->list, &hdr->rpc_list); 11151da177e4SLinus Torvalds requests++; 1116e9f7bee1STrond Myklebust nbytes -= len; 1117275acaafSTrond Myklebust offset += len; 1118e9f7bee1STrond Myklebust } while (nbytes != 0); 111925b11dcdSTrond Myklebust nfs_list_remove_request(req); 112025b11dcdSTrond Myklebust nfs_list_add_request(req, &hdr->pages); 11216c75dc0dSFred Isaman desc->pg_rpc_callops = &nfs_write_common_ops; 112225b11dcdSTrond Myklebust return 0; 11231da177e4SLinus Torvalds } 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds /* 11261da177e4SLinus Torvalds * Create an RPC task for the given write request and kick it. 11271da177e4SLinus Torvalds * The page must have been locked by the caller. 11281da177e4SLinus Torvalds * 11291da177e4SLinus Torvalds * It may happen that the page we're passed is not marked dirty. 11301da177e4SLinus Torvalds * This is the case if nfs_updatepage detects a conflicting request 11311da177e4SLinus Torvalds * that has been written but not committed. 11321da177e4SLinus Torvalds */ 11336c75dc0dSFred Isaman static int nfs_flush_one(struct nfs_pageio_descriptor *desc, 11346c75dc0dSFred Isaman struct nfs_pgio_header *hdr) 11351da177e4SLinus Torvalds { 11361da177e4SLinus Torvalds struct nfs_page *req; 11371da177e4SLinus Torvalds struct page **pages; 11381da177e4SLinus Torvalds struct nfs_write_data *data; 1139c76069bdSFred Isaman struct list_head *head = &desc->pg_list; 1140ea2cf228SFred Isaman struct nfs_commit_info cinfo; 11411da177e4SLinus Torvalds 11426c75dc0dSFred Isaman data = nfs_writedata_alloc(hdr, nfs_page_array_len(desc->pg_base, 1143c76069bdSFred Isaman desc->pg_count)); 11446c75dc0dSFred Isaman if (!data) { 114525b11dcdSTrond Myklebust nfs_flush_error(desc, hdr); 114625b11dcdSTrond Myklebust return -ENOMEM; 114744b83799SFred Isaman } 11486c75dc0dSFred Isaman 1149ea2cf228SFred Isaman nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq); 115030dd374fSFred Isaman pages = data->pages.pagevec; 11511da177e4SLinus Torvalds while (!list_empty(head)) { 11521da177e4SLinus Torvalds req = nfs_list_entry(head->next); 11531da177e4SLinus Torvalds nfs_list_remove_request(req); 11546c75dc0dSFred Isaman nfs_list_add_request(req, &hdr->pages); 11551da177e4SLinus Torvalds *pages++ = req->wb_page; 11561da177e4SLinus Torvalds } 11571da177e4SLinus Torvalds 1158b31268acSTrond Myklebust if ((desc->pg_ioflags & FLUSH_COND_STABLE) && 1159ea2cf228SFred Isaman (desc->pg_moreio || nfs_reqs_to_commit(&cinfo))) 1160b31268acSTrond Myklebust desc->pg_ioflags &= ~FLUSH_COND_STABLE; 1161b31268acSTrond Myklebust 11621da177e4SLinus Torvalds /* Set up the argument struct */ 1163ea2cf228SFred Isaman nfs_write_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo); 11646c75dc0dSFred Isaman list_add(&data->list, &hdr->rpc_list); 11656c75dc0dSFred Isaman desc->pg_rpc_callops = &nfs_write_common_ops; 116625b11dcdSTrond Myklebust return 0; 11671da177e4SLinus Torvalds } 11681da177e4SLinus Torvalds 11696c75dc0dSFred Isaman int nfs_generic_flush(struct nfs_pageio_descriptor *desc, 11706c75dc0dSFred Isaman struct nfs_pgio_header *hdr) 1171dce81290STrond Myklebust { 1172dce81290STrond Myklebust if (desc->pg_bsize < PAGE_CACHE_SIZE) 11736c75dc0dSFred Isaman return nfs_flush_multi(desc, hdr); 11746c75dc0dSFred Isaman return nfs_flush_one(desc, hdr); 1175dce81290STrond Myklebust } 1176dce81290STrond Myklebust 1177dce81290STrond Myklebust static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc) 11781751c363STrond Myklebust { 11796c75dc0dSFred Isaman struct nfs_write_header *whdr; 11806c75dc0dSFred Isaman struct nfs_pgio_header *hdr; 1181275acaafSTrond Myklebust int ret; 1182275acaafSTrond Myklebust 11836c75dc0dSFred Isaman whdr = nfs_writehdr_alloc(); 11846c75dc0dSFred Isaman if (!whdr) { 11859b5415b5STrond Myklebust desc->pg_completion_ops->error_cleanup(&desc->pg_list); 11866c75dc0dSFred Isaman return -ENOMEM; 11876c75dc0dSFred Isaman } 11886c75dc0dSFred Isaman hdr = &whdr->header; 11896c75dc0dSFred Isaman nfs_pgheader_init(desc, hdr, nfs_writehdr_free); 11906c75dc0dSFred Isaman atomic_inc(&hdr->refcnt); 11916c75dc0dSFred Isaman ret = nfs_generic_flush(desc, hdr); 1192275acaafSTrond Myklebust if (ret == 0) 11936c75dc0dSFred Isaman ret = nfs_do_multiple_writes(&hdr->rpc_list, 11946c75dc0dSFred Isaman desc->pg_rpc_callops, 1195dce81290STrond Myklebust desc->pg_ioflags); 11966c75dc0dSFred Isaman if (atomic_dec_and_test(&hdr->refcnt)) 1197061ae2edSFred Isaman hdr->completion_ops->completion(hdr); 1198275acaafSTrond Myklebust return ret; 11991751c363STrond Myklebust } 12001751c363STrond Myklebust 12011751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_write_ops = { 12021751c363STrond Myklebust .pg_test = nfs_generic_pg_test, 12031751c363STrond Myklebust .pg_doio = nfs_generic_pg_writepages, 12041751c363STrond Myklebust }; 12051751c363STrond Myklebust 120657208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, 1207061ae2edSFred Isaman struct inode *inode, int ioflags, 1208061ae2edSFred Isaman const struct nfs_pgio_completion_ops *compl_ops) 12091751c363STrond Myklebust { 1210061ae2edSFred Isaman nfs_pageio_init(pgio, inode, &nfs_pageio_write_ops, compl_ops, 12111751c363STrond Myklebust NFS_SERVER(inode)->wsize, ioflags); 12121751c363STrond Myklebust } 12131751c363STrond Myklebust 1214dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio) 1215dce81290STrond Myklebust { 1216dce81290STrond Myklebust pgio->pg_ops = &nfs_pageio_write_ops; 1217dce81290STrond Myklebust pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize; 1218dce81290STrond Myklebust } 12191f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds); 1220dce81290STrond Myklebust 12211da177e4SLinus Torvalds 1222def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata) 1223def6ed7eSAndy Adamson { 1224def6ed7eSAndy Adamson struct nfs_write_data *data = calldata; 1225cd841605SFred Isaman NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data); 1226def6ed7eSAndy Adamson } 1227def6ed7eSAndy Adamson 12280b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata) 12290b7c0153SFred Isaman { 12300b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 12310b7c0153SFred Isaman 12320b7c0153SFred Isaman NFS_PROTO(data->inode)->commit_rpc_prepare(task, data); 12330b7c0153SFred Isaman } 12340b7c0153SFred Isaman 12351da177e4SLinus Torvalds /* 12361da177e4SLinus Torvalds * Handle a write reply that flushes a whole page. 12371da177e4SLinus Torvalds * 12381da177e4SLinus Torvalds * FIXME: There is an inherent race with invalidate_inode_pages and 12391da177e4SLinus Torvalds * writebacks since the page->count is kept > 1 for as long 12401da177e4SLinus Torvalds * as the page has a write request pending. 12411da177e4SLinus Torvalds */ 12426c75dc0dSFred Isaman static void nfs_writeback_done_common(struct rpc_task *task, void *calldata) 12431da177e4SLinus Torvalds { 1244788e7a89STrond Myklebust struct nfs_write_data *data = calldata; 12451da177e4SLinus Torvalds 1246c9d8f89dSTrond Myklebust nfs_writeback_done(task, data); 1247c9d8f89dSTrond Myklebust } 1248c9d8f89dSTrond Myklebust 12496c75dc0dSFred Isaman static void nfs_writeback_release_common(void *calldata) 1250c9d8f89dSTrond Myklebust { 1251c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1252cd841605SFred Isaman struct nfs_pgio_header *hdr = data->header; 1253e2fecb21STrond Myklebust int status = data->task.tk_status; 1254788e7a89STrond Myklebust 12556c75dc0dSFred Isaman if ((status >= 0) && nfs_write_need_commit(data)) { 12566c75dc0dSFred Isaman spin_lock(&hdr->lock); 12576c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) 12586c75dc0dSFred Isaman ; /* Do nothing */ 12596c75dc0dSFred Isaman else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) 12609bce008bSTrond Myklebust memcpy(hdr->verf, &data->verf, sizeof(*hdr->verf)); 12619bce008bSTrond Myklebust else if (memcmp(hdr->verf, &data->verf, sizeof(*hdr->verf))) 12626c75dc0dSFred Isaman set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags); 12636c75dc0dSFred Isaman spin_unlock(&hdr->lock); 12641da177e4SLinus Torvalds } 1265cd841605SFred Isaman nfs_writedata_release(data); 12661da177e4SLinus Torvalds } 12671da177e4SLinus Torvalds 12686c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops = { 1269def6ed7eSAndy Adamson .rpc_call_prepare = nfs_write_prepare, 12706c75dc0dSFred Isaman .rpc_call_done = nfs_writeback_done_common, 12716c75dc0dSFred Isaman .rpc_release = nfs_writeback_release_common, 1272788e7a89STrond Myklebust }; 1273788e7a89STrond Myklebust 1274788e7a89STrond Myklebust 12751da177e4SLinus Torvalds /* 12761da177e4SLinus Torvalds * This function is called when the WRITE call is complete. 12771da177e4SLinus Torvalds */ 127813602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data) 12791da177e4SLinus Torvalds { 12801da177e4SLinus Torvalds struct nfs_writeargs *argp = &data->args; 12811da177e4SLinus Torvalds struct nfs_writeres *resp = &data->res; 1282cd841605SFred Isaman struct inode *inode = data->header->inode; 1283788e7a89STrond Myklebust int status; 12841da177e4SLinus Torvalds 1285a3f565b1SChuck Lever dprintk("NFS: %5u nfs_writeback_done (status %d)\n", 12861da177e4SLinus Torvalds task->tk_pid, task->tk_status); 12871da177e4SLinus Torvalds 1288f551e44fSChuck Lever /* 1289f551e44fSChuck Lever * ->write_done will attempt to use post-op attributes to detect 1290f551e44fSChuck Lever * conflicting writes by other clients. A strict interpretation 1291f551e44fSChuck Lever * of close-to-open would allow us to continue caching even if 1292f551e44fSChuck Lever * another writer had changed the file, but some applications 1293f551e44fSChuck Lever * depend on tighter cache coherency when writing. 1294f551e44fSChuck Lever */ 1295cd841605SFred Isaman status = NFS_PROTO(inode)->write_done(task, data); 1296788e7a89STrond Myklebust if (status != 0) 129713602896SFred Isaman return; 1298cd841605SFred Isaman nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count); 129991d5b470SChuck Lever 13001da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 13011da177e4SLinus Torvalds if (resp->verf->committed < argp->stable && task->tk_status >= 0) { 13021da177e4SLinus Torvalds /* We tried a write call, but the server did not 13031da177e4SLinus Torvalds * commit data to stable storage even though we 13041da177e4SLinus Torvalds * requested it. 13051da177e4SLinus Torvalds * Note: There is a known bug in Tru64 < 5.0 in which 13061da177e4SLinus Torvalds * the server reports NFS_DATA_SYNC, but performs 13071da177e4SLinus Torvalds * NFS_FILE_SYNC. We therefore implement this checking 13081da177e4SLinus Torvalds * as a dprintk() in order to avoid filling syslog. 13091da177e4SLinus Torvalds */ 13101da177e4SLinus Torvalds static unsigned long complain; 13111da177e4SLinus Torvalds 1312a69aef14SFred Isaman /* Note this will print the MDS for a DS write */ 13131da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 13141da177e4SLinus Torvalds dprintk("NFS: faulty NFS server %s:" 13151da177e4SLinus Torvalds " (committed = %d) != (stable = %d)\n", 1316cd841605SFred Isaman NFS_SERVER(inode)->nfs_client->cl_hostname, 13171da177e4SLinus Torvalds resp->verf->committed, argp->stable); 13181da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 13191da177e4SLinus Torvalds } 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds #endif 13226c75dc0dSFred Isaman if (task->tk_status < 0) 13236c75dc0dSFred Isaman nfs_set_pgio_error(data->header, task->tk_status, argp->offset); 13246c75dc0dSFred Isaman else if (resp->count < argp->count) { 13251da177e4SLinus Torvalds static unsigned long complain; 13261da177e4SLinus Torvalds 13276c75dc0dSFred Isaman /* This a short write! */ 1328cd841605SFred Isaman nfs_inc_stats(inode, NFSIOS_SHORTWRITE); 132991d5b470SChuck Lever 13301da177e4SLinus Torvalds /* Has the server at least made some progress? */ 13316c75dc0dSFred Isaman if (resp->count == 0) { 13326c75dc0dSFred Isaman if (time_before(complain, jiffies)) { 13336c75dc0dSFred Isaman printk(KERN_WARNING 13346c75dc0dSFred Isaman "NFS: Server wrote zero bytes, expected %u.\n", 13356c75dc0dSFred Isaman argp->count); 13366c75dc0dSFred Isaman complain = jiffies + 300 * HZ; 13376c75dc0dSFred Isaman } 13386c75dc0dSFred Isaman nfs_set_pgio_error(data->header, -EIO, argp->offset); 13396c75dc0dSFred Isaman task->tk_status = -EIO; 13406c75dc0dSFred Isaman return; 13416c75dc0dSFred Isaman } 13421da177e4SLinus Torvalds /* Was this an NFSv2 write or an NFSv3 stable write? */ 13431da177e4SLinus Torvalds if (resp->verf->committed != NFS_UNSTABLE) { 13441da177e4SLinus Torvalds /* Resend from where the server left off */ 1345a69aef14SFred Isaman data->mds_offset += resp->count; 13461da177e4SLinus Torvalds argp->offset += resp->count; 13471da177e4SLinus Torvalds argp->pgbase += resp->count; 13481da177e4SLinus Torvalds argp->count -= resp->count; 13491da177e4SLinus Torvalds } else { 13501da177e4SLinus Torvalds /* Resend as a stable write in order to avoid 13511da177e4SLinus Torvalds * headaches in the case of a server crash. 13521da177e4SLinus Torvalds */ 13531da177e4SLinus Torvalds argp->stable = NFS_FILE_SYNC; 13541da177e4SLinus Torvalds } 1355d00c5d43STrond Myklebust rpc_restart_call_prepare(task); 13561da177e4SLinus Torvalds } 13571da177e4SLinus Torvalds } 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 136171d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait) 136271d0a611STrond Myklebust { 1363b8413f98STrond Myklebust int ret; 1364b8413f98STrond Myklebust 136571d0a611STrond Myklebust if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags)) 136671d0a611STrond Myklebust return 1; 1367b8413f98STrond Myklebust if (!may_wait) 136871d0a611STrond Myklebust return 0; 1369b8413f98STrond Myklebust ret = out_of_line_wait_on_bit_lock(&nfsi->flags, 1370b8413f98STrond Myklebust NFS_INO_COMMIT, 1371b8413f98STrond Myklebust nfs_wait_bit_killable, 1372b8413f98STrond Myklebust TASK_KILLABLE); 1373b8413f98STrond Myklebust return (ret < 0) ? ret : 1; 137471d0a611STrond Myklebust } 137571d0a611STrond Myklebust 1376f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi) 137771d0a611STrond Myklebust { 137871d0a611STrond Myklebust clear_bit(NFS_INO_COMMIT, &nfsi->flags); 137971d0a611STrond Myklebust smp_mb__after_clear_bit(); 138071d0a611STrond Myklebust wake_up_bit(&nfsi->flags, NFS_INO_COMMIT); 138171d0a611STrond Myklebust } 138271d0a611STrond Myklebust 13830b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data) 13841da177e4SLinus Torvalds { 13850b7c0153SFred Isaman put_nfs_open_context(data->context); 13860b7c0153SFred Isaman nfs_commit_free(data); 13871da177e4SLinus Torvalds } 1388e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release); 13891da177e4SLinus Torvalds 13900b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data, 13919ace33cdSFred Isaman const struct rpc_call_ops *call_ops, 13929f0ec176SAndy Adamson int how, int flags) 13931da177e4SLinus Torvalds { 139407737691STrond Myklebust struct rpc_task *task; 13959ace33cdSFred Isaman int priority = flush_task_priority(how); 1396bdc7f021STrond Myklebust struct rpc_message msg = { 1397bdc7f021STrond Myklebust .rpc_argp = &data->args, 1398bdc7f021STrond Myklebust .rpc_resp = &data->res, 13999ace33cdSFred Isaman .rpc_cred = data->cred, 1400bdc7f021STrond Myklebust }; 140184115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 140207737691STrond Myklebust .task = &data->task, 14039ace33cdSFred Isaman .rpc_client = clnt, 1404bdc7f021STrond Myklebust .rpc_message = &msg, 14059ace33cdSFred Isaman .callback_ops = call_ops, 140684115e1cSTrond Myklebust .callback_data = data, 1407101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 14089f0ec176SAndy Adamson .flags = RPC_TASK_ASYNC | flags, 14093ff7576dSTrond Myklebust .priority = priority, 141084115e1cSTrond Myklebust }; 1411788e7a89STrond Myklebust /* Set up the initial task struct. */ 14129ace33cdSFred Isaman NFS_PROTO(data->inode)->commit_setup(data, &msg); 14131da177e4SLinus Torvalds 1414a3f565b1SChuck Lever dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); 1415bdc7f021STrond Myklebust 141607737691STrond Myklebust task = rpc_run_task(&task_setup_data); 1417dbae4c73STrond Myklebust if (IS_ERR(task)) 1418dbae4c73STrond Myklebust return PTR_ERR(task); 1419d2224e7aSJeff Layton if (how & FLUSH_SYNC) 1420d2224e7aSJeff Layton rpc_wait_for_completion_task(task); 142107737691STrond Myklebust rpc_put_task(task); 1422dbae4c73STrond Myklebust return 0; 14231da177e4SLinus Torvalds } 1424e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit); 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds /* 14279ace33cdSFred Isaman * Set up the argument/result storage required for the RPC call. 14289ace33cdSFred Isaman */ 14290b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data, 1430988b6dceSFred Isaman struct list_head *head, 1431f453a54aSFred Isaman struct pnfs_layout_segment *lseg, 1432f453a54aSFred Isaman struct nfs_commit_info *cinfo) 14339ace33cdSFred Isaman { 14349ace33cdSFred Isaman struct nfs_page *first = nfs_list_entry(head->next); 14353d4ff43dSAl Viro struct inode *inode = first->wb_context->dentry->d_inode; 14369ace33cdSFred Isaman 14379ace33cdSFred Isaman /* Set up the RPC argument and reply structs 14389ace33cdSFred Isaman * NB: take care not to mess about with data->commit et al. */ 14399ace33cdSFred Isaman 14409ace33cdSFred Isaman list_splice_init(head, &data->pages); 14419ace33cdSFred Isaman 14429ace33cdSFred Isaman data->inode = inode; 14439ace33cdSFred Isaman data->cred = first->wb_context->cred; 1444988b6dceSFred Isaman data->lseg = lseg; /* reference transferred */ 14459ace33cdSFred Isaman data->mds_ops = &nfs_commit_ops; 1446f453a54aSFred Isaman data->completion_ops = cinfo->completion_ops; 1447b359f9d0SFred Isaman data->dreq = cinfo->dreq; 14489ace33cdSFred Isaman 14499ace33cdSFred Isaman data->args.fh = NFS_FH(data->inode); 14509ace33cdSFred Isaman /* Note: we always request a commit of the entire inode */ 14519ace33cdSFred Isaman data->args.offset = 0; 14529ace33cdSFred Isaman data->args.count = 0; 14530b7c0153SFred Isaman data->context = get_nfs_open_context(first->wb_context); 14549ace33cdSFred Isaman data->res.fattr = &data->fattr; 14559ace33cdSFred Isaman data->res.verf = &data->verf; 14569ace33cdSFred Isaman nfs_fattr_init(&data->fattr); 14579ace33cdSFred Isaman } 1458e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit); 14599ace33cdSFred Isaman 1460e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list, 1461ea2cf228SFred Isaman struct pnfs_layout_segment *lseg, 1462ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 146364bfeb49SFred Isaman { 146464bfeb49SFred Isaman struct nfs_page *req; 146564bfeb49SFred Isaman 146664bfeb49SFred Isaman while (!list_empty(page_list)) { 146764bfeb49SFred Isaman req = nfs_list_entry(page_list->next); 146864bfeb49SFred Isaman nfs_list_remove_request(req); 1469ea2cf228SFred Isaman nfs_mark_request_commit(req, lseg, cinfo); 147056f9cd68SFred Isaman if (!cinfo->dreq) { 147164bfeb49SFred Isaman dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 147264bfeb49SFred Isaman dec_bdi_stat(req->wb_page->mapping->backing_dev_info, 147364bfeb49SFred Isaman BDI_RECLAIMABLE); 147456f9cd68SFred Isaman } 14751d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 147664bfeb49SFred Isaman } 147764bfeb49SFred Isaman } 1478e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit); 147964bfeb49SFred Isaman 14809ace33cdSFred Isaman /* 14811da177e4SLinus Torvalds * Commit dirty pages 14821da177e4SLinus Torvalds */ 14831da177e4SLinus Torvalds static int 1484ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how, 1485ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 14861da177e4SLinus Torvalds { 14870b7c0153SFred Isaman struct nfs_commit_data *data; 14881da177e4SLinus Torvalds 1489c9d8f89dSTrond Myklebust data = nfs_commitdata_alloc(); 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds if (!data) 14921da177e4SLinus Torvalds goto out_bad; 14931da177e4SLinus Torvalds 14941da177e4SLinus Torvalds /* Set up the argument struct */ 1495f453a54aSFred Isaman nfs_init_commit(data, head, NULL, cinfo); 1496f453a54aSFred Isaman atomic_inc(&cinfo->mds->rpcs_out); 14979f0ec176SAndy Adamson return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops, 14989f0ec176SAndy Adamson how, 0); 14991da177e4SLinus Torvalds out_bad: 1500ea2cf228SFred Isaman nfs_retry_commit(head, NULL, cinfo); 1501f453a54aSFred Isaman cinfo->completion_ops->error_cleanup(NFS_I(inode)); 15021da177e4SLinus Torvalds return -ENOMEM; 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds /* 15061da177e4SLinus Torvalds * COMMIT call returned 15071da177e4SLinus Torvalds */ 1508788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata) 15091da177e4SLinus Torvalds { 15100b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 15111da177e4SLinus Torvalds 1512a3f565b1SChuck Lever dprintk("NFS: %5u nfs_commit_done (status %d)\n", 15131da177e4SLinus Torvalds task->tk_pid, task->tk_status); 15141da177e4SLinus Torvalds 1515788e7a89STrond Myklebust /* Call the NFS version-specific code */ 1516c0d0e96bSTrond Myklebust NFS_PROTO(data->inode)->commit_done(task, data); 1517c9d8f89dSTrond Myklebust } 1518c9d8f89dSTrond Myklebust 1519f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data) 1520c9d8f89dSTrond Myklebust { 1521c9d8f89dSTrond Myklebust struct nfs_page *req; 1522c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1523f453a54aSFred Isaman struct nfs_commit_info cinfo; 1524788e7a89STrond Myklebust 15251da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 15261da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 15271da177e4SLinus Torvalds nfs_list_remove_request(req); 1528d6d6dc7cSFred Isaman nfs_clear_page_commit(req->wb_page); 15291da177e4SLinus Torvalds 153048186c7dSChuck Lever dprintk("NFS: commit (%s/%lld %d@%lld)", 15313d4ff43dSAl Viro req->wb_context->dentry->d_sb->s_id, 15323d4ff43dSAl Viro (long long)NFS_FILEID(req->wb_context->dentry->d_inode), 15331da177e4SLinus Torvalds req->wb_bytes, 15341da177e4SLinus Torvalds (long long)req_offset(req)); 1535c9d8f89dSTrond Myklebust if (status < 0) { 1536c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 15371da177e4SLinus Torvalds nfs_inode_remove_request(req); 1538c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 15391da177e4SLinus Torvalds goto next; 15401da177e4SLinus Torvalds } 15411da177e4SLinus Torvalds 15421da177e4SLinus Torvalds /* Okay, COMMIT succeeded, apparently. Check the verifier 15431da177e4SLinus Torvalds * returned by the server against all stored verfs. */ 15442f2c63bcSTrond Myklebust if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) { 15451da177e4SLinus Torvalds /* We have a match */ 15461da177e4SLinus Torvalds nfs_inode_remove_request(req); 15471da177e4SLinus Torvalds dprintk(" OK\n"); 15481da177e4SLinus Torvalds goto next; 15491da177e4SLinus Torvalds } 15501da177e4SLinus Torvalds /* We have a mismatch. Write the page again */ 15511da177e4SLinus Torvalds dprintk(" mismatch\n"); 15526d884e8fSFred nfs_mark_request_dirty(req); 15531da177e4SLinus Torvalds next: 15541d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 15551da177e4SLinus Torvalds } 1556f453a54aSFred Isaman nfs_init_cinfo(&cinfo, data->inode, data->dreq); 1557f453a54aSFred Isaman if (atomic_dec_and_test(&cinfo.mds->rpcs_out)) 1558f453a54aSFred Isaman nfs_commit_clear_lock(NFS_I(data->inode)); 15595917ce84SFred Isaman } 15605917ce84SFred Isaman 15615917ce84SFred Isaman static void nfs_commit_release(void *calldata) 15625917ce84SFred Isaman { 15630b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 15645917ce84SFred Isaman 1565f453a54aSFred Isaman data->completion_ops->completion(data); 1566c9d8f89dSTrond Myklebust nfs_commitdata_release(calldata); 15671da177e4SLinus Torvalds } 1568788e7a89STrond Myklebust 1569788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = { 15700b7c0153SFred Isaman .rpc_call_prepare = nfs_commit_prepare, 1571788e7a89STrond Myklebust .rpc_call_done = nfs_commit_done, 1572788e7a89STrond Myklebust .rpc_release = nfs_commit_release, 1573788e7a89STrond Myklebust }; 15741da177e4SLinus Torvalds 1575f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = { 1576f453a54aSFred Isaman .completion = nfs_commit_release_pages, 1577f453a54aSFred Isaman .error_cleanup = nfs_commit_clear_lock, 1578f453a54aSFred Isaman }; 1579f453a54aSFred Isaman 15801763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head, 1581ea2cf228SFred Isaman int how, struct nfs_commit_info *cinfo) 158284c53ab5SFred Isaman { 158384c53ab5SFred Isaman int status; 158484c53ab5SFred Isaman 1585ea2cf228SFred Isaman status = pnfs_commit_list(inode, head, how, cinfo); 158684c53ab5SFred Isaman if (status == PNFS_NOT_ATTEMPTED) 1587ea2cf228SFred Isaman status = nfs_commit_list(inode, head, how, cinfo); 158884c53ab5SFred Isaman return status; 158984c53ab5SFred Isaman } 159084c53ab5SFred Isaman 1591b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how) 15921da177e4SLinus Torvalds { 15931da177e4SLinus Torvalds LIST_HEAD(head); 1594ea2cf228SFred Isaman struct nfs_commit_info cinfo; 159571d0a611STrond Myklebust int may_wait = how & FLUSH_SYNC; 1596b8413f98STrond Myklebust int res; 15971da177e4SLinus Torvalds 1598b8413f98STrond Myklebust res = nfs_commit_set_lock(NFS_I(inode), may_wait); 1599b8413f98STrond Myklebust if (res <= 0) 1600c5efa5fcSTrond Myklebust goto out_mark_dirty; 1601ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, inode); 1602ea2cf228SFred Isaman res = nfs_scan_commit(inode, &head, &cinfo); 16031da177e4SLinus Torvalds if (res) { 1604a861a1e1SFred Isaman int error; 1605a861a1e1SFred Isaman 1606ea2cf228SFred Isaman error = nfs_generic_commit_list(inode, &head, how, &cinfo); 16071da177e4SLinus Torvalds if (error < 0) 16081da177e4SLinus Torvalds return error; 1609b8413f98STrond Myklebust if (!may_wait) 1610b8413f98STrond Myklebust goto out_mark_dirty; 1611b8413f98STrond Myklebust error = wait_on_bit(&NFS_I(inode)->flags, 1612b8413f98STrond Myklebust NFS_INO_COMMIT, 161371d0a611STrond Myklebust nfs_wait_bit_killable, 161471d0a611STrond Myklebust TASK_KILLABLE); 1615b8413f98STrond Myklebust if (error < 0) 1616b8413f98STrond Myklebust return error; 161771d0a611STrond Myklebust } else 161871d0a611STrond Myklebust nfs_commit_clear_lock(NFS_I(inode)); 1619c5efa5fcSTrond Myklebust return res; 1620c5efa5fcSTrond Myklebust /* Note: If we exit without ensuring that the commit is complete, 1621c5efa5fcSTrond Myklebust * we must mark the inode as dirty. Otherwise, future calls to 1622c5efa5fcSTrond Myklebust * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure 1623c5efa5fcSTrond Myklebust * that the data is on the disk. 1624c5efa5fcSTrond Myklebust */ 1625c5efa5fcSTrond Myklebust out_mark_dirty: 1626c5efa5fcSTrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 16271da177e4SLinus Torvalds return res; 16281da177e4SLinus Torvalds } 16298fc795f7STrond Myklebust 16308fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 16318fc795f7STrond Myklebust { 1632420e3646STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 1633420e3646STrond Myklebust int flags = FLUSH_SYNC; 1634420e3646STrond Myklebust int ret = 0; 16358fc795f7STrond Myklebust 16363236c3e1SJeff Layton /* no commits means nothing needs to be done */ 1637ea2cf228SFred Isaman if (!nfsi->commit_info.ncommit) 16383236c3e1SJeff Layton return ret; 16393236c3e1SJeff Layton 1640a00dd6c0SJeff Layton if (wbc->sync_mode == WB_SYNC_NONE) { 1641a00dd6c0SJeff Layton /* Don't commit yet if this is a non-blocking flush and there 1642a00dd6c0SJeff Layton * are a lot of outstanding writes for this mapping. 1643420e3646STrond Myklebust */ 1644ea2cf228SFred Isaman if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1)) 1645420e3646STrond Myklebust goto out_mark_dirty; 1646420e3646STrond Myklebust 1647a00dd6c0SJeff Layton /* don't wait for the COMMIT response */ 1648420e3646STrond Myklebust flags = 0; 1649a00dd6c0SJeff Layton } 1650a00dd6c0SJeff Layton 1651420e3646STrond Myklebust ret = nfs_commit_inode(inode, flags); 1652420e3646STrond Myklebust if (ret >= 0) { 1653420e3646STrond Myklebust if (wbc->sync_mode == WB_SYNC_NONE) { 1654420e3646STrond Myklebust if (ret < wbc->nr_to_write) 1655420e3646STrond Myklebust wbc->nr_to_write -= ret; 1656420e3646STrond Myklebust else 1657420e3646STrond Myklebust wbc->nr_to_write = 0; 1658420e3646STrond Myklebust } 16598fc795f7STrond Myklebust return 0; 1660420e3646STrond Myklebust } 1661420e3646STrond Myklebust out_mark_dirty: 16628fc795f7STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 16638fc795f7STrond Myklebust return ret; 16648fc795f7STrond Myklebust } 1665c63c7b05STrond Myklebust #else 16668fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 16678fc795f7STrond Myklebust { 16688fc795f7STrond Myklebust return 0; 16698fc795f7STrond Myklebust } 16701da177e4SLinus Torvalds #endif 16711da177e4SLinus Torvalds 16728fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc) 16738fc795f7STrond Myklebust { 1674*a8d8f02cSBryan Schumaker return nfs_commit_unstable_pages(inode, wbc); 1675*a8d8f02cSBryan Schumaker } 1676863a3c6cSAndy Adamson 1677*a8d8f02cSBryan Schumaker #ifdef CONFIG_NFS_V4 1678*a8d8f02cSBryan Schumaker int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc) 1679*a8d8f02cSBryan Schumaker { 1680*a8d8f02cSBryan Schumaker int ret = nfs_write_inode(inode, wbc); 1681*a8d8f02cSBryan Schumaker 1682863a3c6cSAndy Adamson if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) { 1683ef311537SAndy Adamson int status; 1684ef311537SAndy Adamson bool sync = true; 1685863a3c6cSAndy Adamson 1686846d5a09SWu Fengguang if (wbc->sync_mode == WB_SYNC_NONE) 1687ef311537SAndy Adamson sync = false; 1688863a3c6cSAndy Adamson 1689863a3c6cSAndy Adamson status = pnfs_layoutcommit_inode(inode, sync); 1690863a3c6cSAndy Adamson if (status < 0) 1691863a3c6cSAndy Adamson return status; 1692863a3c6cSAndy Adamson } 1693863a3c6cSAndy Adamson return ret; 16948fc795f7STrond Myklebust } 1695*a8d8f02cSBryan Schumaker #endif 16968fc795f7STrond Myklebust 1697acdc53b2STrond Myklebust /* 1698acdc53b2STrond Myklebust * flush the inode to disk. 1699acdc53b2STrond Myklebust */ 1700acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode) 170134901f70STrond Myklebust { 170234901f70STrond Myklebust struct writeback_control wbc = { 170372cb77f4STrond Myklebust .sync_mode = WB_SYNC_ALL, 170434901f70STrond Myklebust .nr_to_write = LONG_MAX, 1705d7fb1207STrond Myklebust .range_start = 0, 1706d7fb1207STrond Myklebust .range_end = LLONG_MAX, 170734901f70STrond Myklebust }; 170834901f70STrond Myklebust 1709acdc53b2STrond Myklebust return sync_inode(inode, &wbc); 17101c75950bSTrond Myklebust } 17111c75950bSTrond Myklebust 17121b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page) 17131b3b4a1aSTrond Myklebust { 17141b3b4a1aSTrond Myklebust struct nfs_page *req; 17151b3b4a1aSTrond Myklebust int ret = 0; 17161b3b4a1aSTrond Myklebust 17171b3b4a1aSTrond Myklebust BUG_ON(!PageLocked(page)); 17181b3b4a1aSTrond Myklebust for (;;) { 1719ba8b06e6STrond Myklebust wait_on_page_writeback(page); 17201b3b4a1aSTrond Myklebust req = nfs_page_find_request(page); 17211b3b4a1aSTrond Myklebust if (req == NULL) 17221b3b4a1aSTrond Myklebust break; 17237ad84aa9STrond Myklebust if (nfs_lock_request(req)) { 17248dd37758STrond Myklebust nfs_clear_request_commit(req); 17251b3b4a1aSTrond Myklebust nfs_inode_remove_request(req); 17261b3b4a1aSTrond Myklebust /* 17271b3b4a1aSTrond Myklebust * In case nfs_inode_remove_request has marked the 17281b3b4a1aSTrond Myklebust * page as being dirty 17291b3b4a1aSTrond Myklebust */ 17301b3b4a1aSTrond Myklebust cancel_dirty_page(page, PAGE_CACHE_SIZE); 17311d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 17321b3b4a1aSTrond Myklebust break; 17331b3b4a1aSTrond Myklebust } 17341b3b4a1aSTrond Myklebust ret = nfs_wait_on_request(req); 1735c9edda71STrond Myklebust nfs_release_request(req); 17361b3b4a1aSTrond Myklebust if (ret < 0) 1737c988950eSTrond Myklebust break; 17381b3b4a1aSTrond Myklebust } 17391b3b4a1aSTrond Myklebust return ret; 17401b3b4a1aSTrond Myklebust } 17411b3b4a1aSTrond Myklebust 17421c75950bSTrond Myklebust /* 17431c75950bSTrond Myklebust * Write back all requests on one page - we do this before reading it. 17441c75950bSTrond Myklebust */ 17451c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page) 17461c75950bSTrond Myklebust { 17477f2f12d9STrond Myklebust loff_t range_start = page_offset(page); 17487f2f12d9STrond Myklebust loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); 17497f2f12d9STrond Myklebust struct writeback_control wbc = { 17507f2f12d9STrond Myklebust .sync_mode = WB_SYNC_ALL, 17517f2f12d9STrond Myklebust .nr_to_write = 0, 17527f2f12d9STrond Myklebust .range_start = range_start, 17537f2f12d9STrond Myklebust .range_end = range_end, 17547f2f12d9STrond Myklebust }; 17557f2f12d9STrond Myklebust int ret; 17567f2f12d9STrond Myklebust 17570522f6adSTrond Myklebust for (;;) { 1758ba8b06e6STrond Myklebust wait_on_page_writeback(page); 17597f2f12d9STrond Myklebust if (clear_page_dirty_for_io(page)) { 17607f2f12d9STrond Myklebust ret = nfs_writepage_locked(page, &wbc); 17617f2f12d9STrond Myklebust if (ret < 0) 17627f2f12d9STrond Myklebust goto out_error; 17630522f6adSTrond Myklebust continue; 17647f2f12d9STrond Myklebust } 17650522f6adSTrond Myklebust if (!PagePrivate(page)) 17660522f6adSTrond Myklebust break; 17670522f6adSTrond Myklebust ret = nfs_commit_inode(inode, FLUSH_SYNC); 17687f2f12d9STrond Myklebust if (ret < 0) 17697f2f12d9STrond Myklebust goto out_error; 17707f2f12d9STrond Myklebust } 17717f2f12d9STrond Myklebust return 0; 17727f2f12d9STrond Myklebust out_error: 17737f2f12d9STrond Myklebust return ret; 17741c75950bSTrond Myklebust } 17751c75950bSTrond Myklebust 1776074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION 1777074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage, 1778a6bc32b8SMel Gorman struct page *page, enum migrate_mode mode) 1779074cc1deSTrond Myklebust { 17802da95652SJeff Layton /* 17812da95652SJeff Layton * If PagePrivate is set, then the page is currently associated with 17822da95652SJeff Layton * an in-progress read or write request. Don't try to migrate it. 17832da95652SJeff Layton * 17842da95652SJeff Layton * FIXME: we could do this in principle, but we'll need a way to ensure 17852da95652SJeff Layton * that we can safely release the inode reference while holding 17862da95652SJeff Layton * the page lock. 17872da95652SJeff Layton */ 17882da95652SJeff Layton if (PagePrivate(page)) 17892da95652SJeff Layton return -EBUSY; 1790074cc1deSTrond Myklebust 1791074cc1deSTrond Myklebust nfs_fscache_release_page(page, GFP_KERNEL); 1792074cc1deSTrond Myklebust 1793a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 1794074cc1deSTrond Myklebust } 1795074cc1deSTrond Myklebust #endif 1796074cc1deSTrond Myklebust 1797f7b422b1SDavid Howells int __init nfs_init_writepagecache(void) 17981da177e4SLinus Torvalds { 17991da177e4SLinus Torvalds nfs_wdata_cachep = kmem_cache_create("nfs_write_data", 1800cd841605SFred Isaman sizeof(struct nfs_write_header), 18011da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 180220c2df83SPaul Mundt NULL); 18031da177e4SLinus Torvalds if (nfs_wdata_cachep == NULL) 18041da177e4SLinus Torvalds return -ENOMEM; 18051da177e4SLinus Torvalds 180693d2341cSMatthew Dobson nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, 18071da177e4SLinus Torvalds nfs_wdata_cachep); 18081da177e4SLinus Torvalds if (nfs_wdata_mempool == NULL) 18091da177e4SLinus Torvalds return -ENOMEM; 18101da177e4SLinus Torvalds 18110b7c0153SFred Isaman nfs_cdata_cachep = kmem_cache_create("nfs_commit_data", 18120b7c0153SFred Isaman sizeof(struct nfs_commit_data), 18130b7c0153SFred Isaman 0, SLAB_HWCACHE_ALIGN, 18140b7c0153SFred Isaman NULL); 18150b7c0153SFred Isaman if (nfs_cdata_cachep == NULL) 18160b7c0153SFred Isaman return -ENOMEM; 18170b7c0153SFred Isaman 181893d2341cSMatthew Dobson nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, 18191da177e4SLinus Torvalds nfs_wdata_cachep); 18201da177e4SLinus Torvalds if (nfs_commit_mempool == NULL) 18211da177e4SLinus Torvalds return -ENOMEM; 18221da177e4SLinus Torvalds 182389a09141SPeter Zijlstra /* 182489a09141SPeter Zijlstra * NFS congestion size, scale with available memory. 182589a09141SPeter Zijlstra * 182689a09141SPeter Zijlstra * 64MB: 8192k 182789a09141SPeter Zijlstra * 128MB: 11585k 182889a09141SPeter Zijlstra * 256MB: 16384k 182989a09141SPeter Zijlstra * 512MB: 23170k 183089a09141SPeter Zijlstra * 1GB: 32768k 183189a09141SPeter Zijlstra * 2GB: 46340k 183289a09141SPeter Zijlstra * 4GB: 65536k 183389a09141SPeter Zijlstra * 8GB: 92681k 183489a09141SPeter Zijlstra * 16GB: 131072k 183589a09141SPeter Zijlstra * 183689a09141SPeter Zijlstra * This allows larger machines to have larger/more transfers. 183789a09141SPeter Zijlstra * Limit the default to 256M 183889a09141SPeter Zijlstra */ 183989a09141SPeter Zijlstra nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); 184089a09141SPeter Zijlstra if (nfs_congestion_kb > 256*1024) 184189a09141SPeter Zijlstra nfs_congestion_kb = 256*1024; 184289a09141SPeter Zijlstra 18431da177e4SLinus Torvalds return 0; 18441da177e4SLinus Torvalds } 18451da177e4SLinus Torvalds 1846266bee88SDavid Brownell void nfs_destroy_writepagecache(void) 18471da177e4SLinus Torvalds { 18481da177e4SLinus Torvalds mempool_destroy(nfs_commit_mempool); 18491da177e4SLinus Torvalds mempool_destroy(nfs_wdata_mempool); 18501a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_wdata_cachep); 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds 1853