11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/nfs/write.c 31da177e4SLinus Torvalds * 47c85d900STrond Myklebust * Write file data over NFS. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de> 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds #include <linux/types.h> 101da177e4SLinus Torvalds #include <linux/slab.h> 111da177e4SLinus Torvalds #include <linux/mm.h> 121da177e4SLinus Torvalds #include <linux/pagemap.h> 131da177e4SLinus Torvalds #include <linux/file.h> 141da177e4SLinus Torvalds #include <linux/writeback.h> 1589a09141SPeter Zijlstra #include <linux/swap.h> 16074cc1deSTrond Myklebust #include <linux/migrate.h> 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 191da177e4SLinus Torvalds #include <linux/nfs_fs.h> 201da177e4SLinus Torvalds #include <linux/nfs_mount.h> 211da177e4SLinus Torvalds #include <linux/nfs_page.h> 223fcfab16SAndrew Morton #include <linux/backing-dev.h> 23afeacc8cSPaul Gortmaker #include <linux/export.h> 243fcfab16SAndrew Morton 251da177e4SLinus Torvalds #include <asm/uaccess.h> 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds #include "delegation.h" 2849a70f27STrond Myklebust #include "internal.h" 2991d5b470SChuck Lever #include "iostat.h" 30def6ed7eSAndy Adamson #include "nfs4_fs.h" 31074cc1deSTrond Myklebust #include "fscache.h" 3294ad1c80SFred Isaman #include "pnfs.h" 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_PAGECACHE 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds #define MIN_POOL_WRITE (32) 371da177e4SLinus Torvalds #define MIN_POOL_COMMIT (4) 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds /* 401da177e4SLinus Torvalds * Local function declarations 411da177e4SLinus Torvalds */ 42c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc, 43c63c7b05STrond Myklebust struct inode *inode, int ioflags); 44f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req); 456c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops; 46788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops; 471da177e4SLinus Torvalds 48e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep; 493feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool; 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); 833feb2d49STrond Myklebust } 843feb2d49STrond Myklebust return p; 853feb2d49STrond Myklebust } 863feb2d49STrond Myklebust 876c75dc0dSFred Isaman struct nfs_write_data *nfs_writedata_alloc(struct nfs_pgio_header *hdr, 886c75dc0dSFred Isaman unsigned int pagecount) 896c75dc0dSFred Isaman { 906c75dc0dSFred Isaman struct nfs_write_data *data, *prealloc; 916c75dc0dSFred Isaman 926c75dc0dSFred Isaman prealloc = &container_of(hdr, struct nfs_write_header, header)->rpc_data; 936c75dc0dSFred Isaman if (prealloc->header == NULL) 946c75dc0dSFred Isaman data = prealloc; 956c75dc0dSFred Isaman else 966c75dc0dSFred Isaman data = kzalloc(sizeof(*data), GFP_KERNEL); 976c75dc0dSFred Isaman if (!data) 986c75dc0dSFred Isaman goto out; 996c75dc0dSFred Isaman 1006c75dc0dSFred Isaman if (nfs_pgarray_set(&data->pages, pagecount)) { 1016c75dc0dSFred Isaman data->header = hdr; 1026c75dc0dSFred Isaman atomic_inc(&hdr->refcnt); 1036c75dc0dSFred Isaman } else { 1046c75dc0dSFred Isaman if (data != prealloc) 1056c75dc0dSFred Isaman kfree(data); 1066c75dc0dSFred Isaman data = NULL; 1076c75dc0dSFred Isaman } 1086c75dc0dSFred Isaman out: 1096c75dc0dSFred Isaman return data; 1106c75dc0dSFred Isaman } 1116c75dc0dSFred Isaman 112cd841605SFred Isaman void nfs_writehdr_free(struct nfs_pgio_header *hdr) 1133feb2d49STrond Myklebust { 114cd841605SFred Isaman struct nfs_write_header *whdr = container_of(hdr, struct nfs_write_header, header); 115cd841605SFred Isaman mempool_free(whdr, nfs_wdata_mempool); 1163feb2d49STrond Myklebust } 1173feb2d49STrond Myklebust 118dce81290STrond Myklebust void nfs_writedata_release(struct nfs_write_data *wdata) 1191da177e4SLinus Torvalds { 1206c75dc0dSFred Isaman struct nfs_pgio_header *hdr = wdata->header; 1216c75dc0dSFred Isaman struct nfs_write_header *write_header = container_of(hdr, struct nfs_write_header, header); 1226c75dc0dSFred Isaman 123383ba719STrond Myklebust put_nfs_open_context(wdata->args.context); 12430dd374fSFred Isaman if (wdata->pages.pagevec != wdata->pages.page_array) 12530dd374fSFred Isaman kfree(wdata->pages.pagevec); 1266c75dc0dSFred Isaman if (wdata != &write_header->rpc_data) 1276c75dc0dSFred Isaman kfree(wdata); 1286c75dc0dSFred Isaman else 1296c75dc0dSFred Isaman wdata->header = NULL; 1306c75dc0dSFred Isaman if (atomic_dec_and_test(&hdr->refcnt)) 1316c75dc0dSFred Isaman nfs_write_completion(hdr); 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds 1347b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error) 1357b159fc1STrond Myklebust { 1367b159fc1STrond Myklebust ctx->error = error; 1377b159fc1STrond Myklebust smp_wmb(); 1387b159fc1STrond Myklebust set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 1397b159fc1STrond Myklebust } 1407b159fc1STrond Myklebust 141277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page) 142277459d2STrond Myklebust { 143277459d2STrond Myklebust struct nfs_page *req = NULL; 144277459d2STrond Myklebust 145277459d2STrond Myklebust if (PagePrivate(page)) { 146277459d2STrond Myklebust req = (struct nfs_page *)page_private(page); 147277459d2STrond Myklebust if (req != NULL) 148c03b4024STrond Myklebust kref_get(&req->wb_kref); 149277459d2STrond Myklebust } 150277459d2STrond Myklebust return req; 151277459d2STrond Myklebust } 152277459d2STrond Myklebust 153277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page) 154277459d2STrond Myklebust { 155587142f8STrond Myklebust struct inode *inode = page->mapping->host; 156277459d2STrond Myklebust struct nfs_page *req = NULL; 157277459d2STrond Myklebust 158587142f8STrond Myklebust spin_lock(&inode->i_lock); 159277459d2STrond Myklebust req = nfs_page_find_request_locked(page); 160587142f8STrond Myklebust spin_unlock(&inode->i_lock); 161277459d2STrond Myklebust return req; 162277459d2STrond Myklebust } 163277459d2STrond Myklebust 1641da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */ 1651da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) 1661da177e4SLinus Torvalds { 1671da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 168a3d01454STrond Myklebust loff_t end, i_size; 169a3d01454STrond Myklebust pgoff_t end_index; 1701da177e4SLinus Torvalds 171a3d01454STrond Myklebust spin_lock(&inode->i_lock); 172a3d01454STrond Myklebust i_size = i_size_read(inode); 173a3d01454STrond Myklebust end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; 1741da177e4SLinus Torvalds if (i_size > 0 && page->index < end_index) 175a3d01454STrond Myklebust goto out; 1761da177e4SLinus Torvalds end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count); 1771da177e4SLinus Torvalds if (i_size >= end) 178a3d01454STrond Myklebust goto out; 1791da177e4SLinus Torvalds i_size_write(inode, end); 180a3d01454STrond Myklebust nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); 181a3d01454STrond Myklebust out: 182a3d01454STrond Myklebust spin_unlock(&inode->i_lock); 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 185a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */ 186a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page) 187a301b777STrond Myklebust { 188a301b777STrond Myklebust SetPageError(page); 189a301b777STrond Myklebust nfs_zap_mapping(page->mapping->host, page->mapping); 190a301b777STrond Myklebust } 191a301b777STrond Myklebust 1921da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request 1931da177e4SLinus Torvalds * covers the full page. 1941da177e4SLinus Torvalds */ 1951da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count) 1961da177e4SLinus Torvalds { 1971da177e4SLinus Torvalds if (PageUptodate(page)) 1981da177e4SLinus Torvalds return; 1991da177e4SLinus Torvalds if (base != 0) 2001da177e4SLinus Torvalds return; 20149a70f27STrond Myklebust if (count != nfs_page_length(page)) 2021da177e4SLinus Torvalds return; 2031da177e4SLinus Torvalds SetPageUptodate(page); 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc) 2071da177e4SLinus Torvalds { 2081da177e4SLinus Torvalds if (wbc->for_reclaim) 209c63c7b05STrond Myklebust return FLUSH_HIGHPRI | FLUSH_STABLE; 210b17621feSWu Fengguang if (wbc->for_kupdate || wbc->for_background) 211b31268acSTrond Myklebust return FLUSH_LOWPRI | FLUSH_COND_STABLE; 212b31268acSTrond Myklebust return FLUSH_COND_STABLE; 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds /* 21689a09141SPeter Zijlstra * NFS congestion control 21789a09141SPeter Zijlstra */ 21889a09141SPeter Zijlstra 21989a09141SPeter Zijlstra int nfs_congestion_kb; 22089a09141SPeter Zijlstra 22189a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) 22289a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH \ 22389a09141SPeter Zijlstra (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) 22489a09141SPeter Zijlstra 2255a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page) 22689a09141SPeter Zijlstra { 2275a6d41b3STrond Myklebust int ret = test_set_page_writeback(page); 2285a6d41b3STrond Myklebust 2295a6d41b3STrond Myklebust if (!ret) { 23089a09141SPeter Zijlstra struct inode *inode = page->mapping->host; 23189a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 23289a09141SPeter Zijlstra 233a6305ddbSTrond Myklebust page_cache_get(page); 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); 249a6305ddbSTrond Myklebust page_cache_release(page); 250c4dc4beeSPeter Zijlstra if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) 2518aa7e847SJens Axboe clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC); 25289a09141SPeter Zijlstra } 25389a09141SPeter Zijlstra 254cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock) 255e261f51fSTrond Myklebust { 256587142f8STrond Myklebust struct inode *inode = page->mapping->host; 257e261f51fSTrond Myklebust struct nfs_page *req; 258e261f51fSTrond Myklebust int ret; 259e261f51fSTrond Myklebust 260587142f8STrond Myklebust spin_lock(&inode->i_lock); 261e261f51fSTrond Myklebust for (;;) { 262e261f51fSTrond Myklebust req = nfs_page_find_request_locked(page); 263074cc1deSTrond Myklebust if (req == NULL) 264074cc1deSTrond Myklebust break; 2659994b62bSFred Isaman if (nfs_lock_request_dontget(req)) 266e261f51fSTrond Myklebust break; 267e261f51fSTrond Myklebust /* Note: If we hold the page lock, as is the case in nfs_writepage, 2689994b62bSFred Isaman * then the call to nfs_lock_request_dontget() will always 269e261f51fSTrond Myklebust * succeed provided that someone hasn't already marked the 270e261f51fSTrond Myklebust * request as dirty (in which case we don't care). 271e261f51fSTrond Myklebust */ 272587142f8STrond Myklebust spin_unlock(&inode->i_lock); 273cfb506e1STrond Myklebust if (!nonblock) 274e261f51fSTrond Myklebust ret = nfs_wait_on_request(req); 275cfb506e1STrond Myklebust else 276cfb506e1STrond Myklebust ret = -EAGAIN; 277e261f51fSTrond Myklebust nfs_release_request(req); 278e261f51fSTrond Myklebust if (ret != 0) 279074cc1deSTrond Myklebust return ERR_PTR(ret); 280587142f8STrond Myklebust spin_lock(&inode->i_lock); 281e261f51fSTrond Myklebust } 282587142f8STrond Myklebust spin_unlock(&inode->i_lock); 283074cc1deSTrond Myklebust return req; 284612c9384STrond Myklebust } 285074cc1deSTrond Myklebust 286074cc1deSTrond Myklebust /* 287074cc1deSTrond Myklebust * Find an associated nfs write request, and prepare to flush it out 288074cc1deSTrond Myklebust * May return an error if the user signalled nfs_wait_on_request(). 289074cc1deSTrond Myklebust */ 290074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, 291cfb506e1STrond Myklebust struct page *page, bool nonblock) 292074cc1deSTrond Myklebust { 293074cc1deSTrond Myklebust struct nfs_page *req; 294074cc1deSTrond Myklebust int ret = 0; 295074cc1deSTrond Myklebust 296cfb506e1STrond Myklebust req = nfs_find_and_lock_request(page, nonblock); 297074cc1deSTrond Myklebust if (!req) 298074cc1deSTrond Myklebust goto out; 299074cc1deSTrond Myklebust ret = PTR_ERR(req); 300074cc1deSTrond Myklebust if (IS_ERR(req)) 301074cc1deSTrond Myklebust goto out; 302074cc1deSTrond Myklebust 303074cc1deSTrond Myklebust ret = nfs_set_page_writeback(page); 304074cc1deSTrond Myklebust BUG_ON(ret != 0); 305074cc1deSTrond Myklebust BUG_ON(test_bit(PG_CLEAN, &req->wb_flags)); 306074cc1deSTrond Myklebust 307f8512ad0SFred Isaman if (!nfs_pageio_add_request(pgio, req)) { 308f8512ad0SFred Isaman nfs_redirty_request(req); 309074cc1deSTrond Myklebust ret = pgio->pg_error; 310f8512ad0SFred Isaman } 311074cc1deSTrond Myklebust out: 312074cc1deSTrond Myklebust return ret; 313e261f51fSTrond Myklebust } 314e261f51fSTrond Myklebust 315f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio) 316f758c885STrond Myklebust { 317f758c885STrond Myklebust struct inode *inode = page->mapping->host; 318cfb506e1STrond Myklebust int ret; 319f758c885STrond Myklebust 320f758c885STrond Myklebust nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); 321f758c885STrond Myklebust nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); 322f758c885STrond Myklebust 323f758c885STrond Myklebust nfs_pageio_cond_complete(pgio, page->index); 3241b430beeSWu Fengguang ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE); 325cfb506e1STrond Myklebust if (ret == -EAGAIN) { 326cfb506e1STrond Myklebust redirty_page_for_writepage(wbc, page); 327cfb506e1STrond Myklebust ret = 0; 328cfb506e1STrond Myklebust } 329cfb506e1STrond Myklebust return ret; 330f758c885STrond Myklebust } 331f758c885STrond Myklebust 332e261f51fSTrond Myklebust /* 3331da177e4SLinus Torvalds * Write an mmapped page to the server. 3341da177e4SLinus Torvalds */ 3354d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc) 3361da177e4SLinus Torvalds { 337f758c885STrond Myklebust struct nfs_pageio_descriptor pgio; 338e261f51fSTrond Myklebust int err; 3391da177e4SLinus Torvalds 340f758c885STrond Myklebust nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc)); 341f758c885STrond Myklebust err = nfs_do_writepage(page, wbc, &pgio); 342f758c885STrond Myklebust nfs_pageio_complete(&pgio); 343f758c885STrond Myklebust if (err < 0) 3444d770ccfSTrond Myklebust return err; 345f758c885STrond Myklebust if (pgio.pg_error < 0) 346f758c885STrond Myklebust return pgio.pg_error; 347f758c885STrond Myklebust return 0; 3484d770ccfSTrond Myklebust } 3494d770ccfSTrond Myklebust 3504d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc) 3514d770ccfSTrond Myklebust { 352f758c885STrond Myklebust int ret; 3534d770ccfSTrond Myklebust 354f758c885STrond Myklebust ret = nfs_writepage_locked(page, wbc); 3551da177e4SLinus Torvalds unlock_page(page); 356f758c885STrond Myklebust return ret; 357f758c885STrond Myklebust } 358f758c885STrond Myklebust 359f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) 360f758c885STrond Myklebust { 361f758c885STrond Myklebust int ret; 362f758c885STrond Myklebust 363f758c885STrond Myklebust ret = nfs_do_writepage(page, wbc, data); 364f758c885STrond Myklebust unlock_page(page); 365f758c885STrond Myklebust return ret; 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) 3691da177e4SLinus Torvalds { 3701da177e4SLinus Torvalds struct inode *inode = mapping->host; 37172cb77f4STrond Myklebust unsigned long *bitlock = &NFS_I(inode)->flags; 372c63c7b05STrond Myklebust struct nfs_pageio_descriptor pgio; 3731da177e4SLinus Torvalds int err; 3741da177e4SLinus Torvalds 37572cb77f4STrond Myklebust /* Stop dirtying of new pages while we sync */ 37672cb77f4STrond Myklebust err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING, 37772cb77f4STrond Myklebust nfs_wait_bit_killable, TASK_KILLABLE); 37872cb77f4STrond Myklebust if (err) 37972cb77f4STrond Myklebust goto out_err; 38072cb77f4STrond Myklebust 38191d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); 38291d5b470SChuck Lever 383c63c7b05STrond Myklebust nfs_pageio_init_write(&pgio, inode, wb_priority(wbc)); 384f758c885STrond Myklebust err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); 385c63c7b05STrond Myklebust nfs_pageio_complete(&pgio); 38672cb77f4STrond Myklebust 38772cb77f4STrond Myklebust clear_bit_unlock(NFS_INO_FLUSHING, bitlock); 38872cb77f4STrond Myklebust smp_mb__after_clear_bit(); 38972cb77f4STrond Myklebust wake_up_bit(bitlock, NFS_INO_FLUSHING); 39072cb77f4STrond Myklebust 391f758c885STrond Myklebust if (err < 0) 39272cb77f4STrond Myklebust goto out_err; 39372cb77f4STrond Myklebust err = pgio.pg_error; 39472cb77f4STrond Myklebust if (err < 0) 39572cb77f4STrond Myklebust goto out_err; 396c63c7b05STrond Myklebust return 0; 39772cb77f4STrond Myklebust out_err: 39872cb77f4STrond Myklebust return err; 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds /* 4021da177e4SLinus Torvalds * Insert a write request into an inode 4031da177e4SLinus Torvalds */ 404d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req) 4051da177e4SLinus Torvalds { 4061da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 407e7d39069STrond Myklebust 408e7d39069STrond Myklebust /* Lock the request! */ 409e7d39069STrond Myklebust nfs_lock_request_dontget(req); 410e7d39069STrond Myklebust 411e7d39069STrond Myklebust spin_lock(&inode->i_lock); 4124d65c520STrond Myklebust if (!nfsi->npages && nfs_have_delegation(inode, FMODE_WRITE)) 413a9a4a87aSTrond Myklebust inode->i_version++; 4142df485a7STrond Myklebust set_bit(PG_MAPPED, &req->wb_flags); 415deb7d638STrond Myklebust SetPagePrivate(req->wb_page); 416277459d2STrond Myklebust set_page_private(req->wb_page, (unsigned long)req); 4171da177e4SLinus Torvalds nfsi->npages++; 418c03b4024STrond Myklebust kref_get(&req->wb_kref); 419e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 4201da177e4SLinus Torvalds } 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds /* 42389a09141SPeter Zijlstra * Remove a write request from an inode 4241da177e4SLinus Torvalds */ 4251da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req) 4261da177e4SLinus Torvalds { 4273d4ff43dSAl Viro struct inode *inode = req->wb_context->dentry->d_inode; 4281da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds BUG_ON (!NFS_WBACK_BUSY(req)); 4311da177e4SLinus Torvalds 432587142f8STrond Myklebust spin_lock(&inode->i_lock); 433277459d2STrond Myklebust set_page_private(req->wb_page, 0); 434deb7d638STrond Myklebust ClearPagePrivate(req->wb_page); 4352df485a7STrond Myklebust clear_bit(PG_MAPPED, &req->wb_flags); 4361da177e4SLinus Torvalds nfsi->npages--; 437587142f8STrond Myklebust spin_unlock(&inode->i_lock); 4381da177e4SLinus Torvalds nfs_release_request(req); 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds 44161822ab5STrond Myklebust static void 4426d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req) 44361822ab5STrond Myklebust { 44461822ab5STrond Myklebust __set_page_dirty_nobuffers(req->wb_page); 44561822ab5STrond Myklebust } 44661822ab5STrond Myklebust 4471da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 4488dd37758STrond Myklebust /** 4498dd37758STrond Myklebust * nfs_request_add_commit_list - add request to a commit list 4508dd37758STrond Myklebust * @req: pointer to a struct nfs_page 4518dd37758STrond Myklebust * @head: commit list head 4528dd37758STrond Myklebust * 4538dd37758STrond Myklebust * This sets the PG_CLEAN bit, updates the inode global count of 4548dd37758STrond Myklebust * number of outstanding requests requiring a commit as well as 4558dd37758STrond Myklebust * the MM page stats. 4568dd37758STrond Myklebust * 4578dd37758STrond Myklebust * The caller must _not_ hold the inode->i_lock, but must be 4588dd37758STrond Myklebust * holding the nfs_page lock. 4598dd37758STrond Myklebust */ 4608dd37758STrond Myklebust void 4618dd37758STrond Myklebust nfs_request_add_commit_list(struct nfs_page *req, struct list_head *head) 4628dd37758STrond Myklebust { 4638dd37758STrond Myklebust struct inode *inode = req->wb_context->dentry->d_inode; 4648dd37758STrond Myklebust 4658dd37758STrond Myklebust set_bit(PG_CLEAN, &(req)->wb_flags); 4668dd37758STrond Myklebust spin_lock(&inode->i_lock); 4678dd37758STrond Myklebust nfs_list_add_request(req, head); 4688dd37758STrond Myklebust NFS_I(inode)->ncommit++; 4698dd37758STrond Myklebust spin_unlock(&inode->i_lock); 4708dd37758STrond Myklebust inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 4718dd37758STrond Myklebust inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE); 4728dd37758STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 4738dd37758STrond Myklebust } 4748dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list); 4758dd37758STrond Myklebust 4768dd37758STrond Myklebust /** 4778dd37758STrond Myklebust * nfs_request_remove_commit_list - Remove request from a commit list 4788dd37758STrond Myklebust * @req: pointer to a nfs_page 4798dd37758STrond Myklebust * 4808dd37758STrond Myklebust * This clears the PG_CLEAN bit, and updates the inode global count of 4818dd37758STrond Myklebust * number of outstanding requests requiring a commit 4828dd37758STrond Myklebust * It does not update the MM page stats. 4838dd37758STrond Myklebust * 4848dd37758STrond Myklebust * The caller _must_ hold the inode->i_lock and the nfs_page lock. 4858dd37758STrond Myklebust */ 4868dd37758STrond Myklebust void 4878dd37758STrond Myklebust nfs_request_remove_commit_list(struct nfs_page *req) 4888dd37758STrond Myklebust { 4898dd37758STrond Myklebust struct inode *inode = req->wb_context->dentry->d_inode; 4908dd37758STrond Myklebust 4918dd37758STrond Myklebust if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) 4928dd37758STrond Myklebust return; 4938dd37758STrond Myklebust nfs_list_remove_request(req); 4948dd37758STrond Myklebust NFS_I(inode)->ncommit--; 4958dd37758STrond Myklebust } 4968dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list); 4978dd37758STrond Myklebust 4988dd37758STrond Myklebust 4991da177e4SLinus Torvalds /* 5001da177e4SLinus Torvalds * Add a request to the inode's commit list. 5011da177e4SLinus Torvalds */ 5021da177e4SLinus Torvalds static void 503a861a1e1SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg) 5041da177e4SLinus Torvalds { 5053d4ff43dSAl Viro struct inode *inode = req->wb_context->dentry->d_inode; 5061da177e4SLinus Torvalds 5078dd37758STrond Myklebust if (pnfs_mark_request_commit(req, lseg)) 5088dd37758STrond Myklebust return; 5098dd37758STrond Myklebust nfs_request_add_commit_list(req, &NFS_I(inode)->commit_list); 5101da177e4SLinus Torvalds } 5118e821cadSTrond Myklebust 512d6d6dc7cSFred Isaman static void 513d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page) 514e468bae9STrond Myklebust { 515e468bae9STrond Myklebust dec_zone_page_state(page, NR_UNSTABLE_NFS); 516e468bae9STrond Myklebust dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE); 517e468bae9STrond Myklebust } 518d6d6dc7cSFred Isaman 5198dd37758STrond Myklebust static void 520d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req) 521d6d6dc7cSFred Isaman { 5228dd37758STrond Myklebust if (test_bit(PG_CLEAN, &req->wb_flags)) { 5238dd37758STrond Myklebust struct inode *inode = req->wb_context->dentry->d_inode; 524d6d6dc7cSFred Isaman 5258dd37758STrond Myklebust if (!pnfs_clear_request_commit(req)) { 5268dd37758STrond Myklebust spin_lock(&inode->i_lock); 5278dd37758STrond Myklebust nfs_request_remove_commit_list(req); 5288dd37758STrond Myklebust spin_unlock(&inode->i_lock); 529d6d6dc7cSFred Isaman } 5308dd37758STrond Myklebust nfs_clear_page_commit(req->wb_page); 5318dd37758STrond Myklebust } 532e468bae9STrond Myklebust } 533e468bae9STrond Myklebust 5348e821cadSTrond Myklebust static inline 5358e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 5368e821cadSTrond Myklebust { 537465d5243SFred Isaman if (data->verf.committed == NFS_DATA_SYNC) 538cd841605SFred Isaman return data->header->lseg == NULL; 5398e821cadSTrond Myklebust return data->verf.committed != NFS_FILE_SYNC; 5408e821cadSTrond Myklebust } 5418e821cadSTrond Myklebust 5428e821cadSTrond Myklebust #else 5438dd37758STrond Myklebust static void 544a861a1e1SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg) 5458e821cadSTrond Myklebust { 5468e821cadSTrond Myklebust } 5478e821cadSTrond Myklebust 5488dd37758STrond Myklebust static void 549e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req) 550e468bae9STrond Myklebust { 551e468bae9STrond Myklebust } 552e468bae9STrond Myklebust 5538e821cadSTrond Myklebust static inline 5548e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 5558e821cadSTrond Myklebust { 5568e821cadSTrond Myklebust return 0; 5578e821cadSTrond Myklebust } 5588e821cadSTrond Myklebust 5591da177e4SLinus Torvalds #endif 5601da177e4SLinus Torvalds 5616c75dc0dSFred Isaman void nfs_write_completion(struct nfs_pgio_header *hdr) 5626c75dc0dSFred Isaman { 5636c75dc0dSFred Isaman unsigned long bytes = 0; 5646c75dc0dSFred Isaman 5656c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 5666c75dc0dSFred Isaman goto out; 5676c75dc0dSFred Isaman while (!list_empty(&hdr->pages)) { 5686c75dc0dSFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 5696c75dc0dSFred Isaman struct page *page = req->wb_page; 5706c75dc0dSFred Isaman 5716c75dc0dSFred Isaman bytes += req->wb_bytes; 5726c75dc0dSFred Isaman nfs_list_remove_request(req); 5736c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && 5746c75dc0dSFred Isaman (hdr->good_bytes < bytes)) { 5756c75dc0dSFred Isaman nfs_set_pageerror(page); 5766c75dc0dSFred Isaman nfs_context_set_write_error(req->wb_context, hdr->error); 5776c75dc0dSFred Isaman goto remove_req; 5786c75dc0dSFred Isaman } 5796c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) { 5806c75dc0dSFred Isaman nfs_mark_request_dirty(req); 5816c75dc0dSFred Isaman goto next; 5826c75dc0dSFred Isaman } 5836c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) { 5846c75dc0dSFred Isaman nfs_mark_request_commit(req, hdr->lseg); 5856c75dc0dSFred Isaman goto next; 5866c75dc0dSFred Isaman } 5876c75dc0dSFred Isaman remove_req: 5886c75dc0dSFred Isaman nfs_inode_remove_request(req); 5896c75dc0dSFred Isaman next: 5906c75dc0dSFred Isaman nfs_unlock_request(req); 5916c75dc0dSFred Isaman nfs_end_page_writeback(page); 5926c75dc0dSFred Isaman } 5936c75dc0dSFred Isaman out: 5946c75dc0dSFred Isaman hdr->release(hdr); 5956c75dc0dSFred Isaman } 5966c75dc0dSFred Isaman 59747c62564STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 598fb8a1f11STrond Myklebust static int 599fb8a1f11STrond Myklebust nfs_need_commit(struct nfs_inode *nfsi) 600fb8a1f11STrond Myklebust { 601d6d6dc7cSFred Isaman return nfsi->ncommit > 0; 602fb8a1f11STrond Myklebust } 603fb8a1f11STrond Myklebust 604d6d6dc7cSFred Isaman /* i_lock held by caller */ 6058dd37758STrond Myklebust static int 6063b3be88dSTrond Myklebust nfs_scan_commit_list(struct list_head *src, struct list_head *dst, int max, 6073b3be88dSTrond Myklebust spinlock_t *lock) 608d6d6dc7cSFred Isaman { 609d6d6dc7cSFred Isaman struct nfs_page *req, *tmp; 610d6d6dc7cSFred Isaman int ret = 0; 611d6d6dc7cSFred Isaman 612d6d6dc7cSFred Isaman list_for_each_entry_safe(req, tmp, src, wb_list) { 6138dd37758STrond Myklebust if (!nfs_lock_request(req)) 6148dd37758STrond Myklebust continue; 6153b3be88dSTrond Myklebust if (cond_resched_lock(lock)) 6163b3be88dSTrond Myklebust list_safe_reset_next(req, tmp, wb_list); 6178dd37758STrond Myklebust nfs_request_remove_commit_list(req); 6188dd37758STrond Myklebust nfs_list_add_request(req, dst); 619d6d6dc7cSFred Isaman ret++; 620d6d6dc7cSFred Isaman if (ret == max) 621d6d6dc7cSFred Isaman break; 622d6d6dc7cSFred Isaman } 623d6d6dc7cSFred Isaman return ret; 624d6d6dc7cSFred Isaman } 625d6d6dc7cSFred Isaman 6261da177e4SLinus Torvalds /* 6271da177e4SLinus Torvalds * nfs_scan_commit - Scan an inode for commit requests 6281da177e4SLinus Torvalds * @inode: NFS inode to scan 6291da177e4SLinus Torvalds * @dst: destination list 6301da177e4SLinus Torvalds * 6311da177e4SLinus Torvalds * Moves requests from the inode's 'commit' request list. 6321da177e4SLinus Torvalds * The requests are *not* checked to ensure that they form a contiguous set. 6331da177e4SLinus Torvalds */ 6341da177e4SLinus Torvalds static int 635d6d6dc7cSFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst) 6361da177e4SLinus Torvalds { 6371da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 638d6d6dc7cSFred Isaman int ret = 0; 639fb8a1f11STrond Myklebust 6400d88f6e8SDave Chinner spin_lock(&inode->i_lock); 641d6d6dc7cSFred Isaman if (nfsi->ncommit > 0) { 6428dd37758STrond Myklebust const int max = INT_MAX; 643d6d6dc7cSFred Isaman 6443b3be88dSTrond Myklebust ret = nfs_scan_commit_list(&nfsi->commit_list, dst, max, 6453b3be88dSTrond Myklebust &inode->i_lock); 6463b3be88dSTrond Myklebust ret += pnfs_scan_commit_lists(inode, max - ret, 6473b3be88dSTrond Myklebust &inode->i_lock); 648d6d6dc7cSFred Isaman } 6490d88f6e8SDave Chinner spin_unlock(&inode->i_lock); 650ff778d02STrond Myklebust return ret; 6511da177e4SLinus Torvalds } 652d6d6dc7cSFred Isaman 653c42de9ddSTrond Myklebust #else 654fb8a1f11STrond Myklebust static inline int nfs_need_commit(struct nfs_inode *nfsi) 655fb8a1f11STrond Myklebust { 656fb8a1f11STrond Myklebust return 0; 657fb8a1f11STrond Myklebust } 658fb8a1f11STrond Myklebust 659d6d6dc7cSFred Isaman static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst) 660c42de9ddSTrond Myklebust { 661c42de9ddSTrond Myklebust return 0; 662c42de9ddSTrond Myklebust } 6631da177e4SLinus Torvalds #endif 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds /* 666e7d39069STrond Myklebust * Search for an existing write request, and attempt to update 667e7d39069STrond Myklebust * it to reflect a new dirty region on a given page. 6681da177e4SLinus Torvalds * 669e7d39069STrond Myklebust * If the attempt fails, then the existing request is flushed out 670e7d39069STrond Myklebust * to disk. 6711da177e4SLinus Torvalds */ 672e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode, 673e7d39069STrond Myklebust struct page *page, 674e7d39069STrond Myklebust unsigned int offset, 675e7d39069STrond Myklebust unsigned int bytes) 6761da177e4SLinus Torvalds { 677e7d39069STrond Myklebust struct nfs_page *req; 678e7d39069STrond Myklebust unsigned int rqend; 679e7d39069STrond Myklebust unsigned int end; 6801da177e4SLinus Torvalds int error; 681277459d2STrond Myklebust 682e7d39069STrond Myklebust if (!PagePrivate(page)) 683e7d39069STrond Myklebust return NULL; 684e7d39069STrond Myklebust 685e7d39069STrond Myklebust end = offset + bytes; 686e7d39069STrond Myklebust spin_lock(&inode->i_lock); 687e7d39069STrond Myklebust 688e7d39069STrond Myklebust for (;;) { 689e7d39069STrond Myklebust req = nfs_page_find_request_locked(page); 690e7d39069STrond Myklebust if (req == NULL) 691e7d39069STrond Myklebust goto out_unlock; 692e7d39069STrond Myklebust 693e7d39069STrond Myklebust rqend = req->wb_offset + req->wb_bytes; 694e7d39069STrond Myklebust /* 695e7d39069STrond Myklebust * Tell the caller to flush out the request if 696e7d39069STrond Myklebust * the offsets are non-contiguous. 697e7d39069STrond Myklebust * Note: nfs_flush_incompatible() will already 698e7d39069STrond Myklebust * have flushed out requests having wrong owners. 699e7d39069STrond Myklebust */ 700e468bae9STrond Myklebust if (offset > rqend 701e7d39069STrond Myklebust || end < req->wb_offset) 702e7d39069STrond Myklebust goto out_flushme; 703e7d39069STrond Myklebust 7049994b62bSFred Isaman if (nfs_lock_request_dontget(req)) 705e7d39069STrond Myklebust break; 706e7d39069STrond Myklebust 707e7d39069STrond Myklebust /* The request is locked, so wait and then retry */ 708587142f8STrond Myklebust spin_unlock(&inode->i_lock); 7091da177e4SLinus Torvalds error = nfs_wait_on_request(req); 7101da177e4SLinus Torvalds nfs_release_request(req); 711e7d39069STrond Myklebust if (error != 0) 712e7d39069STrond Myklebust goto out_err; 713e7d39069STrond Myklebust spin_lock(&inode->i_lock); 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds /* Okay, the request matches. Update the region */ 7171da177e4SLinus Torvalds if (offset < req->wb_offset) { 7181da177e4SLinus Torvalds req->wb_offset = offset; 7191da177e4SLinus Torvalds req->wb_pgbase = offset; 7201da177e4SLinus Torvalds } 7211da177e4SLinus Torvalds if (end > rqend) 7221da177e4SLinus Torvalds req->wb_bytes = end - req->wb_offset; 723e7d39069STrond Myklebust else 724e7d39069STrond Myklebust req->wb_bytes = rqend - req->wb_offset; 725e7d39069STrond Myklebust out_unlock: 726e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 727ca138f36SFred Isaman if (req) 7288dd37758STrond Myklebust nfs_clear_request_commit(req); 729e7d39069STrond Myklebust return req; 730e7d39069STrond Myklebust out_flushme: 731e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 732e7d39069STrond Myklebust nfs_release_request(req); 733e7d39069STrond Myklebust error = nfs_wb_page(inode, page); 734e7d39069STrond Myklebust out_err: 735e7d39069STrond Myklebust return ERR_PTR(error); 736e7d39069STrond Myklebust } 7371da177e4SLinus Torvalds 738e7d39069STrond Myklebust /* 739e7d39069STrond Myklebust * Try to update an existing write request, or create one if there is none. 740e7d39069STrond Myklebust * 741e7d39069STrond Myklebust * Note: Should always be called with the Page Lock held to prevent races 742e7d39069STrond Myklebust * if we have to add a new request. Also assumes that the caller has 743e7d39069STrond Myklebust * already called nfs_flush_incompatible() if necessary. 744e7d39069STrond Myklebust */ 745e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, 746e7d39069STrond Myklebust struct page *page, unsigned int offset, unsigned int bytes) 747e7d39069STrond Myklebust { 748e7d39069STrond Myklebust struct inode *inode = page->mapping->host; 749e7d39069STrond Myklebust struct nfs_page *req; 750e7d39069STrond Myklebust 751e7d39069STrond Myklebust req = nfs_try_to_update_request(inode, page, offset, bytes); 752e7d39069STrond Myklebust if (req != NULL) 753e7d39069STrond Myklebust goto out; 754e7d39069STrond Myklebust req = nfs_create_request(ctx, inode, page, offset, bytes); 755e7d39069STrond Myklebust if (IS_ERR(req)) 756e7d39069STrond Myklebust goto out; 757d6d6dc7cSFred Isaman nfs_inode_add_request(inode, req); 758efc91ed0STrond Myklebust out: 75961e930a9STrond Myklebust return req; 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds 762e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, 763e7d39069STrond Myklebust unsigned int offset, unsigned int count) 764e7d39069STrond Myklebust { 765e7d39069STrond Myklebust struct nfs_page *req; 766e7d39069STrond Myklebust 767e7d39069STrond Myklebust req = nfs_setup_write_request(ctx, page, offset, count); 768e7d39069STrond Myklebust if (IS_ERR(req)) 769e7d39069STrond Myklebust return PTR_ERR(req); 770e7d39069STrond Myklebust /* Update file length */ 771e7d39069STrond Myklebust nfs_grow_file(page, offset, count); 772e7d39069STrond Myklebust nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); 773a6305ddbSTrond Myklebust nfs_mark_request_dirty(req); 7749994b62bSFred Isaman nfs_unlock_request(req); 775e7d39069STrond Myklebust return 0; 776e7d39069STrond Myklebust } 777e7d39069STrond Myklebust 7781da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page) 7791da177e4SLinus Torvalds { 780cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 7811da177e4SLinus Torvalds struct nfs_page *req; 7821a54533eSTrond Myklebust int do_flush, status; 7831da177e4SLinus Torvalds /* 7841da177e4SLinus Torvalds * Look for a request corresponding to this page. If there 7851da177e4SLinus Torvalds * is one, and it belongs to another file, we flush it out 7861da177e4SLinus Torvalds * before we try to copy anything into the page. Do this 7871da177e4SLinus Torvalds * due to the lack of an ACCESS-type call in NFSv2. 7881da177e4SLinus Torvalds * Also do the same if we find a request from an existing 7891da177e4SLinus Torvalds * dropped page. 7901da177e4SLinus Torvalds */ 7911a54533eSTrond Myklebust do { 792277459d2STrond Myklebust req = nfs_page_find_request(page); 7931a54533eSTrond Myklebust if (req == NULL) 7941a54533eSTrond Myklebust return 0; 795f11ac8dbSTrond Myklebust do_flush = req->wb_page != page || req->wb_context != ctx || 796f11ac8dbSTrond Myklebust req->wb_lock_context->lockowner != current->files || 797f11ac8dbSTrond Myklebust req->wb_lock_context->pid != current->tgid; 7981da177e4SLinus Torvalds nfs_release_request(req); 7991a54533eSTrond Myklebust if (!do_flush) 8001a54533eSTrond Myklebust return 0; 801277459d2STrond Myklebust status = nfs_wb_page(page->mapping->host, page); 8021a54533eSTrond Myklebust } while (status == 0); 8031a54533eSTrond Myklebust return status; 8041da177e4SLinus Torvalds } 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds /* 8075d47a356STrond Myklebust * If the page cache is marked as unsafe or invalid, then we can't rely on 8085d47a356STrond Myklebust * the PageUptodate() flag. In this case, we will need to turn off 8095d47a356STrond Myklebust * write optimisations that depend on the page contents being correct. 8105d47a356STrond Myklebust */ 8115d47a356STrond Myklebust static int nfs_write_pageuptodate(struct page *page, struct inode *inode) 8125d47a356STrond Myklebust { 8135d47a356STrond Myklebust return PageUptodate(page) && 8145d47a356STrond Myklebust !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA)); 8155d47a356STrond Myklebust } 8165d47a356STrond Myklebust 8175d47a356STrond Myklebust /* 8181da177e4SLinus Torvalds * Update and possibly write a cached page of an NFS file. 8191da177e4SLinus Torvalds * 8201da177e4SLinus Torvalds * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad 8211da177e4SLinus Torvalds * things with a page scheduled for an RPC call (e.g. invalidate it). 8221da177e4SLinus Torvalds */ 8231da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page, 8241da177e4SLinus Torvalds unsigned int offset, unsigned int count) 8251da177e4SLinus Torvalds { 826cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 8271da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 8281da177e4SLinus Torvalds int status = 0; 8291da177e4SLinus Torvalds 83091d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 83191d5b470SChuck Lever 83248186c7dSChuck Lever dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n", 83301cce933SJosef "Jeff" Sipek file->f_path.dentry->d_parent->d_name.name, 83401cce933SJosef "Jeff" Sipek file->f_path.dentry->d_name.name, count, 8350bbacc40SChuck Lever (long long)(page_offset(page) + offset)); 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds /* If we're not using byte range locks, and we know the page 8385d47a356STrond Myklebust * is up to date, it may be more efficient to extend the write 8395d47a356STrond Myklebust * to cover the entire page in order to avoid fragmentation 8405d47a356STrond Myklebust * inefficiencies. 8411da177e4SLinus Torvalds */ 8425d47a356STrond Myklebust if (nfs_write_pageuptodate(page, inode) && 8435d47a356STrond Myklebust inode->i_flock == NULL && 8446b2f3d1fSChristoph Hellwig !(file->f_flags & O_DSYNC)) { 84549a70f27STrond Myklebust count = max(count + offset, nfs_page_length(page)); 8461da177e4SLinus Torvalds offset = 0; 8471da177e4SLinus Torvalds } 8481da177e4SLinus Torvalds 849e21195a7STrond Myklebust status = nfs_writepage_setup(ctx, page, offset, count); 85003fa9e84STrond Myklebust if (status < 0) 85103fa9e84STrond Myklebust nfs_set_pageerror(page); 85259b7c05fSTrond Myklebust else 85359b7c05fSTrond Myklebust __set_page_dirty_nobuffers(page); 8541da177e4SLinus Torvalds 85548186c7dSChuck Lever dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", 8561da177e4SLinus Torvalds status, (long long)i_size_read(inode)); 8571da177e4SLinus Torvalds return status; 8581da177e4SLinus Torvalds } 8591da177e4SLinus Torvalds 8603ff7576dSTrond Myklebust static int flush_task_priority(int how) 8611da177e4SLinus Torvalds { 8621da177e4SLinus Torvalds switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { 8631da177e4SLinus Torvalds case FLUSH_HIGHPRI: 8641da177e4SLinus Torvalds return RPC_PRIORITY_HIGH; 8651da177e4SLinus Torvalds case FLUSH_LOWPRI: 8661da177e4SLinus Torvalds return RPC_PRIORITY_LOW; 8671da177e4SLinus Torvalds } 8681da177e4SLinus Torvalds return RPC_PRIORITY_NORMAL; 8691da177e4SLinus Torvalds } 8701da177e4SLinus Torvalds 871c5996c4eSFred Isaman int nfs_initiate_write(struct rpc_clnt *clnt, 872c5996c4eSFred Isaman struct nfs_write_data *data, 873788e7a89STrond Myklebust const struct rpc_call_ops *call_ops, 8741da177e4SLinus Torvalds int how) 8751da177e4SLinus Torvalds { 876cd841605SFred Isaman struct inode *inode = data->header->inode; 8773ff7576dSTrond Myklebust int priority = flush_task_priority(how); 87807737691STrond Myklebust struct rpc_task *task; 879bdc7f021STrond Myklebust struct rpc_message msg = { 880bdc7f021STrond Myklebust .rpc_argp = &data->args, 881bdc7f021STrond Myklebust .rpc_resp = &data->res, 882cd841605SFred Isaman .rpc_cred = data->header->cred, 883bdc7f021STrond Myklebust }; 88484115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 885d138d5d1SAndy Adamson .rpc_client = clnt, 88607737691STrond Myklebust .task = &data->task, 887bdc7f021STrond Myklebust .rpc_message = &msg, 88884115e1cSTrond Myklebust .callback_ops = call_ops, 88984115e1cSTrond Myklebust .callback_data = data, 890101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 8912c61be0aSTrond Myklebust .flags = RPC_TASK_ASYNC, 8923ff7576dSTrond Myklebust .priority = priority, 89384115e1cSTrond Myklebust }; 8942c61be0aSTrond Myklebust int ret = 0; 8951da177e4SLinus Torvalds 896d138d5d1SAndy Adamson /* Set up the initial task struct. */ 897d138d5d1SAndy Adamson NFS_PROTO(inode)->write_setup(data, &msg); 898d138d5d1SAndy Adamson 899d138d5d1SAndy Adamson dprintk("NFS: %5u initiated write call " 900d138d5d1SAndy Adamson "(req %s/%lld, %u bytes @ offset %llu)\n", 901d138d5d1SAndy Adamson data->task.tk_pid, 902d138d5d1SAndy Adamson inode->i_sb->s_id, 903d138d5d1SAndy Adamson (long long)NFS_FILEID(inode), 904d138d5d1SAndy Adamson data->args.count, 905d138d5d1SAndy Adamson (unsigned long long)data->args.offset); 906d138d5d1SAndy Adamson 907d138d5d1SAndy Adamson task = rpc_run_task(&task_setup_data); 908d138d5d1SAndy Adamson if (IS_ERR(task)) { 909d138d5d1SAndy Adamson ret = PTR_ERR(task); 910d138d5d1SAndy Adamson goto out; 911d138d5d1SAndy Adamson } 912d138d5d1SAndy Adamson if (how & FLUSH_SYNC) { 913d138d5d1SAndy Adamson ret = rpc_wait_for_completion_task(task); 914d138d5d1SAndy Adamson if (ret == 0) 915d138d5d1SAndy Adamson ret = task->tk_status; 916d138d5d1SAndy Adamson } 917d138d5d1SAndy Adamson rpc_put_task(task); 918d138d5d1SAndy Adamson out: 919d138d5d1SAndy Adamson return ret; 920d138d5d1SAndy Adamson } 921a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write); 922d138d5d1SAndy Adamson 923d138d5d1SAndy Adamson /* 924d138d5d1SAndy Adamson * Set up the argument/result storage required for the RPC call. 925d138d5d1SAndy Adamson */ 9266c75dc0dSFred Isaman static void nfs_write_rpcsetup(struct nfs_write_data *data, 927d138d5d1SAndy Adamson unsigned int count, unsigned int offset, 928d138d5d1SAndy Adamson int how) 929d138d5d1SAndy Adamson { 9306c75dc0dSFred Isaman struct nfs_page *req = data->header->req; 931d138d5d1SAndy Adamson 9321da177e4SLinus Torvalds /* Set up the RPC argument and reply structs 9331da177e4SLinus Torvalds * NB: take care not to mess about with data->commit et al. */ 9341da177e4SLinus Torvalds 9356c75dc0dSFred Isaman data->args.fh = NFS_FH(data->header->inode); 9361da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 9372bea038cSBoaz Harrosh /* pnfs_set_layoutcommit needs this */ 9382bea038cSBoaz Harrosh data->mds_offset = data->args.offset; 9391da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 94030dd374fSFred Isaman data->args.pages = data->pages.pagevec; 9411da177e4SLinus Torvalds data->args.count = count; 942383ba719STrond Myklebust data->args.context = get_nfs_open_context(req->wb_context); 943f11ac8dbSTrond Myklebust data->args.lock_context = req->wb_lock_context; 944bdc7f021STrond Myklebust data->args.stable = NFS_UNSTABLE; 94587ed5eb4STrond Myklebust switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) { 94687ed5eb4STrond Myklebust case 0: 94787ed5eb4STrond Myklebust break; 94887ed5eb4STrond Myklebust case FLUSH_COND_STABLE: 9496c75dc0dSFred Isaman if (nfs_need_commit(NFS_I(data->header->inode))) 95087ed5eb4STrond Myklebust break; 95187ed5eb4STrond Myklebust default: 952bdc7f021STrond Myklebust data->args.stable = NFS_FILE_SYNC; 953bdc7f021STrond Myklebust } 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds data->res.fattr = &data->fattr; 9561da177e4SLinus Torvalds data->res.count = count; 9571da177e4SLinus Torvalds data->res.verf = &data->verf; 9580e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 9596e4efd56STrond Myklebust } 9601da177e4SLinus Torvalds 9616e4efd56STrond Myklebust static int nfs_do_write(struct nfs_write_data *data, 9626e4efd56STrond Myklebust const struct rpc_call_ops *call_ops, 9636e4efd56STrond Myklebust int how) 9646e4efd56STrond Myklebust { 965cd841605SFred Isaman struct inode *inode = data->header->inode; 9660382b744SAndy Adamson 967c5996c4eSFred Isaman return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how); 9681da177e4SLinus Torvalds } 9691da177e4SLinus Torvalds 970275acaafSTrond Myklebust static int nfs_do_multiple_writes(struct list_head *head, 971275acaafSTrond Myklebust const struct rpc_call_ops *call_ops, 972275acaafSTrond Myklebust int how) 973275acaafSTrond Myklebust { 974275acaafSTrond Myklebust struct nfs_write_data *data; 975275acaafSTrond Myklebust int ret = 0; 976275acaafSTrond Myklebust 977275acaafSTrond Myklebust while (!list_empty(head)) { 978275acaafSTrond Myklebust int ret2; 979275acaafSTrond Myklebust 9806c75dc0dSFred Isaman data = list_first_entry(head, struct nfs_write_data, list); 981275acaafSTrond Myklebust list_del_init(&data->list); 982275acaafSTrond Myklebust 983dce81290STrond Myklebust ret2 = nfs_do_write(data, call_ops, how); 984275acaafSTrond Myklebust if (ret == 0) 985275acaafSTrond Myklebust ret = ret2; 986275acaafSTrond Myklebust } 987275acaafSTrond Myklebust return ret; 988275acaafSTrond Myklebust } 989275acaafSTrond Myklebust 9906d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and 9916d884e8fSFred * call this on each, which will prepare them to be retried on next 9926d884e8fSFred * writeback using standard nfs. 9936d884e8fSFred */ 9946d884e8fSFred static void nfs_redirty_request(struct nfs_page *req) 9956d884e8fSFred { 996a6305ddbSTrond Myklebust struct page *page = req->wb_page; 997a6305ddbSTrond Myklebust 9986d884e8fSFred nfs_mark_request_dirty(req); 9999994b62bSFred Isaman nfs_unlock_request(req); 1000a6305ddbSTrond Myklebust nfs_end_page_writeback(page); 10016d884e8fSFred } 10026d884e8fSFred 10036c75dc0dSFred Isaman void nfs_async_write_error(struct list_head *head) 10046c75dc0dSFred Isaman { 10056c75dc0dSFred Isaman struct nfs_page *req; 10066c75dc0dSFred Isaman 10076c75dc0dSFred Isaman while (!list_empty(head)) { 10086c75dc0dSFred Isaman req = nfs_list_entry(head->next); 10096c75dc0dSFred Isaman nfs_list_remove_request(req); 10106c75dc0dSFred Isaman nfs_redirty_request(req); 10116c75dc0dSFred Isaman } 10126c75dc0dSFred Isaman } 10136c75dc0dSFred Isaman 10141da177e4SLinus Torvalds /* 10151da177e4SLinus Torvalds * Generate multiple small requests to write out a single 10161da177e4SLinus Torvalds * contiguous dirty area on one page. 10171da177e4SLinus Torvalds */ 10186c75dc0dSFred Isaman static int nfs_flush_multi(struct nfs_pageio_descriptor *desc, 10196c75dc0dSFred Isaman struct nfs_pgio_header *hdr) 10201da177e4SLinus Torvalds { 10216c75dc0dSFred Isaman struct nfs_page *req = hdr->req; 10221da177e4SLinus Torvalds struct page *page = req->wb_page; 10231da177e4SLinus Torvalds struct nfs_write_data *data; 1024d097971dSTrond Myklebust size_t wsize = desc->pg_bsize, nbytes; 1025e9f7bee1STrond Myklebust unsigned int offset; 10261da177e4SLinus Torvalds int requests = 0; 1027dbae4c73STrond Myklebust int ret = 0; 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds nfs_list_remove_request(req); 10306c75dc0dSFred Isaman nfs_list_add_request(req, &hdr->pages); 10311da177e4SLinus Torvalds 1032b31268acSTrond Myklebust if ((desc->pg_ioflags & FLUSH_COND_STABLE) && 1033b31268acSTrond Myklebust (desc->pg_moreio || NFS_I(desc->pg_inode)->ncommit || 1034b31268acSTrond Myklebust desc->pg_count > wsize)) 1035b31268acSTrond Myklebust desc->pg_ioflags &= ~FLUSH_COND_STABLE; 1036b31268acSTrond Myklebust 1037b31268acSTrond Myklebust 1038275acaafSTrond Myklebust offset = 0; 1039c76069bdSFred Isaman nbytes = desc->pg_count; 1040e9f7bee1STrond Myklebust do { 1041e9f7bee1STrond Myklebust size_t len = min(nbytes, wsize); 1042e9f7bee1STrond Myklebust 10436c75dc0dSFred Isaman data = nfs_writedata_alloc(hdr, 1); 10446c75dc0dSFred Isaman if (!data) 10451da177e4SLinus Torvalds goto out_bad; 104630dd374fSFred Isaman data->pages.pagevec[0] = page; 10476c75dc0dSFred Isaman nfs_write_rpcsetup(data, len, offset, desc->pg_ioflags); 10486c75dc0dSFred Isaman list_add(&data->list, &hdr->rpc_list); 10491da177e4SLinus Torvalds requests++; 1050e9f7bee1STrond Myklebust nbytes -= len; 1051275acaafSTrond Myklebust offset += len; 1052e9f7bee1STrond Myklebust } while (nbytes != 0); 10531da177e4SLinus Torvalds atomic_set(&req->wb_complete, requests); 10546c75dc0dSFred Isaman desc->pg_rpc_callops = &nfs_write_common_ops; 1055dbae4c73STrond Myklebust return ret; 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds out_bad: 10586c75dc0dSFred Isaman while (!list_empty(&hdr->rpc_list)) { 10596c75dc0dSFred Isaman data = list_first_entry(&hdr->rpc_list, struct nfs_write_data, list); 10606e4efd56STrond Myklebust list_del(&data->list); 10618ccd271fSFred Isaman nfs_writedata_release(data); 10621da177e4SLinus Torvalds } 10636c75dc0dSFred Isaman nfs_async_write_error(&hdr->pages); 10641da177e4SLinus Torvalds return -ENOMEM; 10651da177e4SLinus Torvalds } 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds /* 10681da177e4SLinus Torvalds * Create an RPC task for the given write request and kick it. 10691da177e4SLinus Torvalds * The page must have been locked by the caller. 10701da177e4SLinus Torvalds * 10711da177e4SLinus Torvalds * It may happen that the page we're passed is not marked dirty. 10721da177e4SLinus Torvalds * This is the case if nfs_updatepage detects a conflicting request 10731da177e4SLinus Torvalds * that has been written but not committed. 10741da177e4SLinus Torvalds */ 10756c75dc0dSFred Isaman static int nfs_flush_one(struct nfs_pageio_descriptor *desc, 10766c75dc0dSFred Isaman struct nfs_pgio_header *hdr) 10771da177e4SLinus Torvalds { 10781da177e4SLinus Torvalds struct nfs_page *req; 10791da177e4SLinus Torvalds struct page **pages; 10801da177e4SLinus Torvalds struct nfs_write_data *data; 1081c76069bdSFred Isaman struct list_head *head = &desc->pg_list; 10823b609184SPeng Tao int ret = 0; 10831da177e4SLinus Torvalds 10846c75dc0dSFred Isaman data = nfs_writedata_alloc(hdr, nfs_page_array_len(desc->pg_base, 1085c76069bdSFred Isaman desc->pg_count)); 10866c75dc0dSFred Isaman if (!data) { 10876c75dc0dSFred Isaman nfs_async_write_error(head); 108844b83799SFred Isaman ret = -ENOMEM; 108944b83799SFred Isaman goto out; 109044b83799SFred Isaman } 10916c75dc0dSFred Isaman 109230dd374fSFred Isaman pages = data->pages.pagevec; 10931da177e4SLinus Torvalds while (!list_empty(head)) { 10941da177e4SLinus Torvalds req = nfs_list_entry(head->next); 10951da177e4SLinus Torvalds nfs_list_remove_request(req); 10966c75dc0dSFred Isaman nfs_list_add_request(req, &hdr->pages); 10971da177e4SLinus Torvalds *pages++ = req->wb_page; 10981da177e4SLinus Torvalds } 10991da177e4SLinus Torvalds 1100b31268acSTrond Myklebust if ((desc->pg_ioflags & FLUSH_COND_STABLE) && 1101b31268acSTrond Myklebust (desc->pg_moreio || NFS_I(desc->pg_inode)->ncommit)) 1102b31268acSTrond Myklebust desc->pg_ioflags &= ~FLUSH_COND_STABLE; 1103b31268acSTrond Myklebust 11041da177e4SLinus Torvalds /* Set up the argument struct */ 11056c75dc0dSFred Isaman nfs_write_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags); 11066c75dc0dSFred Isaman list_add(&data->list, &hdr->rpc_list); 11076c75dc0dSFred Isaman desc->pg_rpc_callops = &nfs_write_common_ops; 110844b83799SFred Isaman out: 110944b83799SFred Isaman return ret; 11101da177e4SLinus Torvalds } 11111da177e4SLinus Torvalds 11126c75dc0dSFred Isaman int nfs_generic_flush(struct nfs_pageio_descriptor *desc, 11136c75dc0dSFred Isaman struct nfs_pgio_header *hdr) 1114dce81290STrond Myklebust { 1115dce81290STrond Myklebust if (desc->pg_bsize < PAGE_CACHE_SIZE) 11166c75dc0dSFred Isaman return nfs_flush_multi(desc, hdr); 11176c75dc0dSFred Isaman return nfs_flush_one(desc, hdr); 1118dce81290STrond Myklebust } 1119dce81290STrond Myklebust 1120dce81290STrond Myklebust static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc) 11211751c363STrond Myklebust { 11226c75dc0dSFred Isaman struct nfs_write_header *whdr; 11236c75dc0dSFred Isaman struct nfs_pgio_header *hdr; 1124275acaafSTrond Myklebust int ret; 1125275acaafSTrond Myklebust 11266c75dc0dSFred Isaman whdr = nfs_writehdr_alloc(); 11276c75dc0dSFred Isaman if (!whdr) { 11286c75dc0dSFred Isaman nfs_async_write_error(&desc->pg_list); 11296c75dc0dSFred Isaman return -ENOMEM; 11306c75dc0dSFred Isaman } 11316c75dc0dSFred Isaman hdr = &whdr->header; 11326c75dc0dSFred Isaman nfs_pgheader_init(desc, hdr, nfs_writehdr_free); 11336c75dc0dSFred Isaman atomic_inc(&hdr->refcnt); 11346c75dc0dSFred Isaman ret = nfs_generic_flush(desc, hdr); 1135275acaafSTrond Myklebust if (ret == 0) 11366c75dc0dSFred Isaman ret = nfs_do_multiple_writes(&hdr->rpc_list, 11376c75dc0dSFred Isaman desc->pg_rpc_callops, 1138dce81290STrond Myklebust desc->pg_ioflags); 11396c75dc0dSFred Isaman else 11406c75dc0dSFred Isaman set_bit(NFS_IOHDR_REDO, &hdr->flags); 11416c75dc0dSFred Isaman if (atomic_dec_and_test(&hdr->refcnt)) 11426c75dc0dSFred Isaman nfs_write_completion(hdr); 1143275acaafSTrond Myklebust return ret; 11441751c363STrond Myklebust } 11451751c363STrond Myklebust 11461751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_write_ops = { 11471751c363STrond Myklebust .pg_test = nfs_generic_pg_test, 11481751c363STrond Myklebust .pg_doio = nfs_generic_pg_writepages, 11491751c363STrond Myklebust }; 11501751c363STrond Myklebust 1151e2fecb21STrond Myklebust void nfs_pageio_init_write_mds(struct nfs_pageio_descriptor *pgio, 11521751c363STrond Myklebust struct inode *inode, int ioflags) 11531751c363STrond Myklebust { 11541751c363STrond Myklebust nfs_pageio_init(pgio, inode, &nfs_pageio_write_ops, 11551751c363STrond Myklebust NFS_SERVER(inode)->wsize, ioflags); 11561751c363STrond Myklebust } 11571751c363STrond Myklebust 1158dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio) 1159dce81290STrond Myklebust { 1160dce81290STrond Myklebust pgio->pg_ops = &nfs_pageio_write_ops; 1161dce81290STrond Myklebust pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize; 1162dce81290STrond Myklebust } 11631f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds); 1164dce81290STrond Myklebust 1165c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, 1166c63c7b05STrond Myklebust struct inode *inode, int ioflags) 11671da177e4SLinus Torvalds { 11681751c363STrond Myklebust if (!pnfs_pageio_init_write(pgio, inode, ioflags)) 11691751c363STrond Myklebust nfs_pageio_init_write_mds(pgio, inode, ioflags); 11701da177e4SLinus Torvalds } 11711da177e4SLinus Torvalds 1172def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata) 1173def6ed7eSAndy Adamson { 1174def6ed7eSAndy Adamson struct nfs_write_data *data = calldata; 1175cd841605SFred Isaman NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data); 1176def6ed7eSAndy Adamson } 1177def6ed7eSAndy Adamson 11780b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata) 11790b7c0153SFred Isaman { 11800b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 11810b7c0153SFred Isaman 11820b7c0153SFred Isaman NFS_PROTO(data->inode)->commit_rpc_prepare(task, data); 11830b7c0153SFred Isaman } 11840b7c0153SFred Isaman 11851da177e4SLinus Torvalds /* 11861da177e4SLinus Torvalds * Handle a write reply that flushes a whole page. 11871da177e4SLinus Torvalds * 11881da177e4SLinus Torvalds * FIXME: There is an inherent race with invalidate_inode_pages and 11891da177e4SLinus Torvalds * writebacks since the page->count is kept > 1 for as long 11901da177e4SLinus Torvalds * as the page has a write request pending. 11911da177e4SLinus Torvalds */ 11926c75dc0dSFred Isaman static void nfs_writeback_done_common(struct rpc_task *task, void *calldata) 11931da177e4SLinus Torvalds { 1194788e7a89STrond Myklebust struct nfs_write_data *data = calldata; 11951da177e4SLinus Torvalds 1196c9d8f89dSTrond Myklebust nfs_writeback_done(task, data); 1197c9d8f89dSTrond Myklebust } 1198c9d8f89dSTrond Myklebust 11996c75dc0dSFred Isaman static void nfs_writeback_release_common(void *calldata) 1200c9d8f89dSTrond Myklebust { 1201c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1202cd841605SFred Isaman struct nfs_pgio_header *hdr = data->header; 1203e2fecb21STrond Myklebust int status = data->task.tk_status; 12046c75dc0dSFred Isaman struct nfs_page *req = hdr->req; 1205788e7a89STrond Myklebust 12066c75dc0dSFred Isaman if ((status >= 0) && nfs_write_need_commit(data)) { 12076c75dc0dSFred Isaman spin_lock(&hdr->lock); 12086c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) 12096c75dc0dSFred Isaman ; /* Do nothing */ 12106c75dc0dSFred Isaman else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) 12111da177e4SLinus Torvalds memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); 12126c75dc0dSFred Isaman else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) 12136c75dc0dSFred Isaman set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags); 12146c75dc0dSFred Isaman spin_unlock(&hdr->lock); 12151da177e4SLinus Torvalds } 1216cd841605SFred Isaman nfs_writedata_release(data); 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds 12196c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops = { 1220def6ed7eSAndy Adamson .rpc_call_prepare = nfs_write_prepare, 12216c75dc0dSFred Isaman .rpc_call_done = nfs_writeback_done_common, 12226c75dc0dSFred Isaman .rpc_release = nfs_writeback_release_common, 1223788e7a89STrond Myklebust }; 1224788e7a89STrond Myklebust 1225788e7a89STrond Myklebust 12261da177e4SLinus Torvalds /* 12271da177e4SLinus Torvalds * This function is called when the WRITE call is complete. 12281da177e4SLinus Torvalds */ 122913602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data) 12301da177e4SLinus Torvalds { 12311da177e4SLinus Torvalds struct nfs_writeargs *argp = &data->args; 12321da177e4SLinus Torvalds struct nfs_writeres *resp = &data->res; 1233cd841605SFred Isaman struct inode *inode = data->header->inode; 1234788e7a89STrond Myklebust int status; 12351da177e4SLinus Torvalds 1236a3f565b1SChuck Lever dprintk("NFS: %5u nfs_writeback_done (status %d)\n", 12371da177e4SLinus Torvalds task->tk_pid, task->tk_status); 12381da177e4SLinus Torvalds 1239f551e44fSChuck Lever /* 1240f551e44fSChuck Lever * ->write_done will attempt to use post-op attributes to detect 1241f551e44fSChuck Lever * conflicting writes by other clients. A strict interpretation 1242f551e44fSChuck Lever * of close-to-open would allow us to continue caching even if 1243f551e44fSChuck Lever * another writer had changed the file, but some applications 1244f551e44fSChuck Lever * depend on tighter cache coherency when writing. 1245f551e44fSChuck Lever */ 1246cd841605SFred Isaman status = NFS_PROTO(inode)->write_done(task, data); 1247788e7a89STrond Myklebust if (status != 0) 124813602896SFred Isaman return; 1249cd841605SFred Isaman nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count); 125091d5b470SChuck Lever 12511da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 12521da177e4SLinus Torvalds if (resp->verf->committed < argp->stable && task->tk_status >= 0) { 12531da177e4SLinus Torvalds /* We tried a write call, but the server did not 12541da177e4SLinus Torvalds * commit data to stable storage even though we 12551da177e4SLinus Torvalds * requested it. 12561da177e4SLinus Torvalds * Note: There is a known bug in Tru64 < 5.0 in which 12571da177e4SLinus Torvalds * the server reports NFS_DATA_SYNC, but performs 12581da177e4SLinus Torvalds * NFS_FILE_SYNC. We therefore implement this checking 12591da177e4SLinus Torvalds * as a dprintk() in order to avoid filling syslog. 12601da177e4SLinus Torvalds */ 12611da177e4SLinus Torvalds static unsigned long complain; 12621da177e4SLinus Torvalds 1263a69aef14SFred Isaman /* Note this will print the MDS for a DS write */ 12641da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 12651da177e4SLinus Torvalds dprintk("NFS: faulty NFS server %s:" 12661da177e4SLinus Torvalds " (committed = %d) != (stable = %d)\n", 1267cd841605SFred Isaman NFS_SERVER(inode)->nfs_client->cl_hostname, 12681da177e4SLinus Torvalds resp->verf->committed, argp->stable); 12691da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 12701da177e4SLinus Torvalds } 12711da177e4SLinus Torvalds } 12721da177e4SLinus Torvalds #endif 12736c75dc0dSFred Isaman if (task->tk_status < 0) 12746c75dc0dSFred Isaman nfs_set_pgio_error(data->header, task->tk_status, argp->offset); 12756c75dc0dSFred Isaman else if (resp->count < argp->count) { 12761da177e4SLinus Torvalds static unsigned long complain; 12771da177e4SLinus Torvalds 12786c75dc0dSFred Isaman /* This a short write! */ 1279cd841605SFred Isaman nfs_inc_stats(inode, NFSIOS_SHORTWRITE); 128091d5b470SChuck Lever 12811da177e4SLinus Torvalds /* Has the server at least made some progress? */ 12826c75dc0dSFred Isaman if (resp->count == 0) { 12836c75dc0dSFred Isaman if (time_before(complain, jiffies)) { 12846c75dc0dSFred Isaman printk(KERN_WARNING 12856c75dc0dSFred Isaman "NFS: Server wrote zero bytes, expected %u.\n", 12866c75dc0dSFred Isaman argp->count); 12876c75dc0dSFred Isaman complain = jiffies + 300 * HZ; 12886c75dc0dSFred Isaman } 12896c75dc0dSFred Isaman nfs_set_pgio_error(data->header, -EIO, argp->offset); 12906c75dc0dSFred Isaman task->tk_status = -EIO; 12916c75dc0dSFred Isaman return; 12926c75dc0dSFred Isaman } 12931da177e4SLinus Torvalds /* Was this an NFSv2 write or an NFSv3 stable write? */ 12941da177e4SLinus Torvalds if (resp->verf->committed != NFS_UNSTABLE) { 12951da177e4SLinus Torvalds /* Resend from where the server left off */ 1296a69aef14SFred Isaman data->mds_offset += resp->count; 12971da177e4SLinus Torvalds argp->offset += resp->count; 12981da177e4SLinus Torvalds argp->pgbase += resp->count; 12991da177e4SLinus Torvalds argp->count -= resp->count; 13001da177e4SLinus Torvalds } else { 13011da177e4SLinus Torvalds /* Resend as a stable write in order to avoid 13021da177e4SLinus Torvalds * headaches in the case of a server crash. 13031da177e4SLinus Torvalds */ 13041da177e4SLinus Torvalds argp->stable = NFS_FILE_SYNC; 13051da177e4SLinus Torvalds } 1306d00c5d43STrond Myklebust rpc_restart_call_prepare(task); 13071da177e4SLinus Torvalds } 13081da177e4SLinus Torvalds } 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds 13111da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 131271d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait) 131371d0a611STrond Myklebust { 1314b8413f98STrond Myklebust int ret; 1315b8413f98STrond Myklebust 131671d0a611STrond Myklebust if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags)) 131771d0a611STrond Myklebust return 1; 1318b8413f98STrond Myklebust if (!may_wait) 131971d0a611STrond Myklebust return 0; 1320b8413f98STrond Myklebust ret = out_of_line_wait_on_bit_lock(&nfsi->flags, 1321b8413f98STrond Myklebust NFS_INO_COMMIT, 1322b8413f98STrond Myklebust nfs_wait_bit_killable, 1323b8413f98STrond Myklebust TASK_KILLABLE); 1324b8413f98STrond Myklebust return (ret < 0) ? ret : 1; 132571d0a611STrond Myklebust } 132671d0a611STrond Myklebust 1327e0c2b380SFred Isaman void nfs_commit_clear_lock(struct nfs_inode *nfsi) 132871d0a611STrond Myklebust { 132971d0a611STrond Myklebust clear_bit(NFS_INO_COMMIT, &nfsi->flags); 133071d0a611STrond Myklebust smp_mb__after_clear_bit(); 133171d0a611STrond Myklebust wake_up_bit(&nfsi->flags, NFS_INO_COMMIT); 133271d0a611STrond Myklebust } 1333e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_clear_lock); 133471d0a611STrond Myklebust 13350b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data) 13361da177e4SLinus Torvalds { 13370b7c0153SFred Isaman put_nfs_open_context(data->context); 13380b7c0153SFred Isaman nfs_commit_free(data); 13391da177e4SLinus Torvalds } 1340e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release); 13411da177e4SLinus Torvalds 13420b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data, 13439ace33cdSFred Isaman const struct rpc_call_ops *call_ops, 1344788e7a89STrond Myklebust int how) 13451da177e4SLinus Torvalds { 134607737691STrond Myklebust struct rpc_task *task; 13479ace33cdSFred Isaman int priority = flush_task_priority(how); 1348bdc7f021STrond Myklebust struct rpc_message msg = { 1349bdc7f021STrond Myklebust .rpc_argp = &data->args, 1350bdc7f021STrond Myklebust .rpc_resp = &data->res, 13519ace33cdSFred Isaman .rpc_cred = data->cred, 1352bdc7f021STrond Myklebust }; 135384115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 135407737691STrond Myklebust .task = &data->task, 13559ace33cdSFred Isaman .rpc_client = clnt, 1356bdc7f021STrond Myklebust .rpc_message = &msg, 13579ace33cdSFred Isaman .callback_ops = call_ops, 135884115e1cSTrond Myklebust .callback_data = data, 1359101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 13602c61be0aSTrond Myklebust .flags = RPC_TASK_ASYNC, 13613ff7576dSTrond Myklebust .priority = priority, 136284115e1cSTrond Myklebust }; 1363788e7a89STrond Myklebust /* Set up the initial task struct. */ 13649ace33cdSFred Isaman NFS_PROTO(data->inode)->commit_setup(data, &msg); 13651da177e4SLinus Torvalds 1366a3f565b1SChuck Lever dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); 1367bdc7f021STrond Myklebust 136807737691STrond Myklebust task = rpc_run_task(&task_setup_data); 1369dbae4c73STrond Myklebust if (IS_ERR(task)) 1370dbae4c73STrond Myklebust return PTR_ERR(task); 1371d2224e7aSJeff Layton if (how & FLUSH_SYNC) 1372d2224e7aSJeff Layton rpc_wait_for_completion_task(task); 137307737691STrond Myklebust rpc_put_task(task); 1374dbae4c73STrond Myklebust return 0; 13751da177e4SLinus Torvalds } 1376e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit); 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds /* 13799ace33cdSFred Isaman * Set up the argument/result storage required for the RPC call. 13809ace33cdSFred Isaman */ 13810b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data, 1382988b6dceSFred Isaman struct list_head *head, 1383988b6dceSFred Isaman struct pnfs_layout_segment *lseg) 13849ace33cdSFred Isaman { 13859ace33cdSFred Isaman struct nfs_page *first = nfs_list_entry(head->next); 13863d4ff43dSAl Viro struct inode *inode = first->wb_context->dentry->d_inode; 13879ace33cdSFred Isaman 13889ace33cdSFred Isaman /* Set up the RPC argument and reply structs 13899ace33cdSFred Isaman * NB: take care not to mess about with data->commit et al. */ 13909ace33cdSFred Isaman 13919ace33cdSFred Isaman list_splice_init(head, &data->pages); 13929ace33cdSFred Isaman 13939ace33cdSFred Isaman data->inode = inode; 13949ace33cdSFred Isaman data->cred = first->wb_context->cred; 1395988b6dceSFred Isaman data->lseg = lseg; /* reference transferred */ 13969ace33cdSFred Isaman data->mds_ops = &nfs_commit_ops; 13979ace33cdSFred Isaman 13989ace33cdSFred Isaman data->args.fh = NFS_FH(data->inode); 13999ace33cdSFred Isaman /* Note: we always request a commit of the entire inode */ 14009ace33cdSFred Isaman data->args.offset = 0; 14019ace33cdSFred Isaman data->args.count = 0; 14020b7c0153SFred Isaman data->context = get_nfs_open_context(first->wb_context); 14039ace33cdSFred Isaman data->res.fattr = &data->fattr; 14049ace33cdSFred Isaman data->res.verf = &data->verf; 14059ace33cdSFred Isaman nfs_fattr_init(&data->fattr); 14069ace33cdSFred Isaman } 1407e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit); 14089ace33cdSFred Isaman 1409e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list, 1410a861a1e1SFred Isaman struct pnfs_layout_segment *lseg) 141164bfeb49SFred Isaman { 141264bfeb49SFred Isaman struct nfs_page *req; 141364bfeb49SFred Isaman 141464bfeb49SFred Isaman while (!list_empty(page_list)) { 141564bfeb49SFred Isaman req = nfs_list_entry(page_list->next); 141664bfeb49SFred Isaman nfs_list_remove_request(req); 1417a861a1e1SFred Isaman nfs_mark_request_commit(req, lseg); 141864bfeb49SFred Isaman dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 141964bfeb49SFred Isaman dec_bdi_stat(req->wb_page->mapping->backing_dev_info, 142064bfeb49SFred Isaman BDI_RECLAIMABLE); 14219994b62bSFred Isaman nfs_unlock_request(req); 142264bfeb49SFred Isaman } 142364bfeb49SFred Isaman } 1424e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit); 142564bfeb49SFred Isaman 14269ace33cdSFred Isaman /* 14271da177e4SLinus Torvalds * Commit dirty pages 14281da177e4SLinus Torvalds */ 14291da177e4SLinus Torvalds static int 143040859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how) 14311da177e4SLinus Torvalds { 14320b7c0153SFred Isaman struct nfs_commit_data *data; 14331da177e4SLinus Torvalds 1434c9d8f89dSTrond Myklebust data = nfs_commitdata_alloc(); 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds if (!data) 14371da177e4SLinus Torvalds goto out_bad; 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds /* Set up the argument struct */ 1440988b6dceSFred Isaman nfs_init_commit(data, head, NULL); 14410b7c0153SFred Isaman return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops, how); 14421da177e4SLinus Torvalds out_bad: 1443a861a1e1SFred Isaman nfs_retry_commit(head, NULL); 144471d0a611STrond Myklebust nfs_commit_clear_lock(NFS_I(inode)); 14451da177e4SLinus Torvalds return -ENOMEM; 14461da177e4SLinus Torvalds } 14471da177e4SLinus Torvalds 14481da177e4SLinus Torvalds /* 14491da177e4SLinus Torvalds * COMMIT call returned 14501da177e4SLinus Torvalds */ 1451788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata) 14521da177e4SLinus Torvalds { 14530b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 14541da177e4SLinus Torvalds 1455a3f565b1SChuck Lever dprintk("NFS: %5u nfs_commit_done (status %d)\n", 14561da177e4SLinus Torvalds task->tk_pid, task->tk_status); 14571da177e4SLinus Torvalds 1458788e7a89STrond Myklebust /* Call the NFS version-specific code */ 1459c0d0e96bSTrond Myklebust NFS_PROTO(data->inode)->commit_done(task, data); 1460c9d8f89dSTrond Myklebust } 1461c9d8f89dSTrond Myklebust 14620b7c0153SFred Isaman void nfs_commit_release_pages(struct nfs_commit_data *data) 1463c9d8f89dSTrond Myklebust { 1464c9d8f89dSTrond Myklebust struct nfs_page *req; 1465c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1466788e7a89STrond Myklebust 14671da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 14681da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 14691da177e4SLinus Torvalds nfs_list_remove_request(req); 1470d6d6dc7cSFred Isaman nfs_clear_page_commit(req->wb_page); 14711da177e4SLinus Torvalds 147248186c7dSChuck Lever dprintk("NFS: commit (%s/%lld %d@%lld)", 14733d4ff43dSAl Viro req->wb_context->dentry->d_sb->s_id, 14743d4ff43dSAl Viro (long long)NFS_FILEID(req->wb_context->dentry->d_inode), 14751da177e4SLinus Torvalds req->wb_bytes, 14761da177e4SLinus Torvalds (long long)req_offset(req)); 1477c9d8f89dSTrond Myklebust if (status < 0) { 1478c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 14791da177e4SLinus Torvalds nfs_inode_remove_request(req); 1480c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 14811da177e4SLinus Torvalds goto next; 14821da177e4SLinus Torvalds } 14831da177e4SLinus Torvalds 14841da177e4SLinus Torvalds /* Okay, COMMIT succeeded, apparently. Check the verifier 14851da177e4SLinus Torvalds * returned by the server against all stored verfs. */ 14861da177e4SLinus Torvalds if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) { 14871da177e4SLinus Torvalds /* We have a match */ 14881da177e4SLinus Torvalds nfs_inode_remove_request(req); 14891da177e4SLinus Torvalds dprintk(" OK\n"); 14901da177e4SLinus Torvalds goto next; 14911da177e4SLinus Torvalds } 14921da177e4SLinus Torvalds /* We have a mismatch. Write the page again */ 14931da177e4SLinus Torvalds dprintk(" mismatch\n"); 14946d884e8fSFred nfs_mark_request_dirty(req); 14951da177e4SLinus Torvalds next: 14969994b62bSFred Isaman nfs_unlock_request(req); 14971da177e4SLinus Torvalds } 14985917ce84SFred Isaman } 1499e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_release_pages); 15005917ce84SFred Isaman 15015917ce84SFred Isaman static void nfs_commit_release(void *calldata) 15025917ce84SFred Isaman { 15030b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 15045917ce84SFred Isaman 15055917ce84SFred Isaman nfs_commit_release_pages(data); 150671d0a611STrond Myklebust nfs_commit_clear_lock(NFS_I(data->inode)); 1507c9d8f89dSTrond Myklebust nfs_commitdata_release(calldata); 15081da177e4SLinus Torvalds } 1509788e7a89STrond Myklebust 1510788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = { 15110b7c0153SFred Isaman .rpc_call_prepare = nfs_commit_prepare, 1512788e7a89STrond Myklebust .rpc_call_done = nfs_commit_done, 1513788e7a89STrond Myklebust .rpc_release = nfs_commit_release, 1514788e7a89STrond Myklebust }; 15151da177e4SLinus Torvalds 1516b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how) 15171da177e4SLinus Torvalds { 15181da177e4SLinus Torvalds LIST_HEAD(head); 151971d0a611STrond Myklebust int may_wait = how & FLUSH_SYNC; 1520b8413f98STrond Myklebust int res; 15211da177e4SLinus Torvalds 1522b8413f98STrond Myklebust res = nfs_commit_set_lock(NFS_I(inode), may_wait); 1523b8413f98STrond Myklebust if (res <= 0) 1524c5efa5fcSTrond Myklebust goto out_mark_dirty; 1525d6d6dc7cSFred Isaman res = nfs_scan_commit(inode, &head); 15261da177e4SLinus Torvalds if (res) { 1527a861a1e1SFred Isaman int error; 1528a861a1e1SFred Isaman 1529a861a1e1SFred Isaman error = pnfs_commit_list(inode, &head, how); 1530a861a1e1SFred Isaman if (error == PNFS_NOT_ATTEMPTED) 1531a861a1e1SFred Isaman error = nfs_commit_list(inode, &head, how); 15321da177e4SLinus Torvalds if (error < 0) 15331da177e4SLinus Torvalds return error; 1534b8413f98STrond Myklebust if (!may_wait) 1535b8413f98STrond Myklebust goto out_mark_dirty; 1536b8413f98STrond Myklebust error = wait_on_bit(&NFS_I(inode)->flags, 1537b8413f98STrond Myklebust NFS_INO_COMMIT, 153871d0a611STrond Myklebust nfs_wait_bit_killable, 153971d0a611STrond Myklebust TASK_KILLABLE); 1540b8413f98STrond Myklebust if (error < 0) 1541b8413f98STrond Myklebust return error; 154271d0a611STrond Myklebust } else 154371d0a611STrond Myklebust nfs_commit_clear_lock(NFS_I(inode)); 1544c5efa5fcSTrond Myklebust return res; 1545c5efa5fcSTrond Myklebust /* Note: If we exit without ensuring that the commit is complete, 1546c5efa5fcSTrond Myklebust * we must mark the inode as dirty. Otherwise, future calls to 1547c5efa5fcSTrond Myklebust * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure 1548c5efa5fcSTrond Myklebust * that the data is on the disk. 1549c5efa5fcSTrond Myklebust */ 1550c5efa5fcSTrond Myklebust out_mark_dirty: 1551c5efa5fcSTrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 15521da177e4SLinus Torvalds return res; 15531da177e4SLinus Torvalds } 15548fc795f7STrond Myklebust 15558fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 15568fc795f7STrond Myklebust { 1557420e3646STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 1558420e3646STrond Myklebust int flags = FLUSH_SYNC; 1559420e3646STrond Myklebust int ret = 0; 15608fc795f7STrond Myklebust 15613236c3e1SJeff Layton /* no commits means nothing needs to be done */ 15623236c3e1SJeff Layton if (!nfsi->ncommit) 15633236c3e1SJeff Layton return ret; 15643236c3e1SJeff Layton 1565a00dd6c0SJeff Layton if (wbc->sync_mode == WB_SYNC_NONE) { 1566a00dd6c0SJeff Layton /* Don't commit yet if this is a non-blocking flush and there 1567a00dd6c0SJeff Layton * are a lot of outstanding writes for this mapping. 1568420e3646STrond Myklebust */ 1569a00dd6c0SJeff Layton if (nfsi->ncommit <= (nfsi->npages >> 1)) 1570420e3646STrond Myklebust goto out_mark_dirty; 1571420e3646STrond Myklebust 1572a00dd6c0SJeff Layton /* don't wait for the COMMIT response */ 1573420e3646STrond Myklebust flags = 0; 1574a00dd6c0SJeff Layton } 1575a00dd6c0SJeff Layton 1576420e3646STrond Myklebust ret = nfs_commit_inode(inode, flags); 1577420e3646STrond Myklebust if (ret >= 0) { 1578420e3646STrond Myklebust if (wbc->sync_mode == WB_SYNC_NONE) { 1579420e3646STrond Myklebust if (ret < wbc->nr_to_write) 1580420e3646STrond Myklebust wbc->nr_to_write -= ret; 1581420e3646STrond Myklebust else 1582420e3646STrond Myklebust wbc->nr_to_write = 0; 1583420e3646STrond Myklebust } 15848fc795f7STrond Myklebust return 0; 1585420e3646STrond Myklebust } 1586420e3646STrond Myklebust out_mark_dirty: 15878fc795f7STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 15888fc795f7STrond Myklebust return ret; 15898fc795f7STrond Myklebust } 1590c63c7b05STrond Myklebust #else 15918fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 15928fc795f7STrond Myklebust { 15938fc795f7STrond Myklebust return 0; 15948fc795f7STrond Myklebust } 15951da177e4SLinus Torvalds #endif 15961da177e4SLinus Torvalds 15978fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc) 15988fc795f7STrond Myklebust { 1599863a3c6cSAndy Adamson int ret; 1600863a3c6cSAndy Adamson 1601863a3c6cSAndy Adamson ret = nfs_commit_unstable_pages(inode, wbc); 1602863a3c6cSAndy Adamson if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) { 1603ef311537SAndy Adamson int status; 1604ef311537SAndy Adamson bool sync = true; 1605863a3c6cSAndy Adamson 1606846d5a09SWu Fengguang if (wbc->sync_mode == WB_SYNC_NONE) 1607ef311537SAndy Adamson sync = false; 1608863a3c6cSAndy Adamson 1609863a3c6cSAndy Adamson status = pnfs_layoutcommit_inode(inode, sync); 1610863a3c6cSAndy Adamson if (status < 0) 1611863a3c6cSAndy Adamson return status; 1612863a3c6cSAndy Adamson } 1613863a3c6cSAndy Adamson return ret; 16148fc795f7STrond Myklebust } 16158fc795f7STrond Myklebust 1616acdc53b2STrond Myklebust /* 1617acdc53b2STrond Myklebust * flush the inode to disk. 1618acdc53b2STrond Myklebust */ 1619acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode) 162034901f70STrond Myklebust { 162134901f70STrond Myklebust struct writeback_control wbc = { 162272cb77f4STrond Myklebust .sync_mode = WB_SYNC_ALL, 162334901f70STrond Myklebust .nr_to_write = LONG_MAX, 1624d7fb1207STrond Myklebust .range_start = 0, 1625d7fb1207STrond Myklebust .range_end = LLONG_MAX, 162634901f70STrond Myklebust }; 162734901f70STrond Myklebust 1628acdc53b2STrond Myklebust return sync_inode(inode, &wbc); 16291c75950bSTrond Myklebust } 16301c75950bSTrond Myklebust 16311b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page) 16321b3b4a1aSTrond Myklebust { 16331b3b4a1aSTrond Myklebust struct nfs_page *req; 16341b3b4a1aSTrond Myklebust int ret = 0; 16351b3b4a1aSTrond Myklebust 16361b3b4a1aSTrond Myklebust BUG_ON(!PageLocked(page)); 16371b3b4a1aSTrond Myklebust for (;;) { 1638ba8b06e6STrond Myklebust wait_on_page_writeback(page); 16391b3b4a1aSTrond Myklebust req = nfs_page_find_request(page); 16401b3b4a1aSTrond Myklebust if (req == NULL) 16411b3b4a1aSTrond Myklebust break; 16421b3b4a1aSTrond Myklebust if (nfs_lock_request_dontget(req)) { 16438dd37758STrond Myklebust nfs_clear_request_commit(req); 16441b3b4a1aSTrond Myklebust nfs_inode_remove_request(req); 16451b3b4a1aSTrond Myklebust /* 16461b3b4a1aSTrond Myklebust * In case nfs_inode_remove_request has marked the 16471b3b4a1aSTrond Myklebust * page as being dirty 16481b3b4a1aSTrond Myklebust */ 16491b3b4a1aSTrond Myklebust cancel_dirty_page(page, PAGE_CACHE_SIZE); 16501b3b4a1aSTrond Myklebust nfs_unlock_request(req); 16511b3b4a1aSTrond Myklebust break; 16521b3b4a1aSTrond Myklebust } 16531b3b4a1aSTrond Myklebust ret = nfs_wait_on_request(req); 1654c9edda71STrond Myklebust nfs_release_request(req); 16551b3b4a1aSTrond Myklebust if (ret < 0) 1656c988950eSTrond Myklebust break; 16571b3b4a1aSTrond Myklebust } 16581b3b4a1aSTrond Myklebust return ret; 16591b3b4a1aSTrond Myklebust } 16601b3b4a1aSTrond Myklebust 16611c75950bSTrond Myklebust /* 16621c75950bSTrond Myklebust * Write back all requests on one page - we do this before reading it. 16631c75950bSTrond Myklebust */ 16641c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page) 16651c75950bSTrond Myklebust { 16667f2f12d9STrond Myklebust loff_t range_start = page_offset(page); 16677f2f12d9STrond Myklebust loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); 16687f2f12d9STrond Myklebust struct writeback_control wbc = { 16697f2f12d9STrond Myklebust .sync_mode = WB_SYNC_ALL, 16707f2f12d9STrond Myklebust .nr_to_write = 0, 16717f2f12d9STrond Myklebust .range_start = range_start, 16727f2f12d9STrond Myklebust .range_end = range_end, 16737f2f12d9STrond Myklebust }; 16747f2f12d9STrond Myklebust int ret; 16757f2f12d9STrond Myklebust 16760522f6adSTrond Myklebust for (;;) { 1677ba8b06e6STrond Myklebust wait_on_page_writeback(page); 16787f2f12d9STrond Myklebust if (clear_page_dirty_for_io(page)) { 16797f2f12d9STrond Myklebust ret = nfs_writepage_locked(page, &wbc); 16807f2f12d9STrond Myklebust if (ret < 0) 16817f2f12d9STrond Myklebust goto out_error; 16820522f6adSTrond Myklebust continue; 16837f2f12d9STrond Myklebust } 16840522f6adSTrond Myklebust if (!PagePrivate(page)) 16850522f6adSTrond Myklebust break; 16860522f6adSTrond Myklebust ret = nfs_commit_inode(inode, FLUSH_SYNC); 16877f2f12d9STrond Myklebust if (ret < 0) 16887f2f12d9STrond Myklebust goto out_error; 16897f2f12d9STrond Myklebust } 16907f2f12d9STrond Myklebust return 0; 16917f2f12d9STrond Myklebust out_error: 16927f2f12d9STrond Myklebust return ret; 16931c75950bSTrond Myklebust } 16941c75950bSTrond Myklebust 1695074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION 1696074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage, 1697a6bc32b8SMel Gorman struct page *page, enum migrate_mode mode) 1698074cc1deSTrond Myklebust { 16992da95652SJeff Layton /* 17002da95652SJeff Layton * If PagePrivate is set, then the page is currently associated with 17012da95652SJeff Layton * an in-progress read or write request. Don't try to migrate it. 17022da95652SJeff Layton * 17032da95652SJeff Layton * FIXME: we could do this in principle, but we'll need a way to ensure 17042da95652SJeff Layton * that we can safely release the inode reference while holding 17052da95652SJeff Layton * the page lock. 17062da95652SJeff Layton */ 17072da95652SJeff Layton if (PagePrivate(page)) 17082da95652SJeff Layton return -EBUSY; 1709074cc1deSTrond Myklebust 1710074cc1deSTrond Myklebust nfs_fscache_release_page(page, GFP_KERNEL); 1711074cc1deSTrond Myklebust 1712a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 1713074cc1deSTrond Myklebust } 1714074cc1deSTrond Myklebust #endif 1715074cc1deSTrond Myklebust 1716f7b422b1SDavid Howells int __init nfs_init_writepagecache(void) 17171da177e4SLinus Torvalds { 17181da177e4SLinus Torvalds nfs_wdata_cachep = kmem_cache_create("nfs_write_data", 1719cd841605SFred Isaman sizeof(struct nfs_write_header), 17201da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 172120c2df83SPaul Mundt NULL); 17221da177e4SLinus Torvalds if (nfs_wdata_cachep == NULL) 17231da177e4SLinus Torvalds return -ENOMEM; 17241da177e4SLinus Torvalds 172593d2341cSMatthew Dobson nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, 17261da177e4SLinus Torvalds nfs_wdata_cachep); 17271da177e4SLinus Torvalds if (nfs_wdata_mempool == NULL) 17281da177e4SLinus Torvalds return -ENOMEM; 17291da177e4SLinus Torvalds 17300b7c0153SFred Isaman nfs_cdata_cachep = kmem_cache_create("nfs_commit_data", 17310b7c0153SFred Isaman sizeof(struct nfs_commit_data), 17320b7c0153SFred Isaman 0, SLAB_HWCACHE_ALIGN, 17330b7c0153SFred Isaman NULL); 17340b7c0153SFred Isaman if (nfs_cdata_cachep == NULL) 17350b7c0153SFred Isaman return -ENOMEM; 17360b7c0153SFred Isaman 173793d2341cSMatthew Dobson nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, 17381da177e4SLinus Torvalds nfs_wdata_cachep); 17391da177e4SLinus Torvalds if (nfs_commit_mempool == NULL) 17401da177e4SLinus Torvalds return -ENOMEM; 17411da177e4SLinus Torvalds 174289a09141SPeter Zijlstra /* 174389a09141SPeter Zijlstra * NFS congestion size, scale with available memory. 174489a09141SPeter Zijlstra * 174589a09141SPeter Zijlstra * 64MB: 8192k 174689a09141SPeter Zijlstra * 128MB: 11585k 174789a09141SPeter Zijlstra * 256MB: 16384k 174889a09141SPeter Zijlstra * 512MB: 23170k 174989a09141SPeter Zijlstra * 1GB: 32768k 175089a09141SPeter Zijlstra * 2GB: 46340k 175189a09141SPeter Zijlstra * 4GB: 65536k 175289a09141SPeter Zijlstra * 8GB: 92681k 175389a09141SPeter Zijlstra * 16GB: 131072k 175489a09141SPeter Zijlstra * 175589a09141SPeter Zijlstra * This allows larger machines to have larger/more transfers. 175689a09141SPeter Zijlstra * Limit the default to 256M 175789a09141SPeter Zijlstra */ 175889a09141SPeter Zijlstra nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); 175989a09141SPeter Zijlstra if (nfs_congestion_kb > 256*1024) 176089a09141SPeter Zijlstra nfs_congestion_kb = 256*1024; 176189a09141SPeter Zijlstra 17621da177e4SLinus Torvalds return 0; 17631da177e4SLinus Torvalds } 17641da177e4SLinus Torvalds 1765266bee88SDavid Brownell void nfs_destroy_writepagecache(void) 17661da177e4SLinus Torvalds { 17671da177e4SLinus Torvalds mempool_destroy(nfs_commit_mempool); 17681da177e4SLinus Torvalds mempool_destroy(nfs_wdata_mempool); 17691a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_wdata_cachep); 17701da177e4SLinus Torvalds } 17711da177e4SLinus Torvalds 1772