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 34f4ce1299STrond Myklebust #include "nfstrace.h" 35f4ce1299STrond Myklebust 361da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_PAGECACHE 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds #define MIN_POOL_WRITE (32) 391da177e4SLinus Torvalds #define MIN_POOL_COMMIT (4) 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds /* 421da177e4SLinus Torvalds * Local function declarations 431da177e4SLinus Torvalds */ 44f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req); 456c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops; 46788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops; 47061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops; 48f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops; 491da177e4SLinus Torvalds 50e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep; 513feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool; 520b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep; 531da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool; 541da177e4SLinus Torvalds 550b7c0153SFred Isaman struct nfs_commit_data *nfs_commitdata_alloc(void) 561da177e4SLinus Torvalds { 57192e501bSMel Gorman struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOIO); 5840859d7eSChuck Lever 591da177e4SLinus Torvalds if (p) { 601da177e4SLinus Torvalds memset(p, 0, sizeof(*p)); 611da177e4SLinus Torvalds INIT_LIST_HEAD(&p->pages); 621da177e4SLinus Torvalds } 631da177e4SLinus Torvalds return p; 641da177e4SLinus Torvalds } 65e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc); 661da177e4SLinus Torvalds 670b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p) 681da177e4SLinus Torvalds { 691da177e4SLinus Torvalds mempool_free(p, nfs_commit_mempool); 701da177e4SLinus Torvalds } 71e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free); 721da177e4SLinus Torvalds 736c75dc0dSFred Isaman struct nfs_write_header *nfs_writehdr_alloc(void) 743feb2d49STrond Myklebust { 75192e501bSMel Gorman struct nfs_write_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO); 763feb2d49STrond Myklebust 773feb2d49STrond Myklebust if (p) { 78cd841605SFred Isaman struct nfs_pgio_header *hdr = &p->header; 79cd841605SFred Isaman 803feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 81cd841605SFred Isaman INIT_LIST_HEAD(&hdr->pages); 826c75dc0dSFred Isaman INIT_LIST_HEAD(&hdr->rpc_list); 836c75dc0dSFred Isaman spin_lock_init(&hdr->lock); 846c75dc0dSFred Isaman atomic_set(&hdr->refcnt, 0); 859bce008bSTrond Myklebust hdr->verf = &p->verf; 863feb2d49STrond Myklebust } 873feb2d49STrond Myklebust return p; 883feb2d49STrond Myklebust } 8989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_writehdr_alloc); 903feb2d49STrond Myklebust 911763da12SFred Isaman static struct nfs_write_data *nfs_writedata_alloc(struct nfs_pgio_header *hdr, 926c75dc0dSFred Isaman unsigned int pagecount) 936c75dc0dSFred Isaman { 946c75dc0dSFred Isaman struct nfs_write_data *data, *prealloc; 956c75dc0dSFred Isaman 966c75dc0dSFred Isaman prealloc = &container_of(hdr, struct nfs_write_header, header)->rpc_data; 976c75dc0dSFred Isaman if (prealloc->header == NULL) 986c75dc0dSFred Isaman data = prealloc; 996c75dc0dSFred Isaman else 1006c75dc0dSFred Isaman data = kzalloc(sizeof(*data), GFP_KERNEL); 1016c75dc0dSFred Isaman if (!data) 1026c75dc0dSFred Isaman goto out; 1036c75dc0dSFred Isaman 1046c75dc0dSFred Isaman if (nfs_pgarray_set(&data->pages, pagecount)) { 1056c75dc0dSFred Isaman data->header = hdr; 1066c75dc0dSFred Isaman atomic_inc(&hdr->refcnt); 1076c75dc0dSFred Isaman } else { 1086c75dc0dSFred Isaman if (data != prealloc) 1096c75dc0dSFred Isaman kfree(data); 1106c75dc0dSFred Isaman data = NULL; 1116c75dc0dSFred Isaman } 1126c75dc0dSFred Isaman out: 1136c75dc0dSFred Isaman return data; 1146c75dc0dSFred Isaman } 1156c75dc0dSFred Isaman 116cd841605SFred Isaman void nfs_writehdr_free(struct nfs_pgio_header *hdr) 1173feb2d49STrond Myklebust { 118cd841605SFred Isaman struct nfs_write_header *whdr = container_of(hdr, struct nfs_write_header, header); 119cd841605SFred Isaman mempool_free(whdr, nfs_wdata_mempool); 1203feb2d49STrond Myklebust } 12189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_writehdr_free); 1223feb2d49STrond Myklebust 123dce81290STrond Myklebust void nfs_writedata_release(struct nfs_write_data *wdata) 1241da177e4SLinus Torvalds { 1256c75dc0dSFred Isaman struct nfs_pgio_header *hdr = wdata->header; 1266c75dc0dSFred Isaman struct nfs_write_header *write_header = container_of(hdr, struct nfs_write_header, header); 1276c75dc0dSFred Isaman 128383ba719STrond Myklebust put_nfs_open_context(wdata->args.context); 12930dd374fSFred Isaman if (wdata->pages.pagevec != wdata->pages.page_array) 13030dd374fSFred Isaman kfree(wdata->pages.pagevec); 1316db6dd7dSTrond Myklebust if (wdata == &write_header->rpc_data) { 1326c75dc0dSFred Isaman wdata->header = NULL; 1336db6dd7dSTrond Myklebust wdata = NULL; 1346db6dd7dSTrond Myklebust } 1356c75dc0dSFred Isaman if (atomic_dec_and_test(&hdr->refcnt)) 136061ae2edSFred Isaman hdr->completion_ops->completion(hdr); 1376db6dd7dSTrond Myklebust /* Note: we only free the rpc_task after callbacks are done. 1386db6dd7dSTrond Myklebust * See the comment in rpc_free_task() for why 1396db6dd7dSTrond Myklebust */ 1406db6dd7dSTrond Myklebust kfree(wdata); 1411da177e4SLinus Torvalds } 14289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_writedata_release); 1431da177e4SLinus Torvalds 1447b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error) 1457b159fc1STrond Myklebust { 1467b159fc1STrond Myklebust ctx->error = error; 1477b159fc1STrond Myklebust smp_wmb(); 1487b159fc1STrond Myklebust set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 1497b159fc1STrond Myklebust } 1507b159fc1STrond Myklebust 15129418aa4SMel Gorman static struct nfs_page * 15229418aa4SMel Gorman nfs_page_find_request_locked(struct nfs_inode *nfsi, struct page *page) 153277459d2STrond Myklebust { 154277459d2STrond Myklebust struct nfs_page *req = NULL; 155277459d2STrond Myklebust 15629418aa4SMel Gorman if (PagePrivate(page)) 157277459d2STrond Myklebust req = (struct nfs_page *)page_private(page); 15829418aa4SMel Gorman else if (unlikely(PageSwapCache(page))) { 15929418aa4SMel Gorman struct nfs_page *freq, *t; 16029418aa4SMel Gorman 16129418aa4SMel Gorman /* Linearly search the commit list for the correct req */ 16229418aa4SMel Gorman list_for_each_entry_safe(freq, t, &nfsi->commit_info.list, wb_list) { 16329418aa4SMel Gorman if (freq->wb_page == page) { 16429418aa4SMel Gorman req = freq; 16529418aa4SMel Gorman break; 166277459d2STrond Myklebust } 16729418aa4SMel Gorman } 16829418aa4SMel Gorman } 16929418aa4SMel Gorman 17029418aa4SMel Gorman if (req) 17129418aa4SMel Gorman kref_get(&req->wb_kref); 17229418aa4SMel Gorman 173277459d2STrond Myklebust return req; 174277459d2STrond Myklebust } 175277459d2STrond Myklebust 176277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page) 177277459d2STrond Myklebust { 178d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 179277459d2STrond Myklebust struct nfs_page *req = NULL; 180277459d2STrond Myklebust 181587142f8STrond Myklebust spin_lock(&inode->i_lock); 18229418aa4SMel Gorman req = nfs_page_find_request_locked(NFS_I(inode), page); 183587142f8STrond Myklebust spin_unlock(&inode->i_lock); 184277459d2STrond Myklebust return req; 185277459d2STrond Myklebust } 186277459d2STrond Myklebust 1871da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */ 1881da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) 1891da177e4SLinus Torvalds { 190d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 191a3d01454STrond Myklebust loff_t end, i_size; 192a3d01454STrond Myklebust pgoff_t end_index; 1931da177e4SLinus Torvalds 194a3d01454STrond Myklebust spin_lock(&inode->i_lock); 195a3d01454STrond Myklebust i_size = i_size_read(inode); 196a3d01454STrond Myklebust end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; 197d56b4ddfSMel Gorman if (i_size > 0 && page_file_index(page) < end_index) 198a3d01454STrond Myklebust goto out; 199d56b4ddfSMel Gorman end = page_file_offset(page) + ((loff_t)offset+count); 2001da177e4SLinus Torvalds if (i_size >= end) 201a3d01454STrond Myklebust goto out; 2021da177e4SLinus Torvalds i_size_write(inode, end); 203a3d01454STrond Myklebust nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); 204a3d01454STrond Myklebust out: 205a3d01454STrond Myklebust spin_unlock(&inode->i_lock); 2061da177e4SLinus Torvalds } 2071da177e4SLinus Torvalds 208a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */ 209a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page) 210a301b777STrond Myklebust { 211d56b4ddfSMel Gorman nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page)); 212a301b777STrond Myklebust } 213a301b777STrond Myklebust 2141da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request 2151da177e4SLinus Torvalds * covers the full page. 2161da177e4SLinus Torvalds */ 2171da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count) 2181da177e4SLinus Torvalds { 2191da177e4SLinus Torvalds if (PageUptodate(page)) 2201da177e4SLinus Torvalds return; 2211da177e4SLinus Torvalds if (base != 0) 2221da177e4SLinus Torvalds return; 22349a70f27STrond Myklebust if (count != nfs_page_length(page)) 2241da177e4SLinus Torvalds return; 2251da177e4SLinus Torvalds SetPageUptodate(page); 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc) 2291da177e4SLinus Torvalds { 2301da177e4SLinus Torvalds if (wbc->for_reclaim) 231c63c7b05STrond Myklebust return FLUSH_HIGHPRI | FLUSH_STABLE; 232b17621feSWu Fengguang if (wbc->for_kupdate || wbc->for_background) 233b31268acSTrond Myklebust return FLUSH_LOWPRI | FLUSH_COND_STABLE; 234b31268acSTrond Myklebust return FLUSH_COND_STABLE; 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds /* 23889a09141SPeter Zijlstra * NFS congestion control 23989a09141SPeter Zijlstra */ 24089a09141SPeter Zijlstra 24189a09141SPeter Zijlstra int nfs_congestion_kb; 24289a09141SPeter Zijlstra 24389a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) 24489a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH \ 24589a09141SPeter Zijlstra (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) 24689a09141SPeter Zijlstra 247deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page) 24889a09141SPeter Zijlstra { 249deed85e7STrond Myklebust struct nfs_server *nfss = NFS_SERVER(page_file_mapping(page)->host); 2505a6d41b3STrond Myklebust int ret = test_set_page_writeback(page); 2515a6d41b3STrond Myklebust 252deed85e7STrond Myklebust WARN_ON_ONCE(ret != 0); 25389a09141SPeter Zijlstra 254277866a0SPeter Zijlstra if (atomic_long_inc_return(&nfss->writeback) > 2558aa7e847SJens Axboe NFS_CONGESTION_ON_THRESH) { 2568aa7e847SJens Axboe set_bdi_congested(&nfss->backing_dev_info, 2578aa7e847SJens Axboe BLK_RW_ASYNC); 2588aa7e847SJens Axboe } 25989a09141SPeter Zijlstra } 26089a09141SPeter Zijlstra 26189a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page) 26289a09141SPeter Zijlstra { 263d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 26489a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 26589a09141SPeter Zijlstra 26689a09141SPeter Zijlstra end_page_writeback(page); 267c4dc4beeSPeter Zijlstra if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) 2688aa7e847SJens Axboe clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC); 26989a09141SPeter Zijlstra } 27089a09141SPeter Zijlstra 271cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock) 272e261f51fSTrond Myklebust { 273d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 274e261f51fSTrond Myklebust struct nfs_page *req; 275e261f51fSTrond Myklebust int ret; 276e261f51fSTrond Myklebust 277587142f8STrond Myklebust spin_lock(&inode->i_lock); 278e261f51fSTrond Myklebust for (;;) { 27929418aa4SMel Gorman req = nfs_page_find_request_locked(NFS_I(inode), page); 280074cc1deSTrond Myklebust if (req == NULL) 281074cc1deSTrond Myklebust break; 2827ad84aa9STrond Myklebust if (nfs_lock_request(req)) 283e261f51fSTrond Myklebust break; 284e261f51fSTrond Myklebust /* Note: If we hold the page lock, as is the case in nfs_writepage, 2857ad84aa9STrond Myklebust * then the call to nfs_lock_request() will always 286e261f51fSTrond Myklebust * succeed provided that someone hasn't already marked the 287e261f51fSTrond Myklebust * request as dirty (in which case we don't care). 288e261f51fSTrond Myklebust */ 289587142f8STrond Myklebust spin_unlock(&inode->i_lock); 290cfb506e1STrond Myklebust if (!nonblock) 291e261f51fSTrond Myklebust ret = nfs_wait_on_request(req); 292cfb506e1STrond Myklebust else 293cfb506e1STrond Myklebust ret = -EAGAIN; 294e261f51fSTrond Myklebust nfs_release_request(req); 295e261f51fSTrond Myklebust if (ret != 0) 296074cc1deSTrond Myklebust return ERR_PTR(ret); 297587142f8STrond Myklebust spin_lock(&inode->i_lock); 298e261f51fSTrond Myklebust } 299587142f8STrond Myklebust spin_unlock(&inode->i_lock); 300074cc1deSTrond Myklebust return req; 301612c9384STrond Myklebust } 302074cc1deSTrond Myklebust 303074cc1deSTrond Myklebust /* 304074cc1deSTrond Myklebust * Find an associated nfs write request, and prepare to flush it out 305074cc1deSTrond Myklebust * May return an error if the user signalled nfs_wait_on_request(). 306074cc1deSTrond Myklebust */ 307074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, 308cfb506e1STrond Myklebust struct page *page, bool nonblock) 309074cc1deSTrond Myklebust { 310074cc1deSTrond Myklebust struct nfs_page *req; 311074cc1deSTrond Myklebust int ret = 0; 312074cc1deSTrond Myklebust 313cfb506e1STrond Myklebust req = nfs_find_and_lock_request(page, nonblock); 314074cc1deSTrond Myklebust if (!req) 315074cc1deSTrond Myklebust goto out; 316074cc1deSTrond Myklebust ret = PTR_ERR(req); 317074cc1deSTrond Myklebust if (IS_ERR(req)) 318074cc1deSTrond Myklebust goto out; 319074cc1deSTrond Myklebust 320deed85e7STrond Myklebust nfs_set_page_writeback(page); 321deed85e7STrond Myklebust WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags)); 322074cc1deSTrond Myklebust 323deed85e7STrond Myklebust ret = 0; 324f8512ad0SFred Isaman if (!nfs_pageio_add_request(pgio, req)) { 325f8512ad0SFred Isaman nfs_redirty_request(req); 326074cc1deSTrond Myklebust ret = pgio->pg_error; 327f8512ad0SFred Isaman } 328074cc1deSTrond Myklebust out: 329074cc1deSTrond Myklebust return ret; 330e261f51fSTrond Myklebust } 331e261f51fSTrond Myklebust 332f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio) 333f758c885STrond Myklebust { 334d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 335cfb506e1STrond Myklebust int ret; 336f758c885STrond Myklebust 337f758c885STrond Myklebust nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); 338f758c885STrond Myklebust nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); 339f758c885STrond Myklebust 340d56b4ddfSMel Gorman nfs_pageio_cond_complete(pgio, page_file_index(page)); 3411b430beeSWu Fengguang ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE); 342cfb506e1STrond Myklebust if (ret == -EAGAIN) { 343cfb506e1STrond Myklebust redirty_page_for_writepage(wbc, page); 344cfb506e1STrond Myklebust ret = 0; 345cfb506e1STrond Myklebust } 346cfb506e1STrond Myklebust return ret; 347f758c885STrond Myklebust } 348f758c885STrond Myklebust 349e261f51fSTrond Myklebust /* 3501da177e4SLinus Torvalds * Write an mmapped page to the server. 3511da177e4SLinus Torvalds */ 3524d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc) 3531da177e4SLinus Torvalds { 354f758c885STrond Myklebust struct nfs_pageio_descriptor pgio; 355e261f51fSTrond Myklebust int err; 3561da177e4SLinus Torvalds 357d56b4ddfSMel Gorman NFS_PROTO(page_file_mapping(page)->host)->write_pageio_init(&pgio, 35857208fa7SBryan Schumaker page->mapping->host, 35957208fa7SBryan Schumaker wb_priority(wbc), 360061ae2edSFred Isaman &nfs_async_write_completion_ops); 361f758c885STrond Myklebust err = nfs_do_writepage(page, wbc, &pgio); 362f758c885STrond Myklebust nfs_pageio_complete(&pgio); 363f758c885STrond Myklebust if (err < 0) 3644d770ccfSTrond Myklebust return err; 365f758c885STrond Myklebust if (pgio.pg_error < 0) 366f758c885STrond Myklebust return pgio.pg_error; 367f758c885STrond Myklebust return 0; 3684d770ccfSTrond Myklebust } 3694d770ccfSTrond Myklebust 3704d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc) 3714d770ccfSTrond Myklebust { 372f758c885STrond Myklebust int ret; 3734d770ccfSTrond Myklebust 374f758c885STrond Myklebust ret = nfs_writepage_locked(page, wbc); 3751da177e4SLinus Torvalds unlock_page(page); 376f758c885STrond Myklebust return ret; 377f758c885STrond Myklebust } 378f758c885STrond Myklebust 379f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) 380f758c885STrond Myklebust { 381f758c885STrond Myklebust int ret; 382f758c885STrond Myklebust 383f758c885STrond Myklebust ret = nfs_do_writepage(page, wbc, data); 384f758c885STrond Myklebust unlock_page(page); 385f758c885STrond Myklebust return ret; 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) 3891da177e4SLinus Torvalds { 3901da177e4SLinus Torvalds struct inode *inode = mapping->host; 39172cb77f4STrond Myklebust unsigned long *bitlock = &NFS_I(inode)->flags; 392c63c7b05STrond Myklebust struct nfs_pageio_descriptor pgio; 3931da177e4SLinus Torvalds int err; 3941da177e4SLinus Torvalds 39572cb77f4STrond Myklebust /* Stop dirtying of new pages while we sync */ 39672cb77f4STrond Myklebust err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING, 39772cb77f4STrond Myklebust nfs_wait_bit_killable, TASK_KILLABLE); 39872cb77f4STrond Myklebust if (err) 39972cb77f4STrond Myklebust goto out_err; 40072cb77f4STrond Myklebust 40191d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); 40291d5b470SChuck Lever 40357208fa7SBryan Schumaker NFS_PROTO(inode)->write_pageio_init(&pgio, inode, wb_priority(wbc), &nfs_async_write_completion_ops); 404f758c885STrond Myklebust err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); 405c63c7b05STrond Myklebust nfs_pageio_complete(&pgio); 40672cb77f4STrond Myklebust 40772cb77f4STrond Myklebust clear_bit_unlock(NFS_INO_FLUSHING, bitlock); 40872cb77f4STrond Myklebust smp_mb__after_clear_bit(); 40972cb77f4STrond Myklebust wake_up_bit(bitlock, NFS_INO_FLUSHING); 41072cb77f4STrond Myklebust 411f758c885STrond Myklebust if (err < 0) 41272cb77f4STrond Myklebust goto out_err; 41372cb77f4STrond Myklebust err = pgio.pg_error; 41472cb77f4STrond Myklebust if (err < 0) 41572cb77f4STrond Myklebust goto out_err; 416c63c7b05STrond Myklebust return 0; 41772cb77f4STrond Myklebust out_err: 41872cb77f4STrond Myklebust return err; 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds /* 4221da177e4SLinus Torvalds * Insert a write request into an inode 4231da177e4SLinus Torvalds */ 424d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req) 4251da177e4SLinus Torvalds { 4261da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 427e7d39069STrond Myklebust 428e7d39069STrond Myklebust /* Lock the request! */ 4297ad84aa9STrond Myklebust nfs_lock_request(req); 430e7d39069STrond Myklebust 431e7d39069STrond Myklebust spin_lock(&inode->i_lock); 432011e2a7fSBryan Schumaker if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE)) 433a9a4a87aSTrond Myklebust inode->i_version++; 43429418aa4SMel Gorman /* 43529418aa4SMel Gorman * Swap-space should not get truncated. Hence no need to plug the race 43629418aa4SMel Gorman * with invalidate/truncate. 43729418aa4SMel Gorman */ 43829418aa4SMel Gorman if (likely(!PageSwapCache(req->wb_page))) { 4392df485a7STrond Myklebust set_bit(PG_MAPPED, &req->wb_flags); 440deb7d638STrond Myklebust SetPagePrivate(req->wb_page); 441277459d2STrond Myklebust set_page_private(req->wb_page, (unsigned long)req); 44229418aa4SMel Gorman } 4431da177e4SLinus Torvalds nfsi->npages++; 444c03b4024STrond Myklebust kref_get(&req->wb_kref); 445e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds /* 44989a09141SPeter Zijlstra * Remove a write request from an inode 4501da177e4SLinus Torvalds */ 4511da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req) 4521da177e4SLinus Torvalds { 4533d4ff43dSAl Viro struct inode *inode = req->wb_context->dentry->d_inode; 4541da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 4551da177e4SLinus Torvalds 456587142f8STrond Myklebust spin_lock(&inode->i_lock); 45729418aa4SMel Gorman if (likely(!PageSwapCache(req->wb_page))) { 458277459d2STrond Myklebust set_page_private(req->wb_page, 0); 459deb7d638STrond Myklebust ClearPagePrivate(req->wb_page); 4602df485a7STrond Myklebust clear_bit(PG_MAPPED, &req->wb_flags); 46129418aa4SMel Gorman } 4621da177e4SLinus Torvalds nfsi->npages--; 463587142f8STrond Myklebust spin_unlock(&inode->i_lock); 4641da177e4SLinus Torvalds nfs_release_request(req); 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds 46761822ab5STrond Myklebust static void 4686d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req) 46961822ab5STrond Myklebust { 47061822ab5STrond Myklebust __set_page_dirty_nobuffers(req->wb_page); 47161822ab5STrond Myklebust } 47261822ab5STrond Myklebust 47389d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4) 4748dd37758STrond Myklebust /** 4758dd37758STrond Myklebust * nfs_request_add_commit_list - add request to a commit list 4768dd37758STrond Myklebust * @req: pointer to a struct nfs_page 477ea2cf228SFred Isaman * @dst: commit list head 478ea2cf228SFred Isaman * @cinfo: holds list lock and accounting info 4798dd37758STrond Myklebust * 480ea2cf228SFred Isaman * This sets the PG_CLEAN bit, updates the cinfo count of 4818dd37758STrond Myklebust * number of outstanding requests requiring a commit as well as 4828dd37758STrond Myklebust * the MM page stats. 4838dd37758STrond Myklebust * 484ea2cf228SFred Isaman * The caller must _not_ hold the cinfo->lock, but must be 4858dd37758STrond Myklebust * holding the nfs_page lock. 4868dd37758STrond Myklebust */ 4878dd37758STrond Myklebust void 488ea2cf228SFred Isaman nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst, 489ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 4908dd37758STrond Myklebust { 4918dd37758STrond Myklebust set_bit(PG_CLEAN, &(req)->wb_flags); 492ea2cf228SFred Isaman spin_lock(cinfo->lock); 493ea2cf228SFred Isaman nfs_list_add_request(req, dst); 494ea2cf228SFred Isaman cinfo->mds->ncommit++; 495ea2cf228SFred Isaman spin_unlock(cinfo->lock); 49656f9cd68SFred Isaman if (!cinfo->dreq) { 4978dd37758STrond Myklebust inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 498d56b4ddfSMel Gorman inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info, 49956f9cd68SFred Isaman BDI_RECLAIMABLE); 50056f9cd68SFred Isaman __mark_inode_dirty(req->wb_context->dentry->d_inode, 50156f9cd68SFred Isaman I_DIRTY_DATASYNC); 50256f9cd68SFred Isaman } 5038dd37758STrond Myklebust } 5048dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list); 5058dd37758STrond Myklebust 5068dd37758STrond Myklebust /** 5078dd37758STrond Myklebust * nfs_request_remove_commit_list - Remove request from a commit list 5088dd37758STrond Myklebust * @req: pointer to a nfs_page 509ea2cf228SFred Isaman * @cinfo: holds list lock and accounting info 5108dd37758STrond Myklebust * 511ea2cf228SFred Isaman * This clears the PG_CLEAN bit, and updates the cinfo's count of 5128dd37758STrond Myklebust * number of outstanding requests requiring a commit 5138dd37758STrond Myklebust * It does not update the MM page stats. 5148dd37758STrond Myklebust * 515ea2cf228SFred Isaman * The caller _must_ hold the cinfo->lock and the nfs_page lock. 5168dd37758STrond Myklebust */ 5178dd37758STrond Myklebust void 518ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req, 519ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 5208dd37758STrond Myklebust { 5218dd37758STrond Myklebust if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) 5228dd37758STrond Myklebust return; 5238dd37758STrond Myklebust nfs_list_remove_request(req); 524ea2cf228SFred Isaman cinfo->mds->ncommit--; 5258dd37758STrond Myklebust } 5268dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list); 5278dd37758STrond Myklebust 528ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, 529ea2cf228SFred Isaman struct inode *inode) 530ea2cf228SFred Isaman { 531ea2cf228SFred Isaman cinfo->lock = &inode->i_lock; 532ea2cf228SFred Isaman cinfo->mds = &NFS_I(inode)->commit_info; 533ea2cf228SFred Isaman cinfo->ds = pnfs_get_ds_info(inode); 534b359f9d0SFred Isaman cinfo->dreq = NULL; 535f453a54aSFred Isaman cinfo->completion_ops = &nfs_commit_completion_ops; 536ea2cf228SFred Isaman } 537ea2cf228SFred Isaman 538ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo, 539ea2cf228SFred Isaman struct inode *inode, 540ea2cf228SFred Isaman struct nfs_direct_req *dreq) 541ea2cf228SFred Isaman { 5421763da12SFred Isaman if (dreq) 5431763da12SFred Isaman nfs_init_cinfo_from_dreq(cinfo, dreq); 5441763da12SFred Isaman else 545ea2cf228SFred Isaman nfs_init_cinfo_from_inode(cinfo, inode); 546ea2cf228SFred Isaman } 547ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo); 5488dd37758STrond Myklebust 5491da177e4SLinus Torvalds /* 5501da177e4SLinus Torvalds * Add a request to the inode's commit list. 5511da177e4SLinus Torvalds */ 5521763da12SFred Isaman void 553ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, 554ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 5551da177e4SLinus Torvalds { 556ea2cf228SFred Isaman if (pnfs_mark_request_commit(req, lseg, cinfo)) 5578dd37758STrond Myklebust return; 558ea2cf228SFred Isaman nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo); 5591da177e4SLinus Torvalds } 5608e821cadSTrond Myklebust 561d6d6dc7cSFred Isaman static void 562d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page) 563e468bae9STrond Myklebust { 564e468bae9STrond Myklebust dec_zone_page_state(page, NR_UNSTABLE_NFS); 565d56b4ddfSMel Gorman dec_bdi_stat(page_file_mapping(page)->backing_dev_info, BDI_RECLAIMABLE); 566e468bae9STrond Myklebust } 567d6d6dc7cSFred Isaman 5688dd37758STrond Myklebust static void 569d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req) 570d6d6dc7cSFred Isaman { 5718dd37758STrond Myklebust if (test_bit(PG_CLEAN, &req->wb_flags)) { 5728dd37758STrond Myklebust struct inode *inode = req->wb_context->dentry->d_inode; 573ea2cf228SFred Isaman struct nfs_commit_info cinfo; 574d6d6dc7cSFred Isaman 575ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, inode); 576ea2cf228SFred Isaman if (!pnfs_clear_request_commit(req, &cinfo)) { 577ea2cf228SFred Isaman spin_lock(cinfo.lock); 578ea2cf228SFred Isaman nfs_request_remove_commit_list(req, &cinfo); 579ea2cf228SFred Isaman spin_unlock(cinfo.lock); 580d6d6dc7cSFred Isaman } 5818dd37758STrond Myklebust nfs_clear_page_commit(req->wb_page); 5828dd37758STrond Myklebust } 583e468bae9STrond Myklebust } 584e468bae9STrond Myklebust 5858e821cadSTrond Myklebust static inline 5868e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 5878e821cadSTrond Myklebust { 588465d5243SFred Isaman if (data->verf.committed == NFS_DATA_SYNC) 589cd841605SFred Isaman return data->header->lseg == NULL; 5908e821cadSTrond Myklebust return data->verf.committed != NFS_FILE_SYNC; 5918e821cadSTrond Myklebust } 5928e821cadSTrond Myklebust 5938e821cadSTrond Myklebust #else 59468cd6fa4SBryan Schumaker static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, 59568cd6fa4SBryan Schumaker struct inode *inode) 59668cd6fa4SBryan Schumaker { 59768cd6fa4SBryan Schumaker } 59868cd6fa4SBryan Schumaker 59968cd6fa4SBryan Schumaker void nfs_init_cinfo(struct nfs_commit_info *cinfo, 60068cd6fa4SBryan Schumaker struct inode *inode, 60168cd6fa4SBryan Schumaker struct nfs_direct_req *dreq) 60268cd6fa4SBryan Schumaker { 60368cd6fa4SBryan Schumaker } 60468cd6fa4SBryan Schumaker 6051763da12SFred Isaman void 606ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, 607ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 6088e821cadSTrond Myklebust { 6098e821cadSTrond Myklebust } 6108e821cadSTrond Myklebust 6118dd37758STrond Myklebust static void 612e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req) 613e468bae9STrond Myklebust { 614e468bae9STrond Myklebust } 615e468bae9STrond Myklebust 6168e821cadSTrond Myklebust static inline 6178e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data) 6188e821cadSTrond Myklebust { 6198e821cadSTrond Myklebust return 0; 6208e821cadSTrond Myklebust } 6218e821cadSTrond Myklebust 6221da177e4SLinus Torvalds #endif 6231da177e4SLinus Torvalds 624061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr) 6256c75dc0dSFred Isaman { 626ea2cf228SFred Isaman struct nfs_commit_info cinfo; 6276c75dc0dSFred Isaman unsigned long bytes = 0; 6286c75dc0dSFred Isaman 6296c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 6306c75dc0dSFred Isaman goto out; 631ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, hdr->inode); 6326c75dc0dSFred Isaman while (!list_empty(&hdr->pages)) { 6336c75dc0dSFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 6346c75dc0dSFred Isaman 6356c75dc0dSFred Isaman bytes += req->wb_bytes; 6366c75dc0dSFred Isaman nfs_list_remove_request(req); 6376c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && 6386c75dc0dSFred Isaman (hdr->good_bytes < bytes)) { 639d1182b33STrond Myklebust nfs_set_pageerror(req->wb_page); 6406c75dc0dSFred Isaman nfs_context_set_write_error(req->wb_context, hdr->error); 6416c75dc0dSFred Isaman goto remove_req; 6426c75dc0dSFred Isaman } 6436c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) { 6446c75dc0dSFred Isaman nfs_mark_request_dirty(req); 6456c75dc0dSFred Isaman goto next; 6466c75dc0dSFred Isaman } 6476c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) { 6482f2c63bcSTrond Myklebust memcpy(&req->wb_verf, &hdr->verf->verifier, sizeof(req->wb_verf)); 649ea2cf228SFred Isaman nfs_mark_request_commit(req, hdr->lseg, &cinfo); 6506c75dc0dSFred Isaman goto next; 6516c75dc0dSFred Isaman } 6526c75dc0dSFred Isaman remove_req: 6536c75dc0dSFred Isaman nfs_inode_remove_request(req); 6546c75dc0dSFred Isaman next: 6551d1afcbcSTrond Myklebust nfs_unlock_request(req); 656d1182b33STrond Myklebust nfs_end_page_writeback(req->wb_page); 6573aff4ebbSTrond Myklebust nfs_release_request(req); 6586c75dc0dSFred Isaman } 6596c75dc0dSFred Isaman out: 6606c75dc0dSFred Isaman hdr->release(hdr); 6616c75dc0dSFred Isaman } 6626c75dc0dSFred Isaman 66389d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4) 664ea2cf228SFred Isaman static unsigned long 665ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo) 666fb8a1f11STrond Myklebust { 667ea2cf228SFred Isaman return cinfo->mds->ncommit; 668fb8a1f11STrond Myklebust } 669fb8a1f11STrond Myklebust 670ea2cf228SFred Isaman /* cinfo->lock held by caller */ 6711763da12SFred Isaman int 672ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst, 673ea2cf228SFred Isaman struct nfs_commit_info *cinfo, int max) 674d6d6dc7cSFred Isaman { 675d6d6dc7cSFred Isaman struct nfs_page *req, *tmp; 676d6d6dc7cSFred Isaman int ret = 0; 677d6d6dc7cSFred Isaman 678d6d6dc7cSFred Isaman list_for_each_entry_safe(req, tmp, src, wb_list) { 6798dd37758STrond Myklebust if (!nfs_lock_request(req)) 6808dd37758STrond Myklebust continue; 6817ad84aa9STrond Myklebust kref_get(&req->wb_kref); 682ea2cf228SFred Isaman if (cond_resched_lock(cinfo->lock)) 6833b3be88dSTrond Myklebust list_safe_reset_next(req, tmp, wb_list); 684ea2cf228SFred Isaman nfs_request_remove_commit_list(req, cinfo); 6858dd37758STrond Myklebust nfs_list_add_request(req, dst); 686d6d6dc7cSFred Isaman ret++; 6871763da12SFred Isaman if ((ret == max) && !cinfo->dreq) 688d6d6dc7cSFred Isaman break; 689d6d6dc7cSFred Isaman } 690d6d6dc7cSFred Isaman return ret; 691d6d6dc7cSFred Isaman } 692d6d6dc7cSFred Isaman 6931da177e4SLinus Torvalds /* 6941da177e4SLinus Torvalds * nfs_scan_commit - Scan an inode for commit requests 6951da177e4SLinus Torvalds * @inode: NFS inode to scan 696ea2cf228SFred Isaman * @dst: mds destination list 697ea2cf228SFred Isaman * @cinfo: mds and ds lists of reqs ready to commit 6981da177e4SLinus Torvalds * 6991da177e4SLinus Torvalds * Moves requests from the inode's 'commit' request list. 7001da177e4SLinus Torvalds * The requests are *not* checked to ensure that they form a contiguous set. 7011da177e4SLinus Torvalds */ 7021763da12SFred Isaman int 703ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst, 704ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 7051da177e4SLinus Torvalds { 706d6d6dc7cSFred Isaman int ret = 0; 707fb8a1f11STrond Myklebust 708ea2cf228SFred Isaman spin_lock(cinfo->lock); 709ea2cf228SFred Isaman if (cinfo->mds->ncommit > 0) { 7108dd37758STrond Myklebust const int max = INT_MAX; 711d6d6dc7cSFred Isaman 712ea2cf228SFred Isaman ret = nfs_scan_commit_list(&cinfo->mds->list, dst, 713ea2cf228SFred Isaman cinfo, max); 714ea2cf228SFred Isaman ret += pnfs_scan_commit_lists(inode, cinfo, max - ret); 715d6d6dc7cSFred Isaman } 716ea2cf228SFred Isaman spin_unlock(cinfo->lock); 717ff778d02STrond Myklebust return ret; 7181da177e4SLinus Torvalds } 719d6d6dc7cSFred Isaman 720c42de9ddSTrond Myklebust #else 721ea2cf228SFred Isaman static unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo) 722fb8a1f11STrond Myklebust { 723fb8a1f11STrond Myklebust return 0; 724fb8a1f11STrond Myklebust } 725fb8a1f11STrond Myklebust 7261763da12SFred Isaman int nfs_scan_commit(struct inode *inode, struct list_head *dst, 727ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 728c42de9ddSTrond Myklebust { 729c42de9ddSTrond Myklebust return 0; 730c42de9ddSTrond Myklebust } 7311da177e4SLinus Torvalds #endif 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds /* 734e7d39069STrond Myklebust * Search for an existing write request, and attempt to update 735e7d39069STrond Myklebust * it to reflect a new dirty region on a given page. 7361da177e4SLinus Torvalds * 737e7d39069STrond Myklebust * If the attempt fails, then the existing request is flushed out 738e7d39069STrond Myklebust * to disk. 7391da177e4SLinus Torvalds */ 740e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode, 741e7d39069STrond Myklebust struct page *page, 742e7d39069STrond Myklebust unsigned int offset, 743e7d39069STrond Myklebust unsigned int bytes) 7441da177e4SLinus Torvalds { 745e7d39069STrond Myklebust struct nfs_page *req; 746e7d39069STrond Myklebust unsigned int rqend; 747e7d39069STrond Myklebust unsigned int end; 7481da177e4SLinus Torvalds int error; 749277459d2STrond Myklebust 750e7d39069STrond Myklebust if (!PagePrivate(page)) 751e7d39069STrond Myklebust return NULL; 752e7d39069STrond Myklebust 753e7d39069STrond Myklebust end = offset + bytes; 754e7d39069STrond Myklebust spin_lock(&inode->i_lock); 755e7d39069STrond Myklebust 756e7d39069STrond Myklebust for (;;) { 75729418aa4SMel Gorman req = nfs_page_find_request_locked(NFS_I(inode), page); 758e7d39069STrond Myklebust if (req == NULL) 759e7d39069STrond Myklebust goto out_unlock; 760e7d39069STrond Myklebust 761e7d39069STrond Myklebust rqend = req->wb_offset + req->wb_bytes; 762e7d39069STrond Myklebust /* 763e7d39069STrond Myklebust * Tell the caller to flush out the request if 764e7d39069STrond Myklebust * the offsets are non-contiguous. 765e7d39069STrond Myklebust * Note: nfs_flush_incompatible() will already 766e7d39069STrond Myklebust * have flushed out requests having wrong owners. 767e7d39069STrond Myklebust */ 768e468bae9STrond Myklebust if (offset > rqend 769e7d39069STrond Myklebust || end < req->wb_offset) 770e7d39069STrond Myklebust goto out_flushme; 771e7d39069STrond Myklebust 7727ad84aa9STrond Myklebust if (nfs_lock_request(req)) 773e7d39069STrond Myklebust break; 774e7d39069STrond Myklebust 775e7d39069STrond Myklebust /* The request is locked, so wait and then retry */ 776587142f8STrond Myklebust spin_unlock(&inode->i_lock); 7771da177e4SLinus Torvalds error = nfs_wait_on_request(req); 7781da177e4SLinus Torvalds nfs_release_request(req); 779e7d39069STrond Myklebust if (error != 0) 780e7d39069STrond Myklebust goto out_err; 781e7d39069STrond Myklebust spin_lock(&inode->i_lock); 7821da177e4SLinus Torvalds } 7831da177e4SLinus Torvalds 7841da177e4SLinus Torvalds /* Okay, the request matches. Update the region */ 7851da177e4SLinus Torvalds if (offset < req->wb_offset) { 7861da177e4SLinus Torvalds req->wb_offset = offset; 7871da177e4SLinus Torvalds req->wb_pgbase = offset; 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds if (end > rqend) 7901da177e4SLinus Torvalds req->wb_bytes = end - req->wb_offset; 791e7d39069STrond Myklebust else 792e7d39069STrond Myklebust req->wb_bytes = rqend - req->wb_offset; 793e7d39069STrond Myklebust out_unlock: 794e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 795ca138f36SFred Isaman if (req) 7968dd37758STrond Myklebust nfs_clear_request_commit(req); 797e7d39069STrond Myklebust return req; 798e7d39069STrond Myklebust out_flushme: 799e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 800e7d39069STrond Myklebust nfs_release_request(req); 801e7d39069STrond Myklebust error = nfs_wb_page(inode, page); 802e7d39069STrond Myklebust out_err: 803e7d39069STrond Myklebust return ERR_PTR(error); 804e7d39069STrond Myklebust } 8051da177e4SLinus Torvalds 806e7d39069STrond Myklebust /* 807e7d39069STrond Myklebust * Try to update an existing write request, or create one if there is none. 808e7d39069STrond Myklebust * 809e7d39069STrond Myklebust * Note: Should always be called with the Page Lock held to prevent races 810e7d39069STrond Myklebust * if we have to add a new request. Also assumes that the caller has 811e7d39069STrond Myklebust * already called nfs_flush_incompatible() if necessary. 812e7d39069STrond Myklebust */ 813e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, 814e7d39069STrond Myklebust struct page *page, unsigned int offset, unsigned int bytes) 815e7d39069STrond Myklebust { 816d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 817e7d39069STrond Myklebust struct nfs_page *req; 818e7d39069STrond Myklebust 819e7d39069STrond Myklebust req = nfs_try_to_update_request(inode, page, offset, bytes); 820e7d39069STrond Myklebust if (req != NULL) 821e7d39069STrond Myklebust goto out; 822e7d39069STrond Myklebust req = nfs_create_request(ctx, inode, page, offset, bytes); 823e7d39069STrond Myklebust if (IS_ERR(req)) 824e7d39069STrond Myklebust goto out; 825d6d6dc7cSFred Isaman nfs_inode_add_request(inode, req); 826efc91ed0STrond Myklebust out: 82761e930a9STrond Myklebust return req; 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds 830e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, 831e7d39069STrond Myklebust unsigned int offset, unsigned int count) 832e7d39069STrond Myklebust { 833e7d39069STrond Myklebust struct nfs_page *req; 834e7d39069STrond Myklebust 835e7d39069STrond Myklebust req = nfs_setup_write_request(ctx, page, offset, count); 836e7d39069STrond Myklebust if (IS_ERR(req)) 837e7d39069STrond Myklebust return PTR_ERR(req); 838e7d39069STrond Myklebust /* Update file length */ 839e7d39069STrond Myklebust nfs_grow_file(page, offset, count); 840e7d39069STrond Myklebust nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); 841a6305ddbSTrond Myklebust nfs_mark_request_dirty(req); 8421d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 843e7d39069STrond Myklebust return 0; 844e7d39069STrond Myklebust } 845e7d39069STrond Myklebust 8461da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page) 8471da177e4SLinus Torvalds { 848cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 8492a369153STrond Myklebust struct nfs_lock_context *l_ctx; 8501da177e4SLinus Torvalds struct nfs_page *req; 8511a54533eSTrond Myklebust int do_flush, status; 8521da177e4SLinus Torvalds /* 8531da177e4SLinus Torvalds * Look for a request corresponding to this page. If there 8541da177e4SLinus Torvalds * is one, and it belongs to another file, we flush it out 8551da177e4SLinus Torvalds * before we try to copy anything into the page. Do this 8561da177e4SLinus Torvalds * due to the lack of an ACCESS-type call in NFSv2. 8571da177e4SLinus Torvalds * Also do the same if we find a request from an existing 8581da177e4SLinus Torvalds * dropped page. 8591da177e4SLinus Torvalds */ 8601a54533eSTrond Myklebust do { 861277459d2STrond Myklebust req = nfs_page_find_request(page); 8621a54533eSTrond Myklebust if (req == NULL) 8631a54533eSTrond Myklebust return 0; 8642a369153STrond Myklebust l_ctx = req->wb_lock_context; 8652a369153STrond Myklebust do_flush = req->wb_page != page || req->wb_context != ctx; 8660f1d2605STrond Myklebust if (l_ctx && ctx->dentry->d_inode->i_flock != NULL) { 8672a369153STrond Myklebust do_flush |= l_ctx->lockowner.l_owner != current->files 8682a369153STrond Myklebust || l_ctx->lockowner.l_pid != current->tgid; 8692a369153STrond Myklebust } 8701da177e4SLinus Torvalds nfs_release_request(req); 8711a54533eSTrond Myklebust if (!do_flush) 8721a54533eSTrond Myklebust return 0; 873d56b4ddfSMel Gorman status = nfs_wb_page(page_file_mapping(page)->host, page); 8741a54533eSTrond Myklebust } while (status == 0); 8751a54533eSTrond Myklebust return status; 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds /* 879dc24826bSAndy Adamson * Avoid buffered writes when a open context credential's key would 880dc24826bSAndy Adamson * expire soon. 881dc24826bSAndy Adamson * 882dc24826bSAndy Adamson * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL. 883dc24826bSAndy Adamson * 884dc24826bSAndy Adamson * Return 0 and set a credential flag which triggers the inode to flush 885dc24826bSAndy Adamson * and performs NFS_FILE_SYNC writes if the key will expired within 886dc24826bSAndy Adamson * RPC_KEY_EXPIRE_TIMEO. 887dc24826bSAndy Adamson */ 888dc24826bSAndy Adamson int 889dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode) 890dc24826bSAndy Adamson { 891dc24826bSAndy Adamson struct nfs_open_context *ctx = nfs_file_open_context(filp); 892dc24826bSAndy Adamson struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth; 893dc24826bSAndy Adamson 894dc24826bSAndy Adamson return rpcauth_key_timeout_notify(auth, ctx->cred); 895dc24826bSAndy Adamson } 896dc24826bSAndy Adamson 897dc24826bSAndy Adamson /* 898dc24826bSAndy Adamson * Test if the open context credential key is marked to expire soon. 899dc24826bSAndy Adamson */ 900dc24826bSAndy Adamson bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx) 901dc24826bSAndy Adamson { 902dc24826bSAndy Adamson return rpcauth_cred_key_to_expire(ctx->cred); 903dc24826bSAndy Adamson } 904dc24826bSAndy Adamson 905dc24826bSAndy Adamson /* 9065d47a356STrond Myklebust * If the page cache is marked as unsafe or invalid, then we can't rely on 9075d47a356STrond Myklebust * the PageUptodate() flag. In this case, we will need to turn off 9085d47a356STrond Myklebust * write optimisations that depend on the page contents being correct. 9095d47a356STrond Myklebust */ 9108d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode) 9115d47a356STrond Myklebust { 9128d197a56STrond Myklebust if (nfs_have_delegated_attributes(inode)) 9138d197a56STrond Myklebust goto out; 91481d9bce5SJeff Layton if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_DATA|NFS_INO_REVAL_PAGECACHE)) 9158d197a56STrond Myklebust return false; 9168d197a56STrond Myklebust out: 9178d197a56STrond Myklebust return PageUptodate(page) != 0; 9185d47a356STrond Myklebust } 9195d47a356STrond Myklebust 920c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or 921c7559663SScott Mayhew * if we have the whole file locked for writing), it may be more efficient to 922c7559663SScott Mayhew * extend the write to cover the entire page in order to avoid fragmentation 923c7559663SScott Mayhew * inefficiencies. 924c7559663SScott Mayhew * 925263b4509SScott Mayhew * If the file is opened for synchronous writes then we can just skip the rest 926263b4509SScott Mayhew * of the checks. 927c7559663SScott Mayhew */ 928c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode) 929c7559663SScott Mayhew { 930c7559663SScott Mayhew if (file->f_flags & O_DSYNC) 931c7559663SScott Mayhew return 0; 932263b4509SScott Mayhew if (!nfs_write_pageuptodate(page, inode)) 933263b4509SScott Mayhew return 0; 934c7559663SScott Mayhew if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE)) 935c7559663SScott Mayhew return 1; 936263b4509SScott Mayhew if (inode->i_flock == NULL || (inode->i_flock->fl_start == 0 && 937c7559663SScott Mayhew inode->i_flock->fl_end == OFFSET_MAX && 938263b4509SScott Mayhew inode->i_flock->fl_type != F_RDLCK)) 939c7559663SScott Mayhew return 1; 940c7559663SScott Mayhew return 0; 941c7559663SScott Mayhew } 942c7559663SScott Mayhew 9435d47a356STrond Myklebust /* 9441da177e4SLinus Torvalds * Update and possibly write a cached page of an NFS file. 9451da177e4SLinus Torvalds * 9461da177e4SLinus Torvalds * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad 9471da177e4SLinus Torvalds * things with a page scheduled for an RPC call (e.g. invalidate it). 9481da177e4SLinus Torvalds */ 9491da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page, 9501da177e4SLinus Torvalds unsigned int offset, unsigned int count) 9511da177e4SLinus Torvalds { 952cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 953d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 9541da177e4SLinus Torvalds int status = 0; 9551da177e4SLinus Torvalds 95691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 95791d5b470SChuck Lever 9586de1472fSAl Viro dprintk("NFS: nfs_updatepage(%pD2 %d@%lld)\n", 9596de1472fSAl Viro file, count, (long long)(page_file_offset(page) + offset)); 9601da177e4SLinus Torvalds 961c7559663SScott Mayhew if (nfs_can_extend_write(file, page, inode)) { 96249a70f27STrond Myklebust count = max(count + offset, nfs_page_length(page)); 9631da177e4SLinus Torvalds offset = 0; 9641da177e4SLinus Torvalds } 9651da177e4SLinus Torvalds 966e21195a7STrond Myklebust status = nfs_writepage_setup(ctx, page, offset, count); 96703fa9e84STrond Myklebust if (status < 0) 96803fa9e84STrond Myklebust nfs_set_pageerror(page); 96959b7c05fSTrond Myklebust else 97059b7c05fSTrond Myklebust __set_page_dirty_nobuffers(page); 9711da177e4SLinus Torvalds 97248186c7dSChuck Lever dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", 9731da177e4SLinus Torvalds status, (long long)i_size_read(inode)); 9741da177e4SLinus Torvalds return status; 9751da177e4SLinus Torvalds } 9761da177e4SLinus Torvalds 9773ff7576dSTrond Myklebust static int flush_task_priority(int how) 9781da177e4SLinus Torvalds { 9791da177e4SLinus Torvalds switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { 9801da177e4SLinus Torvalds case FLUSH_HIGHPRI: 9811da177e4SLinus Torvalds return RPC_PRIORITY_HIGH; 9821da177e4SLinus Torvalds case FLUSH_LOWPRI: 9831da177e4SLinus Torvalds return RPC_PRIORITY_LOW; 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds return RPC_PRIORITY_NORMAL; 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 988c5996c4eSFred Isaman int nfs_initiate_write(struct rpc_clnt *clnt, 989c5996c4eSFred Isaman struct nfs_write_data *data, 990788e7a89STrond Myklebust const struct rpc_call_ops *call_ops, 9919f0ec176SAndy Adamson int how, int flags) 9921da177e4SLinus Torvalds { 993cd841605SFred Isaman struct inode *inode = data->header->inode; 9943ff7576dSTrond Myklebust int priority = flush_task_priority(how); 99507737691STrond Myklebust struct rpc_task *task; 996bdc7f021STrond Myklebust struct rpc_message msg = { 997bdc7f021STrond Myklebust .rpc_argp = &data->args, 998bdc7f021STrond Myklebust .rpc_resp = &data->res, 999cd841605SFred Isaman .rpc_cred = data->header->cred, 1000bdc7f021STrond Myklebust }; 100184115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 1002d138d5d1SAndy Adamson .rpc_client = clnt, 100307737691STrond Myklebust .task = &data->task, 1004bdc7f021STrond Myklebust .rpc_message = &msg, 100584115e1cSTrond Myklebust .callback_ops = call_ops, 100684115e1cSTrond Myklebust .callback_data = data, 1007101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 10089f0ec176SAndy Adamson .flags = RPC_TASK_ASYNC | flags, 10093ff7576dSTrond Myklebust .priority = priority, 101084115e1cSTrond Myklebust }; 10112c61be0aSTrond Myklebust int ret = 0; 10121da177e4SLinus Torvalds 1013d138d5d1SAndy Adamson /* Set up the initial task struct. */ 1014d138d5d1SAndy Adamson NFS_PROTO(inode)->write_setup(data, &msg); 1015d138d5d1SAndy Adamson 1016d138d5d1SAndy Adamson dprintk("NFS: %5u initiated write call " 10171e8968c5SNiels de Vos "(req %s/%llu, %u bytes @ offset %llu)\n", 1018d138d5d1SAndy Adamson data->task.tk_pid, 1019d138d5d1SAndy Adamson inode->i_sb->s_id, 10201e8968c5SNiels de Vos (unsigned long long)NFS_FILEID(inode), 1021d138d5d1SAndy Adamson data->args.count, 1022d138d5d1SAndy Adamson (unsigned long long)data->args.offset); 1023d138d5d1SAndy Adamson 10248c21c62cSWeston Andros Adamson nfs4_state_protect_write(NFS_SERVER(inode)->nfs_client, 10258c21c62cSWeston Andros Adamson &task_setup_data.rpc_client, &msg, data); 10268c21c62cSWeston Andros Adamson 1027d138d5d1SAndy Adamson task = rpc_run_task(&task_setup_data); 1028d138d5d1SAndy Adamson if (IS_ERR(task)) { 1029d138d5d1SAndy Adamson ret = PTR_ERR(task); 1030d138d5d1SAndy Adamson goto out; 1031d138d5d1SAndy Adamson } 1032d138d5d1SAndy Adamson if (how & FLUSH_SYNC) { 1033d138d5d1SAndy Adamson ret = rpc_wait_for_completion_task(task); 1034d138d5d1SAndy Adamson if (ret == 0) 1035d138d5d1SAndy Adamson ret = task->tk_status; 1036d138d5d1SAndy Adamson } 1037d138d5d1SAndy Adamson rpc_put_task(task); 1038d138d5d1SAndy Adamson out: 1039d138d5d1SAndy Adamson return ret; 1040d138d5d1SAndy Adamson } 1041a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write); 1042d138d5d1SAndy Adamson 1043d138d5d1SAndy Adamson /* 1044d138d5d1SAndy Adamson * Set up the argument/result storage required for the RPC call. 1045d138d5d1SAndy Adamson */ 10466c75dc0dSFred Isaman static void nfs_write_rpcsetup(struct nfs_write_data *data, 1047d138d5d1SAndy Adamson unsigned int count, unsigned int offset, 1048ea2cf228SFred Isaman int how, struct nfs_commit_info *cinfo) 1049d138d5d1SAndy Adamson { 10506c75dc0dSFred Isaman struct nfs_page *req = data->header->req; 1051d138d5d1SAndy Adamson 10521da177e4SLinus Torvalds /* Set up the RPC argument and reply structs 10531da177e4SLinus Torvalds * NB: take care not to mess about with data->commit et al. */ 10541da177e4SLinus Torvalds 10556c75dc0dSFred Isaman data->args.fh = NFS_FH(data->header->inode); 10561da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 10572bea038cSBoaz Harrosh /* pnfs_set_layoutcommit needs this */ 10582bea038cSBoaz Harrosh data->mds_offset = data->args.offset; 10591da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 106030dd374fSFred Isaman data->args.pages = data->pages.pagevec; 10611da177e4SLinus Torvalds data->args.count = count; 1062383ba719STrond Myklebust data->args.context = get_nfs_open_context(req->wb_context); 1063f11ac8dbSTrond Myklebust data->args.lock_context = req->wb_lock_context; 1064bdc7f021STrond Myklebust data->args.stable = NFS_UNSTABLE; 106587ed5eb4STrond Myklebust switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) { 106687ed5eb4STrond Myklebust case 0: 106787ed5eb4STrond Myklebust break; 106887ed5eb4STrond Myklebust case FLUSH_COND_STABLE: 1069ea2cf228SFred Isaman if (nfs_reqs_to_commit(cinfo)) 107087ed5eb4STrond Myklebust break; 107187ed5eb4STrond Myklebust default: 1072bdc7f021STrond Myklebust data->args.stable = NFS_FILE_SYNC; 1073bdc7f021STrond Myklebust } 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds data->res.fattr = &data->fattr; 10761da177e4SLinus Torvalds data->res.count = count; 10771da177e4SLinus Torvalds data->res.verf = &data->verf; 10780e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 10796e4efd56STrond Myklebust } 10801da177e4SLinus Torvalds 10816e4efd56STrond Myklebust static int nfs_do_write(struct nfs_write_data *data, 10826e4efd56STrond Myklebust const struct rpc_call_ops *call_ops, 10836e4efd56STrond Myklebust int how) 10846e4efd56STrond Myklebust { 1085cd841605SFred Isaman struct inode *inode = data->header->inode; 10860382b744SAndy Adamson 10879f0ec176SAndy Adamson return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how, 0); 10881da177e4SLinus Torvalds } 10891da177e4SLinus Torvalds 1090275acaafSTrond Myklebust static int nfs_do_multiple_writes(struct list_head *head, 1091275acaafSTrond Myklebust const struct rpc_call_ops *call_ops, 1092275acaafSTrond Myklebust int how) 1093275acaafSTrond Myklebust { 1094275acaafSTrond Myklebust struct nfs_write_data *data; 1095275acaafSTrond Myklebust int ret = 0; 1096275acaafSTrond Myklebust 1097275acaafSTrond Myklebust while (!list_empty(head)) { 1098275acaafSTrond Myklebust int ret2; 1099275acaafSTrond Myklebust 11006c75dc0dSFred Isaman data = list_first_entry(head, struct nfs_write_data, list); 1101275acaafSTrond Myklebust list_del_init(&data->list); 1102275acaafSTrond Myklebust 1103dce81290STrond Myklebust ret2 = nfs_do_write(data, call_ops, how); 1104275acaafSTrond Myklebust if (ret == 0) 1105275acaafSTrond Myklebust ret = ret2; 1106275acaafSTrond Myklebust } 1107275acaafSTrond Myklebust return ret; 1108275acaafSTrond Myklebust } 1109275acaafSTrond Myklebust 11106d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and 11116d884e8fSFred * call this on each, which will prepare them to be retried on next 11126d884e8fSFred * writeback using standard nfs. 11136d884e8fSFred */ 11146d884e8fSFred static void nfs_redirty_request(struct nfs_page *req) 11156d884e8fSFred { 11166d884e8fSFred nfs_mark_request_dirty(req); 11171d1afcbcSTrond Myklebust nfs_unlock_request(req); 1118d1182b33STrond Myklebust nfs_end_page_writeback(req->wb_page); 11193aff4ebbSTrond Myklebust nfs_release_request(req); 11206d884e8fSFred } 11216d884e8fSFred 1122061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head) 11236c75dc0dSFred Isaman { 11246c75dc0dSFred Isaman struct nfs_page *req; 11256c75dc0dSFred Isaman 11266c75dc0dSFred Isaman while (!list_empty(head)) { 11276c75dc0dSFred Isaman req = nfs_list_entry(head->next); 11286c75dc0dSFred Isaman nfs_list_remove_request(req); 11296c75dc0dSFred Isaman nfs_redirty_request(req); 11306c75dc0dSFred Isaman } 11316c75dc0dSFred Isaman } 11326c75dc0dSFred Isaman 1133061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = { 1134061ae2edSFred Isaman .error_cleanup = nfs_async_write_error, 1135061ae2edSFred Isaman .completion = nfs_write_completion, 1136061ae2edSFred Isaman }; 1137061ae2edSFred Isaman 113825b11dcdSTrond Myklebust static void nfs_flush_error(struct nfs_pageio_descriptor *desc, 113925b11dcdSTrond Myklebust struct nfs_pgio_header *hdr) 114025b11dcdSTrond Myklebust { 114125b11dcdSTrond Myklebust set_bit(NFS_IOHDR_REDO, &hdr->flags); 114225b11dcdSTrond Myklebust while (!list_empty(&hdr->rpc_list)) { 114325b11dcdSTrond Myklebust struct nfs_write_data *data = list_first_entry(&hdr->rpc_list, 114425b11dcdSTrond Myklebust struct nfs_write_data, list); 114525b11dcdSTrond Myklebust list_del(&data->list); 114625b11dcdSTrond Myklebust nfs_writedata_release(data); 114725b11dcdSTrond Myklebust } 114825b11dcdSTrond Myklebust desc->pg_completion_ops->error_cleanup(&desc->pg_list); 114925b11dcdSTrond Myklebust } 115025b11dcdSTrond Myklebust 11511da177e4SLinus Torvalds /* 11521da177e4SLinus Torvalds * Generate multiple small requests to write out a single 11531da177e4SLinus Torvalds * contiguous dirty area on one page. 11541da177e4SLinus Torvalds */ 11556c75dc0dSFred Isaman static int nfs_flush_multi(struct nfs_pageio_descriptor *desc, 11566c75dc0dSFred Isaman struct nfs_pgio_header *hdr) 11571da177e4SLinus Torvalds { 11586c75dc0dSFred Isaman struct nfs_page *req = hdr->req; 11591da177e4SLinus Torvalds struct page *page = req->wb_page; 11601da177e4SLinus Torvalds struct nfs_write_data *data; 1161d097971dSTrond Myklebust size_t wsize = desc->pg_bsize, nbytes; 1162e9f7bee1STrond Myklebust unsigned int offset; 11631da177e4SLinus Torvalds int requests = 0; 1164ea2cf228SFred Isaman struct nfs_commit_info cinfo; 11651da177e4SLinus Torvalds 1166ea2cf228SFred Isaman nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq); 11671da177e4SLinus Torvalds 1168b31268acSTrond Myklebust if ((desc->pg_ioflags & FLUSH_COND_STABLE) && 1169ea2cf228SFred Isaman (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) || 1170b31268acSTrond Myklebust desc->pg_count > wsize)) 1171b31268acSTrond Myklebust desc->pg_ioflags &= ~FLUSH_COND_STABLE; 1172b31268acSTrond Myklebust 1173b31268acSTrond Myklebust 1174275acaafSTrond Myklebust offset = 0; 1175c76069bdSFred Isaman nbytes = desc->pg_count; 1176e9f7bee1STrond Myklebust do { 1177e9f7bee1STrond Myklebust size_t len = min(nbytes, wsize); 1178e9f7bee1STrond Myklebust 11796c75dc0dSFred Isaman data = nfs_writedata_alloc(hdr, 1); 118025b11dcdSTrond Myklebust if (!data) { 118125b11dcdSTrond Myklebust nfs_flush_error(desc, hdr); 118225b11dcdSTrond Myklebust return -ENOMEM; 118325b11dcdSTrond Myklebust } 118430dd374fSFred Isaman data->pages.pagevec[0] = page; 1185ea2cf228SFred Isaman nfs_write_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo); 11866c75dc0dSFred Isaman list_add(&data->list, &hdr->rpc_list); 11871da177e4SLinus Torvalds requests++; 1188e9f7bee1STrond Myklebust nbytes -= len; 1189275acaafSTrond Myklebust offset += len; 1190e9f7bee1STrond Myklebust } while (nbytes != 0); 119125b11dcdSTrond Myklebust nfs_list_remove_request(req); 119225b11dcdSTrond Myklebust nfs_list_add_request(req, &hdr->pages); 11936c75dc0dSFred Isaman desc->pg_rpc_callops = &nfs_write_common_ops; 119425b11dcdSTrond Myklebust return 0; 11951da177e4SLinus Torvalds } 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds /* 11981da177e4SLinus Torvalds * Create an RPC task for the given write request and kick it. 11991da177e4SLinus Torvalds * The page must have been locked by the caller. 12001da177e4SLinus Torvalds * 12011da177e4SLinus Torvalds * It may happen that the page we're passed is not marked dirty. 12021da177e4SLinus Torvalds * This is the case if nfs_updatepage detects a conflicting request 12031da177e4SLinus Torvalds * that has been written but not committed. 12041da177e4SLinus Torvalds */ 12056c75dc0dSFred Isaman static int nfs_flush_one(struct nfs_pageio_descriptor *desc, 12066c75dc0dSFred Isaman struct nfs_pgio_header *hdr) 12071da177e4SLinus Torvalds { 12081da177e4SLinus Torvalds struct nfs_page *req; 12091da177e4SLinus Torvalds struct page **pages; 12101da177e4SLinus Torvalds struct nfs_write_data *data; 1211c76069bdSFred Isaman struct list_head *head = &desc->pg_list; 1212ea2cf228SFred Isaman struct nfs_commit_info cinfo; 12131da177e4SLinus Torvalds 12146c75dc0dSFred Isaman data = nfs_writedata_alloc(hdr, nfs_page_array_len(desc->pg_base, 1215c76069bdSFred Isaman desc->pg_count)); 12166c75dc0dSFred Isaman if (!data) { 121725b11dcdSTrond Myklebust nfs_flush_error(desc, hdr); 121825b11dcdSTrond Myklebust return -ENOMEM; 121944b83799SFred Isaman } 12206c75dc0dSFred Isaman 1221ea2cf228SFred Isaman nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq); 122230dd374fSFred Isaman pages = data->pages.pagevec; 12231da177e4SLinus Torvalds while (!list_empty(head)) { 12241da177e4SLinus Torvalds req = nfs_list_entry(head->next); 12251da177e4SLinus Torvalds nfs_list_remove_request(req); 12266c75dc0dSFred Isaman nfs_list_add_request(req, &hdr->pages); 12271da177e4SLinus Torvalds *pages++ = req->wb_page; 12281da177e4SLinus Torvalds } 12291da177e4SLinus Torvalds 1230b31268acSTrond Myklebust if ((desc->pg_ioflags & FLUSH_COND_STABLE) && 1231ea2cf228SFred Isaman (desc->pg_moreio || nfs_reqs_to_commit(&cinfo))) 1232b31268acSTrond Myklebust desc->pg_ioflags &= ~FLUSH_COND_STABLE; 1233b31268acSTrond Myklebust 12341da177e4SLinus Torvalds /* Set up the argument struct */ 1235ea2cf228SFred Isaman nfs_write_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo); 12366c75dc0dSFred Isaman list_add(&data->list, &hdr->rpc_list); 12376c75dc0dSFred Isaman desc->pg_rpc_callops = &nfs_write_common_ops; 123825b11dcdSTrond Myklebust return 0; 12391da177e4SLinus Torvalds } 12401da177e4SLinus Torvalds 12416c75dc0dSFred Isaman int nfs_generic_flush(struct nfs_pageio_descriptor *desc, 12426c75dc0dSFred Isaman struct nfs_pgio_header *hdr) 1243dce81290STrond Myklebust { 1244dce81290STrond Myklebust if (desc->pg_bsize < PAGE_CACHE_SIZE) 12456c75dc0dSFred Isaman return nfs_flush_multi(desc, hdr); 12466c75dc0dSFred Isaman return nfs_flush_one(desc, hdr); 1247dce81290STrond Myklebust } 124889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_generic_flush); 1249dce81290STrond Myklebust 1250dce81290STrond Myklebust static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc) 12511751c363STrond Myklebust { 12526c75dc0dSFred Isaman struct nfs_write_header *whdr; 12536c75dc0dSFred Isaman struct nfs_pgio_header *hdr; 1254275acaafSTrond Myklebust int ret; 1255275acaafSTrond Myklebust 12566c75dc0dSFred Isaman whdr = nfs_writehdr_alloc(); 12576c75dc0dSFred Isaman if (!whdr) { 12589b5415b5STrond Myklebust desc->pg_completion_ops->error_cleanup(&desc->pg_list); 12596c75dc0dSFred Isaman return -ENOMEM; 12606c75dc0dSFred Isaman } 12616c75dc0dSFred Isaman hdr = &whdr->header; 12626c75dc0dSFred Isaman nfs_pgheader_init(desc, hdr, nfs_writehdr_free); 12636c75dc0dSFred Isaman atomic_inc(&hdr->refcnt); 12646c75dc0dSFred Isaman ret = nfs_generic_flush(desc, hdr); 1265275acaafSTrond Myklebust if (ret == 0) 12666c75dc0dSFred Isaman ret = nfs_do_multiple_writes(&hdr->rpc_list, 12676c75dc0dSFred Isaman desc->pg_rpc_callops, 1268dce81290STrond Myklebust desc->pg_ioflags); 12696c75dc0dSFred Isaman if (atomic_dec_and_test(&hdr->refcnt)) 1270061ae2edSFred Isaman hdr->completion_ops->completion(hdr); 1271275acaafSTrond Myklebust return ret; 12721751c363STrond Myklebust } 12731751c363STrond Myklebust 12741751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_write_ops = { 12751751c363STrond Myklebust .pg_test = nfs_generic_pg_test, 12761751c363STrond Myklebust .pg_doio = nfs_generic_pg_writepages, 12771751c363STrond Myklebust }; 12781751c363STrond Myklebust 127957208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, 1280061ae2edSFred Isaman struct inode *inode, int ioflags, 1281061ae2edSFred Isaman const struct nfs_pgio_completion_ops *compl_ops) 12821751c363STrond Myklebust { 1283061ae2edSFred Isaman nfs_pageio_init(pgio, inode, &nfs_pageio_write_ops, compl_ops, 12841751c363STrond Myklebust NFS_SERVER(inode)->wsize, ioflags); 12851751c363STrond Myklebust } 1286ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write); 12871751c363STrond Myklebust 1288dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio) 1289dce81290STrond Myklebust { 1290dce81290STrond Myklebust pgio->pg_ops = &nfs_pageio_write_ops; 1291dce81290STrond Myklebust pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize; 1292dce81290STrond Myklebust } 12931f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds); 1294dce81290STrond Myklebust 12951da177e4SLinus Torvalds 1296def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata) 1297def6ed7eSAndy Adamson { 1298def6ed7eSAndy Adamson struct nfs_write_data *data = calldata; 1299ef1820f9SNeilBrown int err; 1300ef1820f9SNeilBrown err = NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data); 1301ef1820f9SNeilBrown if (err) 1302ef1820f9SNeilBrown rpc_exit(task, err); 1303def6ed7eSAndy Adamson } 1304def6ed7eSAndy Adamson 13050b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata) 13060b7c0153SFred Isaman { 13070b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 13080b7c0153SFred Isaman 13090b7c0153SFred Isaman NFS_PROTO(data->inode)->commit_rpc_prepare(task, data); 13100b7c0153SFred Isaman } 13110b7c0153SFred Isaman 13121da177e4SLinus Torvalds /* 13131da177e4SLinus Torvalds * Handle a write reply that flushes a whole page. 13141da177e4SLinus Torvalds * 13151da177e4SLinus Torvalds * FIXME: There is an inherent race with invalidate_inode_pages and 13161da177e4SLinus Torvalds * writebacks since the page->count is kept > 1 for as long 13171da177e4SLinus Torvalds * as the page has a write request pending. 13181da177e4SLinus Torvalds */ 13196c75dc0dSFred Isaman static void nfs_writeback_done_common(struct rpc_task *task, void *calldata) 13201da177e4SLinus Torvalds { 1321788e7a89STrond Myklebust struct nfs_write_data *data = calldata; 13221da177e4SLinus Torvalds 1323c9d8f89dSTrond Myklebust nfs_writeback_done(task, data); 1324c9d8f89dSTrond Myklebust } 1325c9d8f89dSTrond Myklebust 13266c75dc0dSFred Isaman static void nfs_writeback_release_common(void *calldata) 1327c9d8f89dSTrond Myklebust { 1328c9d8f89dSTrond Myklebust struct nfs_write_data *data = calldata; 1329cd841605SFred Isaman struct nfs_pgio_header *hdr = data->header; 1330e2fecb21STrond Myklebust int status = data->task.tk_status; 1331788e7a89STrond Myklebust 13326c75dc0dSFred Isaman if ((status >= 0) && nfs_write_need_commit(data)) { 13336c75dc0dSFred Isaman spin_lock(&hdr->lock); 13346c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) 13356c75dc0dSFred Isaman ; /* Do nothing */ 13366c75dc0dSFred Isaman else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) 13379bce008bSTrond Myklebust memcpy(hdr->verf, &data->verf, sizeof(*hdr->verf)); 13389bce008bSTrond Myklebust else if (memcmp(hdr->verf, &data->verf, sizeof(*hdr->verf))) 13396c75dc0dSFred Isaman set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags); 13406c75dc0dSFred Isaman spin_unlock(&hdr->lock); 13411da177e4SLinus Torvalds } 1342cd841605SFred Isaman nfs_writedata_release(data); 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds 13456c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops = { 1346def6ed7eSAndy Adamson .rpc_call_prepare = nfs_write_prepare, 13476c75dc0dSFred Isaman .rpc_call_done = nfs_writeback_done_common, 13486c75dc0dSFred Isaman .rpc_release = nfs_writeback_release_common, 1349788e7a89STrond Myklebust }; 1350788e7a89STrond Myklebust 1351788e7a89STrond Myklebust 13521da177e4SLinus Torvalds /* 13531da177e4SLinus Torvalds * This function is called when the WRITE call is complete. 13541da177e4SLinus Torvalds */ 135513602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data) 13561da177e4SLinus Torvalds { 13571da177e4SLinus Torvalds struct nfs_writeargs *argp = &data->args; 13581da177e4SLinus Torvalds struct nfs_writeres *resp = &data->res; 1359cd841605SFred Isaman struct inode *inode = data->header->inode; 1360788e7a89STrond Myklebust int status; 13611da177e4SLinus Torvalds 1362a3f565b1SChuck Lever dprintk("NFS: %5u nfs_writeback_done (status %d)\n", 13631da177e4SLinus Torvalds task->tk_pid, task->tk_status); 13641da177e4SLinus Torvalds 1365f551e44fSChuck Lever /* 1366f551e44fSChuck Lever * ->write_done will attempt to use post-op attributes to detect 1367f551e44fSChuck Lever * conflicting writes by other clients. A strict interpretation 1368f551e44fSChuck Lever * of close-to-open would allow us to continue caching even if 1369f551e44fSChuck Lever * another writer had changed the file, but some applications 1370f551e44fSChuck Lever * depend on tighter cache coherency when writing. 1371f551e44fSChuck Lever */ 1372cd841605SFred Isaman status = NFS_PROTO(inode)->write_done(task, data); 1373788e7a89STrond Myklebust if (status != 0) 137413602896SFred Isaman return; 1375cd841605SFred Isaman nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count); 137691d5b470SChuck Lever 137789d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4) 13781da177e4SLinus Torvalds if (resp->verf->committed < argp->stable && task->tk_status >= 0) { 13791da177e4SLinus Torvalds /* We tried a write call, but the server did not 13801da177e4SLinus Torvalds * commit data to stable storage even though we 13811da177e4SLinus Torvalds * requested it. 13821da177e4SLinus Torvalds * Note: There is a known bug in Tru64 < 5.0 in which 13831da177e4SLinus Torvalds * the server reports NFS_DATA_SYNC, but performs 13841da177e4SLinus Torvalds * NFS_FILE_SYNC. We therefore implement this checking 13851da177e4SLinus Torvalds * as a dprintk() in order to avoid filling syslog. 13861da177e4SLinus Torvalds */ 13871da177e4SLinus Torvalds static unsigned long complain; 13881da177e4SLinus Torvalds 1389a69aef14SFred Isaman /* Note this will print the MDS for a DS write */ 13901da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 13911da177e4SLinus Torvalds dprintk("NFS: faulty NFS server %s:" 13921da177e4SLinus Torvalds " (committed = %d) != (stable = %d)\n", 1393cd841605SFred Isaman NFS_SERVER(inode)->nfs_client->cl_hostname, 13941da177e4SLinus Torvalds resp->verf->committed, argp->stable); 13951da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 13961da177e4SLinus Torvalds } 13971da177e4SLinus Torvalds } 13981da177e4SLinus Torvalds #endif 13996c75dc0dSFred Isaman if (task->tk_status < 0) 14006c75dc0dSFred Isaman nfs_set_pgio_error(data->header, task->tk_status, argp->offset); 14016c75dc0dSFred Isaman else if (resp->count < argp->count) { 14021da177e4SLinus Torvalds static unsigned long complain; 14031da177e4SLinus Torvalds 14046c75dc0dSFred Isaman /* This a short write! */ 1405cd841605SFred Isaman nfs_inc_stats(inode, NFSIOS_SHORTWRITE); 140691d5b470SChuck Lever 14071da177e4SLinus Torvalds /* Has the server at least made some progress? */ 14086c75dc0dSFred Isaman if (resp->count == 0) { 14096c75dc0dSFred Isaman if (time_before(complain, jiffies)) { 14106c75dc0dSFred Isaman printk(KERN_WARNING 14116c75dc0dSFred Isaman "NFS: Server wrote zero bytes, expected %u.\n", 14126c75dc0dSFred Isaman argp->count); 14136c75dc0dSFred Isaman complain = jiffies + 300 * HZ; 14146c75dc0dSFred Isaman } 14156c75dc0dSFred Isaman nfs_set_pgio_error(data->header, -EIO, argp->offset); 14166c75dc0dSFred Isaman task->tk_status = -EIO; 14176c75dc0dSFred Isaman return; 14186c75dc0dSFred Isaman } 14191da177e4SLinus Torvalds /* Was this an NFSv2 write or an NFSv3 stable write? */ 14201da177e4SLinus Torvalds if (resp->verf->committed != NFS_UNSTABLE) { 14211da177e4SLinus Torvalds /* Resend from where the server left off */ 1422a69aef14SFred Isaman data->mds_offset += resp->count; 14231da177e4SLinus Torvalds argp->offset += resp->count; 14241da177e4SLinus Torvalds argp->pgbase += resp->count; 14251da177e4SLinus Torvalds argp->count -= resp->count; 14261da177e4SLinus Torvalds } else { 14271da177e4SLinus Torvalds /* Resend as a stable write in order to avoid 14281da177e4SLinus Torvalds * headaches in the case of a server crash. 14291da177e4SLinus Torvalds */ 14301da177e4SLinus Torvalds argp->stable = NFS_FILE_SYNC; 14311da177e4SLinus Torvalds } 1432d00c5d43STrond Myklebust rpc_restart_call_prepare(task); 14331da177e4SLinus Torvalds } 14341da177e4SLinus Torvalds } 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds 143789d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4) 143871d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait) 143971d0a611STrond Myklebust { 1440b8413f98STrond Myklebust int ret; 1441b8413f98STrond Myklebust 144271d0a611STrond Myklebust if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags)) 144371d0a611STrond Myklebust return 1; 1444b8413f98STrond Myklebust if (!may_wait) 144571d0a611STrond Myklebust return 0; 1446b8413f98STrond Myklebust ret = out_of_line_wait_on_bit_lock(&nfsi->flags, 1447b8413f98STrond Myklebust NFS_INO_COMMIT, 1448b8413f98STrond Myklebust nfs_wait_bit_killable, 1449b8413f98STrond Myklebust TASK_KILLABLE); 1450b8413f98STrond Myklebust return (ret < 0) ? ret : 1; 145171d0a611STrond Myklebust } 145271d0a611STrond Myklebust 1453f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi) 145471d0a611STrond Myklebust { 145571d0a611STrond Myklebust clear_bit(NFS_INO_COMMIT, &nfsi->flags); 145671d0a611STrond Myklebust smp_mb__after_clear_bit(); 145771d0a611STrond Myklebust wake_up_bit(&nfsi->flags, NFS_INO_COMMIT); 145871d0a611STrond Myklebust } 145971d0a611STrond Myklebust 14600b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data) 14611da177e4SLinus Torvalds { 14620b7c0153SFred Isaman put_nfs_open_context(data->context); 14630b7c0153SFred Isaman nfs_commit_free(data); 14641da177e4SLinus Torvalds } 1465e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release); 14661da177e4SLinus Torvalds 14670b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data, 14689ace33cdSFred Isaman const struct rpc_call_ops *call_ops, 14699f0ec176SAndy Adamson int how, int flags) 14701da177e4SLinus Torvalds { 147107737691STrond Myklebust struct rpc_task *task; 14729ace33cdSFred Isaman int priority = flush_task_priority(how); 1473bdc7f021STrond Myklebust struct rpc_message msg = { 1474bdc7f021STrond Myklebust .rpc_argp = &data->args, 1475bdc7f021STrond Myklebust .rpc_resp = &data->res, 14769ace33cdSFred Isaman .rpc_cred = data->cred, 1477bdc7f021STrond Myklebust }; 147884115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 147907737691STrond Myklebust .task = &data->task, 14809ace33cdSFred Isaman .rpc_client = clnt, 1481bdc7f021STrond Myklebust .rpc_message = &msg, 14829ace33cdSFred Isaman .callback_ops = call_ops, 148384115e1cSTrond Myklebust .callback_data = data, 1484101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 14859f0ec176SAndy Adamson .flags = RPC_TASK_ASYNC | flags, 14863ff7576dSTrond Myklebust .priority = priority, 148784115e1cSTrond Myklebust }; 1488788e7a89STrond Myklebust /* Set up the initial task struct. */ 14899ace33cdSFred Isaman NFS_PROTO(data->inode)->commit_setup(data, &msg); 14901da177e4SLinus Torvalds 1491a3f565b1SChuck Lever dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); 1492bdc7f021STrond Myklebust 14938c21c62cSWeston Andros Adamson nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client, 14948c21c62cSWeston Andros Adamson NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg); 14958c21c62cSWeston Andros Adamson 149607737691STrond Myklebust task = rpc_run_task(&task_setup_data); 1497dbae4c73STrond Myklebust if (IS_ERR(task)) 1498dbae4c73STrond Myklebust return PTR_ERR(task); 1499d2224e7aSJeff Layton if (how & FLUSH_SYNC) 1500d2224e7aSJeff Layton rpc_wait_for_completion_task(task); 150107737691STrond Myklebust rpc_put_task(task); 1502dbae4c73STrond Myklebust return 0; 15031da177e4SLinus Torvalds } 1504e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit); 15051da177e4SLinus Torvalds 15061da177e4SLinus Torvalds /* 15079ace33cdSFred Isaman * Set up the argument/result storage required for the RPC call. 15089ace33cdSFred Isaman */ 15090b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data, 1510988b6dceSFred Isaman struct list_head *head, 1511f453a54aSFred Isaman struct pnfs_layout_segment *lseg, 1512f453a54aSFred Isaman struct nfs_commit_info *cinfo) 15139ace33cdSFred Isaman { 15149ace33cdSFred Isaman struct nfs_page *first = nfs_list_entry(head->next); 15153d4ff43dSAl Viro struct inode *inode = first->wb_context->dentry->d_inode; 15169ace33cdSFred Isaman 15179ace33cdSFred Isaman /* Set up the RPC argument and reply structs 15189ace33cdSFred Isaman * NB: take care not to mess about with data->commit et al. */ 15199ace33cdSFred Isaman 15209ace33cdSFred Isaman list_splice_init(head, &data->pages); 15219ace33cdSFred Isaman 15229ace33cdSFred Isaman data->inode = inode; 15239ace33cdSFred Isaman data->cred = first->wb_context->cred; 1524988b6dceSFred Isaman data->lseg = lseg; /* reference transferred */ 15259ace33cdSFred Isaman data->mds_ops = &nfs_commit_ops; 1526f453a54aSFred Isaman data->completion_ops = cinfo->completion_ops; 1527b359f9d0SFred Isaman data->dreq = cinfo->dreq; 15289ace33cdSFred Isaman 15299ace33cdSFred Isaman data->args.fh = NFS_FH(data->inode); 15309ace33cdSFred Isaman /* Note: we always request a commit of the entire inode */ 15319ace33cdSFred Isaman data->args.offset = 0; 15329ace33cdSFred Isaman data->args.count = 0; 15330b7c0153SFred Isaman data->context = get_nfs_open_context(first->wb_context); 15349ace33cdSFred Isaman data->res.fattr = &data->fattr; 15359ace33cdSFred Isaman data->res.verf = &data->verf; 15369ace33cdSFred Isaman nfs_fattr_init(&data->fattr); 15379ace33cdSFred Isaman } 1538e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit); 15399ace33cdSFred Isaman 1540e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list, 1541ea2cf228SFred Isaman struct pnfs_layout_segment *lseg, 1542ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 154364bfeb49SFred Isaman { 154464bfeb49SFred Isaman struct nfs_page *req; 154564bfeb49SFred Isaman 154664bfeb49SFred Isaman while (!list_empty(page_list)) { 154764bfeb49SFred Isaman req = nfs_list_entry(page_list->next); 154864bfeb49SFred Isaman nfs_list_remove_request(req); 1549ea2cf228SFred Isaman nfs_mark_request_commit(req, lseg, cinfo); 155056f9cd68SFred Isaman if (!cinfo->dreq) { 155164bfeb49SFred Isaman dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 1552d56b4ddfSMel Gorman dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info, 155364bfeb49SFred Isaman BDI_RECLAIMABLE); 155456f9cd68SFred Isaman } 15551d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 155664bfeb49SFred Isaman } 155764bfeb49SFred Isaman } 1558e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit); 155964bfeb49SFred Isaman 15609ace33cdSFred Isaman /* 15611da177e4SLinus Torvalds * Commit dirty pages 15621da177e4SLinus Torvalds */ 15631da177e4SLinus Torvalds static int 1564ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how, 1565ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 15661da177e4SLinus Torvalds { 15670b7c0153SFred Isaman struct nfs_commit_data *data; 15681da177e4SLinus Torvalds 1569c9d8f89dSTrond Myklebust data = nfs_commitdata_alloc(); 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds if (!data) 15721da177e4SLinus Torvalds goto out_bad; 15731da177e4SLinus Torvalds 15741da177e4SLinus Torvalds /* Set up the argument struct */ 1575f453a54aSFred Isaman nfs_init_commit(data, head, NULL, cinfo); 1576f453a54aSFred Isaman atomic_inc(&cinfo->mds->rpcs_out); 15779f0ec176SAndy Adamson return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops, 15789f0ec176SAndy Adamson how, 0); 15791da177e4SLinus Torvalds out_bad: 1580ea2cf228SFred Isaman nfs_retry_commit(head, NULL, cinfo); 1581f453a54aSFred Isaman cinfo->completion_ops->error_cleanup(NFS_I(inode)); 15821da177e4SLinus Torvalds return -ENOMEM; 15831da177e4SLinus Torvalds } 15841da177e4SLinus Torvalds 15851da177e4SLinus Torvalds /* 15861da177e4SLinus Torvalds * COMMIT call returned 15871da177e4SLinus Torvalds */ 1588788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata) 15891da177e4SLinus Torvalds { 15900b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 15911da177e4SLinus Torvalds 1592a3f565b1SChuck Lever dprintk("NFS: %5u nfs_commit_done (status %d)\n", 15931da177e4SLinus Torvalds task->tk_pid, task->tk_status); 15941da177e4SLinus Torvalds 1595788e7a89STrond Myklebust /* Call the NFS version-specific code */ 1596c0d0e96bSTrond Myklebust NFS_PROTO(data->inode)->commit_done(task, data); 1597c9d8f89dSTrond Myklebust } 1598c9d8f89dSTrond Myklebust 1599f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data) 1600c9d8f89dSTrond Myklebust { 1601c9d8f89dSTrond Myklebust struct nfs_page *req; 1602c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1603f453a54aSFred Isaman struct nfs_commit_info cinfo; 1604788e7a89STrond Myklebust 16051da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 16061da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 16071da177e4SLinus Torvalds nfs_list_remove_request(req); 1608d6d6dc7cSFred Isaman nfs_clear_page_commit(req->wb_page); 16091da177e4SLinus Torvalds 16101e8968c5SNiels de Vos dprintk("NFS: commit (%s/%llu %d@%lld)", 16113d4ff43dSAl Viro req->wb_context->dentry->d_sb->s_id, 16121e8968c5SNiels de Vos (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode), 16131da177e4SLinus Torvalds req->wb_bytes, 16141da177e4SLinus Torvalds (long long)req_offset(req)); 1615c9d8f89dSTrond Myklebust if (status < 0) { 1616c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 16171da177e4SLinus Torvalds nfs_inode_remove_request(req); 1618c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 16191da177e4SLinus Torvalds goto next; 16201da177e4SLinus Torvalds } 16211da177e4SLinus Torvalds 16221da177e4SLinus Torvalds /* Okay, COMMIT succeeded, apparently. Check the verifier 16231da177e4SLinus Torvalds * returned by the server against all stored verfs. */ 16242f2c63bcSTrond Myklebust if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) { 16251da177e4SLinus Torvalds /* We have a match */ 16261da177e4SLinus Torvalds nfs_inode_remove_request(req); 16271da177e4SLinus Torvalds dprintk(" OK\n"); 16281da177e4SLinus Torvalds goto next; 16291da177e4SLinus Torvalds } 16301da177e4SLinus Torvalds /* We have a mismatch. Write the page again */ 16311da177e4SLinus Torvalds dprintk(" mismatch\n"); 16326d884e8fSFred nfs_mark_request_dirty(req); 163305990d1bSTrond Myklebust set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags); 16341da177e4SLinus Torvalds next: 16351d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 16361da177e4SLinus Torvalds } 1637f453a54aSFred Isaman nfs_init_cinfo(&cinfo, data->inode, data->dreq); 1638f453a54aSFred Isaman if (atomic_dec_and_test(&cinfo.mds->rpcs_out)) 1639f453a54aSFred Isaman nfs_commit_clear_lock(NFS_I(data->inode)); 16405917ce84SFred Isaman } 16415917ce84SFred Isaman 16425917ce84SFred Isaman static void nfs_commit_release(void *calldata) 16435917ce84SFred Isaman { 16440b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 16455917ce84SFred Isaman 1646f453a54aSFred Isaman data->completion_ops->completion(data); 1647c9d8f89dSTrond Myklebust nfs_commitdata_release(calldata); 16481da177e4SLinus Torvalds } 1649788e7a89STrond Myklebust 1650788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = { 16510b7c0153SFred Isaman .rpc_call_prepare = nfs_commit_prepare, 1652788e7a89STrond Myklebust .rpc_call_done = nfs_commit_done, 1653788e7a89STrond Myklebust .rpc_release = nfs_commit_release, 1654788e7a89STrond Myklebust }; 16551da177e4SLinus Torvalds 1656f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = { 1657f453a54aSFred Isaman .completion = nfs_commit_release_pages, 1658f453a54aSFred Isaman .error_cleanup = nfs_commit_clear_lock, 1659f453a54aSFred Isaman }; 1660f453a54aSFred Isaman 16611763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head, 1662ea2cf228SFred Isaman int how, struct nfs_commit_info *cinfo) 166384c53ab5SFred Isaman { 166484c53ab5SFred Isaman int status; 166584c53ab5SFred Isaman 1666ea2cf228SFred Isaman status = pnfs_commit_list(inode, head, how, cinfo); 166784c53ab5SFred Isaman if (status == PNFS_NOT_ATTEMPTED) 1668ea2cf228SFred Isaman status = nfs_commit_list(inode, head, how, cinfo); 166984c53ab5SFred Isaman return status; 167084c53ab5SFred Isaman } 167184c53ab5SFred Isaman 1672b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how) 16731da177e4SLinus Torvalds { 16741da177e4SLinus Torvalds LIST_HEAD(head); 1675ea2cf228SFred Isaman struct nfs_commit_info cinfo; 167671d0a611STrond Myklebust int may_wait = how & FLUSH_SYNC; 1677b8413f98STrond Myklebust int res; 16781da177e4SLinus Torvalds 1679b8413f98STrond Myklebust res = nfs_commit_set_lock(NFS_I(inode), may_wait); 1680b8413f98STrond Myklebust if (res <= 0) 1681c5efa5fcSTrond Myklebust goto out_mark_dirty; 1682ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, inode); 1683ea2cf228SFred Isaman res = nfs_scan_commit(inode, &head, &cinfo); 16841da177e4SLinus Torvalds if (res) { 1685a861a1e1SFred Isaman int error; 1686a861a1e1SFred Isaman 1687ea2cf228SFred Isaman error = nfs_generic_commit_list(inode, &head, how, &cinfo); 16881da177e4SLinus Torvalds if (error < 0) 16891da177e4SLinus Torvalds return error; 1690b8413f98STrond Myklebust if (!may_wait) 1691b8413f98STrond Myklebust goto out_mark_dirty; 1692b8413f98STrond Myklebust error = wait_on_bit(&NFS_I(inode)->flags, 1693b8413f98STrond Myklebust NFS_INO_COMMIT, 169471d0a611STrond Myklebust nfs_wait_bit_killable, 169571d0a611STrond Myklebust TASK_KILLABLE); 1696b8413f98STrond Myklebust if (error < 0) 1697b8413f98STrond Myklebust return error; 169871d0a611STrond Myklebust } else 169971d0a611STrond Myklebust nfs_commit_clear_lock(NFS_I(inode)); 1700c5efa5fcSTrond Myklebust return res; 1701c5efa5fcSTrond Myklebust /* Note: If we exit without ensuring that the commit is complete, 1702c5efa5fcSTrond Myklebust * we must mark the inode as dirty. Otherwise, future calls to 1703c5efa5fcSTrond Myklebust * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure 1704c5efa5fcSTrond Myklebust * that the data is on the disk. 1705c5efa5fcSTrond Myklebust */ 1706c5efa5fcSTrond Myklebust out_mark_dirty: 1707c5efa5fcSTrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 17081da177e4SLinus Torvalds return res; 17091da177e4SLinus Torvalds } 17108fc795f7STrond Myklebust 17118fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 17128fc795f7STrond Myklebust { 1713420e3646STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 1714420e3646STrond Myklebust int flags = FLUSH_SYNC; 1715420e3646STrond Myklebust int ret = 0; 17168fc795f7STrond Myklebust 17173236c3e1SJeff Layton /* no commits means nothing needs to be done */ 1718ea2cf228SFred Isaman if (!nfsi->commit_info.ncommit) 17193236c3e1SJeff Layton return ret; 17203236c3e1SJeff Layton 1721a00dd6c0SJeff Layton if (wbc->sync_mode == WB_SYNC_NONE) { 1722a00dd6c0SJeff Layton /* Don't commit yet if this is a non-blocking flush and there 1723a00dd6c0SJeff Layton * are a lot of outstanding writes for this mapping. 1724420e3646STrond Myklebust */ 1725ea2cf228SFred Isaman if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1)) 1726420e3646STrond Myklebust goto out_mark_dirty; 1727420e3646STrond Myklebust 1728a00dd6c0SJeff Layton /* don't wait for the COMMIT response */ 1729420e3646STrond Myklebust flags = 0; 1730a00dd6c0SJeff Layton } 1731a00dd6c0SJeff Layton 1732420e3646STrond Myklebust ret = nfs_commit_inode(inode, flags); 1733420e3646STrond Myklebust if (ret >= 0) { 1734420e3646STrond Myklebust if (wbc->sync_mode == WB_SYNC_NONE) { 1735420e3646STrond Myklebust if (ret < wbc->nr_to_write) 1736420e3646STrond Myklebust wbc->nr_to_write -= ret; 1737420e3646STrond Myklebust else 1738420e3646STrond Myklebust wbc->nr_to_write = 0; 1739420e3646STrond Myklebust } 17408fc795f7STrond Myklebust return 0; 1741420e3646STrond Myklebust } 1742420e3646STrond Myklebust out_mark_dirty: 17438fc795f7STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 17448fc795f7STrond Myklebust return ret; 17458fc795f7STrond Myklebust } 1746c63c7b05STrond Myklebust #else 17478fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 17488fc795f7STrond Myklebust { 17498fc795f7STrond Myklebust return 0; 17508fc795f7STrond Myklebust } 17511da177e4SLinus Torvalds #endif 17521da177e4SLinus Torvalds 17538fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc) 17548fc795f7STrond Myklebust { 1755a8d8f02cSBryan Schumaker return nfs_commit_unstable_pages(inode, wbc); 1756a8d8f02cSBryan Schumaker } 175789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode); 1758863a3c6cSAndy Adamson 1759acdc53b2STrond Myklebust /* 1760acdc53b2STrond Myklebust * flush the inode to disk. 1761acdc53b2STrond Myklebust */ 1762acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode) 176334901f70STrond Myklebust { 176434901f70STrond Myklebust struct writeback_control wbc = { 176572cb77f4STrond Myklebust .sync_mode = WB_SYNC_ALL, 176634901f70STrond Myklebust .nr_to_write = LONG_MAX, 1767d7fb1207STrond Myklebust .range_start = 0, 1768d7fb1207STrond Myklebust .range_end = LLONG_MAX, 176934901f70STrond Myklebust }; 1770f4ce1299STrond Myklebust int ret; 177134901f70STrond Myklebust 1772f4ce1299STrond Myklebust trace_nfs_writeback_inode_enter(inode); 1773f4ce1299STrond Myklebust 1774f4ce1299STrond Myklebust ret = sync_inode(inode, &wbc); 1775f4ce1299STrond Myklebust 1776f4ce1299STrond Myklebust trace_nfs_writeback_inode_exit(inode, ret); 1777f4ce1299STrond Myklebust return ret; 17781c75950bSTrond Myklebust } 1779ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all); 17801c75950bSTrond Myklebust 17811b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page) 17821b3b4a1aSTrond Myklebust { 17831b3b4a1aSTrond Myklebust struct nfs_page *req; 17841b3b4a1aSTrond Myklebust int ret = 0; 17851b3b4a1aSTrond Myklebust 17861b3b4a1aSTrond Myklebust for (;;) { 1787ba8b06e6STrond Myklebust wait_on_page_writeback(page); 17881b3b4a1aSTrond Myklebust req = nfs_page_find_request(page); 17891b3b4a1aSTrond Myklebust if (req == NULL) 17901b3b4a1aSTrond Myklebust break; 17917ad84aa9STrond Myklebust if (nfs_lock_request(req)) { 17928dd37758STrond Myklebust nfs_clear_request_commit(req); 17931b3b4a1aSTrond Myklebust nfs_inode_remove_request(req); 17941b3b4a1aSTrond Myklebust /* 17951b3b4a1aSTrond Myklebust * In case nfs_inode_remove_request has marked the 17961b3b4a1aSTrond Myklebust * page as being dirty 17971b3b4a1aSTrond Myklebust */ 17981b3b4a1aSTrond Myklebust cancel_dirty_page(page, PAGE_CACHE_SIZE); 17991d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 18001b3b4a1aSTrond Myklebust break; 18011b3b4a1aSTrond Myklebust } 18021b3b4a1aSTrond Myklebust ret = nfs_wait_on_request(req); 1803c9edda71STrond Myklebust nfs_release_request(req); 18041b3b4a1aSTrond Myklebust if (ret < 0) 1805c988950eSTrond Myklebust break; 18061b3b4a1aSTrond Myklebust } 18071b3b4a1aSTrond Myklebust return ret; 18081b3b4a1aSTrond Myklebust } 18091b3b4a1aSTrond Myklebust 18101c75950bSTrond Myklebust /* 18111c75950bSTrond Myklebust * Write back all requests on one page - we do this before reading it. 18121c75950bSTrond Myklebust */ 18131c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page) 18141c75950bSTrond Myklebust { 181529418aa4SMel Gorman loff_t range_start = page_file_offset(page); 18167f2f12d9STrond Myklebust loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); 18177f2f12d9STrond Myklebust struct writeback_control wbc = { 18187f2f12d9STrond Myklebust .sync_mode = WB_SYNC_ALL, 18197f2f12d9STrond Myklebust .nr_to_write = 0, 18207f2f12d9STrond Myklebust .range_start = range_start, 18217f2f12d9STrond Myklebust .range_end = range_end, 18227f2f12d9STrond Myklebust }; 18237f2f12d9STrond Myklebust int ret; 18247f2f12d9STrond Myklebust 1825f4ce1299STrond Myklebust trace_nfs_writeback_page_enter(inode); 1826f4ce1299STrond Myklebust 18270522f6adSTrond Myklebust for (;;) { 1828ba8b06e6STrond Myklebust wait_on_page_writeback(page); 18297f2f12d9STrond Myklebust if (clear_page_dirty_for_io(page)) { 18307f2f12d9STrond Myklebust ret = nfs_writepage_locked(page, &wbc); 18317f2f12d9STrond Myklebust if (ret < 0) 18327f2f12d9STrond Myklebust goto out_error; 18330522f6adSTrond Myklebust continue; 18347f2f12d9STrond Myklebust } 1835f4ce1299STrond Myklebust ret = 0; 18360522f6adSTrond Myklebust if (!PagePrivate(page)) 18370522f6adSTrond Myklebust break; 18380522f6adSTrond Myklebust ret = nfs_commit_inode(inode, FLUSH_SYNC); 18397f2f12d9STrond Myklebust if (ret < 0) 18407f2f12d9STrond Myklebust goto out_error; 18417f2f12d9STrond Myklebust } 18427f2f12d9STrond Myklebust out_error: 1843f4ce1299STrond Myklebust trace_nfs_writeback_page_exit(inode, ret); 18447f2f12d9STrond Myklebust return ret; 18451c75950bSTrond Myklebust } 18461c75950bSTrond Myklebust 1847074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION 1848074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage, 1849a6bc32b8SMel Gorman struct page *page, enum migrate_mode mode) 1850074cc1deSTrond Myklebust { 18512da95652SJeff Layton /* 18522da95652SJeff Layton * If PagePrivate is set, then the page is currently associated with 18532da95652SJeff Layton * an in-progress read or write request. Don't try to migrate it. 18542da95652SJeff Layton * 18552da95652SJeff Layton * FIXME: we could do this in principle, but we'll need a way to ensure 18562da95652SJeff Layton * that we can safely release the inode reference while holding 18572da95652SJeff Layton * the page lock. 18582da95652SJeff Layton */ 18592da95652SJeff Layton if (PagePrivate(page)) 18602da95652SJeff Layton return -EBUSY; 1861074cc1deSTrond Myklebust 18628c209ce7SDavid Howells if (!nfs_fscache_release_page(page, GFP_KERNEL)) 18638c209ce7SDavid Howells return -EBUSY; 1864074cc1deSTrond Myklebust 1865a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 1866074cc1deSTrond Myklebust } 1867074cc1deSTrond Myklebust #endif 1868074cc1deSTrond Myklebust 1869f7b422b1SDavid Howells int __init nfs_init_writepagecache(void) 18701da177e4SLinus Torvalds { 18711da177e4SLinus Torvalds nfs_wdata_cachep = kmem_cache_create("nfs_write_data", 1872cd841605SFred Isaman sizeof(struct nfs_write_header), 18731da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 187420c2df83SPaul Mundt NULL); 18751da177e4SLinus Torvalds if (nfs_wdata_cachep == NULL) 18761da177e4SLinus Torvalds return -ENOMEM; 18771da177e4SLinus Torvalds 187893d2341cSMatthew Dobson nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, 18791da177e4SLinus Torvalds nfs_wdata_cachep); 18801da177e4SLinus Torvalds if (nfs_wdata_mempool == NULL) 18813dd4765fSJeff Layton goto out_destroy_write_cache; 18821da177e4SLinus Torvalds 18830b7c0153SFred Isaman nfs_cdata_cachep = kmem_cache_create("nfs_commit_data", 18840b7c0153SFred Isaman sizeof(struct nfs_commit_data), 18850b7c0153SFred Isaman 0, SLAB_HWCACHE_ALIGN, 18860b7c0153SFred Isaman NULL); 18870b7c0153SFred Isaman if (nfs_cdata_cachep == NULL) 18883dd4765fSJeff Layton goto out_destroy_write_mempool; 18890b7c0153SFred Isaman 189093d2341cSMatthew Dobson nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, 18914c100210SYanchuan Nian nfs_cdata_cachep); 18921da177e4SLinus Torvalds if (nfs_commit_mempool == NULL) 18933dd4765fSJeff Layton goto out_destroy_commit_cache; 18941da177e4SLinus Torvalds 189589a09141SPeter Zijlstra /* 189689a09141SPeter Zijlstra * NFS congestion size, scale with available memory. 189789a09141SPeter Zijlstra * 189889a09141SPeter Zijlstra * 64MB: 8192k 189989a09141SPeter Zijlstra * 128MB: 11585k 190089a09141SPeter Zijlstra * 256MB: 16384k 190189a09141SPeter Zijlstra * 512MB: 23170k 190289a09141SPeter Zijlstra * 1GB: 32768k 190389a09141SPeter Zijlstra * 2GB: 46340k 190489a09141SPeter Zijlstra * 4GB: 65536k 190589a09141SPeter Zijlstra * 8GB: 92681k 190689a09141SPeter Zijlstra * 16GB: 131072k 190789a09141SPeter Zijlstra * 190889a09141SPeter Zijlstra * This allows larger machines to have larger/more transfers. 190989a09141SPeter Zijlstra * Limit the default to 256M 191089a09141SPeter Zijlstra */ 191189a09141SPeter Zijlstra nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); 191289a09141SPeter Zijlstra if (nfs_congestion_kb > 256*1024) 191389a09141SPeter Zijlstra nfs_congestion_kb = 256*1024; 191489a09141SPeter Zijlstra 19151da177e4SLinus Torvalds return 0; 19163dd4765fSJeff Layton 19173dd4765fSJeff Layton out_destroy_commit_cache: 19183dd4765fSJeff Layton kmem_cache_destroy(nfs_cdata_cachep); 19193dd4765fSJeff Layton out_destroy_write_mempool: 19203dd4765fSJeff Layton mempool_destroy(nfs_wdata_mempool); 19213dd4765fSJeff Layton out_destroy_write_cache: 19223dd4765fSJeff Layton kmem_cache_destroy(nfs_wdata_cachep); 19233dd4765fSJeff Layton return -ENOMEM; 19241da177e4SLinus Torvalds } 19251da177e4SLinus Torvalds 1926266bee88SDavid Brownell void nfs_destroy_writepagecache(void) 19271da177e4SLinus Torvalds { 19281da177e4SLinus Torvalds mempool_destroy(nfs_commit_mempool); 19293dd4765fSJeff Layton kmem_cache_destroy(nfs_cdata_cachep); 19301da177e4SLinus Torvalds mempool_destroy(nfs_wdata_mempool); 19311a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_wdata_cachep); 19321da177e4SLinus Torvalds } 19331da177e4SLinus Torvalds 1934