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> 24af7cf057STrond Myklebust #include <linux/freezer.h> 25af7cf057STrond Myklebust #include <linux/wait.h> 263fcfab16SAndrew Morton 277c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds #include "delegation.h" 3049a70f27STrond Myklebust #include "internal.h" 3191d5b470SChuck Lever #include "iostat.h" 32def6ed7eSAndy Adamson #include "nfs4_fs.h" 33074cc1deSTrond Myklebust #include "fscache.h" 3494ad1c80SFred Isaman #include "pnfs.h" 351da177e4SLinus Torvalds 36f4ce1299STrond Myklebust #include "nfstrace.h" 37f4ce1299STrond Myklebust 381da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_PAGECACHE 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds #define MIN_POOL_WRITE (32) 411da177e4SLinus Torvalds #define MIN_POOL_COMMIT (4) 421da177e4SLinus Torvalds 43919e3bd9STrond Myklebust struct nfs_io_completion { 44919e3bd9STrond Myklebust void (*complete)(void *data); 45919e3bd9STrond Myklebust void *data; 46919e3bd9STrond Myklebust struct kref refcount; 47919e3bd9STrond Myklebust }; 48919e3bd9STrond Myklebust 491da177e4SLinus Torvalds /* 501da177e4SLinus Torvalds * Local function declarations 511da177e4SLinus Torvalds */ 52f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req); 53788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops; 54061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops; 55f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops; 564a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops; 57d4581383SWeston Andros Adamson static void nfs_clear_request_commit(struct nfs_page *req); 5802d1426cSWeston Andros Adamson static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, 5902d1426cSWeston Andros Adamson struct inode *inode); 603a3908c8STrond Myklebust static struct nfs_page * 613a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi, 623a3908c8STrond Myklebust struct page *page); 631da177e4SLinus Torvalds 64e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep; 653feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool; 660b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep; 671da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool; 681da177e4SLinus Torvalds 69518662e0SNeilBrown struct nfs_commit_data *nfs_commitdata_alloc(bool never_fail) 701da177e4SLinus Torvalds { 71518662e0SNeilBrown struct nfs_commit_data *p; 7240859d7eSChuck Lever 73518662e0SNeilBrown if (never_fail) 74518662e0SNeilBrown p = mempool_alloc(nfs_commit_mempool, GFP_NOIO); 75518662e0SNeilBrown else { 76518662e0SNeilBrown /* It is OK to do some reclaim, not no safe to wait 77518662e0SNeilBrown * for anything to be returned to the pool. 78518662e0SNeilBrown * mempool_alloc() cannot handle that particular combination, 79518662e0SNeilBrown * so we need two separate attempts. 80518662e0SNeilBrown */ 81518662e0SNeilBrown p = mempool_alloc(nfs_commit_mempool, GFP_NOWAIT); 82518662e0SNeilBrown if (!p) 83518662e0SNeilBrown p = kmem_cache_alloc(nfs_cdata_cachep, GFP_NOIO | 84518662e0SNeilBrown __GFP_NOWARN | __GFP_NORETRY); 85518662e0SNeilBrown if (!p) 86518662e0SNeilBrown return NULL; 87518662e0SNeilBrown } 88518662e0SNeilBrown 891da177e4SLinus Torvalds memset(p, 0, sizeof(*p)); 901da177e4SLinus Torvalds INIT_LIST_HEAD(&p->pages); 911da177e4SLinus Torvalds return p; 921da177e4SLinus Torvalds } 93e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc); 941da177e4SLinus Torvalds 950b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p) 961da177e4SLinus Torvalds { 971da177e4SLinus Torvalds mempool_free(p, nfs_commit_mempool); 981da177e4SLinus Torvalds } 99e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free); 1001da177e4SLinus Torvalds 1011e7f3a48SWeston Andros Adamson static struct nfs_pgio_header *nfs_writehdr_alloc(void) 1023feb2d49STrond Myklebust { 1031e7f3a48SWeston Andros Adamson struct nfs_pgio_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO); 1043feb2d49STrond Myklebust 105fbe77c30SBenjamin Coddington if (p) { 1063feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 107fbe77c30SBenjamin Coddington p->rw_mode = FMODE_WRITE; 108fbe77c30SBenjamin Coddington } 1093feb2d49STrond Myklebust return p; 1103feb2d49STrond Myklebust } 1113feb2d49STrond Myklebust 1121e7f3a48SWeston Andros Adamson static void nfs_writehdr_free(struct nfs_pgio_header *hdr) 1136c75dc0dSFred Isaman { 1141e7f3a48SWeston Andros Adamson mempool_free(hdr, nfs_wdata_mempool); 1153feb2d49STrond Myklebust } 1161da177e4SLinus Torvalds 117919e3bd9STrond Myklebust static struct nfs_io_completion *nfs_io_completion_alloc(gfp_t gfp_flags) 118919e3bd9STrond Myklebust { 119919e3bd9STrond Myklebust return kmalloc(sizeof(struct nfs_io_completion), gfp_flags); 120919e3bd9STrond Myklebust } 121919e3bd9STrond Myklebust 122919e3bd9STrond Myklebust static void nfs_io_completion_init(struct nfs_io_completion *ioc, 123919e3bd9STrond Myklebust void (*complete)(void *), void *data) 124919e3bd9STrond Myklebust { 125919e3bd9STrond Myklebust ioc->complete = complete; 126919e3bd9STrond Myklebust ioc->data = data; 127919e3bd9STrond Myklebust kref_init(&ioc->refcount); 128919e3bd9STrond Myklebust } 129919e3bd9STrond Myklebust 130919e3bd9STrond Myklebust static void nfs_io_completion_release(struct kref *kref) 131919e3bd9STrond Myklebust { 132919e3bd9STrond Myklebust struct nfs_io_completion *ioc = container_of(kref, 133919e3bd9STrond Myklebust struct nfs_io_completion, refcount); 134919e3bd9STrond Myklebust ioc->complete(ioc->data); 135919e3bd9STrond Myklebust kfree(ioc); 136919e3bd9STrond Myklebust } 137919e3bd9STrond Myklebust 138919e3bd9STrond Myklebust static void nfs_io_completion_get(struct nfs_io_completion *ioc) 139919e3bd9STrond Myklebust { 140919e3bd9STrond Myklebust if (ioc != NULL) 141919e3bd9STrond Myklebust kref_get(&ioc->refcount); 142919e3bd9STrond Myklebust } 143919e3bd9STrond Myklebust 144919e3bd9STrond Myklebust static void nfs_io_completion_put(struct nfs_io_completion *ioc) 145919e3bd9STrond Myklebust { 146919e3bd9STrond Myklebust if (ioc != NULL) 147919e3bd9STrond Myklebust kref_put(&ioc->refcount, nfs_io_completion_release); 148919e3bd9STrond Myklebust } 149919e3bd9STrond Myklebust 1507b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error) 1517b159fc1STrond Myklebust { 1527b159fc1STrond Myklebust ctx->error = error; 1537b159fc1STrond Myklebust smp_wmb(); 1547b159fc1STrond Myklebust set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 1557b159fc1STrond Myklebust } 1567b159fc1STrond Myklebust 15784d3a9a9SWeston Andros Adamson /* 15884d3a9a9SWeston Andros Adamson * nfs_page_find_head_request_locked - find head request associated with @page 15984d3a9a9SWeston Andros Adamson * 16084d3a9a9SWeston Andros Adamson * must be called while holding the inode lock. 16184d3a9a9SWeston Andros Adamson * 16284d3a9a9SWeston Andros Adamson * returns matching head request with reference held, or NULL if not found. 16384d3a9a9SWeston Andros Adamson */ 16429418aa4SMel Gorman static struct nfs_page * 16584d3a9a9SWeston Andros Adamson nfs_page_find_head_request_locked(struct nfs_inode *nfsi, struct page *page) 166277459d2STrond Myklebust { 167277459d2STrond Myklebust struct nfs_page *req = NULL; 168277459d2STrond Myklebust 16929418aa4SMel Gorman if (PagePrivate(page)) 170277459d2STrond Myklebust req = (struct nfs_page *)page_private(page); 17102d1426cSWeston Andros Adamson else if (unlikely(PageSwapCache(page))) 17202d1426cSWeston Andros Adamson req = nfs_page_search_commits_for_head_request_locked(nfsi, 17302d1426cSWeston Andros Adamson page); 17429418aa4SMel Gorman 17584d3a9a9SWeston Andros Adamson if (req) { 17684d3a9a9SWeston Andros Adamson WARN_ON_ONCE(req->wb_head != req); 17729418aa4SMel Gorman kref_get(&req->wb_kref); 17884d3a9a9SWeston Andros Adamson } 17929418aa4SMel Gorman 180277459d2STrond Myklebust return req; 181277459d2STrond Myklebust } 182277459d2STrond Myklebust 18384d3a9a9SWeston Andros Adamson /* 18484d3a9a9SWeston Andros Adamson * nfs_page_find_head_request - find head request associated with @page 18584d3a9a9SWeston Andros Adamson * 18684d3a9a9SWeston Andros Adamson * returns matching head request with reference held, or NULL if not found. 18784d3a9a9SWeston Andros Adamson */ 18884d3a9a9SWeston Andros Adamson static struct nfs_page *nfs_page_find_head_request(struct page *page) 189277459d2STrond Myklebust { 190d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 191277459d2STrond Myklebust struct nfs_page *req = NULL; 192277459d2STrond Myklebust 19382749dd4STrond Myklebust if (PagePrivate(page) || PageSwapCache(page)) { 194587142f8STrond Myklebust spin_lock(&inode->i_lock); 19584d3a9a9SWeston Andros Adamson req = nfs_page_find_head_request_locked(NFS_I(inode), page); 196587142f8STrond Myklebust spin_unlock(&inode->i_lock); 19782749dd4STrond Myklebust } 198277459d2STrond Myklebust return req; 199277459d2STrond Myklebust } 200277459d2STrond Myklebust 2011da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */ 2021da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) 2031da177e4SLinus Torvalds { 204d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 205a3d01454STrond Myklebust loff_t end, i_size; 206a3d01454STrond Myklebust pgoff_t end_index; 2071da177e4SLinus Torvalds 208a3d01454STrond Myklebust spin_lock(&inode->i_lock); 209a3d01454STrond Myklebust i_size = i_size_read(inode); 21009cbfeafSKirill A. Shutemov end_index = (i_size - 1) >> PAGE_SHIFT; 2118cd79788SHuang Ying if (i_size > 0 && page_index(page) < end_index) 212a3d01454STrond Myklebust goto out; 213d56b4ddfSMel Gorman end = page_file_offset(page) + ((loff_t)offset+count); 2141da177e4SLinus Torvalds if (i_size >= end) 215a3d01454STrond Myklebust goto out; 2161da177e4SLinus Torvalds i_size_write(inode, end); 217a3d01454STrond Myklebust nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); 218a3d01454STrond Myklebust out: 219a3d01454STrond Myklebust spin_unlock(&inode->i_lock); 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds 222a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */ 223a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page) 224a301b777STrond Myklebust { 225d56b4ddfSMel Gorman nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page)); 226a301b777STrond Myklebust } 227a301b777STrond Myklebust 228d72ddcbaSWeston Andros Adamson /* 229d72ddcbaSWeston Andros Adamson * nfs_page_group_search_locked 230d72ddcbaSWeston Andros Adamson * @head - head request of page group 231d72ddcbaSWeston Andros Adamson * @page_offset - offset into page 232d72ddcbaSWeston Andros Adamson * 233d72ddcbaSWeston Andros Adamson * Search page group with head @head to find a request that contains the 234d72ddcbaSWeston Andros Adamson * page offset @page_offset. 235d72ddcbaSWeston Andros Adamson * 236d72ddcbaSWeston Andros Adamson * Returns a pointer to the first matching nfs request, or NULL if no 237d72ddcbaSWeston Andros Adamson * match is found. 238d72ddcbaSWeston Andros Adamson * 239d72ddcbaSWeston Andros Adamson * Must be called with the page group lock held 240d72ddcbaSWeston Andros Adamson */ 241d72ddcbaSWeston Andros Adamson static struct nfs_page * 242d72ddcbaSWeston Andros Adamson nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset) 243d72ddcbaSWeston Andros Adamson { 244d72ddcbaSWeston Andros Adamson struct nfs_page *req; 245d72ddcbaSWeston Andros Adamson 246d72ddcbaSWeston Andros Adamson WARN_ON_ONCE(head != head->wb_head); 247d72ddcbaSWeston Andros Adamson WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_head->wb_flags)); 248d72ddcbaSWeston Andros Adamson 249d72ddcbaSWeston Andros Adamson req = head; 250d72ddcbaSWeston Andros Adamson do { 251d72ddcbaSWeston Andros Adamson if (page_offset >= req->wb_pgbase && 252d72ddcbaSWeston Andros Adamson page_offset < (req->wb_pgbase + req->wb_bytes)) 253d72ddcbaSWeston Andros Adamson return req; 254d72ddcbaSWeston Andros Adamson 255d72ddcbaSWeston Andros Adamson req = req->wb_this_page; 256d72ddcbaSWeston Andros Adamson } while (req != head); 257d72ddcbaSWeston Andros Adamson 258d72ddcbaSWeston Andros Adamson return NULL; 259d72ddcbaSWeston Andros Adamson } 260d72ddcbaSWeston Andros Adamson 261d72ddcbaSWeston Andros Adamson /* 262d72ddcbaSWeston Andros Adamson * nfs_page_group_covers_page 263d72ddcbaSWeston Andros Adamson * @head - head request of page group 264d72ddcbaSWeston Andros Adamson * 265d72ddcbaSWeston Andros Adamson * Return true if the page group with head @head covers the whole page, 266d72ddcbaSWeston Andros Adamson * returns false otherwise 267d72ddcbaSWeston Andros Adamson */ 268d72ddcbaSWeston Andros Adamson static bool nfs_page_group_covers_page(struct nfs_page *req) 269d72ddcbaSWeston Andros Adamson { 270d72ddcbaSWeston Andros Adamson struct nfs_page *tmp; 271d72ddcbaSWeston Andros Adamson unsigned int pos = 0; 272d72ddcbaSWeston Andros Adamson unsigned int len = nfs_page_length(req->wb_page); 273d72ddcbaSWeston Andros Adamson 274*1344b7eaSTrond Myklebust nfs_page_group_lock(req); 275d72ddcbaSWeston Andros Adamson 276d72ddcbaSWeston Andros Adamson do { 277d72ddcbaSWeston Andros Adamson tmp = nfs_page_group_search_locked(req->wb_head, pos); 278d72ddcbaSWeston Andros Adamson if (tmp) { 279d72ddcbaSWeston Andros Adamson /* no way this should happen */ 280d72ddcbaSWeston Andros Adamson WARN_ON_ONCE(tmp->wb_pgbase != pos); 281d72ddcbaSWeston Andros Adamson pos += tmp->wb_bytes - (pos - tmp->wb_pgbase); 282d72ddcbaSWeston Andros Adamson } 283d72ddcbaSWeston Andros Adamson } while (tmp && pos < len); 284d72ddcbaSWeston Andros Adamson 285d72ddcbaSWeston Andros Adamson nfs_page_group_unlock(req); 286d72ddcbaSWeston Andros Adamson WARN_ON_ONCE(pos > len); 287d72ddcbaSWeston Andros Adamson return pos == len; 288d72ddcbaSWeston Andros Adamson } 289d72ddcbaSWeston Andros Adamson 2901da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request 2911da177e4SLinus Torvalds * covers the full page. 2921da177e4SLinus Torvalds */ 293d72ddcbaSWeston Andros Adamson static void nfs_mark_uptodate(struct nfs_page *req) 2941da177e4SLinus Torvalds { 295d72ddcbaSWeston Andros Adamson if (PageUptodate(req->wb_page)) 2961da177e4SLinus Torvalds return; 297d72ddcbaSWeston Andros Adamson if (!nfs_page_group_covers_page(req)) 2981da177e4SLinus Torvalds return; 299d72ddcbaSWeston Andros Adamson SetPageUptodate(req->wb_page); 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc) 3031da177e4SLinus Torvalds { 304e87b4c7aSNeilBrown int ret = 0; 305cca588d6STrond Myklebust 306e87b4c7aSNeilBrown if (wbc->sync_mode == WB_SYNC_ALL) 307e87b4c7aSNeilBrown ret = FLUSH_COND_STABLE; 308e87b4c7aSNeilBrown return ret; 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds /* 31289a09141SPeter Zijlstra * NFS congestion control 31389a09141SPeter Zijlstra */ 31489a09141SPeter Zijlstra 31589a09141SPeter Zijlstra int nfs_congestion_kb; 31689a09141SPeter Zijlstra 31789a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) 31889a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH \ 31989a09141SPeter Zijlstra (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) 32089a09141SPeter Zijlstra 321deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page) 32289a09141SPeter Zijlstra { 3230db10944SJan Kara struct inode *inode = page_file_mapping(page)->host; 3240db10944SJan Kara struct nfs_server *nfss = NFS_SERVER(inode); 3255a6d41b3STrond Myklebust int ret = test_set_page_writeback(page); 3265a6d41b3STrond Myklebust 327deed85e7STrond Myklebust WARN_ON_ONCE(ret != 0); 32889a09141SPeter Zijlstra 329277866a0SPeter Zijlstra if (atomic_long_inc_return(&nfss->writeback) > 3300db10944SJan Kara NFS_CONGESTION_ON_THRESH) 3310db10944SJan Kara set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC); 33289a09141SPeter Zijlstra } 33389a09141SPeter Zijlstra 33420633f04SWeston Andros Adamson static void nfs_end_page_writeback(struct nfs_page *req) 33589a09141SPeter Zijlstra { 33620633f04SWeston Andros Adamson struct inode *inode = page_file_mapping(req->wb_page)->host; 33789a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 33831a01f09STrond Myklebust bool is_done; 33989a09141SPeter Zijlstra 34031a01f09STrond Myklebust is_done = nfs_page_group_sync_on_bit(req, PG_WB_END); 34131a01f09STrond Myklebust nfs_unlock_request(req); 34231a01f09STrond Myklebust if (!is_done) 34320633f04SWeston Andros Adamson return; 34420633f04SWeston Andros Adamson 34520633f04SWeston Andros Adamson end_page_writeback(req->wb_page); 346c4dc4beeSPeter Zijlstra if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) 3470db10944SJan Kara clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC); 34889a09141SPeter Zijlstra } 34989a09141SPeter Zijlstra 350d4581383SWeston Andros Adamson /* 351d4581383SWeston Andros Adamson * nfs_unroll_locks_and_wait - unlock all newly locked reqs and wait on @req 352d4581383SWeston Andros Adamson * 353d4581383SWeston Andros Adamson * this is a helper function for nfs_lock_and_join_requests 354d4581383SWeston Andros Adamson * 355d4581383SWeston Andros Adamson * @inode - inode associated with request page group, must be holding inode lock 356d4581383SWeston Andros Adamson * @head - head request of page group, must be holding head lock 357d4581383SWeston Andros Adamson * @req - request that couldn't lock and needs to wait on the req bit lock 358d4581383SWeston Andros Adamson * 359b5bab9bfSTrond Myklebust * NOTE: this must be called holding page_group bit lock 360b5bab9bfSTrond Myklebust * which will be released before returning. 361d4581383SWeston Andros Adamson * 362d4581383SWeston Andros Adamson * returns 0 on success, < 0 on error. 363d4581383SWeston Andros Adamson */ 36474a6d4b5STrond Myklebust static void 36574a6d4b5STrond Myklebust nfs_unroll_locks(struct inode *inode, struct nfs_page *head, 3666d17d653STrond Myklebust struct nfs_page *req) 367d4581383SWeston Andros Adamson { 368d4581383SWeston Andros Adamson struct nfs_page *tmp; 369e261f51fSTrond Myklebust 370d4581383SWeston Andros Adamson /* relinquish all the locks successfully grabbed this run */ 3715b2b5187STrond Myklebust for (tmp = head->wb_this_page ; tmp != req; tmp = tmp->wb_this_page) { 3725b2b5187STrond Myklebust if (!kref_read(&tmp->wb_kref)) 3735b2b5187STrond Myklebust continue; 3745b2b5187STrond Myklebust nfs_unlock_and_release_request(tmp); 3755b2b5187STrond Myklebust } 376e261f51fSTrond Myklebust } 377d4581383SWeston Andros Adamson 378d4581383SWeston Andros Adamson /* 379d4581383SWeston Andros Adamson * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests 380d4581383SWeston Andros Adamson * 381d4581383SWeston Andros Adamson * @destroy_list - request list (using wb_this_page) terminated by @old_head 382d4581383SWeston Andros Adamson * @old_head - the old head of the list 383d4581383SWeston Andros Adamson * 384d4581383SWeston Andros Adamson * All subrequests must be locked and removed from all lists, so at this point 385d4581383SWeston Andros Adamson * they are only "active" in this function, and possibly in nfs_wait_on_request 386d4581383SWeston Andros Adamson * with a reference held by some other context. 387d4581383SWeston Andros Adamson */ 388d4581383SWeston Andros Adamson static void 389d4581383SWeston Andros Adamson nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list, 390b66aaa8dSTrond Myklebust struct nfs_page *old_head, 391b66aaa8dSTrond Myklebust struct inode *inode) 392d4581383SWeston Andros Adamson { 393d4581383SWeston Andros Adamson while (destroy_list) { 394d4581383SWeston Andros Adamson struct nfs_page *subreq = destroy_list; 395d4581383SWeston Andros Adamson 396d4581383SWeston Andros Adamson destroy_list = (subreq->wb_this_page == old_head) ? 397d4581383SWeston Andros Adamson NULL : subreq->wb_this_page; 398d4581383SWeston Andros Adamson 399d4581383SWeston Andros Adamson WARN_ON_ONCE(old_head != subreq->wb_head); 400d4581383SWeston Andros Adamson 401d4581383SWeston Andros Adamson /* make sure old group is not used */ 402d4581383SWeston Andros Adamson subreq->wb_this_page = subreq; 403d4581383SWeston Andros Adamson 404902a4c00STrond Myklebust clear_bit(PG_REMOVE, &subreq->wb_flags); 405902a4c00STrond Myklebust 4065b2b5187STrond Myklebust /* Note: races with nfs_page_group_destroy() */ 4075b2b5187STrond Myklebust if (!kref_read(&subreq->wb_kref)) { 4085b2b5187STrond Myklebust /* Check if we raced with nfs_page_group_destroy() */ 409902a4c00STrond Myklebust if (test_and_clear_bit(PG_TEARDOWN, &subreq->wb_flags)) 4105b2b5187STrond Myklebust nfs_free_request(subreq); 4115b2b5187STrond Myklebust continue; 4125b2b5187STrond Myklebust } 413d4581383SWeston Andros Adamson 4145b2b5187STrond Myklebust subreq->wb_head = subreq; 4155b2b5187STrond Myklebust 416b66aaa8dSTrond Myklebust if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags)) { 417d4581383SWeston Andros Adamson nfs_release_request(subreq); 418b66aaa8dSTrond Myklebust spin_lock(&inode->i_lock); 419b66aaa8dSTrond Myklebust NFS_I(inode)->nrequests--; 420b66aaa8dSTrond Myklebust spin_unlock(&inode->i_lock); 421d4581383SWeston Andros Adamson } 4225b2b5187STrond Myklebust 4235b2b5187STrond Myklebust /* subreq is now totally disconnected from page group or any 4245b2b5187STrond Myklebust * write / commit lists. last chance to wake any waiters */ 4255b2b5187STrond Myklebust nfs_unlock_and_release_request(subreq); 426d4581383SWeston Andros Adamson } 427d4581383SWeston Andros Adamson } 428d4581383SWeston Andros Adamson 429d4581383SWeston Andros Adamson /* 430d4581383SWeston Andros Adamson * nfs_lock_and_join_requests - join all subreqs to the head req and return 431d4581383SWeston Andros Adamson * a locked reference, cancelling any pending 432d4581383SWeston Andros Adamson * operations for this page. 433d4581383SWeston Andros Adamson * 434d4581383SWeston Andros Adamson * @page - the page used to lookup the "page group" of nfs_page structures 435d4581383SWeston Andros Adamson * 436d4581383SWeston Andros Adamson * This function joins all sub requests to the head request by first 437d4581383SWeston Andros Adamson * locking all requests in the group, cancelling any pending operations 438d4581383SWeston Andros Adamson * and finally updating the head request to cover the whole range covered by 439d4581383SWeston Andros Adamson * the (former) group. All subrequests are removed from any write or commit 440d4581383SWeston Andros Adamson * lists, unlinked from the group and destroyed. 441d4581383SWeston Andros Adamson * 442d4581383SWeston Andros Adamson * Returns a locked, referenced pointer to the head request - which after 443d4581383SWeston Andros Adamson * this call is guaranteed to be the only request associated with the page. 444d4581383SWeston Andros Adamson * Returns NULL if no requests are found for @page, or a ERR_PTR if an 445d4581383SWeston Andros Adamson * error was encountered. 446d4581383SWeston Andros Adamson */ 447d4581383SWeston Andros Adamson static struct nfs_page * 4486d17d653STrond Myklebust nfs_lock_and_join_requests(struct page *page) 449d4581383SWeston Andros Adamson { 450d4581383SWeston Andros Adamson struct inode *inode = page_file_mapping(page)->host; 451d4581383SWeston Andros Adamson struct nfs_page *head, *subreq; 452d4581383SWeston Andros Adamson struct nfs_page *destroy_list = NULL; 453d4581383SWeston Andros Adamson unsigned int total_bytes; 454d4581383SWeston Andros Adamson int ret; 455d4581383SWeston Andros Adamson 456d4581383SWeston Andros Adamson try_again: 457b5bab9bfSTrond Myklebust if (!(PagePrivate(page) || PageSwapCache(page))) 458b5bab9bfSTrond Myklebust return NULL; 459d4581383SWeston Andros Adamson spin_lock(&inode->i_lock); 460d4581383SWeston Andros Adamson /* 461d4581383SWeston Andros Adamson * A reference is taken only on the head request which acts as a 462d4581383SWeston Andros Adamson * reference to the whole page group - the group will not be destroyed 463d4581383SWeston Andros Adamson * until the head reference is released. 464d4581383SWeston Andros Adamson */ 465d4581383SWeston Andros Adamson head = nfs_page_find_head_request_locked(NFS_I(inode), page); 466d4581383SWeston Andros Adamson 467d4581383SWeston Andros Adamson if (!head) { 468587142f8STrond Myklebust spin_unlock(&inode->i_lock); 469d4581383SWeston Andros Adamson return NULL; 470d4581383SWeston Andros Adamson } 471d4581383SWeston Andros Adamson 472a0e265bcSTrond Myklebust /* lock the page head first in order to avoid an ABBA inefficiency */ 473a0e265bcSTrond Myklebust if (!nfs_lock_request(head)) { 474a0e265bcSTrond Myklebust spin_unlock(&inode->i_lock); 475a0e265bcSTrond Myklebust ret = nfs_wait_on_request(head); 476a0e265bcSTrond Myklebust nfs_release_request(head); 477a0e265bcSTrond Myklebust if (ret < 0) 478a0e265bcSTrond Myklebust return ERR_PTR(ret); 479a0e265bcSTrond Myklebust goto try_again; 480a0e265bcSTrond Myklebust } 48194970014SWeston Andros Adamson spin_unlock(&inode->i_lock); 4827c3af975SWeston Andros Adamson 483*1344b7eaSTrond Myklebust ret = nfs_page_group_lock(head); 484b5bab9bfSTrond Myklebust if (ret < 0) { 485a0e265bcSTrond Myklebust nfs_unlock_and_release_request(head); 486b5bab9bfSTrond Myklebust return ERR_PTR(ret); 4877c3af975SWeston Andros Adamson } 4887c3af975SWeston Andros Adamson 4897c3af975SWeston Andros Adamson /* lock each request in the page group */ 490a0e265bcSTrond Myklebust total_bytes = head->wb_bytes; 491a0e265bcSTrond Myklebust for (subreq = head->wb_this_page; subreq != head; 492a0e265bcSTrond Myklebust subreq = subreq->wb_this_page) { 49374a6d4b5STrond Myklebust 4945b2b5187STrond Myklebust if (!kref_get_unless_zero(&subreq->wb_kref)) 4955b2b5187STrond Myklebust continue; 49674a6d4b5STrond Myklebust while (!nfs_lock_request(subreq)) { 497b5bab9bfSTrond Myklebust /* 49874a6d4b5STrond Myklebust * Unlock page to allow nfs_page_group_sync_on_bit() 49974a6d4b5STrond Myklebust * to succeed 500b5bab9bfSTrond Myklebust */ 50174a6d4b5STrond Myklebust nfs_page_group_unlock(head); 50274a6d4b5STrond Myklebust ret = nfs_wait_on_request(subreq); 50374a6d4b5STrond Myklebust if (!ret) 504*1344b7eaSTrond Myklebust ret = nfs_page_group_lock(head); 50574a6d4b5STrond Myklebust if (ret < 0) { 50674a6d4b5STrond Myklebust nfs_unroll_locks(inode, head, subreq); 5075b2b5187STrond Myklebust nfs_release_request(subreq); 50874a6d4b5STrond Myklebust nfs_unlock_and_release_request(head); 509d4581383SWeston Andros Adamson return ERR_PTR(ret); 510d4581383SWeston Andros Adamson } 51174a6d4b5STrond Myklebust } 512e14bebf6STrond Myklebust /* 513e14bebf6STrond Myklebust * Subrequests are always contiguous, non overlapping 514e14bebf6STrond Myklebust * and in order - but may be repeated (mirrored writes). 515e14bebf6STrond Myklebust */ 516e14bebf6STrond Myklebust if (subreq->wb_offset == (head->wb_offset + total_bytes)) { 517e14bebf6STrond Myklebust /* keep track of how many bytes this group covers */ 518e14bebf6STrond Myklebust total_bytes += subreq->wb_bytes; 519e14bebf6STrond Myklebust } else if (WARN_ON_ONCE(subreq->wb_offset < head->wb_offset || 520e14bebf6STrond Myklebust ((subreq->wb_offset + subreq->wb_bytes) > 521e14bebf6STrond Myklebust (head->wb_offset + total_bytes)))) { 52274a6d4b5STrond Myklebust nfs_unroll_locks(inode, head, subreq); 5235b2b5187STrond Myklebust nfs_unlock_and_release_request(subreq); 52474a6d4b5STrond Myklebust nfs_page_group_unlock(head); 52574a6d4b5STrond Myklebust nfs_unlock_and_release_request(head); 526e14bebf6STrond Myklebust return ERR_PTR(-EIO); 527e14bebf6STrond Myklebust } 528a0e265bcSTrond Myklebust } 529d4581383SWeston Andros Adamson 530d4581383SWeston Andros Adamson /* Now that all requests are locked, make sure they aren't on any list. 531d4581383SWeston Andros Adamson * Commit list removal accounting is done after locks are dropped */ 532d4581383SWeston Andros Adamson subreq = head; 533d4581383SWeston Andros Adamson do { 534411a99adSWeston Andros Adamson nfs_clear_request_commit(subreq); 535d4581383SWeston Andros Adamson subreq = subreq->wb_this_page; 536d4581383SWeston Andros Adamson } while (subreq != head); 537d4581383SWeston Andros Adamson 538d4581383SWeston Andros Adamson /* unlink subrequests from head, destroy them later */ 539d4581383SWeston Andros Adamson if (head->wb_this_page != head) { 540d4581383SWeston Andros Adamson /* destroy list will be terminated by head */ 541d4581383SWeston Andros Adamson destroy_list = head->wb_this_page; 542d4581383SWeston Andros Adamson head->wb_this_page = head; 543d4581383SWeston Andros Adamson 544d4581383SWeston Andros Adamson /* change head request to cover whole range that 545d4581383SWeston Andros Adamson * the former page group covered */ 546d4581383SWeston Andros Adamson head->wb_bytes = total_bytes; 547d4581383SWeston Andros Adamson } 548d4581383SWeston Andros Adamson 549b66aaa8dSTrond Myklebust /* Postpone destruction of this request */ 550b66aaa8dSTrond Myklebust if (test_and_clear_bit(PG_REMOVE, &head->wb_flags)) { 551b66aaa8dSTrond Myklebust set_bit(PG_INODE_REF, &head->wb_flags); 552b66aaa8dSTrond Myklebust kref_get(&head->wb_kref); 553b5bab9bfSTrond Myklebust spin_lock(&inode->i_lock); 554b66aaa8dSTrond Myklebust NFS_I(inode)->nrequests++; 555b5bab9bfSTrond Myklebust spin_unlock(&inode->i_lock); 556b66aaa8dSTrond Myklebust } 557b66aaa8dSTrond Myklebust 558d4581383SWeston Andros Adamson nfs_page_group_unlock(head); 559d4581383SWeston Andros Adamson 560b66aaa8dSTrond Myklebust nfs_destroy_unlinked_subrequests(destroy_list, head, inode); 561d4581383SWeston Andros Adamson 562b5bab9bfSTrond Myklebust /* Did we lose a race with nfs_inode_remove_request()? */ 563b5bab9bfSTrond Myklebust if (!(PagePrivate(page) || PageSwapCache(page))) { 564b5bab9bfSTrond Myklebust nfs_unlock_and_release_request(head); 565b5bab9bfSTrond Myklebust return NULL; 566b5bab9bfSTrond Myklebust } 567b5bab9bfSTrond Myklebust 568d4581383SWeston Andros Adamson /* still holds ref on head from nfs_page_find_head_request_locked 569d4581383SWeston Andros Adamson * and still has lock on head from lock loop */ 570d4581383SWeston Andros Adamson return head; 571612c9384STrond Myklebust } 572074cc1deSTrond Myklebust 5730bcbf039SPeng Tao static void nfs_write_error_remove_page(struct nfs_page *req) 5740bcbf039SPeng Tao { 5750bcbf039SPeng Tao nfs_end_page_writeback(req); 5760bcbf039SPeng Tao generic_error_remove_page(page_file_mapping(req->wb_page), 5770bcbf039SPeng Tao req->wb_page); 5781f84ccdfSFred Isaman nfs_release_request(req); 5790bcbf039SPeng Tao } 5800bcbf039SPeng Tao 581a6598813STrond Myklebust static bool 582a6598813STrond Myklebust nfs_error_is_fatal_on_server(int err) 583a6598813STrond Myklebust { 584a6598813STrond Myklebust switch (err) { 585a6598813STrond Myklebust case 0: 586a6598813STrond Myklebust case -ERESTARTSYS: 587a6598813STrond Myklebust case -EINTR: 588a6598813STrond Myklebust return false; 589a6598813STrond Myklebust } 590a6598813STrond Myklebust return nfs_error_is_fatal(err); 591e261f51fSTrond Myklebust } 592074cc1deSTrond Myklebust 593074cc1deSTrond Myklebust /* 594074cc1deSTrond Myklebust * Find an associated nfs write request, and prepare to flush it out 595074cc1deSTrond Myklebust * May return an error if the user signalled nfs_wait_on_request(). 596074cc1deSTrond Myklebust */ 597074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, 5986d17d653STrond Myklebust struct page *page) 599074cc1deSTrond Myklebust { 600074cc1deSTrond Myklebust struct nfs_page *req; 601074cc1deSTrond Myklebust int ret = 0; 602074cc1deSTrond Myklebust 6036d17d653STrond Myklebust req = nfs_lock_and_join_requests(page); 604074cc1deSTrond Myklebust if (!req) 605074cc1deSTrond Myklebust goto out; 606074cc1deSTrond Myklebust ret = PTR_ERR(req); 607074cc1deSTrond Myklebust if (IS_ERR(req)) 608074cc1deSTrond Myklebust goto out; 609074cc1deSTrond Myklebust 610deed85e7STrond Myklebust nfs_set_page_writeback(page); 611deed85e7STrond Myklebust WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags)); 612074cc1deSTrond Myklebust 613deed85e7STrond Myklebust ret = 0; 614a6598813STrond Myklebust /* If there is a fatal error that covers this write, just exit */ 615a6598813STrond Myklebust if (nfs_error_is_fatal_on_server(req->wb_context->error)) 616a6598813STrond Myklebust goto out_launder; 617a6598813STrond Myklebust 618f8512ad0SFred Isaman if (!nfs_pageio_add_request(pgio, req)) { 619074cc1deSTrond Myklebust ret = pgio->pg_error; 6200bcbf039SPeng Tao /* 621c373fff7STrond Myklebust * Remove the problematic req upon fatal errors on the server 6220bcbf039SPeng Tao */ 6230bcbf039SPeng Tao if (nfs_error_is_fatal(ret)) { 6240bcbf039SPeng Tao nfs_context_set_write_error(req->wb_context, ret); 625c373fff7STrond Myklebust if (nfs_error_is_fatal_on_server(ret)) 626a6598813STrond Myklebust goto out_launder; 627d6c843b9SPeng Tao } 6280bcbf039SPeng Tao nfs_redirty_request(req); 6290bcbf039SPeng Tao ret = -EAGAIN; 63040f90271STrond Myklebust } else 63140f90271STrond Myklebust nfs_add_stats(page_file_mapping(page)->host, 63240f90271STrond Myklebust NFSIOS_WRITEPAGES, 1); 633074cc1deSTrond Myklebust out: 634074cc1deSTrond Myklebust return ret; 635a6598813STrond Myklebust out_launder: 636a6598813STrond Myklebust nfs_write_error_remove_page(req); 637a6598813STrond Myklebust return ret; 638e261f51fSTrond Myklebust } 639e261f51fSTrond Myklebust 640d6c843b9SPeng Tao static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, 641c373fff7STrond Myklebust struct nfs_pageio_descriptor *pgio) 642f758c885STrond Myklebust { 643cfb506e1STrond Myklebust int ret; 644f758c885STrond Myklebust 6458cd79788SHuang Ying nfs_pageio_cond_complete(pgio, page_index(page)); 6466d17d653STrond Myklebust ret = nfs_page_async_flush(pgio, page); 647cfb506e1STrond Myklebust if (ret == -EAGAIN) { 648cfb506e1STrond Myklebust redirty_page_for_writepage(wbc, page); 649cfb506e1STrond Myklebust ret = 0; 650cfb506e1STrond Myklebust } 651cfb506e1STrond Myklebust return ret; 652f758c885STrond Myklebust } 653f758c885STrond Myklebust 654e261f51fSTrond Myklebust /* 6551da177e4SLinus Torvalds * Write an mmapped page to the server. 6561da177e4SLinus Torvalds */ 657d6c843b9SPeng Tao static int nfs_writepage_locked(struct page *page, 658c373fff7STrond Myklebust struct writeback_control *wbc) 6591da177e4SLinus Torvalds { 660f758c885STrond Myklebust struct nfs_pageio_descriptor pgio; 66140f90271STrond Myklebust struct inode *inode = page_file_mapping(page)->host; 662e261f51fSTrond Myklebust int err; 6631da177e4SLinus Torvalds 66440f90271STrond Myklebust nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); 665811ed92eSTrond Myklebust nfs_pageio_init_write(&pgio, inode, 0, 666a20c93e3SChristoph Hellwig false, &nfs_async_write_completion_ops); 667c373fff7STrond Myklebust err = nfs_do_writepage(page, wbc, &pgio); 668f758c885STrond Myklebust nfs_pageio_complete(&pgio); 669f758c885STrond Myklebust if (err < 0) 6704d770ccfSTrond Myklebust return err; 671f758c885STrond Myklebust if (pgio.pg_error < 0) 672f758c885STrond Myklebust return pgio.pg_error; 673f758c885STrond Myklebust return 0; 6744d770ccfSTrond Myklebust } 6754d770ccfSTrond Myklebust 6764d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc) 6774d770ccfSTrond Myklebust { 678f758c885STrond Myklebust int ret; 6794d770ccfSTrond Myklebust 680c373fff7STrond Myklebust ret = nfs_writepage_locked(page, wbc); 6811da177e4SLinus Torvalds unlock_page(page); 682f758c885STrond Myklebust return ret; 683f758c885STrond Myklebust } 684f758c885STrond Myklebust 685f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) 686f758c885STrond Myklebust { 687f758c885STrond Myklebust int ret; 688f758c885STrond Myklebust 689c373fff7STrond Myklebust ret = nfs_do_writepage(page, wbc, data); 690f758c885STrond Myklebust unlock_page(page); 691f758c885STrond Myklebust return ret; 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds 694919e3bd9STrond Myklebust static void nfs_io_completion_commit(void *inode) 695919e3bd9STrond Myklebust { 696919e3bd9STrond Myklebust nfs_commit_inode(inode, 0); 697919e3bd9STrond Myklebust } 698919e3bd9STrond Myklebust 6991da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) 7001da177e4SLinus Torvalds { 7011da177e4SLinus Torvalds struct inode *inode = mapping->host; 702c63c7b05STrond Myklebust struct nfs_pageio_descriptor pgio; 703919e3bd9STrond Myklebust struct nfs_io_completion *ioc = nfs_io_completion_alloc(GFP_NOFS); 7041da177e4SLinus Torvalds int err; 7051da177e4SLinus Torvalds 70691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); 70791d5b470SChuck Lever 708919e3bd9STrond Myklebust if (ioc) 709919e3bd9STrond Myklebust nfs_io_completion_init(ioc, nfs_io_completion_commit, inode); 710919e3bd9STrond Myklebust 711a20c93e3SChristoph Hellwig nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false, 712a20c93e3SChristoph Hellwig &nfs_async_write_completion_ops); 713919e3bd9STrond Myklebust pgio.pg_io_completion = ioc; 714f758c885STrond Myklebust err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); 715c63c7b05STrond Myklebust nfs_pageio_complete(&pgio); 716919e3bd9STrond Myklebust nfs_io_completion_put(ioc); 71772cb77f4STrond Myklebust 718f758c885STrond Myklebust if (err < 0) 71972cb77f4STrond Myklebust goto out_err; 72072cb77f4STrond Myklebust err = pgio.pg_error; 72172cb77f4STrond Myklebust if (err < 0) 72272cb77f4STrond Myklebust goto out_err; 723c63c7b05STrond Myklebust return 0; 72472cb77f4STrond Myklebust out_err: 72572cb77f4STrond Myklebust return err; 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds 7281da177e4SLinus Torvalds /* 7291da177e4SLinus Torvalds * Insert a write request into an inode 7301da177e4SLinus Torvalds */ 731d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req) 7321da177e4SLinus Torvalds { 7331da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 734e7d39069STrond Myklebust 7352bfc6e56SWeston Andros Adamson WARN_ON_ONCE(req->wb_this_page != req); 7362bfc6e56SWeston Andros Adamson 737e7d39069STrond Myklebust /* Lock the request! */ 7387ad84aa9STrond Myklebust nfs_lock_request(req); 739e7d39069STrond Myklebust 740e7d39069STrond Myklebust spin_lock(&inode->i_lock); 741cb1410c7SWeston Andros Adamson if (!nfsi->nrequests && 742cb1410c7SWeston Andros Adamson NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE)) 743a9a4a87aSTrond Myklebust inode->i_version++; 74429418aa4SMel Gorman /* 74529418aa4SMel Gorman * Swap-space should not get truncated. Hence no need to plug the race 74629418aa4SMel Gorman * with invalidate/truncate. 74729418aa4SMel Gorman */ 74829418aa4SMel Gorman if (likely(!PageSwapCache(req->wb_page))) { 7492df485a7STrond Myklebust set_bit(PG_MAPPED, &req->wb_flags); 750deb7d638STrond Myklebust SetPagePrivate(req->wb_page); 751277459d2STrond Myklebust set_page_private(req->wb_page, (unsigned long)req); 75229418aa4SMel Gorman } 753cb1410c7SWeston Andros Adamson nfsi->nrequests++; 75417089a29SWeston Andros Adamson /* this a head request for a page group - mark it as having an 755cb1410c7SWeston Andros Adamson * extra reference so sub groups can follow suit. 756cb1410c7SWeston Andros Adamson * This flag also informs pgio layer when to bump nrequests when 757cb1410c7SWeston Andros Adamson * adding subrequests. */ 75817089a29SWeston Andros Adamson WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags)); 759c03b4024STrond Myklebust kref_get(&req->wb_kref); 760e7d39069STrond Myklebust spin_unlock(&inode->i_lock); 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds /* 76489a09141SPeter Zijlstra * Remove a write request from an inode 7651da177e4SLinus Torvalds */ 7661da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req) 7671da177e4SLinus Torvalds { 7682b0143b5SDavid Howells struct inode *inode = d_inode(req->wb_context->dentry); 7691da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 77020633f04SWeston Andros Adamson struct nfs_page *head; 77120633f04SWeston Andros Adamson 77220633f04SWeston Andros Adamson if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) { 77320633f04SWeston Andros Adamson head = req->wb_head; 7741da177e4SLinus Torvalds 775587142f8STrond Myklebust spin_lock(&inode->i_lock); 77667911c8fSAnna Schumaker if (likely(head->wb_page && !PageSwapCache(head->wb_page))) { 77720633f04SWeston Andros Adamson set_page_private(head->wb_page, 0); 77820633f04SWeston Andros Adamson ClearPagePrivate(head->wb_page); 77920633f04SWeston Andros Adamson clear_bit(PG_MAPPED, &head->wb_flags); 78029418aa4SMel Gorman } 781cb1410c7SWeston Andros Adamson nfsi->nrequests--; 782cb1410c7SWeston Andros Adamson spin_unlock(&inode->i_lock); 783cb1410c7SWeston Andros Adamson } else { 784cb1410c7SWeston Andros Adamson spin_lock(&inode->i_lock); 785cb1410c7SWeston Andros Adamson nfsi->nrequests--; 786587142f8STrond Myklebust spin_unlock(&inode->i_lock); 78720633f04SWeston Andros Adamson } 78817089a29SWeston Andros Adamson 78917089a29SWeston Andros Adamson if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags)) 7901da177e4SLinus Torvalds nfs_release_request(req); 7911da177e4SLinus Torvalds } 7921da177e4SLinus Torvalds 79361822ab5STrond Myklebust static void 7946d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req) 79561822ab5STrond Myklebust { 79667911c8fSAnna Schumaker if (req->wb_page) 79761822ab5STrond Myklebust __set_page_dirty_nobuffers(req->wb_page); 79861822ab5STrond Myklebust } 79961822ab5STrond Myklebust 8003a3908c8STrond Myklebust /* 8013a3908c8STrond Myklebust * nfs_page_search_commits_for_head_request_locked 8023a3908c8STrond Myklebust * 8033a3908c8STrond Myklebust * Search through commit lists on @inode for the head request for @page. 8043a3908c8STrond Myklebust * Must be called while holding the inode (which is cinfo) lock. 8053a3908c8STrond Myklebust * 8063a3908c8STrond Myklebust * Returns the head request if found, or NULL if not found. 8073a3908c8STrond Myklebust */ 8083a3908c8STrond Myklebust static struct nfs_page * 8093a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi, 8103a3908c8STrond Myklebust struct page *page) 8113a3908c8STrond Myklebust { 8123a3908c8STrond Myklebust struct nfs_page *freq, *t; 8133a3908c8STrond Myklebust struct nfs_commit_info cinfo; 8143a3908c8STrond Myklebust struct inode *inode = &nfsi->vfs_inode; 8153a3908c8STrond Myklebust 8163a3908c8STrond Myklebust nfs_init_cinfo_from_inode(&cinfo, inode); 8173a3908c8STrond Myklebust 8183a3908c8STrond Myklebust /* search through pnfs commit lists */ 8193a3908c8STrond Myklebust freq = pnfs_search_commit_reqs(inode, &cinfo, page); 8203a3908c8STrond Myklebust if (freq) 8213a3908c8STrond Myklebust return freq->wb_head; 8223a3908c8STrond Myklebust 8233a3908c8STrond Myklebust /* Linearly search the commit list for the correct request */ 8243a3908c8STrond Myklebust list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) { 8253a3908c8STrond Myklebust if (freq->wb_page == page) 8263a3908c8STrond Myklebust return freq->wb_head; 8273a3908c8STrond Myklebust } 8283a3908c8STrond Myklebust 8293a3908c8STrond Myklebust return NULL; 8303a3908c8STrond Myklebust } 8313a3908c8STrond Myklebust 8328dd37758STrond Myklebust /** 83386d80f97STrond Myklebust * nfs_request_add_commit_list_locked - add request to a commit list 83486d80f97STrond Myklebust * @req: pointer to a struct nfs_page 83586d80f97STrond Myklebust * @dst: commit list head 83686d80f97STrond Myklebust * @cinfo: holds list lock and accounting info 83786d80f97STrond Myklebust * 83886d80f97STrond Myklebust * This sets the PG_CLEAN bit, updates the cinfo count of 83986d80f97STrond Myklebust * number of outstanding requests requiring a commit as well as 84086d80f97STrond Myklebust * the MM page stats. 84186d80f97STrond Myklebust * 842fe238e60SDave Wysochanski * The caller must hold cinfo->inode->i_lock, and the nfs_page lock. 84386d80f97STrond Myklebust */ 84486d80f97STrond Myklebust void 84586d80f97STrond Myklebust nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst, 84686d80f97STrond Myklebust struct nfs_commit_info *cinfo) 84786d80f97STrond Myklebust { 84886d80f97STrond Myklebust set_bit(PG_CLEAN, &req->wb_flags); 84986d80f97STrond Myklebust nfs_list_add_request(req, dst); 85086d80f97STrond Myklebust cinfo->mds->ncommit++; 85186d80f97STrond Myklebust } 85286d80f97STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked); 85386d80f97STrond Myklebust 85486d80f97STrond Myklebust /** 8558dd37758STrond Myklebust * nfs_request_add_commit_list - add request to a commit list 8568dd37758STrond Myklebust * @req: pointer to a struct nfs_page 857ea2cf228SFred Isaman * @dst: commit list head 858ea2cf228SFred Isaman * @cinfo: holds list lock and accounting info 8598dd37758STrond Myklebust * 860ea2cf228SFred Isaman * This sets the PG_CLEAN bit, updates the cinfo count of 8618dd37758STrond Myklebust * number of outstanding requests requiring a commit as well as 8628dd37758STrond Myklebust * the MM page stats. 8638dd37758STrond Myklebust * 864ea2cf228SFred Isaman * The caller must _not_ hold the cinfo->lock, but must be 8658dd37758STrond Myklebust * holding the nfs_page lock. 8668dd37758STrond Myklebust */ 8678dd37758STrond Myklebust void 8686272dcc6SAnna Schumaker nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo) 8698dd37758STrond Myklebust { 870fe238e60SDave Wysochanski spin_lock(&cinfo->inode->i_lock); 8716272dcc6SAnna Schumaker nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo); 872fe238e60SDave Wysochanski spin_unlock(&cinfo->inode->i_lock); 87367911c8fSAnna Schumaker if (req->wb_page) 87486d80f97STrond Myklebust nfs_mark_page_unstable(req->wb_page, cinfo); 8758dd37758STrond Myklebust } 8768dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list); 8778dd37758STrond Myklebust 8788dd37758STrond Myklebust /** 8798dd37758STrond Myklebust * nfs_request_remove_commit_list - Remove request from a commit list 8808dd37758STrond Myklebust * @req: pointer to a nfs_page 881ea2cf228SFred Isaman * @cinfo: holds list lock and accounting info 8828dd37758STrond Myklebust * 883ea2cf228SFred Isaman * This clears the PG_CLEAN bit, and updates the cinfo's count of 8848dd37758STrond Myklebust * number of outstanding requests requiring a commit 8858dd37758STrond Myklebust * It does not update the MM page stats. 8868dd37758STrond Myklebust * 887ea2cf228SFred Isaman * The caller _must_ hold the cinfo->lock and the nfs_page lock. 8888dd37758STrond Myklebust */ 8898dd37758STrond Myklebust void 890ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req, 891ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 8928dd37758STrond Myklebust { 8938dd37758STrond Myklebust if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) 8948dd37758STrond Myklebust return; 8958dd37758STrond Myklebust nfs_list_remove_request(req); 896ea2cf228SFred Isaman cinfo->mds->ncommit--; 8978dd37758STrond Myklebust } 8988dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list); 8998dd37758STrond Myklebust 900ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, 901ea2cf228SFred Isaman struct inode *inode) 902ea2cf228SFred Isaman { 903fe238e60SDave Wysochanski cinfo->inode = inode; 904ea2cf228SFred Isaman cinfo->mds = &NFS_I(inode)->commit_info; 905ea2cf228SFred Isaman cinfo->ds = pnfs_get_ds_info(inode); 906b359f9d0SFred Isaman cinfo->dreq = NULL; 907f453a54aSFred Isaman cinfo->completion_ops = &nfs_commit_completion_ops; 908ea2cf228SFred Isaman } 909ea2cf228SFred Isaman 910ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo, 911ea2cf228SFred Isaman struct inode *inode, 912ea2cf228SFred Isaman struct nfs_direct_req *dreq) 913ea2cf228SFred Isaman { 9141763da12SFred Isaman if (dreq) 9151763da12SFred Isaman nfs_init_cinfo_from_dreq(cinfo, dreq); 9161763da12SFred Isaman else 917ea2cf228SFred Isaman nfs_init_cinfo_from_inode(cinfo, inode); 918ea2cf228SFred Isaman } 919ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo); 9208dd37758STrond Myklebust 9211da177e4SLinus Torvalds /* 9221da177e4SLinus Torvalds * Add a request to the inode's commit list. 9231da177e4SLinus Torvalds */ 9241763da12SFred Isaman void 925ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, 926b57ff130SWeston Andros Adamson struct nfs_commit_info *cinfo, u32 ds_commit_idx) 9271da177e4SLinus Torvalds { 928b57ff130SWeston Andros Adamson if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx)) 9298dd37758STrond Myklebust return; 9306272dcc6SAnna Schumaker nfs_request_add_commit_list(req, cinfo); 9311da177e4SLinus Torvalds } 9328e821cadSTrond Myklebust 933d6d6dc7cSFred Isaman static void 934d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page) 935e468bae9STrond Myklebust { 93611fb9989SMel Gorman dec_node_page_state(page, NR_UNSTABLE_NFS); 93793f78d88STejun Heo dec_wb_stat(&inode_to_bdi(page_file_mapping(page)->host)->wb, 93893f78d88STejun Heo WB_RECLAIMABLE); 939e468bae9STrond Myklebust } 940d6d6dc7cSFred Isaman 941b5bab9bfSTrond Myklebust /* Called holding the request lock on @req */ 9428dd37758STrond Myklebust static void 943d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req) 944d6d6dc7cSFred Isaman { 9458dd37758STrond Myklebust if (test_bit(PG_CLEAN, &req->wb_flags)) { 9462b0143b5SDavid Howells struct inode *inode = d_inode(req->wb_context->dentry); 947ea2cf228SFred Isaman struct nfs_commit_info cinfo; 948d6d6dc7cSFred Isaman 949ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, inode); 950b5bab9bfSTrond Myklebust spin_lock(&inode->i_lock); 951ea2cf228SFred Isaman if (!pnfs_clear_request_commit(req, &cinfo)) { 952ea2cf228SFred Isaman nfs_request_remove_commit_list(req, &cinfo); 953d6d6dc7cSFred Isaman } 954b5bab9bfSTrond Myklebust spin_unlock(&inode->i_lock); 9558dd37758STrond Myklebust nfs_clear_page_commit(req->wb_page); 9568dd37758STrond Myklebust } 957e468bae9STrond Myklebust } 958e468bae9STrond Myklebust 959d45f60c6SWeston Andros Adamson int nfs_write_need_commit(struct nfs_pgio_header *hdr) 9608e821cadSTrond Myklebust { 961c65e6254SWeston Andros Adamson if (hdr->verf.committed == NFS_DATA_SYNC) 962d45f60c6SWeston Andros Adamson return hdr->lseg == NULL; 963c65e6254SWeston Andros Adamson return hdr->verf.committed != NFS_FILE_SYNC; 9648e821cadSTrond Myklebust } 9658e821cadSTrond Myklebust 966919e3bd9STrond Myklebust static void nfs_async_write_init(struct nfs_pgio_header *hdr) 967919e3bd9STrond Myklebust { 968919e3bd9STrond Myklebust nfs_io_completion_get(hdr->io_completion); 969919e3bd9STrond Myklebust } 970919e3bd9STrond Myklebust 971061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr) 9726c75dc0dSFred Isaman { 973ea2cf228SFred Isaman struct nfs_commit_info cinfo; 9746c75dc0dSFred Isaman unsigned long bytes = 0; 9756c75dc0dSFred Isaman 9766c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 9776c75dc0dSFred Isaman goto out; 978ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, hdr->inode); 9796c75dc0dSFred Isaman while (!list_empty(&hdr->pages)) { 9806c75dc0dSFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 9816c75dc0dSFred Isaman 9826c75dc0dSFred Isaman bytes += req->wb_bytes; 9836c75dc0dSFred Isaman nfs_list_remove_request(req); 9846c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && 9856c75dc0dSFred Isaman (hdr->good_bytes < bytes)) { 986d1182b33STrond Myklebust nfs_set_pageerror(req->wb_page); 9876c75dc0dSFred Isaman nfs_context_set_write_error(req->wb_context, hdr->error); 9886c75dc0dSFred Isaman goto remove_req; 9896c75dc0dSFred Isaman } 990c65e6254SWeston Andros Adamson if (nfs_write_need_commit(hdr)) { 991f79d06f5SAnna Schumaker memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf)); 992b57ff130SWeston Andros Adamson nfs_mark_request_commit(req, hdr->lseg, &cinfo, 993a7d42ddbSWeston Andros Adamson hdr->pgio_mirror_idx); 9946c75dc0dSFred Isaman goto next; 9956c75dc0dSFred Isaman } 9966c75dc0dSFred Isaman remove_req: 9976c75dc0dSFred Isaman nfs_inode_remove_request(req); 9986c75dc0dSFred Isaman next: 99920633f04SWeston Andros Adamson nfs_end_page_writeback(req); 10003aff4ebbSTrond Myklebust nfs_release_request(req); 10016c75dc0dSFred Isaman } 10026c75dc0dSFred Isaman out: 1003919e3bd9STrond Myklebust nfs_io_completion_put(hdr->io_completion); 10046c75dc0dSFred Isaman hdr->release(hdr); 10056c75dc0dSFred Isaman } 10066c75dc0dSFred Isaman 1007ce59515cSAnna Schumaker unsigned long 1008ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo) 1009fb8a1f11STrond Myklebust { 1010ea2cf228SFred Isaman return cinfo->mds->ncommit; 1011fb8a1f11STrond Myklebust } 1012fb8a1f11STrond Myklebust 1013fe238e60SDave Wysochanski /* cinfo->inode->i_lock held by caller */ 10141763da12SFred Isaman int 1015ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst, 1016ea2cf228SFred Isaman struct nfs_commit_info *cinfo, int max) 1017d6d6dc7cSFred Isaman { 1018d6d6dc7cSFred Isaman struct nfs_page *req, *tmp; 1019d6d6dc7cSFred Isaman int ret = 0; 1020d6d6dc7cSFred Isaman 1021d6d6dc7cSFred Isaman list_for_each_entry_safe(req, tmp, src, wb_list) { 10228dd37758STrond Myklebust if (!nfs_lock_request(req)) 10238dd37758STrond Myklebust continue; 10247ad84aa9STrond Myklebust kref_get(&req->wb_kref); 1025fe238e60SDave Wysochanski if (cond_resched_lock(&cinfo->inode->i_lock)) 10263b3be88dSTrond Myklebust list_safe_reset_next(req, tmp, wb_list); 1027ea2cf228SFred Isaman nfs_request_remove_commit_list(req, cinfo); 10288dd37758STrond Myklebust nfs_list_add_request(req, dst); 1029d6d6dc7cSFred Isaman ret++; 10301763da12SFred Isaman if ((ret == max) && !cinfo->dreq) 1031d6d6dc7cSFred Isaman break; 1032d6d6dc7cSFred Isaman } 1033d6d6dc7cSFred Isaman return ret; 1034d6d6dc7cSFred Isaman } 1035d6d6dc7cSFred Isaman 10361da177e4SLinus Torvalds /* 10371da177e4SLinus Torvalds * nfs_scan_commit - Scan an inode for commit requests 10381da177e4SLinus Torvalds * @inode: NFS inode to scan 1039ea2cf228SFred Isaman * @dst: mds destination list 1040ea2cf228SFred Isaman * @cinfo: mds and ds lists of reqs ready to commit 10411da177e4SLinus Torvalds * 10421da177e4SLinus Torvalds * Moves requests from the inode's 'commit' request list. 10431da177e4SLinus Torvalds * The requests are *not* checked to ensure that they form a contiguous set. 10441da177e4SLinus Torvalds */ 10451763da12SFred Isaman int 1046ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst, 1047ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 10481da177e4SLinus Torvalds { 1049d6d6dc7cSFred Isaman int ret = 0; 1050fb8a1f11STrond Myklebust 1051fe238e60SDave Wysochanski spin_lock(&cinfo->inode->i_lock); 1052ea2cf228SFred Isaman if (cinfo->mds->ncommit > 0) { 10538dd37758STrond Myklebust const int max = INT_MAX; 1054d6d6dc7cSFred Isaman 1055ea2cf228SFred Isaman ret = nfs_scan_commit_list(&cinfo->mds->list, dst, 1056ea2cf228SFred Isaman cinfo, max); 1057ea2cf228SFred Isaman ret += pnfs_scan_commit_lists(inode, cinfo, max - ret); 1058d6d6dc7cSFred Isaman } 1059fe238e60SDave Wysochanski spin_unlock(&cinfo->inode->i_lock); 1060ff778d02STrond Myklebust return ret; 10611da177e4SLinus Torvalds } 1062d6d6dc7cSFred Isaman 10631da177e4SLinus Torvalds /* 1064e7d39069STrond Myklebust * Search for an existing write request, and attempt to update 1065e7d39069STrond Myklebust * it to reflect a new dirty region on a given page. 10661da177e4SLinus Torvalds * 1067e7d39069STrond Myklebust * If the attempt fails, then the existing request is flushed out 1068e7d39069STrond Myklebust * to disk. 10691da177e4SLinus Torvalds */ 1070e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode, 1071e7d39069STrond Myklebust struct page *page, 1072e7d39069STrond Myklebust unsigned int offset, 1073e7d39069STrond Myklebust unsigned int bytes) 10741da177e4SLinus Torvalds { 1075e7d39069STrond Myklebust struct nfs_page *req; 1076e7d39069STrond Myklebust unsigned int rqend; 1077e7d39069STrond Myklebust unsigned int end; 10781da177e4SLinus Torvalds int error; 1079277459d2STrond Myklebust 1080e7d39069STrond Myklebust end = offset + bytes; 1081e7d39069STrond Myklebust 1082f6032f21STrond Myklebust req = nfs_lock_and_join_requests(page); 1083f6032f21STrond Myklebust if (IS_ERR_OR_NULL(req)) 1084f6032f21STrond Myklebust return req; 10852bfc6e56SWeston Andros Adamson 1086e7d39069STrond Myklebust rqend = req->wb_offset + req->wb_bytes; 1087e7d39069STrond Myklebust /* 1088e7d39069STrond Myklebust * Tell the caller to flush out the request if 1089e7d39069STrond Myklebust * the offsets are non-contiguous. 1090e7d39069STrond Myklebust * Note: nfs_flush_incompatible() will already 1091e7d39069STrond Myklebust * have flushed out requests having wrong owners. 1092e7d39069STrond Myklebust */ 1093f6032f21STrond Myklebust if (offset > rqend || end < req->wb_offset) 1094e7d39069STrond Myklebust goto out_flushme; 1095e7d39069STrond Myklebust 10961da177e4SLinus Torvalds /* Okay, the request matches. Update the region */ 10971da177e4SLinus Torvalds if (offset < req->wb_offset) { 10981da177e4SLinus Torvalds req->wb_offset = offset; 10991da177e4SLinus Torvalds req->wb_pgbase = offset; 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds if (end > rqend) 11021da177e4SLinus Torvalds req->wb_bytes = end - req->wb_offset; 1103e7d39069STrond Myklebust else 1104e7d39069STrond Myklebust req->wb_bytes = rqend - req->wb_offset; 1105e7d39069STrond Myklebust return req; 1106e7d39069STrond Myklebust out_flushme: 1107f6032f21STrond Myklebust /* 1108f6032f21STrond Myklebust * Note: we mark the request dirty here because 1109f6032f21STrond Myklebust * nfs_lock_and_join_requests() cannot preserve 1110f6032f21STrond Myklebust * commit flags, so we have to replay the write. 1111f6032f21STrond Myklebust */ 1112f6032f21STrond Myklebust nfs_mark_request_dirty(req); 1113f6032f21STrond Myklebust nfs_unlock_and_release_request(req); 1114e7d39069STrond Myklebust error = nfs_wb_page(inode, page); 1115f6032f21STrond Myklebust return (error < 0) ? ERR_PTR(error) : NULL; 1116e7d39069STrond Myklebust } 11171da177e4SLinus Torvalds 1118e7d39069STrond Myklebust /* 1119e7d39069STrond Myklebust * Try to update an existing write request, or create one if there is none. 1120e7d39069STrond Myklebust * 1121e7d39069STrond Myklebust * Note: Should always be called with the Page Lock held to prevent races 1122e7d39069STrond Myklebust * if we have to add a new request. Also assumes that the caller has 1123e7d39069STrond Myklebust * already called nfs_flush_incompatible() if necessary. 1124e7d39069STrond Myklebust */ 1125e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, 1126e7d39069STrond Myklebust struct page *page, unsigned int offset, unsigned int bytes) 1127e7d39069STrond Myklebust { 1128d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 1129e7d39069STrond Myklebust struct nfs_page *req; 1130e7d39069STrond Myklebust 1131e7d39069STrond Myklebust req = nfs_try_to_update_request(inode, page, offset, bytes); 1132e7d39069STrond Myklebust if (req != NULL) 1133e7d39069STrond Myklebust goto out; 11342bfc6e56SWeston Andros Adamson req = nfs_create_request(ctx, page, NULL, offset, bytes); 1135e7d39069STrond Myklebust if (IS_ERR(req)) 1136e7d39069STrond Myklebust goto out; 1137d6d6dc7cSFred Isaman nfs_inode_add_request(inode, req); 1138efc91ed0STrond Myklebust out: 113961e930a9STrond Myklebust return req; 11401da177e4SLinus Torvalds } 11411da177e4SLinus Torvalds 1142e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, 1143e7d39069STrond Myklebust unsigned int offset, unsigned int count) 1144e7d39069STrond Myklebust { 1145e7d39069STrond Myklebust struct nfs_page *req; 1146e7d39069STrond Myklebust 1147e7d39069STrond Myklebust req = nfs_setup_write_request(ctx, page, offset, count); 1148e7d39069STrond Myklebust if (IS_ERR(req)) 1149e7d39069STrond Myklebust return PTR_ERR(req); 1150e7d39069STrond Myklebust /* Update file length */ 1151e7d39069STrond Myklebust nfs_grow_file(page, offset, count); 1152d72ddcbaSWeston Andros Adamson nfs_mark_uptodate(req); 1153a6305ddbSTrond Myklebust nfs_mark_request_dirty(req); 11541d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 1155e7d39069STrond Myklebust return 0; 1156e7d39069STrond Myklebust } 1157e7d39069STrond Myklebust 11581da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page) 11591da177e4SLinus Torvalds { 1160cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 11612a369153STrond Myklebust struct nfs_lock_context *l_ctx; 1162bd61e0a9SJeff Layton struct file_lock_context *flctx = file_inode(file)->i_flctx; 11631da177e4SLinus Torvalds struct nfs_page *req; 11641a54533eSTrond Myklebust int do_flush, status; 11651da177e4SLinus Torvalds /* 11661da177e4SLinus Torvalds * Look for a request corresponding to this page. If there 11671da177e4SLinus Torvalds * is one, and it belongs to another file, we flush it out 11681da177e4SLinus Torvalds * before we try to copy anything into the page. Do this 11691da177e4SLinus Torvalds * due to the lack of an ACCESS-type call in NFSv2. 11701da177e4SLinus Torvalds * Also do the same if we find a request from an existing 11711da177e4SLinus Torvalds * dropped page. 11721da177e4SLinus Torvalds */ 11731a54533eSTrond Myklebust do { 117484d3a9a9SWeston Andros Adamson req = nfs_page_find_head_request(page); 11751a54533eSTrond Myklebust if (req == NULL) 11761a54533eSTrond Myklebust return 0; 11772a369153STrond Myklebust l_ctx = req->wb_lock_context; 1178138a2935STrond Myklebust do_flush = req->wb_page != page || 1179138a2935STrond Myklebust !nfs_match_open_context(req->wb_context, ctx); 1180bd61e0a9SJeff Layton if (l_ctx && flctx && 1181bd61e0a9SJeff Layton !(list_empty_careful(&flctx->flc_posix) && 1182bd61e0a9SJeff Layton list_empty_careful(&flctx->flc_flock))) { 1183d51fdb87SNeilBrown do_flush |= l_ctx->lockowner != current->files; 11845263e31eSJeff Layton } 11851da177e4SLinus Torvalds nfs_release_request(req); 11861a54533eSTrond Myklebust if (!do_flush) 11871a54533eSTrond Myklebust return 0; 1188d56b4ddfSMel Gorman status = nfs_wb_page(page_file_mapping(page)->host, page); 11891a54533eSTrond Myklebust } while (status == 0); 11901a54533eSTrond Myklebust return status; 11911da177e4SLinus Torvalds } 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds /* 1194dc24826bSAndy Adamson * Avoid buffered writes when a open context credential's key would 1195dc24826bSAndy Adamson * expire soon. 1196dc24826bSAndy Adamson * 1197dc24826bSAndy Adamson * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL. 1198dc24826bSAndy Adamson * 1199dc24826bSAndy Adamson * Return 0 and set a credential flag which triggers the inode to flush 1200dc24826bSAndy Adamson * and performs NFS_FILE_SYNC writes if the key will expired within 1201dc24826bSAndy Adamson * RPC_KEY_EXPIRE_TIMEO. 1202dc24826bSAndy Adamson */ 1203dc24826bSAndy Adamson int 1204dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode) 1205dc24826bSAndy Adamson { 1206dc24826bSAndy Adamson struct nfs_open_context *ctx = nfs_file_open_context(filp); 1207dc24826bSAndy Adamson struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth; 1208dc24826bSAndy Adamson 1209dc24826bSAndy Adamson return rpcauth_key_timeout_notify(auth, ctx->cred); 1210dc24826bSAndy Adamson } 1211dc24826bSAndy Adamson 1212dc24826bSAndy Adamson /* 1213dc24826bSAndy Adamson * Test if the open context credential key is marked to expire soon. 1214dc24826bSAndy Adamson */ 1215ce52914eSScott Mayhew bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode) 1216dc24826bSAndy Adamson { 1217ce52914eSScott Mayhew struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth; 1218ce52914eSScott Mayhew 1219ce52914eSScott Mayhew return rpcauth_cred_key_to_expire(auth, ctx->cred); 1220dc24826bSAndy Adamson } 1221dc24826bSAndy Adamson 1222dc24826bSAndy Adamson /* 12235d47a356STrond Myklebust * If the page cache is marked as unsafe or invalid, then we can't rely on 12245d47a356STrond Myklebust * the PageUptodate() flag. In this case, we will need to turn off 12255d47a356STrond Myklebust * write optimisations that depend on the page contents being correct. 12265d47a356STrond Myklebust */ 12278d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode) 12285d47a356STrond Myklebust { 1229d529ef83SJeff Layton struct nfs_inode *nfsi = NFS_I(inode); 1230d529ef83SJeff Layton 12318d197a56STrond Myklebust if (nfs_have_delegated_attributes(inode)) 12328d197a56STrond Myklebust goto out; 123318dd78c4SScott Mayhew if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) 1234d529ef83SJeff Layton return false; 12354db72b40SJeff Layton smp_rmb(); 1236d529ef83SJeff Layton if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags)) 12378d197a56STrond Myklebust return false; 12388d197a56STrond Myklebust out: 123918dd78c4SScott Mayhew if (nfsi->cache_validity & NFS_INO_INVALID_DATA) 124018dd78c4SScott Mayhew return false; 12418d197a56STrond Myklebust return PageUptodate(page) != 0; 12425d47a356STrond Myklebust } 12435d47a356STrond Myklebust 12445263e31eSJeff Layton static bool 12455263e31eSJeff Layton is_whole_file_wrlock(struct file_lock *fl) 12465263e31eSJeff Layton { 12475263e31eSJeff Layton return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX && 12485263e31eSJeff Layton fl->fl_type == F_WRLCK; 12495263e31eSJeff Layton } 12505263e31eSJeff Layton 1251c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or 1252c7559663SScott Mayhew * if we have the whole file locked for writing), it may be more efficient to 1253c7559663SScott Mayhew * extend the write to cover the entire page in order to avoid fragmentation 1254c7559663SScott Mayhew * inefficiencies. 1255c7559663SScott Mayhew * 1256263b4509SScott Mayhew * If the file is opened for synchronous writes then we can just skip the rest 1257263b4509SScott Mayhew * of the checks. 1258c7559663SScott Mayhew */ 1259c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode) 1260c7559663SScott Mayhew { 12615263e31eSJeff Layton int ret; 12625263e31eSJeff Layton struct file_lock_context *flctx = inode->i_flctx; 12635263e31eSJeff Layton struct file_lock *fl; 12645263e31eSJeff Layton 1265c7559663SScott Mayhew if (file->f_flags & O_DSYNC) 1266c7559663SScott Mayhew return 0; 1267263b4509SScott Mayhew if (!nfs_write_pageuptodate(page, inode)) 1268263b4509SScott Mayhew return 0; 1269c7559663SScott Mayhew if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE)) 1270c7559663SScott Mayhew return 1; 1271bd61e0a9SJeff Layton if (!flctx || (list_empty_careful(&flctx->flc_flock) && 1272bd61e0a9SJeff Layton list_empty_careful(&flctx->flc_posix))) 12738fa4592aSTrond Myklebust return 1; 12745263e31eSJeff Layton 12755263e31eSJeff Layton /* Check to see if there are whole file write locks */ 12765263e31eSJeff Layton ret = 0; 12776109c850SJeff Layton spin_lock(&flctx->flc_lock); 1278bd61e0a9SJeff Layton if (!list_empty(&flctx->flc_posix)) { 1279bd61e0a9SJeff Layton fl = list_first_entry(&flctx->flc_posix, struct file_lock, 1280bd61e0a9SJeff Layton fl_list); 1281bd61e0a9SJeff Layton if (is_whole_file_wrlock(fl)) 12825263e31eSJeff Layton ret = 1; 1283bd61e0a9SJeff Layton } else if (!list_empty(&flctx->flc_flock)) { 12845263e31eSJeff Layton fl = list_first_entry(&flctx->flc_flock, struct file_lock, 12855263e31eSJeff Layton fl_list); 12865263e31eSJeff Layton if (fl->fl_type == F_WRLCK) 12875263e31eSJeff Layton ret = 1; 12885263e31eSJeff Layton } 12896109c850SJeff Layton spin_unlock(&flctx->flc_lock); 12905263e31eSJeff Layton return ret; 1291c7559663SScott Mayhew } 1292c7559663SScott Mayhew 12935d47a356STrond Myklebust /* 12941da177e4SLinus Torvalds * Update and possibly write a cached page of an NFS file. 12951da177e4SLinus Torvalds * 12961da177e4SLinus Torvalds * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad 12971da177e4SLinus Torvalds * things with a page scheduled for an RPC call (e.g. invalidate it). 12981da177e4SLinus Torvalds */ 12991da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page, 13001da177e4SLinus Torvalds unsigned int offset, unsigned int count) 13011da177e4SLinus Torvalds { 1302cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 1303d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 13041da177e4SLinus Torvalds int status = 0; 13051da177e4SLinus Torvalds 130691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 130791d5b470SChuck Lever 13086de1472fSAl Viro dprintk("NFS: nfs_updatepage(%pD2 %d@%lld)\n", 13096de1472fSAl Viro file, count, (long long)(page_file_offset(page) + offset)); 13101da177e4SLinus Torvalds 1311149a4fddSBenjamin Coddington if (!count) 1312149a4fddSBenjamin Coddington goto out; 1313149a4fddSBenjamin Coddington 1314c7559663SScott Mayhew if (nfs_can_extend_write(file, page, inode)) { 131549a70f27STrond Myklebust count = max(count + offset, nfs_page_length(page)); 13161da177e4SLinus Torvalds offset = 0; 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds 1319e21195a7STrond Myklebust status = nfs_writepage_setup(ctx, page, offset, count); 132003fa9e84STrond Myklebust if (status < 0) 132103fa9e84STrond Myklebust nfs_set_pageerror(page); 132259b7c05fSTrond Myklebust else 132359b7c05fSTrond Myklebust __set_page_dirty_nobuffers(page); 1324149a4fddSBenjamin Coddington out: 132548186c7dSChuck Lever dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", 13261da177e4SLinus Torvalds status, (long long)i_size_read(inode)); 13271da177e4SLinus Torvalds return status; 13281da177e4SLinus Torvalds } 13291da177e4SLinus Torvalds 13303ff7576dSTrond Myklebust static int flush_task_priority(int how) 13311da177e4SLinus Torvalds { 13321da177e4SLinus Torvalds switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { 13331da177e4SLinus Torvalds case FLUSH_HIGHPRI: 13341da177e4SLinus Torvalds return RPC_PRIORITY_HIGH; 13351da177e4SLinus Torvalds case FLUSH_LOWPRI: 13361da177e4SLinus Torvalds return RPC_PRIORITY_LOW; 13371da177e4SLinus Torvalds } 13381da177e4SLinus Torvalds return RPC_PRIORITY_NORMAL; 13391da177e4SLinus Torvalds } 13401da177e4SLinus Torvalds 1341d45f60c6SWeston Andros Adamson static void nfs_initiate_write(struct nfs_pgio_header *hdr, 1342d45f60c6SWeston Andros Adamson struct rpc_message *msg, 1343abde71f4STom Haynes const struct nfs_rpc_ops *rpc_ops, 13441ed26f33SAnna Schumaker struct rpc_task_setup *task_setup_data, int how) 13451da177e4SLinus Torvalds { 13463ff7576dSTrond Myklebust int priority = flush_task_priority(how); 13471da177e4SLinus Torvalds 13481ed26f33SAnna Schumaker task_setup_data->priority = priority; 1349abde71f4STom Haynes rpc_ops->write_setup(hdr, msg); 1350d138d5d1SAndy Adamson 1351abde71f4STom Haynes nfs4_state_protect_write(NFS_SERVER(hdr->inode)->nfs_client, 1352d45f60c6SWeston Andros Adamson &task_setup_data->rpc_client, msg, hdr); 1353275acaafSTrond Myklebust } 1354275acaafSTrond Myklebust 13556d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and 13566d884e8fSFred * call this on each, which will prepare them to be retried on next 13576d884e8fSFred * writeback using standard nfs. 13586d884e8fSFred */ 13596d884e8fSFred static void nfs_redirty_request(struct nfs_page *req) 13606d884e8fSFred { 13616d884e8fSFred nfs_mark_request_dirty(req); 1362c7070113STrond Myklebust set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags); 136320633f04SWeston Andros Adamson nfs_end_page_writeback(req); 13643aff4ebbSTrond Myklebust nfs_release_request(req); 13656d884e8fSFred } 13666d884e8fSFred 1367061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head) 13686c75dc0dSFred Isaman { 13696c75dc0dSFred Isaman struct nfs_page *req; 13706c75dc0dSFred Isaman 13716c75dc0dSFred Isaman while (!list_empty(head)) { 13726c75dc0dSFred Isaman req = nfs_list_entry(head->next); 13736c75dc0dSFred Isaman nfs_list_remove_request(req); 13746c75dc0dSFred Isaman nfs_redirty_request(req); 13756c75dc0dSFred Isaman } 13766c75dc0dSFred Isaman } 13776c75dc0dSFred Isaman 1378dc602dd7STrond Myklebust static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr) 1379dc602dd7STrond Myklebust { 1380dc602dd7STrond Myklebust nfs_async_write_error(&hdr->pages); 1381dc602dd7STrond Myklebust } 1382dc602dd7STrond Myklebust 1383061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = { 1384919e3bd9STrond Myklebust .init_hdr = nfs_async_write_init, 1385061ae2edSFred Isaman .error_cleanup = nfs_async_write_error, 1386061ae2edSFred Isaman .completion = nfs_write_completion, 1387dc602dd7STrond Myklebust .reschedule_io = nfs_async_write_reschedule_io, 1388061ae2edSFred Isaman }; 1389061ae2edSFred Isaman 139057208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, 1391a20c93e3SChristoph Hellwig struct inode *inode, int ioflags, bool force_mds, 1392061ae2edSFred Isaman const struct nfs_pgio_completion_ops *compl_ops) 13931751c363STrond Myklebust { 1394a20c93e3SChristoph Hellwig struct nfs_server *server = NFS_SERVER(inode); 139541d8d5b7SAnna Schumaker const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops; 1396a20c93e3SChristoph Hellwig 1397a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1 1398a20c93e3SChristoph Hellwig if (server->pnfs_curr_ld && !force_mds) 1399a20c93e3SChristoph Hellwig pg_ops = server->pnfs_curr_ld->pg_write_ops; 1400a20c93e3SChristoph Hellwig #endif 14014a0de55cSAnna Schumaker nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops, 1402fbe77c30SBenjamin Coddington server->wsize, ioflags, GFP_NOIO); 14031751c363STrond Myklebust } 1404ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write); 14051751c363STrond Myklebust 1406dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio) 1407dce81290STrond Myklebust { 1408a7d42ddbSWeston Andros Adamson struct nfs_pgio_mirror *mirror; 1409a7d42ddbSWeston Andros Adamson 14106f29b9bbSKinglong Mee if (pgio->pg_ops && pgio->pg_ops->pg_cleanup) 14116f29b9bbSKinglong Mee pgio->pg_ops->pg_cleanup(pgio); 14126f29b9bbSKinglong Mee 141341d8d5b7SAnna Schumaker pgio->pg_ops = &nfs_pgio_rw_ops; 1414a7d42ddbSWeston Andros Adamson 1415a7d42ddbSWeston Andros Adamson nfs_pageio_stop_mirroring(pgio); 1416a7d42ddbSWeston Andros Adamson 1417a7d42ddbSWeston Andros Adamson mirror = &pgio->pg_mirrors[0]; 1418a7d42ddbSWeston Andros Adamson mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize; 1419dce81290STrond Myklebust } 14201f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds); 1421dce81290STrond Myklebust 14221da177e4SLinus Torvalds 14230b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata) 14240b7c0153SFred Isaman { 14250b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 14260b7c0153SFred Isaman 14270b7c0153SFred Isaman NFS_PROTO(data->inode)->commit_rpc_prepare(task, data); 14280b7c0153SFred Isaman } 14290b7c0153SFred Isaman 14301f2edbe3STrond Myklebust /* 14311f2edbe3STrond Myklebust * Special version of should_remove_suid() that ignores capabilities. 14321f2edbe3STrond Myklebust */ 14331f2edbe3STrond Myklebust static int nfs_should_remove_suid(const struct inode *inode) 14341f2edbe3STrond Myklebust { 14351f2edbe3STrond Myklebust umode_t mode = inode->i_mode; 14361f2edbe3STrond Myklebust int kill = 0; 1437788e7a89STrond Myklebust 14381f2edbe3STrond Myklebust /* suid always must be killed */ 14391f2edbe3STrond Myklebust if (unlikely(mode & S_ISUID)) 14401f2edbe3STrond Myklebust kill = ATTR_KILL_SUID; 14411f2edbe3STrond Myklebust 14421f2edbe3STrond Myklebust /* 14431f2edbe3STrond Myklebust * sgid without any exec bits is just a mandatory locking mark; leave 14441f2edbe3STrond Myklebust * it alone. If some exec bits are set, it's a real sgid; kill it. 14451f2edbe3STrond Myklebust */ 14461f2edbe3STrond Myklebust if (unlikely((mode & S_ISGID) && (mode & S_IXGRP))) 14471f2edbe3STrond Myklebust kill |= ATTR_KILL_SGID; 14481f2edbe3STrond Myklebust 14491f2edbe3STrond Myklebust if (unlikely(kill && S_ISREG(mode))) 14501f2edbe3STrond Myklebust return kill; 14511f2edbe3STrond Myklebust 14521f2edbe3STrond Myklebust return 0; 14531f2edbe3STrond Myklebust } 1454788e7a89STrond Myklebust 1455a08a8cd3STrond Myklebust static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr, 1456a08a8cd3STrond Myklebust struct nfs_fattr *fattr) 1457a08a8cd3STrond Myklebust { 1458a08a8cd3STrond Myklebust struct nfs_pgio_args *argp = &hdr->args; 1459a08a8cd3STrond Myklebust struct nfs_pgio_res *resp = &hdr->res; 14602b83d3deSTrond Myklebust u64 size = argp->offset + resp->count; 1461a08a8cd3STrond Myklebust 1462a08a8cd3STrond Myklebust if (!(fattr->valid & NFS_ATTR_FATTR_SIZE)) 14632b83d3deSTrond Myklebust fattr->size = size; 14642b83d3deSTrond Myklebust if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) { 14652b83d3deSTrond Myklebust fattr->valid &= ~NFS_ATTR_FATTR_SIZE; 1466a08a8cd3STrond Myklebust return; 14672b83d3deSTrond Myklebust } 14682b83d3deSTrond Myklebust if (size != fattr->size) 1469a08a8cd3STrond Myklebust return; 1470a08a8cd3STrond Myklebust /* Set attribute barrier */ 1471a08a8cd3STrond Myklebust nfs_fattr_set_barrier(fattr); 14722b83d3deSTrond Myklebust /* ...and update size */ 14732b83d3deSTrond Myklebust fattr->valid |= NFS_ATTR_FATTR_SIZE; 1474a08a8cd3STrond Myklebust } 1475a08a8cd3STrond Myklebust 1476a08a8cd3STrond Myklebust void nfs_writeback_update_inode(struct nfs_pgio_header *hdr) 1477a08a8cd3STrond Myklebust { 14782b83d3deSTrond Myklebust struct nfs_fattr *fattr = &hdr->fattr; 1479a08a8cd3STrond Myklebust struct inode *inode = hdr->inode; 1480a08a8cd3STrond Myklebust 1481a08a8cd3STrond Myklebust spin_lock(&inode->i_lock); 1482a08a8cd3STrond Myklebust nfs_writeback_check_extend(hdr, fattr); 1483a08a8cd3STrond Myklebust nfs_post_op_update_inode_force_wcc_locked(inode, fattr); 1484a08a8cd3STrond Myklebust spin_unlock(&inode->i_lock); 1485a08a8cd3STrond Myklebust } 1486a08a8cd3STrond Myklebust EXPORT_SYMBOL_GPL(nfs_writeback_update_inode); 1487a08a8cd3STrond Myklebust 14881da177e4SLinus Torvalds /* 14891da177e4SLinus Torvalds * This function is called when the WRITE call is complete. 14901da177e4SLinus Torvalds */ 1491d45f60c6SWeston Andros Adamson static int nfs_writeback_done(struct rpc_task *task, 1492d45f60c6SWeston Andros Adamson struct nfs_pgio_header *hdr, 14930eecb214SAnna Schumaker struct inode *inode) 14941da177e4SLinus Torvalds { 1495788e7a89STrond Myklebust int status; 14961da177e4SLinus Torvalds 1497f551e44fSChuck Lever /* 1498f551e44fSChuck Lever * ->write_done will attempt to use post-op attributes to detect 1499f551e44fSChuck Lever * conflicting writes by other clients. A strict interpretation 1500f551e44fSChuck Lever * of close-to-open would allow us to continue caching even if 1501f551e44fSChuck Lever * another writer had changed the file, but some applications 1502f551e44fSChuck Lever * depend on tighter cache coherency when writing. 1503f551e44fSChuck Lever */ 1504d45f60c6SWeston Andros Adamson status = NFS_PROTO(inode)->write_done(task, hdr); 1505788e7a89STrond Myklebust if (status != 0) 15060eecb214SAnna Schumaker return status; 1507d45f60c6SWeston Andros Adamson nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count); 150891d5b470SChuck Lever 1509d45f60c6SWeston Andros Adamson if (hdr->res.verf->committed < hdr->args.stable && 1510d45f60c6SWeston Andros Adamson task->tk_status >= 0) { 15111da177e4SLinus Torvalds /* We tried a write call, but the server did not 15121da177e4SLinus Torvalds * commit data to stable storage even though we 15131da177e4SLinus Torvalds * requested it. 15141da177e4SLinus Torvalds * Note: There is a known bug in Tru64 < 5.0 in which 15151da177e4SLinus Torvalds * the server reports NFS_DATA_SYNC, but performs 15161da177e4SLinus Torvalds * NFS_FILE_SYNC. We therefore implement this checking 15171da177e4SLinus Torvalds * as a dprintk() in order to avoid filling syslog. 15181da177e4SLinus Torvalds */ 15191da177e4SLinus Torvalds static unsigned long complain; 15201da177e4SLinus Torvalds 1521a69aef14SFred Isaman /* Note this will print the MDS for a DS write */ 15221da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 15231da177e4SLinus Torvalds dprintk("NFS: faulty NFS server %s:" 15241da177e4SLinus Torvalds " (committed = %d) != (stable = %d)\n", 1525cd841605SFred Isaman NFS_SERVER(inode)->nfs_client->cl_hostname, 1526d45f60c6SWeston Andros Adamson hdr->res.verf->committed, hdr->args.stable); 15271da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 15281da177e4SLinus Torvalds } 15291da177e4SLinus Torvalds } 15301f2edbe3STrond Myklebust 15311f2edbe3STrond Myklebust /* Deal with the suid/sgid bit corner case */ 15321f2edbe3STrond Myklebust if (nfs_should_remove_suid(inode)) 15331f2edbe3STrond Myklebust nfs_mark_for_revalidate(inode); 15340eecb214SAnna Schumaker return 0; 15350eecb214SAnna Schumaker } 15360eecb214SAnna Schumaker 15370eecb214SAnna Schumaker /* 15380eecb214SAnna Schumaker * This function is called when the WRITE call is complete. 15390eecb214SAnna Schumaker */ 1540d45f60c6SWeston Andros Adamson static void nfs_writeback_result(struct rpc_task *task, 1541d45f60c6SWeston Andros Adamson struct nfs_pgio_header *hdr) 15420eecb214SAnna Schumaker { 1543d45f60c6SWeston Andros Adamson struct nfs_pgio_args *argp = &hdr->args; 1544d45f60c6SWeston Andros Adamson struct nfs_pgio_res *resp = &hdr->res; 15451f2edbe3STrond Myklebust 15461f2edbe3STrond Myklebust if (resp->count < argp->count) { 15471da177e4SLinus Torvalds static unsigned long complain; 15481da177e4SLinus Torvalds 15496c75dc0dSFred Isaman /* This a short write! */ 1550d45f60c6SWeston Andros Adamson nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE); 155191d5b470SChuck Lever 15521da177e4SLinus Torvalds /* Has the server at least made some progress? */ 15536c75dc0dSFred Isaman if (resp->count == 0) { 15546c75dc0dSFred Isaman if (time_before(complain, jiffies)) { 15556c75dc0dSFred Isaman printk(KERN_WARNING 15566c75dc0dSFred Isaman "NFS: Server wrote zero bytes, expected %u.\n", 15576c75dc0dSFred Isaman argp->count); 15586c75dc0dSFred Isaman complain = jiffies + 300 * HZ; 15596c75dc0dSFred Isaman } 1560d45f60c6SWeston Andros Adamson nfs_set_pgio_error(hdr, -EIO, argp->offset); 15616c75dc0dSFred Isaman task->tk_status = -EIO; 15626c75dc0dSFred Isaman return; 15636c75dc0dSFred Isaman } 1564f8417b48SKinglong Mee 1565f8417b48SKinglong Mee /* For non rpc-based layout drivers, retry-through-MDS */ 1566f8417b48SKinglong Mee if (!task->tk_ops) { 1567f8417b48SKinglong Mee hdr->pnfs_error = -EAGAIN; 1568f8417b48SKinglong Mee return; 1569f8417b48SKinglong Mee } 1570f8417b48SKinglong Mee 15711da177e4SLinus Torvalds /* Was this an NFSv2 write or an NFSv3 stable write? */ 15721da177e4SLinus Torvalds if (resp->verf->committed != NFS_UNSTABLE) { 15731da177e4SLinus Torvalds /* Resend from where the server left off */ 1574d45f60c6SWeston Andros Adamson hdr->mds_offset += resp->count; 15751da177e4SLinus Torvalds argp->offset += resp->count; 15761da177e4SLinus Torvalds argp->pgbase += resp->count; 15771da177e4SLinus Torvalds argp->count -= resp->count; 15781da177e4SLinus Torvalds } else { 15791da177e4SLinus Torvalds /* Resend as a stable write in order to avoid 15801da177e4SLinus Torvalds * headaches in the case of a server crash. 15811da177e4SLinus Torvalds */ 15821da177e4SLinus Torvalds argp->stable = NFS_FILE_SYNC; 15831da177e4SLinus Torvalds } 1584d00c5d43STrond Myklebust rpc_restart_call_prepare(task); 15851da177e4SLinus Torvalds } 15861da177e4SLinus Torvalds } 15871da177e4SLinus Torvalds 1588af7cf057STrond Myklebust static int wait_on_commit(struct nfs_mds_commit_info *cinfo) 158971d0a611STrond Myklebust { 1590af7cf057STrond Myklebust return wait_on_atomic_t(&cinfo->rpcs_out, 1591af7cf057STrond Myklebust nfs_wait_atomic_killable, TASK_KILLABLE); 1592af7cf057STrond Myklebust } 1593af7cf057STrond Myklebust 1594af7cf057STrond Myklebust static void nfs_commit_begin(struct nfs_mds_commit_info *cinfo) 1595af7cf057STrond Myklebust { 1596af7cf057STrond Myklebust atomic_inc(&cinfo->rpcs_out); 1597af7cf057STrond Myklebust } 1598af7cf057STrond Myklebust 1599af7cf057STrond Myklebust static void nfs_commit_end(struct nfs_mds_commit_info *cinfo) 1600af7cf057STrond Myklebust { 1601af7cf057STrond Myklebust if (atomic_dec_and_test(&cinfo->rpcs_out)) 1602af7cf057STrond Myklebust wake_up_atomic_t(&cinfo->rpcs_out); 160371d0a611STrond Myklebust } 160471d0a611STrond Myklebust 16050b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data) 16061da177e4SLinus Torvalds { 16070b7c0153SFred Isaman put_nfs_open_context(data->context); 16080b7c0153SFred Isaman nfs_commit_free(data); 16091da177e4SLinus Torvalds } 1610e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release); 16111da177e4SLinus Torvalds 16120b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data, 1613c36aae9aSPeng Tao const struct nfs_rpc_ops *nfs_ops, 16149ace33cdSFred Isaman const struct rpc_call_ops *call_ops, 16159f0ec176SAndy Adamson int how, int flags) 16161da177e4SLinus Torvalds { 161707737691STrond Myklebust struct rpc_task *task; 16189ace33cdSFred Isaman int priority = flush_task_priority(how); 1619bdc7f021STrond Myklebust struct rpc_message msg = { 1620bdc7f021STrond Myklebust .rpc_argp = &data->args, 1621bdc7f021STrond Myklebust .rpc_resp = &data->res, 16229ace33cdSFred Isaman .rpc_cred = data->cred, 1623bdc7f021STrond Myklebust }; 162484115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 162507737691STrond Myklebust .task = &data->task, 16269ace33cdSFred Isaman .rpc_client = clnt, 1627bdc7f021STrond Myklebust .rpc_message = &msg, 16289ace33cdSFred Isaman .callback_ops = call_ops, 162984115e1cSTrond Myklebust .callback_data = data, 1630101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 16319f0ec176SAndy Adamson .flags = RPC_TASK_ASYNC | flags, 16323ff7576dSTrond Myklebust .priority = priority, 163384115e1cSTrond Myklebust }; 1634788e7a89STrond Myklebust /* Set up the initial task struct. */ 1635c36aae9aSPeng Tao nfs_ops->commit_setup(data, &msg); 16361da177e4SLinus Torvalds 1637b4839ebeSKinglong Mee dprintk("NFS: initiated commit call\n"); 1638bdc7f021STrond Myklebust 16398c21c62cSWeston Andros Adamson nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client, 16408c21c62cSWeston Andros Adamson NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg); 16418c21c62cSWeston Andros Adamson 164207737691STrond Myklebust task = rpc_run_task(&task_setup_data); 1643dbae4c73STrond Myklebust if (IS_ERR(task)) 1644dbae4c73STrond Myklebust return PTR_ERR(task); 1645d2224e7aSJeff Layton if (how & FLUSH_SYNC) 1646d2224e7aSJeff Layton rpc_wait_for_completion_task(task); 164707737691STrond Myklebust rpc_put_task(task); 1648dbae4c73STrond Myklebust return 0; 16491da177e4SLinus Torvalds } 1650e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit); 16511da177e4SLinus Torvalds 1652378520b8SPeng Tao static loff_t nfs_get_lwb(struct list_head *head) 1653378520b8SPeng Tao { 1654378520b8SPeng Tao loff_t lwb = 0; 1655378520b8SPeng Tao struct nfs_page *req; 1656378520b8SPeng Tao 1657378520b8SPeng Tao list_for_each_entry(req, head, wb_list) 1658378520b8SPeng Tao if (lwb < (req_offset(req) + req->wb_bytes)) 1659378520b8SPeng Tao lwb = req_offset(req) + req->wb_bytes; 1660378520b8SPeng Tao 1661378520b8SPeng Tao return lwb; 1662378520b8SPeng Tao } 1663378520b8SPeng Tao 16641da177e4SLinus Torvalds /* 16659ace33cdSFred Isaman * Set up the argument/result storage required for the RPC call. 16669ace33cdSFred Isaman */ 16670b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data, 1668988b6dceSFred Isaman struct list_head *head, 1669f453a54aSFred Isaman struct pnfs_layout_segment *lseg, 1670f453a54aSFred Isaman struct nfs_commit_info *cinfo) 16719ace33cdSFred Isaman { 16729ace33cdSFred Isaman struct nfs_page *first = nfs_list_entry(head->next); 16732b0143b5SDavid Howells struct inode *inode = d_inode(first->wb_context->dentry); 16749ace33cdSFred Isaman 16759ace33cdSFred Isaman /* Set up the RPC argument and reply structs 16769ace33cdSFred Isaman * NB: take care not to mess about with data->commit et al. */ 16779ace33cdSFred Isaman 16789ace33cdSFred Isaman list_splice_init(head, &data->pages); 16799ace33cdSFred Isaman 16809ace33cdSFred Isaman data->inode = inode; 16819ace33cdSFred Isaman data->cred = first->wb_context->cred; 1682988b6dceSFred Isaman data->lseg = lseg; /* reference transferred */ 1683378520b8SPeng Tao /* only set lwb for pnfs commit */ 1684378520b8SPeng Tao if (lseg) 1685378520b8SPeng Tao data->lwb = nfs_get_lwb(&data->pages); 16869ace33cdSFred Isaman data->mds_ops = &nfs_commit_ops; 1687f453a54aSFred Isaman data->completion_ops = cinfo->completion_ops; 1688b359f9d0SFred Isaman data->dreq = cinfo->dreq; 16899ace33cdSFred Isaman 16909ace33cdSFred Isaman data->args.fh = NFS_FH(data->inode); 16919ace33cdSFred Isaman /* Note: we always request a commit of the entire inode */ 16929ace33cdSFred Isaman data->args.offset = 0; 16939ace33cdSFred Isaman data->args.count = 0; 16940b7c0153SFred Isaman data->context = get_nfs_open_context(first->wb_context); 16959ace33cdSFred Isaman data->res.fattr = &data->fattr; 16969ace33cdSFred Isaman data->res.verf = &data->verf; 16979ace33cdSFred Isaman nfs_fattr_init(&data->fattr); 16989ace33cdSFred Isaman } 1699e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit); 17009ace33cdSFred Isaman 1701e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list, 1702ea2cf228SFred Isaman struct pnfs_layout_segment *lseg, 1703b57ff130SWeston Andros Adamson struct nfs_commit_info *cinfo, 1704b57ff130SWeston Andros Adamson u32 ds_commit_idx) 170564bfeb49SFred Isaman { 170664bfeb49SFred Isaman struct nfs_page *req; 170764bfeb49SFred Isaman 170864bfeb49SFred Isaman while (!list_empty(page_list)) { 170964bfeb49SFred Isaman req = nfs_list_entry(page_list->next); 171064bfeb49SFred Isaman nfs_list_remove_request(req); 1711b57ff130SWeston Andros Adamson nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx); 1712487b9b8aSTom Haynes if (!cinfo->dreq) 1713487b9b8aSTom Haynes nfs_clear_page_commit(req->wb_page); 17141d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 171564bfeb49SFred Isaman } 171664bfeb49SFred Isaman } 1717e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit); 171864bfeb49SFred Isaman 1719b20135d0STrond Myklebust static void 1720b20135d0STrond Myklebust nfs_commit_resched_write(struct nfs_commit_info *cinfo, 1721b20135d0STrond Myklebust struct nfs_page *req) 1722b20135d0STrond Myklebust { 1723b20135d0STrond Myklebust __set_page_dirty_nobuffers(req->wb_page); 1724b20135d0STrond Myklebust } 1725b20135d0STrond Myklebust 17269ace33cdSFred Isaman /* 17271da177e4SLinus Torvalds * Commit dirty pages 17281da177e4SLinus Torvalds */ 17291da177e4SLinus Torvalds static int 1730ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how, 1731ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 17321da177e4SLinus Torvalds { 17330b7c0153SFred Isaman struct nfs_commit_data *data; 17341da177e4SLinus Torvalds 1735ade8febdSWeston Andros Adamson /* another commit raced with us */ 1736ade8febdSWeston Andros Adamson if (list_empty(head)) 1737ade8febdSWeston Andros Adamson return 0; 1738ade8febdSWeston Andros Adamson 1739518662e0SNeilBrown data = nfs_commitdata_alloc(true); 17401da177e4SLinus Torvalds 17411da177e4SLinus Torvalds /* Set up the argument struct */ 1742f453a54aSFred Isaman nfs_init_commit(data, head, NULL, cinfo); 1743f453a54aSFred Isaman atomic_inc(&cinfo->mds->rpcs_out); 1744c36aae9aSPeng Tao return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode), 1745c36aae9aSPeng Tao data->mds_ops, how, 0); 17461da177e4SLinus Torvalds } 17471da177e4SLinus Torvalds 17481da177e4SLinus Torvalds /* 17491da177e4SLinus Torvalds * COMMIT call returned 17501da177e4SLinus Torvalds */ 1751788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata) 17521da177e4SLinus Torvalds { 17530b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 17541da177e4SLinus Torvalds 1755a3f565b1SChuck Lever dprintk("NFS: %5u nfs_commit_done (status %d)\n", 17561da177e4SLinus Torvalds task->tk_pid, task->tk_status); 17571da177e4SLinus Torvalds 1758788e7a89STrond Myklebust /* Call the NFS version-specific code */ 1759c0d0e96bSTrond Myklebust NFS_PROTO(data->inode)->commit_done(task, data); 1760c9d8f89dSTrond Myklebust } 1761c9d8f89dSTrond Myklebust 1762f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data) 1763c9d8f89dSTrond Myklebust { 1764c9d8f89dSTrond Myklebust struct nfs_page *req; 1765c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1766f453a54aSFred Isaman struct nfs_commit_info cinfo; 1767353db796SNeilBrown struct nfs_server *nfss; 1768788e7a89STrond Myklebust 17691da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 17701da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 17711da177e4SLinus Torvalds nfs_list_remove_request(req); 177267911c8fSAnna Schumaker if (req->wb_page) 1773d6d6dc7cSFred Isaman nfs_clear_page_commit(req->wb_page); 17741da177e4SLinus Torvalds 17751e8968c5SNiels de Vos dprintk("NFS: commit (%s/%llu %d@%lld)", 17763d4ff43dSAl Viro req->wb_context->dentry->d_sb->s_id, 17772b0143b5SDavid Howells (unsigned long long)NFS_FILEID(d_inode(req->wb_context->dentry)), 17781da177e4SLinus Torvalds req->wb_bytes, 17791da177e4SLinus Torvalds (long long)req_offset(req)); 1780c9d8f89dSTrond Myklebust if (status < 0) { 1781c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 178238a33101SKinglong Mee if (req->wb_page) 17831da177e4SLinus Torvalds nfs_inode_remove_request(req); 1784ddeaa637SJoe Perches dprintk_cont(", error = %d\n", status); 17851da177e4SLinus Torvalds goto next; 17861da177e4SLinus Torvalds } 17871da177e4SLinus Torvalds 17881da177e4SLinus Torvalds /* Okay, COMMIT succeeded, apparently. Check the verifier 17891da177e4SLinus Torvalds * returned by the server against all stored verfs. */ 17908fc3c386STrond Myklebust if (!nfs_write_verifier_cmp(&req->wb_verf, &data->verf.verifier)) { 17911da177e4SLinus Torvalds /* We have a match */ 179238a33101SKinglong Mee if (req->wb_page) 17931da177e4SLinus Torvalds nfs_inode_remove_request(req); 1794ddeaa637SJoe Perches dprintk_cont(" OK\n"); 17951da177e4SLinus Torvalds goto next; 17961da177e4SLinus Torvalds } 17971da177e4SLinus Torvalds /* We have a mismatch. Write the page again */ 1798ddeaa637SJoe Perches dprintk_cont(" mismatch\n"); 17996d884e8fSFred nfs_mark_request_dirty(req); 180005990d1bSTrond Myklebust set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags); 18011da177e4SLinus Torvalds next: 18021d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 18031da177e4SLinus Torvalds } 1804353db796SNeilBrown nfss = NFS_SERVER(data->inode); 1805353db796SNeilBrown if (atomic_long_read(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) 18060db10944SJan Kara clear_bdi_congested(inode_to_bdi(data->inode), BLK_RW_ASYNC); 1807353db796SNeilBrown 1808f453a54aSFred Isaman nfs_init_cinfo(&cinfo, data->inode, data->dreq); 1809af7cf057STrond Myklebust nfs_commit_end(cinfo.mds); 18105917ce84SFred Isaman } 18115917ce84SFred Isaman 18125917ce84SFred Isaman static void nfs_commit_release(void *calldata) 18135917ce84SFred Isaman { 18140b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 18155917ce84SFred Isaman 1816f453a54aSFred Isaman data->completion_ops->completion(data); 1817c9d8f89dSTrond Myklebust nfs_commitdata_release(calldata); 18181da177e4SLinus Torvalds } 1819788e7a89STrond Myklebust 1820788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = { 18210b7c0153SFred Isaman .rpc_call_prepare = nfs_commit_prepare, 1822788e7a89STrond Myklebust .rpc_call_done = nfs_commit_done, 1823788e7a89STrond Myklebust .rpc_release = nfs_commit_release, 1824788e7a89STrond Myklebust }; 18251da177e4SLinus Torvalds 1826f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = { 1827f453a54aSFred Isaman .completion = nfs_commit_release_pages, 1828b20135d0STrond Myklebust .resched_write = nfs_commit_resched_write, 1829f453a54aSFred Isaman }; 1830f453a54aSFred Isaman 18311763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head, 1832ea2cf228SFred Isaman int how, struct nfs_commit_info *cinfo) 183384c53ab5SFred Isaman { 183484c53ab5SFred Isaman int status; 183584c53ab5SFred Isaman 1836ea2cf228SFred Isaman status = pnfs_commit_list(inode, head, how, cinfo); 183784c53ab5SFred Isaman if (status == PNFS_NOT_ATTEMPTED) 1838ea2cf228SFred Isaman status = nfs_commit_list(inode, head, how, cinfo); 183984c53ab5SFred Isaman return status; 184084c53ab5SFred Isaman } 184184c53ab5SFred Isaman 1842b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how) 18431da177e4SLinus Torvalds { 18441da177e4SLinus Torvalds LIST_HEAD(head); 1845ea2cf228SFred Isaman struct nfs_commit_info cinfo; 184671d0a611STrond Myklebust int may_wait = how & FLUSH_SYNC; 1847af7cf057STrond Myklebust int error = 0; 1848b8413f98STrond Myklebust int res; 18491da177e4SLinus Torvalds 1850ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, inode); 1851af7cf057STrond Myklebust nfs_commit_begin(cinfo.mds); 1852ea2cf228SFred Isaman res = nfs_scan_commit(inode, &head, &cinfo); 1853af7cf057STrond Myklebust if (res) 1854ea2cf228SFred Isaman error = nfs_generic_commit_list(inode, &head, how, &cinfo); 1855af7cf057STrond Myklebust nfs_commit_end(cinfo.mds); 18561da177e4SLinus Torvalds if (error < 0) 1857af7cf057STrond Myklebust goto out_error; 1858b8413f98STrond Myklebust if (!may_wait) 1859b8413f98STrond Myklebust goto out_mark_dirty; 1860af7cf057STrond Myklebust error = wait_on_commit(cinfo.mds); 1861b8413f98STrond Myklebust if (error < 0) 1862b8413f98STrond Myklebust return error; 1863c5efa5fcSTrond Myklebust return res; 1864af7cf057STrond Myklebust out_error: 1865af7cf057STrond Myklebust res = error; 1866c5efa5fcSTrond Myklebust /* Note: If we exit without ensuring that the commit is complete, 1867c5efa5fcSTrond Myklebust * we must mark the inode as dirty. Otherwise, future calls to 1868c5efa5fcSTrond Myklebust * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure 1869c5efa5fcSTrond Myklebust * that the data is on the disk. 1870c5efa5fcSTrond Myklebust */ 1871c5efa5fcSTrond Myklebust out_mark_dirty: 1872c5efa5fcSTrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 18731da177e4SLinus Torvalds return res; 18741da177e4SLinus Torvalds } 1875b20135d0STrond Myklebust EXPORT_SYMBOL_GPL(nfs_commit_inode); 18768fc795f7STrond Myklebust 1877ae09c31fSAnna Schumaker int nfs_write_inode(struct inode *inode, struct writeback_control *wbc) 18788fc795f7STrond Myklebust { 1879420e3646STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 1880420e3646STrond Myklebust int flags = FLUSH_SYNC; 1881420e3646STrond Myklebust int ret = 0; 18828fc795f7STrond Myklebust 18833236c3e1SJeff Layton /* no commits means nothing needs to be done */ 1884ea2cf228SFred Isaman if (!nfsi->commit_info.ncommit) 18853236c3e1SJeff Layton return ret; 18863236c3e1SJeff Layton 1887a00dd6c0SJeff Layton if (wbc->sync_mode == WB_SYNC_NONE) { 1888a00dd6c0SJeff Layton /* Don't commit yet if this is a non-blocking flush and there 1889a00dd6c0SJeff Layton * are a lot of outstanding writes for this mapping. 1890420e3646STrond Myklebust */ 18911a4edf0fSTrond Myklebust if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK)) 1892420e3646STrond Myklebust goto out_mark_dirty; 1893420e3646STrond Myklebust 1894a00dd6c0SJeff Layton /* don't wait for the COMMIT response */ 1895420e3646STrond Myklebust flags = 0; 1896a00dd6c0SJeff Layton } 1897a00dd6c0SJeff Layton 1898420e3646STrond Myklebust ret = nfs_commit_inode(inode, flags); 1899420e3646STrond Myklebust if (ret >= 0) { 1900420e3646STrond Myklebust if (wbc->sync_mode == WB_SYNC_NONE) { 1901420e3646STrond Myklebust if (ret < wbc->nr_to_write) 1902420e3646STrond Myklebust wbc->nr_to_write -= ret; 1903420e3646STrond Myklebust else 1904420e3646STrond Myklebust wbc->nr_to_write = 0; 1905420e3646STrond Myklebust } 19068fc795f7STrond Myklebust return 0; 1907420e3646STrond Myklebust } 1908420e3646STrond Myklebust out_mark_dirty: 19098fc795f7STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 19108fc795f7STrond Myklebust return ret; 19118fc795f7STrond Myklebust } 191289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode); 1913863a3c6cSAndy Adamson 1914acdc53b2STrond Myklebust /* 1915837bb1d7STrond Myklebust * Wrapper for filemap_write_and_wait_range() 1916837bb1d7STrond Myklebust * 1917837bb1d7STrond Myklebust * Needed for pNFS in order to ensure data becomes visible to the 1918837bb1d7STrond Myklebust * client. 1919837bb1d7STrond Myklebust */ 1920837bb1d7STrond Myklebust int nfs_filemap_write_and_wait_range(struct address_space *mapping, 1921837bb1d7STrond Myklebust loff_t lstart, loff_t lend) 1922837bb1d7STrond Myklebust { 1923837bb1d7STrond Myklebust int ret; 1924837bb1d7STrond Myklebust 1925837bb1d7STrond Myklebust ret = filemap_write_and_wait_range(mapping, lstart, lend); 1926837bb1d7STrond Myklebust if (ret == 0) 1927837bb1d7STrond Myklebust ret = pnfs_sync_inode(mapping->host, true); 1928837bb1d7STrond Myklebust return ret; 1929837bb1d7STrond Myklebust } 1930837bb1d7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range); 1931837bb1d7STrond Myklebust 1932837bb1d7STrond Myklebust /* 1933acdc53b2STrond Myklebust * flush the inode to disk. 1934acdc53b2STrond Myklebust */ 1935acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode) 193634901f70STrond Myklebust { 1937f4ce1299STrond Myklebust int ret; 193834901f70STrond Myklebust 1939f4ce1299STrond Myklebust trace_nfs_writeback_inode_enter(inode); 1940f4ce1299STrond Myklebust 19415bb89b47STrond Myklebust ret = filemap_write_and_wait(inode->i_mapping); 19426b196875SChuck Lever if (ret) 19436b196875SChuck Lever goto out; 19445bb89b47STrond Myklebust ret = nfs_commit_inode(inode, FLUSH_SYNC); 19456b196875SChuck Lever if (ret < 0) 19466b196875SChuck Lever goto out; 19475bb89b47STrond Myklebust pnfs_sync_inode(inode, true); 19486b196875SChuck Lever ret = 0; 1949f4ce1299STrond Myklebust 19506b196875SChuck Lever out: 1951f4ce1299STrond Myklebust trace_nfs_writeback_inode_exit(inode, ret); 1952f4ce1299STrond Myklebust return ret; 19531c75950bSTrond Myklebust } 1954ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all); 19551c75950bSTrond Myklebust 19561b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page) 19571b3b4a1aSTrond Myklebust { 19581b3b4a1aSTrond Myklebust struct nfs_page *req; 19591b3b4a1aSTrond Myklebust int ret = 0; 19601b3b4a1aSTrond Myklebust 1961ba8b06e6STrond Myklebust wait_on_page_writeback(page); 19623e217045SWeston Andros Adamson 19633e217045SWeston Andros Adamson /* blocking call to cancel all requests and join to a single (head) 19643e217045SWeston Andros Adamson * request */ 19656d17d653STrond Myklebust req = nfs_lock_and_join_requests(page); 19663e217045SWeston Andros Adamson 19673e217045SWeston Andros Adamson if (IS_ERR(req)) { 19683e217045SWeston Andros Adamson ret = PTR_ERR(req); 19693e217045SWeston Andros Adamson } else if (req) { 19703e217045SWeston Andros Adamson /* all requests from this page have been cancelled by 19713e217045SWeston Andros Adamson * nfs_lock_and_join_requests, so just remove the head 19723e217045SWeston Andros Adamson * request from the inode / page_private pointer and 19733e217045SWeston Andros Adamson * release it */ 19741b3b4a1aSTrond Myklebust nfs_inode_remove_request(req); 19751d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 19761b3b4a1aSTrond Myklebust } 19773e217045SWeston Andros Adamson 19781b3b4a1aSTrond Myklebust return ret; 19791b3b4a1aSTrond Myklebust } 19801b3b4a1aSTrond Myklebust 19811c75950bSTrond Myklebust /* 19821c75950bSTrond Myklebust * Write back all requests on one page - we do this before reading it. 19831c75950bSTrond Myklebust */ 1984c373fff7STrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page) 19851c75950bSTrond Myklebust { 198629418aa4SMel Gorman loff_t range_start = page_file_offset(page); 198709cbfeafSKirill A. Shutemov loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1); 19887f2f12d9STrond Myklebust struct writeback_control wbc = { 19897f2f12d9STrond Myklebust .sync_mode = WB_SYNC_ALL, 19907f2f12d9STrond Myklebust .nr_to_write = 0, 19917f2f12d9STrond Myklebust .range_start = range_start, 19927f2f12d9STrond Myklebust .range_end = range_end, 19937f2f12d9STrond Myklebust }; 19947f2f12d9STrond Myklebust int ret; 19957f2f12d9STrond Myklebust 1996f4ce1299STrond Myklebust trace_nfs_writeback_page_enter(inode); 1997f4ce1299STrond Myklebust 19980522f6adSTrond Myklebust for (;;) { 1999ba8b06e6STrond Myklebust wait_on_page_writeback(page); 20007f2f12d9STrond Myklebust if (clear_page_dirty_for_io(page)) { 2001c373fff7STrond Myklebust ret = nfs_writepage_locked(page, &wbc); 20027f2f12d9STrond Myklebust if (ret < 0) 20037f2f12d9STrond Myklebust goto out_error; 20040522f6adSTrond Myklebust continue; 20057f2f12d9STrond Myklebust } 2006f4ce1299STrond Myklebust ret = 0; 20070522f6adSTrond Myklebust if (!PagePrivate(page)) 20080522f6adSTrond Myklebust break; 20090522f6adSTrond Myklebust ret = nfs_commit_inode(inode, FLUSH_SYNC); 20107f2f12d9STrond Myklebust if (ret < 0) 20117f2f12d9STrond Myklebust goto out_error; 20127f2f12d9STrond Myklebust } 20137f2f12d9STrond Myklebust out_error: 2014f4ce1299STrond Myklebust trace_nfs_writeback_page_exit(inode, ret); 20157f2f12d9STrond Myklebust return ret; 20161c75950bSTrond Myklebust } 20171c75950bSTrond Myklebust 2018074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION 2019074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage, 2020a6bc32b8SMel Gorman struct page *page, enum migrate_mode mode) 2021074cc1deSTrond Myklebust { 20222da95652SJeff Layton /* 20232da95652SJeff Layton * If PagePrivate is set, then the page is currently associated with 20242da95652SJeff Layton * an in-progress read or write request. Don't try to migrate it. 20252da95652SJeff Layton * 20262da95652SJeff Layton * FIXME: we could do this in principle, but we'll need a way to ensure 20272da95652SJeff Layton * that we can safely release the inode reference while holding 20282da95652SJeff Layton * the page lock. 20292da95652SJeff Layton */ 20302da95652SJeff Layton if (PagePrivate(page)) 20312da95652SJeff Layton return -EBUSY; 2032074cc1deSTrond Myklebust 20338c209ce7SDavid Howells if (!nfs_fscache_release_page(page, GFP_KERNEL)) 20348c209ce7SDavid Howells return -EBUSY; 2035074cc1deSTrond Myklebust 2036a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 2037074cc1deSTrond Myklebust } 2038074cc1deSTrond Myklebust #endif 2039074cc1deSTrond Myklebust 2040f7b422b1SDavid Howells int __init nfs_init_writepagecache(void) 20411da177e4SLinus Torvalds { 20421da177e4SLinus Torvalds nfs_wdata_cachep = kmem_cache_create("nfs_write_data", 20431e7f3a48SWeston Andros Adamson sizeof(struct nfs_pgio_header), 20441da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 204520c2df83SPaul Mundt NULL); 20461da177e4SLinus Torvalds if (nfs_wdata_cachep == NULL) 20471da177e4SLinus Torvalds return -ENOMEM; 20481da177e4SLinus Torvalds 204993d2341cSMatthew Dobson nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, 20501da177e4SLinus Torvalds nfs_wdata_cachep); 20511da177e4SLinus Torvalds if (nfs_wdata_mempool == NULL) 20523dd4765fSJeff Layton goto out_destroy_write_cache; 20531da177e4SLinus Torvalds 20540b7c0153SFred Isaman nfs_cdata_cachep = kmem_cache_create("nfs_commit_data", 20550b7c0153SFred Isaman sizeof(struct nfs_commit_data), 20560b7c0153SFred Isaman 0, SLAB_HWCACHE_ALIGN, 20570b7c0153SFred Isaman NULL); 20580b7c0153SFred Isaman if (nfs_cdata_cachep == NULL) 20593dd4765fSJeff Layton goto out_destroy_write_mempool; 20600b7c0153SFred Isaman 206193d2341cSMatthew Dobson nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, 20624c100210SYanchuan Nian nfs_cdata_cachep); 20631da177e4SLinus Torvalds if (nfs_commit_mempool == NULL) 20643dd4765fSJeff Layton goto out_destroy_commit_cache; 20651da177e4SLinus Torvalds 206689a09141SPeter Zijlstra /* 206789a09141SPeter Zijlstra * NFS congestion size, scale with available memory. 206889a09141SPeter Zijlstra * 206989a09141SPeter Zijlstra * 64MB: 8192k 207089a09141SPeter Zijlstra * 128MB: 11585k 207189a09141SPeter Zijlstra * 256MB: 16384k 207289a09141SPeter Zijlstra * 512MB: 23170k 207389a09141SPeter Zijlstra * 1GB: 32768k 207489a09141SPeter Zijlstra * 2GB: 46340k 207589a09141SPeter Zijlstra * 4GB: 65536k 207689a09141SPeter Zijlstra * 8GB: 92681k 207789a09141SPeter Zijlstra * 16GB: 131072k 207889a09141SPeter Zijlstra * 207989a09141SPeter Zijlstra * This allows larger machines to have larger/more transfers. 208089a09141SPeter Zijlstra * Limit the default to 256M 208189a09141SPeter Zijlstra */ 208289a09141SPeter Zijlstra nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); 208389a09141SPeter Zijlstra if (nfs_congestion_kb > 256*1024) 208489a09141SPeter Zijlstra nfs_congestion_kb = 256*1024; 208589a09141SPeter Zijlstra 20861da177e4SLinus Torvalds return 0; 20873dd4765fSJeff Layton 20883dd4765fSJeff Layton out_destroy_commit_cache: 20893dd4765fSJeff Layton kmem_cache_destroy(nfs_cdata_cachep); 20903dd4765fSJeff Layton out_destroy_write_mempool: 20913dd4765fSJeff Layton mempool_destroy(nfs_wdata_mempool); 20923dd4765fSJeff Layton out_destroy_write_cache: 20933dd4765fSJeff Layton kmem_cache_destroy(nfs_wdata_cachep); 20943dd4765fSJeff Layton return -ENOMEM; 20951da177e4SLinus Torvalds } 20961da177e4SLinus Torvalds 2097266bee88SDavid Brownell void nfs_destroy_writepagecache(void) 20981da177e4SLinus Torvalds { 20991da177e4SLinus Torvalds mempool_destroy(nfs_commit_mempool); 21003dd4765fSJeff Layton kmem_cache_destroy(nfs_cdata_cachep); 21011da177e4SLinus Torvalds mempool_destroy(nfs_wdata_mempool); 21021a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_wdata_cachep); 21031da177e4SLinus Torvalds } 21041da177e4SLinus Torvalds 21054a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = { 21064a0de55cSAnna Schumaker .rw_alloc_header = nfs_writehdr_alloc, 21074a0de55cSAnna Schumaker .rw_free_header = nfs_writehdr_free, 21080eecb214SAnna Schumaker .rw_done = nfs_writeback_done, 21090eecb214SAnna Schumaker .rw_result = nfs_writeback_result, 21101ed26f33SAnna Schumaker .rw_initiate = nfs_initiate_write, 21114a0de55cSAnna Schumaker }; 2112