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; 494a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops; 501da177e4SLinus Torvalds 51e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep; 523feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool; 530b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep; 541da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool; 551da177e4SLinus Torvalds 560b7c0153SFred Isaman struct nfs_commit_data *nfs_commitdata_alloc(void) 571da177e4SLinus Torvalds { 58192e501bSMel Gorman struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOIO); 5940859d7eSChuck Lever 601da177e4SLinus Torvalds if (p) { 611da177e4SLinus Torvalds memset(p, 0, sizeof(*p)); 621da177e4SLinus Torvalds INIT_LIST_HEAD(&p->pages); 631da177e4SLinus Torvalds } 641da177e4SLinus Torvalds return p; 651da177e4SLinus Torvalds } 66e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc); 671da177e4SLinus Torvalds 680b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p) 691da177e4SLinus Torvalds { 701da177e4SLinus Torvalds mempool_free(p, nfs_commit_mempool); 711da177e4SLinus Torvalds } 72e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free); 731da177e4SLinus Torvalds 744a0de55cSAnna Schumaker static struct nfs_rw_header *nfs_writehdr_alloc(void) 753feb2d49STrond Myklebust { 76c0752cdfSAnna Schumaker struct nfs_rw_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO); 773feb2d49STrond Myklebust 784a0de55cSAnna Schumaker if (p) 793feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 803feb2d49STrond Myklebust return p; 813feb2d49STrond Myklebust } 823feb2d49STrond Myklebust 834a0de55cSAnna Schumaker static void nfs_writehdr_free(struct nfs_rw_header *whdr) 843feb2d49STrond Myklebust { 85cd841605SFred Isaman mempool_free(whdr, nfs_wdata_mempool); 863feb2d49STrond Myklebust } 873feb2d49STrond Myklebust 887b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error) 897b159fc1STrond Myklebust { 907b159fc1STrond Myklebust ctx->error = error; 917b159fc1STrond Myklebust smp_wmb(); 927b159fc1STrond Myklebust set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 937b159fc1STrond Myklebust } 947b159fc1STrond Myklebust 9529418aa4SMel Gorman static struct nfs_page * 9629418aa4SMel Gorman nfs_page_find_request_locked(struct nfs_inode *nfsi, struct page *page) 97277459d2STrond Myklebust { 98277459d2STrond Myklebust struct nfs_page *req = NULL; 99277459d2STrond Myklebust 10029418aa4SMel Gorman if (PagePrivate(page)) 101277459d2STrond Myklebust req = (struct nfs_page *)page_private(page); 10229418aa4SMel Gorman else if (unlikely(PageSwapCache(page))) { 10329418aa4SMel Gorman struct nfs_page *freq, *t; 10429418aa4SMel Gorman 10529418aa4SMel Gorman /* Linearly search the commit list for the correct req */ 10629418aa4SMel Gorman list_for_each_entry_safe(freq, t, &nfsi->commit_info.list, wb_list) { 10729418aa4SMel Gorman if (freq->wb_page == page) { 10829418aa4SMel Gorman req = freq; 10929418aa4SMel Gorman break; 110277459d2STrond Myklebust } 11129418aa4SMel Gorman } 11229418aa4SMel Gorman } 11329418aa4SMel Gorman 11429418aa4SMel Gorman if (req) 11529418aa4SMel Gorman kref_get(&req->wb_kref); 11629418aa4SMel Gorman 117277459d2STrond Myklebust return req; 118277459d2STrond Myklebust } 119277459d2STrond Myklebust 120277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page) 121277459d2STrond Myklebust { 122d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 123277459d2STrond Myklebust struct nfs_page *req = NULL; 124277459d2STrond Myklebust 125587142f8STrond Myklebust spin_lock(&inode->i_lock); 12629418aa4SMel Gorman req = nfs_page_find_request_locked(NFS_I(inode), page); 127587142f8STrond Myklebust spin_unlock(&inode->i_lock); 128277459d2STrond Myklebust return req; 129277459d2STrond Myklebust } 130277459d2STrond Myklebust 1311da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */ 1321da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) 1331da177e4SLinus Torvalds { 134d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 135a3d01454STrond Myklebust loff_t end, i_size; 136a3d01454STrond Myklebust pgoff_t end_index; 1371da177e4SLinus Torvalds 138a3d01454STrond Myklebust spin_lock(&inode->i_lock); 139a3d01454STrond Myklebust i_size = i_size_read(inode); 140a3d01454STrond Myklebust end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; 141d56b4ddfSMel Gorman if (i_size > 0 && page_file_index(page) < end_index) 142a3d01454STrond Myklebust goto out; 143d56b4ddfSMel Gorman end = page_file_offset(page) + ((loff_t)offset+count); 1441da177e4SLinus Torvalds if (i_size >= end) 145a3d01454STrond Myklebust goto out; 1461da177e4SLinus Torvalds i_size_write(inode, end); 147a3d01454STrond Myklebust nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); 148a3d01454STrond Myklebust out: 149a3d01454STrond Myklebust spin_unlock(&inode->i_lock); 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds 152a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */ 153a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page) 154a301b777STrond Myklebust { 155d56b4ddfSMel Gorman nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page)); 156a301b777STrond Myklebust } 157a301b777STrond Myklebust 1581da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request 1591da177e4SLinus Torvalds * covers the full page. 1601da177e4SLinus Torvalds */ 1611da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count) 1621da177e4SLinus Torvalds { 1631da177e4SLinus Torvalds if (PageUptodate(page)) 1641da177e4SLinus Torvalds return; 1651da177e4SLinus Torvalds if (base != 0) 1661da177e4SLinus Torvalds return; 16749a70f27STrond Myklebust if (count != nfs_page_length(page)) 1681da177e4SLinus Torvalds return; 1691da177e4SLinus Torvalds SetPageUptodate(page); 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc) 1731da177e4SLinus Torvalds { 1741da177e4SLinus Torvalds if (wbc->for_reclaim) 175c63c7b05STrond Myklebust return FLUSH_HIGHPRI | FLUSH_STABLE; 176b17621feSWu Fengguang if (wbc->for_kupdate || wbc->for_background) 177b31268acSTrond Myklebust return FLUSH_LOWPRI | FLUSH_COND_STABLE; 178b31268acSTrond Myklebust return FLUSH_COND_STABLE; 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds /* 18289a09141SPeter Zijlstra * NFS congestion control 18389a09141SPeter Zijlstra */ 18489a09141SPeter Zijlstra 18589a09141SPeter Zijlstra int nfs_congestion_kb; 18689a09141SPeter Zijlstra 18789a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) 18889a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH \ 18989a09141SPeter Zijlstra (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) 19089a09141SPeter Zijlstra 191deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page) 19289a09141SPeter Zijlstra { 193deed85e7STrond Myklebust struct nfs_server *nfss = NFS_SERVER(page_file_mapping(page)->host); 1945a6d41b3STrond Myklebust int ret = test_set_page_writeback(page); 1955a6d41b3STrond Myklebust 196deed85e7STrond Myklebust WARN_ON_ONCE(ret != 0); 19789a09141SPeter Zijlstra 198277866a0SPeter Zijlstra if (atomic_long_inc_return(&nfss->writeback) > 1998aa7e847SJens Axboe NFS_CONGESTION_ON_THRESH) { 2008aa7e847SJens Axboe set_bdi_congested(&nfss->backing_dev_info, 2018aa7e847SJens Axboe BLK_RW_ASYNC); 2028aa7e847SJens Axboe } 20389a09141SPeter Zijlstra } 20489a09141SPeter Zijlstra 20589a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page) 20689a09141SPeter Zijlstra { 207d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 20889a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 20989a09141SPeter Zijlstra 21089a09141SPeter Zijlstra end_page_writeback(page); 211c4dc4beeSPeter Zijlstra if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) 2128aa7e847SJens Axboe clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC); 21389a09141SPeter Zijlstra } 21489a09141SPeter Zijlstra 215cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock) 216e261f51fSTrond Myklebust { 217d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 218e261f51fSTrond Myklebust struct nfs_page *req; 219e261f51fSTrond Myklebust int ret; 220e261f51fSTrond Myklebust 221587142f8STrond Myklebust spin_lock(&inode->i_lock); 222e261f51fSTrond Myklebust for (;;) { 22329418aa4SMel Gorman req = nfs_page_find_request_locked(NFS_I(inode), page); 224074cc1deSTrond Myklebust if (req == NULL) 225074cc1deSTrond Myklebust break; 2267ad84aa9STrond Myklebust if (nfs_lock_request(req)) 227e261f51fSTrond Myklebust break; 228e261f51fSTrond Myklebust /* Note: If we hold the page lock, as is the case in nfs_writepage, 2297ad84aa9STrond Myklebust * then the call to nfs_lock_request() will always 230e261f51fSTrond Myklebust * succeed provided that someone hasn't already marked the 231e261f51fSTrond Myklebust * request as dirty (in which case we don't care). 232e261f51fSTrond Myklebust */ 233587142f8STrond Myklebust spin_unlock(&inode->i_lock); 234cfb506e1STrond Myklebust if (!nonblock) 235e261f51fSTrond Myklebust ret = nfs_wait_on_request(req); 236cfb506e1STrond Myklebust else 237cfb506e1STrond Myklebust ret = -EAGAIN; 238e261f51fSTrond Myklebust nfs_release_request(req); 239e261f51fSTrond Myklebust if (ret != 0) 240074cc1deSTrond Myklebust return ERR_PTR(ret); 241587142f8STrond Myklebust spin_lock(&inode->i_lock); 242e261f51fSTrond Myklebust } 243587142f8STrond Myklebust spin_unlock(&inode->i_lock); 244074cc1deSTrond Myklebust return req; 245612c9384STrond Myklebust } 246074cc1deSTrond Myklebust 247074cc1deSTrond Myklebust /* 248074cc1deSTrond Myklebust * Find an associated nfs write request, and prepare to flush it out 249074cc1deSTrond Myklebust * May return an error if the user signalled nfs_wait_on_request(). 250074cc1deSTrond Myklebust */ 251074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, 252cfb506e1STrond Myklebust struct page *page, bool nonblock) 253074cc1deSTrond Myklebust { 254074cc1deSTrond Myklebust struct nfs_page *req; 255074cc1deSTrond Myklebust int ret = 0; 256074cc1deSTrond Myklebust 257cfb506e1STrond Myklebust req = nfs_find_and_lock_request(page, nonblock); 258074cc1deSTrond Myklebust if (!req) 259074cc1deSTrond Myklebust goto out; 260074cc1deSTrond Myklebust ret = PTR_ERR(req); 261074cc1deSTrond Myklebust if (IS_ERR(req)) 262074cc1deSTrond Myklebust goto out; 263074cc1deSTrond Myklebust 264deed85e7STrond Myklebust nfs_set_page_writeback(page); 265deed85e7STrond Myklebust WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags)); 266074cc1deSTrond Myklebust 267deed85e7STrond Myklebust ret = 0; 268f8512ad0SFred Isaman if (!nfs_pageio_add_request(pgio, req)) { 269f8512ad0SFred Isaman nfs_redirty_request(req); 270074cc1deSTrond Myklebust ret = pgio->pg_error; 271f8512ad0SFred Isaman } 272074cc1deSTrond Myklebust out: 273074cc1deSTrond Myklebust return ret; 274e261f51fSTrond Myklebust } 275e261f51fSTrond Myklebust 276f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio) 277f758c885STrond Myklebust { 278d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 279cfb506e1STrond Myklebust int ret; 280f758c885STrond Myklebust 281f758c885STrond Myklebust nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); 282f758c885STrond Myklebust nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); 283f758c885STrond Myklebust 284d56b4ddfSMel Gorman nfs_pageio_cond_complete(pgio, page_file_index(page)); 2851b430beeSWu Fengguang ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE); 286cfb506e1STrond Myklebust if (ret == -EAGAIN) { 287cfb506e1STrond Myklebust redirty_page_for_writepage(wbc, page); 288cfb506e1STrond Myklebust ret = 0; 289cfb506e1STrond Myklebust } 290cfb506e1STrond Myklebust return ret; 291f758c885STrond Myklebust } 292f758c885STrond Myklebust 293e261f51fSTrond Myklebust /* 2941da177e4SLinus Torvalds * Write an mmapped page to the server. 2951da177e4SLinus Torvalds */ 2964d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc) 2971da177e4SLinus Torvalds { 298f758c885STrond Myklebust struct nfs_pageio_descriptor pgio; 299e261f51fSTrond Myklebust int err; 3001da177e4SLinus Torvalds 301a20c93e3SChristoph Hellwig nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc), 302a20c93e3SChristoph Hellwig false, &nfs_async_write_completion_ops); 303f758c885STrond Myklebust err = nfs_do_writepage(page, wbc, &pgio); 304f758c885STrond Myklebust nfs_pageio_complete(&pgio); 305f758c885STrond Myklebust if (err < 0) 3064d770ccfSTrond Myklebust return err; 307f758c885STrond Myklebust if (pgio.pg_error < 0) 308f758c885STrond Myklebust return pgio.pg_error; 309f758c885STrond Myklebust return 0; 3104d770ccfSTrond Myklebust } 3114d770ccfSTrond Myklebust 3124d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc) 3134d770ccfSTrond Myklebust { 314f758c885STrond Myklebust int ret; 3154d770ccfSTrond Myklebust 316f758c885STrond Myklebust ret = nfs_writepage_locked(page, wbc); 3171da177e4SLinus Torvalds unlock_page(page); 318f758c885STrond Myklebust return ret; 319f758c885STrond Myklebust } 320f758c885STrond Myklebust 321f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) 322f758c885STrond Myklebust { 323f758c885STrond Myklebust int ret; 324f758c885STrond Myklebust 325f758c885STrond Myklebust ret = nfs_do_writepage(page, wbc, data); 326f758c885STrond Myklebust unlock_page(page); 327f758c885STrond Myklebust return ret; 3281da177e4SLinus Torvalds } 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) 3311da177e4SLinus Torvalds { 3321da177e4SLinus Torvalds struct inode *inode = mapping->host; 33372cb77f4STrond Myklebust unsigned long *bitlock = &NFS_I(inode)->flags; 334c63c7b05STrond Myklebust struct nfs_pageio_descriptor pgio; 3351da177e4SLinus Torvalds int err; 3361da177e4SLinus Torvalds 33772cb77f4STrond Myklebust /* Stop dirtying of new pages while we sync */ 33872cb77f4STrond Myklebust err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING, 33972cb77f4STrond Myklebust nfs_wait_bit_killable, TASK_KILLABLE); 34072cb77f4STrond Myklebust if (err) 34172cb77f4STrond Myklebust goto out_err; 34272cb77f4STrond Myklebust 34391d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); 34491d5b470SChuck Lever 345a20c93e3SChristoph Hellwig nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false, 346a20c93e3SChristoph Hellwig &nfs_async_write_completion_ops); 347f758c885STrond Myklebust err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); 348c63c7b05STrond Myklebust nfs_pageio_complete(&pgio); 34972cb77f4STrond Myklebust 35072cb77f4STrond Myklebust clear_bit_unlock(NFS_INO_FLUSHING, bitlock); 35172cb77f4STrond Myklebust smp_mb__after_clear_bit(); 35272cb77f4STrond Myklebust wake_up_bit(bitlock, NFS_INO_FLUSHING); 35372cb77f4STrond Myklebust 354f758c885STrond Myklebust if (err < 0) 35572cb77f4STrond Myklebust goto out_err; 35672cb77f4STrond Myklebust err = pgio.pg_error; 35772cb77f4STrond Myklebust if (err < 0) 35872cb77f4STrond Myklebust goto out_err; 359c63c7b05STrond Myklebust return 0; 36072cb77f4STrond Myklebust out_err: 36172cb77f4STrond Myklebust return err; 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds /* 3651da177e4SLinus Torvalds * Insert a write request into an inode 3661da177e4SLinus Torvalds */ 367d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req) 3681da177e4SLinus Torvalds { 3691da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 370e7d39069STrond Myklebust 371e7d39069STrond Myklebust /* Lock the request! */ 3727ad84aa9STrond Myklebust nfs_lock_request(req); 373e7d39069STrond Myklebust 374e7d39069STrond Myklebust spin_lock(&inode->i_lock); 375011e2a7fSBryan Schumaker if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE)) 376a9a4a87aSTrond Myklebust inode->i_version++; 37729418aa4SMel Gorman /* 37829418aa4SMel Gorman * Swap-space should not get truncated. Hence no need to plug the race 37929418aa4SMel Gorman * with invalidate/truncate. 38029418aa4SMel Gorman */ 38129418aa4SMel Gorman if (likely(!PageSwapCache(req->wb_page))) { 3822df485a7STrond Myklebust set_bit(PG_MAPPED, &req->wb_flags); 383deb7d638STrond Myklebust SetPagePrivate(req->wb_page); 384277459d2STrond Myklebust set_page_private(req->wb_page, (unsigned long)req); 38529418aa4SMel Gorman } 3861da177e4SLinus Torvalds nfsi->npages++; 387c03b4024STrond Myklebust kref_get(&req->wb_kref); 388e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds /* 39289a09141SPeter Zijlstra * Remove a write request from an inode 3931da177e4SLinus Torvalds */ 3941da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req) 3951da177e4SLinus Torvalds { 3963d4ff43dSAl Viro struct inode *inode = req->wb_context->dentry->d_inode; 3971da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 3981da177e4SLinus Torvalds 399587142f8STrond Myklebust spin_lock(&inode->i_lock); 40029418aa4SMel Gorman if (likely(!PageSwapCache(req->wb_page))) { 401277459d2STrond Myklebust set_page_private(req->wb_page, 0); 402deb7d638STrond Myklebust ClearPagePrivate(req->wb_page); 4032df485a7STrond Myklebust clear_bit(PG_MAPPED, &req->wb_flags); 40429418aa4SMel Gorman } 4051da177e4SLinus Torvalds nfsi->npages--; 406587142f8STrond Myklebust spin_unlock(&inode->i_lock); 4071da177e4SLinus Torvalds nfs_release_request(req); 4081da177e4SLinus Torvalds } 4091da177e4SLinus Torvalds 41061822ab5STrond Myklebust static void 4116d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req) 41261822ab5STrond Myklebust { 41361822ab5STrond Myklebust __set_page_dirty_nobuffers(req->wb_page); 41461822ab5STrond Myklebust } 41561822ab5STrond Myklebust 41689d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4) 4178dd37758STrond Myklebust /** 4188dd37758STrond Myklebust * nfs_request_add_commit_list - add request to a commit list 4198dd37758STrond Myklebust * @req: pointer to a struct nfs_page 420ea2cf228SFred Isaman * @dst: commit list head 421ea2cf228SFred Isaman * @cinfo: holds list lock and accounting info 4228dd37758STrond Myklebust * 423ea2cf228SFred Isaman * This sets the PG_CLEAN bit, updates the cinfo count of 4248dd37758STrond Myklebust * number of outstanding requests requiring a commit as well as 4258dd37758STrond Myklebust * the MM page stats. 4268dd37758STrond Myklebust * 427ea2cf228SFred Isaman * The caller must _not_ hold the cinfo->lock, but must be 4288dd37758STrond Myklebust * holding the nfs_page lock. 4298dd37758STrond Myklebust */ 4308dd37758STrond Myklebust void 431ea2cf228SFred Isaman nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst, 432ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 4338dd37758STrond Myklebust { 4348dd37758STrond Myklebust set_bit(PG_CLEAN, &(req)->wb_flags); 435ea2cf228SFred Isaman spin_lock(cinfo->lock); 436ea2cf228SFred Isaman nfs_list_add_request(req, dst); 437ea2cf228SFred Isaman cinfo->mds->ncommit++; 438ea2cf228SFred Isaman spin_unlock(cinfo->lock); 43956f9cd68SFred Isaman if (!cinfo->dreq) { 4408dd37758STrond Myklebust inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 441d56b4ddfSMel Gorman inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info, 44256f9cd68SFred Isaman BDI_RECLAIMABLE); 44356f9cd68SFred Isaman __mark_inode_dirty(req->wb_context->dentry->d_inode, 44456f9cd68SFred Isaman I_DIRTY_DATASYNC); 44556f9cd68SFred Isaman } 4468dd37758STrond Myklebust } 4478dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list); 4488dd37758STrond Myklebust 4498dd37758STrond Myklebust /** 4508dd37758STrond Myklebust * nfs_request_remove_commit_list - Remove request from a commit list 4518dd37758STrond Myklebust * @req: pointer to a nfs_page 452ea2cf228SFred Isaman * @cinfo: holds list lock and accounting info 4538dd37758STrond Myklebust * 454ea2cf228SFred Isaman * This clears the PG_CLEAN bit, and updates the cinfo's count of 4558dd37758STrond Myklebust * number of outstanding requests requiring a commit 4568dd37758STrond Myklebust * It does not update the MM page stats. 4578dd37758STrond Myklebust * 458ea2cf228SFred Isaman * The caller _must_ hold the cinfo->lock and the nfs_page lock. 4598dd37758STrond Myklebust */ 4608dd37758STrond Myklebust void 461ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req, 462ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 4638dd37758STrond Myklebust { 4648dd37758STrond Myklebust if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) 4658dd37758STrond Myklebust return; 4668dd37758STrond Myklebust nfs_list_remove_request(req); 467ea2cf228SFred Isaman cinfo->mds->ncommit--; 4688dd37758STrond Myklebust } 4698dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list); 4708dd37758STrond Myklebust 471ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, 472ea2cf228SFred Isaman struct inode *inode) 473ea2cf228SFred Isaman { 474ea2cf228SFred Isaman cinfo->lock = &inode->i_lock; 475ea2cf228SFred Isaman cinfo->mds = &NFS_I(inode)->commit_info; 476ea2cf228SFred Isaman cinfo->ds = pnfs_get_ds_info(inode); 477b359f9d0SFred Isaman cinfo->dreq = NULL; 478f453a54aSFred Isaman cinfo->completion_ops = &nfs_commit_completion_ops; 479ea2cf228SFred Isaman } 480ea2cf228SFred Isaman 481ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo, 482ea2cf228SFred Isaman struct inode *inode, 483ea2cf228SFred Isaman struct nfs_direct_req *dreq) 484ea2cf228SFred Isaman { 4851763da12SFred Isaman if (dreq) 4861763da12SFred Isaman nfs_init_cinfo_from_dreq(cinfo, dreq); 4871763da12SFred Isaman else 488ea2cf228SFred Isaman nfs_init_cinfo_from_inode(cinfo, inode); 489ea2cf228SFred Isaman } 490ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo); 4918dd37758STrond Myklebust 4921da177e4SLinus Torvalds /* 4931da177e4SLinus Torvalds * Add a request to the inode's commit list. 4941da177e4SLinus Torvalds */ 4951763da12SFred Isaman void 496ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, 497ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 4981da177e4SLinus Torvalds { 499ea2cf228SFred Isaman if (pnfs_mark_request_commit(req, lseg, cinfo)) 5008dd37758STrond Myklebust return; 501ea2cf228SFred Isaman nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo); 5021da177e4SLinus Torvalds } 5038e821cadSTrond Myklebust 504d6d6dc7cSFred Isaman static void 505d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page) 506e468bae9STrond Myklebust { 507e468bae9STrond Myklebust dec_zone_page_state(page, NR_UNSTABLE_NFS); 508d56b4ddfSMel Gorman dec_bdi_stat(page_file_mapping(page)->backing_dev_info, BDI_RECLAIMABLE); 509e468bae9STrond Myklebust } 510d6d6dc7cSFred Isaman 5118dd37758STrond Myklebust static void 512d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req) 513d6d6dc7cSFred Isaman { 5148dd37758STrond Myklebust if (test_bit(PG_CLEAN, &req->wb_flags)) { 5158dd37758STrond Myklebust struct inode *inode = req->wb_context->dentry->d_inode; 516ea2cf228SFred Isaman struct nfs_commit_info cinfo; 517d6d6dc7cSFred Isaman 518ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, inode); 519ea2cf228SFred Isaman if (!pnfs_clear_request_commit(req, &cinfo)) { 520ea2cf228SFred Isaman spin_lock(cinfo.lock); 521ea2cf228SFred Isaman nfs_request_remove_commit_list(req, &cinfo); 522ea2cf228SFred Isaman spin_unlock(cinfo.lock); 523d6d6dc7cSFred Isaman } 5248dd37758STrond Myklebust nfs_clear_page_commit(req->wb_page); 5258dd37758STrond Myklebust } 526e468bae9STrond Myklebust } 527e468bae9STrond Myklebust 5288e821cadSTrond Myklebust static inline 5299c7e1b3dSAnna Schumaker int nfs_write_need_commit(struct nfs_pgio_data *data) 5308e821cadSTrond Myklebust { 531465d5243SFred Isaman if (data->verf.committed == NFS_DATA_SYNC) 532cd841605SFred Isaman return data->header->lseg == NULL; 5338e821cadSTrond Myklebust return data->verf.committed != NFS_FILE_SYNC; 5348e821cadSTrond Myklebust } 5358e821cadSTrond Myklebust 5368e821cadSTrond Myklebust #else 53768cd6fa4SBryan Schumaker static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, 53868cd6fa4SBryan Schumaker struct inode *inode) 53968cd6fa4SBryan Schumaker { 54068cd6fa4SBryan Schumaker } 54168cd6fa4SBryan Schumaker 54268cd6fa4SBryan Schumaker void nfs_init_cinfo(struct nfs_commit_info *cinfo, 54368cd6fa4SBryan Schumaker struct inode *inode, 54468cd6fa4SBryan Schumaker struct nfs_direct_req *dreq) 54568cd6fa4SBryan Schumaker { 54668cd6fa4SBryan Schumaker } 54768cd6fa4SBryan Schumaker 5481763da12SFred Isaman void 549ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, 550ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 5518e821cadSTrond Myklebust { 5528e821cadSTrond Myklebust } 5538e821cadSTrond Myklebust 5548dd37758STrond Myklebust static void 555e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req) 556e468bae9STrond Myklebust { 557e468bae9STrond Myklebust } 558e468bae9STrond Myklebust 5598e821cadSTrond Myklebust static inline 5609c7e1b3dSAnna Schumaker int nfs_write_need_commit(struct nfs_pgio_data *data) 5618e821cadSTrond Myklebust { 5628e821cadSTrond Myklebust return 0; 5638e821cadSTrond Myklebust } 5648e821cadSTrond Myklebust 5651da177e4SLinus Torvalds #endif 5661da177e4SLinus Torvalds 567061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr) 5686c75dc0dSFred Isaman { 569ea2cf228SFred Isaman struct nfs_commit_info cinfo; 5706c75dc0dSFred Isaman unsigned long bytes = 0; 5716c75dc0dSFred Isaman 5726c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 5736c75dc0dSFred Isaman goto out; 574ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, hdr->inode); 5756c75dc0dSFred Isaman while (!list_empty(&hdr->pages)) { 5766c75dc0dSFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 5776c75dc0dSFred Isaman 5786c75dc0dSFred Isaman bytes += req->wb_bytes; 5796c75dc0dSFred Isaman nfs_list_remove_request(req); 5806c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && 5816c75dc0dSFred Isaman (hdr->good_bytes < bytes)) { 582d1182b33STrond Myklebust nfs_set_pageerror(req->wb_page); 5836c75dc0dSFred Isaman nfs_context_set_write_error(req->wb_context, hdr->error); 5846c75dc0dSFred Isaman goto remove_req; 5856c75dc0dSFred Isaman } 5866c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) { 5876c75dc0dSFred Isaman nfs_mark_request_dirty(req); 5886c75dc0dSFred Isaman goto next; 5896c75dc0dSFred Isaman } 5906c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) { 591f79d06f5SAnna Schumaker memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf)); 592ea2cf228SFred Isaman nfs_mark_request_commit(req, hdr->lseg, &cinfo); 5936c75dc0dSFred Isaman goto next; 5946c75dc0dSFred Isaman } 5956c75dc0dSFred Isaman remove_req: 5966c75dc0dSFred Isaman nfs_inode_remove_request(req); 5976c75dc0dSFred Isaman next: 5981d1afcbcSTrond Myklebust nfs_unlock_request(req); 599d1182b33STrond Myklebust nfs_end_page_writeback(req->wb_page); 6003aff4ebbSTrond Myklebust nfs_release_request(req); 6016c75dc0dSFred Isaman } 6026c75dc0dSFred Isaman out: 6036c75dc0dSFred Isaman hdr->release(hdr); 6046c75dc0dSFred Isaman } 6056c75dc0dSFred Isaman 60689d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4) 607ea2cf228SFred Isaman static unsigned long 608ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo) 609fb8a1f11STrond Myklebust { 610ea2cf228SFred Isaman return cinfo->mds->ncommit; 611fb8a1f11STrond Myklebust } 612fb8a1f11STrond Myklebust 613ea2cf228SFred Isaman /* cinfo->lock held by caller */ 6141763da12SFred Isaman int 615ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst, 616ea2cf228SFred Isaman struct nfs_commit_info *cinfo, int max) 617d6d6dc7cSFred Isaman { 618d6d6dc7cSFred Isaman struct nfs_page *req, *tmp; 619d6d6dc7cSFred Isaman int ret = 0; 620d6d6dc7cSFred Isaman 621d6d6dc7cSFred Isaman list_for_each_entry_safe(req, tmp, src, wb_list) { 6228dd37758STrond Myklebust if (!nfs_lock_request(req)) 6238dd37758STrond Myklebust continue; 6247ad84aa9STrond Myklebust kref_get(&req->wb_kref); 625ea2cf228SFred Isaman if (cond_resched_lock(cinfo->lock)) 6263b3be88dSTrond Myklebust list_safe_reset_next(req, tmp, wb_list); 627ea2cf228SFred Isaman nfs_request_remove_commit_list(req, cinfo); 6288dd37758STrond Myklebust nfs_list_add_request(req, dst); 629d6d6dc7cSFred Isaman ret++; 6301763da12SFred Isaman if ((ret == max) && !cinfo->dreq) 631d6d6dc7cSFred Isaman break; 632d6d6dc7cSFred Isaman } 633d6d6dc7cSFred Isaman return ret; 634d6d6dc7cSFred Isaman } 635d6d6dc7cSFred Isaman 6361da177e4SLinus Torvalds /* 6371da177e4SLinus Torvalds * nfs_scan_commit - Scan an inode for commit requests 6381da177e4SLinus Torvalds * @inode: NFS inode to scan 639ea2cf228SFred Isaman * @dst: mds destination list 640ea2cf228SFred Isaman * @cinfo: mds and ds lists of reqs ready to commit 6411da177e4SLinus Torvalds * 6421da177e4SLinus Torvalds * Moves requests from the inode's 'commit' request list. 6431da177e4SLinus Torvalds * The requests are *not* checked to ensure that they form a contiguous set. 6441da177e4SLinus Torvalds */ 6451763da12SFred Isaman int 646ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst, 647ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 6481da177e4SLinus Torvalds { 649d6d6dc7cSFred Isaman int ret = 0; 650fb8a1f11STrond Myklebust 651ea2cf228SFred Isaman spin_lock(cinfo->lock); 652ea2cf228SFred Isaman if (cinfo->mds->ncommit > 0) { 6538dd37758STrond Myklebust const int max = INT_MAX; 654d6d6dc7cSFred Isaman 655ea2cf228SFred Isaman ret = nfs_scan_commit_list(&cinfo->mds->list, dst, 656ea2cf228SFred Isaman cinfo, max); 657ea2cf228SFred Isaman ret += pnfs_scan_commit_lists(inode, cinfo, max - ret); 658d6d6dc7cSFred Isaman } 659ea2cf228SFred Isaman spin_unlock(cinfo->lock); 660ff778d02STrond Myklebust return ret; 6611da177e4SLinus Torvalds } 662d6d6dc7cSFred Isaman 663c42de9ddSTrond Myklebust #else 664ea2cf228SFred Isaman static unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo) 665fb8a1f11STrond Myklebust { 666fb8a1f11STrond Myklebust return 0; 667fb8a1f11STrond Myklebust } 668fb8a1f11STrond Myklebust 6691763da12SFred Isaman int nfs_scan_commit(struct inode *inode, struct list_head *dst, 670ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 671c42de9ddSTrond Myklebust { 672c42de9ddSTrond Myklebust return 0; 673c42de9ddSTrond Myklebust } 6741da177e4SLinus Torvalds #endif 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds /* 677e7d39069STrond Myklebust * Search for an existing write request, and attempt to update 678e7d39069STrond Myklebust * it to reflect a new dirty region on a given page. 6791da177e4SLinus Torvalds * 680e7d39069STrond Myklebust * If the attempt fails, then the existing request is flushed out 681e7d39069STrond Myklebust * to disk. 6821da177e4SLinus Torvalds */ 683e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode, 684e7d39069STrond Myklebust struct page *page, 685e7d39069STrond Myklebust unsigned int offset, 686e7d39069STrond Myklebust unsigned int bytes) 6871da177e4SLinus Torvalds { 688e7d39069STrond Myklebust struct nfs_page *req; 689e7d39069STrond Myklebust unsigned int rqend; 690e7d39069STrond Myklebust unsigned int end; 6911da177e4SLinus Torvalds int error; 692277459d2STrond Myklebust 693e7d39069STrond Myklebust if (!PagePrivate(page)) 694e7d39069STrond Myklebust return NULL; 695e7d39069STrond Myklebust 696e7d39069STrond Myklebust end = offset + bytes; 697e7d39069STrond Myklebust spin_lock(&inode->i_lock); 698e7d39069STrond Myklebust 699e7d39069STrond Myklebust for (;;) { 70029418aa4SMel Gorman req = nfs_page_find_request_locked(NFS_I(inode), page); 701e7d39069STrond Myklebust if (req == NULL) 702e7d39069STrond Myklebust goto out_unlock; 703e7d39069STrond Myklebust 704e7d39069STrond Myklebust rqend = req->wb_offset + req->wb_bytes; 705e7d39069STrond Myklebust /* 706e7d39069STrond Myklebust * Tell the caller to flush out the request if 707e7d39069STrond Myklebust * the offsets are non-contiguous. 708e7d39069STrond Myklebust * Note: nfs_flush_incompatible() will already 709e7d39069STrond Myklebust * have flushed out requests having wrong owners. 710e7d39069STrond Myklebust */ 711e468bae9STrond Myklebust if (offset > rqend 712e7d39069STrond Myklebust || end < req->wb_offset) 713e7d39069STrond Myklebust goto out_flushme; 714e7d39069STrond Myklebust 7157ad84aa9STrond Myklebust if (nfs_lock_request(req)) 716e7d39069STrond Myklebust break; 717e7d39069STrond Myklebust 718e7d39069STrond Myklebust /* The request is locked, so wait and then retry */ 719587142f8STrond Myklebust spin_unlock(&inode->i_lock); 7201da177e4SLinus Torvalds error = nfs_wait_on_request(req); 7211da177e4SLinus Torvalds nfs_release_request(req); 722e7d39069STrond Myklebust if (error != 0) 723e7d39069STrond Myklebust goto out_err; 724e7d39069STrond Myklebust spin_lock(&inode->i_lock); 7251da177e4SLinus Torvalds } 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds /* Okay, the request matches. Update the region */ 7281da177e4SLinus Torvalds if (offset < req->wb_offset) { 7291da177e4SLinus Torvalds req->wb_offset = offset; 7301da177e4SLinus Torvalds req->wb_pgbase = offset; 7311da177e4SLinus Torvalds } 7321da177e4SLinus Torvalds if (end > rqend) 7331da177e4SLinus Torvalds req->wb_bytes = end - req->wb_offset; 734e7d39069STrond Myklebust else 735e7d39069STrond Myklebust req->wb_bytes = rqend - req->wb_offset; 736e7d39069STrond Myklebust out_unlock: 737e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 738ca138f36SFred Isaman if (req) 7398dd37758STrond Myklebust nfs_clear_request_commit(req); 740e7d39069STrond Myklebust return req; 741e7d39069STrond Myklebust out_flushme: 742e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 743e7d39069STrond Myklebust nfs_release_request(req); 744e7d39069STrond Myklebust error = nfs_wb_page(inode, page); 745e7d39069STrond Myklebust out_err: 746e7d39069STrond Myklebust return ERR_PTR(error); 747e7d39069STrond Myklebust } 7481da177e4SLinus Torvalds 749e7d39069STrond Myklebust /* 750e7d39069STrond Myklebust * Try to update an existing write request, or create one if there is none. 751e7d39069STrond Myklebust * 752e7d39069STrond Myklebust * Note: Should always be called with the Page Lock held to prevent races 753e7d39069STrond Myklebust * if we have to add a new request. Also assumes that the caller has 754e7d39069STrond Myklebust * already called nfs_flush_incompatible() if necessary. 755e7d39069STrond Myklebust */ 756e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, 757e7d39069STrond Myklebust struct page *page, unsigned int offset, unsigned int bytes) 758e7d39069STrond Myklebust { 759d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 760e7d39069STrond Myklebust struct nfs_page *req; 761e7d39069STrond Myklebust 762e7d39069STrond Myklebust req = nfs_try_to_update_request(inode, page, offset, bytes); 763e7d39069STrond Myklebust if (req != NULL) 764e7d39069STrond Myklebust goto out; 765e7d39069STrond Myklebust req = nfs_create_request(ctx, inode, page, offset, bytes); 766e7d39069STrond Myklebust if (IS_ERR(req)) 767e7d39069STrond Myklebust goto out; 768d6d6dc7cSFred Isaman nfs_inode_add_request(inode, req); 769efc91ed0STrond Myklebust out: 77061e930a9STrond Myklebust return req; 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds 773e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, 774e7d39069STrond Myklebust unsigned int offset, unsigned int count) 775e7d39069STrond Myklebust { 776e7d39069STrond Myklebust struct nfs_page *req; 777e7d39069STrond Myklebust 778e7d39069STrond Myklebust req = nfs_setup_write_request(ctx, page, offset, count); 779e7d39069STrond Myklebust if (IS_ERR(req)) 780e7d39069STrond Myklebust return PTR_ERR(req); 781e7d39069STrond Myklebust /* Update file length */ 782e7d39069STrond Myklebust nfs_grow_file(page, offset, count); 783e7d39069STrond Myklebust nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); 784a6305ddbSTrond Myklebust nfs_mark_request_dirty(req); 7851d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 786e7d39069STrond Myklebust return 0; 787e7d39069STrond Myklebust } 788e7d39069STrond Myklebust 7891da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page) 7901da177e4SLinus Torvalds { 791cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 7922a369153STrond Myklebust struct nfs_lock_context *l_ctx; 7931da177e4SLinus Torvalds struct nfs_page *req; 7941a54533eSTrond Myklebust int do_flush, status; 7951da177e4SLinus Torvalds /* 7961da177e4SLinus Torvalds * Look for a request corresponding to this page. If there 7971da177e4SLinus Torvalds * is one, and it belongs to another file, we flush it out 7981da177e4SLinus Torvalds * before we try to copy anything into the page. Do this 7991da177e4SLinus Torvalds * due to the lack of an ACCESS-type call in NFSv2. 8001da177e4SLinus Torvalds * Also do the same if we find a request from an existing 8011da177e4SLinus Torvalds * dropped page. 8021da177e4SLinus Torvalds */ 8031a54533eSTrond Myklebust do { 804277459d2STrond Myklebust req = nfs_page_find_request(page); 8051a54533eSTrond Myklebust if (req == NULL) 8061a54533eSTrond Myklebust return 0; 8072a369153STrond Myklebust l_ctx = req->wb_lock_context; 8082a369153STrond Myklebust do_flush = req->wb_page != page || req->wb_context != ctx; 8090f1d2605STrond Myklebust if (l_ctx && ctx->dentry->d_inode->i_flock != NULL) { 8102a369153STrond Myklebust do_flush |= l_ctx->lockowner.l_owner != current->files 8112a369153STrond Myklebust || l_ctx->lockowner.l_pid != current->tgid; 8122a369153STrond Myklebust } 8131da177e4SLinus Torvalds nfs_release_request(req); 8141a54533eSTrond Myklebust if (!do_flush) 8151a54533eSTrond Myklebust return 0; 816d56b4ddfSMel Gorman status = nfs_wb_page(page_file_mapping(page)->host, page); 8171a54533eSTrond Myklebust } while (status == 0); 8181a54533eSTrond Myklebust return status; 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds /* 822dc24826bSAndy Adamson * Avoid buffered writes when a open context credential's key would 823dc24826bSAndy Adamson * expire soon. 824dc24826bSAndy Adamson * 825dc24826bSAndy Adamson * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL. 826dc24826bSAndy Adamson * 827dc24826bSAndy Adamson * Return 0 and set a credential flag which triggers the inode to flush 828dc24826bSAndy Adamson * and performs NFS_FILE_SYNC writes if the key will expired within 829dc24826bSAndy Adamson * RPC_KEY_EXPIRE_TIMEO. 830dc24826bSAndy Adamson */ 831dc24826bSAndy Adamson int 832dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode) 833dc24826bSAndy Adamson { 834dc24826bSAndy Adamson struct nfs_open_context *ctx = nfs_file_open_context(filp); 835dc24826bSAndy Adamson struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth; 836dc24826bSAndy Adamson 837dc24826bSAndy Adamson return rpcauth_key_timeout_notify(auth, ctx->cred); 838dc24826bSAndy Adamson } 839dc24826bSAndy Adamson 840dc24826bSAndy Adamson /* 841dc24826bSAndy Adamson * Test if the open context credential key is marked to expire soon. 842dc24826bSAndy Adamson */ 843dc24826bSAndy Adamson bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx) 844dc24826bSAndy Adamson { 845dc24826bSAndy Adamson return rpcauth_cred_key_to_expire(ctx->cred); 846dc24826bSAndy Adamson } 847dc24826bSAndy Adamson 848dc24826bSAndy Adamson /* 8495d47a356STrond Myklebust * If the page cache is marked as unsafe or invalid, then we can't rely on 8505d47a356STrond Myklebust * the PageUptodate() flag. In this case, we will need to turn off 8515d47a356STrond Myklebust * write optimisations that depend on the page contents being correct. 8525d47a356STrond Myklebust */ 8538d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode) 8545d47a356STrond Myklebust { 855d529ef83SJeff Layton struct nfs_inode *nfsi = NFS_I(inode); 856d529ef83SJeff Layton 8578d197a56STrond Myklebust if (nfs_have_delegated_attributes(inode)) 8588d197a56STrond Myklebust goto out; 859d529ef83SJeff Layton if (nfsi->cache_validity & (NFS_INO_INVALID_DATA|NFS_INO_REVAL_PAGECACHE)) 860d529ef83SJeff Layton return false; 8614db72b40SJeff Layton smp_rmb(); 862d529ef83SJeff Layton if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags)) 8638d197a56STrond Myklebust return false; 8648d197a56STrond Myklebust out: 8658d197a56STrond Myklebust return PageUptodate(page) != 0; 8665d47a356STrond Myklebust } 8675d47a356STrond Myklebust 868c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or 869c7559663SScott Mayhew * if we have the whole file locked for writing), it may be more efficient to 870c7559663SScott Mayhew * extend the write to cover the entire page in order to avoid fragmentation 871c7559663SScott Mayhew * inefficiencies. 872c7559663SScott Mayhew * 873263b4509SScott Mayhew * If the file is opened for synchronous writes then we can just skip the rest 874263b4509SScott Mayhew * of the checks. 875c7559663SScott Mayhew */ 876c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode) 877c7559663SScott Mayhew { 878c7559663SScott Mayhew if (file->f_flags & O_DSYNC) 879c7559663SScott Mayhew return 0; 880263b4509SScott Mayhew if (!nfs_write_pageuptodate(page, inode)) 881263b4509SScott Mayhew return 0; 882c7559663SScott Mayhew if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE)) 883c7559663SScott Mayhew return 1; 884263b4509SScott Mayhew if (inode->i_flock == NULL || (inode->i_flock->fl_start == 0 && 885c7559663SScott Mayhew inode->i_flock->fl_end == OFFSET_MAX && 886263b4509SScott Mayhew inode->i_flock->fl_type != F_RDLCK)) 887c7559663SScott Mayhew return 1; 888c7559663SScott Mayhew return 0; 889c7559663SScott Mayhew } 890c7559663SScott Mayhew 8915d47a356STrond Myklebust /* 8921da177e4SLinus Torvalds * Update and possibly write a cached page of an NFS file. 8931da177e4SLinus Torvalds * 8941da177e4SLinus Torvalds * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad 8951da177e4SLinus Torvalds * things with a page scheduled for an RPC call (e.g. invalidate it). 8961da177e4SLinus Torvalds */ 8971da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page, 8981da177e4SLinus Torvalds unsigned int offset, unsigned int count) 8991da177e4SLinus Torvalds { 900cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 901d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 9021da177e4SLinus Torvalds int status = 0; 9031da177e4SLinus Torvalds 90491d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 90591d5b470SChuck Lever 9066de1472fSAl Viro dprintk("NFS: nfs_updatepage(%pD2 %d@%lld)\n", 9076de1472fSAl Viro file, count, (long long)(page_file_offset(page) + offset)); 9081da177e4SLinus Torvalds 909c7559663SScott Mayhew if (nfs_can_extend_write(file, page, inode)) { 91049a70f27STrond Myklebust count = max(count + offset, nfs_page_length(page)); 9111da177e4SLinus Torvalds offset = 0; 9121da177e4SLinus Torvalds } 9131da177e4SLinus Torvalds 914e21195a7STrond Myklebust status = nfs_writepage_setup(ctx, page, offset, count); 91503fa9e84STrond Myklebust if (status < 0) 91603fa9e84STrond Myklebust nfs_set_pageerror(page); 91759b7c05fSTrond Myklebust else 91859b7c05fSTrond Myklebust __set_page_dirty_nobuffers(page); 9191da177e4SLinus Torvalds 92048186c7dSChuck Lever dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", 9211da177e4SLinus Torvalds status, (long long)i_size_read(inode)); 9221da177e4SLinus Torvalds return status; 9231da177e4SLinus Torvalds } 9241da177e4SLinus Torvalds 9253ff7576dSTrond Myklebust static int flush_task_priority(int how) 9261da177e4SLinus Torvalds { 9271da177e4SLinus Torvalds switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { 9281da177e4SLinus Torvalds case FLUSH_HIGHPRI: 9291da177e4SLinus Torvalds return RPC_PRIORITY_HIGH; 9301da177e4SLinus Torvalds case FLUSH_LOWPRI: 9311da177e4SLinus Torvalds return RPC_PRIORITY_LOW; 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds return RPC_PRIORITY_NORMAL; 9341da177e4SLinus Torvalds } 9351da177e4SLinus Torvalds 936c5996c4eSFred Isaman int nfs_initiate_write(struct rpc_clnt *clnt, 9379c7e1b3dSAnna Schumaker struct nfs_pgio_data *data, 938788e7a89STrond Myklebust const struct rpc_call_ops *call_ops, 9399f0ec176SAndy Adamson int how, int flags) 9401da177e4SLinus Torvalds { 941cd841605SFred Isaman struct inode *inode = data->header->inode; 9423ff7576dSTrond Myklebust int priority = flush_task_priority(how); 94307737691STrond Myklebust struct rpc_task *task; 944bdc7f021STrond Myklebust struct rpc_message msg = { 945bdc7f021STrond Myklebust .rpc_argp = &data->args, 946bdc7f021STrond Myklebust .rpc_resp = &data->res, 947cd841605SFred Isaman .rpc_cred = data->header->cred, 948bdc7f021STrond Myklebust }; 94984115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 950d138d5d1SAndy Adamson .rpc_client = clnt, 95107737691STrond Myklebust .task = &data->task, 952bdc7f021STrond Myklebust .rpc_message = &msg, 95384115e1cSTrond Myklebust .callback_ops = call_ops, 95484115e1cSTrond Myklebust .callback_data = data, 955101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 9569f0ec176SAndy Adamson .flags = RPC_TASK_ASYNC | flags, 9573ff7576dSTrond Myklebust .priority = priority, 95884115e1cSTrond Myklebust }; 9592c61be0aSTrond Myklebust int ret = 0; 9601da177e4SLinus Torvalds 961d138d5d1SAndy Adamson /* Set up the initial task struct. */ 962d138d5d1SAndy Adamson NFS_PROTO(inode)->write_setup(data, &msg); 963d138d5d1SAndy Adamson 964d138d5d1SAndy Adamson dprintk("NFS: %5u initiated write call " 9651e8968c5SNiels de Vos "(req %s/%llu, %u bytes @ offset %llu)\n", 966d138d5d1SAndy Adamson data->task.tk_pid, 967d138d5d1SAndy Adamson inode->i_sb->s_id, 9681e8968c5SNiels de Vos (unsigned long long)NFS_FILEID(inode), 969d138d5d1SAndy Adamson data->args.count, 970d138d5d1SAndy Adamson (unsigned long long)data->args.offset); 971d138d5d1SAndy Adamson 9728c21c62cSWeston Andros Adamson nfs4_state_protect_write(NFS_SERVER(inode)->nfs_client, 9738c21c62cSWeston Andros Adamson &task_setup_data.rpc_client, &msg, data); 9748c21c62cSWeston Andros Adamson 975d138d5d1SAndy Adamson task = rpc_run_task(&task_setup_data); 976d138d5d1SAndy Adamson if (IS_ERR(task)) { 977d138d5d1SAndy Adamson ret = PTR_ERR(task); 978d138d5d1SAndy Adamson goto out; 979d138d5d1SAndy Adamson } 980d138d5d1SAndy Adamson if (how & FLUSH_SYNC) { 981d138d5d1SAndy Adamson ret = rpc_wait_for_completion_task(task); 982d138d5d1SAndy Adamson if (ret == 0) 983d138d5d1SAndy Adamson ret = task->tk_status; 984d138d5d1SAndy Adamson } 985d138d5d1SAndy Adamson rpc_put_task(task); 986d138d5d1SAndy Adamson out: 987d138d5d1SAndy Adamson return ret; 988d138d5d1SAndy Adamson } 989a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write); 990d138d5d1SAndy Adamson 991d138d5d1SAndy Adamson /* 992d138d5d1SAndy Adamson * Set up the argument/result storage required for the RPC call. 993d138d5d1SAndy Adamson */ 9949c7e1b3dSAnna Schumaker static void nfs_write_rpcsetup(struct nfs_pgio_data *data, 995d138d5d1SAndy Adamson unsigned int count, unsigned int offset, 996ea2cf228SFred Isaman int how, struct nfs_commit_info *cinfo) 997d138d5d1SAndy Adamson { 9986c75dc0dSFred Isaman struct nfs_page *req = data->header->req; 999d138d5d1SAndy Adamson 10001da177e4SLinus Torvalds /* Set up the RPC argument and reply structs 10011da177e4SLinus Torvalds * NB: take care not to mess about with data->commit et al. */ 10021da177e4SLinus Torvalds 10036c75dc0dSFred Isaman data->args.fh = NFS_FH(data->header->inode); 10041da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 10052bea038cSBoaz Harrosh /* pnfs_set_layoutcommit needs this */ 10062bea038cSBoaz Harrosh data->mds_offset = data->args.offset; 10071da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 100830dd374fSFred Isaman data->args.pages = data->pages.pagevec; 10091da177e4SLinus Torvalds data->args.count = count; 1010383ba719STrond Myklebust data->args.context = get_nfs_open_context(req->wb_context); 1011f11ac8dbSTrond Myklebust data->args.lock_context = req->wb_lock_context; 1012bdc7f021STrond Myklebust data->args.stable = NFS_UNSTABLE; 101387ed5eb4STrond Myklebust switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) { 101487ed5eb4STrond Myklebust case 0: 101587ed5eb4STrond Myklebust break; 101687ed5eb4STrond Myklebust case FLUSH_COND_STABLE: 1017ea2cf228SFred Isaman if (nfs_reqs_to_commit(cinfo)) 101887ed5eb4STrond Myklebust break; 101987ed5eb4STrond Myklebust default: 1020bdc7f021STrond Myklebust data->args.stable = NFS_FILE_SYNC; 1021bdc7f021STrond Myklebust } 10221da177e4SLinus Torvalds 10231da177e4SLinus Torvalds data->res.fattr = &data->fattr; 10241da177e4SLinus Torvalds data->res.count = count; 10251da177e4SLinus Torvalds data->res.verf = &data->verf; 10260e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 10276e4efd56STrond Myklebust } 10281da177e4SLinus Torvalds 10299c7e1b3dSAnna Schumaker static int nfs_do_write(struct nfs_pgio_data *data, 10306e4efd56STrond Myklebust const struct rpc_call_ops *call_ops, 10316e4efd56STrond Myklebust int how) 10326e4efd56STrond Myklebust { 1033cd841605SFred Isaman struct inode *inode = data->header->inode; 10340382b744SAndy Adamson 10359f0ec176SAndy Adamson return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how, 0); 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 1038275acaafSTrond Myklebust static int nfs_do_multiple_writes(struct list_head *head, 1039275acaafSTrond Myklebust const struct rpc_call_ops *call_ops, 1040275acaafSTrond Myklebust int how) 1041275acaafSTrond Myklebust { 10429c7e1b3dSAnna Schumaker struct nfs_pgio_data *data; 1043275acaafSTrond Myklebust int ret = 0; 1044275acaafSTrond Myklebust 1045275acaafSTrond Myklebust while (!list_empty(head)) { 1046275acaafSTrond Myklebust int ret2; 1047275acaafSTrond Myklebust 10489c7e1b3dSAnna Schumaker data = list_first_entry(head, struct nfs_pgio_data, list); 1049275acaafSTrond Myklebust list_del_init(&data->list); 1050275acaafSTrond Myklebust 1051dce81290STrond Myklebust ret2 = nfs_do_write(data, call_ops, how); 1052275acaafSTrond Myklebust if (ret == 0) 1053275acaafSTrond Myklebust ret = ret2; 1054275acaafSTrond Myklebust } 1055275acaafSTrond Myklebust return ret; 1056275acaafSTrond Myklebust } 1057275acaafSTrond Myklebust 10586d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and 10596d884e8fSFred * call this on each, which will prepare them to be retried on next 10606d884e8fSFred * writeback using standard nfs. 10616d884e8fSFred */ 10626d884e8fSFred static void nfs_redirty_request(struct nfs_page *req) 10636d884e8fSFred { 10646d884e8fSFred nfs_mark_request_dirty(req); 10651d1afcbcSTrond Myklebust nfs_unlock_request(req); 1066d1182b33STrond Myklebust nfs_end_page_writeback(req->wb_page); 10673aff4ebbSTrond Myklebust nfs_release_request(req); 10686d884e8fSFred } 10696d884e8fSFred 1070061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head) 10716c75dc0dSFred Isaman { 10726c75dc0dSFred Isaman struct nfs_page *req; 10736c75dc0dSFred Isaman 10746c75dc0dSFred Isaman while (!list_empty(head)) { 10756c75dc0dSFred Isaman req = nfs_list_entry(head->next); 10766c75dc0dSFred Isaman nfs_list_remove_request(req); 10776c75dc0dSFred Isaman nfs_redirty_request(req); 10786c75dc0dSFred Isaman } 10796c75dc0dSFred Isaman } 10806c75dc0dSFred Isaman 1081061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = { 1082061ae2edSFred Isaman .error_cleanup = nfs_async_write_error, 1083061ae2edSFred Isaman .completion = nfs_write_completion, 1084061ae2edSFred Isaman }; 1085061ae2edSFred Isaman 108625b11dcdSTrond Myklebust static void nfs_flush_error(struct nfs_pageio_descriptor *desc, 108725b11dcdSTrond Myklebust struct nfs_pgio_header *hdr) 108825b11dcdSTrond Myklebust { 108925b11dcdSTrond Myklebust set_bit(NFS_IOHDR_REDO, &hdr->flags); 109025b11dcdSTrond Myklebust while (!list_empty(&hdr->rpc_list)) { 10919c7e1b3dSAnna Schumaker struct nfs_pgio_data *data = list_first_entry(&hdr->rpc_list, 10929c7e1b3dSAnna Schumaker struct nfs_pgio_data, list); 109325b11dcdSTrond Myklebust list_del(&data->list); 109400bfa30aSAnna Schumaker nfs_pgio_data_release(data); 109525b11dcdSTrond Myklebust } 109625b11dcdSTrond Myklebust desc->pg_completion_ops->error_cleanup(&desc->pg_list); 109725b11dcdSTrond Myklebust } 109825b11dcdSTrond Myklebust 10991da177e4SLinus Torvalds /* 11001da177e4SLinus Torvalds * Generate multiple small requests to write out a single 11011da177e4SLinus Torvalds * contiguous dirty area on one page. 11021da177e4SLinus Torvalds */ 11036c75dc0dSFred Isaman static int nfs_flush_multi(struct nfs_pageio_descriptor *desc, 11046c75dc0dSFred Isaman struct nfs_pgio_header *hdr) 11051da177e4SLinus Torvalds { 11066c75dc0dSFred Isaman struct nfs_page *req = hdr->req; 11071da177e4SLinus Torvalds struct page *page = req->wb_page; 11089c7e1b3dSAnna Schumaker struct nfs_pgio_data *data; 1109d097971dSTrond Myklebust size_t wsize = desc->pg_bsize, nbytes; 1110e9f7bee1STrond Myklebust unsigned int offset; 11111da177e4SLinus Torvalds int requests = 0; 1112ea2cf228SFred Isaman struct nfs_commit_info cinfo; 11131da177e4SLinus Torvalds 1114ea2cf228SFred Isaman nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq); 11151da177e4SLinus Torvalds 1116b31268acSTrond Myklebust if ((desc->pg_ioflags & FLUSH_COND_STABLE) && 1117ea2cf228SFred Isaman (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) || 1118b31268acSTrond Myklebust desc->pg_count > wsize)) 1119b31268acSTrond Myklebust desc->pg_ioflags &= ~FLUSH_COND_STABLE; 1120b31268acSTrond Myklebust 1121b31268acSTrond Myklebust 1122275acaafSTrond Myklebust offset = 0; 1123c76069bdSFred Isaman nbytes = desc->pg_count; 1124e9f7bee1STrond Myklebust do { 1125e9f7bee1STrond Myklebust size_t len = min(nbytes, wsize); 1126e9f7bee1STrond Myklebust 112700bfa30aSAnna Schumaker data = nfs_pgio_data_alloc(hdr, 1); 112825b11dcdSTrond Myklebust if (!data) { 112925b11dcdSTrond Myklebust nfs_flush_error(desc, hdr); 113025b11dcdSTrond Myklebust return -ENOMEM; 113125b11dcdSTrond Myklebust } 113230dd374fSFred Isaman data->pages.pagevec[0] = page; 1133ea2cf228SFred Isaman nfs_write_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo); 11346c75dc0dSFred Isaman list_add(&data->list, &hdr->rpc_list); 11351da177e4SLinus Torvalds requests++; 1136e9f7bee1STrond Myklebust nbytes -= len; 1137275acaafSTrond Myklebust offset += len; 1138e9f7bee1STrond Myklebust } while (nbytes != 0); 113925b11dcdSTrond Myklebust nfs_list_remove_request(req); 114025b11dcdSTrond Myklebust nfs_list_add_request(req, &hdr->pages); 11416c75dc0dSFred Isaman desc->pg_rpc_callops = &nfs_write_common_ops; 114225b11dcdSTrond Myklebust return 0; 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds /* 11461da177e4SLinus Torvalds * Create an RPC task for the given write request and kick it. 11471da177e4SLinus Torvalds * The page must have been locked by the caller. 11481da177e4SLinus Torvalds * 11491da177e4SLinus Torvalds * It may happen that the page we're passed is not marked dirty. 11501da177e4SLinus Torvalds * This is the case if nfs_updatepage detects a conflicting request 11511da177e4SLinus Torvalds * that has been written but not committed. 11521da177e4SLinus Torvalds */ 11536c75dc0dSFred Isaman static int nfs_flush_one(struct nfs_pageio_descriptor *desc, 11546c75dc0dSFred Isaman struct nfs_pgio_header *hdr) 11551da177e4SLinus Torvalds { 11561da177e4SLinus Torvalds struct nfs_page *req; 11571da177e4SLinus Torvalds struct page **pages; 11589c7e1b3dSAnna Schumaker struct nfs_pgio_data *data; 1159c76069bdSFred Isaman struct list_head *head = &desc->pg_list; 1160ea2cf228SFred Isaman struct nfs_commit_info cinfo; 11611da177e4SLinus Torvalds 116200bfa30aSAnna Schumaker data = nfs_pgio_data_alloc(hdr, nfs_page_array_len(desc->pg_base, 1163c76069bdSFred Isaman desc->pg_count)); 11646c75dc0dSFred Isaman if (!data) { 116525b11dcdSTrond Myklebust nfs_flush_error(desc, hdr); 116625b11dcdSTrond Myklebust return -ENOMEM; 116744b83799SFred Isaman } 11686c75dc0dSFred Isaman 1169ea2cf228SFred Isaman nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq); 117030dd374fSFred Isaman pages = data->pages.pagevec; 11711da177e4SLinus Torvalds while (!list_empty(head)) { 11721da177e4SLinus Torvalds req = nfs_list_entry(head->next); 11731da177e4SLinus Torvalds nfs_list_remove_request(req); 11746c75dc0dSFred Isaman nfs_list_add_request(req, &hdr->pages); 11751da177e4SLinus Torvalds *pages++ = req->wb_page; 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds 1178b31268acSTrond Myklebust if ((desc->pg_ioflags & FLUSH_COND_STABLE) && 1179ea2cf228SFred Isaman (desc->pg_moreio || nfs_reqs_to_commit(&cinfo))) 1180b31268acSTrond Myklebust desc->pg_ioflags &= ~FLUSH_COND_STABLE; 1181b31268acSTrond Myklebust 11821da177e4SLinus Torvalds /* Set up the argument struct */ 1183ea2cf228SFred Isaman nfs_write_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo); 11846c75dc0dSFred Isaman list_add(&data->list, &hdr->rpc_list); 11856c75dc0dSFred Isaman desc->pg_rpc_callops = &nfs_write_common_ops; 118625b11dcdSTrond Myklebust return 0; 11871da177e4SLinus Torvalds } 11881da177e4SLinus Torvalds 11896c75dc0dSFred Isaman int nfs_generic_flush(struct nfs_pageio_descriptor *desc, 11906c75dc0dSFred Isaman struct nfs_pgio_header *hdr) 1191dce81290STrond Myklebust { 1192dce81290STrond Myklebust if (desc->pg_bsize < PAGE_CACHE_SIZE) 11936c75dc0dSFred Isaman return nfs_flush_multi(desc, hdr); 11946c75dc0dSFred Isaman return nfs_flush_one(desc, hdr); 1195dce81290STrond Myklebust } 119689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_generic_flush); 1197dce81290STrond Myklebust 1198dce81290STrond Myklebust static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc) 11991751c363STrond Myklebust { 1200c0752cdfSAnna Schumaker struct nfs_rw_header *whdr; 12016c75dc0dSFred Isaman struct nfs_pgio_header *hdr; 1202275acaafSTrond Myklebust int ret; 1203275acaafSTrond Myklebust 12044a0de55cSAnna Schumaker whdr = nfs_rw_header_alloc(desc->pg_rw_ops); 12056c75dc0dSFred Isaman if (!whdr) { 12069b5415b5STrond Myklebust desc->pg_completion_ops->error_cleanup(&desc->pg_list); 12076c75dc0dSFred Isaman return -ENOMEM; 12086c75dc0dSFred Isaman } 12096c75dc0dSFred Isaman hdr = &whdr->header; 12104a0de55cSAnna Schumaker nfs_pgheader_init(desc, hdr, nfs_rw_header_free); 12116c75dc0dSFred Isaman atomic_inc(&hdr->refcnt); 12126c75dc0dSFred Isaman ret = nfs_generic_flush(desc, hdr); 1213275acaafSTrond Myklebust if (ret == 0) 12146c75dc0dSFred Isaman ret = nfs_do_multiple_writes(&hdr->rpc_list, 12156c75dc0dSFred Isaman desc->pg_rpc_callops, 1216dce81290STrond Myklebust desc->pg_ioflags); 12176c75dc0dSFred Isaman if (atomic_dec_and_test(&hdr->refcnt)) 1218061ae2edSFred Isaman hdr->completion_ops->completion(hdr); 1219275acaafSTrond Myklebust return ret; 12201751c363STrond Myklebust } 12211751c363STrond Myklebust 12221751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_write_ops = { 12231751c363STrond Myklebust .pg_test = nfs_generic_pg_test, 12241751c363STrond Myklebust .pg_doio = nfs_generic_pg_writepages, 12251751c363STrond Myklebust }; 12261751c363STrond Myklebust 122757208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, 1228a20c93e3SChristoph Hellwig struct inode *inode, int ioflags, bool force_mds, 1229061ae2edSFred Isaman const struct nfs_pgio_completion_ops *compl_ops) 12301751c363STrond Myklebust { 1231a20c93e3SChristoph Hellwig struct nfs_server *server = NFS_SERVER(inode); 1232a20c93e3SChristoph Hellwig const struct nfs_pageio_ops *pg_ops = &nfs_pageio_write_ops; 1233a20c93e3SChristoph Hellwig 1234a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1 1235a20c93e3SChristoph Hellwig if (server->pnfs_curr_ld && !force_mds) 1236a20c93e3SChristoph Hellwig pg_ops = server->pnfs_curr_ld->pg_write_ops; 1237a20c93e3SChristoph Hellwig #endif 12384a0de55cSAnna Schumaker nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops, 12394a0de55cSAnna Schumaker server->wsize, ioflags); 12401751c363STrond Myklebust } 1241ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write); 12421751c363STrond Myklebust 1243dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio) 1244dce81290STrond Myklebust { 1245dce81290STrond Myklebust pgio->pg_ops = &nfs_pageio_write_ops; 1246dce81290STrond Myklebust pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize; 1247dce81290STrond Myklebust } 12481f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds); 1249dce81290STrond Myklebust 12501da177e4SLinus Torvalds 12510b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata) 12520b7c0153SFred Isaman { 12530b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 12540b7c0153SFred Isaman 12550b7c0153SFred Isaman NFS_PROTO(data->inode)->commit_rpc_prepare(task, data); 12560b7c0153SFred Isaman } 12570b7c0153SFred Isaman 1258a4cdda59SAnna Schumaker static void nfs_writeback_release_common(struct nfs_pgio_data *data) 1259c9d8f89dSTrond Myklebust { 1260cd841605SFred Isaman struct nfs_pgio_header *hdr = data->header; 1261e2fecb21STrond Myklebust int status = data->task.tk_status; 1262788e7a89STrond Myklebust 12636c75dc0dSFred Isaman if ((status >= 0) && nfs_write_need_commit(data)) { 12646c75dc0dSFred Isaman spin_lock(&hdr->lock); 12656c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) 12666c75dc0dSFred Isaman ; /* Do nothing */ 12676c75dc0dSFred Isaman else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) 1268f79d06f5SAnna Schumaker memcpy(&hdr->verf, &data->verf, sizeof(hdr->verf)); 1269f79d06f5SAnna Schumaker else if (memcmp(&hdr->verf, &data->verf, sizeof(hdr->verf))) 12706c75dc0dSFred Isaman set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags); 12716c75dc0dSFred Isaman spin_unlock(&hdr->lock); 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds } 12741da177e4SLinus Torvalds 12756c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops = { 1276a4cdda59SAnna Schumaker .rpc_call_prepare = nfs_pgio_prepare, 1277*0eecb214SAnna Schumaker .rpc_call_done = nfs_pgio_result, 1278a4cdda59SAnna Schumaker .rpc_release = nfs_pgio_release, 1279788e7a89STrond Myklebust }; 1280788e7a89STrond Myklebust 12811f2edbe3STrond Myklebust /* 12821f2edbe3STrond Myklebust * Special version of should_remove_suid() that ignores capabilities. 12831f2edbe3STrond Myklebust */ 12841f2edbe3STrond Myklebust static int nfs_should_remove_suid(const struct inode *inode) 12851f2edbe3STrond Myklebust { 12861f2edbe3STrond Myklebust umode_t mode = inode->i_mode; 12871f2edbe3STrond Myklebust int kill = 0; 12881f2edbe3STrond Myklebust 12891f2edbe3STrond Myklebust /* suid always must be killed */ 12901f2edbe3STrond Myklebust if (unlikely(mode & S_ISUID)) 12911f2edbe3STrond Myklebust kill = ATTR_KILL_SUID; 12921f2edbe3STrond Myklebust 12931f2edbe3STrond Myklebust /* 12941f2edbe3STrond Myklebust * sgid without any exec bits is just a mandatory locking mark; leave 12951f2edbe3STrond Myklebust * it alone. If some exec bits are set, it's a real sgid; kill it. 12961f2edbe3STrond Myklebust */ 12971f2edbe3STrond Myklebust if (unlikely((mode & S_ISGID) && (mode & S_IXGRP))) 12981f2edbe3STrond Myklebust kill |= ATTR_KILL_SGID; 12991f2edbe3STrond Myklebust 13001f2edbe3STrond Myklebust if (unlikely(kill && S_ISREG(mode))) 13011f2edbe3STrond Myklebust return kill; 13021f2edbe3STrond Myklebust 13031f2edbe3STrond Myklebust return 0; 13041f2edbe3STrond Myklebust } 1305788e7a89STrond Myklebust 13061da177e4SLinus Torvalds /* 13071da177e4SLinus Torvalds * This function is called when the WRITE call is complete. 13081da177e4SLinus Torvalds */ 1309*0eecb214SAnna Schumaker static int nfs_writeback_done(struct rpc_task *task, struct nfs_pgio_data *data, 1310*0eecb214SAnna Schumaker struct inode *inode) 13111da177e4SLinus Torvalds { 1312788e7a89STrond Myklebust int status; 13131da177e4SLinus Torvalds 1314f551e44fSChuck Lever /* 1315f551e44fSChuck Lever * ->write_done will attempt to use post-op attributes to detect 1316f551e44fSChuck Lever * conflicting writes by other clients. A strict interpretation 1317f551e44fSChuck Lever * of close-to-open would allow us to continue caching even if 1318f551e44fSChuck Lever * another writer had changed the file, but some applications 1319f551e44fSChuck Lever * depend on tighter cache coherency when writing. 1320f551e44fSChuck Lever */ 1321cd841605SFred Isaman status = NFS_PROTO(inode)->write_done(task, data); 1322788e7a89STrond Myklebust if (status != 0) 1323*0eecb214SAnna Schumaker return status; 1324*0eecb214SAnna Schumaker nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, data->res.count); 132591d5b470SChuck Lever 132689d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4) 1327*0eecb214SAnna Schumaker if (data->res.verf->committed < data->args.stable && task->tk_status >= 0) { 13281da177e4SLinus Torvalds /* We tried a write call, but the server did not 13291da177e4SLinus Torvalds * commit data to stable storage even though we 13301da177e4SLinus Torvalds * requested it. 13311da177e4SLinus Torvalds * Note: There is a known bug in Tru64 < 5.0 in which 13321da177e4SLinus Torvalds * the server reports NFS_DATA_SYNC, but performs 13331da177e4SLinus Torvalds * NFS_FILE_SYNC. We therefore implement this checking 13341da177e4SLinus Torvalds * as a dprintk() in order to avoid filling syslog. 13351da177e4SLinus Torvalds */ 13361da177e4SLinus Torvalds static unsigned long complain; 13371da177e4SLinus Torvalds 1338a69aef14SFred Isaman /* Note this will print the MDS for a DS write */ 13391da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 13401da177e4SLinus Torvalds dprintk("NFS: faulty NFS server %s:" 13411da177e4SLinus Torvalds " (committed = %d) != (stable = %d)\n", 1342cd841605SFred Isaman NFS_SERVER(inode)->nfs_client->cl_hostname, 1343*0eecb214SAnna Schumaker data->res.verf->committed, data->args.stable); 13441da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds #endif 13481f2edbe3STrond Myklebust 13491f2edbe3STrond Myklebust /* Deal with the suid/sgid bit corner case */ 13501f2edbe3STrond Myklebust if (nfs_should_remove_suid(inode)) 13511f2edbe3STrond Myklebust nfs_mark_for_revalidate(inode); 1352*0eecb214SAnna Schumaker return 0; 1353*0eecb214SAnna Schumaker } 1354*0eecb214SAnna Schumaker 1355*0eecb214SAnna Schumaker /* 1356*0eecb214SAnna Schumaker * This function is called when the WRITE call is complete. 1357*0eecb214SAnna Schumaker */ 1358*0eecb214SAnna Schumaker static void nfs_writeback_result(struct rpc_task *task, struct nfs_pgio_data *data) 1359*0eecb214SAnna Schumaker { 1360*0eecb214SAnna Schumaker struct nfs_pgio_args *argp = &data->args; 1361*0eecb214SAnna Schumaker struct nfs_pgio_res *resp = &data->res; 13621f2edbe3STrond Myklebust 13631f2edbe3STrond Myklebust if (resp->count < argp->count) { 13641da177e4SLinus Torvalds static unsigned long complain; 13651da177e4SLinus Torvalds 13666c75dc0dSFred Isaman /* This a short write! */ 1367*0eecb214SAnna Schumaker nfs_inc_stats(data->header->inode, NFSIOS_SHORTWRITE); 136891d5b470SChuck Lever 13691da177e4SLinus Torvalds /* Has the server at least made some progress? */ 13706c75dc0dSFred Isaman if (resp->count == 0) { 13716c75dc0dSFred Isaman if (time_before(complain, jiffies)) { 13726c75dc0dSFred Isaman printk(KERN_WARNING 13736c75dc0dSFred Isaman "NFS: Server wrote zero bytes, expected %u.\n", 13746c75dc0dSFred Isaman argp->count); 13756c75dc0dSFred Isaman complain = jiffies + 300 * HZ; 13766c75dc0dSFred Isaman } 13776c75dc0dSFred Isaman nfs_set_pgio_error(data->header, -EIO, argp->offset); 13786c75dc0dSFred Isaman task->tk_status = -EIO; 13796c75dc0dSFred Isaman return; 13806c75dc0dSFred Isaman } 13811da177e4SLinus Torvalds /* Was this an NFSv2 write or an NFSv3 stable write? */ 13821da177e4SLinus Torvalds if (resp->verf->committed != NFS_UNSTABLE) { 13831da177e4SLinus Torvalds /* Resend from where the server left off */ 1384a69aef14SFred Isaman data->mds_offset += resp->count; 13851da177e4SLinus Torvalds argp->offset += resp->count; 13861da177e4SLinus Torvalds argp->pgbase += resp->count; 13871da177e4SLinus Torvalds argp->count -= resp->count; 13881da177e4SLinus Torvalds } else { 13891da177e4SLinus Torvalds /* Resend as a stable write in order to avoid 13901da177e4SLinus Torvalds * headaches in the case of a server crash. 13911da177e4SLinus Torvalds */ 13921da177e4SLinus Torvalds argp->stable = NFS_FILE_SYNC; 13931da177e4SLinus Torvalds } 1394d00c5d43STrond Myklebust rpc_restart_call_prepare(task); 13951da177e4SLinus Torvalds } 13961da177e4SLinus Torvalds } 13971da177e4SLinus Torvalds 13981da177e4SLinus Torvalds 139989d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4) 140071d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait) 140171d0a611STrond Myklebust { 1402b8413f98STrond Myklebust int ret; 1403b8413f98STrond Myklebust 140471d0a611STrond Myklebust if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags)) 140571d0a611STrond Myklebust return 1; 1406b8413f98STrond Myklebust if (!may_wait) 140771d0a611STrond Myklebust return 0; 1408b8413f98STrond Myklebust ret = out_of_line_wait_on_bit_lock(&nfsi->flags, 1409b8413f98STrond Myklebust NFS_INO_COMMIT, 1410b8413f98STrond Myklebust nfs_wait_bit_killable, 1411b8413f98STrond Myklebust TASK_KILLABLE); 1412b8413f98STrond Myklebust return (ret < 0) ? ret : 1; 141371d0a611STrond Myklebust } 141471d0a611STrond Myklebust 1415f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi) 141671d0a611STrond Myklebust { 141771d0a611STrond Myklebust clear_bit(NFS_INO_COMMIT, &nfsi->flags); 141871d0a611STrond Myklebust smp_mb__after_clear_bit(); 141971d0a611STrond Myklebust wake_up_bit(&nfsi->flags, NFS_INO_COMMIT); 142071d0a611STrond Myklebust } 142171d0a611STrond Myklebust 14220b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data) 14231da177e4SLinus Torvalds { 14240b7c0153SFred Isaman put_nfs_open_context(data->context); 14250b7c0153SFred Isaman nfs_commit_free(data); 14261da177e4SLinus Torvalds } 1427e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release); 14281da177e4SLinus Torvalds 14290b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data, 14309ace33cdSFred Isaman const struct rpc_call_ops *call_ops, 14319f0ec176SAndy Adamson int how, int flags) 14321da177e4SLinus Torvalds { 143307737691STrond Myklebust struct rpc_task *task; 14349ace33cdSFred Isaman int priority = flush_task_priority(how); 1435bdc7f021STrond Myklebust struct rpc_message msg = { 1436bdc7f021STrond Myklebust .rpc_argp = &data->args, 1437bdc7f021STrond Myklebust .rpc_resp = &data->res, 14389ace33cdSFred Isaman .rpc_cred = data->cred, 1439bdc7f021STrond Myklebust }; 144084115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 144107737691STrond Myklebust .task = &data->task, 14429ace33cdSFred Isaman .rpc_client = clnt, 1443bdc7f021STrond Myklebust .rpc_message = &msg, 14449ace33cdSFred Isaman .callback_ops = call_ops, 144584115e1cSTrond Myklebust .callback_data = data, 1446101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 14479f0ec176SAndy Adamson .flags = RPC_TASK_ASYNC | flags, 14483ff7576dSTrond Myklebust .priority = priority, 144984115e1cSTrond Myklebust }; 1450788e7a89STrond Myklebust /* Set up the initial task struct. */ 14519ace33cdSFred Isaman NFS_PROTO(data->inode)->commit_setup(data, &msg); 14521da177e4SLinus Torvalds 1453a3f565b1SChuck Lever dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); 1454bdc7f021STrond Myklebust 14558c21c62cSWeston Andros Adamson nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client, 14568c21c62cSWeston Andros Adamson NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg); 14578c21c62cSWeston Andros Adamson 145807737691STrond Myklebust task = rpc_run_task(&task_setup_data); 1459dbae4c73STrond Myklebust if (IS_ERR(task)) 1460dbae4c73STrond Myklebust return PTR_ERR(task); 1461d2224e7aSJeff Layton if (how & FLUSH_SYNC) 1462d2224e7aSJeff Layton rpc_wait_for_completion_task(task); 146307737691STrond Myklebust rpc_put_task(task); 1464dbae4c73STrond Myklebust return 0; 14651da177e4SLinus Torvalds } 1466e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit); 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds /* 14699ace33cdSFred Isaman * Set up the argument/result storage required for the RPC call. 14709ace33cdSFred Isaman */ 14710b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data, 1472988b6dceSFred Isaman struct list_head *head, 1473f453a54aSFred Isaman struct pnfs_layout_segment *lseg, 1474f453a54aSFred Isaman struct nfs_commit_info *cinfo) 14759ace33cdSFred Isaman { 14769ace33cdSFred Isaman struct nfs_page *first = nfs_list_entry(head->next); 14773d4ff43dSAl Viro struct inode *inode = first->wb_context->dentry->d_inode; 14789ace33cdSFred Isaman 14799ace33cdSFred Isaman /* Set up the RPC argument and reply structs 14809ace33cdSFred Isaman * NB: take care not to mess about with data->commit et al. */ 14819ace33cdSFred Isaman 14829ace33cdSFred Isaman list_splice_init(head, &data->pages); 14839ace33cdSFred Isaman 14849ace33cdSFred Isaman data->inode = inode; 14859ace33cdSFred Isaman data->cred = first->wb_context->cred; 1486988b6dceSFred Isaman data->lseg = lseg; /* reference transferred */ 14879ace33cdSFred Isaman data->mds_ops = &nfs_commit_ops; 1488f453a54aSFred Isaman data->completion_ops = cinfo->completion_ops; 1489b359f9d0SFred Isaman data->dreq = cinfo->dreq; 14909ace33cdSFred Isaman 14919ace33cdSFred Isaman data->args.fh = NFS_FH(data->inode); 14929ace33cdSFred Isaman /* Note: we always request a commit of the entire inode */ 14939ace33cdSFred Isaman data->args.offset = 0; 14949ace33cdSFred Isaman data->args.count = 0; 14950b7c0153SFred Isaman data->context = get_nfs_open_context(first->wb_context); 14969ace33cdSFred Isaman data->res.fattr = &data->fattr; 14979ace33cdSFred Isaman data->res.verf = &data->verf; 14989ace33cdSFred Isaman nfs_fattr_init(&data->fattr); 14999ace33cdSFred Isaman } 1500e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit); 15019ace33cdSFred Isaman 1502e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list, 1503ea2cf228SFred Isaman struct pnfs_layout_segment *lseg, 1504ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 150564bfeb49SFred Isaman { 150664bfeb49SFred Isaman struct nfs_page *req; 150764bfeb49SFred Isaman 150864bfeb49SFred Isaman while (!list_empty(page_list)) { 150964bfeb49SFred Isaman req = nfs_list_entry(page_list->next); 151064bfeb49SFred Isaman nfs_list_remove_request(req); 1511ea2cf228SFred Isaman nfs_mark_request_commit(req, lseg, cinfo); 151256f9cd68SFred Isaman if (!cinfo->dreq) { 151364bfeb49SFred Isaman dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 1514d56b4ddfSMel Gorman dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info, 151564bfeb49SFred Isaman BDI_RECLAIMABLE); 151656f9cd68SFred Isaman } 15171d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 151864bfeb49SFred Isaman } 151964bfeb49SFred Isaman } 1520e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit); 152164bfeb49SFred Isaman 15229ace33cdSFred Isaman /* 15231da177e4SLinus Torvalds * Commit dirty pages 15241da177e4SLinus Torvalds */ 15251da177e4SLinus Torvalds static int 1526ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how, 1527ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 15281da177e4SLinus Torvalds { 15290b7c0153SFred Isaman struct nfs_commit_data *data; 15301da177e4SLinus Torvalds 1531c9d8f89dSTrond Myklebust data = nfs_commitdata_alloc(); 15321da177e4SLinus Torvalds 15331da177e4SLinus Torvalds if (!data) 15341da177e4SLinus Torvalds goto out_bad; 15351da177e4SLinus Torvalds 15361da177e4SLinus Torvalds /* Set up the argument struct */ 1537f453a54aSFred Isaman nfs_init_commit(data, head, NULL, cinfo); 1538f453a54aSFred Isaman atomic_inc(&cinfo->mds->rpcs_out); 15399f0ec176SAndy Adamson return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops, 15409f0ec176SAndy Adamson how, 0); 15411da177e4SLinus Torvalds out_bad: 1542ea2cf228SFred Isaman nfs_retry_commit(head, NULL, cinfo); 1543f453a54aSFred Isaman cinfo->completion_ops->error_cleanup(NFS_I(inode)); 15441da177e4SLinus Torvalds return -ENOMEM; 15451da177e4SLinus Torvalds } 15461da177e4SLinus Torvalds 15471da177e4SLinus Torvalds /* 15481da177e4SLinus Torvalds * COMMIT call returned 15491da177e4SLinus Torvalds */ 1550788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata) 15511da177e4SLinus Torvalds { 15520b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 15531da177e4SLinus Torvalds 1554a3f565b1SChuck Lever dprintk("NFS: %5u nfs_commit_done (status %d)\n", 15551da177e4SLinus Torvalds task->tk_pid, task->tk_status); 15561da177e4SLinus Torvalds 1557788e7a89STrond Myklebust /* Call the NFS version-specific code */ 1558c0d0e96bSTrond Myklebust NFS_PROTO(data->inode)->commit_done(task, data); 1559c9d8f89dSTrond Myklebust } 1560c9d8f89dSTrond Myklebust 1561f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data) 1562c9d8f89dSTrond Myklebust { 1563c9d8f89dSTrond Myklebust struct nfs_page *req; 1564c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1565f453a54aSFred Isaman struct nfs_commit_info cinfo; 1566788e7a89STrond Myklebust 15671da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 15681da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 15691da177e4SLinus Torvalds nfs_list_remove_request(req); 1570d6d6dc7cSFred Isaman nfs_clear_page_commit(req->wb_page); 15711da177e4SLinus Torvalds 15721e8968c5SNiels de Vos dprintk("NFS: commit (%s/%llu %d@%lld)", 15733d4ff43dSAl Viro req->wb_context->dentry->d_sb->s_id, 15741e8968c5SNiels de Vos (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode), 15751da177e4SLinus Torvalds req->wb_bytes, 15761da177e4SLinus Torvalds (long long)req_offset(req)); 1577c9d8f89dSTrond Myklebust if (status < 0) { 1578c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 15791da177e4SLinus Torvalds nfs_inode_remove_request(req); 1580c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 15811da177e4SLinus Torvalds goto next; 15821da177e4SLinus Torvalds } 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds /* Okay, COMMIT succeeded, apparently. Check the verifier 15851da177e4SLinus Torvalds * returned by the server against all stored verfs. */ 15862f2c63bcSTrond Myklebust if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) { 15871da177e4SLinus Torvalds /* We have a match */ 15881da177e4SLinus Torvalds nfs_inode_remove_request(req); 15891da177e4SLinus Torvalds dprintk(" OK\n"); 15901da177e4SLinus Torvalds goto next; 15911da177e4SLinus Torvalds } 15921da177e4SLinus Torvalds /* We have a mismatch. Write the page again */ 15931da177e4SLinus Torvalds dprintk(" mismatch\n"); 15946d884e8fSFred nfs_mark_request_dirty(req); 159505990d1bSTrond Myklebust set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags); 15961da177e4SLinus Torvalds next: 15971d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 15981da177e4SLinus Torvalds } 1599f453a54aSFred Isaman nfs_init_cinfo(&cinfo, data->inode, data->dreq); 1600f453a54aSFred Isaman if (atomic_dec_and_test(&cinfo.mds->rpcs_out)) 1601f453a54aSFred Isaman nfs_commit_clear_lock(NFS_I(data->inode)); 16025917ce84SFred Isaman } 16035917ce84SFred Isaman 16045917ce84SFred Isaman static void nfs_commit_release(void *calldata) 16055917ce84SFred Isaman { 16060b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 16075917ce84SFred Isaman 1608f453a54aSFred Isaman data->completion_ops->completion(data); 1609c9d8f89dSTrond Myklebust nfs_commitdata_release(calldata); 16101da177e4SLinus Torvalds } 1611788e7a89STrond Myklebust 1612788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = { 16130b7c0153SFred Isaman .rpc_call_prepare = nfs_commit_prepare, 1614788e7a89STrond Myklebust .rpc_call_done = nfs_commit_done, 1615788e7a89STrond Myklebust .rpc_release = nfs_commit_release, 1616788e7a89STrond Myklebust }; 16171da177e4SLinus Torvalds 1618f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = { 1619f453a54aSFred Isaman .completion = nfs_commit_release_pages, 1620f453a54aSFred Isaman .error_cleanup = nfs_commit_clear_lock, 1621f453a54aSFred Isaman }; 1622f453a54aSFred Isaman 16231763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head, 1624ea2cf228SFred Isaman int how, struct nfs_commit_info *cinfo) 162584c53ab5SFred Isaman { 162684c53ab5SFred Isaman int status; 162784c53ab5SFred Isaman 1628ea2cf228SFred Isaman status = pnfs_commit_list(inode, head, how, cinfo); 162984c53ab5SFred Isaman if (status == PNFS_NOT_ATTEMPTED) 1630ea2cf228SFred Isaman status = nfs_commit_list(inode, head, how, cinfo); 163184c53ab5SFred Isaman return status; 163284c53ab5SFred Isaman } 163384c53ab5SFred Isaman 1634b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how) 16351da177e4SLinus Torvalds { 16361da177e4SLinus Torvalds LIST_HEAD(head); 1637ea2cf228SFred Isaman struct nfs_commit_info cinfo; 163871d0a611STrond Myklebust int may_wait = how & FLUSH_SYNC; 1639b8413f98STrond Myklebust int res; 16401da177e4SLinus Torvalds 1641b8413f98STrond Myklebust res = nfs_commit_set_lock(NFS_I(inode), may_wait); 1642b8413f98STrond Myklebust if (res <= 0) 1643c5efa5fcSTrond Myklebust goto out_mark_dirty; 1644ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, inode); 1645ea2cf228SFred Isaman res = nfs_scan_commit(inode, &head, &cinfo); 16461da177e4SLinus Torvalds if (res) { 1647a861a1e1SFred Isaman int error; 1648a861a1e1SFred Isaman 1649ea2cf228SFred Isaman error = nfs_generic_commit_list(inode, &head, how, &cinfo); 16501da177e4SLinus Torvalds if (error < 0) 16511da177e4SLinus Torvalds return error; 1652b8413f98STrond Myklebust if (!may_wait) 1653b8413f98STrond Myklebust goto out_mark_dirty; 1654b8413f98STrond Myklebust error = wait_on_bit(&NFS_I(inode)->flags, 1655b8413f98STrond Myklebust NFS_INO_COMMIT, 165671d0a611STrond Myklebust nfs_wait_bit_killable, 165771d0a611STrond Myklebust TASK_KILLABLE); 1658b8413f98STrond Myklebust if (error < 0) 1659b8413f98STrond Myklebust return error; 166071d0a611STrond Myklebust } else 166171d0a611STrond Myklebust nfs_commit_clear_lock(NFS_I(inode)); 1662c5efa5fcSTrond Myklebust return res; 1663c5efa5fcSTrond Myklebust /* Note: If we exit without ensuring that the commit is complete, 1664c5efa5fcSTrond Myklebust * we must mark the inode as dirty. Otherwise, future calls to 1665c5efa5fcSTrond Myklebust * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure 1666c5efa5fcSTrond Myklebust * that the data is on the disk. 1667c5efa5fcSTrond Myklebust */ 1668c5efa5fcSTrond Myklebust out_mark_dirty: 1669c5efa5fcSTrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 16701da177e4SLinus Torvalds return res; 16711da177e4SLinus Torvalds } 16728fc795f7STrond Myklebust 16738fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 16748fc795f7STrond Myklebust { 1675420e3646STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 1676420e3646STrond Myklebust int flags = FLUSH_SYNC; 1677420e3646STrond Myklebust int ret = 0; 16788fc795f7STrond Myklebust 16793236c3e1SJeff Layton /* no commits means nothing needs to be done */ 1680ea2cf228SFred Isaman if (!nfsi->commit_info.ncommit) 16813236c3e1SJeff Layton return ret; 16823236c3e1SJeff Layton 1683a00dd6c0SJeff Layton if (wbc->sync_mode == WB_SYNC_NONE) { 1684a00dd6c0SJeff Layton /* Don't commit yet if this is a non-blocking flush and there 1685a00dd6c0SJeff Layton * are a lot of outstanding writes for this mapping. 1686420e3646STrond Myklebust */ 1687ea2cf228SFred Isaman if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1)) 1688420e3646STrond Myklebust goto out_mark_dirty; 1689420e3646STrond Myklebust 1690a00dd6c0SJeff Layton /* don't wait for the COMMIT response */ 1691420e3646STrond Myklebust flags = 0; 1692a00dd6c0SJeff Layton } 1693a00dd6c0SJeff Layton 1694420e3646STrond Myklebust ret = nfs_commit_inode(inode, flags); 1695420e3646STrond Myklebust if (ret >= 0) { 1696420e3646STrond Myklebust if (wbc->sync_mode == WB_SYNC_NONE) { 1697420e3646STrond Myklebust if (ret < wbc->nr_to_write) 1698420e3646STrond Myklebust wbc->nr_to_write -= ret; 1699420e3646STrond Myklebust else 1700420e3646STrond Myklebust wbc->nr_to_write = 0; 1701420e3646STrond Myklebust } 17028fc795f7STrond Myklebust return 0; 1703420e3646STrond Myklebust } 1704420e3646STrond Myklebust out_mark_dirty: 17058fc795f7STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 17068fc795f7STrond Myklebust return ret; 17078fc795f7STrond Myklebust } 1708c63c7b05STrond Myklebust #else 17098fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 17108fc795f7STrond Myklebust { 17118fc795f7STrond Myklebust return 0; 17128fc795f7STrond Myklebust } 17131da177e4SLinus Torvalds #endif 17141da177e4SLinus Torvalds 17158fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc) 17168fc795f7STrond Myklebust { 1717a8d8f02cSBryan Schumaker return nfs_commit_unstable_pages(inode, wbc); 1718a8d8f02cSBryan Schumaker } 171989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode); 1720863a3c6cSAndy Adamson 1721acdc53b2STrond Myklebust /* 1722acdc53b2STrond Myklebust * flush the inode to disk. 1723acdc53b2STrond Myklebust */ 1724acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode) 172534901f70STrond Myklebust { 172634901f70STrond Myklebust struct writeback_control wbc = { 172772cb77f4STrond Myklebust .sync_mode = WB_SYNC_ALL, 172834901f70STrond Myklebust .nr_to_write = LONG_MAX, 1729d7fb1207STrond Myklebust .range_start = 0, 1730d7fb1207STrond Myklebust .range_end = LLONG_MAX, 173134901f70STrond Myklebust }; 1732f4ce1299STrond Myklebust int ret; 173334901f70STrond Myklebust 1734f4ce1299STrond Myklebust trace_nfs_writeback_inode_enter(inode); 1735f4ce1299STrond Myklebust 1736f4ce1299STrond Myklebust ret = sync_inode(inode, &wbc); 1737f4ce1299STrond Myklebust 1738f4ce1299STrond Myklebust trace_nfs_writeback_inode_exit(inode, ret); 1739f4ce1299STrond Myklebust return ret; 17401c75950bSTrond Myklebust } 1741ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all); 17421c75950bSTrond Myklebust 17431b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page) 17441b3b4a1aSTrond Myklebust { 17451b3b4a1aSTrond Myklebust struct nfs_page *req; 17461b3b4a1aSTrond Myklebust int ret = 0; 17471b3b4a1aSTrond Myklebust 17481b3b4a1aSTrond Myklebust for (;;) { 1749ba8b06e6STrond Myklebust wait_on_page_writeback(page); 17501b3b4a1aSTrond Myklebust req = nfs_page_find_request(page); 17511b3b4a1aSTrond Myklebust if (req == NULL) 17521b3b4a1aSTrond Myklebust break; 17537ad84aa9STrond Myklebust if (nfs_lock_request(req)) { 17548dd37758STrond Myklebust nfs_clear_request_commit(req); 17551b3b4a1aSTrond Myklebust nfs_inode_remove_request(req); 17561b3b4a1aSTrond Myklebust /* 17571b3b4a1aSTrond Myklebust * In case nfs_inode_remove_request has marked the 17581b3b4a1aSTrond Myklebust * page as being dirty 17591b3b4a1aSTrond Myklebust */ 17601b3b4a1aSTrond Myklebust cancel_dirty_page(page, PAGE_CACHE_SIZE); 17611d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 17621b3b4a1aSTrond Myklebust break; 17631b3b4a1aSTrond Myklebust } 17641b3b4a1aSTrond Myklebust ret = nfs_wait_on_request(req); 1765c9edda71STrond Myklebust nfs_release_request(req); 17661b3b4a1aSTrond Myklebust if (ret < 0) 1767c988950eSTrond Myklebust break; 17681b3b4a1aSTrond Myklebust } 17691b3b4a1aSTrond Myklebust return ret; 17701b3b4a1aSTrond Myklebust } 17711b3b4a1aSTrond Myklebust 17721c75950bSTrond Myklebust /* 17731c75950bSTrond Myklebust * Write back all requests on one page - we do this before reading it. 17741c75950bSTrond Myklebust */ 17751c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page) 17761c75950bSTrond Myklebust { 177729418aa4SMel Gorman loff_t range_start = page_file_offset(page); 17787f2f12d9STrond Myklebust loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); 17797f2f12d9STrond Myklebust struct writeback_control wbc = { 17807f2f12d9STrond Myklebust .sync_mode = WB_SYNC_ALL, 17817f2f12d9STrond Myklebust .nr_to_write = 0, 17827f2f12d9STrond Myklebust .range_start = range_start, 17837f2f12d9STrond Myklebust .range_end = range_end, 17847f2f12d9STrond Myklebust }; 17857f2f12d9STrond Myklebust int ret; 17867f2f12d9STrond Myklebust 1787f4ce1299STrond Myklebust trace_nfs_writeback_page_enter(inode); 1788f4ce1299STrond Myklebust 17890522f6adSTrond Myklebust for (;;) { 1790ba8b06e6STrond Myklebust wait_on_page_writeback(page); 17917f2f12d9STrond Myklebust if (clear_page_dirty_for_io(page)) { 17927f2f12d9STrond Myklebust ret = nfs_writepage_locked(page, &wbc); 17937f2f12d9STrond Myklebust if (ret < 0) 17947f2f12d9STrond Myklebust goto out_error; 17950522f6adSTrond Myklebust continue; 17967f2f12d9STrond Myklebust } 1797f4ce1299STrond Myklebust ret = 0; 17980522f6adSTrond Myklebust if (!PagePrivate(page)) 17990522f6adSTrond Myklebust break; 18000522f6adSTrond Myklebust ret = nfs_commit_inode(inode, FLUSH_SYNC); 18017f2f12d9STrond Myklebust if (ret < 0) 18027f2f12d9STrond Myklebust goto out_error; 18037f2f12d9STrond Myklebust } 18047f2f12d9STrond Myklebust out_error: 1805f4ce1299STrond Myklebust trace_nfs_writeback_page_exit(inode, ret); 18067f2f12d9STrond Myklebust return ret; 18071c75950bSTrond Myklebust } 18081c75950bSTrond Myklebust 1809074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION 1810074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage, 1811a6bc32b8SMel Gorman struct page *page, enum migrate_mode mode) 1812074cc1deSTrond Myklebust { 18132da95652SJeff Layton /* 18142da95652SJeff Layton * If PagePrivate is set, then the page is currently associated with 18152da95652SJeff Layton * an in-progress read or write request. Don't try to migrate it. 18162da95652SJeff Layton * 18172da95652SJeff Layton * FIXME: we could do this in principle, but we'll need a way to ensure 18182da95652SJeff Layton * that we can safely release the inode reference while holding 18192da95652SJeff Layton * the page lock. 18202da95652SJeff Layton */ 18212da95652SJeff Layton if (PagePrivate(page)) 18222da95652SJeff Layton return -EBUSY; 1823074cc1deSTrond Myklebust 18248c209ce7SDavid Howells if (!nfs_fscache_release_page(page, GFP_KERNEL)) 18258c209ce7SDavid Howells return -EBUSY; 1826074cc1deSTrond Myklebust 1827a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 1828074cc1deSTrond Myklebust } 1829074cc1deSTrond Myklebust #endif 1830074cc1deSTrond Myklebust 1831f7b422b1SDavid Howells int __init nfs_init_writepagecache(void) 18321da177e4SLinus Torvalds { 18331da177e4SLinus Torvalds nfs_wdata_cachep = kmem_cache_create("nfs_write_data", 1834c0752cdfSAnna Schumaker sizeof(struct nfs_rw_header), 18351da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 183620c2df83SPaul Mundt NULL); 18371da177e4SLinus Torvalds if (nfs_wdata_cachep == NULL) 18381da177e4SLinus Torvalds return -ENOMEM; 18391da177e4SLinus Torvalds 184093d2341cSMatthew Dobson nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, 18411da177e4SLinus Torvalds nfs_wdata_cachep); 18421da177e4SLinus Torvalds if (nfs_wdata_mempool == NULL) 18433dd4765fSJeff Layton goto out_destroy_write_cache; 18441da177e4SLinus Torvalds 18450b7c0153SFred Isaman nfs_cdata_cachep = kmem_cache_create("nfs_commit_data", 18460b7c0153SFred Isaman sizeof(struct nfs_commit_data), 18470b7c0153SFred Isaman 0, SLAB_HWCACHE_ALIGN, 18480b7c0153SFred Isaman NULL); 18490b7c0153SFred Isaman if (nfs_cdata_cachep == NULL) 18503dd4765fSJeff Layton goto out_destroy_write_mempool; 18510b7c0153SFred Isaman 185293d2341cSMatthew Dobson nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, 18534c100210SYanchuan Nian nfs_cdata_cachep); 18541da177e4SLinus Torvalds if (nfs_commit_mempool == NULL) 18553dd4765fSJeff Layton goto out_destroy_commit_cache; 18561da177e4SLinus Torvalds 185789a09141SPeter Zijlstra /* 185889a09141SPeter Zijlstra * NFS congestion size, scale with available memory. 185989a09141SPeter Zijlstra * 186089a09141SPeter Zijlstra * 64MB: 8192k 186189a09141SPeter Zijlstra * 128MB: 11585k 186289a09141SPeter Zijlstra * 256MB: 16384k 186389a09141SPeter Zijlstra * 512MB: 23170k 186489a09141SPeter Zijlstra * 1GB: 32768k 186589a09141SPeter Zijlstra * 2GB: 46340k 186689a09141SPeter Zijlstra * 4GB: 65536k 186789a09141SPeter Zijlstra * 8GB: 92681k 186889a09141SPeter Zijlstra * 16GB: 131072k 186989a09141SPeter Zijlstra * 187089a09141SPeter Zijlstra * This allows larger machines to have larger/more transfers. 187189a09141SPeter Zijlstra * Limit the default to 256M 187289a09141SPeter Zijlstra */ 187389a09141SPeter Zijlstra nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); 187489a09141SPeter Zijlstra if (nfs_congestion_kb > 256*1024) 187589a09141SPeter Zijlstra nfs_congestion_kb = 256*1024; 187689a09141SPeter Zijlstra 18771da177e4SLinus Torvalds return 0; 18783dd4765fSJeff Layton 18793dd4765fSJeff Layton out_destroy_commit_cache: 18803dd4765fSJeff Layton kmem_cache_destroy(nfs_cdata_cachep); 18813dd4765fSJeff Layton out_destroy_write_mempool: 18823dd4765fSJeff Layton mempool_destroy(nfs_wdata_mempool); 18833dd4765fSJeff Layton out_destroy_write_cache: 18843dd4765fSJeff Layton kmem_cache_destroy(nfs_wdata_cachep); 18853dd4765fSJeff Layton return -ENOMEM; 18861da177e4SLinus Torvalds } 18871da177e4SLinus Torvalds 1888266bee88SDavid Brownell void nfs_destroy_writepagecache(void) 18891da177e4SLinus Torvalds { 18901da177e4SLinus Torvalds mempool_destroy(nfs_commit_mempool); 18913dd4765fSJeff Layton kmem_cache_destroy(nfs_cdata_cachep); 18921da177e4SLinus Torvalds mempool_destroy(nfs_wdata_mempool); 18931a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_wdata_cachep); 18941da177e4SLinus Torvalds } 18951da177e4SLinus Torvalds 18964a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = { 1897a4cdda59SAnna Schumaker .rw_mode = FMODE_WRITE, 18984a0de55cSAnna Schumaker .rw_alloc_header = nfs_writehdr_alloc, 18994a0de55cSAnna Schumaker .rw_free_header = nfs_writehdr_free, 1900a4cdda59SAnna Schumaker .rw_release = nfs_writeback_release_common, 1901*0eecb214SAnna Schumaker .rw_done = nfs_writeback_done, 1902*0eecb214SAnna Schumaker .rw_result = nfs_writeback_result, 19034a0de55cSAnna Schumaker }; 1904