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); 45788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops; 46061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops; 47f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops; 484a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops; 491da177e4SLinus Torvalds 50e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep; 513feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool; 520b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep; 531da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool; 541da177e4SLinus Torvalds 550b7c0153SFred Isaman struct nfs_commit_data *nfs_commitdata_alloc(void) 561da177e4SLinus Torvalds { 57192e501bSMel Gorman struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOIO); 5840859d7eSChuck Lever 591da177e4SLinus Torvalds if (p) { 601da177e4SLinus Torvalds memset(p, 0, sizeof(*p)); 611da177e4SLinus Torvalds INIT_LIST_HEAD(&p->pages); 621da177e4SLinus Torvalds } 631da177e4SLinus Torvalds return p; 641da177e4SLinus Torvalds } 65e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc); 661da177e4SLinus Torvalds 670b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p) 681da177e4SLinus Torvalds { 691da177e4SLinus Torvalds mempool_free(p, nfs_commit_mempool); 701da177e4SLinus Torvalds } 71e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free); 721da177e4SLinus Torvalds 731e7f3a48SWeston Andros Adamson static struct nfs_pgio_header *nfs_writehdr_alloc(void) 743feb2d49STrond Myklebust { 751e7f3a48SWeston Andros Adamson struct nfs_pgio_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO); 763feb2d49STrond Myklebust 774a0de55cSAnna Schumaker if (p) 783feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 793feb2d49STrond Myklebust return p; 803feb2d49STrond Myklebust } 813feb2d49STrond Myklebust 821e7f3a48SWeston Andros Adamson static void nfs_writehdr_free(struct nfs_pgio_header *hdr) 836c75dc0dSFred Isaman { 841e7f3a48SWeston Andros Adamson mempool_free(hdr, nfs_wdata_mempool); 853feb2d49STrond Myklebust } 861da177e4SLinus Torvalds 877b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error) 887b159fc1STrond Myklebust { 897b159fc1STrond Myklebust ctx->error = error; 907b159fc1STrond Myklebust smp_wmb(); 917b159fc1STrond Myklebust set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 927b159fc1STrond Myklebust } 937b159fc1STrond Myklebust 9429418aa4SMel Gorman static struct nfs_page * 9529418aa4SMel Gorman nfs_page_find_request_locked(struct nfs_inode *nfsi, struct page *page) 96277459d2STrond Myklebust { 97277459d2STrond Myklebust struct nfs_page *req = NULL; 98277459d2STrond Myklebust 9929418aa4SMel Gorman if (PagePrivate(page)) 100277459d2STrond Myklebust req = (struct nfs_page *)page_private(page); 10129418aa4SMel Gorman else if (unlikely(PageSwapCache(page))) { 10229418aa4SMel Gorman struct nfs_page *freq, *t; 10329418aa4SMel Gorman 10429418aa4SMel Gorman /* Linearly search the commit list for the correct req */ 10529418aa4SMel Gorman list_for_each_entry_safe(freq, t, &nfsi->commit_info.list, wb_list) { 10629418aa4SMel Gorman if (freq->wb_page == page) { 10729418aa4SMel Gorman req = freq; 10829418aa4SMel Gorman break; 109277459d2STrond Myklebust } 11029418aa4SMel Gorman } 11129418aa4SMel Gorman } 11229418aa4SMel Gorman 11329418aa4SMel Gorman if (req) 11429418aa4SMel Gorman kref_get(&req->wb_kref); 11529418aa4SMel Gorman 116277459d2STrond Myklebust return req; 117277459d2STrond Myklebust } 118277459d2STrond Myklebust 119277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page) 120277459d2STrond Myklebust { 121d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 122277459d2STrond Myklebust struct nfs_page *req = NULL; 123277459d2STrond Myklebust 124587142f8STrond Myklebust spin_lock(&inode->i_lock); 12529418aa4SMel Gorman req = nfs_page_find_request_locked(NFS_I(inode), page); 126587142f8STrond Myklebust spin_unlock(&inode->i_lock); 127277459d2STrond Myklebust return req; 128277459d2STrond Myklebust } 129277459d2STrond Myklebust 1301da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */ 1311da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) 1321da177e4SLinus Torvalds { 133d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 134a3d01454STrond Myklebust loff_t end, i_size; 135a3d01454STrond Myklebust pgoff_t end_index; 1361da177e4SLinus Torvalds 137a3d01454STrond Myklebust spin_lock(&inode->i_lock); 138a3d01454STrond Myklebust i_size = i_size_read(inode); 139a3d01454STrond Myklebust end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; 140d56b4ddfSMel Gorman if (i_size > 0 && page_file_index(page) < end_index) 141a3d01454STrond Myklebust goto out; 142d56b4ddfSMel Gorman end = page_file_offset(page) + ((loff_t)offset+count); 1431da177e4SLinus Torvalds if (i_size >= end) 144a3d01454STrond Myklebust goto out; 1451da177e4SLinus Torvalds i_size_write(inode, end); 146a3d01454STrond Myklebust nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); 147a3d01454STrond Myklebust out: 148a3d01454STrond Myklebust spin_unlock(&inode->i_lock); 1491da177e4SLinus Torvalds } 1501da177e4SLinus Torvalds 151a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */ 152a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page) 153a301b777STrond Myklebust { 154d56b4ddfSMel Gorman nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page)); 155a301b777STrond Myklebust } 156a301b777STrond Myklebust 157d72ddcbaSWeston Andros Adamson /* 158d72ddcbaSWeston Andros Adamson * nfs_page_group_search_locked 159d72ddcbaSWeston Andros Adamson * @head - head request of page group 160d72ddcbaSWeston Andros Adamson * @page_offset - offset into page 161d72ddcbaSWeston Andros Adamson * 162d72ddcbaSWeston Andros Adamson * Search page group with head @head to find a request that contains the 163d72ddcbaSWeston Andros Adamson * page offset @page_offset. 164d72ddcbaSWeston Andros Adamson * 165d72ddcbaSWeston Andros Adamson * Returns a pointer to the first matching nfs request, or NULL if no 166d72ddcbaSWeston Andros Adamson * match is found. 167d72ddcbaSWeston Andros Adamson * 168d72ddcbaSWeston Andros Adamson * Must be called with the page group lock held 169d72ddcbaSWeston Andros Adamson */ 170d72ddcbaSWeston Andros Adamson static struct nfs_page * 171d72ddcbaSWeston Andros Adamson nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset) 172d72ddcbaSWeston Andros Adamson { 173d72ddcbaSWeston Andros Adamson struct nfs_page *req; 174d72ddcbaSWeston Andros Adamson 175d72ddcbaSWeston Andros Adamson WARN_ON_ONCE(head != head->wb_head); 176d72ddcbaSWeston Andros Adamson WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_head->wb_flags)); 177d72ddcbaSWeston Andros Adamson 178d72ddcbaSWeston Andros Adamson req = head; 179d72ddcbaSWeston Andros Adamson do { 180d72ddcbaSWeston Andros Adamson if (page_offset >= req->wb_pgbase && 181d72ddcbaSWeston Andros Adamson page_offset < (req->wb_pgbase + req->wb_bytes)) 182d72ddcbaSWeston Andros Adamson return req; 183d72ddcbaSWeston Andros Adamson 184d72ddcbaSWeston Andros Adamson req = req->wb_this_page; 185d72ddcbaSWeston Andros Adamson } while (req != head); 186d72ddcbaSWeston Andros Adamson 187d72ddcbaSWeston Andros Adamson return NULL; 188d72ddcbaSWeston Andros Adamson } 189d72ddcbaSWeston Andros Adamson 190d72ddcbaSWeston Andros Adamson /* 191d72ddcbaSWeston Andros Adamson * nfs_page_group_covers_page 192d72ddcbaSWeston Andros Adamson * @head - head request of page group 193d72ddcbaSWeston Andros Adamson * 194d72ddcbaSWeston Andros Adamson * Return true if the page group with head @head covers the whole page, 195d72ddcbaSWeston Andros Adamson * returns false otherwise 196d72ddcbaSWeston Andros Adamson */ 197d72ddcbaSWeston Andros Adamson static bool nfs_page_group_covers_page(struct nfs_page *req) 198d72ddcbaSWeston Andros Adamson { 199d72ddcbaSWeston Andros Adamson struct nfs_page *tmp; 200d72ddcbaSWeston Andros Adamson unsigned int pos = 0; 201d72ddcbaSWeston Andros Adamson unsigned int len = nfs_page_length(req->wb_page); 202d72ddcbaSWeston Andros Adamson 203d72ddcbaSWeston Andros Adamson nfs_page_group_lock(req); 204d72ddcbaSWeston Andros Adamson 205d72ddcbaSWeston Andros Adamson do { 206d72ddcbaSWeston Andros Adamson tmp = nfs_page_group_search_locked(req->wb_head, pos); 207d72ddcbaSWeston Andros Adamson if (tmp) { 208d72ddcbaSWeston Andros Adamson /* no way this should happen */ 209d72ddcbaSWeston Andros Adamson WARN_ON_ONCE(tmp->wb_pgbase != pos); 210d72ddcbaSWeston Andros Adamson pos += tmp->wb_bytes - (pos - tmp->wb_pgbase); 211d72ddcbaSWeston Andros Adamson } 212d72ddcbaSWeston Andros Adamson } while (tmp && pos < len); 213d72ddcbaSWeston Andros Adamson 214d72ddcbaSWeston Andros Adamson nfs_page_group_unlock(req); 215d72ddcbaSWeston Andros Adamson WARN_ON_ONCE(pos > len); 216d72ddcbaSWeston Andros Adamson return pos == len; 217d72ddcbaSWeston Andros Adamson } 218d72ddcbaSWeston Andros Adamson 2191da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request 2201da177e4SLinus Torvalds * covers the full page. 2211da177e4SLinus Torvalds */ 222d72ddcbaSWeston Andros Adamson static void nfs_mark_uptodate(struct nfs_page *req) 2231da177e4SLinus Torvalds { 224d72ddcbaSWeston Andros Adamson if (PageUptodate(req->wb_page)) 2251da177e4SLinus Torvalds return; 226d72ddcbaSWeston Andros Adamson if (!nfs_page_group_covers_page(req)) 2271da177e4SLinus Torvalds return; 228d72ddcbaSWeston Andros Adamson SetPageUptodate(req->wb_page); 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc) 2321da177e4SLinus Torvalds { 2331da177e4SLinus Torvalds if (wbc->for_reclaim) 234c63c7b05STrond Myklebust return FLUSH_HIGHPRI | FLUSH_STABLE; 235b17621feSWu Fengguang if (wbc->for_kupdate || wbc->for_background) 236b31268acSTrond Myklebust return FLUSH_LOWPRI | FLUSH_COND_STABLE; 237b31268acSTrond Myklebust return FLUSH_COND_STABLE; 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds /* 24189a09141SPeter Zijlstra * NFS congestion control 24289a09141SPeter Zijlstra */ 24389a09141SPeter Zijlstra 24489a09141SPeter Zijlstra int nfs_congestion_kb; 24589a09141SPeter Zijlstra 24689a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) 24789a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH \ 24889a09141SPeter Zijlstra (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) 24989a09141SPeter Zijlstra 250deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page) 25189a09141SPeter Zijlstra { 252deed85e7STrond Myklebust struct nfs_server *nfss = NFS_SERVER(page_file_mapping(page)->host); 2535a6d41b3STrond Myklebust int ret = test_set_page_writeback(page); 2545a6d41b3STrond Myklebust 255deed85e7STrond Myklebust WARN_ON_ONCE(ret != 0); 25689a09141SPeter Zijlstra 257277866a0SPeter Zijlstra if (atomic_long_inc_return(&nfss->writeback) > 2588aa7e847SJens Axboe NFS_CONGESTION_ON_THRESH) { 2598aa7e847SJens Axboe set_bdi_congested(&nfss->backing_dev_info, 2608aa7e847SJens Axboe BLK_RW_ASYNC); 2618aa7e847SJens Axboe } 26289a09141SPeter Zijlstra } 26389a09141SPeter Zijlstra 26420633f04SWeston Andros Adamson static void nfs_end_page_writeback(struct nfs_page *req) 26589a09141SPeter Zijlstra { 26620633f04SWeston Andros Adamson struct inode *inode = page_file_mapping(req->wb_page)->host; 26789a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 26889a09141SPeter Zijlstra 26920633f04SWeston Andros Adamson if (!nfs_page_group_sync_on_bit(req, PG_WB_END)) 27020633f04SWeston Andros Adamson return; 27120633f04SWeston Andros Adamson 27220633f04SWeston Andros Adamson end_page_writeback(req->wb_page); 273c4dc4beeSPeter Zijlstra if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) 2748aa7e847SJens Axboe clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC); 27589a09141SPeter Zijlstra } 27689a09141SPeter Zijlstra 277cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock) 278e261f51fSTrond Myklebust { 279d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 280e261f51fSTrond Myklebust struct nfs_page *req; 281e261f51fSTrond Myklebust int ret; 282e261f51fSTrond Myklebust 283587142f8STrond Myklebust spin_lock(&inode->i_lock); 284e261f51fSTrond Myklebust for (;;) { 28529418aa4SMel Gorman req = nfs_page_find_request_locked(NFS_I(inode), page); 286074cc1deSTrond Myklebust if (req == NULL) 287074cc1deSTrond Myklebust break; 2887ad84aa9STrond Myklebust if (nfs_lock_request(req)) 289e261f51fSTrond Myklebust break; 290e261f51fSTrond Myklebust /* Note: If we hold the page lock, as is the case in nfs_writepage, 2917ad84aa9STrond Myklebust * then the call to nfs_lock_request() will always 292e261f51fSTrond Myklebust * succeed provided that someone hasn't already marked the 293e261f51fSTrond Myklebust * request as dirty (in which case we don't care). 294e261f51fSTrond Myklebust */ 295587142f8STrond Myklebust spin_unlock(&inode->i_lock); 296cfb506e1STrond Myklebust if (!nonblock) 297e261f51fSTrond Myklebust ret = nfs_wait_on_request(req); 298cfb506e1STrond Myklebust else 299cfb506e1STrond Myklebust ret = -EAGAIN; 300e261f51fSTrond Myklebust nfs_release_request(req); 301e261f51fSTrond Myklebust if (ret != 0) 302074cc1deSTrond Myklebust return ERR_PTR(ret); 303587142f8STrond Myklebust spin_lock(&inode->i_lock); 304e261f51fSTrond Myklebust } 305587142f8STrond Myklebust spin_unlock(&inode->i_lock); 306074cc1deSTrond Myklebust return req; 307612c9384STrond Myklebust } 308074cc1deSTrond Myklebust 309074cc1deSTrond Myklebust /* 310074cc1deSTrond Myklebust * Find an associated nfs write request, and prepare to flush it out 311074cc1deSTrond Myklebust * May return an error if the user signalled nfs_wait_on_request(). 312074cc1deSTrond Myklebust */ 313074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, 314cfb506e1STrond Myklebust struct page *page, bool nonblock) 315074cc1deSTrond Myklebust { 316074cc1deSTrond Myklebust struct nfs_page *req; 317074cc1deSTrond Myklebust int ret = 0; 318074cc1deSTrond Myklebust 319cfb506e1STrond Myklebust req = nfs_find_and_lock_request(page, nonblock); 320074cc1deSTrond Myklebust if (!req) 321074cc1deSTrond Myklebust goto out; 322074cc1deSTrond Myklebust ret = PTR_ERR(req); 323074cc1deSTrond Myklebust if (IS_ERR(req)) 324074cc1deSTrond Myklebust goto out; 325074cc1deSTrond Myklebust 326deed85e7STrond Myklebust nfs_set_page_writeback(page); 327deed85e7STrond Myklebust WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags)); 328074cc1deSTrond Myklebust 329deed85e7STrond Myklebust ret = 0; 330f8512ad0SFred Isaman if (!nfs_pageio_add_request(pgio, req)) { 331f8512ad0SFred Isaman nfs_redirty_request(req); 332074cc1deSTrond Myklebust ret = pgio->pg_error; 333f8512ad0SFred Isaman } 334074cc1deSTrond Myklebust out: 335074cc1deSTrond Myklebust return ret; 336e261f51fSTrond Myklebust } 337e261f51fSTrond Myklebust 338f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio) 339f758c885STrond Myklebust { 340d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 341cfb506e1STrond Myklebust int ret; 342f758c885STrond Myklebust 343f758c885STrond Myklebust nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); 344f758c885STrond Myklebust nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); 345f758c885STrond Myklebust 346d56b4ddfSMel Gorman nfs_pageio_cond_complete(pgio, page_file_index(page)); 3471b430beeSWu Fengguang ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE); 348cfb506e1STrond Myklebust if (ret == -EAGAIN) { 349cfb506e1STrond Myklebust redirty_page_for_writepage(wbc, page); 350cfb506e1STrond Myklebust ret = 0; 351cfb506e1STrond Myklebust } 352cfb506e1STrond Myklebust return ret; 353f758c885STrond Myklebust } 354f758c885STrond Myklebust 355e261f51fSTrond Myklebust /* 3561da177e4SLinus Torvalds * Write an mmapped page to the server. 3571da177e4SLinus Torvalds */ 3584d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc) 3591da177e4SLinus Torvalds { 360f758c885STrond Myklebust struct nfs_pageio_descriptor pgio; 361e261f51fSTrond Myklebust int err; 3621da177e4SLinus Torvalds 363a20c93e3SChristoph Hellwig nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc), 364a20c93e3SChristoph Hellwig false, &nfs_async_write_completion_ops); 365f758c885STrond Myklebust err = nfs_do_writepage(page, wbc, &pgio); 366f758c885STrond Myklebust nfs_pageio_complete(&pgio); 367f758c885STrond Myklebust if (err < 0) 3684d770ccfSTrond Myklebust return err; 369f758c885STrond Myklebust if (pgio.pg_error < 0) 370f758c885STrond Myklebust return pgio.pg_error; 371f758c885STrond Myklebust return 0; 3724d770ccfSTrond Myklebust } 3734d770ccfSTrond Myklebust 3744d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc) 3754d770ccfSTrond Myklebust { 376f758c885STrond Myklebust int ret; 3774d770ccfSTrond Myklebust 378f758c885STrond Myklebust ret = nfs_writepage_locked(page, wbc); 3791da177e4SLinus Torvalds unlock_page(page); 380f758c885STrond Myklebust return ret; 381f758c885STrond Myklebust } 382f758c885STrond Myklebust 383f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) 384f758c885STrond Myklebust { 385f758c885STrond Myklebust int ret; 386f758c885STrond Myklebust 387f758c885STrond Myklebust ret = nfs_do_writepage(page, wbc, data); 388f758c885STrond Myklebust unlock_page(page); 389f758c885STrond Myklebust return ret; 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) 3931da177e4SLinus Torvalds { 3941da177e4SLinus Torvalds struct inode *inode = mapping->host; 39572cb77f4STrond Myklebust unsigned long *bitlock = &NFS_I(inode)->flags; 396c63c7b05STrond Myklebust struct nfs_pageio_descriptor pgio; 3971da177e4SLinus Torvalds int err; 3981da177e4SLinus Torvalds 39972cb77f4STrond Myklebust /* Stop dirtying of new pages while we sync */ 40072cb77f4STrond Myklebust err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING, 40172cb77f4STrond Myklebust nfs_wait_bit_killable, TASK_KILLABLE); 40272cb77f4STrond Myklebust if (err) 40372cb77f4STrond Myklebust goto out_err; 40472cb77f4STrond Myklebust 40591d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); 40691d5b470SChuck Lever 407a20c93e3SChristoph Hellwig nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false, 408a20c93e3SChristoph Hellwig &nfs_async_write_completion_ops); 409f758c885STrond Myklebust err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); 410c63c7b05STrond Myklebust nfs_pageio_complete(&pgio); 41172cb77f4STrond Myklebust 41272cb77f4STrond Myklebust clear_bit_unlock(NFS_INO_FLUSHING, bitlock); 4134e857c58SPeter Zijlstra smp_mb__after_atomic(); 41472cb77f4STrond Myklebust wake_up_bit(bitlock, NFS_INO_FLUSHING); 41572cb77f4STrond Myklebust 416f758c885STrond Myklebust if (err < 0) 41772cb77f4STrond Myklebust goto out_err; 41872cb77f4STrond Myklebust err = pgio.pg_error; 41972cb77f4STrond Myklebust if (err < 0) 42072cb77f4STrond Myklebust goto out_err; 421c63c7b05STrond Myklebust return 0; 42272cb77f4STrond Myklebust out_err: 42372cb77f4STrond Myklebust return err; 4241da177e4SLinus Torvalds } 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds /* 4271da177e4SLinus Torvalds * Insert a write request into an inode 4281da177e4SLinus Torvalds */ 429d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req) 4301da177e4SLinus Torvalds { 4311da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 432e7d39069STrond Myklebust 4332bfc6e56SWeston Andros Adamson WARN_ON_ONCE(req->wb_this_page != req); 4342bfc6e56SWeston Andros Adamson 435e7d39069STrond Myklebust /* Lock the request! */ 4367ad84aa9STrond Myklebust nfs_lock_request(req); 437e7d39069STrond Myklebust 438e7d39069STrond Myklebust spin_lock(&inode->i_lock); 439011e2a7fSBryan Schumaker if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE)) 440a9a4a87aSTrond Myklebust inode->i_version++; 44129418aa4SMel Gorman /* 44229418aa4SMel Gorman * Swap-space should not get truncated. Hence no need to plug the race 44329418aa4SMel Gorman * with invalidate/truncate. 44429418aa4SMel Gorman */ 44529418aa4SMel Gorman if (likely(!PageSwapCache(req->wb_page))) { 4462df485a7STrond Myklebust set_bit(PG_MAPPED, &req->wb_flags); 447deb7d638STrond Myklebust SetPagePrivate(req->wb_page); 448277459d2STrond Myklebust set_page_private(req->wb_page, (unsigned long)req); 44929418aa4SMel Gorman } 4501da177e4SLinus Torvalds nfsi->npages++; 4512bfc6e56SWeston Andros Adamson set_bit(PG_INODE_REF, &req->wb_flags); 452c03b4024STrond Myklebust kref_get(&req->wb_kref); 453e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds /* 45789a09141SPeter Zijlstra * Remove a write request from an inode 4581da177e4SLinus Torvalds */ 4591da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req) 4601da177e4SLinus Torvalds { 4613d4ff43dSAl Viro struct inode *inode = req->wb_context->dentry->d_inode; 4621da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 46320633f04SWeston Andros Adamson struct nfs_page *head; 46420633f04SWeston Andros Adamson 46520633f04SWeston Andros Adamson if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) { 46620633f04SWeston Andros Adamson head = req->wb_head; 4671da177e4SLinus Torvalds 468587142f8STrond Myklebust spin_lock(&inode->i_lock); 46920633f04SWeston Andros Adamson if (likely(!PageSwapCache(head->wb_page))) { 47020633f04SWeston Andros Adamson set_page_private(head->wb_page, 0); 47120633f04SWeston Andros Adamson ClearPagePrivate(head->wb_page); 47220633f04SWeston Andros Adamson clear_bit(PG_MAPPED, &head->wb_flags); 47329418aa4SMel Gorman } 4741da177e4SLinus Torvalds nfsi->npages--; 475587142f8STrond Myklebust spin_unlock(&inode->i_lock); 47620633f04SWeston Andros Adamson } 4771da177e4SLinus Torvalds nfs_release_request(req); 4781da177e4SLinus Torvalds } 4791da177e4SLinus Torvalds 48061822ab5STrond Myklebust static void 4816d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req) 48261822ab5STrond Myklebust { 48361822ab5STrond Myklebust __set_page_dirty_nobuffers(req->wb_page); 48461822ab5STrond Myklebust } 48561822ab5STrond Myklebust 48689d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4) 4878dd37758STrond Myklebust /** 4888dd37758STrond Myklebust * nfs_request_add_commit_list - add request to a commit list 4898dd37758STrond Myklebust * @req: pointer to a struct nfs_page 490ea2cf228SFred Isaman * @dst: commit list head 491ea2cf228SFred Isaman * @cinfo: holds list lock and accounting info 4928dd37758STrond Myklebust * 493ea2cf228SFred Isaman * This sets the PG_CLEAN bit, updates the cinfo count of 4948dd37758STrond Myklebust * number of outstanding requests requiring a commit as well as 4958dd37758STrond Myklebust * the MM page stats. 4968dd37758STrond Myklebust * 497ea2cf228SFred Isaman * The caller must _not_ hold the cinfo->lock, but must be 4988dd37758STrond Myklebust * holding the nfs_page lock. 4998dd37758STrond Myklebust */ 5008dd37758STrond Myklebust void 501ea2cf228SFred Isaman nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst, 502ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 5038dd37758STrond Myklebust { 5048dd37758STrond Myklebust set_bit(PG_CLEAN, &(req)->wb_flags); 505ea2cf228SFred Isaman spin_lock(cinfo->lock); 506ea2cf228SFred Isaman nfs_list_add_request(req, dst); 507ea2cf228SFred Isaman cinfo->mds->ncommit++; 508ea2cf228SFred Isaman spin_unlock(cinfo->lock); 50956f9cd68SFred Isaman if (!cinfo->dreq) { 5108dd37758STrond Myklebust inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 511d56b4ddfSMel Gorman inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info, 51256f9cd68SFred Isaman BDI_RECLAIMABLE); 51356f9cd68SFred Isaman __mark_inode_dirty(req->wb_context->dentry->d_inode, 51456f9cd68SFred Isaman I_DIRTY_DATASYNC); 51556f9cd68SFred Isaman } 5168dd37758STrond Myklebust } 5178dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list); 5188dd37758STrond Myklebust 5198dd37758STrond Myklebust /** 5208dd37758STrond Myklebust * nfs_request_remove_commit_list - Remove request from a commit list 5218dd37758STrond Myklebust * @req: pointer to a nfs_page 522ea2cf228SFred Isaman * @cinfo: holds list lock and accounting info 5238dd37758STrond Myklebust * 524ea2cf228SFred Isaman * This clears the PG_CLEAN bit, and updates the cinfo's count of 5258dd37758STrond Myklebust * number of outstanding requests requiring a commit 5268dd37758STrond Myklebust * It does not update the MM page stats. 5278dd37758STrond Myklebust * 528ea2cf228SFred Isaman * The caller _must_ hold the cinfo->lock and the nfs_page lock. 5298dd37758STrond Myklebust */ 5308dd37758STrond Myklebust void 531ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req, 532ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 5338dd37758STrond Myklebust { 5348dd37758STrond Myklebust if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) 5358dd37758STrond Myklebust return; 5368dd37758STrond Myklebust nfs_list_remove_request(req); 537ea2cf228SFred Isaman cinfo->mds->ncommit--; 5388dd37758STrond Myklebust } 5398dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list); 5408dd37758STrond Myklebust 541ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, 542ea2cf228SFred Isaman struct inode *inode) 543ea2cf228SFred Isaman { 544ea2cf228SFred Isaman cinfo->lock = &inode->i_lock; 545ea2cf228SFred Isaman cinfo->mds = &NFS_I(inode)->commit_info; 546ea2cf228SFred Isaman cinfo->ds = pnfs_get_ds_info(inode); 547b359f9d0SFred Isaman cinfo->dreq = NULL; 548f453a54aSFred Isaman cinfo->completion_ops = &nfs_commit_completion_ops; 549ea2cf228SFred Isaman } 550ea2cf228SFred Isaman 551ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo, 552ea2cf228SFred Isaman struct inode *inode, 553ea2cf228SFred Isaman struct nfs_direct_req *dreq) 554ea2cf228SFred Isaman { 5551763da12SFred Isaman if (dreq) 5561763da12SFred Isaman nfs_init_cinfo_from_dreq(cinfo, dreq); 5571763da12SFred Isaman else 558ea2cf228SFred Isaman nfs_init_cinfo_from_inode(cinfo, inode); 559ea2cf228SFred Isaman } 560ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo); 5618dd37758STrond Myklebust 5621da177e4SLinus Torvalds /* 5631da177e4SLinus Torvalds * Add a request to the inode's commit list. 5641da177e4SLinus Torvalds */ 5651763da12SFred Isaman void 566ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, 567ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 5681da177e4SLinus Torvalds { 569ea2cf228SFred Isaman if (pnfs_mark_request_commit(req, lseg, cinfo)) 5708dd37758STrond Myklebust return; 571ea2cf228SFred Isaman nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo); 5721da177e4SLinus Torvalds } 5738e821cadSTrond Myklebust 574d6d6dc7cSFred Isaman static void 575d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page) 576e468bae9STrond Myklebust { 577e468bae9STrond Myklebust dec_zone_page_state(page, NR_UNSTABLE_NFS); 578d56b4ddfSMel Gorman dec_bdi_stat(page_file_mapping(page)->backing_dev_info, BDI_RECLAIMABLE); 579e468bae9STrond Myklebust } 580d6d6dc7cSFred Isaman 5818dd37758STrond Myklebust static void 582d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req) 583d6d6dc7cSFred Isaman { 5848dd37758STrond Myklebust if (test_bit(PG_CLEAN, &req->wb_flags)) { 5858dd37758STrond Myklebust struct inode *inode = req->wb_context->dentry->d_inode; 586ea2cf228SFred Isaman struct nfs_commit_info cinfo; 587d6d6dc7cSFred Isaman 588ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, inode); 589ea2cf228SFred Isaman if (!pnfs_clear_request_commit(req, &cinfo)) { 590ea2cf228SFred Isaman spin_lock(cinfo.lock); 591ea2cf228SFred Isaman nfs_request_remove_commit_list(req, &cinfo); 592ea2cf228SFred Isaman spin_unlock(cinfo.lock); 593d6d6dc7cSFred Isaman } 5948dd37758STrond Myklebust nfs_clear_page_commit(req->wb_page); 5958dd37758STrond Myklebust } 596e468bae9STrond Myklebust } 597e468bae9STrond Myklebust 598d45f60c6SWeston Andros Adamson int nfs_write_need_commit(struct nfs_pgio_header *hdr) 5998e821cadSTrond Myklebust { 600c65e6254SWeston Andros Adamson if (hdr->verf.committed == NFS_DATA_SYNC) 601d45f60c6SWeston Andros Adamson return hdr->lseg == NULL; 602c65e6254SWeston Andros Adamson return hdr->verf.committed != NFS_FILE_SYNC; 6038e821cadSTrond Myklebust } 6048e821cadSTrond Myklebust 6058e821cadSTrond Myklebust #else 60668cd6fa4SBryan Schumaker static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, 60768cd6fa4SBryan Schumaker struct inode *inode) 60868cd6fa4SBryan Schumaker { 60968cd6fa4SBryan Schumaker } 61068cd6fa4SBryan Schumaker 61168cd6fa4SBryan Schumaker void nfs_init_cinfo(struct nfs_commit_info *cinfo, 61268cd6fa4SBryan Schumaker struct inode *inode, 61368cd6fa4SBryan Schumaker struct nfs_direct_req *dreq) 61468cd6fa4SBryan Schumaker { 61568cd6fa4SBryan Schumaker } 61668cd6fa4SBryan Schumaker 6171763da12SFred Isaman void 618ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, 619ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 6208e821cadSTrond Myklebust { 6218e821cadSTrond Myklebust } 6228e821cadSTrond Myklebust 6238dd37758STrond Myklebust static void 624e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req) 625e468bae9STrond Myklebust { 626e468bae9STrond Myklebust } 627e468bae9STrond Myklebust 628d45f60c6SWeston Andros Adamson int nfs_write_need_commit(struct nfs_pgio_header *hdr) 6298e821cadSTrond Myklebust { 6308e821cadSTrond Myklebust return 0; 6318e821cadSTrond Myklebust } 6328e821cadSTrond Myklebust 6331da177e4SLinus Torvalds #endif 6341da177e4SLinus Torvalds 635061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr) 6366c75dc0dSFred Isaman { 637ea2cf228SFred Isaman struct nfs_commit_info cinfo; 6386c75dc0dSFred Isaman unsigned long bytes = 0; 6392bfc6e56SWeston Andros Adamson bool do_destroy; 6406c75dc0dSFred Isaman 6416c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 6426c75dc0dSFred Isaman goto out; 643ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, hdr->inode); 6446c75dc0dSFred Isaman while (!list_empty(&hdr->pages)) { 6456c75dc0dSFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 6466c75dc0dSFred Isaman 6476c75dc0dSFred Isaman bytes += req->wb_bytes; 6486c75dc0dSFred Isaman nfs_list_remove_request(req); 6496c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && 6506c75dc0dSFred Isaman (hdr->good_bytes < bytes)) { 651d1182b33STrond Myklebust nfs_set_pageerror(req->wb_page); 6526c75dc0dSFred Isaman nfs_context_set_write_error(req->wb_context, hdr->error); 6536c75dc0dSFred Isaman goto remove_req; 6546c75dc0dSFred Isaman } 655c65e6254SWeston Andros Adamson if (nfs_write_need_commit(hdr)) { 656f79d06f5SAnna Schumaker memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf)); 657ea2cf228SFred Isaman nfs_mark_request_commit(req, hdr->lseg, &cinfo); 6586c75dc0dSFred Isaman goto next; 6596c75dc0dSFred Isaman } 6606c75dc0dSFred Isaman remove_req: 6616c75dc0dSFred Isaman nfs_inode_remove_request(req); 6626c75dc0dSFred Isaman next: 6631d1afcbcSTrond Myklebust nfs_unlock_request(req); 66420633f04SWeston Andros Adamson nfs_end_page_writeback(req); 665c65e6254SWeston Andros Adamson do_destroy = !nfs_write_need_commit(hdr); 6663aff4ebbSTrond Myklebust nfs_release_request(req); 6676c75dc0dSFred Isaman } 6686c75dc0dSFred Isaman out: 6696c75dc0dSFred Isaman hdr->release(hdr); 6706c75dc0dSFred Isaman } 6716c75dc0dSFred Isaman 67289d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4) 673ce59515cSAnna Schumaker unsigned long 674ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo) 675fb8a1f11STrond Myklebust { 676ea2cf228SFred Isaman return cinfo->mds->ncommit; 677fb8a1f11STrond Myklebust } 678fb8a1f11STrond Myklebust 679ea2cf228SFred Isaman /* cinfo->lock held by caller */ 6801763da12SFred Isaman int 681ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst, 682ea2cf228SFred Isaman struct nfs_commit_info *cinfo, int max) 683d6d6dc7cSFred Isaman { 684d6d6dc7cSFred Isaman struct nfs_page *req, *tmp; 685d6d6dc7cSFred Isaman int ret = 0; 686d6d6dc7cSFred Isaman 687d6d6dc7cSFred Isaman list_for_each_entry_safe(req, tmp, src, wb_list) { 6888dd37758STrond Myklebust if (!nfs_lock_request(req)) 6898dd37758STrond Myklebust continue; 6907ad84aa9STrond Myklebust kref_get(&req->wb_kref); 691ea2cf228SFred Isaman if (cond_resched_lock(cinfo->lock)) 6923b3be88dSTrond Myklebust list_safe_reset_next(req, tmp, wb_list); 693ea2cf228SFred Isaman nfs_request_remove_commit_list(req, cinfo); 6948dd37758STrond Myklebust nfs_list_add_request(req, dst); 695d6d6dc7cSFred Isaman ret++; 6961763da12SFred Isaman if ((ret == max) && !cinfo->dreq) 697d6d6dc7cSFred Isaman break; 698d6d6dc7cSFred Isaman } 699d6d6dc7cSFred Isaman return ret; 700d6d6dc7cSFred Isaman } 701d6d6dc7cSFred Isaman 7021da177e4SLinus Torvalds /* 7031da177e4SLinus Torvalds * nfs_scan_commit - Scan an inode for commit requests 7041da177e4SLinus Torvalds * @inode: NFS inode to scan 705ea2cf228SFred Isaman * @dst: mds destination list 706ea2cf228SFred Isaman * @cinfo: mds and ds lists of reqs ready to commit 7071da177e4SLinus Torvalds * 7081da177e4SLinus Torvalds * Moves requests from the inode's 'commit' request list. 7091da177e4SLinus Torvalds * The requests are *not* checked to ensure that they form a contiguous set. 7101da177e4SLinus Torvalds */ 7111763da12SFred Isaman int 712ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst, 713ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 7141da177e4SLinus Torvalds { 715d6d6dc7cSFred Isaman int ret = 0; 716fb8a1f11STrond Myklebust 717ea2cf228SFred Isaman spin_lock(cinfo->lock); 718ea2cf228SFred Isaman if (cinfo->mds->ncommit > 0) { 7198dd37758STrond Myklebust const int max = INT_MAX; 720d6d6dc7cSFred Isaman 721ea2cf228SFred Isaman ret = nfs_scan_commit_list(&cinfo->mds->list, dst, 722ea2cf228SFred Isaman cinfo, max); 723ea2cf228SFred Isaman ret += pnfs_scan_commit_lists(inode, cinfo, max - ret); 724d6d6dc7cSFred Isaman } 725ea2cf228SFred Isaman spin_unlock(cinfo->lock); 726ff778d02STrond Myklebust return ret; 7271da177e4SLinus Torvalds } 728d6d6dc7cSFred Isaman 729c42de9ddSTrond Myklebust #else 730ce59515cSAnna Schumaker unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo) 731fb8a1f11STrond Myklebust { 732fb8a1f11STrond Myklebust return 0; 733fb8a1f11STrond Myklebust } 734fb8a1f11STrond Myklebust 7351763da12SFred Isaman int nfs_scan_commit(struct inode *inode, struct list_head *dst, 736ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 737c42de9ddSTrond Myklebust { 738c42de9ddSTrond Myklebust return 0; 739c42de9ddSTrond Myklebust } 7401da177e4SLinus Torvalds #endif 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds /* 743e7d39069STrond Myklebust * Search for an existing write request, and attempt to update 744e7d39069STrond Myklebust * it to reflect a new dirty region on a given page. 7451da177e4SLinus Torvalds * 746e7d39069STrond Myklebust * If the attempt fails, then the existing request is flushed out 747e7d39069STrond Myklebust * to disk. 7481da177e4SLinus Torvalds */ 749e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode, 750e7d39069STrond Myklebust struct page *page, 751e7d39069STrond Myklebust unsigned int offset, 752e7d39069STrond Myklebust unsigned int bytes) 7531da177e4SLinus Torvalds { 754e7d39069STrond Myklebust struct nfs_page *req; 755e7d39069STrond Myklebust unsigned int rqend; 756e7d39069STrond Myklebust unsigned int end; 7571da177e4SLinus Torvalds int error; 758277459d2STrond Myklebust 759e7d39069STrond Myklebust if (!PagePrivate(page)) 760e7d39069STrond Myklebust return NULL; 761e7d39069STrond Myklebust 762e7d39069STrond Myklebust end = offset + bytes; 763e7d39069STrond Myklebust spin_lock(&inode->i_lock); 764e7d39069STrond Myklebust 765e7d39069STrond Myklebust for (;;) { 76629418aa4SMel Gorman req = nfs_page_find_request_locked(NFS_I(inode), page); 767e7d39069STrond Myklebust if (req == NULL) 768e7d39069STrond Myklebust goto out_unlock; 769e7d39069STrond Myklebust 7702bfc6e56SWeston Andros Adamson /* should be handled by nfs_flush_incompatible */ 7712bfc6e56SWeston Andros Adamson WARN_ON_ONCE(req->wb_head != req); 7722bfc6e56SWeston Andros Adamson WARN_ON_ONCE(req->wb_this_page != req); 7732bfc6e56SWeston Andros Adamson 774e7d39069STrond Myklebust rqend = req->wb_offset + req->wb_bytes; 775e7d39069STrond Myklebust /* 776e7d39069STrond Myklebust * Tell the caller to flush out the request if 777e7d39069STrond Myklebust * the offsets are non-contiguous. 778e7d39069STrond Myklebust * Note: nfs_flush_incompatible() will already 779e7d39069STrond Myklebust * have flushed out requests having wrong owners. 780e7d39069STrond Myklebust */ 781e468bae9STrond Myklebust if (offset > rqend 782e7d39069STrond Myklebust || end < req->wb_offset) 783e7d39069STrond Myklebust goto out_flushme; 784e7d39069STrond Myklebust 7857ad84aa9STrond Myklebust if (nfs_lock_request(req)) 786e7d39069STrond Myklebust break; 787e7d39069STrond Myklebust 788e7d39069STrond Myklebust /* The request is locked, so wait and then retry */ 789587142f8STrond Myklebust spin_unlock(&inode->i_lock); 7901da177e4SLinus Torvalds error = nfs_wait_on_request(req); 7911da177e4SLinus Torvalds nfs_release_request(req); 792e7d39069STrond Myklebust if (error != 0) 793e7d39069STrond Myklebust goto out_err; 794e7d39069STrond Myklebust spin_lock(&inode->i_lock); 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds /* Okay, the request matches. Update the region */ 7981da177e4SLinus Torvalds if (offset < req->wb_offset) { 7991da177e4SLinus Torvalds req->wb_offset = offset; 8001da177e4SLinus Torvalds req->wb_pgbase = offset; 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds if (end > rqend) 8031da177e4SLinus Torvalds req->wb_bytes = end - req->wb_offset; 804e7d39069STrond Myklebust else 805e7d39069STrond Myklebust req->wb_bytes = rqend - req->wb_offset; 806e7d39069STrond Myklebust out_unlock: 807e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 808ca138f36SFred Isaman if (req) 8098dd37758STrond Myklebust nfs_clear_request_commit(req); 810e7d39069STrond Myklebust return req; 811e7d39069STrond Myklebust out_flushme: 812e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 813e7d39069STrond Myklebust nfs_release_request(req); 814e7d39069STrond Myklebust error = nfs_wb_page(inode, page); 815e7d39069STrond Myklebust out_err: 816e7d39069STrond Myklebust return ERR_PTR(error); 817e7d39069STrond Myklebust } 8181da177e4SLinus Torvalds 819e7d39069STrond Myklebust /* 820e7d39069STrond Myklebust * Try to update an existing write request, or create one if there is none. 821e7d39069STrond Myklebust * 822e7d39069STrond Myklebust * Note: Should always be called with the Page Lock held to prevent races 823e7d39069STrond Myklebust * if we have to add a new request. Also assumes that the caller has 824e7d39069STrond Myklebust * already called nfs_flush_incompatible() if necessary. 825e7d39069STrond Myklebust */ 826e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, 827e7d39069STrond Myklebust struct page *page, unsigned int offset, unsigned int bytes) 828e7d39069STrond Myklebust { 829d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 830e7d39069STrond Myklebust struct nfs_page *req; 831e7d39069STrond Myklebust 832e7d39069STrond Myklebust req = nfs_try_to_update_request(inode, page, offset, bytes); 833e7d39069STrond Myklebust if (req != NULL) 834e7d39069STrond Myklebust goto out; 8352bfc6e56SWeston Andros Adamson req = nfs_create_request(ctx, page, NULL, offset, bytes); 836e7d39069STrond Myklebust if (IS_ERR(req)) 837e7d39069STrond Myklebust goto out; 838d6d6dc7cSFred Isaman nfs_inode_add_request(inode, req); 839efc91ed0STrond Myklebust out: 84061e930a9STrond Myklebust return req; 8411da177e4SLinus Torvalds } 8421da177e4SLinus Torvalds 843e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, 844e7d39069STrond Myklebust unsigned int offset, unsigned int count) 845e7d39069STrond Myklebust { 846e7d39069STrond Myklebust struct nfs_page *req; 847e7d39069STrond Myklebust 848e7d39069STrond Myklebust req = nfs_setup_write_request(ctx, page, offset, count); 849e7d39069STrond Myklebust if (IS_ERR(req)) 850e7d39069STrond Myklebust return PTR_ERR(req); 851e7d39069STrond Myklebust /* Update file length */ 852e7d39069STrond Myklebust nfs_grow_file(page, offset, count); 853d72ddcbaSWeston Andros Adamson nfs_mark_uptodate(req); 854a6305ddbSTrond Myklebust nfs_mark_request_dirty(req); 8551d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 856e7d39069STrond Myklebust return 0; 857e7d39069STrond Myklebust } 858e7d39069STrond Myklebust 8591da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page) 8601da177e4SLinus Torvalds { 861cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 8622a369153STrond Myklebust struct nfs_lock_context *l_ctx; 8631da177e4SLinus Torvalds struct nfs_page *req; 8641a54533eSTrond Myklebust int do_flush, status; 8651da177e4SLinus Torvalds /* 8661da177e4SLinus Torvalds * Look for a request corresponding to this page. If there 8671da177e4SLinus Torvalds * is one, and it belongs to another file, we flush it out 8681da177e4SLinus Torvalds * before we try to copy anything into the page. Do this 8691da177e4SLinus Torvalds * due to the lack of an ACCESS-type call in NFSv2. 8701da177e4SLinus Torvalds * Also do the same if we find a request from an existing 8711da177e4SLinus Torvalds * dropped page. 8721da177e4SLinus Torvalds */ 8731a54533eSTrond Myklebust do { 874277459d2STrond Myklebust req = nfs_page_find_request(page); 8751a54533eSTrond Myklebust if (req == NULL) 8761a54533eSTrond Myklebust return 0; 8772a369153STrond Myklebust l_ctx = req->wb_lock_context; 8782a369153STrond Myklebust do_flush = req->wb_page != page || req->wb_context != ctx; 8792bfc6e56SWeston Andros Adamson /* for now, flush if more than 1 request in page_group */ 8802bfc6e56SWeston Andros Adamson do_flush |= req->wb_this_page != req; 8810f1d2605STrond Myklebust if (l_ctx && ctx->dentry->d_inode->i_flock != NULL) { 8822a369153STrond Myklebust do_flush |= l_ctx->lockowner.l_owner != current->files 8832a369153STrond Myklebust || l_ctx->lockowner.l_pid != current->tgid; 8842a369153STrond Myklebust } 8851da177e4SLinus Torvalds nfs_release_request(req); 8861a54533eSTrond Myklebust if (!do_flush) 8871a54533eSTrond Myklebust return 0; 888d56b4ddfSMel Gorman status = nfs_wb_page(page_file_mapping(page)->host, page); 8891a54533eSTrond Myklebust } while (status == 0); 8901a54533eSTrond Myklebust return status; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds /* 894dc24826bSAndy Adamson * Avoid buffered writes when a open context credential's key would 895dc24826bSAndy Adamson * expire soon. 896dc24826bSAndy Adamson * 897dc24826bSAndy Adamson * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL. 898dc24826bSAndy Adamson * 899dc24826bSAndy Adamson * Return 0 and set a credential flag which triggers the inode to flush 900dc24826bSAndy Adamson * and performs NFS_FILE_SYNC writes if the key will expired within 901dc24826bSAndy Adamson * RPC_KEY_EXPIRE_TIMEO. 902dc24826bSAndy Adamson */ 903dc24826bSAndy Adamson int 904dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode) 905dc24826bSAndy Adamson { 906dc24826bSAndy Adamson struct nfs_open_context *ctx = nfs_file_open_context(filp); 907dc24826bSAndy Adamson struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth; 908dc24826bSAndy Adamson 909dc24826bSAndy Adamson return rpcauth_key_timeout_notify(auth, ctx->cred); 910dc24826bSAndy Adamson } 911dc24826bSAndy Adamson 912dc24826bSAndy Adamson /* 913dc24826bSAndy Adamson * Test if the open context credential key is marked to expire soon. 914dc24826bSAndy Adamson */ 915dc24826bSAndy Adamson bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx) 916dc24826bSAndy Adamson { 917dc24826bSAndy Adamson return rpcauth_cred_key_to_expire(ctx->cred); 918dc24826bSAndy Adamson } 919dc24826bSAndy Adamson 920dc24826bSAndy Adamson /* 9215d47a356STrond Myklebust * If the page cache is marked as unsafe or invalid, then we can't rely on 9225d47a356STrond Myklebust * the PageUptodate() flag. In this case, we will need to turn off 9235d47a356STrond Myklebust * write optimisations that depend on the page contents being correct. 9245d47a356STrond Myklebust */ 9258d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode) 9265d47a356STrond Myklebust { 927d529ef83SJeff Layton struct nfs_inode *nfsi = NFS_I(inode); 928d529ef83SJeff Layton 9298d197a56STrond Myklebust if (nfs_have_delegated_attributes(inode)) 9308d197a56STrond Myklebust goto out; 93118dd78c4SScott Mayhew if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) 932d529ef83SJeff Layton return false; 9334db72b40SJeff Layton smp_rmb(); 934d529ef83SJeff Layton if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags)) 9358d197a56STrond Myklebust return false; 9368d197a56STrond Myklebust out: 93718dd78c4SScott Mayhew if (nfsi->cache_validity & NFS_INO_INVALID_DATA) 93818dd78c4SScott Mayhew return false; 9398d197a56STrond Myklebust return PageUptodate(page) != 0; 9405d47a356STrond Myklebust } 9415d47a356STrond Myklebust 942c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or 943c7559663SScott Mayhew * if we have the whole file locked for writing), it may be more efficient to 944c7559663SScott Mayhew * extend the write to cover the entire page in order to avoid fragmentation 945c7559663SScott Mayhew * inefficiencies. 946c7559663SScott Mayhew * 947263b4509SScott Mayhew * If the file is opened for synchronous writes then we can just skip the rest 948263b4509SScott Mayhew * of the checks. 949c7559663SScott Mayhew */ 950c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode) 951c7559663SScott Mayhew { 952c7559663SScott Mayhew if (file->f_flags & O_DSYNC) 953c7559663SScott Mayhew return 0; 954263b4509SScott Mayhew if (!nfs_write_pageuptodate(page, inode)) 955263b4509SScott Mayhew return 0; 956c7559663SScott Mayhew if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE)) 957c7559663SScott Mayhew return 1; 958263b4509SScott Mayhew if (inode->i_flock == NULL || (inode->i_flock->fl_start == 0 && 959c7559663SScott Mayhew inode->i_flock->fl_end == OFFSET_MAX && 960263b4509SScott Mayhew inode->i_flock->fl_type != F_RDLCK)) 961c7559663SScott Mayhew return 1; 962c7559663SScott Mayhew return 0; 963c7559663SScott Mayhew } 964c7559663SScott Mayhew 9655d47a356STrond Myklebust /* 9661da177e4SLinus Torvalds * Update and possibly write a cached page of an NFS file. 9671da177e4SLinus Torvalds * 9681da177e4SLinus Torvalds * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad 9691da177e4SLinus Torvalds * things with a page scheduled for an RPC call (e.g. invalidate it). 9701da177e4SLinus Torvalds */ 9711da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page, 9721da177e4SLinus Torvalds unsigned int offset, unsigned int count) 9731da177e4SLinus Torvalds { 974cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 975d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 9761da177e4SLinus Torvalds int status = 0; 9771da177e4SLinus Torvalds 97891d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 97991d5b470SChuck Lever 9806de1472fSAl Viro dprintk("NFS: nfs_updatepage(%pD2 %d@%lld)\n", 9816de1472fSAl Viro file, count, (long long)(page_file_offset(page) + offset)); 9821da177e4SLinus Torvalds 983c7559663SScott Mayhew if (nfs_can_extend_write(file, page, inode)) { 98449a70f27STrond Myklebust count = max(count + offset, nfs_page_length(page)); 9851da177e4SLinus Torvalds offset = 0; 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 988e21195a7STrond Myklebust status = nfs_writepage_setup(ctx, page, offset, count); 98903fa9e84STrond Myklebust if (status < 0) 99003fa9e84STrond Myklebust nfs_set_pageerror(page); 99159b7c05fSTrond Myklebust else 99259b7c05fSTrond Myklebust __set_page_dirty_nobuffers(page); 9931da177e4SLinus Torvalds 99448186c7dSChuck Lever dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", 9951da177e4SLinus Torvalds status, (long long)i_size_read(inode)); 9961da177e4SLinus Torvalds return status; 9971da177e4SLinus Torvalds } 9981da177e4SLinus Torvalds 9993ff7576dSTrond Myklebust static int flush_task_priority(int how) 10001da177e4SLinus Torvalds { 10011da177e4SLinus Torvalds switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { 10021da177e4SLinus Torvalds case FLUSH_HIGHPRI: 10031da177e4SLinus Torvalds return RPC_PRIORITY_HIGH; 10041da177e4SLinus Torvalds case FLUSH_LOWPRI: 10051da177e4SLinus Torvalds return RPC_PRIORITY_LOW; 10061da177e4SLinus Torvalds } 10071da177e4SLinus Torvalds return RPC_PRIORITY_NORMAL; 10081da177e4SLinus Torvalds } 10091da177e4SLinus Torvalds 1010d45f60c6SWeston Andros Adamson static void nfs_initiate_write(struct nfs_pgio_header *hdr, 1011d45f60c6SWeston Andros Adamson struct rpc_message *msg, 10121ed26f33SAnna Schumaker struct rpc_task_setup *task_setup_data, int how) 10131da177e4SLinus Torvalds { 1014d45f60c6SWeston Andros Adamson struct inode *inode = hdr->inode; 10153ff7576dSTrond Myklebust int priority = flush_task_priority(how); 10161da177e4SLinus Torvalds 10171ed26f33SAnna Schumaker task_setup_data->priority = priority; 1018d45f60c6SWeston Andros Adamson NFS_PROTO(inode)->write_setup(hdr, msg); 1019d138d5d1SAndy Adamson 10208c21c62cSWeston Andros Adamson nfs4_state_protect_write(NFS_SERVER(inode)->nfs_client, 1021d45f60c6SWeston Andros Adamson &task_setup_data->rpc_client, msg, hdr); 1022275acaafSTrond Myklebust } 1023275acaafSTrond Myklebust 10246d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and 10256d884e8fSFred * call this on each, which will prepare them to be retried on next 10266d884e8fSFred * writeback using standard nfs. 10276d884e8fSFred */ 10286d884e8fSFred static void nfs_redirty_request(struct nfs_page *req) 10296d884e8fSFred { 10306d884e8fSFred nfs_mark_request_dirty(req); 10311d1afcbcSTrond Myklebust nfs_unlock_request(req); 103220633f04SWeston Andros Adamson nfs_end_page_writeback(req); 10333aff4ebbSTrond Myklebust nfs_release_request(req); 10346d884e8fSFred } 10356d884e8fSFred 1036061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head) 10376c75dc0dSFred Isaman { 10386c75dc0dSFred Isaman struct nfs_page *req; 10396c75dc0dSFred Isaman 10406c75dc0dSFred Isaman while (!list_empty(head)) { 10416c75dc0dSFred Isaman req = nfs_list_entry(head->next); 10426c75dc0dSFred Isaman nfs_list_remove_request(req); 10436c75dc0dSFred Isaman nfs_redirty_request(req); 10446c75dc0dSFred Isaman } 10456c75dc0dSFred Isaman } 10466c75dc0dSFred Isaman 1047061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = { 1048061ae2edSFred Isaman .error_cleanup = nfs_async_write_error, 1049061ae2edSFred Isaman .completion = nfs_write_completion, 1050061ae2edSFred Isaman }; 1051061ae2edSFred Isaman 105257208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, 1053a20c93e3SChristoph Hellwig struct inode *inode, int ioflags, bool force_mds, 1054061ae2edSFred Isaman const struct nfs_pgio_completion_ops *compl_ops) 10551751c363STrond Myklebust { 1056a20c93e3SChristoph Hellwig struct nfs_server *server = NFS_SERVER(inode); 105741d8d5b7SAnna Schumaker const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops; 1058a20c93e3SChristoph Hellwig 1059a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1 1060a20c93e3SChristoph Hellwig if (server->pnfs_curr_ld && !force_mds) 1061a20c93e3SChristoph Hellwig pg_ops = server->pnfs_curr_ld->pg_write_ops; 1062a20c93e3SChristoph Hellwig #endif 10634a0de55cSAnna Schumaker nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops, 10644a0de55cSAnna Schumaker server->wsize, ioflags); 10651751c363STrond Myklebust } 1066ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write); 10671751c363STrond Myklebust 1068dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio) 1069dce81290STrond Myklebust { 107041d8d5b7SAnna Schumaker pgio->pg_ops = &nfs_pgio_rw_ops; 1071dce81290STrond Myklebust pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize; 1072dce81290STrond Myklebust } 10731f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds); 1074dce81290STrond Myklebust 10751da177e4SLinus Torvalds 10760b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata) 10770b7c0153SFred Isaman { 10780b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 10790b7c0153SFred Isaman 10800b7c0153SFred Isaman NFS_PROTO(data->inode)->commit_rpc_prepare(task, data); 10810b7c0153SFred Isaman } 10820b7c0153SFred Isaman 1083d45f60c6SWeston Andros Adamson static void nfs_writeback_release_common(struct nfs_pgio_header *hdr) 10841da177e4SLinus Torvalds { 1085c65e6254SWeston Andros Adamson /* do nothing! */ 10861da177e4SLinus Torvalds } 10871da177e4SLinus Torvalds 10881f2edbe3STrond Myklebust /* 10891f2edbe3STrond Myklebust * Special version of should_remove_suid() that ignores capabilities. 10901f2edbe3STrond Myklebust */ 10911f2edbe3STrond Myklebust static int nfs_should_remove_suid(const struct inode *inode) 10921f2edbe3STrond Myklebust { 10931f2edbe3STrond Myklebust umode_t mode = inode->i_mode; 10941f2edbe3STrond Myklebust int kill = 0; 1095788e7a89STrond Myklebust 10961f2edbe3STrond Myklebust /* suid always must be killed */ 10971f2edbe3STrond Myklebust if (unlikely(mode & S_ISUID)) 10981f2edbe3STrond Myklebust kill = ATTR_KILL_SUID; 10991f2edbe3STrond Myklebust 11001f2edbe3STrond Myklebust /* 11011f2edbe3STrond Myklebust * sgid without any exec bits is just a mandatory locking mark; leave 11021f2edbe3STrond Myklebust * it alone. If some exec bits are set, it's a real sgid; kill it. 11031f2edbe3STrond Myklebust */ 11041f2edbe3STrond Myklebust if (unlikely((mode & S_ISGID) && (mode & S_IXGRP))) 11051f2edbe3STrond Myklebust kill |= ATTR_KILL_SGID; 11061f2edbe3STrond Myklebust 11071f2edbe3STrond Myklebust if (unlikely(kill && S_ISREG(mode))) 11081f2edbe3STrond Myklebust return kill; 11091f2edbe3STrond Myklebust 11101f2edbe3STrond Myklebust return 0; 11111f2edbe3STrond Myklebust } 1112788e7a89STrond Myklebust 11131da177e4SLinus Torvalds /* 11141da177e4SLinus Torvalds * This function is called when the WRITE call is complete. 11151da177e4SLinus Torvalds */ 1116d45f60c6SWeston Andros Adamson static int nfs_writeback_done(struct rpc_task *task, 1117d45f60c6SWeston Andros Adamson struct nfs_pgio_header *hdr, 11180eecb214SAnna Schumaker struct inode *inode) 11191da177e4SLinus Torvalds { 1120788e7a89STrond Myklebust int status; 11211da177e4SLinus Torvalds 1122f551e44fSChuck Lever /* 1123f551e44fSChuck Lever * ->write_done will attempt to use post-op attributes to detect 1124f551e44fSChuck Lever * conflicting writes by other clients. A strict interpretation 1125f551e44fSChuck Lever * of close-to-open would allow us to continue caching even if 1126f551e44fSChuck Lever * another writer had changed the file, but some applications 1127f551e44fSChuck Lever * depend on tighter cache coherency when writing. 1128f551e44fSChuck Lever */ 1129d45f60c6SWeston Andros Adamson status = NFS_PROTO(inode)->write_done(task, hdr); 1130788e7a89STrond Myklebust if (status != 0) 11310eecb214SAnna Schumaker return status; 1132d45f60c6SWeston Andros Adamson nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count); 113391d5b470SChuck Lever 113489d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4) 1135d45f60c6SWeston Andros Adamson if (hdr->res.verf->committed < hdr->args.stable && 1136d45f60c6SWeston Andros Adamson task->tk_status >= 0) { 11371da177e4SLinus Torvalds /* We tried a write call, but the server did not 11381da177e4SLinus Torvalds * commit data to stable storage even though we 11391da177e4SLinus Torvalds * requested it. 11401da177e4SLinus Torvalds * Note: There is a known bug in Tru64 < 5.0 in which 11411da177e4SLinus Torvalds * the server reports NFS_DATA_SYNC, but performs 11421da177e4SLinus Torvalds * NFS_FILE_SYNC. We therefore implement this checking 11431da177e4SLinus Torvalds * as a dprintk() in order to avoid filling syslog. 11441da177e4SLinus Torvalds */ 11451da177e4SLinus Torvalds static unsigned long complain; 11461da177e4SLinus Torvalds 1147a69aef14SFred Isaman /* Note this will print the MDS for a DS write */ 11481da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 11491da177e4SLinus Torvalds dprintk("NFS: faulty NFS server %s:" 11501da177e4SLinus Torvalds " (committed = %d) != (stable = %d)\n", 1151cd841605SFred Isaman NFS_SERVER(inode)->nfs_client->cl_hostname, 1152d45f60c6SWeston Andros Adamson hdr->res.verf->committed, hdr->args.stable); 11531da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 11541da177e4SLinus Torvalds } 11551da177e4SLinus Torvalds } 11561da177e4SLinus Torvalds #endif 11571f2edbe3STrond Myklebust 11581f2edbe3STrond Myklebust /* Deal with the suid/sgid bit corner case */ 11591f2edbe3STrond Myklebust if (nfs_should_remove_suid(inode)) 11601f2edbe3STrond Myklebust nfs_mark_for_revalidate(inode); 11610eecb214SAnna Schumaker return 0; 11620eecb214SAnna Schumaker } 11630eecb214SAnna Schumaker 11640eecb214SAnna Schumaker /* 11650eecb214SAnna Schumaker * This function is called when the WRITE call is complete. 11660eecb214SAnna Schumaker */ 1167d45f60c6SWeston Andros Adamson static void nfs_writeback_result(struct rpc_task *task, 1168d45f60c6SWeston Andros Adamson struct nfs_pgio_header *hdr) 11690eecb214SAnna Schumaker { 1170d45f60c6SWeston Andros Adamson struct nfs_pgio_args *argp = &hdr->args; 1171d45f60c6SWeston Andros Adamson struct nfs_pgio_res *resp = &hdr->res; 11721f2edbe3STrond Myklebust 11731f2edbe3STrond Myklebust if (resp->count < argp->count) { 11741da177e4SLinus Torvalds static unsigned long complain; 11751da177e4SLinus Torvalds 11766c75dc0dSFred Isaman /* This a short write! */ 1177d45f60c6SWeston Andros Adamson nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE); 117891d5b470SChuck Lever 11791da177e4SLinus Torvalds /* Has the server at least made some progress? */ 11806c75dc0dSFred Isaman if (resp->count == 0) { 11816c75dc0dSFred Isaman if (time_before(complain, jiffies)) { 11826c75dc0dSFred Isaman printk(KERN_WARNING 11836c75dc0dSFred Isaman "NFS: Server wrote zero bytes, expected %u.\n", 11846c75dc0dSFred Isaman argp->count); 11856c75dc0dSFred Isaman complain = jiffies + 300 * HZ; 11866c75dc0dSFred Isaman } 1187d45f60c6SWeston Andros Adamson nfs_set_pgio_error(hdr, -EIO, argp->offset); 11886c75dc0dSFred Isaman task->tk_status = -EIO; 11896c75dc0dSFred Isaman return; 11906c75dc0dSFred Isaman } 11911da177e4SLinus Torvalds /* Was this an NFSv2 write or an NFSv3 stable write? */ 11921da177e4SLinus Torvalds if (resp->verf->committed != NFS_UNSTABLE) { 11931da177e4SLinus Torvalds /* Resend from where the server left off */ 1194d45f60c6SWeston Andros Adamson hdr->mds_offset += resp->count; 11951da177e4SLinus Torvalds argp->offset += resp->count; 11961da177e4SLinus Torvalds argp->pgbase += resp->count; 11971da177e4SLinus Torvalds argp->count -= resp->count; 11981da177e4SLinus Torvalds } else { 11991da177e4SLinus Torvalds /* Resend as a stable write in order to avoid 12001da177e4SLinus Torvalds * headaches in the case of a server crash. 12011da177e4SLinus Torvalds */ 12021da177e4SLinus Torvalds argp->stable = NFS_FILE_SYNC; 12031da177e4SLinus Torvalds } 1204d00c5d43STrond Myklebust rpc_restart_call_prepare(task); 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds } 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds 120989d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4) 121071d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait) 121171d0a611STrond Myklebust { 1212b8413f98STrond Myklebust int ret; 1213b8413f98STrond Myklebust 121471d0a611STrond Myklebust if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags)) 121571d0a611STrond Myklebust return 1; 1216b8413f98STrond Myklebust if (!may_wait) 121771d0a611STrond Myklebust return 0; 1218b8413f98STrond Myklebust ret = out_of_line_wait_on_bit_lock(&nfsi->flags, 1219b8413f98STrond Myklebust NFS_INO_COMMIT, 1220b8413f98STrond Myklebust nfs_wait_bit_killable, 1221b8413f98STrond Myklebust TASK_KILLABLE); 1222b8413f98STrond Myklebust return (ret < 0) ? ret : 1; 122371d0a611STrond Myklebust } 122471d0a611STrond Myklebust 1225f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi) 122671d0a611STrond Myklebust { 122771d0a611STrond Myklebust clear_bit(NFS_INO_COMMIT, &nfsi->flags); 12284e857c58SPeter Zijlstra smp_mb__after_atomic(); 122971d0a611STrond Myklebust wake_up_bit(&nfsi->flags, NFS_INO_COMMIT); 123071d0a611STrond Myklebust } 123171d0a611STrond Myklebust 12320b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data) 12331da177e4SLinus Torvalds { 12340b7c0153SFred Isaman put_nfs_open_context(data->context); 12350b7c0153SFred Isaman nfs_commit_free(data); 12361da177e4SLinus Torvalds } 1237e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release); 12381da177e4SLinus Torvalds 12390b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data, 12409ace33cdSFred Isaman const struct rpc_call_ops *call_ops, 12419f0ec176SAndy Adamson int how, int flags) 12421da177e4SLinus Torvalds { 124307737691STrond Myklebust struct rpc_task *task; 12449ace33cdSFred Isaman int priority = flush_task_priority(how); 1245bdc7f021STrond Myklebust struct rpc_message msg = { 1246bdc7f021STrond Myklebust .rpc_argp = &data->args, 1247bdc7f021STrond Myklebust .rpc_resp = &data->res, 12489ace33cdSFred Isaman .rpc_cred = data->cred, 1249bdc7f021STrond Myklebust }; 125084115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 125107737691STrond Myklebust .task = &data->task, 12529ace33cdSFred Isaman .rpc_client = clnt, 1253bdc7f021STrond Myklebust .rpc_message = &msg, 12549ace33cdSFred Isaman .callback_ops = call_ops, 125584115e1cSTrond Myklebust .callback_data = data, 1256101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 12579f0ec176SAndy Adamson .flags = RPC_TASK_ASYNC | flags, 12583ff7576dSTrond Myklebust .priority = priority, 125984115e1cSTrond Myklebust }; 1260788e7a89STrond Myklebust /* Set up the initial task struct. */ 12619ace33cdSFred Isaman NFS_PROTO(data->inode)->commit_setup(data, &msg); 12621da177e4SLinus Torvalds 1263a3f565b1SChuck Lever dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); 1264bdc7f021STrond Myklebust 12658c21c62cSWeston Andros Adamson nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client, 12668c21c62cSWeston Andros Adamson NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg); 12678c21c62cSWeston Andros Adamson 126807737691STrond Myklebust task = rpc_run_task(&task_setup_data); 1269dbae4c73STrond Myklebust if (IS_ERR(task)) 1270dbae4c73STrond Myklebust return PTR_ERR(task); 1271d2224e7aSJeff Layton if (how & FLUSH_SYNC) 1272d2224e7aSJeff Layton rpc_wait_for_completion_task(task); 127307737691STrond Myklebust rpc_put_task(task); 1274dbae4c73STrond Myklebust return 0; 12751da177e4SLinus Torvalds } 1276e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit); 12771da177e4SLinus Torvalds 12781da177e4SLinus Torvalds /* 12799ace33cdSFred Isaman * Set up the argument/result storage required for the RPC call. 12809ace33cdSFred Isaman */ 12810b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data, 1282988b6dceSFred Isaman struct list_head *head, 1283f453a54aSFred Isaman struct pnfs_layout_segment *lseg, 1284f453a54aSFred Isaman struct nfs_commit_info *cinfo) 12859ace33cdSFred Isaman { 12869ace33cdSFred Isaman struct nfs_page *first = nfs_list_entry(head->next); 12873d4ff43dSAl Viro struct inode *inode = first->wb_context->dentry->d_inode; 12889ace33cdSFred Isaman 12899ace33cdSFred Isaman /* Set up the RPC argument and reply structs 12909ace33cdSFred Isaman * NB: take care not to mess about with data->commit et al. */ 12919ace33cdSFred Isaman 12929ace33cdSFred Isaman list_splice_init(head, &data->pages); 12939ace33cdSFred Isaman 12949ace33cdSFred Isaman data->inode = inode; 12959ace33cdSFred Isaman data->cred = first->wb_context->cred; 1296988b6dceSFred Isaman data->lseg = lseg; /* reference transferred */ 12979ace33cdSFred Isaman data->mds_ops = &nfs_commit_ops; 1298f453a54aSFred Isaman data->completion_ops = cinfo->completion_ops; 1299b359f9d0SFred Isaman data->dreq = cinfo->dreq; 13009ace33cdSFred Isaman 13019ace33cdSFred Isaman data->args.fh = NFS_FH(data->inode); 13029ace33cdSFred Isaman /* Note: we always request a commit of the entire inode */ 13039ace33cdSFred Isaman data->args.offset = 0; 13049ace33cdSFred Isaman data->args.count = 0; 13050b7c0153SFred Isaman data->context = get_nfs_open_context(first->wb_context); 13069ace33cdSFred Isaman data->res.fattr = &data->fattr; 13079ace33cdSFred Isaman data->res.verf = &data->verf; 13089ace33cdSFred Isaman nfs_fattr_init(&data->fattr); 13099ace33cdSFred Isaman } 1310e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit); 13119ace33cdSFred Isaman 1312e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list, 1313ea2cf228SFred Isaman struct pnfs_layout_segment *lseg, 1314ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 131564bfeb49SFred Isaman { 131664bfeb49SFred Isaman struct nfs_page *req; 131764bfeb49SFred Isaman 131864bfeb49SFred Isaman while (!list_empty(page_list)) { 131964bfeb49SFred Isaman req = nfs_list_entry(page_list->next); 132064bfeb49SFred Isaman nfs_list_remove_request(req); 1321ea2cf228SFred Isaman nfs_mark_request_commit(req, lseg, cinfo); 132256f9cd68SFred Isaman if (!cinfo->dreq) { 132364bfeb49SFred Isaman dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 1324d56b4ddfSMel Gorman dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info, 132564bfeb49SFred Isaman BDI_RECLAIMABLE); 132656f9cd68SFred Isaman } 13271d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 132864bfeb49SFred Isaman } 132964bfeb49SFred Isaman } 1330e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit); 133164bfeb49SFred Isaman 13329ace33cdSFred Isaman /* 13331da177e4SLinus Torvalds * Commit dirty pages 13341da177e4SLinus Torvalds */ 13351da177e4SLinus Torvalds static int 1336ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how, 1337ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 13381da177e4SLinus Torvalds { 13390b7c0153SFred Isaman struct nfs_commit_data *data; 13401da177e4SLinus Torvalds 1341c9d8f89dSTrond Myklebust data = nfs_commitdata_alloc(); 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds if (!data) 13441da177e4SLinus Torvalds goto out_bad; 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds /* Set up the argument struct */ 1347f453a54aSFred Isaman nfs_init_commit(data, head, NULL, cinfo); 1348f453a54aSFred Isaman atomic_inc(&cinfo->mds->rpcs_out); 13499f0ec176SAndy Adamson return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops, 13509f0ec176SAndy Adamson how, 0); 13511da177e4SLinus Torvalds out_bad: 1352ea2cf228SFred Isaman nfs_retry_commit(head, NULL, cinfo); 1353f453a54aSFred Isaman cinfo->completion_ops->error_cleanup(NFS_I(inode)); 13541da177e4SLinus Torvalds return -ENOMEM; 13551da177e4SLinus Torvalds } 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds /* 13581da177e4SLinus Torvalds * COMMIT call returned 13591da177e4SLinus Torvalds */ 1360788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata) 13611da177e4SLinus Torvalds { 13620b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 13631da177e4SLinus Torvalds 1364a3f565b1SChuck Lever dprintk("NFS: %5u nfs_commit_done (status %d)\n", 13651da177e4SLinus Torvalds task->tk_pid, task->tk_status); 13661da177e4SLinus Torvalds 1367788e7a89STrond Myklebust /* Call the NFS version-specific code */ 1368c0d0e96bSTrond Myklebust NFS_PROTO(data->inode)->commit_done(task, data); 1369c9d8f89dSTrond Myklebust } 1370c9d8f89dSTrond Myklebust 1371f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data) 1372c9d8f89dSTrond Myklebust { 1373c9d8f89dSTrond Myklebust struct nfs_page *req; 1374c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1375f453a54aSFred Isaman struct nfs_commit_info cinfo; 1376788e7a89STrond Myklebust 13771da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 13781da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 13791da177e4SLinus Torvalds nfs_list_remove_request(req); 1380d6d6dc7cSFred Isaman nfs_clear_page_commit(req->wb_page); 13811da177e4SLinus Torvalds 13821e8968c5SNiels de Vos dprintk("NFS: commit (%s/%llu %d@%lld)", 13833d4ff43dSAl Viro req->wb_context->dentry->d_sb->s_id, 13841e8968c5SNiels de Vos (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode), 13851da177e4SLinus Torvalds req->wb_bytes, 13861da177e4SLinus Torvalds (long long)req_offset(req)); 1387c9d8f89dSTrond Myklebust if (status < 0) { 1388c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 13891da177e4SLinus Torvalds nfs_inode_remove_request(req); 1390c9d8f89dSTrond Myklebust dprintk(", error = %d\n", status); 13911da177e4SLinus Torvalds goto next; 13921da177e4SLinus Torvalds } 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds /* Okay, COMMIT succeeded, apparently. Check the verifier 13951da177e4SLinus Torvalds * returned by the server against all stored verfs. */ 13962f2c63bcSTrond Myklebust if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) { 13971da177e4SLinus Torvalds /* We have a match */ 13981da177e4SLinus Torvalds nfs_inode_remove_request(req); 13991da177e4SLinus Torvalds dprintk(" OK\n"); 14001da177e4SLinus Torvalds goto next; 14011da177e4SLinus Torvalds } 14021da177e4SLinus Torvalds /* We have a mismatch. Write the page again */ 14031da177e4SLinus Torvalds dprintk(" mismatch\n"); 14046d884e8fSFred nfs_mark_request_dirty(req); 140505990d1bSTrond Myklebust set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags); 14061da177e4SLinus Torvalds next: 14071d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 14081da177e4SLinus Torvalds } 1409f453a54aSFred Isaman nfs_init_cinfo(&cinfo, data->inode, data->dreq); 1410f453a54aSFred Isaman if (atomic_dec_and_test(&cinfo.mds->rpcs_out)) 1411f453a54aSFred Isaman nfs_commit_clear_lock(NFS_I(data->inode)); 14125917ce84SFred Isaman } 14135917ce84SFred Isaman 14145917ce84SFred Isaman static void nfs_commit_release(void *calldata) 14155917ce84SFred Isaman { 14160b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 14175917ce84SFred Isaman 1418f453a54aSFred Isaman data->completion_ops->completion(data); 1419c9d8f89dSTrond Myklebust nfs_commitdata_release(calldata); 14201da177e4SLinus Torvalds } 1421788e7a89STrond Myklebust 1422788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = { 14230b7c0153SFred Isaman .rpc_call_prepare = nfs_commit_prepare, 1424788e7a89STrond Myklebust .rpc_call_done = nfs_commit_done, 1425788e7a89STrond Myklebust .rpc_release = nfs_commit_release, 1426788e7a89STrond Myklebust }; 14271da177e4SLinus Torvalds 1428f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = { 1429f453a54aSFred Isaman .completion = nfs_commit_release_pages, 1430f453a54aSFred Isaman .error_cleanup = nfs_commit_clear_lock, 1431f453a54aSFred Isaman }; 1432f453a54aSFred Isaman 14331763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head, 1434ea2cf228SFred Isaman int how, struct nfs_commit_info *cinfo) 143584c53ab5SFred Isaman { 143684c53ab5SFred Isaman int status; 143784c53ab5SFred Isaman 1438ea2cf228SFred Isaman status = pnfs_commit_list(inode, head, how, cinfo); 143984c53ab5SFred Isaman if (status == PNFS_NOT_ATTEMPTED) 1440ea2cf228SFred Isaman status = nfs_commit_list(inode, head, how, cinfo); 144184c53ab5SFred Isaman return status; 144284c53ab5SFred Isaman } 144384c53ab5SFred Isaman 1444b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how) 14451da177e4SLinus Torvalds { 14461da177e4SLinus Torvalds LIST_HEAD(head); 1447ea2cf228SFred Isaman struct nfs_commit_info cinfo; 144871d0a611STrond Myklebust int may_wait = how & FLUSH_SYNC; 1449b8413f98STrond Myklebust int res; 14501da177e4SLinus Torvalds 1451b8413f98STrond Myklebust res = nfs_commit_set_lock(NFS_I(inode), may_wait); 1452b8413f98STrond Myklebust if (res <= 0) 1453c5efa5fcSTrond Myklebust goto out_mark_dirty; 1454ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, inode); 1455ea2cf228SFred Isaman res = nfs_scan_commit(inode, &head, &cinfo); 14561da177e4SLinus Torvalds if (res) { 1457a861a1e1SFred Isaman int error; 1458a861a1e1SFred Isaman 1459ea2cf228SFred Isaman error = nfs_generic_commit_list(inode, &head, how, &cinfo); 14601da177e4SLinus Torvalds if (error < 0) 14611da177e4SLinus Torvalds return error; 1462b8413f98STrond Myklebust if (!may_wait) 1463b8413f98STrond Myklebust goto out_mark_dirty; 1464b8413f98STrond Myklebust error = wait_on_bit(&NFS_I(inode)->flags, 1465b8413f98STrond Myklebust NFS_INO_COMMIT, 146671d0a611STrond Myklebust nfs_wait_bit_killable, 146771d0a611STrond Myklebust TASK_KILLABLE); 1468b8413f98STrond Myklebust if (error < 0) 1469b8413f98STrond Myklebust return error; 147071d0a611STrond Myklebust } else 147171d0a611STrond Myklebust nfs_commit_clear_lock(NFS_I(inode)); 1472c5efa5fcSTrond Myklebust return res; 1473c5efa5fcSTrond Myklebust /* Note: If we exit without ensuring that the commit is complete, 1474c5efa5fcSTrond Myklebust * we must mark the inode as dirty. Otherwise, future calls to 1475c5efa5fcSTrond Myklebust * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure 1476c5efa5fcSTrond Myklebust * that the data is on the disk. 1477c5efa5fcSTrond Myklebust */ 1478c5efa5fcSTrond Myklebust out_mark_dirty: 1479c5efa5fcSTrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 14801da177e4SLinus Torvalds return res; 14811da177e4SLinus Torvalds } 14828fc795f7STrond Myklebust 14838fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 14848fc795f7STrond Myklebust { 1485420e3646STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 1486420e3646STrond Myklebust int flags = FLUSH_SYNC; 1487420e3646STrond Myklebust int ret = 0; 14888fc795f7STrond Myklebust 14893236c3e1SJeff Layton /* no commits means nothing needs to be done */ 1490ea2cf228SFred Isaman if (!nfsi->commit_info.ncommit) 14913236c3e1SJeff Layton return ret; 14923236c3e1SJeff Layton 1493a00dd6c0SJeff Layton if (wbc->sync_mode == WB_SYNC_NONE) { 1494a00dd6c0SJeff Layton /* Don't commit yet if this is a non-blocking flush and there 1495a00dd6c0SJeff Layton * are a lot of outstanding writes for this mapping. 1496420e3646STrond Myklebust */ 1497ea2cf228SFred Isaman if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1)) 1498420e3646STrond Myklebust goto out_mark_dirty; 1499420e3646STrond Myklebust 1500a00dd6c0SJeff Layton /* don't wait for the COMMIT response */ 1501420e3646STrond Myklebust flags = 0; 1502a00dd6c0SJeff Layton } 1503a00dd6c0SJeff Layton 1504420e3646STrond Myklebust ret = nfs_commit_inode(inode, flags); 1505420e3646STrond Myklebust if (ret >= 0) { 1506420e3646STrond Myklebust if (wbc->sync_mode == WB_SYNC_NONE) { 1507420e3646STrond Myklebust if (ret < wbc->nr_to_write) 1508420e3646STrond Myklebust wbc->nr_to_write -= ret; 1509420e3646STrond Myklebust else 1510420e3646STrond Myklebust wbc->nr_to_write = 0; 1511420e3646STrond Myklebust } 15128fc795f7STrond Myklebust return 0; 1513420e3646STrond Myklebust } 1514420e3646STrond Myklebust out_mark_dirty: 15158fc795f7STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 15168fc795f7STrond Myklebust return ret; 15178fc795f7STrond Myklebust } 1518c63c7b05STrond Myklebust #else 15198fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) 15208fc795f7STrond Myklebust { 15218fc795f7STrond Myklebust return 0; 15228fc795f7STrond Myklebust } 15231da177e4SLinus Torvalds #endif 15241da177e4SLinus Torvalds 15258fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc) 15268fc795f7STrond Myklebust { 1527a8d8f02cSBryan Schumaker return nfs_commit_unstable_pages(inode, wbc); 1528a8d8f02cSBryan Schumaker } 152989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode); 1530863a3c6cSAndy Adamson 1531acdc53b2STrond Myklebust /* 1532acdc53b2STrond Myklebust * flush the inode to disk. 1533acdc53b2STrond Myklebust */ 1534acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode) 153534901f70STrond Myklebust { 153634901f70STrond Myklebust struct writeback_control wbc = { 153772cb77f4STrond Myklebust .sync_mode = WB_SYNC_ALL, 153834901f70STrond Myklebust .nr_to_write = LONG_MAX, 1539d7fb1207STrond Myklebust .range_start = 0, 1540d7fb1207STrond Myklebust .range_end = LLONG_MAX, 154134901f70STrond Myklebust }; 1542f4ce1299STrond Myklebust int ret; 154334901f70STrond Myklebust 1544f4ce1299STrond Myklebust trace_nfs_writeback_inode_enter(inode); 1545f4ce1299STrond Myklebust 1546f4ce1299STrond Myklebust ret = sync_inode(inode, &wbc); 1547f4ce1299STrond Myklebust 1548f4ce1299STrond Myklebust trace_nfs_writeback_inode_exit(inode, ret); 1549f4ce1299STrond Myklebust return ret; 15501c75950bSTrond Myklebust } 1551ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all); 15521c75950bSTrond Myklebust 15531b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page) 15541b3b4a1aSTrond Myklebust { 15551b3b4a1aSTrond Myklebust struct nfs_page *req; 15561b3b4a1aSTrond Myklebust int ret = 0; 15571b3b4a1aSTrond Myklebust 15581b3b4a1aSTrond Myklebust for (;;) { 1559ba8b06e6STrond Myklebust wait_on_page_writeback(page); 15601b3b4a1aSTrond Myklebust req = nfs_page_find_request(page); 15611b3b4a1aSTrond Myklebust if (req == NULL) 15621b3b4a1aSTrond Myklebust break; 15637ad84aa9STrond Myklebust if (nfs_lock_request(req)) { 15648dd37758STrond Myklebust nfs_clear_request_commit(req); 15651b3b4a1aSTrond Myklebust nfs_inode_remove_request(req); 15661b3b4a1aSTrond Myklebust /* 15671b3b4a1aSTrond Myklebust * In case nfs_inode_remove_request has marked the 15681b3b4a1aSTrond Myklebust * page as being dirty 15691b3b4a1aSTrond Myklebust */ 15701b3b4a1aSTrond Myklebust cancel_dirty_page(page, PAGE_CACHE_SIZE); 15711d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 15721b3b4a1aSTrond Myklebust break; 15731b3b4a1aSTrond Myklebust } 15741b3b4a1aSTrond Myklebust ret = nfs_wait_on_request(req); 1575c9edda71STrond Myklebust nfs_release_request(req); 15761b3b4a1aSTrond Myklebust if (ret < 0) 1577c988950eSTrond Myklebust break; 15781b3b4a1aSTrond Myklebust } 15791b3b4a1aSTrond Myklebust return ret; 15801b3b4a1aSTrond Myklebust } 15811b3b4a1aSTrond Myklebust 15821c75950bSTrond Myklebust /* 15831c75950bSTrond Myklebust * Write back all requests on one page - we do this before reading it. 15841c75950bSTrond Myklebust */ 15851c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page) 15861c75950bSTrond Myklebust { 158729418aa4SMel Gorman loff_t range_start = page_file_offset(page); 15887f2f12d9STrond Myklebust loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); 15897f2f12d9STrond Myklebust struct writeback_control wbc = { 15907f2f12d9STrond Myklebust .sync_mode = WB_SYNC_ALL, 15917f2f12d9STrond Myklebust .nr_to_write = 0, 15927f2f12d9STrond Myklebust .range_start = range_start, 15937f2f12d9STrond Myklebust .range_end = range_end, 15947f2f12d9STrond Myklebust }; 15957f2f12d9STrond Myklebust int ret; 15967f2f12d9STrond Myklebust 1597f4ce1299STrond Myklebust trace_nfs_writeback_page_enter(inode); 1598f4ce1299STrond Myklebust 15990522f6adSTrond Myklebust for (;;) { 1600ba8b06e6STrond Myklebust wait_on_page_writeback(page); 16017f2f12d9STrond Myklebust if (clear_page_dirty_for_io(page)) { 16027f2f12d9STrond Myklebust ret = nfs_writepage_locked(page, &wbc); 16037f2f12d9STrond Myklebust if (ret < 0) 16047f2f12d9STrond Myklebust goto out_error; 16050522f6adSTrond Myklebust continue; 16067f2f12d9STrond Myklebust } 1607f4ce1299STrond Myklebust ret = 0; 16080522f6adSTrond Myklebust if (!PagePrivate(page)) 16090522f6adSTrond Myklebust break; 16100522f6adSTrond Myklebust ret = nfs_commit_inode(inode, FLUSH_SYNC); 16117f2f12d9STrond Myklebust if (ret < 0) 16127f2f12d9STrond Myklebust goto out_error; 16137f2f12d9STrond Myklebust } 16147f2f12d9STrond Myklebust out_error: 1615f4ce1299STrond Myklebust trace_nfs_writeback_page_exit(inode, ret); 16167f2f12d9STrond Myklebust return ret; 16171c75950bSTrond Myklebust } 16181c75950bSTrond Myklebust 1619074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION 1620074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage, 1621a6bc32b8SMel Gorman struct page *page, enum migrate_mode mode) 1622074cc1deSTrond Myklebust { 16232da95652SJeff Layton /* 16242da95652SJeff Layton * If PagePrivate is set, then the page is currently associated with 16252da95652SJeff Layton * an in-progress read or write request. Don't try to migrate it. 16262da95652SJeff Layton * 16272da95652SJeff Layton * FIXME: we could do this in principle, but we'll need a way to ensure 16282da95652SJeff Layton * that we can safely release the inode reference while holding 16292da95652SJeff Layton * the page lock. 16302da95652SJeff Layton */ 16312da95652SJeff Layton if (PagePrivate(page)) 16322da95652SJeff Layton return -EBUSY; 1633074cc1deSTrond Myklebust 16348c209ce7SDavid Howells if (!nfs_fscache_release_page(page, GFP_KERNEL)) 16358c209ce7SDavid Howells return -EBUSY; 1636074cc1deSTrond Myklebust 1637a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 1638074cc1deSTrond Myklebust } 1639074cc1deSTrond Myklebust #endif 1640074cc1deSTrond Myklebust 1641f7b422b1SDavid Howells int __init nfs_init_writepagecache(void) 16421da177e4SLinus Torvalds { 16431da177e4SLinus Torvalds nfs_wdata_cachep = kmem_cache_create("nfs_write_data", 16441e7f3a48SWeston Andros Adamson sizeof(struct nfs_pgio_header), 16451da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 164620c2df83SPaul Mundt NULL); 16471da177e4SLinus Torvalds if (nfs_wdata_cachep == NULL) 16481da177e4SLinus Torvalds return -ENOMEM; 16491da177e4SLinus Torvalds 165093d2341cSMatthew Dobson nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, 16511da177e4SLinus Torvalds nfs_wdata_cachep); 16521da177e4SLinus Torvalds if (nfs_wdata_mempool == NULL) 16533dd4765fSJeff Layton goto out_destroy_write_cache; 16541da177e4SLinus Torvalds 16550b7c0153SFred Isaman nfs_cdata_cachep = kmem_cache_create("nfs_commit_data", 16560b7c0153SFred Isaman sizeof(struct nfs_commit_data), 16570b7c0153SFred Isaman 0, SLAB_HWCACHE_ALIGN, 16580b7c0153SFred Isaman NULL); 16590b7c0153SFred Isaman if (nfs_cdata_cachep == NULL) 16603dd4765fSJeff Layton goto out_destroy_write_mempool; 16610b7c0153SFred Isaman 166293d2341cSMatthew Dobson nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, 16634c100210SYanchuan Nian nfs_cdata_cachep); 16641da177e4SLinus Torvalds if (nfs_commit_mempool == NULL) 16653dd4765fSJeff Layton goto out_destroy_commit_cache; 16661da177e4SLinus Torvalds 166789a09141SPeter Zijlstra /* 166889a09141SPeter Zijlstra * NFS congestion size, scale with available memory. 166989a09141SPeter Zijlstra * 167089a09141SPeter Zijlstra * 64MB: 8192k 167189a09141SPeter Zijlstra * 128MB: 11585k 167289a09141SPeter Zijlstra * 256MB: 16384k 167389a09141SPeter Zijlstra * 512MB: 23170k 167489a09141SPeter Zijlstra * 1GB: 32768k 167589a09141SPeter Zijlstra * 2GB: 46340k 167689a09141SPeter Zijlstra * 4GB: 65536k 167789a09141SPeter Zijlstra * 8GB: 92681k 167889a09141SPeter Zijlstra * 16GB: 131072k 167989a09141SPeter Zijlstra * 168089a09141SPeter Zijlstra * This allows larger machines to have larger/more transfers. 168189a09141SPeter Zijlstra * Limit the default to 256M 168289a09141SPeter Zijlstra */ 168389a09141SPeter Zijlstra nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); 168489a09141SPeter Zijlstra if (nfs_congestion_kb > 256*1024) 168589a09141SPeter Zijlstra nfs_congestion_kb = 256*1024; 168689a09141SPeter Zijlstra 16871da177e4SLinus Torvalds return 0; 16883dd4765fSJeff Layton 16893dd4765fSJeff Layton out_destroy_commit_cache: 16903dd4765fSJeff Layton kmem_cache_destroy(nfs_cdata_cachep); 16913dd4765fSJeff Layton out_destroy_write_mempool: 16923dd4765fSJeff Layton mempool_destroy(nfs_wdata_mempool); 16933dd4765fSJeff Layton out_destroy_write_cache: 16943dd4765fSJeff Layton kmem_cache_destroy(nfs_wdata_cachep); 16953dd4765fSJeff Layton return -ENOMEM; 16961da177e4SLinus Torvalds } 16971da177e4SLinus Torvalds 1698266bee88SDavid Brownell void nfs_destroy_writepagecache(void) 16991da177e4SLinus Torvalds { 17001da177e4SLinus Torvalds mempool_destroy(nfs_commit_mempool); 17013dd4765fSJeff Layton kmem_cache_destroy(nfs_cdata_cachep); 17021da177e4SLinus Torvalds mempool_destroy(nfs_wdata_mempool); 17031a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_wdata_cachep); 17041da177e4SLinus Torvalds } 17051da177e4SLinus Torvalds 17064a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = { 1707a4cdda59SAnna Schumaker .rw_mode = FMODE_WRITE, 17084a0de55cSAnna Schumaker .rw_alloc_header = nfs_writehdr_alloc, 17094a0de55cSAnna Schumaker .rw_free_header = nfs_writehdr_free, 1710a4cdda59SAnna Schumaker .rw_release = nfs_writeback_release_common, 17110eecb214SAnna Schumaker .rw_done = nfs_writeback_done, 17120eecb214SAnna Schumaker .rw_result = nfs_writeback_result, 17131ed26f33SAnna Schumaker .rw_initiate = nfs_initiate_write, 17144a0de55cSAnna Schumaker }; 1715