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; 49*4a0de55cSAnna 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 74*4a0de55cSAnna 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 78*4a0de55cSAnna Schumaker if (p) 793feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 803feb2d49STrond Myklebust return p; 813feb2d49STrond Myklebust } 823feb2d49STrond Myklebust 83*4a0de55cSAnna 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 1204*4a0de55cSAnna 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; 1210*4a0de55cSAnna 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 1238*4a0de55cSAnna Schumaker nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops, 1239*4a0de55cSAnna 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 1251def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata) 1252def6ed7eSAndy Adamson { 12539c7e1b3dSAnna Schumaker struct nfs_pgio_data *data = calldata; 1254ef1820f9SNeilBrown int err; 1255ef1820f9SNeilBrown err = NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data); 1256ef1820f9SNeilBrown if (err) 1257ef1820f9SNeilBrown rpc_exit(task, err); 1258def6ed7eSAndy Adamson } 1259def6ed7eSAndy Adamson 12600b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata) 12610b7c0153SFred Isaman { 12620b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 12630b7c0153SFred Isaman 12640b7c0153SFred Isaman NFS_PROTO(data->inode)->commit_rpc_prepare(task, data); 12650b7c0153SFred Isaman } 12660b7c0153SFred Isaman 12671da177e4SLinus Torvalds /* 12681da177e4SLinus Torvalds * Handle a write reply that flushes a whole page. 12691da177e4SLinus Torvalds * 12701da177e4SLinus Torvalds * FIXME: There is an inherent race with invalidate_inode_pages and 12711da177e4SLinus Torvalds * writebacks since the page->count is kept > 1 for as long 12721da177e4SLinus Torvalds * as the page has a write request pending. 12731da177e4SLinus Torvalds */ 12746c75dc0dSFred Isaman static void nfs_writeback_done_common(struct rpc_task *task, void *calldata) 12751da177e4SLinus Torvalds { 12769c7e1b3dSAnna Schumaker struct nfs_pgio_data *data = calldata; 12771da177e4SLinus Torvalds 1278c9d8f89dSTrond Myklebust nfs_writeback_done(task, data); 1279c9d8f89dSTrond Myklebust } 1280c9d8f89dSTrond Myklebust 12816c75dc0dSFred Isaman static void nfs_writeback_release_common(void *calldata) 1282c9d8f89dSTrond Myklebust { 12839c7e1b3dSAnna Schumaker struct nfs_pgio_data *data = calldata; 1284cd841605SFred Isaman struct nfs_pgio_header *hdr = data->header; 1285e2fecb21STrond Myklebust int status = data->task.tk_status; 1286788e7a89STrond Myklebust 12876c75dc0dSFred Isaman if ((status >= 0) && nfs_write_need_commit(data)) { 12886c75dc0dSFred Isaman spin_lock(&hdr->lock); 12896c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) 12906c75dc0dSFred Isaman ; /* Do nothing */ 12916c75dc0dSFred Isaman else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) 1292f79d06f5SAnna Schumaker memcpy(&hdr->verf, &data->verf, sizeof(hdr->verf)); 1293f79d06f5SAnna Schumaker else if (memcmp(&hdr->verf, &data->verf, sizeof(hdr->verf))) 12946c75dc0dSFred Isaman set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags); 12956c75dc0dSFred Isaman spin_unlock(&hdr->lock); 12961da177e4SLinus Torvalds } 129700bfa30aSAnna Schumaker nfs_pgio_data_release(data); 12981da177e4SLinus Torvalds } 12991da177e4SLinus Torvalds 13006c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops = { 1301def6ed7eSAndy Adamson .rpc_call_prepare = nfs_write_prepare, 13026c75dc0dSFred Isaman .rpc_call_done = nfs_writeback_done_common, 13036c75dc0dSFred Isaman .rpc_release = nfs_writeback_release_common, 1304788e7a89STrond Myklebust }; 1305788e7a89STrond Myklebust 13061f2edbe3STrond Myklebust /* 13071f2edbe3STrond Myklebust * Special version of should_remove_suid() that ignores capabilities. 13081f2edbe3STrond Myklebust */ 13091f2edbe3STrond Myklebust static int nfs_should_remove_suid(const struct inode *inode) 13101f2edbe3STrond Myklebust { 13111f2edbe3STrond Myklebust umode_t mode = inode->i_mode; 13121f2edbe3STrond Myklebust int kill = 0; 13131f2edbe3STrond Myklebust 13141f2edbe3STrond Myklebust /* suid always must be killed */ 13151f2edbe3STrond Myklebust if (unlikely(mode & S_ISUID)) 13161f2edbe3STrond Myklebust kill = ATTR_KILL_SUID; 13171f2edbe3STrond Myklebust 13181f2edbe3STrond Myklebust /* 13191f2edbe3STrond Myklebust * sgid without any exec bits is just a mandatory locking mark; leave 13201f2edbe3STrond Myklebust * it alone. If some exec bits are set, it's a real sgid; kill it. 13211f2edbe3STrond Myklebust */ 13221f2edbe3STrond Myklebust if (unlikely((mode & S_ISGID) && (mode & S_IXGRP))) 13231f2edbe3STrond Myklebust kill |= ATTR_KILL_SGID; 13241f2edbe3STrond Myklebust 13251f2edbe3STrond Myklebust if (unlikely(kill && S_ISREG(mode))) 13261f2edbe3STrond Myklebust return kill; 13271f2edbe3STrond Myklebust 13281f2edbe3STrond Myklebust return 0; 13291f2edbe3STrond Myklebust } 1330788e7a89STrond Myklebust 13311da177e4SLinus Torvalds /* 13321da177e4SLinus Torvalds * This function is called when the WRITE call is complete. 13331da177e4SLinus Torvalds */ 13349c7e1b3dSAnna Schumaker void nfs_writeback_done(struct rpc_task *task, struct nfs_pgio_data *data) 13351da177e4SLinus Torvalds { 13363c6b899cSAnna Schumaker struct nfs_pgio_args *argp = &data->args; 13379137bdf3SAnna Schumaker struct nfs_pgio_res *resp = &data->res; 1338cd841605SFred Isaman struct inode *inode = data->header->inode; 1339788e7a89STrond Myklebust int status; 13401da177e4SLinus Torvalds 1341a3f565b1SChuck Lever dprintk("NFS: %5u nfs_writeback_done (status %d)\n", 13421da177e4SLinus Torvalds task->tk_pid, task->tk_status); 13431da177e4SLinus Torvalds 1344f551e44fSChuck Lever /* 1345f551e44fSChuck Lever * ->write_done will attempt to use post-op attributes to detect 1346f551e44fSChuck Lever * conflicting writes by other clients. A strict interpretation 1347f551e44fSChuck Lever * of close-to-open would allow us to continue caching even if 1348f551e44fSChuck Lever * another writer had changed the file, but some applications 1349f551e44fSChuck Lever * depend on tighter cache coherency when writing. 1350f551e44fSChuck Lever */ 1351cd841605SFred Isaman status = NFS_PROTO(inode)->write_done(task, data); 1352788e7a89STrond Myklebust if (status != 0) 135313602896SFred Isaman return; 1354cd841605SFred Isaman nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count); 135591d5b470SChuck Lever 135689d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4) 13571da177e4SLinus Torvalds if (resp->verf->committed < argp->stable && task->tk_status >= 0) { 13581da177e4SLinus Torvalds /* We tried a write call, but the server did not 13591da177e4SLinus Torvalds * commit data to stable storage even though we 13601da177e4SLinus Torvalds * requested it. 13611da177e4SLinus Torvalds * Note: There is a known bug in Tru64 < 5.0 in which 13621da177e4SLinus Torvalds * the server reports NFS_DATA_SYNC, but performs 13631da177e4SLinus Torvalds * NFS_FILE_SYNC. We therefore implement this checking 13641da177e4SLinus Torvalds * as a dprintk() in order to avoid filling syslog. 13651da177e4SLinus Torvalds */ 13661da177e4SLinus Torvalds static unsigned long complain; 13671da177e4SLinus Torvalds 1368a69aef14SFred Isaman /* Note this will print the MDS for a DS write */ 13691da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 13701da177e4SLinus Torvalds dprintk("NFS: faulty NFS server %s:" 13711da177e4SLinus Torvalds " (committed = %d) != (stable = %d)\n", 1372cd841605SFred Isaman NFS_SERVER(inode)->nfs_client->cl_hostname, 13731da177e4SLinus Torvalds resp->verf->committed, argp->stable); 13741da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 13751da177e4SLinus Torvalds } 13761da177e4SLinus Torvalds } 13771da177e4SLinus Torvalds #endif 13781f2edbe3STrond Myklebust if (task->tk_status < 0) { 13796c75dc0dSFred Isaman nfs_set_pgio_error(data->header, task->tk_status, argp->offset); 13801f2edbe3STrond Myklebust return; 13811f2edbe3STrond Myklebust } 13821f2edbe3STrond Myklebust 13831f2edbe3STrond Myklebust /* Deal with the suid/sgid bit corner case */ 13841f2edbe3STrond Myklebust if (nfs_should_remove_suid(inode)) 13851f2edbe3STrond Myklebust nfs_mark_for_revalidate(inode); 13861f2edbe3STrond Myklebust 13871f2edbe3STrond Myklebust if (resp->count < argp->count) { 13881da177e4SLinus Torvalds static unsigned long complain; 13891da177e4SLinus Torvalds 13906c75dc0dSFred Isaman /* This a short write! */ 1391cd841605SFred Isaman nfs_inc_stats(inode, NFSIOS_SHORTWRITE); 139291d5b470SChuck Lever 13931da177e4SLinus Torvalds /* Has the server at least made some progress? */ 13946c75dc0dSFred Isaman if (resp->count == 0) { 13956c75dc0dSFred Isaman if (time_before(complain, jiffies)) { 13966c75dc0dSFred Isaman printk(KERN_WARNING 13976c75dc0dSFred Isaman "NFS: Server wrote zero bytes, expected %u.\n", 13986c75dc0dSFred Isaman argp->count); 13996c75dc0dSFred Isaman complain = jiffies + 300 * HZ; 14006c75dc0dSFred Isaman } 14016c75dc0dSFred Isaman nfs_set_pgio_error(data->header, -EIO, argp->offset); 14026c75dc0dSFred Isaman task->tk_status = -EIO; 14036c75dc0dSFred Isaman return; 14046c75dc0dSFred Isaman } 14051da177e4SLinus Torvalds /* Was this an NFSv2 write or an NFSv3 stable write? */ 14061da177e4SLinus Torvalds if (resp->verf->committed != NFS_UNSTABLE) { 14071da177e4SLinus Torvalds /* Resend from where the server left off */ 1408a69aef14SFred Isaman data->mds_offset += resp->count; 14091da177e4SLinus Torvalds argp->offset += resp->count; 14101da177e4SLinus Torvalds argp->pgbase += resp->count; 14111da177e4SLinus Torvalds argp->count -= resp->count; 14121da177e4SLinus Torvalds } else { 14131da177e4SLinus Torvalds /* Resend as a stable write in order to avoid 14141da177e4SLinus Torvalds * headaches in the case of a server crash. 14151da177e4SLinus Torvalds */ 14161da177e4SLinus Torvalds argp->stable = NFS_FILE_SYNC; 14171da177e4SLinus Torvalds } 1418d00c5d43STrond Myklebust rpc_restart_call_prepare(task); 14191da177e4SLinus Torvalds } 14201da177e4SLinus Torvalds } 14211da177e4SLinus Torvalds 14221da177e4SLinus Torvalds 142389d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4) 142471d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait) 142571d0a611STrond Myklebust { 1426b8413f98STrond Myklebust int ret; 1427b8413f98STrond Myklebust 142871d0a611STrond Myklebust if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags)) 142971d0a611STrond Myklebust return 1; 1430b8413f98STrond Myklebust if (!may_wait) 143171d0a611STrond Myklebust return 0; 1432b8413f98STrond Myklebust ret = out_of_line_wait_on_bit_lock(&nfsi->flags, 1433b8413f98STrond Myklebust NFS_INO_COMMIT, 1434b8413f98STrond Myklebust nfs_wait_bit_killable, 1435b8413f98STrond Myklebust TASK_KILLABLE); 1436b8413f98STrond Myklebust return (ret < 0) ? ret : 1; 143771d0a611STrond Myklebust } 143871d0a611STrond Myklebust 1439f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi) 144071d0a611STrond Myklebust { 144171d0a611STrond Myklebust clear_bit(NFS_INO_COMMIT, &nfsi->flags); 144271d0a611STrond Myklebust smp_mb__after_clear_bit(); 144371d0a611STrond Myklebust wake_up_bit(&nfsi->flags, NFS_INO_COMMIT); 144471d0a611STrond Myklebust } 144571d0a611STrond Myklebust 14460b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data) 14471da177e4SLinus Torvalds { 14480b7c0153SFred Isaman put_nfs_open_context(data->context); 14490b7c0153SFred Isaman nfs_commit_free(data); 14501da177e4SLinus Torvalds } 1451e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release); 14521da177e4SLinus Torvalds 14530b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data, 14549ace33cdSFred Isaman const struct rpc_call_ops *call_ops, 14559f0ec176SAndy Adamson int how, int flags) 14561da177e4SLinus Torvalds { 145707737691STrond Myklebust struct rpc_task *task; 14589ace33cdSFred Isaman int priority = flush_task_priority(how); 1459bdc7f021STrond Myklebust struct rpc_message msg = { 1460bdc7f021STrond Myklebust .rpc_argp = &data->args, 1461bdc7f021STrond Myklebust .rpc_resp = &data->res, 14629ace33cdSFred Isaman .rpc_cred = data->cred, 1463bdc7f021STrond Myklebust }; 146484115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 146507737691STrond Myklebust .task = &data->task, 14669ace33cdSFred Isaman .rpc_client = clnt, 1467bdc7f021STrond Myklebust .rpc_message = &msg, 14689ace33cdSFred Isaman .callback_ops = call_ops, 146984115e1cSTrond Myklebust .callback_data = data, 1470101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 14719f0ec176SAndy Adamson .flags = RPC_TASK_ASYNC | flags, 14723ff7576dSTrond Myklebust .priority = priority, 147384115e1cSTrond Myklebust }; 1474788e7a89STrond Myklebust /* Set up the initial task struct. */ 14759ace33cdSFred Isaman NFS_PROTO(data->inode)->commit_setup(data, &msg); 14761da177e4SLinus Torvalds 1477a3f565b1SChuck Lever dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); 1478bdc7f021STrond Myklebust 14798c21c62cSWeston Andros Adamson nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client, 14808c21c62cSWeston Andros Adamson NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg); 14818c21c62cSWeston Andros Adamson 148207737691STrond Myklebust task = rpc_run_task(&task_setup_data); 1483dbae4c73STrond Myklebust if (IS_ERR(task)) 1484dbae4c73STrond Myklebust return PTR_ERR(task); 1485d2224e7aSJeff Layton if (how & FLUSH_SYNC) 1486d2224e7aSJeff Layton rpc_wait_for_completion_task(task); 148707737691STrond Myklebust rpc_put_task(task); 1488dbae4c73STrond Myklebust return 0; 14891da177e4SLinus Torvalds } 1490e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit); 14911da177e4SLinus Torvalds 14921da177e4SLinus Torvalds /* 14939ace33cdSFred Isaman * Set up the argument/result storage required for the RPC call. 14949ace33cdSFred Isaman */ 14950b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data, 1496988b6dceSFred Isaman struct list_head *head, 1497f453a54aSFred Isaman struct pnfs_layout_segment *lseg, 1498f453a54aSFred Isaman struct nfs_commit_info *cinfo) 14999ace33cdSFred Isaman { 15009ace33cdSFred Isaman struct nfs_page *first = nfs_list_entry(head->next); 15013d4ff43dSAl Viro struct inode *inode = first->wb_context->dentry->d_inode; 15029ace33cdSFred Isaman 15039ace33cdSFred Isaman /* Set up the RPC argument and reply structs 15049ace33cdSFred Isaman * NB: take care not to mess about with data->commit et al. */ 15059ace33cdSFred Isaman 15069ace33cdSFred Isaman list_splice_init(head, &data->pages); 15079ace33cdSFred Isaman 15089ace33cdSFred Isaman data->inode = inode; 15099ace33cdSFred Isaman data->cred = first->wb_context->cred; 1510988b6dceSFred Isaman data->lseg = lseg; /* reference transferred */ 15119ace33cdSFred Isaman data->mds_ops = &nfs_commit_ops; 1512f453a54aSFred Isaman data->completion_ops = cinfo->completion_ops; 1513b359f9d0SFred Isaman data->dreq = cinfo->dreq; 15149ace33cdSFred Isaman 15159ace33cdSFred Isaman data->args.fh = NFS_FH(data->inode); 15169ace33cdSFred Isaman /* Note: we always request a commit of the entire inode */ 15179ace33cdSFred Isaman data->args.offset = 0; 15189ace33cdSFred Isaman data->args.count = 0; 15190b7c0153SFred Isaman data->context = get_nfs_open_context(first->wb_context); 15209ace33cdSFred Isaman data->res.fattr = &data->fattr; 15219ace33cdSFred Isaman data->res.verf = &data->verf; 15229ace33cdSFred Isaman nfs_fattr_init(&data->fattr); 15239ace33cdSFred Isaman } 1524e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit); 15259ace33cdSFred Isaman 1526e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list, 1527ea2cf228SFred Isaman struct pnfs_layout_segment *lseg, 1528ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 152964bfeb49SFred Isaman { 153064bfeb49SFred Isaman struct nfs_page *req; 153164bfeb49SFred Isaman 153264bfeb49SFred Isaman while (!list_empty(page_list)) { 153364bfeb49SFred Isaman req = nfs_list_entry(page_list->next); 153464bfeb49SFred Isaman nfs_list_remove_request(req); 1535ea2cf228SFred Isaman nfs_mark_request_commit(req, lseg, cinfo); 153656f9cd68SFred Isaman if (!cinfo->dreq) { 153764bfeb49SFred Isaman dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 1538d56b4ddfSMel Gorman dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info, 153964bfeb49SFred Isaman BDI_RECLAIMABLE); 154056f9cd68SFred Isaman } 15411d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 154264bfeb49SFred Isaman } 154364bfeb49SFred Isaman } 1544e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit); 154564bfeb49SFred Isaman 15469ace33cdSFred Isaman /* 15471da177e4SLinus Torvalds * Commit dirty pages 15481da177e4SLinus Torvalds */ 15491da177e4SLinus Torvalds static int 1550ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how, 1551ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 15521da177e4SLinus Torvalds { 15530b7c0153SFred Isaman struct nfs_commit_data *data; 15541da177e4SLinus Torvalds 1555c9d8f89dSTrond Myklebust data = nfs_commitdata_alloc(); 15561da177e4SLinus Torvalds 15571da177e4SLinus Torvalds if (!data) 15581da177e4SLinus Torvalds goto out_bad; 15591da177e4SLinus Torvalds 15601da177e4SLinus Torvalds /* Set up the argument struct */ 1561f453a54aSFred Isaman nfs_init_commit(data, head, NULL, cinfo); 1562f453a54aSFred Isaman atomic_inc(&cinfo->mds->rpcs_out); 15639f0ec176SAndy Adamson return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops, 15649f0ec176SAndy Adamson how, 0); 15651da177e4SLinus Torvalds out_bad: 1566ea2cf228SFred Isaman nfs_retry_commit(head, NULL, cinfo); 1567f453a54aSFred Isaman cinfo->completion_ops->error_cleanup(NFS_I(inode)); 15681da177e4SLinus Torvalds return -ENOMEM; 15691da177e4SLinus Torvalds } 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds /* 15721da177e4SLinus Torvalds * COMMIT call returned 15731da177e4SLinus Torvalds */ 1574788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata) 15751da177e4SLinus Torvalds { 15760b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 15771da177e4SLinus Torvalds 1578a3f565b1SChuck Lever dprintk("NFS: %5u nfs_commit_done (status %d)\n", 15791da177e4SLinus Torvalds task->tk_pid, task->tk_status); 15801da177e4SLinus Torvalds 1581788e7a89STrond Myklebust /* Call the NFS version-specific code */ 1582c0d0e96bSTrond Myklebust NFS_PROTO(data->inode)->commit_done(task, data); 1583c9d8f89dSTrond Myklebust } 1584c9d8f89dSTrond Myklebust 1585f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data) 1586c9d8f89dSTrond Myklebust { 1587c9d8f89dSTrond Myklebust struct nfs_page *req; 1588c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1589f453a54aSFred Isaman struct nfs_commit_info cinfo; 1590788e7a89STrond Myklebust 15911da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 15921da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 15931da177e4SLinus Torvalds nfs_list_remove_request(req); 1594d6d6dc7cSFred Isaman nfs_clear_page_commit(req->wb_page); 15951da177e4SLinus Torvalds 15961e8968c5SNiels de Vos dprintk("NFS: commit (%s/%llu %d@%lld)", 15973d4ff43dSAl Viro req->wb_context->dentry->d_sb->s_id, 15981e8968c5SNiels de Vos (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode), 15991da177e4SLinus Torvalds req->wb_bytes, 16001da177e4SLinus Torvalds (long long)req_offset(req)); 1601c9d8f89dSTrond Myklebust if (status < 0) { 1602c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 16031da177e4SLinus Torvalds nfs_inode_remove_request(req); 1604c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 16051da177e4SLinus Torvalds goto next; 16061da177e4SLinus Torvalds } 16071da177e4SLinus Torvalds 16081da177e4SLinus Torvalds /* Okay, COMMIT succeeded, apparently. Check the verifier 16091da177e4SLinus Torvalds * returned by the server against all stored verfs. */ 16102f2c63bcSTrond Myklebust if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) { 16111da177e4SLinus Torvalds /* We have a match */ 16121da177e4SLinus Torvalds nfs_inode_remove_request(req); 16131da177e4SLinus Torvalds dprintk(" OK\n"); 16141da177e4SLinus Torvalds goto next; 16151da177e4SLinus Torvalds } 16161da177e4SLinus Torvalds /* We have a mismatch. Write the page again */ 16171da177e4SLinus Torvalds dprintk(" mismatch\n"); 16186d884e8fSFred nfs_mark_request_dirty(req); 161905990d1bSTrond Myklebust set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags); 16201da177e4SLinus Torvalds next: 16211d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 16221da177e4SLinus Torvalds } 1623f453a54aSFred Isaman nfs_init_cinfo(&cinfo, data->inode, data->dreq); 1624f453a54aSFred Isaman if (atomic_dec_and_test(&cinfo.mds->rpcs_out)) 1625f453a54aSFred Isaman nfs_commit_clear_lock(NFS_I(data->inode)); 16265917ce84SFred Isaman } 16275917ce84SFred Isaman 16285917ce84SFred Isaman static void nfs_commit_release(void *calldata) 16295917ce84SFred Isaman { 16300b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 16315917ce84SFred Isaman 1632f453a54aSFred Isaman data->completion_ops->completion(data); 1633c9d8f89dSTrond Myklebust nfs_commitdata_release(calldata); 16341da177e4SLinus Torvalds } 1635788e7a89STrond Myklebust 1636788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = { 16370b7c0153SFred Isaman .rpc_call_prepare = nfs_commit_prepare, 1638788e7a89STrond Myklebust .rpc_call_done = nfs_commit_done, 1639788e7a89STrond Myklebust .rpc_release = nfs_commit_release, 1640788e7a89STrond Myklebust }; 16411da177e4SLinus Torvalds 1642f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = { 1643f453a54aSFred Isaman .completion = nfs_commit_release_pages, 1644f453a54aSFred Isaman .error_cleanup = nfs_commit_clear_lock, 1645f453a54aSFred Isaman }; 1646f453a54aSFred Isaman 16471763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head, 1648ea2cf228SFred Isaman int how, struct nfs_commit_info *cinfo) 164984c53ab5SFred Isaman { 165084c53ab5SFred Isaman int status; 165184c53ab5SFred Isaman 1652ea2cf228SFred Isaman status = pnfs_commit_list(inode, head, how, cinfo); 165384c53ab5SFred Isaman if (status == PNFS_NOT_ATTEMPTED) 1654ea2cf228SFred Isaman status = nfs_commit_list(inode, head, how, cinfo); 165584c53ab5SFred Isaman return status; 165684c53ab5SFred Isaman } 165784c53ab5SFred Isaman 1658b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how) 16591da177e4SLinus Torvalds { 16601da177e4SLinus Torvalds LIST_HEAD(head); 1661ea2cf228SFred Isaman struct nfs_commit_info cinfo; 166271d0a611STrond Myklebust int may_wait = how & FLUSH_SYNC; 1663b8413f98STrond Myklebust int res; 16641da177e4SLinus Torvalds 1665b8413f98STrond Myklebust res = nfs_commit_set_lock(NFS_I(inode), may_wait); 1666b8413f98STrond Myklebust if (res <= 0) 1667c5efa5fcSTrond Myklebust goto out_mark_dirty; 1668ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, inode); 1669ea2cf228SFred Isaman res = nfs_scan_commit(inode, &head, &cinfo); 16701da177e4SLinus Torvalds if (res) { 1671a861a1e1SFred Isaman int error; 1672a861a1e1SFred Isaman 1673ea2cf228SFred Isaman error = nfs_generic_commit_list(inode, &head, how, &cinfo); 16741da177e4SLinus Torvalds if (error < 0) 16751da177e4SLinus Torvalds return error; 1676b8413f98STrond Myklebust if (!may_wait) 1677b8413f98STrond Myklebust goto out_mark_dirty; 1678b8413f98STrond Myklebust error = wait_on_bit(&NFS_I(inode)->flags, 1679b8413f98STrond Myklebust NFS_INO_COMMIT, 168071d0a611STrond Myklebust nfs_wait_bit_killable, 168171d0a611STrond Myklebust TASK_KILLABLE); 1682b8413f98STrond Myklebust if (error < 0) 1683b8413f98STrond Myklebust return error; 168471d0a611STrond Myklebust } else 168571d0a611STrond Myklebust nfs_commit_clear_lock(NFS_I(inode)); 1686c5efa5fcSTrond Myklebust return res; 1687c5efa5fcSTrond Myklebust /* Note: If we exit without ensuring that the commit is complete, 1688c5efa5fcSTrond Myklebust * we must mark the inode as dirty. Otherwise, future calls to 1689c5efa5fcSTrond Myklebust * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure 1690c5efa5fcSTrond Myklebust * that the data is on the disk. 1691c5efa5fcSTrond Myklebust */ 1692c5efa5fcSTrond Myklebust out_mark_dirty: 1693c5efa5fcSTrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 16941da177e4SLinus Torvalds return res; 16951da177e4SLinus Torvalds } 16968fc795f7STrond Myklebust 16978fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 16988fc795f7STrond Myklebust { 1699420e3646STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 1700420e3646STrond Myklebust int flags = FLUSH_SYNC; 1701420e3646STrond Myklebust int ret = 0; 17028fc795f7STrond Myklebust 17033236c3e1SJeff Layton /* no commits means nothing needs to be done */ 1704ea2cf228SFred Isaman if (!nfsi->commit_info.ncommit) 17053236c3e1SJeff Layton return ret; 17063236c3e1SJeff Layton 1707a00dd6c0SJeff Layton if (wbc->sync_mode == WB_SYNC_NONE) { 1708a00dd6c0SJeff Layton /* Don't commit yet if this is a non-blocking flush and there 1709a00dd6c0SJeff Layton * are a lot of outstanding writes for this mapping. 1710420e3646STrond Myklebust */ 1711ea2cf228SFred Isaman if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1)) 1712420e3646STrond Myklebust goto out_mark_dirty; 1713420e3646STrond Myklebust 1714a00dd6c0SJeff Layton /* don't wait for the COMMIT response */ 1715420e3646STrond Myklebust flags = 0; 1716a00dd6c0SJeff Layton } 1717a00dd6c0SJeff Layton 1718420e3646STrond Myklebust ret = nfs_commit_inode(inode, flags); 1719420e3646STrond Myklebust if (ret >= 0) { 1720420e3646STrond Myklebust if (wbc->sync_mode == WB_SYNC_NONE) { 1721420e3646STrond Myklebust if (ret < wbc->nr_to_write) 1722420e3646STrond Myklebust wbc->nr_to_write -= ret; 1723420e3646STrond Myklebust else 1724420e3646STrond Myklebust wbc->nr_to_write = 0; 1725420e3646STrond Myklebust } 17268fc795f7STrond Myklebust return 0; 1727420e3646STrond Myklebust } 1728420e3646STrond Myklebust out_mark_dirty: 17298fc795f7STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 17308fc795f7STrond Myklebust return ret; 17318fc795f7STrond Myklebust } 1732c63c7b05STrond Myklebust #else 17338fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 17348fc795f7STrond Myklebust { 17358fc795f7STrond Myklebust return 0; 17368fc795f7STrond Myklebust } 17371da177e4SLinus Torvalds #endif 17381da177e4SLinus Torvalds 17398fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc) 17408fc795f7STrond Myklebust { 1741a8d8f02cSBryan Schumaker return nfs_commit_unstable_pages(inode, wbc); 1742a8d8f02cSBryan Schumaker } 174389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode); 1744863a3c6cSAndy Adamson 1745acdc53b2STrond Myklebust /* 1746acdc53b2STrond Myklebust * flush the inode to disk. 1747acdc53b2STrond Myklebust */ 1748acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode) 174934901f70STrond Myklebust { 175034901f70STrond Myklebust struct writeback_control wbc = { 175172cb77f4STrond Myklebust .sync_mode = WB_SYNC_ALL, 175234901f70STrond Myklebust .nr_to_write = LONG_MAX, 1753d7fb1207STrond Myklebust .range_start = 0, 1754d7fb1207STrond Myklebust .range_end = LLONG_MAX, 175534901f70STrond Myklebust }; 1756f4ce1299STrond Myklebust int ret; 175734901f70STrond Myklebust 1758f4ce1299STrond Myklebust trace_nfs_writeback_inode_enter(inode); 1759f4ce1299STrond Myklebust 1760f4ce1299STrond Myklebust ret = sync_inode(inode, &wbc); 1761f4ce1299STrond Myklebust 1762f4ce1299STrond Myklebust trace_nfs_writeback_inode_exit(inode, ret); 1763f4ce1299STrond Myklebust return ret; 17641c75950bSTrond Myklebust } 1765ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all); 17661c75950bSTrond Myklebust 17671b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page) 17681b3b4a1aSTrond Myklebust { 17691b3b4a1aSTrond Myklebust struct nfs_page *req; 17701b3b4a1aSTrond Myklebust int ret = 0; 17711b3b4a1aSTrond Myklebust 17721b3b4a1aSTrond Myklebust for (;;) { 1773ba8b06e6STrond Myklebust wait_on_page_writeback(page); 17741b3b4a1aSTrond Myklebust req = nfs_page_find_request(page); 17751b3b4a1aSTrond Myklebust if (req == NULL) 17761b3b4a1aSTrond Myklebust break; 17777ad84aa9STrond Myklebust if (nfs_lock_request(req)) { 17788dd37758STrond Myklebust nfs_clear_request_commit(req); 17791b3b4a1aSTrond Myklebust nfs_inode_remove_request(req); 17801b3b4a1aSTrond Myklebust /* 17811b3b4a1aSTrond Myklebust * In case nfs_inode_remove_request has marked the 17821b3b4a1aSTrond Myklebust * page as being dirty 17831b3b4a1aSTrond Myklebust */ 17841b3b4a1aSTrond Myklebust cancel_dirty_page(page, PAGE_CACHE_SIZE); 17851d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 17861b3b4a1aSTrond Myklebust break; 17871b3b4a1aSTrond Myklebust } 17881b3b4a1aSTrond Myklebust ret = nfs_wait_on_request(req); 1789c9edda71STrond Myklebust nfs_release_request(req); 17901b3b4a1aSTrond Myklebust if (ret < 0) 1791c988950eSTrond Myklebust break; 17921b3b4a1aSTrond Myklebust } 17931b3b4a1aSTrond Myklebust return ret; 17941b3b4a1aSTrond Myklebust } 17951b3b4a1aSTrond Myklebust 17961c75950bSTrond Myklebust /* 17971c75950bSTrond Myklebust * Write back all requests on one page - we do this before reading it. 17981c75950bSTrond Myklebust */ 17991c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page) 18001c75950bSTrond Myklebust { 180129418aa4SMel Gorman loff_t range_start = page_file_offset(page); 18027f2f12d9STrond Myklebust loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); 18037f2f12d9STrond Myklebust struct writeback_control wbc = { 18047f2f12d9STrond Myklebust .sync_mode = WB_SYNC_ALL, 18057f2f12d9STrond Myklebust .nr_to_write = 0, 18067f2f12d9STrond Myklebust .range_start = range_start, 18077f2f12d9STrond Myklebust .range_end = range_end, 18087f2f12d9STrond Myklebust }; 18097f2f12d9STrond Myklebust int ret; 18107f2f12d9STrond Myklebust 1811f4ce1299STrond Myklebust trace_nfs_writeback_page_enter(inode); 1812f4ce1299STrond Myklebust 18130522f6adSTrond Myklebust for (;;) { 1814ba8b06e6STrond Myklebust wait_on_page_writeback(page); 18157f2f12d9STrond Myklebust if (clear_page_dirty_for_io(page)) { 18167f2f12d9STrond Myklebust ret = nfs_writepage_locked(page, &wbc); 18177f2f12d9STrond Myklebust if (ret < 0) 18187f2f12d9STrond Myklebust goto out_error; 18190522f6adSTrond Myklebust continue; 18207f2f12d9STrond Myklebust } 1821f4ce1299STrond Myklebust ret = 0; 18220522f6adSTrond Myklebust if (!PagePrivate(page)) 18230522f6adSTrond Myklebust break; 18240522f6adSTrond Myklebust ret = nfs_commit_inode(inode, FLUSH_SYNC); 18257f2f12d9STrond Myklebust if (ret < 0) 18267f2f12d9STrond Myklebust goto out_error; 18277f2f12d9STrond Myklebust } 18287f2f12d9STrond Myklebust out_error: 1829f4ce1299STrond Myklebust trace_nfs_writeback_page_exit(inode, ret); 18307f2f12d9STrond Myklebust return ret; 18311c75950bSTrond Myklebust } 18321c75950bSTrond Myklebust 1833074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION 1834074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage, 1835a6bc32b8SMel Gorman struct page *page, enum migrate_mode mode) 1836074cc1deSTrond Myklebust { 18372da95652SJeff Layton /* 18382da95652SJeff Layton * If PagePrivate is set, then the page is currently associated with 18392da95652SJeff Layton * an in-progress read or write request. Don't try to migrate it. 18402da95652SJeff Layton * 18412da95652SJeff Layton * FIXME: we could do this in principle, but we'll need a way to ensure 18422da95652SJeff Layton * that we can safely release the inode reference while holding 18432da95652SJeff Layton * the page lock. 18442da95652SJeff Layton */ 18452da95652SJeff Layton if (PagePrivate(page)) 18462da95652SJeff Layton return -EBUSY; 1847074cc1deSTrond Myklebust 18488c209ce7SDavid Howells if (!nfs_fscache_release_page(page, GFP_KERNEL)) 18498c209ce7SDavid Howells return -EBUSY; 1850074cc1deSTrond Myklebust 1851a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 1852074cc1deSTrond Myklebust } 1853074cc1deSTrond Myklebust #endif 1854074cc1deSTrond Myklebust 1855f7b422b1SDavid Howells int __init nfs_init_writepagecache(void) 18561da177e4SLinus Torvalds { 18571da177e4SLinus Torvalds nfs_wdata_cachep = kmem_cache_create("nfs_write_data", 1858c0752cdfSAnna Schumaker sizeof(struct nfs_rw_header), 18591da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 186020c2df83SPaul Mundt NULL); 18611da177e4SLinus Torvalds if (nfs_wdata_cachep == NULL) 18621da177e4SLinus Torvalds return -ENOMEM; 18631da177e4SLinus Torvalds 186493d2341cSMatthew Dobson nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, 18651da177e4SLinus Torvalds nfs_wdata_cachep); 18661da177e4SLinus Torvalds if (nfs_wdata_mempool == NULL) 18673dd4765fSJeff Layton goto out_destroy_write_cache; 18681da177e4SLinus Torvalds 18690b7c0153SFred Isaman nfs_cdata_cachep = kmem_cache_create("nfs_commit_data", 18700b7c0153SFred Isaman sizeof(struct nfs_commit_data), 18710b7c0153SFred Isaman 0, SLAB_HWCACHE_ALIGN, 18720b7c0153SFred Isaman NULL); 18730b7c0153SFred Isaman if (nfs_cdata_cachep == NULL) 18743dd4765fSJeff Layton goto out_destroy_write_mempool; 18750b7c0153SFred Isaman 187693d2341cSMatthew Dobson nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, 18774c100210SYanchuan Nian nfs_cdata_cachep); 18781da177e4SLinus Torvalds if (nfs_commit_mempool == NULL) 18793dd4765fSJeff Layton goto out_destroy_commit_cache; 18801da177e4SLinus Torvalds 188189a09141SPeter Zijlstra /* 188289a09141SPeter Zijlstra * NFS congestion size, scale with available memory. 188389a09141SPeter Zijlstra * 188489a09141SPeter Zijlstra * 64MB: 8192k 188589a09141SPeter Zijlstra * 128MB: 11585k 188689a09141SPeter Zijlstra * 256MB: 16384k 188789a09141SPeter Zijlstra * 512MB: 23170k 188889a09141SPeter Zijlstra * 1GB: 32768k 188989a09141SPeter Zijlstra * 2GB: 46340k 189089a09141SPeter Zijlstra * 4GB: 65536k 189189a09141SPeter Zijlstra * 8GB: 92681k 189289a09141SPeter Zijlstra * 16GB: 131072k 189389a09141SPeter Zijlstra * 189489a09141SPeter Zijlstra * This allows larger machines to have larger/more transfers. 189589a09141SPeter Zijlstra * Limit the default to 256M 189689a09141SPeter Zijlstra */ 189789a09141SPeter Zijlstra nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); 189889a09141SPeter Zijlstra if (nfs_congestion_kb > 256*1024) 189989a09141SPeter Zijlstra nfs_congestion_kb = 256*1024; 190089a09141SPeter Zijlstra 19011da177e4SLinus Torvalds return 0; 19023dd4765fSJeff Layton 19033dd4765fSJeff Layton out_destroy_commit_cache: 19043dd4765fSJeff Layton kmem_cache_destroy(nfs_cdata_cachep); 19053dd4765fSJeff Layton out_destroy_write_mempool: 19063dd4765fSJeff Layton mempool_destroy(nfs_wdata_mempool); 19073dd4765fSJeff Layton out_destroy_write_cache: 19083dd4765fSJeff Layton kmem_cache_destroy(nfs_wdata_cachep); 19093dd4765fSJeff Layton return -ENOMEM; 19101da177e4SLinus Torvalds } 19111da177e4SLinus Torvalds 1912266bee88SDavid Brownell void nfs_destroy_writepagecache(void) 19131da177e4SLinus Torvalds { 19141da177e4SLinus Torvalds mempool_destroy(nfs_commit_mempool); 19153dd4765fSJeff Layton kmem_cache_destroy(nfs_cdata_cachep); 19161da177e4SLinus Torvalds mempool_destroy(nfs_wdata_mempool); 19171a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_wdata_cachep); 19181da177e4SLinus Torvalds } 19191da177e4SLinus Torvalds 1920*4a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = { 1921*4a0de55cSAnna Schumaker .rw_alloc_header = nfs_writehdr_alloc, 1922*4a0de55cSAnna Schumaker .rw_free_header = nfs_writehdr_free, 1923*4a0de55cSAnna Schumaker }; 1924