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, 43061ae2edSFred Isaman struct inode *inode, int ioflags, 44061ae2edSFred Isaman const struct nfs_pgio_completion_ops *compl_ops); 45f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req); 466c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops; 47788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops; 48061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops; 49*f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops; 501da177e4SLinus Torvalds 51e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep; 523feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool; 530b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep; 541da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool; 551da177e4SLinus Torvalds 560b7c0153SFred Isaman struct nfs_commit_data *nfs_commitdata_alloc(void) 571da177e4SLinus Torvalds { 580b7c0153SFred Isaman struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS); 5940859d7eSChuck Lever 601da177e4SLinus Torvalds if (p) { 611da177e4SLinus Torvalds memset(p, 0, sizeof(*p)); 621da177e4SLinus Torvalds INIT_LIST_HEAD(&p->pages); 631da177e4SLinus Torvalds } 641da177e4SLinus Torvalds return p; 651da177e4SLinus Torvalds } 66e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc); 671da177e4SLinus Torvalds 680b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p) 691da177e4SLinus Torvalds { 701da177e4SLinus Torvalds mempool_free(p, nfs_commit_mempool); 711da177e4SLinus Torvalds } 72e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free); 731da177e4SLinus Torvalds 746c75dc0dSFred Isaman struct nfs_write_header *nfs_writehdr_alloc(void) 753feb2d49STrond Myklebust { 76cd841605SFred Isaman struct nfs_write_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS); 773feb2d49STrond Myklebust 783feb2d49STrond Myklebust if (p) { 79cd841605SFred Isaman struct nfs_pgio_header *hdr = &p->header; 80cd841605SFred Isaman 813feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 82cd841605SFred Isaman INIT_LIST_HEAD(&hdr->pages); 836c75dc0dSFred Isaman INIT_LIST_HEAD(&hdr->rpc_list); 846c75dc0dSFred Isaman spin_lock_init(&hdr->lock); 856c75dc0dSFred Isaman atomic_set(&hdr->refcnt, 0); 863feb2d49STrond Myklebust } 873feb2d49STrond Myklebust return p; 883feb2d49STrond Myklebust } 893feb2d49STrond Myklebust 906c75dc0dSFred Isaman struct nfs_write_data *nfs_writedata_alloc(struct nfs_pgio_header *hdr, 916c75dc0dSFred Isaman unsigned int pagecount) 926c75dc0dSFred Isaman { 936c75dc0dSFred Isaman struct nfs_write_data *data, *prealloc; 946c75dc0dSFred Isaman 956c75dc0dSFred Isaman prealloc = &container_of(hdr, struct nfs_write_header, header)->rpc_data; 966c75dc0dSFred Isaman if (prealloc->header == NULL) 976c75dc0dSFred Isaman data = prealloc; 986c75dc0dSFred Isaman else 996c75dc0dSFred Isaman data = kzalloc(sizeof(*data), GFP_KERNEL); 1006c75dc0dSFred Isaman if (!data) 1016c75dc0dSFred Isaman goto out; 1026c75dc0dSFred Isaman 1036c75dc0dSFred Isaman if (nfs_pgarray_set(&data->pages, pagecount)) { 1046c75dc0dSFred Isaman data->header = hdr; 1056c75dc0dSFred Isaman atomic_inc(&hdr->refcnt); 1066c75dc0dSFred Isaman } else { 1076c75dc0dSFred Isaman if (data != prealloc) 1086c75dc0dSFred Isaman kfree(data); 1096c75dc0dSFred Isaman data = NULL; 1106c75dc0dSFred Isaman } 1116c75dc0dSFred Isaman out: 1126c75dc0dSFred Isaman return data; 1136c75dc0dSFred Isaman } 1146c75dc0dSFred Isaman 115cd841605SFred Isaman void nfs_writehdr_free(struct nfs_pgio_header *hdr) 1163feb2d49STrond Myklebust { 117cd841605SFred Isaman struct nfs_write_header *whdr = container_of(hdr, struct nfs_write_header, header); 118cd841605SFred Isaman mempool_free(whdr, nfs_wdata_mempool); 1193feb2d49STrond Myklebust } 1203feb2d49STrond Myklebust 121dce81290STrond Myklebust void nfs_writedata_release(struct nfs_write_data *wdata) 1221da177e4SLinus Torvalds { 1236c75dc0dSFred Isaman struct nfs_pgio_header *hdr = wdata->header; 1246c75dc0dSFred Isaman struct nfs_write_header *write_header = container_of(hdr, struct nfs_write_header, header); 1256c75dc0dSFred Isaman 126383ba719STrond Myklebust put_nfs_open_context(wdata->args.context); 12730dd374fSFred Isaman if (wdata->pages.pagevec != wdata->pages.page_array) 12830dd374fSFred Isaman kfree(wdata->pages.pagevec); 1296c75dc0dSFred Isaman if (wdata != &write_header->rpc_data) 1306c75dc0dSFred Isaman kfree(wdata); 1316c75dc0dSFred Isaman else 1326c75dc0dSFred Isaman wdata->header = NULL; 1336c75dc0dSFred Isaman if (atomic_dec_and_test(&hdr->refcnt)) 134061ae2edSFred Isaman hdr->completion_ops->completion(hdr); 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds 1377b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error) 1387b159fc1STrond Myklebust { 1397b159fc1STrond Myklebust ctx->error = error; 1407b159fc1STrond Myklebust smp_wmb(); 1417b159fc1STrond Myklebust set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 1427b159fc1STrond Myklebust } 1437b159fc1STrond Myklebust 144277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page) 145277459d2STrond Myklebust { 146277459d2STrond Myklebust struct nfs_page *req = NULL; 147277459d2STrond Myklebust 148277459d2STrond Myklebust if (PagePrivate(page)) { 149277459d2STrond Myklebust req = (struct nfs_page *)page_private(page); 150277459d2STrond Myklebust if (req != NULL) 151c03b4024STrond Myklebust kref_get(&req->wb_kref); 152277459d2STrond Myklebust } 153277459d2STrond Myklebust return req; 154277459d2STrond Myklebust } 155277459d2STrond Myklebust 156277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page) 157277459d2STrond Myklebust { 158587142f8STrond Myklebust struct inode *inode = page->mapping->host; 159277459d2STrond Myklebust struct nfs_page *req = NULL; 160277459d2STrond Myklebust 161587142f8STrond Myklebust spin_lock(&inode->i_lock); 162277459d2STrond Myklebust req = nfs_page_find_request_locked(page); 163587142f8STrond Myklebust spin_unlock(&inode->i_lock); 164277459d2STrond Myklebust return req; 165277459d2STrond Myklebust } 166277459d2STrond Myklebust 1671da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */ 1681da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) 1691da177e4SLinus Torvalds { 1701da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 171a3d01454STrond Myklebust loff_t end, i_size; 172a3d01454STrond Myklebust pgoff_t end_index; 1731da177e4SLinus Torvalds 174a3d01454STrond Myklebust spin_lock(&inode->i_lock); 175a3d01454STrond Myklebust i_size = i_size_read(inode); 176a3d01454STrond Myklebust end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; 1771da177e4SLinus Torvalds if (i_size > 0 && page->index < end_index) 178a3d01454STrond Myklebust goto out; 1791da177e4SLinus Torvalds end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count); 1801da177e4SLinus Torvalds if (i_size >= end) 181a3d01454STrond Myklebust goto out; 1821da177e4SLinus Torvalds i_size_write(inode, end); 183a3d01454STrond Myklebust nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); 184a3d01454STrond Myklebust out: 185a3d01454STrond Myklebust spin_unlock(&inode->i_lock); 1861da177e4SLinus Torvalds } 1871da177e4SLinus Torvalds 188a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */ 189a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page) 190a301b777STrond Myklebust { 191a301b777STrond Myklebust SetPageError(page); 192a301b777STrond Myklebust nfs_zap_mapping(page->mapping->host, page->mapping); 193a301b777STrond Myklebust } 194a301b777STrond Myklebust 1951da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request 1961da177e4SLinus Torvalds * covers the full page. 1971da177e4SLinus Torvalds */ 1981da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count) 1991da177e4SLinus Torvalds { 2001da177e4SLinus Torvalds if (PageUptodate(page)) 2011da177e4SLinus Torvalds return; 2021da177e4SLinus Torvalds if (base != 0) 2031da177e4SLinus Torvalds return; 20449a70f27STrond Myklebust if (count != nfs_page_length(page)) 2051da177e4SLinus Torvalds return; 2061da177e4SLinus Torvalds SetPageUptodate(page); 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc) 2101da177e4SLinus Torvalds { 2111da177e4SLinus Torvalds if (wbc->for_reclaim) 212c63c7b05STrond Myklebust return FLUSH_HIGHPRI | FLUSH_STABLE; 213b17621feSWu Fengguang if (wbc->for_kupdate || wbc->for_background) 214b31268acSTrond Myklebust return FLUSH_LOWPRI | FLUSH_COND_STABLE; 215b31268acSTrond Myklebust return FLUSH_COND_STABLE; 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds /* 21989a09141SPeter Zijlstra * NFS congestion control 22089a09141SPeter Zijlstra */ 22189a09141SPeter Zijlstra 22289a09141SPeter Zijlstra int nfs_congestion_kb; 22389a09141SPeter Zijlstra 22489a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) 22589a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH \ 22689a09141SPeter Zijlstra (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) 22789a09141SPeter Zijlstra 2285a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page) 22989a09141SPeter Zijlstra { 2305a6d41b3STrond Myklebust int ret = test_set_page_writeback(page); 2315a6d41b3STrond Myklebust 2325a6d41b3STrond Myklebust if (!ret) { 23389a09141SPeter Zijlstra struct inode *inode = page->mapping->host; 23489a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 23589a09141SPeter Zijlstra 236a6305ddbSTrond Myklebust page_cache_get(page); 237277866a0SPeter Zijlstra if (atomic_long_inc_return(&nfss->writeback) > 2388aa7e847SJens Axboe NFS_CONGESTION_ON_THRESH) { 2398aa7e847SJens Axboe set_bdi_congested(&nfss->backing_dev_info, 2408aa7e847SJens Axboe BLK_RW_ASYNC); 2418aa7e847SJens Axboe } 24289a09141SPeter Zijlstra } 2435a6d41b3STrond Myklebust return ret; 24489a09141SPeter Zijlstra } 24589a09141SPeter Zijlstra 24689a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page) 24789a09141SPeter Zijlstra { 24889a09141SPeter Zijlstra struct inode *inode = page->mapping->host; 24989a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 25089a09141SPeter Zijlstra 25189a09141SPeter Zijlstra end_page_writeback(page); 252a6305ddbSTrond Myklebust page_cache_release(page); 253c4dc4beeSPeter Zijlstra if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) 2548aa7e847SJens Axboe clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC); 25589a09141SPeter Zijlstra } 25689a09141SPeter Zijlstra 257cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock) 258e261f51fSTrond Myklebust { 259587142f8STrond Myklebust struct inode *inode = page->mapping->host; 260e261f51fSTrond Myklebust struct nfs_page *req; 261e261f51fSTrond Myklebust int ret; 262e261f51fSTrond Myklebust 263587142f8STrond Myklebust spin_lock(&inode->i_lock); 264e261f51fSTrond Myklebust for (;;) { 265e261f51fSTrond Myklebust req = nfs_page_find_request_locked(page); 266074cc1deSTrond Myklebust if (req == NULL) 267074cc1deSTrond Myklebust break; 2689994b62bSFred Isaman if (nfs_lock_request_dontget(req)) 269e261f51fSTrond Myklebust break; 270e261f51fSTrond Myklebust /* Note: If we hold the page lock, as is the case in nfs_writepage, 2719994b62bSFred Isaman * then the call to nfs_lock_request_dontget() will always 272e261f51fSTrond Myklebust * succeed provided that someone hasn't already marked the 273e261f51fSTrond Myklebust * request as dirty (in which case we don't care). 274e261f51fSTrond Myklebust */ 275587142f8STrond Myklebust spin_unlock(&inode->i_lock); 276cfb506e1STrond Myklebust if (!nonblock) 277e261f51fSTrond Myklebust ret = nfs_wait_on_request(req); 278cfb506e1STrond Myklebust else 279cfb506e1STrond Myklebust ret = -EAGAIN; 280e261f51fSTrond Myklebust nfs_release_request(req); 281e261f51fSTrond Myklebust if (ret != 0) 282074cc1deSTrond Myklebust return ERR_PTR(ret); 283587142f8STrond Myklebust spin_lock(&inode->i_lock); 284e261f51fSTrond Myklebust } 285587142f8STrond Myklebust spin_unlock(&inode->i_lock); 286074cc1deSTrond Myklebust return req; 287612c9384STrond Myklebust } 288074cc1deSTrond Myklebust 289074cc1deSTrond Myklebust /* 290074cc1deSTrond Myklebust * Find an associated nfs write request, and prepare to flush it out 291074cc1deSTrond Myklebust * May return an error if the user signalled nfs_wait_on_request(). 292074cc1deSTrond Myklebust */ 293074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, 294cfb506e1STrond Myklebust struct page *page, bool nonblock) 295074cc1deSTrond Myklebust { 296074cc1deSTrond Myklebust struct nfs_page *req; 297074cc1deSTrond Myklebust int ret = 0; 298074cc1deSTrond Myklebust 299cfb506e1STrond Myklebust req = nfs_find_and_lock_request(page, nonblock); 300074cc1deSTrond Myklebust if (!req) 301074cc1deSTrond Myklebust goto out; 302074cc1deSTrond Myklebust ret = PTR_ERR(req); 303074cc1deSTrond Myklebust if (IS_ERR(req)) 304074cc1deSTrond Myklebust goto out; 305074cc1deSTrond Myklebust 306074cc1deSTrond Myklebust ret = nfs_set_page_writeback(page); 307074cc1deSTrond Myklebust BUG_ON(ret != 0); 308074cc1deSTrond Myklebust BUG_ON(test_bit(PG_CLEAN, &req->wb_flags)); 309074cc1deSTrond Myklebust 310f8512ad0SFred Isaman if (!nfs_pageio_add_request(pgio, req)) { 311f8512ad0SFred Isaman nfs_redirty_request(req); 312074cc1deSTrond Myklebust ret = pgio->pg_error; 313f8512ad0SFred Isaman } 314074cc1deSTrond Myklebust out: 315074cc1deSTrond Myklebust return ret; 316e261f51fSTrond Myklebust } 317e261f51fSTrond Myklebust 318f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio) 319f758c885STrond Myklebust { 320f758c885STrond Myklebust struct inode *inode = page->mapping->host; 321cfb506e1STrond Myklebust int ret; 322f758c885STrond Myklebust 323f758c885STrond Myklebust nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); 324f758c885STrond Myklebust nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); 325f758c885STrond Myklebust 326f758c885STrond Myklebust nfs_pageio_cond_complete(pgio, page->index); 3271b430beeSWu Fengguang ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE); 328cfb506e1STrond Myklebust if (ret == -EAGAIN) { 329cfb506e1STrond Myklebust redirty_page_for_writepage(wbc, page); 330cfb506e1STrond Myklebust ret = 0; 331cfb506e1STrond Myklebust } 332cfb506e1STrond Myklebust return ret; 333f758c885STrond Myklebust } 334f758c885STrond Myklebust 335e261f51fSTrond Myklebust /* 3361da177e4SLinus Torvalds * Write an mmapped page to the server. 3371da177e4SLinus Torvalds */ 3384d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc) 3391da177e4SLinus Torvalds { 340f758c885STrond Myklebust struct nfs_pageio_descriptor pgio; 341e261f51fSTrond Myklebust int err; 3421da177e4SLinus Torvalds 343061ae2edSFred Isaman nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc), 344061ae2edSFred Isaman &nfs_async_write_completion_ops); 345f758c885STrond Myklebust err = nfs_do_writepage(page, wbc, &pgio); 346f758c885STrond Myklebust nfs_pageio_complete(&pgio); 347f758c885STrond Myklebust if (err < 0) 3484d770ccfSTrond Myklebust return err; 349f758c885STrond Myklebust if (pgio.pg_error < 0) 350f758c885STrond Myklebust return pgio.pg_error; 351f758c885STrond Myklebust return 0; 3524d770ccfSTrond Myklebust } 3534d770ccfSTrond Myklebust 3544d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc) 3554d770ccfSTrond Myklebust { 356f758c885STrond Myklebust int ret; 3574d770ccfSTrond Myklebust 358f758c885STrond Myklebust ret = nfs_writepage_locked(page, wbc); 3591da177e4SLinus Torvalds unlock_page(page); 360f758c885STrond Myklebust return ret; 361f758c885STrond Myklebust } 362f758c885STrond Myklebust 363f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) 364f758c885STrond Myklebust { 365f758c885STrond Myklebust int ret; 366f758c885STrond Myklebust 367f758c885STrond Myklebust ret = nfs_do_writepage(page, wbc, data); 368f758c885STrond Myklebust unlock_page(page); 369f758c885STrond Myklebust return ret; 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) 3731da177e4SLinus Torvalds { 3741da177e4SLinus Torvalds struct inode *inode = mapping->host; 37572cb77f4STrond Myklebust unsigned long *bitlock = &NFS_I(inode)->flags; 376c63c7b05STrond Myklebust struct nfs_pageio_descriptor pgio; 3771da177e4SLinus Torvalds int err; 3781da177e4SLinus Torvalds 37972cb77f4STrond Myklebust /* Stop dirtying of new pages while we sync */ 38072cb77f4STrond Myklebust err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING, 38172cb77f4STrond Myklebust nfs_wait_bit_killable, TASK_KILLABLE); 38272cb77f4STrond Myklebust if (err) 38372cb77f4STrond Myklebust goto out_err; 38472cb77f4STrond Myklebust 38591d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); 38691d5b470SChuck Lever 387061ae2edSFred Isaman nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), 388061ae2edSFred Isaman &nfs_async_write_completion_ops); 389f758c885STrond Myklebust err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); 390c63c7b05STrond Myklebust nfs_pageio_complete(&pgio); 39172cb77f4STrond Myklebust 39272cb77f4STrond Myklebust clear_bit_unlock(NFS_INO_FLUSHING, bitlock); 39372cb77f4STrond Myklebust smp_mb__after_clear_bit(); 39472cb77f4STrond Myklebust wake_up_bit(bitlock, NFS_INO_FLUSHING); 39572cb77f4STrond Myklebust 396f758c885STrond Myklebust if (err < 0) 39772cb77f4STrond Myklebust goto out_err; 39872cb77f4STrond Myklebust err = pgio.pg_error; 39972cb77f4STrond Myklebust if (err < 0) 40072cb77f4STrond Myklebust goto out_err; 401c63c7b05STrond Myklebust return 0; 40272cb77f4STrond Myklebust out_err: 40372cb77f4STrond Myklebust return err; 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds /* 4071da177e4SLinus Torvalds * Insert a write request into an inode 4081da177e4SLinus Torvalds */ 409d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req) 4101da177e4SLinus Torvalds { 4111da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 412e7d39069STrond Myklebust 413e7d39069STrond Myklebust /* Lock the request! */ 414e7d39069STrond Myklebust nfs_lock_request_dontget(req); 415e7d39069STrond Myklebust 416e7d39069STrond Myklebust spin_lock(&inode->i_lock); 4174d65c520STrond Myklebust if (!nfsi->npages && nfs_have_delegation(inode, FMODE_WRITE)) 418a9a4a87aSTrond Myklebust inode->i_version++; 4192df485a7STrond Myklebust set_bit(PG_MAPPED, &req->wb_flags); 420deb7d638STrond Myklebust SetPagePrivate(req->wb_page); 421277459d2STrond Myklebust set_page_private(req->wb_page, (unsigned long)req); 4221da177e4SLinus Torvalds nfsi->npages++; 423c03b4024STrond Myklebust kref_get(&req->wb_kref); 424e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 4251da177e4SLinus Torvalds } 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds /* 42889a09141SPeter Zijlstra * Remove a write request from an inode 4291da177e4SLinus Torvalds */ 4301da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req) 4311da177e4SLinus Torvalds { 4323d4ff43dSAl Viro struct inode *inode = req->wb_context->dentry->d_inode; 4331da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds BUG_ON (!NFS_WBACK_BUSY(req)); 4361da177e4SLinus Torvalds 437587142f8STrond Myklebust spin_lock(&inode->i_lock); 438277459d2STrond Myklebust set_page_private(req->wb_page, 0); 439deb7d638STrond Myklebust ClearPagePrivate(req->wb_page); 4402df485a7STrond Myklebust clear_bit(PG_MAPPED, &req->wb_flags); 4411da177e4SLinus Torvalds nfsi->npages--; 442587142f8STrond Myklebust spin_unlock(&inode->i_lock); 4431da177e4SLinus Torvalds nfs_release_request(req); 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 44661822ab5STrond Myklebust static void 4476d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req) 44861822ab5STrond Myklebust { 44961822ab5STrond Myklebust __set_page_dirty_nobuffers(req->wb_page); 45061822ab5STrond Myklebust } 45161822ab5STrond Myklebust 4521da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 4538dd37758STrond Myklebust /** 4548dd37758STrond Myklebust * nfs_request_add_commit_list - add request to a commit list 4558dd37758STrond Myklebust * @req: pointer to a struct nfs_page 456ea2cf228SFred Isaman * @dst: commit list head 457ea2cf228SFred Isaman * @cinfo: holds list lock and accounting info 4588dd37758STrond Myklebust * 459ea2cf228SFred Isaman * This sets the PG_CLEAN bit, updates the cinfo count of 4608dd37758STrond Myklebust * number of outstanding requests requiring a commit as well as 4618dd37758STrond Myklebust * the MM page stats. 4628dd37758STrond Myklebust * 463ea2cf228SFred Isaman * The caller must _not_ hold the cinfo->lock, but must be 4648dd37758STrond Myklebust * holding the nfs_page lock. 4658dd37758STrond Myklebust */ 4668dd37758STrond Myklebust void 467ea2cf228SFred Isaman nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst, 468ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 4698dd37758STrond Myklebust { 4708dd37758STrond Myklebust set_bit(PG_CLEAN, &(req)->wb_flags); 471ea2cf228SFred Isaman spin_lock(cinfo->lock); 472ea2cf228SFred Isaman nfs_list_add_request(req, dst); 473ea2cf228SFred Isaman cinfo->mds->ncommit++; 474ea2cf228SFred Isaman spin_unlock(cinfo->lock); 4758dd37758STrond Myklebust inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 4768dd37758STrond Myklebust inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE); 477ea2cf228SFred Isaman __mark_inode_dirty(req->wb_context->dentry->d_inode, I_DIRTY_DATASYNC); 4788dd37758STrond Myklebust } 4798dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list); 4808dd37758STrond Myklebust 4818dd37758STrond Myklebust /** 4828dd37758STrond Myklebust * nfs_request_remove_commit_list - Remove request from a commit list 4838dd37758STrond Myklebust * @req: pointer to a nfs_page 484ea2cf228SFred Isaman * @cinfo: holds list lock and accounting info 4858dd37758STrond Myklebust * 486ea2cf228SFred Isaman * This clears the PG_CLEAN bit, and updates the cinfo's count of 4878dd37758STrond Myklebust * number of outstanding requests requiring a commit 4888dd37758STrond Myklebust * It does not update the MM page stats. 4898dd37758STrond Myklebust * 490ea2cf228SFred Isaman * The caller _must_ hold the cinfo->lock and the nfs_page lock. 4918dd37758STrond Myklebust */ 4928dd37758STrond Myklebust void 493ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req, 494ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 4958dd37758STrond Myklebust { 4968dd37758STrond Myklebust if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) 4978dd37758STrond Myklebust return; 4988dd37758STrond Myklebust nfs_list_remove_request(req); 499ea2cf228SFred Isaman cinfo->mds->ncommit--; 5008dd37758STrond Myklebust } 5018dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list); 5028dd37758STrond Myklebust 503ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, 504ea2cf228SFred Isaman struct inode *inode) 505ea2cf228SFred Isaman { 506ea2cf228SFred Isaman cinfo->lock = &inode->i_lock; 507ea2cf228SFred Isaman cinfo->mds = &NFS_I(inode)->commit_info; 508ea2cf228SFred Isaman cinfo->ds = pnfs_get_ds_info(inode); 509*f453a54aSFred Isaman cinfo->completion_ops = &nfs_commit_completion_ops; 510ea2cf228SFred Isaman } 511ea2cf228SFred Isaman 512ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo, 513ea2cf228SFred Isaman struct inode *inode, 514ea2cf228SFred Isaman struct nfs_direct_req *dreq) 515ea2cf228SFred Isaman { 516ea2cf228SFred Isaman nfs_init_cinfo_from_inode(cinfo, inode); 517ea2cf228SFred Isaman } 518ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo); 5198dd37758STrond Myklebust 5201da177e4SLinus Torvalds /* 5211da177e4SLinus Torvalds * Add a request to the inode's commit list. 5221da177e4SLinus Torvalds */ 5231da177e4SLinus Torvalds static void 524ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, 525ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 5261da177e4SLinus Torvalds { 527ea2cf228SFred Isaman if (pnfs_mark_request_commit(req, lseg, cinfo)) 5288dd37758STrond Myklebust return; 529ea2cf228SFred Isaman nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo); 5301da177e4SLinus Torvalds } 5318e821cadSTrond Myklebust 532d6d6dc7cSFred Isaman static void 533d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page) 534e468bae9STrond Myklebust { 535e468bae9STrond Myklebust dec_zone_page_state(page, NR_UNSTABLE_NFS); 536e468bae9STrond Myklebust dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE); 537e468bae9STrond Myklebust } 538d6d6dc7cSFred Isaman 5398dd37758STrond Myklebust static void 540d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req) 541d6d6dc7cSFred Isaman { 5428dd37758STrond Myklebust if (test_bit(PG_CLEAN, &req->wb_flags)) { 5438dd37758STrond Myklebust struct inode *inode = req->wb_context->dentry->d_inode; 544ea2cf228SFred Isaman struct nfs_commit_info cinfo; 545d6d6dc7cSFred Isaman 546ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, inode); 547ea2cf228SFred Isaman if (!pnfs_clear_request_commit(req, &cinfo)) { 548ea2cf228SFred Isaman spin_lock(cinfo.lock); 549ea2cf228SFred Isaman nfs_request_remove_commit_list(req, &cinfo); 550ea2cf228SFred Isaman spin_unlock(cinfo.lock); 551d6d6dc7cSFred Isaman } 5528dd37758STrond Myklebust nfs_clear_page_commit(req->wb_page); 5538dd37758STrond Myklebust } 554e468bae9STrond Myklebust } 555e468bae9STrond Myklebust 5568e821cadSTrond Myklebust static inline 5578e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 5588e821cadSTrond Myklebust { 559465d5243SFred Isaman if (data->verf.committed == NFS_DATA_SYNC) 560cd841605SFred Isaman return data->header->lseg == NULL; 5618e821cadSTrond Myklebust return data->verf.committed != NFS_FILE_SYNC; 5628e821cadSTrond Myklebust } 5638e821cadSTrond Myklebust 5648e821cadSTrond Myklebust #else 5658dd37758STrond Myklebust static void 566ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, 567ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 5688e821cadSTrond Myklebust { 5698e821cadSTrond Myklebust } 5708e821cadSTrond Myklebust 5718dd37758STrond Myklebust static void 572e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req) 573e468bae9STrond Myklebust { 574e468bae9STrond Myklebust } 575e468bae9STrond Myklebust 5768e821cadSTrond Myklebust static inline 5778e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 5788e821cadSTrond Myklebust { 5798e821cadSTrond Myklebust return 0; 5808e821cadSTrond Myklebust } 5818e821cadSTrond Myklebust 5821da177e4SLinus Torvalds #endif 5831da177e4SLinus Torvalds 584061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr) 5856c75dc0dSFred Isaman { 586ea2cf228SFred Isaman struct nfs_commit_info cinfo; 5876c75dc0dSFred Isaman unsigned long bytes = 0; 5886c75dc0dSFred Isaman 5896c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 5906c75dc0dSFred Isaman goto out; 591ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, hdr->inode); 5926c75dc0dSFred Isaman while (!list_empty(&hdr->pages)) { 5936c75dc0dSFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 5946c75dc0dSFred Isaman struct page *page = req->wb_page; 5956c75dc0dSFred Isaman 5966c75dc0dSFred Isaman bytes += req->wb_bytes; 5976c75dc0dSFred Isaman nfs_list_remove_request(req); 5986c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && 5996c75dc0dSFred Isaman (hdr->good_bytes < bytes)) { 6006c75dc0dSFred Isaman nfs_set_pageerror(page); 6016c75dc0dSFred Isaman nfs_context_set_write_error(req->wb_context, hdr->error); 6026c75dc0dSFred Isaman goto remove_req; 6036c75dc0dSFred Isaman } 6046c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) { 6056c75dc0dSFred Isaman nfs_mark_request_dirty(req); 6066c75dc0dSFred Isaman goto next; 6076c75dc0dSFred Isaman } 6086c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) { 609ea2cf228SFred Isaman nfs_mark_request_commit(req, hdr->lseg, &cinfo); 6106c75dc0dSFred Isaman goto next; 6116c75dc0dSFred Isaman } 6126c75dc0dSFred Isaman remove_req: 6136c75dc0dSFred Isaman nfs_inode_remove_request(req); 6146c75dc0dSFred Isaman next: 6156c75dc0dSFred Isaman nfs_unlock_request(req); 6166c75dc0dSFred Isaman nfs_end_page_writeback(page); 6176c75dc0dSFred Isaman } 6186c75dc0dSFred Isaman out: 6196c75dc0dSFred Isaman hdr->release(hdr); 6206c75dc0dSFred Isaman } 6216c75dc0dSFred Isaman 62247c62564STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 623ea2cf228SFred Isaman static unsigned long 624ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo) 625fb8a1f11STrond Myklebust { 626ea2cf228SFred Isaman return cinfo->mds->ncommit; 627fb8a1f11STrond Myklebust } 628fb8a1f11STrond Myklebust 629ea2cf228SFred Isaman /* cinfo->lock held by caller */ 6308dd37758STrond Myklebust static int 631ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst, 632ea2cf228SFred Isaman struct nfs_commit_info *cinfo, int max) 633d6d6dc7cSFred Isaman { 634d6d6dc7cSFred Isaman struct nfs_page *req, *tmp; 635d6d6dc7cSFred Isaman int ret = 0; 636d6d6dc7cSFred Isaman 637d6d6dc7cSFred Isaman list_for_each_entry_safe(req, tmp, src, wb_list) { 6388dd37758STrond Myklebust if (!nfs_lock_request(req)) 6398dd37758STrond Myklebust continue; 640ea2cf228SFred Isaman if (cond_resched_lock(cinfo->lock)) 6413b3be88dSTrond Myklebust list_safe_reset_next(req, tmp, wb_list); 642ea2cf228SFred Isaman nfs_request_remove_commit_list(req, cinfo); 6438dd37758STrond Myklebust nfs_list_add_request(req, dst); 644d6d6dc7cSFred Isaman ret++; 645d6d6dc7cSFred Isaman if (ret == max) 646d6d6dc7cSFred Isaman break; 647d6d6dc7cSFred Isaman } 648d6d6dc7cSFred Isaman return ret; 649d6d6dc7cSFred Isaman } 650d6d6dc7cSFred Isaman 6511da177e4SLinus Torvalds /* 6521da177e4SLinus Torvalds * nfs_scan_commit - Scan an inode for commit requests 6531da177e4SLinus Torvalds * @inode: NFS inode to scan 654ea2cf228SFred Isaman * @dst: mds destination list 655ea2cf228SFred Isaman * @cinfo: mds and ds lists of reqs ready to commit 6561da177e4SLinus Torvalds * 6571da177e4SLinus Torvalds * Moves requests from the inode's 'commit' request list. 6581da177e4SLinus Torvalds * The requests are *not* checked to ensure that they form a contiguous set. 6591da177e4SLinus Torvalds */ 6601da177e4SLinus Torvalds static int 661ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst, 662ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 6631da177e4SLinus Torvalds { 664d6d6dc7cSFred Isaman int ret = 0; 665fb8a1f11STrond Myklebust 666ea2cf228SFred Isaman spin_lock(cinfo->lock); 667ea2cf228SFred Isaman if (cinfo->mds->ncommit > 0) { 6688dd37758STrond Myklebust const int max = INT_MAX; 669d6d6dc7cSFred Isaman 670ea2cf228SFred Isaman ret = nfs_scan_commit_list(&cinfo->mds->list, dst, 671ea2cf228SFred Isaman cinfo, max); 672ea2cf228SFred Isaman ret += pnfs_scan_commit_lists(inode, cinfo, max - ret); 673d6d6dc7cSFred Isaman } 674ea2cf228SFred Isaman spin_unlock(cinfo->lock); 675ff778d02STrond Myklebust return ret; 6761da177e4SLinus Torvalds } 677d6d6dc7cSFred Isaman 678c42de9ddSTrond Myklebust #else 679ea2cf228SFred Isaman static unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo) 680fb8a1f11STrond Myklebust { 681fb8a1f11STrond Myklebust return 0; 682fb8a1f11STrond Myklebust } 683fb8a1f11STrond Myklebust 684ea2cf228SFred Isaman static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, 685ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 686c42de9ddSTrond Myklebust { 687c42de9ddSTrond Myklebust return 0; 688c42de9ddSTrond Myklebust } 6891da177e4SLinus Torvalds #endif 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds /* 692e7d39069STrond Myklebust * Search for an existing write request, and attempt to update 693e7d39069STrond Myklebust * it to reflect a new dirty region on a given page. 6941da177e4SLinus Torvalds * 695e7d39069STrond Myklebust * If the attempt fails, then the existing request is flushed out 696e7d39069STrond Myklebust * to disk. 6971da177e4SLinus Torvalds */ 698e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode, 699e7d39069STrond Myklebust struct page *page, 700e7d39069STrond Myklebust unsigned int offset, 701e7d39069STrond Myklebust unsigned int bytes) 7021da177e4SLinus Torvalds { 703e7d39069STrond Myklebust struct nfs_page *req; 704e7d39069STrond Myklebust unsigned int rqend; 705e7d39069STrond Myklebust unsigned int end; 7061da177e4SLinus Torvalds int error; 707277459d2STrond Myklebust 708e7d39069STrond Myklebust if (!PagePrivate(page)) 709e7d39069STrond Myklebust return NULL; 710e7d39069STrond Myklebust 711e7d39069STrond Myklebust end = offset + bytes; 712e7d39069STrond Myklebust spin_lock(&inode->i_lock); 713e7d39069STrond Myklebust 714e7d39069STrond Myklebust for (;;) { 715e7d39069STrond Myklebust req = nfs_page_find_request_locked(page); 716e7d39069STrond Myklebust if (req == NULL) 717e7d39069STrond Myklebust goto out_unlock; 718e7d39069STrond Myklebust 719e7d39069STrond Myklebust rqend = req->wb_offset + req->wb_bytes; 720e7d39069STrond Myklebust /* 721e7d39069STrond Myklebust * Tell the caller to flush out the request if 722e7d39069STrond Myklebust * the offsets are non-contiguous. 723e7d39069STrond Myklebust * Note: nfs_flush_incompatible() will already 724e7d39069STrond Myklebust * have flushed out requests having wrong owners. 725e7d39069STrond Myklebust */ 726e468bae9STrond Myklebust if (offset > rqend 727e7d39069STrond Myklebust || end < req->wb_offset) 728e7d39069STrond Myklebust goto out_flushme; 729e7d39069STrond Myklebust 7309994b62bSFred Isaman if (nfs_lock_request_dontget(req)) 731e7d39069STrond Myklebust break; 732e7d39069STrond Myklebust 733e7d39069STrond Myklebust /* The request is locked, so wait and then retry */ 734587142f8STrond Myklebust spin_unlock(&inode->i_lock); 7351da177e4SLinus Torvalds error = nfs_wait_on_request(req); 7361da177e4SLinus Torvalds nfs_release_request(req); 737e7d39069STrond Myklebust if (error != 0) 738e7d39069STrond Myklebust goto out_err; 739e7d39069STrond Myklebust spin_lock(&inode->i_lock); 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds /* Okay, the request matches. Update the region */ 7431da177e4SLinus Torvalds if (offset < req->wb_offset) { 7441da177e4SLinus Torvalds req->wb_offset = offset; 7451da177e4SLinus Torvalds req->wb_pgbase = offset; 7461da177e4SLinus Torvalds } 7471da177e4SLinus Torvalds if (end > rqend) 7481da177e4SLinus Torvalds req->wb_bytes = end - req->wb_offset; 749e7d39069STrond Myklebust else 750e7d39069STrond Myklebust req->wb_bytes = rqend - req->wb_offset; 751e7d39069STrond Myklebust out_unlock: 752e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 753ca138f36SFred Isaman if (req) 7548dd37758STrond Myklebust nfs_clear_request_commit(req); 755e7d39069STrond Myklebust return req; 756e7d39069STrond Myklebust out_flushme: 757e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 758e7d39069STrond Myklebust nfs_release_request(req); 759e7d39069STrond Myklebust error = nfs_wb_page(inode, page); 760e7d39069STrond Myklebust out_err: 761e7d39069STrond Myklebust return ERR_PTR(error); 762e7d39069STrond Myklebust } 7631da177e4SLinus Torvalds 764e7d39069STrond Myklebust /* 765e7d39069STrond Myklebust * Try to update an existing write request, or create one if there is none. 766e7d39069STrond Myklebust * 767e7d39069STrond Myklebust * Note: Should always be called with the Page Lock held to prevent races 768e7d39069STrond Myklebust * if we have to add a new request. Also assumes that the caller has 769e7d39069STrond Myklebust * already called nfs_flush_incompatible() if necessary. 770e7d39069STrond Myklebust */ 771e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, 772e7d39069STrond Myklebust struct page *page, unsigned int offset, unsigned int bytes) 773e7d39069STrond Myklebust { 774e7d39069STrond Myklebust struct inode *inode = page->mapping->host; 775e7d39069STrond Myklebust struct nfs_page *req; 776e7d39069STrond Myklebust 777e7d39069STrond Myklebust req = nfs_try_to_update_request(inode, page, offset, bytes); 778e7d39069STrond Myklebust if (req != NULL) 779e7d39069STrond Myklebust goto out; 780e7d39069STrond Myklebust req = nfs_create_request(ctx, inode, page, offset, bytes); 781e7d39069STrond Myklebust if (IS_ERR(req)) 782e7d39069STrond Myklebust goto out; 783d6d6dc7cSFred Isaman nfs_inode_add_request(inode, req); 784efc91ed0STrond Myklebust out: 78561e930a9STrond Myklebust return req; 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds 788e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, 789e7d39069STrond Myklebust unsigned int offset, unsigned int count) 790e7d39069STrond Myklebust { 791e7d39069STrond Myklebust struct nfs_page *req; 792e7d39069STrond Myklebust 793e7d39069STrond Myklebust req = nfs_setup_write_request(ctx, page, offset, count); 794e7d39069STrond Myklebust if (IS_ERR(req)) 795e7d39069STrond Myklebust return PTR_ERR(req); 796e7d39069STrond Myklebust /* Update file length */ 797e7d39069STrond Myklebust nfs_grow_file(page, offset, count); 798e7d39069STrond Myklebust nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); 799a6305ddbSTrond Myklebust nfs_mark_request_dirty(req); 8009994b62bSFred Isaman nfs_unlock_request(req); 801e7d39069STrond Myklebust return 0; 802e7d39069STrond Myklebust } 803e7d39069STrond Myklebust 8041da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page) 8051da177e4SLinus Torvalds { 806cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 8071da177e4SLinus Torvalds struct nfs_page *req; 8081a54533eSTrond Myklebust int do_flush, status; 8091da177e4SLinus Torvalds /* 8101da177e4SLinus Torvalds * Look for a request corresponding to this page. If there 8111da177e4SLinus Torvalds * is one, and it belongs to another file, we flush it out 8121da177e4SLinus Torvalds * before we try to copy anything into the page. Do this 8131da177e4SLinus Torvalds * due to the lack of an ACCESS-type call in NFSv2. 8141da177e4SLinus Torvalds * Also do the same if we find a request from an existing 8151da177e4SLinus Torvalds * dropped page. 8161da177e4SLinus Torvalds */ 8171a54533eSTrond Myklebust do { 818277459d2STrond Myklebust req = nfs_page_find_request(page); 8191a54533eSTrond Myklebust if (req == NULL) 8201a54533eSTrond Myklebust return 0; 821f11ac8dbSTrond Myklebust do_flush = req->wb_page != page || req->wb_context != ctx || 822f11ac8dbSTrond Myklebust req->wb_lock_context->lockowner != current->files || 823f11ac8dbSTrond Myklebust req->wb_lock_context->pid != current->tgid; 8241da177e4SLinus Torvalds nfs_release_request(req); 8251a54533eSTrond Myklebust if (!do_flush) 8261a54533eSTrond Myklebust return 0; 827277459d2STrond Myklebust status = nfs_wb_page(page->mapping->host, page); 8281a54533eSTrond Myklebust } while (status == 0); 8291a54533eSTrond Myklebust return status; 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds /* 8335d47a356STrond Myklebust * If the page cache is marked as unsafe or invalid, then we can't rely on 8345d47a356STrond Myklebust * the PageUptodate() flag. In this case, we will need to turn off 8355d47a356STrond Myklebust * write optimisations that depend on the page contents being correct. 8365d47a356STrond Myklebust */ 8375d47a356STrond Myklebust static int nfs_write_pageuptodate(struct page *page, struct inode *inode) 8385d47a356STrond Myklebust { 8395d47a356STrond Myklebust return PageUptodate(page) && 8405d47a356STrond Myklebust !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA)); 8415d47a356STrond Myklebust } 8425d47a356STrond Myklebust 8435d47a356STrond Myklebust /* 8441da177e4SLinus Torvalds * Update and possibly write a cached page of an NFS file. 8451da177e4SLinus Torvalds * 8461da177e4SLinus Torvalds * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad 8471da177e4SLinus Torvalds * things with a page scheduled for an RPC call (e.g. invalidate it). 8481da177e4SLinus Torvalds */ 8491da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page, 8501da177e4SLinus Torvalds unsigned int offset, unsigned int count) 8511da177e4SLinus Torvalds { 852cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 8531da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 8541da177e4SLinus Torvalds int status = 0; 8551da177e4SLinus Torvalds 85691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 85791d5b470SChuck Lever 85848186c7dSChuck Lever dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n", 85901cce933SJosef "Jeff" Sipek file->f_path.dentry->d_parent->d_name.name, 86001cce933SJosef "Jeff" Sipek file->f_path.dentry->d_name.name, count, 8610bbacc40SChuck Lever (long long)(page_offset(page) + offset)); 8621da177e4SLinus Torvalds 8631da177e4SLinus Torvalds /* If we're not using byte range locks, and we know the page 8645d47a356STrond Myklebust * is up to date, it may be more efficient to extend the write 8655d47a356STrond Myklebust * to cover the entire page in order to avoid fragmentation 8665d47a356STrond Myklebust * inefficiencies. 8671da177e4SLinus Torvalds */ 8685d47a356STrond Myklebust if (nfs_write_pageuptodate(page, inode) && 8695d47a356STrond Myklebust inode->i_flock == NULL && 8706b2f3d1fSChristoph Hellwig !(file->f_flags & O_DSYNC)) { 87149a70f27STrond Myklebust count = max(count + offset, nfs_page_length(page)); 8721da177e4SLinus Torvalds offset = 0; 8731da177e4SLinus Torvalds } 8741da177e4SLinus Torvalds 875e21195a7STrond Myklebust status = nfs_writepage_setup(ctx, page, offset, count); 87603fa9e84STrond Myklebust if (status < 0) 87703fa9e84STrond Myklebust nfs_set_pageerror(page); 87859b7c05fSTrond Myklebust else 87959b7c05fSTrond Myklebust __set_page_dirty_nobuffers(page); 8801da177e4SLinus Torvalds 88148186c7dSChuck Lever dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", 8821da177e4SLinus Torvalds status, (long long)i_size_read(inode)); 8831da177e4SLinus Torvalds return status; 8841da177e4SLinus Torvalds } 8851da177e4SLinus Torvalds 8863ff7576dSTrond Myklebust static int flush_task_priority(int how) 8871da177e4SLinus Torvalds { 8881da177e4SLinus Torvalds switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { 8891da177e4SLinus Torvalds case FLUSH_HIGHPRI: 8901da177e4SLinus Torvalds return RPC_PRIORITY_HIGH; 8911da177e4SLinus Torvalds case FLUSH_LOWPRI: 8921da177e4SLinus Torvalds return RPC_PRIORITY_LOW; 8931da177e4SLinus Torvalds } 8941da177e4SLinus Torvalds return RPC_PRIORITY_NORMAL; 8951da177e4SLinus Torvalds } 8961da177e4SLinus Torvalds 897c5996c4eSFred Isaman int nfs_initiate_write(struct rpc_clnt *clnt, 898c5996c4eSFred Isaman struct nfs_write_data *data, 899788e7a89STrond Myklebust const struct rpc_call_ops *call_ops, 9001da177e4SLinus Torvalds int how) 9011da177e4SLinus Torvalds { 902cd841605SFred Isaman struct inode *inode = data->header->inode; 9033ff7576dSTrond Myklebust int priority = flush_task_priority(how); 90407737691STrond Myklebust struct rpc_task *task; 905bdc7f021STrond Myklebust struct rpc_message msg = { 906bdc7f021STrond Myklebust .rpc_argp = &data->args, 907bdc7f021STrond Myklebust .rpc_resp = &data->res, 908cd841605SFred Isaman .rpc_cred = data->header->cred, 909bdc7f021STrond Myklebust }; 91084115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 911d138d5d1SAndy Adamson .rpc_client = clnt, 91207737691STrond Myklebust .task = &data->task, 913bdc7f021STrond Myklebust .rpc_message = &msg, 91484115e1cSTrond Myklebust .callback_ops = call_ops, 91584115e1cSTrond Myklebust .callback_data = data, 916101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 9172c61be0aSTrond Myklebust .flags = RPC_TASK_ASYNC, 9183ff7576dSTrond Myklebust .priority = priority, 91984115e1cSTrond Myklebust }; 9202c61be0aSTrond Myklebust int ret = 0; 9211da177e4SLinus Torvalds 922d138d5d1SAndy Adamson /* Set up the initial task struct. */ 923d138d5d1SAndy Adamson NFS_PROTO(inode)->write_setup(data, &msg); 924d138d5d1SAndy Adamson 925d138d5d1SAndy Adamson dprintk("NFS: %5u initiated write call " 926d138d5d1SAndy Adamson "(req %s/%lld, %u bytes @ offset %llu)\n", 927d138d5d1SAndy Adamson data->task.tk_pid, 928d138d5d1SAndy Adamson inode->i_sb->s_id, 929d138d5d1SAndy Adamson (long long)NFS_FILEID(inode), 930d138d5d1SAndy Adamson data->args.count, 931d138d5d1SAndy Adamson (unsigned long long)data->args.offset); 932d138d5d1SAndy Adamson 933d138d5d1SAndy Adamson task = rpc_run_task(&task_setup_data); 934d138d5d1SAndy Adamson if (IS_ERR(task)) { 935d138d5d1SAndy Adamson ret = PTR_ERR(task); 936d138d5d1SAndy Adamson goto out; 937d138d5d1SAndy Adamson } 938d138d5d1SAndy Adamson if (how & FLUSH_SYNC) { 939d138d5d1SAndy Adamson ret = rpc_wait_for_completion_task(task); 940d138d5d1SAndy Adamson if (ret == 0) 941d138d5d1SAndy Adamson ret = task->tk_status; 942d138d5d1SAndy Adamson } 943d138d5d1SAndy Adamson rpc_put_task(task); 944d138d5d1SAndy Adamson out: 945d138d5d1SAndy Adamson return ret; 946d138d5d1SAndy Adamson } 947a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write); 948d138d5d1SAndy Adamson 949d138d5d1SAndy Adamson /* 950d138d5d1SAndy Adamson * Set up the argument/result storage required for the RPC call. 951d138d5d1SAndy Adamson */ 9526c75dc0dSFred Isaman static void nfs_write_rpcsetup(struct nfs_write_data *data, 953d138d5d1SAndy Adamson unsigned int count, unsigned int offset, 954ea2cf228SFred Isaman int how, struct nfs_commit_info *cinfo) 955d138d5d1SAndy Adamson { 9566c75dc0dSFred Isaman struct nfs_page *req = data->header->req; 957d138d5d1SAndy Adamson 9581da177e4SLinus Torvalds /* Set up the RPC argument and reply structs 9591da177e4SLinus Torvalds * NB: take care not to mess about with data->commit et al. */ 9601da177e4SLinus Torvalds 9616c75dc0dSFred Isaman data->args.fh = NFS_FH(data->header->inode); 9621da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 9632bea038cSBoaz Harrosh /* pnfs_set_layoutcommit needs this */ 9642bea038cSBoaz Harrosh data->mds_offset = data->args.offset; 9651da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 96630dd374fSFred Isaman data->args.pages = data->pages.pagevec; 9671da177e4SLinus Torvalds data->args.count = count; 968383ba719STrond Myklebust data->args.context = get_nfs_open_context(req->wb_context); 969f11ac8dbSTrond Myklebust data->args.lock_context = req->wb_lock_context; 970bdc7f021STrond Myklebust data->args.stable = NFS_UNSTABLE; 97187ed5eb4STrond Myklebust switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) { 97287ed5eb4STrond Myklebust case 0: 97387ed5eb4STrond Myklebust break; 97487ed5eb4STrond Myklebust case FLUSH_COND_STABLE: 975ea2cf228SFred Isaman if (nfs_reqs_to_commit(cinfo)) 97687ed5eb4STrond Myklebust break; 97787ed5eb4STrond Myklebust default: 978bdc7f021STrond Myklebust data->args.stable = NFS_FILE_SYNC; 979bdc7f021STrond Myklebust } 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds data->res.fattr = &data->fattr; 9821da177e4SLinus Torvalds data->res.count = count; 9831da177e4SLinus Torvalds data->res.verf = &data->verf; 9840e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 9856e4efd56STrond Myklebust } 9861da177e4SLinus Torvalds 9876e4efd56STrond Myklebust static int nfs_do_write(struct nfs_write_data *data, 9886e4efd56STrond Myklebust const struct rpc_call_ops *call_ops, 9896e4efd56STrond Myklebust int how) 9906e4efd56STrond Myklebust { 991cd841605SFred Isaman struct inode *inode = data->header->inode; 9920382b744SAndy Adamson 993c5996c4eSFred Isaman return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how); 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds 996275acaafSTrond Myklebust static int nfs_do_multiple_writes(struct list_head *head, 997275acaafSTrond Myklebust const struct rpc_call_ops *call_ops, 998275acaafSTrond Myklebust int how) 999275acaafSTrond Myklebust { 1000275acaafSTrond Myklebust struct nfs_write_data *data; 1001275acaafSTrond Myklebust int ret = 0; 1002275acaafSTrond Myklebust 1003275acaafSTrond Myklebust while (!list_empty(head)) { 1004275acaafSTrond Myklebust int ret2; 1005275acaafSTrond Myklebust 10066c75dc0dSFred Isaman data = list_first_entry(head, struct nfs_write_data, list); 1007275acaafSTrond Myklebust list_del_init(&data->list); 1008275acaafSTrond Myklebust 1009dce81290STrond Myklebust ret2 = nfs_do_write(data, call_ops, how); 1010275acaafSTrond Myklebust if (ret == 0) 1011275acaafSTrond Myklebust ret = ret2; 1012275acaafSTrond Myklebust } 1013275acaafSTrond Myklebust return ret; 1014275acaafSTrond Myklebust } 1015275acaafSTrond Myklebust 10166d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and 10176d884e8fSFred * call this on each, which will prepare them to be retried on next 10186d884e8fSFred * writeback using standard nfs. 10196d884e8fSFred */ 10206d884e8fSFred static void nfs_redirty_request(struct nfs_page *req) 10216d884e8fSFred { 1022a6305ddbSTrond Myklebust struct page *page = req->wb_page; 1023a6305ddbSTrond Myklebust 10246d884e8fSFred nfs_mark_request_dirty(req); 10259994b62bSFred Isaman nfs_unlock_request(req); 1026a6305ddbSTrond Myklebust nfs_end_page_writeback(page); 10276d884e8fSFred } 10286d884e8fSFred 1029061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head) 10306c75dc0dSFred Isaman { 10316c75dc0dSFred Isaman struct nfs_page *req; 10326c75dc0dSFred Isaman 10336c75dc0dSFred Isaman while (!list_empty(head)) { 10346c75dc0dSFred Isaman req = nfs_list_entry(head->next); 10356c75dc0dSFred Isaman nfs_list_remove_request(req); 10366c75dc0dSFred Isaman nfs_redirty_request(req); 10376c75dc0dSFred Isaman } 10386c75dc0dSFred Isaman } 10396c75dc0dSFred Isaman 1040061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = { 1041061ae2edSFred Isaman .error_cleanup = nfs_async_write_error, 1042061ae2edSFred Isaman .completion = nfs_write_completion, 1043061ae2edSFred Isaman }; 1044061ae2edSFred Isaman 10451da177e4SLinus Torvalds /* 10461da177e4SLinus Torvalds * Generate multiple small requests to write out a single 10471da177e4SLinus Torvalds * contiguous dirty area on one page. 10481da177e4SLinus Torvalds */ 10496c75dc0dSFred Isaman static int nfs_flush_multi(struct nfs_pageio_descriptor *desc, 10506c75dc0dSFred Isaman struct nfs_pgio_header *hdr) 10511da177e4SLinus Torvalds { 10526c75dc0dSFred Isaman struct nfs_page *req = hdr->req; 10531da177e4SLinus Torvalds struct page *page = req->wb_page; 10541da177e4SLinus Torvalds struct nfs_write_data *data; 1055d097971dSTrond Myklebust size_t wsize = desc->pg_bsize, nbytes; 1056e9f7bee1STrond Myklebust unsigned int offset; 10571da177e4SLinus Torvalds int requests = 0; 1058dbae4c73STrond Myklebust int ret = 0; 1059ea2cf228SFred Isaman struct nfs_commit_info cinfo; 10601da177e4SLinus Torvalds 1061ea2cf228SFred Isaman nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq); 10621da177e4SLinus Torvalds nfs_list_remove_request(req); 10636c75dc0dSFred Isaman nfs_list_add_request(req, &hdr->pages); 10641da177e4SLinus Torvalds 1065b31268acSTrond Myklebust if ((desc->pg_ioflags & FLUSH_COND_STABLE) && 1066ea2cf228SFred Isaman (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) || 1067b31268acSTrond Myklebust desc->pg_count > wsize)) 1068b31268acSTrond Myklebust desc->pg_ioflags &= ~FLUSH_COND_STABLE; 1069b31268acSTrond Myklebust 1070b31268acSTrond Myklebust 1071275acaafSTrond Myklebust offset = 0; 1072c76069bdSFred Isaman nbytes = desc->pg_count; 1073e9f7bee1STrond Myklebust do { 1074e9f7bee1STrond Myklebust size_t len = min(nbytes, wsize); 1075e9f7bee1STrond Myklebust 10766c75dc0dSFred Isaman data = nfs_writedata_alloc(hdr, 1); 10776c75dc0dSFred Isaman if (!data) 10781da177e4SLinus Torvalds goto out_bad; 107930dd374fSFred Isaman data->pages.pagevec[0] = page; 1080ea2cf228SFred Isaman nfs_write_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo); 10816c75dc0dSFred Isaman list_add(&data->list, &hdr->rpc_list); 10821da177e4SLinus Torvalds requests++; 1083e9f7bee1STrond Myklebust nbytes -= len; 1084275acaafSTrond Myklebust offset += len; 1085e9f7bee1STrond Myklebust } while (nbytes != 0); 10866c75dc0dSFred Isaman desc->pg_rpc_callops = &nfs_write_common_ops; 1087dbae4c73STrond Myklebust return ret; 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds out_bad: 10906c75dc0dSFred Isaman while (!list_empty(&hdr->rpc_list)) { 10916c75dc0dSFred Isaman data = list_first_entry(&hdr->rpc_list, struct nfs_write_data, list); 10926e4efd56STrond Myklebust list_del(&data->list); 10938ccd271fSFred Isaman nfs_writedata_release(data); 10941da177e4SLinus Torvalds } 1095061ae2edSFred Isaman desc->pg_completion_ops->error_cleanup(&hdr->pages); 10961da177e4SLinus Torvalds return -ENOMEM; 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds /* 11001da177e4SLinus Torvalds * Create an RPC task for the given write request and kick it. 11011da177e4SLinus Torvalds * The page must have been locked by the caller. 11021da177e4SLinus Torvalds * 11031da177e4SLinus Torvalds * It may happen that the page we're passed is not marked dirty. 11041da177e4SLinus Torvalds * This is the case if nfs_updatepage detects a conflicting request 11051da177e4SLinus Torvalds * that has been written but not committed. 11061da177e4SLinus Torvalds */ 11076c75dc0dSFred Isaman static int nfs_flush_one(struct nfs_pageio_descriptor *desc, 11086c75dc0dSFred Isaman struct nfs_pgio_header *hdr) 11091da177e4SLinus Torvalds { 11101da177e4SLinus Torvalds struct nfs_page *req; 11111da177e4SLinus Torvalds struct page **pages; 11121da177e4SLinus Torvalds struct nfs_write_data *data; 1113c76069bdSFred Isaman struct list_head *head = &desc->pg_list; 11143b609184SPeng Tao int ret = 0; 1115ea2cf228SFred Isaman struct nfs_commit_info cinfo; 11161da177e4SLinus Torvalds 11176c75dc0dSFred Isaman data = nfs_writedata_alloc(hdr, nfs_page_array_len(desc->pg_base, 1118c76069bdSFred Isaman desc->pg_count)); 11196c75dc0dSFred Isaman if (!data) { 1120061ae2edSFred Isaman desc->pg_completion_ops->error_cleanup(head); 112144b83799SFred Isaman ret = -ENOMEM; 112244b83799SFred Isaman goto out; 112344b83799SFred Isaman } 11246c75dc0dSFred Isaman 1125ea2cf228SFred Isaman nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq); 112630dd374fSFred Isaman pages = data->pages.pagevec; 11271da177e4SLinus Torvalds while (!list_empty(head)) { 11281da177e4SLinus Torvalds req = nfs_list_entry(head->next); 11291da177e4SLinus Torvalds nfs_list_remove_request(req); 11306c75dc0dSFred Isaman nfs_list_add_request(req, &hdr->pages); 11311da177e4SLinus Torvalds *pages++ = req->wb_page; 11321da177e4SLinus Torvalds } 11331da177e4SLinus Torvalds 1134b31268acSTrond Myklebust if ((desc->pg_ioflags & FLUSH_COND_STABLE) && 1135ea2cf228SFred Isaman (desc->pg_moreio || nfs_reqs_to_commit(&cinfo))) 1136b31268acSTrond Myklebust desc->pg_ioflags &= ~FLUSH_COND_STABLE; 1137b31268acSTrond Myklebust 11381da177e4SLinus Torvalds /* Set up the argument struct */ 1139ea2cf228SFred Isaman nfs_write_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo); 11406c75dc0dSFred Isaman list_add(&data->list, &hdr->rpc_list); 11416c75dc0dSFred Isaman desc->pg_rpc_callops = &nfs_write_common_ops; 114244b83799SFred Isaman out: 114344b83799SFred Isaman return ret; 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds 11466c75dc0dSFred Isaman int nfs_generic_flush(struct nfs_pageio_descriptor *desc, 11476c75dc0dSFred Isaman struct nfs_pgio_header *hdr) 1148dce81290STrond Myklebust { 1149dce81290STrond Myklebust if (desc->pg_bsize < PAGE_CACHE_SIZE) 11506c75dc0dSFred Isaman return nfs_flush_multi(desc, hdr); 11516c75dc0dSFred Isaman return nfs_flush_one(desc, hdr); 1152dce81290STrond Myklebust } 1153dce81290STrond Myklebust 1154dce81290STrond Myklebust static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc) 11551751c363STrond Myklebust { 11566c75dc0dSFred Isaman struct nfs_write_header *whdr; 11576c75dc0dSFred Isaman struct nfs_pgio_header *hdr; 1158275acaafSTrond Myklebust int ret; 1159275acaafSTrond Myklebust 11606c75dc0dSFred Isaman whdr = nfs_writehdr_alloc(); 11616c75dc0dSFred Isaman if (!whdr) { 1162061ae2edSFred Isaman desc->pg_completion_ops->error_cleanup(&hdr->pages); 11636c75dc0dSFred Isaman return -ENOMEM; 11646c75dc0dSFred Isaman } 11656c75dc0dSFred Isaman hdr = &whdr->header; 11666c75dc0dSFred Isaman nfs_pgheader_init(desc, hdr, nfs_writehdr_free); 11676c75dc0dSFred Isaman atomic_inc(&hdr->refcnt); 11686c75dc0dSFred Isaman ret = nfs_generic_flush(desc, hdr); 1169275acaafSTrond Myklebust if (ret == 0) 11706c75dc0dSFred Isaman ret = nfs_do_multiple_writes(&hdr->rpc_list, 11716c75dc0dSFred Isaman desc->pg_rpc_callops, 1172dce81290STrond Myklebust desc->pg_ioflags); 11736c75dc0dSFred Isaman else 11746c75dc0dSFred Isaman set_bit(NFS_IOHDR_REDO, &hdr->flags); 11756c75dc0dSFred Isaman if (atomic_dec_and_test(&hdr->refcnt)) 1176061ae2edSFred Isaman hdr->completion_ops->completion(hdr); 1177275acaafSTrond Myklebust return ret; 11781751c363STrond Myklebust } 11791751c363STrond Myklebust 11801751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_write_ops = { 11811751c363STrond Myklebust .pg_test = nfs_generic_pg_test, 11821751c363STrond Myklebust .pg_doio = nfs_generic_pg_writepages, 11831751c363STrond Myklebust }; 11841751c363STrond Myklebust 1185e2fecb21STrond Myklebust void nfs_pageio_init_write_mds(struct nfs_pageio_descriptor *pgio, 1186061ae2edSFred Isaman struct inode *inode, int ioflags, 1187061ae2edSFred Isaman const struct nfs_pgio_completion_ops *compl_ops) 11881751c363STrond Myklebust { 1189061ae2edSFred Isaman nfs_pageio_init(pgio, inode, &nfs_pageio_write_ops, compl_ops, 11901751c363STrond Myklebust NFS_SERVER(inode)->wsize, ioflags); 11911751c363STrond Myklebust } 11921751c363STrond Myklebust 1193dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio) 1194dce81290STrond Myklebust { 1195dce81290STrond Myklebust pgio->pg_ops = &nfs_pageio_write_ops; 1196dce81290STrond Myklebust pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize; 1197dce81290STrond Myklebust } 11981f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds); 1199dce81290STrond Myklebust 1200c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, 1201061ae2edSFred Isaman struct inode *inode, int ioflags, 1202061ae2edSFred Isaman const struct nfs_pgio_completion_ops *compl_ops) 12031da177e4SLinus Torvalds { 1204061ae2edSFred Isaman if (!pnfs_pageio_init_write(pgio, inode, ioflags, compl_ops)) 1205061ae2edSFred Isaman nfs_pageio_init_write_mds(pgio, inode, ioflags, compl_ops); 12061da177e4SLinus Torvalds } 12071da177e4SLinus Torvalds 1208def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata) 1209def6ed7eSAndy Adamson { 1210def6ed7eSAndy Adamson struct nfs_write_data *data = calldata; 1211cd841605SFred Isaman NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data); 1212def6ed7eSAndy Adamson } 1213def6ed7eSAndy Adamson 12140b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata) 12150b7c0153SFred Isaman { 12160b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 12170b7c0153SFred Isaman 12180b7c0153SFred Isaman NFS_PROTO(data->inode)->commit_rpc_prepare(task, data); 12190b7c0153SFred Isaman } 12200b7c0153SFred Isaman 12211da177e4SLinus Torvalds /* 12221da177e4SLinus Torvalds * Handle a write reply that flushes a whole page. 12231da177e4SLinus Torvalds * 12241da177e4SLinus Torvalds * FIXME: There is an inherent race with invalidate_inode_pages and 12251da177e4SLinus Torvalds * writebacks since the page->count is kept > 1 for as long 12261da177e4SLinus Torvalds * as the page has a write request pending. 12271da177e4SLinus Torvalds */ 12286c75dc0dSFred Isaman static void nfs_writeback_done_common(struct rpc_task *task, void *calldata) 12291da177e4SLinus Torvalds { 1230788e7a89STrond Myklebust struct nfs_write_data *data = calldata; 12311da177e4SLinus Torvalds 1232c9d8f89dSTrond Myklebust nfs_writeback_done(task, data); 1233c9d8f89dSTrond Myklebust } 1234c9d8f89dSTrond Myklebust 12356c75dc0dSFred Isaman static void nfs_writeback_release_common(void *calldata) 1236c9d8f89dSTrond Myklebust { 1237c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1238cd841605SFred Isaman struct nfs_pgio_header *hdr = data->header; 1239e2fecb21STrond Myklebust int status = data->task.tk_status; 12406c75dc0dSFred Isaman struct nfs_page *req = hdr->req; 1241788e7a89STrond Myklebust 12426c75dc0dSFred Isaman if ((status >= 0) && nfs_write_need_commit(data)) { 12436c75dc0dSFred Isaman spin_lock(&hdr->lock); 12446c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) 12456c75dc0dSFred Isaman ; /* Do nothing */ 12466c75dc0dSFred Isaman else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) 12471da177e4SLinus Torvalds memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); 12486c75dc0dSFred Isaman else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) 12496c75dc0dSFred Isaman set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags); 12506c75dc0dSFred Isaman spin_unlock(&hdr->lock); 12511da177e4SLinus Torvalds } 1252cd841605SFred Isaman nfs_writedata_release(data); 12531da177e4SLinus Torvalds } 12541da177e4SLinus Torvalds 12556c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops = { 1256def6ed7eSAndy Adamson .rpc_call_prepare = nfs_write_prepare, 12576c75dc0dSFred Isaman .rpc_call_done = nfs_writeback_done_common, 12586c75dc0dSFred Isaman .rpc_release = nfs_writeback_release_common, 1259788e7a89STrond Myklebust }; 1260788e7a89STrond Myklebust 1261788e7a89STrond Myklebust 12621da177e4SLinus Torvalds /* 12631da177e4SLinus Torvalds * This function is called when the WRITE call is complete. 12641da177e4SLinus Torvalds */ 126513602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data) 12661da177e4SLinus Torvalds { 12671da177e4SLinus Torvalds struct nfs_writeargs *argp = &data->args; 12681da177e4SLinus Torvalds struct nfs_writeres *resp = &data->res; 1269cd841605SFred Isaman struct inode *inode = data->header->inode; 1270788e7a89STrond Myklebust int status; 12711da177e4SLinus Torvalds 1272a3f565b1SChuck Lever dprintk("NFS: %5u nfs_writeback_done (status %d)\n", 12731da177e4SLinus Torvalds task->tk_pid, task->tk_status); 12741da177e4SLinus Torvalds 1275f551e44fSChuck Lever /* 1276f551e44fSChuck Lever * ->write_done will attempt to use post-op attributes to detect 1277f551e44fSChuck Lever * conflicting writes by other clients. A strict interpretation 1278f551e44fSChuck Lever * of close-to-open would allow us to continue caching even if 1279f551e44fSChuck Lever * another writer had changed the file, but some applications 1280f551e44fSChuck Lever * depend on tighter cache coherency when writing. 1281f551e44fSChuck Lever */ 1282cd841605SFred Isaman status = NFS_PROTO(inode)->write_done(task, data); 1283788e7a89STrond Myklebust if (status != 0) 128413602896SFred Isaman return; 1285cd841605SFred Isaman nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count); 128691d5b470SChuck Lever 12871da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 12881da177e4SLinus Torvalds if (resp->verf->committed < argp->stable && task->tk_status >= 0) { 12891da177e4SLinus Torvalds /* We tried a write call, but the server did not 12901da177e4SLinus Torvalds * commit data to stable storage even though we 12911da177e4SLinus Torvalds * requested it. 12921da177e4SLinus Torvalds * Note: There is a known bug in Tru64 < 5.0 in which 12931da177e4SLinus Torvalds * the server reports NFS_DATA_SYNC, but performs 12941da177e4SLinus Torvalds * NFS_FILE_SYNC. We therefore implement this checking 12951da177e4SLinus Torvalds * as a dprintk() in order to avoid filling syslog. 12961da177e4SLinus Torvalds */ 12971da177e4SLinus Torvalds static unsigned long complain; 12981da177e4SLinus Torvalds 1299a69aef14SFred Isaman /* Note this will print the MDS for a DS write */ 13001da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 13011da177e4SLinus Torvalds dprintk("NFS: faulty NFS server %s:" 13021da177e4SLinus Torvalds " (committed = %d) != (stable = %d)\n", 1303cd841605SFred Isaman NFS_SERVER(inode)->nfs_client->cl_hostname, 13041da177e4SLinus Torvalds resp->verf->committed, argp->stable); 13051da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 13061da177e4SLinus Torvalds } 13071da177e4SLinus Torvalds } 13081da177e4SLinus Torvalds #endif 13096c75dc0dSFred Isaman if (task->tk_status < 0) 13106c75dc0dSFred Isaman nfs_set_pgio_error(data->header, task->tk_status, argp->offset); 13116c75dc0dSFred Isaman else if (resp->count < argp->count) { 13121da177e4SLinus Torvalds static unsigned long complain; 13131da177e4SLinus Torvalds 13146c75dc0dSFred Isaman /* This a short write! */ 1315cd841605SFred Isaman nfs_inc_stats(inode, NFSIOS_SHORTWRITE); 131691d5b470SChuck Lever 13171da177e4SLinus Torvalds /* Has the server at least made some progress? */ 13186c75dc0dSFred Isaman if (resp->count == 0) { 13196c75dc0dSFred Isaman if (time_before(complain, jiffies)) { 13206c75dc0dSFred Isaman printk(KERN_WARNING 13216c75dc0dSFred Isaman "NFS: Server wrote zero bytes, expected %u.\n", 13226c75dc0dSFred Isaman argp->count); 13236c75dc0dSFred Isaman complain = jiffies + 300 * HZ; 13246c75dc0dSFred Isaman } 13256c75dc0dSFred Isaman nfs_set_pgio_error(data->header, -EIO, argp->offset); 13266c75dc0dSFred Isaman task->tk_status = -EIO; 13276c75dc0dSFred Isaman return; 13286c75dc0dSFred Isaman } 13291da177e4SLinus Torvalds /* Was this an NFSv2 write or an NFSv3 stable write? */ 13301da177e4SLinus Torvalds if (resp->verf->committed != NFS_UNSTABLE) { 13311da177e4SLinus Torvalds /* Resend from where the server left off */ 1332a69aef14SFred Isaman data->mds_offset += resp->count; 13331da177e4SLinus Torvalds argp->offset += resp->count; 13341da177e4SLinus Torvalds argp->pgbase += resp->count; 13351da177e4SLinus Torvalds argp->count -= resp->count; 13361da177e4SLinus Torvalds } else { 13371da177e4SLinus Torvalds /* Resend as a stable write in order to avoid 13381da177e4SLinus Torvalds * headaches in the case of a server crash. 13391da177e4SLinus Torvalds */ 13401da177e4SLinus Torvalds argp->stable = NFS_FILE_SYNC; 13411da177e4SLinus Torvalds } 1342d00c5d43STrond Myklebust rpc_restart_call_prepare(task); 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds } 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 134871d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait) 134971d0a611STrond Myklebust { 1350b8413f98STrond Myklebust int ret; 1351b8413f98STrond Myklebust 135271d0a611STrond Myklebust if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags)) 135371d0a611STrond Myklebust return 1; 1354b8413f98STrond Myklebust if (!may_wait) 135571d0a611STrond Myklebust return 0; 1356b8413f98STrond Myklebust ret = out_of_line_wait_on_bit_lock(&nfsi->flags, 1357b8413f98STrond Myklebust NFS_INO_COMMIT, 1358b8413f98STrond Myklebust nfs_wait_bit_killable, 1359b8413f98STrond Myklebust TASK_KILLABLE); 1360b8413f98STrond Myklebust return (ret < 0) ? ret : 1; 136171d0a611STrond Myklebust } 136271d0a611STrond Myklebust 1363*f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi) 136471d0a611STrond Myklebust { 136571d0a611STrond Myklebust clear_bit(NFS_INO_COMMIT, &nfsi->flags); 136671d0a611STrond Myklebust smp_mb__after_clear_bit(); 136771d0a611STrond Myklebust wake_up_bit(&nfsi->flags, NFS_INO_COMMIT); 136871d0a611STrond Myklebust } 136971d0a611STrond Myklebust 13700b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data) 13711da177e4SLinus Torvalds { 13720b7c0153SFred Isaman put_nfs_open_context(data->context); 13730b7c0153SFred Isaman nfs_commit_free(data); 13741da177e4SLinus Torvalds } 1375e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release); 13761da177e4SLinus Torvalds 13770b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data, 13789ace33cdSFred Isaman const struct rpc_call_ops *call_ops, 1379788e7a89STrond Myklebust int how) 13801da177e4SLinus Torvalds { 138107737691STrond Myklebust struct rpc_task *task; 13829ace33cdSFred Isaman int priority = flush_task_priority(how); 1383bdc7f021STrond Myklebust struct rpc_message msg = { 1384bdc7f021STrond Myklebust .rpc_argp = &data->args, 1385bdc7f021STrond Myklebust .rpc_resp = &data->res, 13869ace33cdSFred Isaman .rpc_cred = data->cred, 1387bdc7f021STrond Myklebust }; 138884115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 138907737691STrond Myklebust .task = &data->task, 13909ace33cdSFred Isaman .rpc_client = clnt, 1391bdc7f021STrond Myklebust .rpc_message = &msg, 13929ace33cdSFred Isaman .callback_ops = call_ops, 139384115e1cSTrond Myklebust .callback_data = data, 1394101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 13952c61be0aSTrond Myklebust .flags = RPC_TASK_ASYNC, 13963ff7576dSTrond Myklebust .priority = priority, 139784115e1cSTrond Myklebust }; 1398788e7a89STrond Myklebust /* Set up the initial task struct. */ 13999ace33cdSFred Isaman NFS_PROTO(data->inode)->commit_setup(data, &msg); 14001da177e4SLinus Torvalds 1401a3f565b1SChuck Lever dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); 1402bdc7f021STrond Myklebust 140307737691STrond Myklebust task = rpc_run_task(&task_setup_data); 1404dbae4c73STrond Myklebust if (IS_ERR(task)) 1405dbae4c73STrond Myklebust return PTR_ERR(task); 1406d2224e7aSJeff Layton if (how & FLUSH_SYNC) 1407d2224e7aSJeff Layton rpc_wait_for_completion_task(task); 140807737691STrond Myklebust rpc_put_task(task); 1409dbae4c73STrond Myklebust return 0; 14101da177e4SLinus Torvalds } 1411e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit); 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds /* 14149ace33cdSFred Isaman * Set up the argument/result storage required for the RPC call. 14159ace33cdSFred Isaman */ 14160b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data, 1417988b6dceSFred Isaman struct list_head *head, 1418*f453a54aSFred Isaman struct pnfs_layout_segment *lseg, 1419*f453a54aSFred Isaman struct nfs_commit_info *cinfo) 14209ace33cdSFred Isaman { 14219ace33cdSFred Isaman struct nfs_page *first = nfs_list_entry(head->next); 14223d4ff43dSAl Viro struct inode *inode = first->wb_context->dentry->d_inode; 14239ace33cdSFred Isaman 14249ace33cdSFred Isaman /* Set up the RPC argument and reply structs 14259ace33cdSFred Isaman * NB: take care not to mess about with data->commit et al. */ 14269ace33cdSFred Isaman 14279ace33cdSFred Isaman list_splice_init(head, &data->pages); 14289ace33cdSFred Isaman 14299ace33cdSFred Isaman data->inode = inode; 14309ace33cdSFred Isaman data->cred = first->wb_context->cred; 1431988b6dceSFred Isaman data->lseg = lseg; /* reference transferred */ 14329ace33cdSFred Isaman data->mds_ops = &nfs_commit_ops; 1433*f453a54aSFred Isaman data->completion_ops = cinfo->completion_ops; 14349ace33cdSFred Isaman 14359ace33cdSFred Isaman data->args.fh = NFS_FH(data->inode); 14369ace33cdSFred Isaman /* Note: we always request a commit of the entire inode */ 14379ace33cdSFred Isaman data->args.offset = 0; 14389ace33cdSFred Isaman data->args.count = 0; 14390b7c0153SFred Isaman data->context = get_nfs_open_context(first->wb_context); 14409ace33cdSFred Isaman data->res.fattr = &data->fattr; 14419ace33cdSFred Isaman data->res.verf = &data->verf; 14429ace33cdSFred Isaman nfs_fattr_init(&data->fattr); 14439ace33cdSFred Isaman } 1444e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit); 14459ace33cdSFred Isaman 1446e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list, 1447ea2cf228SFred Isaman struct pnfs_layout_segment *lseg, 1448ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 144964bfeb49SFred Isaman { 145064bfeb49SFred Isaman struct nfs_page *req; 145164bfeb49SFred Isaman 145264bfeb49SFred Isaman while (!list_empty(page_list)) { 145364bfeb49SFred Isaman req = nfs_list_entry(page_list->next); 145464bfeb49SFred Isaman nfs_list_remove_request(req); 1455ea2cf228SFred Isaman nfs_mark_request_commit(req, lseg, cinfo); 145664bfeb49SFred Isaman dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 145764bfeb49SFred Isaman dec_bdi_stat(req->wb_page->mapping->backing_dev_info, 145864bfeb49SFred Isaman BDI_RECLAIMABLE); 14599994b62bSFred Isaman nfs_unlock_request(req); 146064bfeb49SFred Isaman } 146164bfeb49SFred Isaman } 1462e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit); 146364bfeb49SFred Isaman 14649ace33cdSFred Isaman /* 14651da177e4SLinus Torvalds * Commit dirty pages 14661da177e4SLinus Torvalds */ 14671da177e4SLinus Torvalds static int 1468ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how, 1469ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 14701da177e4SLinus Torvalds { 14710b7c0153SFred Isaman struct nfs_commit_data *data; 14721da177e4SLinus Torvalds 1473c9d8f89dSTrond Myklebust data = nfs_commitdata_alloc(); 14741da177e4SLinus Torvalds 14751da177e4SLinus Torvalds if (!data) 14761da177e4SLinus Torvalds goto out_bad; 14771da177e4SLinus Torvalds 14781da177e4SLinus Torvalds /* Set up the argument struct */ 1479*f453a54aSFred Isaman nfs_init_commit(data, head, NULL, cinfo); 1480*f453a54aSFred Isaman atomic_inc(&cinfo->mds->rpcs_out); 14810b7c0153SFred Isaman return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops, how); 14821da177e4SLinus Torvalds out_bad: 1483ea2cf228SFred Isaman nfs_retry_commit(head, NULL, cinfo); 1484*f453a54aSFred Isaman cinfo->completion_ops->error_cleanup(NFS_I(inode)); 14851da177e4SLinus Torvalds return -ENOMEM; 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds 14881da177e4SLinus Torvalds /* 14891da177e4SLinus Torvalds * COMMIT call returned 14901da177e4SLinus Torvalds */ 1491788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata) 14921da177e4SLinus Torvalds { 14930b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 14941da177e4SLinus Torvalds 1495a3f565b1SChuck Lever dprintk("NFS: %5u nfs_commit_done (status %d)\n", 14961da177e4SLinus Torvalds task->tk_pid, task->tk_status); 14971da177e4SLinus Torvalds 1498788e7a89STrond Myklebust /* Call the NFS version-specific code */ 1499c0d0e96bSTrond Myklebust NFS_PROTO(data->inode)->commit_done(task, data); 1500c9d8f89dSTrond Myklebust } 1501c9d8f89dSTrond Myklebust 1502*f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data) 1503c9d8f89dSTrond Myklebust { 1504c9d8f89dSTrond Myklebust struct nfs_page *req; 1505c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1506*f453a54aSFred Isaman struct nfs_commit_info cinfo; 1507788e7a89STrond Myklebust 15081da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 15091da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 15101da177e4SLinus Torvalds nfs_list_remove_request(req); 1511d6d6dc7cSFred Isaman nfs_clear_page_commit(req->wb_page); 15121da177e4SLinus Torvalds 151348186c7dSChuck Lever dprintk("NFS: commit (%s/%lld %d@%lld)", 15143d4ff43dSAl Viro req->wb_context->dentry->d_sb->s_id, 15153d4ff43dSAl Viro (long long)NFS_FILEID(req->wb_context->dentry->d_inode), 15161da177e4SLinus Torvalds req->wb_bytes, 15171da177e4SLinus Torvalds (long long)req_offset(req)); 1518c9d8f89dSTrond Myklebust if (status < 0) { 1519c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 15201da177e4SLinus Torvalds nfs_inode_remove_request(req); 1521c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 15221da177e4SLinus Torvalds goto next; 15231da177e4SLinus Torvalds } 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds /* Okay, COMMIT succeeded, apparently. Check the verifier 15261da177e4SLinus Torvalds * returned by the server against all stored verfs. */ 15271da177e4SLinus Torvalds if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) { 15281da177e4SLinus Torvalds /* We have a match */ 15291da177e4SLinus Torvalds nfs_inode_remove_request(req); 15301da177e4SLinus Torvalds dprintk(" OK\n"); 15311da177e4SLinus Torvalds goto next; 15321da177e4SLinus Torvalds } 15331da177e4SLinus Torvalds /* We have a mismatch. Write the page again */ 15341da177e4SLinus Torvalds dprintk(" mismatch\n"); 15356d884e8fSFred nfs_mark_request_dirty(req); 15361da177e4SLinus Torvalds next: 15379994b62bSFred Isaman nfs_unlock_request(req); 15381da177e4SLinus Torvalds } 1539*f453a54aSFred Isaman nfs_init_cinfo(&cinfo, data->inode, data->dreq); 1540*f453a54aSFred Isaman if (atomic_dec_and_test(&cinfo.mds->rpcs_out)) 1541*f453a54aSFred Isaman nfs_commit_clear_lock(NFS_I(data->inode)); 15425917ce84SFred Isaman } 15435917ce84SFred Isaman 15445917ce84SFred Isaman static void nfs_commit_release(void *calldata) 15455917ce84SFred Isaman { 15460b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 15475917ce84SFred Isaman 1548*f453a54aSFred Isaman data->completion_ops->completion(data); 1549c9d8f89dSTrond Myklebust nfs_commitdata_release(calldata); 15501da177e4SLinus Torvalds } 1551788e7a89STrond Myklebust 1552788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = { 15530b7c0153SFred Isaman .rpc_call_prepare = nfs_commit_prepare, 1554788e7a89STrond Myklebust .rpc_call_done = nfs_commit_done, 1555788e7a89STrond Myklebust .rpc_release = nfs_commit_release, 1556788e7a89STrond Myklebust }; 15571da177e4SLinus Torvalds 1558*f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = { 1559*f453a54aSFred Isaman .completion = nfs_commit_release_pages, 1560*f453a54aSFred Isaman .error_cleanup = nfs_commit_clear_lock, 1561*f453a54aSFred Isaman }; 1562*f453a54aSFred Isaman 156384c53ab5SFred Isaman static int nfs_generic_commit_list(struct inode *inode, struct list_head *head, 1564ea2cf228SFred Isaman int how, struct nfs_commit_info *cinfo) 156584c53ab5SFred Isaman { 156684c53ab5SFred Isaman int status; 156784c53ab5SFred Isaman 1568ea2cf228SFred Isaman status = pnfs_commit_list(inode, head, how, cinfo); 156984c53ab5SFred Isaman if (status == PNFS_NOT_ATTEMPTED) 1570ea2cf228SFred Isaman status = nfs_commit_list(inode, head, how, cinfo); 157184c53ab5SFred Isaman return status; 157284c53ab5SFred Isaman } 157384c53ab5SFred Isaman 1574b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how) 15751da177e4SLinus Torvalds { 15761da177e4SLinus Torvalds LIST_HEAD(head); 1577ea2cf228SFred Isaman struct nfs_commit_info cinfo; 157871d0a611STrond Myklebust int may_wait = how & FLUSH_SYNC; 1579b8413f98STrond Myklebust int res; 15801da177e4SLinus Torvalds 1581b8413f98STrond Myklebust res = nfs_commit_set_lock(NFS_I(inode), may_wait); 1582b8413f98STrond Myklebust if (res <= 0) 1583c5efa5fcSTrond Myklebust goto out_mark_dirty; 1584ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, inode); 1585ea2cf228SFred Isaman res = nfs_scan_commit(inode, &head, &cinfo); 15861da177e4SLinus Torvalds if (res) { 1587a861a1e1SFred Isaman int error; 1588a861a1e1SFred Isaman 1589ea2cf228SFred Isaman error = nfs_generic_commit_list(inode, &head, how, &cinfo); 15901da177e4SLinus Torvalds if (error < 0) 15911da177e4SLinus Torvalds return error; 1592b8413f98STrond Myklebust if (!may_wait) 1593b8413f98STrond Myklebust goto out_mark_dirty; 1594b8413f98STrond Myklebust error = wait_on_bit(&NFS_I(inode)->flags, 1595b8413f98STrond Myklebust NFS_INO_COMMIT, 159671d0a611STrond Myklebust nfs_wait_bit_killable, 159771d0a611STrond Myklebust TASK_KILLABLE); 1598b8413f98STrond Myklebust if (error < 0) 1599b8413f98STrond Myklebust return error; 160071d0a611STrond Myklebust } else 160171d0a611STrond Myklebust nfs_commit_clear_lock(NFS_I(inode)); 1602c5efa5fcSTrond Myklebust return res; 1603c5efa5fcSTrond Myklebust /* Note: If we exit without ensuring that the commit is complete, 1604c5efa5fcSTrond Myklebust * we must mark the inode as dirty. Otherwise, future calls to 1605c5efa5fcSTrond Myklebust * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure 1606c5efa5fcSTrond Myklebust * that the data is on the disk. 1607c5efa5fcSTrond Myklebust */ 1608c5efa5fcSTrond Myklebust out_mark_dirty: 1609c5efa5fcSTrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 16101da177e4SLinus Torvalds return res; 16111da177e4SLinus Torvalds } 16128fc795f7STrond Myklebust 16138fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 16148fc795f7STrond Myklebust { 1615420e3646STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 1616420e3646STrond Myklebust int flags = FLUSH_SYNC; 1617420e3646STrond Myklebust int ret = 0; 16188fc795f7STrond Myklebust 16193236c3e1SJeff Layton /* no commits means nothing needs to be done */ 1620ea2cf228SFred Isaman if (!nfsi->commit_info.ncommit) 16213236c3e1SJeff Layton return ret; 16223236c3e1SJeff Layton 1623a00dd6c0SJeff Layton if (wbc->sync_mode == WB_SYNC_NONE) { 1624a00dd6c0SJeff Layton /* Don't commit yet if this is a non-blocking flush and there 1625a00dd6c0SJeff Layton * are a lot of outstanding writes for this mapping. 1626420e3646STrond Myklebust */ 1627ea2cf228SFred Isaman if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1)) 1628420e3646STrond Myklebust goto out_mark_dirty; 1629420e3646STrond Myklebust 1630a00dd6c0SJeff Layton /* don't wait for the COMMIT response */ 1631420e3646STrond Myklebust flags = 0; 1632a00dd6c0SJeff Layton } 1633a00dd6c0SJeff Layton 1634420e3646STrond Myklebust ret = nfs_commit_inode(inode, flags); 1635420e3646STrond Myklebust if (ret >= 0) { 1636420e3646STrond Myklebust if (wbc->sync_mode == WB_SYNC_NONE) { 1637420e3646STrond Myklebust if (ret < wbc->nr_to_write) 1638420e3646STrond Myklebust wbc->nr_to_write -= ret; 1639420e3646STrond Myklebust else 1640420e3646STrond Myklebust wbc->nr_to_write = 0; 1641420e3646STrond Myklebust } 16428fc795f7STrond Myklebust return 0; 1643420e3646STrond Myklebust } 1644420e3646STrond Myklebust out_mark_dirty: 16458fc795f7STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 16468fc795f7STrond Myklebust return ret; 16478fc795f7STrond Myklebust } 1648c63c7b05STrond Myklebust #else 16498fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 16508fc795f7STrond Myklebust { 16518fc795f7STrond Myklebust return 0; 16528fc795f7STrond Myklebust } 16531da177e4SLinus Torvalds #endif 16541da177e4SLinus Torvalds 16558fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc) 16568fc795f7STrond Myklebust { 1657863a3c6cSAndy Adamson int ret; 1658863a3c6cSAndy Adamson 1659863a3c6cSAndy Adamson ret = nfs_commit_unstable_pages(inode, wbc); 1660863a3c6cSAndy Adamson if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) { 1661ef311537SAndy Adamson int status; 1662ef311537SAndy Adamson bool sync = true; 1663863a3c6cSAndy Adamson 1664846d5a09SWu Fengguang if (wbc->sync_mode == WB_SYNC_NONE) 1665ef311537SAndy Adamson sync = false; 1666863a3c6cSAndy Adamson 1667863a3c6cSAndy Adamson status = pnfs_layoutcommit_inode(inode, sync); 1668863a3c6cSAndy Adamson if (status < 0) 1669863a3c6cSAndy Adamson return status; 1670863a3c6cSAndy Adamson } 1671863a3c6cSAndy Adamson return ret; 16728fc795f7STrond Myklebust } 16738fc795f7STrond Myklebust 1674acdc53b2STrond Myklebust /* 1675acdc53b2STrond Myklebust * flush the inode to disk. 1676acdc53b2STrond Myklebust */ 1677acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode) 167834901f70STrond Myklebust { 167934901f70STrond Myklebust struct writeback_control wbc = { 168072cb77f4STrond Myklebust .sync_mode = WB_SYNC_ALL, 168134901f70STrond Myklebust .nr_to_write = LONG_MAX, 1682d7fb1207STrond Myklebust .range_start = 0, 1683d7fb1207STrond Myklebust .range_end = LLONG_MAX, 168434901f70STrond Myklebust }; 168534901f70STrond Myklebust 1686acdc53b2STrond Myklebust return sync_inode(inode, &wbc); 16871c75950bSTrond Myklebust } 16881c75950bSTrond Myklebust 16891b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page) 16901b3b4a1aSTrond Myklebust { 16911b3b4a1aSTrond Myklebust struct nfs_page *req; 16921b3b4a1aSTrond Myklebust int ret = 0; 16931b3b4a1aSTrond Myklebust 16941b3b4a1aSTrond Myklebust BUG_ON(!PageLocked(page)); 16951b3b4a1aSTrond Myklebust for (;;) { 1696ba8b06e6STrond Myklebust wait_on_page_writeback(page); 16971b3b4a1aSTrond Myklebust req = nfs_page_find_request(page); 16981b3b4a1aSTrond Myklebust if (req == NULL) 16991b3b4a1aSTrond Myklebust break; 17001b3b4a1aSTrond Myklebust if (nfs_lock_request_dontget(req)) { 17018dd37758STrond Myklebust nfs_clear_request_commit(req); 17021b3b4a1aSTrond Myklebust nfs_inode_remove_request(req); 17031b3b4a1aSTrond Myklebust /* 17041b3b4a1aSTrond Myklebust * In case nfs_inode_remove_request has marked the 17051b3b4a1aSTrond Myklebust * page as being dirty 17061b3b4a1aSTrond Myklebust */ 17071b3b4a1aSTrond Myklebust cancel_dirty_page(page, PAGE_CACHE_SIZE); 17081b3b4a1aSTrond Myklebust nfs_unlock_request(req); 17091b3b4a1aSTrond Myklebust break; 17101b3b4a1aSTrond Myklebust } 17111b3b4a1aSTrond Myklebust ret = nfs_wait_on_request(req); 1712c9edda71STrond Myklebust nfs_release_request(req); 17131b3b4a1aSTrond Myklebust if (ret < 0) 1714c988950eSTrond Myklebust break; 17151b3b4a1aSTrond Myklebust } 17161b3b4a1aSTrond Myklebust return ret; 17171b3b4a1aSTrond Myklebust } 17181b3b4a1aSTrond Myklebust 17191c75950bSTrond Myklebust /* 17201c75950bSTrond Myklebust * Write back all requests on one page - we do this before reading it. 17211c75950bSTrond Myklebust */ 17221c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page) 17231c75950bSTrond Myklebust { 17247f2f12d9STrond Myklebust loff_t range_start = page_offset(page); 17257f2f12d9STrond Myklebust loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); 17267f2f12d9STrond Myklebust struct writeback_control wbc = { 17277f2f12d9STrond Myklebust .sync_mode = WB_SYNC_ALL, 17287f2f12d9STrond Myklebust .nr_to_write = 0, 17297f2f12d9STrond Myklebust .range_start = range_start, 17307f2f12d9STrond Myklebust .range_end = range_end, 17317f2f12d9STrond Myklebust }; 17327f2f12d9STrond Myklebust int ret; 17337f2f12d9STrond Myklebust 17340522f6adSTrond Myklebust for (;;) { 1735ba8b06e6STrond Myklebust wait_on_page_writeback(page); 17367f2f12d9STrond Myklebust if (clear_page_dirty_for_io(page)) { 17377f2f12d9STrond Myklebust ret = nfs_writepage_locked(page, &wbc); 17387f2f12d9STrond Myklebust if (ret < 0) 17397f2f12d9STrond Myklebust goto out_error; 17400522f6adSTrond Myklebust continue; 17417f2f12d9STrond Myklebust } 17420522f6adSTrond Myklebust if (!PagePrivate(page)) 17430522f6adSTrond Myklebust break; 17440522f6adSTrond Myklebust ret = nfs_commit_inode(inode, FLUSH_SYNC); 17457f2f12d9STrond Myklebust if (ret < 0) 17467f2f12d9STrond Myklebust goto out_error; 17477f2f12d9STrond Myklebust } 17487f2f12d9STrond Myklebust return 0; 17497f2f12d9STrond Myklebust out_error: 17507f2f12d9STrond Myklebust return ret; 17511c75950bSTrond Myklebust } 17521c75950bSTrond Myklebust 1753074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION 1754074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage, 1755a6bc32b8SMel Gorman struct page *page, enum migrate_mode mode) 1756074cc1deSTrond Myklebust { 17572da95652SJeff Layton /* 17582da95652SJeff Layton * If PagePrivate is set, then the page is currently associated with 17592da95652SJeff Layton * an in-progress read or write request. Don't try to migrate it. 17602da95652SJeff Layton * 17612da95652SJeff Layton * FIXME: we could do this in principle, but we'll need a way to ensure 17622da95652SJeff Layton * that we can safely release the inode reference while holding 17632da95652SJeff Layton * the page lock. 17642da95652SJeff Layton */ 17652da95652SJeff Layton if (PagePrivate(page)) 17662da95652SJeff Layton return -EBUSY; 1767074cc1deSTrond Myklebust 1768074cc1deSTrond Myklebust nfs_fscache_release_page(page, GFP_KERNEL); 1769074cc1deSTrond Myklebust 1770a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 1771074cc1deSTrond Myklebust } 1772074cc1deSTrond Myklebust #endif 1773074cc1deSTrond Myklebust 1774f7b422b1SDavid Howells int __init nfs_init_writepagecache(void) 17751da177e4SLinus Torvalds { 17761da177e4SLinus Torvalds nfs_wdata_cachep = kmem_cache_create("nfs_write_data", 1777cd841605SFred Isaman sizeof(struct nfs_write_header), 17781da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 177920c2df83SPaul Mundt NULL); 17801da177e4SLinus Torvalds if (nfs_wdata_cachep == NULL) 17811da177e4SLinus Torvalds return -ENOMEM; 17821da177e4SLinus Torvalds 178393d2341cSMatthew Dobson nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, 17841da177e4SLinus Torvalds nfs_wdata_cachep); 17851da177e4SLinus Torvalds if (nfs_wdata_mempool == NULL) 17861da177e4SLinus Torvalds return -ENOMEM; 17871da177e4SLinus Torvalds 17880b7c0153SFred Isaman nfs_cdata_cachep = kmem_cache_create("nfs_commit_data", 17890b7c0153SFred Isaman sizeof(struct nfs_commit_data), 17900b7c0153SFred Isaman 0, SLAB_HWCACHE_ALIGN, 17910b7c0153SFred Isaman NULL); 17920b7c0153SFred Isaman if (nfs_cdata_cachep == NULL) 17930b7c0153SFred Isaman return -ENOMEM; 17940b7c0153SFred Isaman 179593d2341cSMatthew Dobson nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, 17961da177e4SLinus Torvalds nfs_wdata_cachep); 17971da177e4SLinus Torvalds if (nfs_commit_mempool == NULL) 17981da177e4SLinus Torvalds return -ENOMEM; 17991da177e4SLinus Torvalds 180089a09141SPeter Zijlstra /* 180189a09141SPeter Zijlstra * NFS congestion size, scale with available memory. 180289a09141SPeter Zijlstra * 180389a09141SPeter Zijlstra * 64MB: 8192k 180489a09141SPeter Zijlstra * 128MB: 11585k 180589a09141SPeter Zijlstra * 256MB: 16384k 180689a09141SPeter Zijlstra * 512MB: 23170k 180789a09141SPeter Zijlstra * 1GB: 32768k 180889a09141SPeter Zijlstra * 2GB: 46340k 180989a09141SPeter Zijlstra * 4GB: 65536k 181089a09141SPeter Zijlstra * 8GB: 92681k 181189a09141SPeter Zijlstra * 16GB: 131072k 181289a09141SPeter Zijlstra * 181389a09141SPeter Zijlstra * This allows larger machines to have larger/more transfers. 181489a09141SPeter Zijlstra * Limit the default to 256M 181589a09141SPeter Zijlstra */ 181689a09141SPeter Zijlstra nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); 181789a09141SPeter Zijlstra if (nfs_congestion_kb > 256*1024) 181889a09141SPeter Zijlstra nfs_congestion_kb = 256*1024; 181989a09141SPeter Zijlstra 18201da177e4SLinus Torvalds return 0; 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds 1823266bee88SDavid Brownell void nfs_destroy_writepagecache(void) 18241da177e4SLinus Torvalds { 18251da177e4SLinus Torvalds mempool_destroy(nfs_commit_mempool); 18261da177e4SLinus Torvalds mempool_destroy(nfs_wdata_mempool); 18271a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_wdata_cachep); 18281da177e4SLinus Torvalds } 18291da177e4SLinus Torvalds 1830